From 17a902ece2457c8cb0ee70c320bf0583c0f9a4ce Mon Sep 17 00:00:00 2001 From: Lee Wei Date: Mon, 23 Oct 2017 02:59:42 +0800 Subject: [PATCH] added more (unverified) code --- 100.cpp | 86 ++- 1000.cpp | 75 +++ 10000.cpp | 79 +++ 10001.cpp | 105 ++++ 10002.cpp | 119 +++++ 10003.cpp | 116 ++-- 10004.cpp | 91 ++++ 10005.cpp | 165 ++++++ 10006.cpp | 132 ++++- 10007.cpp | 57 ++ 10008.cpp | 111 ++-- 10009.cpp | 114 ++++ 1001.cpp | 87 +++ 10010.cpp | 301 ++++++++--- 10011.cpp | 193 +++++++ 10012.cpp | 93 ++++ 10013.cpp | 75 +-- 10014.cpp | 45 +- 10015.cpp | 106 ++-- 10016.cpp | 106 ++++ 10017.cpp | 123 +++++ 10018.cpp | 96 ++-- 10019.cpp | 54 +- 10020.cpp | 57 ++ 10021.cpp | 395 ++++++++++++++ 10022.cpp | 151 ++++++ 10023.cpp | 100 +++- 10024.cpp | 179 +++++++ 10025.cpp | 139 +++-- 10026.cpp | 52 ++ 10027.cpp | 128 +++++ 10028.cpp | 158 ++++++ 10029.cpp | 114 ++++ 10030.cpp | 97 ++++ 10031.cpp | 109 ++++ 10032.cpp | 130 +++++ 10033.cpp | 99 ++++ 10034.cpp | 100 ++++ 10035.cpp | 124 +++-- 10036.cpp | 73 +++ 10037.cpp | 91 ++++ 10038.cpp | 95 ++-- 10039.cpp | 134 +++++ 10040.cpp | 68 +++ 10041.cpp | 71 ++- 10042.cpp | 89 ++++ 10043.cpp | 169 ++++++ 10044.cpp | 115 ++++ 10045.cpp | 100 ++++ 10047.cpp | 133 +++-- 10048.cpp | 138 ++--- 10049.cpp | 67 +++ 10050.cpp | 75 +-- 10051.cpp | 101 ++++ 10053.cpp | 131 +++++ 10054.cpp | 116 ++++ 10055.cpp | 29 +- 10056.cpp | 48 ++ 10057.cpp | 61 +++ 10058.cpp | 138 +++++ 1006.cpp | 220 ++++++++ 10060.cpp | 42 ++ 10061.cpp | 116 ++++ 10062.cpp | 96 ++-- 10063.cpp | 55 ++ 10065.cpp | 122 +++++ 10066.cpp | 93 ++-- 10067.cpp | 136 +++++ 10068.cpp | 168 ++++++ 10069.cpp | 290 ++++++++++ 10070.cpp | 123 ++++- 10071.cpp | 13 + 10073.cpp | 113 ++++ 10074.cpp | 137 +++-- 10075.cpp | 113 ++++ 10077.cpp | 39 ++ 10078.cpp | 143 +++++ 10079.cpp | 37 +- 1008.cpp | 216 ++++++++ 10080.cpp | 164 ++++++ 10081.cpp | 49 ++ 10082.cpp | 40 +- 10083.cpp | 179 +++++++ 10084.cpp | 201 +++++++ 10085.cpp | 74 +++ 10086.cpp | 62 +++ 10087.cpp | 157 ++++++ 10088.cpp | 85 +++ 10089.cpp | 136 +++++ 1009.cpp | 117 +++++ 10090.cpp | 234 +++++++++ 10091.cpp | 126 +++++ 10092.cpp | 210 ++++++++ 10093.cpp | 84 +-- 10094.cpp | 115 ++++ 10095.cpp | 161 ++++++ 10097.cpp | 98 ++++ 10098.cpp | 58 +- 10099.cpp | 121 +++-- 101.cpp | 208 +++----- 10100.cpp | 197 ++++--- 10101.cpp | 116 ++-- 10102.cpp | 157 +++--- 10103.cpp | 231 ++++++++ 10104.cpp | 83 +-- 10105.cpp | 117 +++++ 10106.cpp | 110 ++++ 10107.cpp | 109 ++-- 10109.cpp | 274 ++++++++++ 1011.cpp | 72 +++ 10110.cpp | 44 +- 10111.cpp | 214 ++++++++ 10112.cpp | 107 ++++ 10113.cpp | 220 ++++++++ 10114.cpp | 46 ++ 10115.cpp | 101 ++-- 10116.cpp | 108 ++++ 10117.cpp | 169 ++++++ 10118.cpp | 79 +++ 10119.cpp | 268 ++++++++++ 10120.cpp | 72 +++ 10122.cpp | 153 ++++++ 10123.cpp | 134 +++++ 10124.cpp | 61 +++ 10125.cpp | 55 ++ 10126.cpp | 81 +++ 10127.cpp | 35 +- 10128.cpp | 39 ++ 10129.cpp | 111 ++++ 1013.cpp | 87 +++ 10130.cpp | 104 ++-- 10131.cpp | 118 +++++ 10132.cpp | 114 ++++ 10134.cpp | 37 ++ 10135.cpp | 228 ++++++++ 10136.cpp | 109 ++++ 10137.cpp | 40 ++ 10138.cpp | 224 ++++++++ 10139.cpp | 149 ++++++ 10140.cpp | 126 +++++ 10141.cpp | 65 +++ 10142.cpp | 106 ++++ 10144.cpp | 41 ++ 10145.cpp | 72 +++ 10146.cpp | 56 ++ 10147.cpp | 140 +++++ 10148.cpp | 80 +++ 10149.cpp | 209 ++++++++ 10150.cpp | 152 ++++++ 10152.cpp | 37 ++ 10154.cpp | 83 +++ 10155.cpp | 230 ++++++++ 10156.cpp | 150 ++++++ 10157.cpp | 129 +++++ 10158.cpp | 126 +++++ 10159.cpp | 132 +++++ 1016.cpp | 52 ++ 10160.cpp | 229 ++++++++ 10161.cpp | 110 ++-- 10162.cpp | 133 ++--- 10163.cpp | 83 +++ 10164.cpp | 81 +++ 10165.cpp | 19 + 10166.cpp | 170 ++++++ 10167.cpp | 66 +++ 10168.cpp | 114 ++-- 10169.cpp | 39 ++ 10170.cpp | 68 +++ 10171.cpp | 188 ++++--- 10172.cpp | 93 ++++ 10173.cpp | 281 ++++++++++ 10174.cpp | 75 +++ 10175.cpp | 72 +++ 10176.cpp | 53 ++ 10177.cpp | 60 +++ 10178.cpp | 87 +++ 10179.cpp | 46 ++ 1018.cpp | 232 ++++++++ 10180.cpp | 102 ++++ 10181.cpp | 190 +++++++ 10182.cpp | 97 ++++ 10183.cpp | 103 ++++ 10184.cpp | 70 +++ 10185.cpp | 46 ++ 10186.cpp | 149 ++++++ 10187.cpp | 165 ++++++ 10188.cpp | 81 +++ 10189.cpp | 136 +++-- 10190.cpp | 42 ++ 10191.cpp | 93 ++++ 10192.cpp | 72 +-- 10193.cpp | 120 ++--- 10194.cpp | 199 +++++++ 10195.cpp | 36 +- 10196.cpp | 137 +++++ 10197.cpp | 56 ++ 10198.cpp | 300 +++++++++++ 10199.cpp | 204 +++++++ 102.cpp | 67 ++- 10200.cpp | 74 +-- 10201.cpp | 85 +++ 10202.cpp | 171 ++++++ 10203.cpp | 40 ++ 10205.cpp | 115 ++++ 10206.cpp | 388 ++++++++++++++ 10207.cpp | 212 ++++++++ 10208.cpp | 120 +++++ 10209.cpp | 15 + 10210.cpp | 84 +++ 10211.cpp | 54 ++ 10212.cpp | 167 ++++++ 10213.cpp | 336 ++++++++++++ 10214.cpp | 61 +++ 10215.cpp | 30 ++ 10216.cpp | 67 +++ 10217.cpp | 18 + 10218.cpp | 30 ++ 10219.cpp | 18 + 10220.cpp | 58 ++ 10221.cpp | 26 + 10222.cpp | 44 +- 10223.cpp | 84 ++- 10224.cpp | 237 +++++++++ 10225.cpp | 117 +++++ 10226.cpp | 125 +++-- 10227.cpp | 108 ++++ 10228.cpp | 125 +++++ 10229.cpp | 95 ++-- 10230.cpp | 145 +++++ 10231.cpp | 234 +++++++++ 10232.cpp | 144 +++++ 10233.cpp | 26 + 10234.cpp | 98 ++++ 10235.cpp | 99 ++-- 10236.cpp | 98 ++++ 10237.cpp | 47 ++ 10239.cpp | 48 ++ 10241.cpp | 45 ++ 10242.cpp | 34 ++ 10243.cpp | 66 +++ 10245.cpp | 87 +-- 10246.cpp | 136 ++--- 10247.cpp | 156 ++++++ 10248.cpp | 169 ++++++ 10249.cpp | 99 ++++ 1025.cpp | 80 +++ 10250.cpp | 23 + 10251.cpp | 49 ++ 10252.cpp | 86 ++- 10253.cpp | 58 ++ 10254.cpp | 63 +++ 10256.cpp | 204 +++++++ 10257.cpp | 33 ++ 10258.cpp | 155 +++--- 10259.cpp | 70 +++ 1026.cpp | 37 ++ 10260.cpp | 50 +- 10261.cpp | 228 ++++++++ 10262.cpp | 119 +++++ 10263.cpp | 147 ++++++ 10264.cpp | 40 ++ 10266.cpp | 135 +++++ 10267.cpp | 145 +++++ 10268.cpp | 74 ++- 10269.cpp | 141 +++++ 1027.cpp | 103 ++++ 10270.cpp | 358 +++++++++++++ 10271.cpp | 49 ++ 10272.cpp | 93 ++++ 10273.cpp | 107 ++++ 10274.cpp | 262 +++++++++ 10275.cpp | 79 +++ 10276.cpp | 30 ++ 10277.cpp | 77 +++ 10278.cpp | 137 +++++ 10279.cpp | 158 ++++++ 10280.cpp | 64 +++ 10281.cpp | 77 +-- 10282.cpp | 133 +++-- 10283.cpp | 53 ++ 10284.cpp | 206 ++++++++ 10285.cpp | 81 +++ 10286.cpp | 16 +- 10287.cpp | 18 + 10288.cpp | 67 +++ 1029.cpp | 253 +++++++++ 10290.cpp | 66 +++ 10291.cpp | 90 ++++ 10293.cpp | 51 ++ 10294.cpp | 63 +++ 10295.cpp | 129 ++++- 10296.cpp | 173 ++++++ 10297.cpp | 37 +- 10298.cpp | 59 ++- 10299.cpp | 86 +++ 103.cpp | 113 ++++ 1030.cpp | 148 ++++++ 10300.cpp | 44 +- 10301.cpp | 92 ++++ 10302.cpp | 13 + 10303.cpp | 325 ++++++++++++ 10304.cpp | 46 ++ 10305.cpp | 146 +++++ 10306.cpp | 61 +++ 10307.cpp | 216 ++++++++ 10308.cpp | 144 +++++ 10309.cpp | 167 ++++++ 1031.cpp | 141 +++++ 10310.cpp | 82 +-- 10311.cpp | 166 +++--- 10312.cpp | 48 ++ 10313.cpp | 65 +++ 10315.cpp | 138 +++++ 10316.cpp | 98 ++++ 10318.cpp | 171 ++++++ 10319.cpp | 187 +++++++ 1032.cpp | 148 ++++++ 10320.cpp | 53 ++ 10321.cpp | 129 +++++ 10323.cpp | 92 ++-- 10324.cpp | 76 +-- 10325.cpp | 80 +++ 10326.cpp | 121 +++++ 10327.cpp | 31 ++ 10328.cpp | 68 +++ 10329.cpp | 336 ++++++++++++ 10330.cpp | 115 ++++ 10331.cpp | 116 ++++ 10333.cpp | 121 +++++ 10334.cpp | 61 ++- 10335.cpp | 181 +++++++ 10336.cpp | 175 +++--- 10337.cpp | 53 ++ 10338.cpp | 27 + 10339.cpp | 23 + 10340.cpp | 65 ++- 10341.cpp | 107 ++-- 10342.cpp | 121 +++++ 10344.cpp | 92 ++-- 10346.cpp | 37 +- 10347.cpp | 44 +- 10348.cpp | 186 +++++++ 10349.cpp | 101 ++++ 10350.cpp | 56 ++ 10351.cpp | 44 ++ 10352.cpp | 139 +++++ 10354.cpp | 187 +++++++ 10355.cpp | 99 ++++ 10356.cpp | 122 +++++ 10357.cpp | 72 +++ 10358.cpp | 284 ++++++++++ 10359.cpp | 86 +++ 10360.cpp | 126 ++--- 10361.cpp | 79 ++- 10362.cpp | 115 ++++ 10363.cpp | 190 ++++--- 10364.cpp | 117 +++++ 10365.cpp | 71 ++- 10366.cpp | 110 ++++ 10367.cpp | 346 ++++++++++++ 10368.cpp | 40 ++ 10369.cpp | 128 +++++ 10370.cpp | 68 +-- 10371.cpp | 156 ++++++ 10372.cpp | 52 ++ 10373.cpp | 83 +++ 10374.cpp | 144 ++--- 10375.cpp | 79 +++ 10377.cpp | 120 +++++ 10378.cpp | 116 ++++ 10379.cpp | 64 +++ 1038.cpp | 339 ++++++++++++ 10380.cpp | 161 ++++++ 10381.cpp | 124 +++++ 10382.cpp | 71 +++ 10384.cpp | 412 +++++++++++++++ 10385.cpp | 111 ++++ 10386.cpp | 25 + 10387.cpp | 27 + 10388.cpp | 94 ++++ 10389.cpp | 119 +++++ 1039.cpp | 124 +++++ 10390.cpp | 62 +++ 10391.cpp | 72 ++- 10392.cpp | 65 +++ 10393.cpp | 178 ++++--- 10394.cpp | 44 ++ 10396.cpp | 121 +++++ 10397.cpp | 135 +++++ 10398.cpp | 24 + 10399.cpp | 1144 ++++++++++++++++++++++++++++++++++++++++ 104.cpp | 183 ++++--- 1040.cpp | 188 +++++++ 10400.cpp | 148 ++++++ 10401.cpp | 73 +++ 10403.cpp | 175 ++++++ 10404.cpp | 69 +++ 10405.cpp | 67 ++- 10406.cpp | 91 ++++ 10407.cpp | 148 +++--- 10408.cpp | 28 + 10409.cpp | 80 +++ 1041.cpp | 155 ++++++ 10410.cpp | 107 ++++ 10411.cpp | 115 ++++ 10412.cpp | 88 ++++ 10413.cpp | 84 +++ 10415.cpp | 77 +++ 10416.cpp | 104 ++++ 10417.cpp | 94 ++++ 10418.cpp | 326 ++++++++++++ 10419.cpp | 148 ++++++ 1042.cpp | 49 ++ 10420.cpp | 67 ++- 10421.cpp | 106 ++++ 10422.cpp | 97 ++++ 10423.cpp | 69 +++ 10424.cpp | 103 ++-- 10425.cpp | 50 ++ 10426.cpp | 158 ++++++ 10427.cpp | 47 ++ 10428.cpp | 100 ++++ 10429.cpp | 41 ++ 1043.cpp | 136 +++++ 10430.cpp | 50 ++ 10431.cpp | 52 ++ 10432.cpp | 13 + 10433.cpp | 128 +++++ 10434.cpp | 81 +++ 10436.cpp | 159 ++++++ 10437.cpp | 195 +++++++ 10438.cpp | 58 ++ 10439.cpp | 36 ++ 1044.cpp | 160 ++++++ 10440.cpp | 90 ++-- 10441.cpp | 168 ++++++ 10442.cpp | 103 ++++ 10443.cpp | 106 ++++ 10444.cpp | 53 ++ 10445.cpp | 141 +++++ 10446.cpp | 51 ++ 10447.cpp | 105 ++++ 10448.cpp | 131 +++++ 10449.cpp | 110 ++++ 1045.cpp | 217 ++++++++ 10450.cpp | 54 ++ 10451.cpp | 25 + 10452.cpp | 56 ++ 10453.cpp | 119 +++++ 10454.cpp | 100 ++++ 10456.cpp | 106 ++++ 10457.cpp | 82 +++ 10458.cpp | 359 +++++++++++++ 10459.cpp | 173 ++++++ 1046.cpp | 62 +++ 10460.cpp | 86 +++ 10461.cpp | 94 ++++ 10462.cpp | 139 +++++ 10463.cpp | 201 +++++++ 10464.cpp | 320 +++++++++++ 10465.cpp | 59 +++ 10466.cpp | 56 ++ 10467.cpp | 201 +++++++ 10468.cpp | 16 + 10469.cpp | 26 +- 1047.cpp | 83 +++ 10471.cpp | 22 + 10473.cpp | 47 ++ 10474.cpp | 113 ++-- 10475.cpp | 119 +++++ 10476.cpp | 214 ++++++++ 10477.cpp | 243 +++++++++ 10479.cpp | 69 +++ 1048.cpp | 184 +++++++ 10480.cpp | 149 ++++++ 10482.cpp | 139 +++++ 10483.cpp | 82 +++ 10484.cpp | 144 +++++ 10486.cpp | 83 +++ 10487.cpp | 120 ++--- 10488.cpp | 39 ++ 10489.cpp | 57 +- 1049.cpp | 181 +++++++ 10490.cpp | 88 +++- 10491.cpp | 31 +- 10493.cpp | 96 ++-- 10494.cpp | 78 +++ 10495.cpp | 29 + 10496.cpp | 71 +++ 10497.cpp | 359 +++++++++++-- 10498.cpp | 164 ++++++ 10499.cpp | 19 + 105.cpp | 52 +- 10500.cpp | 104 ++++ 10501.cpp | 241 +++++++++ 10502.cpp | 122 +++++ 10503.cpp | 85 +++ 10504.cpp | 66 +++ 10505.cpp | 77 +++ 10506.cpp | 119 +++++ 10507.cpp | 119 +++++ 10508.cpp | 35 ++ 10509.cpp | 34 ++ 1051.cpp | 293 +++++++++++ 10510.cpp | 183 +++++++ 10511.cpp | 175 ++++++ 10512.cpp | 126 +++++ 10513.cpp | 107 ++++ 10514.cpp | 181 +++++++ 10515.cpp | 117 ++--- 10516.cpp | 190 +++++++ 10517.cpp | 48 ++ 10518.cpp | 37 ++ 10519.cpp | 70 +++ 1052.cpp | 73 +++ 10520.cpp | 60 +++ 10521.cpp | 351 +++++++++++++ 10522.cpp | 80 +++ 10523.cpp | 82 +++ 10524.cpp | 74 +++ 10525.cpp | 79 +++ 10526.cpp | 146 +++++ 10527.cpp | 124 +++++ 10528.cpp | 56 ++ 10529.cpp | 29 + 10530.cpp | 77 +++ 10531.cpp | 230 ++++++++ 10532.cpp | 96 ++++ 10533.cpp | 110 ++-- 10534.cpp | 110 ++++ 10535.cpp | 73 +++ 10536.cpp | 193 +++++++ 10537.cpp | 157 ++++++ 10539.cpp | 112 ++++ 1054.cpp | 115 ++++ 10540.cpp | 27 + 10541.cpp | 64 +++ 10542.cpp | 73 +++ 10543.cpp | 83 +++ 10544.cpp | 93 ++++ 10545.cpp | 28 + 10546.cpp | 104 ++++ 10547.cpp | 30 ++ 10548.cpp | 57 ++ 10549.cpp | 177 +++++++ 10550.cpp | 105 ++-- 10551.cpp | 49 ++ 10552.cpp | 138 +++++ 10553.cpp | 80 +++ 10554.cpp | 88 ++++ 10555.cpp | 41 ++ 10556.cpp | 48 ++ 10557.cpp | 95 ++++ 10558.cpp | 102 ++++ 10559.cpp | 63 +++ 1056.cpp | 85 +++ 10560.cpp | 101 ++++ 10561.cpp | 106 ++++ 10562.cpp | 63 +++ 10563.cpp | 111 ++++ 10564.cpp | 175 ++++++ 10565.cpp | 18 + 10566.cpp | 85 +++ 10567.cpp | 72 +++ 10568.cpp | 250 +++++++++ 10569.cpp | 125 +++++ 1057.cpp | 79 +++ 10570.cpp | 44 ++ 10571.cpp | 169 ++++++ 10572.cpp | 372 +++++++++++++ 10573.cpp | 71 +++ 10574.cpp | 125 +++++ 10575.cpp | 52 ++ 10576.cpp | 40 ++ 10577.cpp | 108 ++++ 10578.cpp | 83 +++ 10579.cpp | 38 ++ 1058.cpp | 87 +++ 10580.cpp | 107 ++++ 10581.cpp | 36 ++ 10582.cpp | 189 +++++++ 10583.cpp | 43 ++ 10584.cpp | 157 ++++++ 10585.cpp | 68 +++ 10586.cpp | 41 ++ 10587.cpp | 164 ++++++ 10588.cpp | 89 ++++ 10589.cpp | 32 ++ 1059.cpp | 89 ++++ 10590.cpp | 126 +++++ 10591.cpp | 52 +- 10592.cpp | 130 +++++ 10593.cpp | 145 +++++ 10594.cpp | 141 +++++ 10595.cpp | 244 +++++++++ 10596.cpp | 92 ++++ 10597.cpp | 83 +++ 10598.cpp | 23 + 10599.cpp | 140 +++++ 106.cpp | 129 +++-- 1060.cpp | 231 ++++++++ 10600.cpp | 130 +++++ 10601.cpp | 116 ++++ 10602.cpp | 94 ++++ 10603.cpp | 272 ++++++++++ 10604.cpp | 115 ++++ 10605.cpp | 143 +++++ 10606.cpp | 454 ++++++++++++++++ 10608.cpp | 40 ++ 10609.cpp | 119 +++++ 1061.cpp | 156 ++++++ 10610.cpp | 145 +++++ 10611.cpp | 98 +++- 10613.cpp | 123 +++++ 10614.cpp | 198 +++++++ 10615.cpp | 161 ++++++ 10616.cpp | 84 +++ 10617.cpp | 95 ++++ 10618.cpp | 107 ++++ 10619.cpp | 93 ++++ 1062.cpp | 49 ++ 10620.cpp | 66 +++ 10621.cpp | 133 +++++ 10622.cpp | 54 ++ 10623.cpp | 83 +++ 10624.cpp | 121 +++++ 10625.cpp | 166 ++++++ 10626.cpp | 63 +++ 10627.cpp | 75 +++ 10629.cpp | 134 +++++ 1063.cpp | 153 ++++++ 10631.cpp | 258 +++++++++ 10632.cpp | 119 +++++ 10633.cpp | 68 +-- 10634.cpp | 59 +++ 10635.cpp | 109 +++- 10637.cpp | 124 +++++ 10638.cpp | 116 ++++ 1064.cpp | 152 ++++++ 10640.cpp | 63 +++ 10641.cpp | 168 ++++++ 10642.cpp | 18 + 10643.cpp | 56 ++ 10644.cpp | 59 +++ 10645.cpp | 165 ++++++ 10646.cpp | 52 ++ 10647.cpp | 54 ++ 10648.cpp | 56 ++ 10649.cpp | 31 ++ 10650.cpp | 109 ++++ 10651.cpp | 105 ++++ 10652.cpp | 85 +++ 10653.cpp | 48 ++ 10654.cpp | 53 ++ 10655.cpp | 45 ++ 10656.cpp | 47 +- 10658.cpp | 24 + 10659.cpp | 109 ++++ 10660.cpp | 63 +++ 10662.cpp | 159 ++++++ 10663.cpp | 91 ++++ 10664.cpp | 61 +++ 10665.cpp | 101 ++++ 10666.cpp | 37 ++ 10667.cpp | 177 ++++--- 10668.cpp | 34 ++ 10669.cpp | 81 +++ 1067.cpp | 165 ++++++ 10670.cpp | 106 ++++ 10671.cpp | 196 +++++++ 10672.cpp | 124 +++++ 10673.cpp | 36 ++ 10674.cpp | 90 ++++ 10675.cpp | 159 ++++++ 10676.cpp | 115 ++++ 10677.cpp | 56 ++ 10678.cpp | 20 + 10679.cpp | 139 ++++- 1068.cpp | 188 +++++++ 10680.cpp | 104 ++++ 10681.cpp | 99 ++++ 10682.cpp | 171 ++++++ 10683.cpp | 40 +- 10684.cpp | 71 +-- 10685.cpp | 99 ++++ 10686.cpp | 91 ++++ 10687.cpp | 124 +++++ 10688.cpp | 30 ++ 10689.cpp | 77 +++ 1069.cpp | 216 ++++++++ 10690.cpp | 76 +++ 10691.cpp | 119 +++++ 10692.cpp | 51 ++ 10693.cpp | 21 + 10694.cpp | 99 ++++ 10696.cpp | 41 +- 10697.cpp | 92 ++++ 10698.cpp | 183 +++++++ 10699.cpp | 115 ++-- 107.cpp | 48 ++ 10700.cpp | 97 ++++ 10701.cpp | 124 +++++ 10702.cpp | 110 ++++ 10703.cpp | 121 +++-- 10704.cpp | 194 +++++++ 10705.cpp | 111 ++++ 10706.cpp | 199 +++++++ 10707.cpp | 230 ++++++++ 10708.cpp | 90 ++++ 10709.cpp | 148 ++++++ 10710.cpp | 56 ++ 10711.cpp | 110 ++++ 10712.cpp | 100 ++++ 10713.cpp | 122 +++++ 10714.cpp | 37 +- 10715.cpp | 86 +++ 10716.cpp | 144 +++++ 10717.cpp | 118 +++++ 10718.cpp | 101 +++- 10719.cpp | 69 +++ 10720.cpp | 78 +++ 10721.cpp | 27 + 10722.cpp | 304 +++++++++++ 10723.cpp | 84 +++ 10724.cpp | 139 +++++ 10725.cpp | 59 +++ 10726.cpp | 67 +++ 10727.cpp | 67 +++ 10728.cpp | 106 ++++ 10729.cpp | 185 +++++++ 1073.cpp | 25 + 10730.cpp | 42 ++ 10731.cpp | 143 +++++ 10732.cpp | 139 +++++ 10733.cpp | 18 + 10734.cpp | 57 ++ 10735.cpp | 147 ++++++ 10736.cpp | 40 ++ 10737.cpp | 228 ++++++++ 10738.cpp | 69 +++ 10739.cpp | 112 ++-- 10740.cpp | 98 ++++ 10741.cpp | 59 +++ 10742.cpp | 83 +++ 10743.cpp | 83 +++ 10744.cpp | 110 ++++ 10745.cpp | 96 ++++ 10746.cpp | 143 +++++ 10747.cpp | 236 +++++++++ 10748.cpp | 72 +++ 1075.cpp | 243 +++++++++ 10750.cpp | 162 ++++++ 10751.cpp | 22 + 10754.cpp | 141 +++++ 10755.cpp | 80 +++ 10757.cpp | 711 +++++++++++++++++++++++++ 10758.cpp | 212 ++++++++ 10759.cpp | 108 ++++ 1076.cpp | 178 +++++++ 10760.cpp | 111 ++++ 10761.cpp | 86 +++ 10763.cpp | 97 ++-- 10764.cpp | 56 ++ 10765.cpp | 83 +++ 10766.cpp | 101 ++++ 10767.cpp | 48 ++ 10768.cpp | 169 ++++++ 10769.cpp | 67 +++ 1077.cpp | 366 +++++++++++++ 10770.cpp | 382 ++++++++++++++ 10771.cpp | 24 + 10772.cpp | 89 ++++ 10773.cpp | 69 +-- 10774.cpp | 39 ++ 10775.cpp | 129 +++++ 10776.cpp | 64 +++ 10777.cpp | 40 ++ 10778.cpp | 353 +++++++++++++ 10779.cpp | 180 +++++++ 1078.cpp | 185 +++++++ 10780.cpp | 79 +++ 10782.cpp | 123 +++++ 10783.cpp | 26 + 10784.cpp | 15 + 10785.cpp | 86 ++- 10787.cpp | 80 +++ 10788.cpp | 137 +++++ 10789.cpp | 136 +++-- 1079.cpp | 111 ++++ 10790.cpp | 39 ++ 10791.cpp | 75 +++ 10792.cpp | 22 + 10793.cpp | 82 +++ 10794.cpp | 47 ++ 10795.cpp | 52 ++ 10797.cpp | 107 ++++ 10798.cpp | 110 ++++ 10799.cpp | 30 ++ 108.cpp | 135 ++--- 1080.cpp | 489 +++++++++++++++++ 10800.cpp | 106 ++-- 10801.cpp | 262 +++++++++ 10802.cpp | 110 ++++ 10803.cpp | 166 +++--- 10804.cpp | 108 ++++ 10805.cpp | 120 +++++ 10806.cpp | 114 ++++ 10807.cpp | 154 ++++++ 10808.cpp | 309 +++++++++++ 10809.cpp | 119 +++++ 1081.cpp | 245 +++++++++ 10810.cpp | 125 +++-- 10811.cpp | 115 ++++ 10812.cpp | 36 +- 10813.cpp | 94 ++++ 10814.cpp | 275 ++++++++++ 10815.cpp | 34 ++ 10816.cpp | 117 +++++ 10817.cpp | 73 +++ 10818.cpp | 224 ++++++++ 10819.cpp | 112 ++++ 1082.cpp | 82 +++ 10820.cpp | 50 ++ 10821.cpp | 68 +++ 10823.cpp | 138 +++++ 10824.cpp | 104 ++++ 10825.cpp | 101 ++++ 10826.cpp | 103 ++++ 10827.cpp | 72 +++ 10828.cpp | 174 ++++++ 10829.cpp | 201 +++++++ 1083.cpp | 182 +++++++ 10830.cpp | 39 ++ 10831.cpp | 68 +++ 10832.cpp | 164 ++++++ 10833.cpp | 145 +++++ 10834.cpp | 93 ++++ 10835.cpp | 74 +++ 10836.cpp | 161 ++++++ 10837.cpp | 113 ++++ 10838.cpp | 214 ++++++++ 10839.cpp | 220 ++++++++ 1084.cpp | 370 +++++++++++++ 10841.cpp | 99 ++++ 10842.cpp | 89 ++++ 10843.cpp | 63 ++- 10844.cpp | 93 ++++ 10846.cpp | 70 +++ 10847.cpp | 83 +++ 10848.cpp | 191 +++++++ 10849.cpp | 44 ++ 1085.cpp | 159 ++++++ 10850.cpp | 104 ++++ 10851.cpp | 35 ++ 10852.cpp | 82 +-- 10853.cpp | 31 ++ 10854.cpp | 44 ++ 10855.cpp | 77 +++ 10856.cpp | 109 ++++ 10858.cpp | 50 ++ 10859.cpp | 74 +++ 1086.cpp | 155 ++++++ 10860.cpp | 156 ++++++ 10861.cpp | 274 ++++++++++ 10862.cpp | 489 +++++++++++++++++ 10863.cpp | 163 ++++++ 10864.cpp | 144 +++++ 10865.cpp | 56 ++ 10866.cpp | 29 + 10867.cpp | 134 +++++ 10868.cpp | 44 ++ 10869.cpp | 302 +++++++++++ 10870.cpp | 81 +++ 10871.cpp | 75 +++ 10872.cpp | 17 + 10873.cpp | 90 ++++ 10874.cpp | 85 +++ 10875.cpp | 135 +++++ 10876.cpp | 104 ++++ 10877.cpp | 265 ++++++++++ 10878.cpp | 75 +-- 10879.cpp | 67 ++- 1088.cpp | 229 ++++++++ 10880.cpp | 51 ++ 10881.cpp | 100 ++++ 10882.cpp | 50 ++ 10883.cpp | 88 +--- 10884.cpp | 413 +++++++++++++++ 10886.cpp | 84 +++ 10887.cpp | 160 +++--- 10888.cpp | 115 ++++ 10889.cpp | 54 ++ 1089.cpp | 111 ++++ 10890.cpp | 130 +++++ 10891.cpp | 53 ++ 10892.cpp | 72 +++ 10893.cpp | 104 ++++ 10894.cpp | 124 +++++ 10895.cpp | 53 ++ 10896.cpp | 70 +++ 10897.cpp | 83 +++ 10898.cpp | 102 ++++ 10899.cpp | 218 ++++++++ 109.cpp | 177 +++++++ 10900.cpp | 34 ++ 10901.cpp | 283 +++++++--- 10902.cpp | 117 +++++ 10903.cpp | 149 +++--- 10904.cpp | 96 ++++ 10905.cpp | 91 +--- 10906.cpp | 103 ++++ 10907.cpp | 230 ++++++++ 10908.cpp | 82 +++ 10909.cpp | 180 +++++++ 1091.cpp | 187 +++++++ 10910.cpp | 62 +++ 10911.cpp | 119 +++-- 10912.cpp | 65 +++ 10913.cpp | 199 +++++++ 10914.cpp | 142 +++++ 10915.cpp | 46 ++ 10916.cpp | 38 ++ 10917.cpp | 101 ++++ 10918.cpp | 57 +- 10919.cpp | 41 ++ 1092.cpp | 153 ++++++ 10920.cpp | 64 +++ 10921.cpp | 68 ++- 10922.cpp | 147 +++--- 10923.cpp | 229 ++++++++ 10924.cpp | 117 ++--- 10925.cpp | 497 ++++++++++++++++++ 10926.cpp | 74 +++ 10927.cpp | 92 ++++ 10928.cpp | 109 ++-- 10929.cpp | 93 ++-- 1093.cpp | 69 +++ 10930.cpp | 136 +++-- 10931.cpp | 56 +- 10932.cpp | 138 +++++ 10933.cpp | 116 ++++ 10934.cpp | 66 +++ 10935.cpp | 68 ++- 10936.cpp | 30 ++ 10937.cpp | 201 +++++++ 10938.cpp | 92 ++++ 10939.cpp | 125 +++++ 1094.cpp | 319 +++++++++++ 10940.cpp | 62 ++- 10941.cpp | 111 ++++ 10942.cpp | 73 +++ 10943.cpp | 53 +- 10944.cpp | 137 +++++ 10945.cpp | 76 +-- 10946.cpp | 133 +++-- 10947.cpp | 123 +++++ 10948.cpp | 106 ++-- 10949.cpp | 102 ++++ 1095.cpp | 204 +++++++ 10950.cpp | 99 ++++ 10951.cpp | 176 +++++++ 10952.cpp | 164 ++++++ 10953.cpp | 67 +++ 10954.cpp | 88 ++-- 10956.cpp | 125 +++++ 10957.cpp | 132 +++++ 10958.cpp | 76 +++ 10959.cpp | 128 +++++ 10960.cpp | 288 ++++++++++ 10961.cpp | 134 +++++ 10962.cpp | 94 ++++ 10963.cpp | 64 ++- 10964.cpp | 133 +++++ 10965.cpp | 126 +++++ 10967.cpp | 158 ++++++ 10968.cpp | 118 +++++ 10969.cpp | 136 +++++ 10970.cpp | 23 +- 10972.cpp | 93 ++++ 10973.cpp | 153 ++++++ 10975.cpp | 132 +++++ 10976.cpp | 67 ++- 10977.cpp | 158 ++++++ 10978.cpp | 49 ++ 10979.cpp | 193 +++++++ 10980.cpp | 100 ++++ 10981.cpp | 133 +++++ 10982.cpp | 97 ++++ 10983.cpp | 138 +++++ 10984.cpp | 221 ++++++++ 10985.cpp | 84 +++ 10986.cpp | 129 +++++ 10987.cpp | 78 +++ 10989.cpp | 74 +++ 1099.cpp | 202 +++++++ 10990.cpp | 46 ++ 10991.cpp | 30 ++ 10992.cpp | 587 +++++++++++++++++++++ 10993.cpp | 83 +++ 10994.cpp | 88 +++- 10995.cpp | 31 ++ 10996.cpp | 108 ++++ 10997.cpp | 77 +++ 10998.cpp | 61 +++ 10999.cpp | 67 +++ 110.cpp | 127 +++++ 1100.cpp | 245 +++++++++ 11000.cpp | 52 +- 11001.cpp | 79 ++- 11002.cpp | 161 ++++++ 11003.cpp | 60 +++ 11004.cpp | 57 ++ 11005.cpp | 55 ++ 11006.cpp | 69 +++ 11007.cpp | 315 +++++++++++ 11008.cpp | 87 +++ 11009.cpp | 25 + 1101.cpp | 202 +++++++ 11010.cpp | 185 +++++++ 11011.cpp | 66 +++ 11012.cpp | 50 ++ 11013.cpp | 195 +++++++ 11014.cpp | 31 ++ 11015.cpp | 156 +++--- 11016.cpp | 131 +++++ 11017.cpp | 59 +++ 11018.cpp | 116 ++++ 11019.cpp | 202 +++++++ 11020.cpp | 49 ++ 11021.cpp | 38 ++ 11022.cpp | 72 +++ 11023.cpp | 1490 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 11024.cpp | 73 +++ 11025.cpp | 109 ++++ 11026.cpp | 62 +++ 11027.cpp | 154 ++++++ 11028.cpp | 33 +- 11029.cpp | 97 ++++ 1103.cpp | 167 ++++++ 11030.cpp | 68 +++ 11031.cpp | 88 ++++ 11032.cpp | 203 +++++++ 11033.cpp | 124 +++++ 11034.cpp | 48 ++ 11035.cpp | 50 ++ 11036.cpp | 90 ++++ 11037.cpp | 120 +++++ 11038.cpp | 105 +++- 11039.cpp | 44 ++ 11040.cpp | 43 ++ 11041.cpp | 97 ++++ 11042.cpp | 64 +++ 11043.cpp | 72 +++ 11044.cpp | 44 +- 11045.cpp | 126 +++++ 11046.cpp | 155 ++++++ 11047.cpp | 119 +++++ 11048.cpp | 177 ++++--- 11049.cpp | 96 ++++ 1105.cpp | 86 +++ 11050.cpp | 331 ++++++++++++ 11051.cpp | 84 +++ 11052.cpp | 150 ++++++ 11053.cpp | 31 ++ 11054.cpp | 68 +-- 11055.cpp | 48 ++ 11056.cpp | 86 +++ 11057.cpp | 61 ++- 11058.cpp | 62 +++ 11059.cpp | 45 +- 1106.cpp | 109 ++++ 11060.cpp | 122 +++++ 11061.cpp | 1017 +++++++++++++++++++++++++++++++++++ 11062.cpp | 69 +++ 11063.cpp | 112 ++-- 11064.cpp | 72 +++ 11065.cpp | 144 +++++ 11066.cpp | 85 +++ 11067.cpp | 71 +++ 11068.cpp | 176 +++++++ 11069.cpp | 26 + 11070.cpp | 74 +++ 11071.cpp | 79 +++ 11072.cpp | 158 ++++++ 11073.cpp | 132 +++++ 11074.cpp | 69 +++ 11075.cpp | 152 ++++++ 11076.cpp | 84 +++ 11077.cpp | 28 + 11078.cpp | 26 + 11079.cpp | 161 ++++++ 1108.cpp | 171 ++++++ 11080.cpp | 96 ++++ 11081.cpp | 45 ++ 11082.cpp | 101 ++++ 11083.cpp | 123 +++++ 11084.cpp | 65 +++ 11085.cpp | 116 ++++ 11086.cpp | 67 +++ 11087.cpp | 127 +++++ 11088.cpp | 110 ++++ 11089.cpp | 91 ++++ 1109.cpp | 128 +++++ 11090.cpp | 113 ++++ 11091.cpp | 159 ++++++ 11092.cpp | 63 +++ 11093.cpp | 76 +++ 11094.cpp | 91 ++-- 11095.cpp | 209 ++++++++ 11096.cpp | 96 ++++ 11097.cpp | 69 +++ 11098.cpp | 171 ++++++ 11099.cpp | 45 ++ 111.cpp | 88 ++-- 1110.cpp | 88 ++++ 11100.cpp | 62 +++ 11101.cpp | 74 +++ 11102.cpp | 135 +++++ 11103.cpp | 52 ++ 11104.cpp | 45 ++ 11105.cpp | 56 ++ 11106.cpp | 145 +++++ 11107.cpp | 195 +++++++ 11108.cpp | 68 +++ 11109.cpp | 42 ++ 1111.cpp | 110 ++++ 11110.cpp | 184 +++++-- 11111.cpp | 73 +++ 11112.cpp | 28 + 11113.cpp | 122 +++++ 11114.cpp | 428 +++++++++++++++ 11115.cpp | 353 +++++++++++++ 11116.cpp | 69 +++ 11117.cpp | 175 ++++++ 11118.cpp | 22 + 11119.cpp | 110 ++++ 1112.cpp | 63 +++ 11120.cpp | 1299 +++++++++++++++++++++++++++++++++++++++++++++ 11121.cpp | 56 +- 11122.cpp | 137 +++++ 11123.cpp | 158 ++++++ 11124.cpp | 70 +++ 11125.cpp | 98 ++++ 11126.cpp | 92 ++++ 11127.cpp | 138 +++++ 11128.cpp | 96 ++++ 11129.cpp | 50 ++ 1113.cpp | 162 ++++++ 11130.cpp | 23 + 11131.cpp | 120 +++++ 11132.cpp | 69 +++ 11133.cpp | 35 ++ 11134.cpp | 109 ++++ 11135.cpp | 104 ++++ 11136.cpp | 89 ++-- 11137.cpp | 51 +- 11138.cpp | 95 ++++ 11139.cpp | 255 +++++++++ 11140.cpp | 212 ++++++++ 11141.cpp | 158 ++++++ 11142.cpp | 217 ++++++++ 11144.cpp | 157 ++++++ 11145.cpp | 83 +++ 11146.cpp | 150 ++++++ 11147.cpp | 83 +++ 11148.cpp | 83 +++ 11149.cpp | 96 ++++ 1115.cpp | 58 ++ 11150.cpp | 65 +-- 11151.cpp | 111 ++-- 11152.cpp | 33 +- 11153.cpp | 103 ++++ 11154.cpp | 171 ++++++ 11155.cpp | 28 + 11156.cpp | 266 ++++++++++ 11157.cpp | 191 +++++++ 11158.cpp | 46 ++ 11159.cpp | 103 ++++ 11160.cpp | 109 ++++ 11161.cpp | 120 +++++ 11162.cpp | 72 +++ 11163.cpp | 115 ++++ 11164.cpp | 36 ++ 11165.cpp | 147 ++++++ 11166.cpp | 132 +++++ 11167.cpp | 191 +++++++ 11168.cpp | 161 ++++++ 11169.cpp | 110 ++++ 11170.cpp | 63 +++ 11171.cpp | 165 ++++++ 11172.cpp | 78 +-- 11173.cpp | 15 + 11174.cpp | 109 ++++ 11175.cpp | 82 +++ 11176.cpp | 52 ++ 11177.cpp | 126 +++++ 11178.cpp | 87 +++ 11179.cpp | 160 ++++++ 1118.cpp | 15 + 11180.cpp | 65 +++ 11181.cpp | 73 +++ 11182.cpp | 63 +++ 11183.cpp | 199 +++++++ 11184.cpp | 65 +++ 11185.cpp | 85 ++- 11186.cpp | 30 ++ 11187.cpp | 117 +++++ 11189.cpp | 140 +++++ 1119.cpp | 48 ++ 11190.cpp | 34 ++ 11191.cpp | 186 +++++++ 11192.cpp | 65 +-- 11193.cpp | 96 ++++ 11195.cpp | 51 ++ 11196.cpp | 113 ++++ 11198.cpp | 192 +++++++ 112.cpp | 57 ++ 1120.cpp | 45 ++ 11200.cpp | 49 ++ 11201.cpp | 110 ++++ 11202.cpp | 24 + 11203.cpp | 137 ++--- 11204.cpp | 33 ++ 11205.cpp | 71 +++ 11206.cpp | 76 +++ 11207.cpp | 83 ++- 11208.cpp | 330 ++++++++++++ 11209.cpp | 96 ++++ 1121.cpp | 38 ++ 11210.cpp | 146 +++++ 11212.cpp | 165 ++++++ 11214.cpp | 66 +++ 11215.cpp | 113 ++++ 11218.cpp | 125 +++-- 11219.cpp | 95 +++- 11220.cpp | 41 ++ 11221.cpp | 143 ++--- 11222.cpp | 73 +++ 11223.cpp | 244 ++++----- 11224.cpp | 90 ++++ 11225.cpp | 89 ++++ 11226.cpp | 120 +++++ 11227.cpp | 72 +++ 11228.cpp | 91 ++++ 11230.cpp | 55 ++ 11231.cpp | 26 + 11232.cpp | 33 ++ 11233.cpp | 131 +++-- 11234.cpp | 58 ++ 11235.cpp | 112 ++++ 11236.cpp | 40 ++ 11237.cpp | 52 ++ 11238.cpp | 67 +++ 11239.cpp | 156 +++--- 1124.cpp | 18 +- 11240.cpp | 30 ++ 11241.cpp | 76 +++ 11242.cpp | 49 ++ 11243.cpp | 76 +++ 11244.cpp | 235 ++++++++- 11245.cpp | 34 ++ 11246.cpp | 33 ++ 11247.cpp | 51 +- 11248.cpp | 193 +++++++ 11249.cpp | 45 ++ 1125.cpp | 60 +++ 11250.cpp | 1279 ++++++++++++++++++++++++++++++++++++++++++++ 11251.cpp | 461 ++++++++++++++++ 11252.cpp | 94 ++++ 11253.cpp | 62 +++ 11254.cpp | 79 +++ 11255.cpp | 89 ++++ 11256.cpp | 83 +++ 11257.cpp | 158 ++++++ 11258.cpp | 53 ++ 11259.cpp | 59 +++ 11260.cpp | 117 ++++- 11261.cpp | 126 +++++ 11262.cpp | 203 +++++++ 11264.cpp | 31 ++ 11265.cpp | 100 ++++ 11266.cpp | 50 ++ 11267.cpp | 165 ++++++ 11268.cpp | 63 +++ 11269.cpp | 86 +++ 11275.cpp | 248 +++++++++ 11277.cpp | 69 +++ 11278.cpp | 21 + 11279.cpp | 73 +++ 11280.cpp | 120 +++++ 11281.cpp | 64 +++ 11282.cpp | 50 ++ 11283.cpp | 104 ++++ 11284.cpp | 124 +++++ 11285.cpp | 52 ++ 11286.cpp | 115 ++-- 11287.cpp | 175 +++--- 11288.cpp | 53 ++ 11290.cpp | 46 ++ 11291.cpp | 52 ++ 11292.cpp | 138 +++-- 11294.cpp | 120 +++++ 11296.cpp | 10 + 112972.cpp | 154 ++++++ 11298.cpp | 20 + 113.cpp | 26 +- 11300.cpp | 119 +++++ 11301.cpp | 104 ++++ 11307.cpp | 83 +++ 11308.cpp | 138 ++--- 11309.cpp | 90 ++-- 11310.cpp | 28 + 11311.cpp | 32 ++ 11313.cpp | 27 + 11314.cpp | 101 ++++ 11317.cpp | 68 +++ 11318.cpp | 153 ++++++ 11319.cpp | 131 +++++ 11321.cpp | 122 ++--- 11323.cpp | 117 +++++ 11324.cpp | 178 +++++++ 11325.cpp | 85 +++ 11326.cpp | 45 ++ 11327.cpp | 82 +++ 11328.cpp | 57 ++ 11329.cpp | 171 ++++++ 1133.cpp | 61 +++ 11330.cpp | 97 ++++ 11331.cpp | 128 +++++ 11332.cpp | 27 + 11333.cpp | 122 +++++ 11334.cpp | 112 ++++ 11335.cpp | 46 ++ 11336.cpp | 89 ++++ 11337.cpp | 121 +++++ 11338.cpp | 132 +++++ 11340.cpp | 76 +-- 11341.cpp | 50 ++ 11342.cpp | 120 ++--- 11343.cpp | 95 ++++ 11344.cpp | 48 ++ 11345.cpp | 111 ++++ 11346.cpp | 22 + 11347.cpp | 37 ++ 11348.cpp | 67 +++ 11349.cpp | 90 ++-- 11350.cpp | 52 ++ 11351.cpp | 77 +-- 11352.cpp | 99 ++++ 11353.cpp | 64 +++ 11354.cpp | 251 +++++++++ 11355.cpp | 130 +++++ 11356.cpp | 73 +++ 11357.cpp | 103 ++++ 11358.cpp | 129 +++++ 11359.cpp | 266 ++++++++++ 11360.cpp | 187 ++++--- 11361.cpp | 83 +++ 11362.cpp | 127 +++-- 11363.cpp | 78 +++ 11364.cpp | 22 + 11366.cpp | 96 ++++ 11367.cpp | 107 ++++ 11368.cpp | 61 +++ 11369.cpp | 72 +-- 1137.cpp | 67 +++ 11370.cpp | 71 +++ 11371.cpp | 59 +-- 11372.cpp | 150 ++++++ 11374.cpp | 112 ++++ 11375.cpp | 128 +++++ 11376.cpp | 254 +++++++++ 11377.cpp | 108 ++++ 11378.cpp | 104 ++++ 11379.cpp | 77 +++ 11380.cpp | 162 ++++++ 11381.cpp | 236 +++++++++ 11382.cpp | 251 +++++++++ 11383.cpp | 220 ++++++++ 11384.cpp | 44 +- 11385.cpp | 60 +++ 11386.cpp | 29 +- 11387.cpp | 34 ++ 11388.cpp | 74 +-- 11389.cpp | 77 +-- 11390.cpp | 218 ++++++++ 11391.cpp | 69 +++ 11392.cpp | 115 ++++ 11393.cpp | 20 + 11394.cpp | 89 ++++ 11395.cpp | 15 + 11396.cpp | 78 +++ 11398.cpp | 100 ++-- 11399.cpp | 127 +++++ 114.cpp | 108 ++++ 11400.cpp | 54 ++ 11401.cpp | 79 +-- 11402.cpp | 165 ++++++ 11403.cpp | 86 +++ 11404.cpp | 114 ++++ 11405.cpp | 75 +++ 11407.cpp | 31 ++ 11408.cpp | 42 ++ 11409.cpp | 183 +++++++ 1141.cpp | 231 ++++++++ 11410.cpp | 79 +++ 11411.cpp | 109 ++++ 11412.cpp | 110 ++++ 11413.cpp | 64 +++ 11414.cpp | 63 +++ 11415.cpp | 53 ++ 11416.cpp | 240 +++++++++ 11417.cpp | 75 ++- 11418.cpp | 87 +++ 11419.cpp | 108 ++++ 11420.cpp | 66 +++ 11421.cpp | 140 +++++ 11422.cpp | 189 +++++++ 11423.cpp | 108 ++++ 11424.cpp | 65 +++ 11426.cpp | 52 ++ 11427.cpp | 37 ++ 11428.cpp | 86 ++- 11430.cpp | 54 ++ 11432.cpp | 620 ++++++++++++++++++++++ 11436.cpp | 84 ++- 11437.cpp | 106 +++- 11439.cpp | 196 +++++++ 11440.cpp | 102 ++++ 11444.cpp | 92 ++++ 11447.cpp | 66 +++ 11448.cpp | 361 +++++++++++++ 11450.cpp | 123 ++--- 11451.cpp | 72 +++ 11452.cpp | 52 ++ 11455.cpp | 72 ++- 11456.cpp | 47 ++ 11458.cpp | 224 ++++++++ 11459.cpp | 92 ++-- 1146.cpp | 133 +++++ 11460.cpp | 64 +++ 11461.cpp | 43 +- 11462.cpp | 59 ++- 11463.cpp | 120 ++--- 11464.cpp | 104 ++++ 11465.cpp | 74 +++ 11466.cpp | 171 +++--- 11468.cpp | 156 ++++++ 11470.cpp | 69 ++- 11471.cpp | 92 ++++ 11472.cpp | 52 ++ 11473.cpp | 69 +++ 11474.cpp | 94 ++++ 11475.cpp | 100 +++- 11476.cpp | 208 ++++++++ 11478.cpp | 67 +++ 11479.cpp | 40 ++ 1148.cpp | 61 +++ 11480.cpp | 30 ++ 11481.cpp | 88 ++++ 11482.cpp | 44 ++ 11483.cpp | 66 ++- 11484.cpp | 96 ++++ 11485.cpp | 43 ++ 11486.cpp | 130 +++++ 11487.cpp | 109 ++++ 11488.cpp | 66 +++ 11489.cpp | 82 +++ 1149.cpp | 44 ++ 11490.cpp | 44 ++ 11491.cpp | 42 ++ 11492.cpp | 232 ++++++++ 11493.cpp | 81 +++ 11494.cpp | 84 ++- 11495.cpp | 129 +++-- 11496.cpp | 77 +++ 11497.cpp | 149 ++++++ 11498.cpp | 71 +-- 11499.cpp | 111 ++++ 115.cpp | 175 ++++++ 11500.cpp | 134 +++++ 11501.cpp | 286 ++++++++++ 11502.cpp | 82 +++ 11503.cpp | 90 ++++ 11504.cpp | 94 ++++ 11505.cpp | 91 ++-- 11506.cpp | 92 ++++ 11507.cpp | 69 +++ 11508.cpp | 73 +++ 11509.cpp | 75 +++ 1151.cpp | 119 +++++ 11510.cpp | 71 +++ 11511.cpp | 29 + 11512.cpp | 82 +++ 11513.cpp | 80 +++ 11514.cpp | 59 +++ 11515.cpp | 98 ++++ 11516.cpp | 89 ++++ 11517.cpp | 53 ++ 11518.cpp | 74 +++ 11519.cpp | 97 ++++ 1152.cpp | 85 +++ 11520.cpp | 89 ++++ 11522.cpp | 58 ++ 11523.cpp | 109 ++++ 11524.cpp | 31 ++ 11525.cpp | 154 ++++-- 11526.cpp | 73 ++- 11529.cpp | 67 +++ 1153.cpp | 53 ++ 11530.cpp | 60 ++- 11532.cpp | 89 ++-- 11534.cpp | 152 ++++++ 11535.cpp | 63 +++ 11536.cpp | 96 ++++ 11538.cpp | 55 +- 11539.cpp | 97 ++++ 11540.cpp | 187 +++++++ 11541.cpp | 67 ++- 11542.cpp | 109 ++++ 11544.cpp | 167 ++++++ 11545.cpp | 86 +++ 11546.cpp | 72 +++ 11547.cpp | 34 +- 11548.cpp | 83 +++ 11549.cpp | 46 ++ 11550.cpp | 66 +++ 11551.cpp | 103 ++++ 11552.cpp | 92 ++++ 11553.cpp | 70 +++ 11554.cpp | 28 + 11555.cpp | 31 ++ 11556.cpp | 22 + 11557.cpp | 136 +++++ 11559.cpp | 78 +-- 11561.cpp | 85 +++ 11562.cpp | 109 ++++ 11563.cpp | 68 +++ 11565.cpp | 36 ++ 11566.cpp | 73 +++ 11567.cpp | 46 ++ 11568.cpp | 262 +++++++++ 11569.cpp | 70 +++ 11570.cpp | 113 ++++ 11571.cpp | 56 ++ 11572.cpp | 60 +++ 11573.cpp | 88 ++++ 11574.cpp | 135 +++++ 11575.cpp | 284 ++++++++++ 11576.cpp | 78 +++ 11577.cpp | 126 ++--- 11578.cpp | 145 +++++ 11579.cpp | 59 +++ 1158.cpp | 35 ++ 11581.cpp | 168 +++--- 11582.cpp | 73 +++ 11583.cpp | 34 ++ 11584.cpp | 59 +++ 11585.cpp | 268 ++++++++++ 11586.cpp | 68 +++ 11587.cpp | 107 ++++ 11588.cpp | 139 ++--- 11589.cpp | 106 ++++ 11590.cpp | 152 ++++++ 11591.cpp | 208 ++++++++ 11593.cpp | 34 ++ 11594.cpp | 137 +++++ 11597.cpp | 30 +- 11598.cpp | 160 ++++++ 116.cpp | 156 +++--- 1160.cpp | 50 ++ 11600.cpp | 134 +++++ 11601.cpp | 98 ++++ 11603.cpp | 77 +++ 11604.cpp | 155 ++++++ 11605.cpp | 98 ++++ 11608.cpp | 76 +-- 11609.cpp | 73 ++- 1161.cpp | 164 ++++++ 11610.cpp | 127 +++++ 11611.cpp | 159 ++++++ 11612.cpp | 127 +++++ 11613.cpp | 124 +++++ 11614.cpp | 16 + 11615.cpp | 116 ++++ 11616.cpp | 95 ++-- 11617.cpp | 70 +++ 1162.cpp | 64 +++ 11620.cpp | 132 +++++ 11621.cpp | 37 ++ 11623.cpp | 169 ++++++ 11624.cpp | 189 +++++++ 11625.cpp | 107 ++++ 11626.cpp | 94 ++++ 11628.cpp | 39 ++ 11629.cpp | 100 ++++ 1163.cpp | 120 +++++ 11630.cpp | 60 +++ 11631.cpp | 94 ++++ 11632.cpp | 49 ++ 11633.cpp | 84 +++ 11634.cpp | 54 +- 11635.cpp | 216 ++++++++ 11636.cpp | 53 +- 11637.cpp | 45 ++ 11638.cpp | 65 +++ 11639.cpp | 29 + 1164.cpp | 150 ++++++ 11643.cpp | 133 +++++ 11645.cpp | 1195 ++++++++++++++++++++++++++++++++++++++++- 11646.cpp | 16 + 11648.cpp | 36 ++ 11649.cpp | 93 ++++ 11650.cpp | 50 +- 11651.cpp | 132 +++++ 11653.cpp | 150 ++++++ 11654.cpp | 164 ++++++ 11655.cpp | 69 +++ 11656.cpp | 164 ++++++ 11657.cpp | 63 +++ 11658.cpp | 75 +++ 11659.cpp | 30 ++ 1166.cpp | 238 +++++++++ 11660.cpp | 40 ++ 11661.cpp | 96 ++-- 11662.cpp | 85 +++ 11663.cpp | 127 +++++ 11664.cpp | 146 +++++ 11665.cpp | 242 +++++++++ 11666.cpp | 19 + 11667.cpp | 33 ++ 11668.cpp | 107 ++++ 11669.cpp | 114 ++++ 11670.cpp | 53 ++ 11671.cpp | 269 ++++++++++ 11672.cpp | 198 +++++++ 11673.cpp | 82 +++ 11675.cpp | 146 +++++ 11676.cpp | 125 +++++ 11677.cpp | 47 +- 11678.cpp | 92 ++-- 11679.cpp | 44 ++ 11680.cpp | 140 +++++ 11681.cpp | 137 +++++ 11683.cpp | 38 ++ 11684.cpp | 110 ++++ 11685.cpp | 116 ++++ 11686.cpp | 78 +++ 11687.cpp | 60 +-- 11688.cpp | 75 +++ 11689.cpp | 46 +- 1169.cpp | 52 ++ 11690.cpp | 110 ++++ 11691.cpp | 51 ++ 11692.cpp | 43 ++ 11693.cpp | 135 +++++ 11694.cpp | 253 +++++++++ 11695.cpp | 201 +++++++ 11696.cpp | 254 +++++++++ 11697.cpp | 99 ++++ 11698.cpp | 107 ++++ 11699.cpp | 51 ++ 117.cpp | 163 ++++++ 1170.cpp | 165 ++++++ 11700.cpp | 149 ++++++ 11701.cpp | 28 + 11702.cpp | 73 +++ 11703.cpp | 74 +-- 11704.cpp | 135 +++++ 11705.cpp | 105 ++++ 11706.cpp | 86 +++ 11708.cpp | 74 +++ 11709.cpp | 86 +++ 1171.cpp | 69 +++ 11710.cpp | 84 +++ 11711.cpp | 101 ++++ 11712.cpp | 36 ++ 11713.cpp | 82 +-- 11714.cpp | 26 +- 11715.cpp | 107 ++-- 11716.cpp | 75 ++- 11717.cpp | 34 ++ 11718.cpp | 116 ++-- 11719.cpp | 53 ++ 1172.cpp | 85 +++ 11721.cpp | 201 +++++++ 11722.cpp | 49 ++ 11723.cpp | 67 ++- 11724.cpp | 114 ++++ 11725.cpp | 278 ++++++++++ 11726.cpp | 228 ++++++++ 11727.cpp | 50 +- 11728.cpp | 65 ++- 11729.cpp | 52 ++ 1173.cpp | 159 ++++++ 11730.cpp | 92 ++++ 11731.cpp | 142 +++++ 11732.cpp | 61 +++ 11733.cpp | 96 ++++ 11734.cpp | 83 +-- 11735.cpp | 112 ++++ 11736.cpp | 54 ++ 11737.cpp | 51 ++ 11739.cpp | 179 +++++++ 1174.cpp | 85 +++ 11741.cpp | 270 ++++++++++ 11742.cpp | 75 +++ 11743.cpp | 107 ++-- 11744.cpp | 49 ++ 11745.cpp | 105 ++++ 11746.cpp | 183 +++++++ 11747.cpp | 108 ++++ 11748.cpp | 50 ++ 11749.cpp | 102 ++++ 1175.cpp | 115 ++++ 11751.cpp | 42 ++ 11752.cpp | 45 ++ 11753.cpp | 118 ++--- 11754.cpp | 330 ++++++++++++ 11755.cpp | 162 ++++++ 11757.cpp | 158 ++++++ 11759.cpp | 169 ++++++ 11760.cpp | 47 ++ 11761.cpp | 142 +++++ 11762.cpp | 124 +++++ 11764.cpp | 56 +- 11765.cpp | 132 +++++ 11766.cpp | 33 ++ 11768.cpp | 98 ++++ 11769.cpp | 209 ++++++++ 1177.cpp | 97 ++++ 11770.cpp | 90 ++++ 11773.cpp | 175 ++++-- 11774.cpp | 75 ++- 11775.cpp | 89 ++++ 11776.cpp | 46 ++ 11777.cpp | 86 ++- 11779.cpp | 131 +++++ 11780.cpp | 39 ++ 11782.cpp | 59 +++ 11783.cpp | 109 ++++ 11784.cpp | 134 +++++ 11785.cpp | 112 ++++ 11786.cpp | 34 ++ 11787.cpp | 96 ++++ 11788.cpp | 72 +++ 11790.cpp | 48 ++ 11792.cpp | 100 ++++ 11795.cpp | 62 +++ 11796.cpp | 161 ++++++ 11797.cpp | 51 ++ 11798.cpp | 95 ++++ 11799.cpp | 60 +-- 118.cpp | 77 +++ 1180.cpp | 40 ++ 11800.cpp | 254 ++++----- 11801.cpp | 70 +++ 11802.cpp | 62 +++ 11803.cpp | 215 ++++++++ 11804.cpp | 178 ++++--- 11805.cpp | 68 +-- 11806.cpp | 67 +++ 11809.cpp | 40 ++ 11813.cpp | 104 ++++ 11814.cpp | 175 ++++++ 11815.cpp | 130 +++++ 11816.cpp | 149 +++--- 11817.cpp | 43 ++ 11818.cpp | 192 +++++++ 11819.cpp | 71 +++ 1182.cpp | 83 +++ 11820.cpp | 163 ++++++ 11821.cpp | 173 ++++++ 11823.cpp | 128 +++++ 11824.cpp | 102 ++-- 11825.cpp | 70 +++ 11827.cpp | 167 +++--- 11828.cpp | 97 ++++ 11829.cpp | 19 + 11830.cpp | 101 ++-- 11831.cpp | 281 +++++----- 11832.cpp | 77 +++ 11833.cpp | 88 ++++ 11834.cpp | 73 +-- 11835.cpp | 75 +++ 11836.cpp | 322 +++++++++--- 11837.cpp | 95 ++++ 11838.cpp | 129 ++--- 11839.cpp | 97 ++-- 1184.cpp | 145 +++++ 11840.cpp | 111 ++++ 11841.cpp | 139 +++++ 11842.cpp | 353 +++++++++++++ 11843.cpp | 44 ++ 11844.cpp | 121 +++++ 11845.cpp | 189 +++++++ 11846.cpp | 118 +++++ 11847.cpp | 27 +- 11848.cpp | 115 ++++ 11849.cpp | 61 +-- 1185.cpp | 35 +- 11850.cpp | 79 +-- 11851.cpp | 43 ++ 11852.cpp | 78 +++ 11853.cpp | 405 ++++++++++++++ 11854.cpp | 48 +- 11855.cpp | 262 +++++++++ 11856.cpp | 97 ++-- 11857.cpp | 174 +++--- 11858.cpp | 61 +++ 1186.cpp | 281 ++++++++++ 11860.cpp | 112 ++++ 11863.cpp | 74 +++ 11865.cpp | 169 ++++++ 11866.cpp | 103 ++-- 11867.cpp | 206 ++++++++ 11869.cpp | 182 ++++--- 1187.cpp | 56 ++ 11870.cpp | 64 +++ 11871.cpp | 257 ++++++--- 11872.cpp | 97 ++++ 11873.cpp | 91 +--- 11874.cpp | 133 ++--- 11875.cpp | 69 +-- 11876.cpp | 105 ++-- 11877.cpp | 120 ++--- 11878.cpp | 108 +--- 11879.cpp | 406 ++++++++++++++ 1188.cpp | 37 ++ 11880.cpp | 46 ++ 11881.cpp | 61 +++ 11882.cpp | 157 ++++++ 11883.cpp | 110 ++++ 11884.cpp | 76 +++ 11885.cpp | 90 ++++ 11886.cpp | 221 ++++++++ 11888.cpp | 146 ++--- 11889.cpp | 130 ++++- 1189.cpp | 50 ++ 11890.cpp | 118 +++++ 11891.cpp | 99 ++++ 11892.cpp | 59 +++ 11893.cpp | 106 ++++ 11894.cpp | 85 +++ 11895.cpp | 64 +++ 11897.cpp | 123 +++++ 11898.cpp | 131 +++++ 119.cpp | 102 ++-- 11900.cpp | 93 ++-- 11901.cpp | 208 ++++++++ 11902.cpp | 152 +++--- 11903.cpp | 82 +++ 11904.cpp | 92 ++++ 11906.cpp | 70 +++ 11907.cpp | 76 +++ 11908.cpp | 83 +++ 11909.cpp | 48 +- 11910.cpp | 67 +++ 11911.cpp | 158 ++++++ 11913.cpp | 87 +++ 11916.cpp | 179 +++++++ 11917.cpp | 57 +- 11918.cpp | 159 ++++++ 11919.cpp | 170 ++++++ 1192.cpp | 71 +++ 11920.cpp | 185 +++++++ 11921.cpp | 592 +++++++++++++++++++++ 11922.cpp | 162 ++++++ 11923.cpp | 254 +++++++++ 11925.cpp | 54 ++ 11926.cpp | 67 +++ 11927.cpp | 65 +++ 1193.cpp | 80 +++ 11930.cpp | 197 +++++++ 11931.cpp | 100 ++++ 11932.cpp | 121 +++++ 11933.cpp | 82 +-- 11934.cpp | 73 +-- 11935.cpp | 152 ++++++ 11936.cpp | 63 +-- 1194.cpp | 121 +++++ 11940.cpp | 184 +++++++ 11941.cpp | 114 ++++ 11942.cpp | 89 ++-- 11943.cpp | 127 +++++ 11944.cpp | 74 +++ 11945.cpp | 57 +- 11946.cpp | 71 +-- 11947.cpp | 109 ++++ 11948.cpp | 127 +++++ 11949.cpp | 56 ++ 1195.cpp | 57 ++ 11950.cpp | 126 +++++ 11951.cpp | 51 ++ 11952.cpp | 53 ++ 11953.cpp | 88 ++-- 11954.cpp | 174 ++++++ 11955.cpp | 176 ++++--- 11956.cpp | 65 +++ 11957.cpp | 80 +++ 11958.cpp | 32 ++ 11959.cpp | 27 + 1196.cpp | 82 +++ 11960.cpp | 144 ++--- 11961.cpp | 60 +++ 11962.cpp | 62 +++ 11964.cpp | 128 +++++ 11965.cpp | 101 ++-- 11966.cpp | 136 +++++ 11967.cpp | 49 ++ 11968.cpp | 51 ++ 1197.cpp | 71 +++ 11970.cpp | 38 ++ 11971.cpp | 44 ++ 11972.cpp | 156 ++++++ 11974.cpp | 75 +++ 11975.cpp | 50 ++ 11976.cpp | 51 ++ 11977.cpp | 115 ++++ 11978.cpp | 163 ++++++ 11979.cpp | 85 ++- 11980.cpp | 79 +++ 11981.cpp | 98 ++++ 11982.cpp | 63 +++ 11983.cpp | 148 ++++++ 11984.cpp | 64 +-- 11985.cpp | 236 +++++++++ 11986.cpp | 77 ++- 11987.cpp | 150 ++++++ 11988.cpp | 66 +++ 11989.cpp | 167 ++++++ 1199.cpp | 68 +++ 11990.cpp | 152 ++++++ 11991.cpp | 35 ++ 11992.cpp | 253 +++++++++ 11993.cpp | 880 +++++++++++++++++++++++++++++++ 11994.cpp | 538 +++++++++++++++++++ 11995.cpp | 93 ++++ 11996.cpp | 299 +++++++++++ 11997.cpp | 98 ++++ 11999.cpp | 197 +++++++ 120.cpp | 107 ++++ 1200.cpp | 162 ++++++ 12000.cpp | 162 ++++++ 12001.cpp | 71 +++ 12002.cpp | 75 +++ 12003.cpp | 120 +++++ 12004.cpp | 43 ++ 12005.cpp | 57 ++ 12008.cpp | 119 +++++ 12009.cpp | 103 ++++ 1201.cpp | 114 ++++ 12010.cpp | 187 +++++++ 12011.cpp | 70 +++ 12012.cpp | 54 ++ 12013.cpp | 96 ++++ 12014.cpp | 316 +++++++++++ 12015.cpp | 101 ++-- 12016.cpp | 117 +++++ 12017.cpp | 139 +++++ 12018.cpp | 78 +++ 12019.cpp | 80 +-- 1202.cpp | 128 +++++ 12022.cpp | 48 +- 12024.cpp | 35 +- 12026.cpp | 111 ++++ 12027.cpp | 55 +- 12028.cpp | 47 ++ 12029.cpp | 148 ++++++ 1203.cpp | 47 ++ 12030.cpp | 65 +++ 12031.cpp | 125 +++++ 12032.cpp | 79 +++ 12033.cpp | 60 +++ 12034.cpp | 63 +++ 12035.cpp | 116 ++++ 12036.cpp | 48 +- 12037.cpp | 79 +++ 12038.cpp | 319 +++++++++++ 12039.cpp | 66 +++ 1204.cpp | 178 +++++++ 12040.cpp | 96 ++++ 12041.cpp | 82 +++ 12042.cpp | 121 +++++ 12043.cpp | 124 +++++ 12045.cpp | 152 ++++++ 12046.cpp | 84 +++ 12047.cpp | 160 ++++++ 12048.cpp | 125 +++++ 12049.cpp | 101 ++-- 1205.cpp | 86 +++ 12050.cpp | 85 +++ 12051.cpp | 88 ++++ 12052.cpp | 84 +++ 12054.cpp | 122 +++++ 12057.cpp | 102 ++++ 12058.cpp | 162 ++++++ 1206.cpp | 74 +++ 12060.cpp | 108 ++++ 12061.cpp | 22 + 12062.cpp | 67 +++ 12063.cpp | 38 ++ 12064.cpp | 288 ++++++++++ 12065.cpp | 66 +++ 12068.cpp | 40 ++ 12069.cpp | 273 ++++++++++ 1207.cpp | 68 +++ 12070.cpp | 157 ++++++ 12071.cpp | 26 + 12072.cpp | 128 +++++ 12073.cpp | 63 +++ 12074.cpp | 208 ++++++++ 12075.cpp | 34 ++ 12077.cpp | 116 ++++ 12078.cpp | 74 +++ 12079.cpp | 107 ++++ 1208.cpp | 69 +++ 12081.cpp | 40 ++ 12082.cpp | 227 ++++++++ 12083.cpp | 91 ++++ 12084.cpp | 47 ++ 12085.cpp | 96 ++++ 12086.cpp | 129 +++++ 12087.cpp | 108 ++++ 12088.cpp | 112 ++++ 12089.cpp | 61 +++ 1209.cpp | 65 +++ 12090.cpp | 72 +++ 12091.cpp | 67 +++ 12092.cpp | 128 +++++ 12093.cpp | 81 +++ 12094.cpp | 274 ++++++++++ 12095.cpp | 135 +++++ 12096.cpp | 101 ++++ 12097.cpp | 41 ++ 12098.cpp | 149 ++++++ 12099.cpp | 106 ++++ 121.cpp | 37 ++ 1210.cpp | 103 ++-- 12100.cpp | 96 ++++ 12101.cpp | 82 +++ 12102.cpp | 89 ++++ 12103.cpp | 24 + 12105.cpp | 150 ++++++ 12107.cpp | 163 ++++++ 12108.cpp | 47 ++ 12109.cpp | 43 ++ 1211.cpp | 53 ++ 12112.cpp | 297 +++++++++++ 12113.cpp | 66 +++ 12114.cpp | 55 +- 12115.cpp | 61 +++ 12116.cpp | 148 ++++++ 12117.cpp | 44 ++ 12118.cpp | 67 +++ 12119.cpp | 220 ++++++++ 1212.cpp | 194 +++++++ 12120.cpp | 63 +++ 12121.cpp | 101 ++++ 12122.cpp | 120 +++++ 12123.cpp | 156 ++++++ 12124.cpp | 83 +++ 12125.cpp | 134 +++++ 12126.cpp | 47 ++ 12128.cpp | 136 +++++ 1213.cpp | 77 +++ 12130.cpp | 81 +++ 12131.cpp | 135 +++++ 12132.cpp | 48 ++ 12134.cpp | 108 ++++ 12135.cpp | 77 +++ 12136.cpp | 40 +- 12137.cpp | 64 +++ 12138.cpp | 126 +++++ 12139.cpp | 42 ++ 1214.cpp | 156 ++++++ 12140.cpp | 139 +++++ 12141.cpp | 125 +++++ 12143.cpp | 180 +++++++ 12144.cpp | 141 +++++ 12146.cpp | 79 +++ 12147.cpp | 40 ++ 12148.cpp | 42 ++ 12149.cpp | 23 + 1215.cpp | 66 +++ 12150.cpp | 33 ++ 12153.cpp | 141 +++++ 12154.cpp | 47 ++ 12155.cpp | 43 ++ 12157.cpp | 45 +- 12159.cpp | 164 ++++++ 1216.cpp | 77 +++ 12160.cpp | 74 +++ 12161.cpp | 123 +++++ 12163.cpp | 106 ++++ 12165.cpp | 46 ++ 12166.cpp | 76 +++ 12167.cpp | 98 ++++ 12168.cpp | 82 +++ 12169.cpp | 64 +++ 1217.cpp | 83 +++ 12170.cpp | 70 +++ 12171.cpp | 101 ++++ 12172.cpp | 96 ++++ 12173.cpp | 73 +++ 12174.cpp | 43 ++ 12175.cpp | 235 +++++++++ 12176.cpp | 127 +++++ 12177.cpp | 91 ++++ 12178.cpp | 59 +++ 12179.cpp | 67 +++ 1218.cpp | 89 ++++ 12180.cpp | 165 ++++++ 12181.cpp | 124 +++++ 12182.cpp | 48 ++ 12183.cpp | 89 ++++ 12184.cpp | 69 +++ 12186.cpp | 111 ++++ 12187.cpp | 71 +++ 12188.cpp | 53 ++ 12189.cpp | 63 +++ 1219.cpp | 133 +++++ 12190.cpp | 58 ++ 12191.cpp | 88 ++++ 12192.cpp | 84 +++ 12193.cpp | 136 +++++ 12194.cpp | 53 ++ 12195.cpp | 75 ++- 12196.cpp | 47 ++ 12197.cpp | 83 +++ 12198.cpp | 51 ++ 12199.cpp | 75 +++ 122.cpp | 177 +++++++ 1220.cpp | 76 +++ 12200.cpp | 81 +++ 12201.cpp | 271 ++++++++++ 12202.cpp | 99 ++++ 12203.cpp | 118 +++++ 12204.cpp | 139 +++++ 12205.cpp | 101 +++- 12206.cpp | 147 ++++++ 12207.cpp | 48 ++ 12208.cpp | 34 +- 12209.cpp | 97 ++++ 1221.cpp | 120 +++++ 12210.cpp | 31 ++ 12211.cpp | 129 +++++ 12212.cpp | 81 +++ 12213.cpp | 265 ++++++++++ 12214.cpp | 189 +++++++ 12215.cpp | 266 ++++++++++ 12216.cpp | 49 ++ 12217.cpp | 214 ++++++++ 12218.cpp | 71 +++ 12219.cpp | 172 ++++++ 1222.cpp | 264 ++++++++++ 12220.cpp | 37 ++ 12222.cpp | 74 +++ 12223.cpp | 131 +++++ 12225.cpp | 111 ++++ 12226.cpp | 69 +++ 12227.cpp | 191 +++++++ 1223.cpp | 39 ++ 12230.cpp | 26 + 12231.cpp | 33 ++ 12232.cpp | 187 +++++++ 12233.cpp | 186 +++++++ 12235.cpp | 76 +++ 12238.cpp | 116 ++++ 12239.cpp | 60 ++- 1224.cpp | 30 ++ 12240.cpp | 63 +++ 12241.cpp | 66 +++ 12242.cpp | 150 ++++++ 12243.cpp | 65 ++- 12244.cpp | 292 ++++++++++ 12245.cpp | 55 ++ 12246.cpp | 62 +++ 12247.cpp | 120 +++-- 12248.cpp | 148 ++++++ 12249.cpp | 94 ++++ 1225.cpp | 71 +-- 12250.cpp | 54 +- 12251.cpp | 50 ++ 12252.cpp | 186 +++++++ 12253.cpp | 69 +++ 12255.cpp | 96 ++++ 12256.cpp | 19 + 12257.cpp | 73 +++ 12259.cpp | 81 +++ 1226.cpp | 23 + 12260.cpp | 44 ++ 12261.cpp | 60 +++ 12262.cpp | 79 +++ 12263.cpp | 102 ++++ 12264.cpp | 261 +++++++++ 12265.cpp | 96 ++++ 12266.cpp | 99 ++++ 12267.cpp | 152 ++++++ 12268.cpp | 50 ++ 12269.cpp | 73 ++- 1227.cpp | 274 ++++++++++ 12270.cpp | 198 +++++++ 12271.cpp | 51 ++ 12272.cpp | 156 ++++++ 12273.cpp | 175 ++++++ 12274.cpp | 89 ++++ 12275.cpp | 216 ++++++++ 12276.cpp | 70 +++ 12278.cpp | 58 ++ 12279.cpp | 32 +- 1228.cpp | 129 +++++ 12280.cpp | 158 ++++++ 12281.cpp | 64 +++ 12282.cpp | 559 ++++++++++++++++++++ 12283.cpp | 60 +++ 12284.cpp | 108 ++++ 12285.cpp | 63 +++ 12286.cpp | 106 ++++ 12289.cpp | 48 +- 1229.cpp | 129 +++++ 12290.cpp | 55 ++ 12291.cpp | 114 ++++ 12292.cpp | 234 +++++++++ 12293.cpp | 10 + 12294.cpp | 72 +++ 12295.cpp | 146 +++++ 12296.cpp | 513 ++++++++++++++++++ 12297.cpp | 108 ++++ 12298.cpp | 63 +++ 12299.cpp | 108 ++++ 123.cpp | 114 ++++ 1230.cpp | 49 +- 12300.cpp | 27 + 12301.cpp | 44 ++ 12302.cpp | 147 ++++++ 12303.cpp | 245 +++++++++ 12304.cpp | 460 ++++++++++++++++ 12305.cpp | 422 +++++++++++++++ 12306.cpp | 392 ++++++++++++++ 12307.cpp | 154 ++++++ 12308.cpp | 384 ++++++++++++++ 1231.cpp | 45 ++ 12311.cpp | 170 ++++++ 12312.cpp | 564 ++++++++++++++++++++ 12314.cpp | 741 ++++++++++++++++++++++++++ 12315.cpp | 270 ++++++++++ 12317.cpp | 61 +++ 12318.cpp | 48 ++ 12319.cpp | 45 ++ 1232.cpp | 75 +++ 12320.cpp | 243 +++++++++ 12321.cpp | 98 ++++ 12322.cpp | 77 +++ 12323.cpp | 104 ++++ 12324.cpp | 62 +++ 12325.cpp | 42 ++ 12326.cpp | 33 ++ 12327.cpp | 252 +++++++++ 12328.cpp | 567 ++++++++++++++++++++ 12329.cpp | 208 ++++++++ 1233.cpp | 65 +++ 12330.cpp | 169 ++++++ 12331.cpp | 371 +++++++++++++ 12332.cpp | 412 +++++++++++++++ 12333.cpp | 124 +++++ 12334.cpp | 79 +++ 12335.cpp | 44 ++ 12336.cpp | 110 ++++ 12337.cpp | 64 +++ 12338.cpp | 85 +++ 12339.cpp | 74 +++ 1234.cpp | 97 ++++ 12342.cpp | 64 ++- 12343.cpp | 84 +++ 12345.cpp | 204 +++++++ 12346.cpp | 69 +++ 12347.cpp | 55 ++ 12349.cpp | 24 + 1235.cpp | 83 +++ 12350.cpp | 126 +++++ 12351.cpp | 337 ++++++++++++ 12352.cpp | 170 ++++++ 12353.cpp | 38 ++ 12354.cpp | 108 ++++ 12355.cpp | 60 +++ 12356.cpp | 63 +++ 12357.cpp | 46 ++ 12358.cpp | 69 +++ 12359.cpp | 92 ++++ 1236.cpp | 192 +++++++ 12360.cpp | 154 ++++++ 12361.cpp | 179 +++++++ 12362.cpp | 163 ++++++ 12363.cpp | 86 +++ 12364.cpp | 63 +++ 12365.cpp | 100 ++++ 12366.cpp | 72 +++ 12367.cpp | 192 +++++++ 12368.cpp | 129 +++++ 12369.cpp | 99 ++++ 1237.cpp | 80 ++- 12370.cpp | 123 +++++ 12371.cpp | 64 +++ 12372.cpp | 18 +- 12373.cpp | 109 ++++ 12374.cpp | 182 +++++++ 12375.cpp | 105 ++++ 12376.cpp | 68 +++ 12377.cpp | 70 +++ 12378.cpp | 100 ++++ 12379.cpp | 74 +++ 1238.cpp | 71 +++ 12380.cpp | 84 +++ 12381.cpp | 117 +++++ 12382.cpp | 38 ++ 12383.cpp | 66 +++ 12384.cpp | 83 +++ 12385.cpp | 46 ++ 12386.cpp | 157 ++++++ 12387.cpp | 103 ++++ 12388.cpp | 147 ++++++ 12389.cpp | 318 +++++++++++ 1239.cpp | 54 ++ 12390.cpp | 56 ++ 12391.cpp | 64 +++ 12392.cpp | 103 ++++ 12393.cpp | 37 ++ 12394.cpp | 166 ++++++ 12395.cpp | 48 ++ 12396.cpp | 71 +++ 12397.cpp | 20 + 12398.cpp | 46 ++ 12399.cpp | 114 ++++ 124.cpp | 86 +++ 12400.cpp | 76 +++ 12401.cpp | 217 ++++++++ 12402.cpp | 147 ++++++ 12403.cpp | 32 +- 12404.cpp | 63 +++ 12405.cpp | 45 +- 12406.cpp | 97 ++-- 12407.cpp | 44 ++ 12408.cpp | 23 + 12409.cpp | 47 ++ 1241.cpp | 71 +++ 12410.cpp | 139 +++++ 12411.cpp | 48 ++ 12412.cpp | 255 +++++++++ 12414.cpp | 56 ++ 12415.cpp | 329 ++++++++++++ 12416.cpp | 46 +- 12418.cpp | 253 +++++++++ 1242.cpp | 64 +++ 12420.cpp | 108 ++++ 12424.cpp | 266 ++++++++++ 12425.cpp | 62 +++ 12426.cpp | 166 ++++++ 12427.cpp | 19 + 12428.cpp | 36 ++ 12429.cpp | 69 +++ 12430.cpp | 91 ++++ 12431.cpp | 76 +++ 12432.cpp | 103 ++++ 12433.cpp | 175 ++++++ 12435.cpp | 55 ++ 12436.cpp | 189 +++++++ 12437.cpp | 93 ++++ 12438.cpp | 64 +++ 12439.cpp | 72 +++ 1244.cpp | 149 ++++++ 12440.cpp | 146 +++++ 12441.cpp | 146 +++++ 12442.cpp | 114 ++++ 12443.cpp | 55 ++ 12444.cpp | 77 +++ 12445.cpp | 178 +++++++ 12446.cpp | 34 ++ 12447.cpp | 48 ++ 12448.cpp | 95 ++++ 12449.cpp | 91 ++++ 1245.cpp | 93 ++++ 12450.cpp | 51 ++ 12451.cpp | 79 +++ 12452.cpp | 90 ++++ 12453.cpp | 106 ++++ 12454.cpp | 72 +++ 12455.cpp | 34 ++ 12456.cpp | 27 + 12457.cpp | 49 ++ 12458.cpp | 92 ++++ 12459.cpp | 22 + 1246.cpp | 81 +++ 12460.cpp | 115 ++++ 12461.cpp | 27 + 12462.cpp | 77 +++ 12463.cpp | 21 + 12464.cpp | 39 ++ 12465.cpp | 42 ++ 12466.cpp | 82 +++ 12467.cpp | 65 +++ 12468.cpp | 21 + 12469.cpp | 41 ++ 1247.cpp | 103 ++++ 12470.cpp | 81 +++ 12471.cpp | 96 ++++ 12472.cpp | 109 ++++ 12473.cpp | 46 ++ 12474.cpp | 75 +++ 12475.cpp | 34 ++ 12476.cpp | 134 +++++ 12477.cpp | 165 ++++++ 12478.cpp | 9 + 12479.cpp | 161 ++++++ 1248.cpp | 39 ++ 12480.cpp | 155 ++++++ 12481.cpp | 77 +++ 12482.cpp | 33 ++ 12483.cpp | 62 +++ 12484.cpp | 27 + 12485.cpp | 43 ++ 12486.cpp | 88 ++++ 12487.cpp | 65 +++ 12488.cpp | 33 ++ 12489.cpp | 106 ++++ 1249.cpp | 605 +++++++++++++++++++++ 12490.cpp | 88 ++++ 12491.cpp | 79 +++ 12492.cpp | 165 ++++++ 12493.cpp | 36 ++ 12494.cpp | 45 ++ 12495.cpp | 26 + 12496.cpp | 41 ++ 12497.cpp | 80 +++ 12498.cpp | 59 +++ 12499.cpp | 34 ++ 125.cpp | 88 ++++ 1250.cpp | 45 ++ 12501.cpp | 112 ++++ 12502.cpp | 11 + 12503.cpp | 39 ++ 12504.cpp | 228 ++++++++ 12505.cpp | 80 +++ 12506.cpp | 69 +++ 12507.cpp | 96 ++++ 12508.cpp | 91 ++++ 12509.cpp | 229 ++++++++ 1251.cpp | 75 +++ 12510.cpp | 179 +++++++ 12511.cpp | 48 ++ 12512.cpp | 231 ++++++++ 12514.cpp | 262 +++++++++ 12515.cpp | 66 +++ 12516.cpp | 40 ++ 12517.cpp | 27 + 12518.cpp | 93 ++++ 12519.cpp | 91 ++++ 1252.cpp | 61 +++ 12520.cpp | 64 +++ 12521.cpp | 88 ++++ 12522.cpp | 192 +++++++ 12523.cpp | 49 ++ 12524.cpp | 96 ++++ 12525.cpp | 43 ++ 12526.cpp | 56 ++ 12527.cpp | 59 +++ 12528.cpp | 89 ++++ 12529.cpp | 80 +++ 1253.cpp | 164 ++++++ 12530.cpp | 97 ++++ 12531.cpp | 80 +++ 12532.cpp | 151 ++++++ 12533.cpp | 210 ++++++++ 12534.cpp | 141 +++++ 12535.cpp | 55 ++ 12536.cpp | 131 +++++ 12537.cpp | 71 +++ 12538.cpp | 155 ++++++ 12539.cpp | 160 ++++++ 1254.cpp | 263 ++++++++++ 12540.cpp | 86 +++ 12541.cpp | 72 +++ 12542.cpp | 75 +++ 12543.cpp | 52 ++ 12544.cpp | 84 +++ 12545.cpp | 34 ++ 12546.cpp | 77 +++ 12547.cpp | 137 +++++ 12548.cpp | 245 +++++++++ 12549.cpp | 147 ++++++ 1255.cpp | 111 ++++ 12550.cpp | 78 +++ 12551.cpp | 87 +++ 12553.cpp | 179 +++++++ 12554.cpp | 72 +++ 12555.cpp | 66 +++ 12556.cpp | 252 +++++++++ 12557.cpp | 73 +++ 12558.cpp | 104 ++++ 12559.cpp | 122 +++++ 1256.cpp | 113 ++++ 12560.cpp | 106 ++++ 12561.cpp | 74 +++ 12562.cpp | 158 ++++++ 12563.cpp | 52 ++ 12564.cpp | 39 ++ 12569.cpp | 111 ++++ 12571.cpp | 30 ++ 12572.cpp | 48 ++ 12573.cpp | 53 ++ 12574.cpp | 26 + 12575.cpp | 31 ++ 12576.cpp | 61 +++ 12577.cpp | 41 ++ 12578.cpp | 34 ++ 1258.cpp | 36 ++ 12580.cpp | 73 +++ 12581.cpp | 101 ++++ 12582.cpp | 41 ++ 12583.cpp | 56 ++ 12584.cpp | 91 ++++ 12585.cpp | 46 ++ 12586.cpp | 54 ++ 12587.cpp | 191 +++++++ 12588.cpp | 121 +++++ 12589.cpp | 57 ++ 1259.cpp | 192 +++++++ 12590.cpp | 181 +++++++ 12592.cpp | 38 ++ 12593.cpp | 134 +++++ 12594.cpp | 76 +++ 12595.cpp | 127 +++++ 12596.cpp | 125 +++++ 12597.cpp | 45 ++ 12598.cpp | 111 ++++ 12599.cpp | 81 +++ 126.cpp | 128 +++++ 1260.cpp | 51 +- 12600.cpp | 284 ++++++++++ 12602.cpp | 20 + 12603.cpp | 73 +++ 12604.cpp | 83 +++ 12605.cpp | 188 +++++++ 12606.cpp | 188 +++++++ 12607.cpp | 108 ++++ 12608.cpp | 57 ++ 12609.cpp | 111 ++++ 1261.cpp | 43 ++ 12610.cpp | 256 +++++++++ 12611.cpp | 21 + 12612.cpp | 101 ++++ 12613.cpp | 113 ++++ 12614.cpp | 21 + 12616.cpp | 117 +++++ 12617.cpp | 165 ++++++ 12618.cpp | 248 +++++++++ 12619.cpp | 133 +++++ 1262.cpp | 123 +++++ 12620.cpp | 39 ++ 12621.cpp | 35 ++ 12622.cpp | 59 +++ 12623.cpp | 71 +++ 12625.cpp | 84 +++ 12626.cpp | 26 + 12627.cpp | 34 ++ 12629.cpp | 100 ++++ 1263.cpp | 75 +++ 12630.cpp | 13 + 12631.cpp | 150 ++++++ 12632.cpp | 190 +++++++ 12633.cpp | 132 +++++ 12634.cpp | 192 +++++++ 12635.cpp | 87 +++ 12636.cpp | 193 +++++++ 12637.cpp | 121 +++++ 12638.cpp | 54 ++ 12639.cpp | 159 ++++++ 1264.cpp | 103 ++++ 12640.cpp | 81 +++ 12641.cpp | 53 ++ 12642.cpp | 49 ++ 12643.cpp | 24 + 12644.cpp | 96 ++++ 12645.cpp | 82 +++ 12646.cpp | 28 + 12647.cpp | 295 +++++++++++ 12648.cpp | 105 ++++ 12649.cpp | 82 +++ 1265.cpp | 106 ++++ 12650.cpp | 40 ++ 12651.cpp | 39 ++ 12652.cpp | 117 +++++ 12653.cpp | 69 +++ 12654.cpp | 52 ++ 12655.cpp | 167 ++++++ 12656.cpp | 53 ++ 12657.cpp | 101 ++++ 12658.cpp | 33 ++ 12659.cpp | 156 ++++++ 1266.cpp | 77 +++ 12660.cpp | 224 ++++++++ 12661.cpp | 72 +++ 12662.cpp | 61 +++ 12663.cpp | 70 +++ 12664.cpp | 72 +++ 12665.cpp | 32 ++ 12667.cpp | 38 ++ 12668.cpp | 106 ++++ 12669.cpp | 195 +++++++ 1267.cpp | 78 +++ 12670.cpp | 101 ++++ 12671.cpp | 81 +++ 12672.cpp | 85 +++ 12673.cpp | 59 +++ 12674.cpp | 77 +++ 12675.cpp | 187 +++++++ 12676.cpp | 45 ++ 12677.cpp | 121 +++++ 12679.cpp | 189 +++++++ 12680.cpp | 171 ++++++ 12681.cpp | 76 +++ 12682.cpp | 158 ++++++ 12683.cpp | 99 ++++ 12684.cpp | 116 ++++ 12685.cpp | 45 ++ 12686.cpp | 97 ++++ 12688.cpp | 25 + 12689.cpp | 80 +++ 12690.cpp | 23 + 12691.cpp | 271 ++++++++++ 12692.cpp | 61 +++ 12693.cpp | 107 ++++ 12694.cpp | 51 ++ 12696.cpp | 28 + 12697.cpp | 74 +++ 12698.cpp | 224 ++++++++ 12699.cpp | 34 ++ 127.cpp | 122 +++++ 12700.cpp | 46 ++ 12701.cpp | 75 +++ 12702.cpp | 76 +++ 12703.cpp | 55 ++ 12704.cpp | 14 + 12705.cpp | 45 ++ 12706.cpp | 60 +++ 12707.cpp | 57 ++ 12708.cpp | 13 + 12709.cpp | 20 + 12710.cpp | 86 +++ 12711.cpp | 120 +++++ 12712.cpp | 30 ++ 12713.cpp | 64 +++ 12714.cpp | 29 + 12715.cpp | 138 +++++ 12716.cpp | 26 + 12717.cpp | 90 ++++ 12718.cpp | 37 ++ 12719.cpp | 177 +++++++ 12720.cpp | 47 ++ 12721.cpp | 109 ++++ 12722.cpp | 99 ++++ 12723.cpp | 62 +++ 12724.cpp | 68 +++ 12725.cpp | 29 + 12726.cpp | 67 +++ 12727.cpp | 103 ++++ 12728.cpp | 176 +++++++ 12729.cpp | 31 ++ 12730.cpp | 36 ++ 12743.cpp | 61 +++ 12745.cpp | 96 ++++ 12747.cpp | 51 ++ 12748.cpp | 42 ++ 12749.cpp | 64 +++ 12750.cpp | 53 ++ 12751.cpp | 17 + 12753.cpp | 52 ++ 12754.cpp | 80 +++ 12755.cpp | 53 ++ 12761.cpp | 84 +++ 12763.cpp | 76 +++ 12764.cpp | 76 +++ 12765.cpp | 53 ++ 12766.cpp | 259 +++++++++ 12768.cpp | 64 +++ 12769.cpp | 73 +++ 12770.cpp | 36 ++ 12775.cpp | 81 +++ 12776.cpp | 175 ++++++ 12778.cpp | 153 ++++++ 12779.cpp | 101 ++++ 12783.cpp | 65 +++ 12784.cpp | 73 +++ 12785.cpp | 90 ++++ 12786.cpp | 36 ++ 12787.cpp | 175 ++++++ 12788.cpp | 40 ++ 12789.cpp | 252 +++++++++ 1279.cpp | 192 +++++++ 12790.cpp | 99 ++++ 12791.cpp | 30 ++ 12792.cpp | 48 ++ 12793.cpp | 59 +++ 12794.cpp | 116 ++++ 12795.cpp | 194 +++++++ 12796.cpp | 91 ++++ 12797.cpp | 96 ++++ 12798.cpp | 45 ++ 12799.cpp | 60 +++ 128.cpp | 68 +++ 1280.cpp | 93 ++++ 12800.cpp | 65 +++ 12801.cpp | 44 ++ 12802.cpp | 48 ++ 12803.cpp | 151 ++++++ 12804.cpp | 81 +++ 12805.cpp | 66 +++ 12806.cpp | 138 +++++ 12808.cpp | 26 + 12809.cpp | 53 ++ 1281.cpp | 107 ++++ 12810.cpp | 36 ++ 12811.cpp | 65 +++ 12812.cpp | 61 +++ 12813.cpp | 91 ++++ 12814.cpp | 52 ++ 12815.cpp | 51 ++ 12816.cpp | 71 +++ 12817.cpp | 26 + 12818.cpp | 206 ++++++++ 12819.cpp | 151 ++++++ 1282.cpp | 183 +++++++ 12820.cpp | 36 ++ 12821.cpp | 100 ++++ 12822.cpp | 61 +++ 12825.cpp | 73 +++ 12826.cpp | 41 ++ 12828.cpp | 100 ++++ 1283.cpp | 94 ++++ 12830.cpp | 76 +++ 12831.cpp | 161 ++++++ 12832.cpp | 41 ++ 12833.cpp | 130 +++++ 12834.cpp | 33 ++ 12836.cpp | 131 +++++ 12837.cpp | 132 +++++ 12839.cpp | 34 ++ 12840.cpp | 58 ++ 12841.cpp | 106 ++++ 12842.cpp | 24 + 12843.cpp | 43 ++ 12844.cpp | 82 +++ 12846.cpp | 78 +++ 12848.cpp | 23 + 12849.cpp | 76 +++ 1285.cpp | 210 ++++++++ 12850.cpp | 65 +++ 12851.cpp | 24 + 12852.cpp | 36 ++ 12853.cpp | 19 + 12854.cpp | 27 + 12855.cpp | 70 +++ 12856.cpp | 38 ++ 12857.cpp | 166 ++++++ 12858.cpp | 66 +++ 12859.cpp | 153 ++++++ 12860.cpp | 110 ++++ 12861.cpp | 41 ++ 12862.cpp | 65 +++ 12863.cpp | 185 +++++++ 12864.cpp | 67 +++ 12865.cpp | 45 ++ 12866.cpp | 77 +++ 12869.cpp | 13 + 1287.cpp | 263 ++++++++++ 12870.cpp | 100 ++++ 12871.cpp | 103 ++++ 12872.cpp | 99 ++++ 12873.cpp | 144 +++++ 12874.cpp | 84 +++ 12875.cpp | 65 +++ 12876.cpp | 68 +++ 12877.cpp | 116 ++++ 12878.cpp | 90 ++++ 12879.cpp | 108 ++++ 12880.cpp | 131 +++++ 12881.cpp | 115 ++++ 12882.cpp | 316 +++++++++++ 12883.cpp | 135 +++++ 12884.cpp | 151 ++++++ 12885.cpp | 110 ++++ 12886.cpp | 188 +++++++ 12887.cpp | 54 ++ 12888.cpp | 91 ++++ 12889.cpp | 87 +++ 1289.cpp | 88 ++++ 12890.cpp | 37 ++ 12891.cpp | 216 ++++++++ 12892.cpp | 97 ++++ 12893.cpp | 14 + 12894.cpp | 118 +++++ 12895.cpp | 30 ++ 12896.cpp | 25 + 12897.cpp | 44 ++ 12898.cpp | 37 ++ 12899.cpp | 112 ++++ 129.cpp | 110 ++++ 1290.cpp | 108 ++++ 12900.cpp | 180 +++++++ 12901.cpp | 100 ++++ 12902.cpp | 119 +++++ 12903.cpp | 219 ++++++++ 12904.cpp | 82 +++ 12905.cpp | 63 +++ 12906.cpp | 74 +++ 12907.cpp | 93 ++++ 12908.cpp | 23 + 12909.cpp | 26 + 1291.cpp | 76 +++ 12910.cpp | 78 +++ 12911.cpp | 57 ++ 12912.cpp | 59 +++ 12913.cpp | 38 ++ 12914.cpp | 91 ++++ 12915.cpp | 58 ++ 12916.cpp | 32 ++ 12917.cpp | 20 + 12918.cpp | 19 + 12919.cpp | 28 + 1292.cpp | 50 ++ 12920.cpp | 103 ++++ 12921.cpp | 54 ++ 12922.cpp | 72 +++ 12923.cpp | 119 +++++ 12926.cpp | 74 +++ 12927.cpp | 151 ++++++ 12928.cpp | 79 +++ 12929.cpp | 92 ++++ 12930.cpp | 95 ++++ 12931.cpp | 229 ++++++++ 12932.cpp | 42 ++ 12933.cpp | 142 +++++ 12934.cpp | 36 ++ 12936.cpp | 53 ++ 12937.cpp | 82 +++ 12938.cpp | 27 + 12939.cpp | 145 +++++ 12940.cpp | 66 +++ 12942.cpp | 130 +++++ 12943.cpp | 139 +++++ 12944.cpp | 107 ++++ 12945.cpp | 86 +++ 12946.cpp | 20 + 12947.cpp | 123 +++++ 12948.cpp | 96 ++++ 12949.cpp | 94 ++++ 12950.cpp | 81 +++ 12951.cpp | 45 ++ 12952.cpp | 13 + 12953.cpp | 88 ++++ 12954.cpp | 55 ++ 12955.cpp | 18 + 12956.cpp | 60 +++ 12957.cpp | 78 +++ 12959.cpp | 25 + 12960.cpp | 45 ++ 12961.cpp | 75 +++ 12962.cpp | 29 + 12963.cpp | 143 +++++ 12965.cpp | 39 ++ 12966.cpp | 285 ++++++++++ 12967.cpp | 18 + 12968.cpp | 84 +++ 12970.cpp | 93 ++++ 12972.cpp | 244 +++++++++ 12974.cpp | 126 +++++ 12976.cpp | 41 ++ 12978.cpp | 76 +++ 1298.cpp | 260 +++++++++ 12981.cpp | 48 ++ 12983.cpp | 141 +++++ 12984.cpp | 59 +++ 12987.cpp | 76 +++ 12988.cpp | 73 +++ 12992.cpp | 16 + 12994.cpp | 79 +++ 12995.cpp | 42 ++ 12996.cpp | 22 + 12997.cpp | 31 ++ 12999.cpp | 143 +++++ 130.cpp | 119 +++-- 13000.cpp | 168 ++++++ 13001.cpp | 271 ++++++++++ 13003.cpp | 83 +++ 13004.cpp | 49 ++ 13005.cpp | 163 ++++++ 13006.cpp | 80 +++ 13007.cpp | 38 ++ 13008.cpp | 97 ++++ 13009.cpp | 129 +++++ 13010.cpp | 106 ++++ 13011.cpp | 130 +++++ 13012.cpp | 19 + 13013.cpp | 59 +++ 13014.cpp | 81 +++ 13015.cpp | 53 ++ 13017.cpp | 35 ++ 13018.cpp | 29 + 13021.cpp | 55 ++ 13022.cpp | 43 ++ 13024.cpp | 115 ++++ 13025.cpp | 9 + 13026.cpp | 27 + 13028.cpp | 46 ++ 13029.cpp | 43 ++ 1303.cpp | 121 +++++ 13031.cpp | 28 + 13032.cpp | 44 ++ 13034.cpp | 17 + 13035.cpp | 39 ++ 13036.cpp | 82 +++ 13037.cpp | 59 +++ 13038.cpp | 47 ++ 13039.cpp | 32 ++ 1304.cpp | 236 +++++++++ 13045.cpp | 27 + 13046.cpp | 80 +++ 13047.cpp | 37 ++ 13048.cpp | 34 ++ 13049.cpp | 25 + 13052.cpp | 23 + 13053.cpp | 21 + 13054.cpp | 42 ++ 13055.cpp | 37 ++ 13056.cpp | 17 + 13057.cpp | 120 +++++ 13058.cpp | 29 + 13059.cpp | 13 + 1306.cpp | 205 ++++++++ 13061.cpp | 36 ++ 13062.cpp | 34 ++ 13067.cpp | 47 ++ 13068.cpp | 47 ++ 13069.cpp | 41 ++ 13070.cpp | 33 ++ 13071.cpp | 18 + 13072.cpp | 66 +++ 13073.cpp | 43 ++ 13074.cpp | 39 ++ 13075.cpp | 31 ++ 13078.cpp | 62 +++ 13079.cpp | 36 ++ 1308.cpp | 120 +++++ 13080.cpp | 56 ++ 13081.cpp | 41 ++ 13082.cpp | 34 ++ 13085.cpp | 52 ++ 13086.cpp | 25 + 13088.cpp | 31 ++ 1309.cpp | 186 +++++++ 13090.cpp | 16 + 13091.cpp | 36 ++ 13092.cpp | 96 ++++ 13093.cpp | 21 + 13094.cpp | 95 ++++ 13095.cpp | 30 ++ 13096.cpp | 20 + 13097.cpp | 30 ++ 13098.cpp | 31 ++ 13099.cpp | 13 + 131.cpp | 354 +++++++++++++ 1310.cpp | 113 ++++ 13100.cpp | 229 ++++++++ 13101.cpp | 207 ++++++++ 13103.cpp | 41 ++ 13104.cpp | 127 +++++ 13105.cpp | 148 ++++++ 13106.cpp | 38 ++ 13107.cpp | 40 ++ 13108.cpp | 25 + 13109.cpp | 22 + 13110.cpp | 173 ++++++ 13111.cpp | 64 +++ 13112.cpp | 109 ++++ 13113.cpp | 44 ++ 13114.cpp | 63 +++ 13115.cpp | 34 ++ 13116.cpp | 83 +++ 13117.cpp | 91 ++++ 13118.cpp | 72 +++ 13119.cpp | 88 ++++ 1312.cpp | 79 +++ 13121.cpp | 39 ++ 13122.cpp | 34 ++ 13123.cpp | 229 ++++++++ 13124.cpp | 70 +++ 13125.cpp | 69 +++ 13126.cpp | 236 +++++++++ 13127.cpp | 100 ++++ 13128.cpp | 107 ++++ 13129.cpp | 95 ++++ 13130.cpp | 22 + 13131.cpp | 82 +++ 13132.cpp | 45 ++ 13133.cpp | 103 ++++ 13134.cpp | 54 ++ 13135.cpp | 27 + 13136.cpp | 124 +++++ 13137.cpp | 77 +++ 1314.cpp | 72 +++ 13140.cpp | 41 ++ 13141.cpp | 14 + 13142.cpp | 25 + 13144.cpp | 239 +++++++++ 13145.cpp | 29 + 13146.cpp | 62 +++ 13147.cpp | 123 +++++ 13148.cpp | 24 + 13149.cpp | 172 ++++++ 1315.cpp | 24 + 13154.cpp | 99 ++++ 1316.cpp | 35 ++ 13161.cpp | 21 + 1317.cpp | 169 ++++++ 1319.cpp | 71 +++ 132.cpp | 203 +++++++ 1322.cpp | 52 ++ 1324.cpp | 192 +++++++ 1325.cpp | 76 +++ 1326.cpp | 73 +++ 1327.cpp | 103 ++++ 1328.cpp | 39 ++ 1329.cpp | 51 ++ 133.cpp | 67 +++ 1330.cpp | 65 +++ 1331.cpp | 89 ++++ 1333.cpp | 36 ++ 1335.cpp | 73 +++ 1336.cpp | 72 +++ 1337.cpp | 616 ++++++++++++++++++++++ 1339.cpp | 40 ++ 134.cpp | 110 ++++ 1340.cpp | 299 +++++++++++ 1341.cpp | 145 +++++ 1342.cpp | 134 +++++ 1343.cpp | 111 ++++ 1344.cpp | 75 +++ 1346.cpp | 35 ++ 1347.cpp | 52 ++ 1349.cpp | 136 +++++ 135.cpp | 59 +++ 1350.cpp | 103 ++++ 1351.cpp | 91 ++++ 1352.cpp | 87 +++ 1354.cpp | 82 +++ 1356.cpp | 86 +++ 1357.cpp | 177 +++++++ 1358.cpp | 150 ++++++ 136.cpp | 63 +-- 1361.cpp | 227 ++++++++ 1362.cpp | 43 ++ 1363.cpp | 43 ++ 1364.cpp | 154 ++++++ 1365.cpp | 102 ++++ 1366.cpp | 96 ++++ 1367.cpp | 283 ++++++++++ 1368.cpp | 41 ++ 137.cpp | 291 ++++++++++ 1371.cpp | 59 +++ 1372.cpp | 45 ++ 1374.cpp | 45 ++ 1376.cpp | 143 +++++ 1377.cpp | 140 +++++ 1378.cpp | 96 ++++ 1379.cpp | 142 +++++ 138.cpp | 22 +- 1380.cpp | 133 +++++ 1381.cpp | 43 ++ 1382.cpp | 79 +++ 1383.cpp | 101 ++++ 1385.cpp | 159 ++++++ 1386.cpp | 75 +++ 1388.cpp | 20 + 139.cpp | 77 +++ 1390.cpp | 180 +++++++ 1391.cpp | 190 +++++++ 1392.cpp | 46 ++ 1393.cpp | 57 ++ 1394.cpp | 67 +++ 1395.cpp | 85 +++ 1396.cpp | 238 +++++++++ 1397.cpp | 281 ++++++++++ 1398.cpp | 92 ++++ 1399.cpp | 132 +++++ 140.cpp | 126 +++++ 1400.cpp | 153 ++++++ 1401.cpp | 81 +++ 1402.cpp | 267 ++++++++++ 1404.cpp | 79 +++ 1406.cpp | 81 +++ 1407.cpp | 111 ++++ 1408.cpp | 111 ++++ 141.cpp | 111 ++++ 1411.cpp | 118 +++++ 1412.cpp | 199 +++++++ 1413.cpp | 61 +++ 1414.cpp | 61 +++ 1415.cpp | 61 +++ 1416.cpp | 178 +++++++ 1417.cpp | 153 ++++++ 1418.cpp | 27 + 1419.cpp | 110 ++++ 142.cpp | 98 ++++ 1420.cpp | 99 ++++ 1421.cpp | 70 +++ 1422.cpp | 87 +++ 1423.cpp | 91 ++++ 1424.cpp | 48 ++ 1425.cpp | 96 ++++ 1426.cpp | 110 ++++ 1427.cpp | 85 +++ 1428.cpp | 80 +++ 143.cpp | 126 +++++ 1431.cpp | 286 ++++++++++ 1432.cpp | 67 +++ 1433.cpp | 156 ++++++ 1434.cpp | 59 +++ 1435.cpp | 71 +++ 1436.cpp | 123 +++++ 1437.cpp | 62 +++ 1438.cpp | 300 +++++++++++ 1439.cpp | 89 ++++ 144.cpp | 51 ++ 1440.cpp | 206 ++++++++ 1442.cpp | 47 ++ 1443.cpp | 83 +++ 1445.cpp | 31 ++ 1449.cpp | 164 ++++++ 145.cpp | 71 +++ 1450.cpp | 85 +++ 1451.cpp | 56 ++ 1452.cpp | 65 +++ 1453.cpp | 174 ++++++ 1455.cpp | 172 ++++++ 1456.cpp | 95 ++++ 146.cpp | 37 +- 1462.cpp | 140 +++++ 1464.cpp | 208 ++++++++ 1466.cpp | 175 ++++++ 1467.cpp | 87 +++ 1468.cpp | 58 ++ 1469.cpp | 132 +++++ 147.cpp | 84 ++- 1470.cpp | 102 ++++ 1471.cpp | 118 +++++ 1473.cpp | 56 ++ 1474.cpp | 82 +++ 1475.cpp | 174 ++++++ 1476.cpp | 52 ++ 1478.cpp | 138 +++++ 1479.cpp | 287 ++++++++++ 148.cpp | 97 ++++ 1480.cpp | 115 ++++ 1481.cpp | 41 ++ 1482.cpp | 38 ++ 1484.cpp | 91 ++++ 1485.cpp | 93 ++++ 1486.cpp | 131 +++++ 1487.cpp | 26 + 1488.cpp | 634 ++++++++++++++++++++++ 1489.cpp | 132 +++++ 149.cpp | 226 ++++++++ 1492.cpp | 138 +++++ 1493.cpp | 158 ++++++ 1494.cpp | 112 ++++ 1498.cpp | 53 ++ 150.cpp | 109 ++++ 1501.cpp | 285 ++++++++++ 1502.cpp | 344 ++++++++++++ 1508.cpp | 104 ++++ 1509.cpp | 75 +++ 151.cpp | 98 ++-- 1510.cpp | 33 ++ 1511.cpp | 72 +++ 1513.cpp | 71 +++ 1514.cpp | 101 ++++ 1515.cpp | 158 ++++++ 1516.cpp | 165 ++++++ 1517.cpp | 146 +++++ 1518.cpp | 86 +++ 1519.cpp | 159 ++++++ 152.cpp | 95 +++- 1521.cpp | 48 ++ 1523.cpp | 40 ++ 1524.cpp | 30 ++ 1525.cpp | 63 +++ 1526.cpp | 109 ++++ 1529.cpp | 42 ++ 153.cpp | 144 +++-- 1530.cpp | 70 +++ 1531.cpp | 138 +++++ 1533.cpp | 135 +++++ 1534.cpp | 77 +++ 1537.cpp | 217 ++++++++ 154.cpp | 75 +++ 1541.cpp | 77 +++ 1543.cpp | 59 +++ 1544.cpp | 211 ++++++++ 1546.cpp | 36 ++ 1548.cpp | 122 +++++ 1549.cpp | 128 +++++ 155.cpp | 58 ++ 1554.cpp | 57 ++ 1555.cpp | 45 ++ 1556.cpp | 78 +++ 1557.cpp | 26 + 1558.cpp | 59 +++ 1559.cpp | 45 ++ 156.cpp | 122 ++--- 1560.cpp | 74 +++ 1564.cpp | 129 +++++ 1566.cpp | 45 ++ 1569.cpp | 151 ++++++ 157.cpp | 236 +++++++++ 1571.cpp | 243 +++++++++ 1572.cpp | 56 ++ 1574.cpp | 207 ++++++++ 1575.cpp | 66 +++ 1577.cpp | 48 ++ 1579.cpp | 98 ++++ 158.cpp | 188 +++++++ 1580.cpp | 81 +++ 1581.cpp | 345 ++++++++++++ 1583.cpp | 52 ++ 1584.cpp | 21 + 1585.cpp | 25 + 1586.cpp | 39 ++ 1587.cpp | 51 ++ 1588.cpp | 31 ++ 1589.cpp | 187 +++++++ 159.cpp | 151 ++++++ 1590.cpp | 105 ++++ 1591.cpp | 32 ++ 1592.cpp | 83 +++ 1593.cpp | 43 ++ 1594.cpp | 58 ++ 1595.cpp | 38 ++ 1596.cpp | 110 ++++ 1597.cpp | 124 +++++ 1598.cpp | 153 ++++++ 1599.cpp | 131 +++++ 160.cpp | 141 +++-- 1600.cpp | 71 +++ 1601.cpp | 167 ++++++ 1602.cpp | 214 ++++++++ 1603.cpp | 229 ++++++++ 1604.cpp | 247 +++++++++ 1605.cpp | 22 + 1606.cpp | 85 +++ 1607.cpp | 71 +++ 1608.cpp | 56 ++ 1609.cpp | 145 +++++ 161.cpp | 103 ++-- 1610.cpp | 107 ++++ 1611.cpp | 57 ++ 1612.cpp | 102 ++++ 1613.cpp | 107 ++++ 1614.cpp | 40 ++ 1615.cpp | 44 ++ 1616.cpp | 62 +++ 1617.cpp | 48 ++ 1618.cpp | 240 +++++++++ 1619.cpp | 91 ++++ 162.cpp | 149 ++++++ 1620.cpp | 63 +++ 1623.cpp | 65 +++ 1624.cpp | 84 +++ 1625.cpp | 65 +++ 1626.cpp | 83 +++ 1627.cpp | 134 +++++ 1628.cpp | 106 ++++ 1629.cpp | 58 ++ 1630.cpp | 91 ++++ 1631.cpp | 47 ++ 1632.cpp | 49 ++ 1633.cpp | 105 ++++ 1634.cpp | 223 ++++++++ 1635.cpp | 98 ++++ 1636.cpp | 29 + 1637.cpp | 119 +++++ 1638.cpp | 34 ++ 1639.cpp | 37 ++ 164.cpp | 138 +++++ 1640.cpp | 90 ++++ 1641.cpp | 53 ++ 1642.cpp | 65 +++ 1643.cpp | 137 +++++ 1644.cpp | 48 ++ 1645.cpp | 23 + 1646.cpp | 43 ++ 1647.cpp | 32 ++ 1648.cpp | 23 + 1649.cpp | 77 +++ 165.cpp | 113 ++++ 1650.cpp | 46 ++ 1653.cpp | 64 +++ 1654.cpp | 64 +++ 1658.cpp | 118 +++++ 1659.cpp | 126 +++++ 166.cpp | 162 ++++++ 1660.cpp | 168 ++++++ 1661.cpp | 295 +++++++++++ 1662.cpp | 178 +++++++ 1663.cpp | 226 ++++++++ 1664.cpp | 81 +++ 1665.cpp | 89 ++++ 1666.cpp | 161 ++++++ 1667.cpp | 111 ++++ 1668.cpp | 58 ++ 1669.cpp | 92 ++++ 167.cpp | 87 +++ 1671.cpp | 87 +++ 1673.cpp | 139 +++++ 1674.cpp | 105 ++++ 1676.cpp | 275 ++++++++++ 168.cpp | 118 +++++ 1683.cpp | 330 ++++++++++++ 1684.cpp | 107 ++++ 1687.cpp | 162 ++++++ 169.cpp | 164 ++++++ 170.cpp | 169 ++++++ 1709.cpp | 34 ++ 171.cpp | 233 ++++++++ 1711.cpp | 135 +++++ 1712.cpp | 85 +++ 1714.cpp | 128 +++++ 1718.cpp | 189 +++++++ 172.cpp | 140 +++++ 1726.cpp | 74 +++ 1727.cpp | 33 ++ 1729.cpp | 24 + 173.cpp | 111 ++++ 1730.cpp | 28 + 174.cpp | 219 ++++++++ 175.cpp | 92 ++++ 176.cpp | 383 ++++++++++++++ 177.cpp | 165 ++++++ 178.cpp | 145 +++++ 179.cpp | 303 +++++++++++ 180.cpp | 46 ++ 181.cpp | 221 ++++++++ 182.cpp | 129 +++++ 183.cpp | 209 ++++++++ 184.cpp | 117 +++++ 185.cpp | 268 ++++++++++ 186.cpp | 157 ++++++ 187.cpp | 77 +++ 188.cpp | 99 ++++ 189.cpp | 200 +++++++ 190.cpp | 85 +++ 191.cpp | 131 +++++ 192.cpp | 175 ++++++ 193.cpp | 149 ++++++ 194.cpp | 406 ++++++++++++++ 195.cpp | 148 ++++-- 196.cpp | 106 ++++ 197.cpp | 276 ++++++++++ 198.cpp | 398 ++++++++++++++ 199.cpp | 84 +++ 200.cpp | 130 +++++ 201.cpp | 89 ++++ 202.cpp | 74 +++ 205.cpp | 275 ++++++++++ 206.cpp | 98 ++++ 207.cpp | 174 ++++++ 208.cpp | 125 +++++ 209.cpp | 227 ++++++++ 210.cpp | 153 ++++++ 211.cpp | 110 ++++ 212.cpp | 181 +++++++ 213.cpp | 87 +++ 214.cpp | 196 +++++++ 215.cpp | 156 ++++++ 216.cpp | 110 ++++ 217.cpp | 147 ++++++ 218.cpp | 105 ++++ 219.cpp | 136 +++++ 220.cpp | 262 +++++++++ 221.cpp | 94 ++++ 222.cpp | 53 ++ 223.cpp | 148 ++++++ 225.cpp | 101 ++++ 227.cpp | 236 ++++++--- 229.cpp | 181 +++++++ 230.cpp | 116 ++++ 231.cpp | 194 +++++-- 232.cpp | 82 +++ 233.cpp | 110 ++++ 234.cpp | 110 ++++ 238.cpp | 168 ++++++ 239.cpp | 132 +++++ 240.cpp | 96 ++++ 242.cpp | 99 ++++ 243.cpp | 162 ++++++ 244.cpp | 99 ++++ 245.cpp | 78 +++ 246.cpp | 361 +++++++++++++ 247.cpp | 118 +++++ 248.cpp | 191 +++++++ 250.cpp | 56 ++ 251.cpp | 120 +++++ 253.cpp | 97 ++++ 254.cpp | 79 +++ 255.cpp | 178 +++++++ 256.cpp | 47 +- 257.cpp | 72 +++ 258.cpp | 222 ++++++++ 259.cpp | 129 +++++ 260.cpp | 179 ++++--- 261.cpp | 77 +++ 263.cpp | 200 ++++--- 264.cpp | 185 +++---- 265.cpp | 159 ++++++ 267.cpp | 35 ++ 268.cpp | 72 +++ 269.cpp | 138 +++++ 270.cpp | 78 +++ 271.cpp | 139 ++--- 272.cpp | 61 ++- 273.cpp | 122 +++++ 274.cpp | 105 ++++ 275.cpp | 62 +++ 276.cpp | 135 +++++ 277.cpp | 163 ++++++ 278.cpp | 48 ++ 279.cpp | 34 ++ 280.cpp | 145 +++++ 282.cpp | 68 +++ 283.cpp | 102 ++++ 284.cpp | 320 +++++++++++ 286.cpp | 164 ++++++ 288.cpp | 354 +++++++++++++ 290.cpp | 122 +++++ 291.cpp | 176 +++++-- 293.cpp | 71 +++ 294.cpp | 114 ++-- 295.cpp | 73 +++ 296.cpp | 105 ++++ 297.cpp | 81 +++ 298.cpp | 112 ++++ 299.cpp | 59 ++- 300.cpp | 46 ++ 301.cpp | 101 ++++ 302.cpp | 83 +++ 303.cpp | 109 ++++ 305.cpp | 14 + 306.cpp | 125 +++++ 307.cpp | 136 +++++ 308.cpp | 447 ++++++++++++++++ 309.cpp | 168 ++++++ 310.cpp | 104 ++++ 311.cpp | 133 +++++ 312.cpp | 112 ++++ 313.cpp | 62 +++ 314.cpp | 177 +++++++ 315.cpp | 129 +++++ 317.cpp | 75 +++ 318.cpp | 91 ++++ 319.cpp | 660 +++++++++++++++++++++++ 320.cpp | 60 +++ 321.cpp | 150 ++++++ 322.cpp | 346 ++++++++++++ 323.cpp | 98 ++++ 324.cpp | 100 ++++ 325.cpp | 128 +++++ 326.cpp | 35 ++ 327.cpp | 130 +++++ 328.cpp | 153 ++++++ 330.cpp | 92 ++++ 331.cpp | 111 ++++ 332.cpp | 60 +++ 333.cpp | 80 +++ 334.cpp | 136 +++++ 335.cpp | 140 +++++ 336.cpp | 183 ++++--- 337.cpp | 129 +++++ 338.cpp | 162 ++++++ 339.cpp | 281 ++++++++++ 340.cpp | 97 ++-- 341.cpp | 76 +++ 342.cpp | 106 ++++ 343.cpp | 99 ++-- 344.cpp | 147 ++++-- 345.cpp | 198 +++++++ 346.cpp | 85 +++ 347.cpp | 78 +++ 348.cpp | 115 ++++ 349.cpp | 165 ++++++ 350.cpp | 40 ++ 352.cpp | 120 ++--- 353.cpp | 101 ++-- 354.cpp | 178 +++++++ 355.cpp | 54 ++ 356.cpp | 28 + 357.cpp | 42 ++ 358.cpp | 49 ++ 359.cpp | 109 ++++ 361.cpp | 215 ++++++++ 362.cpp | 47 ++ 366.cpp | 103 ++++ 369.cpp | 61 +-- 370.cpp | 258 +++++++++ 371.cpp | 88 ++-- 372.cpp | 52 ++ 373.cpp | 65 +++ 374.cpp | 51 +- 375.cpp | 157 ++++++ 376.cpp | 117 +++++ 377.cpp | 161 ++++++ 378.cpp | 125 ++--- 379.cpp | 185 +++++++ 380.cpp | 109 ++++ 381.cpp | 207 ++++++++ 382.cpp | 67 ++- 383.cpp | 187 +++---- 384.cpp | 116 ++++ 385.cpp | 281 ++++++++++ 386.cpp | 99 ++-- 387.cpp | 179 +++++++ 388.cpp | 103 ++++ 389.cpp | 117 ++--- 390.cpp | 192 +++++++ 391.cpp | 64 +++ 392.cpp | 177 +++++++ 393.cpp | 178 +++++++ 394.cpp | 67 +++ 395.cpp | 392 ++++++++++++++ 397.cpp | 141 +++++ 398.cpp | 130 +++++ 399.cpp | 127 +++++ 400.cpp | 60 +++ 401.cpp | 154 +++--- 402.cpp | 60 +++ 403.cpp | 216 ++++++++ 405.cpp | 127 ++++- 406.cpp | 121 ++--- 407.cpp | 160 ++++++ 408.cpp | 85 ++- 409.cpp | 127 +++-- 410.cpp | 136 +++++ 411.cpp | 152 ++++++ 412.cpp | 102 ++-- 413.cpp | 122 +++++ 414.cpp | 57 +- 415.cpp | 65 +++ 416.cpp | 38 ++ 417.cpp | 145 +++-- 418.cpp | 81 +++ 419.cpp | 129 +++++ 420.cpp | 138 +++++ 421.cpp | 209 ++++++++ 422.cpp | 169 ++++++ 423.cpp | 111 ++-- 424.cpp | 63 +++ 425.cpp | 112 ++++ 426.cpp | 109 ++++ 427.cpp | 48 ++ 428.cpp | 65 +++ 429.cpp | 236 +++++---- 430.cpp | 108 ++++ 432.cpp | 101 ++++ 433.cpp | 156 ++++++ 434.cpp | 62 +++ 435.cpp | 64 +++ 436.cpp | 112 ++++ 437.cpp | 119 +++++ 438.cpp | 20 + 439.cpp | 59 +++ 440.cpp | 64 +++ 441.cpp | 53 ++ 442.cpp | 84 +++ 443.cpp | 142 ++--- 444.cpp | 90 ++++ 445.cpp | 74 +-- 446.cpp | 24 + 447.cpp | 164 ++++++ 448.cpp | 66 +++ 449.cpp | 42 ++ 450.cpp | 99 ++++ 451.cpp | 292 ++++++++++ 452.cpp | 161 ++++++ 453.cpp | 126 +++++ 454.cpp | 127 +++-- 455.cpp | 95 ++-- 456.cpp | 50 ++ 457.cpp | 67 ++- 458.cpp | 36 +- 459.cpp | 147 ++---- 460.cpp | 35 ++ 462.cpp | 220 ++++++++ 464.cpp | 288 ++++++++++ 465.cpp | 60 +++ 466.cpp | 130 +++++ 467.cpp | 59 +++ 468.cpp | 86 +++ 469.cpp | 200 ++++--- 471.cpp | 49 ++ 472.cpp | 190 +++++++ 473.cpp | 57 ++ 474.cpp | 14 + 475.cpp | 100 ++++ 476.cpp | 54 ++ 477.cpp | 110 ++++ 478.cpp | 165 ++++++ 481.cpp | 141 ++--- 482.cpp | 99 ++-- 483.cpp | 27 + 484.cpp | 65 +-- 485.cpp | 95 ++++ 486.cpp | 99 ++++ 487.cpp | 124 +++++ 488.cpp | 71 ++- 489.cpp | 80 +++ 490.cpp | 40 ++ 491.cpp | 13 + 492.cpp | 126 +++-- 493.cpp | 140 +++++ 494.cpp | 29 + 495.cpp | 70 ++- 496.cpp | 150 +++--- 497.cpp | 162 +++++- 498.cpp | 112 ++++ 499.cpp | 50 ++ 500.cpp | 143 +++++ 501.cpp | 158 ++++++ 502.cpp | 276 ++++++++++ 503.cpp | 76 +++ 505.cpp | 226 ++++++++ 506.cpp | 155 ++++++ 507.cpp | 137 ++--- 508.cpp | 183 +++++++ 509.cpp | 151 ++++++ 511.cpp | 153 ++++++ 512.cpp | 173 ++++++ 514.cpp | 145 ++--- 515.cpp | 71 +++ 516.cpp | 163 ++++-- 517.cpp | 104 ++++ 518.cpp | 197 +++++++ 519.cpp | 200 +++++++ 520.cpp | 52 ++ 521.cpp | 133 +++++ 523.cpp | 204 +++++++ 524.cpp | 85 +++ 526.cpp | 131 +++++ 527.cpp | 135 +++++ 529.cpp | 71 +++ 530.cpp | 88 ++-- 531.cpp | 163 +++--- 532.cpp | 129 +++-- 533.cpp | 132 +++++ 534.cpp | 124 ++--- 535.cpp | 195 ++++--- 536.cpp | 42 ++ 537.cpp | 118 +++-- 538.cpp | 88 ++++ 539.cpp | 56 ++ 540.cpp | 86 +++ 541.cpp | 144 +++-- 542.cpp | 55 ++ 543.cpp | 103 ++-- 544.cpp | 161 ++++++ 545.cpp | 20 + 546.cpp | 74 +++ 547.cpp | 109 ++++ 548.cpp | 87 +++ 549.cpp | 71 +++ 550.cpp | 28 + 551.cpp | 156 ++++++ 552.cpp | 82 +++ 554.cpp | 114 ++++ 555.cpp | 99 ++++ 556.cpp | 153 ++++++ 557.cpp | 24 + 558.cpp | 102 ++++ 559.cpp | 118 +++++ 560.cpp | 309 +++++++++++ 561.cpp | 60 +++ 562.cpp | 88 ++-- 563.cpp | 211 ++++++++ 565.cpp | 125 +++++ 566.cpp | 88 ++++ 567.cpp | 60 +++ 568.cpp | 24 + 570.cpp | 111 ++++ 571.cpp | 130 +++++ 572.cpp | 107 ++-- 573.cpp | 83 ++- 574.cpp | 143 +++++ 575.cpp | 71 +-- 576.cpp | 97 ++++ 577.cpp | 180 +++++++ 578.cpp | 64 +++ 579.cpp | 64 +-- 580.cpp | 38 ++ 581.cpp | 160 ++++++ 583.cpp | 152 ++++-- 584.cpp | 103 ++++ 585.cpp | 147 ++++++ 586.cpp | 80 +++ 587.cpp | 92 ++++ 588.cpp | 89 ++++ 589.cpp | 169 ++++++ 590.cpp | 112 ++++ 591.cpp | 33 ++ 592.cpp | 167 ++++++ 593.cpp | 139 +++++ 594.cpp | 68 +-- 595.cpp | 107 ++++ 596.cpp | 105 ++++ 598.cpp | 77 +++ 599.cpp | 114 ++++ 601.cpp | 225 ++++++++ 602.cpp | 122 +++++ 603.cpp | 119 +++++ 604.cpp | 206 ++++++++ 606.cpp | 101 ++++ 607.cpp | 145 +++++ 608.cpp | 141 +++++ 609.cpp | 102 ++++ 610.cpp | 115 ++++ 612.cpp | 119 +++-- 613.cpp | 73 +++ 614.cpp | 147 ++++++ 615.cpp | 201 +++++++ 616.cpp | 52 ++ 617.cpp | 98 ++++ 618.cpp | 130 +++++ 619.cpp | 114 ++++ 620.cpp | 120 +++++ 621.cpp | 103 ++-- 622.cpp | 109 ++++ 623.cpp | 108 ++++ 624.cpp | 61 +++ 625.cpp | 84 +++ 626.cpp | 123 +++++ 627.cpp | 219 ++++---- 628.cpp | 68 +++ 629.cpp | 145 +++++ 630.cpp | 109 ++-- 632.cpp | 99 ++++ 633.cpp | 155 ++++++ 634.cpp | 66 +++ 635.cpp | 85 +++ 636.cpp | 75 +-- 637.cpp | 107 ++-- 638.cpp | 99 ++++ 639.cpp | 118 +++++ 640.cpp | 74 +-- 641.cpp | 62 +++ 642.cpp | 91 ++-- 643.cpp | 162 ++++++ 644.cpp | 93 ++-- 645.cpp | 82 +++ 647.cpp | 126 +++++ 648.cpp | 132 +++++ 649.cpp | 116 ++++ 650.cpp | 88 ++++ 651.cpp | 40 ++ 652.cpp | 225 ++++++++ 653.cpp | 150 ++++++ 654.cpp | 66 +++ 656.cpp | 180 +++++++ 657.cpp | 96 ++++ 658.cpp | 107 ++++ 659.cpp | 152 ++++++ 660.cpp | 137 +++++ 661.cpp | 119 ++--- 662.cpp | 75 +++ 663.cpp | 97 ++++ 664.cpp | 154 ++++++ 665.cpp | 93 ++++ 667.cpp | 100 ++++ 668.cpp | 57 ++ 669.cpp | 77 +++ 670.cpp | 114 ++++ 671.cpp | 101 ++++ 672.cpp | 98 ++++ 673.cpp | 140 +++-- 674.cpp | 32 ++ 675.cpp | 104 ++++ 676.cpp | 142 +++++ 677.cpp | 88 ++++ 679.cpp | 37 ++ 680.cpp | 102 ++++ 681.cpp | 150 ++++++ 682.cpp | 371 +++++++++++++ 683.cpp | 154 ++++++ 684.cpp | 84 +++ 685.cpp | 96 ++++ 686.cpp | 100 ++-- 687.cpp | 140 +++++ 688.cpp | 86 +++ 689.cpp | 109 ++++ 690.cpp | 157 ++++++ 694.cpp | 63 ++- 696.cpp | 60 +++ 697.cpp | 39 ++ 698.cpp | 150 ++++++ 699.cpp | 61 +++ 700.cpp | 51 ++ 701.cpp | 33 ++ 702.cpp | 56 ++ 703.cpp | 70 +++ 704.cpp | 261 +++++++++ 705.cpp | 149 ++++++ 706.cpp | 146 +++++ 707.cpp | 166 ++++++ 708.cpp | 148 ++++++ 709.cpp | 116 ++++ 710.cpp | 127 +++++ 711.cpp | 165 ++++++ 712.cpp | 79 +++ 713.cpp | 105 ++++ 714.cpp | 80 +++ 715.cpp | 110 ++++ 716.cpp | 88 ++++ 718.cpp | 143 +++++ 719.cpp | 54 ++ 721.cpp | 136 +++++ 722.cpp | 106 ++++ 725.cpp | 45 ++ 726.cpp | 65 +++ 727.cpp | 76 +++ 729.cpp | 106 ++-- 732.cpp | 106 ++++ 734.cpp | 107 ++++ 735.cpp | 98 ++++ 736.cpp | 76 +++ 737.cpp | 51 ++ 738.cpp | 276 ++++++++++ 739.cpp | 43 ++ 740.cpp | 58 ++ 741.cpp | 87 +++ 742.cpp | 221 ++++++++ 743.cpp | 63 +++ 745.cpp | 204 +++++++ 746.cpp | 155 ++++++ 748.cpp | 158 ++++++ 750.cpp | 74 +++ 751.cpp | 116 ++++ 752.cpp | 176 +++++++ 753.cpp | 203 +++++++ 754.cpp | 210 ++++++++ 755.cpp | 194 ++++--- 756.cpp | 35 ++ 757.cpp | 119 +++++ 758.cpp | 180 +++++++ 759.cpp | 107 ++++ 760.cpp | 131 +++++ 762.cpp | 243 ++++++--- 763.cpp | 85 +++ 766.cpp | 1322 ++++++++++++++++++++++++++++++++++++++++++++++ 769.cpp | 34 ++ 775.cpp | 151 ++++++ 776.cpp | 262 +++++---- 782.cpp | 148 ++++++ 783.cpp | 30 ++ 784.cpp | 94 ++++ 785.cpp | 177 +++++++ 787.cpp | 184 +++++++ 789.cpp | 112 ++-- 790.cpp | 131 +++++ 793.cpp | 105 ++++ 795.cpp | 42 ++ 796.cpp | 80 +++ 798.cpp | 96 ++++ 799.cpp | 56 ++ 801.cpp | 92 ++++ 802.cpp | 180 +++++++ 803.cpp | 115 ++++ 804.cpp | 107 ++++ 805.cpp | 405 ++++++++++++++ 806.cpp | 170 ++++++ 807.cpp | 182 +++++++ 808.cpp | 76 +++ 810.cpp | 202 +++++++ 811.cpp | 196 +++++++ 812.cpp | 128 +++++ 813.cpp | 170 ++++++ 814.cpp | 125 +++++ 815.cpp | 107 ++++ 816.cpp | 257 +++++++++ 817.cpp | 133 +++++ 818.cpp | 87 +++ 819.cpp | 188 +++++++ 820.cpp | 121 +++++ 821.cpp | 179 ++++--- 822.cpp | 126 +++++ 823.cpp | 133 +++++ 824.cpp | 56 ++ 825.cpp | 170 ++++++ 828.cpp | 87 +++ 830.cpp | 104 ++++ 832.cpp | 60 +++ 833.cpp | 74 +++ 834.cpp | 40 ++ 835.cpp | 288 ++++++++++ 836.cpp | 177 ++++--- 837.cpp | 71 +++ 838.cpp | 91 ++++ 839.cpp | 99 ++++ 840.cpp | 153 ++++++ 841.cpp | 174 ++++++ 843.cpp | 115 ++++ 844.cpp | 184 +++++++ 845.cpp | 67 +++ 846.cpp | 81 +++ 847.cpp | 72 ++- 848.cpp | 114 ++++ 850.cpp | 131 +++++ 851.cpp | 209 ++++++++ 852.cpp | 199 +++++++ 855.cpp | 67 ++- 856.cpp | 45 ++ 857.cpp | 82 +++ 858.cpp | 56 ++ 859.cpp | 101 ++++ 860.cpp | 41 ++ 861.cpp | 40 ++ 862.cpp | 213 ++++++++ 864.cpp | 301 +++++++++++ 865.cpp | 49 ++ 866.cpp | 63 +++ 868.cpp | 75 +++ 869.cpp | 185 +++++-- 870.cpp | 104 ++++ 871.cpp | 93 ++++ 872.cpp | 100 ++++ 873.cpp | 29 + 874.cpp | 111 ++++ 877.cpp | 63 +++ 878.cpp | 75 +++ 879.cpp | 109 ++++ 880.cpp | 110 +--- 881.cpp | 200 +++++++ 882.cpp | 62 +++ 883.cpp | 96 ++++ 884.cpp | 75 +++ 886.cpp | 294 +++++++++++ 890.cpp | 79 +++ 891.cpp | 75 +++ 892.cpp | 55 ++ 893.cpp | 84 +++ 895.cpp | 50 ++ 897.cpp | 97 ++++ 898.cpp | 134 +++++ 899.cpp | 131 +++++ 900.cpp | 42 +- 902.cpp | 98 ++-- 903.cpp | 90 ++++ 904.cpp | 83 +++ 905.cpp | 94 ++++ 906.cpp | 37 ++ 907.cpp | 49 ++ 908.cpp | 108 ++++ 909.cpp | 63 +++ 910.cpp | 62 +++ 911.cpp | 46 ++ 912.cpp | 150 ++++++ 913.cpp | 42 +- 914.cpp | 95 ++++ 915.cpp | 65 +++ 917.cpp | 188 +++++++ 918.cpp | 42 ++ 920.cpp | 105 ++++ 921.cpp | 72 +++ 922.cpp | 114 ++++ 924.cpp | 152 ++++++ 925.cpp | 179 +++++++ 926.cpp | 125 +++++ 927.cpp | 33 ++ 928.cpp | 80 +++ 929.cpp | 101 ++++ 930.cpp | 31 ++ 932.cpp | 105 ++++ 933.cpp | 87 +++ 938.cpp | 76 +++ 939.cpp | 104 ++++ 941.cpp | 86 ++- 942.cpp | 47 ++ 944.cpp | 106 ++++ 945.cpp | 99 ++++ 946.cpp | 86 +++ 947.cpp | 238 +++++++++ 948.cpp | 67 +++ 949.cpp | 95 ++++ 953.cpp | 64 +++ 954.cpp | 216 ++++++++ 956.cpp | 85 +++ 957.cpp | 44 ++ 960.cpp | 42 ++ 962.cpp | 84 +++ 963.cpp | 85 +++ 964.cpp | 305 +++++++++++ 967.cpp | 149 +++--- 969.cpp | 222 ++++++++ 970.cpp | 55 ++ 972.cpp | 70 +++ 973.cpp | 119 +++++ 974.cpp | 60 ++- 976.cpp | 88 ++++ 978.cpp | 84 +++ 979.cpp | 162 ++++++ 982.cpp | 179 +++++++ 983.cpp | 59 +++ 985.cpp | 84 +++ 986.cpp | 49 ++ 988.cpp | 57 ++ 989.cpp | 100 ++++ 990.cpp | 81 +++ 991.cpp | 86 +-- 993.cpp | 53 ++ 995.cpp | 55 ++ 996.cpp | 95 ++++ 997.cpp | 57 ++ 999.cpp | 35 ++ 4027 files changed, 422305 insertions(+), 21268 deletions(-) create mode 100644 1000.cpp create mode 100644 10000.cpp create mode 100644 10001.cpp create mode 100644 10002.cpp create mode 100644 10004.cpp create mode 100644 10005.cpp create mode 100644 10007.cpp create mode 100644 10009.cpp create mode 100644 1001.cpp create mode 100644 10011.cpp create mode 100644 10012.cpp create mode 100644 10016.cpp create mode 100644 10017.cpp create mode 100644 10020.cpp create mode 100644 10021.cpp create mode 100644 10022.cpp create mode 100644 10024.cpp create mode 100644 10026.cpp create mode 100644 10027.cpp create mode 100644 10028.cpp create mode 100644 10029.cpp create mode 100644 10030.cpp create mode 100644 10031.cpp create mode 100644 10032.cpp create mode 100644 10033.cpp create mode 100644 10034.cpp create mode 100644 10036.cpp create mode 100644 10037.cpp create mode 100644 10039.cpp create mode 100644 10040.cpp create mode 100644 10042.cpp create mode 100644 10043.cpp create mode 100644 10044.cpp create mode 100644 10045.cpp create mode 100644 10049.cpp create mode 100644 10051.cpp create mode 100644 10053.cpp create mode 100644 10054.cpp create mode 100644 10056.cpp create mode 100644 10057.cpp create mode 100644 10058.cpp create mode 100644 1006.cpp create mode 100644 10060.cpp create mode 100644 10061.cpp create mode 100644 10063.cpp create mode 100644 10065.cpp create mode 100644 10067.cpp create mode 100644 10068.cpp create mode 100644 10069.cpp create mode 100644 10071.cpp create mode 100644 10073.cpp create mode 100644 10075.cpp create mode 100644 10077.cpp create mode 100644 10078.cpp create mode 100644 1008.cpp create mode 100644 10080.cpp create mode 100644 10081.cpp create mode 100644 10083.cpp create mode 100644 10084.cpp create mode 100644 10085.cpp create mode 100644 10086.cpp create mode 100644 10087.cpp create mode 100644 10088.cpp create mode 100644 10089.cpp create mode 100644 1009.cpp create mode 100644 10090.cpp create mode 100644 10091.cpp create mode 100644 10092.cpp create mode 100644 10094.cpp create mode 100644 10095.cpp create mode 100644 10097.cpp create mode 100644 10103.cpp create mode 100644 10105.cpp create mode 100644 10106.cpp create mode 100644 10109.cpp create mode 100644 1011.cpp create mode 100644 10111.cpp create mode 100644 10112.cpp create mode 100644 10113.cpp create mode 100644 10114.cpp create mode 100644 10116.cpp create mode 100644 10117.cpp create mode 100644 10118.cpp create mode 100644 10119.cpp create mode 100644 10120.cpp create mode 100644 10122.cpp create mode 100644 10123.cpp create mode 100644 10124.cpp create mode 100644 10125.cpp create mode 100644 10126.cpp create mode 100644 10128.cpp create mode 100644 10129.cpp create mode 100644 1013.cpp create mode 100644 10131.cpp create mode 100644 10132.cpp create mode 100644 10134.cpp create mode 100644 10135.cpp create mode 100644 10136.cpp create mode 100644 10137.cpp create mode 100644 10138.cpp create mode 100644 10139.cpp create mode 100644 10140.cpp create mode 100644 10141.cpp create mode 100644 10142.cpp create mode 100644 10144.cpp create mode 100644 10145.cpp create mode 100644 10146.cpp create mode 100644 10147.cpp create mode 100644 10148.cpp create mode 100644 10149.cpp create mode 100644 10150.cpp create mode 100644 10152.cpp create mode 100644 10154.cpp create mode 100644 10155.cpp create mode 100644 10156.cpp create mode 100644 10157.cpp create mode 100644 10158.cpp create mode 100644 10159.cpp create mode 100644 1016.cpp create mode 100644 10160.cpp create mode 100644 10163.cpp create mode 100644 10164.cpp create mode 100644 10165.cpp create mode 100644 10166.cpp create mode 100644 10167.cpp create mode 100644 10169.cpp create mode 100644 10170.cpp create mode 100644 10172.cpp create mode 100644 10173.cpp create mode 100644 10174.cpp create mode 100644 10175.cpp create mode 100644 10176.cpp create mode 100644 10177.cpp create mode 100644 10178.cpp create mode 100644 10179.cpp create mode 100644 1018.cpp create mode 100644 10180.cpp create mode 100644 10181.cpp create mode 100644 10182.cpp create mode 100644 10183.cpp create mode 100644 10184.cpp create mode 100644 10185.cpp create mode 100644 10186.cpp create mode 100644 10187.cpp create mode 100644 10188.cpp create mode 100644 10190.cpp create mode 100644 10191.cpp create mode 100644 10194.cpp create mode 100644 10196.cpp create mode 100644 10197.cpp create mode 100644 10198.cpp create mode 100644 10199.cpp create mode 100644 10201.cpp create mode 100644 10202.cpp create mode 100644 10203.cpp create mode 100644 10205.cpp create mode 100644 10206.cpp create mode 100644 10207.cpp create mode 100644 10208.cpp create mode 100644 10209.cpp create mode 100644 10210.cpp create mode 100644 10211.cpp create mode 100644 10212.cpp create mode 100644 10213.cpp create mode 100644 10214.cpp create mode 100644 10215.cpp create mode 100644 10216.cpp create mode 100644 10217.cpp create mode 100644 10218.cpp create mode 100644 10219.cpp create mode 100644 10220.cpp create mode 100644 10221.cpp create mode 100644 10224.cpp create mode 100644 10225.cpp create mode 100644 10227.cpp create mode 100644 10228.cpp create mode 100644 10230.cpp create mode 100644 10231.cpp create mode 100644 10232.cpp create mode 100644 10233.cpp create mode 100644 10234.cpp create mode 100644 10236.cpp create mode 100644 10237.cpp create mode 100644 10239.cpp create mode 100644 10241.cpp create mode 100644 10242.cpp create mode 100644 10243.cpp create mode 100644 10247.cpp create mode 100644 10248.cpp create mode 100644 10249.cpp create mode 100644 1025.cpp create mode 100644 10250.cpp create mode 100644 10251.cpp create mode 100644 10253.cpp create mode 100644 10254.cpp create mode 100644 10256.cpp create mode 100644 10257.cpp create mode 100644 10259.cpp create mode 100644 1026.cpp create mode 100644 10261.cpp create mode 100644 10262.cpp create mode 100644 10263.cpp create mode 100644 10264.cpp create mode 100644 10266.cpp create mode 100644 10267.cpp create mode 100644 10269.cpp create mode 100644 1027.cpp create mode 100644 10270.cpp create mode 100644 10271.cpp create mode 100644 10272.cpp create mode 100644 10273.cpp create mode 100644 10274.cpp create mode 100644 10275.cpp create mode 100644 10276.cpp create mode 100644 10277.cpp create mode 100644 10278.cpp create mode 100644 10279.cpp create mode 100644 10280.cpp create mode 100644 10283.cpp create mode 100644 10284.cpp create mode 100644 10285.cpp create mode 100644 10287.cpp create mode 100644 10288.cpp create mode 100644 1029.cpp create mode 100644 10290.cpp create mode 100644 10291.cpp create mode 100644 10293.cpp create mode 100644 10294.cpp create mode 100644 10296.cpp create mode 100644 10299.cpp create mode 100644 103.cpp create mode 100644 1030.cpp create mode 100644 10301.cpp create mode 100644 10302.cpp create mode 100644 10303.cpp create mode 100644 10304.cpp create mode 100644 10305.cpp create mode 100644 10306.cpp create mode 100644 10307.cpp create mode 100644 10308.cpp create mode 100644 10309.cpp create mode 100644 1031.cpp create mode 100644 10312.cpp create mode 100644 10313.cpp create mode 100644 10315.cpp create mode 100644 10316.cpp create mode 100644 10318.cpp create mode 100644 10319.cpp create mode 100644 1032.cpp create mode 100644 10320.cpp create mode 100644 10321.cpp create mode 100644 10325.cpp create mode 100644 10326.cpp create mode 100644 10327.cpp create mode 100644 10328.cpp create mode 100644 10329.cpp create mode 100644 10330.cpp create mode 100644 10331.cpp create mode 100644 10333.cpp create mode 100644 10335.cpp create mode 100644 10337.cpp create mode 100644 10338.cpp create mode 100644 10339.cpp create mode 100644 10342.cpp create mode 100644 10348.cpp create mode 100644 10349.cpp create mode 100644 10350.cpp create mode 100644 10351.cpp create mode 100644 10352.cpp create mode 100644 10354.cpp create mode 100644 10355.cpp create mode 100644 10356.cpp create mode 100644 10357.cpp create mode 100644 10358.cpp create mode 100644 10359.cpp create mode 100644 10362.cpp create mode 100644 10364.cpp create mode 100644 10366.cpp create mode 100644 10367.cpp create mode 100644 10368.cpp create mode 100644 10369.cpp create mode 100644 10371.cpp create mode 100644 10372.cpp create mode 100644 10373.cpp create mode 100644 10375.cpp create mode 100644 10377.cpp create mode 100644 10378.cpp create mode 100644 10379.cpp create mode 100644 1038.cpp create mode 100644 10380.cpp create mode 100644 10381.cpp create mode 100644 10382.cpp create mode 100644 10384.cpp create mode 100644 10385.cpp create mode 100644 10386.cpp create mode 100644 10387.cpp create mode 100644 10388.cpp create mode 100644 10389.cpp create mode 100644 1039.cpp create mode 100644 10390.cpp create mode 100644 10392.cpp create mode 100644 10394.cpp create mode 100644 10396.cpp create mode 100644 10397.cpp create mode 100644 10398.cpp create mode 100644 10399.cpp create mode 100644 1040.cpp create mode 100644 10400.cpp create mode 100644 10401.cpp create mode 100644 10403.cpp create mode 100644 10404.cpp create mode 100644 10406.cpp create mode 100644 10408.cpp create mode 100644 10409.cpp create mode 100644 1041.cpp create mode 100644 10410.cpp create mode 100644 10411.cpp create mode 100644 10412.cpp create mode 100644 10413.cpp create mode 100644 10415.cpp create mode 100644 10416.cpp create mode 100644 10417.cpp create mode 100644 10418.cpp create mode 100644 10419.cpp create mode 100644 1042.cpp create mode 100644 10421.cpp create mode 100644 10422.cpp create mode 100644 10423.cpp create mode 100644 10425.cpp create mode 100644 10426.cpp create mode 100644 10427.cpp create mode 100644 10428.cpp create mode 100644 10429.cpp create mode 100644 1043.cpp create mode 100644 10430.cpp create mode 100644 10431.cpp create mode 100644 10432.cpp create mode 100644 10433.cpp create mode 100644 10434.cpp create mode 100644 10436.cpp create mode 100644 10437.cpp create mode 100644 10438.cpp create mode 100644 10439.cpp create mode 100644 1044.cpp create mode 100644 10441.cpp create mode 100644 10442.cpp create mode 100644 10443.cpp create mode 100644 10444.cpp create mode 100644 10445.cpp create mode 100644 10446.cpp create mode 100644 10447.cpp create mode 100644 10448.cpp create mode 100644 10449.cpp create mode 100644 1045.cpp create mode 100644 10450.cpp create mode 100644 10451.cpp create mode 100644 10452.cpp create mode 100644 10453.cpp create mode 100644 10454.cpp create mode 100644 10456.cpp create mode 100644 10457.cpp create mode 100644 10458.cpp create mode 100644 10459.cpp create mode 100644 1046.cpp create mode 100644 10460.cpp create mode 100644 10461.cpp create mode 100644 10462.cpp create mode 100644 10463.cpp create mode 100644 10464.cpp create mode 100644 10465.cpp create mode 100644 10466.cpp create mode 100644 10467.cpp create mode 100644 10468.cpp create mode 100644 1047.cpp create mode 100644 10471.cpp create mode 100644 10473.cpp create mode 100644 10475.cpp create mode 100644 10476.cpp create mode 100644 10477.cpp create mode 100644 10479.cpp create mode 100644 1048.cpp create mode 100644 10480.cpp create mode 100644 10482.cpp create mode 100644 10483.cpp create mode 100644 10484.cpp create mode 100644 10486.cpp create mode 100644 10488.cpp create mode 100644 1049.cpp create mode 100644 10494.cpp create mode 100644 10495.cpp create mode 100644 10496.cpp create mode 100644 10498.cpp create mode 100644 10499.cpp create mode 100644 10500.cpp create mode 100644 10501.cpp create mode 100644 10502.cpp create mode 100644 10503.cpp create mode 100644 10504.cpp create mode 100644 10505.cpp create mode 100644 10506.cpp create mode 100644 10507.cpp create mode 100644 10508.cpp create mode 100644 10509.cpp create mode 100644 1051.cpp create mode 100644 10510.cpp create mode 100644 10511.cpp create mode 100644 10512.cpp create mode 100644 10513.cpp create mode 100644 10514.cpp create mode 100644 10516.cpp create mode 100644 10517.cpp create mode 100644 10518.cpp create mode 100644 10519.cpp create mode 100644 1052.cpp create mode 100644 10520.cpp create mode 100644 10521.cpp create mode 100644 10522.cpp create mode 100644 10523.cpp create mode 100644 10524.cpp create mode 100644 10525.cpp create mode 100644 10526.cpp create mode 100644 10527.cpp create mode 100644 10528.cpp create mode 100644 10529.cpp create mode 100644 10530.cpp create mode 100644 10531.cpp create mode 100644 10532.cpp create mode 100644 10534.cpp create mode 100644 10535.cpp create mode 100644 10536.cpp create mode 100644 10537.cpp create mode 100644 10539.cpp create mode 100644 1054.cpp create mode 100644 10540.cpp create mode 100644 10541.cpp create mode 100644 10542.cpp create mode 100644 10543.cpp create mode 100644 10544.cpp create mode 100644 10545.cpp create mode 100644 10546.cpp create mode 100644 10547.cpp create mode 100644 10548.cpp create mode 100644 10549.cpp create mode 100644 10551.cpp create mode 100644 10552.cpp create mode 100644 10553.cpp create mode 100644 10554.cpp create mode 100644 10555.cpp create mode 100644 10556.cpp create mode 100644 10557.cpp create mode 100644 10558.cpp create mode 100644 10559.cpp create mode 100644 1056.cpp create mode 100644 10560.cpp create mode 100644 10561.cpp create mode 100644 10562.cpp create mode 100644 10563.cpp create mode 100644 10564.cpp create mode 100644 10565.cpp create mode 100644 10566.cpp create mode 100644 10567.cpp create mode 100644 10568.cpp create mode 100644 10569.cpp create mode 100644 1057.cpp create mode 100644 10570.cpp create mode 100644 10571.cpp create mode 100644 10572.cpp create mode 100644 10573.cpp create mode 100644 10574.cpp create mode 100644 10575.cpp create mode 100644 10576.cpp create mode 100644 10577.cpp create mode 100644 10578.cpp create mode 100644 10579.cpp create mode 100644 1058.cpp create mode 100644 10580.cpp create mode 100644 10581.cpp create mode 100644 10582.cpp create mode 100644 10583.cpp create mode 100644 10584.cpp create mode 100644 10585.cpp create mode 100644 10586.cpp create mode 100644 10587.cpp create mode 100644 10588.cpp create mode 100644 10589.cpp create mode 100644 1059.cpp create mode 100644 10590.cpp create mode 100644 10592.cpp create mode 100644 10593.cpp create mode 100644 10594.cpp create mode 100644 10595.cpp create mode 100644 10596.cpp create mode 100644 10597.cpp create mode 100644 10598.cpp create mode 100644 10599.cpp create mode 100644 1060.cpp create mode 100644 10600.cpp create mode 100644 10601.cpp create mode 100644 10602.cpp create mode 100644 10603.cpp create mode 100644 10604.cpp create mode 100644 10605.cpp create mode 100644 10606.cpp create mode 100644 10608.cpp create mode 100644 10609.cpp create mode 100644 1061.cpp create mode 100644 10610.cpp create mode 100644 10613.cpp create mode 100644 10614.cpp create mode 100644 10615.cpp create mode 100644 10616.cpp create mode 100644 10617.cpp create mode 100644 10618.cpp create mode 100644 10619.cpp create mode 100644 1062.cpp create mode 100644 10620.cpp create mode 100644 10621.cpp create mode 100644 10622.cpp create mode 100644 10623.cpp create mode 100644 10624.cpp create mode 100644 10625.cpp create mode 100644 10626.cpp create mode 100644 10627.cpp create mode 100644 10629.cpp create mode 100644 1063.cpp create mode 100644 10631.cpp create mode 100644 10632.cpp create mode 100644 10634.cpp create mode 100644 10637.cpp create mode 100644 10638.cpp create mode 100644 1064.cpp create mode 100644 10640.cpp create mode 100644 10641.cpp create mode 100644 10642.cpp create mode 100644 10643.cpp create mode 100644 10644.cpp create mode 100644 10645.cpp create mode 100644 10646.cpp create mode 100644 10647.cpp create mode 100644 10648.cpp create mode 100644 10649.cpp create mode 100644 10650.cpp create mode 100644 10651.cpp create mode 100644 10652.cpp create mode 100644 10653.cpp create mode 100644 10654.cpp create mode 100644 10655.cpp create mode 100644 10658.cpp create mode 100644 10659.cpp create mode 100644 10660.cpp create mode 100644 10662.cpp create mode 100644 10663.cpp create mode 100644 10664.cpp create mode 100644 10665.cpp create mode 100644 10666.cpp create mode 100644 10668.cpp create mode 100644 10669.cpp create mode 100644 1067.cpp create mode 100644 10670.cpp create mode 100644 10671.cpp create mode 100644 10672.cpp create mode 100644 10673.cpp create mode 100644 10674.cpp create mode 100644 10675.cpp create mode 100644 10676.cpp create mode 100644 10677.cpp create mode 100644 10678.cpp create mode 100644 1068.cpp create mode 100644 10680.cpp create mode 100644 10681.cpp create mode 100644 10682.cpp create mode 100644 10685.cpp create mode 100644 10686.cpp create mode 100644 10687.cpp create mode 100644 10688.cpp create mode 100644 10689.cpp create mode 100644 1069.cpp create mode 100644 10690.cpp create mode 100644 10691.cpp create mode 100644 10692.cpp create mode 100644 10693.cpp create mode 100644 10694.cpp create mode 100644 10697.cpp create mode 100644 10698.cpp create mode 100644 107.cpp create mode 100644 10700.cpp create mode 100644 10701.cpp create mode 100644 10702.cpp create mode 100644 10704.cpp create mode 100644 10705.cpp create mode 100644 10706.cpp create mode 100644 10707.cpp create mode 100644 10708.cpp create mode 100644 10709.cpp create mode 100644 10710.cpp create mode 100644 10711.cpp create mode 100644 10712.cpp create mode 100644 10713.cpp create mode 100644 10715.cpp create mode 100644 10716.cpp create mode 100644 10717.cpp create mode 100644 10719.cpp create mode 100644 10720.cpp create mode 100644 10721.cpp create mode 100644 10722.cpp create mode 100644 10723.cpp create mode 100644 10724.cpp create mode 100644 10725.cpp create mode 100644 10726.cpp create mode 100644 10727.cpp create mode 100644 10728.cpp create mode 100644 10729.cpp create mode 100644 1073.cpp create mode 100644 10730.cpp create mode 100644 10731.cpp create mode 100644 10732.cpp create mode 100644 10733.cpp create mode 100644 10734.cpp create mode 100644 10735.cpp create mode 100644 10736.cpp create mode 100644 10737.cpp create mode 100644 10738.cpp create mode 100644 10740.cpp create mode 100644 10741.cpp create mode 100644 10742.cpp create mode 100644 10743.cpp create mode 100644 10744.cpp create mode 100644 10745.cpp create mode 100644 10746.cpp create mode 100644 10747.cpp create mode 100644 10748.cpp create mode 100644 1075.cpp create mode 100644 10750.cpp create mode 100644 10751.cpp create mode 100644 10754.cpp create mode 100644 10755.cpp create mode 100644 10757.cpp create mode 100644 10758.cpp create mode 100644 10759.cpp create mode 100644 1076.cpp create mode 100644 10760.cpp create mode 100644 10761.cpp create mode 100644 10764.cpp create mode 100644 10765.cpp create mode 100644 10766.cpp create mode 100644 10767.cpp create mode 100644 10768.cpp create mode 100644 10769.cpp create mode 100644 1077.cpp create mode 100644 10770.cpp create mode 100644 10771.cpp create mode 100644 10772.cpp create mode 100644 10774.cpp create mode 100644 10775.cpp create mode 100644 10776.cpp create mode 100644 10777.cpp create mode 100644 10778.cpp create mode 100644 10779.cpp create mode 100644 1078.cpp create mode 100644 10780.cpp create mode 100644 10782.cpp create mode 100644 10783.cpp create mode 100644 10784.cpp create mode 100644 10787.cpp create mode 100644 10788.cpp create mode 100644 1079.cpp create mode 100644 10790.cpp create mode 100644 10791.cpp create mode 100644 10792.cpp create mode 100644 10793.cpp create mode 100644 10794.cpp create mode 100644 10795.cpp create mode 100644 10797.cpp create mode 100644 10798.cpp create mode 100644 10799.cpp create mode 100644 1080.cpp create mode 100644 10801.cpp create mode 100644 10802.cpp create mode 100644 10804.cpp create mode 100644 10805.cpp create mode 100644 10806.cpp create mode 100644 10807.cpp create mode 100644 10808.cpp create mode 100644 10809.cpp create mode 100644 1081.cpp create mode 100644 10811.cpp create mode 100644 10813.cpp create mode 100644 10814.cpp create mode 100644 10815.cpp create mode 100644 10816.cpp create mode 100644 10817.cpp create mode 100644 10818.cpp create mode 100644 10819.cpp create mode 100644 1082.cpp create mode 100644 10820.cpp create mode 100644 10821.cpp create mode 100644 10823.cpp create mode 100644 10824.cpp create mode 100644 10825.cpp create mode 100644 10826.cpp create mode 100644 10827.cpp create mode 100644 10828.cpp create mode 100644 10829.cpp create mode 100644 1083.cpp create mode 100644 10830.cpp create mode 100644 10831.cpp create mode 100644 10832.cpp create mode 100644 10833.cpp create mode 100644 10834.cpp create mode 100644 10835.cpp create mode 100644 10836.cpp create mode 100644 10837.cpp create mode 100644 10838.cpp create mode 100644 10839.cpp create mode 100644 1084.cpp create mode 100644 10841.cpp create mode 100644 10842.cpp create mode 100644 10844.cpp create mode 100644 10846.cpp create mode 100644 10847.cpp create mode 100644 10848.cpp create mode 100644 10849.cpp create mode 100644 1085.cpp create mode 100644 10850.cpp create mode 100644 10851.cpp create mode 100644 10853.cpp create mode 100644 10854.cpp create mode 100644 10855.cpp create mode 100644 10856.cpp create mode 100644 10858.cpp create mode 100644 10859.cpp create mode 100644 1086.cpp create mode 100644 10860.cpp create mode 100644 10861.cpp create mode 100644 10862.cpp create mode 100644 10863.cpp create mode 100644 10864.cpp create mode 100644 10865.cpp create mode 100644 10866.cpp create mode 100644 10867.cpp create mode 100644 10868.cpp create mode 100644 10869.cpp create mode 100644 10870.cpp create mode 100644 10871.cpp create mode 100644 10872.cpp create mode 100644 10873.cpp create mode 100644 10874.cpp create mode 100644 10875.cpp create mode 100644 10876.cpp create mode 100644 10877.cpp create mode 100644 1088.cpp create mode 100644 10880.cpp create mode 100644 10881.cpp create mode 100644 10882.cpp create mode 100644 10884.cpp create mode 100644 10886.cpp create mode 100644 10888.cpp create mode 100644 10889.cpp create mode 100644 1089.cpp create mode 100644 10890.cpp create mode 100644 10891.cpp create mode 100644 10892.cpp create mode 100644 10893.cpp create mode 100644 10894.cpp create mode 100644 10895.cpp create mode 100644 10896.cpp create mode 100644 10897.cpp create mode 100644 10898.cpp create mode 100644 10899.cpp create mode 100644 109.cpp create mode 100644 10900.cpp create mode 100644 10902.cpp create mode 100644 10904.cpp create mode 100644 10906.cpp create mode 100644 10907.cpp create mode 100644 10908.cpp create mode 100644 10909.cpp create mode 100644 1091.cpp create mode 100644 10910.cpp create mode 100644 10912.cpp create mode 100644 10913.cpp create mode 100644 10914.cpp create mode 100644 10915.cpp create mode 100644 10916.cpp create mode 100644 10917.cpp create mode 100644 10919.cpp create mode 100644 1092.cpp create mode 100644 10920.cpp create mode 100644 10923.cpp create mode 100644 10925.cpp create mode 100644 10926.cpp create mode 100644 10927.cpp create mode 100644 1093.cpp create mode 100644 10932.cpp create mode 100644 10933.cpp create mode 100644 10934.cpp create mode 100644 10936.cpp create mode 100644 10937.cpp create mode 100644 10938.cpp create mode 100644 10939.cpp create mode 100644 1094.cpp create mode 100644 10941.cpp create mode 100644 10942.cpp create mode 100644 10944.cpp create mode 100644 10947.cpp create mode 100644 10949.cpp create mode 100644 1095.cpp create mode 100644 10950.cpp create mode 100644 10951.cpp create mode 100644 10952.cpp create mode 100644 10953.cpp create mode 100644 10956.cpp create mode 100644 10957.cpp create mode 100644 10958.cpp create mode 100644 10959.cpp create mode 100644 10960.cpp create mode 100644 10961.cpp create mode 100644 10962.cpp create mode 100644 10964.cpp create mode 100644 10965.cpp create mode 100644 10967.cpp create mode 100644 10968.cpp create mode 100644 10969.cpp create mode 100644 10972.cpp create mode 100644 10973.cpp create mode 100644 10975.cpp create mode 100644 10977.cpp create mode 100644 10978.cpp create mode 100644 10979.cpp create mode 100644 10980.cpp create mode 100644 10981.cpp create mode 100644 10982.cpp create mode 100644 10983.cpp create mode 100644 10984.cpp create mode 100644 10985.cpp create mode 100644 10986.cpp create mode 100644 10987.cpp create mode 100644 10989.cpp create mode 100644 1099.cpp create mode 100644 10990.cpp create mode 100644 10991.cpp create mode 100644 10992.cpp create mode 100644 10993.cpp create mode 100644 10995.cpp create mode 100644 10996.cpp create mode 100644 10997.cpp create mode 100644 10998.cpp create mode 100644 10999.cpp create mode 100644 110.cpp create mode 100644 1100.cpp create mode 100644 11002.cpp create mode 100644 11003.cpp create mode 100644 11004.cpp create mode 100644 11005.cpp create mode 100644 11006.cpp create mode 100644 11007.cpp create mode 100644 11008.cpp create mode 100644 11009.cpp create mode 100644 1101.cpp create mode 100644 11010.cpp create mode 100644 11011.cpp create mode 100644 11012.cpp create mode 100644 11013.cpp create mode 100644 11014.cpp create mode 100644 11016.cpp create mode 100644 11017.cpp create mode 100644 11018.cpp create mode 100644 11019.cpp create mode 100644 11020.cpp create mode 100644 11021.cpp create mode 100644 11022.cpp create mode 100644 11023.cpp create mode 100644 11024.cpp create mode 100644 11025.cpp create mode 100644 11026.cpp create mode 100644 11027.cpp create mode 100644 11029.cpp create mode 100644 1103.cpp create mode 100644 11030.cpp create mode 100644 11031.cpp create mode 100644 11032.cpp create mode 100644 11033.cpp create mode 100644 11034.cpp create mode 100644 11035.cpp create mode 100644 11036.cpp create mode 100644 11037.cpp create mode 100644 11039.cpp create mode 100644 11040.cpp create mode 100644 11041.cpp create mode 100644 11042.cpp create mode 100644 11043.cpp create mode 100644 11045.cpp create mode 100644 11046.cpp create mode 100644 11047.cpp create mode 100644 11049.cpp create mode 100644 1105.cpp create mode 100644 11050.cpp create mode 100644 11051.cpp create mode 100644 11052.cpp create mode 100644 11053.cpp create mode 100644 11055.cpp create mode 100644 11056.cpp create mode 100644 11058.cpp create mode 100644 1106.cpp create mode 100644 11060.cpp create mode 100644 11061.cpp create mode 100644 11062.cpp create mode 100644 11064.cpp create mode 100644 11065.cpp create mode 100644 11066.cpp create mode 100644 11067.cpp create mode 100644 11068.cpp create mode 100644 11069.cpp create mode 100644 11070.cpp create mode 100644 11071.cpp create mode 100644 11072.cpp create mode 100644 11073.cpp create mode 100644 11074.cpp create mode 100644 11075.cpp create mode 100644 11076.cpp create mode 100644 11077.cpp create mode 100644 11078.cpp create mode 100644 11079.cpp create mode 100644 1108.cpp create mode 100644 11080.cpp create mode 100644 11081.cpp create mode 100644 11082.cpp create mode 100644 11083.cpp create mode 100644 11084.cpp create mode 100644 11085.cpp create mode 100644 11086.cpp create mode 100644 11087.cpp create mode 100644 11088.cpp create mode 100644 11089.cpp create mode 100644 1109.cpp create mode 100644 11090.cpp create mode 100644 11091.cpp create mode 100644 11092.cpp create mode 100644 11093.cpp create mode 100644 11095.cpp create mode 100644 11096.cpp create mode 100644 11097.cpp create mode 100644 11098.cpp create mode 100644 11099.cpp create mode 100644 1110.cpp create mode 100644 11100.cpp create mode 100644 11101.cpp create mode 100644 11102.cpp create mode 100644 11103.cpp create mode 100644 11104.cpp create mode 100644 11105.cpp create mode 100644 11106.cpp create mode 100644 11107.cpp create mode 100644 11108.cpp create mode 100644 11109.cpp create mode 100644 1111.cpp create mode 100644 11111.cpp create mode 100644 11112.cpp create mode 100644 11113.cpp create mode 100644 11114.cpp create mode 100644 11115.cpp create mode 100644 11116.cpp create mode 100644 11117.cpp create mode 100644 11118.cpp create mode 100644 11119.cpp create mode 100644 1112.cpp create mode 100644 11120.cpp create mode 100644 11122.cpp create mode 100644 11123.cpp create mode 100644 11124.cpp create mode 100644 11125.cpp create mode 100644 11126.cpp create mode 100644 11127.cpp create mode 100644 11128.cpp create mode 100644 11129.cpp create mode 100644 1113.cpp create mode 100644 11130.cpp create mode 100644 11131.cpp create mode 100644 11132.cpp create mode 100644 11133.cpp create mode 100644 11134.cpp create mode 100644 11135.cpp create mode 100644 11138.cpp create mode 100644 11139.cpp create mode 100644 11140.cpp create mode 100644 11141.cpp create mode 100644 11142.cpp create mode 100644 11144.cpp create mode 100644 11145.cpp create mode 100644 11146.cpp create mode 100644 11147.cpp create mode 100644 11148.cpp create mode 100644 11149.cpp create mode 100644 1115.cpp create mode 100644 11153.cpp create mode 100644 11154.cpp create mode 100644 11155.cpp create mode 100644 11156.cpp create mode 100644 11157.cpp create mode 100644 11158.cpp create mode 100644 11159.cpp create mode 100644 11160.cpp create mode 100644 11161.cpp create mode 100644 11162.cpp create mode 100644 11163.cpp create mode 100644 11164.cpp create mode 100644 11165.cpp create mode 100644 11166.cpp create mode 100644 11167.cpp create mode 100644 11168.cpp create mode 100644 11169.cpp create mode 100644 11170.cpp create mode 100644 11171.cpp create mode 100644 11173.cpp create mode 100644 11174.cpp create mode 100644 11175.cpp create mode 100644 11176.cpp create mode 100644 11177.cpp create mode 100644 11178.cpp create mode 100644 11179.cpp create mode 100644 1118.cpp create mode 100644 11180.cpp create mode 100644 11181.cpp create mode 100644 11182.cpp create mode 100644 11183.cpp create mode 100644 11184.cpp create mode 100644 11186.cpp create mode 100644 11187.cpp create mode 100644 11189.cpp create mode 100644 1119.cpp create mode 100644 11190.cpp create mode 100644 11191.cpp create mode 100644 11193.cpp create mode 100644 11195.cpp create mode 100644 11196.cpp create mode 100644 11198.cpp create mode 100644 112.cpp create mode 100644 1120.cpp create mode 100644 11200.cpp create mode 100644 11201.cpp create mode 100644 11202.cpp create mode 100644 11204.cpp create mode 100644 11205.cpp create mode 100644 11206.cpp create mode 100644 11208.cpp create mode 100644 11209.cpp create mode 100644 1121.cpp create mode 100644 11210.cpp create mode 100644 11212.cpp create mode 100644 11214.cpp create mode 100644 11215.cpp create mode 100644 11220.cpp create mode 100644 11222.cpp create mode 100644 11224.cpp create mode 100644 11225.cpp create mode 100644 11226.cpp create mode 100644 11227.cpp create mode 100644 11228.cpp create mode 100644 11230.cpp create mode 100644 11231.cpp create mode 100644 11232.cpp create mode 100644 11234.cpp create mode 100644 11235.cpp create mode 100644 11236.cpp create mode 100644 11237.cpp create mode 100644 11238.cpp create mode 100644 11240.cpp create mode 100644 11241.cpp create mode 100644 11242.cpp create mode 100644 11243.cpp create mode 100644 11245.cpp create mode 100644 11246.cpp create mode 100644 11248.cpp create mode 100644 11249.cpp create mode 100644 1125.cpp create mode 100644 11250.cpp create mode 100644 11251.cpp create mode 100644 11252.cpp create mode 100644 11253.cpp create mode 100644 11254.cpp create mode 100644 11255.cpp create mode 100644 11256.cpp create mode 100644 11257.cpp create mode 100644 11258.cpp create mode 100644 11259.cpp create mode 100644 11261.cpp create mode 100644 11262.cpp create mode 100644 11264.cpp create mode 100644 11265.cpp create mode 100644 11266.cpp create mode 100644 11267.cpp create mode 100644 11268.cpp create mode 100644 11269.cpp create mode 100644 11275.cpp create mode 100644 11277.cpp create mode 100644 11278.cpp create mode 100644 11279.cpp create mode 100644 11280.cpp create mode 100644 11281.cpp create mode 100644 11282.cpp create mode 100644 11283.cpp create mode 100644 11284.cpp create mode 100644 11285.cpp create mode 100644 11288.cpp create mode 100644 11290.cpp create mode 100644 11291.cpp create mode 100644 11294.cpp create mode 100644 11296.cpp create mode 100644 112972.cpp create mode 100644 11298.cpp create mode 100644 11300.cpp create mode 100644 11301.cpp create mode 100644 11307.cpp create mode 100644 11310.cpp create mode 100644 11311.cpp create mode 100644 11313.cpp create mode 100644 11314.cpp create mode 100644 11317.cpp create mode 100644 11318.cpp create mode 100644 11319.cpp create mode 100644 11323.cpp create mode 100644 11324.cpp create mode 100644 11325.cpp create mode 100644 11326.cpp create mode 100644 11327.cpp create mode 100644 11328.cpp create mode 100644 11329.cpp create mode 100644 1133.cpp create mode 100644 11330.cpp create mode 100644 11331.cpp create mode 100644 11332.cpp create mode 100644 11333.cpp create mode 100644 11334.cpp create mode 100644 11335.cpp create mode 100644 11336.cpp create mode 100644 11337.cpp create mode 100644 11338.cpp create mode 100644 11341.cpp create mode 100644 11343.cpp create mode 100644 11344.cpp create mode 100644 11345.cpp create mode 100644 11346.cpp create mode 100644 11347.cpp create mode 100644 11348.cpp create mode 100644 11350.cpp create mode 100644 11352.cpp create mode 100644 11353.cpp create mode 100644 11354.cpp create mode 100644 11355.cpp create mode 100644 11356.cpp create mode 100644 11357.cpp create mode 100644 11358.cpp create mode 100644 11359.cpp create mode 100644 11361.cpp create mode 100644 11363.cpp create mode 100644 11364.cpp create mode 100644 11366.cpp create mode 100644 11367.cpp create mode 100644 11368.cpp create mode 100644 1137.cpp create mode 100644 11370.cpp create mode 100644 11372.cpp create mode 100644 11374.cpp create mode 100644 11375.cpp create mode 100644 11376.cpp create mode 100644 11377.cpp create mode 100644 11378.cpp create mode 100644 11379.cpp create mode 100644 11380.cpp create mode 100644 11381.cpp create mode 100644 11382.cpp create mode 100644 11383.cpp create mode 100644 11385.cpp create mode 100644 11387.cpp create mode 100644 11390.cpp create mode 100644 11391.cpp create mode 100644 11392.cpp create mode 100644 11393.cpp create mode 100644 11394.cpp create mode 100644 11395.cpp create mode 100644 11396.cpp create mode 100644 11399.cpp create mode 100644 114.cpp create mode 100644 11400.cpp create mode 100644 11402.cpp create mode 100644 11403.cpp create mode 100644 11404.cpp create mode 100644 11405.cpp create mode 100644 11407.cpp create mode 100644 11408.cpp create mode 100644 11409.cpp create mode 100644 1141.cpp create mode 100644 11410.cpp create mode 100644 11411.cpp create mode 100644 11412.cpp create mode 100644 11413.cpp create mode 100644 11414.cpp create mode 100644 11415.cpp create mode 100644 11416.cpp create mode 100644 11418.cpp create mode 100644 11419.cpp create mode 100644 11420.cpp create mode 100644 11421.cpp create mode 100644 11422.cpp create mode 100644 11423.cpp create mode 100644 11424.cpp create mode 100644 11426.cpp create mode 100644 11427.cpp create mode 100644 11430.cpp create mode 100644 11432.cpp create mode 100644 11439.cpp create mode 100644 11440.cpp create mode 100644 11444.cpp create mode 100644 11447.cpp create mode 100644 11448.cpp create mode 100644 11451.cpp create mode 100644 11452.cpp create mode 100644 11456.cpp create mode 100644 11458.cpp create mode 100644 1146.cpp create mode 100644 11460.cpp create mode 100644 11464.cpp create mode 100644 11465.cpp create mode 100644 11468.cpp create mode 100644 11471.cpp create mode 100644 11472.cpp create mode 100644 11473.cpp create mode 100644 11474.cpp create mode 100644 11476.cpp create mode 100644 11478.cpp create mode 100644 11479.cpp create mode 100644 1148.cpp create mode 100644 11480.cpp create mode 100644 11481.cpp create mode 100644 11482.cpp create mode 100644 11484.cpp create mode 100644 11485.cpp create mode 100644 11486.cpp create mode 100644 11487.cpp create mode 100644 11488.cpp create mode 100644 11489.cpp create mode 100644 1149.cpp create mode 100644 11490.cpp create mode 100644 11491.cpp create mode 100644 11492.cpp create mode 100644 11493.cpp create mode 100644 11496.cpp create mode 100644 11497.cpp create mode 100644 11499.cpp create mode 100644 115.cpp create mode 100644 11500.cpp create mode 100644 11501.cpp create mode 100644 11502.cpp create mode 100644 11503.cpp create mode 100644 11504.cpp create mode 100644 11506.cpp create mode 100644 11507.cpp create mode 100644 11508.cpp create mode 100644 11509.cpp create mode 100644 1151.cpp create mode 100644 11510.cpp create mode 100644 11511.cpp create mode 100644 11512.cpp create mode 100644 11513.cpp create mode 100644 11514.cpp create mode 100644 11515.cpp create mode 100644 11516.cpp create mode 100644 11517.cpp create mode 100644 11518.cpp create mode 100644 11519.cpp create mode 100644 1152.cpp create mode 100644 11520.cpp create mode 100644 11522.cpp create mode 100644 11523.cpp create mode 100644 11524.cpp create mode 100644 11529.cpp create mode 100644 1153.cpp create mode 100644 11534.cpp create mode 100644 11535.cpp create mode 100644 11536.cpp create mode 100644 11539.cpp create mode 100644 11540.cpp create mode 100644 11542.cpp create mode 100644 11544.cpp create mode 100644 11545.cpp create mode 100644 11546.cpp create mode 100644 11548.cpp create mode 100644 11549.cpp create mode 100644 11550.cpp create mode 100644 11551.cpp create mode 100644 11552.cpp create mode 100644 11553.cpp create mode 100644 11554.cpp create mode 100644 11555.cpp create mode 100644 11556.cpp create mode 100644 11557.cpp create mode 100644 11561.cpp create mode 100644 11562.cpp create mode 100644 11563.cpp create mode 100644 11565.cpp create mode 100644 11566.cpp create mode 100644 11567.cpp create mode 100644 11568.cpp create mode 100644 11569.cpp create mode 100644 11570.cpp create mode 100644 11571.cpp create mode 100644 11572.cpp create mode 100644 11573.cpp create mode 100644 11574.cpp create mode 100644 11575.cpp create mode 100644 11576.cpp create mode 100644 11578.cpp create mode 100644 11579.cpp create mode 100644 1158.cpp create mode 100644 11582.cpp create mode 100644 11583.cpp create mode 100644 11584.cpp create mode 100644 11585.cpp create mode 100644 11586.cpp create mode 100644 11587.cpp create mode 100644 11589.cpp create mode 100644 11590.cpp create mode 100644 11591.cpp create mode 100644 11593.cpp create mode 100644 11594.cpp create mode 100644 11598.cpp create mode 100644 1160.cpp create mode 100644 11600.cpp create mode 100644 11601.cpp create mode 100644 11603.cpp create mode 100644 11604.cpp create mode 100644 11605.cpp create mode 100644 1161.cpp create mode 100644 11610.cpp create mode 100644 11611.cpp create mode 100644 11612.cpp create mode 100644 11613.cpp create mode 100644 11614.cpp create mode 100644 11615.cpp create mode 100644 11617.cpp create mode 100644 1162.cpp create mode 100644 11620.cpp create mode 100644 11621.cpp create mode 100644 11623.cpp create mode 100644 11624.cpp create mode 100644 11625.cpp create mode 100644 11626.cpp create mode 100644 11628.cpp create mode 100644 11629.cpp create mode 100644 1163.cpp create mode 100644 11630.cpp create mode 100644 11631.cpp create mode 100644 11632.cpp create mode 100644 11633.cpp create mode 100644 11635.cpp create mode 100644 11637.cpp create mode 100644 11638.cpp create mode 100644 11639.cpp create mode 100644 1164.cpp create mode 100644 11643.cpp create mode 100644 11646.cpp create mode 100644 11648.cpp create mode 100644 11649.cpp create mode 100644 11651.cpp create mode 100644 11653.cpp create mode 100644 11654.cpp create mode 100644 11655.cpp create mode 100644 11656.cpp create mode 100644 11657.cpp create mode 100644 11658.cpp create mode 100644 11659.cpp create mode 100644 1166.cpp create mode 100644 11660.cpp create mode 100644 11662.cpp create mode 100644 11663.cpp create mode 100644 11664.cpp create mode 100644 11665.cpp create mode 100644 11666.cpp create mode 100644 11667.cpp create mode 100644 11668.cpp create mode 100644 11669.cpp create mode 100644 11670.cpp create mode 100644 11671.cpp create mode 100644 11672.cpp create mode 100644 11673.cpp create mode 100644 11675.cpp create mode 100644 11676.cpp create mode 100644 11679.cpp create mode 100644 11680.cpp create mode 100644 11681.cpp create mode 100644 11683.cpp create mode 100644 11684.cpp create mode 100644 11685.cpp create mode 100644 11686.cpp create mode 100644 11688.cpp create mode 100644 1169.cpp create mode 100644 11690.cpp create mode 100644 11691.cpp create mode 100644 11692.cpp create mode 100644 11693.cpp create mode 100644 11694.cpp create mode 100644 11695.cpp create mode 100644 11696.cpp create mode 100644 11697.cpp create mode 100644 11698.cpp create mode 100644 11699.cpp create mode 100644 117.cpp create mode 100644 1170.cpp create mode 100644 11700.cpp create mode 100644 11701.cpp create mode 100644 11702.cpp create mode 100644 11704.cpp create mode 100644 11705.cpp create mode 100644 11706.cpp create mode 100644 11708.cpp create mode 100644 11709.cpp create mode 100644 1171.cpp create mode 100644 11710.cpp create mode 100644 11711.cpp create mode 100644 11712.cpp create mode 100644 11717.cpp create mode 100644 11719.cpp create mode 100644 1172.cpp create mode 100644 11721.cpp create mode 100644 11722.cpp create mode 100644 11724.cpp create mode 100644 11725.cpp create mode 100644 11726.cpp create mode 100644 11729.cpp create mode 100644 1173.cpp create mode 100644 11730.cpp create mode 100644 11731.cpp create mode 100644 11732.cpp create mode 100644 11733.cpp create mode 100644 11735.cpp create mode 100644 11736.cpp create mode 100644 11737.cpp create mode 100644 11739.cpp create mode 100644 1174.cpp create mode 100644 11741.cpp create mode 100644 11742.cpp create mode 100644 11744.cpp create mode 100644 11745.cpp create mode 100644 11746.cpp create mode 100644 11747.cpp create mode 100644 11748.cpp create mode 100644 11749.cpp create mode 100644 1175.cpp create mode 100644 11751.cpp create mode 100644 11752.cpp create mode 100644 11754.cpp create mode 100644 11755.cpp create mode 100644 11757.cpp create mode 100644 11759.cpp create mode 100644 11760.cpp create mode 100644 11761.cpp create mode 100644 11762.cpp create mode 100644 11765.cpp create mode 100644 11766.cpp create mode 100644 11768.cpp create mode 100644 11769.cpp create mode 100644 1177.cpp create mode 100644 11770.cpp create mode 100644 11775.cpp create mode 100644 11776.cpp create mode 100644 11779.cpp create mode 100644 11780.cpp create mode 100644 11782.cpp create mode 100644 11783.cpp create mode 100644 11784.cpp create mode 100644 11785.cpp create mode 100644 11786.cpp create mode 100644 11787.cpp create mode 100644 11788.cpp create mode 100644 11790.cpp create mode 100644 11792.cpp create mode 100644 11795.cpp create mode 100644 11796.cpp create mode 100644 11797.cpp create mode 100644 11798.cpp create mode 100644 118.cpp create mode 100644 1180.cpp create mode 100644 11801.cpp create mode 100644 11802.cpp create mode 100644 11803.cpp create mode 100644 11806.cpp create mode 100644 11809.cpp create mode 100644 11813.cpp create mode 100644 11814.cpp create mode 100644 11815.cpp create mode 100644 11817.cpp create mode 100644 11818.cpp create mode 100644 11819.cpp create mode 100644 1182.cpp create mode 100644 11820.cpp create mode 100644 11821.cpp create mode 100644 11823.cpp create mode 100644 11825.cpp create mode 100644 11828.cpp create mode 100644 11829.cpp create mode 100644 11832.cpp create mode 100644 11833.cpp create mode 100644 11835.cpp create mode 100644 11837.cpp create mode 100644 1184.cpp create mode 100644 11840.cpp create mode 100644 11841.cpp create mode 100644 11842.cpp create mode 100644 11843.cpp create mode 100644 11844.cpp create mode 100644 11845.cpp create mode 100644 11846.cpp create mode 100644 11848.cpp create mode 100644 11851.cpp create mode 100644 11852.cpp create mode 100644 11853.cpp create mode 100644 11855.cpp create mode 100644 11858.cpp create mode 100644 1186.cpp create mode 100644 11860.cpp create mode 100644 11863.cpp create mode 100644 11865.cpp create mode 100644 11867.cpp create mode 100644 1187.cpp create mode 100644 11870.cpp create mode 100644 11872.cpp create mode 100644 11879.cpp create mode 100644 1188.cpp create mode 100644 11880.cpp create mode 100644 11881.cpp create mode 100644 11882.cpp create mode 100644 11883.cpp create mode 100644 11884.cpp create mode 100644 11885.cpp create mode 100644 11886.cpp create mode 100644 1189.cpp create mode 100644 11890.cpp create mode 100644 11891.cpp create mode 100644 11892.cpp create mode 100644 11893.cpp create mode 100644 11894.cpp create mode 100644 11895.cpp create mode 100644 11897.cpp create mode 100644 11898.cpp create mode 100644 11901.cpp create mode 100644 11903.cpp create mode 100644 11904.cpp create mode 100644 11906.cpp create mode 100644 11907.cpp create mode 100644 11908.cpp create mode 100644 11910.cpp create mode 100644 11911.cpp create mode 100644 11913.cpp create mode 100644 11916.cpp create mode 100644 11918.cpp create mode 100644 11919.cpp create mode 100644 1192.cpp create mode 100644 11920.cpp create mode 100644 11921.cpp create mode 100644 11922.cpp create mode 100644 11923.cpp create mode 100644 11925.cpp create mode 100644 11926.cpp create mode 100644 11927.cpp create mode 100644 1193.cpp create mode 100644 11930.cpp create mode 100644 11931.cpp create mode 100644 11932.cpp create mode 100644 11935.cpp create mode 100644 1194.cpp create mode 100644 11940.cpp create mode 100644 11941.cpp create mode 100644 11943.cpp create mode 100644 11944.cpp create mode 100644 11947.cpp create mode 100644 11948.cpp create mode 100644 11949.cpp create mode 100644 1195.cpp create mode 100644 11950.cpp create mode 100644 11951.cpp create mode 100644 11952.cpp create mode 100644 11954.cpp create mode 100644 11956.cpp create mode 100644 11957.cpp create mode 100644 11958.cpp create mode 100644 11959.cpp create mode 100644 1196.cpp create mode 100644 11961.cpp create mode 100644 11962.cpp create mode 100644 11964.cpp create mode 100644 11966.cpp create mode 100644 11967.cpp create mode 100644 11968.cpp create mode 100644 1197.cpp create mode 100644 11970.cpp create mode 100644 11971.cpp create mode 100644 11972.cpp create mode 100644 11974.cpp create mode 100644 11975.cpp create mode 100644 11976.cpp create mode 100644 11977.cpp create mode 100644 11978.cpp create mode 100644 11980.cpp create mode 100644 11981.cpp create mode 100644 11982.cpp create mode 100644 11983.cpp create mode 100644 11985.cpp create mode 100644 11987.cpp create mode 100644 11988.cpp create mode 100644 11989.cpp create mode 100644 1199.cpp create mode 100644 11990.cpp create mode 100644 11991.cpp create mode 100644 11992.cpp create mode 100644 11993.cpp create mode 100644 11994.cpp create mode 100644 11995.cpp create mode 100644 11996.cpp create mode 100644 11997.cpp create mode 100644 11999.cpp create mode 100644 120.cpp create mode 100644 1200.cpp create mode 100644 12000.cpp create mode 100644 12001.cpp create mode 100644 12002.cpp create mode 100644 12003.cpp create mode 100644 12004.cpp create mode 100644 12005.cpp create mode 100644 12008.cpp create mode 100644 12009.cpp create mode 100644 1201.cpp create mode 100644 12010.cpp create mode 100644 12011.cpp create mode 100644 12012.cpp create mode 100644 12013.cpp create mode 100644 12014.cpp create mode 100644 12016.cpp create mode 100644 12017.cpp create mode 100644 12018.cpp create mode 100644 1202.cpp create mode 100644 12026.cpp create mode 100644 12028.cpp create mode 100644 12029.cpp create mode 100644 1203.cpp create mode 100644 12030.cpp create mode 100644 12031.cpp create mode 100644 12032.cpp create mode 100644 12033.cpp create mode 100644 12034.cpp create mode 100644 12035.cpp create mode 100644 12037.cpp create mode 100644 12038.cpp create mode 100644 12039.cpp create mode 100644 1204.cpp create mode 100644 12040.cpp create mode 100644 12041.cpp create mode 100644 12042.cpp create mode 100644 12043.cpp create mode 100644 12045.cpp create mode 100644 12046.cpp create mode 100644 12047.cpp create mode 100644 12048.cpp create mode 100644 1205.cpp create mode 100644 12050.cpp create mode 100644 12051.cpp create mode 100644 12052.cpp create mode 100644 12054.cpp create mode 100644 12057.cpp create mode 100644 12058.cpp create mode 100644 1206.cpp create mode 100644 12060.cpp create mode 100644 12061.cpp create mode 100644 12062.cpp create mode 100644 12063.cpp create mode 100644 12064.cpp create mode 100644 12065.cpp create mode 100644 12068.cpp create mode 100644 12069.cpp create mode 100644 1207.cpp create mode 100644 12070.cpp create mode 100644 12071.cpp create mode 100644 12072.cpp create mode 100644 12073.cpp create mode 100644 12074.cpp create mode 100644 12075.cpp create mode 100644 12077.cpp create mode 100644 12078.cpp create mode 100644 12079.cpp create mode 100644 1208.cpp create mode 100644 12081.cpp create mode 100644 12082.cpp create mode 100644 12083.cpp create mode 100644 12084.cpp create mode 100644 12085.cpp create mode 100644 12086.cpp create mode 100644 12087.cpp create mode 100644 12088.cpp create mode 100644 12089.cpp create mode 100644 1209.cpp create mode 100644 12090.cpp create mode 100644 12091.cpp create mode 100644 12092.cpp create mode 100644 12093.cpp create mode 100644 12094.cpp create mode 100644 12095.cpp create mode 100644 12096.cpp create mode 100644 12097.cpp create mode 100644 12098.cpp create mode 100644 12099.cpp create mode 100644 121.cpp create mode 100644 12100.cpp create mode 100644 12101.cpp create mode 100644 12102.cpp create mode 100644 12103.cpp create mode 100644 12105.cpp create mode 100644 12107.cpp create mode 100644 12108.cpp create mode 100644 12109.cpp create mode 100644 1211.cpp create mode 100644 12112.cpp create mode 100644 12113.cpp create mode 100644 12115.cpp create mode 100644 12116.cpp create mode 100644 12117.cpp create mode 100644 12118.cpp create mode 100644 12119.cpp create mode 100644 1212.cpp create mode 100644 12120.cpp create mode 100644 12121.cpp create mode 100644 12122.cpp create mode 100644 12123.cpp create mode 100644 12124.cpp create mode 100644 12125.cpp create mode 100644 12126.cpp create mode 100644 12128.cpp create mode 100644 1213.cpp create mode 100644 12130.cpp create mode 100644 12131.cpp create mode 100644 12132.cpp create mode 100644 12134.cpp create mode 100644 12135.cpp create mode 100644 12137.cpp create mode 100644 12138.cpp create mode 100644 12139.cpp create mode 100644 1214.cpp create mode 100644 12140.cpp create mode 100644 12141.cpp create mode 100644 12143.cpp create mode 100644 12144.cpp create mode 100644 12146.cpp create mode 100644 12147.cpp create mode 100644 12148.cpp create mode 100644 12149.cpp create mode 100644 1215.cpp create mode 100644 12150.cpp create mode 100644 12153.cpp create mode 100644 12154.cpp create mode 100644 12155.cpp create mode 100644 12159.cpp create mode 100644 1216.cpp create mode 100644 12160.cpp create mode 100644 12161.cpp create mode 100644 12163.cpp create mode 100644 12165.cpp create mode 100644 12166.cpp create mode 100644 12167.cpp create mode 100644 12168.cpp create mode 100644 12169.cpp create mode 100644 1217.cpp create mode 100644 12170.cpp create mode 100644 12171.cpp create mode 100644 12172.cpp create mode 100644 12173.cpp create mode 100644 12174.cpp create mode 100644 12175.cpp create mode 100644 12176.cpp create mode 100644 12177.cpp create mode 100644 12178.cpp create mode 100644 12179.cpp create mode 100644 1218.cpp create mode 100644 12180.cpp create mode 100644 12181.cpp create mode 100644 12182.cpp create mode 100644 12183.cpp create mode 100644 12184.cpp create mode 100644 12186.cpp create mode 100644 12187.cpp create mode 100644 12188.cpp create mode 100644 12189.cpp create mode 100644 1219.cpp create mode 100644 12190.cpp create mode 100644 12191.cpp create mode 100644 12192.cpp create mode 100644 12193.cpp create mode 100644 12194.cpp create mode 100644 12196.cpp create mode 100644 12197.cpp create mode 100644 12198.cpp create mode 100644 12199.cpp create mode 100644 122.cpp create mode 100644 1220.cpp create mode 100644 12200.cpp create mode 100644 12201.cpp create mode 100644 12202.cpp create mode 100644 12203.cpp create mode 100644 12204.cpp create mode 100644 12206.cpp create mode 100644 12207.cpp create mode 100644 12209.cpp create mode 100644 1221.cpp create mode 100644 12210.cpp create mode 100644 12211.cpp create mode 100644 12212.cpp create mode 100644 12213.cpp create mode 100644 12214.cpp create mode 100644 12215.cpp create mode 100644 12216.cpp create mode 100644 12217.cpp create mode 100644 12218.cpp create mode 100644 12219.cpp create mode 100644 1222.cpp create mode 100644 12220.cpp create mode 100644 12222.cpp create mode 100644 12223.cpp create mode 100644 12225.cpp create mode 100644 12226.cpp create mode 100644 12227.cpp create mode 100644 1223.cpp create mode 100644 12230.cpp create mode 100644 12231.cpp create mode 100644 12232.cpp create mode 100644 12233.cpp create mode 100644 12235.cpp create mode 100644 12238.cpp create mode 100644 1224.cpp create mode 100644 12240.cpp create mode 100644 12241.cpp create mode 100644 12242.cpp create mode 100644 12244.cpp create mode 100644 12245.cpp create mode 100644 12246.cpp create mode 100644 12248.cpp create mode 100644 12249.cpp create mode 100644 12251.cpp create mode 100644 12252.cpp create mode 100644 12253.cpp create mode 100644 12255.cpp create mode 100644 12256.cpp create mode 100644 12257.cpp create mode 100644 12259.cpp create mode 100644 1226.cpp create mode 100644 12260.cpp create mode 100644 12261.cpp create mode 100644 12262.cpp create mode 100644 12263.cpp create mode 100644 12264.cpp create mode 100644 12265.cpp create mode 100644 12266.cpp create mode 100644 12267.cpp create mode 100644 12268.cpp create mode 100644 1227.cpp create mode 100644 12270.cpp create mode 100644 12271.cpp create mode 100644 12272.cpp create mode 100644 12273.cpp create mode 100644 12274.cpp create mode 100644 12275.cpp create mode 100644 12276.cpp create mode 100644 12278.cpp create mode 100644 1228.cpp create mode 100644 12280.cpp create mode 100644 12281.cpp create mode 100644 12282.cpp create mode 100644 12283.cpp create mode 100644 12284.cpp create mode 100644 12285.cpp create mode 100644 12286.cpp create mode 100644 1229.cpp create mode 100644 12290.cpp create mode 100644 12291.cpp create mode 100644 12292.cpp create mode 100644 12293.cpp create mode 100644 12294.cpp create mode 100644 12295.cpp create mode 100644 12296.cpp create mode 100644 12297.cpp create mode 100644 12298.cpp create mode 100644 12299.cpp create mode 100644 123.cpp create mode 100644 12300.cpp create mode 100644 12301.cpp create mode 100644 12302.cpp create mode 100644 12303.cpp create mode 100644 12304.cpp create mode 100644 12305.cpp create mode 100644 12306.cpp create mode 100644 12307.cpp create mode 100644 12308.cpp create mode 100644 1231.cpp create mode 100644 12311.cpp create mode 100644 12312.cpp create mode 100644 12314.cpp create mode 100644 12315.cpp create mode 100644 12317.cpp create mode 100644 12318.cpp create mode 100644 12319.cpp create mode 100644 1232.cpp create mode 100644 12320.cpp create mode 100644 12321.cpp create mode 100644 12322.cpp create mode 100644 12323.cpp create mode 100644 12324.cpp create mode 100644 12325.cpp create mode 100644 12326.cpp create mode 100644 12327.cpp create mode 100644 12328.cpp create mode 100644 12329.cpp create mode 100644 1233.cpp create mode 100644 12330.cpp create mode 100644 12331.cpp create mode 100644 12332.cpp create mode 100644 12333.cpp create mode 100644 12334.cpp create mode 100644 12335.cpp create mode 100644 12336.cpp create mode 100644 12337.cpp create mode 100644 12338.cpp create mode 100644 12339.cpp create mode 100644 1234.cpp create mode 100644 12343.cpp create mode 100644 12345.cpp create mode 100644 12346.cpp create mode 100644 12347.cpp create mode 100644 12349.cpp create mode 100644 1235.cpp create mode 100644 12350.cpp create mode 100644 12351.cpp create mode 100644 12352.cpp create mode 100644 12353.cpp create mode 100644 12354.cpp create mode 100644 12355.cpp create mode 100644 12356.cpp create mode 100644 12357.cpp create mode 100644 12358.cpp create mode 100644 12359.cpp create mode 100644 1236.cpp create mode 100644 12360.cpp create mode 100644 12361.cpp create mode 100644 12362.cpp create mode 100644 12363.cpp create mode 100644 12364.cpp create mode 100644 12365.cpp create mode 100644 12366.cpp create mode 100644 12367.cpp create mode 100644 12368.cpp create mode 100644 12369.cpp create mode 100644 12370.cpp create mode 100644 12371.cpp create mode 100644 12373.cpp create mode 100644 12374.cpp create mode 100644 12375.cpp create mode 100644 12376.cpp create mode 100644 12377.cpp create mode 100644 12378.cpp create mode 100644 12379.cpp create mode 100644 1238.cpp create mode 100644 12380.cpp create mode 100644 12381.cpp create mode 100644 12382.cpp create mode 100644 12383.cpp create mode 100644 12384.cpp create mode 100644 12385.cpp create mode 100644 12386.cpp create mode 100644 12387.cpp create mode 100644 12388.cpp create mode 100644 12389.cpp create mode 100644 1239.cpp create mode 100644 12390.cpp create mode 100644 12391.cpp create mode 100644 12392.cpp create mode 100644 12393.cpp create mode 100644 12394.cpp create mode 100644 12395.cpp create mode 100644 12396.cpp create mode 100644 12397.cpp create mode 100644 12398.cpp create mode 100644 12399.cpp create mode 100644 124.cpp create mode 100644 12400.cpp create mode 100644 12401.cpp create mode 100644 12402.cpp create mode 100644 12404.cpp create mode 100644 12407.cpp create mode 100644 12408.cpp create mode 100644 12409.cpp create mode 100644 1241.cpp create mode 100644 12410.cpp create mode 100644 12411.cpp create mode 100644 12412.cpp create mode 100644 12414.cpp create mode 100644 12415.cpp create mode 100644 12418.cpp create mode 100644 1242.cpp create mode 100644 12420.cpp create mode 100644 12424.cpp create mode 100644 12425.cpp create mode 100644 12426.cpp create mode 100644 12427.cpp create mode 100644 12428.cpp create mode 100644 12429.cpp create mode 100644 12430.cpp create mode 100644 12431.cpp create mode 100644 12432.cpp create mode 100644 12433.cpp create mode 100644 12435.cpp create mode 100644 12436.cpp create mode 100644 12437.cpp create mode 100644 12438.cpp create mode 100644 12439.cpp create mode 100644 1244.cpp create mode 100644 12440.cpp create mode 100644 12441.cpp create mode 100644 12442.cpp create mode 100644 12443.cpp create mode 100644 12444.cpp create mode 100644 12445.cpp create mode 100644 12446.cpp create mode 100644 12447.cpp create mode 100644 12448.cpp create mode 100644 12449.cpp create mode 100644 1245.cpp create mode 100644 12450.cpp create mode 100644 12451.cpp create mode 100644 12452.cpp create mode 100644 12453.cpp create mode 100644 12454.cpp create mode 100644 12455.cpp create mode 100644 12456.cpp create mode 100644 12457.cpp create mode 100644 12458.cpp create mode 100644 12459.cpp create mode 100644 1246.cpp create mode 100644 12460.cpp create mode 100644 12461.cpp create mode 100644 12462.cpp create mode 100644 12463.cpp create mode 100644 12464.cpp create mode 100644 12465.cpp create mode 100644 12466.cpp create mode 100644 12467.cpp create mode 100644 12468.cpp create mode 100644 12469.cpp create mode 100644 1247.cpp create mode 100644 12470.cpp create mode 100644 12471.cpp create mode 100644 12472.cpp create mode 100644 12473.cpp create mode 100644 12474.cpp create mode 100644 12475.cpp create mode 100644 12476.cpp create mode 100644 12477.cpp create mode 100644 12478.cpp create mode 100644 12479.cpp create mode 100644 1248.cpp create mode 100644 12480.cpp create mode 100644 12481.cpp create mode 100644 12482.cpp create mode 100644 12483.cpp create mode 100644 12484.cpp create mode 100644 12485.cpp create mode 100644 12486.cpp create mode 100644 12487.cpp create mode 100644 12488.cpp create mode 100644 12489.cpp create mode 100644 1249.cpp create mode 100644 12490.cpp create mode 100644 12491.cpp create mode 100644 12492.cpp create mode 100644 12493.cpp create mode 100644 12494.cpp create mode 100644 12495.cpp create mode 100644 12496.cpp create mode 100644 12497.cpp create mode 100644 12498.cpp create mode 100644 12499.cpp create mode 100644 125.cpp create mode 100644 1250.cpp create mode 100644 12501.cpp create mode 100644 12502.cpp create mode 100644 12503.cpp create mode 100644 12504.cpp create mode 100644 12505.cpp create mode 100644 12506.cpp create mode 100644 12507.cpp create mode 100644 12508.cpp create mode 100644 12509.cpp create mode 100644 1251.cpp create mode 100644 12510.cpp create mode 100644 12511.cpp create mode 100644 12512.cpp create mode 100644 12514.cpp create mode 100644 12515.cpp create mode 100644 12516.cpp create mode 100644 12517.cpp create mode 100644 12518.cpp create mode 100644 12519.cpp create mode 100644 1252.cpp create mode 100644 12520.cpp create mode 100644 12521.cpp create mode 100644 12522.cpp create mode 100644 12523.cpp create mode 100644 12524.cpp create mode 100644 12525.cpp create mode 100644 12526.cpp create mode 100644 12527.cpp create mode 100644 12528.cpp create mode 100644 12529.cpp create mode 100644 1253.cpp create mode 100644 12530.cpp create mode 100644 12531.cpp create mode 100644 12532.cpp create mode 100644 12533.cpp create mode 100644 12534.cpp create mode 100644 12535.cpp create mode 100644 12536.cpp create mode 100644 12537.cpp create mode 100644 12538.cpp create mode 100644 12539.cpp create mode 100644 1254.cpp create mode 100644 12540.cpp create mode 100644 12541.cpp create mode 100644 12542.cpp create mode 100644 12543.cpp create mode 100644 12544.cpp create mode 100644 12545.cpp create mode 100644 12546.cpp create mode 100644 12547.cpp create mode 100644 12548.cpp create mode 100644 12549.cpp create mode 100644 1255.cpp create mode 100644 12550.cpp create mode 100644 12551.cpp create mode 100644 12553.cpp create mode 100644 12554.cpp create mode 100644 12555.cpp create mode 100644 12556.cpp create mode 100644 12557.cpp create mode 100644 12558.cpp create mode 100644 12559.cpp create mode 100644 1256.cpp create mode 100644 12560.cpp create mode 100644 12561.cpp create mode 100644 12562.cpp create mode 100644 12563.cpp create mode 100644 12564.cpp create mode 100644 12569.cpp create mode 100644 12571.cpp create mode 100644 12572.cpp create mode 100644 12573.cpp create mode 100644 12574.cpp create mode 100644 12575.cpp create mode 100644 12576.cpp create mode 100644 12577.cpp create mode 100644 12578.cpp create mode 100644 1258.cpp create mode 100644 12580.cpp create mode 100644 12581.cpp create mode 100644 12582.cpp create mode 100644 12583.cpp create mode 100644 12584.cpp create mode 100644 12585.cpp create mode 100644 12586.cpp create mode 100644 12587.cpp create mode 100644 12588.cpp create mode 100644 12589.cpp create mode 100644 1259.cpp create mode 100644 12590.cpp create mode 100644 12592.cpp create mode 100644 12593.cpp create mode 100644 12594.cpp create mode 100644 12595.cpp create mode 100644 12596.cpp create mode 100644 12597.cpp create mode 100644 12598.cpp create mode 100644 12599.cpp create mode 100644 126.cpp create mode 100644 12600.cpp create mode 100644 12602.cpp create mode 100644 12603.cpp create mode 100644 12604.cpp create mode 100644 12605.cpp create mode 100644 12606.cpp create mode 100644 12607.cpp create mode 100644 12608.cpp create mode 100644 12609.cpp create mode 100644 1261.cpp create mode 100644 12610.cpp create mode 100644 12611.cpp create mode 100644 12612.cpp create mode 100644 12613.cpp create mode 100644 12614.cpp create mode 100644 12616.cpp create mode 100644 12617.cpp create mode 100644 12618.cpp create mode 100644 12619.cpp create mode 100644 1262.cpp create mode 100644 12620.cpp create mode 100644 12621.cpp create mode 100644 12622.cpp create mode 100644 12623.cpp create mode 100644 12625.cpp create mode 100644 12626.cpp create mode 100644 12627.cpp create mode 100644 12629.cpp create mode 100644 1263.cpp create mode 100644 12630.cpp create mode 100644 12631.cpp create mode 100644 12632.cpp create mode 100644 12633.cpp create mode 100644 12634.cpp create mode 100644 12635.cpp create mode 100644 12636.cpp create mode 100644 12637.cpp create mode 100644 12638.cpp create mode 100644 12639.cpp create mode 100644 1264.cpp create mode 100644 12640.cpp create mode 100644 12641.cpp create mode 100644 12642.cpp create mode 100644 12643.cpp create mode 100644 12644.cpp create mode 100644 12645.cpp create mode 100644 12646.cpp create mode 100644 12647.cpp create mode 100644 12648.cpp create mode 100644 12649.cpp create mode 100644 1265.cpp create mode 100644 12650.cpp create mode 100644 12651.cpp create mode 100644 12652.cpp create mode 100644 12653.cpp create mode 100644 12654.cpp create mode 100644 12655.cpp create mode 100644 12656.cpp create mode 100644 12657.cpp create mode 100644 12658.cpp create mode 100644 12659.cpp create mode 100644 1266.cpp create mode 100644 12660.cpp create mode 100644 12661.cpp create mode 100644 12662.cpp create mode 100644 12663.cpp create mode 100644 12664.cpp create mode 100644 12665.cpp create mode 100644 12667.cpp create mode 100644 12668.cpp create mode 100644 12669.cpp create mode 100644 1267.cpp create mode 100644 12670.cpp create mode 100644 12671.cpp create mode 100644 12672.cpp create mode 100644 12673.cpp create mode 100644 12674.cpp create mode 100644 12675.cpp create mode 100644 12676.cpp create mode 100644 12677.cpp create mode 100644 12679.cpp create mode 100644 12680.cpp create mode 100644 12681.cpp create mode 100644 12682.cpp create mode 100644 12683.cpp create mode 100644 12684.cpp create mode 100644 12685.cpp create mode 100644 12686.cpp create mode 100644 12688.cpp create mode 100644 12689.cpp create mode 100644 12690.cpp create mode 100644 12691.cpp create mode 100644 12692.cpp create mode 100644 12693.cpp create mode 100644 12694.cpp create mode 100644 12696.cpp create mode 100644 12697.cpp create mode 100644 12698.cpp create mode 100644 12699.cpp create mode 100644 127.cpp create mode 100644 12700.cpp create mode 100644 12701.cpp create mode 100644 12702.cpp create mode 100644 12703.cpp create mode 100644 12704.cpp create mode 100644 12705.cpp create mode 100644 12706.cpp create mode 100644 12707.cpp create mode 100644 12708.cpp create mode 100644 12709.cpp create mode 100644 12710.cpp create mode 100644 12711.cpp create mode 100644 12712.cpp create mode 100644 12713.cpp create mode 100644 12714.cpp create mode 100644 12715.cpp create mode 100644 12716.cpp create mode 100644 12717.cpp create mode 100644 12718.cpp create mode 100644 12719.cpp create mode 100644 12720.cpp create mode 100644 12721.cpp create mode 100644 12722.cpp create mode 100644 12723.cpp create mode 100644 12724.cpp create mode 100644 12725.cpp create mode 100644 12726.cpp create mode 100644 12727.cpp create mode 100644 12728.cpp create mode 100644 12729.cpp create mode 100644 12730.cpp create mode 100644 12743.cpp create mode 100644 12745.cpp create mode 100644 12747.cpp create mode 100644 12748.cpp create mode 100644 12749.cpp create mode 100644 12750.cpp create mode 100644 12751.cpp create mode 100644 12753.cpp create mode 100644 12754.cpp create mode 100644 12755.cpp create mode 100644 12761.cpp create mode 100644 12763.cpp create mode 100644 12764.cpp create mode 100644 12765.cpp create mode 100644 12766.cpp create mode 100644 12768.cpp create mode 100644 12769.cpp create mode 100644 12770.cpp create mode 100644 12775.cpp create mode 100644 12776.cpp create mode 100644 12778.cpp create mode 100644 12779.cpp create mode 100644 12783.cpp create mode 100644 12784.cpp create mode 100644 12785.cpp create mode 100644 12786.cpp create mode 100644 12787.cpp create mode 100644 12788.cpp create mode 100644 12789.cpp create mode 100644 1279.cpp create mode 100644 12790.cpp create mode 100644 12791.cpp create mode 100644 12792.cpp create mode 100644 12793.cpp create mode 100644 12794.cpp create mode 100644 12795.cpp create mode 100644 12796.cpp create mode 100644 12797.cpp create mode 100644 12798.cpp create mode 100644 12799.cpp create mode 100644 128.cpp create mode 100644 1280.cpp create mode 100644 12800.cpp create mode 100644 12801.cpp create mode 100644 12802.cpp create mode 100644 12803.cpp create mode 100644 12804.cpp create mode 100644 12805.cpp create mode 100644 12806.cpp create mode 100644 12808.cpp create mode 100644 12809.cpp create mode 100644 1281.cpp create mode 100644 12810.cpp create mode 100644 12811.cpp create mode 100644 12812.cpp create mode 100644 12813.cpp create mode 100644 12814.cpp create mode 100644 12815.cpp create mode 100644 12816.cpp create mode 100644 12817.cpp create mode 100644 12818.cpp create mode 100644 12819.cpp create mode 100644 1282.cpp create mode 100644 12820.cpp create mode 100644 12821.cpp create mode 100644 12822.cpp create mode 100644 12825.cpp create mode 100644 12826.cpp create mode 100644 12828.cpp create mode 100644 1283.cpp create mode 100644 12830.cpp create mode 100644 12831.cpp create mode 100644 12832.cpp create mode 100644 12833.cpp create mode 100644 12834.cpp create mode 100644 12836.cpp create mode 100644 12837.cpp create mode 100644 12839.cpp create mode 100644 12840.cpp create mode 100644 12841.cpp create mode 100644 12842.cpp create mode 100644 12843.cpp create mode 100644 12844.cpp create mode 100644 12846.cpp create mode 100644 12848.cpp create mode 100644 12849.cpp create mode 100644 1285.cpp create mode 100644 12850.cpp create mode 100644 12851.cpp create mode 100644 12852.cpp create mode 100644 12853.cpp create mode 100644 12854.cpp create mode 100644 12855.cpp create mode 100644 12856.cpp create mode 100644 12857.cpp create mode 100644 12858.cpp create mode 100644 12859.cpp create mode 100644 12860.cpp create mode 100644 12861.cpp create mode 100644 12862.cpp create mode 100644 12863.cpp create mode 100644 12864.cpp create mode 100644 12865.cpp create mode 100644 12866.cpp create mode 100644 12869.cpp create mode 100644 1287.cpp create mode 100644 12870.cpp create mode 100644 12871.cpp create mode 100644 12872.cpp create mode 100644 12873.cpp create mode 100644 12874.cpp create mode 100644 12875.cpp create mode 100644 12876.cpp create mode 100644 12877.cpp create mode 100644 12878.cpp create mode 100644 12879.cpp create mode 100644 12880.cpp create mode 100644 12881.cpp create mode 100644 12882.cpp create mode 100644 12883.cpp create mode 100644 12884.cpp create mode 100644 12885.cpp create mode 100644 12886.cpp create mode 100644 12887.cpp create mode 100644 12888.cpp create mode 100644 12889.cpp create mode 100644 1289.cpp create mode 100644 12890.cpp create mode 100644 12891.cpp create mode 100644 12892.cpp create mode 100644 12893.cpp create mode 100644 12894.cpp create mode 100644 12895.cpp create mode 100644 12896.cpp create mode 100644 12897.cpp create mode 100644 12898.cpp create mode 100644 12899.cpp create mode 100644 129.cpp create mode 100644 1290.cpp create mode 100644 12900.cpp create mode 100644 12901.cpp create mode 100644 12902.cpp create mode 100644 12903.cpp create mode 100644 12904.cpp create mode 100644 12905.cpp create mode 100644 12906.cpp create mode 100644 12907.cpp create mode 100644 12908.cpp create mode 100644 12909.cpp create mode 100644 1291.cpp create mode 100644 12910.cpp create mode 100644 12911.cpp create mode 100644 12912.cpp create mode 100644 12913.cpp create mode 100644 12914.cpp create mode 100644 12915.cpp create mode 100644 12916.cpp create mode 100644 12917.cpp create mode 100644 12918.cpp create mode 100644 12919.cpp create mode 100644 1292.cpp create mode 100644 12920.cpp create mode 100644 12921.cpp create mode 100644 12922.cpp create mode 100644 12923.cpp create mode 100644 12926.cpp create mode 100644 12927.cpp create mode 100644 12928.cpp create mode 100644 12929.cpp create mode 100644 12930.cpp create mode 100644 12931.cpp create mode 100644 12932.cpp create mode 100644 12933.cpp create mode 100644 12934.cpp create mode 100644 12936.cpp create mode 100644 12937.cpp create mode 100644 12938.cpp create mode 100644 12939.cpp create mode 100644 12940.cpp create mode 100644 12942.cpp create mode 100644 12943.cpp create mode 100644 12944.cpp create mode 100644 12945.cpp create mode 100644 12946.cpp create mode 100644 12947.cpp create mode 100644 12948.cpp create mode 100644 12949.cpp create mode 100644 12950.cpp create mode 100644 12951.cpp create mode 100644 12952.cpp create mode 100644 12953.cpp create mode 100644 12954.cpp create mode 100644 12955.cpp create mode 100644 12956.cpp create mode 100644 12957.cpp create mode 100644 12959.cpp create mode 100644 12960.cpp create mode 100644 12961.cpp create mode 100644 12962.cpp create mode 100644 12963.cpp create mode 100644 12965.cpp create mode 100644 12966.cpp create mode 100644 12967.cpp create mode 100644 12968.cpp create mode 100644 12970.cpp create mode 100644 12972.cpp create mode 100644 12974.cpp create mode 100644 12976.cpp create mode 100644 12978.cpp create mode 100644 1298.cpp create mode 100644 12981.cpp create mode 100644 12983.cpp create mode 100644 12984.cpp create mode 100644 12987.cpp create mode 100644 12988.cpp create mode 100644 12992.cpp create mode 100644 12994.cpp create mode 100644 12995.cpp create mode 100644 12996.cpp create mode 100644 12997.cpp create mode 100644 12999.cpp create mode 100644 13000.cpp create mode 100644 13001.cpp create mode 100644 13003.cpp create mode 100644 13004.cpp create mode 100644 13005.cpp create mode 100644 13006.cpp create mode 100644 13007.cpp create mode 100644 13008.cpp create mode 100644 13009.cpp create mode 100644 13010.cpp create mode 100644 13011.cpp create mode 100644 13012.cpp create mode 100644 13013.cpp create mode 100644 13014.cpp create mode 100644 13015.cpp create mode 100644 13017.cpp create mode 100644 13018.cpp create mode 100644 13021.cpp create mode 100644 13022.cpp create mode 100644 13024.cpp create mode 100644 13025.cpp create mode 100644 13026.cpp create mode 100644 13028.cpp create mode 100644 13029.cpp create mode 100644 1303.cpp create mode 100644 13031.cpp create mode 100644 13032.cpp create mode 100644 13034.cpp create mode 100644 13035.cpp create mode 100644 13036.cpp create mode 100644 13037.cpp create mode 100644 13038.cpp create mode 100644 13039.cpp create mode 100644 1304.cpp create mode 100644 13045.cpp create mode 100644 13046.cpp create mode 100644 13047.cpp create mode 100644 13048.cpp create mode 100644 13049.cpp create mode 100644 13052.cpp create mode 100644 13053.cpp create mode 100644 13054.cpp create mode 100644 13055.cpp create mode 100644 13056.cpp create mode 100644 13057.cpp create mode 100644 13058.cpp create mode 100644 13059.cpp create mode 100644 1306.cpp create mode 100644 13061.cpp create mode 100644 13062.cpp create mode 100644 13067.cpp create mode 100644 13068.cpp create mode 100644 13069.cpp create mode 100644 13070.cpp create mode 100644 13071.cpp create mode 100644 13072.cpp create mode 100644 13073.cpp create mode 100644 13074.cpp create mode 100644 13075.cpp create mode 100644 13078.cpp create mode 100644 13079.cpp create mode 100644 1308.cpp create mode 100644 13080.cpp create mode 100644 13081.cpp create mode 100644 13082.cpp create mode 100644 13085.cpp create mode 100644 13086.cpp create mode 100644 13088.cpp create mode 100644 1309.cpp create mode 100644 13090.cpp create mode 100644 13091.cpp create mode 100644 13092.cpp create mode 100644 13093.cpp create mode 100644 13094.cpp create mode 100644 13095.cpp create mode 100644 13096.cpp create mode 100644 13097.cpp create mode 100644 13098.cpp create mode 100644 13099.cpp create mode 100644 131.cpp create mode 100644 1310.cpp create mode 100644 13100.cpp create mode 100644 13101.cpp create mode 100644 13103.cpp create mode 100644 13104.cpp create mode 100644 13105.cpp create mode 100644 13106.cpp create mode 100644 13107.cpp create mode 100644 13108.cpp create mode 100644 13109.cpp create mode 100644 13110.cpp create mode 100644 13111.cpp create mode 100644 13112.cpp create mode 100644 13113.cpp create mode 100644 13114.cpp create mode 100644 13115.cpp create mode 100644 13116.cpp create mode 100644 13117.cpp create mode 100644 13118.cpp create mode 100644 13119.cpp create mode 100644 1312.cpp create mode 100644 13121.cpp create mode 100644 13122.cpp create mode 100644 13123.cpp create mode 100644 13124.cpp create mode 100644 13125.cpp create mode 100644 13126.cpp create mode 100644 13127.cpp create mode 100644 13128.cpp create mode 100644 13129.cpp create mode 100644 13130.cpp create mode 100644 13131.cpp create mode 100644 13132.cpp create mode 100644 13133.cpp create mode 100644 13134.cpp create mode 100644 13135.cpp create mode 100644 13136.cpp create mode 100644 13137.cpp create mode 100644 1314.cpp create mode 100644 13140.cpp create mode 100644 13141.cpp create mode 100644 13142.cpp create mode 100644 13144.cpp create mode 100644 13145.cpp create mode 100644 13146.cpp create mode 100644 13147.cpp create mode 100644 13148.cpp create mode 100644 13149.cpp create mode 100644 1315.cpp create mode 100644 13154.cpp create mode 100644 1316.cpp create mode 100644 13161.cpp create mode 100644 1317.cpp create mode 100644 1319.cpp create mode 100644 132.cpp create mode 100644 1322.cpp create mode 100644 1324.cpp create mode 100644 1325.cpp create mode 100644 1326.cpp create mode 100644 1327.cpp create mode 100644 1328.cpp create mode 100644 1329.cpp create mode 100644 133.cpp create mode 100644 1330.cpp create mode 100644 1331.cpp create mode 100644 1333.cpp create mode 100644 1335.cpp create mode 100644 1336.cpp create mode 100644 1337.cpp create mode 100644 1339.cpp create mode 100644 134.cpp create mode 100644 1340.cpp create mode 100644 1341.cpp create mode 100644 1342.cpp create mode 100644 1343.cpp create mode 100644 1344.cpp create mode 100644 1346.cpp create mode 100644 1347.cpp create mode 100644 1349.cpp create mode 100644 135.cpp create mode 100644 1350.cpp create mode 100644 1351.cpp create mode 100644 1352.cpp create mode 100644 1354.cpp create mode 100644 1356.cpp create mode 100644 1357.cpp create mode 100644 1358.cpp create mode 100644 1361.cpp create mode 100644 1362.cpp create mode 100644 1363.cpp create mode 100644 1364.cpp create mode 100644 1365.cpp create mode 100644 1366.cpp create mode 100644 1367.cpp create mode 100644 1368.cpp create mode 100644 137.cpp create mode 100644 1371.cpp create mode 100644 1372.cpp create mode 100644 1374.cpp create mode 100644 1376.cpp create mode 100644 1377.cpp create mode 100644 1378.cpp create mode 100644 1379.cpp create mode 100644 1380.cpp create mode 100644 1381.cpp create mode 100644 1382.cpp create mode 100644 1383.cpp create mode 100644 1385.cpp create mode 100644 1386.cpp create mode 100644 1388.cpp create mode 100644 139.cpp create mode 100644 1390.cpp create mode 100644 1391.cpp create mode 100644 1392.cpp create mode 100644 1393.cpp create mode 100644 1394.cpp create mode 100644 1395.cpp create mode 100644 1396.cpp create mode 100644 1397.cpp create mode 100644 1398.cpp create mode 100644 1399.cpp create mode 100644 140.cpp create mode 100644 1400.cpp create mode 100644 1401.cpp create mode 100644 1402.cpp create mode 100644 1404.cpp create mode 100644 1406.cpp create mode 100644 1407.cpp create mode 100644 1408.cpp create mode 100644 141.cpp create mode 100644 1411.cpp create mode 100644 1412.cpp create mode 100644 1413.cpp create mode 100644 1414.cpp create mode 100644 1415.cpp create mode 100644 1416.cpp create mode 100644 1417.cpp create mode 100644 1418.cpp create mode 100644 1419.cpp create mode 100644 142.cpp create mode 100644 1420.cpp create mode 100644 1421.cpp create mode 100644 1422.cpp create mode 100644 1423.cpp create mode 100644 1424.cpp create mode 100644 1425.cpp create mode 100644 1426.cpp create mode 100644 1427.cpp create mode 100644 1428.cpp create mode 100644 143.cpp create mode 100644 1431.cpp create mode 100644 1432.cpp create mode 100644 1433.cpp create mode 100644 1434.cpp create mode 100644 1435.cpp create mode 100644 1436.cpp create mode 100644 1437.cpp create mode 100644 1438.cpp create mode 100644 1439.cpp create mode 100644 144.cpp create mode 100644 1440.cpp create mode 100644 1442.cpp create mode 100644 1443.cpp create mode 100644 1445.cpp create mode 100644 1449.cpp create mode 100644 145.cpp create mode 100644 1450.cpp create mode 100644 1451.cpp create mode 100644 1452.cpp create mode 100644 1453.cpp create mode 100644 1455.cpp create mode 100644 1456.cpp create mode 100644 1462.cpp create mode 100644 1464.cpp create mode 100644 1466.cpp create mode 100644 1467.cpp create mode 100644 1468.cpp create mode 100644 1469.cpp create mode 100644 1470.cpp create mode 100644 1471.cpp create mode 100644 1473.cpp create mode 100644 1474.cpp create mode 100644 1475.cpp create mode 100644 1476.cpp create mode 100644 1478.cpp create mode 100644 1479.cpp create mode 100644 148.cpp create mode 100644 1480.cpp create mode 100644 1481.cpp create mode 100644 1482.cpp create mode 100644 1484.cpp create mode 100644 1485.cpp create mode 100644 1486.cpp create mode 100644 1487.cpp create mode 100644 1488.cpp create mode 100644 1489.cpp create mode 100644 149.cpp create mode 100644 1492.cpp create mode 100644 1493.cpp create mode 100644 1494.cpp create mode 100644 1498.cpp create mode 100644 150.cpp create mode 100644 1501.cpp create mode 100644 1502.cpp create mode 100644 1508.cpp create mode 100644 1509.cpp create mode 100644 1510.cpp create mode 100644 1511.cpp create mode 100644 1513.cpp create mode 100644 1514.cpp create mode 100644 1515.cpp create mode 100644 1516.cpp create mode 100644 1517.cpp create mode 100644 1518.cpp create mode 100644 1519.cpp create mode 100644 1521.cpp create mode 100644 1523.cpp create mode 100644 1524.cpp create mode 100644 1525.cpp create mode 100644 1526.cpp create mode 100644 1529.cpp create mode 100644 1530.cpp create mode 100644 1531.cpp create mode 100644 1533.cpp create mode 100644 1534.cpp create mode 100644 1537.cpp create mode 100644 154.cpp create mode 100644 1541.cpp create mode 100644 1543.cpp create mode 100644 1544.cpp create mode 100644 1546.cpp create mode 100644 1548.cpp create mode 100644 1549.cpp create mode 100644 155.cpp create mode 100644 1554.cpp create mode 100644 1555.cpp create mode 100644 1556.cpp create mode 100644 1557.cpp create mode 100644 1558.cpp create mode 100644 1559.cpp create mode 100644 1560.cpp create mode 100644 1564.cpp create mode 100644 1566.cpp create mode 100644 1569.cpp create mode 100644 157.cpp create mode 100644 1571.cpp create mode 100644 1572.cpp create mode 100644 1574.cpp create mode 100644 1575.cpp create mode 100644 1577.cpp create mode 100644 1579.cpp create mode 100644 158.cpp create mode 100644 1580.cpp create mode 100644 1581.cpp create mode 100644 1583.cpp create mode 100644 1584.cpp create mode 100644 1585.cpp create mode 100644 1586.cpp create mode 100644 1587.cpp create mode 100644 1588.cpp create mode 100644 1589.cpp create mode 100644 159.cpp create mode 100644 1590.cpp create mode 100644 1591.cpp create mode 100644 1592.cpp create mode 100644 1593.cpp create mode 100644 1594.cpp create mode 100644 1595.cpp create mode 100644 1596.cpp create mode 100644 1597.cpp create mode 100644 1598.cpp create mode 100644 1599.cpp create mode 100644 1600.cpp create mode 100644 1601.cpp create mode 100644 1602.cpp create mode 100644 1603.cpp create mode 100644 1604.cpp create mode 100644 1605.cpp create mode 100644 1606.cpp create mode 100644 1607.cpp create mode 100644 1608.cpp create mode 100644 1609.cpp create mode 100644 1610.cpp create mode 100644 1611.cpp create mode 100644 1612.cpp create mode 100644 1613.cpp create mode 100644 1614.cpp create mode 100644 1615.cpp create mode 100644 1616.cpp create mode 100644 1617.cpp create mode 100644 1618.cpp create mode 100644 1619.cpp create mode 100644 162.cpp create mode 100644 1620.cpp create mode 100644 1623.cpp create mode 100644 1624.cpp create mode 100644 1625.cpp create mode 100644 1626.cpp create mode 100644 1627.cpp create mode 100644 1628.cpp create mode 100644 1629.cpp create mode 100644 1630.cpp create mode 100644 1631.cpp create mode 100644 1632.cpp create mode 100644 1633.cpp create mode 100644 1634.cpp create mode 100644 1635.cpp create mode 100644 1636.cpp create mode 100644 1637.cpp create mode 100644 1638.cpp create mode 100644 1639.cpp create mode 100644 164.cpp create mode 100644 1640.cpp create mode 100644 1641.cpp create mode 100644 1642.cpp create mode 100644 1643.cpp create mode 100644 1644.cpp create mode 100644 1645.cpp create mode 100644 1646.cpp create mode 100644 1647.cpp create mode 100644 1648.cpp create mode 100644 1649.cpp create mode 100644 165.cpp create mode 100644 1650.cpp create mode 100644 1653.cpp create mode 100644 1654.cpp create mode 100644 1658.cpp create mode 100644 1659.cpp create mode 100644 166.cpp create mode 100644 1660.cpp create mode 100644 1661.cpp create mode 100644 1662.cpp create mode 100644 1663.cpp create mode 100644 1664.cpp create mode 100644 1665.cpp create mode 100644 1666.cpp create mode 100644 1667.cpp create mode 100644 1668.cpp create mode 100644 1669.cpp create mode 100644 167.cpp create mode 100644 1671.cpp create mode 100644 1673.cpp create mode 100644 1674.cpp create mode 100644 1676.cpp create mode 100644 168.cpp create mode 100644 1683.cpp create mode 100644 1684.cpp create mode 100644 1687.cpp create mode 100644 169.cpp create mode 100644 170.cpp create mode 100644 1709.cpp create mode 100644 171.cpp create mode 100644 1711.cpp create mode 100644 1712.cpp create mode 100644 1714.cpp create mode 100644 1718.cpp create mode 100644 172.cpp create mode 100644 1726.cpp create mode 100644 1727.cpp create mode 100644 1729.cpp create mode 100644 173.cpp create mode 100644 1730.cpp create mode 100644 174.cpp create mode 100644 175.cpp create mode 100644 176.cpp create mode 100644 177.cpp create mode 100644 178.cpp create mode 100644 179.cpp create mode 100644 180.cpp create mode 100644 181.cpp create mode 100644 182.cpp create mode 100644 183.cpp create mode 100644 184.cpp create mode 100644 185.cpp create mode 100644 186.cpp create mode 100644 187.cpp create mode 100644 188.cpp create mode 100644 189.cpp create mode 100644 190.cpp create mode 100644 191.cpp create mode 100644 192.cpp create mode 100644 193.cpp create mode 100644 194.cpp create mode 100644 196.cpp create mode 100644 197.cpp create mode 100644 198.cpp create mode 100644 199.cpp create mode 100644 200.cpp create mode 100644 201.cpp create mode 100644 202.cpp create mode 100644 205.cpp create mode 100644 206.cpp create mode 100644 207.cpp create mode 100644 208.cpp create mode 100644 209.cpp create mode 100644 210.cpp create mode 100644 211.cpp create mode 100644 212.cpp create mode 100644 213.cpp create mode 100644 214.cpp create mode 100644 215.cpp create mode 100644 216.cpp create mode 100644 217.cpp create mode 100644 218.cpp create mode 100644 219.cpp create mode 100644 220.cpp create mode 100644 221.cpp create mode 100644 222.cpp create mode 100644 223.cpp create mode 100644 225.cpp create mode 100644 229.cpp create mode 100644 230.cpp create mode 100644 232.cpp create mode 100644 233.cpp create mode 100644 234.cpp create mode 100644 238.cpp create mode 100644 239.cpp create mode 100644 240.cpp create mode 100644 242.cpp create mode 100644 243.cpp create mode 100644 244.cpp create mode 100644 245.cpp create mode 100644 246.cpp create mode 100644 247.cpp create mode 100644 248.cpp create mode 100644 250.cpp create mode 100644 251.cpp create mode 100644 253.cpp create mode 100644 254.cpp create mode 100644 255.cpp create mode 100644 257.cpp create mode 100644 258.cpp create mode 100644 259.cpp create mode 100644 261.cpp create mode 100644 265.cpp create mode 100644 267.cpp create mode 100644 268.cpp create mode 100644 269.cpp create mode 100644 270.cpp create mode 100644 273.cpp create mode 100644 274.cpp create mode 100644 275.cpp create mode 100644 276.cpp create mode 100644 277.cpp create mode 100644 278.cpp create mode 100644 279.cpp create mode 100644 280.cpp create mode 100644 282.cpp create mode 100644 283.cpp create mode 100644 284.cpp create mode 100644 286.cpp create mode 100644 288.cpp create mode 100644 290.cpp create mode 100644 293.cpp create mode 100644 295.cpp create mode 100644 296.cpp create mode 100644 297.cpp create mode 100644 298.cpp create mode 100644 300.cpp create mode 100644 301.cpp create mode 100644 302.cpp create mode 100644 303.cpp create mode 100644 305.cpp create mode 100644 306.cpp create mode 100644 307.cpp create mode 100644 308.cpp create mode 100644 309.cpp create mode 100644 310.cpp create mode 100644 311.cpp create mode 100644 312.cpp create mode 100644 313.cpp create mode 100644 314.cpp create mode 100644 315.cpp create mode 100644 317.cpp create mode 100644 318.cpp create mode 100644 319.cpp create mode 100644 320.cpp create mode 100644 321.cpp create mode 100644 322.cpp create mode 100644 323.cpp create mode 100644 324.cpp create mode 100644 325.cpp create mode 100644 326.cpp create mode 100644 327.cpp create mode 100644 328.cpp create mode 100644 330.cpp create mode 100644 331.cpp create mode 100644 332.cpp create mode 100644 333.cpp create mode 100644 334.cpp create mode 100644 335.cpp create mode 100644 337.cpp create mode 100644 338.cpp create mode 100644 339.cpp create mode 100644 341.cpp create mode 100644 342.cpp create mode 100644 345.cpp create mode 100644 346.cpp create mode 100644 347.cpp create mode 100644 348.cpp create mode 100644 349.cpp create mode 100644 350.cpp create mode 100644 354.cpp create mode 100644 355.cpp create mode 100644 356.cpp create mode 100644 357.cpp create mode 100644 358.cpp create mode 100644 359.cpp create mode 100644 361.cpp create mode 100644 362.cpp create mode 100644 366.cpp create mode 100644 370.cpp create mode 100644 372.cpp create mode 100644 373.cpp create mode 100644 375.cpp create mode 100644 376.cpp create mode 100644 377.cpp create mode 100644 379.cpp create mode 100644 380.cpp create mode 100644 381.cpp create mode 100644 384.cpp create mode 100644 385.cpp create mode 100644 387.cpp create mode 100644 388.cpp create mode 100644 390.cpp create mode 100644 391.cpp create mode 100644 392.cpp create mode 100644 393.cpp create mode 100644 394.cpp create mode 100644 395.cpp create mode 100644 397.cpp create mode 100644 398.cpp create mode 100644 399.cpp create mode 100644 400.cpp create mode 100644 402.cpp create mode 100644 403.cpp create mode 100644 407.cpp create mode 100644 410.cpp create mode 100644 411.cpp create mode 100644 413.cpp create mode 100644 415.cpp create mode 100644 416.cpp create mode 100644 418.cpp create mode 100644 419.cpp create mode 100644 420.cpp create mode 100644 421.cpp create mode 100644 422.cpp create mode 100644 424.cpp create mode 100644 425.cpp create mode 100644 426.cpp create mode 100644 427.cpp create mode 100644 428.cpp create mode 100644 430.cpp create mode 100644 432.cpp create mode 100644 433.cpp create mode 100644 434.cpp create mode 100644 435.cpp create mode 100644 436.cpp create mode 100644 437.cpp create mode 100644 438.cpp create mode 100644 439.cpp create mode 100644 440.cpp create mode 100644 441.cpp create mode 100644 442.cpp create mode 100644 444.cpp create mode 100644 446.cpp create mode 100644 447.cpp create mode 100644 448.cpp create mode 100644 449.cpp create mode 100644 450.cpp create mode 100644 451.cpp create mode 100644 452.cpp create mode 100644 453.cpp create mode 100644 456.cpp create mode 100644 460.cpp create mode 100644 462.cpp create mode 100644 464.cpp create mode 100644 465.cpp create mode 100644 466.cpp create mode 100644 467.cpp create mode 100644 468.cpp create mode 100644 471.cpp create mode 100644 472.cpp create mode 100644 473.cpp create mode 100644 474.cpp create mode 100644 475.cpp create mode 100644 476.cpp create mode 100644 477.cpp create mode 100644 478.cpp create mode 100644 483.cpp create mode 100644 485.cpp create mode 100644 486.cpp create mode 100644 487.cpp create mode 100644 489.cpp create mode 100644 490.cpp create mode 100644 491.cpp create mode 100644 493.cpp create mode 100644 494.cpp create mode 100644 498.cpp create mode 100644 499.cpp create mode 100644 500.cpp create mode 100644 501.cpp create mode 100644 502.cpp create mode 100644 503.cpp create mode 100644 505.cpp create mode 100644 506.cpp create mode 100644 508.cpp create mode 100644 509.cpp create mode 100644 511.cpp create mode 100644 512.cpp create mode 100644 515.cpp create mode 100644 517.cpp create mode 100644 518.cpp create mode 100644 519.cpp create mode 100644 520.cpp create mode 100644 521.cpp create mode 100644 523.cpp create mode 100644 524.cpp create mode 100644 526.cpp create mode 100644 527.cpp create mode 100644 529.cpp create mode 100644 533.cpp create mode 100644 536.cpp create mode 100644 538.cpp create mode 100644 539.cpp create mode 100644 540.cpp create mode 100644 542.cpp create mode 100644 544.cpp create mode 100644 545.cpp create mode 100644 546.cpp create mode 100644 547.cpp create mode 100644 548.cpp create mode 100644 549.cpp create mode 100644 550.cpp create mode 100644 551.cpp create mode 100644 552.cpp create mode 100644 554.cpp create mode 100644 555.cpp create mode 100644 556.cpp create mode 100644 557.cpp create mode 100644 558.cpp create mode 100644 559.cpp create mode 100644 560.cpp create mode 100644 561.cpp create mode 100644 563.cpp create mode 100644 565.cpp create mode 100644 566.cpp create mode 100644 567.cpp create mode 100644 568.cpp create mode 100644 570.cpp create mode 100644 571.cpp create mode 100644 574.cpp create mode 100644 576.cpp create mode 100644 577.cpp create mode 100644 578.cpp create mode 100644 580.cpp create mode 100644 581.cpp create mode 100644 584.cpp create mode 100644 585.cpp create mode 100644 586.cpp create mode 100644 587.cpp create mode 100644 588.cpp create mode 100644 589.cpp create mode 100644 590.cpp create mode 100644 591.cpp create mode 100644 592.cpp create mode 100644 593.cpp create mode 100644 595.cpp create mode 100644 596.cpp create mode 100644 598.cpp create mode 100644 599.cpp create mode 100644 601.cpp create mode 100644 602.cpp create mode 100644 603.cpp create mode 100644 604.cpp create mode 100644 606.cpp create mode 100644 607.cpp create mode 100644 608.cpp create mode 100644 609.cpp create mode 100644 610.cpp create mode 100644 613.cpp create mode 100644 614.cpp create mode 100644 615.cpp create mode 100644 616.cpp create mode 100644 617.cpp create mode 100644 618.cpp create mode 100644 619.cpp create mode 100644 620.cpp create mode 100644 622.cpp create mode 100644 623.cpp create mode 100644 624.cpp create mode 100644 625.cpp create mode 100644 626.cpp create mode 100644 628.cpp create mode 100644 629.cpp create mode 100644 632.cpp create mode 100644 633.cpp create mode 100644 634.cpp create mode 100644 635.cpp create mode 100644 638.cpp create mode 100644 639.cpp create mode 100644 641.cpp create mode 100644 643.cpp create mode 100644 645.cpp create mode 100644 647.cpp create mode 100644 648.cpp create mode 100644 649.cpp create mode 100644 650.cpp create mode 100644 651.cpp create mode 100644 652.cpp create mode 100644 653.cpp create mode 100644 654.cpp create mode 100644 656.cpp create mode 100644 657.cpp create mode 100644 658.cpp create mode 100644 659.cpp create mode 100644 660.cpp create mode 100644 662.cpp create mode 100644 663.cpp create mode 100644 664.cpp create mode 100644 665.cpp create mode 100644 667.cpp create mode 100644 668.cpp create mode 100644 669.cpp create mode 100644 670.cpp create mode 100644 671.cpp create mode 100644 672.cpp create mode 100644 674.cpp create mode 100644 675.cpp create mode 100644 676.cpp create mode 100644 677.cpp create mode 100644 679.cpp create mode 100644 680.cpp create mode 100644 681.cpp create mode 100644 682.cpp create mode 100644 683.cpp create mode 100644 684.cpp create mode 100644 685.cpp create mode 100644 687.cpp create mode 100644 688.cpp create mode 100644 689.cpp create mode 100644 690.cpp create mode 100644 696.cpp create mode 100644 697.cpp create mode 100644 698.cpp create mode 100644 699.cpp create mode 100644 700.cpp create mode 100644 701.cpp create mode 100644 702.cpp create mode 100644 703.cpp create mode 100644 704.cpp create mode 100644 705.cpp create mode 100644 706.cpp create mode 100644 707.cpp create mode 100644 708.cpp create mode 100644 709.cpp create mode 100644 710.cpp create mode 100644 711.cpp create mode 100644 712.cpp create mode 100644 713.cpp create mode 100644 714.cpp create mode 100644 715.cpp create mode 100644 716.cpp create mode 100644 718.cpp create mode 100644 719.cpp create mode 100644 721.cpp create mode 100644 722.cpp create mode 100644 725.cpp create mode 100644 726.cpp create mode 100644 727.cpp create mode 100644 732.cpp create mode 100644 734.cpp create mode 100644 735.cpp create mode 100644 736.cpp create mode 100644 737.cpp create mode 100644 738.cpp create mode 100644 739.cpp create mode 100644 740.cpp create mode 100644 741.cpp create mode 100644 742.cpp create mode 100644 743.cpp create mode 100644 745.cpp create mode 100644 746.cpp create mode 100644 748.cpp create mode 100644 750.cpp create mode 100644 751.cpp create mode 100644 752.cpp create mode 100644 753.cpp create mode 100644 754.cpp create mode 100644 756.cpp create mode 100644 757.cpp create mode 100644 758.cpp create mode 100644 759.cpp create mode 100644 760.cpp create mode 100644 763.cpp create mode 100644 766.cpp create mode 100644 769.cpp create mode 100644 775.cpp create mode 100644 782.cpp create mode 100644 783.cpp create mode 100644 784.cpp create mode 100644 785.cpp create mode 100644 787.cpp create mode 100644 790.cpp create mode 100644 793.cpp create mode 100644 795.cpp create mode 100644 796.cpp create mode 100644 798.cpp create mode 100644 799.cpp create mode 100644 801.cpp create mode 100644 802.cpp create mode 100644 803.cpp create mode 100644 804.cpp create mode 100644 805.cpp create mode 100644 806.cpp create mode 100644 807.cpp create mode 100644 808.cpp create mode 100644 810.cpp create mode 100644 811.cpp create mode 100644 812.cpp create mode 100644 813.cpp create mode 100644 814.cpp create mode 100644 815.cpp create mode 100644 816.cpp create mode 100644 817.cpp create mode 100644 818.cpp create mode 100644 819.cpp create mode 100644 820.cpp create mode 100644 822.cpp create mode 100644 823.cpp create mode 100644 824.cpp create mode 100644 825.cpp create mode 100644 828.cpp create mode 100644 830.cpp create mode 100644 832.cpp create mode 100644 833.cpp create mode 100644 834.cpp create mode 100644 835.cpp create mode 100644 837.cpp create mode 100644 838.cpp create mode 100644 839.cpp create mode 100644 840.cpp create mode 100644 841.cpp create mode 100644 843.cpp create mode 100644 844.cpp create mode 100644 845.cpp create mode 100644 846.cpp create mode 100644 848.cpp create mode 100644 850.cpp create mode 100644 851.cpp create mode 100644 852.cpp create mode 100644 856.cpp create mode 100644 857.cpp create mode 100644 858.cpp create mode 100644 859.cpp create mode 100644 860.cpp create mode 100644 861.cpp create mode 100644 862.cpp create mode 100644 864.cpp create mode 100644 865.cpp create mode 100644 866.cpp create mode 100644 868.cpp create mode 100644 870.cpp create mode 100644 871.cpp create mode 100644 872.cpp create mode 100644 873.cpp create mode 100644 874.cpp create mode 100644 877.cpp create mode 100644 878.cpp create mode 100644 879.cpp create mode 100644 881.cpp create mode 100644 882.cpp create mode 100644 883.cpp create mode 100644 884.cpp create mode 100644 886.cpp create mode 100644 890.cpp create mode 100644 891.cpp create mode 100644 892.cpp create mode 100644 893.cpp create mode 100644 895.cpp create mode 100644 897.cpp create mode 100644 898.cpp create mode 100644 899.cpp create mode 100644 903.cpp create mode 100644 904.cpp create mode 100644 905.cpp create mode 100644 906.cpp create mode 100644 907.cpp create mode 100644 908.cpp create mode 100644 909.cpp create mode 100644 910.cpp create mode 100644 911.cpp create mode 100644 912.cpp create mode 100644 914.cpp create mode 100644 915.cpp create mode 100644 917.cpp create mode 100644 918.cpp create mode 100644 920.cpp create mode 100644 921.cpp create mode 100644 922.cpp create mode 100644 924.cpp create mode 100644 925.cpp create mode 100644 926.cpp create mode 100644 927.cpp create mode 100644 928.cpp create mode 100644 929.cpp create mode 100644 930.cpp create mode 100644 932.cpp create mode 100644 933.cpp create mode 100644 938.cpp create mode 100644 939.cpp create mode 100644 942.cpp create mode 100644 944.cpp create mode 100644 945.cpp create mode 100644 946.cpp create mode 100644 947.cpp create mode 100644 948.cpp create mode 100644 949.cpp create mode 100644 953.cpp create mode 100644 954.cpp create mode 100644 956.cpp create mode 100644 957.cpp create mode 100644 960.cpp create mode 100644 962.cpp create mode 100644 963.cpp create mode 100644 964.cpp create mode 100644 969.cpp create mode 100644 970.cpp create mode 100644 972.cpp create mode 100644 973.cpp create mode 100644 976.cpp create mode 100644 978.cpp create mode 100644 979.cpp create mode 100644 982.cpp create mode 100644 983.cpp create mode 100644 985.cpp create mode 100644 986.cpp create mode 100644 988.cpp create mode 100644 989.cpp create mode 100644 990.cpp create mode 100644 993.cpp create mode 100644 995.cpp create mode 100644 996.cpp create mode 100644 997.cpp create mode 100644 999.cpp diff --git a/100.cpp b/100.cpp index 4095cd3..cb97460 100644 --- a/100.cpp +++ b/100.cpp @@ -1,24 +1,62 @@ -#include -int main(){ - int i,j; - while(scanf("%d%d",&i,&j)!=EOF){ - int x=0,y=0,k=0,max=0; - x=(i>j)?i:j; - y=(imax) - max=nc; - } - printf("%d %d %d\n",i,j,max+1); - } - return 0; -} +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +#define SIZE 1000001 + +static unsigned short tbl[SIZE]; + +inline unsigned short cycle_len(register unsigned long n) +{ + if (n < SIZE && tbl[n]) + { + return tbl[n]; + } + if (n & 1) + { + // odd n + if (n < SIZE) + { + // calc two steps at once + tbl[n] = 2 + cycle_len((3 * n + 1) >> 1); + return tbl[n]; + } + else + { + return 2 + cycle_len((3 * n + 1) >> 1); + } + } + else + { + if (n < SIZE) + { + tbl[n] = 1 + cycle_len(n >> 1);// n/2 + return tbl[n]; + } + else + { + return 1 + cycle_len(n >> 1); + } + } +} + +int main() +{ + unsigned long fn = 0, sn = 0, i; + short out = 0, temp; + tbl[1] = 1; + while (scanf("%lu%lu", &fn, &sn) == 2) + { + unsigned long a = min(fn, sn), b = max(fn, sn); + for (int i = a; i <= b; i++) + { + out = max(out, cycle_len(i)); + } + printf("%lu %lu %d\n", fn, sn, out); + out = 0; + } + return 0; +} diff --git a/1000.cpp b/1000.cpp new file mode 100644 index 0000000..91429a3 --- /dev/null +++ b/1000.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +typedef struct +{ + int NO, ac[30], dc[30], w; +} Cas; + +Cas cas[21]; +int len; +int N, K, Ar; +int arr[30][30]; + +int cmp(const void *a, const void *b) +{ + Cas *pa = (Cas *)a, *pb = (Cas *)b; + if (pa->w == pb->w) + { + return pa->NO - pb->NO; + } + else + { + return pa->w - pb->w; + } +} + +int main() +{ + int i, j, k, D, flow; + while (scanf("%d", &N), N != 0) + { + memset(arr, 0, sizeof(arr)); + memset(cas, 0, sizeof(cas)); + len = 0; + for (i = 0; i < N; i++) + { + scanf("%d%d", &Ar, &K); + for (j = 0; j < K; j++) + { + scanf("%d%d", &D, &flow); + arr[Ar][D] = flow; + } + } + while (scanf("%d", &K), K != 0) + { + cas[len].NO = K; + cas[len].w = 0; + for (i = 0; i < N; i++) + { + scanf("%d", &cas[len].ac[i]); + } + for (i = 0; i < N; i++) + { + scanf("%d", &cas[len].dc[i]); + } + cas[len].w = 0; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + cas[len].w += arr[cas[len].ac[i]][cas[len].dc[j]] * (1 + abs(i - j)); + } + } + len++; + } + qsort(cas, len, sizeof(cas[0]), cmp); + printf("Configuration Load\n"); + for (i = 0; i < len; i++) + { + printf("%5d %d\n", cas[i].NO, cas[i].w); + } + } + return 0; +} diff --git a/10000.cpp b/10000.cpp new file mode 100644 index 0000000..d55ee97 --- /dev/null +++ b/10000.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) +#define SZ(a) ((int)(a).size()) + +typedef struct _node +{ + int n, length; + vector adj; +} node; + +static vector G; +static pair result; +static int n, start, a, b, counter = 0; + +void dfs(int start, int length) +{ + if (G[start - 1].length > length) + { + return; + } + else if (G[start - 1].length < length) + { + G[start - 1].length = length; + } + if (G[start - 1].adj.empty()) + { + if (length > result.first) + { + result.first = length; + result.second = start; + } + else if (length == result.first) + { + if (start < result.second) + { + result.second = start; + } + } + } + else + { + int end = SZ(G[start - 1].adj); + FOR(i, 0, end) + { + dfs(G[start - 1].adj[i], length + 1); + } + } +} + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + G.clear(); + counter++; + FOREQ(i, 1, n) + { + node N; + N.n = i; + N.length = 0; + G.push_back(N); + } + scanf("%d", &start); + while (scanf("%d%d", &a, &b) && a && b) + { + G[a - 1].adj.push_back(b); + } + result.first = 0; + result.second = 0; + dfs(start, 0); + printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n", + counter, start, result.first, result.second); + } + return 0; +} diff --git a/10001.cpp b/10001.cpp new file mode 100644 index 0000000..ba3b47c --- /dev/null +++ b/10001.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +int automaton[2][2][2], state[40], map_[160][160], reachable[160], n, maxn; + +void init_automaton(int identifier) +{ + memset(automaton, 0, sizeof(automaton)); + for (int i = 0; i <= 1; ++i) + { + for (int j = 0; j <= 1; ++j) + { + for (int k = 0; k <= 1; ++k) + { + automaton[i][j][k] = identifier & 1; + identifier >>= 1; + } + } + } +} + +void init_state(char str_state[]) +{ + memset(state, 0, sizeof(state)); + for (int i = 0; i < strlen(str_state); ++i) + { + state[i] = str_state[i] - '0'; + } +} + +void add_edge(int id1, int cur1, int next1, int id2, int cur2, int next2) +{ + int node1 = id1 * 4 + cur1 * 2 + next1; + int node2 = id2 * 4 + cur2 * 2 + next2; + map_[node1][node2] = 1; +} + +void init_map_(void) +{ + memset(map_, 0, sizeof(map_)); + maxn = n * 4; + for (int id = 0; id < n; ++id) + { + for (int cur = 0; cur <= 1; ++cur) + { + for (int next = 0; next <= 1; ++next) + { + for (int next2 = 0; next2 <= 1; ++next2) + { + int next_id = (id + 1) % n; + if (state[next_id] == automaton[cur][next][next2]) + { + add_edge(id, cur, next, next_id, next, next2); + } + } + } + } + } +} + +int calc_state() +{ + for (int first = 0; first < 4; ++first) + { + memset(reachable, 0, sizeof(reachable)); + reachable[first] = 1; + for (int id = 0; id < n - 1; ++id) + { + for (int i = 0; i < 4; ++i) + { + for (int j = 0; j < 4; ++j) + { + if (reachable[id * 4 + i] && map_[id * 4 + i][(id + 1) * 4 + j]) + { + reachable[(id + 1) * 4 + j] = 1; + if (id == n - 2) + { + if (map_[(id + 1) * 4 + j][first]) + { + return 1; + } + } + } + } + } + } + } + return 0; +} + +int main() +{ + int identifier, is_reachable; + char str_state[1000]; + while (scanf("%d%d%s", &identifier, &n, str_state) != EOF) + { + init_automaton(identifier); + init_state(str_state); + init_map_(); + is_reachable = calc_state(); + printf(is_reachable ? "REACHABLE\n" : "GARDEN OF EDEN\n"); + } + return 0; +} diff --git a/10002.cpp b/10002.cpp new file mode 100644 index 0000000..53ede6e --- /dev/null +++ b/10002.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) + +const int MAX_POINT = 101; +struct Point +{ + double x, y; +}; + +inline double crsProd(const Point &start1, const Point &end1, + const Point &start2, const Point &end2) +{ + return (end1.x - start1.x) * (end2.y - start2.y) - (end2.x - start2.x) * (end1.y - start1.y); +} +inline double vecLenSqr(const Point &start, const Point &end) +{ + double x1 = start.x, y1 = start.y; + double x2 = end.x, y2 = end.y; + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); +} +class Polygon +{ +public: + Point m_point[MAX_POINT]; + int m_pntCnt; + void addPoint(const Point &p) + { + m_point[m_pntCnt] = p; + m_pntCnt++; + } + //Find convex hull, sorted in counter clockwise order + void getConvexHull(const Point *const points, const int pntCnt) + { + init(); + //Find the lowest and leftest point + int leftLow = 0; + FOR(i, 1, pntCnt) + { + if (points[i].y < points[leftLow].y && points[i].x < points[leftLow].x) + { + leftLow = i; + } + } + bool used[MAX_POINT]; + memset(used, 0, sizeof(used)); + int next = leftLow; + do + { + int current = next; + addPoint(points[current]); + used[current] = true; + next = leftLow; + FOR(i, 0, pntCnt) + { + if (!used[i]) + { + double crossP; + crossP = crsProd(points[current], points[i], + points[current], points[next]); + if (crossP > 0) + { + next = i; + } + else if (crossP == 0 && vecLenSqr(points[current], points[i]) > vecLenSqr(points[current], points[next])) + { + next = i; + } + } + } + } while (next != leftLow); + } + void init() + { + m_pntCnt = 0; + } + int pointCnt() const + { + return m_pntCnt; + } + Point point(int i) const + { + return m_point[i]; + } +}; + +int main() +{ + Point m_point[MAX_POINT]; + int m_pntCnt; + while (scanf("%d", &m_pntCnt) == 1 && m_pntCnt >= 3) + { + // input + for (int i = 0; i < m_pntCnt; i++) + { + scanf("%lf%lf", &m_point[i].x, &m_point[i].y); + } + // solve + Polygon convex; + convex.getConvexHull(m_point, m_pntCnt); + double A = 0.0; + Point center = {0.0, 0.0}; + for (int i = 2; i < convex.pointCnt(); i++) + { + double a = crsProd(convex.point(0), convex.point(i - 1), + convex.point(0), convex.point(i)); + Point c; + c.x = (convex.point(0).x + convex.point(i - 1).x + convex.point(i).x) / 3.0; + c.y = (convex.point(0).y + convex.point(i - 1).y + convex.point(i).y) / 3.0; + center.x = (center.x * A + c.x * a) / (a + A); + center.y = (center.y * A + c.y * a) / (a + A); + A += a; + } + printf("%.3lf %.3lf\n", center.x, center.y); + } + return 0; +} diff --git a/10003.cpp b/10003.cpp index a6c5135..f8eaef4 100644 --- a/10003.cpp +++ b/10003.cpp @@ -1,87 +1,53 @@ -#include -#include -#include -#include -using namespace std; +#include -#define INF 1<<30 -#define MAX 53 -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAXINT 9999999 -#define NCUTS 50 +using namespace std; -int L, N; -int C[MAX]; -int dp[MAX][MAX]; +#define INF 0x7fffffff +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) -int Cost(int J, int I) { - if (J - I <= 1) return 0; - return C[J] - C[I]; -} +static int n, j, i, l, k, length, num_of_cut, cutPos[55], table[55][55]; -int memo(int I, int J) { - if (I + 1 == J) return 0; - if (dp[I][J] != -1) return dp[I][J]; - - int gMin = INF, K; - - for (K = I + 1; K < J; K++) { - gMin = MIN(gMin, memo(I, K) + memo(K, J) + C[J] - C[I]); - } - return dp[I][J] = gMin; +void readData() +{ + scanf("%d", &num_of_cut); + FOREQ(i, 1, num_of_cut) + scanf("%d", &cutPos[i]); + cutPos[0] = 0; + cutPos[num_of_cut + 1] = length; } -void doIt() { - int i, j, k; - for (i = 0; i <= N; i++) { - for (j = 0; j <= N; j++) { - for (k = i + 1; k < j; k++) { - dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + Cost(j, i)); +void solve() +{ + n = num_of_cut + 1; + FOREQ(i, 0, n) + table[i][i] = 0; + FOREQ(l, 2, n) + { + FOREQ(i, 1, n - l + 1) + { + j = i + l - 1; + table[i][j] = INF; + FOR(k, i, j) + { + int temp; + temp = table[i][k] + table[k + 1][j] + cutPos[j] - cutPos[i - 1]; + if (temp < table[i][j]) + { + table[i][j] = temp; + } } } } } -int main() { - int len; - int nc; - int places[NCUTS+2]; - int mincost[NCUTS+2][NCUTS+2]; - - while(scanf("%d", &len) && (len != 0)) { - scanf("%d", &nc); - - for(int i=0; i + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) + +#define MAXN 202 + +struct SS {char color;} V[MAXN]; + +static char Link[MAXN][MAXN]; +static int a, b, s, N, E, Q[MAXN], QH, QT; + +inline void push(int n) +{ + Q[QH++] = n; + QH %= MAXN; +} +inline int pop() +{ + int n = Q[QT++]; + QT %= MAXN; + return n; +} +inline bool is_empty_() +{ + return QH == QT; +} + +inline int BFS(int s) +{ + int u; + char co; + V[s].color = 'w'; + QH = QT = 0; + push(s); + while (!is_empty_()) + { + u = pop(); + co = 'w'; + if (V[u].color == 'w') + { + co = 'b'; + } + FOR(i, 0, N) + { + if (Link[i][u] == 1) + { + if (V[i].color == 'g') + { + V[i].color = co; + Link[i][u] = Link[u][i] = 0; + push(i); + } + else if (V[i].color == V[u].color) + { + return 0; + } + } + } + } + return 1; +} + +int main() +{ + while (scanf("%d", &N) && N) + { + scanf("%d", &E); + FOR(i, 0, E) + { + scanf("%d%d", &a, &b); + Link[a][b] = Link[b][a] = 1; + s = a; + } + FOREQ(i, 0, N) + V[i].color = 'g'; + if (BFS(s)) + { + printf("BICOLORABLE.\n"); + } + else + { + printf("NOT BICOLORABLE.\n"); + } + FOR(i, 0, MAXN) + memset(Link[i], 0, 201 * sizeof(char)); + } + return 0; +} diff --git a/10005.cpp b/10005.cpp new file mode 100644 index 0000000..53f9404 --- /dev/null +++ b/10005.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +typedef double coord_t; +typedef double coord2_t; + +class Point +{ +public: + coord_t x, y; + Point() + { + x = 0; + y = 0; + } + Point(coord_t tx, coord_t ty) + { + x = tx; + y = ty; + } + bool operator<(const Point &p) const + { + return x < p.x || x == p.x && y < p.y; + } +}; + +double R; +int n; +vector points; + +inline coord2_t cross(const Point &O, const Point &A, const Point &B) +{ + return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); +} + +vector convex_hull(vector P) +{ + int n = P.size(), k = 0; + vector H(2 * n); + sort(P.begin(), P.end()); + for (int i = 0; i < n; ++i) + { + while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) + { + k--; + } + H[k++] = P[i]; + } + for (int i = n - 2, t = k + 1; i >= 0; --i)// The point with index n - 1 is already in H + { + while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) + { + k--; + } + H[k++] = P[i]; + } + H.resize(k - 1); + return H; +} + +int fsig(double x) +{ + if (fabs(x) < 1e-6) + { + return 0; + } + if (x > 0) + { + return 1; + } + else + { + return -1; + } +} + +int fcmp(double x, double y) +{ + return fsig(x - y); +} + +inline double sqr(double x) +{ + return x * x; +} + +inline double sqr_dis(Point a, Point b) +{ + return sqr(b.x - a.x) + sqr(b.y - a.y); +} + +int is_center(Point p) +{ + for (int i = 0; i < n; ++i) + { + if (fcmp(sqr_dis(points[i], p), R * R) > 0) + { + return 0; + } + } + return 1; +} + +int check(Point a, Point b) +{ + double x1 = a.x, x2 = b.x, y1 = a.y, y2 = b.y; + double q = sqrt(sqr_dis(a, b)); + if (fcmp(q * q, 4 * R * R) > 0) + { + return 0; + } + double x_part = sqrt(R * R - 0.25 * q * q) * (y1 - y2) / q; + double y_part = sqrt(R * R - 0.25 * q * q) * (x2 - x1) / q; + double x3 = 0.5 * (x1 + x2), y3 = 0.5 * (y1 + y2); + Point p1 = Point(x3 + x_part, y3 + y_part); + Point p2 = Point(x3 - x_part, y3 - y_part); + return is_center(p1) || is_center(p2); +} + +int is_satisfied(void) +{ + if (n == 1) + { + return 1; + } + else if (n == 2) + { + return sqr_dis(points[0], points[1]) <= 4 * R * R; + } + else + { + for (int i = 0; i < n; ++i) + { + for (int j = i + 1; j < n; ++j) + { + if (check(points[i], points[j])) + { + return 1; + } + } + } + return 0; + } +} + +int main() +{ + while (scanf("%d", &n)) + { + if (!n) + { + break; + } + points.clear(); + for (int i = 0, x, y; i < n; ++i) + { + scanf("%d%d", &x, &y); + points.push_back(Point(x, y)); + } + scanf("%lf", &R); + printf(is_satisfied() ? "The polygon can be packed in the circle.\n" : "There is no way of packing that polygon.\n"); + } + return 0; +} diff --git a/10006.cpp b/10006.cpp index 2213a5f..82d234d 100644 --- a/10006.cpp +++ b/10006.cpp @@ -1,25 +1,107 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int carmichel[]={561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341, 41041, 46657, 52633, 62745, 63973}; - for(;;){ - int n,ctr=0; - scanf("%d",&n); - if(n==0) - break; - for(int i=0;i<15;i++){ - if(carmichel[i]==n){ - ctr=-1; - break; - } - } - if(ctr<0) - printf("The number %d is a Carmichael number.\n",n); - else - printf("%d is normal.\n",n); - } - return 0; -} +#include + +using namespace std; + +#define MAXPRIME 65001 + +// mod exp, i.e. b^e mod m. +unsigned long long fast_mod_pow(unsigned long long b, unsigned long long e, unsigned long long m) +{ + unsigned long long r = 1; + while (e > 0) + { + if ((e & 1) == 1) + { + r = (r * b) % m; + } + e >>= 1; + b = (b * b) % m; + } + return r; +} + +// gen all prime ≤MAXPRIME, via sieve +void gen_primes(bool p[]) +{ + p[0] = p[1] = false; + + // starting at number 2 and going to the upper limit, mark + // all numbers as potential primes + for (int i = 2; i < MAXPRIME; i++) + { + p[i] = true; + } + int m = floor(sqrt(MAXPRIME)); + int n; + + // mark all multiples of a prime as non-primes. this has to be done for primes + // only up to the square root, since every number in the array has at least + // one factor smaller than the square root of the limit + for (int i = 2; i < m; i++) + { + if (p[i]) + { + n = MAXPRIME / i; + for (int j = 2; j <= n; j++) + { + p[i * j] = false; + } + } + } +} + +// gen all carmichael numbers ≤given limit by performing fermat tst +void gen_carmi(bool c[], bool p[]) +{ + // initialize carmichael numbers array with false + memset(c, 0, MAXPRIME * sizeof(bool)); + + // starting from the first non-prime, mark all + // odd numbers as potential carmichael numbers + for (int i = 9; i < MAXPRIME; i += 2) + { + c[i] = true; + } + + // again, for all odd numbers, we exclude the primes and perform + // the fermat test for 2 <= a <= n-1. + for (int n = 9; n < MAXPRIME; n += 2) + { + // VERY IMPORTANT! check first if this number is prime, otherwise TLE + if (p[n]) + { + c[n] = false; + continue; + } + for (int a = 2; a <= n - 1; a++) + { + if (fast_mod_pow(a, n, n) != a) + { + c[n] = false; + break; + } + } + } +} + +int main() +{ + unsigned long long n; // # + unsigned long long a; // a of fermat tst + bool prime[MAXPRIME]; // prime[] + bool carmi[MAXPRIME]; // carmichael[] + gen_primes(prime); + gen_carmi(carmi, prime); + while (cin >> n && (n != 0)) + { + if (carmi[n]) + { + printf("The number %llu is a Carmichael number.\n", n); + } + else + { + printf("%llu is normal.\n", n); + } + } + return 0; +} diff --git a/10007.cpp b/10007.cpp new file mode 100644 index 0000000..c3d31ca --- /dev/null +++ b/10007.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +#define limit 1000000000LL + +static int T[305][100] = {0}, len[305]; + +int main() +{ + int i, j, n; + long long int tmp; + T[0][0] = 1; + len[0] = 1; + for (i = 1; i < 305; i++) + { + len[i] = len[i - 1]; + for (j = 0; j < len[i]; j++) + { + tmp = (long long int)T[i - 1][j] * (4 * i - 2); + T[i][j] += tmp % limit; + T[i][j + 1] = tmp / limit; + } + if (T[i][len[i]]) + { + len[i]++; + } + } + for (i = 1; i < 305; i++) + { + tmp = 0LL; + for (j = len[i] - 1; j >= 0; j--) + { + tmp = tmp * limit + T[i][j]; + T[i][j] = (int)(tmp / (i + 1)); + tmp %= (i + 1); + } + if (!T[i][len[i] - 1]) + { + len[i]--; + } + } + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + printf("%d", T[n][len[n] - 1]); + for (i = len[n] - 2; i >= 0; i--) + { + printf("%09d", T[n][i]); + } + printf("\n"); + } + return 0; +} diff --git a/10008.cpp b/10008.cpp index ce13f83..c4c4336 100644 --- a/10008.cpp +++ b/10008.cpp @@ -1,66 +1,67 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +#define ALL(a) (a).begin(), (a).end() +#define SZ(a) ((int)(a).size()) -map Map; -map::iterator it; +#define fst first +#define snd second +#define pb push_back -bool comp(char A, char B) { - if (Map[A] != Map[B]) - return Map[A] > Map[B]; - return A < B; +inline bool by_val_desc(const pair &a, const pair &b) +{ + if (a.second == b.second) + { + return a.first < b.first; + } + else + { + return a.second > b.second; + } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string alfa = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - vector< char > Char; - int i, j, N; - FOI(i, 0, 25) Map[alfa[i]] = 0; - cin >> N; - string str; - getline(cin, str); - while (N--) { - getline(cin, str); - int L = str.length() - 1; - FOI(i, 0, L) { - if (isalpha(str[i])) { - char C = toupper(str[i]); - if (Map[C] == 0) Char.push_back(C); - ++Map[C]; +static int n; +static string input; +static map m; +static vector> v; + +int main() +{ + cin >> n; + getline(cin, input); // fetch $'\n' + while (n--) + { + getline(cin, input); + int y = SZ(input); + FOR(i, 0, y) + { + if (isalpha(input[i])) + { + if (m.find(toupper(input[i])) != m.end()) + { + m[toupper(input[i])]++; + } + else + { + m[toupper(input[i])] = 1; + } } } } - sort(Char.begin(), Char.end(), comp); - int S = Char.size() - 1; - FOI(i, 0, S) - cout << Char[i] << " " << Map[Char[i]] << endl; + FOREACH(m, it) + { + v.pb(pair(it->fst, it->snd)); + } + sort(ALL(v), by_val_desc); + int y = SZ(v); + FOR(i, 0, y) + { + printf("%c %d\n", v[i].fst, v[i].snd); + } return 0; } diff --git a/10009.cpp b/10009.cpp new file mode 100644 index 0000000..db0a4af --- /dev/null +++ b/10009.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) + +#define MAXN 27 +#define MAX 100 + +static char city1[100000], city2[100000], FL[MAXN][MAXN]; +static int QH, QT, Q[MAX]; + +struct Vertex {int pr; char cl;} V[MAXN]; + +inline void push(int n) +{ + Q[QH++] = n; + QH %= MAX; +} +inline int pop() +{ + int n = Q[QT++]; + QT %= MAX; + return n; +} +inline bool is_empty_() +{ + return QH == QT; +} + +void BFS(int st, int ter) +{ + int u; + FOR(i, 0, MAXN) + V[i].cl = 'W'; + V[st].cl = 'G'; + QH = QT = 0; + push(st); + while (!is_empty_()) + { + u = pop(); + FOR(i, 0, MAXN) + { + if (FL[u][i] && V[i].cl == 'W') + { + V[i].pr = u; + V[i].cl = 'G'; + if (ter == i) + { + return; + } + push(i); + } + } + } +} + +void print_path(int s, int t) +{ + if (s == t) + { + printf("%c", s + 'A'); + return; + } + else + { + print_path(s, V[t].pr); + } + printf("%c", t + 'A'); +} + +static int T, nt, q, a, b; + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &nt, &q); + FOR(i, 0, nt) + { + scanf("%s%s", city1, city2); + a = city1[0] - 'A'; + b = city2[0] - 'A'; + FL[b][a] = FL[a][b] = 1; + } + while (q--) + { + scanf("%s%s", city1, city2); + a = city1[0] - 'A'; + b = city2[0] - 'A'; + if (a == b) + { + continue; + } + BFS(a, b); + print_path(a, b); + printf("\n"); + } + if (T) + { + printf("\n"); + } + FOR(i, 0, MAXN) + { + FOR(j, 0, MAXN) + { + FL[i][j] = 0; + } + } + } + return 0; +} diff --git a/1001.cpp b/1001.cpp new file mode 100644 index 0000000..d3698ad --- /dev/null +++ b/1001.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const int maxn = 5000; +const int INF = 0x7fffffff; + +struct node +{ + int x; + int y; + int z; + int r; +} a[110]; +double dis(int i, int j) +{ + if (i == j) + { + return 0.0; + } + else + { + return sqrt(1.0 * (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].z - a[j].z) * (a[i].z - a[j].z)); + } +} + +int main() +{ + int n, kase = 1; + double road[110][110]; + double d[110][110]; + while (cin >> n) + { + for (int i = 0; i <= n + 1; i++) + { + for (int j = 0; j <= n + 1; j++) + { + if (i == j) + { + d[i][j] = 0; + } + else + { + d[i][j] = INF; + } + } + } + if (n < 0) + { + return 0; + } + for (int i = 1; i <= n; i++) + { + cin >> a[i].x >> a[i].y >> a[i].z >> a[i].r; + } + cin >> a[0].x >> a[0].y >> a[0].z; + a[0].r = a[n + 1].r = 0; + cin >> a[n + 1].x >> a[n + 1].y >> a[n + 1].z; + for (int i = 0; i <= n + 1; i++) + { + for (int j = 0; j <= n + 1; j++) + { + if (dis(i, j) - a[i].r - a[j].r > 0.0) + { + d[i][j] = dis(i, j) - a[i].r - a[j].r; + } + else + { + d[i][j] = 0.0; + } + } + } + for (int k = 0; k <= n + 1; k++) + { + for (int i = 0; i <= n + 1; i++) + { + for (int j = 0; j <= n + 1; j++) + { + d[i][j] = min(d[i][j], d[i][k] + d[k][j]); + } + } + } + int kk = int(d[0][n + 1] * 10 + 0.5 + 1e-7); + cout << "Cheese " << kase++ << ": Travel time = " << kk << " sec" << endl; + } + return 0; +} diff --git a/10010.cpp b/10010.cpp index 6e07684..986b200 100644 --- a/10010.cpp +++ b/10010.cpp @@ -1,85 +1,236 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +#define MAXN 10000 -string low(string S) { - int i, L = S.length() - 1; - FOI(i, 0, L) - S[i] = tolower(S[i]); - return S; -} +char GRID[MAXN][51]; +char WORD[55]; +int M, N, WW, R, C; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--) { - int M, N; - int i, j, k; - cin >> M >> N; - string str[M]; - FOI(i, 0, M-1) { - cin >> str[i]; - str[i] = low(str[i]); - } - int Q; - cin >> Q; - while (Q--) { - string S; - cin >> S; - S = low(S); - int L = S.length(); - bool flag = false; - FOI(i, 0, M-1) { - FOI(j, 0, N-1) { - string A = "", B = "", C = "", D = "", E = "", F = "", G = "", H = ""; - FOI(k, 0, L-1) { - if (j + k < N) A += str[i][j + k]; - if (i + k < M && j + k < N) B += str[i + k][j + k]; - if (i + k < M) C += str[i + k][j]; - if (i + k < M && j - k >= 0) D += str[i + k][j - k]; - if (j - k >= 0) E += str[i][j - k]; - if (i - k >= 0 && j - k >=0) F += str[i - k][j - k]; - if (i - k >= 0) G += str[i - k][j]; - if (i - k >= 0 && j + k < N) H += str[i - k][j + k]; - } - if (A == S || B == S || C == S || D == S || E == S || F == S || G == S || H == S) { - flag = true; - break; - } +int HF(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + if (WORD[i] != GRID[r][c++]) + { + return 0; + } + R = r; + C = c - 1; + return 1; +} +int HB(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (c < 0) + { + return 0; + } + if (WORD[i] != GRID[r][c--]) + { + return 0; + } + } + R = r; + C = c + 1; + return 1; +} +int VF(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r >= M) + { + return 0; + } + if (WORD[i] != GRID[r++][c]) + { + return 0; + } + } + R = r - 1; + C = c; + return 1; +} +int VB(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r < 0) + { + return 0; + } + if (WORD[i] != GRID[r--][c]) + { + return 0; + } + } + R = r + 1; + C = c; + return 1; +} +int DWN(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r < 0 || c < 0) + { + return 0; + } + if (WORD[i] != GRID[r--][c--]) + { + return 0; + } + } + R = r + 1; + C = c + 1; + return 1; +} +int DNE(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r < 0 || c >= N) + { + return 0; + } + if (WORD[i] != GRID[r--][c++]) + { + return 0; + } + } + R = r + 1; + C = r - 1; + return 1; +} +int DES(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r >= M || c >= N) + { + return 0; + } + if (WORD[i] != GRID[r++][c++]) + { + return 0; + } + } + R = r - 1; + C = c - 1; + return 1; +} +int DSW(int r, int c) +{ + int i, j; + for (i = 0; WORD[i]; i++) + { + if (r >= M || c < 0) + { + return 0; + } + if (WORD[i] != GRID[r++][c--]) + { + return 0; + } + } + R = r - 1; + C = c + 1; + return 1; +} +int CHECK(int r, int c) +{ + if (HF(r, c)) + { + return 1; + } + else if (HB(r, c)) + { + return 1; + } + else if (VF(r, c)) + { + return 1; + } + else if (VB(r, c)) + { + return 1; + } + else if (DWN(r, c)) + { + return 1; + } + else if (DNE(r, c)) + { + return 1; + } + else if (DES(r, c)) + { + return 1; + } + else if (DSW(r, c)) + { + return 1; + } + return 0; +} +int SOLVECASE() +{ + int p, q; + for (p = 0; p < M; p++) + for (q = 0; q < N; q++) + if (WORD[0] == GRID[p][q]) + if (CHECK(p, q)) + { + goto done; } - if (flag) break; +done:; + printf("%d %d\n", p + 1, q + 1); + return 0; +} +int main() +{ + int kase, i, n, m, f, j; + char input[50], fal[10]; + gets(input); + sscanf(input, "%d", &kase); + f = kase; + while (kase--) + { + gets(fal); + gets(input); + sscanf(input, "%d%d", &M, &N); + for (i = 0; i < M; i++) + { + gets(GRID[i]); + for (j = 0; GRID[i][j]; j++) + { + GRID[i][j] = tolower(GRID[i][j]); } - cout << i + 1 << " " << j + 1 << endl; } - if (T) cout << endl; + gets(input); + sscanf(input, "%d", &WW); + for (i = 0; i < WW; i++) + { + gets(WORD); + for (j = 0; WORD[j]; j++) + { + WORD[j] = tolower(WORD[j]); + } + SOLVECASE(); + } + if (kase > 0) + { + printf("\n"); + } } return 0; } diff --git a/10011.cpp b/10011.cpp new file mode 100644 index 0000000..32f989c --- /dev/null +++ b/10011.cpp @@ -0,0 +1,193 @@ +#include + +using namespace std; + +/** +1. test if the line through origin and house passes through the circle and has two +intersection points. +2. test if the segment connecting house and center of tree intersects with at least +one of the two tangent lines +3. test if the house is within the triangle formed by the origin and two tangent points + +if all three tests pass, then pick the minimum among the distance from the house to +the tree and the distance from the house from two tangent lines +**/ + +class Point +{ +public: + double x, y; + Point() + { + x = y = 0; + } + Point(double tx, double ty) + { + x = tx; + y = ty; + } +}; + +Point origin = Point(0, 0); + +int fsig(double x) +{ + if (fabs(x) < 1e-6) + { + return 0; + } + else if (x > 0) + { + return 1; + } + else + { + return -1; + } +} + +int fcmp(double x, double y) +{ + return fsig(x - y); +} + +double sqr(double x) +{ + return x * x; +} + +double safe_sqrt(double x) +{ + if (fsig(x) == 0) + { + return 0; + } + else + { + return sqrt(x); + } +} + +double dis(double x1, double y1, double x2, double y2) +{ + return safe_sqrt(sqr(x1 - x2) + sqr(y1 - y2)); +} + +double cross(double x1, double y1, double x2, double y2) +{ + return x1 * y2 - x2 * y1; +} + +double dis_ptl(double x, double y, double a, double b, double c) +{ + return fabs(a * x + b * y + c) / sqrt(sqr(a) + sqr(b)); +} + +double line_circle_delta(double a, double b, Point center, double r) +{ + double offset_x = center.x, offset_y = center.y; + double x1 = b, y1 = -a, x2 = b + b, y2 = -a - a; + x1 -= offset_x; + y1 -= offset_y; + x2 -= offset_x; + y2 -= offset_y; + double dx = x2 - x1, dy = y2 - y1; + double sqr_dr = sqr(dx) + sqr(dy); + double D = x1 * y2 - x2 * y1; + return sqr(r) * sqr_dr - sqr(D); +} + +Point get_tangent(double a, double b, Point center, double r) +{ + double offset_x = center.x, offset_y = center.y; + double x1 = b, y1 = -a, x2 = b + b, y2 = -a - a; + x1 -= offset_x; + y1 -= offset_y; + x2 -= offset_x; + y2 -= offset_y; + double dx = x2 - x1, dy = y2 - y1; + double sqr_dr = sqr(dx) + sqr(dy); + double D = x1 * y2 - x2 * y1; + double x = D * dy / sqr_dr; + double y = -D * dx / sqr_dr; + return Point(x + offset_x, y + offset_y); +} + +int equal(Point a, Point b) +{ + return fcmp(a.x, b.x) == 0 && fcmp(a.y, b.y) == 0; +} + +double area_triangle(Point a, Point b, Point c) +{ + return 0.5 * fabs(cross(c.x - a.x, c.y - a.y, c.x - b.x, c.y - b.y)); +} + +int point_in_triangle(Point p, Point a, Point b, Point c) +{ + if (equal(a, b) || equal(a, c) || equal(b, c)) + { + return 0; + } + return fcmp(area_triangle(a, b, c), area_triangle(p, a, b) + area_triangle(p, b, c) + area_triangle(p, a, c)) == 0; +} + +double calc(double cx, double cy, double r, double hx, double hy) +{ + Point center = Point(cx, cy); + Point house = Point(hx, hy); + double a1, b1, a2, b2; + if (fcmp(fabs(cy), r) == 0) + { + a1 = 0; + b1 = 1; + a2 = 2 * cx * cy; + b2 = sqr(r) - sqr(cx); + if (b2 > 0) + { + a2 = -a2; + b2 = -b2; + } + } + else + { + a1 = 1; + a2 = -1; + b1 = (-cx * cy - r * safe_sqrt(sqr(cy) + sqr(cx) - sqr(r))) / (sqr(cy) - sqr(r)); + b2 = -(-cx * cy + r * safe_sqrt(sqr(cy) + sqr(cx) - sqr(r))) / (sqr(cy) - sqr(r)); + } + double vx1 = b1, vy1 = -a1, vx2 = b2, vy2 = -a2; + /* Intersection test */ + if (fsig(line_circle_delta(hy, -hx, center, r)) <= 0) + { + return 0; + } + /* cross test */ + if (fsig(cross(vx1, vy1, cx, cy) * cross(vx1, vy1, hx, hy)) <= 0 || fsig(cross(vx2, vy2, cx, cy) * cross(vx2, vy2, hx, hy)) <= 0) + { + return 0; + } + Point t1 = get_tangent(a1, b1, center, r); + Point t2 = get_tangent(a2, b2, center, r); + if (point_in_triangle(house, origin, t1, t2)) + { + return 0; + } + double ans = dis(cx, cy, hx, hy) - r; + ans = min(ans, dis_ptl(hx, hy, a1, b1, 0)); + ans = min(ans, dis_ptl(hx, hy, a2, b2, 0)); + return ans; +} + +int main() +{ + int times; + scanf("%d", ×); + while (times--) + { + double cx, cy, r, hx, hy; + scanf("%lf%lf%lf%lf%lf", &cx, &cy, &r, &hx, &hy); + printf("%.3lf\n", calc(cx, cy, r, hx, hy)); + } + return 0; +} diff --git a/10012.cpp b/10012.cpp new file mode 100644 index 0000000..602b963 --- /dev/null +++ b/10012.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define ABS(X) ((X) < 0 ? -(X) : (X)) +#define MAX_CIRCLES 8 + +double R[MAX_CIRCLES], C[MAX_CIRCLES]; + +double distance(const int k, const int l) +{ + // hypot(a, b)? + return sqrt(pow(R[k] + R[l], 2) - pow(ABS(R[k] - R[l]), 2)); +} + +double width(double *R, const int circles) +{ + int i, j; + double max; + if (circles == 1) + { + return 2 * R[0]; + } + C[0] = R[0]; + for (i = 1; i < circles; i++) + { + for (j = max = 0; j < i; j++) + { + max = MAX(max, C[j] + distance(i, j)); + } + C[i] = MAX(R[i], max); + } + for (i = max = 0; i < circles; i++) + { + max = MAX(max, C[i] + R[i]); + } + return max; +} + +double minWidth(double *R, const int start, const int circles) +{ + if (start == circles - 1) + { + return width(R, circles); + } + else + { + int i; + double tmp, min = .0, w; + for (i = start; i < circles; i++) + { + tmp = R[i]; + R[i] = R[start]; + R[start] = tmp; + w = minWidth(R, start + 1, circles); + if (w < min || min == .0) + { + min = w; + } + R[start] = R[i]; + R[i] = tmp; + } + return min; + } +} + +int main() +{ + int i, j, inputs, circles; + double radius, width; + if (scanf("%d", &inputs) != 1) + { + return EXIT_SUCCESS; + } + for (i = 0; i < inputs; i++) + { + if (scanf("%d", &circles) != 1) + { + return EXIT_SUCCESS; + } + for (j = 0; j < circles; j++) + { + if (scanf("%lf", &radius) != 1) + { + return EXIT_SUCCESS; + } + R[j] = radius; + } + printf("%.3f\n", minWidth(R, 0, circles)); + } + return 0; +} diff --git a/10013.cpp b/10013.cpp index 78c20cc..61e2faa 100644 --- a/10013.cpp +++ b/10013.cpp @@ -1,32 +1,43 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int test,i; - scanf("%d",&test); - for(i=0;i=0; j--){ - int temp = sum[j]; - sum[j] = (temp+carry)%10; - carry = (temp+carry)/10; - } - for(j=0;j + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) +#define RFOR(i, a, b) for (int(i) = (a), _b(b); (i) >= _b; --(i)) + +static char a[1000003], b[1000010], s[1000010]; +static long n, m, i, j, k, t, c, sum, sp = 0; + +int main() +{ + scanf("%ld", &n); + FOREQ(t, 1, n) + { + scanf("%ld", &m); + FOREQ(i, 0, m) + a[i] = b[i] = s[i] = 0; + FOR(i, 0, m) + scanf("%ld %ld", &a[i], &b[i]); + c = 0; + k = 0; + RFOR(j, m - 1, 0) + { + sum = a[j] + b[j] + c; + s[k++] = sum % 10; + c = sum / 10; + } + if (sp++) + { + printf("\n"); + } + if (c) + { + s[k++] = c; + } + RFOR(i, k - 1, 0) + printf("%d", s[i]); + printf("\n"); + } + return 0; +} diff --git a/10014.cpp b/10014.cpp index 444d76c..03400fa 100644 --- a/10014.cpp +++ b/10014.cpp @@ -1,23 +1,30 @@ -#include +#include + using namespace std; -int main() { - int T; - scanf("%d", &T); - while (T--) { - int N; - scanf("%d", &N); - double start = 0, end = 0, value = 0; - scanf("%lf", &start); - scanf("%lf", &end); - for (int i = 0; i < N; i++) { - double carr = 0; - scanf("%lf", &carr); - value += (N - i) * carr; +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) + +static int cases, t, n, f = 0, n1; +static double a0, a_last, c, sum = 0.0f; + +int main() +{ + scanf("%d", &cases); + FOR(t, 0, cases) + { + sum = 0.0f; + scanf("%d\n%lf\n%lf\n", &n, &a0, &a_last); + n1 = n; + FOR(s, 0, n) + { + scanf("%lf", &c); + sum += (n1--) * c; + } + printf("%.2lf\n", (n * a0 + a_last - 2 * sum) / (double)(n + 1)); + if (t < cases - 1) + { + printf("\n"); } - double sum = (N * start + end - 2 * value) / (N + 1.0); - printf("%.2lf\n", sum); - if (T) printf("\n"); - } - return 0; + } + return 0; } diff --git a/10015.cpp b/10015.cpp index 27b009b..58379e9 100644 --- a/10015.cpp +++ b/10015.cpp @@ -1,69 +1,59 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 100000 +using namespace std; -bool isP[MAX + 1]; -vector Prime; +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) -void seive(){ - memset(isP, true, sizeof isP); - isP[0] = false; isP[1] = false; - for (int i = 2; i <= MAX; i++){ - if (isP[i]){ - Prime.push_back(i); - for (int j = 2 * i; j <= MAX; j += i) - isP[j] = false; +bool is_prime(int n) +{ + if (n == 2) + { + return true; + } + if (n % 2 == 0) + { + return false; + } + for (int i = 3; i * i <= n; i += 2) + if (n % i == 0) + { + return false; } + return true; +} + +static vector primes; + +inline int survivor(int n) +{ + int i, s; + for (s = 0, i = 1; i <= n; i++) + { + s = (s + primes[n - i]) % i; } - //cout << Prime.size() << endl; + return s + 1; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - while (true){ - int N, i; - scanf("%d", &N); - if (N == 0) break; - vector V(N); - FOI(i, 0, N-1) - V[i] = i + 1; - int pos = 0; - FOI(i, 0, N-2){ - pos = (pos + Prime[i] - 1) % V.size(); - //cout << Prime[i] << " " << pos << endl; - V.erase(V.begin() + pos); +static int n; + +int main() +{ + primes.push_back(2); + for (int i = 3; i < 100000; i += 2) + { + if (is_prime(i)) + { + primes.push_back(i); } - printf("%d\n", V[0]); + } + while (scanf("%d", &n)) + { + if (!n) + { + break; + } + printf("%d\n", survivor(n)); } return 0; } - diff --git a/10016.cpp b/10016.cpp new file mode 100644 index 0000000..d6afc38 --- /dev/null +++ b/10016.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) +#define RFOR(i, a, b) for (int(i) = (a), _b(b); (i) >= _b; --(i)) + +static int cases, t, c, n, board[101][101], tmp[101][101]; + +inline void apply_transformation(int ring) +{ + FOREQ(i, ring, n - ring - 1) + FOREQ(j, ring, n - ring - 1) + if (i == ring || i == n - ring - 1 || j == ring || j == n - ring - 1) + { + board[i][j] = tmp[i][j]; + } +} +inline void upside_down_flip(int ring) +{ + RFOR(i, n - 1, 0) + FOR(j, 0, n) + tmp[n - i - 1][j] = board[i][j]; + apply_transformation(ring); +} +inline void left_right_flip(int ring) +{ + FOR(i, 0, n) + RFOR(j, n - 1, 0) + tmp[i][n - j - 1] = board[i][j]; + apply_transformation(ring); +} +inline void flip_through_main_diagonal(int ring) +{ + FOR(i, 0, n) + FOR(j, 0, n) + tmp[i][j] = board[i][j]; + FOR(i, 0, n) + RFOR(j, n - 1, i + 1) + swap(tmp[i][j], tmp[j][i]); + apply_transformation(ring); +} +inline void flip_through_main_inverse_diagonal(int ring) +{ + FOR(i, 0, n) + FOR(j, 0, n) + tmp[i][j] = board[i][j]; + FOR(i, 0, n) + FOR(j, 0, n - i) + swap(tmp[i][j], tmp[n - j - 1][n - i - 1]); + apply_transformation(ring); +} +inline void print_board() +{ + FOR(i, 0, n) + { + FOR(j, 0, n) + { + if (j > 0) + { + printf(" "); + } + printf("%d", board[i][j]); + } + printf("\n"); + } +} + +int main() +{ + scanf("%d", &cases); + while (cases--) + { + scanf("%d", &n); + FOR(i, 0, n) + FOR(j, 0, n) + scanf("%d", &board[i][j]); + int rings = (n % 2 == 0) ? n / 2 : n / 2 + 1; + FOR(ring, 0, rings) + { + scanf("%d", &t); + while (t--) + { + scanf("%d", &c); + switch (c) + { + case 1: + upside_down_flip(ring); + break; + case 2: + left_right_flip(ring); + break; + case 3: + flip_through_main_diagonal(ring); + break; + case 4: + flip_through_main_inverse_diagonal(ring); + break; + } + } + } + print_board(); + } + return 0; +} diff --git a/10017.cpp b/10017.cpp new file mode 100644 index 0000000..faf9944 --- /dev/null +++ b/10017.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +int pillar[3][300], n, times, num[3], i; + +void hanoi(int source, int target, int temp, int disk) +{ + if (!disk) + { + return; + } + hanoi(source, temp, target, disk - 1); + if (!times) + { + return; + } + --times; + pillar[target][num[target]] = pillar[source][--num[source]]; + ++num[target]; + if (num[0]) + { + printf("A=> "); + for (i = 0; i < num[0]; ++i) + { + printf(" %d", pillar[0][i]); + } + putchar('\n'); + } + else + { + puts("A=>"); + } + if (num[1]) + { + printf("B=> "); + for (i = 0; i < num[1]; ++i) + { + printf(" %d", pillar[1][i]); + } + putchar('\n'); + } + else + { + puts("B=>"); + } + if (num[2]) + { + printf("C=> "); + for (i = 0; i < num[2]; ++i) + { + printf(" %d", pillar[2][i]); + } + putchar('\n'); + } + else + { + puts("C=>"); + } + putchar('\n'); + hanoi(temp, target, source, disk - 1); +} + +int main() +{ + int t = 0; + while (~scanf("%d %d", &n, ×)) + { + if (!n && !times) + { + break; + } + printf("Problem #%d\n\n", ++t); + for (i = 0; i < n; ++i) + { + pillar[0][i] = n - i; + } + num[0] = n; + num[1] = num[2] = 0; + if (num[0]) + { + printf("A=> "); + for (i = 0; i < num[0]; ++i) + { + printf(" %d", pillar[0][i]); + } + putchar('\n'); + } + else + { + puts("A=>"); + } + if (num[1]) + { + printf("B=> "); + for (i = 0; i < num[1]; ++i) + { + printf(" %d", pillar[1][i]); + } + putchar('\n'); + } + else + { + puts("B=>"); + } + if (num[2]) + { + printf("C=> "); + for (i = 0; i < num[2]; ++i) + { + printf(" %d", pillar[2][i]); + } + putchar('\n'); + } + else + { + puts("C=>"); + } + putchar('\n'); + hanoi(0, 2, 1, n); + } + return 0; +} diff --git a/10018.cpp b/10018.cpp index e4be376..cfe4ca5 100644 --- a/10018.cpp +++ b/10018.cpp @@ -1,49 +1,47 @@ -#include - -using namespace std; - -long reverse(long n); - -int main() -{ - long num=0,rev_num=0; - int i=0,n=0,iter=0; - cin>>n; - - for(i=0;i>num; - rev_num=reverse(num); - - if(num==rev_num) - cout<<"0 "< + +using namespace std; + +unsigned long reverse(unsigned long n) +{ + unsigned long rev_n = 0; + while (n != 0) + { + rev_n = (rev_n * 10) + n % 10; + n /= 10; + } + return rev_n; +} + +inline int is_palindrome(unsigned long n) +{ + return n == reverse(n); +} + +int reverse_and_add(unsigned long num, unsigned long &result) +{ + int counter = 0; + do + { + num += reverse(num); + counter++; + } while (!is_palindrome(num)); + result = num; + return counter; +} + +int main() +{ + int numOfCase; + scanf("%d", &numOfCase); + while (numOfCase--) + { + unsigned long num; + scanf("%lu", &num); + int iteration; // #iteration + unsigned long result; // final palindrome, like 9339 + iteration = reverse_and_add(num, result); + printf("%d %lu\n", iteration, result); + } + return 0; +} diff --git a/10019.cpp b/10019.cpp index 0fa5493..150d573 100644 --- a/10019.cpp +++ b/10019.cpp @@ -1,35 +1,41 @@ -#include +#include + using namespace std; -int hexa[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2}; +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) -int countHex(int N){ - int cnt = 0; - while( N ){ - cnt += hexa[N%10]; - N /= 10; +int mypower(int a, int b) +{ + int result = 1; + FOR(i, 0, b) + { + result *= a; } - return cnt; + return result; } -int countDec(int N){ - int cnt = 0; - while( N ){ - cnt += N%2; - N /= 2; +int hex_to_int(int n) +{ + int result = 0, counter = 0, tmp; + while (n > 0) + { + tmp = n % 10; + n /= 10; + result += tmp * mypower(16, counter); + counter++; } - return cnt; + return result; } -int main(){ - int T; - cin >> T; - while( T-- ){ - int N; - cin >> N; - int B1 = countDec(N); - int B2 = countHex(N); - //int V = N ^ ( B1 * B2 ); - cout << B1 << " " << B2 << endl; +static unsigned int n, num; + +int main() +{ + scanf("%u", &n); + while (n--) + { + scanf("%u", &num); + printf("%d %d\n", __builtin_popcount(num), __builtin_popcount(hex_to_int(num))); } + return 0; } diff --git a/10020.cpp b/10020.cpp new file mode 100644 index 0000000..0ba2407 --- /dev/null +++ b/10020.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +struct interval +{ + int l, r; + bool operator<(interval x) const + { + return l < x.l || (l == x.l && r < x.r); + } +} v[100048]; + +int idx[100048]; + +int main() +{ + int c, k, l, m, r, t; + for (scanf("%d", &t); t-- && scanf("%d", &m) == 1;) + { + for (c = 0; scanf("%d %d", &v[c].l, &v[c].r) && (v[c].l || v[c].r); c++) + ; + sort(v, v + c); + k = l = r = 0; + for (int i = 0; i < c && r < m; k++) + { + if (v[i].l > l) + { + i++; + } + for (; i < c && v[i].l <= l; ++i) + if (v[i].r >= r) + { + r = v[i].r; + idx[k] = i; + } + l = r; + } + if (r < m) + { + puts("0"); + } + else + { + printf("%d\n", k); + for (int *p = idx; p < (idx + k); ++p) + { + printf("%d %d\n", v[*p].l, v[*p].r); + } + } + if (t) + { + putc(10, stdout); + } + } + return 0; +} diff --git a/10021.cpp b/10021.cpp new file mode 100644 index 0000000..afab44c --- /dev/null +++ b/10021.cpp @@ -0,0 +1,395 @@ +#include + +using namespace std; + +/* + * +------+ + * / UP /| + * +------+ | + * |FRONT |R+ ----------------- + * | |/ board + * +------+ ----------------- + */ +#define FRONT 0 +#define BACK 1 +#define UP 2 +#define DOWN 3 +#define LEFT 4 +#define RIGHT 5 + +__inline void roll_left(short *cube); +__inline void roll_right(short *cube); +__inline void roll_up(short *cube); +__inline void roll_down(short *cube); + +short cube_start[6] = {1, 2, 3, 4, 5, 6}; + +struct Pos +{ + int x; + int y; + + bool operator==(Pos &pos) + { + if (this->x == pos.x && this->y == pos.y) + { + return true; + } + return false; + } +}; + +struct Node +{ + bool operator==(Node &node) + { + if (pos == node.pos && 0 == memcmp(node.cube, cube, 6 * sizeof(short))) + { + return true; + } + return false; + } + short cube[6]; + Pos pos; + unsigned int prev_node; +}; + +struct Cell +{ + Cell(); + vector used_node; + + // + // 0: no wall + // 1: down wall + // 2: right wall + // 3: down and right wall + // + short wall; +}; + +struct Case +{ + Case(); + int search_move_num(); + bool find_used_node(Node &node); + void insert_used_node(Pos &pos, unsigned int index); + void show_path(Node &node); + + vector node_list; + int row; + int col; + Pos start; + Pos end; + vector> board; + bool showflg; +}; + +int Case::search_move_num() +{ + vector expand_list; + int move_count = 0; + Node first_node; + first_node.pos = start; + first_node.prev_node = 0xffffffff; + memcpy(first_node.cube, cube_start, 6 * sizeof(short)); + node_list.push_back(first_node); + insert_used_node(first_node.pos, 0); + expand_list.push_back(node_list.size() - 1); + while (expand_list.size() != 0) + { + vector tmp_node_list; + move_count++; + for (int i = 0; i < expand_list.size(); i++) + { + Node cur_expand_node = node_list[expand_list[i]]; + bool expand = true; + if (cur_expand_node.pos.x == 8 && cur_expand_node.pos.y == 0) + { + show_path(cur_expand_node); + } + // move left + if (cur_expand_node.pos.y > 0 && !(board[cur_expand_node.pos.x][cur_expand_node.pos.y - 1].wall & 0x02)) + { + if (cur_expand_node.prev_node != -1) + { + Node prev_node = node_list[cur_expand_node.prev_node]; + if (prev_node.pos.y == cur_expand_node.pos.y - 1 && prev_node.pos.x == cur_expand_node.pos.x) + { + expand = false; + } + } + if (expand) + { + Node tmp_node = cur_expand_node; + tmp_node.pos.y--; + roll_left(tmp_node.cube); + tmp_node.prev_node = expand_list[i]; + if (tmp_node.pos == end && tmp_node.cube[DOWN] == cube_start[DOWN]) + { + cout << move_count << endl; + return move_count; + } + if (!find_used_node(tmp_node)) + { + node_list.push_back(tmp_node); + tmp_node_list.push_back(node_list.size() - 1); + insert_used_node(tmp_node.pos, node_list.size() - 1); + } + } + } + expand = true; + // move right + if (cur_expand_node.pos.y < col - 1 && !(board[cur_expand_node.pos.x][cur_expand_node.pos.y].wall & 0x02)) + { + if (cur_expand_node.prev_node != -1) + { + Node prev_node = node_list[cur_expand_node.prev_node]; + if (prev_node.pos.y == cur_expand_node.pos.y + 1 && prev_node.pos.x == cur_expand_node.pos.x) + { + expand = false; + } + } + if (expand) + { + Node tmp_node = cur_expand_node; + tmp_node.pos.y++; + roll_right(tmp_node.cube); + tmp_node.prev_node = expand_list[i]; + if (tmp_node.pos == end && tmp_node.cube[DOWN] == cube_start[DOWN]) + { + cout << move_count << endl; + return move_count; + } + if (!find_used_node(tmp_node)) + { + node_list.push_back(tmp_node); + tmp_node_list.push_back(node_list.size() - 1); + insert_used_node(tmp_node.pos, node_list.size() - 1); + } + } + } + // move up + expand = true; + if (cur_expand_node.pos.x > 0 && !(board[cur_expand_node.pos.x - 1][cur_expand_node.pos.y].wall & 0x01)) + { + if (cur_expand_node.prev_node != -1) + { + Node prev_node = node_list[cur_expand_node.prev_node]; + if (prev_node.pos.y == cur_expand_node.pos.y && prev_node.pos.x == cur_expand_node.pos.x - 1) + { + expand = false; + } + } + if (expand) + { + Node tmp_node = cur_expand_node; + tmp_node.pos.x--; + roll_up(tmp_node.cube); + tmp_node.prev_node = expand_list[i]; + if (tmp_node.pos == end && tmp_node.cube[DOWN] == cube_start[DOWN]) + { + cout << move_count << endl; + return move_count; + } + if (!find_used_node(tmp_node)) + { + node_list.push_back(tmp_node); + tmp_node_list.push_back(node_list.size() - 1); + insert_used_node(tmp_node.pos, node_list.size() - 1); + } + } + } + // move down + expand = true; + if (cur_expand_node.pos.x < row - 1 && !(board[cur_expand_node.pos.x][cur_expand_node.pos.y].wall & 0x01)) + { + if (cur_expand_node.prev_node != -1) + { + Node prev_node = node_list[cur_expand_node.prev_node]; + if (prev_node.pos.y == cur_expand_node.pos.y && prev_node.pos.x == cur_expand_node.pos.x + 1) + { + expand = false; + } + } + if (expand) + { + Node tmp_node = cur_expand_node; + tmp_node.pos.x++; + roll_down(tmp_node.cube); + tmp_node.prev_node = expand_list[i]; + if (tmp_node.pos == end && tmp_node.cube[DOWN] == cube_start[DOWN]) + { + cout << move_count << endl; + return move_count; + } + if (!find_used_node(tmp_node)) + { + node_list.push_back(tmp_node); + tmp_node_list.push_back(node_list.size() - 1); + insert_used_node(tmp_node.pos, node_list.size() - 1); + } + } + } + } + expand_list = tmp_node_list; + } + cout << "No solution" << endl; + return 0; +} + +int main() +{ + int case_num = 0; + Case cs; + char line_buffer[10] = ""; + cin >> case_num; + for (; case_num > 0; case_num--) + { + // input board size + cs.node_list.clear(); + cin >> cs.row >> cs.col; + cs.board = vector>(cs.row, vector(cs.col, Cell())); + cin >> cs.start.x >> cs.start.y; + cin >> cs.end.x >> cs.end.y; + cs.start.x--; + cs.start.y--; + cs.end.x--; + cs.end.y--; + char wall_type = ' '; + Pos wall_pos; + cin.getline(line_buffer, 10); + while (1) + { + cin.getline(line_buffer, 10); + if (cin.eof() || line_buffer[0] == '\0') + { + break; + } + if (line_buffer[0] == 'v' || line_buffer[0] == 'h') + { + wall_type = line_buffer[0]; + continue; + } + else + { + stringstream ss; + ss << line_buffer; + ss >> wall_pos.x >> wall_pos.y; + wall_pos.x--; + wall_pos.y--; + switch (wall_type) + { + case 'v': + cs.board[wall_pos.x][wall_pos.y].wall |= 0x01; + break; + case 'h': + cs.board[wall_pos.x][wall_pos.y].wall |= 0x02; + break; + default: + cout << "Wrong input" << endl; + exit(1); + } + } + } + cs.search_move_num(); + if (case_num != 1) + { + cout << endl; + } + } + return 0; +} + +__inline void roll_left(short *cube) +{ + short old_left = cube[LEFT]; + cube[LEFT] = cube[UP]; + cube[UP] = cube[RIGHT]; + cube[RIGHT] = cube[DOWN]; + cube[DOWN] = old_left; +} + +__inline void roll_right(short *cube) +{ + short old_left = cube[LEFT]; + cube[LEFT] = cube[DOWN]; + cube[DOWN] = cube[RIGHT]; + cube[RIGHT] = cube[UP]; + cube[UP] = old_left; +} + +__inline void roll_up(short *cube) +{ + short old_up = cube[UP]; + cube[UP] = cube[FRONT]; + cube[FRONT] = cube[DOWN]; + cube[DOWN] = cube[BACK]; + cube[BACK] = old_up; +} + +__inline void roll_down(short *cube) +{ + short old_up = cube[UP]; + cube[UP] = cube[BACK]; + cube[BACK] = cube[DOWN]; + cube[DOWN] = cube[FRONT]; + cube[FRONT] = old_up; +} + +Case::Case() +{ + row = 0; + col = 0; + showflg = false; +} + +Cell::Cell() +{ + wall = 0; +} + +bool Case::find_used_node(Node &node) +{ + Cell cell = board[node.pos.x][node.pos.y]; + for (int i = 0; i < cell.used_node.size(); i++) + { + if (node == node_list[cell.used_node[i]]) + { + return true; + } + } + return false; +} + +void Case::insert_used_node(Pos &pos, unsigned int index) +{ + board[pos.x][pos.y].used_node.push_back(index); +} + +void Case::show_path(Node &node) +{ + if (showflg) + { + cout << '(' << node.pos.x << ',' << node.pos.y << ')' << '\t'; + for (int i = 0; i < 6; i++) + { + cout << node.cube[i]; + } + cout << endl; + unsigned int prev = node.prev_node; + cout << '(' << node.pos.x << ',' << node.pos.y << ')' << endl; + while (prev != 0xffffffff) + { + Node prev_node = node_list[prev]; + cout << '(' << prev_node.pos.x << ',' << prev_node.pos.y << ')' << '\t'; + for (int i = 0; i < 6; i++) + { + cout << prev_node.cube[i]; + } + cout << endl; + prev = prev_node.prev_node; + } + cout << endl; + } +} diff --git a/10022.cpp b/10022.cpp new file mode 100644 index 0000000..41697ab --- /dev/null +++ b/10022.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +#define MAXI(a, b) (a > b ? a : b) +#define MINI(a, b) (a > b ? b : a) + +int N, M, N_row, M_row, N_left, M_left, N_right, M_right, MAX; +int Find_R(int n) +{ + int k; + k = sqrt((double)n); + if (k * k < n) + { + return k + 1; + } + return k; +} +int Find_L_R(int n, int row) +{ + int m, d, k; + m = (row - 1) * (row - 1) + 1; + d = n - m + 1; + k = ceil((double)d / 2); + return k; +} +int Find_R_R(int n, int row) +{ + int m, d, k; + m = row * row; + d = m - n + 1; + k = ceil((double)d / 2); + return k; +} +int Dis() +{ + if (M_left >= N_left && M_right >= N_right) + { + return 1; + } + return 0; +} +int Distance(int m, int m_r, int n, int n_r) +{ + int d = 0, k, dis; + if ((m % 2 && m_r % 2) || (!(m % 2) && !(m_r % 2))) + { + d++; + } + if ((n % 2 && n_r % 2 == 0) || (n % 2 == 0 && n_r % 2 != 0)) + { + d++; + } + k = m_r - n_r - 1; + dis = k * 2 + d + 1; + return dis; +} +int LEFT_inside() +{ + int d, val, dis, meet_row; + int meet_point; + d = M_left - N_left - 1; + meet_row = M_row - d; + dis = d * 2 + 1; + if ((M_row % 2 == 0 && M % 2 != 0) || (M_row % 2 != 0 && M % 2 == 0)) + { + dis++; + meet_row--; + } + meet_point = (meet_row - 1) * (meet_row - 1) + N_left * 2; + val = dis + Distance(meet_point, meet_row, N, N_row); + return val; +} +void Out_side() +{ + int meet_point, val; + int d = (M_row - 1) * (M_row - 1); + meet_point = d + N_left * 2; + val = Distance(meet_point, M_row, N, N_row); + val += abs(M - meet_point); + if (val < MAX) + { + MAX = val; + } + d = (M_row) * (M_row); + meet_point = d - N_right * 2 + 1; + val = Distance(meet_point, M_row, N, N_row); + val += abs(M - meet_point); + if (val < MAX) + { + MAX = val; + } +} +void Cal() +{ + int temp; + N_row = Find_R(N); + M_row = Find_R(M); + if (N_row == M_row) + { + printf("%d\n", abs(M - N)); + return; + } + N_left = Find_L_R(N, N_row); + M_left = Find_L_R(M, M_row); + if (N_left == M_left) + { + printf("%d\n", Distance(M, M_row, N, N_row)); + return; + } + N_right = Find_R_R(N, N_row); + M_right = Find_R_R(M, M_row); + if (N_right == M_right) + { + printf("%d\n", Distance(M, M_row, N, N_row)); + return; + } + if (Dis() == 1) + { + temp = LEFT_inside(); + if (temp < MAX) + { + MAX = temp; + } + } + else + { + Out_side(); + } + printf("%d\n", MAX); +} + +int main() +{ + int a, b; + int kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &a, &b); + MAX = 2147483647; + N = MINI(a, b); + M = MAXI(a, b); + Cal(); + if (kase) + { + printf("\n"); + } + } + return 0; +} diff --git a/10023.cpp b/10023.cpp index efdc426..3fc3b22 100644 --- a/10023.cpp +++ b/10023.cpp @@ -1,16 +1,84 @@ -#include -#include -#include -#include -#include -using namespace std; -int main(){ - int test; - scanf("%d",&test); - while(test--){ - long double Y; - scanf("%lf",&Y); - printf("%.0lf\n",sqrtl(Y)); - } - return 0; -} +#include + +using namespace std; + +static int N[505], O[1005], T[1005]; +static char in[1005], cp[1005]; + +int main() +{ + int t, first = 1, len, n, i, i2, j, k; + scanf("%d", &t); + getchar(); + while (t--) + { + getchar(); + gets(in); + len = strlen(in); + if (len & 1) + { + for (i = len; i > 0; i--) + { + in[i] = in[i - 1]; + } + in[0] = '0', in[++len] = '\0'; + } + n = len >> 1; + for (i = 0; i < len; i++) + { + O[i] = 0; + } + for (i = 0; i < len; i++) + { + cp[i] = '0'; + } + cp[len] = '\0'; + for (i = 1; i <= n; i++) + { + i2 = i * 2; + for (j = 0; j < i2; j++) + { + T[j] = O[j]; + } + for (N[i - 1] = 0; N[i - 1] < 10; N[i - 1]++) + { + for (j = 0; j < i; j++) + { + T[i + j] += 2 * N[j]; + } + T[i2 - 1]++; + for (j = i2 - 1; j > 0; j--) + { + T[j - 1] += T[j] / 10; + T[j] %= 10; + } + for (j = 0; j < i2; j++) + { + cp[j] = T[j] + '0'; + } + if (strncmp(cp, in, i2) > 0) + { + break; + } + for (j = 0; j < i2; j++) + { + O[j] = T[j]; + } + } + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + for (i = 0; i < n; i++) + { + printf("%d", N[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10024.cpp b/10024.cpp new file mode 100644 index 0000000..3dc1b64 --- /dev/null +++ b/10024.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +// roll dir +enum +{ + NORTH = 0, + SOUTH = 1, + WEST = 2, + EAST = 3, + NONE = 4, +}; + +// cube face +enum +{ + DOWN = 0, + UP = 1, + RIGHT = 2, + LEFT = 3, + FRONT = 4, + BACK = 5, +}; + +struct Pos +{ + int i; + int j; +}; + +short board[6][6]; +short ok_cube[6] = {1, 1, 1, 1, 1, 1}; + +void search(Pos *pos, int direction, short *cube); +void roll(short *cube, int direction); +int roll_east(short *cube); +int roll_west(short *cube); +int roll_north(short *cube); +int roll_south(short *cube); + +int main() +{ + int caseNum = 0; + Pos first_pos = {-1, -1}; + short cube[6] = {0, 0, 0, 0, 0, 0}; + cin >> caseNum; + for (; caseNum != 0; caseNum--) + { + for (int i = 0; i < 6; i++) + { + for (int j = 0; j < 6; j++) + { + cin >> board[i][j]; + if (board[i][j] != 0 && first_pos.i == -1) + { + first_pos.i = i; + first_pos.j = j; + } + } + } + // put cube on the first pos + cube[DOWN] = 1; + board[first_pos.i][first_pos.j] = 2; + search(&first_pos, NONE, cube); + if (0 == memcmp(ok_cube, cube, sizeof(cube))) + { + cout << "correct" << endl; + } + else + { + cout << "incorrect" << endl; + } + if (caseNum > 1) + { + cout << endl; + } + first_pos.i = -1; + first_pos.j = -1; + memset(cube, 0, sizeof(cube)); + } + return 0; +} + +void search(Pos *pos, int direction, short *cube) +{ + // mark the pos has been visited + board[pos->i][pos->j] = 2; + // roll west + if (pos->j > 0 && board[pos->i][pos->j - 1] == 1) + { + pos->j--; + roll_west(cube); + search(pos, EAST, cube); + pos->j++; + } + // roll east + if (pos->j < 5 && board[pos->i][pos->j + 1] == 1) + { + pos->j++; + roll_east(cube); + search(pos, WEST, cube); + pos->j--; + } + // roll north + if (pos->i > 0 && board[pos->i - 1][pos->j] == 1) + { + pos->i--; + roll_north(cube); + search(pos, SOUTH, cube); + pos->i++; + } + // roll south + if (pos->i < 5 && board[pos->i + 1][pos->j] == 1) + { + pos->i++; + roll_south(cube); + search(pos, NORTH, cube); + pos->i--; + } + roll(cube, direction); +} + +void roll(short *cube, int direction) +{ + switch (direction) + { + case NORTH: + roll_north(cube); + break; + case SOUTH: + roll_south(cube); + break; + case WEST: + roll_west(cube); + break; + case EAST: + roll_east(cube); + break; + case NONE: + break; + } +} + +int roll_east(short *cube) +{ + cube[RIGHT] = cube[UP]; + cube[UP] = cube[LEFT]; + cube[LEFT] = cube[DOWN]; + cube[DOWN] = 1; + return 0; +} + +int roll_west(short *cube) +{ + cube[LEFT] = cube[UP]; + cube[UP] = cube[RIGHT]; + cube[RIGHT] = cube[DOWN]; + cube[DOWN] = 1; + return 0; +} + +int roll_south(short *cube) +{ + cube[FRONT] = cube[UP]; + cube[UP] = cube[BACK]; + cube[BACK] = cube[DOWN]; + cube[DOWN] = 1; + return 0; +} + +int roll_north(short *cube) +{ + cube[BACK] = cube[UP]; + cube[UP] = cube[FRONT]; + cube[FRONT] = cube[DOWN]; + cube[DOWN] = 1; + return 0; +} diff --git a/10025.cpp b/10025.cpp index e4c0ded..009acaa 100644 --- a/10025.cpp +++ b/10025.cpp @@ -1,52 +1,87 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int64 a, i, j, s=0; - cin>>a; - if(a<0) - a*=-1; - for(i=1;s<=a;i++) - s+=i; - while((s-a)%2){ - s+=i; - i++; - } - cout< 0) - cout< + +using namespace std; + +typedef long long Big; +Big K; + +int GETI() +{ + Big L, U, sum, M; + L = 1; + U = K / 2 + 1; + M = (L + U) / 2; + sum = (M * (M + 1)) / 2; + while (L != U && sum != K && L != U - 1) + { + if (sum > K) + { + U = M - 1; + } + else + { + L = M + 1; + } + M = (U + L) / 2; + sum = (M * (M + 1)) / 2; + } + return M; +} +void CAL() +{ + Big I, i, sum; + I = GETI(); + sum = (I * (I + 1)) / 2; + if (sum == K) + { + printf("%lld\n", I); + return; + } + for (i = I;; i++) + { + sum = ((i * (i + 1)) / 2) - K; + if (sum % 2 == 0) + { + printf("%lld\n", i); + return; + } + } +} + +int main() +{ + int kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%lld", &K); + if (K < 0) + { + K = -K; + } + if (K == 0 || K == 2) + { + printf("3\n"); + if (kase) + { + printf("\n"); + } + continue; + } + else if (K == 1) + { + printf("1\n"); + if (kase) + { + printf("\n"); + } + continue; + } + CAL(); + if (kase) + { + putchar('\n'); + } + } + return 0; +} diff --git a/10026.cpp b/10026.cpp new file mode 100644 index 0000000..845748f --- /dev/null +++ b/10026.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define ALL(a) (a).begin(), (a).end() +#define SZ(a) ((int)(a).size()) +#define pb push_back +#define mp make_pair + +inline bool by_key_desc_val_asc(const pair &a, const pair &b) +{ + if (a.first == b.first) + { + return a.second < b.second; + } + return a.first > b.first; +} + +static int cases, n, t, f; +static vector> v; + +int main() +{ + scanf("%d", &cases); + while (cases--) + { + scanf("%d", &n); + v.clear(); + FOR(i, 0, n) + { + scanf("%d%d", &t, &f); + v.pb(mp(f * 1.0 / t, i + 1)); + } + sort(ALL(v), by_key_desc_val_asc); + int sz = SZ(v); + FOR(i, 0, sz) + { + if (i > 0) + { + printf(" "); + } + printf("%d", v[i].second); + } + printf("\n"); + if (cases) + { + printf("\n"); + } + } + return 0; +} diff --git a/10027.cpp b/10027.cpp new file mode 100644 index 0000000..efbe682 --- /dev/null +++ b/10027.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define LINESIZE 32 + +struct Rule +{ + string str1; + string str2; +}; + +int calcCount(string &initialStr, vector &rules); + +int main() +{ + int caseNum = 0; + char buffer[LINESIZE]; + vector ruleList; + string initialStr; + char *p1; + char *p2; + // get case num + cin.getline(buffer, LINESIZE); + caseNum = atoi(buffer); + // skip blank line + cin.getline(buffer, 32); + for (; caseNum != 0; caseNum--) + { + // get initial string + cin.getline(buffer, LINESIZE); + buffer[strlen(buffer) - 1] = '\0'; + initialStr = buffer + 1; + // get replace list + cin.getline(buffer, LINESIZE); + while (buffer[0] != '\0') + { + p1 = strchr(buffer, '\"'); + p1++; + p2 = strchr(p1, '\"'); + *p2++ = '\0'; + Rule rule; + rule.str1 = p1; + p1 = strchr(p2, '\"'); + p1++; + p2 = strchr(p1, '\"'); + *p2 = '\0'; + rule.str2 = p1; + ruleList.push_back(rule); + cin.getline(buffer, LINESIZE); + } + int count = calcCount(initialStr, ruleList); + if (0 == count) + { + cout << "Too many." << endl; + } + else + { + cout << count << endl; + } + if (caseNum != 1) + { + cout << endl; + } + ruleList.clear(); + } + return 0; +} + +int calcCount(string &initialStr, vector &rules) +{ + vector strList; + strList.push_back(initialStr); + int i = 0; + int count = 0; + int maxSize = initialStr.size(); + for (i = 0; i < strList.size(); i++) + { + for (int k = 0; k < rules.size(); k++) + { + if (rules[k].str1 == "" && rules[k].str2 != "") + { + // must over 1000 + return 0; + } + if (rules[k].str1 != rules[k].str2 && rules[k].str2.find(rules[k].str1, 0) != string::npos) + { + // must over 1000 + return 0; + } + int firstPos = -1; + while ((firstPos = strList[i].find(rules[k].str1, firstPos + 1)) != string::npos) + { + string tmpStr = strList[i]; + tmpStr.replace(firstPos, rules[k].str1.size(), rules[k].str2); + int m = 0; + if (tmpStr.size() > maxSize) + { + if (count == 1000) + { + return 0; + } + maxSize = tmpStr.size(); + strList.push_back(tmpStr); + count++; + continue; + } + for (m = 0; m < strList.size(); m++) + { + if (strList[m] == tmpStr) + { + break; + } + } + if (m == strList.size()) + { + if (count == 1000) + { + return 0; + } + strList.push_back(tmpStr); + count++; + } + } + } + } + return i; +} diff --git a/10028.cpp b/10028.cpp new file mode 100644 index 0000000..8087a4a --- /dev/null +++ b/10028.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +#define OFFENCE false +#define AWARD true +#define pb push_back +bool year_check(int y1, int m1, int d1, int y2, int m2, int d2) +{ + if (y1 < y2) + { + return AWARD; + } + else if (y1 > y2) + { + return OFFENCE; + } + else + { + if (m1 < m2) + { + return AWARD; + } + else if (m1 > m2) + { + return OFFENCE; + } + else + { + if (d1 < d2) + { + return AWARD; + } + else if (d1 > d2) + { + return OFFENCE; + } + else + { + return AWARD; + } + } + } + return false; +} + +void solve(int f_year, int f_month, int f_day, int n, + vector &year, vector &month, vector &day, + vector &point) +{ + int dem = 0, me = 0, p = 0; + int cyear = f_year, cmonth = f_month, cday = f_day; + printf("%04d-%02d-%02d No merit or demerit points.\n", + cyear, cmonth, cday); + cyear += 2; + while (true) + { + if (p == n && me == 5) + { + break; + } + if (p != n && year_check(cyear, cmonth, cday, year[p], month[p], day[p]) == OFFENCE) + { + dem += point[p]; + if (me * 2 < dem) + { + dem -= me * 2; + me = 0; + printf("%04d-%02d-%02d %d demerit point(s).\n", + year[p], month[p], day[p], dem); + cyear = year[p] + 1; + cmonth = month[p]; + cday = day[p]; + } + else if (me * 2 >= dem) + { + dem = 0; + me = me - dem / 2; + if (me == 0) + printf("%04d-%02d-%02d No merit or demerit points.\n", + cyear, cmonth, cday); + else + printf("%04d-%02d-%02d %d merit point(s).\n", + cyear, cmonth, cday, me); + cyear = year[p] + 2; + cmonth = month[p]; + cday = day[p]; + } + p++; + } + else + { + if (dem > 2) + { + dem /= 2; + printf("%04d-%02d-%02d %d demerit point(s).\n", + cyear, cmonth, cday, dem); + cyear++; + } + else if (dem > 0) + { + dem = 0; + me = 0; + printf("%04d-%02d-%02d No merit or demerit points.\n", + cyear, cmonth, cday); + cyear += 2; + } + else + { + me++; + printf("%04d-%02d-%02d %d merit point(s).\n", + cyear, cmonth, cday, me); + cyear += 2; + } + } + } + return; +} + +int main() +{ + string in; + int te, tc = 0; + cin >> te; + while (te--) + { + if (tc) + { + puts(""); + } + tc++; + cin >> in; + int fyear, fmonth, fday; + fyear = (in[0] - '0') * 1000 + (in[1] - '0') * 100 + (in[2] - '0') * 10 + (in[3] - '0'); + fmonth = (in[4] - '0') * 10 + (in[5] - '0'); + fday = (in[6] - '0') * 10 + (in[7] - '0'); + getline(cin, in); + vector year, month, day, point; + while (getline(cin, in) && in.size() > 0) + { + int ty, tm, td, tp; + ty = (in[0] - '0') * 1000 + (in[1] - '0') * 100 + (in[2] - '0') * 10 + (in[3] - '0'); + tm = (in[4] - '0') * 10 + (in[5] - '0'); + td = (in[6] - '0') * 10 + (in[7] - '0'); + year.pb(ty); + month.pb(tm); + day.pb(td); + stringstream sin(in); + sin >> ty >> tp; + point.pb(tp); + } + solve(fyear, fmonth, fday, year.size(), year, month, day, point); + } + return 0; +} diff --git a/10029.cpp b/10029.cpp new file mode 100644 index 0000000..4c96353 --- /dev/null +++ b/10029.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +const int N = 26000; + +char str[N][20]; +int dp[N]; + +void dele(char *str, char *res, int pos) +{ + int len = strlen(str), cnt = 0; + for (int i = 0; i < len; i++) + if (i != pos) + res[cnt++] = str[i]; + res[cnt] = '\0'; +} + +void repla(char *str, char *res, int pos, char letter) +{ + int len = strlen(str); + for (int i = 0; i < len; i++) + { + if (i == pos) + { + res[i] = letter; + } + else + { + res[i] = str[i]; + } + } + res[len] = '\0'; +} + +void add(char *str, char *res, int pos, char letter) +{ + int len = strlen(str), cnt = 0; + for (int i = 0; i <= len; i++) + { + if (i == pos) + res[cnt++] = letter; + if (i < len) + res[cnt++] = str[i]; + } + str[cnt] = '\0'; +} + +int bin_srch(int start, int end, char *a) +{ + while (start <= end) + { + int mid = start + (end - start) / 2; + int res = strcmp(a, str[mid]); + if (res < 0) // smaller + { + end = mid - 1; + } + else if (res == 0) // equal: found val + { + return mid; + } + else // larger + { + start = mid + 1; + } + } + return -1; +} + +void fun(char *a, int pos) +{ + int pre = bin_srch(0, pos - 1, a); + if (pre != -1) + { + dp[pos] = max(dp[pos], dp[pre] + 1); + } +} + +int main() +{ + int n = 0, imax = 0; + while (scanf("%s", str[n]) != EOF) + { + n++; + } + memset(dp, 0, sizeof(dp)); + for (int i = 1; i < n; i++) + { + int len = strlen(str[i]); + for (int j = 0; j <= len; j++) + { + char temp[20] = {0}; + for (char k = 'a'; k <= 'z'; (int)k++) + { + if (j < len) + { + repla(str[i], temp, j, k); + fun(temp, i); + } + add(str[i], temp, j, k); + fun(temp, i); + } + if (j < len) + { + dele(str[i], temp, j); + fun(temp, i); + } + } + imax = max(imax, dp[i]); + } + printf("%d\n", imax + 1); + return 0; +} diff --git a/10030.cpp b/10030.cpp new file mode 100644 index 0000000..65dc068 --- /dev/null +++ b/10030.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +class state +{ +public: +#define FILE 1 +#define EXT 0 + string in; + int data[2]; + bool is_exist; +}; + +int check[1000]; +void solve(int n, int m, state *in) +{ + int cnt = n; + rep(k, m) + { + fill(check, check + n, 0); + rep(i, n) + { + if (in[i].is_exist == true) + { + check[in[i].data[k % 2]]++; + } + } + rep(i, n) + { + if (in[i].is_exist == true && check[in[i].data[k % 2]] == 1) + { + in[i].is_exist = false, cnt--; + } + } + } + cout << cnt << endl; + rep(i, n) if (in[i].is_exist == true) + { + cout << in[i].in << endl; + } +} + +int main() +{ + int te, tc = 0; + cin >> te; + while (te--) + { + if (tc) + { + puts(""); + } + tc++; + int n, m; + cin >> n >> m; + map File, Ext; + state in[n]; + rep(i, n) + { + in[i].is_exist = true; + cin >> in[i].in; + int p = 0; + string f = "", e = ""; + while (p < in[i].in.size() && in[i].in[p] != '.') + { + f += in[i].in[p++]; + } + p++; + while (p < in[i].in.size()) + { + e += in[i].in[p++]; + } + if (File.find(f) != File.end()) + { + in[i].data[FILE] = File[f]; + } + else + { + in[i].data[FILE] = File.size(), File[f] = in[i].data[FILE]; + } + if (Ext.find(e) != Ext.end()) + { + in[i].data[EXT] = Ext[e]; + } + else + { + in[i].data[EXT] = Ext.size(), Ext[e] = in[i].data[EXT]; + } + } + solve(n, m, in); + } + return 0; +} diff --git a/10031.cpp b/10031.cpp new file mode 100644 index 0000000..dccdded --- /dev/null +++ b/10031.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back +#define mp make_pair + +typedef complex P; +typedef long long LL; + +const int N = 201; + +struct Line +{ + int minx, maxx; + double A, B; +}; + +vector cons(vector

&in) +{ + vector ret; + for (int i = 0; i < in.size() - 1; i++) + { + if (in[i].real() == in[i + 1].real()) + { + continue; + } + double A = (double)(in[i + 1].imag() - in[i].imag()) / (in[i + 1].real() - in[i].real()); + double B = -A * in[i + 1].real() + in[i + 1].imag(); + ret.pb((Line){ + min(in[i].real(), in[i + 1].real()), + max(in[i].real(), in[i + 1].real()), A, B}); + } + return ret; +} + +LL get_sect(vector &in, int x) +{ + LL ret = 0; + static pair data[N]; + int p = 0; + for (int i = 0; i < in.size(); i++) + { + if (in[i].minx <= x && x < in[i].maxx) + { + data[p++] = mp(in[i].A * x + in[i].B, in[i].A * (x + 1) + in[i].B); + if (data[p - 1].first > data[p - 1].second) + { + swap(data[p - 1].first, data[p - 1].second); + } + } + } + sort(data, data + p); + // assert(p % 2 == 0 && p < N); + for (int i = 0; i < p; i += 2) + { + int tmp = (int)(floor(data[i + 1].first)) - (int)(ceil(data[i].second)); + if (tmp < 0) + { + continue; + } + ret += (int)(floor(data[i + 1].first)) - (int)(ceil(data[i].second)); + } + return ret; +} + +// y-y1 = (y2-y1)/(x2-x1)*x(x-x1); +// y = (y2-y1)/(x2-x1)*x(x-x1) + y1; +// y = ax - ax1 + y1; +LL solve(vector

&in) +{ + vector a = cons(in); + LL ret = 0LL; + for (int i = 0; i < 100000; i++) + { + ret += get_sect(a, i); + } + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + string _; + getline(cin, _); + getline(cin, _); + while (T--) + { + vector

in; + while (getline(cin, _) && _.size() > 0) + { + stringstream sin(_); + int re, im; + sin >> re >> im; + in.pb(P(re, im)); + } + in.pb(in[0]); + printf("%lld\n", solve(in)); + if (T) + { + printf("\n"); + } + } + return 0; +} diff --git a/10032.cpp b/10032.cpp new file mode 100644 index 0000000..02f3c09 --- /dev/null +++ b/10032.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +#define maxn 30000 + +char Fg[51][maxn]; +int W[51][maxn]; +int N, A[101], Ind[51], Sum; + +void Dynamic() +{ + int i, j, k, d, l, ind, lim = Sum / 2; + k = N / 2; + if (N % 2 == 0) + { + k--; + } + for (i = 0; i < N; i++) + { + for (j = k; j >= 1; j--) + { + for (l = 0; l < Ind[j]; l++) + { + d = W[j][l] + A[i]; + if (d > lim) + continue; + if (Fg[j + 1][d]) + continue; + ind = Ind[j + 1]; + W[j + 1][ind] = d; + Fg[j + 1][d] = 1; + Ind[j + 1]++; + if (j == k && d == lim) + return; + } + } + if (Fg[1][A[i]] == 0 && A[i] <= lim) + { + W[1][Ind[1]++] = A[i]; + Fg[1][A[i]] = 1; + } + } +} + +int Find() +{ + int i, d, k, max = 0, res; + k = N / 2; + for (i = 0; i < Ind[k]; i++) + { + d = W[k][i]; + if (d > max) + { + res = W[k][i]; + max = W[k][i]; + } + } + if (N % 2) + k++; + for (i = 0; i < Ind[k]; i++) + { + d = W[k][i]; + if (d > max) + { + res = W[k][i]; + max = W[k][i]; + } + } + return res; +} + +void Cal() +{ + int d; + if (N == 1) + { + cout << "0 " << A[0] << endl; + return; + } + else if (N == 2) + { + cout << MIN(A[0], A[1]) << " " << MAX(A[0], A[1]) << endl; + return; + } + Dynamic(); + d = Find(); + cout << d << " " << Sum - d << endl; +} + +void Free() +{ + int i, j, k = N / 2, d; + if (N % 2) + k++; + for (i = 1; i <= k; i++) + { + for (j = 0; j < Ind[i]; j++) + { + d = W[i][j]; + Fg[i][d] = 0; + } + Ind[i] = 0; + } +} + +int main() +{ + int k, i; + cin >> k; + while (k--) + { + cin >> N; + Sum = 0; + for (i = 0; i < N; i++) + { + cin >> A[i]; + Sum += A[i]; + } + Cal(); + if (k) + { + cout << endl; + } + Free(); + } + return 0; +} diff --git a/10033.cpp b/10033.cpp new file mode 100644 index 0000000..3857cb8 --- /dev/null +++ b/10033.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +int main() +{ + int r[11]; + int ram[1001]; + int cases; + int instruction; + int count; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + count = 0; + fill(r, r + 11, 0); + fill(ram, ram + 1001, 0); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> instruction; + cin.ignore(100, '\n'); + ram[count] = instruction; + count++; + } + cin.ignore(100, '\n'); + int pos = 0; + int command; + int result = 0; + int d, n; + while (true) + { + command = ram[pos]; + result++; + if (command == 100) + { + break; + } + d = (command % 100) / 10; + n = command % 10; + switch (command / 100) + { + case 0: + if (r[n] > 0) + { + pos = r[d]; + } + else + { + pos++; + } + break; + case 2: + r[d] = n; + pos++; + break; + case 3: + r[d] += n; + r[d] %= 1000; + pos++; + break; + case 4: + r[d] *= n; + r[d] %= 1000; + pos++; + break; + case 5: + r[d] = r[n]; + pos++; + break; + case 6: + r[d] += r[n]; + r[d] %= 1000; + pos++; + break; + case 7: + r[d] *= r[n]; + r[d] %= 1000; + pos++; + break; + case 8: + r[d] = ram[r[n]]; + pos++; + break; + case 9: + ram[r[n]] = r[d]; + pos++; + break; + } + } + cout << result << endl; + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/10034.cpp b/10034.cpp new file mode 100644 index 0000000..ae5bb1c --- /dev/null +++ b/10034.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int t, n; + double x, y; + cin >> t; + while (t--) + { + cin >> n; + vector> dots; + priority_queue>> edgeList; + while (n--) + { + cin >> x >> y; + for (int i = 0, sz = dots.size(); i < sz; i++) + { + double dist = sqrt((x - dots[i].first) * (x - dots[i].first) + (y - dots[i].second) * (y - dots[i].second)); + edgeList.push(make_pair(-dist, make_pair(i, sz))); + } + dots.push_back(make_pair(x, y)); + } + double mst_cost = 0; + UnionFindDisjointSets ds(dots.size()); + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + mst_cost += (-front.first); + ds.unionSet(front.second.first, front.second.second); + } + } + cout << setprecision(2) << fixed << showpoint << mst_cost << endl; + if (t) + { + cout << endl; + } + } + return 0; +} diff --git a/10035.cpp b/10035.cpp index f7a6ad3..9b6df97 100644 --- a/10035.cpp +++ b/10035.cpp @@ -1,52 +1,72 @@ -#include - -using namespace std; - -long long carry(long long n) -{ - long long carry=0; - if(n>9) - carry =1; - else - carry =0; - - return carry; -} - -int main() -{ - long long a,b,r1=0,r2=0,r=0,c=0,count=0; - for(;;) - { - cin>>a>>b; - - if(a==0 && b==0) - break; - - count =0; - r1=0;r2=0;r=0; - while((a!=0)||(b!=0)) - { - r1=a%10; - r2=b%10; - a/=10; - b/=10; - - r=r1+r2+c; - - if(r>=9) - count++; - - c=carry(r); - } - if(count>1) - cout< + +using namespace std; + +enum +{ + MAX_LEN = 12 +}; + +int to_big_number(char *num) +{ + // reverse + int len = strlen(num); + for (int i = 0; i < len / 2; ++i) + { + swap(num[i], num[len - i - 1]); + } + // sub ascii + for (int i = 0; i < len; ++i) + { + num[i] = num[i] - '0'; + } + // fill zero + for (int i = len; i < MAX_LEN; ++i) + { + num[i] = 0; + } +} + +int count_carry(char *a, char *b) +{ + to_big_number(a); + to_big_number(b); + int carry_counter = 0; + for (int i = 0; i < MAX_LEN; ++i) + { + a[i] = a[i] + b[i]; + if (a[i] > 9) + { + a[i] = a[i] - 10; + a[i + 1]++; + carry_counter++; + } + } + return carry_counter; +} + +int main() +{ + char a[MAX_LEN], b[MAX_LEN]; + while (scanf("%s %s ", a, b) == 2) + { + if (a[0] == '0' && b[0] == '0') + { + break; + } + int result = count_carry(a, b); + if (result == 0) + { + printf("No carry operation.\n"); + } + else if (result == 1) + { + printf("1 carry operation.\n"); + } + else + { + printf("%d carry operations.\n", result); + } + } + return 0; +} diff --git a/10036.cpp b/10036.cpp new file mode 100644 index 0000000..fb6809b --- /dev/null +++ b/10036.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define MAXN 102 + +int Table[10002][MAXN], K, N, V, F; + +void Cal() +{ + int i, j; + int pos, neg; + for (i = 2; i <= N; i++) + { + scanf("%d", &V); + if (V) + { + F = 1; + } + //V = fabs(V); + for (j = 0; j < K; j++) + { + if (Table[i - 1][j]) + { + pos = (j + V) % K; + neg = (j - V) % K; + pos = fabs(pos); + neg = fabs(neg); + Table[i][pos] = 1; + Table[i][neg] = 1; + } + } + } + if (Table[N][0] == 1) + { + printf("Divisible\n"); + } + else + { + printf("Not divisible\n"); + } +} +void INI() +{ + int i, j; + for (i = 1; i <= N; i++) + for (j = 0; j <= K; j++) + { + Table[i][j] = 0; + } +} + +int main() +{ + int temp, kase; + scanf("%d", &kase); + while (kase--) + { + F = 0; + scanf("%d%d", &N, &K); + scanf("%d", &V); + V = fabs(V); + if (V) + { + F = 1; + } + temp = V % K; + INI(); + Table[1][temp] = 1; + Cal(); + } + return 0; +} diff --git a/10037.cpp b/10037.cpp new file mode 100644 index 0000000..d01e334 --- /dev/null +++ b/10037.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +vector people; + +int main() +{ + int temp; + bool first = true; + int st; + int N, total; + int s1, s2; + scanf("%d", &N); + scanf("\n"); + for (int i = 0; i < N; i++) + { + people.clear(); + scanf("%d", &st); + for (int j = 0; j < st; j++) + { + scanf("%d", &temp); + people.push_back(temp); + } + if (first) + { + first = false; + } + else + { + putchar(10); + } + sort(people.begin(), people.end()); + if (st == 1) + { + printf("%d\n%d\n", people[0], people[0]); + continue; + } + else if (st == 2) + { + printf("%d\n%d %d\n", people[1], people[0], people[1]); + continue; + } + total = 0; + if (st % 2 == 0) + { + for (int j = 1; j < st / 2; j++) + { + int score = min(people[2 * j + 1] + people[0] + people[1] * 2, people[0] * 2 + people[2 * j] + people[2 * j + 1]); + total += score; + } + total += people[1]; + printf("%d\n", total); + for (int j = st / 2 - 1; j > 0; j--) + { + if (people[2 * j + 1] + people[0] + people[1] * 2 <= people[0] * 2 + people[2 * j] + people[2 * j + 1]) + { + printf("%d %d\n%d\n%d %d\n%d\n", people[0], people[1], people[0], people[2 * j], people[2 * j + 1], people[1]); + } + else + { + printf("%d %d\n%d\n%d %d\n%d\n", people[0], people[2 * j + 1], people[0], people[0], people[2 * j], people[0]); + } + } + printf("%d %d\n", people[0], people[1]); + } + else if (st % 2 == 1) + { + for (int j = 2; j <= st / 2; j++) + { + int score = min(people[2 * j] + people[0] + people[1] * 2, people[0] * 2 + people[2 * j] + people[2 * j - 1]); + total += score; + } + total += people[1] + people[0] + people[2]; + printf("%d\n", total); + for (int j = st / 2; j > 1; j--) + { + if (people[2 * j] + people[0] + people[1] * 2 < people[0] * 2 + people[2 * j] + people[2 * j - 1]) + { + printf("%d %d\n%d\n%d %d\n%d\n", people[0], people[1], people[0], people[2 * j - 1], people[2 * j], people[1]); + } + else + { + printf("%d %d\n%d\n%d %d\n%d\n", people[0], people[2 * j - 1], people[0], people[0], people[2 * j], people[0]); + } + } + printf("%d %d\n%d\n%d %d\n", people[0], people[1], people[0], people[0], people[2]); + } + } + return 0; +} diff --git a/10038.cpp b/10038.cpp index 36f3da4..9daf794 100644 --- a/10038.cpp +++ b/10038.cpp @@ -1,46 +1,49 @@ -#include - -using namespace std; - -int main() -{ - int n,i,j,k,l,t,temp,ip[3000]={0},seque[3000],diff[3000]; - - cin>>n; - - for(i=0;i>ip[i]; - } - - for(j=0;jdiff[j+1]) - { - temp=diff[j]; - diff[j]=diff[j+1]; - diff[j+1]=temp; - } - - for(t=0;t + +using namespace std; + +bool flag[3010]; + +int main() +{ + int n, value, rec; + while (cin >> n) + { + memset(flag, 0, sizeof(flag)); + int loop; + for (loop = 0; loop < n; loop++) + { + cin >> value; + if (loop == 0) + { + flag[(int)fabs(value)] = true; + rec = value; + } + else + { + int temp; + temp = (int)fabs(value - rec); + if (temp <= 3000) + { + flag[temp] = true; + rec = value; + } + } + } + bool Joly = true; + for (loop = 1; loop < n; loop++) + { + if (flag[loop] == false) + { + cout << "Not jolly" << endl; + Joly = false; + break; + } + } + if (Joly) + { + cout << "Jolly" << endl; + } + } + return 0; +} diff --git a/10039.cpp b/10039.cpp new file mode 100644 index 0000000..92e6f9a --- /dev/null +++ b/10039.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define MAXN 110 +#define MAXT 2400 + +int sr, tg, n, m, begTime, endTime, ansBeg, ansEnd, start; +int visitTime[MAXN][MAXT]; +bool FIND, visitCity[MAXN]; +map city; +vector>> train, g; +vector> visit; +char cityName[MAXN][100]; + +int DFS(int now, int t) +{ + if (now == sr && t >= start) + { + return visitTime[now][t] = t; + } + if (now == sr) + { + return -1e9; + } + if (visitTime[now][t] != -1) + { + return visitTime[now][t]; + } + for (int i = 0; i < g[now].size(); ++i) + { + int nowTrain = g[now][i].first; + int nowStation = g[now][i].second; + int nowTime = train[nowTrain][nowStation].first; + if (nowTime > t) + { + continue; + } + for (int j = nowStation + 1; j < train[nowTrain].size(); ++j) + { + int next = train[nowTrain][j].second; + visitTime[now][nowTime] = max(visitTime[now][nowTime], DFS(next, train[nowTrain][j].first)); + } + visitTime[now][t] = max(visitTime[now][t], visitTime[now][nowTime]); + } + return visitTime[now][t]; +} + +bool cmp(pair a, pair b) +{ + return a.first > b.first; +} + +int main() +{ + int t; + scanf("%d", &t); + for (int k = 0; k < t; ++k) + { + city.clear(); + train.clear(); + visit.clear(); + g.clear(); + char buf[100]; + scanf("%d", &n); + g = vector>>(n); + for (int i = 0; i < n; ++i) + { + scanf("%s", cityName[i]); + city[cityName[i]] = i; + } + scanf("%d", &m); + train = vector>>(m); + for (int i = 0; i < m; ++i) + { + int x; + scanf("%d", &x); + for (int j = 0; j < x; ++j) + { + int t; + scanf("%d %s", &t, buf); + t = t / 100 * 60 + t % 100; + int c = city[buf]; + train[i].push_back(pair(t, c)); + } + sort(train[i].begin(), train[i].end(), cmp); + for (int j = 0; j < x; ++j) + { + int c = train[i][j].second; + g[c].push_back(pair(i, j)); + } + } + scanf("%d", &start); + start = start / 100 * 60 + start % 100; + scanf("%s", buf); + sr = city[buf]; + scanf("%s", buf); + tg = city[buf]; + memset(visitTime, -1, sizeof(visitTime)); + DFS(tg, 2400); + ansBeg = -1e9, ansEnd = 1e9; + for (int i = 0; i < g[tg].size(); ++i) + { + int nowTrain = g[tg][i].first; + int nowStation = g[tg][i].second; + int nowTime = train[nowTrain][nowStation].first; + if (visitTime[tg][nowTime] != -1) + { + if (ansEnd > nowTime) + { + ansBeg = visitTime[tg][nowTime], ansEnd = nowTime; + } + else if (ansEnd == nowTime && ansEnd - ansBeg > nowTime - visitTime[tg][nowTime]) + { + ansBeg = visitTime[tg][nowTime], ansEnd = nowTime; + } + } + } + printf("Scenario %d\n", k + 1); + if (ansEnd != 1e9) + { + printf("Departure %04d %s\nArrival %04d %s\n\n", + ansBeg / 60 * 100 + ansBeg % 60, + cityName[sr], + ansEnd / 60 * 100 + ansEnd % 60, + cityName[tg]); + } + else + { + puts("No connection\n"); + } + } + return 0; +} diff --git a/10040.cpp b/10040.cpp new file mode 100644 index 0000000..fb512be --- /dev/null +++ b/10040.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 2097152 + +int data[N] = {0}; +bool used[N]; + +inline void construct(int n) +{ + int def = (1 << n); + int div = (1 << n); + int now = div - 1; + fill(used, used + (1 << n), false); + used[now] = true; + REP(i, (1 << n) - n, (1 << n)) + { + data[i] |= def; + now = (now * 2) % div; + used[now] = true; + } + REP(i, n, (1 << n) - n) + { + now = (now * 2) % div; + if (used[now] == false)//put 0 + { + } + else//put 1 + { + now += 1; + data[i] |= def; + } + used[now] = true; + } +} + +int main() +{ + REP(i, 1, 22) + { + construct(i); + } + int te; + cin >> te; + while (te--) + { + int n, k; + cin >> n >> k; + int ans = 0; + int def = (1 << n); + rep(i, n) + { + int now = (k + i) % def; + ans *= 2; + if ((data[now] & def) == 0) + ; + else + { + ans++; + } + } + cout << ans << endl; + } + return 0; +} diff --git a/10041.cpp b/10041.cpp index 2b17563..36ec0f5 100644 --- a/10041.cpp +++ b/10041.cpp @@ -1,22 +1,49 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int test; - scanf("%d",&test); - while(test--){ - int r,i; - scanf("%d",&r); - int relative[r]; - for(i=0;i + +using namespace std; + +#define MAXN 30001 + +int RELATIVE[MAXN]; +long long DIFF, MID; + +int sf(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +void COM_DIFF(int N) +{ + int i; + DIFF = 0; + for (i = 0; i < N; i++) + { + DIFF += abs(RELATIVE[i] - MID); + } + printf("%lld\n", DIFF); +} + +int main() +{ + int N, i, kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%d", &RELATIVE[i]); + } + qsort(RELATIVE, N, sizeof(int), sf); + int k = N / 2; + if (N % 2 == 0) + { + MID = (RELATIVE[k - 1] + RELATIVE[k]) / 2; + } + else + { + MID = RELATIVE[k]; + } + COM_DIFF(N); + } + return 0; +} diff --git a/10042.cpp b/10042.cpp new file mode 100644 index 0000000..02589d5 --- /dev/null +++ b/10042.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +bool is_prime(int n) +{ + if (n == 2) + { + return true; + } + if (n % 2 == 0) + { + return false; + } + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; + } + } + return true; +} + +int digitsum(int n) +{ + int result = 0; + while (n) + { + result += n % 10; + n /= 10; + } + return result; +} + +int main() +{ + int t, n, tmp; + int sum1, sum2; + vector primes; + primes.push_back(2); + for (int i = 3; i < 35000; i += 2) + { + if (is_prime(i)) + { + primes.push_back(i); + } + } + cin >> t; + while (t--) + { + cin >> n; + while (++n) + { + if (is_prime(n)) + { + continue; + } + sum1 = digitsum(n); + tmp = n; + sum2 = 0; + for (int i = 0, sz = primes.size(); i < sz; i++) + { + if (tmp == 1 || is_prime(tmp)) + { + if (tmp != 1) + { + sum2 += digitsum(tmp); + } + break; + } + if (tmp % primes[i] == 0) + { + while (tmp % primes[i] == 0) + { + sum2 += digitsum(primes[i]); + tmp /= primes[i]; + } + } + } + if (sum1 == sum2) + { + cout << n << endl; + break; + } + } + } + return 0; +} diff --git a/10043.cpp b/10043.cpp new file mode 100644 index 0000000..9271e55 --- /dev/null +++ b/10043.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 10001 +#define TREE 2002 + +int is_exist_x[10001]; +int is_exist_y[10001]; +int x[TREE]; +int y[TREE]; +int inx[TREE]; +int iny[TREE]; +int mapping[TREE]; +int list_up[TREE]; +int list_down[TREE]; +vector y_at_x[TREE]; // [(x,y)] +int t_list_up[TREE], t_list_down[TREE]; +int t_mapping[TREE]; + +int solve(int r, int c, int maxheight) +{ + int ret = 0; + for (int i = c - 1; i > 0; i--) + { + int c_width = x[i] - x[0]; + int cut = i; + c_width = x[cut] - x[0]; + rep(j, y_at_x[cut].size()) + { + int now = y_at_x[cut][j]; + mapping[now]--; + if (mapping[now] == 0) + { + list_up[list_down[now]] = list_up[now]; + list_down[list_up[now]] = list_down[now]; + maxheight = max(maxheight, y[list_up[now]] - y[list_down[now]]); + ret = max(ret, maxheight * c_width); + } + } + int tmp_height = maxheight; + ret = max(ret, c_width * tmp_height); + // copy list + rep(j, r) + { + t_mapping[j] = mapping[j]; + t_list_up[j] = list_up[j]; + t_list_down[j] = list_down[j]; + } + // check all value + REP(j, 0, i) + { + c_width = x[i] - x[j]; + rep(k, y_at_x[j].size()) + { + int now = y_at_x[j][k]; + t_mapping[now]--; + if (t_mapping[now] == 0) + { + t_list_up[t_list_down[now]] = t_list_up[now]; + t_list_down[t_list_up[now]] = t_list_down[now]; + tmp_height = max(tmp_height, y[t_list_up[now]] - y[t_list_down[now]]); + ret = max(ret, tmp_height * c_width); + } + } + } + } + return ret; +} +inline void make_list(int r) +{ + list_up[0] = 1; + list_down[0] = -1; + REP(i, 1, r - 1) + { + list_up[i] = i + 1; + list_down[i] = i - 1; + } + list_up[r - 1] = -1; + list_down[r - 1] = r - 2; + return; +} +inline int calc_maxheight(int r) +{ + int maxheight = 0; + rep(i, r - 1) maxheight = max(maxheight, y[i + 1] - y[i]); + return maxheight; +} +void put_tree(int k, int sx, int sy, int dx, int dy, int height, int width, int &p) +{ + rep(i, k) + { + if (sx != 0 && sy != 0 && sx != width && sy != height) + { + is_exist_x[sx] = true; + is_exist_y[sy]++; + inx[p] = sx; + iny[p++] = sy; + } + sx += dx; + sy += dy; + } +} +void construct_data(int &r, int &c, int &p, int height, int width) +{ + r = 0; + c = 0; + x[c++] = 0; + y[r++] = 0; + rep(i, 10001)// is_exist changes to point index + { + if (is_exist_x[i] == true) + { + is_exist_x[i] = c; + x[c++] = i; + } + if (is_exist_y[i] > 0) + { + mapping[r] = is_exist_y[i]; + is_exist_y[i] = r; + y[r++] = i; + } + } + y[r++] = height; + x[c++] = width; + rep(i, c) y_at_x[i].clear(); + rep(i, p) + { + y_at_x[is_exist_x[inx[i]]].push_back(is_exist_y[iny[i]]); + assert(is_exist_y[iny[i]] != 0); + } + rep(i, c) sort(y_at_x[i].begin(), y_at_x[i].end()); +} + +int main() +{ + int width, height; + int te; + cin >> te; + while (te--) + { + rep(i, 10001) is_exist_x[i] = is_exist_y[i] = 0; + cin >> width >> height; + int p = 0; + int r = 0, c = 0; + while (true) + { + int k, x, y, dx, dy; + cin >> k; + if (k == 0) + { + break; + } + cin >> x >> y; + if (k > 1) + { + cin >> dx >> dy; + } + put_tree(k, x, y, dx, dy, height, width, p); + } + construct_data(r, c, p, height, width); + make_list(r); + int maxheight = calc_maxheight(r); + cout << solve(r, c, maxheight) << endl; + } + return 0; +} diff --git a/10044.cpp b/10044.cpp new file mode 100644 index 0000000..9f0c509 --- /dev/null +++ b/10044.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define MAX 5000 + +vector G[MAX]; +int n, m; +bool V[MAX]; +map A; + +struct Step +{ + int x, v; + Step() {} + Step(int x, int v) : x(x), v(v) {} +}; + +queue Q; + +int author(const string &a) +{ + if (A.find(a) != A.end()) + { + return A[a]; + } + else + { + return A[a] = A.size() - 1; + } +} + +char C[MAX]; +void parseAuthors(const string &s) +{ + vector TA; + int commas = 0, chars = 0; + for (int i = 0; i < s.size(); i++) + { + char c = s[i]; + if (chars == 0 && c == ' ') + { + continue; + } + if ((c == ',' || c == ':') && ++commas == 2) + { + TA.push_back(author(string(C, chars))); + chars = commas = 0; + } + else + { + C[chars++] = c; + } + } + for (int i = 0; i < TA.size(); i++) + { + for (int j = i + 1; j < TA.size(); j++) + { + G[TA[i]].push_back(TA[j]); + G[TA[j]].push_back(TA[i]); + } + } +} + +int main() +{ + string s; + int t = 0, tt; + cin >> tt; + while (t++ < tt) + { + cin >> n >> m; + memset(G, 0, sizeof(G)); + A.clear(); + getline(cin, s); + while (n--) + { + getline(cin, s); + parseAuthors(s); + } + cout << "Scenario " << t << endl; + for (int i = 0; i < m; i++) + { + bool stop; + memset(V, 0, sizeof(V)); + getline(cin, s); + int b = author(s); + Q = queue(); + Q.push(Step(author("Erdos, P."), 0)); + bool found = false; + while (!Q.empty()) + { + Step it = Q.front(); + Q.pop(); + if (it.x == b) + { + cout << s << " " << it.v << endl; + found = true; + break; + } + V[it.x] = true; + for (int i = 0; i < G[it.x].size(); i++) + if (!V[G[it.x][i]]) + { + Q.push(Step(G[it.x][i], it.v + 1)); + } + } + if (!found) + { + cout << s << " infinity" << endl; + } + } + } + return 0; +} diff --git a/10045.cpp b/10045.cpp new file mode 100644 index 0000000..099a0a6 --- /dev/null +++ b/10045.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +char s[1010]; + +struct TStrList +{ + int len; + char *s; +}; + +int exist(char *s, struct TStrList *list, int n) +{ + int i; + for (i = 0; i < n; i++) + if (!strcmp(s, list->s + i * 11)) + { + return 1; + } + return 0; +} + +int main() +{ + int i, j, k, N; + struct TStrList *cur, *prev; + char *p; + scanf("%d\n", &N); + while (N-- > 0) + { + gets(s); + prev = new TStrList; + prev->len = 1; + prev->s = new char[11]; + prev->s[0] = 0; + p = s; + while (*p) + { + cur = new TStrList; + cur->s = (char *)malloc(11 * prev->len * 2); + j = 0; + for (i = 0; i < prev->len; i++) + { + if (prev->s[i * 11] == *p) + { + strcpy(cur->s + 11 * j, prev->s + 11 * i + 1); + if (!exist(cur->s + j * 11, cur, j)) + { + j++; + } + } + k = strlen(prev->s + i * 11); + if (k < 10) + { + strcpy(cur->s + j * 11, prev->s + i * 11); + cur->s[j * 11 + k++] = *p; + cur->s[j * 11 + k++] = 0; + if (!exist(cur->s + j * 11, cur, j)) + { + j++; + } + } + } + cur->len = j; + /* + printf("->"); + for(i=0;ilen;i++) + printf("\"%s\" ",cur->s+i*11); + printf("\n"); + */ + // printf("%d\n",cur->len); + prev = cur; + p++; + } + if (prev->len == 0) + { + printf("Not consistent with the theory\n"); + } + else + { + for (i = j = 0; (i < prev->len) && !j; i++) + { + if (cur->s[i * 11] == 0) + { + j = 1; + } + } + if (j) + { + printf("An echo string with buffer size ten\n"); + } + else + { + printf("Not an echo string, but still consistent with the theory\n"); + } + } + } + return 0; +} diff --git a/10047.cpp b/10047.cpp index 28c0f0e..a9dad35 100644 --- a/10047.cpp +++ b/10047.cpp @@ -1,44 +1,101 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; +int nrows, ncols, endx, endy, map_[32][32], dist[32768], queue_[32768], head, tail; + +int state(int y, int x, int d, int c) +{ + return (y << 10) | (x << 5) | (d << 3) | c; +} -int main() { - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - while (true) { - int M, N; - cin >> M >> N; - if (M == 0 && N == 0) break; - string mat[M]; - FOI(i, 0, M-1) cin >> mat[i]; - +void push(int s, int d) +{ + if (dist[s] == 0) + { + dist[s] = d; + queue_[tail++] = s; } - return 0; } +int solve() +{ + int i, j, k, y, x, d, c; + memset(map_, 1, sizeof(map_)); + memset(dist, 0, sizeof(dist)); + for (i = 1; i <= nrows; i++) + { + for (j = 1; j <= ncols && (k = getchar()) != EOF;) + { + if (k == '.') + { + map_[i][j++] = 0; + } + else if (k == '#') + { + map_[i][j++] = 1; + } + else if (k == 'S') + { + queue_[0] = state(i, j, 0, 0); + map_[i][j++] = 0; + } + else if (k == 'T') + { + endy = i; + endx = j; + map_[i][j++] = 0; + } + } + } + dist[queue_[0]] = 1; + head = 0; + tail = 1; + while (head < tail) + { + k = queue_[head++]; + y = k >> 10; + x = (k >> 5) & 0x1F; + d = (k >> 3) & 3; + c = k & 7; + for (i = -1; i <= 1; i += 2) + { + push(state(y, x, (d + 4 + i) % 4, c), dist[k] + 1); + } + y += dy[d]; + x += dx[d]; + if (map_[y][x] == 0) + { + push(state(y, x, d, (c + 1) % 5), dist[k] + 1); + } + } + for (i = 0, j = 0; i < 4; i++) + { + k = state(endy, endx, i, 0); + if (dist[k] != 0 && (j == 0 || dist[k] < j)) + { + j = dist[k]; + } + } + return (j - 1); +} + +int main() +{ + int r, t; + for (t = 1; scanf("%d %d", &nrows, &ncols) == 2 && nrows > 0; t++) + { + printf("%sCase #%d\n", (t == 1) ? "" : "\n", t); + r = solve(); + if (r < 0) + { + printf("destination not reachable\n"); + } + else + { + printf("minimum time = %d sec\n", r); + } + } + return 0; +} diff --git a/10048.cpp b/10048.cpp index 0b8e5d5..0340da4 100644 --- a/10048.cpp +++ b/10048.cpp @@ -1,78 +1,80 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +#define INF 21474836 +#define MAXN 102 + +int N, M, Q, A[MAXN][MAXN]; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + A[i][j] = MIN(A[i][j], MAX(A[i][k], A[k][j])); + } + } + } +} +void Cal() +{ + int a, b; + Floyd(); + while (Q--) + { + scanf("%d%d", &a, &b); + if (A[a][b] == INF) + { + printf("no path\n"); + } + else + { + printf("%d\n", A[a][b]); + } + } +} +void INI() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + A[i][j] = INF; + } + A[i][i] = 0; + } +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - int C, S, Q; - scanf("%d%d%d", &C, &S, &Q); - if (C == 0 && S == 0 && Q == 0) +int main() +{ + int i, kase = 1; + int a, b, c; + while (scanf("%d%d%d", &N, &M, &Q) == 3) + { + if (!N && !M && !Q) + { break; - int mat[C][C]; - int i, j, k; - FOI(i, 0, C-1) - FOI(j, 0, C-1) - mat[i][j] = INF; - FOI(i, 1, S){ - int C1, C2, D; - scanf("%d%d%d", &C1, &C2, &D); - --C1; --C2; - mat[C1][C2] = D; - mat[C2][C1] = D; } - FOI(k, 0, C-1) - FOI(i, 0, C-1) - FOI(j, 0, C-1) - mat[i][j] = min(mat[i][j], max(mat[i][k], mat[k][j])); - if (t > 1) - printf("\n"); - printf("Case #%d\n", t); - while (Q--){ - int C1, C2; - scanf("%d%d", &C1, &C2); - --C1; --C2; - if (mat[C1][C2] == INF) - printf("no path\n"); - else - printf("%d\n", mat[C1][C2]); + INI(); + for (i = 0; i < M; i++) + { + scanf("%d%d%d", &a, &b, &c); + A[a][b] = A[b][a] = c; + } + if (kase > 1) + { + putchar('\n'); } + printf("Case #%d\n", kase++); + Cal(); } return 0; } - diff --git a/10049.cpp b/10049.cpp new file mode 100644 index 0000000..8d386ad --- /dev/null +++ b/10049.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define MAX 4387500 + +typedef long long ss; + +ss F[MAX + 3], G[MAX + 3]; + +void Gen() +{ + ss i, a, b, sum = 1; + G[1] = F[1] = 1; + for (i = 2; sum <= 2000000000; i++) + { + a = F[i - 1]; + a = F[a]; + b = i - a; + b = F[b] + 1; + F[i] = b; + sum += b; + G[i] = sum; + } +} + +// bin srch +ss Find(ss key) +{ + ss lo = 1, up = 673366; + ss mid = (lo + up) / 2; + while (lo < up && G[mid] != key) + { + if (key > G[mid]) + { + lo = mid + 1; + } + else + { + if (G[mid - 1] < key) + { + return mid; + } + else + { + up = mid - 1; + } + } + mid = (lo + up) / 2; + } + return mid; +} + +int main() +{ + Gen(); + ss n; + while (scanf("%lld", &n) == 1) + { + if (n == 0) + { + break; + } + printf("%lld\n", Find(n)); + } + return 0; +} diff --git a/10050.cpp b/10050.cpp index d951cb9..acf09ac 100644 --- a/10050.cpp +++ b/10050.cpp @@ -1,32 +1,43 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int test; - scanf("%d",&test); - while(test--){ - int n,p,i,j; - scanf("%d",&n); - int day[3651]={0}; - scanf("%d",&p); - for(i=1;i<=p;i++){ - int hpar; - scanf("%d",&hpar); - for(j=hpar;j<=n;j+=hpar) - day[j]=1; - } - for(i=6;i<=n;i+=7){ - day[i]=0; - day[i+1]=0; - } - int cnt=0; - for(i=1;i<=n;i++){ - if(day[i]==1) - cnt++; - } - printf("%d\n",cnt); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int t, n, p, h; + cin >> t; + while (t--) + { + cin >> n; + char days[n + 1]; + for (int i = 1; i <= n; i++) + { + days[i] = '0'; + } + cin >> p; + while (p--) + { + cin >> h; + int i = 1; + while (true) + { + if (i * h > n) + { + break; + } + days[i * h] = '1'; + i++; + } + } + int result = 0; + for (int i = 1; i <= n; i++) + { + if (days[i] == '1' && ((i - 1) % 7 != 5) && ((i - 1) % 7 != 6)) + { + result++; + } + } + cout << result << endl; + } + return 0; +} diff --git a/10051.cpp b/10051.cpp new file mode 100644 index 0000000..c455a90 --- /dev/null +++ b/10051.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define MAXN 505 + +int A[MAXN][MAXN], F[MAXN][MAXN]; +int Kase, N; +char Fase[6][7] = {"front", "back", "left", "right", "top", "bottom"}; + +struct SS +{ + int row; + int col; +} V[MAXN][MAXN]; +void PrintPath(int r, int c) +{ + int g; + if (V[r][c].row == -1) + { + g = c + 1; + if (c % 2) + { + g = c - 1; + } + printf("%d %s\n", r, Fase[g]); + return; + } + PrintPath(V[r][c].row, V[r][c].col); + g = c + 1; + if (c % 2) + { + g = c - 1; + } + printf("%d %s\n", r, Fase[g]); +} +void LIS() +{ + int i, j, max, largest = 0, k, m, g; + int pr, pc, str, stc; + for (i = 1; i <= N; i++) + { + for (j = 0; j < 6; j++) + { + max = 0; + pr = pc = -1; + for (k = i - 1; k >= 0; k--) + { + for (m = 0; m < 6; m++) + { + if (F[k][m] > max) + if (A[i][j] == A[k][m]) + { + pr = k; + pc = m; + max = F[k][m]; + } + } + } + g = j + 1; + if (j % 2) + { + g = j - 1; + } + F[i][g] = max + 1; + V[i][g].row = pr; + V[i][g].col = pc; + if (F[i][g] > largest) + { + largest = F[i][g]; + str = i; + stc = g; + } + } + } + printf("Case #%d\n", Kase++); + printf("%d\n", largest); + PrintPath(str, stc); +} + +int main() +{ + int i, j; + Kase = 1; + while (scanf("%d", &N) && N) + { + for (i = 1; i <= N; i++) + { + for (j = 0; j < 6; j++) + { + scanf("%d", &A[i][j]); + } + } + if (Kase > 1) + { + putchar('\n'); + } + LIS(); + } + return 0; +} diff --git a/10053.cpp b/10053.cpp new file mode 100644 index 0000000..ab844c3 --- /dev/null +++ b/10053.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define INF 999999999 +#define eps 0.000000001 +#define equal(a, b) (abs((a) - (b)) < eps) + +bool cy[10]; +double e_w[10], e_h[10], c_w[5], c_h[5], halfpi; +int m, n, min_, e_c[10], p[10], ans[10], g[5][10], c[5][10]; + +bool fits(long double cw, long double ch, long double ew, long double eh) +{ + long double l = 0.0, r = halfpi, m; + long double wp, hp; + if (cw < ew + eps) + { + l = r = 0.0; + } + else + { + if (ch > ew) + { + return 0; + } + while (1) + { + m = (l + r) * 0.5; + wp = cos(halfpi - m) * ch + cos(m) * cw; + if (equal(wp, ew)) + { + break; + } + if (wp > ew) + { + l = m; + } + else + { + r = m; + } + } + } + m = (l + r) * 0.5; + wp = cos(halfpi - m) * ch + cos(m) * cw; + hp = sin(halfpi - m) * ch + sin(m) * cw; + return (wp <= ew + eps) && (hp <= eh + eps); +} +void DFS(int depth, int sum) +{ + if (sum >= min_) + { + return; + } + if (depth == m) + { + min_ = sum; + for (int i = 0; i < n; i++) + { + ans[i] = p[i]; + } + } + else + { + for (int i = 0; i < n; i++) + if (g[depth][i] && p[i] == -1) + { + p[i] = depth; + DFS(depth + 1, sum + c[depth][i]); + p[i] = -1; + } + } +} +void solve(void) +{ + int i, j; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + { + g[i][j] = fits(c_w[i], c_h[i], e_w[j], e_h[j]) || + fits(c_h[i], c_w[i], e_w[j], e_h[j]); + c[i][j] = e_c[j]; + } + min_ = 999999999; + for (i = 0; i < n; i++) + { + p[i] = -1; + } + DFS(0, 0); + if (min_ == 999999999) + { + puts("cannot buy"); + } + else + { + for (i = 0; i < n; i++) + if (ans[i] != -1) + { + printf("%d\n", i + 1); + } + } +} + +int main() +{ + int i, c = 0; + halfpi = M_PI * 0.5; + while (scanf("%d %d", &m, &n) == 2) + { + if (m == 0 && n == 0) + { + break; + } + for (i = 0; i < m; i++) + { + scanf("%lf %lf", &c_w[i], &c_h[i]); + } + for (i = 0; i < n; i++) + { + scanf("%lf %lf %d", &e_w[i], &e_h[i], &e_c[i]); + } + if (c++) + { + printf("\n"); + } + printf("Case #%d\n", c); + solve(); + } + return 0; +} diff --git a/10054.cpp b/10054.cpp new file mode 100644 index 0000000..34fe81e --- /dev/null +++ b/10054.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +const int MAXN = 51; + +bool visited[MAXN]; +int n, adj[MAXN][MAXN], degree[MAXN]; +deque path; + +void visit_component(int u) +{ + if (visited[u]) + { + return; + } + visited[u] = true; + for (int v = 0; v < n; ++v) + { + if (adj[u][v]) + { + visit_component(v); + } + } +} + +void euler(int u) +{ + for (int v = 0; v < n; ++v) + { + if (adj[u][v]) + { + adj[u][v]--; + adj[v][u]--; + euler(v); + } + } + path.push_front(u); +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + if (t > 1) + { + printf("\n"); + } + printf("Case #%d\n", t); + + bzero(visited, sizeof visited); + bzero(adj, sizeof adj); + bzero(degree, sizeof degree); + path.clear(); + n = 0; + + int edges; + for (scanf("%d", &edges); edges--;) + { + int u, v; + scanf("%d %d", &u, &v); + n = max(n, u + 1); + n = max(n, v + 1); + degree[u]++; + degree[v]++; + adj[u][v]++; + adj[v][u]++; + } + + for (int i = 0; i < n; ++i) + { + if (degree[i] > 0) + { + visit_component(i); + break; + } + } + + bool impossible = false; + for (int i = 0; i < n; ++i) + { + if (degree[i] > 0 && !visited[i] || degree[i] % 2 == 1) + { + impossible = true; + break; + } + } + + if (!impossible) + { + for (int i = 0; i < n; ++i) + { + if (degree[i] > 0) + { + euler(i); + break; + } + } + } + + if (impossible) + { + printf("some beads may be lost\n"); + } + else + { + for (int i = 0; i < path.size() - 1; ++i) + { + printf("%d %d\n", path[i], path[i + 1]); + } + } + } + return 0; +} diff --git a/10055.cpp b/10055.cpp index acc57f1..62485da 100644 --- a/10055.cpp +++ b/10055.cpp @@ -1,11 +1,18 @@ -#include - -int main() { - int p,q,r; - while (scanf("%d %d",&p,&q) != EOF){ - r=q-p; - if (r < 0) r *= -1; - printf("%ld\n",r); - } - return 0; -} +#include + +using namespace std; + +long long myabs(long long in) +{ + return (in >= 0) ? in : -in; +} + +int main() +{ + long long hashmat, enemy; + while (scanf("%lld %lld ", &hashmat, &enemy) == 2) + { + printf("%lld\n", myabs(enemy - hashmat)); + } + return 0; +} diff --git a/10056.cpp b/10056.cpp new file mode 100644 index 0000000..6f204e7 --- /dev/null +++ b/10056.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define maxn 1000 +double P, Ind[maxn + 2]; +int C, Ith; + +void Gen() +{ + double p = 1 - P, t = 1; + int i; + Ind[0] = 1; + for (i = 1; i <= C; i++) + { + t *= p; + Ind[i] = t; + } +} +void Cal() +{ + double u, d; + if ((P - 0) < 1E-5) + { + printf("0.0000\n"); + return; + } + /* if(C == 1 && Ith == 1) { + printf("1.0000\n"); + return; + }*/ + d = 1 - (Ind[C]); + u = P * Ind[Ith - 1]; + printf("%.4lf\n", (u / d)); +} + +int main() +{ + int ks; + scanf("%d", &ks); + while (ks--) + { + scanf("%d%lf%d", &C, &P, &Ith); + Gen(); + Cal(); + } + return 0; +} diff --git a/10057.cpp b/10057.cpp new file mode 100644 index 0000000..625034d --- /dev/null +++ b/10057.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define maxn 65536 + +int F[maxn + 2], N; +vector V; + +void Cal() +{ + int n, m, c; + sort(V.begin(), V.end()); + if (N % 2) + { + cout << V[N / 2]; + cout << " " << F[V[N / 2]] << " " + << "1\n"; + } + else + { + n = V[N / 2]; + m = V[N / 2 - 1]; + c = F[n]; + if (n != m) + { + c = F[n] + F[m]; + } + cout << m << " "; + cout << c << " "; + cout << n - m + 1 << endl; + } +} + +void ReSet() +{ + int i; + for (i = 0; i < N; i++) + { + F[V[i]] = 0; + } + V.clear(); +} + +int main() +{ + int n, m; + while (cin >> N && N) + { + m = N; + while (m--) + { + cin >> n; + F[n]++; + V.push_back(n); + } + Cal(); + ReSet(); + } + return 0; +} diff --git a/10058.cpp b/10058.cpp new file mode 100644 index 0000000..6728b8c --- /dev/null +++ b/10058.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +char *tokp; +int tok, err; + +int next() +{ + static char *n[] = {"tom", "jerry", "goofy", "mickey", "jimmy", "dog", "cat", "mouse", NULL}; + static char *v[] = {"hate", "love", "know", "like", "hates", "loved", "knows", "likes", NULL}; + static char s[1024]; + int i; + if (err) + { + return (tok = 0); + } + while (*tokp && !isalpha(*tokp) && *tokp != ',') + { + tokp++; + } + if (*tokp == '\0') + { + return (tok = 0); + } + if (*tokp == ',') + { + return (tok = *tokp++); + } + for (i = 0; *tokp && isalpha(*tokp); tokp++) + { + s[i++] = *tokp; + } + s[i] = 0; + if (strcmp(s, "and") == 0) + { + return (tok = '&'); + } + if (strcmp(s, "a") == 0 || strcmp(s, "the") == 0) + { + return (tok = 'a'); + } + for (i = 0; v[i]; i++) + if (strcmp(s, v[i]) == 0) + { + return (tok = 'v'); + } + for (i = 0; n[i]; i++) + if (strcmp(s, n[i]) == 0) + { + return (tok = 'n'); + } + err = 1; + return (tok = 0); +} + +int action() +{ + for (;;) + { + if (tok == 'a') + { + next(); + } + if (tok != 'n') + { + return 0; + } + next(); + if (tok == '&') + { + next(); + } + else + { + break; + } + } + if (tok != 'v') + { + return 0; + } + next(); + for (;;) + { + if (tok == 'a') + { + next(); + } + if (tok != 'n') + { + return 0; + } + next(); + if (tok == '&') + { + next(); + } + else + { + break; + } + } + return 1; +} + +int solve() +{ + while (tok != 0) + { + if (!action()) + { + return 0; + } + if (tok == ',') + { + next(); + } + else if (tok == 0) + { + return 1; + } + } + return 0; +} + +int main() +{ + static char s[65536]; + while (gets(s)) + { + err = 0; + tokp = s; + next(); + printf((solve() && err == 0) ? "YES I WILL\n" : "NO I WON'T\n"); + } + return 0; +} diff --git a/1006.cpp b/1006.cpp new file mode 100644 index 0000000..6856f36 --- /dev/null +++ b/1006.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +const int MAXN = 500; +const int INF = INT_MAX; + +struct edge +{ + int u, v, wt; +}; + +struct node +{ + int S, T; + bool operator<(const node &rhs) const + { + return S < rhs.S; + } +}; + +int n, m; +int Size[MAXN + 10]; +vector A[MAXN + 10]; +int res[MAXN + 10]; + +int work_time(vector &pro, int blo) +{ + if (pro.size() == 0) + { + return 0; + } + if (Size[blo] < pro[0].S) + { + return INF; + } + int tmp = upper_bound(pro.begin(), pro.end(), (node){ + Size[blo], 0}) - + pro.begin() - 1; + return pro[tmp].T; +} + +struct KM_algorithm +{ + vector G[MAXN + 10]; + vector Edge; + + int Lx[MAXN + 10], Ly[MAXN + 10], Slack[MAXN + 10]; + int nx, ny, Left[MAXN + 10]; + bool S[MAXN + 10], T[MAXN + 10]; + + void init(int nx, int ny) + { + this->nx = nx, this->ny = ny; + for (int u = 1; u <= nx; u++) + { + G[u].clear(); + } + Edge.clear(); + } + + void Addedge(int u, int v, int wt) + { + Edge.push_back((edge){ + u, v, wt}); + G[u].push_back(Edge.size() - 1); + } + + bool match(int u) + { + S[u] = 1; + for (int i = 0; i < G[u].size(); i++) + { + edge &e = Edge[G[u][i]]; + if (!T[e.v]) + { + int t = Lx[e.u] + Ly[e.v] - e.wt; + if (t == 0) + { + T[e.v] = 1; + if (!Left[e.v] || match(Left[e.v])) + { + Left[e.v] = e.u; + return 1; + } + } + Slack[e.v] = min(Slack[e.v], t); + } + } + return 0; + } + + void Update() + { + int i, a = INF; + for (i = 1; i <= ny; i++) + if (!T[i]) + { + a = min(a, Slack[i]); + } + for (i = 1; i <= ny; i++) + { + if (S[i]) + { + Lx[i] -= a; + } + if (T[i]) + { + Ly[i] += a; + } + else + { + Slack[i] -= a; + } + } + } + + int KM() + { + int i, ans = 0; + for (i = 1; i <= ny; i++) + { + Lx[i] = Ly[i] = Left[i] = 0; + } + for (i = 1; i <= nx; i++) + { + memset(Slack, 0x3f, sizeof(Slack)); + while (1) + { + memset(S, 0, sizeof(S)); + memset(T, 0, sizeof(T)); + if (match(i)) + { + break; + } + else + { + Update(); + } + } + } + for (i = 1; i <= ny; i++) + { + ans += Lx[Left[i]] + Ly[i]; + } + return -ans; + } + +} sp; + +void read_input() +{ + sp.init(n, m * n); + for (int i = 1; i <= m; i++) + { + scanf("%d", &Size[i]); + } + for (int i = 1, c; i <= n; i++) + { + scanf("%d", &c); + A[i].clear(); + for (int j = 1; j <= c; j++) + { + node t; + scanf("%d%d", &t.S, &t.T); + A[i].push_back(t); + } + for (int j = 1; j <= m; j++) + { + int tmp = work_time(A[i], j); + if (tmp == INF) + { + continue; + } + for (int k = 1; k <= n; k++) + { + sp.Addedge(i, (j - 1) * n + k, -k * tmp); + } + } + } +} + +int Start[MAXN + 10]; + +void work() +{ + int ans = sp.KM(); + printf("Average turnaround time = %.2lf\n", 1.0 * ans / n); + for (int i = 1; i <= m; i++) + { + for (int j = n; j >= 1; j--) + { + int v = (i - 1) * n + j; + if (sp.Left[v]) + { + res[sp.Left[v]] = i; + int Last = j == n ? 0 : sp.Left[v + 1]; + int tmp = work_time(A[Last], i); + Start[sp.Left[v]] = tmp + Start[Last]; + } + } + } + for (int i = 1; i <= n; i++) + { + printf("Program %d runs in region %d from %d to %d\n", i, res[i], Start[i], Start[i] + work_time(A[i], res[i])); + } +} + +int main() +{ + int t = 0; + while (scanf("%d%d",&m,&n) == 2 && (n||m)) + { + read_input(); + printf("Case %d\n", ++t); + work(); + puts(""); + } + return 0; +} diff --git a/10060.cpp b/10060.cpp new file mode 100644 index 0000000..de83fc3 --- /dev/null +++ b/10060.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const double PI = acos(-1.0f); +const double EPS = 1e-5; + +bool cmp(double a, double b) +{ + return fabs(a - b) > EPS; +} + +int main() +{ + double total_mat, area; + double R, T, thickness, x0, y0, xp, yp, x, y; + for (int n; scanf("%d", &n) == 1 && n;) + { + total_mat = 0; + for (int i = 0; i < n; ++i) + { + scanf("%lf%lf%lf", &thickness, &x0, &y0); + area = 0; + xp = x0; + yp = y0; + while (scanf("%lf%lf", &x, &y) == 2) + { + area += xp * y - yp * x; + xp = x; + yp = y; + if (!(cmp(x, x0) || cmp(y, y0))) + { + break; + } + } + total_mat += fabs(area) * thickness; + } + scanf("%lf%lf", &R, &T); + printf("%.0lf\n", floor(total_mat / (2 * PI * R * R * T))); + } + return 0; +} diff --git a/10061.cpp b/10061.cpp new file mode 100644 index 0000000..40307f8 --- /dev/null +++ b/10061.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define maxn 1048579 + +char sv[maxn + 2]; +int N, B; +vector P; + +void Primetable() +{ + int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j <= maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + P.push_back(2); + for (i = 3; i < maxn; i += 2) + if (sv[i] == 0) + { + P.push_back(i); + } +} +int times(int n) +{ + int i, sum = 0; + for (i = n; i <= N; i *= n) + { + sum += N / i; + } + return sum; +} +int Zeros() +{ + int i, k, n = B, zero = 2147483647, t, a, m; + for (i = 0; i < P.size() && n > 1; i++) + { + m = P[i]; + m *= m; + if (m > n) + { + break; + } + if (n % P[i] == 0) + { + k = 0; + while (n % P[i] == 0) + { + k++; + n /= P[i]; + } + t = times(P[i]); + a = t / k; + if (zero > a) + { + zero = a; + } + } + } + if (n > 1) + { + t = times(n); + } + if (zero > t) + { + zero = t; + } + return zero; +} +int digit() +{ + int i, dig; + double sum = 0; + for (i = 1; i <= N; i++) + { + sum += log10(i) / log10(B); + } + sum += 1e-5; + dig = ceil(sum); + return dig; +} +void Cal() +{ + int z, dig; + if (N == 0) + { + cout << "0 " + << "1\n"; + return; + } + if (N == 1) + { + cout << "0 " + << "1\n"; + return; + } + z = Zeros(); + dig = digit(); + cout << z << " " << dig << endl; +} + +int main() +{ + Primetable(); + while (cin >> N >> B) + { + Cal(); + } + return 0; +} diff --git a/10062.cpp b/10062.cpp index c479d19..2f843aa 100644 --- a/10062.cpp +++ b/10062.cpp @@ -1,43 +1,53 @@ -# include -using namespace std; - -struct freq -{ - char ch; - long int X ; -}; - -bool compare(freq A, freq B) -{ - if (A.X>B.X) - return false; - else if (A.XB.ch); -} - -int main() -{ - char str[1005]; - int start=0; - while(gets(str)!=NULL) - { - - if (start) - cout<0) - cout<<(int)arr[i].ch<<" "< + +using namespace std; + +char str[1005]; +int sum[129]; + +typedef struct +{ + int ascii, cnt; +} kind; +kind data[128]; +int cmp(kind a, kind b) +{ + if (a.cnt != b.cnt) + { + return a.cnt < b.cnt; + } + return a.ascii > b.ascii; +} + +int main() +{ + int total = 0; + while (gets(str)) + { + if (total++) + { + printf("\n"); + } + memset(sum, 0, sizeof(sum)); + for (int i = 0; str[i]; ++i) + { + sum[str[i]]++; + } + int cnt = 0; + for (int i = 32; i < 128; ++i) + { + if (sum[i]) + { + data[cnt].ascii = i; + data[cnt].cnt = sum[i]; + cnt++; + } + } + sort(data, data + cnt, cmp); + for (int i = 0; i < cnt; ++i) + { + printf("%d %d\n", data[i].ascii, data[i].cnt); + } + } + return 0; +} diff --git a/10063.cpp b/10063.cpp new file mode 100644 index 0000000..85388e2 --- /dev/null +++ b/10063.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +char str[15], dummy[15]; +void Insert(char c, char ss[], int p) +{ + int i, l = strlen(ss); + strcpy(dummy, ss); + if (p == l) + { + dummy[l] = c; + dummy[l + 1] = NULL; + return; + } + for (i = l; i > p; i--) + { + dummy[i] = dummy[i - 1]; + } + dummy[p] = c; + dummy[l + 1] = NULL; +} +void recur(int level, char ss[]) +{ + char temp[100]; + strcpy(temp, ss); + if (level == strlen(str)) + { + puts(ss); + return; + } + for (int i = 0; i <= level; i++) + { + Insert(str[level], temp, i); + recur(level + 1, dummy); + strcpy(dummy, temp); + } +} + +int main() +{ + int f = 0; + char temp[15]; + while (scanf("%s", str) != EOF) + { + if (f++) + { + putchar('\n'); + } + temp[0] = str[0]; + temp[1] = NULL; + recur(1, temp); + } + return 0; +} diff --git a/10065.cpp b/10065.cpp new file mode 100644 index 0000000..899e666 --- /dev/null +++ b/10065.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define MAXPOLY 105 + +struct point +{ + int x; + int y; +}; + +struct polygon +{ + int vertexNumber; + point vertex[MAXPOLY]; +}; + +double area(point vertex[], int vertexNumber) +{ + double total = 0.0; + for (int i = 0; i < vertexNumber; i++) + { + int j = (i + 1) % vertexNumber; + total += (vertex[i].x * vertex[j].y - vertex[j].x * vertex[i].y); + } + return fabs(total / 2.0); +} + +int crossProduct(point first, point second, point third) +{ + return (second.x - first.x) * (third.y - first.y) - + (second.y - first.y) * (third.x - first.x); +} + +bool left_lower(point first, point second) +{ + if (first.x == second.x) + { + return first.y < second.y; + } + else + { + return first.x < second.x; + } +} + +void convex_hull(point vertex[], int vertexNumber, polygon &container) +{ + if (vertexNumber <= 3) + { + for (int i = 0; i < vertexNumber; i++) + { + container.vertex[i] = vertex[i]; + } + container.vertexNumber = vertexNumber; + return; + } + sort(vertex, vertex + vertexNumber, left_lower); + point upper[MAXPOLY], lower[MAXPOLY]; + int top; + upper[0] = vertex[0]; + upper[1] = vertex[1]; + top = 2; + for (int i = 2; i < vertexNumber; i++) + { + upper[top] = vertex[i]; + while (top >= 2 && crossProduct(upper[top - 2], upper[top - 1], upper[top]) >= 0) + { + upper[top - 1] = upper[top]; + top--; + } + top++; + } + container.vertexNumber = 0; + for (int i = 0; i < top; i++) + { + container.vertex[container.vertexNumber++] = upper[i]; + } + lower[0] = vertex[vertexNumber - 1]; + lower[1] = vertex[vertexNumber - 2]; + top = 2; + for (int i = vertexNumber - 3; i >= 0; i--) + { + lower[top] = vertex[i]; + while (top >= 2 && crossProduct(lower[top - 2], lower[top - 1], lower[top]) >= 0) + { + lower[top - 1] = lower[top]; + top--; + } + top++; + } + for (int i = 1; i < top - 1; i++) + { + container.vertex[container.vertexNumber++] = lower[i]; + } +} + +int main(int ac, char *av[]) +{ + point tile[MAXPOLY]; + polygon container; + int vertexNumber, currentCase = 1; + cout.precision(2); + cout.setf(ios::fixed | ios::showpoint); + while (cin >> vertexNumber, vertexNumber) + { + for (int i = 0; i < vertexNumber; i++) + { + cin >> tile[i].x; + cin >> tile[i].y; + } + double used = area(tile, vertexNumber); + convex_hull(tile, vertexNumber, container); + cout << "Tile #" << currentCase++ << endl; + double all = area(container.vertex, container.vertexNumber); + double rate = (1.0 - used / all) * 100.0; + cout << "Wasted Space = " << rate << " %" << endl; + cout << endl; + } + return 0; +} diff --git a/10066.cpp b/10066.cpp index 8d6ba0f..5eb0179 100644 --- a/10066.cpp +++ b/10066.cpp @@ -1,34 +1,59 @@ -#include -#include -using namespace std; - -int main(){ - for(int test=1; ; test++){ - int m, n; - cin>>m>>n; - if(m==0 && n==0) - break; - int tower[m+1][n+1]; - int row[m+1], col[n+1]; - int i, j; - for(i=0; i>row[i]; - for(i=1; i>col[i]; - for(i=1; i + +using namespace std; + +const int MAXSIZE = 100; + +int N1, N2; +int T1[MAXSIZE + 1], T2[MAXSIZE + 1]; +int lcs[MAXSIZE + 1][MAXSIZE + 1]; + +void input() +{ + for (int i = 1; i <= N1; i++) + { + scanf("%d", T1 + i); + } + for (int i = 1; i <= N2; i++) + { + scanf("%d", T2 + i); + } +} + +void solve(int kase) +{ + int i, j; + for (i = 0; i <= N1; i++) + { + lcs[i][0] = 0; + } + for (j = 0; j <= N2; j++) + { + lcs[0][j] = 0; + } + for (i = 1; i <= N1; i++) + for (j = 1; j <= N2; j++) + { + if (T1[i] == T2[j]) + { + lcs[i][j] = lcs[i - 1][j - 1] + 1; + } + else + { + lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]); + } + } + printf("Twin Towers #%d\n", kase); + printf("Number of Tiles : %d\n\n", lcs[N1][N2]); +} + +int main() +{ + int kase = 0; + while (EOF != scanf("%d%d", &N1, &N2) && N1) + { + input(); + solve(++kase); + } + return 0; +} diff --git a/10067.cpp b/10067.cpp new file mode 100644 index 0000000..739ee85 --- /dev/null +++ b/10067.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +#define MAXN 10000 + +int Tval, Q[MAXN], QH, QT, MAX; +struct ss +{ + int level; + char visit; +} V[MAXN]; +int Val(int a, int b, int c, int d) +{ + return a * 1000 + b * 100 + c * 10 + d; +} +int Push(int n, int l) +{ + if (V[n].visit == 'r') + { + return 0; + } + if (n == Tval) + { + return 1; + } + V[n].visit = 'r'; + V[n].level = l; + Q[QH++] = n; + QH %= MAXN; + return 0; +} +int Pop() +{ + int n; + n = Q[QT++]; + QT %= MAXN; + return n; +} +int IsEmpty() +{ + return QH == QT; +} +int PushAll(int a, int b, int c, int d, int lev) +{ + int val, i, j; + int x[] = {1, -1}; + char temp[100], dummy[100]; + val = Val(a, b, c, d); + sprintf(temp, "%04d", val); + strcpy(dummy, temp); + for (i = 0; i < 4; i++) + { + for (j = 0; j < 2; j++) + { + temp[i] = ((dummy[i] - '0' + x[j] + 10) % 10) + '0'; + if (Push(atoi(temp), lev)) + { + return 1; + } + } + strcpy(temp, dummy); + } + return 0; +} +void Ini() +{ + int i; + for (i = 0; i < MAXN; i++) + { + V[i].level = 0; + V[i].visit = 'w'; + } +} +void DoBfs(int a, int b, int c, int d) +{ + int p, q, r, s, val, lev, n; + char dummy[100]; + MAX = 21474873; + if (V[Tval].visit == 'r') + { + printf("-1\n"); + return; + } + QH = QT = 0; + val = Val(a, b, c, d); + V[val].level = 0; + Push(val, 0); + while (!IsEmpty()) + { + n = Pop(); + sprintf(dummy, "%04d", n); + p = dummy[0] - '0'; + q = dummy[1] - '0'; + r = dummy[2] - '0'; + s = dummy[3] - '0'; + lev = V[n].level + 1; + if (PushAll(p, q, r, s, lev)) + { + printf("%d\n", lev); + return; + } + } + printf("-1\n"); +} + +int main() +{ + int kase, i, r, f, t; + int sa, sb, sc, sd; + int a, b, c, d, v; + scanf("%d", &kase); + while (kase--) + { + Ini(); + f = 0; + scanf("%d%d%d%d", &sa, &sb, &sc, &sd); + scanf("%d%d%d%d", &a, &b, &c, &d); + Tval = Val(a, b, c, d); + t = Val(sa, sb, sc, sd); + scanf("%d", &r); + for (i = 0; i < r; i++) + { + scanf("%d%d%d%d", &a, &b, &c, &d); + v = Val(a, b, c, d); + V[v].visit = 'r'; + } + if (Tval == t && V[t].visit != 'r') + { + printf("0\n"); + continue; + } + DoBfs(sa, sb, sc, sd); + } + return 0; +} diff --git a/10068.cpp b/10068.cpp new file mode 100644 index 0000000..e0341cf --- /dev/null +++ b/10068.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +const int MAXN = 11; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; +char dstr[4][4] = {"E", "W", "S", "N"}; + +char sg[32][32]; +int R, C, N, WALK, W[1 << MAXN], Ci[MAXN], Pi[MAXN], inq[20][20][1 << MAXN]; + +struct State +{ + int d, step; + State *prev; + void init() + { + d = 0x3f3f3f3f, step = 0, prev = NULL; + } +}; +State dist[20][20][1 << MAXN]; + +void spfa(int sx, int sy, int ex, int ey) +{ + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + for (int k = (1 << N) - 1; k >= 0; k--) + { + dist[i][j][k].init(); + inq[i][j][k] = 0; + } + } + } + + for (int i = (1 << N); i >= 0; i--) + { + int sum = 0; + for (int j = 0; j < N; j++) + { + if ((i >> j) & 1) + { + sum += Ci[j]; + } + } + W[i] = sum; + } + + int tx, ty, ts, ss; + queue X, Y, S; + + dist[sx][sy][0].d = 0; + X.push(sx), Y.push(sy), S.push(0); + while (!X.empty()) + { + sx = X.front(), X.pop(); + sy = Y.front(), Y.pop(); + ss = S.front(), S.pop(); + inq[sx][sy][ss] = 0; + for (int i = 0; i < 4; i++) + { + tx = sx + dx[i], ty = sy + dy[i], ts = ss; + if (tx < 0 || ty < 0 || tx >= R || ty >= C) + { + continue; + } + if (sg[tx][ty] == '#') + { + continue; + } + if (dist[tx][ty][ts].d > dist[sx][sy][ss].d + W[ss] + WALK) + { + dist[tx][ty][ts].d = dist[sx][sy][ss].d + W[ss] + WALK; + dist[tx][ty][ts].prev = &dist[sx][sy][ss]; + dist[tx][ty][ts].step = 1 << i; + if (!inq[tx][ty][ts]) + { + inq[tx][ty][ts] = 1; + X.push(tx), Y.push(ty), S.push(ts); + } + } + if (sg[tx][ty] >= '0' && sg[tx][ty] <= '9') + { + ts = ss | (1 << (sg[tx][ty] - '0')); + if (dist[tx][ty][ts].d > dist[sx][sy][ss].d + W[ss] + WALK + Pi[sg[tx][ty] - '0']) + { + dist[tx][ty][ts].d = dist[sx][sy][ss].d + W[ss] + WALK + Pi[sg[tx][ty] - '0']; + dist[tx][ty][ts].prev = &dist[sx][sy][ss]; + dist[tx][ty][ts].step = (1 << i) | (1 << 4); + if (!inq[tx][ty][ts]) + { + inq[tx][ty][ts] = 1; + X.push(tx), Y.push(ty), S.push(ts); + } + } + } + } + } + if (dist[ex][ey][(1 << N) - 1].d == 0x3f3f3f3f) + { + printf("The hunt is impossible.\n"); + } + else + { + printf("Minimum energy required = %d cal\n", dist[ex][ey][(1 << N) - 1].d); + State *ptr = &dist[ex][ey][(1 << N) - 1]; + string way = ""; + while (ptr != NULL) + { + if (((ptr->step) >> 4) & 1) + { + way = "P" + way; + } + for (int i = 0; i < 4; i++) + { + if (((ptr->step) >> i) & 1) + { + way = dstr[i] + way; + } + } + ptr = ptr->prev; + } + printf("%s\n", way.c_str()); + } +} + +int main() +{ + int t = 0; + while (scanf("%d%d", &R, &C) == 2 && R) + { + for (int i = 0; i < R; i++) + { + scanf("%s", sg[i]); + } + scanf("%d", &WALK); + + int sx, sy, ex, ey; + N = 0; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + if (sg[i][j] == '*') + { + sg[i][j] = '0' + N; + scanf("%d %d", &Pi[N], &Ci[N]); + N++; + } + if (sg[i][j] == 'S') + { + sx = i, sy = j; + } + if (sg[i][j] == 'T') + { + ex = i, ey = j; + } + } + } + + printf("Hunt #%d\n", ++t); + spfa(sx, sy, ex, ey); + printf("\n"); + } + return 0; +} diff --git a/10069.cpp b/10069.cpp new file mode 100644 index 0000000..baa95f0 --- /dev/null +++ b/10069.cpp @@ -0,0 +1,290 @@ +#include + +using namespace std; + +#define MAXN 10003 +#define BASE 1000000 +#define MAX_DIGIT 70 + +typedef long long INT; + +struct BigNum +{ + INT sign, len, digit[MAX_DIGIT]; + void absAdd(BigNum, BigNum); + + void operator=(INT); //ok + void operator=(BigNum); //ok + void operator=(char *); //ok + void add(BigNum, INT); //ok- + void add(BigNum, BigNum);//ok- + void print(); //ok- + void update(); //ok- + void clear(); //ok- + BigNum() + { + clear(); + } +}; +void BigNum::operator=(INT x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + INT i; + clear(); + len = n.len; + sign = n.sign; + for (i = 0; i < len; ++i) + { + digit[i] = n.digit[i]; + } +} +void BigNum::operator=(char *s) +{ + INT i, v, p; + clear(); + p = 1; + i = strlen(s) - 1; + for (v = 0; i >= 0; i--) + { + if (s[i] == '-') + { + --i; + sign = 1; + break; + } + else if (s[i] == '+') + { + --i; + break; + } + if (s[i] < '0' || s[i] > '9') + { + continue; + } + v = v + (s[i] - '0') * p; + p *= 10; + if (p == BASE) + { + digit[len++] = v; + p = 1; + v = 0; + } + } + if (v) + { + digit[len++] = v; + } + update(); +} +void BigNum::absAdd(BigNum a, BigNum b) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < max(a.len, b.len); ++pos) + { + digit[pos] = a.digit[pos] + b.digit[pos] + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + if (carry != 0) + { + digit[max(a.len, b.len)] = carry; + len = max(a.len, b.len) + 1; + } + else + { + len = max(a.len, b.len); + } + update(); +} +void BigNum::add(BigNum a, INT n) +{ + BigNum t; + t = n; + add(a, t); +} +void BigNum::add(BigNum a, BigNum b) +{ + clear(); + absAdd(a, b); + sign = a.sign; +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::print() +{ + INT i; + if (sign) + { + printf("-"); + } + printf("%ld", digit[len - 1]); + for (i = len - 2; i >= 0; --i) + { + switch (BASE) + { + case 10: + printf("%ld", digit[i]); + break; + case 100: + printf("%02ld", digit[i]); + break; + case 1000: + printf("%03ld", digit[i]); + break; + case 10000: + printf("%04ld", digit[i]); + break; + case 100000: + printf("%05ld", digit[i]); + break; + case 1000000: + printf("%06ld", digit[i]); + break; + case 10000000: + printf("%07ld", digit[i]); + break; + case 100000000: + printf("%08ld", digit[i]); + break; + case 1000000000: + printf("%09ld", digit[i]); + break; + default: + break; + } + } +} +char X[MAXN], Z[120]; +BigNum Table[2][MAXN]; +int Equal() +{ + int i; + for (i = 1; X[i]; i++) + { + if (X[i] != Z[i]) + { + return 1; + } + } + return 0; +} +void Assaign(int x) +{ + int i, p = 0, j; + for (i = 0; i < 2; i++) + for (j = 0; j <= x; j++) + { + Table[i][j] = p; + } +} +void Dynamic(int x, int z) +{ + int i, j, ind1 = 0, ind2 = 1; + Assaign(x); + for (i = 1; i <= x; i++) + { + if (X[i] == Z[1]) + { + Table[1][i].add(Table[1][i], Table[1][i - 1]); + Table[1][i].add(Table[1][i], 1); + } + else + { + Table[1][i] = Table[1][i - 1]; + } + } + for (i = 2; i <= z; i++) + { + for (j = 1; j <= x; j++) + { + if (Z[i] == X[j]) + { + Table[ind1][j].add(Table[ind1][j - 1], Table[ind2][j - 1]); + } + else + { + Table[ind1][j] = Table[ind1][j - 1]; + } + } + ind1 = !ind1; + ind2 = !ind2; + } + Table[ind2][x].print(); + printf("\n"); +} +void Cal() +{ + int x, z; + for (x = 1; X[x]; x++) + ; + for (z = 1; Z[z]; z++) + ; + x--; + z--; + if (x == z) + { + if (!Equal()) + { + printf("1\n"); + } + else + { + printf("0\n"); + } + return; + } + if (x < z) + { + printf("0\n"); + return; + } + Dynamic(x, z); +} + +int main() +{ + int ks; + char ss[20]; + gets(ss); + sscanf(ss, "%d", &ks); + while (ks--) + { + gets(X + 1); + gets(Z + 1); + Cal(); + } + return 0; +} diff --git a/10070.cpp b/10070.cpp index c1776a6..4108651 100644 --- a/10070.cpp +++ b/10070.cpp @@ -1,27 +1,96 @@ -#include - -using namespace std; - -int main(){ - long long y=0; - while(cin>>y){ - long long a=0,b=0,c=0; - if(((y%400)==0) || (y%4==0 && y%100!=0)){ - a=1; - cout<<"This is leap year."< + +using namespace std; + +bool is_leap(const string &y) +{ + if (y.size() < 9) + { + stringstream ss; + ss << y; + int year; + ss >> year; + return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); + } + else + { + int last_two = int(y[y.size() - 2] - '0') * 10 + int(y[y.size() - 1] - '0'); + if (last_two == 0) + { + last_two = int(y[y.size() - 4] - '0') * 10 + int(y[y.size() - 3] - '0'); + return last_two % 4 == 0; + } + else + { + return last_two % 4 == 0; + } + } +} + +bool is_hulu(const string &y) +{ + int sum = 0; + if (y[y.size() - 1] != '5' && y[y.size() - 1] != '0') + { + return false; + } + for (int i = 0, sz = y.size(); i < sz; i++) + { + sum += int(y[i] - '0'); + } + return sum % 3 == 0; +} + +bool is_bulu(const string &y) +{ + if (y[y.size() - 1] != '5' && y[y.size() - 1] != '0') + { + return false; + } + int sum1, sum2; + sum1 = sum2 = 0; + for (int i = 0, sz = y.size(); i < sz; i += 2) + { + sum1 += int(y[i] - '0'); + } + for (int i = 1, sz = y.size(); i < sz; i += 2) + { + sum2 += int(y[i] - '0'); + } + return (sum1 - sum2) % 11 == 0; +} + +int main() +{ + string input; + int counter = 0; + bool leap, hulu, bulu; + while (getline(cin, input)) + { + leap = is_leap(input); + hulu = is_hulu(input); + bulu = is_bulu(input) && leap; + if (counter > 0) + { + cout << endl; + } + counter++; + if (leap) + { + cout << "This is leap year." << endl; + } + if (hulu) + { + cout << "This is huluculu festival year." << endl; + } + if (bulu) + { + cout << "This is bulukulu festival year." << endl; + } + if (!leap && !hulu && !bulu) + { + cout << "This is an ordinary year." << endl; + } + } + return 0; +} diff --git a/10071.cpp b/10071.cpp new file mode 100644 index 0000000..5a86900 --- /dev/null +++ b/10071.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + int v, t; + while (scanf("%d%d", &v, &t) != EOF) + { + printf("%d\n", 2 * v * t); + } + return 0; +} diff --git a/10073.cpp b/10073.cpp new file mode 100644 index 0000000..daba29c --- /dev/null +++ b/10073.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +int dir[6] = {1, 3, 6, -6, -3, -1}; +int dir2[6] = {3, 6, 12, 12, 6, 3}; +char t[12][12] = {{0, 1, 2, 1, 2, 3, 1, 2, 3, 2, 3, 4}, {1, 0, 1, 2, 1, 2, 2, 1, 2, 3, 2, 3}, {2, 1, 0, 3, 2, 1, 3, 2, 1, 4, 3, 2}, {1, 2, 3, 0, 1, 2, 2, 3, 4, 1, 2, 3}, {2, 1, 2, 1, 0, 1, 3, 2, 3, 2, 1, 2}, {3, 2, 1, 2, 1, 0, 4, 3, 2, 3, 2, 1}, {1, 2, 3, 2, 3, 4, 0, 1, 2, 1, 2, 3}, {2, 1, 2, 3, 2, 3, 1, 0, 1, 2, 1, 2}, {3, 2, 1, 4, 3, 2, 2, 1, 0, 3, 2, 1}, {2, 3, 4, 1, 2, 3, 1, 2, 3, 0, 1, 2}, {3, 2, 3, 2, 1, 2, 2, 1, 2, 1, 0, 1}, {4, 3, 2, 3, 2, 1, 3, 2, 1, 2, 1, 0}}; + +char str[15], ans[60], mov[12][12][6]; +int lim, nextlim, n; + +void init() +{ + mov[0][1][0] = mov[1][2][0] = mov[3][4][0] = mov[4][5][0] = mov[6][7][0] = + mov[7][8][0] = mov[9][10][0] = mov[10][11][0] = mov[0][3][1] = mov[1][4][1] = + mov[2][5][1] = mov[6][9][1] = mov[7][10][1] = mov[8][11][1] = mov[0][6][2] = + mov[1][7][2] = mov[2][8][2] = mov[3][9][2] = mov[4][10][2] = mov[5][11][2] = + mov[6][0][3] = mov[7][1][3] = mov[8][2][3] = mov[9][3][3] = mov[10][4][3] = + mov[11][5][3] = mov[3][0][4] = mov[4][1][4] = mov[5][2][4] = mov[9][6][4] = + mov[10][7][4] = mov[11][8][4] = mov[1][0][5] = mov[2][1][5] = mov[4][3][5] = + mov[5][4][5] = mov[7][6][5] = mov[8][7][5] = mov[10][9][5] = mov[11][10][5] = 1; +} +int eval() +{ + int sum = 0, i; + for (i = 0; i < 12; i++) + { + if (str[i] == 'L') + { + continue; + } + sum += t[i][str[i] - 'A']; + } + return sum; +} +char IDA_STAR(int depth, int prev, int loc, int ee) +{ + int i; + if (ee == 0) + { + puts(ans); + return 1; + } + else + { + int tl, te = ee; + char ts, xx; + for (i = 0; i < 6; i++) + { + if (i + prev == 5) + { + continue; + } + tl = loc + dir[i]; + if (tl < 0 || tl >= 12) + { + continue; + } + if (mov[loc][tl][i] == 0) + { + continue; + } + xx = str[tl] - 'A'; + ee -= (t[tl][xx] - t[loc][xx]); + ans[depth] = str[tl]; + ts = str[tl]; + str[tl] = str[loc]; + str[loc] = ts; + if (depth + 1 + ee <= lim && IDA_STAR(depth + 1, i, tl, ee)) + { + return 1; + } + else if (depth + 1 + ee > lim && depth + 1 + ee < nextlim) + { + nextlim = depth + 1 + ee; + } + str[loc] = str[tl]; + str[tl] = ts; + ans[depth] = '\0'; + ee = te; + } + return 0; + } +} + +int main() +{ + int caseno = 1, p, ev; + init(); + scanf("%d", &n); + while (n--) + { + memset(ans, '\0', sizeof(ans)); + scanf("%s", str); + for (p = 0; p < 12 && str[p] != 'L'; p++) + ; + printf("Permutation #%d\n", caseno++); + lim = eval(); + nextlim = 1000; + ev = lim; + do + { + if (IDA_STAR(0, -1, p, ev)) + { + break; + } + lim = nextlim; + nextlim = 1000; + } while (1); + puts(""); + } + return 0; +} diff --git a/10074.cpp b/10074.cpp index a24a53c..5a304a8 100644 --- a/10074.cpp +++ b/10074.cpp @@ -1,82 +1,79 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define INF 1LL<<50 +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int64 M, N; - cin >> M >> N; - if (M == 0 && N == 0) +int main() +{ + int m, n, v, field[101][101]; + while (scanf("%d%d", &m,&n) != EOF) + { + if (m == 0 && n == 0) + { break; - int64 mat[M][N]; - int64 i, j, k; - FOI(i, 0, M-1){ - FOI(j, 0, N-1){ - cin >> mat[i][j]; - if (mat[i][j] == 1) - mat[i][j] = -1 * INF; + } + int best = 0; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> v; + if (j == 0) + { + if (v == 0) + { + field[i][j] = 1; + if (field[i][j] > best) + { + best = field[i][j]; + } + } + else + { + field[i][j] = 0; + } + } else - mat[i][j] = 1; + { + if (v == 0) + { + field[i][j] = field[i][j - 1] + 1; + if (field[i][j] > best) + { + best = field[i][j]; + } + } + else + { + field[i][j] = 0; + } + } } } - - FOI(i, 1, M-1) - FOI(j, 0, N-1) - mat[i][j] += mat[i-1][j]; - - int64 maxSum = -1 * INF; - FOI(i, 0, M-1){ - FOI(j, i, M-1){ - int64 arr[N]; - FOI(k, 0, N-1) - arr[k] = mat[j][k]; - if (i > 0) - FOI(k, 0, N-1) - arr[k] -= mat[i-1][k]; - - int64 curSum = 0; - FOI(k, 0, N-1){ - curSum += arr[k]; - maxSum = max(curSum, maxSum); - if (curSum < 0) - curSum = 0; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + if (field[i][j] > 0) + { + int rows = 1; + int cols = field[i][j]; + for (int k = i + 1; k < m; k++) + { + if (field[k][j] == 0) + { + break; + } + cols = min(cols, field[k][j]); + rows++; + if (rows * cols > best) + { + best = rows * cols; + } + } } } } - maxSum = max(0LL, maxSum); - cout << maxSum << endl; + cout << best << endl; } return 0; } - diff --git a/10075.cpp b/10075.cpp new file mode 100644 index 0000000..0801678 --- /dev/null +++ b/10075.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define pi 3.141592653589793 +#define R 6378 +#define MAX 110 +#define INF 2147483647 + +int N, M, Q, index1, index2; + +struct node +{ + double lat, lon; + char name[100]; +}; +long long data[MAX][MAX]; +node store[MAX]; + +double distance(double a_lat, double b_lat, double a_long, double b_long) +{ + return R * acos(cos(a_lat) * cos(b_lat) * cos(a_long - b_long) + sin(a_lat) * sin(b_lat)); +} + +int index(char *name) +{ + int i = 0; + for (; i < N; i++) + if (!strcmp(name, store[i].name)) + { + return i; + } + return -1; +} + +int main() +{ + char temp[100], temp2[100]; + bool next_line = false; + int times = 1; + while (scanf("%d %d %d", &N, &M, &Q)) + { + if (N == 0 && M == 0 && Q == 0) + { + return 0; + } + if (next_line) + { + printf("\n"); + } + next_line = true; + printf("Case #%d\n", times++); + //initial + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + data[i][j] = INF; + } + data[i][i] = 0; + } + // + for (int i = 0; i < N; i++) + { + scanf("%99s %lf %lf", store[i].name, &store[i].lat, &store[i].lon); + store[i].lat = store[i].lat * pi / 180.0; + store[i].lon = store[i].lon * pi / 180.0; + } + for (int k = 0; k < M; k++) + { + scanf("%99s %99s", temp, temp2); + index1 = index(temp); + index2 = index(temp2); + if (index1 == index2) + { + data[index1][index2] = 0; + } + else + { + data[index1][index2] = (long long)(distance(store[index1].lat, store[index2].lat, store[index1].lon, store[index2].lon) + 0.5); + } + } + //APSP + for (int k = 0; k < N; k++) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (data[i][k] + data[k][j] < data[i][j]) + { + data[i][j] = data[i][k] + data[k][j]; + } + } + } + } + for (int i = 0; i < Q; i++) + { + scanf("%99s %99s", temp, temp2); + index1 = index(temp); + index2 = index(temp2); + if (data[index1][index2] != INF) + { + printf("%lld km\n", data[index1][index2]); + } + else + { + printf("no route exists\n"); + } + } + } + return 0; +} diff --git a/10077.cpp b/10077.cpp new file mode 100644 index 0000000..6e1b776 --- /dev/null +++ b/10077.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +void Find(long long a, long b) +{ + while (1) + { + if (a == 1 && b == 1) + { + return; + } + if (a < b) + { + b = b - a; + printf("L"); + } + else + { + a = a - b; + printf("R"); + } + } +} + +int main() +{ + long long a, b; + while (scanf("%lld%lld", &a, &b) == 2) + { + if (a == 1 && b == 1) + { + break; + } + Find(a, b); + printf("\n"); + } + return 0; +} diff --git a/10078.cpp b/10078.cpp new file mode 100644 index 0000000..e937a17 --- /dev/null +++ b/10078.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +struct Point {int x, y;} ref_; + +vector points; +const int infinity = 2147483647; +int flag; + +int is_left_special(Point p0, Point p1, Point p2) +{ + long long int cross_product = (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); + if (cross_product > 0) + { + return 1; + } + else if (cross_product < 0) + { + return -1; + } + else + { + return 0; + } +} + +bool is_left(Point p0, Point p1, Point p2) +{ + long long int cross_product = (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); + if (cross_product > 0) + { + return true; + } + else if (cross_product < 0) + { + return false; + } + else + { + if ((p0.x == p1.x) && (p0.y == p1.y)) + { + return true; + } + else if ((p0.x == p2.x) && (p0.y == p2.y)) + { + return false; + } + else + { + return false; + } + } +} + +bool compare(Point p2, Point p1) +{ + return !is_left(ref_, p1, p2); +} + +void graham_scan() +{ + stack final; + int i = 0; + int count = 0; + int n_vertex = points.size(); + int left_flag; + if (i < n_vertex) + { + final.push(points[i]); + i++; + count++; + } + if (i < n_vertex) + { + final.push(points[i]); + i++; + count++; + } + Point p0, p1, p2; + while ((count <= n_vertex) && (count > 1)) + { + p2 = points[i % n_vertex]; + p1 = final.top(); + final.pop(); + p0 = final.top(); + left_flag = is_left_special(p0, p1, p2); + if (left_flag >= 0) + { + final.push(p1); + final.push(p2); + i++; + count++; + } + else + { + flag = 0; + } + } +} + +int main() +{ + int n_vertex; + Point input; + while (cin >> n_vertex) + { + if (n_vertex == 0) + { + break; + } + int i = 0; + flag = 1; + ref_.x = infinity; + ref_.y = infinity; + points.clear(); + while (i < n_vertex) + { + cin >> input.x >> input.y; + points.push_back(input); + if (input.y < ref_.y) + { + ref_ = input; + } + else if ((input.y == ref_.y) && (input.x < ref_.x)) + { + ref_ = input; + } + i++; + } + sort(points.begin(), points.end(), compare); + graham_scan(); + if (flag == 0) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + return 0; +} diff --git a/10079.cpp b/10079.cpp index da449fa..5d2dafb 100644 --- a/10079.cpp +++ b/10079.cpp @@ -1,20 +1,17 @@ -#include - -using namespace std; - -int main() -{ - int n,cuts; - for(;;) - { - cin>>n; - - if(n<0) - break; - - cuts=(unsigned long long)n*(n+1)/2+1; - cout< + +using namespace std; + +int main() +{ + long long n; + while (scanf("%lld", &n)) + { + if (n < 0) + { + break; + } + cout << n * (n + 1) / 2 + 1 << endl; + } + return 0; +} diff --git a/1008.cpp b/1008.cpp new file mode 100644 index 0000000..aafd5aa --- /dev/null +++ b/1008.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +#define hash_mod 1000003 + +int n, m, visited[16][16], cases = 0; + +struct State +{ + char g[10][10]; + int label; + bool operator<(const State &x) const + { + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (g[i][j] != x.g[i][j]) + return g[i][j] < x.g[i][j]; + return false; + } + unsigned int hash() + { + unsigned int a = 63689, b = 378551; + unsigned int value = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + value = value * a + g[i][j]; + a *= b; + } + } + return value % hash_mod; + } + int dfs(int x, int y, char c) + { + if (x < 0 || y < 0 || x >= n || y >= m) + return 0; + if (g[x][y] != c || visited[x][y] == cases) + return 0; + visited[x][y] = cases; + int ret = 0; + ret += dfs(x + 1, y, c); + ret += dfs(x, y + 1, c); + ret += dfs(x - 1, y, c); + ret += dfs(x, y - 1, c); + return 1 + ret; + } + void erase(int x, int y, int c) + { + if (x < 0 || y < 0 || x >= n || y >= m) + return; + if (g[x][y] != c || visited[x][y] != cases) + return; + g[x][y] = '-'; + erase(x + 1, y, c); + erase(x, y + 1, c); + erase(x - 1, y, c); + erase(x, y - 1, c); + } + void fall() + { + for (int i = n - 1; i >= 0; i--) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] >= 'A' && g[i][j] <= 'Z') + { + int k = i + 1; + while (k < n && g[k][j] == '-') + { + g[k][j] = g[k - 1][j]; + g[k - 1][j] = '-'; + k++; + } + } + } + } + } + void refresh() + { + int update = 0, cnt; + do + { + fall(); + update = 0; + memset(visited, 0, sizeof(visited)); + cases = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (visited[i][j] == 0 && g[i][j] >= 'A' && g[i][j] <= 'Z') + { + cases++; + cnt = dfs(i, j, g[i][j]); + if (cnt > 1) + erase(i, j, g[i][j]), update = 1; + } + } + } + } while (update); + } + int isCompleted() + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + if (g[i][j] >= 'A' && g[i][j] <= 'Z') + return 0; + } + return 1; + } +}; +struct Info +{ + int x, y, dir, prev; + char c; + Info(int s1 = 0, int s2 = 0, int s3 = 0, int s4 = 0, char s = 0) : x(s1), y(s2), dir(s3), prev(s4), c(s) + { + } +}; + +map R[hash_mod]; +vector history; + +int main() +{ + char casesName[105]; + State st, next; + int h, step; + while (scanf("%d %d %s", &n, &m, casesName) == 3 && n) + { + for (int i = 0; i < n; i++) + scanf("%s", &st.g[i]); + history.resize(1); + for (int i = 0; i < hash_mod; i++) + R[i].clear(); + + int labelCnt = 0, label; + queue Q; + st.refresh(), st.label = 0; + h = st.hash(); + R[h][st] = 0, Q.push(st); + while (!Q.empty()) + { + st = Q.front(), Q.pop(); + h = st.hash(), label = st.label; + step = R[h][st]; + if (st.isCompleted()) + break; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (st.g[i][j] >= 'A' && st.g[i][j] <= 'Z') + { + if (j + 1 < m && st.g[i][j + 1] == '-') + { + next = st; + swap(next.g[i][j], next.g[i][j + 1]); + next.refresh(); + h = next.hash(); + if (R[h].find(next) == R[h].end()) + { + next.label = ++labelCnt; + history.push_back(Info(i, j, 1, label, st.g[i][j])); + R[h][next] = step + 1; + Q.push(next); + } + } + if (j - 1 >= 0 && st.g[i][j - 1] == '-') + { + next = st; + swap(next.g[i][j], next.g[i][j - 1]); + next.refresh(); + h = next.hash(); + if (R[h].find(next) == R[h].end()) + { + next.label = ++labelCnt; + history.push_back(Info(i, j, 0, label, st.g[i][j])); + R[h][next] = step + 1; + Q.push(next); + } + } + } + } + } + } + printf("%s: Minimum solution length = %d\n", casesName, step); + int idx = st.label; + vector ret; + while (idx) + { + ret.push_back(history[idx]); + idx = history[idx].prev; + } + for (int i = ret.size() - 1, j = 0; i >= 0; i--, j++) + { + if (j % 4) + { + putchar(' '); + } + printf("(%c,%d,%d,%c)", ret[i].c, ret[i].x, ret[i].y, ret[i].dir ? 'R' : 'L'); + if (j % 4 == 3) + { + puts(""); + } + } + if (ret.size() % 4) + { + puts(""); + } + } + return 0; +} diff --git a/10080.cpp b/10080.cpp new file mode 100644 index 0000000..9d0f68e --- /dev/null +++ b/10080.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define maxn 203 + +struct dog +{ + double x; + double y; +} D[maxn]; +struct bob +{ + double x; + double y; +} B[maxn]; +int cost[2 * maxn][2 * maxn]; +int match[2 * maxn][2 * maxn]; +int parent[maxn * 2]; +list links[maxn * 2]; +int dp, bp, des, total; +int A[maxn], Ind, Time, Velo; +int Q[maxn * maxn]; +double Dis(double x, double y, double x1, double y1) +{ + double t, s; + s = (x - x1); + s *= s; + t = (y - y1); + t *= t; + return sqrt(s + t); +} +void Set() +{ + int i, j; + double dis1; + for (i = 1; i <= bp; i++) + { + for (j = 1; j <= dp; j++) + { + dis1 = Dis(B[i].x, B[i].y, D[j].x, D[j].y) / Velo; + if (Time >= dis1) + { + links[i].push_back(j + bp); + links[j + bp].push_back(i); + cost[i][j + bp] = 1; + } + } + } + des = bp + dp + 1; + for (j = 1; j <= dp; j++) + { + links[j + bp].push_back(des); + cost[j + bp][des] = 1; + } +} +void Recur(int n) +{ + if (n < 0) + { + return; + } + Recur(parent[n]); + A[Ind++] = n; +} +void Mark(int n) +{ + int i, u, v; + Ind = 0; + Recur(n); + total++; + for (i = 1; i < Ind; i++) + { + u = A[i - 1]; + v = A[i]; + match[u][v] = 1; + match[v][u] = 0; + cost[u][v] = 0; + cost[v][u] = 1; + } +} +void Bfs(int n) +{ + int ind = 0, i, u, v; + int Fg[maxn * 2] = {0}; + list::iterator p; + Q[ind++] = n; + Fg[n] = 1; + parent[n] = -1; + for (i = 0; i < ind; i++) + { + u = Q[i]; + for (p = links[u].begin(); p != links[u].end(); p++) + { + v = *p; + if (cost[u][v] == 0 || Fg[v]) + { + continue; + } + parent[v] = u; + if (v == des) + { + Mark(v); + return; + } + Fg[v] = 1; + Q[ind++] = v; + } + } +} +void Cal() +{ + int i; + total = 0; + Set(); + for (i = 1; i <= bp; i++) + { + Bfs(i); + } + total = bp - total; + cout << total << endl; +} +void Free() +{ + int i, j, d; + for (i = 1; i <= bp; i++) + { + for (j = 1; j <= dp; j++) + { + d = j + bp; + cost[i][d] = 0; + cost[d][i] = 0; + match[i][d] = 0; + match[d][i] = 0; + } + links[i].clear(); + } + for (i = 1; i <= dp; i++) + { + d = i + bp; + links[d].clear(); + cost[d][des] = 0; + cost[des][d] = 0; + } +} + +int main() +{ + int i; + while (cin >> bp >> dp >> Time >> Velo) + { + for (i = 1; i <= bp; i++) + { + cin >> B[i].x >> B[i].y; + } + for (i = 1; i <= dp; i++) + { + cin >> D[i].x >> D[i].y; + } + Cal(); + Free(); + } + return 0; +} diff --git a/10081.cpp b/10081.cpp new file mode 100644 index 0000000..1871b3d --- /dev/null +++ b/10081.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int n, k; +double f[12][102] = {0}, ans = 0, pow_; + +int main() +{ + while (scanf("%d%d", &k, &n) == 2) + { + memset(f, 0, sizeof(f)); + for (int i = 0; i <= k; i++) + { + f[i][1] = 1; + } + for (int j = 2; j <= n; j++) + { + for (int i = 0; i <= k; i++) + { + if (i == 0) + { + f[i][j] = f[i][j - 1] + f[i + 1][j - 1]; + } + else if (i == k) + { + f[i][j] = f[i - 1][j - 1] + f[i][j - 1]; + } + else + { + f[i][j] = f[i - 1][j - 1] + f[i][j - 1] + f[i + 1][j - 1]; + } + } + } + ans = 0; + for (int i = 0; i <= k; i++) + { + ans += f[i][n]; + } + k++; + pow_ = (double)k; + for (int i = 1; i < n; i++) + { + pow_ *= k; + } + printf("%.5lf\n", ans / pow_ * 100, ans, pow_); + } + return 0; +} diff --git a/10082.cpp b/10082.cpp index 931f02d..603c3ba 100644 --- a/10082.cpp +++ b/10082.cpp @@ -1,18 +1,22 @@ -#include -#include - -using namespace std; - -int main() -{ - char ch[1000]; - int i; - while(cin>>ch) - { - if(ch=="S") - cout<<'A'; - else - cout< + +using namespace std; + +int main() +{ + string keys = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; + char c; + while (cin.get(c)) + { + if (c == '\n' || c == ' ') + { + printf("%c", c); + } + else + { + size_t found = keys.find(c); + printf("%c", keys[found - 1]); + } + } + return 0; +} diff --git a/10083.cpp b/10083.cpp new file mode 100644 index 0000000..b415a97 --- /dev/null +++ b/10083.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +const char fail[] = "is not an integer with less than 100 digits."; + +int B[1000], S[1000], T[1000], lb, ls, lt;// B is t^b, S is sol + +void mult(int M1[1000], int *l1, int M2[1000], int *l2) +{ + lt = *l1 + *l2 - 1; + for (int i = 0; i < 1000; i++) + { + T[i] = 0; + } + for (int i = 0; i < *l1; i++) + { + for (int j = 0; j < *l2; j++) + { + T[i + j] += M1[i] * M2[j]; + } + } + for (int i = 0; i < lt; i++) + { + T[i + 1] += T[i] / 10; + T[i] %= 10; + } + if (T[lt]) + { + lt++; + } + for (int i = M1[lt] = 0; i < lt; i++) + { + M1[i] = T[i]; + } + *l1 = lt; +} + +int main() +{ + int t, a, b, m; + while (scanf("%d%d%d", &t, &a, &b) == 3) + { + printf("(%d^%d-1)/(%d^%d-1) ", t, a, t, b); + if (t == 1) + { + printf("%s\n", fail); + continue; + } + if (t == 2 && b == 1) + { + if (a * log(2.0) / log(10.0) > 100.0) + { + printf("%s\n", fail); + continue; + } + for (int i = 0; i < 1000; i++) + { + B[i] = S[i] = 0; + } + lb = ls = 1; + S[0] = 2; + for (B[0] = 1; a; a >>= 1) + { + if (ls > 100) + { + lb = 1000; + break; + } + if (a & 1) + { + mult(B, &lb, S, &ls); + } + mult(S, &ls, S, &ls); + if (lb > 100) + { + break; + } + } + if (lb > 100) + { + printf("%s\n", fail); + continue; + } + B[0]--; + for (int i = lb - 1; i >= 0; i--) + { + printf("%d", B[i]); + } + printf("\n"); + continue; + } + m = a / b; + if (a != m * b) + { + printf("%s\n", fail); + continue; + } + if ((a - b) * log((double)t) / log(10.0) > 100.0) + { + printf("%s\n", fail); + continue; + } + if (a == b) + { + printf("1\n"); + continue; + } + for (int i = 0; i < 1000; i++) + { + B[i] = S[i] = 0; + } + lb = 1; + for (ls = 0; t; ls++, t /= 10) + { + S[ls] = t % 10; + } + for (B[0] = 1; b; b >>= 1) + { + if (ls > 100) + { + lb = 1000; + break; + } + if (b & 1) + { + mult(B, &lb, S, &ls); + } + mult(S, &ls, S, &ls); + if (lb > 100) + { + break; + } + } + if (lb > 100) + { + printf("%s\n", fail); + continue; + } + for (int i = 0; i < 1000; i++) + { + S[i] = 0; + } + ls = 1; + for (int i = 0; i < m; i++) + { + // S=S*B+1 + if (ls != 1 || S[0]) + { + mult(S, &ls, B, &lb); + } + S[0]++; + for (int j = S[ls] = 0; j < ls; j++) + { + S[j + 1] += S[j] / 10; + S[j] %= 10; + } + if (S[ls]) + { + ls++; + } + if (ls > 100) + { + break; + } + } + if (ls > 100) + { + printf("%s\n", fail); + continue; + } + for (int i = ls - 1; i >= 0; i--) + { + printf("%d", S[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10084.cpp b/10084.cpp new file mode 100644 index 0000000..619d2a2 --- /dev/null +++ b/10084.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +const int MAX = 55; + +struct point +{ + double x, y; +}; +struct line +{ + point a, b; + double ang; +}; +line ln[MAX], deq[MAX], ltmp[MAX]; +point s[MAX]; +const double eps = 1e-6; +bool dy(double x, double y) +{ + return x > y + eps;// x > y +} +bool xy(double x, double y) +{ + return x < y - eps;// x < y +} +bool dyd(double x, double y) +{ + return x > y - eps;// x >= y +} +bool xyd(double x, double y) +{ + return x < y + eps;// x <= y +} +bool dd(double x, double y) +{ + return fabs(x - y) < eps;// x == y +} +double crossProduct(point a, point b, point c)//?? ac ? ab ??? +{ + return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y); +} +void makeline(double x1, double y1, double x2, double y2, line &l) +{ + l.a.x = x1; + l.a.y = y1; + l.b.x = x2; + l.b.y = y2; + l.ang = atan2(y1 - y2, x1 - x2); +} +point foot_line(point a, point l1, point l2)//ac?l1l2?????? +{ + point c; + c.x = a.x - l2.y + l1.y; + c.y = a.y + l2.x - l1.x; + return c; +} +bool cmphp(line a, line b) +{ + if (dd(a.ang, b.ang)) + { + return xy(crossProduct(b.a, b.b, a.a), 0.0); + } + return xy(a.ang, b.ang); +} +bool equalcmp(line a, line b) +{ + return dd(a.ang, b.ang); +} +bool parallel(line u, line v) +{ + return dd((u.a.x - u.b.x) * (v.a.y - v.b.y) - (v.a.x - v.b.x) * (u.a.y - u.b.y), 0.0); +} +bool equalp(point a, point b) +{ + return dd(a.x, b.x) && dd(a.y, b.y); +} +double area_polygon(point p[], int n) +{ + double s = 0.0; + for (int i = 0; i < n; i++) + { + s += p[(i + 1) % n].y * p[i].x - p[(i + 1) % n].x * p[i].y; + } + return fabs(s) / 2.0; +} +point l2l_inst_p(line l1, line l2) +{ + point ans = l1.a; + double t = ((l1.a.x - l2.a.x) * (l2.a.y - l2.b.y) - (l1.a.y - l2.a.y) * (l2.a.x - l2.b.x)) / + ((l1.a.x - l1.b.x) * (l2.a.y - l2.b.y) - (l1.a.y - l1.b.y) * (l2.a.x - l2.b.x)); + ans.x += (l1.b.x - l1.a.x) * t; + ans.y += (l1.b.y - l1.a.y) * t; + return ans; +} +void inst_hp_nlogn(line *ln, int n, point *s, int &len) +{ + len = 0; + sort(ln, ln + n, cmphp); + n = unique(ln, ln + n, equalcmp) - ln; + int bot = 0, top = 1; + deq[0] = ln[0]; + deq[1] = ln[1]; + for (int i = 2; i < n; i++) + { + if (parallel(deq[top], deq[top - 1]) || parallel(deq[bot], deq[bot + 1])) + { + return; + } + while (bot < top && dy(crossProduct(ln[i].a, ln[i].b, + l2l_inst_p(deq[top], deq[top - 1])), + 0.0)) + { + top--; + } + while (bot < top && dy(crossProduct(ln[i].a, ln[i].b, + l2l_inst_p(deq[bot], deq[bot + 1])), + 0.0)) + { + bot++; + } + deq[++top] = ln[i]; + } + while (bot < top && dy(crossProduct(deq[bot].a, deq[bot].b, + l2l_inst_p(deq[top], deq[top - 1])), + 0.0)) + { + top--; + } + while (bot < top && dy(crossProduct(deq[top].a, deq[top].b, + l2l_inst_p(deq[bot], deq[bot + 1])), + 0.0)) + { + bot++; + } + if (top <= bot + 1) + { + return; + } + for (int i = bot; i < top; i++) + { + s[len++] = l2l_inst_p(deq[i], deq[i + 1]); + } + if (bot < top + 1) + { + s[len++] = l2l_inst_p(deq[bot], deq[top]); + } + len = unique(s, s + len, equalp) - s; +} + +int main() +{ + double x, y; + char str[100]; + point a, b; + a.x = a.y = 0.0; + int cnt = 0; + double area = 1e20; + makeline(0, 0, 10, 0, ln[cnt++]); + makeline(10, 0, 10, 10, ln[cnt++]); + makeline(10, 10, 0, 10, ln[cnt++]); + makeline(0, 10, 0, 0, ln[cnt++]); + while (~scanf("%lf%lf%s", &b.x, &b.y, str)) + { + if (strcmp(str, "Same") == 0) + { + area = 0.0; + } + point c; + c.x = (a.x + b.x) / 2.0; + c.y = (a.y + b.y) / 2.0; + point d; + if (strcmp(str, "Colder") == 0) + { + d = foot_line(c, a, b); + makeline(c.x, c.y, d.x, d.y, ln[cnt++]); + } + if (strcmp(str, "Hotter") == 0) + { + d = foot_line(c, a, b); + makeline(d.x, d.y, c.x, c.y, ln[cnt++]); + } + a = b; + if (area == 0.0) + { + printf("0.00\n"); + continue; + } + int len = 0; + memcpy(ltmp, ln, sizeof(ln)); + inst_hp_nlogn(ltmp, cnt, s, len); + area = area_polygon(s, len); + if (len < 3) + { + printf("0.00\n"); + continue; + } + printf("%.2lf\n", area); + } + return 0; +} diff --git a/10085.cpp b/10085.cpp new file mode 100644 index 0000000..315b1f2 --- /dev/null +++ b/10085.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +char com[9][45] = {"RRDLLURRDDLLURDLUURRDLURDDLLUUR", + "LDRRULLDDRRULDRUULLDRULDDRRUULL", + "LLDRRULLDDRRULDRUULLDRULDDRRUUL", + "RRULLDDRRULULDDRRULULDDRRUULLDD", + "LURRDLLURRDLLURDDLURDRULLURRDL", + "LLURRDDLLURURDDLLURURDDLLUURRDD", + "RRULLURDDRUULDDLUURDRDLLURRDLLU", + "LURRDLLUURRDLURDDLLURDLUURRDDLL", + "LLURRULDDLUURDDRUULDLDRRULLDRRU"}; +int A[12]; + +void Cal(int z) +{ + int i, j, p; + p = z; + for (i = 0; com[z][i]; i++) + { + switch (com[z][i]) + { + case 'L': + A[p] = A[p - 1]; + A[p - 1] = 0; + p--; + break; + case 'R': + A[p] = A[p + 1]; + A[p + 1] = 0; + p++; + break; + case 'U': + A[p] = A[p - 3]; + A[p - 3] = 0; + p -= 3; + break; + case 'D': + A[p] = A[p + 3]; + A[p + 3] = 0; + p += 3; + break; + } + } + p = 0; + for (i = 0; i < 3; i++) + { + printf("%d", A[p++]); + for (j = 1; j < 3; j++) + printf(" %d", A[p++]); + printf("\n"); + } +} + +int main() +{ + int T, f = 1, i, z; + scanf("%d", &T); + while (T--) + { + for (i = 0; i < 9; i++) + { + scanf("%d", &A[i]); + if (A[i] == 0) + z = i; + } + printf("Puzzle #%d\n", f++); + Cal(z); + puts(com[z]); + printf("\n"); + } + return 0; +} diff --git a/10086.cpp b/10086.cpp new file mode 100644 index 0000000..3f98286 --- /dev/null +++ b/10086.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define X first +#define Y second + +int main() +{ + ios::sync_with_stdio(false); + int T, S, N, M[35], X[35][35], tot, tmp, prev, O[35]; + pair dp[35][305]; + while (cin >> T >> S && (T + S)) + { + cin >> N; + for (int i = 0; i < N; i += 1) + { + cin >> M[i]; + X[i][0] = 0; + for (int j = 0; j < M[i]; j += 1) + { + cin >> X[i][j + 1]; + } + for (int j = 1; j <= M[i]; j += 1) + { + cin >> tmp; + X[i][M[i] - j] += tmp; + } + } + tot = 0; + dp[0][0] = make_pair(0, 0); + for (int n = 1; n <= N; n += 1) + { + for (int i = 0; i <= min(T, tot + M[n - 1]); i += 1) + { + dp[n][i].X = 100000000; + for (int j = max(i - M[n - 1], 0); j <= min(i, tot); j += 1) + { + if (dp[n][i].X > dp[n - 1][j].X + X[n - 1][i - j]) + { + dp[n][i].X = dp[n - 1][j].X + X[n - 1][i - j]; + dp[n][i].Y = i - j; + } + } + } + tot += M[n - 1]; + } + cout << dp[N][T].X << '\n'; + prev = T; + for (int i = N; i >= 1; i -= 1) + { + O[i] = dp[i][prev].Y; + prev -= dp[i][prev].Y; + } + for (int i = 1; i < N; i += 1) + { + cout << O[i] << ' '; + } + cout << O[N] << "\n\n"; + } + return 0; +} diff --git a/10087.cpp b/10087.cpp new file mode 100644 index 0000000..a529e0c --- /dev/null +++ b/10087.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +int m[101][101], L, S; + +int main() +{ + while (scanf("%d %d", &L, &S) == 2) + { + int p = L * (L * L + 1) / 2; + if (L == 2 || (S - p) % L) + { + puts("You can't be like Shahjahan!\n"); + } + else if (L == 1) + { + printf("%10d\n\n", S); + } + else + { + int diff = (S - p) / L; + if (L % 2) + { + for (int i = 1; i <= L; i++) + { + for (int j = 1; j <= L; j++) + { + m[i][j] = 0; + } + } + int nc = (L + 1) / 2, nr = nc + 1, k = 0; + while (k < L * L) + { + if (m[nr][nc] == 0) + { + m[nr++][nc++] = ++k; + } + else + { + nr++, nc--; + } + if (nr > L) + { + nr = 1; + } + if (nc < 1) + { + nc = L; + } + if (nc > L) + { + nc = 1; + } + } + } + else if (L % 4 == 0) + { + int x = L / 4; + for (int i = 1; i <= L; i++) + { + for (int j = 1; j <= L; j++) + { + m[i][j] = (i - 1) * L + j; + } + } + for (int i = 1, k = L; i <= x; i++, k--) + { + for (int j = 1, l = L; j <= L; j++, l--) + { + if (j <= x || j > L - x) + { + swap(m[i][j], m[k][l]); + } + } + } + for (int i = x + 1, k = L - x; i < k; i++, k--) + { + for (int j = x + 1, l = L - x; j <= L - x; j++, l--) + { + swap(m[i][j], m[k][l]); + } + } + } + else + { + int x = L / 4; + for (int i = 1; i <= L; i++) + { + for (int j = 1; j <= L; j++) + { + m[i][j] = (i - 1) * L + j; + } + } + for (int i = x + 2; i < 3 * x + 2; i++) + { + for (int j = 1, k = L; j < k; j++, k--) + { + if (j != x + 1) + { + swap(m[i][j], m[i][k]); + } + } + } + for (int i = x + 2; i < 3 * x + 2; i++) + { + for (int j = 1, k = L; j < k; j++, k--) + { + if (j != x + 1) + { + swap(m[j][i], m[k][i]); + } + } + } + for (int j = 1, k = L; j < x + 1; j++, k--) + { + swap(m[x + 1][j], m[x + 1][k]); + swap(m[3 * x + 2][j], m[3 * x + 2][k]); + swap(m[x + 2][j], m[x + 2][k]); + } + for (int i = x + 2; i < 3 * x + 2; i++) + { + swap(m[x + 1][i], m[3 * x + 2][i]); + } + for (int i = 1, j = L; i < j; i++, j--) + { + if (i != x + 1) + { + swap(m[i][x + 1], m[j][x + 1]); + } + } + swap(m[x + 1][1], m[3 * x + 2][1]); + for (int i = x + 2, j = 3 * x + 1; i < j; i++, j--) + { + swap(m[x + 1][i], m[x + 1][j]); + } + for (int i = x + 2; i < 3 * x + 2; i++) + { + if (i != 2 * x + 1) + { + swap(m[i][x + 1], m[i][3 * x + 2]); + } + } + } + for (int i = 1; i <= L; i++) + { + for (int j = 1; j <= L; j++) + { + printf("%10d", m[i][j] + diff); + } + printf("\n"); + } + printf("\n"); + } + } + return 0; +} diff --git a/10088.cpp b/10088.cpp new file mode 100644 index 0000000..e3e88a0 --- /dev/null +++ b/10088.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +typedef long long int LLI; + +#define MAXPOLY 1050 + +struct point {LLI x, y;}; + +struct polygon +{ + LLI n; + point p[MAXPOLY]; +}; + +LLI gcd(LLI a, LLI b) +{ + if (b == 0) + { + return a; + } + if (b > a) + { + return gcd(b, a); + } + else + { + return gcd(b, a % b); + } +} + +LLI calArea(polygon &contour) +{ + LLI total = 0.0; + for (LLI i = 0; i < contour.n; i++) + { + LLI j = (i + 1) % contour.n; + total += (contour.p[i].x * contour.p[j].y - + contour.p[j].x * contour.p[i].y); + } + return abs(total); +} + +LLI boundaryPoLLIs(polygon &contour) +{ + LLI points = 0; + for (LLI i = 0; i < contour.n; i++) + { + LLI j = (i + 1) % contour.n; + if (contour.p[i].x == contour.p[j].x) + { + points += (abs(contour.p[j].y - contour.p[i].y) - 1); + } + else if (contour.p[i].y == contour.p[j].y) + { + points += (abs(contour.p[j].x - contour.p[i].x) - 1); + } + else + { + LLI xDiff = abs(contour.p[j].x - contour.p[i].x); + LLI yDiff = abs(contour.p[j].y - contour.p[i].y); + points += gcd(xDiff, yDiff) - 1; + } + } + return (points + contour.n); +} + +int main(int argc, char const *argv[]) +{ + LLI vertexNumber; + polygon contour; + while (cin >> vertexNumber, vertexNumber) + { + contour.n = vertexNumber; + for (LLI i = 0; i < vertexNumber; i++) + { + cin >> contour.p[i].x >> contour.p[i].y; + } + LLI vertexBoundary = boundaryPoLLIs(contour); + LLI area = calArea(contour); + cout << (2 * area + 4 - 2 * vertexBoundary) / 4 << endl; + } + return 0; +} diff --git a/10089.cpp b/10089.cpp new file mode 100644 index 0000000..928d4b0 --- /dev/null +++ b/10089.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +// @ http://www.ddj.com/cpp/184403801 +template +struct StringTok +{ + StringTok(const T &seq, typename T::size_type pos = 0) : seq_(seq), pos_(pos) {} + T operator()(const T &delim); + + const T &seq_; + typename T::size_type pos_; +}; + +template +T StringTok::operator()(const T &delim) +{ + T token; + if (pos_ != T::npos) + { + // start of found token + typename T::size_type first(seq_.find_first_not_of(delim.c_str(), pos_)); + if (first != T::npos) + { + // length of found token + typename T::size_type num(seq_.find_first_of(delim.c_str(), first) - first); + // do all the work off to the side + token = seq_.substr(first, num); + // done; now commit using + // nonthrowing operations only + pos_ = first + num; + if (pos_ != T::npos) + { + ++pos_; + } + if (pos_ >= seq_.size()) + { + pos_ = T::npos; + } + } + } + return token; +} + +// @ http://www.gotw.ca/gotw/029.htm for case-insensitive lexicographic compares +struct ci_char_traits : public std::char_traits +{ + static bool eq(char c1, char c2) + { + return toupper(c1) == toupper(c2); + } + static bool ne(char c1, char c2) + { + return toupper(c1) != toupper(c2); + } + static bool lt(char c1, char c2) + { + return toupper(c1) < toupper(c2); + } + static int compare(const char *s1, const char *s2, size_t n) + { + return strncasecmp(s1, s2, n); + } + static const char *find(const char *s, int n, char a) + { + while (n-- > 0 && toupper(*s) != toupper(a)) + { + ++s; + } + return s; + } +}; + +// no boost makes c++ angry; lexical cast comes in handy for string <=> int conversion without expliucitly writing out the streams all the time. +template +T lexical_cast(const S &arg) +{ + stringstream interpreter; + T result; + if (!(interpreter << arg) || !(interpreter >> result) || !(interpreter >> std::ws).eof()) + { + throw logic_error("bad lexical_cast"); + } + return result; +} + +typedef vector vs; +typedef vector vi; +typedef std::basic_string ci_string; + +struct coord +{ + int x_, y_; + double angle_; + coord(int x, int y) : x_(x), y_(y), angle_(atan2(static_cast(y), static_cast(x))) + { + if (angle_ < 0) + { + angle_ += M_PI + M_PI; + } + } +}; + +bool operator<(const coord &a, const coord &b) +{ + return a.angle_ < b.angle_; +} + +int main() +{ + int n; + while (cin >> n, n) + { + vector coords; + while (n--) + { + int a, b, c; + cin >> a >> b >> c; + coords.push_back(coord(a - c, b - c)); + } + sort(coords.begin(), coords.end()); + bool succ(true); + for (vector::const_iterator i(coords.begin()), ie(coords.end()); succ && (i + 1) != ie; ++i) + if (fmod((i + 1)->angle_ - i->angle_, M_PI + M_PI) > M_PI) + { + succ = false; + } + if (fmod(coords.begin()->angle_ + M_PI + M_PI - coords.rbegin()->angle_, M_PI + M_PI) > M_PI) + { + succ = false; + } + printf(succ ? "Yes\n" : "No\n"); + } + return 0; +} diff --git a/1009.cpp b/1009.cpp new file mode 100644 index 0000000..3213df6 --- /dev/null +++ b/1009.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +const double comp = 4 * acos(-1) / 3; + +double d[8][8], r[8], maxv; +int x[8], y[8], z[8], n, xx, yy, zz; +bool used[8]; + +double abs(double a) +{ + return a < 0 ? -a : a; +} +double cube(double x) +{ + return x * x * x; +} +double min(double a, double b) +{ + return a < b ? a : b; +} +int sqr(int x) +{ + return x * x; +} + +void bt(int k) +{ + if (k == n) + { + double v = 0.0; + for (int i = 0; i < n; ++i) + { + v += cube(r[i]); + } + v *= comp; + if (v > maxv) + { + maxv = v; + } + return; + } + FOR(i, n) + if (!used[i]) + { + r[i] = min(x[i], xx - x[i]); + r[i] = min(r[i], min(y[i], yy - y[i])); + r[i] = min(r[i], min(z[i], zz - z[i])); + bool usable = true; + FOR(j, n) + { + usable &= !used[j] || d[i][j] > r[j]; + if (used[j]) + { + r[i] = min(r[i], d[i][j] - r[j]); + } + } + if (!usable) + { + r[i] = 0.0; + } + used[i] = true; + bt(k + 1); + used[i] = false; + } +} + +int main() +{ + for (int box = 0; scanf("%d", &n) == 1 && n;) + { + int xb, yb, zb, xt, yt, zt; + FOR(i, 8) + used[i] = false; + scanf("%d %d %d", &xb, &yb, &zb); + scanf("%d %d %d", &xt, &yt, &zt); + if (xb > xt) + { + swap(xb, xt); + } + if (yb > yt) + { + swap(yb, yt); + } + if (zb > zt) + { + swap(zb, zt); + } + xx = xt - xb; + yy = yt - yb; + zz = zt - zb; + FOR(i, n) + { + scanf("%d %d %d", &x[i], &y[i], &z[i]); + if (x[i] < xb || x[i] > xt || y[i] < yb || y[i] > yt || z[i] < zb || z[i] > zt) + { + --n, --i; + continue; + } + x[i] -= xb; + y[i] -= yb; + z[i] -= zb; + FOR(j, i) + { + d[i][j] = d[j][i] = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]) + sqr(z[i] - z[j])); + } + d[i][i] = 0.0; + } + maxv = -1; + bt(0); + printf("Box %d: %.0lf\n\n", ++box, xx * yy * zz - maxv); + } + return 0; +} diff --git a/10090.cpp b/10090.cpp new file mode 100644 index 0000000..e9f4d44 --- /dev/null +++ b/10090.cpp @@ -0,0 +1,234 @@ +#include + +using namespace std; + +// @ http://www.ddj.com/cpp/184403801 +template +struct StringTok +{ + StringTok(const T &seq, typename T::size_type pos = 0) : seq_(seq), pos_(pos) {} + T operator()(const T &delim); + + const T &seq_; + typename T::size_type pos_; +}; + +template +T StringTok::operator()(const T &delim) +{ + T token; + if (pos_ != T::npos) + { + // start of found token + typename T::size_type first(seq_.find_first_not_of(delim.c_str(), pos_)); + if (first != T::npos) + { + // length of found token + typename T::size_type num(seq_.find_first_of(delim.c_str(), first) - first); + // do all the work off to the side + token = seq_.substr(first, num); + // done; now commit using + // nonthrowing operations only + pos_ = first + num; + if (pos_ != T::npos) + { + ++pos_; + } + if (pos_ >= seq_.size()) + { + pos_ = T::npos; + } + } + } + return token; +} + +// @ http://www.gotw.ca/gotw/029.htm for case-insensitive lexicographic compares +struct ci_char_traits : public std::char_traits +{ + static bool eq(char c1, char c2) + { + return toupper(c1) == toupper(c2); + } + static bool ne(char c1, char c2) + { + return toupper(c1) != toupper(c2); + } + static bool lt(char c1, char c2) + { + return toupper(c1) < toupper(c2); + } + static int compare(const char *s1, const char *s2, size_t n) + { + return strncasecmp(s1, s2, n); + } + static const char *find(const char *s, int n, char a) + { + while (n-- > 0 && toupper(*s) != toupper(a)) + { + ++s; + } + return s; + } +}; + +// no boost makes c++ angry; lexical cast comes in handy for string <=> int conversion without expliucitly writing out the streams all the time. +template +T lexical_cast(const S &arg) +{ + stringstream interpreter; + T result; + if (!(interpreter << arg) || !(interpreter >> result) || !(interpreter >> std::ws).eof()) + { + throw logic_error("bad lexical_cast"); + } + return result; +} + +typedef unsigned long ul; +typedef long long ll; +typedef vector vs; +typedef vector vi; +typedef std::basic_string ci_string; + +pair extended_gcd(ll a, ll b) +{ + if (a % b == 0) + { + return make_pair(0, 1); + } + else + { + pair temp(extended_gcd(b, a % b)); + return make_pair(temp.second, temp.first - temp.second * (a / b)); + } +} + +pair extended_gcd_2(ll a, ll b) +{ + ll x(0), y(1), lastx(1), lasty(0); + while (b != 0) + { + ll temp(b); + ll quotient(a / b); + b = a % b; + a = temp; + temp = x; + x = lastx - quotient * x; + lastx = temp; + temp = y; + y = lasty - quotient * y; + lasty = temp; + } + return make_pair(lastx, lasty); +} + +int main() +{ + ll n; + while (scanf("%lld", &n) == 1, n) + { + ll c1, n1, c2, n2; + cin >> c1 >> n1 >> c2 >> n2; + pair res(extended_gcd(n1, n2)); + ll gcd(res.first * n1 + res.second * n2); + ll lcm((n1 * n2) / gcd); + if (0 == n % gcd) + { + n /= gcd; + lcm /= gcd; + n1 /= gcd; + n2 /= gcd; + } + else + { + cout << "failed" << endl; + continue; + } + ll n1_delta(lcm / n1); + ll n2_delta(lcm / n2); + ll t_n1(res.first), t_n2(res.second); + // overflow the UL to chk `extended_gcd` + t_n1 *= n; + t_n2 *= n; + if (t_n1 < 0) + { + ll steps((0 - t_n1) / n1_delta); + t_n1 += steps * n1_delta; + t_n2 -= steps * n2_delta; + if (t_n1 < 0) + { + t_n1 += n1_delta; + t_n2 -= n2_delta; + } + ll cand_cost(t_n1 * c1 + t_n2 * c2); + ll t_t_n1(t_n1 + n1_delta); + ll t_t_n2(t_n2 - n2_delta); + ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2); + if (t_cand_cost < cand_cost && t_t_n2 >= 0) + { + ll more_steps(t_n2 / n2_delta); + t_n2 -= more_steps * n2_delta; + t_n1 += more_steps * n1_delta; + } + } + else if (t_n2 < 0) + { + ll steps((0 - t_n2) / n2_delta); + t_n1 -= steps * n1_delta; + t_n2 += steps * n2_delta; + if (t_n2 < 0) + { + t_n1 -= n1_delta; + t_n2 += n2_delta; + } + ll cand_cost(t_n1 * c1 + t_n2 * c2); + ll t_t_n1(t_n1 - n1_delta); + ll t_t_n2(t_n2 + n2_delta); + ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2); + if (t_cand_cost < cand_cost && t_t_n1 >= 0) + { + ll more_steps(t_n1 / n1_delta); + t_n1 -= more_steps * n1_delta; + t_n2 += more_steps * n2_delta; + } + } + else + { + { + ll cand_cost(t_n1 * c1 + t_n2 * c2); + ll t_t_n1(t_n1 + n1_delta); + ll t_t_n2(t_n2 - n2_delta); + ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2); + if (t_cand_cost < cand_cost && t_t_n2 >= 0) + { + ll more_steps(t_n2 / n2_delta); + t_n2 -= more_steps * n2_delta; + t_n1 += more_steps * n1_delta; + } + } + { + ll cand_cost(t_n1 * c1 + t_n2 * c2); + ll t_t_n1(t_n1 - n1_delta); + ll t_t_n2(t_n2 + n2_delta); + ll t_cand_cost(t_t_n1 * c1 + t_t_n2 * c2); + if (t_cand_cost < cand_cost && t_t_n1 >= 0) + { + ll more_steps(t_n1 / n1_delta); + t_n1 -= more_steps * n1_delta; + t_n2 += more_steps * n2_delta; + } + } + } + if (t_n1 < 0 || t_n2 < 0) + { + printf("failed\n"); + continue; + } + else + { + cout << t_n1 << " " << t_n2 << endl; + } + } + return 0; +} diff --git a/10091.cpp b/10091.cpp new file mode 100644 index 0000000..40dbfaf --- /dev/null +++ b/10091.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int mth(int y, int m, int d) +{ + int cnt = 0; + if (y < 470) + { + while (true) + { + cnt++; + m++; + if (m == 13) + { + y++; + m = 1; + } + if (y == 470 && m == 2) + { + break; + } + } + return cnt; + } + if (m < 2) + { + return 2 - m + cnt; + } + else if (m == 2) + { + if (d < 14) + { + return 0 + cnt; + } + else + { + return 12 + cnt; + } + } + else if (m > 2) + { + return 14 - m + cnt; + } + return -1; +} + +int m[26][26], orig[26][26], cpy[26][26]; +void mma(int n) +{ + rep(i, n) rep(j, n) cpy[i][j] = m[i][j], m[i][j] = 0; + rep(i, n) rep(j, n) rep(k, n) m[i][j] += cpy[i][k] * orig[k][j]; +} + +double solve(int n, int y, int mon, int d, int *tar) +{ + int num = mth(y, mon, d); + double dp[num + 1][26][2]; + rep(i, num + 1) rep(j, 26) rep(k, 2) dp[i][j][k] = 0; + double prob[26]; + rep(i, n) + { + int sum = 0; + rep(j, n) sum += orig[i][j]; + prob[i] = 1 / (double)sum; + } + dp[0][tar[0]][0] = 1; + dp[0][tar[1]][1] = 1; + rep(i, num) + { + rep(j, n) + { + rep(k, 2) + { + rep(l, n) + { + dp[i + 1][l][k] += prob[j] * orig[j][l] * dp[i][j][k]; + } + } + } + } + double ret = 0; + rep(i, n) + { + ret += dp[num][i][0] * dp[num][i][1]; + } + return ret; +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int y, mon, d; + int r; + cin >> y >> mon >> d; + cin >> r; + rep(i, 26) + { + rep(j, 26) m[i][j] = 0, orig[i][j] = 0; + m[i][i] = 1; + orig[i][i] = 1; + } + rep(i, r) + { + char a, b; + cin >> a >> b; + if (a != b) + { + orig[a - 'A'][b - 'A'] = orig[b - 'A'][a - 'A'] = 1; + } + } + char a, b; + cin >> a >> b; + int tar[2]; + tar[0] = a - 'A'; + tar[1] = b - 'A'; + printf("%.5lf\n", solve(26, y, mon, d, tar)); + } + return false; +} diff --git a/10092.cpp b/10092.cpp new file mode 100644 index 0000000..814ee3d --- /dev/null +++ b/10092.cpp @@ -0,0 +1,210 @@ +#include + +using namespace std; + +#define maxn 1024 + +list links[maxn], Cat[22]; +int Cost[maxn][maxn], parent[maxn], Block[maxn], Req[22], A[22]; +char Match[maxn][maxn], Fg[maxn]; +queue Q; +int P, CT, Des, Ter, ind; + +int SetAll() +{ + int i, d; + Des = P + CT + 1; + Ter = 0; + for (i = 1; i <= CT; i++) + { + d = P + i; + links[d].push_back(Des); + Cost[d][Des] = Req[i]; + Ter += Req[i]; + } + return 0; +} + +void Print() +{ + int c, i, j; + list::iterator p; + cout << "1\n"; + for (i = 1; i <= CT; i++) + { + c = 0; + for (p = Cat[i].begin(); p != Cat[i].end(); p++) + { + j = *p; + if (c++) + { + cout << " "; + } + cout << j; + } + cout << endl; + } +} + +void Recur(int n) +{ + if (n < 0) + { + return; + } + Recur(parent[n]); + A[ind++] = n; +} + +void Mark(int n) +{ + int i, u, v; + ind = 0; + Recur(Des); + for (i = 1; i < ind; i++) + { + u = A[i - 1]; + v = A[i]; + Match[u][v] = 1; + Match[v][u] = 0; + Cost[u][v] -= 1; + Cost[v][u] += 1; + } + Ter--; +} + +void BFS(int n) +{ + int u, v; + for (u = 1; u <= Des; u++) + { + Fg[u] = 0; + } + list::iterator p; + parent[n] = -1; + Q.push(n); + Fg[n] = 1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (p = links[u].begin(); p != links[u].end(); p++) + { + v = *p; + if (Cost[u][v] == 0 || Block[v] || Fg[v]) + { + continue; + } + parent[v] = u; + if (v == Des) + { + Mark(n); + return; + } + Fg[v] = 1; + Q.push(v); + } + } +} + +int Impossible() +{ + int i, d; + for (i = 1; i <= CT; i++) + { + d = i + P; + if (Req[i] > (int)links[d].size()) + return 1; + } + return 0; +} + +void Cal() +{ + int i; + if (SetAll()) + { + Print(); + return; + } + for (i = 1; i <= P && Ter; i++) + { + BFS(i); + while (!Q.empty()) + Q.pop(); + } + if (Ter) + cout << "0\n"; + else + { + for (i = 1; i <= P; i++) + { + if (Block[i]) + continue; + for (int j = 1; j <= CT; j++) + { + if (Match[i][j + P]) + { + Cat[j].push_back(i); + break; + } + } + } + Print(); + } +} + +void Free() +{ + int i, c; + list::iterator p; + for (i = 1; i <= CT; i++) + { + Cat[i].clear(); + } + for (i = 1; i <= P; i++) + { + for (p = links[i].begin(); p != links[i].end(); p++) + { + c = *p; + Match[i][c] = Match[c][i] = 0; + Cost[c][i] = Cost[i][c] = 0; + } + links[i].clear(); + } + for (i = P + 1; i <= P + CT; i++) + { + links[i].clear(); + Cost[i][Des] = Cost[Des][i] = 0; + } +} + +int main() +{ + int i, c, j, nc; + while (cin >> CT >> P) + { + if (!P && !CT) + { + break; + } + for (i = 1; i <= CT; i++) + { + cin >> Req[i]; + } + for (i = 1; i <= P; i++) + { + cin >> nc; + for (j = 0; j < nc; j++) + { + cin >> c; + links[i].push_back(c + P); + links[c + P].push_back(i); + Cost[i][c + P] = 1; + } + } + Cal(); + Free(); + } + return 0; +} diff --git a/10093.cpp b/10093.cpp index 7907616..5f9c901 100644 --- a/10093.cpp +++ b/10093.cpp @@ -1,31 +1,53 @@ -#include -using namespace std; - -int main(){ - string master = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - int max = master.length(); - string str; - while(cin >> str) { - int len = str.length(), i, ind = -1, ctr = 0; - char out = '0'; - for (i = 0; i < len; i++) { - if(str[i] > out) - out = str[i]; - if((str[i] < '0'||str[i] > '9') && (str[i] < 'A' || str[i] > 'Z') && (str[i] < 'a' || str[i] > 'z')) - ctr = -1; - } - for(i = 0; i < max; i++) { - if(out == master[i]) { - ind = i + 1; - break; - } - } - if (ind < 0 || ctr < 0) { - cout << "such number is impossible!\n"; - continue; - } - else - cout << ind << endl; - } - return 0; -} +#include + +using namespace std; + +int T[128] = {0}; + +int main() +{ + int i, b, s; + char c[12000]; + for (i = 0; i < 10; i++) + { + T['0' + i] = i; + } + for (i = 10; i < 36; i++) + { + T['A' + i - 10] = i; + } + for (i = 36; i < 62; i++) + { + T['a' + i - 36] = i; + } + while (scanf("%s", c) == 1) + { + for (i = 0, b = 1; c[i]; i++) + { + if (T[c[i]] > b) + { + b = T[c[i]]; + } + } + for (i = s = 0; c[i]; i++) + { + s += T[c[i]]; + } + for (b++; b < 63; b++) + { + if (s % (b - 1) == 0) + { + break; + } + } + if (b < 63) + { + printf("%d\n", b); + } + else + { + printf("such number is impossible!\n"); + } + } + return 0; +} diff --git a/10094.cpp b/10094.cpp new file mode 100644 index 0000000..6bf4a30 --- /dev/null +++ b/10094.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +bool check(int a[], int N) +{ + static char b[34096]; + memset(b, 0, sizeof(b)); + for (int i = 0; i < N; i++) + { + if (a[i] < 1 || a[i] > N) + { + return false; + } + if (b[a[i]] & 1) + { + return false; + } + b[a[i]] |= 1; + if (b[i + a[i]] & 2) + { + return false; + } + b[i + a[i]] |= 2; + if (b[a[i] - i + N] & 4) + { + return false; + } + b[a[i] - i + N] |= 4; + } + return true; +} + +bool gen(int a[], int N) +{ + int m; + m = 0; + for (int i = 2; i <= N; i += 2) + { + a[m++] = i; + } + for (int i = 1; i <= N; i += 2) + { + a[m++] = i; + } + if (m == N && check(a, N)) + { + return true; + } + m = 0; + for (int i = 4; i <= N; i += 2) + { + a[m++] = i; + } + if (N >= 2) + { + a[m++] = 2; + } + for (int i = 5; i <= N; i += 2) + { + a[m++] = i; + } + for (int i = 1; i <= 3 && i <= N; i += 2) + { + a[m++] = i; + } + if (m == N && check(a, N)) + { + return true; + } + m = 0; + for (int i = 2; i <= N; i += 2) + { + a[m++] = i; + } + for (int i = 3; i >= 1; i -= 2) + if (i <= N) + { + a[m++] = i; + } + for (int i = 7; i <= N; i += 2) + { + a[m++] = i; + } + if (5 <= N) + { + a[m++] = 5; + } + if (m == N && check(a, N)) + { + return true; + } + return false; +} + +int main() +{ + int a[2048], N; + while (scanf("%d", &N) == 1 && N > 0) + { + if (!gen(a, N)) + { + printf("Impossible\n"); + } + else + { + for (int i = 0; i < N; i++) + { + printf(i == 0 ? "%d" : " %d", a[i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/10095.cpp b/10095.cpp new file mode 100644 index 0000000..a7e307b --- /dev/null +++ b/10095.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +int maxpoints = 10000, n; +double eps = 1e-7; + +struct Point +{ + double x, y, z; +} p[10000], t[4], res, q[3]; +double r, m[3][3], sol[3], l[3], det; +double d() +{ + return m[0][0] * m[1][1] * m[2][2] + m[0][1] * m[1][2] * m[2][0] + + m[0][2] * m[1][0] * m[2][1] - m[0][2] * m[1][1] * m[2][0] - + m[0][1] * m[1][0] * m[2][2] - m[0][0] * m[1][2] * m[2][1]; +} +double dot(Point a, Point b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +void ball(int tt) +{ + if (tt == 0) + { + r = 0; + } + else if (tt == 1) + { + res = t[0]; + r = 0; + } + else if (tt == 2) + { + res.x = (t[0].x + t[1].x) / 2; + res.y = (t[0].y + t[1].y) / 2; + res.z = (t[0].z + t[1].z) / 2; + r = ((t[0].x - t[1].x) * (t[0].x - t[1].x) + + (t[0].y - t[1].y) * (t[0].y - t[1].y) + + (t[0].z - t[1].z) * (t[0].z - t[1].z)) / + 4; + } + else if (tt == 3) + { + for (int i = 0; i < 2; i++) + { + q[i].x = t[i + 1].x - t[0].x; + q[i].y = t[i + 1].y - t[0].y; + q[i].z = t[i + 1].z - t[0].z; + } + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + { + m[i][j] = 2 * dot(q[i], q[j]); + } + for (int i = 0; i < 2; i++) + { + sol[i] = dot(q[i], q[i]); + } + double det = m[0][0] * m[1][1] - m[0][1] * m[1][0]; + // if abs(det) < eps then exit; + l[0] = (sol[0] * m[1][1] - m[0][1] * sol[1]) / det; + l[1] = (m[0][0] * sol[1] - sol[0] * m[1][0]) / det; + res.x = t[0].x + q[0].x * l[0] + q[1].x * l[1]; + res.y = t[0].y + q[0].y * l[0] + q[1].y * l[1]; + res.z = t[0].z + q[0].z * l[0] + q[1].z * l[1]; + r = (res.x - t[0].x) * (res.x - t[0].x) + + (res.y - t[0].y) * (res.y - t[0].y) + + (res.z - t[0].z) * (res.z - t[0].z); + } + else + { + for (int i = 0; i < 3; i++) + { + q[i].x = t[i + 1].x - t[0].x; + q[i].y = t[i + 1].y - t[0].y; + q[i].z = t[i + 1].z - t[0].z; + } + for (int i = 0; i < 3; i++) + { + sol[i] = dot(q[i], q[i]); + } + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + { + m[i][j] = 2 * dot(q[i], q[j]); + } + double det = d(); + // if abs(det) < eps then exit; + for (int j = 0; j < 3; j++) + { + for (int i = 0; i < 3; i++) + { + m[i][j] = sol[i]; + } + l[j] = d() / det; + for (int i = 0; i < 3; i++) + { + m[i][j] = 2 * dot(q[i], q[j]); + } + } + res = t[0]; + for (int i = 0; i < 3; i++) + { + res.x += q[i].x * l[i]; + res.y += q[i].y * l[i]; + res.z += q[i].z * l[i]; + } + r = (res.x - t[0].x) * (res.x - t[0].x) + + (res.y - t[0].y) * (res.y - t[0].y) + + (res.z - t[0].z) * (res.z - t[0].z); + } +} + +void minball(int tt, int n) +{ + Point tmp; + ball(tt); + if (tt == 4) + { + return; + } + for (int i = 0; i < n; i++) + { + if ((res.x - p[i].x) * (res.x - p[i].x) + + (res.y - p[i].y) * (res.y - p[i].y) + + (res.z - p[i].z) * (res.z - p[i].z) > + r) + { + t[tt] = p[i]; + minball(tt + 1, i); + tmp = p[i]; + for (int j = i - 1; j >= 0; j--) + { + p[j + 1] = p[j]; + } + p[0] = tmp; + } + } +} + +int main() +{ + while (true) + { + scanf("%d", &n); + if (!n) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].z); + } + r = 0; + minball(0, n); + printf("%.4lf %.4lf %.4lf %.4lf\n", sqrt(r), res.x, res.y, res.z); + } + return 0; +} diff --git a/10097.cpp b/10097.cpp new file mode 100644 index 0000000..21c635a --- /dev/null +++ b/10097.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define maxn 102 + +struct ss +{ + int u, v; + int cost; +}; +queue Q; +char F[maxn][maxn]; +int Link[maxn][maxn], N, n1, n2, n3, game; + +int BFS() +{ + ss temp, dum; + int u, v; + F[n1][n2] = F[n2][n1] = 1; + temp.u = n1; + temp.v = n2; + temp.cost = 0; + Q.push(temp); + while (!Q.empty()) + { + dum = Q.front(); + Q.pop(); + u = Link[dum.u][dum.v]; + v = Link[dum.v][dum.u]; + if (u) + { + if (u == n3) + return dum.cost + 1; + if (F[u][dum.v] == 0) + { + F[u][dum.v] = F[dum.v][u] = 1; + temp.u = u; + temp.v = dum.v; + temp.cost = dum.cost + 1; + Q.push(temp); + } + } + if (v) + { + if (v == n3) + return dum.cost + 1; + if (F[v][dum.u] == 0) + { + F[v][dum.u] = F[dum.u][v] = 1; + temp.u = v; + temp.v = dum.u; + temp.cost = dum.cost + 1; + Q.push(temp); + } + } + } + return 0; +} + +void Cal() +{ + int d; + cin >> n1 >> n2 >> n3; + cout << "Game #" << game++ << endl; + if (n1 == n3 || n2 == n3) + { + cout << "Minimum Number of Moves = 0\n"; + return; + } + d = BFS(); + if (d > 0) + cout << "Minimum Number of Moves = " << d << endl; + else + cout << "Destination is Not Reachable !" << endl; +} + +int main() +{ + int i, j; + game = 1; + while (cin >> N && N) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + cin >> Link[i][j]; + F[i][j] = 0; + } + } + Cal(); + cout << endl; + while (!Q.empty()) + Q.pop(); + } + return 0; +} diff --git a/10098.cpp b/10098.cpp index 9474cad..88039d7 100644 --- a/10098.cpp +++ b/10098.cpp @@ -1,28 +1,30 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - char ch[10]={'\0'}; - cin>>ch; - int len=strlen(ch); - for(int j=0;jch[k]){ - char temp=ch[j]; - ch[j]=ch[k]; - ch[k]=temp; - } - } - } - do{ - for(int j=0;j + +using namespace std; + +template +void snail_sort(BidirectionalIterator first, BidirectionalIterator last) +{ + while (next_permutation(first, last)) + { + copy(first, last, ostream_iterator(cout, "")); + cout << endl; + } +} + +int main() +{ + int n, i, len; + cin >> n; + char str[15]; + for (i = 0; i < n; ++i) + { + cin >> str; + len = strlen(str); + sort(str, str + len); + cout << str << endl; + snail_sort(str, str + len); + cout << endl; + } + return 0; +} diff --git a/10099.cpp b/10099.cpp index 6f67f1e..c4de1cc 100644 --- a/10099.cpp +++ b/10099.cpp @@ -1,70 +1,69 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define MAX(A, B) (A > B ? A : B) +#define MIN(A, B) (A > B ? B : A) +#define MAXN 102 +#define INF 21474836 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +int A[MAXN][MAXN], N, M; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - int N, R; - scanf("%d%d", &N, &R); - if (N == 0 && R == 0) +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + A[i][j] = 0; + } +} +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i < N; i++) + { + for (j = 1 + i; j <= N; j++) + { + A[i][j] = A[j][i] = MAX(A[i][j], MIN(A[i][k], A[k][j])); + } + } + } +} +void Cal(int a, int b, int c) +{ + int p; + p = c / (A[a][b] - 1); + if (c > p * (A[a][b] - 1)) + { + p++; + } + printf("Minimum Number of Trips = %d\n", p); +} + +int main() +{ + int i, a, b, c; + int kase = 1; + while (scanf("%d%d", &N, &M) == 2) + { + if (!N && !M) + { break; - int mat[N][N]; - int i, j, k; - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = 0; - FOI(i, 1, R){ - int C1, C2, P; - scanf("%d%d%d", &C1, &C2, &P); - --C1; --C2; - mat[C1][C2] = P; - mat[C2][C1] = P; } - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = mat[j][i] = max(mat[i][j], min(mat[i][k], mat[k][j])); - int S, D, T; - scanf("%d%d%d", &S, &D, &T); - --S; --D; - printf("Scenario #%d\nMinimum Number of Trips = %d\n\n", t, (int)ceil((double)T / (mat[S][D] - 1.0))); + Ini(); + for (i = 0; i < M; i++) + { + scanf("%d%d%d", &a, &b, &c); + A[a][b] = A[b][a] = c; + } + Floyd(); + scanf("%d%d%d", &a, &b, &c); + printf("Scenario #%d\n", kase++); + Cal(a, b, c); + putchar('\n'); } return 0; } - diff --git a/101.cpp b/101.cpp index 333c866..c0bab7f 100644 --- a/101.cpp +++ b/101.cpp @@ -1,161 +1,101 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -int N; -vector< int > P(25); -stack < int > tmp; -stack < int > S[25]; +vector stk[25]; // blks +int pos[25]; // curr pos of each blk -void moveOnto(int A, int B) { - while (S[P[A]].top() != A) { - int t = S[P[A]].top(); - S[t].push(t); - P[t] = t; - S[P[A]].pop(); +inline void blk_init(int n) +{ + for (int i = 0; i < n; ++i) + { + stk[i].clear(); + stk[i].push_back(i); } - while (S[P[B]].top() != B) { - int t = S[P[B]].top(); - S[t].push(t); - P[t] = t; - S[P[B]].pop(); + for (int i = 0; i < n; ++i) + { + pos[i] = i; } - S[P[B]].pop(); - P[B] = B; - S[B].push(B); - S[B].push(S[P[A]].top()); - S[P[A]].pop(); - P[A] = B; } -void moveOver(int A, int B) { - while (S[P[A]].top() != A) { - int t = S[P[A]].top(); - S[t].push(t); - P[t] = t; - S[P[A]].pop(); - } - S[P[B]].push(S[P[A]].top()); - S[P[A]].pop(); - P[A] = P[B]; +inline bool same_stk(int x, int y) +{ + return pos[x] == pos[y]; } -void pileOnto(int A, int B) { - while (S[P[B]].top() != B) { - int t = S[P[B]].top(); - S[t].push(t); - P[t] = t; - S[P[B]].pop(); - } - S[P[B]].pop(); - P[B] = B; - S[B].push(B); - - while (S[P[A]].top() != A) { - tmp.push(S[P[A]].top()); - S[P[A]].pop(); - } - tmp.push(S[P[A]].top()); - S[P[A]].pop(); - - while (!tmp.empty()) { - int t = tmp.top(); - S[P[B]].push(t); - P[t] = P[B]; - tmp.pop(); +// clr all blks above 'x' +inline void clr_above(int x) +{ + int px = pos[x]; + while (stk[px].back() != x) + { + // place blk 'num' in its orig stk[num] + int num = stk[px].back(); + stk[num].push_back(num); + pos[num] = num; + stk[px].pop_back(); } } -void pileOver(int A, int B) { - while (S[P[A]].top() != A) { - tmp.push(S[P[A]].top()); - S[P[A]].pop(); - } - tmp.push(S[P[A]].top()); - S[P[A]].pop(); - - while (!tmp.empty()) { - int t = tmp.top(); - S[P[B]].push(t); - P[t] = P[B]; - tmp.pop(); +inline void mv(int x, int y) +{ + int px = pos[x], py = pos[y]; + vector::iterator mv_begin, mv_end; + mv_begin = find(stk[px].begin(), stk[px].end(), x); + mv_end = stk[px].end(); + // append blks (mv_begin ~ mv_end) to end of stk[pos[y]] + stk[py].insert(stk[py].end(), mv_begin, mv_end); + // upd pos[] + vector::const_iterator it; + for (it = mv_begin; it != mv_end; ++it) + { + pos[(*it)] = py; } + stk[px].erase(mv_begin, mv_end); } -void quit() { - int i, j; - FOI(i, 0, N-1) { - cout << i << ":"; - while (!S[i].empty()) { - tmp.push(S[i].top()); - S[i].pop(); +inline void print_result(int num_blks) +{ + for (int i = 0; i < num_blks; ++i) + { + printf("%d:", i); + for (int j = 0; j < (int)stk[i].size(); ++j) + { + printf(" %d", stk[i][j]); } - while (!tmp.empty()) { - cout << " " << tmp.top(); - tmp.pop(); - } - cout << endl; + putchar('\n'); } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - cin >> N; - int i, j; - FOI(i, 0, N-1) { - S[i].push(i); - P[i] = i; - } - while (true) { - string A, B; - int I, J; - cin >> A; - if (A == "quit") - break; - cin >> I >> B >> J; - if (I == J || P[I] == P[J]) - continue; - if (A == "move" && B == "onto") { - moveOnto(I, J); +int main() +{ + int num_blks; + scanf("%d", &num_blks); + blk_init(num_blks); + char cmd1[10], cmd2[10]; + int a, b; + // [mv/pile] a [onto/over] b + while (scanf("%s %d %s %d", cmd1, &a, cmd2, &b) == 4) + { + // ign invalid cmd + if (a == b) + { continue; } - if (A == "move" && B == "over") { - moveOver(I, J); + if (same_stk(a, b)) + { continue; } - if (A == "pile" && B == "onto") { - pileOnto(I, J); - continue; + if (strcmp("mv", cmd1) == 0) + { + clr_above(a); + } + if (strcmp("onto", cmd2) == 0) + { + clr_above(b); } - pileOver(I, J); + mv(a, b); } - quit(); + print_result(num_blks); return 0; } diff --git a/10100.cpp b/10100.cpp index 9750067..3afa17b 100644 --- a/10100.cpp +++ b/10100.cpp @@ -1,100 +1,97 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int LCSLength(vector X, vector Y){ - int m = X.size(); - int n = Y.size(); - int c[m+1][n+1], i, j; - for(i=0; i<=m; i++) - c[i][0] = 0; - for(j=0; j<=n; j++) - c[0][j] = 0; - for(i=1; i<=m; i++) - for(j=1; j<=n; j++){ - if(X[i-1] == Y[j-1]) - c[i][j] = c[i-1][j-1] + 1; - else if(c[i-1][j] >= c[i][j-1]) - c[i][j] = c[i-1][j]; - else - c[i][j] = c[i][j-1]; - } - return c[m][n]; -} - -int main(){ - int i, j, t=1; - string strA, strB; - while(getline(cin, strA, '\n')){ - getline(cin, strB, '\n'); - vector < string > arrA; - vector < string > arrB; - int lenA=strA.length(), lenB=strB.length(); - string temp=""; - for(i=0; i + +using namespace std; + +#define MAX 1003 + +char strf[MAX][21], strs[MAX][21], str[MAX]; +int LIS[MAX][MAX], fg, fl, sl; + +void Sep(char ss[][21], int &n) +{ + char *p; + p = strtok(str, " "); + while (p) + { + strcpy(ss[n++], p); + p = strtok(NULL, " "); + } +} +int Dp() +{ + int i, j; + if (sl == 1 || fl == 1) + { + return 0; + } + for (i = 1; i < fl; i++) + { + for (j = 1; j < sl; j++) + { + if (!strcmp(strf[i], strs[j])) + { + LIS[i][j] = LIS[i - 1][j - 1] + 1; + } + else + { + LIS[i][j] = max(LIS[i][j - 1], LIS[i - 1][j]); + } + } + } + return LIS[fl - 1][sl - 1]; +} +int Ini() +{ + int i; + for (i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = NULL; + break; + } + if (!isalpha(str[i]) && !isdigit(str[i])) + { + str[i] = ' '; + } + } + if (strlen(str) == 0) + { + return 1; + } + return 0; +} +void Cal() +{ + int k; + k = Dp(); + printf("Length of longest match: %d\n", k); +} + +int main() +{ + int i, k = 1; + while (gets(str)) + { + printf("%2d. ", k++); + i = Ini(); + fl = sl = 1; + if (i) + { + gets(str); + printf("Blank!\n"); + continue; + } + Sep(strf, fl); + gets(str); + i = Ini(); + if (i) + { + printf("Blank!\n"); + continue; + } + Sep(strs, sl); + Cal(); + } + return 0; +} diff --git a/10101.cpp b/10101.cpp index a9f579f..7b07040 100644 --- a/10101.cpp +++ b/10101.cpp @@ -1,39 +1,77 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - long long val,test=1; - while(scanf("%ld",&val)!=EOF){ - string master[]={" kuti", " lakh", " hajar", " shata", " kuti", " lakh", " hajar", " shata", ""}; - long long n=val; - long long num[9]={0}; - num[8]=n%100; n/=100; -// if(n==0) goto XXX; - num[7]=n%10; n/=10; -// if(n==0) goto XXX; - num[6]=n%100; n/=100; -// if(n==0) goto XXX; - num[5]=n%100; n/=100; -// if(n==0) goto XXX; - num[4]=n%100; n/=100; -// if(n==0) goto XXX; - num[3]=n%10; n/=10; -// if(n==0) goto XXX; - num[2]=n%100; n/=100; -// if(n==0) goto XXX; - num[1]=n%100; n/=100; -// if(n==0) goto XXX; - num[0]=n%100; n/=100; - int i=0; - while(num[i]==0) i++; - printf("%4d.",test++); - for(i=0;i<9;i++){ - if(num[i]!=0) - cout<<" "< + +using namespace std; + +int main() +{ + long long int n, a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, q, r, s; + int count = 0; + while (scanf("%lld", &n) == 1) + { + count++; + printf("%4d.", count); + b = n / 100000000000000; + if (b > 0) + { + printf(" %lld kuti", b); + } + c = n % 100000000000000; + d = c / 1000000000000; + if (d > 0) + { + printf(" %lld lakh", d); + } + e = c % 1000000000000; + f = e / 10000000000; + if (f > 0) + { + printf(" %lld hajar", f); + } + g = e % 10000000000; + h = g / 1000000000; + if (h > 0) + { + printf(" %lld shata", h); + } + i = g % 1000000000; + j = i / 10000000; + if (j == 0 && n > 10000000) + { + printf(" kuti"); + } + if (j > 0) + { + printf(" %lld kuti", j); + } + k = i % 10000000; + l = k / 100000; + if (l > 0) + { + printf(" %lld lakh", l); + } + m = k % 100000; + o = m / 1000; + if (o > 0) + { + printf(" %lld hajar", o); + } + p = m % 1000; + q = p / 100; + if (q > 0) + { + printf(" %lld shata", q); + } + r = p % 100; + s = r / 1; + if (s > 0) + { + printf(" %lld", s); + } + if (n == 0) + { + printf(" %lld", n); + } + printf("\n"); + } + return 0; +} diff --git a/10102.cpp b/10102.cpp index bb1630e..00aca0f 100644 --- a/10102.cpp +++ b/10102.cpp @@ -1,101 +1,84 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define INF 1<<30 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -string mat[500]; -string use[500]; -int N; +typedef unsigned long long ULL; +typedef long long LL; -void display(string matrix[]){ - int i; - FOI(i, 0, N-1) - cout << matrix[i] << endl; - - cout << endl; -} - +vector G; +int dist[1000][1000]; +int cx[] = {0, 0, 1, -1}; +int cy[] = {1, -1, 0, 0}; +vector> axis1; +vector> axis3; +int M; -struct Node{ - int II, JJ, DD; - Node(){}; - Node(int _I, int _J, int _D){ - II = _I; JJ = _J; DD = _D; +void BFS() +{ + queue> Q; + memset(dist, -1, sizeof(dist)); + for (int i = 0; i < axis3.size(); i++) + { + int x = axis3[i].first; + int y = axis3[i].second; + dist[x][y] = 0; + Q.push(make_pair(x, y)); } -}; - -int dI[] = {0, -1, 0, 1}; -int dJ[] = {-1, 0, 1, 0}; - -int bfs(int I, int J){ - queue q; - q.push(Node(I, J, 0)); - while( !q.empty() ){ - Node n = q.front(); - q.pop(); - if( n.II < 0 || n.II >= N ) - continue; - if( n.JJ < 0 || n.JJ >= N ) - continue; - if( use[n.II][n.JJ] == '0' ) - continue; - - if( use[n.II][n.JJ] == '3' ) - return n.DD; - use[n.II][n.JJ] = '0'; - int j; - FOI(j, 0, 3) - q.push( Node(n.II + dI[j], n.JJ + dJ[j], n.DD + 1) ); + while (!Q.empty()) + { + int x = Q.front().first; + int y = Q.front().second; + Q.pop(); + for (int i = 0; i < 4; i++) + { + int tempx = x + cx[i]; + int tempy = y + cy[i]; + if (tempx >= 0 && tempx < M && tempy >= 0 && tempy < M && + dist[tempx][tempy] == -1) + { + dist[tempx][tempy] = dist[x][y] + 1; + Q.push(make_pair(tempx, tempy)); + } + } } - return 0; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( cin >> N ){ - int i, j, k; - FOI(i, 0, N-1) - cin >> mat[i]; - int gmin = 1; - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - if( mat[i][j] == '1' ){ - FOI(k, 0, N-1) - use[k] = mat[k]; - //display(use); - gmin = max(gmin, bfs(i, j)); +int main() +{ + while (cin >> M) + { + int maxstep = 0; + axis1.clear(); + axis3.clear(); + G.resize(M); + for (int i = 0; i < M; i++) + { + string line; + cin >> line; + G[i] = line; + for (int j = 0; j < M; j++) + { + if (G[i][j] == '1') + { + axis1.push_back(make_pair(i, j)); + } + if (G[i][j] == '3') + { + axis3.push_back(make_pair(i, j)); } } } - //display(use); - cout << gmin << endl; + BFS(); + for (int i = 0; i < axis1.size(); i++) + { + int x = axis1[i].first; + int y = axis1[i].second; + if (dist[x][y] > maxstep) + { + maxstep = dist[x][y]; + } + } + cout << maxstep << endl; } return 0; } diff --git a/10103.cpp b/10103.cpp new file mode 100644 index 0000000..15b68ec --- /dev/null +++ b/10103.cpp @@ -0,0 +1,231 @@ +#include + +using namespace std; + +char way[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; + +char ary[10][10][11], queue_[1001][3], chk[21][21][21], rgb[3] = {'R', 'G', 'B'}, ans, color; +int conum[3], n; + +char valid(int x, int y, int z) +{ + return x > -1 && x < n && y > -1 && y < n && z > -1 && z < n; +} + +char chkmov(int x, int y, int z, char col) +{ + return !ary[x][y][z] || ary[x][y][z] == col; +} + +char chkvalid(int x, int y, int z) +{ + return x >= -n && x <= n && y >= -n && y <= n && z >= -n && z <= n; +} + +char movable(int type, char col) +{ + int i, j, k, it, jt, kt; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (ary[i][j][k] == col) + { + it = i + way[type][0]; + jt = j + way[type][1]; + kt = k + way[type][2]; + if (valid(it, jt, kt) && ary[it][jt][kt] != col) + { + return 0; + } + } + } + } + } + return 1; +} + +void remove(char col) +{ + int i, j, k; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (ary[i][j][k] == col) + { + ary[i][j][k] = 0; + } + } + } + } +} + +void move(char col, int num) +{ + int i, j, k, l, p, q, x, y, z, xt, yt, zt, c; + for (p = 0, q = 1; p < q; p++) + { + x = queue_[p][0]; + y = queue_[p][1]; + z = queue_[p][2]; + for (l = 0; l < 6; l++) + { + xt = x + way[l][0]; + yt = y + way[l][1]; + zt = z + way[l][2]; + if (chkvalid(xt, yt, zt) && chk[xt + 10][yt + 10][zt + 10]) + { + chk[xt + 10][yt + 10][zt + 10] = 0; + for (i = 0, c = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (ary[i][j][k] == col) + { + if (valid(i + xt, j + yt, k + zt)) + { + if (!chkmov(i + xt, j + yt, k + zt, col)) + { + break; + } + } + else + { + c++; + } + } + } + if (k < n) + { + break; + } + } + if (j < n) + { + break; + } + } + if (i == n) + { + if (c == num) + { + ans = 3; + return; + } + else + { + queue_[q][0] = xt; + queue_[q][1] = yt; + queue_[q++][2] = zt; + } + } + } + } + } +} + +int main() +{ + int i, j, k; + while (scanf("%d", &n) == 1) + { + for (i = 0, conum[0] = conum[1] = conum[2] = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + scanf("%s", &ary[i][j]); + for (k = 0; k < n; k++) + { + if (ary[i][j][k] == 'R') + { + conum[0]++; + } + else if (ary[i][j][k] == 'G') + { + conum[1]++; + } + else + { + conum[2]++; + } + } + } + } + ans = 0; + for (i = 0; i < 6; i++) + { + if (movable(i, 'R')) + { + ans++; + color = 'R'; + conum[0] = 0; + break; + } + } + for (i = 0; i < 6; i++) + { + if (movable(i, 'G')) + { + ans++; + color = 'G'; + conum[1] = 0; + break; + } + } + for (i = 0; i < 6; i++) + { + if (movable(i, 'B')) + { + ans++; + color = 'B'; + conum[2] = 0; + break; + } + } + if (ans) + { + if (ans > 1) + { + printf("RGB\n"); + } + else + { + remove(color); + for (i = 0, j = 1001; i < 3; i++) + { + if (conum[i]) + { + if (conum[i] < j) + { + j = conum[i]; + k = i; + } + } + } + memset(chk, 1, sizeof(chk)); + queue_[0][0] = queue_[0][1] = queue_[0][2] = chk[10][10][10] = 0; + move(rgb[k], conum[k]); + if (ans == 3) + { + printf("RGB\n"); + } + else + { + printf("%c\n", color); + } + } + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/10104.cpp b/10104.cpp index d826ead..ec86ea3 100644 --- a/10104.cpp +++ b/10104.cpp @@ -1,54 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int64 G[3]; -void Ext_gcd(int64 a,int64 b) { - if(b == 0){ - G[0] = a; G[1] = 1; G[2] = 0; - return; - } - Ext_gcd(b,a%b); - int64 x = G[1]; - G[0] = G[0]; G[1] = G[2]; G[2] = (x - ((a/b)*G[2])); -} - -int main(){ - int64 A, B; - while(cin>>A>>B){ - Ext_gcd(A, B); - cout< + +using namespace std; + +int main() +{ + int s = 1, t = 0, q, r; + int u = 0, v = 1, a, b, x, y; + while (scanf("%d%d", &a, &b) != EOF) + { + s = 1; + v = 1, t = u = 0; + while (b > 0) + { + q = a / b; + r = a - q * b; + x = s - q * u; + y = t - q * v; + a = b; + s = u; + t = v; + b = r; + u = x; + v = y; + } + printf("%d %d %d\n", s, t, a); + } + return 0; +} diff --git a/10105.cpp b/10105.cpp new file mode 100644 index 0000000..3e302a8 --- /dev/null +++ b/10105.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +// @ http://www.ddj.com/cpp/184403801 +template +struct StringTok +{ + StringTok(const T &seq, typename T::size_type pos = 0) : seq_(seq), pos_(pos) {} + T operator()(const T &delim); + + const T &seq_; + typename T::size_type pos_; +}; + +template +T StringTok::operator()(const T &delim) +{ + T token; + if (pos_ != T::npos) + { + // start of found token + typename T::size_type first(seq_.find_first_not_of(delim.c_str(), pos_)); + if (first != T::npos) + { + // length of found token + typename T::size_type num(seq_.find_first_of(delim.c_str(), first) - first); + // do all the work off to the side + token = seq_.substr(first, num); + // done; now commit using + // nonthrowing operations only + pos_ = first + num; + if (pos_ != T::npos) + { + ++pos_; + } + if (pos_ >= seq_.size()) + { + pos_ = T::npos; + } + } + } + return token; +} + +// @ http://www.gotw.ca/gotw/029.htm for case-insensitive lexicographic compares +struct ci_char_traits : public char_traits +{ + static bool eq(char c1, char c2) + { + return toupper(c1) == toupper(c2); + } + static bool ne(char c1, char c2) + { + return toupper(c1) != toupper(c2); + } + static bool lt(char c1, char c2) + { + return toupper(c1) < toupper(c2); + } + static int compare(const char *s1, const char *s2, size_t n) + { + return strncasecmp(s1, s2, n); + } + static const char *find(const char *s, int n, char a) + { + while (n-- > 0 && toupper(*s) != toupper(a)) + { + ++s; + } + return s; + } +}; + +// no boost makes c++ angry; lexical cast comes in handy for string <=> int conversion without expliucitly writing out the streams all the time. +template +T lexical_cast(const S &arg) +{ + stringstream interpreter; + T res; + if (!(interpreter << arg) || !(interpreter >> res) || !(interpreter >> ws).eof()) + { + throw logic_error("bad lexical_cast"); + } + return res; +} + +typedef vector vs; +typedef vector vi; +typedef basic_string ci_string; + +int fact(int n) +{ + int res(1); + for (int i(1); i <= n; i++) + { + res *= i; + } + return res; +} + +int main() +{ + int n, k; + while (cin >> n >> k) + { + int prod(1); + for (int i(0); i < k; ++i) + { + int temp; + cin >> temp; + prod *= fact(temp); + } + cout << (fact(n) / prod) << endl; + } + return 0; +} diff --git a/10106.cpp b/10106.cpp new file mode 100644 index 0000000..103c1a5 --- /dev/null +++ b/10106.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +char mid[100000], in[100000]; + +int main() +{ + char a[1000], s[1000], g[2], ch; + int i, j, k, c = 0, d = 0, pp, cary, end; + int m, sum, cary_1, l, x = 0, y = 0; + int ck = 0, ck_1 = 1, ck_2 = 1; + while ((ch = getc(stdin)) != EOF) + { + if (ch != '\n' && ck_1) + { + g[0] = ch; + g[1] = NULL; + a[x++] = atoi(g); + } + else if (ck_2) + { + ck_1 = 0; + if (ch == '\n') + { + ck++; + if (ck == 2) + { + goto done; + } + continue; + } + g[0] = ch; + g[1] = NULL; + s[y++] = atoi(g); + if (ck == 2) + { + ck_2 = 0; + } + } + else + { + done: + for (i = 0; i < 1000; i++) + { + mid[i] = in[i] = 0; + } + for (i = y - 1; i >= 0; i--) + { + k = c; + cary = 0; + for (j = x - 1; j >= 0; j--) + { + pp = s[i] * a[j] + cary; + cary = pp / 10; + in[k++] = pp % 10; + } + if (cary) + { + in[k++] = cary; + } + if (k > d) + { + end = k; + } + else + { + end = d; + } + d = 0; + cary_1 = 0; + for (m = 0; m < end; m++) + { + sum = in[m] + mid[m] + cary_1; + mid[d++] = sum % 10; + cary_1 = sum / 10; + } + if (cary_1) + { + mid[d++] = cary_1; + } + for (l = 0; l < k; l++) + { + in[l] = 0; + } + c++; + } + for (l = d - 1; !mid[l]; l--) + ; + if (l < 0) + { + printf("0"); + goto st; + } + for (i = l; i >= 0; i--) + { + printf("%d", mid[i]); + } + for (i = 0; i < 1000; i++) + { + a[i] = s[i] = 0; + } + st: + printf("\n"); + ck = x = y = c = 0; + ck_1 = ck_2 = 1; + } + } + return 0; +} diff --git a/10107.cpp b/10107.cpp index b4bd330..e72dac0 100644 --- a/10107.cpp +++ b/10107.cpp @@ -1,54 +1,55 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - vector vec; - vector ::iterator it; - int64 num, val; - while(cin>>num){ - - vec.push_back(num); - - sort(vec.begin(), vec.end()); - - int len = (int) vec.size(); - int cen = (int) len/2; - - if(len%2 == 0) - val = ( vec[cen] + vec[cen-1] ) / 2; - else - val = vec[cen]; - - cout< + +using namespace std; + +int main() +{ + int x, array[10010], num = 0, i, j; + while (cin >> x) + { + num++; + if (num == 1) + { + array[0] = x; + } + else + { + if (x == 0) + { + for (j = num; j > 0; j--) + { + array[j] = array[j - 1]; + } + array[0] = x; + } + else + { + for (i = 1; i < num; i++) + { + if (x >= array[i - 1] && x <= array[i]) + { + for (j = num; j > i; j--) + { + array[j] = array[j - 1]; + } + array[j] = x; + break; + } + } + if (i == num) + { + array[i - 1] = x; + } + } + } + if (num % 2 == 1) + { + cout << array[num / 2] << endl; + } + else + { + cout << (array[num / 2 - 1] + array[num / 2]) / 2 << endl; + } + } + return 0; +} diff --git a/10109.cpp b/10109.cpp new file mode 100644 index 0000000..a40bc3e --- /dev/null +++ b/10109.cpp @@ -0,0 +1,274 @@ +#include + +using namespace std; + +long long gcd(long long a, long long b) +{ + if (b == 0) + { + return a; + } + return gcd(b, a % b); +} + +class fraction +{ +public: + long long n, d; + fraction(long long n, long long d) + { + this->n = n; + this->d = d; + long long g = gcd(abs(n), abs(d)); + n /= g; + d /= g; + if (n == 0) + { + d = 1; + } + if (n < 1 && d < 1) + { + n *= -1; + d *= -1; + } + else if (n < 1 || d < 1) + { + n = abs(n); + d = abs(d); + n *= -1; + } + } + fraction() + { + n = 0; + d = 1; + } +}; + +bool hasSol; +int rankA, rankAC, N, NumberOfUnknowns, NumberOfEquations; +fraction mat[50][50], x[50]; + +void simplify(fraction &f) +{ + long long g = gcd(abs(f.n), abs(f.d)); + f.n /= g; + f.d /= g; + if (f.n == 0) + { + f.d = 1; + } + if (f.n < 1 && f.d < 1) + { + f.n *= -1; + f.d *= -1; + } + else if (f.n < 1 || f.d < 1) + { + f.n = abs(f.n); + f.d = abs(f.d); + f.n *= -1; + } +} + +fraction operator/(const fraction &f1, const fraction &f2) +{ + fraction ret(f1.n * f2.d, f1.d * f2.n); + simplify(ret); + return ret; +} + +fraction operator*(const fraction &f1, const fraction &f2) +{ + fraction ret(f1.n * f2.n, f1.d * f2.d); + simplify(ret); + return ret; +} + +fraction operator+(const fraction &f1, const fraction &f2) +{ + fraction ret((f1.n * f2.d) + (f2.n * f1.d), f1.d * f2.d); + simplify(ret); + return ret; +} + +fraction operator+=(fraction &f1, const fraction &f2) +{ + return f1 = f1 + f2; +} + +fraction operator*(long long &x, const fraction &f) +{ + fraction ret(x * f.n, f.d); + simplify(ret); + return ret; +} +fraction operator*=(fraction &f1, const fraction &f2) +{ + return f1 = f1 * f2; +} +fraction operator-(const fraction &f) +{ + fraction ret(f.n, f.d); + ret.n *= -1; + simplify(ret); + return ret; +} + +fraction operator-(const fraction &f1, const fraction &f2) +{ + fraction ret = f1 + (-f2); + return ret; +} + +bool operator>(const fraction &f1, const fraction &f2) +{ + bool ret = abs(f1.n * f2.d) > abs(f2.n * f1.d); + return ret; +} + +void printMatrix() +{ + for (int i = 1; i <= NumberOfEquations; ++i) + { + for (int j = 1; j <= NumberOfUnknowns + 1; ++j) + printf("%lld/%lld ", mat[i][j].n, mat[i][j].d); + printf("\n"); + } +} + +void swapRows(int r1, int r2) +{ + for (int j = 1; j <= NumberOfUnknowns + 1; ++j) + swap(mat[r1][j], mat[r2][j]); +} + +void gauss() +{ + for (int p = 1; p <= NumberOfUnknowns; ++p) + { + int maxR = p; + for (int i = p + 1; i <= NumberOfEquations; ++i) + if (mat[i][p] > mat[maxR][p]) + maxR = i; + swapRows(p, maxR); + if (mat[p][p].n != 0) + { + fraction x(mat[p][p].d, mat[p][p].n); + for (int i = 1; i <= (NumberOfUnknowns + 1); ++i) + mat[p][i] *= x; + for (int i = p + 1; i <= NumberOfEquations; ++i) + { + x = (-mat[i][p]); + for (int j = 1; j <= NumberOfUnknowns + 1; ++j) + mat[i][j] += (x * mat[p][j]); + } + } + } +} + +void rank_() +{ + rankA = rankAC = NumberOfEquations; + bool allZeroes; + for (int i = 1; i <= NumberOfEquations; ++i) + { + allZeroes = true; + for (int j = 1; j <= (NumberOfUnknowns); ++j) + { + if (mat[i][j].n != 0) + { + allZeroes = false; + break; + } + } + if (allZeroes) + { + --rankA; + if (mat[i][NumberOfUnknowns + 1].n == 0) + --rankAC; + } + } +} + +void back() +{ + for (int p = NumberOfUnknowns; p >= 1; --p) + { + fraction s(0, 1); + for (int k = p + 1; k <= NumberOfUnknowns; ++k) + s += (x[k] * mat[p][k]); + x[p] = (mat[p][NumberOfUnknowns + 1] - s) / mat[p][p]; + } +} + +int main() +{ + char c; + bool isFirst = true; + while (true) + { + scanf("%d", &N); + if (N == 0) + { + break; + } + if (!isFirst) + { + printf("\n"); + } + scanf("%d%d", &NumberOfUnknowns, &NumberOfEquations); + for (int i = 1; i <= NumberOfEquations; ++i) + { + for (int j = 1; j <= (NumberOfUnknowns + 1); ++j) + { + scanf("%lld", &mat[i][j].n); + scanf("%c", &c); + if (c == '/') + { + scanf("%lld", &mat[i][j].d); + } + else + { + mat[i][j].d = 1; + } + } + } + hasSol = true; + gauss(); + rank_(); + if (rankAC != rankA) + { + hasSol = false; + } + if (hasSol && rankAC == NumberOfUnknowns) + { + back(); + } + printf("Solution for Matrix System # %d\n", N); + if (!hasSol) + { + printf("No Solution.\n"); + } + else + { + if (rankAC < NumberOfUnknowns) + { + printf("Infinitely many solutions containing %d arbitrary constants.\n", NumberOfUnknowns - rankAC); + } + else + { + for (int i = 1; i <= NumberOfUnknowns; ++i) + { + printf("x[%d] = ", i); + if (x[i].d == 1) + printf("%lld\n", x[i].n); + else + printf("%lld/%lld\n", x[i].n, x[i].d); + } + } + } + isFirst = false; + } + return 0; +} diff --git a/1011.cpp b/1011.cpp new file mode 100644 index 0000000..dd6e074 --- /dev/null +++ b/1011.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define eps 1e-8 + +int main() +{ + int n, m; + int cases = 0; + while (scanf("%d %d", &n, &m) == 2 && n + m) + { + double x[50], y[50]; + double g[50][50]; + double dp[50]; + int used[50] = {}; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x[i], &y[i]); + dp[i] = 1e+6; + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + double d = hypot(x[i] - x[j], y[i] - y[j]); + g[i][j] = d; + } + } + dp[n - 1] = 0; + for (int i = 0; i < n; i++) + { + int u = -1; + for (int j = 0; j < n; j++) + { + if (used[j] == 0 && (u == -1 || dp[u] > dp[j])) + { + u = j; + } + } + used[u] = 1; + for (int j = 0; j < n; j++) + { + if (used[j]) + { + continue; + } + double w = g[u][j]; + if (m >= dp[u] + 2 * w) + { + dp[j] = min(dp[j], dp[u] + w); + } + else if (m >= 3 * w) + { + int times = ceil((dp[u] - (m - 2 * w)) / (m - 3 * w)); + dp[j] = min(dp[j], dp[u] + times * (2 * w) + w); + } + } + } + printf("Trial %d: ", ++cases); + if (dp[0] != 1e+6) + { + printf("%.0lf units of food\n", ceil(dp[0])); + } + else + { + puts("Impossible"); + } + puts(""); + } + return 0; +} diff --git a/10110.cpp b/10110.cpp index 558c661..81fd8ed 100644 --- a/10110.cpp +++ b/10110.cpp @@ -1,19 +1,25 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - for(;;){ - long long n,sqr; - scanf("%lld",&n); - if(n==0) - break; - sqr=(int)sqrt(n); - if(sqr*sqr==n) - printf("yes\n"); - else - printf("no\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + long long n, sq; + while (cin >> n) + { + if (n == 0) + { + break; + } + sq = (long long)sqrt(n); + if (n == sq * sq) + { + printf("yes\n"); + } + else + { + printf("no\n"); + } + } + return 0; +} diff --git a/10111.cpp b/10111.cpp new file mode 100644 index 0000000..ab24ec8 --- /dev/null +++ b/10111.cpp @@ -0,0 +1,214 @@ +#include + +using namespace std; + +char B[5][5]; + +struct sss +{ + int r, c; +} X[5], Y[5]; +struct dig +{ + int d1, d2; +} xx, yy; +int Recur(int r, int c, int pice, int level) +{ + int i, j, f = 0; + if (pice == 0) + { + if (X[r].r + 1 == 4) + { + return 1; + } + if (X[c].c + 1 == 4) + { + return 1; + } + if (r == c) + { + if (xx.d2 + 1 == 4) + { + return 1; + } + xx.d2++; + } + if (r + c == 3) + { + if (xx.d1 + 1 == 4) + { + return 1; + } + xx.d1++; + } + B[r][c] = 'x'; + X[r].r++; + X[c].c++; + } + else if (pice == 1) + { + if (Y[r].r + 1 == 4) + { + return 1; + } + if (Y[c].c + 1 == 4) + { + return 1; + } + if (r == c) + { + if (yy.d2 + 1 == 4) + { + return 1; + } + yy.d2++; + } + if (r + c == 3) + { + if (yy.d1 + 1 == 4) + { + return 1; + } + yy.d1++; + } + B[r][c] = 'o'; + Y[r].r++; + Y[c].c++; + } + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (B[i][j] == '.') + { + if (Recur(i, j, !pice, level + 1)) + { + f = 1; + break; + } + } + } + if (f) + { + break; + } + } + if (pice == 0) + { + B[r][c] = '.'; + X[r].r--; + X[c].c--; + if (r == c) + { + xx.d2--; + } + if (r + c == 3) + { + xx.d1--; + } + } + else + { + B[r][c] = '.'; + Y[r].r--; + Y[c].c--; + if (r == c) + { + yy.d2--; + } + if (r + c == 3) + { + yy.d1--; + } + } + if (f) + { + return 0; + } + return 1; +} +int Cal() +{ + int i, total = 0, j; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (B[i][j] == 'x') + { + X[i].r++; + X[j].c++; + if (i == j) + { + xx.d2++; + } + if (i + j == 3) + { + xx.d1++; + } + } + else if (B[i][j] == 'o') + { + Y[i].r++; + Y[j].c++; + if (i == j) + { + yy.d2++; + } + if (i + j == 3) + { + yy.d1++; + } + } + } + } + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (B[i][j] == '.') + { + if (Recur(i, j, 0, 0)) + { + printf("(%d,%d)\n", i, j); + return 1; + } + } + } + } + return 0; +} +void Free() +{ + int i; + for (i = 0; i < 4; i++) + { + X[i].r = X[i].c = 0; + Y[i].c = Y[i].r = 0; + } + xx.d1 = yy.d1 = 0; + xx.d2 = yy.d2 = 0; +} + +int main() +{ + char ss[10]; + int i; + while (gets(ss)) + { + if (!strcmp(ss, "$")) + { + break; + } + for (i = 0; i < 4; i++) + { + gets(B[i]); + } + if (!Cal()) + { + puts("#####"); + } + Free(); + } + return 0; +} diff --git a/10112.cpp b/10112.cpp new file mode 100644 index 0000000..7e3b84f --- /dev/null +++ b/10112.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +int numOfPoint; + +class Point +{ +public: + double x, y; + char alpha; +} point[20]; + +class MaxRecord +{ +public: + double maxArea; + int i, j, k; +} maxRec; + +double distance(Point *a, Point *b) +{ + // hypot(a,b)? + double t = (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y); + return sqrt(t); +} + +double area(Point *a, Point *b, Point *c) +{ + double A, AC, AB, BC; + AC = distance(a, c); + AB = distance(a, b); + BC = distance(b, c); + double tmp; + tmp = (AC * AC + AB * AB - BC * BC) / (2.0 * AC * AB); + A = acos(tmp); + return 0.5 * AC * AB * sin(A); +} + +bool isLegal(Point *a, Point *b, Point *c, Point *judge) +{ + double area0, area1, area2, area3; + area0 = area(a, b, c); + area1 = area(a, b, judge); + area2 = area(a, judge, c); + area3 = area(judge, b, c); + if (fabs(area0 - (area1 + area2 + area3)) <= 0.00001) + { + return false; + } + return true; +} + +bool notContain(int i, int j, int k) +{ + int t; + for (t = 0; t < numOfPoint; t++) + { + if (t != i && t != j && t != k) + { + if (!isLegal(&point[i], &point[j], &point[k], &point[t])) + { + return false; + } + } + } + return true; +} + +void solve() +{ + double ar; + int i, j, k; + maxRec.maxArea = 0; + for (i = 0; i < numOfPoint - 2; i++) + { + for (j = i + 1; j < numOfPoint - 1; j++) + { + for (k = j + 1; k < numOfPoint; k++) + { + ar = area(&point[i], &point[j], &point[k]); + if (ar > maxRec.maxArea && notContain(i, j, k)) + { + maxRec.maxArea = ar; + maxRec.i = i; + maxRec.j = j; + maxRec.k = k; + } + } + } + } + cout << point[maxRec.i].alpha << point[maxRec.j].alpha << point[maxRec.k].alpha << endl; +} + +int main() +{ + while (cin >> numOfPoint && numOfPoint) + { + int i; + for (i = 0; i < numOfPoint; i++) + { + cin >> point[i].alpha >> point[i].x >> point[i].y; + } + solve(); + } + return 0; +} diff --git a/10113.cpp b/10113.cpp new file mode 100644 index 0000000..af423e7 --- /dev/null +++ b/10113.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +#define MAXN 102 + +struct ss +{ + int a; + int b; + char f; +} V[MAXN][MAXN]; +struct node +{ + char city[31]; + int index; + node *next, *left, *right; +}; +node *root; +int Q[MAXN], QH, QT, IN, P[MAXN]; +int N[MAXN]; +char F[MAXN]; +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} +void Insert(node *rt, char city[], int in) +{ + node *p = (node *)malloc(sizeof(node)); + strcpy(p->city, city); + p->index = in; + p->left = p->right = NULL; + if (rt == NULL) + { + root = p; + return; + } + node *x = root; + while (1) + { + if (strcmp(x->city, city) < 0) + { + if (x->right == NULL) + { + x->right = p; + return; + } + else + { + x = x->right; + } + } + else + { + if (x->left == NULL) + { + x->left = p; + return; + } + else + { + x = x->left; + } + } + } +} +int Search(node *rt, char city[]) +{ + node *x = root; + while (x != NULL) + { + if (!strcmp(x->city, city)) + { + return x->index; + } + if (strcmp(x->city, city) < 0) + { + x = x->right; + } + else + { + x = x->left; + } + } + return -1; +} +void Set(char city1[], int cost1, char city2[], int cost2) +{ + int s, t; + s = Search(root, city1); + t = Search(root, city2); + if (s == -1) + { + Insert(root, city1, IN); + s = IN++; + } + if (t == -1) + { + Insert(root, city2, IN); + t = IN++; + } + V[s][t].f = V[t][s].f = 1; + V[s][t].a = cost1; + V[s][t].b = cost2; + V[t][s].a = cost2; + V[t][s].b = cost1; +} +void Push(int n) +{ + Q[QH++] = n; + QH %= MAXN; +} +int Pop() +{ + int n = Q[QT++]; + QT %= MAXN; + return n; +} +int IsEmpty() +{ + return QH == QT; +} +int BFS(int s, int t) +{ + int i, j; + for (i = 0; i < IN; i++) + { + F[i] = 0; + P[i] = -1; + } + QH = QT = 0; + F[s] = 1; + Push(s); + while (!IsEmpty()) + { + j = Pop(); + for (i = 0; i < IN; i++) + { + if (!V[j][i].f || F[i]) + { + continue; + } + if (i == t) + { + P[i] = j; + return 1; + } + F[i] = 1; + P[i] = j; + Push(i); + } + } + return 0; +} +int T = 0; +void Find(int a, int b) +{ + if (a == b) + { + N[T++] = a; + return; + } + Find(a, P[b]); + N[T++] = b; +} +void Cal(char p1[], char p2[]) +{ + int s, t, x, i; + int n, d; + n = d = 1; + s = Search(root, p1); + t = Search(root, p2); + x = BFS(s, t); + if (x == 0) + { + printf("? %s = ? %s\n", p1, p2); + return; + } + T = 0; + Find(s, t); + for (i = 0; i < T - 1; i++) + { + s = N[i]; + t = N[i + 1]; + n *= V[s][t].a; + d *= V[s][t].b; + x = gcd(n, d); + n /= x; + d /= x; + } + printf("%d %s = %d %s\n", n, p1, d, p2); +} + +int main() +{ + IN = 0; + char text[10], pr1[22], pr2[22]; + int co1, co2; + root = NULL; + while (1) + { + char ch; + cin >> ch; + if (ch == '.') + { + break; + } + if (ch == '!') + { + cin >> co1 >> pr1 >> ch >> co2 >> pr2; + Set(pr1, co1, pr2, co2); + } + else + { + cin >> pr1 >> ch >> pr2; + Cal(pr1, pr2); + } + } + return 0; +} diff --git a/10114.cpp b/10114.cpp new file mode 100644 index 0000000..d9f4ab5 --- /dev/null +++ b/10114.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int main() +{ + int duration; + double down_payment, loan; + int deprecation_num; + double deprecation[101]; + int m; + double d; + while (cin >> duration >> down_payment >> loan >> deprecation_num) + { + if (duration < 0) + { + break; + } + while (deprecation_num--) + { + cin >> m >> d; + for (int i = m; i < 101; i++) + { + deprecation[i] = d; + } + } + int months = 0; + double loan_diff = loan / duration; + double car_value = (down_payment + loan) * (1 - deprecation[0]); + while (car_value < loan) + { + months++; + loan -= loan_diff; + car_value = car_value * (1 - deprecation[months]); + } + if (months == 1) + { + cout << "1 month" << endl; + } + else + { + cout << months << " months" << endl; + } + } + return 0; +} diff --git a/10115.cpp b/10115.cpp index e6f52c5..dce12a9 100644 --- a/10115.cpp +++ b/10115.cpp @@ -1,31 +1,70 @@ -#include -#include -#include -#include -#include -using namespace std; -int main(){ - for(;;){ - int i,n; - scanf("%d",&n); - if(n==0) - break; - char ch=getchar(); - string toReplace[n],replaceBy[n],str; - for(i=0;i + +using namespace std; + +char rule[15][2][400]; +char editText[500]; +int numOfRule; + +void input() +{ + cin.get(); + int loop; + for (loop = 0; loop < numOfRule; loop++) + { + cin.getline(rule[loop][0], 400); + cin.getline(rule[loop][1], 400); + } +} + +//be(ba b)oat be(hind the g)oat +void replace(int index, char *find, char *by) +{ + char temp[500]; + int i, j; + for (i = 0; i < index; i++) + { + temp[i] = editText[i]; + } + index += strlen(find); + for (j = 0; by[j]; j++) + { + temp[i++] = by[j]; + } + for (; editText[index]; index++) + { + temp[i++] = editText[index]; + } + temp[i] = '\0'; + strcpy(editText, temp); + // cout<> numOfRule && numOfRule) + { + input(); + cin.getline(editText, 500); + solve(); + cout << editText << endl; + } + return 0; +} diff --git a/10116.cpp b/10116.cpp new file mode 100644 index 0000000..5e64f6b --- /dev/null +++ b/10116.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +struct position +{ + int r, c; +}; + +int main() +{ + int row, col, enter; + char field[10][10]; + while (cin >> row >> col >> enter) + { + if (!row && !col && !enter) + { + break; + } + int visited[10][10]; + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + visited[i][j] = -1; + } + } + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + cin >> field[i][j]; + } + } + position current; + current.r = 0; + current.c = enter - 1; + visited[current.r][current.c] = 0; + int result = 0; + bool loop = false; + while (true) + { + result++; + if (field[current.r][current.c] == 'N') + { + if (current.r > 0) + { + current.r--; + } + else + { + break; + } + } + else if (field[current.r][current.c] == 'S') + { + if (current.r + 1 < row) + { + current.r++; + } + else + { + break; + } + } + else if (field[current.r][current.c] == 'E') + { + if (current.c + 1 < col) + { + current.c++; + } + else + { + break; + } + } + else if (field[current.r][current.c] = 'W') + { + if (current.c > 0) + { + current.c--; + } + else + { + break; + } + } + if (visited[current.r][current.c] >= 0) + { + loop = true; + break; + } + else + { + visited[current.r][current.c] = result; + } + } + if (loop) + { + cout << visited[current.r][current.c] << " step(s) before a loop of " << result - visited[current.r][current.c] << " step(s)" << endl; + } + else + { + cout << result << " step(s) to exit" << endl; + } + } + return 0; +} diff --git a/10117.cpp b/10117.cpp new file mode 100644 index 0000000..e226d7e --- /dev/null +++ b/10117.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define eps 1e-10 +#define MAXN 131072 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } +}; +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +struct Seg +{ + Pt s, e; + double angle; + int label; + Seg(Pt a = Pt(), Pt b = Pt(), int l = 0) : s(a), e(b), label(l) + { + angle = atan2(e.y - s.y, e.x - s.x); + } + bool operator<(const Seg &other) const + { + if (fabs(angle - other.angle) > eps) + return angle > other.angle; + if (cross(other.s, other.e, s) > -eps) + return true; + return false; + } +}; +Pt getIntersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +Seg deq[MAXN]; +vector halfPlaneIntersect(vector segs) +{ + sort(segs.begin(), segs.end()); + int n = segs.size(), m = 1; + int front = 0, rear = -1; + for (int i = 1; i < n; i++) + { + if (fabs(segs[i].angle - segs[m - 1].angle) > eps) + segs[m++] = segs[i]; + } + n = m; + deq[++rear] = segs[0]; + deq[++rear] = segs[1]; + for (int i = 2; i < n; i++) + { + while (front < rear && cross(segs[i].s, segs[i].e, getIntersect(deq[rear], deq[rear - 1])) < eps) + rear--; + while (front < rear && cross(segs[i].s, segs[i].e, getIntersect(deq[front], deq[front + 1])) < eps) + front++; + deq[++rear] = segs[i]; + } + while (front < rear && cross(deq[front].s, deq[front].e, getIntersect(deq[rear], deq[rear - 1])) < eps) + rear--; + while (front < rear && cross(deq[rear].s, deq[rear].e, getIntersect(deq[front], deq[front + 1])) < eps) + front++; + vector ret; + for (int i = front; i < rear; i++) + { + Pt p = getIntersect(deq[i], deq[i + 1]); + ret.push_back(p); + } + if (rear > front + 1) + { + Pt p = getIntersect(deq[front], deq[rear]); + ret.push_back(p); + } + return ret; +} +double calcArea(vector p) +{ + double ret = 0; + int n = p.size(); + if (n < 3) + return 0.0; + for (int i = 0, j = n - 1; i < n; j = i++) + ret += p[i].x * p[j].y - p[i].y * p[j].x; + return fabs(ret) / 2; +} +Pt D[32]; +int main() +{ + int n, m; + double h; + while (scanf("%d %d %lf", &n, &m, &h) == 3 && n) + { + vector O; + Seg Oe[32]; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &D[i].x, &D[i].y); + O.push_back(D[i]); + } + D[n] = D[0], O.push_back(D[0]); + for (int i = 0; i < n; i++) + { + Pt a = D[i], b = D[i + 1]; // \vec{ab} + Pt nab(b.y - a.y, a.x - b.x);// normal vector + double v = hypot(nab.x, nab.y); + nab.x = nab.x / v * h, nab.y = nab.y / v * h; + a = a - nab, b = b - nab; + Oe[i] = Seg(a, b); + } + int A[32] = {}; + for (int i = 0; i < m; i++) + A[i] = 1; + sort(A, A + n); + double area = calcArea(O), ret = 0; + do + { + vector segs; + for (int i = 0; i < n; i++) + if (A[i]) + segs.push_back(Oe[i]); + else + segs.push_back(Seg(O[i], O[i + 1])); + vector convex = halfPlaneIntersect(segs); + double d = calcArea(convex); + ret = max(ret, area - d); + } while (next_permutation(A, A + n)); + printf("%.2lf\n", ret); + } + return 0; +} diff --git a/10118.cpp b/10118.cpp new file mode 100644 index 0000000..55ff7f5 --- /dev/null +++ b/10118.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define MAXD 50 + +int N, f[MAXD][MAXD][MAXD][MAXD], x[5][MAXD]; + +int init() +{ + int i, j; + scanf("%d", &N); + if (!N) + { + return 0; + } + for (i = 0; i < N; i++) + for (j = 0; j < 4; j++) + { + scanf("%d", &x[j][i]); + } + return 1; +} +int dp(int y[], int s, int t[]) +{ + int i, j, k, max = 0; + if (f[t[0]][t[1]][t[2]][t[3]] != -1) + { + return f[t[0]][t[1]][t[2]][t[3]]; + } + if (s == 5) + { + return f[t[0]][t[1]][t[2]][t[3]] = 0; + } + for (i = 0; i < 4; i++) + if (t[i] != N) + { + int r[25]; + memcpy(r, y, sizeof(r)); + int top[5]; + memcpy(top, t, sizeof(top)); + top[i]++; + if (r[x[i][t[i]]]) + { + r[x[i][t[i]]] = 0; + k = dp(r, s - 1, top); + if (k + 1 > max) + { + max = k + 1; + } + } + else + { + r[x[i][t[i]]] = 1; + k = dp(r, s + 1, top); + if (k > max) + { + max = k; + } + } + } + return f[t[0]][t[1]][t[2]][t[3]] = max; +} +void solve() +{ + int res, r[25] = {0}, top[5] = {0}; + memset(f, -1, sizeof(f)); + res = dp(r, 0, top); + printf("%d\n", res); +} + +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/10119.cpp b/10119.cpp new file mode 100644 index 0000000..c4f3439 --- /dev/null +++ b/10119.cpp @@ -0,0 +1,268 @@ +#include + +using namespace std; + +#define MAXN 21 + +const int dx[4] = {-1, 1, 0, 0}; +const int dy[4] = {0, 0, -1, 1}; + +char map_[MAXN][MAXN], kind[MAXN][MAXN]; +int n, k, m, c, depth, candidates[MAXN * MAXN], qx[MAXN * MAXN], qy[MAXN * MAXN]; + +int check(); + +void preprocess() +{ + int visited[MAXN][MAXN]; + int qx[MAXN * MAXN], qy[MAXN * MAXN]; + int i, j, x, y, px, py, get, put; + // flood-fill + get = 0; + put = 2; + qx[0] = 0; + qy[0] = 0; + qx[1] = n - 1; + qy[1] = n - 1; + memset(visited, 0, sizeof(visited)); + visited[0][0] = 1; + visited[n - 1][n - 1] = 1; + while (get != put) + { + x = qx[get]; + y = qy[get++]; + for (i = 0; i < 4; i++) + { + px = x + dx[i]; + py = y + dy[i]; + if (px >= 0 && px <= n - 1 && py >= 0 && py <= n - 1) + if (!visited[px][py] && map_[px][py] == '.') + { + qx[put] = px; + qy[put++] = py; + visited[px][py] = 1; + } + } + } + c = 0; + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + if (i + j >= m && 2 * n - 2 - i - j >= m && visited[i][j]) + { + candidates[++c] = i * n + j; + } +} + +int cantforall(int s) +{ + int i, ret; + for (i = s; i <= c; i++) + { + map_[candidates[i] / n][candidates[i] % n] = '*'; + } + ret = !check(); + for (i = s; i <= c; i++) + { + map_[candidates[i] / n][candidates[i] % n] = '.'; + } + return ret; +} + +int check() +{ + int i, j, x, y, px, py, get, put; + memcpy(kind, map_, sizeof(kind)); + /* flood-fill from liz's house */ + get = 0; + put = 1; + qx[0] = 0; + qy[0] = 0; + kind[0][0] = '1'; + while (get != put) + { + x = qx[get]; + y = qy[get++]; + for (i = 0; i < 4; i++) + { + px = x + dx[i]; + py = y + dy[i]; + if (px >= 0 && px <= n - 1 && py >= 0 && py <= n - 1) + if (kind[px][py] == '.') + { + if (px == n - 1 && py == n - 1) + { + return 0; /* can go directly there! */ + } + qx[put] = px; + qy[put++] = py; + kind[px][py] = '1'; + } + } + } + /* flood-fill from lily's house with check */ + get = 0; + put = 1; + qx[0] = n - 1; + qy[0] = n - 1; + kind[n - 1][n - 1] = '2'; + while (get != put) + { + x = qx[get]; + y = qy[get++]; + for (i = 0; i < 4; i++) + { + px = x + dx[i]; + py = y + dy[i]; + if (px >= 0 && px <= n - 1 && py >= 0 && py <= n - 1) + if (kind[px][py] == '.') + { + qx[put] = px; + qy[put++] = py; + kind[px][py] = '2'; + /* check: for speed reason, we do not use constants here */ + /* up */ + if (px > 0) + { + if (kind[px - 1][py] == 'O') + for (j = 2; j <= k; j++) + { + if (px - j < 0) + { + break; + } + if (kind[px - j][py] == '1') + { + return 0; + } + if (kind[px - j][py] == '*') + { + break; + } + } + } + /* down */ + if (px < n - 1) + { + if (kind[px + 1][py] == 'O') + for (j = 2; j <= k; j++) + { + if (px + j > n - 1) + { + break; + } + if (kind[px + j][py] == '1') + { + return 0; + } + if (kind[px + j][py] == '*') + { + break; + } + } + } + /* left */ + if (py > 0) + { + if (kind[px][py - 1] == 'O') + for (j = 2; j <= k; j++) + { + if (py - j < 0) + { + break; + } + if (kind[px][py - j] == '1') + { + return 0; + } + if (kind[px][py - j] == '*') + { + break; + } + } + } + /* right */ + if (py < n - 1) + { + if (kind[px][py + 1] == 'O') + for (j = 1; j <= k; j++) + { + if (py + j > n - 1) + { + break; + } + if (kind[px][py + j] == '1') + { + return 0; + } + if (kind[px][py + j] == '*') + { + break; + } + } + } + } /* if kind[px][py]=='.' */ + } + } + return 1; +} + +/* now depth, starting candidate number */ + +int search(int d, int s) +{ + int i; + if (d == depth + 1) + { + if (check()) + { + return 1; + } + return 0; + } + else + for (i = c + d - depth; i >= s; i--) + { + map_[candidates[i] / n][candidates[i] % n] = '*'; + if (!cantforall(i + 1)) /* a big prune */ + if (search(d + 1, i + 1)) + { + return 1; + } + map_[candidates[i] / n][candidates[i] % n] = '.'; + } + return 0; +} + +int main() +{ + int i; + char buf[100]; + while (1) + { + scanf("%d%d%d", &n, &k, &m); + if (n == 0 && k == 0 && m == 0) + { + break; + } + gets(buf); + for (i = 0; i < n; i++) + { + gets(map_[i]); + } + preprocess(); + if (cantforall(1)) + { + printf("-1\n"); + } + else + for (depth = 0; depth <= n * n; depth++) + { + if (search(1, 1)) + { + printf("%d\n", depth); + break; + } + } + } + return 0; +} diff --git a/10120.cpp b/10120.cpp new file mode 100644 index 0000000..ad5c186 --- /dev/null +++ b/10120.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define MAXN 1000002 + +int Jump[2][MAXN], N, T; + +int Dynamic() +{ + int i, nth = 2, a1 = 1, a2 = 0, j; + int inda = 1, indb, forward, backward; + Jump[a1][0] = 1; + j = nth * 2 - 1; + while (inda) + { + indb = 0; + for (i = 0; i < inda; i++) + { + forward = Jump[a1][i] + j; + if (forward <= N) + { + Jump[a2][indb++] = forward; + } + backward = Jump[a1][i] - j; + if (backward > 0) + { + Jump[a2][indb++] = backward; + } + if (forward == T || backward == T) + { + return 1; + } + } + inda = indb; + a1++; + a2++; + nth++; + a1 %= 2; + a2 %= 2; + j = nth * 2 - 1; + } + return 0; +} + +int main() +{ + int g; + while (scanf("%d%d", &N, &T) && N && T) + { + if (N > 48) + { + printf("Let me try!\n"); + continue; + } + g = int(sqrt(T)); + if (g * g == T) + { + printf("Let me try!\n"); + continue; + } + if (Dynamic()) + { + printf("Let me try!\n"); + } + else + { + printf("Don't make fun of me!\n"); + } + } + return 0; +} diff --git a/10122.cpp b/10122.cpp new file mode 100644 index 0000000..06e11a2 --- /dev/null +++ b/10122.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +const int maxn = 110; +int path[maxn][maxn], visited[maxn], link[maxn]; +int n, m; +double a[maxn][maxn]; + +struct point +{ + int x, y; + point(int x0 = 0, int y0 = 0) + { + x = x0; + y = y0; + } +} p[maxn]; + +struct line +{ + point p1, p2; + line(point p10, point p20) + { + p1 = p10; + p2 = p20; + } +}; + +void initial() +{ + memset(link, -1, sizeof(link)); + memset(path, 0, sizeof(path)); +} + +double getvalue(int l, int r, point p0, int x, int vc, int vw) +{ + double ans = 1e9; + for (int t = l; t <= r; t++) + { + double tmp = abs(x - t) * 1.0 / (vw * 1.0) + sqrt((t - p0.x) * (t - p0.x) + p0.y * p0.y) / (vc * 1.0); + if (tmp < ans) + ans = tmp; + } + return ans; +} + +void input() +{ + m = n; + for (int i = 0; i <= n + 1; i++) + { + scanf("%d%d", &p[i].x, &p[i].y); + } + int vc, vw, x, l, r; + for (int i = 1; i <= m; i++) + { + scanf("%d%d%d", &vc, &vw, &x); + for (int j = 1; j <= n; j++) + { + if (x < p[j].x) + { + l = x; + for (int t = 0; t < j; t++) + { + if (p[t].y < p[j].y - 1e-6) + { + double lx = 1.0 * (p[t].x * p[j].y - p[j].x * p[t].y) / (p[j].y - p[t].y); + if (int(lx - 1e-5) + 1 > l) + l = int(lx - 1e-5) + 1; + } + } + a[j][i] = getvalue(l, p[j].x, p[j], x, vc, vw); + } + else + { + r = x; + for (int t = j + 1; t <= n + 1; t++) + { + if (p[t].y < p[j].y - 1e-6) + { + double rx = 1.0 * (p[t].x * p[j].y - p[j].x * p[t].y) / (p[j].y - p[t].y); + if (int(rx + 1e-5) < r) + r = int(rx + 1e-5); + } + } + a[j][i] = getvalue(p[j].x, r, p[j], x, vc, vw); + } + } + } +} + +int can(int x) +{ + for (int i = 1; i <= m; i++) + { + if (visited[i] == -1 && path[x][i] > 0) + { + visited[i] = 1; + if (link[i] == -1 || can(link[i])) + { + link[i] = x; + return 1; + } + } + } + return 0; +} + +int getBip(double c) +{ + int ans = 0; + initial(); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (a[i][j] <= c) + path[i][j] = 1; + } + } + for (int i = 1; i <= n; i++) + { + memset(visited, -1, sizeof(visited)); + if (can(i)) + ans++; + } + return ans; +} + +void solve() +{ + double l = 0, r = 4000; + while (r - l > 1e-7) + { + double mid = (l + r) / 2.0; + if (getBip(mid) >= n) + r = mid; + else + l = mid; + } + printf("%.2lf\n", (l + r) / 2.0); +} + +int main() +{ + while (scanf("%d", &n) != EOF && n != 0) + { + input(); + solve(); + } + return 0; +} diff --git a/10123.cpp b/10123.cpp new file mode 100644 index 0000000..e9b0f4c --- /dev/null +++ b/10123.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define MAXD 30 + +int L, N, W, s[MAXD], r1[MAXD], r2[MAXD], N1, N2, x[MAXD], w[MAXD], vis[MAXD]; + +int cmp(const void *_p, const void *_q) +{ + int *p = (int *)_p; + int *q = (int *)_q; + int x1, x2; + if (x[*p] < 0) + x1 = (-3 - x[*p]) * w[*p]; + else + x1 = (x[*p] - 3) * w[*p]; + if (x[*q] < 0) + x2 = (-3 - x[*q]) * w[*q]; + else + x2 = (x[*q] - 3) * w[*q]; + return x1 - x2; +} + +void init() +{ + int i; + for (i = 0; i < N; i++) + { + scanf("%d%d", &x[i], &w[i]); + x[i] *= 2; + } +} + +int dfs(int left, int right, int num) +{ + int i, j, k, t, flag, mleft, mright, tleft = 1, tright = 1; + if (num == N) + return 2; + for (i = 0; i < N1; i++) + { + k = r1[i]; + if (!vis[k]) + { + vis[k] = 1; + mleft = left + (x[k] + 3) * w[k], mright = right + (3 - x[k]) * w[k]; + s[num] = k; + if (mleft >= 0) + tleft = 0; + if (mleft >= 0 && mright >= 0) + { + flag = dfs(mleft, mright, num + 1); + if (flag == 2) + return 2; + if (flag == -1) + break; + tleft = 0; + } + vis[k] = 0; + } + } + for (i = 0; i < N2; i++) + { + k = r2[i]; + if (!vis[k]) + { + vis[k] = 1; + mleft = left + (x[k] + 3) * w[k], mright = right + (3 - x[k]) * w[k]; + s[num] = k; + if (mright >= 0) + tright = 0; + if (mleft >= 0 && mright >= 0) + { + flag = dfs(mleft, mright, num + 1); + if (flag == 2) + return 2; + if (flag == 1) + break; + tright = 0; + } + vis[k] = 0; + } + } + return tright - tleft; +} +void solve() +{ + int i, j, k, left, right, num; + memset(vis, 0, sizeof(vis)); + num = 0; + left = right = 3 * W; + for (i = 0; i < N; i++) + if (x[i] >= -3 && x[i] <= 3) + { + s[num++] = i; + vis[i] = 1; + left = left + (x[i] + 3) * w[i], right = right + (3 - x[i]) * w[i]; + } + N1 = N2 = 0; + for (i = 0; i < N; i++) + if (!vis[i]) + { + if (x[i] < 0) + r1[N1++] = i; + else + r2[N2++] = i; + } + qsort(r1, N1, sizeof(r1[0]), cmp); + qsort(r2, N2, sizeof(r2[0]), cmp); + if (dfs(left, right, num) != 2) + printf("Impossible\n"); + else + { + for (i = N - 1; i >= 0; i--) + printf("%d %d\n", x[s[i]] / 2, w[s[i]]); + } +} + +int main() +{ + int t = 0; + for (;;) + { + scanf("%d%d%d", &L, &W, &N); + if (!L) + { + break; + } + init(); + printf("Case %d:\n", ++t); + solve(); + } + return 0; +} diff --git a/10124.cpp b/10124.cpp new file mode 100644 index 0000000..e05bb85 --- /dev/null +++ b/10124.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int main() +{ + double D, M, A, J, d; + while (scanf("%lf%lf%lf%lf", &D, &M, &A, &J) == 4) + { + d = D / 2; + double t1, t2, v3, d1, d2, d3; + double ret = 1e+60; + double l = 0, r = M, m; + m = M; + t1 = A / J; + if (2 * t1 * A / 2 > m) + { + t1 = sqrt(m / J); + t2 = t1; + } + else + { + t2 = (m - A * t1) / A + t1; + } + v3 = J * t1 * t1 / 2 + J * t1 * (t2 - t1) + J * t1 * t1 / 2; + d1 = J * t1 * t1 * t1 / 6; + d2 = (J * t1 * t1 + J * t1 * (t2 - t1)) * (t2 - t1) / 2; + d3 = (J * t1 * t1 / 2 + J * t1 * (t2 - t1)) * t1 + J * t1 * t1 * t1 / 2 - J * t1 * t1 * t1 / 6; + if (d1 + d2 + d3 > d) + { + t1 = pow(d / J, 1 / 3.0); + t2 = t1; + v3 = J * t1 * t1 / 2 + J * t1 * (t2 - t1) + J * t1 * t1 / 2; + d1 = J * t1 * t1 * t1 / 6; + d2 = (J * t1 * t1 + J * t1 * (t2 - t1)) * (t2 - t1) / 2; + d3 = (J * t1 * t1 / 2 + J * t1 * (t2 - t1)) * t1 + J * t1 * t1 * t1 / 2 - J * t1 * t1 * t1 / 6; + if (t1 * J > A)//d1+d2+d3 = d => at2^2 + bt2 + c = 0 + { + t1 = A / J; + double a, b, c; + a = J * t1 / 2, b = J * t1 * t1 / 2, c = -d; + t2 = ((-b) + sqrt(b * b - 4 * a * c)) / (2 * a); + v3 = J * t1 * t1 / 2 + J * t1 * (t2 - t1) + J * t1 * t1 / 2; + d1 = J * t1 * t1 * t1 / 6; + d2 = (J * t1 * t1 + J * t1 * (t2 - t1)) * (t2 - t1) / 2; + d3 = (J * t1 * t1 / 2 + J * t1 * (t2 - t1)) * t1 + J * t1 * t1 * t1 / 2 - J * t1 * t1 * t1 / 6; + ret = min(ret, t1 + t2 + (d - d1 - d2 - d3) / v3); + } + else + { + ret = min(ret, t1 + t2 + (d - d1 - d2 - d3) / v3); + } + } + else + { + ret = min(ret, t1 + t2 + (d - d1 - d2 - d3) / v3); + } + printf("%.1lf\n", ret * 2); + } + return 0; +} diff --git a/10125.cpp b/10125.cpp new file mode 100644 index 0000000..196a3d3 --- /dev/null +++ b/10125.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int find(vector &set) +{ + for (int d = set.size() - 1; d >= 0; --d) // find the largest d + { + for (int a = 0; a < set.size(); ++a) + { + for (int b = a + 1; b < set.size(); ++b) + { + for (int c = b + 1; c < set.size(); ++c) + { + if ((set[d] == set[a] + set[b] + set[c]) && + a != d && b != d && c != d) + { + return set[d]; + } + } + } + } + } + return INT_MAX; +} + +int main() +{ + int numElement; + while (cin >> numElement) + { + if (numElement == 0) + { + break; + } + /* input */ + vector set(numElement); + for (int i = 0; i < numElement; ++i) + { + cin >> set[i]; + } + sort(set.begin(), set.end()); + /* find d = a + b + c */ + int d = find(set); + if (d == INT_MAX) + { + cout << "no solution\n"; + } + else + { + cout << d << "\n"; + } + } + return 0; +} diff --git a/10126.cpp b/10126.cpp new file mode 100644 index 0000000..b76e1ea --- /dev/null +++ b/10126.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 20002 + +char word[MAXN][40], str[100000]; +int tw, fre; + +int com(const void *a, const void *b) +{ + return (strcmp((char *)a, (char *)b)); +} + +void Sep() +{ + int i, j, k; + char temp[25]; + for (i = 0; str[i];) + { + if (!isalpha(str[i])) + { + i++; + continue; + } + k = 0; + for (j = i; str[j] && isalpha(str[j]); j++) + { + temp[k++] = tolower(str[j]); + } + i = j; + temp[k] = NULL; + strcpy(word[tw++], temp); + } +} + +void Cal() +{ + int i, j, k, f = 1; + qsort((void *)word, tw, sizeof(word[0]), com); + for (i = 0; i < tw;) + { + k = 1; + for (j = i + 1; j < tw && !strcmp(word[i], word[j]); j++) + { + k++; + } + i = j; + if (k == fre) + { + f = 0; + printf("%s\n", word[i - 1]); + } + } + if (f) + { + printf("There is no such word.\n"); + } +} + +int main() +{ + int k = 0, m = 0; + while (scanf("%s", str) != EOF) + { + fre = atoi(str); + tw = 0; + while (1) + { + scanf("%s", str); + if (!strcmp(str, "EndOfText")) + break; + Sep(); + m++; + } + if (k++) + printf("\n"); + Cal(); + } + return 0; +} diff --git a/10127.cpp b/10127.cpp index 4f9482e..6381f8d 100644 --- a/10127.cpp +++ b/10127.cpp @@ -1,16 +1,19 @@ -#include -using namespace std; - -int main(){ - int n=0; - while(cin>>n){ - int m=1; - int c=1; - while(m%n){ - c++; - m=(m*10+1)%n; - } - cout< + +using namespace std; + +int main() +{ + int digit, n, i; + while (scanf("%d", &n) == 1) + { + digit = 1; + for (i = 1; i;) + { + i = (i * 10 + 1) % n; + digit++; + } + printf("%d\n", digit); + } + return 0; +} diff --git a/10128.cpp b/10128.cpp new file mode 100644 index 0000000..1db370f --- /dev/null +++ b/10128.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define MAX 13 + +int a[MAX + 1][MAX + 1][MAX + 1]; + +void init() +{ + a[1][1][1] = 1; + for (int n = 2; n <= MAX; n++) + { + for (int l = 1; l <= n; l++) + { + int max_r = n - l + 1; + for (int r = 1; r <= max_r; r++) + { + a[n][l][r] = a[n - 1][l - 1][r] + a[n - 1][l][r - 1] + + (n - 2) * a[n - 1][l][r]; + } + } + } +} + +int main() +{ + init(); + int t; + scanf("%d", &t); + for (int i = 0; i < t; i++) + { + int n, l, r; + scanf("%d %d %d", &n, &l, &r); + int x = (0 < l && l <= MAX && 0 < r && r <= MAX) ? a[n][l][r] : 0; + printf("%d\n", x); + } + return 0; +} diff --git a/10129.cpp b/10129.cpp new file mode 100644 index 0000000..0cb8479 --- /dev/null +++ b/10129.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define Maxn 100000 + +int T, N, cou[130], in[130], out[130]; +char Graph[130][Maxn]; +bool yes, mark[200]; + +int main() +{ + cin >> T; + char c, rec, sc; + while (T--) + { + yes = false; + memset(in, 0, sizeof(in)); + memset(cou, 0, sizeof(cou)); + memset(mark, false, sizeof(mark)); + memset(out, 0, sizeof(out)); + cin >> N; + getchar(); + int tc = 0; + for (int i = 0; i < N; i++) + { + while (1) + { + scanf("%c", &c); + if (tc == 0) + { + sc = c, tc = 1; + } + if (c == '\n') + { + Graph[sc][cou[sc]++] = rec; + Graph[rec][cou[rec]++] = sc; + if (!mark[rec]) + { + mark[rec] = true; + } + if (!mark[sc]) + { + mark[sc] = true; + } + in[rec]++; + out[sc]++; + tc = 0; + break; + } + rec = c; + } + } + queue q; + for (char i = 'a'; i <= 'z'; i++) + { + if (mark[i]) + { + mark[i] = false; + q.push(i); + break; + } + } + while (!q.empty()) + { + char tch = q.front(); + q.pop(); + for (int j = 0; j < cou[tch]; j++) + { + if (mark[Graph[tch][j]] == true) + { + mark[Graph[tch][j]] = false; + q.push(Graph[tch][j]); + } + } + } + int ans = 0; + for (int i = 'a'; i <= 'z'; i++) + { + if (mark[i] == true) + { + ans = 2; + break; + } + } + int oyes = 0, iyes = 0; + for (char i = 'a'; ans != 2 && i <= 'z'; i++) + { + int temp = in[i] - out[i]; + if (temp >= 2 || temp <= -2) + { + ans = 2; + break; + } + if (temp == 1) + { + iyes++; + } + if (temp == -1) + { + oyes++; + } + } + if (ans == 0 && ((iyes == 1 && oyes == 1) || (iyes == 0 && oyes == 0))) + { + ans = 1; + } + cout << (ans == 1 ? "Ordering is possible." : "The door cannot be opened.") << endl; + } + return 0; +} diff --git a/1013.cpp b/1013.cpp new file mode 100644 index 0000000..ec11001 --- /dev/null +++ b/1013.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const int MAXN = 64; +double X[MAXN], Y[MAXN], M[MAXN]; +struct Edge +{ + int x, y; + double v; + Edge(int a = 0, int b = 0, double c = 0) : x(a), y(b), v(c) {} + bool operator<(const Edge &x) const + { + return v < x.v; + } +}; +vector E; +int parent[MAXN], weight[MAXN]; +double weight2[MAXN]; +int findp(int x) +{ + return parent[x] == x ? x : parent[x] = findp(parent[x]); +} +int joint(int x, int y) +{ + x = findp(x), y = findp(y); + if (x == y) + return 0; + if (weight[x] > weight[y]) + { + parent[y] = x, weight[x] += weight[y]; + weight2[x] += weight2[y]; + } + else + { + parent[x] = y, weight[y] += weight[x]; + weight2[y] += weight2[x]; + } + return 1; +} +double solve(int n) +{ + sort(E.begin(), E.end()); + double div = 0, sum = 0; + int u, v; + for (int i = 0; i < n; i++) + parent[i] = i, weight[i] = 1, weight2[i] = M[i], div += M[i]; + + for (int i = 0; i < E.size(); i++) + { + u = E[i].x, v = E[i].y; + if (findp(u) != findp(v)) + { + if (findp(u) == findp(0)) + sum += weight2[findp(v)] * E[i].v; + if (findp(v) == findp(0)) + sum += weight2[findp(u)] * E[i].v; + joint(u, v); + } + } + return sum / div; +} + +int main() +{ + int n, cases = 0; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf", &X[i], &Y[i], &M[i]); + } + E.clear(); + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + double v = hypot(X[i] - X[j], Y[i] - Y[j]); + E.push_back(Edge(i, j, v)); + } + } + double ret = solve(n); + printf("Island Group: %d Average %.2lf\n", ++cases, ret); + puts(""); + } + return 0; +} diff --git a/10130.cpp b/10130.cpp index f376d72..5ef3d4c 100644 --- a/10130.cpp +++ b/10130.cpp @@ -1,36 +1,68 @@ -#include -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int N, i, w; - cin>>N; - int price[N+1], weight[N+1]; - for(i=1; i<=N; i++) - cin>>price[i]>>weight[i]; - int nMem, totVal=0; - cin>>nMem; - while(nMem--){ - int MW; - cin>>MW; - int mat[N+1][MW+1]; - for(i=0; i<=N; i++) - mat[i][0] = 0; - for(w=0; w<=MW; w++) - mat[0][w] = 0; - for(i=1; i<=N; i++){ - for(w=1; w<=MW; w++){ - if(weight[i] > w) - mat[i][w] = mat[i-1][w]; - else - mat[i][w] = max(mat[i-1][w], mat[i-1][w-weight[i]] + price[i]); - } - } - totVal += mat[N][MW]; - } - cout< + +using namespace std; + +int numOfCase, numOfObj, numOfPeople; +int c[1001][31]; + +struct Object +{ + int price, weight; +} obj[1001]; + +void InputObj() +{ + int i; + for (i = 1; i <= numOfObj; i++) + { + cin >> obj[i].price >> obj[i].weight; + } +} + +void KnapSack() +{ + memset(c, 0, sizeof(c)); + int i, w; + for (i = 0; i <= 1000; i++) + { + c[i][0] = 0; + } + for (w = 0; w <= 30; w++) + { + c[0][w] = 0; + } + for (i = 1; i <= numOfObj; i++) + for (w = 1; w <= 30; w++) + { + if (obj[i].weight > w) + { + c[i][w] = c[i - 1][w]; + } + else + { + int temp = c[i - 1][w - obj[i].weight] + obj[i].price; + c[i][w] = c[i - 1][w] > temp ? c[i - 1][w] : temp; + } + } +} + +int main() +{ + cin >> numOfCase; + while (numOfCase--) + { + cin >> numOfObj; + InputObj(); + cin >> numOfPeople; + int sum = 0; + KnapSack(); + while (numOfPeople--) + { + int mw; + cin >> mw; + sum += c[numOfObj][mw]; + } + cout << sum << endl; + } + return 0; +} diff --git a/10131.cpp b/10131.cpp new file mode 100644 index 0000000..831c26b --- /dev/null +++ b/10131.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +const int MAXSIZE = 1010; + +struct Element +{ + int key, pre, num, iq; + bool operator<(Element const &another) + { + return this->key < another.key; + } + + bool operator>(Element const &another) + { + return this->key > another.key; + } + + bool operator==(Element const &another) + { + return this->key == another.key; + } +}; + +Element arr[MAXSIZE]; // arr of elements +int lis[MAXSIZE]; // arr of indexes in a +int len; // arr len +int last; // lis len + +// bin srch +int bin_srch(int start, int end, Element e) +{ + int low = start, mid, high = end; + while (low <= high) + { + mid = (low + high) >> 1; + if (arr[lis[mid]] == e) + return mid; + else if (arr[lis[mid]] < e) + low = mid + 1; + else + high = mid - 1; + } + return mid; +} + +void solve() +{ + arr[0].pre = -1; + lis[last++] = 0; + int i; + for (i = 1; i < len; i++) + { + if (arr[i] > arr[lis[last - 1]]) + { + arr[i].pre = lis[last - 1]; + lis[last++] = i; + } + else + { + int x = bin_srch(0, last - 1, arr[i]); + if (arr[lis[x]] == arr[i]) + arr[i].pre = arr[lis[x]].pre; + else if (arr[lis[x]] < arr[i]) + { + arr[i].pre = lis[x]; + lis[x + 1] = i; + } + else + { + if (x == 0) + arr[i].pre = -1; + else + arr[i].pre = lis[x - 1]; + lis[x] = i; + } + } + } +} + +void output() +{ + int result[MAXSIZE]; + cout << last << endl; + int x = lis[last - 1]; + for (int i = 0; i < last; i++) + { + result[i] = arr[x].num; + x = arr[x].pre; + } + for (int i = last - 1; i >= 0; i--) + cout << result[i] << endl; +} + +int cmp(const void *a, const void *b) +{ + Element *aa = (Element *)a; + Element *bb = (Element *)b; + if (aa->iq != bb->iq) + return bb->iq - aa->iq; + else + return bb->key - aa->key; +} + +int main() +{ + len = last = 0; + while (cin >> arr[len].key >> arr[len].iq) + { + arr[len].num = len + 1; + len++; + } + qsort(arr, len, sizeof(Element), cmp); + solve(); + output(); + return 0; +} diff --git a/10132.cpp b/10132.cpp new file mode 100644 index 0000000..e00d6ed --- /dev/null +++ b/10132.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +vector files, sshort, slong; +string temp; + +bool check(); + +int main() +{ + bool first = true; + int N; + scanf("%d", &N); + scanf("\n"); + for (int i = 0; i < N; i++) + { + files.clear(); + sshort.clear(); + slong.clear(); + while (true) + { + if (!getline(cin, temp, '\n')) + { + break; + } + if (temp == "") + { + break; + } + files.push_back(temp); + } + int min = 1000, max = 0; + for (int j = 0; j < files.size(); j++) + { + if (files[j].size() < min) + { + min = files[j].size(); + } + if (files[j].size() > max) + { + max = files[j].size(); + } + } + for (int j = 0; j < files.size(); j++) + { + if (files[j].size() == min) + { + bool exist = false; + for (int k = 0; k < sshort.size(); k++) + if (files[j] == sshort[k]) + { + exist = true; + } + if (!exist) + { + sshort.push_back(files[j]); + } + } + if (files[j].size() == max) + { + bool exist = false; + for (int k = 0; k < slong.size(); k++) + if (files[j] == slong[k]) + { + exist = true; + } + if (!exist) + { + slong.push_back(files[j]); + } + } + } + bool found = false; + for (int j = 0; !found && j < sshort.size(); j++) + for (int k = 0; !found && k < slong.size(); k++) + { + temp = sshort[j] + slong[k]; + found = check(); + } + for (int j = 0; !found && j < sshort.size(); j++) + for (int k = 0; !found && k < slong.size(); k++) + { + temp = slong[k] + sshort[j]; + found = check(); + } + if (first) + { + first = false; + } + else + { + putchar(10); + } + cout << temp << endl; + } + return 0; +} + +bool check() +{ + int loc1, loc2; + int len = temp.size(); + for (int i = 0; i < files.size(); i++) + { + loc1 = temp.find(files[i]); + loc2 = temp.rfind(files[i]); + if (loc1 != 0 && loc2 != len - files[i].size()) + { + return false; + } + } + return true; +} diff --git a/10134.cpp b/10134.cpp new file mode 100644 index 0000000..baa896c --- /dev/null +++ b/10134.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int t; + char cmd[10]; + for (scanf("%d\n", &t); t--;) + { + int fish = 0, bait = 0, get = 0, time = 0; + while (gets(cmd) && *cmd) + { + if (!strcmp(cmd, "bait") && bait < 6) + { + ++bait; + } + else if (!strcmp(cmd, "fish") && bait > 1) + { + if (!get || !fish && !time) + { + ++get, bait -= 2, fish = 3, time = 7; + } + } + if (fish) + { + --fish; + } + if (time) + { + --time; + } + } + printf("%d%s\n", get, t ? "\n" : ""); + } + return 0; +} diff --git a/10135.cpp b/10135.cpp new file mode 100644 index 0000000..bea59ac --- /dev/null +++ b/10135.cpp @@ -0,0 +1,228 @@ +#include + +using namespace std; + +#define MAXPOLY 1010 +#define EPSILON (1E-9) + +struct point +{ + double x; + double y; +}; + +point origin = (point){0.0, 0.0}; + +struct polygon +{ + int vertexNumber; + point vertex[MAXPOLY]; +}; + +double calDistance(point first, point second) +{ + return sqrt(pow(first.x - second.x, 2) + pow(first.y - second.y, 2)); +} + +double crossProduct(point first, point second, point third) +{ + return (second.x - first.x) * (third.y - first.y) - + (second.y - first.y) * (third.x - first.x); +} + +bool leftLower(point first, point second) +{ + if (first.x == second.x) + { + return first.y < second.y; + } + else + { + return first.x < second.x; + } +} + +void convexHull(point vertex[], int vertexNumber, polygon &container) +{ + if (vertexNumber <= 3) + { + for (int i = 0; i < vertexNumber; i++) + { + container.vertex[i] = vertex[i]; + } + container.vertexNumber = vertexNumber; + return; + } + sort(vertex, vertex + vertexNumber, leftLower); + point upper[MAXPOLY], lower[MAXPOLY]; + int top; + upper[0] = vertex[0]; + upper[1] = vertex[1]; + top = 2; + for (int i = 2; i < vertexNumber; i++) + { + upper[top] = vertex[i]; + while (top >= 2 && crossProduct(upper[top - 2], upper[top - 1], upper[top]) >= 0) + { + upper[top - 1] = upper[top]; + top--; + } + top++; + } + container.vertexNumber = 0; + for (int i = 0; i < top; i++) + { + container.vertex[container.vertexNumber++] = upper[i]; + } + lower[0] = vertex[vertexNumber - 1]; + lower[1] = vertex[vertexNumber - 2]; + top = 2; + for (int i = vertexNumber - 3; i >= 0; i--) + { + lower[top] = vertex[i]; + while (top >= 2 && crossProduct(lower[top - 2], lower[top - 1], lower[top]) >= 0) + { + lower[top - 1] = lower[top]; + top--; + } + top++; + } + for (int i = 1; i < top - 1; i++) + { + container.vertex[container.vertexNumber++] = lower[i]; + } +} + +double convexHullLength(polygon &container) +{ + double length = 0.0; + for (int i = 0; i < container.vertexNumber; i++) + { + int j = (i + 1) % container.vertexNumber; + length += calDistance(container.vertex[i], container.vertex[j]); + } + return length; +} + +bool smallerAngle(point first, point second) +{ + if (first.y == 0 && second.y == 0 && first.x * second.x <= 0) + { + return first.x > second.x; + } + if (first.y == 0 && first.x >= 0 && second.y != 0) + { + return true; + } + if (second.y == 0 && second.x >= 0 && first.y != 0) + { + return false; + } + if (first.y * second.y < 0) + { + return first.y > second.y; + } + double cp = crossProduct(origin, first, second); + return cp > 0 || (cp == 0 && fabs(first.x) < fabs(second.x)); +} + +double minConvexHullLength(point in[], int &n) +{ + double length = 1e20; + sort(in, in + n, leftLower); + int hole = 0; + for (int i = 0; i < n; i++) + { + if (in[i].x == 0 && in[i].y == 0) + { + continue; + } + if (hole == 0) + { + in[hole++] = in[i]; + } + else if (in[hole - 1].x == in[i].x && in[hole - 1].y == in[i].y) + { + continue; + } + else + { + in[hole++] = in[i]; + } + } + n = hole; + sort(in, in + n, smallerAngle); + polygon out; + for (int i = 0; i < n; i++) + { + int top = 1, current = i + 1; + out.vertex[0] = origin; + out.vertex[1] = in[i]; + while (current < (i + n)) + { + if (top >= 1 && crossProduct(out.vertex[top - 1], out.vertex[top], in[current % n]) <= 0) + { + top--; + } + else + { + top++; + out.vertex[top] = in[current % n]; + current++; + } + } + top++; + out.vertex[top] = in[(i - 1 + n) % n]; + out.vertexNumber = top; + length = min(length, convexHullLength(out)); + } + return length; +} + +int main(int ac, char *av[]) +{ + point frosh[MAXPOLY]; + polygon silk; + int cases, vertexNumber, froshNumber; + bool printBlankLine = false; + cout.precision(2); + cout.setf(ios::fixed | ios::showpoint); + cin >> cases; + while (cases--) + { + cin >> froshNumber; + frosh[0] = origin; + for (int i = 1; i <= froshNumber; i++) + { + cin >> frosh[i].x >> frosh[i].y; + } + froshNumber++; + convexHull(frosh, froshNumber, silk); + double minLength = convexHullLength(silk) + 2.0; + bool onPolygon = false; + for (int i = 0; i < silk.vertexNumber; i++) + { + int j = (i + 1) % silk.vertexNumber; + if (fabs(crossProduct(origin, silk.vertex[i], + silk.vertex[j])) <= EPSILON) + { + onPolygon = true; + break; + } + } + if (!onPolygon) + { + minLength = minConvexHullLength(frosh, froshNumber) + 2.0; + } + if (printBlankLine) + { + cout << endl; + } + else + { + printBlankLine = true; + } + cout << minLength << endl; + } + return 0; +} diff --git a/10136.cpp b/10136.cpp new file mode 100644 index 0000000..073fbad --- /dev/null +++ b/10136.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAXN 200 + +struct point +{ + double x, y; +}; + +point chips[MAXN], center, median; +int totalChips; + +int chipsInCutter() +{ + int nCount = 0; + for (int i = 0; i < totalChips; i++) + { + double distance = sqrt(pow(chips[i].x - center.x, 2) + + pow(chips[i].y - center.y, 2)); + if (distance <= 2.50) + { + nCount++; + } + } + return nCount; +} + +int main(int ac, char *av[]) +{ + istringstream iss; + string line; + int cases; + bool printEmptyLine = false; + cin >> cases; + cin.ignore(); + getline(cin, line); + while (cases--) + { + totalChips = 0; + while (getline(cin, line), line.length()) + { + iss.clear(); + iss.str(line); + iss >> chips[totalChips].x >> chips[totalChips].y; + totalChips++; + } + int maxChips = 1; + for (int i = 0; i < totalChips - 1; i++) + for (int j = i + 1; j < totalChips; j++) + { + double distance = sqrt(pow(chips[i].x - chips[j].x, 2) + + pow(chips[i].y - chips[j].y, 2)); + if (distance > 5.0) + { + continue; + } + if (chips[i].x == chips[j].x) + { + center.x = chips[i].x + sqrt(2.50 * 2.50 - + pow(fabs(chips[i].y - chips[j].y) / 2.0, 2)); + center.y = (chips[i].y + chips[j].y) / 2.0; + maxChips = max(maxChips, chipsInCutter()); + center.x = chips[i].x - sqrt(2.50 * 2.50 - + pow((chips[i].y - chips[j].y) / 2.0, 2)); + maxChips = max(maxChips, chipsInCutter()); + continue; + } + if (chips[i].y == chips[j].y) + { + center.x = (chips[i].x + chips[j].x) / 2.0; + center.y = chips[i].y + sqrt(2.50 * 2.50 - + pow(fabs(chips[i].x - chips[j].x) / 2.0, 2)); + maxChips = max(maxChips, chipsInCutter()); + center.y = chips[i].y - sqrt(2.50 * 2.50 - + pow(fabs(chips[i].x - chips[j].x) / 2.0, 2)); + maxChips = max(maxChips, chipsInCutter()); + continue; + } + point median; + median.x = (chips[i].x + chips[j].x) / 2.0; + median.y = (chips[i].y + chips[j].y) / 2.0; + double slope = -(chips[j].x - chips[i].x) / + (chips[j].y - chips[i].y); + double segment = sqrt(2.50 * 2.50 - + (pow(chips[i].x - chips[j].x, 2) + + pow(chips[i].y - chips[j].y, 2)) / + 4.0); + double A = atan(slope); + center.x = median.x + segment * cos(A); + center.y = median.y + segment * sin(A); + maxChips = max(maxChips, chipsInCutter()); + center.x = median.x - segment * cos(A); + center.y = median.y - segment * sin(A); + maxChips = max(maxChips, chipsInCutter()); + } + if (printEmptyLine) + { + cout << endl; + } + else + { + printEmptyLine = true; + } + cout << maxChips << endl; + } + return 0; +} diff --git a/10137.cpp b/10137.cpp new file mode 100644 index 0000000..8ebb7df --- /dev/null +++ b/10137.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +double ss[1001]; + +int main() +{ + int n, i, s; + double sum, av; + while (scanf("%d", &n) && n) + { + sum = s = 0; + for (i = 0; i < n; i++) + { + scanf("%lf", &ss[i]); + ss[i] *= 100; + sum += ss[i]; + } + av = sum / (double)n; + s = sum = 0; + for (i = 0; i < n; i++) + { + if (av > ss[i]) + { + s += int(av - ss[i]); + } + if (av < ss[i]) + { + sum += int(ss[i] - av); + } + } + if (sum < s) + { + sum = s; + } + printf("$%.2lf\n", double(sum) / 100); + } + return 0; +} diff --git a/10138.cpp b/10138.cpp new file mode 100644 index 0000000..1bfef1b --- /dev/null +++ b/10138.cpp @@ -0,0 +1,224 @@ +#include + +using namespace std; + +string car[1000]; +int day[1000], hour[1000], minute[1000], pos[1000]; +bool enter[1000]; +int cost[24]; +deque rsort[60]; +vector charcar, running; +vector charmon, bh, posi; + +int main() +{ + int N; + bool begin = true; + string record; + scanf("%d", &N); + scanf("\n"); + for (int i = 0; i < N; i++) + { + charcar.clear(); + running.clear(); + charmon.clear(); + bh.clear(); + posi.clear(); + int count = 0; + for (int j = 0; j < 24; j++) + { + scanf("%d", cost + j); + } + bool first = true; + while (true) + { + if (first) + { + first = false; + getline(cin, record, '\n'); + } + if (!getline(cin, record, '\n')) + { + break; + } + if (record == "") + { + break; + } + int loc = record.find(' '); + car[count] = record.substr(0, loc); + record = record.substr(loc + 1); + loc = record.find(':'); + record = record.substr(loc + 1); + loc = record.find(':'); + day[count] = (record[loc - 2] - '0') * 10 + record[loc - 1] - '0'; + record = record.substr(loc + 1); + loc = record.find(':'); + hour[count] = (record[loc - 2] - '0') * 10 + record[loc - 1] - '0'; + record = record.substr(loc + 1); + loc = record.find(' '); + minute[count] = (record[loc - 2] - '0') * 10 + record[loc - 1] - '0'; + record = record.substr(loc + 1); + loc = record.find("enter"); + if (loc >= 0) + { + enter[count] = true; + record = record.substr(loc + 6); + } + else + { + loc = record.find("exit"); + enter[count] = false; + record = record.substr(loc + 5); + } + int temp = 0; + for (int j = 0; j < record.size(); j++) + { + if (record[j] <= '9' && record[j] >= '0') + { + temp *= 10; + temp += record[j] - '0'; + } + } + pos[count++] = temp; + } + int index[count]; + for (int j = 0; j < count; j++) + { + rsort[minute[j]].push_back(j); + } + int count2 = 0; + for (int j = 0; j < 60; j++) + { + while (rsort[j].size() != 0) + { + index[count2++] = rsort[j].front(); + rsort[j].pop_front(); + } + } + for (int j = 0; j < count; j++) + { + rsort[hour[index[j]]].push_back(index[j]); + } + count2 = 0; + for (int j = 0; j < 24; j++) + { + while (rsort[j].size() != 0) + { + index[count2++] = rsort[j].front(); + rsort[j].pop_front(); + } + } + for (int j = 0; j < count; j++) + { + rsort[day[index[j]]].push_back(index[j]); + } + count2 = 0; + for (int j = 0; j < 32; j++) + { + while (rsort[j].size() != 0) + { + index[count2++] = rsort[j].front(); + rsort[j].pop_front(); + } + } + string carnow; + bool enow; + for (int j = 0; j < count; j++) + { + carnow = car[index[j]]; + enow = enter[index[j]]; + if (enow) + { + bool find = false; + int loc; + for (int k = 0; k < running.size(); k++) + { + if (running[k] == carnow) + { + loc = k; + find = true; + break; + } + } + if (find) + { + bh[loc] = hour[index[j]]; + posi[loc] = pos[index[j]]; + } + else + { + running.push_back(carnow); + bh.push_back(hour[index[j]]); + posi.push_back(pos[index[j]]); + } + } + else + { + bool find = false; + int loc; + for (int k = 0; k < running.size(); k++) + { + if (running[k] == carnow) + { + loc = k; + find = true; + break; + } + } + if (find) + { + find = false; + int loc2; + for (int k = 0; k < charcar.size(); k++) + { + if (charcar[k] == carnow) + { + loc2 = k; + find = true; + break; + } + } + if (find) + { + charmon[loc2] += fabs((pos[index[j]] - posi[loc]) * cost[bh[loc]]) + 100; + } + else + { + charcar.push_back(carnow); + charmon.push_back(fabs((pos[index[j]] - posi[loc]) * cost[bh[loc]]) + 100); + } + running.erase(running.begin() + loc); + bh.erase(bh.begin() + loc); + posi.erase(posi.begin() + loc); + } + } + } + if (begin) + { + begin = false; + } + else + { + putchar(10); + } + while (charcar.size() > 0) + { + int next = 0; + string ncar = charcar[0]; + for (int k = 1; k < charcar.size(); k++) + { + if (charcar[k] < ncar) + { + next = k; + ncar = charcar[k]; + } + } + cout << charcar[next]; + printf(" $%.2lf\n", (charmon[next] + 200) / 100.0); + charcar.erase(charcar.begin() + next); + charmon.erase(charmon.begin() + next); + } + } + return 0; +} diff --git a/10139.cpp b/10139.cpp new file mode 100644 index 0000000..e58cf39 --- /dev/null +++ b/10139.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +const int plist[4793] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839, 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047, 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233, 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329, 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539, 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959, 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583, 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949, 19961, 19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, 20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, 20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, 20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, 20359, 20369, 20389, 20393, 20399, 20407, 20411, 20431, 20441, 20443, 20477, 20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, 20599, 20611, 20627, 20639, 20641, 20663, 20681, 20693, 20707, 20717, 20719, 20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, 20849, 20857, 20873, 20879, 20887, 20897, 20899, 20903, 20921, 20929, 20939, 20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, 21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, 21157, 21163, 21169, 21179, 21187, 21191, 21193, 21211, 21221, 21227, 21247, 21269, 21277, 21283, 21313, 21317, 21319, 21323, 21341, 21347, 21377, 21379, 21383, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, 21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, 21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21661, 21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, 21787, 21799, 21803, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, 21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, 22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, 22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, 22189, 22193, 22229, 22247, 22259, 22271, 22273, 22277, 22279, 22283, 22291, 22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, 22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, 22549, 22567, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, 22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, 22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, 22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, 23003, 23011, 23017, 23021, 23027, 23029, 23039, 23041, 23053, 23057, 23059, 23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, 23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, 23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, 23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, 23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, 23633, 23663, 23669, 23671, 23677, 23687, 23689, 23719, 23741, 23743, 23747, 23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, 23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, 23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, 24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, 24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, 24239, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, 24391, 24407, 24413, 24419, 24421, 24439, 24443, 24469, 24473, 24481, 24499, 24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, 24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24767, 24781, 24793, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, 24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989, 25013, 25031, 25033, 25037, 25057, 25073, 25087, 25097, 25111, 25117, 25121, 25127, 25147, 25153, 25163, 25169, 25171, 25183, 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261, 25301, 25303, 25307, 25309, 25321, 25339, 25343, 25349, 25357, 25367, 25373, 25391, 25409, 25411, 25423, 25439, 25447, 25453, 25457, 25463, 25469, 25471, 25523, 25537, 25541, 25561, 25577, 25579, 25583, 25589, 25601, 25603, 25609, 25621, 25633, 25639, 25643, 25657, 25667, 25673, 25679, 25693, 25703, 25717, 25733, 25741, 25747, 25759, 25763, 25771, 25793, 25799, 25801, 25819, 25841, 25847, 25849, 25867, 25873, 25889, 25903, 25913, 25919, 25931, 25933, 25939, 25943, 25951, 25969, 25981, 25997, 25999, 26003, 26017, 26021, 26029, 26041, 26053, 26083, 26099, 26107, 26111, 26113, 26119, 26141, 26153, 26161, 26171, 26177, 26183, 26189, 26203, 26209, 26227, 26237, 26249, 26251, 26261, 26263, 26267, 26293, 26297, 26309, 26317, 26321, 26339, 26347, 26357, 26371, 26387, 26393, 26399, 26407, 26417, 26423, 26431, 26437, 26449, 26459, 26479, 26489, 26497, 26501, 26513, 26539, 26557, 26561, 26573, 26591, 26597, 26627, 26633, 26641, 26647, 26669, 26681, 26683, 26687, 26693, 26699, 26701, 26711, 26713, 26717, 26723, 26729, 26731, 26737, 26759, 26777, 26783, 26801, 26813, 26821, 26833, 26839, 26849, 26861, 26863, 26879, 26881, 26891, 26893, 26903, 26921, 26927, 26947, 26951, 26953, 26959, 26981, 26987, 26993, 27011, 27017, 27031, 27043, 27059, 27061, 27067, 27073, 27077, 27091, 27103, 27107, 27109, 27127, 27143, 27179, 27191, 27197, 27211, 27239, 27241, 27253, 27259, 27271, 27277, 27281, 27283, 27299, 27329, 27337, 27361, 27367, 27397, 27407, 27409, 27427, 27431, 27437, 27449, 27457, 27479, 27481, 27487, 27509, 27527, 27529, 27539, 27541, 27551, 27581, 27583, 27611, 27617, 27631, 27647, 27653, 27673, 27689, 27691, 27697, 27701, 27733, 27737, 27739, 27743, 27749, 27751, 27763, 27767, 27773, 27779, 27791, 27793, 27799, 27803, 27809, 27817, 27823, 27827, 27847, 27851, 27883, 27893, 27901, 27917, 27919, 27941, 27943, 27947, 27953, 27961, 27967, 27983, 27997, 28001, 28019, 28027, 28031, 28051, 28057, 28069, 28081, 28087, 28097, 28099, 28109, 28111, 28123, 28151, 28163, 28181, 28183, 28201, 28211, 28219, 28229, 28277, 28279, 28283, 28289, 28297, 28307, 28309, 28319, 28349, 28351, 28387, 28393, 28403, 28409, 28411, 28429, 28433, 28439, 28447, 28463, 28477, 28493, 28499, 28513, 28517, 28537, 28541, 28547, 28549, 28559, 28571, 28573, 28579, 28591, 28597, 28603, 28607, 28619, 28621, 28627, 28631, 28643, 28649, 28657, 28661, 28663, 28669, 28687, 28697, 28703, 28711, 28723, 28729, 28751, 28753, 28759, 28771, 28789, 28793, 28807, 28813, 28817, 28837, 28843, 28859, 28867, 28871, 28879, 28901, 28909, 28921, 28927, 28933, 28949, 28961, 28979, 29009, 29017, 29021, 29023, 29027, 29033, 29059, 29063, 29077, 29101, 29123, 29129, 29131, 29137, 29147, 29153, 29167, 29173, 29179, 29191, 29201, 29207, 29209, 29221, 29231, 29243, 29251, 29269, 29287, 29297, 29303, 29311, 29327, 29333, 29339, 29347, 29363, 29383, 29387, 29389, 29399, 29401, 29411, 29423, 29429, 29437, 29443, 29453, 29473, 29483, 29501, 29527, 29531, 29537, 29567, 29569, 29573, 29581, 29587, 29599, 29611, 29629, 29633, 29641, 29663, 29669, 29671, 29683, 29717, 29723, 29741, 29753, 29759, 29761, 29789, 29803, 29819, 29833, 29837, 29851, 29863, 29867, 29873, 29879, 29881, 29917, 29921, 29927, 29947, 29959, 29983, 29989, 30011, 30013, 30029, 30047, 30059, 30071, 30089, 30091, 30097, 30103, 30109, 30113, 30119, 30133, 30137, 30139, 30161, 30169, 30181, 30187, 30197, 30203, 30211, 30223, 30241, 30253, 30259, 30269, 30271, 30293, 30307, 30313, 30319, 30323, 30341, 30347, 30367, 30389, 30391, 30403, 30427, 30431, 30449, 30467, 30469, 30491, 30493, 30497, 30509, 30517, 30529, 30539, 30553, 30557, 30559, 30577, 30593, 30631, 30637, 30643, 30649, 30661, 30671, 30677, 30689, 30697, 30703, 30707, 30713, 30727, 30757, 30763, 30773, 30781, 30803, 30809, 30817, 30829, 30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, 30937, 30941, 30949, 30971, 30977, 30983, 31013, 31019, 31033, 31039, 31051, 31063, 31069, 31079, 31081, 31091, 31121, 31123, 31139, 31147, 31151, 31153, 31159, 31177, 31181, 31183, 31189, 31193, 31219, 31223, 31231, 31237, 31247, 31249, 31253, 31259, 31267, 31271, 31277, 31307, 31319, 31321, 31327, 31333, 31337, 31357, 31379, 31387, 31391, 31393, 31397, 31469, 31477, 31481, 31489, 31511, 31513, 31517, 31531, 31541, 31543, 31547, 31567, 31573, 31583, 31601, 31607, 31627, 31643, 31649, 31657, 31663, 31667, 31687, 31699, 31721, 31723, 31727, 31729, 31741, 31751, 31769, 31771, 31793, 31799, 31817, 31847, 31849, 31859, 31873, 31883, 31891, 31907, 31957, 31963, 31973, 31981, 31991, 32003, 32009, 32027, 32029, 32051, 32057, 32059, 32063, 32069, 32077, 32083, 32089, 32099, 32117, 32119, 32141, 32143, 32159, 32173, 32183, 32189, 32191, 32203, 32213, 32233, 32237, 32251, 32257, 32261, 32297, 32299, 32303, 32309, 32321, 32323, 32327, 32341, 32353, 32359, 32363, 32369, 32371, 32377, 32381, 32401, 32411, 32413, 32423, 32429, 32441, 32443, 32467, 32479, 32491, 32497, 32503, 32507, 32531, 32533, 32537, 32561, 32563, 32569, 32573, 32579, 32587, 32603, 32609, 32611, 32621, 32633, 32647, 32653, 32687, 32693, 32707, 32713, 32717, 32719, 32749, 32771, 32779, 32783, 32789, 32797, 32801, 32803, 32831, 32833, 32839, 32843, 32869, 32887, 32909, 32911, 32917, 32933, 32939, 32941, 32957, 32969, 32971, 32983, 32987, 32993, 32999, 33013, 33023, 33029, 33037, 33049, 33053, 33071, 33073, 33083, 33091, 33107, 33113, 33119, 33149, 33151, 33161, 33179, 33181, 33191, 33199, 33203, 33211, 33223, 33247, 33287, 33289, 33301, 33311, 33317, 33329, 33331, 33343, 33347, 33349, 33353, 33359, 33377, 33391, 33403, 33409, 33413, 33427, 33457, 33461, 33469, 33479, 33487, 33493, 33503, 33521, 33529, 33533, 33547, 33563, 33569, 33577, 33581, 33587, 33589, 33599, 33601, 33613, 33617, 33619, 33623, 33629, 33637, 33641, 33647, 33679, 33703, 33713, 33721, 33739, 33749, 33751, 33757, 33767, 33769, 33773, 33791, 33797, 33809, 33811, 33827, 33829, 33851, 33857, 33863, 33871, 33889, 33893, 33911, 33923, 33931, 33937, 33941, 33961, 33967, 33997, 34019, 34031, 34033, 34039, 34057, 34061, 34123, 34127, 34129, 34141, 34147, 34157, 34159, 34171, 34183, 34211, 34213, 34217, 34231, 34253, 34259, 34261, 34267, 34273, 34283, 34297, 34301, 34303, 34313, 34319, 34327, 34337, 34351, 34361, 34367, 34369, 34381, 34403, 34421, 34429, 34439, 34457, 34469, 34471, 34483, 34487, 34499, 34501, 34511, 34513, 34519, 34537, 34543, 34549, 34583, 34589, 34591, 34603, 34607, 34613, 34631, 34649, 34651, 34667, 34673, 34679, 34687, 34693, 34703, 34721, 34729, 34739, 34747, 34757, 34759, 34763, 34781, 34807, 34819, 34841, 34843, 34847, 34849, 34871, 34877, 34883, 34897, 34913, 34919, 34939, 34949, 34961, 34963, 34981, 35023, 35027, 35051, 35053, 35059, 35069, 35081, 35083, 35089, 35099, 35107, 35111, 35117, 35129, 35141, 35149, 35153, 35159, 35171, 35201, 35221, 35227, 35251, 35257, 35267, 35279, 35281, 35291, 35311, 35317, 35323, 35327, 35339, 35353, 35363, 35381, 35393, 35401, 35407, 35419, 35423, 35437, 35447, 35449, 35461, 35491, 35507, 35509, 35521, 35527, 35531, 35533, 35537, 35543, 35569, 35573, 35591, 35593, 35597, 35603, 35617, 35671, 35677, 35729, 35731, 35747, 35753, 35759, 35771, 35797, 35801, 35803, 35809, 35831, 35837, 35839, 35851, 35863, 35869, 35879, 35897, 35899, 35911, 35923, 35933, 35951, 35963, 35969, 35977, 35983, 35993, 35999, 36007, 36011, 36013, 36017, 36037, 36061, 36067, 36073, 36083, 36097, 36107, 36109, 36131, 36137, 36151, 36161, 36187, 36191, 36209, 36217, 36229, 36241, 36251, 36263, 36269, 36277, 36293, 36299, 36307, 36313, 36319, 36341, 36343, 36353, 36373, 36383, 36389, 36433, 36451, 36457, 36467, 36469, 36473, 36479, 36493, 36497, 36523, 36527, 36529, 36541, 36551, 36559, 36563, 36571, 36583, 36587, 36599, 36607, 36629, 36637, 36643, 36653, 36671, 36677, 36683, 36691, 36697, 36709, 36713, 36721, 36739, 36749, 36761, 36767, 36779, 36781, 36787, 36791, 36793, 36809, 36821, 36833, 36847, 36857, 36871, 36877, 36887, 36899, 36901, 36913, 36919, 36923, 36929, 36931, 36943, 36947, 36973, 36979, 36997, 37003, 37013, 37019, 37021, 37039, 37049, 37057, 37061, 37087, 37097, 37117, 37123, 37139, 37159, 37171, 37181, 37189, 37199, 37201, 37217, 37223, 37243, 37253, 37273, 37277, 37307, 37309, 37313, 37321, 37337, 37339, 37357, 37361, 37363, 37369, 37379, 37397, 37409, 37423, 37441, 37447, 37463, 37483, 37489, 37493, 37501, 37507, 37511, 37517, 37529, 37537, 37547, 37549, 37561, 37567, 37571, 37573, 37579, 37589, 37591, 37607, 37619, 37633, 37643, 37649, 37657, 37663, 37691, 37693, 37699, 37717, 37747, 37781, 37783, 37799, 37811, 37813, 37831, 37847, 37853, 37861, 37871, 37879, 37889, 37897, 37907, 37951, 37957, 37963, 37967, 37987, 37991, 37993, 37997, 38011, 38039, 38047, 38053, 38069, 38083, 38113, 38119, 38149, 38153, 38167, 38177, 38183, 38189, 38197, 38201, 38219, 38231, 38237, 38239, 38261, 38273, 38281, 38287, 38299, 38303, 38317, 38321, 38327, 38329, 38333, 38351, 38371, 38377, 38393, 38431, 38447, 38449, 38453, 38459, 38461, 38501, 38543, 38557, 38561, 38567, 38569, 38593, 38603, 38609, 38611, 38629, 38639, 38651, 38653, 38669, 38671, 38677, 38693, 38699, 38707, 38711, 38713, 38723, 38729, 38737, 38747, 38749, 38767, 38783, 38791, 38803, 38821, 38833, 38839, 38851, 38861, 38867, 38873, 38891, 38903, 38917, 38921, 38923, 38933, 38953, 38959, 38971, 38977, 38993, 39019, 39023, 39041, 39043, 39047, 39079, 39089, 39097, 39103, 39107, 39113, 39119, 39133, 39139, 39157, 39161, 39163, 39181, 39191, 39199, 39209, 39217, 39227, 39229, 39233, 39239, 39241, 39251, 39293, 39301, 39313, 39317, 39323, 39341, 39343, 39359, 39367, 39371, 39373, 39383, 39397, 39409, 39419, 39439, 39443, 39451, 39461, 39499, 39503, 39509, 39511, 39521, 39541, 39551, 39563, 39569, 39581, 39607, 39619, 39623, 39631, 39659, 39667, 39671, 39679, 39703, 39709, 39719, 39727, 39733, 39749, 39761, 39769, 39779, 39791, 39799, 39821, 39827, 39829, 39839, 39841, 39847, 39857, 39863, 39869, 39877, 39883, 39887, 39901, 39929, 39937, 39953, 39971, 39979, 39983, 39989, 40009, 40013, 40031, 40037, 40039, 40063, 40087, 40093, 40099, 40111, 40123, 40127, 40129, 40151, 40153, 40163, 40169, 40177, 40189, 40193, 40213, 40231, 40237, 40241, 40253, 40277, 40283, 40289, 40343, 40351, 40357, 40361, 40387, 40423, 40427, 40429, 40433, 40459, 40471, 40483, 40487, 40493, 40499, 40507, 40519, 40529, 40531, 40543, 40559, 40577, 40583, 40591, 40597, 40609, 40627, 40637, 40639, 40693, 40697, 40699, 40709, 40739, 40751, 40759, 40763, 40771, 40787, 40801, 40813, 40819, 40823, 40829, 40841, 40847, 40849, 40853, 40867, 40879, 40883, 40897, 40903, 40927, 40933, 40939, 40949, 40961, 40973, 40993, 41011, 41017, 41023, 41039, 41047, 41051, 41057, 41077, 41081, 41113, 41117, 41131, 41141, 41143, 41149, 41161, 41177, 41179, 41183, 41189, 41201, 41203, 41213, 41221, 41227, 41231, 41233, 41243, 41257, 41263, 41269, 41281, 41299, 41333, 41341, 41351, 41357, 41381, 41387, 41389, 41399, 41411, 41413, 41443, 41453, 41467, 41479, 41491, 41507, 41513, 41519, 41521, 41539, 41543, 41549, 41579, 41593, 41597, 41603, 41609, 41611, 41617, 41621, 41627, 41641, 41647, 41651, 41659, 41669, 41681, 41687, 41719, 41729, 41737, 41759, 41761, 41771, 41777, 41801, 41809, 41813, 41843, 41849, 41851, 41863, 41879, 41887, 41893, 41897, 41903, 41911, 41927, 41941, 41947, 41953, 41957, 41959, 41969, 41981, 41983, 41999, 42013, 42017, 42019, 42023, 42043, 42061, 42071, 42073, 42083, 42089, 42101, 42131, 42139, 42157, 42169, 42179, 42181, 42187, 42193, 42197, 42209, 42221, 42223, 42227, 42239, 42257, 42281, 42283, 42293, 42299, 42307, 42323, 42331, 42337, 42349, 42359, 42373, 42379, 42391, 42397, 42403, 42407, 42409, 42433, 42437, 42443, 42451, 42457, 42461, 42463, 42467, 42473, 42487, 42491, 42499, 42509, 42533, 42557, 42569, 42571, 42577, 42589, 42611, 42641, 42643, 42649, 42667, 42677, 42683, 42689, 42697, 42701, 42703, 42709, 42719, 42727, 42737, 42743, 42751, 42767, 42773, 42787, 42793, 42797, 42821, 42829, 42839, 42841, 42853, 42859, 42863, 42899, 42901, 42923, 42929, 42937, 42943, 42953, 42961, 42967, 42979, 42989, 43003, 43013, 43019, 43037, 43049, 43051, 43063, 43067, 43093, 43103, 43117, 43133, 43151, 43159, 43177, 43189, 43201, 43207, 43223, 43237, 43261, 43271, 43283, 43291, 43313, 43319, 43321, 43331, 43391, 43397, 43399, 43403, 43411, 43427, 43441, 43451, 43457, 43481, 43487, 43499, 43517, 43541, 43543, 43573, 43577, 43579, 43591, 43597, 43607, 43609, 43613, 43627, 43633, 43649, 43651, 43661, 43669, 43691, 43711, 43717, 43721, 43753, 43759, 43777, 43781, 43783, 43787, 43789, 43793, 43801, 43853, 43867, 43889, 43891, 43913, 43933, 43943, 43951, 43961, 43963, 43969, 43973, 43987, 43991, 43997, 44017, 44021, 44027, 44029, 44041, 44053, 44059, 44071, 44087, 44089, 44101, 44111, 44119, 44123, 44129, 44131, 44159, 44171, 44179, 44189, 44201, 44203, 44207, 44221, 44249, 44257, 44263, 44267, 44269, 44273, 44279, 44281, 44293, 44351, 44357, 44371, 44381, 44383, 44389, 44417, 44449, 44453, 44483, 44491, 44497, 44501, 44507, 44519, 44531, 44533, 44537, 44543, 44549, 44563, 44579, 44587, 44617, 44621, 44623, 44633, 44641, 44647, 44651, 44657, 44683, 44687, 44699, 44701, 44711, 44729, 44741, 44753, 44771, 44773, 44777, 44789, 44797, 44809, 44819, 44839, 44843, 44851, 44867, 44879, 44887, 44893, 44909, 44917, 44927, 44939, 44953, 44959, 44963, 44971, 44983, 44987, 45007, 45013, 45053, 45061, 45077, 45083, 45119, 45121, 45127, 45131, 45137, 45139, 45161, 45179, 45181, 45191, 45197, 45233, 45247, 45259, 45263, 45281, 45289, 45293, 45307, 45317, 45319, 45329, 45337, 45341, 45343, 45361, 45377, 45389, 45403, 45413, 45427, 45433, 45439, 45481, 45491, 45497, 45503, 45523, 45533, 45541, 45553, 45557, 45569, 45587, 45589, 45599, 45613, 45631, 45641, 45659, 45667, 45673, 45677, 45691, 45697, 45707, 45737, 45751, 45757, 45763, 45767, 45779, 45817, 45821, 45823, 45827, 45833, 45841, 45853, 45863, 45869, 45887, 45893, 45943, 45949, 45953, 45959, 45971, 45979, 45989, 46021, 46027, 46049, 46051, 46061, 46073, 46091, 46093, 46099, 46103, 46133, 46141, 46147, 46153, 46171, 46181, 46183, 46187, 46199, 46219, 46229, 46237, 46261, 46271, 46273, 46279, 46301, 46307, 46309, 46327, 46337, 46349}; + +int p, q; + +int gcd(int a, int b) +{ + int t; + while (b) + { + t = a % b; + a = b; + b = t; + } + return a; +} + +bool isprime(int n) +{ + int s = (int)sqrt((double)n); + for (int i = 0; plist[i] <= s; i++) + { + if (n % plist[i] == 0) + { + p = plist[i]; + q = n / plist[i]; + return false; + } + } + return true; +} + +int main() +{ + int n, m, s, t, g, k, value; + while (scanf("%d%d", &n, &m) == 2) + { + if (m <= 1) + { + if (m == 0) + { + value = 0; + } + else if (m == 1) + { + value = 1; + } + } + else if (n >= m) + { + value = 1; + } + else + { + value = 0; + t = m; + s = 2; + while (s <= n) + { + if (isprime(t)) + { + if (t >= s && t <= n) + { + value = 1; + break; + } + else if (t < s) + { + k = 1; + while (k * t < s) + { + k++; + } + if (k * t > n) + { + value = 0; + break; + } + else + { + value = 1; + break; + } + } + else + { + value = 0; + break; + } + } + else + { + if (s <= p) + { + s = p + 1; + t = q; + } + else + { + k = 1; + while (k * p < s) + { + k++; + } + if (k * p > n) + { + value = 0; + break; + } + else + { + g = gcd(k, q); + q /= g; + if (q == 1) + { + value = 1; + break; + } + for (int i = k * p - 1; i >= s; i--) + { + g = gcd(i, q); + q /= g; + if (q == 1) + { + value = 1; + break; + } + } + t = q; + s = k * p + 1; + } + } + } + } + } + if (value) + { + printf("%d divides %d!\n", m, n); + } + else + { + printf("%d does not divide %d!\n", m, n); + } + } + return 0; +} diff --git a/10140.cpp b/10140.cpp new file mode 100644 index 0000000..fa10784 --- /dev/null +++ b/10140.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +long long low, upper; +bool flag[1000010]; +int prime[1000000]; + +void sieve() +{ + int d, i, j; + d = upper - low + 1; + for (i = 0; i < d; i++) + { + flag[i] = true; + } + if (low % 2 == 0) + { + i = 0; + } + else + { + i = 1; + } + for (; i < d; i += 2) + { + flag[i] = false; + } + for (i = 3; i <= sqrt(upper); i += 2) + { + if (i > low && !flag[i - low]) + { + continue; + } + j = low / i * i; + if (j < low) + { + j += i; + } + if (j == i) + { + j += i; + } + j -= low; + for (; j < d; j += i) + { + flag[j] = false; + } + } + if (low == 1) + { + flag[1 - low] = false; + } + if (low <= 2) + { + flag[2 - low] = true; + } +} + +bool isPrime(int n) +{ + if (n == 0 || n == 1) + { + return 0; + } + int i; + for (i = 2; i <= sqrt(n); i++) + { + if (n % i == 0) + { + return 0; + } + } + return 1; +} + +void solve() +{ + int max, min, maxFir, maxSec, minFir, minSec; + bool exist = false; + max = -1; + min = 0x7fffffff; + long long index, numOfPrime = 0; + for (index = low; index <= upper; index++) + { + if (flag[index - low]) + { + prime[numOfPrime++] = index; + } + } + for (index = 0; index < numOfPrime - 1; index++) + { + if ((prime[index + 1] - prime[index]) > max) + { + max = prime[index + 1] - prime[index]; + maxFir = prime[index]; + maxSec = prime[index + 1]; + exist = true; + } + if ((prime[index + 1] - prime[index]) < min) + { + min = prime[index + 1] - prime[index]; + minFir = prime[index]; + minSec = prime[index + 1]; + exist = true; + } + } + if (exist) + { + cout << minFir << "," << minSec << " are closest, " << maxFir << "," << maxSec << " are most distant." << endl; + } + else + { + cout << "There are no adjacent primes." << endl; + } +} + +int main() +{ + while (cin >> low >> upper) + { + sieve(); + solve(); + } + return 0; +} diff --git a/10141.cpp b/10141.cpp new file mode 100644 index 0000000..8f2ea79 --- /dev/null +++ b/10141.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +struct winner +{ + string name; + double price; + int match; + winner() : name(""), price(0.0), match(-1) {} +}; + +int main() +{ + int n, p, match; + double price; + int counter = 0; + string input, name; + while (cin >> n >> p) + { + if (n == 0 && p == 0) + { + break; + } + if (counter > 0) + { + cout << endl; + } + counter++; + winner best; + getline(cin, input);// \n + while (n--) + { + getline(cin, input); + } + while (p--) + { + getline(cin, name); + cin >> price >> match; + getline(cin, input);// \n + for (int i = 0; i < match; i++) + { + getline(cin, input); + } + if (match > best.match) + { + best.name = name; + best.price = price; + best.match = match; + } + else if (match == best.match) + { + if (price < best.price) + { + best.name = name; + best.price = price; + best.match = match; + } + } + } + cout << "RFP #" << counter << endl; + cout << best.name << endl; + } + return 0; +} diff --git a/10142.cpp b/10142.cpp new file mode 100644 index 0000000..140fa1a --- /dev/null +++ b/10142.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int main() +{ + int n, tmp; + int cases; + string input; + map candidates; + int votes[1001][21]; + map eliminated; + cin >> cases; + while (cases--) + { + cin >> n; + cin.ignore(100, '\n'); + candidates.clear(); + eliminated.clear(); + for (int i = 1; i <= n; i++) + { + getline(cin, input); + candidates[i] = input; + } + int cur = 0; + while (cin.peek() != '\n' && cin.peek() != -1) + { + for (int i = 0; i < n; i++) + { + cin >> votes[cur][i]; + } + cin.ignore(100, '\n'); + cur++; + } + int best; + int count[21]; + while (true) + { + best = 0; + fill(count, count + 21, 0); + for (int i = 0; i < cur; i++) + { + for (int j = 0; j < n; j++) + { + if (eliminated[votes[i][j]]) + { + continue; + } + count[votes[i][j]]++; + if (count[votes[i][j]] > best) + { + best = count[votes[i][j]]; + } + break; + } + } + if (best * 2 > cur) + { + break; + } + int lowest = best; + for (int i = 1; i <= n; i++) + { + if (eliminated[i]) + { + continue; + } + if (count[i] < lowest) + { + lowest = count[i]; + } + } + if (lowest == best) + { + break; + } + for (int i = 1; i <= n; i++) + { + if (eliminated[i]) + { + continue; + } + if (count[i] == lowest) + { + eliminated[i] = true; + } + } + } + for (int i = 1; i <= n; i++) + { + if (eliminated[i]) + { + continue; + } + if (count[i] == best) + { + cout << candidates[i] << endl; + } + } + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/10144.cpp b/10144.cpp new file mode 100644 index 0000000..64bba24 --- /dev/null +++ b/10144.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +void Recursion(int d) +{ + printf("("); + if (d) + { + printf("(A%d|B%d)|(", d, d); + Recursion(d - 1); + printf("|((A%d|A%d)|(B%d|B%d)))", d, d, d, d); + } + else + { + printf("(A0|B0)|(A0|B0)"); + } + printf(")"); +} + +int main() +{ + char str[80]; + int times, n; + gets(str); + sscanf(str, "%d", ×); + gets(str); + for (; times; times--) + { + gets(str); + sscanf(str, "%d", &n); + Recursion(n - 1); + printf("\n"); + gets(str); + if (times != 1) + { + puts(""); + } + } + return 0; +} diff --git a/10145.cpp b/10145.cpp new file mode 100644 index 0000000..66858be --- /dev/null +++ b/10145.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int T, trid, item; +char mode, s[100]; + +int main() +{ + for (scanf("%d\n", &T); T--; gets(s)) + { + set ignore; + map mutex; + map > shared; + + while (gets(s) && *s != '#') + { + sscanf(s, "%c%d%d", &mode, &trid, &item); + if (ignore.find(trid) != ignore.end()) + { + printf("IGNORED\n"); + } + else if (mode == 'S') + { + if (mutex.find(item) != mutex.end()) + { + if (mutex[item] != trid) + { + printf("DENIED\n"); + ignore.insert(trid); + } + else + { + printf("GRANTED\n"); + } + } + else + { + printf("GRANTED\n"); + shared[item].insert(trid); + } + } + else if (shared.find(item) != shared.end()) + { + // X + if (shared[item].size() > 1 || *shared[item].begin() != trid) + { + printf("DENIED\n"), ignore.insert(trid); + } + else + { + printf("GRANTED\n"), mutex[item] = trid; + } + } + else if (mutex.find(item) == mutex.end() || mutex[item] == trid) + { + // not in shared + printf("GRANTED\n"); + mutex[item] = trid; + } + else + { + printf("DENIED\n"); + ignore.insert(trid); + } + } + if (T) + { + printf("\n"); + } + } +} diff --git a/10146.cpp b/10146.cpp new file mode 100644 index 0000000..b1aae4e --- /dev/null +++ b/10146.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define MAX 10 + +void ToRun(void) +{ + char tmp[MAX + 1], in[MAX + 1]; + int space, i, min; + gets(tmp); + puts(tmp); + for (space = 0; gets(in);) + { + if (!strlen(in)) + { + break; + } + min = strlen(tmp); + min = min < strlen(in) ? min : strlen(in); + for (i = 0; i < min; ++i) + if (tmp[i] != in[i]) + { + break; + } + if (i > space) + { + ++space; + } + else + { + space = i; + } + for (i = 0; i < space; ++i) + { + putchar(' '); + } + puts(in); + strcpy(tmp, in); + } +} + +int main() +{ + int N_case; + scanf("%d\n\n", &N_case); + for (; N_case; --N_case) + { + ToRun(); + if (N_case - 1) + { + putchar('\n'); + } + } + return 0; +} diff --git a/10147.cpp b/10147.cpp new file mode 100644 index 0000000..59884b8 --- /dev/null +++ b/10147.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +#define MAXN 281626 +#define MAX 752 + +struct Edge +{ + int x, y; + int dis; +} B[MAXN], Mst[MAXN]; +struct ss +{ + int x, y; +} pp[MAX]; +int P[MAX], Rank[MAX]; +char F[MAX][MAX]; +int N, E; +int com(const void *a, const void *b) +{ + Edge *x = (Edge *)a; + Edge *y = (Edge *)b; + return x->dis - y->dis; +} +int Find(int n) +{ + if (n != P[n]) + { + P[n] = Find(P[n]); + } + return P[n]; +} +void Link(int x, int y) +{ + if (Rank[x] > Rank[y]) + { + P[y] = x; + } + else + { + P[x] = y; + if (Rank[x] == Rank[y]) + { + Rank[y]++; + } + } +} +int Dis(int x1, int y1, int x2, int y2) +{ + int a, b, dis; + a = (x1 - x2) * (x1 - x2); + b = (y1 - y2) * (y1 - y2); + dis = (a + b); + return dis; +} +void MST() +{ + int i, a, b, len = 0; + qsort(B, E, sizeof(B[0]), com); + for (i = 0; i < E && len < N - 1; i++) + { + a = Find(B[i].x); + b = Find(B[i].y); + if (a != b) + { + Mst[len++] = B[i]; + Link(a, b); + } + } + if (len == 0) + { + printf("No new highways need\n"); + return; + } + for (i = 0; i < len; i++) + { + printf("%d %d\n", Mst[i].x, Mst[i].y); + } +} +void Cal() +{ + int given, a, b, x, y; + int i, j; + int dis; + E = 0; + scanf("%d", &given); + for (i = 1; i <= N; i++) + { + Rank[i] = 0; + P[i] = i; + } + while (given--) + { + scanf("%d%d", &a, &b); + F[a][b] = 1; + x = Find(a); + y = Find(b); + if (x != y) + { + Link(x, y); + } + } + for (i = 0; i < N - 1; i++) + { + for (j = i + 1; j < N; j++) + { + if (F[i + 1][j + 1] == 1) + { + F[i + 1][j + 1] = 0; + continue; + } + dis = Dis(pp[i].x, pp[i].y, pp[j].x, pp[j].y); + B[E].dis = dis; + B[E].x = i + 1; + B[E++].y = j + 1; + } + } + MST(); +} + +int main() +{ + int i, kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%d%d", &pp[i].x, &pp[i].y); + } + Cal(); + if (kase) + { + putchar('\n'); + } + } + return 0; +} diff --git a/10148.cpp b/10148.cpp new file mode 100644 index 0000000..3fb7254 --- /dev/null +++ b/10148.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define MAXD 20010 +#define MAXN 1010 +#define D 10000 + +int a[MAXN], b[MAXN], K, N, r[MAXN], hash_[MAXD]; + +int cmp(const void *_p, const void *_q) +{ + int *p = (int *)_p; + int *q = (int *)_q; + return b[*p] - b[*q]; +} +void init() +{ + int i, j, k; + scanf("%d%d", &K, &N); + for (i = 0; i < N; ++i) + { + scanf("%d%d", &a[i], &b[i]); + if (a[i] > b[i]) + { + k = a[i], a[i] = b[i], b[i] = k; + } + a[i] += D, b[i] += D; + } +} +void solve() +{ + int i, j, k, e, ans; + for (i = 0; i < N; ++i) + { + r[i] = i; + } + qsort(r, N, sizeof(r[0]), cmp); + memset(hash_, 0, sizeof(hash_)); + ans = 0; + for (i = 0; i < N; i++) + { + k = 0; + e = r[i]; + for (j = a[e]; j <= b[e]; ++j) + if (hash_[j]) + { + ++k; + } + for (j = b[e]; j >= a[e] && k < K; --j) + if (!hash_[j]) + { + hash_[j] = 1; + ++k; + ++ans; + } + } + printf("%d\n", ans); + for (i = 0; i <= 20000; ++i) + if (hash_[i]) + { + printf("%d\n", i - D); + } +} + +int main() +{ + int t, tt; + scanf("%d", &t); + for (tt = 0; tt < t; ++tt) + { + init(); + if (tt) + { + printf("\n"); + } + solve(); + } + return 0; +} diff --git a/10149.cpp b/10149.cpp new file mode 100644 index 0000000..3174a1b --- /dev/null +++ b/10149.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +#define STATE 8192// 1<<13 +#define STATE2 64 // bonus use +#define DSIZE 13 + +int DICE[13][5]; + +int score_cat(int dice[], int cat) +{ + int tot = 0, i, D[7]; + switch (cat) + { + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + for (i = 0; i < 5; i++) + { + if (dice[i] == cat) + { + tot += cat; + } + } + break; + case 7:// chance + for (i = 0; i < 5; i++) + { + tot += dice[i]; + } + break; + case 8:// three of a kind + if (dice[0] == dice[2] || dice[1] == dice[3] || dice[2] == dice[4]) + { + for (i = 0; i < 5; i++) + { + tot += dice[i]; + } + } + break; + case 9:// four of a kind + if (dice[0] == dice[3] || dice[1] == dice[4]) + { + for (i = 0; i < 5; i++) + { + tot += dice[i]; + } + } + break; + case 10:// five of a kind + if (dice[0] == dice[4]) + { + tot = 50; + } + break; + case 11:// short straight + for (i = 0; i <= 6; i++) + { + D[i] = 0; + } + for (i = 0; i < 5; i++) + { + D[dice[i]] = 1; + } + for (i = 1; i <= 3; i++) + { + if (D[i] && D[i + 1] && D[i + 2] && D[i + 3]) + { + tot = 25; + } + } + break; + case 12:// long straight + for (i = 0; i < 4; i++) + { + if (dice[i] != dice[i + 1] - 1) + { + return 0; + } + } + tot = 35; + break; + case 13:// full house + if (dice[0] == dice[1] && dice[2] == dice[4]) + { + tot = 40; + } + if (dice[0] == dice[2] && dice[3] == dice[4]) + { + tot = 40; + } + break; + } + return tot; +} + +int cnt_bit(int n) +{ + static int i, j; + for (i = 0, j = 0; i < 13; i++) + { + j += (n >> i) & 1; + } + return j; +} + +int dp[STATE][STATE2]; // = max score +int score[DSIZE][DSIZE]; // score[i][j] = DICE[i] cat j +int arg_dp[STATE][STATE2][2];// [0] category, [1] pre(1-6) total score + +void sol_dp() +{ + int i, j, k, p, q; + for (i = 0; i < 13; i++) + { + for (j = 0; j < 13; j++) + { + score[i][j] = score_cat(DICE[i], j + 1); + } + } + memset(dp, -1, sizeof(dp)); + dp[0][0] = 0; + int s, nstate, a, b; + for (k = 0; k < 13; k++) + {// add DICE[k] + for (i = 0; i < STATE; i++) + {// i = STATE mark + if (cnt_bit(i) == k) + { + for (j = 0; j < 13; j++) + {// set category + if (!((i >> j) & 1)) + { + s = score[k][j]; + nstate = i | (1 << j);// new state + a = j < 6 ? s : 0; + for (p = 0; p < STATE2; p++) + { + if (dp[i][p] >= 0) + { + b = (p + a < 63) ? p + a : 63; + if (dp[nstate][b] < dp[i][p] + s) + { + dp[nstate][b] = dp[i][p] + s; + arg_dp[nstate][b][0] = j; + arg_dp[nstate][b][1] = p; + } + } + } + } + } + } + } + } + int mx = 0, bouns = 0; + for (i = 0; i < 63; i++) + { + if (dp[STATE - 1][i] > mx) + { + mx = dp[STATE - 1][i]; + b = i; + } + } + if (dp[STATE - 1][63] >= 0 && dp[STATE - 1][63] + 35 > mx) + { + mx = dp[STATE - 1][63] + 35; + b = 63; + bouns = 35; + } + // backtrack + int category[13], ans[13], state = STATE - 1; + for (i = 12; i >= 0; i--) + { + category[i] = arg_dp[state][b][0]; + b = arg_dp[state][b][1]; + state ^= 1 << category[i]; + ans[category[i]] = score[i][category[i]]; + } + for (i = 0; i < 13; i++) + { + printf("%d ", ans[i]); + } + printf("%d %d\n", bouns, mx); +} + +int main() +{ + int i, j; + while (1) + { + for (i = 0; i < 13; i++) + { + for (j = 0; j < 5; j++) + { + if (scanf("%d", &DICE[i][j]) != 1) + { + return 0; + } + } + sort(DICE[i], DICE[i] + 5); + } + sol_dp(); + } + return 0; +} diff --git a/10150.cpp b/10150.cpp new file mode 100644 index 0000000..c180a3f --- /dev/null +++ b/10150.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef string vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + void bfs(const vdata &a, const vdata &b); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + + Vertex *getVertex(const vdata &v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(const vdata &v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::bfs(const string &a, const string &b) +{ + Vertex *v; + map visited; + pair tmp; + queue> q; + q.push(make_pair(a, a + "\n")); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + v = getVertex(tmp.first); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + if (visited.find(v->adj[i]->id) != visited.end()) + { + continue; + } + if (v->adj[i]->id == b) + { + cout << tmp.second << b << endl; + return; + } + visited[v->adj[i]->id] = true; + q.push(make_pair(v->adj[i]->id, tmp.second + v->adj[i]->id + "\n")); + } + } + cout << "No solution." << endl; +} + +bool is_doublet(const string &a, const string &b) +{ + int diff = 0; + for (int i = 0, sz = a.size(); i < sz; i++) + { + if (a[i] != b[i]) + { + diff++; + if (diff > 1) + { + return false; + } + } + } + return true; +} + +int main() +{ + int len; + bool first = true; + Graph g; + vector words[17]; + string input, a, b; + while (getline(cin, input)) + { + if (input == "") + { + break; + } + len = input.size(); + for (int i = 0, sz = words[len].size(); i < sz; i++) + { + if (!is_doublet(input, words[len][i])) + { + continue; + } + g.addEdge(input, words[len][i]); + } + words[len].push_back(input); + } + while (cin >> a >> b) + { + if (!first) + { + cout << endl; + } + g.bfs(a, b); + first = false; + } + return 0; +} diff --git a/10152.cpp b/10152.cpp new file mode 100644 index 0000000..861c151 --- /dev/null +++ b/10152.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int k; + scanf("%d", &k); + while (k--) + { + int n; + scanf("%d\n", &n); + char original[200][82], required[200][82]; + for (int i = 0; i < n; ++i) + { + fgets(original[i], 82, stdin); + } + for (int i = 0; i < n; ++i) + { + fgets(required[i], 82, stdin); + } + int rIdx = n - 1; + for (int oIdx = n - 1; oIdx >= 0; --oIdx) + { + if (strcmp(original[oIdx], required[rIdx]) == 0) + { + rIdx--; + } + } + for (int i = rIdx; i >= 0; --i) + { + printf(required[i]); + } + putchar('\n'); + } + return 0; +} diff --git a/10154.cpp b/10154.cpp new file mode 100644 index 0000000..ec45136 --- /dev/null +++ b/10154.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define maxn 5610 +#define INF 10000000 + +long n = 0, ans = 0, a[maxn], b[maxn], d[maxn][maxn]; + +void Qsort(long begin, long end) +{ + long i = begin, j = end, mid = b[(begin + end) / 2], t; + do + { + while (b[i] < mid) + { + i++; + } + while (b[j] > mid) + { + j--; + } + if (i <= j) + { + t = a[i]; + a[i] = a[j]; + a[j] = t; + t = b[i]; + b[i] = b[j]; + b[j] = t; + i++; + j--; + } + } while (i <= j); + if (begin < j) + { + Qsort(begin, j); + } + if (i < end) + { + Qsort(i, end); + } +} + +int main() +{ + while (scanf("%ld%ld", &a[n + 1], &b[n + 1]) == 2) + { + n++; + b[n] -= a[n]; + } + // Read In + Qsort(1, n); + // Sort + for (long i = 0; i <= n; i++) + for (long j = 0; j <= n; j++) + { + d[i][j] = INF; + } + for (long i = 0; i <= n; i++) + { + d[i][0] = 0; + } + // Init + for (long i = 1; i <= n; i++) + for (long j = 1; j <= i; j++) + { + d[i][j] = min(d[i][j], d[i - 1][j]); + if (d[i - 1][j - 1] <= b[i]) + { + d[i][j] = min(d[i][j], d[i - 1][j - 1] + a[i]); + } + } + // DP + for (long i = n; i >= 1; i--) + if (d[n][i] < INF) + { + ans = i; + break; + } + printf("%ld\n", ans); + return 0; +} diff --git a/10155.cpp b/10155.cpp new file mode 100644 index 0000000..f11396e --- /dev/null +++ b/10155.cpp @@ -0,0 +1,230 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +typedef struct +{ + int h, w, c; + char s[1]; +} box_t; +int tok; +char toks[65536]; + +box_t *make(int h, int w, int c) +{ + box_t *p = (box_t *)malloc(sizeof(box_t) + h * w + 1); + p->h = h; + p->w = w; + p->c = c; + memset(p->s, ' ', h * w); + return p; +} +void put(box_t *p, int y, int x, int c) +{ + p->s[y * p->w + x] = c; +} +void copy(box_t *z, int y0, int x0, box_t *p) +{ + int i, j; + for (i = 0; i < p->h; i++) + for (j = 0; j < p->w; j++) + { + z->s[(y0 + i) * z->w + (x0 + j)] = p->s[i * p->w + j]; + } +} +box_t *join(box_t *a, char op, box_t *b) +{ + box_t *p = make(MAX(a->c, b->c) + MAX(a->h - a->c, b->h - b->c), + a->w + 3 + b->w, MAX(a->c, b->c)); + copy(p, p->c - a->c, 0, a); + copy(p, p->c - b->c, a->w + 3, b); + put(p, p->c, a->w + 1, op); + free(a); + free(b); + return p; +} +int next() +{ + int i, c; + while ((c = getchar()) != EOF && isspace(c)) + { + c++; + } + if (c == EOF) + { + return (tok = EOF); + } + if (strchr("+-*/^=(){}", c) != NULL) + { + return (tok = c); + } + toks[0] = c; + for (i = 1; (c = getchar()) != EOF && (isalnum(c) || c == '.');) + { + toks[i++] = c; + } + toks[i] = '\0'; + if (c != EOF) + { + ungetc(c, stdin); + } + return (tok = 's'); +} +box_t *level5(); +box_t *level1() +{ + box_t *p, *q; + int i; + if (tok == '-') + { + next(); + q = level1(); + p = make(q->h, q->w + 1, q->c); + copy(p, 0, 1, q); + put(p, q->c, 0, '-'); + free(q); + return p; + } + else if (tok == '{') + { + next(); + p = level5(); + assert(tok == '}'); + next(); + return p; + } + else if (tok == '(') + { + next(); + q = level5(); + assert(tok == ')'); + next(); + p = make(q->h, q->w + 2, q->c); + copy(p, 0, 1, q); + for (i = 0; i < p->h; i++) + { + put(p, i, 0, '('); + put(p, i, p->w - 1, ')'); + } + free(q); + p->c = p->h / 2; + return p; + } + else + { + assert(tok == 's'); + p = make(1, strlen(toks), 0); + strcpy(p->s, toks); + next(); + return p; + } +} +box_t *level2() +{ + box_t *p, *q, *r; + p = level1(); + if (tok != '^') + { + return p; + } + next(); + q = level2(); + r = make(p->h + q->h, p->w + q->w, p->c + q->h); + copy(r, q->h, 0, p); + copy(r, 0, p->w, q); + free(p); + free(q); + return r; +} +box_t *level3() +{ + box_t *p, *q, *r; + int i; + for (p = level2();;) + { + if (tok == '*') + { + next(), p = join(p, '*', level2()); + } + else if (tok == '/') + { + next(); + r = level2(); + q = make(p->h + 1 + r->h, MAX(p->w, r->w), p->h); + if (p->w < r->w) + { + copy(q, 0, (r->w - p->w + 1) / 2, p); + copy(q, p->h + 1, 0, r); + } + else + { + copy(q, 0, 0, p); + copy(q, p->h + 1, (p->w - r->w + 1) / 2, r); + } + for (i = 0; i < q->w; i++) + { + put(q, p->h, i, '-'); + } + free(p); + free(r); + p = q; + } + else + { + return p; + } + } +} +box_t *level4() +{ + box_t *p; + int op; + for (p = level3(); tok == '+' || tok == '-';) + { + op = tok, next(), p = join(p, op, level3()); + } + return p; +} +box_t *level5() +{ + box_t *p; + for (p = level4(); tok == '=';) + { + next(), p = join(p, '=', level4()); + } + return p; +} + +int main() +{ + int i, j, t; + box_t *p; + next(); + for (t = 0; tok != EOF;) + { + if (t++) + { + printf("\n"); + } + p = level5(); + for (i = 0; i < p->h; i++) + { + for (j = p->w - 1; j >= 0 && p->s[i * p->w + j] == ' '; j--) + { + p->s[i * p->w + j] = 0; + } + printf("%.*s", p->w, p->s + i * p->w); + // PE: print trailing spaces to make rectangular box + for (int x = strlen(p->s + i * p->w); x < p->w; x++) + { + printf(" "); + } + printf("\n"); + } + free(p); + } + return 0; +} diff --git a/10156.cpp b/10156.cpp new file mode 100644 index 0000000..89d5d9a --- /dev/null +++ b/10156.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define M 10005 + +int nx, ny, t, k, mz[65][65]; + +struct turs +{ + int x, y; +} tur[M]; + +bool chk(int x, int y) +{ + return !(x >= nx || x < 0 || y >= ny || y < 0 || mz[x][y]); +} + +void solve() +{ + char str[100]; + while (k--) + { + int id; + scanf("%d%s", &id, str); + if (!strcmp(str, "W")) + { + int x = tur[id].x - 1, y = tur[id].y; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "E")) + { + int x = tur[id].x + 1, y = tur[id].y; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "N")) + { + int x = tur[id].x, y = tur[id].y - 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "S")) + { + int x = tur[id].x, y = tur[id].y + 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "NE")) + { + int x = tur[id].x + 1, y = tur[id].y - 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "NW")) + { + int x = tur[id].x - 1, y = tur[id].y - 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "SE")) + { + int x = tur[id].x + 1, y = tur[id].y + 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + else if (!strcmp(str, "SW")) + { + int x = tur[id].x - 1, y = tur[id].y + 1; + if (chk(x, y)) + { + mz[tur[id].x][tur[id].y] = 0; + mz[x][y] = 1; + tur[id].x = x; + tur[id].y = y; + } + } + } + for (int i = 0; i < ny; i++) + { + int to = -1; + for (int j = nx - 1; j >= 0; j--) + { + if (mz[j][i]) + { + to = j; + break; + } + } + for (int j = 0; j <= to; j++) + { + printf((mz[j][i]) ? "*" : " "); + } + printf("\n"); + } +} + +int main() +{ + while (scanf("%d%d%d%d", &ny, &nx, &t, &k) != EOF) + { + memset(mz, 0, sizeof(mz)); + for (int i = 0; i < t; i++) + { + int id; + scanf("%d", &id); + scanf("%d%d", &tur[id].y, &tur[id].x); + mz[tur[id].x][tur[id].y] = 1; + } + solve(); + printf("\n"); + } + return 0; +} diff --git a/10157.cpp b/10157.cpp new file mode 100644 index 0000000..0bd4ad4 --- /dev/null +++ b/10157.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define NLIMBS 24 + +typedef struct +{ + unsigned a[NLIMBS], n; +} num_t; + +void set_(num_t *x, unsigned a) +{ + x->n = (x->a[0] = a) ? 1 : 0; +} + +void copy(num_t *z, num_t *x) +{ + memcpy(z->a, x->a, (z->n = x->n) * sizeof(x->a[0])); +} + +void muladd(num_t *z, num_t *x, num_t *y) +{ + unsigned i, j, k, c; + for (k = x->n + y->n + 1; z->n < k;) + { + z->a[z->n++] = 0; + } + z->a[z->n++] = 0; + for (i = 0; i < x->n; i++) + { + for (c = j = 0, k = i; j < y->n; j++, k++, c >>= 16) + { + z->a[k] = (c += z->a[k] + x->a[i] * y->a[j]) & 0xFFFF; + } + for (; c > 0; k++, c >>= 16) + { + z->a[k] = (c += z->a[k]) & 0xFFFF; + } + } + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } +} + +void sub(num_t *z, num_t *x) +{ + unsigned i, b; + for (b = 0, i = 0; i < z->n; i++) + { + if (z->a[i] >= (x->a[i] + b)) + { + z->a[i] -= x->a[i] + b; + b = 0; + } + else + { + z->a[i] = 0x10000 + z->a[i] - x->a[i] - b; + b = 1; + } + } + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } +} + +void print(num_t *t) +{ + static unsigned s[65536]; + unsigned i, k, r; + for (k = 0; k == 0 || t->n > 0;) + { + for (r = 0, i = t->n; i-- > 0; r %= 10) + { + t->a[i] = (r = (r << 16) | t->a[i]) / 10; + } + for (s[k++] = r; t->n > 0 && t->a[t->n - 1] == 0; t->n--) + ; + } + while (k-- > 0) + { + putchar(s[k] + '0'); + } + putchar('\n'); +} + +num_t zero, one, tab[160][160]; +int got[160][160]; + +num_t *f(int n, int d) +{ + int k; + if (n < 0 || d < 0 || (n & 1) != 0) + { + return &zero; + } + if (n == 0) + { + return &one; + } + if (got[n >> 1][d]) + { + return &tab[n >> 1][d]; + } + for (set_(&tab[n >> 1][d], 0), k = 2; k <= n; k += 2) + { + muladd(&tab[n >> 1][d], f(k - 2, d - 1), f(n - k, d)); + } + got[n >> 1][d] = 1; + return &tab[n >> 1][d]; +} + +int main() +{ + static num_t t; + int n, d; + set_(&zero, 0); + set_(&one, 1); + memset(got, 0, sizeof(got)); + while (scanf("%d %d", &n, &d) == 2) + { + copy(&t, f(n, d)); + sub(&t, f(n, d - 1)); + print(&t); + } + return 0; +} diff --git a/10158.cpp b/10158.cpp new file mode 100644 index 0000000..a7debc3 --- /dev/null +++ b/10158.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +#define E(c) ((c % 2) ? (c + 1) : (c - 1)) + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + bool areEnemies(int a, int b); + bool areFriends(int a, int b); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +bool UnionFindDisjointSets::areEnemies(int a, int b) +{ + return findSet(a) == findSet(E(b)) || findSet(b) == findSet(E(a)); +} + +bool UnionFindDisjointSets::areFriends(int a, int b) +{ + return findSet(a) == findSet(b) || findSet(E(a)) == findSet(E(b)); +} + +int main() +{ + int n, c, a, b; + cin >> n; + UnionFindDisjointSets ds(2 * n); + while (cin >> c >> a >> b) + { + if (c == 0 && a == 0 && b == 0) + { + break; + } + a = a * 2 + 1; + b = b * 2 + 1; + switch (c) + { + case 1: + // setFriends + if (ds.areEnemies(a, b)) + { + cout << "-1" << endl; + } + else + { + ds.unionSet(a, b), ds.unionSet(E(a), E(b)); + } + break; + case 2: + // setEnemies + if (ds.areFriends(a, b)) + { + cout << "-1" << endl; + } + else + { + ds.unionSet(a, E(b)), ds.unionSet(b, E(a)); + } + break; + case 3: + // areFriends + cout << (ds.areFriends(a, b) ? "1" : "0") << endl; + break; + case 4: + // areEnemies + cout << (ds.areEnemies(a, b) ? "1" : "0") << endl; + break; + } + } + return 0; +} diff --git a/10159.cpp b/10159.cpp new file mode 100644 index 0000000..54c02d5 --- /dev/null +++ b/10159.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +#define MAXLINES 12 +#define MAXCELLS 48 +#define MAXINT 10 +#define EMPTY (-1) +#define MAXTYPES (1 << 12) + +int maxValue[MAXLINES]; +int cells[MAXCELLS]; +string belongs[MAXCELLS] = + { + "EL", + "EK", "EL", "FL", + "AI", "AI", "AJ", "AEJ", "AEK", "AFK", "AFL", "AGL", "AG", "AH", "AH", + "BI", "BEI", "BEJ", "BFJ", "BFK", "BGK", "BGL", "BHL", "BH", + "CE", "CEI", "CFI", "CFJ", "CGJ", "CGK", "CHK", "CHL", "CL", + "DE", "DE", "DF", "DFI", "DGI", "DGJ", "DHJ", "DHK", "DK", "DL", "DL", + "GI", "HI", "HJ", + "HI"}; +int lines[MAXLINES][MAXLINES - 1] = + { + {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, + {15, 16, 17, 18, 19, 20, 21, 22, 23, EMPTY, EMPTY}, + {24, 25, 26, 27, 28, 29, 30, 31, 32, EMPTY, EMPTY}, + {33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43}, + {0, 1, 2, 7, 8, 16, 17, 24, 25, 33, 34}, + {3, 9, 10, 18, 19, 26, 27, 35, 36, EMPTY, EMPTY}, + {11, 12, 20, 21, 28, 29, 37, 38, 44, EMPTY, EMPTY}, + {13, 14, 22, 23, 30, 31, 39, 40, 45, 46, 47}, + {4, 5, 15, 16, 25, 26, 36, 37, 44, 45, 47}, + {6, 7, 17, 18, 27, 28, 38, 39, 46, EMPTY, EMPTY}, + {1, 8, 9, 19, 20, 29, 30, 40, 41, EMPTY, EMPTY}, + {0, 2, 3, 10, 11, 21, 22, 31, 32, 42, 43}}; +int dp() +{ + int sum[MAXTYPES], tmp[MAXTYPES]; + memset(sum, EMPTY, sizeof(sum)); + sum[0] = 0; + for (int i = 0; i < MAXLINES; i++) + { + memcpy(tmp, sum, sizeof(sum)); + for (int j = 0; j < MAXTYPES; j++) + if (sum[j] > EMPTY) + { + for (int k = 0; k < MAXLINES - 1; k++) + { + if (lines[i][k] == EMPTY) + { + break; + } + if (cells[lines[i][k]] == maxValue[i]) + { + int t = j; + string cell = belongs[lines[i][k]]; + for (int c = 0; c < cell.length(); c++) + if (cells[lines[i][k]] == maxValue[cell[c] - 'A']) + { + t = t | (1 << (cell[c] - 'A')); + } + if (tmp[t] > EMPTY) + { + tmp[t] = min(tmp[t], sum[j] + maxValue[i]); + } + else + { + tmp[t] = sum[j] + maxValue[i]; + } + } + } + } + memcpy(sum, tmp, sizeof(tmp)); + } + return sum[MAXTYPES - 1]; +} + +int main() +{ + string line; + while (getline(cin, line)) + { + istringstream iss(line); + for (int i = 0; i < MAXLINES; i++) + { + iss >> maxValue[i]; + } + memset(cells, 0, sizeof(cells)); + for (int i = 0; i < MAXCELLS; i++) + { + int value = MAXINT; + for (int j = 0; j < belongs[i].length(); j++) + value = min(value, + maxValue[belongs[i][j] - 'A']); + cells[i] = value; + } + bool noSolution = false; + for (int i = 0; i < MAXLINES; i++) + { + int tmp = 0; + for (int j = 0; j < MAXLINES - 1; j++) + { + if (lines[i][j] == EMPTY) + { + break; + } + tmp = max(tmp, cells[lines[i][j]]); + } + if (tmp != maxValue[i]) + { + noSolution = true; + break; + } + } + if (noSolution) + { + cout << "NO SOLUTION" << endl; + } + else + { + int maxSum = 0; + for (int i = 0; i < MAXCELLS; i++) + { + maxSum += cells[i]; + } + int minSum = dp(); + cout << minSum << " " << maxSum << endl; + } + } + return 0; +} diff --git a/1016.cpp b/1016.cpp new file mode 100644 index 0000000..5fa72b3 --- /dev/null +++ b/1016.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +const int NN = 1024; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +int pos[NN], v[NN], v2[NN]; + +int main() +{ + for (int n, T = 1; scanf("%d", &n) == 1 && n; ++T) + { + FOR(i, n) + scanf("%d", v + i), pos[v[i]] = i, v2[i] = v[i]; + sort(v2, v2 + n); + int cost = 0, swapper = v2[0]; + FOR(i, n) + { + int idx = -1, steps = 0, z = NN; + FOR(j, n) + if (v[j] != v2[j] && v[j] < z) + { + idx = j, z = v[j]; + } + if (idx == -1) + { + break; + } + while (v[idx] != v2[idx]) + { + z = pos[v2[idx]]; + cost += v[z]; + swap(v[z], v[idx]); + swap(pos[v2[idx]], pos[v[idx]]); + idx = pos[v[idx]]; + ++steps; + } + if (steps * (v[idx] - swapper) < 2 * (v[idx] + swapper)) + { + cost += v[idx] * steps; + } + else + { + cost += 2 * (v[idx] + swapper) + swapper * steps; + } + } + printf("Case %d: %d\n\n", T, cost); + } + return 0; +} diff --git a/10160.cpp b/10160.cpp new file mode 100644 index 0000000..101a20a --- /dev/null +++ b/10160.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +#define MAX_TOWN 35 + +typedef long long unsigned LLUINT; + +bool finish; +int minimum; +vector edge; +LLUINT target_tag, origin_tag; + +void check(int flag[], int position) +{ + int index = 0; + LLUINT new_tag = origin_tag, old_tag = origin_tag; + for (int i = 0; i < edge.size(); i++) + { + if ((index < position) && (flag[index] == i)) + { + new_tag |= edge[i]; + if (new_tag > old_tag) + { + old_tag = new_tag; + } + else + { + return; + } + index++; + } + } + if (new_tag == target_tag) + { + minimum = index; + finish = true; + } +} +void generate(int flag[], int position, int positions) +{ + if (finish) + { + return; + } + if (position < positions) + { + if (position == 0) + { + for (int i = 0; i < edge.size(); i++) + { + flag[position] = i; + generate(flag, position + 1, positions); + } + } + else + { + for (int i = flag[position - 1] + 1; i < edge.size(); i++) + { + flag[position] = i; + generate(flag, position + 1, positions); + } + } + } + else + { + check(flag, position); + } +} +void enumerating_subset() +{ + for (int i = 1; i <= edge.size(); i++) + { + int *flag = new int[edge.size()]; + generate(flag, 0, i); + delete[] flag; + if (finish) + { + return; + } + } +} +bool cmp(LLUINT x, LLUINT y) +{ + return x > y; +} +int mdsn(vector> &vertex) +{ + int base = 0; + origin_tag = 0; + target_tag = 0; + vector dirty(vertex.size()); + fill(dirty.begin(), dirty.end(), false); + for (int i = 0; i < vertex.size(); i++) + { + if (vertex[i].size() == 0) + { + base++; + origin_tag |= ((LLUINT)1 << i); + } + if (vertex[i].size() == 1 && dirty[i] == false) + { + dirty[i] = true; + if (dirty[vertex[i][0] - 1] == false) + { + base++; + dirty[vertex[i][0] - 1] = true; + } + } + target_tag |= ((LLUINT)1 << i); + } + edge.clear(); + for (int i = 0; i < vertex.size(); i++) + { + if (dirty[i] == true) + { + origin_tag |= ((LLUINT)1 << i); + for (int j = 0; j < vertex[i].size(); j++) + { + origin_tag |= ((LLUINT)1 << (vertex[i][j] - 1)); + } + } + if (dirty[i] == false && vertex[i].size() > 0) + { + LLUINT t = ((LLUINT)1 << i); + for (int j = 0; j < vertex[i].size(); j++) + { + t |= ((LLUINT)1 << (vertex[i][j] - 1)); + } + edge.push_back(t); + } + } + sort(edge.begin(), edge.end(), cmp); + minimum = 0; + finish = false; + enumerating_subset(); + return (base + minimum); +} +int servicing_stations(vector> &vertex) +{ + int total = 0; + while (vertex.size() > 0) + { + vector> open; + set close; + int size = 0; + open.push_back(vertex[0]); + close.insert(vertex[0][0]); + vertex.erase(vertex.begin()); + while (open.size() > size) + { + int origin = size; + int current = open.size() - 1; + size = open.size(); + for (int i = origin; i <= current; i++) + { + for (int j = 1; j < open[i].size(); j++) + { + if (close.find(open[i][j]) == close.end()) + { + close.insert(open[i][j]); + for (int m = 0; m < vertex.size(); m++) + { + if (vertex[m][0] == open[i][j]) + { + open.push_back(vertex[m]); + vertex.erase(vertex.begin() + m); + break; + } + } + } + } + } + } + vector> tmp; + for (int c = 1; c <= MAX_TOWN; c++) + if (close.find(c) != close.end()) + { + for (int i = 0; i < open.size(); i++) + if (open[i][0] == c) + { + tmp.push_back(open[i]); + } + } + for (int i = 0; i < tmp.size(); i++) + { + int current = tmp[i][0]; + for (int m = 0; m < tmp.size(); m++) + for (int n = 1; n < tmp[m].size(); n++) + if (tmp[m][n] == current) + { + tmp[m][n] = (i + 1); + } + } + for (int i = 0; i < tmp.size(); i++) + { + tmp[i].erase(tmp[i].begin()); + } + total += mdsn(tmp); + } + return total; +} +int main(int ac, char *av[]) +{ + int n; + int m; + int x, y; + vector> vertex; + while (cin >> n >> m, n && m) + { + vertex.clear(); + vertex.resize(n); + for (int i = 0; i < n; i++) + { + vertex[i].push_back((i + 1)); + } + for (int i = 0; i < m; i++) + { + cin >> x >> y; + if (x != y) + { + vertex[x - 1].push_back(y); + vertex[y - 1].push_back(x); + } + } + cout << servicing_stations(vertex) << endl; + } + return 0; +} diff --git a/10161.cpp b/10161.cpp index 99b1fa6..74407e1 100644 --- a/10161.cpp +++ b/10161.cpp @@ -1,56 +1,54 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int t, x, y, d; - scanf("%d", &t); - if (t == 0) break; - x = y = (int) ceil(sqrt(t)); - d = (x%2 ? 1 : -1) * (t - (int)(sqr(x-1) + x)); - - x -= max(d, 0); - y += min(d, 0); - - printf("%d %d\n", x, y); - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + int tmp = sqrt(n); + int left = n - tmp * tmp; + if (tmp % 2 == 0) + { + if (tmp * tmp == n) + { + cout << tmp << " 1" << endl; + } + else + { + if (left < tmp + 1) + { + cout << tmp + 1 << " " << left << endl; + } + else + { + cout << tmp + 1 - (left - tmp - 1) << " " << tmp + 1 << endl; + } + } + } + else + { + if (tmp * tmp == n) + { + cout << "1 " << tmp << endl; + } + else + { + if (left < tmp + 1) + { + cout << left << " " << tmp + 1 << endl; + } + else + { + cout << tmp + 1 << " " << tmp + 1 - (left - tmp - 1) << endl; + } + } + } + } + return 0; +} diff --git a/10162.cpp b/10162.cpp index 3fd3ff5..e50b4b9 100644 --- a/10162.cpp +++ b/10162.cpp @@ -1,63 +1,70 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int64 power(int64 b, int64 n){ - if(n==0) - return 1; - int64 x = power(b, n/2); - if(n%2 == 0) - return (x*x)%100000; - else - return (x*x*b)%100000; -} - -int main(){ - int lDigit[101]={0}, i; - for(i=1; i<=100; i++) - lDigit[i] = ( lDigit[i-1] + power(i, i) )%100000; - while(true){ - string str; - cin>>str; - if(str == "0") - break; - - int ind, len=str.length(); - - if( len == 1 ) - ind = str[len-1]; - else - ind = (str[len-2]-'0')*10 + (str[len-1]-'0'); - - cout< + +using namespace std; + +#define MAXN 202 +int A[MAXN], N; +char DD[MAXN]; + +void LastD(int n) +{ + int i, j, k; + k = n % 10; + if (k == 0 || k == 1 || k == 6 || k == 5) + { + N += k; + N = N % 10; + return; + } + j = 1; + for (i = 1; i <= n; i++) + { + j *= n; + j %= 10; + } + N += j; + N %= 10; +} +void Gen() +{ + int i; + N = 0; + for (i = 1; i <= 100; i++) + { + LastD(i); + A[i] = N; + } +} +int KEY() +{ + int i, k = 0, c = 1, sum = 0; + int l = strlen(DD); + for (i = l - 1; i >= 0; i--) + { + sum = (DD[i] - '0') * c; + k += sum; + c *= 10; + if (c == 100) + { + break; + } + } + return k; +} + +int main() +{ + int i; + Gen(); + while (1) + { + scanf("%s", DD); + if (!strcmp(DD, "0")) + { + break; + } + i = KEY(); + printf("%d\n", A[i]); + } + return 0; +} diff --git a/10163.cpp b/10163.cpp new file mode 100644 index 0000000..b3f3363 --- /dev/null +++ b/10163.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define MAXN 110 +#define MAXM 40 +#define INF 0x3f3f3f3f + +int N, M, P, f[MAXM][MAXN], p[MAXM]; + +int init() +{ + int i; + scanf("%d%d", &N, &M); + if (!N && !M) + { + return 0; + } + P = 0; + for (i = 1; i <= M; i++) + { + scanf("%d", &p[i]); + if (p[i] > P) + { + P = p[i]; + } + } + return 1; +} + +void solve() +{ + int i, j, k, min, mid, max; + min = 0, max = P + 1; + for (;;) + { + mid = (min + max) / 2;// binary search + for (i = 1; i <= N; i++) + { + f[0][i] = INF; + } + for (i = 0; i <= M; i++) + { + f[i][0] = 0; + } + for (i = 1; i <= M; i++) + for (j = 1; j <= N; j++) + { + f[i][j] = f[i - 1][j]; + for (k = 0; k < j; k++) + if (p[i] / (j - k) >= mid && f[i - 1][k] + p[i] < f[i][j]) + { + f[i][j] = f[i - 1][k] + p[i]; + } + } + if (min == mid) + { + break; + } + if (f[M][N] == INF) + { + max = mid; + } + else + { + min = mid; + } + } + + if (min == 0) + printf("0 0\n"); + else + printf("%d %d\n", mid, f[M][N]); +} + +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/10164.cpp b/10164.cpp new file mode 100644 index 0000000..950afe4 --- /dev/null +++ b/10164.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 1026 + +int Dummy[MAXN], Num[2 * MAXN], A[MAXN], Sum, N; +void Print(int k) +{ + int i, j; + j = A[0]; + printf("%d", Num[j]); + for (i = 1; i < k; i++) + { + j = A[i]; + printf(" %d", Num[j]); + } + printf("\n"); +} +int ncr(int level, int index, int r, int n) +{ + if (level == r) + { + if (Sum % N == 0) + { + printf("Yes\n"); + Print(r); + return 1; + } + return 0; + } + for (int i = index; i <= n; i++) + { + A[level] = i; + Sum += Num[i]; + if (ncr(level + 1, i + 1, r, n) == 1) + { + return 1; + } + Sum -= Num[i]; + } + return 0; +} + +int main() +{ + int i, k; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + Sum = 0; + k = 0; + for (i = 1; i <= 2 * N - 1; i++) + { + scanf("%d", &Num[i]); + if (Num[i] % N == 0 && k < N) + { + Dummy[k++] = Num[i]; + } + } + if (k == N) + { + printf("Yes\n"); + printf("%d", Dummy[0]); + for (i = 1; i < N; i++) + { + printf(" %d", Dummy[i]); + } + printf("\n"); + continue; + } + if (ncr(0, 1, N, 2 * N - 1) == 0) + { + printf("No\n"); + } + } + return 0; +} diff --git a/10165.cpp b/10165.cpp new file mode 100644 index 0000000..085f138 --- /dev/null +++ b/10165.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + int piles, stones, tmpStones; + while (cin >> piles, piles) + { + stones = 0; + for (int i = 1; i <= piles; i++) + { + cin >> tmpStones; + stones ^= tmpStones; + } + cout << (stones ? "Yes" : "No") << endl; + } + return 0; +} diff --git a/10166.cpp b/10166.cpp new file mode 100644 index 0000000..eb6034a --- /dev/null +++ b/10166.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +#define N 128 +#define pii pair +#define INF (1 << 29) + +struct edge +{ + int leave, arrive, nextCity; + edge(int _leave = 0, int _arrive = 0, int _nextCity = 0) + { + leave = _leave, arrive = _arrive, nextCity = _nextCity; + } +}; + +int dist[N]; +struct comp +{ + bool operator()(pii i, pii j) const + { + if (i.first != j.first) + { + return i.first > j.first; + } + return i.second < j.second; + } +}; + +set dij; +map tag; +vector adj[N], rev[N]; + +string toStr(int x) +{ + ostringstream oss; + oss << x; + return oss.str(); +} +string toHour(int x) +{ + int h = x / 60; + int m = x % 60; + string s; + if (h < 10) + { + s += '0'; + s += h + '0'; + } + else + { + s += toStr(h); + } + if (m < 10) + { + s += '0'; + s += m + '0'; + } + else + { + s += toStr(m); + } + return s; +} + +int toInt(string &s) +{ + return ((s[0] - '0') * 10 + s[1] - '0') * 60 + + ((s[2] - '0') * 10 + s[3] - '0'); +} + +int main() +{ + int n, lines, src, dst; + int stations, t1, t2, city1, city2, startTime; + int srcLeave, dstArrive; + string cityName, tt; + while (cin >> n, n) + { + tag.clear(); + for (int i = 0; i < n; i++) + { + cin >> cityName; + tag[cityName] = i; + dist[i] = INF; + adj[i].clear(); + rev[i].clear(); + } + cin >> lines; + while (lines--) + { + cin >> stations; + cin >> tt >> cityName; + t1 = toInt(tt); + city1 = tag[cityName]; + for (int i = 1; i < stations; i++) + { + cin >> tt >> cityName; + t2 = toInt(tt); + city2 = tag[cityName]; + adj[city1].push_back(edge(t1, t2, city2)); + rev[city2].push_back(edge(t2, t1, city1)); + t1 = t2; + city1 = city2; + } + } + cin >> tt; + startTime = toInt(tt); + cin >> cityName; + src = tag[cityName]; + cin >> cityName; + dst = tag[cityName]; + dij.clear(); + dist[src] = startTime; + dij.insert(pii(dist[src], src)); + while (!dij.empty()) + { + city1 = dij.begin()->second; + dij.erase(dij.begin()); + for (int i = 0; i < adj[city1].size(); i++) + { + t1 = adj[city1][i].leave; + t2 = adj[city1][i].arrive; + city2 = adj[city1][i].nextCity; + if (t1 >= dist[city1] && dist[city2] > t2) + { + dij.erase(pii(dist[city2], city2)); + dist[city2] = t2; + dij.insert(pii(dist[city2], city2)); + } + } + } + if (dist[dst] == INF) + { + cout << "No connection" << endl; + continue; + } + set revDij; + dstArrive = dist[dst]; + startTime = dist[dst]; + for (int i = 0; i < n; i++) + { + dist[i] = -1; + } + revDij.clear(); + dist[dst] = startTime; + revDij.insert(pii(dist[dst], dst)); + while (!revDij.empty()) + { + city1 = revDij.begin()->second; + revDij.erase(revDij.begin()); + for (int i = 0; i < rev[city1].size(); i++) + { + t1 = rev[city1][i].leave; + t2 = rev[city1][i].arrive; + city2 = rev[city1][i].nextCity; + if (t1 <= dist[city1] && dist[city2] < t2) + { + revDij.erase(pii(dist[city2], city2)); + dist[city2] = t2; + revDij.insert(pii(dist[city2], city2)); + } + } + } + srcLeave = dist[src]; + cout << toHour(srcLeave) << " " << toHour(dstArrive) << endl; + } + return 0; +} diff --git a/10167.cpp b/10167.cpp new file mode 100644 index 0000000..646a260 --- /dev/null +++ b/10167.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int n; + +struct point +{ + int x, y; + point() {} + point(const int &X, const int &Y) : x(X), y(Y) {} +}; +struct line +{ + int a, b; + line() {} + line(const int &A, const int &B) : a(A), b(B) {} +}; + +// positive if p is to the left of line l +// negative if p is to the right of line l +// zero if p is collinear with line l +int cross(const point &p, const line &l) +{ + point q; + q.x = l.b; + q.y = -l.a; + return (p.x * q.y - q.x * p.y); +} + +bool check(const vector &c, const line &l) +{ + int left = 0, right = 0; + for (int i = 0; i < 2 * n; ++i) + { + int t = cross(c[i], l); + left += (t > 0); + right += (t < 0); + } + return (left == n && right == n); +} + +int main() +{ + while (cin >> n && n) + { + vector cherry(2 * n); + for (int i = 0; i < 2 * n; ++i) + { + cin >> cherry[i].x >> cherry[i].y; + } + bool found = false; + for (int i = -500; i <= 500 && !found; ++i) + { + for (int j = -500; j <= 500 && !found; ++j) + { + if (check(cherry, line(i, j))) + { + cout << i << " " << j << endl; + found = true; + } + } + } + } + return 0; +} diff --git a/10168.cpp b/10168.cpp index 12bb375..4faaa99 100644 --- a/10168.cpp +++ b/10168.cpp @@ -1,73 +1,71 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define MAX 3165 +#define MAXN 10000000 -int N; -vector< int > Prime; -bool P[MAX + 1]; -map > Map; +char p[MAXN + 3]; -void seive() { - memset(P, true, sizeof P); - P[0] = false; P[1] = false; +int prime_table() +{ int i, j; - FOI(i, 2, MAX) { - if (P[i]) { - Prime.push_back(i); - for (j = i * i; j <= MAX; j+= i) - P[j] = false; + p[1] = 0; + for (i = 2; i <= MAXN; i++) + { + p[i] = 1; + } + for (i = 2; i < sqrt(MAXN);) + { + for (j = i + i; j <= MAXN; j += i) + { + p[j] = 0; } + for (i++; !p[i]; i++) + ; } - N = Prime.size() - 1; - cout << N << endl; + return 0; } -void Cal() { - FOI(a, 0, N) { - int V = Prime[a]; - FOI(b, a, N) { - V += Prime[b]; - FOI(c, b, N) { - V += Prime[c]; - FOI(d, c, N) { - V += Prime[d]; - } - } +int even(int n) +{ + int i, j; + if (n == 4) + { + printf("2 2\n"); + return 1; + } + for (i = 3, j = n - 3; i <= j; i++, j--) + { + if (p[i] && p[j]) + { + printf("%d %d\n", i, j); + return 1; } } + return 0; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - Cal(); +int main() +{ + int n, a, b, k; + prime_table(); + while (scanf("%d", &n) == 1) + { + if (n < 8) + { + printf("Impossible.\n"); + continue; + } + if (n % 2 == 0) + { + printf("2 2 "); + even(n - 4); + } + else + { + printf("2 3 "); + even(n - 5); + } + } return 0; } diff --git a/10169.cpp b/10169.cpp new file mode 100644 index 0000000..64bc185 --- /dev/null +++ b/10169.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define MAXD 1000010 + +double f[MAXD]; +int d[MAXD]; + +void prepare() +{ + int i, j, k; + double p1 = 1, p2 = 0; + for (i = 1; i < 1000000; i++) + { + p1 = p1 * ((double)i * i + i - 1) / ((double)i * i + i); + p2 += log10((double)i * i + i); + f[i] = 1 - p1; + d[i] = (int)(p2 - fmod(p2, 1) + 0.5); + } +} + +int main() +{ + int n; + prepare(); + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + printf("0.000000 0\n"); + } + else + { + printf("%.6lf %d\n", f[n], d[n]); + } + } + return 0; +} diff --git a/10170.cpp b/10170.cpp new file mode 100644 index 0000000..ab363bd --- /dev/null +++ b/10170.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +typedef unsigned long long Big; + +Big St, TDay; + +Big Val(Big g) +{ + Big v; + v = 2 * (St - 1) * g + (g * (g + 1)); + return v; +} + +Big Search() +{ + Big temp, dummy, hi, low, mid; + long long key; + key = TDay * 2; + temp = 2 * (St - 1); + dummy = temp * 1 + (1 * (1 + 1)); + if (dummy >= TDay * 2) + { + return 1; + } + low = 1; + hi = sqrt(key) + 1; + mid = (1 + hi) / 2;// binary search + dummy = temp * mid + (mid * (mid + 1)); + while (hi > low && dummy != key) + { + if (dummy > key) + { + if (Val(mid - 1) < key) + { + return mid; + } + hi = mid - 1; + } + else + { + low = mid + 1; + } + mid = (hi + low) / 2; + dummy = temp * mid + (mid * (mid + 1)); + } + return mid; +} + +void Cal() +{ + printf("%llu\n", Search() + St - 1); +} + +int main() +{ + while (scanf("%llu%llu", &St, &TDay) == 2) + { + if (St >= TDay) + { + printf("%llu\n", St); + continue; + } + Cal(); + } + return 0; +} diff --git a/10171.cpp b/10171.cpp index 51bdbef..821dac9 100644 --- a/10171.cpp +++ b/10171.cpp @@ -1,89 +1,117 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define INF 1<<30 +#define MX 30 +#define INF 900005 +#define MIN(a, b) (a > b ? b : a) -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int N; - cin >> N; - if (N == 0) break; - map MapY, namY; - map MapO, namO; - map::iterator it1, it2; - int matY[30][30], matO[30][30]; - int indY = 0, indO = 0; - int i, j, k; - FOI(i, 0, 29) { - FOI(j, 0, 29) { - if (i == j) - matY[i][j] = matO[i][j] = 0; - else - matY[i][j] = matO[i][j] = INF; - } +int S[MX][MX], M[MX][MX]; + +void Ini() +{ + int i, j; + for (i = 0; i < 26; i++) + { + for (j = 0; j < 26; j++) + { + S[i][j] = M[i][j] = INF; } - while (N--) { - string YM, UB, X, Y; - int D; - cin >> YM >> UB >> X >> Y >> D; - if (YM == "Y") { - it1 = MapY.find(X); - it2 = MapY.find(Y); - if (it1 == MapY.end()) { MapY[X] = indO; namY[indO] = X; indY++; } - if (it2 == MapY.end()) { MapY[Y] = indO; namY[indO] = Y; indY++; } - - matY[MapY[X]][MapY[Y]] = min(D, matY[MapY[X]][MapY[Y]]); - if (UB == "B") - matY[MapY[Y]][MapY[X]] = min(D, matY[MapY[Y]][MapY[X]]); - } - else { - it1 = MapO.find(X); - it2 = MapO.find(Y); - if (it1 == MapO.end()) { MapO[X] = indO; namO[indO] = X; ind0++; } - if (it2 == MapO.end()) { MapO[Y] = indO; namO[indO] = Y; ind0++; } - - matO[MapO[X]][MapO[Y]] = min(D, matO[MapO[X]][MapO[Y]]); - if (UB == "B") - matO[MapO[Y]][MapO[X]] = min(D, matO[MapO[Y]][MapO[X]]); + S[i][i] = M[i][i] = 0; + } +} + +void Set(char p, char dr, char u, char v, int cost) +{ + int U, V; + U = u - 'A'; + V = v - 'A'; + if (p == 'M') + { + if (M[U][V] > cost) + { + M[U][V] = cost; + } + if (dr == 'B' && M[V][U] > cost) + { + M[V][U] = cost; + } + } + if (p == 'Y') + { + if (S[U][V] > cost) + { + S[U][V] = cost; + } + if (dr == 'B' && S[V][U] > cost) + { + S[V][U] = cost; + } + } +} + +void Floyd(int ss[][MX]) +{ + int i, j, k; + for (k = 0; k < 26; k++) + { + for (i = 0; i < 26; i++) + { + for (j = 0; j < 26; j++) + { + ss[i][j] = MIN(ss[i][j], ss[i][k] + ss[k][j]); } } - FOI(k, 0, indY-1) - FOI(i, 0, indY-1) - FOI(j, 0, indY-1) - matY[i][j] = min(matY[i][j], matY[i][k] + matY[k][j]); - FOI(k, 0, indO-1) - FOI(i, 0, indO-1) - FOI(j, 0, indO-1) - matO[i][j] = min(matO[i][j], matO[i][k] + matO[k][j]); - + } +} + +void Cal(int s, int m) +{ + int i, min = 900000, x, j = 0; + int dummy[30]; + Floyd(S); + Floyd(M); + for (i = 0; i < 26; i++) + { + x = S[s][i] + M[m][i]; + if (x < min) + { + dummy[0] = i; + j = 1; + min = x; + } + else if (x == min) + { + dummy[j++] = i; + } + } + if (j == 0) + { + printf("You will never meet.\n"); + return; + } + printf("%d", min); + for (i = 0; i < j; i++) + { + printf(" %c", dummy[i] + 'A'); + } + printf("\n"); +} + +int main() +{ + int n, m; + char p[3], d[3], u[3], v[3]; + while (scanf("%d", &n) && n) + { + Ini(); + while (n--) + { + scanf("%s%s%s%s%d", p, d, u, v, &m); + Set(p[0], d[0], u[0], v[0], m); + } + scanf("%s%s", p, d); + Cal(p[0] - 'A', d[0] - 'A'); } return 0; } diff --git a/10172.cpp b/10172.cpp new file mode 100644 index 0000000..c7d76e0 --- /dev/null +++ b/10172.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +queue queues[101]; + +bool job_done(int n) +{ + rep(i, n) + { + if (!queues[i].empty()) + { + return false; + } + } + return true; +} + +int main() +{ + int t, n, s, q, queue_size, tmp; + cin >> t; + while (t--) + { + cin >> n >> s >> q; + rep(i, n) + { + cin >> queue_size; + while (queue_size--) + { + cin >> tmp; + tmp--; + queues[i].push(tmp); + } + } + int result_time = 0; + int current_station = 0; + stack carrier; + while (true) + { + if (carrier.empty() && job_done(n)) + { + break; + } + // unload as much as possible + while (!carrier.empty()) + { + if (carrier.top() == current_station) + { + result_time += 1; + carrier.pop(); + } + else + { + if (queues[current_station].size() < q) + { + queues[current_station].push(carrier.top()); + carrier.pop(); + result_time += 1; + } + else + { + break; + } + } + } + // load as much as possible + while (carrier.size() < s) + { + if (queues[current_station].empty()) + { + break; + } + carrier.push(queues[current_station].front()); + queues[current_station].pop(); + result_time += 1; + } + current_station++; + if (current_station == n) + { + current_station = 0; + } + result_time += 2; + } + // last station change ign + result_time -= 2; + cout << result_time << endl; + } + return 0; +} diff --git a/10173.cpp b/10173.cpp new file mode 100644 index 0000000..fdf115f --- /dev/null +++ b/10173.cpp @@ -0,0 +1,281 @@ +#include + +using namespace std; + +typedef double db; +const db EPS = 1e-10; +const int maxn = 5005; +const db PI = acos(-1.0); +int sign(db x) +{ + if (fabs(x) < EPS) + { + return 0; + } + return x > 0 ? 1 : -1; +} +db sqr(db x) +{ + return x * x; +} +struct TPoint +{ + db x, y; + TPoint() {} + TPoint(db xx, db yy) : x(xx), y(yy) {} + TPoint sub(TPoint P) + { + return TPoint(x - P.x, y - P.y); + } + TPoint add(TPoint P) + { + return TPoint(x + P.x, y + P.y); + } + db X(TPoint P) + { + return x * P.y - y * P.x; + } + db dot(TPoint P) + { + return x * P.x + y * P.y; + } + db len() + { + return sqrt(sqr(x) + sqr(y)); + } + db disTo(TPoint P) + { + return sqrt(sqr(x - P.x) + sqr(y - P.y)); + } + void get() + { + scanf("%lf%lf", &x, &y); + } + void out() + { + printf("POS = (%.4f %.4f)\n", x, y); + } + bool operator<(const TPoint P) const + { + return sign(y - P.y) < 0 || (sign(y - P.y) == 0 && sign(x - P.x) < 0); + } + db X(TPoint a, TPoint b) + { + return (x - a.x) * (b.y - y) - (y - a.y) * (b.x - x); + } + TPoint rot(db c, db s) + { + return TPoint(x * c - y * s, x * s + y * c); + } +}; + +struct TLine +{ + db A, B, C;// Bx + Ay + C = 0... + TPoint a, b; + void init(TPoint aa, TPoint bb) + { + a = aa; + b = bb; + A = a.x - b.x; + B = b.y - a.y; + C = b.X(a); + } + bool inter(TLine L, TPoint &P) + { + TPoint AA(A, L.A), BB(B, L.B), CC(C, L.C); + db c = AA.X(BB); + if (!sign(c)) + { + return 0; + } + P = TPoint(CC.X(AA) / c, -CC.X(BB) / c); + return 1; + } +} vL[5]; + +db get_dot_ang(TPoint A, TPoint B) +{ + return A.dot(B) / 16.0 / B.len(); +} +db get_X_ang(TPoint A, TPoint B) +{ + return fabs(A.X(B) / 16.0 / B.len()); +} + +int n; +int sta[maxn]; +TPoint P[maxn], bP[maxn], TV[maxn]; +bool cmp(TPoint a, TPoint b) +{ + int flg = sign(P[0].X(a, b)); + return flg ? flg < 0 : sign(a.disTo(P[0]) - b.disTo(P[0])) < 0; +} +bool eq(TPoint a, TPoint b) +{ + return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0; +} +int TuBao(TPoint P[], int n) +{ + int top = -1, i; + sort(P, P + n); + sort(P + 1, P + n, cmp); + sta[++top] = 0; + sta[++top] = 1; + for (i = 2; i < n; ++i) + { + while (top && (sign(P[sta[top]].X(P[sta[top - 1]], P[i])) <= 0 || eq(P[sta[top]], P[sta[top - 1]]))) + { + --top; + } + if (!eq(P[i], P[sta[top]])) + { + sta[++top] = i; + } + } + return top + 1; +} + +bool get() +{ + scanf("%d", &n); + if (n == 0) + { + return 0; + } + int i; + for (i = 0; i < n; ++i) + { + P[i].get(); + } + return 1; +} + +db cal_poly_area(TPoint P[], int n) +{ + db ans = 0.0; + int i; + P[n] = P[0]; + for (i = 0; i < n; ++i) + { + ans += P[i].X(P[i + 1]); + } + return fabs(ans) * 0.5; +} + +void checkmin(db &x, db y) +{ + if (sign(x) < 0) + { + x = y; + } + else if (sign(y - x) < 0) + { + x = y; + } +} + +void work() +{ + int i, j, k, sz; + db ans = -1.0; + sz = TuBao(P, n); + n = sz; + for (i = 0; i < n; ++i) + { + bP[i] = P[sta[i]]; + } + bP[n] = bP[0]; + for (i = 0; i < n; ++i) + { + P[i] = bP[i]; + } + P[n] = P[0]; + if (n < 3) + { + puts("0.0000"); + return; + } + for (i = 0; i < n; ++i) + { + TV[i] = P[i + 1].sub(P[i]); + } + TPoint vec[4]; + int V[4]; + db theta[4]; + vec[0] = TPoint(-16.0, 0); + vec[1] = TPoint(0, -16.0); + vec[2] = TPoint(16.0, 0); + vec[3] = TPoint(0, 16.0); + for (i = 0; i < 4; ++i) + { + V[i] = 0; + } + for (i = 0; i < n; ++i) + { + if (sign(P[i].y - P[V[0]].y) > 0) + { + V[0] = i; + } + if (sign(P[i].x - P[V[1]].x) < 0) + { + V[1] = i; + } + if (sign(P[i].y - P[V[2]].y) < 0) + { + V[2] = i; + } + if (sign(P[i].x - P[V[3]].x) > 0) + { + V[3] = i; + } + } + for (i = 0; i <= n; ++i) + { + for (j = 0; j < 4; ++j) + { + vL[j].init(bP[V[j]], bP[V[j]].add(vec[j])); + } + int L = 0; + for (j = 0; j < 4; ++j) + for (k = j + 1; k < 4; ++k) + { + TPoint pp; + if (vL[j].inter(vL[k], pp)) + { + P[L++] = pp; + } + } + int ss = TuBao(P, L); /*P[] may be the smallest Bounding Rectangle*/ + checkmin(ans, cal_poly_area(P, ss)); + if (i == n) + { + break; + } + for (j = 0; j < 4; ++j) + { + theta[j] = get_dot_ang(vec[j], TV[V[j]]); + } + for (k = j = 0; j < 4; ++j) + if (sign(theta[j] - theta[k]) > 0) + { + k = j; + } + db c = theta[k], s = get_X_ang(vec[k], TV[V[k]]); + for (j = 0; j < 4; ++j) + { + vec[j] = vec[j].rot(c, s); + } + V[k] = (V[k] + 1) % n; + } + printf("%.4f\n", ans); +} + +int main() +{ + while (get()) + { + work(); + } + return 0; +} diff --git a/10174.cpp b/10174.cpp new file mode 100644 index 0000000..961ab82 --- /dev/null +++ b/10174.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +int N, M; + +void Single() +{ + if (N % 2) + { + printf("%.lf %.lf\n", ceil((double)N / 2), floor((double)N / 2)); + } + else if (N % 4 == 0) + { + N = N / 2; + printf("%d %d\n", N / 2 + 1, N / 2 - 1); + } + else + { + printf("Bachelor Number.\n"); + } +} +void Double() +{ + int Tm, Teven, Tb = 0, b = 0, Tdiv4; + Tm = M - N + 1; + if (Tm % 2) + { + b = 1; + } + Teven = Tm / 2; + if (N % 2 == 0) + { + Tb = Teven += b; + } + while (N % 4) + { + N++; + if (N > M) + { + goto done; + } + } + while (M % 4) + { + M--; + if (M < N) + { + goto done; + } + } + Tdiv4 = (M - N) / 4 + 1; + Tb = Teven - Tdiv4; +done:; + printf("%d\n", Tb); +} + +int main() +{ + char input[100]; + int k; + while (gets(input)) + { + k = sscanf(input, "%d%d", &N, &M); + if (k == 1) + { + Single(); + } + else + { + Double(); + } + } + return 0; +} diff --git a/10175.cpp b/10175.cpp new file mode 100644 index 0000000..a1e4c3f --- /dev/null +++ b/10175.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define sq(a) ((a) * (a)) +#define cq(a) (sq(a) * (a)) + +const double PI = 2 * acos(0); + +typedef double elem; +typedef complex point, vec; + +vec uvec(vec a) +{ + return a / abs(a); +} +vec normr(vec a) +{ + return a * vec(0, -1); +} +vec unormr(vec a) +{ + return uvec(normr(a)); +} + +inline elem CircleCrossX(elem x2, elem r1, elem r2) +{ + vec ab = point(x2, 0); + elem k = (sq(r1) - sq(r2) + sq(x2)) / (2 * sq(x2)); + point t = k * ab; + elem x = sqrt(sq(r1) - sq(x2 * k)); + vec abr = unormr(ab); + vec vp1 = x * abr; + return (vp1 + t).real(); +} + +inline elem SphereSurface(elem r, elem x) +{ + return 2 * PI * r * (x + r); +} +inline elem SphereVolume(elem r, elem x) +{ + return PI * ((2 / 3.0) * cq(r) + sq(r) * x - cq(x) / 3.0); +} + +int main() +{ + int n; + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + elem dr1, dr2, dd, dw, ds; + elem X, V, A; + scanf("%lf%lf%lf%lf%lf", &dr1, &dr2, &dd, &dw, &ds); + X = CircleCrossX(dd, dr1, dr2); + V = SphereVolume(dr1, X); + A = SphereSurface(dr1, X); + X = CircleCrossX(dd, dr2, dr1); + V += SphereVolume(dr2, X); + A += SphereSurface(dr2, X); + printf("%.4lf %.4lf\n", V, A); + if (dw - ds * V < 0) + { + puts("The Paired-Sphere Floats."); + } + else if (dw - ds * V > 0) + { + puts("The Paired-Sphere Sinks."); + } + } + return 0; +} diff --git a/10176.cpp b/10176.cpp new file mode 100644 index 0000000..d010fae --- /dev/null +++ b/10176.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int main() +{ + char b[10000], c; + long i, k = 0, n = 0, cary, trem, rem, p; + while ((c = getc(stdin)) != EOF) + { + if (c == '\n') + { + continue; + } + if (c != '#') + { + b[k++] = c; + } + if (c == '#') + { + if (b[k - 1] == '1') + { + cary = 1; + } + else + { + cary = 0; + } + for (i = k - 2; i >= 0; i--) + { + n++; + if (b[i] == '1') + { + p = n % 17; + rem = pow(2, p) + cary; + trem = rem % 131071; + cary = trem; + } + } + if (cary == 0) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + k = 0; + n = 0; + } + } + return 0; +} diff --git a/10177.cpp b/10177.cpp new file mode 100644 index 0000000..ed3d29f --- /dev/null +++ b/10177.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define MAXN 101 + +long long Store[3][MAXN], RStore[3][MAXN]; + +void Gen_Sq() +{ + int i, j, k = 1; + long long Sum; + for (i = 0; i < 3; i++) + { + Sum = 0; + k++; + for (j = 1; j <= 100; j++) + { + Sum += pow(j, k); + Store[i][j] = Sum; + } + } +} +void Gen_Rec() +{ + int i, j, k; + int p, q, n = 1; + for (i = 0; i < 3; i++) + { + n++; + for (j = 2; j <= 100; j++) + { + k = j - 1; + p = k + 1; + q = k + 2; + if (p % 2 == 0) + { + p /= 2; + } + else + { + q /= 2; + } + RStore[i][j] = pow((p * q), n) - Store[i][j]; + } + } +} + +int main() +{ + int n; + Gen_Sq(); + Gen_Rec(); + while (scanf("%d", &n) == 1) + { + printf("%lld %lld %lld %lld %lld %lld\n", Store[0][n], RStore[0][n], + Store[1][n], RStore[1][n], Store[2][n], RStore[2][n]); + } + return 0; +} diff --git a/10178.cpp b/10178.cpp new file mode 100644 index 0000000..b75f463 --- /dev/null +++ b/10178.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + int numFaces(); + + int faces, size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->faces = 1; + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + faces++; + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int UnionFindDisjointSets::numFaces() +{ + return faces; +} + +int main() +{ + int N, E; + char a, b; + while (cin >> N >> E) + { + UnionFindDisjointSets ds(10 * N); + while (E--) + { + cin >> a >> b; + ds.unionSet(int(a - 'A'), int(b - 'A')); + } + cout << ds.numFaces() << endl; + } + return 0; +} diff --git a/10179.cpp b/10179.cpp new file mode 100644 index 0000000..b3ac21f --- /dev/null +++ b/10179.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int phi(int n) +{ + int result = n; + if (n % 2 == 0) + { + while (n % 2 == 0) + { + n /= 2; + } + result -= result / 2; + } + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + while (n % i == 0) + { + n /= i; + } + result -= result / i; + } + } + if (n > 1) + { + result -= result / n; + } + return result; +} + +int main() +{ + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + cout << phi(n) << endl; + } + return 0; +} diff --git a/1018.cpp b/1018.cpp new file mode 100644 index 0000000..86df96d --- /dev/null +++ b/1018.cpp @@ -0,0 +1,232 @@ +#include + +using namespace std; + +int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}, + dy[] = {-1, 1, 0, -1, 1, 0, 1, -1}; + +struct E +{ + int x, y, w; +}; +E lst[25 * 1000000 + 25]; + +char grid[105][105]; +int r, c, col[105][105], set_[10005], edge[5005][5005]; + +bool cmp(E a, E b) +{ + return a.w < b.w; +} + +void color(int x, int y, int cl) +{ + col[x][y] = cl; + int xx, yy, i; + for (i = 0; i < 8; i++) + { + xx = x + dx[i]; + yy = y + dy[i]; + if (xx < 0 || yy < 0 || xx > r - 1 || yy > c - 1) + { + continue; + } + if (grid[xx][yy] != '#' || col[xx][yy] != 0) + { + continue; + } + color(xx, yy, cl); + } +} + +int getparent(int i) +{ + if (set_[i] == i) + { + return i; + } + else + { + return set_[i] = getparent(set_[i]); + } +} +int isunion(int a, int b) +{ + return getparent(a) == getparent(b); +} +void makeunion(int a, int b) +{ + set_[getparent(a)] = getparent(b); +} + +int main() +{ + int t = 0, clr; + while (scanf("%d%d", &r, &c) == 2) + { + if (r == 0 && c == 0) + { + return 0; + } + for (int i = 0; i < r; i++) + { + scanf("%s", &grid[i]); + } + if (t) + { + printf("\n"); + } + printf("City %d\n", ++t); + + memset(col, 0, sizeof(col)); + + clr = 0; + + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + if (grid[i][j] == '#' && col[i][j] == 0) + { + ++clr; + color(i, j, clr); + } + } + } + + if (clr <= 1) + { + printf("No bridges are needed.\n"); + continue; + } + for (int i = 1; i <= clr; i++) + { + for (int j = 1; j <= clr; j++) + { + if (i == j) + { + edge[i][j] = 0; + } + else + { + edge[i][j] = 2000; + } + } + } + + // build edges + int a, b; + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + if (grid[i][j] == '.') + { + continue; + } + // along right + for (int k = j + 2; k < c; k++) + { + if (i - 1 >= 0 && grid[i - 1][k] == '#' && col[i - 1][k] != col[i][j]) + { + a = col[i - 1][k]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - j - 1); + edge[b][a] = min(edge[b][a], k - j - 1); + } + if (grid[i][k] == '#' && col[i][k] != col[i][j]) + { + a = col[i][k]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - j - 1); + edge[b][a] = min(edge[b][a], k - j - 1); + } + if (i + 1 < r && grid[i + 1][k] == '#' && col[i + 1][k] != col[i][j]) + { + a = col[i + 1][k]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - j - 1); + edge[b][a] = min(edge[b][a], k - j - 1); + } + } + // along downward + for (int k = i + 2; k < r; k++) + { + if (j - 1 >= 0 && grid[k][j - 1] == '#' && col[k][j - 1] != col[i][j]) + { + a = col[k][j - 1]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - i - 1); + edge[b][a] = min(edge[b][a], k - i - 1); + } + if (grid[k][j] == '#' && col[k][j] != col[i][j]) + { + a = col[k][j]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - i - 1); + edge[b][a] = min(edge[b][a], k - i - 1); + } + if (j + 1 < c && grid[k][j + 1] == '#' && col[k][j + 1] != col[i][j]) + { + a = col[k][j + 1]; + b = col[i][j]; + edge[a][b] = min(edge[a][b], k - i - 1); + edge[b][a] = min(edge[b][a], k - i - 1); + } + } + } + } + int num = 0; + for (int i = 1; i <= clr; i++) + { + for (int j = i + 1; j <= clr; j++) + { + E ed; + ed.x = i; + ed.y = j; + ed.w = min(edge[i][j], edge[j][i]); + if (ed.w != 2000) + { + lst[num++] = ed; + } + } + } + + sort(lst, lst + num, cmp); + for (int i = 1; i <= clr; i++) + { + set_[i] = i; + } + int cnt = 0, cost = 0; + for (int i = 0; i < num && cnt <= clr - 1; i++) + { + a = lst[i].x; + b = lst[i].y; + if (!isunion(a, b)) + { + makeunion(a, b); + ++cnt; + cost += lst[i].w; + } + } + + if (cnt == 0) + { + printf("No bridges are possible.\n", cost); + } + else if (cnt == 1) + { + printf("1 bridge of total length %d\n", cost); + } + else + { + printf("%d bridges of total length %d\n", cnt, cost); + } + int disc = clr - cnt; + if (disc > 1) + { + printf("%d disconnected groups\n", disc); + } + } + return 0; +} diff --git a/10180.cpp b/10180.cpp new file mode 100644 index 0000000..140b78a --- /dev/null +++ b/10180.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +const double PI = 2 * acos(0.0); + +int main() +{ + int n; + scanf("%d", &n); + while (n--) + { + double x1, y1, x2, y2, r; + scanf("%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &r); + // ax + by + c = 0 + // a = (y2 - y1) / (x2 - x1) + // b = -1 + // c = y1 - ax1 + double dx = x2 - x1, dy = y2 - y1; + if (dx == 0.0) // can't get slope(infinity) + { + if (dy == 0.0) + { + printf("%.3lf\n", 0.0); + continue; + } + double yg = y1, yl = y2; + if (y1 < y2) + { + yg = y2; + yl = y1; + } + if ((x1 < r && x1 > -r) && (yg > 0.0 && yl < 0.0)) + { + double d1 = sqrt(x1 * x1 + y1 * y1), d2 = sqrt(x2 * x2 + y2 * y2), d3 = sqrt(dx * dx + dy * dy); + double a; + if (x1 == 0.0) + { + a = PI; + } + else + { + a = acos((d1 * d1 + d2 * d2 - d3 * d3) / (2.0 * d1 * d2)); + } + double a1 = acos(r / d1), a2 = acos(r / d2); + double theta = a - (a1 + a2); + printf("%.3lf\n", r * theta + sqrt(d1 * d1 - r * r) + sqrt(d2 * d2 - r * r)); + } + else + { + printf("%.3lf\n", yg - yl); + } + } + else + { + // ax - y + c = 0, x^2 + y^2 = r^2 + // -> (a^2 + 1)x^2 + (2ac)x + (c^2 - r^2) = 0 + // + // (a)x^2 + (b)x + (c) = 0 + // d = b^2 - 4ac + // x = -b + sqrt(d) / 2a, -b - sqrt(d) / 2a + double a = dy / dx, c = y1 - a * x1; + double aSq = a * a, cSq = c * c, rSq = r * r; + double d = 4.0 * (aSq * cSq - (aSq + 1.0) * (cSq - rSq));// discriminant + if (d > 0.0) + { + double x = (-(2.0 * a * c) + sqrt(d)) / (2.0 * (aSq + 1.0)); + double xg = x1, xl = x2; + if (x1 < x2) + { + xg = x2; + xl = x1; + } + if (x >= xl && x <= xg) + { + double d1 = sqrt(x1 * x1 + y1 * y1), d2 = sqrt(x2 * x2 + y2 * y2), d3 = sqrt(dx * dx + dy * dy); + double a; + if (c == 0.0) + { + a = PI; + } + else + { + a = acos((d1 * d1 + d2 * d2 - d3 * d3) / (2.0 * d1 * d2)); + } + double a1 = acos(r / d1), a2 = acos(r / d2); + double theta = a - (a1 + a2); + printf("%.3lf\n", r * theta + sqrt(d1 * d1 - rSq) + sqrt(d2 * d2 - rSq)); + } + else + { + printf("%.3lf\n", sqrt(dx * dx + dy * dy)); + } + } + else + { + printf("%.3lf\n", sqrt(dx * dx + dy * dy)); + } + } + } + return 0; +} diff --git a/10181.cpp b/10181.cpp new file mode 100644 index 0000000..8ab167c --- /dev/null +++ b/10181.cpp @@ -0,0 +1,190 @@ +#include + +using namespace std; + +#define LEN 4 +#define IDA_MAX 1000 +#define MAX 100 +#define BASE 6731 + +const char direct[5] = "URLD"; +const int step[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}}; +const int answer[LEN][LEN] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}}; + +bool flag; +int rev[LEN * LEN][2], pre[LEN * LEN][LEN][LEN], map_[LEN][LEN], ans[100], c1, c2, anscnt; + +bool valid(int x, int y) +{ + return x >= 0 && y >= 0 && x < LEN && y < LEN; +} + +int abs(int p) +{ + if (p < 0) + { + return -p; + } + return p; +} +int Heuristic(int map_[LEN][LEN]) +{ + bool found = 0; + int p1, q1, p2, q2, x = 0; + for (p1 = 0; p1 < LEN; p1++) + { + for (q1 = 0; q1 < LEN; q1++) + { + if (answer[p1][q1]) + { + for (p2 = 0, found = 0; p2 < LEN; p2++) + { + for (q2 = 0; q2 < LEN; q2++) + { + if (map_[p2][q2] == answer[p1][q1]) + { + x += (abs(p1 - p2) + abs(q1 - q2)); + found = 1; + break; + } + } + if (found) + { + break; + } + } + } + } + } + return x; +} + +int IDA_DFS(int bound, int depth, int nscore, int nx, int ny) +{ + if (nscore > bound) + { + return nscore; + } + if (nscore - depth + 1 == 0) + { + flag = 1; + return -1; + } + int k, next = IDA_MAX, tmp, tx, ty, tscore; + for (k = 0; k < 4; k++) + { + tx = nx + step[k][0], ty = ny + step[k][1]; + if (!valid(tx, ty)) + { + continue; + } + if (k + ans[anscnt - 1] == 3) + { + continue; + } + tmp = map_[tx][ty], map_[nx][ny] = map_[tx][ty], map_[tx][ty] = 0; + tscore = nscore - pre[tmp][tx][ty] + pre[tmp][nx][ny] + 1; + ans[anscnt++] = k; + tmp = IDA_DFS(bound, depth + 1, tscore, tx, ty); + if (flag) + { + return -1; + } + if (tmp < next) + { + next = tmp; + } + map_[tx][ty] = map_[nx][ny], map_[nx][ny] = 0; + anscnt--; + } + return next; +} + +int main() +{ + int i, j, k, t, num, cnt, a, b, c, d, bound, x, y, score; + for (k = 0; k < LEN * LEN; k++) + { + for (i = 0; i < LEN; i++) + { + for (j = 0; j < LEN; j++) + { + if (answer[i][j] == k) + { + rev[k][0] = i, rev[k][1] = j; + } + } + } + } + for (k = 0; k < LEN * LEN; k++) + { + for (i = 0; i < LEN; i++) + { + for (j = 0; j < LEN; j++) + { + pre[k][i][j] = abs(i - rev[k][0]) + abs(j - rev[k][1]); + } + } + } + scanf("%ld", &cnt), ans[0] = -1; + for (t = 0; t < cnt; t++) + { + for (i = 0; i < LEN; i++) + { + for (j = 0; j < LEN; j++) + { + scanf("%d", &map_[i][j]); + } + } + for (i = 0; i < LEN; i++) + { + for (j = 0; j < LEN; j++) + { + if (map_[i][j] == 0) + { + x = i, y = j, map_[i][j] = 16; + } + } + } + score = Heuristic(map_); + for (a = 0, k = x + y; a < LEN; a++) + { + for (b = 0; b < LEN; b++) + { + for (c = 0; c < LEN; c++) + { + for (d = 0; d < LEN; d++) + { + if (((a * LEN + b) < (c * LEN + d)) && map_[a][b] > map_[c][d]) + { + k++; + } + } + } + } + } + map_[x][y] = 0; + if (score == 0) + { + printf("\n"); + continue; + } + else if (k % 2 == 1) + { + printf("This puzzle is not solvable.\n"); + continue; + } + bound = 46; + anscnt = 1; + for (flag = 0; !flag;) + { + bound = IDA_DFS(bound, 1, score, x, y); + } + for (i = 1; i < anscnt; i++) + { + printf("%c", direct[ans[i]]); + } + printf("\n"); + } + return 0; +} diff --git a/10182.cpp b/10182.cpp new file mode 100644 index 0000000..3893871 --- /dev/null +++ b/10182.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +#define NHONEYCOMB 100000 + +int maja_x, maja_y; // maja system coordinates +int step; // current step +int comb_x[NHONEYCOMB + 1]; // honey comb x coordinates +int comb_y[NHONEYCOMB + 1]; // honey comb y coordinates + +void walk() +{ + comb_x[step] = maja_x; + comb_y[step] = maja_y; + step++; +} + +void walk_south(int rows) +{ + while (rows--) + { + maja_y++; + walk(); + } +} + +void walk_north(int rows) +{ + while (rows--) + { + maja_y--; + walk(); + } +} + +void walk_southeast(int rows) +{ + while (rows--) + { + maja_x++; + walk(); + } +} + +void walk_southwest(int rows) +{ + while (rows--) + { + maja_x--; + maja_y++; + walk(); + } +} + +void walk_northwest(int rows) +{ + while (rows--) + { + maja_x--; + walk(); + } +} + +void walk_northeast(int rows) +{ + while (rows--) + { + maja_x++; + maja_y--; + walk(); + } +} + +int main() +{ + int honeycomb; + step = 1; + maja_x = 0; + maja_y = -1; + // pre-compute + walk_south(2); + for (int i = 1; step < NHONEYCOMB / 10; i++) + { + walk_northwest(i); + walk_north(i); + walk_northeast(i); + walk_southeast(i); + walk_south(i + 1); + walk_southwest(i); + } + while (cin >> honeycomb) + { + cout << comb_x[honeycomb] << " " << comb_y[honeycomb] << endl; + } + return 0; +} diff --git a/10183.cpp b/10183.cpp new file mode 100644 index 0000000..09a161a --- /dev/null +++ b/10183.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int fibonacci[500][110]; + +void get_fibonacci() +{ + memset(fibonacci, 0, sizeof(fibonacci)); + fibonacci[0][0] = 1; + fibonacci[1][0] = 1; + fibonacci[1][1] = 1; + int i, j, jinwei, tmp; + for (i = 2; i < 500; i++) + { + jinwei = 0; + fibonacci[i][0] = fibonacci[i - 1][0]; + for (j = 1; j <= fibonacci[i - 1][0]; j++) + { + tmp = fibonacci[i - 2][j] + fibonacci[i - 1][j] + jinwei; + if (tmp > 9) + { + fibonacci[i][j] = tmp % 10; + jinwei = 1; + } + else + { + jinwei = 0; + fibonacci[i][j] = tmp % 10; + } + } + if (jinwei == 1) + { + fibonacci[i][j] = 1; + fibonacci[i][0]++; + } + } +} + +int compare(int *a, int *b) /*return 1 if a>b*/ +{ + if (a[0] > b[0]) + { + return 1; + } + if (a[0] < b[0]) + { + return -1; + } + int i; + for (i = a[0]; i > 0; i--) + { + if (a[i] > b[i]) + { + return 1; + } + if (a[i] < b[i]) + { + return -1; + } + } + return 0; +} + +int main() +{ + get_fibonacci(); + char pa[110], pb[110]; + int a[110], b[110], i, j; + while (cin >> pa >> pb) + { + if (!strcmp(pa, "0") && !strcmp(pb, "0")) + { + break; + } + //get a and b; + a[0] = strlen(pa); + for (i = 0, j = a[0]; i < a[0]; i++, j--) + { + a[j] = pa[i] - '0'; + } + b[0] = strlen(pb); + for (i = 0, j = b[0]; i < b[0]; i++, j--) + { + b[j] = pb[i] - '0'; + } + // + int cnt = 0; + for (i = 2; i < 500; i++) + { + if (compare(fibonacci[i], b) == 1) + { + break; + } + if (compare(fibonacci[i], a) != -1) + { + cnt++; + } + } + cout << cnt << endl; + } + return 0; +} diff --git a/10184.cpp b/10184.cpp new file mode 100644 index 0000000..40ba18a --- /dev/null +++ b/10184.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +typedef struct A +{ + double x, y, z; +} AA; +AA a[105]; +map MAP; + +int main() +{ + int i, j, all = 0, i1, i2, i3; + double lat, lng; + string s1, s2, s3; + MAP.clear(); + while (cin >> s1) + { + if (s1 == "#") + { + break; + } + MAP[s1] = all; + cin >> lat >> lng; + lat = lat * acos(-1) / 180; + lng = lng * acos(-1) / 180; + a[all].x = (double)6378 * cos(lat) * cos(lng); + a[all].y = (double)6378 * cos(lat) * sin(lng); + a[all].z = (double)6378 * sin(lat); + all++; + } + while (cin >> s1) + { + if (s1 == "#") + { + break; + } + cin >> s2 >> s3; + if (MAP.find(s1) == MAP.end() || MAP.find(s2) == MAP.end() || MAP.find(s3) == MAP.end()) + { + cout << s3 << " is ? km off " << s1 << "/" << s2 << " equidistance." << endl; + } + else + { + i1 = MAP[s1]; + i2 = MAP[s2]; + i3 = MAP[s3]; + if (a[i1].x == a[i2].x && a[i1].y == a[i2].y && a[i1].z == a[i2].z) + { + cout << s3 << " is 0 km off " << s1 << "/" << s2 << " equidistance." << endl; + } + else + { + double dx = a[i1].x - a[i2].x; + double dy = a[i1].y - a[i2].y; + double dz = a[i1].z - a[i2].z; + // inner product + double child = dx * a[i3].x + dy * a[i3].y + dz * a[i3].z; + double mom = sqrt(dx * dx + dy * dy + dz * dz) * sqrt(a[i3].x * a[i3].x + a[i3].y * a[i3].y + a[i3].z * a[i3].z); + double final = fabs(child / mom); + final = acos(final); + final = acos(-1) / 2 - final; + final = final * 6378; + cout << s3 << " is " << floor(final + 0.5) << " km off " << s1 << "/" << s2 << " equidistance." << endl; + } + } + } + return 0; +} diff --git a/10185.cpp b/10185.cpp new file mode 100644 index 0000000..174a625 --- /dev/null +++ b/10185.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +char s[1030][1005], final[1030]; +int SET[2055]; + +int main() +{ + int N, n, M, m, i, j; + while (scanf(" %d %d", &N, &M) == 2 && (N + M)) + { + for (n = 0; n < N; n++) + { + scanf(" %s", s[n]); + } + int ans = 0, N2 = N * 2; + for (m = 0; m < M; m++) + { + for (n = N; n < N2; n++) + { + SET[n] = (1 << (s[n - N][m] - 'A')); + } + for (n = N - 1; n > 0; n--) + { + SET[n] = SET[n << 1] & SET[n << 1 | 1]; + if (SET[n] == 0) + { + ans++; + SET[n] = SET[n << 1] | SET[n << 1 | 1]; + } + } + for (i = 0; i < 26; i++) + { + if (SET[1] & (1 << i)) + { + final[m] = i + 'A'; + break; + } + } + } + final[m] = '\0'; + printf("%s %d\n", final, ans); + } + return 0; +} diff --git a/10186.cpp b/10186.cpp new file mode 100644 index 0000000..c0c1260 --- /dev/null +++ b/10186.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +int n, o, highest[21], lowest[21], game[21][21][2], num[21][21], point[21], lst[21][2]; +char name[21][31]; + +int find(char *s) +{ + for (int i = 0; i < n; i++) + if (!strcmp(s, name[i])) + return i; + return -1; +} + +void dfs(int depth) +{ + int i, j, p, q, high, low; + if (depth == o) + { + for (i = 0; i < n; i++) + { + for (j = 0, high = n, low = 1; j < n; j++) + { + if (i != j) + { + if (point[i] > point[j]) + { + high--; + } + else if (point[i] == point[j]) + { + high--; + low++; + } + else + { + low++; + } + } + } + if (high < highest[i]) + { + highest[i] = high; + } + if (low > lowest[i]) + { + lowest[i] = low; + } + } + return; + } + p = lst[depth][0]; + q = lst[depth][1]; + + game[p][q][num[p][q]++] = 1; + game[q][p][num[q][p]++] = 0; + point[p] += 3; + dfs(depth + 1); + num[p][q]--; + num[q][p]--; + point[p] -= 3; + + game[p][q][num[p][q]++] = 0; + game[q][p][num[q][p]++] = 1; + point[q] += 3; + dfs(depth + 1); + num[p][q]--; + num[q][p]--; + point[q] -= 3; + + game[p][q][num[p][q]++] = 2; + game[q][p][num[q][p]++] = 2; + point[p]++; + point[q]++; + dfs(depth + 1); + num[p][q]--; + num[q][p]--; + + point[p]--; + point[q]--; +} + +int main() +{ + int cas, i, j, k, m, g1, g2, i1, i2; + char s1[100], s2[100]; + cas = 0; + while (scanf("%d", &n) == 1) + { + if (!n) + break; + for (i = 0; i < n; i++) + { + scanf("%s", name[i]); + highest[i] = n; + lowest[i] = 0; + } + memset(num, 0, sizeof(num)); + memset(point, 0, sizeof(point)); + scanf("%d", &m); + while (m--) + { + scanf("%s%s%d%d", s1, s2, &g1, &g2); + i1 = find(s1); + i2 = find(s2); + if (g1 > g2) + { + game[i1][i2][num[i1][i2]++] = 1; + game[i2][i1][num[i2][i1]++] = 0; + point[i1] += 3; + } + else if (g1 == g2) + { + game[i1][i2][num[i1][i2]++] = game[i2][i1][num[i2][i1]++] = 2; + point[i1]++; + point[i2]++; + } + else + { + game[i1][i2][num[i1][i2]++] = 0; + game[i2][i1][num[i2][i1]++] = 1; + point[i2] += 3; + } + } + for (i = 1, o = 0; i < n; i++) + { + for (j = 0; j < i; j++) + { + if (num[i][j] < 2) + { + for (k = num[i][j]; k < 2; k++) + { + lst[o][0] = i; + lst[o++][1] = j; + } + } + } + } + dfs(0); + printf("Group #%d\n", ++cas); + for (i = 0; i < n; i++) + { + printf("%s %d-%d\n", name[i], highest[i], lowest[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10187.cpp b/10187.cpp new file mode 100644 index 0000000..e3fb1d0 --- /dev/null +++ b/10187.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 100; +const int inf = (1 << 21); +const int M = 25; + +class state +{ +public: + int now, time, cost; + bool needcost(int dep) + { + if (time <= dep) + { + if (time <= 12 && 12 <= dep) + { + return true; + } + } + else + { + if (time >= 18 && dep <= 6) + { + return false; + } + return true; + } + return false; + } + bool operator<(const state &a) const + { + return cost > a.cost; + } +}; +class Edge +{ +public: + int dep, arr, to; + bool isvalid() + { + if (!(18 <= dep || dep <= 6) || !(18 <= arr || arr <= 6)) + { + return false; + } + return true; + } +}; +vector edge[N]; +int cost[N][M]; +int dijkstra(int n, int s, int d) +{ + if (s == d) + { + return 0; + } + rep(i, n) rep(j, M) cost[i][j] = inf; + priority_queue Q; + rep(i, 7) Q.push((state){ + s, i, 0}); + REP(i, 18, 25) + Q.push((state){ + s, i, 0}); + while (!Q.empty()) + { + state now = Q.top(); + Q.pop(); + if (cost[now.now][now.time] != inf) + { + continue; + } + cost[now.now][now.time] = now.cost; + if (now.now == d) + { + return now.cost; + } + rep(i, edge[now.now].size()) + { + int nec = now.cost; + if (now.needcost(edge[now.now][i].dep)) + { + nec++; + } + Q.push((state){ + edge[now.now][i].to, edge[now.now][i].arr, nec}); + } + } + return -1; +} +int getname(string in, map &M) +{ + int index = M.size(); + if (M.find(in) == M.end()) + { + M[in] = index; + } + return M[in]; +} +int main() +{ + int te, tc = 1; + cin >> te; + while (te--) + { + int n; + map M; + cin >> n; + rep(i, 100) edge[i].clear(); + rep(i, n) + { + Edge in; + string from, to; + int f; + cin >> from >> to; + f = getname(from, M); + in.to = getname(to, M); + cin >> in.dep >> in.arr; + if (in.arr > 12) + { + continue; + } + if (in.arr == 12 && in.dep == 6) + { + continue; + } + in.arr = (in.dep + in.arr) % 24; + in.dep %= 24; + if (in.isvalid()) + { + edge[f].push_back(in); + } + //,cout << from <<" " << to <<" " << in.dep <<" " << in.arr << endl; + } + cout << "Test Case " << tc++ << "." << endl; + string start, dest; + cin >> start >> dest; + if (M.find(start) == M.end() || M.find(dest) == M.end()) + { + if (start == dest) + { + cout << "Vladimir needs " << 0 << " litre(s) of blood." << endl; + } + else + { + cout << "There is no route Vladimir can take." << endl; + } + continue; + } + int s = getname(start, M), d = getname(dest, M); + int ans = dijkstra(M.size(), s, d); + if (ans == -1) + { + cout << "There is no route Vladimir can take." << endl; + } + else + { + cout << "Vladimir needs " << ans << " litre(s) of blood." << endl; + } + } + return 0; +} diff --git a/10188.cpp b/10188.cpp new file mode 100644 index 0000000..f8be63d --- /dev/null +++ b/10188.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +string J, nJ, O, nO; +int N; + +void AddJ(string ss) +{ + int i; + ss += "\n"; + J += ss; + for (i = 0; i < ss.length(); i++) + { + if (isdigit(ss[i])) + { + nJ += ss[i]; + } + } +} + +void AddO(string ss) +{ + int i; + ss += "\n"; + O += ss; + for (i = 0; i < ss.length(); i++) + { + if (isdigit(ss[i])) + { + nO += ss[i]; + } + } +} + +void Cal() +{ + if (J.compare(O) == 0) + { + cout << "Accepted" << endl; + return; + } + if (nJ.compare(nO) == 0) + { + cout << "Presentation Error" << endl; + return; + } + cout << "Wrong Answer" << endl; +} + +int main() +{ + char ss[200]; + int r = 1; + while (1) + { + gets(ss); + N = atoi(ss); + if (!N) + { + break; + } + J = O = ""; + nJ = nO = ""; + while (N--) + { + gets(ss); + AddJ(ss); + } + gets(ss); + N = atoi(ss); + while (N--) + { + gets(ss); + AddO(ss); + } + cout << "Run #" << r++ << ": "; + Cal(); + } + return 0; +} diff --git a/10189.cpp b/10189.cpp index d315320..e7ce455 100644 --- a/10189.cpp +++ b/10189.cpp @@ -1,51 +1,85 @@ -#include -#include -using namespace std; -int main(){ - int i=0,j=0,R,C,no=0,cnt=0; - char ele[100][100]; - for(no=1;;no++){ - cin>>R>>C; - if(R==0 && C==0) - break; - if(no!=1) - cout<>ele[i][j]; - } - for(i=0;i + +using namespace std; + +char square[110][110]; +int n, m; + +bool isInTheSquare(int x, int y) +{ + if (x < 0 || x >= n) + { + return false; + } + if (y < 0 || y >= m) + { + return false; + } + return true; +} + +int main() +{ + int kase = 1; + int i, j; + int f = 0; + while (cin >> n >> m && n) + { + if (f) + { + cout << endl; + } + f = 1; + for (i = 0; i < n; i++) + { + cin >> square[i]; + } + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + { + if (square[i][j] == '.') + { + int cnt = 0; + if (isInTheSquare(i - 1, j - 1) && square[i - 1][j - 1] == '*') + { + cnt++; + } + if (isInTheSquare(i - 1, j) && square[i - 1][j] == '*') + { + cnt++; + } + if (isInTheSquare(i - 1, j + 1) && square[i - 1][j + 1] == '*') + { + cnt++; + } + if (isInTheSquare(i, j - 1) && square[i][j - 1] == '*') + { + cnt++; + } + if (isInTheSquare(i, j + 1) && square[i][j + 1] == '*') + { + cnt++; + } + if (isInTheSquare(i + 1, j - 1) && square[i + 1][j - 1] == '*') + { + cnt++; + } + if (isInTheSquare(i + 1, j) && square[i + 1][j] == '*') + { + cnt++; + } + if (isInTheSquare(i + 1, j + 1) && square[i + 1][j + 1] == '*') + { + cnt++; + } + square[i][j] = '0' + cnt; + } + } + cout << "Field #" << kase++ << ":" << endl; + for (i = 0; i < n; i++) + { + cout << square[i] << endl; + } + } + return 0; +} diff --git a/10190.cpp b/10190.cpp new file mode 100644 index 0000000..60fefc4 --- /dev/null +++ b/10190.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int main() +{ + double n, m, a[100], num; + int k, i; + while (scanf("%lf%lf", &n, &m) == 2) + { + k = 1; + num = 1; + if (fmod(n, m) || m == 1) + { + printf("Boring!"); + } + else + { + while (num < n) + { + num = num * m; + a[k] = num; + k++; + } + a[0] = 1; + if (num == n) + { + printf("%.0lf", a[k - 1]); + for (i = k - 2; i >= 0; i--) + { + printf(" %.0lf", a[i]); + } + } + else + { + printf("Boring!"); + } + } + printf("\n"); + } + return 0; +} diff --git a/10191.cpp b/10191.cpp new file mode 100644 index 0000000..6b194cb --- /dev/null +++ b/10191.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define MAXN 1090 + +char fg[MAXN][MAXN]; + +void Mark(char aa[], char bb[]) +{ + int i, st, end, m, h; + aa[2] = ' '; + sscanf(aa, "%d%d", &h, &m); + st = h * 60 + m; + bb[2] = ' '; + sscanf(bb, "%d%d", &h, &m); + end = h * 60 + m; + for (i = st; i < end; i++) + { + fg[i][i + 1] = 1; + } +} + +void Sep(char ss[]) +{ + char *p, aa[20], bb[20]; + p = strtok(ss, " "); + strcpy(aa, p); + p = strtok(NULL, " "); + strcpy(bb, p); + Mark(aa, bb); +} + +void Cal() +{ + int i, max = 0, st, end, j; + for (i = 600; i < 1080;) + { + if (fg[i][i + 1] == 1) + { + i++; + continue; + } + else + { + j = 0; + end = i; + while (fg[i][i + 1] == 0 && i < 1080) + { + j++; + i++; + } + if (j > max) + { + st = end; + max = j; + } + } + } + printf("the longest nap starts at %d:%02d and will last for ", st / 60, st % 60); + if (max < 60) + printf("%d minutes.\n", max); + else + printf("%d hours and %d minutes.\n", max / 60, max % 60); +} + +void Free() +{ + int i; + for (i = 600; i <= 1080; i++) + { + fg[i][i + 1] = 0; + } +} + +int main() +{ + char input[500]; + int k, f = 1; + while (gets(input)) + { + sscanf(input, "%d", &k); + while (k--) + { + gets(input); + Sep(input); + } + printf("Day #%d: ", f++); + Cal(); + Free(); + } + return 0; +} diff --git a/10192.cpp b/10192.cpp index 6c02325..a64fffe 100644 --- a/10192.cpp +++ b/10192.cpp @@ -1,30 +1,42 @@ -#include -using namespace std; - -int main(){ - string row, col; - for(int test=1; ; test++){ - getline(cin,row); - if(row=="#") - break; - getline(cin,col); - int m, n; - m=row.length(); - n=col.length(); - int i, j; - int tower[m+1][n+1]; - for(i=0; i + +using namespace std; + +const int MAXSIZE = 105; + +char line1[MAXSIZE + 5], line2[MAXSIZE + 5]; +int dp[MAXSIZE + 5][MAXSIZE + 5]; + +int main() +{ + int kase = 0; + while (cin.getline(&line1[1], MAXSIZE) && line1[1] != '#') + { + cin.getline(&line2[1], MAXSIZE); + int len1 = strlen(line1 + 1); + int len2 = strlen(line2 + 1); + int i, j; + for (i = 0; i <= len1; i++) + { + dp[i][0] = 0; + } + for (j = 0; j <= len2; j++) + { + dp[0][j] = 0; + } + for (i = 1; i <= len1; i++) + for (j = 1; j <= len2; j++) + { + if (line1[i] == line2[j]) + { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + cout << "Case #" << ++kase << ": you can visit at most " << dp[len1][len2] << " cities." << endl; + } + return 0; +} diff --git a/10193.cpp b/10193.cpp index ca372cf..9ac2e32 100644 --- a/10193.cpp +++ b/10193.cpp @@ -1,68 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int binToDec(string str){ - int LEN = str.length()-1, i, mul=1; - int val = 0; - FOD(i, LEN, 0){ - val += (str[i]-'0')*mul; - mul *= 2; - } - return val; -} - -int gcd(int A, int B){ - if(A < B) - return gcd(B, A); - if(B==0) - return A; - return gcd(B, A%B); -} -int main(){ - int test, t; - cin>>test; - FOI(t, 1, test){ - string S1, S2; - cin>>S1>>S2; - int V1 = binToDec(S1); - int V2 = binToDec(S2); - int V = gcd(V1, V2); - if(V > 1) - cout<<"Pair #"< + +using namespace std; + +int bin2dec(const string &s) +{ + int counter = 0; + int result = 0; + for (int i = s.size() - 1; i >= 0; i--) + { + if (s[i] == '1') + { + result += (int)pow(2, counter); + } + counter++; + } + return result; +} + +int gcd(int a, int b) +{ + while (b) + { + a %= b; + swap(a, b); + } + return a; +} + +int main() +{ + int n, a, b; + string input; + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> input; + a = bin2dec(input); + cin >> input; + b = bin2dec(input); + cout << "Pair #" << i + 1 << ": "; + if (gcd(a, b) > 1) + { + cout << "All you need is love!" << endl; + } + else + { + cout << "Love is not all you need!" << endl; + } + } + return 0; +} diff --git a/10194.cpp b/10194.cpp new file mode 100644 index 0000000..fbafae9 --- /dev/null +++ b/10194.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +bool issame(const char *t1, const char *t2) +{ + int len1 = strlen(t1), len2 = strlen(t2); + if (len1 != len2) + { + return false; + } + for (int i = 0; i < len1; i++) + if (toupper(t1[i]) != toupper(t2[i])) + { + return false; + } + return true; +} +// be careful with the CASE INSENSITIVE COMPARISON +int compare(const char *t1, const char *t2) +{ + int i; + for (i = 0; (t1[i] != '\0') && (t2[i] != '\0'); i++) + { + if (toupper(t1[i]) > toupper(t2[i])) + { + return 1; + } + else if (toupper(t1[i]) < toupper(t2[i])) + { + return -1; + } + } + if (t1[i] == '\0') + { + if (t2[i] == '\0') + { + return 0; + } + else + { + return -1; + } + } + else + { + return 1; + } +} + +struct TEAM +{ + // name of teams + char name[31]; + // total points earned, game played, wins, ties, losses, goals scored, goals against + int ttal; + int game; + int wins; + int ties; + int loss; + int scor; + int agan; + // initializing + void init() + { + ttal = 0, game = 0, wins = 0, ties = 0, loss = 0, scor = 0, agan = 0; + } + friend bool operator<=(const TEAM &t1, const TEAM &t2) + { + if (t1.ttal != t2.ttal) + { + return (t1.ttal > t2.ttal); + } + if (t1.wins != t2.wins) + { + return (t1.wins > t2.wins); + } + if (t1.scor - t1.agan != t2.scor - t2.agan) + { + return (t1.scor - t1.agan > t2.scor - t2.agan); + } + if (t1.scor != t2.scor) + { + return (t1.scor > t2.scor); + } + if (t1.game != t2.game) + { + return (t1.game < t2.game); + } + return (compare(t1.name, t2.name) < 0); + } +}; + +void merge_sort(TEAM array[], int begin_p, int end_p) +{ + if (begin_p >= end_p - 1) + { + return; + } + int temp = (end_p + begin_p) / 2, i, j, k; + TEAM array2[end_p - begin_p]; + merge_sort(array, begin_p, temp); + merge_sort(array, temp, end_p); + for (i = begin_p, j = temp, k = 0; (i < temp) && (j < end_p); k++) + { + if (array[i] <= array[j]) + { + array2[k] = array[i++]; + } + else + { + array2[k] = array[j++]; + } + } + while (i < temp) + { + array2[k++] = array[i++]; + } + while (j < end_p) + { + array2[k++] = array[j++]; + } + copy(array2, array2 + end_p - begin_p, array + begin_p); +} + +int main() +{ + // number of tournaments, number of teams, number of games + int num_of_tour, num_of_team, num_of_game; + // name of tournaments, game playing + char name_of_tour[101], game_play[1001]; + // information of teams + TEAM info[30]; + int team1, team2, score1, score2; + char *temp; + cin >> num_of_tour, getchar(); + for (int cnt_tour = 0; cnt_tour < num_of_tour; cnt_tour++) + { + if (cnt_tour != 0) + { + printf("\n"); + } + cin.getline(name_of_tour, 101, '\n'); + cin >> num_of_team, getchar(); + for (int cnt_team = 0; cnt_team < num_of_team; cnt_team++) + { + info[cnt_team].init(), cin.getline(info[cnt_team].name, 31, '\n'); + } + cin >> num_of_game, getchar(); + for (int cnt_game = 0; cnt_game < num_of_game; cnt_game++) + { + cin.getline(game_play, 1001, '\n'); + temp = strtok(game_play, "#@"); + for (int i = 0; i < num_of_team; i++) + { + if (issame(info[i].name, temp)) + { + team1 = i; + break; + } + } + temp = strtok(NULL, "#@"); + score1 = atoi(temp); + temp = strtok(NULL, "#@"); + score2 = atoi(temp); + temp = strtok(NULL, "#@"); + for (int i = 0; i < num_of_team; i++) + { + if (issame(info[i].name, temp)) + { + team2 = i; + break; + } + } + info[team1].game++, info[team2].game++; + info[team1].scor += score1, info[team2].scor += score2; + info[team1].agan += score2, info[team2].agan += score1; + if (score1 > score2) + { + info[team1].wins++, info[team2].loss++, info[team1].ttal += 3; + } + else if (score1 < score2) + { + info[team1].loss++, info[team2].wins++, info[team2].ttal += 3; + } + else + { + info[team1].ties++, info[team2].ties++, info[team1].ttal++, info[team2].ttal++; + } + } + merge_sort(info, 0, num_of_team); + printf("%s\n", name_of_tour); + for (int i = 0; i < num_of_team; i++) + { + printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", i + 1, info[i].name, info[i].ttal, info[i].game, info[i].wins, info[i].ties, info[i].loss, info[i].scor - info[i].agan, info[i].scor, info[i].agan); + } + } + return 0; +} diff --git a/10195.cpp b/10195.cpp index 9365c40..152308d 100644 --- a/10195.cpp +++ b/10195.cpp @@ -1,14 +1,22 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - double a,b,c; - while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){ - double s=(a+b+c)/2; - double rad=sqrt((s-a)*(s-b)*(s-c)/s); - printf("The radius of the round table is: %.3lf\n",rad); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + double a, b, c, s, r; + while (scanf("%lf%lf%lf", &a, &b, &c) == 3) + { + s = (a + b + c) / 2.0; + if (s != 0) + { + r = sqrt((s - a) * (s - b) * (s - c) / s); + } + else + { + r = 0; + } + printf("The radius of the round table is: %.3lf\n", r); + } + return 0; +} diff --git a/10196.cpp b/10196.cpp new file mode 100644 index 0000000..a103b8d --- /dev/null +++ b/10196.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +char board[12][12]; + +bool find_check(pair &king, int offset) +{ + int i, j; + // temp + board[king.first][king.second] = '.'; + // up + for (i = king.first, j = king.second; board[i][j] == '.' && i >= 2; i--) + ; + if (board[i][j] == char('R' + offset) || board[i][j] == char('Q' + offset)) + { + return true; + } + // down + for (i = king.first, j = king.second; board[i][j] == '.' && i <= 9; i++) + ; + if (board[i][j] == char('R' + offset) || board[i][j] == char('Q' + offset)) + { + return true; + } + // left + for (i = king.first, j = king.second; board[i][j] == '.' && j >= 2; j--) + ; + if (board[i][j] == char('R' + offset) || board[i][j] == char('Q' + offset)) + { + return true; + } + // right + for (i = king.first, j = king.second; board[i][j] == '.' && j <= 9; j++) + ; + if (board[i][j] == char('R' + offset) || board[i][j] == char('Q' + offset)) + { + return true; + } + // up left + for (i = king.first, j = king.second; board[i][j] == '.' && i >= 2 && j >= 2; i--, j--) + ; + if (board[i][j] == char('B' + offset) || board[i][j] == char('Q' + offset) || board[i][j] == char('P' + offset) && king.first - i == 1 && king.second - j == 1 && offset == 32) + { + return true; + } + // up right + for (i = king.first, j = king.second; board[i][j] == '.' && i >= 2 && j <= 9; i--, j++) + ; + if (board[i][j] == char('B' + offset) || board[i][j] == char('Q' + offset) || board[i][j] == char('P' + offset) && king.first - i == 1 && j - king.second == 1 && offset == 32) + { + return true; + } + // down left + for (i = king.first, j = king.second; board[i][j] == '.' && i <= 9 && j >= 2; i++, j--) + ; + if (board[i][j] == char('B' + offset) || board[i][j] == char('Q' + offset) || board[i][j] == char('P' + offset) && i - king.first == 1 && king.second - j == 1 && offset == 0) + { + return true; + } + // down right + for (i = king.first, j = king.second; board[i][j] == '.' && i <= 9 && j <= 9; i++, j++) + ; + if (board[i][j] == char('B' + offset) || board[i][j] == char('Q' + offset) || board[i][j] == char('P' + offset) && i - king.first == 1 && j - king.second == 1 && offset == 0) + { + return true; + } + // knights + i = king.first; + j = king.second; + if (board[i - 2][j - 1] == char('N' + offset) || board[i - 1][j - 2] == char('N' + offset) || board[i + 1][j - 2] == char('N' + offset) || board[i + 2][j - 1] == char('N' + offset) || board[i + 2][j + 1] == char('N' + offset) || board[i + 1][j + 2] == char('N' + offset) || board[i - 1][j + 2] == char('N' + offset) || board[i - 2][j + 1] == char('N' + offset)) + { + return true; + } + // restore temporary change made in the beginning + board[king.first][king.second] = char('K' + offset); + return false; +} + +int main() +{ + char c; + int counter = 0; + pair whiteking, blackking; + while (cin.peek() != -1) + { + counter++; + for (int i = 0; i < 12; i++) + for (int j = 0; j < 12; j++) + { + board[i][j] = '.'; + } + bool empty = true; + for (int i = 2; i <= 9; i++) + { + for (int j = 2; j <= 9; j++) + { + cin >> board[i][j]; + if (board[i][j] != '.') + { + empty = false; + } + if (board[i][j] == 'k') + { + blackking.first = i; + blackking.second = j; + } + else if (board[i][j] == 'K') + { + whiteking.first = i; + whiteking.second = j; + } + } + } + if (empty) + { + break; + } + cin.ignore(100, '\n'); + bool black_check = find_check(blackking, 0); + bool white_check = find_check(whiteking, 32); + cout << "Game #" << counter << ": "; + if (white_check) + { + cout << "white king is in check." << endl; + } + else if (black_check) + { + cout << "black king is in check." << endl; + } + else + { + cout << "no king is in check." << endl; + } + } + return 0; +} diff --git a/10197.cpp b/10197.cpp new file mode 100644 index 0000000..39d678b --- /dev/null +++ b/10197.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +string conj_[6] = {"eu ", "tu ", "ele/ela ", "nos ", "vos ", "eles/elas "}; +string suffix[3][6] = {{"o", "as", "a", "amos", "ais", "am"}, {"o", "es", "e", "emos", "eis", "em"}, {"o", "es", "e", "imos", "is", "em"}}; + +int main() +{ + bool fst = true; + string v1, v2, root; + int n; + + conj_[3][1] = (char)243; + conj_[4][1] = (char)243; + + while (cin >> v1 >> v2) + { + if (!fst) + { + printf("\n"); + } + cout << v1 << " (to " << v2 << ")" << endl; + if (v1.size() < 2) + { + printf("Unknown conjugation\n"); + fst = false; + continue; + } + if (v1[v1.size() - 2] == 'a' && v1[v1.size() - 1] == 'r') + { + n = 0; + } + else if (v1[v1.size() - 2] == 'e' && v1[v1.size() - 1] == 'r') + { + n = 1; + } + else if (v1[v1.size() - 2] == 'i' && v1[v1.size() - 1] == 'r') + { + n = 2; + } + else + { + printf("Unknown conjugation\n"); + fst = false; + continue; + } + root = string(v1.begin(), v1.begin() + v1.size() - 2); + for (int i = 0; i < 6; i++) + { + printf("%s%s%s\n", conj_[i].c_str(), root.c_str(), suffix[n][i].c_str()); + } + fst = false; + } + return 0; +} diff --git a/10198.cpp b/10198.cpp new file mode 100644 index 0000000..5014dc8 --- /dev/null +++ b/10198.cpp @@ -0,0 +1,300 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < (int)n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() + +const static int BASE = 10000, LANGE = 4; +const string BigIntBase = "10000"; + +enum +{ + LESS, + EQUAL, + GREATER +}; + +#define FRONT "%d" +#define FORMAT "%04d" + +class BigInt +{ +public: + bool neg; + vector value; + + BigInt(void) + : neg(false) + { + value.resize(1, 0); + } + BigInt(int s) + : neg(false) + { + value.push_back(s); + } + BigInt(vector v, bool sign) + : neg(sign), value(v) + { + } + BigInt(string in) + { + neg = false; + if (in[0] == '-') + { + neg = true; + in.erase(in.begin()); + } + while (in.size() % LANGE != 0) + in.insert(in.begin(), 1, '0'); + for (int i = in.size() - LANGE; i >= 0; i -= LANGE) + value.push_back(atoi(in.substr(i, LANGE).c_str())); + regulation(); + } + BigInt operator+(BigInt in) + { + if (sign() == in.sign()) + return add(*this, in, sign()); + else if (compare(in, true) == LESS) + return sub(in, *this, in.sign()); + else + return sub(*this, in, sign()); + } + BigInt operator*(BigInt in) + { + return mul(*this, in, sign() != in.sign()); + } + BigInt operator*(int in) + { + assert(in < BASE); + BigInt re = *this; + REP(i, size()) + re[i] *= in; + re.regulation(); + re.neg = in < 0; + return re; + } + BigInt operator-(BigInt in) + { + if (sign() != in.sign()) + return add(*this, in, sign()); + else if (compare(in, true) == GREATER)// != LESS ? + return sub(*this, in, sign()); + else + return sub(in, *this, !in.sign()); + } + BigInt operator/(BigInt in) + { + return div(*this, in, (sign() != in.sign())); + } + BigInt operator/(int n) + { + return div(*this, n); + } + BigInt operator%(BigInt in) + { + return *this - ((*this / in) * in); + } + bool operator==(BigInt in) + { + return compare(in, false) == EQUAL; + } + bool operator<(BigInt in) + { + return compare(in, false) == LESS; + } + bool operator>(BigInt in) + { + return compare(in, false) == GREATER; + } + int &operator[](int n) + { + return value[n]; + } + string toString(void) + { + char buffer[LANGE + 1]; + string re; + + if (neg == true) + re = "-"; + sprintf(buffer, FRONT, value.back()), re += buffer; + + for (int i = value.size() - 2; i >= 0; i--) + sprintf(buffer, FORMAT, value[i]), re += buffer; + return re; + } + int size(void) + { + return value.size(); + } + void resize(int n) + { + value.resize(n); + } + bool sign(void) + { + return neg; + } + void regulation(void) + { + value.push_back(0); + REP(i, value.size() - 1) + { + value[i + 1] += value[i] / BASE; + value[i] %= BASE; + } + while (value.size() > 1 && value.back() == 0) + value.pop_back(); + } + int compare(BigInt in, bool abs) + { + if (abs) + { + in.neg = sign(); + return compare(in, false); + } + if (sign() != in.sign()) + { + return sign() ? LESS : GREATER; + } + if (size() != in.size()) + { + if (!sign()) + return size() > in.size() ? GREATER : LESS; + else + return size() > in.size() ? LESS : GREATER; + } + for (int i = in.size(); i--;) + { + if (value[i] == in.value[i]) + continue; + return (value[i] > in.value[i]) ? GREATER : LESS; + } + return EQUAL; + } + BigInt add(BigInt a, BigInt b, bool sign) + { + if (a.size() < b.size()) + swap(a, b); + REP(i, b.size()) + a[i] += b[i]; + a.regulation(); + a.neg = sign; + return a; + } + BigInt sub(BigInt a, BigInt b, bool sign) + { + BigInt re = BigInt(a.value, sign); + re.value.resize(a.size(), 0); + int borrow = 0; + REP(i, re.size()) + { + if (i < b.size()) + re[i] -= b[i] + borrow; + else + re[i] -= borrow; + if (re[i] >= 0) + borrow = 0; + else + borrow = 1, re[i] += BASE; + } + re.regulation(); + return re; + } + BigInt mul(BigInt a, BigInt b, bool sign) + { + BigInt re; + re.value.resize(a.size() + b.size() + 1); + REP(i, a.size()) + { + REP(j, b.size()) + re[i + j] += a[i] * b[j]; + } + re.neg = sign; + re.regulation(); + return re; + } + BigInt div(BigInt a, int n) + { + int c = 0, tmp; + for (int i = a.size(); i--;) + { + tmp = BASE * c + a[i]; + a[i] = tmp / n; + c = tmp % n; + } + a.regulation(); + return a; + } + BigInt div(BigInt a, BigInt b, bool sign) + { + if (a < b) + return BigInt("0"); + int f = BASE / (b[(int)b.size() - 1] + 1); + a = a * f; + b = b * f; + BigInt c(a.size() - b.size() + 1); + for (int i = a.size() - 1, k = c.size() - 1; k >= 0; --i, --k) + { + c[k] = (i + 1 < a.size() ? a[i + 1] : 0) * BASE; + c[k] = (c[k] + a[i]) / b[(int)b.size() - 1]; + BigInt d(k + b.size()); + REP(j, b.size()) + { + d[k + j] = c[k] * b[j]; + } + d.regulation(); + while (a < d) + { + --c[k]; + REP(j, b.size()) + { + d[k + j] -= b[j]; + } + REP(j, d.size()) + { + while (d.value[j] < 0) + { + --d.value[j + 1]; + d.value[j] += BASE; + } + } + d.regulation(); + } + a = a - d; + } + c.neg = sign; + c.regulation(); + return c; + } +}; + +const int F = 100 + 1; +BigInt bi[F]; + +int main() +{ + const int BI = 1000 + 1; + BigInt bi[BI]; + bi[1] = BigInt("2"); + bi[2] = BigInt("5"); + bi[3] = BigInt("13"); + for (int i = 4; i < BI; ++i) + { + bi[i] = BigInt("2") * bi[i - 1] + bi[i - 2] + bi[i - 3]; + } + for (int n; cin >> n; cout << bi[n].toString() << endl) + ; + return 0; +} diff --git a/10199.cpp b/10199.cpp new file mode 100644 index 0000000..ea61a78 --- /dev/null +++ b/10199.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +#define NAME_LEN 50 +#define MAX_NODES 200 +#define MAX_EDGES (MAX_NODES * MAX_NODES * 2) +#define HASHTABLE_SIZE 237 +#define WHITE 0 +#define GRAY 1 +#define BLACK 2 + +struct Edge; +struct Node +{ + int num; + char name[NAME_LEN + 1]; + Node *next; + Edge *edges; + Node *parent; + int color, children, d; + bool articulation; +}; +struct Edge +{ + Node *node; + Edge *next; +}; + +Node nodes[MAX_NODES], *tbl[HASHTABLE_SIZE], *sorted[MAX_NODES]; +Edge edges[MAX_EDGES]; + +int n, free_edge, time_; + +int hash_(char *s) +{ + unsigned h = 0; + while (*s) + { + h = h * 31 + *s++; + } + return h % HASHTABLE_SIZE; +} + +void put_to_hash_tbl(char *name, Node *node) +{ + int i = hash_(name); + node->next = tbl[i]; + tbl[i] = node; +} + +Node *find_node(char *name) +{ + Node *p = tbl[hash_(name)]; + while (p) + { + if (!strcmp(name, p->name)) + { + return p; + } + p = p->next; + } + return p; +} + +void read_names() +{ + for (int i = 0; i < HASHTABLE_SIZE; i++) + { + tbl[i] = NULL; + } + for (int i = 0; i < n; i++) + { + Node *node = &nodes[i]; + node->num = i; + node->color = WHITE; + node->articulation = false; + node->edges = NULL; + node->d = 0; + node->children = 0; + node->parent = NULL; + scanf("%s", node->name); + put_to_hash_tbl(node->name, node); + } +} + +void add_edge(Node *src, Node *dest) +{ + Edge *edge = &edges[free_edge++]; + edge->node = dest; + edge->next = src->edges; + src->edges = edge; +} + +void read_roads() +{ + int r; + char name1[NAME_LEN + 1], name2[NAME_LEN + 1]; + free_edge = 0; + scanf("%d", &r); + for (int i = 0; i < r; i++) + { + scanf("%s %s", name1, name2); + Node *node1 = find_node(name1); + Node *node2 = find_node(name2); + add_edge(node1, node2); + add_edge(node2, node1); + } +} + +int dfs_visit(Node *u) +{ + u->color = GRAY; + u->d = ++time_; + int ret = u->d; + for (Edge *e = u->edges; e; e = e->next) + { + Node *v = e->node; + if (v->color == WHITE) + { + v->parent = u; + int r = dfs_visit(v); + u->children++; + if (r < ret) + { + ret = r; + } + if (u->d <= r) + { + u->articulation = true; + } + } + else if (v->color == GRAY && u->parent != v) + { + if (v->d < ret) + { + ret = v->d; + } + } + } + u->color = BLACK; + return ret; +} + +void dfs() +{ + time_ = 0; + for (int i = 0; i < n; i++) + { + Node *u = &nodes[i]; + if (u->color == WHITE) + { + dfs_visit(u); + u->articulation = (u->children > 1); + } + } +} + +int compare(Node **node1, Node **node2) +{ + return strcmp((*node1)->name, (*node2)->name); +} + +void print_solution() +{ + int cameras = 0; + for (int i = 0; i < n; i++) + { + Node *node = &nodes[i]; + if (!node->articulation) + { + continue; + } + sorted[cameras++] = node; + } + qsort(sorted, cameras, sizeof(Node *), (int (*)(const void *, const void *))compare); + printf("%d camera(s) found\n", cameras); + for (int i = 0; i < cameras; i++) + { + printf("%s\n", sorted[i]->name); + } +} + +int main() +{ + int count = 0; + while (scanf("%d", &n) == 1) + { + if (n <= 0) + { + break; + } + read_names(); + read_roads(); + if (count > 0) + { + putchar('\n'); + } + printf("City map #%d: ", ++count); + dfs(); + print_solution(); + } + return 0; +} diff --git a/102.cpp b/102.cpp index ce51689..eb9f5c1 100644 --- a/102.cpp +++ b/102.cpp @@ -1,27 +1,40 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - string str[6]={"BCG","BGC","CBG","CGB","GBC","GCB"}; - int b1,b2,b3; - int c1,c2,c3; - int g1,g2,g3; - while(scanf("%d%d%d%d%d%d%d%d%d",&b1,&g1,&c1,&b2,&g2,&c2,&b3,&g3,&c3)!=EOF){ - int b=b1+b2+b3; - int g=g1+g2+g3; - int c=c1+c2+c3; - int val[6]={b-b1+c-c2+g-g3,b-b1+g-g2+c-c3,c-c1+b-b2+g-g3,c-c1+g-g2+b-b3,g-g1+b-b2+c-c3,g-g1+c-c2+b-b3}; - int min=val[0]; - int ind=0,i; - for(i=1;i<6;i++){ - if(min>val[i]){ - min=val[i]; - ind=i; - } - } - cout< + +using namespace std; + +enum +{ + B = 0, + C = 1, + G = 2 +}; + +int main() +{ + int binColor[][3] = {{B, C, G}, {B, G, C}, {C, B, G}, {C, G, B}, {G, B, C}, {G, C, B}}; + char s[][4] = {"BCG", "BGC", "CBG", "CGB", "GBC", "GCB"}; + int bin[3][3]; + while (scanf("%d%d%d%d%d%d%d%d%d", + &bin[0][B], &bin[0][G], &bin[0][C], + &bin[1][B], &bin[1][G], &bin[1][C], + &bin[2][B], &bin[2][G], &bin[2][C]) != EOF) + { + int currMv = 0, totalGlasses = 0; + for (int i = 0; i < 3; i++) + { + totalGlasses += (bin[i][B] + bin[i][G] + bin[i][C]); + } + int minMv = totalGlasses, minNo = 0; + for (int i = 0; i < 6; i++) + { + currMv = totalGlasses - bin[0][binColor[i][0]] - bin[1][binColor[i][1]] - bin[2][binColor[i][2]]; + if (currMv < minMv) + { + minMv = currMv; + minNo = i; + } + } + printf("%s %d\n", s[minNo], minMv); + } + return 0; +} diff --git a/10200.cpp b/10200.cpp index e5f434c..8276c64 100644 --- a/10200.cpp +++ b/10200.cpp @@ -1,44 +1,56 @@ -#include -#include -#include +#include + using namespace std; -const int LIM = 10001; +const int MAXSIZE = 10010; -int Cal(int N){ - return ( N * N + N + 41 ); -} +int cntPrime[MAXSIZE]; -bool isPrime(int N){ - for (int i=2; i*i<=N; i++) - if( N%i == 0 ) +bool isPrime(int n) +{ + for (int i = 2; i <= (int)sqrt(n); i++) + if (n % i == 0) + { return false; + } return true; } -int main(){ - int prime[LIM+1]; +void init() +{ int i; - memset(prime, 0, sizeof prime); - prime[0] = 1; - for (i=1; i 0 ) - val -= prime[A-1]; - double per = (double)val / (double)(B - A + 1); - per *= 10000; - per = floor( per + 0.5 ); - per /= 100; - printf("%.2lf\n", per); + for (; i <= 10000; i++) + if (isPrime(i * i + i + 41)) + { + cntPrime[i] = cntPrime[i - 1] + 1; + } + else + { + cntPrime[i] = cntPrime[i - 1]; + } +} + +int main() +{ + init(); + int a, b; + while (scanf("%d%d", &a, &b) == 2) + { + int cnt; + if (a == 0) + { + cnt = cntPrime[b]; + } + else + { + cnt = cntPrime[b] - cntPrime[a - 1]; + } + printf("%.2lf\n", cnt * 100.0 / (b - a + 1) + 1e-6); } return 0; } - diff --git a/10201.cpp b/10201.cpp new file mode 100644 index 0000000..8c82418 --- /dev/null +++ b/10201.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define MAX_POSTOS 101 + +int candidato, custo, i, litros, mais_barato, n, npostos, pos_atual, pos_final; +int pos[MAX_POSTOS], preco[MAX_POSTOS]; +char ch; +bool possivel, achou; + +int main() +{ + scanf("%d", &n); + while (n) + { + n--; + scanf("%d", &pos_final); + ch = getchar(); + for (i = 0;; ch = getchar(), i++) + { + if ((ch = getchar()) == '\n') + { + break; + } + ungetc(ch, stdin); + if (scanf("%d %d", &pos[i], &preco[i]) == EOF) + { + break; + } + } + possivel = 1; + npostos = i; + pos[npostos] = pos_final = pos_final + 100; + preco[npostos++] = 0; + if (pos[0] > 100 || npostos == 1) + { + possivel = 0; + } + else + for (i = 1; possivel && i < npostos; i++) + if (pos[i] - pos[i - 1] > 200) + { + possivel = 0; + } + if (possivel) + { + litros = 100; + pos_atual = custo = candidato = 0; + while (pos_atual + litros < pos_final) + { + achou = 0; + for (i = candidato, mais_barato = 2001; pos_atual + litros >= pos[i]; i++) + if (preco[i] < mais_barato) + { + mais_barato = preco[i]; + candidato = i; + } + litros -= pos[candidato] - pos_atual; + pos_atual = pos[candidato]; + for (i = candidato + 1; i < npostos && pos[i] - pos_atual < 200; i++) + if (preco[i] < preco[candidato]) + { + custo += preco[candidato] * (pos[i] - pos_atual - litros); + litros = 0; + pos_atual = pos[i]; + candidato = i; + achou = 1; + break; + } + if (!achou) + { + custo += preco[candidato++] * (200 - litros); + litros = 200; + } + } + } + (possivel ? printf("%d\n", custo) : printf("Impossible\n")); + if (n) + { + printf("\n"); + } + } + return 0; +} diff --git a/10202.cpp b/10202.cpp new file mode 100644 index 0000000..8e7e65c --- /dev/null +++ b/10202.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +#define MAX 10 + +typedef long int Int; + +Int a[MAX * MAX], b[MAX]; +int n, n2, prev_[MAX * MAX], nxt[MAX * MAX], first_item, search_start; + +// init the singly linked list that chains the sum S altogether +void init_list() +{ + prev_[2] = 0;// zero indicates end of list + nxt[2] = 3;// The 'nxt' of S3 is S4 + for (int i = 3; i < n2; i++) + { + prev_[i] = i - 1; + nxt[i] = i + 1; + } + first_item = 2;// first_item is the zero-based index of the current smallest sum S +} + +// remove a node (sum S) from the singly linked list +void remove(int i) +{ + // are we at the end of the linked list? + if (prev_[i] == 0) + { + // Yes. + prev_[nxt[i]] = 0; + first_item = nxt[i]; + } + else + { + // No. + nxt[prev_[i]] = nxt[i]; + prev_[nxt[i]] = prev_[i]; + } +} + +// gets the current smallest sum and remove it from the linked list +Int get_smallest() +{ + Int x = a[first_item]; + remove(first_item); + return x; +} + +// search the linked list for the sum x and remove it from the list +bool find_and_remove(Int x) +{ + // assign the sum at the end so that the while loop below will stop + a[n2] = x; + int i = search_start; + while (a[i] < x) + { + i = nxt[i]; + } + // Is the sum found? Did we reach the end of the list? + if (i == n2) + { + return false; + } + // Is the sum found? + if (a[i] != x) + { + return false; + } + remove(i); + // The nxt search shall start at the smallest sum that is bigger than x + // so as to ensure that all sums are in ascending order + search_start = nxt[i]; + return true; +} + +bool test(int t) +{ + // a[0] is S1 = N1 + N2 and a[1] is S2 = N1 + N3 + // here we assume a[t] is N2 + N3 + // a[t] is S[t+1] + // so x will be 2 N1 + N2 + N3 - (N2 + N3) + Int x = a[0] + a[1] - a[t]; + if (x % 2 != 0) + { + return false; + } + // b is the array of integers that are the solution + b[0] = x / 2; // N1 + b[1] = a[0] - b[0];// N2 + b[2] = a[t] - b[1];// N3 + init_list(); + // remove a[t] ( S[t+1] ) from the linked list. + remove(t); + // now we try to figure out whats N4, N5 ... etc. + // by brute force + // we start from N4 first + // This is the main algorithm + for (int i = 3; i < n; i++) + { + // Assume the current smallest sum is N1 + N[i + 1] + x = get_smallest(); + b[i] = x - b[0];// subtract from N1 to calculate N[i + 1] + search_start = first_item; + // Compute the possible pair wise sums of N[i + 1] + // The pair wise sums computed are: + // N[i + 1] + N2 + // N[i + 1] + N3 + // ... + // N[i + 1] + N[i ] + // Basically, the loop below will check whether the sums are in ascending order in the linked list. + for (int j = 1; j < i; j++) + { + // Calculate the sum of N[i+1] and N[j + 1] and check whether it exists in the input + // if exists, then remove it from the linked list + // if does not exist, assumption fails. + if (!find_and_remove(b[i] + b[j])) + { + return false; + } + } + } + return true; +} + +// compare function to sort out the array +int compare(Int *v1, Int *v2) +{ + return *v1 - *v2; +} + +void solve() +{ + // sort the sums from smallest to biggest + qsort(a, n2, sizeof(Int), (int (*)(const void *, const void *))compare); + // brute force + for (int i = 2; i < n; i++) + { + // we assume a[i] is N2 + N3 and then we will go from there + // if things doesn't work out, that means the assumption is false + // note that a[i] is N[i+1] + if (test(i)) + { + // we reach here when we solved it, that means our assumption is correct + // print out solution + printf("%ld", b[0]); + for (int j = 1; j < n; j++) + { + printf(" %ld", b[j]); + } + printf("\n"); + return; + } + } + printf("Impossible\n"); +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + n2 = n * (n - 1) / 2; // calculate the no. of sums + for (int i = 0; i < n2; i++) + { + scanf("%ld", &a[i]); + } + solve(); + } + return 0; +} diff --git a/10203.cpp b/10203.cpp new file mode 100644 index 0000000..9b56f7f --- /dev/null +++ b/10203.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int ncase, minute, kk; +double sx, sy, X1, X2, Y1, Y2; +double dx, dy, dist, time1, time2, total_time; +char str[256]; + +int main() +{ + gets(str); + ncase = atoi(str); + for (kk = 0; kk < ncase; ++kk) + { + gets(str); + sscanf(str, "%lf %lf", &sx, &sy); + total_time = time1 = time2 = 0; + while (gets(str)) + { + if (!strlen(str)) + { + break; + } + sscanf(str, "%lf%lf%lf%lf", &X1, &Y1, &X2, &Y2); + dx = X1 - X2; + dy = Y1 - Y2; + dist = sqrt(dx * dx + dy * dy); + time1 += dist / 10000; + } + total_time = time1; + minute = (int)(total_time * 60 + 0.5); + if (kk) + { + puts(""); + } + printf("%d:%02d\n", minute / 60, minute % 60); + } + return 0; +} diff --git a/10205.cpp b/10205.cpp new file mode 100644 index 0000000..77c4b0e --- /dev/null +++ b/10205.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +struct card +{ + int value; + int suit; // 0 - clubs, 1 - diamonds, 2 - hearts, 3 - spades +}; + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second < b.second; +} + +string card2str(const card &c) +{ + stringstream ss; + switch (c.value) + { + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + ss << c.value; + break; + case 11: + ss << "Jack"; + break; + case 12: + ss << "Queen"; + break; + case 13: + ss << "King"; + break; + case 14: + ss << "Ace"; + break; + } + switch (c.suit) + { + case 0: + ss << " of Clubs"; + break; + case 1: + ss << " of Diamonds"; + break; + case 2: + ss << " of Hearts"; + break; + case 3: + ss << " of Spades"; + break; + } + return ss.str(); +} + +int main() +{ + int t, n, v; + card tmp; + vector> cards; + vector> shuffles; + cin >> t; + while (t--) + { + cin >> n; + cards.clear(); + shuffles.clear(); + for (int i = 0; i < 4; i++) + { + for (int j = 2; j < 15; j++) + { + tmp.value = j; + tmp.suit = i; + cards.push_back(make_pair(tmp, 0)); + } + } + while (n--) + { + vector shuffle; + for (int i = 0; i < 52; i++) + { + cin >> v; + shuffle.push_back(v); + } + shuffles.push_back(shuffle); + } + cin.ignore(100, '\n'); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> v; + cin.ignore(100, '\n'); + for (int i = 0; i < 52; i++) + { + cards[shuffles[v - 1][i] - 1].second = i; + } + sort(cards.begin(), cards.end(), theTruthIsOutThere); + } + for (int i = 0; i < 52; i++) + { + cout << card2str(cards[i].first) << endl; + } + if (t) + { + cout << endl; + } + } + return 0; +} diff --git a/10206.cpp b/10206.cpp new file mode 100644 index 0000000..27a17bb --- /dev/null +++ b/10206.cpp @@ -0,0 +1,388 @@ +#include + +using namespace std; + +const int MAX_STAR = 1000; +const int MAX_STAR_PER_CONSTEL = 500; +const int NAME_LEN = 50; +const int COOR_BOUND = 100; +const int MAX_OCCUR = 500000; + +struct Star +{ + int x, y, bright; +}; + +int starCmp(const void *star1, const void *star2) +{ + int res = ((Star *)star1)->x - ((Star *)star2)->x; + if (res == 0) + { + res = ((Star *)star1)->y - ((Star *)star2)->y; + } + return res; +} + +struct Constel +{ + char m_name[NAME_LEN]; + Star m_star[MAX_STAR_PER_CONSTEL]; + int m_cnt_star; + + void init() + { + m_cnt_star = 0; + } + void addStar(Star star) + { + m_star[m_cnt_star] = star; + m_cnt_star++; + } + int bright() + { + int res = 0; + for (int i = 0; i < m_cnt_star; i++) + { + res += m_star[i].bright; + } + return res; + } + void input() + { + scanf("%d%s", &m_cnt_star, m_name); + for (int i = 0; i < m_cnt_star; i++) + { + scanf("%d%d", &m_star[i].x, &m_star[i].y); + } + } + char *name() + { + return m_name; + } + void print_constel() + { + for (int i = 0; i < m_cnt_star; i++) + { + printf("(%d,%d)", m_star[i].x, m_star[i].y); + if (i < m_cnt_star - 1) + { + printf(" "); + } + else + { + printf("\n"); + } + } + } + Star star(int i) + { + return m_star[i]; + } + int cnt_star() + { + return m_cnt_star; + } + void sort_star() + { + qsort(m_star, m_cnt_star, sizeof(Star), starCmp); + } +}; + +bool m_have_star[COOR_BOUND * 2 + 1][COOR_BOUND * 2 + 1]; +int m_star_no[COOR_BOUND * 2 + 1][COOR_BOUND * 2 + 1]; + +struct Map +{ + Star m_star[MAX_STAR]; + int m_cnt_star; + inline int coorMap(int coor) + { + return coor + COOR_BOUND; + } + + bool input() + { + bool have_nxt = false; + scanf("%d", &m_cnt_star); + if (m_cnt_star > 0) + { + have_nxt = true; + memset(m_have_star, 0, sizeof(m_have_star)); + for (int i = 0; i < m_cnt_star; i++) + { + scanf("%d %d %d", &m_star[i].x, &m_star[i].y, &m_star[i].bright); + m_have_star[coorMap(m_star[i].x)][coorMap(m_star[i].y)] = true; + m_star_no[coorMap(m_star[i].x)][coorMap(m_star[i].y)] = i; + } + } + return have_nxt; + } + bool have_star(int x, int y) + { + bool have; + if (x > COOR_BOUND || x < -COOR_BOUND || y > COOR_BOUND || y < -COOR_BOUND) + { + have = false; + } + else + { + have = m_have_star[coorMap(x)][coorMap(y)]; + } + return have; + } + Star star(int i) + { + return m_star[i]; + } + Star star(int x, int y) + { + return m_star[m_star_no[coorMap(x)][coorMap(y)]]; + } + int cnt_star() + { + return m_cnt_star; + } +}; + +struct Occur +{ + Star left_low, right_low, right_high, left_high; +}; + +int occurCmp(const void *occur1, const void *occur2) +{ + Occur *p1 = (Occur *)occur1; + Occur *p2 = (Occur *)occur2; + int res = starCmp(&p1->left_low, &p2->left_low); + if (res == 0) + { + res = starCmp(&p1->right_low, &p2->right_low); + } + if (res == 0) + { + res = starCmp(&p1->right_high, &p2->right_high); + } + if (res == 0) + { + res = starCmp(&p1->left_high, &p2->left_high); + } + return res; +} +Occur m_occur[MAX_OCCUR]; + +struct Soln +{ + int m_cnt_sketch, m_cnt_occur; + Constel m_sketch, m_brightest; + Map m_map; + + Constel checkOccur(Star occur_star1, Star occur_star2) + { + int sinUp, cosUp, down; + getAngle(m_sketch.star(0), m_sketch.star(1), occur_star1, occur_star2, sinUp, cosUp, down); + Constel occur_constel; + occur_constel.init(); + occur_constel.addStar(occur_star1); + occur_constel.addStar(occur_star2); + for (int i = 2; i < m_sketch.cnt_star(); i++) + { + Star oldStar = m_sketch.star(i); + oldStar.x = occur_star1.x + (oldStar.x - m_sketch.star(0).x); + oldStar.y = occur_star1.y + (oldStar.y - m_sketch.star(0).y); + Star star_new = rotate(occur_star1, oldStar, sinUp, cosUp, down); + if (m_map.have_star(star_new.x, star_new.y)) + { + star_new.bright = m_map.star(star_new.x, star_new.y).bright; + occur_constel.addStar(star_new); + } + else + { + occur_constel.init(); + break; + } + } + return occur_constel; + } + void dealWithOneStar() + { + m_cnt_occur = m_map.cnt_star(); + Star brightest; + brightest = m_map.star(0); + for (int i = 1; i < m_cnt_occur; i++) + { + if (m_map.star(i).bright > brightest.bright) + { + brightest = m_map.star(i); + } + } + m_brightest.init(); + m_brightest.addStar(brightest); + } + inline void getAngle(Star sketchStar1, Star sketchStar2, Star occur_star1, Star occur_star2, int &sinUp, int &cosUp, int &down) + { + int oldX = sketchStar2.x - sketchStar1.x; + int oldY = sketchStar2.y - sketchStar1.y; + int newX = occur_star2.x - occur_star1.x; + int newY = occur_star2.y - occur_star1.y; + sinUp = oldX * newY - newX * oldY; + cosUp = oldX * newX + oldY * newY; + down = oldX * oldX + oldY * oldY; + } + void removeRepeat() + { + qsort(m_occur, m_cnt_occur, sizeof(Occur), occurCmp); + int oldOccurCnt = m_cnt_occur; + int i = 0; + while (i < oldOccurCnt - 1) + { + int j = i + 1; + while (j < oldOccurCnt && occurCmp(&m_occur[i], &m_occur[j]) == 0) + { + m_cnt_occur--; + j++; + } + i = j; + } + } + inline Star rotate(Star center, Star old, int sinUp, int cosUp, int down) + { + Star res; + int relativeX = old.x - center.x; + int relativeY = old.y - center.y; + int xUp = relativeX * cosUp - relativeY * sinUp; + int yUp = relativeX * sinUp + relativeY * cosUp; + if (xUp % down == 0 && yUp % down == 0) + { + res.x = center.x + xUp / down; + res.y = center.y + yUp / down; + } + else + { + res.x = COOR_BOUND + 1; + res.y = COOR_BOUND + 1; + } + return res; + } + inline int starDis(Star star1, Star star2) + { + return (star1.x - star2.x) * (star1.x - star2.x) + (star1.y - star2.y) * (star1.y - star2.y); + } + void toOccur(Constel &constel, Occur &occur) + { + occur.left_low = constel.star(0); + occur.right_low = constel.star(0); + occur.right_high = constel.star(0); + occur.left_high = constel.star(0); + for (int i = 1; i < constel.cnt_star(); i++) + { + Star current = constel.star(i); + if (current.x < occur.left_low.x || current.x == occur.left_low.x && current.y < occur.left_low.y) + { + occur.left_low = current; + } + if (current.y < occur.right_low.y || current.y == occur.right_low.y && current.x > occur.right_low.x) + { + occur.right_low = current; + } + if (current.x > occur.right_high.x || current.x == occur.right_high.x && current.y > occur.right_high.y) + { + occur.right_high = current; + } + if (current.y > occur.left_high.y || current.y == occur.left_high.y && current.x < occur.left_high.x) + { + occur.left_high = current; + } + } + } + +public: + bool input() + { + bool have_nxt = m_map.input(); + if (have_nxt) + { + scanf("%d", &m_cnt_sketch); + } + return have_nxt; + } + bool inputConstel() + { + bool have_nxt = false; + if (m_cnt_sketch > 0) + { + have_nxt = true; + m_sketch.input(); + m_cnt_sketch--; + } + return have_nxt; + } + void solve() + { + if (m_sketch.cnt_star() == 1) + { + dealWithOneStar(); + } + else if (m_sketch.cnt_star() > m_map.cnt_star()) + { + m_cnt_occur = 0; + } + else + { + m_cnt_occur = 0; + int max_bright = INT_MIN; + for (int i = 0; i < m_map.cnt_star(); i++) + { + Star occur_star1 = m_map.star(i); + for (int j = 0; j < m_map.cnt_star(); j++) + { + if (j != i) + { + Star occur_star2 = m_map.star(j); + Constel occur_constel = checkOccur(occur_star1, occur_star2); + if (occur_constel.cnt_star() > 0) + { + occur_constel.sort_star(); + int bright = occur_constel.bright(); + if (bright > max_bright) + { + max_bright = bright; + m_brightest = occur_constel; + } + toOccur(occur_constel, m_occur[m_cnt_occur]); + m_cnt_occur++; + } + } + } + } + removeRepeat(); + } + } + void output() + { + printf("%s occurs %d time(s) in the map.\n", m_sketch.name(), m_cnt_occur); + if (m_cnt_occur > 0) + { + printf("Brightest occurrence: "); + m_brightest.print_constel(); + } + } +}; + +int main() +{ + Soln soln; + int mapCnt = 1; + while (soln.input()) + { + printf("Map #%d\n", mapCnt); + while (soln.inputConstel()) + { + soln.solve(); + printf("\n"); + soln.output(); + } + printf("-----\n"); + mapCnt++; + } + return 0; +} diff --git a/10207.cpp b/10207.cpp new file mode 100644 index 0000000..5fea2c2 --- /dev/null +++ b/10207.cpp @@ -0,0 +1,212 @@ +#include + +using namespace std; + +#define MAX_SIZE 69 +#define MAX_VALUE 1000000000 +#define MAX_DIGIT "9" +#define MAX_DIGIT_ 9 + +struct BCD_Type +{ + unsigned data[MAX_SIZE]; + + int get_left() + { + for (int i = MAX_SIZE - 1; i > 0; i--) + if (data[i]) + { + break; + } + return i; + } + + void carry() + { + int i; + for (i = 0; i < MAX_SIZE - 1; i++) + { + data[i + 1] += data[i] / MAX_VALUE, data[i] %= MAX_VALUE; + } + data[i] %= MAX_VALUE; + } + + BCD_Type() + { + } + BCD_Type(const BCD_Type &a) + { + memcpy(data, a.data, MAX_SIZE * sizeof(unsigned)); + } + BCD_Type(unsigned a) + { + memset(data, 0, MAX_SIZE * sizeof(unsigned)); + data[0] = a % MAX_VALUE; + a /= MAX_VALUE; + data[1] = a % MAX_VALUE; + a /= MAX_VALUE; + data[2] = a % MAX_VALUE; + a /= MAX_VALUE; + } + + //ADD + BCD_Type &operator+=(unsigned n)//O(n) + { + data[0] += n % MAX_VALUE; + n /= MAX_VALUE; + data[1] += n; + carry(); + return *this; + } + BCD_Type &operator+=(BCD_Type &n)//O(n) + { + int i, carry = 0; + for (i = 0; i < MAX_SIZE; i++) + { + data[i] += n.data[i] + carry, + carry = data[i] / MAX_VALUE, + data[i] %= MAX_VALUE; + } + return *this; + } + + void print() + { + int i = get_left(); + printf("%u", data[i]); + while (--i >= 0) + { + printf("%0" MAX_DIGIT "u", data[i]); + } + } +}; + +bool ok[1024][1024];//init=false +double val[1024][1024]; +double p, q; + +void P(int i, int j) +{ + if (ok[i][j]) + { + return; + } + ok[i][j] = true; + if (i == 0) + { + val[i][j] = 1; + return; + } + if (j == 0) + { + val[i][j] = 0; + return; + } + P(i - 1, j); + P(i, j - 1); + val[i][j] = p * val[i - 1][j] + q * val[i][j - 1]; + return; +} + +BCD_Type recur[2][1024]; + +struct InputStruct +{ + double p; + vector pos; +}; + +vector input; +map res; + +int main() +{ + memset(ok, 0, 1024 * 1024); + int maxx = 1, maxy = 1; + char line[128]; + while (gets(line)) + { + InputStruct t; + t.p = atof(strtok(line, " \t")); + int n = atoi(strtok(NULL, " \t")); + if (n == 0) + { + continue; + } + while (--n >= 0) + { + int y, x; + gets(line); + char *yy = strtok(line, " \t"); + char *xx = strtok(NULL, " \t"); + y = atoi(yy); + x = atoi(xx); + maxx = max(maxx, x); + maxy = max(maxy, y); + t.pos.push_back((y << 16) | x); + ok[y][x] = true; + } + input.push_back(t); + } + + // init mk tbl + BCD_Type one(1); + recur[0][0] = one; + for (int x = 1; x <= maxx; x++) + { + recur[0][x] = one; + } + int now = 1, prev = 0; + for (int y = 1; y <= maxy; y++) + { + recur[now][0] = one; + for (int x = 1; x <= maxx; x++) + { + recur[now][x] = recur[prev][x]; + recur[now][x] += recur[now][x - 1]; + if (ok[y][x]) + { + res[(y << 16) | x] = recur[now][x]; + } + recur[now][x] += 1; + } + now ^= 1, prev ^= 1; + } + + // proc input + for (int c = 0; c < input.size(); c++) + { + memset(ok, 0, 1024 * 1024); + p = input[c].p; + q = 1 - p; + if (c) + { + printf("\n"); + } + vector &pos = input[c].pos; + for (int n = 0; n < pos.size(); n++) + { + if (pos[n] == 0) + { + printf("-1.00000\n0\n"); + continue; + } + int i = pos[n] >> 16, j = pos[n] & 0xffff; + if (i == 0) + { + printf("1.00000\n0\n"); + continue; + } + if (j == 0) + { + printf("0.00000\n0\n"); + continue; + } + P(i, j); + printf("%.5f\n", val[i][j]); + res[pos[n]].print(); + printf("\n"); + } + } + return 0; +} diff --git a/10208.cpp b/10208.cpp new file mode 100644 index 0000000..4135c9d --- /dev/null +++ b/10208.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define ex 2.7182818284590452354 +#define pi acos(-1.0) +#define inf 0x3fffffff +#define DC(n) printf("Case #%d:", ++n) +#define SD(n) scanf("%d", &n) +#define SS(str) scanf("%s", str) +#define SDB(n) scanf("%lf", &n) +#define ll long long +#define mm 1000000007 +#define mmax 10000010 + +bool fg[mmax]; + +void pre() +{ + memset(fg, 0, sizeof fg); + for (int i = 2; i * i < mmax; i++) + { + if (!fg[i]) + { + for (int j = i * i; j < mmax; j += i) + { + fg[j] = 1; + } + } + } +} +int ans[60]; +int getcnt(int x, int n) +{ + int ans = 0; + while (n) + { + ans += n / x; + n /= x; + } + return ans; +} + +int main() +{ + pre(); + int n, ca = 0; + string str; + while (cin >> str) + { + if (ca) + { + printf("\n"); + } + ca++; + char c = str[str.size() - 1]; + n = 0; + bool ft = 0; + if (c != '!') + { + for (int i = 0; i < str.size(); i++) + { + n = 10 * n + str[i] - '0'; + } + for (int i = 0; i * i <= n; i++) + { + int d = n - i * i; + int tmp = sqrt(d); + if (tmp * tmp == d) + { + printf("He might be honest.\n"); + ft = 1; + break; + } + } + if (!ft) + { + printf("He is a liar.\n"); + } + } + if (c == '!') + { + for (int i = 0; i < str.size() - 1; i++) + { + n = 10 * n + str[i] - '0'; + } + int cnt = 0; + for (int i = 3; i <= n; i++) + { + if ((i % 4 == 3) && (!fg[i])) + { + int num = getcnt(i, n); + if (num & 1) + { + ft = 1; + ans[cnt++] = i; + if (cnt >= 50) + { + break; + } + } + } + } + if (!ft) + { + printf("He might be honest.\n"); + } + else + { + printf("He is a liar.\n"); + for (int i = 0; i < cnt; i++) + { + printf("%d%c", ans[i], i == cnt - 1 ? '\n' : ' '); + } + } + } + } + return 0; +} diff --git a/10209.cpp b/10209.cpp new file mode 100644 index 0000000..1d15624 --- /dev/null +++ b/10209.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + double a, area1, area2; + while (scanf("%lf", &a) == 1) + { + area1 = (M_PI / 3.0 - sqrt(3) + 1.0) * a * a; + area2 = (M_PI / 3.0 + 2.0 * sqrt(3) - 4.0) * a * a; + printf("%.3lf %.3lf %.3lf\n", area1, area2, a * a - area1 - area2); + } + return 0; +} diff --git a/10210.cpp b/10210.cpp new file mode 100644 index 0000000..da34a3a --- /dev/null +++ b/10210.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +typedef long double elem; +typedef complex point, vec; + +const elem pi = acos(-1.0); + +elem rad(elem deg) +{ + return (deg / 180) * pi; +} +elem dist(point a, point b) +{ + return abs(b - a); +} +elem cross(vec a, vec b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} +vec uvec(vec v) +{ + return v / abs(v); +} +vec norml(vec v) +{ + return v * vec(0, 1); +} +vec normr(vec v) +{ + return v * vec(0, -1); +} +vec unorml(vec v) +{ + return uvec(norml(v)); +} +vec unormr(vec v) +{ + return uvec(normr(v)); +} +vec rot(vec v, elem deg) +{ + return v * exp(point(0, rad(deg))); +} + +point intersection(point a1, point a2, point b1, point b2) +{ + vec a = a2 - a1; + vec b = b2 - b1; + return a1 + (cross(b, b1 - a1) / cross(b, a)) * (a2 - a1); +} + +elem RomeoAndJuliet(point a, point b, elem CMD, elem ENF) +{ + elem ret = 0; + vec ba = a - b; + vec uam = unormr(ba); + vec ubm = rot(uam, CMD); + vec uan = unorml(ba); + vec ubn = rot(uan, -ENF); + point M = intersection(a, a + uam, b, b + ubm); + point N = intersection(a, a + uan, b, b + ubn); + ret = max(ret, dist(M, N)); + uan = uvec(rot(vec(b - a), -CMD)); + ubn = uvec(rot(vec(b - a), -(CMD + ENF))); + N = intersection(a, a + uan, b, b + ubn); + ret = max(ret, dist(N, a)); + return ret; +} + +int main() +{ + while (true) + { + elem x1, y1, x2, y2, CMD, ENF; + if (6 != scanf("%Lf%Lf%Lf%Lf%Lf%Lf", &x1, &y1, &x2, &y2, &CMD, &ENF)) + { + break; + } + printf("%.3Lf\n", RomeoAndJuliet(point(x1, y1), point(x2, y2), CMD, ENF)); + } + return 0; +} diff --git a/10211.cpp b/10211.cpp new file mode 100644 index 0000000..7b30c48 --- /dev/null +++ b/10211.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int main() +{ + int T, B, D; + scanf("%d", &T); + while (T-- > 0 && scanf("%d %d", &B, &D) == 2) + { + assert(2 <= B && B <= 500 && 2 <= D && D <= 5000); + int found = 0; + for (int step = 0; step < 3; step++) + { + int z; + char *fmt; + if (step == 0) + { + z = 0, fmt = "Rightmost %d\n"; + } + else if (step == 1) + { + z = 1, fmt = "Add all %d\n"; + } + else + { + z = D - 1, fmt = "Alternate %d change sign\n"; + } + for (int r = 1, x = 1; r <= D + 10; r++) + { + if (step > 0 && r > 1000) + { + break; + } + x = (x * B) % D; + if (x == z) + { + found++; + printf(fmt, r); + break; + } + } + } + if (found == 0) + { + printf("condition not found.\n"); + } + if (T > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/10212.cpp b/10212.cpp new file mode 100644 index 0000000..214e1c9 --- /dev/null +++ b/10212.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +#define MN(a, b) (a > b ? b : a) +#define MAXN 20000000 +#define MAX 1270649 + +typedef long long ss; + +char Temp[MAXN + 2]; +ss Prime[10]; +int Store[MAX], N, M, K, P; +int Tw[] = {6, 2, 4, 8}; +int Th[] = {1, 3, 9, 7}; +int Fo[] = {6, 4}; +int Se[] = {1, 7, 9, 3}; +int Ei[] = {6, 8, 4, 2}; +int Ni[] = {1, 9}; + +void Prime_table() +{ + int i, j, max = 0; + for (i = 2; i * i <= MAXN;) + { + for (j = i + i; j <= MAXN; j += i) + Temp[j] = 1; + for (++i; Temp[i]; i++) + ; + } + for (i = 2; i <= MAXN; i++) + if (!Temp[i]) + Store[K++] = i; +} + +int LastD(int b, int g) +{ + int r; + if (g == 0) + return 1; + switch (b) + { + case 0: + return 0; + case 1: + return 1; + case 2: + r = g % 4; + return Tw[r]; + case 3: + r = g % 4; + return Th[r]; + case 4: + r = g % 2; + return Fo[r]; + case 5: + return 5; + case 6: + return 6; + case 7: + r = g % 4; + return Se[r]; + case 8: + r = g % 4; + return Ei[r]; + case 9: + r = g % 2; + return Ni[r]; + } + return -1; +} + +void Times(int n) +{ + int i; + ss j; + for (i = 0; i < 3; i++) + { + for (j = Store[i]; j <= N; j *= Store[i]) + { + Prime[i] += N / j; + Prime[i] -= n / j; + } + } +} + +int Binary(int key) +{ + int lo = 0, up = K - 1, mid; + mid = up / 2; + if (key > Store[K - 1]) + return K - 1; + while (Store[mid] != key) + { + if (Store[mid] > key) + { + if (Store[mid - 1] < key) + return mid - 1; + up = mid - 1; + } + else if (Store[mid] < key) + { + if (Store[mid + 1] > key) + return mid; + lo = mid + 1; + } + mid = (lo + up) / 2; + } + return mid; +} + +void CalCulate(int n) +{ + ss i, t, j; + int a[12] = {0}, ind1, k = 1; + if (n == N) + { + printf("1\n"); + return; + } + t = MN(Prime[0], Prime[2]); + Prime[0] -= t; + Prime[2] -= t; + for (i = 0; i <= 2; i++) + { + k *= LastD(Store[i] % 10, Prime[i]); + k %= 10; + } + if (N <= 6) + { + printf("%d\n", k); + return; + } + for (i = 3; Store[i] <= N && i < K; i++) + { + t = 0; + for (j = Store[i]; j <= N; j *= Store[i]) + { + t += N / j; + t -= n / j; + } + if (t == 1) + { + k *= Store[i] % 10; + k %= 10; + } + else if (t > 1) + { + k *= LastD(Store[i] % 10, t); + k %= 10; + } + } + printf("%d\n", k % 10); +} + +int main() +{ + Prime_table(); + while (scanf("%d%d", &N, &M) == 2) + { + Times(N - M); + CalCulate(N - M); + for (int i = 0; i < 10; i++) + Prime[i] = 0; + } + return 0; +} diff --git a/10213.cpp b/10213.cpp new file mode 100644 index 0000000..b86c896 --- /dev/null +++ b/10213.cpp @@ -0,0 +1,336 @@ +#include + +using namespace std; + +#define MAXDIGITS 1000 /* maximum length bignum */ +#define PLUS 1 /* positive sign bit */ +#define MINUS -1 /* negative sign bit */ + +typedef struct +{ + char digits[MAXDIGITS]; /* represent the number */ + int signbit; /* 1 if positive, -1 if negative */ + int lastdigit; /* index of high-order digit */ +} bignum; +void int_to_bignum(int s, bignum *n); +void initialize_bignum(bignum *n); +int add_bignum(bignum *a, bignum *b, bignum *c); +int compare_bignum(bignum *a, bignum *b); +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} +void int_to_bignum(int s, bignum *n) +{ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + int t = abs(s); + sprintf(n->digits, "%d", t); + n->lastdigit = strlen(n->digits); +} +void int_to_bignum(char *s, bignum *n) +{ + int i; + if (s[0] != -1) + { + n->signbit = PLUS; + i = 0; + } + else + { + i = 1; + n->signbit = MINUS; + } + strcpy(n->digits, &s[i]); + n->lastdigit = strlen(n->digits); +} +void initialize_bignum(bignum *n) +{ + int_to_bignum(0, n); +} +int compare_bignum(bignum *a, bignum *b) +{ + int i; /* counter */ + if ((a->signbit == MINUS) && (b->signbit == PLUS)) + { + return (PLUS); + } + if ((a->signbit == PLUS) && (b->signbit == MINUS)) + { + return (MINUS); + } + if (b->lastdigit > a->lastdigit) + { + return (PLUS * a->signbit); + } + if (a->lastdigit > b->lastdigit) + { + return (MINUS * a->signbit); + } + for (i = 0; i < a->lastdigit; i++) + { + if (a->digits[i] > b->digits[i]) + { + return (MINUS * a->signbit); + } + if (b->digits[i] > a->digits[i]) + { + return (PLUS * a->signbit); + } + } + return (0); +} +int subtract_bignum(bignum *a, bignum *b, bignum *c) +{ + register int i, j, op = 0; + ; /* counter */ + int n_borrow; + int temp; + c->signbit = PLUS; + if ((a->signbit == MINUS) || (b->signbit == MINUS)) + { + b->signbit = -1 * b->signbit; + n_borrow = add_bignum(a, b, c); + b->signbit = -1 * b->signbit; + return n_borrow; + } + if (compare_bignum(a, b) == PLUS) + { + n_borrow = subtract_bignum(b, a, c); + c->signbit = MINUS; + return n_borrow; + } + int k = c->lastdigit = max(a->lastdigit, b->lastdigit); + n_borrow = 0; + c->digits[k--] = '\0'; + for (i = a->lastdigit - 1, j = b->lastdigit - 1; j >= 0; i--, j--) + { + temp = a->digits[i] - '0' - (b->digits[j] - '0' + op); + if (temp < 0) + { + temp += 10; + op = 1; + n_borrow++; + } + else + { + op = 0; + } + c->digits[k--] = temp + '0'; + } + while (op) + { + temp = a->digits[i--] - op - '0'; + if (temp < 0) + { + temp += 10; + op = 1; + n_borrow++; + } + else + { + op = 0; + } + c->digits[k--] = temp + '0'; + } + for (; i >= 0; i--) + { + c->digits[k--] = a->digits[i]; + } + for (i = 0; !(c->digits[i] - '0'); i++) + ; + c->lastdigit = c->lastdigit - i; + if (i == a->lastdigit) + { + strcpy(c->digits, "0"); + } + else + { + char string[MAXDIGITS]; + strcpy(string, &c->digits[i]); + strcpy(c->digits, string); + } + return n_borrow; +} +int add_bignum(bignum *a, bignum *b, bignum *c) +{ + int carry; /* carry digit */ + int i, j, op = 0; + ; /* counter */ + int n_carry; + initialize_bignum(c); + if (a->signbit == b->signbit) + { + c->signbit = a->signbit; + } + else + { + if (a->signbit == MINUS) + { + a->signbit = PLUS; + n_carry = subtract_bignum(b, a, c); + a->signbit = MINUS; + } + else + { + b->signbit = PLUS; + n_carry = subtract_bignum(a, b, c); + b->signbit = MINUS; + } + return n_carry; + } + if (a->lastdigit < b->lastdigit) + { + return add_bignum(b, a, c); + } + int k = c->lastdigit = a->lastdigit + 1; + c->digits[k--] = '\0'; + carry = 0; + n_carry = 0; + for (i = b->lastdigit - 1, j = a->lastdigit - 1; i >= 0; i--, j--) + { + carry = b->digits[i] - '0' + a->digits[j] - '0' + carry; + c->digits[k--] = (carry % 10) + '0'; + carry = carry / 10; + if (carry) + { + n_carry++; + } + } + for (; j >= 0; j--) + { + carry = a->digits[j] - '0' + carry; + c->digits[k--] = (carry % 10) + '0'; + carry = carry / 10; + if (carry) + { + n_carry++; + } + } + if (carry) + { + c->digits[k] = carry + '0'; + } + else + { + char string[MAXDIGITS]; + strcpy(string, &c->digits[1]); + strcpy(c->digits, string); + } + c->lastdigit = c->lastdigit - k - 1; + return n_carry; +} +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + long int n_d; + register long int i, j, k; + short int num1[MAXDIGITS], num2[MAXDIGITS], of = 0, res[MAXDIGITS] = {0}; + n_d = (a->lastdigit < b->lastdigit) ? b->lastdigit : a->lastdigit; + n_d++; + for (i = 0, j = a->lastdigit - 1; i < a->lastdigit; i++, j--) + { + num1[i] = a->digits[j] - 48; + } + for (i = 0, j = b->lastdigit - 1; i < b->lastdigit; j--, i++) + { + num2[i] = b->digits[j] - 48; + } + res[0] = 0; + for (j = 0; j < b->lastdigit; j++) + { + for (i = 0, k = j; i < a->lastdigit || of; k++, i++) + { + if (i < a->lastdigit) + { + res[k] += num1[i] * num2[j] + of; + } + else + { + res[k] += of; + } + of = res[k] / 10; + res[k] = res[k] % 10; + } + } + for (i = k - 1, j = 0; i >= 0; i--, j++) + { + c->digits[j] = res[i] + 48; + } + c->digits[j] = '\0'; + c->lastdigit = k; + c->signbit = a->signbit * b->signbit; +} +void copy(bignum *a, bignum *b) +{ + a->lastdigit = b->lastdigit; + a->signbit = b->signbit; + strcpy(a->digits, b->digits); +} +typedef long long ss; +void Combi(int N, int r, bignum *n) +{ + int a[10], j, k = 0, m, min; + int d; + ss i; + bignum n1, n2; + char dummy[100]; + m = max(r, N - r); + min = N - m; + for (i = m + 1; i <= N; i++) + { + a[k++] = i; + } + for (i = 2; i <= min; i++) + { + m = i; + for (j = 0; j < k && m > 1; j++) + { + d = gcd(i, a[j]); + m /= d; + a[j] /= d; + } + } + int_to_bignum(1, &n1); + for (i = 0; i < k; i++) + { + int_to_bignum(a[i], &n2); + multiply_bignum(&n1, &n2, n); + copy(&n1, n); + } +} +void Cal(int N) +{ + bignum n1, n2, n3, n4; + Combi(N, 4, &n1); + Combi(N, 2, &n2); + int_to_bignum(1, &n3); + add_bignum(&n1, &n2, &n4); + add_bignum(&n4, &n3, &n1); + printf("%s\n", n1.digits); +} + +int main() +{ + int a[] = {1, 1, 2, 4, 8}; + int n, kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &n); + if (n <= 4) + { + printf("%d\n", a[n]); + } + else + { + Cal(n); + } + } + return 0; +} diff --git a/10214.cpp b/10214.cpp new file mode 100644 index 0000000..e760cdc --- /dev/null +++ b/10214.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXA = 2005; +const int MAXB = 2000005; + +int phi[MAXA + 5]; + +int gcd(int a, int b) +{ + return b == 0 ? a : gcd(b, a % b); +} + +int main() +{ + memset(phi, 0, sizeof(phi)); + phi[1] = 1; + for (int i = 2; i <= MAXA; i++) + { + if (!phi[i]) + { + for (int j = i; j <= MAXA; j += i) + { + if (!phi[j]) + { + phi[j] = j; + } + phi[j] = phi[j] / i * (i - 1); + } + } + } + + LL a, b; + while (scanf("%lld%lld", &a, &b) == 2) + { + if (!a && !b) + { + return 0; + } + LL ans = 0LL; + for (int i = 1; i <= a; i++) + { + int v = b / i; + ans += v * phi[i]; + for (int j = v * i + 1; j <= b; j++) + { + if (gcd(i, j) == 1) + { + ans++; + } + } + } + ans = 4 * ans + 4; + LL N = 4 * a * b + 2 * a + 2 * b; + printf("%.7lf\n", ans * 1.0f / N); + } + return 0; +} diff --git a/10215.cpp b/10215.cpp new file mode 100644 index 0000000..67489eb --- /dev/null +++ b/10215.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +int main() +{ + double l, w, x; + while (scanf("%lf %lf", &l, &w) != EOF) + { + // Calc x at maximum + x = (w + l - sqrt(w * w + l * l - w * l)) / 6.0; + printf("%.3f 0.000 ", x + EPS); + // Calc x at minimum + if (l > 0 & w > 0) + { + if (l < w) + { + printf("%.3f", l / 2.0 + EPS); + } + else + { + printf("%.3f", w / 2.0 + EPS); + } + } + printf("\n"); + } + return 0; +} diff --git a/10216.cpp b/10216.cpp new file mode 100644 index 0000000..a0d5dc9 --- /dev/null +++ b/10216.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define EPS std::numeric_limits::epsilon() + +int T; +double a, b, c; +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%lf%lf%lf", &a, &b, &c); + double s = 0.5 * (double)(a + b + c); + double S = sqrt(s * (s - a) * (s - b) * (s - c)); + // fermat point + double dm; + // a - BC; b - AC; c - AB + double gamma_A = acos((b * b + c * c - a * a) / (2.0 * b * c)); + double gamma_B = acos((a * a + c * c - b * b) / (2.0 * a * c)); + double gamma_C = acos((a * a + b * b - c * c) / (2.0 * a * b)); + if (gamma_A + EPS > M_PI * (2.0 / 3.0)) + { + dm = b + c; + } + else if (gamma_B + EPS > M_PI * (2.0 / 3.0)) + { + dm = a + c; + } + else if (gamma_C + EPS > M_PI * (2.0 / 3.0)) + { + dm = a + b; + } + else + { + dm = sqrt(a * a + b * b - 2 * a * b * cos(M_PI / 3.0 + gamma_C)); + } + double r = 2.0 * S / (a + b + c); + double ah = (c + b - a) / 2.0; + double ai = sqrt(ah * ah + r * r); + double bh = (c + a - b) / 2.0; + double bi = sqrt(bh * bh + r * r); + double ch = (a + b - c) / 2.0; + double ci = sqrt(ch * ch + r * r); + double di = ai + bi + ci; + double ma = sqrt((c * c) / 2 + (b * b) / 2 - (a * a) / 4); + double mb = sqrt((a * a) / 2 + (c * c) / 2 - (b * b) / 4); + double mc = sqrt((a * a) / 2 + (b * b) / 2 - (c * c) / 4); + double dg = (2.0 / 3.0) * (ma + mb + mc); + // circumcentre + double d0 = 3.0 * ((a * b * c) / (double)(4.0 * S)); + if (std::isinf(d0)) + { + d0 = -1.0; + } + if (a > b + c) + { + printf("-1.000 -1.000 -1.000 -1.000\n"); + } + else + { + printf("%.3lf %.3lf %.3lf %.3lf\n", dm, di, dg, d0); + } + } + return 0; +} diff --git a/10217.cpp b/10217.cpp new file mode 100644 index 0000000..01182ac --- /dev/null +++ b/10217.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int N; + int a = 1, b = 1, c; + double D, x; + while (scanf("%d", &N) == 1) + { + c = -N; + D = sqrt(b * b - 4 * a * c); + x = (-b + D) / 2 / a; + printf("%.2lf %d\n", x, (int)floor(x) + 1); + } + return 0; +} diff --git a/10218.cpp b/10218.cpp new file mode 100644 index 0000000..fb051da --- /dev/null +++ b/10218.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +const int MAX = 1001; +double dp[MAX][MAX]; +int m, w, c; + +int main() +{ + while (true) + { + scanf("%d %d %d", &m, &w, &c); + if (m == 0 && w == 0) + { + break; + } + double p = m / (double)(m + w); + memset(dp, 0.0, MAX * MAX); + dp[0][0] = 1.0; + dp[0][1] = 0.0; + for (int i = 0; i < c; ++i) + { + dp[i + 1][0] = dp[i][0] * (1 - p) + dp[i][1] * p; + dp[i + 1][1] = dp[i][0] * p + dp[i][1] * (1 - p); + } + printf("%.7lf\n", dp[c][0]); + } + return 0; +} diff --git a/10219.cpp b/10219.cpp new file mode 100644 index 0000000..7bf8c33 --- /dev/null +++ b/10219.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + long long n, k, i; + while (cin >> n >> k) + { + double digit = 0; + for (i = 1; i <= k; i++) + { + digit += (log(n - i + 1) - log(i)) / log(10); + } + cout << floor(digit) + 1 << endl; + } + return 0; +} diff --git a/10220.cpp b/10220.cpp new file mode 100644 index 0000000..a0265e6 --- /dev/null +++ b/10220.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +class BigInt +{ +public: + void ComputeSum(); + int p[3000]; + int len, sum; +} test[1010]; + +void BigInt::ComputeSum() +{ + sum = 0; + int i; + for (i = 0; i < len; i++) + { + sum += p[i]; + } +} + +void Compute(BigInt *f, int x) +{ + int i, k, g = 0; + for (i = 0; i < f[x - 1].len; i++) + { + k = f[x - 1].p[i] * x + g; + f[x].p[i] = k % 10; + g = k / 10; + } + if (g > 0) + while (g) + { + f[x].p[i] = g % 10; + g /= 10; + i++; + } + f[x].len = i; +} + +int main() +{ + test[0].p[0] = 1; + test[0].len = 1; + test[0].ComputeSum(); + int i, n; + for (i = 1; i <= 1002; i++) + { + Compute(test, i); + test[i].ComputeSum(); + } + while (cin >> n) + { + cout << test[n].sum << endl; + } + return 0; +} diff --git a/10221.cpp b/10221.cpp new file mode 100644 index 0000000..6a016ac --- /dev/null +++ b/10221.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const double pi = acos(-1); + +int main() +{ + double s, a; + char info[10]; + while (scanf("%lf %lf %s", &s, &a, info) == 3) + { + if (strcmp(info, "min") == 0) + { + a /= 60; + } + s += 6440.0; + if (a > 180) + { + a = 360 - a; + } + a = a * pi / 180.0; + printf("%.6lf %.6lf\n", a * s, sqrt(2 * s * s * (1 - cos(a)))); + } + return 0; +} diff --git a/10222.cpp b/10222.cpp index adfa3f1..bf6aa8a 100644 --- a/10222.cpp +++ b/10222.cpp @@ -1,22 +1,22 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - string master="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; - int max=master.length(); - string str; - while(getline(cin,str,'\n')){ - int len=str.length(),i,j; - for(i=0;i + +using namespace std; + +int main() +{ + string keys = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; + char c; + while (cin.get(c)) + { + if (c == '\n' || c == '\t' || c == ' ') + { + cout << c; + } + else + { + size_t found = keys.find(tolower(c)); + cout << keys[found - 2]; + } + } + return 0; +} diff --git a/10223.cpp b/10223.cpp index fb41b9e..dfc50b5 100644 --- a/10223.cpp +++ b/10223.cpp @@ -1,19 +1,65 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int catalan[]={0,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440,9694845,35357670,129644790,477638700,1767263190}; - int max=20; - int n; - while(scanf("%d",&n)!=EOF){ - for(int i=1;i<=20;i++){ - if(n==catalan[i]){ - printf("%d\n",i); - break; - } - } - } - return 0; -} +#include + +using namespace std; + +#define MAXN 25 +#define MAX 4294967295 + +int A[MAXN]; + +int Cal() +{ + int i, j; + int sum; + for (i = 3; i < MAXN; i++) + { + sum = 0; + for (j = 1; j < i; j++) + { + sum += A[j] * A[i - j]; + } + if (sum > MAX) + { + break; + } + A[i] = sum; + } + return 0; +} + +int main() +{ + int i; + int j; + A[0] = 0; + A[1] = A[2] = 1; + ; + Cal(); + while (scanf("%d", &i) == 1) + { + if (i == 0) + { + printf("1\n"); + continue; + } + if (i == 1) + { + printf("1\n"); + continue; + } + if (i == 2) + { + printf("2\n"); + continue; + } + for (j = 0; j <= 20; j++) + { + if (i == A[j]) + { + break; + } + } + printf("%d\n", j - 1); + } + return 0; +} diff --git a/10224.cpp b/10224.cpp new file mode 100644 index 0000000..f41b4ea --- /dev/null +++ b/10224.cpp @@ -0,0 +1,237 @@ +#include + +using namespace std; + +double PI = 2 * acos(0.0); +double INF = 1.0 / 0.0; +double EPS = 1e-9; + +struct Point +{ + double x, y; +}; + +struct Circle +{ + Point o; + double r; + vector arc; +} C[20]; + +Point V[512]; +double adj[512][512]; +int N, K; + +double dist(const Point &a, const Point &b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +double cross(const Point &o, const Point &a, const Point &b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} + +bool visible(const Point &a, const Point &b) +{ + if (dist(a, b) < EPS) + { + return true; + } + for (int i = 0; i < K; i++) + { + const Circle &c = C[i]; + const Point &o = c.o; + double h = fabs(cross(a, b, o)) / dist(a, b); + if (h < c.r - EPS) + { + double t = (o.x - a.x) * (b.x - a.x) + (o.y - a.y) * (b.y - a.y); + double s = (o.x - b.x) * (a.x - b.x) + (o.y - b.y) * (a.y - b.y); + if (t > EPS && s > EPS) + { + return false; + } + } + } + return true; +} + +void buildGraph() +{ + // Line of sight between source and destination + if (visible(V[0], V[1])) + { + adj[0][1] = adj[1][0] = dist(V[0], V[1]); + return; + } + // Tangents from source/destination to each circle + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < K; j++) + { + Point p = V[i]; + Circle c = C[j]; + double alpha = atan2(p.y - c.o.y, p.x - c.o.x); + double delta = acos(c.r / dist(p, c.o)); + for (int sign = -1; sign <= 1; sign += 2) + { + Point q; + q.x = c.o.x + c.r * cos(alpha + sign * delta); + q.y = c.o.y + c.r * sin(alpha + sign * delta); + //printf("(%.2f, %.2f) -> circle %.2f, %.2f, r=%.2f: tangent point %.2f %.2f\n",p.x, p.y, c.o.x, c.o.y, c.r, q.x, q.y); + if (visible(p, q)) + { + C[j].arc.push_back(N); + V[N] = q; + adj[i][N] = adj[N][i] = dist(p, q); + N++; + } + } + } + } + // Tangent points at circles + for (int ci = 0; ci < K; ci++) + { + for (int cj = 0; cj < K; cj++) + { + if (ci == cj) + { + continue; + } + Point A = C[ci].o; + Point B = C[cj].o; + double phi = atan2(B.y - A.y, B.x - A.x); + for (int sign = -1; sign <= 1; sign += 2) + { + double cs = (C[ci].r + sign * C[cj].r) / dist(A, B); + if (cs > 1.0) + { + cs = 1.0; + } + if (cs < -1.0) + { + cs = -1.0; + } + double delta = acos(cs); + for (int sign2 = -1; sign2 <= 1; sign2 += 2) + { + Point P; + P.x = A.x + C[ci].r * cos(phi + sign2 * delta); + P.y = A.y + C[ci].r * sin(phi + sign2 * delta); + C[ci].arc.push_back(N); + V[N++] = P; + //printf("circle %.2f, %.2f, r=%.2f: tangent point %.2f, %.2f\n", C[ci].o.x, C[ci].o.y, C[ci].r, P.x, P.y); + if (fabs(delta) < EPS) + { + break; + } + } + } + } + } + // Tangents between circles + for (int i = 2; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (visible(V[i], V[j])) + { + // printf("%.2f, %.2f - %.2f, %.2f\n", V[i].x, V[i].y, V[j].x, V[j].y); + adj[i][j] = adj[j][i] = dist(V[i], V[j]); + } + } + } + // Circular segments + for (int i = 0; i < K; i++) + { + int *a = &C[i].arc[0]; + int m = C[i].arc.size(); + for (int j = 0; j < m; j++) + { + for (int k = j + 1; k < m; k++) + { + double t = (V[a[j]].x - C[i].o.x) * (V[a[k]].x - C[i].o.x); + t += (V[a[j]].y - C[i].o.y) * (V[a[k]].y - C[i].o.y); + t /= C[i].r * C[i].r; + if (t < -1) + { + t = -1; + } + if (t > 1) + { + t = 1; + } + t = acos(t); + // printf("%.2f, %.2f - %.2f, %.2f: circular segment %.5f\n", + // V[a[j]].x, V[a[j]].y, + // V[a[k]].x, V[a[k]].y, t*C[i].r); + adj[a[j]][a[k]] = adj[a[k]][a[j]] = t * C[i].r; + } + } + } + for (int i = 0; i < N; i++) + { + adj[i][i] = 0; + } +} + +double dijkstra() +{ + double dist[512]; + int seen[512]; + memset(seen, 0, sizeof(seen)); + for (int i = 0; i < N; i++) + { + dist[i] = INF; + } + dist[0] = 0; + for (;;) + { + int u = -1; + for (int i = 0; i < N; i++) + if (seen[i] == 0 && (u < 0 || dist[i] < dist[u])) + { + u = i; + } + if (u < 0 || u == 1) + { + break; + } + seen[u] = 1; + for (int v = 0; v < N; v++) + { + dist[v] = min(dist[v], dist[u] + adj[u][v]); + } + } + return dist[1]; +} + +int main() +{ + int T; + scanf("%d", &T); + N = 512; + for (int cs = 1; cs <= T; cs++) + { + if (cs != 1) + { + printf("\n"); + } + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + adj[i][j] = INF; + } + scanf("%d %lf %lf %lf %lf", &K, &V[0].x, &V[0].y, &V[1].x, &V[1].y); + N = 2; + for (int i = 0; i < K; i++) + { + scanf("%lf %lf %lf", &C[i].o.x, &C[i].o.y, &C[i].r); + C[i].r /= 2; + C[i].arc.clear(); + } + buildGraph(); + printf("%.2f\n", dijkstra() * 3600.0 / 200.0); + } + return 0; +} diff --git a/10225.cpp b/10225.cpp new file mode 100644 index 0000000..7f4350a --- /dev/null +++ b/10225.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +struct data +{ + int i; + long long int mod; +} arr[65536]; + +bool flag; +long long int P, B, N, L, m; + +long long int mods(long long int base, long long int exp, long long int P) +{ + if (exp == 1) + { + return (long long int)(base % P); + } + else if (exp == 0) + { + return (long long int)1; + } + else if (exp % 2) + { + long long int t = mods(base, exp - 1, P); + return (long long int)((t * base) % P); + } + else + { + long long int t = mods(base, exp / 2, P); + return (long long int)((t * t) % P); + } +} + +int compare(const void *a, const void *b) +{ + return (*(data *)a).mod - (*(data *)b).mod; +} + +// binary search +int binarysearch(long long int key, int left, int right) +{ + long long int k; + int l, mid = (left + right) / 2, j; + while (left <= right) + { + if (arr[mid].mod == key) + { + k = arr[mid].i; + l = mid; + for (j = mid - 1; arr[j].mod == key; j--) + if (arr[j].i < k) + { + k = arr[j].i; + l = j; + } + for (j = mid + 1; arr[j].mod == key; j++) + if (arr[j].i < k) + { + k = arr[j].i; + l = j; + } + return l; + } + else if (arr[mid].mod < key) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + mid = (left + right) / 2; + } + return -1; +} + +int main() +{ + int i, j; + long long int tt; + while (scanf("%lld%lld%lld", &P, &B, &N) == 3) + { + m = (int)sqrt(P - 1); + if (m == 1) + { + m = P; + } + for (i = 0; i < m; i++) + { + arr[i].i = i; + arr[i].mod = mods(B, m * i, P); + } + qsort(arr, m, sizeof(data), compare); + unsigned long k = 1000000000UL; + for (j = 0; j < m; j++) + { + tt = (long long int)((N * mods(B, P - 1 - j, P))) % P; + int t = binarysearch(tt, 0, m - 1); + if (t != -1) + { + if ((m * arr[t].i + j) % (P - 1) < k) + { + k = (m * arr[t].i + j) % (P - 1); + } + flag = false; + } + } + + if (flag || k == 1000000000) + printf("no solution\n"); + else + printf("%lu\n", k); + } + return 0; +} diff --git a/10226.cpp b/10226.cpp index 3e4846b..0c893c9 100644 --- a/10226.cpp +++ b/10226.cpp @@ -1,64 +1,83 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define MAXN 10001 + +struct Tree +{ + char name[31]; + int cnt = 0; +} species[MAXN], a_tree; +Tree *the_tree; + +char input[100]; +int total, N; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +int cmp(const void *a, const void *b) +{ + Tree *t1 = (Tree *)a; + Tree *t2 = (Tree *)b; + return strcmp(t1->name, t2->name); +} +int cmp_t(const Tree &t1, const Tree &t2) +{ + return strcmp(t1.name, t2.name) < 0; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +int main() +{ int T; - scanf("%d\n", &T); - while (T--){ - string str; - map Tree; - map::iterator it; - double tot = 0; - while (true){ - getline(cin, str); - if (str == "") + scanf("%d", &T); + gets(input); + gets(input); + while (T--) + { + total = 0; + N = 0; + while (gets(a_tree.name)) + { + for (int i = 0; a_tree.name[i]; i++) + { + if (a_tree.name[i] == '\n') + { + a_tree.name[i] = '\0'; + } + } + if (strlen(a_tree.name) < 1) + { break; - Tree[str] += 1.0; - tot += 1.0; + } + + N++; + + the_tree = (Tree *)bsearch(&a_tree, species, total, sizeof(species[0]), cmp); + if (the_tree != NULL) + { + the_tree->cnt++; + } + else + { + strcpy(species[total].name, a_tree.name); + species[total].cnt++; + total++; + sort(species, species + total, cmp_t); + } + } + + for (int i = 0; i < total; i++) + { + printf("%s %.4lf\n", species[i].name, (species[i].cnt * 100.0f) / (double)N); } - tot /= 100.0; - for (it = Tree.begin(); it != Tree.end(); it++) - printf("%s %.4lf\n", ((*it).first).c_str(), ((*it).second) / tot); + if (T) - cout << endl; + { + printf("\n"); + for (int i = 0; i < MAXN; i++) + { + species[i].cnt = 0; + } + } } return 0; } - diff --git a/10227.cpp b/10227.cpp new file mode 100644 index 0000000..f672527 --- /dev/null +++ b/10227.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + set fallen_trees[100]; + void combine(int P); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +void UnionFindDisjointSets::combine(int P) +{ + rep(i, P) + { + REP(j, i + 1, P) + { + if (!isSameSet(i, j)) + { + vector diff; + set_symmetric_difference(fallen_trees[i].begin(), fallen_trees[i].end(), fallen_trees[j].begin(), fallen_trees[j].end(), back_inserter(diff)); + if (diff.empty()) + { + unionSet(i, j); + } + } + } + } +} + +int main() +{ + int t, person, tree, highest_person_number, P, T; + cin >> t; + while (t--) + { + cin >> P >> T; + cin.ignore(100, '\n'); + UnionFindDisjointSets ds(P); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> person >> tree; + cin.ignore(100, '\n'); + ds.fallen_trees[person - 1].insert(tree); + } + ds.combine(P); + cout << ds.numDisjointSets() << endl; + if (t) + { + cout << endl; + } + } + return 0; +} diff --git a/10228.cpp b/10228.cpp new file mode 100644 index 0000000..9b0158e --- /dev/null +++ b/10228.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +const int maxn = 110; +const double eps = 1e-6; + +inline double sqr(double a) +{ + return a * a; +} +inline double dis(double x1, double y1, double x2, double y2) +{ + return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); +} +double x[maxn], y[maxn]; +int n; +double f(double x0, double y0) +{ + int i; + double tot = 0; + for (i = 0; i < n; i++) + { + tot += dis(x0, y0, x[i], y[i]); + } + return tot; +} +double countdfx(double x0, double y0) +{ + int i; + double tot = 0; + for (i = 0; i < n; i++) + { + tot += (x0 - x[i]) / dis(x0, y0, x[i], y[i]); + } + return tot; +} +double countdfy(double x0, double y0) +{ + int i; + double tot = 0; + for (i = 0; i < n; i++) + { + tot += (y0 - y[i]) / dis(x0, y0, x[i], y[i]); + } + return tot; +} +double countdfxx(double x0, double y0) +{ + int i; + double tot = 0; + for (i = 0; i < n; i++) + { + tot += sqr(y0 - y[i]) / pow(dis(x0, y0, x[i], y[i]), 3); + } + return tot; +} +double countdfyy(double x0, double y0) +{ + int i; + double tot = 0; + for (i = 0; i < n; i++) + { + tot += sqr(x0 - x[i]) / pow(dis(x0, y0, x[i], y[i]), 3); + } + return tot; +} +double work() +{ + const double maxl = 1e5; + double x0 = maxl, y0 = maxl; + int i; + for (i = x0 = y0 = 0.0; i < n; i++) + { + x0 += x[i], y0 += y[i]; + } + x0 /= n; + y0 /= n; + while (1) + { + double xp = x0, yp = y0; + for (i = 0; i < n; i++) + if (dis(x0, y0, x[i], y[i]) < eps) + { + x0 += eps; + y0 += eps; + } + double xd = countdfx(xp, yp), yd = countdfy(xp, yp); + double xdd = countdfxx(xp, yp), ydd = countdfyy(xp, yp); + x0 -= xd / xdd; + y0 -= yd / ydd; + if (f(x0, y0) > f(xp, yp)) + { + x0 = xp; + y0 = yp; + break; + } + if (dis(x0, y0, xp, yp) < eps) + { + break; + } + } + return f(x0, y0); +} + +int T; +int main() +{ + scanf("%d", &T); + while (T--) + { + int i; + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%lf%lf", &x[i], &y[i]); + } + printf("%.0lf\n", work()); + if (T > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/10229.cpp b/10229.cpp index 9784903..70d9d3b 100644 --- a/10229.cpp +++ b/10229.cpp @@ -1,74 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +long long n, m, myMode; -int64[][2] multiply(int64 A[2][2], int64 B[2][2], int64 MOD){ - int64 M[2][2]; - M[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % MOD; - M[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % MOD; - M[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % MOD; - M[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % MOD; - return M; -} - -int64[][2] power(int64 N, int64 MOD){ - int64 F[2][2] = {{1, 1}, {1, 0}}; - int64 A[2][2]; - if (N == 0) - return A; - if (N == 1) - return F; - else if (N & 1) - return multiply(F, power(N - 1, MOD), MOD); - else{ - int64 M[][] = power(N / 2); - return multiply(M, M, MOD); +// (a * b + c) % d = ((a % d) * (b % d) + c % d) %d +long long Divide_Conquer_Fib(long long n) +{ + long long i, j, h, k, t; + i = h = 1; + j = k = 0; + while (n > 0) + { + if (n % 2 == 1) + { + // if n is odd + t = ((j % myMode) * (h % myMode)) % myMode; + j = ((i % myMode) * (h % myMode) + (j % myMode) * (k % myMode) + (t % myMode)) % myMode; + i = ((i % myMode) * (k % myMode) + (t % myMode)) % myMode; + // cout<> N >> M; - int64 MOD = 1 << M; - cout << power(B, N, MOD)[0][1] % MOD << endl; +int main() +{ + while (cin >> n >> m) + { + myMode = (long long)pow(2, m); + cout << Divide_Conquer_Fib(n) % myMode << endl; } return 0; } - diff --git a/10230.cpp b/10230.cpp new file mode 100644 index 0000000..dce11b3 --- /dev/null +++ b/10230.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +int diry[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dirx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; + +int map_[1024][1024], n, c, r, color, cnt; + +int select(int r, int c) +{ + bool used[26]; + int tr, tc, i; + for (i = 0; i < 26; i++) + { + used[i] = false; + } + for (i = 0; i < 8; i++) + { + tr = r + diry[i]; + tc = c + dirx[i]; + if (tr >= 0 && tr < n && tc >= 0 && tc < n && map_[tr][tc] >= 'a' && map_[tr][tc] <= 'z') + { + used[map_[tr][tc] - 'a'] = true; + } + } + for (i = 0; i < 8; i++) + { + tr = r + diry[i] - 1; + tc = c + dirx[i]; + if (tr >= 0 && tr < n && tc >= 0 && tc < n && map_[tr][tc] >= 'a' && map_[tr][tc] <= 'z') + { + used[map_[tr][tc] - 'a'] = true; + } + } + for (i = 0; i < 8; i++) + { + tr = r + diry[i]; + tc = c + dirx[i] - 1; + if (tr >= 0 && tr < n && tc >= 0 && tc < n && map_[tr][tc] >= 'a' && map_[tr][tc] <= 'z') + { + used[map_[tr][tc] - 'a'] = true; + } + } + for (i = 0; i < 8; i++) + { + tr = r + diry[i] - 1; + tc = c + dirx[i] - 1; + if (tr >= 0 && tr < n && tc >= 0 && tc < n && map_[tr][tc] >= 'a' && map_[tr][tc] <= 'z') + { + used[map_[tr][tc] - 'a'] = true; + } + } + for (i = 0; i < 26; i++) + if (!used[i]) + { + return i; + } +} +void paint(int sr, int sc, int dr, int dc, int size) +{ + if (size == 1) + { + return; + } + int s = size / 2; + int c = cnt++; + char color; + if (dr < sr + s && dc < sc + s) + { + paint(sr, sc, dr, dc, s); + } + else + { + map_[sr + s - 1][sc + s - 1] = c; + paint(sr, sc, sr + s - 1, sc + s - 1, s); + } + if (dr < sr + s && dc >= sc + s) + { + paint(sr, sc + s, dr, dc, s); + } + else + { + map_[sr + s - 1][sc + s] = c; + paint(sr, sc + s, sr + s - 1, sc + s, s); + } + if (dr >= sr + s && dc < sc + s) + { + paint(sr + s, sc, dr, dc, s); + } + else + { + map_[sr + s][sc + s - 1] = c; + paint(sr + s, sc, sr + s, sc + s - 1, s); + } + if (dr >= sr + s && dc >= sc + s) + { + paint(sr + s, sc + s, dr, dc, s); + } + else + { + map_[sr + s][sc + s] = c; + paint(sr + s, sc + s, sr + s, sc + s, s); + } + color = select(sr + s, sc + s) + 'a'; + if (map_[sr + s - 1][sc + s - 1] == c) + { + map_[sr + s - 1][sc + s - 1] = color; + } + if (map_[sr + s - 1][sc + s] == c) + { + map_[sr + s - 1][sc + s] = color; + } + if (map_[sr + s][sc + s - 1] == c) + { + map_[sr + s][sc + s - 1] = color; + } + if (map_[sr + s][sc + s] == c) + { + map_[sr + s][sc + s] = color; + } +} + +int main() +{ + int i, j; + while (scanf("%d%d%d", &n, &c, &r) == 3) + { + memset(map_, 0, sizeof(map_)); + map_[r - 1][c - 1] = '*'; + n = (int)pow(2, n); + cnt = 0; + paint(0, 0, r - 1, c - 1, n); + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + printf("%c", map_[i][j]); + } + printf("\n"); + } + printf("\n"); + } + return 0; +} diff --git a/10231.cpp b/10231.cpp new file mode 100644 index 0000000..5b0f525 --- /dev/null +++ b/10231.cpp @@ -0,0 +1,234 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < (int)n; i++) +#define rep(i, n) REP(i, 0, n) + +const int INF = 1000000000; +const int N = 30; +const int M = 12; +const int dx[] = {0, 1, 0, -1}; +const int dy[] = {-1, 0, 1, 0}; + +int H, W; +char t[N][N]; +bool visited[N][N]; +int roboCost[N][N], edge[N][N], man, node, rCost[M]; + +class State +{ +public: + int cost, y, x; +}; + +bool invalid(int y, int x) +{ + if (y >= H || x >= W) + { + return true; + } + if (y < 0 || x < 0) + { + return true; + } + if (t[y][x] == '#') + { + return true; + } + return false; +} + +void BFS1(int sy, int sx) +{ + rep(i, N) rep(j, N) visited[i][j] = false; + visited[sy][sx] = true; + queue Q; + for (Q.push((State){ + 0, sy, sx}); + !Q.empty(); Q.pop()) + { + State now = Q.front(); + int nextCost = now.cost + 1; + rep(d, 4) + { + int nx = now.x + dx[d]; + int ny = now.y + dy[d]; + if (invalid(ny, nx)) + { + continue; + } + if (visited[ny][nx]) + { + continue; + } + visited[ny][nx] = true; + roboCost[ny][nx] = min(roboCost[ny][nx], nextCost); + Q.push((State){ + nextCost, ny, nx}); + } + } +} + +int BFS2(int sy, int sx, int gy, int gx) +{ + rep(i, H) rep(j, W) visited[i][j] = false; + visited[sy][sx] = true; + queue Q; + for (Q.push((State){ + 0, sy, sx}); + !Q.empty(); Q.pop()) + { + State now = Q.front(); + int nextCost = now.cost + 1; + rep(d, 4) + { + int nx = now.x + dx[d]; + int ny = now.y + dy[d]; + if (invalid(ny, nx)) + { + continue; + } + if (visited[ny][nx]) + { + continue; + } + visited[ny][nx] = true; + if (ny == gy && nx == gx) + { + return nextCost; + } + Q.push((State){ + nextCost, ny, nx}); + } + } + return INF; +} + +void makeGraph() +{ + int X[M], Y[M]; + rep(i, N) rep(j, N) roboCost[i][j] = INF; + rep(i, M) rep(j, M) edge[i][j] = INF; + node = 0; + man = -1; + rep(i, H) + { + rep(j, W) + { + if (t[i][j] == 'X') + { + BFS1(i, j); + } + } + } + rep(i, H) + { + rep(j, W) + { + if (t[i][j] != 'O' && t[i][j] != '*') + { + continue; + } + if (t[i][j] == 'O') + { + man = node; + } + X[node] = j; + Y[node] = i; + rCost[node] = roboCost[i][j]; + node++; + } + } + rep(i, node) + { + REP(j, i + 1, node) + { + edge[j][i] = edge[i][j] = BFS2(Y[i], X[i], Y[j], X[j]); + } + } +} + +void solve() +{ + if (man < 0) + { + cout << "No treasures can be collected." << endl; + return; + } + int cost[M][(1 << M)]; + rep(i, node) + { + rep(j, (1 << node)) + { + cost[i][j] = INF; + } + } + cost[man][1 << man] = 0; + rep(b, (1 << node)) + { + rep(pos, node) + { + if (cost[pos][b] == INF) + { + continue; + } + rep(nextpos, node) + { + if ((b & (1 << nextpos)) > 0) + { + continue; + } + int nextCost = cost[pos][b] + edge[pos][nextpos] + 1; + int nextb = b + (1 << nextpos); + if (rCost[nextpos] <= nextCost) + { + continue; + } + cost[nextpos][nextb] = min(nextCost, cost[nextpos][nextb]); + } + } + } + int ans[M]; + rep(i, M) ans[i] = INF; + rep(i, (1 << node)) + { + int tmp = __builtin_popcount(i); + rep(pos, node) + { + ans[tmp] = min(ans[tmp], cost[pos][i]); + } + } + int tmp = node; + while (1) + { + if (tmp <= 1) + { + cout << "No treasures can be collected." << endl; + return; + } + if (ans[tmp] != INF) + { + cout << "Maximum number of collectible treasures: " << tmp - 1 << '.' << endl; + cout << "Minimum Time: " << ans[tmp] << " sec." << endl; + return; + } + tmp--; + } + return; +} + +int main() +{ + int tc = 0; + while (cin >> H >> W) + { + tc++; + cout << "Case " << tc << ":" << endl; + rep(i, H) rep(j, W) cin >> t[i][j]; + makeGraph(); + solve(); + cout << endl; + } + return 0; +} diff --git a/10232.cpp b/10232.cpp new file mode 100644 index 0000000..8e69498 --- /dev/null +++ b/10232.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +int Com[34][34], N, B[35]; +int comb(int n, int r) +{ + int sum; + if (n == r) + { + return 1; + } + if (r == 0) + { + return 1; + } + if (r == 1) + { + return n; + } + if (Com[n][r]) + { + return Com[n][r]; + } + sum = comb(n - 1, r) + comb(n - 1, r - 1); + Com[n][r] = sum; + return sum; +} +void Gen() +{ + int i, j; + for (i = 0; i <= 31; i++) + { + for (j = 0; j <= i; j++) + { + Com[i][j] = comb(i, j); + } + } +} +int bit_toal() +{ + int sum = 0; + int i; + for (i = 1; i; i++) + { + sum += Com[31][i]; + if (sum >= N) + { + break; + } + } + return i; +} +int Dec() +{ + int i, sum = 0, p = 1; + for (i = 1; i <= 31; i++) + { + sum += B[i] * p; + p *= 2; + } + return sum; +} +int Left(int b) +{ + int sum = 0, i; + for (i = 1; i < b; i++) + { + sum += Com[31][i]; + } + return sum; +} +int bitpos(int sum, int n) +{ + int i, t = 0; + for (i = n - 1; i; i++) + { + t += Com[i][n - 1]; + if (t >= sum) + { + return i + 1; + } + } + return 0; +} +int Pre(int b, int n) +{ + int sum = 0, i; + for (i = b; i <= n; i++) + { + sum += Com[i][b]; + } + return sum; +} +void Cal() +{ + int b, limit = 31, Sum, n; + for (n = 0; n < 32; n++) + { + B[n] = 0; + } + if (N <= 1) + { + cout << N; + return; + } + b = bit_toal(); + if (b == 1) + { + B[N] = 1; + limit = pow(2, N - 1); + cout << limit; + return; + } + Sum = N - Left(b); + for (int i = b; i >= 1; i--) + { + if (Sum == 1) + { + B[i] = 1; + continue; + } + if (i == 1) + { + B[Sum] = 1; + break; + } + n = bitpos(Sum, i); + B[n] = 1; + Sum -= Pre(i - 1, n - 2); + } + cout << Dec(); +} + +int main() +{ + Gen(); + while (cin >> N) + { + Cal(); + cout << endl; + } + return 0; +} diff --git a/10233.cpp b/10233.cpp new file mode 100644 index 0000000..4116c5c --- /dev/null +++ b/10233.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +#define SQR(n) (n * n) +#define DISTANCE(x, y) sqrt(SQR(x) + SQR(y)) + +int f(const int n, int *x, int *y) +{ + int temp = (int)sqrt(n); + *y = n - SQR(temp); + *x = temp * 3 + ((*y % 2) ? 1 : 2); + *y -= temp; +} + +int main() +{ + int n[2], x[2], y[2]; + double sqrt3 = sqrt(3); + while (scanf("%d%d", &n[0], &n[1]) == 2) + { + f(n[0], &x[0], &y[0]), f(n[1], &x[1], &y[1]); + printf("%.3f\n", DISTANCE((double)(x[0] - x[1]) / sqrt3, (double)(y[0] - y[1])) * 0.5); + } + return 0; +} diff --git a/10234.cpp b/10234.cpp new file mode 100644 index 0000000..3e89be0 --- /dev/null +++ b/10234.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +char data[2048], buf[2048]; +int T, N, ans[2048], ans_f[2048], pos[2048], suf[2048], xsuf[2048], xlcp[2048]; + +bool xcmp(int s, int t) +{ + if (s == t) + { + return false; + } + while (tolower(data[s]) == tolower(data[t])) + { + s++, t++; + } + return tolower(data[s]) < tolower(data[t]); +} + +bool cmp(int s, int t) +{ + return s != t && strcmp(data + s, data + t) < 0; +} + +int main() +{ + while (gets(data)) + { + if (gets(buf) == NULL || sscanf(buf, "%d", &T) != 1) + { + break; + } + N = strlen(data); + while (N > 0 && (data[N - 1] == '\r' || data[N - 1] == '\n')) + { + data[--N] = 0; + } + for (int i = 0; i <= N; i++) + { + xsuf[i] = suf[i] = i; + } + sort(xsuf, xsuf + N, xcmp); + sort(suf, suf + N, cmp); + for (int i = 0; i < N; i++) + { + pos[suf[i]] = i; + } + pos[N] = N; + for (int i = 0; i < N - 1; i++) + for (xlcp[i] = 0;; xlcp[i]++) + if (tolower(data[xsuf[i] + xlcp[i]]) != + tolower(data[xsuf[i + 1] + xlcp[i]])) + { + break; + } + xlcp[N - 1] = xlcp[N] = 0; + for (int m = 1; m <= N; m++) + { + ans_f[m] = 0; + ans[m] = 0; + for (int s = 0, t; s < N; s = t) + { + if (N - xsuf[s] < m) + { + t = s + 1; + continue; + } + int mi = pos[xsuf[s]], q = s; + for (t = s + 1; xlcp[t - 1] >= m; t++) + { + if (pos[xsuf[t]] < mi) + { + mi = pos[xsuf[t]]; + q = t; + } + } + if (t - s > ans_f[m] || + (t - s == ans_f[m] && mi < pos[ans[m]])) + { + ans_f[m] = t - s; + ans[m] = xsuf[q]; + } + } + } + int m; + while (T-- > 0 && gets(buf) && sscanf(buf, "%d", &m) == 1) + { + assert(1 <= m && m <= N); + for (int i = 0; i < strlen(data + ans[m]); i++) + { + data[ans[m] + i] = tolower(data[ans[m] + i]); + } + printf("%d %.*s\n", ans_f[m], m, data + ans[m]); + } + } + return 0; +} diff --git a/10235.cpp b/10235.cpp index 0a5ec84..efe098f 100644 --- a/10235.cpp +++ b/10235.cpp @@ -1,69 +1,60 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +#include -bool prime[MAX + 5]; +using namespace std; -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; +int p[1000010]; +void compute_prime_table() /* with Sieve of Eratosthenes */ +{ int i, j; - FOI(i, 2, MAX) - if( prime[i] ) - for (j = 2*i; j <= MAX; j += i) - prime[j] = false; + p[0] = p[1] = 0; + for (i = 2; i <= 1000000; i++) + { + p[i] = 1; /* initialization */ + } + for (i = 2; i <= 1000;) /* for all primes up to 1000 */ + { + for (j = i + i; j <= 1000000; j += i) + { + p[j] = 0; /* delete all multiples of i */ + } + for (i++; !p[i]; i++) + ; /* find next prime */ + } } -int reverse(int N){ - int R = 0; - while( N > 0 ){ - R = R*10 + N%10; - N /= 10; +int reversed(int n) +{ + int res = 0; + while (n) + { + res *= 10; + res += (n % 10); + n /= 10; } - return R; + return res; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int N; - while( cin >> N ){ - if( !prime[N] ) +int main() +{ + compute_prime_table(); + int N, rev; + while (cin >> N) + { + if (!p[N]) + { cout << N << " is not prime." << endl; - else{ - int R = reverse(N); - if( prime[R] && N != R ) + } + else + { + rev = reversed(N); + if (p[rev] && rev != N) + { cout << N << " is emirp." << endl; + } else + { cout << N << " is prime." << endl; + } } } return 0; diff --git a/10236.cpp b/10236.cpp new file mode 100644 index 0000000..c5d1137 --- /dev/null +++ b/10236.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +long long int fib[60]; +int N, prime[1000010], p[100010]; +long double a[60][4], b[4]; + +void prepare() +{ + int i, j, k, n; + fib[0] = 0, fib[1] = 1; + for (i = 2; i < 50; i++) + { + fib[i] = fib[i - 1] + fib[i - 2]; + } + k = (int)sqrt((double)1000001); + memset(prime, -1, sizeof(prime)); + n = 0; + for (i = 2; i <= k; i++) + if (prime[i]) + { + p[n++] = i; + for (j = i * i; j < 1000001; j += i) + { + prime[j] = 0; + } + } + for (; i < 1000001; i++) + if (prime[i]) + { + p[n++] = i; + } + a[0][0] = a[0][1] = a[0][2] = 1; + a[0][3] = 0; +} +void pow_mod(int n, int e) +{ + if (n == 1) + { + a[e][0] = a[e][1] = a[e][2] = 1; + a[e][3] = 0; + return; + } + pow_mod(n / 2, e + 1); + a[e][0] = a[e + 1][0] * a[e + 1][0] + a[e + 1][1] * a[e + 1][2]; + a[e][1] = a[e + 1][0] * a[e + 1][1] + a[e + 1][1] * a[e + 1][3]; + a[e][2] = a[e + 1][2] * a[e + 1][0] + a[e + 1][3] * a[e + 1][2]; + a[e][3] = a[e + 1][2] * a[e + 1][1] + a[e + 1][3] * a[e + 1][3]; + if (n % 2) + { + b[0] = a[e][0] * a[0][0] + a[e][1] * a[0][2]; + b[1] = a[e][0] * a[0][1] + a[e][1] * a[0][3]; + b[2] = a[e][2] * a[0][0] + a[e][3] * a[0][2]; + b[3] = a[e][2] * a[0][1] + a[e][3] * a[0][3]; + a[e][0] = b[0], a[e][1] = b[1], a[e][2] = b[2], a[e][3] = b[3]; + } + while (a[e][0] > 10000) + { + a[e][0] /= 10, a[e][1] /= 10, a[e][2] /= 10, a[e][3] /= 10; + } +} +void solve() +{ + int i, j, n; + long long int t; + long double x; + n = p[N - 1]; + pow_mod(n - 1, 1); + x = a[1][0]; + while (x < 100000000) + { + x *= 10; + } + t = (long long int)x; + printf("%lld\n", t); +} + +int main() +{ + prepare(); + while (scanf("%d", &N) == 1) + { + if (N <= 2) + { + printf("%s\n", N == 1 ? "2" : "3"); + } + else if (N <= 14) + { + printf("%lld\n", fib[p[N - 1]]); + } + else + { + solve(); + } + } + return 0; +} diff --git a/10237.cpp b/10237.cpp new file mode 100644 index 0000000..f05b68c --- /dev/null +++ b/10237.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +long long int O[35][905] = {0}, E[35][905] = {0}, B[35][905] = {0}; + +int main() +{ + int i, j, k; + for (i = 0; i <= 30; i++) + { + O[i][0] = E[i][0] = 1; + } + for (i = 1; i <= 30; i++) + { + for (j = 1; j <= i; j++) + { + O[i][j] = O[i - 1][j] + O[i - 1][j - 1] * (i + (i & 1) - j); + } + } + for (i = 1; i <= 30; i++) + { + for (j = 1; j <= i; j++) + { + E[i][j] = E[i - 1][j] + E[i - 1][j - 1] * ((i | 1) - j); + } + } + for (i = 1; i <= 30; i++) + { + for (j = 0; j < 2 * i; j++) + { + for (k = 0; k <= j; k++) + { + B[i][j] += O[i][k] * E[i][j - k]; + } + } + } + while (scanf("%d%d", &i, &j) == 2) + { + if ((i | j) == 0) + { + break; + } + printf("%lld\n", B[i][j]); + } + return 0; +} diff --git a/10239.cpp b/10239.cpp new file mode 100644 index 0000000..73c50d9 --- /dev/null +++ b/10239.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int w[1001], h[1001], ans[1001], sw, mh, mw, n; +double ww, dh, dw; + +int main() +{ + while (scanf("%d%lf", &n, &ww) == 2) + { + if (n == 0) + { + break; + } + sw = (int)(10000 * (ww + 0.00001)); + for (int i = 1; i <= n; i++) + { + double th, tw; + scanf("%lf %lf", &th, &tw); + h[i] = (int)(10000 * (th + 0.00001)); + w[i] = (int)(10000 * (tw + 0.00001)); + ans[i] = ans[i - 1] + h[i]; + mh = h[i]; + mw = w[i]; + for (int j = i - 2; j >= 0; j--) + { + mw += w[j + 1]; + if (h[j + 1] > mh) + { + mh = h[j + 1]; + } + if (mw > sw) + { + break; + } + if (ans[j] + mh < ans[i]) + { + ans[i] = ans[j] + mh; + } + } + } + int a = ans[n]; + printf("%d.%d%d%d%d\n", a / 10000, (a % 10000) / 1000, (a % 1000) / 100, + (a % 100) / 10, a % 10); + } + return 0; +} diff --git a/10241.cpp b/10241.cpp new file mode 100644 index 0000000..1514e81 --- /dev/null +++ b/10241.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +static const char s[] = + "1\n9\n36\n196\n225\n324\n900\n1225\n4225\n11025\n41616\n53361\n88209\n" + "108900\n176400\n324900\n374544\n1413721\n8643600\n12723489\n43956900\n" + "48024900\n52490025\n158407396\n432224100\n468506025\n846984609\n16314" + "32881\n8283548196\n14682895929\n25300083600\n55420693056\n82995848100" + "\n142249665600\n457239678025\n498786237504\n675585363600\n18826721310" + "25\n8132746129209\n12194587805625\n16944049179225\n63955431761796\n12" + "7740041301796\n575598885856164\n796926124814400\n974193625805625\n128" + "5888320711225\n2172602007770041\n4756207086867600\n5877766022228100\n" + "19553418069930369\n73804512832419600\n78090627485682009\n" + "114710414839347600\n664240615491776400\n1404786532826348100\n25071808" + "34294496361\n2833071028126139025\n7652084567472022500\n22564627508650" + "467249\n49474065660357105625\n85170343853180456676\n10300982478569284" + "3396\n199613587569002860176\n749826196984772523009\n76653309467862411" + "0084\n1365534357790776782400\n2025701206141968155025\n289328451017384" + "1030625\n26039560591564569275625\n73475315219940235232400\n9250270794" + "7137334022400\n98286503002057414584576\n514629226242827525852100\n658" + "184727384126282978225\n884578527018516731261184\n29210597344701852531" + "98400\n3338847817559778254844961\n7199831065357158280252209\n30049630" + "358038004293604649\n30986815651400701675956681\n830673287267045402592" + "72196\n113422539294030403250144100\n317243673064791077618722500\n7055" + "09969089781571229484100\n1020802853646273629251296900\n38530274881794" + "73932250054441\n4212166111404800993143938225\n53531731619038262635564" + "47025\n34677247393615265390250489969\n69132777139733236822209189609\n" + "74594368693872730015492410000\n130889512058808083293251706896\n152910" + "792232501915285941267225\n1178005608529272749639265362064\n4446390382" + "511295358038307980025\n4810207340629922936396456664900\n6073940611585" + "988561928305722500\n6774306649724767427005271097600\n4001751344260165" + "8222344771820225\n55683706715494374550686636110400\n66985660019768984" + "849371924908196\n73702684612392858376746072080400\n151046383493325234" + "090009219613956\n663812918895887474609694358375209\n13594174514399271" + "06810082976525604\n5131130648390546663702275158894481\n87586181497408" + "84101499623707907225\n35524541072381125235676320801486025\n4618017583" + "5514919973320476430050329\n60153048103383854522005973075150400\n65046" + "891745147247744323041849672900\n"; + +int main() +{ + printf("%s", s); + return 0; +} diff --git a/10242.cpp b/10242.cpp new file mode 100644 index 0000000..272ac00 --- /dev/null +++ b/10242.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +double x1, y1_, x2, y2, x3, y3, x4, y4, x, y; + +int main() +{ + while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x1,&y1_,&x2,&y2,&x3,&y3,&x4,&y4) != EOF) + { + if (x1 == x3 && y1_ == y3) + { + x = x2 + x4 - x3; + y = y2 + y4 - y3; + } + else if (x1 == x4 && y1_ == y4) + { + x = x2 + x3 - x4; + y = y2 + y3 - y4; + } + else if (x2 == x3 && y2 == y3) + { + x = x1 + x4 - x3; + y = y1_ + y4 - y3; + } + else + { + x = x1 + x3 - x4; + y = y1_ + y3 - y4; + } + printf("%.3lf %.3lf\n", x, y); + } + return 0; +} diff --git a/10243.cpp b/10243.cpp new file mode 100644 index 0000000..449f0f3 --- /dev/null +++ b/10243.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +bool seen[1003]; +int N, dp[1003][2]; +vector g[1003]; + +int solve(int node, int f) +{ + int &ret = dp[node][f]; + if (ret != -1) + { + return ret; + } + seen[node] = true; + // 2 choices + if (f == 1) + { + ret = 1; + for (int i = 0; i < g[node].size(); i++) + { + if (seen[g[node][i]]) + { + continue; + } + ret += min(solve(g[node][i], 0), solve(g[node][i], 1)); + } + } + else + { + ret = 0; + for (int i = 0; i < g[node].size(); i++) + { + if (seen[g[node][i]]) + { + continue; + } + ret += solve(g[node][i], 1); + } + } + seen[node] = false; + return ret; +} + +int main() +{ + while (scanf("%d", &N) == 1, N) + { + for (int i = 1; i <= N; i++) + { + int n; + scanf("%d", &n); + g[i].resize(n); + for (int j = 0; j < n; j++) + { + scanf("%d", &g[i][j]); + } + } + memset(dp, -1, sizeof dp); + memset(seen, 0, sizeof seen); + seen[1] = true; + printf("%d\n", max(1, min(solve(1, 0), solve(1, 1)))); + } + return 0; +} diff --git a/10245.cpp b/10245.cpp index cd4b7dd..47b98c2 100644 --- a/10245.cpp +++ b/10245.cpp @@ -1,31 +1,56 @@ -#include -#include -using namespace std; -int main(){ - for(;;){ - long long n,i,j; - cin>>n; - if(n==0) - break; - double cood[n][2]; - for(i=0;i>cood[i][0]>>cood[i][1]; - if(n==1){ - cout<<"INFINITY\n"; - continue; - } - double min=sqrt(pow((cood[0][0]-cood[1][0]),2)+pow((cood[0][1]-cood[1][1]),2)); - for(i=0;i=10000) - cout<<"INFINITY\n"; - else - printf("%.4lf\n",min); - } - return 0; -} +#include + +using namespace std; + +#define EPS 1e-6 + +struct Point +{ + double x, y; + double dist(const Point &a) + { + return hypot(a.x - x, a.y - y); + } +} point[10000]; + +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &point[i].x, &point[i].y); + } + + sort(point, point + n, [](const Point &a, const Point &b) -> bool + { + return a.x < b.x; + }); + + double ans = 10000.0f; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + // dist > this pt + shortest dist; two pts cannot be < curr shortest dist + if (point[i].x + ans < point[j].x) + { + break; + } + double d = point[i].dist(point[j]); + ans = min(ans, d); + } + } + + if (fabs(ans - 10000.0f) < EPS) + { + printf("INFINITY\n"); + } + else + { + printf("%.4lf\n", ans); + } + } + return 0; +} diff --git a/10246.cpp b/10246.cpp index 72f4e69..cf40454 100644 --- a/10246.cpp +++ b/10246.cpp @@ -1,59 +1,77 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define sqr(X) ((X) * (X)) -#define FOI(I, A, B) for (I = A; I <= B; I++) -struct Cod{ - double X; - double Y; -}; - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - int N; - cin >> N; - if (N == 0) - break; - Cod frog[N]; - int i, j, k; - FOI(i, 0, N-1) - cin >> frog[i].X >> frog[i].Y; - double mat[N][N]; - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = sqrt(sqr(frog[i].X - frog[j].X) + sqr(frog[i].Y - frog[j].Y)); - - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = min(mat[i][j], max(mat[i][k], mat[k][j])); - printf("Scenario #%d\nFrog Distance = %.3lf\n\n", t, mat[0][1]); - } - return 0; -} +#include + +using namespace std; + +typedef unsigned long long ULL; + +const ULL MAX_VAL = 1000000000ULL; + +int main() +{ + int t = 1, c, r, q; + while (scanf("%d%d%d", &c, &r, &q) == 3, c + r + q) + { + if (t != 1) + { + printf("\n"); + } + ++c; + vector > worst_feast_costs(c, vector(c, MAX_VAL)); + for (int i = 1; i < c; ++i) + { + scanf("%llu", &worst_feast_costs[i][i]); + } + + vector > travel_costs(c, vector(c, MAX_VAL)); + for (int i = 0; i < r; ++i) + { + int start, end; + ULL curr; + scanf("%d%d%llu", &start, &end, &curr); + if (curr < travel_costs[start][end]) + { + travel_costs[start][end] = curr; + travel_costs[end][start] = curr; + ULL feast_cost = max(worst_feast_costs[start][start], + worst_feast_costs[end][end]); + worst_feast_costs[start][end] = feast_cost; + worst_feast_costs[end][start] = feast_cost; + } + } + + // run floyd-warshall 2x because of interaction with worst_feast_costs + for (int p = 0; p < 2; ++p) + { + for (int k = 1; k < c; ++k) + { + for (int i = 1; i < c; ++i) + { + for (int j = 1; j < c; ++j) + { + ULL worst_feast = max(worst_feast_costs[i][k], worst_feast_costs[k][j]); + if (travel_costs[i][j] + worst_feast_costs[i][j] > travel_costs[i][k] + travel_costs[k][j] + worst_feast) + { + travel_costs[i][j] = travel_costs[i][k] + travel_costs[k][j]; + worst_feast_costs[i][j] = worst_feast; + } + } + } + } + } + + printf("Case #%d\n", t++); + for (int i = 0; i < q; ++i) + { + int start, end; + scanf("%d%d", &start, &end); + if (travel_costs[start][end] == MAX_VAL) + { + printf("-1\n"); + } + else + { + printf("%llu\n", travel_costs[start][end] + worst_feast_costs[start][end]); + } + } + } +} diff --git a/10247.cpp b/10247.cpp new file mode 100644 index 0000000..ee6a4e2 --- /dev/null +++ b/10247.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define PI 462 + +int pw[32][32], prime[PI], fact_p[64][PI], fact_n[64], fact_a[4096], fact_k; + +int factpp(int n, int p) +{ + int r, i; + for (r = 0, i = p; i <= n; i *= p) + { + r += n / i; + } + return r; +} + +void sieve() +{ + int s[4096], i, j, k, d; + for (i = 0; i <= 21; i++) + for (pw[i][0] = 1, j = 1; j <= 21; j++) + { + pw[i][j] = pw[i][j - 1] * i; + } + memset(s, 0, sizeof(s)); + for (i = 3; i < 57; i += 2) + if (s[i] == 0) + for (j = i * i; j < 3279; j += 2 * i) + { + s[j] = 1; + } + prime[0] = 2; + for (i = 3, j = 1; i < 3279; i += 2) + if (s[i] == 0) + { + prime[j++] = i; + } + memset(fact_a, 0, sizeof(fact_a)); + for (k = 2; k <= 21; k++) + for (d = 0; d <= 21; d++) + if (k * d <= 21) + { + fact_a[(pw[k][d] - 1) / (k - 1)] = fact_a[k * (pw[k][d] - 1) / (k - 1)] = 1; + } + for (k = i = 0; i < 4096; i++) + if (fact_a[i]) + { + fact_a[i] = k; + fact_n[k++] = i; + } + fact_k = k; + for (i = 0; i < fact_k; i++) + for (j = 0; j < PI; j++) + { + fact_p[i][j] = factpp(fact_n[i], prime[j]); + } +} + +int R[PI], K; +unsigned D[65536]; + +void muldig(unsigned a) +{ + unsigned i, c; + for (c = 0, i = 0; i < K; i++, c /= 10000) + { + D[i] = (c += D[i] * a) % 10000; + } + if (c) + { + D[K++] = c; + } +} + +void mulfact(int m, int n) +{ + int i; + n = fact_a[n]; + for (i = 0; i < PI; i++) + { + R[i] += m * fact_p[n][i]; + } +} + +char *solve(int k, int d) +{ + static char buf[65536]; + int i, j, t; + if (k <= 1 || d <= 0) + { + return "1"; + } + memset(R, 0, sizeof(R)); + for (i = 1; i <= d; i++) + { + for (j = 0; j < PI; j++) + { + R[j] *= k; + } + mulfact(+1, (k * (pw[k][i] - 1)) / (k - 1)); + mulfact(-k, (pw[k][i] - 1) / (k - 1)); + } + K = 1; + D[0] = 1; + for (t = 1, i = 0; i < PI; i++) + { + for (j = 0; j < R[i]; j++) + { + if ((t * prime[i]) >= 10000) + { + muldig(t); + t = 1; + } + t *= prime[i]; + } + } + if (t > 1) + { + muldig(t); + } + sprintf(buf, "%d", D[--K]); + i = strlen(buf); + while (K-- > 0) + { + j = D[K]; + buf[i + 3] = (j % 10) + '0'; + j /= 10; + buf[i + 2] = (j % 10) + '0'; + j /= 10; + buf[i + 1] = (j % 10) + '0'; + j /= 10; + buf[i] = (j % 10) + '0'; + i += 4; + } + buf[i] = 0; + return buf; +} + +int main() +{ + char *sol[32][32]; + int k, d; + sieve(); + memset(sol, 0, sizeof(sol)); + while (scanf("%d %d", &k, &d) == 2) + { + if (sol[k][d] == NULL) + { + sol[k][d] = strdup(solve(k, d)); + } + printf("%s\n", sol[k][d]); + } + return 0; +} diff --git a/10248.cpp b/10248.cpp new file mode 100644 index 0000000..de1574d --- /dev/null +++ b/10248.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +int adj[16][16], ncc, ccid[16], in[16], out[16]; + +void euler(int x, string &str) +{ + while (out[x] > 0) + { + int y; + for (y = 0; y <= 9 && adj[x][y] == 0; y++) + ; + assert(y <= 9 && adj[x][y] > 0); + adj[x][y]--; + out[x]--; + in[y]--; + euler(y, str); + str += (char)(y + '0'); + } +} + +void fw() +{ + memset(in, 0, sizeof(in)); + memset(out, 0, sizeof(out)); + int w[16][16]; + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + w[i][j] = (adj[i][j] > 0 || adj[j][i] > 0); + out[i] += adj[i][j]; + in[j] += adj[i][j]; + } + w[i][i] = 1; + } + for (int k = 0; k < 10; k++) + { + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + w[i][j] |= w[i][k] & w[k][j]; + } + } + } + memset(ccid, 0xff, sizeof(ccid)); + ncc = 0; + for (int i = 0; i < 10; i++) + { + if (ccid[i] >= 0) + { + continue; + } + if (out[i] == 0 && in[i] == 0) + { + continue; + } + for (int j = 0; j < 10; j++) + { + if (w[j][i]) + { + ccid[j] = ncc; + } + } + ncc++; + } +} + +string solve(int n, int m) +{ + if (n > m) + { + return ""; + } + assert(10 <= n && n <= m && m <= 99); + memset(adj, 0, sizeof(adj)); + for (int i = n; i <= m; i++) + { + adj[i / 10][i % 10]++; + } + for (;;) + { + fw(); + if (ncc <= 1) + { + break; + } + int x = -1, y = -1; + for (int i = 0; i < 10; i++) + { + if (ccid[i] == 0 && (x < 0 || out[i] < in[i])) + { + x = i; + } + if (ccid[i] > 0 && (y < 0 || out[i] > in[i])) + { + y = i; + } + } + assert(x >= 0 && y >= 0); + adj[x][y]++; + } + int cnt = 0; + for (int i = 0; i < 10; i++) + { + cnt += abs(in[i] - out[i]); + } + for (int x = 0; x < 10 && cnt > 2; x++) + { + for (int y = 0; y < 10 && cnt > 2; y++) + { + while (cnt > 2 && out[x] < in[x] && out[y] > in[y]) + { + adj[x][y]++; + out[x]++; + in[y]++; + cnt -= 2; + } + } + } + fw(); + string res = ""; + for (int x = 0, k = 0; x <= 9; x++) + { + if (in[x] < out[x]) + { + assert(in[x] == out[x] - 1 && ++k == 1); + euler(x, res); + res += (char)('0' + x); + } + } + for (int x = 0; x <= 9; x++) + { + if (in[x] != 0 || out[x] != 0) + { + assert(in[x] == out[x]); + euler(x, res); + res += (char)('0' + x); + } + } + reverse(res.begin(), res.end()); + return res; +} + +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + string s = solve(max(n, 10), m); + for (int i = n; i <= 9 && i <= m; i++) + { + if (strchr(s.c_str(), '0' + i) == NULL) + { + s += (char)('0' + i); + } + } + for (int i = n; i <= m; i++) + { + char t[10]; + sprintf(t, "%d", i); + assert(strstr(s.c_str(), t) != NULL); + } + printf("%s\n", s.c_str()); + } + return 0; +} diff --git a/10249.cpp b/10249.cpp new file mode 100644 index 0000000..39b2ae4 --- /dev/null +++ b/10249.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +typedef vector VI; +typedef vector VVI; + +struct seat +{ + int val, id; +}; +struct team +{ + int val, id; +}; + +bool cmp(seat a, seat b) +{ + return a.val > b.val; +}; +bool cmp1(team a, team b) +{ + return a.val > b.val; +}; + +int main() +{ + bool flag; + int m, n, tmp, tmp1; + VVI ans; + seat s[75]; + team t[75]; + + while (scanf("%d%d", &m, &n) == 2, (m | n)) + { + ans.assign(m, VI()); + flag = true; + + for (int i = 0; i < m; i++) + { + scanf("%d", &t[i].val); + t[i].id = i + 1; + } + for (int i = 0; i < n; i++) + { + scanf("%d", &s[i].val); + s[i].id = i + 1; + } + + sort(t, t + m, cmp1); + sort(s, s + n, cmp); + + for (int i = 0; i < m; i++) + { + tmp = t[i].val; + for (int j = 0; j < n; j++) + { + if (!tmp) + { + break; + } + if (s[j].val) + { + ans[t[i].id - 1].push_back(s[j].id); + tmp--; + s[j].val--; + } + else + { + continue; + } + } + if (tmp) + { + flag = false; + break; + } + } + + if (!flag) + { + printf("0\n"); + } + else + { + printf("1\n"); + for (int i = 0; i < m; i++) + { + sort(ans[i].begin(), ans[i].end()); + for (int j = 0; j < ans[i].size(); j++) + { + printf("%d ", ans[i][j]); + } + printf("\n"); + } + } + } + return 0; +} diff --git a/1025.cpp b/1025.cpp new file mode 100644 index 0000000..7e45eac --- /dev/null +++ b/1025.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define mem(name, value) memset(name, value, sizeof(name)) +#define FOR(i, n) for (int i = 1; i <= n; i++) + +const int MAXN = 60; +const int MAXM = 200 + 10; +const int INF = 0x3f3f3f3f; + +int d[MAXN][MAXM], has_train[MAXN][MAXM][2], time_[MAXN], N, T, M1, M2; + +void init() +{ + mem(d, 0); + mem(has_train, 0); + scanf("%d", &T); + FOR(i, N - 1) + scanf("%d", time_ + i); + scanf("%d", &M1); + int a; + for (int i = 0; i < M1; i++) + { + scanf("%d", &a); + has_train[1][a][0] = 1; + for (int j = 1; j < N; j++) + { + a += time_[j]; + has_train[j + 1][a][0] = 1; + } + } + scanf("%d", &M2); + for (int i = 0; i < M2; i++) + { + scanf("%d", &a); + has_train[N][a][1] = 1; + for (int j = N; j > 1; j--) + { + a += time_[j - 1]; + has_train[j - 1][a][1] = 1; + } + } + FOR(i, N - 1) + d[i][T] = INF; +} + +int main() +{ + int kase = 0; + while (~scanf("%d", &N) && N) + { + init(); + for (int i = T - 1; i >= 0; i--) + { + for (int j = 1; j <= N; j++) + { + d[j][i] = d[j][i + 1] + 1; + if (j + 1 <= N && i + time_[j] <= T && has_train[j][i][0]) + { + d[j][i] = min(d[j][i], d[j + 1][i + time_[j]]); + } + if (j - 1 >= 1 && i + time_[j - 1] <= T && has_train[j][i][1]) + { + d[j][i] = min(d[j][i], d[j - 1][i + time_[j - 1]]); + } + } + } + printf("Case Number %d: ", ++kase); + if (d[1][0] >= INF) + { + printf("impossible\n"); + } + else + { + printf("%d\n", d[1][0]); + } + } + return 0; +} diff --git a/10250.cpp b/10250.cpp new file mode 100644 index 0000000..1562eeb --- /dev/null +++ b/10250.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +struct point +{ + long double x, y; +}; +struct point a, b; + +int main() +{ + long double x1, x2, y1, y2, A, B, C, D, k, x, y, mx, my, dx, dy; + while (scanf("%Lf%Lf%Lf%Lf", &x1, &y1, &x2, &y2) != EOF) + { + mx = (x1 + x2) / 2; + my = (y1 + y2) / 2; + dx = mx - x1; + dy = my - y1; + printf("%.10Lf %.10Lf %.10Lf %.10Lf\n", mx - dy, my + dx, mx + dy, my - dx); + } + return 0; +} diff --git a/10251.cpp b/10251.cpp new file mode 100644 index 0000000..54452a1 --- /dev/null +++ b/10251.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +double pi = 2 * acos(0.0f), L, H, D, DX, DY; +char str[100]; +int n; + +int main() +{ + gets(str); + sscanf(str, "%d", &n); + while (n--) + { + gets(str); + if (sscanf(str, "%lf %lf %lf %lf", &L, &H, &DX, &DY) == 4) + { + double area; + if (DX == 0 && DY == 0) + { + area = L * L / 2; + } + else if (DX == 0) + { + area = L * (L / 2 - DY); + } + else if (DY == 0) + { + area = L * (L / 2 - DX); + } + else + { + double m = -DX / DY; + double b = DY - m * DX; + double x = (L / 2) - (L / 2 - b) / m; + double y = (L / 2) - (m * L / 2 + b); + area = 0.5 * x * y; + } + printf("%.3lf\n", area * H); + } + else if (sscanf(str, "%lf %lf %lf", &L, &H, &D) == 3) + { + double theta = acos(D / L); + double area = L * L * theta - L * D * sin(theta); + printf("%.3lf\n", area * H); + } + } + return 0; +} diff --git a/10252.cpp b/10252.cpp index 5c649c3..929da58 100644 --- a/10252.cpp +++ b/10252.cpp @@ -1,54 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -string alpha = "abcdefghijklmnopqrstuvwxyz"; +char line1[1010], line2[1010]; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string A, B; - while (getline(cin, A) && getline(cin, B)) { - int arr[26], brr[26] ; - memset(arr, 0, sizeof arr); - memset(brr, 0, sizeof brr); - int aL = A.length() - 1; - int bL = B.length() - 1; - int i, j; - FOI(i, 0, aL) ++arr[A[i] - 'a']; - FOI(i, 0, bL) ++brr[B[i] - 'a']; - FOI(i, 0, 25){ - int M = min(arr[i], brr[i]) - 1; - FOI(j, 0, M) cout << alpha[i]; +int main() +{ + while (cin.getline(line1, 1010)) + { + cin.getline(line2, 1010); + int i; + int cnt1[26], cnt2[26]; + memset(cnt1, 0, sizeof(cnt1)); + memset(cnt2, 0, sizeof(cnt2)); + for (i = 0; line1[i]; i++) + { + line1[i] = tolower(line1[i]); + if (isalpha(line1[i])) + { + cnt1[line1[i] - 'a']++; + } + } + for (i = 0; line2[i]; i++) + { + line2[i] = tolower(line2[i]); + if (isalpha(line2[i])) + { + cnt2[line2[i] - 'a']++; + } + } + for (i = 0; i < 26; i++) + { + if (cnt1[i] && cnt2[i]) + { + int j; + int min; + min = cnt1[i] < cnt2[i] ? cnt1[i] : cnt2[i]; + for (j = 0; j < min; j++) + { + cout << (char)(i + 'a'); + } + } } cout << endl; } return 0; } - diff --git a/10253.cpp b/10253.cpp new file mode 100644 index 0000000..838bb46 --- /dev/null +++ b/10253.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +typedef unsigned long long num; + +num a[32], c[32], f[32]; + +num choose(num n, num k) +{ + num r, i; + if (k > n) + { + return 0; + } + for (r = 1, i = 0; i < k; i++) + { + r = (r * (n - i)) / (i + 1); + } + return r; +} + +void part(int k, int r, int n) +{ + if (r > 0) + { + a[k] = (k == 0) ? 1 : (a[k - 1] + 1); + for (; a[k] <= r && a[k] < n; a[k]++) + for (c[k] = 1; (c[k] * a[k]) <= r; c[k]++) + { + part(k + 1, r - c[k] * a[k], n); + } + } + else + { + num t; + for (t = 1; k--;) + { + t *= choose(f[a[k]] + c[k] - 1, c[k]); + } + f[n] += t; + } +} + +int main() +{ + int n; + for (f[1] = 1, n = 2; n <= 31; n++) + { + f[n] = 0; + part(0, n, n); + } + while (scanf("%d", &n) == 1 && n > 0) + { + printf("%llu\n", (n == 1) ? 1ULL : (2 * f[n])); + } + return 0; +} diff --git a/10254.cpp b/10254.cpp new file mode 100644 index 0000000..acb6973 --- /dev/null +++ b/10254.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define limit 1000000000 + +int T[10005][6] = {0}, len[10005] = {0}, A[6] = {0}, la; + +int main() +{ + int i, j, k, c, s; + la = len[0] = A[0] = 1; + for (i = c = s = 1; i < 10005; i++, c++) + { + len[i] = ((len[i - 1] > la) ? len[i - 1] : la); + for (j = 0; j < len[i]; j++) + { + T[i][j] = T[i - 1][j] + A[j]; + } + for (j = 0; j < len[i]; j++) + { + if (T[i][j] >= limit) + { + T[i][j + 1]++; + T[i][j] -= limit; + } + } + if (T[i][len[i]]) + { + len[i]++; + } + if (c == s) + { + c = 0, s++; + for (j = 0; j < la; j++) + { + A[j] <<= 1; + } + for (j = 0; j < la; j++) + { + if (A[j] >= limit) + { + A[j + 1]++; + A[j] -= limit; + } + } + if (A[la]) + { + la++; + } + } + } + while (scanf("%d", &c) == 1) + { + printf("%d", T[c][len[c] - 1]); + for (i = len[c] - 2; i >= 0; i--) + { + printf("%09d", T[c][i]); + } + printf("\n"); + } + return 0; +} diff --git a/10256.cpp b/10256.cpp new file mode 100644 index 0000000..da33be5 --- /dev/null +++ b/10256.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +#define SMALL 0 +#define POW2(x) ((x) * (x)) + +struct Point +{ + double x, y; +}; +struct Poly +{ + vector p; // last != first +}; + +// >0:left, <0:right +double Cross(const Point &p0, const Point &p1, const Point &p2) +{ + double dx1 = p1.x - p0.x, dy1 = p1.y - p0.y; + double dx2 = p2.x - p0.x, dy2 = p2.y - p0.y; + return dx1 * dy2 - dy1 * dx2; +} + +// CONVEX POLYGON ======================================================== + +Point start; + +struct CompareCross +{ + bool operator()(const Point &p1, const Point &p2) const + { + double c = Cross(start, p1, p2); + if (c < 0) + { + return false;//counter-clockwise + } + else if (c > 0) + { + return true; + } + double dist = (POW2(p1.x - start.x) + POW2(p1.y - start.y)) - + (POW2(p2.x - start.x) + POW2(p2.y - start.y));//by dist.. + if (dist > 0) + { + return false; + } + return true; + } +} comp_cross; + +void SortByCross(Poly &r) +{ + int i, dl; + for (dl = 0, i = 1; i < r.p.size(); i++) + if (r.p[i].y < r.p[dl].y || + (fabs(r.p[i].y - r.p[dl].y) < SMALL && r.p[i].x < r.p[dl].x)) + { + dl = i; + } + Point tmp = r.p[dl]; + r.p[dl] = r.p[0]; + r.p[0] = tmp;//swap + start = tmp; + sort(r.p.begin() + 1, r.p.end(), comp_cross); +} + +void GrahamScanCross(Poly &r)//the last != the first +{ + //check more then 2 point!! + SortByCross(r); + r.p.push_back(r.p.front()); + int i, j; + for (j = 1, i = 2; i < r.p.size(); i++)//j=1,i=2 prevent (5,1) (6,1) (7,1) ... + { + //counter-clockwise, exclude the side.. + while (j > 0 && Cross(r.p[j - 1], r.p[j], r.p[i]) <= SMALL) + { + j--; + } + j++; + Point t = r.p[j]; + r.p[j] = r.p[i]; + r.p[i] = t;//swap.. + } + r.p.erase(r.p.begin() + j, r.p.end()); + return; +} + +bool isInsideConvex(const Point &p0, Poly &r) +{ + if (r.p.size() < 3) + { + return false; + } + r.p.push_back(r.p.front()); + for (int i = r.p.size(); --i >= 1;) + if (Cross(r.p[i - 1], r.p[i], p0) < SMALL)//on line false + { + r.p.pop_back(); + return false;//counter-clockwise + } + r.p.pop_back(); + return true; +} + +int main() +{ + int m, c; + while (scanf("%d%d", &m, &c) == 2) + { + if (m == 0 && c == 0) + { + break; + } + //input + Poly a, b; + while (--m >= 0) + { + Point t; + scanf("%lf %lf", &t.x, &t.y); + a.p.push_back(t); + } + while (--c >= 0) + { + Point t; + scanf("%lf %lf", &t.x, &t.y); + b.p.push_back(t); + } + //test + if (a.p.size() == 1 && b.p.size() == 1) + { + puts("Yes"); + goto END; + } + if (a.p.size() == 1 && b.p.size() == 2) + { + if (fabs(Cross(b.p[0], b.p[1], a.p[0])) < SMALL) + { + puts("No"); + } + else + { + puts("Yes"); + } + goto END; + } + if (b.p.size() == 1 && a.p.size() == 2) + { + if (fabs(Cross(a.p[0], a.p[1], b.p[0])) < SMALL) + { + puts("No"); + } + else + { + puts("Yes"); + } + goto END; + } + //convex + GrahamScanCross(a); + GrahamScanCross(b); + a.p.push_back(a.p.front()); + for (int i = a.p.size(); --i >= 1;) + { + int j; + for (j = b.p.size(); --j >= 0;) + { + double ss = Cross(a.p[i - 1], a.p[i], b.p[j]); + if (ss > SMALL) + { + break; + } + } + if (j < 0) + { + puts("Yes"); + goto END; + } + } + a.p.pop_back(); + b.p.push_back(b.p.front()); + for (int i = b.p.size(); --i >= 1;) + { + int j; + for (j = a.p.size(); --j >= 0;) + { + double ss = Cross(b.p[i - 1], b.p[i], a.p[j]); + if (ss > SMALL) + { + break; + } + } + if (j < 0) + { + puts("Yes"); + goto END; + } + } + b.p.pop_back(); + puts("No"); + END:; + } +} diff --git a/10257.cpp b/10257.cpp new file mode 100644 index 0000000..3806dbd --- /dev/null +++ b/10257.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int s, p, y, j; + while (cin >> s >> p >> y >> j) + { + int x = (12 + j - (y + p)) / 3; + int z = (12 + j) - (3 * x + y + p); + if (z == 2) + { + cout << x + y + 1 << " " << x + p + 1 << " " << x << endl; + } + else if (z == 0) + { + cout << x + y << " " << x + p << " " << x << endl; + } + else + { + if (s + p == y) + { + cout << x + y + 1 << " " << x + p << " " << x << endl; + } + else + { + cout << x + y << " " << x + p + 1 << " " << x << endl; + } + } + } + return 0; +} diff --git a/10258.cpp b/10258.cpp index 15e9af6..487bb80 100644 --- a/10258.cpp +++ b/10258.cpp @@ -1,83 +1,102 @@ -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -struct Contestant { - bool solved[10]; - int attempt[10]; - int penalty, count; +struct submit +{ + int problem; + int time; + char result; }; -map Contest; -vector< int > Index; +struct team +{ + int id; + int solved; + int time; + bool submission_made; + team() : solved(0), time(0), submission_made(false) {} +}; -bool comp(int A, int B) { - if (Contest[A].count != Contest[B].count) - return (Contest[A].count > Contest[B].count); - if (Contest[A].penalty != Contest[B].penalty) - return (Contest[A].penalty < Contest[B].penalty); - return (A < B); +bool theTruthIsOutThere(const team &a, const team &b) +{ + if (a.solved == b.solved) + { + if (a.time == b.time) + { + if (a.submission_made == b.submission_made) + { + return a.id < b.id; + } + return a.submission_made; + } + return a.time < b.time; + } + return a.solved > b.solved; } -int main() { - int T; - stringstream ss; - string str; - getline(cin, str); - //cout << str << endl; - ss.clear(); - ss << str; - ss >> T; - //cout << T << endl; - getline(cin, str); - //cout << str << endl; - while (T--) { - //cout << "T--\n"; - Contest.clear(); - Index.clear(); - while (getline(cin, str)) { - //cout << str << endl; - if (str.empty()) break; - int A, B, C; - string D; - ss.clear(); - ss << str; - ss >> A >> B >> C >> D; - //cout << A << " " << B << " " << C << " " << D << endl; - - if (Contest.find(A) == Contest.end()) { - Contestant Con; - memset(Con.solved, false, sizeof Con.solved); - memset(Con.attempt, 0, sizeof Con.attempt); - Con.penalty = 0; - Con.count = 0; - - Index.push_back(A); - Contest[A] = Con; - } - if (D == "C" || D == "I") { - if (Contest[A].solved[B]) continue; - - if (D == "C") { - Contest[A].solved[B] = true; - Contest[A].penalty += (20 * Contest[A].attempt[B] + C); - Contest[A].count += 1; +int main() +{ + int cases; + int contestant; + submit tmp; + bool solved_already; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + team teams[101]; + vector contestants[101]; + for (int i = 0; i < 101; i++) + { + teams[i].id = i; + } + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> contestant >> tmp.problem >> tmp.time >> tmp.result; + cin.ignore(100, '\n'); + teams[contestant].submission_made = true; + if (tmp.result == 'C') + { + solved_already = false; + for (int i = 0, sz = contestants[contestant].size(); i < sz; i++) + { + if (contestants[contestant][i].problem == tmp.problem && contestants[contestant][i].result == 'C') + { + solved_already = true; + break; + } } - else { - Contest[A].attempt[B] += 1; + if (!solved_already) + { + teams[contestant].time += tmp.time; + teams[contestant].solved += 1; + for (int i = 0, sz = contestants[contestant].size(); i < sz; i++) + { + if (contestants[contestant][i].problem == tmp.problem && contestants[contestant][i].result == 'I') + { + teams[contestant].time += 20; + } + } } } + contestants[contestant].push_back(tmp); + } + cin.ignore(100, '\n'); + sort(teams, teams + 101, theTruthIsOutThere); + for (int i = 0; i < 101; i++) + { + if (teams[i].submission_made == false) + { + break; + } + cout << teams[i].id << " " << teams[i].solved << " " << teams[i].time << endl; } - sort(Index.begin(), Index.end(), comp); - for (int i = 0; i < Index.size(); i++) { - cout << Index[i] << " " << Contest[Index[i]].count << " " << Contest[Index[i]].penalty << endl; + if (cases) + { + cout << endl; } - if (T) cout << endl; } return 0; } diff --git a/10259.cpp b/10259.cpp new file mode 100644 index 0000000..189fc4e --- /dev/null +++ b/10259.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define MAXN 103 +#define MAX(a, b) (a > b ? a : b) + +int N, B[MAXN][MAXN], maximum, K, large, Or[MAXN][MAXN]; + +void BFS() +{ + int i, nx, ny, pos, j, k; + B[1][1] = Or[1][1]; + maximum = B[1][1]; + for (k = B[1][1]; k <= 100; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (Or[i][j] != k) + continue; + for (pos = -K; pos <= K; pos++) + { + ny = j + pos; + if (ny > 0 && ny <= N && Or[i][j] < Or[i][ny]) + { + B[i][ny] = MAX(B[i][ny], B[i][j] + Or[i][ny]); + maximum = MAX(B[i][ny], maximum); + } + nx = i + pos; + if (nx < 1 || nx > N) + continue; + if (Or[i][j] < Or[nx][j]) + { + B[nx][j] = MAX(B[nx][j], B[i][j] + Or[nx][j]); + maximum = MAX(B[nx][j], maximum); + } + } + } + } + } +} + +int main() +{ + int ks, i, j; + cin >> ks; + while (ks--) + { + cin >> N >> K; + large = -1; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + cin >> Or[i][j]; + large = MAX(large, Or[i][j]); + B[i][j] = -1000000; + } + } + BFS(); + cout << maximum << endl; + if (ks) + { + cout << "\n"; + } + } + return 0; +} diff --git a/1026.cpp b/1026.cpp new file mode 100644 index 0000000..51ed6c8 --- /dev/null +++ b/1026.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#define EPS 1e-7 + +double a1, b1, t1, a2, b2, t2, t2_; + +int main() +{ + int t = 1; + while (scanf("%lf%lf%lf%lf%lf%lf", &a1,&b1,&t1,&a2,&b2,&t2_), a1 + b1 + t1) + { + t2 = sqrt(t1 * t1 * a2 * a2 * a2 / a1 / a1 / a1); + t2_ = fmod(t2_, t2); + double c2 = sqrt(a2 * a2 - b2 * b2); + double area_ellipse = M_PI * a2 * b2; + double area_run = area_ellipse * (t2_ / t2); + double lo = 0.0f, hi = 2 * M_PI, mid = (lo + hi) / 2; + while (hi - lo > EPS) + { + double area = (mid * a2 * b2) / 2. - (c2 * b2 * sin(mid)) / 2.; + if (area < area_run) + { + lo = mid; + mid = (lo + hi) / 2; + } + else + { + hi = mid; + mid = (lo + hi) / 2; + } + } + printf("Solar System %d: %.3lf %.3lf\n", t++,a2*cos(lo),b2*sin(lo)); + } + return 0; +} diff --git a/10260.cpp b/10260.cpp index a213e0c..ba088aa 100644 --- a/10260.cpp +++ b/10260.cpp @@ -1,24 +1,26 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - char code[] = { '0','1','2','3','0','1','2','0','0','2','2','4','5','5','0','1','2','6','2','3','0','1','0','2','0','2' }; - string str; - while(cin>>str){ - int len=str.length(),i; - string out,finalStr; - for(i=0;i + +using namespace std; + +char table[30] = "01230120022455012623010202"; + +int main() +{ + char buf[100]; + while (cin >> buf) + { + int i; + for (i = 0; buf[i]; i++) + { + if (table[buf[i] - 'A'] != '0') + { + if (table[buf[i] - 'A'] != table[buf[i - 1] - 'A']) + { + cout << table[buf[i] - 'A']; + } + } + } + cout << endl; + } + return 0; +} diff --git a/10261.cpp b/10261.cpp new file mode 100644 index 0000000..6a94ca9 --- /dev/null +++ b/10261.cpp @@ -0,0 +1,228 @@ +#include + +using namespace std; + +#define MAX_CARS 300 +#define MAX_LEN 15000 +#define BITMAP_LEN ((MAX_LEN + 1 + 31) >> 5) + +typedef unsigned int uint; + +int max_len, bl, n, len[MAX_CARS + 1]; +uint mask, b[MAX_CARS + 1][BITMAP_LEN], tmp[BITMAP_LEN]; +bool sol[MAX_CARS + 1]; + +bool get(uint *src, int i) +{ + return ((src[i >> 5] & (1U << (i & 0x1F))) != 0); +} + +void print_set(uint *src) +{ + bool first = true; + for (int i = 0; i <= max_len; i++) + { + if (get(src, i)) + { + if (first) + { + first = false; + } + else + { + printf(" "); + } + printf("%d", i); + } + } +} + +void init() +{ + bl = (max_len + 1) >> 5; + int x = (max_len + 1) & 0x1F; + if (x == 0) + { + bl--; + mask = (uint)(-1); + } + else + { + mask = (1U << x) - 1; + } + uint *p = b[0]; + p[0] = 1; + for (int i = 1; i <= bl; i++) + { + p[i] = 0; + } + n = 0; +} + +void do_shift(uint *src, uint *dest, int sh) +{ + int sh1 = sh & 0x1F; + if (sh1) + { + int sh2 = 32 - sh1; + uint prev = 0; + for (int i = 0; i <= bl; i++) + { + uint x = src[i]; + dest[i] = (x << sh1) | prev; + prev = (x >> sh2); + } + } + else + { + memcpy(dest, src, (bl + 1) * sizeof(uint)); + } + sh >>= 5; + if (sh) + { + if (sh > bl) + { + sh = bl + 1; + } + else + { + memmove(dest + sh, dest, (bl + 1 - sh) * sizeof(uint)); + } + memset(dest, 0, sh * sizeof(uint)); + } + dest[bl] &= mask; +} + +void do_or(uint *src1, uint *src2, uint *dest) +{ + for (int i = 0; i <= bl; i++) + { + dest[i] = src1[i] | src2[i]; + } +} + +void do_zero(uint *dest, int x) +{ + int k = x >> 5; + if (k > 0) + { + memset(dest, 0, k * sizeof(uint)); + } + dest[k] &= ~((1U << (x & 0x1F)) - 1); +} + +bool is_zero(uint *src) +{ + for (int i = 0; i <= bl; i++) + { + if (src[i]) + { + return false; + } + } + return true; +} + +int find_non_zero(uint *src) +{ + for (int i = 0; i <= bl; i++) + { + if (src[i]) + { + uint x = src[i]; + i <<= 5; + while ((x & 1) == 0) + { + i++; + x >>= 1; + } + return i; + } + } + return -1; +} + +void solve() +{ + int sum = 0; + while (1) + { + int x; + scanf("%d", &x); + if (x == 0) + { + break; + } + len[++n] = x; + sum += x; + do_shift(b[n - 1], tmp, x); + do_or(b[n - 1], tmp, b[n]); + if (sum > max_len) + { + int y = sum - max_len; + if (y > max_len) + { + y = max_len + 1; + } + do_zero(b[n], y); + if (is_zero(b[n])) + { + n--; + do + { + scanf("%d", &x); + } while (x != 0); + return; + } + } + } +} + +void print_solution() +{ + printf("%d\n", n); + int s = find_non_zero(b[n]); + for (int i = n; i > 0; i--) + { + int x = len[i]; + if (x <= s) + { + if (get(b[i - 1], s - x)) + { + sol[i] = true; + s -= x; + } + else + { + sol[i] = false; + } + } + else + { + sol[i] = false; + } + } + for (int i = 1; i <= n; i++) + { + puts(sol[i] ? "port" : "starboard"); + } +} + +int main() +{ + int cases; + scanf("%d", &cases); + for (int i = 0; i < cases; i++) + { + scanf("%d", &max_len); + max_len *= 100; + init(); + solve(); + if (i > 0) + { + putchar('\n'); + } + print_solution(); + } + return 0; +} diff --git a/10262.cpp b/10262.cpp new file mode 100644 index 0000000..ad8f50f --- /dev/null +++ b/10262.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAX 2048 + +int pal(char *s) +{ + char *p; + for (p = s; *p; p++) + ; + while (*s) + { + if (*s++ != *--p) + { + return 0; + } + } + return 1; +} + +void filt(char *s) +{ + char *t; + for (t = s; *s; s++) + { + if ('a' <= (*s) && (*s) <= 'z') + { + *t++ = *s; + } + } + *t = '\0'; +} + +int check(char *a, char *b, char *r) +{ + static char t[2 * MAX]; + sprintf(t, "%s%s", a, r); + if (!pal(t)) + { + return 0; + } + sprintf(t, "%s%s", b, r); + if (pal(t)) + { + return 0; + } + return 1; +} + +int sol(char *a, char *b, char *r) +{ + static char t[2 * MAX]; + int i, j, k, n; + n = strlen(a); + for (i = 0; i <= n; i++) + { + for (j = i - 1, k = 0; j >= 0; j--) + { + r[k++] = a[j]; + } + r[k] = '\0'; + if (check(a, b, r)) + { + return 0; + } + } + for (j = n - 1, k = 1; j >= 0; j--) + { + r[k++] = a[j]; + } + r[k] = '\0'; + for (r[0] = 'a'; r[0] <= 'z'; r[0]++) + if (check(a, b, r)) + { + return 0; + } + return 1; +} + +char *solve(char *a, char *b) +{ + static char r[MAX], s[MAX]; + if (strcmp(a, b) == 0) + { + return NULL; + } + if (sol(a, b, r)) + { + if (sol(b, a, s)) + { + return NULL; + } + return s; + } + if (sol(b, a, s)) + { + return r; + } + if (strlen(r) != strlen(s)) + { + return (strlen(r) < strlen(s)) ? r : s; + } + return (strcmp(r, s) < 0) ? r : s; +} + +int main() +{ + static char a[MAX], b[MAX]; + char *r; + while (gets(a) && gets(b)) + { + filt(a); + filt(b); + r = solve(a, b); + printf(r ? "%s\n" : "No solution.\n", r); + } + return 0; +} diff --git a/10263.cpp b/10263.cpp new file mode 100644 index 0000000..9fbeed8 --- /dev/null +++ b/10263.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +/* Point 2d {{{ */ +typedef double pdata; +struct p2d +{ + pdata x, y; + p2d operator+(p2d p) + { + return p2d(x + p.x, y + p.y); + } + p2d operator-(p2d p) + { + return p2d(x - p.x, y - p.y); + } + p2d operator*(pdata k) + { + return p2d(k * x, k * y); + } + p2d operator/(pdata k) + { + return p2d(x / k, y / k); + } + pdata operator*(p2d p) + { + return (x * p.y - y * p.x); + } + pdata operator^(p2d p) + { + return (x * p.x + y * p.y); + } + bool operator==(p2d p) + { + return (x == p.x && y == p.y); + } + bool operator<(p2d p) const + { + return (x < p.x || (x == p.x && y < p.y)); + } + bool operator>(p2d p) const + { + return (x > p.x || (x == p.x && y > p.y)); + } + bool point_on_line(p2d p0, p2d p1) + { + return ((p1 - p0) * (*this - p0)) == 0; + } + pdata sqr(pdata x) + { + return (x * x); + } + double dist(p2d p) + { + return hypot(x - p.x, y - p.y); + } + double dist2(p2d p) + { + return sqr(x - p.x) + sqr(y - p.y); + } + double mod() + { + return sqrt(x * x + y * y); + } + double mod2() + { + return (x * x + y * y); + } + double point_line_distance(p2d p0, p2d p1) + { + p2d v1 = *this - p0, v2 = p1 - p0; + double u = (v1 ^ v2) / v2.mod2(); + p2d p = p0 + v2 * u; + return (*this).dist(p); + } + double point_line_segment_distance(p2d p0, p2d p1) + { + p2d v1 = *this - p0, v2 = p1 - p0; + double u = (v1 ^ v2) / v2.mod2(); + if (u < 0) + { + return (*this).dist(p0); + } + if (u > 1) + { + return (*this).dist(p1); + } + return (*this).dist(p0 + v2 * u); + } + p2d(pdata _x = 0, pdata _y = 0) : x(_x), y(_y){}; +}; +/* }}} */ + +p2d ans; +double mind; +double point_line_segment_distance(p2d p2, p2d p0, p2d p1) +{ + p2d v1 = p2 - p0, v2 = p1 - p0; + double u = (v1 ^ v2) / v2.mod2(); + double d0 = p2.dist(p0 + v2 * u); + double d1 = p2.dist(p0); + double d2 = p2.dist(p1); + if (u < 0) + { + if (d1 < mind) + { + mind = d1, ans = p0; + } + return d1; + } + if (u > 1) + { + if (d2 < mind) + { + mind = d2, ans = p1; + } + return d2; + } + if (d0 < mind) + { + mind = d0, ans = p0 + v2 * u; + } + return d0; +} + +int main() +{ + int n; + double xf, xm, xt, yf, ym, yt; + while (scanf("%lf %lf", &xm, &ym) == 2) + { + mind = 2e15; + scanf("%d", &n); + for (int i = 0; i <= n; ++i) + { + xf = xt, yf = yt; + scanf("%lf %lf", &xt, &yt); + if (i) + { + point_line_segment_distance(p2d(xm, ym), p2d(xf, yf), p2d(xt, yt)); + } + } + printf("%.4lf\n%.4lf\n", ans.x, ans.y); + } + return 0; +} diff --git a/10264.cpp b/10264.cpp new file mode 100644 index 0000000..7db9b9c --- /dev/null +++ b/10264.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int t[15] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}; + +int n, N, max_, a[16384], s[16384]; + +int main() +{ + while (scanf("%d", &N) == 1) + { + n = t[N]; + max_ = 0; + for (int i = 0; i < n; i++) + { + scanf("%d", a + i); + s[i] = 0; + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < N; j++) + { + s[i] += a[i ^ t[j]]; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < N; j++) + { + if (s[i] + s[i ^ t[j]] > max_) + { + max_ = s[i] + s[i ^ t[j]]; + } + } + } + printf("%d\n", max_); + } + return 0; +} diff --git a/10266.cpp b/10266.cpp new file mode 100644 index 0000000..184a06c --- /dev/null +++ b/10266.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define MAXN 128 + +vector g[MAXN][MAXN]; + +struct Survey +{ + int x, y, h; + Survey(int a = 0, int b = 0, int c = 0) : x(a), y(b), h(c) {} +}; +vector> D; +int n, m; + +int spfa(int sx, int sy) +{ + int ret[MAXN][MAXN], used[MAXN][MAXN] = {}; + vector sid; + sid.resize(D.size(), 0); + int x, y, h, id, tx, ty; + queue X, Y, ID; + used[sx][sy] = 1, ret[sx][sy] = 0; + for (int i = 0; i < g[sx][sy].size(); i++) + { + int u = g[sx][sy][i]; + X.push(sx), Y.push(sy), ID.push(u); + sid[u] = 1; + } + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + id = ID.front(), ID.pop(); + int shift = 0; + for (int i = 0; i < D[id].size(); i++) + { + if (D[id][i].x == x && D[id][i].y == y) + { + shift = ret[x][y] - D[id][i].h; + break; + } + } + for (int i = 0; i < D[id].size(); i++) + { + tx = D[id][i].x, ty = D[id][i].y; + h = D[id][i].h + shift; + if (used[tx][ty] && h != ret[tx][ty]) + return 0; + ret[tx][ty] = h; + if (used[tx][ty] == 0) + { + used[tx][ty] = 1; + for (int j = 0; j < g[tx][ty].size(); j++) + { + int u = g[tx][ty][j]; + if (sid[u]) + continue; + X.push(tx), Y.push(ty), ID.push(u); + sid[u] = 1; + } + } + } + } + + int ok = 1; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + ok &= used[i][j]; + } + } + if (ok) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + printf("%d%c", ret[i][j], j == m ? '\n' : ' '); + } + } + } + else + { + puts("the lack of measurements"); + } + return 1; +} +char line[1048576 * 8]; +int main() +{ + int testcase, bx, by; + int x, y, h; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + scanf("%d %d", &bx, &by); + while (getchar() != '\n') + ; + + for (int i = 0; i <= n; i++) + for (int j = 0; j <= m; j++) + g[i][j].clear(); + D.clear(); + while (gets(line)) + { + if (line[0] == '\0') + { + break; + } + stringstream sin(line); + int id = (int)D.size(); + vector item; + while (sin >> x >> y >> h) + { + item.push_back(Survey(x, y, h)); + g[x][y].push_back(id); + } + D.push_back(item); + } + int f = spfa(bx, by); + if (!f) + { + puts("conflicting measurements"); + } + if (testcase) + { + puts(""); + } + } + return 0; +} diff --git a/10267.cpp b/10267.cpp new file mode 100644 index 0000000..b75dd3b --- /dev/null +++ b/10267.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +void fill(char pic[260][260], int x, int y, char c, int m, int n) +{ + pair tmp; + queue> q; + bool visited[260][260]; + if (c == pic[x][y]) + { + return; + } + for (int i = 0; i < 260; i++) + { + for (int j = 0; j < 260; j++) + { + visited[i][j] = false; + } + } + tmp.first = x; + tmp.second = y; + q.push(tmp); + char old_c = pic[x][y]; + pic[x][y] = c; + visited[x][y] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 1 && pic[tmp.first - 1][tmp.second] == old_c && !visited[tmp.first - 1][tmp.second]) + { + pic[tmp.first - 1][tmp.second] = c; + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + } + if (tmp.second > 1 && pic[tmp.first][tmp.second - 1] == old_c && !visited[tmp.first][tmp.second - 1]) + { + pic[tmp.first][tmp.second - 1] = c; + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + } + if (tmp.first + 1 <= m && pic[tmp.first + 1][tmp.second] == old_c && !visited[tmp.first + 1][tmp.second]) + { + pic[tmp.first + 1][tmp.second] = c; + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + } + if (tmp.second + 1 <= n && pic[tmp.first][tmp.second + 1] == old_c && !visited[tmp.first][tmp.second + 1]) + { + pic[tmp.first][tmp.second + 1] = c; + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + } + } +} + +int main() +{ + int m, n; + int x, y; + int y1, y2; + int x1, x2; + char c; + string input; + char pic[260][260]; + while (cin >> c) + { + switch (c) + { + case 'I': + cin >> m >> n; + for (int i = 1; i <= m; i++) + for (int j = 1; j <= n; j++) + { + pic[i][j] = 'O'; + } + break; + case 'C': + for (int i = 1; i <= m; i++) + for (int j = 1; j <= n; j++) + { + pic[i][j] = 'O'; + } + break; + case 'L': + cin >> x >> y >> c; + pic[x][y] = c; + break; + case 'V': + cin >> x >> y1 >> y2 >> c; + if (y1 > y2) + { + swap(y1, y2); + } + for (int i = y1; i <= y2; i++) + { + pic[x][i] = c; + } + break; + case 'H': + cin >> x1 >> x2 >> y >> c; + if (x1 > x2) + { + swap(x1, x2); + } + for (int i = x1; i <= x2; i++) + { + pic[i][y] = c; + } + break; + case 'K': + cin >> x1 >> y1 >> x2 >> y2 >> c; + for (int i = x1; i <= x2; i++) + for (int j = y1; j <= y2; j++) + { + pic[i][j] = c; + } + break; + case 'F': + cin >> x >> y >> c; + fill(pic, x, y, c, m, n); + break; + case 'S': + cin >> input; + cout << input << endl; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + cout << pic[j][i]; + } + cout << endl; + } + break; + case 'X': + return 0; + break; + default: + cin.ignore(10000, '\n'); + break; + } + } + return 0; +} diff --git a/10268.cpp b/10268.cpp index 525344a..b63bf2d 100644 --- a/10268.cpp +++ b/10268.cpp @@ -1,26 +1,48 @@ -#include -#include -#include -using namespace std; - -int main(){ - int x=0; - while(cin>>x){ - string str,s; - int coef[100]={0},j=0; - getline(cin,str); - for(int i=0;i + +using namespace std; + +const long MX = 1000001; + +char str1[MX], str2[MX], *ptr; +double arr[MX], arrD[MX], result, temp; + +double horner(int n, double arr[], double x) +{ + // compute val of polynomial of ord n + double p; + p = arr[n]; + for (int i = n - 1; i >= 0; --i) + { + p = p * x + arr[i]; + } + return p; +} + +int main() +{ + int x, i, order; + while (cin.getline(str1, sizeof(str1))) + { + cin.getline(str2, sizeof(str2)); + sscanf(str1, "%d", &x); + i = -1; + ptr = strtok(str2, " \t\n"); + while (ptr) + { + ++i; + sscanf(ptr, "%lf", &arr[i]); + ptr = strtok(NULL, " \t\n"); + } + temp = i; + order = --i; + for (int j = 0; j <= order; ++j) + { + arrD[j] = temp-- * arr[j]; + } + reverse(arrD, arrD + order + 1); + result = horner(order, arrD, x); + printf("%.0lf\n", result); + } + return 0; +} diff --git a/10269.cpp b/10269.cpp new file mode 100644 index 0000000..eda6667 --- /dev/null +++ b/10269.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +#define maxn 101 +#define INF 9999999 +#define MIN(a, b) (a > b ? b : a) + +int Cost[maxn][11], F[maxn][maxn], W[maxn][maxn]; +int N, M, A, B, K, L, mincost; +vector V[maxn]; +queue Q; + +void Ini() +{ + int i, j; + N = A + B; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + F[i][j] = W[i][j] = INF; + } + F[i][i] = W[i][i] = 0; + for (j = 0; j <= K; j++) + { + Cost[i][j] = INF; + } + } + for (j = 0; j <= K; j++) + { + Cost[1][j] = 0; + } +} + +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + W[i][j] = MIN(W[i][j], W[i][k] + W[k][j]); + if (k <= A) + { + F[i][j] = MIN(F[i][j], F[i][k] + F[k][j]); + } + } + } + } +} + +void BFS() +{ + int i, u, v, w, d, g, mm; + Q.push(1); + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (v = 1; v <= N; v++) + { + if (v == u) + { + continue; + } + g = 0; + mm = INF; + for (i = 0; i <= K; i++) + { + d = Cost[u][i]; + if (d >= mincost) + { + continue; + } + w = d + W[u][v]; + if (w < mincost && Cost[v][i] > w && w < mm) + { + Cost[v][i] = w; + g = 1; + mm = w; + if (v == N) + { + mincost = w; + } + } + if (i == K || F[u][v] > L) + { + continue; + } + if (d < mincost && Cost[v][i + 1] > d && mm > d) + { + Cost[v][i + 1] = d; + g = 1; + mm = d; + if (v == N) + { + mincost = d; + } + } + } + if (g && v < N) + { + Q.push(v); + } + } + } +} + +void Cal() +{ + Floyd(); + mincost = W[1][N]; + if (F[1][N] < mincost) + { + mincost = F[1][N]; + } + BFS(); + cout << mincost << endl; +} + +int main() +{ + int ks, i, u, v, c; + cin >> ks; + while (ks--) + { + cin >> A >> B >> M >> L >> K; + Ini(); + for (i = 1; i <= M; i++) + { + cin >> u >> v >> c; + F[u][v] = F[v][u] = c; + W[u][v] = W[v][u] = c; + } + Cal(); + } + return 0; +} diff --git a/1027.cpp b/1027.cpp new file mode 100644 index 0000000..7473db0 --- /dev/null +++ b/1027.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +vector g[1000]; +long long dis[1000]; +int inq[1000] = {}, prev_[1000] = {}; +queue Q; + +void solByspfa(int st, int ed, long long goal) +{ + int tn, i, j; + memset(dis, 31, sizeof(dis)); + memset(inq, 0, sizeof(inq)); + memset(prev_, 0, sizeof(prev_)); + Q.push(ed); + dis[ed] = goal; + while (!Q.empty()) + { + tn = Q.front(), Q.pop(); + inq[tn] = 0; + for (i = 0; i < g[tn].size(); i++) + { + j = g[tn][i]; + if ('A' <= tn && tn <= 'Z') + {//town + if (dis[tn] % 19 == 0) + { + if (dis[j] > dis[tn] * 20 / 19) + { + dis[j] = dis[tn] * 20 / 19; + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + prev_[j] = tn; + } + if (dis[j] == dis[tn] * 20 / 19) + { + prev_[j] = min(prev_[j], tn); + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + } + } + else + { + if (dis[j] > dis[tn] * 20 / 19 + 1) + { + dis[j] = dis[tn] * 20 / 19 + 1; + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + prev_[j] = tn; + } + if (dis[j] == dis[tn] * 20 / 19 + 1) + { + prev_[j] = min(prev_[j], tn); + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + } + } + } + else + {// viliage + if (dis[j] > dis[tn] + 1) + { + dis[j] = dis[tn] + 1; + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + prev_[j] = tn; + } + if (dis[j] == dis[tn] + 1) + { + prev_[j] = min(prev_[j], tn); + if (inq[j] == 0) + Q.push(j), inq[j] = 1; + } + } + } + } + printf("%lld\n", dis[st]); +} + +int main() +{ + int n; + int i, j; + int cases = 0; + long long goal; + char s1[10], s2[10]; + while (scanf("%d", &n) == 1 && n >= 0) + { + for (i = 0; i < 1000; i++) + g[i].clear(); + for (i = 0; i < n; i++) + { + scanf("%s %s", s1, s2); + g[s1[0]].push_back(s2[0]); + g[s2[0]].push_back(s1[0]); + } + scanf("%lld %s %s", &goal, s1, s2); + printf("Case %d: ", ++cases); + solByspfa(s1[0], s2[0], goal); + } + return 0; +} diff --git a/10270.cpp b/10270.cpp new file mode 100644 index 0000000..d7a245a --- /dev/null +++ b/10270.cpp @@ -0,0 +1,358 @@ +#include + +using namespace std; + +const int MAX_N = 50; +bool Square[MAX_N + 3][MAX_N + 3]; + +void BuildBarrier(int N) +{ + for (int i = 0; i <= N + 1; i++) + { + Square[0][i] = true; + Square[N + 1][i] = true; + Square[i][0] = true; + Square[i][N + 1] = true; + } +} + +void SetUpSquare(int i0, int j0, int w, bool v) +{ + for (int i = 0; i < w; i++) + { + for (int j = 0; j < w; j++) + { + Square[i0 + i][j0 + j] = v; + } + } +} + +bool IsPlaceClear(int i0, int j0, int w) +{ + for (int i = 0; i < w; i++) + { + for (int j = 0; j < w; j++) + if (Square[i0 + i][j0 + j]) + { + return false; + } + } + return true; +} + +struct Trace +{ + int i, j; + int N; +}; + +Trace tr[MAX_N * MAX_N]; + +struct Place_t +{ + char c[MAX_N + 1]; +}; + +void FindNextSquarePosition(int i, int j, int n, int N, int *i0, int *j0) +{ + int r = (i - 1) * N + (j - 1); + for (int k = n; k < N * N; k++) + { + *i0 = 1 + (r + k) / N; + *j0 = 1 + ((r + k) % N); + if (!Square[*i0][*j0]) + { + break; + } + } +} + +Place_t Squares; +bool Simulate_Placing(int N, int best, int i, int j, int depth = 0) +{ + if (depth > best) + { + return false; + } + if (depth == 0) + { + for (int n = N - 1; n >= 1; n--) + { + if (Squares.c[n] > 0) + { + tr[0].i = i; + tr[0].j = j; + tr[0].N = n; + tr[1].i = i + n; + tr[1].j = j; + tr[1].N = N - n; + tr[2].i = i; + tr[2].j = j + n; + tr[2].N = N - n; + SetUpSquare(i, j, n, true); + SetUpSquare(i + n, j, N - n, true); + SetUpSquare(i, j + n, N - n, true); + Squares.c[n] -= 1; + Squares.c[N - n] -= 2; + int i0, j0; + FindNextSquarePosition(i, j, n, N, &i0, &j0); + if (Simulate_Placing(N, best, i0, j0, depth + 3)) + { + return true; + } + Squares.c[n] += 1; + Squares.c[N - n] += 2; + SetUpSquare(i, j, n, false); + SetUpSquare(i + n, j, N - n, false); + SetUpSquare(i, j + n, N - n, false); + break; + } + } + return false; + } + else if (i <= N && j <= N) + { + int Nr = N - 1; + while (!IsPlaceClear(i, j, Nr)) + { + Nr--; + } + for (int n = Nr; n >= 1; n--) + { + if (Squares.c[n] > 0) + { + tr[depth].i = i; + tr[depth].j = j; + tr[depth].N = n; + SetUpSquare(i, j, n, true); + Squares.c[n]--; + int i0, j0; + FindNextSquarePosition(i, j, n, N, &i0, &j0); + if (Simulate_Placing(N, best, i0, j0, depth + 1)) + { + return true; + } + SetUpSquare(i, j, n, false); + Squares.c[n]++; + } + } + return false; + } + return true; +} + +int max(int a, int b) +{ + return a > b ? a : b; +} + +int min(int a, int b) +{ + return a < b ? a : b; +} + +bool Place(int N, int best, int S = 0, int start0 = -1, int used = 0) +{ + if (S < 0 || used > best) + { + return false; + } + if (used == best) + { + if (S == 0) + { + return Simulate_Placing(N, best, 1, 1); + } + else + { + return false; + } + } + if (used == 0) + { + S = N * N; + for (int i = max(1, N / 2); i < N; i++) + { + Squares.c[i] += 1; + Squares.c[N - i] += 2; + if (Place(N, best, S - i * i - 2 * (N - i) * (N - i), N - i, used + 1 + 2)) + { + return true; + } + Squares.c[N - i] -= 2; + Squares.c[i] -= 1; + } + } + else + { + for (int i = start0; i >= 1; i--) + { + for (int k = min(5, S / (i * i)); k >= 1; k--) + { + Squares.c[i] += k; + if (Place(N, best, S - k * i * i, i - 1, used + k)) + { + return true; + } + Squares.c[i] -= k; + } + } + } + return false; +} + +char Results[MAX_N + 1][512] = +{ + "", + "", + "4\n1 1 1\n2 1 1\n1 2 1\n2 2 1\n", + "6\n1 1 2\n3 1 1\n1 3 1\n2 3 1\n3 2 1\n3 3 1\n", + "4\n1 1 2\n3 1 2\n1 3 2\n3 3 2\n", + "8\n1 1 3\n4 1 2\n1 4 2\n3 4 2\n4 3 1\n5 3 1\n5 4 1\n5 5 1\n", + "4\n1 1 3\n4 1 3\n1 4 3\n4 4 3\n", + "9\n1 1 4\n5 1 3\n1 5 3\n4 5 2\n4 7 1\n5 4 1\n5 7 1\n6 4 2\n6 6 2\n", + "4\n1 1 4\n5 1 4\n1 5 4\n5 5 4\n", + "6\n1 1 6\n7 1 3\n1 7 3\n4 7 3\n7 4 3\n7 7 3\n", + "4\n1 1 5\n6 1 5\n1 6 5\n6 6 5\n", + "11\n1 1 6\n7 1 5\n1 7 5\n6 7 2\n6 9 2\n6 11 1\n7 6 1\n7 11 1\n8 6 4\n8 10 2\n10 10 2\n", + "4\n1 1 6\n7 1 6\n1 7 6\n7 7 6\n", + "11\n1 1 7\n8 1 6\n1 8 6\n7 8 2\n7 10 4\n8 7 1\n9 7 3\n11 10 1\n11 11 3\n12 7 2\n12 9 2\n", + "4\n1 1 7\n8 1 7\n1 8 7\n8 8 7\n", + "6\n1 1 10\n11 1 5\n1 11 5\n6 11 5\n11 6 5\n11 11 5\n", + "4\n1 1 8\n9 1 8\n1 9 8\n9 9 8\n", + "12\n1 1 9\n10 1 8\n1 10 8\n9 10 2\n9 12 4\n9 16 2\n10 9 1\n11 9 3\n11 16 2\n13 12 1\n13 13 5\n14 9 4\n", + "4\n1 1 9\n10 1 9\n1 10 9\n10 10 9\n", + "13\n1 1 10\n11 1 9\n1 11 9\n10 11 2\n10 13 7\n11 10 1\n12 10 3\n15 10 3\n17 13 1\n17 14 3\n17 17 3\n18 10 2\n18 12 2\n", + "4\n1 1 10\n11 1 10\n1 11 10\n11 11 10\n", + "6\n1 1 14\n15 1 7\n1 15 7\n8 15 7\n15 8 7\n15 15 7\n", + "4\n1 1 11\n12 1 11\n1 12 11\n12 12 11\n", + "13\n1 1 12\n13 1 11\n1 13 11\n12 13 2\n12 15 5\n12 20 4\n13 12 1\n14 12 3\n16 20 1\n16 21 3\n17 12 7\n17 19 2\n19 19 5\n", + "4\n1 1 12\n13 1 12\n1 13 12\n13 13 12\n", + "8\n1 1 15\n16 1 10\n1 16 10\n11 16 10\n16 11 5\n21 11 5\n21 16 5\n21 21 5\n", + "4\n1 1 13\n14 1 13\n1 14 13\n14 14 13\n", + "6\n1 1 18\n19 1 9\n1 19 9\n10 19 9\n19 10 9\n19 19 9\n", + "4\n1 1 14\n15 1 14\n1 15 14\n15 15 14\n", + "14\n1 1 15\n16 1 14\n1 16 14\n15 16 2\n15 18 5\n15 23 7\n16 15 1\n17 15 3\n20 15 3\n20 18 3\n20 21 2\n22 21 1\n22 22 8\n23 15 7\n", + "4\n1 1 15\n16 1 15\n1 16 15\n16 16 15\n", + "15\n1 1 16\n17 1 15\n1 17 15\n16 17 2\n16 19 2\n16 21 11\n17 16 1\n18 16 5\n23 16 5\n27 21 1\n27 22 5\n27 27 5\n28 16 4\n28 20 2\n30 20 2\n", + "4\n1 1 16\n17 1 16\n1 17 16\n17 17 16\n", + "6\n1 1 22\n23 1 11\n1 23 11\n12 23 11\n23 12 11\n23 23 11\n", + "4\n1 1 17\n18 1 17\n1 18 17\n18 18 17\n", + "8\n1 1 21\n22 1 14\n1 22 14\n15 22 14\n22 15 7\n29 15 7\n29 22 7\n29 29 7\n", + "4\n1 1 18\n19 1 18\n1 19 18\n19 19 18\n", + "15\n1 1 19\n20 1 18\n1 20 18\n19 20 2\n19 22 5\n19 27 4\n19 31 7\n20 19 1\n21 19 3\n23 27 1\n23 28 3\n24 19 7\n24 26 2\n26 26 12\n31 19 7\n", + "4\n1 1 19\n20 1 19\n1 20 19\n20 20 19\n", + "6\n1 1 26\n27 1 13\n1 27 13\n14 27 13\n27 14 13\n27 27 13\n", + "4\n1 1 20\n21 1 20\n1 21 20\n21 21 20\n", + "15\n1 1 23\n24 1 18\n1 24 18\n19 24 7\n19 31 11\n24 19 3\n24 22 2\n26 22 1\n26 23 5\n26 28 3\n27 19 4\n29 28 2\n29 30 1\n30 30 12\n31 19 11\n", + "4\n1 1 21\n22 1 21\n1 22 21\n22 22 21\n", + "16\n1 1 22\n23 1 21\n1 23 21\n22 23 2\n22 25 5\n22 30 7\n22 37 7\n23 22 1\n24 22 3\n27 22 3\n27 25 3\n27 28 2\n29 28 1\n29 29 15\n30 22 7\n37 22 7\n", + "4\n1 1 22\n23 1 22\n1 23 22\n23 23 22\n", + "6\n1 1 30\n31 1 15\n1 31 15\n16 31 15\n31 16 15\n31 31 15\n", + "4\n1 1 23\n24 1 23\n1 24 23\n24 24 23\n", + "16\n1 1 24\n25 1 23\n1 25 23\n24 25 2\n24 27 1\n24 28 6\n24 34 14\n25 24 1\n25 27 1\n26 24 4\n30 24 10\n38 34 4\n38 38 10\n40 24 8\n40 32 2\n42 32 6\n", + "4\n1 1 24\n25 1 24\n1 25 24\n25 25 24\n", + "9\n1 1 28\n29 1 21\n1 29 21\n22 29 14\n22 43 7\n29 22 7\n29 43 7\n36 22 14\n36 36 14\n", + "4\n1 1 25\n26 1 25\n1 26 25\n26 26 25\n" +}; + +enum TYPES +{ + GENERATION, + JUDGE, +}; + +const int TYPE = JUDGE;//GENERATION; + +Trace solution[MAX_N + 1][MAX_N]; +int solution_len[MAX_N + 1]; +char Solution_Chart[MAX_N + 2][MAX_N + 2]; + +int main() +{ + /* Generation */ + if (TYPE == GENERATION) + { + int bestPrime = 2; + for (int i = 2; i <= MAX_N; i++) + { + printf("%.2i : ", i); + int div = 2; + while (i % div != 0) + { + div++; + } + if (div != i) + { + solution_len[i] = solution_len[div]; + for (int k = 0; k < solution_len[i]; k++) + { + solution[i][k].i = 1 + (solution[div][k].i - 1) * i / div; + solution[i][k].j = 1 + (solution[div][k].j - 1) * i / div; + solution[i][k].N = (solution[div][k].N) * i / div; + } + } + else + { + SetUpSquare(1, 1, i + 2, false); + BuildBarrier(i); + for (int k = 0; k <= MAX_N; k++) + { + Squares.c[k] = 0; + } + while (true) + { + if (Place(i, bestPrime)) + { + break; + } + bestPrime++; + } + solution_len[i] = bestPrime; + for (int k = 0; k < solution_len[i]; k++) + { + solution[i][k].i = 1 + (tr[k].i - 1) * i / div; + solution[i][k].j = 1 + (tr[k].j - 1) * i / div; + solution[i][k].N = 1 + (tr[k].N - 1) * i / div; + } + } + printf("\"%i\\n", solution_len[i]); + for (int k = 0; k < solution_len[i]; k++) + { + printf("%i %i %i\\n", solution[i][k].i, solution[i][k].j, solution[i][k].N); + } + printf("\",\n"); + printf("Chart:\n"); + for (int k = 0; k < solution_len[i]; k++) + { + for (int i0 = 0; i0 < solution[i][k].N; i0++) + { + for (int j0 = 0; j0 < solution[i][k].N; j0++) + { + Solution_Chart[solution[i][k].i + i0][solution[i][k].j + j0] = 'A' + k; + } + } + } + for (int i0 = 1; i0 <= i; i0++) + { + for (int j0 = 1; j0 <= i; j0++) + { + putchar(Solution_Chart[i0][j0]); + } + putchar('\n'); + } + } + } + else + { + int testCases; + scanf("%i", &testCases); + int n; + while (testCases--) + { + scanf("%i", &n); + printf("%s", Results[n]); + } + return 0; + } +} diff --git a/10271.cpp b/10271.cpp new file mode 100644 index 0000000..b68dfe9 --- /dev/null +++ b/10271.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#define NCHOPSTICKS 5000 + +int main() +{ + int nc; /* number of cases */ + int nge; /* numger of guests */ + int nch; /* number of chopsticks */ + int diff[NCHOPSTICKS + 1]; /* smallest differences between chopsticks */ + int chop[NCHOPSTICKS + 1]; /* chopsticks */ + long badness[NCHOPSTICKS + 1]; /* badness for each pair of chopsticks */ + cin >> nc; + for (int t = 0; t < nc; t++) + { + cin >> nge >> nch; + nge += 8; /* add family members */ + diff[0] = 0; + for (int i = 0; i < nch; i++) + { + cin >> chop[i + 1]; + diff[i + 1] = 0; + /* calculate badness as (A-B)^2 */ + badness[i + 1] = pow(chop[i + 1] - chop[i], 2); + } + /* dynamic programming */ + for (int i = 1; i <= nge; i++) + { + for (int j = (nch - 3 * (nge - i) - 1); j >= 2 * i; j--) + { + diff[j] = diff[j - 2] + badness[j]; + } + /* find the neighbouring pairs with smallest difference */ + for (int j = (2 * i) + 1; j <= (nch - 3 * (nge - i) - 1); j++) + { + if (diff[j - 1] < diff[j]) + { + diff[j] = diff[j - 1]; + } + } + /* the third chopstick */ + diff[nch - 3 * (nge - i)] = diff[nch - 3 * (nge - i) - 1]; + } + cout << diff[nch] << endl; + } + return 0; +} diff --git a/10272.cpp b/10272.cpp new file mode 100644 index 0000000..c6913c7 --- /dev/null +++ b/10272.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +int n, k, m, min_[501], cnt[501]; + +bool check() +{ + if (k > n) + { + return false; + } + for (int i = 1; i <= k; i++) + { + min_[i] = min_[i - 1] + ((i / 4) * (i / 4) + 1) * 100; + cnt[i] = 0; + } + if (min_[k] > m) + { + return false; + } + int left = m, space = n; + for (int i = k; i >= 1; i--) + { + int temp; + while (space >= i + 1 && left >= min_[i]) + { + temp = space; + space -= i + 1; + left -= min_[i]; + cnt[i]++; + } + if (space == i && left >= min_[i]) + { + temp = space; + space = 0; + left -= min_[i]; + cnt[i]++; + } + if (((left / 100) & 1) && cnt[i]) + { + left += min_[i]; + space = temp; + cnt[i]--; + } + if (space == i + 2 && ((left / 100) % 2 == 0) && (left >= min_[i] + 100) && ((min_[i] / 100) & 1)) + { + space = 0; + left -= min_[i] + 100; + cnt[i]++; + cnt[1]++; + } + } + if (((left / 100) & 1) && cnt[1]) + { + left += 100; + space += 1; + cnt[1]--; + } + if (left == 0) + { + return true; + } + for (int i = k; i >= 1; i--) + { + int x = cnt[i] * (2 * min_[i]); + if (x >= left) + { + return true; + } + left -= x; + } + return left == 0; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d %d %d", &n, &k, &m); + if (check()) + { + puts("Yes"); + } + else + { + puts("No"); + } + } + return 0; +} diff --git a/10273.cpp b/10273.cpp new file mode 100644 index 0000000..4f7a4c6 --- /dev/null +++ b/10273.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define inf 1000000 + +vector t[1000]; +bool eaten[1000]; +int n, result, lastTime, currentTime, elapsedTime; + +int GCD(int a, int b) +{ + if (b == 0) + { + return a; + } + return GCD(b, a % b); +} + +void input() +{ + cin >> n; + for (int i = 0; i < n; i++) + { + t[i].clear(); + int ti; + cin >> ti; + for (int j = 0; j < ti; j++) + { + int x; + cin >> x; + t[i].push_back(x); + } + } +} + +int LCM() +{ + int lcm = 1, gcd; + for (int i = 0; i < n; i++) + if (!eaten[i]) + { + gcd = GCD(lcm, t[i].size()); + lcm = lcm / gcd * t[i].size(); + } + return lcm; +} + +void process() +{ + memset(eaten, false, sizeof(eaten)); + currentTime = 0; + elapsedTime = LCM(); + lastTime = 0; + result = 0; + while (elapsedTime > 0) + { + int minV = inf, minP = -1, count = 0; + for (int i = 0; i < n; i++) + if (!eaten[i]) + { + int k = t[i][currentTime % t[i].size()]; + if (minV >= k) + { + if (minV > k) + { + minV = k; + minP = i; + count = 1; + } + else + { + count++; + } + } + } + currentTime++; + if (count == 1) + { + //cout << "Eat " << minP << endl; + result++; + lastTime = currentTime; + eaten[minP] = true; + elapsedTime = LCM(); + continue; + } + elapsedTime--; + } +} + +void output() +{ + cout << (n - result) << " " << lastTime << endl; +} + +int main() +{ + int ntest; + cin >> ntest; + while (0 < ntest--) + { + input(); + process(); + output(); + } + return 0; +} diff --git a/10274.cpp b/10274.cpp new file mode 100644 index 0000000..1738d5c --- /dev/null +++ b/10274.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +const int MAXN = 100005; +const int R = 15; +const int C = 22; +const int dir[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}}; +const char sign[5] = "DLRU"; + +struct Map +{ + char state[R][C]; + char order[C]; + int cnt; +} aid, start; + +int sumPace, sumGem, r, c; + +bool isGem(char c) +{ + if (c > '0' && c < '4' || c == '@') + { + return true; + } + return false; +} + +void flyGem(Map now, int &x, int &y, int d) +{ + do + { + x += dir[d][0]; + y += dir[d][1]; + } while (!isGem(now.state[x][y]) && now.state[x][y] != '#'); + x -= dir[d][0]; + y -= dir[d][1]; +} + +void moveGem(Map &now, int d) +{ + int x, y; + if (d == 0) + { + for (int i = r - 1; i >= 0; i--) + { + for (int j = 0; j < c; j++) + { + if (isGem(now.state[i][j])) + { + x = i, y = j; + flyGem(now, x, y, d); + now.state[x][y] = now.state[i][j]; + if (x != i || y != j) + { + now.state[i][j] = ' '; + } + } + } + } + } + else + { + for (int i = 0; i < r; i++) + { + if (d == 1) + { + for (int j = 0; j < c; j++) + { + if (isGem(now.state[i][j])) + { + x = i, y = j; + flyGem(now, x, y, d); + now.state[x][y] = now.state[i][j]; + if (x != i || y != j) + { + now.state[i][j] = ' '; + } + } + } + } + else + { + for (int j = c - 1; j >= 0; j--) + { + if (isGem(now.state[i][j])) + { + x = i, y = j; + flyGem(now, x, y, d); + now.state[x][y] = now.state[i][j]; + if (x != i || y != j) + { + now.state[i][j] = ' '; + } + } + } + } + } + } +} +// // +Map que[MAXN]; +int head[MAXN], nxt[MAXN], dist[MAXN]; + +void inInit() +{ + memset(head, 0, sizeof(head)); + memset(que, 0, sizeof(que)); + memcpy(que[1].state, start.state, sizeof(start.state)); + dist[1] = 0; +} + +inline int hash_(Map &cur) +{ + int seed = 131, v = 0; + for (int i = 0; i < r; ++i) + { + for (int j = 0; j < c; ++j) + { + v = (v * seed + cur.state[i][j]) & 0x7FFFFFFF; + } + } + return v % MAXN; +} + +bool tryInsert(int cur) +{ + int h = hash_(que[cur]); + int u = head[h]; + while (u) + { + if (memcmp(que[cur].state, que[u].state, sizeof(que[u].state)) == 0) + { + return false; + } + u = nxt[u]; + } + nxt[cur] = head[h]; + head[h] = cur; + return true; +} + +int dfs(Map &now, int x, int y, int ch) +{ + int sum = 0; + for (int i = 0; i < 4; i++) + if (ch == now.state[x + dir[i][0]][y + dir[i][1]]) + { + now.state[x + dir[i][0]][y + dir[i][1]] = ' '; + sum += dfs(now, x + dir[i][0], y + dir[i][1], ch); + } + return sum + 1; +} + +int delGem(Map &now) +{ + int sum = 0; + char ch; + for (int x = 0; x < r; x++) + { + for (int y = 0; y < c; y++) + { + int flag = 0; + for (int i = 0; i < 4; i++) + { + if (now.state[x][y] == now.state[x + dir[i][0]][y + dir[i][1]] && isGem(now.state[x][y]) && now.state[x][y] != '@') + { + flag = 1; + ch = now.state[x][y]; + now.state[x][y] = ' '; + break; + } + } + if (flag) + { + sum += dfs(now, x, y, ch); + } + } + } + return sum; +} + +bool bfs() +{ + int front = 1, rear = 2, k; + inInit(); + while (front < rear) + { + Map &now = que[front]; + if (dist[front] > 18) + { + return false; + } + if (now.cnt == sumGem) + { + aid = que[front]; + sumPace = dist[front]; + return true; + } + for (int i = 0; i < 4; i++) + { + Map &net = que[rear]; + net = now; + net.order[dist[front]] = sign[i]; + while (1) + { + moveGem(net, i); + k = delGem(net); + if (k == 0) + { + break; + } + else + { + net.cnt += k; + } + } + if (tryInsert(rear)) + { + dist[rear] = dist[front] + 1; + rear++; + } + } + front++; + } + return false; +} + +int main() +{ + int cas; + char str[1000]; + scanf("%d", &cas); + while (cas--) + { + // Init + sumPace = sumGem = 0; + // Read + scanf("%d%d%*c", &r, &c); + for (int i = 0; i < r; i++) + { + gets(start.state[i]); + int len = strlen(start.state[i]); + for (int j = 0; j < len; j++) + if (start.state[i][j] > '0' && start.state[i][j] < '4') + { + sumGem++; + } + } + gets(str); + if (bfs()) + { + aid.order[sumPace] = '\0'; + puts(aid.order); + } + else + { + printf("-1\n"); + } + } + return 0; +} diff --git a/10275.cpp b/10275.cpp new file mode 100644 index 0000000..e15526c --- /dev/null +++ b/10275.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +int bin_srch(int len, int left, int right) +{ + int mid = (left + right) / 2; + while (left <= right) + { + int p = (int)(mid * log10(mid)) + 1; + if (p == len) + { + return mid; + } + else if (p < len) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + mid = (left + right) / 2; + } + return -1; +} + +int powermod(int base, int exp, int mod) +{ + if (exp == 1) + { + return base % mod; + } + else if (exp % 2) + { + int k = (exp - 1) / 2; + return (powermod(base, k, mod) * powermod(base, k, mod) * base) % mod; + } + else + { + return (powermod(base, exp / 2, mod) * powermod(base, exp / 2, mod)) % mod; + } +} + +int main() +{ + int n, i, k, m, c, len; + char str[500000]; + scanf("%d\n", &n); + for (; n; n--) + { + m = len = 0; + gets(str); + len = strlen(str); + for (i = 0; i < len; i++) + { + m = (m * 10 + str[i] - '0') % 11; + } + if (strcmp(str, "1") == 0) + { + printf("1\n"); + continue; + } + int k = bin_srch(len, 2, 100000); + if (k == -1) + { + printf("-1\n"); + } + else if (powermod(k, k, 11) != m) + { + printf("-1\n"); + } + else + { + printf("%d\n", k); + } + } + return 0; +} diff --git a/10276.cpp b/10276.cpp new file mode 100644 index 0000000..9b88352 --- /dev/null +++ b/10276.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int pesos[52]; + pesos[0] = 1; + pesos[1] = 2; + for (int i = 2; i < 52; i = i + 2) + { + pesos[i] = i + 2; + pesos[i + 1] = i + 2; + } + int bolas[52]; + bolas[0] = 1; + for (int i = 1; i < 52; i++) + { + bolas[i] = bolas[i - 1] + pesos[i - 1]; + } + int cases = 0; + cin >> cases; + for (int i = 0; i < cases; i++) + { + int n = 0; + cin >> n; + cout << bolas[n] - 1 << endl; + } + return 0; +} diff --git a/10277.cpp b/10277.cpp new file mode 100644 index 0000000..ec1be85 --- /dev/null +++ b/10277.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +long long int P, Q; + +long long int gcd(long long int x, long long int y) +{ + return y == 0 ? x : gcd(y, x % y); +} + +void solve() +{ + long long int min, mid, max, ans, total, temp; + if (P == 0) + { + printf("0 2\n"); + } + else if (P == Q) + { + printf("2 0\n"); + } + else + { + ans = gcd(Q, P); + P /= ans, Q /= ans; + for (total = 3; total <= 50000; total++) + { + temp = P * total * (total - 1); + if (temp % Q) + { + continue; + } + else + { + temp /= Q; + } + min = 1, max = total; + for (;;) + { + mid = (min + max) / 2;// binary search + if (mid == min) + { + break; + } + if (mid * mid - mid - temp > 0) + { + max = mid; + } + else + { + min = mid; + } + } + if (mid * mid - mid - temp == 0) + { + printf("%lld %lld\n", mid, total - mid); + return; + } + } + printf("impossible\n"); + } +} + +int main() +{ + for (;;) + { + scanf("%lld%lld", &P, &Q); + if (!P && !Q) + { + break; + } + solve(); + } + return 0; +} diff --git a/10278.cpp b/10278.cpp new file mode 100644 index 0000000..05ffc6c --- /dev/null +++ b/10278.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +const int kMaxn = 507; +const int kInf = 0x7f7f7f7f; + +struct Edge {int v, w;}; +Edge e[kMaxn * 40]; + +int r[107], fst[kMaxn], nxt[kMaxn * 40], f, n, cnt, ans, value, d[107][kMaxn], near[kMaxn]; + +void Clear() +{ + cnt = -1; + memset(fst, -1, sizeof(fst)); +} + +void AddEdge(int u, int v, int w) +{ + cnt++; + e[cnt].v = v; + e[cnt].w = w; + nxt[cnt] = fst[u]; + fst[u] = cnt; +} + +void Input() +{ + scanf("%d%d", &f, &n); + for (int i = 1; i <= f; i++) + { + scanf("%d", &r[i]); + } + getchar(); + Clear(); + string in; + while (getline(cin, in) && !in.empty()) + { + int u, v, w; + istringstream(in) >> u >> v >> w; + AddEdge(u, v, w); + AddEdge(v, u, w); + } +} + +void SPFA(int start, int *dist) +{ + deque q; + bool inq[kMaxn]; + memset(inq, false, sizeof(inq)); + for (int i = 1; i <= n; i++) + { + dist[i] = kInf; + } + dist[start] = 0; + q.push_back(start); + while (!q.empty()) + { + int u = q.front(); + q.pop_front(); + inq[u] = false; + for (int i = fst[u]; i != -1; i = nxt[i]) + { + int v = e[i].v; + if (dist[v] > dist[u] + e[i].w) + { + dist[v] = dist[u] + e[i].w; + if (!inq[v]) + { + if (!q.empty() && dist[v] < dist[q.front()]) + { + q.push_front(v); + } + else + { + q.push_back(v); + } + inq[v] = true; + } + } + } + } +} + +void Solve() +{ + for (int i = 1; i <= f; i++) + { + SPFA(r[i], d[i]); + } + memset(near, 0x7f, sizeof(near)); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= f; j++) + { + near[i] = min(near[i], d[j][i]); + } + } + value = kInf; + for (int i = 1; i <= n; i++) + { + SPFA(i, d[f + 1]); + int t = 0; + for (int j = 1; j <= n; j++) + { + t = max(t, min(near[j], d[f + 1][j])); + } + if (value > t) + { + ans = i; + value = t; + } + } +} + +int main() +{ + bool fst_case = true; + int T; + scanf("%d", &T); + while (T--) + { + Input(); + Solve(); + if (fst_case) + { + fst_case = false; + } + else + { + printf("\n"); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/10279.cpp b/10279.cpp new file mode 100644 index 0000000..b9d666e --- /dev/null +++ b/10279.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +char mine[15][15], touch[15][15], map[15][15]; +int numOfCase, mapSize; + +void input() +{ + int i; + for (i = 0; i < mapSize; i++) + { + scanf("%c", &mine[i]); + } + for (i = 0; i < mapSize; i++) + { + scanf("%c", &touch[i]); + } + memset(map, '.', sizeof(map)); +} + +bool insideMap(int i, int j) +{ + if (i < 0 || i >= mapSize || j < 0 || j >= mapSize) + { + return false; + } + else + { + return true; + } +} + +bool UserFail() +{ + bool fail = false; + int i, j; + for (i = 0; i < mapSize; i++) + for (j = 0; j < mapSize; j++) + if (mine[i][j] == '*' && touch[i][j] == 'x') + { + fail = true; + map[i][j] = '*'; + } + return fail; +} + +void findMine() +{ + int i, j; + for (i = 0; i < mapSize; i++) + for (j = 0; j < mapSize; j++) + { + if (touch[i][j] == 'x') + { + int cnt = 0; + if (insideMap(i - 1, j - 1) && mine[i - 1][j - 1] == '*') + { + cnt++; + } + if (insideMap(i - 1, j) && mine[i - 1][j] == '*') + { + cnt++; + } + if (insideMap(i - 1, j + 1) && mine[i - 1][j + 1] == '*') + { + cnt++; + } + if (insideMap(i, j - 1) && mine[i][j - 1] == '*') + { + cnt++; + } + if (insideMap(i, j + 1) && mine[i][j + 1] == '*') + { + cnt++; + } + if (insideMap(i + 1, j - 1) && mine[i + 1][j - 1] == '*') + { + cnt++; + } + if (insideMap(i + 1, j) && mine[i + 1][j] == '*') + { + cnt++; + } + if (insideMap(i + 1, j + 1) && mine[i + 1][j + 1] == '*') + { + cnt++; + } + map[i][j] = (char)(cnt + '0'); + } + } +} + +void printMap() +{ + int i, j; + for (i = 0; i < mapSize; i++) + { + for (j = 0; j < mapSize; j++) + { + printf("%c", map[i][j]); + } + printf("\n"); + } +} + +void printMine() +{ + int i, j; + for (i = 0; i < mapSize; i++) + { + for (j = 0; j < mapSize; j++) + { + if (mine[i][j] == '*') + { + cout << '*'; + } + else + { + cout << map[i][j]; + } + } + cout << endl; + } +} + +void solve() +{ + findMine(); + if (UserFail()) + { + printMine(); + } + else + { + printMap(); + } +} + +int main() +{ + int f = 0; + while (scanf("%d", &numOfCase) == 1) + { + while (numOfCase--) + { + if (f) + { + printf("\n"); + } + f = 1; + scanf("%d", &mapSize); + input(); + solve(); + } + } + return 0; +} diff --git a/10280.cpp b/10280.cpp new file mode 100644 index 0000000..0c74cb2 --- /dev/null +++ b/10280.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define MAXD 4600 +#define MAXL 450000 +#define MAXN 110 +#define MAXn 18000 +#define INF 0x3f3f3f3f + +int L, N, min_[MAXN], max_[MAXN], f[MAXL], vis[MAXD], v[MAXn]; + +void solve() +{ + int i, j, k, n, limit; + scanf("%d%d", &L, &N); + L *= 1000; + limit = INF; + for (i = 0; i < N; i++) + { + scanf("%d%d", &min_[i], &max_[i]); + if (min_[i] * min_[i] / (max_[i] - min_[i]) < limit) + { + limit = min_[i] * min_[i] / (max_[i] - min_[i]); + } + } + if (L > limit) + { + printf("0\n"); + return; + } + n = 0; + memset(vis, 0, sizeof(vis)); + for (i = 0; i < N; i++) + for (j = min_[i]; j <= max_[i]; j++) + if (!vis[j]) + { + vis[j] = 1; + v[n++] = j; + } + memset(f, 0, sizeof(f)); + for (i = 0; i < n; i++) + for (j = v[i]; j <= L; j++) + if (f[j - v[i]] + v[i] > f[j]) + { + f[j] = f[j - v[i]] + v[i]; + } + printf("%d\n", L - f[L]); +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + solve(); + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/10281.cpp b/10281.cpp index b815342..6fa85b1 100644 --- a/10281.cpp +++ b/10281.cpp @@ -1,60 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -double toDouble(string s){ - stringstream ss(s); - double D; - ss >> D; - return D; -} +char time_[2000]; +int h, m, s, speed, newspeed, now; +float dist = 0; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - double last = 0.0; - double dis = 0.0, speed = 0.0; - string temp; - while( getline(cin, temp) ){ - stringstream ss(temp); - string str = "", s = ""; - ss >> str >> s; - double curr = ( (str[0] - '0') * 10 + (str[1] - '0') ) / 1.0; - curr += ( (str[3] - '0') * 10 + (str[4] - '0') ) / 60.0; - curr += ( (str[6] - '0') * 10 + (str[7] - '0') ) / 3600.0; - dis += ( speed * (curr - last) ); - last = curr; - //cout << temp << endl; - if( s == "" ) - printf("%s %2.2lf km\n", str.c_str(), dis); - else - speed = toDouble(s); +int main() +{ + int n, oldtime_ = 0; + speed = 0; + while (gets(time_)) + { + n = sscanf(time_, "%d:%d:%d %d", &h, &m, &s, &newspeed); + now = h * 3600 + m * 60 + s; + dist += (now - oldtime_) / 3600.0 * speed; + oldtime_ = now; + if (n == 3) + { + printf("%02d:%02d:%02d %.2f km\n", h, m, s, dist); + } + else if (n == 4) + { + speed = newspeed; + } } return 0; } diff --git a/10282.cpp b/10282.cpp index e2c5a96..87c714e 100644 --- a/10282.cpp +++ b/10282.cpp @@ -1,62 +1,89 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(I, A, B) for (I=A; I<=B; I++) -#define FOD(I, A, B) for (I=A; I>=B; I--) - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - map dict; - map::iterator it; - - string str; - while( getline(cin, str) ){ - if( str == "" ) +char cmd[100]; + +class Dictionary +{ +public: + char english[20]; + char foreign[20]; +} dic[100010]; + +int total = 0; + +int cmp(const void *a, const void *b) +{ + Dictionary *aa = (Dictionary *)a; + Dictionary *bb = (Dictionary *)b; + return strcmp(aa->foreign, bb->foreign); +} + +void input() +{ + while (cin.getline(cmd, 100)) + { + if (!strcmp(cmd, "\0")) + { break; - stringstream ss; - string key, val; - ss << str; - ss >> val >> key; - dict[key] = val; + } + char *p; + p = strtok(cmd, " "); + if (p) + { + strcpy(dic[total].english, p); + } + p = strtok(NULL, " "); + if (p) + { + strcpy(dic[total].foreign, p); + } + total++; } - - while( cin >> str ){ - it = dict.find(str); - if( it != dict.end() ) - cout << dict[str] << endl; + qsort(dic, total, sizeof(Dictionary), cmp); +} + +// binary search +int bin_search(char *key) +{ + int low, mid, high; + low = 0; + high = total - 1; + while (low <= high) + { + mid = (low + high) / 2; + if (strcmp(key, dic[mid].foreign) == 0) + { + return mid; + } + else if (strcmp(key, dic[mid].foreign) < 0) + { + high = mid - 1; + } else + { + low = mid + 1; + } + } + return -1; +} + +int main() +{ + input(); + while (cin.getline(cmd, 100)) + { + int index; + index = bin_search(cmd); + if (index < 0) + { cout << "eh" << endl; + } + else + { + cout << dic[index].english << endl; + } } return 0; } diff --git a/10283.cpp b/10283.cpp new file mode 100644 index 0000000..1008c73 --- /dev/null +++ b/10283.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +/* Globals */ +long double r, I, E; +int R, N; +const long double PI = 2 * acos(0.0); + +/* Functions */ + +/* Calculates the radius of each smaller circle */ +void radius(void) +{ + if (N < 3) + { + r = (double)R / N; + return; + } + long double num = sin(2.0 / N * PI); + long double denom = 2 * sin((N - 2) / (2.0 * N) * PI) + sin((2.0 / N) * PI); + r = (double)R * num / denom; +} + +/* Calculates the blue area */ +void internal(void) +{ + if (N < 3) + { + I = 0; + return; + } + I = N * r * sqrt(R * R - 2 * r * R) - 0.5 * PI * r * r * (N - 2.0); +} + +/* Calculates the leftover area */ +void external(void) +{ + /* Area of whole circle minus area of all small circles minus I */ + E = PI * R * R - N * PI * r * r - I; +} + +int main(int argc, char **argv) +{ + while (cin >> R >> N) + { + radius(); + internal(); + external(); + printf("%.10Lf %.10Lf %.10Lf\n", r, I, E); + } + return 0; +} diff --git a/10284.cpp b/10284.cpp new file mode 100644 index 0000000..8daf4b0 --- /dev/null +++ b/10284.cpp @@ -0,0 +1,206 @@ +#include + +using namespace std; + +static int piece[64], color[64], attacked[64]; + +#define CELL(row, col) (((row) << 3) + (col)) +#define ROW(cell) ((cell) >> 3) +#define COL(cell) ((cell)&7) + +#define PIECE(r, c) piece[CELL(r, c)] +#define COLOR(r, c) color[CELL(r, c)] +#define ATTACKED(r, c) attacked[CELL(r, c)] + +#define VALID(r, c) (((r) >= 0) && ((r) < 8) && ((c) >= 0) && ((c) < 8)) +#define FREE(r, c) (VALID(r, c) && PIECE(r, c) == 0) +#define MARK(r, c) \ + { \ + if (VALID(r, c)) \ + ATTACKED(r, c) = 1; \ + } + +static void parse(char *s) +{ + int i; + for (i = 0; i < 64; i++) + { + piece[i] = 0, color[i] = 0, attacked[i] = 0; + } + for (i = 0; *s; s++) + { + if (*s >= '1' && *s <= '8') + { + i += *s - '0'; + continue; + } + if (strchr("PNBRQK", *s) != NULL) + { + piece[i] = *s; + color[i] = 'W'; + i++; + } + else if (strchr("pnbrqk", *s) != NULL) + { + piece[i] = *s - 'a' + 'A'; + color[i] = 'B'; + i++; + } + } +} + +static void pawns() +{ + register int r, c; + for (r = 0; r < 7; r++) + { + for (c = 0; c < 8; c++) + { + if (PIECE(r, c) != 'P') + { + continue; + } + if (COLOR(r, c) == 'B') + { + MARK(r + 1, c - 1); + MARK(r + 1, c + 1); + } + else + { + MARK(r - 1, c - 1); + MARK(r - 1, c + 1); + } + } + } +} + +static void knights() +{ + register int r, c; + for (r = 0; r < 8; r++) + { + for (c = 0; c < 8; c++) + { + if (PIECE(r, c) != 'N') + { + continue; + } + MARK(r - 2, c - 1); + MARK(r - 2, c + 1); + MARK(r - 1, c - 2); + MARK(r - 1, c + 2); + MARK(r + 1, c - 2); + MARK(r + 1, c + 2); + MARK(r + 2, c - 1); + MARK(r + 2, c + 1); + } + } +} + +static void kings() +{ + register int r, c; + for (r = 0; r < 8; r++) + { + for (c = 0; c < 8; c++) + { + if (PIECE(r, c) != 'K') + { + continue; + } + MARK(r - 1, c - 1); + MARK(r - 1, c); + MARK(r - 1, c + 1); + MARK(r, c - 1); + MARK(r, c + 1); + MARK(r + 1, c - 1); + MARK(r + 1, c); + MARK(r + 1, c + 1); + } + } +} + +static void rooks() +{ + register int r, c, i; + for (i = 0; i < 64; i++) + { + if (piece[i] != 'R' && piece[i] != 'Q') + { + continue; + } + c = COL(i); + for (r = ROW(i) - 1; FREE(r, c); r--) + { + MARK(r, c); + } + for (r = ROW(i) + 1; FREE(r, c); r++) + { + MARK(r, c); + } + r = ROW(i); + for (c = COL(i) - 1; FREE(r, c); c--) + { + MARK(r, c); + } + for (c = COL(i) + 1; FREE(r, c); c++) + { + MARK(r, c); + } + } +} + +static void bishops() +{ + register int r, c, i; + for (i = 0; i < 64; i++) + { + if (piece[i] != 'B' && piece[i] != 'Q') + { + continue; + } + for (r = ROW(i) - 1, c = COL(i) - 1; FREE(r, c); r--, c--) + { + MARK(r, c); + } + for (r = ROW(i) + 1, c = COL(i) + 1; FREE(r, c); r++, c++) + { + MARK(r, c); + } + for (r = ROW(i) + 1, c = COL(i) - 1; FREE(r, c); r++, c--) + { + MARK(r, c); + } + for (r = ROW(i) - 1, c = COL(i) + 1; FREE(r, c); r--, c++) + { + MARK(r, c); + } + } +} + +static int total() +{ + int i, r; + for (i = 0, r = 0; i < 64; i++) + if (piece[i] == 0 && !attacked[i]) + { + r++; + } + return r; +} + +int main() +{ + static char buf[1024]; + while (gets(buf)) + { + parse(buf); + pawns(); + knights(); + rooks(); + bishops(); + kings(); + printf("%d\n", total()); + } + return 0; +} diff --git a/10285.cpp b/10285.cpp new file mode 100644 index 0000000..5d11d20 --- /dev/null +++ b/10285.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 101 + +int Snow[MAXN][MAXN]; +int R, C; +int MAXD, Temp; +int MoveX[] = {0, 0, 1, -1}; +int MoveY[] = {1, -1, 0, 0}; + +struct ss +{ + char f; + char c; +} V[MAXN][MAXN]; +void Find(int r, int c) +{ + int i; + V[r][c].f = 'y'; + V[r][c].c = 'b'; + Temp++; + if (Temp > MAXD) + { + MAXD = Temp; + } + for (i = 0; i < 4; i++) + { + int nr = MoveX[i] + r; + int nc = MoveY[i] + c; + if (nr < 0 || nc < 0 || nr >= R || nc >= C || Snow[r][c] <= Snow[nr][nc]) + { + continue; + } + if (V[nr][nc].c != 'b') + { + Find(nr, nc); + Temp--; + V[nr][nc].c = 'w'; + } + } + V[r][c].c = 'w'; +} +void Cal() +{ + int i, j; + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + { + if (V[i][j].f == 'n') + { + Temp = 0; + Find(i, j); + } + } +} + +int main() +{ + int i, j, kase; + char name[100]; + scanf("%d", &kase); + while (kase--) + { + scanf("%s%d%d", name, &R, &C); + MAXD = 0; + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + scanf("%d", &Snow[i][j]); + V[i][j].f = 'n'; + V[i][j].c = 'w'; + } + } + Cal(); + printf("%s: %d\n", name, MAXD); + } + return 0; +} diff --git a/10286.cpp b/10286.cpp index 0273742..10b5c38 100644 --- a/10286.cpp +++ b/10286.cpp @@ -1,13 +1,13 @@ -#include -#include +#include -const double PI = acos(-1.0) / 180.0; -const double D = sin(108.0 * PI) / sin(63.0 * PI); +using namespace std; -int main() { - double F; - while (scanf("%lf", &F) != EOF) { - printf("%.10lf\n", F * D); +int main() +{ + double a, tmp = 1.0673956817111818692592637626711; + while (scanf("%lf", &a) == 1) + { + printf("%.10lf\n", a * tmp); } return 0; } diff --git a/10287.cpp b/10287.cpp new file mode 100644 index 0000000..6f75359 --- /dev/null +++ b/10287.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + double r; + while (scanf("%lf", &r) == 1) + { + double a, b, c, d; + a = sqrt(3.0) / 2 * r; + b = (2 * sqrt(3.0) - 3) * r; + c = sqrt(3.0) / 4 * r; + d = (3 * sqrt(28) - 7 * sqrt(3)) / 10 * r; + printf("%.10lf %.10lf %.10lf %.10lf\n", a, b, c, d); + } + return 0; +} diff --git a/10288.cpp b/10288.cpp new file mode 100644 index 0000000..1134ac2 --- /dev/null +++ b/10288.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +long long int GCD(long long int a, long long int b) +{ + return (b == 0) ? a : GCD(b, a % b); +} +long long int c[34], p[34]; +int getLength(long long int d) +{ + int len = 0; + for (len = 0; d > 9; len++, d /= 10) + ; + len++; + return len; +} +void print(int n, char c) +{ + for (int i = 0; i < n; i++) + { + putchar(c); + } +} + +int main() +{ + long long int one = 1ll, d, ba, bb; + int n; + c[1] = one; + p[1] = one; + for (int i = 2; i <= 33; i++) + { + long long int t = (long long int)i; + d = GCD(p[i - 1], t); + p[i] = p[i - 1] * t / d; + ba = p[i] / p[i - 1]; + bb = p[i] / t; + c[i] = c[i - 1] * ba + bb; + } + while (scanf("%d", &n) == 1) + { + d = p[n] / (long long int)n; + if (d == 1) + { + printf("%lld\n", c[n]); + } + else + { + long long int a = c[n] / d; + int spaces = getLength(a) + 1; + ba = c[n] - a * d; + bb = GCD(ba, d); + ba /= bb; + d /= bb; + int dashspaces = getLength(d); + print(spaces, ' '); + printf("%lld\n", ba); + printf("%lld ", a); + print(dashspaces, '-'); + printf("\n"); + print(spaces, ' '); + printf("%lld\n", d); + } + } + return 0; +} diff --git a/1029.cpp b/1029.cpp new file mode 100644 index 0000000..e47cb51 --- /dev/null +++ b/1029.cpp @@ -0,0 +1,253 @@ +#include + +using namespace std; + +#define eps 1e-6 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; +struct Seg +{ + Pt s, e; + Seg(Pt a = Pt(), Pt b = Pt()) : s(a), e(b) {} +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= 0 && dot(c - b, a - b) >= 0; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +int intersection(Pt as, Pt at, Pt bs, Pt bt) +{ + if (cross(as, at, bs) * cross(as, at, bt) < 0 && + cross(at, as, bs) * cross(at, as, bt) < 0 && + cross(bs, bt, as) * cross(bs, bt, at) < 0 && + cross(bt, bs, as) * cross(bt, bs, at) < 0) + return 1; + return 0; +} +double distProjection(Pt as, Pt at, Pt s) +{ + int a, b, c; + a = at.y - as.y; + b = as.x - at.x; + c = -(a * as.x + b * as.y); + return fabs(a * s.x + b * s.y + c) / hypot(a, b); +} +double ptToSeg(Seg seg, Pt p) +{ + double c = 1e+30; + if (between(seg.s, seg.e, p)) + c = min(c, distProjection(seg.s, seg.e, p)); + else + c = min(c, min(dist(seg.s, p), dist(seg.e, p))); + return c; +} +int inPolygon(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (onSeg(q, p[i], p[j])) + return 1; + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + cnt++; + } + return cnt & 1; +} +// this problem +Pt D[1024]; +int N; +int checkCircle(double x, double y, double r) +{ + int cnt = 0; + for (int i = 0; i < N; i++) + { + if (D[i].x > x && (D[i].y > y != D[i + 1].y > y)) + cnt++; + } + if (cnt % 2 == 0) + return 0; + for (int i = 0; i < N; i++) + { + if ((D[i].x - x) * (D[i].x - x) + (D[i].y - y) * (D[i].y - y) < (r - eps) * (r - eps)) + return 0; + if (D[i].x == D[i + 1].x && (D[i].y > y != D[i + 1].y > y) && fabs(D[i].x - x) < r - eps) + return 0; + if (D[i].y == D[i + 1].y && (D[i].x > x != D[i + 1].x > x) && fabs(D[i].y - y) < r - eps) + return 0; + } + return 1; +} +int checkRadius(double r) +{ + for (int i = 0; i < N; i++) + { + if (D[i].x == D[i + 1].x) + for (int j = 0; j < N; j++) + { + if (D[j].y == D[j + 1].y) + { + if (checkCircle(D[i].x + r, D[j].y + r, r)) + return 1; + if (checkCircle(D[i].x + r, D[j].y - r, r)) + return 1; + if (checkCircle(D[i].x - r, D[j].y + r, r)) + return 1; + if (checkCircle(D[i].x - r, D[j].y - r, r)) + return 1; + } + } + } + + double d, dd, dx, dy, x, y; + for (int i = 0; i < N; i++) + {// (line, point) => circle + for (int j = 0; j < N; j++) + { + if (D[i].x == D[i + 1].x) + { + d = fabs(D[j].x - (D[i].x + r)); + if (d < r) + { + dy = sqrt(r * r - d * d); + if (checkCircle(D[i].x + r, D[j].y + dy, r)) + return 1; + if (checkCircle(D[i].x + r, D[j].y - dy, r)) + return 1; + } + d = fabs(D[j].x - (D[i].x - r)); + if (d < r) + { + dy = sqrt(r * r - d * d); + if (checkCircle(D[i].x - r, D[j].y + dy, r)) + return 1; + if (checkCircle(D[i].x - r, D[j].y - dy, r)) + return 1; + } + } + else + { + d = fabs(D[j].y - (D[i].y + r)); + if (d < r) + { + dx = sqrt(r * r - d * d); + if (checkCircle(D[j].x + dx, D[i].y + r, r)) + return 1; + if (checkCircle(D[j].x - dx, D[i].y + r, r)) + return 1; + } + d = fabs(D[j].y - (D[i].y - r)); + if (d < r) + { + dx = sqrt(r * r - d * d); + if (checkCircle(D[j].x + dx, D[i].y - r, r)) + return 1; + if (checkCircle(D[j].x - dx, D[i].y - r, r)) + return 1; + } + } + } + } + + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + x = (D[i].x + D[j].x) / 2; + y = (D[i].y + D[j].y) / 2; + d = hypot(x - D[i].x, y - D[i].y); + if (d < r) + { + dd = sqrt(r * r - d * d); + dx = (y - D[i].y) / d * dd; + dy = (D[i].x - x) / d * dd; + + if (checkCircle(x + dx, y + dy, r)) + return 1; + if (checkCircle(x - dx, y - dy, r)) + return 1; + } + } + } + return 0; +} + +int main() +{ + int n, x, cases = 0; + char cmd[1024]; + const int dx[] = {1, 0, -1, 0}; + const int dy[] = {0, 1, 0, -1}; + int R[128]; + R['R'] = 0, R['U'] = 1, R['L'] = 2, R['D'] = 3; + while (scanf("%d", &n) == 1 && n) + { + D[0] = Pt(0, 0), N = n; + for (int i = 1; i <= n; i++) + { + scanf("%d%s", &x, cmd); + D[i].x = D[i - 1].x + x * dx[R[cmd[0]]]; + D[i].y = D[i - 1].y + x * dy[R[cmd[0]]]; + } + double l = 0, r = 1000, ret, mid; + while (fabs(l - r) > eps) + { + mid = (l + r) / 2; + if (checkRadius(mid)) + { + l = mid, ret = mid; + } + else + { + r = mid; + } + } + if (cases) + puts(""); + printf("Case Number %d radius is: %.2lf\n", ++cases, ret); + } + return 0; +} diff --git a/10290.cpp b/10290.cpp new file mode 100644 index 0000000..5342256 --- /dev/null +++ b/10290.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define MAXN 3000001 +#define SMAXN 5478 + +bool sieve[MAXN]; +int p[216815]; + +int main() +{ + int c, i, j, r, pc = 0; + long long N; + memset(sieve, 1, MAXN); + for (i = 3; i < SMAXN; i += 2) + { + if (sieve[i]) + { + for (p[pc++] = i, j = i * i; j < MAXN; j += i) + { + sieve[j] = 0; + } + } + } + for (i = SMAXN + 1; i < MAXN; i += 2) + { + if (sieve[i]) + { + p[pc++] = i; + } + } + while (scanf("%lld", &N) == 1) + { + r = 1; + if (N) + { + while (!(N & 1)) + { + N >>= 1; + } + } + if (N) + { + for (i = 0; i < pc; i++) + { + if (!(N % p[i])) + { + c = 0; + do + { + N /= p[i], c++; + } while (!(N % p[i])); + r *= (c + 1); + if (N == 1 || (N < MAXN && sieve[N])) + { + break; + } + } + } + } + r <<= (N > 1); + printf("%d\n", r); + } + return 0; +} diff --git a/10291.cpp b/10291.cpp new file mode 100644 index 0000000..34a13a4 --- /dev/null +++ b/10291.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; +} p[51]; +Point origin = {0, 0}; +int len[51]; +int n, ptr, total; +const double PI = 3.1415926535897932384626433832795; +double cross(Point &a, Point &b, Point &ref) +{ + return (a.x - ref.x) * (b.y - ref.y) - (a.y - ref.y) * (b.x - ref.x); +} +bool check() +{ + double area = 0; + for (int i = 0; i < ptr; i++) + { + area += p[i].x * p[i + 1].y - p[i].y * p[i + 1].x; + } + area = fabs(area) * 0.5; + double sum = 0; + for (int i = 0; i < ptr; i++) + { + double c = cross(p[i], p[i + 1], origin); + c = fabs(c) * 0.5; + if (c <= 1e-10) + { + return false; + } + sum += c; + } + if (fabs(sum - area) <= 1e-10) + { + return true; + } + else + { + return false; + } +} + +int main() +{ + int x = 0; + while (scanf("%d", &n) == 1) + { + int min = 99999999; + if (!n) + { + break; + } + if (x++) + { + puts(""); + } + for (int i = 0; i < n; i++) + { + scanf("%d", &len[i]); + } + for (int i = 0; i < n; i++) + { + ptr = total = 0; + for (int j = 0; j < n; j++) + { + if (len[j] >= len[i]) + { + p[ptr].x = cos(2 * j * PI / n); + p[ptr].y = sin(2 * j * PI / n); + ptr++; + total += len[j] - len[i]; + } + } + p[ptr] = p[0]; + if (ptr < 3) + { + continue; + } + if (check() && total < min) + { + min = total; + } + } + printf("%d\n", min); + } + return 0; +} diff --git a/10293.cpp b/10293.cpp new file mode 100644 index 0000000..58f4f4f --- /dev/null +++ b/10293.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +char buf[65536]; +int freq[128]; + +int main() +{ + char *p; + int i; + memset(freq, 0, sizeof(freq)); + i = 0; + while (gets(buf)) + { + if (buf[0] == '#') + { + for (i = 1; i < 80; i++) + if (freq[i] != 0) + { + printf("%d %d\n", i, freq[i]); + } + printf("\n"); + memset(freq, 0, sizeof(freq)); + i = 0; + continue; + } + if (buf[0] == '\0') + { + continue; + } + for (p = buf; *p; p++) + { + if (isalpha(*p)) + { + i++; + } + else if (*p != '\'' && *p != '-') + { + freq[i]++; + i = 0; + } + } + if (p[-1] != '-') + { + freq[i]++; + i = 0; + } + } + return 0; +} diff --git a/10294.cpp b/10294.cpp new file mode 100644 index 0000000..7e13e31 --- /dev/null +++ b/10294.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +// Polya-Burnside counting + +typedef vector VI; + +long long doit(int N, int C, bool canFlip) +{ + VI p(N); + set G; + for (int f = 0; f < 2; f++) + { + for (int s = 0; s < N; s++) + { + for (int i = 0; i < N; i++) + { + p[i] = (i + s) % N; + } + if (f == 1) + { + reverse(p.begin(), p.end()); + } + G.insert(p); + } + if (!canFlip) + { + break; + } + } + long long res = 0; + for (set::iterator it = G.begin(); it != G.end(); ++it) + { + p = *it; + long long t = 1; + for (int i = 0; i < N; i++) + { + if (p[i] < 0) + { + continue; + } + for (int x = p[i]; p[x] != -1;) + { + int y = p[x]; + p[x] = -1; + x = y; + } + t *= C; + } + res += t; + } + return res / G.size(); +} + +int main() +{ + int N, C; + while (cin >> N >> C) + { + cout << doit(N, C, false) << ' ' << doit(N, C, true) << '\n'; + } +} diff --git a/10295.cpp b/10295.cpp index e171208..2bfbe6a 100644 --- a/10295.cpp +++ b/10295.cpp @@ -1,28 +1,101 @@ -#include -using namespace std; -int main(){ - long long n,m,i; - cin>>n>>m; - long long value[n]; - string job[n]; - for(i=0;i>job[i]>>value[i]; - while(m--){ - long long sum=0; - for(;;){ - - string str; - cin>>str; - if(str==".") - break; - for(i=0;i + +using namespace std; + +int numOfWords, numOfDescriptions; +char cmd[10000000]; + +struct Dictionary +{ + char name[20]; + double value; +} dic[1010]; + +void input() +{ + int i; + for (i = 0; i < numOfWords; i++) + { + cin >> dic[i].name >> dic[i].value; + } +} + +int cmp(const void *a, const void *b) +{ + Dictionary *aa = (Dictionary *)a; + Dictionary *bb = (Dictionary *)b; + return strcmp(aa->name, bb->name); +} + +// binary search +int bin_search(char *p) +{ + int low, high, mid; + low = 0; + high = numOfWords - 1; + while (low <= high) + { + mid = (low + high) / 2; + if (!strcmp(dic[mid].name, p)) + { + return mid; + } + else if (strcmp(p, dic[mid].name) < 0) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } + return -1; +} + +void solve() +{ + input(); + qsort(dic, numOfWords, sizeof(Dictionary), cmp); + char *p; + int i; + cin.get(); + for (i = 0; i < numOfDescriptions; i++) + { + double cnt = 0.0; + while (cin.getline(cmd, 100000)) + { + int index; + if (!strcmp(cmd, ".")) + { + break; + } + p = strtok(cmd, " "); + if (p) + { + index = bin_search(p); + if (index >= 0) + { + cnt += dic[index].value; + } + } + while (p = strtok(NULL, " ")) + { + index = bin_search(p); + if (index >= 0) + { + cnt += dic[index].value; + } + } + } + cout << cnt << endl; + } +} + +int main() +{ + while (cin >> numOfWords >> numOfDescriptions) + { + solve(); + } + return 0; +} diff --git a/10296.cpp b/10296.cpp new file mode 100644 index 0000000..a29b2d8 --- /dev/null +++ b/10296.cpp @@ -0,0 +1,173 @@ +#include + +using namespace std; + +#define maxn 20 +#define INF 10000000 +#define MIN(a, b) (a > b ? b : a) + +int Cost[maxn][maxn], N, E, edge; +int Deg[maxn], mincost, totalcost; +struct ss +{ + int cost; + int u, v; +}; +ss Edge[250]; +char Fg[maxn]; + +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return x->cost - y->cost; +} + +void Set() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + Cost[i][j] = Cost[j][i] = INF; + } + Cost[i][i] = Deg[i] = 0; + } +} + +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + Cost[i][j] = MIN(Cost[i][j], Cost[i][k] + Cost[k][j]); + } + } + } +} + +void Make_pair() +{ + int i, j; + edge = 0; + for (i = 1; i < N; i++) + { + if (Deg[i] % 2 == 0) + { + continue; + } + for (j = i + 1; j <= N; j++) + { + if (Deg[j] % 2 == 0) + { + continue; + } + Edge[edge].u = i; + Edge[edge].v = j; + Edge[edge++].cost = Cost[i][j]; + } + } +} + +int H(int ind, int level, int terget) +{ + int i, k = 0, j = 0; + for (i = ind + 1; j < (terget - level); i++, j++) + { + k += Edge[i].cost; + } + return k; +} + +void Recur(int n, int level, int terget, int cost) +{ + int i; + if (cost >= mincost) + { + return; + } + if (level == terget) + { + if (cost < mincost) + { + mincost = cost; + } + return; + } + if (cost + H(n, level, terget) >= mincost) + { + return; + } + Fg[Edge[n].u] = Fg[Edge[n].v] = 1; + for (i = n + 1; i < edge; i++) + { + if (Fg[Edge[i].u] || Fg[Edge[i].v]) + { + continue; + } + if ((edge - i) < (terget - level)) + { + break; + } + Recur(i, level + 1, terget, cost + Edge[i].cost); + } + Fg[Edge[n].u] = Fg[Edge[n].v] = 0; +} + +void Cal() +{ + int i, k = 0; + for (i = 1; i <= N; i++) + { + if (Deg[i] % 2) + { + k++; + } + } + if (k == 0) + { + cout << totalcost << endl; + return; + } + Floyd(); + Make_pair(); + if (edge == 1) + { + cout << Edge[0].cost + totalcost << endl; + return; + } + qsort(Edge, edge, sizeof(ss), com); + mincost = 99999999; + for (i = 0; i + 1 < edge; i++) + { + Recur(i, 1, k / 2, Edge[i].cost); + } + cout << mincost + totalcost << endl; +} + +int main() +{ + int i, u, v, c; + while (cin >> N && N) + { + Set(); + cin >> E; + totalcost = 0; + for (i = 0; i < E; i++) + { + cin >> u >> v >> c; + totalcost += c; + if (Cost[u][v] > c) + Cost[u][v] = Cost[v][u] = c; + Deg[u]++; + Deg[v]++; + } + Cal(); + } + return 0; +} diff --git a/10297.cpp b/10297.cpp index 22dc62d..a53fb7f 100644 --- a/10297.cpp +++ b/10297.cpp @@ -1,18 +1,19 @@ -#include -#include -#include -#include -#define PI 3.141592653589793 -using namespace std; -int main(){ - for(;;){ - double D,V; - scanf("%lf%lf",&D,&V); - if(D==0 && V==0) - break; - double val=D*D*D - 6*V/PI; - double d=pow(val,1.0/3.0); - printf("%.3lf\n",d); - } - return 0; -} +#include + +using namespace std; + +const double pi = 3.1415926; + +int main() +{ + double D, V; + while (cin >> D >> V) + { + if (D == 0.0f && V == 0.0f) + { + break; + } + printf("%.3lf\n", pow((D * D * D - 6 * V / pi), (1 / 3.0))); + } + return 0; +} diff --git a/10298.cpp b/10298.cpp index 772fec4..b966f57 100644 --- a/10298.cpp +++ b/10298.cpp @@ -1,27 +1,32 @@ -#include -using namespace std; -int main(){ - for(;;){ - long long i=0,min=10000000; - string str; - getline(cin,str,'\n'); - if(str==".") - break; - if(str==""){ - cout<<"0\n"; - continue; - } - int len=str.length(); - int allStr[130]={0},actStr[130]={0}; - for(i=0;i + +using namespace std; + +char inp[1000005]; + +int main() +{ + int len, i; + gets(inp); + while (inp[0] != '.' || inp[1]) + { + len = 1; + for (i = 1; inp[i]; i++) + { + while (inp[i] != inp[i % len]) + { + len++; + } + } + if (i % len == 0) + { + printf("%d\n", i / len); + } + else + { + puts("1"); + } + gets(inp); + } + return 0; +} diff --git a/10299.cpp b/10299.cpp new file mode 100644 index 0000000..393d61f --- /dev/null +++ b/10299.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define MAXN 46345 + +char ss[MAXN + 2]; +int Prime[7000], K, M; +int Store[1000]; + +void Prime_table() +{ + int i, j; + K = 0; + for (i = 2; i <= sqrt(MAXN);) + { + for (j = i + i; j <= MAXN; j += i) + { + ss[j] = 1; + } + for (i++; ss[i]; i++) + ; + } + for (i = 2; i <= MAXN; i++) + if (!ss[i]) + { + Prime[K++] = i; + } +} +void FindValue(int n) +{ + int i; + int Sum = 1; + Sum = n - n / Store[0]; + for (i = 1; i < M; i++) + { + Sum = Sum - (Sum / Store[i]); + } + printf("%d\n", Sum); +} +void Com(int n) +{ + int i, temp = n; + M = 0; + for (i = 0; i < K && Prime[i] <= sqrt(n); i++) + { + if (n % Prime[i] == 0) + { + Store[M++] = Prime[i]; + while (n % Prime[i] == 0) + { + n /= Prime[i]; + if (n == 1) + { + goto done; + } + } + } + } +done:; + if (n != 1) + { + Store[M++] = n; + } + FindValue(temp); +} + +int main() +{ + int n; + Prime_table(); + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + if (n == 1) + { + printf("0\n"); + continue; + } + Com(n); + } + return 0; +} diff --git a/103.cpp b/103.cpp new file mode 100644 index 0000000..f78d0c6 --- /dev/null +++ b/103.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +struct box +{ + int num, d, dimensions[10]; +}; + +struct res +{ + int len, pos; +}; + +// tst if box a fits into box b +inline bool box_fits(const box &a, const box &b) +{ + for (int i = 0; i < a.d; i++) + if (a.dimensions[i] >= b.dimensions[i]) + return false; + return true; +} + +inline bool ordering(const box &a, const box &b) +{ + for (int i = 0; i < a.d; i++) + { + if (a.dimensions[i] >= b.dimensions[i]) + { + if (box_fits(b, a)) + { + return true; + } + else + { + return a.num > b.num; + } + } + } + return false; +} + +static int n, k, dp[30]; +static box boxes[30]; +static res best, tmp; + +int main() +{ + while (scanf("%d%d", &n, &k) == 2) + { + for (int i = 0; i < 30; i++) + { + dp[i] = 1; + } + best.pos = 0; + best.len = 1; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < k; j++) + { + scanf("%d", &boxes[i].dimensions[j]); + } + boxes[i].d = k; + boxes[i].num = i + 1; + sort(boxes[i].dimensions, boxes[i].dimensions + k); + } + sort(boxes, boxes + n, ordering); + for (int i = 0; i < n; i++) + { + if (boxes[i].num == 1) + { + best.pos = i; + } + } + for (int i = 1; i < n; i++) + { + for (int j = 0; j < i; j++) + { + if (box_fits(boxes[i], boxes[j])) + { + dp[i] = max(dp[i], dp[j] + 1); + if (dp[i] > best.len) + { + best.len = dp[i]; + best.pos = i; + } + } + } + } + printf("%d\n%d", best.len, boxes[best.pos].num); + best.len--; + while (best.len) + { + printf(" "); + tmp.len = 2000000; + tmp.pos = 0; + for (int i = 0; i < best.pos; i++) + { + if (dp[i] == best.len && box_fits(boxes[best.pos], boxes[i])) + { + tmp.len = min(tmp.len, boxes[i].num); + if (tmp.len == boxes[i].num) + tmp.pos = i; + } + } + printf("%d", boxes[tmp.pos].num); + best.pos = tmp.pos; + best.len--; + } + printf("\n"); + } + return 0; +} diff --git a/1030.cpp b/1030.cpp new file mode 100644 index 0000000..da53bd9 --- /dev/null +++ b/1030.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +char view[6][15][15], mp[15][15][15], n; + +void get(int i, int j, int k, int dep, int &x, int &y, int &z) +{ + switch (i) + { + case 0: + x = j; + y = k; + z = dep; + break; + case 1: + x = j; + y = dep; + z = n - k - 1; + break; + case 2: + x = j; + y = n - k - 1; + z = n - dep - 1; + break; + case 3: + x = j; + y = n - dep - 1; + z = k; + break; + case 4: + x = dep; + y = k; + z = n - j - 1; + break; + case 5: + x = n - dep - 1; + y = k; + z = j; + break; + } +} + +char read_char() +{ + char ch; + while (true) + { + ch = getchar(); + if ((ch >= 'A' && ch <= 'Z') || ch == '.') + { + return ch; + } + } +} + +int main() +{ + int x, y, z; + while (scanf("%d", &n), n) + { + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < 6; ++j) + { + for (int k = 0; k < n; ++k) + { + view[j][i][k] = read_char(); + } + } + } + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + for (int k = 0; k < n; ++k) + { + mp[i][j][k] = '#'; + } + } + } + for (int i = 0; i < 6; ++i) + { + for (int j = 0; j < n; ++j) + { + for (int k = 0; k < n; ++k) + { + if (view[i][j][k] == '.') + { + for (int dep = 0; dep < n; ++dep) + { + get(i, j, k, dep, x, y, z); + mp[x][y][z] = '.'; + } + } + } + } + } + + while (true) + { + bool done(true); + for (int i = 0; i < 6; ++i) + for (int j = 0; j < n; ++j) + for (int k = 0; k < n; ++k) + if (view[i][j][k] != '.') + for (int dep = 0; dep < n; ++dep) + { + get(i, j, k, dep, x, y, z); + if (mp[x][y][z] == '.') + { + continue; + } + if (mp[x][y][z] == '#') + { + mp[x][y][z] = view[i][j][k]; + break; + } + if (mp[x][y][z] == view[i][j][k]) + { + break; + } + mp[x][y][z] = '.'; + done = false; + } + if (done) + { + break; + } + } + int ans = 0; + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + for (int k = 0; k < n; ++k) + { + if (mp[i][j][k] != '.') + { + ++ans; + } + } + } + } + printf("Maximum weight: %d gram(s)\n", ans); + } + return 0; +} diff --git a/10300.cpp b/10300.cpp index 7c2a175..794abf3 100644 --- a/10300.cpp +++ b/10300.cpp @@ -1,23 +1,21 @@ -#include - -using namespace std; - -int main() -{ - int n,f,f_det[20][3],sum,i,j; - cin>>n; - for(int z=0;z>f; - for(int i=0;i>f_det[i][j]; - - for(int i=0;i + +using namespace std; + +int main() +{ + int n, f, t, s, a, b, c; + cin >> n; + for (t = 0; t < n; t++) + { + cin >> f; + int sum = 0; + for (s = 0; s < f; s++) + { + cin >> a >> b >> c; + sum += a * c; + } + cout << sum << endl; + } + return 0; +} diff --git a/10301.cpp b/10301.cpp new file mode 100644 index 0000000..d611fb6 --- /dev/null +++ b/10301.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +const int MAXRINGS = 101; +const int oo = 0x7fffffff; +const double delta = 0.0000001; + +int numOfRings, maxRings; +bool visited[MAXRINGS]; + +struct Rings +{ + double x, y; + double radius; +} rings[MAXRINGS]; + +void Input() +{ + int i; + for (i = 0; i < numOfRings; i++) + { + scanf("%lf%lf%lf", &rings[i].x, &rings[i].y, &rings[i].radius); + } +} + +double distance(Rings a, Rings b) +{ + double xx = a.x - b.x; + double yy = a.y - b.y; + return sqrt(xx * xx + yy * yy); +} + +bool isGlued(Rings a, Rings b) +{ + double d = distance(a, b); + if (d <= fabs(a.radius + b.radius) + delta && d >= fabs(a.radius - b.radius) + delta) + { + return true; + } + else + { + return false; + } +} + +void dfs(int index, int &count) +{ + count++; + if (count > maxRings) + { + maxRings = count; + } + visited[index] = true; + for (int w = 0; w < numOfRings; w++) + { + if (!visited[w] && isGlued(rings[w], rings[index])) + { + dfs(w, count); + } + } +} + +void Solve() +{ + maxRings = 0; + int root, cnt; + for (root = 0; root < numOfRings; root++) + { + cnt = 0; + memset(visited, 0, sizeof(visited)); + dfs(root, cnt); + } +} + +int main() +{ + while (scanf("%d", &numOfRings) == 1 && numOfRings >= 0) + { + Input(); + Solve(); + if (maxRings != 1) + { + printf("The largest component contains %d rings.\n", maxRings); + } + else + { + printf("The largest component contains %d ring.\n", maxRings); + } + } + return 0; +} diff --git a/10302.cpp b/10302.cpp new file mode 100644 index 0000000..61a92a8 --- /dev/null +++ b/10302.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + long long int x, ans; + while (scanf("%lld", &x) == 1) + { + printf("%lld\n", (x * x * (x + 1) * (x + 1)) / 4LL); + } + return 0; +} diff --git a/10303.cpp b/10303.cpp new file mode 100644 index 0000000..c310b27 --- /dev/null +++ b/10303.cpp @@ -0,0 +1,325 @@ +// + 424 +// - 11448 +// / 10494 +// % 10814 10494 +// * 623 324 +// < 728 + +// dont use (BigInt % int) in your code ! + +#include +#include +#include +#include +#include +#include +#include + +#define REP(i, n) for (int i = 0; i < (int)n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() + +using namespace std; + +const static int BASE = 10000, LANGE = 4; +const string BigIntBase = "10000"; + +enum +{ + LESS, + EQUAL, + GREATER +}; + +#define FRONT "%d" +#define FORMAT "%04d" + +class BigInt +{ +public: + bool neg; + vector value; + + BigInt(void) + : neg(false) + { + value.resize(1, 0); + } + BigInt(int s) + : neg(false) + { + //value.push_back(s); + value.resize(s); + } + BigInt(vector v, bool sign) + : neg(sign), value(v) + { + } + BigInt(string in) + { + neg = false; + if (in[0] == '-') + { + neg = true; + in.erase(in.begin()); + } + while (in.size() % LANGE != 0) + in.insert(in.begin(), 1, '0'); + for (int i = in.size() - LANGE; i >= 0; i -= LANGE) + value.push_back(atoi(in.substr(i, LANGE).c_str())); + regulation(); + } + BigInt operator+(int n) + { + assert(0 <= n); + BigInt re = *this; + re.value[0] += n; + re.regulation(); + return re; + } + BigInt operator+(BigInt in) + { + if (sign() == in.sign()) + return add(*this, in, sign()); + else if (compare(in, true) == LESS) + return sub(in, *this, in.sign()); + else + return sub(*this, in, sign()); + } + BigInt operator*(BigInt in) + { + return mul(*this, in, sign() != in.sign()); + } + BigInt operator*(int in) + { + assert(in < BASE); + BigInt re = *this; + REP(i, size()) + re[i] *= in; + re.regulation(); + re.neg = in < 0; + return re; + } + BigInt operator-(BigInt in) + { + if (sign() != in.sign()) + return add(*this, in, sign()); + else if (compare(in, true) == GREATER)// != LESS ? + return sub(*this, in, sign()); + else + return sub(in, *this, !in.sign()); + } + BigInt operator/(BigInt in) + { + return div(*this, in, (sign() != in.sign())); + } + BigInt operator/(int n) + { + return div(*this, n); + } + BigInt operator%(BigInt in) + { + BigInt tmp = *this - ((*this / in) * in); + tmp.regulation(); + return tmp; + } + bool operator==(BigInt in) + { + return compare(in, false) == EQUAL; + } + bool operator<(BigInt in) + { + return compare(in, false) == LESS; + } + bool operator>(BigInt in) + { + return compare(in, false) == GREATER; + } + int &operator[](int n) + { + return value[n]; + } + string toString(void) + { + char buffer[LANGE + 1]; + string re; + + if (neg == true) + re = "-"; + sprintf(buffer, FRONT, value.back()), re += buffer; + + for (int i = value.size() - 2; i >= 0; i--) + sprintf(buffer, FORMAT, value[i]), re += buffer; + return re; + } + int size(void) + { + return value.size(); + } + void resize(int n) + { + value.resize(n); + } + bool sign(void) + { + return neg; + } + void regulation(void) + { + value.push_back(0); + REP(i, value.size() - 1) + { + value[i + 1] += value[i] / BASE; + value[i] %= BASE; + } + while (value.size() > 1 && value.back() == 0) + value.pop_back(); + if (value.size() == 1 && value[0] == 0) + { + neg = false; + } + return; + } + int compare(BigInt in, bool abs) + { + if (abs) + { + in.neg = sign(); + return compare(in, false); + } + if (sign() != in.sign()) + { + return sign() ? LESS : GREATER; + } + if (size() != in.size()) + { + if (!sign()) + return size() > in.size() ? GREATER : LESS; + else + return size() > in.size() ? LESS : GREATER; + } + for (int i = in.size(); i--;) + { + if (value[i] == in.value[i]) + continue; + return (value[i] > in.value[i]) ? GREATER : LESS; + } + return EQUAL; + } + BigInt add(BigInt a, BigInt b, bool sign) + { + if (a.size() < b.size()) + swap(a, b); + REP(i, b.size()) + a[i] += b[i]; + a.regulation(); + a.neg = sign; + return a; + } + BigInt sub(BigInt a, BigInt b, bool sign) + { + BigInt re = BigInt(a.value, sign); + re.value.resize(a.size(), 0); + int borrow = 0; + REP(i, re.size()) + { + if (i < b.size()) + re[i] -= b[i] + borrow; + else + re[i] -= borrow; + if (re[i] >= 0) + borrow = 0; + else + borrow = 1, re[i] += BASE; + } + re.regulation(); + return re; + } + BigInt mul(BigInt a, BigInt b, bool sign) + { + BigInt re; + re.value.resize(a.size() + b.size() + 1); + REP(i, a.size()) + { + REP(j, b.size()) + re[i + j] += a[i] * b[j]; + } + re.neg = sign; + re.regulation(); + return re; + } + BigInt div(BigInt a, int n) + { + int c = 0, tmp; + for (int i = a.size(); i--;) + { + tmp = BASE * c + a[i]; + a[i] = tmp / n; + c = tmp % n; + } + a.regulation(); + return a; + } + BigInt div(BigInt a, BigInt b, bool sign) + { + if (a < b) + return BigInt("0"); + int f = BASE / (b[(int)b.size() - 1] + 1); + a = a * f; + b = b * f; + BigInt c((int)a.size() - (int)b.size() + 1); + for (int i = (int)a.size() - 1, k = (int)c.size() - 1; k >= 0; --i, --k) + { + c[k] = (i + 1 < a.size() ? a[i + 1] : 0) * BASE; + c[k] = (c[k] + a[i]) / b[(int)b.size() - 1]; + BigInt d(k + b.size()); + REP(j, b.size()) + { + d[k + j] = c[k] * b[j]; + } + d.regulation(); + while (a < d) + { + --c[k]; + REP(j, b.size()) + { + d[k + j] -= b[j]; + } + REP(j, d.size()) + { + while (d.value[j] < 0) + { + --d.value[j + 1]; + d.value[j] += BASE; + } + } + d.regulation(); + } + a = a - d; + } + c.neg = sign; + c.regulation(); + return c; + } +}; + +const int T = 1000 + 1; +BigInt t[T]; + +int main(void) +{ + t[0] = BigInt("1"); + for (int i = 1; i < T; ++i) + { + int a = 2 * (2 * i + 1); + BigInt b = t[i - 1] * a; + t[i] = b / (i + 2); + } + + int n; + while (cin >> n) + { + cout << t[n - 1].toString() << endl; + } + return 0; +} diff --git a/10304.cpp b/10304.cpp new file mode 100644 index 0000000..1e03fdf --- /dev/null +++ b/10304.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +#define FOR(i, s, n) for (int i(s); i <= n; ++i) +const int N = 256; +int acc[N], c[N], dp[N][N], r[N][N]; + +inline int min(int a, int b) +{ + return a < b ? a : b; +} + +int main() +{ + acc[0] = 0; + for (int n; scanf("%d", &n) == 1;) + { + FOR(i, 1, n) + { + scanf("%d", c + i); + acc[i] = c[i] + acc[i - 1]; + r[i][i] = i; + } + FOR(i, 1, n) + FOR(j, 1, n) dp[i][j] = 1048576; + FOR(i, 1, n) + dp[i][i - 1] = dp[i][i] = 0; + FOR(L, 1, n) + FOR(i, 1, n - L) + { + int j = i + L, t, tc = acc[j] - acc[i - 1]; + FOR(k, r[i][j - 1], r[i + 1][j]) + { + t = dp[i][k - 1] + tc - c[k] + dp[k + 1][j]; + if (t < dp[i][j]) + { + r[i][j] = k; + dp[i][j] = t; + } + } + } + printf("%d\n", dp[1][n]); + } + return 0; +} diff --git a/10305.cpp b/10305.cpp new file mode 100644 index 0000000..77ff3ce --- /dev/null +++ b/10305.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + vector result; + void solve(int n); + void visit(int n, bool *visited); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::solve(int n) +{ + Vertex *v; + bool nodes[n + 1]; + bool visited[n + 1]; + fill(nodes, nodes + n + 1, true); + fill(visited, visited + n + 1, false); + // get all nodes with no incoming edges + for (int i = 1; i <= n; i++) + { + v = getVertex(i); + for (int j = 0, sz = v->adj.size(); j < sz; j++) + { + nodes[v->adj[j]->id] = false; + } + } + for (int i = 1; i <= n; i++) + { + if (nodes[i]) + { + visit(i, visited); + } + } +} + +void Graph::visit(int n, bool *visited) +{ + if (visited[n]) + { + return; + } + visited[n] = true; + Vertex *v = getVertex(n); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + visit(v->adj[i]->id, visited); + } + result.push_back(n); +} + +int main() +{ + int n, m; + int a, b; + while (cin >> n >> m) + { + if (n == 0 && m == 0) + { + break; + } + Graph g; + for (int i = 1; i <= n; i++) + { + g.addVertex(i); + } + while (m--) + { + cin >> a >> b; + //g.addEdge(a, b, true); + g.addEdge(b, a, true); + } + g.solve(n); + for (int i = 0; i < n; i++) + { + if (i > 0) + { + cout << " "; + } + cout << g.result[i]; + } + cout << endl; + } + return 0; +} diff --git a/10306.cpp b/10306.cpp new file mode 100644 index 0000000..c31ca67 --- /dev/null +++ b/10306.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define N 301 +#define INF 10000 + +int min_[N][N], v1[41], v2[41]; + +int main() +{ + int c, m, S, square, least, i, j, k; + scanf("%d", &c); + while (c--) + { + scanf("%d%d", &m, &S); + for (i = 0; i < m; i++) + { + scanf("%d%d", v1 + i, v2 + i); + } + for (i = 0; i <= S; i++) + { + for (j = 0; j <= S; j++) + { + min_[i][j] = INF; + } + } + min_[0][0] = 0; + for (i = 0; i < m; i++) + { + for (j = v1[i]; j <= S; j++) + { + for (k = v2[i]; k <= S; k++) + { + min_[j][k] = min(min_[j][k], 1 + min_[j - v1[i]][k - v2[i]]); + } + } + } + square = S * S; + least = INF; + for (i = 0; i <= S; i++) + { + for (j = 0; j <= S; j++) + { + if (i * i + j * j == square) + { + least = min(least, min_[i][j]); + } + } + } + if (least == INF) + { + puts("not possible"); + } + else + { + printf("%d\n", least); + } + } + return 0; +} diff --git a/10307.cpp b/10307.cpp new file mode 100644 index 0000000..99d73e3 --- /dev/null +++ b/10307.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +#define MAXN 52 +#define MAX 10000 + +char Maze[MAXN][MAX], Flag[MAXN][MAX], flg[102][102]; +int node[MAXN][MAX], p[102], rank_[102], R, C, edg, ali, QH, QT; + +int X[] = {-1, 0, 0, 1}; +int Y[] = {0, -1, 1, 0}; + +struct queue_ +{ + int r, c, dis; +} Q[MAX]; + +struct ss +{ + int r, c; +} st[106]; + +struct edge +{ + int u, v; + int weight; +} E[100000]; + +int cmp(const void *a, const void *b) +{ + edge *x = (edge *)a; + edge *y = (edge *)b; + return x->weight - y->weight; +} +void Push(int r, int c, int dis) +{ + Q[QH].r = r; + Q[QH].dis = dis; + Q[QH++].c = c; + QH %= MAX; +} +queue_ Pop() +{ + queue_ temp; + temp.r = Q[QT].r; + temp.dis = Q[QT].dis; + temp.c = Q[QT++].c; + QT %= MAX; + return temp; +} +int IsEmpty() +{ + return QH == QT; +} +void BFS(int r, int c, int in) +{ + int i, j, k, dis, nr, nc, co = 0; + queue_ t; + QH = QT = 0; + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + Flag[i][j] = 0; + Q[i * C + j].dis = 0; + } + } + Push(r, c, 0); + Flag[r][c] = 1; + while (!IsEmpty()) + { + t = Pop(); + r = t.r; + c = t.c; + dis = t.dis; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + nr = X[i] + r; + nc = Y[i] + c; + if (nc > C || nc < 0 || nr > R || nr < 0) + { + continue; + } + if (Flag[nr][nc] == 1 || Maze[nr][nc] == '#') + { + continue; + } + if (Maze[nr][nc] == 'A' || Maze[nr][nc] == 'S') + { + k = node[nr][nc]; + if (!flg[in][k]) + { + E[edg].u = in; + E[edg].v = k; + E[edg++].weight = dis + 1; + flg[in][k] = flg[k][in] = 1; + co++; + if (co == ali - 1) + { + return; + } + } + } + Flag[nr][nc] = 1; + Push(nr, nc, dis + 1); + } + } + } +} +void Ini() +{ + int i; + for (i = 1; i <= ali; i++) + { + p[i] = i; + rank_[i] = 0; + } +} +int FindSet(int s) +{ + if (s != p[s]) + { + p[s] = FindSet(p[s]); + } + return p[s]; +} +void Link(int x, int y) +{ + if (rank_[x] > rank_[y]) + { + p[y] = x; + } + else + { + p[x] = y; + if (rank_[x] == rank_[y]) + { + rank_[y]++; + } + } +} +void krukal() +{ + int i, sum = 0, mstLen = 0; + int x, y; + Ini(); + qsort(E, edg, sizeof(E[0]), cmp); + for (i = 0; i < edg; i++) + { + x = FindSet(E[i].u); + y = FindSet(E[i].v); + if (x != y) + { + mstLen++; + Link(x, y); + sum += E[i].weight; + if (mstLen == ali - 1) + { + break; + } + } + } + printf("%d", sum); +} +void Cal() +{ + int i; + for (i = 0; i < ali - 1; i++) + { + BFS(st[i].r, st[i].c, i + 1); + } + krukal(); +} +int main() +{ + int kase, i, j; + char input[100]; + gets(input); + sscanf(input, "%d", &kase); + while (kase--) + { + gets(input); + sscanf(input, "%d%d", &C, &R); + ali = edg = 0; + for (i = 0; i < R; i++) + { + gets(Maze[i]); + for (j = 0; Maze[i][j]; j++) + { + if (Maze[i][j] == 'A' || Maze[i][j] == 'S') + { + node[i][j] = ali + 1; + st[ali].r = i; + st[ali].c = j; + ali++; + } + } + } + Cal(); + putchar('\n'); + if (kase) + { + for (i = 1; i < ali; i++) + { + for (j = i + 1; j <= ali; j++) + { + flg[i][j] = flg[j][i] = 0; + } + } + } + } + return 0; +} diff --git a/10308.cpp b/10308.cpp new file mode 100644 index 0000000..c10e052 --- /dev/null +++ b/10308.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +#define MAXN 10002 + +int Mnode, Mdis, Q[MAXN], QH, QT; + +struct node +{ + int name; + int Val; + node *next; +}; +struct Edge +{ + int tempVal; + char colur; + node *next; +} V[MAXN]; + +int AddEdge(int n, int m, int v) +{ + node *p = (node *)malloc(sizeof(node)); + node *x = (node *)malloc(sizeof(node)); + V[m].tempVal = V[n].tempVal = 0; + p->name = m; + p->Val = v; + p->next = V[n].next; + V[n].next = p; + x->name = n; + x->Val = v; + x->next = V[m].next; + V[m].next = x; + return 0; +} +void Push(int n) +{ + Q[QH++] = n; + QH %= MAXN; +} +int Pop() +{ + int n; + n = Q[QT++]; + QT %= MAXN; + return n; +} +int IsEmpty() +{ + return QH == QT; +} +int BFS(int st) +{ + int i, j, k, v; + int max = -1, start; + for (i = 1; i <= Mnode; i++) + { + V[i].colur = 'w'; + V[i].tempVal = 0; + } + QH = QT = 0; + V[st].colur = 'r'; + Push(st); + while (!IsEmpty()) + { + k = Pop(); + node *vpt = V[k].next; + while (vpt != NULL) + { + j = vpt->name; + v = vpt->Val; + if (V[j].colur != 'r') + { + i = V[k].tempVal + v; + if (i > max) + { + max = i; + start = j; + Mdis = max; + } + V[j].tempVal = i; + V[j].colur = 'r'; + Push(j); + } + vpt = vpt->next; + } + } + return start; +} +int Cal(int n) +{ + int k; + k = BFS(n); + if (k == 0) + { + printf("0\n"); + return 0; + } + BFS(k); + printf("%d\n", Mdis); + return 0; +} + +int main() +{ + char input[100]; + int m, n, v, i; + while (gets(input)) + { + Mnode = 0; + n = m = v = 0; + sscanf(input, "%d%d%d", &n, &m, &v); + if (n == 0) + { + printf("0\n"); + gets(input); + continue; + } + AddEdge(n, m, v); + if (n > Mnode) + Mnode = n; + if (m > Mnode) + Mnode = m; + while (gets(input)) + { + for (i = 0; input[i]; i++) + if (input[i] == '\n') + input[i] = NULL; + if (strlen(input) <= 0) + break; + sscanf(input, "%d%d%d", &n, &m, &v); + AddEdge(n, m, v); + if (n > Mnode) + Mnode = n; + if (m > Mnode) + Mnode = m; + } + Cal(n); + for (i = 1; i <= Mnode; i++) + V[i].next = NULL; + } + return 0; +} diff --git a/10309.cpp b/10309.cpp new file mode 100644 index 0000000..905e860 --- /dev/null +++ b/10309.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +char board[15][15], boardcopy[15][15]; + +bool inside(int x, int y) +{ + return !(x < 0 || x > 9 || y < 0 || y > 9); +} + +void setLight(int x, int y) +{ + if (board[x][y] == '#') + { + board[x][y] = 'O'; + } + else + { + board[x][y] = '#'; + } +} + +void on(int x, int y) +{ + if (inside(x, y)) + { + board[x][y] = 'O'; + if (inside(x - 1, y)) + { + setLight(x - 1, y); + } + if (inside(x + 1, y)) + { + setLight(x + 1, y); + } + if (inside(x, y - 1)) + { + setLight(x, y - 1); + } + if (inside(x, y + 1)) + { + setLight(x, y + 1); + } + } +} + +void off(int x, int y) +{ + if (inside(x, y)) + { + board[x][y] = '#'; + if (inside(x - 1, y)) + { + setLight(x - 1, y); + } + if (inside(x + 1, y)) + { + setLight(x + 1, y); + } + if (inside(x, y - 1)) + { + setLight(x, y - 1); + } + if (inside(x, y + 1)) + { + setLight(x, y + 1); + } + } +} + +bool check() +{ + for (int i = 0; i < 10; i++) + if (board[9][i] == 'O') + { + return false; + } + return true; +} + +int solve() +{ + int i, j; + int cnt = 0; + for (i = 1; i < 10; i++) + for (j = 0; j < 10; j++) + if (board[i - 1][j] == 'O') + { + if (board[i][j] == '#') + { + on(i, j); + } + else + { + off(i, j); + } + cnt++; + } + if (check()) + { + return cnt; + } + else + { + return 100000000; + } +} + +int main() +{ + char name[100]; + while (cin >> name && strcmp(name, "end")) + { + int i, j; + for (i = 0; i < 10; i++) + for (j = 0; j < 10; j++) + { + cin >> board[i][j]; + boardcopy[i][j] = board[i][j]; + } + int state; + int min = 100000000; + for (state = 0; state < 1024; state++) + { + int temp = state; + int index = 0; + int cntStep = 0; + while (temp) + { + if (temp % 2 == 1) + { + if (board[0][index] == '#') + { + on(0, index); + } + else + { + off(0, index); + } + cntStep++; + } + temp /= 2; + index++; + } + int r = solve(); + if (r + cntStep < min) + { + min = r + cntStep; + } + for (i = 0; i < 10; i++) + for (j = 0; j < 10; j++) + { + board[i][j] = boardcopy[i][j]; + } + } + if (min == 100000000) + { + cout << name << " " << 0 << endl; + } + else + { + cout << name << " " << min << endl; + } + } + return 0; +} diff --git a/1031.cpp b/1031.cpp new file mode 100644 index 0000000..f0ef8d2 --- /dev/null +++ b/1031.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +vector putOrder[51][51]; // [m][i] + +int main() +{ + for (int m = 2; m < 50; m++) + { + for (int i = 0; i < m; i++) + { + int visited[50] = {}, pos = 0, skip = 0; + putOrder[m][i].push_back(pos), visited[pos] = 1; + for (int p = 1; p < m; p++) + { + skip = i % (m - p); + while (skip >= 0) + { + pos++; + if (pos >= m) + pos = 0; + if (!visited[pos]) + skip--; + } + visited[pos] = 1; + putOrder[m][i].push_back(pos); + } + } + } + // for (int i = 0; i < putOrder[15][6].size(); i++) // example + // printf("%d\n", (putOrder[15][6][i] + 1)%15); + char text[64]; + int cases = 0; + while (gets(text)) + { + if (!strcmp("X", text)) + break; + int m = strlen(text); + int appear[128] = {}; + for (int i = 0; text[i]; i++) + appear[text[i]]++; + + int mxlen = 1, remainUsed[64]; + set ret[64]; + for (int s = m - 1; s >= 0; s--) + { + if (appear[text[s]] < 2)// must not start alphabet + continue; + for (int n = m / 2; n >= mxlen; n--) + { + if (ret[n].size() > 1) + continue; + char plain[128]; + for (int i = 0; i < m; i++) + { + if (ret[n].size() > 1) + continue; + int cnt[128] = {}, used[64] = {}; + int ok = 1; + for (int p = 0; p < putOrder[m][i].size() && p < n; p++) + plain[p] = text[(putOrder[m][i][p] + s) % m], used[(putOrder[m][i][p] + s) % m] = 1; + plain[n] = '\0'; + for (int p = 0; p < n; p++) + { + cnt[plain[p]]++; + if (cnt[plain[p]] * 2 > appear[plain[p]]) + ok = 0; + } + if (!ok) + continue; + if (ret[n].find(plain) != ret[n].end()) + continue; + + int mm = 0; + for (int p = 0; p < m; p++) + { + if (!used[p]) + remainUsed[mm++] = p; + } + for (int t = 0; t < mm; t++) + { + if (plain[0] != text[remainUsed[t]]) + continue; + if (ret[n].size() > 1) + continue; + for (int j = i + 1; j < m; j++) + { + if (ret[n].size() > 1) + continue; + int visited[50] = {}; + int pos = t, skip = 0, ok2 = 1; + visited[pos] = 1; + for (int p = 1; p < n; p++) + { + skip = j % (mm - p); + while (skip >= 0) + { + pos++; + if (pos >= mm) + pos = 0; + if (!visited[pos]) + skip--; + } + if (plain[p] != text[remainUsed[pos]]) + { + ok2 = 0; + break; + } + visited[pos] = 1; + } + if (ok2) + { + if (n > mxlen) + mxlen = n; + ret[n].insert(plain); + } + } + } + } + } + } + printf("Code %d: ", ++cases); + for (int i = m / 2; i >= 0; i--) + { + if (ret[i].size()) + { + if (ret[i].size() == 1) + { + printf("%s\n", ret[i].begin()->c_str()); + } + else + { + puts("Codeword not unique"); + } + break; + } + } + } + return 0; +} diff --git a/10310.cpp b/10310.cpp index 4339f36..8708db5 100644 --- a/10310.cpp +++ b/10310.cpp @@ -1,32 +1,50 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - int n; - while(cin>>n){ - double gopX,gopY,dogX,dogY; - cin>>gopX>>gopY>>dogX>>dogY; - int ctr=0,i; - double hole[n][2]; - for(i=0;i>hole[i][0]>>hole[i][1]; - for(i=0;i=4*d1){ - printf("The gopher can escape through the hole at (%.3lf,%.3lf).\n",hole[i][0],hole[i][1]); - ctr=-1; - break; - } - } - if(ctr==0) - cout<<"The gopher cannot escape.\n"; - } - return 0; -} +#include + +using namespace std; + +double distance(double x1, double x2, double y1, double y2) +{ + double dis; + dis = (x1 - x2) * (x1 - x2); + dis += (y1 - y2) * (y1 - y2); + return dis; +} + +int main() +{ + int t; + double gx, gy, dx, dy, rx, ry; + double hx, hy, disg, disd; + bool escape; + while (scanf("%d", &t) == 1) + { + escape = false; + scanf("%lf %lf %lf %lf", &gx, &gy, &dx, &dy); + while (t--) + { + scanf("%lf %lf", &hx, &hy); + if (escape) + { + continue; + } + disg = distance(gx, hx, gy, hy); + disd = distance(dx, hx, dy, hy); + disd /= 4; + if (disg <= disd) + { + rx = hx; + ry = hy; + escape = true; + } + } + if (escape) + { + printf("The gopher can escape through the hole at (%.3lf,%.3lf).\n", rx, ry); + } + else + { + printf("The gopher cannot escape.\n"); + } + } + return 0; +} diff --git a/10311.cpp b/10311.cpp index 8b1b2f2..c27ee9d 100644 --- a/10311.cpp +++ b/10311.cpp @@ -1,72 +1,120 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 100000000 +#define MAXBITS 100000000 +#define USIZE (8 * sizeof(unsigned)) +unsigned bitarray[MAXBITS / USIZE]; +#define SETBIT(X) (bitarray[X / USIZE] |= 1 << (X % USIZE)) +#define RESETBIT(X) (bitarray[X / USIZE] &= ~(1 << (X % USIZE))) +#define GETBIT(X) ((bitarray[X / USIZE] & (1 << (X % USIZE))) ? 1 : 0) -bool prime[MAX + 5]; -vector P; +int P[5900000], tp; -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; +void sieve() +{ int i, j; - FOI(i, 2, MAX) - if( prime[i] ){ - P.push_back(i); - for (j = 2*i; j <= MAX; j += i) - prime[j] = false; + for (i = 1; i <= MAXBITS; i++) + { + SETBIT(i); + } + for (i = 2; i < sqrt(MAXBITS);) + { + for (j = i + i; j < MAXBITS; j += i) + { + RESETBIT(j); } + for (i++; !GETBIT(i); i++) + ; + } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int M = P.size(); - while( true ){ - int N, i; - scanf("%d", &N); - if( N == 0 ) - break; - bool gold = false; - FOI(i, 0, M-1){ - if( P[i] > N ) - break; - int NP = N - P[i]; - if( prime[NP] ){ - printf("%d is the sum of %d and %d.\n", N, P[i], NP); - gold = true; - break; +// binary search +int BinLo(int key) +{ + int lo = 1, up = tp - 1; + int mid = (lo + up) / 2; + while (lo < up) + { + if (P[mid] >= key) + { + if (P[mid - 1] < key) + { + return mid - 1; + } + up = mid - 1; + } + else if (P[mid] < key) + { + if (P[mid + 1] >= key) + { + return mid; } + lo = mid + 1; + } + mid = (lo + up) / 2; + } + return 0; +} + +void Cal(int n) +{ + int res1, res2, s1 = 1, s2 = tp - 1, dif, f = 1; + if (n <= 4) + { + printf("%d is not the sum of two primes!\n", n); + return; + } + if (n % 2) + { + dif = n - 2; + if (GETBIT(dif)) + { + printf("%d is the sum of 2 and %d.\n", n, dif); + } + else + { + printf("%d is not the sum of two primes!\n", n); + } + return; + } + s1 = BinLo(n / 2); + while (s1 > 0) + { + dif = n - P[s1]; + if (GETBIT(dif)) + { + res1 = P[s1]; + res2 = dif; + f = 0; + break; } - if( !gold ) - printf("%d is not the sum of two primes!\n", N); + s1--; + } + if (f) + { + printf("%d is not the sum of two primes!\n", n); + return; + } + printf("%d is the sum of %d and %d.\n", n, res1, res2); +} + +int main() +{ + int n; + P[0] = 2; + sieve(); + tp = 1; + for (int i = 3; i + 1 < MAXBITS; i += 2) + { + if (GETBIT(i)) + { + P[tp++] = i; + } + } + while (scanf("%d", &n) == 1) + { + Cal(n); } return 0; } diff --git a/10312.cpp b/10312.cpp new file mode 100644 index 0000000..3cb15e2 --- /dev/null +++ b/10312.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define maxn 30 + +typedef long long ss; + +ss B[maxn], BB[maxn]; + +void Brack() +{ + ss d; + B[1] = B[2] = 1; + B[3] = 3; + B[4] = 11; + for (ss i = 5; i <= 26; i++) + { + d = 3 * (2 * i - 3) * B[i - 1] - (i - 3) * B[i - 2]; + B[i] = d / i; + } +} +void Binary() +{ + BB[1] = 1; + ss n, k, sum; + for (n = 2; n <= 26; n++) + { + sum = 0; + for (k = 1; k < n; k++) + { + sum += BB[k] * BB[n - k]; + } + BB[n] = sum; + } +} + +int main() +{ + ss n, k; + Brack(); + Binary(); + while (scanf("%lld", &n) == 1) + { + printf("%lld\n", B[n] - BB[n]); + } + return 0; +} diff --git a/10313.cpp b/10313.cpp new file mode 100644 index 0000000..691784d --- /dev/null +++ b/10313.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define N 305; +#define min(a, b) (a < b) ? a : b + +unsigned long long int sum[305][305]; +unsigned long long int res[305][305]; + +int main() +{ + int i, j, k, tokens; + char inp[100]; + for (i = 0; i < 305; i++) + { + for (j = 0; j < 305; j++) + { + res[i][j] = 0; + } + } + res[0][0] = 1; + for (i = 1; i <= 300; i++) + { + for (j = i; j <= 300; j++) + { + for (k = 1; k <= 300; k++) + { + res[j][k] += res[j - i][k - 1]; + } + } + } + for (i = 0; i < 305; i++) + { + sum[0][i] = res[0][i]; + } + for (i = 0; i < 305; i++) + { + for (j = 1; j < 305; j++) + { + sum[i][j] = res[i][j] + sum[i][j - 1]; + } + } + while (gets(inp)) + { + tokens = sscanf(inp, "%d %d %d", &i, &j, &k); + if (tokens == 1) + { + printf("%llu\n", sum[i][i]); + } + else if (tokens == 2) + { + printf("%llu\n", sum[i][min(j, i)]); + } + else if (j <= i) + { + printf("%llu\n", sum[i][min(k, i)] - sum[i][j] + res[i][j]); + } + else + { + puts("0"); + } + } + return 0; +} diff --git a/10315.cpp b/10315.cpp new file mode 100644 index 0000000..6cb906d --- /dev/null +++ b/10315.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + +#include "10315.cpp" + +using namespace std; + +TEST(PokerHandTest, testFlush) { + PokerHand p; + p.fetchCard("TC JC 9C QC AC"); + EXPECT_TRUE(p.isFlush()); + EXPECT_TRUE(p.getRank() == FLUSH); + p.fetchCard("4S 9S TS QS 3S"); + EXPECT_TRUE(p.isFlush()); +} + +TEST(PokerHandTest, testStraight) { + PokerHand p; + p.fetchCard("TR JQ 9T QO KG"); + EXPECT_EQ(p.getRank(), STRAIGHT); + EXPECT_TRUE(p.isStraight()); +} + +TEST(PokerHandTest, testStraightFlush) { + PokerHand p; + p.fetchCard("2C 3C 4C 5C 6C"); + EXPECT_TRUE(p.isStraight()); + EXPECT_TRUE(p.isFlush()); + EXPECT_EQ(p.getRank(), STRAIGHT_FLUSH); +} + +TEST(PokerHandTest, test4Kind) { + PokerHand p; + p.fetchCard("4A 4B 4C 7A 4C"); + EXPECT_TRUE(p.is4Kind()); + EXPECT_EQ(p.getRank(), FOUR_KIND); +} + +TEST(PokerHandTest, testFullHouse) { + PokerHand p; + p.fetchCard("8C 2C 8D 2R 8E"); + EXPECT_EQ(p.getRank(), FULL_HOUSE); + EXPECT_TRUE(p.is3Kind()); + EXPECT_EQ(p.numPairs(), 2); + EXPECT_FALSE(p.is4Kind()); +} + +TEST(PokerHandTest, test3Kind) { + PokerHand p; + p.fetchCard("AD 2C AC 4V AH"); + EXPECT_TRUE(p.is3Kind()); + EXPECT_EQ(p.getRank(), THREE_KIND); +} + +TEST(PokerHandTest, testNumPairs) { + PokerHand p; + p.fetchCard("1C 2C 1E 4A 3A"); + EXPECT_EQ(p.numPairs(), 1); + EXPECT_EQ(p.getRank(), PAIR); + p.fetchCard("JC QR JH 4C QH"); + EXPECT_EQ(p.numPairs(), 2); + EXPECT_EQ(p.getRank(), TWO_PAIR); + p.fetchCard("3B 6H AH AC AY"); + EXPECT_EQ(p.numPairs(), 1); + EXPECT_EQ(p.getRank(), THREE_KIND); +} + +TEST(PokerHandTest, testHighCard) { + PokerHand p; + p.fetchCard("6C 7C 8H JQ KQ"); + EXPECT_EQ(p.getRank(), HIGH_CARD); +} + +TEST(PokerHandTest, compare1) { + PokerHand p1, p2; + p1.fetchCard("6C 7H 8C 9C TC"); + p2.fetchCard("AC AH 7H AD 7C"); + EXPECT_EQ(p1.compareTo(p2), -1); + EXPECT_EQ(p2.compareTo(p1), 1); +} + +TEST(PokerHandTest, compareTwoPair) { + PokerHand p1, p2; + p1.fetchCard("6C 6H 8C 8C TC"); + p2.fetchCard("AC AH 2H 9D 9C"); + EXPECT_EQ(p1.compareTo(p2), -1); + EXPECT_EQ(p2.compareTo(p1), 1); + p1.fetchCard("3D 3S 4C 4H KS"); + p2.fetchCard("3C 3H 4D 4D 7H"); + EXPECT_EQ(p1.compareTo(p2), 1); + EXPECT_EQ(p2.compareTo(p1), -1); + p1.fetchCard("3C 3H 4D 4D 7H"); + EXPECT_EQ(p1.compareTo(p2), 0); +} + +TEST(PokerHand, comparePair) { + PokerHand p1, p2; + p1.fetchCard("3C 6H 8C 8C TC"); + p2.fetchCard("2C AH 8H AD 9C"); + EXPECT_EQ(p2.compareTo(p1), 1); + p1.fetchCard("3C 6H 8C 8C TC"); + p2.fetchCard("2C AH 8H 8D 9C"); + EXPECT_EQ(p1.compareTo(p2), -1); + EXPECT_EQ(p2.compareTo(p1), 1); + p1.fetchCard("3C 6H 8S 8C TC"); + p2.fetchCard("2C 6H 8H 8D TC"); + EXPECT_EQ(p1.compareTo(p2), 1); + p1.fetchCard("9C 5H 8C JC JS"); + p2.fetchCard("9D 5H 8H JD JC"); + EXPECT_EQ(p1.compareTo(p2), 0); +} + +TEST(PokerHand, compareHighCard) { + PokerHand p1, p2; + p1.fetchCard("3C 6H 8C KC TC"); + p2.fetchCard("2C AH 8H 4D 9C"); + EXPECT_EQ(p2.compareTo(p1), 1); + EXPECT_EQ(p1.compareTo(p2), -1); +} + +TEST(PokerHand, compareTie) { + PokerHand p1, p2; + p1.fetchCard("7C 8H 9C TC JC"); + p2.fetchCard("7H 8D 9H TD JS"); + EXPECT_EQ(p1.compareTo(p2), 0); + p1.fetchCard("7C 7H 7H TC 7D"); + p2.fetchCard("AH 7D 7H 7D 7S"); + EXPECT_EQ(p1.compareTo(p2), 0); +} + +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + int r = RUN_ALL_TESTS(); + cin.get(); + return r; +} diff --git a/10316.cpp b/10316.cpp new file mode 100644 index 0000000..91ef8e5 --- /dev/null +++ b/10316.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = 0; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define convert(a) ((a)*pi / 180.0) + +const double pi = 2 * acos(0.0); +const double eps = 1e-10; +const double inf = 1e10; + +double dist(double x, double y, double z) +{ + return sqrt(x * x + y * y + z * z); +} +class Point +{ +public: + double x, y, z; + void init(double phi, double theta) + { + z = sin(phi); + x = cos(phi) * cos(theta); + y = cos(phi) * sin(theta); + } + double compute_dist(Point &a) + { + double theta = (1 + 1 - dist(x - a.x, y - a.y, z - a.z)) / 2; + return acos(theta); + } +}; +class state +{ +public: + int now; + double cost; + bool operator<(const state &a) const + { + return cost > a.cost; + } +}; +const int N = 1000; +double edge[N][N]; +double cost[N]; +double dijkstra(int n, int s) +{ + double maxi = 0; + rep(i, n) + { + maxi = max(maxi, edge[s][i]); + } + return maxi; +} +void solve(int n, Point *in, double *phi, double *theta) +{ + int ansindex = -1; + double ans = 1e10; + rep(i, n) + { + double tmp = dijkstra(n, i); + if (tmp + eps < ans) + { + ans = tmp; + ansindex = i; + } + } + printf("%.2lf %.2lf\n", phi[ansindex], theta[ansindex]); +} + +int main() +{ + Point in[N]; + double phi[N]; + double theta[N]; + int n; + while (cin >> n) + { + rep(i, n) + { + cin >> phi[i] >> theta[i]; + in[i].init(convert(phi[i]), convert(theta[i])); + } + reverse(phi, phi + n); + reverse(theta, theta + n); + reverse(in, in + n); + rep(i, n) + { + edge[i][i] = 0; + REP(j, i + 1, n) + { + edge[i][j] = edge[j][i] = in[i].compute_dist(in[j]); + } + } + solve(n, in, phi, theta); + } + return 0; +} diff --git a/10318.cpp b/10318.cpp new file mode 100644 index 0000000..bdefd89 --- /dev/null +++ b/10318.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +int button[6][6]; // [1~5][1~5] : 1 -> light +int patt[4][4]; // [1~3][1~3] +int Prs[26]; +int tmp[26]; //1 ~25 : 1 -> press +int r, c, flag, Case, min_; + +void initial(); +void Panel(int); +void Press(int, int); +int Unlock(); + +int main() +{ + char patn[4]; + Case = 0; + while (1) + { + scanf("%d %d\n", &r, &c); + if (r == 0 && c == 0) + { + break; + } + initial(); + for (int i = 1; i <= 3; i++) + { + scanf("%s", patn); + for (int j = 1; j <= 3; j++) + { + if (patn[j - 1] == '*') + { + patt[i][j] = 1; + } + } + } + Panel(1); + // output + printf("Case #%d\n", ++Case); + if (min_ < r * c + 1) + { + for (int i = 1; i <= r * c; i++) + { + if (Prs[i] == 1 && min_ > 0) + { + printf("%d", i); + min_--; + if (min_ > 0) + { + printf(" "); + } + } + } + printf("\n"); + } + else + { + printf("Impossible.\n"); + } + }//while(1) + return 0; +} + +void Panel(int depth) +{ + int i, j, total; + for (int k = 0; k <= 1; k++) + { + flag = 0; + Press(depth, k); + i = (depth - 1) / c + 1; + j = (depth - 1) % c + 1; + if (depth >= 2 * c + 1) + { + for (int m = 1; m <= i - 2; m++) + for (int n = 1; n <= c; n++) + if (button[m][n] == 0) + { + flag = 1; + } + } + if (depth == r * c && flag == 0) + { + if (Unlock() == 1) + { + total = 0; + for (int m = 1; m <= r * c; m++) + { + total += tmp[m]; + } + if (total < min_) + { + min_ = total; + for (int m = 1; m <= r * c; m++) + { + Prs[m] = tmp[m]; + } + flag = 1; + } + } + } + else if (flag == 0) + { + Panel(depth + 1); + } + Press(depth, k); + }//for(k) 按或ä¸æŒ‰ +} + +void Press(int depth, int k) +{ + int i, j; + if (k == 1) + { + tmp[depth] = 1 - tmp[depth]; + i = (depth - 1) / c + 1; + j = (depth - 1) % c + 1; + for (int m = i - 1; m <= i + 1; m++) + { + for (int n = j - 1; n <= j + 1; n++) + { + if ((1 <= m && m <= r) && (1 <= n && n <= c)) + { + button[m][n] = button[m][n] + patt[2 + m - i][2 + n - j]; + if (button[m][n] == 2) + { + button[m][n] = 0; + } + } + } + } + }//if +} + +int Unlock() +{ + for (int i = 1; i <= r; i++) + for (int j = 1; j <= c; j++) + if (button[i][j] == 0) + { + return 0; + } + return 1; +} + +void initial() +{ + flag = 0; + min_ = r * c + 1; + for (int i = 1; i <= r; i++) + { + for (int j = 1; j <= c; j++) + { + button[i][j] = 0; + } + } + for (int i = 1; i <= 3; i++) + { + for (int j = 1; j <= 3; j++) + { + patt[i][j] = 0; + } + } + for (int i = 1; i <= 25; i++) + { + Prs[i] = 0; + tmp[i] = 0; + } +} diff --git a/10319.cpp b/10319.cpp new file mode 100644 index 0000000..daefb24 --- /dev/null +++ b/10319.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define MED 50 +#define N 100 + +vector edges[2 * N + 30]; +int CC[210], id[210], tipo[210]; +stack pila; +int ID, SCC = 0; + +int DFS3(int node) +{ + id[node] = ID++; + int minid = id[node]; + tipo[node] = 1; + pila.push(node); + for (int i = 0; i < edges[node].size(); i++) + { + int w = edges[node][i]; + if (tipo[w] == 0) + { + minid = min(minid, DFS3(w)); + } + else if (tipo[w] == 1) + { + minid = min(minid, id[w]); + } + } + if (minid == id[node]) + { + SCC++; + while (1) + { + int n = pila.top(); + CC[n] = SCC; + pila.pop(); + if (n == node) + { + break; + } + } + } + tipo[node] = 2; + return minid; +} + +int main() +{ + int i, j, k, cases; + scanf("%i", &cases); + for (int h = 0; h < cases; h++) + { + for (i = 0; i < 2 * N + 30; i++) + { + edges[i].clear(); + } + SCC = 0; + int row, col, cant; + scanf("%i %i %i", &row, &col, &cant); + int f1, c1, f2, c2; + for (i = 0; i < cant; i++) + { + scanf("%i %i %i %i", &f1, &c1, &f2, &c2); + c1 = MED + c1; + c2 = MED + c2; + if (c1 == c2) + { + if (f1 < f2) + { + edges[N + c1].push_back(c1); + } + else if (f2 < f1) + { + edges[c1].push_back(N + c1); + } + continue; + } + if (f1 == f2) + { + if (c1 < c2) + { + edges[N + f1].push_back(f1); + } + else if (c2 < c1) + { + edges[f1].push_back(N + f1); + } + } + else if (f1 < f2) + { + if (c1 < c2) + { + edges[N + f1].push_back(f2); + edges[N + f1].push_back(c1); + edges[N + c2].push_back(f2); + edges[N + c2].push_back(c1); + edges[N + f2].push_back(f1); + edges[N + f2].push_back(c2); + edges[N + c1].push_back(f1); + edges[N + c1].push_back(c2); + } + else + { + edges[f1].push_back(N + f2); + edges[f1].push_back(c1); + edges[N + c2].push_back(N + f2); + edges[N + c2].push_back(c1); + edges[f2].push_back(N + f1); + edges[f2].push_back(c2); + edges[N + c1].push_back(N + f1); + edges[N + c1].push_back(c2); + } + } + else + { + if (c1 < c2) + { + edges[N + f1].push_back(f2); + edges[N + f1].push_back(N + c1); + edges[c2].push_back(f2); + edges[c2].push_back(N + c1); + edges[N + f2].push_back(f1); + edges[N + f2].push_back(N + c2); + edges[c1].push_back(f1); + edges[c1].push_back(N + c2); + } + else + { + edges[f1].push_back(N + f2); + edges[f1].push_back(N + c1); + edges[c2].push_back(N + f2); + edges[c2].push_back(N + c1); + edges[f2].push_back(N + f1); + edges[f2].push_back(N + c2); + edges[c1].push_back(N + f1); + edges[c1].push_back(N + c2); + } + } + } + int ID = 1; + int vale = 1; + memset(CC, -1, sizeof(CC)); + memset(id, -1, sizeof(id)); + memset(tipo, 0, sizeof(tipo)); + for (i = 1; i <= row; i++) + { + if (CC[i] < 0) + { + DFS3(i); + } + if (CC[i + N] < 0) + { + DFS3(i + N); + } + if (CC[i] == CC[i + N]) + { + vale = 0; + } + } + for (i = MED + 1; i <= MED + col; i++) + { + if (CC[i] < 0) + { + DFS3(i); + } + if (CC[i + N] < 0) + { + DFS3(i + N); + } + if (CC[i] == CC[i + N]) + { + vale = 0; + } + } + if (vale) + { + printf("Yes\n"); + } + else + { + printf("No\n"); + } + } + return 0; +} diff --git a/1032.cpp b/1032.cpp new file mode 100644 index 0000000..37ec1e7 --- /dev/null +++ b/1032.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +const int MAXN = 128; + +map R; + +int day_prev(int x) +{ + // mday[] will be modified + int mday[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int yy = x / 10000, mm = x % 10000 / 100, dd = x % 100; + if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) + { + mday[2] = 29; + } + if (mm == 1 && dd == 1) + { + yy--; + mm = 12; + dd = 31; + } + else + { + dd--; + if (dd < 1) + { + mm--; + dd = mday[mm]; + } + } + return yy * 10000 + mm * 100 + dd; +} + +int day_nxt(int x) +{ + // mday[] will be modified + int mday[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + int yy = x / 10000, mm = x % 10000 / 100, dd = x % 100; + if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0) + { + mday[2] = 29; + } + dd++; + if (dd > mday[mm]) + { + dd = 1; + mm++; + } + if (mm == 13) + { + mm = 1; + yy++; + } + return yy * 10000 + mm * 100 + dd; +} + +void record(int x) +{ + R[x] = 0, R[day_nxt(x)] = 0, R[day_prev(x)] = 0; +} + +void print(int x) +{ + int yy = x / 10000, mm = x % 10000 / 100, dd = x % 100; + printf("%d/%d/%d", mm, dd, yy); +} + +int main() +{ + int cases = 0, N, M, aSt[MAXN], aEd[MAXN], bSt[MAXN], bEd[MAXN]; + while (scanf("%d%d", &N, &M) == 2, N, M) + { + R.clear(); + for (int i = 0; i < N; i++) + { + scanf("%d%d", &aSt[i], &aEd[i]); + record(aSt[i]); + record(aEd[i]); + } + for (int i = 0; i < M; i++) + { + scanf("%d%d", &bSt[i], &bEd[i]); + record(bSt[i]); + record(bEd[i]); + } + + int size = 0; + vector date; + for (map::iterator it = R.begin(); + it != R.end(); it++) + { + it->second = size++; + date.push_back(it->first); + } + + vector mark(size, 0); + for (int i = 0; i < M; i++) + { + int l = R[bSt[i]], r = R[bEd[i]]; + for (int j = l; j <= r; j++) + { + mark[j] = 1; + } + } + for (int i = 0; i < N; i++) + { + int l = R[aSt[i]], r = R[aEd[i]]; + for (int j = l; j <= r; j++) + { + mark[j] = 0; + } + } + + if (cases) + { + printf("\n"); + } + printf("Case %d:\n", ++cases); + int has = 0; + for (int i = 0; i < mark.size(); i++) + { + if (mark[i] == 1) + { + has = 1; + printf(" "); + print(date[i]); + if (i + 1 < mark.size() && mark[i + 1] == 1) + { + printf(" to "); + while (i + 1 < mark.size() && mark[i + 1] == 1) + { + i++; + } + print(date[i]); + } + printf("\n"); + } + } + + if (!has) + { + printf(" No additional quotes are required.\n"); + } + } + return 0; +} diff --git a/10320.cpp b/10320.cpp new file mode 100644 index 0000000..55cf21e --- /dev/null +++ b/10320.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +double gettheta(double a, double b, double c) +{ + double t = (a * a + b * b - c * c) / 2 / a / b; + return acos(t); +} + +double l, w, R, r1, r2; + +int main() +{ + const double pi = 3.1415926535897932384626433832795; + while (scanf("%lf %lf %lf", &l, &w, &R) == 3) + { + double area = 0.75 * pi * R * R; + if (l < w) + { + double t = l; + l = w; + w = t; + } + r1 = R - l; + r2 = R - w; + if (r1 < 0) + { + r1 = 0; + } + if (r2 < 0) + { + r2 = 0; + } + if (r1 <= w && r2 <= l) + { + area += pi * r1 * r1 / 4 + pi * r2 * r2 / 4; + } + else + { + double diag = sqrt(l * l + w * w); + double theta = pi - gettheta(diag, l, w) - gettheta(diag, r1, r2); + area += 0.5 * r1 * r1 * theta; + theta = pi - gettheta(diag, w, l) - gettheta(diag, r2, r1); + area += 0.5 * r2 * r2 * theta; + theta = gettheta(r1, r2, diag); + area += 0.5 * r1 * r2 * sin(theta); + area -= 0.5 * l * w; + } + printf("%.10lf\n", area); + } + return 0; +} diff --git a/10321.cpp b/10321.cpp new file mode 100644 index 0000000..46b6501 --- /dev/null +++ b/10321.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define eps 1e-8 + +struct Pt +{ + double x, y; + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + { + return x < a.x; + } + return y < a.y; + } +}; + +struct Seg +{ + Pt s, e; +}; + +int inInterval(Seg a, Pt p) +{ + return min(a.s.x, a.e.x) <= p.x && + p.x <= max(a.s.x, a.e.x) && + min(a.s.y, a.e.y) <= p.y && + p.y <= max(a.s.y, a.e.y); +} + +int calcIntersection(Seg a, Seg b, Pt &p) +{ + double a1, b1, c1, a2, b2, c2; + double D, Dx, Dy; + a1 = a.s.y - a.e.y, b1 = -a.s.x + a.e.x; + a2 = b.s.y - b.e.y, b2 = -b.s.x + b.e.x; + c1 = a1 * a.s.x + b1 * a.s.y; + c2 = a2 * b.s.x + b2 * b.s.y; + D = a1 * b2 - a2 * b1; + Dx = c1 * b2 - c2 * b1; + Dy = a1 * c2 - a2 * c1; + if (fabs(D) < eps)// NONE or LINE + { + return 0; + } + p.x = Dx / D, p.y = Dy / D; + return inInterval(a, p) == 1 && inInterval(b, p) == 1; +} + +int inPolygon(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + { + cnt++; + } + } + return cnt & 1; +} + +int main() +{ + int n, m; + int i, j, p, q; + Pt a[105], b[105]; + while (scanf("%d", &n) == 1 && n) + { + for (i = 0; i < n; i++) + { + scanf("%lf %lf", &a[i].x, &a[i].y); + } + scanf("%d", &m); + for (i = 0; i < m; i++) + { + scanf("%lf %lf", &b[i].x, &b[i].y); + } + Seg s1, s2; + Pt ret[10005]; + int retN = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + s1.s = a[i], s1.e = a[j]; + for (p = 0, q = m - 1; p < m; q = p++) + { + s2.s = b[p], s2.e = b[q]; + if (calcIntersection(s1, s2, ret[retN])) + { + retN++; + } + } + } + for (i = 0; i < n; i++) + if (inPolygon(b, m, a[i])) + { + ret[retN++] = a[i]; + } + for (i = 0; i < m; i++) + if (inPolygon(a, n, b[i])) + { + ret[retN++] = b[i]; + } + if (retN == 0) + { + puts("0"); + continue; + } + sort(ret, ret + retN); + for (i = 1, j = 0; i < retN; i++) + { + if (fabs(ret[i].x - ret[j].x) > eps || + fabs(ret[i].y - ret[j].y) > eps) + { + ret[++j] = ret[i]; + } + } + retN = j + 1; + printf("%d\n", retN); + for (i = 0; i < retN; i++) + { + printf("%.2lf %.2lf\n", ret[i].x, ret[i].y); + } + } + return 0; +} diff --git a/10323.cpp b/10323.cpp index f9967ad..d1a60d1 100644 --- a/10323.cpp +++ b/10323.cpp @@ -1,48 +1,44 @@ -#include -using namespace std; - -int main(){ - long long n; - while(cin>>n){ - if(n<0){ - if(n%2==0) - cout<<"Underflow!"<=0){ - cout<<"Underflow!"<13){ - cout<<"Overflow!"< + +using namespace std; + +int main() +{ + long long fac[20]; + int i, n; + fac[0] = 1; + for (i = 1; i <= 13; i++) + { + fac[i] = fac[i - 1] * i; + } + while (cin >> n) + { + if (n >= 0) + { + if (n > 13) + { + cout << "Overflow!" << endl; + } + else if (n < 8) + { + cout << "Underflow!" << endl; + } + else + { + cout << fac[n] << endl; + } + } + else + { + if ((n * -1) % 2 == 1) + { + cout << "Overflow!" << endl; + } + else + { + cout << "Underflow!" << endl; + } + } + } + return 0; +} diff --git a/10324.cpp b/10324.cpp index 044c76b..7a020b3 100644 --- a/10324.cpp +++ b/10324.cpp @@ -1,36 +1,40 @@ -#include -#include -#include -#include -#include -using namespace std; -int main(){ - for(int z=1;;z++){ - string str; - int i; - getline(cin,str,'\n'); - if(str=="EOF" || str=="\0") - break; - int len=str.length(); - int counter[len]; - counter[0]=0; - for(i=1;i + +using namespace std; + +int main() +{ + char buf[1000001]; + int table[1000001]; + int i, j, n, t, kase = 1; + while (cin >> buf) + { + cout << "Case " << kase++ << ":" << endl; + memset(table, 0, sizeof(table)); + for (i = 1; buf[i]; i++) + { + if (buf[i] == buf[i - 1]) + { + table[i] = table[i - 1]; + } + else + { + table[i] = table[i - 1] + 1; + } + } + cin >> n; + for (t = 0; t < n; t++) + { + cin >> i >> j; + if (table[i] == table[j]) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + } + return 0; +} diff --git a/10325.cpp b/10325.cpp new file mode 100644 index 0000000..58c4c7f --- /dev/null +++ b/10325.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +typedef long long ss; +vector lt; +int N, M; +ss A[20], Sum; + +ss gcd(ss a, ss b) +{ + return b ? gcd(b, a % b) : a; +} +ss Count(ss limit) +{ + ss i, lcm = A[1], gd, pro; + for (i = 2; i <= limit; i++) + { + gd = gcd(lcm, A[i]); + pro = lcm * A[i]; + lcm = pro / gd; + } + return N / lcm; +} +ss Recur(ss n, ss level, ss limit) +{ + ss i; + if (level >= 0) + { + A[level] = lt[n]; + } + if (level == limit) + { + if (limit % 2) + { + Sum += Count(limit); + } + else + { + Sum -= Count(limit); + } + return 0; + } + for (i = n + 1; i < M; i++) + { + Recur(i, level + 1, limit); + } + return 0; +} +void Cal() +{ + int i, j; + Sum = 0; + for (i = 0; i < M; i++) + { + Sum += N / lt[i]; + } + for (i = 2; i <= M; i++) + { + Recur(-1, 0, i); + } + j = N - Sum; + cout << j << endl; +} + +int main() +{ + int i, n; + while (cin >> N >> M) + { + for (i = 0; i < M; i++) + { + cin >> n; + lt.push_back(n); + } + Cal(); + lt.clear(); + } + return 0; +} diff --git a/10326.cpp b/10326.cpp new file mode 100644 index 0000000..0d954e5 --- /dev/null +++ b/10326.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define MAXN 53 + +typedef long long ss; + +ss C[MAXN], F[MAXN], CN; + +void Print2(ss x, ss y) +{ + ss d = x + y, n = x * y; + printf("x^2"); + if (d > 0) + printf(" + %lldx", d); + else if (d < 0) + { + d *= -1; + printf(" - %lldx", d); + } + if (n >= 0) + { + printf(" + %lld", n); + } + else + { + n *= -1; + printf(" - %lld", n); + } + printf(" = 0\n"); +} + +void Print1(ss n) +{ + printf("x"); + if (n < 0) + { + n *= -1; + printf(" - %lld = 0\n", n); + } + else + printf(" + %lld = 0\n", n); +} + +void Print() +{ + ss i; + printf("x^%lld", CN); + for (i = CN - 1; i >= 1; i--) + { + if (F[i] < 0) + { + F[i] *= -1; + if (F[i] > 1) + printf(" - %lldx", F[i]); + else + printf(" - x"); + if (i > 1) + printf("^%lld", i); + } + else if (F[i] > 0) + { + if (F[i] > 1) + printf(" + %lldx", F[i]); + else + printf(" + x"); + if (i > 1) + printf("^%lld", i); + } + } + if (F[0] < 0) + { + F[0] *= -1; + printf(" - %lld = 0", F[0]); + } + else + printf(" + %lld = 0", F[0]); +} + +void Set(ss n) +{ + ss i; + F[n] = 1; + for (i = n - 1; i > 0; i--) + F[i] = F[i - 1] + F[i] * C[n]; + F[0] *= C[n]; +} + +void Cal() +{ + ss i; + F[0] = C[1]; + F[1] = 1; + for (i = 2; i <= CN; i++) + { + Set(i); + } + Print(); + printf("\n"); +} + +int main() +{ + ss i; + while (scanf("%lld", &CN) == 1) + { + for (i = 1; i <= CN; i++) + { + scanf("%lld", &C[i]); + C[i] *= -1; + } + if (CN == 2) + Print2(C[1], C[2]); + else if (CN == 1) + Print1(C[1]); + else + Cal(); + } + return 0; +} diff --git a/10327.cpp b/10327.cpp new file mode 100644 index 0000000..d557db6 --- /dev/null +++ b/10327.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int a[1010]; + +int main() +{ + int i, j, k, n, s; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + s = 0; + for (i = 0; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { + if (a[i] > a[j]) + { + s++; + } + } + } + printf("Minimum exchange operations : %d\n", s); + s = 0; + } + return 0; +} diff --git a/10328.cpp b/10328.cpp new file mode 100644 index 0000000..cfd0709 --- /dev/null +++ b/10328.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define M 100000000000000000ULL + +typedef unsigned long long u64; + +struct num +{ + u64 a, b; + num(u64 a = 0, u64 b = 0) : a(a), b(b){}; +}; + +num operator+(num x, num y) +{ + u64 t = (x.a + y.a); + return num(t % M, x.b + y.b + t / M); +} + +void print(num x) +{ + if (x.b > 0) + { + printf("%llu%.17llu\n", x.b, x.a); + } + else + { + printf("%llu\n", x.a); + } +} + +num memo[128][128], pw[128]; +int got[128][128]; + +num f(int n, int k) +{ + if (got[n][k]++) + { + return memo[n][k]; + } + if (n <= k) + { + return memo[n][k] = (n == k) ? 1 : 0; + } + memo[n][k] = pw[n - k]; + for (int i = 0; i < k; i++) + { + memo[n][k] = memo[n][k] + f(n - i - 1, k); + } + return memo[n][k]; +} + +int main() +{ + int n, k; + pw[0] = 1; + for (n = 1; n < 128; n++) + { + pw[n] = pw[n - 1] + pw[n - 1]; + } + memset(got, 0, sizeof(got)); + while (scanf("%d %d", &n, &k) == 2) + { + print(f(n, k)); + } + return 0; +} diff --git a/10329.cpp b/10329.cpp new file mode 100644 index 0000000..ebf5055 --- /dev/null +++ b/10329.cpp @@ -0,0 +1,336 @@ +#include + +using namespace std; + +#define MAXN 5009 +#define BASE 10000000 +#define MAX_DIGIT 192 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +typedef long long INT; + +char ss[MAXN]; +int P[700], Res[700], tp, max_; + +struct BigNum +{ + INT sign, len, digit[MAX_DIGIT]; + void absAdd(BigNum, BigNum); + void absSubtract(BigNum, BigNum); + void multAndAdd(BigNum, INT, INT); + + void operator=(INT); //ok + void operator=(BigNum); //ok + void operator=(char *); //ok + void add(BigNum, INT); //ok- + void add(BigNum, BigNum); //ok- + void subtract(BigNum, INT); //ok- + void subtract(BigNum, BigNum); //ok- + void mult(BigNum, INT); //ok- + void mult(BigNum, BigNum); //ok- + void div(BigNum, INT, INT &); //ok-, third param is the remainder + void div(BigNum, BigNum, BigNum &);//ok- + INT comp(BigNum); //ok- + INT absComp(BigNum); //ok- + void print(); //ok- + void update(); //ok- + void clear(); //ok- + BigNum() + { + clear(); + } +}; +void BigNum::mult(BigNum a, BigNum b) +{ + INT pos; + clear(); + for (pos = 0; pos < b.len; ++pos) + { + multAndAdd(a, b.digit[pos], pos); + } + sign = (a.sign + b.sign) % 2; +} +void BigNum::multAndAdd(BigNum a, INT s, INT offset) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos + offset] += a.digit[pos] * s + carry; + carry = digit[pos + offset] / BASE; + digit[pos + offset] %= BASE; + } + pos = a.len + offset; + while (carry != 0) + { + //check overflow + digit[pos] += carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + ++pos; + } + if (len < pos) + { + len = pos; + } + update(); +} +void BigNum::operator=(INT x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + INT i; + clear(); + len = n.len; + sign = n.sign; + //memset(digit, 0, sizeof(digit)); + for (i = 0; i < len; ++i) + { + digit[i] = n.digit[i]; + } +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::mult(BigNum a, INT s) +{ + INT pos, carry; + if (s < 0) + { + sign = 1 - a.sign; + s = -s; + } + else + { + sign = a.sign; + } + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + //take care about any overflow + digit[pos] = a.digit[pos] * s + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + pos = a.len; + while (carry != 0) + { + //check overflow + digit[pos] = carry % BASE; + carry /= BASE; + ++pos; + } + len = pos; + update(); +} +void BigNum::print() +{ + INT i; + if (sign) + { + printf("-"); + } + printf("%lld", digit[len - 1]); + for (i = len - 2; i >= 0; --i) + { + //BE CAREFUL HERE + switch (BASE) + { + case 10: + printf("%lld", digit[i]); + break; + case 100: + printf("%02lld", digit[i]); + break; + case 1000: + printf("%03lld", digit[i]); + break; + case 10000: + printf("%04lld", digit[i]); + break; + case 100000: + printf("%05lld", digit[i]); + break; + case 1000000: + printf("%06lld", digit[i]); + break; + case 10000000: + printf("%07lld", digit[i]); + break; + case 100000000: + printf("%08lld", digit[i]); + break; + case 1000000000: + printf("%09lld", digit[i]); + break; + default: + break; + } + } +} +void Prime_Table() +{ + int i, j; + tp = 1; + for (i = 2; i * i < MAXN;) + { + for (j = i + i; j < MAXN; j += i) + { + ss[j] = 1; + } + for (i++; ss[i]; i++) + ; + } + P[0] = 2; + for (i = 3; i < MAXN; i += 2) + if (!ss[i]) + { + P[tp++] = i; + } +} +void Count(int n, int k) +{ + int i, j, p; + if (n <= 1) + return; + for (i = 0; i < tp && P[i] <= n; i++) + { + p = 0; + for (j = P[i]; j <= n; j *= P[i]) + p += n / j; + Res[i] += k * p; + } +} +void ReadNumerator(int n) +{ + int p, q; + while (n--) + { + scanf("%d%d", &p, &q); + Count(p, 1); + Count(q, -1); + Count(p - q, -1); + if (p > max_) + max_ = p; + } +} +void ReadDenominator(int m) +{ + int p, q; + while (m--) + { + scanf("%d%d", &p, &q); + Count(p, -1); + Count(q, 1); + Count(p - q, 1); + if (p > max_) + max_ = p; + } +} +void Free() +{ + int i; + for (i = 0; P[i] <= max_; i++) + { + Res[i] = 0; + } +} +int Digits() +{ + int dig = 0, i, j; + double x = 0.0; + for (i = 0; P[i] <= max_; i++) + x += Res[i] * log10(double(P[i])); + j = int(x); + return j + 1; +} +void Product() +{ + int i, j; + BigNum dum; + dum = 1; + for (i = 0; i < tp && P[i] <= max_; i++) + { + if (!Res[i]) + { + continue; + } + for (j = 1; j <= Res[i]; j++) + { + dum.mult(dum, P[i]); + } + } + dum.print(); + printf("\n"); +} +int isDivisible() +{ + int i; + for (i = 0; P[i] <= max_ && i < tp; i++) + if (Res[i] < 0) + return 0; + return 1; +} +void Cal() +{ + int dg; + if (!isDivisible()) + { + printf("0\n"); + return; + } + dg = Digits(); + if (dg > 100) + { + printf("-1\n"); + return; + } + Product(); +} + +int main() +{ + int n, m; + Prime_Table(); + while (scanf("%d%d", &n, &m) == 2) + { + max_ = 0; + ReadNumerator(n); + ReadDenominator(m); + Cal(); + Free(); + } + return 0; +} diff --git a/10330.cpp b/10330.cpp new file mode 100644 index 0000000..e4746ba --- /dev/null +++ b/10330.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define WHITE 0 +#define GRAY 1 +#define BLACK 2 +#define MAX_NODES 202 +#define oo 1 << 30 + +int cap[MAX_NODES][MAX_NODES]; +int flow[MAX_NODES][MAX_NODES]; +int color[MAX_NODES]; +int pred[MAX_NODES]; +int b, d, head, i, increment, j, m, n, tail, x, y, z, lim_sup; +int q[MAX_NODES]; + +void enqueue(int x) +{ + q[tail] = x; + tail++; + color[x] = GRAY; +} +int dequeue() +{ + int x = q[head]; + head++; + color[x] = BLACK; + return x; +} +int bfs(int start, int target) +{ + for (i = 0; i < lim_sup; i++) + { + color[i] = WHITE; + } + head = tail = 0; + enqueue(start); + pred[start] = -1; + while (head != tail) + { + i = dequeue(); + for (j = 0; j < lim_sup; j++) + { + if (color[j] == WHITE && cap[i][j] - flow[i][j] > 0) + { + enqueue(j); + pred[j] = i; + } + } + } + return color[target] == BLACK; +} +int min(int x, int y) +{ + return x < y ? x : y; +} +int max_flow(int source, int sink) +{ + int max_flow = 0; + for (i = 0; i < lim_sup; i++) + for (j = 0; j < lim_sup; j++) + { + flow[i][j] = 0; + } + while (bfs(source, sink)) + + { + increment = oo; + for (i = lim_sup - 1; pred[i] != -1; i = pred[i]) + { + increment = min(increment, cap[pred[i]][i] - flow[pred[i]][i]); + } + for (i = lim_sup - 1; pred[i] != -1; i = pred[i]) + { + flow[pred[i]][i] += increment; + flow[i][pred[i]] -= increment; + } + max_flow += increment; + } + return max_flow; +} + +int main() +{ + while (scanf("%d", &n) != EOF) + { + lim_sup = (n + 1) << 1; + for (i = 0; i < lim_sup; i++) + { + for (j = 0; j < lim_sup; j++) + { + cap[i][j] = 0; + } + } + for (i = 1; i <= n; scanf("%d", &cap[i][n + i]), i++) + ; + scanf("%d", &m); + for (i = 0; i < m; cap[n + x][y] = z, i++) + { + scanf("%d %d %d", &x, &y, &z); + } + scanf("%d %d", &b, &d); + for (i = 0; i < b; cap[0][x] = oo, i++) + { + scanf("%d", &x); + } + for (i = 0; i < d; cap[n + x][2 * n + 1] = oo, i++) + { + scanf("%d", &x); + } + printf("%d\n", max_flow(0, 2 * n + 1)); + } + return 0; +} diff --git a/10331.cpp b/10331.cpp new file mode 100644 index 0000000..ce6c14b --- /dev/null +++ b/10331.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define maxn 102 +#define INF 99999999 +#define min(a, b) (a > b ? b : a) + +int t[maxn][maxn], N; +struct ss +{ + int u, v, c; + int used; +}; +ss V[100000]; +int tv; + +void ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + t[i][j] = t[j][i] = INF; + } + t[i][i] = 0; + } +} + +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + t[i][j] = min(t[i][j], t[i][k] + t[k][j]); + } + } + } +} + +void Count() +{ + int i, j, k, max = 0; + for (i = 1; i < N; i++) + { + for (j = i + 1; j <= N; j++) + { + for (k = 0; k < tv; k++) + { + if (V[k].c > t[V[k].u][V[k].v]) + { + continue; + } + if (t[i][V[k].u] + V[k].c + t[V[k].v][j] == t[i][j] || + t[i][V[k].v] + V[k].c + t[V[k].u][j] == t[i][j]) + { + V[k].used++; + } + if (V[k].used > max) + { + max = V[k].used; + } + } + } + } + k = 0; + for (i = 0; i < tv; i++) + { + if (V[i].used == max) + { + if (k++ > 0) + { + cout << " "; + } + cout << i + 1; + } + } + cout << endl; +} + +void Cal() +{ + Floyd(); + Count(); +} + +int main() +{ + int m, u, v, c, k = 0; + ss dum; + while (cin >> N >> m) + { + ini(); + tv = 0; + while (m--) + { + cin >> u >> v >> c; + if (t[u][v] > c) + { + t[u][v] = t[v][u] = c; + } + dum.u = u; + dum.v = v; + dum.c = c; + dum.used = 0; + V[tv++] = dum; + } + Cal(); + } + return 0; +} diff --git a/10333.cpp b/10333.cpp new file mode 100644 index 0000000..def0438 --- /dev/null +++ b/10333.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back +#define ALL(C) (C).begin(), (C).end() + +const int H = 501, NUM = 501; + +int dp[H][NUM]; +char m[500][1000]; + +void make_table() +{ + rep(i, H) rep(j, NUM) dp[i][j] = -1; + REP(i, 1, H) + dp[i][1] = i; + REP(k, 2, H) + { + rep(i, H) + { + REP(j, 1, NUM) + { + if (i - k < 0) + { + continue; + } + if (dp[i - k][j - 1] > -1 && k > dp[i - k][j - 1]) + { + dp[i][j] = max(dp[i][j], k); + } + } + } + } +} + +void make_seq(vector &a, int h, int num) +{ + while (num > 0) + { + a.pb(dp[h][num]); + h -= dp[h][num]; + num--; + } +} + +void make_tower(vector &l, int h, int base) +{ + int total = 0; + rep(j, l.size()) + { + rep(i, l[j]) + { + m[total + i][base + j * 2] = '#'; + } + total += l[j]; + m[total - 1][base + j * 2 + 1] = '*'; + m[total - 1][base + j * 2 + 2] = '*'; + REP(i, total, h) + { + m[i][base + j * 2] = ' '; + m[i][base + j * 2 + 1] = ' '; + } + } +} + +void solve(int h) +{ + int num = 0; + REP(i, 1, NUM) + { + if (dp[h][i] > -1 && dp[h][i - 1] > -1) + { + num = i; + } + } + rep(i, 500) rep(j, 1000) m[i][j] = ' '; + vector l, r; + int width = 0; + make_seq(l, h, num); + make_seq(r, h, num - 1); + width = 2 + r.size() * 2 + l.size() * 2; + rep(i, h) + { + rep(j, width) m[i][j] = '.'; + m[i][width] = '\0'; + } + make_tower(l, h, 0); + make_tower(r, h, 2 + r.size() * 2 + 1); + rep(i, h) + { + reverse(m[i] + 3 + r.size() * 2, m[i] + width); + } + rep(i, h) + { + for (int j = width - 1; j >= 0; j--) + if (m[h - i - 1][j] == ' ') + { + m[h - i - 1][j] = '\0'; + } + else + { + break; + } + cout << m[h - 1 - i] << endl; + } +} + +main() +{ + make_table(); + int h, tc = 1; + while (cin >> h) + { + cout << "Tower #" << tc++ << endl; + solve(h); + cout << endl; + } +} diff --git a/10334.cpp b/10334.cpp index 10430fd..692259a 100644 --- a/10334.cpp +++ b/10334.cpp @@ -1,20 +1,41 @@ -#include -using namespace std; - -int main(){ - int n; - while(cin>>n){ - if(n==0){ - cout<<"1"< + +using namespace std; + +int main() +{ + int n, l[1001]; + short x[1001][220] = {0}; + l[0] = 1; + l[1] = 1; + x[0][0] = 1; + x[1][0] = 2; + for (int i = 2; i <= 1000; i++) + { + for (int j = 0; j < l[i - 1]; j++) + { + if (x[i - 1][j] + x[i - 2][j] + x[i][j] >= 10) + { + x[i][j + 1] = 1; + } + x[i][j] = (x[i][j] + x[i - 1][j] + x[i - 2][j]) % 10; + } + if (x[i][l[i - 1]]) + { + l[i] = l[i - 1] + 1; + } + else + { + l[i] = l[i - 1]; + } + } + while (cin >> n) + { + for (int i = l[n] - 1; i >= 0; i--) + { + cout << x[n][i]; + } + cout << endl; + } + return 0; +} diff --git a/10335.cpp b/10335.cpp new file mode 100644 index 0000000..4dcb7a6 --- /dev/null +++ b/10335.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +#define eps 1e-8 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +struct Seg +{ + Pt s, e; + double angle; + int label; + Seg(Pt a = Pt(), Pt b = Pt(), int l = 0) : s(a), e(b), label(l) + { + // angle = atan2(e.y - s.y, e.x - s.x); + } + bool operator<(const Seg &other) const + { + if (fabs(angle - other.angle) > eps) + return angle > other.angle; + if (cross(other.s, other.e, s) > -eps) + return true; + return false; + } + bool operator!=(const Seg &other) const + { + return !((s == other.s && e == other.e) || (e == other.s && s == other.e)); + } +}; +Pt getIntersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +double getAngle(Pt va, Pt vb) +{// segment, not vector + return acos(dot(va, vb) / va.length() / vb.length()); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} +const double pi = acos(-1); +int cmpZero(double v) +{ + if (fabs(v) > eps) + return v > 0 ? 1 : -1; + return 0; +} +int same(Pt a, Pt b) +{ + return fabs(a.x - b.x) < 0.005 && fabs(a.y - b.y) < 0.005; +} + +int main() +{ + Pt p[32]; + double theta; + int n, m; + while (scanf("%d %d", &n, &m) == 2 && n) + { + Pt S, lvec; + S.read(); + scanf("%lf", &theta); + theta = theta * pi / 180; + lvec = Pt(cos(theta), sin(theta)); + for (int i = 0; i < n; i++)// anti-clockwise + p[i].read(); + + int lost = 0; + Seg on; + for (int i = 0, j = n - 1; i < n; j = i++) + { + Seg b(p[j], p[i]); + if (onSeg(b.s, b.e, S)) + on = b; + } + for (int it = 0; it <= m; it++) + {// #reflect. + Seg pick = on, a(S, S + lvec); + for (int i = 0, j = n - 1; i < n; j = i++) + { + Seg b(p[j], p[i]); + if (cmpZero(cross(a.s, a.e, b.s)) * cmpZero(cross(a.s, a.e, b.e)) <= 0) + { + if (b != pick) + { + pick = b; + break; + } + } + } + + Pt poj = getIntersect(pick, Seg(S, S + Pt(pick.s.y - pick.e.y, pick.e.x - pick.s.x))); + Pt sym = S + (poj - S) * 2; + S = getIntersect(a, pick); + lvec = S - sym; + if (same(S, pick.s) || same(S, pick.e)) + { + lost = 1; + break; + } + on = pick; + } + if (lost) + puts("lost forever..."); + else + { + if (fabs(S.x) < eps && S.x < 0) + { + S.x = -S.x; + } + if (fabs(S.y) < eps && S.y < 0) + { + S.x = -S.y; + } + printf("%.2lf %.2lf\n", S.x, S.y); + } + } + return 0; +} diff --git a/10336.cpp b/10336.cpp index 204ad3c..43dd633 100644 --- a/10336.cpp +++ b/10336.cpp @@ -1,86 +1,113 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -int R, C; -string mat[500]; +using namespace std; -int dI[] = {0, -1, 0, 1}; -int dJ[] = {-1, 0, 1, 0}; +#define MAXN 20000 +/* MORE 2000*/ +char LAN[MAXN][MAXN]; +char STORE[MAXN]; +int N, R, C, K; -void floodFill(int I, int J, char ch){ - if( I < 0 || I >= R ) - return; - if( J < 0 || J >= C ) +typedef struct list +{ + int index; + int value; +} ss; +ss V[30]; +int sort(const void *a, const void *b) +{ + int m = ((ss *)a)->value; + int n = ((ss *)b)->value; + if (m != n) + { + return n - m; + } + return ((ss *)a)->index - ((ss *)b)->index; +} +void FFILL(int r, int c, char S) +{ + if (r >= R || r < 0 || c >= C || c < 0) + { return; - if( mat[I][J] != ch ) + } + if (LAN[r][c] != S) + { return; - mat[I][J] = '-'; - int k; - FOI(k, 0, 3) - floodFill(I + dI[k], J + dJ[k], ch); + } + LAN[r][c] = '1'; + FFILL(r + 1, c, S); + FFILL(r - 1, c, S); + FFILL(r, c + 1, S); + FFILL(r, c - 1, S); } - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - for (int t = 1; t <= T; t++){ - cin >> R >> C; - int i, j; - FOI(i, 0, R-1) - cin >> mat[i]; - int alp[26]; - FOI(i, 0, 25) - alp[i] = 0; - FOI(i, 0, R-1){ - FOI(j, 0, C-1){ - if( isalpha( mat[i][j] ) ){ - alp[mat[i][j] - 'a']++; - floodFill(i, j, mat[i][j]); - } +void CAL() +{ + int i, m, j; + char c; + K = 0; + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + if (isalpha(LAN[i][j])) + { + c = LAN[i][j]; + STORE[K++] = LAN[i][j]; + m = LAN[i][j] - 97; + V[m].index = m; + V[m].value++; + FFILL(i, j, c); } } - cout << "World #" << t << endl; - FOI(i, 0, 25){ - int ind = 0; - FOI(j, 0, 25){ - if( alp[ind] < alp[j] ){ - ind = j; - } - } - if( alp[ind] > 0 ){ - cout << (char)('a' + ind) << ": " << alp[ind] << endl; - alp[ind] = 0; - } + } + STORE[K] = NULL; +} +void MAKEZERO() +{ + int i; + for (i = 0; i < 30; i++) + { + V[i].index = 0; + V[i].value = 0; + } +} +void PRINT() +{ + int i, k; + qsort(V, 30, sizeof(V[0]), sort); + for (i = 0; i < 30; i++) + { + k = 0; + if (V[i].value) + { + k = 1; + printf("%c: %d\n", V[i].index + 97, V[i].value); + } + if (k == 0) + { + break; + } + } +} + +int main() +{ + int i, kase = 1; + char input[20]; + gets(input); + sscanf(input, "%d", &N); + while (N--) + { + gets(input); + sscanf(input, "%d%d", &R, &C); + for (i = 0; i < R; i++) + { + gets(LAN[i]); } + CAL(); + printf("World #%d\n", kase++); + PRINT(); + MAKEZERO(); } return 0; } diff --git a/10337.cpp b/10337.cpp new file mode 100644 index 0000000..0d4e7ca --- /dev/null +++ b/10337.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define INF 0x7ff00000 + +char grid[1010][11]; +int x, f[1010][11]; + +int doit() +{ + int i, j; + f[0][0] = 0; + for (i = 1; i < x; i++) + { + for (j = 1; j < 9; j++) + { + f[i][j] = min(30 - (int)grid[i - 1][j] + f[i - 1][j], + min(20 - (int)grid[i - 1][j + 1] + f[i - 1][j + 1], + 60 - (int)grid[i - 1][j - 1] + f[i - 1][j - 1])); + } + f[i][9] = min(30 - (int)grid[i - 1][9] + f[i - 1][9], + 60 - (int)grid[i - 1][8] + f[i - 1][8]); + } + i = x; + f[i][0] = min(20 - (int)grid[i - 1][1] + f[i - 1][1], INF); + return f[x][0]; +} + +int main() +{ + int nu; + cin >> nu; + while (nu--) + { + cin >> x; + x /= 100; + int i, j; + for (i = 9; i >= 0; i--) + { + for (j = 0; j < x; j++) + { + int k; + cin >> k; + grid[j][i] = k; + f[j][i] = INF; + } + f[j][i] = INF; + } + cout << doit() << endl << endl; + } + return 0; +} diff --git a/10338.cpp b/10338.cpp new file mode 100644 index 0000000..7d021c2 --- /dev/null +++ b/10338.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int X[128], i, j, t, tt; + long long int a, b; + char in[30]; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%s", in); + a = b = 1LL; + for (i = 'A'; i <= 'Z'; i++) + { + X[i] = 0; + } + for (i = 0; in[i] != '\0'; i++) + { + a *= (i + 1); + b *= (++X[in[i]]); + } + printf("Data set %d: %lld\n", t, a / b); + } + return 0; +} diff --git a/10339.cpp b/10339.cpp new file mode 100644 index 0000000..edeac7c --- /dev/null +++ b/10339.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +double k, m, d, kd; +int mins; + +int main() +{ + while (scanf("%lf %lf", &k, &m) == 2) + { + d = (double)(43200 / fabs(k - m)); + kd = d * (86400.0 - k) / 86400; + mins = (int)floor(1440.0 * fmod(kd + 1e-7, 0.5) + 0.5); + int hours = mins / 60; + if (hours == 0) + { + hours = 12; + } + printf("%.lf %.lf %02d:%02d\n", k, m, hours, mins % 60); + } + return 0; +} diff --git a/10340.cpp b/10340.cpp index e7e2e18..e98cc08 100644 --- a/10340.cpp +++ b/10340.cpp @@ -1,33 +1,32 @@ -#include -#include -using namespace std; - -int main(){ - - string row, col; - while(cin>>row>>col){ - int m, n; - m=row.length(); - n=col.length(); - int i, j; - int tower[m+1][n+1]; - for(i=0; i + +using namespace std; + +int main() +{ + char a[100000], s[100000]; + int i, j, k, ck; + while (scanf("%s %s", a, s) != EOF) + { + k = 0; + for (i = 0; a[i]; i++) + { + ck = 0; + for (j = k; s[j]; j++) + { + if (a[i] == s[j]) + { + k = j + 1; + ck = 1; + break; + } + } + if (!ck) + { + break; + } + } + printf(ck ? "Yes\n" : "No\n"); + } + return 0; +} diff --git a/10341.cpp b/10341.cpp index 042522c..9df4fad 100644 --- a/10341.cpp +++ b/10341.cpp @@ -1,87 +1,44 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; +#include -#define PI acos(-1.0) -#define EPSILON 1e-5 -#define EPS 1e-15 -#define INF 1<<30 +using namespace std; -#define FOI(I, A, B) for (I=A; I<=B; I++) -#define FOD(I, A, B) for (I=A; I>=B; I--) +#define EPS 1e-8 -double p, q, r, s, t, u; +int p, q, r, s, t, u; -double fun(double x){ +double f(double x) +{ return p * exp(-x) + q * sin(x) + r * cos(x) + s * tan(x) + t * x * x + u; } -double der(double x){ - return -p * exp(-x) + q * cos(x) -r * sin(x) + s * 1/( cos(x) * cos(x) ) + 2 * t * x; -} - -double Bisection(double lo, double hi){ - double bot = fun(lo); - double top = fun(hi); - double mid = (lo + hi) / 2.0; - double cen = fun(mid); - - if( bot * cen < -EPS ) - return Bisection(lo, mid); - else if( top * cen < -EPS ) - return Bisection(mid, hi); - else - return mid; +double bisection() +{ + double lo = 0, hi = 1; + while (lo + EPS < hi) + { + double x = (lo + hi) / 2; + if (f(lo) * f(x) <= 0) + { + hi = x; + } + else + { + lo = x; + } + } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( scanf("%lf%lf%lf%lf%lf%lf", &p, &q, &r, &s, &t, &u) != EOF ){ - - double bot = fun(0.0); - double top = fun(1.0); - - if( ( bot > EPS && top > EPS ) || ( bot < -EPS && top < -EPS ) ) - printf("No solution\n"); - - else if( bot > -EPS && bot < EPS ) - printf("0.0000\n"); - - else if( top > -EPS && top < EPS ) - printf("1.0000\n"); - - else{ - double x = Bisection(0.0, 1.0); - printf("%.4lf\n", x); +int main() +{ + while (cin >> p >> q >> r >> s >> t >> u) + { + if (f(0) * f(1) > 0) + { + cout << "No solution" << endl; + } + else + { + printf("%.4f\n", bisection()); } } return 0; diff --git a/10342.cpp b/10342.cpp new file mode 100644 index 0000000..949f946 --- /dev/null +++ b/10342.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define MAXN 102 +#define INF 1000000 +#define MIN(a, b) (a > b ? b : a) + +int N, E; + +struct ss +{ + int ini; + int best; + int sec; +} Cost[MAXN][MAXN]; +void BestIni() +{ + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + Cost[i][j].best = INF; + Cost[i][j].ini = INF; + } + Cost[i][i].best = 0; + Cost[i][i].ini = 0; + } +} +void FindBest() +{ + int i, j, k; + for (k = 0; k < N; k++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + Cost[i][j].best = + MIN(Cost[i][j].best, Cost[i][k].best + Cost[k][j].best); + } + } + } +} +void SecIni() +{ + int i, j, min; + for (i = 0; i < N; i++) + { + min = INF; + for (j = 0; j < N; j++) + { + if (i != j) + { + min = MIN(min, Cost[i][j].best); + } + Cost[i][j].sec = MIN(Cost[i][j].ini, Cost[i][j].best * 3); + if (Cost[i][j].sec == Cost[i][j].best) + { + Cost[i][j].sec = Cost[i][j].best * 3; + } + } + Cost[i][i].sec = min * 2; + } +} +void FindSecond() +{ + int i, j, k, secbest; + int bb, bs, sb, ss; + for (k = 0; k < N; k++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + bb = Cost[i][k].best + Cost[k][j].best; + bs = Cost[i][k].best + Cost[k][j].sec; + sb = Cost[i][k].sec + Cost[k][j].best; + ss = Cost[i][k].sec + Cost[k][j].sec; + secbest = MIN(bs, MIN(sb, ss)); + if (bb != Cost[i][j].best) + { + secbest = MIN(secbest, bb); + } + Cost[i][j].sec = MIN(Cost[i][j].sec, secbest); + } + } + } +} +void Cal() +{ + int Q, u, v; + scanf("%d", &Q); + FindBest(); + SecIni(); + FindSecond(); + while (Q--) + { + scanf("%d%d", &u, &v); + Cost[u][v].sec < INF ? printf("%d\n", Cost[u][v].sec) : printf("?\n"); + } +} + +int main() +{ + int i, u, v, c, Set = 1; + while (scanf("%d%d", &N, &E) == 2) + { + BestIni(); + for (i = 0; i < E; i++) + { + scanf("%d%d%d", &u, &v, &c); + Cost[u][v].best = Cost[v][u].best = c; + Cost[u][v].ini = Cost[v][u].ini = c; + } + printf("Set #%d\n", Set++); + Cal(); + } + return 0; +} diff --git a/10344.cpp b/10344.cpp index 9851f16..2751ccd 100644 --- a/10344.cpp +++ b/10344.cpp @@ -1,47 +1,71 @@ -#include -#include +#include + using namespace std; -int func(int tot, int val, int I) { - switch (I) { - case 0: return (tot + val); - case 1: return (tot - val); - case 2: return (tot * val); +int X[5]; +int check(int lv, int v) +{ + if (lv == 5) + { + return (v == 23); + } + if (check(lv + 1, v + X[lv])) + { + return 1; + } + if (check(lv + 1, v - X[lv])) + { + return 1; + } + if (check(lv + 1, v * X[lv])) + { + return 1; + } + return 0; +} +int perm(int lv) +{ + if (lv == 5) + { + return check(1, X[0]); + } + int i, t; + if (perm(lv + 1)) + { + return 1; + } + for (i = lv + 1; i < 5; i++) + { + t = X[lv]; + X[lv] = X[i]; + X[i] = t; + if (perm(lv + 1)) + { + return 1; + } + t = X[lv]; + X[lv] = X[i]; + X[i] = t; } return 0; } -int main() { - while (true) { - int num[5]; - for (int i = 0; i < 5; i++) - scanf("%d", &num[i]); - if (count(num, num + 5, 0) == 5) +int main() +{ + while (scanf("%d%d%d%d%d", X, X + 1, X + 2, X + 3, X + 4) == 5) + { + if ((X[0] | X[1] | X[2] | X[3] | X[4]) == 0) + { break; - - bool flag = false; - sort(num, num + 5); - do { - for (int a = 0; a < 3; a++) - for (int b = 0; b < 3; b++) - for (int c = 0; c < 3; c++) - for (int d = 0; d < 3; d++) { - int tot = num[0]; - tot = func(tot, num[1], a); - tot = func(tot, num[2], b); - tot = func(tot, num[3], c); - tot = func(tot, num[4], d); - if (tot == 23) { - flag = true; - break; - } - } - } while (next_permutation(num, num + 5)); - - if (flag) + } + if (perm(0)) + { printf("Possible\n"); + } else + { printf("Impossible\n"); + } } return 0; } diff --git a/10346.cpp b/10346.cpp index a434c44..8625759 100644 --- a/10346.cpp +++ b/10346.cpp @@ -1,16 +1,21 @@ -#include - -int main() { - int n, k; - while (scanf("%d %d", &n, &k) != EOF) { - int total = n; - while(n >= k) { - int curr = n / k; - total += curr; - n %= k; - n += curr; - } - printf("%d\n", total); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n, k, total, butt; + while (cin >> n >> k) + { + total = butt = 0; + while (n) + { + total += n; + butt += n; + n = butt / k; + butt %= k; + } + cout << total << endl; + } + return 0; +} diff --git a/10347.cpp b/10347.cpp index e39854b..9d67119 100644 --- a/10347.cpp +++ b/10347.cpp @@ -1,21 +1,23 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - double a,b,c; - while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){ - double s=0.5*(a+b+c); - double area=s*(s-a)*(s-b)*(s-c); - if(area<=0){ - printf("-1.000\n"); - continue; - } - else{ - area=4*sqrt(area)/3; - printf("%.3lf\n",area); - } - } - return 0; -} +#include + +using namespace std; + +int main() +{ + double m1, m2, m3, s; + while (scanf("%lf%lf%lf", &m1, &m2, &m3) == 3) + { + double temp; + s = (m1 + m2 + m3) / 2.0; + temp = s * (s - m1) * (s - m2) * (s - m3); + if (temp > 0) + { + printf("%.3lf\n", 4.0 * sqrt(temp) / 3.0); + } + else + { + printf("-1.000\n"); + } + } + return 0; +} diff --git a/10348.cpp b/10348.cpp new file mode 100644 index 0000000..27e9aae --- /dev/null +++ b/10348.cpp @@ -0,0 +1,186 @@ +#include + +using namespace std; + +#define inf 2100000000 +#define EP 1e-10 + +typedef struct +{ + double x, y; +} TPoint; +typedef struct +{ + TPoint a, b; +} TLineSeg; +double max(double a, double b) +{ + return a > b ? a : b; +} +double min(double a, double b) +{ + return a < b ? a : b; +} +double the_distance(TPoint p1, TPoint p2) +{ + return (sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))); +} +double xmult(TPoint p1, TPoint p2, TPoint p0) +{ + return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y)); +} +double dmult(TPoint p1, TPoint p2, TPoint p0) +{ + return ((p1.x - p0.x) * (p2.x - p0.x) + (p1.y - p0.y) * (p2.y - p0.y)); +} +int euqal_point(TPoint p1, TPoint p2) +{ + return ((fabs(p1.x - p2.x) < EP) && (fabs(p1.y - p2.y) < EP)); +} + +int intersect(TLineSeg u, TLineSeg v); +int Done(); +int insidepolygon(int vcount, TPoint polygon[], TPoint q); + +TLineSeg ship[110]; +TPoint id[60][25]; +int ge[60]; +int N, M; + +int main() +{ + int i; + cin >> N; + for (i = 0; i < N; i++) + { + cin >> ship[i].a.x >> ship[i].a.y; + cin >> ship[i].b.x >> ship[i].b.y; + } + cin >> M; + for (i = 0; i < M; i++) + { + cin >> ge[i]; + for (int j = 0; j < ge[i]; j++) + { + cin >> id[i][j].x >> id[i][j].y; + } + } + Done(); + return 0; +} + +int Done() +{ + TLineSeg l1; + for (int i = 0; i < N; i++) + { + int flag = 0; + for (int j = 0; j < M; j++) + { + for (int k = 0; k < ge[j]; k++) + { + l1.a.x = id[j][k].x; + l1.a.y = id[j][k].y; + int k1 = (k + 1) % ge[j]; + l1.b.x = id[j][k1].x; + l1.b.y = id[j][k1].y; + if (intersect(l1, ship[i])) + { + flag = 1; + j = M; + break; + } + } + } + if (flag) + { + cout << "Submarine " << i + 1 << " is partially on land." << endl; + } + else + { + int ff = 0; + for (int j = 0; j < M; j++) + { + if (insidepolygon(ge[j], id[j], ship[i].a)) + { + ff = 1; + break; + } + } + if (ff) + { + cout << "Submarine " << i + 1 << " is completely on land." << endl; + } + else + { + cout << "Submarine " << i + 1 << " is still in water." << endl; + } + } + } + return 0; +} + +int intersect(TLineSeg u, TLineSeg v) +{ + return ((max(u.a.x, u.b.x) >= min(v.a.x, v.b.x)) && + (max(v.a.x, v.b.x) >= min(u.a.x, u.b.x)) && + (max(u.a.y, u.b.y) >= min(v.a.y, v.b.y)) && + (max(v.a.y, v.b.y) >= min(u.a.y, u.b.y)) && + (xmult(v.a, u.b, u.a) * xmult(u.b, v.b, u.a) >= 0) && + (xmult(u.a, v.b, v.a) * xmult(v.b, u.b, v.a) >= 0)); +} + +int insidepolygon(int vcount, TPoint polygon[], TPoint q) +{ + int c, i, j; + double t, angle1, angle2; + TLineSeg l1, l2; + TPoint dir[16] = {{100, 0}, {0, 100}, {-100, 0}, {0, -100}, {100, 100}, {-100, 100}, {-100, -100}, {100, -100}, {50, 100}, {-50, 100}, {-50, -100}, {50, -100}, {100, 50}, {-100, 50}, {-100, -50}, {100, -50}}; + l1.a = q; + for (i = 0; i < 16; i++) + { + l1.b.x = q.x + dir[i].x; + l1.b.y = q.y + dir[i].y; + for (c = 0, j = 0; j < vcount; j++) + { + l2.a = polygon[j]; + l2.b = polygon[(j + 1) % vcount]; + t = xmult(l2.a, l1.b, l1.a) * xmult(l1.b, l2.b, l1.a); + if (t > 1e-6) + { + if (xmult(l1.a, l2.b, l2.a) * xmult(l2.b, l1.b, l2.a) >= 0) + { + c++; + } + else + { + double one = dmult(l1.a, l2.b, l2.a); + double two = the_distance(l2.a, l1.a); + double three = the_distance(l2.a, l2.b); + angle1 = one / (two * three); + one = dmult(l1.b, l2.b, l2.a); + two = the_distance(l2.a, l1.b); + three = the_distance(l2.a, l2.b); + angle2 = one / (two * three); + if (angle1 < angle2) + { + c++; + } + } + } + else if (t < -1e-6) + { + continue; + } + else + { + break; + } + } + if (j == vcount) + { + break; + } + } + return (c % 2 != 0); +} diff --git a/10349.cpp b/10349.cpp new file mode 100644 index 0000000..d5239f6 --- /dev/null +++ b/10349.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +struct Node +{ + int x, y, v;// x->y, v + int next; +} edge[500005]; +int e, head[500], dis[500], prev_[500], record[500]; +void addEdge(int x, int y, int v) +{ + edge[e].x = x, edge[e].y = y, edge[e].v = v; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].v = 0; + edge[e].next = head[y], head[y] = e++; +} +int maxflow(int s, int t) +{ + int flow = 0; + int i, j, x, y; + while (1) + { + memset(dis, 0, sizeof(dis)); + dis[s] = 0xffff;// oo + queue Q; + Q.push(s); + while (!Q.empty()) + { + x = Q.front(); + Q.pop(); + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (dis[y] == 0 && edge[i].v > 0) + { + prev_[y] = x, record[y] = i; + dis[y] = min(dis[x], edge[i].v); + Q.push(y); + } + } + if (dis[t]) + break; + } + if (dis[t] == 0) + break; + flow += dis[t]; + for (x = t; x != s; x = prev_[x]) + { + int ri = record[x]; + edge[ri].v -= dis[t]; + edge[ri ^ 1].v += dis[t]; + } + } + return flow; +} +int main() +{ + int t, n, m; + int i, j; + char g[105][105]; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &m); + for (i = 0; i < n; i++) + scanf("%s", g[i]); + int st = 0, ed = n * m + 1; + //g[i][j] = i*m+j+1 + e = 0; + memset(head, -1, sizeof(head)); + int cntNode = 0; + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + if (g[i][j] == '*') + cntNode++; + if ((i + j) % 2 && g[i][j] == '*') + { + addEdge(st, i * m + j + 1, 1); + if (i - 1 >= 0 && g[i - 1][j] == '*') + addEdge(i * m + j + 1, (i - 1) * m + j + 1, 1); + if (j - 1 >= 0 && g[i][j - 1] == '*') + addEdge(i * m + j + 1, i * m + j - 1 + 1, 1); + if (i + 1 < n && g[i + 1][j] == '*') + addEdge(i * m + j + 1, (i + 1) * m + j + 1, 1); + if (j + 1 < m && g[i][j + 1] == '*') + addEdge(i * m + j + 1, i * m + j + 1 + 1, 1); + } + if ((i + j) % 2 == 0 && g[i][j] == '*') + { + addEdge(i * m + j + 1, ed, 1); + } + } + } + int flow = maxflow(st, ed); + printf("%d\n", (cntNode - 2 * flow) + flow); + } + return 0; +} diff --git a/10350.cpp b/10350.cpp new file mode 100644 index 0000000..a7042c3 --- /dev/null +++ b/10350.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +char name[16]; +int n, m, time_[120][15], t[15][15]; + +int main() +{ + while (scanf("%s", name) == 1) + { + puts(name); + scanf("%d %d", &n, &m); + for (int i = 0; i < 120; i++) + { + for (int j = 0; j < 15; j++) + { + time_[i][j] = 99999999; + } + } + for (int i = 0; i < 15; i++) + { + time_[0][i] = 0; + } + for (int i = 1; i <= n - 1; i++) + { + for (int j = 0; j < m; j++) + { + for (int k = 0; k < m; k++) + { + scanf("%d", &t[j][k]); + } + } + for (int j = 0; j < m; j++) + { + for (int k = 0; k < m; k++) + { + if (time_[i - 1][j] + t[j][k] < time_[i][k]) + { + time_[i][k] = time_[i - 1][j] + t[j][k]; + } + } + } + } + int p = 99999999; + for (int i = 0; i < m; i++) + { + if (time_[n - 1][i] < p) + { + p = time_[n - 1][i]; + } + } + printf("%d\n", p + 2 * (n - 1)); + } + return 0; +} diff --git a/10351.cpp b/10351.cpp new file mode 100644 index 0000000..69886ed --- /dev/null +++ b/10351.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int main() +{ + int t = 1, x, y, z, xx, yy, zz; + while (scanf("%d%d%d%d%d%d", &x, &y, &z, &xx, &yy, &zz) == 6) + { + double a, b, c, diff = 0.0f; + if (x < xx) + { + a = y; + b = z; + c = xx; + diff = xx - (xx * 2 - x * 2); + } + else if (y < yy) + { + a = x; + b = z; + c = yy; + diff = yy - (yy * 2 - y * 2); + } + else if (z < zz) + { + a = x; + b = y; + c = zz; + diff = zz - (zz * 2 - z * 2); + } + else + { + printf("Set #%d\n", t++); + printf("%.06lf\n", 0.0f); + continue; + } + double a1 = sqrt(a * a * (1 - (diff * diff) / (c * c))); + double b1 = sqrt(b * b * (1 - (diff * diff) / (c * c))); + printf("Set #%d\n", t++); + printf("%.06lf\n", M_PI / 4 * a1 * b1); + } + return 0; +} diff --git a/10352.cpp b/10352.cpp new file mode 100644 index 0000000..e1d9219 --- /dev/null +++ b/10352.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +struct Word +{ + char word[6]; + int times; + Word *left, *right; + Word(char *s) + { + strcpy(word, s); + times = 1; + left = right = NULL; + } +} * root; + +char temp[20], str[25]; + +int cmpstr(char *s, char *d) +{ + int i; + for (i = 0; s[i] && d[i]; i++) + { + if (i == 2) + { + continue; + } + if (s[i] < d[i]) + { + return -1; + } + if (s[i] > d[i]) + { + return 1; + } + } + if (d[i]) + { + return -1; + } + if (s[i]) + { + return 1; + } + return 0; +} +Word *find(char *s, Word *p) +{ + if (p == NULL) + { + return NULL; + } + else if (cmpstr(s, p->word) == 0) + { + return p; + } + else if (cmpstr(s, p->word) < 0) + { + return find(s, p->left); + } + else + { + return find(s, p->right); + } +} +void insert(char *s, Word *&p) +{ + if (p == NULL) + { + p = new Word(s); + } + else if (cmpstr(s, p->word) < 0) + { + return insert(s, p->left); + } + else + { + insert(s, p->right); + } +} +void inorder(Word *p) +{ + if (p) + { + inorder(p->left); + printf("%s %d\n", p->word, p->times); + inorder(p->right); + } +} +void del(Word *&p) +{ + if (p) + { + del(p->left); + del(p->right); + delete p; + } +} + +int main() +{ + int caseno = 1; + while (scanf("%s", str) == 1) + { + root = NULL; + while (true) + { + if (strcmp(str, "#") == 0) + { + break; + } + if (strlen(str) > 4) + { + strncpy(temp, str, 5); + } + else + { + strcpy(temp, str); + } + Word *s = find(temp, root); + if (s) + { + strcpy(s->word, temp); + s->times++; + } + else + { + insert(temp, root); + } + scanf("%s", str); + } + printf("Set #%d:\n", caseno++); + inorder(root); + printf("\n"); + del(root); + } + return 0; +} diff --git a/10354.cpp b/10354.cpp new file mode 100644 index 0000000..42915c2 --- /dev/null +++ b/10354.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define MIN(A, B) (A < B ? A : B) +#define MAXN 103 +#define INF 21474836 + +int D[MAXN][MAXN], Temp[MAXN][MAXN], P[MAXN][MAXN]; +int Flag[MAXN], N, R, BH, OF, YH, M; +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + D[i][j] = Temp[i][j] = INF; + D[j][i] = Temp[j][i] = INF; + P[i][j] = P[j][i] = -1; + } + D[i][i] = 0; + Flag[i] = 0; + } +} +void Flyod() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (D[i][j] > (D[i][k] + D[k][j])) + { + D[i][j] = D[i][k] + D[k][j]; + P[i][j] = P[k][j]; + } + else + { + D[i][j] = D[i][j]; + P[i][j] = P[i][j]; + } + } + } + } +} +void Flyod_2() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + if (Flag[k]) + { + continue; + } + for (i = 1; i <= N; i++) + { + if (Flag[i]) + { + continue; + } + for (j = 1; j <= N; j++) + { + if (Flag[j]) + { + continue; + } + if (Temp[i][j] > (Temp[i][k] + Temp[k][j])) + { + Temp[i][j] = Temp[i][k] + Temp[k][j]; + } + else + { + Temp[i][j] = Temp[i][j]; + } + } + } + } +} +void BossPath(int i, int j) +{ + if (i == j) + { + Flag[i] = 1; + } + else + { + BossPath(i, P[i][j]); + Flag[j] = 1; + } +} +int MyPath(int i, int j) +{ + if (i == j) + { + if (Flag[i]) + { + return 1; + } + } + else + { + if (MyPath(i, P[i][j])) + { + return 1; + } + if (Flag[j]) + { + return 1; + } + } + return 0; +} +void Mark() +{ + int k; + for (k = 1; k <= N; k++) + { + if (Flag[k]) + { + continue; + } + if (D[BH][k] + D[k][OF] == D[BH][OF]) + { + BossPath(BH, k); + BossPath(k, OF); + } + } +} +void Cal() +{ + if (BH == YH || BH == M || OF == YH || OF == M) + { + printf("MISSION IMPOSSIBLE.\n"); + return; + } + if (R == 0) + { + if (YH == M) + { + printf("0\n"); + } + else + { + printf("MISSION IMPOSSIBLE.\n"); + } + return; + } + Flyod(); + Mark(); + if (Flag[M] || Flag[YH]) + { + printf("MISSION IMPOSSIBLE.\n"); + return; + } + if (!MyPath(YH, M)) + { + printf("%d\n", D[YH][M]); + return; + } + Flyod_2(); + if (Temp[YH][M] == INF) + { + printf("MISSION IMPOSSIBLE.\n"); + return; + } + printf("%d\n", Temp[YH][M]); +} +int main() +{ + int i, a, b, c; + while (scanf("%d%d%d%d%d%d", &N, &R, &BH, &OF, &YH, &M) == 6) + { + Ini(); + for (i = 0; i < R; i++) + { + scanf("%d%d%d", &a, &b, &c); + Temp[a][b] = Temp[b][a] = D[a][b] = D[b][a] = c; + P[a][b] = a; + P[b][a] = b; + } + Cal(); + } + return 0; +} diff --git a/10355.cpp b/10355.cpp new file mode 100644 index 0000000..c797ddc --- /dev/null +++ b/10355.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +double a, b, c, d, e, f, dx, dy, dz, x, y, z, r, A, B, C, D, t1, t2, total, sum; +int n; +double dist(double x, double y, double z) +{ + return sqrt(x * x + y * y + z * z); +} +bool valid(double t) +{ + if (t >= 0 && t <= 1) + { + return true; + } + return false; +} +int main() +{ + char str[200]; + int flag; + while (scanf("%s", str) == 1) + { + flag = 0; + puts(str); + scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f); + dx = d - a; + dy = e - b; + dz = f - c; + scanf("%d", &n); + total = dist(dx, dy, dz); + sum = 0; + A = dx * dx + dy * dy + dz * dz; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf %lf %lf", &x, &y, &z, &r); + if (flag) + { + continue; + } + if (dist(a - x, b - y, c - z) <= r && dist(d - x, e - y, f - z) <= r) + { + flag = 1; + } + B = 2 * (dx * (a - x) + dy * (b - y) + dz * (c - z)); + C = (a - x) * (a - x) + (b - y) * (b - y) + (c - z) * (c - z) - r * r; + D = B * B - 4 * A * C; + if (D <= 0) + { + continue; + } + D = sqrt(D); + t1 = (-B - D) / (2 * A); + t2 = (-B + D) / (2 * A); + if (valid(t1) && valid(t2)) + { + sum += dist((t2 - t1) * dx, (t2 - t1) * dy, (t2 - t1) * dz); + } + else if (valid(t1)) + { + double tempx = a + dx * t1, tempy = b + dy * t1, tempz = c + dz * t1; + if (dist(a - x, b - y, c - z) < r) + { + sum += dist(t1 * dx, t1 * dy, t1 * dz); + } + else if (dist(d - x, e - y, f - z) < r) + { + sum += dist(tempx - d, tempy - e, tempz - f); + } + } + else if (valid(t2)) + { + double tempx = a + dx * t2, tempy = b + dy * t2, tempz = c + dz * t2; + if (dist(a - x, b - y, c - z) < r) + { + sum += dist(t2 * dx, t2 * dy, t2 * dz); + } + else if (dist(d - x, e - y, f - z) < r) + { + sum += dist(tempx - d, tempy - e, tempz - f); + } + } + } + if (sum > total) + { + sum = total; + } + if (flag == 1) + { + printf("100.00\n"); + } + else + { + printf("%.2lf\n", sum * 100 / total); + } + } + return 0; +} diff --git a/10356.cpp b/10356.cpp new file mode 100644 index 0000000..b1bbff9 --- /dev/null +++ b/10356.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define MAXN 502 +#define MAX MAXN * 3 +#define INF 21474836 + +struct ss +{ + int A[MAXN]; + int ind; + int Odd, Even; + char C; +} link[MAXN]; +int Q[MAX], qh, qt, List[MAXN][MAXN], N, M; +void Ini() +{ + int i, j; + for (i = 0; i < N; i++) + { + link[i].Odd = link[i].Even = INF; + link[i].ind = 0; + link[i].C = 'w'; + for (j = i + 1; j < N; j++) + { + List[i][j] = List[j][i] = 0; + } + } +} +void Push(int n) +{ + Q[qh++] = n; + qh %= MAX; +} +int Pop() +{ + int n; + n = Q[qt++]; + qt %= MAX; + return n; +} +void BFS(int des) +{ + int i, k, n, ind, c; + int Od, Ev, f; + qh = qt = 0; + link[0].Odd = INF; + link[0].Even = 0; + Push(0); + while (qt != qh) + { + n = Pop(); + ind = link[n].ind; + link[n].C = 'w'; + for (i = 0; i < ind; i++) + { + f = 0; + k = link[n].A[i]; + c = List[n][k]; + Od = link[n].Even + c; + Ev = link[n].Odd + c; + if (link[k].Even > Ev) + { + link[k].Even = Ev; + f = 1; + } + if (link[k].Odd > Od) + { + link[k].Odd = Od; + f = 1; + } + if (f) + { + link[k].C = 'r'; + Push(k); + } + } + } +} +void Cal() +{ + BFS(N - 1); + if (link[N - 1].Even < INF) + { + printf("%d\n", link[N - 1].Even); + } + else + { + printf("?\n"); + } +} +int main() +{ + int i, u, v, c, ind1, ind2, st = 1; + while (scanf("%d%d", &N, &M) == 2) + { + Ini(); + for (i = 0; i < M; i++) + { + scanf("%d%d%d", &u, &v, &c); + ind1 = link[u].ind; + ind2 = link[v].ind; + if (List[u][v] || List[v][u]) + { + if (List[u][v] > c) + { + List[u][v] = List[v][u] = c; + } + continue; + } + link[u].A[ind1] = v; + link[v].A[ind2] = u; + List[u][v] = List[v][u] = c; + link[u].ind++; + link[v].ind++; + } + printf("Set #%d\n", st++); + Cal(); + } + return 0; +} diff --git a/10357.cpp b/10357.cpp new file mode 100644 index 0000000..17b855f --- /dev/null +++ b/10357.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +struct Player +{ + int x, y, v; +} p[20]; +int no, balls; + +int main() +{ + char str[80]; + int i, j; + gets(str); + for (i = 0; !isdigit(str[i]); i++) + ; + no = atoi(str + i); + for (i = 0; i < no; i++) + { + gets(str); + sscanf(str, "%d %d %d", &p[i].x, &p[i].y, &p[i].v); + } + gets(str); + for (i = 0; !isdigit(str[i]); i++) + ; + balls = atoi(str + i); + for (i = 0; i < balls; i++) + { + int a, b, c, d, e, f, g; + int x, y, tt; + double t; + gets(str); + sscanf(str, "%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g); + double D = sqrt(b * b - 4 * a * c); + t = (-b - D) / 2 / (double)a; + if (t < 0) + { + t = (-b + D) / 2 / (double)a; + } + tt = (int)t; + if (a * tt * tt + b * tt + c > 0) + { + tt++; + } + printf("Ball %d was ", i + 1); + x = d * tt + e; + y = f * tt + g; + if (x < 0 || y < 0) + { + printf("foul at (%d,%d)\n", x, y); + continue; + } + for (j = 0; j < no; j++) + { + int dx = x - p[j].x, dy = y - p[j].y; + if (dx * dx + dy * dy <= p[j].v * p[j].v * t * t) + { + break; + } + } + if (j == no) + { + printf("safe at (%d,%d)\n", x, y); + } + else + { + printf("caught at (%d,%d)\n", x, y); + } + } + return 0; +} diff --git a/10358.cpp b/10358.cpp new file mode 100644 index 0000000..3631b00 --- /dev/null +++ b/10358.cpp @@ -0,0 +1,284 @@ +#include + +using namespace std; + +#define CLR(a, x) memset(a, x, sizeof(a)) + +typedef long long LL; + +const int MAXN = 100005; +const int MOVE[2][5] = {{-1, 0, 1, 0, 0}, {0, 1, 0, -1, 0}}; + +char maze[10][10]; +int win[2][66][66][66], deg[2][66][66][66], emt[66], M, A1, A2, c; + +inline int get_x(int r, int c) +{ + return r * 8 + c; +} +inline int row(int x) +{ + return x / 8; +} +inline int col(int x) +{ + return x % 8; +} +bool in_range(int r, int c) +{ + return 0 <= r && r < 8 && 0 <= c && c < 8; +} + +void input() +{ + for (int i = 0; i < 8; ++i) + { + scanf("%s", maze[i]); + } + M = A1 = A2 = -1; + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + if (maze[i][j] == 'M') + { + M = get_x(i, j); + } + else if (maze[i][j] == 'A') + { + if (A1 == -1) + { + A1 = get_x(i, j); + } + else + { + A2 = get_x(i, j); + } + } + } + } + CLR(deg, 0); + c = 0; + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + if (maze[i][j] == '#') + continue; + emt[c++] = get_x(i, j); + } + } + CLR(win, -1); + for (int i = 0; i < c; ++i) + { + for (int j = 0; j < c; ++j) + { + for (int k = 0; k < c; ++k) + { + int x = emt[i], y = emt[j], z = emt[k]; + int r = row(x), c = col(x); + if (maze[r][c] == 'P') + { + if (x == y || x == z) + { + win[1][x][y][z] = 1; + } + else + { + win[0][x][y][z] = 1, win[1][x][y][z] = 0; + } + } + else if (x == y || x == z) + { + win[0][x][y][z] = 0, win[1][x][y][z] = 1; + } + else + { + for (int d = 0; d < 5; ++d) + { + int rr = r + MOVE[0][d]; + int cc = c + MOVE[1][d]; + if (!in_range(rr, cc) || maze[rr][cc] == '#') + { + continue; + } + ++deg[0][x][y][z]; + } + r = row(y), c = col(y); + int r1 = row(z), c1 = col(z); + for (int d = 0; d < 5; ++d) + { + int rr = r + MOVE[0][d]; + int cc = c + MOVE[1][d]; + if (!in_range(rr, cc) || maze[rr][cc] == '#') + { + continue; + } + for (int dd = 0; dd < 5; ++dd) + { + int rr1 = r1 + MOVE[0][dd]; + int cc1 = c1 + MOVE[1][dd]; + if (!in_range(rr1, cc1) || maze[rr1][cc1] == '#') + { + continue; + } + ++deg[1][x][y][z]; + } + } + } + } + } + } +} + +struct State +{ + int m, a1, a2, player; + State(int m = 0, int a1 = 0, int a2 = 0, int player = 0) + : m(m), a1(a1), a2(a2), player(player) + { + } +}; + +void solve() +{ + queue q; + for (int i = 0; i < c; ++i) + { + for (int j = 0; j < c; ++j) + { + for (int k = 0; k < c; ++k) + { + int x = emt[i], y = emt[j], z = emt[k]; + if (win[0][x][y][z] != -1) + { + q.push(State(x, y, z, 0)); + } + if (win[1][x][y][z] != -1) + { + q.push(State(x, y, z, 1)); + } + } + } + } + int c_m = 26, c_a1 = 25, c_a2 = 25, c_p = 0; + while (q.size()) + { + State t = q.front(); + q.pop(); + int p = t.player, m = t.m, a1 = t.a1, a2 = t.a2; + if (p == 1) + { + int r = row(m), c = col(m); + assert(win[p][m][a1][a2] != -1); + if (win[p][m][a1][a2] == 0) + { + for (int d = 0; d < 5; ++d) + { + int rr = r + MOVE[0][d]; + int cc = c + MOVE[1][d]; + if (!in_range(rr, cc) || maze[rr][cc] == '#') + { + continue; + } + int y = get_x(rr, cc); + assert(win[p ^ 1][a1][a2] != 0); + if (win[p ^ 1][y][a1][a2] == -1) + { + win[p ^ 1][y][a1][a2] = 1; + q.push(State(y, a1, a2, p ^ 1)); + } + } + } + else if (win[p][m][a1][a2] == 1) + { + for (int d = 0; d < 5; ++d) + { + int rr = r + MOVE[0][d]; + int cc = c + MOVE[1][d]; + if (!in_range(rr, cc) || maze[rr][cc] == '#') + { + continue; + } + int y = get_x(rr, cc); + if (win[p ^ 1][y][a1][a2] == -1) + { + --deg[p ^ 1][y][a1][a2]; + if (deg[p ^ 1][y][a1][a2] == 0) + { + q.push(State(y, a1, a2, p ^ 1)); + win[p ^ 1][y][a1][a2] = 0; + } + } + } + } + } + else + { + int r1 = row(a1), c1 = col(a1); + int r2 = row(a2), c2 = col(a2); + for (int d = 0; d < 5; ++d) + { + int rr1 = r1 + MOVE[0][d]; + int cc1 = c1 + MOVE[1][d]; + if (!in_range(rr1, cc1) || maze[rr1][cc1] == '#') + { + continue; + } + int y = get_x(rr1, cc1); + for (int dd = 0; dd < 5; ++dd) + { + int rr2 = r2 + MOVE[0][dd]; + int cc2 = c2 + MOVE[1][dd]; + if (!in_range(rr2, cc2) || maze[rr2][cc2] == '#') + { + continue; + } + int z = get_x(rr2, cc2); + if (win[p][m][a1][a2] == 0) + { + assert(win[p ^ 1][m][y][z] != 0); + if (win[p ^ 1][m][y][z] == -1) + { + win[p ^ 1][m][y][z] = 1; + q.push(State(m, y, z, p ^ 1)); + } + } + else if (win[p ^ 1][m][y][z] == -1) + { + --deg[p ^ 1][m][y][z]; + if (deg[p ^ 1][m][y][z] == 0) + { + win[p ^ 1][m][y][z] = 0; + q.push(State(m, y, z, p ^ 1)); + } + } + } + } + } + } + if (win[0][M][A1][A2] == 1) + { + puts("You can escape."); + } + else if (win[0][M][A1][A2] == 0) + { + puts("You are eliminated."); + } + else + { + puts("You are trapped in the Matrix."); + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + input(); + solve(); + } + return 0; +} diff --git a/10359.cpp b/10359.cpp new file mode 100644 index 0000000..7f055e4 --- /dev/null +++ b/10359.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define MAXN 1000 + +int N, J; +char Store[MAXN], Temp[MAXN], Temp1[MAXN]; +struct LONGINT +{ + char Digit[MAXN]; + int Len; +} V[251]; +void ADD(int s, int k) +{ + int i, n, m, carry = 0; + int p = 0; + int l = V[k].Len; + J > l ? m = J : m = l; + for (i = m - 1; i >= 0; i--) + { + n = V[k].Digit[i] + Temp1[i] + carry; + Temp[p++] = n % 10; + carry = n / 10; + } + if (carry) + { + Temp[p++] = carry; + } + m = 0; + for (i = p - 1; i >= 0; i--) + { + V[s].Digit[m++] = Temp[i]; + } + V[s].Len = m; +} +void MUL(int k) +{ + int i, n, carry = 0, f = 0; + int l = V[k].Len; + J = 0; + for (i = l - 1; i >= 0; i--) + { + n = V[k].Digit[i] * 2 + carry; + Store[J++] = n % 10; + carry = n / 10; + } + if (carry) + { + Store[J++] = carry; + } + for (i = J - 1; i >= 0; i--) + { + Temp1[f++] = Store[i]; + } +} +void Print() +{ + int i; + for (i = 0; i < V[N].Len; i++) + { + printf("%d", V[N].Digit[i]); + } + putchar('\n'); +} +void Gen() +{ + int i; + V[0].Digit[0] = V[1].Digit[0] = 1; + V[0].Len = V[1].Len = 1; + for (i = 2; i <= 250; i++) + { + MUL(i - 2); + ADD(i, i - 1); + } +} + +int main() +{ + Gen(); + while (scanf("%d", &N) == 1) + { + Print(); + } + return 0; +} diff --git a/10360.cpp b/10360.cpp index e5877fb..c3dc310 100644 --- a/10360.cpp +++ b/10360.cpp @@ -1,86 +1,56 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define MAXN 1027 +#include -int Field[MAXN][MAXN]; -int rad, MaxR, MaxC, MaxRat; +using namespace std; -void Select(int x, int y, int rat) { - if(x > MaxR) return; - if(x < MaxR) { - MaxR = x; - MaxC = y; - return; - } - if(x == MaxR) { - if(y > MaxC) return; - MaxC = y; - } -} +int A[1030][1030]; -void Set(int x, int y, int rat) { - int tempx, tempy, i, j; - tempx = x - rad; - tempy = y - rad; - for (i = tempx; i<= x + rad; i++) { - if (i < 0) continue; - if (i > 1024) break; - for (j = tempy; j <= y + rad; j++){ - if (j < 0) continue; - if (j > 1024) break; - Field[i][j] += rat; - if (Field[i][j] > MaxRat) { - MaxR = i; - MaxC = j; - MaxRat = Field[i][j]; +int main() +{ + int T, d, n, x1, y1, z1; + scanf("%d", &T); + while (T-- > 0 && scanf("%d %d", &d, &n) == 2) + { + memset(A, 0, sizeof(A)); + while (n-- > 0 && scanf("%d %d %d", &x1, &y1, &z1) == 3) + { + A[y1 + 1][x1 + 1] = z1; + } + for (int y = 1; y <= 1025; y++) + { + for (int x = 1, s = 0; x <= 1025; x++) + { + s += A[y][x]; + A[y][x] = A[y - 1][x] + s; } - else if (Field[i][j] == MaxRat) - Select(i, j, Field[i][j]); } - } -} - -int main() { - int kase, popu; - int i, x, y, rat; - scanf("%d", &kase); - while (kase--) { - scanf("%d", &rad); - scanf("%d", &popu); - MaxRat = 0; - MaxR = MaxC = 1024; - for (i = 0; i < popu; i++){ - scanf("%d%d%d",&x, &y, &rat); - Set(x, y, rat); + x1 = y1 = 1; + z1 = 0; + int D = 2 * d + 1; + for (int x = d + 1; x <= 1025; x++) + { + for (int y = d + 1; y <= 1025; y++) + { + int t = A[y][x]; + if (y >= D) + { + t -= A[y - D][x]; + } + if (x >= D) + { + t -= A[y][x - D]; + } + if (x >= D && y >= D) + { + t += A[y - D][x - D]; + } + if (t > z1) + { + x1 = x - d; + y1 = y - d; + z1 = t; + } + } } - printf("%d %d %d\n",MaxR, MaxC, MaxRat); - for (i = 0; i <= 1025; i++) - memset(Field[i],0,sizeof(int)*1026); + printf("%d %d %d\n", x1 - 1, y1 - 1, z1); } - return 0; } diff --git a/10361.cpp b/10361.cpp index 269fcba..0676629 100644 --- a/10361.cpp +++ b/10361.cpp @@ -1,26 +1,63 @@ -#include -#include -#include -#include +#include + using namespace std; -int main() { - int T, i; - scanf("%d\n", &T); - while (T--) { - string A, B; - string s1 = "", s2 = "", s3 = "", s4 = "", s5 = ""; - getline(cin, A); getline(cin, B); - for (i = 0; A[i] != '<'; i++) s1 += A[i]; - for (i = i + 1; A[i] != '>'; i++) s2 += A[i]; - for (i = i + 1; A[i] != '<'; i++) s3 += A[i]; - for (i = i + 1; A[i] != '>'; i++) s4 += A[i]; - for (i = i + 1; i < A.length(); i++) s5 += A[i]; - A = s1 + s2 + s3 + s4 + s5; - int pos = (int)B.find("..."); - B.replace(pos, 3, s4 + s3 + s2 + s5); - cout << A << endl; - cout << B << endl; +char line1[150], line2[150]; +char s1[100], s2[100], s3[100], s4[100], s5[100]; +int T; + +void solveLine1() +{ + int i, j; + for (i = 0, j = 0; line1[i] != '<'; i++) + { + s1[j++] = line1[i]; + } + s1[j] = '\0'; + for (i++, j = 0; line1[i] != '>'; i++) + { + s2[j++] = line1[i]; + } + s2[j] = '\0'; + for (i++, j = 0; line1[i] != '<'; i++) + { + s3[j++] = line1[i]; + } + s3[j] = '\0'; + for (i++, j = 0; line1[i] != '>'; i++) + { + s4[j++] = line1[i]; + } + s4[j] = '\0'; + for (i++, j = 0; line1[i]; i++) + { + s5[j++] = line1[i]; + } + s5[j] = '\0'; + cout << s1 << s2 << s3 << s4 << s5 << endl; +} + +void solveLine2() +{ + int i; + for (i = 0; line2[i] != '.'; i++) + { + cout << line2[i]; + } + cout << s4 << s3 << s2 << s5 << endl; +} + +int main() +{ + cin >> T; + cin.get(); + int t; + for (t = 0; t < T; t++) + { + cin.getline(line1, 150); + solveLine1(); + cin.getline(line2, 150); + solveLine2(); } return 0; } diff --git a/10362.cpp b/10362.cpp new file mode 100644 index 0000000..c83e00f --- /dev/null +++ b/10362.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +typedef vector VI; +typedef vector> VVI; +typedef vector VS; +typedef vector> VVS; +typedef signed long long i64; +typedef unsigned long long u64; +typedef pair PII; + +#define MIN (24 * 60) + +VS nam; +int get(string s) +{ + int i; + for (i = 0; i < nam.size(); i++) + if (s == nam[i]) + { + return i; + } + nam.push_back(s); + return i; +} + +VI cs, cd, cst, ct; + +int main() +{ + int i, j, k, x, y, z, n; + int h, m, t; + int ss, es; + int N, first = 1; + for (scanf(" %d", &N); N; N--) + { + if (!first) + { + putchar('\n'); + } + first = 0; + nam = VS(); + cs = cd = cst = ct = VI(); + string s; + cin >> x; + for (i = 0; i < x; i++) + { + cin >> y; + t = 0; + for (j = 0; j < y; j++) + { + scanf(" %d:%d", &h, &m); + if (j) + { + cst.push_back(t); + } + if (j) + { + cs.push_back(z); + } + if (j) + { + ct.push_back(h * 60 + m); + } + t = (t + h * 60 + m) % MIN; + cin >> s; + z = get(s); + if (j) + { + cd.push_back(z); + } + } + } + cin >> s; + ss = get(s); + cin >> s; + es = get(s); + VVI travel(nam.size(), VI(MIN, 1000000000)); + travel[ss] = VI(MIN); + bool change = true; + while (change) + { + change = false; + for (i = 0; i < cs.size(); i++) + { + x = travel[cs[i]][cst[i]] + ct[i]; + y = (cst[i] + ct[i]) % MIN; + VI &ar = travel[cd[i]]; + while (ar[y] > x) + { + change = true; + ar[y] = x; + y = (y + 1) % MIN; + x++; + } + } + } + vector ret; + for (i = 0; i < MIN; i++) + { + ret.push_back(PII(((i - travel[es][i]) % MIN + MIN) % MIN, travel[es][i])); + } + sort(ret.begin(), ret.end()); + for (i = 0; i < MIN; i++) + { + if (i == 0 || ret[i - 1].first != ret[i].first) + { + printf("%02d:%02d ", ret[i].first / 60, ret[i].first % 60); + printf("%d:%02d\n", ret[i].second / 60, ret[i].second % 60); + } + } + } + return 0; +} diff --git a/10363.cpp b/10363.cpp index e7ed6ba..016eb3c 100644 --- a/10363.cpp +++ b/10363.cpp @@ -1,93 +1,115 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +char field[3][3]; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while( T-- ){ - string S[3]; - cin >> S[0] >> S[1] >> S[2]; - int xWin = 0, yWin = 0; - int X = 0, Y = 0; - int i, j; - if( S[0][0]=='X' && S[1][1]=='X' && S[2][2]=='X' ) xWin++; - if( S[2][0]=='X' && S[1][1]=='X' && S[0][2]=='X' ) xWin++; - if( S[0][0]=='X' && S[1][0]=='X' && S[2][0]=='X' ) xWin++; - if( S[0][1]=='X' && S[1][1]=='X' && S[2][1]=='X' ) xWin++; - if( S[0][2]=='X' && S[1][2]=='X' && S[2][2]=='X' ) xWin++; - if( S[0][0]=='X' && S[0][1]=='X' && S[0][2]=='X' ) xWin++; - if( S[1][0]=='X' && S[1][1]=='X' && S[1][2]=='X' ) xWin++; - if( S[2][0]=='X' && S[2][1]=='X' && S[2][2]=='X' ) xWin++; - - if( S[0][0]=='O' && S[1][1]=='O' && S[2][2]=='O' ) yWin++; - if( S[2][0]=='O' && S[1][1]=='O' && S[0][2]=='O' ) yWin++; - if( S[0][0]=='O' && S[1][0]=='O' && S[2][0]=='O' ) yWin++; - if( S[0][1]=='O' && S[1][1]=='O' && S[2][1]=='O' ) yWin++; - if( S[0][2]=='O' && S[1][2]=='O' && S[2][2]=='O' ) yWin++; - if( S[0][0]=='O' && S[0][1]=='O' && S[0][2]=='O' ) yWin++; - if( S[1][0]=='O' && S[1][1]=='O' && S[1][2]=='O' ) yWin++; - if( S[2][0]=='O' && S[2][1]=='O' && S[2][2]=='O' ) yWin++; - - FOI(i, 0, 2){ - FOI(j ,0, 2){ - if( S[i][j] == 'O' ) - Y++; - if( S[i][j] == 'X' ) - X++; +int count(char c) +{ + int counter = 0; + for (int i = 0; i < 3; i++) + { + for (int y = 0; y < 3; y++) + { + if (field[i][y] == c) + { + counter++; } } - bool flag = true; - if( xWin > 0 && yWin > 0 ) - goto no; - if( xWin > 0 && Y + 1 == X ) - goto yes; - if( xWin > 0 && Y + 1 != X ) - goto no; - if( yWin > 0 && X == Y ) - goto yes; - if( yWin > 0 && X != Y ) - goto no; - if( X == Y || X == Y + 1 ) - goto yes; + } + return counter; +} + +bool find_win(char c) +{ + bool won = false; + for (int i = 0; i < 3; i++) + { + bool tmp = true; + for (int y = 0; y < 3; y++) + { + if (field[i][y] != c) + { + tmp = false; + break; + } + } + if (tmp) + { + won = true; + } + tmp = true; + for (int y = 0; y < 3; y++) + { + if (field[y][i] != c) + { + tmp = false; + break; + } + } + if (tmp) + { + won = true; + } + } + if (field[0][0] == c && field[1][1] == c && field[2][2] == c) + { + won = true; + } + if (field[0][2] == c && field[1][1] == c && field[2][0] == c) + { + won = true; + } + return won; +} + +int main() +{ + int n; + string input; + cin >> n; + while (n--) + { + for (int i = 0; i < 3; i++) + { + cin >> input; + for (int y = 0, sz = input.size(); y < sz; y++) + { + field[i][y] = input[y]; + } + } + int x = count('X'); + int o = count('O'); + bool valid = true; + if (x - o == 0 || x - o == 1) + { + bool x_won = find_win('X'); + bool o_won = find_win('O'); + if (x_won && o_won) + { + valid = false; + } + if (x_won && x == o) + { + valid = false; + } + if (o_won && x - o == 1) + { + valid = false; + } + } + else + { + valid = false; + } + if (valid) + { + cout << "yes" << endl; + } else - goto no; - -yes: cout << "yes"; - goto end; - -no: cout << "no"; - goto end; - -end: cout << endl; + { + cout << "no" << endl; + } } return 0; } diff --git a/10364.cpp b/10364.cpp new file mode 100644 index 0000000..3b59d23 --- /dev/null +++ b/10364.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +int M, ST; +struct ss +{ + int val; + char flag; +} V[21]; +int com(const void *a, const void *s) +{ + ss *x = (ss *)a; + ss *y = (ss *)s; + return (y->val - x->val); +} +int OK(int k) +{ + if (V[k - 1].flag == 0) + { + return 1; + } + if (V[k - 1].flag == 1 && (V[k].val == V[k - 1].val)) + { + return 0; + } + return 1; +} +int Recur(int index, int level, int sum, int av, int c, int st) +{ + int i; + sum += V[index].val; + if (sum > av) + { + return 0; + } + if (sum == av) + { + sum = 0; + c++; + if (c == 3) + { + return 1; + } + V[index].flag = 0; + for (i = 1; i < M; i++) + if (V[i].flag == 1) + { + break; + } + if (Recur(i, level + 1, sum, av, c, 1) == 1) + { + return 1; + } + } + V[index].flag = 0; + if (level == M - 1) + { + return 0; + } + for (i = st; i < M; i++) + { + if (V[i].flag == 1 && OK(i)) + { + if (Recur(i, level + 1, sum, av, c, i + 1) == 1) + { + return 1; + } + } + } + V[index].flag = 1; + return 0; +} +void Cal(int sum, int max) +{ + int av = 0, d, i = 0; + av = sum / 4; + d = Recur(0, 0, 0, av, 0, 0); + if (d == 1) + { + printf("yes\n"); + } + else + { + printf("no\n"); + } +} + +int main() +{ + int kase, i, sum, max; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &M); + max = 0; + sum = 0; + for (i = 0; i < M; i++) + { + V[i].flag = 1; + scanf("%d", &V[i].val); + sum += V[i].val; + if (V[i].val > max) + { + max = V[i].val; + } + } + if ((sum % 4) != 0) + { + printf("no\n"); + continue; + } + qsort(V, M, sizeof(V[0]), com); + Cal(sum, max); + } + return 0; +} diff --git a/10365.cpp b/10365.cpp index 9040c13..3c35536 100644 --- a/10365.cpp +++ b/10365.cpp @@ -1,29 +1,42 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int test; - scanf("%d",&test); - while(test--){ - int n,i,j,k,area=1000000000; - scanf("%d",&n); - for(i=1;i<=n;i++){ - for(j=1;j<=n;j++){ - if(i*j>n) - break; - for(k=1;k<=n;k++){ - if(i*j*k>n) - break; - if(i*j*k==n){ - int temp=2*(i*j+j*k+k*i); - area=area<=temp?area:temp; - } - } - } - } - printf("%d\n",area); - } - return 0; -} +#include + +using namespace std; + +int Volume, numCase, minimum, height, weight, length; + +int main() +{ + while (cin >> numCase) + { + while (numCase) + { + minimum = 10000000; + numCase--; + cin >> Volume; + for (height = 1; height <= Volume; height++) + { + for (length = 1; length <= Volume; length++) + { + int tmp; + tmp = height * length; + if (tmp > Volume) + { + break; + } + weight = Volume / tmp; + if (weight * height * length == Volume) + { + int area; + area = 2 * (Volume / height + Volume / length + Volume / weight); + if (area < minimum) + { + minimum = area; + } + } + } + } + cout << minimum << endl; + } + } + return 0; +} diff --git a/10366.cpp b/10366.cpp new file mode 100644 index 0000000..54b94fc --- /dev/null +++ b/10366.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +double htx[6000]; +#define ht (htx + 3000) + +int leftx, rightx, L, R, i, j, k, ii, jj; +double rheight, lheight, rneed, lneed, need, H, vol; + +int main() +{ + while (2 == scanf("%d%d", &leftx, &rightx) && leftx) + { + memset(htx, 0, sizeof(htx)); + vol = 0; + ht[leftx - 2] = ht[rightx + 2] = 0x7fffffff; + for (i = leftx; i <= rightx; i += 2) + { + scanf("%lf", &ht[i]); + } + L = -1; + R = 1; + while (ht[leftx] > ht[leftx + 1] && ht[rightx] > ht[rightx - 1]) + { + H = min(ht[L], ht[R]); + if (ht[L + 1] < H || ht[R - 1] < H) + { + for (i = L + 1; i <= R - 1; i += 2) + { + vol += 2 * (H - ht[i]); + ht[i] = H; + } + continue; + } + if (ht[R - 1] == ht[R]) + { + for (i = R - 1; ht[i] >= ht[i + 1]; i += 2) + ; + for (j = i; ht[i] == ht[j]; j -= 2) + ; + rheight = min(ht[i + 1], ht[0]); + rheight = min(rheight, ht[j + 1]); + rneed = (rheight - ht[i]) * (i - j); + } + else + { + rheight = rneed = 0; + } + if (ht[L + 1] == ht[L]) + { + for (ii = L - 1; ht[ii] >= ht[ii - 1]; ii -= 2) + ; + for (jj = ii; ht[ii] == ht[jj]; jj += 2) + ; + lheight = min(ht[ii - 1], ht[0]); + lheight = min(lheight, ht[jj - 1]); + lneed = (lheight - ht[ii]) * (jj - ii); + } + else + { + lneed = lheight = 0; + } + if (rneed) + for (k = j + 2; k <= i; k += 2) + { + if (lneed && lneed < rneed) + { + ht[k] += (rheight - ht[i]) * lneed / rneed; + } + else + { + ht[k] = rheight; + } + } + if (lneed) + for (k = jj - 2; k >= ii; k -= 2) + { + if (rneed && rneed < lneed) + { + ht[k] += (lheight - ht[ii]) * rneed / lneed; + } + else + { + ht[k] = lheight; + } + } + if (lneed && rneed) + { + vol += 2 * min(lneed, rneed); + } + else + { + vol += (lneed + rneed); + } + while (ht[R + 1] == ht[R - 1]) + { + R += 2; + } + while (ht[L - 1] == ht[L + 1]) + { + L -= 2; + } + } + printf("%0.0lf\n", vol); + } + return 0; +} diff --git a/10367.cpp b/10367.cpp new file mode 100644 index 0000000..2145b48 --- /dev/null +++ b/10367.cpp @@ -0,0 +1,346 @@ +#include + +using namespace std; + +int a[2], b[2], c[2]; +char str[200]; +bool fatalerror; + +void analyze(char *s, int p) +{ + int temp = 0; + int pos = 1, sign = 1; + a[p] = b[p] = c[p] = 0; + while (*s) + { + if (*s == ' ') + { + s++; + continue; + } + if (*s == '=') + { + sign = -1; + s++; + continue; + } + if (*s == '+') + { + s++; + while (*s == ' ') + { + s++; + } + if (*s == '-') + { + pos = -1; + s++; + } + if (*s == '+') + { + s++; + } + while (isdigit(*s)) + { + temp = temp * 10 + (*s++ - '0'); + } + temp *= pos; + if (temp == 0 && isalpha(*s)) + { + temp = pos; + } + if (*s == 'x') + { + s++; + a[p] += sign * temp; + } + else if (*s == 'y') + { + s++; + b[p] += sign * temp; + } + else + { + c[p] -= sign * temp; + } + } + else if (*s == '-') + { + s++; + while (*s == ' ') + { + s++; + } + if (*s == '-') + { + pos = -1; + s++; + } + if (*s == '+') + { + s++; + } + while (isdigit(*s)) + { + temp = temp * 10 + (*s++ - '0'); + } + temp *= pos; + if (temp == 0 && isalpha(*s)) + { + temp = pos; + } + if (*s == 'x') + { + s++; + a[p] -= sign * temp; + } + else if (*s == 'y') + { + s++; + b[p] -= sign * temp; + } + else + { + c[p] += sign * temp; + } + } + else + { + while (*s == ' ') + { + s++; + } + if (*s == '-') + { + pos = -1; + s++; + } + if (*s == '+') + { + s++; + } + while (isdigit(*s)) + { + temp = temp * 10 + (*s++ - '0'); + } + temp *= pos; + if (temp == 0 && isalpha(*s)) + { + temp = pos; + } + if (*s == 'x') + { + s++; + a[p] += sign * temp; + } + else if (*s == 'y') + { + s++; + b[p] += sign * temp; + } + else + { + c[p] -= sign * temp; + } + } + temp = 0; + pos = 1; + } + if (a[p] == 0 && b[p] == 0 && c[p] != 0) + { + fatalerror = true; + } + // printf("%d %d %d\n",a[p],b[p],c[p]); +} +int GCD(int a, int b) +{ + return (b == 0) ? a : GCD(b, a % b); +} +void solvex() +{ + bool pos = true; + int m = -999999, d, delta, deltax, gcd, tempm, tempd; + if (a[0] != 0 && b[0] == 0) + { + if (m == -999999) + { + d = c[0]; + m = a[0]; + } + else + { + tempm = a[0], tempd = c[0]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + } + if (a[1] != 0 && b[1] == 0) + { + if (m == -999999) + { + d = c[1]; + m = a[1]; + } + else + { + tempm = a[1], tempd = c[1]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + } + if (m == -999999) + { + m = a[0] * b[1] - a[1] * b[0]; + d = c[0] * b[1] - c[1] * b[0]; + } + else + { + tempm = a[0] * b[1] - a[1] * b[0]; + tempd = c[0] * b[1] - c[1] * b[0]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + if (m * d < 0) + { + pos = false; + } + m = (m > 0) ? m : -m; + d = (d > 0) ? d : -d; + if (m == 0) + { + puts("don't know"); + return; + } + gcd = GCD(m, d); + if (!pos) + { + printf("-"); + } + if (d % m == 0) + { + printf("%d\n", d / m); + } + else + { + printf("%d/%d\n", d / gcd, m / gcd); + } +} +void solvey() +{ + bool pos = true; + int m = -999999, d, gcd, tempm, tempd; + if (a[0] == 0 && b[0] != 0) + { + if (m == -999999) + { + d = c[0]; + m = b[0]; + } + else + { + tempm = b[0], tempd = c[0]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + } + if (a[1] == 0 && b[1] != 0) + { + if (m == -999999) + { + d = c[1]; + m = b[1]; + } + else + { + tempm = b[1]; + tempd = c[1]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + } + if (m == -999999) + { + m = a[0] * b[1] - a[1] * b[0]; + d = a[0] * c[1] - a[1] * c[0]; + } + else + { + tempm = a[0] * b[1] - a[1] * b[0]; + tempd = a[0] * c[1] - a[1] * c[0]; + if (tempm * d != tempd * m) + { + puts("don't know"); + return; + } + } + if (m * d < 0) + { + pos = false; + } + m = (m > 0) ? m : -m; + d = (d > 0) ? d : -d; + if (m == 0) + { + puts("don't know"); + return; + } + gcd = GCD(m, d); + if (!pos) + { + printf("-"); + } + if (d % m == 0) + { + printf("%d\n", d / m); + } + else + { + printf("%d/%d\n", d / gcd, m / gcd); + } +} + +int main() +{ + int times; + gets(str); + sscanf(str, "%d", ×); + for (int i = 0; i < times; i++) + { + fatalerror = false; + if (i) + { + gets(str); + puts(str); + } + for (int i = 0; i < 2; i++) + { + gets(str); + analyze(str, i); + } + if (fatalerror) + { + puts("don't know\ndon't know"); + } + else + { + solvex(); + solvey(); + } + } + return 0; +} diff --git a/10368.cpp b/10368.cpp new file mode 100644 index 0000000..00c72ce --- /dev/null +++ b/10368.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int i, j, k, a, b; + +int gcd(int a, int b) +{ + if (b > a) + { + return gcd(b, a); + } + if (b == 0) + { + return 0; + } + if (a / b == 1) + { + return !gcd(b, a % b); + } + return 1; +} + +int main() +{ + int x; + while (2 == scanf("%d%d", &a, &b) && a) + { + x = gcd(a, b); + if (x % 2) + { + printf("Stan wins\n"); + } + else + { + printf("Ollie wins\n"); + } + } + return 0; +} diff --git a/10369.cpp b/10369.cpp new file mode 100644 index 0000000..9c05101 --- /dev/null +++ b/10369.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +const double EPS = 1e-9; + +template +string toStr(const T &x) +{ + stringstream s; + s << x; + return s.str(); +} + +template +int toInt(const T &x) +{ + stringstream s; + s << x; + int r; + s >> r; + return r; +} + +struct Edge +{ + int a, b; + double w; + Edge(int A, int B, double W) + : a(A), b(B), w(W) + { + } + + bool operator<(const Edge &other) const + { + return w < other.w; + } +}; + +struct Point +{ + int x, y; + Point(int X, int Y) + : x(X), y(Y) + { + } +}; + +double dist(const Point &a, const Point &b) +{ + return hypot(a.x - b.x, a.y - b.y); +} + +const int MAXN = 505; + +int T, s, n, p[MAXN]; +vector edges; +vector points; + +void init() +{ + for (int i = 0; i <= n; i++) + { + p[i] = i; + } +} + +int find_(int u) +{ + if (p[u] == u) + { + return u; + } + return p[u] = find_(p[u]); +} + +void join(int u, int v) +{ + int a = find_(u), b = find_(v); + if (a == b) + { + return; + } + p[a] = b; +} + +double kruskal() +{ + init(); + sort(edges.begin(), edges.end()); + int times = 0; + double ans = 0.0f; + for (int i = 0; i < edges.size() && times < n - s; i++) + { + int u = edges[i].a, v = edges[i].b; + double w = edges[i].w; + if (find_(u) != find_(v)) + { + ans = w; + times++; + join(u, v); + } + } + return ans; +} + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &s,&n); + edges.clear(); + points.clear(); + for (int i = 0; i < n; i++) + { + int x, y; + scanf("%d%d", &x,&y); + points.push_back(Point(x, y)); + for (int j = 0; j < i; j++) + { + edges.push_back(Edge(j, i, dist(points[j], points[i]))); + } + } + printf("%.2lf\n", kruskal()); + } + return 0; +} diff --git a/10370.cpp b/10370.cpp index 4248829..6fc4ecf 100644 --- a/10370.cpp +++ b/10370.cpp @@ -1,33 +1,35 @@ -#include -#include - -using namespace std; - -int main() -{ - int C,N,i=0,j=0; - float marks[100],avg=0,sum=0,count=0,res=0; - cin>>C; - for(i=0;i>N; - for(j=0;j>marks[j]; - sum+=marks[j]; - } - avg=sum/N; - for(j=0;javg) - count++; - } - res=(count/N)*100; - printf("%.3f",res); - cout<<"%"< + +using namespace std; + +int main() +{ + int test_case, N, a[1001], i, j; + int sum; + float av, p; + scanf("%d", &test_case); + for (i = 0; i < test_case; i++) + { + sum = 0; + p = 0; + scanf("%d", &N); + for (j = 0; j < N; j++) + { + scanf("%d", &a[j]); + } + for (j = 0; j < N; j++) + { + sum += a[j]; + } + av = sum / N; + for (j = 0; j < N; j++) + { + if (a[j] > av) + { + p++; + } + } + printf("%.3f%\n", (p * 100) / N); + } + return 0; +} diff --git a/10371.cpp b/10371.cpp new file mode 100644 index 0000000..2431989 --- /dev/null +++ b/10371.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +map> timezones; + +void normalize_time(int &h, int &m) +{ + if (h < 0) + { + h += 24; + } + else if (h > 23) + { + h -= 24; + } + if (m < 0) + { + m += 60; + h -= 1; + } + else if (m > 59) + { + m -= 60; + h += 1; + } + if (h < 0) + { + h += 24; + } + else if (h > 23) + { + h -= 24; + } +} + +void to_utc(int &h, int &m, string &zone) +{ + h -= timezones[zone].first; + m -= timezones[zone].second; + normalize_time(h, m); +} + +void to_local(int &h, int &m, string &zone) +{ + h += timezones[zone].first; + m += timezones[zone].second; + normalize_time(h, m); +} + +int main() +{ + int cases; + char separator; + string current_time, time_notation, zone1, zone2; + int h, m; + timezones["UTC"] = make_pair(0, 0); + timezones["GMT"] = make_pair(0, 0); + timezones["BST"] = make_pair(1, 0); + timezones["IST"] = make_pair(1, 0); + timezones["WET"] = make_pair(0, 0); + timezones["WEST"] = make_pair(1, 0); + timezones["CET"] = make_pair(1, 0); + timezones["CEST"] = make_pair(2, 0); + timezones["EET"] = make_pair(2, 0); + timezones["EEST"] = make_pair(3, 0); + timezones["MSK"] = make_pair(3, 0); + timezones["MSD"] = make_pair(4, 0); + timezones["AST"] = make_pair(-4, 0); + timezones["ADT"] = make_pair(-3, 0); + timezones["NST"] = make_pair(-3, -30); + timezones["NDT"] = make_pair(-2, -30); + timezones["EST"] = make_pair(-5, 0); + timezones["EDT"] = make_pair(-4, 0); + timezones["CST"] = make_pair(-6, 0); + timezones["CDT"] = make_pair(-5, 0); + timezones["MST"] = make_pair(-7, 0); + timezones["MDT"] = make_pair(-6, 0); + timezones["PST"] = make_pair(-8, 0); + timezones["PDT"] = make_pair(-7, 0); + timezones["HST"] = make_pair(-10, 0); + timezones["AKST"] = make_pair(-9, 0); + timezones["AKDT"] = make_pair(-8, 0); + timezones["AEST"] = make_pair(10, 0); + timezones["AEDT"] = make_pair(11, 0); + timezones["ACST"] = make_pair(9, 30); + timezones["ACDT"] = make_pair(10, 30); + timezones["AWST"] = make_pair(8, 0); + cin >> cases; + while (cases--) + { + cin >> current_time; + if (current_time != "noon" && current_time != "midnight") + { + cin >> time_notation; + } + cin >> zone1 >> zone2; + if (current_time == "noon") + { + h = 12, m = 0; + } + else if (current_time == "midnight") + { + h = m = 0; + } + else + { + istringstream ct(current_time); + ct >> h; + ct >> separator; + ct >> m; + if (time_notation[0] == 'p' && h < 12) + { + h += 12; + if (h == 24) + { + h = 0; + } + } + else if (time_notation[0] == 'a' && h == 12) + { + h = 0; + } + } + to_utc(h, m, zone1); + to_local(h, m, zone2); + if (h == 0 && m == 0) + { + cout << "midnight" << endl; + } + else if (h == 12 && m == 0) + { + cout << "noon" << endl; + } + else + { + if (h == 0) + { + cout << "12:" << (m > 9 ? "" : "0") << m << " a.m." << endl; + } + else if (h == 12) + { + cout << "12:" << (m > 9 ? "" : "0") << m << " p.m." << endl; + } + else if (h > 12) + { + cout << h - 12 << ":" << (m > 9 ? "" : "0") << m << " p.m." << endl; + } + else + { + cout << h << ":" << (m > 9 ? "" : "0") << m << " a.m." << endl; + } + } + } + return 0; +} diff --git a/10372.cpp b/10372.cpp new file mode 100644 index 0000000..648c17d --- /dev/null +++ b/10372.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define eps 1e-8 + +int check(int n, double h[], double d[], double theta, double v) +{ + double vx = v * cos(theta), vy = v * sin(theta); + double t, x = 0, hh; + int i; + for (i = 0; i < n; i++) + { + t = x / vx; + hh = vy * t - 0.5 * 9.8 * t * t; + if (hh < h[i] - eps) + return 0; + t = (x + d[i]) / vx; + hh = vy * t - 0.5 * 9.8 * t * t; + if (hh < h[i] - eps) + return 0; + x += d[i]; + } + return 1; +} + +int main() +{ + int n, i; + double h[105], d[105]; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + scanf("%lf %lf", &h[i], &d[i]); + double X = 0; + for (i = 0; i < n; i++) + X += d[i]; + double l = 0, r = acos(-1) / 2, mid;// bin srch + double theta, v; + while (fabs(l - r) > 1e-5) + { + mid = (l + r) / 2; + theta = mid, v = sqrt(X * 9.8 / sin(2 * theta)); + if (check(n, h, d, theta, v)) + r = mid; + else + l = mid; + } + printf("%.2lf %.2lf\n", theta / acos(-1) * 180, v); + } + return 0; +} diff --git a/10373.cpp b/10373.cpp new file mode 100644 index 0000000..dc0cb84 --- /dev/null +++ b/10373.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +const int MAXM = 20, MAXN = 200, MAXSUM = MAXM * 1000; + +unsigned int dp[MAXM + 1][MAXSUM + 1]; +int w[MAXN], p[MAXN]; + +void check(bool b) +{ + while (!b) + ; +} + +int main() +{ + int runs; + scanf("%d", &runs); + for (int run = 0; run < runs; ++run) + { + if (run) + { + printf("\n"); + } + int n, sum = 0; + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + scanf("%d %d", &w[i], &p[i]); + check(1 <= w[i] && w[i] <= 999); + sum += w[i]; + } + sum = std::min(sum, MAXSUM); + for (int i = 0; i <= MAXM; ++i) + for (int j = 0; j <= sum; ++j) + { + dp[i][j] = INT_MAX; + } + dp[0][0] = 0; + dp[1][w[0]] = p[0]; + for (int k = 1; k < n; ++k) + { + //en este momento, dp[i][j] = minimo precio en que puedo escoger i ladrillos entre los ladrillos [0..k] + //tal que la suma neta de cobre sea j. + for (int i = MAXM; i >= 1; --i)//i va descendiendo para no ir a usar el mismo ladrillo dos veces. + { + for (int j = 0; j <= sum; ++j) + { + if (j - w[k] >= 0) + { + dp[i][j] = std::min(dp[i][j], dp[i - 1][j - w[k]] + p[k]); + } + } + } + } + int c; + scanf("%d", &c); + while (c--) + { + int m, cmin, cmax; + scanf("%d %d %d", &m, &cmin, &cmax); + check(0 <= m && m <= 20); + check(1 <= cmin && cmin <= 999); + check(1 <= cmax && cmax <= 999); + unsigned int answer = INT_MAX; + for (int j = m * cmin; j <= m * cmax && j <= sum; ++j) + { + //if (answer > dp[m][j]) printf("better answer: dp[%d][%d] = %u\n", m, j, dp[m][j]); + answer = std::min(answer, dp[m][j]); + } + if (answer < INT_MAX) + { + printf("%u\n", answer); + } + else + { + printf("impossible\n"); + } + } + } + return 0; +} diff --git a/10374.cpp b/10374.cpp index 63b6408..20bac92 100644 --- a/10374.cpp +++ b/10374.cpp @@ -1,78 +1,86 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define MAXN 22 -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--){ - map Party; - map::iterator itp; - map Vote; - map::iterator itv; - - int N, i, j; - string str; - cin >> N; - getline(cin, str); - while (N--){ - string A, B; - getline(cin, A); - getline(cin, B); - Party[A] = B; - Vote[A] = 0; - } - cin >> N; - getline(cin, str); - int maxm = 0; - while (N--){ - getline(cin, str); - Vote[str] += 1; - maxm = max(maxm, Vote[str]); +int N, M, C[MAXN]; +struct ss +{ + char candidate[82]; + char party[82]; + int cand_number; +} V[MAXN], Temp; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return strcmp(x->candidate, y->candidate); +} +int Cal() +{ + int i, j, max = 0, c = 0; + ss *p; + qsort(V, N, sizeof(V[0]), com); + for (j = 0; j < N; j++) + { + V[j].cand_number = j; + C[j] = 0; + } + for (i = 0; i < M; i++) + { + gets(Temp.candidate); + p = (ss *)bsearch(&Temp, V, N, sizeof(V[0]), com); + if (p) + { + C[p->cand_number]++; + if (C[p->cand_number] > max) + { + max = C[p->cand_number]; + } } - int cnt = 0; - for (itv = Vote.begin(); itv != Vote.end(); itv++){ - if (maxm == (*itv).second){ - ++cnt; - str = Party[(*itv).first]; + } + for (i = 0; i < N; i++) + { + if (C[i] == max) + { + j = i; + c++; + if (c > 1) + { + printf("tie\n"); + return 0; } } - if (cnt > 1) - cout << "tie" << endl; - else - cout << str << endl; - - if (T) - cout << endl; } + printf("%s\n", V[j].party); return 0; } +int main() +{ + char input[100]; + int kase, i; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + gets(input); + sscanf(input, "%d", &N); + for (i = 0; i < N; i++) + { + gets(V[i].candidate); + gets(V[i].party); + } + gets(input); + sscanf(input, "%d", &M); + Cal(); + if (kase) + { + printf("\n"); + gets(input); + } + } + return 0; +} diff --git a/10375.cpp b/10375.cpp new file mode 100644 index 0000000..350a02d --- /dev/null +++ b/10375.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define MAXN 10000 + +char P[MAXN + 2]; +int Den[MAXN + 2], Num[MAXN + 2]; +void prime_table() +{ + int i, j; + for (i = 2; i <= MAXN; i++) + { + P[i] = 1; + } + for (i = 2; i <= sqrt(MAXN);) + { + for (j = i + i; j <= MAXN; j += i) + { + P[j] = 0; + } + for (++i; !P[i]; i++) + ; + } +} +void Fact(int *a, int n) +{ + long i, j; + for (i = 2; i <= n; i++) + { + if (P[i]) + { + for (j = i; j <= n; j *= i) + { + a[i] += n / j; + } + } + } +} + +int main() +{ + int p, q, r, s, i; + prime_table(); + while (scanf("%d%d%d%d", &p, &q, &r, &s) == 4) + { + for (i = 0; i <= MAXN; i++) + { + Den[i] = 0; + Num[i] = 0; + } + Fact(Num, p); + Fact(Num, s); + Fact(Num, r - s); + Fact(Den, r); + Fact(Den, q); + Fact(Den, p - q); + double j = 1; + for (i = 2; i <= MAXN; i++) + { + if (P[i]) + { + Num[i] -= Den[i]; + while (Num[i] > 0) + { + j *= i; + Num[i]--; + } + while (Num[i] < 0) + { + j /= i; + Num[i]++; + } + } + } + printf("%.5lf\n", j); + } + return 0; +} diff --git a/10377.cpp b/10377.cpp new file mode 100644 index 0000000..94cc8c0 --- /dev/null +++ b/10377.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +char dir = 'N'; +string coridor[100]; +int test, row, col, i, _row, _col; + +void forward() +{ + if (dir == 'E') + { + if (coridor[_row][_col + 1] != '*') + { + ++_col; + } + } + else if (dir == 'W') + { + if (coridor[_row][_col - 1] != '*') + { + --_col; + } + } + else if (dir == 'S') + { + if (coridor[_row + 1][_col] != '*') + { + ++_row; + } + } + else if (dir == 'N') + { + if (coridor[_row - 1][_col] != '*') + { + --_row; + } + } +} + +void right() +{ + switch (dir) + { + case 'N': + dir = 'E'; + break; + case 'E': + dir = 'S'; + break; + case 'S': + dir = 'W'; + break; + case 'W': + dir = 'N'; + break; + } +} + +void left() +{ + switch (dir) + { + case 'N': + dir = 'W'; + break; + case 'W': + dir = 'S'; + break; + case 'S': + dir = 'E'; + break; + case 'E': + dir = 'N'; + break; + } +} + +int main() +{ + char c, str[200]; + cin >> test; + bool flag = false; + while (test--) + { + cin >> row >> col; + cin.getline(str, sizeof(str));// eat blank line + for (i = 0; i < row; ++i) + { + cin.getline(str, sizeof(str)); + coridor[i] = str; + } + cin >> _row >> _col; + dir = 'N'; + --_row; + --_col; + while ((c = cin.get()) != 'Q') + { + switch (c) + { + case 'R': + right(); + break; + case 'L': + left(); + break; + case 'F': + forward(); + break; + } + } + if (flag) + { + cout << endl; + } + flag = true; + cout << _row + 1 << " " << _col + 1 << " " << dir << endl; + } + return 0; +} diff --git a/10378.cpp b/10378.cpp new file mode 100644 index 0000000..eca1f15 --- /dev/null +++ b/10378.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES // expose const, e.g. M_PI +#define EPS std::numeric_limits::epsilon() + +struct my_compl +{ + double real, imag; +}; +double a, b, n; +my_compl roots[101]; +double fix(double v) +{ + if (v > 0) + { + return v; + } + if (v > -0.0005) + { + return 0.0; + } + return v; +} +double sqr(double arg) +{ + return arg * arg; +} +int sort_func(const void *a, const void *b) +{ + my_compl tmpa, tmpb; + tmpa = *(my_compl *)a; + tmpb = *(my_compl *)b; + double tmp; + if (fabs(tmpa.real - tmpb.real) < 1e-9) + { + tmp = (tmpb.imag - tmpa.imag); + } + else + { + tmp = (tmpb.real - tmpa.real); + } + if (fabs(tmp) < EPS) + { + return tmp; + } + else if (tmp < 0) + { + return (-1); + } + else + { + return 1; + } +} +void outdata() +{ + int ansnum; + ansnum = n; + for (int i = 0; i < ansnum; i++) + { + if ((roots[i].real <= 0) && (roots[i].real > -0.0005)) + { + roots[i].real = +0.0; + } + if ((roots[i].imag <= 0) && (roots[i].imag > -0.0005)) + { + roots[i].imag = +0.0; + } + printf("%.3lf", fix(roots[i].real)); + if (roots[i].imag >= 0) + { + printf("+"); + } + printf("%.3lfi\n", fix(roots[i].imag)); + } +} +int main() +{ + int curcase = 0; + int in_a, in_b, in_n; + while (scanf("%d%di %d", &in_a, &in_b, &in_n) == 3) + { + curcase++; + a = in_a; + b = in_b; + n = in_n; + double r, phi; + r = sqrt(sqr(a) + sqr(b)); + if ((a == 0) && (b == 0)) + { + phi = M_PI / 2; + } + else + { + phi = atan2(b, a); + } + double r_mod; + r_mod = pow(r, 1 / n); + printf("Case %d:\n", curcase); + double cur_phi, new_a, new_b; + for (int i = 0; i < in_n; i++) + { + cur_phi = (phi + 2 * M_PI * i) / n; + new_a = r_mod * cos(cur_phi); + new_b = r_mod * sin(cur_phi); + roots[i].real = new_a; + roots[i].imag = new_b; + } + qsort((void *)roots, in_n, sizeof(roots[0]), sort_func); + outdata(); + printf("\n"); + } + return 0; +} diff --git a/10379.cpp b/10379.cpp new file mode 100644 index 0000000..f0074a3 --- /dev/null +++ b/10379.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +double cons[101], time_[101], race[101], fuel[101]; +int since[101], stops[101], nstops[101]; + +int main() +{ + int laps, i, j, k, l; + double lapt, laptinc, lapc, lapcinc, pt, ptinc; + int nstops, lastop; + while (7 == scanf("%d %lf %lf %lf %lf %lf %lf", + &laps, &lapt, &laptinc, &lapc, &lapcinc, &pt, &ptinc)) + { + fprintf(stdout, "%d %.3lf %.3lf %.3lf %.3lf %.3lf %.3lf\n", + laps, lapt, laptinc, lapc, lapcinc, pt, ptinc); + for (i = 0; i < laps; i++) + { + double x = (cons[i] * lapcinc + lapc) / (1 - lapcinc); + cons[i + 1] = cons[i] + x; + time_[i + 1] = time_[i] + lapt + laptinc * cons[i + 1]; + } + for (i = 0; i <= laps; i++) + { + race[i] = time_[i]; + since[i] = i; + } + for (i = 1; i < laps; i++) + { + for (j = 1; j <= laps - i; j++) + { + double x = race[i] + time_[j] + pt + ptinc * cons[j]; + if (x < race[i + j]) + { + race[i + j] = x; + since[i + j] = j; + } + } + } + nstops = 0; + lastop = laps; + for (;;) + { + if (lastop == 0) + { + break; + } + stops[nstops] = lastop - since[lastop]; + fuel[nstops] = cons[since[lastop]]; + lastop -= since[lastop]; + if (lastop == 0) + { + break; + } + nstops++; + } + printf("%.3lf %.3lf %d\n", race[laps], fuel[nstops], nstops); + for (i = nstops - 1; i >= 0; i--) + { + printf("%d %.3lf\n", stops[i], fuel[i]); + } + } +} diff --git a/1038.cpp b/1038.cpp new file mode 100644 index 0000000..bf5005c --- /dev/null +++ b/1038.cpp @@ -0,0 +1,339 @@ +#include + +using namespace std; + +const int MAXN = 60; +const double eps = 1e-5; + +double uy, dy, lx, rx, ratio_, px1, py1, px2, py2, ax1, ay1, ax2, ay2, px[MAXN * MAXN], py[MAXN * MAXN]; +int n, m; + +int sgn(double x) +{ + if (fabs(x) <= eps) + { + return 0; + } + if (x > 0) + { + return 1; + } + else + { + return -1; + } +} + +struct segment +{ + double x1, y1, x2, y2; + int dir; + void init() + { + scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); + if (sgn(y1 - y2) == 0) + { + dir = 0; + if (sgn(x1 - x2) > 0) + { + swap(x1, x2); + } + } + else + { + dir = 1; + if (sgn(y1 - y2) > 0) + { + swap(y1, y2); + } + } + } + bool operator==(const segment &a) const + { + return dir == a.dir && sgn(x1 - a.x1) == 0 && sgn(x2 - a.x2) == 0 && sgn(y1 - a.y1) == 0 && sgn(y2 - a.y2) == 0; + } +} seg1[MAXN], seg2[MAXN], seg3[MAXN], seg4[MAXN]; + +double cross(double x1, double y1, double x2, double y2, double x3, double y3) +{ + return (x1 - x2) * (y2 - y3) - (y1 - y2) * (x2 - x3); +} + +double move(double pre, double o, double delta, double ratio_) +{ + return o + (pre - o) * ratio_ + delta; +} + +bool cro(segment a, segment b) +{ + if (a.dir) + { + swap(a, b); + } + if (sgn(b.y1 - a.y1) >= 0 || sgn(a.y1 - b.y2) >= 0) + { + return false; + } + if (sgn(a.x1 - b.x1) >= 0 || sgn(b.x1 - a.x2) >= 0) + { + return false; + } + return true; +} + +bool cmp(const segment &a, const segment &b) +{ + if (a.dir != b.dir) + { + return a.dir < b.dir; + } + if (sgn(a.x1 - b.x1) != 0) + { + return a.x1 < b.x1; + } + if (sgn(a.x2 - b.x2) != 0) + { + return a.x2 < b.x2; + } + if (sgn(a.y1 - b.y1) != 0) + { + return a.y1 < b.y1; + } + if (sgn(a.y2 - b.y2) != 0) + { + return a.y2 < b.y2; + } + return false; +} + +bool check() +{ + if (sgn(ratio_ - 1) >= 0) + return false; + double l, r, u, d; + double dex = ax1 - px1, dey = ay1 - py1; + l = move(lx, px1, dex, ratio_); + r = move(rx, px1, dex, ratio_); + u = move(uy, py1, dey, ratio_); + d = move(dy, py1, dey, ratio_); + int cnt = 0; + for (int a = 1; a <= m; a++) + { + if (seg2[a].dir == 0) + { + if (sgn(seg2[a].y1 - d) >= 0 && sgn(u - seg2[a].y1) >= 0) + { + if (sgn(l - seg2[a].x2) >= 0 || sgn(seg2[a].x1 - r) >= 0) + ; + else + { + cnt++; + seg4[cnt].dir = 0; + seg4[cnt].y1 = seg4[cnt].y2 = seg2[a].y1; + seg4[cnt].x1 = max(l, seg2[a].x1); + seg4[cnt].x2 = min(r, seg2[a].x2); + } + } + } + else + { + if (sgn(seg2[a].x1 - l) >= 0 && sgn(r - seg2[a].x2) >= 0) + { + if (sgn(d - seg2[a].y2) >= 0 || sgn(seg2[a].y1 - u) >= 0) + ; + else + { + cnt++; + seg4[cnt].dir = 1; + seg4[cnt].x1 = seg4[cnt].x2 = seg2[a].x1; + seg4[cnt].y1 = max(d, seg2[a].y1); + seg4[cnt].y2 = min(u, seg2[a].y2); + } + } + } + } + if (cnt != n) + { + return false; + } + for (int a = 1; a <= n; a++) + { + seg3[a].x1 = move(seg1[a].x1, px1, dex, ratio_); + seg3[a].x2 = move(seg1[a].x2, px1, dex, ratio_); + seg3[a].y1 = move(seg1[a].y1, py1, dey, ratio_); + seg3[a].y2 = move(seg1[a].y2, py1, dey, ratio_); + seg3[a].dir = seg1[a].dir; + if (sgn(fabs(seg3[a].x1 - seg3[a].x2) + fabs(seg3[a].y1 - seg3[a].y2) - 0.05) < 0) + { + return false; + } + } + sort(seg3 + 1, seg3 + n + 1, cmp); + sort(seg4 + 1, seg4 + n + 1, cmp); + for (int a = 1; a <= n; a++) + { + if (seg3[a] == seg4[a]) + ; + else + { + return false; + } + } + return true; +} + +void dfs(int now) +{ + dfs(now + 1); +} + +int main() +{ + int test = 0; + while (~scanf("%d%d", &n, &m)) + { + if (!n && !m) + { + break; + } + test++; + printf("Case %d: ", test); + rx = uy = -1e+6; + lx = dy = 1e+6; + for (int a = 1; a <= n; a++) + { + seg1[a].init(); + uy = max(uy, max(seg1[a].y1, seg1[a].y2)); + dy = min(dy, min(seg1[a].y1, seg1[a].y2)); + rx = max(rx, max(seg1[a].x1, seg1[a].x2)); + lx = min(lx, min(seg1[a].x1, seg1[a].x2)); + } + for (int a = 1; a <= m; a++) + { + seg2[a].init(); + } + bool chkpt = false; + for (int a = 1; a <= n; a++) + { + if (chkpt) + { + break; + } + for (int b = a + 1; b <= n; b++) + { + if (seg1[a].dir != seg1[b].dir && cro(seg1[a], seg1[b])) + { + chkpt = true; + if (seg1[a].dir) + { + px1 = seg1[a].x1, py1 = seg1[b].y1; + } + else + { + px1 = seg1[b].x1, py1 = seg1[a].y1; + } + break; + } + } + } + int cnt = 0; + for (int a = 1; a <= m; a++) + { + for (int b = a + 1; b <= m; b++) + { + if (seg2[a].dir != seg2[b].dir && cro(seg2[a], seg2[b])) + { + cnt++; + if (seg2[a].dir) + { + px[cnt] = seg2[a].x1, py[cnt] = seg2[b].y1; + } + else + { + px[cnt] = seg2[b].x1, py[cnt] = seg2[a].y1; + } + } + } + } + bool find = false; + if (chkpt) + { + for (int a = 1; a <= n && !find; a++) + { + for (int b = 1; b <= m && !find; b++) + { + if (seg1[a].dir == seg2[b].dir) + { + px2 = seg1[a].x1; + py2 = seg1[a].y1; + ax2 = seg2[b].x1; + ay2 = seg2[b].y1; + double dx = px2 - px1, dy = py2 - py1; + for (int c = 1; c <= cnt && !find; c++) + { + double dx1 = ax2 - px[c], dy1 = ay2 - py[c]; + if (sgn(dx * dy1 - dx1 * dy) == 0) + { + ax1 = px[c]; + ay1 = py[c]; + if (sgn(dx) != 0) + { + ratio_ = dx1 / dx; + } + else + { + ratio_ = dy1 / dy; + } + if (check()) + { + find = true; + } + } + } + px2 = seg1[a].x2; + py2 = seg1[a].y2; + ax2 = seg2[b].x2; + ay2 = seg2[b].y2; + dx = px2 - px1, dy = py2 - py1; + for (int c = 1; c <= cnt && !find; c++) + { + double dx1 = ax2 - px[c], dy1 = ay2 - py[c]; + if (sgn(dx * dy1 - dx1 * dy) == 0) + { + ax1 = px[c]; + ay1 = py[c]; + if (sgn(dx) != 0) + { + ratio_ = dx1 / dx; + } + else + { + ratio_ = dy1 / dy; + } + if (check()) + { + find = true; + } + } + } + } + } + } + } + else + { + dfs(1); + } + if (find) + { + printf("valid puzzle\n"); + } + else + { + printf("impossible\n"); + } + printf("\n"); + } + + return 0; +} diff --git a/10380.cpp b/10380.cpp new file mode 100644 index 0000000..98fb67a --- /dev/null +++ b/10380.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +char buf[1001]; +int n, m, slim, score[55], used[55]; + +struct data {int first, second;}; +struct data mark[55][55], map_[55][55]; + +int fill(int now) +{ + int i; + if (score[now] < slim) + { + return 1; + } + used[now] = 1; + for (i = 1; i <= n; i++) + { + if (now != i && i != m && !used[i]) + { + if (mark[now][i].first && map_[now][i].first) + { + used[i] = 1; + if (fill(i)) + { + map_[now][i].first = 0; + map_[i][now].first = 1; + score[now]--; + score[i]++; + return 1; + } + continue; + } + if (mark[now][i].second && map_[now][i].second) + { + used[i] = 1; + if (fill(i)) + { + map_[now][i].second = 0; + map_[i][now].second = 1; + score[now]--; + score[i]++; + return 1; + } + } + } + } + return 0; +} + +int main() +{ + int count, i, j, left, right, center, ans; + scanf("%d", &count); + while (count--) + { + memset(mark, 0, sizeof(mark)); + memset(score, 0, sizeof(score)); + scanf("%d%d", &n, &m); + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + scanf("%s", buf); + score[i] += (buf[0] == '1') + (buf[1] == '1'); + if (buf[0] != '-') + { + map_[i][j].first = buf[0] - '0'; + } + if (buf[1] != '-') + { + map_[i][j].second = buf[1] - '0'; + } + if (i != j && buf[0] == '-') + { + mark[i][j].first = 1; + if (i == m || (i > j && j != m)) + { + score[i]++; + map_[i][j].first = 1; + map_[j][i].first = 0; + } + } + if (i != j && buf[1] == '-') + { + mark[i][j].second = 1; + if (i == m || (i > j && j != m)) + { + score[i]++; + map_[i][j].second = 1; + map_[j][i].second = 0; + } + } + } + } + + for (left = 0, right = score[m], ans = -1; left <= right;) + { + center = left + right; + center /= 2; + slim = center; + for (i = 1; i <= n; i++) + { + if (i != m) + { + for (; score[i] > slim;) + { + memset(used, 0, sizeof(used)); + if (!fill(i)) + { + break; + } + } + if (score[i] > slim) + { + break; + } + } + } + if (i > n) + { + ans = center; + right = center - 1; + } + else + { + left = center + 1; + } + } + if (ans == -1) + { + printf("Player %d can't win!\n", m); + } + else + { + printf("Player %d can win with %d point(s).\n\n", m, score[m] - ans); + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + if (i == j) + { + printf("-- "); + } + else + { + printf("%d%d ", map_[i][j].first, map_[i][j].second); + } + } + printf(": %d\n", score[i]); + } + } + if (count) + { + printf("\n"); + } + } + return 0; +} diff --git a/10381.cpp b/10381.cpp new file mode 100644 index 0000000..f10ab39 --- /dev/null +++ b/10381.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef pair pii; + +const int N = 50; +const int inf = (1 << 20); +const int dy[] = {0, 0, 1, -1}; +const int dx[] = {1, -1, 0, 0}; + +char m[N][N]; +int cost[N][N]; +int ans[N][N]; + +bool isout(int r, int c, int y, int x) +{ + return y == -1 || x == -1 || y == r || x == c; +} + +int bfs(int r, int c, int sy, int sx, int gy, int gx) +{ + rep(i, r) rep(j, c) cost[i][j] = inf; + cost[sy][sx] = 0; + queue Q; + Q.push(sy * c + sx); + while (!Q.empty()) + { + int y = Q.front() / c, x = Q.front() % c; + Q.pop(); + if (y == gy && x == gx) + { + return cost[y][x]; + } + rep(i, 4) + { + int ney = y + dy[i], nex = x + dx[i]; + if (isout(r, c, ney, nex) || m[ney][nex] == '#' || cost[ney][nex] != inf) + { + continue; + } + cost[ney][nex] = cost[y][x] + 1; + Q.push(ney * c + nex); + } + } + return -1; +} + +int solve(int r, int c) +{ + int sy, sx, gy, gx; + rep(i, r) + { + rep(j, c) if (m[i][j] == 'E') + { + gy = i, gx = j; + } + else if (m[i][j] == 'X') + { + sy = i, sx = j; + } + } + bfs(r, c, sy, sx, -1, -1); + priority_queue, greater> Q; + rep(i, r) rep(j, c) ans[i][j] = inf; + rep(i, 4) + { + int ney = sy + dy[i], nex = sx + dx[i]; + if (isout(r, c, ney, nex) || m[ney][nex] == '#') + { + continue; + } + Q.push(make_pair(cost[sy][sx] + 1, ney * c + nex)); + } + while (!Q.empty()) + { + int nowc = Q.top().first, y = Q.top().second / c, x = Q.top().second % c; + Q.pop(); + if (ans[y][x] != inf) + { + continue; + } + ans[y][x] = nowc; + rep(i, 4) + { + int ney = y + dy[i], nex = x + dx[i]; + if (isout(r, c, ney, nex) || m[ney][nex] == '#' || ans[ney][nex] == inf) + { + continue; + } + char tmp = m[ney][nex]; + m[ney][nex] = '#'; + ans[y][x] = max(ans[y][x], bfs(r, c, y, x, sy, sx)); + m[ney][nex] = tmp; + } + rep(i, 4) + { + int ney = y + dy[i], nex = x + dx[i]; + if (isout(r, c, ney, nex) || m[ney][nex] == '#') + { + continue; + } + Q.push(make_pair(ans[y][x] + 1, ney * c + nex)); + } + } + return ans[gy][gx]; +} + +int main() +{ + int te; + cin >> te; + while (te--) + { + int r, c; + cin >> r >> c; + rep(i, r) cin >> m[i]; + cout << solve(r, c) << endl; + } + return 0; +} diff --git a/10382.cpp b/10382.cpp new file mode 100644 index 0000000..c7f4ac2 --- /dev/null +++ b/10382.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define MAX_SIZE 10000 + +struct Sprinkler +{ + double L, R; + bool operator<(const Sprinkler &s) const + { + return L < s.L; + } +} sprinklers[MAX_SIZE + 1]; + +int main() +{ + double w; + int n, l; + while (scanf("%d%d%lf", &n, &l, &w) == 3) + { + w /= 2.0; + int m = 0; + for (int i = 0; i < n; ++i) + { + int p, r; + scanf("%d%d", &p, &r); + if (r > w) + { + double half_len = sqrt((double)r * r - w * w); + sprinklers[m].L = (double)p - half_len; + sprinklers[m++].R = (double)p + half_len; + } + } + sort(sprinklers, sprinklers + m); + + double rightmost = 0.0f; + int cnt = 0; + int i, j; + for (i = 0; i < m; i = j) + { + if (sprinklers[i].L > rightmost) + { + break; + } + for (j = i + 1; j < m && sprinklers[j].L <= rightmost; ++j) + { + if (sprinklers[j].R > sprinklers[i].R) + { + i = j; + } + } + ++cnt; + rightmost = sprinklers[i].R; + if (rightmost >= l) + { + break; + } + } + + if (rightmost >= l) + { + printf("%d\n", cnt); + } + else + { + printf("-1\n", cnt); + } + } + return 0; +} diff --git a/10384.cpp b/10384.cpp new file mode 100644 index 0000000..c6a7567 --- /dev/null +++ b/10384.cpp @@ -0,0 +1,412 @@ +#include + +using namespace std; + +#define maxn 26 + +char Bit[16][5]; +int M[5][7], xxx; +struct ss +{ + string A; + int c, r; +}; +struct xx +{ + string M; + string Path; + int r, c; + int level; +}; +bool operator<(const ss &p, const ss &q) +{ + if (p.A.compare(q.A) != 0) + { + return true; + } + if (p.r != q.r || p.c != q.c) + { + return true; + } + return false; +} +map Map; +queue Q; +void Setup(int n) +{ + int ind = 1, d, k = n; + while (n) + { + d = n % 2; + Bit[k][ind++] = d; + n /= 2; + } +} +void Ini() +{ + int i; + for (i = 1; i <= 15; i++) + Setup(i); +} +void Ext(string st) +{ + int i, j, ind = 0, d; + for (i = 1; i <= 4; i++) + for (j = 1; j <= 6; j++) + { + d = st[ind++] - 'A'; + M[i][j] = d; + } +} +int GoLeft(int r, int c, string path, string st, int lv) +{ + int i, j, d, a, k; + d = M[r][c]; + ss t; + xx y; + if (lv == 1) + return 0; + if (c == 1) + { + if (Bit[d][1] == 0) + { + path += "W"; + cout << path.c_str() << endl; + return 1; + } + return 0; + } + if (Bit[d][1] == 1) + { + a = M[r][c - 1]; + if (Bit[a][1] == 1) + return 0; + t.c = c - 1; + t.r = r; + t.A = st; + i = (r - 1) * 6 + (c - 2); + j = (r - 1) * 6 + (c - 1); + if (c > 2) + { + k = (r - 1) * 6 + (c - 3); + t.A[k] += 4; + } + t.A[j] -= 1; + t.A[i] -= 3; + if (Map[t]) + return 0; + Map[t] = true; + xxx++; + y.r = t.r; + y.c = t.c; + y.M = t.A; + y.Path = path + "W"; + y.level = 2; + Q.push(y); + return 0; + } + if (Bit[d][1] == 0) + { + t.r = r; + t.c = c - 1; + t.A = st; + if (Map[t]) + return 0; + Map[t] = true; + xxx++; + y.c = c - 1; + y.r = r; + y.M = st; + y.Path = path + "W"; + y.level = 2; + Q.push(y); + return 0; + } + return 0; +} +int GoRight(int r, int c, string path, string st, int lv) +{ + int i, j, d, a, k; + d = M[r][c]; + ss t; + xx y; + if (lv == 2) + return 0; + if (c == 6) + { + if (Bit[d][3] == 0) + { + path += "E"; + cout << path.c_str() << endl; + return 1; + } + return 0; + } + if (Bit[d][3] == 1) + { + a = M[r][c + 1]; + if (Bit[a][3] == 1) + return 0; + t.c = c + 1; + t.r = r; + t.A = st; + i = (r - 1) * 6 + (c); + j = (r - 1) * 6 + (c - 1); + if (c < 5) + { + k = (r - 1) * 6 + (c + 1); + t.A[k] += 1; + } + t.A[i] += 3; + t.A[j] -= 4; + if (Map[t]) + return 0; + xxx++; + Map[t] = true; + y.r = t.r; + y.c = t.c; + y.M = t.A; + y.Path = path + "E"; + y.level = 1; + Q.push(y); + return 0; + } + if (Bit[d][3] == 0) + { + t.r = r; + t.c = c + 1; + t.A = st; + if (Map[t]) + return 0; + xxx++; + Map[t] = true; + y.c = c + 1; + y.r = r; + y.M = st; + y.Path = path + "E"; + y.level = 1; + Q.push(y); + return 0; + } + return 0; +} +int GoUp(int r, int c, string path, string st, int lv) +{ + int i, j, d, a, k; + d = M[r][c]; + ss t; + xx y; + if (lv == 3) + { + return 0; + } + if (r == 1) + { + if (Bit[d][2] == 0) + { + path += "N"; + cout << path.c_str() << endl; + return 1; + } + return 0; + } + if (Bit[d][2] == 1) + { + a = M[r - 1][c]; + if (Bit[a][2] == 1) + { + return 0; + } + t.c = c; + t.r = r - 1; + t.A = st; + i = (r - 2) * 6 + (c - 1); + j = (r - 1) * 6 + (c - 1); + if (r > 2) + { + k = (r - 3) * 6 + (c - 1); + t.A[k] += 8; + } + t.A[j] -= 2; + t.A[i] -= 6; + if (Map[t]) + { + return 0; + } + Map[t] = true; + xxx++; + y.r = t.r; + y.c = t.c; + y.M = t.A; + y.level = 4; + y.Path = path + "N"; + Q.push(y); + return 0; + } + if (Bit[d][2] == 0) + { + t.r = r - 1; + t.c = c; + t.A = st; + if (Map[t]) + { + return 0; + } + xxx++; + Map[t] = true; + y.c = c; + y.r = r - 1; + y.M = st; + y.Path = path + "N"; + y.level = 4; + Q.push(y); + return 0; + } + return 0; +} +int GoDown(int r, int c, string path, string st, int lv) +{ + int i, j, d, a, k; + d = M[r][c]; + ss t; + xx y; + if (lv == 4) + { + return 0; + } + if (r == 4) + { + if (Bit[d][4] == 0) + { + path += "S"; + cout << path.c_str() << endl; + return 1; + } + return 0; + } + if (Bit[d][4] == 1) + { + a = M[r + 1][c]; + if (Bit[a][4] == 1) + { + return 0; + } + t.c = c; + t.r = r + 1; + t.A = st; + i = (r)*6 + (c - 1); + j = (r - 1) * 6 + (c - 1); + if (r < 3) + { + k = (r + 1) * 6 + (c - 1); + t.A[k] += 2; + } + t.A[j] -= 8; + t.A[i] += 6; + if (Map[t]) + { + return 0; + } + Map[t] = true; + xxx++; + y.r = t.r; + y.c = t.c; + y.M = t.A; + y.level = 3; + y.Path = path + "S"; + Q.push(y); + return 0; + } + if (Bit[d][4] == 0) + { + t.r = r + 1; + t.c = c; + t.A = st; + if (Map[t]) + { + return 0; + } + Map[t] = true; + xxx++; + y.c = c; + y.r = r + 1; + y.M = st; + y.Path = path + "S"; + y.level = 3; + Q.push(y); + return 0; + } + return 0; +} +void BFS(int r, int c, string st) +{ + int i, j; + xx t, v; + t.c = c; + t.r = r; + t.level = 0; + t.Path = ""; + t.M = st; + Q.push(t); + xxx = 0; + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + Ext(t.M); + if (GoLeft(t.r, t.c, t.Path, t.M, t.level)) + { + return; + } + if (GoRight(t.r, t.c, t.Path, t.M, t.level)) + { + return; + } + if (GoUp(t.r, t.c, t.Path, t.M, t.level)) + { + return; + } + if (GoDown(t.r, t.c, t.Path, t.M, t.level)) + { + return; + } + } +} +void Cal(int r, int c, string st) +{ + ss t; + t.A = st; + t.r = r; + t.c = c; + Map[t] = true; + BFS(r, c, st); +} +void Free() +{ + Map.clear(); + while (!Q.empty()) + Q.pop(); +} + +int main() +{ + int r, c, i, n; + string A; + Ini(); + while (cin >> c >> r) + { + if (!r && !c) + { + break; + } + A = ""; + for (i = 1; i <= 24; i++) + { + cin >> n; + A += ('A' + n); + } + Cal(r, c, A); + Free(); + } + return 0; +} diff --git a/10385.cpp b/10385.cpp new file mode 100644 index 0000000..c8ad382 --- /dev/null +++ b/10385.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +int Dis, F, L, R; + +struct ss +{ + double walk, cycle; +}; +ss S[22]; +int N; +double Max; +int Impossible() +{ + int i; + if (N == 1) + { + return 0; + } + for (i = 0; i + 1 < N; i++) + { + if (S[i].walk > S[N - 1].walk && S[i].cycle > S[N - 1].cycle) + { + return 1; + } + } + return 0; +} +double Times(double d, double sp) +{ + double x, y, v; + if (d == 0) + { + return 0; + } + x = 36 * d; + y = sp * 10; + v = x / y; + return v; +} +void Find(double w) +{ + double c = (Dis * 1000) - w; + double a, b, min = 100000000, d; + int i; + // w *= 1000; + // c *= 1000; + a = Times(w, S[N - 1].walk); + a += Times(c, S[N - 1].cycle); + for (i = 0; i + 1 < N; i++) + { + b = Times(w, S[i].walk); + b += Times(c, S[i].cycle); + if (b < min) + { + min = b; + } + } + if (a <= min) + { + F = 1; + d = min - a; + if (d > Max) + { + Max = d; + L = (int)w; + R = Dis * 1000 - L; + } + } +} +void Cal() +{ + int d; + double x, y; + if (Impossible()) + { + printf("The cheater cannot win.\n"); + return; + } + Max = F = 0; + d = Dis * 1000; + for (int i = 0; i <= d; i++) + { + Find(i); + } + if (F == 0) + { + printf("The cheater cannot win.\n"); + return; + } + printf("The cheater can win by "); + printf("%.lf seconds with ", Max); + x = double(L) / 1000; + y = double(R) / 1000; + printf("r = %.2lfkm and k = %.2lfkm.\n", x, y); +} + +int main() +{ + while (scanf("%d", &Dis) == 1) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%lf%lf", &S[i].walk, &S[i].cycle); + } + Cal(); + } + return 0; +} diff --git a/10386.cpp b/10386.cpp new file mode 100644 index 0000000..ffbf112 --- /dev/null +++ b/10386.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +#define PREC 1.0e-8 + +int main() +{ + int N; + double pi, l, h, m, Y, r, s, m1, m2; + pi = acos(-1.0); + scanf("%d", &N); + r = 1; + m2 = 0.43171784545542; + m1 = 0.75435478480197; + while (N--) + { + scanf("%lf", &r); + s = 8 * r * cos(m1) + 2 * sqrt(3.0) * r; + printf("%.10lf", s); + s = 2 * r * sqrt(3.0) + 8 * r * cos(m2); + printf(" %.10lf\n", s); + } + return 0; +} diff --git a/10387.cpp b/10387.cpp new file mode 100644 index 0000000..60b1f9f --- /dev/null +++ b/10387.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int a, b, s, m, n; + double dis, angle; + cout.precision(2); + cout.setf(ios::fixed); + while (true) + { + cin >> a >> b >> s >> m >> n; + if (!a && !b && !s && !m && !n) + { + break; + } + dis = hypot(b * n, a * m); + angle = fabs(atan2(b * n, a * m) * 180 / M_PI); + if (angle > 90) + { + angle = 180 - angle; + } + dis /= s; + cout << angle << " " << dis << endl; + } +} diff --git a/10388.cpp b/10388.cpp new file mode 100644 index 0000000..5bc97a6 --- /dev/null +++ b/10388.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +char up[2][100], down[2][100]; +int pu[2], pd[2], len[2]; + +int sim() +{ + int i, j, p; + for (i = 0; i < 1000; i++) + { + if (pu[0] + len[0] == 0) + { + return 1; + } + if (pu[1] + len[1] == 0) + { + return -1; + } + up[0][pu[0]++] = down[0][pd[0]++]; + up[1][pu[1]++] = down[1][pd[1]++]; + if (up[0][pu[0] - 1] == up[1][pu[1] - 1]) + { + p = random() / 141 % 2; + for (j = 0; j < pu[1 - p]; j++) + { + up[p][pu[p]++] = up[1 - p][j]; + } + pu[1 - p] = 0; + if (p == 0) + { + printf("Snap! for Jane: "); + } + else + { + printf("Snap! for John: "); + } + for (j = pu[p] - 1; j >= 0; j--) + { + printf("%c", up[p][j]); + } + printf("\n"); + } + if (pd[0] == len[0]) + { + len[0] = pu[0]; + pd[0] = pu[0] = 0; + for (j = 0; j < len[0]; j++) + { + down[0][j] = up[0][j]; + } + } + if (pd[1] == len[1]) + { + len[1] = pu[1]; + pd[1] = pu[1] = 0; + for (j = 0; j < len[1]; j++) + { + down[1][j] = up[1][j]; + } + } + } + return 0; +} +int main() +{ + int times, ans; + scanf("%d", ×); + for (; times; times--) + { + scanf("%s %s", down[0], down[1]); + len[0] = len[1] = strlen(down[0]); + pu[0] = pu[1] = pd[0] = pd[1] = 0; + ans = sim(); + if (ans == 0) + { + puts("Keeps going and going ..."); + } + else if (ans == 1) + { + puts("John wins."); + } + else + { + puts("Jane wins."); + } + if (times != 1) + { + puts(""); + } + } + return 0; +} diff --git a/10389.cpp b/10389.cpp new file mode 100644 index 0000000..a3bb7a5 --- /dev/null +++ b/10389.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +const int Max = 205; +const double inf = 999999999; + +struct tag_nodes +{ + int x, y; +} the_nodes[Max]; +int cc, m, n, i, j; +double d, edge[Max][Max], dis[Max]; +bool vis[Max]; +char buf[3]; +void init_data() +{ + vis[0] = true; + for (int i = 1; i < n; i++) + { + dis[i] = inf; + } +} +double dijkstra() +{ + init_data(); + int now = 0, count = n - 1; + while (count--) + { + int k; + double min_dis = inf; + for (int i = 1; i < n; ++i) + if (!vis[i]) + { + if (dis[i] > dis[now] + edge[now][i]) + { + dis[i] = dis[now] + edge[now][i]; + } + if (min_dis > dis[i]) + { + min_dis = dis[k = i]; + } + } + if (k == 1) + { + break; + } + now = k; + vis[k] = true; + } + return dis[1]; +} +double math_dis(int i, int j) +{ + int a = (the_nodes[i].x - the_nodes[j].x) * (the_nodes[i].x - the_nodes[j].x); + int b = (the_nodes[i].y - the_nodes[j].y) * (the_nodes[i].y - the_nodes[j].y); + return sqrt(1.0 * a + b); +} + +int main() +{ + scanf("%d", &cc); + while (cc--) + { + for (i = 0; i < Max; ++i) + { + the_nodes[i].x = 0; + the_nodes[i].y = 0; + } + for (i = 0; i < Max; ++i) + for (j = 0; j < Max; ++j) + { + edge[i][j] = 0.0; + } + for (i = 0; i < Max; ++i) + { + vis[i] = false; + } + scanf("%d%d%d%d", &the_nodes[0].x, &the_nodes[0].y, &the_nodes[1].x, &the_nodes[1].y); + n = m = 2; + while (scanf("%d%d", &the_nodes[n].x, &the_nodes[n].y) == 2) + { + if (the_nodes[n].x == -1 && the_nodes[n].y == -1) + { + m = n; + scanf("%2[\n]", buf); + if (buf[1] != '\0') + { + break; + } + else + { + continue; + } + } + if (m != n) + { + d = math_dis(n, n - 1); + edge[n][n - 1] = edge[n - 1][n] = 3 * d / 2000; + } + n++; + } + for (i = 0; i < n; ++i) + for (j = i + 1; j < n; ++j) + { + if (edge[i][j] == 0) + { + d = math_dis(i, j); + edge[i][j] = edge[j][i] = 3 * d / 500; + } + } + printf("%.0lf\n", dijkstra()); + if (cc > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/1039.cpp b/1039.cpp new file mode 100644 index 0000000..944fa1a --- /dev/null +++ b/1039.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +inline int max(int a, int b) +{ + return a > b ? a : b; +} + +struct nodes +{ + double x, y; +} tower[100], city[100]; +double eps = 1e-6; +int B, C, R, Q, mat[100][100], oo; + +int belong(double x, double y) +{ + double d = oo, dis; + int cho; + for (int i = 0; i < B; i++) + if ((dis = hypot(x - tower[i].x, y - tower[i].y)) < d) + { + d = dis; + cho = i; + } + return cho; +} + +int main() +{ + int tcas = 1; + int i, j, k; + while (scanf("%d%d%d%d", &B, &C, &R, &Q) != EOF) + { + if (B == 0 && C == 0 && R == 0 && Q == 0) + { + break; + } + memset(mat, 0x3f, sizeof(mat)); + oo = mat[0][0]; + for (i = 0; i < B; i++) + { + scanf("%lf%lf", &tower[i].x, &tower[i].y); + } + for (i = 0; i < C; i++) + { + scanf("%lf%lf", &city[i].x, &city[i].y); + } + for (i = 0; i < R; i++) + { + int a, b; + scanf("%d%d", &a, &b); + a--, b--; + double x = city[b].x - city[a].x, + y = city[b].y - city[a].y; + double L = 0, R = 1, mid; + int ori = belong(city[a].x + L * x, + city[a].y + L * y); + int to = belong(city[b].x, city[b].y); + int res = 0; + while (L + eps < R) + { + if (ori == to) + { + break; + } + else + { + res++; + } + double left = L, right = R; + int ans; + while (left + eps < right) + { + mid = (left + right) / 2;// binary search + int tmp = belong(city[a].x + mid * x, + city[a].y + mid * y); + if (tmp != ori) + { + right = mid - eps, ans = tmp; + } + else + { + left = mid + eps; + } + } + L = right + eps; + ori = ans; + } + mat[a][b] = mat[b][a] = res; + } + for (i = 0; i < C; i++) + { + mat[i][i] = 0; + } + for (k = 0; k < C; k++) + { + for (i = 0; i < C; i++) + { + for (j = 0; j < C; j++) + { + mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]); + } + } + } + printf("Case %d:\n", tcas++); + for (i = 0; i < Q; i++) + { + int a, b; + scanf("%d%d", &a, &b); + a--, b--; + if (mat[a][b] == oo) + { + printf("Impossible\n"); + } + else + { + printf("%d\n", mat[a][b]); + } + } + } + return 0; +} diff --git a/10390.cpp b/10390.cpp new file mode 100644 index 0000000..8b4320b --- /dev/null +++ b/10390.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int main() +{ + int bean[26], guess[26]; + int i, n, m, g[5], tp, tg; + char line[8192], c1, c[5]; + double potcount; + gets(line); + sscanf(line, "%d", &n); + gets(line); + for (; n > 0; n--) + { + memset(bean, 0, 26 * sizeof(int)); + gets(line); + for (i = 0; i < (int)strlen(line); i++) + { + bean[line[i] - 'a']++; + } + memset(guess, 0, 26 * sizeof(int)); + tp = 0; + tg = 0; + while (gets(line)) + { + if (strlen(line) == 0) + { + break; + } + m = sscanf(line, "%c %c:%d,%c:%d,%c:%d,%c:%d,%c:%d", &c1, &c[0], &g[0], &c[1], &g[1], &c[2], &g[2], &c[3], &g[3], &c[4], &g[4]); + m = (m - 1) / 2; + for (i = 0; i < m; i++) + { + tg++; + if (g[i] == bean[c[i] - 'a']) + { + guess[c1 - 'A'] += 2; + tp += 2; + } + if ((g[i] == bean[c[i] - 'a'] - 1) || (g[i] == bean[c[i] - 'a'] + 1)) + { + guess[c1 - 'A'] += 1; + tp++; + } + } + } + potcount = tg * 2.0 / tp; + for (i = 0; i < 26; i++) + { + if (guess[i] != 0) + { + printf("%c %.2lf\n", (i + 'A'), (potcount * guess[i])); + } + } + if (n > 1) + { + printf("\n"); + } + } + return 0; +} diff --git a/10391.cpp b/10391.cpp index 9b327db..aab8b13 100644 --- a/10391.cpp +++ b/10391.cpp @@ -1,27 +1,61 @@ -#include -#include -#include -#include +#include + using namespace std; -int main() { - set< string > Set; - set< string >::iterator it; - string str, A, B; - while (cin >> str) - Set.insert(str); - - for (it = Set.begin(); it != Set.end(); it++) { - str = *it; - for (int i = 1; i < str.length(); i++) { - A = str.substr(0, i); - B = str.substr(i); - if (Set.find(A) != Set.end() && Set.find(B) != Set.end()) { - cout << str << endl; +#define MAXW 120000 +#define MAXC 200 + +char WORD[MAXW][MAXC]; +char M[MAXC], F[MAXW]; +int K; + +int qs(const void *a, const void *b) +{ + return (strcmp((char *)a, (char *)b)); +} +void SOLVEDCASE() +{ + int i, j, p, q; + int *c; + //qsort(WORD,K,sizeof(WORD[0]),qs); + for (i = 0; i < K - 1; i++) + { + for (j = i + 1; j < K; j++) + { + if (strncmp(WORD[j], WORD[i], strlen(WORD[i])) == 0) + { + q = 0; + for (p = strlen(WORD[i]); p < strlen(WORD[j]); p++) + { + M[q++] = WORD[j][p]; + } + M[q] = NULL; + c = (int *)bsearch(M, WORD, K, sizeof(WORD[0]), qs); + if (c) + { + F[j] = 1; + } + } + else + { break; } - //cout << A << " " << B << endl; } } + for (i = 0; i < K; i++) + if (F[i]) + { + puts(WORD[i]); + } +} + +int main() +{ + K = 0; + while (scanf("%s", WORD[K]) != EOF) + { + K++; + } + SOLVEDCASE(); return 0; } diff --git a/10392.cpp b/10392.cpp new file mode 100644 index 0000000..dde2dd1 --- /dev/null +++ b/10392.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define maxn 10000000 + +typedef long long sss; +char sv[maxn + 2]; +int P[664579 + 2], tp; + +void Prime_table() +{ + int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + { + sv[j] = 1; + } + for (i++; sv[i]; i++) + ; + } + P[0] = 2; + tp = 1; + for (i = 3; i < maxn; i += 2) + if (!sv[i]) + { + P[tp++] = i; + } +} +void Factor(sss N) +{ + int i, j; + for (i = 0; i < tp && P[i] * P[i] <= N; i++) + { + if (N % P[i] == 0) + { + while (N % P[i] == 0) + { + printf(" %d\n", P[i]); + N /= P[i]; + } + } + if (N == 1) + { + return; + } + } + if (N > 1) + { + printf(" %lld\n", N); + } +} + +int main() +{ + sss n; + Prime_table(); + while (scanf("%lld", &n) && n >= 0) + { + Factor(n); + printf("\n"); + } + return 0; +} diff --git a/10393.cpp b/10393.cpp index 655dba0..fcc2890 100644 --- a/10393.cpp +++ b/10393.cpp @@ -1,91 +1,109 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +char FINGER[10][10] = {"aqz", "wsx", "edc", "rfvtgb", " ", " ", "yhnujm", "ik,", "ol.", "p;/"}; +char WORD[1002][52]; +char STORE[1002][52]; +char FLAG[300]; +int N, F, MAX; +int CR[15], LEN; + +int sort_function(const void *a, const void *s) +{ + char *c = (char *)a; + char *d = (char *)s; + if (strlen(c) == strlen(d)) + { + return strcmp((char *)c, (char *)d); + } + return strlen(d) - strlen(c); +} +void MAKEZERO() +{ + int i, j, k, m; + for (i = 0; i < 300; i++) + { + FLAG[i] = 0; + } + for (i = 0; i < F; i++) + { + k = CR[i] - 1; + for (j = 0; FINGER[k][j]; j++) + { + m = FINGER[k][j]; + FLAG[m] = 1; + } + } +} +int TYPEABLE(int i) +{ + int j, k; + for (j = 0; WORD[i][j]; j++) + { + k = WORD[i][j]; + if (FLAG[k]) + { + return 0; + } + } + return 1; +} +void PRINT() +{ + int i; + printf("%d\n", MAX); + for (i = 0; i < MAX; i++) + { + if (strlen(STORE[i]) == LEN) + { + printf("%s\n", STORE[i]); + } + } +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - map Map; - Map['q'] = 1; Map['a'] = 1; Map['z'] = 1; - Map['w'] = 2; Map['s'] = 2; Map['x'] = 2; - Map['e'] = 3; Map['d'] = 3; Map['c'] = 3; - Map['r'] = 4; Map['f'] = 4; Map['v'] = 4; Map['t'] = 4; Map['g'] = 4; Map['b'] = 4; - //Special Case : ' ' -- > 5, 6 - Map['y'] = 7; Map['h'] = 7; Map['n'] = 7; Map['u'] = 7; Map['j'] = 7; Map['m'] = 7; - Map['i'] = 8; Map['k'] = 8; Map[','] = 8; - Map['o'] = 9; Map['l'] = 9; Map['.'] = 9; - Map['p'] = 10; Map[';'] = 10; Map['/'] = 10; - - int F, N; - while (cin >> F >> N) { - int i, j; - bool hand[11]; - memset(hand, true, sizeof hand); - FOI(i, 1, F) { - int ind; - cin >> ind; - hand[ind] = false; +int main() +{ + int i; + char buf[60]; + int x; + while (scanf("%d%d", &F, &N) == 2) + { + for (i = 0; i < F; i++) + { + scanf("%d", &CR[i]); } - vector< string > Vec(N); - vector< string > Ans, Print; - int L = 0; - FOI(i, 0, N-1) { - cin >> Vec[i]; - L = max(L, (int)Vec[i].length()); + MAKEZERO(); + for (i = 0; i < N; i++) + { + scanf("%s", WORD[i]); } - do { - FOI(i, 0, N-1) { - bool flag = true; - if ((int)Vec[i].length() == L) { - FOI(j, 0, L-1) { - if ( !hand[Map[Vec[i][j]]] ) { - flag = false; - break; - } - } - if (flag) Ans.push_back(Vec[i]); + qsort((void *)WORD, N, sizeof(WORD[0]), sort_function); + MAX = 0; + LEN = 0; + strcpy(buf, "$$$"); + for (i = 0; i < N; i++) + { + x = strlen(WORD[i]); + if (!strcmp(buf, WORD[i])) + { + continue; + } + if (x < LEN) + { + break; + } + if (TYPEABLE(i)) + { + if (x >= LEN) + { + LEN = x; + strcpy(STORE[MAX++], WORD[i]); + strcpy(buf, WORD[i]); } } - if (Ans.size() > 0) break; - }while (L--); - - int S = Ans.size(); - sort(Ans.begin(), Ans.end()); - FOI(i, 0, S-1) { - if (i > 0 && Ans[i] == Ans[i - 1]) continue; - else Print.push_back(Ans[i]); } - - S = Print.size(); - cout << S << endl; - FOI(i, 0, S-1) - cout << Print[i] << endl; + PRINT(); } return 0; } diff --git a/10394.cpp b/10394.cpp new file mode 100644 index 0000000..c50112c --- /dev/null +++ b/10394.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +enum +{ + MAX = 20000000 + 1 +}; + +bitset p; + +void sieve() +{ + p.flip(); + p[0] = p[1] = 0; + for (int i = 3; i * i <= MAX; i += 2)// WA: i<=MAX + { + if (p[i]) + for (unsigned int j = i * i; j < MAX; j += i) + { + p[j] = 0; + } + } +} + +int main() +{ + sieve(); + vector> twin; + twin.push_back(make_pair(0, 0)); // skip twin[0] + for (int i = 5; i < MAX; i += 2) + { + if (p[i] && p[i - 2]) + { + twin.push_back(make_pair(i - 2, i)); + } + } + int no; + while (scanf("%d", &no) == 1) + { + printf("(%d, %d)\n", twin[no].first, twin[no].second); + } + return 0; +} diff --git a/10396.cpp b/10396.cpp new file mode 100644 index 0000000..4353185 --- /dev/null +++ b/10396.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +// problem: find numbers that has the property like 1260 = 60*21 +// solution: bruteforce a,b => c = a*b , determine c's digits +// are the same as a's digits + b's digits + +set s4, s6, s8; +int acc[2][10]; + +void pre(int n) +{ + set v; + int i, bound, bigger; + if (n == 4) + { + i = 10; + bound = 100; + bigger = 1000; + } + else if (n == 6) + { + i = 100; + bound = 1000; + bigger = 100000; + } + else if (n == 8) + { + i = 1000; + bound = 10000; + bigger = 10000000; + } + for (; i < bound; i++) + for (int j = i; j < bound; j++) + { + memset(acc, false, sizeof(acc)); + int mul, ti, tj; + ti = i; + tj = j; + if (ti % 10 == 0 && tj % 10 == 0) + { + continue; // no trailing zero + } + mul = i * j; + if ((mul / bigger) == 0 || mul & 1) + { + continue; + } + while (ti) + { + acc[0][ti % 10]++; + ti /= 10; + } + while (tj) + { + acc[0][tj % 10]++; + tj /= 10; + } + while (mul) + { + acc[1][mul % 10]++; + mul /= 10; + } + for (ti = 0; ti < 10; ti++) + if (acc[0][ti] != acc[1][ti]) + { + break; + } + if (ti == 10) + { + v.insert(i * j); + } + } + if (n == 4) + { + s4 = v; + } + else if (n == 6) + { + s6 = v; + } + else + { + s8 = v; + } +} + +int main() +{ + int n; + bool first = true; + set::iterator iter1, iter2; + pre(4); + pre(6); + pre(8); + while (cin >> n) + { + if (n == 4) + { + iter1 = s4.begin(); + iter2 = s4.end(); + } + else if (n == 6) + { + iter1 = s6.begin(); + iter2 = s6.end(); + } + else + { + iter1 = s8.begin(); + iter2 = s8.end(); + } + for (; iter1 != iter2; iter1++) + { + cout << *iter1 << endl; + } + printf("\n"); + } + return 0; +} diff --git a/10397.cpp b/10397.cpp new file mode 100644 index 0000000..e19855e --- /dev/null +++ b/10397.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define MAXN 281626 +#define MAX 752 + +struct Edge +{ + int x, y; + double dis; +} B[MAXN]; +struct ss +{ + double x, y; +} pp[MAX]; +int P[MAX], Rank[MAX]; +char F[MAX][MAX]; +int N, E; +int com(const void *a, const void *b) +{ + Edge *x = (Edge *)a; + Edge *y = (Edge *)b; + if (x->dis > y->dis) + { + return 1; + } + if (x->dis < y->dis) + { + return -1; + } + return 0; +} +int Find(int n) +{ + if (n != P[n]) + { + P[n] = Find(P[n]); + } + return P[n]; +} +void Link(int x, int y) +{ + if (Rank[x] > Rank[y]) + { + P[y] = x; + } + else + { + P[x] = y; + if (Rank[x] == Rank[y]) + { + Rank[y]++; + } + } +} +double Dis(double x1, double y1, double x2, double y2) +{ + double a, b, dis; + a = (x1 - x2) * (x1 - x2); + b = (y1 - y2) * (y1 - y2); + dis = sqrt(a + b); + return dis; +} +double MST() +{ + int i, a, b; + double Sum = 0.0; + qsort(B, E, sizeof(B[0]), com); + for (i = 0; i < E; i++) + { + a = Find(B[i].x); + b = Find(B[i].y); + if (a != b) + { + Sum += B[i].dis; + Link(a, b); + } + } + return Sum; +} +void Cal() +{ + int given, a, b, x, y; + int i, j; + double dis; + E = 0; + scanf("%d", &given); + for (i = 1; i <= N; i++) + { + Rank[i] = 0; + P[i] = i; + } + while (given--) + { + scanf("%d%d", &a, &b); + F[a][b] = 1; + x = Find(a); + y = Find(b); + if (x != y) + { + Link(x, y); + } + } + for (i = 0; i < N - 1; i++) + { + for (j = i + 1; j < N; j++) + { + if (F[i + 1][j + 1] == 1) + { + F[i + 1][j + 1] = 0; + continue; + } + dis = Dis(pp[i].x, pp[i].y, pp[j].x, pp[j].y); + B[E].dis = dis; + B[E].x = i + 1; + B[E++].y = j + 1; + } + } + printf("%.2lf\n", MST()); +} + +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + for (i = 0; i < N; i++) + { + scanf("%lf%lf", &pp[i].x, &pp[i].y); + } + Cal(); + } + return 0; +} diff --git a/10398.cpp b/10398.cpp new file mode 100644 index 0000000..4df2087 --- /dev/null +++ b/10398.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + double S; + int I; + double base = log10(1.324717957244746); + while (scanf("%lf%d", &S, &I) == 2) + { + double ans = log10(S); + ans += (I - 1) * base; + if (ans < 9) + { + printf("%.lf\n", floor(pow(10, ans))); + } + else + { + printf("%d\n", (int)(ans + 1)); + } + } + return 0; +} diff --git a/10399.cpp b/10399.cpp new file mode 100644 index 0000000..11d7d82 --- /dev/null +++ b/10399.cpp @@ -0,0 +1,1144 @@ +#include + +using namespace std; + +int main() +{ + int z[1001], N, T; + z[2] = -1; + z[3] = 3; + z[4] = -1; + z[5] = 5; + z[6] = 5; + z[7] = 5; + z[8] = 7; + z[9] = 7; + z[10] = 7; + z[11] = 7; + z[12] = 7; + z[13] = 7; + z[14] = 7; + z[15] = 7; + z[16] = 13; + z[17] = 13; + z[18] = 3; + z[19] = 3; + z[20] = 13; + z[21] = 13; + z[22] = 13; + z[23] = 13; + z[24] = 23; + z[25] = 23; + z[26] = 23; + z[27] = 23; + z[28] = 23; + z[29] = 23; + z[30] = 13; + z[31] = 13; + z[32] = 31; + z[33] = 31; + z[34] = 23; + z[35] = 23; + z[36] = 31; + z[37] = 31; + z[38] = 13; + z[39] = 13; + z[40] = 29; + z[41] = 29; + z[42] = -1; + z[43] = 43; + z[44] = 19; + z[45] = 19; + z[46] = 31; + z[47] = 31; + z[48] = 37; + z[49] = 37; + z[50] = 7; + z[51] = 7; + z[52] = 29; + z[53] = 29; + z[54] = 53; + z[55] = 53; + z[56] = 19; + z[57] = 19; + z[58] = 13; + z[59] = 13; + z[60] = 47; + z[61] = 47; + z[62] = 3; + z[63] = 3; + z[64] = 61; + z[65] = 61; + z[66] = 31; + z[67] = 31; + z[68] = 67; + z[69] = 67; + z[70] = 17; + z[71] = 17; + z[72] = 13; + z[73] = 13; + z[74] = 37; + z[75] = 37; + z[76] = 31; + z[77] = 31; + z[78] = 31; + z[79] = 31; + z[80] = 2; + z[81] = -1; + z[82] = -1; + z[83] = 83; + z[84] = 79; + z[85] = 79; + z[86] = 47; + z[87] = 47; + z[88] = 19; + z[89] = 19; + z[90] = 37; + z[91] = 37; + z[92] = 83; + z[93] = 83; + z[94] = 5; + z[95] = 5; + z[96] = 83; + z[97] = 83; + z[98] = 89; + z[99] = 89; + z[100] = 37; + z[101] = 37; + z[102] = 73; + z[103] = 73; + z[104] = 31; + z[105] = 31; + z[106] = 5; + z[107] = 5; + z[108] = 89; + z[109] = 89; + z[110] = 53; + z[111] = 53; + z[112] = 43; + z[113] = 43; + z[114] = 113; + z[115] = 113; + z[116] = 109; + z[117] = 109; + z[118] = 29; + z[119] = 29; + z[120] = 71; + z[121] = 71; + z[122] = 109; + z[123] = 109; + z[124] = 101; + z[125] = 101; + z[126] = 53; + z[127] = 53; + z[128] = 47; + z[129] = 47; + z[130] = 47; + z[131] = 47; + z[132] = 127; + z[133] = 127; + z[134] = 103; + z[135] = 103; + z[136] = 13; + z[137] = 13; + z[138] = 97; + z[139] = 97; + z[140] = 23; + z[141] = 23; + z[142] = 131; + z[143] = 131; + z[144] = 139; + z[145] = 139; + z[146] = 109; + z[147] = 109; + z[148] = 31; + z[149] = 31; + z[150] = 89; + z[151] = 89; + z[152] = 53; + z[153] = 53; + z[154] = 71; + z[155] = 71; + z[156] = 31; + z[157] = 31; + z[158] = 13; + z[159] = 13; + z[160] = 131; + z[161] = 131; + z[162] = 113; + z[163] = 113; + z[164] = 43; + z[165] = 43; + z[166] = 43; + z[167] = 43; + z[168] = 167; + z[169] = 167; + z[170] = 2; + z[171] = -1; + z[172] = -1; + z[173] = 173; + z[174] = 73; + z[175] = 73; + z[176] = 139; + z[177] = 139; + z[178] = 89; + z[179] = 89; + z[180] = 67; + z[181] = 67; + z[182] = 131; + z[183] = 131; + z[184] = 181; + z[185] = 181; + z[186] = 67; + z[187] = 67; + z[188] = 37; + z[189] = 37; + z[190] = 7; + z[191] = 7; + z[192] = 181; + z[193] = 181; + z[194] = 13; + z[195] = 13; + z[196] = 113; + z[197] = 113; + z[198] = 29; + z[199] = 29; + z[200] = 199; + z[201] = 199; + z[202] = 199; + z[203] = 199; + z[204] = 173; + z[205] = 173; + z[206] = 3; + z[207] = 3; + z[208] = 103; + z[209] = 103; + z[210] = 29; + z[211] = 29; + z[212] = 193; + z[213] = 193; + z[214] = 181; + z[215] = 181; + z[216] = 5; + z[217] = 5; + z[218] = 163; + z[219] = 163; + z[220] = 59; + z[221] = 59; + z[222] = 163; + z[223] = 163; + z[224] = 109; + z[225] = 109; + z[226] = 5; + z[227] = 5; + z[228] = 47; + z[229] = 47; + z[230] = 181; + z[231] = 181; + z[232] = 103; + z[233] = 103; + z[234] = 23; + z[235] = 23; + z[236] = 43; + z[237] = 43; + z[238] = 211; + z[239] = 211; + z[240] = 157; + z[241] = 157; + z[242] = 241; + z[243] = 241; + z[244] = 83; + z[245] = 83; + z[246] = 43; + z[247] = 43; + z[248] = 181; + z[249] = 181; + z[250] = 179; + z[251] = 179; + z[252] = 211; + z[253] = 211; + z[254] = 89; + z[255] = 89; + z[256] = 59; + z[257] = 59; + z[258] = 199; + z[259] = 199; + z[260] = 193; + z[261] = 193; + z[262] = 67; + z[263] = 67; + z[264] = 47; + z[265] = 47; + z[266] = 139; + z[267] = 139; + z[268] = 113; + z[269] = 113; + z[270] = 227; + z[271] = 227; + z[272] = 79; + z[273] = 79; + z[274] = 211; + z[275] = 211; + z[276] = 13; + z[277] = 13; + z[278] = 241; + z[279] = 241; + z[280] = 173; + z[281] = 173; + z[282] = 89; + z[283] = 89; + z[284] = 83; + z[285] = 83; + z[286] = 191; + z[287] = 191; + z[288] = 281; + z[289] = 281; + z[290] = 181; + z[291] = 181; + z[292] = 271; + z[293] = 271; + z[294] = 19; + z[295] = 19; + z[296] = 211; + z[297] = 211; + z[298] = 199; + z[299] = 199; + z[300] = 293; + z[301] = 293; + z[302] = 199; + z[303] = 199; + z[304] = 263; + z[305] = 263; + z[306] = 211; + z[307] = 211; + z[308] = 263; + z[309] = 263; + z[310] = 131; + z[311] = 131; + z[312] = 227; + z[313] = 227; + z[314] = 53; + z[315] = 53; + z[316] = 157; + z[317] = 157; + z[318] = 211; + z[319] = 211; + z[320] = 139; + z[321] = 139; + z[322] = 23; + z[323] = 23; + z[324] = 61; + z[325] = 61; + z[326] = 79; + z[327] = 79; + z[328] = 269; + z[329] = 269; + z[330] = 107; + z[331] = 107; + z[332] = 131; + z[333] = 131; + z[334] = 211; + z[335] = 211; + z[336] = 199; + z[337] = 199; + z[338] = 199; + z[339] = 199; + z[340] = 331; + z[341] = 331; + z[342] = 331; + z[343] = 331; + z[344] = 271; + z[345] = 271; + z[346] = 241; + z[347] = 241; + z[348] = 269; + z[349] = 269; + z[350] = 241; + z[351] = 241; + z[352] = 167; + z[353] = 167; + z[354] = 293; + z[355] = 293; + z[356] = 109; + z[357] = 109; + z[358] = 293; + z[359] = 293; + z[360] = 157; + z[361] = 157; + z[362] = 157; + z[363] = 157; + z[364] = 307; + z[365] = 307; + z[366] = 359; + z[367] = 359; + z[368] = 67; + z[369] = 67; + z[370] = 29; + z[371] = 29; + z[372] = 149; + z[373] = 149; + z[374] = 139; + z[375] = 139; + z[376] = 31; + z[377] = 31; + z[378] = 181; + z[379] = 181; + z[380] = 139; + z[381] = 139; + z[382] = 293; + z[383] = 293; + z[384] = 233; + z[385] = 233; + z[386] = 271; + z[387] = 271; + z[388] = 73; + z[389] = 73; + z[390] = 389; + z[391] = 389; + z[392] = 389; + z[393] = 389; + z[394] = 337; + z[395] = 337; + z[396] = 293; + z[397] = 293; + z[398] = 193; + z[399] = 193; + z[400] = 191; + z[401] = 191; + z[402] = 277; + z[403] = 277; + z[404] = 157; + z[405] = 157; + z[406] = 401; + z[407] = 401; + z[408] = 157; + z[409] = 157; + z[410] = 199; + z[411] = 199; + z[412] = 293; + z[413] = 293; + z[414] = 107; + z[415] = 107; + z[416] = 3; + z[417] = 3; + z[418] = 409; + z[419] = 409; + z[420] = 149; + z[421] = 149; + z[422] = 199; + z[423] = 199; + z[424] = 293; + z[425] = 293; + z[426] = 317; + z[427] = 317; + z[428] = 89; + z[429] = 89; + z[430] = 17; + z[431] = 17; + z[432] = 317; + z[433] = 317; + z[434] = 193; + z[435] = 193; + z[436] = 193; + z[437] = 193; + z[438] = 167; + z[439] = 167; + z[440] = 79; + z[441] = 79; + z[442] = 389; + z[443] = 389; + z[444] = 409; + z[445] = 409; + z[446] = 359; + z[447] = 359; + z[448] = 421; + z[449] = 421; + z[450] = 71; + z[451] = 71; + z[452] = 113; + z[453] = 113; + z[454] = 337; + z[455] = 337; + z[456] = 173; + z[457] = 173; + z[458] = 443; + z[459] = 443; + z[460] = 277; + z[461] = 277; + z[462] = 3; + z[463] = 3; + z[464] = 337; + z[465] = 337; + z[466] = 283; + z[467] = 283; + z[468] = 53; + z[469] = 53; + z[470] = 211; + z[471] = 211; + z[472] = 227; + z[473] = 227; + z[474] = 139; + z[475] = 139; + z[476] = 467; + z[477] = 467; + z[478] = 401; + z[479] = 401; + z[480] = 113; + z[481] = 113; + z[482] = 127; + z[483] = 127; + z[484] = 113; + z[485] = 113; + z[486] = 239; + z[487] = 239; + z[488] = 409; + z[489] = 409; + z[490] = 83; + z[491] = 83; + z[492] = 223; + z[493] = 223; + z[494] = 313; + z[495] = 313; + z[496] = 431; + z[497] = 431; + z[498] = 317; + z[499] = 317; + z[500] = 157; + z[501] = 157; + z[502] = 109; + z[503] = 109; + z[504] = 421; + z[505] = 421; + z[506] = 67; + z[507] = 67; + z[508] = 13; + z[509] = 13; + z[510] = 347; + z[511] = 347; + z[512] = 31; + z[513] = 31; + z[514] = 31; + z[515] = 31; + z[516] = 191; + z[517] = 191; + z[518] = 277; + z[519] = 277; + z[520] = 449; + z[521] = 449; + z[522] = 443; + z[523] = 443; + z[524] = 401; + z[525] = 401; + z[526] = 173; + z[527] = 173; + z[528] = 373; + z[529] = 373; + z[530] = 409; + z[531] = 409; + z[532] = 293; + z[533] = 293; + z[534] = 317; + z[535] = 317; + z[536] = 139; + z[537] = 139; + z[538] = 137; + z[539] = 137; + z[540] = 113; + z[541] = 113; + z[542] = 367; + z[543] = 367; + z[544] = 181; + z[545] = 181; + z[546] = 479; + z[547] = 479; + z[548] = 367; + z[549] = 367; + z[550] = 131; + z[551] = 131; + z[552] = 271; + z[553] = 271; + z[554] = 113; + z[555] = 113; + z[556] = 509; + z[557] = 509; + z[558] = 547; + z[559] = 547; + z[560] = 181; + z[561] = 181; + z[562] = 127; + z[563] = 127; + z[564] = 263; + z[565] = 263; + z[566] = 421; + z[567] = 421; + z[568] = 73; + z[569] = 73; + z[570] = 179; + z[571] = 179; + z[572] = 277; + z[573] = 277; + z[574] = 373; + z[575] = 373; + z[576] = 73; + z[577] = 73; + z[578] = 211; + z[579] = 211; + z[580] = 317; + z[581] = 317; + z[582] = 397; + z[583] = 397; + z[584] = 263; + z[585] = 263; + z[586] = 577; + z[587] = 577; + z[588] = 241; + z[589] = 241; + z[590] = 97; + z[591] = 97; + z[592] = 509; + z[593] = 509; + z[594] = 563; + z[595] = 563; + z[596] = 337; + z[597] = 337; + z[598] = 433; + z[599] = 433; + z[600] = 227; + z[601] = 227; + z[602] = 409; + z[603] = 409; + z[604] = 211; + z[605] = 211; + z[606] = 577; + z[607] = 577; + z[608] = 509; + z[609] = 509; + z[610] = 601; + z[611] = 601; + z[612] = 373; + z[613] = 373; + z[614] = 167; + z[615] = 167; + z[616] = 89; + z[617] = 89; + z[618] = 353; + z[619] = 353; + z[620] = 409; + z[621] = 409; + z[622] = 73; + z[623] = 73; + z[624] = 523; + z[625] = 523; + z[626] = 251; + z[627] = 251; + z[628] = 607; + z[629] = 607; + z[630] = 521; + z[631] = 521; + z[632] = 479; + z[633] = 479; + z[634] = 137; + z[635] = 137; + z[636] = 43; + z[637] = 43; + z[638] = 313; + z[639] = 313; + z[640] = 83; + z[641] = 83; + z[642] = 479; + z[643] = 479; + z[644] = 631; + z[645] = 631; + z[646] = 523; + z[647] = 523; + z[648] = 577; + z[649] = 577; + z[650] = 643; + z[651] = 643; + z[652] = 317; + z[653] = 317; + z[654] = 467; + z[655] = 467; + z[656] = 509; + z[657] = 509; + z[658] = 163; + z[659] = 163; + z[660] = 71; + z[661] = 71; + z[662] = 157; + z[663] = 157; + z[664] = 331; + z[665] = 331; + z[666] = 613; + z[667] = 613; + z[668] = 631; + z[669] = 631; + z[670] = 397; + z[671] = 397; + z[672] = 449; + z[673] = 449; + z[674] = 211; + z[675] = 211; + z[676] = 647; + z[677] = 647; + z[678] = 503; + z[679] = 503; + z[680] = 643; + z[681] = 643; + z[682] = 227; + z[683] = 227; + z[684] = 353; + z[685] = 353; + z[686] = 683; + z[687] = 683; + z[688] = 547; + z[689] = 547; + z[690] = 467; + z[691] = 467; + z[692] = 67; + z[693] = 67; + z[694] = 401; + z[695] = 401; + z[696] = 211; + z[697] = 211; + z[698] = 109; + z[699] = 109; + z[700] = 677; + z[701] = 677; + z[702] = 293; + z[703] = 293; + z[704] = 113; + z[705] = 113; + z[706] = 613; + z[707] = 613; + z[708] = 359; + z[709] = 359; + z[710] = 449; + z[711] = 449; + z[712] = 401; + z[713] = 401; + z[714] = 509; + z[715] = 509; + z[716] = 449; + z[717] = 449; + z[718] = 467; + z[719] = 467; + z[720] = 223; + z[721] = 223; + z[722] = 131; + z[723] = 131; + z[724] = 461; + z[725] = 461; + z[726] = 359; + z[727] = 359; + z[728] = 67; + z[729] = 67; + z[730] = 29; + z[731] = 29; + z[732] = 151; + z[733] = 151; + z[734] = 661; + z[735] = 661; + z[736] = 149; + z[737] = 149; + z[738] = 491; + z[739] = 491; + z[740] = 113; + z[741] = 113; + z[742] = 509; + z[743] = 509; + z[744] = 139; + z[745] = 139; + z[746] = 613; + z[747] = 613; + z[748] = 401; + z[749] = 401; + z[750] = 653; + z[751] = 653; + z[752] = 109; + z[753] = 109; + z[754] = 503; + z[755] = 503; + z[756] = 29; + z[757] = 29; + z[758] = 421; + z[759] = 421; + z[760] = 461; + z[761] = 461; + z[762] = 523; + z[763] = 523; + z[764] = 479; + z[765] = 479; + z[766] = 173; + z[767] = 173; + z[768] = 157; + z[769] = 157; + z[770] = 397; + z[771] = 397; + z[772] = 733; + z[773] = 733; + z[774] = 53; + z[775] = 53; + z[776] = 373; + z[777] = 373; + z[778] = 317; + z[779] = 317; + z[780] = 499; + z[781] = 499; + z[782] = 283; + z[783] = 283; + z[784] = 251; + z[785] = 251; + z[786] = 523; + z[787] = 523; + z[788] = 47; + z[789] = 47; + z[790] = 523; + z[791] = 523; + z[792] = 307; + z[793] = 307; + z[794] = 67; + z[795] = 67; + z[796] = 709; + z[797] = 709; + z[798] = 349; + z[799] = 349; + z[800] = 661; + z[801] = 661; + z[802] = 743; + z[803] = 743; + z[804] = 409; + z[805] = 409; + z[806] = 389; + z[807] = 389; + z[808] = 509; + z[809] = 509; + z[810] = 617; + z[811] = 617; + z[812] = 433; + z[813] = 433; + z[814] = 313; + z[815] = 313; + z[816] = 467; + z[817] = 467; + z[818] = 53; + z[819] = 53; + z[820] = 433; + z[821] = 433; + z[822] = 683; + z[823] = 683; + z[824] = 499; + z[825] = 499; + z[826] = 59; + z[827] = 59; + z[828] = 467; + z[829] = 467; + z[830] = 769; + z[831] = 769; + z[832] = 829; + z[833] = 829; + z[834] = 601; + z[835] = 601; + z[836] = 23; + z[837] = 23; + z[838] = 523; + z[839] = 523; + z[840] = 523; + z[841] = 523; + z[842] = 19; + z[843] = 19; + z[844] = 661; + z[845] = 661; + z[846] = 37; + z[847] = 37; + z[848] = 163; + z[849] = 163; + z[850] = 727; + z[851] = 727; + z[852] = 653; + z[853] = 653; + z[854] = 643; + z[855] = 643; + z[856] = 719; + z[857] = 719; + z[858] = 199; + z[859] = 199; + z[860] = 661; + z[861] = 661; + z[862] = 619; + z[863] = 619; + z[864] = 661; + z[865] = 661; + z[866] = 757; + z[867] = 757; + z[868] = 757; + z[869] = 757; + z[870] = 863; + z[871] = 863; + z[872] = 787; + z[873] = 787; + z[874] = 719; + z[875] = 719; + z[876] = 449; + z[877] = 449; + z[878] = 379; + z[879] = 379; + z[880] = 317; + z[881] = 317; + z[882] = 751; + z[883] = 751; + z[884] = 829; + z[885] = 829; + z[886] = 293; + z[887] = 293; + z[888] = 397; + z[889] = 397; + z[890] = 631; + z[891] = 631; + z[892] = 563; + z[893] = 563; + z[894] = 31; + z[895] = 31; + z[896] = 467; + z[897] = 467; + z[898] = 313; + z[899] = 313; + z[900] = 821; + z[901] = 821; + z[902] = 887; + z[903] = 887; + z[904] = 887; + z[905] = 887; + z[906] = 241; + z[907] = 241; + z[908] = 631; + z[909] = 631; + z[910] = 829; + z[911] = 829; + z[912] = 617; + z[913] = 617; + z[914] = 797; + z[915] = 797; + z[916] = 211; + z[917] = 211; + z[918] = 109; + z[919] = 109; + z[920] = 673; + z[921] = 673; + z[922] = 599; + z[923] = 599; + z[924] = 563; + z[925] = 563; + z[926] = 13; + z[927] = 13; + z[928] = 211; + z[929] = 211; + z[930] = 389; + z[931] = 389; + z[932] = 523; + z[933] = 523; + z[934] = 257; + z[935] = 257; + z[936] = 3; + z[937] = 3; + z[938] = 383; + z[939] = 383; + z[940] = 509; + z[941] = 509; + z[942] = 163; + z[943] = 163; + z[944] = 353; + z[945] = 353; + z[946] = 503; + z[947] = 503; + z[948] = 683; + z[949] = 683; + z[950] = 647; + z[951] = 647; + z[952] = 199; + z[953] = 199; + z[954] = 547; + z[955] = 547; + z[956] = 109; + z[957] = 109; + z[958] = 839; + z[959] = 839; + z[960] = 691; + z[961] = 691; + z[962] = 563; + z[963] = 563; + z[964] = 317; + z[965] = 317; + z[966] = 233; + z[967] = 233; + z[968] = 787; + z[969] = 787; + z[970] = 607; + z[971] = 607; + z[972] = 257; + z[973] = 257; + z[974] = 113; + z[975] = 113; + z[976] = 151; + z[977] = 151; + z[978] = 727; + z[979] = 727; + z[980] = 457; + z[981] = 457; + z[982] = 739; + z[983] = 739; + z[984] = 563; + z[985] = 563; + z[986] = 523; + z[987] = 523; + z[988] = 983; + z[989] = 983; + z[990] = 563; + z[991] = 563; + z[992] = 167; + z[993] = 167; + z[994] = 53; + z[995] = 53; + z[996] = 619; + z[997] = 619; + z[998] = 421; + z[999] = 421; + z[1000] = 127; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d", &N) == 1; cs++) + { + printf(z[N] < 0 ? "B\n" : "A %d\n", z[N]); + } +} + +/* +#define SIEVE_SIZE 5000000 +unsigned char sieve[SIEVE_SIZE/8+10]; + +bool isPrime(int n) { + assert(n <= SIEVE_SIZE); + return n >= 2 && ((sieve[n >> 3] >> (n & 7)) & 1) == 0; +} + +void makeSieve() { + for (int i = 2; i*i <= SIEVE_SIZE; i++) { + if ((sieve[i >> 3] >> (i & 7)) & 1) continue; + for (int j = i*i; j <= SIEVE_SIZE; j += i) { + sieve[j >> 3] |= 1 << (j & 7); + } + } +} + +int N; + +// Assuming that n is a losing position, returns its preceding losing losion. +int pred(int n) { + int x = n - N - 1; + while (x >= 2) { + if (isPrime(x)) return x; + x--; + } + return 0; +} + +#define LOG_PRED 12 +int bigpred[LOG_PRED+1][SIEVE_SIZE+1]; // 2^k-th predecessors +void makeBig(int s) +{ + bigpred[0][s] = pred(s); + for (int k = 1; k <= LOG_PRED; k++) + bigpred[k][s] = bigpred[k-1][ bigpred[k-1][s] ]; +} + +void solve() +{ + int losing = -1; + + for (int i = 2; i <= N+2; i++) + makeBig(i); + + // do a bruteforce to find some losing position + for (int s = 2;; s++) { + if (isPrime(s+N)) makeBig(s+N); + if (!isPrime(s)) continue; + + // for any reachable prime s, interval [s, s+N] must contain + // at least one losing position. + + // if position x is a losing position, this implies + // that its predecessor pred(x) = largest prime in interval [2, x-N-1] + // is also a losing position. + + // basic algorithm to find some losing position is to + // take the set of all primes in [s, s+N] (which must contain at least one losing + // position), and repeatedly find replace each number with its predecessor, + // until the set becomes empty, or contains just a single number, which *has* + // to be a losing position. + // The algorithm is speed up by a space-time tradeoff - a table of + // all 2^k-th predecessors of all occuring primes is computed. + + for (int k = 1; k <= LOG_PRED; k++) { + int p = bigpred[k][s]; + if (p == 0) break; + + for (int i = 0; i <= N; i++) { + if (isPrime(s+i)) { + int q = bigpred[k][s+i]; + assert(q != 0); + if (p != q) { p = 0; break; } + } + } + + if (p == 0) continue; + + // found a certainly losing position + losing = p; +printf("[%d]", s); + goto found; + } + } + +found: + // fast-forward to the origin + while (true) { + int p = pred(losing); + if (p == 0) break; + losing = p; + } + + // now 'losing' is the first losing position in interval [2, ...] + + FILE *fp = fopen("cache.cc", "a"); + if (losing <= N) { + printf("A %d\n", losing); + fprintf(fp, "z[%d] = %d;\n", N, losing); + } else { + printf("B\n"); + fprintf(fp, "z[%d] = -1;\n", N); + } + fclose(fp); +} + +int main() +{ + int T; + makeSieve(); + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d", &N) == 1; cs++) { + solve(); + } +} +#endif +*/ + +// Here's how I thought about it when I made the problem. For each prime p, we determine the _unique_ first move for A that will allow A to force taking p. The reason that it is unique is simple: suppose A could pick both 'x' and 'y' as his first move to force taking p, with x < y. Then when A picks x, B could pick y and force taking p himself - a contradiction. And to ensure that there is ALWAYS a forcing move, pretend "A 0" is a valid first move (this is equivalent to a win for B). + +// So for every prime p, there is precisely one value 0 <= x <= n such that A can force taking p if and only if his first move is x. Let's call x the "forcing move" for p. + +// Now we can calculate this for each p. We start with the initial range of primes up to n (obviously). Then for successive p, note that the forcing move of p is the same as the forcing move of the maximum prime q that satisfies p-q > n. A just forces taking q, then B will have to take some prime between p-n and p-1, and then A can take p. + +// So there is an easy way to calculate the forcing move, and now all the question really asks for is the value at the (extremely large) p that is followed by a gap of size n. Of course, that's intractable for large n. But note that once a possible value of forcing move "drops out" of all the primes in a range slightly larger than n, IT CAN NEVER COME BACK. So in fact, random fluctuations will cause the forcing moves to homogenize as p gets large - the distribution of the primes serves as an effectively random perturbing factor to ensure this shakeout will happen. Further, the more primes a specific forcing move applies to, the more likely FUTURE primes will share that forcing move. So the homogenization actually occurs fairly quickly, as anyone who has solved this problem has observed. + +// Also, that's why you have a good chance of getting the right answer even if you don't go as far as necessary; the forcing move that will eventually win will probably already be in the majority. In fact, even if you stopped as soon as one forcing move takes over, say, 2/3rds of the primes in a range, and printed that one, you'd probably have an extremely high probability of correctness. (I haven't looked into it, so I'm not sure if there's a substantial time gain as a result) + +// This makes it clear that the problem is very CHAOTIC in nature; you have a bunch of forcing moves start out equally valid, and then very slight fluctuations in the prime distribution weed them out effectively randomly. It's like horse racing over the primes. I suspect that if you plotted the distribution of the "winners" over the starting range, it would be close to uniform. And there's surely no better way to predict (with certainty) the eventual winner than to simulate it. Interestingly enough, this may mean that the algorithm for this problem IS polynomial in runtime, but while we can argue it probabilistically, there may be no way to actually PROVE it. Cool, huh? diff --git a/104.cpp b/104.cpp index aedbe9a..397d49b 100644 --- a/104.cpp +++ b/104.cpp @@ -1,84 +1,99 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int N; - while(cin>>N){ - double best[20][20][20]; - int path[20][20][20]; - int i, j, k, steps; - memset(best, 0, sizeof(double)*20*20*20); - for(i=0; i>best[i][j][0]; - } - } - bool stat=false; - for(steps=1; steps best[i][j][steps]){ - best[i][j][steps] = tmp; - path[i][j][steps] = k; - } - } - } - } - } - for(steps=1; steps 1.01){ - int seq[steps]; - seq[steps]=path[i][i][steps]; - for(j=steps-1; j>=0; j--) - seq[j]=path[i][seq[j+1]][j]; - for(j=0;j<=steps;j++) - cout< + +using namespace std; + +enum +{ + MAX = 20 +}; + +int main() +{ + int n; // dim(tbl) + double profit[MAX][MAX][MAX]; + int path[MAX][MAX][MAX]; // baktrk + while (scanf("%d", &n) == 1) + { + memset(profit, 0, sizeof(profit)); + // profit[0][i][j] = input + // profit[0][i][i] = 1, for all i + // path[0][i][j] = i, for all i, j + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + if (i == j) + { + profit[0][i][j] = 1.0; + } + else + { + scanf("%lf", &profit[0][i][j]); + } + path[0][i][j] = i; + } + } + + // Floyd-Warshall + for (int steps = 1; steps < n; ++steps) + { + for (int k = 0; k < n; ++k) // intermediate node k + { + for (int i = 0; i < n; ++i) // path from i -> j + { + for (int j = 0; j < n; ++j) + { + double tmp = profit[steps - 1][i][k] * profit[0][k][j]; + if (tmp > profit[steps][i][j]) + { + profit[steps][i][j] = tmp; + path[steps][i][j] = k; + } + } + } + } + } + + // find SP to profit 1% + int steps, tgtNo = -1; // #currency wanted + for (steps = 1; steps < n; steps++) + { + for (int i = 0; i < n; i++) + { + if (profit[steps][i][i] > 1.01) + { + tgtNo = i; + break; + } + } + if (tgtNo != -1) + { + break; + } + } + + if (tgtNo == -1) + { + printf("no arbitrage sequence exists\n"); + } + else + { + int outSeq[MAX]; // rev seq + int idx = 0; + int currNo = tgtNo; + outSeq[idx++] = tgtNo; + // tgtNo -> currNo + for (int s = steps; s >= 0; --s) + { + currNo = path[s][tgtNo][currNo]; + outSeq[idx++] = currNo; + } + for (int i = idx - 1; i > 0; --i) + { + printf("%d ", outSeq[i] + 1); + } + printf("%d\n", outSeq[0] + 1); + } + } + return 0; +} diff --git a/1040.cpp b/1040.cpp new file mode 100644 index 0000000..a013d58 --- /dev/null +++ b/1040.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +struct edges +{ + int a, b, c; +} e[500]; +int nc, nr, dc, nj; +int father[100], mark[100], flag[100], g[20][20]; +int order[500]; +bool cmp(int i, int j) +{ + if (e[i].c == e[j].c) + { + if (e[i].a == e[j].a) + { + return e[i].b < e[i].b; + } + else + { + return e[i].a < e[j].a; + } + } + else + { + return e[i].c < e[j].c; + } +} +int getf(int x) +{ + if (father[x] == x) + { + return x; + } + else + { + return father[x] = getf(father[x]); + } +} +bool Unoin(int a, int b) +{ + int fa = getf(a), fb = getf(b); + if (fa == fb) + { + return false; + } + father[fa] = fb; + g[a][b] = g[b][a] = 1; + return 1; +} + +vector path[100]; +int arr[100], tot; +void dfs(int now, int dep, int fa) +{ + // printf("%d %d %d\n",now,dep,fa); + arr[dep++] = now; + if (mark[now]) + { + path[now].clear(); + for (int i = dep - 1; i >= 0; i--) + { + path[now].push_back(arr[i]); + } + } + for (int i = 0; i < nc; i++) + if (g[now][i] && i != fa) + { + dfs(i, dep, now); + } +} + +void renew() +{ + dfs(dc, 0, -1); +} +bool better(int st0, int st1) +{ + for (int i = 0; i < tot; i++) + { + if (!((1 << i) & st0) && ((1 << i) & st1)) + { + return 0; + } + if (!((1 << i) & st1) && ((1 << i) & st0)) + { + return 1; + } + } + return 0; +} +int main() +{ + int i, j, tcas = 1; + while (scanf("%d", &nc) && nc != -1) + { + int Ans = 1 << 30, Anscnt = 1 << 30, Ansmask; + scanf("%d%d", &dc, &nr); + dc--; + for (i = 0; i < nr; i++) + { + scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c); + e[i].a--, e[i].b--; + } + scanf("%d", &nj); + memset(mark, 0, sizeof(mark)); + int In[100]; + for (i = 0; i < nj; i++) + { + int a; + scanf("%d", &a); + a--; + In[i] = a; + mark[a] = 1; + } + mark[dc] = 1; + int Tn = 0; + tot = 0; + int num[100]; + for (i = 0; i < nc; i++) + if (!mark[i]) + { + num[tot++] = i; + } + else + { + Tn++; + } + for (int st = 0; st < (1 << tot); st++) + { + memcpy(flag, mark, sizeof(mark)); + int addn = 0; + for (i = 0; i < tot; i++) + if ((1 << i) & st) + { + addn++; + flag[num[i]] = 1; + } + int T = 0; + for (i = 0; i < nr; i++) + if (flag[e[i].a] && flag[e[i].b]) + { + order[T++] = i; + } + sort(order, order + T, cmp); + memset(g, 0, sizeof(g)); + int cnt = 0; + for (i = 0; i < nc; i++) + { + father[i] = i; + } + int tmps = 0; + memset(g, 0, sizeof(g)); + for (i = 0; i < T; i++) + if (Unoin(e[order[i]].a, e[order[i]].b)) + { + cnt++; + tmps += e[order[i]].c; + if (cnt == Tn + addn - 1) + { + break; + } + } + if (cnt == Tn + addn - 1) + { + if (tmps < Ans || (tmps == Ans && cnt < Anscnt) || (tmps == Ans && cnt == Anscnt && better(st, Ansmask))) + { + Ans = tmps; + Anscnt = cnt; + Ansmask = st; + renew(); + } + } + } + printf("Case %d: distance = %d\n", tcas++, Ans); + for (i = 0; i < nj; i++) + { + printf(" %d", In[i] + 1); + for (j = 1; j < path[In[i]].size(); j++) + { + printf("-%d", path[In[i]][j] + 1); + } + printf("\n"); + } + printf("\n"); + } +} diff --git a/10400.cpp b/10400.cpp new file mode 100644 index 0000000..e7f9086 --- /dev/null +++ b/10400.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +/**** +10400 +****/ +#define MAXN 64002 +char ss[] = "0+-*/"; +char F[101][MAXN]; +int N, R, C, MX; +int V[101]; +int Rec[101]; +void Print() +{ + int i, j = 1, k; + if (Rec[0] == 2) + { + printf("-"); + } + printf("%d", V[0]); + for (i = 1; i < N; i++) + { + k = Rec[j++]; + printf("%c%d", ss[k], V[i]); + } + printf("=%d\n", R); +} +int GetVal(int sum, int op, int level, int index) +{ + if (level == 0 && (op == 3 || op == 4)) + { + C = 0; + return 0; + } + switch (op) + { + case 1: + sum += V[index]; + if (sum > 32000 || sum < -32000) + { + C = 0; + return 0; + } + break; + case 2: + sum -= V[index]; + if (sum > 32000 || sum < -32000) + { + C = 0; + return 0; + } + break; + case 3: + sum *= V[index]; + if (sum > 32000 || sum < -32000) + { + C = 0; + return 0; + } + break; + case 4: + if (V[index] == 0) + { + C = 0; + return 0; + } + if (sum % V[index]) + { + C = 0; + return 0; + } + sum /= V[index]; + break; + } + return sum; +} +int Recur(int level, int index, int sum, int op) +{ + int temp; + C = 1; + sum = GetVal(sum, op, level, index); + if (C == 0) + { + return 0; + } + if (level == N) + { + if (sum == R) + { + return 1; + } + return 0; + } + temp = sum + 32000; + if (temp > MX) + { + MX = temp; + } + if (level && F[level][temp] == 1) + { + return 0; + } + F[level][temp] = 1; + for (int i = 1; i <= 4; i++) + { + Rec[level] = i; + if (Recur(level + 1, index + 1, sum, i)) + { + return 1; + } + } + return 0; +} +void Cal() +{ + int d; + d = Recur(0, -1, 0, -1); + if (d) + { + Print(); + } + else + { + printf("NO EXPRESSION\n"); + } +} +int main() +{ + int i, kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%d", &V[i]); + } + scanf("%d", &R); + MX = -65000; + Cal(); + for (i = 0; i <= N; i++) + { + memset(F[i], 0, MX); + } + } + return 0; +} diff --git a/10401.cpp b/10401.cpp new file mode 100644 index 0000000..6a15ad8 --- /dev/null +++ b/10401.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +typedef long long ss; + +ss B[17][17], P[100], R[17][17][17], F[17][17], k; +char com[100]; +char pos[] = "123456789ABCDEF"; + +void Ini() +{ + int i; + for (i = 0; pos[i]; i++) + P[pos[i]] = i + 1; +} + +ss recur(ss r, ss c, ss level, ss limit) +{ + ss i, fg = 1; + if (F[r][c] == 1) + return B[r][c]; + if (level == limit) + return 1; + F[r][c] = 1; + if (com[r] == '?') + { + for (i = 1; i <= limit; i++) + { + if (i == c - 1 || i == c || i == c + 1) + continue; + B[r][c] += recur(r + 1, i, level + 1, limit); + } + } + else + { + if (c == P[com[r]] - 1 || c == P[com[r]] || c == P[com[r]] + 1) + return 0; + else + B[r][c] = recur(r + 1, P[com[r]], level + 1, limit); + } + return B[r][c]; +} +void Free() +{ + int i, j; + for (i = 1; i <= 15; i++) + for (j = 1; j <= 15; j++) + B[i][j] = F[i][j] = 0; +} +void Cal() +{ + ss i, j; + k = 0; + j = strlen(com); + if (com[0] == '?') + for (i = 1; i <= j; i++) + k += recur(1, i, 1, j); + else + k = recur(1, P[com[0]], 1, j); + printf("%lld\n", k); +} + +int main() +{ + Ini(); + while (scanf("%s", com) != EOF) + { + Cal(); + Free(); + } + return 0; +} diff --git a/10403.cpp b/10403.cpp new file mode 100644 index 0000000..b047ba6 --- /dev/null +++ b/10403.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +struct Clue +{ + int left[26], right[26]; + char compare; + int pl, pr; +} clues[10000]; +char str[300], *p; +bool used[27]; +int ans[26]; +int n, m; +bool check() +{ + int left, right, nl; + int total; + for (int i = 0; i < m; i++) + { + left = right = nl = 0; + for (int j = 0; j < clues[i].pl; j++) + if (ans[clues[i].left[j]] != 0) + { + nl++; + left += ans[clues[i].left[j]]; + } + total = n * (n + 1) / 2; + if (clues[i].compare == '>') + { + for (int p = n, count = 0; count < clues[i].pl - nl; p--) + if (!used[p]) + { + left += p; + count++; + } + right = total - left; + if (left <= right) + { + return false; + } + } + else if (clues[i].compare == '<') + { + for (int p = 1, count = 0; count < clues[i].pl - nl; p++) + if (!used[p]) + { + left += p; + count++; + } + right = total - left; + if (left >= right) + { + return false; + } + } + else + { + bool flag1 = false, flag2 = false; + int temp = left, best; + for (int p = n, count = 0; count < clues[i].pl - nl; p--) + if (!used[p]) + { + left += p; + count++; + } + right = total - left; + best = (left > right) ? left : right; + if (best * 2 < total) + { + return false; + } + if (nl == clues[i].pl) + if (left == right) + { + continue; + } + else + { + return false; + } + if (left >= right) + { + flag1 = true; + } + left = temp; + for (int p = 1, count = 0; count < clues[i].pl - nl; p++) + if (!used[p]) + { + left += p; + count++; + } + right = total - left; + best = (left > right) ? left : right; + if (best * 2 < total) + { + return false; + } + if (!flag1 && !flag2) + { + return false; + } + } + } + return true; +} +bool DFS(int d) +{ + if (d == n) + { + printf("Solution:"); + for (int i = 0; i < n; i++) + { + printf(" %d", ans[i]); + } + printf("\n"); + return true; + } + else + { + for (int i = 1; i <= n; i++) + { + if (used[i]) + { + continue; + } + ans[d] = i; + used[i] = true; + if (!check()) + { + ans[d] = 0; + used[i] = false; + continue; + } + if (DFS(d + 1)) + { + return true; + } + used[i] = false; + ans[d] = 0; + } + return false; + } +} +int main() +{ + while (gets(str) != NULL) + { + sscanf(str, "%d %d", &n, &m); + memset(used, false, sizeof(used)); + memset(ans, 0, sizeof(ans)); + for (int i = 0; i < m; i++) + { + gets(str); + clues[i].pl = 0; + bool flag = true; + for (p = strtok(str, " "); p; p = strtok(NULL, " ")) + { + if (*p == '=' || *p == '>' || *p == '<') + { + clues[i].compare = *p; + flag = false; + } + else if (flag) + { + clues[i].left[clues[i].pl++] = *p - 'a'; + } + } + } + if (!DFS(0)) + { + puts("No solution possible!"); + } + } +} diff --git a/10404.cpp b/10404.cpp new file mode 100644 index 0000000..2a96a93 --- /dev/null +++ b/10404.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +/**** +10404 +****/ +#define MAXN 1000005 +char F[MAXN]; +int N, T[12], S; +void Cal() +{ + int i, j, diff, best; + F[0] = 2; + for (i = 1; i <= N; i++) + { + best = 1; + for (j = 0; j < S; j++) + { + diff = i - T[j]; + if (diff >= 0) + { + if (F[diff] == 2) + { + best = 2; + break; + } + else if (F[diff] == 3 && best != 2) + { + best = 3; + } + } + } + if (best == 2) + { + F[i] = 3; + } + else if (best == 3) + { + F[i] = 2; + } + } + if (F[N] == 3) + { + printf("Stan wins\n"); + } + else + { + printf("Ollie wins\n"); + } +} +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + scanf("%d", &S); + for (i = 0; i < S; i++) + { + scanf("%d", &T[i]); + } + for (i = 0; i <= N; i++) + { + F[i] = 1; + } + Cal(); + } + return 0; +} diff --git a/10405.cpp b/10405.cpp index 7927462..e95740d 100644 --- a/10405.cpp +++ b/10405.cpp @@ -1,27 +1,40 @@ -#include -#include -using namespace std; - -int main(){ - string row, col; - while(getline(cin,row) && getline(cin,col)){ - int m, n; - m=row.length(); - n=col.length(); - int i, j; - int tower[m+1][n+1]; - for(i=0; i + +using namespace std; + +const int MAXSIZE = 1000; + +char A[MAXSIZE + 5], B[MAXSIZE + 5]; +int LCS[MAXSIZE + 1][MAXSIZE + 1]; + +int main() +{ + while (cin.getline(A + 1, MAXSIZE)) + { + cin.getline(B + 1, MAXSIZE); + int i, j, lenA, lenB; + lenA = strlen(A + 1); + lenB = strlen(B + 1); + LCS[0][0] = 0; + for (i = 0; i <= lenA; i++) + { + LCS[i][0] = 0; + } + for (i = 0; i <= lenB; i++) + { + LCS[0][i] = 0; + } + for (i = 1; i <= lenA; i++) + for (j = 1; j <= lenB; j++) + if (A[i] == B[j]) + { + LCS[i][j] = LCS[i - 1][j - 1] + 1; + } + else + { + LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]); + } + cout << LCS[lenA][lenB] << endl; + } + return 0; +} diff --git a/10406.cpp b/10406.cpp new file mode 100644 index 0000000..b57a6c8 --- /dev/null +++ b/10406.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; +} p[100], temp[100]; +double dist(Point &a, Point &b) +{ + double dx = a.x - b.x, dy = a.y - b.y; + return sqrt(dx * dx + dy * dy); +} +double d, cutarea, costheta, sintheta; +int n; +int main() +{ + while (scanf("%lf %d", &d, &n) == 2) + { + if (d == 0 && n == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &p[i].x, &p[i].y); + } + p[n] = p[0]; + cutarea = 0; + for (int i = 0; i < n; i++) + { + double r = dist(p[i + 1], p[i]); + costheta = (p[i + 1].x - p[i].x) / r; + sintheta = (p[i].y - p[i + 1].y) / r; + for (int j = 0; j < n; j++) + { + if (j == i) + { + continue; + } + double tx, ty; + tx = (p[j].x - p[i].x) * costheta - + (p[j].y - p[i].y) * sintheta + p[i].x; + ty = (p[j].x - p[i].x) * sintheta + + (p[j].y - p[i].y) * costheta + p[i].y; + p[j].x = tx; + p[j].y = ty; + } + p[n] = p[0]; + if (p[(i + 2) % n].y > 0) + { + double tx, ty, dx, dy; + tx = p[(i + n - 1) % n].x - p[i].x; + ty = p[(i + n - 1) % n].y - p[i].y; + dy = d / ty; + dx = dy * tx; + p[i].x -= dx; + p[i].y += d; + tx = p[(i + 2) % n].x - p[(i + 1) % n].x; + ty = p[(i + 2) % n].y - p[(i + 1) % n].y; + dy = d / ty; + dx = dy * tx; + p[(i + 1) % n].x -= dx; + p[(i + 1) % n].y += d; + } + else + { + double tx, ty, dx, dy; + tx = p[(i + n - 1) % n].x - p[i].x; + ty = p[(i + n - 1) % n].y - p[i].y; + dy = d / ty; + dx = dy * tx; + p[i].x -= dx; + p[i].y -= d; + tx = p[(i + 2) % n].x - p[(i + 1) % n].x; + ty = p[(i + 2) % n].y - p[(i + 1) % n].y; + dy = d / ty; + dx = dy * tx; + p[(i + 1) % n].x -= dx; + p[(i + 1) % n].y -= d; + } + } + p[n] = p[0]; + for (int i = 0; i < n; i++) + { + cutarea += p[i].x * p[i + 1].y - p[i].y * p[i + 1].x; + } + printf("%.3lf\n", fabs(cutarea * 0.5)); + } + return 0; +} diff --git a/10407.cpp b/10407.cpp index 458d675..0c46bc8 100644 --- a/10407.cpp +++ b/10407.cpp @@ -1,62 +1,86 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int gcd(int a, int b){ - if(b > a) return gcd(b, a); - if(b==0) return a; - else return gcd(b, a%b); -} -int main(){ - while(true){ - int arr[1000], i=0, n=1; - cin>>arr[0]; - if(arr[0]==0) - break; - while(true){ - cin>>arr[n]; - if(arr[n] == 0) - break; - n++; - } - int gd = gcd(arr[0], arr[1]); - if(arr[0]==arr[1] && n==2){ - cout< + +using namespace std; + +int a[1001], dif[1001], p; +int sort_function(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int diff() +{ + int i; + for (i = 0; i < p - 1; i++) + { + dif[i] = abs(a[i + 1] - a[i]); + } + return 0; +} +int gsg(int a, int b) +{ + int d, c; + if (!a && b) + { + return b; + } + if (a && !b) + { + return a; + } + if (!a && !b) + { + return 0; + } + while (1) + { + d = b % a; + if (!d) + { + break; + } + b = a; + a = d; + } + return a; +} +int result() +{ + int min, i, j, c; + for (i = 0; i < p - 1; i++) + if (p == 2) + { + return dif[0]; + } + min = gsg(dif[0], dif[1]); + if (min == 1) + { + return 1; + } + for (i = 2; i < p - 1; i++) + { + c = gsg(min, dif[i]); + if (c == 1) + { + return 1; + } + min = c; + } + return min; +} +int main() +{ + p = 0; + while (scanf("%d", &a[p]) && a[p]) + { + p++; + while (scanf("%d", &a[p]) && a[p]) + { + p++; + } + qsort((void *)a, p, sizeof(int), sort_function); + diff(); + printf("%d\n", result()); + p = 0; + } + return 0; +} diff --git a/10408.cpp b/10408.cpp new file mode 100644 index 0000000..10cfaec --- /dev/null +++ b/10408.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int n, k; + int a, b, c, d, t, tmp_a, tmp_b; + while (cin >> n >> k) + { + a = 0; + b = 1; + c = 1; + d = n; + while (k--) + { + t = (n + b) / d; + tmp_a = a; + tmp_b = b; + a = c; + b = d; + c = t * c - tmp_a; + d = t * d - tmp_b; + } + cout << a << "/" << b << endl; + } + return 0; +} diff --git a/10409.cpp b/10409.cpp new file mode 100644 index 0000000..f73affa --- /dev/null +++ b/10409.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +void south(int *p) +{ + int t1, t2, t3; + t1 = p[0]; + t2 = p[1]; + t3 = p[2]; + p[0] = t2; + p[1] = 7 - t1; + p[2] = t3; +} +void north(int *p) +{ + int t1, t2, t3; + t1 = p[0]; + t2 = p[1]; + t3 = p[2]; + p[0] = 7 - t2; + p[1] = t1; + p[2] = t3; +} +void east(int *p) +{ + int t1, t2, t3; + t1 = p[0]; + t2 = p[1]; + t3 = p[2]; + p[0] = t3; + p[1] = t2; + p[2] = 7 - t1; +} +void west(int *p) +{ + int t1, t2, t3; + t1 = p[0]; + t2 = p[1]; + t3 = p[2]; + p[0] = 7 - t3; + p[1] = t2; + p[2] = t1; +} + +int main() +{ + char input[10]; + int i, t; + while (cin >> t) + { + int string[3] = {1, 2, 3}; + if (t == 0) + { + break; + } + for (i = 1; i <= t; i++) + { + cin >> input; + if (strcmp(input, "south") == 0) + { + south(string); + } + else if (strcmp(input, "north") == 0) + { + north(string); + } + else if (strcmp(input, "west") == 0) + { + west(string); + } + else if (strcmp(input, "east") == 0) + { + east(string); + } + } + cout << string[0] << endl; + } + return (0); +} diff --git a/1041.cpp b/1041.cpp new file mode 100644 index 0000000..0355302 --- /dev/null +++ b/1041.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +typedef vector VI; +VI right_[52], goal; + +int pos[52], trans[52], wa[52][2], In[52], T, ans_wn, ans[52][2]; + +void disp(VI A) +{ + int i; + for (i = 0; i < 52; i++) + { + printf("%d ", A[i]); + } + printf("\n"); +} +int count_dif(VI A, VI B) +{ + int cnt = 0, i, tmp; + for (i = 0; i < 52; i++) + { + pos[A[i]] = i; + } + for (i = 0; i < 52; i++) + { + if (B[i] != A[i]) + { + int now = B[i]; + do + { + cnt++; + tmp = B[pos[now]]; + B[pos[now]] = now; + now = tmp; + } while (pos[now] != i); + B[i] = now; + } + } + return cnt; +} + +void dfs(int dep, int wn, VI A, int must) +{ + if (dep > T || wn >= ans_wn) + { + return; + } + int need = count_dif(A, right_[T - dep]); + if (need > must) + { + return; + } + if (need + dep > T) + { + return; + } + if (need == 0) + { + if (wn < ans_wn) + { + ans_wn = wn; + for (int i = 0; i < wn; i++) + { + ans[i][0] = wa[i][0], ans[i][1] = wa[i][1]; + } + } + return; + } + VI B = A, C; + C.resize(52); + for (int i = 0; i < 51; i++) + { + wa[wn][1] = i; + wa[wn][0] = dep; + swap(B[i], B[i + 1]); + for (int j = 0; j < 52; j++) + { + C[j] = B[trans[j]]; + } + dfs(dep + 1, wn + 1, C, need); + swap(B[i], B[i + 1]); + } + for (int i = 0; i < 52; i++) + { + C[i] = B[trans[i]]; + } + dfs(dep + 1, wn, C, need); +} + +int main() +{ + int tt, tcas = 1, i, j; + scanf("%d", &tt); + for (i = 0; i < 52; i++) + { + goal.push_back(i); + } + while (tt--) + { + vector init; + for (i = 0; i < 52; i++) + { + scanf("%d", &In[i]), init.push_back(In[i]); + } + for (i = 0; i < 52; i++) + { + if (i < 26) + { + trans[i] = i * 2 + 1; + } + else + { + trans[i] = (i - 26) * 2; + } + } + T = 0; + vector A; + A = goal; + right_[0] = goal; + while (++T) + { + vector tmp; + tmp.resize(52); + for (i = 0; i < 52; i++) + { + tmp[trans[i]] = A[i]; + } + A = tmp; + right_[T] = A; + if (count_dif(A, init) <= T) + { + break; + } + } + printf("Case %d\n", tcas++); + printf("Number of shuffles = %d\n", T); + ans_wn = 52; + dfs(0, 0, init, 100); + if (ans_wn == 0) + { + printf("No error in any shuffle\n"); + } + else + { + for (i = ans_wn - 1; i >= 0; i--) + { + printf("Error in shuffle %d at location %d\n", T - ans[i][0], ans[i][1]); + } + } + printf("\n"); + } + return 0; +} diff --git a/10410.cpp b/10410.cpp new file mode 100644 index 0000000..7649626 --- /dev/null +++ b/10410.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAX 1001 + +typedef struct NODE +{ + int son[MAX]; + int size; + int used; +} NODE; + +NODE node[MAX]; +int bfs[MAX]; +int dfs[MAX]; +int n; + +void print_tree() +{ + int i, j; + for (i = 1; i <= n; i++) + { + printf("%d:", i); + for (j = 0; j < node[i].size; j++) + { + printf(" %d", node[i].son[j]); + } + printf("\n"); + } +} + +int inside(int t, int b) +{ + int i; + for (i = 1; i < b; i++) + if (dfs[i] == t) + { + return 1; + } + return 0; +} + +int loc(int b) +{ + int i; + for (i = 1; i <= n; i++) + if (dfs[i] == b) + { + return i; + } +} + +void reconstruct(int now, int b) +{ + int i, prev; + for (i = now + 1; i <= n; i++) + if (!node[bfs[i]].used && inside(bfs[i], b)) + { + break; + } + if (i > n) + { + return; + } + node[bfs[now]].son[node[bfs[now]].size++] = bfs[i]; + node[bfs[i]].used = 1; + prev = i; + for (i = i + 1; inside(bfs[i], b) && !node[bfs[i]].used && bfs[prev] < bfs[i]; i++) + { + node[bfs[now]].son[node[bfs[now]].size++] = bfs[i]; + node[bfs[i]].used = 1; + reconstruct(prev, loc(bfs[i])); + prev = i; + } + reconstruct(prev, b); +} + +void ini() +{ + int i; + for (i = 1; i <= n; i++) + { + node[i].used = 0; + node[i].size = 0; + } +} + +int main() +{ + int i; + while (scanf("%d", &n) != EOF) + { + for (i = 1; i <= n; i++) + { + scanf("%d", &bfs[i]); + } + for (i = 1; i <= n; i++) + { + scanf("%d", &dfs[i]); + } + ini(); + reconstruct(1, n + 1); + print_tree(); + } + return 0; +} diff --git a/10411.cpp b/10411.cpp new file mode 100644 index 0000000..68f855b --- /dev/null +++ b/10411.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +int h[100], n; +int cc[19], ptr; +int d[19] = {0, 0, 0, -1, 0, -1, 0, 0, -1, -1, 0, 0, -2, 0, 0, 0, -1, 0, 0}; +int l[19] = {1, 4, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2}; +int c[19][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, -1, -1, 0}, {0, 0, 1, 0}, {0, -1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, -1, 0, 0}, {0, -1, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, -1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 2, 0, 0}, {0, -2, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; +int a[19][4] = {{4, 0, 0, 0}, {1, 1, 1, 1}, {2, 2, 0, 0}, {1, 2, 1, 0}, {1, 2, 1, 0}, {2, 2, 0, 0}, {2, 2, 0, 0}, {1, 2, 1, 0}, {1, 2, 1, 0}, {1, 3, 0, 0}, {3, 1, 0, 0}, {2, 1, 1, 0}, {1, 1, 2, 0}, {2, 1, 1, 0}, {1, 1, 2, 0}, {3, 1, 0, 0}, {1, 3, 0, 0}, {3, 1, 0, 0}, {1, 3, 0, 0}}; +bool gameover() +{ + for (int i = 0; i < n; i++) + if (h[i]) + { + return false; + } + return true; +} +int can(int col, int type) +{ + int temp[100], max = 0; + if (col + d[type] < 0 || col + d[type] + l[type] >= n + 1) + { + return -1; + } + int basecol = col + d[type]; + for (int i = 1; i < l[type]; i++) + if (h[basecol + i] != h[basecol] + c[type][i]) + { + return -1; + } + for (int i = 0; i < n; i++) + { + temp[i] = h[i]; + } + for (int i = 0; i < l[type]; i++) + { + temp[basecol + i] += a[type][i]; + if (temp[basecol + i] > max) + { + max = temp[basecol + i]; + } + } + return max; +} +void put(int col, int type) +{ + int min = 9999999; + int basecol = col + d[type]; + for (int i = 0; i < l[type]; i++) + { + h[basecol + i] += a[type][i]; + } + for (int i = 0; i < n; i++) + if (h[i] < min) + { + min = h[i]; + } + for (int i = 0; i < n; i++) + { + h[i] -= min; + } +} +int main() +{ + int t; + scanf("%d", &t); + srand(time(NULL)); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", h + i); + } + while (true) + { + if (gameover()) + { + break; + } + int c = 0, min = 999, p; + ptr = 0; + for (int i = 0; i < n; i++) + if (h[i] < h[c]) + { + c = i; + } + for (int k = 0; k < 19; k++) + { + int d = can(c, k); + if (d == -1) + { + continue; + } + if (d < min) + { + min = d; + ptr = 0; + cc[ptr++] = k; + } + else if (d == min) + { + cc[ptr++] = k; + } + } + p = cc[rand() % ptr]; + put(c, p); + printf("%d %d\n", p + 1, c + d[p] + 1); + } + puts("-1 -1"); + } + return 0; +} diff --git a/10412.cpp b/10412.cpp new file mode 100644 index 0000000..f8b79cb --- /dev/null +++ b/10412.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int cost[1000], h[1000], l[1000][20]; +int n, m, kk; +int t; +int dist(int x, int y) +{ + return x * x + y * y; +} +int max(int a, int b) +{ + return (a > b) ? a : b; +} +int min(int a, int b) +{ + return (a < b) ? a : b; +} +int cross(int x1, int y1, int x2, int y2) +{ + return x1 * y2 - y1 * x2; +} +bool intersect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) +{ + if (x2 >= x3 && x4 >= x1 && cross(x3 - x1, y3 - y1, x2 - x1, y2 - y1) * cross(x4 - x1, y4 - y1, x2 - x1, y2 - y1) <= 0 && cross(x1 - x3, y1 - y3, x4 - x3, y4 - y3) * cross(x2 - x3, y2 - y3, x4 - x3, y4 - y4) <= 0) + { + return true; + } + return false; +} +bool check(int x1, int y1, int x2, int y2, int t1, int t2) +{ + int p = min(y1, y2), q = max(y1, y2); + for (int i = p + 1; i < q; i++) + { + if (l[t1][i] && intersect(x1, y1, x2, y2, 0, i, l[t1][i], i)) + { + return false; + } + if (l[t2][i] && intersect(x1, y1, x2, y2, m - l[t2][i], i, m, i)) + { + return false; + } + } + return true; +} +int main() +{ + scanf("%d", &t); + for (; t; t--) + { + scanf("%d %d %d", &n, &m, &kk); + memset(l, 0, sizeof(l)); + for (int i = 0; i < n; i++) + { + scanf("%d", &h[i]); + for (int j = 0; j < h[i]; j++) + { + scanf("%d", &l[i][j]); + } + } + for (int i = 1; i < n; i++) + { + cost[i] = cost[i - 1] + m; + for (int j = 0; j < h[i - 1]; j++) + { + for (int k = 0; k < h[i]; k++) + { + if (dist(j - k, m - l[i - 1][j] - l[i][k]) > kk * kk) + { + continue; + } + if (!check(l[i - 1][j], j, m - l[i][k], k, i - 1, i)) + { + continue; + } + if (cost[i - 1] + l[i - 1][j] + l[i][k] < cost[i]) + { + cost[i] = cost[i - 1] + l[i - 1][j] + l[i][k]; + } + } + } + } + printf("%d\n", cost[n - 1]); + } + return 0; +} diff --git a/10413.cpp b/10413.cpp new file mode 100644 index 0000000..495fe4b --- /dev/null +++ b/10413.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int t, n, m, c[15], p[15], l[15]; +int EGCD(int a, int b, int &x, int &y) +{ + if (b == 0) + { + x = 1; + y = 0; + return a; + } + else + { + int d = EGCD(b, a % b, x, y), t = x; + x = y; + y = t - (a / b) * y; + return d; + } +} +int min(int a, int b) +{ + return (a < b) ? a : b; +} +int solve(int ci, int pi, int cj, int pj, int n) +{ + int a = pi - pj, b = cj - ci; + if (a < 0) + { + a = -a; + b = -b; + } + int x, y, base, period; + int d = EGCD(a, n, x, y); + if (b % d != 0) + { + return 99999999; + } + base = (x * (b / d)) % n; + period = n / d; + if (base < 0) + { + return base + (int)(ceil(-base / (double)period)) * period; + } + else + { + return base - (int)(floor(base / (double)period)) * period; + } +} +int main() +{ + scanf("%d", &t); + for (; t; t--) + { + bool flag = false; + scanf("%d", &n); + m = 0; + for (int i = 0; i < n; i++) + { + scanf("%d %d %d", &c[i], &p[i], &l[i]); + if (c[i] > m) + { + m = c[i]; + } + } + for (; m <= 1000000; m++) + { + flag = true; + for (int i = 0; i < n && flag; i++) + for (int j = i + 1; j < n && flag; j++) + if (solve(c[i], p[i], c[j], p[j], m) <= min(l[i], l[j])) + { + flag = false; + } + if (flag) + { + break; + } + } + printf("%d\n", m); + } + return 0; +} diff --git a/10415.cpp b/10415.cpp new file mode 100644 index 0000000..47c0c13 --- /dev/null +++ b/10415.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int tbl[14][11] = +{ + 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, + 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, + 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, + 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, + 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, + 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, + 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, + 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +bool state[11]; +int num[11], hash_[255]; + +void solve(char *data) +{ + memset(num, 0, sizeof(num)); + memset(state, 0, sizeof(state)); + int l = strlen(data), t; + for (int i = 0; i < l; i++) + { + t = hash_[data[i]]; + for (int j = 1; j < 11; j++) + { + if (tbl[t][j] && !state[j]) + { + num[j]++, state[j] = 1; + } + if (!tbl[t][j]) + { + state[j] = 0; + } + } + } + for (int i = 1; i < 11; i++) + { + printf("%d", num[i]); + if (i == 10) + { + printf("\n"); + } + else + { + printf(" "); + } + } +} + +int main() +{ + int t; + char data[205]; + hash_['c'] = 0, hash_['d'] = 1, hash_['e'] = 2; + hash_['f'] = 3, hash_['g'] = 4, hash_['a'] = 5; + hash_['b'] = 6, hash_['C'] = 7, hash_['D'] = 8; + hash_['E'] = 9, hash_['F'] = 10, hash_['G'] = 11; + hash_['A'] = 12, hash_['B'] = 13; + scanf("%d", &t); + gets(data); + while (t--) + { + gets(data); + solve(data); + } + return 0; +} diff --git a/10416.cpp b/10416.cpp new file mode 100644 index 0000000..ce453ca --- /dev/null +++ b/10416.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +const double eps = 1e-01; + +bool isp(P &a, P &b, P &c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + eps; +} + +bool check(int n, P a, P b, P *in, int lstart, int rstart, int lend, int rend) +{ + for (; rstart != rend; rstart = (rstart + 1) % n, lstart = (lstart + n - 1) % n) + { + if (abs(abs(a - in[rstart]) - abs(a - in[lstart])) < eps && + abs(abs(b - in[rstart]) - abs(b - in[lstart])) < eps) + ; + else + { + return false; + } + } + if (lstart != lend) + { + return false; + } + return true; +} + +bool solve(int n, P *in) +{ + rep(i, n) + { + REP(j, 1, n - 1) + { + if (check(n, in[i], in[(i + j) % n], in, + (i + n - 1) % n, (i + 1) % n, + (i + j + 1) % n, (i + j + n - 1) % n)) + { + return true; + } + } + } + return false; +} + +int relax(int n, P *tmp, P *in) +{ + int p = 0; + bool check[n]; + fill(check, check + n, true); + rep(i, n) + { + if (isp(tmp[(i + n - 1) % n], tmp[(i + 1) % n], tmp[i])) + { + check[i] = false; + } + } + rep(i, n) if (check[i]) + { + in[p++] = tmp[i]; + } + return p; +} + +int put_mid_point(int n, P *tmp, P *in) +{ + int p = 0; + rep(i, n) + { + in[p++] = tmp[i]; + in[p] = (tmp[i] + tmp[(i + 1) % n]); + in[p++] /= 2; + } + return p; +} + +int main() +{ + int T = 0; + P tmp1[100], tmp2[100], in[300]; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + rep(i, n) + { + double re, im; + scanf("%lf%lf", &re,&im); + tmp1[i] = P(re,im); + } + int tmpp = relax(n, tmp1, tmp2); + int p = put_mid_point(tmpp, tmp2, in); + puts(solve(p, in) ? "Yes" : "No"); + } + return 0; +} diff --git a/10417.cpp b/10417.cpp new file mode 100644 index 0000000..ffd7aaa --- /dev/null +++ b/10417.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +#define MAXN 13 +#define MAXB 6 +#define MAXD 350000 + +double f[MAXN][MAXD], g[MAXN][MAXB]; +int vis[MAXN][MAXD], a[MAXB], b[MAXB], N, M, gift[MAXB]; + +double dfs(int cur, int st) +{ + int i, j, k; + double ans = 0; + if (vis[cur][st]) + { + return f[cur][st]; + } + k = st; + for (i = 5; i > 0; i--) + { + a[i] = k % 13; + k /= 13; + } + for (i = 1; i <= 5; i++) + { + if (a[i]) + { + --a[i]; + for (j = 1, k = 0; j <= 5; j++) + k = k * 13 + a[j]; + ans += dfs(cur + 1, k) * g[cur][i]; + ++a[i]; + } + } + vis[cur][st] = 1; + return f[cur][st] = ans; +} + +void solve() +{ + int i, j, k, box; + double max, ans, res; + scanf("%d", &N); + for (i = 1; i <= 5; i++) + { + scanf("%d", &gift[i]); + } + for (i = 1; i <= N; i++) + { + for (j = 1; j <= 5; j++) + { + scanf("%lf", &g[i][j]); + } + } + for (i = 1, k = 0; i <= 5; i++) + { + k = k * 13 + gift[i]; + } + memset(vis, 0, sizeof(vis)); + vis[N + 1][0] = 1; + f[N + 1][0] = 1; + res = dfs(1, k); + max = -1; + for (i = 1; i <= 5; i++) + { + if (gift[i]) + { + --gift[i]; + for (j = 1, k = 0; j <= 5; j++) + k = k * 13 + gift[j]; + ++gift[i]; + ans = f[2][k] * g[1][i] / gift[i]; + if (ans / res > max + 1e-9) + { + max = ans / res; + box = i; + } + } + } + printf("%d %.3lf\n", box, max); +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + solve(); + } + return 0; +} diff --git a/10418.cpp b/10418.cpp new file mode 100644 index 0000000..9382a05 --- /dev/null +++ b/10418.cpp @@ -0,0 +1,326 @@ +#include + +using namespace std; + +int diry[4] = {-1, 0, 1, 0}, dirx[4] = {0, 1, 0, -1}; + +int times, m, n, k, t, source = 0, dest, min_, size, h[101][101], r[101], dist[101][101], hp[101][101], c[204]; + +struct Point +{ + int x, y; + Point(){}; + Point(int xx, int yy) : x(xx), y(yy){}; +} red[50], green[50], gold, goal[101], heap[10000]; + +struct Edge +{ + int g, f, w, from, to; + Edge *next, *back; + Edge(int f, int t, int gg, int ww) : from(f), to(t), g(gg), f(0), w(ww), next(NULL), back(NULL){}; +} *graph[204], *p[204]; + +void addEdge(int from, int to, int g, int w) +{ + Edge *p = new Edge(from, to, g, w), *q = new Edge(to, from, 0, -w), *s, *t; + p->back = q; + q->back = p; + for (s = NULL, t = graph[from]; t; s = t, t = t->next) + if (p->w < t->w) + { + break; + } + if (s == NULL) + { + p->next = graph[from]; + graph[from] = p; + } + else + { + p->next = s->next; + s->next = p; + } + for (s = NULL, t = graph[to]; t; s = t, t = t->next) + if (q->w < t->w) + { + break; + } + if (s == NULL) + { + q->next = graph[to]; + graph[to] = q; + } + else + { + q->next = s->next; + s->next = q; + } +} +void insert(Point p) +{ + int k = ++size; + while (k != 1 && dist[heap[k / 2].x][heap[k / 2].y] > dist[p.x][p.y]) + { + heap[k] = heap[k / 2]; + hp[heap[k].x][heap[k].y] = k; + k /= 2; + } + heap[k] = p; + hp[p.x][p.y] = k; +} +void vertify(int k) +{ + int m = k; + if (2 * k <= size && dist[heap[2 * k].x][heap[2 * k].y] < dist[heap[k].x][heap[k].y]) + { + m = 2 * k; + } + if (2 * k + 1 <= size && dist[heap[2 * k + 1].x][heap[2 * k + 1].y] < dist[heap[m].x][heap[m].y]) + { + m = 2 * k + 1; + } + if (k != m) + { + Point temp = heap[k]; + heap[k] = heap[m]; + heap[m] = temp; + hp[heap[k].x][heap[k].y] = k; + hp[heap[m].x][heap[m].y] = m; + vertify(m); + } +} +Point deletemin_() +{ + Point ans = heap[1]; + heap[1] = heap[size--]; + vertify(1); + return ans; +} +void adjust(int pos) +{ + Point temp = heap[pos]; + while (pos != 1 && dist[heap[pos / 2].x][heap[pos / 2].y] > dist[temp.x][temp.y]) + { + heap[pos] = heap[pos / 2]; + hp[heap[pos].x][heap[pos].y] = pos; + pos /= 2; + } + heap[pos] = temp; + hp[heap[pos].x][heap[pos].y] = pos; +} +void BFS(int r, int c, int p) +{ + dist[r][c] = 0; + size = 0; + int d; + Point u(r, c); + insert(u); + while (size != 0) + { + u = deletemin_(); + d = dist[u.x][u.y]; + for (int i = 0; i < 4; i++) + { + int tr = u.x + diry[i], tc = u.y + dirx[i]; + if (tr < 1 || tr > m || tc < 1 || tc > n) + { + continue; + } + if (p == d % 2) + { + if (h[tr][tc] >= h[u.x][u.y] && dist[tr][tc] > d) + { + if (dist[tr][tc] == 255) + { + dist[tr][tc] = d; + Point v(tr, tc); + insert(v); + } + else + { + dist[tr][tc] = d; + adjust(hp[tr][tc]); + } + } + else if (h[tr][tc] < h[u.x][u.y] && dist[tr][tc] > d + 1) + { + if (dist[tr][tc] == 255) + { + dist[tr][tc] = d + 1; + Point v(tr, tc); + insert(v); + } + else + { + dist[tr][tc] = d + 1; + adjust(hp[tr][tc]); + } + } + } + else + { + if (h[tr][tc] <= h[u.x][u.y] && dist[tr][tc] > d) + { + if (dist[tr][tc] == 255) + { + dist[tr][tc] = d; + Point v(tr, tc); + insert(v); + } + else + { + dist[tr][tc] = d; + adjust(hp[tr][tc]); + } + } + else if (h[tr][tc] > h[u.x][u.y] && dist[tr][tc] > d + 1) + { + if (dist[tr][tc] == 255) + { + dist[tr][tc] = d + 1; + Point v(tr, tc); + insert(v); + } + else + { + dist[tr][tc] = d + 1; + adjust(hp[tr][tc]); + } + } + } + } + } +} +bool findpath() +{ + for (int i = 0; i < 204; i++) + { + c[i] = 999999, p[i] = NULL; + } + c[source] = 0; + bool flag; + do + { + flag = false; + for (int i = 0; i <= dest; i++) + { + if (i != 0 && p[i] == NULL) + { + continue; + } + for (Edge *ptr = graph[i]; ptr; ptr = ptr->next) + { + if (ptr->w > min_) + { + break; + } + if (ptr->g - ptr->f > 0 && c[ptr->to] > max(c[i], ptr->w)) + { + p[ptr->to] = ptr; + c[ptr->to] = max(c[i], ptr->w); + flag = true; + } + } + } + } while (flag); + return p[dest] != NULL; +} +void update() +{ + Edge *ptr = p[dest]; + do + { + ptr->f += 1; + ptr->back->f = -ptr->f; + ptr = p[ptr->from]; + } while (ptr != NULL); +} +void freemem(Edge *&p) +{ + if (p) + { + freemem(p->next); + delete p; + } + p = NULL; +} +int main() +{ + scanf("%d", ×); + for (; times; times--) + { + scanf("%d %d %d %d", &m, &n, &k, &t); + for (int i = 0; i < k; i++) + { + scanf("%d %d", &red[i].x, &red[i].y); + } + for (int i = 0; i < k; i++) + { + scanf("%d %d", &green[i].x, &green[i].y); + } + scanf("%d %d", &gold.x, &gold.y); + for (int i = 0; i < t; i++) + { + scanf("%d %d %d", &goal[i].x, &goal[i].y, &r[i]); + } + for (int i = 1; i <= m; i++) + for (int j = 1; j <= n; j++) + { + scanf("%d", &h[i][j]); + } + dest = 2 * k + t + 1; + for (int i = 0; i < k; i++) + { + addEdge(source, i + 1, 1, 0); + for (int x = 1; x <= m; x++) + for (int y = 1; y <= n; y++) + { + dist[x][y] = 255; + } + BFS(red[i].x, red[i].y, 0); + for (int j = 0; j < t; j++) + { + addEdge(i + 1, j + 1 + 2 * k, 1, dist[goal[j].x][goal[j].y]); + } + } + for (int i = 0; i < k; i++) + { + addEdge(source, i + k + 1, 1, 0); + for (int x = 1; x <= m; x++) + for (int y = 1; y <= n; y++) + { + dist[x][y] = 255; + } + BFS(green[i].x, green[i].y, 1); + for (int j = 0; j < t; j++) + { + addEdge(i + k + 1, j + 1 + 2 * k, 1, dist[goal[j].x][goal[j].y]); + } + } + for (int j = 0; j < t; j++) + { + addEdge(j + 1 + 2 * k, dest, r[j], 0); + } + int s = 0; + min_ = 0; + while (true) + { + while (findpath() && s < 2 * k - min_) + { + update(); + s++; + } + if (s == 2 * k - min_) + { + break; + } + min_++; + } + printf("%d\n", min_); + for (int i = 0; i < 204; i++) + { + freemem(graph[i]); + } + } + return 0; +} diff --git a/10419.cpp b/10419.cpp new file mode 100644 index 0000000..9ff20ff --- /dev/null +++ b/10419.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +char buf[100], *token; +int isPrime[2000], prime[2000], sol[20], used[2000], target, n, total, sum, find_, flag; + +void reverse(char *s) +{ + int size = strlen(s), i; + char temp; + for (i = 0; i < size / 2; ++i) + { + temp = s[i]; + s[i] = s[size - 1 - i]; + s[size - 1 - i] = temp; + } +} + +int cmp(const void *a, const void *b) +{ + int d1 = *(int *)a, d2 = *(int *)b, i; + char n1[6], n2[6]; + for (i = 0; d1; ++i) + { + n1[i] = d1 % 10 + '0'; + d1 /= 10; + } + n1[i] = 0; + for (i = 0; d2; ++i) + { + n2[i] = d2 % 10 + '0'; + d2 /= 10; + } + n2[i] = 0; + reverse(n1); + reverse(n2); + return strcmp(n1, n2); +} +void initial() +{ + int i, j; + memset(isPrime, 0, sizeof(isPrime)); + for (i = 3; i < 2000; i += 2) + { + isPrime[i] = 1; + } + for (i = 3; i < 2000; i += 2) + { + for (j = 2; i * j < 2000; ++j) + { + isPrime[i * j] = 0; + } + } + total = 0; + for (i = 3; i < 300; ++i) + if (isPrime[i]) + { + prime[total++] = i; + } + qsort(prime, total, sizeof(int), cmp); +} +void back(int x, int k) +{ + int i; + if (find_) + { + return; + } + if (x == n) + { + if (!target) + { + find_ = 1; + if (flag) + { + qsort(sol, n, sizeof(int), cmp); + } + printf("%d", sol[0]); + for (i = 1; i < n; ++i) + { + printf("+%d", sol[i]); + } + putchar('\n'); + } + return; + } + for (i = k; i < total; ++i) + { + if (target - prime[i] >= 0 && used[prime[i]] < 2) + { + target -= prime[i]; + sol[x] = prime[i]; + ++used[prime[i]]; + back(x + 1, i); + if (find_) + { + return; + } + target += prime[i]; + --used[prime[i]]; + } + } +} + +int main() +{ + int t = 0; + initial(); + while (gets(buf)) + { + token = strtok(buf, " "); + target = atoi(token); + token = strtok(NULL, " "); + n = atoi(token); + if (!target && !n) + { + break; + } + if (target == 2 && n == 1) + { + printf("CASE %d:\n2\n", ++t); + continue; + } + printf("CASE %d:\n", ++t); + find_ = 0; + flag = 0; + sum = 0; + memset(used, 0, sizeof(used)); + if (target % 2 ^ n % 2) + { + flag = 1; + sol[0] = 2; + sum = 2; + target -= 2; + back(1, 0); + } + else + { + back(0, 0); + } + if (!find_) + { + puts("No Solution."); + } + } + return 0; +} diff --git a/1042.cpp b/1042.cpp new file mode 100644 index 0000000..9cb8e89 --- /dev/null +++ b/1042.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +string ttt(double a) +{ + char s[9]; + int x = (337 + a * 760) * 60 + 1e-6; + sprintf(s, "%02d:%02d:%02d", x / 3600, x / 60 % 60, x % 60); + return s; +} +int main() +{ + int m[100], d[100] = {0, 0}; + for (size_t tc = 1, w, h, n; cin >> n, n; ++tc) + { + cin >> w >> h >> m[1]; + for (size_t i = 2; i <= n; ++i) + { + cin >> d[i] >> m[i]; + d[i] += d[i - 1] + w; + } + cout << "Apartment Complex: " << tc << endl + << endl; + for (size_t b, l, q; cin >> q, q; cout << endl) + { + b = q % 100; + l = q / 100; + cout << "Apartment " << q << ": "; + if (!b || b > n || l > m[b] || l == 0) + { + cout << "Does not exist" << endl; + continue; + } + double a1 = 0, a2 = 0; + for (size_t j = 1; j < b; ++j) + if (m[j] >= l) + { + a1 = max(a1, atan2((m[j] - l + 1) * h, d[b] - d[j] - w)); + } + for (size_t j = n; j > b; --j) + if (m[j] >= l) + { + a2 = max(a2, atan2((m[j] - l + 1) * h, d[j] - d[b] - w)); + } + cout << ttt(a1 / M_PI) << " - " << ttt(1 - a2 / M_PI) << endl; + } + } +} diff --git a/10420.cpp b/10420.cpp index 3959597..35bddb3 100644 --- a/10420.cpp +++ b/10420.cpp @@ -1,24 +1,43 @@ -#include -#include -using namespace std; -int main(){ - int n=0,i=0; - cin>>n; - string allCon[n+1]; - for(i=0;i>allCon[i]; - getline(cin,name); - } - sort(allCon,allCon+n); - int count=1; - for(i=0;i + +using namespace std; + +char name[2009][80]; +int n; + +int sort_function(const void *a, const void *s) +{ + return (strcmp((char *)a, (char *)s)); +} + +int count() +{ + int i, c; + char inter[80]; + qsort((void *)name, n, sizeof(name[0]), sort_function); + for (i = 0; i < n;) + { + c = 1; + strcpy(inter, name[i]); + for (i++; !strcmp(inter, name[i]) && i < n; i++) + { + c++; + } + printf("%s %d\n", inter, c); + } + return 0; +} + +int main() +{ + char mid[100]; + int i; + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%s", name[i]); + gets(mid); + } + count(); + return 0; +} diff --git a/10421.cpp b/10421.cpp new file mode 100644 index 0000000..b810ca0 --- /dev/null +++ b/10421.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define maxn 1001 +#define Cst 32767 + +struct ss +{ + int x, y; +}; +ss Point[maxn]; +ss LIS[maxn]; +vector V[65700]; +int N, Max; + +int com(const void *a, const void *b) +{ + ss *p = (ss *)a; + ss *q = (ss *)b; + return p->x - q->x; +} +int FindUp(int key, int x) +{ + int max = 0, i, l, ind; + l = V[key].size(); + for (i = 0; i < l; i++) + { + ind = V[key][i]; + if (Point[ind].x >= x) + break; + if (LIS[ind].x > max) + max = LIS[ind].x; + } + return max; +} +int FindD(int key, int x) +{ + int max = 0, i, l, ind; + l = V[key].size(); + for (i = 0; i < l; i++) + { + ind = V[key][i]; + if (Point[ind].x >= x) + break; + if (LIS[ind].y > max) + max = LIS[ind].y; + } + return max; +} +void Lis() +{ + int i, up, down; + Max = 1; + LIS[0].x = LIS[0].y = 1; + for (i = 1; i < N; i++) + { + up = Point[i].y + 2; + down = Point[i].y - 2; + LIS[i].y = FindUp(up + Cst, Point[i].x) + 1; + LIS[i].x = FindD(down + Cst, Point[i].x) + 1; + if (Max < LIS[i].x) + Max = LIS[i].x; + if (Max < LIS[i].y) + Max = LIS[i].y; + } +} +void Setting() +{ + int i, d; + qsort(Point, N, sizeof(ss), com); + for (i = 0; i < N; i++) + { + d = Point[i].y + Cst; + V[d].push_back(i); + } +} +void Free() +{ + int i, d; + for (i = 0; i < N; i++) + { + d = Point[i].y + Cst; + V[d].clear(); + } +} + +int main() +{ + int i; + while (cin >> N) + { + for (i = 0; i < N; i++) + cin >> Point[i].x >> Point[i].y; + if (N == 0) + { + cout << 0 << endl; + continue; + } + Setting(); + Lis(); + cout << Max << endl; + Free(); + } + return 0; +} diff --git a/10422.cpp b/10422.cpp new file mode 100644 index 0000000..6d4c82a --- /dev/null +++ b/10422.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +/**** +10422 +****/ +char Terget[6][6] = {{1, 1, 1, 1, 1}, {0, 1, 1, 1, 1}, {0, 0, 2, 1, 1}, {0, 0, 0, 0, 1}, {0, 0, 0, 0, 0}}; +char Board[6][6]; +int MAX, F; +int MOVEX[] = {-2, -2, -1, -1, 1, 1, 2, 2}; +int MOVEY[] = {-1, 1, -2, 2, -2, 2, -1, 1}; +int OK() +{ + int i, j; + for (i = 0; i < 5; i++) + for (j = 0; j < 5; j++) + if (Board[i][j] != Terget[i][j]) + { + return 0; + } + return 1; +} +void Recur(int level, int r, int c, int prer, int prec) +{ + if (level > 10) + { + return; + } + if (level >= MAX) + { + return; + } + if (OK()) + { + MAX = level; + F = 1; + return; + } + for (int i = 0; i < 8; i++) + { + int nr = MOVEX[i] + r; + int nc = MOVEY[i] + c; + if (nr >= 5 || nc >= 5 || nr < 0 || nc < 0) + { + continue; + } + if (nr == prer && nc == prec) + { + continue; + } + Board[r][c] = Board[nr][nc]; + Board[nr][nc] = 2; + Recur(level + 1, nr, nc, r, c); + Board[nr][nc] = Board[r][c]; + Board[r][c] = 2; + } +} +int main() +{ + int kase, i, j, t; + int sr, sc; + char input[100]; + gets(input); + sscanf(input, "%d", &kase); + while (kase--) + { + MAX = 100; + for (t = 0; t < 5; t++) + { + gets(input); + for (i = 0; input[i]; i++) + { + if (input[i] == ' ') + { + Board[t][i] = 2; + sr = t; + sc = i; + continue; + } + j = input[i] - '0'; + Board[t][i] = j; + } + } + F = 0; + Recur(0, sr, sc, -1, -1); + if (F == 0) + { + printf("Unsolvable in less than 11 move(s).\n"); + } + else + { + printf("Solvable in %d move(s).\n", MAX); + } + } + return 0; +} diff --git a/10423.cpp b/10423.cpp new file mode 100644 index 0000000..b6672b5 --- /dev/null +++ b/10423.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int main() +{ + int tc = 1; + while (true) + { + int T; + cin >> T; + if (T == 0) + { + break; + } + int res = -1; + int min_diff = 1 << 25; + vector trams; + vector> times; + for (int i = 0; i < T; ++i) + { + int id; + cin >> id; + char dam; + cin >> dam; + while (true) + { + int time; + cin >> time; + if (time == -1) + { + break; + } + times.push_back(make_pair(time, id)); + } + } + int m, n; + cin >> m >> n; + int st = 0; + sort(times.begin(), times.end()); + for (int i = 0; i < (int)times.size(); ++i) + { + int diff = times[i].first - m; + if (diff < 0) + { + diff = times[i].first + 60 - m; + } + if (min_diff > diff) + { + min_diff = diff; + st = i; + } + } + for (int i = st; i < times.size(); ++i) + { + if (n - 1 == i - st) + { + res = times[i].second; + } + } + if (res < 0) + { + res = times[((n - 1) - (times.size() - st)) % times.size()].second; + } + cout << "Case " << tc << ": Peter arrives at stop " << n << " by tram " << res << "." << endl; + ++tc; + } + return 0; +} diff --git a/10424.cpp b/10424.cpp index 7092073..dffa3b2 100644 --- a/10424.cpp +++ b/10424.cpp @@ -1,48 +1,55 @@ -#include -#include -using namespace std; -long long digSum(long long n){ - long long sum=0; - while(n>0){ - sum+=n%10; - n/=10; - } - return sum; -} -int main(){ - string strA,strB; - while(getline(cin,strA) && getline(cin,strB)){ - int lenA=strA.length(), lenB=strB.length(),i; - long long sumA=0,sumB=0; - for(i=0;i9) - sumA=digSum(sumA); - for(i=0;i9) - sumB=digSum(sumB); - - double ratio; - if(sumA<=sumB) - ratio=(double)sumA/(double)sumB; - else - ratio=(double)sumB/(double)sumA; - ratio*=100.00; - printf("%.2lf",ratio); - cout<<" %\n"; - } - return 0; -} +#include + +using namespace std; + +double getdigit(char *p) +{ + int i, n = 0, sum; + for (i = 0; p[i]; i++) + { + if (islower(p[i])) + { + n += p[i] - 'a' + 1; + } + if (isupper(p[i])) + { + n += p[i] - 'A' + 1; + } + } + while (1) + { + if (n / 10 == 0) + { + double tmp = n; + return tmp; + } + sum = 0; + while (n) + { + sum += n % 10; + n /= 10; + } + n = sum; + } +} + +int main() +{ + char p1[50], p2[50]; + double m, n; + while (cin.getline(p1, 50)) + { + cin.getline(p2, 50); + m = getdigit(p1); + n = getdigit(p2); + if (m >= n) + { + printf("%.2lf %%\n", n * 100 / m); + } + else + { + printf("%.2lf %%\n", m * 100 / n); + } + } + return 0; +} diff --git a/10425.cpp b/10425.cpp new file mode 100644 index 0000000..04982d8 --- /dev/null +++ b/10425.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +struct Point +{ + double x, y, z; +} p[3]; +double r1, r2, a, b, c, d; +char name[10]; +double dist(Point a, Point b) +{ + double dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z; + return sqrt(dx * dx + dy * dy + dz * dz); +} +void print(bool s) +{ + if (s == true) + { + puts("y"); + } + else + { + puts("n"); + } +} +int main() +{ + bool in1, in2, s, stat; + while (scanf("%s", name) == 1) + { + scanf("%lf %lf %lf %lf", &p[0].x, &p[0].y, &p[0].z, &r1); + scanf("%lf %lf %lf %lf", &p[1].x, &p[1].y, &p[1].z, &r2); + scanf("%lf %lf %lf", &p[2].x, &p[2].y, &p[2].z); + puts(name); + in1 = (dist(p[0], p[2]) < r1 || fabs(dist(p[0], p[2]) - r1) < 1e-7) ? true : false; + in2 = (dist(p[1], p[2]) < r2 || fabs(dist(p[1], p[2]) - r2) < 1e-7) ? true : false; + a = (p[0].x - p[1].x) * (-2); + b = (p[0].y - p[1].y) * (-2); + c = (p[0].z - p[1].z) * (-2); + d = (r1 * r1 - r2 * r2); + d -= (p[0].x * p[0].x - p[1].x * p[1].x + p[0].y * p[0].y - p[1].y * p[1].y + p[0].z * p[0].z - p[1].z * p[1].z); + stat = (fabs(a * p[2].x + b * p[2].y + c * p[2].z - d) < 1e-7) ? true : false; + s = (in1 && !in2 || !in1 && in2 || in1 && in2 && !stat); + print(in1); + print(in2); + print(s); + } + return 0; +} diff --git a/10426.cpp b/10426.cpp new file mode 100644 index 0000000..e8b3591 --- /dev/null +++ b/10426.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define maxn 17 +#define INF 999999 + +int R, C, mx, my; +int X[] = {1, 2, -1, -2, -1, -2, 1, 2}; +int Y[] = {-2, -1, -2, 1, 2, -1, 2, 1}; + +struct ss +{ + int with; + int without; +}; +ss B[4][maxn][maxn]; +ss P[4]; +struct xx +{ + int r, c, t; + char d; +}; +queue Q; + +void Ini() +{ + int k, j; + for (j = 1; j <= R; j++) + { + for (k = 1; k <= C; k++) + { + B[0][j][k].with = B[0][j][k].without = INF; + B[1][j][k].with = B[1][j][k].without = INF; + B[2][j][k].with = B[2][j][k].without = INF; + B[3][j][k].with = B[3][j][k].without = INF; + } + } +} +void BFS(int r, int c, int ind) +{ + xx t, d; + int nx, ny, i, f; + t.r = r; + t.c = c; + t.d = t.t = 0; + B[ind][r][c].with = B[ind][r][c].without = 0; + Q.push(t); + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + for (i = 0; i < 8; i++) + { + nx = X[i] + t.r; + ny = Y[i] + t.c; + f = 0; + if (nx > R || nx < 1 || ny > C || ny < 1) + continue; + if (t.d == 0) + { + if (B[ind][nx][ny].without > t.t + 1) + { + B[ind][nx][ny].without = t.t + 1; + f = 1; + } + if (B[ind][nx][ny].with > t.t + 1) + { + B[ind][nx][ny].with = t.t + 1; + f = 1; + } + if (f) + { + d.d = 0; + if (nx == mx && my == ny) + d.d = 1; + d.t = t.t + 1; + d.r = nx; + d.c = ny; + Q.push(d); + } + } + else + { + if (nx == mx && ny == my) + continue; + if (B[ind][nx][ny].with > t.t + 1) + { + B[ind][nx][ny].with = t.t + 1; + d.t = t.t + 1; + d.r = nx; + d.c = ny; + d.d = 1; + Q.push(d); + } + } + } + } +} +int FindMin() +{ + int i, min = INF, d, k, j, l, sum; + for (j = 1; j <= R; j++) + { + for (l = 1; l <= C; l++) + { + if (j == mx && l == my) + continue; + sum = 0; + for (i = 0; i < 4; i++) + sum += B[i][j][l].without; + k = sum; + d = k - B[0][j][l].without + B[0][j][l].with; + if (d < sum) + sum = d; + d = k - B[1][j][l].without + B[1][j][l].with; + if (d < sum) + sum = d; + d = k - B[2][j][l].without + B[2][j][l].with; + if (d < sum) + sum = d; + d = k - B[3][j][l].without + B[3][j][l].with; + if (d < sum) + sum = d; + if (min > sum) + min = sum; + } + } + return min; +} +void Cal() +{ + int i; + Ini(); + for (i = 0; i < 4; i++) + BFS(P[i].with, P[i].without, i); + i = FindMin(); + if (i >= INF) + cout << "Meeting is impossible.\n"; + else + cout << "Minimum time required is " << i << " minutes.\n"; +} + +int main() +{ + char str[100]; + int i; + while (cin >> str) + { + cout << str << endl; + cin >> R >> C; + for (i = 0; i < 4; i++) + cin >> P[i].with >> P[i].without; + cin >> mx >> my; + Cal(); + } + return 0; +} diff --git a/10427.cpp b/10427.cpp new file mode 100644 index 0000000..4dd723f --- /dev/null +++ b/10427.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int get_digit(int n, int d) +{ + int digits[9]; + for (int i = int(log10(n)); i >= 0; i--) + { + digits[i] = n % 10; + n /= 10; + } + return digits[d]; +} + +int main() +{ + int n, i; + int sum, tmp, num; + int count[9] = {0, 9, 90, 900, 9000, 90000, 900000, 9000000, 90000000}; + while (cin >> n) + { + sum = 0; + if (n < 10) + { + cout << n << endl; + continue; + } + for (i = 1; i <= 8; i++) + { + sum += i * count[i]; + if (sum >= n) + { + break; + } + } + for (int j = 0; j < i; j++) + { + n -= j * count[j]; + } + tmp = (n % i == 0 ? n / i - 1 : n / i); + num = (i == 1 ? 0 : int(pow(10, i - 1))) + tmp; + tmp = (n % i == 0 ? i - 1 : n % i - 1); + cout << (i == 1 ? num : get_digit(num, tmp)) << endl; + } + return 0; +} diff --git a/10428.cpp b/10428.cpp new file mode 100644 index 0000000..879ea6b --- /dev/null +++ b/10428.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +double f[6], ff[6]; +double ans[5], x, lastx; +int n, nn, ptr; +double calcf(double x) +{ + double sum = 0; + for (int i = 0; i <= nn; i++) + { + sum = sum * x + f[i]; + } + return sum; +} +double calcff(double x) +{ + double sum = 0; + for (int i = 0; i <= nn - 1; i++) + { + sum = sum * x + ff[i]; + } + return sum; +} +int main() +{ + int caseno = 1; + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + nn = n; + ptr = 0; + for (int i = 0; i <= n; i++) + { + scanf("%lf", &f[i]); + } + for (int i = 0; i < n; i++) + { + ff[i] = f[i] * (n - i); + } + for (int i = 0; ptr < n; i++) + { + x = 100; + lastx = x - 1; + while (fabs(x - lastx) >= 1e-6) + { + lastx = x; + if (calcff(x) == 0)//||fabs(calcff(x))<1e-4) + { + x = x * x + 1; + lastx = x - 1; + } + else + { + x = x - calcf(x) / calcff(x); + } + } + while (true) + { + if (fabs(calcf(x)) > 1e-6) + { + break; + } + ans[ptr++] = x; + if (ptr == n) + { + break; + } + for (int j = 1; j < nn; j++) + { + f[j] += f[j - 1] * x; + } + nn--; + for (int j = 0; j < nn; j++) + { + ff[j] = f[j] * (nn - j); + } + } + } + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (ans[i] > ans[j]) + { + double temp = ans[i]; + ans[i] = ans[j]; + ans[j] = temp; + } + printf("Equation %d:", caseno++); + for (int i = 0; i < n; i++) + { + printf(" %.4lf", ans[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10429.cpp b/10429.cpp new file mode 100644 index 0000000..80838bb --- /dev/null +++ b/10429.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const double PI = atan(1) * 4; +int kase = 1; +double px, py, txy; +char str[20]; +int main() +{ + int i = 0; + while (scanf("%lf %lf %lf", &px, &py, &txy) != EOF) + { + if (i > 0) + { + printf("\n"); + } + if (fabs(txy) >= 1e-12) + { + scanf("%s", str); + if (!strcmp(str, "CCW")) + { + txy *= -1.0; + } + } + double r; + r = sqrt((px - py) * (px - py) + 4.0 * txy * txy) / 2.0; + double oc, angle; + oc = (px + py) / 2.0; + angle = acos((px - oc) / r) / 2.0; + angle = angle * 180 / PI; + printf("Element : %d\n", kase++); + printf("Position of maximum normal stresses : %.2lf deg\n", angle); + printf("Maximum normal stresses : %.2lf MPa and %.2lf MPa\n\n", oc + r, oc - r); + printf("Position of maximum shear stresses : %.2lf deg\n", angle + 45); + printf("Maximum shear stress (xy plane) : %.2lf MPa\n", r); + printf("Normal stress at this condition : %.2lf MPa\n", oc); + i++; + } + return 0; +} diff --git a/1043.cpp b/1043.cpp new file mode 100644 index 0000000..0bd72ec --- /dev/null +++ b/1043.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +int dist[2010][2010]; +bool inq[2010][2010]; +int Q[2][2010 * 2010]; +int forb[2010][2010][4]; +int l[2], r[2]; +int dx[4] = {0, 1, 0, -1}; +int dy[4] = {1, 0, -1, 0}; +int T; +bool isin(int x, int y) +{ + return x >= 0 && x < T && y >= 0 && y < T; +} +void push(int no, int x, int y) +{ + if (inq[x][y]) + { + return; + } + inq[x][y] = 1; + Q[no][r[no]++] = x * T + y; +} +int srcx, srcy, destx, desty; +int bfs() +{ + int i, j, dir; + for (i = 0; i < T; i++) + for (j = 0; j < T; j++) + { + dist[i][j] = -1, inq[i][j] = 0; + } + l[0] = l[1] = r[0] = r[1] = 0; + int now = 0, then = 1; + push(0, srcx, srcy); + int cross = 0; + while (1) + { + for (; l[now] < r[now]; l[now]++) + { + int nx = Q[now][l[now]] / T, ny = Q[now][l[now]] % T; + //printf("%d : %d %d\n",cross,nx,ny); + dist[nx][ny] = cross; + if (nx == destx && ny == desty) + { + goto gotit; + } + for (dir = 0; dir < 4; dir++) + { + int tx = nx + dx[dir], ty = ny + dy[dir]; + if (!isin(tx, ty)) + { + continue; + } + if (dir == 0 || dir == 3) + { + if (!forb[nx][ny][dir]) + { + push(now, tx, ty); + } + else + { + push(then, tx, ty); + } + } + else + { + if (!forb[tx][ty][(dir + 2) % 4]) + { + push(now, tx, ty); + } + else + { + push(then, tx, ty); + } + } + } + } + cross++; + now ^= 1, then ^= 1; + } +gotit: + return cross; +} + +int num[10000], order[10000]; + +int main() +{ + int N, i, j, tcas = 1; + while (scanf("%d", &N) && N) + { + for (i = 0; i < 4 * N + 4; i += 2) + { + scanf("%d%d", &num[i], &num[i + 1]); + order[i] = num[i], order[i + 1] = num[i + 1]; + } + int tot = 4 * N + 4; + sort(order, order + tot); + T = unique(order, order + tot) - order; + T++; + for (i = 0; i < tot; i++) + { + num[i] = lower_bound(order, order + T - 1, num[i]) - order + 1; + } + memset(forb, 0, sizeof(forb)); + for (i = 0; i < 4 * N; i += 4) + { + int x1 = num[i], y1 = num[i + 1], x2 = num[i + 2], y2 = num[i + 3]; + if (y1 > y2) + { + swap(y1, y2); + } + if (x1 > x2) + { + swap(x1, x2); + } + //printf("%d %d --- %d %d\n",x1,y1,x2,y2); + if (x1 == x2) + for (j = y1 + 1; j <= y2; j++) + { + forb[x1][j][3] = 1; + } + else if (y1 == y2) + for (j = x1; j < x2; j++) + { + forb[j][y1][0] = 1; + } + } + srcx = num[i], srcy = num[i + 1], destx = num[i + 2], desty = num[i + 3]; + int res = bfs(); + printf("City %d\nPeter has to cross %d streets\n", tcas++, res); + } +} diff --git a/10430.cpp b/10430.cpp new file mode 100644 index 0000000..24fa407 --- /dev/null +++ b/10430.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +void normal(int *a) +{ + for (int i = 0; i < 100; ++i) + { + a[i + 1] += a[i] / 10; + a[i] %= 10; + } +} + +int main() +{ + int times, round; + printf("Dear GOD, Pardon Me"); + + while (scanf("%d %d", ×, &round) != EOF) + { + int tree[100] = {1}, temple[100] = {1}, out = 100; + + for (int i = 1; i < round; ++i) + { + for (int k = 0; k < 100; ++k) + temple[k] = temple[k] * times; + normal(temple); + for (int k = 0; k < 100; ++k) + tree[k] += temple[k]; + normal(tree); + } + for (int k = 0; k < 100; ++k) + temple[k] = temple[k] * times; + normal(temple); + while (!tree[--out]) + ; + printf("\nX = "); + for (; out >= 0; --out) + printf("%d", tree[out]); + printf("\nK = "); + out = 100; + while (!temple[--out]) + ; + for (; out >= 0; --out) + printf("%d", temple[out]); + puts(""); + } + + return 0; +} diff --git a/10431.cpp b/10431.cpp new file mode 100644 index 0000000..dd21fa3 --- /dev/null +++ b/10431.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +const double b[] = {0.319381530, -0.356563782, 1.781477937, -1.821255978, 1.330274429}; +const double p = 0.2316419; +const double PI = atan(1) * 4; + +int kase = 1; +int N, Q; +double mean, dev, value; + +int main() +{ + int line = 0; + while (scanf("%d", &N) != EOF) + { + if (line > 0) + { + printf("\n"); + } + printf("Data Set #%d\n", kase++); + int i; + double sum_x = 0, sum_xx = 0; + for (i = 0; i < N; i++) + { + cin >> value; + sum_x += value; + sum_xx += value * value; + } + mean = sum_x / N; + dev = sum_xx - 2 * sum_x * mean + mean * mean * N; + dev /= (double)(N - 1); + dev = sqrt(dev); + printf("Mean = %.4lf\n", mean); + printf("Standard Deviation = %.4lf\n", dev); + cin >> Q; + for (i = 0; i < Q; i++) + { + cin >> value; + double z, fz, t, qz; + z = (value - mean) / dev; + fz = exp(-0.5 * z * z) / sqrt(2 * PI); + t = 1 / (1 + p * fabs(z)); + qz = fz * (b[0] * t + b[1] * t * t + b[2] * t * t * t + b[3] * t * t * t * t + b[4] * t * t * t * t * t); + //P(z) = 0.9963, Q(z) = 0.0037, T = 0.0403 + printf("P(z) = %.4lf, Q(z) = %.4lf, T = %.4lf\n", 1.0 - qz, qz, qz * N); + } + line++; + } + return 0; +} diff --git a/10432.cpp b/10432.cpp new file mode 100644 index 0000000..00ecdfb --- /dev/null +++ b/10432.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + double p = 4 * acos(0.0), r, n; + while (scanf("%lf%lf", &r, &n) != -1) + { + printf("%0.3lf\n", n * r * r * sin(p / n) / 2.0); + } + return 0; +} diff --git a/10433.cpp b/10433.cpp new file mode 100644 index 0000000..6e21a12 --- /dev/null +++ b/10433.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +const int AutoMorphicLength = 2001; +const char *AutoMorphic[] = + { + "603026954569487924380165488488051064862760620827164159\ +1325236097905009383854054263247198939318022098236001625\ +4517768102915939650450665780903305277219838528634187964\ +5511424748536307235457049044509125214234275955491843973\ +9844587125286948198269270292552648349032065268512722029\ +6131869994777653548129151985764042296818309177344527772\ +3200737603825883172729279563657419014445235954319103063\ +5724961789882031757877610621377080809678113749319117665\ +6303149020578435250957288066846412106925280227506129851\ +1616206384006778979402449023875111258689534549514888200\ +6786677023410028395492829702864472736252175354431979118\ +5506815726485880485267387168480400218852947302222334454\ +1221328464844153593793663133604458940328723478401947357\ +5603613462120086753733469133143387173508802126002857529\ +8538664393102232655345477684502995702556165814337023650\ +2074744856814787872902092412582905301249124668868351587\ +6774998917686787157281349408792768945297970977723054033\ +5661882819870221063055796723980661119019774464242102513\ +6748701117131278125400133690086034889084364023875765936\ +8219796261819178335204927041993248752378258671482789053\ +4489744014261231703569954841949944461060814620725403655\ +9998271588356035049327795540741961849280952093753026852\ +3909375628391485716123673519706092242423987770075749557\ +8727155976741345899753769551586271888794151630756966881\ +6352155048898271704378508028434084412644126821848514157\ +7299160344970178923357966849914473895660019325458276780\ +0061832985442623282725755611073316069701586498422229125\ +5485729879337147866323172405515756102352543994999345608\ +0838011907415300600560557448187096927850997759180500754\ +1642852770816201135024680605816327617167676526093752805\ +6844214486193960499834472806721906670417240094234466197\ +8124266907875359446166985080646361371663840490292193418\ +8190958165952447786184614091287829843843170324817342888\ +6572737663146519104988029447960814673760503957196893714\ +6718013756190554629968147642639039530073191081698029385\ +0989006216650958086381100055742342323089610900410661997\ +7392256259918212890625", + "396973045430512075619834511511948935137239379172835840\ +8674763902094990616145945736752801060681977901763998374\ +5482231897084060349549334219096694722780161471365812035\ +4488575251463692764542950955490874785765724044508156026\ +0155412874713051801730729707447351650967934731487277970\ +3868130005222346451870848014235957703181690822655472227\ +6799262396174116827270720436342580985554764045680896936\ +4275038210117968242122389378622919190321886250680882334\ +3696850979421564749042711933153587893074719772493870148\ +8383793615993221020597550976124888741310465450485111799\ +3213322976589971604507170297135527263747824645568020881\ +4493184273514119514732612831519599781147052697777665545\ +8778671535155846406206336866395541059671276521598052642\ +4396386537879913246266530866856612826491197873997142470\ +1461335606897767344654522315497004297443834185662976349\ +7925255143185212127097907587417094698750875331131648412\ +3225001082313212842718650591207231054702029022276945966\ +4338117180129778936944203276019338880980225535757897486\ +3251298882868721874599866309913965110915635976124234063\ +1780203738180821664795072958006751247621741328517210946\ +5510255985738768296430045158050055538939185379274596344\ +0001728411643964950672204459258038150719047906246973147\ +6090624371608514283876326480293907757576012229924250442\ +1272844023258654100246230448413728111205848369243033118\ +3647844951101728295621491971565915587355873178151485842\ +2700839655029821076642033150085526104339980674541723219\ +9938167014557376717274244388926683930298413501577770874\ +4514270120662852133676827594484243897647456005000654391\ +9161988092584699399439442551812903072149002240819499245\ +8357147229183798864975319394183672382832323473906247194\ +3155785513806039500165527193278093329582759905765533802\ +1875733092124640553833014919353638628336159509707806581\ +1809041834047552213815385908712170156156829675182657111\ +3427262336853480895011970552039185326239496042803106285\ +3281986243809445370031852357360960469926808918301970614\ +9010993783349041913618899944257657676910389099589338002\ +2607743740081787109376"}; + +bool isAutoMorphic(int index, char *arg, int len) +{ + for (int i = AutoMorphicLength - 1, j = len - 1; j >= 0; j--, i--) + { + if (AutoMorphic[index][i] != arg[j]) + { + return false; + } + } + return true; +} + +int main() +{ + char input[2010]; + int len; + bool isAM; + while (!feof(stdin)) + { + if (scanf("%s", input) == EOF) + { + break; + } + len = strlen(input); + switch (input[len - 1]) + { + case '5': + isAM = isAutoMorphic(0, input, len); + break; + case '6': + isAM = isAutoMorphic(1, input, len); + break; + default: + isAM = false; + } + if (isAM) + { + printf("Automorphic number of %d-digit.\n", len); + } + else + { + printf("Not an Automorphic number.\n"); + } + } + return 0; +} diff --git a/10434.cpp b/10434.cpp new file mode 100644 index 0000000..7b9cde4 --- /dev/null +++ b/10434.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const double pg = 9806.65; +const double PI = acos(-1.0); + +char str[50]; +double W, D, h, SG; +int type;//1--h unknow 2--SG unknow + +void Parse(char *p) +{ + if (*p == 'W') + { + while (*p != '=') + { + p++; + } + p++; + sscanf(p, "%lf", &W); + W /= 1000.0; + W *= 9.80665; + } + if (*p == 'D') + { + while (*p != '=') + { + p++; + } + p++; + sscanf(p, "%lf", &D); + D /= 1000.0; + } + if (*p == 'h') + { + while (*p != '=') + { + p++; + } + p++; + sscanf(p, "%lf", &h); + h /= 100.0; + type = 2; + } + if (*p == 'S') + { + while (*p != '=') + { + p++; + } + p++; + sscanf(p, "%lf", &SG); + type = 1; + } +} + +int main() +{ + while (cin.getline(str, 50)) + { + Parse(str); + cin.getline(str, 50); + Parse(str); + cin.getline(str, 50); + Parse(str); + if (type == 1) + { + h = 4.0 * W * (1.0 - 1.0 / SG) / (PI * D * D * pg); + printf("h=%.2lf\n", h * 100.0); + } + else if (type == 2) + { + double temp; + temp = W - pg * h * PI * D * D / 4.0; + SG = W / temp; + printf("SG=%.2lf\n", SG); + } + } + return 0; +} diff --git a/10436.cpp b/10436.cpp new file mode 100644 index 0000000..074e770 --- /dev/null +++ b/10436.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +/* +Cheapest way +10436 +*/ +#define MAXN 22 +int N; +struct ss +{ + char city[100]; + int cost; + int index; +} p[MAXN], c, cc; +int B[MAXN][MAXN], P[MAXN][MAXN], C[MAXN][MAXN]; +int Path[MAXN][MAXN]; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return strcmp(x->city, y->city); +} +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + B[i][j] = B[j][i] = 2147483; + P[i][j] = P[j][i] = 2147483; + Path[i][j] = Path[j][i] = -1; + } + B[i][i] = 0; + P[i][i] = 0; + Path[i][i] = -1; + } +} +void Floyd() +{ + int i, j, k; + int x, y; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + x = B[i][j] * 2 + P[i][j]; + y = B[i][k] * 2 + B[k][j] * 2 + P[i][k] + P[k][j] - p[k - 1].cost; + if (i == k || k == j) + { + y += p[k - 1].cost; + } + if (x > y) + { + C[i][j] = y; + B[i][j] = B[i][k] + B[k][j]; + P[i][j] = P[i][k] + P[k][j] - p[k - 1].cost; + if (i == k || k == j) + { + P[i][j] += p[k - 1].cost; + } + Path[i][j] = Path[k][j]; + } + else + { + C[i][j] = x; + B[i][j] = B[i][j]; + P[i][j] = P[i][j]; + Path[i][j] = Path[i][j]; + } + } + } + } +} +void PrintPath(int i, int j) +{ + if (i == j) + { + printf("%s", p[i - 1].city); + } + else + { + PrintPath(i, Path[i][j]); + printf(" %s", p[j - 1].city); + } +} +double Cost(int m, int y) +{ + double x; + if (m != y) + { + x = ((10 * (double)C[m][y]) / 100) + (double)C[m][y]; + } + else + { + x = ((10 * (double)p[m - 1].cost) / 100) + (double)p[m - 1].cost; + } + return x; +} +void Cal() +{ + int i, m, d, q, g = 1; + double pp; + ss *x, *y; + qsort(p, N, sizeof(p[0]), com); + for (i = 0; i < N; i++) + { + p[i].index = i + 1; + } + scanf("%d", &m); + while (m--) + { + scanf("%s%s%d", c.city, cc.city, &d); + x = (ss *)bsearch(&c, p, N, sizeof(p[0]), com); + y = (ss *)bsearch(&cc, p, N, sizeof(p[0]), com); + B[x->index][y->index] = B[y->index][x->index] = d; + P[x->index][y->index] = P[y->index][x->index] = x->cost + y->cost; + Path[x->index][y->index] = x->index; + Path[y->index][x->index] = y->index; + } + Floyd(); + scanf("%d", &q); + while (q--) + { + scanf("%s%s%d", c.city, cc.city, &d); + x = (ss *)bsearch(&c, p, N, sizeof(p[0]), com); + y = (ss *)bsearch(&cc, p, N, sizeof(p[0]), com); + printf("Query #%d\n", g++); + PrintPath(x->index, y->index); + printf("\n"); + pp = Cost(x->index, y->index); + printf("Each passenger has to pay : %.2lf taka\n", pp / (double)d); + } +} +int main() +{ + int kase, i, q = 1; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &N); + Ini(); + for (i = 0; i < N; i++) + { + scanf("%s%d", p[i].city, &p[i].cost); + } + if (q > 1) + { + putchar('\n'); + } + printf("Map #%d\n", q++); + Cal(); + } + return 0; +} diff --git a/10437.cpp b/10437.cpp new file mode 100644 index 0000000..b72668d --- /dev/null +++ b/10437.cpp @@ -0,0 +1,195 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +typedef long long ll; +typedef pair frac; +#define nu first +#define de second// n/d +bool flag; + +ll gcd(ll a, ll b) +{ + if (b == 0) + { + return a; + } + else + { + return gcd(b, a % b); + } +} + +void adjust(frac &a) +{ + if (a.de < 0) + { + a.nu *= -1, a.de *= -1; + } + int val = a.nu < 0 ? a.nu * -1 : a.nu; + int g = gcd(val, a.de); + a.nu /= g; + a.de /= g; + if (a.de == 0) + { + flag = false; + } + return; +} + +frac exp(string &t, int &p); + +frac num(string &t, int &p) +{ + frac ret(0, 0); + if (t[p] == '(') + { + p++; + ret = exp(t, p); + p++; + adjust(ret); + return ret; + } + while (p < t.size() && isdigit(t[p])) + { + ret.nu = ret.nu * 10 + t[p++] - '0'; + } + if (p < t.size() && t[p] == '|') + { + p++; + while (p < t.size() && isdigit(t[p])) + { + ret.de = ret.de * 10 + t[p++] - '0'; + } + } + else + { + ret.de = 1; + } + adjust(ret); + return ret; +} + +frac fact(string &t, int &p) +{ + frac tmp, ret = num(t, p); + while (p < t.size() && flag) + { + if (t[p] == '*') + { + p++; + tmp = num(t, p); + if (flag == false || tmp.de == 0) + { + flag = false; + return ret; + } + ret.nu *= tmp.nu; + ret.de *= tmp.de; + adjust(ret); + } + else if (t[p] == '/') + { + p++; + tmp = num(t, p); + if (flag == false || tmp.de == 0) + { + flag = false; + return ret; + } + ret.nu *= tmp.de; + ret.de *= tmp.nu; + adjust(ret); + } + else + { + break; + } + } + return ret; +} + +frac exp(string &t, int &p) +{ + frac tmp, ret = fact(t, p); + ll a; + while (p < t.size()) + { + if (t[p] == '+') + { + p++; + tmp = fact(t, p); + if (flag == false || tmp.de == 0) + { + flag = false; + return ret; + } + a = ret.nu * tmp.de + ret.de * tmp.nu; + ret.nu = a; + ret.de *= tmp.de; + adjust(ret); + } + else if (t[p] == '-') + { + p++; + tmp = fact(t, p); + if (flag == false || tmp.de == 0) + { + flag = false; + return ret; + } + a = ret.nu * tmp.de - ret.de * tmp.nu; + ret.nu = a; + ret.de *= tmp.de; + adjust(ret); + } + else + { + break; + } + } + return ret; +} + +main() +{ + string t, sol; + int p; + while (getline(cin, t)) + { + sol.clear(); + p = 0; + while (p < t.size()) + { + if (isdigit(t[p]) || t[p] == '|' || + t[p] == '(' || t[p] == ')' || + t[p] == '+' || t[p] == '-' || + t[p] == '*' || t[p] == '/') + { + sol += t[p++]; + } + else + { + p++; + } + } + p = 0; + flag = true; + frac ret = exp(sol, p); + if (flag) + { + cout << ret.nu; + if (ret.nu != 0 && ret.de != 1) + { + cout << '|' << ret.de; + } + cout << endl; + } + else + { + puts("INVALID"); + } + } +} diff --git a/10438.cpp b/10438.cpp new file mode 100644 index 0000000..0a00e0d --- /dev/null +++ b/10438.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +char str[20000], *words[400], *p; +int ptr; +bool compare(int from, int to) +{ + for (int i = from, j = to; i != to; i++, j++) + { + if (i == ptr || j == ptr) + { + return false; + } + if (strcmp(words[i], words[j]) != 0) + { + return false; + } + } + return true; +} +int main() +{ + bool flag; + while (gets(str) != NULL) + { + ptr = 0; + for (p = strtok(str, " \t"); p; p = strtok(NULL, " \t")) + { + words[ptr++] = p; + } + do + { + flag = false; + for (int i = 0; i < ptr; i++) + for (int j = ptr - 1; j > i; j--) + if (compare(i, j)) + { + flag = true; + for (int k = j + j - i, p = j; k < ptr; k++) + { + words[p++] = words[k]; + } + ptr -= (j - i); + } + } while (flag); + for (int i = 0; i < ptr; i++) + { + if (i) + { + putchar(' '); + } + printf("%s", words[i]); + } + puts(""); + } + return 0; +} diff --git a/10439.cpp b/10439.cpp new file mode 100644 index 0000000..b284723 --- /dev/null +++ b/10439.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +double x, y, x1, yy1, x2, y2, x3, y3, a, b, c, d, e, f, r; +double theta[3], pi = 3.1415926535897932384626433832795; +int t; +double GCD(double a, double b) +{ + if (fabs(b) < 6e-3) + { + return a; + } + else + { + return GCD(b, fmod(a, b)); + } +} +int main() +{ + scanf("%d", &t); + for (; t; t--) + { + scanf("%lf %lf %lf %lf %lf %lf", &x1, &yy1, &x2, &y2, &x3, &y3); + a = 2 * (x1 - x2), b = 2 * (yy1 - y2), c = x1 * x1 + yy1 * yy1 - x2 * x2 - y2 * y2; + d = 2 * (x2 - x3), e = 2 * (y2 - y3), f = x2 * x2 + y2 * y2 - x3 * x3 - y3 * y3; + x = (c * e - b * f) / (a * e - b * d); + y = (a * f - c * d) / (a * e - b * d); + theta[0] = atan2(x1 - x, yy1 - y); + theta[1] = atan2(x2 - x, y2 - y); + theta[2] = atan2(x3 - x, y3 - y); + double ans = GCD(fabs(theta[2] - theta[0]), 2 * pi); + ans = GCD(ans, fabs(theta[1] - theta[0])); + printf("%.lf\n", 2 * pi / ans); + } +} diff --git a/1044.cpp b/1044.cpp new file mode 100644 index 0000000..70d7087 --- /dev/null +++ b/1044.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +int N, num[100]; +char dir[100][10]; +int arr[100000]; +int getdir(char c) +{ + switch (c) + { + case 'N': + return 0; + case 'E': + return 1; + case 'S': + return 2; + case 'W': + return 3; + } +} +bool cmpfinish(int st, int nst, int limitlen) +{ + int k; + for (k = 0; k < limitlen; k++) + if (arr[st + k] != (arr[nst + limitlen - 1 - k] + 2) % 4) + { + break; + } + if (k < limitlen) + { + return false; + } + return true; +} + +bool cmpone(int st, int nst, int limitlen, int flag) +{ + //printf("%d %d %d %d\n",st,nst,limitlen,flag); + int i, j, k; + int num0[4], num1[4]; + memset(num0, 0, sizeof(num0)); + memset(num1, 0, sizeof(num1)); + for (i = st, j = nst; i < st + limitlen; i++, j++) + { + num0[arr[i]]++; + num1[(arr[j] + 2) % 4]++; + for (k = 0; k < 4; k++) + if (num0[k] != num1[k]) + { + break; + } + if (k < 4) + { + continue; + } + for (k = 0; k < i - st; k++) + if (arr[st + k] != (arr[j - k] + 2) % 4) + { + break; + } + if (k < i - st) + { + continue; + } + if (flag == 0) + if (cmpone(i + 1, j + 1, st + limitlen - i - 1, 1)) + { + return true; + } + if (cmpfinish(i + 1, j + 1, st + limitlen - i - 1)) + { + return true; + } + } + return false; +} +int main() +{ + int i, j, tcas = 1, st; + while (scanf("%d", &N) && N) + { + for (i = 0; i < N; i++) + { + scanf("%s%d", dir[i], &num[i]); + } + int div = num[0]; + for (i = 1; i < N; i++) + { + div = __gcd(div, num[i]); + } + int tot = 0; + for (i = 0; i < N; i++) + { + num[i] /= div; + int c = getdir(dir[i][0]); + for (j = 0; j < num[i]; j++) + { + arr[tot++] = c; + } + } + printf("Polygon %d: ", tcas++); + for (i = 0; i < tot; i++) + { + arr[tot + i] = arr[i]; + } + // for (i=0;i -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int Test; - cin >> Test; - while( Test-- ){ - int N, T, M; - cin >> N >> T >> M; - int i, arr[M]; - FOI(i, 0, M-1) - cin >> arr[i]; - int trip = 0, time = 0; - for (i = min(N-1, M-1); i < M; i += N){ - time = max(time, arr[i]); +/* +Ferry Loading II +10440 +*/ +int i, j, k, nf, n, m, t; +int ss[2000]; +void Cal() +{ + nf = (m + n - 1) / n; + t *= 2; + k = m - (nf - 1) * n; + for (i = 0; i < k; i++) + { + scanf("%d", &ss[0]); + } + for (i = 1; i < nf; i++) + { + for (j = 0; j < n; j++) + { + scanf("%d", &ss[i]); } - time = arr[M - 1]; - trip = ceil( (double)M / (double)N ); - time += (2 * T * trip); - cout << time << " " << trip << endl; + } + for (i = 0; i + 1 < nf; i++) + { + if (ss[i] + t > ss[i + 1]) + { + ss[i + 1] = ss[i] + t; + } + } + printf("%d %d\n", ss[nf - 1] + t / 2, nf); +} +int main() +{ + int kases; + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d%d", &n, &t, &m); + Cal(); } return 0; } diff --git a/10441.cpp b/10441.cpp new file mode 100644 index 0000000..547b9c9 --- /dev/null +++ b/10441.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +typedef struct MAP +{ + int to, v, flag; + MAP *next; +} MAP; +MAP node[1010], *alp[26]; +int n, cou, ind[26], outd[26]; +int beg; +int vis[26][26]; +int pre[26], used[26]; +char words[1010][25]; +void init() +{ + cou = 0; + memset(alp, '\0', sizeof(alp)); + memset(node, '\0', sizeof(node)); + for (int i = 0; i < 26; i++) + { + pre[i] = i; + } + memset(vis, 0, sizeof(vis)); + memset(used, 0, sizeof(used)); + memset(ind, 0, sizeof(ind)); + memset(outd, 0, sizeof(outd)); +} +int isoula() +{ + int i; + int sum = 0, out[26]; + for (i = 0; i < 26; i++) + if (ind[i] != outd[i]) + { + out[sum] = i; + sum++; + } + if (sum == 0) + { + i = 0; + while (!used[i]) + { + i++; + } + beg = i; + return 1; + } + if (sum == 2) + { + int x = out[0], y = out[1]; + if (ind[x] - outd[x] == 1 && outd[y] - ind[y] == 1) + { + beg = y; + return 1; + } + if (ind[y] - outd[y] == 1 && outd[x] - ind[x] == 1) + { + beg = x; + return 1; + } + } + return 0; +} +int find(int x) +{ + while (x != pre[x]) + { + x = pre[x]; + } + return x; +} +int isconnect() +{ + int tmp, i = 0, k; + while (!used[i]) + { + i++; + } + tmp = i; + for (k = i + 1; k < 26; k++) + if (find(k) != tmp && used[k]) + { + return 0; + } + return 1; +} +void Union(int x, int y) +{ + int a = find(x); + int b = find(y); + if (a < b) + { + pre[b] = pre[x] = pre[y] = a; + } + if (b < a) + { + pre[a] = pre[x] = pre[y] = b; + } +} +stack s; +void output(int u) +{ + MAP *head = alp[u]; + while (head != NULL) + { + if (head->flag == 0) + { + head->flag = 1; + output(head->v); + s.push(head->to); + } + head = head->next; + } +} +int cmp(const void *a, const void *b) +{ + char *x = (char *)a; + char *y = (char *)b; + return -strcmp(x, y); +} +int main() +{ + int ncases, i, len; + scanf("%d", &ncases); + while (ncases--) + { + init(); + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%s", words[i]); + } + qsort(words, n, sizeof(words[0]), cmp); + for (i = 0; i < n; i++) + { + len = strlen(words[i]); + int x = words[i][0] - 'a'; + int y = words[i][len - 1] - 'a'; + outd[x]++; + ind[y]++; + used[x] = used[y] = 1; + Union(x, y); + node[cou].to = i; + node[cou].v = y; + node[cou].next = alp[x]; + alp[x] = &node[cou++]; + } + if (!isconnect() || !isoula()) + { + printf("***\n"); + continue; + } + output(beg); + int k = s.top(); + s.pop(); + printf("%s", words[k]); + while (!s.empty()) + { + k = s.top(); + s.pop(); + printf(".%s", words[k]); + } + printf("\n"); + } + return 0; +} diff --git a/10442.cpp b/10442.cpp new file mode 100644 index 0000000..8566155 --- /dev/null +++ b/10442.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int i, j, k, m, n; +char buf[1000]; +int state; +double base, val; +int v[256]; +#define check(x) \ + if (!(x)) \ + { \ + printf("no\n"); \ + goto bad; \ + } +#define DSTART 1 +#define DEC 2 +#define BSTART 3 +#define BASE 4 +#define BDONE 5 + +int main() +{ + for (i = 0; i < 255; i++) + { + v[i] = -1; + } + for (i = 0; i <= 9; i++) + { + v[i + '0'] = i; + } + for (i = 0; i <= 5; i++) + { + v[i + 'a'] = i + 10; + } + scanf("%d", &n); + gets(buf); + while (n--) + { + gets(buf); + state = DSTART; + val = base = 0; + for (i = 0; buf[i]; i++) + { + char c = buf[i]; + switch (state) + { + case DSTART: + { + check(v[c] >= 0 && v[c] <= 9); + val = v[c]; + state = DEC; + break; + } + case DEC: + { + if (c == '#') + { + check(val >= 2 && val <= 16); + base = val; + val = 0; + state = BSTART; + break; + } + check(v[c] >= 0 && v[c] <= 9); + val = val * 10 + v[c]; + break; + } + case BSTART: + { + check(v[c] >= 0 && v[c] < base); + val = v[c]; + state = BASE; + break; + } + case BASE: + { + if (c == '#') + { + state = BDONE; + break; + } + check(v[c] >= 0 && v[c] < base); + val = val * base + v[c]; + break; + } + case BDONE: + { + check(c == '#'); + check(val >= 2 && val <= 16); + base = val; + val = 0; + state = BSTART; + break; + } + } + } + check(state == DEC || state == BDONE); + printf("yes\n"); + bad:; + } + return 0; +} diff --git a/10443.cpp b/10443.cpp new file mode 100644 index 0000000..e5cafb9 --- /dev/null +++ b/10443.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +/*************************** +Title : Rocks Scissors,Paper +No : 10443 +****************************/ +#define MAXN 101 +char MAP1[MAXN][MAXN]; +char MAP2[MAXN][MAXN]; +char E; +int R, C, D; +void enemy(char c) +{ + if (c == 'R') + { + E = 'P'; + } + else if (c == 'S') + { + E = 'R'; + } + else + { + E = 'S'; + } +} +void REPLACE(char ch, int r, int c) +{ + enemy(ch); + int i, j; + j = c + 1; + if ((r >= 0 && r < R) && (j >= 0 && j < C)) + if (MAP2[r][j] == E) + { + MAP1[r][c] = E; + return; + } + j = c - 1; + if (r >= 0 && r < R && j >= 0 && j < C) + if (MAP2[r][j] == E) + { + MAP1[r][c] = E; + return; + } + i = r + 1; + if (i >= 0 && i < R && c >= 0 && c < C) + if (MAP2[i][c] == E) + { + MAP1[r][c] = E; + return; + } + i = r - 1; + if (i >= 0 && i < R && c >= 0 && c < C) + if (MAP2[i][c] == E) + { + MAP1[r][c] = E; + return; + } +} +void COM() +{ + int i, j; + while (D--) + { + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + { + REPLACE(MAP1[i][j], i, j); + } + for (i = 0; i < R; i++) + { + strcpy(MAP2[i], MAP1[i]); + } + } +} +void PRINT() +{ + int i; + for (i = 0; i < R; i++) + { + puts(MAP1[i]); + } +} +int main() +{ + int N, i; + scanf("%d", &N); + while (N--) + { + scanf("%d%d%d", &R, &C, &D); + for (i = 0; i < R; i++) + { + scanf("%s", MAP1[i]); + strcpy(MAP2[i], MAP1[i]); + } + COM(); + PRINT(); + if (N) + { + printf("\n"); + } + } + return 0; +} diff --git a/10444.cpp b/10444.cpp new file mode 100644 index 0000000..6151f53 --- /dev/null +++ b/10444.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +//10444 +//Multi-peg Towers of Hanoi +//Dynamic Programming;Ad hoc +#define MAX 205 + +int T[MAX][MAX]; +int main() +{ + int n = 201, p = 21, t = 0; + for (int i = 0; i <= n; i++) + { + if (i < 31) + { + T[i][3] = (1 << i) - 1; + } + else + { + T[i][3] = INT_MAX;//avoid overflow + } + } + for (int i = 1; i <= n; i++) + { + for (int j = 4; j <= p; j++) + { + if (i < j) + { + T[i][j] = 2 * i - 1; + } + else + { + int minn = INT_MAX; + for (int k = 1; k < i; k++) + { + int value = 2 * T[k][j] + T[i - k][j - 1]; + if (value >= 0)//avoid overflow + { + minn = min(minn, value); + } + } + T[i][j] = minn; + } + } + } + while (cin >> n >> p, n | p) + { + cout << "Case " << ++t << ": " << T[n][p] << endl; + } + return 0; +} diff --git a/10445.cpp b/10445.cpp new file mode 100644 index 0000000..b69982c --- /dev/null +++ b/10445.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +typedef long double elem; +typedef complex point, vec; + +const elem eps = 1.0e-10; +const elem pi = acos(-1.0); + +inline elem deg(elem rad) +{ + return (rad / pi) * 180; +} +bool eq(elem a, elem b) +{ + return abs(b - a) < eps; +} +bool lessthan(elem a, elem b) +{ + return !eq(a, b) && a < b; +} +elem dot(vec a, vec b) +{ + return a.real() * b.real() + a.imag() * b.imag(); +} +elem cross(vec a, vec b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} +elem len(vec v) +{ + return abs(v); +} +elem Radians(vec a, vec b) +{ + return acos(dot(a, b) / (len(a) * len(b))); +} + +bool isRight(vec base, vec isv) +{ + if (eq(cross(base, isv), 0)) + { + return len(isv) < len(base); + } + else + { + return lessthan(cross(base, isv), 0); + } +} +bool isLeft(vec base, vec isv) +{ + if (eq(cross(base, isv), 0)) + { + return len(isv) < len(base); + } + else + { + return lessthan(0, cross(base, isv)); + } +} + +struct Point +{ + int id; + point p; + Point() : p(0, 0) {} + Point(point p) : p(p) {} + bool operator<(const Point &t) const + { + if (p.imag() < t.p.imag()) + { + return true; + } + else if (eq(p.imag(), t.p.imag())) + { + return p.real() < t.p.real(); + } + return false; + } +}; + +int main() +{ + while (true) + { + int n; + elem min_rad = 2 * pi, max_rad = 0; + bool brevclk = true; + cin >> n; + if (n < 3) + { + break; + } + vector vp, tmpvp; + for (int i = 0; i < n; ++i) + { + elem x, y; + cin >> x >> y; + Point p(point(x, y)); + p.id = i; + vp.push_back(p); + } + Point topmost; + topmost = vp[0]; + for (int i = 0; i < n; ++i) + { + if (topmost < vp[i]) + { + topmost = vp[i]; + } + } + //cout << topmost.p << endl; + if (isLeft(vec(topmost.p - vp[(topmost.id - 1 + n) % n].p), vec(vp[(topmost.id + 1) % n].p - topmost.p))) + { + brevclk = true; + } + else + { + brevclk = false; + } + for (int i = 0; i < n; ++i) + { + point p = vp[(i + 1) % n].p; + point a = vp[i].p; + point b = vp[(i + 2) % n].p; + vec pa = a - p; + vec pb = b - p; + elem rad = Radians(pa, pb); + bool rev = brevclk ? isLeft(pa, pb) : isRight(pa, pb); + if (rev) + { + rad = 2 * pi - rad; + } + max_rad = max(max_rad, rad); + min_rad = min(min_rad, rad); + } + printf("%.6Lf %.6Lf\n", deg(min_rad), deg(max_rad)); + } + return 0; +} diff --git a/10446.cpp b/10446.cpp new file mode 100644 index 0000000..a9d3cfb --- /dev/null +++ b/10446.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int n, back; +unsigned long long f[100][100]; + +void init() +{ + int i, j, k; + for (i = 0; i < 61; i++) + { + f[0][i] = 1; + f[1][i] = 1; + } + for (i = 2; i < 61; i++) + for (j = 0; j < 61; j++) + { + f[i][j] = 1; + for (k = 1; k <= j; k++) + { + if (i - k < 0) + { + f[i][j] += f[0][j]; + } + else + { + f[i][j] += f[i - k][j]; + } + } + } +} + +int main() +{ + init(); + int kase = 1; + while (cin >> n >> back) + { + if (n > 60) + { + break; + } + if (n < 0) + { + n = 0; + } + cout << "Case " << kase++ << ": " << f[n][back] << endl; + } + return 0; +} diff --git a/10447.cpp b/10447.cpp new file mode 100644 index 0000000..e3c163e --- /dev/null +++ b/10447.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +int primes[] = {101, 103, 107, 109, 11, 113, 127, 13, 131, 137, 139, 149, 151, 157, 163, 167, 17, + 173, 179, 181, 19, 191, 193, 197, 199, 2, 211, 223, 227, 229, 23, 233, 239, 241, 251, 257, + 263, 269, 271, 277, 281, 283, 29, 293, 3, 31, 37, 41, 43, 47, 5, 53, 59, 61, 67, 7, 71, 73, 79, + 83, 89, 97}; +int p[] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, + 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, + 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293}; + +int f[62], cnt[62], ans[14], ptr, n, t; + +bool DFS(int depth, int sum, int last) +{ + if (f[ptr] == 0 && t % 2 != n % 2) + { + return false; + } + if (depth == t) + { + if (sum == 0) + { + printf("%d", ans[0]); + for (int i = 1; i < depth; i++) + { + printf("+%d", ans[i]); + } + } + return (sum == 0); + } + else + { + for (int i = last; i < 62; i++) + { + if (cnt[i] >= f[i]) + { + continue; + } + if (primes[i] == 2 && sum % 2 == (t - depth) % 2) + { + continue; + } + if (primes[i] == 2 && sum % 2 != (t - depth) % 2) + { + ans[depth] = 2; + cnt[i]++; + if (sum >= 2 && DFS(depth + 1, sum - 2, i + 1)) + { + return true; + } + cnt[i]--; + return false; + } + ans[depth] = primes[i]; + cnt[i]++; + if (sum - primes[i] >= 0 && DFS(depth + 1, sum - primes[i], i)) + { + return true; + } + cnt[i]--; + } + return false; + } +} + +int main() +{ + int B, Q; + scanf("%d", &B); + for (int i = 1; i <= B; i++) + { + printf("Block %d:\n", i); + for (int j = 0; j < 61; j++) + { + int x; + for (x = 0; p[j] != primes[x]; x++) + ; + scanf("%d", &f[x]); + } + for (ptr = 0; primes[ptr] != 2; ptr++) + ; + scanf("%d", &Q); + for (int j = 1; j <= Q; j++) + { + printf("Query %d:\n", j); + scanf("%d %d %d", &n, &t, &f[ptr]); + for (int k = 0; k < 62; k++) + { + cnt[k] = 0; + } + if (!DFS(0, n, 0)) + { + printf("No Solution.\n"); + } + else + { + printf("\n"); + } + } + printf("\n"); + } + return 0; +} diff --git a/10448.cpp b/10448.cpp new file mode 100644 index 0000000..aafe954 --- /dev/null +++ b/10448.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 50; +const int M = 50001; +const int inf = (1 << 21); + +struct Edge +{ + int t, c; +}; +vector edge[N]; +bool vis[N]; +bool findPath(int now, vector &cost, int &ans, int tar) +{ + if (now == tar) + { + return true; + } + if (vis[now]) + { + return false; + } + vis[now] = true; + rep(i, edge[now].size()) + { + if (findPath(edge[now][i].t, cost, ans, tar)) + { + cost.push_back(edge[now][i].c); + ans -= edge[now][i].c; + return true; + } + } + return false; +} + +int dp[50][M]; +int solve(int n, int s, int t, int ans) +{ + rep(i, n) vis[i] = false; + vector cost; + if (!findPath(s, cost, ans, t)) + { + return inf; + } + if (ans < 0 || ans % 2 == 1) + { + return inf; + } + if (ans == 0) + { + return cost.size(); + } + cost.erase(cost.begin()); + if (cost.size() == 0) + { + return inf; + } + ans /= 2; + rep(i, cost.size()) rep(j, ans + 1) dp[i][j] = inf; + dp[0][0] = 0; + rep(i, cost.size()) + { + rep(j, ans + 1) + { + if (j - cost[i] >= 0) + { + dp[i][j] = min(dp[i][j], dp[i][j - cost[i]] + 1); + } + if (i - 1 >= 0) + { + dp[i][j] = min(dp[i - 1][j], dp[i][j]); + } + } + } + if (dp[cost.size() - 1][ans] == inf) + { + return inf; + } + return dp[cost.size() - 1][ans] * 2 + cost.size() + 1; +} + +main() +{ + int te; + cin >> te; + rep(tc, te) + { + if (tc) + { + cout << endl; + } + int n, m; + cin >> n >> m; + rep(i, n) edge[i].clear(); + rep(i, m) + { + int f, t, c; + cin >> f >> t >> c; + f--; + t--; + edge[f].push_back((Edge){ + t, c}); + edge[t].push_back((Edge){ + f, c}); + } + int q; + cin >> q; + rep(i, q) + { + int f, t, c; + cin >> f >> t >> c; + f--; + t--; + int ans = solve(n, f, t, c); + if (ans == inf) + { + cout << "No" << endl; + } + else + { + cout << "Yes " << ans << endl; + } + } + } + return false; +} diff --git a/10449.cpp b/10449.cpp new file mode 100644 index 0000000..34f3f99 --- /dev/null +++ b/10449.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +struct Edge +{ + int to, cost; + Edge *next; +} * g[200]; +int busy[200], dist[200], n, m; +bool label[200], visited[200]; +void addEdge(int f, int t, int c) +{ + Edge *p = new Edge; + p->to = t; + p->cost = c; + p->next = g[f]; + g[f] = p; +} +void freemem(Edge *&p) +{ + if (p) + { + freemem(p->next); + delete p; + } +} +void DFS(int s) +{ + visited[s] = true; + for (Edge *p = g[s]; p; p = p->next) + if (!visited[p->to]) + { + label[p->to] = true; + DFS(p->to); + } +} +void BellmanFord() +{ + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + for (Edge *p = g[j]; p; p = p->next) + if (dist[j] != 999999999 && dist[p->to] > dist[j] + p->cost) + { + dist[p->to] = dist[j] + p->cost; + if (i == n - 1) + { + label[j] = label[p->to] = true; + } + } + for (int i = 0; i < n; i++) + if (label[i] && !visited[i]) + { + DFS(i); + } + for (int i = 0; i < n; i++) + if (dist[i] == 999999999 || label[i]) + { + dist[i] = 0; + } +} +int main() +{ + int caseno = 1; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%d", &busy[i]); + } + for (int i = 0; i < n; i++) + { + dist[i] = (i == 0) ? 0 : 999999999; + g[i] = NULL; + label[i] = visited[i] = false; + } + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + int a, b, c; + scanf("%d %d", &a, &b); + a--; + b--; + c = busy[b] - busy[a]; + addEdge(a, b, c * c * c); + } + BellmanFord(); + printf("Set #%d\n", caseno++); + int q; + scanf("%d", &q); + while (q--) + { + int node; + scanf("%d", &node); + if (label[node - 1] || dist[node - 1] < 3) + { + printf("?\n"); + } + else + { + printf("%d\n", dist[node - 1]); + } + } + for (int i = 0; i < n; i++) + { + freemem(g[i]); + } + } + return 0; +} diff --git a/1045.cpp b/1045.cpp new file mode 100644 index 0000000..e1da2e2 --- /dev/null +++ b/1045.cpp @@ -0,0 +1,217 @@ +#include + +using namespace std; + +const int N = 40000; +const int MAXE = 2000; +const int inf = 1 << 30; +int ha[15], li[15], n; +struct pp +{ + int x, y; +} a[20]; + +int head[N], s, t, cnt; +int d[N], pre[N]; +bool vis[N]; +int q[MAXE]; +struct Edge +{ + int u, v, c, w, next; +} edge[MAXE]; +void add(int u, int v, int w, int c) +{ + edge[cnt].u = u; + edge[cnt].v = v; + edge[cnt].w = w; + edge[cnt].c = c; + edge[cnt].next = head[u]; + head[u] = cnt++; + edge[cnt].v = u; + edge[cnt].u = v; + edge[cnt].w = -w; + edge[cnt].c = 0; + edge[cnt].next = head[v]; + head[v] = cnt++; +} +int SPFA() +{ + int l, r; + memset(pre, -1, sizeof(pre)); + memset(vis, 0, sizeof(vis)); + for (int i = 0; i <= t; i++) + { + d[i] = inf; + } + d[s] = 0; + l = 0; + r = 0; + q[r++] = s; + vis[s] = 1; + while (l < r) + { + int u = q[l++]; + vis[u] = 0; + for (int j = head[u]; j != -1; j = edge[j].next) + { + int v = edge[j].v; + if (edge[j].c > 0 && d[u] + edge[j].w < d[v]) + { + d[v] = d[u] + edge[j].w; + pre[v] = j; + if (!vis[v]) + { + vis[v] = 1; + q[r++] = v; + } + } + } + } + if (d[t] == inf) + { + return 0; + } + return 1; +} +int MCMF() +{ + int flow = 0; + int ans = 0; + while (SPFA()) + { + ans += d[t]; + int u = t; + int mini = inf; + while (u != s) + { + if (edge[pre[u]].c < mini) + { + mini = edge[pre[u]].c; + } + u = edge[pre[u]].u; + } + flow += mini; + u = t; + while (u != s) + { + edge[pre[u]].c -= mini; + edge[pre[u] ^ 1].c += mini; + u = edge[pre[u]].u; + } + } + return ans; +} + +int cc(int x, int y, int x1, int y1) +{ + return abs(x - x1) + abs(y - y1); +} + +int main() +{ + int i, j, mi, sum, ans, cas = 0, k; + while (scanf("%d", &n) != EOF) + { + cas++; + if (n == 0) + { + break; + } + memset(ha, 0, sizeof(ha)); + memset(li, 0, sizeof(li)); + for (i = 0; i < n; i++) + { + scanf("%d%d", &a[i].x, &a[i].y); + } + mi = 1000000000; + s = 0; + t = 2 * n + 1; + for (i = 1; i <= n; i++) + { + memset(head, -1, sizeof(head)); + cnt = 0; + for (j = 1; j <= n; j++) + { + add(s, j, 0, 1); + add(j + n, t, 0, 1); + } + for (j = 1; j <= n; j++) + { + for (k = 0; k < n; k++) + { + int cost = cc(a[k].x, a[k].y, i, j); + add(k + 1, j + n, cost, 1); + } + } + ans = MCMF(); + if (ans < mi) + { + mi = ans; + } + } + for (i = 1; i <= n; i++) + { + memset(head, -1, sizeof(head)); + cnt = 0; + for (j = 1; j <= n; j++) + { + add(s, j, 0, 1); + add(j + n, t, 0, 1); + } + for (j = 1; j <= n; j++) + { + for (k = 0; k < n; k++) + { + int cost = cc(a[k].x, a[k].y, j, i); + add(k + 1, j + n, cost, 1); + } + } + ans = MCMF(); + if (ans < mi) + { + mi = ans; + } + } + memset(head, -1, sizeof(head)); + cnt = 0; + for (i = 1; i <= n; i++) + { + add(s, i, 0, 1); + add(i + n, t, 0, 1); + } + for (i = 0; i < n; i++) + { + for (j = 1; j <= n; j++) + { + int cost = cc(a[i].x, a[i].y, j, j); + add(i + 1, j + n, cost, 1); + } + } + ans = MCMF(); + if (ans < mi) + { + mi = ans; + } + memset(head, -1, sizeof(head)); + cnt = 0; + for (i = 1; i <= n; i++) + { + add(s, i, 0, 1); + add(i + n, t, 0, 1); + } + for (i = 0; i < n; i++) + { + for (j = 1; j <= n; j++) + { + int cost = cc(a[i].x, a[i].y, j, n + 1 - j); + add(i + 1, j + n, cost, 1); + } + } + ans = MCMF(); + if (ans < mi) + { + mi = ans; + } + printf("Board %d: %d moves required.\n\n", cas, mi); + } +} diff --git a/10450.cpp b/10450.cpp new file mode 100644 index 0000000..b28d87a --- /dev/null +++ b/10450.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int fibonacci[55][55]; + +int main() +{ + memset(fibonacci, 0, sizeof(fibonacci)); + fibonacci[1][0] = 1; + fibonacci[1][1] = 2; + fibonacci[2][0] = 1; + fibonacci[2][1] = 3; + int i, j, jinwei, tmp; + for (i = 3; i < 55; i++) + { + jinwei = 0; + fibonacci[i][0] = fibonacci[i - 1][0]; + for (j = 1; j <= fibonacci[i - 1][0]; j++) + { + tmp = fibonacci[i - 2][j] + fibonacci[i - 1][j] + jinwei; + if (tmp > 9) + { + fibonacci[i][j] = tmp % 10; + jinwei = 1; + } + else + { + jinwei = 0; + fibonacci[i][j] = tmp % 10; + } + } + if (jinwei == 1) + { + fibonacci[i][j] = 1; + fibonacci[i][0]++; + } + } + //begin input cases; + int n, t, kase = 1; + cin >> n; + for (t = 0; t < n; t++) + { + cout << "Scenario #" << kase++ << ":" << endl; + cin >> i; + for (j = fibonacci[i][0]; j > 0; j--) + { + cout << fibonacci[i][j]; + } + cout << '\n' + << endl; + } + return 0; +} diff --git a/10451.cpp b/10451.cpp new file mode 100644 index 0000000..77f9e1f --- /dev/null +++ b/10451.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int n; +double A; +int kase = 1; + +const double PI = acos(-1.0); + +int main() +{ + while (scanf("%d%lf", &n, &A) == 2) + { + if (n < 3) + { + break; + } + double R, r; + R = sqrt(2.0 * A / n / (sin(2 * PI / n))); + r = R * cos(PI / n); + printf("Case %d: %.5lf %.5lf\n", kase++, PI * R * R - A, A - PI * r * r); + } + return 0; +} diff --git a/10452.cpp b/10452.cpp new file mode 100644 index 0000000..5ab7b34 --- /dev/null +++ b/10452.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int T, Y, X, sx, sy, path[100], top = 0; +char m[20][20], vis[20][20], ans[3][10] = {"left", "forth", "right"}; +int dy[3] = {0, -1, 0}, dx[3] = {-1, 0, 1}; +string letters = "@IEHOVA#"; + +void dfs(int y, int x) +{ + if (top == 7) + for (int i = 0; i < top; i++) + { + printf(i ? " " : ""), printf("%s", ans[path[i]]), printf(i == top - 1 ? "\n" : ""); + } + for (int d = 0; d < 3; d++) + { + int cy = y + dy[d], cx = x + dx[d]; + if (cx > X || cy > Y || cy <= 0 || cx <= 0) + { + continue; + } + if (vis[cy][cx] || letters[top + 1] != m[cy][cx]) + { + continue; + } + vis[cy][cx] = 1, path[top++] = d; + dfs(cy, cx); + vis[cy][cx] = 0, top--; + } +} + +int main() +{ + memset(vis, 0, sizeof vis); + scanf("%d\n", &T); + while (T-- && scanf("%d %d\n", &Y, &X) != EOF) + { + for (int y = 1; y <= Y; y++, getchar()) + { + for (int x = 1; x <= X; x++) + { + m[y][x] = getchar(); + if (m[y][x] == '@') + { + sy = y, sx = x; + } + } + } + vis[sy][sx] = 1; + dfs(sy, sx); + vis[sy][sx] = 0; + } + return 0; +} diff --git a/10453.cpp b/10453.cpp new file mode 100644 index 0000000..67c06e4 --- /dev/null +++ b/10453.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +/* +10739 +String to Palindrome +*/ +#define MAXN 1002 +struct ss +{ + int cost, cmd; + int pi, pj; +} Cost[MAXN][MAXN]; +int len, ft, re; +char Str[MAXN]; +char F[MAXN], R[MAXN]; +int recur(int r, int c) +{ + int x; + if (r >= c) + { + return 0; + } + if (Cost[r][c].cost) + { + return Cost[r][c].cost; + } + if (Str[r] == Str[c]) + { + Cost[r][c].cost = recur(r + 1, c - 1); + Cost[r][c].cmd = 3; + Cost[r][c].pi = r + 1; + Cost[r][c].pj = c - 1; + } + else + { + Cost[r][c].cost = recur(r, c - 1) + 1; + Cost[r][c].cmd = 1; + Cost[r][c].pi = r; + Cost[r][c].pj = c - 1; + x = recur(r + 1, c) + 1; + if (x < Cost[r][c].cost) + { + Cost[r][c].cost = x; + Cost[r][c].cmd = 2; + Cost[r][c].pi = r + 1; + Cost[r][c].pj = c; + } + } + return Cost[r][c].cost; +} +int Gen(int r, int c) +{ + if (r == c) + { + F[ft++] = Str[r]; + return 1; + } + if (Cost[r][c].cmd == 1) + { + F[ft++] = Str[c]; + R[re++] = Str[c]; + } + else if (Cost[r][c].cmd == 2) + { + F[ft++] = Str[r]; + R[re++] = Str[r]; + } + else if (Cost[r][c].cmd == 3) + { + F[ft++] = Str[r]; + R[re++] = Str[r]; + if (c == r + 1) + { + return 1; + } + } + if (Gen(Cost[r][c].pi, Cost[r][c].pj)) + { + return 1; + } + return 0; +} +void Cal() +{ + int i; + len = strlen(Str) - 1; + printf("%d ", recur(0, len)); + ft = re = 0; + Gen(0, len); + for (i = 0; i < ft; i++) + { + printf("%c", F[i]); + } + for (i = re - 1; i >= 0; i--) + { + printf("%c", R[i]); + } + printf("\n"); +} +void Free() +{ + int i, j; + for (i = 0; i <= len; i++) + for (j = 0; j <= len; j++) + { + Cost[i][j].cost = 0; + } +} +int main() +{ + while (gets(Str)) + { + Cal(); + Free(); + } + return 0; +} diff --git a/10454.cpp b/10454.cpp new file mode 100644 index 0000000..e1f4f32 --- /dev/null +++ b/10454.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +unsigned long long int table[130][130]; +char str[300]; +unsigned long long int count(int left, int right) +{ + if (left == right) + { + return 1ull; + } + if (table[left][right]) + { + return table[left][right]; + } + bool flag1 = false, flag2 = false, flag3; + unsigned long long int ans = 0ull; + int o = 0; + for (int p = right; p >= left; p--) + { + if (str[p] == ')') + { + o++; + } + if (str[p] == '(') + { + o--; + } + if (o == 0 && str[p] == '+') + { + flag1 = true; + } + if (o == 0 && str[p] == '*') + { + flag2 = true; + } + } + flag3 = flag1 && flag2; + if (flag1 || flag3) + { + o = 0; + for (int p = right; p >= left; p--) + { + if (str[p] == ')') + { + o++; + } + if (str[p] == '(') + { + o--; + } + if (o == 0 && str[p] == '+') + { + ans += count(left, p - 1) * count(p + 1, right); + } + } + } + else + { + o = 0; + for (int p = right; p >= left; p--) + { + if (str[p] == ')') + { + o++; + } + if (str[p] == '(') + { + o--; + } + if (o == 0 && str[p] == '*') + { + ans += count(left, p - 1) * count(p + 1, right); + } + } + } + table[left][right] = ans; + if (ans) + { + return ans; + } + table[left][right] = count(left + 1, right - 1); + return table[left][right]; +} +int main() +{ + int len; + while (gets(str) != NULL) + { + memset(table, 0, sizeof(table)); + len = strlen(str); + if (len == 0) + { + break; + } + printf("%llu\n", count(0, len - 1)); + } + return 0; +} diff --git a/10456.cpp b/10456.cpp new file mode 100644 index 0000000..4a35251 --- /dev/null +++ b/10456.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int x[31], y[31], x1, y1_, n, tx[31], ty[31], len, totalarea; + +int GCD(int a, int b) +{ + return (b == 0) ? a : GCD(b, a % b); +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%d %d", x + i, y + i); + } + x[n] = x[0]; + y[n] = y[0]; + totalarea = 0; + for (int i = 0; i < n; i++) + { + totalarea += x[i] * y[i + 1] - x[i + 1] * y[i]; + } + int k; + scanf("%d", &k); + while (k--) + { + scanf("%d %d", &x1, &y1_); + int p = 0; + len = 0; + for (int i = n - 1; i >= 0; i--) + if ((x[i + 1] - x[i]) * (y1_ - y[i]) == (x1 - x[i]) * (y[i + 1] - y[i])) + { + p = i; + } + tx[len] = x1; + ty[len++] = y1_; + tx[len] = x[p + 1]; + ty[len++] = y[p + 1]; + for (int i = (p + 2) % n; i != p; i = (i + 1) % n) + { + tx[len] = x[i]; + ty[len++] = y[i]; + tx[len] = x1; + ty[len] = y1_; + int temp = 0; + for (int j = 0; j < len; j++) + { + temp += tx[j] * ty[j + 1] - tx[j + 1] * ty[j]; + } + if (2 * temp < totalarea) + { + continue; + } + int lx = x[(i + n - 1) % n], ly = y[(i + n - 1) % n]; + int a = x[i] - lx, b = y[i] - ly; + int la = 0, lb = 0; + for (int j = 0; j < len; j++) + { + if (j < len - 2) + { + la += tx[j] * ty[j + 1] - tx[j + 1] * ty[j]; + } + else if (j == len - 2) + { + //la+=lx*ly-ly*lx; + lb += lx * b - ly * a; + } + else + { + la += lx * ty[0] - ly * tx[0]; + lb += a * ty[0] - b * tx[0]; + } + } + la = (totalarea - 2 * la); + int nx = lx * 2 * lb + a * la; + int fx = 2 * lb; + int d = GCD(nx, fx); + nx /= d; + fx /= d; + if (fx < 0) + { + nx *= -1; + fx *= -1; + } + printf("(%d/%d),", nx, fx); + int ny = ly * 2 * lb + b * la; + int fy = 2 * lb; + int dd = GCD(ny, fy); + ny /= dd; + fy /= dd; + if (fy < 0) + { + ny *= -1; + fy *= -1; + } + printf("(%d/%d)\n", ny, fy); + break; + } + } + } + return 0; +} diff --git a/10457.cpp b/10457.cpp new file mode 100644 index 0000000..b653b2b --- /dev/null +++ b/10457.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +struct Edge +{ + int a, b, c; +} e[1000]; +int compare(const void *a, const void *b) +{ + return ((Edge *)a)->c - ((Edge *)b)->c; +} +int p[201], m, n, q, s, d; +int find(int s) +{ + while (p[s] > 0) + { + s = p[s]; + } + return s; +} +void unions(int a, int b) +{ + int pa = find(a), pb = find(b); + if (p[pa] > p[pb]) + { + p[pb] = +p[pa], p[pa] = pb; + } + else + { + p[pa] += p[pb], p[pb] = pa; + } +} +int run() +{ + int min = 99999999; + for (int i = 0; i < m; i++) + { + for (int j = 1; j <= n; j++) + { + p[j] = -1; + } + for (int j = i; j < m; j++) + { + if (e[j].c - e[i].c > min) + { + break; + } + if (find(e[j].a) == find(e[j].b)) + { + continue; + } + unions(e[j].a, e[j].b); + if (find(s) == find(d)) + { + min = e[j].c - e[i].c; + break; + } + } + } + return min; +} +int main() +{ + int k1, k2; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].c); + } + qsort(e, m, sizeof(Edge), compare); + scanf("%d %d", &k1, &k2); + scanf("%d", &q); + for (int i = 0; i < q; i++) + { + scanf("%d %d", &s, &d); + printf("%d\n", run() + k1 + k2); + } + } + return 0; +} diff --git a/10458.cpp b/10458.cpp new file mode 100644 index 0000000..8d72be5 --- /dev/null +++ b/10458.cpp @@ -0,0 +1,359 @@ +#include + +using namespace std; + +typedef long long ll; +const int MAXN = 1005; + +struct bign +{ + int len; + ll num[MAXN]; + + bign() + { + len = 0; + memset(num, 0, sizeof(num)); + } + bign(ll number) + { + *this = number; + } + bign(const char *number) + { + *this = number; + } + + void DelZero(); + void Put(); + + void operator=(ll number); + void operator=(char *number); + + bool operator<(const bign &b) const; + bool operator>(const bign &b) const + { + return b < *this; + } + bool operator<=(const bign &b) const + { + return !(b < *this); + } + bool operator>=(const bign &b) const + { + return !(*this < b); + } + bool operator!=(const bign &b) const + { + return b < *this || *this < b; + } + bool operator==(const bign &b) const + { + return !(b != *this); + } + + void operator++(); + void operator--(); + bign operator+(const int &b); + bign operator+(const bign &b); + bign operator-(const int &b); + bign operator-(const bign &b); + bign operator*(const ll &b); + bign operator*(const bign &b); + bign operator/(const ll &b); + //bign operator/(const bign& b); + int operator%(const int &b); +}; + +/*Code*/ + +int k; +long long n, f[10]; + +int bitcount(int x) +{ + return x == 0 ? 0 : bitcount(x >> 1) + (x & 1); +} + +bign C(long long n, long long m) +{ + bign ans = 1; + for (long long i = 0; i < m; i++) + { + ans = ans * (n - i) / (i + 1); + } + return ans; +} + +int main() +{ + while (~scanf("%d%lld", &k, &n)) + { + long long l, r; + for (int i = 0; i < k; i++) + { + scanf("%lld%lld", &l, &r); + f[i] = r - l; + n -= l; + } + bign ans1 = 0LL, ans2 = 0LL; + for (int i = 0; i < (1 << k); i++) + { + long long s = n; + for (int j = 0; j < k; j++) + { + if (i & (1 << j)) + { + s -= f[j] + 1; + if (s < 0) + { + break; + } + } + } + if (s < 0) + { + continue; + } + if (bitcount(i) & 1) + { + ans2 = ans2 + C(s + k - 1, k - 1); + } + else + { + ans1 = ans1 + C(s + k - 1, k - 1); + } + } + (ans1 - ans2).Put(); + printf("\n"); + } + return 0; +} + +/*********************************************/ + +void bign::DelZero() +{ + while (len && num[len - 1] == 0) + { + len--; + } + if (len == 0) + { + num[len++] = 0; + } +} + +void bign::Put() +{ + for (int i = len - 1; i >= 0; i--) + { + printf("%lld", num[i]); + } +} + +void bign::operator=(char *number) +{ + len = strlen(number); + for (int i = 0; i < len; i++) + { + num[i] = number[len - i - 1] - '0'; + } + DelZero(); +} + +void bign::operator=(ll number) +{ + len = 0; + while (number) + { + num[len++] = number % 10; + number /= 10; + } + DelZero(); +} + +bool bign::operator<(const bign &b) const +{ + if (len != b.len) + { + return len < b.len; + } + for (int i = len - 1; i >= 0; i--) + if (num[i] != b.num[i]) + { + return num[i] < b.num[i]; + } + return false; +} + +void bign::operator++() +{ + int s = 1; + for (int i = 0; i < len; i++) + { + s = s + num[i]; + num[i] = s % 10; + s /= 10; + if (!s) + { + break; + } + } + while (s) + { + num[len++] = s % 10; + s /= 10; + } +} + +void bign::operator--() +{ + if (num[0] == 0 && len == 1) + { + return; + } + int s = -1; + for (int i = 0; i < len; i++) + { + s = s + num[i]; + num[i] = (s + 10) % 10; + if (s >= 0) + { + break; + } + } + DelZero(); +} + +bign bign::operator+(const int &b) +{ + bign a = b; + return *this + a; +} + +bign bign::operator+(const bign &b) +{ + int bignSum = 0; + bign ans; + for (int i = 0; i < len || i < b.len; i++) + { + if (i < len) + { + bignSum += num[i]; + } + if (i < b.len) + { + bignSum += b.num[i]; + } + ans.num[ans.len++] = bignSum % 10; + bignSum /= 10; + } + while (bignSum) + { + ans.num[ans.len++] = bignSum % 10; + bignSum /= 10; + } + return ans; +} + +bign bign::operator-(const int &b) +{ + bign a = b; + return *this - a; +} + +bign bign::operator-(const bign &b) +{ + ll bignSub = 0; + bign ans; + for (int i = 0; i < len || i < b.len; i++) + { + bignSub += num[i]; + if (i < b.len) + { + bignSub -= b.num[i]; + } + ans.num[ans.len++] = (bignSub + 10) % 10; + if (bignSub < 0) + { + bignSub = -1; + } + else + { + bignSub = 0; + } + } + ans.DelZero(); + return ans; +} + +bign bign::operator*(const ll &b) +{ + ll bignSum = 0; + bign ans; + ans.len = len; + for (int i = 0; i < len; i++) + { + bignSum += num[i] * b; + ans.num[i] = bignSum % 10; + bignSum /= 10; + } + while (bignSum) + { + ans.num[ans.len++] = bignSum % 10; + bignSum /= 10; + } + return ans; +} + +bign bign::operator*(const bign &b) +{ + bign ans; + ans.len = 0; + for (int i = 0; i < len; i++) + { + int bignSum = 0; + for (int j = 0; j < b.len; j++) + { + bignSum += num[i] * b.num[j] + ans.num[i + j]; + ans.num[i + j] = bignSum % 10; + bignSum /= 10; + } + ans.len = i + b.len; + while (bignSum) + { + ans.num[ans.len++] = bignSum % 10; + bignSum /= 10; + } + } + return ans; +} + +bign bign::operator/(const ll &b) +{ + bign ans; + ll s = 0; + for (int i = len - 1; i >= 0; i--) + { + s = s * 10 + num[i]; + ans.num[i] = s / b; + s %= b; + } + ans.len = len; + ans.DelZero(); + return ans; +} + +int bign::operator%(const int &b) +{ + bign ans; + int s = 0; + for (int i = len - 1; i >= 0; i--) + { + s = s * 10 + num[i]; + ans.num[i] = s / b; + s %= b; + } + return s; +} diff --git a/10459.cpp b/10459.cpp new file mode 100644 index 0000000..c1a4a36 --- /dev/null +++ b/10459.cpp @@ -0,0 +1,173 @@ +#include + +using namespace std; + +#define MAXN 5002 + +struct ss +{ + int *child; + int deg1, deg2; +}; +ss *node; +int *Worst, N, worst, Max, tw; +int *Leaf, tf; +char Fg[MAXN]; + +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +void ReadCase() +{ + int i, j, v, ad; + tf = 0; + node = new ss[N + 2]; + Worst = new int[N + 2]; + Leaf = new int[N + 2]; + for (i = 1; i <= N; i++) + { + scanf("%d", &ad); + node[i].deg1 = node[i].deg2 = ad; + if (ad == 1) + { + Leaf[tf++] = i; + Fg[i] = 1; + } + node[i].child = new int[ad + 2]; + for (j = 0; j < ad; j++) + { + scanf("%d", &v); + node[i].child[j] = v; + } + } +} +void FindBest() +{ + int i, j = 0, rem, u, v, k; + rem = N; + while (rem != tf) + { + for (i = 0; i < tf; i++) + { + u = Leaf[i]; + rem--; + for (k = 0; k < node[u].deg2; k++) + { + v = node[u].child[k]; + if (Fg[v]) + continue; + node[v].deg1--; + if (node[v].deg1 == 1) + { + Leaf[j++] = v; + Fg[v] = 1; + } + } + } + tf = j; + j = 0; + } + qsort(Leaf, tf, sizeof(int), com); + printf("Best Roots :"); + for (i = 0; i < tf; i++) + { + printf(" %d", Leaf[i]); + } + printf("\n"); + memset(Fg, 0, MAXN); +} +void FindWorst1(int n, int level) +{ + int i, u; + Fg[n] = 1; + if (node[n].deg2 == 1) + { + if (level > Max) + { + Worst[0] = n; + Max = level; + worst = 1; + } + else if (level == Max) + { + Worst[worst++] = n; + } + return; + } + for (i = 0; i < node[n].deg2; i++) + { + u = node[n].child[i]; + if (Fg[u]) + { + continue; + } + FindWorst1(node[n].child[i], level + 1); + } +} +void FindWorst(int n, int level, int st) +{ + int i, u; + if (node[n].deg2 == 1 && n != st) + { + if (level > Max) + { + if (Fg[n] == 0) + { + Worst[worst] = n; + tw = worst + 1; + } + else + { + tw = worst; + } + Max = level; + Fg[n] = 1; + } + else if (level == Max && Fg[n] == 0) + { + Fg[n] = 1; + Worst[tw++] = n; + } + return; + } + Fg[n] = 1; + for (i = 0; i < node[n].deg2; i++) + { + u = node[n].child[i]; + if (Fg[u] && node[u].deg2 > 1) + { + continue; + } + FindWorst(node[n].child[i], level + 1, st); + } +} +void Cal() +{ + int i; + Max = 0; + FindWorst1(Leaf[0], 0); + memset(Fg, 0, MAXN); + Max = 0; + for (i = 0; i < worst; i++) + Fg[Worst[i]] = 1; + tw = worst; + FindWorst(Worst[0], 0, Worst[0]); + qsort(Worst, tw, sizeof(Worst[0]), com); + printf("Worst Roots :"); + for (i = 0; i < tw; i++) + printf(" %d", Worst[i]); + printf("\n"); + memset(Fg, 0, MAXN); +} + +int main() +{ + while (scanf("%d", &N) == 1) + { + ReadCase(); + FindBest(); + Cal(); + } + return 0; +} diff --git a/1046.cpp b/1046.cpp new file mode 100644 index 0000000..b9751f5 --- /dev/null +++ b/1046.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (int)(n); i++) + +typedef pair PII; + +const int N = 1005; + +int n, m, th, tm_, v[N]; +PII a[N], b[N]; + +bool cmp(PII a, PII b) +{ + return a.first > b.first || (a.first == b.first && a.second > b.second); +} + +int main() +{ + int ri = 0; + while (scanf("%d", &n) && n) + { + int sum = 0; + rep(i, n) + { + scanf("%d%d", &a[i].first, &a[i].second); + sum += a[i].first; + } + sort(a, a + n, cmp); + scanf("%d", &m); + rep(i, m) + { + scanf("%d%d:%d", &b[i].first, &th, &tm_); + b[i].second = (th - 14) * 60 + tm_; + } + memset(v, 0, sizeof(v)); + int res1 = 0, res2 = 0; + rep(i, n) + { + int c = -1; + rep(j, m) + { + if (!v[j] && b[j].first >= a[i].first && b[j].second >= a[i].second) + { + if (c == -1 || b[j].second < b[c].second) + { + c = j; + } + } + } + if (c != -1) + { + v[c] = 1; + res1++; + res2 += a[i].first; + } + } + printf("Trial %d: %d %d\n\n", ++ri, n - res1, sum - res2); + } + return 0; +} diff --git a/10460.cpp b/10460.cpp new file mode 100644 index 0000000..165320f --- /dev/null +++ b/10460.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int numOfCase; +char str[30]; +int str_index; + +class MyString +{ +public: + char buffer[30]; + int len; + +public: + MyString(); + MyString(char *p); + void Insert(int k, char alpha); + void Print() + { + cout << buffer << endl; + } +}; + +MyString::MyString() +{ + memset(buffer, 0, sizeof(buffer)); + len = 0; +} + +MyString::MyString(char *p) +{ + strcpy(buffer, p); + len = strlen(p); +} + +void MyString::Insert(int k, char alpha) +{ + if (k > len) + { + return; + } + int i; + buffer[len + 1] = '\0'; + for (i = len; i > k; i--) + { + buffer[i] = buffer[i - 1]; + } + buffer[k] = alpha; + len++; +} + +MyString Solve(int th, int num) +{ + if (th == 1) + { + char temp[3]; + temp[0] = str[0]; + temp[1] = '\0'; + MyString ms(temp); + return ms; + } + int a, b; + a = (int)ceil(num * 1.0 / th); + b = num % th; + if (!b) + { + b += th; + } + MyString ms = Solve(th - 1, a); + ms.Insert(b - 1, str[th - 1]); + return ms; +} + +int main() +{ + cin >> numOfCase; + while (numOfCase--) + { + cin >> str >> str_index; + int len = strlen(str); + MyString ms = Solve(len, str_index); + ms.Print(); + } + return 0; +} diff --git a/10461.cpp b/10461.cpp new file mode 100644 index 0000000..2380bf5 --- /dev/null +++ b/10461.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +/* +10461 +Difference +*/ + +#define mx 500 +vector V[mx + 2], U[mx + 2]; +int D[mx + 2], tday, Sum; +char col[mx + 2]; +int N; +void DFS1(int n) +{ + int i, u; + col[n] = 1; + tday += D[n]; + for (i = 0; i < V[n].size(); i++) + { + u = V[n][i]; + if (col[u] == 0) + { + DFS1(u); + } + } +} +void DFS2(int n) +{ + int i, u; + col[n] = 1; + tday += D[n]; + for (i = 0; i < U[n].size(); i++) + { + u = U[n][i]; + if (col[u] == 0) + { + DFS2(u); + } + } +} +void Cal(int q) +{ + int n; + while (q--) + { + cin >> n; + tday = 0; + DFS1(n); + DFS2(n); + tday -= D[n]; + cout << (Sum - tday) << endl; + memset(col, 0, mx + 2); + } +} +void Free() +{ + int i; + for (i = 1; i <= N; i++) + { + V[i].clear(); + U[i].clear(); + } +} +int main() +{ + int n, m, u, v, ks = 1; + while (cin >> N >> m) + { + if (!N && !m) + { + break; + } + Sum = 0; + for (n = 1; n <= N; n++) + { + cin >> D[n]; + Sum += D[n]; + } + while (m--) + { + cin >> u >> v; + V[v].push_back(u); + U[u].push_back(v); + } + cin >> v; + cout << "Case #" << ks++ << ":\n"; + Cal(v); + cout << endl; + Free(); + } + return 0; +} diff --git a/10462.cpp b/10462.cpp new file mode 100644 index 0000000..7006e1c --- /dev/null +++ b/10462.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +#define MAXN 102 +#define MAXE 202 + +struct ss +{ + int u, v; + int weight; +} E[MAXE], MST[MAXE]; + +int P[MAXN], rank_[MAXN], N, M; + +int cmp(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return x->weight - y->weight; +} +int Find(int n) +{ + if (n != P[n]) + { + P[n] = Find(P[n]); + } + return P[n]; +} +void Link(int x, int y) +{ + if (rank_[x] > rank_[y]) + { + P[y] = x; + } + else + { + P[x] = y; + if (rank_[x] == rank_[y]) + { + rank_[y]++; + } + } +} +void MakeSet() +{ + int i; + for (i = 0; i < N; i++) + { + rank_[i] = 0; + P[i] = i; + } +} +int mSt(int p) +{ + int sum = 0, i, x, y; + int mstLen = 0; + for (i = 0; i < M && mstLen < N - 1; i++) + { + if (i == p) + { + continue; + } + x = Find(E[i].u); + y = Find(E[i].v); + if (x != y) + { + Link(x, y); + sum += E[i].weight; + mstLen++; + } + } + if (mstLen < N - 1) + { + return -1; + } + return sum; +} +void Cal() +{ + int i, mstLen = 0, x, y; + int f = 1; + int Best_1 = 0, Best_2 = 21474836; + int flag[102]; + MakeSet(); + qsort(E, M, sizeof(ss), cmp); + for (i = 0; i < M && mstLen < N - 1; i++) + { + x = Find(E[i].u); + y = Find(E[i].v); + if (x != y) + { + MST[mstLen++] = E[i]; + flag[mstLen - 1] = i; + Link(x, y); + Best_1 += E[i].weight; + } + } + if (mstLen < N - 1) + { + printf("No way\n"); + return; + } + for (i = 0; i < mstLen; i++) + { + MakeSet(); + x = mSt(flag[i]); + if (x != -1 && x < Best_2) + { + Best_2 = x; + f = 0; + } + } + if (f) + { + printf("No second way\n"); + return; + } + printf("%d\n", Best_2); +} + +int main() +{ + int kase, i, c = 1; + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &N, &M); + for (i = 0; i < M; i++) + { + scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].weight); + E[i].u--; + E[i].v--; + } + printf("Case #%d : ", c++); + Cal(); + } + return 0; +} diff --git a/10463.cpp b/10463.cpp new file mode 100644 index 0000000..c71823d --- /dev/null +++ b/10463.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +/**** +10463 +****/ +#define MAXN 400 +#define MAX(a, b) (a > b ? a : b) +struct nn +{ + char colour; + int move; +} B[MAXN]; +char F[150][250]; +char P[115]; +int X[] = {-3, -3, 3, 3, -1, 1, -1, 1}; +int Y[] = {-1, 1, -1, 1, 3, 3, -3, -3}; +int Q[MAXN], H, T, pp[31]; +int R, C, I; +int Sr, Sc, Tr, Tc; +void Prime_table() +{ + int i, j; + for (i = 2; i * i <= 113;) + { + for (j = i + i; j <= 113; j += i) + { + P[j] = 1; + } + for (i++; P[i]; i++) + ; + } + I = 1; + pp[0] = 2; + for (i = 3; i <= 113; i += 2) + if (P[i] == 0) + { + pp[I++] = i; + } +} +void Push(int n) +{ + Q[H++] = n; + H %= MAXN; +} +int Pop() +{ + int n = Q[T++]; + T %= MAXN; + return n; +} +int IsEmpty() +{ + return H == T; +} +int BFS(int s, int t) +{ + int i, j, k, r, c = 0; + int nr, nc, ss; + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + { + B[c].move = 0; + B[c].colour = 0; + c++; + } + H = T = 0; + B[s].move = 0; + Push(s); + B[s].colour = 1; + while (!IsEmpty()) + { + k = Pop(); + r = k / C; + c = k % C; + j = B[k].move; + for (i = 0; i < 8; i++) + { + nr = r + X[i]; + nc = c + Y[i]; + if (nr >= R || nr < 0 || nc >= C || nc < 0) + { + continue; + } + ss = nr * C + nc; + if (ss == t) + { + return j + 1; + } + if (B[ss].colour != 1) + { + B[ss].move = j + 1; + B[ss].colour = 1; + Push(ss); + } + } + } + return 0; +} +int IDS(int move, int t, int cu, int limit) +{ + int i, ss, r, c, nr, nc; + r = move / C; + c = move % C; + if (move == t && limit == cu) + { + return 1; + } + if (limit == cu) + { + return 0; + } + if (F[limit][move] == 1) + { + return 0; + } + if (B[move].colour == 1) + { + return 0; + } + B[move].colour = 1; + F[limit][move] = 1; + for (i = 0; i < 8; i++) + { + nr = r + X[i]; + nc = c + Y[i]; + if (nr >= R || nr < 0 || nc >= C || nc < 0) + { + continue; + } + ss = nr * C + nc; + if (IDS(ss, t, cu + 1, limit)) + { + return 1; + } + } + B[move].colour = 0; + return 0; +} +void Cal() +{ + int i, s; + int j, k, m; + P[1] = P[0] = 1; + s = BFS(Sr * C + Sc, Tr * C + Tc); + if (!P[s] && s) + { + printf("The knight takes %d prime moves.\n", s); + return; + } + if (s == 0) + { + printf("Destination is not reachable.\n"); + return; + } + for (i = 0; i < I; i++) + { + if (pp[i] > (R * C) / 2) + { + break; + } + m = 0; + for (j = 0; j < R; j++) + { + for (k = 0; k < C; k++) + { + B[m++].colour = 0; + } + } + for (j = 0; j <= pp[i]; j++) + for (k = 0; k <= R * C; k++) + { + F[j][k] = 0; + } + m = IDS(Sr * C + Sc, Tr * C + Tc, 0, pp[i]); + if (m) + { + printf("The knight takes %d prime moves.\n", pp[i]); + return; + } + } + printf("The knight takes %d composite move(s).\n", s); + ; +} +int main() +{ + int kase = 1; + Prime_table(); + while (scanf("%d%d%d%d%d%d", &R, &C, &Sr, &Sc, &Tr, &Tc) == 6) + { + printf("CASE# %d: ", kase++); + if (Sr == Tr && Sc == Tc) + { + printf("The knight takes 0 composite move(s).\n"); + continue; + } + Cal(); + } + return 0; +} diff --git a/10464.cpp b/10464.cpp new file mode 100644 index 0000000..7fff760 --- /dev/null +++ b/10464.cpp @@ -0,0 +1,320 @@ +#include + +using namespace std; + +char num1[3000], num2[3000], dec1[2000], dec2[2000], frk1[2000], frk2[2000]; +char n1[2000], n2[2000]; +int big1, big2; + +int sep() +{ + int i, p1, p2, k, m; + int l1, l2; + k = 0; + m = 0; + strcpy(n1, num1); + strcpy(n2, num2); + l1 = strlen(num1); + l2 = strlen(num2); + if (num1[0] == '-') + { + n1[0] = 'A'; + } + if (num2[0] == '-') + { + n2[0] = 'A'; + } + for (i = 0; n1[i] != '.'; i++) + ; + p1 = i; + for (i = 0; n2[i] != '.'; i++) + ; + p2 = i; + for (i = p1 + 1; i < l1; i++) + { + frk1[k++] = n1[i]; + } + frk1[k] = NULL; + for (i = p2 + 1; i < l2; i++) + { + frk2[m++] = n2[i]; + } + frk2[m] = NULL; + if (k > m) + { + for (i = m; i < k; i++) + { + frk2[i] = 'A'; + } + frk2[i] = NULL; + } + else if (m > k) + { + for (i = k; i < m; i++) + { + frk1[i] = 'A'; + } + frk1[i] = NULL; + } + k = m = 0; + for (i = p1 - 1; i >= 0; i--) + { + dec1[k++] = n1[i]; + } + dec1[k] = NULL; + if (k == 0) + { + dec1[k++] = 'A'; + dec1[k] = NULL; + } + for (i = p2 - 1; i >= 0; i--) + { + dec2[m++] = n2[i]; + } + dec2[m] = NULL; + if (m == 0) + { + dec2[m++] = 'A'; + dec2[m] = NULL; + } + if (k > m) + { + for (i = m; i < k; i++) + { + dec2[i] = 'A'; + } + dec2[i] = NULL; + } + else if (m > k) + { + for (i = k; i < m; i++) + { + dec1[i] = 'A'; + } + dec1[i] = NULL; + } + return 0; +} +int add() +{ + char mid1[3000], mid2[3000]; + int i, j, k, l, m; + int cary, sum; + l = strlen(frk1); + cary = 0; + k = 0; + m = 0; + for (i = l - 1; i >= 0; i--) + { + sum = (frk1[i] % 65) + (frk2[i] % 65) + cary; + mid1[k++] = sum % 10; + cary = sum / 10; + } + for (i = 0; dec1[i]; i++) + { + sum = (dec1[i] % 65) + (dec2[i] % 65) + cary; + mid2[m++] = sum % 10; + cary = sum / 10; + } + if (cary) + { + mid2[m++] = cary; + } + for (i = m - 1; !mid2[i] && i >= 0; i--) + ; + if (i < 0) + { + printf("0"); + } + else + for (j = i; j >= 0; j--) + { + printf("%d", mid2[j]); + } + printf("."); + for (i = 0; !mid1[i]; i++) + ; + if (i >= k) + { + printf("0"); + } + else + for (j = k - 1; j >= i; j--) + { + printf("%d", mid1[j]); + } + printf("\n"); + return 0; +} +int sorter() +{ + int i, j, l, l1; + l = strlen(dec1); + l1 = strlen(frk1); + big1 = 0; + big2 = 0; + for (i = l - 1; i >= 0; i--) + { + if ((dec1[i] % 65) > (dec2[i] % 65)) + { + big1 = 1; + goto done; + } + else if ((dec1[i] % 65) < (dec2[i] % 65)) + { + big2 = 1; + goto done; + } + } + for (i = 0; i < l1; i++) + { + if ((frk1[i] % 65) > (frk2[i] % 65)) + { + big1 = 1; + goto done; + } + else if ((frk1[i] % 65) < (frk2[i] % 65)) + { + big2 = 1; + goto done; + } + } + big1 = 1; +done:; + return 0; +} +int sub() +{ + char dgr[1000], dso[1000], frg[1000], frs[1000]; + char sub1[1000], sub2[1000]; + int i, m, n, d, j, l, cary, k, p; + if (big1) + { + strcpy(dgr, dec1); + strcpy(dso, dec2); + strcpy(frg, frk1); + strcpy(frs, frk2); + if (num1[0] == '-') + { + printf("-"); + } + } + else + { + strcpy(dgr, dec2); + strcpy(dso, dec1); + strcpy(frg, frk2); + strcpy(frs, frk1); + if (num2[0] == '-') + { + printf("-"); + } + } + l = strlen(frg); + cary = 0; + k = 0; + p = 0; + for (i = l - 1; i >= 0; i--) + { + m = frg[i] % 65; + n = (frs[i] % 65) + cary; + if (m < n) + { + m += 10; + cary = 1; + } + else + { + cary = 0; + } + sub1[k++] = m - n; + } + for (i = 0; dgr[i]; i++) + { + m = dgr[i] % 65; + n = (dso[i] % 65) + cary; + if (m < n) + { + m += 10; + cary = 1; + } + else + { + cary = 0; + } + sub2[p++] = m - n; + } + for (i = p - 1; !sub2[i] && i >= 0; i--) + ; + if (i < 0) + { + printf("0"); + } + else + for (j = i; j >= 0; j--) + { + printf("%d", sub2[j]); + } + printf("."); + for (i = 0; !sub1[i]; i++) + ; + if (i >= k) + { + printf("0"); + } + else + for (j = k - 1; j >= i; j--) + { + printf("%d", sub1[j]); + } + printf("\n"); + return 0; +} +int main() +{ + int i, j, kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%s%s", num1, num2); + for (i = 0; num1[i]; i++) + if (isdigit(num1[i])) + { + if (num1[i] > 48) + { + num1[i] = num1[i] - 48; + } + else + { + num1[i] = 'A'; + } + } + for (i = 0; num2[i]; i++) + if (isdigit(num2[i])) + { + if (num2[i] > 48) + { + num2[i] = num2[i] - 48; + } + else + { + num2[i] = 'A'; + } + } + sep(); + if (num1[0] != '-' && num2[0] != '-') + { + add(); + continue; + } + if (num1[0] == '-' && num2[0] == '-') + { + printf("-"); + add(); + continue; + } + sorter(); + sub(); + } + return 0; +} diff --git a/10465.cpp b/10465.cpp new file mode 100644 index 0000000..8e896cb --- /dev/null +++ b/10465.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +/**** +10465 +****/ +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +void Cal(int n, int m, int t) +{ + int max, min, T, rem, B; + int temp, MAXIMUM = 1000000; + max = MAX(n, m); + min = MIN(n, m); + if (t % min == 0) + { + printf("%d\n", t / min); + return; + } + temp = t; + while (temp > 0) + { + if (temp % min == 0) + { + break; + } + T = temp / min + (t - temp) / max; + rem = temp % min + (t - temp) % max; + if (MAXIMUM > rem) + { + MAXIMUM = rem; + B = T; + } + temp -= max; + } + if (temp % min == 0 && temp >= 0) + { + printf("%d\n", temp / min + (t - temp) / max); + } + else + { + printf("%d %d\n", B, MAXIMUM); + } +} +int main() +{ + int n, m, t; + while (scanf("%d%d%d", &n, &m, &t) == 3) + { + if (m == 1 || n == 1) + { + printf("%d\n", t); + continue; + } + Cal(n, m, t); + } + return 0; +} diff --git a/10466.cpp b/10466.cpp new file mode 100644 index 0000000..78590e3 --- /dev/null +++ b/10466.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES// exposes constants, eg. M_PI +const double PI = M_PI; +#define MAXBODIES (50) +typedef struct +{ + double x, y; +} point; +typedef struct +{ + int r, t; +} planet; +planet table[MAXBODIES + 1]; +point res[MAXBODIES + 1]; +void solve(int n, int t) +{ + int centre = 0; + for (int i = 1; i <= n; i++) + { + double angle = ((t % table[i].t) * 2 * PI) / (double)table[i].t; + res[i].x = table[i].r * cos(angle) + res[i - 1].x; + res[i].y = table[i].r * sin(angle) + res[i - 1].y; + centre += table[i].r; + } + for (int i = 1; i < n; i++) + { + printf("%.4lf ", sqrt(res[i].x * res[i].x + res[i].y * res[i].y)); + } + printf("%.4lf\n", sqrt(res[n].x * res[n].x + res[n].y * res[n].y)); +} +int main() +{ + int n, t; + while (scanf("%d %d", &n, &t) != EOF) + { + res[0].x = 0.0; + res[0].y = 0.0; + int ri, ti; + for (int i = 1; i <= n; i++) + { + scanf("%d %d", &ri, &ti); + table[i].r = ri; + table[i].t = ti; + } + solve(n, t); + if (!feof(stdin)) + { + //printf("\n"); + } + } + fflush(stdout); + return 0; +} diff --git a/10467.cpp b/10467.cpp new file mode 100644 index 0000000..def5f08 --- /dev/null +++ b/10467.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +/* @JUDGE_ID: 2905EH 10467 C++ "carfull output!!" */ +//2003/3/26 a_q10467.cpp +//#include + +#define GAP 5 +#define ADDR(x, y) (((y) << 16) | (x)) +#define GETY(y) ((y) >> 16) +#define GETX(x) ((x)&0xFFFF) + +struct PictureStruct +{ + map pos2char; + int width; + void set(int x, int y, char ch) + { + pos2char[ADDR(x, y)] = ch; + } + /* + char get(int x,int y) const + { + map::const_iterator it=pos2char.find(y*65536+x); + if (it==pos2char.end()) return ' '; + return (*it).second; + } + */ + int getFirstLineX() const + { + map::const_iterator it = pos2char.begin(); + return GETX((*it).first); + } + void print() const + { + int y = 0, x = -1; + map::const_iterator it; + for (it = pos2char.begin(); it != pos2char.end(); ++it) + { + int yy = GETY((*it).first); + int xx = GETX((*it).first); + if (y < yy) + { + x = -1; + putchar('\n'); + } + for (int k = xx - x - 1; --k >= 0;) + { + putchar(' '); + } + putchar((*it).second); + y = yy; + x = xx; + } + putchar('\n'); + } + void paint(int x, int y, const PictureStruct &pic) + { + map::const_iterator it; + for (it = pic.pos2char.begin(); it != pic.pos2char.end(); ++it) + { + int yy = GETY((*it).first); + int xx = GETX((*it).first); + pos2char[ADDR((x + xx), (y + yy))] = (*it).second; + } + } +}; + +char line[256]; + +PictureStruct parseExpr(int a, int b); +PictureStruct parseTerm(int a, int b); +PictureStruct parseFactor(int a, int b); + +PictureStruct parseExpr(int a, int b) +{ + PictureStruct ret; + int depth = 0; + for (int i = b; --i >= a;) + { + if (line[i] == ')') + { + depth++; + } + else if (line[i] == '(') + { + depth--; + } + else if (depth == 0 && (line[i] == '+' || line[i] == '-')) + { + PictureStruct left = parseExpr(a, i); + PictureStruct right = parseTerm(i + 1, b); + ret.width = left.width + right.width + GAP; + ret.paint(0, 2, left); + ret.paint(left.width + GAP, 2, right); + int x1 = 0 + left.getFirstLineX(); + int x2 = left.width + GAP + right.getFirstLineX(); + for (int i = x1; i <= x2; i++) + { + ret.set(i, 1, '='); + } + ret.set(left.width + GAP / 2, 0, 'E'); + ret.set(left.width + GAP / 2, 1, '|'); + ret.set(left.width + GAP / 2, 2, line[i]); + return ret; + } + } + PictureStruct c = parseTerm(a, b); + ret.width = c.width; + ret.paint(0, 2, c); + int xx = c.getFirstLineX(); + ret.set(xx, 0, 'E'); + ret.set(xx, 1, '|'); + return ret; +} + +PictureStruct parseTerm(int a, int b) +{ + PictureStruct ret; + int depth = 0; + for (int i = b; --i >= a;) + { + if (line[i] == ')') + { + depth++; + } + else if (line[i] == '(') + { + depth--; + } + else if (depth == 0 && (line[i] == '*' || line[i] == '/')) + { + PictureStruct left = parseTerm(a, i); + PictureStruct right = parseFactor(i + 1, b); + ret.width = left.width + right.width + GAP; + ret.paint(0, 2, left); + ret.paint(left.width + GAP, 2, right); + int x1 = 0 + left.getFirstLineX(); + int x2 = left.width + GAP + right.getFirstLineX(); + for (int i = x1; i <= x2; i++) + { + ret.set(i, 1, '='); + } + ret.set(left.width + GAP / 2, 0, 'T'); + ret.set(left.width + GAP / 2, 1, '|'); + ret.set(left.width + GAP / 2, 2, line[i]); + return ret; + } + } + PictureStruct c = parseFactor(a, b); + ret.width = c.width; + ret.paint(0, 2, c); + int xx = c.getFirstLineX(); + ret.set(xx, 0, 'T'); + ret.set(xx, 1, '|'); + return ret; +} + +PictureStruct parseFactor(int a, int b) +{ + PictureStruct ret; + if (line[a] == '(' && line[b - 1] == ')') + { + PictureStruct c = parseExpr(a + 1, b - 1); + ret.width = c.width + GAP + 1; + ret.paint((GAP + 1) / 2, 2, c); + int xx = c.getFirstLineX() + (GAP + 1) / 2; + for (int k = ret.width; --k >= 0;) + { + ret.set(k, 1, '='); + } + ret.set(xx, 0, 'F'); + ret.set(xx, 1, '|'); + ret.set(0, 2, '('); + ret.set(ret.width - 1, 2, ')'); + return ret; + } + //assert(b==a+1 && line[a]=='i'); + ret.width = 1; + ret.set(0, 0, 'F'); + ret.set(0, 1, '|'); + ret.set(0, 2, 'i'); + return ret; +} + +int main() +{ + int c = 0; + while (gets(line)) + { + //assert(strspn(line,"+-*/()i")==strlen(line)); //yes + if (c++) + { + putchar('\n'); + } + parseExpr(0, strlen(line)).print(); + } + return 0; +} +/*@END_OF_SOURCE_CODE*/ diff --git a/10468.cpp b/10468.cpp new file mode 100644 index 0000000..063b76b --- /dev/null +++ b/10468.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +#define NINE 7.022509503430381 +#define TEN 7.519130890631598 + +int main() +{ + double x; + while (scanf("%lf", &x) != EOF) + { + printf("%.5lf %.5lf\n", NINE * x, TEN * x); + } + return 0; +} diff --git a/10469.cpp b/10469.cpp index 4144aef..a9d96d9 100644 --- a/10469.cpp +++ b/10469.cpp @@ -1,13 +1,13 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - unsigned long long a,b; - while(scanf("%ld%ld",&a,&b)!=EOF){ - unsigned long long c=a^b; - printf("%ld\n",c); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + unsigned int n, m; + while (scanf("%u%u", &n, &m) == 2) + { + printf("%u\n", n ^ m); + } + return 0; +} diff --git a/1047.cpp b/1047.cpp new file mode 100644 index 0000000..0288f47 --- /dev/null +++ b/1047.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +typedef pair pii; + +int read() +{ + int static t; + cin >> t; + return t; +} +int main() +{ + pii x[50]; + for (int Tc = 1, n, m;; ++Tc) + { + n = read(); + m = read(); + if (!n && !m) + { + return 0; + } + for (int i = 0; i < n; ++i) + { + x[i] = pii(1 << i, read()); + } + int t = n + read(); + for (int i = n; i < t; ++i) + { + int o = 0; + for (int k = read(); k--;) + { + o |= 1 << read() - 1; + } + x[i] = pii(o, read()); + for (int k = 0; k < n; ++k) + if (x[i].first & 1 << k) + { + x[k].second -= x[i].second; + } + } + int ans = 0; + unsigned sc = 0; + for (unsigned s = 1; s < 1 << n; ++s) + if (__builtin_popcount(s) == m) + { + int tmp = 0; + for (int i = 0; i < t; ++i) + if (s & x[i].first) + { + tmp += x[i].second; + } + if (tmp > ans) + { + ans = tmp; + sc = s; + } + else if (tmp == ans) + { + for (int j = 0; j < n; ++j) + if ((sc ^ s) & 1 << j) + { + if (s & 1 << j) + { + sc = s; + } + break; + } + } + } + cout << "Case Number " << Tc << endl; + cout << "Number of Customers: " << ans << endl; + cout << "Locations recommended:"; + for (int i = 0; i < n; ++i) + if (sc & 1 << i) + { + cout << " " << 1 + i; + } + cout << endl + << endl; + } +} diff --git a/10471.cpp b/10471.cpp new file mode 100644 index 0000000..39b709c --- /dev/null +++ b/10471.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int k; + while (scanf("%d", &k) == 1) + { + printf("%d %d\n", k, k - 1); + for (int i = 1; i <= k - 1; i++) + { + printf("%d %d\n", i, i + 1); + } + for (int i = 1; i <= k; i++) + { + printf("%d ", i); + } + printf("\n"); + } + return 0; +} diff --git a/10473.cpp b/10473.cpp new file mode 100644 index 0000000..2bd1506 --- /dev/null +++ b/10473.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +long K; +char DIGIT[] = {"0123456789ABCDEF"}; +int A[200]; +char input[50]; +void VALUE() +{ + for (int i = 0; i < 16; i++) + { + A[DIGIT[i]] = i; + } +} +void HEXA() +{ + int p = 0; + long sum = 0; + for (int i = strlen(input) - 1; i >= 2; i--) + { + sum += pow(16, p++) * A[input[i]]; + } + printf("%ld\n", sum); +} +main() +{ + VALUE(); + long k; + while (1) + { + gets(input); + if (input[0] == '-') + { + break; + } + if (input[0] == '0' && (input[1] == 'x' || input[1] == 'X')) + { + HEXA(); + continue; + } + sscanf(input, "%ld", &K); + printf("0x"); + printf("%lX\n", K); + } + return 0; +} diff --git a/10474.cpp b/10474.cpp index 95b2bed..629de36 100644 --- a/10474.cpp +++ b/10474.cpp @@ -1,32 +1,81 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - for(int z=1;;z++){ - int n,query,i; - scanf("%d%d",&n,&query); - if(n==0 && query==0) - break; - int num[n]; - for(i=0;i=0) - printf("%d not found\n",find); - } - } - return 0; -} +#include + +using namespace std; + +int arr[1000000]; + +int qsort_cmp(const void *a, const void *b) +{ + if (*(int *)a > *(int *)b) + { + return 1; + } + else if (*(int *)a == *(int *)b) + { + return 0; + } + else if (*(int *)a < *(int *)b) + { + return -1; + } +} + +int bin_srch(int key, int n) +{ + int low, high, mid; + low = 0; + high = n; + while (low <= high) + { + mid = (low + high) / 2; + if (key == arr[mid]) + { + while (arr[mid] == arr[mid - 1]) + { + mid--; + } + return mid; + } + else if (key < arr[mid]) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } + return -1; +} + +int main() +{ + int N, Q, kase = 1, i, key, tmp; + while (cin >> N >> Q) + { + if (N == 0 && Q == 0) + { + break; + } + cout << "CASE# " << kase++ << ":" << endl; + for (i = 0; i < N; i++) + { + cin >> arr[i]; + } + qsort(arr, N, sizeof(int), qsort_cmp); + for (i = 0; i < Q; i++) + { + cin >> key; + tmp = bin_srch(key, N); + if (tmp < 0) + { + cout << key << " not found" << endl; + } + else + { + cout << key << " found at " << tmp + 1 << endl; + } + } + } + return 0; +} diff --git a/10475.cpp b/10475.cpp new file mode 100644 index 0000000..36db87e --- /dev/null +++ b/10475.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +static char name[15][16], temp[16], graph[15][15]; +static int ans[5], t, p, s; + +int compare(const void *a, const void *b) +{ + const char *c, *d; + int la, lb; + c = (const char *)a; + d = (const char *)b; + la = strlen(c); + lb = strlen(d); + if (la > lb) + { + return -1; + } + else if (la < lb) + { + return 1; + } + else + { + return strcmp(c, d); + } +} +int find(char *s) +{ + for (int i = 0; i < t; i++) + if (strcmp(name[i], s) == 0) + { + return i; + } + return -1; +} +void DFS(int depth, int choose) +{ + ans[depth] = choose; + int i, j; + if (depth == s - 1) + { + for (i = 0; i <= depth; i++) + { + if (i) + { + printf(" "); + } + printf("%s", name[ans[i]]); + } + printf("\n"); + return; + } + for (i = choose + 1; i < t; i++) + { + for (j = 0; j <= depth; j++) + if (!graph[ans[j]][i]) + { + break; + } + if (j == depth + 1) + { + DFS(depth + 1, i); + } + } +} +int main() +{ + int times, i, j, caseno, a, b; + scanf("%d", ×); + for (caseno = 1; caseno <= times; caseno++) + { + printf("Set %d:\n", caseno); + scanf("%d %d %d", &t, &p, &s); + for (i = 0; i < t; i++) + { + scanf("%s", name + i); + for (j = 0; name[i][j]; j++) + if (isalpha(name[i][j])) + { + name[i][j] = toupper(name[i][j]); + } + } + qsort(name, t, 16, compare); + memset(graph, true, sizeof(graph)); + for (i = 0; i < p; i++) + { + scanf("%s", temp); + for (j = 0; temp[j]; j++) + if (isalpha(temp[j])) + { + temp[j] = toupper(temp[j]); + } + a = find(temp); + scanf("%s", temp); + for (j = 0; temp[j]; j++) + if (isalpha(temp[j])) + { + temp[j] = toupper(temp[j]); + } + b = find(temp); + if (a < b) + { + graph[a][b] = false; + } + else + { + graph[b][a] = false; + } + } + for (i = 0; i < t; i++) + { + DFS(0, i); + } + puts(""); + } + return 0; +} diff --git a/10476.cpp b/10476.cpp new file mode 100644 index 0000000..99728d7 --- /dev/null +++ b/10476.cpp @@ -0,0 +1,214 @@ +#include + +using namespace std; + +/* +Spam or Not Spam +10476 +*/ +#define MIN(a, b) (a > b ? b : a) +#define MAX(a, b) (a > b ? a : b) +#define MAXN 884738 +int F_SP[MAXN], F_NON[MAXN], F_MS[MAXN]; +char Text[1002]; +int Max_sp, Max_non, Max_ms; +int min_sp, min_non, min_ms; +int Sp, Non, Ms; +double Sm1, Sm2; +int Val(int a, int b, int c) +{ + int p; + p = a * 96 * 96 + b * 96 + c; + return p; +} +void Similarity_1() +{ + int i, tf = 0, t1f = 0, t2f = 0; + int max = MAX(Max_ms, Max_sp); + int min = MIN(min_ms, min_sp); + for (i = min; i <= max; i++) + { + tf += F_MS[i] * F_SP[i]; + t1f += F_SP[i] * F_SP[i]; + t2f += F_MS[i] * F_MS[i]; + } + if (tf == 0 || t1f == 0 || t2f == 0) + { + Sm1 = 0.00000; + } + else + { + Sm1 = (double)tf / (sqrt((double)t1f) * sqrt((double)t2f)); + } + printf("%.5lf", Sm1); +} +void Similarity_2() +{ + int i, tf = 0, t1f = 0, t2f = 0; + int max = MAX(Max_ms, Max_non); + int min = MIN(min_ms, min_non); + for (i = min; i <= max; i++) + { + tf += F_MS[i] * F_NON[i]; + t1f += F_NON[i] * F_NON[i]; + t2f += F_MS[i] * F_MS[i]; + } + if (tf == 0 || t1f == 0 || t2f == 0) + { + Sm2 = 0.00000; + } + else + { + Sm2 = (double)tf / (sqrt((double)t1f) * sqrt((double)t2f)); + } + printf(" %.5lf\n", Sm2); +} +void ReadSp() +{ + int i, j = 0, l; + int p; + Max_sp = 0; + min_sp = 21474836; + while (j < Sp) + { + gets(Text); + if (!strcmp(Text, "ENDMESSAGE")) + { + j++; + } + else + { + l = strlen(Text); + if (l < 3) + { + continue; + } + Text[l] = NULL; + for (i = 0; i < l - 2; i++) + { + p = Val(Text[i] - 32, Text[i + 1] - 32, Text[i + 2] - 32); + F_SP[p]++; + if (p > Max_sp) + { + Max_sp = p; + } + if (p < min_sp) + { + min_sp = p; + } + } + } + } +} +void ReadNon() +{ + int i, j = 0, l; + int p; + Max_non = 0; + min_non = 21474836; + while (j < Non) + { + gets(Text); + if (!strcmp(Text, "ENDMESSAGE")) + { + j++; + } + else + { + l = strlen(Text); + if (l < 3) + { + continue; + } + Text[l] = NULL; + for (i = 0; i < l - 2; i++) + { + p = Val(Text[i] - 32, Text[i + 1] - 32, Text[i + 2] - 32); + F_NON[p]++; + if (p > Max_non) + { + Max_non = p; + } + if (p < min_non) + { + min_non = p; + } + } + } + } +} +void ReadMs() +{ + int i, j = 0, l; + int p, max, min; + Max_ms = 0; + min_ms = 21474836; + while (j < Ms) + { + gets(Text); + if (!strcmp(Text, "ENDMESSAGE")) + { + j++; + Similarity_1(); + Similarity_2(); + if (Sm1 > Sm2) + { + printf("spam\n"); + } + else + { + printf("non-spam\n"); + } + for (i = min_ms; i <= Max_ms; i++) + { + F_MS[i] = 0; + } + } + else + { + l = strlen(Text); + if (l < 3) + { + continue; + } + Text[l] = NULL; + for (i = 0; i < l - 2; i++) + { + p = Val(Text[i] - 32, Text[i + 1] - 32, Text[i + 2] - 32); + F_MS[p]++; + if (p > Max_ms) + { + Max_ms = p; + } + if (p < min_ms) + { + min_ms = p; + } + } + } + } + max = MAX(Max_sp, Max_non); + min = MIN(min_sp, min_non); + for (i = min; i <= max; i++) + { + F_SP[i] = F_NON[i] = 0; + } +} +int main() +{ + char input[100]; + int Set = 1; + while (gets(input)) + { + sscanf(input, "%d%d%d", &Sp, &Non, &Ms); + if (!Sp && !Non && !Ms) + { + break; + } + ReadSp(); + ReadNon(); + printf("Set %d:\n", Set++); + ReadMs(); + } + return 0; +} diff --git a/10477.cpp b/10477.cpp new file mode 100644 index 0000000..c4b605a --- /dev/null +++ b/10477.cpp @@ -0,0 +1,243 @@ +#include + +using namespace std; + +/* +The Hybrid Knight +10447 +*/ +#define MAX 12500 +#define INF 2147480 +struct Queue +{ + int move; + int pice; + int r, c; +} que[MAX]; +struct Board +{ + int pice[3]; +} B[22][22]; +int qh, qt, Size, Min, tr, tc; +int maximum; +int kx[] = {-2, -2, -1, -1, 2, 2, 1, 1}; +int ky[] = {-1, 1, -2, 2, -1, 1, -2, 2}; +int mx[] = {-3, -3, -1, -1, 3, 3, 1, 1}; +int my[] = {1, -1, -3, 3, -1, 1, -3, 3}; +int dx[] = {1, 1, -1, -1}; +int dy[] = {-1, 1, -1, 1}; +int px[] = {0, 0, -1, 1}; +int py[] = {-1, 1, 0, 0}; +void Ini() +{ + int i, j; + for (i = 0; i <= Size; i++) + { + for (j = 0; j <= Size; j++) + { + B[i][j].pice[0] = B[i][j].pice[1] = B[i][j].pice[2] = 0; + } + } +} +void Push(int pice, int r, int c, int move) +{ + if (B[r][c].pice[pice]) + { + return; + } + B[r][c].pice[pice] = 1; + que[qh].c = c; + que[qh].r = r; + que[qh].pice = pice; + que[qh].move = move; + qh++; + if (qh > maximum) + { + maximum = qh; + } + qh %= MAX; +} +Queue Pop() +{ + int temp = qt; + qt++; + qt %= MAX; + return que[temp]; +} +void PushK(int r, int c, int move) +{ + int i, nr, nc; + for (i = 0; i < 8; i++) + { + nr = kx[i] + r; + nc = ky[i] + c; + if (nr >= Size || nr < 0 || nc >= Size || nc < 0) + { + continue; + } + if (nr == tr && nc == tc) + { + if (Min > move) + { + Min = move; + } + } + else + { + Push(0, nr, nc, move); + } + } +} +void PushM(int r, int c, int move) +{ + int i, nr, nc; + for (i = 0; i < 8; i++) + { + nr = mx[i] + r; + nc = my[i] + c; + if (nr >= Size || nr < 0 || nc >= Size || nc < 0) + { + continue; + } + if (nr == tr && nc == tc) + { + if (Min > move) + { + Min = move; + } + } + else + { + Push(1, nr, nc, move); + } + } +} +void PushP(int r, int c, int move) +{ + int i, nr, nc; + for (i = 0; i < 4; i++) + { + nr = px[i] + r; + nc = py[i] + c; + if (nr >= Size || nr < 0 || nc >= Size || nc < 0) + { + continue; + } + if (nr == tr && nc == tc) + { + if (Min > move) + { + Min = move; + } + } + else + { + Push(2, nr, nc, move); + } + } +} +void PushD(int r, int c, int move) +{ + int i, nr, nc; + for (i = 0; i < 4; i++) + { + nr = dx[i] + r; + nc = dy[i] + c; + if (nr >= Size || nr < 0 || nc >= Size || nc < 0) + { + continue; + } + if (nr == tr && nc == tc) + { + Min = move; + return; + } + } +} +void BFS(int sr, int sc) +{ + Queue temp; + int pice, move; + qh = qt = 0; + Push(0, sr, sc, 0); + Push(1, sr, sc, 0); + Push(2, sr, sc, 0); + Min = INF; + while (qh != qt) + { + temp = Pop(); + pice = temp.pice + 1; + pice %= 3; + move = temp.move + 1; + if (pice == 0) + { + PushK(temp.r, temp.c, move); + } + if (Min != INF) + { + return; + } + if (pice == 1) + { + PushM(temp.r, temp.c, move); + } + if (Min != INF) + { + return; + } + if (pice == 2) + { + PushP(temp.r, temp.c, move); + } + if (Min != INF) + { + return; + } + if (pice == 2) + { + PushD(temp.r, temp.c, move); + } + if (Min != INF) + { + return; + } + } +} +void Cal() +{ + int q, b, e; + scanf("%d", &q); + while (q--) + { + scanf("%d%d", &b, &e); + b--; + e--; + tr = e / Size; + tc = e % Size; + Ini(); + maximum = 0; + BFS(b / Size, b % Size); + if (Min == INF) + { + printf("?\n"); + } + else + { + printf("%d\n", Min); + } + } +} +int main() +{ + int set = 1; + while (scanf("%d", &Size) == 1) + { + if (!Size) + { + break; + } + printf("Set %d:\n", set++); + Cal(); + } + return 0; +} diff --git a/10479.cpp b/10479.cpp new file mode 100644 index 0000000..8ac493a --- /dev/null +++ b/10479.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +unsigned long long int base[64], n; +int temp[9] = {0, 0, 1, 0, 2, 1, 0, 0, 3}; +int main() +{ + base[0] = 1ull; + for (int i = 1; i <= 63; i++) + { + base[i] = base[i - 1] * 2ull; + } + while (scanf("%llu", &n) == 1) + { + if (!n) + { + break; + } + if (n < 9) + { + printf("%d\n", temp[n]); + } + else + { + while (true) + { + if (n < 9) + { + printf("%d\n", temp[n]); + break; + } + int i, j, k, p; + for (i = 1; i <= 63; i++) + if (base[i - 1] <= n && base[i] > n) + { + break; + } + i--; + if (base[i] == n) + { + printf("%d\n", i); + break; + } + unsigned long long int b = base[i], temp = base[i + 1]; + p = i; + for (i--, j = 1; i >= 1; i--, j++) + for (k = 0; k < j; k++) + if (b + base[i] / 2 < n) + { + b += base[i] / 2; + } + else + { + goto end; + } + end: + if (n < temp && n >= temp - p) + { + printf("0\n"); + break; + } + n -= b; + n += base[i - 1]; + } + } + } + return 0; +} diff --git a/1048.cpp b/1048.cpp new file mode 100644 index 0000000..19fed08 --- /dev/null +++ b/1048.cpp @@ -0,0 +1,184 @@ +#include + +using namespace std; + +#define inf 1 << 28 +#define maxn 4000 + +vector ticket[25]; +vector trip; +map city; +int cost[25], n, cnt; + +struct Edge +{ + int from, to, dist; + int id; + Edge(int from_, int to_, int dis_, int iid) + { + from = from_; + to = to_; + dist = dis_; + id = iid; + } +}; +struct HeapNode +{ + int d, u; + HeapNode(int d_, int u_) + { + d = d_; + u = u_; + } + bool operator<(const HeapNode &rhs) const + { + return d > rhs.d; + } +}; +struct Dijkstra +{ + int n, m; + vector edges; + vector G[maxn]; + bool done[maxn]; + int d[maxn]; + int p[maxn]; + + void init(int n_) + { + int i; + n = n_; + for (i = 0; i <= n; i++) + { + G[i].clear(); + } + edges.clear(); + } + + void addedge(int from, int to, int dist, int id) + { + edges.push_back(Edge(from, to, dist, id)); + m = edges.size(); + G[from].push_back(m - 1); + } + + void dijkstra(int s, int t) + { + int i; + priority_queue q; + for (i = 0; i <= n; i++) + { + d[i] = inf; + } + d[s] = 0; + memset(done, 0, sizeof(done)); + q.push(HeapNode(0, s)); + while (!q.empty()) + { + HeapNode x = q.top(); + q.pop(); + int u = x.u; + if (done[u]) + { + continue; + } + done[u] = true; + for (i = 0; i < G[u].size(); i++) + { + Edge &e = edges[G[u][i]]; + if (d[e.to] > d[u] + e.dist) + { + d[e.to] = d[u] + e.dist; + p[e.to] = G[u][i];//最短路的上一æ¡è¾¹ + q.push(HeapNode(d[e.to], e.to)); + } + } + } + stack st; + int cur = t; + while (cur != s) + { + st.push(edges[p[cur]].id); + cur = edges[p[cur]].from; + } + printf("%d\n", d[t]); + printf(" Tickets used: "); + while (!st.empty()) + { + printf("%d", st.top() + 1); + st.pop(); + if (st.empty()) + printf("\n"); + else + printf(" "); + } + } +}; + +Dijkstra solver; +void init() +{ + int i, j; + for (i = 0; i < n; i++) + { + ticket[i].clear(); + } + city.clear(); + cnt = 0; +} + +int main() +{ + int i, j, k, test = 0; + while (scanf("%d", &n) == 1 && n) + { + init(); + int m; + for (i = 0; i < n; i++) + { + scanf("%d", &cost[i]); + scanf("%d", &m); + while (m--) + { + int temp; + scanf("%d", &temp); + if (city.find(temp) == city.end()) + city[temp] = cnt++; + ticket[i].push_back(city[temp]); + } + } + scanf("%d", &m); + int ts = 0; + while (m--) + { + trip.clear(); + int tn; + scanf("%d", &tn); + while (tn--) + { + int temp; + scanf("%d", &temp); + trip.push_back(city[temp]); + } + solver.init(5000); + for (i = 0; i < trip.size(); i++) + { + int vis; + for (j = 0; j < n; j++) + { + vis = i + 1; + for (k = 1; k < ticket[j].size(); k++) + { + if (vis < trip.size() && trip[vis] == ticket[j][k]) + vis++; + solver.addedge(12 * ticket[j][0] + i + 1, 12 * ticket[j][k] + vis, cost[j], j); + } + } + } + printf("Case %d, Trip %d: Cost = ", test + 1, ++ts); + solver.dijkstra(12 * trip[0] + 1, 12 * trip[trip.size() - 1] + trip.size()); + } + test++; + } + return 0; +} diff --git a/10480.cpp b/10480.cpp new file mode 100644 index 0000000..25decee --- /dev/null +++ b/10480.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +/* +10480 +Sabotage +*/ +/* +Multiple Input +*/ +#define maxn 52 + +#define MIN(a, b) (a > b ? b : a) +list Edge[maxn]; +struct ss +{ + int u; + int cost; +}; +class comp +{ +public: + bool operator()(const ss &a, const ss &b) + { + return a.cost < b.cost; + } +}; +priority_queue, comp> Q; +char Color[maxn]; +int MaxF[maxn][maxn]; +int Org[maxn][maxn]; +int P[maxn]; +int N, M; +void Updates(int v, int c) +{ + int u; + if (v == 1) + { + return; + } + u = P[v]; + MaxF[u][v] -= c; + MaxF[v][u] += c; + Updates(P[v], c); +} +int BFS() +{ + ss t, d; + list::iterator p; + int c, v; + t.u = 1; + t.cost = 1000000000; + Q.push(t); + Color[1] = 1; + while (!Q.empty()) + { + t = Q.top(); + Q.pop(); + for (p = Edge[t.u].begin(); p != Edge[t.u].end(); p++) + { + v = *p; + if (Color[v] || !MaxF[t.u][v]) + { + continue; + } + Color[v] = 1; + P[v] = t.u; + c = MIN(t.cost, MaxF[t.u][v]); + if (v == 2) + { + Updates(v, c); + return 1; + } + d.cost = c; + d.u = v; + Q.push(d); + } + } + return 0; +} +void Reset() +{ + int i; + for (i = 1; i <= N; i++) + { + Color[i] = 0; + } + while (!Q.empty()) + { + Q.pop(); + } +} +void Cal() +{ + int i, v; + list::iterator p; + while (BFS()) + { + Reset(); + } + for (i = 1; i <= N; i++) + { + if (Color[i]) + { + for (p = Edge[i].begin(); p != Edge[i].end(); p++) + { + v = *p; + if (Color[v]) + { + continue; + } + cout << i << " " << v << endl; + } + } + } + cout << endl; +} +void Free() +{ + int i; + for (i = 1; i <= N; i++) + { + Edge[i].clear(); + Color[i] = 0; + } +} +int main() +{ + int i, u, v, c; + while (cin >> N >> M) + { + if (!N && !M) + { + break; + } + for (i = 0; i < M; i++) + { + cin >> u >> v >> c; + Org[u][v] = Org[v][u] = c; + MaxF[u][v] = MaxF[v][u] = c; + Edge[u].push_back(v); + Edge[v].push_back(u); + } + Cal(); + Free(); + } + return 0; +} diff --git a/10482.cpp b/10482.cpp new file mode 100644 index 0000000..05b85b2 --- /dev/null +++ b/10482.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +#define MX(a, b) (a > b ? a : b) +#define MN(a, b) (a < b ? a : b) +#define MAXN 342 +#define MAX MAXN*MAXN + +char F[MAXN][MAXN]; +int V[MAXN], min_, ind, N, s1, s2, xxx; + +struct ss {int a, b;} tbl[MAX]; + +void Gen(int n, int half, int sum) +{ + int p, q, i, s, r, a, b; + if (n > half) + { + return; + } + for (i = ind - 1; i >= 0; i--) + { + a = tbl[i].a; + b = tbl[i].b; + p = a + n; + q = b + n; + if (p <= half) + { + if (!F[p][b]) + { + F[p][b] = 1; + F[b][p] = 1; + tbl[ind].a = p; + tbl[ind++].b = b; + s = p + b; + r = sum - s; + s = MX(MX(p, b), r); + r = MN(MN(p, b), r); + if ((s - r) < min_) + { + min_ = s - r; + } + if (min_ == xxx) + { + return; + } + } + } + if (q <= half) + { + if (!F[q][a]) + { + F[q][a] = 1; + F[tbl[i].a][q] = 1; + tbl[ind].b = q; + tbl[ind++].a = a; + s = q + a; + r = sum - s; + s = MX(MX(q, a), r); + r = MN(MN(q, a), r); + if ((s - r) < min_) + { + min_ = s - r; + } + if (min_ == xxx) + { + return; + } + } + } + } +} +void Cal(int sum) +{ + int i, half; + if (N == 1) + { + printf("%d\n", V[0]); + return; + } + ind = 1; + min_ = 1000; + half = sum / 2; + xxx = 0; + if (sum % 3) + { + xxx = 1; + } + for (i = 0; i < N; i++) + { + Gen(V[i], half, sum); + if (min_ == xxx) + { + break; + } + } + printf("%d\n", min_); +} +void Free() +{ + int i; + for (i = 0; i < ind; i++) + { + F[tbl[i].a][tbl[i].b] = F[tbl[i].b][tbl[i].a] = 0; + } +} +int main() +{ + int kases, i, sum, c = 1; + scanf("%d", &kases); + while (kases--) + { + scanf("%d", &N); + sum = 0; + for (i = 0; i < N; i++) + { + scanf("%d", &V[i]); + sum += V[i]; + } + printf("Case %d: ", c++); + if (N == 2) + { + printf("%d\n", MX(V[0], V[1])); + continue; + } + if (N == 3) + { + printf("%d\n", MX(V[0], MX(V[1], V[2])) - MN(V[0], MN(V[1], V[2]))); + continue; + } + Cal(sum); + if (kases) + { + Free(); + } + } + return 0; +} diff --git a/10483.cpp b/10483.cpp new file mode 100644 index 0000000..dab1968 --- /dev/null +++ b/10483.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +double min_, max_; +int p[20], ptr, temp[3]; + +void print(int i, int a, int b, int c) +{ + printf("%.2lf = %.2lf + %.2lf + %.2lf = %.2lf * %.2lf * %.2lf\n", (double)i / 100, + (double)a / 100, (double)b / 100, (double)c / 100, + (double)a / 100, (double)b / 100, (double)c / 100); +} +bool find() +{ + for (int i = 0; i < ptr; i++) + if (p[i] == temp[0]) + { + return true; + } + p[ptr++] = temp[0]; + return false; +} + +int main() +{ + while (scanf("%lf %lf", &min_, &max_) == 2) + { + int min_i = (int)(min_ * 100 + 0.01), max_i = (int)(max_ * 100 + 0.01); + if (min_i > max_i) + { + swap(min_i, max_i); + } + for (int i = min_i; i <= max_i; i += 1) + { + ptr = 0; + for (int j = 1; j * j * j <= i * 10000; j += 1) + { + if (i * 10000 % j != 0) + { + continue; + } + int d = i * 10000 / j, c = i - j; + if (c * c - 4 * d < 0) + { + continue; + } + int sq = (int)sqrt(c * c - 4 * d); + if (sq * sq != (c * c - 4 * d)) + { + continue; + } + if ((c + sq) % 2 != 0) + { + continue; + } + int a = (c + sq) / 2, b = (c - sq) / 2; + temp[0] = j; + temp[1] = a; + temp[2] = b; + if (temp[0] > temp[1]) + { + swap(temp[0], temp[1]); + } + if (temp[0] > temp[2]) + { + swap(temp[0], temp[2]); + } + if (temp[1] > temp[2]) + { + swap(temp[1], temp[2]); + } + if (find()) + { + continue; + } + print(i, temp[0], temp[1], temp[2]); + } + } + } + return 0; +} diff --git a/10484.cpp b/10484.cpp new file mode 100644 index 0000000..677817b --- /dev/null +++ b/10484.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +#define MAXN 100 +char FLAG[MAXN + 3]; +int PR[MAXN]; +int STORE[MAXN + 2]; +long long K, N, D; +void PRIME_TABLE() +{ + int i, j; + K = 0; + for (i = 2; i <= MAXN; i++) + { + FLAG[i] = 1; + } + for (i = 2; i <= 10;) + { + for (j = i + i; j <= MAXN; j += i) + { + FLAG[j] = 0; + } + for (++i; !FLAG[i]; i++) + ; + } + for (i = 2; i <= MAXN; i++) + if (FLAG[i]) + { + PR[K++] = i; + } +} +long long FACT(int j) +{ + long long d = 1, n; + long long m = 0; + n = pow(j, d++); + while (N >= n) + { + m += N / n; + n = pow(j, d++); + } + return m; +} +void FACTOR1() +{ + int i, j; + for (i = 0; i < K && PR[i] <= N; i++) + { + j = PR[i]; + STORE[j] = FACT(j); + } +} +int FACTOR2() +{ + int i, j, m = 0; + long long k = D; + for (i = 0; i < K && PR[i] <= D; i++) + { + m = 0; + j = PR[i]; + if (D % PR[i] == 0) + { + while (D % j == 0 && D > 1) + { + m++; + D /= j; + } + } + STORE[j] -= m; + if (STORE[j] < 0) + { + return 0; + } + if (D == 1) + { + return 1; + } + } + if (D != 1) + { + return 0; + } + return 1; +} +void CALCULATE() +{ + int i; + long long n = 1; + for (i = 0; i <= 100; i++) + { + n *= STORE[i] + 1; + } + printf("%lld\n", n); +} +int main() +{ + int j, i; + PRIME_TABLE(); + while (1) + { + scanf("%lld%lld", &N, &D); + if (D < 0) + { + D = -D; + } + if (!N) + if (!D) + { + break; + } + if (D == 0) + { + printf("0\n"); + continue; + } + if (N == 0 && D == 1) + { + printf("1\n"); + continue; + } + FACTOR1(); + if (D == 1) + { + CALCULATE(); + goto done; + } + j = FACTOR2(); + if (j) + { + CALCULATE(); + } + else + { + printf("0\n"); + } + done:; + for (i = 0; i < MAXN; i++) + { + STORE[i] = 0; + } + } + return 0; +} diff --git a/10486.cpp b/10486.cpp new file mode 100644 index 0000000..310f535 --- /dev/null +++ b/10486.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int n, m, d[50][50], a[50][50]; +int mj, ori, cha, retmj, F; +int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; + +void dodye(int s[][50], int x, int y, int &dyeno) +{ + int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; + int i, x1, y1; + s[x][y] = dyeno; + for (i = 0; i < 4; i++) + { + x1 = x + dir[i][0], y1 = y + dir[i][1]; + if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && s[x1][y1] != 0 && s[x1][y1] != dyeno) + { + dodye(s, x1, y1, dyeno); + } + } +} +int graphdye(int s[][50], int w, int h) +{ + int i, j, dyeno = 0; + for (i = 0; i < w; i++) + for (j = 0; j < h; j++) + if (s[i][j] == -1) + { + dodye(s, i, j, (++dyeno)); + } + return dyeno; +} +void done() +{ + int i, j, min; + int num[1601]; + for (cha = 0; cha < 100; cha++) + { + for (min = 0; min < 100; min++) + { + memset(a, 0, sizeof(a)); + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + if (d[i][j] >= min && d[i][j] <= min + cha) + { + a[i][j] = -1; + } + graphdye(a, n, m); + memset(num, 0, sizeof(num)); + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + { + num[a[i][j]]++; + if (a[i][j] && num[a[i][j]] >= mj) + { + cout << cha << endl; + return; + } + } + } + } +} +int main() +{ + int i, j, k; + while (cin >> n >> m) + { + for (i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + cin >> d[i][j]; + } + cin >> k; + for (i = 0; i < k; i++) + { + cin >> mj; + done(); + } + //break; + } + return 0; +} diff --git a/10487.cpp b/10487.cpp index 5cb89e1..7af59cf 100644 --- a/10487.cpp +++ b/10487.cpp @@ -1,71 +1,49 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - int t; - for(t=1; ; t++){ - int N, M, i, j; - cin>>N; - if(N == 0) - break; - int64 vec[N]; - FOI(i, 0, N-1) - cin>>vec[i]; - vector mat; - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - if(i != j) - mat.push_back(vec[i]+vec[j]); - } - } - int SZ = mat.size(); - cin>>M; - cout<<"Case "<>K; - int64 diff = INT_MAX, val=vec[0]; - FOI(i, 1, SZ-1){ - int64 temp = abs(mat[i] - K); - if(temp < diff){ - diff = temp; - val = mat[i]; - } - } - cout<<"Closest sum to "< + +using namespace std; + +int numOfInteger, numOfQuery, kase = 1, arr[1001]; + +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +void solve(int value) +{ + int min_, record, i, j; + min_ = 0x7fffffff; + for (i = 0; i < numOfInteger - 1; i++) + for (j = i + 1; j < numOfInteger; j++) + { + int sum; + sum = arr[i] + arr[j]; + if (fabs(value - sum) < min_) + { + min_ = (int)fabs(value - sum); + record = sum; + } + } + cout << "Closest sum to " << value << " is " << record << "." << endl; +} + +int main() +{ + while (cin >> numOfInteger && numOfInteger) + { + cout << "Case " << kase++ << ":" << endl; + int index, value; + for (index = 0; index < numOfInteger; index++) + { + cin >> arr[index]; + } + qsort(arr, numOfInteger, sizeof(int), cmp); + cin >> numOfQuery; + while (numOfQuery--) + { + cin >> value; + solve(value); + } + } + return 0; +} diff --git a/10488.cpp b/10488.cpp new file mode 100644 index 0000000..a17f7d0 --- /dev/null +++ b/10488.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +double D, L; + +double getdryno(double x, double y) +{ + return (floor(y / D)); +} +double dist(double x1, double y1, double x2, double y2) +{ + return (sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))); +} +int main() +{ + double x1, y1, x2, y2, ret; + double d1, d2; + while (cin >> D >> L >> x1 >> y1 >> x2 >> y2) + { + d1 = getdryno(x1, y1); + d2 = getdryno(x2, y2); + if (y1 > y2) + { + y1 -= labs(d1 - d2) * L * 2; + } + else + { + y2 -= labs(d1 - d2) * L * 2; + } + ret = dist(x1, y1, x2, y2); + cout.setf(ios::fixed); + cout.precision(2); + cout << "The gopher has to swim "; + cout << labs(d1 - d2) * L * 2 << " meters and walk "; + cout << ret << " meters." << endl; + } + return 0; +} diff --git a/10489.cpp b/10489.cpp index 08c95d1..ca08a30 100644 --- a/10489.cpp +++ b/10489.cpp @@ -1,28 +1,29 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int test; - scanf("%d",&test); - while(test--){ - int total,lines,i,value=0; - scanf("%d%d",&total,&lines); - for(i=0;i + +using namespace std; + +int main() +{ + int numOfFriends, numOfBox, kase, k; + int cho; + cin >> kase; + while (kase) + { + kase--; + cin >> numOfFriends >> numOfBox; + int t, sum = 0; + for (t = 0; t < numOfBox; t++) + { + cin >> k; + int i, r = 1; + for (i = 0; i < k; i++) + { + cin >> cho; + r = (r * (cho % numOfFriends)) % numOfFriends; + } + sum += r; + } + cout << sum % numOfFriends << endl; + } + return 0; +} diff --git a/1049.cpp b/1049.cpp new file mode 100644 index 0000000..6518c1b --- /dev/null +++ b/1049.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +typedef long long ll; + +#define pb push_back +#define sz(x) ((x).size()) + +const int N = 510; +const int inf = 0x3f3f3f3f; +const double eps = 1e-6; + +struct CostFlow +{ + struct node + { + int from, to, next, cap; + double w; + } edge[N * N]; + int box[N], ecnt; + + void _make_edge(int from, int to, int cap, double w) + { + edge[ecnt].w = w; + edge[ecnt].to = to; + edge[ecnt].cap = cap; + edge[ecnt].from = from; + edge[ecnt].next = box[from]; + box[from] = ecnt++; + } + void make_edge(int from, int to, int cap, double w) + { + //cout << from << " " << to << " " << cap << " " << w << endl; + _make_edge(from, to, cap, w); + _make_edge(to, from, 0, -w); + } + + int pre[N]; + double dis[N]; + bool vis[N]; + + bool spfa(int s, int t) + { + queue que; + memset(vis, 0, sizeof(vis)); + memset(pre, -1, sizeof(pre)); + for (int i = 0; i < N; i++) + { + dis[i] = 1e100; + } + que.push(s); + vis[s] = 1; + dis[s] = 0; + while (que.size()) + { + int u = que.front(); + que.pop(); + vis[u] = 0; + //cout << u << " " << dis[u] << " tt" << endl; + for (int i = box[u]; i + 1; i = edge[i].next) + { + if (!edge[i].cap) + { + continue; + } + int v = edge[i].to; + double w = edge[i].w; + if (dis[v] - eps <= dis[u] + w) + { + continue; + } + dis[v] = dis[u] + w; + pre[v] = i; + if (vis[v]) + { + continue; + } + vis[v] = 1; + que.push(v); + } + } + return dis[t] < 1e100; + } + + double min_cost(int s, int t) + { + double ans = 0; + int flow = inf; + while (spfa(s, t)) + { + flow = inf; + for (int i = pre[t]; i + 1; i = pre[edge[i].from]) + { + if (flow > edge[i].cap) + { + flow = edge[i].cap; + } + } + for (int i = pre[t]; i + 1; i = pre[edge[i].from]) + { + edge[i].cap -= flow; + edge[i ^ 1].cap += flow; + } + ans += flow * dis[t]; + } + return ans; + } + + void clear() + { + ecnt = 0; + memset(box, -1, sizeof(box)); + } +}; + +int a[N], b[N]; +double w[N][N]; + +CostFlow cf; + +int main() +{ + int n, m, ca = 0; + while (scanf("%d%d", &n, &m), n + m) + { + for (int i = 1; i <= n; i++) + { + scanf("%d", a + i); + } + for (int i = 1; i <= m; i++) + { + scanf("%d", b + i); + } + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + { + scanf("%lf", w[i] + j); + } + cf.clear(); + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + { + if (w[i][j] < 0) + { + continue; + } + cf.make_edge(i, j + n, inf, w[i][j]); + } + for (int i = 1; i <= n; i++) + { + cf.make_edge(0, i, a[i], 0); + } + for (int i = 1; i <= m; i++) + { + cf.make_edge(i + n, m + n + 1, b[i], 0); + } + double mn = cf.min_cost(0, 1 + n + m); + cf.clear(); + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + { + if (w[i][j] < 0) + { + continue; + } + cf.make_edge(i, j + n, inf, -w[i][j]); + } + for (int i = 1; i <= n; i++) + { + cf.make_edge(0, i, a[i], 0); + } + for (int i = 1; i <= m; i++) + { + cf.make_edge(i + n, m + n + 1, b[i], 0); + } + double mx = -cf.min_cost(0, 1 + n + m); + printf("Problem %d: %.2f to %.2f\n", ++ca, mn, mx); + } + return 0; +} diff --git a/10490.cpp b/10490.cpp index 4f7c970..d665fd7 100644 --- a/10490.cpp +++ b/10490.cpp @@ -1,29 +1,59 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int prime[]={2,3,5,7,11,13,17,19,23,29,31}; - for(;;){ - int n,i,ctr=0; - scanf("%d",&n); - if(n==0) - break; - if(n==11 || n==23 || n==29){ - printf("Given number is prime. But, NO perfect number is available.\n"); - continue; - } - for(i=0;i<=10;i++){ - if(prime[i]==n){ - unsigned long long val=(long long)(pow(2.0,n-1)*(pow(2.0,n)-1)); - printf("Perfect: %lld!\n",val); - ctr=-1; - break; - } - } - if(ctr>=0) - printf("Given number is NOT prime! NO perfect number is available.\n"); - } - return 0; -} +#include + +using namespace std; + +/*104902*/ +char pr[50]; +int perfect[] = {2, 3, 5, 7, 13, 17, 19, 31}; +int N; +long long PER; +void prime_table() +{ + int i, j; + for (i = 2; i <= 32; i++) + { + pr[i] = 1; + } + for (i = 2; i <= sqrt(32);) + { + for (j = i + i; j <= 31; j += i) + { + pr[j] = 0; + } + for (++i; !pr[i]; i++) + ; + } +} +int PERFECT() +{ + int i; + for (i = 0; i < 8; i++) + if (perfect[i] == N) + { + PER = pow(2.0, (double)N - 1.0) * (pow(2.0, (double)N) - 1); + return 1; + } + return 0; +} +int main() +{ + int p; + prime_table(); + while (scanf("%d", &N) && N) + { + p = PERFECT(); + if (p) + { + printf("Perfect: %lld!\n", PER); + } + else if (pr[N] && !p) + { + printf("Given number is prime. But, NO perfect number is available.\n"); + } + else if (!pr[N] && !p) + { + printf("Given number is NOT prime! NO perfect number is available.\n"); + } + } + return 0; +} diff --git a/10491.cpp b/10491.cpp index 98da0e8..3c616bb 100644 --- a/10491.cpp +++ b/10491.cpp @@ -1,15 +1,16 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - double ncow,ncar,nshow; - while(scanf("%lf%lf%lf",&ncow,&ncar,&nshow)!=EOF){ - double val1=ncar * (ncow + ncar - 1); - double val2=(ncow+ncar) * (ncow + ncar - nshow - 1); - double val=val1/val2; - printf("%.5lf\n",val); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + double ncow, ncar, nshow; + while (scanf("%lf%lf%lf", &ncow, &ncar, &nshow) != EOF) + { + double val1 = ncar * (ncow + ncar - 1); + double val2 = (ncow + ncar) * (ncow + ncar - nshow - 1); + double val = val1 / val2; + printf("%.5lf\n", val); + } + return 0; +} diff --git a/10493.cpp b/10493.cpp index 70af77f..7fab5e6 100644 --- a/10493.cpp +++ b/10493.cpp @@ -1,52 +1,44 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - while(true){ - int64 M, N; - cin>>N>>M; - if(N==0 && M==0) - break; - int64 num = M*N - 1; - int64 den = N - 1; - if(N==1 && M==1) - cout<1) || (num%den!=0)) - cout< + +using namespace std; + +/******************************* +TITLE : CATS WITH OR WITHOUT HATS +NO : 10493 +********************************/ +int N, M, T; +int solvecase() +{ + if (N == 1 && M == 1) + { + printf(" Multiple\n"); + return 0; + } + if (M > 1 && N <= 1) + { + printf(" Impossible\n"); + return 0; + } + //if(N>M) { printf(" Impossible\n"); return 0; } + if ((M - 1) % (N - 1)) + { + printf(" Impossible\n"); + return 0; + } + printf(" %d\n", M + (M - 1) / (N - 1)); + return 0; +} +int main() +{ + while (1) + { + scanf("%d%d", &N, &M); + if (N == 0) + { + break; + } + printf("%d %d", N, M); + solvecase(); + } + return 0; +} diff --git a/10494.cpp b/10494.cpp new file mode 100644 index 0000000..0ab03e9 --- /dev/null +++ b/10494.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +/******************************* +TITLE : IF WE WERE A CHILD AGAIN + NO : 10494 +********************************/ +#define MAXN 10000 +/*100000 */ +char SIGN[5], NUM[MAXN], DEN[15]; +long K; +int DIVISION() +{ + long long i, j, n, g, l; + l = strlen(NUM); + i = 0; + n = NUM[i] - '0'; + i++; + while (n < K && NUM[i]) + { + n = n * 10 + NUM[i] - '0'; + i++; + } + if (n < K) + { + printf("0"); + return 0; + } + if (i >= l) + { + printf("%lld", n / K); + return 0; + } + while (1) + { + printf("%lld", n / K); + if (i >= l) + { + break; + } + g = n % K; + n = g * 10 + NUM[i] - '0'; + i++; + } + return 0; +} +int MOD() +{ + long i, j, k, l; + i = 0; + j = 0; + while (NUM[i]) + { + k = j * 10 + NUM[i] - '0'; + j = k % K; + i++; + } + printf("%ld", j); + return 0; +} +int main() +{ + while (scanf("%s%s%s", NUM, SIGN, DEN) != EOF) + { + sscanf(DEN, "%ld", &K); + if (!strcmp(SIGN, "/")) + { + DIVISION(); + } + else + { + MOD(); + } + putchar('\n'); + } + return 0; +} diff --git a/10495.cpp b/10495.cpp new file mode 100644 index 0000000..121ee2b --- /dev/null +++ b/10495.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES +int main() +{ + double r, h, d1, A1, d2, A2; + double x1, x2, y1, y2; + double l, A, Ap; + while (scanf("%lf%lf%lf%lf%lf%lf", &r, &h, &d1, &A1, &d2, &A2) == 6) + { + l = sqrt(r * r + h * h); + assert(d1 >= 0 && d1 <= l && 0 <= A1 && A1 < 360); + assert(d2 >= 0 && d1 <= l && 0 <= A2 && A2 < 360); + A = r / l * 2 * M_PI; + Ap = fabs(A1 - A2) / 360 * A; + if (Ap > A - Ap) + { + Ap = A - Ap; + } + x1 = d1; + y1 = 0; + x2 = d2 * cos(Ap); + y2 = d2 * sin(Ap); + printf("%.2f\n", sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); + } + return 0; +} diff --git a/10496.cpp b/10496.cpp new file mode 100644 index 0000000..0ef6d95 --- /dev/null +++ b/10496.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +const int oo = 0x7fffffff; + +int numOfCase, numOfBeepers; +int sizeX, sizeY; +int shortest; +bool visited[15]; + +struct Position +{ + int x, y; +} start, beepers[15]; + +int CalDistance(Position from, Position to) +{ + return (int)(fabs(from.x - to.x) + fabs(from.y - to.y)); +} + +void Backtrace(Position now, int index, int cost) +{ + if (index >= numOfBeepers) + { + cost += CalDistance(start, now); + if (cost < shortest) + { + shortest = cost; + } + return; + } + if (cost > shortest) + { + return; + } + int k; + Position last = now; + for (k = 0; k < numOfBeepers; k++) + { + if (visited[k] == false) + { + visited[k] = true; + now = beepers[k]; + Backtrace(now, index + 1, cost + CalDistance(now, last)); + now = last; + visited[k] = false; + } + } +} + +int main() +{ + scanf("%d", &numOfCase); + while (numOfCase--) + { + scanf("%d%d", &sizeX, &sizeY); + scanf("%d%d", &start.x, &start.y); + scanf("%d", &numOfBeepers); + int i; + for (i = 0; i < numOfBeepers; i++) + { + scanf("%d%d", &beepers[i].x, &beepers[i].y); + } + shortest = oo; + memset(visited, 0, sizeof(visited)); + Backtrace(start, 0, 0); + printf("The shortest path has length %d\n", shortest); + } + return 0; +} diff --git a/10497.cpp b/10497.cpp index bd63b41..adb94aa 100644 --- a/10497.cpp +++ b/10497.cpp @@ -1,46 +1,313 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int n; - cin>>n; - if(n == -1) - break; - if(n == 3){ - cout<<"2\n"; - continue; - } - cout<<(n-1)*(n-1)< + +using namespace std; + +#define MAXDIGITS 2000 +#define PLUS 1 +#define MINUS -1 +#define max(a, b) ((a > b) ? a : b) + +typedef struct +{ + char digits[MAXDIGITS]; + int signbit; + int lastdigit; +} bignum; + +void print_bignum(bignum *n); +void int_to_bignum(int s, bignum *n); +void initialize_bignum(bignum *n); +int add_bignum(bignum *a, bignum *b, bignum *c); +int subtract_bignum(bignum *a, bignum *b, bignum *c); +int compare_bignum(bignum *a, bignum *b); + +void int_to_bignum(int s, bignum *n) +{ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + int t = abs(s); + sprintf(n->digits, "%d", t); + n->lastdigit = strlen(n->digits); +} +void int_to_bignum(char *s, bignum *n) +{ + int i; + if (s[0] != -1) + { + n->signbit = PLUS; + i = 0; + } + else + { + i = 1; + n->signbit = MINUS; + } + strcpy(n->digits, &s[i]); + n->lastdigit = strlen(n->digits); +} +void initialize_bignum(bignum *n) +{ + int_to_bignum(0, n); +} +int add_bignum(bignum *a, bignum *b, bignum *c) +{ + int carry; /* carry digit */ + int i, j, op = 0; + ; /* counter */ + int n_carry; + initialize_bignum(c); + if (a->signbit == b->signbit) + { + c->signbit = a->signbit; + } + else + { + if (a->signbit == MINUS) + { + a->signbit = PLUS; + n_carry = subtract_bignum(b, a, c); + a->signbit = MINUS; + } + else + { + b->signbit = PLUS; + n_carry = subtract_bignum(a, b, c); + b->signbit = MINUS; + } + return n_carry; + } + if (a->lastdigit < b->lastdigit) + { + return add_bignum(b, a, c); + } + int k = c->lastdigit = a->lastdigit + 1; + c->digits[k--] = '\0'; + carry = 0; + n_carry = 0; + for (i = b->lastdigit - 1, j = a->lastdigit - 1; i >= 0; i--, j--) + { + carry = b->digits[i] - '0' + a->digits[j] - '0' + carry; + c->digits[k--] = (carry % 10) + '0'; + carry = carry / 10; + if (carry) + { + n_carry++; + } + } + for (; j >= 0; j--) + { + carry = a->digits[j] - '0' + carry; + c->digits[k--] = (carry % 10) + '0'; + carry = carry / 10; + if (carry) + { + n_carry++; + } + } + if (carry) + { + c->digits[k] = carry + '0'; + } + else + { + char string[MAXDIGITS]; + strcpy(string, &c->digits[1]); + strcpy(c->digits, string); + } + c->lastdigit = c->lastdigit - k - 1; + return n_carry; +} +int subtract_bignum(bignum *a, bignum *b, bignum *c) +{ + register int i, j, op = 0; + ; /* counter */ + int n_borrow; + int temp; + c->signbit = PLUS; + if ((a->signbit == MINUS) || (b->signbit == MINUS)) + { + b->signbit = -1 * b->signbit; + n_borrow = add_bignum(a, b, c); + b->signbit = -1 * b->signbit; + return n_borrow; + } + if (compare_bignum(a, b) == PLUS) + { + n_borrow = subtract_bignum(b, a, c); + c->signbit = MINUS; + return n_borrow; + } + int k = c->lastdigit = max(a->lastdigit, b->lastdigit); + n_borrow = 0; + c->digits[k--] = '\0'; + for (i = a->lastdigit - 1, j = b->lastdigit - 1; j >= 0; i--, j--) + { + temp = a->digits[i] - '0' - (b->digits[j] - '0' + op); + if (temp < 0) + { + temp += 10; + op = 1; + n_borrow++; + } + else + { + op = 0; + } + c->digits[k--] = temp + '0'; + } + while (op) + { + temp = a->digits[i--] - op - '0'; + if (temp < 0) + { + temp += 10; + op = 1; + n_borrow++; + } + else + { + op = 0; + } + c->digits[k--] = temp + '0'; + } + for (; i >= 0; i--) + { + c->digits[k--] = a->digits[i]; + } + for (i = 0; !(c->digits[i] - '0'); i++) + ; + c->lastdigit = c->lastdigit - i; + if (i == a->lastdigit) + { + strcpy(c->digits, "0"); + } + else + { + char string[MAXDIGITS]; + strcpy(string, &c->digits[i]); + strcpy(c->digits, string); + } + return n_borrow; +} +int compare_bignum(bignum *a, bignum *b) +{ + int i; /* counter */ + if ((a->signbit == MINUS) && (b->signbit == PLUS)) + { + return (PLUS); + } + if ((a->signbit == PLUS) && (b->signbit == MINUS)) + { + return (MINUS); + } + if (b->lastdigit > a->lastdigit) + { + return (PLUS * a->signbit); + } + if (a->lastdigit > b->lastdigit) + { + return (MINUS * a->signbit); + } + for (i = 0; i < a->lastdigit; i++) + { + if (a->digits[i] > b->digits[i]) + { + return (MINUS * a->signbit); + } + if (b->digits[i] > a->digits[i]) + { + return (PLUS * a->signbit); + } + } + return (0); +} +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + long int n_d; + register long int i, j, k; + short int num1[MAXDIGITS], num2[MAXDIGITS], of = 0, res[MAXDIGITS] = {0}; + n_d = (a->lastdigit < b->lastdigit) ? b->lastdigit : a->lastdigit; + n_d++; + for (i = 0, j = a->lastdigit - 1; i < a->lastdigit; i++, j--) + { + num1[i] = a->digits[j] - 48; + } + for (i = 0, j = b->lastdigit - 1; i < b->lastdigit; j--, i++) + { + num2[i] = b->digits[j] - 48; + } + res[0] = 0; + for (j = 0; j < b->lastdigit; j++) + { + for (i = 0, k = j; i < a->lastdigit || of; k++, i++) + { + if (i < a->lastdigit) + { + res[k] += num1[i] * num2[j] + of; + } + else + { + res[k] += of; + } + of = res[k] / 10; + res[k] = res[k] % 10; + } + } + for (i = k - 1, j = 0; i >= 0; i--, j++) + { + c->digits[j] = res[i] + 48; + } + c->digits[j] = '\0'; + c->lastdigit = k; + c->signbit = a->signbit * b->signbit; +} +bignum f[802]; +void Cal() +{ + bignum n1, n2, n3; + int i; + char temp[2000], dummy[2000]; + strcpy(temp, "1"); + strcpy(dummy, "0"); + for (i = 3; i <= 800; i++) + { + int_to_bignum(temp, &f[i - 1]); + int_to_bignum(dummy, &f[i - 2]); + add_bignum(&f[i - 1], &f[i - 2], &n1); + int_to_bignum(i - 1, &n2); + strcpy(temp, n1.digits); + strcpy(dummy, n2.digits); + int_to_bignum(temp, &n1); + int_to_bignum(dummy, &n2); + multiply_bignum(&n1, &n2, &n3); + strcpy(f[i].digits, n3.digits); + n3.digits[0] = NULL; + strcpy(temp, f[i].digits); + strcpy(dummy, f[i - 1].digits); + } +} +int main() +{ + int n; + Cal(); + while (scanf("%d", &n) == 1) + { + if (n < 0) + { + break; + } + if (n == 0 || n == 1) + { + printf("0\n"); + continue; + } + printf("%s\n", f[n].digits); + } + return 0; +} diff --git a/10498.cpp b/10498.cpp new file mode 100644 index 0000000..b22bbd8 --- /dev/null +++ b/10498.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +//=============================== +// Simplex Method +// UVa 10498 +// AC on 22/11/07 +// Code by kn +//=============================== +#define EPSILON 1e-8 +int n;// # of unknwons +int m;// # of constraints + +int main() +{ + while (scanf("%d%d", &n, &m) > 0) + { + double arr[m + 1][n + m + 1]; + double rhs[m + 1]; + int var[m + 1]; + for (int j = 0; j < m + 1; j++) + for (int i = 0; i < m + n + 1; i++) + { + arr[j][i] = 0.00; + } + // Objective fct + for (int i = 1; i <= n; i++) + { + scanf("%lf", &arr[0][i]); + } + for (int i = 1; i <= n; i++) + { + arr[0][i] = -arr[0][i]; + } + rhs[0] = 0; + // m constraints + for (int row = 1; row <= m; row++) + { + for (int i = 1; i <= n; i++) + { + scanf("%lf", &arr[row][i]); + } + scanf("%lf", &rhs[row]); + } + arr[0][0] = 1; + for (int row = 1; row <= m; row++) + { + arr[row][n + row] = 1; + } + // Each row, which variable is dependent + // 0 : Z + // 1 : x1, 2 : x2, ... + // n+1 : S1, n+2 : S2, ... + var[0] = 0; + for (int row = 1; row <= m; row++) + { + var[row] = n + row; + } + // Print + /* + for(int row=0; row<=m; row++){ + for(int col=0; col<=n+m; col++) + printf("%10.4lf", arr[row][col]); + printf("%10.4lf", rhs[row]); + puts(""); + } + puts(""); + */ + while (true) + { + // Find entering variable + bool quit = false; + double min = 0.000; + int enter_col = -1;// col index + for (int i = 0; i <= n + m; i++) + { + if (arr[0][i] < min - EPSILON) + { + min = arr[0][i]; + enter_col = i; + } + } + // Check if iteration ends + if (enter_col == -1 || fabs(min) < EPSILON) + { + // puts("Iteration ends"); + break; + } + // Find entering variables + // Perform minimum ratio test + double mrt = -1; + int mrt_row = -1; + for (int row = 1; row <= m; row++) + { + if (fabs(arr[row][enter_col]) < EPSILON) + { + continue; + } + double tmp = rhs[row] / arr[row][enter_col]; + if (tmp >= EPSILON || fabs(tmp) < EPSILON) + if (mrt < 0 || tmp < mrt) + { + mrt = tmp; + mrt_row = row; + } + } + /* + printf("mrt_row = %d, enter_col = %d\n", mrt_row, enter_col); + printf("mrt=%lf\n", mrt); + + if(mrt_row==-1 || mrt < 0){ + printf("BUG at 76\n"); + return 1; + } + */ + // Gaussian Elimination for other rows + for (int row = 0; row <= m; row++) + { + if (row == mrt_row) + { + continue; + } + if (fabs(arr[row][enter_col]) < EPSILON) + { + continue; + } + double factor = -arr[row][enter_col] / arr[mrt_row][enter_col]; + arr[row][enter_col] = 0.0000; + for (int col = 1; col <= n + m; col++) + { + if (col == enter_col) + { + continue; + } + arr[row][col] += factor * arr[mrt_row][col]; + } + rhs[row] += factor * rhs[mrt_row]; + } + // Normalize row - mrt_row + for (int col = 1; col <= n + m; col++) + { + if (col == enter_col) + { + continue; + } + arr[mrt_row][col] /= arr[mrt_row][enter_col]; + } + rhs[mrt_row] /= arr[mrt_row][enter_col]; + arr[mrt_row][enter_col] = 1; + /* + for(int row=0; row<=m; row++){ + for(int col=0; col<=n+m; col++) + printf("%10.4lf", arr[row][col]); + printf("%10.4lf", rhs[row]); + puts(""); + } + */ + var[mrt_row] = enter_col; + } + printf("Nasa can spend %d taka.\n", (int)ceil(rhs[0] * m)); + } + return 0; +} diff --git a/10499.cpp b/10499.cpp new file mode 100644 index 0000000..bd7339b --- /dev/null +++ b/10499.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + long int N; + while (scanf("%ld", &N) && N > 0) + { + if (N == 1) + { + printf("0%%\n"); + } + else + { + printf("%ld%%\n", 25 * N); + } + } +} diff --git a/105.cpp b/105.cpp index 4c2be65..938fdb9 100644 --- a/105.cpp +++ b/105.cpp @@ -1,25 +1,39 @@ -#include -#include -#include +#include + using namespace std; -int main() { - int line[10005] = {0}; - int L, H, R; - int Min = 10004, Max = 0; - while (scanf("%d %d %d", &L, &H, &R) != EOF) { - Min = min(L, Min); - Max = max(R, Max); - - for (int i = L; i < R; i++) - line[i] = max(line[i], H); +int main() +{ + int hs[10001] = {0}, l, h, r; + while (scanf("%d%d%d", &l, &h, &r) == 3) + { + for (int i = l; i < r; ++i) + { + if (h > hs[i]) + { + hs[i] = h; + } + } } - - for (int i = Min; i <= Max; i++) { - if (line[i] != line[i - 1]) { - if (i > Min) printf(" "); - - printf("%d %d", i, line[i]); + + // only show preceeding space on non-first cases + bool not_fst = false; + int curr_h = 0; + for (int pos = 0; pos != 10000; ++pos) + { + if (hs[pos] != curr_h) + { + if (not_fst) + { + printf(" "); + } + else + { + not_fst = true; + } + printf("%d %d", pos, hs[pos]); + + curr_h = hs[pos]; } } printf("\n"); diff --git a/10500.cpp b/10500.cpp new file mode 100644 index 0000000..19f13c3 --- /dev/null +++ b/10500.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +void print_border(int m) +{ + cout << "|"; + for (int i = 0; i < m; i++) + { + cout << "---|"; + } + cout << endl; +} + +void mark_visibility(bool visible[12][12], int x, int y) +{ + visible[x][y] = true; + visible[x - 1][y] = true; + visible[x][y - 1] = true; + visible[x + 1][y] = true; + visible[x][y + 1] = true; +} + +int main() +{ + int n, m, x, y; + char map[12][12]; + bool visited[12][12]; + bool visible[12][12]; + while (cin >> n >> m) + { + if (n == 0 && m == 0) + { + break; + } + cout << endl; + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 12; j++) + { + map[i][j] = 'X'; + visited[i][j] = visible[i][j] = false; + } + } + cin >> x >> y; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + cin >> map[i + 1][j + 1]; + } + } + int moves = 0; + while (true) + { + visited[x][y] = true; + mark_visibility(visible, x, y); + if (map[x - 1][y] == '0' && !visited[x - 1][y]) + { + x--; + } + else if (map[x][y + 1] == '0' && !visited[x][y + 1]) + { + y++; + } + else if (map[x + 1][y] == '0' && !visited[x + 1][y]) + { + x++; + } + else if (map[x][y - 1] == '0' && !visited[x][y - 1]) + { + y--; + } + else + { + break; + } + moves++; + } + print_border(m); + for (int i = 0; i < n; i++) + { + cout << "|"; + for (int j = 0; j < m; j++) + { + cout << " "; + if (!visible[i + 1][j + 1]) + { + cout << "?"; + } + else + { + cout << map[i + 1][j + 1]; + } + cout << " |"; + } + cout << endl; + print_border(m); + } + cout << endl; + cout << "NUMBER OF MOVEMENTS: " << moves << endl; + } + return 0; +} diff --git a/10501.cpp b/10501.cpp new file mode 100644 index 0000000..3bb3070 --- /dev/null +++ b/10501.cpp @@ -0,0 +1,241 @@ +#include + +using namespace std; + +char map_[25][25]; +int n, m, board[25][25], lst[200][4], aryx[128][400], aryy[128][400], num[128]; + +int dfs(int depth, int remains) +{ + int i, j, k, l, s, x, y, check, ch; + char mark[25][25]; + if (!remains) + { + for (i = 0; i < depth; i++) + { + printf("(%d,%d),(%d,%d)\n", lst[i][0], lst[i][1], lst[i][2], lst[i][3]); + } + return 1; + } + for (i = 1; i <= n; i++) + { + for (j = 1; j <= m; j++) + { + if (map_[i][j]) + { + ch = map_[i][j]; + map_[i][j] = 0; + lst[depth][0] = j; + lst[depth][1] = i; + memset(mark, 0, sizeof(mark)); + for (k = i - 1; k > -1; k--) + { + if (map_[k][j]) + { + break; + } + for (l = j; l > -1; l--) + { + if (map_[k][l]) + { + break; + } + mark[k][l] = 1; + } + for (l = j + 1; l <= m + 1; l++) + { + if (map_[k][l]) + { + break; + } + mark[k][l] = 1; + } + } + for (k = i + 1; k <= n + 1; k++) + { + if (map_[k][j]) + { + break; + } + for (l = j; l > -1; l--) + { + if (map_[k][l]) + { + break; + } + mark[k][l] = 1; + } + for (l = j + 1; l <= m + 1; l++) + { + if (map_[k][l]) + { + break; + } + mark[k][l] = 1; + } + } + for (k = j - 1; k > -1; k--) + { + if (map_[i][k]) + { + break; + } + for (l = i; l > -1; l--) + { + if (map_[l][k]) + { + break; + } + mark[l][k] = 1; + } + for (l = i + 1; l <= n + 1; l++) + { + if (map_[l][k]) + { + break; + } + mark[l][k] = 1; + } + } + for (k = j + 1; k <= m + 1; k++) + { + if (map_[i][k]) + { + break; + } + for (l = i; l > -1; l--) + { + if (map_[l][k]) + { + break; + } + mark[l][k] = 1; + } + for (l = i + 1; l <= n + 1; l++) + { + if (map_[l][k]) + { + break; + } + mark[l][k] = 1; + } + } + for (s = board[i][j] + 1; s < num[ch]; s++) + { + x = aryx[ch][s]; + y = aryy[ch][s]; + if (map_[x][y]) + { + check = 0; + for (k = x - 1; k > -1; k--) + { + if (mark[k][y]) + { + check = 1; + break; + } + if (map_[k][y]) + { + break; + } + } + if (!check) + { + for (k = x + 1; k <= n + 1; k++) + { + if (mark[k][y]) + { + check = 1; + break; + } + if (map_[k][y]) + { + break; + } + } + if (!check) + { + for (k = y - 1; k > -1; k--) + { + if (mark[x][k]) + { + check = 1; + break; + } + if (map_[x][k]) + { + break; + } + } + if (!check) + { + for (k = y + 1; k <= m + 1; k++) + { + if (mark[x][k]) + { + check = 1; + break; + } + if (map_[x][k]) + { + break; + } + } + } + } + } + if (check) + { + map_[x][y] = 0; + lst[depth][2] = y; + lst[depth][3] = x; + if (dfs(depth + 1, remains - 2)) + { + return 1; + } + map_[x][y] = ch; + } + } + } + map_[i][j] = ch; + } + } + } + return 0; +} + +int main() +{ + int i, j, temp; + while (scanf("%d%d", &m, &n) == 2) + { + memset(num, 0, sizeof(num)); + for (i = 1; i <= n; i++) + { + scanf("%s", map_[i] + 1); + for (j = 1; j <= m; j++) + { + temp = map_[i][j]; + aryx[temp][num[temp]] = i; + aryy[temp][num[temp]] = j; + board[i][j] = num[temp]++; + } + } + for (i = 0; i < 128; i++) + { + if (num[i] % 2) + { + break; + } + } + if (i < 128) + { + printf("No solution\n"); + } + else if (!dfs(0, n * m)) + { + printf("No solution\n"); + } + } + return 0; +} diff --git a/10502.cpp b/10502.cpp new file mode 100644 index 0000000..3ef6bc5 --- /dev/null +++ b/10502.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +/* +Counting Rectangles +10502 +*/ +#define MAX 105 +char Field[MAX][MAX]; +int A[MAX][MAX]; +int R, C; +int Sum(int n) +{ + int m = n + 1; + int s = (n * (m)) / 2; + return s; +} +int Rcount(int n) +{ + int i, j = 0, c, total = 0; + while (j < R) + { + c = 0; + for (i = j; i < R && Field[i][n] == '1'; i++) + { + c++; + } + total += Sum(c); + for (j = i; j < R && Field[j][n] == '0'; j++) + ; + } + return total; +} +int Compare(int n, int m) +{ + int i, j = 0, k, l, d = m - n, total = 0, c; + if (m > C || n > C) + { + return 0; + } + while (j < R) + { + i = j; + c = 0; + while (i < R) + { + k = A[i][n]; + l = A[i][m]; + if (l - k == d && Field[i][n] == '1' && Field[i][m] == '1') + { + c++; + } + else + { + break; + } + i++; + } + total += Sum(c); + j = i; + while (j < R) + { + k = A[j][n]; + l = A[j][m]; + if (l - k == d && Field[j][n] == '1' && Field[j][m] == '1') + { + break; + }; + j++; + } + } + return total; +} +void Cal() +{ + int i, j, total = 0; + for (i = 1; i <= C; i++) + { + total += Rcount(i); + for (j = i + 1; j <= C; j++) + { + total += Compare(i, j); + } + } + printf("%d\n", total); +} +void Free() +{ + int i, j; + for (i = 0; i < R; i++) + { + for (j = 0; j <= C; j++) + { + A[i][j] = 0; + } + } +} +int main() +{ + int i, j; + while (scanf("%d", &R) == 1) + { + if (!R) + { + break; + } + scanf("%d", &C); + for (i = 0; i < R; i++) + { + scanf("%s", &(Field[i][1])); + A[i][1] = Field[i][1] - '0'; + for (j = 2; Field[i][j]; j++) + { + A[i][j] += (A[i][j - 1] + Field[i][j] - '0'); + } + } + Cal(); + Free(); + } + return 0; +} diff --git a/10503.cpp b/10503.cpp new file mode 100644 index 0000000..6ca0a9f --- /dev/null +++ b/10503.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +/* +The dominoes solitaire +10503 +*/ +int space, spices, sp; +int sr, sc, tr, tc; +struct ss +{ + int r, c; + int ind; +} point[30]; +char fg[40]; +int recur(int r, int c, int index, int level) +{ + int i, x, y; + if (level == space) + { + if (c == tr) + { + return 1; + } + return 0; + } + fg[index] = 1; + for (i = 0; i < sp; i++) + { + if (fg[point[i].ind]) + { + continue; + } + if (c == point[i].r) + if (recur(point[i].r, point[i].c, point[i].ind, level + 1)) + { + return 1; + } + } + fg[index] = 0; + return 0; +} +void Cal() +{ + int x; + x = recur(sr, sc, 30, 0); + if (x) + { + puts("YES"); + } + else + { + puts("NO"); + } + for (x = 0; x < 35; x++) + { + fg[x] = 0; + } +} +int main() +{ + int i; + while (scanf("%d", &space), space) + { + scanf("%d", &spices); + scanf("%d%d", &sr, &sc); + scanf("%d%d", &tr, &tc); + sp = spices; + for (i = 0; i < spices; i++) + { + scanf("%d%d", &point[i].r, &point[i].c); + point[i].ind = i; + if (point[i].r == point[i].c) + { + continue; + } + point[sp].r = point[i].c; + point[sp].c = point[i].r; + point[sp++].ind = i; + } + Cal(); + } + return 0; +} diff --git a/10504.cpp b/10504.cpp new file mode 100644 index 0000000..8ef3a24 --- /dev/null +++ b/10504.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define MAXN 302 + +char grid[MAXN][MAXN]; +int N, M; + +int Count(char cc) +{ + int t = 0, i, j, k, l; + int dx, dy, p, q, r, s; + for (i = 0; i < N; i++) + { + for (j = 0; j + 1 < N; j++) + { + if (grid[i][j] != cc) + continue; + for (k = i; k < N; k++) + { + for (l = j + 1; l < N; l++) + { + if (grid[k][l] == cc) + { + dx = k - i; + dy = l - j; + p = k + dy; + q = l - dx; + r = i + dy; + s = j - dx; + if (grid[p][q] == cc && grid[r][s] == cc) + t++; + } + } + } + } + } + return t; +} + +void Cal() +{ + char ch[3]; + while (M--) + { + scanf("%s", &ch); + printf("%s", ch); + printf(" %d\n", Count(ch[0])); + } +} + +int main() +{ + int i, k = 0; + while (scanf("%d", &N), N) + { + scanf("%d", &M); + for (i = 0; i < N; i++) + scanf("%s", grid[i]); + if (k++) + printf("\n"); + Cal(); + } + return 0; +} diff --git a/10505.cpp b/10505.cpp new file mode 100644 index 0000000..9cdfbab --- /dev/null +++ b/10505.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +struct edge {int nxt, to;} e[80048]; +enum {BLANK, BLUE, RED}; + +int ecnt, cnt[3], color[256], nxt[256]; + +void add_edge(int from, int to) +{ + edge &E = e[ecnt]; + E.nxt = nxt[from]; + E.to = to; + nxt[from] = ecnt++; +} + +bool bicolor(int u, int c) +{ + bool r = true; + color[u] = c; + ++cnt[c]; + for (int at = nxt[u], v; at != -1; at = e[at].nxt) + { + v = e[at].to; + if (color[v] == BLANK) + { + r &= bicolor(v, 3 - c); + } + else + { + r &= color[v] == 3 - c; + } + } + return r; +} + +int main() +{ + scanf("%*d"); + for (int n; scanf("%d", &n) == 1;) + { + int max_inv = 0; + ecnt = 0; + for (int i = 0; i < n; ++i) + { + nxt[i] = -1; + color[i] = BLANK; + } + for (int enemies, i = 0, to; i < n; ++i) + { + scanf("%d", &enemies); + while (enemies--) + { + scanf("%d", &to); + --to; + if (to < n) + { + add_edge(i, to); + add_edge(to, i); + } + } + } + for (int i = 0; i < n; ++i) + if (color[i] == BLANK) + { + cnt[RED] = cnt[BLUE] = 0; + if (!bicolor(i, BLUE)) + { + continue; + } + max_inv += cnt[BLUE] < cnt[RED] ? cnt[RED] : cnt[BLUE]; + } + printf("%d\n", max_inv); + } + return 0; +} diff --git a/10506.cpp b/10506.cpp new file mode 100644 index 0000000..133af29 --- /dev/null +++ b/10506.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +/* +10506 +Ouroboros +*/ +#define maxn 65539 +char F[maxn]; +char R[maxn]; +int B, S, Ter; +int Dec(char ss[]) +{ + int i, d = 1, sum = 0; + for (i = S - 1; i >= 0; i--) + { + sum += (ss[i] - '0') * d; + d *= B; + } + return sum; +} +void Print(int ind) +{ + int i = 0, j = ind; + while (1) + { + if (R[i] != R[j]) + { + R[j + 1] = NULL; + puts(R); + return; + } + i++; + j--; + } +} +int Recur(char ss[], int level, int ind) +{ + char temp[50]; + int k, i; + k = Dec(ss); + if (F[k]) + { + return 0; + } + R[ind] = ss[S - 1]; + if (level == Ter) + { + R[ind + 1] = NULL; + //puts(R); + Print(ind); + return 1; + } + F[k] = 1; + for (i = 1; i < S; i++) + { + temp[i - 1] = ss[i]; + } + for (i = 0; i < B; i++) + { + temp[S - 1] = i + '0'; + temp[S] = NULL; + if (Recur(temp, level + 1, ind + 1)) + { + F[k] = 0; + return 1; + } + } + F[k] = 0; + return 0; +} +void Cal() +{ + int i, j, ind, k; + char ss[20], temp[20]; + Ter = (int)pow(B, S); + for (i = 1; i < Ter; i++) + { + ind = 0; + k = i; + while (k) + { + temp[ind++] = k % B + '0'; + k /= B; + } + for (j = 0; j < S - ind; j++) + { + ss[j] = '0'; + } + k = ind - 1; + for (j; j < S; j++) + { + ss[j] = temp[k--]; + } + ss[j] = NULL; + for (k = 0; k + 1 < S; k++) + { + R[k] = ss[k]; + } + if (Recur(ss, 1, S - 1)) + { + return; + } + } +} +int main() +{ + while (scanf("%d%d", &S, &B) == 2) + { + if (B <= 1) + { + printf("0\n"); + continue; + } + Cal(); + } + return 0; +} diff --git a/10507.cpp b/10507.cpp new file mode 100644 index 0000000..1348cf6 --- /dev/null +++ b/10507.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAXN 28 + +struct ss +{ + char visit_times; + char color; + int year; +} V[MAXN]; +char Wake[MAXN]; +char Link[MAXN][MAXN]; +int Q[MAXN], H, T, N; + +void Push(int n) +{ + Q[H++] = n; + H %= MAXN; +} +int Pop() +{ + int n; + n = Q[T++]; + T %= MAXN; + return n; +} +int IsEmpty() +{ + return H == T; +} +int BFS() +{ + int i, j, totalvisit = 0; + int k; + for (i = 0; i < 26; i++) + { + V[i].visit_times = 0; + V[i].color = V[i].year = 0; + } + H = T = 0; + for (i = 0; Wake[i]; i++) + { + j = Wake[i] - 'A'; + V[j].color = 1; + V[j].visit_times = 3; + Push(j); + totalvisit++; + } + if (totalvisit == N) + { + return 0; + } + while (!IsEmpty()) + { + k = Pop(); + for (i = 0; i < 26; i++) + { + if (Link[k][i] == 0 || V[i].color == 1) + { + continue; + } + V[i].visit_times++; + if (V[i].year < V[k].year + 1) + { + V[i].year = V[k].year + 1; + } + if (V[i].visit_times == 3) + { + V[i].color = 1; + Push(i); + totalvisit++; + } + if (totalvisit == N) + { + return V[i].year; + } + } + } + return -1; +} +void Free() +{ + int i, j; + for (i = 0; i < 26; i++) + for (j = 0; j < 26; j++) + { + Link[i][j] = Link[j][i] = 0; + } +} +int main() +{ + int i, edge, j, k; + char link[5]; + while (scanf("%d", &N) == 1) + { + scanf("%d", &edge); + scanf("%s", Wake); + for (i = 0; i < edge; i++) + { + scanf("%s", link); + j = link[0] - 'A'; + k = link[1] - 'A'; + Link[j][k] = Link[k][j] = 1; + } + k = BFS(); + if (k == -1) + { + printf("THIS BRAIN NEVER WAKES UP\n"); + } + else + { + printf("WAKE UP IN, %d, YEARS\n", k); + } + Free(); + } + return 0; +} diff --git a/10508.cpp b/10508.cpp new file mode 100644 index 0000000..32243b1 --- /dev/null +++ b/10508.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +/**** +10508 +****/ +#define MAXN 2000 +char Word[MAXN][MAXN]; +char Temp[MAXN]; +int N, M; +int main() +{ + int diff, i, j; + while (scanf("%d%d", &N, &M) == 2) + { + scanf("%s", Word[0]); + for (i = 1; i < N; i++) + { + scanf("%s", Temp); + diff = 0; + for (j = 0; j < M; j++) + if (Word[0][j] != Temp[j]) + { + diff++; + } + strcpy(Word[diff], Temp); + } + for (i = 0; i < N; i++) + { + printf("%s\n", Word[i]); + } + } + return 0; +} diff --git a/10509.cpp b/10509.cpp new file mode 100644 index 0000000..404421c --- /dev/null +++ b/10509.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + double a, n, dx; + while (1) + { + scanf("%lf", &n); + if (n == 0) + { + break; + } + a = 1; + while (1) + { + if (a * a * a >= n) + { + break; + } + a++; + } + if (a * a * a == n) + { + printf("%.4lf\n", a); + continue; + } + a--; + dx = (n - a * a * a) / (3 * a * a); + printf("%.4lf\n", a + dx); + } + return 0; +} diff --git a/1051.cpp b/1051.cpp new file mode 100644 index 0000000..2a587d6 --- /dev/null +++ b/1051.cpp @@ -0,0 +1,293 @@ +#include + +using namespace std; + +#define X first +#define Y second +#define pb push_back +#define mp make_pair + +typedef long long lld; + +#define INF 0xfffffff + +struct Node +{ + int flag, val, used; + Node() {} + Node(int flag0, int val0, int used0) : flag(flag0), val(val0), used(used0) {} +}; +Node queue_[400010]; + +bool is; +int mod, ax, ay, dp[2][100010][2], rear, front, pp[110], qq; +pair> pre[2][100010][2]; + +int bfs(int x, int y, int &nx, int &ny) +{ + for (int i = 0; i < 2; i++) + for (int j = 0; j < mod; j++) + for (int k = 0; k < 2; k++) + { + dp[i][j][k] = INF; + } + dp[0][x % mod][0] = 1; + rear = front = 0; + queue_[front++] = Node(0, x % mod, 0); + while (rear != front) + { + int flag = queue_[rear].flag; + int val = queue_[rear].val; + int used = queue_[rear].used; + if (!is && flag == 0 && val == 0 && used == 1) + { + break; + } + if (flag == 1 && val == 0 && used == 1) + { + break; + } + rear++; + int tmp = dp[flag][val][used]; + int to1 = (val * 10 + x) % mod; + int to2 = (val * 10 + y) % mod; + if (used == 0) + { + if (x < y) + { + if (dp[0][to1][0] == INF) + { + dp[0][to1][0] = tmp + 1; + queue_[front++] = Node(0, to1, 0); + pre[0][to1][0] = mp(flag, mp(val, used)); + } + else if (dp[1][to1][0] == INF) + { + dp[1][to1][0] = tmp + 1; + queue_[front++] = Node(1, to1, 0); + pre[1][to1][0] = mp(flag, mp(val, used)); + } + if (dp[0][to2][1] == INF) + { + dp[0][to2][1] = tmp + 1; + queue_[front++] = Node(0, to2, 1); + pre[0][to2][1] = mp(flag, mp(val, used)); + } + else if (dp[1][to2][1] == INF) + { + dp[1][to2][1] = tmp + 1; + queue_[front++] = Node(1, to2, 1); + pre[1][to2][1] = mp(flag, mp(val, used)); + } + } + else + { + if (dp[0][to2][1] == INF) + { + dp[0][to2][1] = tmp + 1; + queue_[front++] = Node(0, to2, 1); + pre[0][to2][1] = mp(flag, mp(val, used)); + } + else if (dp[1][to2][1] == INF) + { + dp[1][to2][1] = tmp + 1; + queue_[front++] = Node(1, to2, 1); + pre[1][to2][1] = mp(flag, mp(val, used)); + } + if (dp[0][to1][0] == INF) + { + dp[0][to1][0] = tmp + 1; + queue_[front++] = Node(0, to1, 0); + pre[0][to1][0] = mp(flag, mp(val, used)); + } + else if (dp[1][to1][0] == INF) + { + dp[1][to1][0] = tmp + 1; + queue_[front++] = Node(1, to1, 0); + pre[1][to1][0] = mp(flag, mp(val, used)); + } + } + } + else + { + if (dp[0][to2][1] == INF) + { + dp[0][to2][1] = tmp + 1; + queue_[front++] = Node(0, to2, 1); + pre[0][to2][1] = mp(flag, mp(val, used)); + } + else if (dp[1][to2][1] == INF) + { + dp[1][to2][1] = tmp + 1; + queue_[front++] = Node(1, to2, 1); + pre[1][to2][1] = mp(flag, mp(val, used)); + } + } + } + if (dp[0][0][1] == INF) + { + return INF; + } + int a, b, c; + a = 0; + b = 0; + c = 1; + nx = 1; + ny = 0; + while (dp[a][b][c] != 1) + { + if (c == 0) + { + nx++; + } + else + { + ny++; + } + int na = pre[a][b][c].X; + int nb = pre[a][b][c].Y.X; + int nc = pre[a][b][c].Y.Y; + a = na; + b = nb; + c = nc; + } + bool same = false; + if (dp[0][0][1] <= 6) + { + lld now = 0; + for (int i = 0; i < nx; i++) + { + now = now * 10 + x; + } + for (int i = 0; i < ny; i++) + { + now = now * 10 + y; + } + if (now == mod) + { + same = true; + } + } + if (same) + { + if (dp[1][0][1] == INF) + { + return INF; + } + a = 1; + b = 0; + c = 1; + nx = 1; + ny = 0; + while (dp[a][b][c] != 1) + { + if (c == 0) + { + nx++; + } + else + { + ny++; + } + int na = pre[a][b][c].X; + int nb = pre[a][b][c].Y.X; + int nc = pre[a][b][c].Y.Y; + a = na; + b = nb; + c = nc; + } + return dp[1][0][1]; + } + return dp[0][0][1]; +} + +void init() +{ + is = false; + int T = mod; + qq = 0; + while (T) + { + pp[qq++] = T % 10; + T /= 10; + } + int tmp = 0; + for (int i = 1; i < qq; i++) + if (pp[i - 1] != pp[i]) + { + tmp++; + } + if (tmp == 1) + { + is = true; + } +} + +int main() +{ + while (scanf("%d", &mod) != EOF) + { + if (mod == 0) + { + break; + } + init(); + int sx, sy, numx, numy; + int ans = INF; + for (int x = 1; x <= 9; x++) + { + for (int y = 0; y <= 9; y++) + { + if (x == y) + { + continue; + } + int nx, ny; + int tmp = bfs(x, y, nx, ny); + if (tmp < ans) + { + ans = tmp; + sx = x; + sy = y; + numx = nx; + numy = ny; + } + else if (tmp == ans) + { + string old = ""; + old += '0' + sx; + if (numx > nx) + { + old += '0' + sx; + } + else + { + old += '0' + sy; + } + old += '0' + sy; + string newnew = ""; + newnew += '0' + x; + if (numx < nx) + { + newnew += '0' + x; + } + else + { + newnew += '0' + y; + } + newnew += '0' + y; + if (newnew < old) + { + ans = tmp; + sx = x; + sy = y; + numx = nx; + numy = ny; + } + } + } + } + printf("%d: %d %d %d %d\n", mod, numx, sx, numy, sy); + } + return 0; +} diff --git a/10510.cpp b/10510.cpp new file mode 100644 index 0000000..2f6998f --- /dev/null +++ b/10510.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +#define maxn 10002 + +struct ss +{ + int node; + int edge; +}; +struct St +{ + int indeg; + int outdeg; + int distime; + int fintime; +}; +struct Edge +{ + int u, v; +}; +list Link[maxn]; +St Node[maxn]; +Edge edge[maxn]; +ss P[maxn]; +int N, E, cover, Fg, Time; +char NFg[maxn]; +char EFg[maxn]; + +void Free() +{ + int i; + for (i = 0; i <= N; i++) + { + Link[i].clear(); + Node[i].distime = Node[i].fintime = 0; + Node[i].indeg = Node[i].outdeg = 0; + NFg[i] = EFg[i] = 0; + } + for (i = N + 1; i <= E; i++) + EFg[i] = 0; +} +int Tra(int n) +{ + int v; + ss t; + cover++; + list::iterator p; + Node[n].distime = ++Time; + NFg[n] = 1; + for (p = Link[n].begin(); p != Link[n].end(); p++) + { + t = *p; + v = t.node; + if (NFg[v] == 1) + { + if (Node[v].distime > Node[n].distime) + return 1; + continue; + } + if (Tra(v)) + return 1; + } + Node[n].fintime = ++Time; + return 0; +} +int IsEuler() +{ + int i, d, u, v; + cover = 0; + Time = 0; + d = Tra(0); + if (d) + return d; + if (cover < N) + return 1; + for (i = 0; i < N; i++) + if (Node[i].indeg != Node[i].outdeg) + return 1; + for (i = 0; i < E; i++) + { + u = edge[i].u; + v = edge[i].v; + if (Node[u].distime > Node[v].distime) + if (Node[u].fintime > Node[v].fintime) + return 1; + if (Node[u].distime < Node[v].distime) + if (Node[u].fintime < Node[v].fintime) + return 1; + } + return 0; +} +int Mark(int v, int key) +{ + int u, edge; + if (v == key) + return 0; + if (v == -1) + return 1; + u = P[v].node; + edge = P[v].edge; + if (EFg[edge]) + return 1; + EFg[edge] = 1; + return Mark(u, key); +} +int DFS(int u) +{ + int v; + ss tmp; + list::iterator p; + NFg[u] = 1; + for (p = Link[u].begin(); p != Link[u].end(); p++) + { + v = p->node; + if (NFg[v] == 1) + { + if (EFg[p->edge] == 1) + return 1; + EFg[p->edge] = 1; + if (Mark(u, v)) + return 1; + } + else + { + tmp.node = u; + tmp.edge = p->edge; + P[v] = tmp; + if (DFS(v)) + return 1; + } + } + return 0; +} +void Cal() +{ + int d; + if (N <= 1) + { + printf("YES\n"); + return; + } + if (!E || IsEuler()) + { + printf("NO\n"); + return; + } + P[0].node = -1; + for (d = 0; d < N; d++) + NFg[d] = 0; + if (DFS(0)) + { + printf("NO\n"); + return; + } + printf("YES\n"); +} + +int main() +{ + int i, k, c = 1; + ss t; + Edge ed; + scanf("%d", &k); + while (k--) + { + scanf("%d%d", &N, &E); + for (i = 0; i < E; i++) + { + scanf("%d%d", &ed.u, &ed.v); + t.node = ed.v; + t.edge = i; + Link[ed.u].push_back(t); + edge[i] = ed; + Node[ed.u].outdeg++; + Node[ed.v].indeg++; + } + Cal(); + Free(); + } + return 0; +} diff --git a/10511.cpp b/10511.cpp new file mode 100644 index 0000000..5d545be --- /dev/null +++ b/10511.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +#define forn(i, n) for (int i = 0; i < (int)(n); i++) +#define forsn(i, s, n) for (int i = (s); i < (int)(n); i++) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) +#define all(v) (v).begin(), (v).end() +#define isIn(i, c) ((c).find(i) != (c).end()) + +#define DIF_F(i, j) (red[i][j].d()) +#define DIF_FI(i) (i->second.d()) + +const int MAXN = 10100; +const int INF = 20000000; + +struct Eje +{ + long long f, m; + long long d() + { + return m - f; + } +}; +typedef map MIE; + +MIE red[MAXN]; +int N, F, D, v[MAXN]; + +void iniG(int n, int f, int d) +{ + N = n; + F = f; + D = d; + fill(red, red + N, MIE()); +} +void aEje(int d, int h, int m) +{ + red[d][h].m = m; + red[d][h].f = 0; +} + +long long camAu() +{ + fill(v, v + N, -1); + queue c; + c.push(F); + while (!(c.empty()) && v[D] == -1) + { + int n = c.front(); + c.pop(); + FOREACH(red[n], i) + { + if (v[i->first] == -1 && DIF_FI(i) > 0) + { + v[i->first] = n; + c.push(i->first); + } + } + } + if (v[D] == -1) + { + return 0; + } + int n = D; + long long f = DIF_F(v[n], n); + while (n != F) + { + f = min(f, DIF_F(v[n], n)); + n = v[n]; + } + n = D; + while (n != F) + { + red[n][v[n]].f = -(red[v[n]][n].f += f); + n = v[n]; + } + return f; +} + +long long flujo() +{ + long long tot = 0, c; + do + { + tot += (c = camAu()); + } while (c > 0); + return tot; +} + +int main() +{ + int nclubs, npartidos, t; + map clubs, partidos; + vector p, miembros[MAXN]; + vector personas, clubnames; + scanf("%d\n\n", &t); + while (t-- > 0) + { + nclubs = npartidos = 0; + clubs.clear(); + partidos.clear(); + personas.clear(); + clubnames.clear(); + p.clear(); + miembros[0].clear(); + string s; + while (getline(cin, s) && !s.empty()) + { + string nombre, partido, club; + istringstream ss(s); + ss >> nombre >> partido; + personas.push_back(nombre); + if (!isIn(partido, partidos)) + { + partidos[partido] = npartidos++; + } + p.push_back(partidos[partido]); + while (ss >> club) + { + if (!isIn(club, clubs)) + { + miembros[nclubs].clear(); + clubs[club] = nclubs++; + clubnames.push_back(club); + } + miembros[clubs[club]].push_back(personas.size() - 1); + } + } + int npersonas = personas.size(); + iniG(2 + nclubs + npartidos + npersonas, 0, 1 + nclubs + npartidos + npersonas); + forn(i, nclubs) + { + aEje(0, 1 + i, 1); + } + forn(i, nclubs) + { + forn(j, miembros[i].size()) + { + aEje(1 + i, 1 + nclubs + miembros[i][j], 1); + } + } + forn(i, npersonas) + { + aEje(1 + nclubs + i, 1 + nclubs + npersonas + p[i], 1); + } + forn(i, npartidos) + { + aEje(1 + nclubs + npersonas + i, 1 + npartidos + nclubs + npersonas, (nclubs + 1) / 2 - 1); + } + int res = flujo(); + if (res != nclubs) + { + printf("Impossible.\n"); + } + else + { + forn(i, nclubs) + { + forn(j, npersonas) + { + if (red[1 + i][1 + nclubs + j].f == 1) + { + cout << personas[j] << " " << clubnames[i] << endl; + } + } + } + } + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/10512.cpp b/10512.cpp new file mode 100644 index 0000000..3cc204c --- /dev/null +++ b/10512.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +long long int ans[6][2]; +int pa; + +// binary search +long long int sqrt(long long int k) +{ + long long int left = 0ll, right = 4294967295ll, mid = (left + right) / 2; + while (left <= right) + { + if (mid * mid == k) + { + return mid; + } + else if (mid * mid < k) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + mid = (left + right) / 2; + } + return -1; +} + +long long int a, b, c, d; +int P, Q; + +int main() +{ + int t; + scanf("%d", &t); + for (int i = 1; i <= t; i++) + { + scanf("%d %d", &P, &Q); + a = 2ll, b = (long long int)-P - P - P - Q, c = (long long int)P * P; + //printf("%I64d %I64d %I64d\n",a,b,c); + printf("Case %d:\n", i); + d = b * b - 4 * a * c; + d = sqrt(d); + if (d == -1) + { + puts("Impossible."); + } + else + { + pa = 0; + long long int t1 = -b + d, t2 = -b - d, x = 65536, y, tx, ty; + if (t1 % (2 * a) == 0) + { + t1 = t1 / (2 * a); + t1 = sqrt(t1); + if (t1 != -1) + { + if (t1 != 0 && P % t1 == 0) + { + tx = P / t1 - t1; + ty = t1; + ans[pa][0] = tx; + ans[pa++][1] = ty; + ty = -t1; + tx = P / ty - ty; + ans[pa][0] = tx; + ans[pa++][1] = ty; + } + else + { + tx = sqrt(Q); + ty = 0; + if (tx != -1) + { + ans[pa][0] = tx; + ans[pa++][1] = ty; + } + } + } + } + if (t2 % (2 * a) == 0) + { + t2 = t2 / (2 * a); + t2 = sqrt(t2); + if (t2 != 0 && P % t2 == 0) + { + tx = P / t2 - t2; + ty = t2; + ans[pa][0] = tx; + ans[pa++][1] = ty; + ty = -t2; + tx = P / ty - ty; + ans[pa][0] = tx; + ans[pa++][1] = ty; + } + else + { + tx = sqrt(Q); + ty = 0; + if (tx != -1) + { + ans[pa][0] = tx; + ans[pa++][1] = ty; + } + } + } + long long int ansx = 65536ll, ansy = 65536ll; + for (int i = 0; i < pa; i++) + if (ans[i][0] >= ans[i][1] && ans[i][0] < ansx) + { + ansx = ans[i][0], ansy = ans[i][1]; + } + if (ansx == 65536ll) + { + puts("Impossible."); + } + else + { + printf("%lld %lld\n", ansx, ansy); + } + } + } + return 0; +} diff --git a/10513.cpp b/10513.cpp new file mode 100644 index 0000000..88f7cb3 --- /dev/null +++ b/10513.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +int n, nxt[18], num[16], tbl[16][127]; +char str[16][33000][17], lst[17], s[20], *ptr; + +int ab(int now) +{ + if (now < 0) + { + return -now; + } + return now; +} + +void dfs(int depth) +{ + int i, j, k; + if (depth == n) + { + ptr = str[n][num[n]++]; + for (i = 0; i < n; i++) + { + ptr[i] = lst[i] + 64; + } + return; + } + for (i = 0; j = nxt[i]; i = j) + { + if (!(depth && ab(lst[depth - 1] - j) == 2) && !(depth > 1 && ab(lst[depth - 2] - j) == 1)) + { + for (k = 0; k < depth; k++) + { + if (depth - k == ab(lst[k] - j)) + { + break; + } + } + if (k == depth) + { + lst[depth] = j; + nxt[i] = nxt[j]; + dfs(depth + 1); + nxt[i] = j; + } + } + } +} + +int main() +{ + int cas, i, j; + long long count; + cas = 0; + for (i = 0; i < 16; i++) + { + nxt[i] = i + 1; + } + strcpy(str[1][num[1]++], "A"); + for (i = 10; i < 16; i++) + { + n = i; + lst[n] = 0; + nxt[n] = 0; + dfs(0); + nxt[n] = n + 1; + } + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + memset(tbl, 0, sizeof(tbl)); + for (i = 0, count = 1; i < n; i++) + { + scanf("%s", s); + if (s[0] == '?') + { + tbl[i][0] = 1; + count *= n; + } + else + { + for (j = 0; s[j]; j++) + { + tbl[i][s[j]] = 1; + } + count *= j; + } + } + for (i = 0; i < num[n]; i++) + { + for (j = 0; j < n; j++) + { + if (!tbl[j][0] && !tbl[j][str[n][i][j]]) + { + break; + } + } + count -= (j == n); + } + printf("Case %d: %lld\n", ++cas, count); + } + return 0; +} diff --git a/10514.cpp b/10514.cpp new file mode 100644 index 0000000..0c2cc93 --- /dev/null +++ b/10514.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +//ifstream fin( "10514.in" ); +//#define cin fin + +const double PI = acos(-1.0); +const int MAX_POLYGON = 15; +const double INF = 1e100; + +struct MPoint +{ + double x, y; +}; + +vector polygon[MAX_POLYGON]; +double graph[MAX_POLYGON][MAX_POLYGON]; +int nIsland; + +void init() +{ + for (int i = 0; i < MAX_POLYGON; i++) + { + polygon[i].clear(); + } + for (int i = 0; i < MAX_POLYGON; i++) + for (int j = 0; j < MAX_POLYGON; j++) + { + graph[i][j] = INF; + } +} + +void input() +{ + int r1, r2; + cin >> r1 >> r2 >> nIsland; + double x, y; + for (int i = 0; i < r1; i++) + { + cin >> x >> y; + MPoint p = {x, y}; + polygon[0].push_back(p); + } + for (int i = 0; i < r2; i++) + { + cin >> x >> y; + MPoint p = {x, y}; + polygon[nIsland + 1].push_back(p); + } + for (int i = 1; i <= nIsland; i++) + { + int m; + cin >> m; + for (int j = 0; j < m; j++) + { + cin >> x >> y; + MPoint p = {x, y}; + polygon[i].push_back(p); + } + MPoint p = polygon[i][0]; + polygon[i].push_back(p); + } +} + +inline double dist(MPoint A, MPoint B) +{ + double xx = A.x - B.x; + double yy = A.y - B.y; + return sqrt(xx * xx + yy * yy); +} + +double minDistanceDot2Segment(MPoint A, MPoint B, MPoint C) +{ + double a = dist(B, C); + double b = dist(A, C); + double c = dist(A, B); + double BB = acos((a * a + c * c - b * b) / (2.0 * a * c)); + double CC = acos((a * a + b * b - c * c) / (2.0 * a * b)); + if (BB > PI / 2) + { + return c; + } + else if (CC > PI / 2) + { + return b; + } + else + { + return c * sin(BB); + } +} + +double minDistanceBetween2Polygon(int poly1, int poly2) +{ + double minDist = INF; + for (int i = 0; i < polygon[poly1].size(); i++) + for (int j = 0; j < polygon[poly2].size() - 1; j++) + { + double tmp = minDistanceDot2Segment(polygon[poly1][i], polygon[poly2][j], polygon[poly2][j + 1]); + if (tmp < minDist) + { + minDist = tmp; + } + } + for (int i = 0; i < polygon[poly2].size(); i++) + for (int j = 0; j < polygon[poly1].size() - 1; j++) + { + double tmp = minDistanceDot2Segment(polygon[poly2][i], polygon[poly1][j], polygon[poly1][j + 1]); + if (tmp < minDist) + { + minDist = tmp; + } + } + return minDist; +} + +void make_graph() +{ + for (int i = 0; i < nIsland + 2; i++) + for (int j = i; j < nIsland + 2; j++) + { + if (i == j) + { + graph[i][j] = graph[j][i] = 0; + } + else + { + graph[i][j] = graph[j][i] = minDistanceBetween2Polygon(i, j); + } + } +} + +void dijkstra(int num, double dist[], int s) +{ + bool vis[MAX_POLYGON]; + for (int i = 0; i < num; i++) + { + dist[i] = graph[s][i]; + vis[i] = false; + } + dist[s] = 0; + for (int i = 0; i < num; i++) + { + double minValue = INF; + int k = 0; + for (int j = 0; j < num; j++) + if (!vis[j] && dist[j] < minValue) + { + minValue = dist[j]; + k = j; + } + vis[k] = true; + for (int j = 0; j < num; j++) + if (!vis[j] && dist[k] + graph[k][j] < dist[j]) + { + dist[j] = dist[k] + graph[k][j]; + } + } +} + +void solve() +{ + make_graph(); + double dist[MAX_POLYGON]; + dijkstra(nIsland + 2, dist, 0); + printf("%.3lf\n", dist[nIsland + 1]); +} + +int main() +{ + int numOfCase; + cin >> numOfCase; + while (numOfCase--) + { + init(); + input(); + solve(); + } + return 0; +} diff --git a/10515.cpp b/10515.cpp index 5d422e4..bb98355 100644 --- a/10515.cpp +++ b/10515.cpp @@ -1,75 +1,42 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - int vec[10][4]; - vec[0][0]=0; vec[0][1]=0; vec[0][2]=0; vec[0][3]=0; - vec[1][0]=1; vec[1][1]=1; vec[1][2]=1; vec[1][3]=1; - vec[2][0]=6; vec[2][1]=2; vec[2][2]=4; vec[2][3]=8; - vec[3][0]=1; vec[3][1]=3; vec[3][2]=9; vec[3][3]=7; - vec[4][0]=6; vec[4][1]=4; vec[4][2]=6; vec[4][3]=4; - vec[5][0]=5; vec[5][1]=5; vec[5][2]=5; vec[5][3]=5; - vec[6][0]=6; vec[6][1]=6; vec[6][2]=6; vec[6][3]=6; - vec[7][0]=1; vec[7][1]=7; vec[7][2]=9; vec[7][3]=3; - vec[8][0]=6; vec[8][1]=8; vec[8][2]=4; vec[8][3]=2; - vec[9][0]=1; vec[9][1]=9; vec[9][2]=1; vec[9][3]=9; - while(true){ - string bas, exp; - cin >> bas >> exp; - if(bas=="0" && exp=="0") - break; - int b = (int)bas.length(); - int e = (int)exp.length(); - int base = (int)(bas[b-1] - '0'); - int expo = (int)(exp[e-1] - '0'); - if(exp.length() > 1) - expo += (int)(exp[e-2] - '0') * 10; - int val=0; - if(base == 0){ - val = 0; - } - else if(exp[0] == '0' && expo==0){ - val = 1; - } - else{ - int inX = (base % 10); - int inY = (expo % 4); - val = vec[inX][inY]; - } - cout< + +using namespace std; + +int main() +{ + char ca[102], cb[102]; + int a, b, c; + for (;;) + { + scanf("%s%s", &ca, &cb); + if (ca[0] == '0' && ca[1] == '\0' && cb[0] == '0' && cb[1] == '\0') + { + break; + } + if (cb[0] == '0' && cb[1] == '\0') + { + printf("1\n"); + continue; + } + for (c = 0; ca[c] != '\0'; c++) + ; + a = (int)(ca[c - 1] - '0'); + for (c = 0; cb[c] != '\0'; c++) + ; + if (c >= 2) + { + b = (int)(cb[c - 2] - '0') * 10 + (int)(cb[c - 1] - '0'); + } + else + { + b = (int)(cb[c - 1] - '0'); + } + c = 1; + for (int i = 0; i < (b + 3) % 4 + 1; i++) + { + c = c * a % 10; + } + printf("%d\n", c); + } + return 0; +} diff --git a/10516.cpp b/10516.cpp new file mode 100644 index 0000000..cc2a9b6 --- /dev/null +++ b/10516.cpp @@ -0,0 +1,190 @@ +#include + +using namespace std; + +typedef struct +{ + unsigned n, a[63]; +} num_t; + +num_t *zero, *one, *ftab[64][64], *htab[64][64], *gtab[64][64][64]; + +void muladd(num_t *z, num_t *x, num_t *y) +{ + unsigned i, j, k, c; + for (k = x->n + y->n + 1; z->n < k;) + { + z->a[z->n++] = 0; + } + for (i = 0; i < x->n; i++) + { + for (j = c = 0, k = i; j < y->n; j++, k++, c >>= 16) + { + z->a[k] = (c += z->a[k] + x->a[i] * y->a[j]) & 0xFFFF; + } + for (; c > 0; k++, c >>= 16) + { + z->a[k] = (c += z->a[k]) & 0xFFFF; + } + } + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } +} + +void copy(num_t *x, num_t *y) +{ + int i; + for (x->n = y->n, i = 0; i < x->n; i++) + { + x->a[i] = y->a[i]; + } +} + +void add(num_t *z, num_t *x) +{ + unsigned i, c; + while (z->n < x->n) + { + z->a[z->n++] = 0; + } + for (i = c = 0; i < x->n; i++, c >>= 16) + { + z->a[i] = (c += x->a[i] + z->a[i]) & 0xFFFF; + } + for (; c > 0 && i < x->n; i++, c >>= 16) + { + z->a[i] = (c += z->a[i]) & 0xFFFF; + } + if (c) + { + z->a[z->n++] = c; + } +} + +num_t *make(unsigned x) +{ + static num_t *buf = NULL; + static unsigned left = 0; + if (left == 0) + { + left = 65536; + buf = (num_t *)malloc(left * sizeof(num_t)); + } + buf->a[0] = x; + buf->n = x ? 1 : 0; + return buf++; +} + +void mulby(num_t *x, num_t *y) +{ + static num_t t; + copy(&t, x); + x->n = 0; + muladd(x, &t, y); +} + +unsigned div1(num_t *x, unsigned d) +{ + unsigned i, r; + for (r = 0, i = x->n; i--; r %= d) + { + x->a[i] = (r = (r << 16) | x->a[i]) / d; + } + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } + return r; +} + +char *fmt(num_t *x) +{ + static num_t t; + static char buf[65536]; + char *s; + copy(&t, x); + for (*(s = buf + sizeof(buf) - 2) = '\0'; t.n > 0 || *s == '\0';) + { + *--s = div1(&t, 10) + '0'; + } + return s; +} + +num_t *H(int, int); + +/* G(n,d,k) = [H(n,d)]^k == \sum_{0 <= a_1,a_2,...,a_k <= d} F(n,a_1)...F(n,a_k) + G(n,d,0) = 1 */ +num_t *G(int n, int d, int k) +{ + if (k == 0) + { + return one; + } + if (d < 0) + { + return zero; + } + if (gtab[n][d][k]) + { + return gtab[n][d][k]; + } + gtab[n][d][k] = make(0); + muladd(gtab[n][d][k], G(n, d, k - 1), H(n, d)); + return gtab[n][d][k]; +} + +/* F(n,0) = 1 + F(n,d) = F(n,d-1) \sum_{0 <= k < n} G(n,d-2,k) G(n,d-1,n-1-k) */ +num_t *F(int n, int d) +{ + int k; + if (ftab[n][d]) + { + return ftab[n][d]; + } + if (d == 0) + { + return one; + } + ftab[n][d] = make(0); + for (k = 0; k < n; k++) + { + muladd(ftab[n][d], G(n, d - 2, k), G(n, d - 1, n - 1 - k)); + } + mulby(ftab[n][d], F(n, d - 1)); + return ftab[n][d]; +} + +/* H(n,d) = \sum_{0 <= i <= d} F(n,i) */ +num_t *H(int n, int d) +{ + if (d < 0) + { + return zero; + } + if (htab[n][d]) + { + return htab[n][d]; + } + htab[n][d] = make(0); + copy(htab[n][d], H(n, d - 1)); + add(htab[n][d], F(n, d)); + return htab[n][d]; +} + +int main() +{ + int n, d; + memset(ftab, 0, sizeof(ftab)); + memset(gtab, 0, sizeof(gtab)); + memset(htab, 0, sizeof(htab)); + zero = make(0); + one = make(1); + while (scanf("%d %d", &n, &d) == 2 && (n | d) != 0) + { + printf("%d %d %s\n", n, d, fmt(F(n, d))); + } + return 0; +} diff --git a/10517.cpp b/10517.cpp new file mode 100644 index 0000000..24f7587 --- /dev/null +++ b/10517.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int main() +{ + int test, i = 1; + double n, m, t; + scanf("%d", &test); + double angle, R = 6378.0, pi = 2 * acos(0), min, sec; + while (i <= test) + { + scanf("%lf%lf", &n, &m); + printf("Case %d: ", i++); + if (n >= 1001.0) + { + printf("Invalid or impossible.\n"); + continue; + } + if ((m > -0.0000001 && m < 0.0000001) || (n > -0.0000001 && n < 0.0000001)) + { + angle = pi / 2; + } + else + { + t = (m * cos(n / R) - n) / (m * sin(n / R)); + angle = atan(t); + } + double lim = 0.388888888888888888889 * pi; + if (angle < 0.000001 || angle > lim) + { + printf("Invalid or impossible.\n"); + } + else + { + angle *= (180 / pi); + int dg = (int)angle; + min = angle - (long)angle; + min *= 60; + int mn = (int)min; + sec = min - (long)min; + sec *= 60; + int sc = (int)(sec + 0.5);// sec should be rounded + printf("%d deg %d min %d sec\n", dg, mn, sc); + } + } + return 0; +} diff --git a/10518.cpp b/10518.cpp new file mode 100644 index 0000000..7c73bb2 --- /dev/null +++ b/10518.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +/**** +10518 +****/ +#define MAXN 1000000 +long dd[MAXN]; +int main() +{ + long long n; + long b, m; + long cases = 0; + while (1) + { + scanf("%lld%ld", &n, &b); + if (!n && !b) + { + break; + } + cases++; + dd[0] = 1; + dd[1] = 1; + for (m = 2; m < 1000000; m++) + { + dd[m] = (dd[m - 1] + dd[m - 2] + 1) % b; + if (dd[m] == 1 && dd[m - 1] == 1) + { + break; + } + } + m--; + printf("Case %ld: %lld %ld %ld\n", cases, n, b, dd[n % m]); + } + return 0; +} diff --git a/10519.cpp b/10519.cpp new file mode 100644 index 0000000..3ce9fd1 --- /dev/null +++ b/10519.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define limit 1000000000 +int main() +{ + int N[20], O[40]; + int n, len, i, j, k; + long long int tmp; + char in[120]; + while (gets(in)) + { + if (in[1] == '\0' && in[0] == '0') + { + printf("1\n"); + continue; + } + n = strlen(in); + len = (n - 1) / 9 + 1; + for (i = 0; i <= len; i++) + { + N[i] = 0; + } + for (i = 0; i < n; i++) + { + j = (n - i - 1) / 9; + N[j] = 10 * N[j] + in[i] - '0'; + } + n = 2 * len; + for (i = 0; i <= n; i++) + { + O[i] = 0; + } + for (i = 0; i < len; i++) + { + for (j = 0; j < len; j++) + { + tmp = O[i + j] + (long long int)N[i] * N[j]; + O[i + j] = tmp % limit; + O[i + j + 1] += tmp / limit; + } + } + if (!O[n - 1]) + { + n--; + } + O[0] += 2; + for (i = 0; i < len; i++) + { + O[i] -= N[i]; + } + for (i = 0; i < n; i++) + { + if (O[i] >= 0) + { + continue; + } + O[i + 1]--; + O[i] += limit; + } + printf("%d", O[n - 1]); + for (i = n - 2; i >= 0; i--) + { + printf("%09d", O[i]); + } + printf("\n"); + } + return 0; +} diff --git a/1052.cpp b/1052.cpp new file mode 100644 index 0000000..f24b24c --- /dev/null +++ b/1052.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +typedef long long ll; + +const int N = 40; + +enum {NO, UNIQUE, NOT_UNIQUE}; + +int l, n, tc; +map> dp[N]; +string s; + +int solve(int i, int ones, int zeroes) +{ + if (ones < 0 || zeroes < 0) + { + return NO; + } + if (i == s.length()) + { + return ones == 0 && zeroes == 0 ? UNIQUE : NO; + } + if (dp[i][ones].count(zeroes)) + { + return dp[i][ones][zeroes]; + } + if (s[i] == '0') + { + return dp[i][ones][zeroes] = solve(i + 1, ones, zeroes - 1); + } + ll def = 0; + int ans = 0; + for (int j = i; j < s.length(); j++) + { + def = (def << 1) | (s[j] == '1'); + if (def != 2 && (j + 1 >= s.length() || s[j + 1] == '0')) + { + ans += solve(j + 1, ones - def, zeroes); + if (def == 3) + { + ans += solve(j + 1, ones - 2, zeroes); // special case + } + } + if (def > ones) + { + break; + } + if (ans >= NOT_UNIQUE) + { + return dp[i][ones][zeroes] = NOT_UNIQUE; + } + } + return dp[i][ones][zeroes] = ans; +} + +string outp[] = {"NO", "YES", "NOT UNIQUE"}; + +int main() +{ + while (cin >> l >> n && (n || l)) + { + cin >> s; + for (int i = 0; i < s.length(); i++) + { + dp[i].clear(); + } + int ans = solve(0, n, l - n); + cout << "Case #" << ++tc << ": " << outp[ans] << endl; + } + return 0; +} diff --git a/10520.cpp b/10520.cpp new file mode 100644 index 0000000..88dd913 --- /dev/null +++ b/10520.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back +#define D distance_ls_p +#define NEXT(i, n) ((i + 1) % n) +typedef long long ll; +ll table[30][30]; +ll dp(int i, int j, int n) +{ + if (i == 0) + { + return 0; + } + if (table[i][j] != -1) + { + return table[i][j]; + } + ll ret = 0; + if (i < j) + { + REP(k, i, j) + ret = max(ret, dp(i, k, n) + dp(k + 1, j, n)); + } + else if (i >= j) + { + ll ret2 = 0; + if (i < n) + { + REP(k, i + 1, n + 1) + { + ret = max(ret, dp(k, 1, n) + dp(k, j, n)); + } + } + if (j > 0) + { + REP(k, 1, j) + { + ret2 = max(ret2, dp(i, k, n) + dp(n, k, n)); + } + } + ret += ret2; + } + return table[i][j] = ret; +} +main() +{ + int n, an; + while (cin >> n >> an) + { + rep(i, 21) rep(j, 21) table[i][j] = -1; + table[n][1] = an; + ll ans = dp(1, n, n); + cout << ans << endl; + } +} diff --git a/10521.cpp b/10521.cpp new file mode 100644 index 0000000..d8d0a44 --- /dev/null +++ b/10521.cpp @@ -0,0 +1,351 @@ +#include + +using namespace std; + +typedef struct +{ + unsigned n, a[512]; + int sign; +} num_t; + +void set1(num_t *x, int c) +{ + x->sign = (c < 0) ? -1 : 1; + x->a[0] = c * x->sign; + x->n = (c != 0) ? 1 : 0; +} + +void muladd1(num_t *x, unsigned m, unsigned c) +{ + unsigned i; + for (i = 0; i < x->n; i++, c >>= 16) + { + x->a[i] = (c += m * x->a[i]) & 0xFFFF; + } + if (c) + { + x->a[x->n++] = c; + } +} + +void uinc(num_t *x) +{ + unsigned i; + for (i = 0; i < x->n; i++) + if (++x->a[i] < 0x10000) + { + return; + } + else + { + x->a[i] = 0; + } + x->a[x->n++] = 1; +} + +void usub(num_t *z, num_t *x, num_t *y) +{ + unsigned i, c; + assert(x->n >= y->n); + for (i = 0, c = 0; i < y->n; i++) + { + c += y->a[i]; + if (x->a[i] >= c) + { + z->a[i] = x->a[i] - c; + c = 0; + } + else + { + z->a[i] = x->a[i] + 0x10000 - c; + c = 1; + } + } + if (c) + { + for (; i < x->n; i++) + { + z->a[i] = x->a[i] - 1; + if (x->a[i] > 0) + { + break; + } + else + { + z->a[i] = 0; + } + } + assert(i < x->n); + } + for (; i < x->n; i++) + { + z->a[i] = x->a[i]; + } + z->n = x->n; + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } +} + +void cvt(num_t *x, char *s) +{ + unsigned m, a; + set1(x, 0); + for (m = 1, a = 0; *s; s++) + { + if ('0' <= *s && *s <= '9') + { + m *= 10; + a = a * 10 + *s - '0'; + if (m == 10000) + { + muladd1(x, m, a); + m = 1; + a = 0; + } + } + else if (*s == '-') + { + x->sign *= -1; + } + } + muladd1(x, m, a); + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } + if (x->n == 0) + { + x->sign = 1; + } +} + +void lshift(num_t *x, unsigned s) +{ + unsigned i, c; + for (i = 0, c = 0; i < x->n; i++, c >>= 16) + { + x->a[i] = (c |= x->a[i] << s) & 0xFFFF; + } + if (c != 0) + { + x->a[x->n++] = c; + } +} + +void rshift(num_t *x, unsigned s) +{ + unsigned i, c; + for (i = x->n, c = 0; i--; c <<= 16) + { + x->a[i] = ((c |= x->a[i]) >> s) & 0xFFFF; + } +} + +unsigned udiv1(num_t *q, num_t *x, unsigned y) +{ + unsigned i, r; + for (i = x->n, r = 0; i--;) + { + r = (r << 16) | x->a[i]; + q->a[i] = r / y; + r %= y; + } + for (q->n = x->n; q->n > 0 && q->a[q->n - 1] == 0; q->n--) + ; + return r; +} + +char *fmt(num_t *x) +{ + static char buf[8][65536]; + static int t[65536], z = 0; + static num_t y; + int i, j; + char *s; + if (x->n == 0) + { + return "0"; + } + s = buf[z = (z + 1) & 7]; + if (x->sign < 0) + { + *s++ = '-'; + } + for (y.sign = 1, y.n = j = x->n, i = 0; i < j; i++) + { + y.a[i] = x->a[i]; + } + for (i = 0; i == 0 || y.n > 0; i++) + { + t[i] = udiv1(&y, &y, 10000); + } + for (j = 0; i--; j++) + { + s += sprintf(s, j ? "%.4d" : "%d", t[i]); + while (*s) + { + s++; + } + } + return buf[z]; +} + +void udiv(num_t *q, num_t *r, num_t *x, num_t *y) +{ + unsigned c, m, yh, yh2; + int i, j, ns, rp, qp; + assert(y->n >= 1 && y->a[y->n - 1] != 0); + if (y->n == 1) + { + r->a[0] = udiv1(q, x, y->a[0]); + r->n = (r->a[0] != 0) ? 1 : 0; + return; + } + if (x != r) + for (r->n = x->n, i = 0; i < x->n; i++) + { + r->a[i] = x->a[i]; + } + if (x->n < y->n) + { + q->n = 0; + return; + } + for (i = 0, c = y->a[y->n - 1]; ((c << i) & 0x8000) == 0; i++) + ; + ns = i; + if (i > 0) + { + lshift(r, i); + lshift(y, i); + } + r->a[r->n] = 0; + q->n = qp = 1 + r->n - y->n; + y->a[y->n] = 0; + yh = y->a[y->n - 1]; + yh2 = y->a[y->n - 2]; + for (rp = r->n - 1; qp--; rp--) + { + c = (r->a[rp + 1] << 16) | r->a[rp]; + m = c / yh; + if (m > 0xFFFF) + { + m = 0xFFFF; + } + while ((c - m * yh) < 0x10000 && + (((c - m * yh) << 16) + r->a[rp - 1]) < (m * yh2)) + { + m--; + } + q->a[qp] = m; + for (c = 0, i = qp, j = 0; j <= y->n; i++, j++) + { + c += m * y->a[j]; + if (r->a[i] >= (c & 0xFFFF)) + { + r->a[i] -= c & 0xFFFF; + c >>= 16; + } + else + { + r->a[i] = r->a[i] + 0x10000 - (c & 0xFFFF); + c = (c >> 16) + 1; + } + } + if (c != 0) + { + q->a[qp]--; + assert(c == 1); + for (c = 0, i = qp, j = 0; j <= y->n; i++, j++, c >>= 16) + { + r->a[i] = (c += r->a[i] + y->a[j]) & 0xFFFF; + } + assert(c == 1); + } + } + rshift(r, ns); + rshift(y, ns); + while (r->n > 0 && r->a[r->n - 1] == 0) + { + r->n--; + } + while (q->n > 0 && q->a[q->n - 1] == 0) + { + q->n--; + } +} + +void sdiv(num_t *q, num_t *r, num_t *x, num_t *y) +{ + int s; + if (x->n == 0) + { + x->sign = 1; + } + s = x->sign * y->sign; + udiv(q, r, x, y); + if (s > 0) + { + q->sign = r->sign = 1; + } + else + { + if (r->n != 0) + { + uinc(q); + usub(r, y, r); + } + q->sign = -1; + r->sign = 1; + } +} + +int main() +{ + static char s1[65536], s2[65536], s3[65536]; + static num_t t1, t2, t3, q; + num_t *x = &t1, *y = &t2, *z = &t3, *w; + int k; + while (gets(s3) && sscanf(s3, " %s %s", s1, s2) == 2) + { + cvt(x, s1); + cvt(y, s2); + printf("%s/%s = ", fmt(x), fmt(y)); + if (x->sign < 0 && y->sign < 0) + { + x->sign = y->sign = 1; + } + if (y->sign < 0) + { + y->sign = 1; + x->sign = -1; + } + for (k = 0;;) + { + sdiv(&q, z, x, y); + printf("%s", fmt(&q)); + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } + if (z->n == 0) + { + break; + } + printf("+1/{"); + k++; + w = x; + x = y; + y = z; + z = w; + } + while (k--) + { + putchar('}'); + } + printf("\n"); + } + return 0; +} diff --git a/10522.cpp b/10522.cpp new file mode 100644 index 0000000..9e384f6 --- /dev/null +++ b/10522.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +typedef long double elem; + +const elem pi = acos(-1); +const elem eps = 1.0e-8; + +bool eq(elem a, elem b) +{ + return abs(b - a) < eps; +} +bool eqlt(elem a, elem b) +{ + return eq(a, b) || a < b; +} +bool eqgt(elem a, elem b) +{ + return eq(a, b) || a > b; +} + +bool isInvalidTriangle(elem Ha, elem Hb, elem Hc, elem &S) +{ + if (eqlt(Ha, 0) || eqlt(Hb, 0) || eqlt(Hc, 0)) + { + return true; + } + elem s = 1. / pow(Hb, 2) + 1. / pow(Hc, 2) - 1. / pow(Ha, 2); + elem CosA = ((Hb * Hc) * (s / 2)); + if (eqlt(CosA, -1) || eqgt(CosA, 1)) + { + return true; + } + elem SinA = sin(acos(CosA)); + S = (Hb * Hc) / (2 * SinA); + elem a = 2 * S / Ha; + elem b = 2 * S / Hb; + elem c = 2 * S / Hc; + if (eqlt(a + b, c) || eqlt(a + c, b) || eqlt(b + c, a)) + { + return true; + } + else + { + return false; + } +} + +int main() +{ + int n; + int n_invalid = 0; + scanf("%d", &n); + while (true) + { + bool bInvalid; + elem Ha, Hb, Hc, S; + assert(n > 0); + if (3 != scanf("%Lf%Lf%Lf", &Ha, &Hb, &Hc)) + { + break; + } + bInvalid = isInvalidTriangle(Ha, Hb, Hc, S); + if (bInvalid) + { + n_invalid++; + printf("These are invalid inputs!\n"); + if (n_invalid >= n) + { + break; + } + } + else + { + printf("%.3Lf\n", S); + } + } + return 0; +} diff --git a/10523.cpp b/10523.cpp new file mode 100644 index 0000000..1582cc1 --- /dev/null +++ b/10523.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define limit 10000000 +int T[16][160][30] = {0}, len[16][160] = {0}; +int main() +{ + int i, j, k; + /* process i^j */ + for (i = 0; i < 16; i++) + { + T[i][0][0] = len[i][0] = 1; + for (j = 1; j < 151; j++) + { + len[i][j] = len[i][j - 1]; + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k] = T[i][j - 1][k] * i; + } + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k + 1] += T[i][j][k] / limit; + T[i][j][k] %= limit; + } + if (T[i][j][len[i][j]]) + { + len[i][j]++; + } + } + } + /* process j*i^j */ + for (i = 0; i < 16; i++) + { + for (j = 0; j < 151; j++) + { + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k] *= j; + } + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k + 1] += T[i][j][k] / limit; + T[i][j][k] %= limit; + } + if (T[i][j][len[i][j]]) + { + len[i][j]++; + } + } + } + /* process sigma */ + for (i = 0; i < 16; i++) + { + for (j = 1; j < 151; j++) + { + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k] += T[i][j - 1][k]; + } + for (k = 0; k < len[i][j]; k++) + { + T[i][j][k + 1] += T[i][j][k] / limit; + T[i][j][k] %= limit; + } + if (T[i][j][len[i][j]]) + { + len[i][j]++; + } + } + } + while (scanf("%d%d", &j, &i) == 2) + { + printf("%d", T[i][j][len[i][j] - 1]); + for (k = len[i][j] - 2; k >= 0; k--) + { + printf("%07d", T[i][j][k]); + } + printf("\n"); + } + return 0; +} diff --git a/10524.cpp b/10524.cpp new file mode 100644 index 0000000..30134a7 --- /dev/null +++ b/10524.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +double matrix[100][200]; +int n; +bool solve() +{ + for (int i = 0; i < n; i++) + { + if (matrix[i][i] == 0.0) + { + return false; + } + double base = matrix[i][i]; + for (int j = i; j < 2 * n; j++) + { + matrix[i][j] /= base; + } + for (int j = 0; j < n; j++) + { + if (i != j && matrix[j][i]) + { + double p = matrix[j][i] / matrix[i][i]; + for (int k = i; k < 2 * n; k++) + { + matrix[j][k] -= p * matrix[i][k]; + } + } + } + } + return true; +} +int main() +{ + while (scanf("%d", &n), n) + { + for (int i = 0; i < n; i++) + for (int j = 0; j < 2 * n; j++) + { + matrix[i][j] = 0; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + scanf("%lf", &matrix[i][j]); + } + for (int i = 0; i < n; i++) + { + matrix[i][n + i] = 1.0; + } + if (!solve()) + { + puts("Not possible"); + } + else + { + for (int i = 0; i < n; i++) + { + for (int j = n; j < 2 * n; j++) + { + if (j != n) + { + printf(" "); + } + printf("%.6lf", matrix[i][j]); + } + printf("\n"); + } + } + puts(""); + } + return 0; +} diff --git a/10525.cpp b/10525.cpp new file mode 100644 index 0000000..00e9e2a --- /dev/null +++ b/10525.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +struct edge +{ + int u_;// src vertex + int v_;// dst vertex + int t_;// time + int l_;// len + + edge() : u_(-1), v_(-1), t_(-1), l_(-1) {} + edge(int u, int v, int t, int l) : u_(u), v_(v), t_(t), l_(l) {} +}; + +pair bellman_ford(int n, int s, int t, const vector &edges) +{ + vector > dists(n, make_pair(numeric_limits::max(), numeric_limits::max())); + dists[s].first = dists[s].second = 0; + for (int i = 0; i < n - 1; i++) + { + for (size_t j = 0, je = edges.size(); j < je; j++) + { + const edge &e = edges[j]; + long long lt = static_cast(dists[e.u_].first), + ll = static_cast(dists[e.u_].second); + if (lt + e.t_ < dists[e.v_].first || + lt + e.t_ == dists[e.v_].first && ll + e.l_ < dists[e.v_].second) + { + dists[e.v_] = make_pair(dists[e.u_].first + e.t_, dists[e.u_].second + e.l_); + } + } + } + return dists[t]; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n, m; + scanf("%d%d", &n, &m); + vector edges(m * 2); + for (int i = 0; i < m; i++) + { + int a, b, c, d; + scanf("%d%d%d%d", &a, &b, &c, &d); + a--; + b--; + edges[i * 2].u_ = edges[i * 2 + 1].v_ = a; + edges[i * 2].v_ = edges[i * 2 + 1].u_ = b; + edges[i * 2].t_ = edges[i * 2 + 1].t_ = c; + edges[i * 2].l_ = edges[i * 2 + 1].l_ = d; + } + int q; + scanf("%d", &q); + while (q--) + { + int s, t; + scanf("%d%d", &s, &t); + pair d = bellman_ford(n, s - 1, t - 1, edges); + if (d.first != numeric_limits::max()) + { + printf("Distance and time to reach destination is %d & %d.\n", d.second, d.first); + } + else + { + printf("No Path.\n"); + } + } + if (T) + { + printf("\n"); + } + } + return 0; +} diff --git a/10526.cpp b/10526.cpp new file mode 100644 index 0000000..15ebd36 --- /dev/null +++ b/10526.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +struct Seg +{ + char *start; + int len; +} seg[50000]; +char str[50000], *s[50000], TDP[50000], JCN[50000]; +int k, lenTDP, lenJCN, ptr, prev_, flag; + +int cmp(const void *a, const void *b) +{ + char *c = *(char **)a, *d = *(char **)b; + return strcmp(c, d); +} + +int cmps(const void *a, const void *b) +{ + Seg *c = (Seg *)a, *d = (Seg *)b; + if (c->len != d->len) + { + return d->len - c->len; + } + return c->start - d->start; +} + +int comp(const char *s, const char *d, int len) +{ + int i; + for (i = 0; i < len; i++) + { + if (s[i] < d[i]) + { + return -1; + } + if (s[i] > d[i]) + { + return 1; + } + } + return 0; +} + +bool BinarySearch(char *str, int len) +{ + int left = 0, right = lenTDP - 1, mid = (left + right) / 2, other; + while (left <= right) + { + int c = comp(s[mid], str, len); + if (c == 0) + { + return true; + } + else if (c < 0) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + mid = (left + right) / 2; + } + return false; +} + +void add(char *start, int l, int last) +{ + seg[ptr].start = start; + seg[ptr++].len = l; + prev_ = last; + flag = 1; +} + +int main() +{ + int caseno = 0; + while (gets(str), sscanf(str, "%d", &k), k) + { + if (caseno++) + { + puts(""); + } + ptr = lenTDP = lenJCN = 0, prev_ = -1; + strcpy(TDP, ""); + strcpy(JCN, ""); + gets(str); + while (gets(str), strcmp(str, "END TDP CODEBASE")) + { + strcat(TDP, str), strcat(TDP, "\n"); + } + gets(str); + while (gets(str), strcmp(str, "END JCN CODEBASE")) + { + strcat(JCN, str), strcat(JCN, "\n"); + } + lenTDP = strlen(TDP), lenJCN = strlen(JCN); + for (int i = 0; i < lenTDP; i++) + { + s[i] = TDP + i; + } + qsort(s, lenTDP, sizeof(char *), cmp); + for (int i = 0; i < lenJCN; i++) + { + if (BinarySearch(JCN + i, lenJCN - i)) + { + add(JCN + i, lenJCN - i, lenJCN - 1); + break; + } + if (!BinarySearch(JCN + i, 1)) + { + continue; + } + for (int j = i + 1; j < lenJCN; j++) + { + if (j < prev_ + 1) + { + continue; + } + if (!BinarySearch(JCN + i, j - i + 1)) + { + if (j > prev_ + 1) + { + add(JCN + i, j - i, j - 1); + } + break; + } + } + } + qsort(seg, ptr, sizeof(Seg), cmps); + printf("CASE %d\n", caseno); + for (int i = 0; i < k && i < ptr; i++) + { + printf("INFRINGING SEGMENT %d LENGTH %d POSITION %d\n", + i + 1, seg[i].len, seg[i].start - JCN); + for (int j = 0; j < seg[i].len; j++) + { + printf("%c", *(seg[i].start + j)); + } + printf("\n"); + } + } + return 0; +} diff --git a/10527.cpp b/10527.cpp new file mode 100644 index 0000000..09edea0 --- /dev/null +++ b/10527.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +const int MAXSIZE = 10010; + +char num[MAXSIZE]; +int numLen; + +bool Input() +{ + cin.getline(num, MAXSIZE); + numLen = strlen(num); + if (num[0] == '-') + { + return false; + } + else + { + return true; + } +} + +int divisible(int fact) +{ + char temp[MAXSIZE]; + int tempLen = 0, carry = 0, i; + for (i = 0; i < numLen; i++) + { + temp[tempLen++] = (carry * 10 + num[i] - '0') / fact + '0'; + carry = (carry * 10 + num[i] - '0') % fact; + } + if (!carry)//divisible + { + i = numLen = 0; + while (temp[i] == '0') + { + i++; + } + for (; i < tempLen; i++) + { + num[numLen++] = temp[i]; + } + // cout<<"it is divisible"< 1) + { + int fact; + bool flg = false; + for (fact = 9; fact > 1; fact--) + { + if (divisible(fact)) + { + result[resLen++] = fact; + flg = true; + break; + } + } + if (!flg) + { + loop = false; + } + } + if (numLen > 1) + { + cout << "There is no such number." << endl; + } + else + { + int i; + cout << num[0]; + for (i = resLen - 1; i >= 0; i--) + { + cout << result[i]; + } + cout << endl; + } +} + +int main() +{ + while (Input()) + { + solve(); + } + return 0; +} diff --git a/10528.cpp b/10528.cpp new file mode 100644 index 0000000..df1719b --- /dev/null +++ b/10528.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int main() +{ + bool result[12]; + string input; + int diff[8] = {0, 2, 4, 5, 7, 9, 11, 12}; + const string notes[25] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C"}; + vector scales[12]; + for (int i = 0; i < 12; i++) + { + for (int j = 0; j < 8; j++) + { + scales[i].push_back(notes[i + diff[j]]); + } + } + for (int i = 0; i < 12; i++) + { + result[i] = true; + } + while (cin >> input) + { + if (input == "END") + { + break; + } + for (int i = 0; i < 12; i++) + { + if (find(scales[i].begin(), scales[i].end(), input) == scales[i].end()) + { + result[i] = false; + } + } + if (cin.peek() == '\n') + { + bool first = true; + for (int i = 0; i < 12; i++) + { + if (result[i] == true) + { + if (!first) + { + cout << " "; + } + cout << notes[i]; + first = false; + } + result[i] = true; + } + cout << endl; + } + } + return 0; +} diff --git a/10529.cpp b/10529.cpp new file mode 100644 index 0000000..b5131ef --- /dev/null +++ b/10529.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +double a[1001], left_, right_; +int n; + +int main() +{ + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + scanf("%lf %lf", &left_, &right_); + for (int i = 1; i <= n; i++) + { + a[i] = 99999999; + for (int j = 0; j < i; j++) + if (a[j] + a[i - 1 - j] + (1 + left_ * a[j] + right_ * a[i - 1 - j]) / (1 - left_ - right_) < a[i]) + { + a[i] = a[j] + a[i - 1 - j] + (1 + left_ * a[j] + right_ * a[i - 1 - j]) / (1 - left_ - right_); + } + } + printf("%.2lf\n", a[n]); + } + return 0; +} diff --git a/10530.cpp b/10530.cpp new file mode 100644 index 0000000..8571132 --- /dev/null +++ b/10530.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int num; +char cmd[30]; +int MIN, MAX; +bool cheat; + +void Input() +{ + cin.get(); + while (cin.getline(cmd, 30)) + { + if (!strcmp(cmd, "right on")) + { + break; + } + if (!strcmp(cmd, "too high")) + { + if (num <= MAX) + { + MAX = num - 1; + } + } + if (!strcmp(cmd, "too low")) + { + if (num >= MIN) + { + MIN = num + 1; + } + } + if (MIN > MAX) + { + cheat = true; + } + if (num > MAX && !strcmp(cmd, "too low")) + { + cheat = true; + } + if (num < MIN && !strcmp(cmd, "too high")) + { + cheat = true; + } + // cout<<"["<> num; + cin.get(); + } + if (MIN > MAX) + { + cheat = true; + } + if (num < MIN || num > MAX) + { + cheat = true; + } +} + +int main() +{ + while (cin >> num && num) + { + cheat = false; + MIN = 0; + MAX = 11; + Input(); + if (cheat) + { + cout << "Stan is dishonest" << endl; + } + else + { + cout << "Stan may be honest" << endl; + } + } + return 0; +} diff --git a/10531.cpp b/10531.cpp new file mode 100644 index 0000000..6e79d04 --- /dev/null +++ b/10531.cpp @@ -0,0 +1,230 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +int fgot[8][1024], enforce_r, enforce_b, id, n, m; +double P[8][8], R[8][8], ftab[8][1024], xtab[8][1024]; + +int jtab[6][1024][32]; + +int min(int x, int y) +{ + return (x < y) ? x : y; +} +int max(int x, int y) +{ + return (x > y) ? x : y; +} + +int join(int A, int B) +{ + int a[8], b[8], c[8], i, j, k, t, s, chg; + for (i = 0; i < m; i++) + { + a[i] = (A >> (2 * i)) & 3; + b[i] = (B >> i) & 1; + c[i] = 0; + } + for (i = 0; i < m; i++) + { + if (a[i] != 1) + { + continue; + } + for (j = i; j >= 0 && b[j] == 0; j--) + { + c[j] = 1; + } + for (j = i; j < m && b[j] == 0; j++) + { + c[j] = 1; + } + } + for (i = 0; i < m && c[i] != 1; i++) + ; + if (i >= m) + { + return 0; + } + for (i = 0, k = 1;;) + { + while (i < m && (b[i] || c[i])) + { + i++; + } + if (i >= m) + { + break; + } + for (k++; i < m && b[i] == 0; i++) + { + c[i] = k; + } + } + do + { + chg = 0; + for (i = 0; i < m; i++) + { + for (j = i + 1; j < m; j++) + { + if (c[i] != c[j] || c[i] == 0) + { + continue; + } + if (a[i] == a[j] || a[i] == 0 || a[j] == 0) + { + continue; + } + t = max(a[i], a[j]); + s = min(a[i], a[j]); + for (k = 0; k < m; k++) + if (a[k] == t) + { + a[k] = s; + chg = 1; + } + } + } + for (i = 0; i < m; i++) + { + for (j = i + 1; j < m; j++) + { + if (a[i] != a[j] || a[i] == 0) + { + continue; + } + if (c[i] == c[j] || c[i] == 0 || c[j] == 0) + { + continue; + } + t = max(c[i], c[j]); + s = min(c[i], c[j]); + for (k = 0; k < m; k++) + if (c[k] == t) + { + c[k] = s; + chg = 1; + } + } + } + } while (chg); + for (k = 0, i = 0; i < m; i++) + { + k |= c[i] << (2 * i); + } + return k; +} + +double f(int r, int A) +{ + int i, B; + double p; + if (fgot[r][A] == id) + { + return ftab[r][A]; + } + fgot[r][A] = id; + if (r == n) + { + return ftab[r][A] = (((A >> (2 * (m - 1))) & 3) == 1) ? 1. : 0.; + } + if (A == 0) + { + return ftab[r][A] = 0.; + } + if (enforce_r == r) + { + return ftab[r][A] = f(r + 1, jtab[m][A][enforce_b]); + } + ftab[r][A] = 0.; + for (B = 0; B < (1 << m); B++) + { + for (p = 1., i = 0; i < m; i++) + { + p *= (B & (1 << i)) ? P[r][i] : (1. - P[r][i]); + } + if (p < EPS) + { + continue; + } + ftab[r][A] += p * f(r + 1, jtab[m][A][B]); + } + return ftab[r][A]; +} + +void solve() +{ + int i, j, r; + double p, q; + id++; + enforce_r = -1; + q = f(0, 1); + for (r = 0; r < n; r++) + { + for (i = 0; i < m; i++) + { + R[r][i] = 0.; + } + enforce_r = r; + for (enforce_b = 0; enforce_b < (1 << m); enforce_b++) + { + for (p = 1., i = 0; i < m; i++) + { + p *= (enforce_b & (1 << i)) ? P[r][i] : (1. - P[r][i]); + } + if (p < EPS) + { + continue; + } + id++; + p *= f(0, 1); + for (i = 0; i < m; i++) + if (enforce_b & (1 << i)) + { + R[r][i] += p; + } + } + for (i = 0; i < m; i++) + { + R[r][i] /= q; + } + } + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + printf(j ? " %.6f" : "%.6f", R[j][i]); + } + printf("\n"); + } +} + +int main() +{ + int i, j, t; + for (m = 1; m <= 5; m++) + for (i = 0; i < (1 << (2 * m)); i++) + for (j = 0; j < (1 << m); j++) + { + jtab[m][i][j] = join(i, j); + } + id = 42; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &m, &n) == 2;) + { + assert(1 <= n && n <= 6 && 1 <= m && m <= 5); + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + { + scanf("%lf", &P[j][i]); + } + solve(); + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/10532.cpp b/10532.cpp new file mode 100644 index 0000000..2057b3e --- /dev/null +++ b/10532.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +/* +Combination Once Again +10532 +*/ +typedef unsigned long long ss; +int F[52], A[52], Fre[52]; +ss R[52][52], Sum; +int N, M, ind; +ss Count(int type, int r, int sum) +{ + int i; + ss ways = 0; + if (Sum - sum < r) + { + return 0; + } + if (r == 0) + { + return 1; + } + if (R[type][r]) + { + return R[type][r]; + } + if (type == 0 && r <= A[0]) + { + R[type][r] = 1; + return 1; + } + if (type == 0) + { + return 0; + } + for (i = 0; i <= A[type] && r - i >= 0; i++) + { + ways += Count(type - 1, r - i, sum + Fre[type]); + } + R[type][r] = ways; + return ways; +} +void Cal() +{ + int i, n; + for (i = 0; i < M; i++) + { + scanf("%d", &n); + printf("%llu\n", Count(ind - 1, n, 0)); + } +} +void Free() +{ + int i, j; + for (i = 0; i <= N; i++) + { + F[i] = 0; + for (j = 0; j <= N; j++) + { + R[i][j] = 0; + } + } +} +int main() +{ + int i, n, kase = 1; + while (scanf("%d%d", &N, &M) == 2) + { + if (N == 0) + { + break; + } + Sum = 0; + for (i = 0; i < N; i++) + { + scanf("%d", &n); + F[n]++; + } + ind = 0; + for (i = 0; i <= N; i++) + { + if (F[i]) + { + A[ind++] = F[i]; + Sum += F[i]; + Fre[ind - 1] = F[i]; + } + } + printf("Case %d:\n", kase++); + Cal(); + Free(); + } + return 0; +} diff --git a/10533.cpp b/10533.cpp index 2894a8f..aeb6870 100644 --- a/10533.cpp +++ b/10533.cpp @@ -1,72 +1,66 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define MAX 1000000 +#include -bool prime[MAX + 1]; -int dPrime[MAX + 1]; +using namespace std; -int dSum(int N) { - int T = 0; - while (N) { - T += (N % 10); - N /= 10; +int p[1000010]; +int digit[1000010]; +void compute_prime_table() /* with Sieve of Eratosthenes */ +{ + int i, j; + p[0] = p[1] = 0; + for (i = 2; i <= 1000000; i++) + { + p[i] = 1; /* initialization */ + } + for (i = 2; i <= 1000;) /* for all primes up to 1000 */ + { + for (j = i + i; j <= 1000000; j += i) + { + p[j] = 0; /* delete all multiples of i */ + } + for (i++; !p[i]; i++) + ; /* find next prime */ } - return T; } -void seive() { - long long i, j; - for (i = 0; i <= MAX; i++) { - prime[i] = true; - dPrime[i] = 0; +int digit_sum(int n) +{ + int sum = 0; + while (n) + { + sum += n % 10; + n /= 10; } - prime[0] = false; - prime[1] = false; - for (i = 2; i <= MAX; i++) { - dPrime[i] = dPrime[i - 1]; - if (prime[i]) { - if (prime[dSum(i)]) ++dPrime[i]; - for (j = i * i; j <= MAX; j += i) - prime[j] = false; + return sum; +} + +void get_digit_prime_table() +{ + compute_prime_table(); + int i; + digit[0] = 0; + for (i = 1; i <= 1000000; i++) + { + if (p[i] && p[digit_sum(i)]) + { + digit[i] = digit[i - 1] + 1; + } + else + { + digit[i] = digit[i - 1]; } } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int N; - int A, B; +int main() +{ + get_digit_prime_table(); + int N, t, t1, t2; scanf("%d", &N); - while (N--) { - scanf("%d%d", &A, &B); - printf("%d\n", dPrime[B] - dPrime[A - 1]); + for (t = 0; t < N; t++) + { + scanf("%d %d", &t1, &t2); + printf("%d\n", digit[t2] - digit[t1 - 1]); } return 0; } diff --git a/10534.cpp b/10534.cpp new file mode 100644 index 0000000..13d4e26 --- /dev/null +++ b/10534.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a); +#define MAXN 10005 + +int SEQ[MAXN], TEMP[MAXN], ORDER[MAXN], N, MAX, LIMIT; + +// binary search +int FIND_BEST(int key) +{ + int lo, up, mid; + lo = 1; + up = LIMIT; + mid = (lo + up) / 2; + if (TEMP[1] > key) + { + return 1; + } + else if (TEMP[LIMIT] < key) + { + return LIMIT + 1; + } + else if (TEMP[LIMIT] == key) + { + return LIMIT; + } + while (lo < up && TEMP[mid] != key) + { + if (TEMP[mid] < key) + { + lo = mid + 1; + } + else if (TEMP[mid] > key) + { + if (TEMP[mid - 1] < key) + { + return mid; + } + up = mid - 1; + } + mid = (lo + up) / 2; + } + return mid; +} + +void FIND_LIS() +{ + int i, pos; + TEMP[1] = SEQ[0]; + ORDER[0] = 1; + LIMIT = 1; + for (i = 1; i < N; i++) + { + pos = FIND_BEST(SEQ[i]); + if (pos > LIMIT) + { + LIMIT = pos; + TEMP[pos] = SEQ[i]; + } + else if (TEMP[pos] > SEQ[i]) + { + TEMP[pos] = SEQ[i]; + } + ORDER[i] = pos; + } +} + +void FIND_LDS() +{ + int i, pos, lis; + MAX = 1; + LIMIT = 1; + TEMP[1] = SEQ[N - 1]; + for (i = N - 2; i >= 0; i--) + { + pos = FIND_BEST(SEQ[i]); + if (pos > LIMIT) + { + LIMIT = pos; + TEMP[pos] = SEQ[i]; + } + else if (TEMP[pos] > SEQ[i]) + { + TEMP[pos] = SEQ[i]; + } + lis = MIN(ORDER[i], pos); + if (lis > MAX) + { + MAX = lis; + } + } + printf("%d\n", MAX * 2 - 1); +} + +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + for (i = 0; i < N; i++) + { + scanf("%d", &SEQ[i]); + } + FIND_LIS(); + FIND_LDS(); + } + return 0; +} diff --git a/10535.cpp b/10535.cpp new file mode 100644 index 0000000..21319ae --- /dev/null +++ b/10535.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +int ax[1024], ay[1024], bx[1024], by[1024], n; + +int max(int x, int y) +{ + return (x > y) ? x : y; +} + +int f(int x, int y) +{ + int i, k, t, s; + if (x == 0 && y == 0) + { + return 0; + } + for (i = k = 0; i < n; i++) + { + t = ax[i] * y - ay[i] * x; + s = bx[i] * y - by[i] * x; + if ((t > 0 && s > 0) || (t < 0 && s < 0)) + { + continue; + } + t = ax[i] * (ay[i] - by[i]) - (ax[i] - bx[i]) * ay[i]; + s = x * (ay[i] - by[i]) - (ax[i] - bx[i]) * y; + if (s != 0) + { + if ((t >= 0 && s >= 0) || (t <= 0 && s <= 0)) + { + k++; + } + } + else + { + s = ax[i] * x + ay[i] * y; + t = bx[i] * x + by[i] * y; + if (s >= 0 || t >= 0) + { + k++; + } + } + } + return k; +} + +int main() +{ + int i, j, x0, y0; + while (scanf("%d", &n) == 1 && n > 0) + { + for (i = 0; i < n; i++) + { + scanf("%d %d %d %d", &ax[i], &ay[i], &bx[i], &by[i]); + } + scanf("%d %d", &x0, &y0); + for (i = 0; i < n; i++) + { + ax[i] -= x0; + ay[i] -= y0; + bx[i] -= x0; + by[i] -= y0; + } + for (i = j = 0; i < n; i++) + { + j = max(j, max(f(ax[i], ay[i]), f(bx[i], by[i]))); + } + printf("%d\n", j); + } + return 0; +} diff --git a/10536.cpp b/10536.cpp new file mode 100644 index 0000000..cacb4af --- /dev/null +++ b/10536.cpp @@ -0,0 +1,193 @@ +#include + +using namespace std; + +#define maxn 65539 + +int B[5][5], Res[maxn]; +char F[maxn]; + +int Dec() +{ + int sum = 0, p = 1; + int i, j; + for (i = 3; i >= 0; i--) + { + for (j = 3; j >= 0; j--) + { + sum += B[i][j] * p; + p *= 2; + } + } + return sum; +} +int Recur(int r, int c, int lim, int dir) +{ + int i, j, k, d, f = 0; + if (dir == 1) + for (i = c; i <= lim; i++) + B[r][i] = 1; + else + for (i = r; i <= lim; i++) + B[i][c] = 1; + d = Dec(); + if (F[d] == 1 || d == 65535) + { + if (dir == 1) + for (i = c; i <= lim; i++) + B[r][i] = 0; + else + for (i = r; i <= lim; i++) + B[i][c] = 0; + if (d == 65535) + return 1; + return Res[d]; + } + F[d] = 1; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (B[i][j] == 0) + { + k = Recur(i, j, j, 1); + if (!k) + { + f = 1; + break; + } + if (j + 1 < 4 && (j == 0 || j == 2) && !B[i][j + 1]) + { + k = Recur(i, j, j + 1, 1); + if (!k) + { + f = 1; + break; + } + } + if (j + 2 < 4 && (j == 0 || j == 1) && !B[i][j + 2] && !B[i][j + 1]) + { + k = Recur(i, j, j + 2, 1); + if (!k) + { + f = 1; + break; + } + } + if (i + 1 < 4 && (i == 0 || i == 2) && !B[i + 1][j]) + { + k = Recur(i, j, i + 1, 2); + if (!k) + { + f = 1; + break; + } + } + if (i + 2 < 4 && (i == 0 || i == 1) && !B[i + 2][j] && !B[i + 1][j]) + { + k = Recur(i, j, i + 2, 2); + if (!k) + { + f = 1; + break; + } + } + } + } + if (f) + break; + } + d = Dec(); + if (dir == 1) + for (i = c; i <= lim; i++) + B[r][i] = 0; + else + for (i = r; i <= lim; i++) + B[i][c] = 0; + Res[d] = 0; + if (f) + Res[d] = 1; + return Res[d]; +} +void Cal() +{ + int i, j, k, f = 0; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (B[i][j] == 0) + { + k = Recur(i, j, j, 1); + if (!k) + { + f = 1; + break; + } + if (j + 1 < 4 && (j == 0 || j == 2) && !B[i][j + 1]) + { + k = Recur(i, j, j + 1, 1); + if (!k) + { + f = 1; + break; + } + } + if (j + 2 < 4 && (j == 0 || j == 1) && !B[i][j + 2] && !B[i][j + 1]) + { + k = Recur(i, j, j + 2, 1); + if (!k) + { + f = 1; + break; + } + } + if (i + 1 < 4 && (i == 0 || i == 2) && !B[i + 1][j]) + { + k = Recur(i, j, i + 1, 2); + if (!k) + { + f = 1; + break; + } + } + if (i + 2 < 4 && (i == 0 || i == 1) && !B[i + 2][j] && !B[i + 1][j]) + { + k = Recur(i, j, i + 2, 2); + if (!k) + { + f = 1; + break; + } + } + } + } + if (f) + { + break; + } + } + printf(f ? "WINNING\n" : "LOSING\n"); +} +int main() +{ + int ks, i; + char ss[10]; + scanf("%d", &ks); + while (ks--) + { + for (i = 0; i < 4; i++) + { + scanf("%s", ss); + for (int j = 0; ss[j]; j++) + { + if (ss[j] == 'X') + B[i][j] = 1; + else + B[i][j] = 0; + } + } + Cal(); + } + return 0; +} diff --git a/10537.cpp b/10537.cpp new file mode 100644 index 0000000..b8d8634 --- /dev/null +++ b/10537.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define N 52 + +int target, from[52], map_[52][52], num[52], used[52]; +long long inf, dis[52]; + +int comp(int p, int q) +{ + if (p < 26 && q >= 26) + { + return 1; + } + if (p >= 26 && q < 26) + { + return -1; + } + return p - q; +} + +int letter(char ch) +{ + return (ch > 64 && ch < 91) || (ch > 96 && ch < 123); +} + +int decode(int now) +{ + if (now < 26) + { + return now + 97; + } + return now + 39; +} + +int trans(char ch) +{ + if (ch > 90) + { + return ch - 97; + } + return ch - 39; +} + +void backtracking(int now) +{ + if (now == target) + { + printf("%c", decode(now)); + } + else + { + printf("-%c", decode(now)); + } + if (from[now] != -1) + { + backtracking(from[now]); + } +} + +int main() +{ + int cas, m, i, t, temp; + long long min, j, p, q, c; + char c1, c2; + cas = 0; + inf = (1ll << 62); + while (scanf("%d", &m) == 1) + { + if (m == -1) + { + break; + } + memset(num, 0, sizeof(num)); + while (m--) + { + for (; scanf("%c", &c1) == 1 && !letter(c1);) + ; + for (; scanf("%c", &c2) == 1 && !letter(c2);) + ; + c1 = trans(c1); + c2 = trans(c2); + map_[c1][num[c1]++] = c2; + map_[c2][num[c2]++] = c1; + } + for (i = 0; i < N; i++) + { + dis[i] = inf; + used[i] = 0; + } + scanf("%d", &temp); + for (; scanf("%c", &c1) == 1 && !letter(c1);) + ; + for (; scanf("%c", &c2) == 1 && !letter(c2);) + ; + c1 = trans(c1); + c2 = trans(c2); + target = c1; + dis[c2] = temp; + used[c2] = 0; + from[c2] = -1; + for (;;) + { + for (i = 0, min = inf; i < N; i++) + { + if (!used[i] && dis[i] < min) + { + min = dis[i]; + temp = i; + } + } + if (min == inf) + { + break; + } + used[temp] = 1; + if (temp == target) + { + break; + } + for (i = 0; i < num[temp]; i++) + { + t = map_[temp][i]; + if (!used[t]) + { + if (temp >= 26) + { + for (j = 20 * dis[temp] / 19; j - (j + 19) / 20 < dis[temp]; j++) + ; + } + else + { + j = dis[temp] + 1; + } + if (j < dis[t]) + { + dis[t] = j; + from[t] = temp; + } + else if (j == dis[t]) + { + if (comp(from[t], temp) > 0) + { + from[t] = temp; + } + } + } + } + } + printf("Case %d:\n", ++cas); + printf("%lld\n", dis[target]); + backtracking(target); + printf("\n"); + } + return 0; +} diff --git a/10539.cpp b/10539.cpp new file mode 100644 index 0000000..9de0200 --- /dev/null +++ b/10539.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +bool prime[1000010]; +int realPrime[100000], pIndex, numOfPrime; +long long alPrimeNum[90000]; + +void sieve() +{ + int i, j; + memset(prime, 1, sizeof(prime)); + prime[0] = prime[1] = false; + for (i = 2; i <= 1000;) + { + for (j = i + i; j <= 1000000; j += i) + { + prime[j] = 0; + } + for (i++; !prime[i]; i++) + ; + } + numOfPrime = 0; + for (i = 2; i < 1000000; i++) + if (prime[i]) + { + realPrime[numOfPrime++] = i; + } +} + +int cmp(const void *a, const void *b) +{ + long long *aa = (long long *)a; + long long *bb = (long long *)b; + if (*aa > *bb) + { + return 1; + } + else if (*aa == *bb) + { + return 0; + } + else + { + return -1; + } +} + +void preCal() +{ + pIndex = 0; + int pri; + for (pri = 0; pri < numOfPrime; pri++) + { + long long value; + value = realPrime[pri]; + while ((value *= realPrime[pri]) < 1e12) + { + alPrimeNum[pIndex++] = value; + } + } + qsort(alPrimeNum, pIndex, sizeof(long long), cmp); +} + +// binary search +int bin_search(long long range) +{ + int low, mid, high; + low = 0; + high = 80069; + while (1) + { + mid = (int)(low + high) / 2; + if (range < alPrimeNum[mid] && range < alPrimeNum[mid - 1]) + { + high = mid - 1; + continue; + } + if (range > alPrimeNum[mid] && range > alPrimeNum[mid + 1]) + { + low = mid + 1; + continue; + } + if (range >= alPrimeNum[mid - 1] && range < alPrimeNum[mid]) + { + return mid; + } + if (range >= alPrimeNum[mid] && range < alPrimeNum[mid + 1]) + { + return mid + 1; + } + if (range == alPrimeNum[mid + 1]) + { + return mid + 2; + } + } +} + +int main() +{ + sieve(); + preCal(); + int numOfCase, k; + cin >> numOfCase; + for (k = 0; k < numOfCase; k++) + { + long long low, high; + cin >> low >> high; + cout << bin_search(high) - bin_search(low - 1) << endl; + } + return 0; +} diff --git a/1054.cpp b/1054.cpp new file mode 100644 index 0000000..c19fa2a --- /dev/null +++ b/1054.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +struct node +{ + char s[10]; + int p; + int v; +} a[50]; +vector ans; +int n; +bool check(int now) +{ + int i; + bool flag = false; + int sum = 0; + for (i = 0; i < n; ++i) + { + if (a[i].p == 0) + { + sum += a[i].v; + } + else if (a[i].p == 1) + { + if (flag) + { + if (sum % now != 0) + { + return false; + } + } + flag = true; + now += a[i].v; + sum = 0; + } + else if (a[i].p == 2) + { + if (flag) + { + if (sum % now != 0) + { + return false; + } + } + flag = true; + now -= a[i].v; + if (now <= 0) + { + return false; + } + sum = 0; + } + } + return true; +} +int main() +{ + int i; + while (scanf("%d", &n) != EOF) + { + if (n == 0) + { + break; + } + for (i = 0; i < n; ++i) + { + scanf("%s%d", a[i].s, &a[i].v); + if (strcmp(a[i].s, "PAY") == 0) + { + a[i].p = 0; + } + else if (strcmp(a[i].s, "IN") == 0) + { + a[i].p = 1; + } + else if (strcmp(a[i].s, "OUT") == 0) + { + a[i].p = 2; + } + else + { + a[i].p = -1; + } + } + ans.clear(); + for (i = 1; i <= 100000; ++i) + { + if (check(i)) + { + ans.push_back(i); + } + } + if (ans.size() == 0) + { + printf("IMPOSSIBLE\n"); + } + else if (ans[ans.size() - 1] == 100000) + { + printf("SIZE >= %d\n", ans[0]); + } + else + { + for (i = 0; i < ans.size(); ++i) + { + if (i) + { + printf(" "); + } + printf("%d", ans[i]); + } + puts(""); + } + } +} diff --git a/10540.cpp b/10540.cpp new file mode 100644 index 0000000..6d36a1e --- /dev/null +++ b/10540.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +double a[101], v[101], vs[101]; +const double pi = 2 * acos(0), eps = 1e-10; +int r, n; +int main() +{ + int cases = 1, l; + while (scanf("%d %d", &r, &n), r && n) + { + printf("Set %d:\n", cases++); + l = (n - 1) / 2 + 1; + printf("%d\n", l); + double theta = pi / n, d = pi / 2 - fmod(pi / 2 - eps, theta); + for (int i = 1; i <= l; i++, d -= theta) + { + double alpha = pi - 2 * d; + a[i] = alpha - sin(alpha); + a[i] *= n * r * r; + v[i] = a[i] - a[i - 1] - vs[i - 1]; + vs[i] = v[i] + vs[i - 1]; + printf("%.4lf %.4lf\n", a[i], v[i]); + } + } +} diff --git a/10541.cpp b/10541.cpp new file mode 100644 index 0000000..97d471e --- /dev/null +++ b/10541.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int N, K; +void solve(int m, int n) +{ + int array[20], p; + if (n < 0) + { + puts("0"); + } + else if (n == 0) + { + puts("1"); + } + else + { + memset(array, 0, sizeof(array)); + array[0] = 1; + for (int i = m; i > m - n; i--) + { + int carry = 0; + for (int j = 0; j < 20; j++) + { + int temp = array[j] * i + carry; + carry = temp / 10000000; + array[j] = temp % 10000000; + } + } + for (int i = n; i >= 2; i--) + { + int mod = 0; + for (int j = 19; j >= 0; j--) + { + int temp = mod * 10000000 + array[j]; + array[j] = temp / i; + mod = temp % i; + } + } + for (p = 19; array[p] == 0; p--) + ; + printf("%d", array[p]); + for (p--; p >= 0; p--) + { + printf("%07d", array[p]); + } + printf("\n"); + } +} +int main() +{ + int t, d; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &N, &K); + for (int i = 0; i < K; i++, scanf("%d", &d), N -= d) + ; + N -= (K - 1); + solve(K + N, N); + } + return 0; +} diff --git a/10542.cpp b/10542.cpp new file mode 100644 index 0000000..74d2afa --- /dev/null +++ b/10542.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +long long GCD(long long a, long long b) +{ + return (b == 0) ? a : GCD(b, a % b); +} +long long a[10], b[10], ans; +int n, depth; +void DFS(int d, int start, long s) +{ + if (d == depth) + { + if (d % 2) + { + ans += s; + } + else + { + ans -= s; + } + } + else + { + for (int i = start; i < n; i++) + { + DFS(d + 1, i + 1, (d == 0) ? a[i] : GCD(s, a[i])); + } + } +} +int main() +{ + int t; + scanf("%d", &t); + for (int caseno = 1; caseno <= t; caseno++) + { + scanf("%d", &n); + ans = 0; + for (int i = 0; i < n; i++) + { + scanf("%lld", a + i); + } + for (int i = 0; i < n; i++) + { + scanf("%lld", b + i); + } + for (int i = 0; i < n; i++) + { + a[i] -= b[i]; + if (a[i] == 0) + { + ans = -1; + } + if (a[i] < 0) + { + a[i] = -a[i]; + } + } + printf("Case %d: ", caseno); + if (ans == -1) + { + puts("0"); + continue; + } + for (depth = 1; depth <= n; depth++) + { + DFS(0, 0, 0); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/10543.cpp b/10543.cpp new file mode 100644 index 0000000..54834b0 --- /dev/null +++ b/10543.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +/**** +10543 +****/ +#define MAXN 52 +int MAP[MAXN][MAXN]; +int Dummy[MAXN][MAXN]; +int Temp[MAXN][MAXN]; +int Node, Vertex, Sp; +void Copy() +{ + int i, j; + for (i = 0; i < Node; i++) + for (j = 0; j < Node; j++) + { + Dummy[i][j] = Temp[i][j]; + } +} +void MULT() +{ + int i, j, k; + int sum; + for (i = 0; i < Node; i++) + { + for (j = 0; j < Node; j++) + { + sum = 0; + for (k = 0; k < Node - 1; k++) + { + sum += MAP[i][k] * Dummy[k][j]; + } + Temp[i][j] = sum; + } + } + Copy(); +} +void Cal() +{ + int i, limit; + limit = Sp - 2; + for (i = 1; i <= 20; i++) + { + MULT(); + if (i >= limit && Dummy[0][Node - 1] != 0) + { + printf("%d\n", i + 2); + return; + } + } + printf("LOSER\n"); +} +int main() +{ + int i, u, v, j; + while (scanf("%d%d%d", &Node, &Vertex, &Sp) == 3) + { + if (!Node && !Vertex && !Sp) + { + break; + } + for (i = 0; i < Vertex; i++) + { + scanf("%d%d", &u, &v); + MAP[u][v] = 1; + Dummy[u][v] = 1; + } + if (Sp == 2 && Dummy[0][Node - 1] == 1) + { + printf("2\n"); + continue; + } + Cal(); + for (i = 0; i < MAXN; i++) + for (j = 0; j < MAXN; j++) + { + MAP[i][j] = Dummy[i][j] = 0; + } + } + return 0; +} diff --git a/10544.cpp b/10544.cpp new file mode 100644 index 0000000..200ccb1 --- /dev/null +++ b/10544.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define MAX 30 + +typedef long long sss; +char Link[MAX][MAX]; +sss Path[MAX]; +char F[MAX], str[MAX]; +sss N, E; + +sss Mark(sss n) +{ + sss i, j = 1; + if (Path[n]) + return Path[n]; + for (i = 0; i < N; i++) + { + if (Link[n][i]) + { + j = 0; + Path[n] += Mark(i); + } + } + if (j) + Path[n] = 1; + return Path[n]; +} +void Reset() +{ + sss i, j; + for (i = 0; i <= N; i++) + { + Path[i] = 0; + for (j = 0; j <= N; j++) + Link[i][j] = 0; + } +} +sss Find(sss sum, sss c, sss level, sss len) +{ + sss i, j = 1, k; + if (len == level) + { + printf("%lld\n", sum + 1); + return 1; + } + k = str[level + 1] - 'A'; + for (i = 0; i < N; i++) + { + if (!Link[c][i]) + continue; + if (i == k) + if (Find(sum, i, level + 1, len)) + return 1; + sum += Path[i]; + } + return 1; +} +void Cal() +{ + int q, len; + scanf("%d", &q); + while (q--) + { + scanf("%s", str); + len = strlen(str); + printf("%s: ", str); + Find(0, str[0] - 'A', 0, len - 1); + } +} + +int main() +{ + int i, u, v, kases; + char ss[4]; + scanf("%d", &kases); + while (kases--) + { + scanf("%lld%lld", &N, &E); + for (i = 0; i < E; i++) + { + scanf("%s", ss); + u = ss[0] - 'A'; + v = ss[1] - 'A'; + Link[u][v] = 1; + } + Mark(0); + Cal(); + Reset(); + } + return 0; +} diff --git a/10545.cpp b/10545.cpp new file mode 100644 index 0000000..16550ca --- /dev/null +++ b/10545.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +const double pi = acos(-1); +double p, a, b, c, d, s; +int main() +{ + int t; + scanf("%d", &t); + for (int cases = 1; cases <= t; cases++) + { + scanf("%lf %lf %lf", &p, &a, &b); + s = p / 2; + c = s - a; + d = s - b; + double temp = (a * c + b * d) * (a * c + b * d) - (a - b) * (a - b) * (a + b - s) * (a + b - s); + printf("Case %d: ", cases); + if (c <= 0 || d <= 0 || temp < 0) + { + puts("Eta Shombhob Na."); + } + else + { + printf("%.6lf\n", sqrt(temp) / 2 / s); + } + } +} diff --git a/10546.cpp b/10546.cpp new file mode 100644 index 0000000..6ea0691 --- /dev/null +++ b/10546.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +char name[100][25]; +int g[202][202], f[202][202], v[100], d[100], w[202], p[202], n, max_, source, dest; + +bool findpath() +{ + for (int i = 0; i <= dest; i++) + { + w[i] = 99999999; + } + w[source] = 0; + p[source] = -1; + int queue[300], front = -1, rear = 0; + queue[0] = source; + while (front < rear) + { + int u = queue[++front]; + for (int v = 0; v <= dest; v++) + { + if (g[u][v] - f[u][v] > 0 && w[v] > w[u] + 1) + { + w[v] = w[u] + 1; + p[v] = u; + queue[++rear] = v; + } + } + } + return w[dest] != 99999999; +} +void update() +{ + int u, v = dest; + do + { + u = p[v]; + f[u][v]++; + f[v][u] = -f[u][v]; + v = u; + } while (p[v] != -1); +} +int main() +{ + int caseno = 1; + while (scanf("%d", &n), n) + { + max_ = 0; + for (int i = 0; i < n; i++) + { + scanf("%s %d", name[i], &v[i]); + } + for (int i = 0; i < n; i++) + { + d[i] = 1; + for (int j = i - 1; j >= 0; j--) + if (v[j] < v[i] && d[j] + 1 > d[i]) + { + d[i] = d[j] + 1; + } + if (d[i] > max_) + { + max_ = d[i]; + } + } + source = 0; + dest = 2 * n + 1; + memset(g, 0, sizeof(g)); + memset(f, 0, sizeof(f)); + for (int i = 1; i <= n; i++) + { + g[i][i + n] = 1; + } + for (int i = 0; i < n; i++) + { + if (d[i] == max_) + { + g[i + 1 + n][dest] = 1; + } + if (d[i] == 1) + { + g[source][i + 1] = 1; + } + } + for (int s = 1; s < max_; s++) + for (int i = 0; i < n; i++) + if (d[i] == s) + { + for (int j = i + 1; j < n; j++) + if (d[j] == s + 1 && v[i] < v[j]) + { + g[i + 1 + n][j + 1] = 1; + } + } + int path = 0; + while (findpath()) + { + update(); + path++; + } + printf("Case #%d: %d\n", caseno++, max_ * path); + } +} diff --git a/10547.cpp b/10547.cpp new file mode 100644 index 0000000..9c7f2fe --- /dev/null +++ b/10547.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int t, p; + unsigned long long int n, b, c, m; + for (t = 1; scanf("%llu%llu%d", &c, &n, &p) == 3; t++) + { + if (n == 0 && c == 0 && p == 0) + { + break; + } + for (m = 1LL; p; p--) + { + m *= 10LL; + } + for (b = 1LL, c %= m; n; n >>= 1) + { + if (n & 1LL) + { + b = (b * c) % m; + } + c = (c * c) % m; + } + printf("Case #%d: %llu\n", t, b); + } + return 0; +} diff --git a/10548.cpp b/10548.cpp new file mode 100644 index 0000000..0b7f5b1 --- /dev/null +++ b/10548.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +long long a, b, c, x, y; +long long E_GCD(long long aa, long long bb, long long &xx, long long &yy) +{ + if (bb == 0) + { + xx = 1; + yy = 0; + return aa; + } + else + { + long long tx, ty; + long long d = E_GCD(bb, aa % bb, tx, ty); + xx = ty; + yy = tx - aa / bb * ty; + return d; + } +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%lld %lld %lld", &a, &b, &c); + long long d = E_GCD(a, b, x, y); + if (c % d) + { + puts("Impossible"); + } + else if (a * b < 0) + { + puts("Infinitely many solutions"); + } + else + { + c /= d; + x *= c; + y *= c; + long long lower = (long long int)ceil((double)-x * d / b); + long long upper = (long long int)floor((double)y * d / a); + if (upper - lower + 1 > 0) + { + printf("%lld\n", upper - lower + 1); + } + else + { + puts("Impossible"); + } + } + } + return 0; +} diff --git a/10549.cpp b/10549.cpp new file mode 100644 index 0000000..820c354 --- /dev/null +++ b/10549.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +/* +10549 +Russina Doll +*/ + +#define maxn 202 +struct ss +{ + int d, h, w; +} D[maxn]; +ss A[maxn], B[maxn]; +int N; +int com(const void *a, const void *b) +{ + ss *x; + ss *y; + x = (ss *)a; + y = (ss *)b; + if (x->h != y->h) + { + return y->h - x->h; + } + if (x->d != y->d) + { + return y->d - x->d; + } + return x->w - y->w; +} +int Hold(int n, int m) +{ + if (A[n].h - A[n].w * 2 < D[m].h) + { + return 0; + } + if (A[n].d - A[n].w * 2 < D[m].d) + { + return 0; + } + return 1; +} +int HoldB(int n, int m) +{ + if (B[n].h - B[n].w * 2 < D[m].h) + { + return 0; + } + if (B[n].d - B[n].w * 2 < D[m].d) + { + return 0; + } + return 1; +} +int Fit(int n, int m) +{ + int i, x, y, z; + x = B[n].h; + y = B[n].d; + z = B[n].w; + for (i = m + 1; i < 2 * N; i++) + { + if (x - z * 2 < D[i].h) + { + return 0; + } + if (y - z * 2 < D[i].d) + { + return 0; + } + x = D[i].h; + y = D[i].d; + z = D[i].w; + } + return 1; +} +int Recur(int ind1, int ind2, int level) +{ + int i, dif, res, d = ind2; + dif = N - (level + ind2); + res = N - ind1; + if (dif > res) + { + return 0; + } + A[level] = D[ind1]; + if (level == N) + { + if (ind2 == N) + { + return 1; + } + else if (Fit(ind2, ind1)) + { + for (i = ind1 + 1; i < 2 * N; i++) + { + B[++d] = D[i]; + } + return 1; + } + return 0; + } + for (i = ind1 + 1; i < N * 2; i++) + { + if (Hold(level, i)) + { + if (Recur(i, ind2, level + 1)) + { + return 1; + } + else if (HoldB(ind2, i)) + { + ind2++; + B[ind2] = D[i]; + } + else + { + return 0; + } + } + else if (HoldB(ind2, i)) + { + ind2++; + if (ind2 > N) + { + return 0; + } + B[ind2] = D[i]; + } + else + { + return 0; + } + } + return 0; +} +void Print() +{ + int i; + for (i = 1; i <= N; i++) + { + cout << A[i].h << " " << A[i].d << " " << A[i].w << endl; + } + cout << "-\n"; + for (i = 1; i <= N; i++) + { + cout << B[i].h << " " << B[i].d << " " << B[i].w << endl; + } + cout << "\n"; +} +void Cal() +{ + B[0].h = B[0].d = 1000000; + B[0].w = 0; + Recur(0, 0, 1); + Print(); +} +int main() +{ + int i; + while (cin >> N) + { + if (!N) + { + break; + } + for (i = 0; i < 2 * N; i++) + { + cin >> D[i].h >> D[i].d >> D[i].w; + } + qsort(D, N * 2, sizeof(ss), com); + Cal(); + } + return 0; +} diff --git a/10550.cpp b/10550.cpp index 5b88098..97df759 100644 --- a/10550.cpp +++ b/10550.cpp @@ -1,56 +1,49 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int A, B, C, D, tot=0; - cin>>A>>B>>C>>D; - if(A==0 & B==0 & C==0 & D==0) - break; - tot += 80; - if(B >= A) - tot += (B-A); - else - tot += (40 - A + B); - tot += 40; - if(C <= B) - tot += (B-C); - else - tot += (40 - C + B); - if(D >= C) - tot += (D-C); - else - tot += (40 - C + D); - cout<< 3240 - tot*9 < + +using namespace std; + +/**** +10550 +****/ +#define MAXN 1080 +int Close(int a, int b) +{ + int c = 0; + while (a != b) + { + c++; + a--; + if (a == -1) + { + a = 39; + } + } + return c * 9; +} +int Counter(int a, int b) +{ + int c = 0; + while (a != b) + { + c++; + a++; + if (a == 40) + { + a = 0; + } + } + return c * 9; +} +int main() +{ + int a, b, c, d; + while (scanf("%d%d%d%d", &a, &b, &c, &d) == 4) + { + if (a == 0 && b == 0 && c == 0 && d == 0) + { + break; + } + printf("%d\n", Close(a, b) + Counter(b, c) + Close(c, d) + MAXN); + } + return 0; +} diff --git a/10551.cpp b/10551.cpp new file mode 100644 index 0000000..188eb4d --- /dev/null +++ b/10551.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + int b, i, array[500], num; + char buf1[1010], buf2[100]; + long long R, s; + while (cin >> b) + { + if (b == 0) + { + break; + } + cin >> buf1 >> buf2; + R = s = 0; + for (i = 0; buf2[i]; i++) + { + R *= b; + R += buf2[i] - '0'; + } + for (i = 0; buf1[i]; i++) + { + s *= b; + s += buf1[i] - '0'; + s %= R; + } + if (s == 0) + { + cout << 0 << endl; + } + else + { + num = 0; + while (s) + { + array[num++] = s % b; + s /= b; + } + for (i = num - 1; i >= 0; i--) + { + cout << array[i]; + } + cout << endl; + } + } + return 0; +} diff --git a/10552.cpp b/10552.cpp new file mode 100644 index 0000000..7c668f5 --- /dev/null +++ b/10552.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +typedef char str[101]; + +struct pp +{ + str name, p1, p2, b, d; +} p[1000]; +int np, first; + +str buf, cmd, name, p1, p2, b, d; + +void fix(char *s) +{ + while (s[strlen(s) - 1] == ' ') + { + s[strlen(s) - 1] = 0; + } +} +void ancestors(str n, int ind) +{ + int i, j; + for (i = 0; i < np && strcmp(n, p[i].name); i++) + { + } + if (ind) + { + for (j = 0; j < ind; j++) + { + printf(" "); + } + printf("%s", n); + if (i < np) + { + printf(" %s -", p[i].b); + if (*p[i].d) + { + printf(" %s", p[i].d); + } + } + printf("\n"); + } + if (i < np) + { + ancestors(p[i].p1, ind + 2); + ancestors(p[i].p2, ind + 2); + } +} +void descendants(str n, int ind) +{ + int i, j; + for (i = 0; i < np; i++) + { + if (!strcmp(n, p[i].p1) || !strcmp(n, p[i].p2)) + { + for (j = 0; j < ind; j++) + { + printf(" "); + } + printf("%s %s -", p[i].name, p[i].b); + if (*p[i].d) + { + printf(" %s", p[i].d); + } + printf("\n"); + descendants(p[i].name, ind + 2); + } + } +} +int cmp(const void *a, const void *b) +{ + return strcmp((char *)a, (char *)b); +} +int main() +{ + int i; + while (gets(buf)) + { + *cmd = *name = *d = *p1 = *p2 = 0; + sscanf(buf, "%s %[^:] : %[^:] : %[^:] : %[^:]", cmd, name, d, p1, p2); + fix(name); + fix(p1); + fix(p2); + fix(d); + if (!strcmp(cmd, "BIRTH")) + { + strcpy(p[np].name, name); + strcpy(p[np].b, d); + if (strcmp(p1, p2) < 0) + { + strcpy(p[np].p1, p1); + strcpy(p[np].p2, p2); + } + else + { + strcpy(p[np].p1, p2); + strcpy(p[np].p2, p1); + } + np++; + qsort(p, np, sizeof(struct pp), cmp); + } + else if (!strcmp(cmd, "DEATH")) + { + for (i = 0; i < np && strcmp(name, p[i].name); i++) + ; + strcpy(p[i].d, d); + } + else if (!strcmp(cmd, "ANCESTORS")) + { + if (first++) + { + printf("\n"); + } + printf("ANCESTORS of %s\n", name); + ancestors(name, 0); + } + else if (!strcmp(cmd, "DESCENDANTS")) + { + if (first++) + { + printf("\n"); + } + printf("DESCENDANTS of %s\n", name); + descendants(name, 2); + } + else if (!strcmp(cmd, "QUIT")) + { + exit(0); + } + else + { + printf("invalid command %s\n", cmd); + } + } + printf("missing QUIT command \n"); +} diff --git a/10553.cpp b/10553.cpp new file mode 100644 index 0000000..b0e9df7 --- /dev/null +++ b/10553.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +} X; + +Point operator+(const Point &a, const Point &b) +{ + return Point(a.x + b.x, a.y + b.y); +} +Point operator-(const Point &a, const Point &b) +{ + return Point(a.x - b.x, a.y - b.y); +} + +double dot(Point a, Point b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Point a, Point b) +{ + return a.x * b.y - a.y * b.x; +} +double len(Point a) +{ + return sqrt(dot(a, a)); +} + +Point rad(double r, double a) +{ + return Point(r * cos(a), r * sin(a)); +} + +int main() +{ + string dirs[] = + { + "N", "NbE", "NNE", "NEbN", "NE", "NEbE", "ENE", "EbN", "E", "EbS", "ESE", + "SEbE", "SE", "SEbS", "SSE", "SbE", "S", "SbW", "SSW", "SWbS", "SW", + "SWbW", "WSW", "WbS", "W", "WbN", "WNW", "NWbW", "NW", "NWbN", "NNW", + "NbW"}; + double pi = 2 * acos(0.), wr[1024], wa[1024], d; + char s[128]; + int n; + while (scanf("%d", &n) == 1 && n > 0) + { + for (int i = 0; i < n; i++) + { + scanf(" %s %lf", s, &wr[i]); + wa[i] = (find(dirs, dirs + 32, s) - dirs) * pi / 16.; + } + scanf("%lf", &d); + d *= pi / 180.; + X = Point(0, 0); + for (int i = 0; i < n; i++) + { + X = X + rad(wr[i], wa[i] + d); + } + d = len(X); + Point A(0, 0); + for (int i = 0; i < n; i++) + { + Point B = A + rad(wr[i], wa[i]); + d = MIN(d, MIN(len(A - X), len(B - X))); + if (dot(X - A, B - A) > 0 && dot(X - B, A - B) > 0 && len(A - B) > 1e-8) + { + d = MIN(d, fabs(cross(A - X, B - X)) / len(A - B)); + } + A = B; + } + printf("%.2f\n", d); + } + return 0; +} diff --git a/10554.cpp b/10554.cpp new file mode 100644 index 0000000..193bc59 --- /dev/null +++ b/10554.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef vector> pic; + +pic getdata(string &inp) +{ + pic in; + stringstream sin(inp); + pair tmp; + while (sin >> tmp.first >> tmp.second) + { + in.push_back(tmp); + } + return in; +} + +const double caldata[] = {9, 4, 4, 4, 7}; + +double solve() +{ + string in; + double cal = 0, fat = 0; + int n = 0; + while (getline(cin, in) && in != "-") + { + n++; + pic now = getdata(in); + double total = 1; + double left = 0; + rep(i, 5) + { + if (now[i].second == 'C') + { + left += now[i].first; + } + else if (now[i].second == 'g') + { + left += now[i].first * caldata[i]; + } + else + { + total -= 0.01 * now[i].first; + } + } + total = left / total; + if (now[0].second == 'C') + { + fat += now[0].first; + } + else if (now[0].second == 'g') + { + fat += now[0].first * caldata[0]; + } + else + { + fat += 0.01 * now[0].first * total; + } + cal += total; + //cout << cal <<" " << total << endl; + } + if (n == 0) + { + return -1; + } + return fat / cal; +} + +int main() +{ + while (true) + { + double ans = solve(); + if (ans < 0) + { + break; + } + else + { + printf("%.0lf%%\n", ans * 100); + } + } + return 0; +} diff --git a/10555.cpp b/10555.cpp new file mode 100644 index 0000000..34a2f60 --- /dev/null +++ b/10555.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int g, i, j, k, m, n, term, rep, q, num, den; +int bestn, bestd; +char d[10], a[10], b[10], c[10]; + +int gcd(int a, int b) +{ + if (b == 0) + { + return a; + } + return gcd(b, a % b); +} + +int main() +{ + while (1 == scanf(" 0.%[0-9]...", d)) + { + bestn = bestd = 1000000000; + for (i = 0; d[i]; i++) + { + strncpy(a, d, i); + a[i] = 0; + strcpy(b, d + i); + num = atoi(a) * ((int)pow(10, strlen(b)) - 1) + atoi(b); + den = (int)pow(10, i) * ((int)pow(10, strlen(b)) - 1); + g = gcd(num, den); + num /= g; + den /= g; + if (den < bestd) + { + bestn = num; + bestd = den; + } + } + printf("%d/%d\n", bestn, bestd); + } +} diff --git a/10556.cpp b/10556.cpp new file mode 100644 index 0000000..e5f1b7b --- /dev/null +++ b/10556.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define sq(x) ((x) * (x)) + +int x[10], y[10], X[10], Y[10]; +int i, j, k, f, a, A, d, D, s, S; + +int main() +{ + while (1 == scanf("%d", &f), f) + { + for (i = 0; i < f; i++) + { + scanf("%d%d", &x[i], &y[i]); + } + for (i = 0; i < f; i++) + { + scanf("%d%d", &X[i], &Y[i]); + } + a = sq(x[0] - x[1]) + sq(y[0] - y[1]); + A = sq(X[0] - X[1]) + sq(Y[0] - Y[1]); + for (i = 0; i < f; i++) + for (j = 0; j < f; j++) + { + d = sq(x[i] - x[j]) + sq(y[i] - y[j]); + D = sq(X[i] - X[j]) + sq(Y[i] - Y[j]); + if ((long long)a * D != (long long)A * d) + { + goto foo; + } + } + for (i = 2; i < f; i++) + { + s = (x[1] - x[0]) * (y[i] - y[0]) - (y[1] - y[0]) * (x[i] - x[0]); + S = (X[1] - X[0]) * (Y[i] - Y[0]) - (Y[1] - Y[0]) * (X[i] - X[0]); + if (s < 0 && S > 0 || s > 0 && S < 0) + { + goto foo; + } + } + printf("similar\n"); + continue; + foo: + printf("dissimilar\n"); + } +} diff --git a/10557.cpp b/10557.cpp new file mode 100644 index 0000000..e3fc024 --- /dev/null +++ b/10557.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +/* +10557 +XYZZY +*/ +#define MAXN 10005 +#define INF 1000000000 +struct Vertex +{ + int Fixed; + int Energy; +}; +struct ss +{ + int u, v; +} E[MAXN]; +Vertex V[102]; +int Ind, N; +void ReadCase() +{ + int i, v, eng, n; + Ind = 0; + for (i = 1; i <= N; i++) + { + scanf("%d", &eng); + V[i].Fixed = -eng; + scanf("%d", &n); + while (n--) + { + scanf("%d", &v); + E[Ind].u = i; + E[Ind++].v = v; + } + } + V[1].Fixed = 0; + V[N].Fixed = 0; +} +void Bellman_Ford() +{ + int i, j, u, v, d; + for (i = 2; i <= N; i++) + { + V[i].Energy = INF; + } + V[1].Energy = -100; + for (i = 1; i <= 19900; i++) + { + for (j = 0; j < Ind; j++) + { + u = E[j].u; + v = E[j].v; + if (V[u].Energy >= 0) + { + continue; + } + d = V[u].Energy + V[v].Fixed; + if (d < V[v].Energy) + { + V[v].Energy = d; + } + if (V[N].Energy < INF) + { + return; + } + } + } +} +void Cal() +{ + Bellman_Ford(); + if (V[N].Energy < INF) + { + printf("winnable\n"); + } + else + { + printf("hopeless\n"); + } +} +int main() +{ + while (scanf("%d", &N) == 1) + { + if (N < 0) + { + break; + } + ReadCase(); + Cal(); + } + return 0; +} diff --git a/10558.cpp b/10558.cpp new file mode 100644 index 0000000..7b48905 --- /dev/null +++ b/10558.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +#define MAXD 110 +int N, S, A, g[MAXD][MAXD], f[MAXD][MAXD], street[MAXD]; +int t[MAXD][MAXD], st[MAXD][MAXD][MAXD], s[MAXD][MAXD], p[MAXD][MAXD]; +int init() +{ + int i, j, k, flag, x, y; + scanf("%d", &N); + if (N == -1) + { + return 0; + } + memset(g, 0, sizeof(g)); + for (i = 0; i < N; i++) + { + scanf("%d%d", &y, &x); + g[x][y] = 1; + } + scanf("%d", &S); + for (i = 0; i < S; i++) + { + scanf("%d", &street[i]); + } + scanf("%d", &A); + memset(t, 0, sizeof(t)); + for (i = 2; i <= 100; i++) + for (j = 1; j < S; j++) + { + flag = 0; + for (k = street[j - 1]; k < street[j]; k++) + if (g[i - 1][k]) + { + flag = 1; + } + if (flag) + { + t[i][j] = 1; + } + } + memset(s, 0, sizeof(s)); + memset(st, 0, sizeof(st)); + for (i = 1; i < 100; i++) + for (j = i + 1; j <= 100; j++) + { + for (k = 1; k < S; k++) + { + st[i][j][k] |= st[i][j - 1][k]; + } + for (k = 1; k < S; k++) + { + st[i][j][k] |= t[j][k]; + } + for (k = 1; k < S; k++) + if (st[i][j][k]) + { + s[i][j]++; + } + } + return 1; +} +void printpath(int i, int k) +{ + if (k != 1) + { + printpath(i - 1, p[i][k]); + } + printf(" %d", k); +} +void solve() +{ + int i, j, k, max; + memset(f, -1, sizeof(f)); + for (i = 2; i <= 100; i++) + { + f[1][i] = s[1][i]; + p[1][i] = 1; + } + for (i = 2; i < A; i++) + for (j = i + 1; j <= 100; j++) + { + for (k = i; k < j; k++) + if (f[i - 1][k] + s[k][j] > f[i][j]) + { + f[i][j] = f[i - 1][k] + s[k][j]; + p[i][j] = k; + } + } + printf("%d", A); + printpath(A - 1, 100); + printf("\n"); +} +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/10559.cpp b/10559.cpp new file mode 100644 index 0000000..c6404a1 --- /dev/null +++ b/10559.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int F[201][201][201]; +int len[201], color[201], ptr; +int n, seq[201], t; +inline int sqr(int k) +{ + return k * k; +} +int f(int i, int j, int k) +{ + if (i > j) + { + return 0; + } + if (F[i][j][k]) + { + return F[i][j][k]; + } + F[i][j][k] = f(i, j - 1, 0) + sqr(len[j] + k); + for (int p = i; p < j; p++) + if (color[p] == color[j] && f(i, p, len[j] + k) + f(p + 1, j - 1, 0) > F[i][j][k]) + { + F[i][j][k] = F[i][p][len[j] + k] + F[p + 1][j - 1][0]; + } + return F[i][j][k]; +} +int main() +{ + int i, j; + scanf("%d", &t); + for (int c = 1; c <= t; c++) + { + scanf("%d", &n); + ptr = 0; + for (i = 1; i <= n; i++) + { + scanf("%d", seq + i); + } + for (i = 1; i <= n; i = j) + { + color[++ptr] = seq[i]; + for (j = i + 1; j <= n && seq[i] == seq[j]; j++) + ; + len[ptr] = j - i; + } + memset(F, 0, sizeof(F)); + /*for(int i=1;i<=ptr;i++) F[i][i-1][0]=0; + for(int j=0;j<=ptr;j++) + for(int i=1;i<=j;i++) + for(int k=0;k<=n;k++) + { + F[i][j][k]=F[i][j-1][0]+sqr(len[j]+k); + for(int p=i;pF[i][j][k]) + F[i][j][k]=F[i][p][len[j]+k]+F[p+1][j-1][0]; + }*/ + printf("Case %d: %d\n", c, f(1, ptr, 0)); + } + return 0; +} diff --git a/1056.cpp b/1056.cpp new file mode 100644 index 0000000..f025c70 --- /dev/null +++ b/1056.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int main() +{ + int p, r; + int case_num = 0; + string a, b; + int connections[51][51]; + int DISCONNECTED = 999999999; + while (cin >> p >> r) + { + if (p == 0 && r == 0) + { + break; + } + case_num++; + map people; + int counter = 0; + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + if (i == j) + { + connections[i][j] = 0; + continue; + } + connections[i][j] = DISCONNECTED; + } + } + while (r--) + { + cin >> a >> b; + if (people.find(a) == people.end()) + { + people[a] = counter; + counter++; + } + if (people.find(b) == people.end()) + { + people[b] = counter; + counter++; + } + connections[people[a]][people[b]] = 1; + connections[people[b]][people[a]] = 1; + } + for (int k = 0; k < p; k++) + { + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + connections[i][j] = min(connections[i][j], connections[i][k] + connections[k][j]); + } + } + } + int result = 0; + for (int i = 0; i < p; i++) + { + for (int j = 0; j < p; j++) + { + if (connections[i][j] == DISCONNECTED) + { + result = -1; + i = p; + break; + } + result = max(result, connections[i][j]); + } + } + cout << "Network " << case_num << ": "; + if (result == -1) + { + cout << "DISCONNECTED" << endl; + } + else + { + cout << result << endl; + } + cout << endl; + } + return 0; +} diff --git a/10560.cpp b/10560.cpp new file mode 100644 index 0000000..e91bd22 --- /dev/null +++ b/10560.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +typedef long long i64; + +#define nmax 41 + +i64 n, k, c; +i64 a[nmax], b[nmax]; +int f; + +void out(int i, int zf) +{ + if (f) + { + f = 0; + } + else + { + if (zf == 1) + { + cout << "+"; + } + else + { + cout << "-"; + } + } + cout << b[i]; +} +void done(i64 c) +{ + int i; + while (c) + { + if (c > 0) + { + for (i = 0; i < nmax; ++i) + { + if ((a[i] > 0 && a[i] >= c))//|| (a[i]>0 && a[i]<=c)) { + { + c -= b[i]; + out(i, 1); + break; + } + } + } + else + { + c = -c; + for (i = 0; i < nmax; ++i) + { + if ((a[i] > 0 && a[i] >= c))//|| (a[i]>0 && a[i]<=c)) { + { + c -= b[i]; + out(i, -1); + break; + } + } + c = -c; + } + } + cout << endl; +} +/* +12157665459056928801 3^40 +9223372036854775808 2^63 +36472996377170786403 3^41 +*/ +int main() +{ + int i; + a[0] = b[0] = 1; + for (i = 1; i < nmax; ++i) + b[i] = b[i - 1] * 3, + a[i] = a[i - 1] + b[i]; + while (cin >> n >> k && n && k) + { + for (i = 0; i < nmax; ++i) + { + if (a[i] >= n) + { + cout << i + 1; + for (int j = 0; j <= i; ++j) + { + cout << " " << b[j]; + } + cout << endl; + break; + } + } + for (i = 0; i < k; ++i) + { + cin >> c; + f = 1; + done(c); + } + } + return 0; +} diff --git a/10561.cpp b/10561.cpp new file mode 100644 index 0000000..185f6c8 --- /dev/null +++ b/10561.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +char str[300]; +int g[201], n; +void init() +{ + g[0] = 0; + bool f[201]; + for (int i = 1; i <= 200; i++) + { + memset(f, false, sizeof(f)); + for (int j = 1; j <= i; j++) + { + int g1 = (j - 3 <= 0) ? 0 : g[j - 3]; + int g2 = (i - j - 2 <= 0) ? 0 : g[i - j - 2]; + f[g1 ^ g2] = true; + } + int p; + for (p = 0; f[p]; p++) + ; + g[i] = p; + } +} +bool check() +{ + int loc[301], ptr = 0, ga = 0; + for (int i = 0; i < n - 2; i++) + if (str[i] == 'X' && str[i + 1] == 'X' && str[i + 2] == 'X') + { + return false; + } + for (int i = 0; i < n; i++) + { + if (str[i] == 'X') + { + loc[ptr++] = i; + if (i + 1 < n && str[i + 1] == 'X') + { + return true; + } + if (i + 2 < n && str[i + 2] == 'X') + { + return true; + } + } + } + if (ptr == 0) + { + return g[n] != 0; + } + else + { + if (loc[0] - 2 >= 0) + { + ga ^= g[loc[0] - 2]; + } + for (int i = 1; i < ptr; i++) + if (loc[i] - loc[i - 1] - 5 >= 0) + { + ga ^= g[loc[i] - loc[i - 1] - 5]; + } + if (n - loc[ptr - 1] - 3 >= 0) + { + ga ^= g[n - loc[ptr - 1] - 3]; + } + return ga != 0; + } +} +int main() +{ + init(); + int times; + int f; + scanf("%d", ×); + while (times--) + { + f = 0; + scanf("%s", str); + n = strlen(str); + if (!check()) + { + puts("LOSING\n"); + } + else + { + puts("WINNING"); + for (int i = 0; i < n; i++) + if (str[i] == '.') + { + str[i] = 'X'; + if (!check()) + { + if (f++) + { + putchar(' '); + } + printf("%d", i + 1); + } + str[i] = '.'; + } + printf("\n"); + } + } +} diff --git a/10562.cpp b/10562.cpp new file mode 100644 index 0000000..ecc4246 --- /dev/null +++ b/10562.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int depth, N; +char s[210][210], str[210]; +bool valid(char c) +{ + return !(c == ' ' || c == '\0' || c == '\n' || c == '-' || c == '|' || c == '#'); +} +void print(int r, int c, char ch) +{ + printf("%c(", ch); + int left, right; + if (r + 3 <= depth && s[r + 1][c] == '|') + { + for (left = c; left >= 0 && s[r + 2][left] == '-'; left--) + ; + for (right = c; s[r + 2][right] == '-'; right++) + ; + for (int k = left + 1; k < right; k++) + if (valid(s[r + 3][k])) + { + print(r + 3, k, s[r + 3][k]); + } + } + printf(")"); +} +int main() +{ + fgets(str, 210, stdin); + sscanf(str, "%d\n", &N); + while (N--) + { + memset(s, 0, 210 * 210); + for (depth = 0;; ++depth) + { + fgets(s[depth], 210, stdin); + int k; + for (k = strlen(s[depth]); k >= 0; --k) + if (s[depth][k] != '\0' && s[depth][k] != ' ' && s[depth][k] != '\n') + { + break; + } + s[depth][k + 1] = '\0'; + if (strcmp(s[depth], "#") == 0) + { + break; + } + } + printf("("); + for (int i = 0; i < (int)strlen(s[0]); i++) + { + if (valid(s[0][i])) + { + print(0, i, s[0][i]); + break; + } + } + printf(")\n"); + } + return 0; +} diff --git a/10563.cpp b/10563.cpp new file mode 100644 index 0000000..1ea7e02 --- /dev/null +++ b/10563.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +char board[110][110]; + +char find(int n, int i, int j) +{ + char color = 'A'; + while (1) + { + if ((!i || board[i - 1][j] != color) && + (!j || board[i][j - 1] != color) && + (j == n - 1 || board[i][j + 1] != color)) + { + break; + } + color++; + } + return color; +} + +int isok(int m, int n, int i, int j, int len) +{ + int k; + if (i + len < m && j + len < n) + { + for (k = 0; k <= len; k++) + if (board[i + k][j + len] != '?' || board[i + len][j + k] != '?') + { + return 0; + } + return 1; + } + else + { + return 0; + } +} + +void search(int m, int n, int i, int j) +{ + char c1, c2; + int len, k; + c1 = find(n, i, j); + board[i][j] = c1; + len = 0; + while (++len) + { + if (j + len >= n || board[i][j + len] != '?') + { + break; + } + c2 = find(n, i, j + len); + if (c2 < c1) + { + break; + } + if (isok(m, n, i, j, len) && (j + len + 1 >= n || board[i][j + len + 1] != c1) && (!i || board[i - 1][j + len] != c1)) + { + for (k = 0; k <= len; k++) + { + board[i + k][j + len] = board[i + len][j + k] = c1; + } + } + else + { + break; + } + } +} + +int main() +{ + int m, n, i, j; + int first = 0; + while (1) + { + cin >> m >> n; + if (!n && !m) + { + break; + } + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + { + cin >> board[i][j]; + } + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + { + if (board[i][j] == '?') + { + search(m, n, i, j); + } + } + if (first++) + { + cout << endl; + } + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + cout << board[i][j]; + } + cout << endl; + } + } + return 0; +} diff --git a/10564.cpp b/10564.cpp new file mode 100644 index 0000000..f174923 --- /dev/null +++ b/10564.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +#define MAXN 42 +#define max(a, b) (a > b ? a : b) +#define min(a, b) (a > b ? b : a) + +typedef long long Long; + +struct ss +{ + int u; +} Size[MAXN]; +struct bd +{ + int max, min, val; +}; +bd B[MAXN][MAXN]; +char Fg[MAXN][MAXN][355]; +Long C[MAXN][MAXN][355]; +int N, S, O; + +void Set() +{ + int i, l = N; + O = N; + for (i = 1; i <= N; i++) + Size[i].u = l--; + l = 2; + for (i = N + 1; i < 2 * N; i++) + Size[i].u = l++; + N = N * 2 - 1; +} +void Dynamic() +{ + int i, j; + for (i = 1; i <= Size[N].u; i++) + B[N][i].max = B[N][i].min = B[N][i].val; + for (i = N - 1; i >= 1; i--) + { + for (j = 1; j <= Size[i].u; j++) + { + if (i >= O) + { + B[i][j].max = max(B[i][j].val + B[i + 1][j].max, + B[i][j].val + B[i + 1][j + 1].max); + B[i][j].min = min(B[i][j].val + B[i + 1][j].min, + B[i][j].val + B[i + 1][j + 1].min); + } + else + { + if (j == 1) + { + B[i][j].max = B[i][j].val + B[i + 1][j].max; + B[i][j].min = B[i][j].val + B[i + 1][j].min; + } + else if (j == Size[i].u) + { + B[i][j].max = B[i][j].val + B[i + 1][j - 1].max; + B[i][j].min = B[i][j].val + B[i + 1][j - 1].min; + } + else + { + B[i][j].max = max(B[i][j].val + B[i + 1][j].max, + B[i][j].val + B[i + 1][j - 1].max); + B[i][j].min = min(B[i][j].val + B[i + 1][j].min, + B[i][j].val + B[i + 1][j - 1].min); + } + } + } + } +} +char Path[45]; +int done; +Long Recur(int sum, int r, int c, int dir) +{ + Long temp = 0; + if (Fg[r][c][sum] == 1) + return C[r][c][sum]; + if (r > 1 && done) + { + if (dir == 1) + Path[r - 2] = 'L'; + else + Path[r - 2] = 'R'; + } + if (r == N) + { + if (sum + B[r][c].val == S) + { + if (done) + { + Path[r - 1] = NULL; + done = 0; + } + return 1; + } + return 0; + } + if (B[r][c].max + sum < S) + return 0; + if (B[r][c].min + sum > S) + return 0; + Fg[r][c][sum] = 1; + if (r < O) + { + if (c == Size[r].u) + temp += Recur(sum + B[r][c].val, r + 1, c - 1, 1); + else if (c == 1) + temp += Recur(sum + B[r][c].val, r + 1, c, 2); + else + { + temp += Recur(sum + B[r][c].val, r + 1, c - 1, 1); + temp += Recur(sum + B[r][c].val, r + 1, c, 2); + } + } + else + { + temp += Recur(sum + B[r][c].val, r + 1, c, 1); + temp += Recur(sum + B[r][c].val, r + 1, c + 1, 2); + } + C[r][c][sum] = temp; + return C[r][c][sum]; +} +void Cal() +{ + int i, c, d = 0; + Long count = 0; + Dynamic(); + done = 1; + for (i = 1; i <= O; i++) + { + if (B[1][i].min <= S && S <= B[1][i].max) + count += Recur(0, 1, i, 0); + if (count > 0 && d == 0) + { + c = i; + d = 1; + } + } + printf("%lld\n", count); + if (count) + { + printf("%d ", c - 1); + puts(Path); + } + else + puts(""); +} +void Free() +{ + int i, j, max = 0; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= Size[i].u; j++) + for (int k = 0; k <= 352; k++) + Fg[i][j][k] = 0; + } +} + +int main() +{ + int i, j; + while (scanf("%d%d", &N, &S) && (N || S)) + { + Set(); + for (i = 1; i <= N; i++) + for (j = 1; j <= Size[i].u; j++) + scanf("%d", &B[i][j].val); + Cal(); + Free(); + } + return 0; +} diff --git a/10565.cpp b/10565.cpp new file mode 100644 index 0000000..b596b11 --- /dev/null +++ b/10565.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int a, x, m, t; + for (scanf("%d", &t); t-- > 0 && scanf("%d", &m) == 1;) + { + for (x = 0; m-- > 0 && scanf("%d", &a) == 1;) + if (x == 0) + { + x = a; + } + printf(x ? "1\n%d\n" : "-1\n", -x); + } + return 0; +} diff --git a/10566.cpp b/10566.cpp new file mode 100644 index 0000000..f5db1c5 --- /dev/null +++ b/10566.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +double x, y, c; + +double f(double t) +{ + if (t == 0.) + { + return (min(x, y) - c); + } + double A = acos(t / y); + double B = acos(t / x); + double x1, x2, x3, x4; + double y1, y2, y3, y4; + double a1, b1, c1; + double a2, b2, c2; + x1 = 0; + y1 = 0; + x2 = t; + y2 = y * sin(A); + x3 = t; + y3 = 0; + x4 = 0; + y4 = x * sin(B); + a1 = y1 - y2; + b1 = x2 - x1; + c1 = (x2 * y1 - x1 * y2); + a2 = y3 - y4; + b2 = x4 - x3; + c2 = (x4 * y3 - x3 * y4); + double cy = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1); + //printf("huh %0.3lf %0.3lf%\n",cy,c); + return (cy - c); +} + +double min(double a, double b) +{ + if (a < b) + { + return a; + } + else + { + return b; + } +} + +int main() +{ + double a, b, t; + while (scanf("%lf %lf %lf", &x, &y, &c) == 3) + { + a = 0.; + b = min(x, y); + t = (a + b) / 2.; + double v1 = f(a); + double v2 = f(b); + //printf("%0.3lf %0.3lf\n",a,b); + //printf("%0.3lf %0.3lf\n",v1,v2); + while (true) + { + double m = f(t); + if (v1 * m < 0.) + { + b = t; + v2 = m; + t = (a + b) / 2.; + } + else + { + a = t; + v1 = m; + t = (a + b) / 2.; + } + if (fabs(a - b) <= 1e-6) + { + break; + } + } + printf("%0.3lf\n", a); + } + return 0; +} diff --git a/10567.cpp b/10567.cpp new file mode 100644 index 0000000..0975ab5 --- /dev/null +++ b/10567.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +/* +10576 +Helping Fill Bates +*/ + +#define maxn 53 +int Ind[200], len; +char Seq[1000002]; +char temp[102]; +set SS[maxn]; +void SetUp() +{ + int i, z = 65, d = 97; + for (i = 0; i < 26; i++) + { + Ind[z++] = i; + Ind[d++] = i + 26; + } + cin >> Seq; + len = strlen(Seq); + for (i = 0; Seq[i]; i++) + { + z = Seq[i]; + z = Ind[z]; + SS[z].insert(i); + } +} +int Find() +{ + int i, top = -1, z, fs = 100000000; + set::iterator upper; + for (i = 0; temp[i]; i++) + { + z = temp[i]; + z = Ind[z]; + upper = SS[z].upper_bound(top); + if (upper == SS[z].end()) + { + return 0; + } + top = *upper; + if (top < fs) + { + fs = top; + } + } + cout << "Matched " << fs << " " << top << endl; + return 1; +} +void Cal() +{ + if (Find() == 0) + { + cout << "Not matched\n"; + } +} +int main() +{ + int n, k; + SetUp(); + cin >> n; + while (n--) + { + cin >> temp; + Cal(); + } + return 0; +} diff --git a/10568.cpp b/10568.cpp new file mode 100644 index 0000000..bc417d7 --- /dev/null +++ b/10568.cpp @@ -0,0 +1,250 @@ +#include + +using namespace std; + +#define maxn 35 + +typedef unsigned long long ss; + +ss Com[maxn][maxn], Val[maxn][maxn], G[maxn], total, N, K; +char CFg[maxn][maxn], Fg[maxn], A[maxn][maxn], BB[maxn][maxn]; +set S; + +int com(const void *a, const void *b) +{ + return strcmp((char *)a, (char *)b); +} +ss Combination(ss n, ss r) +{ + ss d; + if (n == r) + return 1; + if (r == 1) + return n; + if (r == 0) + return 1; + if (CFg[n][r] == 1) + return Com[n][r]; + CFg[n][r] = 1; + d = Combination(n - 1, r) + Combination(n - 1, r - 1); + Com[n][r] = d; + return d; +} +void Gen() +{ + ss i, j, k, n, r, p; + for (i = 2; i <= 15; i++) + { + Val[i][1] = 1; + n = 2 * i - 1; + r = i - 1; + p = 2; + for (j = 2 * i; j <= 30; j += i) + { + k = Combination(n, r); + Val[i][p] = Val[i][p - 1] * k; + p++; + n += i; + } + } + for (i = 16; i <= 30; i++) + Val[i][1] = 1; +} +int Rem(int n, int lim) +{ + int i, c = 0; + for (i = n; i < lim; i++) + if (Fg[i] == 0) + c++; + return c; +} +int Rem1(int n, int tg) +{ + int i, c = 0; + for (i = n; i < tg; i++) + c += G[i]; + return c; +} +void Process(int tg) +{ + int i, j; + string st = ""; + for (i = 0; i < tg; i++) + strcpy(BB[i], A[i]); + qsort(BB, tg, sizeof(BB[0]), com); + st += BB[0]; + for (i = 1; i < tg; i++) + { + st += " "; + st += BB[i]; + } + S.insert(st); +} +void Recur(int n, int g, int tg, int level, string st, int lim, int ck) +{ + int i, j, ng = g, k; + char ch = 'A'; + if (G[g] == level) + { + strcpy(A[g], st.c_str()); + st = ""; + ng = g + 1; + level = 0; + } + if (ng + 1 > tg) + { + if (ck) + { + printf("%s", A[0]); + for (k = 1; k < tg; k++) + printf(" %s", A[k]); + cout << endl; + } + else + Process(tg); + return; + } + for (i = 0; i < lim; i++) + { + if (Fg[i] == 0) + { + if (level > 0) + { + if (i <= n) + continue; + else + { + k = Rem(i, lim) + level; + j = G[ng]; + if (j > k) + return; + ch = i + 'A'; + Fg[i] = 1; + Recur(i, ng, tg, level + 1, st + ch, lim, ck); + Fg[i] = 0; + } + } + else + { + k = Rem(i, lim); + j = Rem1(ng, tg); + if (j > k) + return; + ch = 'A' + i; + Fg[i] = 1; + Recur(i, ng, tg, level + 1, st + ch, lim, ck); + Fg[i] = 0; + } + } + } +} +void Recur2(int n, int level, int ter, string st, int tg, int r) +{ + int i; + char ch; + Fg[n] = 1; + if (level == ter) + { + strcpy(A[0], st.c_str()); + Recur(0, 1, tg, 0, "", N, 0); + Fg[n] = 0; + return; + } + for (i = n + 1; i < N; i++) + { + ch = 'A' + i; + Recur2(i, level + 1, ter, st + ch, tg, r); + } + Fg[n] = 0; +} +void Count() +{ + ss d, k; + if (N == K || K > N || K == 1) + { + printf("1\n"); + return; + } + d = N / K; + if (N % K == 0) + { + total = Val[K][d]; + printf("%llu\n", Val[K][d]); + return; + } + k = Combination(N, N % K); + total = Val[K][d] * k; + printf("%llu\n", Val[K][d] * k); +} +void Print() +{ + ss d, r, tg, i; + string st; + char ch = 'A'; + set::iterator p; + d = N / K; + r = N % K; + if (r == 0) + { + for (tg = 0; tg < d; tg++) + G[tg] = K; + Recur(0, 0, d, 0, "", N, 1); + return; + } + tg = d + 1; + G[0] = r; + for (i = 1; i < tg; i++) + G[i] = K; + for (i = 0; i + r - 1 < N; i++, ch++) + { + st = ch; + Recur2(i, 1, r, st, tg, r); + } + for (p = S.begin(); p != S.end(); p++) + { + st = *p; + printf("%s\n", st.c_str()); + } + S.clear(); +} +void Cal(string Cmd) +{ + ss d; + char ch; + Count(); + if (Cmd == "COUNT") + return; + if (K >= N) + { + for (ch = 'A', d = 0; d < N; ch++, d++) + printf("%c", ch); + printf("\n"); + return; + } + if (K == 1) + { + printf("A"); + for (ch = 'B', d = 1; d < N; d++, ch++) + printf(" %c", ch); + printf("\n"); + return; + } + Print(); +} + +int main() +{ + char Cmd[15]; + Gen(); + while (scanf("%s", Cmd)) + { + if (!strcmp(Cmd, "END")) + { + break; + } + scanf("%llu%llu", &N, &K); + total = 0; + Cal(Cmd); + } + return 0; +} diff --git a/10569.cpp b/10569.cpp new file mode 100644 index 0000000..33a0c47 --- /dev/null +++ b/10569.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +int a[101], n; +bool used[221]; + +// binary search +long long sqrt(long long x) +{ + long long left = 1, right = x, mid = (left + right) / 2; + while (left <= right) + { + if (mid * mid == x) + { + return mid; + } + else if (mid * mid > x) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + return -1; +} + +void solve() +{ + long long t; + srand(time(NULL)); + while (true) + { + memset(used, false, sizeof(used)); + t = 0; + for (int i = 1; i < n - 1; i++) + { + if (rand() % 2) + { + a[i] = 2 * i + 1; + } + else + { + a[i] = 2 * i + 2; + } + used[a[i]] = true; + t += a[i] * a[i] * a[i]; + } + for (int i = 1; i <= 220; i++) + { + if (used[i]) + { + continue; + } + a[n - 1] = i; + long long x = t + i * i * i; + long long k = (9 - 4 * 3 * (1 - x)); + long long p = sqrt(k); + if (p < 0 || p * p != k) + { + continue; + } + int x1 = (int)(-3 + p), x2 = (int)(-3 - p); + if (x1 % 6 == 0) + { + x1 /= 6; + a[n] = x1; + a[0] = x1 + 1; + for (int j = 0; j <= n; j++) + { + if (j) + { + putchar(' '); + } + printf("%d", a[j]); + } + printf("\n"); + return; + } + if (x2 % 6 == 0) + { + x2 /= 6; + a[n] = x2; + a[0] = x2 + 1; + for (int j = 0; j <= n; j++) + { + if (j) + { + putchar(' '); + } + printf("%d", a[j]); + } + printf("\n"); + return; + } + } + } +} + +int main() +{ + int t; + scanf("%d", &t); + for (int c = 1; c <= t; c++) + { + printf("Case %d: ", c); + scanf("%d", &n); + if (n == 1) + { + printf("1 1\n"); + } + else if (n == 2) + { + printf("-1 0 0\n"); + } + else + { + solve(); + } + } + return 0; +} diff --git a/1057.cpp b/1057.cpp new file mode 100644 index 0000000..2e02187 --- /dev/null +++ b/1057.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +const int MaxN = 110; + +int dis[MaxN][MaxN]; +int dp[MaxN][MaxN]; +int N, M; + +int solve() +{ + memset(dp, 0x3f, sizeof(dp)); + dp[0][0] = 1; + priority_queue> pQ; + pQ.push(make_pair(-1, 0)); + while (!pQ.empty()) + { + int d = -pQ.top().first; + int a = pQ.top().second / N; + int b = pQ.top().second % N; + pQ.pop(); + if (d != dp[a][b]) + { + continue; + } + if (a != b && dp[b][a] > dp[a][b] + dis[b][a] - 1) + { + dp[b][a] = dp[a][b] + dis[b][a] - 1; + pQ.push(make_pair(-dp[b][a], b * N + a)); + } + for (int i = 0; i < N; i++) + if (dis[i][a] == 1 && dp[i][b] > dp[a][b] + (i != b)) + { + dp[i][b] = dp[a][b] + (i != b); + pQ.push(make_pair(-dp[i][b], i * N + b)); + } + for (int i = 0; i < N; i++) + if (dis[b][i] == 1 && dp[a][i] > dp[a][b] + (a != i)) + { + dp[a][i] = dp[a][b] + (a != i); + pQ.push(make_pair(-dp[a][i], a * N + i)); + } + } + return dp[1][1]; +} + +int main() +{ + int cas = 0; + while (cin >> N >> M, N || M) + { + memset(dis, 0x3f, sizeof(dis)); + while (M--) + { + int a, b; + cin >> a >> b; + dis[--a][--b] = 1; + } + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); + } + int ans = solve(); + printf("Network %d\n", ++cas); + if (ans <= N) + { + printf("Minimum number of nodes = %d\n", ans); + } + else + { + puts("Impossible"); + } + puts(""); + } + return 0; +} diff --git a/10570.cpp b/10570.cpp new file mode 100644 index 0000000..8b9fc80 --- /dev/null +++ b/10570.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +int n, v[512], z[512]; + +int min(int a, int b) +{ + return a < b ? a : b; +} +int count_swaps() +{ + int cnt = 0, t; + FOR(i, n) + while (z[i] != i) + { + t = z[i]; + z[i] = z[t]; + z[t] = t; + ++cnt; + } + return cnt; +} +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + int ans = 1 << 30; + FOR(i, n) + scanf("%d", v + i), --v[i]; + FOR(i, n)// consider all positions the first position + { + FOR(j, n) + z[j] = v[(i + j) % n]; + ans = min(ans, count_swaps()); + FOR(j, n) + z[j] = v[(i + n - j) % n]; + ans = min(ans, count_swaps()); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/10571.cpp b/10571.cpp new file mode 100644 index 0000000..16e034c --- /dev/null +++ b/10571.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define maxn 12 + +int RT[maxn], CT[maxn], RC[maxn], CC[maxn]; +int RP[maxn], CP[maxn], B[maxn][maxn], N; +char Fg[1002]; +vector D[1002]; + +void Gen() +{ + int i, j; + D[1].push_back(1); + for (i = 2; i < 1001; i++) + { + for (j = 2; j * j <= i; j++) + { + if (i % j == 0) + { + D[i].push_back(j); + D[i].push_back(i / j); + } + } + D[i].push_back(1); + D[i].push_back(i); + } +} +int Ok(int n, int c, int st) +{ + int i, d; + if (c == 2) + return 1; + if (st >= N) + return 1; + for (i = st; i <= N; i++) + { + if (RT[i] % n == 0) + { + d = RT[i] / n; + if (Fg[d] == 0) + return 1; + } + } + return 0; +} +void Print() +{ + int i, j; + for (i = 1; i <= N; i++) + { + printf("%d", B[i][1]); + for (j = 2; j <= N; j++) + printf(" %d", B[i][j]); + printf("\n"); + } + printf("\n"); +} +int Recur(int r, int c1, int c2, int n, int m) +{ + int i, j, k, l, d, q, p; + B[r][c1] = n; + B[r][c2] = m; + CC[c1]++; + CC[c2]++; + CP[c1] *= n; + CP[c2] *= m; + Fg[n] = Fg[m] = 1; + if (r == N) + { + Print(); + return 1; + } + for (i = 1; i < N; i++) + { + if (CC[i] == 2) + continue; + p = CT[i]; + for (k = 0; k < D[p].size(); k++) + { + if (RT[r + 1] % D[p][k] != 0) + continue; + if (Fg[D[p][k]]) + continue; + d = RT[r + 1] / D[p][k]; + if (Fg[d]) + continue; + if (CC[i] > 0) + if (CP[i] * D[p][k] != CT[i]) + continue; + if (CC[i] == 0) + if (Ok(CT[i] / D[p][k], 1, r + 2) == 0) + continue; + if (d == D[p][k]) + continue; + for (j = i + 1; j <= N; j++) + { + if (CC[j] == 2) + continue; + if (CC[j] > 0) + if ((CT[j] / CP[j]) != d) + continue; + if (CT[j] % d == 0) + if (Ok(CT[j] / d, CC[j] + 1, r + 2)) + if (Recur(r + 1, i, j, D[p][k], d)) + return 1; + } + } + } + B[r][c1] = 0; + B[r][c2] = 0; + CC[c1]--; + CC[c2]--; + CP[c1] /= n; + CP[c2] /= m; + Fg[n] = Fg[m] = 0; + return 0; +} +void Cal() +{ + int i, j, k, n, d, g; + for (i = 1; i < N; i++) + { + n = CT[i]; + for (k = 0; k < D[n].size(); k++) + { + g = D[n][k]; + if (RT[1] % D[n][k] != 0) + continue; + d = RT[1] / D[n][k]; + if (d == D[n][k]) + continue; + for (j = i + 1; j <= N; j++) + if (CT[j] % d == 0) + if (Ok(CT[j] / d, 1, 2)) + if (Recur(1, i, j, D[n][k], d)) + return; + } + } +} +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + CC[i] = 0; + CP[i] = 1; + for (j = 1; j <= N; j++) + B[i][j] = 0; + } + for (i = 0; i <= 1000; i++) + Fg[i] = 0; +} +int main() +{ + int i; + Gen(); + while (scanf("%d", &N) && N) + { + for (i = 1; i <= N; i++) + scanf("%d", &CT[i]); + for (i = 1; i <= N; i++) + scanf("%d", &RT[i]); + Ini(); + Cal(); + } + return 0; +} diff --git a/10572.cpp b/10572.cpp new file mode 100644 index 0000000..776c408 --- /dev/null +++ b/10572.cpp @@ -0,0 +1,372 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; +#define mp make_pair +#define rep(i, n) for (int i = 0; i < n; ++i) + +struct myHash +{ + const static int deep = 65; + const static int mod = 4001; + const static int size = 125000; + int head[deep][mod], nxt[size], cs; + int s1[size], s2[size], cnt[size]; + int getHash(int x1, int x2) + { + return abs(x1 * x2 + x1 + x2) % mod; + } + void clear() + { + cs = 0, memset(head, -1, sizeof(head)); + } + int find(int x1, int x2, int f) + { + int u = getHash(x1, x2); + for (int i = head[f][u]; i != -1; i = nxt[i]) + if (s1[i] == x1 && s2[i] == x2) + { + return i; + } + return -1; + } + void insert(int x1, int x2, int f) + { + int u = getHash(x1, x2); + s1[cs] = x1, s2[cs] = x2, cnt[cs] = 0; + nxt[cs] = head[f][u], head[f][u] = cs++; + } + int push(int x1, int x2, int f, int c) + { + int t = find(x1, x2, f); + if (t == -1) + { + insert(x1, x2, f); + t = cs - 1; + } + cnt[t] += c; + return t; + } +}; + +const char *base = "o#."; +const int maxn = 10; +int n, m, sg[maxn][maxn]; +char g[maxn][maxn]; +int state[maxn], st, ed; +PII pre[125000]; +int ynn[2][maxn][maxn]; +int result, flag, tool[maxn]; +myHash dp; + +inline int get(int s, int p) +{ + return (s >> p) & 1; +} + +void expand(int s) +{ + for (int i = 0; i < m; ++i) + { + state[i] = s & 7; + s >>= 3; + } +} +int encode() +{ + int s = 0; + for (int i = m - 1; i >= 0; --i) + { + s = (s << 3) | state[i]; + } + return s; +} + +void preWork() +{ + dp.clear(); + int tot = 1 << m, j, k; + for (int s = 0; s < tot; ++s) + { + for (j = 0; j < m; ++j) + { + if (sg[0][j] != 2 && sg[0][j] != ((s >> j) & 1)) + { + break; + } + } + if (j < m) + { + continue; + } + for (k = 1, j = state[0] = 0; k < m; ++k) + { + if (((s >> k) & 1) != ((s >> (k - 1)) & 1)) + { + ++j; + } + state[k] = j; + } + int t = dp.push(s, encode(), 0, 1); + pre[t] = mp(-1, -1); + } +} + +bool checkl(int s, int x) +{ + expand(s); + for (int i = 0; i < m; ++i) + { + if (i != x && state[i] == state[x]) + { + return false; + } + } + return true; +} + +void weihu() +{ + memset(tool, -1, sizeof(tool)); + for (int i = 0, j = 0; i < m; ++i) + { + if (tool[state[i]] == -1) + { + tool[state[i]] = j++; + } + } + for (int i = 0; i < m; ++i) + { + state[i] = tool[state[i]]; + } +} + +void un(int s, int a, int b) +{ + expand(s); + b = state[b], a = state[a]; + for (int i = 0; i < m; ++i) + { + if (state[i] == b) + { + state[i] = a; + } + } +} + +void create(int s, int a) +{ + expand(s); + int j = -1; + for (int i = 0; i < m; ++i) + { + if (i != a && state[i] > j) + { + j = state[i]; + } + } + state[a] = j + 1; +} + +bool checkc(int s, int x) +{ + for (int i = 0; i < m; ++i) + { + if (i != x && get(s, i) == get(s, x)) + { + return true; + } + } + return false; +} +int ans[maxn * maxn]; +void dfs(int u, int p) +{ + int s1 = dp.s1[p], s2 = dp.s2[p], c = pre[p].second; + if (c == -1) + { + for (int i = 0; i < m; ++i) + { + ans[i] = (s1 >> i) & 1; + } + } + else + { + ans[u + m - 1] = c; + dfs(u - 1, pre[p].first); + } +} + +void output(int u, int p, int c) +{ + for (int i = u + 1; i < n * m; ++i) + { + ans[i] = c; + } + dfs(u, p); +} + +int bfs() +{ + preWork(); + int u = 0; + st = 0, ed = dp.cs; + for (int i = 0; i < n; ++i) + { + for (int j = ((!i) ? (m - 1) : (0)); j < m; ++j) + { + if (i == n - 1 && j == m - 1) + { + return u; + } + for (; st < ed; ++st) + { + for (int c = 0; c < 2; ++c) + { + int x = i, y = j, s1 = dp.s1[st], s2 = dp.s2[st]; + int x1 = get(s1, j), x2 = get(s1, j + 1), x3 = get(s1, m); + if (j == m - 1) + { + s1 ^= x3 << m; + x1 = x3 = -1, x2 = get(s1, 0); + ++x, y = -1; + } + if (sg[x][y + 1] != 2 && sg[x][y + 1] != c) + { + continue; + } + if (x == n - 1 && y + 1 == m - 1 && x1 != c && x2 != c && x3 == c) + { + continue; + } + if (x1 == c && x2 == c && x3 == c) + { + continue; + } + if (x2 != c && checkl(s2, y + 1)) + { + if (checkc(s1, y + 1) || ynn[!c][x][y + 1]) + { + continue; + } + if (x < n - 1 || (y + 1) < m - 2) + { + continue; + } + result += dp.cnt[st]; + if (!flag) + { + flag = 1; + output(u, st, c); + } + continue; + } + if (x3 != -1) + { + s1 ^= (x3 << m); + } + s1 ^= (x2 << (y + 1)); + s1 |= (x2 << m) | (c << (y + 1)); + if (x1 == c && x2 == c) + { + un(s2, y, y + 1); + } + else if (x1 != c && x2 != c) + { + create(s2, y + 1); + } + else if (x1 == c && x2 != c) + { + expand(s2); + state[y + 1] = state[y]; + } + else if (x1 != c && x2 == c) + { + expand(s2); + } + weihu(); + int t = dp.push(s1, encode(), u + 1, dp.cnt[st]); + pre[t] = mp(st, c); + } + } + ++u, ed = dp.cs; + } + } + return -1; +} + +void calc(int u) +{ + memset(tool, -1, sizeof(tool)); + for (; st < dp.cs; ++st) + { + expand(dp.s2[st]); + int c = 0; + for (int i = 0; i < m; ++i) + { + if (tool[state[i]] != -st) + { + tool[state[i]] = -st, ++c; + } + } + if (c <= 2) + { + result += dp.cnt[st]; + if (!flag) + { + flag = 1; + output(u, st, pre[st].second); + } + } + } +} + +void solved(int nT) +{ + scanf("%d %d", &n, &m); + rep(i, n) + { + scanf("%s", g[i]); + rep(j, m) sg[i][j] = find(base, base + 3, g[i][j]) - base; + } + int t[2] = {0, 0}; + for (int i = n - 1; i >= 0; --i) + { + for (int j = m - 1; j >= 0; --j) + { + for (int u = 0; u <= 1; ++u) + { + ynn[u][i][j] = sg[i][j] == u || t[u]; + t[u] = ynn[u][i][j]; + } + } + } + result = flag = 0; + calc(bfs()); + printf("%d\n", result); + if (flag) + { + for (int i = 0, c = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j, ++c) + { + putchar(ans[c] ? '#' : 'o'); + } + puts(""); + } + } + puts(""); +} + +int main() +{ + int T = 1; + scanf("%d", &T); + for (int nT = 1; nT <= T; ++nT) + { + solved(nT); + } + return 0; +} diff --git a/10573.cpp b/10573.cpp new file mode 100644 index 0000000..2765ace --- /dev/null +++ b/10573.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); +char cmd[100]; +int numOfSet, r1, r2, t; +bool oneInput; + +bool check(char *buf) +{ + int i; + for (i = 0; i < strlen(buf); i++) + if (isspace(buf[i]) && isdigit(buf[i - 1])) + { + return true; + } + return false; +} + +void input() +{ + cin.getline(cmd, 100); + if (check(cmd)) + { + oneInput = false; + char *p; + p = strtok(cmd, " "); + if (p) + { + r1 = atoi(p); + } + p = strtok(NULL, " "); + if (p) + { + r2 = atoi(p); + } + } + else + { + oneInput = true; + t = atoi(cmd); + } +} + +void solve() +{ + if (oneInput) + { + printf("%.4lf\n", PI * t * t / 8.0); + } + else + { + printf("%.4lf\n", PI * 2 * r1 * r2); + } +} + +int main() +{ + while (cin >> numOfSet) + { + cin.get(); + int case_i; + for (case_i = 0; case_i < numOfSet; case_i++) + { + input(); + solve(); + } + } + return 0; +} diff --git a/10574.cpp b/10574.cpp new file mode 100644 index 0000000..1542643 --- /dev/null +++ b/10574.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +/* +10574 +Counting Rectangles +*/ +#define MAXN 5005 +int np; +struct ss +{ + int x, y; +} point[MAXN], dum; +int A[5000], B[5000]; +int cur, inda, indb; +int com(const void *a, const void *b) +{ + ss *p, *q; + p = (ss *)a; + q = (ss *)b; + if (p->x == q->x) + { + return p->y - q->y; + } + return p->x - q->x; +} +int com1(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int Match() +{ + int i, j, count = 0, mt = 0; + int p = 0, f = B[indb - 1]; + for (i = 0; i < inda; i++) + { + if (A[inda - 1] < B[p]) + { + break; + } + for (j = p; j < indb; j++) + { + if (A[i] < B[j]) + { + p = j; + break; + } + if (A[i] == B[j]) + { + mt++; + p = j + 1; + break; + } + } + } + count = (mt * (mt - 1)) / 2; + return count; +} +int Cal(int mm) +{ + int j, nt, count = 0; + cur = point[mm].x; + indb = 0; + nt = mm; + cur = point[nt].x; + while (nt < np) + { + for (j = nt; j < np && point[j].x == cur; j++) + { + B[indb++] = point[j].y; + } + if (indb > 1) + { + count += Match(); + } + nt = j; + cur = point[nt].x; + indb = 0; + } + return count; +} +void Select() +{ + int i, k, c = 0; + int nt = 0, cur = point[0].x; + while (nt < np) + { + k = 0; + inda = 0; + for (i = nt; i < np && point[i].x == cur; i++) + { + A[inda++] = point[i].y; + } + nt = i; + if (nt >= np - 1) + { + break; + } + cur = point[i].x; + if (inda <= 1) + { + continue; + } + c += Cal(nt); + } + printf("%d\n", c); +} +int main() +{ + int kase, i, f = 1; + scanf("%d", &kase); + while (kase--) + { + scanf("%d", &np); + for (i = 0; i < np; i++) + { + scanf("%d%d", &point[i].x, &point[i].y); + } + qsort(point, np, sizeof(point[0]), com); + printf("Case %d: ", f++); + Select(); + } + return 0; +} diff --git a/10575.cpp b/10575.cpp new file mode 100644 index 0000000..9cfc051 --- /dev/null +++ b/10575.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int x[1000], y[1000]; +int n; +int test(int, int); +int d(int, int); +int main() +{ + int i, j, k, ax; + for (j = 1;; j++) + { + scanf("%d", &n); + if (!n) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%d %d ", x + i, y + i); + } + ax = 0; + for (i = 0; i < n; i++) + { + ax += test(i, i + 1) + test(i, i + 2); + } + printf("Polygon #%d has %d symmetry line(s).\n", j, ax / 2); + } +} + +int test(int a, int b) +{ + int i, j, k; + for (i = 1; i < n; i++) + { + if (d(a, a + n - i) != d(b, b + i)) + { + return 0; + } + if (d(a, b + i) != d(b, a + n - i)) + { + return 0; + } + } + return 1; +} + +int d(int c, int d) +{ + return (x[c % n] - x[d % n]) * (x[c % n] - x[d % n]) + (y[c % n] - y[d % n]) * (y[c % n] - y[d % n]); +} diff --git a/10576.cpp b/10576.cpp new file mode 100644 index 0000000..b7fbc1f --- /dev/null +++ b/10576.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + int s, d; + while (cin >> s >> d) + { + int dnum; + for (dnum = 1; dnum < 6; ++dnum) + { + if (s * (5 - dnum) - d * dnum < 0) + { + break; + } + } + int sum = 0; + for (int m = 0; m < 12; ++m) + { + if (m % 5 < 5 - dnum) + { + sum += s; + } + else + { + sum -= d; + } + } + if (sum <= 0) + { + cout << "Deficit" << endl; + } + else + { + cout << sum << endl; + } + } + return 0; +} diff --git a/10577.cpp b/10577.cpp new file mode 100644 index 0000000..114dff6 --- /dev/null +++ b/10577.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +int n; +double xa, ya, xb, yb, xc, yc; + +struct Point +{ + double x, y; +}; + +Point rotate(Point A, Point B, double delta) +{ + Point C; + Point vec; + vec.x = B.x - A.x; + vec.y = B.y - A.y; + double r = sqrt(vec.x * vec.x + vec.y * vec.y); + double sita = asin(fabs(vec.y) / r); + if (vec.x >= 0 && vec.y >= 0) + { + sita = sita; + } + if (vec.x <= 0 && vec.y >= 0) + { + sita = PI - sita; + } + if (vec.x <= 0 && vec.y <= 0) + { + sita = PI + sita; + } + if (vec.x >= 0 && vec.y <= 0) + { + sita = -sita; + } + sita += delta; + vec.x = r * cos(sita); + vec.y = r * sin(sita); + C.x = A.x + vec.x; + C.y = A.y + vec.y; + return C; +} + +int main() +{ + int kase = 1; + while (scanf("%d", &n) == 1 && n) + { + scanf("%lf%lf", &xa, &ya); + scanf("%lf%lf", &xb, &yb); + scanf("%lf%lf", &xc, &yc); + double A = xa * xa + ya * ya; + double B = xb * xb + yb * yb; + double C = xc * xc + yc * yc; + double Xbc = xb - xc; + double Xab = xa - xb; + double Xac = xa - xc; + double Ybc = yb - yc; + double Yab = ya - yb; + double Yac = ya - yc; + double x, y; + y = ((A - B) * Xbc - (B - C) * Xab) / (2 * Yab * Xbc - 2 * Ybc * Xab); + if (fabs(xa - xb) > 0.0000001) + { + x = ((A - B) - 2 * Yab * y) / (2 * Xab); + } + else + { + x = ((B - C) - 2 * Ybc * y) / (2 * Xbc); + } + Point O, P; + O.x = x; + O.y = y; + P.x = xa; + P.y = ya; + double delta = 2 * acos(-1.0) / n; + double xmax = -100000000; + double xmin = 100000000; + double ymax = -100000000; + double ymin = 100000000; + for (int i = 0; i < n; i++) + { + // printf( "Poly %.3lf %.3lf\n",P.x,P.y ); + if (P.x > xmax) + { + xmax = P.x; + } + if (P.x < xmin) + { + xmin = P.x; + } + if (P.y > ymax) + { + ymax = P.y; + } + if (P.y < ymin) + { + ymin = P.y; + } + P = rotate(O, P, delta); + } + printf("Polygon %d: %.3lf\n", kase++, (xmax - xmin) * (ymax - ymin)); + } + return 0; +} diff --git a/10578.cpp b/10578.cpp new file mode 100644 index 0000000..b9ff548 --- /dev/null +++ b/10578.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int Fre[10]; +char play[100], Fg[500000]; + +int Index() +{ + int i, j = 1, sum = 0; + for (i = 6; i >= 1; i--) + { + sum += Fre[i] * j; + j *= 10; + } + return sum; +} +int Recur(int sum, int n) +{ + int i, k, f = 0, ind; + if (sum > 31) + return 1; + if (sum == 31) + return 0; + Fre[n]++; + for (i = 1; i <= 6; i++) + { + if (Fre[i] >= 4) + continue; + k = Recur(sum + i, i); + if (!k) + { + f = 1; + break; + } + } + Fre[n]--; + if (f) + return 1; + return 0; +} +void Cal() +{ + int i, sum = 0, l, k; + for (i = 1; i < 7; i++) + Fre[i] = 0; + for (i = 0; play[i]; i++) + { + l = play[i] - '0'; + sum += l; + Fre[l]++; + } + l = i; + if (l == 0) + { + printf("%s A\n", play); + return; + } + if (sum == 31) + { + printf("%s %c\n", play, ((l - 1) % 2) + 65); + return; + } + for (i = 1; i < 7; i++) + { + if (Fre[i] >= 4) + continue; + k = Recur(sum + i, i); + if (!k) + { + printf("%s %c\n", play, (l % 2) + 65); + return; + } + } + printf("%s %c\n", play, (l + 1) % 2 + 65); +} + +int main() +{ + while (gets(play)) + Cal(); + return 0; +} diff --git a/10579.cpp b/10579.cpp new file mode 100644 index 0000000..ca3c056 --- /dev/null +++ b/10579.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define limit 1000000000 +int F[5000][120] = {0}, len[5000] = {1, 1}; +int main() +{ + int n, i; + F[1][0] = 1; + for (n = 2; n < 5000; n++) + { + len[n] = len[n - 1]; + for (i = 0; i < len[n]; i++) + { + F[n][i] = F[n - 1][i] + F[n - 2][i]; + } + for (i = 0; i < len[n]; i++) + { + F[n][i + 1] += F[n][i] / limit; + F[n][i] %= limit; + } + if (F[n][len[n]]) + { + len[n]++; + } + } + while (scanf("%d", &n) == 1) + { + printf("%d", F[n][len[n] - 1]); + for (i = len[n] - 2; i >= 0; i--) + { + printf("%09d", F[n][i]); + } + printf("\n"); + } + return 0; +} diff --git a/1058.cpp b/1058.cpp new file mode 100644 index 0000000..560bbd6 --- /dev/null +++ b/1058.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const double PI = M_PI, PI_2 = PI * 0.5, EPS = 1e-8; +int x[10240], y[10240], n, thita; +double th, r[10240], a[10240]; + +bool ok() +{ + if (thita > 0) + { + for (int i = 0; i < n; ++i) + { + if (x[i] > x[i + 1]) + { + return false; + } + } + } + printf("Acceptable as proposed\n"); + return true; +} + +int main() +{ + for (int t = 1; cin >> n >> thita, n || thita; ++t) + { + th = PI / 180 * thita; + for (int i = 1; i <= n; ++i) + { + cin >> x[i] >> y[i]; + } + printf("Case %d: ", t); + if (!ok()) + { + double ul = 0, ur = PI, dl = 0, dr = PI; + for (int i = 0; i < n; ++i) + { + if (x[i] != x[i + 1] || y[i] != y[i + 1]) + { + double tmp = atan2(y[i + 1] - y[i], x[i + 1] - x[i]); + if (fabs(tmp) > PI_2) + { + if (tmp > 0) + { + ul = max(ul, PI_2 + PI - tmp); + dl = max(dl, tmp - PI_2); + } + else + { + ul = max(ul, -tmp - PI_2); + dl = max(dl, PI_2 + PI + tmp); + } + } + else + { + ur = min(ur, PI_2 - tmp); + dr = min(dr, PI_2 + tmp); + } + } + } + if (ul > ur + EPS && dl > dr + EPS) + { + cout << "Unacceptable" << endl; + } + else + { + if (ul > ur + EPS) + { + ul = 1e10; + } + if (dl > dr + EPS) + { + dl = 1e10; + } + cout << "Acceptable after " + << ((ul + EPS < dl) ? "counter" : "") + << "clockwise rotation of " + << fixed << setprecision(2) << min(ul, dl) * 180 / PI + << " degrees" << endl; + } + } + printf("\n"); + } + return 0; +} diff --git a/10580.cpp b/10580.cpp new file mode 100644 index 0000000..d5d8fa7 --- /dev/null +++ b/10580.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define K 1024 + +char note[20 * K], dic[100 * K], dic1[100 * K], *s[100 * K], str[100]; +int len; + +int compare(const void *a, const void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} + +int complen(char *a, char *b) +{ + for (int l = 0;; l++) + if (!a[l] || !b[l] || a[l] != b[l]) + { + return l; + } +} + +// binary search +char *BinarySearch(int left, int right, char *ss) +{ + int mid = (left + right) / 2, ans, ans1; + while (left <= right) + { + if (strcmp(s[mid], ss) >= 0) + { + ans = mid; + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + if (ans != 0) + { + ans1 = ans - 1; + } + char *p1 = s[ans], *p2 = s[ans1]; + return complen(p1, ss) > complen(p2, ss) ? p1 : p2; +} + +int main() +{ + gets(note); + int nl = strlen(note); + char *s1 = dic, *s2 = dic1; + dic[0] = dic1[0] = 0; + while (gets(str) != NULL) + { + strcat(str, "$"); + int l = strlen(str); + strcat(s1, str); + s1 += l; + for (int i = 0; i < l; i++) + { + str[i] = tolower(str[i]); + } + strcat(s2, str); + s2 += l; + } + len = strlen(dic1); + for (int i = 0; i < len; i++) + { + s[i] = dic1 + i; + } + qsort(s, len, sizeof(char *), compare); + vector c; + for (int i = 0, l; i < nl; i += l) + { + if (note[i] == ' ') + { + l = 1; + continue; + } + char *t = BinarySearch(0, len - 1, note + i), *tt = str; + l = complen(note + i, t); + int k = t - dic1; + for (int i = 0; i < l; i++) + { + str[i] = *(dic + k + i); + } + while (*tt == ' ') + { + tt++; + } + while (str[l - 1] == ' ') + { + l--; + } + str[l] = '\0'; + string temp(tt); + c.push_back(temp); + } + printf("%d\n", c.size()); + for (int i = 0; i < c.size(); i++) + { + puts(c[i].c_str()); + } + return 0; +} diff --git a/10581.cpp b/10581.cpp new file mode 100644 index 0000000..dd92942 --- /dev/null +++ b/10581.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int c[221][11][221], n, m, k; +void init() +{ + for (int i = 1; i <= 220; i++) + { + c[i][1][i] = 1; + } + for (int i = 1; i <= 220; i++) + for (int j = 2; j <= 10; j++) + for (int k = 1; j * k <= i; k++) + for (int l = k; (j - 1) * l <= (i - k); l++) + { + c[i][j][k] += c[i - k][j - 1][l]; + } +} +int main() +{ + int t; + init(); + scanf("%d", &t); + while (t--) + { + scanf("%d %d %d", &m, &n, &k); + for (int now = 0, i = 1; n; m -= i, n--) + { + for (; now + c[m][n][i] < k; now += c[m][n][i], i++) + ; + printf("%d\n", i); + } + } + return 0; +} diff --git a/10582.cpp b/10582.cpp new file mode 100644 index 0000000..bd26c7d --- /dev/null +++ b/10582.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +char buf[10000], temp[3][10000], temp2[4][4]; +int map_[65][65], state[65][65], used[65][65], r, c, total; + +void back(int row, int col, int row2, int col2) +{ + int type = map_[row][col], i, j; + if (row == r - 1 && col == c - 1) + { + ++total; + return; + } + if (used[row][col] || row > r || row < 0 || col > c || col < 0) + { + return; + } + if (type == -1) + { + return; + } + if (!row && !col) + { + if (!type) + { + used[row][col] = 1; + back(row, col + 1, row, col); + back(row, col - 1, row, col); + back(row + 1, col, row, col); + back(row - 1, col, row, col); + used[row][col] = 0; + } + else + { + used[row][col] = 1; + /* + _| + */ + back(row, col - 1, row, col); + back(row - 1, col, row, col); + /* + |_ + */ + back(row, col + 1, row, col); + back(row - 1, col, row, col); + /* + _ + | + */ + back(row, col - 1, row, col); + back(row + 1, col, row, col); + used[row][col] = 0; + } + return; + } + if (!type) + { + used[row][col] = 1; + if (col - 1 == col2) + { + back(row, col + 1, row, col); + } + else if (col + 1 == col2) + { + back(row, col - 1, row, col); + } + else if (row - 1 == row2) + { + back(row + 1, col, row, col); + } + else if (row + 1 == row2) + { + back(row - 1, col, row, col); + } + used[row][col] = 0; + } + else + { + used[row][col] = 1; + /* + _| + */ + if (row - 1 == row2) + { + back(row, col - 1, row, col); + } + if (col - 1 == col2) + { + back(row - 1, col, row, col); + } + /* + |_ + */ + if (row - 1 == row2) + { + back(row, col + 1, row, col); + } + if (col + 1 == col2) + { + back(row - 1, col, row, col); + } + /* + _ + | + */ + if (row + 1 == row2) + { + back(row, col + 1, row, col); + } + if (col + 1 == col2) + { + back(row + 1, col, row, col); + } + /* + _ + | + */ + if (row + 1 == row2) + { + back(row, col - 1, row, col); + } + if (col - 1 == col2) + { + back(row + 1, col, row, col); + } + used[row][col] = 0; + } +} + +int main() +{ + int t, i, j; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &r, &c); + getchar(); + gets(buf); + for (i = 0; i < r * 4; i += 4) + { + gets(temp[0]); + gets(temp[1]); + gets(temp[2]); + gets(buf); + for (j = 0; j < c * 4; j += 4) + { + temp2[0][0] = temp[0][j + 1]; + temp2[0][1] = temp[0][j + 2]; + temp2[0][2] = temp[0][j + 3]; + temp2[1][0] = temp[1][j + 1]; + temp2[1][1] = temp[1][j + 2]; + temp2[1][2] = temp[1][j + 3]; + temp2[2][0] = temp[2][j + 1]; + temp2[2][1] = temp[2][j + 2]; + temp2[2][2] = temp[2][j + 3]; + if (temp2[0][0] == temp2[0][1] && temp2[0][1] == temp2[0][2] && + temp2[0][2] == temp2[1][0] && temp2[1][0] == temp2[1][1] && + temp2[1][1] == temp2[1][2] && temp2[1][2] == temp2[2][0] && + temp2[2][0] == temp2[2][1] && temp2[2][1] == temp2[2][2]) + { + map_[i / 4][j / 4] = -1; + } + else if (temp2[1][0] == temp2[1][1] && temp2[1][1] == temp2[1][2] && temp2[1][0] == '*') + { + map_[i / 4][j / 4] = 0; + } + else if (temp2[0][1] == temp2[1][1] && temp2[1][1] == temp2[2][1] && temp2[0][1] == '*') + { + map_[i / 4][j / 4] = 0; + } + else + { + map_[i / 4][j / 4] = 1; + } + } + } + + for (i = 0; i < r; ++i) + { + memset(used, 0, sizeof(int) * c); + } + total = 0; + back(0, 0, 0, 0); + printf("Number of solutions: %d\n", total); + } + return 0; +} diff --git a/10583.cpp b/10583.cpp new file mode 100644 index 0000000..412b752 --- /dev/null +++ b/10583.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int R[50005]; +int r(int x) +{ + if (R[x] == x) + { + return x; + } + return R[x] = r(R[x]); +} +int main() +{ + int t, n, m, i, j, ra, rb, rr; + for (t = 1; scanf("%d%d", &n, &m) == 2; t++) + { + if ((n | m) == 0) + { + break; + } + rr = n; + for (i = 1; i <= n; i++) + { + R[i] = i; + } + while (m--) + { + scanf("%d%d", &i, &j); + ra = r(i); + rb = r(j); + if (ra == rb) + { + continue; + } + rr--; + R[ra] = rb; + } + printf("Case %d: %d\n", t, rr); + } + return 0; +} diff --git a/10584.cpp b/10584.cpp new file mode 100644 index 0000000..a21c473 --- /dev/null +++ b/10584.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +bool exp_[50]; +char con[50][2][80], acr[50][2][80], str[160]; +int len[50][2], len1[50][2], C, A; + +int findcon(char *s) +{ + for (int i = 0; i < C; i++) + { + int length = len[i][0]; + bool flag = true; + for (int j = 0; j < length; j++) + if (tolower(s[j]) != tolower(con[i][0][j])) + { + flag = false; + } + if (flag) + { + return i + 1; + } + } + return 0; +} +int findacr(char *s) +{ + for (int i = 0; i < A; i++) + { + int length = len1[i][0]; + bool flag = true; + for (int j = 0; j < length; j++) + if (s[j] != acr[i][0][j]) + { + flag = false; + } + if (flag) + { + return i + 1; + } + } + return 0; +} + +int main() +{ + gets(str); + sscanf(str, "%d %d", &C, &A); + for (int i = 0, j = 0, l = 0; i < C; i++, l = 0) + { + gets(str); + for (j = 0; str[j] != '\"'; j++) + ; + for (j++; str[j] != '\"'; j++) + { + con[i][0][l++] = tolower(str[j]); + } + con[i][0][l] = '\0'; + len[i][0] = l; + l = 0; + for (j++; str[j] != '\"'; j++) + ; + for (j++; str[j] != '\"'; j++) + { + con[i][1][l++] = tolower(str[j]); + } + con[i][1][l] = '\0'; + len[i][1] = l; + } + for (int i = 0, j = 0, l = 0; i < A; i++, l = 0) + { + gets(str); + for (j = 0; str[j] != '\"'; j++) + ; + for (j++; str[j] != '\"'; j++) + { + acr[i][0][l++] = str[j]; + } + acr[i][0][l] = '\0'; + len1[i][0] = l; + l = 0; + for (j++; str[j] != '\"'; j++) + ; + for (j++; str[j] != '\"'; j++) + { + acr[i][1][l++] = str[j]; + } + acr[i][1][l] = '\0'; + len1[i][1] = l; + } + do + { + memset(exp_, false, sizeof(exp_)); + char temp[80], temp1[80], *p, k; + if (gets(str) == NULL) + { + break; + } + while (1) + { + if (strcmp(str, "#") == 0) + { + puts("#"); + break; + } + for (p = str; *p;) + { + int ans = findcon(p); + if (ans) + { + int length = len[ans - 1][1]; + if (isupper(*p)) + { + printf("%c", toupper(con[ans - 1][1][0])); + } + else + { + printf("%c", con[ans - 1][1][0]); + } + if (isupper(*(p + 1))) + for (int i = 1; i < length; i++) + { + printf("%c", toupper(con[ans - 1][1][i])); + } + else + for (int i = 1; i < length; i++) + { + printf("%c", con[ans - 1][1][i]); + } + p += len[ans - 1][0]; + } + else if (ans = findacr(p)) + { + int length = len1[ans - 1][0]; + if (exp_[ans - 1]) + { + printf("%s", acr[ans - 1][0]); + } + else + { + exp_[ans - 1] = true; + printf("%s (%s)", acr[ans - 1][1], acr[ans - 1][0]); + } + p += length; + } + else + { + putchar(*p++); + } + } + putchar('\n'); + gets(str); + } + } while (1); + return 0; +} diff --git a/10585.cpp b/10585.cpp new file mode 100644 index 0000000..771b068 --- /dev/null +++ b/10585.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define N 10005 + +struct Point +{ + int x, y; + Point(){}; + Point(int _x, int _y) + { + x = _x, y = _y; + } +} p[N]; + +bool solve(Point &ave, int n) +{ + if ((ave.x % n) || (ave.y % n)) + { + return 0; + } + ave.x /= n, ave.y /= n; + for (int i = 0; i < n; i++) + { + int x = 2 * ave.x - p[i].x; + int y = 2 * ave.y - p[i].y; + bool mk = 0; + for (int j = 0; j < n; j++) + { + if (x == p[j].x && y == p[j].y) + { + mk = 1; + break; + } + } + if (!mk) + { + return 0; + } + } + return 1; +} + +int main() +{ + int t, n; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + Point ave(0, 0); + for (int i = 0; i < n; i++) + { + scanf("%d %d", &p[i].x, &p[i].y); + ave.x += p[i].x, ave.y += p[i].y; + } + if ((n == 1) || solve(ave, n)) + { + puts("yes"); + } + else + { + puts("no"); + } + } + return 0; +} diff --git a/10586.cpp b/10586.cpp new file mode 100644 index 0000000..51f209b --- /dev/null +++ b/10586.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int MAXN = 10001; +int n, k, a[MAXN]; +int main() +{ + while (scanf("%d %d", &n, &k) != EOF) + { + if (n == -1 && k == -1) + { + break; + } + memset(a, 0, MAXN); + for (int i = 0; i <= n; ++i) + { + scanf("%d", &a[i]); + } + for (int i = n; i >= k; i--) + { + if (a[i] == 0) + { + continue; + } + a[i - k] -= a[i]; + a[i] = 0; + } + int s = n; + while (a[s] == 0 && s > 0) + { + s--; + } + for (int j = 0; j < s; j++) + { + printf("%d ", a[j]); + } + printf("%d\n", a[s]); + } + return 0; +} diff --git a/10587.cpp b/10587.cpp new file mode 100644 index 0000000..35d4844 --- /dev/null +++ b/10587.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +const int N = 20000; + +class Tree +{ +public: + int lx, rx; + Tree *l, *r; + int node; + Tree(){}; + Tree(int tlx, int trx) : lx(tlx), rx(trx), l(NULL), r(NULL){}; + + void find() + { + cout << lx << " " << rx << " " << node << endl; + if (lx == rx) + { + return; + } + l->find(); + r->find(); + } + + int insert(int ltar, int rtar, int n, bool &flag) + { + int mid = lx + (rx - lx) / 2; + int ret = 0; + + if (node != -1) + { + return 1; + } + if (l != NULL && ltar <= mid) + { + ret += l->insert(ltar, min(rtar, mid), n, flag); + } + if (r != NULL && mid + 1 <= rtar) + { + ret += r->insert(max(ltar, mid + 1), rtar, n, flag); + } + + if (ret == 2) + { + if (!(l->node == n && r->node == n)) + { + node = (1 << 21); + return 1; + } + } + + if (lx == rx) + { + if (node == -1) + { + node = n; + flag = true; + return 1; + } + else + { + return 0; + } + } + + if (ltar <= lx && rx <= rtar) + { + if (node == -1) + { + node = n; + flag = true; + return 1; + } + else + { + return 0; + } + } + return 0; + } + + void construct(int ltar, int rtar) + { + node = -1; + lx = ltar; + rx = rtar; + if (ltar == rtar) + { + return; + } + int mid = ltar + ((rtar - ltar)) / 2; + l = new Tree(ltar, mid); + r = new Tree(mid + 1, rtar); + l->construct(ltar, mid); + r->construct(mid + 1, rtar); + } + + void destroy() + { + if (l != NULL) + { + l->destroy(); + } + if (r != NULL) + { + r->destroy(); + } + free(r); + free(l); + r = l = NULL; + } +}; + +int main() +{ + int te; + cin >> te; + while (te--) + { + int n; + cin >> n; + map M; + vector> in; + vector check; + rep(i, n) + { + pair tmp; + cin >> tmp.first >> tmp.second; + in.push_back(tmp); + check.push_back(tmp.first); + check.push_back(tmp.second); + } + sort(ALL(check)); + check.erase(unique(ALL(check)), check.end()); + rep(i, check.size()) M[check[i]] = i; + rep(i, n) + { + in[i].first = M[in[i].first]; + in[i].second = M[in[i].second]; + } + reverse(ALL(in)); + Tree root; + root.construct(0, M.size() - 1); + int ans = 0; + rep(i, in.size()) + { + bool flag = false; + root.insert(in[i].first, in[i].second, i, flag); + if (flag) + { + ans++; + } + } + cout << ans << endl; + root.destroy(); + } + return 0; +} diff --git a/10588.cpp b/10588.cpp new file mode 100644 index 0000000..d02c348 --- /dev/null +++ b/10588.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +const int N = 1005; + +int T, n, m; + +struct Person +{ + int t, id; + bool operator<(const Person &c) const + { + if (t != c.t) + { + return t > c.t; + } + return id > c.id; + } +} p; + +priority_queue Q[N]; +queue q[N]; + +void init() +{ + scanf("%d%d", &n, &m); + int k, num; + for (int i = 0; i < n; i++) + { + p.id = i; + scanf("%d%d", &p.t, &k); + for (int j = 0; j < k; j++) + { + scanf("%d", &num); + num--; + q[p.id].push(num); + } + Q[q[p.id].front()].push(p); + } +} + +int solve() +{ + int flag = 1, ans = 0; + while (flag) + { + flag = 0; + int Min = 1000000; + for (int i = 0; i < m; i++) + { + if (!Q[i].empty()) + { + flag = 1; + Person now = Q[i].top(); + if (ans < now.t) + { + Min = min(Min, now.t); + continue; + } + Min = min(Min, ans + 1); + Q[i].pop(); + q[now.id].pop(); + if (!q[now.id].empty()) + { + now.t = ans + 1; + Q[q[now.id].front()].push(now); + } + } + } + if (flag) + { + ans++; + ans = max(Min, ans); + } + } + return ans; +} + +int main() +{ + scanf("%d", &T); + while (T--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/10589.cpp b/10589.cpp new file mode 100644 index 0000000..280a733 --- /dev/null +++ b/10589.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +double distance(double x1, double y1, double x2, double y2) +{ + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); +} + +int main() +{ + int N, a; + while (scanf("%d%d", &N, &a) != EOF && N != 0 && a != 0) + { + int i; + int M = 0; + for (i = 0; i < N; i++) + { + double x, y; + scanf("%lf%lf", &x, &y); + if (distance(x, y, 0.0, 0.0) <= (double)(a * a)) + if (distance(x, y, 0.0, (double)a) <= (double)(a * a)) + if (distance(x, y, (double)a, (double)a) <= (double)(a * a)) + if (distance(x, y, (double)a, 0.0) <= (double)(a * a)) + { + M++; + } + } + printf("%.5lf\n", (double)M / (double)N * (double)a * (double)a); + } + return 0; +} diff --git a/1059.cpp b/1059.cpp new file mode 100644 index 0000000..15c8cb0 --- /dev/null +++ b/1059.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +typedef long long ll; + +struct vec +{ + int x, y; + void read() + { + cin >> x >> y; + } + ll mul(const vec &a, const vec &b) + { + return (a.x - x) * (ll)(b.y - y) - (a.y - y) * (ll)(b.x - x); + } +}; +vec p[1024], q[1024]; +int n, m, N; +ll solve() +{ + if (N <= 2) + { + return 0; + } + int g = 0; + q[N] = q[0]; + for (int i = 0; i <= N; ++i) + { + g = __gcd(g, __gcd(q[i].x -= q[N].x, q[i].y -= q[N].y)); + } + for (int i = 1; i < N; ++i) + { + q[i].x /= g; + q[i].y /= g; + } + ll a = 0, p = 0; + for (int i = 2; i < N; ++i) + { + a += q[0].mul(q[i - 1], q[i]); + } + if (a < 0) + { + a = -a; + } + for (int i = 0; i < N; ++i) + { + p += abs(__gcd(q[i + 1].x - q[i].x, q[i + 1].y - q[i].y)); + } + //(a*k^2-p*k)/2 +1, k = 1..m + ll M = m; + ll x = M * (M + 1) >> 1, + y = a * (M << 1 | 1) - p * 3; + if (x & 1) + { + y >>= 1; + } + else + { + x >>= 1; + } + if (x % 3) + { + y /= 3; + } + else + { + x /= 3; + } + return M + x * y; +} +int main() +{ + for (int Tc = 1; cin >> n >> m, n || m; ++Tc) + { + for (int i = 0; i < n; ++i) + { + p[i].read(); + } + N = 0; + for (int i = 0; i < n; ++i) + if (p[i].mul(p[(i + n - 1) % n], p[(i + 1) % n])) + { + q[N++] = p[i]; + } + cout << "Case " << Tc << ": " << solve() << endl; + } +} diff --git a/10590.cpp b/10590.cpp new file mode 100644 index 0000000..19c7255 --- /dev/null +++ b/10590.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define S 24 + +typedef struct {int a[32], n;} num; + +void set_(num *z, int a) +{ + z->a[0] = a; + z->n = a ? 1 : 0; +} + +void add(num *x, num *y) +{ + int i, c; + while (x->n < y->n) + { + x->a[x->n++] = 0; + } + for (c = i = 0; i < y->n; i++, c >>= S) + { + x->a[i] = (c += x->a[i] + y->a[i]) & ((1 << S) - 1); + } + for (; c && i < x->n; i++, c >>= S) + { + x->a[i] = (c += x->a[i]) & ((1 << S) - 1); + } + if (c) + { + x->a[x->n++] = c; + } +} + +void sub(num *x, num *y) +{ + int i, c; + for (i = c = 0; i < y->n; i++) + if ((x->a[i] -= y->a[i] + c) < 0) + { + x->a[i] += 1 << S; + c = 1; + } + else + { + c = 0; + } + if (c) + { + for (;; i++) + if (x->a[i]--) + { + break; + } + else + { + x->a[i] = (1 << S) - 1; + } + } + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } +} + +void prn(num *x) +{ + static char s[1024]; + int i, r, k; + for (k = 0; x->n > 0;) + { + for (r = 0, i = x->n - 1; i >= 0; i--) + { + r = (r << S) | x->a[i]; + x->a[i] = r / 10; + r %= 10; + } + s[k++] = r + '0'; + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } + } + while (k-- > 0) + { + putchar(s[k]); + } + printf("\n"); +} + +num p[5001]; + +int main() +{ + int n, k, t; + set_(&p[0], 1); + for (n = 1; n <= 5000; n++) + { + set_(&p[n], 0); + for (k = 1; (t = k * (3 * k - 1) / 2) <= n; k++) + { + if (k & 1) + { + add(&p[n], &p[n - t]); + if ((t += k) <= n) + { + add(&p[n], &p[n - t]); + } + } + else + { + sub(&p[n], &p[n - t]); + if ((t += k) <= n) + { + sub(&p[n], &p[n - t]); + } + } + } + } + while (scanf("%d", &n) == 1) + { + prn(&p[n]); + } + return 0; +} diff --git a/10591.cpp b/10591.cpp index fa13c7f..42e392e 100644 --- a/10591.cpp +++ b/10591.cpp @@ -1,32 +1,32 @@ -#include -#include +#include + using namespace std; -int main() { - int T, t; - scanf("%d", &T); - for (t = 1; t <= T; t++) { - int N, M; - scanf("%d", &N); - M = N; - set< int > Set; - while (true) { - Set.insert(M); - int temp = 0; - while (M) { - int R = M % 10; - temp += R * R; - M = M / 10; - } - if (temp == 1) { - printf("Case #%d: %d is a Happy number.\n", t, N); - break; - } - if (Set.find(temp) != Set.end()) { - printf("Case #%d: %d is an Unhappy number.\n", t, N); - break; +int main() +{ + int t, n, tmp; + scanf("%d", &t); + for (int i = 1; i <= t; i++) + { + scanf("%d", &n); + printf("Case #%d: %d is ", i, n); + while (n > 9) + { + tmp = 0; + while (n) + { + tmp += (n % 10) * (n % 10); + n /= 10; } - M = temp; + n = tmp; + } + if (n % 6 == 1) + { + printf("a Happy number.\n"); + } + else + { + printf("an Unhappy number.\n"); } } return 0; diff --git a/10592.cpp b/10592.cpp new file mode 100644 index 0000000..b46b2b3 --- /dev/null +++ b/10592.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +int dirr[4] = {-1, 0, 1, 0}; +int dirc[4] = {0, 1, 0, -1}; + +bool v[51][51]; +char map_[51][51]; +int s[51][51], e[501], p[501][2], seq, total, n; + +void mark(int r, int c, int id) +{ + s[r][c] = id; + for (int i = 0; i < 4; i++) + { + int tr = r + dirr[i], tc = c + dirc[i]; + if (tr < 0 || tr >= n || tc < 0 || tc >= n || map_[tr][tc] == '.' || s[tr][tc]) + { + continue; + } + mark(tr, tc, id); + } +} +void mark1(int r, int c, char ch, int id) +{ + v[r][c] = true; + if (id) + { + s[r][c] = id; + } + for (int i = 0; i < 4; i++) + { + int tr = r + dirr[i], tc = c + dirc[i]; + if (id && tr >= 0 && tr < n && tc >= 0 && tc < n && ch == 'B' && map_[tr][tc] == 'P') + { + e[s[r][c]] = 1; + } + if (tr < 0 || tr >= n || tc < 0 || tc >= n || map_[tr][tc] != ch || v[tr][tc]) + { + continue; + } + mark1(tr, tc, ch, id); + } +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%s", map_[i]); + } + memset(s, 0, sizeof(s)); + memset(p, 0, sizeof(p)); + memset(v, 0, sizeof(v)); + memset(e, 0, sizeof(e)); + seq = total = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (map_[i][j] != '.' && s[i][j] == 0) + { + mark(i, j, ++seq); + } + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (map_[i][j] == 'B' || map_[i][j] == 'P') + { + if (v[i][j]) + { + continue; + } + mark1(i, j, map_[i][j], 0); + if (map_[i][j] == 'B') + { + p[s[i][j]][0]++; + } + else + { + p[s[i][j]][1]++; + } + } + } + } + for (int i = 1; i <= seq; i++) + { + printf("Sector #%d: contain %d freedom fighter group(s) & %d enemy group(s)\n", i, p[i][0], p[i][1]); + } + memset(s, 0, sizeof(s)); + memset(v, 0, sizeof(v)); + seq = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (map_[i][j] == 'B' || map_[i][j] == 'P') + { + if (v[i][j]) + { + continue; + } + if (map_[i][j] == 'B') + { + seq++; + } + mark1(i, j, map_[i][j], seq); + } + } + } + int cnt = 0; + for (int i = 1; i <= seq; i++) + { + cnt += e[i]; + } + printf("Total %d group(s) are in fighting position.\n", cnt * 2); + printf("\n"); + } + return 0; +} diff --git a/10593.cpp b/10593.cpp new file mode 100644 index 0000000..0236698 --- /dev/null +++ b/10593.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define REP(i, e) for (int i = 0; i < (int)(e); i++) +#define FOR(i, b, e) for (int i = (int)(b); i < (int)(e); i++) + +template +void ls(T &a, T b) +{ + if (b < a) + { + a = b; + } +} + +const int N = 512; + +char v[N][N]; +int l[N][N]; +int r[N][N]; +int u[N][N]; +int d[N][N]; + +int square(int n) +{ + int ans = 0; + int k, t; + REP(i, n) + REP(j, n) + { + for (k = 1, t = r[i][j]; t >= k; t = min(t, r[i + k][j]), k++) + { + } + if (k >= 2) + { + ans += k - 2; + } + } + return ans; +} + +int diamond(int n) +{ + int ans = 0; + REP(i, n) + REP(j, n) + { + int t = min(min(l[i][j], r[i][j]), min(u[i][j], d[i][j])), k; + for (k = 1; 0 <= i - k && i + k < n && 0 <= j - k && j + k < n && t > 1; k++) + t = min(t, k - 1 + min(min(min(l[i - k][j], r[i - k][j]), min(l[i + k][j], r[i + k][j])), + min(min(u[i][j - k], d[i][j - k]), min(u[i][j + k], d[i][j + k])))); + if (k >= 2) + { + ans += min(t, k - 1); + } + } + return ans; +} + +int brute_force(int n) +{ + if (n == 500) + { + return 0; + } + int a = 0, b = 0; + REP(i, n) + REP(j, n) FOR(k, 2, n + 1) + { + int cnt = 0; + FOR(ii, i, i + k) + FOR(jj, j, j + k) if (v[ii][jj] == 'x') + { + cnt++; + } + if (cnt == k * k) + { + a++; + } + } + REP(i, n) + REP(j, n) FOR(k, 1, n + 1) + { + FOR(ii, i - k, i + k + 1) + FOR(jj, j - k, j + k + 1) + { + if (!(0 <= ii && ii < n && 0 <= jj && jj < n)) + { + goto bad; + } + if (abs(ii - i) + abs(jj - j) <= k && v[ii][jj] != 'x') + { + goto bad; + } + } + b++; + bad:; + } + return a + b; +} + +int main() +{ + int n; + while (scanf("%d", &n) != EOF) + { + memset(v, '.', sizeof v); + REP(i, n) + scanf("%s", v[i]); + memset(l, 0, sizeof l); + memset(r, 0, sizeof r); + memset(u, 0, sizeof u); + memset(d, 0, sizeof d); + REP(i, n) + { + int cnt; + cnt = 0; + for (int j = n - 1; j >= 0; j--) + { + cnt = (v[i][j] == 'x' ? cnt + 1 : 0); + r[i][j] = cnt; + } + cnt = 0; + for (int j = n - 1; j >= 0; j--) + { + cnt = (v[j][i] == 'x' ? cnt + 1 : 0); + d[j][i] = cnt; + } + cnt = 0; + REP(j, n) + { + cnt = (v[i][j] == 'x' ? cnt + 1 : 0); + l[i][j] = cnt; + } + cnt = 0; + REP(j, n) + { + cnt = (v[j][i] == 'x' ? cnt + 1 : 0); + u[j][i] = cnt; + } + } + cout << square(n) + diamond(n) << endl; + } +} diff --git a/10594.cpp b/10594.cpp new file mode 100644 index 0000000..2499572 --- /dev/null +++ b/10594.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +#define MAXD 110 +#define MAXM 20100 + +const long long int INF = 10000000000000000ll; + +int fst[MAXD], nxt[MAXM], v[MAXM], a[MAXM], b[MAXM], p[MAXD], link[MAXD], q[MAXD], inq[MAXD], N, M, e; +long long int w[MAXM], cost[MAXM], flow[MAXM], d[MAXD], D, K; + +void add(int x, int y, long long int c, long long int f) +{ + v[e] = y; + cost[e] = c; + flow[e] = f; + nxt[e] = fst[x]; + fst[x] = e; + e++; +} +int init() +{ + int i; + if (scanf("%d%d", &N, &M) != 2) + { + return 0; + } + for (i = 0; i < M; i++) + { + scanf("%d%d%lld", &a[i], &b[i], &w[i]); + } + scanf("%lld%lld", &D, &K); + e = 0; + memset(fst, -1, sizeof(fst)); + for (i = 0; i < M; i++) + { + add(a[i], b[i], w[i], K); + add(b[i], a[i], -w[i], 0); + add(b[i], a[i], w[i], K); + add(a[i], b[i], -w[i], 0); + } + add(0, 1, 0, D); + add(1, 0, 0, 0); + return 1; +} +void SPFA() +{ + int i, u, front, rear; + for (i = 1; i <= N; i++) + { + d[i] = INF; + } + d[0] = 0; + memset(inq, 0, sizeof(inq)); + front = rear = 0; + q[rear++] = 0; + inq[0] = 1; + while (front != rear) + { + u = q[front++]; + if (front > N) + { + front = 0; + } + inq[u] = 0; + for (i = fst[u]; i != -1; i = nxt[i]) + { + if (flow[i] && d[u] + cost[i] < d[v[i]]) + { + d[v[i]] = d[u] + cost[i]; + p[v[i]] = u; + link[v[i]] = i; + if (!inq[v[i]]) + { + q[rear++] = v[i]; + if (rear > N) + { + rear = 0; + } + inq[v[i]] = 1; + } + } + } + } +} +long long int mincost() +{ + long long int res = 0, f = 0, a; + int i, u; + for (;;) + { + SPFA(); + if (d[N] == INF) + { + break; + } + a = INF; + for (u = N; u != 0; u = p[u]) + { + int y = link[u]; + if (flow[y] < a) + { + a = flow[y]; + } + } + for (u = N; u != 0; u = p[u]) + { + int y = link[u]; + flow[y] -= a; + flow[y ^ 1] += a; + } + res += a * d[N]; + f += a; + } + if (f == D) + { + return res; + } + else + { + return -1; + } +} + +int main() +{ + while (init()) + { + long long int res = mincost(); + if (res >= 0) + { + printf("%lld\n", res); + } + else + { + printf("Impossible.\n"); + } + } + return 0; +} diff --git a/10595.cpp b/10595.cpp new file mode 100644 index 0000000..c894de3 --- /dev/null +++ b/10595.cpp @@ -0,0 +1,244 @@ +#include + +using namespace std; + +/* +Knight on the Bee Board +10595 +*/ +#define MAX(a, b) (a > b ? a : b) +int L, B, Obs, limiT; +char F[217][217]; +int P[217][217]; +int A[185], Qh, Qt; +int X[] = {2, 1, -1, -2, -3, -3, -2, 3, 3, 2, 1, -1}; +int Y[] = {1, 2, 3, 3, 2, 1, -1, -1, -2, -3, -3, -2}; +struct ss +{ + int x, y; +} pp; +struct tt +{ + int x, y; + int Cost; +} Q[10009]; +struct cor +{ + int x, y; +} co[10001]; +void Ini() +{ + int i, j = 0, sum = 1; + for (i = 1; sum < 10010; i++) + { + sum += j; + A[i] = sum; + j += 6; + } + L = i - 1; +} +ss Side_1(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x; + pp.y = -m; + return pp; +} +ss Side_2(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x - m; + pp.y = y; + return pp; +} +ss Side_3(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x - m; + pp.y = y + m; + return pp; +} +ss Side_4(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x; + pp.y = m + y; + return pp; +} +ss Side_5(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x + m; + pp.y = y; + return pp; +} +ss Side_6(int stp, int x, int y, int T) +{ + int m = stp - T; + pp.x = x + m; + pp.y = y - m; + return pp; +} +int Find(int T) +{ + int i; + for (i = 2; i <= L; i++) + { + if (T <= A[i]) + { + return i; + } + } + return 0; +} +ss Cordinate(int T) +{ + int j, k, N, K; + N = Find(T); + K = A[N]; + j = K; + k = j - N + 1; + if (T <= j && k <= T) + { + return Side_1(j, N - 1, 0, T); + } + j = k; + k = j - N + 1; + if (T <= j && k <= T) + { + return Side_2(j, N - 1, -(N - 1), T); + } + j = k; + k = j - N + 1; + if (T <= j && k <= T) + { + return Side_3(j, 0, -(N - 1), T); + } + j = k; + k = j - N + 1; + if (T <= j && k <= T) + { + return Side_4(j, -(N - 1), 0, T); + } + j = k; + k = j - N + 1; + if (T <= j && k <= T) + { + return Side_5(j, -(N - 1), N - 1, T); + } + j = k; + k = j - N + 1; + return Side_6(j, 0, N - 1, T); +} +void Push(int x, int y, int Co) +{ + Q[Qh].x = x; + Q[Qh].Cost = Co; + Q[Qh++].y = y; + Qh %= 10009; +} +tt Pop() +{ + tt temp; + temp = Q[Qt++]; + Qt %= 10009; + return temp; +} +int IsEmpty() +{ + return Qh == Qt; +} +int Bfs(int st, int teR) +{ + int i, d, c; + tt temp; + for (i = 0; i <= B; i++) + { + Q[i].Cost = 0; + } + Qh = Qt = 0; + F[co[st].x + 100][co[st].y + 100] = 1; + Push(co[st].x, co[st].y, 0); + while (!IsEmpty()) + { + temp = Pop(); + for (i = 0; i < 12; i++) + { + c = temp.x + X[i]; + d = temp.y + Y[i]; + if (P[c + 100][d + 100] > B) + { + continue; + } + if (P[c + 100][d + 100] == teR) + { + return temp.Cost + 1; + } + if (F[c + 100][d + 100] == 0) + { + F[c + 100][d + 100] = 1; + Push(c, d, temp.Cost + 1); + } + } + } + return -1; +} +void Set() +{ + ss p; + int i, x, y; + for (i = 2; i <= 10000; i++) + { + p = Cordinate(i); + x = p.x + 100; + y = p.y + 100; + P[x][y] = i; + co[i].x = p.x; + co[i].y = p.y; + } +} +void Cal() +{ + int i, st, des, m; + for (i = 0; i < Obs; i++) + { + scanf("%d", &m); + F[co[m].x + 100][co[m].y + 100] = 1; + } + scanf("%d%d", &st, &des); + if (st == des) + { + printf("0\n"); + return; + } + if (F[co[des].x + 100][co[des].y + 100] == 1) + { + printf("Impossible.\n"); + return; + } + m = Bfs(st, des); + if (m < 0) + { + printf("Impossible.\n"); + } + else + { + printf("%d\n", m); + } +} +int main() +{ + int i, j; + Ini(); + Set(); + while (scanf("%d%d", &B, &Obs) == 2) + { + Cal(); + for (i = 0; i <= 215; i++) + for (j = 0; j <= 215; j++) + { + F[i][j] = 0; + } + } + return 0; +} diff --git a/10596.cpp b/10596.cpp new file mode 100644 index 0000000..78b3a83 --- /dev/null +++ b/10596.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +/** + to check whether an undirected graph has an Eulerian tour or not, + necessary and sufficient conditions for an undirected graph: + an undirected graph has a closed Euler tour if it is connected, + and each vertex has an even degree. + use dfs to count # component +**/ + +bool check_deg(int n, const vector °s) +{ + for (int i = 0; i < n; i++) + { + if (degs[i] & 1)// vetex i ~ odd deg + { + return false; + } + } + return true; +} + +void dfs(int n, int i, const vector> &graph, vector &visited) +{ + visited[i] = true; + const vector &g = graph[i]; + for (int j = 0; j < n; j++) + { + if (g[j] && !visited[j]) + { + dfs(n, j, graph, visited); + } + } +} + +bool check_conn(int n, const vector °s, const vector> &graph) +{ + // dfs to see if all vertices are connected + vector visited(n, false); + int start; + for (start = 0; start < n; start++) + { + if (degs[start]) + { + break; + } + } + if (start < n) + { + dfs(n, start, graph, visited); + } + for (int i = 0; i < n; i++) + { + if (degs[i]) + { + if (!visited[i]) + { + return false; + } + } + } + return true; +} + +int main() +{ + int n, r; + while (scanf("%d%d", &n, &r) != EOF) + { + vector degs(n, 0); + vector> graph(n, vector(n, false)); + for (int i = 0; i < r; i++) + { + int c1, c2; + scanf("%d%d", &c1, &c2); + degs[c1]++; + degs[c2]++; + graph[c1][c2] = graph[c2][c1] = true; + } + if (r && check_deg(n, degs) && check_conn(n, degs, graph)) + { + printf("Possible\n"); + } + else + { + printf("Not Possible\n"); + } + } + return 0; +} diff --git a/10597.cpp b/10597.cpp new file mode 100644 index 0000000..3d6ccbe --- /dev/null +++ b/10597.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 50; +const int M = 26; +const int K = 255; + +int vis[M][N][N]; +vector edge[M]; +char cangene[M][K]; + +int solve(int ch, int l, int r, string &in) +{ + if (r == l) + { + return cangene[ch][in[l]]; + } + int &ret = vis[ch][l][r]; + if (ret == -1) + { + ret = 0; + rep(i, (int)edge[ch].size()) + { + int fir = edge[ch][i][0] - 'A', sec = edge[ch][i][1] - 'A'; + REP(i, l, r) + { + int a = solve(fir, l, i, in), b = solve(sec, i + 1, r, in); + if (a + b == 2) + { + ret = 1; + return ret; + } + } + } + } + return ret; +} + +int main() +{ + string in; + while (cin >> in && in != "#") + { + int st = in[0] - 'A'; + rep(i, M) rep(j, K) cangene[i][j] = false; + rep(i, M) edge[i].clear(); + string a, b; + cin >> a >> b; + string f, t, mid; + while (cin >> f >> mid >> t && f != "#") + { + if (t.size() == 1) + { + cangene[f[0] - 'A'][t[0]] = true; + } + else + { + edge[f[0] - 'A'].push_back(t); + } + } + string tar; + while (cin >> tar && tar != "#") + { + rep(i, M) rep(j, N) rep(k, N) vis[i][j][k] = -1; + int ans = solve(st, 0, tar.size() - 1, tar); + cout << tar; + if (ans == 0) + { + cout << " is not in L(G)" << endl; + } + else + { + cout << " is in L(G)" << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/10598.cpp b/10598.cpp new file mode 100644 index 0000000..5458e91 --- /dev/null +++ b/10598.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +const double pi = 2 * acos(0), k = 180.0 / pi; +double r, n; +int main() +{ + int caseno = 1; + while (scanf("%lf %lf", &r, &n) == 2) + { + if (!r && !n) + { + break; + } + printf("Case %d:\n", caseno++); + for (int i = 1; i <= 10; i++) + { + double t = n / r, s = acos((n / 2 / pi / r / i)); + printf("%.5lf\n", (s - t) * k); + } + } +} diff --git a/10599.cpp b/10599.cpp new file mode 100644 index 0000000..ed55a22 --- /dev/null +++ b/10599.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +#define MAXN 10002 + +typedef long long LL; + +struct ss {int r, c, p;}; +ss pt[MAXN]; + +struct node +{ + int r, c, ad; + LL count; +}; + +struct lis +{ + node A[1000]; + int ind; +} L[202]; + +int total, R, C, max_, chk; + +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + if (x->c != y->c) + { + return x->c - y->c; + } + return x->r - y->r; +} +void Print(int n) +{ + if (pt[n].p == -1) + { + printf("%d", (pt[n].r - 1) * C + pt[n].c); + chk++; + return; + } + Print(pt[n].p); + printf(" %d", (pt[n].r - 1) * C + pt[n].c); + chk++; +} +void Cal() +{ + int i, f, j, l; + LL times; + max_ = 0; + if (total == 1) + { + printf("1 1 %d\n", (pt[0].r - 1) * C + pt[0].c); + return; + } + if (total == 0) + { + printf("0 0\n"); + return; + } + L[0].A[0].r = pt[0].r; + L[0].A[0].c = pt[0].c; + L[0].A[0].ad = 0; + L[0].A[0].count = 1; + L[0].ind = 1; + pt[0].p = -1; + for (i = 1; i < total; i++) + { + f = 0; + times = 0; + for (j = max_; j >= 0; j--) + { + for (int k = L[j].ind - 1; k >= 0; k--) + { + if (pt[i].r >= L[j].A[k].r && pt[i].c >= L[j].A[k].c) + { + f = 1; + times += L[j].A[k].count; + l = L[j].A[k].ad; + } + } + if (f) + { + L[j + 1].A[L[j + 1].ind].r = pt[i].r; + L[j + 1].A[L[j + 1].ind].c = pt[i].c; + L[j + 1].A[L[j + 1].ind].count = times; + L[j + 1].A[L[j + 1].ind++].ad = i; + pt[i].p = l; + if (j + 1 > max_) + max_ = j + 1; + break; + } + } + if (!f) + { + L[0].A[L[0].ind].r = pt[i].r; + L[0].A[L[0].ind].c = pt[i].c; + L[0].A[L[0].ind].count = 1; + L[0].A[L[0].ind++].ad = i; + pt[i].p = -1; + } + } + times = 0; + for (i = 0; i < L[max_].ind; i++) + times += L[max_].A[i].count; + printf("%d %lld ", max_ + 1, times); + Print(L[max_].A[L[max_].ind - 1].ad); + printf("\n"); +} +void ReSet() +{ + int i; + for (i = 0; i <= max_ + 1; i++) + L[i].ind = 0; +} + +int main() +{ + int k = 1; + while (scanf("%d%d", &R, &C) == 2) + { + if (R == -1 && C == -1) + break; + total = 0; + while (1) + { + scanf("%d%d", &pt[total].r, &pt[total].c); + if (!pt[total].r && !pt[total].c) + break; + total++; + } + qsort(pt, total, sizeof(ss), com); + printf("CASE#%d: ", k++); + Cal(); + ReSet(); + } + return 0; +} diff --git a/106.cpp b/106.cpp index ba63778..81e446b 100644 --- a/106.cpp +++ b/106.cpp @@ -1,55 +1,74 @@ -/* -106 -Fermat Vs Pythagoras -*/ - -#include -#include -#include -#define min(a, b) (a + +using namespace std; + +template +inline T gcd(T a, T b) +{ + T c; + while (b) + { + c = b, b = a % b, a = c; + } + return a; +} + +int X[1000005] = {0}, Y[1000005] = {0}, M[1000005]; + +int main() +{ + int i, j, k, sum = 0, a, b, c, n; + for (i = 0; i < 1000005; i++) + { + M[i] = 2000000; + } + for (i = 1; i < 1001; i++) + { + for (j = 1; j < i; j++) + { + if (i * i + j * j > 1000000) + { + break; + } + a = i * i - j * j; + b = 2 * i * j; + c = i * i + j * j; + if (gcd(a, gcd(b, c)) == 1) + { + X[c]++; + for (k = 1; c * k <= 1000000; k++) + { + if (c * k < M[a * k]) + { + M[a * k] = c * k; + } + if (c * k < M[b * k]) + { + M[b * k] = c * k; + } + if (c * k < M[c * k]) + { + M[c * k] = c * k; + } + } + } + } + } + for (i = 0; i < 1000005; i++) + { + if (M[i] != 2000000) + { + Y[M[i]]++; + } + } + for (i = 1; i < 1000005; i++) + { + X[i] += X[i - 1]; + Y[i] += Y[i - 1]; + } + while (scanf("%d", &n) == 1) + { + printf("%d %d\n", X[n], n - Y[n]); + } + return 0; +} diff --git a/1060.cpp b/1060.cpp new file mode 100644 index 0000000..87e6594 --- /dev/null +++ b/1060.cpp @@ -0,0 +1,231 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double INF = 1e30; + +int dcmp(double x) +{ + if (x < -eps) + return -1; + return x > eps; +} + +struct point +{ + double x, y; + point(double _x = 0, double _y = 0) : x(_x), y(_y) {} + double len() + { + return sqrt(x * x + y * y); + } + point nor() + { + double L = len(); + return point(x / L, y / L); + } + bool operator<(const point r) const + { + if (dcmp(x - r.x)) + return x < r.x; + return y < r.y; + } +} p[1000], O; + +int N; +double v1, v2; + +double cross(point &k, point &a, point &b) +{ + return (a.x - k.x) * (b.y - k.y) - (a.y - k.y) * (b.x - k.x); +} + +double dot(point &k, point &a, point &b) +{ + return (a.x - k.x) * (b.x - k.x) + (a.y - k.y) * (b.y - k.y); +} + +bool onSeg(point k, point p1, point p2) +{ + return dcmp(cross(k, p1, p2)) == 0 && + dcmp(dot(k, p1, p2)) <= 0; +} + +bool inPoly(point cp) +{ + int i, j, k, d1, d2, w = 0; + for (i = 0; i < N; ++i) + { + if (onSeg(cp, p[i], p[i + 1])) + return 0; + k = dcmp(cross(p[i], p[i + 1], cp)); + d1 = dcmp(p[i].y - cp.y); + d2 = dcmp(p[i + 1].y - cp.y); + if (k > 0 && d1 <= 0 && d2 > 0) + w++; + if (k < 0 && d2 <= 0 && d1 > 0) + w--; + } + return w != 0; +} + +int inter(point &a1, point &a2, point &b1, point &b2, point &k) +{ + double u = cross(a1, a2, b1), v = cross(a2, a1, b2); + if (dcmp(u + v)) + { + k = point((b1.x * v + b2.x * u) / (v + u), (b1.y * v + b2.y * u) / (v + u)); + return 1; + } + if (dcmp(u) || dcmp(cross(b1, b2, a1))) + return 0; + return -1; +} + +double dis(point &a, point &b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +point tmp[1000]; +bool ok(point a, point b) +{ + point k; + int i, d, e = 0; + tmp[e++] = a, tmp[e++] = b; + for (i = 0; i < N; ++i) + { + d = inter(a, b, p[i], p[i + 1], k); + if (d == 1 && dcmp(dot(k, a, b)) <= 0) + tmp[e++] = k; + } + sort(tmp, tmp + e); + for (i = 1; i < e; ++i) + if (inPoly(point((tmp[i - 1].x + tmp[i].x) / 2, (tmp[i - 1].y + tmp[i].y) / 2))) + return 0; + + return 1; +} + +double d[200][200]; +double len[1000], per; +void init() +{ + per = 0; + p[N] = p[0]; + p[N + 1] = O; + for (int i = 0; i < N; ++i) + { + len[i] = dis(p[i], p[i + 1]); + per += len[i]; + } + for (int i = 1; i <= N + 1; ++i) + { + d[i][i] = 0; + for (int j = i + 1; j <= N + 1; ++j) + { + if (ok(p[i], p[j])) + d[i][j] = d[j][i] = dis(p[i], p[j]); + else + d[i][j] = d[j][i] = INF; + } + } +} + +bool vis[200]; +double h[200]; +bool chk(double tim) +{ + double S = fmod(tim * v1, per); + point t; + int i, j, k; + + for (i = 0; i < N; ++i) + { + if (len[i] < S) + S -= len[i]; + else + { + t = point(p[i + 1].x - p[i].x, p[i + 1].y - p[i].y).nor(); + p[N + 2] = point(p[i].x + t.x * S, p[i].y + t.y * S); + break; + } + } + + d[N + 2][N + 2] = 0; + for (i = 1; i <= N + 1; ++i) + { + if (ok(p[i], p[N + 2])) + d[i][N + 2] = d[N + 2][i] = dis(p[i], p[N + 2]); + else + d[i][N + 2] = d[N + 2][i] = INF; + } + + if (d[N + 2][N + 1] > 1e28) + { + for (i = 1; i <= N + 2; ++i) + { + vis[i] = 0; + h[i] = INF; + } + h[N + 2] = 0; + + while (1) + { + S = INF, j = -1; + for (i = 1; i <= N + 2; ++i) + if (!vis[i]) + { + if (h[i] < S) + S = h[i], j = i; + } + + if (j == N + 1) + break; + + vis[j] = 1; + for (i = 1; i <= N + 2; ++i) + if (!vis[i]) + h[i] = min(h[i], h[j] + d[j][i]); + } + } + else + h[N + 1] = d[N + 2][N + 1]; + + return h[N + 1] / v2 <= tim; +} + +int main() +{ + int cases = 1; + int i, j, k; + double ll, rr, mid, t; + + while (scanf("%d", &N), N) + { + for (i = 0; i < N; ++i) + scanf("%lf %lf", &p[i].x, &p[i].y); + scanf("%lf %lf", &O.x, &O.y); + scanf("%lf %lf", &v1, &v2); + + init(); + + ll = 0, rr = 1e8; + while (rr - ll > eps) + { + mid = (ll + rr) / 2; + if (chk(mid)) + rr = mid; + else + ll = mid; + } + + chk(5); + mid = (ll + rr) * 30; + k = (int)(mid + 0.5 + eps); + printf("Case %d: Time = %d:%02d\n", cases++, k / 60, k % 60); + } + + return 0; +} diff --git a/10600.cpp b/10600.cpp new file mode 100644 index 0000000..70732cc --- /dev/null +++ b/10600.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define MAXN 102 +#define MAXE 5055 + +struct ss {int u, v, weight;} E[MAXE], MST[MAXE]; +int P[MAXN], rank_[MAXN], N, M; + +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return x->weight - y->weight; +} +int Find(int n) +{ + if (n != P[n]) + { + P[n] = Find(P[n]); + } + return P[n]; +} +void Link(int x, int y) +{ + if (rank_[x] > rank_[y]) + P[y] = x; + else + { + P[x] = y; + if (rank_[x] == rank_[y]) + { + rank_[y]++; + } + } +} +void MakeSet() +{ + int i; + for (i = 0; i < N; i++) + { + rank_[i] = 0; + P[i] = i; + } +} +int mSt(int p, int c) +{ + int sum = 0, i, x, y; + int mstLen = 0; + for (i = 0; i < M && mstLen < N - 1; i++) + { + if ((E[i].u == MST[p].u && E[i].v == MST[p].v)) + { + continue; + } + x = Find(E[i].u); + y = Find(E[i].v); + if (x != y) + { + Link(x, y); + sum += E[i].weight; + if (sum > c) + { + return -1; + } + mstLen++; + } + } + if (mstLen == N - 1) + { + return sum; + } + return -1; +} +void Cal() +{ + int i, mstLen = 0, x, y, f = 0; + int Best_1 = 0, Best_2 = 21474836; + MakeSet(); + qsort(E, M, sizeof(ss), com); + for (i = 0; i < M && mstLen < N - 1; i++) + { + x = Find(E[i].u); + y = Find(E[i].v); + if (x != y) + { + MST[mstLen++] = E[i]; + Link(x, y); + Best_1 += E[i].weight; + } + } + printf("%d", Best_1); + for (i = 0; i < mstLen; i++) + { + MakeSet(); + x = mSt(i, Best_2); + if (x != -1 && x < Best_2) + { + f = 1; + Best_2 = x; + } + } + if (f) + { + printf(" %d\n", Best_2); + } + else + { + printf(" %d\n", Best_1); + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &M); + for (int i = 0; i < M; i++) + { + scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].weight); + E[i].u--; + E[i].v--; + } + Cal(); + } + return 0; +} diff --git a/10601.cpp b/10601.cpp new file mode 100644 index 0000000..362248a --- /dev/null +++ b/10601.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +int f[13], t[7], s, k[7]; +void DFS(int depth, int sum) +{ + if (depth == 7 && sum == 2) + { + int temp = f[2], temp1 = f[5]; + for (int i = 1; i <= 6; i++) + { + temp /= f[k[i]], temp1 /= f[(t[i] - k[i]) / 2]; + } + s += temp * temp1; + } + if (depth == 7) + { + return; + } + for (int i = 0; i <= t[depth]; i++) + if (sum + i <= 2 && (t[depth] - i) % 2 == 0) + { + k[depth] = i; + DFS(depth + 1, i + sum); + } + return; +} +int main() +{ + f[0] = 1; + for (int i = 1; i < 13; i++) + { + f[i] = f[i - 1] * i; + } + int times; + scanf("%d", ×); + while (times--) + { + for (int i = 1; i <= 6; i++) + { + t[i] = 0; + } + for (int i = 1; i < 13; i++) + { + int p; + scanf("%d", &p); + t[p]++; + } + int sum = 0, temp = f[12]; + for (int i = 1; i <= 6; i++) + { + temp /= f[t[i]]; + } + sum += temp; + bool flag = true; + for (int i = 1; i <= 6; i++) + if (t[i] % 4) + { + flag = false; + } + if (flag) + { + temp = 6 * f[3]; + for (int i = 1; i <= 6; i++) + { + temp /= f[t[i] / 4]; + } + sum += temp; + } + else + { + flag = true; + } + for (int i = 1; i <= 6; i++) + if (t[i] % 2) + { + flag = false; + } + if (flag) + { + temp = 3 * f[6]; + for (int i = 1; i <= 6; i++) + { + temp /= f[t[i] / 2]; + } + sum += temp; + } + else + { + flag = true; + } + for (int i = 1; i <= 6; i++) + if (t[i] % 3) + { + flag = false; + } + if (flag) + { + temp = 8 * f[4]; + for (int i = 1; i <= 6; i++) + { + temp /= f[t[i] / 3]; + } + sum += temp; + } + else + { + flag = true; + } + s = 0; + DFS(1, 0); + sum += 6 * s; + printf("%d\n", sum / 24); + } +} diff --git a/10602.cpp b/10602.cpp new file mode 100644 index 0000000..3326b01 --- /dev/null +++ b/10602.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +/* +10602 +Editor Nottabad +*/ +#define MIN(a, b) (a > b ? b : a) +struct ss +{ + char str[102]; + int len; + int pre; +} ww[102]; +int N, req; +char Fix[102]; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + if (y->pre != x->pre) + { + return y->pre - x->pre; + } + return strcmp(x->str, y->str); +} +void Count_prefix() +{ + int i, j, c, len, fl; + fl = strlen(Fix); + for (i = 0; i < N - 1; i++) + { + c = 0; + len = MIN(fl, ww[i].len); + for (j = 0; j < len; j++) + { + if (Fix[j] == ww[i].str[j]) + { + c++; + } + else + { + break; + } + } + ww[i].pre = c; + } +} +void Count_req() +{ + int i, j, st, lim, c = 0; + req = ww[0].len - ww[0].pre; + for (i = 1; i + 1 < N; i++) + { + lim = MIN(ww[i].len, ww[i - 1].len); + st = MIN(ww[i].pre, ww[i - 1].pre); + c = 0; + for (j = st; j < lim && (ww[i].str[j] == ww[i - 1].str[j]); j++) + { + c++; + } + req += ww[i].len - c - st; + } +} +void Cal() +{ + Count_prefix(); + qsort(ww, N - 1, sizeof(ww[0]), com); + Count_req(); + printf("%d\n", req + strlen(Fix)); + printf("%s\n", Fix); + for (int i = 0; i + 1 < N; i++) + { + puts(ww[i].str); + } +} +int main() +{ + int kases, i; + scanf("%d", &kases); + while (kases--) + { + scanf("%d", &N); + scanf("%s", Fix); + for (i = 1; i < N; i++) + { + scanf("%s", ww[i - 1].str); + ww[i - 1].len = strlen(ww[i - 1].str); + } + Cal(); + } + return 0; +} diff --git a/10603.cpp b/10603.cpp new file mode 100644 index 0000000..3b68b2e --- /dev/null +++ b/10603.cpp @@ -0,0 +1,272 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a) +#define MAXN 1000000// may have to increase + +typedef short st; + +map M; + +struct ss +{ + short a, b, c; + int pour; +} Q[MAXN]; +int qh, qt, mindiff, minpour, ca, cb, cc, target; + +void Push(st a, st b, st c, int pour) +{ + Q[qh].a = a; + Q[qh].a = a; + Q[qh].b = b; + Q[qh].c = c; + Q[qh++].pour = pour; + qh %= MAXN; +} +ss Pop() +{ + ss t; + t.a = Q[qt].a; + t.b = Q[qt].b; + t.c = Q[qt].c; + t.pour = Q[qt].pour; + qt++; + qt %= MAXN; + return t; +} +void Diff(int a, int b, int c, int np) +{ + int diff; + diff = target - a; + if (diff >= 0 && diff <= mindiff) + { + if (diff < mindiff) + { + mindiff = diff; + minpour = np; + } + else if (diff == mindiff && np < minpour) + minpour = np; + } + diff = target - b; + if (diff >= 0 && diff <= mindiff) + { + if (diff < mindiff) + { + mindiff = diff; + minpour = np; + } + else if (diff == mindiff && np < minpour) + minpour = np; + } + diff = target - c; + if (diff >= 0 && diff <= mindiff) + { + if (diff < mindiff) + { + mindiff = diff; + minpour = np; + } + else if (diff == mindiff && np < minpour) + minpour = np; + } +} +void Pusha(int a, int b, int c, int pour) +{ + int d, ta, tb, tc, val, flag, np; + if (!a) + return; + if (b < cb)//ok + { + d = MIN(cb - b, a); + np = d + pour; + ta = a - d; + tb = b + d; + val = ta * 40401 + 201 * tb + c; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(ta, tb, c, np); + M[val] = np; + } + } + else + { + Push(ta, tb, c, np); + M[val] = np; + } + Diff(ta, tb, c, np); + } + if (c < cc)// ok + { + d = MIN(cc - c, a); + np = d + pour; + ta = a - d; + tc = c + d; + val = ta * 40401 + 201 * b + tc; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(ta, b, tc, np); + M[val] = np; + } + } + else + { + Push(ta, b, tc, np); + M[val] = np; + } + Diff(ta, b, tc, np); + } +} +void Pushb(int a, int b, int c, int pour) +{ + int d, ta, tb, tc, val, flag, np; + if (!b) + return; + if (a < ca)//ok + { + d = MIN(ca - a, b); + np = d + pour; + ta = a + d; + tb = b - d; + val = ta * 40401 + 201 * tb + c; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(ta, tb, c, np); + M[val] = np; + } + } + else + { + Push(ta, tb, c, np); + M[val] = np; + } + Diff(ta, tb, c, np); + } + if (c < cc)//ok + { + d = MIN(cc - c, b); + np = d + pour; + tb = b - d; + tc = c + d; + val = a * 40401 + 201 * tb + tc; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(a, tb, tc, np); + M[val] = np; + } + } + else + { + Push(a, tb, tc, np); + M[val] = np; + } + Diff(a, tb, tc, np); + } +} +void Pushc(int a, int b, int c, int pour) +{ + int d, ta, tb, tc, val, flag, np; + if (!c) + return; + if (a < ca)//ok + { + d = MIN(ca - a, c); + np = d + pour; + ta = a + d; + tc = c - d; + val = ta * 40401 + 201 * b + tc; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(ta, b, tc, np); + M[val] = np; + } + } + else + { + Push(ta, b, tc, np); + M[val] = np; + } + Diff(ta, b, tc, np); + } + if (b < cb)//ok + { + d = MIN(cb - b, c); + np = d + pour; + tc = c - d; + tb = b + d; + val = a * 40401 + 201 * tb + tc; + flag = M[val]; + if (flag) + { + if (flag > np) + { + Push(a, tb, tc, np); + M[val] = np; + } + } + else + { + Push(a, tb, tc, np); + M[val] = np; + } + Diff(a, tb, tc, np); + } +} +void BFS() +{ + ss p; + qh = qt = 0; + Push(0, 0, cc, 0); + while (qh != qt) + { + p = Pop(); + Pusha(p.a, p.b, p.c, p.pour); + Pushb(p.a, p.b, p.c, p.pour); + Pushc(p.a, p.b, p.c, p.pour); + } +} +void Cal() +{ + mindiff = target; + minpour = 0; + if (cc <= target) + { + printf("%d %d\n", 0, cc); + return; + } + BFS(); + printf("%d %d\n", minpour, target - mindiff); +} + +int main() +{ + int kases; + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d%d%d", &ca, &cb, &cc, &target); + Cal(); + if (kases) + { + M.clear(); + } + } + return 0; +} diff --git a/10604.cpp b/10604.cpp new file mode 100644 index 0000000..2989165 --- /dev/null +++ b/10604.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +typedef long bbg; +bbg B[11], Temp[11][11], Pro[11][11], V[1679619], N, C, ind; +char Fg[1679619]; +int S[1679619]; + +int Num(string ss, int l) +{ + int i, j, val = 0, p = 1; + for (i = 0; i < l; i++) + { + j = ss[i] - '0'; + val += (j - 1) * p; + p *= 6; + } + return val; +} +bbg Push_All(string ss) +{ + bbg i, j, k, l, n, m, v, d, temp, temp1; + bbg min = 2147483647; + string tt, xx; + l = ss.length(); + if (l == 2) + { + n = ss[0] - '0'; + m = ss[1] - '0'; + temp = Temp[n][m]; + temp1 = Temp[n][m]; + if (temp > temp1) + temp = temp1; + return temp; + } + if (l <= 8) + { + v = Num(ss, l); + if (Fg[v] == 1) + return V[v]; + Fg[v] = 1; + S[ind++] = v; + } + for (i = 0; i < l; i++) + { + n = ss[i] - '0'; + for (j = 0; j < l; j++) + { + if (i == j) + continue; + m = ss[j] - '0'; + xx = ""; + tt = ""; + xx += Pro[n][m] + '0'; + for (k = 0; k < l; k++) + { + if (k == i || k == j) + continue; + tt += ss[k]; + } + tt += xx; + sort(tt.begin(), tt.end()); + d = Push_All(tt) + Temp[n][m]; + if (min > d) + min = d; + } + } + if (l <= 8) + V[v] = min; + return min; +} +void Cal()//ok +{ + int i; + ind = 0; + string ss = ""; + for (i = 0; i < N; i++) + ss += B[i] + '0'; + sort(ss.begin(), ss.end()); + cout << Push_All(ss) << endl; +} +void Free() +{ + int i, j; + for (i = 0; i < ind; i++) + { + j = S[i]; + Fg[j] = 0; + V[j] = 0; + } +} + +int main() +{ + int i, ks, j; + char ss[100]; + cin >> ks; + while (ks--) + { + cin >> C; + for (i = 1; i <= C; i++) + for (j = 1; j <= C; j++) + cin >> Pro[i][j] >> Temp[i][j]; + cin >> N; + for (i = 0; i < N; i++) + cin >> B[i]; + Cal(); + Free(); + cin >> ss; + if (!strcmp(ss, ".")) + break; + } + return 0; +} diff --git a/10605.cpp b/10605.cpp new file mode 100644 index 0000000..de402d2 --- /dev/null +++ b/10605.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +char m[16][16]; +class state +{ +public: + int r, c; + bool m[11][11]; + state(){}; + state(int tr, int tc) + { + r = tr; + c = tc; + rep(i, r) rep(j, c) m[i][j] = false; + } +}; + +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +int out[] = {1, 0, 3, 2}; + +int ans; + +void solve(int r, int c, int n, int *mx, int *my, int cost, + int now, int x, int y, state &ins, bool flag, int prev, int rem) +{ + if (cost >= ans) + { + return; + } + if (cost + rem >= ans) + { + return; + } + if (n == now) + { + ans = cost; + return; + } + bool tmp = false; + if (flag) + { + if (ins.m[my[now]][mx[now]] == true) + { + solve(r, c, n, mx, my, cost, now + 1, -1, -1, ins, true, -1, rem); + return; + } + else + { + x = mx[now], y = my[now], cost++, ins.m[y][x] = true, tmp = true, rem--, m[y][x] = -1; + } + } + int ok = 0; + rep(i, 4) + { + int nex = x + dx[i], ney = y + dy[i]; + if (out[i] == prev) + { + continue; + } + if (m[ney][nex] == '#') + { + ok = (ok + (1 << i)); + } + } + rep(i, 4) + { + int nex = x + dx[i], ney = y + dy[i]; + if (out[i] == prev) + { + continue; + } + if (ok == 0) + ; + else if ((ok & (1 << i)) == 0) + { + continue; + } + if (m[ney][nex] == '#') + { + solve(r, c, n, mx, my, cost, now + 1, -1, -1, ins, true, -1, rem); + } + else if (ins.m[ney][nex] == true && m[ney][nex] == -1 && (ney != my[now] || nex != mx[now])) + { + char ff = m[ney][nex]; + m[ney][nex] = now; + solve(r, c, n, mx, my, cost, now + 1, -1, -1, ins, true, -1, rem); + m[ney][nex] = ff; + } + else if (ins.m[ney][nex] == false) + { + ins.m[ney][nex] = true; + char ff = m[ney][nex]; + m[ney][nex] = now; + if (ff == '*') + { + solve(r, c, n, mx, my, cost + 1, now, nex, ney, ins, false, i, rem - 1); + } + else + { + solve(r, c, n, mx, my, cost + 1, now, nex, ney, ins, false, i, rem); + } + m[ney][nex] = ff; + ins.m[ney][nex] = false; + } + } + if (tmp) + { + ins.m[y][x] = false, m[y][x] = '*'; + } +} + +main() +{ + int te = 0; + cin >> te; + while (te--) + { + int r, c, mx[20], my[20], index = 0; + ans = 25; + cin >> r >> c; + state ins(r, c); + rep(i, r) cin >> m[i]; + rep(i, r) + { + rep(j, c) + { + if (m[i][j] == '*') + { + mx[index] = j; + my[index++] = i; + } + } + } + solve(r, c, index, mx, my, 0, 0, -1, -1, ins, true, -1, index); + cout << ans << endl; + } +} diff --git a/10606.cpp b/10606.cpp new file mode 100644 index 0000000..b7f2178 --- /dev/null +++ b/10606.cpp @@ -0,0 +1,454 @@ +#include + +using namespace std; + +typedef struct {unsigned n, a[16];} num_t; +typedef unsigned long long u64; + +void sqr(num_t *z, num_t *x) +{ + u64 c; + unsigned i, j, k; + z->n = x->n * 2 + 1; + for (i = 0; i < z->n; i++) + { + z->a[i] = 0; + } + for (i = 0; i < x->n; i++) + { + for (j = 0, k = i, c = 0; j < x->n; j++, k++, c >>= 32) + { + c += (u64)z->a[k] + (u64)x->a[i] * (u64)x->a[j]; + z->a[k] = (unsigned)c; + } + for (; c != 0; k++, c >>= 32) + { + z->a[k] = (unsigned)(c += (u64)z->a[k]); + } + } + while (z->n > 0 && z->a[z->n - 1] == 0) + { + z->n--; + } +} + +unsigned isqrt(unsigned a) +{ + unsigned x, y, i; + for (x = 0, i = 16; i--;) + { + y = x | (1 << i); + if ((y * y) <= a) + { + x = y; + } + } + return x; +} + +void copy(num_t *x, num_t *y) +{ + x->n = y->n; + memcpy(x->a, y->a, y->n * sizeof(y->a[0])); +} + +void muladd(num_t *x, unsigned m, unsigned c0) +{ + unsigned i; + u64 c; + for (i = 0, c = c0; i < x->n; i++, c >>= 32) + { + c += (u64)x->a[i] * (u64)m; + x->a[i] = (unsigned)c; + } + if (c != 0) + { + x->a[x->n++] = (unsigned)c; + } +} + +void set_(num_t *x, char *s) +{ + unsigned a, m; + while (*s == '0') + { + s++; + } + for (x->n = 0, a = 0, m = 1; *s;) + { + a = a * 10 + *s++ - '0'; + m *= 10; + if (m == 1000000000) + { + muladd(x, m, a); + m = 1; + a = 0; + } + } + if (m > 1) + { + muladd(x, m, a); + } +} + +const char *fmt(num_t *x) +{ + static char buf[4][4096]; + static int Z = 0; + static num_t t; + char *s; + u64 r; + unsigned i; + copy(&t, x); + Z = (Z + 1) & 3; + for (*(s = buf[Z] + 4094) = '\0'; t.n > 0;) + { + for (i = t.n, r = 0; i--;) + { + r = (r << 32) | (u64)t.a[i]; + t.a[i] = r / 1000000000ULL; + r %= 1000000000ULL; + } + for (i = 0; i < 9; i++, r /= 10) + { + *--s = (unsigned)(r % 10) + '0'; + } + while (t.n > 0 && t.a[t.n - 1] == 0) + { + t.n--; + } + } + while (*s == '0') + { + s++; + } + return (*s == '\0') ? "0" : s; +} + +int cmp(num_t *x, num_t *y) +{ + unsigned i; + if (x->n != y->n) + { + return (x->n > y->n) ? 1 : -1; + } + for (i = x->n; i--;) + if (x->a[i] != y->a[i]) + { + return (x->a[i] > y->a[i]) ? 1 : -1; + } + return 0; +} + +void add(num_t *x, num_t *y) +{ + unsigned i; + u64 c; + while (x->n < y->n) + { + x->a[x->n++] = 0; + } + for (i = 0, c = 0; i < y->n; i++, c >>= 32) + { + c += (u64)x->a[i] + (u64)y->a[i]; + x->a[i] = (unsigned)c; + } + for (; c > 0 && i < x->n; i++, c >>= 32) + { + c += (u64)x->a[i]; + x->a[i] = (unsigned)c; + } + if (c > 0) + { + x->a[x->n++] = (unsigned)c; + } +} + +void dec(num_t *x) +{ + int i; + for (i = 0;; i++) + { + if (x->a[i] == 0) + { + x->a[i] = 0xFFFFFFFF; + } + else + { + x->a[i]--; + break; + } + } + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } +} + +void lshift(num_t *x, unsigned s) +{ + unsigned i; + u64 c; + for (i = 0, c = 0; i < x->n; i++, c >>= 32) + { + c |= ((u64)x->a[i]) << s; + x->a[i] = (unsigned)c; + } + if (c != 0) + { + x->a[x->n++] = (unsigned)c; + } +} + +void rshift(num_t *x, unsigned s) +{ + unsigned i; + u64 c; + for (i = x->n, c = 0; i--; c <<= 32) + { + c |= (u64)x->a[i]; + x->a[i] = (unsigned)(c >> s); + } + while (x->n > 0 && x->a[x->n - 1] == 0) + { + x->n--; + } +} + +unsigned div1(num_t *q, num_t *x, unsigned y) +{ + unsigned i; + u64 r; + for (i = x->n, r = 0; i--;) + { + r = (r << 32) | (u64)x->a[i]; + q->a[i] = (unsigned)(r / (u64)y); + r %= (u64)y; + } + for (q->n = x->n; q->n > 0 && q->a[q->n - 1] == 0; q->n--) + ; + return (unsigned)r; +} + +void div(num_t *q, num_t *r, num_t *x, num_t *y) +{ + unsigned yh, yh2; + int i, j, ns, rp, qp; + u64 c, m; + assert(y->n >= 1 && y->a[y->n - 1] != 0); + if (y->n == 1) + { + r->a[0] = div1(q, x, y->a[0]); + r->n = (r->a[0] != 0) ? 1 : 0; + return; + } + if (x != r) + for (r->n = x->n, i = 0; i < x->n; i++) + { + r->a[i] = x->a[i]; + } + if (x->n < y->n) + { + q->n = 0; + return; + } + for (i = 0, yh = y->a[y->n - 1]; ((yh << i) & 0x80000000) == 0; i++) + ; + ns = i; + if (i > 0) + { + lshift(r, i); + lshift(y, i); + } + r->a[r->n] = 0; + q->n = qp = 1 + r->n - y->n; + y->a[y->n] = 0; + yh = y->a[y->n - 1]; + yh2 = y->a[y->n - 2]; + for (rp = r->n - 1; qp--; rp--) + { + c = (((u64)(r->a[rp + 1])) << 32) | (u64)(r->a[rp]); + m = c / (u64)yh; + if (m > 0xFFFFFFFF) + { + m = 0xFFFFFFFF; + } + while ((c - m * (u64)yh) < 0x100000000ULL && + (((c - m * (u64)yh) << 32) + (u64)r->a[rp - 1]) < (m * (u64)yh2)) + { + m--; + } + q->a[qp] = m; + for (c = 0, i = qp, j = 0; j <= y->n; i++, j++) + { + c += m * (u64)y->a[j]; + if (r->a[i] >= (unsigned)(c & 0xFFFFFFFFULL)) + { + r->a[i] -= (unsigned)(c & 0xFFFFFFFF); + c >>= 32; + } + else + { + r->a[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL)); + c = (c >> 32) + 1; + } + } + if (c != 0) + { + q->a[qp]--; + assert(c == 1); + for (c = 0, i = qp, j = 0; j <= y->n; i++, j++, c >>= 32) + { + c += (u64)r->a[i] + (u64)y->a[j]; + r->a[i] = (unsigned)c; + } + assert(c == 1); + } + } + rshift(r, ns); + rshift(y, ns); + while (r->n > 0 && r->a[r->n - 1] == 0) + { + r->n--; + } + while (q->n > 0 && q->a[q->n - 1] == 0) + { + q->n--; + } +} + +num_t R, Q; + +int check() +{ + static unsigned q0; + unsigned i; + u64 t; + i = R.n << 1; + if (i < Q.n) + { + return -1; + } + if (i > (Q.n + 1)) + { + return 1; + } + if (i == (Q.n + 1)) + { + q0 = 0; + } + else + { + q0 = Q.a[Q.n - 1]; + } + t = R.a[R.n - 1]; + t *= t; + t >>= 32; + if ((t + 1) < q0) + { + return -1; + } + else if (t > q0) + { + return 1; + } + return 2; +} + +void solve() +{ + static num_t A, B, C; + int i; + u64 t; + if (Q.n <= 1) + { + t = isqrt(Q.a[0]); + printf("%llu\n", t * t); + return; + } + B.n = (Q.n + 1) >> 1; + for (i = 0; i < B.n; i++) + { + B.a[i] = 0; + } + B.a[B.n++] = 1; + A.n = (Q.n - 1) >> 1; + for (i = 0; i < A.n; i++) + { + A.a[i] = 0; + } + if (A.n > 0) + { + A.a[A.n - 1] = 1; + } + while (cmp(&A, &B) < 0) + { + for (i = 0, t = 1; i < A.n; i++, t >>= 32) + { + t += (u64)A.a[i] + (u64)B.a[i]; + R.a[i] = (unsigned)t; + } + for (; i < B.n; i++, t >>= 32) + { + t += (u64)B.a[i]; + R.a[i] = (unsigned)t; + } + for (R.n = B.n; i--;) + { + t = (t << 32) | (u64)R.a[i]; + R.a[i] = (t >> 1); + t &= 1; + } + while (R.a[R.n - 1] == 0) + { + R.n--; + } + i = check(); + if (i == 0) + { + copy(&A, &R); + break; + } + else if (i == -1) + { + copy(&A, &R); + } + else if (i == 1) + { + copy(&B, &R); + dec(&B); + } + else + { + break; + } + } + for (;;) + { + div(&A, &C, &Q, &B); + add(&A, &B); + rshift(&A, 1); + if (cmp(&A, &B) >= 0) + { + break; + } + copy(&B, &A); + } + sqr(&A, &B); + printf("%s\n", fmt(&A)); +} + +int main() +{ + static char s[4096]; + while (scanf(" %[0-9]", s) == 1) + { + set_(&Q, s); + if (Q.n == 0) + { + break; + } + solve(); + } + return 0; +} diff --git a/10608.cpp b/10608.cpp new file mode 100644 index 0000000..136aaba --- /dev/null +++ b/10608.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + int t, n, m, i, j, max, a, b, ra, rb; + short R[30001], N[30001]; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &m); + for (i = 0, max = 1; i <= n; i++) + { + R[i] = i; + N[i] = 1; + } + while (m--) + { + scanf("%d%d", &a, &b); + for (ra = a; ra != R[ra]; ra = R[ra]) + ; + for (rb = b; rb != R[rb]; rb = R[rb]) + ; + R[a] = R[b] = ra; + if (ra == rb) + { + continue; + } + R[rb] = ra; + N[ra] += N[rb]; + if (N[ra] > max) + { + max = N[ra]; + } + } + printf("%d\n", max); + } + return 0; +} diff --git a/10609.cpp b/10609.cpp new file mode 100644 index 0000000..5fae82c --- /dev/null +++ b/10609.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; +} p[100000], start, end_; +int len; +double t; + +const double sqrt3 = sqrt(3); + +int compare(const void *a, const void *b) +{ + Point *c = (Point *)a, *d = (Point *)b; + if (fabs(c->x - d->x) < 1e-8 && fabs(c->y - d->y) < 1e-8) + { + return 0; + } + else if (fabs(c->x - d->x) > 1e-8) + { + double dx = c->x - d->x; + if (dx < 0) + { + return -1; + } + else if (dx == 0) + { + return 0; + } + else + { + return 1; + } + } + else + { + double dy = c->y - d->y; + if (dy < 0) + { + return -1; + } + else if (dy == 0) + { + return 0; + } + else + { + return 1; + } + } +} +Point find(Point c, Point d) +{ + Point e; + double a = sqrt(((c.x - d.x) * (c.x - d.x) + (c.y - d.y) * (c.y - d.y))); + double dx = d.x - c.x, dy = d.y - c.y; + double sint = dy / a, cost = dx / a; + e.x = cost * 0.5 * a - 0.5 * sqrt3 * sint * a + c.x; + e.y = sint * 0.5 * a + 0.5 * sqrt3 * cost * a + c.y; + return e; +} +void run(Point start, Point end_) +{ + // hypot(a,b) + double dist = (start.x - end_.x) * (start.x - end_.x) + + (start.y - end_.y) * (start.y - end_.y); + if (dist < t) + { + return; + } + Point c1, c2, c3; + c1.x = (3 * start.x + end_.x) / 4, c1.y = (3 * start.y + end_.y) / 4; + c2.x = (start.x + 3 * end_.x) / 4, c2.y = (start.y + 3 * end_.y) / 4; + c3 = find(c1, c2); + p[len++] = start; + run(c1, c3); + run(c3, c2); + p[len++] = end_; +} +void rebuild() +{ + int s = 0, i = 0, j; + while (i < len) + { + p[s++] = p[i]; + for (j = i + 1; j < len && fabs(p[j].x - p[i].x) < 1e-8 && + fabs(p[j].y - p[i].y) < 1e-8; + j++) + ; + i = j; + } + len = s; +} + +int main() +{ + int c = 1; + while (scanf("%lf %lf %lf %lf %lf", &start.x, &start.y, &end_.x, &end_.y, &t) == 5) + { + if (t < 1) + { + break; + } + t *= t; + printf("Case %d:\n", c++); + len = 0; + run(start, end_); + qsort(p, len, sizeof(Point), compare); + rebuild(); + printf("%d\n", len); + for (int i = 0; i < len; i++) + { + printf("%.5lf %.5lf\n", p[i].x, p[i].y); + } + } + return 0; +} diff --git a/1061.cpp b/1061.cpp new file mode 100644 index 0000000..6644634 --- /dev/null +++ b/1061.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +string tt[4] = {"O", "A", "B", "AB"}; +bitset<4> cc(int p1, int p2) +{ + if (p1 > p2) + { + return cc(p2, p1); + } + try + { + switch (p1) + { + case 0: + { + if (p2 == 0) + { + throw "1000"; + } + if (p2 == 1) + { + throw "1100"; + } + if (p2 == 2) + { + throw "1010"; + } + if (p2 == 3) + { + throw "0110"; + } + } + case 1: + { + if (p2 == 1) + { + throw "1100"; + } + if (p2 == 2) + { + throw "1111"; + } + if (p2 == 3) + { + throw "0111"; + } + } + case 2: + { + if (p2 == 2) + { + throw "1010"; + } + if (p2 == 3) + { + throw "0111"; + } + } + case 3: + throw "0111"; + } + } + catch (const char *a) + { + string s(a); + reverse(s.begin(), s.end()); + return bitset<4>(s); + } +} +int abo(string s) +{ + int t = 0; + if (strchr(s.c_str(), 'A')) + { + t |= 1; + } + if (strchr(s.c_str(), 'B')) + { + t |= 2; + } + return t; +} +string dc(string p1, string p2) +{ + bitset<4> a(cc(abo(p1), abo(p2))); + ostringstream oss; + for (int i = 0; i < 4; ++i) + if (a[i]) + { + oss << tt[i] + "-, "; + } + if (*p1.rbegin() == '+' || *p2.rbegin() == '+') + for (int i = 0; i < 4; ++i) + if (a[i]) + { + oss << tt[i] + "+, "; + } + string x = oss.str(); + x = x.substr(0, x.length() - 2); + if (strchr(x.c_str(), ',')) + { + x = "{" + x + "}"; + } + return x; +} +string dp(string p, string c) +{ + ostringstream oss; + for (int i = 0; i < 4; ++i) + if (cc(abo(p), i)[abo(c)]) + { + oss << tt[i] + "+, "; + if (*p.rbegin() != '-' || *c.rbegin() != '+') + { + oss << tt[i] + "-, "; + } + } + string x = oss.str(); + if (!x.length()) + { + return "IMPOSSIBLE"; + } + x = x.substr(0, x.length() - 2); + if (strchr(x.c_str(), ',')) + { + x = "{" + x + "}"; + } + return x; +} +int main() +{ + string p1, p2, c; + for (int Tc = 1; cin >> p1 >> p2 >> c; ++Tc) + { + if (p1 == "E" && p2 == "N" && c == "D") + { + return 0; + } + if (c == "?") + { + c = dc(p1, p2); + } + if (p1 == "?") + { + p1 = dp(p2, c); + } + if (p2 == "?") + { + p2 = dp(p1, c); + } + cout << "Case " << Tc << ": " << p1 << " " + << p2 << " " << c << endl; + } +} diff --git a/10610.cpp b/10610.cpp new file mode 100644 index 0000000..54522de --- /dev/null +++ b/10610.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define MAXN 1005 + +typedef long long LL; + +struct node +{ + LL dis; + char color; +} V[MAXN]; + +struct pt +{ + double x, y; +} P[MAXN]; + +LL lim, thole, Dis[MAXN][MAXN], Q[MAXN], qh, qt; + +double ds(double x1, double y1, double x2, double y2) +{ + double x, y, z; + x = (x1 - x2); + x *= x; + y = (y1 - y2); + y *= y; + z = sqrt(x + y); + return z; +} +void GetDis() +{ + int i, j, s; + double d; + for (i = 0; i < thole; i++) + { + for (j = i + 1; j < thole; j++) + { + d = ds(P[i].x, P[i].y, P[j].x, P[j].y); + s = (LL)(d * 1000); + Dis[i + 1][j + 1] = Dis[j + 1][i + 1] = s; + } + } +} +int isBlank(char str[]) +{ + int i; + for (i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = '\0'; + break; + } + } + return strlen(str) == 0; +} +void Ini() +{ + int i; + for (i = 1; i <= thole; i++) + { + V[i].color = 0; + V[i].dis = 0; + } +} +void Push(LL n) +{ + Q[qh++] = n; + qh %= MAXN; +} +LL Pop() +{ + LL n; + n = Q[qt++]; + qt %= MAXN; + return n; +} +LL BFS() +{ + LL i, p; + qh = qt = 0; + Push(1); + V[1].color = 1; + while (qh != qt) + { + p = Pop(); + for (i = 1; i <= thole; i++) + { + if (V[i].color || Dis[p][i] > lim) + { + continue; + } + if (i == 2) + { + return V[p].dis; + } + V[i].color = 1; + V[i].dis = V[p].dis + 1; + Push(i); + } + } + return -1; +} +void Cal() +{ + LL n; + Ini(); + n = BFS(); + if (n >= 0) + { + printf("Yes, visiting %lld other holes.\n", n); + } + else + { + printf("No.\n"); + } +} + +int main() +{ + char str[200]; + LL n, m; + while (gets(str)) + { + sscanf(str, "%lld%lld", &n, &m); + if (!n && !m) + { + break; + } + lim = n * m * 1000 * 60; + thole = 0; + gets(str); + while (!isBlank(str)) + { + sscanf(str, "%lf%lf", &P[thole].x, &P[thole].y); + thole++; + gets(str); + } + GetDis(); + Cal(); + } + return 0; +} diff --git a/10611.cpp b/10611.cpp index 85bb646..10e349c 100644 --- a/10611.cpp +++ b/10611.cpp @@ -1,28 +1,84 @@ -#include -using namespace std; +#include -int F[50000]; +using namespace std; -int main() { - int N, M, Q, i, j, A, B; - scanf("%d", &N); - for (i = 0; i < N; i++) scanf("%d", &F[i]); - scanf("%d", &Q); - while (Q--) { - scanf("%d", &M); - A = -1; B = -1; - for (j = 0; j < N; j++) { - if (F[j] > M) { - B = F[j]; - break; +int M[50005]; +int main() +{ + int n, q, i, j, x; + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + scanf("%d", M + i); + } + M[0] = -1; + M[n + 1] = 2147483647; + scanf("%d", &q); + while (q--) + { + scanf("%d", &x); + if (x > M[1]) + { + for (i = n / 2, j = n / 4;; j = (j + 1) / 2) + { + if (i < 0) + { + i = 0; + } + else if (i > n) + { + i = n; + } + if (M[i] >= x) + { + i -= j; + } + else if (M[i + 1] < x) + { + i += j; + } + else + { + break; + } } - if (F[j] < M) A = F[j]; + printf("%d ", M[i]); + } + else + { + printf("X "); + } + if (x < M[n]) + { + for (i = n / 2, j = n / 4;; j = (j + 1) / 2) + { + if (i <= 0) + { + i = 1; + } + else if (i > n + 1) + { + i = n + 1; + } + if (M[i - 1] > x) + { + i -= j; + } + else if (M[i] <= x) + { + i += j; + } + else + { + break; + } + } + printf("%d\n", M[i]); + } + else + { + printf("X\n"); } - if (A == -1) printf("X "); - else printf("%d ", A); - - if (B == -1) printf("X\n"); - else printf("%d\n", B); } return 0; } diff --git a/10613.cpp b/10613.cpp new file mode 100644 index 0000000..feaccbf --- /dev/null +++ b/10613.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +#define sqr(r) ((r) * (r)) + +struct Circle {double x, y, r;} c[1000]; + +struct Inteval {int up, down;} inte[1000]; + +int compare(const void *a, const void *b) +{ + Inteval *c = (Inteval *)a, *d = (Inteval *)b; + if (c->up != d->up) + { + return d->up - c->up; + } + else + { + return c->down - d->down; + } +} +int calc(int len) +{ + int sum = 0; + int p = 99999999; + for (int i = 0; i < len; i++) + { + if (inte[i].down >= p) + { + continue; + } + if (inte[i].up < p) + { + p = inte[i].up; + } + sum += p - inte[i].down; + p = inte[i].down; + } + return sum; +} + +int size, n, left_, right_; + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &size, &n); + left_ = 99999999; + right_ = -99999999; + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].r); + int l = (int)floor(c[i].x - c[i].r); + if (l < left_) + { + left_ = l; + } + if (left_ < 0) + { + left_ = 0; + } + int r = (int)ceil(c[i].x + c[i].r); + if (r > right_) + { + right_ = r; + } + if (right_ > size) + { + right_ = size; + } + } + int total = 0; + for (int i = left_; i < right_; i++) + { + int a = i, b = i + 1, p = 0; + for (int j = 0; j < n; j++) + { + double k; + if (a <= c[j].x && c[j].x <= b) + { + k = 0; + } + else if (fabs(c[j].x - a) < fabs(c[j].x - b)) + { + k = c[j].x - a; + } + else + { + k = c[j].x - b; + } + double t = (sqr(c[j].r) - sqr(k)); + if (t > 0) + { + t = sqrt(t); + inte[p].up = (int)ceil(c[j].y + t); + if (inte[p].up > size) + { + inte[p].up = size; + } + inte[p].down = (int)floor(c[j].y - t); + if (inte[p].down < 0) + { + inte[p].down = 0; + } + p++; + } + } + if (!p) + { + continue; + } + qsort(inte, p, sizeof(Inteval), compare); + int count = calc(p); + total += count; + } + printf("%d\n", total); + } + return 0; +} diff --git a/10614.cpp b/10614.cpp new file mode 100644 index 0000000..095a527 --- /dev/null +++ b/10614.cpp @@ -0,0 +1,198 @@ +#include + +using namespace std; + +typedef struct +{ + int dim, v[3]; +} value_t; + +static int la, la_num; +static char *tokptr; + +static int next() +{ + register const char *p; + while (*tokptr == ' ') + { + tokptr++; + } + if (*tokptr == '\0') + { + return (la = 0); + } + for (p = "+-*x,[]()"; *p; p++) + if (*tokptr == *p) + { + return (la = *tokptr++); + } + if (*tokptr >= '0' && *tokptr <= '9') + { + for (la_num = 0; *tokptr >= '0' && *tokptr <= '9';) + { + la_num = la_num * 10 + *tokptr++ - '0'; + } + return (la = 'n'); + } + return (la = -1); +} + +static int expr(value_t *); + +static int primary(value_t *r) +{ + if (la == 'n') + { + r->dim = 1; + r->v[0] = la_num; + next(); + return 0; + } + if (la == '(') + { + if (next() <= 0 || expr(r) || la != ')') + { + return 1; + } + next(); + return 0; + } + if (la == '[') + { + value_t t; + r->dim = 3; + if (next() <= 0 || expr(&t) || t.dim != 1 || la != ',') + { + return 1; + } + r->v[0] = t.v[0]; + if (next() <= 0 || expr(&t) || t.dim != 1 || la != ',') + { + return 1; + } + r->v[1] = t.v[0]; + if (next() <= 0 || expr(&t) || t.dim != 1 || la != ']') + { + return 1; + } + r->v[2] = t.v[0]; + next(); + return 0; + } + return 1; +} + +static int term(value_t *r) +{ + value_t t, u; + int op, i, v; + if (primary(r)) + { + return 1; + } + while (la == '*' || la == 'x') + { + op = la; + if (next() <= 0 || primary(&t)) + { + return 1; + } + if (op == '*') + { + if (r->dim == t.dim) + { + for (i = 0, v = 0; i < t.dim; i++) + { + v += r->v[i] * t.v[i]; + } + r->v[0] = v; + r->dim = 1; + } + else if (r->dim == 3) + { + for (i = 0; i < 3; i++) + { + r->v[i] *= t.v[0]; + } + } + else + { + for (v = r->v[0], i = 0; i < 3; i++) + { + r->v[i] = t.v[i] * v; + } + r->dim = 3; + } + } + else + { + if (r->dim != 3 || t.dim != 3) + { + return 1; + } + for (i = 0; i < 3; i++) + { + u.v[i] = r->v[i]; + } + r->v[0] = u.v[1] * t.v[2] - u.v[2] * t.v[1]; + r->v[1] = u.v[2] * t.v[0] - u.v[0] * t.v[2]; + r->v[2] = u.v[0] * t.v[1] - u.v[1] * t.v[0]; + } + } + return 0; +} + +static int expr(value_t *r) +{ + value_t t; + int op, i; + if (term(r)) + { + return 1; + } + while (la == '+' || la == '-') + { + op = la; + if (next() <= 0 || term(&t) || r->dim != t.dim) + { + return 1; + } + for (i = 0; i < t.dim; i++) + { + r->v[i] += (op == '+') ? t.v[i] : (-t.v[i]); + } + } + return 0; +} + +int main() +{ + static char buf[1024]; + static value_t r; + int i; + while (fgets(buf, sizeof(buf), stdin)) + { + for (i = 0; buf[i] && buf[i] != '#' && buf[i] != '\n'; i++) + ; + if (buf[i] == '#') + { + break; + } + buf[i] = '\0'; + tokptr = buf; + next(); + if (expr(&r) || la != 0) + { + printf("Bang!\n"); + } + else if (r.dim == 1) + { + printf("%d\n", r.v[0]); + } + else + { + printf("[%d,%d,%d]\n", r.v[0], r.v[1], r.v[2]); + } + } + return 0; +} diff --git a/10615.cpp b/10615.cpp new file mode 100644 index 0000000..3a4aa51 --- /dev/null +++ b/10615.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +int res_color[111][111]; +char g[111][111]; +int N; + +vector adj[111]; +int M[111];// matched people with y[i]; +int V[111];// visit check +int c; + +void print_grid() +{ + printf("===============\n"); + for (int i = 0; i < N; i++) + { + printf("%s\n", g[i]); + } + printf("===============\n"); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (j) + { + printf(" "); + } + printf("%d", res_color[i][j]); + } + printf("\n"); + } + printf("===============\n"); +} + +bool dfs(int v) +{ + if (V[v] == c) + { + return false; + } + V[v] = c; + for (int i = 0; i < (int)adj[v].size(); i++) + { + int t = adj[v][i]; + //if(res_color[v][t] != 0) continue; + if (M[t] == -1 || (v != M[t] && dfs(M[t]))) + { + M[t] = v; + return true; + } + } + return false; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%s", g[i]); + } + for (int i = 0; i < N; i++) + { + adj[i].clear(); + } + int ldeg[111], rdeg[111]; + int max_degree = 0; + memset(ldeg, 0, sizeof(ldeg)); + memset(rdeg, 0, sizeof(rdeg)); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (g[i][j] == '*') + { + adj[i].push_back(j); + max_degree = max(max_degree, ++ldeg[i]); + max_degree = max(max_degree, ++rdeg[j]); + } + for (int l = 0, r = 0; l < N && r < N;) + { + if (ldeg[l] == max_degree) + { + l++; + } + else if (rdeg[r] == max_degree) + { + r++; + } + else + { + adj[l].push_back(r); + ldeg[l]++; + rdeg[r]++; + } + } + memset(res_color, 0, sizeof(res_color)); + int res_max_color = max_degree; + for (int i = 1; i <= max_degree; i++) + { + memset(V, -1, sizeof(V)); + memset(M, -1, sizeof(M)); + for (c = 0; c < N; c++) + { + dfs(c); + } + for (int j = 0; j < N; j++) + if (M[j] != -1) + { + vector &a = adj[M[j]]; + a.erase(find(a.begin(), a.end(), j)); + //printf("(%d %d)\n", M[j], j); + if (g[M[j]][j] == '*') + { + res_color[M[j]][j] = i; + } + } + //printf("\n"); + } + assert(res_max_color == max_degree); + printf("%d\n", res_max_color); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (j) + { + printf(" "); + } + printf("%d", res_color[i][j]); + } + printf("\n"); + } + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + if (res_color[i][j] == 0 && g[i][j] == '*') + { + print_grid(); + assert(false); + } + if (res_color[i][j] != 0 && g[i][j] == '.') + { + assert(false); + } + } + for (int k = 0; k < N; k++) + { + for (int i = 0; i < N; i++) + for (int j = i + 1; j < N; j++) + { + assert(res_color[k][i] != '*' || res_color[k][j] != '*' || res_color[k][i] != res_color[k][j]); + } + } + } + return 0; +} diff --git a/10616.cpp b/10616.cpp new file mode 100644 index 0000000..cc74fa4 --- /dev/null +++ b/10616.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +/** + * DP Subset Sum + */ + +using std::vector; + +enum +{ + MAX_NUM = 200, + MAX_DIVISOR = 20, + MAX_M = 10 +}; +vector n;// the number set +int D; // the divisor +int M; // the take number +int c[MAX_NUM][MAX_DIVISOR + 1][MAX_M]; +bool v[MAX_NUM][MAX_DIVISOR + 1][MAX_M];// already in table or not + +int mod(int s) +{ + if (s >= 0) + { + return s % D; + } + else + { + return D - (abs(s) % D); + } +} + +// Top-down DP +// (i,j,k) = (i+1, j+n[i], k-1) or (i+1, j, k) +// i : current index +// sum : current sum +// k : numbers left to pick +int recur_subset(int i, int sum, int k) +{ + sum = mod(sum); + if (v[i][sum][k]) + return c[i][sum][k]; + if (k == 0) + return ((sum % D == 0) ? 1 : 0); + if (i == n.size()) + return 0; + int ans_count = 0; + ans_count += recur_subset(i + 1, sum + n[i], k - 1);// take n[i] or + ans_count += recur_subset(i + 1, sum, k); // not take n[i] + c[i][sum][k] = ans_count; + v[i][sum][k] = true; + return ans_count; +} + +int solve() +{ + memset(c, 0, sizeof(c)); + memset(v, 0, sizeof(v)); + return recur_subset(0, 0, M); +} + +int main() +{ + int set_no = 1; + int num_num; // the number of input numbers + int num_query;// the number of queries + while (scanf("%d%d", &num_num, &num_query) == 2) + { + if (num_num == 0 && num_query == 0) + break; + n.resize(num_num); + for (int i = 0; i < num_num; ++i) + scanf("%d", &n[i]); + printf("SET %d:\n", set_no++); + for (int i = 1; i <= num_query; ++i) + { + scanf("%d%d", &D, &M); + printf("QUERY %d: %d\n", i, solve()); + } + } + return 0; +} diff --git a/10617.cpp b/10617.cpp new file mode 100644 index 0000000..04f7635 --- /dev/null +++ b/10617.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +/* +10617 +Again Palindrom +*/ +#define maxn 70 +typedef long long xxx; +char F[maxn][maxn]; +xxx C[maxn][maxn], len; +struct pos +{ + int l, r; +} P[1800]; +int ind; +char ss[70]; +xxx Recur(xxx l, xxx r) +{ + xxx i = l + 1, j = r - 1, c = 1; + if (l == r) + { + return 0; + } + if (F[l][r]) + { + return C[l][r]; + } + F[l][r] = 1; + c += r - l - 1; + for (i = 0; i < ind; i++) + { + if (P[i].l > l && P[i].r < r) + { + c += Recur(P[i].l, P[i].r); + } + } + C[l][r] = c; + return c; +} +void Set() +{ + int i, j; + ind = 0; + for (i = 0; i < len; i++) + { + for (j = len - 1; j > i; j--) + { + if (ss[i] == ss[j]) + { + P[ind].l = i; + P[ind++].r = j; + } + } + } +} +void Cal() +{ + xxx i, j, cc = 0; + len = strlen(ss); + Set(); + for (i = 0; i < ind; i++) + { + cc += Recur(P[i].l, P[i].r); + } + cc += len; + printf("%lld\n", cc); + ; +} +void Reset() +{ + int i, j; + for (i = 0; i < len; i++) + for (j = 0; j < len; j++) + { + F[i][j] = 0; + } +} +int main() +{ + int ks; + scanf("%d", &ks); + while (ks--) + { + scanf("%s", ss); + ; + Cal(); + if (ks) + { + Reset(); + } + } + return 0; +} diff --git a/10618.cpp b/10618.cpp new file mode 100644 index 0000000..8afb661 --- /dev/null +++ b/10618.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +char line[128]; +const char *btn = "ULDR."; +int memo[72][4][4][4]; + +int f(int tm, int left, int right, int prev, int recov = 0) +{ + int &ret = memo[tm][left][right][prev]; + if (!recov) + { + if (ret != -1) + { + return ret; + } + ret = 1 << 30; + } + if (line[tm] == '\0' || strchr(btn, line[tm]) == NULL) + { + if (recov) + { + printf("\n"); + } + return ret = 0; + } + int now = strchr(btn, line[tm]) - btn; + if (now == 4) + { + int x = f(tm + 1, left, right, 0); + ret = MIN(ret, x); + if (recov && ret == x) + { + putchar('.'); + return f(tm + 1, left, right, 0, 1); + } + } + for (int foot = 1; foot <= 2; foot++) + for (int to = 0; to < 4; to++) + { + if (now < 4 && to != now) + { + continue; + } + if (foot == 1 && to == right) + { + continue; + } + if (foot == 2 && to == left) + { + continue; + } + int pos = (foot == 1 ? left : right); + int shift = min((to - pos + 4) % 4, (pos - to + 4) % 4); + if (shift > 0) + { + if (foot == 1 && right == 1) + { + continue; + } + if (foot == 2 && left == 3) + { + continue; + } + } + int x = (prev != foot ? 1 : ("357"[shift] - '0')); + if (foot == 1) + { + x += f(tm + 1, to, right, foot); + } + else + { + x += f(tm + 1, left, to, foot); + } + if (!recov || ret != x) + { + ret = MIN(ret, x); + continue; + } + putchar(".LR"[foot]); + if (foot == 1) + { + return f(tm + 1, to, right, foot, 1); + } + else + { + return f(tm + 1, left, to, foot, 1); + } + } + return ret; +} + +int main() +{ + while (gets(line) && line[0] != '#') + { + memset(memo, 0xff, sizeof(memo)); + for (int r = 0; r < 2; r++) + { + f(0, 1, 3, 0, r); + } + } + return 0; +} diff --git a/10619.cpp b/10619.cpp new file mode 100644 index 0000000..df29815 --- /dev/null +++ b/10619.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +int *e[100001]; +int ar[100001][3]; +int cases, n, j, m, maxt, mint, tx; +bool cmp(int *p, int *q) +{ + return p[0] < q[0]; +} +bool solve(int t) +{ + int v = 0, i = 0, x = e[0][0] - 2 * t; + while (i < n) + { + while ((e[i][2] - e[i][1]) <= x && i < n) + { + ++i; + } + ++v; + if (v > m) + { + break; + } + x = e[i][0] - 2 * t; + } + if (v <= m) + { + return true; + } + else + { + return false; + } +} +int main() +{ + for (int i = 0; i < 100001; ++i) + { + e[i] = ar[i]; + } + scanf("%d", &cases); + for (int k = 1; k <= cases; ++k) + { + int i = 0; + maxt = 1000000; + mint = -2000000; + scanf("%d%d", &n, &m); + for (int num = 0; num < n; ++num) + { + scanf("%d%d", &e[i][1], &e[i][2]); + for (j = 0; j < i; ++j) + { + tx = e[i][2] > e[j][2] ? e[i][2] - e[j][2] : e[j][2] - e[i][2]; + if (e[i][1] >= e[j][1] + tx) + { + break; + } + } + if (j < i) + { + continue; + } + e[i][0] = e[i][2] + e[i][1]; + if (e[i][1] < maxt) + { + maxt = e[i][1]; + } + ++i; + } + n = i; + sort(e, e + n, cmp); + while (mint < maxt) + { + int temp = (mint + maxt) / 2; + if (solve(temp)) + { + mint = temp + 1; + } + else + { + maxt = temp - 1; + } + } + if (solve(maxt) == false) + { + --maxt; + } + printf("Case %d: %d\n", k, maxt); + } + return 0; +} diff --git a/1062.cpp b/1062.cpp new file mode 100644 index 0000000..ebea97a --- /dev/null +++ b/1062.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +vector> S; +string s; + +void insert(char c) +{ + bool found = false; + int pos; + char _min = 'Z' + 1; + for (int i = 0; i < S.size(); i++) + { + if (S[i].back() >= c) + { + if (S[i].back() < _min) + { + _min = S[i].back(); + pos = i; + found = true; + } + } + } + if (found) + { + S[pos].push_back(c); + } + else + { + S.push_back(vector()); + S.back().push_back(c); + } +} + +int casno = 1; +int main() +{ + while (cin >> s && s != "end") + { + S.clear(); + for (int i = 0; i < s.length(); i++) + { + insert(s[i]); + } + printf("Case %d: %d\n", casno++, S.size()); + } + return 0; +} diff --git a/10620.cpp b/10620.cpp new file mode 100644 index 0000000..bee33b1 --- /dev/null +++ b/10620.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +/* +Aflea on a chessboard +10620 +*/ +//typedef __int64 ss; +typedef long long ss; +ss S, X, Y, dx, dy; +struct cor +{ + int x, y; +}; +int Inside() +{ + ss x, y; + x = X / S; + y = Y / S; + if (X % S && Y % S) + { + if ((x + y) % 2) + { + return 1; + } + } + return 0; +} +void Cal() +{ + ss i, x, y; + x = X / S; + y = Y / S; + if (X % S && Y % S) + { + if ((x + y) % 2) + { + printf("After 0 jumps the flea lands at (%lld, %lld).\n", X, Y); + return; + } + } + for (i = 0; i < 1100; i++) + { + X += dx; + Y += dy; + if (Inside()) + { + printf("After %lld jumps the flea lands at (%lld, %lld).\n", i + 1, X, Y); + return; + } + } + printf("The flea cannot escape from black squares.\n"); +} +int main() +{ + while (scanf("%lld%lld%lld%lld%lld", &S, &X, &Y, &dx, &dy) == 5) + { + if (!S && !X && !Y && !dx && !dy) + { + break; + } + Cal(); + } + return 0; +} diff --git a/10621.cpp b/10621.cpp new file mode 100644 index 0000000..40dd2be --- /dev/null +++ b/10621.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +char maze[32 * 32]; +int N, h, H, s, S; +int dist[32 * 32][32 * 32], seen[32 * 32][32 * 32]; +char prev_[32 * 32][32 * 32]; + +int Dist(int a, int b) +{ + int x = (a & 31) - (b & 31); + int y = (a >> 5) - (b >> 5); + return x * x + y * y; +} + +int main() +{ + for (int cs = 1; scanf("%d", &N) == 1 && N > 0; cs++) + { + memset(maze, '*', sizeof(maze)); + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= N; j++) + { + int k = i * 32 + j; + scanf(" %c", &maze[k]); + char c = maze[k]; + if (c == 'H') + { + H = k; + } + if (c == 'h') + { + h = k; + } + if (c == 'S') + { + S = k; + } + if (c == 's') + { + s = k; + } + } + } + seen[H][h] = cs; + dist[H][h] = Dist(H, h); + priority_queue> Q; + Q.push(make_pair(dist[H][h], H * 1024 + h)); + int delta[] = {-1, 1, 32, -32, 0}; + char delta_c[] = "WESN "; + while (!Q.empty()) + { + int jack = Q.top().second >> 10; + int jill = Q.top().second & 1023; + if (Q.top().first != dist[jack][jill]) + { + Q.pop(); + continue; + } + Q.pop(); + if (jack == S && jill == s) + { + break; + } + for (int dir1 = 0; dir1 < 5; dir1++) + { + int ja = jack + delta[dir1]; + if ((dir1 == 4) ^ (maze[jack] == 'S')) + { + continue; + } + if (maze[ja] == '*' || maze[ja] == 's' || maze[ja] == 'h') + { + continue; + } + for (int dir2 = 0; dir2 < 5; dir2++) + { + int ji = jill + delta[dir2]; + if ((dir2 == 4) ^ (maze[jill] == 's')) + { + continue; + } + if (maze[ji] == '*' || maze[ji] == 'S' || maze[ji] == 'H') + { + continue; + } + if (seen[ja][ji] != cs) + { + seen[ja][ji] = cs; + dist[ja][ji] = -1; + } + int d = min(dist[jack][jill], Dist(ja, ji)); + if (d > dist[ja][ji]) + { + dist[ja][ji] = d; + prev_[ja][ji] = dir1 * 8 + dir2; + Q.push(make_pair(d, ja * 1024 + ji)); + } + } + } + } + if (cs != 1) + { + printf("\n"); + } + printf("%.2lf\n", sqrt(dist[S][s] + 0.0)); + string str[2]; + int jack = S, jill = s; + assert(seen[jack][jill] == cs); + while (jack != H || jill != h) + { + int dir1 = prev_[jack][jill] / 8; + int dir2 = prev_[jack][jill] % 8; + if (dir1 < 4) + { + str[0] += delta_c[dir1]; + } + if (dir2 < 4) + { + str[1] += delta_c[dir2]; + } + jack -= delta[dir1]; + jill -= delta[dir2]; + } + reverse(str[0].begin(), str[0].end()); + reverse(str[1].begin(), str[1].end()); + printf("%s\n", str[0].c_str()); + printf("%s\n", str[1].c_str()); + } + return 0; +} diff --git a/10622.cpp b/10622.cpp new file mode 100644 index 0000000..545c289 --- /dev/null +++ b/10622.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +/* +10622 +*/ +//typedef __int64 ss; +typedef long long ss; +int F; +void Cal(ss x) +{ + int i, p; + double k, d; + for (i = 31; i >= 1; i--) + { + k = pow((double)x, 1 / (double)i) + 1E-5; + p = k; + d = pow(p, i); + if (d == x) + { + if (!F) + { + printf("%d\n", i); + return; + } + else if (F && i % 2) + { + printf("%d\n", i); + return; + } + } + } + printf("1\n"); +} +int main() +{ + ss n; + while (scanf("%lld", &n) == 1) + { + if (!n) + { + break; + } + F = 0; + if (n < 0) + { + F = 1; + n *= -1; + } + Cal(n); + } + return 0; +} diff --git a/10623.cpp b/10623.cpp new file mode 100644 index 0000000..a040de3 --- /dev/null +++ b/10623.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +typedef long long int _int64; + +_int64 sum; + +// binary search +bool sqrt(_int64 n, _int64 &ans) +{ + _int64 left = 1, right = 65536, mid = (left + right) / 2; + while (left <= right) + { + if (mid * mid == n) + { + ans = mid; + return true; + } + else if (mid * mid > n) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + return false; +} + +int main() +{ + int caseno = 1; + while (scanf("%lld", &sum), sum != -1) + { + printf("Case %d:\n", caseno++); + bool flag = false; + if (sum == 1) + { + puts("0 0 0"); + } + else if (sum == 2) + { + puts("0 0 1"); + puts("0 1 0"); + puts("1 0 0"); + } + else + { + for (int m = 0; m < 100; m++) + for (int p = 99; p >= 0; p--) + { + _int64 a = 1, b = (4 * m + 6 * p - 1), c = (2 + 2 * m * m - 2 * m + 6 * p * m + 3 * p * p - 3 * p - sum); + _int64 d = b * b - 4 * c, e; + if (sqrt(d, e)) + { + if ((b - e) % 2 == 0) + { + _int64 x = (-b - e) / 2; + if (x >= 0 && x < 20000) + { + printf("%d %lld %d\n", m, x, p); + flag = true; + } + _int64 y = (-b + e) / 2; + if (y >= 0 && y < 20000) + { + printf("%d %lld %d\n", m, y, p); + flag = true; + } + } + } + } + if (!flag) + { + puts("Impossible."); + } + } + } + return 0; +} diff --git a/10624.cpp b/10624.cpp new file mode 100644 index 0000000..f709d7a --- /dev/null +++ b/10624.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +int m, n, a[35]; +int dfs(int cur) +{ + int i, j, s, c; + if (cur == m) + { + return 1; + } + if (cur == 0) + { + if (n == 1) + { + for (i = n; i < 10; i += n) + { + a[cur] = i; + if (dfs(cur + 1)) + { + return 1; + } + } + } + else + for (i = 1; i < 10; i++) + { + a[cur] = i; + if (dfs(cur + 1)) + { + return 1; + } + } + } + else if (cur >= n - 1) + { + for (i = 0; i < 10; i++) + { + a[cur] = i; + c = 0; + for (j = 0; j <= cur; j++) + { + s = c * 10 + a[j]; + c = s % (cur + 1); + } + if (c == 0 && dfs(cur + 1)) + { + return 1; + } + } + } + else + for (i = 0; i < 10; i++) + { + a[cur] = i; + if (dfs(cur + 1)) + { + return 1; + } + } + return 0; +} +int main() +{ + int i, j, k, t, tt; + scanf("%d", &t); + for (tt = 0; tt < t; tt++) + { + scanf("%d%d", &n, &m); + printf("Case %d: ", tt + 1); + if (m == 29) + if (n >= 1 && n <= 12) + { + if (n >= 1 && n <= 5) + { + printf("-1\n"); + } + else + switch (n) + { + case 6: + printf("39208896001212006680167258804\n"); + break; + case 7: + printf("18555536703606084000060008482\n"); + break; + case 8: + printf("10211576408418088880443254681\n"); + break; + case 9: + printf("10101279606082569080443254680\n"); + break; + case 10: + printf("10015192000842085860660000924\n"); + break; + case 11: + printf("10000351372830084060967250126\n"); + break; + case 12: + printf("10000028419290081060024006085\n"); + break; + } + continue; + } + memset(a, 0, sizeof(a)); + if (dfs(0)) + { + for (i = 0; i < m; i++) + { + printf("%d", a[i]); + } + } + else + { + printf("-1"); + } + printf("\n"); + } + return 0; +} diff --git a/10625.cpp b/10625.cpp new file mode 100644 index 0000000..f0959f1 --- /dev/null +++ b/10625.cpp @@ -0,0 +1,166 @@ +#include + +using namespace std; + +#define MAXN 95 + +typedef unsigned long long xx; + +struct sss +{ + int A[MAXN]; + int ind; +} link[MAXN]; +struct zz +{ + int A[MAXN]; + int ind; + int fre[MAXN]; +} tmp[MAXN]; +char Rule[MAXN], H[MAXN]; +int Str[MAXN], rule[MAXN]; +xx Fre[10002][MAXN]; +int R, Q, max, min, tr; + +void Set(char ss[]) +{ + int i, j, k; + k = ss[0] - 33; + Rule[k] = 1; + rule[tr++] = k; + link[k].ind = 0; + for (i = 3; ss[i]; i++) + { + j = ss[i] - 33; + link[k].A[link[k].ind++] = j; + } +} +void Free(int limit, int n) +{ + int i, j; + for (i = 1; i <= limit + 1; i++) + { + for (j = 0; j < R; j++) + { + Fre[i][rule[j]] = 0; + } + Fre[i][n] = 0; + } +} +void Count(int n, char ss[], int limit) +{ + int i, j, ind = 1, k, m = 0; + int tc = 0, l, y, p; + int ff[100] = {0}; + for (i = 0; i < MAXN; i++) + { + H[i] = 0; + Fre[0][i] = 0; + } + for (i = 0; ss[i]; i++) + { + k = ss[i] - 33; + if (n != k && !Rule[k]) + { + continue; + } + if (H[k] == 0) + { + Str[tc++] = k; + H[k] = 1; + } + Fre[0][k]++; + } + for (i = 0; i < R; i++) + { + j = rule[i]; + tmp[j].ind = 0; + for (k = 0; k < link[j].ind; k++) + { + m = link[j].A[k]; + if (!Rule[m] && m != n) + { + continue; + } + if (ff[m] == 0) + { + tmp[j].A[tmp[j].ind++] = m; + tmp[j].fre[tmp[j].ind - 1] = 1; + ff[m] = tmp[j].ind; + } + else + { + tmp[j].fre[ff[m] - 1]++; + } + } + for (k = 0; k < R; k++) + { + ff[rule[k]] = 0; + } + ff[n] = 0; + } + l = tc; + m = 0; + Free(limit, n); + while (m++ < limit) + { + for (i = 0; i < l; i++) + { + k = Str[i]; + if (Rule[k]) + { + for (y = 0; y < tmp[k].ind; y++) + { + p = tmp[k].A[y]; + Fre[ind][p] += Fre[ind - 1][k] * tmp[k].fre[y]; + if (H[p] == 0) + { + H[p] = 1; + Str[tc++] = p; + } + } + } + else + { + Fre[ind][k] += Fre[ind - 1][k]; + } + } + ind++; + l = tc; + } + printf("%llu\n", Fre[ind - 1][n]); +} +void Cal() +{ + int st, k; + char str[200], ch[5]; + scanf("%d", &Q); + while (Q--) + { + scanf("%s%s%d", str, ch, &st); + k = ch[0] - 33; + Count(k, str, st); + } +} + +int main() +{ + int ks, i; + char temp[200]; + scanf("%d", &ks); + while (ks--) + { + scanf("%d", &R); + i = tr = 0; + while (i < R) + { + scanf("%s", temp); + Set(temp); + i++; + } + Cal(); + for (i = 0; i < R; i++) + Rule[rule[i]] = 0; + } + return 0; +} diff --git a/10626.cpp b/10626.cpp new file mode 100644 index 0000000..3c5acae --- /dev/null +++ b/10626.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +int dp[160][160][60]; + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + int cokes, N[3]; + scanf("%d %d %d %d", &cokes, &N[0], &N[1], &N[2]); + int money1 = N[0] + 5 * N[1] + 10 * N[2]; + int money2 = money1 - 8 * cokes; + assert(money2 >= 0); + memset(dp, 0x1f, sizeof(dp)); + for (int C = 0; C <= cokes; C++) + { + int money = money1 - 8 * (cokes - C); + for (int fives = 0; fives <= 150; fives++) + for (int tens = 0; tens <= N[2]; tens++) + { + int ones = money - 5 * fives - 10 * tens; + if (ones < 0) + { + continue; + } + int &res = dp[C][fives][tens]; + if (C == 0) + { + res = 0; + continue; + } + if (tens >= 1) + { + res = MIN(res, 1 + dp[C - 1][fives][tens - 1]); + } + if (tens >= 1 && ones >= 3) + { + res = MIN(res, 4 + dp[C - 1][fives + 1][tens - 1]); + } + if (fives >= 1 && ones >= 3) + { + res = MIN(res, 4 + dp[C - 1][fives - 1][tens]); + } + if (fives >= 2) + { + res = MIN(res, 2 + dp[C - 1][fives - 2][tens]); + } + if (ones >= 8) + { + res = MIN(res, 8 + dp[C - 1][fives][tens]); + } + } + } + printf("%d\n", dp[cokes][N[1]][N[2]]); + } + return 0; +} diff --git a/10627.cpp b/10627.cpp new file mode 100644 index 0000000..0e1592f --- /dev/null +++ b/10627.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define LL long long +const double eps = 1e-8; + +LL gcd(LL a, LL b) +{ + return b ? gcd(b, a % b) : a; +} + +LL lcm(LL a, LL b) +{ + return a / gcd(a, b) * b; +} + +LL L, u, v, t; + +void solve() +{ + if (u == 0 && v == 0) + { + puts("0"); + return; + } + if (u < v) + { + u = v; + } + LL mx = u * t; + LL ans = 0; + if (mx >= L) + { + ans++; + ans += (mx - L) / (L * 2); + } + printf("%lld\n", ans); +} + +int main() +{ + while (scanf("%lld%lld%lld%lld", &L, &u, &v, &t) != -1 && L) + { + if (u == 0 || v == 0) + { + solve(); + continue; + } + if (u < v) + { + swap(u, v); + } + LL mx = t * (u + v); + LL ans = 0; + if (mx >= L) + { + ans++; + ans += (mx - L) / (L * 2); + } + if (t * (u - v) >= L) + { + ans++; + ans += (t * (u - v) - L) / (L * 2); + } + LL tmp = gcd(u, v); + if (((u - v) / tmp) % 2 == 1 && L <= t * tmp) + { + ans--; + ans -= (LL)(t * tmp - L) / (L * 2); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/10629.cpp b/10629.cpp new file mode 100644 index 0000000..3db8bd7 --- /dev/null +++ b/10629.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +int D, N, MaxTime; + +struct Light +{ + int x, red, green; + + int t[6000], k; + double dp[6000]; + + bool operator<(const Light &l) const + { + return x < l.x; + } +} light[52]; + +bool check(int from, int to, int t1_20, int x1, int t2_20, int x2) +{ + assert(x1 < x2); + for (int i = from; i <= to; i++) + { + const Light &L = light[i]; + int p = t1_20 * (x2 - x1) + (t2_20 - t1_20) * (L.x - x1); + int q = 20 * (x2 - x1); + p %= q * (L.red + L.green); + if (0 < p && p < q * L.red) + { + return false; + } + } + return true; +} + +double lineCost(double dx, double dt) +{ + return dx + dt * dt / dx - 0.1 * dt; +} + +double solve() +{ + if (D <= 0) + { + return 0; + } + if (N == 0) + { + return lineCost(D, D / 20.0); + } + MaxTime = D / 20 + 5; + for (int i = 1; i <= N; i++) + { + MaxTime += light[i].red; + } + // printf("maxtime=%d\n", MaxTime); + for (int i = 1; i <= N; i++) + { + Light &L = light[i]; + assert(0 < L.x && L.x < D); + L.k = 1; + L.t[0] = L.red; + while (true) + { + L.t[L.k] = L.t[L.k - 1] + (L.k % 2 == 1 ? L.green : L.red); + if (L.t[L.k] > MaxTime) + { + break; + } + L.k++; + } + } + light[0].k = 1; + light[0].t[0] = 0; + light[0].dp[0] = 0; + for (int b = 1; b <= N; b++) + { + Light &B = light[b]; + for (int bi = 0; bi < B.k; bi++) + { + double res = 1e99; + for (int a = 0; a < b; a++) + { + const Light &A = light[a]; + for (int ai = 0; ai < A.k && A.t[ai] < B.t[bi]; ai++) + { + if (!check(a + 1, b - 1, A.t[ai] * 20, A.x, B.t[bi] * 20, B.x)) + { + continue; + } + double dx = B.x - A.x; + double dt = B.t[bi] - A.t[ai]; + res = min(res, A.dp[ai] + lineCost(dx, dt)); + } + } + B.dp[bi] = res; + //printf("b=%d (x=%d), bi=%d (t=%d): dp=%.3f\n", b, L[b].x, bi, L[b].t[bi], dp[b][bi]); + } + } + double best = 1e99; + for (int a = 0; a <= N; a++) + { + const Light &A = light[a]; + for (int ai = 0; ai < A.k; ai++) + { + int x0 = A.x, dx = D - x0; + int t0_20 = A.t[ai] * 20; + int t1_20 = t0_20 + dx; + if (check(a + 1, N, t0_20, x0, t1_20, D)) + { + best = min(best, A.dp[ai] + lineCost(dx, dx / 20.0)); + } + } + } + return best; +} + +int main() +{ + while (cin >> D >> N) + { + if (D == 0 && N == 0) + { + break; + } + for (int i = 1; i <= N; i++) + { + cin >> light[i].x >> light[i].red >> light[i].green; + } + sort(light + 1, light + N + 1); + printf("%.2f\n", solve()); + } +} diff --git a/1063.cpp b/1063.cpp new file mode 100644 index 0000000..0c265b4 --- /dev/null +++ b/1063.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +int mx[4] = {0, 0, 1, -1}, my[4] = {1, -1, 0, 0}; +struct board +{ + char s[4][4]; + board() {} + board(char t[4][4]) + { + memcpy(s, t, 16); + } + bool operator<(board rhs) const + { + int i, j; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (s[i][j] < rhs.s[i][j]) + { + return true; + } + else if (s[i][j] > rhs.s[i][j]) + { + return false; + } + } + } + return false; + } +}; +bool wall[4][4][4]; +int main() +{ + map dis; + queue q; + char t[4][4]; + int n, m, w, i, j, k, x1, y1, x2, y2, d, cs = 1; + while (scanf("%d%d%d", &n, &m, &w), n || m || w) + { + memset(wall, 0, sizeof(wall)); + for (i = 0; i < 4; i++) + { + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (j + mx[i] < 0 || j + mx[i] >= n || k + my[i] < 0 || k + my[i] >= n) + { + wall[i][j][k] = true; + } + } + } + } + memset(t, 0, sizeof(t)); + for (i = 1; i <= m; i++) + { + scanf("%d%d", &x1, &y1); + t[x1][y1] = i; + } + for (i = 1; i <= m; i++) + { + scanf("%d%d", &x1, &y1); + t[x1][y1] = -i; + } + for (i = 0; i < w; i++) + { + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + for (j = 0; j < 4; j++) + { + if (x2 == x1 + mx[j] && y2 == y1 + my[j]) + { + wall[j][x1][y1] = true; + wall[j ^ 1][x2][y2] = true; + break; + } + } + } + dis.clear(); + dis[board(t)] = 0; + q.push(board(t)); + while (!q.empty()) + { + d = dis[q.front()]; + for (i = 0; i < 4; i++) + { + bool flag, wrong = false; + memcpy(t, q.front().s, 16); + do + { + flag = false; + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (t[j][k] <= 0 || wall[i][j][k]) + { + continue; + } + else + { + x1 = j + mx[i]; + y1 = k + my[i]; + if (t[x1][y1] > 0) + { + continue; + } + else if (t[x1][y1] == 0) + { + t[x1][y1] = t[j][k]; + t[j][k] = 0; + flag = true; + } + else + { + if (t[x1][y1] + t[j][k] != 0) + { + wrong = true; + j = n; + k = n; + } + else + { + t[x1][y1] = t[j][k] = 0; + flag = true; + } + } + } + } + } + } while (flag && !wrong); + if (!wrong && !dis.count(board(t))) + { + dis[board(t)] = d + 1; + q.push(board(t)); + } + } + q.pop(); + } + memset(t, 0, 16); + if (dis.count(board(t))) + { + printf("Case %d: %d moves\n\n", cs++, dis[board(t)]); + } + else + { + printf("Case %d: impossible\n\n", cs++); + } + } + return 0; +} diff --git a/10631.cpp b/10631.cpp new file mode 100644 index 0000000..71b29f6 --- /dev/null +++ b/10631.cpp @@ -0,0 +1,258 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +typedef struct +{ + double a[16]; + int n; +} poly; +typedef struct +{ + double x, y; +} pt; + +double eval(poly P, double x) +{ + double r; + int i; + for (r = 0., i = P.n; i >= 0; i--) + { + r = r * x + P.a[i]; + } + return r; +} + +poly deriv(poly P) +{ + poly Q; + int i; + if (P.n < 1) + { + Q.a[0] = 0.; + Q.n = 0; + return Q; + } + Q.n = P.n - 1; + for (i = 1; i <= P.n; i++) + { + Q.a[i - 1] = i * P.a[i]; + } + return Q; +} + +poly pdiv(poly P, double x) +{ + poly Q; + double r; + int i; + Q.n = P.n - 1; + for (r = 0., i = P.n; i >= 0; i--) + { + Q.a[i] = r; + r = r * x + P.a[i]; + } + return Q; +} + +int sgn(double x) +{ + if (fabs(x) < EPS) + { + return 0; + } + return (x > 0.) ? 1 : -1; +} + +int cmp_d(const void *p, const void *q) +{ + return (*(double *)p > *(double *)q) ? 1 : -1; +} + +int bisec(double *r, poly P, double a, double b) +{ + double c; + int u, v, t; + if (a > b) + { + c = a; + a = b; + b = c; + } + if ((u = sgn(eval(P, a))) == 0) + { + *r = a; + return 1; + } + if ((v = sgn(eval(P, b))) == 0) + { + *r = b; + return 1; + } + if (u == v) + { + return 0; + } + while ((b - a) > 1e-13) + { + t = sgn(eval(P, c = (a + b) / 2.)); + if (t == 0) + { + *r = c; + return 1; + } + if (u == t) + { + a = c; + } + else + { + b = c; + } + } + return 0; +} + +int roots(poly P, double r[]) +{ + int i, qn; + poly Q; + double qr[16]; + while (P.n > 0 && fabs(P.a[P.n]) < EPS) + { + P.n--; + } + for (i = 0; i <= P.n; i++) + { + P.a[i] /= P.a[P.n]; + } + if (P.n < 1) + { + return 0; + } + if (P.n == 1) + { + r[0] = -P.a[0] / P.a[1]; + return 1; + } + if (P.n == 2) + { + double d = P.a[1] * P.a[1] - 4. * P.a[0] * P.a[2]; + if (fabs(d) < EPS) + { + r[0] = -P.a[1] / (2. * P.a[2]); + return 1; + } + if (d < 0) + { + return 0; + } + d = sqrt(d); + r[0] = (-P.a[1] - d) / (2. * P.a[2]); + r[1] = (-P.a[1] + d) / (2. * P.a[2]); + return 2; + } + /*printf("roots: P(x)=");for(i=P.n;i>=0;i--)printf(" + %.4fx^%d",P.a[i],i);printf("\n");*/ + Q = deriv(P); + qn = roots(Q, qr + 1); + qsort(qr + 1, qn, sizeof(qr[0]), &cmp_d); + qr[0] = -1e30; + qr[qn + 1] = 1e30; + for (i = 0; i <= qn; i++) + if (bisec(&r[0], P, qr[i], qr[i + 1])) + { + /*printf("found root: %.5f\n", r[0]);*/ + Q = pdiv(P, r[0]); + return 1 + roots(Q, r + 1); + } + return 0; +} + +int cmp_pt(const void *pp, const void *qq) +{ + pt *p = (pt *)pp, *q = (pt *)qq; + if (fabs(p->x - q->x) < 1e-5) + { + return (p->y > q->y) ? 1 : -1; + } + return (p->x > q->x) ? 1 : -1; +} + +int uniq(double r[], int n) +{ + int i, j, k; + for (i = k = 0; i < n; i++) + { + for (j = 0; j < k; j++) + if (fabs(r[i] - r[j]) < 1e-9) + { + break; + } + if (j < k) + { + continue; + } + r[k++] = r[i]; + } + return k; +} + +void solve(double a, double b, double p, double q, int an, int bn, int pn, int qn) +{ + poly P = {{-b * q, 2 * a * p - 2 * a * a + 2 * b * b, 0, 2 * a * p + 2 * a * a - 2 * b * b, b * q}, 4}; + double u[16], t; + pt r[16]; + int i, j, n; + n = roots(P, u); + n = uniq(u, n); + for (i = 0; i < n; i++) + { + t = 2 * atan(u[i]); + r[i].x = a * cos(t); + r[i].y = b * sin(t); + } + if (pn == 0) + { + r[n].x = 0; + r[n++].y = b; + r[n].x = 0; + r[n++].y = -b; + } + if (qn == 0) + { + r[n].x = -a; + r[n++].y = 0; + r[n].x = a; + r[n++].y = 0; + } + qsort(r, n, sizeof(r[0]), &cmp_pt); + for (i = 0; i < n; i++) + { + for (j = 0; j < i; j++) + if (fabs(r[i].x - r[j].x) < 1e-5 && fabs(r[i].y - r[j].y) < 1e-5) + { + break; + } + if (j < i) + { + continue; + } + printf("%.5f %.5f\n", r[i].x, r[i].y); + } +} + +int main() +{ + int a, b, p, q, t; + srand(53387); + for (t = 1; scanf("%d %d %d %d", &a, &b, &p, &q) == 4 && a >= 0; t++) + { + assert(1 <= a && a <= 300 && 1 <= b && b <= 300); + assert(-300 <= p && p <= 300 && -300 <= q && q <= 300); + printf("Case %d:\n", t); + solve(a, b, p, q, a, b, p, q); + } + return 0; +} diff --git a/10632.cpp b/10632.cpp new file mode 100644 index 0000000..4c9afe8 --- /dev/null +++ b/10632.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +const int Maxn = 55; +int n; +int col[Maxn][Maxn]; +int dirx[] = {-1, 1, -1, 1}; +int diry[] = {-1, 0, 0, 1}; +int dir[] = {7, 1, 9, 3}; +vector vec; +void dfs(int r, int c) +{ + //cout << r << " " << c << endl; + if (r == n && c == n) + { + return; + } + if (r == c) + { + int x = r + 1, y = c + 1; + while (col[r][c] != 2) + { + vec.push_back(3); + vec.push_back(7); + col[x][y]++; + col[x][y] %= 3; + col[r][c]++; + } + if (col[x][y] == 2 && r == n - 1 && c == n - 1) + { + return; + } + vec.push_back(3); + col[x][y]++; + col[x][y] %= 3; + while (x < n) + { + vec.push_back(1); + x++; + col[x][y]++; + col[x][y] %= 3; + } + dfs(n, c + 1); + return; + } + else + { + int x = r - 1, y = c; + while (col[r][c] != 2) + { + vec.push_back(9); + vec.push_back(1); + col[x][y]++; + col[x][y] %= 3; + col[r][c]++; + } + vec.push_back(9); + col[x][y]++; + col[x][y] %= 3; + dfs(x, y); + } +} +int Col[Maxn][Maxn]; +int main() +{ + while (scanf("%d", &n) != EOF) + { + if (n == 0) + { + break; + } + for (int i = 1; i <= n; ++i) + { + char str[Maxn]; + scanf("%s", str + 1); + for (int j = 1; j <= i; ++j) + if (str[j] == 'R') + { + Col[i][j] = col[i][j] = 0; + } + else if (str[j] == 'G') + { + Col[i][j] = col[i][j] = 1; + } + else + { + Col[i][j] = col[i][j] = 2; + } + } + vec.clear(); + dfs(n, 1); + if (col[n][n] != 2) + { + vec.clear(); + vec.push_back(1); + Col[n][1]++; + Col[n][1] %= 3; + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= i; ++j) + { + col[i][j] = Col[i][j]; + } + dfs(n, 1); + printf("%d 1\n", n - 1); + } + else + { + printf("%d 1\n", n); + } + assert(col[n][n] == 2); + for (int i = 0; i < vec.size(); ++i) + { + printf("%d", vec[i]); + } + puts(""); + } + return 0; +} diff --git a/10633.cpp b/10633.cpp index c4e10fc..a912db8 100644 --- a/10633.cpp +++ b/10633.cpp @@ -1,51 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int64 N, A, i; - scanf("%lld", &N); - if (N == 0) - break; - A = N + N/9; - if (N % 9 == 0) - printf("%lld %lld", A-1, A); +/* +10633 +Rare Easy Problem +*/ +typedef unsigned long long ss; +int main() +{ + ss d, m; + ss x, y; + while (scanf("%llu", &d) && d) + { + m = d % 9; + x = d * 10; + y = x / 9; + if (!m) + { + printf("%llu %llu\n", y - 1, y); + } else - printf("%lld", A); - printf("\n"); + { + printf("%llu\n", y); + } } return 0; } - diff --git a/10634.cpp b/10634.cpp new file mode 100644 index 0000000..13e577c --- /dev/null +++ b/10634.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +typedef long long ll; +#define N 1001 +ll table[N][N] = {0};//v,n +ll ans[N][N] = {0}; +ll tmp[N]; + +void make_table() +{ + rep(i, N) table[0][i] = 1; + rep(i, N) table[1][i] = 1;// + rep(i, N) + { + tmp[i] = 1; + table[2][i] = i + 1; + if (i) + { + tmp[i] = tmp[i - 1] + table[2][i]; + } + } + REP(v, 3, N) + { + tmp[0] = 1; + table[v][0] = 1; + REP(n, 1, N) + { + table[v][n] = tmp[n]; + tmp[n] = tmp[n - 1] + table[v][n]; + } + } + REP(v, 1, N) + { + ans[v][0] = table[v][0]; + ans[v][1] = table[v][1]; + for (int n = 3; n < N; n += 2) + { + ans[v][n] = ans[v][n - 2] + table[v][n]; + } + for (int n = 2; n < N; n += 2) + { + ans[v][n] = ans[v][n - 2] + table[v][n]; + } + } +} + +main() +{ + int n, v; + make_table(); + while (cin >> n >> v && v) + { + cout << ans[v][n] << endl; + } +} diff --git a/10635.cpp b/10635.cpp index fc99215..8605afe 100644 --- a/10635.cpp +++ b/10635.cpp @@ -1,15 +1,94 @@ -#include -#include -#include - -using namespace std; - -int main(){ - int testCases; - cin>>testCases; - for(int test=1; test<=testCases; test++){ - stringstream sstr; - sstr >> sstr; - //cout<<"Case "< + +using namespace std; + +/* +10635 +Prince and Princess +*/ + +#define maxn 62505 +int Add[maxn]; +int C[maxn], B[maxn]; +int N, P, Q, lcs; +void Set() +{ + int n, aj; + while (Q--) + { + cin >> n; + aj = Add[n]; + if (aj) + { + C[1] = aj; + lcs = 1; + return; + } + } +} +void Cal() +{ + int i, aj, n; + lcs = 0; + Q++; + Set(); + if (Q < 0) + { + Q = 0; + } + while (Q--) + { + cin >> n; + aj = Add[n]; + if (!aj) + { + continue; + } + for (i = lcs; i >= 0; i--) + { + if (C[i] < aj) + { + if (i + 1 > lcs || C[i + 1] > aj) + { + C[i + 1] = aj; + } + if (i + 1 > lcs) + { + lcs = i + 1; + } + break; + } + } + } + cout << lcs << endl; +} +void Free() +{ + int i; + for (i = 1; i <= P + 1; i++) + { + Add[B[i]] = 0; + } +} +int main() +{ + int ks, n, i, k = 1; + cin >> ks; + while (ks--) + { + cin >> N >> P >> Q; + for (i = 1; i <= P + 1; i++) + { + cin >> n; + B[i] = n; + Add[n] = i; + } + cout << "Case " << k++ << ": "; + Cal(); + if (ks) + { + Free(); + } + } + return 0; +} diff --git a/10637.cpp b/10637.cpp new file mode 100644 index 0000000..5f0ac54 --- /dev/null +++ b/10637.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +/* +Coprime +10637 +*/ +char F[101][101]; +int N, P; +int Gcd(int a, int b) +{ + return b ? Gcd(b, a % b) : a; +} +void Ini() +{ + int i, j, k; + for (i = 2; i < 101; i++) + { + for (j = i; j < 101; j++) + { + k = Gcd(i, j); + if (k > 1) + { + F[i][j] = F[j][i] = 1; + } + } + } +} +int A[102]; +void Print() +{ + int i; + printf("%d", A[1]); + for (i = 2; i <= P; i++) + { + printf(" %d", A[i]); + } + printf("\n"); +} +void Recur(int n, int sum, int level) +{ + int i, j, s, d, left, k; + A[level] = n; + if (level + 1 == P) + { + d = N - sum; + if (d <= n) + { + return; + } + for (k = 1; k <= level; k++) + if (F[d][A[k]]) + { + break; + } + if (k <= level) + { + return; + } + if (sum + d == N) + { + A[level + 1] = d; + Print(); + } + return; + } + s = (n <= 1) ? 1 : n + 1; + for (i = s;; i++) + { + d = P - level; + j = i + d - 1; + left = ((i + j) * d) / 2; + if (i == 1) + { + left = 1; + } + if (sum + left > N) + { + return; + } + for (k = 1; k <= level; k++) + if (F[i][A[k]] == 1) + { + break; + } + if (k <= level) + { + continue; + } + Recur(i, sum + i, level + 1); + } +} +void Cal() +{ + if (P == 1) + { + printf("%d\n", N); + return; + } + if (P == N) + { + printf("1"); + for (int i = 1; i < P; i++) + { + printf(" 1"); + } + printf("\n"); + } + Recur(0, 0, 0); +} +int main() +{ + int ks, k = 1; + Ini(); + scanf("%d", &ks); + while (ks--) + { + scanf("%d%d", &N, &P); + printf("Case %d:\n", k++); + Cal(); + } + return 0; +} diff --git a/10638.cpp b/10638.cpp new file mode 100644 index 0000000..022d485 --- /dev/null +++ b/10638.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +typedef long long ll; + +const int N = 100010; +const int M = 200010; +const int mod = 1e6 + 7; +const int inf = 0x3f3f3f3f; +const ll oo = 0x3f3f3f3f3f3f3f3fll; +const double eps = 1e-6; + +#define pb push_back +#define sz(x) ((x).size()) + +int n, a[N]; + +int dfs(const vector &A, int a, vector B) +{ + if (a >= sz(A)) + { + return 0; + } + int ret = inf; + for (int i = 0; i < (1 << sz(B)); i++) + { + int sum = 0, mx = 0, c = 0; + vector T; + for (int j = 0; j < sz(B); j++) + { + if (i & (1 << j)) + { + sum += B[j]; + mx = max(mx, B[j]); + c++; + } + else if (B[j]) + { + T.pb(B[j]); + } + } + if (sum >= A[a] && mx > sum - A[a]) + { + if (sum - A[a]) + { + T.pb(sum - A[a]); + } + ret = min(ret, dfs(A, a + 1, T) + c); + } + } + return ret; +} + +int main() +{ + while (scanf("%d", &n), n) + { + ll sum = 0; + for (int i = 0; i < n; i++) + { + int x, y; + scanf("%d.%d", &x, &y); + a[i] = x * 100 + y; + sum += a[i]; + } + int l = sum / n; + int r = sum / n + 1; + int k = sum % n; + int ret = inf; + for (int i = 0; i < (1 << n); i++) + { + int c = 0; + for (int j = 0; j < n; j++) + if (i & (1 << j)) + { + c++; + } + if (c != k) + { + continue; + } + vector A, B; + for (int j = 0; j < n; j++) + if (i & (1 << j)) + { + if (a[j] > r) + { + A.pb(a[j] - r); + } + else if (a[j] < r) + { + B.pb(r - a[j]); + } + } + else + { + if (a[j] > l) + { + A.pb(a[j] - l); + } + else if (a[j] < l) + { + B.pb(l - a[j]); + } + } + //for(int j = 0; j < sz(A); j++) + //cout << A[j] << " ";cout << endl; + //for(int j = 0; j < sz(B); j++) + //cout << B[j] << " ";cout << endl; + ret = min(ret, dfs(A, 0, B)); + } + cout << ret << endl; + } + return 0; +} diff --git a/1064.cpp b/1064.cpp new file mode 100644 index 0000000..62860eb --- /dev/null +++ b/1064.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +#define EPS 1e-12 +#define MAXN 1100 +#define MAXM 1100000 +#define INF 0x3f3f3f3f +#define PB push_back +#define MP make_pair + +typedef struct {int len, sta;} node; +node r[10], t[10]; +bool f[5]; +int nxt[10][70000], pck[1010][3], mp[120][5]; + +int main() +{ + int n, m, i, j, k, l, p, q, ans, tmp, now, cnt = 1; + memset(f, true, sizeof(f)); + i = 0; + for (j = 0; j < 5; j++) + { + f[j] = false; + for (k = 0; k < 5; k++) + { + if (f[k]) + { + f[k] = false; + for (l = 0; l < 5; l++) + { + if (f[l]) + { + f[l] = false; + for (p = 0; p < 5; p++) + { + if (f[p]) + { + f[p] = false; + for (q = 0; q < 5; q++) + { + if (f[q]) + { + mp[i][0] = j; + mp[i][1] = k; + mp[i][2] = l; + mp[i][3] = p; + mp[i][4] = q; + i++; + } + } + f[p] = true; + } + } + f[l] = true; + } + } + f[k] = true; + } + } + f[j] = true; + } + + while (scanf("%d %d", &n, &m) != EOF) + { + if (n == 0 && m == 0) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%d", &r[i].len); + r[i].sta = 1; + } + for (i = 0; i < m; i++) + { + scanf("%d %d %d", &pck[i][0], &pck[i][1], &pck[i][2]); + pck[i][0]--; + } + ans = 1000000000; + for (i = 0; i < 120; i++) + { + memcpy(t, r, sizeof(node) * 10); + memset(nxt, 0, sizeof(nxt)); + tmp = now = 0; + for (j = 0; j < n; j++) + { + f[j] = true; + } + for (; j < 5; j++) + { + f[j] = false; + } + for (j = 0; j < 5; j++) + { + if (f[mp[i][j]]) + { + p = mp[i][j]; + break; + } + } + for (k = 0; k < m; k++) + { + nxt[pck[k][0]][pck[k][1]] = pck[k][2]; + if (pck[k][0] == p && pck[k][1] == t[p].sta) + { + for (q = pck[k][2] + 1; nxt[p][q]; q = nxt[p][q] + 1) + ; + now -= (q - pck[k][2] - 1); + t[p].sta = q; + if (q <= t[p].len) + { + continue; + } + f[p] = false; + while (1) + { + for (j += 1; j < 5; j++) + { + if (f[mp[i][j]]) + { + p = mp[i][j]; + break; + } + } + if (j == 5) + { + break; + } + for (q = 1; nxt[p][q]; q = nxt[p][q] + 1) + ; + now -= (q - t[p].sta); + t[p].sta = q; + if (q <= t[p].len) + { + break; + } + f[p] = false; + } + } + else + { + now += (pck[k][2] - pck[k][1] + 1); + tmp = max(tmp, now); + } + } + ans = min(ans, tmp); + } + printf("Case %d: %d\n\n", cnt++, ans); + } + return 0; +} diff --git a/10640.cpp b/10640.cpp new file mode 100644 index 0000000..96edb54 --- /dev/null +++ b/10640.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int limit = 200, limit2 = 400; +double lower, upper; + +inline int fn(double x, int planes, int total) +{ + double n = (double)upper / lower, fuel, tmp; + while (true) + { + fuel = n - (1 - x); + tmp = (double)fuel / (planes + 1); + x = x + tmp, planes++, total++; + if (x >= .999) + { + return planes; + } + if (total > limit2) + { + return 20000; + } + } +} + +inline int solve() +{ + lower = 1 - upper; + double tank; + int ret = 999999999; + for (int i = 1; i < limit; i++) + { + tank = (double)(i - 1) / (i + 1); + tank = (double)(tank * upper) / (lower); + int tmp = fn(tank, 1, i); + ret = min(ret, i + tmp - 1); + } + tank = (double)(3 - 1) / (3 + 1); + if (ret > 10000) + { + ret = -1; + } + return ret; +} + +int main() +{ + int cases, x, y, p = 1; + scanf("%d", &cases); + while (cases--) + { + scanf("%d %d", &x, &y); + if (x >= y) + { + printf("Case %d: %d\n", p++, 1); + continue; + } + upper = (double)x / y; + printf("Case %d: %d\n", p++, solve()); + } + return 0; +} diff --git a/10641.cpp b/10641.cpp new file mode 100644 index 0000000..65d7229 --- /dev/null +++ b/10641.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +typedef pair pii; +typedef unsigned long long ll; + +const double eps = 1e-6; +const int N = 1010; +inline int sign(double x) +{ + return x < -eps ? -1 : x > eps; +} + +struct Point +{ + double x, y; + Point() {} + Point(double x, double y) : x(x), y(y) {} + Point operator+(const Point &o) const + { + return Point(x + o.x, y + o.y); + } + Point operator-(const Point &o) const + { + return Point(x - o.x, y - o.y); + } + Point operator/(double o) + { + return Point(x / o, y / o); + } + Point operator*(double o) + { + return Point(x * o, y * o); + } + double det(const Point &o) const + { + return x * o.y - y * o.x; + } + + void rd() + { + scanf("%lf %lf", &x, &y); + } + void show() + { + cout << x << ' ' << y << endl; + } +}; + +struct Range +{ + int l, r, c; + bool operator<(const Range &o) const + { + return r < o.r; + } +} rg[N]; + +int n, m; +Point p[N], l[N]; +Point o; +int c[N]; +bool flag[100]; + +inline bool check(Point light, Point p1, Point p2) +{ + return sign((light - p1).det(p2 - p1) * (o - p1).det(p2 - p1)) < 0; +} +void DP() +{ + int dp[100]; + int ans = ~0U >> 2; + sort(rg, rg + m); + for (int s = 0; s < n; ++s) + { + fill(dp, dp + 100, ~0U >> 2); + vector cur; + for (int i = 0; i < m; ++i) + if (rg[i].r >= s) + { + cur.push_back(rg[i]); + } + for (int i = 0; i < cur.size(); ++i) + { + int l = cur[i].l, r = cur[i].r, c = cur[i].c; + if (l <= s) + { + dp[r] = min(dp[r], c); + } + for (int j = max(l - 1, s); j <= r; ++j) + { + dp[r] = min(dp[r], dp[j] + c); + } + } + for (int i = s + n - 1; i < n + n; ++i) + { + ans = min(ans, dp[i]); + } + } + if (ans == ~0U >> 2) + { + puts("Impossible."); + } + else + { + printf("%d\n", ans); + } +} +int main() +{ + while (scanf("%d", &n), n) + { + o = Point(0, 0); + for (int i = 0; i < n; ++i) + { + p[i].rd(), o = o + p[i]; + } + o = o / n; + p[n] = p[0]; + scanf("%d", &m); + for (int i = 0; i < m; ++i) + { + l[i].rd(), scanf("%d", c + i); + } + for (int i = 0; i < m; ++i) + { + memset(flag, false, sizeof(flag)); + for (int j = 0; j < n; ++j) + { + Point a = p[j], b = p[j + 1]; + if (check(l[i], a, b)) + { + flag[j] = true; + } + } + if (flag[0] && flag[n - 1]) + { + int l = n - 1, r = n; + while (flag[l]) + { + rg[i].l = l, l--; + } + while (flag[r - n]) + { + rg[i].r = r, r++; + } + } + else + { + int l = 0, r = n - 1; + while (!flag[l]) + { + l++; + } + rg[i].l = l; + while (!flag[r]) + { + r--; + } + rg[i].r = r; + } + rg[i].c = c[i]; + } + DP(); + } + return 0; +} diff --git a/10642.cpp b/10642.cpp new file mode 100644 index 0000000..62a7edf --- /dev/null +++ b/10642.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int kase, n; + long long x1, y1, x2, y2, dis1, dis2; + cin >> n; + for (kase = 1; kase <= n; kase++) + { + cin >> y1 >> x1 >> y2 >> x2; + dis1 = (x1 + y1 - 1) * (x1 + y1) / 2 + x1 + y1 + y1; + dis2 = (x2 + y2 - 1) * (x2 + y2) / 2 + x2 + y2 + y2; + cout << "Case " << kase << ": " << dis2 - dis1 << endl; + } + return 0; +} diff --git a/10643.cpp b/10643.cpp new file mode 100644 index 0000000..b6a74a7 --- /dev/null +++ b/10643.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +void C(int n, int k) +{ + if (k > n - k) + { + k = n - k; + } + int temp[400], i, j; + memset(temp, 0, sizeof(temp)); + temp[0] = 1; + for (i = n; i > n - k; i--) + { + int carry = 0; + for (j = 0; j < 400; j++) + { + int t = temp[j] * i + carry; + carry = t / 10000; + temp[j] = t % 10000; + } + } + for (i = 2; i <= k; i++) + { + int mod = 0; + for (j = 399; j >= 0; j--) + { + int t = mod * 10000 + temp[j]; + mod = t % i; + temp[j] = t / i; + } + } + int p; + for (p = 399; temp[p] == 0; p--) + ; + printf("%d", temp[p]); + for (p--; p >= 0; p--) + { + printf("%04d", temp[p]); + } + printf("\n"); +} +int main() +{ + int t, m; + scanf("%d", &t); + for (int c = 1; c <= t; c++) + { + scanf("%d", &m); + m /= 2; + m--; + printf("Case %d: ", c); + C(2 * m + 1, m + 1); + } +} diff --git a/10644.cpp b/10644.cpp new file mode 100644 index 0000000..c517e92 --- /dev/null +++ b/10644.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int m[1001][1001]; +int main() +{ + for (int i = 2; i <= 1000; i++) + { + if (i % 3 == 0) + { + for (int j = 1; j <= 500; j++) + { + m[i][2 * j] = 1; + } + if (i != 3) + for (int j = (!(i & 1)) ? 1 : 2; j <= 499; j++) + { + m[i][2 * j + 1] = 1; + } + } + else + { + for (int j = 2; j <= 333; j++) + { + m[i][3 * j] = 1; + } + if (!(i & 1)) + { + m[i][3] = 1; + } + } + } + for (int i = 1; i <= 1000; i++) + for (int j = 1; j <= 1000; j++) + { + m[i][j] = m[i][j] + m[i - 1][j] + m[i][j - 1] - m[i - 1][j - 1]; + } + int t; + scanf("%d", &t); + while (t--) + { + int l1, l2, w1, w2, c = 0; + scanf("%d%d%d%d", &l1, &l2, &w1, &w2); + if (l1 > l2) + { + int t = l1; + l1 = l2; + l2 = t; + } + if (w1 > w2) + { + int t = w1; + w1 = w2; + w2 = t; + } + printf("%d\n", m[w2][l2] - m[w2][l1 - 1] - m[w1 - 1][l2] + m[w1 - 1][l1 - 1]); + } +} diff --git a/10645.cpp b/10645.cpp new file mode 100644 index 0000000..ee92e0b --- /dev/null +++ b/10645.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +static int dp[101][21][51][3], last[101][21][51][3], last2[101][21][51][3], cost[51], benefit[51]; +static char chk[101][21][51][3]; +static int n, m, o, i, j, k, l, p, q, b, ans; + +inline void backtrack(int i, int j, int k, int l) +{ + if (j > 1) + { + backtrack(i - cost[k - 1], j - 1, last[i][j][k][l], last2[i][j][k][l]); + printf(" %d", k); + } + else + { + printf("%d", k); + } +} + +int main() +{ + while (scanf("%d%d%d", &n, &m, &o) == 3) + { + if (!(n + m + o)) + { + break; + } + for (i = 0; i < m; i++) + { + scanf("%d%d", &cost[i], &benefit[i]); + } + memset(chk, 0, sizeof(chk)); + chk[0][0][0][0] = 1; + for (i = 0; i < n; i++) + { + for (j = 1; j <= m; j++) + { + b = benefit[j - 1] + benefit[j - 1]; + for (k = o; k >= cost[j - 1]; k--) + { + for (l = 0; l <= m; l++) + { + if (j != l) + { + if (chk[k - cost[j - 1]][i][l][0]) + { + if (dp[k - cost[j - 1]][i][l][0] + b > dp[k][i + 1][j][0] || !chk[k][i + 1][j][0]) + { + chk[k][i + 1][j][0] = 1; + dp[k][i + 1][j][0] = dp[k - cost[j - 1]][i][l][0] + b; + last[k][i + 1][j][0] = l; + last2[k][i + 1][j][0] = 0; + } + } + if (chk[k - cost[j - 1]][i][l][1]) + { + if (dp[k - cost[j - 1]][i][l][1] + b > dp[k][i + 1][j][0] || !chk[k][i + 1][j][0]) + { + chk[k][i + 1][j][0] = 1; + dp[k][i + 1][j][0] = dp[k - cost[j - 1]][i][l][1] + b; + last[k][i + 1][j][0] = l; + last2[k][i + 1][j][0] = 1; + } + } + if (chk[k - cost[j - 1]][i][l][2]) + { + if (dp[k - cost[j - 1]][i][l][2] + b > dp[k][i + 1][j][0] || !chk[k][i + 1][j][0]) + { + chk[k][i + 1][j][0] = 1; + dp[k][i + 1][j][0] = dp[k - cost[j - 1]][i][l][2] + b; + last[k][i + 1][j][0] = l; + last2[k][i + 1][j][0] = 2; + } + } + } + else + { + if (chk[k - cost[j - 1]][i][l][0]) + { + chk[k][i + 1][j][1] = 1; + dp[k][i + 1][j][1] = dp[k - cost[j - 1]][i][l][0] + benefit[j - 1]; + last[k][i + 1][j][1] = l; + last2[k][i + 1][j][1] = 0; + } + if (chk[k - cost[j - 1]][i][l][1]) + { + chk[k][i + 1][j][2] = 1; + dp[k][i + 1][j][2] = dp[k - cost[j - 1]][i][l][1]; + last[k][i + 1][j][2] = l; + last2[k][i + 1][j][2] = 1; + } + if (chk[k - cost[j - 1]][i][l][2]) + { + if (dp[k - cost[j - 1]][i][l][2] > dp[k][i + 1][j][2] || !chk[k][i + 1][j][2]) + { + chk[k][i + 1][j][2] = 1; + dp[k][i + 1][j][2] = dp[k - cost[j - 1]][i][l][2]; + last[k][i + 1][j][2] = l; + last2[k][i + 1][j][2] = 2; + } + } + } + } + } + } + } + for (i = 0, ans = -1; i <= o; i++) + { + for (j = 0; j < m; j++) + { + if (chk[i][n][j][0]) + { + if (dp[i][n][j][0] > ans) + { + ans = dp[i][n][j][0]; + k = i; + l = j; + p = 0; + } + } + if (chk[i][n][j][1]) + { + if (dp[i][n][j][1] > ans) + { + ans = dp[i][n][j][1]; + k = i; + l = j; + p = 1; + } + } + if (chk[i][n][j][2]) + { + if (dp[i][n][j][2] > ans) + { + ans = dp[i][n][j][2]; + k = i; + l = j; + p = 2; + } + } + } + } + if (ans == -1) + { + printf("0.0\n"); + } + else + { + if (ans % 2) + { + printf("%d.5\n", ans / 2); + } + else + { + printf("%d.0\n", ans / 2); + } + backtrack(k, n, l, p); + printf("\n"); + } + printf("\n"); + } + return 0; +} diff --git a/10646.cpp b/10646.cpp new file mode 100644 index 0000000..6583803 --- /dev/null +++ b/10646.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int card2int(const string &card) +{ + switch (card[0]) + { + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return int(card[0] - '0'); + break; + default: + return 10; + break; + } +} + +int main() +{ + int t; + int y; + string c; + vector cards; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + y = 0; + cards.clear(); + for (int i = 0; i < 52; i++) + { + cin >> c; + cards.push_back(c); + } + reverse(cards.begin(), cards.end()); + for (int i = 0; i < 3; i++) + { + int v = card2int(cards[25]); + y += v; + cards.erase(cards.begin() + 25, cards.begin() + 26 + (10 - v)); + } + reverse(cards.begin(), cards.end()); + cout << "Case " << casenum + 1 << ": " << cards[y - 1] << endl; + } + return 0; +} diff --git a/10647.cpp b/10647.cpp new file mode 100644 index 0000000..66f7586 --- /dev/null +++ b/10647.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +typedef unsigned long long ULL; + +// lo a, hi b. +// min penalty = a*x^2 + (1-x)^2*b for x in [0,1]: +// ax^2 + (x^2-2x+1)b = (a+b)x^2 - 2bx + b +// => min at x = 2b/2(a+b) = b/(a+b) (parabola min of f(x)=ax^2+bx+c is -b/2a) +double min_x(int lo, int hi, long A, long B, double &val) +{ + long gap = hi - lo; + double x = gap * B / (A + (double)B); + val = A * x * x + B * (gap - x) * (gap - x); + return lo + x; +} + +int main() +{ + int n, a[10000]; + while (scanf("%d", &n) == 1, n) + { + ULL hi = 0ULL; + scanf("%d", &a[0]); + for (int i = 1; i < n; ++i) + { + scanf("%d", &a[i]); + ULL hse_diff = (a[i] - a[i - 1]); + hi += hse_diff * hse_diff * (n - i); + } + + // find best hse + ULL lo = 0; + double best_x = a[0]; + double best_hse_price = hi; + for (int i = 1; i < n; ++i) + { + // try in space up to next hse + ULL hse_diff = (a[i] - a[i - 1]); + hi -= hse_diff * hse_diff * (n - i); + double mid, x = min_x(a[i - 1], a[i], i, n - i, mid), price = lo + hi + mid; + if (price < best_hse_price - 1e-6) + { + best_hse_price = price; + best_x = x; + } + lo += i * hse_diff * hse_diff; + } + + printf("%.3lf\n", best_x); + } + return 0; +} diff --git a/10648.cpp b/10648.cpp new file mode 100644 index 0000000..3a0c82c --- /dev/null +++ b/10648.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +/** + Stirling Number of the Second Kind + - number of ways to partition a set of n elements + - into m non-empty sets +*/ + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) + +static int n, m, tc = 1; + +int main() +{ + for (;;) + { + scanf("%d", &n); + if (n == -1) + { + break; + } + scanf("%d", &m); + double p[n + 1][m + 1]; + FOREQ(i, 0, n) + { + FOREQ(j, 0, m) + { + p[i][j] = 0.0f; + } + } + p[0][0] = 1.0f; + FOREQ(i, 1, n) + { + FOREQ(j, 0, m) + { + if (m == 0) + { + m = 1; + } + if (m != j) + { + p[i][j] += p[i - 1][j] * (m - j) / (double)m; + } + if (j >= 1) + { + p[i][j] += p[i - 1][j - 1] * j / (double)m; + } + } + } + printf("Case %d: %.7f\n", tc++, 1 - p[n][m]); + } + return 0; +} diff --git a/10649.cpp b/10649.cpp new file mode 100644 index 0000000..ea62fe1 --- /dev/null +++ b/10649.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + double r, c, d, x, y; + while (scanf("%lf%lf", &r, &d) == 2) + { + if (r == 0 && d == 0) + { + break; + } + if (d == 0) + { + printf("%.6lf\n", r / sqrt(2)); + continue; + } + r /= 2; + y = -d / sqrt(2.0); + if (r + y <= 0) + { + printf("INCORRECT INFORMATION !!!\n"); + continue; + } + x = sqrt(r * r - y * y); + x += y; + printf("%.6lf\n", (r * r - x * x) / d + 0.0000000001); + } + return 0; +} diff --git a/10650.cpp b/10650.cpp new file mode 100644 index 0000000..0455378 --- /dev/null +++ b/10650.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAXN 32002 + +char seiv[MAXN]; +int Prime[4000], K, st, L, U; + +struct ss +{ + int inv; + int fre; + int st, en; + int A[100]; +} set_[200]; + +void Prime_table() +{ + int i, j; + K = 1; + for (i = 2; i * i < MAXN;) + { + for (j = i + i; j < MAXN; j += i) + { + seiv[j] = 1; + } + for (i++; seiv[i]; i++) + ; + } + Prime[0] = 2; + for (i = 3; i < MAXN; i += 2) + { + if (seiv[i] == 0) + { + Prime[K++] = i; + } + } +} + +void Determinate() +{ + int i, j, dif; + st = 0; + for (i = 0; i < K - 2;) + { + dif = Prime[i + 1] - Prime[i]; + if (Prime[i + 1] + dif == Prime[i + 2]) + { + set_[st].st = Prime[i]; + set_[st].inv = dif; + set_[st].fre = 0; + j = 1; + set_[st].A[0] = Prime[i]; + while (Prime[i + 1] - Prime[i] == dif) + { + set_[st].A[j++] = Prime[i + 1]; + i++; + } + set_[st].fre = j; + set_[st].en = Prime[i]; + st++; + } + else + { + i++; + } + } +} + +void Print() +{ + int i, j, p, q; + for (i = 0; i < st; i++) + { + if (set_[i].st >= L && set_[i].en <= U) + { + printf("%d", set_[i].A[0]); + for (j = 1; j < set_[i].fre; j++) + { + printf(" %d", set_[i].A[j]); + } + printf("\n"); + } + } +} + +int main() +{ + Prime_table(); + Determinate(); + int temp; + while (1) + { + scanf("%d%d", &L, &U); + if (!L && !U) + { + break; + } + if (L > U) + { + temp = L; + L = U; + U = temp; + } + Print(); + } + return 0; +} diff --git a/10651.cpp b/10651.cpp new file mode 100644 index 0000000..406e05e --- /dev/null +++ b/10651.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define MAXN 1500 + +struct ss +{ + char str[15]; + int len; +} Q[MAXN]; + +char ini[15]; +int qh, qt, min_; + +void Push(char temp[], int len) +{ + strcpy(Q[qh].str, temp); + Q[qh++].len = len; + qh %= MAXN; +} +void BFS(int t) +{ + int i, j, pre; + char xx[15], yy[15]; + qh = qt = 0; + min_ = t; + Push(ini, t); + while (qh != qt) + { + strcpy(xx, Q[qt].str); + pre = Q[qt++].len; + if (pre < min_) + { + min_ = pre; + } + for (i = 0; i < 12; i++) + { + if (xx[i] == 'o') + { + continue; + } + if (i <= 9 && xx[i + 1] == 'o' && xx[i + 2] == 'o') + { + for (j = 0; j < i; j++) + { + yy[j] = xx[j]; + } + yy[j] = 'o'; + yy[i + 1] = yy[i + 2] = '-'; + for (j = i + 3; j < 12; j++) + { + yy[j] = xx[j]; + } + yy[12] = '\0'; + Push(yy, pre - 1); + } + if (i >= 2 && xx[i - 1] == 'o' && xx[i - 2] == 'o') + { + for (j = 11; j > i; j--) + { + yy[j] = xx[j]; + } + yy[j] = 'o'; + yy[i - 1] = yy[i - 2] = '-'; + for (j = 0; j < i - 2; j++) + { + yy[j] = xx[j]; + } + yy[12] = '\0'; + Push(yy, pre - 1); + } + } + } +} +void cal() +{ + int i, t = 0; + for (i = 0; ini[i]; i++) + { + if (ini[i] == 'o') + { + t++; + } + } + if (t == 12 || t == 0 || t == 1) + { + printf("%d\n", t); + return; + } + BFS(t); + printf("%d\n", min_); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%s", ini); + cal(); + } + return 0; +} diff --git a/10652.cpp b/10652.cpp new file mode 100644 index 0000000..19df108 --- /dev/null +++ b/10652.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +typedef struct +{ + double x, y; +} Pt; +Pt P[10000], CH[10000]; + +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} + +double calc_area(int m) +{ + double sum = 0; + int i; + for (i = 0; i < m - 1; i++) + { + sum += (CH[i].x * CH[i + 1].y - CH[i].y * CH[i + 1].x); + } + return fabs(sum / 2); +} + +bool cmp(Pt a, Pt b) +{ + if (a.x != b.x) + { + return a.x < b.x; + } + return a.y < b.y; +} + +double monotone_chain(int n) +{ + sort(P, P + n, cmp); + int m = 0, i, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) + { + m--; + } + CH[m++] = P[i]; + } + for (i = n - 1, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(CH[m - 2], CH[m - 1], P[i]) <= 0) + { + m--; + } + CH[m++] = P[i]; + } + return calc_area(m); +} + +int main() +{ + int t, n, m; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + double x, y, w, h, j, k, l, sum = 0; + m = 0; + while (n--) + { + scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &j); + j = j / 180 * M_PI; + sum += w * h; + l = sqrt(w * w + h * h) / 2; + k = M_PI - (j + atan(w / h)); + P[m].x = x - l * sin(k), P[m].y = y + l * cos(k), m++; + P[m].x = 2 * x - P[m - 1].x, P[m].y = 2 * y - P[m - 1].y, m++; + k = atan(h / w) * 2 + M_PI / 2 - k; + P[m].x = x - l * cos(k), P[m].y = y + l * sin(k), m++; + P[m].x = 2 * x - P[m - 1].x, P[m].y = 2 * y - P[m - 1].y, m++; + } + double area = monotone_chain(m); + printf("%.1lf %%\n", sum * 100 / area); + } + return 0; +} diff --git a/10653.cpp b/10653.cpp new file mode 100644 index 0000000..61a18b7 --- /dev/null +++ b/10653.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int B[1005][1005] = {0}, D[1005][1005], X[1000005], Y[1000005], ptr, len, SX[4] = {0, 1, 0, -1}, SY[4] = {1, 0, -1, 0}; +int main() +{ + int t, r, c, rn, i, j, cn, x, y, nx, ny, ex, ey; + for (t = 1; scanf("%d%d", &r, &c) == 2; t++) + { + if ((r | c) == 0) + { + break; + } + scanf("%d", &rn); + while (rn--) + { + scanf("%d%d", &i, &cn); + while (cn--) + { + scanf("%d", &j); + B[i][j] = t; + } + } + scanf("%d%d%d%d", &X, &Y, &ex, &ey); + D[X[0]][Y[0]] = 0, B[X[0]][Y[0]] = t, len = 1; + for (ptr = 0; ptr < len; ptr++) + { + x = X[ptr], y = Y[ptr]; + if (x == ex && y == ey) + { + break; + } + for (i = 0; i < 4; i++) + { + nx = x + SX[i], ny = y + SY[i]; + if (nx < 0 || ny < 0 || nx >= r || ny >= c || B[nx][ny] == t) + { + continue; + } + B[nx][ny] = t, D[nx][ny] = D[x][y] + 1; + X[len] = nx, Y[len] = ny, len++; + } + } + printf("%d\n", D[x][y]); + } + return 0; +} diff --git a/10654.cpp b/10654.cpp new file mode 100644 index 0000000..8395f55 --- /dev/null +++ b/10654.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int arr[105][8]; +char ch[8][4] = {"NNN", "NNY", "NYN", "NYY", "YNN", "YNY", "YYN", "YYY"}; +int re[8], cmp[8], t[4]; + +int main() +{ + int ca, n; + while (scanf("%d", &ca) != EOF) + { + while (ca--) + { + for (int i = 0; i < 8; i++) + { + re[i] = i; + } + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < 8; j++) + { + scanf("%d", &arr[i][j]); + } + for (int i = n - 1; i >= 0; i--) + { + for (int j = 0; j < 8; j++) + { + for (int k = 0; k < 3; k++) + { + t[k] = re[1 << k ^ j]; + } + cmp[j] = t[0]; + if (arr[i][cmp[j]] > arr[i][t[1]]) + { + cmp[j] = t[1]; + } + if (arr[i][cmp[j]] > arr[i][t[2]]) + { + cmp[j] = t[2]; + } + } + for (int j = 0; j < 8; j++) + { + re[j] = cmp[j]; + } + } + printf("%s\n", ch[re[0]]); + } + } + return 0; +} diff --git a/10655.cpp b/10655.cpp new file mode 100644 index 0000000..2837963 --- /dev/null +++ b/10655.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +long long int B[2][2], C[2][2], T[2][2]; +void ini(int p, int q) +{ + B[0][0] = B[1][1] = 1, B[0][1] = B[1][0] = 0; + C[0][0] = 0, C[0][1] = 1, C[1][0] = -q, C[1][1] = p; +} +void mult(long long int M1[2][2], long long int M2[2][2]) +{ + T[0][0] = M1[0][0] * M2[0][0] + M1[0][1] * M2[1][0]; + T[0][1] = M1[0][0] * M2[0][1] + M1[0][1] * M2[1][1]; + T[1][0] = M1[1][0] * M2[0][0] + M1[1][1] * M2[1][0]; + T[1][1] = M1[1][0] * M2[0][1] + M1[1][1] * M2[1][1]; + M1[0][0] = T[0][0], M1[0][1] = T[0][1]; + M1[1][0] = T[1][0], M1[1][1] = T[1][1]; +} +void print(long long int M1[2][2]) +{ + printf("%10lld %10lld\n%10lld %10lld\n", M1[0][0], M1[0][1], M1[1][0], M1[1][1]); +} +int main() +{ + int i, j, p, q, n; + while (scanf("%d%d%d", &p, &q, &n) == 3) + { + if (n-- == 0) + { + printf("2\n"); + continue; + } + for (ini(p, q); n; n >>= 1) + { + if (n & 1) + { + mult(B, C); + } + mult(C, C); + } + printf("%lld\n", 2 * B[1][0] + p * B[1][1]); + } + return 0; +} diff --git a/10656.cpp b/10656.cpp index a46e0df..1f95116 100644 --- a/10656.cpp +++ b/10656.cpp @@ -1,22 +1,39 @@ -#include +#include -int main() { - while (true) { - int N, I, F = 0; - scanf("%d", &N); - if (!N) break; - - while (N--) { - scanf("%d", &I); - if (I > 0) { - if (F) printf(" "); - printf("%d", I); - F = 1; +using namespace std; + +int a[1001]; +int main() +{ + int i, j, k, n; + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + k = 1; + for (j = 0; j < n; j++) + { + scanf("%d", &i); + if (i) + { + if (k) + { + printf("%d", i); + } + else + { + printf(" %d", i); + } + k = 0; } } - if (!F) printf("0"); + if (k) + { + printf("0"); + } printf("\n"); } return 0; } - diff --git a/10658.cpp b/10658.cpp new file mode 100644 index 0000000..e42e528 --- /dev/null +++ b/10658.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + int n; + scanf("%d", &n); + unsigned long long ans = 0; + for (int i = 0; i < n; i++) + { + ans *= 2; + if (i % 3) + { + ans++; + } + } + printf("%llu\n", ans); + } +} diff --git a/10659.cpp b/10659.cpp new file mode 100644 index 0000000..1b24c8f --- /dev/null +++ b/10659.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +int fit_line(string &s, int x, int size) +{ + int result = 1; + string tmp; + deque q; + istringstream iss(s); + copy(istream_iterator(iss), istream_iterator(), back_inserter(q)); + int current_length = 0; + while (!q.empty()) + { + tmp = q.front(); + q.pop_front(); + if (tmp.size() * size > x) + { + result = -1; + break; + } + // try fitting the word + if (current_length + tmp.size() * size <= x) + { + current_length += tmp.size() * size; + } + else + { + result++; + current_length = tmp.size() * size; + } + // try fitting a space + if (!q.empty()) + { + if (current_length + size <= x) + { + current_length += size; + } + else + { + result++; + current_length = 0; + } + } + } + return result; +} + +bool can_fit(vector &lines, int x, int y, int size) +{ + bool result = true; + int rows = 0, number_of_rows; + for (int i = 0, sz = lines.size(); i < sz; i++) + { + number_of_rows = fit_line(lines[i], x, size); + if (number_of_rows == -1) + { + result = false; + break; + } + rows += number_of_rows; + } + if (result) + { + result = rows * size <= y; + } + return result; +} + +int main() +{ + int n, m, x, y; + string input; + vector lines; + cin >> n; + while (n--) + { + cin >> m; + cin.ignore(100, '\n'); + lines.clear(); + while (m--) + { + getline(cin, input); + lines.push_back(input); + } + cin >> x >> y; + int result = -1; + for (int i = 8; i < 29; i++) + { + if (can_fit(lines, x, y, i)) + { + result = i; + } + else + { + break; + } + } + if (result == -1) + { + cout << "No solution" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/10660.cpp b/10660.cpp new file mode 100644 index 0000000..d66a3cf --- /dev/null +++ b/10660.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define FORI(i, s, n) for (int i = s; i < n; ++i) +#define FOR(i, n) FORI(i, 0, n) + +int ans[8], p[32][3], v[8][2]; +int min_sum, n; + +int dist(int x1, int y1, int x2, int y2) +{ + return abs(x1 - x2) + abs(y1 - y2); +} + +int min(int a, int b) +{ + return a < b ? a : b; +} + +void bt(int x, int cnt) +{ + if (cnt == 5) + { + int sum = 0; + FOR(i, n) + { + int md = 1 << 30; + FOR(j, 5) + md = min(md, dist(v[j][0], v[j][1], p[i][0], p[i][1])); + sum += md * p[i][2]; + } + if (sum < min_sum) + { + min_sum = sum; + FOR(i, 5) + ans[i] = v[i][0] * 5 + v[i][1]; + } + return; + } + FORI(i, x, 25) + { + v[cnt][0] = i / 5; + v[cnt][1] = i % 5; + bt(i + 1, cnt + 1); + } +} + +int main() +{ + int t; + scanf("%d", &t); + while (t-- && scanf("%d", &n) == 1) + { + FOR(i, n) + scanf("%d %d %d", &p[i][0], &p[i][1], &p[i][2]); + min_sum = 1 << 30; + bt(0, 0); + FOR(i, 5) + printf("%d%c", ans[i], i == 4 ? '\n' : ' '); + } + return 0; +} diff --git a/10662.cpp b/10662.cpp new file mode 100644 index 0000000..37b6264 --- /dev/null +++ b/10662.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +char tr[21][21], rh[21][21], ht[21][21], ss[1000]; +int tp[21], rp[21], hp[21], T, R, H, min_; + +void Set() +{ + int i, j, max = T; + if (R > max) + { + max = R; + } + if (max < H) + { + max = H; + } + for (i = 0; i <= max; i++) + { + for (j = 0; j <= max; j++) + { + tr[i][j] = rh[i][j] = ht[i][j] = 0; + } + } +} +void Readt() +{ + int i, j, k; + char *p; + for (i = 0; i < T; i++) + { + gets(ss); + p = strtok(ss, " "); + k = atoi(p); + tp[i] = k; + j = 0; + while (p) + { + p = strtok(NULL, " "); + if (p) + { + k = atoi(p); + tr[i][j++] = k; + } + } + } +} +void Readr() +{ + int i, j, k; + char *p; + for (i = 0; i < R; i++) + { + gets(ss); + p = strtok(ss, " "); + k = atoi(p); + rp[i] = k; + j = 0; + while (p) + { + p = strtok(NULL, " "); + if (p) + { + k = atoi(p); + rh[i][j++] = k; + } + } + } +} +void Readh() +{ + int i, j, k; + char *p; + for (i = 0; i < H; i++) + { + gets(ss); + p = strtok(ss, " "); + k = atoi(p); + hp[i] = k; + j = 0; + while (p) + { + p = strtok(NULL, " "); + if (p) + { + k = atoi(p); + ht[i][j++] = k; + } + } + } +} +void Cal() +{ + int i, j, k; + int Rt, Rr, Rh; + min_ = 2147483646; + for (i = 0; i < T; i++) + { + for (j = 0; j < R; j++) + { + if (tr[i][j]) + { + continue; + } + if (tp[i] + rp[j] >= min_) + { + continue; + } + for (k = 0; k < H; k++) + { + if (rh[j][k] || ht[k][i]) + { + continue; + } + if (tp[i] + rp[j] + hp[k] >= min_) + { + continue; + } + min_ = tp[i] + rp[j] + hp[k]; + Rt = i; + Rr = j; + Rh = k; + } + } + } + if (min_ == 2147483646) + { + printf("Don't get married!\n"); + return; + } + printf("%d %d %d:%d\n", Rt, Rr, Rh, min_); +} + +int main() +{ + int i; + while (gets(ss)) + { + for (i = 0; ss[i]; i++) + { + if (ss[i] == '\n') + { + ss[i] = NULL; + } + } + if (strlen(ss) == 0) + { + break; + } + sscanf(ss, "%d%d%d", &T, &R, &H); + Set(); + Readt(); + Readr(); + Readh(); + Cal(); + } + return 0; +} diff --git a/10663.cpp b/10663.cpp new file mode 100644 index 0000000..585b7a3 --- /dev/null +++ b/10663.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +/* +10663 +Non Powerful Subset +*/ + +#define maxn 499500 +#define mxx 100000 +char nonP[maxn + 2]; +char Fg[maxn]; +int St[mxx], Ind; +int T[mxx]; +int R[1000], rInd; +int N, M; +void Remove() +{ + int i, j; + for (i = 2; i * i <= maxn; i++) + { + for (j = i * i; j <= maxn; j *= i) + { + nonP[j] = 1; + } + } +} +int Dynamic(int n) +{ + int i, d, k = 0; + for (i = Ind - 1; i >= 0; i--) + { + d = St[i] + n; + if (nonP[d] == 1) + { + return 0; + } + if (Fg[d] == 0) + { + T[k++] = d; + } + } + for (i = 0; i < k; i++) + { + St[Ind++] = T[i]; + Fg[T[i]] = 1; + } + if (Fg[n] == 0) + { + St[Ind++] = n; + Fg[n] = 1; + } + return 1; +} +void Cal() +{ + int i, d; + Ind = rInd = 0; + for (i = N; i <= M; i++) + { + if (nonP[i]) + { + continue; + } + if (Dynamic(i)) + { + R[rInd++] = i; + } + } + cout << R[0]; + for (i = 1; i < rInd; i++) + { + cout << " " << R[i]; + } + cout << endl; + for (i = 0; i < Ind; i++) + { + d = St[i]; + Fg[d] = 0; + } +} +int main() +{ + Remove(); + while (cin >> N >> M) + { + Cal(); + } + return 0; +} diff --git a/10664.cpp b/10664.cpp new file mode 100644 index 0000000..ef78eb8 --- /dev/null +++ b/10664.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int main() +{ + int t; + int dp[21][101]; + int items[20]; + cin >> t; + cin.ignore(100, '\n'); + while (t--) + { + int counter = 0; + int sum = 0; + for (int i = 0; i < 21; i++) + { + dp[i][0] = 0; + } + for (int i = 0; i < 101; i++) + { + dp[0][i] = 0; + } + do + { + cin >> items[counter]; + sum += items[counter]; + counter++; + } while (cin.peek() != '\n' && cin.peek() != -1); + cin.ignore(100, '\n'); + if (sum % 2 != 0) + { + cout << "NO" << endl; + continue; + } + sum = sum / 2; + for (int i = 1; i <= counter; i++) + { + for (int j = 1; j <= sum; j++) + { + if (j >= items[i - 1]) + { + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - items[i - 1]] + items[i - 1]); + } + else + { + dp[i][j] = dp[i - 1][j]; + } + } + } + if (dp[counter][sum] == sum) + { + cout << "YES" << endl; + } + else + { + cout << "NO" << endl; + } + } + return 0; +} diff --git a/10665.cpp b/10665.cpp new file mode 100644 index 0000000..50dc833 --- /dev/null +++ b/10665.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +struct Count +{ + char c; + int n; +} w[26], p[26]; +bool used[26]; +char str[500]; +int n; +int compare(const void *a, const void *b) +{ + Count *c = (Count *)a, *d = (Count *)b; + if (c->n != d->n) + { + return d->n - c->n; + } + else + { + return c->c - d->c; + } +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + w[i].c = i + 'A'; + w[i].n = 0; + } + scanf("%s", str); + for (int i = 0; str[i] != '#'; i++) + { + if (str[i] - 'A' >= n) + { + continue; + } + w[str[i] - 'A'].n++; + } + qsort(w, n, sizeof(Count), compare); + memset(used, false, sizeof(used)); + int left = 0, right = n - 1; + for (int i = 0; i < n; i++) + { + if (i & 1) + { + p[right--] = w[i]; + } + else + { + p[left++] = w[i]; + } + } + bool flag; + do + { + flag = false; + for (int i = 0; i < n / 2; i++) + if (p[i].c > p[n - 1 - i].c) + { + Count x = p[i]; + p[i] = p[n - 1 - i]; + p[n - 1 - i] = x; + flag = true; + } + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (p[i].n == p[j].n && p[i].c > p[j].c) + { + Count x = p[i]; + p[i] = p[j]; + p[j] = x; + flag = true; + } + } while (flag); + for (int i = 0; i < n; i++) + { + if (i) + { + putchar(' '); + } + printf("%c", p[i].c); + } + puts(""); + for (int i = 0; i < n; i++) + { + if (i) + { + putchar(' '); + } + printf("%d", p[i].n); + } + puts(""); + } +} diff --git a/10666.cpp b/10666.cpp new file mode 100644 index 0000000..b9430a5 --- /dev/null +++ b/10666.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int base[31]; +int main() +{ + int t; + scanf("%d", &t); + base[0] = 1; + for (int i = 1; i < 31; i++) + { + base[i] = base[i - 1] * 2; + } + while (t--) + { + int n, k; + scanf("%d %d", &n, &k); + int temp = k, best = 0, worst = base[n], s = 0; + bool flag = true; + for (int i = 0; i < n; i++, temp >>= 1) + { + if (temp & 1) + { + flag = false; + } + best += temp & 1; + if (flag) + { + s++; + } + } + worst -= base[s] - 1; + printf("%d %d\n", best + 1, worst); + } + return 0; +} diff --git a/10667.cpp b/10667.cpp index 1bfb461..82b18d5 100644 --- a/10667.cpp +++ b/10667.cpp @@ -1,83 +1,112 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef long long int64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define INF 1LL<<50 +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int64 T; - cin >> T; - while (T--){ - int64 N; - cin >> N; - int64 mat[N][N]; - int64 i, j, k; - - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = 1; - int64 B; - cin >> B; - while (B--){ - int64 R1, C1, R2, C2; - cin >> R1 >> C1 >> R2 >> C2; - FOI(i, min(R1-1, R2-1), max(R1-1, R2-1)) - FOI(j, min(C1-1, C2-1), max(C1-1, C2-1)) - mat[i][j] = -1 * INF; +/** +10667 +**/ +#define Dif(i, j, k) (Table[i + k][j] - Table[i][j]) +#define MAXN 110 +int N, M, MAX; +int Table[MAXN][MAXN]; +void ReadCase() +{ + int i, j, k, a, b, c, d; + scanf("%d", &N); + scanf("%d", &k); + for (i = 0; i <= N; i++) + { + for (j = 0; j <= N; j++) + { + Table[i][j] = 1; + } + } + while (k--) + { + scanf("%d%d%d%d", &a, &b, &c, &d); + for (i = a; i <= c; i++) + { + for (j = b - 1; j < d; j++) + { + Table[i][j] = -10000; + } + } + } +} +void Cal() +{ + int i, j, k, t; + if (N == 1) + { + if (Table[N][N - 1] < 0) + { + printf("0\n"); + } + else + { + printf("1\n"); + } + return; + } + for (i = 1; i <= N; i++) + { + for (j = 0; j < N; j++) + { + Table[i][j] = Table[i][j] + Table[i - 1][j]; } - FOI(i, 1, N-1) - FOI(j, 0, N-1) - mat[i][j] += mat[i-1][j]; - - int64 maxSum = -1 * INF; - - FOI(i, 0, N-1){ - FOI(j, i, N-1){ - int64 arr[N]; - FOI(k, 0, N-1) - arr[k] = mat[j][k]; - if (i > 0) - FOI(k, 0, N-1) - arr[k] -= mat[i-1][k]; - int64 curSum = 0; - FOI(k, 0, N-1){ - curSum += arr[k]; - maxSum = max(maxSum, curSum); - curSum = max(curSum, 0LL); + } + MAX = Table[1][0]; + for (k = 1; k <= N; k++) + { + for (i = 0; i <= N - k; i++) + { + for (t = 0, j = 0; j < N; j++) + { + if (t >= 0) + { + t += Dif(i, j, k); + } + else + { + t = Dif(i, j, k); + } + if (t > MAX) + { + MAX = t; } } } - maxSum = max(0LL, maxSum); - cout << maxSum << endl; + } + if (MAX <= 0) + { + printf("0\n"); + } + else + { + printf("%d\n", MAX); + } +} +void FREE() +{ + int i, j; + for (i = 0; i <= N; i++) + for (j = 0; j <= N; j++) + { + Table[i][j] = 1; + } +} +int main() +{ + int f = 0, kase; + scanf("%d", &kase); + while (kase--) + { + ReadCase(); + Cal(); + if (kase) + { + FREE(); + } } return 0; } - diff --git a/10668.cpp b/10668.cpp new file mode 100644 index 0000000..070c8f2 --- /dev/null +++ b/10668.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + double l, n, c, pt, th, tmp, r; + while (scanf("%lf%lf%lf", &l, &n, &c) == 3) + { + if (l < 0.0 && n < 0.0 && c < 0.0) + { + break; + } + if (n * c == 0.0) + { + printf("0.000\n"); + continue; + } + n = l * (1 + n * c); + c = n / l; + for (pt = 3.141592653 / 8.0, th = 3.141592653 / 4.0; pt != 0; pt /= 2) + { + tmp = th / sin(th); + if (tmp == c) + { + break; + } + th += (th / sin(th) > c) ? (-pt) : pt; + } + r = n / th / 2.0; + printf("%.3lf\n", r * (1 - cos(th))); + } + return 0; +} diff --git a/10669.cpp b/10669.cpp new file mode 100644 index 0000000..8cfa714 --- /dev/null +++ b/10669.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +typedef long long big; + +char dig[70][50]; +char temp[32]; + +void Rev(char *ss) +{ + int i, l = strlen(ss) - 1; + int j = l, t; + l /= 2; + for (i = 0; i <= l; i++) + { + t = ss[i]; + ss[i] = ss[j]; + ss[j--] = t; + } + strcpy(temp, ss); +} +void Gen() +{ + int i, j, k, carry, d; + strcpy(dig[1], "1"); + strcpy(dig[2], "3"); + for (i = 3; i < 65; i++) + { + carry = d = 0; + for (j = strlen(dig[i - 1]) - 1; j >= 0; j--) + { + k = (dig[i - 1][j] - '0') * 3 + carry; + temp[d++] = (k % 10) + '0'; + carry = k / 10; + } + if (carry) + { + temp[d++] = carry + '0'; + } + temp[d] = '\0'; + Rev(temp); + strcpy(dig[i], temp); + } +} +void Cal(big n) +{ + int d = 1, m = 0, i; + int A[70]; + while (n) + { + if (n % 2) + { + A[m++] = d; + } + n /= 2; + d++; + } + printf("{ "); + for (i = 0; i + 1 < m; i++) + { + printf("%s, ", dig[A[i]]); + } + printf("%s }\n", dig[A[m - 1]]); +} + +int main() +{ + big n; + Gen(); + while (scanf("%llu", &n), n) + { + if (n == 1) + { + printf("{ }\n"); + continue; + } + Cal(n - 1); + } + return 0; +} diff --git a/1067.cpp b/1067.cpp new file mode 100644 index 0000000..680229b --- /dev/null +++ b/1067.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define clr(a, x) memset(a, x, sizeof(a)) +#define rep(i, n) for (int i = 0; i < (int)(n); i++) +#define foreach(it, v) for (__typeof((v).end()) it = (v).begin(); it != (v).end(); it++) + +const int N = 55; +const int M = 200005; +const int INF = 1000000000; + +template +void checkmin(T &t, T x) +{ + if (x < t) + { + t = x; + } +} + +struct edge +{ + int x, y, c; + edge *next, *op; +} g[M], *ls[N], *cur[N], *fa[N]; +int d[N], num[N], e, vis[N]; + +void addedge(int x, int y, int c) +{ + g[e].x = x; + g[e].y = y; + g[e].c = c; + g[e].next = ls[x]; + ls[x] = &g[e]; + g[e].op = &g[e + 1]; + ++e; + g[e].x = y; + g[e].y = x; + g[e].c = c; + g[e].next = ls[y]; + ls[y] = &g[e]; + g[e].op = &g[e - 1]; + ++e; +} + +int sap(int S, int T, int n) +{ + rep(i, e) + { + if (i & 1) + { + int sum = g[i - 1].c + g[i].c; + g[i - 1].c = g[i].c = sum / 2; + } + } + rep(i, n + 1) d[i] = -1; + int cnt = 0; + int flow = 0; + while (1) + { + ++cnt; + queue Q; + Q.push(S); + d[S] = cnt; + while (!Q.empty()) + { + int x = Q.front(); + Q.pop(); + for (edge *t = ls[x]; t; t = t->next) + { + if (t->c && d[t->y] != cnt && !vis[t->y]) + { + d[t->y] = cnt; + Q.push(t->y); + fa[t->y] = t; + } + } + } + if (d[T] != cnt) + { + return flow; + } + int nf = INF; + for (int i = T; i != S; i = fa[i]->x) + { + checkmin(nf, fa[i]->c); + } + for (int i = T; i != S; i = fa[i]->x) + { + fa[i]->c -= nf; + fa[i]->op->c += nf; + } + flow += nf; + } + return flow; +} + +int main() +{ + int n, m, Tc = 0; + while (scanf("%d%d", &n, &m) && n + m) + { + printf("Case %d: ", ++Tc); + e = 0; + clr(ls, 0); + map, int> mp; + rep(i, m) + { + int x, y; + scanf("%d%d", &x, &y); + if (x > y) + { + swap(x, y); + } + if (x == y) + { + continue; + } + mp[make_pair(x, y)]++; + } + foreach (it, mp) + { + addedge(it->first.first, it->first.second, it->second); + } + clr(vis, 0); + int w[N]; + rep(i, n) + { + w[i + 1] = sap(0, i + 1, n + 1); + } + while (1) + { + int best = INF; + for (int i = 1; i <= n; i++) + { + if (!vis[i] && w[i] < best) + { + best = w[i]; + } + } + if (best == INF) + { + break; + } + for (int i = 1; i <= n; i++) + if (w[i] == best) + { + vis[i] = 1; + } + for (int i = 1; i <= n; i++) + { + if (!vis[i]) + { + int flow = sap(0, i, n + 1); + if (flow + best < w[i]) + { + w[i] = flow + best; + } + } + } + } + printf("%d\n\n", w[1]); + } +} diff --git a/10670.cpp b/10670.cpp new file mode 100644 index 0000000..dd1c739 --- /dev/null +++ b/10670.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int st, ter, ag, AB[102][2]; + +struct ss +{ + char nam[20]; + int cost; +} Ag[102]; + +int cmp(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + if (x->cost != y->cost) + { + return x->cost - y->cost; + } + return strcmp(x->nam, y->nam); +} +int Count(int a, int b) +{ + int cost = 0, in, h, d; + in = st; + while (in > ter) + { + h = in / 2; + if (h >= ter) + { + d = in - h; + if (d * a > b) + { + cost += b; + } + else + { + cost += d * a; + } + in = h; + } + else + { + cost += (in - ter) * a; + break; + } + } + return cost; +} +void Cal() +{ + int i; + for (i = 0; i < ag; i++) + { + Ag[i].cost = Count(AB[i][0], AB[i][1]); + } + qsort(Ag, ag, sizeof(ss), cmp); + for (i = 0; i < ag; i++) + { + printf("%s %d\n", Ag[i].nam, Ag[i].cost); + } +} +void Sep(char xx[], int n) +{ + int i, j = 0; + char temp[19]; + for (i = 0; xx[i] != ':'; i++) + { + temp[i] = xx[i]; + } + temp[i] = '\0'; + strcpy(Ag[n].nam, temp); + for (++i; xx[i] != ','; i++) + { + temp[j++] = xx[i]; + } + temp[j] = '\0'; + AB[n][0] = atoi(temp); + j = 0; + for (++i; xx[i]; i++) + { + temp[j++] = xx[i]; + } + temp[j] = '\0'; + AB[n][1] = atoi(temp); +} + +int main() +{ + int k, c = 1; + char temp[100]; + scanf("%d", &k); + while (k--) + { + scanf("%d%d%d", &st, &ter, &ag); + for (int i = 0; i < ag; i++) + { + scanf("%s", temp); + Sep(temp, i); + } + printf("Case %d\n", c++); + Cal(); + } + return 0; +} diff --git a/10671.cpp b/10671.cpp new file mode 100644 index 0000000..3433327 --- /dev/null +++ b/10671.cpp @@ -0,0 +1,196 @@ +#include + +using namespace std; + +typedef long long LL; +#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) +#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++) +const int maxn = 11; +int n, x, r[maxn], c[maxn], sx, sy, ex, ey, t1, t2; +const double eps = 1e-9; +const int mod = 2520; +const int maxt = 2520 * 21; +double d[maxn][maxn][maxt], cost[maxn][maxn][maxt]; +bool vis[maxn][maxn][maxt]; +double fee(int i) +{ + return x * 1.0 / (80 - 0.03 * (i * 5) * (i * 5)); +} +struct node +{ + int x, y, z; + node(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} +} pa[maxn][maxn][maxt]; +double dp2(int i, int j, int k) +{ + if (vis[i][j][k]) + { + return d[i][j][k]; + } + vis[i][j][k] = 1; + double &ans = d[i][j][k]; + if (i == ex && j == ey) + { + double tem = 12.0 * x * k / mod; + cost[i][j][k] = 0; + if (tem >= t1 && tem <= t2) + { + ans = tem; + } + else + { + ans = 1e6; + } + return ans; + } + ans = 1e6; + if (i != ex) + { + int ad = (ex > i ? 1 : -1); + for (int v = 1; v <= r[j]; v++) + { + double tem = dp2(i + ad, j, mod / v + k); + if (tem < ans) + { + ans = tem; + cost[i][j][k] = cost[i + ad][j][mod / v + k] + fee(v); + } + else if (tem == ans) + { + cost[i][j][k] = min(cost[i][j][k], cost[i + ad][j][mod / v + k] + fee(v)); + } + } + } + if (j != ey) + { + int ad = (ey > j ? 1 : -1); + for (int v = 1; v <= c[i]; v++) + { + double tem = dp2(i, j + ad, mod / v + k); + if (tem < ans) + { + ans = tem; + cost[i][j][k] = cost[i][j + ad][mod / v + k] + fee(v); + } + else if (tem == ans) + { + cost[i][j][k] = min(cost[i][j][k], cost[i][j + ad][mod / v + k] + fee(v)); + } + } + } + return ans; +} +double dp(int i, int j, int k) +{ + if (vis[i][j][k]) + { + return d[i][j][k]; + } + vis[i][j][k] = 1; + double &ans = d[i][j][k]; + if (i == ex && j == ey) + { + double tem = 12.0 * x * k / mod; + if (tem >= t1 && tem <= t2) + { + cost[i][j][k] = tem; + d[i][j][k] = 0; + } + else + { + ans = 1e6; + } + return ans; + } + ans = 1e6; + if (i != ex) + { + int ad = (ex > i ? 1 : -1); + for (int v = 1; v <= r[j]; v++) + { + double tem = dp(i + ad, j, mod / v + k) + fee(v); + if (tem < ans) + { + ans = tem; + cost[i][j][k] = cost[i + ad][j][mod / v + k]; + } + else if (tem == ans) + { + cost[i][j][k] = min(cost[i][j][k], cost[i + ad][j][mod / v + k]); + } + } + } + if (j != ey) + { + int ad = (ey > j ? 1 : -1); + for (int v = 1; v <= c[i]; v++) + { + double tem = dp(i, j + ad, mod / v + k) + fee(v); + if (tem < ans) + { + ans = tem; + cost[i][j][k] = cost[i][j + ad][mod / v + k]; + } + else if (tem == ans) + { + cost[i][j][k] = min(cost[i][j][k], cost[i][j + ad][mod / v + k]); + } + } + } + return ans; +} +void print_ans(int i, int j, int k) +{ + if (i == ex && j == ey) + { + return; + } + node &te = pa[i][j][k]; + printf("%d %d %d --> v : %d limit : %d\n", i, j, k, mod / (te.z - k), (te.x == i ? r[i] : c[j])); + print_ans(te.x, te.y, te.z); +} +void init() +{ + int x1 = min(sx, ex), x2 = max(sx, ex); + int y1 = min(sy, ey), y2 = max(sy, ey); + for (int i = x1; i <= x2; i++) + for (int j = y1; j <= y2; j++) + for (int k = 0; k < maxt; k++) + { + vis[i][j][k] = false; + } +} +int main() +{ + int T; + scanf("%d", &T); + for (int kase = 1; kase <= T; kase++) + { + scanf("%d %d", &n, &x); + rep1(i, n) + { + scanf("%d", &r[i]); + r[i] /= 5; + } + rep1(i, n) + { + scanf("%d", &c[i]); + c[i] /= 5; + } + scanf("%d %d %d %d %d %d", &sx, &sy, &ex, &ey, &t1, &t2); + printf("Scenario %d:\n", kase); + int flag = 1; + init(); + dp2(sx, sy, 0); + if (d[sx][sy][0] == 1e6) + { + printf("IMPOSSIBLE\n"); + continue; + } + printf("The earliest arrival: %.0lf minutes, fuel %.2lf gallons\n", ceil(d[sx][sy][0]), cost[sx][sy][0]); + init(); + dp(sx, sy, 0); + printf("The economical travel: %.0lf minutes, fuel %.2lf gallons\n", ceil(cost[sx][sy][0]), d[sx][sy][0]); + } + return 0; +} diff --git a/10672.cpp b/10672.cpp new file mode 100644 index 0000000..beb9b04 --- /dev/null +++ b/10672.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define MAXN 10002 +#define max(a, b) (a > b ? a : b) + +struct ret +{ + int child, val; +}; +struct node +{ + int nam; + node *next; +}; +struct Vertex +{ + int child; + int total; + char color; + node *next; +}; +Vertex V[MAXN]; +queue Q; +int Cost, N; + +void ReadCase() +{ + int i, u, ini, n, v, j; + node *x; + for (i = 1; i <= N; i++) + { + scanf("%d%d%d", &u, &ini, &n); + V[u].total = ini; + V[u].child = 1; + for (j = 0; j < n; j++) + { + scanf("%d", &v); + x = new node; + x->nam = v; + x->next = V[u].next; + V[u].next = x; + V[v].color = 1; + } + } +} +ret Set(int u) +{ + ret temp; + node *x = V[u].next; + int ch = 0, val = 0; + while (x) + { + temp = Set(x->nam); + V[u].child += temp.child; + V[u].total += temp.val; + x = x->next; + } + temp.child = V[u].child; + temp.val = V[u].total; + Cost += max(V[u].total - V[u].child, 0); + return temp; +} +void BFS(int u) +{ + ret x, y; + node *nd; + int v, cost; + x.child = u; + x.val = 0; + Q.push(x); + while (!Q.empty()) + { + y = Q.front(); + Q.pop(); + nd = V[y.child].next; + while (nd) + { + v = nd->nam; + if (V[v].child > V[v].total) + { + Cost += V[v].child - V[v].total; + } + y.child = v; + Q.push(y); + nd = nd->next; + } + } +} +int Root() +{ + int i; + for (i = 1; i <= N; i++) + if (!V[i].color) + { + break; + } + return i; +} +void Cal() +{ + int i, r; + Cost = 0; + r = Root(); + Set(r); + BFS(r); + printf("%d\n", Cost); +} +int main() +{ + while (scanf("%d", &N) && N) + { + ReadCase(); + Cal(); + for (int i = 1; i <= N; i++) + { + V[i].next = NULL; + V[i].total = 0; + V[i].color = 0; + } + } + return 0; +} diff --git a/10673.cpp b/10673.cpp new file mode 100644 index 0000000..4f7f435 --- /dev/null +++ b/10673.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +/* +10673 +*/ +typedef long long ss; +void Cal(ss x, ss k) +{ + ss p, temp, diff; + if (x <= k) + { + printf("0 %lld\n", x); + return; + } + p = x / k; + if (x % k) + { + p++; + } + temp = p * k; + diff = temp - x; + printf("%lld %lld\n", diff, k - diff); +} +int main() +{ + ss kase, x, k; + scanf("%lld", &kase); + while (kase--) + { + scanf("%lld%lld", &x, &k); + Cal(x, k); + } + return 0; +} diff --git a/10674.cpp b/10674.cpp new file mode 100644 index 0000000..cb8ec52 --- /dev/null +++ b/10674.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +typedef long long LL; +typedef long double LD; + +LL sqr(LL z) +{ + return z * z; +} + +bool cmp(const vector &a, const vector &b) +{ + for (int i = 0; i < 5; i++) + if (fabs(a[i] - b[i]) > 1e-6) + { + return a[i] < b[i]; + } + return false; +} + +void solve(LL x1, LL y1, LL r1, LL x2, LL y2, LL r2) +{ + LL d_sq = sqr(x1 - x2) + sqr(y1 - y2); + if (d_sq == 0) + { + printf("%d\n", r1 == r2 ? -1 : 0); + return; + } + if (d_sq < sqr(r1 - r2)) + { + printf("0\n"); + return; + } + LD d = sqrt((LD)d_sq); + LD vx = (x2 - x1) / d; + LD vy = (y2 - y1) / d; + vector> res; + for (int s = -1; s <= 1; s += 2) + { + LL delta = r1 + s * r2; + if (sqr(delta) > d_sq) + { + continue; + } + LD c = delta / (LD)d; + LD h = sqrt(max(0.0L, 1.0L - c * c)); + for (int t = -1; t <= 1; t += 2) + { + LD nx = vx * c + t * h * (-vy); + LD ny = vy * c + t * h * (vx); + LD a[5]; + a[0] = x1 + nx * r1; + a[1] = y1 + ny * r1; + a[2] = x2 - s * nx * r2; + a[3] = y2 - s * ny * r2; + a[4] = sqrt((a[0] - a[2]) * (a[0] - a[2]) + (a[1] - a[3]) * (a[1] - a[3])); + res.push_back(vector(a, a + 5)); + if (sqr(delta) == d_sq) + { + break; + } + } + } + sort(res.begin(), res.end(), cmp); + printf("%d\n", res.size()); + for (int i = 0; i < (int)res.size(); i++) + { + for (int j = 0; j < 5; j++) + { + char s[100]; + sprintf(s, "%.5Lf", res[i][j]); + if (strcmp(s, "-0.00000") == 0) + { + strcpy(s, "0.00000"); + } + printf(j == 4 ? "%s\n" : "%s ", s); + } + } +} + +int main() +{ + int x1, y1, r1, x2, y2, r2; + while (scanf("%d%d%d%d%d%d", &x1, &y1, &r1, &x2, &y2, &r2) == 6 && r1 > 0 && r2 > 0) + { + solve(x1, y1, r1, x2, y2, r2); + } +} diff --git a/10675.cpp b/10675.cpp new file mode 100644 index 0000000..c7a4822 --- /dev/null +++ b/10675.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +typedef vector VI; + +double EPS = 1e-8; +int DY[] = {1, -1, 0, 0}, DX[] = {0, 0, 1, -1}; +int nrows, ncols, faucetR, faucetC; +double cell[20][20]; + +struct Zone +{ + int id, area; + VI out; + double inrate, level, maxLevel; +}; + +bool cmp(Zone *a, Zone *b) +{ + return a->level > b->level; +} + +vector zones; +Zone *zoneAt[20][20]; + +int inside(int r, int c) +{ + return 1 <= r && r <= nrows && 1 <= c && c <= ncols; +} + +Zone *do_zone(int r0, int c0); + +void dfs(Zone *z, int r, int c) +{ + if (zoneAt[r][c] != NULL) + { + return; + } + zoneAt[r][c] = z; + z->area++; + for (int i = 0; i < 4; i++) + { + int rr = r + DY[i], cc = c + DX[i]; + if (!inside(rr, cc)) + { + continue; + } + if (fabs(cell[rr][cc] - z->level) < EPS) + { + dfs(z, rr, cc); + } + else if (cell[rr][cc] < z->level) + { + z->out.push_back(do_zone(rr, cc)->id); + } + else + { + z->maxLevel = min(z->maxLevel, cell[rr][cc]); + } + } +} + +Zone *do_zone(int r0, int c0) +{ + if (zoneAt[r0][c0] == NULL) + { + zones.push_back(new Zone()); + Zone *z = zones.back(); + z->level = cell[r0][c0]; + z->inrate = 0; + z->out.clear(); + z->id = zones.size() - 1; + z->maxLevel = 10; + z->area = 0; + dfs(z, r0, c0); + } + return zoneAt[r0][c0]; +} + +int main() +{ + int i, j; + while (cin >> nrows >> ncols >> faucetR >> faucetC) + { + if (nrows == 0 && ncols == 0) + { + break; + } + for (i = 1; i <= nrows; i++) + for (j = 1; j <= ncols; j++) + { + cin >> cell[i][j]; + } + double res = 0; + while (true) + { + for (i = 0; i < (int)zones.size(); i++) + { + delete zones[i]; + } + zones.clear(); + memset(zoneAt, 0, sizeof(zoneAt)); + do_zone(faucetR, faucetC); + bool done = false; + for (i = 1; i <= nrows; i++) + for (j = 1; j <= ncols; j++) + if (i == 1 || i == nrows || j == 1 || j == ncols) + if (zoneAt[i][j] != NULL) + { + done = true; + } + if (done) + { + break; + } + vector zzz = zones; + sort(zzz.begin(), zzz.end(), cmp); + double delta = 1e99; + zones[0]->inrate = 1; + for (i = 0; i < (int)zzz.size(); i++) + { + Zone *z = zzz[i]; + if (z->out.size() != 0) + { + for (j = 0; j < (int)z->out.size(); j++) + { + Zone *w = zones[z->out[j]]; + w->inrate += z->inrate / z->out.size(); + } + } + else + { + delta = min(delta, (z->maxLevel - z->level) / z->inrate * z->area); + } + } + res += delta; + for (i = 0; i < (int)zones.size(); i++) + { + Zone *z = zones[i]; + if (z->out.size() == 0) + { + z->level += delta * z->inrate / z->area; + } + } + for (i = 1; i <= nrows; i++) + { + for (j = 1; j <= ncols; j++) + { + if (zoneAt[i][j] != NULL) + { + cell[i][j] = zoneAt[i][j]->level; + } + } + } + } + printf("%.2f\n", res); + } +} diff --git a/10676.cpp b/10676.cpp new file mode 100644 index 0000000..d59c9c7 --- /dev/null +++ b/10676.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define INF 1000000000000000000LL +#define SHIFT 100000000 + +int type[4] = {10, 20, 25, 50}; +char buf[1005]; +long long times[20]; + +int ab(int now) +{ + if (now < 0) + return -now; + return now; +} + +int main() +{ + int count, n, m, i, j, len, buf1, buf2, maxdiff, diff, degtmp, typetmp, right, left, shift; + long long minnum, maxnum, temp, num; + times[0] = 1; + for (i = 1; i < 18; i++) + times[i] = times[i - 1] * 10; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &n, &m); + for (i = 0, minnum = INF, maxnum = -INF; i < n; i++) + { + scanf("%s", buf); + if (sscanf(buf, "%d.%d", &buf1, &buf2) == 1) + temp = (long long)buf1 * 100000000; + else + { + buf2 *= times[8 - strlen(strstr(buf, ".") + 1)]; + if (buf[0] == '-') + temp = (long long)buf1 * 100000000 - buf2; + else + temp = (long long)buf1 * 100000000 + buf2; + } + if (temp < minnum) + minnum = temp; + if (temp > maxnum) + maxnum = temp; + } + for (i = 0, maxdiff = 1000; i < 18; i++) + { + for (j = 0; j < 4; j++) + { + temp = times[i] * type[j]; + if (maxnum > 0) + right = (maxnum - 1) / temp + 1; + else + right = 0; + + if (minnum < 0) + left = (-minnum - 1) / temp + 1; + else + left = 0; + if ((diff = ab(left + right + 1 - m)) < maxdiff) + { + maxdiff = diff; + degtmp = i; + typetmp = j; + } + } + } + temp = times[degtmp] * type[typetmp]; + shift = degtmp + 1; + len = 7 - degtmp; + if (typetmp == 2) + { + shift--; + len++; + } + for (num = 0; num > minnum; num -= temp) + ; + if (maxnum < 0) + maxnum = 0; + for (; num < maxnum; num += temp) + { + if (num < 0) + { + if (len <= 0) + printf("-%lld ", (-num) / SHIFT); + else + printf("-%lld.%0*lld ", (-num) / SHIFT, len, ((-num) % SHIFT) / times[shift]); + } + else + { + if (len <= 0) + printf("%lld ", num / SHIFT); + else + printf("%lld.%0*lld ", num / SHIFT, len, (num % SHIFT) / times[shift]); + } + } + if (num < 0) + { + if (len <= 0) + printf("-%lld\n", (-num) / SHIFT); + else + printf("-%lld.%0*lld\n", (-num) / SHIFT, len, ((-num) % SHIFT) / times[shift]); + } + else + { + if (len <= 0) + printf("%lld\n", num / SHIFT); + else + printf("%lld.%0*lld\n", num / SHIFT, len, (num % SHIFT) / times[shift]); + } + } + return 0; +} diff --git a/10677.cpp b/10677.cpp new file mode 100644 index 0000000..b10b1b5 --- /dev/null +++ b/10677.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +/* +10677 +Base Equality +*/ +int D[15], ind, B1, B2; +int Con_B2() +{ + int i, k = 1, sum = 0; + for (i = 0; i < ind; i++) + { + sum += D[i] * k; + k *= B2; + } + return sum; +} +void Con_B1(int n) +{ + int k; + ind = 0; + while (n) + { + k = n % B1; + D[ind++] = k; + n /= B1; + } +} +void Cal(int r1, int r2) +{ + int i, d; + for (i = r2 - 1; i > r1; i--) + { + Con_B1(i); + d = Con_B2(); + if (d % i == 0) + { + printf("%d\n", i); + return; + } + } + printf("Non-existent.\n"); +} +int main() +{ + int kases, r1, r2; + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d%d%d", &B1, &B2, &r1, &r2); + Cal(r1, r2); + } + return 0; +} diff --git a/10678.cpp b/10678.cpp new file mode 100644 index 0000000..a7b4857 --- /dev/null +++ b/10678.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +const double pi = 2 * acos(0); + +int main() +{ + int N, t; + double a, b, l, d; + cin >> N; + for (t = 0; t < N; t++) + { + cin >> d >> l; + a = l / 2; + b = sqrt(a * a - (d / 2) * (d / 2)); + printf("%.3lf\n", pi * a * b); + } + return 0; +} diff --git a/10679.cpp b/10679.cpp index a497fed..f6e1f3f 100644 --- a/10679.cpp +++ b/10679.cpp @@ -1,26 +1,113 @@ -#include -using namespace std; - -int main(){ - int t=0; - cin>>t; - for(int i=1;i<=t;i++){ - char str[100000]={'\0'}; - cin>>str; - int n=0; - cin>>n; - for(int j=1;j<=n;j++){ - char s[1000]={'\0'}; - cin>>s; - char *index=strstr(str,s); - cout< + +using namespace std; + +typedef vector VI; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) + +const int MAXLEN = 256; +struct PMA +{ + PMA *next[MAXLEN]; + VI matched; + + PMA() + { + FOR(i, 0, MAXLEN) + next[i] = 0; + } + + ~PMA() + { + FOR(i, 0, MAXLEN) + if (next[i]) + delete next[i]; + } +}; + +inline PMA *PMA_build(char *pattern[], int cnt) +{ + PMA *root = new PMA, *now; + root->next[0] = root; + FOR(i, 0, cnt) + { + now = root; + for (int j = 0; pattern[i][j] != '\0'; j++) + { + if (now->next[(int)pattern[i][j]] == 0) + now->next[(int)pattern[i][j]] = new PMA; + now = now->next[(int)pattern[i][j]]; + } + now->matched.push_back(i); + } + + queue que; + FOR(i, 1, MAXLEN) + { + if (!root->next[i]) + root->next[i] = root; + else + { + root->next[i]->next[0] = root; + que.push(root->next[i]); + } + } + + while (!que.empty()) + { + now = que.front(); + que.pop(); + FOR(i, 1, MAXLEN) + { + if (now->next[i]) + { + PMA *next = now->next[0]; + while (!next->next[i]) + next = next->next[0]; + now->next[i]->next[0] = next->next[i]; + que.push(now->next[i]); + } + } + } + return root; +} + +inline void PMA_match(PMA *pma, const char *s, VI &res) +{ + for (int i = 0; s[i] != '\0'; i++) + { + int c = s[i]; + while (!pma->next[c]) + pma = pma->next[0]; + pma = pma->next[c]; + FOR(j, 0, pma->matched.size()) + res[pma->matched[j]] = true; + } +} + +static int TCASE; +static char S[100005], *T[1005]; + +int main() +{ + FOR(i, 0, 1000) + T[i] = new char[1005]; + + scanf("%d", &TCASE); + FOR(i, 0, TCASE) + { + scanf("%s", S); + int q; + scanf("%d", &q); + FOR(i, 0, q) + scanf("%s", T[i]); + + PMA *root = PMA_build(T, q); + VI res(q); + PMA_match(root, S, res); + FOR(i, 0, q) + puts(res[i] ? "y" : "n"); + } + return 0; +} diff --git a/1068.cpp b/1068.cpp new file mode 100644 index 0000000..9a5fcbd --- /dev/null +++ b/1068.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +template +void checkmin(T &t, T x) +{ + if (x < t) + t = x; +} +template +void checkmax(T &t, T x) +{ + if (x > t) + t = x; +} +template +void _checkmin(T &t, T x) +{ + if (t == -1) + t = x; + if (x < t) + t = x; +} +template +void _checkmax(T &t, T x) +{ + if (t == -1) + t = x; + if (x > t) + t = x; +} +typedef pair PII; +typedef pair PDD; +typedef long long lld; +#define foreach(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) +#define DEBUG(a) cout << #a " = " << (a) << endl; +#define DEBUGARR(a, n) \ + for (int i = 0; i < (n); i++) \ + { \ + cout << #a "[" << i << "] = " << (a)[i] << endl; \ + } +int N[3]; +int mat[21][21][21]; +int ans; +int src[3], dest[3], fd[3]; +bool isin(int pos[]) +{ + for (int i = 0; i < 3; i++) + if (pos[i] >= 1 && pos[i] <= N[i]) + ; + else + return 0; + return 1; +} +void disp(int p[]) +{ + for (int i = 0; i < 3; i++) + printf("%d%c", p[i], i == 2 ? '\n' : ' '); +} +void dfs(int now[], int d[], int used) +{ + + int i, j, forw, back[20], g, nd, ad[3], pos[3]; + for (i = 0; i < 3; i++) + if (now[i] != dest[i] || d[i] != fd[i]) + break; + //disp(now), disp(d), printf("%d\n-------------------------------\n",used); + if (i == 3) + { + ans = min(ans, used); + return; + } + if (used == 6) + return; + if (!isin(now) || used >= ans) + return; + for (forw = 2; forw <= 3; forw++) + { + for (i = 0; i < 3; i++) + pos[i] = now[i]; + int t = 0; + for (j = 0; j < forw; j++) + for (i = 0; i < 3; i++) + { + if (j > 0) + pos[i] += d[i]; + back[t++] = pos[i]; + } + int bt = t; + for (nd = 0; nd < 3; nd++) + if (d[nd] == 0) + { + for (i = 0; i < 3; i++) + ad[i] = 0; + for (ad[nd] = -1; ad[nd] <= 1; ad[nd]++) + if (ad[nd] != 0) + { + t = bt; + for (g = 1; g <= 4 - forw; g++) + { + for (i = 0; i < 3; i++) + back[t++] = pos[i] + g * ad[i]; + } + for (i = 0; i < t; i += 3) + { + if (!isin(back + i) || mat[back[i]][back[i + 1]][back[i + 2]] == 1) + break; + } + // disp(back), disp(back+3), disp(back+6), disp(back+9); + // printf("\n----------------\n"); + if (i == t) + { + int np[3]; + for (i = 0; i < 3; i++) + np[i] = pos[i] + (5 - forw) * ad[i]; + for (i = 0; i < t; i += 3) + mat[back[i]][back[i + 1]][back[i + 2]] = 1; + dfs(np, ad, used + 1); + for (i = 0; i < t; i += 3) + mat[back[i]][back[i + 1]][back[i + 2]] = 0; + } + } + } + } +} + +int main() +{ +#ifdef cwj + freopen("in", "r", stdin); +#endif + int tcas = 1; + char srcdirs[100], destdirs[100]; + int i; + while (scanf("%d", &N[0]) && N[0]) + { + scanf("%d%d", &N[1], &N[2]); + for (i = 0; i < 3; i++) + scanf("%d", &src[i]); + scanf("%s", srcdirs); + for (i = 0; i < 3; i++) + scanf("%d", &dest[i]); + scanf("%s", destdirs); + int mul; + memset(mat, 0, sizeof(mat)); + if (srcdirs[0] == '-') + mul = -1; + else + mul = 1; + char c = srcdirs[1]; + int d[3]; + memset(d, 0, sizeof(d)); + if (c == 'x') + d[0] = mul; + if (c == 'y') + d[1] = mul; + if (c == 'z') + d[2] = mul; + + if (destdirs[0] == '-') + mul = -1; + else + mul = 1; + c = destdirs[1]; + memset(fd, 0, sizeof(fd)); + if (c == 'x') + fd[0] = mul; + if (c == 'y') + fd[1] = mul; + if (c == 'z') + fd[2] = mul; + for (i = 0; i < 3; i++) + dest[i] += fd[i]; + printf("Case %d: ", tcas++); + if (isin(dest)) + { + printf("Impossible\n"); + continue; + } + ans = 1000000000; + dfs(src, d, 0); + if (ans == 1000000000) + printf("Impossible\n"); + else + printf("%d\n", ans); + } +} diff --git a/10680.cpp b/10680.cpp new file mode 100644 index 0000000..f32c7ad --- /dev/null +++ b/10680.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +int prime[1000010]; +int lcmtable[1000010]; +int sublcm[1000010]; +int flag[1000010]; + +void compute_prime_table() +{ + int i, j; + prime[0] = prime[1] = 0; + for (i = 2; i <= 1000000; i++) + { + prime[i] = 1; + } + for (i = 2; i <= 1000;) + { + for (j = i + i; j <= 1000000; j += i) + { + prime[j] = 0; + } + for (i++; !prime[i]; i++) + ; + } +} + +void get_flag_table() +{ + compute_prime_table(); + int pri, t; + //init flag[] + for (t = 0; t <= 1000000; t++) + { + flag[t] = 0; + } + // + for (pri = 2; pri <= 1000000; pri++) + { + if (prime[pri] == 1) + { + int num = (int)(6 / log10(pri)); + int i; + for (i = 1; i <= num; i++) + { + int tmp = ceil(pow(pri, i)); + flag[tmp] = pri; + } + } + } +} + +void get_lcm_table() +{ + int i; + for (i = 3, lcmtable[2] = 2, sublcm[2] = 2; i <= 1000000; i++) + { + if (flag[i] == 0) + { + sublcm[i] = sublcm[i - 1]; + lcmtable[i] = lcmtable[i - 1]; + } + else if (flag[i] == 5) + { + sublcm[i] = sublcm[i - 1] * flag[i]; + sublcm[i] %= 1000; + while (sublcm[i] % 10 == 0) + { + sublcm[i] /= 10; + } + lcmtable[i] = sublcm[i] % 10; + } + else + { + sublcm[i] = sublcm[i - 1] * flag[i]; + sublcm[i] %= 1000; + lcmtable[i] = sublcm[i] % 10; + } + } +} + +int main() +{ + get_flag_table(); + get_lcm_table(); + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + if (n == 1) + { + cout << 1 << endl; + } + else + { + cout << lcmtable[n] << endl; + } + } + return 0; +} diff --git a/10681.cpp b/10681.cpp new file mode 100644 index 0000000..0091d4a --- /dev/null +++ b/10681.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define MAXN 102 +#define MAX MAXN * 201 + +char Link[MAXN][MAXN], fg[MAXN][210]; +struct ss +{ + int node, move; +} Q[MAX]; +int qh, qt, C, D, S, E; + +void Push(int n, int move) +{ + if (fg[n][move]) + { + return; + } + fg[n][move] = 1; + Q[qh].node = n; + Q[qh++].move = move; + qh %= MAX; +} +ss Pop() +{ + ss temp; + temp.node = Q[qt].node; + temp.move = Q[qt++].move; + qt %= MAX; + return temp; +} +int Bfs() +{ + int i, move, n; + ss temp; + qh = qt = 0; + Push(S, 0); + while (qh != qt) + { + temp = Pop(); + n = temp.node; + move = temp.move + 1; + for (i = 1; i <= C; i++) + { + if (Link[n][i]) + { + if (move == D && i == E) + return 1; + if (move < D) + Push(i, move); + } + } + } + return 0; +} +void Cal() +{ + if ((D == 1 && Link[S][E]) || (S == E && D == 0) || Bfs()) + { + printf("Yes, Teobaldo can travel.\n"); + } + else + { + printf("No, Teobaldo can not travel.\n"); + } +} +void Free() +{ + int i, j; + for (i = 1; i <= C; i++) + { + for (j = i + 1; j <= C; j++) + Link[i][j] = Link[j][i] = 0; + for (j = 0; j <= D + 1; j++) + fg[i][j] = 0; + } +} + +int main() +{ + int i, u, v, L, f = 0; + while (scanf("%d%d", &C, &L) == 2) + { + if (!L && !C) + break; + if (f++) + Free(); + for (i = 0; i < L; i++) + { + scanf("%d%d", &u, &v); + Link[u][v] = Link[v][u] = 1; + } + scanf("%d%d%d", &S, &E, &D); + Cal(); + } + return 0; +} diff --git a/10682.cpp b/10682.cpp new file mode 100644 index 0000000..3758637 --- /dev/null +++ b/10682.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +class Coord +{ +public: + int i, step, path; + Coord() {} + Coord(int a, int b, int c) : i(a), step(b), path(c) {} +}; + +template +class Queue_ +{ +public: + int front, rear; + T space[5500]; + Queue_() + { + front = 0; + rear = 0; + } + void init() + { + front = 0; + rear = 0; + } + void add(T element) + { + if (!full()) + { + space[++rear] = element; + } + else + { + printf("FULL\n"); + } + } + T del() + { + if (!empty()) + { + return space[++front]; + } + else + { + return T(); + } + } + bool empty() + { + return front == rear; + } + bool full() + { + return rear + 1 == front; + } +}; + +Queue_ queue_; +string name[1000]; +int map_[1000][1000]; + +int find_pos(string, int &); +void find_path(int); +int BFS(int, int, int &); + +int main() +{ + int cs = 0, m, n; + while (scanf("%d", &m) == 1) + { + if (cs++) + { + printf("\n"); + } + int p, q, i; + for (p = 0; p < 1000; p++) + { + for (q = 0; q < 1000; q++) + { + map_[p][q] = -1; + } + } + queue_.init(); + n = 0; + string sa, sb; + int a, b, c; + for (i = 0; i < m; i++) + { + cin >> sa >> sb >> c; + a = find_pos(sa, n); + b = find_pos(sb, n); + map_[a][b] = c; + map_[b][a] = c; + } + string sstart, send; + cin >> sstart >> send; + int start = find_pos(sstart, n), end = find_pos(send, n); + int pos = BFS(start, end, n); + if (pos == -1) + { + printf("No valid route.\n"); + } + else + { + cout << sstart; + find_path(pos); + printf("\n"); + } + } + return 0; +} + +int find_pos(string s, int &n) +{ + for (int i = 0; i < n; i++) + { + if (name[i] == s) + { + return i; + } + } + name[n] = s; + return n++; +} + +void find_path(int pos) +{ + if (queue_.space[queue_.space[pos].path].i != queue_.space[pos].i) + { + find_path(queue_.space[pos].path); + cout << " " << name[queue_.space[pos].i]; + } +} + +int BFS(int start, int end, int &n) +{ + Coord pos(start, 0, 1); + queue_.add(pos); + while (!queue_.empty() && pos.i != end) + { + pos = queue_.del(); + for (int i = 0; i < n; i++) + { + if (i != queue_.space[pos.path].i && i != pos.i) + { + if (map_[pos.i][i] > 0) + { + if (map_[pos.i][i] >= pos.step) + { + queue_.add(Coord(i, map_[pos.i][i], queue_.front)); + if (i == end) + { + return queue_.rear; + } + } + } + } + } + } + if (pos.i == end) + { + return 1; + } + else + { + return -1; + } +} diff --git a/10683.cpp b/10683.cpp index 2088c9e..51c78de 100644 --- a/10683.cpp +++ b/10683.cpp @@ -1,15 +1,35 @@ -#include +#include + using namespace std; -int main(){ - char str[10]; - while( scanf("%s", str) != EOF ){ - int sec = ( (str[0] - '0') * 10 + (str[1] - '0') ) * 60 * 60 * 100; - sec += ( (str[2] - '0') * 10 + (str[3] - '0') ) * 60 * 100; - sec += ( (str[4] - '0') * 10 + (str[5] - '0') ) * 100; - sec += ( (str[6] - '0') * 10 + (str[7] - '0') ); - - printf("%07d\n", (sec * 125) / 108); +/* +10683 +The decadary watch +*/ +char ss[100]; +typedef long long xx; +void Cal() +{ + xx a, b, c, d, e, f; + a = (ss[0] - '0') * 10 + (ss[1] - '0'); + b = (ss[2] - '0') * 10 + (ss[3] - '0'); + c = (ss[4] - '0') * 10 + (ss[5] - '0'); + d = (ss[6] - '0') * 10 + (ss[7] - '0'); + e = ((a * 3600) + (b * 60) + c) * 100 + d; + f = (e * 125); + f = f / 108; + printf("%07lld\n", f); +} +int main() +{ + while (gets(ss)) + { + if (!strcmp(ss, "0000000")) + { + printf("%s\n", ss); + continue; + } + Cal(); } return 0; } diff --git a/10684.cpp b/10684.cpp index 7f9a189..979ae31 100644 --- a/10684.cpp +++ b/10684.cpp @@ -1,33 +1,38 @@ -#include -using namespace std; - -int MaxIntervalSum(int num[], int n){ - int max = num[0], i, sum[n]; - sum[0] = num[0] > 0 ? num[0] : 0; - for(i=1; i sum[i] ? max : sum[i]; - } - return max; -} - -int main(){ - while(true){ - int N, i; - scanf("%d", &N); - if(N == 0) - break; - int vec[N]; - for(i=0; i 0) - printf("The maximum winning streak is %d.\n", maxm); - else - printf("Losing streak.\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n, x, max, sum; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + max = sum = 0; + while (n--) + { + scanf("%d", &x); + sum += x; + if (sum < 0) + { + sum = 0; + } + else if (sum > max) + { + max = sum; + } + } + if (max) + { + printf("The maximum winning streak is %d.\n", max); + } + else + { + printf("Losing streak.\n"); + } + } + return 0; +} diff --git a/10685.cpp b/10685.cpp new file mode 100644 index 0000000..7a17826 --- /dev/null +++ b/10685.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +/* +Nature +10685 +*/ + +#define MAXN 5002 +struct Friends +{ + int parent; + int rank; + int count; +} F[MAXN]; +int N, M, MAX; +map mp; +int Find(int n) +{ + if (n != F[n].parent) + { + F[n].parent = Find(F[n].parent); + } + return F[n].parent; +} +void Link(int x, int y) +{ + if (F[x].rank > F[y].rank) + { + F[y].parent = x; + F[x].count += F[y].count; + if (MAX < F[x].count) + { + MAX = F[x].count; + } + } + else + { + F[x].parent = y; + F[y].count += F[x].count; + if (MAX < F[y].count) + { + MAX = F[y].count; + } + if (F[x].rank == F[y].rank) + { + F[y].rank++; + } + } +} +void Cal() +{ + int i, a, b, x, y; + char ss[40], sss[40]; + MAX = 0; + for (i = 1; i <= N; i++) + { + F[i].parent = i; + F[i].rank = 0; + F[i].count = 1; + } + for (i = 1; i <= M; i++) + { + scanf("%s%s", ss, sss); + a = mp[ss]; + b = mp[sss]; + x = Find(a); + y = Find(b); + if (x != y) + { + Link(x, y); + } + } + if (!MAX) + { + MAX++; + } + printf("%d\n", MAX); +} +int main() +{ + int i; + char sss[100]; + while (scanf("%d%d", &N, &M) == 2) + { + if (!N && !M) + { + break; + } + for (i = 1; i <= N; i++) + { + scanf("%s", sss); + mp[sss] = i; + } + Cal(); + } + return 0; +} diff --git a/10686.cpp b/10686.cpp new file mode 100644 index 0000000..5cf445e --- /dev/null +++ b/10686.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +int main() +{ + int n, c, w, p; + string category, keyword, input; + cin >> n; + while (n--) + { + cin >> c; + vector categories; + map needed, result; + multimap keywords; + multimap::iterator it; + pair::iterator, multimap::iterator> ret; + for (int i = 0; i < c; i++) + { + cin >> category >> w >> p; + map visited; + categories.push_back(category); + needed[category] = p; + for (int j = 0; j < w; j++) + { + cin >> keyword; + if (visited.find(keyword) == visited.end()) + { + visited[keyword] = true; + result[keyword] = 0; + keywords.insert(make_pair(keyword, category)); + } + } + } + cin.ignore(100, '\n'); + map visited; + while (getline(cin, input)) + { + if (input == "") + { + break; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i] <= 'Z') + { + } + else + { + input[i] = ' '; + } + } + stringstream ss(input, stringstream::in); + while (ss >> input) + { + if (visited.find(input) != visited.end()) + { + continue; + } + visited[input] = true; + if (keywords.count(input) > 0) + { + ret = keywords.equal_range(input); + for (it = ret.first; it != ret.second; it++) + { + result[it->second] += 1; + } + } + } + } + bool first = true; + for (int i = 0, sz = categories.size(); i < sz; i++) + { + if (result[categories[i]] >= needed[categories[i]]) + { + if (!first) + { + cout << ","; + } + cout << categories[i]; + first = false; + } + } + if (first) + { + cout << "SQF Problem."; + } + cout << endl; + } + return 0; +} diff --git a/10687.cpp b/10687.cpp new file mode 100644 index 0000000..bc6c131 --- /dev/null +++ b/10687.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define maxn 1001 + +vector links[maxn]; +struct ss +{ + int x, y; +} P[maxn]; +int N; +char Fg[maxn]; + +int Dis(int p, int q) +{ + int d, n; + d = P[p].x - P[q].x; + d *= d; + n = P[p].y - P[q].y; + n *= n; + return d + n; +} +int Swap(int a, int b) +{ + if (P[a].x > P[b].x) + return 1; + if (P[a].x == P[b].x && P[a].y > P[b].y) + return 1; + return 0; +} +void MakeLink() +{ + int min1, min2; + int i, j, dis, p1, p2; + for (i = 0; i < N; i++) + { + min1 = min2 = 1000000000; + for (j = 0; j < N; j++) + { + if (i == j) + continue; + dis = Dis(i, j); + if (dis < min1) + { + p2 = p1; + p1 = j; + min1 = dis; + } + else if (dis < min2) + { + p2 = j; + min2 = dis; + } + else if (dis == min1) + { + if (Swap(p1, j)) + { + if (min1 == min2 && Swap(p1, p2)) + p2 = p1; + p1 = j; + } + } + else if (dis == min2 && Swap(p2, j)) + p2 = j; + } + links[i].push_back(p1); + links[i].push_back(p2); + } +} +void Recur(int u) +{ + int i, v; + Fg[u] = 1; + for (i = 0; i < links[u].size(); i++) + { + v = links[u][i]; + if (Fg[v]) + continue; + Recur(v); + } +} +void Cal() +{ + int i; + if (N <= 3) + { + printf("All stations are reachable.\n"); + return; + } + Recur(0); + for (i = 0; i < N; i++) + { + if (!Fg[i]) + { + printf("There are stations that are unreachable.\n"); + return; + } + } + printf("All stations are reachable.\n"); +} +void Free() +{ + int i; + for (i = 0; i < N; i++) + { + Fg[i] = 0; + links[i].clear(); + } +} + +int main() +{ + int i; + while (scanf("%d", &N) == 1 && N) + { + for (i = 0; i < N; i++) + scanf("%d%d", &P[i].x, &P[i].y); + MakeLink(); + Cal(); + Free(); + } + return 0; +} diff --git a/10688.cpp b/10688.cpp new file mode 100644 index 0000000..a227b75 --- /dev/null +++ b/10688.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int m[501][501], n, k; +int i, j, l, t; +int main() +{ + for (i = 2; i <= 500; i++) + { + for (j = 0; i + j <= 500; j++) + { + m[i][j] = 99999999; + for (l = 1; l <= i / 2 + 1; l++) + { + t = (l + j) * i + m[l - 1][j] + m[i - l][j + l]; + if (t < m[i][j]) + { + m[i][j] = t; + } + } + } + } + scanf("%d", &t); + for (int caseno = 1; caseno <= t; caseno++) + { + scanf("%d %d", &n, &k); + printf("Case %d: %d\n", caseno, m[n][k]); + } +} diff --git a/10689.cpp b/10689.cpp new file mode 100644 index 0000000..80d5d57 --- /dev/null +++ b/10689.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +/* +10689 +Yet another number sequence +*/ +int Matrix[33][4], B[40], bi; +int St[4]; +void Gen() +{ + int temp[4] = {0, 1, 1, 1}, i; + Matrix[1][0] = 0; + Matrix[1][1] = 1; + Matrix[1][2] = 1; + Matrix[1][3] = 1; + for (i = 2; i <= 32; i++) + { + Matrix[i][0] = (Matrix[i - 1][0] * Matrix[i - 1][0] + Matrix[i - 1][1] * Matrix[i - 1][2]) % 10000; + Matrix[i][1] = (Matrix[i - 1][0] * Matrix[i - 1][1] + Matrix[i - 1][1] * Matrix[i - 1][3]) % 10000; + Matrix[i][2] = Matrix[i][1]; + Matrix[i][3] = (Matrix[i - 1][2] * Matrix[i - 1][1] + Matrix[i - 1][3] * Matrix[i - 1][3]) % 10000; + } +} +void Binary(int n) +{ + bi = 0; + while (n) + { + B[bi++] = n % 2; + n /= 2; + } +} +void Set() +{ + int i, k = 1, temp[4]; + St[0] = 1; + St[1] = 0; + St[2] = 0; + St[3] = 1; + for (i = 0; i < bi; i++) + { + if (B[i]) + { + temp[0] = (Matrix[i + 1][0] * St[0] + Matrix[i + 1][1] * St[2]) % 10000; + temp[1] = (Matrix[i + 1][0] * St[1] + Matrix[i + 1][1] * St[3]) % 10000; + temp[2] = temp[1]; + temp[3] = (Matrix[i + 1][2] * St[1] + Matrix[i + 1][3] * St[3]) % 10000; + St[0] = temp[0]; + St[1] = temp[1]; + St[2] = temp[2]; + St[3] = temp[3]; + } + } +} +void Cal(int a, int b, int n, int digit) +{ + int x, p = int(pow(10, digit)); + Binary(n); + Set(); + x = (St[0] * a) % p + (St[1] * (b)) % p; + x %= p; + printf("%d\n", x); +} +int main() +{ + int a, b, n, digit, kases, k = 1; + Gen(); + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d%d%d", &a, &b, &n, &digit); + Cal(a, b, n, digit); + } + return 0; +} diff --git a/1069.cpp b/1069.cpp new file mode 100644 index 0000000..8fa1cef --- /dev/null +++ b/1069.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +//*************************OUTPUT************************* +#ifdef WIN32 +#define INT64 "%I64d" +#define UINT64 "%I64u" +#else +#define INT64 "%lld" +#define UINT64 "%llu" +#endif + +//**************************CONSTANT*********************** +#define INF 0x7f7f7f7f +#define eps 1e-8 +#define PI acos(-1.) +#define PI2 asin(1.); +typedef long long LL; +//typedef __int64 LL; //codeforces +typedef unsigned int ui; +typedef unsigned long long ui64; +#define MP make_pair +typedef vector VI; +typedef pair PII; +#define pb push_back +#define mp make_pair + +//***************************SENTENCE************************ +#define CL(a, b) memset(a, b, sizeof(a)) +#define sqr(a, b) sqrt((double)(a) * (a) + (double)(b) * (b)) +#define sqr3(a, b, c) sqrt((double)(a) * (a) + (double)(b) * (b) + (double)(c) * (c)) + +//****************************FUNCTION************************ +template +double DIS(T va, T vb) +{ + return sqr(va.x - vb.x, va.y - vb.y); +} +template +inline T INTEGER_LEN(T v) +{ + int len = 1; + while (v /= 10) + { + ++len; + } + return len; +} +template +inline T square(T va, T vb) +{ + return va * va + vb * vb; +} + +// aply for the memory of the stack +//#pragma comment (linker, "/STACK:1024000000,1024000000") +//end + +#define CY 10000 + +bool vis[105]; +int ar[CY], br[CY]; +char S[CY]; +int idx; + +int cal(LL value, int k, int mod) +{ + LL single = 1; + while (k > 0) + { + if (k & 1) + { + single = single * value % mod; + } + value = value * value % mod; + k >>= 1; + } + return (int)single; +} + +int main(void) +{ + int cas = 0; + while (~scanf("%s", S) && S[0] != '.') + { + memset(vis, false, sizeof(vis)); + memset(br, -1, sizeof(br)); + int n = (int)strlen(S); + idx = 0; + int x = 0, sign = 1, i; + bool flag = false; + for (i = 0; S[i] != '/'; ++i) + { + if (S[i] >= '0' && S[i] <= '9') + { + x = 10 * x + S[i] - '0'; + flag = true; + } + if (S[i] == '-') + { + if (-1 == br[idx] && vis[idx]) + { + if (flag) + { + br[idx++] = x; + } + else + { + br[idx++] = 1; + } + } + sign = -1; + x = 0; + flag = false; + } + if (S[i] == '+') + { + if (-1 == br[idx] && vis[idx]) + { + if (flag) + { + br[idx++] = x; + } + else + { + br[idx++] = 1; + } + } + sign = 1; + x = 0; + flag = false; + } + if (S[i] == 'n') + { + if (flag) + { + ar[idx] = sign * x; + } + else + { + ar[idx] = sign; + } + vis[idx] = true; + x = 0; + flag = false; + } + if (S[i] == ')') + { + if (!vis[idx]) + { + ar[idx] = sign * x; + br[idx++] = 0; + flag = false; + x = 0; + } + else + { + if (flag) + { + br[idx++] = x; + } + else + { + br[idx++] = 1; + } + x = 0; + flag = false; + } + } + } + int D = 0; + for (; S[i]; ++i) + { + if (S[i] >= '0' && S[i] <= '9') + { + D = 10 * D + S[i] - '0'; + } + } + int k, sum = 0; + for (k = 0; k <= br[0] + 1; ++k) + { + for (int i = 0; i < idx; ++i) + { + int ans = cal(k, br[i], D); + ans = (int)((LL)ans * ar[i] % D); + sum = (int)(((LL)sum + ans) % D); + } + if (sum) + { + break; + } + } + if (k > br[0] + 1) + { + printf("Case %d: Always an integer\n", ++cas); + } + else + { + printf("Case %d: Not always an integer\n", ++cas); + } + } + return 0; +} + +/* +(n^2-n)/2 +(2n^3+3n^2+n)/6 +(-n^14-11n+1)/3 +(n^4-6n^3+23n^2-18n+24)/24 +(6)/3 +(6)/6 +(n^3-3n^2+2n)/6 +(n^3-3n^2+2n)/12 +(n^4-6n^3+11n^2-6n)/24 +*/ diff --git a/10690.cpp b/10690.cpp new file mode 100644 index 0000000..1e9344e --- /dev/null +++ b/10690.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int N, M, a[110]; +bool bag[5010][55]; +int ll[250006][2], l; + +int cmp(const void *a, const void *b) +{ + int *aa = (int *)a; + int *bb = (int *)b; + return *bb - *aa; +} + +int main() +{ + while (cin >> N >> M) + { + if (N > M) + { + int t = N; + N = M; + M = t; + } + int sum = 0, sum2 = 0; + for (int i = 0; i < N + M; i++) + { + cin >> a[i]; + sum += a[i]; + a[i] += 50; + } + qsort(a, N + M, sizeof(int), cmp); + for (int i = 0; i < N; i++) + { + sum2 += a[i]; + } + memset(bag, 0, sizeof(bag)); + bag[0][0] = true; + l = 0; + ll[l][0] = 0; + ll[l++][1] = 0; + for (int i = 0; i < N + M; i++) + { + int tmpl = l; + for (int j = 0; j < tmpl; j++) + { + if (ll[j][0] + a[i] <= sum2 && ll[j][1] + 1 <= N && bag[ll[j][0] + a[i]][ll[j][1] + 1] == false) + { + bag[ll[j][0] + a[i]][ll[j][1] + 1] = true; + ll[l][0] = ll[j][0] + a[i]; + ll[l++][1] = ll[j][1] + 1; + } + } + } + int mmin = 100000000, mmax = -100000000; + for (int i = 0; i <= sum2; i++) + { + if (bag[i][N]) + { + int x = i - 50 * N; + int y = sum - x; + if (x * y > mmax) + { + mmax = x * y; + } + if (x * y < mmin) + { + mmin = x * y; + } + } + } + cout << mmax << " " << mmin << endl; + } + return 0; +} diff --git a/10691.cpp b/10691.cpp new file mode 100644 index 0000000..05734c9 --- /dev/null +++ b/10691.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +const int maxn = 410; +const double eps = 1e-6; +const double pi = acos(-1.0); +int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} +int n, m; +double d; +struct interval +{ + double s, e; + interval(double s = 0, double e = 0) : s(s), e(e) {} + bool operator<(const interval &tmp) const + { + if (dcmp(e - tmp.e) < 0) + { + return true; + } + if (dcmp(e - tmp.e) == 0) + { + double s1 = dcmp(e - s) < 0 ? s - 2 * pi : s; + double s2 = dcmp(tmp.e < tmp.s) < 0 ? tmp.s - 2 * pi : tmp.s; + if (dcmp(s1 - s2) > 0) + { + return true; + } + } + return false; + } + bool operator==(const interval &tmp) const + { + return dcmp(e - tmp.e) == 0 && dcmp(s - tmp.s) == 0; + } +} p[maxn]; +double length(double x, double y) +{ + return sqrt(x * x + y * y); +} +double formal(double rad) +{ + if (dcmp(rad - 2 * pi) >= 0) + { + return rad - 2 * pi; + } + if (dcmp(rad) < 0) + { + return rad + 2 * pi; + } + return rad; +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%lf", &n, &d); + m = 0; + for (int i = 0; i < n; i++) + { + double x, y; + scanf("%lf%lf", &x, &y); + if (dcmp(length(x, y) - d) <= 0) + { + continue; + } + double angle = asin(d / length(x, y)); + p[m++] = interval(formal(atan2(y, x) - angle), formal(atan2(y, x) + angle)); + } + sort(p, p + m); + m = unique(p, p + m) - p; + for (int i = 0; i < m; i++) + { + p[i + m] = p[i]; + } + int ans = m; + for (int i = 0; i < m; i++) + { + if (i && dcmp(p[i].e - p[i - 1].e) == 0) + { + continue; + } + int tmp = 1, pre = i; + for (int j = i + 1; j < m + i; j++) + { + double s = dcmp(p[j].e - p[j].s) < 0 ? p[j].s - 2 * pi : p[j].s; + if (dcmp(s - p[pre].e) <= 0) + { + continue; + } + else + { + pre = j; + tmp++; + } + } + ans = min(ans, tmp); + double v = p[i].e + 2 * eps; + for (int j = 0; j < 2 * m; j++) + { + p[j] = interval(formal(p[j].s - v), formal(p[j].e - v)); + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/10692.cpp b/10692.cpp new file mode 100644 index 0000000..d9ad111 --- /dev/null +++ b/10692.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int cal(int[], int, int, int, int); +int L[10002]; +int main() +{ + int n, m, X[20], i, j, k; + for (i = 1; scanf("%d%d", &m, &n) == 2; i++) + { + for (j = 0; j < n; j++) + { + scanf("%d", &X[j]); + } + printf("Case #%d: %d\n", i, cal(X, n, 0, 0, m)); + } + return 0; +} +int cal(int X[], int n, int now, int st, int k) +{ + if (now == n - 1) + { + if (X[n - 1] < st) + { + return X[n - 1]; + } + return (X[n - 1] - st) % k + st; + } + int nst, nk, rt, i, j, end, mul; + for (i = 1, L[0] = 1, end = 0; end == 0; i++) + { + L[i] = (L[i - 1] * X[now] - st + k) % k + st; + for (j = 0; j < i; j++) + { + if (L[i] == L[j]) + { + nst = j; + nk = i - j; + end = 1; + break; + } + } + } + rt = cal(X, n, now + 1, nst, nk); + for (i = 0, mul = 1; i < rt; i++) + { + mul = (mul * X[now] - st + k) % k + st; + } + return mul; +} diff --git a/10693.cpp b/10693.cpp new file mode 100644 index 0000000..7316740 --- /dev/null +++ b/10693.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +double v, volume; +long int L, f; +int main() +{ + while (scanf("%ld %ld", &L, &f) != EOF) + { + if (L == 0 && f == 0) + { + break; + } + v = sqrt(L * f * 2.0); + volume = (v * 3600) / (2 * L); + printf("%0.5lf %0.5lf", v, volume); + printf("\n"); + } + return 0; +} diff --git a/10694.cpp b/10694.cpp new file mode 100644 index 0000000..48f05ae --- /dev/null +++ b/10694.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +class BigNumber +{ + vector d; + +public: + BigNumber() + { + d.clear(); + } + BigNumber(int n) + { + d.clear(); + d.push_back(n); + } + BigNumber(int *array, int len) + { + d.clear(); + for (int i = 0; i < len; i++) + { + d.push_back(array[i]); + } + } + void operator=(BigNumber ref) + { + d.clear(); + for (int i = 0; i < ref.d.size(); i++) + { + d.push_back(ref.d[i]); + } + } + BigNumber operator+(BigNumber ref) + { + int temp[25], len, carry = 0; + memset(temp, 0, sizeof(temp)); + for (int i = 0; i < d.size(); i++) + { + temp[i] = d[i]; + } + for (int i = 0; i < ref.d.size(); i++) + { + temp[i] += ref.d[i]; + } + for (int i = 0; i < 25; i++) + { + int t = temp[i] + carry; + carry = t / 1000000000; + temp[i] = t % 1000000000; + } + for (len = 24; temp[len] == 0; len--) + ; + return BigNumber(temp, len + 1); + } + void print() + { + printf("%d", d[d.size() - 1]); + for (int i = d.size() - 2; i >= 0; i--) + { + printf("%09d", d[i]); + } + printf("\n"); + } +} f[1001], s[1001], ans[1001]; +void init() +{ + f[1] = BigNumber(1); + f[2] = BigNumber(1); + s[1] = BigNumber(1); + s[2] = BigNumber(2); + ans[1] = BigNumber(1); + ans[2] = BigNumber(3); + for (int i = 3; i <= 1000; i++) + { + f[i] = f[i - 1] + f[i - 2]; + s[i] = s[i - 1] + f[i]; + ans[i] = ans[i - 1] + s[i]; + } +} +int main() +{ + int t, n; + init(); + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + if (n <= 0) + { + puts("0"); + } + else + { + ans[n].print(); + } + } +} diff --git a/10696.cpp b/10696.cpp index 1bc17eb..1498f1d 100644 --- a/10696.cpp +++ b/10696.cpp @@ -1,21 +1,20 @@ -#include -using namespace std; -long long f91(long long a); -int main(){ - long long N=0; - for(;;){ - cin>>N; - if(N==0) - break; - cout<<"f91("<=101) - return (a-10); - else - return f91(f91(a+11)); -} +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) && n) + { + if (n <= 101) + { + printf("f91(%d) = 91\n", n); + } + else + { + printf("f91(%d) = %d\n", n, n - 10); + } + } + return 0; +} diff --git a/10697.cpp b/10697.cpp new file mode 100644 index 0000000..13e9d0f --- /dev/null +++ b/10697.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +main() +{ + int n, i; + double x1, y1, x2, y2, x3, y3, cx1, cy1, m1, b1, cx2, cy2, m2, b2, r1, rx, ry; + scanf("%d\n", &n); + for (i = 1; i <= n; i++) + { + scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3); + if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) + { + printf("There is an infinity of possible locations.\n"); + } + else + { + cx1 = (x1 + x2) / 2.0; + cy1 = (y1 + y2) / 2.0; + if (y1 == y2) + { + m1 = 1e30; + b1 = cx1; + } + else + { + m1 = -(x2 - x1) / (y2 - y1); + b1 = cy1 - m1 * cx1; + } + cx2 = (x1 + x3) / 2.0; + cy2 = (y1 + y3) / 2.0; + if (y1 == y3) + { + m2 = 1e30; + b2 = cx2; + } + else + { + m2 = -(x3 - x1) / (y3 - y1); + b2 = cy2 - m2 * cx2; + } + if (m1 == m2 || (m1 != 0.0 && m2 != 0.0 && m1 / m2 >= 1 - 1e-14 && m1 / m2 <= 1 + 1e-14)) + { + printf("There is no possible location.\n"); + } + else + { + if (m1 == 1e30) + { + rx = b1; + ry = m2 * rx + b2; + } + else if (m2 == 1e30) + { + rx = b2; + ry = m1 * rx + b1; + } + else + { + rx = (b1 - b2) / (m2 - m1); + ry = (b1 * m2 - b2 * m1) / (m2 - m1); + } + if (rx > 0) + { + rx += 1e-8; + } + if (rx < 0) + { + rx -= 1e-8; + } + if (ry > 0) + { + ry += 1e-8; + } + if (ry < 0) + { + ry -= 1e-8; + } + if (rx < 0.0 && rx > -0.05) + { + rx = 0.0; + } + if (ry < 0.0 && ry > -0.05) + { + ry = 0.0; + } + printf("The equidistant location is (%.1lf, %.1lf).\n", rx, ry); + } + } + } +} diff --git a/10698.cpp b/10698.cpp new file mode 100644 index 0000000..307acb1 --- /dev/null +++ b/10698.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +/* +10698 +Football Sort +*/ + +map M; +struct ss +{ + char nam[20]; + char Or[20]; + char avg[20]; + int played; + int point; + int goaldiff; + int goal; + int aga; +}; +ss F[30]; +int team, game; +char xx[] = "N/A"; +int Com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + if (x->played == 0 && y->played == 0)// op + { + return strcmp(x->nam, y->nam);// op + } + if (x->played && y->played == 0)// op + { + return y->played - x->played;// op + } + if (x->played == 0 && y->played)//op + { + return y->played - x->played;//op + } + if (x->point != y->point) + { + return y->point - x->point; + } + if (x->goaldiff != y->goaldiff) + { + return y->goaldiff - x->goaldiff; + } + if (x->goal != y->goal) + { + return y->goal - x->goal; + } + return strcmp(x->nam, y->nam); +} +void ReadCase() +{ + int i, ga, gb, ia, ib; + char ta[20], tb[20]; + for (i = 0; i < team; i++) + { + scanf("%s", F[i].Or); + M[F[i].Or] = i + 1; + for (ia = 0; F[i].Or[ia]; ia++) + { + F[i].nam[ia] = tolower(F[i].Or[ia]); + } + } + while (game--) + { + scanf("%s%d%s%d%s", ta, &ga, tb, &gb, tb); + ia = M[ta] - 1; + ib = M[tb] - 1; + F[ia].goal += ga; + F[ib].goal += gb; + F[ia].goaldiff += ga - gb; + F[ib].goaldiff += gb - ga; + F[ia].played++; + F[ib].played++; + F[ia].aga += gb; + F[ib].aga += ga; + if (ga > gb) + { + F[ia].point += 3; + } + else if (gb > ga) + { + F[ib].point += 3; + } + else + { + F[ia].point++; + F[ib].point++; + } + } +} +int isEqual(int n, int m) +{ + if (F[n].point != F[m].point) + { + return 0; + } + if (F[n].goaldiff != F[m].goaldiff) + { + return 0; + } + if (F[n].goal != F[m].goal) + { + return 0; + } + return 1; +} +void Print() +{ + int i; + char xyz[] = " "; + printf("%2d.%16s%4d%4d%4d%4d%4d%7s\n", 1, F[0].Or, F[0].point, F[0].played, + F[0].goal, F[0].aga, F[0].goaldiff, F[0].avg); + for (i = 1; i < team; i++) + { + if (isEqual(i, i - 1)) + { + printf("%s", xyz); + } + else + { + printf("%2d.", i + 1); + } + printf("%16s%4d%4d%4d%4d%4d%7s\n", F[i].Or, F[i].point, F[i].played, + F[i].goal, F[i].aga, F[i].goaldiff, F[i].avg); + } +} +void Cal() +{ + int i, j; + double xyz; + char tt[10]; + for (i = 0; i < team; i++) + { + if (F[i].played) + { + xyz = (double)F[i].point * 100 / (double)(F[i].played * 3); + sprintf(tt, "%.2lf", xyz); + strcpy(F[i].avg, tt); + } + else + { + strcpy(F[i].avg, xx); + } + } + Print(); +} +void ReSet() +{ + int i; + for (i = 0; i < team; i++) + { + F[i].goaldiff = F[i].point = 0; + F[i].played = 0; + F[i].aga = 0; + F[i].goal = 0; + } +} +int main() +{ + int f = 0; + while (scanf("%d%d", &team, &game) == 2) + { + if (!team && !game) + { + break; + } + ReSet(); + ReadCase(); + qsort(F, team, sizeof(ss), Com); + if (f++) + { + printf("\n"); + M.empty(); + } + Cal(); + } + return 0; +} diff --git a/10699.cpp b/10699.cpp index 59b067b..ef81dc9 100644 --- a/10699.cpp +++ b/10699.cpp @@ -1,69 +1,46 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) -#define LIM 1000000 -int main(){ - int i, j; - vector pri; - bool vec[LIM]; - memset(vec, true, sizeof(vec)); - vec[0] = false; vec[1] = false; - - for(i=0; i>N; - if(N == 0) - break; - FOI(i, 0, SZ-1){ - if(N%pri[i]==0){ - fac++; - } - } - cout< + +using namespace std; + +int main() +{ + int i, j, k, c = 6, np, t1, t2, X[78500]; + X[1] = 2; + X[2] = 3; + X[3] = 5; + X[4] = 7; + X[5] = 11; + X[6] = 13; + for (i = 17; i <= 1000000; i += 2) + { + for (j = 1; X[j] <= sqrt((double)i); j++) + if (i % X[j] == 0) + { + break; + } + if (i % X[j] != 0) + { + c++; + X[c] = i; + } + } + for (;;) + { + scanf("%d", &t1); + if (t1 == 0) + { + break; + } + t2 = t1; + np = 0; + for (k = 1; k <= c && t2 >= X[k]; k++) + { + if (t2 % X[k] == 0) + { + np++; + } + } + printf("%d : %d\n", t1, np); + } + return 0; +} diff --git a/107.cpp b/107.cpp new file mode 100644 index 0000000..3da47ff --- /dev/null +++ b/107.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +inline void find(int init_height, int kitten) +{ + for (int n = 2; n <= init_height; ++n) + { + int curr_height = init_height; + int now_cat = 1; + int total = 0, notWorkCat = 0; + while ((curr_height % n) == 0) + { + total += curr_height * now_cat; + notWorkCat += now_cat; + curr_height = curr_height / n; + now_cat = now_cat * (n - 1); + } + if (now_cat == kitten) + { + total += now_cat; + printf("%d %d\n", notWorkCat, total); + break; + } + } +} + +int main() +{ + int init_height, kitten; + while (scanf("%d%d", &init_height, &kitten) == 2) + { + if (init_height == 0 || kitten == 0) + { + break; + } + if (init_height == 1 && kitten == 1) + { + printf("0 1\n"); + } + else if (init_height == 1) + { + printf("1 %d\n", kitten); + } + find(init_height, kitten); + } + return 0; +} diff --git a/10700.cpp b/10700.cpp new file mode 100644 index 0000000..5a88ef5 --- /dev/null +++ b/10700.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +/* +camel trading +10700 +*/ +typedef long long INT; +char expr[100], Op[25]; +INT Val[30], tval, top; +void GetVal() +{ + tval = 0; + char *p; + p = strtok(expr, "+*"); + while (p) + { + Val[tval++] = atoi(p); + p = strtok(NULL, "+*"); + } +} +void GenOp() +{ + INT i; + top = 0; + for (i = 0; expr[i]; i++) + { + if (expr[i] == '+' || expr[i] == '*') + { + Op[top++] = expr[i]; + } + } +} +void Max() +{ + INT i, j = 0, temp = 1, sum; + while (j < tval) + { + sum = Val[j]; + for (i = j; i + 1 < tval; i++) + { + if (Op[i] == '+') + { + sum += Val[i + 1]; + } + else + { + break; + } + } + temp *= sum; + j = i + 1; + } + printf(" %lld", temp); +} +void Min() +{ + INT i, j = 0, temp = 0, sum; + while (j < tval) + { + sum = Val[j]; + for (i = j; i + 1 < tval; i++) + { + if (Op[i] == '*') + { + sum *= Val[i + 1]; + } + else + { + break; + } + } + temp += sum; + j = i + 1; + } + printf(" and %lld.\n", temp); +} +void Cal() +{ + GenOp(); + GetVal(); + printf("The maximum and minimum are"); + Max(); + Min(); +} +int main() +{ + INT kase; + scanf("%lld", &kase); + while (kase--) + { + scanf("%s", expr); + Cal(); + } + return 0; +} diff --git a/10701.cpp b/10701.cpp new file mode 100644 index 0000000..ca56bba --- /dev/null +++ b/10701.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +/* +536 +Tree Recovery +*/ +#define MAXN 60 +char in[MAXN], pre[MAXN], F[MAXN]; +int indin[MAXN], indpre[MAXN]; +int lenin, lenpre; +struct ss +{ + int left, right; +} node[MAXN]; +void Ini() +{ + int i, k; + for (i = 0; i < MAXN; i++) + { + node[i].left = node[i].right = -1; + F[i] = 0; + } + lenin = strlen(in); + lenpre = strlen(pre); + for (i = 0; i < lenin; i++) + { + k = in[i] - 'A'; + indin[k] = i; + } + for (i = 0; i < lenpre; i++) + { + k = pre[i] - 'A'; + indpre[k] = i; + } +} +int FindLeft(int ind) +{ + int i, j, k, min = 100; + for (i = ind - 1; i >= 0; i--) + { + k = in[i] - 'A'; + if (F[k]) + { + break; + } + if (indpre[k] < min) + { + min = indpre[k]; + j = in[i]; + } + } + if (min < 100) + { + return j - 'A'; + } + return -1; +} +int FindRight(int ind) +{ + int i, j, k, min = 100; + for (i = ind + 1; i < lenin; i++) + { + k = in[i] - 'A'; + if (F[k]) + { + break; + } + if (indpre[k] < min) + { + min = indpre[k]; + j = in[i]; + } + } + if (min < 100) + { + return j - 'A'; + } + return -1; +} +void Construct(int n) +{ + int k; + F[n] = 1; + k = FindLeft(indin[n]); + if (k >= 0) + { + F[k] = 1; + node[n].left = k; + Construct(k); + } + k = FindRight(indin[n]); + if (k >= 0) + { + F[k] = 1; + node[n].right = k; + Construct(k); + } +} +void Print(int n) +{ + if (n < 0) + { + return; + } + Print(node[n].left); + Print(node[n].right); + printf("%c", n + 'A'); +} +int main() +{ + int kase, node; + scanf("%d", &kase); + while (kase--) + { + scanf("%d%s%s", &node, pre, in); + Ini(); + Construct(pre[0] - 'A'); + Print(pre[0] - 'A'); + printf("\n"); + } + return 0; +} diff --git a/10702.cpp b/10702.cpp new file mode 100644 index 0000000..9742de9 --- /dev/null +++ b/10702.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +/* +10702 +*/ +#define MAXN 102 + +int End[MAXN]; +int C, S, E, T; +struct qq +{ + int n, tour; +} xx; +queue Q; +struct ct +{ + char f; + int cost; +} taka[MAXN][1002]; +struct ss +{ + int list[MAXN]; + int Ad[MAXN]; + int ind; +} node[MAXN]; +void BFS() +{ + int i, nw, n, tour, cost; + qq temp, xyz; + temp.n = S; + temp.tour = 0; + Q.push(temp); + while (!Q.empty()) + { + xyz = Q.front(); + Q.pop(); + tour = xyz.tour + 1; + n = xyz.n; + taka[n][xyz.tour].f = 0; + for (i = 0; i < node[n].ind; i++) + { + nw = node[n].Ad[i]; + if (taka[nw][tour].cost < taka[n][xyz.tour].cost + node[n].list[i]) + { + taka[nw][tour].cost = taka[n][xyz.tour].cost + node[n].list[i]; + if (taka[nw][tour].f == 0 && tour != T) + { + taka[nw][tour].f = 1; + temp.n = nw; + temp.tour = tour; + Q.push(temp); + } + } + } + } +} +void Cal() +{ + int i, min = 0, k; + BFS(); + for (i = 0; i < E; i++) + { + k = End[i]; + if (taka[k][T].cost > min) + { + min = taka[k][T].cost; + } + } + printf("%d\n", min); + Q.empty(); + for (i = 1; i <= C; i++) + { + for (k = 1; k <= T; k++) + { + taka[i][k].f = taka[i][k].cost = 0; + } + } +} +int main() +{ + int i, j, k; + while (scanf("%d%d%d%d", &C, &S, &E, &T) == 4) + { + if (!C && !S && !E && !T) + { + break; + } + for (i = 1; i <= C; i++) + { + node[i].ind = 0; + for (j = 1; j <= C; j++) + { + scanf("%d", &k); + if (k) + { + node[i].list[node[i].ind] = k; + node[i].Ad[node[i].ind++] = j; + } + } + } + for (i = 0; i < E; i++) + { + scanf("%d", &End[i]); + } + Cal(); + } + return 0; +} diff --git a/10703.cpp b/10703.cpp index c4a63d3..29a267f 100644 --- a/10703.cpp +++ b/10703.cpp @@ -1,62 +1,81 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define MAXN 502 + +char B[MAXN][MAXN]; +int R, C, cnt; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - int W, H, N; - scanf("%d%d%d", &W, &H, &N); - if( W==0 && H==0 && N==0 ) +void Ini() +{ + int i, j; + for (i = 0; i <= R; i++) + { + for (j = 0; j <= C; j++) + { + B[i][j] = 0; + } + } +} +void Mark(int r1, int c1, int r2, int c2) +{ + int tr, tc, i, j; + if (r1 > r2) + { + tr = r1; + r1 = r2; + r2 = tr; + } + if (c1 > c2) + { + tc = c1; + c1 = c2; + c2 = tc; + } + for (i = r1; i <= r2; i++) + { + for (j = c1; j <= c2; j++) + { + if (B[i][j]) + { + continue; + } + B[i][j] = 1; + cnt++; + } + } +} + +int main() +{ + int n, r1, c1, r2, c2; + while (scanf("%d%d%d", &C, &R, &n) == 3) + { + if (!R && !C && !n) + { break; - bool mat[W][H]; - int i, j; - memset(mat, false, sizeof(mat) ); - while( N-- ){ - int X1, Y1, X2, Y2; - scanf("%d%d%d%d", &X1, &Y1, &X2, &Y2); - FOI(i, min(X1, X2) - 1, max(X1, X2) - 1) - FOI(j, min(Y1, Y2) - 1, max(Y1, Y2) - 1) - mat[i][j] = true; } - int cnt = 0; - FOI(i, 0, W - 1) - FOI(j, 0, H - 1) - if( !mat[i][j] ) - cnt++; - if( cnt == 0 ) + cnt = 0; + Ini(); + while (n--) + { + scanf("%d%d%d%d", &c1, &r1, &c2, &r2); + Mark(r1, c1, r2, c2); + } + int res = R * C - cnt; + if (!res) + { printf("There is no empty spots.\n"); - else if( cnt == 1) + } + else if (res == 1) + { printf("There is one empty spot.\n"); + } else - printf("There are %d empty spots.\n", cnt); + { + printf("There are %d empty spots.\n", res); + } } return 0; } diff --git a/10704.cpp b/10704.cpp new file mode 100644 index 0000000..c7abebd --- /dev/null +++ b/10704.cpp @@ -0,0 +1,194 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +#define UNDEFINE -1 +#define WHITE 0 +#define V2 1 +#define V3 2 +#define H2 3 +#define H3 4 +#define PLUS 0 +#define MINUS 1 + +#define SOLVED 2 +#define MOVABLE 1 +#define FAILED 0 + +int block_len[] = {2, 2, 3, 2, 3}; +int direc[] = {1, -1}; +int dx[] = {1, 0, 0, 1, 1}; +int dy[] = {0, 1, 1, 0, 0}; + +class state +{ +public: + vector p[5];//pos + short m[6][6]; + int cnt; + void print(); + void put(int, int, int); + int is_movable(int d, int type, int me); + bool operator<(const state &a) const + { + rep(i, 6) rep(j, 6) if (m[i][j] != a.m[i][j]) + { + return m[i][j] < a.m[i][j]; + } + return false; + } +}; +void state::print() +{ + puts("begin"); + cout << "now " << cnt << endl; + rep(i, 6) + { + rep(j, 6) + { + if (m[i][j] == -1) + { + putchar('*'); + } + else + { + cout << m[i][j]; + } + } + cout << endl; + } + puts("end"); +} + +void state::put(int type, int x, int y) +{ + rep(i, block_len[type]) m[y + i * dy[type]][x + i * dx[type]] = type; +} + +//true can move +//false not solve +//x,y len=length to move +int state::is_movable(int d, int type, int me) +{ + int x = p[type][me] % 6, y = p[type][me] / 6; + int nex = x + direc[d] * dx[type], ney = y + direc[d] * dy[type]; + if (d == MINUS) + { + nex = x + -dx[type], ney = y - dy[type]; + } + else if (d == PLUS) + { + nex = x + (block_len[type]) * dx[type]; + ney = y + (block_len[type]) * dy[type]; + } + if (ney < 0 || ney >= 6 || nex < 0) + { + return FAILED; + } + if (nex >= 6) + { + if (type == WHITE) + { + return SOLVED; + } + else + { + return FAILED; + } + } + if (m[ney][nex] != UNDEFINE) + { + return FAILED; + } + //actual move + if (d == MINUS) + { + m[ney][nex] = type; + m[y + (block_len[type] - 1) * dy[type]][x + (block_len[type] - 1) * dx[type]] = UNDEFINE; + } + else if (d == PLUS) + { + m[y][x] = UNDEFINE; + m[ney][nex] = type; + } + p[type][me] = (y + dy[type] * direc[d]) * 6 + x + dx[type] * direc[d]; + return MOVABLE; +} + +int bfs(state &ini) +{ + state now, tmp; + queue Q; + set S; + S.insert(ini); + Q.push(ini); + while (!Q.empty()) + { + now = Q.front(); + Q.pop(); + rep(i, 5)//type + { + rep(j, now.p[i].size()) + { + rep(k, 2)//direc + { + tmp = now; + tmp.cnt++; + while (true) + { + int val = tmp.is_movable(k, i, j); + if (val == SOLVED) + { + return now.cnt + 1; + } + else if (val == MOVABLE) + ; + else + { + break; + } + if (S.find(tmp) == S.end()) + { + Q.push(tmp), S.insert(tmp); + } + } + } + } + } + } + return -1; +} + +main() +{ + int te, tc = 1; + scanf("%d", &te); + while (te--) + { + state ini; + ini.cnt = 0; + rep(i, 6) rep(j, 6) ini.m[i][j] = UNDEFINE; + int x, y; + scanf("%d%d", &y, &x); + ini.p[WHITE].push_back(x + y * 6); + ini.put(WHITE, x, y); + REP(i, 1, 5) + { + int num; + scanf("%d", &num); + rep(j, num) + { + scanf("%d%d", &y, &x); + ini.put(i, x, y); + ini.p[i].push_back(x + y * 6); + } + } + //ini.print(); + int ans = bfs(ini); + printf("The minimal number of moves to solve puzzle %d is %d.\n", tc++, ans); + } + return false; +} diff --git a/10705.cpp b/10705.cpp new file mode 100644 index 0000000..f7e74bd --- /dev/null +++ b/10705.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++) +#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++) +#define fill(ar, val) memset(ar, val, sizeof(ar)) + +#define maxN 70 +#define MIN -9223372036854775807LL + +typedef long long LL; +typedef unsigned long long ULL; + +bool mark[maxN], solved; +char res[maxN]; +int k; +LL n; +ULL pw[maxN]; +string s; + +void backTrack(int x, LL val) +{ + if (solved) + { + return; + } + if (x == k) + { + if (val == n) + { + solved = true; + } + return; + } + LL mn = val, mx = val; + fr(i, x, k - 1) + { + if (s[i] == 'n') + { + mn -= pw[k - i - 1]; + } + else if (s[i] == 'p') + { + mx += pw[k - i - 1]; + } + } + if (n < mn || mx < n) + { + return; + } + LL add = pw[k - x - 1]; + if (s[x] == 'n') + { + add = -add; + } + res[x] = 1 + '0'; + backTrack(x + 1, val + add); + if (solved) + { + return; + } + res[x] = 0 + '0'; + backTrack(x + 1, val); +} + +int main() +{ + pw[0] = 1LL; + fr(i, 1, 63) + { + pw[i] = pw[i - 1] * 2LL; + } + int cases; + for (scanf("%d", &cases); cases--;) + { + scanf(" %d ", &k); + cin >> s >> n; + fill(mark, false); + solved = false; + if (n < MIN) + { + if (k == 64 && s[0] == 'n') + { + rep(i, k) + { + res[i] = '0'; + } + res[0] = '1'; + solved = true; + } + } + else + { + backTrack(0, 0LL); + } + if (!solved) + { + printf("Impossible\n"); + } + else + { + rep(i, k) + { + putchar(res[i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/10706.cpp b/10706.cpp new file mode 100644 index 0000000..c4bdf11 --- /dev/null +++ b/10706.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +/* +10706 +Number sequence +*/ +typedef long long bg; +bg N; +struct ss +{ + bg len, total; + bg st, end; +} digit[10]; +void Ini() +{ + bg i, st = 9, k = 10, l = 99, total, d, g = 9; + digit[1].st = 1; + digit[1].end = 9; + digit[1].total = 45; + for (i = 2; i < 6; i++) + { + st += i; + digit[i].st = st; + digit[i].end = (l - k) * i + st; + d = st + digit[i].end; + total = ((digit[i].end - st) / i) + 1; + digit[i].total = (d * total) / 2; + k *= 10; + l = l * 10 + 9; + g += digit[i].len; + st = g; + } +} +void GetLen() +{ + bg i, f = 1, s = 9, d; + for (i = 1; i < 6; i++) + { + d = s - f; + d = (d * i) + i; + digit[i].len = d; + f = f * 10; + s = s * 10 + 9; + } +} +bg digitNo() +{ + bg i, sum = 0; + for (i = 1; i < 6; i++) + { + sum += digit[i].total; + if (sum >= N) + { + break; + } + } + if (sum == N) + { + return -1; + } + return i; +} +bg Sum(bg f, bg s, bg d) +{ + bg sum; + sum = ((f + s) * d) / 2; + return sum; +} +bg Binary(bg ndigit, bg total) +{ + bg lo, up, sum, mid, k, temp, g; + lo = digit[ndigit].st; + up = digit[ndigit].end; + if (lo >= total) + { + return 0; + } + k = ((up - lo) / ndigit) + 1; + k /= 2; + mid = lo + k * ndigit; + sum = Sum(lo, mid, k + 1); + while (1) + { + if (sum > total) + { + temp = sum - mid; + if (temp < total) + { + return temp; + } + if (temp == total) + { + return temp - (mid - ndigit); + } + up = mid - ndigit; + } + if (sum < total) + { + temp = sum + (mid + ndigit); + if (temp >= total) + { + return sum; + } + lo = mid + ndigit; + } + else if (sum == total) + { + return sum - mid; + } + k = ((up - lo) / ndigit) + 1; + k /= 2; + mid = lo + k * ndigit; + g = ((mid - digit[ndigit].st) / ndigit) + 1; + sum = Sum(digit[ndigit].st, mid, g); + } + return 0; +} +bg dis(bg nn) +{ + int i, sum = 0; + for (i = 1; i < 6; i++) + { + sum += digit[i].len; + if (sum >= nn) + { + break; + } + } + return i; +} +void Final(bg n, bg dig) +{ + bg A[10]; + bg a, b = 1, c, d, k = 0; + for (a = 1; a < dig; a++) + { + b *= 10; + } + d = (n + dig - 1) / dig; + d--; + c = b + d; + b = d * dig + 1; + while (c) + { + A[k++] = c % 10; + c /= 10; + } + for (a = k - 1; a >= 0; a--) + { + if (b == n) + { + printf("%lld\n", A[a]); + return; + } + b++; + } +} +void Cal() +{ + bg i, j, k, nodigit, sum = 0, before, xx; + nodigit = digitNo(); + if (nodigit < 0) + { + puts("9"); + return; + } + for (i = 1; i < nodigit; i++) + { + sum += digit[i].total; + } + before = Binary(nodigit, N - sum); + before = N - sum - before; + sum = 0; + for (i = 1; i < 6; i++) + { + sum += digit[i].len; + if (sum >= before) + { + break; + } + } + sum -= digit[i].len; + Final(before - sum, i); +} +int main() +{ + GetLen(); + Ini(); + bg kases; + scanf("%lld", &kases); + while (kases--) + { + scanf("%lld", &N); + Cal(); + } + return 0; +} diff --git a/10707.cpp b/10707.cpp new file mode 100644 index 0000000..8df62bb --- /dev/null +++ b/10707.cpp @@ -0,0 +1,230 @@ +#include + +using namespace std; + +typedef struct _piece piece; + +bool field[100][100]; +bool tmp_field[100][100]; +int colors[100][100]; + +struct _piece +{ + bool p[100][100]; + bool taken; + int w, h; + + _piece() : taken(false) + { + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + { + p[i][j] = false; + } + } + + void initialize(int minx, int miny, int maxx, int maxy, int color) + { + w = maxx - minx + 1; + h = maxy - miny + 1; + for (int i = minx; i <= maxx; i++) + { + for (int j = miny; j <= maxy; j++) + { + if (!field[i][j] || colors[i][j] != color) + { + continue; + } + p[i - minx][j - miny] = true; + } + } + } + + bool is_same(piece &other) + { + if (w != other.w || h != other.h) + { + return false; + } + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + if (p[i][j] != other.p[i][j]) + { + return false; + } + } + } + return true; + } + + void rotate() + { + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) + { + tmp_field[i][j] = p[i][j]; + } + swap(w, h); + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + p[i][j] = tmp_field[j][w - 1 - i]; + } + } + } + + void reverse() + { + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h / 2; j++) + { + swap(p[i][j], p[i][h - 1 - j]); + } + } + } + + void output() + { + cout << "DEBUG" << endl; + cout << "W: " << w << " H: " << h << endl; + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + cout << p[i][j]; + } + cout << endl; + } + cout << "DEBUGEND" << endl; + } +}; + +void clear_field() +{ + for (int i = 0; i < 100; i++) + { + for (int j = 0; j < 100; j++) + { + field[i][j] = false; + colors[i][j] = -1; + } + } +} + +int dx[] = {0, 0, -1, 1}; +int dy[] = {1, -1, 0, 0}; + +void fill(int w, int h, int W, int H, int color, int &minx, int &miny, int &maxx, int &maxy) +{ + colors[w][h] = color; + minx = min(w, minx); + miny = min(h, miny); + maxx = max(w, maxx); + maxy = max(h, maxy); + for (int i = 0; i < 4; i++) + { + int nextx = w + dx[i], nexty = h + dy[i]; + if (nextx == -1 || nextx == W || nexty == -1 || nexty == H || !field[nextx][nexty] || colors[nextx][nexty] != -1) + { + continue; + } + fill(nextx, nexty, W, H, color, minx, miny, maxx, maxy); + } +} + +bool are_similar(piece &a, piece &b) +{ + bool result = false; + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 4; j++) + { + b.rotate(); + if (a.is_same(b)) + { + result = true; + } + } + b.reverse(); + } + return result; +} + +bool same_board(vector &a, vector &b) +{ + if (a.size() != b.size()) + { + return false; + } + for (int i = 0, sz = a.size(); i < sz; i++) + { + for (int j = 0; j < sz; j++) + { + if (b[j].taken) + { + continue; + } + if (are_similar(a[i], b[j])) + { + a[i].taken = true; + b[j].taken = true; + break; + } + } + if (!a[i].taken) + { + return false; + } + } + return true; +} + +int main() +{ + int number_of_test_cases, W, H, n, x, y; + cin >> number_of_test_cases; + while (number_of_test_cases--) + { + cin >> W >> H >> n; + vector pieces[2]; + for (int i = 0; i < 2; i++) + { + clear_field(); + for (int j = 0; j < n; j++) + { + cin >> x >> y; + field[x][y] = true; + } + int color = 0; + for (int w = 0; w < W; w++) + { + for (int h = 0; h < H; h++) + { + if (!field[w][h] || colors[w][h] != -1) + { + continue; + } + int minx = w, miny = h, maxx = w, maxy = h; + fill(w, h, W, H, color, minx, miny, maxx, maxy); + piece tmp; + tmp.initialize(minx, miny, maxx, maxy, color); + pieces[i].push_back(tmp); + color++; + } + } + } + if (same_board(pieces[0], pieces[1])) + { + cout << "YES" << endl; + } + else + { + cout << "NO" << endl; + } + } + return 0; +} diff --git a/10708.cpp b/10708.cpp new file mode 100644 index 0000000..b2c53ae --- /dev/null +++ b/10708.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +const double pi = acos(-1); +const double eps = 1e-10; + +#define RAD(a) (a * pi / 180) +#define ARG(a) (a * 180 / pi) + +inline bool isp(P a, P b, P c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + eps; +} + +inline bool can_catch(double X, double cx, double Y, double cy, + double v, double l, double px, double py) +{ + double x = X - cx, y = Y - cy; + //if (isp(P(X,Y),P(px,py),P(cx,cy)))return true; + return !(sqrt(x * x + y * y) / v > l); +} + +void solve(double cx, double cy, double px, double py, + double dpx, double dpy, double U, double V, double L) +{ + if (L < 1e-10) + { + if (abs(cx - px) < eps && abs(cy - py) < eps) + { + printf("%.2lf %.2lf %.2lf %.2lf\n", + 0 + eps, 0 + eps, cx + eps, cy + eps); + } + else + { + puts("sorry, buddy"); + } + return; + } + double t = 0; + double l = 0, r = L; + rep(i, 300) + { + t = (l + r) / 2; + double X = px + U * t * dpx; + double Y = py + U * t * dpy; + if (can_catch(X, cx, Y, cy, V, t, px, py)) + { + r = t - eps; + } + else + { + l = t + eps; + } + } + if (abs(l - L) < 1e-9) + { + puts("sorry, buddy"); + } + else + { + double X = px + U * t * dpx; + double Y = py + U * t * dpy; + double C = ARG(atan2(Y - cy, X - cx)); + if (C < -eps) + { + C += 360; + } + printf("%.2lf %.2lf %.2lf %.2lf\n", + C + eps, t + eps, X + eps, Y + eps); + } +} + +main() +{ + int te; + cin >> te; + while (te--) + { + double cx, cy, px, py, p, u, v, l; + cin >> cx >> cy >> px >> py >> p >> u >> v >> l; + solve(cx, cy, px, py, cos(RAD(p)), sin(RAD(p)), + u, v, l); + } +} diff --git a/10709.cpp b/10709.cpp new file mode 100644 index 0000000..8cdb7d6 --- /dev/null +++ b/10709.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +#define eps 0.00000001 +int main() +{ + int is; + double x1, y1, x2, y2, x3, y3, x4, y4, xp, yp; + double v, d, d1, d2, d3, d4, a1, b1, c1, a2, b2, c2; + char s1[5], s2[5]; + while (scanf("%lf%lf%lf%lf%s%lf%lf%lf%lf%s", &x1, &y1, &x2, &y2, s1, &x3, &y3, &x4, &y4, s2) == 10) + { + if (s1[0] == 'E') + { + break; + } + is = 1; + a1 = y2 - y1, b1 = x2 - x1, c1 = x1 * y2 - x2 * y1; + v = sqrt(a1 * a1 + b1 * b1); + a1 /= v, b1 /= v, c1 /= v; + a2 = y4 - y3, b2 = x4 - x3, c2 = x3 * y4 - x4 * y3; + v = sqrt(a2 * a2 + b2 * b2); + a2 /= v, b2 /= v, c2 /= v; + d1 = a2 * x1 - b2 * y1 - c2; + d2 = a2 * x2 - b2 * y2 - c2; + d3 = a1 * x3 - b1 * y3 - c1; + d4 = a1 * x4 - b1 * y4 - c1; + /*printf("d1=%.2lf d2=%.2lf d3=%.2lf d4=%.2lf\n",d1,d2,d3,d4);*/ + if (s1[1]) + { + if (s2[1]) + { + if (d1 * d2 >= 0 || d3 * d4 >= 0) + { + d = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1); + v = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2); + if (v < d) + { + d = v; + } + v = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); + if (v < d) + { + d = v; + } + v = (x4 - x2) * (x4 - x2) + (y4 - y2) * (y4 - y2); + if (v < d) + { + d = v; + } + xp = x1 - a2 * d1, yp = y1 + b2 * d1; + if ((xp - x3) * (xp - x4) < 0 || (yp - y3) * (yp - y4) < 0) + { + v = (xp - x1) * (xp - x1) + (yp - y1) * (yp - y1); + if (v < d) + { + d = v; + } + } + xp = x2 - a2 * d2, yp = y2 + b2 * d2; + if ((xp - x3) * (xp - x4) < 0 || (yp - y3) * (yp - y4) < 0) + { + v = (xp - x2) * (xp - x2) + (yp - y2) * (yp - y2); + if (v < d) + { + d = v; + } + } + xp = x3 - a1 * d3, yp = y3 + b1 * d3; + if ((xp - x1) * (xp - x2) < 0 || (yp - y1) * (yp - y2) < 0) + { + v = (xp - x3) * (xp - x3) + (yp - y3) * (yp - y3); + if (v < d) + { + d = v; + } + } + xp = x4 - a1 * d4, yp = y4 + b1 * d4; + if ((xp - x1) * (xp - x2) < 0 || (yp - y1) * (yp - y2) < 0) + { + v = (xp - x4) * (xp - x4) + (yp - y4) * (yp - y4); + if (v < d) + { + d = v; + } + } + d = sqrt(d); + } + else + { + d = 0.0; + } + } + else + { + if (d1 * d2 <= 0) + { + d = 0.0; + } + else + { + d1 = fabs(d1), d2 = fabs(d2); + if (d1 < d2) + { + d = d1; + } + else + { + d = d2; + } + } + } + } + else + { + if (s2[1]) + { + if (d3 * d4 <= 0) + { + d = 0.0; + } + else + { + d3 = fabs(d3), d4 = fabs(d4); + if (d3 < d4) + { + d = d3; + } + else + { + d = d4; + } + } + } + else if (fabs(a1 * b2 - b1 * a2) < eps) + { + d = fabs(d1); + } + else + { + d = 0; + } + } + printf("%.5lf\n", d); + } + return 0; +} diff --git a/10710.cpp b/10710.cpp new file mode 100644 index 0000000..3f71df8 --- /dev/null +++ b/10710.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef long long ll; + +ll mypow(ll n, ll mod) +{ + if (n == 1) + { + return 2; + } + if (n == 0) + { + return 1; + } + ll ret = mypow(n / 2, mod); + ret = ret * ret; + ret %= mod; + if (n % 2 == 1) + { + ret *= 2; + } + return ret % mod; +} + +bool solve(ll n) +{ + if (mypow(n - 1, n) != 1) + { + return false; + } + else + { + return true; + } +} + +main() +{ + ll n; + while (cin >> n && n != -1) + { + if (solve(n)) + { + cout << n << " is a Jimmy-number" << endl; + } + else + { + cout << n << " is not a Jimmy-number" << endl; + } + } +} diff --git a/10711.cpp b/10711.cpp new file mode 100644 index 0000000..2cf31a1 --- /dev/null +++ b/10711.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +struct Point +{ + double x, y, z; +}; + +double dist(Point a, Point b) +{ + double dx = a.x - b.x; + double dy = a.y - b.y; + double dz = a.z - b.z; + return sqrt(dx * dx + dy * dy + dz * dz); +} + +#if 1 +double area(Point o, Point a, Point b) +{ + a.x -= o.x; + a.y -= o.y; + a.z -= o.z; + b.x -= o.x; + b.y -= o.y; + b.z -= o.z; + double x = (a.y * b.z - a.z * b.y); + double y = -(a.x * b.z - a.z * b.x); + double z = (a.x * b.y - a.y * b.x); + return sqrt(max(x * x + y * y + z * z, 0.0)) / 2; +} +#else +double area(Point A, Point B, Point C) +{ + double a = dist(A, B); + double b = dist(B, C); + double c = dist(A, C); + double p = (a + b + c) / 2; + double s = sqrt(max(p * (p - a) * (p - b) * (p - c), 0.0)); + return s; +} +#endif + +int M, N; +double Down[32][32], Right[32][32]; +double dp[22][22][22][22]; + +int main() +{ + Point P[50], Q[50]; + double z2; + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d %d %lf", &M, &N, &z2); + for (int i = 0; i < M; i++) + { + scanf("%lf %lf", &P[i].x, &P[i].y); + P[i].z = 0; + } + for (int i = 0; i < N; i++) + { + scanf("%lf %lf", &Q[i].x, &Q[i].y); + Q[i].z = z2; + } + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + Down[i][j] = area(P[i], P[(i + 1) % M], Q[j]); + Right[i][j] = area(P[i], Q[j], Q[(j + 1) % N]); + } + } + for (int m = 0; m <= M; m++) + { + for (int n = 0; n <= N; n++) + { + for (int i = 0; i < M; i++) + { + for (int j = 0; j < N; j++) + { + double &res = dp[m][n][i][j]; + res = 1e20; + if (m == 0 && n == 0) + { + res = 0; + continue; + } + if (m > 0) + { + res = min(res, dp[m - 1][n][(i + 1) % M][j] + Down[i][j]); + } + if (n > 0) + { + res = min(res, dp[m][n - 1][i][(j + 1) % N] + Right[i][j]); + } + } + } + } + } + double best = 1e20; + for (int i = 0; i < M; i++) + for (int j = 0; j < N; j++) + { + best = min(best, dp[M][N][i][j]); + } + printf("%.5f\n", best); + } +} diff --git a/10712.cpp b/10712.cpp new file mode 100644 index 0000000..166090b --- /dev/null +++ b/10712.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +char B[16], S[16]; +int F[16], bn, sn, pw[16][16]; + +int tab[6][16][2][2]; + +int f(int sp, int bp, int u, int z) +{ + int i, r = 0, d; + if (tab[sp][bp][u][z] >= 0) + { + return tab[sp][bp][u][z]; + } +#define RET(r) return (tab[sp][bp][u][z] = (r)) + if (sp == sn) + { + if (z) + { + RET(0); + } + if (bn == bp) + { + RET(1); + } + if (u) + { + RET(pw[10][bn - bp]); + } + for (i = bp; i < bn; i++) + { + r = r * 10 + B[i] - '0'; + } + RET(r + 1); + } + if (bp >= bn) + { + RET(0); + } + for (d = 0; d <= 9; d++) + { + if (!u && d > B[bp] - '0') + { + break; + } + for (i = sp; i > 0 && S[i] != d + '0'; i = F[i]) + ; + if (S[i] == d + '0') + { + i++; + } + if (z && S[0] == '0') + { + i = 0; + } + r += f(i, bp + 1, u || (d < B[bp] - '0'), z && (d == 0)); + } + RET(r); +} + +int count(int b, int s) +{ + int i, j; + if (b < 0) + { + return 0; + } + sprintf(B, "%d", b); + bn = strlen(B); + sprintf(S, "%d", s); + sn = strlen(S); + memset(F, 0, sizeof(F)); + for (i = 1; i < sn; i++) + { + for (j = F[i]; j > 0 && S[j] != S[i]; j = F[j]) + ; + F[i + 1] = (S[j] == S[i]) ? (j + 1) : j; + } + //printf("B='%s'\nS='%s'\n",B,S);printf("F=");for(i=0;i<=sn;i++)printf(" %d",F[i]);printf("\n"); + memset(tab, 0xFF, sizeof(tab)); + //printf("count(b=%d,s=%d)=%d\n",b,s, f(0,0,0,1)+(s==0?1:0)); + return f(0, 0, 0, 1) + (s == 0 ? 1 : 0); +} + +int main() +{ + int i, j, a, b, n; + for (i = 1; i < 16; i++) + for (pw[i][0] = j = 1; j < 16; j++) + { + pw[i][j] = pw[i][j - 1] * i; + } + while (scanf("%d %d %d", &a, &b, &n) == 3 && n >= 0) + { + printf("%d\n", count(b, n) - count(a - 1, n)); + } + return 0; +} diff --git a/10713.cpp b/10713.cpp new file mode 100644 index 0000000..5048bd9 --- /dev/null +++ b/10713.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +string opx[2][4] = {"", "east", "east", "east", "", "west", "west", "west"}; +string opy[2][4] = {"north", "north", "", "south", "south", "south", "", "north"}; +P d[4]; + +double cross(P a, P b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +double dot(P a, P b) +{ + return a.real() * b.real() + a.imag() * b.imag(); +} + +bool isp(P a, P b, P c) +{ + return abs(cross(b - a, c - a)) / abs(b - a) < 1e-10; +} + +P intersection_ll(P a1, P a2, P b1, P b2) +{ + P a = a2 - a1, b = b2 - b1; + return a1 + a * cross(b, b1 - a1) / cross(b, a); +} + +void solve(P my, P des, double r) +{ + int indx, indy; + if (my.real() <= des.real()) + { + indx = 0; + } + else + { + indx = 1; + } + if (my.imag() <= des.imag()) + { + indy = 0; + } + else + { + indy = 1; + } + rep(i, 4) + { + if (isp(my, my + d[i], des)) + { + printf("%s%s %.10lf\n", + opy[i % 2 == 1 ? indx : indy][i].c_str(), + opx[indx][i].c_str(), + abs(my - des)); + return; + } + } + P mid; + double ans = 1e100; + int indi = -1, indj = -1; + rep(i, 4) + { + rep(j, 4) + { + if (i == j) + { + continue; + } + P tmid = intersection_ll(my, my + d[i], des, des + d[j]); + if (abs(tmid) > r) + { + continue; + } + double tmp = abs(tmid - my) + abs(tmid - des); + if (tmp < ans) + { + mid = tmid; + ans = tmp; + indi = i; + indj = j; + } + } + } + if (indi == -1) + { + exit(1); + } + printf("%s%s %.10lf\n", + opy[indi % 2 == 1 ? indx : indy][indi].c_str(), + opx[indx][indi].c_str(), + abs(my - mid)); + printf("%s%s %.10lf\n", + opy[indj % 2 == 1 ? indx : indy][indj].c_str(), + opx[indx][indj].c_str(), + abs(des - mid)); +} + +int main() +{ + d[0] = P(0, 1); + d[1] = P(1, 1); + d[2] = P(1, 0); + d[3] = P(1, -1); + int tc = 0; + double r,re1,im1,re2,im2; + while (scanf("%lf%lf%lf%lf%lf", &r,&re1,&im1,&re2,&im2) && (r >= 0)) + { + if (tc++) + { + puts(""); + } + solve(P(re1,im1), P(re2,im2), r); + } + return 0; +} diff --git a/10714.cpp b/10714.cpp index 365b1e3..40dec9a 100644 --- a/10714.cpp +++ b/10714.cpp @@ -1,20 +1,29 @@ -#include +#include -#define min(a, b) ((a) < (b) ? (a) : (b)) -#define max(a, b) ((a) > (b) ? (a) : (b)) +using namespace std; -int main() { - int T, N, M, I, J; - scanf("%d", &T); - while (T--) { - int minM = 0, maxM = 0; - scanf("%d%d", &M, &N); - while (N--) { - scanf("%d", &I); J = M - I; - minM = max(minM, min(I, J)); - maxM = max(maxM, max(I, J)); +/* +10714 +Ants +*/ +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +int main() +{ + int i, max, min, n, m, kases, k; + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d", &n, &m); + max = 0; + min = 0; + for (i = 0; i < m; i++) + { + scanf("%d", &k); + min = MAX(min, MIN(k, n - k)); + max = MAX(max, MAX(k, n - k)); } - printf("%d %d\n", minM, maxM); + printf("%d %d\n", min, max); } return 0; } diff --git a/10715.cpp b/10715.cpp new file mode 100644 index 0000000..47ba91c --- /dev/null +++ b/10715.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define MAXI (10000) +#define MAXV ((MAXI * 1.02) / 2.02) +#define MINV ((MAXI * 0.98) / 1.98) +#define POSI(a) ((int)(MAXI * (a) / tot)) + +double pes[110], norm_[110], possiv[MAXI + 10]; +int n, qual[MAXI + 10], pre[MAXI + 10], possi[MAXI + 10]; + +void imprime(int p) +{ + if (qual[p] == -1 || pre[p] == -1) + { + return; + } + imprime(pre[p]); + cout << (qual[p] + 1) << " "; +} + +int main() +{ + while (1) + { + double tot = 0; + cin >> n; + if (n == 0) + { + return 0; + } + for (int i = 0; i != n; i++) + { + cin >> pes[i]; + tot += pes[i]; + } + for (int i = 0; i != n; i++) + { + norm_[i] = (tot * pes[i]) / MAXI; + } + for (int i = 0; i <= MAXI; i++) + { + possi[i] = 0; + } + possi[0] = 1; + possiv[0] = 0.0; + pre[0] = -1; + qual[0] = -1; + int maxip = 0; + int npos; + for (int i = 0; i != n; i++) + { + for (int j = maxip; j >= 0; j--) + { + if (possi[j]) + { + npos = POSI(possiv[j] + pes[i]); + if (npos > MAXV) + { + continue; + } + if (npos < MAXV && npos > MINV) + { + imprime(j); + cout << (i + 1) << endl; + goto f; + } + if (!possi[npos]) + { + if (npos > maxip) + { + maxip = npos; + } + possi[npos] = 1; + pre[npos] = j; + qual[npos] = i; + possiv[npos] = possiv[j] + pes[i]; + } + } + } + } + f:; + } + return 0; +} diff --git a/10716.cpp b/10716.cpp new file mode 100644 index 0000000..f25117e --- /dev/null +++ b/10716.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +char ss[102]; +int fre[30], cnt, len; + +int f() +{ + int i, j; + len = strlen(ss); + for (i = 0; ss[i]; i++) + { + j = ss[i] - 'a'; + fre[j]++; + } + j = 0; + for (i = 0; i < 26; i++) + { + if (fre[i] % 2) + { + j++; + } + } + if (j > 1) + { + return 1; + } + if (j > 0 && len % 2 == 0) + { + return 1; + } + return 0; +} +int FindB(int n, int m, char ch) +{ + int i; + for (i = m; i > n; i--) + if (ss[i] == ch) + { + break; + } + return i; +} +int FindF(int n, int m, char ch) +{ + int i; + for (i = n; i < m; i++) + if (ss[i] == ch) + { + break; + } + return i; +} +void MoveB(int n, int m, char ch) +{ + int i; + for (i = n; i < m; i++) + { + ss[i] = ss[i + 1]; + } + ss[m] = ch; +} + +void MoveF(int n, int m, char ch) +{ + int i; + for (i = m; i > n; i--) + { + ss[i] = ss[i - 1]; + } + ss[n] = ch; +} + +void recur(int lo, int up) +{ + int k, l, ind1, d1, d2, ind2; + if (lo >= up) + { + return; + } + k = ss[lo] - 'a'; + l = ss[up] - 'a'; + if (ss[lo] == ss[up]) + { + recur(lo + 1, up - 1); + } + else + { + d1 = d2 = 1000; + if (fre[k] > 1) + { + ind1 = FindB(lo, up, ss[lo]); + d1 = up - ind1; + } + if (fre[l] > 1) + { + ind2 = FindF(lo, up, ss[up]); + d2 = ind2 - lo; + } + if (d1 <= d2) + { + cnt += d1; + MoveB(ind1, up, ss[lo]); + fre[k] -= 2; + recur(lo + 1, up - 1); + } + else + { + cnt += d2; + MoveF(lo, ind2, ss[up]); + fre[l] -= 2; + recur(lo + 1, up - 1); + } + } +} + +void cal() +{ + if (f()) + { + printf("Impossible\n"); + return; + } + cnt = 0; + recur(0, strlen(ss) - 1); + cout << cnt << endl; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + cin >> ss; + cal(); + for (int i = 0; i < 26; i++) + { + fre[i] = 0; + } + } + return 0; +} diff --git a/10717.cpp b/10717.cpp new file mode 100644 index 0000000..cac965a --- /dev/null +++ b/10717.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +typedef long long ss; + +ss coin, table, C[110], G[110][110]; + +ss gcd(ss a, ss b) +{ + return b ? gcd(b, a % b) : a; +} +int com(const void *a, const void *b) +{ + return *(ss *)a - *(ss *)b; +} +int Lucky(ss n) +{ + ss i, c = 0; + for (i = 0; i < coin; i++) + { + if (C[i] && n % C[i] == 0) + c++; + if (c == 4) + { + printf("%lld %lld\n", n, n); + return 1; + } + } + return 0; +} +void Lower(ss n) +{ + ss i, j, k, l, p, q, r, x, y, s, t, z; + ss lo = 10000000, up = 100000000; + ss a[110] = {0}; + for (i = 0; i <= coin - 4; i++) + { + if (a[i] || !C[i]) + continue; + if (C[i] >= n) + break; + p = (n / C[i]); + q = p * C[i]; + r = n - q; + z = q + C[i]; + if (r > lo && z > up) + continue; + for (j = i + 1; j <= coin - 3; j++) + { + if (!C[j]) + continue; + if (C[j] % C[i] == 0) + a[j] = 1; + x = gcd(C[i], C[j]); + s = (C[i] * C[j]) / x; + q = (n / s); + p = n - q * s; + z = q * s + s; + if (p > lo && z > up) + continue; + for (k = j + 1; k <= coin - 2; k++) + { + if (!C[k]) + continue; + if (C[k] % C[i] == 0) + a[k] = 1; + y = gcd(s, C[k]); + t = (s * C[k]) / y; + q = (n / t); + p = n - q * t; + z = q * t + t; + if (p > lo && z > up) + continue; + for (l = k + 1; l < coin; l++) + { + if (C[l] % C[i] == 0) + a[l] = 1; + y = gcd(t, C[l]); + y = (t * C[l]) / y; + q = (n / y); + p = n - q * y; + z = q * y + y; + if (p < lo && y <= n) + lo = p; + if (z < up) + up = z; + } + } + } + } + if (lo < 1000000) + printf("%lld", n - lo); + else + printf("0"); + printf(" %lld\n", up); +} + +int main() +{ + ss i, n, k = 0; + while (scanf("%lld%lld", &coin, &table) == 2) + { + if (!coin && !table) + break; + for (i = 0; i < coin; i++) + scanf("%lld", &C[i]); + qsort(C, coin, sizeof(ss), com); + while (table--) + { + scanf("%lld", &n); + if (Lucky(n)) + continue; + Lower(n); + } + } + return 0; +} diff --git a/10718.cpp b/10718.cpp index 98c0223..df3c4cd 100644 --- a/10718.cpp +++ b/10718.cpp @@ -1,19 +1,90 @@ -#include +#include -int main() { - long long N, L, U, M; - while (scanf("%lld%lld%lld", &N, &L, &U) != EOF) { - M = 0; - for (int shift = 32; shift >= 0; shift--) { - long long mask = 1LL< b ? a : b) + +typedef long long ss; + +ss A[40], B[40], ind1, ind2, N, L, U, all; + +void Binary(ss n, ss a[], ss &ind) +{ + ind = 0; + all = 1; + while (n) + { + a[ind++] = n % 2; + if (a[ind - 1] == 0) + all = 0; + n /= 2; + } +} +void Rev(ss a[], ss n) +{ + ss temp[40], i, j = 0; + for (i = n - 1; i >= 0; i--) + temp[j++] = a[i]; + for (i = 0; i < n; i++) + a[i + 1] = temp[i]; +} +int Recur(ss limit, ss level, ss sum) +{ + ss x, y, z; + x = limit - level + 1; + y = (ss)pow(2, (double)x) - 1; + if (sum > U) + return 0; + if (sum + y < L) + return 0; + if (level == limit + 1 && sum <= U) + { + printf("%lld\n", sum); + return 1; + } + if (A[level] == 0) + { + z = sum + (ss)pow(2, double(limit - level)); + if (Recur(limit, level + 1, z)) + return 1; + if (Recur(limit, level + 1, sum)) + return 1; } + if (A[level] == 1) + { + if (Recur(limit, level + 1, sum)) + return 1; + z = sum + (ss)(pow(2, (double)(limit - level))); + if (Recur(limit, level + 1, z)) + return 1; + } + return 0; +} +void Cal() +{ + ss max; + if (U == L) + { + printf("%lld\n", L); + return; + } + for (max = 0; max < 35; max++) + A[max] = 0; + Binary(N, A, ind1); + if (all && N >= U) + { + printf("%lld\n", L); + return; + } + Binary(U, B, ind2); + max = MAX(ind1, ind2); + Rev(A, max); + Recur(max, 1, 0); +} + +int main() +{ + while (scanf("%lld%lld%lld", &N, &L, &U) == 3) + Cal(); return 0; } diff --git a/10719.cpp b/10719.cpp new file mode 100644 index 0000000..5ce4bab --- /dev/null +++ b/10719.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +/* +10719 +Quotient Polynomial +*/ +#define MAXN 10005 +char input[MAXN * 20]; +int P[MAXN], q[MAXN]; +int N, K; +void Sep() +{ + N = 0; + char *p; + p = strtok(input, " "); + while (p) + { + P[N++] = atoi(p); + p = strtok(NULL, " "); + } +} +void SolvedCase() +{ + int i, x, y, r; + q[0] = P[0]; + for (i = 1; i + 1 < N; i++) + { + x = q[i - 1] * -K; + y = P[i] - x; + q[i] = y; + } + r = q[i - 1] * (-K); + r = P[N - 1] - r; + printf("q(x):"); + for (i = 0; i + 1 < N; i++) + { + printf(" %d", q[i]); + } + printf("\n"); + printf("r = %d\n\n", r); +} +void Special() +{ + int r; + printf("q(x): %d\n", P[0]); + r = P[0] * (-K); + r = P[1] - r; + printf("r = %d\n\n", r); +} +int main() +{ + while (gets(input)) + { + K = atoi(input); + gets(input); + Sep(); + if (N > 2) + { + SolvedCase(); + } + else + { + Special(); + } + } + return 0; +} diff --git a/10720.cpp b/10720.cpp new file mode 100644 index 0000000..3bfb2d6 --- /dev/null +++ b/10720.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +int main() +{ + setvbuf(stdin, NULL, _IOFBF, 10000); + setvbuf(stdout, NULL, _IOFBF, 10000); + int n; + int arr[10240];// Degree sequence + int c[10240]; // Conjugate sequence (as per exercise 105, Chapter 7 of TAOCP) + while (scanf("%d", &n) == 1 && n) + { + int parity = 0; + bool possible = true; + for (int i = 0; i < n; ++i) + { + scanf("%d", &arr[i]); + parity ^= arr[i] & 1; + if (arr[i] >= n) + { + possible = false; + } + } + if (possible && !parity) + { + sort(arr, arr + n); + int curr = n; + int run_len = 1; + int idx = 0; + int x = 0; + for (int i = 1; i < n; ++i) + { + if (arr[i] != arr[i - 1]) + { + for (int j = 0; j < arr[i - 1] - x; ++j) + { + c[idx++] = curr; + } + curr -= run_len; + run_len = 0; + x = arr[i - 1]; + } + ++run_len; + } + for (; idx < arr[n - 1]; ++idx) + { + c[idx] = curr; + } + reverse(arr, arr + n); + int d_sum = 0;// degree sum + int c_sum = 0;// conjugate sum + for (int i = 0; i < n; ++i) + { + if (arr[i] < i + 1) + { + break; + } + d_sum += arr[i]; + c_sum += c[i]; + if (d_sum > c_sum - (i + 1)) + { + possible = false; + break; + } + } + } + if (!possible || parity) + { + puts("Not possible"); + } + else + { + puts("Possible"); + } + } + return 0; +} diff --git a/10721.cpp b/10721.cpp new file mode 100644 index 0000000..d95271b --- /dev/null +++ b/10721.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long long dp[55][55]; +int main() +{ + int i, j, k, N, K, M; + while (scanf("%d %d %d", &N, &K, &M) == 3) + { + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (i = 1; i <= K; i++) + { + for (j = 1; j <= N; j++) + { + for (k = 1; k <= M; k++) + { + if (j - k >= 0) + dp[i][j] += dp[i - 1][j - k]; + } + } + } + printf("%lld\n", dp[K][N]); + } + return 0; +} diff --git a/10722.cpp b/10722.cpp new file mode 100644 index 0000000..fb9e61f --- /dev/null +++ b/10722.cpp @@ -0,0 +1,304 @@ +#include + +using namespace std; + +#define MAXN 102 +#define BASE 10000 +#define MAX_DIGIT 250 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +typedef long INT; + +INT B, N; + +struct BigNum +{ + INT sign, len, digit[MAX_DIGIT]; + void multAndAdd(BigNum, INT, INT); + void absSubtract(BigNum, BigNum); + + void operator=(INT); + void operator=(BigNum); + void mult(BigNum, INT); + void add(BigNum, BigNum); + void subtract(BigNum, BigNum); + void absAdd(BigNum, BigNum); + void mult(BigNum, BigNum); + void print(); + void clear(); + void update(); + BigNum() + { + clear(); + } +}; +BigNum Res[129][100]; +BigNum per[129][100]; +void BigNum::absSubtract(BigNum a, BigNum b) +{ + INT pos, borrow, t; + borrow = 0; + t = max(a.len, b.len); + for (pos = 0; pos < t; ++pos) + { + digit[pos] = a.digit[pos] - b.digit[pos] - borrow; + if (digit[pos] < 0) + { + digit[pos] += BASE; + borrow = 1; + } + else + { + borrow = 0; + } + if (digit[pos] != 0) + { + len = pos + 1; + } + } +} +void BigNum::mult(BigNum a, INT s) +{ + INT pos, carry; + if (s < 0) + { + sign = 1 - a.sign; + a = -s; + } + else + { + sign = a.sign; + } + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos] = a.digit[pos] * s + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + pos = a.len; + while (carry != 0) + { + digit[pos] = carry % BASE; + carry /= BASE; + ++pos; + } + len = pos; + update(); +} +void BigNum::multAndAdd(BigNum a, INT s, INT offset) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos + offset] += a.digit[pos] * s + carry; + carry = digit[pos + offset] / BASE; + digit[pos + offset] %= BASE; + } + pos = a.len + offset; + while (carry != 0) + { + digit[pos] += carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + ++pos; + } + if (len < pos) + { + len = pos; + } + update(); +} +void BigNum::mult(BigNum a, BigNum b) +{ + INT pos; + clear(); + for (pos = 0; pos < b.len; ++pos) + { + multAndAdd(a, b.digit[pos], pos); + } + sign = (a.sign + b.sign) % 2; +} +void BigNum::operator=(INT x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + INT i; + clear(); + len = n.len; + sign = n.sign; + for (i = 0; i < len; ++i) + { + digit[i] = n.digit[i]; + } +} +void BigNum::add(BigNum a, BigNum b) +{ + clear(); + if (a.sign == b.sign) + { + absAdd(a, b); + sign = a.sign; + } + else + { + absSubtract(a, b); + sign = a.sign; + } +} +void BigNum::absAdd(BigNum a, BigNum b) +{ + INT pos, carry = 0; + for (pos = 0; pos < max(a.len, b.len); ++pos) + { + digit[pos] = a.digit[pos] + b.digit[pos] + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + if (carry) + { + digit[max(a.len, b.len)] = carry; + len = max(a.len, b.len) + 1; + } + else + { + len = max(a.len, b.len); + } + update(); +} +void BigNum::subtract(BigNum a, BigNum b) +{ + BigNum t; + clear(); + t = b; + t.sign = 1 - b.sign; + add(a, t); +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::print() +{ + INT i; + printf("%ld", digit[len - 1]); + for (i = len - 2; i >= 0; --i) + { + switch (BASE) + { + case 10: + printf("%ld", digit[i]); + break; + case 100: + printf("%02ld", digit[i]); + break; + case 1000: + printf("%03ld", digit[i]); + break; + case 10000: + printf("%04ld", digit[i]); + break; + case 100000: + printf("%05ld", digit[i]); + break; + case 1000000: + printf("%06ld", digit[i]); + break; + case 10000000: + printf("%07ld", digit[i]); + break; + case 100000000: + printf("%08ld", digit[i]); + break; + case 1000000000: + printf("%09ld", digit[i]); + break; + } + } +} +void Dynamic(int b) +{ + INT i; + BigNum temp, dummy; + Res[b][1] = b; + per[b][1] = 1; + for (i = 2; i < 100; i++) + { + per[b][i] = Res[b][i - 1]; + temp.mult(Res[b][i - 1], (b - 1)); + dummy.subtract(Res[b][i - 1], per[b][i - 1]); + temp.add(temp, dummy); + Res[b][i] = temp; + } +} +void Gen() +{ + int i; + for (i = 4; i < 129; i++) + { + Dynamic(i); + } +} +void Cal() +{ + BigNum temp, dummy; + if (N == 0) + { + printf("0\n"); + return; + } + if (N == 1) + { + printf("%ld\n", B - 1); + return; + } + dummy.mult(Res[B][N - 1], B - 2); + temp.subtract(Res[B][N - 1], per[B][N - 1]); + dummy.add(dummy, temp); + dummy.print(); + printf("\n"); +} + +int main() +{ + Gen(); + while (scanf("%ld%ld", &B, &N) == 2) + { + if (!B && !N) + break; + Cal(); + } + return 0; +} diff --git a/10723.cpp b/10723.cpp new file mode 100644 index 0000000..8dc9bfd --- /dev/null +++ b/10723.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define MAXD 31 + +typedef long long LL; + +const LL INF = 0x3f3f3f3fLL; + +char a[MAXD], b[MAXD]; +LL f[MAXD][MAXD], p[MAXD][MAXD]; + +void solve() +{ + gets(a + 1); + gets(b + 1); + int len_a = strlen(a + 1); + int len_b = strlen(b + 1); + + for (int i = 0; i < MAXD; i++) + { + for (int j = 0; j < MAXD; j++) + { + f[i][j] = 0LL; + } + } + + for (int i = 0; i <= len_a; i++) + { + for (int j = 0; j <= len_b; j++) + { + p[i][j] = INF; + } + } + for (int i = 0; i <= len_b; i++) + { + f[0][i] = 1LL; + p[0][i] = 0LL; + } + for (int i = 0; i <= len_a; i++) + { + f[i][0] = 1LL; + p[i][0] = 0LL; + } + for (int i = 1; i <= len_a; i++) + { + for (int j = 1; j <= len_b; j++) + { + if (a[i] == b[j]) + { + p[i][j] = p[i - 1][j - 1] + 1; + f[i][j] += f[i - 1][j - 1]; + } + else + { + p[i][j] = p[i - 1][j] > p[i][j - 1] ? p[i - 1][j] : p[i][j - 1]; + if (p[i - 1][j] == p[i][j]) + { + f[i][j] += f[i - 1][j]; + } + if (p[i][j - 1] == p[i][j]) + { + f[i][j] += f[i][j - 1]; + } + } + } + } + printf("%lld %lld\n", len_a + len_b - p[len_a][len_b], f[len_a][len_b]); +} + +int main() +{ + int T; + scanf("%d", &T); + char _[1]; + gets(_); // chomp newline + for (int t = 1; t <= T; t++) + { + printf("Case #%d: ", t); + solve(); + } + return 0; +} diff --git a/10724.cpp b/10724.cpp new file mode 100644 index 0000000..6728245 --- /dev/null +++ b/10724.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +#define maxn 55 +#define INF 2147483647 +#define MIN(a, b) (a > b ? b : a) + +typedef long SS; + +char links[maxn][maxn]; +double Cost[maxn][maxn]; +SS N, M; + +struct ss +{ + SS x, y; +}; +ss Point[maxn]; + +double Dis(SS x1, SS y1, SS x2, SS y2) +{ + double p, q; + p = (double)(x1 - x2); + p *= p; + q = (double)(y1 - y2); + q *= q; + return sqrt(p + q); +} +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + Cost[i][j] = Cost[j][i] = INF; + } + Cost[i][i] = 0; + } +} +void Floyd() +{ + SS i, j, k; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + Cost[i][j] = MIN(Cost[i][j], Cost[i][k] + Cost[k][j]); +} +double UpDate(SS u, SS v, double dis) +{ + SS i, j; + double d1, d2, k = 0, d; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + d1 = Cost[i][u] + dis + Cost[v][j]; + d2 = Cost[i][v] + dis + Cost[u][j]; + d1 = MIN(d2, d1); + d = Cost[i][j] - d1; + if (d > 0) + k += d; + } + } + return k; +} +void Cal() +{ + SS i, u, v, f = 1, j; + double maxDis = 0, minDis = INF, dis, upd; + Floyd(); + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + if (links[i][j] == 1) + continue; + dis = Dis(Point[i].x, Point[i].y, Point[j].x, Point[j].y); + upd = UpDate(i, j, dis); + if (upd <= 1) + continue; + if (upd > maxDis) + { + maxDis = upd; + minDis = dis; + u = i; + v = j; + f = 0; + } + else if (upd == maxDis && dis < minDis) + { + u = i; + v = j; + minDis = dis; + f = 0; + } + } + } + if (f) + cout << "No road required\n"; + else + cout << u << " " << v << endl; +} +void Free() +{ + int i, j; + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + links[i][j] = 0; +} + +int main() +{ + SS i, u, v; + ss t; + double d; + while (cin >> N >> M) + { + if (!N && !M) + break; + for (i = 1; i <= N; i++) + { + cin >> t.x >> t.y; + Point[i] = t; + } + Ini(); + for (i = 1; i <= M; i++) + { + cin >> u >> v; + d = Dis(Point[u].x, Point[u].y, Point[v].x, Point[v].y); + links[u][v] = links[v][u] = 1; + Cost[u][v] = Cost[v][u] = d; + } + Cal(); + Free(); + } + return 0; +} diff --git a/10725.cpp b/10725.cpp new file mode 100644 index 0000000..9b010b8 --- /dev/null +++ b/10725.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +double pi; + +double sol(double a, double b, double c) +{ + double A = ((b * b + c * c - a * a) / (2 * b * c)); + double B = ((a * a + c * c - b * b) / (2 * a * c)); + if (A > EPS && B > EPS) + { + A /= sqrt(1 - A * A); + B /= sqrt(1 - B * B); + return c / (1 + A + B); + } + else if (A > EPS) + { + A /= sqrt(1 - A * A); + return c / (1 + A); + } + else if (B > EPS) + { + B /= sqrt(1 - B * B); + return c / (1 + B); + } + else + { + return c; + } +} + +double solve(double a, double b, double c) +{ + double r = sol(a, b, c); + r = MAX(r, sol(a, c, b)); + r = MAX(r, sol(b, a, c)); + r = MAX(r, sol(b, c, a)); + r = MAX(r, sol(c, a, b)); + r = MAX(r, sol(c, b, a)); + return r * r; +} + +int main() +{ + double a, b, c; + int t; + pi = 2. * acos(0.); + for (scanf("%d", &t); t-- > 0 && scanf("%lf %lf %lf", &a, &b, &c) == 3;) + { + printf("%.6f\n", solve(a, b, c)); + } + return 0; +} diff --git a/10726.cpp b/10726.cpp new file mode 100644 index 0000000..fde5f68 --- /dev/null +++ b/10726.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +long c, m; +bool find(long n)//§ä³Ì¤pªºcoconut¼Æ +{ + long i; + for (i = 0; i < c; i++) + { + if ((n - m) % c) + { + return false; + } + n = n - (n - m) / c - m; + } + if (n % c == 0) + { + return true; + } + else + { + return false; + } +} +int main() +{ + long low, high; + long i, count, d, sum; + int cases; + scanf("%d", &cases); + for (int x = 1; x <= cases; x++) + { + scanf("%ld %ld %ld %ld", &c, &m, &low, &high); + if (c > 8) + { + printf("Case %d: %ld\n", x, 0); + continue; + } + else + { + for (i = low; i <= high; i++) + if (find(i)) + { + break; + } + if (i > high) + { + printf("Case %d: 0\n", x); + continue; + } + sum = i; + for (i = 0, d = 1; i < c + 1; i++) + { + d *= c; + } + count = 0; + while (sum <= high) + { + sum += d; + count++; + } + printf("Case %d: %ld\n", x, count); + } + } + return 0; +} \ No newline at end of file diff --git a/10727.cpp b/10727.cpp new file mode 100644 index 0000000..2ad3482 --- /dev/null +++ b/10727.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +// System of equations: +// sum p_i = sum w_i +// sum n_i p_i = sum n_i w_i + +int n[200], w[200], N; +double a, b; + +double p(int i, int d) +{ + double t = exp(a + b * n[i]); + if (d == 0) + { + return 1 - 1 / (1 + t); + } + else if (d == 1)// d/da + { + return t / ((1 + t) * (1 + t)); + } + else// d/db + { + return n[i] * t / ((1 + t) * (1 + t)); + } +} + +double f(int k, int d) +{ + double res = 0; + for (int i = 0; i < N; i++) + { + res += (p(i, d) - (d == 0 ? w[i] : 0)) * (k == 0 ? 1 : n[i]); + } + return res; +} + +int main() +{ + while (scanf("%d", &N) == 1 && N > 0) + { + for (int i = 0; i < N; i++) + { + scanf("%d %d", &n[i], &w[i]); + } + // Newton-Raphson's method + a = 0; + b = 0; + for (int k = 0; k < 50; k++) + { + double J[2][2] =// jacobian + { + {f(0, 1), f(0, 2)}, + {f(1, 1), f(1, 2)}}; + double det = J[0][0] * J[1][1] - J[0][1] * J[1][0]; + double JI[2][2] =// jacobian's inverse + { + {J[1][1] / det, -J[1][0] / det}, + {-J[0][1] / det, J[0][0] / det}}; + double F[2] = {f(0, 0), f(1, 0)}; + a -= (JI[0][0] * F[0] + JI[0][1] * F[1]); + b -= (JI[1][0] * F[0] + JI[1][1] * F[1]); + } + printf("%.4f %.4f\n", a, b); + } +} diff --git a/10728.cpp b/10728.cpp new file mode 100644 index 0000000..30ae8ad --- /dev/null +++ b/10728.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +char a[1000], b[1000]; + +int na, nb, i, j, k, m, n, c; +char *pa, *pb, aa[100][100], bb[100][100]; + +int main() +{ + scanf("%d ", &c); + while (c--) + { + gets(a); + gets(b); + na = nb = 0; + for (pa = strtok(a, " "); pa; pa = strtok(NULL, " ")) + { + if (*pa == '<') + { + *pa = '0'; + } + strcpy(aa[na++], pa); + } + for (pb = strtok(b, " "); pb; pb = strtok(NULL, " ")) + { + if (*pb == '<') + { + *pb = '1'; + } + strcpy(bb[nb++], pb); + } + if (na != nb) + { + printf("-\n"); + continue; + } + for (i = 0; i < na; i++) + { + if (isdigit(aa[i][0])) + { + for (j = 0; j < na; j++) + if (!strcmp(bb[j], aa[i])) + { + strcpy(bb[j], bb[i]); + } + for (j = i + 1; j < na; j++) + if (!strcmp(aa[j], aa[i])) + { + strcpy(aa[j], bb[i]); + } + for (j = 0; j <= i; j++) + if (!strcmp(aa[j], aa[i])) + { + strcpy(aa[j], bb[i]); + } + } + else if (isdigit(bb[i][0])) + { + for (j = 0; j < na; j++) + if (!strcmp(aa[j], bb[i])) + { + strcpy(aa[j], aa[i]); + } + for (j = i + 1; j < na; j++) + if (!strcmp(bb[j], bb[i])) + { + strcpy(bb[j], aa[i]); + } + for (j = 0; j <= i; j++) + if (!strcmp(bb[j], bb[i])) + { + strcpy(bb[j], aa[i]); + } + } + else if (strcmp(aa[i], bb[i])) + { + printf("-\n"); + goto dun; + } + } + for (i = 0; i < na; i++) + { + if (i) + { + printf(" "); + } + if (isdigit(aa[i][0])) + { + printf("x"); + } + else + { + printf("%s", aa[i]); + } + } + printf("\n"); + dun:; + } + if (gets(a)) + { + printf("Extra input!\n"); + } + return 0; +} diff --git a/10729.cpp b/10729.cpp new file mode 100644 index 0000000..6fdad3d --- /dev/null +++ b/10729.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +int n, c, num[205], map_[205][205]; +char id[205], buf1[2008], buf2[2008], *ptr; + +typedef struct data +{ + int id, num; + char str[205]; + data *map_[205]; +} data; + +data *head; + +void analyze(data *p) +{ + char *q; + n++; + if (p == NULL) + { + head = (data *)malloc(sizeof(data)); + p = head; + } + q = p->str; + p->id = *(ptr++); + p->num = 0; + if (*ptr == '(') + { + ptr++; + for (;;) + { + p->map_[p->num] = (data *)malloc(sizeof(data)); + *(q++) = *ptr; + analyze(p->map_[p->num]); + p->num++; + if (*(ptr++) == ')') + { + break; + } + } + } + *q = 0; +} + +void build() +{ + int now, temp; + now = c++; + id[now] = *(ptr++); + if (*ptr == '(') + { + ptr++; + for (;;) + { + map_[now][num[now]++] = c; + map_[c][num[c]++] = now; + build(); + if (*(ptr++) == ')') + { + break; + } + } + } +} + +int equal(data *p, int now, int parent) +{ + int i, j, k, temp; + char buf[405], *q; + if (parent == -1) + { + if (!num[now]) + { + return 1; + } + for (i = 0; i < num[now]; i++) + { + buf1[i] = id[map_[now][i]]; + } + buf1[i] = 0; + sprintf(buf, "%s%s\0", buf1, buf1); + for (i = 0; i < num[now]; i++) + { + if (!strncmp(buf + i, p->str, num[now])) + { + for (j = i, k = 0; k < num[now]; j++, k++) + { + if (j >= num[now]) + { + j -= num[now]; + } + if (equal(p->map_[k], map_[now][j], now) == 0) + { + break; + } + } + if (k == num[now]) + { + return 1; + } + } + } + return 0; + } + else + { + if (num[now] - 1 != p->num) + { + return 0; + } + for (i = 0, q = buf; map_[now][i] != parent; i++) + ; + for (i++, j = 1; j < num[now]; i++, j++) + { + if (i >= num[now]) + { + i -= num[now]; + } + *(q++) = id[map_[now][i]]; + } + *q = 0; + if (strcmp(p->str, buf)) + { + return 0; + } + for (temp = i + 1, i = 1; i < num[now]; i++, temp++) + { + if (temp >= num[now]) + { + temp -= num[now]; + } + if (equal(p->map_[i - 1], map_[now][temp], now) == 0) + { + return 0; + } + } + return 1; + } +} + +int main() +{ + int count, i; + scanf("%d", &count); + while (count--) + { + scanf("%s%s", buf1, buf2); + n = 0; + memset(num, 0, sizeof(num)); + head = NULL; + ptr = buf1; + analyze(head); + ptr = buf2; + c = 0; + build(); + if (n != c) + { + printf("different\n"); + } + else + { + for (i = 0; i < c; i++) + { + if (id[i] == head->id && num[i] == head->num) + { + if (equal(head, i, -1)) + { + break; + } + } + } + if (i == c) + { + printf("different\n"); + } + else + { + printf("same\n"); + } + } + } + return 0; +} diff --git a/1073.cpp b/1073.cpp new file mode 100644 index 0000000..4e9fece --- /dev/null +++ b/1073.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +long long n; + +long long cx4(long long x) +{ + return x * (x - 1) * (x - 2) * (x - 3) / 24; +} + +int main() +{ + for (int te = 1; cin >> n, n; ++te) + if (n & 1) + { + cout << "Case " << te << ": 0" << endl; + } + else + { + n = n + 4 >> 1; + cout << "Case " << te << ": " << cx4(n) + cx4(n - 1) << endl; + } + return 0; +} diff --git a/10730.cpp b/10730.cpp new file mode 100644 index 0000000..1e1d57a --- /dev/null +++ b/10730.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int V[10005], pos[10005], N; +int Find() +{ + int i, j, k, pa, pb, pc; + for (i = 0; i < N - 2; i++) + { + for (j = i + 1; j < N; j++) + { + pa = V[j] - V[i]; + pb = V[j] + pa; + if (pb < 0) + continue; + if (pb >= N) + continue; + if (pos[pb] > j) + return 1; + } + } + return 0; +} + +int main() +{ + int i; + while (scanf("%d:", &N), N) + { + for (i = 0; i < N; i++) + { + scanf("%d", &V[i]); + pos[V[i]] = i; + } + if (Find()) + puts("no"); + else + puts("yes"); + } + return 0; +} diff --git a/10731.cpp b/10731.cpp new file mode 100644 index 0000000..5088f63 --- /dev/null +++ b/10731.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +vector seen; +int ecnt; + +struct edge +{ + int next, to; + edge(int _next, int _to) : next(_next), to(_to) {} +}; + +class Tarjan +{ + vector index, lowlink, next, scc, stack; + vector g; + vector stacked; + int cnt, idx, n, top; + + void strongconnect(int u) + { + int at, v; + index[u] = lowlink[u] = idx++; + stack[top++] = u; + stacked[u] = true; + for (at = next[u]; at != -1; at = g[at].next) + { + v = g[at].to; + if (index[v] == -1) + { + strongconnect(v); + lowlink[u] = min(lowlink[u], lowlink[v]); + } + else if (stacked[v]) + { + lowlink[u] = min(lowlink[u], index[v]); + } + } + if (lowlink[u] == index[u]) + { + do + { + scc[v = stack[--top]] = cnt; + stacked[v] = false; + } while (v != u); + ++cnt; + } + } + +public: + vector run(int &scc_cnt) + { + for (int i = 0; i < n; ++i) + if (seen[i] && index[i] == -1) + { + strongconnect(i); + } + scc_cnt = cnt; + return scc; + } + Tarjan(int n, vector &graph, vector &next) + { + this->g = graph; + this->n = n; + this->next = next; + index = vector(n, -1); + lowlink = vector(n); + scc = vector(n); + stack = vector(n); + stacked = vector(n, false); + cnt = idx = top = 0; + } +}; + +void add_edge(vector &g, vector &next, int from, int to) +{ + if (from != to) + { + g.push_back(edge(next[from], to)); + next[from] = ecnt++; + } +} +int main() +{ + bool first = true; + for (int n; scanf("%d\n", &n) == 1 && n;) + { + vector g; + vector next(26, -1); + char a, b, c, d, e, f, line[1024]; + seen = vector(26, false); + ecnt = 0; + if (!first) + { + puts(""); + } + first = false; + for (int i = 0; i < n; ++i) + { + fgets(line, 1024, stdin); + a = *strtok(line, " ") - 'A'; + b = *strtok(NULL, " ") - 'A'; + c = *strtok(NULL, " ") - 'A'; + d = *strtok(NULL, " ") - 'A'; + e = *strtok(NULL, " ") - 'A'; + f = *strtok(NULL, " ") - 'A'; + seen[a] = seen[b] = seen[c] = seen[d] = seen[e] = seen[f] = true; + add_edge(g, next, f, a); + add_edge(g, next, f, b); + add_edge(g, next, f, c); + add_edge(g, next, f, d); + add_edge(g, next, f, e); + } + Tarjan t(26, g, next); + int sccs; + vector scc = t.run(sccs); + vector shown(26, false); + for (int i = 0; i < sccs; ++i) + { + int first = 1, nscc = -1; + for (int j = 0; j < 26; ++j) + { + if (nscc == -1 && seen[j] && !shown[j]) + { + nscc = scc[j]; + } + if (seen[j] && scc[j] == nscc) + { + if (!first) + { + putc(32, stdout); + } + first = 0; + printf("%c", j + 'A'); + shown[j] = true; + } + } + puts(""); + } + } + return 0; +} diff --git a/10732.cpp b/10732.cpp new file mode 100644 index 0000000..1a88d01 --- /dev/null +++ b/10732.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +int compare(const void *a, const void *b) +{ + float c = *(double *)a, d = *(double *)b; + if (c < d) + { + return -1; + } + else if (c > d) + { + return 1; + } + return 0; +} +double neg[10000], pos[10000], t; +int n, lenn, lenp, zero; +int findlower(int left, int right, double value) +{ + if (neg[right] < value) + { + return right; + } + int mid = (left + right) / 2; + while (left <= right) + { + if (neg[mid] < value && neg[mid + 1] >= value) + { + return mid; + } + if (neg[mid] > value) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + return -1; +} +int findlowerp(int left, int right, double value) +{ + if (pos[right] < value) + { + return right; + } + int mid = (left + right) / 2; + while (left <= right) + { + if (pos[mid] < value && pos[mid + 1] >= value) + { + return mid; + } + if (pos[mid] > value) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + return -1; +} +int findupper(int left, int right, double value) +{ + if (neg[left] > value) + { + return left; + } + int mid = (left + right) / 2; + while (left <= right) + { + if (neg[mid] > value && neg[mid - 1] <= value) + { + return mid; + } + if (neg[mid] > value) + { + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + return -1; +} +int main() +{ + int caseno = 1; + while (scanf("%d", &n), n) + { + lenn = lenp = zero = 0; + for (int i = 0; i < n; i++) + { + scanf("%lf", &t); + if (t < 0) + { + neg[lenn++] = t; + } + else if (t > 0) + { + pos[lenp++] = t; + } + else + { + zero++; + } + } + qsort(neg, lenn, sizeof(double), compare); + qsort(pos, lenp, sizeof(double), compare); + int sum = 0; + for (int i = 1; i < lenn; i++) + { + int lower = findlower(0, i - 1, (double)1 / neg[i]); + sum += lower + 1; + } + for (int i = 0; i < lenp; i++) + { + sum += zero; + int s = findupper(0, lenn - 1, -pos[i]); + if (s != -1) + { + sum += lenn - s; + } + int t = findlowerp(0, i - 1, (double)1 / pos[i]); + sum += t + 1; + } + printf("Case %d: %d\n", caseno++, sum); + } + return 0; +} diff --git a/10733.cpp b/10733.cpp new file mode 100644 index 0000000..074f2b1 --- /dev/null +++ b/10733.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + long long int n1, n2; + while (scanf("%lld", &n1) == 1) + { + if (n1 == 0LL) + { + break; + } + n1 += (n2 = n1 * (n1 - 1) / 2LL); + printf("%lld\n", n1 * (n1 + 1LL) / 2LL * (n1 + 2LL) / 3LL + (n2 - 2LL) * (n2 - 1LL) / 2LL * n2 / 3LL); + } + return 0; +} diff --git a/10734.cpp b/10734.cpp new file mode 100644 index 0000000..f79a80d --- /dev/null +++ b/10734.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +double s[100][3], a, b, c; +int num; +void calc(double x, double y, double z) +{ + double t; + if (x > y) + { + t = x; + x = y; + y = t; + } + if (y > z) + { + t = y; + y = z; + z = t; + } + if (x > y) + { + t = x; + x = y; + y = t; + } + for (int i = 0; i < num; i++) + { + double t1 = x / s[i][0], t2 = y / s[i][1], t3 = z / s[i][2]; + if (fabs(t1 - t2) < 1e-6 && fabs(t2 - t3) < 1e-6) + { + return; + } + } + s[num][0] = x; + s[num][1] = y; + s[num][2] = z; + num++; + double cosxz = (x * x + z * z - y * y) / (2 * x * z); + double mid = sqrt((x * x + (z * z) / 4 - x * z * cosxz)); + calc(x, mid, z / 2); + calc(y, mid, z / 2); +} +int main() +{ + int t; + scanf("%d", &t); + for (int cases = 1; cases <= t; cases++) + { + printf("Triangle %d: ", cases); + scanf("%lf %lf %lf", &a, &b, &c); + num = 0; + calc(a, b, c); + printf("%d\n", num); + } +} diff --git a/10735.cpp b/10735.cpp new file mode 100644 index 0000000..db3843e --- /dev/null +++ b/10735.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +/* UVa 10735: find euler tour in a mixed graph */ + +int war[128][128], deg[128], need[128], seen[128], n, m; +int ex[1024], ey[1024], ed[1024], em[1024]; +vector adj[128]; + +void tour(int x) +{ + while (adj[x].size() > 0) + { + int y = adj[x].back(); + adj[x].pop_back(); + tour(y); + } + printf(m++ ? " %d" : "%d", x); +} + +int aug(int x) +{ + if (seen[x]) + { + return 0; + } + seen[x] = 1; + for (int i = 0; i < adj[x].size(); i++) + { + int y = adj[x][i]; + if (em[y] == 0 || aug(em[y])) + { + em[y] = x; + return 1; + } + } + return 0; +} + +int solve() +{ + int i, j, k; + memset(war, 0, sizeof(war)); + memset(deg, 0, sizeof(deg)); + /* check connectedness */ + for (i = 0; i < m; i++) + { + war[ex[i]][ey[i]] = war[ey[i]][ex[i]] = 1; + deg[ex[i]]++; + deg[ey[i]]++; + } + for (k = 1; k <= n; k++) + for (war[k][k] = 1, i = 1; i <= n; i++) + if (war[i][k]) + for (j = 1; j <= n; j++) + { + war[i][j] |= war[k][j]; + } + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + if (war[i][j] == 0) + { + return 0; + } + /* underlying undirected graph must have an euler tour... */ + for (i = 1; i <= n; i++) + if ((deg[i] % 2) != 0) + { + return 0; + } + /* prepare matching */ + memset(em, 0, sizeof(em)); + for (i = 1; i <= n; i++) + { + need[i] = deg[i] / 2; + } + for (i = 1; i <= n; i++) + { + adj[i].clear(); + } + for (i = 0; i < m; i++) + if (!ed[i]) + { + adj[ex[i]].push_back(i); + adj[ey[i]].push_back(i); + } + for (i = 0; i < m; i++) + if (ed[i] && --need[em[i] = ey[i]] < 0) + { + return 0; + } + /* now find a perfect matching... */ + for (i = 1; i <= n; i++) + for (; need[i] > 0; need[i]--) + { + memset(seen, 0, sizeof(seen)); + if (!aug(i)) + { + return 0; + } + } + /* construct fully directed graph from the matching, and + find euler tour in it with a classical algorithm */ + /* edges' directions are reversed, so that tour() can + immediately print the tour's vertices */ + for (i = 1; i <= n; i++) + { + adj[i].clear(); + } + for (i = 0; i < m; i++) + if (ed[i] || ey[i] == em[i]) + { + adj[ey[i]].push_back(ex[i]); + } + else + { + adj[ex[i]].push_back(ey[i]); + } + m = 0; + tour(1); + printf("\n"); + return 1; +} + +int main() +{ + int i, t; + char d; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &m) == 2;) + { + for (i = 0; i < m; i++) + { + scanf("%d %d %c", &ex[i], &ey[i], &d); + ed[i] = (d == 'D' || d == 'd'); + } + if (!solve()) + { + printf("No euler circuit exist\n"); + } + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/10736.cpp b/10736.cpp new file mode 100644 index 0000000..8f8c55a --- /dev/null +++ b/10736.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +/* + * pi = 6 arcsin(1/2) + * + * arcsin(x) = x + 1/2 (x^3/3) + (1/2)(3/4)(x^5/5) + + * (1/2)(3/4)(5/6)(x^7/7) + ... + */ + +double term[1000001]; + +int main() +{ + term[0] = log10(3.0); + for (int n = 0; n < 1000000; n++) + { + double z = 2 * log10(2 * n + 1.) - log10(2 * n + 2.) - log10(2 * n + 3.) - log10(4.0); + term[n + 1] = term[n] + z; + } + int n; + while (scanf("%d", &n) == 1 && n > 0) + { + int left = 0, right = 1000000; + while (right - left > 1) + { + int m = (left + right) / 2; + if (term[m] > -n) + { + left = m; + } + else + { + right = m; + } + } + printf("%d\n", right); + } +} diff --git a/10737.cpp b/10737.cpp new file mode 100644 index 0000000..8cf745f --- /dev/null +++ b/10737.cpp @@ -0,0 +1,228 @@ +#include + +using namespace std; + +int M, P, inv[128]; +int mat[128][128], var[128], seq[128], C[128][128]; + +bool rowreduce() +{ + int row = 0, col = 0; + int i, j, k; + while (row < M && col < P) + { + for (i = row; i < M; i++) + if (mat[i][col] != 0) + { + break; + } + if (i == M) + { + col++; + continue; + } + if (i != row) + { + for (j = 0; j <= P; j++) + { + swap(mat[i][j], mat[row][j]); + } + } + k = inv[mat[row][col]]; + for (j = 0; j <= P; j++) + { + mat[row][j] = (mat[row][j] * k) % P; + } + for (i = 0; i < M; i++) + { + if (i == row) + { + continue; + } + k = (P - mat[i][col]) % P; + if (k == 0) + { + continue; + } + for (j = 0; j <= P; j++) + { + mat[i][j] = (mat[i][j] + k * mat[row][j]) % P; + } + } + row++; + col++; + } + int m_new = row; + for (; row < M; row++) + { + if (mat[row][P] != 0) + { + return false; + } + } + M = m_new; + return true; +} + +bool gauss() +{ +#if 0 + printf("Matrix:\n"); + for(int i = 0; i < M; i++) + { + for(int j = 0; j < P; j++) + { + printf("%d ", mat[i][j]); + } + printf("| %d\n", mat[i][P]); + } + printf(".\n"); +#endif + if (!rowreduce()) + { + return false; + } + vector freeVar(P, true); + for (int i = 0; i < M; i++) + { + for (int j = 0; j < P; j++) + { + if (mat[i][j] != 0) + { + freeVar[j] = false; + break; + } + } + } + // assign zeros to free variables + for (int i = 0; i < P; i++) + { + if (freeVar[i]) + { + mat[M][i] = 1; + mat[M][P] = 0; + M++; + } + } + assert(M == P); + assert(rowreduce()); + for (int i = 0; i < P; i++) + { + var[i] = mat[i][P]; + } + return true; +} + +bool solve(int N) +{ + if (N >= P) + { + for (int i = 0; i < P; i++) + { + if (seq[i] != -1 && seq[i] != 0) + { + return false; + } + seq[i] = 0; + } + return true; + } + assert(1 <= N && N <= P - 1); + int missing = 0; + for (int i = 0; i < P; i++) + if (seq[i] == -1) + { + missing++; + } + assert(missing <= N); + memset(C, 0, sizeof(C)); + for (int i = 0; i <= P; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + { + C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P; + } + } + memset(inv, 0, sizeof(inv)); + for (int i = 1; i < P; i++) + { + for (int j = 1; j < P; j++) + { + if ((i * j) % P == 1) + { + inv[i] = j; + } + } + assert(inv[i] != 0); + } + memset(mat, 0, sizeof(mat)); + memset(var, 0, sizeof(var)); + M = 0; + for (int i = 0; i < P; i++) + { + if (seq[i] == -1) + { + continue; + } + for (int j = 0; j <= N; j++) + { + mat[M][(i + N - j) % P] = (j % 2 == 0 ? C[N][j] : (P - C[N][j])) % P; + } + mat[M][P] = seq[i] % P; + M++; + } + if (!gauss()) + { + return false; + } + for (int i = 0; i < P; i++) + { + int z = 0; + for (int j = 0; j <= N; j++) + { + int coef = (j % 2 == 0 ? C[N][j] : (P - C[N][j])) % P; + z = ((z + coef * var[(i + N - j) % P]) % P + P) % P; + } + if (seq[i] != -1 && z != seq[i]) + { + return false; + } + seq[i] = z; + } + return true; +} + +int main() +{ + int N; + char s[50]; + while (scanf("%d %d", &P, &N) == 2 && P != 0) + { + assert(2 <= P && P <= 100); + for (int i = 0; i < P; i++) + { + scanf(" %s", s); + if (!isdigit(s[0])) + { + seq[i] = -1; + } + else + { + seq[i] = (atoi(s) % P + P) % P; + } + } + if (!solve(N)) + { + printf("Invalid message!\n"); + } + else + { + for (int i = 0; i < P; i++) + { + printf(i == 0 ? "%d" : " %d", seq[i]); + } + printf("\n"); + } + } +} diff --git a/10738.cpp b/10738.cpp new file mode 100644 index 0000000..32f478c --- /dev/null +++ b/10738.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define N 1000005 + +bool p[N]; +int m[N], M[N], prime[N / 10], factor[32], top; + +void sieve() +{ + memset(p, 1, sizeof(p)); + p[0] = p[1] = top = 0; + m[1] = 1; + for (int i = 2; i < 1001; i++) + { + if (p[i]) + { + m[i] = -1; + prime[top++] = i; + for (int j = i * i; j < N; j += i) + p[j] = 0; + } + } + for (int i = 4; i < N; i++) + { + if (p[i]) + { + m[i] = -1; + continue; + } + int t = i, k = 0, mk = 0; + for (int j = 0; prime[j] < t && j < top; j++) + { + while (t % prime[j] == 0) + { + factor[k++] = prime[j]; + t /= prime[j]; + } + } + if (t != 1) + factor[k++] = t; + for (int j = 1; j < k; j++) + { + if (factor[j] == factor[j - 1]) + { + mk = 1; + break; + } + } + if (mk) + continue; + if (k & 1) + m[i] = -1; + else + m[i] = 1; + } + for (int i = 1; i < N; i++) + M[i] = M[i - 1] + m[i]; +} + +int main() +{ + sieve(); + int n; + while (scanf("%d", &n), n) + printf("%8d%8d%8d\n", n, m[n], M[n]); + return 0; +} diff --git a/10739.cpp b/10739.cpp index d2e6af4..0973b03 100644 --- a/10739.cpp +++ b/10739.cpp @@ -1,71 +1,61 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef long long int64; -typedef unsigned long long uint64; +#define MAXN 1002 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +int Cost[MAXN][MAXN], len; +char Str[MAXN]; -int EditDistance(string A, string B){ - int i, j; - int N = A.length(); - int d[N+1][N+1]; - - FOI(i, 0, N){ - d[i][0] = 0; - d[0][i] = 0; +int recur(int r, int c) +{ + int x; + if (r >= c) + return 0; + if (Cost[r][c]) + return Cost[r][c]; + if (Str[r] == Str[c]) + Cost[r][c] = recur(r + 1, c - 1); + else + { + Cost[r][c] = recur(r, c - 1) + 1; + x = recur(r + 1, c) + 1; + if (x < Cost[r][c]) + Cost[r][c] = x; + x = recur(r + 1, c - 1) + 1; + if (x < Cost[r][c]) + Cost[r][c] = x; } + return Cost[r][c]; +} - FOI(i, 1, N) - FOI(j, 1, N){ - if( A[i-1] == B[j-1] ) - d[i][j] = d[i-1][j-1]; - else - d[i][j] = min( min( d[i-1][j], d[i][j-1] ), d[i-1][j-1] ) + 1; - } - return d[N][N]; +void Cal() +{ + len = strlen(Str) - 1; + printf("%d\n", recur(0, len)); } +void Free() +{ + int i, j; + for (i = 0; i <= len; i++) + for (j = 0; j <= len; j++) + Cost[i][j] = 0; +} -int main(){ - //freopen("Test.txt", "r", stdin); - //freopen("TestO.txt", "w", stdout); - int T, i, j, t; - cin >> T; - FOI(t, 1, T){ - string sA, sB; - cin >> sA; - sB = sA; reverse(sB.begin(), sB.end()); - //cout << sA << " " << sB << endl; - - int cost = ( EditDistance(sB, sA) + 1 )/ 2; - - cout << "Case " << t << ": " << cost << endl; - } - return 0; +int main() +{ + int k, c = 1; + char input[100]; + gets(input); + sscanf(input, "%d", &k); + while (k--) + { + gets(Str); + printf("Case %d: ", c++); + Cal(); + if (k) + Free(); + } + return 0; } diff --git a/10740.cpp b/10740.cpp new file mode 100644 index 0000000..152a1c9 --- /dev/null +++ b/10740.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define maxn 102 + +int Fre[maxn]; + +struct ss +{ + int cost; + int node; +}; +class comp +{ + int x; + +public: + bool operator()(const ss &a, const ss &b) + { + return a.cost > b.cost; + } +}; +priority_queue, comp> Q; +list Link[maxn]; +int N, M, X, Y, K; +int BFS() +{ + int v; + list::iterator i; + ss t, d; + t.node = X; + t.cost = 0; + Q.push(t); + while (!Q.empty()) + { + t = Q.top(); + Q.pop(); + Fre[t.node]++; + if (Fre[t.node] >= K) + { + if (Fre[t.node] == K && t.node == Y) + return t.cost; + if (Fre[t.node] > K) + continue; + } + for (i = Link[t.node].begin(); i != Link[t.node].end(); i++) + { + v = i->node; + d.cost = t.cost + i->cost; + d.node = v; + Q.push(d); + } + } + return -1; +} +void Cal() +{ + int d; + d = BFS(); + if (d == -1) + cout << "-1\n"; + else + cout << d << endl; +} +void Free() +{ + int i; + for (i = 1; i <= N; i++) + { + Link[i].clear(); + Fre[i] = 0; + } + while (!Q.empty()) + Q.pop(); +} + +int main() +{ + int i, u, v, c; + ss t; + while (cin >> N >> M) + { + if (!N && !M) + break; + cin >> X >> Y >> K; + for (i = 0; i < M; i++) + { + cin >> u >> v >> c; + t.node = v; + t.cost = c; + Link[u].push_back(t); + } + Cal(); + Free(); + } + return 0; +} diff --git a/10741.cpp b/10741.cpp new file mode 100644 index 0000000..24d7f49 --- /dev/null +++ b/10741.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int cube[25][25][25], n; +int main() +{ + int caseno = 0; + while (scanf("%d", &n), n) + { + if (caseno++) + { + puts(""); + } + memset(cube, 0, sizeof(cube)); + int n2 = n * n, n3 = n2 * n; + int x = n / 2, y = n / 2, z = n - 1, nx, ny, nz; + for (int i = 0; i < n2; i++) + { + for (int j = 1; j <= n; j++) + { + cube[z][y][x] = i * n + j; + z = (z + 1) % n; + x = (x + 1) % n; + } + ny = (y + n - 1) % n; + nx = (x + n - 1) % n; + if (!cube[z][ny][nx]) + { + y = ny; + x = nx; + } + else + { + z = (z + n - 2) % n; + x = (x + n - 1) % n; + } + } + for (int i = 0; i < n; i++) + { + if (i) + { + puts(""); + } + for (int j = 0; j < n; j++) + { + for (int k = 0; k < n; k++) + { + if (k) + { + putchar(' '); + } + printf("%5d", cube[i][j][k]); + } + printf("\n"); + } + } + } +} diff --git a/10742.cpp b/10742.cpp new file mode 100644 index 0000000..7a6356b --- /dev/null +++ b/10742.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +bool p[1000010]; +int prime[80000], numOfPrime = 0; +int amount, kase = 1; + +void sieve() +{ + int i, j; + memset(p, 1, sizeof(p)); + p[0] = p[1] = false; + for (i = 2; i <= 1000;) + { + for (j = i + i; j <= 1000000; j += i) + { + p[j] = false; + } + for (i++; !p[i]; i++) + ; + } + for (i = 0; i < 1000000; i++) + if (p[i]) + { + prime[numOfPrime++] = i; + } +} + +int bin_search(int key) +{ + int low, mid, high; + low = 0; + high = numOfPrime - 1; + while (low <= high) + { + mid = (low + high) / 2; + if (key == prime[mid]) + { + return mid; + } + else if (key < prime[mid]) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } + return -1; +} + +void solve() +{ + int index, cnt = 0; + for (index = 0; prime[index] <= amount / 2; index++) + { + int tail; + for (tail = amount - prime[index];; tail--) + { + int th; + th = bin_search(tail); + if (th >= 0) + { + cnt += th - index; + break; + } + } + } + cout << cnt << endl; +} + +int main() +{ + sieve(); + while (cin >> amount && amount) + { + cout << "Case " << kase++ << ": "; + solve(); + } + return 0; +} diff --git a/10743.cpp b/10743.cpp new file mode 100644 index 0000000..a5a9577 --- /dev/null +++ b/10743.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int d, n, m; +int base[3][3]; +int sol[10] = {0, 1, 2, 6, 19, 61, 196, 629, 2017, 6466}; +void run(int n, int m, int matrix[3][3]) +{ + if (n == 1) + { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + { + matrix[i][j] = base[i][j]; + } + } + else + { + int temp[3][3]; + if (n & 1) + { + run(n - 1, m, temp); + memset(matrix, 0, sizeof(temp)); + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + for (int k = 0; k < 3; k++) + { + matrix[i][j] = (matrix[i][j] + (temp[i][k] * base[k][j])) % m; + } + } + else + { + run(n / 2, m, temp); + memset(matrix, 0, sizeof(temp)); + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + for (int k = 0; k < 3; k++) + { + matrix[i][j] = (matrix[i][j] + (temp[i][k] * temp[k][j])) % m; + } + } + } +} +int main() +{ + base[0][0] = 5; + base[0][1] = -7; + base[0][2] = 4; + base[1][0] = 1; + base[1][1] = 0; + base[1][2] = 0; + base[2][0] = 0; + base[2][1] = 1; + base[2][2] = 0; + int t, sum; + m = 10000; + scanf("%d", &t); + for (int caseno = 1; caseno <= t; caseno++) + { + scanf("%d", &n); + int matrix[3][3]; + sum = 0; + if (n <= 9) + { + sum = sol[n]; + printf("Case %d: %d\n", caseno, sum); + } + else + { + run(n - 4, 10000, matrix); + for (int i = 0; i < 3; i++) + { + sum = (sum + matrix[0][i] * (sol[4 - i] % m)) % m; + } + while (sum < 0) + { + sum += m; + } + printf("Case %d: %04d\n", caseno, sum); + } + } +} diff --git a/10744.cpp b/10744.cpp new file mode 100644 index 0000000..9ebc36e --- /dev/null +++ b/10744.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const double eps = 1e-10; + +typedef complex P; + +class info +{ +public: + double d; + P p; + bool operator<(const info &a) const + { + return d < a.d; + } +}; + +double cross(P a, P b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +double distance_l_p(P a, P b, P c) +{ + double tmp = cross(b - a, c - a); + if (tmp < 0) + { + tmp *= -1; + } + P d = b - a; + return tmp / sqrt(d.real() * d.real() + d.imag() * d.imag()); +} + +void calc_dist(info *in, int n, P a1, P a2, info *cpy) +{ + rep(i, n) + { + cpy[i] = in[i]; + double tmp = distance_l_p(a1, a2, cpy[i].p); + cpy[i].d = tmp; + if (cross(a2 - a1, cpy[i].p - a1) < 0) + { + cpy[i].d *= -1; + } + } + return; +} + +void solve(info *in, int n, int q) +{ + info cpy[n]; + int tc = 1; + rep(i, q) + { + double re,im; + scanf("%lf%lf", &re,&im); + P a1 (re,im); + scanf("%lf%lf", &re,&im); + P a2 (re,im); + P rotate = a2 - a1; + rotate = P(rotate.imag()*-1, rotate.real()); + rotate /= sqrt(rotate.real() * rotate.real() + rotate.imag() * rotate.imag()); + calc_dist(in, n, a1, a2, cpy); + sort(cpy, cpy + n); + double inter[n]; + rep(i, n - 1) + { + inter[i + 1] = cpy[i + 1].d - cpy[i].d; + } + int r = 0, l = n; + rotate *= cpy[0].d; + a1 += rotate; + a2 += rotate; + double ans = 0, tmp = 0; + rep(i, n) + { + ans += cpy[i].d - cpy[0].d; + } + REP(i, 1, n) + { + l--; + r++; + ans = min(ans, ans + r * inter[i] - l * inter[i]); + } + printf("Case %d: %0.lf\n", tc++, ans); + } + return; +} + +int main() +{ + int n, q; + while (scanf("%d%d", &n, &q) != -1) + { + info in[n]; + rep(i, n) + { + double re,im; + scanf("%lf%lf", &re,&im); + in[i].p = P(re,im); + } + solve(in, n, q); + } + return 0; +} diff --git a/10745.cpp b/10745.cpp new file mode 100644 index 0000000..a4bffd7 --- /dev/null +++ b/10745.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +/* +10745 +Dominant String +*/ +#define MAXN 15002 +struct ss +{ + int len; + int fre[27]; + char dominant; +} word[MAXN]; +char W[MAXN][12]; +char Rec[MAXN][12]; +int total, rec; +int com(const void *a, const void *b) +{ + return strcmp((char *)a, (char *)b); +} +int Dominant(int p, int q) +{ + int i, j; + for (i = 0; i < W[q][i]; i++) + { + j = W[q][i] - 'a'; + if (word[p].fre[j] < word[q].fre[j]) + { + return 0; + } + } + return 1; +} +void Cal() +{ + int i, j, k, x = 0; + qsort(W, total, sizeof(W[0]), com); + for (i = 0; i < total; i++) + { + word[i].len = strlen(W[i]); + for (j = 0; W[i][j]; j++) + { + k = W[i][j] - 'a'; + word[i].fre[k]++; + } + } + for (i = 0; i + 1 < total; i++) + { + if (word[i].dominant == 1) + { + continue; + } + for (j = i + 1; j < total; j++) + { + if (word[j].dominant == 1) + { + continue; + } + if (word[i].len > word[j].len) + { + if (Dominant(i, j)) + { + word[j].dominant = 1; + } + } + else if (word[i].len < word[j].len) + { + if (Dominant(j, i)) + { + word[i].dominant = 1; + if (word[i].len == word[j].len) + { + word[j].dominant = 1; + } + } + } + } + } + for (i = 0; i < total; i++) + if (word[i].dominant == 0) + { + printf("%s\n", W[i]); + } +} +int main() +{ + total = 0; + while (gets(W[total])) + { + total++; + } + Cal(); + return 0; +} diff --git a/10746.cpp b/10746.cpp new file mode 100644 index 0000000..f5821d7 --- /dev/null +++ b/10746.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +#define maxn 20 +#define oo 1000000000 + +double c[maxn][maxn], fx[maxn], fy[maxn]; +int mx[maxn], my[maxn], trace[maxn]; +int n, m, k, start, finish; + +void input() +{ + k = m;// as it is given n <= m otherwise, k = max(n, m) + for (int i = 0; i < k; i++) + for (int j = 0; j < k; j++) + c[i][j] = oo; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + cin >> c[i][j]; +} + +void init() +{ + memset(mx, 0xFF, sizeof(mx)); + memset(my, 0xFF, sizeof(my)); + memset(fx, 0, sizeof(fx)); + memset(fy, 0, sizeof(fy)); +} + +int getC(int i, int j) +{ + return c[i][j] - fx[i] - fy[j]; +} + +void findAugmentingPath() +{ + memset(trace, 0xFF, sizeof(trace)); + queue q; + q.push(start); + while (!q.empty()) + { + int i = q.front(); + q.pop(); + for (int j = 0; j < k; j++) + if (trace[j] == -1 && getC(i, j) == 0) + { + trace[j] = i; + if (my[j] == -1) + { + finish = j; + return; + } + q.push(my[j]); + } + } +} + +void subX_AddY() +{ + bool visitedX[maxn], visitedY[maxn]; + memset(visitedX, false, sizeof(visitedX)); + memset(visitedY, false, sizeof(visitedY)); + visitedX[start] = true; + for (int j = 0; j < k; j++) + if (trace[j] != -1) + { + visitedX[my[j]] = true; + visitedY[j] = true; + } + // Tính Delta := min(GetC(i, j)|i ∈ VisitedX và j ∉ VisitedY) + int delta = oo; + for (int i = 0; i < k; i++) + if (visitedX[i]) + for (int j = 0; j < k; j++) + if ((!visitedY[j]) && (delta > getC(i, j))) + { + delta = getC(i, j); + } + for (int t = 0; t < k; t++) + { + if (visitedX[t]) + fx[t] += delta; + if (visitedY[t]) + fy[t] -= delta; + } +} + +void enlarge() +{ + do + { + int i = trace[finish]; + int next = mx[i]; + mx[i] = finish; + my[finish] = i; + finish = next; + } while (finish != -1); +} + +void process() +{ + init(); + for (int i = 0; i < k; i++) + { + start = i; + finish = -1; + do + { + findAugmentingPath(); + if (finish == -1) + subX_AddY(); + } while (finish == -1); + enlarge(); + } +} + +void output() +{ + double result = 0; + for (int i = 0; i < n; i++) + { + int j = mx[i]; + if (c[i][j] < oo) + result += c[i][j]; + } + printf("%.2f\n", result / n); +} + +int main() +{ + while (cin >> n >> m) + { + if ((n == 0) && (m == 0)) + { + break; + } + input(); + process(); + output(); + } + return 0; +} diff --git a/10747.cpp b/10747.cpp new file mode 100644 index 0000000..f35fe2b --- /dev/null +++ b/10747.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +int a[10000], b[10000], n, k; +bool pos, zero; +int swap(int &a, int &b) +{ + int t = a; + a = b; + b = t; +} +int abs(int v) +{ + return (v > 0) ? v : -v; +} +int compare(const void *a, const void *b) +{ + int c = *(int *)a, d = *(int *)b; + int cc = abs(c), dd = abs(d); + if (cc != dd) + { + return dd - cc; + } + else if (c == d) + { + return 0; + } + else if (c > d) + { + return -1; + } + else + { + return 1; + } +} +int main() +{ + while (scanf("%d %d", &n, &k) == 2) + { + if (!n && !k) + { + break; + } + pos = zero = false; + for (int i = 0; i < n; i++) + { + scanf("%d", a + i); + if (a[i] > 0) + { + pos = true; + } + } + if (n == k) + { + int sum = 0; + for (int i = 0; i < n; i++) + { + sum += a[i]; + } + printf("%d\n", sum); + continue; + } + qsort(a, n, sizeof(int), compare); + for (int i = 0; i < k; i++) + if (a[i] == 0) + { + zero = true; + } + if (zero) + { + int sum = 0; + for (int i = 0; i < k; i++) + { + if (a[i] >= 0) + { + sum += a[i]; + } + else + { + int j; + for (j = k; j < n; j++) + if (a[j] == 0) + { + swap(a[i], a[j]); + break; + } + if (j == n) + { + sum += a[i]; + } + } + } + printf("%d\n", sum); + continue; + } + if (!pos) + { + int sum = 0; + if (k & 1) + for (int i = n - 1, j = 0; j < k; i--, j++) + { + sum += a[i]; + } + else + for (int i = 0; i < k; i++) + { + sum += a[i]; + } + printf("%d\n", sum); + continue; + } + int neg = 0, sum = 0; + for (int i = 0; i < k; i++) + if (a[i] < 0) + { + neg++; + } + for (int i = 0; i < k; i++) + { + sum += a[i]; + } + if (neg & 1) + { + for (int i = 0; i < n; i++) + { + b[i] = a[i]; + } + int p, sum1, sum2, temp; + int a1, b1, a2, b2; + char flag1, flag2; + for (p = k; p < n && a[p] < 0; p++) + ; + if (p != n) + { + if (a[p] == 0) + { + sum1 = 0; + for (int i = 0; i < k; i++) + { + if (b[i] < 0) + { + for (int j = k; j < n; j++) + if (b[j] == 0) + { + swap(b[i], b[j]); + break; + } + } + sum1 += b[i]; + } + flag1 = 0; + } + else + { + b1 = a[p]; + for (p = k - 1; a[p] >= 0; p--) + ; + a1 = a[p]; + sum1 = sum - a1 + b1; + flag1 = 1; + } + } + else + { + flag1 = 2; + } + for (int i = 0; i < n; i++) + { + b[i] = a[i]; + } + for (p = k - 1; p >= 0 && a[p] < 0; p--) + ; + if (p < 0) + { + flag2 = 0; + } + else + { + a2 = a[p]; + for (p = k; a[p] >= 0 && p < n; p++) + ; + if (p >= n) + { + flag2 = 0; + } + else + { + b2 = a[p]; + sum2 = sum - a2 + b2; + flag2 = 1; + } + } + if (flag1 == 2 && flag2 == 0) + { + temp = sum; + } + else if (flag1 == 2 && flag2 == 1) + { + temp = sum2; + } + else if (flag1 == 0 && flag2 == 0) + { + temp = sum1; + } + else if (flag1 == 0 && flag2 == 1) + { + temp = sum2; + } + else if (flag1 == 1 && flag2 == 0) + { + temp = sum1; + } + else + { + int t = abs(b1) * abs(a2) - abs(a1) * abs(b2); + if (t < 0) + { + temp = sum2; + } + else if (temp > 0) + { + temp = sum1; + } + else + { + temp = (sum1 > sum2) ? sum1 : sum2; + } + } + sum = temp; + } + end: + printf("%d\n", sum); + } +} diff --git a/10748.cpp b/10748.cpp new file mode 100644 index 0000000..389e92c --- /dev/null +++ b/10748.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define HSIZE 1048576 +#define HDIV 533879 + +int htab[HDIV], hx[HSIZE], hy[HSIZE], hnext[HSIZE], cnt, prev_; + +void add(int x, int y) +{ + int i; + unsigned h; + h = x * 36457 + y; + h %= HDIV; + for (i = htab[h]; i != 0; i = hnext[i]) + { + if (hx[i] == x && hy[i] == y) + { + return; + } + } + i = ++cnt; + hx[i] = x; + hy[i] = y; + hnext[i] = htab[h]; + htab[h] = i; +} + +int main() +{ + static int x[1024], y[1024], k[1024], n, t; + int i, m; + while (scanf("%d", &n) == 1 && n > 0) + { + for (i = 0; i < n; i++) + { + scanf("%d%d%d", &x[i], &y[i], &k[i]); + } + for (i = 0, m = k[0]; i < n; i++) + { + if (k[i] > m) + { + m = k[i]; + } + } + memset(htab, 0, sizeof(htab)); + for (cnt = prev_ = 0; m >= 0; m--) + { + t = cnt; + for (i = cnt; i > prev_; i--) + { + add(hx[i] + 1, hy[i] + 2); + add(hx[i] + 1, hy[i] - 2); + add(hx[i] - 1, hy[i] + 2); + add(hx[i] - 1, hy[i] - 2); + add(hx[i] + 2, hy[i] + 1); + add(hx[i] + 2, hy[i] - 1); + add(hx[i] - 2, hy[i] + 1); + add(hx[i] - 2, hy[i] - 1); + } + prev_ = t; + for (i = 0; i < n; i++) + if (k[i] == m) + { + add(x[i], y[i]); + } + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/1075.cpp b/1075.cpp new file mode 100644 index 0000000..1a0884d --- /dev/null +++ b/1075.cpp @@ -0,0 +1,243 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double PI = acos(-1); +int sgn(double x) +{ + return (x < -eps) ? -1 : (x > eps); +} + +//2D PT +struct PT +{ + double x, y; + PT() {} + PT(double x, double y) : x(x), y(y) {} + void rd() + { + scanf("%lf %lf", &x, &y); + } + PT operator-(const PT o) + { + return PT(x - o.x, y - o.y); + } + PT operator+(const PT o) + { + return PT(x + o.x, y + o.y); + } + PT operator*(double s) + { + return PT(x * s, y * s); + } + PT operator/(double s) + { + return PT(x / s, y / s); + } + bool operator<(const PT &o) const + { + return x < o.x - eps || (x < o.x + eps && y < o.y - eps); + } + bool operator==(const PT &o) const + { + return !sgn(y - o.y) && !sgn(x - o.x); + } + bool operator!=(const PT &o) const + { + return sgn(y - o.y) || sgn(x - o.x); + } +}; + +double cpr(PT a, PT b) +{ + return a.x * b.y - a.y * b.x; +} +double dpr(PT a, PT b) +{ + return a.x * b.x + a.y * b.y; +} + +double cpr(PT a, PT b, PT c) +{ + return cpr(b - a, c - a); +} +double dpr(PT a, PT b, PT c) +{ + return dpr(b - a, c - a); +} + +double vlen(PT a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} +double dist(PT a, PT b) +{ + return vlen(a - b); +} + +struct LE +{ + PT a, b; + LE() {} + LE(PT a, PT b) : a(a), b(b) {} +}; + +//判两点ab与直线cd相对ä½Âç½®, 点在直线上0, åŒ侧1, 异侧-1 +int plside(PT a, PT b, PT c, PT d) +{ + return sgn(cpr(c, a, d) * cpr(c, b, d)); +} + +//判两线段ab cdéž严格相交 +bool ints_in(PT a, PT b, PT c, PT d) +{ + int d1 = plside(a, b, c, d), d2 = plside(c, d, a, b); + if (d1 == 1 || d2 == 1) + { + return 0; + } + if (d1 == -1 || d2 == -1) + { + return 1; + } + return dpr(c, a, b) < eps || dpr(d, a, b) < eps || dpr(a, c, d) < eps || dpr(b, c, d) < eps; +} + +int lineints(LE l1, LE l2) +{ + return ints_in(l1.a, l1.b, l2.a, l2.b); +} + +struct EVENT +{ + double x; + int a, b, c; + EVENT() {} + EVENT(double x, int a, int b, int c) : x(x), a(a), b(b), c(c) {} + bool operator<(const EVENT &b) const + { + return sgn(x - b.x) ? x < b.x : c < b.c; + } +}; + +const int NN = 300005; +LE le[NN]; +EVENT ev[NN]; +int ty[NN], dep[NN]; + +double nowx; + +struct cmp +{ + double gety(LE ll) + { + double dx = ll.b.x - ll.a.x; + double dy = ll.b.y - ll.a.y; + return sgn(dx) ? (nowx - ll.a.x) / dx * dy + ll.a.y : ll.a.y; + } + bool operator()(int a, int b) + { + double y1 = gety(le[a]); + double y2 = gety(le[b]); + return sgn(y1 - y2) ? y1 < y2 : ty[a] < ty[b]; + } +}; + +int work(int n) +{ + set ss; + set::iterator it; + int ans = 0; + for (int i = 0; i < n; ++i) + { + nowx = ev[i].x; + int a = ev[i].a, b = ev[i].b; + if (ev[i].c == 0) + { + it = ss.lower_bound(a); + dep[a] = dep[b] = (it == ss.end() ? 1 : dep[*it] + ty[*it]); + ans = max(ans, dep[a]); + if (it != ss.end() && (lineints(le[a], le[*it]) || lineints(le[b], le[*it]))) + { + return -1; + } + if (it != ss.begin() && (--it, lineints(le[a], le[*it]) || lineints(le[b], le[*it]))) + { + return -1; + } + ss.insert(a); + ss.insert(b); + } + if (ev[i].c == 1) + { + dep[b] = dep[a]; + ss.erase(a); + it = ss.lower_bound(b); + if (it != ss.end() && (b / 3 != *it / 3 && lineints(le[b], le[*it]))) + { + return -1; + } + if (it != ss.begin() && (--it, b / 3 != *it / 3 && lineints(le[b], le[*it]))) + { + return -1; + } + ss.insert(b); + } + if (ev[i].c == 2) + { + ss.erase(a); + ss.erase(b); + it = ss.lower_bound(a); + if (it != ss.end() && it != ss.begin()) + { + int x = *it; + int y = *--it; + if (x / 3 != y / 3 && lineints(le[x], le[y])) + { + return -1; + } + } + } + } + return ans; +} + +int main() +{ + //freopen("1.txt", "r", stdin); + int n, cas = 0; + while (cin >> n, ~n) + { + n *= 3; + for (int i = 0; i < n; i += 3) + { + PT p[3]; + for (int j = 0; j < 3; j++) + { + p[j].rd(); + } + sort(p, p + 3); + le[i + 0] = LE(p[0], p[1]); + le[i + 1] = LE(p[0], p[2]); + le[i + 2] = LE(p[1], p[2]); + ty[i + 0] = ty[i + 2] = (cpr(p[0], p[1], p[2]) < 0); + ty[i + 1] = 1 ^ ty[i]; + ev[i + 0] = EVENT(p[0].x, i + 0, i + 1, 0); + ev[i + 1] = EVENT(p[1].x, i + 0, i + 2, 1); + ev[i + 2] = EVENT(p[2].x, i + 1, i + 2, 2); + } + sort(ev, ev + n); + int ans = work(n); + printf("Case %d: ", ++cas); + if (ans > -1) + { + printf("%d shades\n", ans + 1); + } + else + { + puts("ERROR"); + } + } + return 0; +} diff --git a/10750.cpp b/10750.cpp new file mode 100644 index 0000000..ab7c167 --- /dev/null +++ b/10750.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +template +inline void swap(tx &a, tx &b) +{ + tx c(a); + a = b; + b = c; +} + +template +inline int cmp(tx const &a, tx const &b) +{ + return a < b ? -1 : a == b ? 0 : 1; +} + +struct point +{ + int16_t x, y; +}; + +inline float dist(point const &a, point const &b) +{ + return hypotf(a.x - (float)b.x, a.y - (float)b.y); +} + +inline int compare_y(void const *a, void const *b) +{ + point const &p = *((point *)a); + point const &q = *((point *)b); + return (cmp(p.y, q.y) << 1) + cmp(p.x, q.x); +} + +inline int compare_x(void const *a, void const *b) +{ + point const &p = *((point *)a); + point const &q = *((point *)b); + return (cmp(p.x, q.x) << 1) + cmp(p.y, q.y); +} + +float closest_pair_distance(point const *const &PX, + ptrdiff_t const &x0, ptrdiff_t const &x1, + point *const &P, ptrdiff_t const &len, point &best) +{ + static point L[16384]; + static point R[16384]; + float ret = std::numeric_limits::infinity(); + if (len > 2) + { + ptrdiff_t si = (x0 + x1 - 1) >> 1; + int16_t sx = PX[si].x; + int16_t sy = PX[si].y; + float dr, dl; + ptrdiff_t l = 0, r = 0; + ptrdiff_t i = 0, j = 0, k = 0; + point lp, rp; + for (ptrdiff_t i = 0; i < len; ++i) + { + if (sx < P[i].x || (sx == P[i].x && sy < P[i].y)) + { + R[r++] = P[i]; + } + else + { + L[l++] = P[i]; + } + } + memcpy(P, L, l * sizeof(point)); + memcpy(P + l, R, r * sizeof(point)); + dl = closest_pair_distance(PX, x0, x0 + l, P, l, lp); + dr = closest_pair_distance(PX, x0 + l, x1, P + l, r, rp); + if (dl < dr) + { + best = lp; + ret = dl; + } + else + { + best = rp; + ret = dr; + } + memcpy(L, P, l * sizeof(point)); + memcpy(R, P + l, r * sizeof(point)); + while (i < l && j < r) + { + if (i < l && (L[i].y < R[j].y || (L[i].y == R[j].y && L[i].x < R[j].x))) + { + P[k++] = L[i++]; + } + else + { + P[k++] = R[j++]; + } + } + while (i < l) + { + P[k++] = L[i++]; + } + while (j < r) + { + P[k++] = R[j++]; + } + for (ptrdiff_t i = 0; i < len; ++i) + { + if (fabs(P[i].x - sx) < ret) + { + ptrdiff_t j = i + 1; + for (int k = 0; k < 7 && j < len; ++j) + { + if (fabs(P[j].x - sx) < ret) + { + float t = dist(P[i], P[j]); + if (t < ret) + { + ret = t; + best = P[i]; + } + ++k; + } + } + } + } + } + else if (len == 2) + { + ret = dist(P[0], P[1]); + best = P[0]; + } + return ret; +} + +void test_case() +{ + static point P[16384]; + static point PX[16384]; + float result = std::numeric_limits::infinity(); + point best; + uint16_t N; + scanf("%hu", (short unsigned int *)&N); + for (uint16_t i = 0; i < N; ++i) + { + scanf("%hd %hd", (short int *)&(P[i].x), (short int *)&(P[i].y)); + } + memcpy(PX, P, N * sizeof(point)); + qsort(P, N, sizeof(point), compare_y); + qsort(PX, N, sizeof(point), compare_x); + result = closest_pair_distance(PX, 0, N, P, N, best); + printf("%.3f %.3f\n\n", (float)best.x, (float)best.y); +} + +int main() +{ + uint16_t T; + scanf("%hu", (short unsigned int *)&T); + while (T--) + { + test_case(); + } + return EXIT_SUCCESS; +} diff --git a/10751.cpp b/10751.cpp new file mode 100644 index 0000000..5937328 --- /dev/null +++ b/10751.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + double sqrtwo = sqrt(2.0); + double pathLength; + int times, n; + scanf("%d", ×); + for (int i = 0; i < times; i++) + { + scanf("%d", &n); + pathLength = (n - 1) * 4 + ((n - 2 > 0) ? ((n - 2) * (n - 2) * sqrtwo) : (0)); + printf("%.4f\n", pathLength); + if (i != times - 1) + { + printf("\n"); + } + } + return 0; +} diff --git a/10754.cpp b/10754.cpp new file mode 100644 index 0000000..579fd75 --- /dev/null +++ b/10754.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +typedef long long int INT64; +INT64 c[25], a[24]; +INT64 n, mod, k; +INT64 bm[26][26]; +INT64 matrix[26][26]; +void init(int dim) +{ + for (int i = 0; i <= dim; i++) + for (int j = 0; j <= dim; j++) + { + bm[i][j] = 0; + } + for (int i = 0; i <= dim; i++) + { + bm[0][i] = c[i]; + } + for (int i = 1; i < dim; i++) + { + bm[i][i - 1] = 1; + } + bm[dim][dim] = 1; +} +void Perform(int value, int dim) +{ + INT64 tmp[26][26], tmp1[26][26], tmp2[26][26]; + INT64(*src) + [26] = tmp, + (*dst)[26] = tmp1, (*temp)[26] = tmp2, (*backup)[26]; + int bits; + for (bits = 31; bits >= 0; bits--) + if (value & (1 << bits)) + { + break; + } + for (int i = 0; i <= dim; i++) + for (int j = 0; j <= dim; j++) + { + src[i][j] = bm[i][j]; + } + for (bits--; bits >= 0; bits--) + { + for (int i = 0; i <= dim; i++) + for (int j = 0; j <= dim; j++) + { + INT64 sum = 0; + for (int k = 0; k <= dim; k++) + { + sum = (sum + (src[i][k] * src[k][j]) % mod) % mod; + } + dst[i][j] = sum; + } + if (value & (1 << bits)) + { + for (int i = 0; i <= dim; i++) + for (int j = 0; j <= dim; j++) + { + INT64 sum = 0; + for (int k = 0; k <= dim; k++) + { + sum = (sum + (dst[i][k] * bm[k][j]) % mod) % mod; + } + temp[i][j] = sum; + } + backup = src; + src = temp; + temp = backup; + } + else + { + backup = src; + src = dst; + dst = backup; + } + } + memcpy(matrix, src, sizeof(matrix)); +} +int main() +{ + int t, i; + scanf("%d", &t); + for (i = 0; i < t; i++) + { + if (i) + { + puts(""); + } + //INT64 matrix[26][26]; + scanf("%lld %lld %lld", &k, &mod, &n); + for (int i = 0; i <= 25; i++) + { + c[i] = 0; + } + for (int i = 0; i < k + 1; i++) + { + scanf("%lld", c + i); + c[i] %= mod; + if (c[i] < 0) + { + c[i] += mod; + } + } + for (int i = 0; i <= 23; i++) + { + a[i] = 0; + } + for (int i = 0; i <= k - 1; i++) + { + scanf("%lld", a + i); + a[i] %= mod; + if (a[i] < 0) + { + a[i] += mod; + } + } + INT64 ans; + if (n < k) + { + ans = a[n]; + } + else if (k == 0) + { + ans = c[0]; + } + else + { + init((int)k); + Perform(n - k + 1, k); + ans = 0; + for (int i = 0; i <= k - 1; i++) + { + ans = (ans + (matrix[0][i] * a[k - 1 - i]) % mod) % mod; + } + ans = (ans + matrix[0][k]) % mod; + } + printf("%lld\n", ans); + } +} diff --git a/10755.cpp b/10755.cpp new file mode 100644 index 0000000..dbcdd88 --- /dev/null +++ b/10755.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +int main() +{ + int t, first = 1, a, b, c, i, j, k, p, q; + long long int max, unit, sum, tmp, X[25][25][25] = {0}; + scanf("%d", &t); + while (t--) + { + scanf("%d%d%d", &a, &b, &c); + tmp = -(1LL << 33); + for (i = 1; i <= a; i++) + { + for (j = 1; j <= b; j++) + { + for (k = 1; k <= c; k++) + { + scanf("%lld", &X[i][j][k]); + if (X[i][j][k] > tmp) + { + tmp = X[i][j][k]; + } + X[i][j][k] += X[i][j][k - 1]; + } + for (k = 1; k <= c; k++) + { + X[i][j][k] += X[i][j - 1][k]; + } + } + } + max = 0; + for (p = 0; p < b; p++) + { + for (j = p + 1; j <= b; j++) + { + for (q = 0; q < c; q++) + { + for (k = q + 1; k <= c; k++) + { + for (i = 1, sum = 0; i <= a; i++) + { + unit = X[i][j][k] - X[i][p][k] - X[i][j][q] + X[i][p][q]; + if (sum < 0) + { + sum = unit; + } + else + { + sum += unit; + } + if (sum > max) + { + max = sum; + } + } + } + } + } + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + if (max > 0) + { + printf("%lld\n", max); + } + else + { + printf("%lld\n", tmp); + } + } + return 0; +} diff --git a/10757.cpp b/10757.cpp new file mode 100644 index 0000000..495ba97 --- /dev/null +++ b/10757.cpp @@ -0,0 +1,711 @@ +#include + +using namespace std; + +static int GE = 1001, LE = 1002, NE = 1003; +static int tok, tok_n; +static char tok_s[500000]; + +void assert1(bool x) +{ + if (x == false) + { + for (;;) + ; + } +} + +int strcasecmp(const char *s, const char *t) +{ + while (*s != 0 && *t != 0) + { + if (toupper(*s) != toupper(*t)) + { + return toupper(*s) < toupper(*t) ? -1 : 1; + } + s++; + t++; + } + if (*s == 0 && *t == 0) + { + return 0; + } + return toupper(*s) < toupper(*t) ? -1 : 1; +} + +int next() +{ + for (;;) + { + int c = getchar(); + if (c == EOF) + { + return tok = EOF; + } + if (isspace(c)) + { + continue; + } + if (isalpha(c)) + { + int i; + for (i = 0; c != EOF && isalnum(c);) + { + tok_s[i++] = c; + c = getchar(); + } + tok_s[i] = 0; + if (c != EOF) + { + ungetc(c, stdin); + } + return tok = 'a'; + } + if (c == '+' || c == '-' || isdigit(c)) + { + int i = 0; + if (c == '+' || c == '-') + { + tok_s[i++] = c; + c = getchar(); + } + assert(isdigit(c)); + while (c != EOF && isdigit(c)) + { + tok_s[i++] = c; + c = getchar(); + } + if (c != EOF) + { + ungetc(c, stdin); + } + tok_s[i] = 0; + tok_n = atoi(tok_s); + return tok = 'n'; + } + if (c == '\"') + { + int i = 0; + c = getchar(); + while (c != '\"') + { + assert(c != '\n'); + if (c == '\\') + { + c = getchar(); + assert(c == '\\' || c == '\"'); + } + tok_s[i++] = c; + c = getchar(); + assert(c != EOF); + } + tok_s[i] = 0; + return tok = 's'; + } + if (strchr("*,()=", c) != NULL) + { + return tok = c; + } + if (c == '<' || c == '>') + { + int c1 = getchar(); + if (c1 == '=') + { + return tok = (c == '<' ? LE : GE); + } + if (c == '<' && c1 == '>') + { + return tok = NE; + } + if (c1 != EOF) + { + ungetc(c1, stdin); + } + return tok = c; + } + assert(false); + } +} + +int nextint() +{ + assert(tok == 'n'); + int n = tok_n; + next(); + return n; +} + +void skip(const char *kw) +{ + assert(tok == 'a' && strcasecmp(tok_s, kw) == 0); + next(); +} + +int compare(const string &s1, const string &s2, int type) +{ + if (type == 'I') + { + int a = atoi(s1.c_str()); + int b = atoi(s2.c_str()); + if (a == b) + { + return 0; + } + return a < b ? -1 : 1; + } + else + { + return s1.compare(s2); + } +} + +struct ColumnDesc +{ + string name; + int type; +}; + +struct Table +{ + string name; + int nrows, ncols; + vector col; + vector> data; + + int columnByName(const char *s) const + { + for (int i = 0; i < ncols; i++) + if (strcasecmp(col[i].name.c_str(), s) == 0) + { + return i; + } + assert(false); + } +}; + +class ByColumn +{ + const Table *t; + int c; + +public: + ByColumn(const Table *T, int C) : t(T), c(C) {} + + bool operator()(int i1, int i2) const + { + int res = compare(t->data[i1][c], t->data[i2][c], t->col[c].type); + if (res == 0) + { + return i1 < i2; + } + return res < 0; + } +}; + +struct OrderBy +{ + vector col, dir; + Table *tab; + + bool operator()(int r1, int r2) const + { + for (int i = 0; i < (int)col.size(); i++) + { + int c = col[i]; + int res = dir[i] * compare(tab->data[r1][c], tab->data[r2][c], tab->col[c].type); + if (res != 0) + { + return res < 0; + } + } + return r1 < r2; + } +}; + +struct Where +{ + int op; + string value; + Where *left, *right; + + int tmp; + + Where(int o, const char *s, Where *l = NULL, Where *r = NULL) : op(o), value(s), left(l), right(r), tmp(-1) {} + + ~Where() + { + if (left != NULL) + { + delete left; + } + if (right != NULL) + { + delete right; + } + } +}; + +class WhereEvaluator +{ + Table *tab; + Where *root; + int row; + + void eval2(Where *w, string &s, int &type) + { + if (w->op == 'n' || w->op == 's') + { + s = w->value; + type = (w->op == 'n' ? 'I' : 'S'); + } + else + { + if (w->tmp == -1) + { + w->tmp = tab->columnByName(w->value.c_str()); + } + s = tab->data[row][w->tmp]; + type = tab->col[w->tmp].type; + } + } + + bool eval(Where *w) + { + if (w == NULL) + { + return true; + } + if (w->op == '!') + { + return !eval(w->left); + } + if (w->op == '&') + { + return eval(w->left) && eval(w->right); + } + if (w->op == '|') + { + return eval(w->left) || eval(w->right); + } + string s1, s2; + int type1, type2; + eval2(w->left, s1, type1); + eval2(w->right, s2, type2); + assert(type1 == type2); + if (w->op == '<') + { + return compare(s1, s2, type1) < 0; + } + if (w->op == '>') + { + return compare(s1, s2, type1) > 0; + } + if (w->op == '=') + { + return compare(s1, s2, type1) == 0; + } + if (w->op == LE) + { + return compare(s1, s2, type1) <= 0; + } + if (w->op == GE) + { + return compare(s1, s2, type1) >= 0; + } + if (w->op == NE) + { + return compare(s1, s2, type1) != 0; + } + assert(false); + } + +public: + WhereEvaluator(Table *t, Where *w) : tab(t), root(w) {} + + bool evaluate(int r) + { + row = r; + return eval(root); + } +}; + +vector tables; +vector
tmp_tables; + +void dumpTable(const Table &t) +{ + for (int i = 0; i < t.ncols; i++) + { + printf("%10s", t.col[i].name.c_str()); + } + printf("\n"); + for (int i = 0; i < t.nrows; i++) + { + for (int j = 0; j < t.ncols; j++) + { + printf("%10s", t.data[i][j].c_str()); + } + printf("\n"); + } + printf("\n"); +} + +void readTables() +{ + int ntables = nextint(); + assert(ntables <= 30); + tables.clear(); + tables.resize(ntables); + for (int ti = 0; ti < ntables; ti++) + { + Table &t = tables[ti]; + assert(tok == 'a'); + t.name = string(tok_s); + next(); + assert(tok == 'n'); + t.ncols = tok_n; + next(); + assert(1 <= t.ncols && t.ncols <= 10); + assert(tok == 'n'); + t.nrows = tok_n; + t.col.resize(t.ncols); + for (int i = 0; i < t.ncols; i++) + { + assert(next() == 'a'); + t.col[i].name = string(tok_s); + assert(next() == 'a'); + tok_s[0] = toupper(tok_s[0]); + t.col[i].type = tok_s[0]; + assert(strlen(tok_s) == 1 && (tok_s[0] == 'I' || tok_s[0] == 'S')); + } + t.data.clear(); + t.data.assign(t.nrows, vector(t.ncols)); + for (int i = 0; i < t.nrows; i++) + { + for (int j = 0; j < t.ncols; j++) + { + scanf(" %s", tok_s); + t.data[i][j] = string(tok_s); + } + } + next(); + } +} + +Table *parseFrom() +{ + Table *A = NULL; + if (tok == '(') + { + next(); + A = parseFrom(); + assert(tok == ')'); + next(); + } + else + { + assert(tok == 'a'); + for (int i = 0; i < (int)tables.size(); i++) + { + if (strcasecmp(tables[i].name.c_str(), tok_s) == 0) + { + A = &tables[i]; + break; + } + } + next(); + } + assert(A != NULL); + for (;;) + { + if (tok != 'a' || strcasecmp(tok_s, "INNER") != 0) + { + return A; + } + skip("INNER"); + skip("JOIN"); + Table *B = parseFrom(); + skip("ON"); + assert(tok == 'a'); + int col1 = A->columnByName(tok_s); + next(); + assert(tok == '='); + next(); + assert(tok == 'a'); + int col2 = B->columnByName(tok_s); + next(); + assert(A->col[col1].type == B->col[col2].type); + int coltype = A->col[col1].type; + Table *C = new Table(); + tmp_tables.push_back(C); + C->name = ""; + C->nrows = 0; + C->ncols = A->ncols + B->ncols; + C->col = A->col; + for (int i = 0; i < B->ncols; i++) + { + C->col.push_back(B->col[i]); + } + C->data.clear(); + vector o1(A->nrows), o2(B->nrows); + for (int i = 0; i < A->nrows; i++) + { + o1[i] = i; + } + for (int i = 0; i < B->nrows; i++) + { + o2[i] = i; + } + sort(o1.begin(), o1.end(), ByColumn(A, col1)); + sort(o2.begin(), o2.end(), ByColumn(B, col2)); + int posA = 0, posB = 0; + vector pairs; + while (posA < A->nrows && posB < B->nrows) + { + int res = compare(A->data[o1[posA]][col1], B->data[o2[posB]][col2], coltype); + if (res != 0) + { + if (res < 0) + { + posA++; + } + else + { + posB++; + } + continue; + } + int p, q; + for (p = posA; p < A->nrows; p++) + { + if (compare(A->data[o1[posA]][col1], A->data[o1[p]][col1], coltype) != 0) + { + break; + } + } + for (q = posB; q < B->nrows; q++) + { + if (compare(A->data[o1[posA]][col1], B->data[o2[q]][col2], coltype) != 0) + { + break; + } + } + for (int i = posA; i < p; i++) + for (int j = posB; j < q; j++) + { + pairs.push_back((((long long)o1[i]) << 32) | o2[j]); + } + posA = p; + posB = q; + } + sort(pairs.begin(), pairs.end()); + for (int k = 0; k < (int)pairs.size(); k++) + { + int r1 = pairs[k] >> 32; + int r2 = pairs[k] & 0xffffffff; + C->data.push_back(A->data[r1]); + for (int i = 0; i < B->ncols; i++) + { + C->data.back().push_back(B->data[r2][i]); + } + C->nrows++; + } + A = C; + } +} + +Table *parseFromClause() +{ + skip("FROM"); + tmp_tables.clear(); + return parseFrom(); +} + +Where *parseWhere(); + +Where *parseWhere2() +{ + if (tok == '(') + { + next(); + Where *res = parseWhere(); + assert(tok == ')'); + next(); + return res; + } + char ch; + scanf(" %c", &ch); + ungetc(ch, stdin); + if (tok == 'a' && strcasecmp(tok_s, "NOT") == 0 && strchr("<>=", ch) == NULL) + { + next(); + return new Where('!', "", parseWhere2()); + } + assert(tok == 'a' || tok == 's' || tok == 'n'); + Where *l = new Where(tok, tok_s); + next(); + assert(tok == '=' || tok == LE || tok == GE || tok == NE || tok == '<' || tok == '>'); + int op = tok; + next(); + assert(tok == 'a' || tok == 's' || tok == 'n'); + Where *r = new Where(tok, tok_s); + next(); + return new Where(op, "", l, r); +} + +Where *parseWhere() +{ + Where *res = parseWhere2(); + while (tok == 'a') + { + if (strcasecmp(tok_s, "AND") == 0) + { + next(); + res = new Where('&', "", res, parseWhere2()); + } + else if (strcasecmp(tok_s, "OR") == 0) + { + next(); + res = new Where('|', "", res, parseWhere2()); + } + else + { + break; + } + } + return res; +} + +Where *parseWhereClause() +{ + if (tok != 'a' || strcasecmp(tok_s, "WHERE") != 0) + { + return NULL; + } + skip("WHERE"); + return parseWhere(); +} + +void solveQuery() +{ + skip("SELECT"); + vector selnames; + if (tok == '*') + { + next(); + } + else + { + for (;;) + { + assert(tok == 'a'); + selnames.push_back(tok_s); + next(); + if (tok != ',') + { + break; + } + next(); + } + } + Table *from = parseFromClause(); + Where *where = parseWhereClause(); + WhereEvaluator where_eval(from, where); + // dumpTable(*from); + vector sel; + if (selnames.size() == 0) + { + for (int i = 0; i < (int)from->ncols; i++) + { + sel.push_back(i); + selnames.push_back(from->col[i].name); + } + } + else + { + for (int i = 0; i < (int)selnames.size(); i++) + { + sel.push_back(from->columnByName(selnames[i].c_str())); + } + } + OrderBy order; + order.tab = from; + order.col.clear(); + order.dir.clear(); + if (tok == 'a' && strcasecmp(tok_s, "ORDER") == 0) + { + skip("ORDER"); + skip("BY"); + for (;;) + { + assert(tok == 'a'); + order.col.push_back(from->columnByName(tok_s)); + next(); + if (tok == 'a' && strcasecmp(tok_s, "ASCENDING") == 0) + { + order.dir.push_back(1); + next(); + } + else if (tok == 'a' && strcasecmp(tok_s, "DESCENDING") == 0) + { + order.dir.push_back(-1); + next(); + } + else + { + order.dir.push_back(1); + } + if (tok != ',') + { + break; + } + next(); + } + } + vector rows; + for (int i = 0; i < from->nrows; i++) + if (where_eval.evaluate(i)) + { + rows.push_back(i); + } + sort(rows.begin(), rows.end(), order); + printf("%d %d\n", sel.size(), rows.size()); + for (int i = 0; i < (int)sel.size(); i++) + { + // printf("%s\n", selnames[i].c_str()); + printf("%s\n", from->col[sel[i]].name.c_str()); + } + for (int i = 0; i < (int)rows.size(); i++) + { + int r = rows[i]; + for (int j = 0; j < (int)sel.size(); j++) + { + printf(j == 0 ? "%s" : " %s", from->data[r][sel[j]].c_str()); + } + printf("\n"); + } + if (where != NULL) + { + delete where; + } + for (int i = 0; i < (int)tmp_tables.size(); i++) + { + delete tmp_tables[i]; + } + tmp_tables.clear(); +} + +int main() +{ + next(); + int T = nextint(); + for (int cs = 1; cs <= T; cs++) + { + readTables(); + solveQuery(); + if (cs < T) + { + printf("\n"); + } + } +} diff --git a/10758.cpp b/10758.cpp new file mode 100644 index 0000000..9d073ee --- /dev/null +++ b/10758.cpp @@ -0,0 +1,212 @@ +#include + +using namespace std; + +int nroads; +struct Road +{ + int a, b; + double len; +} roads[15000]; + +struct Edge +{ + int y; + double c; + Edge(int Y, double C) : y(Y), c(C) {} +}; + +int N; +vector G[15000]; + +double vdist[15000]; +int heap[15000], idx[15000], heap_size; + +void heapup(int k) +{ + int x = heap[k]; + for (; k > 1 && vdist[heap[k >> 1]] > vdist[x]; k >>= 1) + { + idx[heap[k] = heap[k >> 1]] = k; + } + idx[heap[k] = x] = k; +} + +void heapdown(int x) +{ + while (2 * x <= heap_size) + { + int y = 2 * x; + if (y + 1 <= heap_size && vdist[heap[y + 1]] < vdist[heap[y]]) + { + y++; + } + if (vdist[heap[x]] < vdist[heap[y]]) + { + break; + } + swap(heap[x], heap[y]); + idx[heap[x]] = x; + idx[heap[y]] = y; + x = y; + } +} + +bool dijkstra(int src, int dst) +{ + int prev[15000]; + for (int i = 0; i < N; i++) + { + idx[i] = -1; + } + for (int i = 0; i < N; i++) + { + vdist[i] = -1; + } + for (int i = 0; i < N; i++) + { + prev[i] = -1; + } + heap[heap_size = 1] = src; + idx[src] = 1; + vdist[src] = 0; + while (heap_size > 0) + { + int x = heap[1]; + idx[x] = -1; + if (--heap_size > 0) + { + idx[heap[1] = heap[heap_size + 1]] = 1; + heapdown(1); + } + // printf("x=%d (prev=%d): dist=%.5f\n", x, prev[x], vdist[x]); + for (int i = 0; i < (int)G[x].size(); i++) + { + int y = G[x][i].y; + if (vdist[y] < 0 || vdist[x] + G[x][i].c < vdist[y] - 1e-8) + { + vdist[y] = vdist[x] + G[x][i].c; + if (idx[y] < 0) + { + idx[y] = ++heap_size; + heap[heap_size] = y; + } + prev[y] = x; + heapup(idx[y]); + } + } + } + if (vdist[dst] < 0) + { + return false; + } + printf("%.3f\n", vdist[dst]); + vector path; + for (int i = dst; i != src; i = prev[i]) + { + path.push_back(roads[i].a); + } + for (int i = path.size() - 1; i >= 0; i--) + { + printf(i == 0 ? "%d\n" : "%d ", path[i] + 1); + } + return true; +} + +int main() +{ + int T; + scanf("%d", &T); + vector adj[1024], adj_rev[1024]; + double cx[1024], cy[1024]; + double PI = 2 * acos(0.0); + for (int cs = 1; cs <= T; cs++) + { + if (cs > 1) + { + printf("\n"); + } + int ncities, city1, city2; + double coef; + scanf("%d %d %d %d %lf", &ncities, &nroads, &city1, &city2, &coef); + city1--; + city2--; + for (int i = 0; i < ncities; i++) + { + scanf("%lf %lf", &cx[i], &cy[i]); + } + for (int i = 0; i < ncities; i++) + { + adj[i].clear(); + adj_rev[i].clear(); + } + for (int i = 0; i < nroads; i++) + { + int a, b; + scanf("%d %d", &a, &b); + a--; + b--; + adj[a].push_back(i); + adj_rev[b].push_back(i); + roads[i].a = a; + roads[i].b = b; + roads[i].len = sqrt((cx[a] - cx[b]) * (cx[a] - cx[b]) + (cy[a] - cy[b]) * (cy[a] - cy[b])); + } + N = 2 + nroads; + for (int i = 0; i < N; i++) + { + G[i].clear(); + } + int src = nroads; + int dst = nroads + 1; + roads[src].a = roads[src].b = city1; + roads[dst].a = roads[dst].b = city2; + for (int i = 0; i < (int)adj[city1].size(); i++) + { + int r = adj[city1][i]; + G[src].push_back(Edge(r, roads[r].len / 2)); + // printf("src-%d: %.5f\n", r, roads[r].len/2); + } + for (int i = 0; i < (int)adj_rev[city2].size(); i++) + { + int r = adj_rev[city2][i]; + G[r].push_back(Edge(dst, roads[r].len / 2)); + // printf("%d-dst: %.5f\n", r, roads[r].len/2); + } + for (int a = 0; a < ncities; a++) + { + for (int i = 0; i < (int)adj[a].size(); i++) + { + int r1 = adj[a][i], b = roads[r1].b; + for (int j = 0; j < (int)adj[b].size(); j++) + { + int r2 = adj[b][j], c = roads[r2].b; + if (a == c) + { + continue; + } + double ang = (cx[c] - cx[b]) * (cx[a] - cx[b]) + (cy[c] - cy[b]) * (cy[a] - cy[b]); + ang = -ang; + ang /= roads[r1].len; + ang /= roads[r2].len; + if (ang > 1.0) + { + ang = 1.0; + } + if (ang < -1.0) + { + ang = -1.0; + } + ang = acos(ang) / PI * 180.0; + double dist = roads[r1].len / 2 + roads[r2].len / 2 + coef * ang; + G[r1].push_back(Edge(r2, dist)); + // printf("%d-%d: %.5f (angle=%.3fdeg)\n", r1,r2,dist,ang); + } + } + } + if (!dijkstra(src, dst)) + { + printf("Impossible\n"); + } + } +} diff --git a/10759.cpp b/10759.cpp new file mode 100644 index 0000000..54ca459 --- /dev/null +++ b/10759.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +typedef long long ss; + +ss F[155][30], N, X; + +ss gcd(ss a, ss b) +{ + return b ? gcd(b, a % b) : a; +} +ss recur(ss n, ss level) +{ + ss i, j, d; + if (F[n][level]) + return F[n][level]; + if (level == N - 1) + { + if (n >= X) + return 1; + return 0; + } + if (n < X) + { + d = N - level - 1; + if (d * 6 + n < X) + return 0; + } + if (n >= X) + { + d = N - level - 1; + j = (ss)pow(6, d); + if (n <= 6) + F[n][level] = j; + return j; + } + if (level == N - 2) + { + d = X - n; + if (d > 6) + return 0; + d = 6 - d + 1; + if (d > 6) + d = 6; + F[n][level] = d; + return F[n][level]; + } + for (i = 1; i < 7; i++) + F[n][level] += recur(n + i, level + 1); + return F[n][level]; +} +void Cal() +{ + ss x = 0, y, z, i; + if (X == 0) + { + printf("1\n"); + return; + } + if (N == 1) + { + x = 6 - X + 1; + if (x <= 0) + { + printf("0\n"); + return; + } + } + else + { + for (i = 1; i < 7; i++) + x += recur(i, 0); + } + y = pow(6, N); + z = gcd(x, y); + x /= z; + y /= z; + if (!x) + { + printf("0\n"); + return; + } + if (x == y || y == 1) + { + printf("%lld\n", x); + return; + } + printf("%lld", x); + if (y) + printf("/%lld", y); + printf("\n"); +} + +int main() +{ + ss f = 0, i, j; + while (scanf("%lld%lld", &N, &X) == 2) + { + if (!N && !X) + break; + Cal(); + for (i = 0; i <= X; i++) + for (j = 0; j <= N; j++) + F[i][j] = 0; + } + return 0; +} diff --git a/1076.cpp b/1076.cpp new file mode 100644 index 0000000..0e2cedb --- /dev/null +++ b/1076.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +template +bool checkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } +template +bool checkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } + +typedef long long LL; +const int KIND = 26, MAXN = 110; +#define code(ch) ((ch) - 'a') +struct ACNode +{ + ACNode *next[KIND], *fail; + int mask; +} pool[MAXN], *pp, *root, *q[MAXN]; +ACNode *NewNode() +{ + pp->fail = NULL; + pp->mask = 0; + memset(pp->next, 0, sizeof(pp->next)); + return pp++; +} +void Initial() +{ + pp = pool; + root = NewNode(); +} + +void Insert(const char *str, int id) +{ + ACNode *now = root; + while (*str) + { + int i = code(*str); + now->next[i] = now->next[i] == 0 ? NewNode() : now->next[i]; + now = now->next[i]; + str++; + } + now->mask |= 1 << id; +} + +void BuildFail(ACNode *&now, int ith) +{ + if (now == root) + now->next[ith]->fail = root; + ACNode *tmp = now->fail; + while (tmp) + { + if (tmp->next[ith]) + { + now->next[ith]->fail = tmp->next[ith]; + return; + } + tmp = tmp->fail; + } + if (tmp == NULL) + now->next[ith]->fail = root; +} + +void Build() +{ + int head = 0, tail = 0; + q[tail++] = root; + while (head != tail) + { + ACNode *beg = q[head++]; + for (int i = 0; i < KIND; i++) + { + if (beg->next[i] == NULL) + continue; + BuildFail(beg, i); + beg->next[i]->mask |= beg->next[i]->fail->mask; + q[tail++] = beg->next[i]; + } + } +} + +ACNode *GoStatus(ACNode *now, int ith) +{ + while (now->next[ith] == NULL && now != root) + { + now = now->fail; + } + now = now->next[ith]; + return now == NULL ? root : now; +} +const int LEN = 30; +const int MASK = 1 << 10; +LL dp[LEN][MAXN][MASK]; +vector pwd; +char buffer[LEN]; +int s, tot; +void DFS(int len, int status, int MASK) +{ + if (dp[len][status][MASK] == 0) + return; + if (len == 0) + { + pwd.push_back(buffer); + return; + } + for (int j = 0; j < s; j++) + { + for (int mask = 0; mask < tot; mask++) + { + for (char c = 0; c < KIND; c++) + { + buffer[len - 1] = c + 'a'; + int k = GoStatus(pool + j, c) - pool, mask_ = pool[k].mask; + if (dp[len - 1][j][mask] != 0 && ((mask | mask_) == MASK) && k == status) + { + DFS(len - 1, j, mask); + } + } + } + } +} +int main() +{ + int n, m, cas = 1; + while (cin >> n >> m && (n + m)) + { + vector dict(m); + for (int i = 0; i < m; i++) + cin >> dict[i]; + sort(dict.begin(), dict.end()); + dict.erase(unique(dict.begin(), dict.end()), dict.end()); + m = int(dict.size()); + + Initial(); + for (int i = 0; i < m; i++) + Insert(dict[i].c_str(), i); + Build(); + memset(dp, 0, sizeof(dp)); + dp[0][0][0] = 1; + s = pp - pool, tot = (1 << m); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < s; j++) + { + for (int mask = 0; mask < tot; mask++) + { + if (dp[i][j][mask] == 0) + continue; + for (int c = 0; c < KIND; c++) + { + int k = GoStatus(pool + j, c) - pool, mask_ = pool[k].mask; + dp[i + 1][k][mask | mask_] += dp[i][j][mask]; + } + } + } + } + LL res = 0; + for (int i = 0; i < s; i++) + { + res += dp[n][i][tot - 1]; + } + printf("Case %d: %lld suspects\n", cas++, res); + memset(buffer, 0, sizeof(buffer)); + pwd.clear(); + if (res <= 42) + { + for (int i = 0; i < s; i++) + { + DFS(n, i, tot - 1); + } + sort(pwd.begin(), pwd.end()); + for (vector::iterator i = pwd.begin(); i != pwd.end(); ++i) + { + cout << *i << endl; + } + } + } + + return 0; +} diff --git a/10760.cpp b/10760.cpp new file mode 100644 index 0000000..0c28d1e --- /dev/null +++ b/10760.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +struct vec +{ + double x, y; +}; +struct seg +{ + vec a, b, d; +}; + +int zero(double x) +{ + return fabs(x) < EPS; +} +int zero(vec v) +{ + return zero(v.x) && zero(v.y); +} +int zero(seg s) +{ + return zero(s.a) && zero(s.b); +} +double operator*(vec &a, vec &b) +{ + return a.x * b.x + a.y * b.y; +} +double skew(vec a, vec b) +{ + return a.x * b.y - a.y * b.x; +} +vec operator-(vec &a, vec &b) +{ + vec c = {a.x - b.x, a.y - b.y}; + return c; +} +double sq(double x) +{ + return x * x; +} +double norm(vec v) +{ + return sqrt(v * v); +} + +int main() +{ + seg s1, s2; + double pi = 2. * acos(0.); + for (;;) + { + int i = scanf("%lf%lf%lf%lf%lf%lf%lf%lf", + &s1.a.x, &s1.a.y, &s2.a.x, &s2.a.y, + &s1.b.x, &s1.b.y, &s2.b.x, &s2.b.y); + if (i < 8 || (zero(s1) && zero(s2))) + { + break; + } + s1.d = s1.b - s1.a; + s2.d = s2.b - s2.a; + if (zero(s1.a - s2.a) && zero(s1.b - s2.b)) + { + printf("No move.\n"); + continue; + } + if (zero(s1.d - s2.d)) + { + printf("Translation by vector <%.1f,%.1f>.\n", + s2.a.x - s1.a.x + EPS, s2.a.y - s1.a.y + EPS); + continue; + } + double a1 = 2 * s2.a.x - 2 * s1.a.x, b1 = 2 * s2.a.y - 2 * s1.a.y; + double c1 = sq(s2.a.x) - sq(s1.a.x) + sq(s2.a.y) - sq(s1.a.y); + double a2 = 2 * s2.b.x - 2 * s1.b.x, b2 = 2 * s2.b.y - 2 * s1.b.y; + double c2 = sq(s2.b.x) - sq(s1.b.x) + sq(s2.b.y) - sq(s1.b.y); + double d = a1 * b2 - a2 * b1; + if (zero(d)) + { + a2 = s1.a.y - s1.b.y; + b2 = s1.b.x - s1.a.x; + c2 = s1.a.x * a2 + s1.a.y * b2; + d = a1 * b2 - a2 * b1; + } + vec O = {(c1 * b2 - b1 * c2) / d, (a1 * c2 - c1 * a2) / d}; + vec U = s1.a - O, V = s2.a - O; + double t = U * V / (norm(U) * norm(V)); + t = acos(t) * 180 / pi; + if (skew(U, V) < 0.) + { + t = -t; + } + while (t < -EPS) + { + t += 360; + } + while (t > 360 - EPS) + { + t -= 360; + } + if (zero(t)) + { + t = EPS; + } + printf("Rotation around (%.1f,%.1f) by %.1f degrees counterclockwise.\n", O.x + EPS, O.y + EPS, t); + } + return 0; +} diff --git a/10761.cpp b/10761.cpp new file mode 100644 index 0000000..f28189a --- /dev/null +++ b/10761.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +string broken; + +bool has_or_not(char x) +{ + for (unsigned int i = 0; i < broken.length(); i++) + { + if (tolower(broken.at(i)) == x) + { + return true; + } + } + return false; +} + +int main() +{ + int keyboard = 0; + cout << "+----------+----------------+-----------------------------+" << endl; + cout << "| Keyboard | # of printable | Additionally, the following |" << endl; + cout << "| | lines | letter keys can be broken |" << endl; + cout << "+----------+----------------+-----------------------------+" << endl; + while (cin >> broken && broken != "finish") + { + getchar(); + string text; + int printable_lines = 0; + int small_letters[28]; + for (int i = 0; i < 28; i++) + { + small_letters[i] = 0; + } + for (unsigned int i = 0; i < broken.length(); i++) + { + if (isalpha(broken.at(i))) + { + small_letters[tolower(broken.at(i)) - 'a'] = 1; + } + } + do + { + getline(cin, text); + bool flag = true; + for (unsigned int i = 0; i < text.length(); i++) + { + if (has_or_not(tolower(text.at(i)))) + { + flag = false; + break; + } + } + if (flag) + { + printable_lines++; + for (unsigned int i = 0; i < text.length(); i++) + { + if (isalpha(text.at(i))) + { + small_letters[tolower(text.at(i)) - 'a'] = 1; + } + } + } + } while (text != "END"); + int space = 0; + printf("| %3d | %3d | ", ++keyboard, printable_lines++); + for (int i = 0; i < 26; i++) + { + if (small_letters[i] == 0) + { + printf("%c", i + 'a'); + space++; + } + } + while (28 - space) + { + printf(" "); + space++; + } + printf("|\n"); + cout << "+----------+----------------+-----------------------------+" << endl; + } + return 0; +} diff --git a/10763.cpp b/10763.cpp index 9b9c80e..c00b991 100644 --- a/10763.cpp +++ b/10763.cpp @@ -1,40 +1,57 @@ -#include -using namespace std; - -int main(){ - for(;;){ - int n; - cin>>n; - if(n==0) - break; - int max=0,min=0; - int original[10000]={0},destination[10000]={0}; - for(int i=0;i>j; - original[j]++; - cin>>k; - destination[k]++; - if(k>max) - max=k; - if(kmax) - max=j; - if(j + +using namespace std; + +/* +Foreign Exchange +10763 +*/ +struct xx +{ + int x, y; +} X[500002], Y[500002]; +int totalcount, N; +int com1(const void *a, const void *b) +{ + xx *p = (xx *)a; + xx *q = (xx *)b; + if (p->x != q->x) + { + return p->x - q->x; + } + return p->y - q->y; +} +void Cal() +{ + int i; + qsort(X, N, sizeof(xx), com1); + qsort(Y, N, sizeof(xx), com1); + for (i = 0; i < N; i++) + { + if (X[i].x != Y[i].x || X[i].y != Y[i].y) + { + printf("NO\n"); + return; + } + } + printf("YES\n"); +} +int main() +{ + int i; + while (scanf("%d", &N), N) + { + if (!N) + { + break; + } + for (i = 0; i < N; i++) + { + scanf("%d%d", &X[i].x, &X[i].y); + Y[i].x = X[i].y; + Y[i].y = X[i].x; + } + Cal(); + } + return 0; +} diff --git a/10764.cpp b/10764.cpp new file mode 100644 index 0000000..3691a9c --- /dev/null +++ b/10764.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define MAXN 128 + +int main() +{ + static int r[MAXN], sign; + int i, b, a; + long long n; + while (scanf("%lld %d %d", &n, &b, &a) == 3 && b > 0) + { + if (n < 0) + { + sign = -1, n = -n; + } + else + { + sign = 1; + } + for (i = 0; i < MAXN; i++) + { + r[i] = 0; + } + for (i = 0; n > 0; i++, n /= (long long)b) + { + r[i] = (int)(n % (long long)b); + } + for (i = 0; i < MAXN; i++) + { + if (r[i] > a) + { + r[i] -= b; + r[i + 1]++; + } + } + for (i = 0; i < MAXN; i++) + { + r[i] *= sign; + } + for (i = MAXN - 1; i > 0 && r[i] == 0; i--) + ; + for (; i >= 0; i--) + if (r[i] >= 0) + { + printf("%d", r[i]); + } + else + { + printf("'%d", -r[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10765.cpp b/10765.cpp new file mode 100644 index 0000000..85df50b --- /dev/null +++ b/10765.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define maxn 10010 + +int low[maxn], pre[maxn], n, m, dfs_clock; +struct node +{ + int id; + int val; +} p[maxn]; +vector g[maxn]; + +bool cmp(node x, node y) +{ + if (x.val == y.val) + return x.id < y.id; + return x.val > y.val; +} + +void init() +{ + int i; + for (i = 0; i < n; i++) + { + g[i].clear(); + p[i].id = i; + p[i].val = 1; + } +} + +int dfs(int u, int fa) +{ + int i, j; + int lowu = pre[u] = ++dfs_clock; + int child = 0; + for (i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (!pre[v]) + { + child++; + int lowv = dfs(v, u); + lowu = min(lowu, lowv); + if (lowv >= pre[u]) + p[u].val++; + } + else if (pre[v] < pre[u] && v != fa) + lowu = min(lowu, pre[v]); + } + if (fa < 0 && child == 1) + p[u].val = 1; + low[u] = lowu; + return lowu; +} + +int main() +{ + int i, j; + while (scanf("%d %d", &n, &m) == 2 && n + m) + { + init(); + while (true) + { + int a, b; + scanf("%d %d", &a, &b); + g[a].push_back(b); + g[b].push_back(a); + if (a == -1 && b == -1) + break; + } + memset(pre, 0, sizeof(pre)); + dfs(0, -1); + if (p[0].val > 1) + p[0].val--; + sort(p, p + n, cmp); + for (i = 0; i < m; i++) + printf("%d %d\n", p[i].id, p[i].val); + puts(""); + } + return 0; +} diff --git a/10766.cpp b/10766.cpp new file mode 100644 index 0000000..49e248a --- /dev/null +++ b/10766.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +typedef unsigned long long num; + +int mat[64][64], N; + +int solveMod(int mod, int inv[]) +{ + int a[64][64]; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + a[i][j] = ((mat[i][j] % mod) + mod) % mod; + } + int det = 1; + for (int i = 0; i < N; i++) + { + int r; + for (r = i; r < N && a[r][i] == 0; r++) + ; + if (r >= N) + { + return 0; + } + if (i != r) + { + for (int k = 0; k < N; k++) + { + swap(a[i][k], a[r][k]); + } + det = (mod - det) % mod; + } + det = (det * a[i][i]) % mod; + for (int j = i + 1; j < N; j++) + { + int t = (a[j][i] * inv[a[i][i]]) % mod; + for (int k = 0; k < N; k++) + { + a[j][k] = ((a[j][k] - a[i][k] * t) % mod + mod) % mod; + } + } + } + return det; +} + +int main() +{ + const int NMOD = 7; + int imod[NMOD] = {223, 227, 229, 233, 239, 241, 251}, itab[NMOD][256]; + for (int i = 0; i < NMOD; i++) + { + for (int x = 0; x < imod[i]; x++) + { + int y; + for (y = imod[i] - 1; y > 0; y--) + if (((x * y) % imod[i]) == 1) + { + break; + } + itab[i][x] = y; + } + } + int M, K; + while (scanf("%d %d %d", &N, &M, &K) == 3 && N > 0) + { + memset(mat, 0, sizeof(mat)); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + mat[i][j] = (i == j ? 0 : -1); + } + for (int i = 0; i < M; i++) + { + int x, y; + scanf("%d %d", &x, &y); + mat[x - 1][y - 1] = mat[y - 1][x - 1] = 0; + } + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (i != j) + { + mat[i][i] -= mat[i][j]; + } + N--; + unsigned long long M = 1, res = 0; + for (int i = 0; i < NMOD; i++) + { + M *= imod[i]; + } + for (int i = 0; i < NMOD; i++) + { + int m = imod[i]; + int x = solveMod(m, itab[i]); + //printf("mod %d: %d\n", imod[i], x); + res = (res + x * (((M / m) * itab[i][(M / m) % m]) % M)) % M; + } + printf("%llu\n", res); + } +} diff --git a/10767.cpp b/10767.cpp new file mode 100644 index 0000000..bbe9f65 --- /dev/null +++ b/10767.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +double m0, len[32], tab[32][32]; +int got[32][32], n; + +double f(int k, int c) +{ + double m, s, v, c1; + if (k >= n) + { + return 0; + } + if (got[k][c]) + { + return tab[k][c]; + } + m = m0 - c; + s = len[k]; + c1 = (s / 10. + 10. + f(k + 1, c + 1) - f(k + 1, c)) / m; + v = sqrt(s / c1); + if ((c1 * m + s / m) < (c1 * v + s / v)) + { + v = m; + } + if (v > m) + { + v = m; + } + got[k][c] = 1; + return (tab[k][c] = c1 * v + s / v + f(k + 1, c) - s / (2. * m)); +} + +int main() +{ + int i; + while (scanf("%lf %d", &m0, &n) == 2) + { + for (i = 0; i < n; i++) + { + scanf("%lf", &len[i]); + } + memset(got, 0, sizeof(got)); + printf("%.4f\n", f(0, 0)); + } + return 0; +} diff --git a/10768.cpp b/10768.cpp new file mode 100644 index 0000000..8435a55 --- /dev/null +++ b/10768.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +static int adj[32][32], n; + +static int k5() +{ + static int p[8]; + int i, k; + for (p[0] = 0, k = 0; k >= 0;) + { + if (++p[k] > n) + { + k--; + continue; + } + for (i = 0; i < k; i++) + if (!adj[p[k]][p[i]]) + { + break; + } + if (i < k) + { + k--; + continue; + } + if (k == 4) + { + return 1; + } + p[k + 1] = p[k]; + k++; + } + return 0; +} + +static int k33() +{ + /* + a b c + | \ / | \ / | + d e f + */ + int a, b, c, d, e, f; + for (a = 1; a <= n; a++) + { + for (d = 1; d <= n; d++) + { + if (!adj[a][d]) + { + continue; + } + for (b = 1; b <= n; b++) + { + if (!adj[b][d] || a == b) + { + continue; + } + for (e = 1; e <= n; e++) + { + if (!adj[a][e] || !adj[b][e] || d == e) + { + continue; + } + for (c = 1; c <= n; c++) + { + if (!adj[d][c] || !adj[e][c] || c == a || c == b) + { + continue; + } + for (f = 1; f <= n; f++) + { + if (!adj[a][f] || !adj[b][f] || !adj[c][f] || f == d || f == e) + { + continue; + } + return 1; + } + } + } + } + } + } + return 0; +} + +int main() +{ + static int deg[32], m, x, y; + int i, j, k; + while (scanf("%d %d", &n, &m) == 2 && n > 0) + { + memset(adj, '\0', sizeof(adj)); + memset(deg, '\0', sizeof(deg)); + while (m-- > 0 && scanf("%d %d", &x, &y) == 2) + if (x != y) + { + adj[x][y] = adj[y][x] = 1; + } + for (i = 1; i <= n; i++) + for (deg[i] = 0, j = 1; j <= n; j++) + { + deg[i] += adj[i][j]; + } + do + { + for (i = 1, j = 0; i <= n; i++) + { + if (deg[i] == 1) + { + for (deg[i] = 0, j = 1; j <= n; j++) + if (adj[i][j]) + { + break; + } + adj[i][j] = adj[j][i] = 0; + deg[j]--; + } + else if (deg[i] == 2) + { + for (deg[i] = 0, j = 1; j <= n; j++) + if (adj[i][j]) + { + break; + } + for (k = j + 1; k <= n; k++) + if (adj[i][k]) + { + break; + } + adj[i][j] = adj[j][i] = 0; + adj[i][k] = adj[k][i] = 0; + adj[j][k] = adj[k][j] = 1; + } + } + } while (j != 0); + for (i = 1; i <= n; i++) + { + if (deg[i] == 0) + { + continue; + } + for (j = 1, k = 1; j <= n; j++) + if (deg[j] != 0) + { + adj[i][k++] = adj[i][j]; + } + } + for (i = 1, k = 0; i <= n; i++) + { + if (deg[i] == 0) + { + continue; + } + if (i != ++k) + { + memcpy(adj[k], adj[i], sizeof(adj[0])); + } + } + n = k; + for (i = 1; i <= n; i++) + { + adj[i][i] = 0; + } + printf((k5() || k33()) ? "NO\n" : "YES\n"); + } + return 0; +} diff --git a/10769.cpp b/10769.cpp new file mode 100644 index 0000000..f83ca66 --- /dev/null +++ b/10769.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +char str[5000], *p; +int w[100], nw; +int b[100], nb; +int H; +int main() +{ + int cases = 0; + while (true) + { + if (gets(str) == NULL) + { + break; + } + if (strcmp(str, "") == 0) + { + continue; + } + sscanf(str, "%d", &H); + gets(str); + for (p = strtok(str, " "), nb = 0; p; p = strtok(NULL, " ")) + { + b[nb++] = atoi(p); + } + gets(str); + for (p = strtok(str, " "), nw = 0; p; p = strtok(NULL, " ")) + { + w[nw++] = atoi(p); + } + for (int i = 0; i < nb; i++) + for (int j = i + 1; j < nb; j++) + if (b[i] < b[j]) + { + int t = b[i]; + b[i] = b[j]; + b[j] = t; + } + for (int i = 0; i < nw; i++) + for (int j = i + 1; j < nw; j++) + if (w[i] < w[j]) + { + int t = w[i]; + w[i] = w[j]; + w[j] = t; + } + bool flag = false; + if (b[0] + b[1] + w[0] + w[1] >= H) + { + for (int i = 0; i < nb && !flag; i++) + for (int j = 0; j < nw && !flag; j++) + for (int k = i + 1; k < nb && !flag; k++) + for (int l = j + 1; l < nw && !flag; l++) + if (b[i] + w[j] + b[k] + w[l] == H) + { + printf("%d %d %d %d\n", b[i], w[j], b[k], w[l]); + flag = true; + } + } + if (!flag) + { + puts("no solution"); + } + } +} diff --git a/1077.cpp b/1077.cpp new file mode 100644 index 0000000..ff0c5d2 --- /dev/null +++ b/1077.cpp @@ -0,0 +1,366 @@ +#include + +using namespace std; + +typedef long long ll; + +const double eps = 1e-8; +const int inf = 0x3f3f3f3f; + +#define maxx(a) memset(a, 0x3f, sizeof(a)) +#define zero(a) memset(a, 0, sizeof(a)) +#define FILL(a, b) memset(a, b, sizeof(a)) +#define REP(i, a, b) for (i = a; i < b; i++) +#define rep(i, n) REP(i, 0, n) +#define srep(i, n) for (i = 1; i <= n; i++) +#define snuke(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) +#define MP make_pair +#define fi first +#define se second +typedef pair PII; + +const int N = 300 + 11; + +struct Point +{ + double x, y; + Point(double xx, double yy) + { + x = xx; + y = yy; + } + Point() + { + x = 0; + y = 0; + } + void show() + { + cout << "??" << x << ' ' << y << endl; + } +}; +typedef Point Vector; +Vector operator+(Vector A, Vector B) +{ + return Vector(A.x + B.x, A.y + B.y); +} +Vector operator-(Point A, Point B) +{ + return Vector(A.x - B.x, A.y - B.y); +} +Vector operator*(Vector A, double p) +{ + return Vector(A.x * p, A.y * p); +} +Vector operator/(Vector A, double p) +{ + return Vector(A.x / p, A.y / p); +} +Vector operator*(double p, Vector A) +{ + return Vector(A.x * p, A.y * p); +} +Vector operator/(double p, Vector A) +{ + return Vector(A.x / p, A.y / p); +} +bool operator<(const Point &a, const Point &b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} +bool operator==(const Point &a, const Point &b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} +double Dot(Vector a, Vector b) +{ + return a.x * b.x + a.y * b.y; +} +inline double Cross(Vector a, Vector b) +{ + return a.x * b.y - a.y * b.x; +} +inline double Cross(Point O, Point A, Point B) +{ + return Cross(A - O, B - O); +} +inline int sign(double x) +{ + return (x > eps) - (x < -eps); +} +double Length(Vector a) +{ + return sqrt(Dot(a, a)); +} +double Angle(Vector A, Vector B) +{ + return acos(Dot(A, B) / Length(A) / Length(B)); +} +double Area2(Point A, Point B, Point C) +{ + return Cross(B - A, C - A); +} +Vector Rotate(Vector a, double rad) +{ + return Vector(a.x * cos(rad) - a.y * sin(rad), a.x * sin(rad) + a.y * cos(rad)); +} +Vector Normal(Vector a) +{ + double l = Length(a); + return Vector(-a.y / l, a.x / l); +} +Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) +{ + //当且仅当cross(v,w)!=0 + Vector u = P - Q; + double t = Cross(w, u) / Cross(v, w); + return P + v * t; +} +double DistanceToLine(Point p, Point a, Point b) +{ + Point v1 = b - a; + Point v2 = p - a; + return fabs(Cross(v1, v2)) / Length(v1); +} +double DistanceToSegment(Point p, Point a, Point b) +{ + if (a == b) + { + return Length(p - a); + } + Vector v1 = b - a, v2 = p - a, v3 = p - b; + if (dcmp(Dot(v1, v2)) < 0) + { + return Length(v2); + } + else if (dcmp(Dot(v1, v3)) > 0) + { + return Length(v3); + } + else + { + return fabs(Cross(v1, v2)) / Length(v1); + } +} +Point GetLineProjection(Point p, Point a, Point b) +{ + Vector v = b - a; + return a + v * (Dot(v, p - a) / Dot(v, v)); +} +bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) +{ + double c1 = Cross(a2 - a1, b1 - a1); + double c2 = Cross(a2 - a1, b2 - a1); + double c3 = Cross(b2 - b1, a1 - b1); + double c4 = Cross(b2 - b1, a2 - b1); + return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; +} +bool OnSegment(Point p, Point a1, Point a2) +{ + return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; +} +double ConvexPolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 1; i < n - 1; i++) + { + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} +struct Line +{ + Vector v; + Point p; + Point pp; + double ang; + Line() + { + } + Line(Point a, Vector vv) + { + p = a; + v = vv; + ang = atan2(v.y, v.x); + } + Line(Point a, Vector vv, Point b) + { + p = a; + v = vv; + ang = atan2(v.y, v.x); + pp = b; + } + Point point(double a) + { + return v + a * (p - v); + } + bool operator<(const Line &L) const + { + return ang < L.ang; + } +}; +inline bool in_line(Point a, Line d) +{ + Point b = d.p, c = d.pp; + return fabs((b.y - a.y) * (b.x - c.x) - (b.y - c.y) * (b.x - a.x)) < eps; +} +inline int across(Point a, Point b, Line line) +{ + int ans = sign(Cross(line.p, line.pp, a)) * sign(Cross(line.p, line.pp, b)); + return ans < 0; +} +bool OnLeft(Line l, Point pp) +{ + return Cross(l.v, pp - l.p) > 0; +} +Point GetLineIntersection(Line a, Line b) +{ + Vector u = a.p - b.p; + double t = Cross(b.v, u) / Cross(a.v, b.v); + return a.p + a.v * t; +} +struct Polygon +{ + int n; + Point p[400]; + Polygon() + { + n = 0; + } + Polygon(int nn, Point *pp) + { + n = nn; + for (int q = 0; q < n; q++) + { + p[q] = pp[q]; + } + } + Point operator[](int a) const + { + return p[a]; + } +} o; +Polygon cut(Polygon &po, Point a, Point s) +{ + Point x, c; + Polygon ans; + int sum = 0; + Vector tp = s - a; + a = a - tp; + s = s + tp; + for (int q = 0; q < po.n; q++) + { + x = po[q]; + c = po[(q + 1) % po.n]; + if (OnLeft(Line(a, tp), x) || OnSegment(x, a, s)) + { + ans.p[sum] = x; + sum++; + } + if (across(x, c, Line(a, tp, s))) + { + ans.p[sum] = GetLineIntersection(x, c - x, a, s - a); + sum++; + } + } + ans.n = sum; + return ans; +} + +int n; +Point a1[N], a2[N]; +int tot; +int stor[N]; +int sum; +void work2() +{ + int i, j; + tot = sum = 0; + int a, b, c; + rep(i, n) + { + cin >> a >> b >> c; + a *= 2; + b *= 2; + c *= 2; + Point b1(a - c / 2, 0), b2(a, b), b3(a + c / 2, 0); + a1[tot] = b1; + a2[tot] = b2; + tot++; + a1[tot] = b2; + a2[tot] = b3; + tot++; + stor[sum++] = a - c / 2; + stor[sum++] = a; + stor[sum++] = a + c / 2; + } + sort(stor, stor + sum); + sum = unique(stor, stor + sum) - stor; + double ans = 0; + for (i = 0; i < sum - 1; i++) + { + o.n = 4; + o.p[0] = Point(stor[i], 0); + o.p[1] = Point(stor[i + 1], 0); + o.p[2] = Point(stor[i + 1], 1000000000); + o.p[3] = Point(stor[i], 1000000000); + rep(j, tot) + { + if (a2[j].x <= stor[i] + eps) + { + continue; + } + if (a1[j].x >= stor[i + 1] - eps) + { + continue; + } + o = cut(o, a1[j], a2[j]); + } + rep(j, o.n) + { + Point s = o[j]; + Point t = o[(j + 1) % (o.n)]; + if (s.x == stor[i] && t.x == stor[i]) + { + continue; + } + if (s.x == stor[i + 1] && t.x == stor[i + 1]) + { + continue; + } + if (s.y == 0 && t.y == 0) + { + continue; + } + if (s.y == 1000000000 && t.y == 1000000000) + { + continue; + } + ans += Length(s - t); + } + } + printf("%.lf\n", ans / 2); +} + +int main() +{ + int cas = 1; + while (cin >> n && n) + { + printf("Case %d: ", cas++); + work2(); + printf("\n"); + } + return 0; +} diff --git a/10770.cpp b/10770.cpp new file mode 100644 index 0000000..c23ed75 --- /dev/null +++ b/10770.cpp @@ -0,0 +1,382 @@ +#include + +using namespace std; + +#define INF 0x3fffffff +#define DIM 32 +#define WALL 1 +#define GOAL 2 +#define BOX 4 +#define HSIZE 700000 +#define HDIV 131071 + +struct hdata {unsigned a[9], m, next;} hdata[HSIZE]; + +int map_[DIM][DIM], ndist[DIM][DIM], dead1[DIM][DIM], htab[HDIV], minx, maxx, miny, maxy, height, width, bsize, many, manx, maxlen, hsize; + +int hcheck(unsigned p, int m) +{ + static unsigned a[9]; + int i, j, k, h; + for (i = 0; i < 9; i++) + { + a[i] = 0; + } + for (h = 0, i = miny; i <= maxy; i++) + { + for (j = minx; j <= maxx; j++) + { + if ((map_[i][j] & BOX) == 0) + { + continue; + } + k = (i - miny) * width + j - minx; + a[k >> 5] |= 1U << (k & 31); + h = h * bsize + k; + if (h >= HDIV) + { + h %= HDIV; + } + } + } + a[8] = p; + for (i = htab[h]; i != 0; i = hdata[i].next) + { + if (memcmp(hdata[i].a, a, 9 * sizeof(unsigned)) == 0) + { + if (m >= hdata[i].m) + { + return 1; + } + hdata[i].m = m; + return 0; + } + } + if ((hsize + 1) >= HSIZE) + { + return 0; + } + i = ++hsize; + hdata[i].next = htab[h]; + hdata[i].m = m; + htab[h] = i; + for (j = 0; j < 9; j++) + { + hdata[i].a[j] = a[j]; + } + return 0; +} + +int estimateit() +{ + int i, j, r; + for (r = 0, i = miny; i <= maxy; i++) + { + for (j = minx; j <= maxx; j++) + { + if ((map_[i][j] & BOX) == 0) + { + continue; + } + if (dead1[i][j]) + { + return INF; + } + r += ndist[i][j]; + } + } + return r; +} + +void fill(int a[DIM][DIM], int y, int x) +{ + static int s[DIM * DIM * 2]; + int t; + a[s[0] = y][s[1] = x] = 1; + for (t = 2; t > 0;) + { + x = s[--t]; + y = s[--t]; +#define GO(Y, X) \ + if (a[Y][X] == 0 && (map_[Y][X] & (WALL | BOX)) == 0) \ + { \ + a[Y][X] = 1; \ + s[t++] = Y; \ + s[t++] = X; \ + } + GO(y + 1, x); + GO(y - 1, x); + GO(y, x + 1); + GO(y, x - 1); +#undef GO + } +} + +int findit(int m, int y0, int x0) +{ + static int a[32][DIM][DIM]; + int y, x; + if (hcheck((y0 << 8) | x0, m)) + { + return 0; + } + x = estimateit();//map_); + if (x == 0) + { + return 1; + } + if ((m + x) > maxlen) + { + return 0; + } + for (y = miny; y <= maxy; y++) + for (x = minx; x <= maxx; x++) + { + a[m][y][x] = 0; + } + fill(a[m], y0, x0); + for (y = miny; y <= maxy; y++) + { + for (x = minx; x <= maxx; x++) + { + if (a[m][y][x] == 0) + { + continue; + } +#define PUSH(dy, dx) \ + if ((map_[y + dy][x + dx] & BOX) && \ + (map_[y + dy + dy][x + dx + dx] & (BOX | WALL)) == 0) \ + { \ + map_[y + dy][x + dx] ^= BOX; \ + map_[y + dy + dy][x + dx + dx] ^= BOX; \ + if (findit(m + 1, y + dy, x + dx)) \ + return 1; \ + map_[y + dy][x + dx] ^= BOX; \ + map_[y + dy + dy][x + dx + dx] ^= BOX; \ + } + PUSH(1, 0); + PUSH(-1, 0); + PUSH(0, 1); + PUSH(0, -1); +#undef PUSH + } + } + return 0; +} + +int get_ndist(int y, int x) +{ + static int d[DIM][DIM], q[DIM * DIM * 2]; + int i, h, t, y1, x1; + if (map_[y][x] & WALL) + { + return 0; + } + memset(d, 0, sizeof(d)); + h = t = 0; + q[t++] = y; + q[t++] = x; + d[y][x] = 1; + while (h < t) + { + y = q[h++]; + x = q[h++]; + if (map_[y][x] & GOAL) + { + return (d[y][x] - 1); + } + for (i = 0; i < 4; i++) + { + y1 = y + "0211"[i] - '1'; + x1 = x + "1102"[i] - '1'; + if ((map_[y1][x1] & WALL) == 0 && d[y1][x1] == 0) + { + d[y1][x1] = d[y][x] + 1; + q[t++] = y1; + q[t++] = x1; + } + } + } + return 0; +} + +int prepmap_() +{ + static int v[DIM][DIM], s[DIM * DIM * 2]; + int i, j, k, y, x, y1, x1; + memset(v, 0, sizeof(v)); + k = 0; + s[k++] = many; + s[k++] = manx; + v[many][manx] = 1; + while (k > 0) + { + x = s[--k]; + y = s[--k]; + for (i = 0; i < 4; i++) + { + y1 = y + "0211"[i] - '1'; + x1 = x + "1102"[i] - '1'; + if ((map_[y1][x1] & WALL) == 0 && v[y1][x1] == 0) + { + v[y1][x1] = 1; + s[k++] = y1; + s[k++] = x1; + } + } + } + for (i = 0; i < DIM; i++) + { + for (j = 0; j < DIM; j++) + { + if (v[i][j]) + { + continue; + } + if (map_[i][j] & (BOX | GOAL)) + { + return 1; + } + map_[i][j] = WALL; + } + } + for (k = 0, i = 0; i < DIM; i++) + { + for (j = 0; j < DIM; j++) + { + if (map_[i][j] == WALL) + { + continue; + } + if (k++ == 0) + { + miny = maxy = i; + minx = maxx = j; + continue; + } + if (i < miny) + { + miny = i; + } + if (i > maxy) + { + maxy = i; + } + if (j < minx) + { + minx = j; + } + if (j > maxx) + { + maxx = j; + } + } + } + for (k = 0, i = miny; i <= maxy; i++) + for (j = minx; j <= maxx; j++) + { + k += ((map_[i][j] & BOX) ? 1 : 0) + ((map_[i][j] & GOAL) ? -1 : 0); + } + if (k != 0) + { + return 1; + } + width = maxx - minx + 1; + height = maxy - miny + 1; + bsize = width * height; + memset(ndist, 0, sizeof(ndist)); + for (i = miny; i <= maxy; i++) + for (j = minx; j <= maxx; j++) + { + ndist[i][j] = get_ndist(i, j); + } + memset(dead1, 0, sizeof(dead1)); + for (i = miny; i <= maxy; i++) + { + for (j = minx; j <= maxx; j++) + { + if (map_[i][j] & GOAL) + { + continue; + } + if (map_[i + 1][j] & map_[i][j + 1] & WALL) + { + dead1[i][j] = 1; + } + if (map_[i - 1][j] & map_[i][j + 1] & WALL) + { + dead1[i][j] = 1; + } + if (map_[i + 1][j] & map_[i][j - 1] & WALL) + { + dead1[i][j] = 1; + } + if (map_[i - 1][j] & map_[i][j - 1] & WALL) + { + dead1[i][j] = 1; + } + } + } + return 0; +} + +int main() +{ + char s[1024]; + int i, j, k; + while (gets(s)) + { + if (sscanf(s, "%d", &maxlen) != 1) + { + continue; + } + for (i = 0; i < DIM; i++) + for (j = 0; j < DIM; j++) + { + map_[i][j] = WALL; + } + for (i = 1; gets(s) && s[0] != '\0' && s[0] != '\r'; i++) + { + for (j = 1, k = 0; s[k]; j++, k++) + { + switch (s[k]) + { + case ' ': + map_[i][j] = 0; + break; + case '#': + map_[i][j] = WALL; + break; + case '.': + map_[i][j] = GOAL; + break; + case '$': + map_[i][j] = BOX; + break; + case '*': + map_[i][j] = BOX | GOAL; + break; + case '@': + map_[i][j] = 0; + many = i; + manx = j; + break; + case '+': + map_[i][j] = GOAL; + many = i; + manx = j; + break; + } + } + } + if (prepmap_()) + { + printf("NO\n"); + continue; + } + memset(htab, 0, sizeof(htab)); + hsize = 0; + printf(findit(0, many, manx) ? "YES\n" : "NO\n"); + } + return 0; +} diff --git a/10771.cpp b/10771.cpp new file mode 100644 index 0000000..1a75cbf --- /dev/null +++ b/10771.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int n, m, k; + while (scanf("%d%d%d", &n, &m, &k) == 3) + { + if (!n && !m && !k) + { + break; + } + if (m % 2) + { + puts("Keka"); + } + else + { + puts("Gared"); + } + } + return 0; +} diff --git a/10772.cpp b/10772.cpp new file mode 100644 index 0000000..df63a19 --- /dev/null +++ b/10772.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define MAX 41 +#define EPS (1.0e-10) +#define PI (acos(-1.0)) + +typedef double elem; +typedef complex point, vec; + +inline bool eq(const elem &a, const elem &b) +{ + return fabs(a - b) < EPS; +} +inline elem dist(const point &a, const point &b) +{ + return std::abs(a - b); +} +inline elem cross(const vec &a, const vec &b) +{ + return (a.real() * b.imag() - a.imag() * b.real()); +} +inline point intersection(const point &a1, const point &a2, const point &b1, const point &b2) +{ + vec b = b2 - b1; + vec a = a2 - a1; + return a1 + (cross(b, b1 - a1) / cross(b, a)) * a; +} +inline point rotp(const point &p, elem theta) +{ + return p * exp(point(0, theta)); +} +void DivideCircle(elem r, int n, point *v) +{ + for (int i = 0; i < n; ++i) + { + v[i] = rotp(point(0, r), (i * PI) / (n / 2)); + } + return; +} +inline elem AreaOfRegularPolygon(elem r, int n_even) +{ + return (n_even / 2) * (r * r * sin(PI / (n_even / 2))); +} + +inline elem AreaOfRoseWindow(elem *r, int n, int k) +{ + if (n / 2 == k) + { + return PI * r[k] * r[k] - AreaOfRegularPolygon(r[k - 1], n); + } + else + { + return AreaOfRegularPolygon(r[k], n) - AreaOfRegularPolygon(r[k - 1], n); + } +} +int main() +{ + int m; + scanf("%d", &m); + for (int tc = 0; tc < m; ++tc) + { + elem r; + int n, k; + point p[MAX]; // 円周ã®n分割点(時計回り) + elem kr[MAX]; // 円内部ã‹ã‚‰k番目ã®æ­£n角形ã®å¾„ + pair v0[MAX];// 円真上ã®é ‚点ã‹ã‚‰ã®å¯¾è§’ç·š + pair v1[MAX];// 上記å³éš£ã‹ã‚‰ã®å¯¾è§’ç·š + scanf("%lf%d%d", &r, &n, &k); + DivideCircle(r, n, p); + for (int i = 0; i < n / 2; ++i) + { + v0[i].first = p[0]; + v0[i].second = p[n / 2 + i]; + v1[i].first = p[1]; + v1[i].second = p[(n / 2 + i + 1) % n]; + } + for (int i = 1; i < n / 2; ++i) + { + point q = intersection(v0[i].first, v0[i].second, v1[i].first, v1[i].second); + kr[i] = std::abs(q); + } + kr[0] = 0.0; + kr[n / 2] = r; + printf("%.4lf\n", AreaOfRoseWindow(kr, n, k)); + } + return 0; +} diff --git a/10773.cpp b/10773.cpp index 16de4a1..0c71ac8 100644 --- a/10773.cpp +++ b/10773.cpp @@ -1,57 +1,24 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define EPS 1e-9 +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - cin >> T; - FOI(t, 1, T){ - printf("Case %d: ", t); - double D, V, U; - scanf("%lf%lf%lf", &D, &V, &U); - bool stat = true; - if (U <= V || U <= 0 || V <= 0) - printf("can't determine"); - else{ - double theta = asin(V / U); - double U1 = V / tan(theta); - double T1 = D / U1; - - double T2 = D / U; - - printf("%.3lf", T1 - T2); +int main() +{ + int t; + int d, v, u; + cin >> t; + for (int i = 0; i < t; i++) + { + cout << "Case " << i + 1 << ": "; + cin >> d >> v >> u; + if (v == 0 || v >= u) + { + cout << "can't determine" << endl; + } + else + { + cout << setprecision(3) << fixed << showpoint << abs(d * 1.0 / u - d * 1.0 / sqrt(u * u - v * v)) << endl; } - printf("\n"); } return 0; } - diff --git a/10774.cpp b/10774.cpp new file mode 100644 index 0000000..8a64b75 --- /dev/null +++ b/10774.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int J[30005], L[30005], E[30005]; +int main() +{ + int t, tt, i, j; + J[1] = 1; + for (i = 2; i <= 30000; i++) + { + if (i & 1) + { + J[i] = 2 * J[i >> 1] + 1; + } + else + { + J[i] = 2 * J[i >> 1] - 1; + } + } + for (i = 1; i <= 30000; i++) + { + if (J[i] == i) + { + L[i] = 0, E[i] = i; + } + else + { + L[i] = L[J[i]] + 1, E[i] = E[J[i]]; + } + } + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d", &i); + printf("Case %d: %d %d\n", t, L[i], E[i]); + } + return 0; +} diff --git a/10775.cpp b/10775.cpp new file mode 100644 index 0000000..031e2f4 --- /dev/null +++ b/10775.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +int matrix[3][3000], n; +void put(int loc, int start, int len) +{ + int s; + int temp[1000]; + memset(temp, 0, sizeof(temp)); + if (loc == 1 || loc == 6 || loc == 8) + { + for (int i = 0; i < len; i++) + { + temp[i] = start + i; + } + } + else if (loc == 5 || loc == 7 || loc == 3) + { + temp[len / 2] = start + len - 1; + int op = len - 1, ep = len / 2 - 1; + for (int i = 0; i < len - 1; i++) + if ((start + i) & 1) + { + temp[op--] = start + i; + } + else + { + temp[ep--] = start + i; + } + } + else + { + for (int i = 0, j = len / 2; i < len; i++, j = (j + 1) % len) + { + temp[j] = start + i; + } + } + if (loc == 1 || loc == 6 || loc == 8) + { + if (loc == 1) + { + s = 0; + } + else if (loc == 6) + { + s = len; + } + else + { + s = 2 * len; + } + for (int i = 0; i < len; i++) + { + matrix[0][s + i] = temp[i]; + } + } + else if (loc == 5 || loc == 7 || loc == 3) + { + if (loc == 5) + { + s = 0; + } + else if (loc == 7) + { + s = len; + } + else + { + s = 2 * len; + } + for (int i = 0; i < len; i++) + { + matrix[1][s + i] = temp[i]; + } + } + else + { + if (loc == 9) + { + s = 0; + } + else if (loc == 2) + { + s = len; + } + else + { + s = 2 * len; + } + for (int i = 0; i < len; i++) + { + matrix[2][s + i] = temp[i]; + } + } +} +int main() +{ + while (scanf("%d", &n), n) + { + int sum = 3 * (1 + 3 * n) * n / 2; + if (sum % n) + { + puts("IMPOSSIBLE"); + } + else + { + sum /= n; + int k = n / 3; + for (int i = 0; i < 9; i++) + { + put(i + 1, i * k + 1, k); + } + // printf("%d\n",n); + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < n; j++) + { + if (j) + { + putchar(' '); + } + printf("%d", matrix[i][j]); + } + printf("\n"); + } + } + } +} diff --git a/10776.cpp b/10776.cpp new file mode 100644 index 0000000..521fd98 --- /dev/null +++ b/10776.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +char ss[100], temp[100], fg[100]; +int R, len; + +int com(const void *a, const void *b) +{ + return *(char *)a - *(char *)b; +} + +void recur(int n, int lvl) +{ + temp[lvl] = ss[n]; + fg[n] = 1; + if (lvl == R - 1) + { + temp[lvl + 1] = '\0'; + puts(temp); + fg[n] = 0; + return; + } + if (lvl == len - 1) + { + fg[n] = 0; + return; + } + for (int i = n + 1; i < len; i++) + { + if (fg[i - 1] == 0 && ss[i] == ss[i - 1]) + { + continue; + } + recur(i, lvl + 1); + } + fg[n] = 0; +} + +void cal() +{ + int i; + len = strlen(ss); + qsort(ss, len, sizeof(char), com); + for (i = 0; i <= len - R; i++) + { + if (i && ss[i] == ss[i - 1]) + { + continue; + } + recur(i, 0); + } +} + +int main() +{ + char input[100]; + while (gets(input)) + { + sscanf(input, "%s%d", ss, &R); + cal(); + } + return 0; +} diff --git a/10777.cpp b/10777.cpp new file mode 100644 index 0000000..cfc893e --- /dev/null +++ b/10777.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int n; +double t1, t2, t3, t4, p; +int main() +{ + int t; + scanf("%d", &t); + for (int c = 1; c <= t; c++) + { + printf("Case %d: ", c); + scanf("%d", &n); + t1 = t2 = p = 0; + bool flag = true; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &t3, &t4); + t1 += fabs(t3) * t4; + if (t3 > 0) + { + t2 += t4; + } + if (t4 > 1 || t4 < 0) + { + flag = false; + } + p += t4; + } + if (fabs(p - 1) > 1e-6 || fabs(t2) < 1e-6 || !flag) + { + printf("God! Save me\n"); + } + else + { + printf("%.2lf\n", t1 / t2); + } + } +} diff --git a/10778.cpp b/10778.cpp new file mode 100644 index 0000000..5c11aaa --- /dev/null +++ b/10778.cpp @@ -0,0 +1,353 @@ +#include + +using namespace std; + +typedef long long num; + +int isqrt(num a) +{ + num x, y; + for (x = a; x > 1; x = y) + if ((y = (x + (a / x)) >> 1) >= x) + { + break; + } + return (int)x; +} + +int divisors(num r[], num x) +{ + static num a[64], b[64]; + static int c[64], e[64]; + int i, k, n; + if (x == 0) + { + r[0] = 1; + r[1] = 0; + return 1; + } + if (x < 0) + { + x = -x; + } + if ((x & 1) == 0) + for (b[0] = 2, e[0] = 0, n = 1; (x & 1) == 0; x >>= 1) + { + e[0]++; + } + else + { + n = 0; + } + for (i = 3, k = isqrt(x) + 1; i <= k; i += 2) + { + if ((x % i) == 0) + { + for (b[n] = i, e[n] = 1, x /= i; (x % i) == 0; e[n]++) + { + x /= i; + } + n++; + k = isqrt(x); + } + } + if (x > 1) + { + b[n] = x, e[n] = 1, n++; + } + for (k = 0, i = 0; i < n; i++) + { + a[i] = 1, c[i] = 0; + } + do + { + for (r[k] = 1, i = 0; i < n; i++) + { + r[k] *= a[i]; + } + for (k++, i = 0; i < n; i++) + { + if (++c[i] <= e[i]) + { + a[i] *= b[i]; + break; + } + else + { + a[i] = 1; + c[i] = 0; + } + } + } while (i < n); + r[k] = 0; + return k; +} + +void sort(num a[], int n) +{ + int i, j; + num t; + for (i = 1; i < n; i++) + { + for (t = a[i], j = i - 1; j >= 0 && a[j] > t; j--) + { + a[j + 1] = a[j]; + } + a[j + 1] = t; + } +} + +num gcd(num x, num y) +{ + int k; + for (k = 0; ((x | y) & 1) == 0; k++, x >>= 1, y >>= 1) + ; + for (;;) + { + if (x == 0) + { + return (y << k); + } + if (y == 0) + { + return (x << k); + } + while ((x & 1) == 0) + { + x >>= 1; + } + while ((y & 1) == 0) + { + y >>= 1; + } + if (x > y) + { + x -= y; + } + else + { + y -= x; + } + } +} + +struct poly +{ + unsigned m, n, a[1024]; +} plist[16]; + +unsigned moduli[] = {32749, 32719, 32693, 32531, 0}; + +num roots[1024]; +int nroots; + +unsigned eval(struct poly *z, num p0, num q0) +{ + static unsigned pw[1024], qw[1024]; + unsigned i, p, q; + if (p0 >= 0) + { + p = p0 % z->m; + } + else + { + p = z->m - ((-p0) % z->m); + if (p == z->m) + { + p = 0; + } + } + if (q0 >= 0) + { + q = q0 % z->m; + } + else + { + q = z->m - ((-q0) % z->m); + if (q == z->m) + { + q = 0; + } + } + for (pw[0] = 1, i = 0; i < z->n; i++) + { + pw[i + 1] = (pw[i] * p) % z->m; + } + for (qw[0] = 1, i = 0; i < z->n; i++) + { + qw[i + 1] = (qw[i] * q) % z->m; + } + for (p = 0, i = 0; i <= z->n; i++) + { + p = (p + ((z->a[i] * pw[z->n - i]) % z->m) * qw[i]) % z->m; + } + return p; +} + +void divpoly(struct poly *z, num p0, num q0) +{ + static unsigned pw[1024], qw[1024], b[1024]; + unsigned i, j, p, q; + if (p0 >= 0) + { + p = p0 % z->m; + } + else + { + p = z->m - ((-p0) % z->m); + if (p == z->m) + { + p = 0; + } + } + if (q0 >= 0) + { + q = q0 % z->m; + } + else + { + q = z->m - ((-q0) % z->m); + if (q == z->m) + { + q = 0; + } + } + for (pw[0] = 1, i = 0; i < z->n; i++) + { + pw[i + 1] = (pw[i] * p) % z->m; + } + for (qw[0] = 1, i = 0; i < z->n; i++) + { + qw[i + 1] = (qw[i] * q) % z->m; + } + for (i = 0; i <= z->n; i++) + for (b[i] = 0, j = 0; j <= i; j++) + { + b[i] = (b[i] + ((z->a[j] * pw[i - j]) % z->m) * qw[z->n - i + j]) % z->m; + } + for (i = 0; i <= z->n; i++) + { + z->a[i] = b[i]; + } + z->n--; +} + +void initpoly(num coef[], int deg) +{ + int i, j; + for (i = 0; moduli[i]; i++) + { + plist[i].m = moduli[i]; + plist[i].n = deg; + for (j = 0; j <= deg; j++) + { + if (coef[j] >= 0) + { + plist[i].a[j] = coef[j] % (num)moduli[i]; + } + else + { + plist[i].a[j] = moduli[i] - ((-coef[j]) % (num)moduli[i]); + if (plist[i].a[j] == moduli[i]) + { + plist[i].a[j] = 0; + } + } + } + } +} + +int check(num p, num q) +{ + int i; + for (i = 0; moduli[i]; i++) + if (eval(&plist[i], p, q) != 0) + { + return 0; + } + return 1; +} + +void divall(num p, num q) +{ + int i; + for (i = 0; moduli[i]; i++) + { + divpoly(&plist[i], p, q); + } +} + +void solve(num coef[], int deg) +{ + static num p[16384], q[16384]; + int i, j; + nroots = 0; + while (deg > 0 && coef[deg] == 0) + { + roots[nroots++] = 0; + deg--; + } + if (deg < 1) + { + return; + } + initpoly(coef, deg); + divisors(p, coef[deg]); + divisors(q, coef[0]); + for (i = 0; p[i] != 0; i++) + { + for (j = 0; q[j] != 0; j++) + { + if (gcd(p[i], q[j]) != 1) + { + continue; + } + while (check(p[i], q[j])) + { + roots[nroots++] = (coef[0] / q[j]) * p[i]; + divall(p[i], q[j]); + } + while (check(-p[i], q[j])) + { + roots[nroots++] = (coef[0] / q[j]) * (-p[i]); + divall(-p[i], q[j]); + } + } + } +} + +int main() +{ + static num coef[1024]; + int deg, i, t; + for (t = 0; scanf("%d %lld", °, &coef[0]) == 2 && deg > 0; t++) + { + if (t > 0) + { + printf("\n"); + } + for (i = 1; i <= deg; i++) + { + scanf("%lld", &coef[i]); + if (i & 1) + { + coef[i] = -coef[i]; + } + } + solve(coef, deg); + if (nroots == 0) + { + printf("No solution.\n"); + } + else + { + sort(roots, nroots); + for (i = 0; i < nroots; i++) + { + printf(i ? " %lld" : "%lld", roots[i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/10779.cpp b/10779.cpp new file mode 100644 index 0000000..03a5c08 --- /dev/null +++ b/10779.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 1500; +const int inf = (1 << 21); +int cap[N][N]; +bool visited[N]; +int parent[N]; +int flow[N][N]; +int layer[N]; +void make_layer(int n, int s, int t) +{ + queue Q; + Q.push(s); + layer[s] = 0; + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + rep(i, n) + { + if (cap[now][i] - flow[now][i] > 0 && layer[i] == -1) + { + layer[i] = layer[now] + 1; + Q.push(i); + } + } + } +} + +int augment(int now, int t, int n, int f) +{ + if (now == t || f == 0) + { + return f; + } + if (visited[now]) + { + return 0; + } + visited[now] = true; + rep(i, n) + { + if (layer[now] < layer[i]) + { + int tmp = augment(i, t, n, min(f, cap[now][i] - flow[now][i])); + if (tmp > 0) + { + flow[now][i] += tmp; + flow[i][now] = -flow[now][i]; + visited[now] = false; + return tmp; + } + } + } + return 0; +} + +int dinic(int n, int s, int t) +{ + int ansflow = 0; + bool flag = true; + rep(i, n) rep(j, n) flow[i][j] = 0; + while (flag) + { + fill(layer, layer + n, -1); + fill(visited, visited + n, false); + flag = false; + //make layer + make_layer(n, s, t); + if (layer[t] == -1) + { + break;//if no such path to go to sink + } + //find blocking flow + for (int f = 1; f; flag = true) + { + f = augment(s, t, n, inf); + if (f == 0) + { + break; + } + ansflow += f; + } + } + return ansflow; +} + +#define FIRST(i, k, m) ((i) * (m)*4 + (k)) +#define SECOND(i, k, m) ((i) * (m)*4 + (m) + (k)) +#define THIRD(i, k, m) ((i) * (m)*4 + 2 * (m) + (k)) +#define FOURTH(i, k, m) ((i) * (m)*4 + 3 * (m) + (k)) + +main() +{ + int te; + cin >> te; + int card[50]; + rep(tc, te) + { + int n, m; + cin >> n >> m; + int node = n * m * 4 + 2, src = node - 2, dst = node - 1; + rep(i, node) rep(j, node) cap[i][j] = 0; + rep(i, n) + { + rep(j, m) card[j] = 0; + int num, t; + cin >> num; + rep(j, num) + { + cin >> t; + card[t - 1]++; + } + rep(j, m) + { + if (i == 0) + { + cap[src][FIRST(0, j, m)] = inf; + cap[FIRST(i, j, m)][SECOND(i, j, m)] = card[j]; + cap[SECOND(i, j, m)][THIRD(i, j, m)] = inf; + cap[THIRD(i, j, m)][FOURTH(i, j, m)] = 1; + cap[FOURTH(i, j, m)][dst] = 1; + } + else + { + if (card[j] > 0) + { + cap[THIRD(i, j, m)][FOURTH(i, j, m)] = card[j] - 1; + } + if (card[j] == 0) + { + cap[FIRST(i, j, m)][SECOND(i, j, m)] = 1; + } + cap[FOURTH(i, j, m)][THIRD(0, j, m)] = 1; + } + } + } + REP(j, 1, n) + { + rep(k, m) + { + cap[SECOND(0, k, m)][FIRST(j, k, m)] = inf; + } + } + REP(i, 1, n) + { + REP(j, 1, n) + { + if (i == j) + { + continue; + } + //trade + rep(k, m) + { + cap[FOURTH(i, k, m)][FIRST(j, k, m)] = inf; + } + } + //self + rep(j, m) + { + rep(k, m) + { + if (j != k) + { + cap[SECOND(i, j, m)][THIRD(i, k, m)] = inf; + } + } + } + } + int ans = dinic(node, src, dst); + cout << "Case #" << tc + 1 << ": " << ans << endl; + } + return false; +} diff --git a/1078.cpp b/1078.cpp new file mode 100644 index 0000000..f4f0fda --- /dev/null +++ b/1078.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (int)(n); i++) +typedef pair PII; +const int N = 105; +const int dx[4] = {-1, 1, 0, 0}; +const int dy[4] = {0, 0, -1, 1}; +int m, n; +int r1, c1, r2, c2; +int R[N][N]; +int D[N][N]; +int d[N][N][4][2]; + +struct Node +{ + int x, y, d, mul; + Node() { x = y = d = mul = 0; } + Node(int x, int y, int d, int mul) : x(x), y(y), d(d), mul(mul) {} + bool operator<(const Node oth) const + { + return x < oth.x; + } +}; + +bool inRange(int x, int y) +{ + return 0 <= x && x < m && 0 <= y && y < n; +} + +int getW(int x, int y, int o) +{ + if (!inRange(x + dx[o], y + dy[o])) + return 0; + switch (o) + { + case 0: + return D[x - 1][y]; + case 1: + return D[x][y]; + case 2: + return R[x][y - 1]; + case 3: + return R[x][y]; + } + while (1) + ; + return 0; +} + +bool up(int &cur, int x) +{ + if (cur == -1 || x < cur) + { + cur = x; + return 1; + } + else + { + return 0; + } +} + +int main() +{ + int Tc = 0; + while (scanf("%d%d%d%d%d%d", &m, &n, &r1, &c1, &r2, &c2) != EOF && m + n + r1 + r2 + c1 + c2) + { + printf("Case %d: ", ++Tc); + r1--; + r2--; + c1--; + c2--; + rep(i, m) + { + rep(j, n - 1) + scanf("%d", &R[i][j]); + if (i + 1 == m) + continue; + rep(j, n) + scanf("%d", &D[i][j]); + } + memset(d, 0xff, sizeof(d)); + priority_queue> Q; + rep(o, 4) + { + int x = r1 + dx[o]; + int y = c1 + dy[o]; + if (inRange(x, y) && getW(r1, c1, o)) + { + d[x][y][o][1] = 2 * getW(r1, c1, o); + Q.push(make_pair(-d[x][y][o][1], Node(x, y, o, 1))); + } + } + if (r1 == r2 && c1 == c2) + { + puts("0"); + continue; + } + while (!Q.empty()) + { + int curd = -Q.top().first; + Node t = Q.top().second; + Q.pop(); + if (curd != d[t.x][t.y][t.d][t.mul]) + continue; + int ori = getW(t.x - dx[t.d], t.y - dy[t.d], t.d); + if (t.mul) + { + rep(o, 4) + { + int x = t.x + dx[o]; + int y = t.y + dy[o]; + int w = getW(t.x, t.y, o); + if (!w) + continue; + if (o == t.d) + { + if (up(d[x][y][o][0], curd + w)) + { + Q.push(make_pair(-d[x][y][o][0], Node(x, y, o, 0))); + } + if (up(d[x][y][o][1], curd + 2 * w)) + { + Q.push(make_pair(-d[x][y][o][1], Node(x, y, o, 1))); + } + } + else + { + if (up(d[x][y][o][1], curd + 2 * w)) + { + Q.push(make_pair(-d[x][y][o][1], Node(x, y, o, 1))); + } + } + } + } + else + { + rep(o, 4) + { + int x = t.x + dx[o]; + int y = t.y + dy[o]; + int w = getW(t.x, t.y, o); + if (!w) + continue; + if (o == t.d) + { + if (up(d[x][y][o][0], curd + w)) + { + Q.push(make_pair(-d[x][y][o][0], Node(x, y, o, 0))); + } + if (up(d[x][y][o][1], curd + 2 * w)) + { + Q.push(make_pair(-d[x][y][o][1], Node(x, y, o, 1))); + } + } + else + { + if (up(d[x][y][o][1], curd + 2 * w + ori)) + { + Q.push(make_pair(-d[x][y][o][1], Node(x, y, o, 1))); + } + } + } + } + } + int ans = -1; + rep(o, 4) + { + if (d[r2][c2][o][1] != -1) + { + up(ans, d[r2][c2][o][1]); + } + } + if (ans == -1) + { + puts("Impossible"); + } + else + { + printf("%d\n", ans); + } + } +} diff --git a/10780.cpp b/10780.cpp new file mode 100644 index 0000000..31fdca1 --- /dev/null +++ b/10780.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a) +#define MAXN 10005 + +int P[1500], tp, N, M, sv[MAXN]; + +void Prime_table() +{ + int i, j; + for (i = 2; i * i < MAXN;) + { + for (j = i + i; j < MAXN; j += i) + sv[j] = 1; + for (i++; sv[i]; i++) + ; + } + P[0] = 2; + tp = 1; + for (i = 3; i < 10000; i += 2) + if (!sv[i]) + P[tp++] = i; +} + +int Factor_in_N(int p) +{ + int t = 0, i; + for (i = p; i <= N; i *= p) + { + t += N / i; + } + return t; +} + +int Factor_in_M(int p) +{ + int t = 0; + while (M % p == 0) + { + t++; + M /= p; + } + return t; +} + +void Cal() +{ + int i, j, k, min = 10000000; + for (i = 0; P[i] <= M; i++) + { + if (M % P[i]) + continue; + j = Factor_in_M(P[i]); + k = Factor_in_N(P[i]); + if (j > k) + { + printf("Impossible to divide\n"); + return; + } + min = MIN(min, k / j); + } + printf("%d\n", min); +} + +int main() +{ + int k = 1, kase; + Prime_table(); + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &M, &N); + printf("Case %d:\n", k++); + Cal(); + } + return 0; +} diff --git a/10782.cpp b/10782.cpp new file mode 100644 index 0000000..19e6564 --- /dev/null +++ b/10782.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +bool alpha[26], digit[10]; +char words[5][10]; +int len[5], x[26]; +int n; +void swap(char &a, char &b) +{ + char c = a; + a = b; + b = c; +} +bool DFS(int r, int c, int sum) +{ + if (c == len[n - 1]) + { + int t[26], s = 0; + for (int i = 0; i < 26; i++) + if (x[i] != -1) + { + t[s++] = i; + } + for (int i = 0; i < s; i++) + { + if (i) + { + putchar(' '); + } + printf("%c=%d", t[i] + 'A', x[t[i]]); + } + printf("\n"); + return true; + } + int index = words[r][c] - 'A'; + if (r == n - 1) + { + if (x[index] != -1) + { + if (sum % 10 != x[index]) + { + return false; + } + return DFS(0, c + 1, sum / 10); + } + else + { + int t = sum % 10; + if (digit[t]) + { + return false; + } + x[index] = sum % 10; + digit[t] = true; + if (DFS(0, c + 1, sum / 10)) + { + return true; + } + digit[t] = false; + x[index] = -1; + return false; + } + } + if (c >= len[r]) + { + return DFS(r + 1, c, sum); + } + if (x[index] != -1) + { + return DFS(r + 1, c, sum + x[index]); + } + else + { + for (int i = 0; i < 10; i++) + { + if (len[r] > 1 && c == len[r] - 1 && i == 0) + { + continue; + } + if (!digit[i]) + { + digit[i] = true; + x[index] = i; + if (DFS(r + 1, c, sum + i)) + { + return true; + } + x[index] = -1; + digit[i] = false; + } + } + return false; + } +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + memset(alpha, false, sizeof(alpha)); + memset(digit, false, sizeof(digit)); + memset(len, 0, sizeof(len)); + memset(x, -1, sizeof(x)); + for (int i = 0; i < n; i++) + { + scanf("%s", words[i]); + len[i] = strlen(words[i]); + for (int j = 0; j < len[i]; j++) + { + alpha[words[i][j] - 'A'] = true; + } + for (int j = 0, k = len[i] - 1; j < k; j++, k--) + { + swap(words[i][j], words[i][k]); + } + } + DFS(0, 0, 0); + } + return 0; +} diff --git a/10783.cpp b/10783.cpp new file mode 100644 index 0000000..5f44d85 --- /dev/null +++ b/10783.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int numOfCase; + +int main() +{ + int leftRange, rightRange; + while (cin >> numOfCase) + { + int kase; + for (kase = 0; kase < numOfCase; kase++) + { + cin >> leftRange >> rightRange; + int index, sum = 0; + for (index = leftRange; index <= rightRange; index++) + if (index % 2 == 1) + { + sum += index; + } + cout << "Case " << kase + 1 << ": " << sum << endl; + } + } + return 0; +} diff --git a/10784.cpp b/10784.cpp new file mode 100644 index 0000000..d52503d --- /dev/null +++ b/10784.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int kase = 1; + +int main() +{ + long long N; + while (cin >> N && N) + { + cout << "Case " << kase++ << ": " << (long long)ceil((3.0 + sqrt(9.0 + 8.0 * N)) / 2.0) << endl; + } + return 0; +} diff --git a/10785.cpp b/10785.cpp index 85a1abc..a923e84 100644 --- a/10785.cpp +++ b/10785.cpp @@ -1,30 +1,68 @@ -#include -#include -#include +#include + using namespace std; -int main() { - string vowel = "AUEOI", V = ""; - string consonant = "JSBKTCLDMVNWFXGPYHQZR", C = ""; - for (int i = 0; i < vowel.length(); i++) - for (int j = 0; j < 21; j++) V += vowel[i]; - for (int i = 0; i < consonant.length(); i++) - for (int j = 0; j < 5; j++) C += consonant[i]; - int T, t, N; - cin >> T; - for (t = 1; t <= T; t++) { - cin >> N; - string S = "", A = "", B = ""; - if (N & 1) A = V.substr(0, (N + 1) / 2); - else A = V.substr(0, N / 2); - B = C.substr(0, N / 2); - sort(A.begin(), A.end()); - sort(B.begin(), B.end()); - for (int i = 0; i < A.length(); i++) { - S += A[i]; - if (i < B.length()) S += B[i]; +#define MIN(a, b) (a > b ? b : a) + +char V[] = "AUEOI"; +char C[] = "JSBKTCLDMVNWFXGPYHQZR"; +char final[300], con[200], vow[200]; + +int N; + +int com(const void *a, const void *b) +{ + return *(char *)a - *(char *)b; +} + +void cal() +{ + int tc, tv, cc, cv, i, j = 0, k = 0, x, temp1, temp2, l = 0; + tv = temp1 = (N + 1) / 2; + tc = temp2 = N - tv; + cc = (tc + 4) / 5; + cv = (tv + 20) / 21; + for (i = 0; i < cv; i++) + { + x = MIN(21, tv); + for (j = 0; j < x; j++) + { + vow[k++] = V[i]; } - cout << "Case " << t << ": " << S << endl; + tv -= x; + } + vow[k] = '\0'; + qsort(vow, temp1, sizeof(vow[0]), com); + for (i = 0; i < cc; i++) + { + x = MIN(5, tc); + for (j = 0; j < x; j++) + { + con[l++] = C[i]; + } + tc -= x; + } + qsort(con, temp2, sizeof(con[0]), com); + for (i = 0; i < temp2; i++) + { + printf("%c%c", vow[i], con[i]); + } + if (temp1 != temp2) + { + printf("%c", vow[i]); + } + printf("\n"); +} + +int main() +{ + int ks, k = 1; + scanf("%d", &ks); + while (ks--) + { + scanf("%d", &N); + printf("Case %d: ", k++); + cal(); } return 0; } diff --git a/10787.cpp b/10787.cpp new file mode 100644 index 0000000..4a40f6b --- /dev/null +++ b/10787.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +int amax, amin, bmax, bmin, mmax, mmin; + +int Neg(int n) +{ + int a, b, c, d; + if (bmin >= 0) + return 0; + b = (int)abs(bmin); + b *= 2; + if (bmax >= 0) + a = 0; + else + { + a = abs(bmax + 1); + a *= 2; + } + c = b / n; + d = a / n; + if (n % 2) + { + c /= 2; + d /= 2; + } + return c - d; +} +int Pos(int n) +{ + int a, b, c, d; + if (bmax <= 0) + return 0; + if (bmin <= 0) + a = 0; + else + a = (bmin - 1) * 2; + b = bmax * 2; + c = b / n; + d = a / n; + if (n % 2) + { + c /= 2; + d /= 2; + } + return c - d; +} +void Cal() +{ + int total = 0, dif, i; + dif = bmax - bmin + 1; + for (i = mmin; i <= mmax; i++) + { + if (i == 1 || i == 2) + { + total += dif; + continue; + } + total += Neg(i); + total += Pos(i); + if (bmax >= 0 && bmin <= 0) + total++; + } + dif = amax - amin + 1; + printf("%d\n", total * dif); +} + +int main() +{ + int ks, k = 1; + scanf("%d", &ks); + while (ks--) + { + scanf("%d%d%d%d%d%d", &amin, &amax, &bmin, &bmax, &mmin, &mmax); + printf("Case %d: ", k++); + Cal(); + } + return 0; +} diff --git a/10788.cpp b/10788.cpp new file mode 100644 index 0000000..f270f44 --- /dev/null +++ b/10788.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +/* +10788. +Parenthesizing Palindromes +*/ + +#define maxn 102 +char F[maxn][maxn]; +int Cost[maxn][maxn]; +char ss[maxn]; +int Impos() +{ + int ff[30] = {0}; + int i, j; + for (i = 0; ss[i]; i++) + { + j = ss[i] - 'a'; + ff[j]++; + } + for (i = 0; i < 26; i++) + if (ff[i] % 2) + { + return 1; + } + return 0; +} +int Recur(int l, int r) +{ + int i, c = 0, d, k; + d = r - l + 1; + if (F[l][r]) + { + return Cost[l][r]; + } + if (d % 2) + { + return 0; + } + if (d == 2) + { + if (ss[l] == ss[r]) + { + Cost[l][r] = 1; + return 1; + } + return 0; + } + F[l][r] = 1; + if (ss[l] == ss[r]) + { + c = Recur(l + 1, r - 1); + } + for (i = l + 1; i + 1 < r; i += 2) + { + if (ss[l] != ss[i]) + { + continue; + } + d = Recur(l, i); + k = Recur(i + 1, r); + c += d * k; + } + Cost[l][r] = c; + return c; +} +void Cal() +{ + int i, l, r, c = 0, d; + l = strlen(ss); + if (l % 2 || Impos()) + { + cout << "Invalid\n"; + return; + } + if (l == 2) + { + if (ss[0] == ss[1]) + { + cout << "Valid, Unique\n"; + } + else + { + cout << "Invalid\n"; + } + return; + } + if (ss[0] == ss[l - 1]) + { + c = Recur(1, l - 2); + } + for (i = 1; i + 2 < l; i += 2) + { + if (ss[0] != ss[i]) + { + continue; + } + r = Recur(0, i); + d = 0; + if (r) + { + d = Recur(i + 1, l - 1); + } + c += r * d; + if (c > 1) + { + break; + } + } + if (c > 1) + { + cout << "Valid, Multiple\n"; + } + else if (c == 1) + { + cout << "Valid, Unique\n"; + } + else + { + cout << "Invalid\n"; + } +} +int main() +{ + int k, c = 1; + cin >> k; + while (k--) + { + cin >> ss; + cout << "Case " << c++ << ": "; + Cal(); + memset(F, 0, maxn * maxn); + } + return 0; +} diff --git a/10789.cpp b/10789.cpp index bcd4ccc..7a2f4f8 100644 --- a/10789.cpp +++ b/10789.cpp @@ -1,57 +1,97 @@ -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 2500 +#include -bool prime[MAX+1]; +using namespace std; -void Seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; - int i, j; - FOI(i, 2, MAX) - for(j=2*i; j<=MAX; j += i) - prime[j] = false; -} - -int main(){ - Seive(); - int T, t; - cin >> T; - FOI(t, 1, T){ - map Map; - map::iterator it; - - int i, j; - string str; - cin >> str; - int L = str.length()-1; - FOI(i, 0, L) - Map[str[i]]++; - bool flag = false; - - cout << "Case " << t << ": "; - - for( it=Map.begin(); it!=Map.end(); it++ ){ - if( prime[(*it).second] ){ - flag = true; - cout << (*it).first; +int main() +{ + int t, x[62], p[303], c1 = 5, j; + char c[2002]; + bool b; + p[0] = 2; + p[1] = 3; + p[2] = 5; + p[3] = 7; + p[4] = 11; + p[5] = 13; + for (int i = 17; i <= 2002; i += 2) + { + for (j = 1; p[j] <= sqrt((double)i); j++) + if (i % p[j] == 0) + { + break; + } + if (i % p[j] != 0) + { + c1++; + p[c1] = i; + } + } + cin >> t; + for (int i = 1; i <= t; i++) + { + cin >> c; + for (int j = 0; j < 62; j++) + { + x[j] = 0; + } + for (int j = 0; c[j] != '\0'; j++) + { + if (c[j] <= '9') + { + x[c[j] - '0']++; + } + else if (c[j] <= 'Z') + { + x[c[j] - 'A' + 10]++; + } + else + { + x[c[j] - 'a' + 36]++; } } - - if( !flag ) + cout << "Case " << i << ": "; + b = 0; + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 303; j++) + { + if (x[i] == p[j]) + { + cout << (char)('0' + i); + b = 1; + break; + } + } + } + for (int i = 10; i < 36; i++) + { + for (int j = 0; j < 303; j++) + { + if (x[i] == p[j]) + { + cout << (char)('A' + i - 10); + b = 1; + break; + } + } + } + for (int i = 36; i < 62; i++) + { + for (int j = 0; j < 303; j++) + { + if (x[i] == p[j]) + { + cout << (char)('a' + i - 36); + b = 1; + break; + } + } + } + if (b == 0) + { cout << "empty"; - + } cout << endl; } return 0; } - diff --git a/1079.cpp b/1079.cpp new file mode 100644 index 0000000..1f7a8ab --- /dev/null +++ b/1079.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define REP(i, a, b) for (int i = int(a); i <= int(b); i++) + +int n; +vector> planes; + +int p[15]; +bool vis[15]; +double maxmin; +double eps = 1e-3; + +bool equal(double x, double y) +{ + return fabs(x - y) <= eps; +} + +bool valid(double gap) +{ + double current = planes[p[0]].first; + REP(i, 1, n - 1) + { + current += gap; + if (planes[p[i]].second < current && !equal(planes[p[i]].second, current)) + { + return false; + } + else if (planes[p[i]].first > current && !equal(planes[p[i]].first, current)) + { + current = planes[p[i]].first; + } + } + return true; +} + +void validate() +{ + double st = 0.0; + double end = (1440 * 60) * 1.0; + int itr = 100; + double mid = -1.0; + while (itr--) + { + mid = (st + end) / 2.0; + if (valid(mid)) + { + st = mid; + } + else + { + end = mid; + } + } + maxmin = max(maxmin, mid); +} + +void permute(int index) +{ + if (index == n) + { + validate(); + return; + } + REP(i, 0, n - 1) + { + if (vis[i]) + { + continue; + } + p[index] = i; + vis[i] = true; + permute(index + 1); + vis[i] = false; + } +} + +int main() +{ + int tc = 1; + while (true) + { + scanf("%d", &n); + if (n == 0) + { + break; + } + planes.clear(); + REP(i, 0, n - 1) + { + int a, b; + scanf("%d %d", &a, &b); + planes.push_back(make_pair((double)(a * 60), (double)(b * 60))); + } + memset(vis, false, sizeof(vis)); + maxmin = -1.0; + permute(0); + int res = (int)(maxmin + 0.5); + printf("Case %d: %d:", tc++, res / 60); + if (res % 60 < 10) + { + printf("0%d\n", res % 60); + } + else + { + printf("%d\n", res % 60); + } + } + return 0; +} diff --git a/10790.cpp b/10790.cpp new file mode 100644 index 0000000..d44015d --- /dev/null +++ b/10790.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a) +typedef long long ss; +ss N, M, O; +void Cal() +{ + ss f, l, total, point; + if (N <= 1) + { + printf("0\n"); + return; + } + f = (M * (M - 1)) / 2; + total = N - 1; + l = f * total; + point = ((f + l) * total) / 2; + printf("%lld\n", point); +} +int main() +{ + ss k = 1; + while (1) + { + scanf("%lld%lld", &N, &M); + if (!N && !M) + { + break; + } + printf("Case %lld: ", k++); + O = N + M; + N = MIN(N, M); + M = O - N; + Cal(); + } + return 0; +} diff --git a/10791.cpp b/10791.cpp new file mode 100644 index 0000000..75eabbe --- /dev/null +++ b/10791.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +/* +LCM +*/ +#define MAX 2147483647 +#define MAXN 46342 +typedef long long ss; +char sv[MAXN]; +int P[5000], tp, N; +void Prime_table() +{ + tp = 1; + int i, j; + for (i = 2; i * i <= MAXN;) + { + for (j = i + i; j <= MAXN; j += i) + { + sv[j] = 1; + } + for (i++; sv[i]; i++) + ; + } + P[0] = 2; + for (i = 3; i < MAXN; i += 2) + if (sv[i] == 0) + { + P[tp++] = i; + } +} +ss Cal() +{ + int i, c, f = 0; + ss total = 0, temp = N; + for (i = 0; P[i] * P[i] <= N && i < tp; i++) + { + if (N % P[i] == 0) + { + f++; + c = 1; + while (N % P[i] == 0) + { + N /= P[i]; + c *= P[i]; + } + total += c; + } + } + if (N > 1) + { + f++; + } + if (f <= 1) + { + return temp + 1; + } + if (N > 1) + { + total += N; + } + return total; +} +int main() +{ + int k = 1; + Prime_table(); + while (scanf("%d", &N) && N) + { + printf("Case %d: ", k++); + printf("%lld\n", Cal()); + } + return 0; +} diff --git a/10792.cpp b/10792.cpp new file mode 100644 index 0000000..450da24 --- /dev/null +++ b/10792.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + double r, d, h1; + int n; + cin >> n; + for (int i = 1; i <= n; i++) + { + cin >> r >> d >> h1; + double ab = sqrt((r * r) - ((r - d) * (r - d))); + ab = ab * 2; + double w = asin((r - h1) / r); + double t = asin((r - d) / r); + double ot = w - t; + double h2 = h1 + ab * sin(ot); + printf("Case %d: %.4lf\n", i, h2); + } + return 0; +} diff --git a/10793.cpp b/10793.cpp new file mode 100644 index 0000000..d577a9c --- /dev/null +++ b/10793.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define MAXN 105 +#define INF 21474478 +#define MIN(a, b) (a > b ? b : a) + +int Cost[MAXN][MAXN], N, E; + +void Ini() +{ + int i, j; + for (i = 1; i <= 100; i++) + { + for (j = 1 + i; j <= 100; j++) + { + Cost[i][j] = Cost[j][i] = INF; + } + Cost[i][i] = 0; + } +} +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + Cost[i][j] = MIN(Cost[i][j], Cost[i][k] + Cost[k][j]); +} +void Cal() +{ + int A[100], ta = 0, i, j, f; + int max = 0, min = INF; + Floyd(); + for (i = 6; i <= N; i++) + { + f = 1; + for (j = 2; j < 6; j++) + { + if (Cost[i][j] != Cost[i][j - 1] || Cost[i][j] == INF) + { + f = 0; + break; + } + } + if (f) + A[ta++] = i; + } + for (i = 0; i < ta; i++) + { + max = 0; + f = A[i]; + for (j = 1; j <= N; j++) + if (Cost[f][j] > max) + max = Cost[f][j]; + min = MIN(min, max); + } + min != INF ? printf("%d\n", min) : printf("-1\n"); +} + +int main() +{ + int ks, k = 1, u, v, c; + scanf("%d", &ks); + while (ks--) + { + scanf("%d%d", &N, &E); + Ini(); + while (E--) + { + scanf("%d%d%d", &u, &v, &c); + if (Cost[u][v] > c) + { + Cost[u][v] = Cost[v][u] = c; + } + } + printf("Map %d: ", k++); + Cal(); + } + return 0; +} diff --git a/10794.cpp b/10794.cpp new file mode 100644 index 0000000..6bd244d --- /dev/null +++ b/10794.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +typedef struct +{ + double x, y, z; +} velocity; + +int main() +{ + int test_case, data = 0, time; + double x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; + double a, b, c, t, dis; + velocity v1, v2; + scanf("%d", &test_case); + while (test_case--) + { + scanf("%d", &time); + scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &z1, &x2, &y2, &z2); + v1.x = (x2 - x1) / time; + v1.y = (y2 - y1) / time; + v1.z = (z2 - z1) / time; + scanf("%lf %lf %lf %lf %lf %lf", &x3, &y3, &z3, &x4, &y4, &z4); + v2.x = (x4 - x3) / time; + v2.y = (y4 - y3) / time; + v2.z = (z4 - z3) / time; + a = (v2.x - v1.x) * (v2.x - v1.x) + (v2.y - v1.y) * (v2.y - v1.y) + (v2.z - v1.z) * (v2.z - v1.z); + b = 2 * ((x1 * v1.x + x3 * v2.x - x3 * v1.x - x1 * v2.x) + (y1 * v1.y + y3 * v2.y - y3 * v1.y - y1 * v2.y) + (z1 * v1.z + z3 * v2.z - z3 * v1.z - z1 * v2.z)); + c = (x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1) + (z3 - z1) * (z3 - z1); + (a == 0) ? (t = 0) : (t = -b / (2 * a)); + if (t < 0) + { + t = 0; + } + if (t > 0 && a * t * t + b * t + c < 0) + { + dis = 0.0; + } + else + { + dis = sqrt(a * t * t + b * t + c); + } + printf("Case %d: %.4lf\n", ++data, dis); + } + return 0; +} diff --git a/10795.cpp b/10795.cpp new file mode 100644 index 0000000..7511bf8 --- /dev/null +++ b/10795.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define MAXN 100 + +typedef long long LL; + +int s[MAXN], t[MAXN]; + +LL fun(int *p, int i, int final) +{ + if (i == 0) + { + return 0; + } + if (p[i] == final) + { + return fun(p, i - 1, final); + } + return fun(p, i - 1, 6 - final - p[i]) + (1LL << (i - 1)); +} + +int main() +{ + int n; + int cas = 1; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 1; i <= n; i++) + { + scanf("%d", &s[i]); + } + for (int i = 1; i <= n; i++) + { + scanf("%d", &t[i]); + } + int k = n; + while (s[k] == t[k] && k) + { + k--; + } + LL ans = 0; + if (k != 0) + { + int other = 6 - s[k] - t[k]; + ans = fun(s, k - 1, other) + fun(t, k - 1, other) + 1; + } + printf("Case %d: %lld\n", cas++, ans); + } + return 0; +} diff --git a/10797.cpp b/10797.cpp new file mode 100644 index 0000000..bf2bab3 --- /dev/null +++ b/10797.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; + int id; + Point *match; +}; + +double cross(Point *o, Point *a, Point *b) +{ + return (a->x - o->x) * (b->y - o->y) - (a->y - o->y) * (b->x - o->x); +} + +Point *findMatch(Point *one, Point **P, int n) +{ + int a = 0, b = n - 1, i, j; + int mid = n / 2; + Point *two; + while (a < b) + { + swap(P[a], P[(a + b) >> 1]); + two = P[a]; + for (i = a, j = a + 1; j <= b; j++) + { + if (cross(one, two, P[j]) > 0) + { + i++; + swap(P[i], P[j]); + } + } + swap(P[a], P[i]); + // P[0..i-1] < P[i] < P[i+1..M-1] + if (i < mid) + { + a = i + 1; + } + else if (i == mid) + { + break; + } + else + { + b = i - 1; + } + } + return P[mid]; +} + +Point all[10010]; +Point *A[10010], *B[10010]; +int N, M; + +pair solve() +{ + assert(N % 2 == 1 && M % 2 == 1); + for (int i = 1; i < N; i++) + { + swap(A[i], A[rand() % (i + 1)]); + } + for (int i = 1; i < M; i++) + { + swap(B[i], B[rand() % (i + 1)]); + } + for (int i = 0; i < N; i++) + { + A[i]->match = findMatch(A[i], B, M); + } + for (int i = 0; i < M; i++) + { + B[i]->match = findMatch(B[i], A, N); + if (B[i]->match->match == B[i]) + { + return make_pair(B[i]->match->id, B[i]->id); + } + } + abort(); +} + +int main() +{ + int total; + srand(10852765); + for (int cs = 1; scanf("%d", &total) == 1 && total > 0; cs++) + { + N = M = 0; + for (int i = 0; i < total; i++) + { + scanf("%lf %lf", &all[i].x, &all[i].y); + all[i].match = NULL; + if (all[i].x < 0) + { + all[i].id = N; + A[N++] = &all[i]; + } + else + { + all[i].id = M; + B[M++] = &all[i]; + } + } + pair res = solve(); + printf("Case %d: %d %d\n", cs, res.first, res.second); + } +} diff --git a/10798.cpp b/10798.cpp new file mode 100644 index 0000000..532b0e9 --- /dev/null +++ b/10798.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define SIZE 22 + +#define N 0 +#define E 1 +#define S 2 +#define W 3 + +int visited[SIZE][SIZE][SIZE / 2][SIZE / 2][SIZE / 2][SIZE / 2] = {0}; +char m[4][SIZE][SIZE]; + +int dx[] = {0, 1, 0, -1}; +int dy[] = {-1, 0, 1, 0}; + +class state +{ +public: + int x, y; + int num[4]; + state(){}; + state(int tx, int ty, int a, int b, int c, int d) : x(tx), y(ty) + { + num[0] = a; + num[1] = b; + num[2] = c; + num[3] = d; + } +}; + +void bfs(int n, int tc) +{ + int ans = n; + queue Q; + Q.push(state(n / 2, n / 2, 0, 0, 0, 0)); + state now; + visited[n / 2][n / 2][0][0][0][0] = tc; + while (!Q.empty()) + { + now = Q.front(); + Q.pop(); + rep(i, 4) + { + state next = now; + next.x += dx[i]; + next.y += dy[i]; + bool flag = true; + rep(j, 4) + { + if (m[j][next.y][next.x] == 'R') + { + next.num[j]++; + } + if (next.num[j] > n / 2) + { + flag = false; + } + } + if (flag == false) + { + continue; + } + int tmp = max(next.num[0], max(next.num[1], max(next.num[2], next.num[3]))); + if (tmp > ans) + { + continue; + } + if (next.x == 0 || next.y == 0 || next.x == n - 1 || next.y == n - 1) + { + ans = min(ans, tmp); + } + else if (visited[next.y][next.x][next.num[0]][next.num[1]][next.num[2]][next.num[3]] != tc) + { + visited[next.y][next.x][next.num[0]][next.num[1]][next.num[2]][next.num[3]] = tc; + Q.push(next); + } + } + } + cout << "At most " << ans << " rose(s) trampled." << endl; +} + +void make_transpose(int n) +{ + REP(k, 1, 4) + { + rep(i, n) + { + rep(j, n) + { + m[k][i][j] = m[k - 1][j][n - i - 1]; + } + } + } +} + +main() +{ + int n, tc = 1; + while (cin >> n && n) + { + rep(i, n) cin >> m[0][i]; + make_transpose(n); + bfs(n, tc); + tc++; + } +} diff --git a/10799.cpp b/10799.cpp new file mode 100644 index 0000000..0c2b222 --- /dev/null +++ b/10799.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + long long start, end, house, counter = 1; + while (cin >> start >> end >> house && !(start == 0 && end == 0 && house == 0)) + { + long long ans = 0, last; + long long S, d, E, N; + if ((house - 1) % 2 == 0) + { + last = start + house - 1; + d = house - 1; + N = (end - start) / (house - 1); + } + else + { + last = start + 2 * (house - 1); + d = 2 * (house - 1); + N = ((end - start) / (house - 1)) / 2; + } + S = end - last + 1; + E = S - (N - 1) * d; + ans = (N * (S + E)) / 2; + cout << "Case " << counter++ << ": " << ans << endl; + } + return 0; +} diff --git a/108.cpp b/108.cpp index 4f4ceaa..137cb45 100644 --- a/108.cpp +++ b/108.cpp @@ -1,80 +1,55 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -#define INF INT_MAX - -int maxSum(int arr[], int N){ - int gVal = -INF; - int temp = 0; - int i; - FOI(i, 0, N){ - temp += arr[i]; - if(temp > gVal) - gVal = temp; - if(temp < 0) - temp = 0; - } - return gVal; -} - -int main(){ - int N, i, j, k; - cin>>N; - int mat[N][N]; - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - cin>>mat[i][j]; - if(i > 0) - mat[i][j] += mat[i-1][j]; - } - } - int gSum = -INF; - FOI(i, 0, N-1){ - FOI(j, i, N-1){ - int vec[N]; - FOI(k, 0, N-1){ - if(i > 0) - vec[k] = mat[j][k] - mat[i-1][k]; - else - vec[k] = mat[i][k]; - } - int mSum = maxSum(vec, N-1); - gSum = max(mSum, gSum); - } - } - cout<>N; - return 0; -} +#include + +using namespace std; + +int size, mat[100][100], record[100][100]; + +inline void Input() +{ + int i, j; + for (i = 0; i < size; i++) + { + for (j = 0; j < size; j++) + { + scanf("%d", &mat[i][j]); + } + } + memset(record, 0, sizeof(record)); +} + +inline void solve() +{ + int max = -10000, row, col, loop, sum, lsum; + for (row = 0; row < size; row++) + { + for (col = 0; col < size; col++) + { + sum = 0; + for (loop = col; loop < size; loop++) + { + sum += mat[row][loop]; + lsum = sum + record[col][loop]; + if (lsum > max) + { + max = lsum; + } + if (lsum < 0) + { + lsum = 0; + } + record[col][loop] = lsum; + } + } + } + printf("%d\n", max); +} + +int main() +{ + while (scanf("%d", &size) != EOF) + { + Input(); + solve(); + } + return 0; +} diff --git a/1080.cpp b/1080.cpp new file mode 100644 index 0000000..c8281cb --- /dev/null +++ b/1080.cpp @@ -0,0 +1,489 @@ +#include + +using namespace std; + +//Type Definition +typedef long long ll; +typedef pair pii; +typedef vector vi; +typedef vector vvi; +typedef vector vs; +typedef map msi; +typedef map mii; + +#define ERR 1e-9 +#define PI 3.141592653589793 + +#define REP(i, n) for (i = 0; i < n; i++) +#define FOR(i, p, k) for (i = p; i < k; i++) +#define FOREACH(it, x) for (__typeof((x).begin()) it = (x.begin()); it != (x).end(); ++it) + +#define Sort(x) sort(x.begin(), x.end()) +#define Reverse(x) reverse(x.begin(), x.end()) +#define MP(a, b) make_pair(a, b) +#define Clear(x, with) memset(x, with, sizeof(x)) +#define SZ(x) (int)x.size() +#define pb push_back +#define popcount(i) __builtin_popcountll(i) +#define gcd(a, b) __gcd(a, b) +#define lcm(a, b) ((a) * ((b) / gcd(a, b))) +#define two(X) (1 << (X)) +#define twoL(X) (((ll)(1)) << (X)) +#define setBit(mask, i) (mask | two(i)) +#define contain(S, X) (((S)&two(X)) != 0) +#define fs first +#define sc second +#define CS c_str() +#define EQ(a, b) (fabs((a) - (b)) < ERR) +#define Unique(store) store.resize(unique(store.begin(), store.end()) - store.begin()); +#define Find(x, y) ((int)x.find(y)) +#define all(a) a.begin(), a.end() +#define rall(a) a.rbegin(), a.rend() + +//For debugging +#define debug_array(a, n) \ + for (ll i = 0; i < n; i++) \ + cerr << a[i] << " "; \ + cerr << endl; +#define debug_matrix(mat, row, col) \ + for (ll i = 0; i < row; i++) \ + { \ + for (ll j = 0; j < col; j++) \ + cerr << mat[i][j] << " "; \ + cerr << endl; \ + } +#define deb(a) cout << #a << " #-> " << a << endl +#define debug(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << "\t"; + return *this; + } +} dbg; + +//Important Functions +template +void setmax(T &a, T b) +{ + if (a < b) + { + a = b; + } +} +template +void setmin(T &a, T b) +{ + if (b < a) + { + a = b; + } +} +template +T Abs(T x) +{ + return x > 0 ? x : -x; +} +template +inline T sqr(T x) +{ + return x * x; +} +template +inline T Mod(T n, T m) +{ + return (n % m + m) % m;//For Positive Negative No. +} +template +string toString(T n) +{ + ostringstream oss; + oss << n; + oss.flush(); + return oss.str(); +} +ll toInt(string s) +{ + ll r = 0; + istringstream sin(s); + sin >> r; + return r; +} +bool isVowel(char ch) +{ + ch = tolower(ch); + if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') + { + return true; + } + return false; +} +bool isUpper(char c) +{ + return c >= 'A' && c <= 'Z'; +} +bool isLower(char c) +{ + return c >= 'a' && c <= 'z'; +} +ll Pow(ll B, ll P) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B); + } + P /= 2; + B = (B * B); + } + return R; +} +ll BigMod(ll B, ll P, ll M) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B) % M; + } + P /= 2; + B = (B * B) % M; + } + return R; +} +void binprint(ll mask, ll n) +{ + ll i; + string s = ""; + do + { + s += (mask % 2 + '0'); + mask /= 2; + } while (mask); + Reverse(s); + s = string(max(n - SZ(s), 0LL), '0') + s; + for (i = SZ(s) - n; i < SZ(s); i++) + { + printf("%c", s[i]); + } + printf("\n"); +} +void ASCII_Chart() +{ + ll i, j; + int k; + printf("ASCII Chart:(30-129)\n"); + FOR(i, 30, 50) + { + REP(j, 5) + { + k = i + j * 20; + printf("%3d---> '%c' ", k, k); + } + printf("\n"); + } +} + +//int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction + +//#include //for using getch(); + +//struct edge{ int cost,node; edge(int _cost=0,int _node=0){cost=_cost;node=_node;}bool operator<(const edge &b)const {return cost>b.cost;}}; // Min Priority Queue +//bool comp(edge a,edge b){ return a.cost < b.cost;} //Asc Sort by cost + +int N, G, U, nodes; +vector adj[110], store; +int color[110]; +int dp[1 << 8][21][4]; + +struct Node +{ + char type; + vector in; + int out; +}; + +Node Ele[100]; + +void dfs(int u) +{ + color[u] = 1; + int i, v; + REP(i, SZ(adj[u])) + { + v = adj[u][i]; + if (color[v] == 0) + { + dfs(v); + } + } + store.pb(u); +} + +vector Topological(int nodes) +{ + int i; + Clear(color, 0); + store.clear(); + for (i = 1; i <= nodes; i++) + { + if (color[i] == 0) + { + dfs(i); + } + } + Reverse(store); + // for(i=0;i + +void Simulate(int mask, int gateN, int type) +{ + int i, j, v; + for (i = 0; i <= nodes; i++) + { + Ele[i].in.clear(); + } + for (i = 0; i < N; i++) + if (contain(mask, i)) + { + Ele[i + 1].out = 1; + } + else + { + Ele[i + 1].out = 0; + } + for (i = 0; i < SZ(store); i++) + { + Node u = Ele[store[i]]; + if (u.type == 'a') + { + u.out = (u.in[0] & u.in[1]); + } + else if (u.type == 'o') + { + u.out = (u.in[0] | u.in[1]); + } + else if (u.type == 'x') + { + u.out = (u.in[0] ^ u.in[1]); + } + else if (u.type == 'n') + { + u.out = !(u.in[0]); + } + if (gateN && (u.type == 'a' || u.type == 'x' || u.type == 'n' || u.type == 'o') && gateN + N == store[i]) + { + if (type == 1) + { + u.out = !u.out; + } + else if (type == 2) + { + u.out = 0; + } + else + { + u.out = 1; + } + } + for (j = 0; j < SZ(adj[store[i]]); j++) + { + v = adj[store[i]][j]; + Ele[v].in.pb(u.out); + // debug(store[i],v,u.out); + // getch(); + } + } + int outMask = 0; + for (i = N + G + 1, j = 0; i <= nodes; i++, j++) + { + if (Ele[i].in[0]) + { + outMask |= two(j); + } + } + dp[mask][gateN][type] = outMask; +} + +int main() +{ + // freopen("B.in","r",stdin); + //freopen("out.out","w",stdout); + int i, j, test, Case = 1, n, x, y, num, ind; + char ch; + char temp[110]; + string str; + // scanf("%d",&test); + while (scanf("%d %d %d ", &N, &G, &U) == 3) + { + // debug(N,G,U); + if (!N && !G && !U) + { + break; + } + REP(i, 100) + adj[i].clear(); + for (i = 1; i <= 100; i++) + { + Ele[i].in.clear(); + } + for (i = 1; i <= N; i++) + { + Ele[i].type = 'i'; + } + for (i = 1; i <= G; i++) + { + //ith gate + gets(temp); + str = temp; + stringstream iss(str); + while (iss >> str) + { + if (SZ(str) == 1) + { + Ele[i + N].type = str[0]; + } + else + { + ch = str[0]; + num = toInt(str.substr(1)); + if (ch == 'g') + { + num += N; + } + adj[num].pb(i + N); + } + } + } + for (i = 1; i <= U; i++) + { + Ele[i + N + G].type = 'u'; + scanf("%d", &ind); + adj[N + ind].pb(i + N + G); + } + nodes = N + G + U; + // for(i=1;i<=nodes;i++) + // debug("i: ",i,Ele[i].type); + // debug(N,G,U); + // debug("Complete\n\n"); + Topological(nodes); + // debug("Came Here:") + int mask, type, g; + for (mask = 0; mask < two(N); mask++) + { + Simulate(mask, 0, 0); + // debug(mask,dp[mask][0][0]); + for (type = 1; type <= 3; type++) + { + for (g = 1; g <= G; g++) + { + Simulate(mask, g, type); + } + } + } + int B, val, outMask; + scanf("%d", &B); + vector obs; + for (i = 1; i <= B; i++) + { + mask = 0; + for (j = 1; j <= N; j++) + { + scanf("%d", &val); + if (val) + { + mask |= two(j - 1); + } + } + outMask = 0; + for (j = 1; j <= U; j++) + { + scanf("%d", &val); + if (val) + { + outMask |= two(j - 1); + } + } + obs.pb(MP(mask, outMask)); + } + // debug("complete"); + bool check = true; + for (i = 0; i < SZ(obs); i++) + { + val = dp[obs[i].fs][0][0]; + if (val != obs[i].sc) + { + check = false; + break; + } + } + if (check) + { + printf("Case %d: No faults detected\n", Case++); + continue; + } + int cnt = 0, ansG, ansT; + for (type = 1; type <= 3; type++) + { + for (g = 1; g <= G; g++) + { + bool check = true; + for (i = 0; i < SZ(obs); i++) + { + val = dp[obs[i].fs][g][type]; + if (val != obs[i].sc) + { + check = false; + break; + } + } + if (check) + { + cnt++; + ansG = g; + ansT = type; + } + } + } + printf("Case %d: ", Case++); + if (cnt == 1) + { + if (ansT == 1) + { + printf("Gate %d is failing; output inverted\n", ansG); + } + else if (ansT == 2) + { + printf("Gate %d is failing; output stuck at 0\n", ansG); + } + else + { + printf("Gate %d is failing; output stuck at 1\n", ansG); + } + } + else + { + printf("Unable to totally classify the failure\n"); + } + } + return 0; +} diff --git a/10800.cpp b/10800.cpp index 032f54d..39499ff 100644 --- a/10800.cpp +++ b/10800.cpp @@ -1,43 +1,73 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - cin >> T; - FOI(t, 1, T) { - cout << "Case #" << t << ":\n" - string S; - cin >> S; - +int main() +{ + char c[50]; + int t, x[50], m, n, l, p; + scanf("%d", &t); + for (int i = 1; i <= t; i++) + { + scanf("%s", &c); + x[0] = 0; + m = 0, n = 0; + for (l = 1; c[l] != '\0'; l++) + { + x[l] = x[l - 1]; + if (c[l - 1] == 'R') + { + x[l]++; + } + if (c[l] == 'F') + { + x[l]--; + } + m = (m > x[l]) ? m : x[l]; + n = (n < x[l]) ? n : x[l]; + } + printf("Case #%d:\n", i); + for (int j = m; j >= n; j--) + { + printf("| "); + p = 0; + for (int k = 0; k < l; k++) + { + if (x[k] == j) + { + p = k; + } + } + for (int k = 0; k <= p; k++) + { + if (x[k] == j) + { + if (c[k] == 'R') + { + printf("/"); + } + else if (c[k] == 'F') + { + printf("\\"); + } + else + { + printf("_"); + } + } + else + { + printf(" "); + } + } + printf("\n"); + } + printf("+"); + for (int j = 0; j < l + 2; j++) + { + printf("-"); + } + printf("\n\n"); } return 0; } diff --git a/10801.cpp b/10801.cpp new file mode 100644 index 0000000..d381af6 --- /dev/null +++ b/10801.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +#define MAXN 102 +#define INF 1000000000 + +struct ss +{ + int list[MAXN]; + int ind; + int time; +}; +struct node +{ + int r, c; +}; +ss Lift[7]; +queue Q; +char F[7][MAXN], H[7][MAXN], str[1000]; +int C[7][MAXN], Ad[7][MAXN], L, T, R; + +void ReadCase() +{ + int i = 1, k, min = 10000; + char *p; + p = strtok(str, " "); + while (p) + { + Lift[i++].time = atoi(p); + p = strtok(NULL, " "); + if (Lift[i - 1].time < min) + { + min = Lift[i - 1].time; + R = i - 1; + } + } + for (i = 1; i <= L; i++) + { + Lift[i].ind = 0; + gets(str); + p = strtok(str, " "); + while (p) + { + k = atoi(p); + Lift[i].list[Lift[i].ind++] = k; + H[i][k] = 1; + C[i][k] = INF; + Ad[i][k] = Lift[i].ind - 1; + p = strtok(NULL, " "); + } + } +} + +void Back(int r, int c) +{ + int d, m, n, k; + node x; + if (c <= 0) + { + return; + } + d = abs(Lift[r].list[c - 1] - Lift[r].list[c]); + n = Lift[r].list[c]; + m = Lift[r].list[c - 1]; + k = d * Lift[r].time + C[r][n]; + if (C[r][m] > k) + { + C[r][m] = k; + x.r = r; + x.c = c - 1; + if (F[r][m] == 0) + { + Q.push(x); + F[r][m] = 1; + } + Q.push(x); + } +} + +void Front(int r, int c) +{ + int d, m, n, k; + node x; + if (c >= Lift[r].ind - 1) + { + return; + } + d = abs(Lift[r].list[c + 1] - Lift[r].list[c]); + n = Lift[r].list[c]; + m = Lift[r].list[c + 1]; + k = d * Lift[r].time + C[r][n]; + if (C[r][m] > k) + { + C[r][m] = k; + x.r = r; + x.c = c + 1; + if (F[r][m] == 0) + { + Q.push(x); + F[r][m] = 1; + } + } +} + +void BFS() +{ + node temp; + int i, j, k, min = INF, r, c, m; + for (i = 1; i <= L; i++) + { + if (H[i][0] == 1) + { + temp.r = i; + temp.c = 0; + C[i][0] = 0; + Q.push(temp); + F[i][0] = 1; + } + } + while (!Q.empty()) + { + temp = Q.front(); + Q.pop(); + r = temp.r; + c = temp.c; + m = Lift[r].list[c]; + F[r][m] = 0; + Back(r, c); + Front(r, c); + for (j = 1; j <= L; j++) + { + if (H[j][m] == 0) + { + continue; + } + k = C[r][m] + 60; + if (C[j][m] > k) + { + C[j][m] = k; + k = Ad[j][m]; + if (F[j][m] == 0) + { + F[j][m] = 1; + temp.r = j; + temp.c = k; + Q.push(temp); + } + } + } + } +} + +int BestCase() +{ + if (H[R][0] == 1 && H[R][T] == 1) + { + return 1; + } + return 0; +} + +int Impossible() +{ + int i; + if (R == INF) + { + return 1; + } + for (i = 1; i <= L; i++) + { + if (H[i][T] == 1) + { + return 0; + } + } + return 1; +} + +void Cal() +{ + int i, min = INF; + if (Impossible()) + { + printf("IMPOSSIBLE\n"); + return; + } + if (BestCase()) + { + printf("%d\n", Lift[R].time * T); + return; + } + BFS(); + for (i = 1; i <= L; i++) + { + if (H[i][T] == 0) + { + continue; + } + if (C[i][T] < min) + { + min = C[i][T]; + } + } + if (min == INF) + { + puts("IMPOSSIBLE"); + } + else + { + printf("%d\n", min); + } +} + +void Free() +{ + int i, j, k; + for (i = 1; i <= L; i++) + { + for (j = 0; j < Lift[i].ind; j++) + { + k = Lift[i].list[j]; + H[i][k] = 0; + F[i][k] = 0; + } + } +} + +int Emt() +{ + for (int i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = '\0'; + break; + } + } + if (strlen(str) == 0) + { + return 1; + } + return 0; +} + +int main() +{ + while (gets(str)) + { + if (Emt()) + { + continue; + } + sscanf(str, "%d%d", &L, &T); + gets(str); + R = INF; + ReadCase(); + Cal(); + Free(); + } + return 0; +} diff --git a/10802.cpp b/10802.cpp new file mode 100644 index 0000000..6fe41f7 --- /dev/null +++ b/10802.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +bool cango[100][100]; +int visited[100]; +int path[100]; +vector Edge[100]; + +void make_edge(int n) +{ + rep(k, n) + rep(i, n) + rep(j, n) cango[i][j] = (cango[i][j] | (cango[i][k] & cango[k][j])); +} + +bool solve(int now, int n, int tar, int p, int prev) +{ + path[p++] = now; + if (now == tar) + { + rep(i, p) + { + if (i) + { + cout << ' '; + } + cout << path[i]; + } + cout << endl; + return true; + } + // if ( cango[now][tar] == false)return false; + visited[now]++; + rep(i, Edge[now].size()) + { + int next = Edge[now][i]; + if (next == prev) + { + continue; + } + if (visited[next] > 0)//if find finite loop + { + /* + if(next > tar){ + if ( visited[i]==2 )continue; + } + else { + puts("No drive."); + visited[now]--; + return true; + } + */ + if (visited[i] == 2) + { + puts("No drive."); + visited[now]--; + return true; + } + } + if (solve(next, n, tar, p, now)) + { + visited[now]--; + return true; + } + } + visited[now]--; + return false; +} + +main() +{ + int te, tc = 1; + cin >> te; + while (te--) + { + int n, m, s; + cin >> n >> m >> s; + rep(i, n) + { + rep(j, n) cango[i][j] = false; + cango[i][i] = true; + Edge[i].clear(); + visited[i] = 0; + } + rep(i, m) + { + int f, t; + cin >> f >> t; + cango[f][t] = cango[t][f] = true; + Edge[f].push_back(t); + Edge[t].push_back(f); + } + rep(i, n) sort(Edge[i].begin(), Edge[i].end()); + make_edge(n); + cout << "Case #" << tc++ << ":" << endl; + rep(i, n) + { + if (cango[s][i] == false || solve(s, n, i, 0, -1) == false) + { + puts("No drive."); + } + } + puts(""); + } + return false; +} diff --git a/10803.cpp b/10803.cpp index 2373f15..d7e0729 100644 --- a/10803.cpp +++ b/10803.cpp @@ -1,80 +1,86 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -struct City{ - double X, Y; -}; -int main(){ - int test; - cin>>test; - for(int t=1; t<=test; t++){ - int N; - cin>>N; - int i, j, k; - City city[N]; - for(i=0; i>city[i].X>>city[i].Y; - double mat[N][N]; - for(i=0; i= 900000000) - printf("Send Kurdy\n\n"); - else - printf("%.4lf\n\n", maxm); - } - return 0; -} +#include + +using namespace std; + +double distance_between(int a, int b, const vector> &towns) +{ + return sqrt((towns[a].first - towns[b].first) * (towns[a].first - towns[b].first) + (towns[a].second - towns[b].second) * (towns[a].second - towns[b].second)); +} + +int main() +{ + int cases, n, x, y; + vector> towns; + double dist[101][101]; + double INF = 1e20; + cin >> cases; + for (int casenum = 0; casenum < cases; casenum++) + { + cin >> n; + towns.clear(); + for (int i = 0; i < n; i++) + { + cin >> x >> y; + towns.push_back(make_pair(x, y)); + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + dist[i][j] = 0; + continue; + } + double tmp = distance_between(i, j, towns); + if (tmp > 10.0) + { + dist[i][j] = INF; + } + else + { + dist[i][j] = tmp; + } + } + } + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); + } + } + } + bool ok = true; + double result = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (dist[i][j] == INF) + { + ok = false; + i = n; + break; + } + if (dist[i][j] > result) + { + result = dist[i][j]; + } + } + } + cout << "Case #" << casenum + 1 << ":" << endl; + if (ok) + { + cout << setprecision(4) << setiosflags(ios::fixed | ios::showpoint) << result << endl; + } + else + { + cout << "Send Kurdy" << endl; + } + cout << endl; + } + return 0; +} diff --git a/10804.cpp b/10804.cpp new file mode 100644 index 0000000..0e7243d --- /dev/null +++ b/10804.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +double w[50][50], weight[2500], a[50][2], b[50][2]; +int cy[50], p[50]; +int n, m, k, len; +int compare(const void *a, const void *b) +{ + double *c = (double *)a, *d = (double *)b; + if (*c < *d) + { + return -1; + } + else if (*c > *d) + { + return 1; + } + return 0; +} +bool find(int m, double bound) +{ + for (int i = 0; i < n; i++) + if (w[m][i] <= bound && !cy[i]) + { + cy[i] = true; + if (p[i] == -1 || find(p[i], bound)) + { + p[i] = m; + return true; + } + } + return false; +} +bool matching(double bound) +{ + int count = 0; + for (int i = 0; i < n; i++) + { + p[i] = -1; + } + for (int i = 0; i < m; i++) + { + memset(cy, 0, sizeof(cy)); + if (find(i, bound)) + { + count++; + } + } + return count >= m - k; +} +int main() +{ + int times; + scanf("%d", ×); + for (int c = 1; c <= times; c++) + { + printf("Case #%d:\n", c); + scanf("%d %d %d", &m, &n, &k); + for (int i = 0; i < m; i++) + { + scanf("%lf %lf", &a[i][0], &a[i][1]); + } + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &b[i][0], &b[i][1]); + } + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + { + double t = sqrt((a[i][0] - b[j][0]) * (a[i][0] - b[j][0]) + (a[i][1] - b[j][1]) * (a[i][1] - b[j][1])); + w[i][j] = weight[i * n + j] = t; + } + qsort(weight, m * n, sizeof(double), compare); + int p, q; + len = 0; + for (p = 0; p < m * n; p = q) + { + weight[len++] = weight[p]; + for (q = p + 1; q < m * n && weight[p] == weight[q]; q++) + ; + } + if (!matching(weight[len - 1])) + { + puts("Too bad."); + } + else + { + int left = 0, right = len - 1, mid = (left + right) / 2; + double best = weight[len - 1]; + while (left <= right) + { + if (matching(weight[mid])) + { + best = weight[mid]; + right = mid - 1; + } + else + { + left = mid + 1; + } + mid = (left + right) / 2; + } + printf("%.3lf\n", best); + } + printf("\n"); + } +} diff --git a/10805.cpp b/10805.cpp new file mode 100644 index 0000000..98dd9ee --- /dev/null +++ b/10805.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +static int adj[32][32], n; +static int bfs1[32]; + +static int bfs2(int u1, int u2) +{ + static int v[32], q[32], m[32][32], d[32][32]; + register int k, i, j, t; + memset(v, 0, sizeof(v)); + memset(m, 0, sizeof(m)); + k = 0; + q[k++] = u1; + v[u1] = 1; + if (u2 != u1) + { + q[k++] = u2; + v[u2] = 1; + m[u1][u2] = m[u2][u1] = 1; + } + for (j = 0; j < k; j++) + { + t = q[j]; + for (i = 0; i < n; i++) + { + if (adj[t][i] && v[i] == 0) + { + v[i] = 1; + q[k++] = i; + m[t][i] = m[i][t] = 1; + } + } + } + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + d[i][j] = (i == j) ? 0 : (m[i][j] ? 1 : 255); + } + for (k = 0; k < n; k++) + { + for (i = 0; i < n; i++) + { + if (d[i][k] >= 255) + { + continue; + } + for (j = 0; j < i; j++) + { + t = d[i][k] + d[k][j]; + if (t < d[i][j]) + { + d[i][j] = d[j][i] = t; + } + } + } + } + for (k = 0, i = 0; i < n; i++) + for (j = 0; j < n; j++) + if (d[i][j] > k) + { + k = d[i][j]; + } + return k; +} + +static int solve() +{ + int i, j, k, t; + for (i = 0; i < n; i++) + { + bfs1[i] = bfs2(i, i); + } + for (i = 1, k = bfs1[0]; i < n; i++) + if (bfs1[i] < k) + { + k = bfs1[i]; + } + if (k <= 3 || (k & 1)) + { + return k; + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (i == j || adj[i][j] == 0) + { + continue; + } + t = bfs2(i, j); + if (t < k) + { + k = t; + } + if (k & 1) + { + return k; + } + } + } + return k; +} + +int main() +{ + static int a, b, c, t, m; + scanf("%d", &c); + for (t = 1; t <= c && scanf("%d %d", &n, &m) == 2; t++) + { + memset(adj, 0, sizeof(adj)); + while (m-- > 0 && scanf("%d %d", &a, &b) == 2) + { + adj[a][b] = adj[b][a] = 1; + } + printf("Case #%d:\n%d\n\n", t, solve()); + } + return 0; +} diff --git a/10806.cpp b/10806.cpp new file mode 100644 index 0000000..abbc9f4 --- /dev/null +++ b/10806.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +/* +10806 +Dijkstra Dijkstra +*/ +#define MAXN 102 +#define INF 999999999 +int Cost[MAXN]; +int P[MAXN], N, E; +int V[MAXN][MAXN]; +struct ss +{ + int u, v; +}; +ss edge[10000]; +void Ini() +{ + int i; + for (i = 1; i <= N; i++) + { + Cost[i] = INF; + } + P[0] = -1; + Cost[1] = 0; +} +void BellMan() +{ + int n, m = N, u, v; + while (m--) + { + for (int i = 0; i < E; i++) + { + u = edge[i].u; + v = edge[i].v; + n = Cost[u] + V[u][v]; + if (Cost[v] > n) + { + Cost[v] = n; + P[v] = u; + } + n = Cost[v] + V[v][u]; + if (Cost[u] > n) + { + Cost[u] = n; + P[u] = v; + } + } + } +} +int Path[MAXN], tp; +void Getpath(int n) +{ + if (n == 1) + { + Path[tp++] = n; + return; + } + Getpath(P[n]); + Path[tp++] = n; +} +void ReSet() +{ + int i, u, v; + for (i = 0; i + 1 < tp; i++) + { + u = Path[i]; + v = Path[i + 1]; + V[u][v] = INF; + V[v][u] = -V[v][u]; + } +} +void Cal() +{ + int c1; + tp = 0; + BellMan(); + c1 = Cost[N]; + Getpath(N); + ReSet(); + Ini(); + BellMan(); + c1 += Cost[N]; + if (c1 >= INF) + { + puts("Back to jail"); + } + else + { + printf("%d\n", c1); + } +} +int main() +{ + int c; + while (scanf("%d%d", &N, &E) == 2) + { + if (!N) + { + break; + } + Ini(); + for (int i = 0; i < E; i++) + { + scanf("%d%d%d", &edge[i].u, &edge[i].v, &c); + V[edge[i].u][edge[i].v] = c; + V[edge[i].v][edge[i].u] = c; + } + Cal(); + } + return 0; +} diff --git a/10807.cpp b/10807.cpp new file mode 100644 index 0000000..754ea1a --- /dev/null +++ b/10807.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +#define INF 999999999 + +struct edge {int a, b, c; bool v;} e[25]; +int p[11], p2[11], n, m, min_; + +int cmp(const void *a, const void *b) +{ + edge *c = (edge *)a, *d = (edge *)b; + return c->c - d->c; +} + +int find(int x) +{ + while (p[x] > 0) + { + x = p[x]; + } + return x; +} + +int find2(int x) +{ + while (p2[x] > 0) + { + x = p2[x]; + } + return x; +} + +void union_(int x, int y) +{ + if (p[x] > p[y]) + { + p[y] += p[x]; + p[x] = y; + } + else + { + p[x] += p[y]; + p[y] = x; + } +} + +void union_2(int x, int y) +{ + if (p2[x] > p2[y]) + { + p2[y] += p2[x]; + p2[x] = y; + } + else + { + p2[x] += p2[y]; + p2[y] = x; + } +} + +int mst(int bound) +{ + for (int i = 1; i <= n; i++) + { + p2[i] = -1; + } + int count = 0, sum = 0; + for (int i = 0; i < m && count < n - 1; i++) + { + if (e[i].v) + { + continue; + } + int pa = find2(e[i].a), pb = find2(e[i].b); + if (pa != pb) + { + union_2(pa, pb); + count++; + sum += e[i].c; + if (sum >= bound) + { + return INF; + } + } + } + return (count == n - 1) ? sum : INF; +} + +void dfs(int choose, int from, int weight) +{ + if (choose == n - 1) + { + if (weight < min_) + { + int second = mst(min_ - weight); + if (weight + second < min_) + { + min_ = weight + second; + } + } + } + else if (choose + m - from >= n - 1) + { + if (weight + e[from].c * (n - 1 - m + from) >= min_) + { + return; + } + int tpa = find(e[from].a), tpb = find(e[from].b); + if (tpa != tpb) + { + int a = p[tpa], b = p[tpb]; + union_(tpa, tpb); + e[from].v = true; + dfs(choose + 1, from + 1, weight + e[from].c); + p[tpa] = a; + p[tpb] = b; + e[from].v = false; + } + if (weight < min_) + { + dfs(choose, from + 1, weight); + } + } +} + +int main() +{ + while (scanf("%d", &n), n) + { + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].c); + e[i].v = false; + } + qsort(e, m, sizeof(edge), cmp); + min_ = INF; + for (int i = 1; i <= n; i++) + { + p[i] = -1; + } + dfs(0, 0, 0); + if (min_ != INF) + { + printf("%d\n", min_); + } + else + { + puts("No way!"); + } + } + return 0; +} diff --git a/10808.cpp b/10808.cpp new file mode 100644 index 0000000..c0047e0 --- /dev/null +++ b/10808.cpp @@ -0,0 +1,309 @@ +#include + +using namespace std; + +struct Frac +{ + long long p, q; + + inline void reduce() + { + if (q < 0) + { + p = -p; + q = -q; + } + if (q == 0) + { + p = 1; + return; + } + long long g = __gcd(p, q); + p /= g; + q /= g; + } + + Frac(long long p = 0, long long q = 1) : p(p), q(q) + { + reduce(); + } + + Frac operator*(const Frac &b) const + { + return Frac(p * b.p, q * b.q); + } + Frac operator/(const Frac &b) const + { + return Frac(p * b.q, q * b.p); + } + Frac operator+(const Frac &b) const + { + return Frac(p * b.q + b.p * q, q * b.q); + } + Frac operator-(const Frac &b) const + { + return Frac(p * b.q - b.p * q, q * b.q); + } + + void operator/=(const Frac &b) + { + p *= b.q; + q *= b.p; + reduce(); + } + void operator-=(const Frac &b) + { + p = p * b.q - b.p * q; + q *= b.q; + reduce(); + } + + bool operator==(const Frac &b) const + { + return p * b.q == q * b.p; + } + bool isZero() const + { + return p == 0 && q != 0; + } + bool isInf() const + { + return p != 0 && q == 0; + } +}; + +Frac abs(const Frac &a) +{ + return Frac(a.p < 0 ? -a.p : a.p, a.q); +} + +int dim; +Frac mat[140][140]; + +void gauss() +{ +#if 0 + printf("input matrix\n"); + for(int i = 0; i < dim; i++) + { + for(int j = 0; j <= dim; j++) + { + printf(mat[i][j].q==1?"%lld ":"%lld/%lld ", mat[i][j].p, mat[i][j].q); + } + printf("\n"); + } +#endif + for (int row = 0; row < dim; row++) + { + int bestRow = row; + for (int i = row; i < dim; i++) + { + if (mat[i][row].isZero()) + { + continue; + } + bestRow = i; + break; + } + if (mat[bestRow][row].isZero()) + { + //printf("matrix is singular\n"); + return; + } + if (bestRow != row) + { + for (int i = row; i <= dim; i++) + { + swap(mat[bestRow][i].p, mat[row][i].p); + swap(mat[bestRow][i].q, mat[row][i].q); + } + } + for (int i = dim; i > row; i--) + { + mat[row][i] /= mat[row][row]; + } + mat[row][row] = 1; + for (int r = row + 1; r < dim; r++) + { + if (!mat[r][row].isZero()) + { + for (int i = dim; i > row; i--) + { + mat[r][i] -= mat[r][row] * mat[row][i]; + } + mat[r][row] = 0; + } + } + } +#if 0 + printf("gauss: output\n"); + for(int i = 0; i < dim; i++) + { + for(int j = 0; j <= dim; j++) + { + printf(mat[i][j].q==1?"%lld ":"%lld/%lld ", mat[i][j].p, mat[i][j].q); + } + printf("\n"); + } +#endif +} + +int N; +Frac adj[32][32], INF(1, 0); + +void dfs(int x, int seen[]) +{ + if (seen[x] != -1) + { + return; + } + seen[x] = -2; + for (int y = 0; y < N; y++) + if (!adj[x][y].isInf()) + { + dfs(y, seen); + } +} + +Frac solve(int s, int t) +{ + if (s == t) + { + return 0; + } + int id[32]; + memset(id, 0xff, sizeof(id)); + dfs(s, id); + if (id[t] == -1) + { + return INF; + } + id[s] = 0; + id[t] = 1; + int K = 2; + for (int i = 0; i < N; i++) + if (id[i] == -2) + { + id[i] = K++; + } + int edgeId[16][16]; + memset(edgeId, 0xff, sizeof(edgeId)); + int M = 0; + for (int x = 0; x < N; x++) + { + for (int y = x + 1; y < N; y++) + { + if (id[x] < 0 || id[y] < 0 || adj[x][y].isInf()) + { + continue; + } + edgeId[x][y] = edgeId[y][x] = M++; + } + } + dim = K + M + 1; + assert(dim <= 139); + for (int i = 0; i < dim; i++) + for (int j = 0; j <= dim; j++) + { + mat[i][j] = 0; + } + // K+M+1 vars (currents, potentials, goal) + // M+(K-2)+2 + 1 eq (ohm's; conservation of pot. - potentials of s and t, goal eq) + int eq = 0; + mat[eq][0] = 1; + eq++;// V0 = 0 + mat[eq][1] = 1; + mat[eq][dim] = 1; + eq++;// V1 = 1 + for (int x = 0; x < N; x++) + { + if (id[x] < 0) + { + continue; + } + for (int y = x + 1; y < N; y++) + { + if (id[y] < 0 || adj[x][y].isInf()) + { + continue; + } + // V[y] - V[x] = R*I[xy] + mat[eq][id[y]] = 1; + mat[eq][id[x]] = -1; + mat[eq][K + edgeId[x][y]] = Frac(0) - adj[x][y]; + eq++; + } + if (x != t) + { + // sum_y I(xy) = 0 for x!=s, x!=t + for (int y = 0; y < N; y++) + { + if (id[y] < 0 || x == y || adj[x][y].isInf()) + { + continue; + } + mat[eq][K + edgeId[x][y]] = (y < x ? -1 : 1); + } + if (x == s) + { + // sum_y I(sy) = voltage between s and t + mat[eq][K + M] = -1; + } + eq++; + } + } + assert(dim == eq); + gauss(); + Frac I = abs(mat[K + M][dim]); + if (I.isZero()) + { + return INF; + } + return Frac(1) / I; +} + +int main() +{ + int T, M; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d %d", &N, &M) == 2; cs++) + { + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + adj[i][j] = (i == j ? 0 : INF); + } + for (int i = 0; i < M; i++) + { + int x, y, r; + scanf("%d %d %d", &x, &y, &r); + if (adj[x][y].isInf()) + { + adj[y][x] = adj[x][y] = r; + } + else + { + adj[y][x] = adj[x][y] = (adj[x][y] * r) / (adj[x][y] + r); + } + } + printf("Case #%d:\n", cs); +#if 0 + for(int i = 0; i < N; i++) + for(int j = j+1; j < N; j++) + if(!adj[i][j].isInf()) + { + printf("%d-%d %lld/%lld\n", i, j, adj[i][j].p, adj[i][j].q); + } +#endif + int Q; + scanf("%d", &Q); + for (int i = 0; i < Q; i++) + { + int s, t; + scanf("%d %d", &s, &t); + Frac res = solve(s, t); + printf("Resistance between %d and %d is %lld/%lld\n", s, t, res.p, res.q); + } + printf("\n"); + } +} diff --git a/10809.cpp b/10809.cpp new file mode 100644 index 0000000..d872878 --- /dev/null +++ b/10809.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define y1 Y1 +#define _USE_MATH_DEFINES +double lat1, long1, lat2, long2, x1, y1, z1, x2, y2, z2; +double alpha, delta, x, y, z, norm, bestx, besty, bestz; +struct Vector +{ + double x, y, z; + Vector(){}; + Vector(double lat, double lon) + { + x = sin(lat * M_PI / 180); + y = sin(lon * M_PI / 180) * cos(lat * M_PI / 180); + z = cos(lon * M_PI / 180) * cos(lat * M_PI / 180); + } + Vector operator*(double scale) + { + Vector ans = *this; + ans.x *= scale; + ans.y *= scale; + ans.z *= scale; + return ans; + } + Vector operator+(Vector s) + { + Vector ans = *this; + ans.x += s.x; + ans.y += s.y; + ans.z += s.z; + return ans; + } + void Normalize() + { + double len = sqrt(x * x + y * y + z * z); + x /= len; + y /= len; + z /= len; + } +} a, b, c; +void run(double s) +{ + if (s < 0 || s > 1) + { + return; + } + c = a * s + b * (1 - s); + c.Normalize(); + if (c.x > bestx) + { + bestx = c.x; + besty = c.y; + bestz = c.z; + alpha = s; + } +} +int main() +{ + int i, j, k, m, n, N; + int deg, sec; + char dir; + scanf("%d", &N); + while (N--) + { + scanf("%d,%d%c", °, &sec, &dir); + lat1 = deg + sec / 60.0; + if (dir == 'S') + { + lat1 = -lat1; + } + scanf("%d,%d%c", °, &sec, &dir); + long1 = deg + sec / 60.0; + if (dir == 'W') + { + long1 = -long1; + } + scanf("%d,%d%c", °, &sec, &dir); + lat2 = deg + sec / 60.0; + if (dir == 'S') + { + lat2 = -lat2; + } + scanf("%d,%d%c", °, &sec, &dir); + long2 = deg + sec / 60.0; + if (dir == 'W') + { + long2 = -long2; + } + if (lat1 == -lat2 && fabs(long1 - long2) == 180) + { + printf("undefined\n"); + continue; + } + a = Vector(lat1, long1); + b = Vector(lat2, long2); + bestx = -2; + run(0); + run(.5); + run(1); + for (delta = .5; delta > .000000000001; delta *= .5) + { + run(alpha + delta); + run(alpha - delta); + } + if (bestx >= 0) + { + i = (int)floor(asin(bestx) * 180 / M_PI * 60 + .5); + printf("%d,%dN\n", i / 60, i % 60); + } + else + { + i = (int)floor(asin(-bestx) * 180 / M_PI * 60 + .5); + printf("%d,%dS\n", i / 60, i % 60); + } + } + return 0; +} diff --git a/1081.cpp b/1081.cpp new file mode 100644 index 0000000..aab9357 --- /dev/null +++ b/1081.cpp @@ -0,0 +1,245 @@ +#include + +using namespace std; + +typedef double dou; + +const dou eps = 1e-7; +const dou pi = acos(-1); + +int sgn(dou k) +{ + return (k > eps) - (k < -eps); +} + +typedef struct +{ + dou x, y, z; +} dot; +dot operator*(dou k, dot a) +{ + return (dot){ + k * a.x, k * a.y, k * a.z}; +} +dot operator-(dot a, dot b) +{ + return (dot){ + a.x - b.x, a.y - b.y, a.z - b.z}; +} +dot operator+(dot a, dot b) +{ + return (dot){ + a.x + b.x, a.y + b.y, a.z + b.z}; +} + +dou operator%(dot a, dot b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +dou dis(dot a) +{ + return sqrt(a % a); +} + +dot getd(int a, int b) +{ + dou ts = sin(b * 2 * pi / 360); + return (dot){ + cos(a * 2 * pi / 360) * ts, sin(a * 2 * pi / 360) * ts, cos(b * 2 * pi / 360)}; +} + +dot jiao(dot d) +{ + dot p = (dot){ + (dou)(sgn(d.x) >= 0 ? 1 : -1), (dou)(sgn(d.y) >= 0 ? 1 : -1), (dou)(sgn(d.z) >= 0 ? 1 : -1)}; + // printf("%f %f %f\n",p.x,p.y,p.z); + // printf("%f %f %f\n",d.x,d.y,d.z); + // t1(d)%p==1 + // t1(d%p)==1 + dou t1 = 1 / (d % p); + return t1 * d; +} + +dot rotate(dot d, int a) +{ + dou s = sin(a * 2 * pi / 360); + dou c = cos(a * 2 * pi / 360); + return (dot){ + d.x * c - d.y * s, d.x * s + d.y * c}; +} +// 1/sqrt(2)^2+1/2^2=1/2+1/4=3/4 +// sqrt(3)/2 +void produce(vector &s, dot a) +{ + // fprintf(stderr,"%f %f %f\n",a.x,a.y,a.z); + int dir = 0; + if (sgn(a.z) < 0) + { + dir += 4; + a.z *= -1; + } + + int cnt = 0; + for (; sgn(a.x) < 0 || sgn(a.y) < 0;) + { + swap(a.x, a.y); + a.x = -a.x; + cnt++; + } + // fprintf(stderr,"cnt=%d\n",cnt); + if (cnt == 0) + { + dir += 0; + } + if (cnt == 1) + { + dir += 2; + } + if (cnt == 2) + { + dir += 3; + } + if (cnt == 3) + { + dir += 1; + } + assert(cnt < 4); + + dou A = dis(a - (dot){ + 1, 0, 0}); + dou C = dis(a - (dot){ + 0, 1, 0}); + dou B = sqrt(2); + dou D = sgn(A) ? (A * A + B * B - C * C) / 2 / A / B : 1; + if (D > 1) + { + D = 1; + } + if (D < -1) + { + D = -1; + } + dou theta = acos(D); + dot O = (dot){ + 1.0 / sqrt(2), 1.0 / sqrt(6), 0}; + dot X = (dot){ + A * cos(theta), A * sin(theta), 0} - + O; + if (dir & 4) + { + X.x = -X.x; + } + // fprintf(stderr,"X=(%f,%f)\n",X.x,X.y); + /* + fprintf(stderr,"A=%f B=%f C=%f theta=%f\n",A,B,C,theta); + fprintf(stderr,"dir=%d\n",dir); + */ + s.clear(); + if (dir == 0) + { + s.push_back(X); + } + + else if (dir == 1) + { + s.push_back(rotate(X, +60) + (2 / sqrt(6)) * getd(+30, 90)); + } + else if (dir == 2) + { + s.push_back(rotate(X, 300) + (2 / sqrt(6)) * getd(150, 90)); + } + else if (dir == 4) + { + s.push_back(rotate(X, 180) + (2 / sqrt(6)) * getd(270, 90)); + } + + else if (dir == 3) + { + s.push_back(rotate(X, 120) + sqrt(2) * getd(+60, 90)); + s.push_back(rotate(X, 240) + sqrt(2) * getd(120, 90)); + } + else if (dir == 6) + { + s.push_back(rotate(X, 120) + sqrt(2) * getd(180, 90)); + s.push_back(rotate(X, 240) + sqrt(2) * getd(240, 90)); + } + else if (dir == 5) + { + s.push_back(rotate(X, 120) + sqrt(2) * getd(300, 90)); + s.push_back(rotate(X, 240) + sqrt(2) * getd(0 + 0, 90)); + } + + else if (dir == 7) + { + s.push_back(rotate(X, 180) + (4 / sqrt(6)) * getd(30, 90) + (2 / sqrt(6)) * getd(330, 90)); + s.push_back(rotate(X, 300) + (4 / sqrt(6)) * getd(30, 90) + (2 / sqrt(6)) * getd(90, 90)); + + s.push_back(rotate(X, 60) + (4 / sqrt(6)) * getd(150, 90) + (2 / sqrt(6)) * getd(90, 90)); + s.push_back(rotate(X, 180) + (4 / sqrt(6)) * getd(150, 90) + (2 / sqrt(6)) * getd(210, 90)); + + s.push_back(rotate(X, 300) + (4 / sqrt(6)) * getd(270, 90) + (2 / sqrt(6)) * getd(210, 90)); + s.push_back(rotate(X, 60) + (4 / sqrt(6)) * getd(270, 90) + (2 / sqrt(6)) * getd(330, 90)); + + } +} + +void remap(int &a, int &c) +{ + for (; a >= 90;) + { + a = (a + 90) % 360; + c = (c + 90) % 360; + } +} + +int a, b, c, d; +vector as, bs; +dou sol(int uuu) +{ + assert(0 <= a && a < 360); + assert(0 <= b && b <= 180); + assert(0 <= c && c < 360); + assert(0 <= d && d <= 180); + remap(a, c); + if (b > 90 || (b == 90 && d > 90)) + { + b = 180 - b; + d = 180 - d; + } + // fprintf(stderr,"%d %d %d %d\n",a,b,c,d); + assert(0 <= a && a < 90); + assert(0 <= b && b <= 90); + produce(as, jiao(getd(a, b))); + produce(bs, jiao(getd(c, d))); + dou ans = 7122; + /* + for(int i=0;i<(int)as.size();i++){ + printf("(%f %f)",as[i].x,as[i].y); + }puts(""); + for(int j=0;j<(int)bs.size();j++){ + printf("(%f %f)",bs[j].x,bs[j].y); + }puts(""); + */ + for (int i = 0; i < (int)as.size(); i++) + { + for (int j = 0; j < (int)bs.size(); j++) + { + ans = min(ans, dis(as[i] - bs[j])); + } + } + assert(sgn(ans - 7122)); + return ans; +} + +int check = 1; +char s[100]; + +int main() +{ + int t = 1; + while (scanf("%d %d %d %d", &a, &b, &c, &d) != EOF && (a + 1)) + { + printf("Case %d: %.3f\n", t++, sol(t) / sqrt(2) * 10.0); + } + return 0; +} diff --git a/10810.cpp b/10810.cpp index 88362b0..b3592d8 100644 --- a/10810.cpp +++ b/10810.cpp @@ -1,51 +1,74 @@ -#include - -int N; -#define MAXN 1000010 - -static int p[MAXN], Temp[MAXN]; -long long count; - -void Cal(int a, int b){ - int md, below, mdi, ni, i; - if (a >= b) return; - md = p[a+(b-a)/2]; - for (mdi=i=a;i md;mdi--){ - int tmp = Temp[mdi-1]; - Temp[mdi-1] = Temp[mdi]; - Temp[mdi] = tmp; - count++; - } - for (i=a;i + +using namespace std; + +#define MAXN 500002 + +int N, p[MAXN], Temp[MAXN]; +long long cnt; + +void Cal(int a, int b) +{ + int md, below, mdi, ni, i; + if (a >= b) + { + return; + } + md = p[a + (b - a) / 2]; + for (mdi = i = a; i < b; i++) + { + if (p[i] < md) + { + mdi++; + } + } + below = mdi - a; + for (ni = i = a; i < b; i++) + { + if (p[i] < md) + { + Temp[ni++] = p[i]; + below--; + } + else + { + Temp[mdi++] = p[i]; + cnt += below; + } + } + for (mdi = a; Temp[mdi] != md; mdi++) + ; + for (mdi; mdi && Temp[mdi - 1] > md; mdi--) + { + int tmp = Temp[mdi - 1]; + Temp[mdi - 1] = Temp[mdi]; + Temp[mdi] = tmp; + cnt++; + } + for (i = a; i < b; i++) + { + p[i] = Temp[i]; + } + Cal(a, mdi); + while (mdi < b && p[mdi] == md) + { + mdi++; + } + Cal(mdi, b); +} + +int main() +{ + int i; + while (scanf("%d", &N) && N) + { + for (i = 0; i < N; i++) + { + scanf("%d", &p[i]); + } + cnt = 0; + Cal(0, N); + printf("%lld\n", cnt); + } + return 0; +} diff --git a/10811.cpp b/10811.cpp new file mode 100644 index 0000000..29bddf5 --- /dev/null +++ b/10811.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define MAX_MONEY 20000 + +double prob[2][MAX_MONEY][10], probWin; +int min_[31][10], max_[31][10], n, k, l, m; + +void play(int round) +{ + for (int i = 0; i < 10; i++) + { + min_[round][i] = min_[round - 1][i]; + } + for (int i = 0; i < 10; i++) + { + max_[round][i] = max_[round - 1][i]; + } + int n = round & 1, p = 1 - n; + memset(prob[n], 0, sizeof(prob[n])); + for (int bet = 0; (1 << bet) <= l; bet++) + { + int from = min_[round - 1][bet], to = max_[round - 1][bet], b = (1 << bet); + for (int i = from; i < to; i++) + { + prob[n][i + 3 * b][0] += 1.0 / 14 * prob[p][i][bet]; + } + for (int i = from; i < to; i++) + { + prob[n][i + 2 * b][0] += 1.0 / 14 * prob[p][i][bet]; + } + for (int i = from; i < to; i++) + { + prob[n][i + 1 * b][0] += 2.0 / 14 * prob[p][i][bet]; + } + if (from + 3 * b < min_[round][0]) + { + min_[round][0] = from + 3 * b; + } + if (to + 3 * b > max_[round][0]) + { + max_[round][0] = to + 3 * b; + } + // consider we lose: 10/14 + if (2 * b > l) + { + // if house limit is reached, go back to 1 dollar + for (int i = from; i < to; i++) + { + prob[n][i - b][0] += 10.0 / 14 * prob[p][i][bet]; + } + if (from - b < min_[round][0]) + { + min_[round][0] = from - b; + } + if (to - b > max_[round][0]) + { + max_[round][0] = to - b; + } + } + else + { + // else: double! + for (int i = from; i < to; i++) + { + prob[n][i - b][bet + 1] += 10.0 / 14 * prob[p][i][bet]; + } + if (from - b < min_[round][bet + 1]) + { + min_[round][bet + 1] = from - b; + } + if (to - b > max_[round][bet + 1]) + { + max_[round][bet + 1] = to - b; + } + } + } + if (round >= k) + { + for (int i = MAX_MONEY / 2 + 1; i < MAX_MONEY; i++) + { + for (int j = 0; (1 << j) <= l; j++) + { + probWin += prob[n][i][j]; + prob[n][i][j] = 0; + } + } + } +} + +int main() +{ + scanf("%i", &n); + while (--n >= 0) + { + scanf("%i%i%i", &k, &m, &l); + memset(prob, 0, sizeof(prob)); + for (int i = 0; i < 10; i++) + { + min_[0][i] = 999999999; + max_[0][i] = -99999999; + } + prob[0][MAX_MONEY / 2][0] = 1.0; + min_[0][0] = MAX_MONEY / 2; + max_[0][0] = MAX_MONEY / 2 + 1; + probWin = 0.0; + for (int i = 1; i <= m; i++) + { + play(i); + } + printf("%.4lf\n", probWin); + } + return 0; +} diff --git a/10812.cpp b/10812.cpp index 295681c..6ee7415 100644 --- a/10812.cpp +++ b/10812.cpp @@ -1,17 +1,27 @@ -#include +#include -int main() { - int T; - scanf("%d", &T); - while( T-- ){ - int S, D; - scanf("%d%d", &S, &D); - int A = S + D; - int B = S - D; - if (A < 0 || A&1 || B < 0 || B&1) - printf("impossible\n"); - else - printf("%d %d\n", A/2, B/2); +using namespace std; + +int main() +{ + int tmp; + scanf("%d ", &tmp); + int sum, dif; + while (scanf("%d %d", &sum, &dif) == 2) + { + if (sum < dif) + { + puts("impossible"); + continue; + } + else if (sum % 2 != dif % 2) + { + puts("impossible"); + continue; + } + int a = (sum + dif) / 2; + int b = (sum - dif) / 2; + printf("%d %d\n", a, b); } return 0; } diff --git a/10813.cpp b/10813.cpp new file mode 100644 index 0000000..9d47d04 --- /dev/null +++ b/10813.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +int main() +{ + int games; + int nums[75]; + int card[5][5]; + cin >> games; + while (games--) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + if (i == 2 && j == 2) + { + card[i][j] = 0; + continue; + } + cin >> card[i][j]; + } + } + for (int i = 0; i < 75; i++) + { + cin >> nums[i]; + } + bool bingo = false; + int result = 0; + bool newentry = false; + for (int i = 0; i < 75; i++) + { + for (int a = 0; a < 5; a++) + { + for (int b = 0; b < 5; b++) + { + if (card[a][b] == nums[i]) + { + card[a][b] = 0; + newentry = true; + } + } + } + if (newentry) + { + for (int a = 0; a < 5; a++) + { + int sum = 0; + for (int b = 0; b < 5; b++) + { + if (card[a][b] == 0) + { + sum++; + } + } + if (sum == 5) + { + bingo = true; + result = i + 1; + break; + } + sum = 0; + for (int b = 0; b < 5; b++) + { + if (card[b][a] == 0) + { + sum++; + } + } + if (sum == 5) + { + bingo = true; + result = i + 1; + break; + } + } + if (card[0][0] == 0 && card[1][1] == 0 && card[3][3] == 0 && card[4][4] == 0 || card[0][4] == 0 && card[1][3] == 0 && card[3][1] == 0 && card[4][0] == 0) + { + bingo = true; + result = i + 1; + break; + } + newentry = false; + } + if (bingo) + { + break; + } + } + cout << "BINGO after " << result << " numbers announced" << endl; + } + return 0; +} diff --git a/10814.cpp b/10814.cpp new file mode 100644 index 0000000..511d68b --- /dev/null +++ b/10814.cpp @@ -0,0 +1,275 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < (int)n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() + +const static int BASE = 10000, LANGE = 4; +const string BigIntBase = "10000"; + +enum +{ + LESS, + EQUAL, + GREATER +}; + +#define FRONT "%d" +#define FORMAT "%04d" + +class BigInt +{ +public: + bool neg; + vector value; + + BigInt(void) + : neg(false) + { + value.push_back(0); + } + BigInt(int s) + : neg(false) + { + value.resize(s, 0); + } + BigInt(vector v, bool n) + : neg(n), value(v) + { + } + BigInt(string in) + { + if (in[0] == '-') + { + neg = true; + in.erase(in.begin()); + } + else + neg = false; + while (in.size() % LANGE) + in = '0' + in; + for (int i = in.size() - LANGE; i >= 0; i -= LANGE) + value.push_back(atoi(in.substr(i, LANGE).c_str())); + regulation(); + } + BigInt operator+(BigInt in) + { + return add_sub(*this, in, neg == in.neg); + } + BigInt operator-(BigInt in) + { + return add_sub(*this, in, neg != in.neg); + } + BigInt operator*(BigInt in) + { + return mul(*this, in); + } + BigInt operator*(int in) + { + return mul(*this, in); + } + BigInt operator/(BigInt in) + { + return div(*this, in, (sign() != in.sign())); + } + BigInt operator/(int n) + { + return div(*this, n); + } + BigInt operator%(BigInt in) + { + return *this - ((*this / in) * in); + } + bool operator==(BigInt &in) + { + return compare(in, false) == EQUAL; + } + bool operator<(BigInt &in) + { + return compare(in, false) == LESS; + } + bool operator>(BigInt &in) + { + return compare(in, false) == GREATER; + } + int &operator[](int n) + { + return value[n]; + } + string toString(void) + { + char buffer[LANGE + 1]; + string re; + + if (neg) + re = "-"; + sprintf(buffer, FRONT, value.back()); + re += buffer; + + for (int i = value.size() - 2; i >= 0; i--) + { + sprintf(buffer, FORMAT, value[i]); + re += buffer; + } + return re; + } + int size(void) + { + return value.size(); + } + bool sign(void) + { + return neg; + } + void regulation(void) + { + value.push_back(0); + REP(i, value.size() - 1) + { + while (value[i] < 0) + { + --value[i + 1]; + value[i] += BASE; + } + value[i + 1] += value[i] / BASE; + value[i] %= BASE; + } + while (value.size() > 1 && value.back() == 0) + value.pop_back(); + neg = neg || value.back() < 0; + } + int compare(BigInt &in, bool abs) + { + if (!abs && sign() != in.sign()) + { + return sign() ? LESS : GREATER; + } + if (size() != in.size()) + { + return (size() > in.size()) ? GREATER : LESS; + } + for (int i = in.size(); i--;) + { + if (value[i] == in.value[i]) + continue; + return (value[i] > in.value[i]) ? GREATER : LESS; + } + return EQUAL; + } + BigInt add_sub(BigInt a, BigInt b, bool flg) + { + if (a.size() < b.size()) + swap(a, b); + REP(i, b.size()) + { + if (flg) + a[i] += b[i]; + else + a[i] -= b[i]; + } + a.regulation(); + return a; + } + BigInt mul(BigInt a, int n) + { + assert(n < BASE); + REP(i, size()) + a[i] *= n; + a.regulation(); + a.neg = a.neg != n < 0; + return a; + } + BigInt mul(BigInt a, BigInt b) + { + BigInt re(a.size() + b.size() + 1); + REP(i, a.size()) + { + REP(j, b.size()) + re[i + j] += a[i] * b[j]; + } + re.regulation(); + re.neg = a.neg != b.neg; + return re; + } + BigInt div(BigInt a, int n) + { + assert(n < BASE); + int c = 0, tmp; + for (int i = a.size(); i--;) + { + tmp = BASE * c + a[i]; + a[i] = tmp / n; + c = tmp % n; + } + tmp = a.neg; + a.regulation(); + a.neg = tmp != n < 0; + return a; + } + BigInt div(BigInt a, BigInt b, bool sign) + { + if (a < b) + return BigInt("0"); + int f = BASE / (b[(int)b.size() - 1] + 1); + a = a * f; + b = b * f; + BigInt c(a.size() - b.size() + 1); + for (int i = a.size() - 1, k = c.size() - 1; k >= 0; --i, --k) + { + c[k] = (i + 1 < a.size() ? a[i + 1] : 0) * BASE; + c[k] = (c[k] + a[i]) / b[(int)b.size() - 1]; + BigInt d(k + b.size()); + REP(j, b.size()) + { + d[k + j] = c[k] * b[j]; + } + d.regulation(); + while (a < d) + { + --c[k]; + REP(j, b.size()) + { + d[k + j] -= b[j]; + } + d.regulation(); + } + a = a - d; + } + c.regulation(); + c.neg = a.neg != b.neg; + return c; + } +}; + +BigInt gcd(BigInt a, BigInt b) +{ + BigInt r = a % b; + if (r.size() == 1 && r.value[0] == 0) + return b; + else + return gcd(b, r); +} + +int main() +{ + int tc; + for (cin >> tc; tc--;) + { + string s, t; + char o; + cin >> s >> o >> t; + BigInt a(s), b(t); + BigInt c = (a < b) ? gcd(b, a) : gcd(a, b); + cout << (a / c).toString() << flush; + cout << " / " << flush; + cout << (b / c).toString() << endl; + } + return 0; +} diff --git a/10815.cpp b/10815.cpp new file mode 100644 index 0000000..f097451 --- /dev/null +++ b/10815.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + string word; + set dict; + char ch = cin.get(); + while (ch != EOF) + { + if (isalpha(ch)) + { + ch = tolower(ch); + word += ch; + } + else + { + dict.insert(word); + word.clear(); + } + ch = cin.get(); + } + bool first = true; + for (set::iterator iter = dict.begin(); iter != dict.end(); iter++) + { + if (!first) + { + cout << *iter << endl; + } + first = false; + } + return 0; +} diff --git a/10816.cpp b/10816.cpp new file mode 100644 index 0000000..aa180f9 --- /dev/null +++ b/10816.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define MAXN 102 +#define INF 2147483647 +#define max(a, b) (a > b ? a : b) +#define min(a, b) (a > b ? b : a) + +struct ss +{ + int u, v; + double temp, dis; +}; +ss Edge[MAXN * 100]; +int N, E, S, T, P[MAXN][MAXN]; +double D[MAXN][MAXN], Tp[MAXN][MAXN]; + +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + D[i][j] = Tp[i][j] = INF; + P[i][j] = i; + } + D[i][i] = Tp[i][i] = 0; + } +} +void ReadCase() +{ + int i, u, v; + double d, t; + scanf("%d%d", &S, &T); + for (i = 0; i < E; i++) + { + scanf("%d%d%lf%lf", &u, &v, &t, &d); + Edge[i].u = u; + Edge[i].v = v; + Edge[i].dis = d; + Edge[i].temp = t; + if (Tp[u][v] > t) + { + Tp[u][v] = t; + Tp[v][u] = t; + } + } +} +void GetTemp() +{ + int i, j, k; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + Tp[i][j] = min(Tp[i][j], max(Tp[i][k], Tp[k][j])); +} +void GetDis(double lim) +{ + int i, j, k; + double dis; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + dis = D[i][k] + D[k][j]; + if (D[i][j] > dis) + { + D[i][j] = dis; + P[i][j] = P[k][j]; + } + } +} +void Set(double lim) +{ + int i, u, v; + for (i = 0; i < E; i++) + { + u = Edge[i].u; + v = Edge[i].v; + if (D[u][v] > Edge[i].dis && Edge[i].temp <= lim) + D[u][v] = D[v][u] = Edge[i].dis; + } +} +void Print(int u, int v) +{ + if (u == v) + printf("%d", v); + else + { + Print(u, P[u][v]); + printf(" %d", v); + } +} +void Cal() +{ + double Limit; + GetTemp(); + Limit = Tp[S][T]; + Set(Limit); + GetDis(Limit); + Print(S, T); + printf("\n"); + printf("%.1lf %.1lf\n", D[S][T], Tp[S][T]); +} + +int main() +{ + while (scanf("%d%d", &N, &E) == 2) + { + Ini(); + ReadCase(); + Cal(); + } + return 0; +} diff --git a/10817.cpp b/10817.cpp new file mode 100644 index 0000000..c926376 --- /dev/null +++ b/10817.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define forn(i, n) for (int i = 0; i < (int)(n); i++) +#define forsn(i, s, n) for (int i = (s); i < (int)(n); i++) + +typedef long long tint; + +int s, m, n; +//Minimo costo si me quedan cubrir los puestos i con dos docentes y los puestos j con un docente +tint dp[110][(1 << 8)][(1 << 8)]; + +const int SIZE = 110; +const int INF = 10000000; + +int servcost[SIZE], servmats[SIZE]; +int appcost[SIZE], appmats[SIZE]; + +tint calc(int p, int onet, int twot) +{ + if (twot == (1 << s) - 1) + return 0; + if (p == n) + return INF; + if (dp[p][onet][twot] == -1) + { + dp[p][onet][twot] = + min(calc(p + 1, onet ^ appmats[p], twot | (onet & appmats[p])) + appcost[p], + calc(p + 1, onet, twot)); + } + return dp[p][onet][twot]; +} + +int main() +{ + while (scanf("%d %d %d\n", &s, &m, &n) && s != 0) + { + forn(i, m) + { + cin >> servcost[i]; + string s; + getline(cin, s); + istringstream ss(s); + int materia, mask = 0; + while (ss >> materia) + mask |= (1 << (materia - 1)); + servmats[i] = mask; + } + forn(i, n) + { + cin >> appcost[i]; + string s; + getline(cin, s); + istringstream ss(s); + int materia, mask = 0; + while (ss >> materia) + mask |= (1 << (materia - 1)); + appmats[i] = mask; + } + int oneteach = 0, twoteach = 0; + forn(i, n) forn(j, 1 << s) forn(k, 1 << s) dp[i][j][k] = -1; + forn(i, m) + { + twoteach |= (oneteach & servmats[i]); + oneteach = oneteach ^ servmats[i]; + } + int tot = 0; + forn(i, m) tot += servcost[i]; + cout << calc(0, oneteach, twoteach) + tot << endl; + } + return 0; +} diff --git a/10818.cpp b/10818.cpp new file mode 100644 index 0000000..dcb1cad --- /dev/null +++ b/10818.cpp @@ -0,0 +1,224 @@ +#include + +using namespace std; + +string Path[25][25], FinalPath; +int X[] = {0, -1, 1, 0}; +int Y[] = {1, 0, 0, -1}; +char D[] = "ENSW"; +int Label[25][25], Dis[25][25], Rec[25], Lowest[15], RE[25]; +int R, C, tt, Ind, Min; +char Mz[22][22], Fg[22][22]; +struct ss +{ + string path; + int dis, r, c; +}; +queue Q; +void ReadCase() +{ + int i, j; + tt = 1; + for (i = 0; i < R; i++) + { + gets(Mz[i]); + for (j = 0; j < C; j++) + { + if (Mz[i][j] == '*') + Label[i][j] = ++tt; + else if (Mz[i][j] == 'S') + Label[i][j] = 1; + } + } +} +void Reach(int r, int c) +{ + if (r >= R || r < 0 || c >= C || c < 0) + return; + if (Fg[r][c]) + return; + if (Mz[r][c] == 'X' || Mz[r][c] == '#') + return; + if (Mz[r][c] == '*') + RE[Ind++] = Label[r][c]; + Fg[r][c] = 1; + Reach(r, c + 1); + Reach(r, c - 1); + Reach(r - 1, c); + Reach(r + 1, c); +} +void BFS(int r, int c, int lab) +{ + ss t, d; + int i, nr, nc, num; + t.r = r; + t.c = c; + t.dis = 0; + t.path = ""; + Q.push(t); + Fg[r][c] = 1; + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + for (i = 0; i < 4; i++) + { + nr = t.r + X[i]; + nc = t.c + Y[i]; + if (nr >= R || nr < 0 || nc >= C || nc < 0) + continue; + if (Mz[nr][nc] == '#' || Mz[nr][nc] == 'X') + continue; + if (Fg[nr][nc] == 1) + continue; + if (Mz[nr][nc] == '*' || Mz[nr][nc] == 'S') + { + num = Label[nr][nc]; + Path[lab][num] = t.path + D[i]; + Dis[lab][num] = t.dis + 1; + } + d.r = nr; + d.c = nc; + d.dis = t.dis + 1; + d.path = t.path + D[i]; + Q.push(d); + Fg[nr][nc] = 1; + } + } +} +void Reset() +{ + int i, j; + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + Fg[i][j] = 0; +} +int A[25]; +int B[25]; +char F[25]; +int Guess(int n, int w) +{ + int i, c = 0, v; + for (i = 0; i < Ind; i++) + if (F[i] == 0) + { + v = RE[i]; + c += Lowest[v]; + } + return c - 10; +} +void Recur(int n, int cost, int level, string path) +{ + int i, c, v, u; + string temp; + if (cost >= Min) + return; + A[level] = RE[n]; + B[level] = n; + if (level == Ind) + { + c = cost + Dis[RE[n]][1]; + if (c > Min) + { + return; + } + temp = path + Path[RE[n]][1]; + if (FinalPath != "" && c == Min) + if (FinalPath.compare(temp) < 0) + { + return; + } + Min = c; + FinalPath = temp; + return; + } + if ((cost + Guess(RE[n], cost)) > Min) + return; + F[n] = 1; + for (i = 0; i < Ind; i++) + { + if (F[i] == 0) + { + u = RE[n]; + v = RE[i]; + c = Dis[u][v]; + Recur(i, cost + c, level + 1, path + Path[u][v]); + } + } + F[n] = 0; +} +void GetLowest() +{ + int i, j, k, u, v; + for (i = 1; i <= tt; i++) + Lowest[i] = 99999; + for (i = 0; i < Ind; i++) + { + u = RE[i]; + for (j = i + 1; j < Ind; j++) + { + v = RE[j]; + k = Dis[u][v]; + if (Lowest[u] > k) + Lowest[u] = k; + if (Lowest[v] > k) + Lowest[v] = k; + } + } +} +void Cal() +{ + int i, j, d = 0; + if (tt == 1) + return; + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + if (Mz[i][j] == 'S') + break; + if (Mz[i][j] == 'S') + break; + } + RE[0] = 1; + Ind = 1; + Reset(); + Reach(i, j); + if (Ind == 1) + { + cout << "Stay home!"; + return; + } + Reset(); + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + if (Mz[i][j] == '*' || Mz[i][j] == 'S') + { + BFS(i, j, Label[i][j]); + Reset(); + } + } + } + GetLowest(); + FinalPath = ""; + Min = 999999999; + Recur(0, 0, 1, ""); + cout << FinalPath.c_str(); +} + +int main() +{ + char temp[100]; + while (1) + { + gets(temp); + sscanf(temp, "%d%d", &R, &C); + if (!R && !C) + break; + ReadCase(); + Cal(); + cout << endl; + } + return 0; +} diff --git a/10819.cpp b/10819.cpp new file mode 100644 index 0000000..74ef138 --- /dev/null +++ b/10819.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define MAX(a, b) (a > b ? a : b) + +struct ss +{ + int cost; + int fav; +}; +struct NewOld +{ + int New, Old; +}; +ss List[102]; +int T[10300]; +char F[10300]; +NewOld B[10300]; +int D, I, Sum, Max, k; +vector tmp; +vector V, New; + +void Dynamic() +{ + int i, ind, cost, fav; + int limit = D; + ss xyz; + if (D > 1800) + limit = D + 200; + Max = 0; + V.clear(); + for (i = 0; i < I; i++) + { + if (List[i].cost > limit) + continue; + tmp.clear(); + New.clear(); + for (ind = 0; ind < V.size(); ind++) + { + cost = V[ind] + List[i].cost; + fav = T[V[ind]] + List[i].fav; + if (cost > limit) + continue; + if (F[cost] == 1) + if (T[cost] >= fav) + continue; + if (F[cost] == 0) + { + F[cost] = 1; + New.push_back(cost); + } + xyz.cost = cost; + xyz.fav = fav; + tmp.push_back(xyz); + if (cost <= D || cost > 2000) + if (fav > Max) + Max = fav; + } + if (F[List[i].cost] == 0 || T[List[i].cost] < List[i].fav) + { + if (F[List[i].cost] == 0) + { + F[List[i].cost] = 1; + V.push_back(List[i].cost); + } + if (List[i].fav > Max && (List[i].cost <= D || List[i].cost > 2000)) + Max = List[i].fav; + T[List[i].cost] = List[i].fav; + } + for (ind = tmp.size() - 1; ind >= 0; ind--) + T[tmp[ind].cost] = tmp[ind].fav; + for (ind = New.size() - 1; ind >= 0; ind--) + V.push_back(New[ind]); + } +} +void Free() +{ + int i; + for (i = V.size() - 1; i >= 0; i--) + F[V[i]] = 0; +} +void Cal() +{ + int i, total = 0; + if (Sum <= D) + { + for (i = 0; i < I; i++) + total += List[i].fav; + printf("%d\n", total); + return; + } + Dynamic(); + printf("%d\n", Max); + Free(); +} + +int main() +{ + int i; + while (scanf("%d%d", &D, &I) == 2) + { + Sum = 0; + for (i = 0; i < I; i++) + { + scanf("%d%d", &List[i].cost, &List[i].fav); + Sum += List[i].cost; + } + Cal(); + } + return 0; +} diff --git a/1082.cpp b/1082.cpp new file mode 100644 index 0000000..eb2e4e1 --- /dev/null +++ b/1082.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define eps 1e-8 +const double pi = acos(-1.0); +int d[5], p[5]; +double tmp[5]; + +int dcmp(double x) +{ + return (x > eps) - (x < -eps); +} + +double sqr(double x) +{ + return x * x; +} + +bool check(double x) +{ + p[4] = p[0]; + double ang = 0; + for (int i = 1; i <= 4; ++i) + { + tmp[i] = acos((sqr(x - d[p[i]]) + sqr(x - d[p[i - 1]]) - sqr(d[p[i]] + d[p[i - 1]])) / (2.0 * (x - d[p[i]]) * (x - d[p[i - 1]]))); + for (int j = 1; j < i; ++j) + { + double ago = acos((sqr(x - d[p[i]]) + sqr(x - d[p[j - 1]]) - sqr(d[p[i]] + d[p[j - 1]])) / (2.0 * (x - d[p[i]]) * (x - d[p[j - 1]]))); + tmp[i] = max(tmp[i], ago - tmp[j]); + } + ang += tmp[i]; + } + double add = acos((sqr(x - d[p[3]]) + sqr(x - d[p[1]]) - sqr(d[p[3]] + d[p[1]])) / (2.0 * (x - d[p[3]]) * (x - d[p[1]]))); + if (dcmp(add - tmp[1] - tmp[4]) > 0) + { + ang += add - tmp[1] - tmp[4]; + } + return dcmp(ang - 2 * pi) <= 0; +} + +int main() +{ + //freopen( "in.in", "r", stdin ); + //freopen( "me.out", "w", stdout ); + int cas = 0; + while (scanf("%d", &d[1]) && d[1]) + { + for (int i = 2; i <= 4; ++i) + { + scanf("%d", d + i); + } + double l = d[1] + d[2], r = (d[1] * 4); + while (r - l > 1e-4) + { + double m = (l + r) / 2; + bool found = false; + for (int i = 0; i < 4; ++i) + { + p[i] = i + 1; + } + do + { + if (check(m)) + { + found = true; + break; + } + } while (next_permutation(p, p + 4)); + if (found) + { + r = m; + } + else + { + l = m; + } + } + printf("Case %d: %.0lf\n", ++cas, l); + } + return 0; +} diff --git a/10820.cpp b/10820.cpp new file mode 100644 index 0000000..2fa43ef --- /dev/null +++ b/10820.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +/* +10820 +Send a Table +*/ +#define MAXN 50002 +int R[MAXN]; +int RP(int n) +{ + int sum = n; + for (int i = 2; i * i <= n; i++) + { + if (n % i == 0) + { + sum -= sum / i; + } + while (n % i == 0) + { + n /= i; + } + } + if (n > 1) + { + sum -= sum / n; + } + return sum; +} +void Gen() +{ + int i; + R[1] = 1; + for (i = 2; i + 1 < MAXN; i++) + { + R[i] = RP(i) * 2; + R[i] += R[i - 1]; + } +} +int main() +{ + int n; + Gen(); + while (scanf("%d", &n) && n) + { + printf("%d\n", R[n]); + } + return 0; +} diff --git a/10821.cpp b/10821.cpp new file mode 100644 index 0000000..fb56805 --- /dev/null +++ b/10821.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int N, H; +int pow2[31]; +int print[10000], len; +void RUN1(int start, int n, int h) +{ + if (!h) + { + return; + } + int s = (2 * start + n - 1) >> 1; + print[len++] = s; + RUN1(start, pow2[h - 1], h - 1); + RUN1(s + 1, pow2[h - 1], h - 1); +} +void RUN(int start, int n, int h) +{ + if (!n) + { + return; + } + if (n <= pow2[h - 1]) + { + print[len++] = start; + RUN(start + 1, n - 1, h - 1); + } + else + { + int s = start + n - pow2[h - 1]; + print[len++] = s; + RUN(start, s - start, h - 1); + RUN1(s + 1, pow2[h - 1], h - 1); + } +} +int main() +{ + int caseno = 1; + pow2[0] = 1; + for (int i = 1; i < 31; i++) + { + pow2[i] = pow2[i - 1] << 1; + } + while (scanf("%d %d", &N, &H) == 2) + { + if (!N || !H) + { + break; + } + printf("Case %d:", caseno++); + if (pow2[H] - 1 < N) + { + puts(" Impossible."); + } + else + { + len = 0; + RUN(1, N, H); + for (int i = 0; i < len; i++) + { + printf(" %d", print[i]); + } + printf("\n"); + } + } +} diff --git a/10823.cpp b/10823.cpp new file mode 100644 index 0000000..cbb531a --- /dev/null +++ b/10823.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define CR "CIRCLE" + +vector Ob; +vector Len[102]; +int R, G, B, total; + +int Round(int p, int q)// Rounds fraction p/q +{ + int x; + double y; + y = (double)p / (double)q; + y += .5; + x = (int)y; + return x; + return (p / q) + (((2 * (p % q)) >= q) ? 1 : 0); +} + +int Dis(int x, int y, int p, int q) +{ + int n, m; + n = (x - p); + n *= n; + m = (y - q); + m *= m; + return m + n; +} + +int Pos(string ob, int x, int y, int ind) +{ + int dis, k, sx, sy, px, py, rx, ry, qx, qy; + if (ob == CR) + { + dis = Dis(Len[ind][0], Len[ind][1], x, y); + k = Len[ind][2]; + if (k * k == dis) + return 1; + else if (k * k < dis) + return 2; + return 0; + } + else + { + ry = sy = Len[ind][1]; + rx = px = Len[ind][0];// ok + qy = py = sy + Len[ind][2]; + qx = sx = rx + Len[ind][2];// ok + if (y == sy && (x >= rx && x <= sx)) + return 1; + if (x == rx && (y >= ry && y <= py)) + return 1; + if (y == py && (x >= px && x <= qx)) + return 1; + if (x == sx && (y >= sy && y <= qy)) + return 1; + if (x < rx || x > sx) + return 2; + if (y < sy || y > qy) + return 2; + } + return 0; +} + +void Count(int x, int y) +{ + int i, d, f = 0; + R = G = B = 0; + total = 0; + for (i = 0; i < Ob.size(); i++) + { + d = Pos(Ob[i], x, y, i); + if (d == 1) + { + R = G = B = 0; + return; + } + else if (d == 0) + { + total++; + R += Len[i][3]; + G += Len[i][4]; + B += Len[i][5]; + } + } + if (!total) + R = G = B = 255; + else + { + R = Round(R, total); + G = Round(G, total); + B = Round(B, total); + } +} + +void Cal(int q) +{ + int x, y; + while (q--) + { + cin >> x >> y; + Count(x, y); + cout << "(" << R << ", " << G << ", " << B << ")\n"; + } +} + +int main() +{ + int ks, i, q, in, k, n, m = 1; + char ss[100]; + cin >> ks; + while (ks--) + { + cin >> in >> q; + k = 0; + while (in--) + { + cin >> ss; + Ob.push_back(ss); + for (i = 0; i < 6; i++) + { + cin >> n; + Len[k].push_back(n); + } + k++; + } + if (m > 1) + cout << endl; + cout << "Case " << m++ << ":\n"; + Cal(q); + for (int i = 0; i < Ob.size(); i++) + Len[i].clear(); + Ob.clear(); + } + return 0; +} diff --git a/10824.cpp b/10824.cpp new file mode 100644 index 0000000..6df5cf8 --- /dev/null +++ b/10824.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const double PI = acos(-1); +const double eps = 1e-8; +#define EQ(a, b) (abs((a) - (b)) < eps) + +class P +{ +public: + double x, y; + double rad; + double r; + void calc_rad() + { + rad = atan2(y, x); + } + bool operator<(const P &a) const + { + return rad < a.rad; + } +}; + +inline double dist(double x, double y) +{ + return sqrt(x * x + y * y); +} + +inline double cross(double xa, double ya, double xb, double yb) +{ + return xa * yb - ya * xb; +} + +inline double dot(double xa, double ya, double xb, double yb) +{ + return xa * xb + ya * yb; +} + +double needr[2001]; +int cnt[2001]; +int table[2001][2001]; +void solve(int n, P *in) +{ + sort(in, in + n); + rep(i, n + 1) cnt[i] = 0; + rep(i, n) rep(j, n + 1) table[i][j] = 0; + rep(i, n) in[i].r = dist(in[i].x, in[i].y); + rep(i, n) + { + int p = n; + REP(j, 1, n && p > 2) + { + int next = (i + j) % n; + if (cross(in[i].x, in[i].y, in[next].x, in[next].y) < 0) + { + break; + } + /* + double tmp =abs(acos(dot(in[i].x,in[i].y,in[next].x,in[next].y)/ + (dist(in[i].x,in[i].y)*dist(in[next].x,in[next].y)))); + */ + double tmp = abs(acos(dot(in[i].x, in[i].y, in[next].x, in[next].y) / + (in[i].r * in[next].r))); + if (EQ(tmp, needr[p])) + { + table[next][p] = table[i][p] + 1; + if (table[next][p] == p - 1) + { + cnt[p]++; + } + } + else if (tmp > needr[p]) + { + p--, j--; + } + } + } + rep(i, n + 1) if (cnt[i] != 0) + { + printf("%d %d\n", i, cnt[i]); + } +} + +main() +{ + int n, tc = 1; + P in[2000]; + P tmp[2000]; + REP(i, 3, 2001) + needr[i] = PI * 2. / i; + while (scanf("%d", &n) != -1 && n) + { + rep(i, n) + scanf("%lf%lf", &in[i].x, &in[i].y); + rep(i, n) in[i].calc_rad(); + printf("Case %d:\n", tc++); + solve(n, in); + } + return false; +} diff --git a/10825.cpp b/10825.cpp new file mode 100644 index 0000000..c41114b --- /dev/null +++ b/10825.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +int used[400]; +int vec[6]; +int ans[10]; +int M, N; +bool DFS(int depth) +{ + if (depth == M) + { + int count, add; + int s[400]; + for (int i = 2; i <= M; i++) + { + add = 0; + memset(s, 0, sizeof(s)); + for (int j = depth - 1; j >= 0; j--) + { + int k = ans[j] * i + add; + add = k / N; + k %= N; + s[k]++; + } + for (int i = 0; i < N; i++) + if (s[i] != used[i]) + { + return false; + } + } + for (int i = 0; i < depth; i++) + { + if (i) + { + putchar(' '); + } + printf("%d", ans[i]); + } + puts(""); + return true; + } + else + { + for (int i = 0; i < M; i++) + { + if (used[vec[i]]) + { + continue; + } + if (depth == 0 && vec[i] * M >= N) + { + continue; + } + if (depth == 0 && vec[i] == 0) + { + continue; + } + used[vec[i]] = true; + ans[depth] = vec[i]; + if (DFS(depth + 1)) + { + return true; + } + used[vec[i]] = false; + } + return false; + } +} +int main() +{ + while (scanf("%d %d", &M, &N) == 2) + { + if (!M && !N) + { + break; + } + bool flag = false; + memset(used, false, sizeof(used)); + if (M != 6 && M != 4) + { + puts("Not found."); + continue; + } + for (int i = 5; i < N; i++) + { + for (int j = 0; j < M; j++) + { + vec[j] = (i * (j + 1)) % N; + } + if ((flag = DFS(0))) + { + break; + } + } + if (!flag) + { + puts("Not found."); + } + } +} diff --git a/10826.cpp b/10826.cpp new file mode 100644 index 0000000..48d069e --- /dev/null +++ b/10826.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int table[300][300]; +int f(int, int, int); +inline int g(int a, int b, int z, int x) +{ + int bounda, boundb; + int max = 0, t; + if (x > z) + { + bounda = (x + z + 1) >> 1, boundb = b; + if (bounda < a) + { + bounda = a; + } + max = f(bounda, boundb, x); + bounda = a; + boundb = (x + z) >> 1; + if (boundb > b) + { + boundb = b; + } + t = f(bounda, boundb, x); + if (t > max) + { + max = t; + } + } + else + { + bounda = (x + z + 1) >> 1, boundb = b; + if (bounda < a) + { + bounda = a; + } + max = f(bounda, boundb, x); + bounda = a, boundb = (x + z) >> 1; + if (boundb > b) + { + boundb = b; + } + t = f(bounda, boundb, x); + if (t > max) + { + max = t; + } + } + return 1 + max; +} +int f(int a, int b, int z) +{ + if ((a == b && b == z) || (a > b)) + { + return 0; + } + if (a == b) + { + return 1; + } + int t1 = b - a, t2 = z - a; + if (t2 < 0) + { + t2 = t1 - t2; + } + if (table[t1][t2]) + { + return table[t1][t2]; + } + int min = 999999; + for (int x = a; x <= b; x++) + { + if (x == z) + { + continue; + } + int t = g(a, b, z, x); + if (t < min) + { + min = t; + } + } + table[t1][t2] = min; + return min; +} +int main() +{ + int n; + while (scanf("%d", &n) == 1, n) + { + int min = 9999999, t; + for (int i = 1; i <= n; i++) + { + t = f(1, n, i) + 1; + if (t < min) + { + min = t; + } + } + printf("%d guess(es) required.\n", min); + } +} diff --git a/10827.cpp b/10827.cpp new file mode 100644 index 0000000..46ee0e5 --- /dev/null +++ b/10827.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define MAXN 200 +#define mx(a, b) (a > b ? a : b) + +int N, B[MAXN][MAXN]; + +void ReadCase() +{ + int i, j, l = N + 1; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + scanf("%d", &B[i][j]); + B[i][N + j] = B[i][j]; + B[l][j] = B[i][j]; + B[l][j + N] = B[i][j]; + } + l++; + } +} + +void Set() +{ + int i, j; + for (i = 1; i <= N * 2; i++) + for (j = 1; j <= N * 2; j++) + B[i][j] += B[i - 1][j]; +} + +void Cal() +{ + int i, j, k, l, sum, m; + int max = 0, temp; + if (N == 1) + { + printf("%d\n", mx(B[1][1], 0)); + return; + } + Set(); + for (i = 1; i <= N; i++) + for (j = i; j < i + N; j++) + for (k = 1; k <= N; k++) + { + m = 0; + for (l = k; l < k + N; l++) + { + m += B[j][l] - B[i - 1][l]; + if (m < 0) + m = 0; + else if (m > max) + max = m; + } + } + printf("%d\n", max); +} + +int main() +{ + int k; + scanf("%d", &k); + while (k--) + { + scanf("%d", &N); + ReadCase(); + Cal(); + } + return 0; +} diff --git a/10828.cpp b/10828.cpp new file mode 100644 index 0000000..7480f14 --- /dev/null +++ b/10828.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +#define MAXN 116 +#define EPS 1E-8 + +vector a[MAXN]; +int degree[MAXN]; +double ans[MAXN]; +double matrix[MAXN][MAXN]; +bool inf[MAXN]; +int n, testnum; + +void Read() +{ + int i, u, v; + memset(degree, 0, sizeof(degree)); + memset(matrix, 0, sizeof(matrix)); + memset(ans, 0, sizeof(ans)); + for (i = 0; i < MAXN; i++) + { + a[i].clear(); + } + while (1) + { + scanf("%d%d", &u, &v); + if (!u || !v) + { + break; + } + degree[u]++; + a[v].push_back(u); + } +} +void Init() +{ + int i, j; + for (i = 1; i <= n; i++) + { + matrix[i][i] = 1; + for (j = 0; j < a[i].size(); j++) + { + matrix[i][a[i][j]] -= 1 / (degree[a[i][j]] + 0.0); + } + } + matrix[1][n + 1] = 1; +} +void Debug() +{ + int i, j; + cerr << endl; + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n + 1; j++) + { + cerr << matrix[i][j] << ' '; + } + cerr << endl; + } +} +void Myswap(int i, int j) +{ + int k; + for (k = 1; k <= n + 1; k++) + { + swap(matrix[i][k], matrix[j][k]); + } +} +double myfabs(double x) +{ + return x < 0 ? -x : x; +} +void Gauss_jordan() +{ + int row, col; + int ret, i, j; + for (row = col = 1; row <= n && col <= n; row++, col++) + { + ret = row; + for (i = row + 1; i <= n; i++) + if (myfabs(matrix[i][col]) > myfabs(matrix[ret][col])) + { + ret = i; + } + if (myfabs(matrix[ret][col]) < EPS) + { + continue; + } + if (ret != row) + { + Myswap(row, ret); + } + for (i = 1; i <= n; i++) + if (i != row) + for (j = n + 1; j >= col; j--) + { + matrix[i][j] -= matrix[i][col] * matrix[row][j] / matrix[row][col]; + } + } +} +void Process_Ans() +{ + int i, j; + memset(inf, false, sizeof(inf)); + for (i = n; i >= 1; i--) + { + if (myfabs(matrix[i][i]) > EPS) + { + for (j = i + 1; j <= n; j++) + if (myfabs(matrix[i][j]) > EPS && inf[j]) + { + inf[i] = true; + break; + } + if (!inf[i]) + { + ans[i] = matrix[i][n + 1] / matrix[i][i]; + } + } + else + { + if (myfabs(matrix[i][n + 1]) > EPS) + { + inf[i] = true; + } + else + { + ans[i] = 0; + } + } + } +} +void Print() +{ + int x, q; + scanf("%d", &q); + if (!q) + { + return; + } + printf("Case #%d:\n", testnum); + while (q--) + { + scanf("%d", &x); + if (inf[x]) + { + printf("infinity\n"); + } + else + { + printf("%.3lf\n", myfabs(ans[x]) < EPS ? 0.0 : ans[x]); + } + } +} + +int main() +{ + while (1) + { + ++testnum; + scanf("%d", &n); + if (!n) + { + break; + } + Read(); + Init(); + Gauss_jordan(); + Process_Ans(); + Print(); + } + return 0; +} diff --git a/10829.cpp b/10829.cpp new file mode 100644 index 0000000..7c1f104 --- /dev/null +++ b/10829.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +#define MX 200005 +#define MAXN MX + +#define REP(i, n) for (int i = 0; i < (n); i++) +#define OREP(i, n) for (int i = 1; i <= (n); i++) + +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int UINT; + +int n, m, k, t; +int sa[MX], rank_[MX], height[MX], text[MX]; +char input[MX]; + +class Suffix_DA +{ +public: + int wa[MAXN], wb[MAXN], wv[MAXN], ws[MAXN]; + int cmp(int *r, int a, int b, int l) + { + return r[a] == r[b] && r[a + l] == r[b + l]; + } + void da(int *r, int *sa, int n, int m) + { + int i, j, p, *x = wa, *y = wb, *t; + for (i = 0; i < m; i++) + { + ws[i] = 0; + } + for (i = 0; i < n; i++) + { + ws[x[i] = r[i]]++; + } + for (i = 1; i < m; i++) + { + ws[i] += ws[i - 1]; + } + for (i = n - 1; i >= 0; i--) + { + sa[--ws[x[i]]] = i; + } + for (j = 1, p = 1; p < n; j *= 2, m = p) + { + for (p = 0, i = n - j; i < n; i++) + { + y[p++] = i; + } + for (i = 0; i < n; i++) + if (sa[i] >= j) + { + y[p++] = sa[i] - j; + } + for (i = 0; i < n; i++) + { + wv[i] = x[y[i]]; + } + for (i = 0; i < m; i++) + { + ws[i] = 0; + } + for (i = 0; i < n; i++) + { + ws[wv[i]]++; + } + for (i = 1; i < m; i++) + { + ws[i] += ws[i - 1]; + } + for (i = n - 1; i >= 0; i--) + { + sa[--ws[wv[i]]] = y[i]; + } + for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) + { + x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; + } + } + } + void get_height(int *r, int *sa, int *rank_, int *height, int n) + { + int i, j, k = 0; + for (i = 1; i <= n; i++) + { + rank_[sa[i]] = i; + } + for (int i = 0; i < n; i++) + { + if (k) + { + k--; + } + j = sa[rank_[i] - 1]; + while (r[i + k] == r[j + k]) + { + k++; + } + height[rank_[i]] = k; + } + } + int dp[MAXN][32]; + void RMQ_init(int *height, int n) + { + for (int i = 0; i < n; i++) + { + dp[i][0] = height[i]; + } + for (int j = 1; (1 << j) <= n; j++) + for (int i = 0; i + (1 << j) - 1 < n; i++) + { + dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); + } + } + int RMQ(int L, int R) + { + int k = 0; + while ((1 << (k + 1)) <= R - L + 1) + { + k++; + } + return min(dp[L][k], dp[R - (1 << k) + 1][k]); + } +}; + +Suffix_DA da; + +int pre(int i, int j) +{ + if (rank_[i] > rank_[j]) + { + swap(i, j); + } + return da.RMQ(rank_[i] + 1, rank_[j]); +} + +int main() +{ + scanf("%d", &t); + OREP(c, t) + { + scanf("%d %s", &n, input); + int len = strlen(input); + REP(i, len) + { + text[i] = input[i] - 'a' + 1; + text[2 * len - i] = input[i] - 'a' + 1; + } + text[len] = 27; + text[2 * len + 1] = 0; + da.da(text, sa, 2 * len + 2, 28); + memset(height, 0, sizeof height); + da.get_height(text, sa, rank_, height, 2 * len + 1); + da.RMQ_init(height, 2 * len + 1); + int ret = 0; + for (int L = (len - n) / 2; L >= 1; L--) + { + for (int i = 0; i < len; i += L) + { + int l, r; + if (i + n + L < len) + { + r = min(n + L, pre(i, i + n + L)); + } + else + { + r = 0; + } + if (i && i + n + L - 1 < len) + { + l = min(n + L - 1, pre(2 * len - (i - 1), 2 * len - (i + n + L - 1))); + } + else + { + l = 0; + } + if (!l) + { + if (r >= L) + { + ret++; + } + } + else if (!r) + { + } + else + { + if (l + r >= L) + { + ret += (min(L - 1, l) - max(0, L - r) + 1); + } + } + } + } + printf("Case %d: %d\n", c, ret); + } + return 0; +} diff --git a/1083.cpp b/1083.cpp new file mode 100644 index 0000000..e693baa --- /dev/null +++ b/1083.cpp @@ -0,0 +1,182 @@ +#include + +using namespace std; + +const int maxn = 50010; +const int maxe = 210000; + +int first[maxn], nxt[maxe], vv[maxe], w[maxe]; +int dis[maxn], dis1[maxn], dif[maxn]; +int d[maxn]; +vector V[maxn]; +vector B[maxn]; +vector W[maxn]; + +queue q; + +void bfs(int n) +{ + while (!q.empty()) + { + q.pop(); + } + int u, v, e; + q.push(n); + memset(dis1, -1, sizeof(dis1)); + dis1[n] = 0; + memset(dif, 0, sizeof(dif)); + while (!q.empty()) + { + u = q.front(); + q.pop(); + e = first[u]; + while (e) + { + v = vv[e]; + if (dis1[v] < 0) + { + dis1[v] = dis1[u] + w[e]; + } + else if (dis1[v] > dis1[u] + w[e]) + { + dif[v] = 1; + } + else if (dis1[v] < dis1[u] + w[e]) + { + dis1[v] = dis1[u] + w[e], dif[v] = 1; + } + if (dif[u]) + { + dif[v] = 1; + } + d[v]--; + if (!d[v]) + { + q.push(v); + } + e = nxt[e]; + } + } +} +void add1(int u, int v, int ww, int &e) +{ + nxt[e] = first[u], vv[e] = v, w[e] = ww, first[u] = e++; + d[v]++; +} +void add2(int u, int v, int ww, int &e, int b) +{ + add1(u, v, ww, e); + V[v].push_back(u); + W[v].push_back(ww); + B[v].push_back(b); +} +int is[maxn], gai[maxn]; +int spfa(int n) +{ + memset(dis, -1, sizeof(dis)); + memset(is, 0, sizeof(is)); + memset(gai, 0, sizeof(gai)); + dis[1] = 0; + while (!q.empty()) + { + q.pop(); + } + q.push(1); + int u, v, e, i; + while (!q.empty()) + { + u = q.front(); + q.pop(); + e = first[u]; + int tmp = 0; + for (i = 0; i < V[u].size(); i++) + { + v = V[u][i]; + if (dis[v] + W[u][i] < dis[u]) + { + if (dif[u]) + { + return 0; + } + tmp = 1; + } + } + if (tmp) + { + for (i = 0; i < V[u].size(); i++) + { + v = V[u][i]; + gai[B[u][i]] = dis1[1] - dis1[u] - dis[v] - W[u][i]; + } + dis[u] = dis1[1] - dis1[u]; + } + e = first[u]; + while (e) + { + v = vv[e]; + if (dis[v] < dis[u] + w[e]) + { + dis[v] = dis[u] + w[e]; + } + --d[v]; + if (!d[v]) + { + q.push(v); + } + e = nxt[e]; + } + } + return 1; +} +int nu[maxn], nv[maxn], nw[maxn]; +int main() +{ + int n, m, i, j, u, v, e; + int ca = 0; + while (scanf("%d%d", &n, &m), n || m) + { + memset(first, 0, sizeof(first)); + memset(d, 0, sizeof(d)); + e = 2; + for (i = 1; i <= m; i++) + { + scanf("%d%d%d", &u, &v, &j); + add1(v, u, j, e); + nu[i] = u, nv[i] = v, nw[i] = j; + } + bfs(n); + memset(first, 0, sizeof(first)); + memset(d, 0, sizeof(d)); + e = 2; + for (i = 1; i <= n; i++) + { + V[i].clear(), B[i].clear(), W[i].clear(); + } + for (i = 1; i <= m; i++) + { + u = nu[i], v = nv[i], j = nw[i]; + add2(u, v, j, e, i); + } + printf("Case %d: ", ++ca); + if (!spfa(n)) + { + puts("No solution"); + } + else + { + j = 0; + for (i = 1; i <= m; i++) + if (gai[i]) + { + j++; + } + printf("%d %d\n", j, dis1[1]); + for (i = 1; i <= m; i++) + if (gai[i]) + { + printf("%d %d\n", i, gai[i]); + } + } + } + return 0; +} diff --git a/10830.cpp b/10830.cpp new file mode 100644 index 0000000..324b48a --- /dev/null +++ b/10830.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +/* +10830 +A new Fun +*/ +typedef long long ss; +ss Summ(ss n, ss m, ss k) +{ + ss i, j; + if (n > m) + { + return 0; + } + i = ((n + m) * (m - n + 1)) / 2; + j = i + (m - n + 1) * k; + return j; +} +ss Cal(ss n) +{ + ss i, total = 0; + for (i = 2; i * i <= n; i++) + { + total += Summ(i + 1, n / i, i); + total += i; + } + return total; +} +int main() +{ + ss n, ks = 1; + while (scanf("%lld", &n) && n) + { + printf("Case %lld: %lld\n", ks++, Cal(n)); + } + return 0; +} diff --git a/10831.cpp b/10831.cpp new file mode 100644 index 0000000..c33cdd9 --- /dev/null +++ b/10831.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define int64 long long int +int64 GCD(int64 a, int64 b) +{ + return (b == 0) ? a : GCD(b, a % b); +} +bool check(int64 p, int64 a) +{ + /* if(p%2==0) return false; + for(int64 i=3;i*i<=p;i+=2) + if(p%i==0) return false;*/ + if (GCD(p, a) != 1) + { + return false; + } + return true; +} +int64 mod(int64 base, int64 exp, int64 m) +{ + if (exp == 0 || base == 0) + { + return 1; + } + else if (exp == 1) + { + return base % m; + } + else if (exp & 1) + { + return (mod(base, exp - 1, m) * base) % m; + } + else + { + int64 t = mod(base, exp / 2, m); + return (t * t) % m; + } +} +int64 a, p; +int main() +{ + while (scanf("%lld %lld", &a, &p) == 2) + { + if (a == -1 && p == -1) + { + break; + } + a %= p; + if (check(p, a) || p < 3 || a == 0) + { + int64 s = mod(a, p / 2, p); + if (s == 1) + { + puts("Yes"); + } + else + { + puts("No"); + } + } + else + { + puts("No"); + } + } +} diff --git a/10832.cpp b/10832.cpp new file mode 100644 index 0000000..3996bfa --- /dev/null +++ b/10832.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +struct ss +{ + int x, y, z; + double dis; + double fuel; + double time; +}; +queue Q; +vector v; +char visited[25]; +double fuel, brate, speed, tra, ttime, lf, home; +int node; + +double Dis(ss t, ss d) +{ + double dis; + dis = (t.x - d.x) * (t.x - d.x) + (t.y - d.y) * (t.y - d.y) + (t.z - d.z) * (t.z - d.z); + dis = sqrt(dis); + return dis; +} + +double Get(ss tt, int visit) +{ + double dis, min; + ss dum; + int i, next; + while (!Q.empty()) + Q.pop(); + Q.push(tt); + while (!Q.empty()) + { + tt = Q.front(); + Q.pop(); + min = 2E+15; + for (i = 0; i < v.size(); i++) + { + if (visited[i] == 1) + continue; + dis = Dis(tt, v[i]); + if (dis < min) + { + min = dis; + next = i; + } + } + visited[next] = 1; + visit++; + dum = v[next]; + dum.dis = tt.dis + min; + if (visit == node) + { + home = dum.dis; + return home; + } + Q.push(dum); + } + return dis; +} + +int BFS() +{ + int i, next, cover = 0; + double time, dis, min, rf; + ss temp, dum; + temp.x = 0; + temp.y = 0; + temp.z = 1; + temp.dis = 0; + temp.fuel = fuel; + temp.time = 0; + Q.push(temp); + while (!Q.empty()) + { + temp = Q.front(); + Q.pop(); + min = 2e+15; + for (i = 0; i < v.size(); i++) + { + if (visited[i] == 1) + continue; + dis = Dis(temp, v[i]); + if (dis < min) + { + min = dis; + next = i; + } + } + time = min / speed; + rf = time * brate; + rf = temp.fuel - rf; + if (rf < 0) + { + rf = temp.fuel / brate; + tra = temp.dis + rf * speed; + temp.dis -= tra; + home = Get(temp, cover); + return 0; + } + dum = v[next]; + dum.dis = temp.dis + min; + dum.fuel = rf; + dum.time = temp.time + time; + visited[next] = 1; + Q.push(dum); + cover++; + if (cover == node) + { + ttime = dum.time; + tra = dum.dis; + lf = dum.fuel; + return 1; + } + } + return 0; +} + +void Cal(int f, int br, int sp) +{ + int d; + fuel = f; + brate = br; + speed = sp; + d = BFS(); + if (d) + { + printf("SUCCESS!! Time: %.2lf", ttime); + printf(" Traveled: %.2lf Fuel Left: %.2lf\n", tra, lf); + } + else + printf("FAILURE!! Traveled: %.2lf From Home: %.2lf\n", tra, home); + while (!Q.empty()) + Q.pop(); +} + +void Free() +{ + v.clear(); + memset(visited, 0, 20); +} + +int main() +{ + int f, br, sp, n, ms = 1; + ss temp; + while (scanf("%d%d%d%d", &f, &br, &sp, &node)) + { + if (!f && !br && !sp && !node) + break; + n = node; + while (n--) + { + scanf("%d%d%d", &temp.x, &temp.y, &temp.z); + v.push_back(temp); + } + printf("Mission %d: ", ms++); + Cal(f, br, sp); + Free(); + } + return 0; +} diff --git a/10833.cpp b/10833.cpp new file mode 100644 index 0000000..2378b1e --- /dev/null +++ b/10833.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +int hp1, hp2, m, flimit, fnext; + +typedef struct +{ + int a[16], n, s, g; +} state_t; + +void canonic(state_t *s) +{ + int i, j, t; + for (i = 1; i < s->n; i++) + { + for (t = s->a[i], j = i - 1; j >= 0 && s->a[j] > t; j--) + { + s->a[j + 1] = s->a[j]; + } + s->a[j + 1] = t; + } + if ((s->s + s->n) > m) + { + s->s = m - s->n; + } +} + +int estimate(state_t *s) +{ + int i, h, t; + for (t = 1, i = 0; i < s->n; i++) + if (s->a[i] > t) + { + t = s->a[i]; + } + for (h = 0, i = 0; i < s->n; i++) + { + h += t - s->a[i]; + } + if (s->n == m) + { + return h; /* exact */ + } + if (s->s >= (m - s->n)) + { + return (h + (m - s->n) * t); + } + return (h + (m - s->n) * t + (m - s->n - s->s)); +} + +int find(state_t *s) +{ + state_t t; + int i, k; + k = s->g + estimate(s); + if (k > flimit) + { + if (k < fnext) + { + fnext = k; + } + return 0; + } + if (s->n == m) + { + fnext = k; + return 1; + } + for (k = s->s; k >= 0; k--) + { + t.n = s->n + k; + if (t.n > m) + { + continue; + } + t.g = s->g + 1; + t.s = s->s - k; + for (i = 0; i < s->n; i++) + { + t.a[i] = s->a[i] + 1; + if (t.a[i] == hp1 || t.a[i] == hp2) + { + t.s++; + } + } + for (i = 0; i < k; i++) + { + t.a[s->n + i] = 1; + } + if (t.n == 0) + { + continue; + } + canonic(&t); + for (i = t.n - 1; i >= 0; i--) + { + if (i > 0 && t.a[i - 1] == t.a[i]) + { + continue; + } + t.a[i]++; + if (t.a[i] == hp1 || t.a[i] == hp2) + { + t.s++; + } + if (find(&t)) + { + return 1; + } + if (t.a[i] == hp1 || t.a[i] == hp2) + { + t.s--; + } + t.a[i]--; + } + } + return 0; +} + +int solve() +{ + state_t s; + for (fnext = 0;;) + { + flimit = fnext; + fnext = 0x7FFFFFFF; + s.n = s.g = 0; + s.s = 1; + if (find(&s)) + { + return fnext; + } + } +} + +int main() +{ + int c = 1, t; + for (scanf("%d", &t); c <= t && scanf("%d %d %d", &hp1, &hp2, &m) == 3;) + { + printf("Case %d: %d\n", c++, solve()); + } + return 0; +} diff --git a/10834.cpp b/10834.cpp new file mode 100644 index 0000000..2e9ae63 --- /dev/null +++ b/10834.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +struct vector2d +{ + double x,y; +}; + +typedef pair vect_p; + +vect_p solve(double R, double r, double x, double y) +{ + vect_p soln; + soln.first.x = 0.0f; + soln.first.y = 0.0f; + soln.second.x = 0.0f; + soln.second.y = 0.0f; + + // intersect btw {c=(0, 0), r=|r + r|} & {c=(t.x, t.y), r=r} + double D = sqrt(x * x + y * y); + double r0 = r + R; + double r1 = r; + if (r0 + r1 > D && D > abs(r0 - r1)) + { + double delta = 0.25f * sqrt((D + r0 + r1) * (D + r0 - r1) * (D - r0 + r1) * (-D + r0 + r1)); + double pxa = (x / 2) + (x * (r0 * r0 - r1 * r1)) / (2 * D * D); + double pxb = (-2 * y * delta) / (D * D); + double cx1 = pxa + pxb; + double cx2 = pxa - pxb; + + double pya = (y / 2) + (y * (r0 * r0 - r1 * r1)) / (2 * D * D); + double pyb = (-2 * x * delta) / (D * D); + double cy1 = pya - pyb; + double cy2 = pya + pyb; + + // angle btw (cx, cy) & (R + r, 0) + double alpha1 = atan2(cy1, cx1); + double alpha2 = atan2(cy2, cx2); + + if (alpha1 < 0) + { + alpha1 += 2 * M_PI; + } + if (alpha2 < 0) + { + alpha2 += 2 * M_PI; + } + + // calc angle small circle has rot + double beta1 = (alpha1 * R) / r; + double beta2 = (alpha2 * R) / r; + + // calc pos of p1 + p2 (finally!) from (cx, cy) w/ beta angle + if (alpha1 < alpha2) + { + soln.first.x = cx1 - sin(alpha1 + beta1) * r; + soln.first.y = cy1 + cos(alpha1 + beta1) * r; + soln.second.x = cx2 - sin(alpha2 + beta2) * r; + soln.second.y = cy2 + cos(alpha2 + beta2) * r; + } + else + { + soln.second.x = cx1 - sin(alpha1 + beta1) * r; + soln.second.y = cy1 + cos(alpha1 + beta1) * r; + soln.first.x = cx2 - sin(alpha2 + beta2) * r; + soln.first.y = cy2 + cos(alpha2 + beta2) * r; + } + } + + return soln; +} + +int main() +{ + double R, r, x, y; + int t = 1; + while (scanf("%lf%lf%lf%lf", &R,&r,&x,&y), R, r) + { + vect_p soln = solve(R, r, x, y); + printf("Case %d:\n", t); + if (soln.first.x == 0 && soln.first.y == 0 && soln.second.x == 0 && soln.second.y == 0) + { + printf("IMPOSSIBLE\n"); + } + else + { + printf("%.3lf %.3lf\n%.3lf %.3lf\n", soln.first.x,soln.first.y,soln.second.x,soln.second.y); + } + t++; + } + return 0; +} diff --git a/10835.cpp b/10835.cpp new file mode 100644 index 0000000..fdd43cd --- /dev/null +++ b/10835.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +int n, p, pat[2048], npat, pmask; +long long tab[64][2048]; + +int main() +{ + char s[256]; + int i, j, k, t; + long long r1, r2; + for (t = 1; scanf("%d %d", &n, &npat) == 2 && n > 0; t++) + { + memset(pat, 0, sizeof(pat)); + for (i = p = 0; i < npat && scanf(" %[HTht]", s) == 1; i++) + { + for (j = 0, k = 0; s[j]; j++) + if ((s[j] | 0x20) == 'h') + { + k = (k << 1) | 1; + } + else + { + k <<= 1; + } + p = j; + pat[i] = k; + } + if (p > n || npat == 0) + { + printf("Case %d: 1/1\n", t); + continue; + } + pmask = (1 << p) - 1; + memset(tab, 0, sizeof(tab)); + tab[0][0] = 1; + for (k = 1; k <= n; k++) + { + for (i = 0; i <= pmask; i++) + { + j = (i << 1) & pmask; + tab[k][j] += tab[k - 1][i]; + j = ((i << 1) | 1) & pmask; + tab[k][j] += tab[k - 1][i]; + } + if (k >= p) + { + for (i = 0; i < npat; i++) + { + tab[k][pat[i]] = 0; + } + } + } + for (i = 0, r1 = 0; i <= pmask; i++) + { + r1 += tab[n][i]; + } + if (r1 == 0) + { + printf("Case %d: 0\n", t); + } + else + { + r2 = (1ULL << n); + while (r2 > 1ULL && (r1 & 1ULL) == 0ULL) + { + r2 >>= 1, r1 >>= 1; + } + printf("Case %d: %lld/%lld\n", t, r1, r2); + } + } + return 0; +} diff --git a/10836.cpp b/10836.cpp new file mode 100644 index 0000000..278b4fc --- /dev/null +++ b/10836.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +#define PI 62 + +typedef unsigned long long ULL; + +static int primes[PI] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293}; +static double logff[512], logc[4]; +static int ffact[512][PI], c[4], r[4], n; + +static unsigned eval(int *overfl) +{ + static int e[PI]; + ULL m; + int i, j, k; + for (i = 0; i < PI; i++) + { + for (e[i] = ffact[n][i], j = 0; j < 4; j++) + { + e[i] -= ffact[r[j]][i]; + } + } + for (i = 0; i < 4; i++) + { + for (k = c[i], j = 0; k > 1; j++) + { + while ((k % primes[j]) == 0) + { + k /= primes[j]; + e[j] += r[i]; + } + } + } + i = (e[0] < e[2]) ? e[0] : e[2]; + e[0] -= i; + e[2] -= i; + (*overfl) = 0; + for (i = 0, m = 1; i < PI; i++) + { + for (j = 0; j < e[i]; j++) + { + m = (m * (ULL)primes[i]); + if (m >= 100000000ULL) + { + (*overfl) = 1; + m %= 100000000ULL; + } + } + } + return (unsigned)m; +} + +static void mktab() +{ + int i, j, k; + for (logff[0] = 0., i = 1; i <= 301; i++) + { + logff[i] = logff[i - 1] + log((double)i); + } + memset(ffact, 0, sizeof(ffact)); + for (i = 1; i <= 301; i++) + { + for (j = 0; j < PI; j++) + { + ffact[i][j] = ffact[i - 1][j]; + } + for (j = 0, k = i; k > 1; j++) + { + while ((k % primes[j]) == 0) + { + k /= primes[j]; + ffact[i][j]++; + } + } + } +} + +static void look() +{ + static int g[4], a[4], rmin[4], rmax[4]; + static double rlog; + register int i, j; + double u; + for (i = 0, j = 0; i < 4; i++) + { + j += c[i]; + } + for (i = 0; i < 4; i++) + { + g[i] = (c[i] * n) / j; + rmin[i] = g[i]; + if (rmin[i] < 0) + { + rmin[i] = 0; + } + rmax[i] = g[i] + 3; + if (rmax[i] > n) + { + rmax[i] = n; + } + r[i] = a[i] = rmin[i]; + } + for (rlog = -1;;) + { + for (i = 0, j = 0; i < 4; i++) + { + j += a[i]; + } + if (j == n) + { + for (u = logff[n], i = 0; i < 4; i++) + { + u += (double)a[i] * logc[i] - logff[a[i]]; + } + if (u > rlog) + for (rlog = u, i = 0; i < 4; i++) + { + r[i] = a[i]; + } + } + for (i = 0; i < 4 && ++a[i] > rmax[i]; i++) + { + a[i] = rmin[i]; + } + if (i == 4) + { + break; + } + } +} + +int main() +{ + int i, fl; + unsigned a; + mktab(); + while (scanf("%d%d%d%d%d", &n, &c[0], &c[1], &c[2], &c[3]) == 5) + { + if (n == 0) + { + break; + } + for (i = 0; i < 4; i++) + { + logc[i] = log((double)c[i]); + } + look(); + a = eval(&fl); + if (fl) + { + printf("%.8u\n", a); + } + else + { + printf("%u\n", a); + } + } + return 0; +} diff --git a/10837.cpp b/10837.cpp new file mode 100644 index 0000000..ebbb20d --- /dev/null +++ b/10837.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +int pf[100], npf, best; + +bool isprime(int n) +{ + if (n <= 3) + return (n >= 2); + if ((n & 1) == 0) + { + return false; + } + for (int p = 3; p * p <= n; p += 2) + if (n % p == 0) + { + return false; + } + return true; +} + +void factor(int n) +{ + npf = 0; + while (n > 0 && (n & 1) == 0) + { + n >>= 1; + pf[npf++] = 2; + } + for (int p = 3; p * p <= n; p += 2) + while ((n % p) == 0) + { + n /= p; + pf[npf++] = p; + } + if (n > 1) + { + pf[npf++] = n; + } +} + +void go(int used, int lastp, long long N); + +void go1(int used, int lastp, long long N, int cc, int i) +{ + if (N * (cc + 1) >= best) + { + return; + } + int p = cc + 1; + if (p > lastp && isprime(p)) + { + int u = used; + long long nn = N * p; + go(u, p, nn); + for (int i = 0; i < npf; i++) + { + if (u & (1 << i)) + { + continue; + } + if (pf[i] != p) + { + continue; + } + u |= 1 << i; + nn *= p; + go(u, p, nn); + } + } + while (i < npf) + { + if (used & (1 << i)) + { + i++; + continue; + } + p = pf[i]; + go1(used | (1 << i), lastp, N, cc * pf[i], i); + while (i < npf && pf[i] == p) + { + i++; + } + if (cc == 1) + { + break; + } + } +} + +void go(int used, int lastp, long long N) +{ + if (used == (1 << npf) - 1) + { + best = MIN(best, N); + } + go1(used, lastp, N, 1, 0); +} + +int main() +{ + int n; + for (int cs = 1; scanf("%d", &n) == 1 && n > 0; cs++) + { + factor(n); + best = 200000001; + go(0, 1, 1); + printf("Case %d: %d %d\n", cs, n, best); + } +} diff --git a/10838.cpp b/10838.cpp new file mode 100644 index 0000000..a372d2c --- /dev/null +++ b/10838.cpp @@ -0,0 +1,214 @@ +#include + +using namespace std; + +#define GET(a, i) (((a) >> (i)) & 1) +#define BIT(i) (1 << (i)) + +static int enc84[8][8][8][8]; +static short table[9175040 + 1024]; + +static void mktab() +{ + int a, b, c, d, k; + for (k = 0, a = 0; a < 8; a++) + for (b = a + 1; b < 8; b++) + for (c = b + 1; c < 8; c++) + for (d = c + 1; d < 8; d++) + { + enc84[a][b][c][d] = (k++ << 1); + } +} + +static int encode(int w, int b, int m) +{ + static int a[16]; + int i, j, k; + a[0] = 4; + a[1] = 5; + a[2] = 6; + a[3] = 7; + for (i = 0, j = 0, k = 0; i < 16; i++) + { + if ((((w | b) >> i) & 1) == 0) + { + continue; + } + if (((w >> i) & 1) != 0) + { + a[k++] = j; + } + j++; + } + return ((w | b) * 140) + enc84[a[0]][a[1]][a[2]][a[3]] + m; +} + +static int play(int w, int b, int m) +{ + int win, lose, cfg, k, s, t; + cfg = encode(w, b, m); + if (table[cfg] != 0) + { + return table[cfg]; + } + s = w | b; + if (m == 0) + { + if (b & 0x000F) + { + return (table[cfg] = -1); + } + if (w == 0) + { + return (table[cfg] = -1); + } + for (win = lose = 0, k = 0; k < 12; k++) + { + if (GET(w, k) == 0) + { + continue; + } + if (GET(s, k + 4) == 0) + { + t = play(w ^ BIT(k) ^ BIT(k + 4), b, 1); + if (t > 0 && (win == 0 || t < win)) + { + win = t; + } + else if (t < 0 && t < lose) + { + lose = t; + } + } + if ((k & 3) != 3 && GET(b, k + 5)) + { + t = play(w ^ BIT(k) ^ BIT(k + 5), b ^ BIT(k + 5), 1); + if (t > 0 && (win == 0 || t < win)) + { + win = t; + } + else if (t < 0 && t < lose) + { + lose = t; + } + } + if ((k & 3) != 0 && GET(b, k + 3)) + { + t = play(w ^ BIT(k) ^ BIT(k + 3), b ^ BIT(k + 3), 1); + if (t > 0 && (win == 0 || t < win)) + { + win = t; + } + else if (t < lose) + { + lose = t; + } + } + } + if (win != 0) + { + return table[cfg] = win + 1; + } + else + { + return table[cfg] = lose - 1; + } + } + else + { + if (w & 0xF000) + { + return (table[cfg] = 1); + } + if (b == 0) + { + return (table[cfg] = 1); + } + for (win = lose = 0, k = 4; k < 16; k++) + { + if (GET(b, k) == 0) + { + continue; + } + if (GET(s, k - 4) == 0) + { + t = play(w, b ^ BIT(k) ^ BIT(k - 4), 0); + if (t < 0 && (win == 0 || win < t)) + { + win = t; + } + else if (lose < t) + { + lose = t; + } + } + if ((k & 3) != 3 && GET(w, k - 3)) + { + t = play(w ^ BIT(k - 3), b ^ BIT(k) ^ BIT(k - 3), 0); + if (t < 0 && (win == 0 || win < t)) + { + win = t; + } + else if (lose < t) + { + lose = t; + } + } + if ((k & 3) != 0 && GET(w, k - 5)) + { + t = play(w ^ BIT(k - 5), b ^ BIT(k) ^ BIT(k - 5), 0); + if (t < 0 && (win == 0 || win < t)) + { + win = t; + } + else if (lose < t) + { + lose = t; + } + } + } + if (win != 0) + { + return table[cfg] = win - 1; + } + else + { + return table[cfg] = lose + 1; + } + } +} + +int main() +{ + int w, b, i, c, t; + mktab(); + memset(table, 0, sizeof(table)); + for (scanf("%d", &t) == 1; t-- > 0;) + { + for (w = b = 0, i = 15; i >= 0 && (c = getchar()) != EOF;) + { + if (c == '.') + { + i--; + } + else if (c == 'P') + { + w |= (1 << i--); + } + else if (c == 'p') + { + b |= (1 << i--); + } + } + i = play(w, b, 0); + if (i < 0) + { + printf("black (%d)\n", -i - 1); + } + else + { + printf("white (%d)\n", i - 1); + } + } + return 0; +} diff --git a/10839.cpp b/10839.cpp new file mode 100644 index 0000000..7b302fb --- /dev/null +++ b/10839.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +// @ http://acmph.blogspot.sg/2012/12/uva-10839-curse-of-barbosa.html +// DP +// observe the problem as a graph with 3 nodes, if self loop was allowed in this problem then it would become a matrix multiplication problem, but now we have to count special paths, dp[x][y][z][node] means how many different roads can be made which city 1 is seen x times, city 2 is seen y times, city 3 is seen z times and we are at city 'node'. how to update this? +// dp[x][y][z][node] += dp[x-1][y][z][1] if node != 1 +// dp[x][y][z][node] += dp[x][y-1][z][2] if node != 2 +// dp[x][y][z][node] += dp[x][y][z-2][3] if node != 3 +// we know the basic case dp[0][0][0][1] = 1, in this way we can update our table. +// each time we read n, output = dp[n/3][n/3][n/3][1] is the number of ways we can start from 1 and end at 1, but output counts some paths and it's reverse, some paths not all paths, palindrome paths are counted just once, their reverse is not counted, so if we add the number of palindrome paths, we can divide that number by two. +// how to add palindrome paths? if n is even then there is no palindrome path, but if n is odd, m = (n+1)/2, and we calculate dp[m/3][m/3][m/3][1] and add it to dp[n/3][n/3][n/3][1], the result would be (dp[m/3][m/3][m/3][1] + dp[n/3][n/3][n/3][1] ) / 2. (need biginteger) + +#define NLIMBS 6 +#define FVAL (&ftab[s][a][b][c]) + +typedef unsigned long UL; +typedef unsigned long long ULL; + +typedef struct mp {UL d[NLIMBS];} mp_t; + +static mp_t c_zero, c_one; + +static void mp_add(mp_t *z, const mp_t *x, const mp_t *y) +{ + register int i; + register ULL c; + for (i = 0, c = 0; i < NLIMBS; i++, c >>= 32) + { + c += (ULL)x->d[i] + (ULL)y->d[i]; + z->d[i] = (UL)(c); + } +} + +static void mp_sub(mp_t *z, const mp_t *x, const mp_t *y) +{ + register int i; + register ULL c; + for (i = 0, c = 1; i < NLIMBS; i++, c >>= 32) + { + c += (ULL)x->d[i] + (((ULL)y->d[i]) ^ 0xFFFFFFFFULL); + z->d[i] = (UL)(c); + } +} + +static void mp_set(mp_t *z, const mp_t *x) +{ + register int i; + for (i = 0; i < NLIMBS; i++) + { + z->d[i] = x->d[i]; + } +} + +static void mp_load(mp_t *x, int n) +{ + int i; + for (x->d[0] = n, i = 1; i < NLIMBS; i++) + { + x->d[i] = 0; + } +} + +static unsigned mp_div1(mp_t *x, unsigned d) +{ + register int i; + register ULL c; + for (c = 0, i = NLIMBS - 1; i >= 0; i--) + { + c = (c << 32) | (ULL)x->d[i]; + x->d[i] = (UL)(c / (ULL)d); + c %= (ULL)d; + } + return c; +} + +static char *mp_print(mp_t *x) +{ + static mp_t t; + static char buf1[1024], buf2[1024]; + int i, j; + mp_set(&t, x); + for (i = 0;;) + { + buf1[i++] = mp_div1(&t, 10) + '0'; + for (j = 0; j < NLIMBS; j++) + if (t.d[j] != 0) + { + break; + } + if (j >= NLIMBS) + { + break; + } + } + for (j = 0; i-- > 0;) + { + buf2[j++] = buf1[i]; + } + buf2[j] = '\0'; + return buf2; +} + +// // + +#define MAXM 40 +#define GVAL (>ab[s][a][b][c]) + +static char gtab_u[4][MAXM][MAXM][MAXM], ftab_u[4][MAXM][MAXM][MAXM]; +static mp_t gtab[4][MAXM][MAXM][MAXM], ftab[4][MAXM][MAXM][MAXM]; + +static mp_t *g(int s, int a, int b, int c) +{ + if (a < 0 || b < 0 || c < 0) + { + return &c_zero; + } + if (gtab_u[s][a][b][c]) + { + return GVAL; + } + gtab_u[s][a][b][c] = 1; + if ((a + b + c) == 1) + { + if ((s == 1 && a == 1) || (s == 2 && b == 1) || + (s == 3 && c == 1)) + { + mp_load(GVAL, 0); + return GVAL; + } + mp_load(GVAL, 1); + return GVAL; + } + if (s == 1) + { + mp_add(GVAL, g(2, a, b - 2, c), g(3, a, b, c - 2)); + return GVAL; + } + else if (s == 2) + { + mp_add(GVAL, g(1, a - 2, b, c), g(3, a, b, c - 2)); + return GVAL; + } + else + { + mp_add(GVAL, g(1, a - 2, b, c), g(2, a, b - 2, c)); + return GVAL; + } +} + +static mp_t *f(int s, int a, int b, int c) +{ + if (a < 0 || b < 0 || c < 0) + { + return &c_zero; + } + if (ftab_u[s][a][b][c]) + { + return FVAL; + } + ftab_u[s][a][b][c] = 1; + if ((a + b + c) <= 0) + { + mp_load(FVAL, 0); + return FVAL; + } + if ((a + b + c) == 1) + { + if (a == 1 && s != 1) + { + mp_load(FVAL, 1); + } + else + { + mp_load(FVAL, 0); + } + return FVAL; + } + if (s == 1) + { + mp_add(FVAL, f(2, a, b - 1, c), f(3, a, b, c - 1)); + } + else if (s == 2) + { + mp_add(FVAL, f(1, a - 1, b, c), f(3, a, b, c - 1)); + } + else + { + mp_add(FVAL, f(1, a - 1, b, c), f(2, a, b - 1, c)); + } + return FVAL; +} + +int main() +{ + int n, m, c, t; + mp_t *fv, *gv, u; + mp_load(&c_zero, 0); + mp_load(&c_one, 1); + memset(gtab_u, 0, sizeof(gtab_u)); + memset(ftab_u, 0, sizeof(ftab_u)); + scanf("%d", &t); + for (c = 1; c <= t && scanf("%d", &n) == 1; c++) + { + if (n == 1) + { + printf("Case %d: 1\n", c); + continue; + } + m = (n - 1) / 3; + fv = f(1, m, m, m); + gv = g(1, m - 1, m, m); + mp_sub(&u, fv, gv); + mp_div1(&u, 2); + mp_add(&u, &u, gv); + printf("Case %d: %s\n", c, mp_print(&u)); + } + return 0; +} diff --git a/1084.cpp b/1084.cpp new file mode 100644 index 0000000..3621006 --- /dev/null +++ b/1084.cpp @@ -0,0 +1,370 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:60000000") + +//Type Definition +typedef long long ll; +typedef pair pii; +typedef vector vi; +typedef vector vvi; +typedef vector vs; +typedef map msi; +typedef map mii; + +#define ERR 1e-9 +#define PI 3.141592653589793 + +#define REP(i, n) for (i = 0; i < n; i++) +#define FOR(i, p, k) for (i = p; i < k; i++) +#define FOREACH(it, x) for (__typeof((x).begin()) it = (x.begin()); it != (x).end(); ++it) + +#define Sort(x) sort(x.begin(), x.end()) +#define Reverse(x) reverse(x.begin(), x.end()) +#define MP(a, b) make_pair(a, b) +#define Clear(x, with) memset(x, with, sizeof(x)) +#define SZ(x) (int)x.size() +#define pb push_back +#define popcount(i) __builtin_popcountll(i) +#define gcd(a, b) __gcd(a, b) +#define lcm(a, b) ((a) * ((b) / gcd(a, b))) +#define two(X) (1 << (X)) +#define twoL(X) (((ll)(1)) << (X)) +#define setBit(mask, i) (mask | two(i)) +#define contain(S, X) (((S)&two(X)) != 0) +#define fs first +#define sc second +#define CS c_str() +#define EQ(a, b) (fabs((a) - (b)) < ERR) +#define Unique(store) store.resize(unique(store.begin(), store.end()) - store.begin()); +#define Find(x, y) ((int)x.find(y)) +#define all(a) a.begin(), a.end() +#define rall(a) a.rbegin(), a.rend() + +//For debugging +#define debug_array(a, n) \ + for (ll i = 0; i < n; i++) \ + cerr << a[i] << " "; \ + cerr << endl; +#define debug_matrix(mat, row, col) \ + for (ll i = 0; i < row; i++) \ + { \ + for (ll j = 0; j < col; j++) \ + cerr << mat[i][j] << " "; \ + cerr << endl; \ + } +#define deb(a) cout << #a << " #-> " << a << endl +#define debug(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << "\t"; + return *this; + } +} dbg; + +//Important Functions +template +void setmax(T &a, T b) +{ + if (a < b) + { + a = b; + } +} +template +void setmin(T &a, T b) +{ + if (b < a) + { + a = b; + } +} +template +T Abs(T x) +{ + return x > 0 ? x : -x; +} +template +inline T sqr(T x) +{ + return x * x; +} +template +inline T Mod(T n, T m) +{ + return (n % m + m) % m;//For Positive Negative No. +} +template +string toString(T n) +{ + ostringstream oss; + oss << n; + oss.flush(); + return oss.str(); +} +ll toInt(string s) +{ + ll r = 0; + istringstream sin(s); + sin >> r; + return r; +} +bool isVowel(char ch) +{ + ch = tolower(ch); + if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') + { + return true; + } + return false; +} +bool isUpper(char c) +{ + return c >= 'A' && c <= 'Z'; +} +bool isLower(char c) +{ + return c >= 'a' && c <= 'z'; +} +ll Pow(ll B, ll P) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B); + } + P /= 2; + B = (B * B); + } + return R; +} +ll BigMod(ll B, ll P, ll M) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B) % M; + } + P /= 2; + B = (B * B) % M; + } + return R; +} +void binprint(ll mask, ll n) +{ + ll i; + string s = ""; + do + { + s += (mask % 2 + '0'); + mask /= 2; + } while (mask); + Reverse(s); + s = string(max(n - SZ(s), 0LL), '0') + s; + for (i = SZ(s) - n; i < SZ(s); i++) + { + printf("%c", s[i]); + } + printf("\n"); +} +void ASCII_Chart() +{ + ll i, j; + int k; + printf("ASCII Chart:(30-129)\n"); + FOR(i, 30, 50) + { + REP(j, 5) + { + k = i + j * 20; + printf("%3d---> '%c' ", k, k); + } + printf("\n"); + } +} + +//int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; //Hexagonal Direction + +//#include //for using getch(); + +//struct edge{ int cost,node; edge(int _cost=0,int _node=0){cost=_cost;node=_node;}bool operator<(const edge &b)const {return cost>b.cost;}}; // Min Priority Queue +//bool comp(edge a,edge b){ return a.cost < b.cost;} //Asc Sort by cost + +struct P +{ + int x, y; + P(int xi = 0, int yi = 0) + { + x = xi; + y = yi; + }; + void scan() + { + scanf("%d%d", &x, &y); + } +}; + +P p[10], a[10], pvt; +int n, r; + +double dis(P a, P b) +{ + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); +} +P MV(P a, P b) +{ + return P(b.x - a.x, b.y - a.y); +} +int CP(P a, P b) +{ + return a.x * b.y - a.y * b.x; +} + +bool com(P a, P b) +{ + int c = CP(MV(pvt, a), MV(pvt, b)); + if (c == 0) + { + return dis(pvt, a) < dis(pvt, b); + } + return c > 0; +} + +double calL(int msk) +{ + // deb(msk); + if (msk == 0) + { + return 0; + } + int i, c = 0; + for (i = 0; i < n; i++) + if (msk & (1 << i)) + { + a[c++] = p[i]; + } + if (c == 1) + { + return 2 * PI * r; + } + else if (c == 2) + { + return dis(a[0], a[1]) * 2 + 2 * PI * r; + } + pvt = a[0]; + for (i = 1; i < c; i++) + if (pvt.y > a[i].y) + { + pvt = a[i]; + } + else if (pvt.y == a[i].y && pvt.x > a[i].x) + { + pvt = a[i]; + } + int sl = 2; + for (i = 2; i < c; i++) + if (CP(MV(a[0], a[1]), MV(a[0], a[i])) == 0) + { + sl++; + } + if (sl == c) + { + double d = 0; + for (i = 0; i < c; i++) + { + d = max(d, dis(pvt, a[i])); + } + return d * 2 + 2 * PI * r; + } + sort(a, a + c, com); + // deb(c); + int j = 1; + for (i = 2; i < c; i++) + { + // debug("paisi",msk,j); + while (j > 1 && CP(MV(a[j - 1], a[j]), MV(a[j - 1], a[i])) <= 0) + { + j--; + // deb(j); + } + // debug(i,j); + // deb(i); + a[++j] = a[i]; + } + a[++j] = a[0]; + double p = 0; + for (i = 0; i < j; i++) + { + p += dis(a[i], a[i + 1]); + } + return p + 2 * PI * r; +} + +double dp[5 + (1 << 9)]; +double stor[5 + (1 << 9)]; + +double go(int msk) +{ + if (msk == 0) + { + return 0; + } + double &re = dp[msk]; + if (re > -1) + { + return re; + } + re = stor[msk]; + // re=0; + int a, b; + a = msk; + while (a) + { + a = (a - 1) & msk; + b = msk ^ a; + re = min(re, go(b) + stor[a]); + } + return re; +} + +int main() +{ + int i, j, test, Case = 1, x, y; + double ans; + while (scanf("%d%d", &n, &r) == 2) + { + if (n == 0 && r == 0) + { + break; + } + REP(i, n) + p[i].scan(); + ans = 0; + for (i = 0; i < two(n); i++) + { + dp[i] = -3; + stor[i] = calL(i); + } + // cout<<"OK"< + +using namespace std; + +struct edge +{ + int to, weight, lift; + edge(int t, int w, int l) : to(t), weight(w), lift(l) {} + bool operator<(const edge &that) const + { + return weight > that.weight; + } +}; + +int main() +{ + int n, k; + while (cin >> n >> k) + { + vector g[100]; + vector floors[n];//List of floors visited by elevator i-th + int t[n]; + for (int i = 0; i < n; ++i) + { + cin >> t[i]; + } + string line; + getline(cin, line); + for (int i = 0; i < n; ++i) + { + getline(cin, line); + stringstream ss(line); + int x, y; + ss >> x; + floors[i].push_back(x); + while (ss >> y) + { + floors[i].push_back(y); + g[x].push_back(edge(y, (y - x) * t[i], i)); + g[y].push_back(edge(x, (y - x) * t[i], i)); + x = y; + } + } + int d[100][n]; + for (int i = 0; i < 100; ++i) + for (int j = 0; j < n; ++j) + { + d[i][j] = INT_MAX; + } + priority_queue q; + for (int j = 0; j < n; ++j) + { + if (floors[j].front() == 0) + { + //cout << "Starting at elevator " << j << " ends at floor " << floors[j].back() << endl; + d[0][j] = floors[j].back() * t[j]; + q.push(edge(0, d[0][j], j)); + } + } + if (k == 0) + { + cout << "0" << endl; + continue; + } + while (q.size()) + { + edge top = q.top(); + q.pop(); + if (top.to == k) + { + break; + } + if (d[top.to][top.lift] < top.weight) + { + continue; + } + for (int i = 0; i < g[top.to].size(); ++i) + { + edge u = g[top.to][i]; + int tmp; + if ((tmp = top.weight + u.weight + (top.lift != u.lift ? (max(top.to - floors[u.lift].front(), floors[u.lift].back() - top.to) * t[u.lift] + 5) : 0)) < d[u.to][u.lift]) + { + d[u.to][u.lift] = tmp; + q.push(edge(u.to, tmp, u.lift)); + } + } + } + int a = *min_element(d[k], d[k] + n); + if (a < INT_MAX) + { + cout << a << endl; + } + else + { + cout << "IMPOSSIBLE" << endl; + } + } + return 0; +} diff --git a/10842.cpp b/10842.cpp new file mode 100644 index 0000000..20a12f6 --- /dev/null +++ b/10842.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define maxn 102 +int P[maxn], R[maxn], N, E; +struct ss +{ + int u, v, cost; +}; +ss edge[10000]; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return y->cost - x->cost; +} +void Ini() +{ + int i; + for (i = 0; i <= N; i++) + { + P[i] = i; + R[i] = 0; + } +} +int Find(int n) +{ + if (P[n] != n) + { + P[n] = Find(P[n]); + } + return P[n]; +} +void Link(int x, int y) +{ + if (R[x] > R[y]) + { + P[y] = x; + } + else + { + P[x] = y; + if (R[x] == R[y]) + { + R[y]++; + } + } +} +int MST() +{ + int x, y, min = 1000000, i; + for (i = 0; i < E; i++) + { + x = Find(edge[i].u); + y = Find(edge[i].v); + if (x != y) + { + Link(x, y); + if (min > edge[i].cost) + { + min = edge[i].cost; + } + } + } + return min; +} +void Cal() +{ + qsort(edge, E, sizeof(ss), com); + cout << MST() << endl; +} +int main() +{ + int ks, k = 1, u, v, i, c; + cin >> ks; + while (ks--) + { + cin >> N >> E; + Ini(); + for (i = 0; i < E; i++) + { + cin >> edge[i].u >> edge[i].v >> edge[i].cost; + } + cout << "Case #" << k++ << ": "; + Cal(); + } + return 0; +} diff --git a/10843.cpp b/10843.cpp index 8825b3b..5e9fc03 100644 --- a/10843.cpp +++ b/10843.cpp @@ -1,23 +1,54 @@ -#include -const long long MOD = 2000000011LL; +#include -long long bigMod(long long a, long long b) { - long long x = 1, y = a; - while (b > 0) { - if (b % 2 == 1) - x = (x * y) %MOD; - y = (y * y) %MOD; - b /= 2; +using namespace std; + +/* +10843 +*/ +typedef long long ss; +ss N; +const ss X = 2000000011; +ss Bmod(ss n, ss m) +{ + ss r, mod; + if (m == 0) + { + return 1; + } + if (m == 1) + { + return n; + } + if (m % 2) + { + mod = (n * Bmod(n, m - 1)) % X; + } + else + { + r = (Bmod(n, m / 2)); + r %= X; + mod = (r * r) % X; } - return x %MOD; + return mod; } - -int main() { - long long T, N, t; - scanf("%lld", &T); - for (t = 1; t <= T; t++) { +void Cal() +{ + if (N <= 1) + { + printf("1\n"); + return; + } + printf("%lld\n", Bmod(N, N - 2)); +} +int main() +{ + ss k, x = 1; + scanf("%lld", &k); + while (k--) + { scanf("%lld", &N); - printf("Case #%lld: %lld\n", t, bigMod(N, N - 2)); + printf("Case #%lld: ", x++); + Cal(); } return 0; } diff --git a/10844.cpp b/10844.cpp new file mode 100644 index 0000000..92fff6f --- /dev/null +++ b/10844.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +const int base = 1000000000; +class BigNumber +{ + int d[200], size; + +public: + BigNumber() + { + size = 0; + } + BigNumber(int n) + { + d[0] = n; + size = 1; + } + BigNumber &operator=(const BigNumber &rhs) + { + size = rhs.size; + for (int i = 0; i < size; i++) + { + d[i] = rhs.d[i]; + } + return *this; + } + BigNumber operator+(const BigNumber &rhs) + { + BigNumber ans; + int carry = 0, p, len = size, rhslen = rhs.size; + for (p = 0; p < len && p < rhslen; p++) + { + int temp = d[p] + rhs.d[p] + carry; + ans.d[p] = temp % base; + carry = temp / base; + } + for (; p < len; p++) + { + int temp = d[p] + carry; + ans.d[p] = temp % base; + carry = temp / base; + } + for (; p < rhslen; p++) + { + int temp = rhs.d[p] + carry; + ans.d[p] = temp % base; + carry = temp / base; + } + if (carry) + { + ans.d[p++] = carry; + } + ans.size = p; + return ans; + } + void print() + { + printf("%d", d[size - 1]); + for (int i = size - 2; i >= 0; i--) + { + printf("%09d", d[i]); + } + puts(""); + } +} f[2][901], t[901]; +void init() +{ + t[0] = BigNumber(1), t[1] = BigNumber(1), t[2] = BigNumber(2); + f[0][1] = BigNumber(1), f[0][2] = BigNumber(2); + for (int i = 3; i <= 900; i++) + { + int p = (i + 1) & 1, n = i & 1; + f[n][1] = f[p][i - 1]; + for (int j = 2; j <= i; j++) + { + f[n][j] = f[p][j - 1] + f[n][j - 1]; + } + t[i] = f[n][i]; + } +} +int main() +{ + init(); + int n; + while (scanf("%d", &n), n) + { + printf("%d, ", n); + t[n].print(); + } + return 0; +} diff --git a/10846.cpp b/10846.cpp new file mode 100644 index 0000000..c3b44a7 --- /dev/null +++ b/10846.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +typedef struct +{ + long long p, q; +} rat_t; + +long long m, n; +rat_t t1, t2; + +void solve() +{ + long long k, r; + if (m <= 1) + { + printf("0\n"); + return; + } + r = ((t2.p * (m * m - 1LL)) / (t2.q * m * n)) - ((t1.p * (m * m - 1LL)) / (t1.q * m * n)); + if (r < 0) + { + r = 0; + } + k = ((t2.p * (m - 1LL)) / (t2.q * m * n)) - ((t1.p * (m - 1LL)) / (t1.q * m * n)); + r -= k; + if (r < 0) + { + r = 0; + } + printf("%lld\n", r); +} + +void get(rat_t *t) +{ + long long hr, mn, num, den; + scanf("%lld : %lld %lld / %lld", &hr, &mn, &num, &den); + if (den <= 0) + { + num = 0; + den = 1; + } + t->p = hr * n * den + mn * den + num; + t->q = den; +} + +int main() +{ + int t, q; + for (t = 1; scanf("%lld %lld %d", &m, &n, &q) == 3; t++) + { + if (m == 0 && n == 0 && q == 0) + { + break; + } + if (t != 1) + { + printf("\n"); + } + printf("Planet %d:\n", t); + while (q-- > 0) + { + get(&t1); + get(&t2); + solve(); + } + } + return 0; +} diff --git a/10847.cpp b/10847.cpp new file mode 100644 index 0000000..730e8fd --- /dev/null +++ b/10847.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +static int isalp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +static int go(char *s) +{ + static int c[256], g, n; + register int i; + for (i = 'a'; i <= 'z'; i++) + { + c[i] = 0; + } + for (i = 'A'; i <= 'Z'; i++) + { + c[i] = 0; + } + for (n = 0, g = 0; *s; s++) + { + if (isalp[(int)*s]) + { + c[*s] ^= 1; + n++; + } + else if (*s == '=') + { + if (n-- < 2) + { + return 0; + } + } + else if (*s == '-') + { + if (n < 1) + { + return 0; + } + g ^= 1; + } + else + { + return 0; + } + } + if (n != 1) + { + return 0; + } + if (g) + { + return 1; + } + for (i = 'a'; i <= 'z'; i++) + { + if (c[i]) + { + return 1; + } + } + for (i = 'A'; i <= 'Z'; i++) + { + if (c[i]) + { + return 1; + } + } + return 2; +} + +int main() +{ + static char s[512]; + static const char *r[] = {"incorrect\n", "formula\n", "tautology\n"}; + static int t; + while (gets(s) && sscanf(s, "%d", &t) != 1) + ; + while (t-- > 0 && gets(s)) + { + printf(r[go(s)]); + } + return 0; +} diff --git a/10848.cpp b/10848.cpp new file mode 100644 index 0000000..f90e475 --- /dev/null +++ b/10848.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +char s1[10002], num[10002], s2[20002]; +int l1, l2, K, F1[30], F2[30]; + +int Pro(int k) +{ + int i, f = 0, l3; + l1 = strlen(s1); + l2 = strlen(s2); + l3 = strlen(num); + if (k != l1 + l2 + l3 + 2) + return 0; + if (l3 > 4) + return 0; + for (i = 0; i < l1; i++) + { + if (isupper(s1[i]) || !isalpha(s1[i])) + return 0; + F1[s1[i] - 'a']++; + } + for (i = 0; i < l2; i++) + { + if (isupper(s2[i]) || !isalpha(s2[i])) + return 0; + F2[s2[i] - 'a']++; + } + if (l1 == 0 || l1 > 1000 || l2 > 2000 || l2 == 0) + return 0; + for (i = 0; num[i]; i++) + if (!isdigit(num[i])) + return 0; + K = atoi(num); + if (K > 1000) + return 0; + return 1; +} + +int isP(int lo, int up) +{ + if (s2[lo] != s2[up]) + return 0; + if (lo >= up) + return 1; + return isP(lo + 1, up - 1); +} + +int App() +{ + int i; + for (i = 0; i < 26; i++) + if (F1[i] > 0 && F2[i] == 0) + return 0; + return 1; +} + +int Fre() +{ + for (int i = 0; i < 26; i++) + if (F1[i] > F2[i]) + return 0; + return 1; +} + +int From() +{ + int i, j, k = 0, f; + for (i = 0; i < l1; i++) + { + f = 1; + for (j = k; j < l2; j++) + { + if (s1[i] == s2[j]) + { + f = 0; + k = j + 1; + break; + } + } + if (f) + return 0; + } + return 1; +} + +int Len() +{ + if (l2 != l1 + K) + return 0; + return 1; +} + +int Len2() +{ + if (K >= l1) + return 0; + return 1; +} + +int Cal(int k) +{ + int f = 0, d; + if (!Pro(k)) + { + printf("FFFFFFF"); + return 0; + } + printf("T");// + d = isP(0, l2 - 1); + if (!d) + { + printf("F"); + f = 1; + } + else + { + printf("T"); + } + d = App(); + if (!d) + { + printf("F"); + f = 1; + } + else + { + printf("T");// + } + d = Fre(); + if (!d) + { + printf("F"); + f = 1; + } + else + { + printf("T");// + } + d = From(); + if (!d) + { + printf("F"); + f = 1; + } + else + { + printf("T");// + } + d = Len(); + if (!d) + { + printf("F"); + f = 1; + } + else + { + printf("T");// + } + d = Len2(); + if (!d) + { + printf("F"); + f = 1; + } + else + printf("T");// + if (f) + return 0; + return 1; +} + +int main() +{ + int d, l; + char input[5002]; + while (gets(input)) + { + l = strlen(input); + sscanf(input, "%s%s%s", s1, num, s2); + d = Cal(l); + if (d) + printf(" The solution is accepted\n"); + else + printf(" The solution is not accepted\n"); + memset(F1, 0, sizeof(int) * 30); + memset(F2, 0, sizeof(int) * 30); + } + return 0; +} diff --git a/10849.cpp b/10849.cpp new file mode 100644 index 0000000..0edc2c3 --- /dev/null +++ b/10849.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int main() +{ + int c, t, n, r1, c1, r2, c2; + scanf("%d", &c); + while (c--) + { + scanf("%d%d", &t, &n); + while (t--) + { + scanf("%d%d%d%d", &r1, &c1, &r2, &c2); + if ((r1 + c1 + r2 + c2) % 2) + { + printf("no move\n"); + continue; + } + n = 0; + if (r1 + c1 == r2 + c2) + { + n |= 1; + } + if (r1 - c1 == r2 - c2) + { + n |= 2; + } + switch (n) + { + case 0: + printf("2\n"); + break; + case 3: + printf("0\n"); + break; + default: + printf("1\n"); + break; + } + } + } + return 0; +} diff --git a/1085.cpp b/1085.cpp new file mode 100644 index 0000000..8acd328 --- /dev/null +++ b/1085.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +const int UP = 0; +const int FLOOR = 1; +const int DOWN = 2; +const int INF = 0x7fffffff; + +int deck[33]; +char s[10], c; +int n; + +inline int abs(int x) +{ + int y = x >> 31; + return (x + y) ^ y; +} + +inline int getScore(int a, int b, int c) +{ + int ret = abs(a) + abs(b) + abs(c); + int sgn = (a > 0) + (b > 0) + (c > 0); + if (sgn > 1) + return ret; + else + return -ret; +} + +struct State +{ + int card[8], type[8]; + int hold[2]; + int pos, score; + + void make_start() + { + for (int i = 0; i < 8; ++i) + { + card[i] = deck[i]; + type[i] = (i % 2 == 0) ? UP : DOWN; + } + hold[0] = hold[1] = score = 0; + pos = 8; + } + + bool isFinal() + { + if (pos == 2 * n) + { + score += hold[0] + hold[1]; + hold[0] = hold[1] = 0; + return true; + } + return false; + } + + State child() const + { + State s; + memcpy(&s, this, sizeof(s)); + s.pos = pos + 1; + return s; + } + + void expand(int player, vector &ret) const + { + int &cur = deck[pos]; + if (hold[player] == 0) + { + State s(child()); + s.hold[player] = cur; + ret.push_back(s); + } + for (int i = 0; i < 7; ++i) + if (type[i] == DOWN && type[i + 1] == UP) + { + State s(child()); + s.score += getScore(card[i], card[i + 1], cur); + s.type[i] = s.type[i + 1] = FLOOR; + s.card[i] = s.card[i + 1] = cur; + ret.push_back(s); + if (hold[player] != 0) + { + State s(child()); + s.score += getScore(card[i], card[i + 1], hold[player]); + s.type[i] = s.type[i + 1] = FLOOR; + s.card[i] = s.card[i + 1] = hold[player]; + s.hold[player] = cur; + ret.push_back(s); + } + } + if (hold[player] != 0) + { + for (int i = 0; i < 7; ++i) + if (type[i] == FLOOR && type[i + 1] == FLOOR && card[i] == card[i + 1]) + { + State s(child()); + s.score += getScore(card[i], hold[player], cur); + s.type[i] = UP; + s.type[i + 1] = DOWN; + s.card[i] = cur; + s.card[i + 1] = hold[player]; + s.hold[player] = 0; + ret.push_back(s); + swap(s.card[i], s.card[i + 1]); + ret.push_back(s); + } + } + } +}; + +int alphabeta(State &s, int player, int alpha, int beta) +{ + if (s.isFinal()) + return s.score; + vector children; + s.expand(player, children); + int n = children.size(); + for (int i = 0; i < n; ++i) + { + int v = alphabeta(children[i], player ^ 1, alpha, beta); + if (!player) + alpha = max(alpha, v); + else + beta = min(beta, v); + if (beta <= alpha) + break; + } + return !player ? alpha : beta; +} + +int main() +{ + int t = 0; + while (scanf("%s", s) != EOF && *s != 'E') + { + scanf("%d", &n); + for (int i = 0; i < 2 * n; ++i) + { + scanf("%d%c", &deck[i], &c); + if (c == 'B') + deck[i] = -deck[i]; + } + int start = !(deck[0] > 0); + State beg; + beg.make_start(); + int ans = alphabeta(beg, start, -INF, INF); + printf("Case %d: ", ++t); + if (s[0] == 'B') + ans = -ans; + if (ans == 0) + puts("Axel and Birgit tie"); + else if (ans > 0) + printf("%s wins %d\n", s, ans); + else + printf("%s loses %d\n", s, -ans); + } +} diff --git a/10850.cpp b/10850.cpp new file mode 100644 index 0000000..5c763a4 --- /dev/null +++ b/10850.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +struct +{ + int x, y, t; +} meet[65536]; +struct +{ + int y, t; +} adj[32][128]; +int adjn[32], n, m; + +int search() +{ + static int v[32], dist[32], q[65536], head, tail; + int i, k, x, y; + memset(v, 0, sizeof(v)); + head = tail = 0; + q[tail++] = 0; + dist[0] = 0; + while (head < tail) + { + x = q[head++]; + v[x] = 1; + for (i = 0; i < adjn[x]; i++) + { + y = adj[x][i].y; + if (adj[x][i].t < (dist[x] % 100)) + { + k = ((dist[x] / 100) + 1) * 100 + adj[x][i].t; + } + else + { + k = ((dist[x] / 100) + 0) * 100 + adj[x][i].t; + } + if (v[y] == 0 || (dist[y] > k)) + { + v[y] = 1; + dist[y] = k; + q[tail++] = y; + } + } + } + for (k = 0, i = 1; i < n; i++) + if (dist[i] > k) + { + k = dist[i]; + } + return k; +} + +int solve() +{ + static int cl[32][32]; + int i, j, k; + memset(cl, 0, sizeof(cl)); + for (i = 0; i < m; i++) + { + cl[meet[i].x][meet[i].y] = cl[meet[i].y][meet[i].x] = 1; + } + for (i = 0; i < n; i++) + { + cl[i][i] = 1; + } + for (k = 0; k < n; k++) + for (i = 0; i < n; i++) + if (cl[i][k]) + for (j = 0; j < n; j++) + { + cl[i][j] |= cl[k][j]; + } + for (i = 1; i < n; i++) + if (cl[0][i] == 0) + { + return -1; + } + return search(); +} + +int main() +{ + int t, k, x, y, v, z; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &k) == 2;) + { + memset(adjn, 0, sizeof(adjn)); + for (m = 0; k-- > 0 && scanf("%d %d %d", &x, &y, &v) == 3;) + { + for (x--, y--; v-- > 0 && scanf("%d", &z) == 1;) + { + meet[m].x = x; + meet[m].y = y; + meet[m++].t = z; + adj[x][adjn[x]].y = y; + adj[x][adjn[x]++].t = z; + adj[y][adjn[y]].y = x; + adj[y][adjn[y]++].t = z; + } + } + printf("%d\n", solve()); + } + return 0; +} diff --git a/10851.cpp b/10851.cpp new file mode 100644 index 0000000..9061d44 --- /dev/null +++ b/10851.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int n; + string input; + string hieroglyphs[10]; + cin >> n; + while (n--) + { + getline(cin, input); + for (int i = 0; i < 10; i++) + { + getline(cin, hieroglyphs[i]); + } + int m = hieroglyphs[0].size() - 2; + for (int i = 0; i < m; i++) + { + char decoded = 0; + for (int j = 0; j < 8; j++) + { + if (hieroglyphs[j + 1][i + 1] == '/') + { + continue; + } + decoded += pow(2, j); + } + cout << decoded; + } + cout << endl; + } + return 0; +} diff --git a/10852.cpp b/10852.cpp index 232ad94..f7eb60a 100644 --- a/10852.cpp +++ b/10852.cpp @@ -1,47 +1,57 @@ -#include -#include -#include -using namespace std; +#include -#define MAX 10010 -bool prime[MAX]; -vector< int > P; +using namespace std; -void sieve() { - memset(prime, true, sizeof prime); - prime[1] = prime[0] = false; - - for (int i = 2; i < MAX; i++) { - if (prime[i]) { - P.push_back(i); - for (int j = i * i; j < MAX; j += i) - prime[j] = false; +bool is_prime(int n) +{ + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; } } - //printf("%d\n", P.size()); + return true; } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +struct result +{ + int p; + int diff; +}; - sieve(); - - int T; - scanf("%d", &T); - while (T--) { - int N; - scanf("%d", &N); - int Max = -1, X = -1; - for (int i = 0; i < P.size(); i++) { - if (P[i] > N) break; - int x = P[i]; - int p = (int) N / x; - if (p * x <= N && N < (p + 1) * x && Max < N - p * x) { - Max = N - p * x; - X = x; +int main() +{ + int t, n; + vector primes; + result best; + primes.push_back(2); + for (int i = 3; i < 15000; i += 2) + if (is_prime(i)) + { + primes.push_back(i); + } + cin >> t; + while (t--) + { + cin >> n; + best.diff = 0; + for (int i = 0, sz = primes.size(); primes[i] <= n; i++) + { + for (int p = 0; p * primes[i] <= n; p++) + { + if ((p + 1) * primes[i] <= n) + { + continue; + } + if (n - primes[i] * p > best.diff) + { + best.p = primes[i]; + best.diff = n - primes[i] * p; + } } } - printf("%d\n", X); + cout << best.p << endl; } + return 0; } diff --git a/10853.cpp b/10853.cpp new file mode 100644 index 0000000..e795635 --- /dev/null +++ b/10853.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +long long L, amin, amax, bmin, bmax; +char solve() +{ + long long d, q; + if (L <= amax) + { + return 'A'; + } + d = amax - amin + bmin - bmax; + q = (L - 1) / (amin + bmax); + if (d < 0 && q > ((bmax - bmin - amax + d + 1) / d)) + { + return 'B'; + } + return (((L - 1) % (amin + bmax)) < (amax + q * d)) ? 'A' : 'B'; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%lld %lld %lld %lld %lld", &L, &amin, &amax, &bmin, &bmax); + printf("%c\n", solve()); + } +} diff --git a/10854.cpp b/10854.cpp new file mode 100644 index 0000000..ffe0177 --- /dev/null +++ b/10854.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define END "ENDPROGRAM" +#define S "S" +#define IF "IF" +#define ELSE "ELSE" +#define ENDIF "END_IF" +unsigned int parse(string end) +{ + unsigned int ret = 1, temp = 0; + string t; + while (cin >> t) + { + //cout << t <<" " << end<< endl; + if (t == S) + { + continue; + } + if (t == end) + { + break; + }; + if (t == IF) + { + temp = parse(ELSE); + temp += parse(ENDIF); + ret *= temp; + } + } + return ret; +} +int main() +{ + int te; + cin >> te; + while (te--) + { + cout << parse(END) << endl; + ; + } + return 0; +} diff --git a/10855.cpp b/10855.cpp new file mode 100644 index 0000000..1c69aa6 --- /dev/null +++ b/10855.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int n1, n2; +char *s1, *s2; + +void rot90(char *s, int n) +{ + char *t; + int i, j; + t = (char *)malloc(n * n + 1024); + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + t[j * n + (n - 1 - i)] = s[i * n + j]; + } + for (i = 0, j = n * n; i < j; i++) + { + s[i] = t[i]; + } + free(t); +} + +int check(int y, int x) +{ + int i, j, k; + char *s, *t; + s = s1 + y * n1; + t = s2; + for (i = 0; i < n2; i++, s += n1) + for (k = x, j = 0; j < n2; j++, k++) + if (s[k] != *t++) + { + return 0; + } + return 1; +} + +int count() +{ + int y, x, m, r; + for (y = 0, m = n1 - n2, r = 0; y <= m; y++) + for (x = 0; x <= m; x++) + { + r += check(y, x); + } + return r; +} + +int main() +{ + int i, j, c; + while (scanf("%d %d", &n1, &n2) == 2 && n1 > 0 && n2 > 0) + { + s1 = (char *)malloc(n1 * n1 + 1024); + s2 = (char *)malloc(n2 * n2 + 1024); + for (i = 0, j = n1 * n1; i < j && (c = getchar()) != EOF;) + if (isalpha(c)) + { + s1[i++] = c; + } + for (i = 0, j = n2 * n2; i < j && (c = getchar()) != EOF;) + if (isalpha(c)) + { + s2[i++] = c; + } + for (i = 0; i < 3; i++, rot90(s2, n2)) + { + printf("%d ", count()); + } + printf("%d\n", count()); + free(s2); + free(s1); + } + return 0; +} diff --git a/10856.cpp b/10856.cpp new file mode 100644 index 0000000..089ef3d --- /dev/null +++ b/10856.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +/* +10856 +*/ +#define mx 3000000 +int P[mx + 2], tp; +char Fg[mx + 2]; +typedef long long ss; +void Table() +{ + ss i, j, k; + for (i = 2; i <= mx;) + { + P[i]++; + for (j = i + i; j <= mx; j += i) + { + Fg[j] = 1; + P[j]++; + } + for (j = i * i; j <= mx; j *= i) + { + P[j]++; + for (k = j + j; k <= mx; k += j) + { + P[k]++; + } + } + for (++i; Fg[i]; i++) + ; + } + for (i = 2; i <= mx; i++) + { + P[i] += P[i - 1]; + } +} +int Count(int n) +{ + int i, p = 0; + if (Fg[n] > 0) + { + return Fg[n]; + } + for (i = 0; P[i] <= n && i < tp; i++) + { + for (int j = P[i]; j <= n; j *= P[i]) + { + p += n / j; + } + } + Fg[n] = p; + return p; +} +int BinarySearch(int key) +{ + int lo = 2, up = mx, mid, fac, lfac, rfac; + mid = (lo + up) / 2; + if (key == 0) + { + return 0; + } + if (key == 1) + { + return 2; + } + while (lo < up) + { + fac = P[mid]; + if (fac > key) + { + up = mid - 1; + } + else if (fac < key) + { + lo = mid + 1; + } + else + { + return mid; + } + mid = (lo + up) / 2; + } + fac = P[mid]; + if (fac != key) + { + return -1; + } + return mid; +} +int main() +{ + int n, k = 1, d; + Table(); + while (scanf("%d", &n) && n >= 0) + { + d = BinarySearch(n); + if (d < 0) + { + printf("Case %d: Not possible.\n", k++); + } + else + { + printf("Case %d: %d!\n", k++, d); + } + } + return 0; +} diff --git a/10858.cpp b/10858.cpp new file mode 100644 index 0000000..b2d47e0 --- /dev/null +++ b/10858.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +vector> result; + +void solve(int n, int div, deque &q) +{ + for (int i = div; i * i <= n; i++) + { + if (n % i == 0) + { + q.push_back(i); + solve(n / i, i, q); + q.pop_back(); + } + } + q.push_back(n); + result.push_back(q); + q.pop_back(); +} + +int main() +{ + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + result.clear(); + deque q; + solve(n, 2, q); + cout << result.size() - 1 << endl; + for (int i = 0, sz = result.size() - 1; i < sz; i++) + { + for (int j = 0, sz2 = result[i].size(); j < sz2; j++) + { + if (j > 0) + { + cout << " "; + } + cout << result[i][j]; + } + cout << endl; + } + } + return 0; +} diff --git a/10859.cpp b/10859.cpp new file mode 100644 index 0000000..6633196 --- /dev/null +++ b/10859.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +vector adj[1010]; +int vis[1010][2], d[1010][2], n, m; +#define DEBUG +int dp(int i, int j, int f) +{ + if (vis[i][j]) + { + return d[i][j]; + } + vis[i][j] = 1; + int &ans = d[i][j]; + int k; + ans = 2000; + for (k = 0; k < adj[i].size(); k++) + if (adj[i][k] != f) + { + ans += dp(adj[i][k], 1, i); + } + if (f >= 0 && !j) + { + ans++; + } + if (f < 0 || j) + { + int sum = 0; + for (k = 0; k < adj[i].size(); k++) + if (adj[i][k] != f) + { + sum += dp(adj[i][k], 0, i); + } + if (f >= 0) + { + sum++; + } + if (ans > sum) + { + ans = sum; + } + } + return ans; +} +int main() +{ + int T, a, b; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + int i; + for (i = 0; i < n; i++) + { + adj[i].clear(); + } + for (i = 0; i < m; i++) + { + scanf("%d%d", &a, &b); + adj[a].push_back(b); + adj[b].push_back(a); + } + memset(vis, 0, sizeof(vis)); + int ans = 0; + for (i = 0; i < n; i++) + if (!vis[i][0]) + { + ans += dp(i, 0, -1); + } + printf("%d %d %d\n", ans / 2000, m - ans % 2000, ans % 2000); + } + return 0; +} diff --git a/1086.cpp b/1086.cpp new file mode 100644 index 0000000..8f10aec --- /dev/null +++ b/1086.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +#define maxn 110 + +vector path[maxn * 2]; +int n, m; +string ans; + +struct TwoSat +{ + int n; + vector g[maxn * 2]; + bool mark[maxn * 2]; + stack s; + bool dfs(int x) + { + int i, j; + if (mark[x ^ 1]) + return 0; + if (mark[x]) + return 1; + mark[x] = 1; + s.push(x); + for (i = 0; i < g[x].size(); i++) + { + int v = g[x][i]; + if (!dfs(v)) + return 0; + } + return 1; + } + + void init(int n_) + { + n = n_; + int i; + for (i = 0; i < 2 * n; i++) + g[i].clear(); + memset(mark, 0, sizeof(mark)); + } + void clr() + { + memset(mark, 0, sizeof(mark)); + while (!s.empty()) + s.pop(); + } + + void addedge(int x, int y) + { + g[x].push_back(y); + } + + bool solve() + { + int i, j; + for (i = 0; i < 2 * n; i = i + 2)//从0开始 + { + if (!mark[i] && !mark[i + 1]) + { + while (!s.empty()) + s.pop(); + if (!dfs(i)) + { + while (!s.empty()) + { + mark[s.top()] = false; + s.pop(); + } + if (!dfs(i + 1)) + return false; + } + } + } + return true; + } +}; + +TwoSat solver; +int vote[maxn]; + +void solve() +{ + int i, j; + ans = ""; + for (i = 0; i < n; i++) + { + int judge = 0; + solver.clr(); + solver.g[i * 2].push_back(i * 2 + 1); + if (solver.solve()) + judge |= 1; + solver.g[i * 2].pop_back(); + solver.clr(); + solver.g[i * 2 + 1].push_back(i * 2); + if (solver.solve()) + judge |= 2; + solver.g[i * 2 + 1].pop_back(); + if (judge == 0) + { + puts("impossible"); + return; + } + else if (judge == 1) + ans += "y"; + else if (judge == 3) + ans += "?"; + else + ans += "n"; + } + cout << ans << endl; +} + +int main() +{ + int i, j, test = 0; + while (scanf("%d %d", &n, &m) == 2 && n + m) + { + solver.init(n); + for (i = 0; i < m; i++) + { + int t; + scanf("%d", &t); + for (j = 0; j < t; j++) + { + char s[2]; + int temp; + scanf("%d %s", &temp, s); + temp--; + vote[j] = temp * 2 + (s[0] == 'y'); + } + if (t <= 2) + { + int a, b; + for (a = 0; a < t; a++) + solver.addedge(vote[a] ^ 1, vote[a]); + } + else + { + int a, b; + for (a = 0; a < t; a++) + for (b = 0; b < t; b++) + { + if (a == b) + continue; + solver.addedge(vote[a] ^ 1, vote[b]); + } + } + } + printf("Case %d: ", ++test); + solve(); + } + return 0; +} diff --git a/10860.cpp b/10860.cpp new file mode 100644 index 0000000..af5b401 --- /dev/null +++ b/10860.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +char SS[10002], HavS[128], HavX[128], Temp[102], Fg[10002]; +int N, R[10002], Len, MinLen, ff; + +struct ss {char str[102]; int len;}; +ss XX[102]; + +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return y->len - x->len; +} + +int Imp() +{ + int i; + for (i = 33; i <= 127; i++) + { + if (HavS[i] > HavX[i]) + { + return 1; + } + } + return 0; +} + +int Match(int ind, int st) +{ + int i, k; + k = Len - st + 1; + if (XX[ind].len > k) + { + return 0; + } + for (i = 0; i < XX[ind].len; i++) + { + if (XX[ind].str[i] != SS[st + i]) + { + return 0; + } + } + return 1; +} + +void Recur(int n, int lvl) +{ + int i; + if (Fg[n] && R[n] <= lvl) + { + return; + } + Fg[n] = 1; + R[n] = lvl; + if (n == Len - 1) + { + MinLen = lvl; + ff = 1; + return; + } + for (i = 0; i < N + N; i++) + { + if (Match(i, n + 1)) + { + Recur(n + XX[i].len, lvl + 1); + } + } +} + +void Cal() +{ + int i; + ff = 0; + if (Imp()) + { + printf("Not possible.\n"); + return; + } + Len = strlen(SS); + qsort(XX, N + N, sizeof(XX[0]), com); + for (i = 0; i < N + N; i++) + { + if (Match(i, 0)) + { + Recur(XX[i].len - 1, 1); + } + } + + if (ff) + { + printf("%d.\n", MinLen); + } + else + { + printf("Not possible.\n"); + } +} + +void Free() +{ + int i; + for (i = 33; i <= 127; i++) + { + HavX[i] = HavS[i] = 0; + } + for (i = 0; i < Len; i++) + { + Fg[i] = 0; + } +} + +void Rev(char xx[], int len) +{ + int i, k = 0; + for (i = len - 1; i >= 0; i--) + { + Temp[k++] = xx[i]; + } + Temp[k] = '\0'; +} + +int main() +{ + int ks, st = 1, i, j, k; + scanf("%d", &ks); + while (ks--) + { + scanf("%s", SS); + for (i = 0; SS[i]; i++) + { + k = SS[i]; + HavS[k] = 1; + } + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%s", XX[i].str); + XX[i].len = strlen(XX[i].str); + Rev(XX[i].str, XX[i].len); + strcpy(XX[i + N].str, Temp); + XX[i + N].len = XX[i].len; + for (j = 0; j < XX[i].len; j++) + { + k = XX[i].str[j]; + HavX[k] = 1; + } + } + printf("Set %d: ", st++); + Cal(); + Free(); + } + return 0; +} diff --git a/10861.cpp b/10861.cpp new file mode 100644 index 0000000..935f188 --- /dev/null +++ b/10861.cpp @@ -0,0 +1,274 @@ +#include + +using namespace std; + +struct +{ + int m, s[64]; +} shapes[1024]; + +struct +{ + int a, cycle, shape; + int sub[64], nsub; +} block[64]; + +int n, nrshapes; + +int result[64]; + +int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; +int nprimes = 20; + +int fact_tab[64][32]; + +void mkfact() +{ + int i, j, t; + memset(fact_tab, 0, sizeof(fact_tab)); + for (i = 2; i < 64; i++) + { + for (j = 0; j < nprimes; j++) + { + fact_tab[i][j] = fact_tab[i - 1][j]; + } + for (t = i, j = 0; t > 1; j++) + { + while ((t % primes[j]) == 0) + { + t /= primes[j]; + fact_tab[i][j]++; + } + } + } +} + +void divnum(int x) +{ + int i; + for (i = 0; x > 1 && i < nprimes; i++) + { + while ((x % primes[i]) == 0) + { + x /= primes[i]; + result[i]--; + } + } +} + +void mulfact(int f) +{ + int i; + if (f >= 0) + { + for (i = 0; i < nprimes; i++) + { + result[i] += fact_tab[f][i]; + } + } + else + { + f = -f; + for (i = 0; i < nprimes; i++) + { + result[i] -= fact_tab[f][i]; + } + } +} + +void sort(int a[], int m) +{ + int i, j, t; + for (i = 1; i < m; i++) + { + for (t = a[i], j = i - 1; j >= 0 && a[j] > t; j--) + { + a[j + 1] = a[j]; + } + a[j + 1] = t; + } +} + +int getshape(int x) +{ + int i; + if (block[x].shape >= 0) + { + return block[x].shape; + } + for (block[x].nsub = 0, i = 0; i < n; i++) + { + if (block[i].a != x || i == x || block[i].cycle != 0) + { + continue; + } + block[x].sub[block[x].nsub++] = getshape(i); + } + sort(block[x].sub, block[x].nsub); + for (i = 0; i < nrshapes; i++) + if (shapes[i].m == block[x].nsub && + memcmp(shapes[i].s, block[x].sub, block[x].nsub * sizeof(int)) == 0) + { + return (block[x].shape = i); + } + shapes[nrshapes].m = block[x].nsub; + memcpy(shapes[nrshapes].s, block[x].sub, block[x].nsub * sizeof(int)); + return nrshapes++; +} + +void print() +{ + unsigned long long r; + int i, j; + for (r = 1, i = 0; i < nprimes; i++) + for (j = result[i]; j-- > 0;) + { + r *= primes[i]; + } + printf("%llu\n", r); +} + +void solve() +{ + static struct + { + int s[128], m; + } cycles[128]; + static int s[1024], v[1024]; + int i, j, k, t, ncycles; + for (i = 0; i < n; i++) + { + block[i].cycle = 0; + block[i].shape = -1; + } + for (i = 0; i < n; i++) + { + if (block[i].cycle) + { + continue; + } + for (j = block[i].a, k = n + 5; k-- > 0 && j != i;) + { + j = block[j].a; + } + if (i != j) + { + continue; + } + for (j = block[i].a;; j = block[j].a) + { + block[j].cycle = 1; + if (j == i) + { + break; + } + } + } + nrshapes = 1; + shapes[0].m = 0; + for (i = 0; i < n; i++) + { + getshape(i); + } + memset(result, 0, sizeof(result)); + mulfact(n); + for (i = 0; i < n; i++) + { + if (block[i].nsub == 0) + { + continue; + } + for (j = 1, k = 1; j < block[i].nsub; j++) + { + if (block[i].sub[j - 1] == block[i].sub[j]) + { + k++; + } + else + { + mulfact(-k); + k = 1; + } + } + mulfact(-k); + } + memset(v, 0, sizeof(v)); + for (ncycles = 0, i = 0; i < n; i++) + { + if (v[i] || !block[i].cycle) + { + continue; + } + for (j = block[i].a, k = 0;; j = block[j].a) + { + v[j] = 1; + s[k++] = getshape(j); + if (j == i) + { + break; + } + } + for (j = 0; j < k; j++) + { + s[k + j] = s[j]; + } + for (t = 0, j = 0; j < k; j++) + if (memcmp(s, s + j, k * sizeof(int)) == 0) + { + t++; + } + divnum(t); + cycles[ncycles].m = k; + memcpy(cycles[ncycles].s, s, 2 * k * sizeof(int)); + ncycles++; + } + memset(v, 0, sizeof(v)); + for (i = 0; i < ncycles; i++) + { + if (v[i]) + { + continue; + } + for (k = 0, j = 0; j < ncycles; j++) + { + if (v[j]) + { + continue; + } + if (cycles[j].m != cycles[i].m) + { + continue; + } + for (t = 0; t < cycles[i].m; t++) + if (memcmp(cycles[i].s, cycles[j].s + t, + cycles[i].m * sizeof(int)) == 0) + { + break; + } + if (t >= cycles[i].m) + { + continue; + } + v[j] = 1; + k++; + } + mulfact(-k); + } + print(); +} + +int main() +{ + int i; + mkfact(); + while (scanf("%d", &n) == 1 && n > 0) + { + for (i = 0; i < n; i++) + { + scanf("%d", &block[i].a); + block[i].a--; + } + solve(); + } + return 0; +} diff --git a/10862.cpp b/10862.cpp new file mode 100644 index 0000000..630d130 --- /dev/null +++ b/10862.cpp @@ -0,0 +1,489 @@ +#include + +using namespace std; + +#define BASE 100000000 +#define MAX_DIGIT 150 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +typedef long long INT; + +struct BigNum +{ + INT sign, len, digit[MAX_DIGIT]; + void absAdd(BigNum, BigNum); + void absSubtract(BigNum, BigNum); + void multAndAdd(BigNum, INT, INT); + + void operator=(INT); //ok + void operator=(BigNum); //ok + void operator=(char *); //ok + void add(BigNum, INT); //ok- + void add(BigNum, BigNum); //ok- + void subtract(BigNum, INT); //ok- + void subtract(BigNum, BigNum); //ok- + void mult(BigNum, INT); //ok- + void mult(BigNum, BigNum); //ok- + void div(BigNum, INT, INT &); //ok- , third param is the remainder + void div(BigNum, BigNum, BigNum &);//ok- + INT comp(BigNum); //ok- + INT absComp(BigNum); //ok- + void print(); //ok- + void update(); //ok- + void clear(); //ok- + BigNum() + { + clear(); + } +}; +void BigNum::operator=(INT x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + INT i; + clear(); + len = n.len; + sign = n.sign; + //memset(digit, 0, sizeof(digit)); + for (i = 0; i < len; ++i) + { + digit[i] = n.digit[i]; + } +} +/* + while providing 's', ensure that there + are no leading spaces in it +*/ +void BigNum::operator=(char *s) +{ + INT i, v, p; + clear(); + p = 1; + i = strlen(s) - 1; + for (v = 0; i >= 0; i--) + { + if (s[i] == '-') + { + --i; + sign = 1; + break; + } + else if (s[i] == '+') + { + --i; + break; + } + if (s[i] < '0' || s[i] > '9') + { + continue; + } + v = v + (s[i] - '0') * p; + p *= 10; + if (p == BASE) + { + digit[len++] = v; + p = 1; + v = 0; + } + } + if (v) + { + digit[len++] = v; + } + update(); +} +/* + return value <===> meaning + 0 equal + 1 greater + -1 less +*/ +INT BigNum::comp(BigNum n) +{ + INT i; + if (sign < n.sign) + { + return 1; + } + if (sign > n.sign) + { + return -1; + } + i = max(len, n.len) - 1; + for (; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + if (sign == 0) + { + return 1; + } + else + { + return -1; + } + } + } + return 0; +} +INT BigNum::absComp(BigNum n) +{ + INT i; + if (len < n.len) + { + return -1; + } + if (len > n.len) + { + return 1; + } + for (i = len - 1; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + return 1; + } + else if (digit[i] < n.digit[i]) + { + return -1; + } + } + return 0; +} +void BigNum::absSubtract(BigNum a, BigNum b) +{ + INT pos, borrow, t; + borrow = 0; + t = max(a.len, b.len); + for (pos = 0; pos < t; ++pos) + { + digit[pos] = a.digit[pos] - b.digit[pos] - borrow; + if (digit[pos] < 0) + { + digit[pos] += BASE; + borrow = 1; + } + else + { + borrow = 0; + } + if (digit[pos] != 0) + { + len = pos + 1; + } + } + update(); + //assert(borrow == 0);//, "|B| > |A|; can't handle this"); +} +void BigNum::absAdd(BigNum a, BigNum b) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < max(a.len, b.len); ++pos) + { + digit[pos] = a.digit[pos] + b.digit[pos] + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + if (carry != 0) + { + //check for overflow + digit[max(a.len, b.len)] = carry; + len = max(a.len, b.len) + 1; + } + else + { + len = max(a.len, b.len); + } + update(); +} +void BigNum::add(BigNum a, INT n) +{ + BigNum t; + t = n; + add(a, t); +} +void BigNum::add(BigNum a, BigNum b) +{ + clear(); + if (a.sign == b.sign) + { + absAdd(a, b); + sign = a.sign; + } + else + { + if (a.absComp(b) >= 0) + { + absSubtract(a, b); + sign = a.sign; + } + else + { + absSubtract(b, a); + sign = b.sign; + } + } +} +void BigNum::subtract(BigNum a, INT n) +{ + BigNum t; + t = n; + subtract(a, t); +} +void BigNum::subtract(BigNum a, BigNum b) +{ + BigNum t; + clear(); + t = b; + t.sign = 1 - b.sign; + add(a, t); +} +void BigNum::mult(BigNum a, INT s) +{ + INT pos, carry; + if (s < 0) + { + sign = 1 - a.sign; + s = -s; + } + else + { + sign = a.sign; + } + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + //take care about any overflow + digit[pos] = a.digit[pos] * s + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + pos = a.len; + while (carry != 0) + { + //check overflow + digit[pos] = carry % BASE; + carry /= BASE; + ++pos; + } + len = pos; + update(); +} +void BigNum::multAndAdd(BigNum a, INT s, INT offset) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos + offset] += a.digit[pos] * s + carry; + carry = digit[pos + offset] / BASE; + digit[pos + offset] %= BASE; + } + pos = a.len + offset; + while (carry != 0) + { + //check overflow + digit[pos] += carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + ++pos; + } + if (len < pos) + { + len = pos; + } + update(); +} +void BigNum::mult(BigNum a, BigNum b) +{ + INT i, pos; + clear(); + for (pos = 0; pos < b.len; ++pos) + { + multAndAdd(a, b.digit[pos], pos); + } + sign = (a.sign + b.sign) % 2; +} +void BigNum::div(BigNum a, INT s, INT &r) +{ + INT pos; + r = 0; + len = 0; + sign = a.sign; + if (s < 0) + { + sign = 1 - sign; + s = -s; + } + for (pos = a.len - 1; pos >= 0; --pos) + { + r = r * BASE + a.digit[pos]; + digit[pos] = r / s; + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + r %= s; + } + update(); +} +void BigNum::div(BigNum a, BigNum b, BigNum &r) +{ + INT i, pos, lower, upper, mid; + BigNum d, e, t; + if (b.sign) + { + t = b; + t.sign = 0; + div(a, t, r); + sign = 1 - a.sign; + return; + } + for (i = 0; i < len; ++i) + { + digit[i] = 0; + } + len = 0; + r = (INT)0; + for (pos = a.len - 1; pos >= 0; --pos) + { + r.mult(r, BASE); + r.add(r, a.digit[pos]); + lower = 0; + upper = BASE - 1; + while (upper > lower) + { + mid = (upper + lower) / 2 + 1; + d.mult(b, mid); + e.subtract(r, d); + if (e.sign == 0) + { + lower = mid; + } + else + { + upper = mid - 1; + } + } + digit[pos] = lower; + d.mult(b, lower); + r.subtract(r, d); + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + } + update(); +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::print() +{ + INT i; + if (sign) + { + printf("-"); + } + printf("%ld", digit[len - 1]); + for (i = len - 2; i >= 0; --i) + { + //BE CAREFUL HERE + switch (BASE) + { + case 10: + printf("%lld", digit[i]); + break; + case 100: + printf("%02lld", digit[i]); + break; + case 1000: + printf("%03lld", digit[i]); + break; + case 10000: + printf("%04lld", digit[i]); + break; + case 100000: + printf("%05lld", digit[i]); + break; + case 1000000: + printf("%06lld", digit[i]); + break; + case 10000000: + printf("%07lld", digit[i]); + break; + case 100000000: + printf("%08lld", digit[i]); + break; + case 1000000000: + printf("%09lld", digit[i]); + break; + default: + break; + } + } +} +BigNum Res[2002]; +void Gen() +{ + BigNum sum, temp; + int i; + Res[1] = 1; + Res[2] = 3; + sum.add(Res[1], Res[2]); + for (i = 3; i <= 2000; i++) + { + temp.add(Res[i - 1], sum); + Res[i].add(temp, 1); + sum.add(sum, Res[i]); + temp.clear(); + } +} +void Cal() +{ + int n; + Gen(); + while (scanf("%d", &n) && n) + { + Res[n].print(); + printf("\n"); + } +} +int main() +{ + Cal(); + return 0; +} diff --git a/10863.cpp b/10863.cpp new file mode 100644 index 0000000..e024ce5 --- /dev/null +++ b/10863.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +char grid[21][21], ng[21][21], bg[21][21]; +int p[4]; +int par[21 * 21], d[21 * 21]; +int dd[5] = {1, 0, -1, 0, 1}; +int n, m; +struct ds +{ + bool operator()(int a, int b) + { + return (d[a] == d[b] ? a < b : d[a] < d[b]); + } +}; +int bfs(int s, int t) +{ + memset(par, -1, sizeof par); + memset(d, -1, sizeof d); + par[s] = s; + d[s] = 0; + set q; + q.insert(s); + while (q.size()) + { + int next = *q.begin(); + q.erase(q.begin()); + if (next == t) + { + break; + } + for (int k = 0; k < 4; k++) + { + int ni = next / 21 + dd[k], nj = next % 21 + dd[k + 1]; + if (ni >= n || ni < 0 || nj >= m || nj < 0 || ng[ni][nj] == '#') + { + continue; + } + int nd = d[next] + (ng[ni][nj] == 'o'); + if (d[ni * 21 + nj] == -1 || nd < d[ni * 21 + nj]) + { + par[ni * 21 + nj] = next; + q.erase(ni * 21 + nj); + d[ni * 21 + nj] = nd; + q.insert(ni * 21 + nj); + } + } + } + return d[t]; +} +int main() +{ + bool first = true; + while (cin >> m >> n) + { + if (first) + { + first = false; + } + else + { + cout << endl; + } + cout << m << " " << n << endl; + if (!m) + { + break; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + cin >> grid[i][j]; + if (grid[i][j] >= 'A' && grid[i][j] <= 'D') + { + p[grid[i][j] - 'A'] = i * 21 + j; + } + } + int best = 1000000; + for (int a = 0; a < 4; a++) + for (int b = a + 1; b < 4; b++) + for (int c = 0; c < 4; c++) + for (int d = c + 1; d < 4; d++) + for (int e = 0; e < 4; e++) + for (int f = e + 1; f < 4; f++) + { + bool good = true; + for (int g = 0; g < 4; g++) + if (g != a && g != b && g != c && g != d && g != e && g != f) + { + good = false; + } + if (!good) + { + continue; + } + if (a == c && b == d || a == e && b == f || c == e && d == f) + { + continue; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + ng[i][j] = grid[i][j]; + } + bfs(p[a], p[b]); + int cur = par[p[b]]; + while (cur != p[a]) + { + if (ng[cur / 21][cur % 21] == 'o') + { + ng[cur / 21][cur % 21] = '.'; + } + cur = par[cur]; + } + bfs(p[c], p[d]); + cur = par[p[d]]; + while (cur != p[c]) + { + if (ng[cur / 21][cur % 21] == 'o') + { + ng[cur / 21][cur % 21] = '.'; + } + cur = par[cur]; + } + bfs(p[e], p[f]); + cur = par[p[f]]; + while (cur != p[e]) + { + if (ng[cur / 21][cur % 21] == 'o') + { + ng[cur / 21][cur % 21] = '.'; + } + cur = par[cur]; + } + int diff = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (ng[i][j] != grid[i][j]) + { + diff++; + } + if (diff < best) + { + best = diff; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + bg[i][j] = ng[i][j]; + } + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + cout << bg[i][j]; + } + cout << endl; + } + } + return 0; +} diff --git a/10864.cpp b/10864.cpp new file mode 100644 index 0000000..9ae8153 --- /dev/null +++ b/10864.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +typedef struct +{ + int x1, y1, x2, y2, c, area; +} rect_t; + +int nrect; +rect_t rect[1024]; + +void addrect(int x1, int y1, int x2, int y2, int c) +{ + if (x1 > x2) + { + return; + } + if (y1 > y2) + { + return; + } + rect[nrect].x1 = x1; + rect[nrect].y1 = y1; + rect[nrect].x2 = x2; + rect[nrect].y2 = y2; + rect[nrect].c = c; + nrect++; +} + +int min(int a, int b) +{ + return (a < b) ? a : b; +} + +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +int isect(rect_t *r, rect_t *a, rect_t *b) +{ + r->x1 = max(a->x1, b->x1); + r->x2 = min(a->x2, b->x2); + r->y1 = max(a->y1, b->y1); + r->y2 = min(a->y2, b->y2); + return ((r->x1 <= r->x2) && (r->y1 <= r->y2)); +} + +int adj(int a, int b) +{ + int dx, dy; + dx = min(rect[a].x2, rect[b].x2) - max(rect[a].x1, rect[b].x1) + 1; + dy = min(rect[a].y2, rect[b].y2) - max(rect[a].y1, rect[b].y1) + 1; + return ((dx == 0 && dy > 0) || (dy == 0 && dx > 0)); +} + +void split(rect_t *r, rect_t *s) +{ + addrect(r->x1, r->y1, r->x2, s->y1 - 1, r->c); + addrect(r->x1, s->y2 + 1, r->x2, r->y2, r->c); + addrect(r->x1, s->y1, s->x1 - 1, s->y2, r->c); + addrect(s->x2 + 1, s->y1, r->x2, s->y2, r->c); +} + +void add(int x1, int y1, int x2, int y2, int m) +{ + rect_t s, r; + int i; + r.x1 = x1; + r.y1 = y1; + r.x2 = x2; + r.y2 = y2; + for (i = nrect - 1; i >= 0; i--) + { + if (!isect(&s, &rect[i], &r)) + { + continue; + } + split(&rect[i], &s); + rect[i].x1 = s.x1; + rect[i].x2 = s.x2; + rect[i].y1 = s.y1; + rect[i].y2 = s.y2; + rect[i].c |= (1 << m); + } +} + +int query(int x, int y) +{ + static int s[1024], u[1024]; + int i, t, area; + ; + for (i = 0; i < nrect; i++) + { + u[i] = 0; + } + for (i = 0; i < nrect; i++) + if (rect[i].x1 <= x && x <= rect[i].x2 && + rect[i].y1 <= y && y <= rect[i].y2) + { + break; + } + if (i >= nrect) + { + return 0; + } + for (area = 0, s[0] = i, u[i] = 1, t = 1; t > 0;) + { + x = s[--t]; + area += rect[x].area; + for (y = 0; y < nrect; y++) + if (u[y] == 0 && rect[x].c == rect[y].c && adj(x, y)) + { + u[s[t++] = y] = 1; + } + } + return area; +} + +int main() +{ + int i, m, t, x1, y1, s; + for (t = 1; scanf("%d", &m) == 1; t++) + { + nrect = 0; + addrect(1, 1, 10000, 10000, 0); + for (i = 0; i < m; i++) + { + scanf("%d %d %d", &x1, &y1, &s); + add(x1, y1, x1 + s - 1, y1 + s - 1, i); + } + for (i = 0; i < nrect; i++) + { + rect[i].area = (rect[i].x2 - rect[i].x1 + 1) * (rect[i].y2 - rect[i].y1 + 1); + } + printf("Case %d:\n", t); + for (scanf("%d", &m); m-- > 0 && scanf("%d %d", &x1, &y1);) + { + printf("%d\n", query(x1, y1)); + } + } + return 0; +} diff --git a/10865.cpp b/10865.cpp new file mode 100644 index 0000000..76823c7 --- /dev/null +++ b/10865.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int reduce(int n) +{ + if (n == 0) + { + return 0; + } + if (n > 0) + { + return 1; + } + if (n < 0) + { + return -1; + } +} + +int main() +{ + int n, x, y; + vector> points; + while (cin >> n) + { + if (!n) + { + break; + } + points.clear(); + for (int i = 0; i < n; i++) + { + cin >> x >> y; + points.push_back(make_pair(x, y)); + } + pair mpoint = points[(n - 1) / 2]; + pair result; + result.first = result.second = 0; + int tmp; + for (int i = 0; i < n; i++) + { + tmp = reduce(mpoint.first - points[i].first) * reduce(mpoint.second - points[i].second); + if (tmp > 0) + { + result.first++; + } + else if (tmp < 0) + { + result.second++; + } + } + cout << result.first << " " << result.second << endl; + } + return 0; +} diff --git a/10866.cpp b/10866.cpp new file mode 100644 index 0000000..95e9b25 --- /dev/null +++ b/10866.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +#define MAXP 100000 +char A[MAXP + 10]; + +int main() +{ + int p; + while (scanf("%d", &p), p) + { + if (p == 2) + { + printf("Impossible\n"); + continue; + } + memset(A, '1', sizeof(A)); + for (unsigned long long i = 1; i < p; i++) + { + A[(i * i) % p] = '0'; + } + for (int i = 1; i < p; i++) + { + printf("%c", A[i]); + } + printf("\n"); + } +} diff --git a/10867.cpp b/10867.cpp new file mode 100644 index 0000000..b3e0788 --- /dev/null +++ b/10867.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; +typedef vector

Polygon; + +const double eps = 1e-10; + +double cross(P a, P b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +double dot(P a, P b, P c) +{ + return a.real() * b.real() + a.imag() * b.imag(); +} + +bool is_point_online_seg(P a, P b, P c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + eps; +} + +P intersect_ll(P a1, P a2, P b1, P b2) +{ + P a = a2 - a1, b = b2 - b1; + return a1 + a * cross(b, b1 - a1) / cross(b, a); +} +double distance_l_p(P &a, P &b, P &c) +{ + return abs(cross(b - a, c - a)) / abs(b - a); +} + +bool is_in(Polygon &in, P &a) +{ + int cnt = 0; + rep(i, in.size()) + { + P cur = in[i] - a, next = in[(i + 1) % in.size()] - a; + if (cur.imag() > next.imag()) + { + swap(cur, next); + } + if (cur.imag() < 0 && 0 <= next.imag() && + cross(next, cur) >= 0) + { + cnt++; + } + if (is_point_online_seg(in[i], in[(i + 1) % in.size()], a)) + { + return true; + } + } + if (cnt % 2 == 1) + { + return true; + } + else + { + return false; + } +} + +bool cmp(const P &a, const P &b) +{ + if (abs(a.real() - b.real()) > eps) + { + return a.real() < b.real(); + } + return a.imag() < b.imag(); +} + +double solve(Polygon &in, P l1, P l2) +{ + vector

CP; + rep(i, in.size()) + { + double leng = distance_l_p(l1, l2, in[i]); + if (leng < eps) + { + CP.push_back(in[i]); + } + else if (cross(l2 - l1, in[i] - l1) * cross(l2 - l1, in[(i + 1) % in.size()] - l1) < 0) + { + CP.push_back(intersect_ll(l1, l2, in[i], in[(i + 1) % in.size()])); + } + } + sort(CP.begin(), CP.end(), cmp); + if (CP.size() == 0) + { + return 0; + } + double ret = 0; + rep(i, CP.size() - 1) + { + P mid = (CP[i] + CP[(i + 1) % CP.size()]); + mid /= 2; + if (is_in(in, mid)) + { + ret += abs(CP[i] - CP[(i + 1) % CP.size()]); + } + } + return ret; +} + +int main() +{ + int n, q; + while (scanf("%d%d", &n, &q) && n) + { + Polygon a; + rep(i, n) + { + double re, im; + scanf("%lf%lf", &re, &im); + P tmp(re, im); + a.push_back(tmp); + } + rep(i, q) + { + double re, im; + scanf("%lf%lf", &re, &im); + P l1(re, im); + scanf("%lf%lf", &re, &im); + P l2(re, im); + printf("%.3lf\n", solve(a, l1, l2)); + } + } + return 0; +} diff --git a/10868.cpp b/10868.cpp new file mode 100644 index 0000000..78e7110 --- /dev/null +++ b/10868.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define dt 0.00005 +#define g 9.81 + +int main() +{ + double k, l, s, w; + while (scanf("%lf%lf%lf%lf", &k, &l, &s, &w) == 4) + { + if (k == 0) + { + break; + } + assert(0 <= k && 0 <= l && 0 <= s && s < 200 && 0 <= w); + double d = s < l ? s : l, a, f, F = g * w, t = sqrt(2 * d / g), v = g * t; + while (1) + { + if (d >= s) + { + if (v > 10) + { + puts("Killed by the impact."); + } + if (v < 10) + { + puts("James Bond survives."); + } + break; + } + if (v < 0) + { + puts("Stuck in the air."); + break; + } + f = F - k * (d < l ? 0 : d - l); + a = f / w; + d += v * dt + dt * dt * a / 2; + v += dt * a; + } + } +} diff --git a/10869.cpp b/10869.cpp new file mode 100644 index 0000000..cd1a03f --- /dev/null +++ b/10869.cpp @@ -0,0 +1,302 @@ +#include + +using namespace std; + +#define MAXN 262144 + +typedef struct {int x,y;} pt_t; + +int left_[MAXN], right_[MAXN], parent[MAXN], key[MAXN], cnt[MAXN], height[MAXN], score1[MAXN], score2[MAXN], line_s[MAXN], line_o[MAXN], total, root, n; +pt_t pt[MAXN]; + +int cmpx(const void *p, const void *q) +{ + pt_t *a = (pt_t *)p, *b = (pt_t *)q; + return (a->x != b->x) ? (a->x - b->x) : (a->y - b->y); +} + +int cmpi(const void *p, const void *q) +{ + return *(int *)p - *(int *)q; +} + +void ptree(int x) +{ + if (x == 0) + { + printf("."); + } + else + { + printf("("); + ptree(left_[x]); + printf((cnt[x] == 1) ? " %d " : " %d:%d ", key[x], cnt[x]); + ptree(right_[x]); + printf(")"); + } +} + +void print(int r) +{ + ptree(r); + printf("\n"); +} + +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +int rotl(int x) +{ + int a, b, c, y, t; + y = right_[x]; + a = left_[x]; + b = left_[y]; + c = right_[y]; + right_[x] = b; + parent[b] = x; + t = parent[x]; + if (t == 0) + { + root = y; + } + else if (left_[t] == x) + { + left_[t] = y; + } + else + { + right_[t] = y; + } + parent[y] = t; + left_[y] = x; + parent[x] = y; + t = cnt[x]; + cnt[x] = cnt[x] - cnt[y] + cnt[b]; + cnt[y] = t; + height[x] = max(height[a], height[b]) + 1; + height[y] = max(height[x], height[c]) + 1; + return y; +} + +int rotr(int y) +{ + int a, b, c, x, t; + x = left_[y]; + a = left_[x]; + b = right_[x]; + c = right_[y]; + left_[y] = b; + parent[b] = y; + t = parent[y]; + if (t == 0) + { + root = x; + } + else if (left_[t] == y) + { + left_[t] = x; + } + else + { + right_[t] = x; + } + parent[x] = t; + right_[x] = y; + parent[y] = x; + t = cnt[y]; + cnt[y] = cnt[y] - cnt[x] + cnt[b]; + cnt[x] = t; + height[y] = max(height[b], height[c]) + 1; + height[x] = max(height[a], height[y]) + 1; + return x; +} + +void insert(int k) +{ + int x; + if (root == 0) + { + left_[0] = right_[0] = parent[0] = height[0] = key[0] = cnt[0] = 0; + left_[1] = right_[1] = parent[1] = 0; + cnt[1] = height[1] = total = root = 1; + key[1] = k; + return; + } + for (total++, x = root;;) + { + if (k < key[x]) + { + if (left_[x] == 0) + { + left_[x] = total; + break; + } + x = left_[x]; + } + else if (k > key[x]) + { + if (right_[x] == 0) + { + right_[x] = total; + break; + } + x = right_[x]; + } + else + { + for (; x != 0; x = parent[x]) + { + cnt[x]++; + } + return; + } + } + parent[total] = x; + x = total; + key[x] = k; + left_[x] = 0; + right_[x] = 0; + cnt[x] = 1; + height[x] = 1; + for (x = parent[x]; x != 0; x = parent[x]) + { + cnt[x]++; + height[x] = max(height[left_[x]], height[right_[x]]) + 1; + if (height[left_[x]] < (height[right_[x]] - 1)) + { + x = rotl(x); + } + else if (height[right_[x]] < (height[left_[x]] - 1)) + { + x = rotr(x); + } + } +} + +int find_less(int k) +{ + int r, x; + for (r = 0, x = root; x != 0;) + { + if (k < key[x]) + { + x = left_[x]; + } + else if (k > key[x]) + { + r += cnt[x] - cnt[right_[x]]; + x = right_[x]; + } + else + { + r += cnt[left_[x]]; + break; + } + } + return r; +} + +int find_greater(int k) +{ + int r, x; + for (r = 0, x = root; x != 0;) + { + if (k < key[x]) + { + r += cnt[x] - cnt[left_[x]]; + x = left_[x]; + } + else if (k > key[x]) + { + x = right_[x]; + } + else + { + r += cnt[right_[x]]; + break; + } + } + return r; +} + +int main() +{ + int i, j, k, s; + while (scanf("%d", &n) == 1 && n > 0) + { + for (i = 0; i < n; i++) + { + scanf("%d %d", &pt[i].x, &pt[i].y); + } + qsort(pt, n, sizeof(pt[0]), &cmpx); + memset(score1, 0, n * sizeof(int)); + memset(score2, 0, n * sizeof(int)); + for (root = 0, i = 0; i < n; i = j) + { + for (j = i; j < n && pt[j].x == pt[i].x; j++) + { + score1[j] += find_less(pt[j].y); + score2[j] += find_greater(pt[j].y); + } + for (j = i; pt[j].x == pt[i].x; j++) + { + insert(pt[j].y); + } + } + for (root = 0, i = n - 1; i >= 0; i = j) + { + for (j = i; j >= 0 && pt[j].x == pt[i].x; j--) + { + score2[j] += find_less(pt[j].y); + score1[j] += find_greater(pt[j].y); + } + for (j = i; j >= 0 && pt[j].x == pt[i].x; j--) + { + insert(pt[j].y); + } + } + for (k = 0, i = 0; i < n; i = j, k++) + { + line_s[k] = score1[i]; + line_o[k] = score2[i]; + for (j = i; j < n && pt[j].x == pt[i].x; j++) + { + if (score1[j] < line_s[k]) + { + line_s[k] = score1[j]; + } + if (score2[j] > line_o[k]) + { + line_o[k] = score2[j]; + } + } + } + for (s = line_s[0], i = 0; i < k; i++) + { + if (line_s[i] > s) + { + s = line_s[i]; + } + } + for (j = 0, i = 0; i < k; i++) + { + if (line_s[i] == s) + { + key[j++] = line_o[i]; + } + } + qsort(key, j, sizeof(key[0]), &cmpi); + printf("Stan: %d; Ollie:", s); + for (i = 0; i < j; i++) + { + if (i == 0 || key[i - 1] != key[i]) + { + printf(" %d", key[i]); + } + } + printf(";\n"); + } + return 0; +} diff --git a/10870.cpp b/10870.cpp new file mode 100644 index 0000000..d25cc5d --- /dev/null +++ b/10870.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +int n, m, d, a[15], f[15], b[15][15], ans; + +void Cf(int a[15][15], int b[15][15], int n) +{ + int c[15][15] = {0}; + int i, j, k; + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + for (k = 0; k < n; k++) + { + c[i][j] += (a[i][k] * b[k][j]) % m; + c[i][j] %= m; + } + memcpy(a, c, sizeof(c)); +} + +void Matrix_Pow(int a[15][15], int n, int b) +{ + int c[15][15] = {0}, d[15][15] = {0}; + int i; + for (i = 0; i < n; i++) + { + c[i][i] = 1; + } + memcpy(d, a, sizeof(d)); + while (b > 0) + { + if (b & 1) + { + Cf(c, d, n); + } + b >>= 1; + Cf(d, d, n); + } + memcpy(a, c, sizeof(c)); +} + +int main() +{ + int i; + while (1) + { + scanf("%d%d%d", &d, &n, &m); + if (d == 0) + { + break; + } + for (i = 0; i < d; i++) + { + scanf("%d", &a[i]); + a[i] %= m; + } + for (i = 0; i < d; i++) + { + scanf("%d", &f[i]); + f[i] %= m; + } + memset(b, 0, sizeof(b)); + for (i = 0; i < d; i++) + { + b[i][0] = a[i]; + } + for (i = 1; i < d; i++) + { + b[i - 1][i] = 1; + } + Matrix_Pow(b, d, n - d); + ans = 0; + for (i = d - 1; i >= 0; i--) + { + ans += (f[i] * b[d - i - 1][0]) % m; + ans %= m; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/10871.cpp b/10871.cpp new file mode 100644 index 0000000..d9c3cd8 --- /dev/null +++ b/10871.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +/* +10871 +*/ +#define maxn 10000000 + +char sv[maxn + 2]; +int V[10000]; +void Prime() +{ + int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } +} +void Cal(int ind) +{ + int i, sum, min = 1000000, j, f = 1, l, r; + for (i = 0; i < ind; i++) + { + sum = V[i]; + for (j = i + 1; j < ind; j++) + { + if (j - i + 1 >= min) + { + break; + } + sum += V[j]; + if (sv[sum] == 0) + { + min = j - i + 1; + l = i; + r = j; + f = 0; + } + } + } + if (f) + { + printf("This sequence is anti-primed.\n"); + return; + } + printf("Shortest primed subsequence is length %d:", min); + for (i = l; i <= r; i++) + { + printf(" %d", V[i]); + } + printf("\n"); +} +int main() +{ + int ks, d; + Prime(); + scanf("%d", &ks); + while (ks--) + { + scanf("%d", &d); + for (int i = 0; i < d; i++) + { + scanf("%d", &V[i]); + } + Cal(d); + //V.clear(); + } + return 0; +} diff --git a/10872.cpp b/10872.cpp new file mode 100644 index 0000000..d62d3d9 --- /dev/null +++ b/10872.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + int cnum = 0; + for (long long n; scanf("%lld", &n) == 1 && n;) + { + if (n & 1) + { + n += 3; + } + printf("Case %d: %lld\n", ++cnum, (n * n + 24) / 48); + } + return 0; +} diff --git a/10873.cpp b/10873.cpp new file mode 100644 index 0000000..cc159a8 --- /dev/null +++ b/10873.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +int loc[64], w, h; + +void push(int c) +{ + loc[c]++; + if (loc[c - 1] <= (loc[c] - 3) && loc[c + 1] <= (loc[c] - 3)) + { + loc[c] -= 2; + push(c - 1); + push(c + 1); + } + else if (loc[c - 1] <= (loc[c] - 3)) + { + loc[c]--; + push(c - 1); + } + else if (loc[c + 1] <= (loc[c] - 3)) + { + loc[c]--; + push(c + 1); + } +} + +int main() +{ + static struct + { + int s, r, c, n, z; + } feed[3]; + int i, j, t = 1; + while (scanf("%d %d", &w, &h) == 2 && w > 0) + { + memset(feed, 0, sizeof(feed)); + for (i = 0; i < 3; i++) + { + scanf("%d", &feed[i].s); + feed[i].z = (feed[i].s < 1); + } + for (i = 0; i < 3; i++) + { + scanf("%d", &feed[i].c); + feed[i].c++; + feed[i].r = 0; + } + memset(loc, 0, sizeof(loc)); + loc[0] = loc[w + 1] = 0x3FFFFFFF; + for (;;) + { + for (i = 0; i < 3 && feed[i].z; i++) + ; + if (i >= 3) + { + break; + } + for (j = feed[i++].r; i < 3; i++) + if (!feed[i].z && feed[i].r < j) + { + j = feed[i].r; + } + for (i = 0; i < 3; i++) + { + if (feed[i].z) + { + continue; + } + feed[i].r -= j; + if (feed[i].r > 0) + { + continue; + } + feed[i].r = feed[i].s; + if (loc[feed[i].c] >= h) + { + feed[i].z = 1; + continue; + } + feed[i].n++; + push(feed[i].c); + } + } + printf("Case %d: %d %d %d %d\n", + t++, feed[0].n, feed[1].n, feed[2].n, + w * h - feed[0].n - feed[1].n - feed[2].n); + } + return 0; +} diff --git a/10874.cpp b/10874.cpp new file mode 100644 index 0000000..218da03 --- /dev/null +++ b/10874.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define maxn 20002 + +struct ss +{ + int l, r; +} P[maxn]; +int N, R[maxn], L[maxn]; + +int Right(int pp, int clp, int crp, int dis) +{ + int d, c; + c = dis + 1; + if (pp <= clp) + c += (crp - pp); + else if (pp >= crp) + { + d = pp - clp; + d += crp - clp; + c += d; + } + else + { + c += (pp - clp); + c += (crp - clp); + } + return c; +} + +int Left(int pp, int clp, int crp, int dis) +{ + int d, c; + c = dis + 1; + if (pp >= crp) + c += (pp - clp); + else if (pp <= clp) + { + d = crp - pp; + d += crp - clp; + c += d; + } + else + { + c += (crp - pp); + c += (crp - clp); + } + return c; +} + +void Cal() +{ + int i, j; + P[N].r = N; + R[1] = P[1].r - 1; + L[1] = R[1] + P[1].r - P[1].l; + for (i = 2; i <= N; i++) + { + R[i] = Right(P[i - 1].l, P[i].l, P[i].r, L[i - 1]); + j = Right(P[i - 1].r, P[i].l, P[i].r, R[i - 1]); + if (R[i] > j) + R[i] = j; + L[i] = Left(P[i - 1].l, P[i].l, P[i].r, L[i - 1]); + j = Left(P[i - 1].r, P[i].l, P[i].r, R[i - 1]); + if (j < L[i]) + L[i] = j; + } + cout << R[N] << endl; +} + +int main() +{ + int i; + while (cin >> N && N) + { + for (i = 1; i <= N; i++) + { + cin >> P[i].l >> P[i].r; + } + Cal(); + } + return 0; +} diff --git a/10875.cpp b/10875.cpp new file mode 100644 index 0000000..fcf7029 --- /dev/null +++ b/10875.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +char B[6][1000], L[32768], E[250] = "0123456789+-*/"; +char C[6][60] = {"000 .0. 000 000 0.0 000 0.. 000 000 000 .0. ... 0.0 .0. ", + "0.0 .0. ..0 ..0 0.0 0.. 0.. ..0 0.0 0.0 .0. ... 0.0 ... ", + "0.0 .0. 000 000 000 000 000 ..0 000 000 000 000 .0. 000 ", + "0.0 .0. 0.. ..0 ..0 ..0 0.0 ..0 0.0 ..0 .0. ... 0.0 ... ", + "000 .0. 000 000 ..0 000 000 ..0 000 ..0 .0. ... 0.0 .0. ", + ""}; +int S[100], ptr; +int main() +{ + int i, j, k, hash, n, len; + len = strlen(C[0]); + for (n = 0; n < len; n += 4) + { + for (i = hash = 0; i < 5; i++) + { + for (j = 0; j < 3; j++) + { + hash <<= 1; + if (C[i][n + j] == '0') + { + hash |= 1; + } + } + } + L[hash] = E[n >> 2]; + } + while (1) + { + for (i = 0; i < 6; i++) + { + gets(B[i]); + } + len = strlen(B[0]); + for (n = 0; n < len; n += 4) + { + for (i = hash = 0; i < 5; i++) + { + for (j = 0; j < 3; j++) + { + hash <<= 1; + if (B[i][n + j] == '0') + { + hash |= 1; + } + } + } + E[n >> 2] = L[hash]; + } + n >>= 2; + E[n] = '\0'; + if (strcmp(E, "0") == 0) + { + break; + } + S[0] = 0; + for (i = ptr = 0; E[i]; i++) + { + switch (E[i]) + { + case '+': + S[++ptr] = -1, S[++ptr] = 0; + break; + case '-': + S[++ptr] = -2, S[++ptr] = 0; + break; + case '*': + S[++ptr] = -3, S[++ptr] = 0; + break; + case '/': + S[++ptr] = -4, S[++ptr] = 0; + break; + default: + S[ptr] = 10 * S[ptr] + E[i] - '0'; + break; + } + } + for (i = 0, j = 1; j < ptr; j += 2) + { + switch (S[j]) + { + case -3: + S[i] *= S[j + 1]; + break; + case -4: + S[i] /= S[j + 1]; + break; + default: + S[++i] = S[j], S[++i] = S[j + 1]; + break; + } + } + for (j = 1; j < i; j += 2) + { + if (S[j] == -1) + { + S[0] += S[j + 1]; + } + else + { + S[0] -= S[j + 1]; + } + } + sprintf(E, "%d", S[0]); + for (ptr = 0; E[ptr]; ptr++) + { + if (E[ptr] == '-') + { + k = 44; + } + else + { + k = (E[ptr] - '0') * 4; + } + for (i = 0; i < 5; i++) + { + for (j = 0; j < 4; j++) + { + B[i][ptr * 4 + j] = C[i][k + j]; + } + } + } + for (i = 0; i < 5; i++) + { + B[i][4 * ptr - 1] = '\0'; + printf("%s\n", B[i]); + } + printf("\n"); + } + return 0; +} diff --git a/10876.cpp b/10876.cpp new file mode 100644 index 0000000..9902461 --- /dev/null +++ b/10876.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +int N; +struct C +{ + int x, y, r; +} C[256]; + +int uf[256]; + +int f(int x) +{ + int z; + for (z = x; uf[z] != z; z = uf[z]) + ; + while (x != z) + { + int y = uf[x]; + uf[x] = z; + x = y; + } + return z; +} + +void merge(int x, int y) +{ + uf[f(x)] = f(y); +} + +double sq(double x) +{ + return x * x; +} + +bool possible(double R) +{ + for (int i = 0; i < N + 4; i++) + { + uf[i] = i; + } + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + double t = sqrt(sq(C[i].x - C[j].x) + sq(C[i].y - C[j].y)) - C[i].r - C[j].r; + if (t < 2 * R) + { + merge(f(i), f(j)); + } + } + if (C[i].x - C[i].r < 2 * R) + { + merge(i, N + 0); + } + if (1000 - C[i].x - C[i].r < 2 * R) + { + merge(i, N + 1); + } + if (C[i].y - C[i].r < 2 * R) + { + merge(i, N + 2); + } + if (1000 - C[i].y - C[i].r < 2 * R) + { + merge(i, N + 3); + } + } + for (int i = 0; i < 4; i++) + for (int j = i + 1; j < 4; j++) + if (f(N + i) == f(N + j)) + { + return false; + } + return true; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d", &N) == 1; cs++) + { + for (int i = 0; i < N; i++) + { + scanf("%d %d %d", &C[i].x, &C[i].y, &C[i].r); + } + double left = 0, right = 500; + while (right - left > 1e-9) + { + double mid = (left + right) / 2; + if (possible(mid)) + { + left = mid; + } + else + { + right = mid; + } + } + printf("%.3f\n", left); + } +} diff --git a/10877.cpp b/10877.cpp new file mode 100644 index 0000000..6561c46 --- /dev/null +++ b/10877.cpp @@ -0,0 +1,265 @@ +#include + +using namespace std; + +bool hash_[1000000]; +int decoids[6], ndecoids; + +int encode(int *p) +{ + int v = p[0]; + for (int cnt = 1; cnt < 6; cnt++) + { + v = v * 10 + p[cnt]; + } + return v; +} + +bool solve() +{ + int copy[6], i; + // 1 + copy[0] = decoids[0]; + copy[1] = decoids[1]; + copy[2] = decoids[2]; + copy[3] = decoids[3]; + copy[4] = decoids[4]; + copy[5] = decoids[5]; + i = encode(copy); + if (hash_[i]) + { + return false; + } + hash_[i] = true; + // 2 + copy[0] = decoids[0]; + copy[1] = decoids[4]; + copy[2] = decoids[1]; + copy[3] = decoids[3]; + copy[4] = decoids[5]; + copy[5] = decoids[2]; + i = encode(copy); + hash_[i] = true; + // 3 + copy[0] = decoids[0]; + copy[1] = decoids[5]; + copy[2] = decoids[4]; + copy[3] = decoids[3]; + copy[4] = decoids[2]; + copy[5] = decoids[1]; + i = encode(copy); + hash_[i] = true; + // 4 + copy[0] = decoids[0]; + copy[1] = decoids[2]; + copy[2] = decoids[5]; + copy[3] = decoids[3]; + copy[4] = decoids[1]; + copy[5] = decoids[4]; + i = encode(copy); + hash_[i] = true; + // 5 + copy[0] = decoids[1]; + copy[1] = decoids[2]; + copy[2] = decoids[0]; + copy[3] = decoids[5]; + copy[4] = decoids[3]; + copy[5] = decoids[4]; + i = encode(copy); + hash_[i] = true; + // 6 + copy[0] = decoids[1]; + copy[1] = decoids[3]; + copy[2] = decoids[2]; + copy[3] = decoids[5]; + copy[4] = decoids[4]; + copy[5] = decoids[0]; + i = encode(copy); + hash_[i] = true; + // 7 + copy[0] = decoids[1]; + copy[1] = decoids[4]; + copy[2] = decoids[3]; + copy[3] = decoids[5]; + copy[4] = decoids[0]; + copy[5] = decoids[2]; + i = encode(copy); + hash_[i] = true; + // 8 + copy[0] = decoids[1]; + copy[1] = decoids[0]; + copy[2] = decoids[4]; + copy[3] = decoids[5]; + copy[4] = decoids[2]; + copy[5] = decoids[3]; + i = encode(copy); + hash_[i] = true; + // 9 + copy[0] = decoids[2]; + copy[1] = decoids[5]; + copy[2] = decoids[0]; + copy[3] = decoids[4]; + copy[4] = decoids[3]; + copy[5] = decoids[1]; + i = encode(copy); + hash_[i] = true; + // 10 + copy[0] = decoids[2]; + copy[1] = decoids[3]; + copy[2] = decoids[5]; + copy[3] = decoids[4]; + copy[4] = decoids[1]; + copy[5] = decoids[0]; + i = encode(copy); + hash_[i] = true; + // 11 + copy[0] = decoids[2]; + copy[1] = decoids[1]; + copy[2] = decoids[3]; + copy[3] = decoids[4]; + copy[4] = decoids[0]; + copy[5] = decoids[5]; + i = encode(copy); + hash_[i] = true; + // 12 + copy[0] = decoids[2]; + copy[1] = decoids[0]; + copy[2] = decoids[1]; + copy[3] = decoids[4]; + copy[4] = decoids[5]; + copy[5] = decoids[3]; + i = encode(copy); + hash_[i] = true; + // 13 + copy[0] = decoids[3]; + copy[1] = decoids[5]; + copy[2] = decoids[2]; + copy[3] = decoids[0]; + copy[4] = decoids[4]; + copy[5] = decoids[1]; + i = encode(copy); + hash_[i] = true; + // 14 + copy[0] = decoids[3]; + copy[1] = decoids[4]; + copy[2] = decoids[5]; + copy[3] = decoids[0]; + copy[4] = decoids[1]; + copy[5] = decoids[2]; + i = encode(copy); + hash_[i] = true; + // 15 + copy[0] = decoids[3]; + copy[1] = decoids[1]; + copy[2] = decoids[4]; + copy[3] = decoids[0]; + copy[4] = decoids[2]; + copy[5] = decoids[5]; + i = encode(copy); + hash_[i] = true; + // 16 + copy[0] = decoids[3]; + copy[1] = decoids[2]; + copy[2] = decoids[1]; + copy[3] = decoids[0]; + copy[4] = decoids[5]; + copy[5] = decoids[4]; + i = encode(copy); + hash_[i] = true; + // 17 + copy[0] = decoids[4]; + copy[1] = decoids[1]; + copy[2] = decoids[0]; + copy[3] = decoids[2]; + copy[4] = decoids[3]; + copy[5] = decoids[5]; + i = encode(copy); + hash_[i] = true; + // 18 + copy[0] = decoids[4]; + copy[1] = decoids[3]; + copy[2] = decoids[1]; + copy[3] = decoids[2]; + copy[4] = decoids[5]; + copy[5] = decoids[0]; + i = encode(copy); + hash_[i] = true; + // 19 + copy[0] = decoids[4]; + copy[1] = decoids[5]; + copy[2] = decoids[3]; + copy[3] = decoids[2]; + copy[4] = decoids[0]; + copy[5] = decoids[1]; + i = encode(copy); + hash_[i] = true; + // 20 + copy[0] = decoids[4]; + copy[1] = decoids[0]; + copy[2] = decoids[5]; + copy[3] = decoids[2]; + copy[4] = decoids[1]; + copy[5] = decoids[3]; + i = encode(copy); + hash_[i] = true; + // 21 + copy[0] = decoids[5]; + copy[1] = decoids[0]; + copy[2] = decoids[2]; + copy[3] = decoids[1]; + copy[4] = decoids[4]; + copy[5] = decoids[3]; + i = encode(copy); + hash_[i] = true; + // 22 + copy[0] = decoids[5]; + copy[1] = decoids[4]; + copy[2] = decoids[0]; + copy[3] = decoids[1]; + copy[4] = decoids[3]; + copy[5] = decoids[2]; + i = encode(copy); + hash_[i] = true; + // 23 + copy[0] = decoids[5]; + copy[1] = decoids[3]; + copy[2] = decoids[4]; + copy[3] = decoids[1]; + copy[4] = decoids[2]; + copy[5] = decoids[0]; + i = encode(copy); + hash_[i] = true; + // 24 + copy[0] = decoids[5]; + copy[1] = decoids[2]; + copy[2] = decoids[3]; + copy[3] = decoids[1]; + copy[4] = decoids[0]; + copy[5] = decoids[4]; + i = encode(copy); + hash_[i] = true; + return true; +} + +int main() +{ + while (scanf("%d", &ndecoids) && ndecoids) + { + memset(hash_, 0, sizeof(hash_)); + int kase, kind = 0; + for (kase = 0; kase < ndecoids; kase++) + { + int i; + for (i = 0; i < 6; i++) + { + scanf("%d", &decoids[i]); + } + if (solve()) + { + kind++; + } + } + printf("%d\n", kind); + } + return 0; +} diff --git a/10878.cpp b/10878.cpp index 03c2567..a3a68fe 100644 --- a/10878.cpp +++ b/10878.cpp @@ -1,62 +1,27 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int val[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; +char str[20]; -char calc(string S){ - int L = S.length() - 1, i; - int V = 0; - FOI(i, 0, L) - if (S[i] == '1') - V += val[i]; - //cout << V << " "; - return (char)V; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string S; - getline(cin, S); - while (!cin.eof()) { - string str = ""; - int L = S.length() - 1, i; - FOD(i, L, 0) { - if (S[i] == ' ') str += "0"; - else if (S[i] == 'o') str += "1"; - else continue; +int main() +{ + gets(str); + while (gets(str) && str[0] == '|') + { + int i, val = 0; + for (i = 1; str[i] != '|'; i++) + { + if (str[i] == 'o') + { + val = val * 2 + 1; + } + if (str[i] == ' ') + { + val = val * 2; + } } - //cout << str << endl; - if (str != "") cout << calc(str); - getline(cin, S); + printf("%c", val); } return 0; } - diff --git a/10879.cpp b/10879.cpp index b1e13f2..e584009 100644 --- a/10879.cpp +++ b/10879.cpp @@ -1,27 +1,40 @@ -#include -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - int K=0; - cin>>K; - int N1=0,N2=0; - for(int j=2;j<=sqrt(K);j++){ - if(j*(K/j)==K){ - N1=j; - break; - } - } - for(int j=N1+1;j<=sqrt(K);j++){ - if(j*(K/j)==K && j!=N1 && j!=(K/N1)){ - N2=j; - break; - } - } - cout<<"Case #"< + +using namespace std; + +/* +10879 +*/ +void Cal(int n) +{ + int i; + printf(" %d = ", n); + for (i = 2; i * i <= n; i++) + { + if (n % i == 0) + { + printf("%d * %d = ", i, n / i); + break; + } + } + for (++i; i * i <= n; i++) + { + if (n % i == 0) + { + printf("%d * %d\n", i, n / i); + break; + } + } +} +int main() +{ + int n, ks, c = 1; + scanf("%d", &n); + while (n--) + { + scanf("%d", &ks); + printf("Case #%d: ", c++); + Cal(ks); + } + return 0; +} diff --git a/1088.cpp b/1088.cpp new file mode 100644 index 0000000..46bf77b --- /dev/null +++ b/1088.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +#define pb push_back +#define mp make_pair +#define X first +#define Y second +const int N = 105; +const int M = 6005; +const int INF = 1000000005; +typedef pair per; +vector E[N]; +bool u[N]; +int dp[N][M], m1[M], m2[M], ld, tmp[M]; +void dfs(int x) +{ + u[x] = true; + dp[x][0] = 0; + for (int i = E[x].size() - 1; i >= 0; i--) + { + int j = E[x][i].first, t = E[x][i].second; + if (!u[j]) + { + dfs(j); + for (int k = 0; k <= ld; k++) + { + tmp[k] = dp[x][k]; + dp[x][k] = INF; + } + m1[0] = tmp[0]; + m2[0] = dp[j][0]; + for (int k = 1; k <= ld; k++) + { + m1[k] = min(m1[k - 1], tmp[k]); + m2[k] = min(m2[k - 1], dp[j][k]); + } + for (int k = t; k <= ld; k++) + { + int h = tmp[k]; + if (h > ld || k + t > ld) + { + continue; + } + int xd = ld - t - k; + if (xd > k - t) + { + xd = k - t; + } + if (xd < 0) + { + continue; + } + int hh = m2[xd]; + if (hh + h - t > ld) + { + continue; + } + int nh = max(h, hh - t); + if (dp[x][k] > nh) + { + dp[x][k] = nh; + } + } + for (int k = 0; k + t <= ld; k++) + { + int h = dp[j][k]; + if (h > ld || k + t > ld) + { + continue; + } + int xd = ld - t - k; + if (xd > k + t) + { + xd = k + t; + } + int hh = m1[xd]; + if (hh + h - t > ld) + { + continue; + } + int nh = max(hh, h - t); + if (dp[x][k + t] > nh) + { + dp[x][k + t] = nh; + } + } + if (!t) + { + continue; + } + //--------- + t = 60 - t; + for (int k = 0; k <= ld; k++) + { + int h = tmp[k]; + if (h + t > ld) + { + continue; + } + int xd = ld + t - k;//k+xd-t<=ld + if (xd > k + t) + { + xd = k + t; + } + if (xd > ld) + { + xd = ld; + } + int hh = m2[xd]; + if (hh + h + t > ld) + { + continue; + } + int nh = max(h, hh + t); + if (dp[x][k] > nh) + { + dp[x][k] = nh; + } + } + for (int k = t; k <= ld; k++) + { + int h = dp[j][k]; + if (h + t > ld) + { + continue; + } + int xd = ld + t - k;//k+xd-t<=ld + if (xd > k - t) + { + xd = k - t; + } + if (xd > ld) + { + xd = ld; + } + int hh = m1[xd]; + if (hh + h + t > ld) + { + continue; + } + int nh = max(h + t, hh); + if (dp[x][k - t] > nh) + { + dp[x][k - t] = nh; + } + } + } + } +} +bool check(int x) +{ + ld = x; + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j <= ld; j++) + { + dp[i][j] = INF; + } + } + memset(u, 0, sizeof(u)); + dfs(1); + for (i = 0; i <= ld; i++) + { + if (dp[1][i] <= ld) + { + return true; + } + } + return false; +} +int main() +{ + int n, i, ca = 1; + while (~scanf("%d", &n)) + { + if (!n) + { + break; + } + for (i = 1; i < n; i++) + { + int a, b, c; + scanf("%d%d%d", &a, &b, &c); + c %= 60; + E[a].push_back(mp(b, c)); + E[b].push_back(mp(a, c)); + } + int low = -1, high = 10000, mid; + while (low < high - 1) + { + int mid = (low + high) / 2; + if (check(mid)) + { + high = mid; + } + else + { + low = mid; + } + } + printf("Case %d: %d\n", ca++, high); + for (i = 1; i <= n; i++) + { + E[i].clear(); + } + } + return 0; +} +/* +5 +2 1 51 +2 3 27 +3 4 0 +5 1 40 + +5 +1 2 59 +1 3 42 +2 4 40 +4 5 24 +r1:37 r2:39 +i:0 A:2 B:1 C:59 +i:1 A:3 B:1 C:42 +i:2 A:4 B:2 C:40 +i:3 A:5 B:4 C:24 +0 + */ diff --git a/10880.cpp b/10880.cpp new file mode 100644 index 0000000..84ee1aa --- /dev/null +++ b/10880.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +vector F; + +void Cal(int n, int m) +{ + int i, d; + if (n == m) + { + printf(" 0\n"); + return; + } + d = n - m; + if (m >= d) + { + printf("\n"); + return; + } + for (i = 1; i * i <= d; i++) + { + if (d % i == 0) + { + if (i > m) + F.push_back(i); + if (d / i > m && i != d / i) + F.push_back(d / i); + } + } + sort(F.begin(), F.end()); + for (i = 0; i < F.size(); i++) + { + printf(" %d", F[i]); + } + printf("\n"); + F.clear(); +} + +int main() +{ + int n, ks, m, k = 1; + scanf("%d", &ks); + while (ks--) + { + scanf("%d%d", &n, &m); + printf("Case #%d:", k++); + Cal(n, m); + } + return 0; +} diff --git a/10881.cpp b/10881.cpp new file mode 100644 index 0000000..4dc052d --- /dev/null +++ b/10881.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +/* + * AUTHOR : benbendog + * DATE : 2011/3/15 + * PROBLEM: lots of ants on a line, going either left or right. when + 2 ants meet, they go in reverse direction. Ask after T seconds, + where is the ants and their direction ? + * SOLUTION: the ants' relative position would not change and we can + regard that the ants would not change, actually that's the + final answer for the position, and the remaining problem is + which direction the ant is ? again actually the ant succeed + the other ant direction so just find the final position for + each ant, then use sort and the idea of relative position to + get the answer + */ + +struct point +{ + int pos; + char dir; + int ind; +}; +int cmp(const point &a, const point &b); +int main() +{ + int cas; + int cnt = 1; + int L, T, n; + scanf("%d", &cas); + while (cas--) + { + scanf("%d %d %d", &L, &T, &n); + struct point p[n]; + for (int i = 0; i < n; i++) + { + scanf("%d %c", &(p[i].pos), &(p[i].dir)); + p[i].ind = i; + } + sort(p, p + n, cmp); + int index[n]; + for (int i = 0; i < n; i++) + { + index[p[i].ind] = i; + } + for (int i = 0; i < n; i++) + { + if (p[i].dir == 'L') + { + p[i].pos -= T; + } + else + { + p[i].pos += T; + } + } + sort(p, p + n, cmp); + printf("Case #%d:\n", cnt++); + for (int i = 0; i < n; i++) + { + bool turning = false; + int pos = p[index[i]].pos; + int left, right; + left = index[i] - 1; + right = index[i] + 1; + if (left > -1 && p[left].pos == pos) + { + turning = true; + } + if (right < n && p[right].pos == pos) + { + turning = true; + } + if (pos > L || pos < 0) + { + printf("Fell off\n"); + } + else + { + printf("%d ", p[index[i]].pos); + if (turning) + { + printf("Turning\n"); + } + else + { + printf("%c\n", p[index[i]].dir); + } + } + } + printf("\n"); + } + return 0; +} +int cmp(const point &a, const point &b) +{ + return a.pos < b.pos; +} diff --git a/10882.cpp b/10882.cpp new file mode 100644 index 0000000..d6380f2 --- /dev/null +++ b/10882.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +/* + * AUTHOR : benbendog + * DATE : 2011/3/13 + * PROBLEM: 3 people having 100 numbers with label(0-100) on it + * given intersection of A^B, A^C, B^C of the same label + * find the range of A^B^C + * SOLUTION: the max is min(A^B,A^C,B^C) + the min is A^B + A^C + B^C - 2x <= 100 + x is min + * split 100 numbers into 4 parts + Xi Yi Zi Ii, i = (1~3) + X1 and X2 is A^B - A^B^C, Y1 and Y3 is A^C - A^B^C , Z2 and Z3 is + B^C - A^B^C + since each people has exactly 100 numbers + so Xi + Yi+ Zi can not > 100, Ii = A^B^C + => A^B + A^C + B^C - 2x <= 100 + */ +int main() +{ + int cas; + int cnt = 1; + cin >> cas; + while (cas--) + { + int a, b, c; + cin >> a >> b >> c; + int lb, ub; + lb = (a + b + c + 1 - 100) >> 1; + if (lb < 0) + { + lb = 0; + } + ub = min(a, b); + ub = min(ub, c); + cout << "Case #" << cnt++ << ": "; + if (lb > ub) + { + cout << "The records are faulty." << endl; + } + else + { + cout << "Between " << lb << " and " << ub << " times." << endl; + } + } + return 0; +} diff --git a/10883.cpp b/10883.cpp index 1a254f9..7e92836 100644 --- a/10883.cpp +++ b/10883.cpp @@ -1,67 +1,31 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -double power(int N){ - if( N == 0 ) - return 1.0; - if( N == 1 ) - return 2.0; - double val = power(N/2); - val = val * val; - if( N % 2 == 0 ) - return val; - else - return val * 2.0; -} +using namespace std; -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int T, test; - scanf("%d", &T); - FOI(test, 1, T){ - int i, N; - scanf("%d", &N); - --N; - double num[N+1], coef[N+1], val = 0.0; - FOI(i, 0, N) - scanf("%lf", &num[i]); - coef[0] = 1; - FOI(i, 1, N) - coef[i] = ( coef[i-1] * (N - i + 1) ) / i; - FOI(i, 0, N) - val += coef[i] * num[i]; - val /= power(N); - printf("Case #%d: %.3lf", test, val ); - if( T > 0 ) - printf("\n"); +double E[50005]; +int main() +{ + int t, tt, n, i, j; + double ans, x, p, lg = log(2.0); + for (i = 1; i < 50005; i++) + { + E[i] = log(i); + } + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d", &n); + n--; + p = -n * lg; + scanf("%lf", &x); + ans = x * exp(p); + for (i = 1, j = n; i <= n; i++, j--) + { + p = p + E[j] - E[i]; + scanf("%lf", &x); + ans += x * exp(p); + } + printf("Case #%d: %.3lf\n", t, ans); } return 0; } diff --git a/10884.cpp b/10884.cpp new file mode 100644 index 0000000..969a067 --- /dev/null +++ b/10884.cpp @@ -0,0 +1,413 @@ +#include + +using namespace std; + +int main() +{ + __asm__ __volatile__( + " pushl %ebp\n" + " movl %esp, %ebp\n" + " pushl %edi\n" + " pushl %esi\n" + " pushl %ebx\n" + " subl $44, %esp\n" + " movl $48, %ecx\n" + " .p2align 4,,15\n" + "L6:\n" + " movl $1, %eax\n" + " movl %eax, _isdig.3(,%ecx,4)\n" + " incl %ecx\n" + " cmpl $57, %ecx\n" + " jle L6\n" + " movl $65536, %eax\n" + " movl %eax, 8(%esp)\n" + " movl $_inbuf.0, %edi\n" + " movl %edi, 4(%esp)\n" + " movl $0, (%esp)\n" + " call read\n" + " movb $0, _inbuf.0(%eax)\n" + " movl $_outbuf.1, %esi\n" + " movsbl _inbuf.0,%ecx\n" + " movl _isdig.3(,%ecx,4), %edx\n" + " testl %edx, %edx\n" + " jne L62\n" + " .p2align 4,,15\n" + "L10:\n" + " incl %edi\n" + " movzbl (%edi), %ecx\n" + " movb %cl, -25(%ebp)\n" + " movsbl %cl,%edx\n" + " movl _isdig.3(,%edx,4), %ebx\n" + " testl %ebx, %ebx\n" + " je L10\n" + "L47:\n" + " movsbl -25(%ebp),%ecx\n" + " subl $48, %ecx\n" + " movl %ecx, -24(%ebp)\n" + " incl %edi\n" + " movzbl (%edi), %edx\n" + " movb %dl, -25(%ebp)\n" + " movsbl %dl,%eax\n" + " movl _isdig.3(,%eax,4), %ebx\n" + " testl %ebx, %ebx\n" + " je L49\n" + " movl %eax, %edx\n" + " .p2align 4,,15\n" + "L15:\n" + " movl -24(%ebp), %ebx\n" + " leal (%ebx,%ebx,4), %eax\n" + " leal -48(%edx,%eax,2), %ebx\n" + " movl %ebx, -24(%ebp)\n" + " incl %edi\n" + " movzbl (%edi), %edx\n" + " movb %dl, -25(%ebp)\n" + " movsbl %dl,%ecx\n" + " movl %ecx, %edx\n" + " movl _isdig.3(,%ecx,4), %ebx\n" + " testl %ebx, %ebx\n" + " jne L15\n" + "L49:\n" + " movl $1, -20(%ebp)\n" + " movl -24(%ebp), %eax\n" + " cmpl %eax, -20(%ebp)\n" + " jg L51\n" + " .p2align 4,,15\n" + "L43:\n" + " movb $67, (%esi)\n" + " incl %esi\n" + " movb $97, (%esi)\n" + " incl %esi\n" + " movb $115, (%esi)\n" + " incl %esi\n" + " movb $101, (%esi)\n" + " incl %esi\n" + " movb $32, (%esi)\n" + " incl %esi\n" + " movb $35, (%esi)\n" + " incl %esi\n" + " movl -20(%ebp), %ecx\n" + " movl $_tmp.2, %ebx\n" + " testl %ecx, %ecx\n" + " jle L53\n" + " .p2align 4,,15\n" + "L24:\n" + " movl $1717986919, %eax\n" + " imull %ecx\n" + " movl %eax, -40(%ebp)\n" + " movl %edx, -36(%ebp)\n" + " movl -36(%ebp), %edx\n" + " sarl $2, %edx\n" + " movl %ecx, %eax\n" + " sarl $31, %eax\n" + " subl %eax, %edx\n" + " leal (%edx,%edx,4), %eax\n" + " addl %eax, %eax\n" + " subl %eax, %ecx\n" + " movb %cl, %al\n" + " addb $48, %al\n" + " movb %al, (%ebx)\n" + " incl %ebx\n" + " movl %edx, %ecx\n" + " testl %edx, %edx\n" + " jg L24\n" + " movzbl (%edi), %ecx\n" + " movb %cl, -25(%ebp)\n" + "L53:\n" + " decl %ebx\n" + " cmpl $_tmp.2-1, %ebx\n" + " jbe L55\n" + " .p2align 4,,15\n" + "L28:\n" + " movzbl (%ebx), %edx\n" + " movb %dl, (%esi)\n" + " incl %esi\n" + " decl %ebx\n" + " cmpl $_tmp.2-1, %ebx\n" + " ja L28\n" + "L55:\n" + " movb $58, (%esi)\n" + " incl %esi\n" + " movb $32, (%esi)\n" + " incl %esi\n" + " movsbl -25(%ebp),%ebx\n" + " movl _isdig.3(,%ebx,4), %ecx\n" + " testl %ecx, %ecx\n" + " jne L57\n" + " .p2align 4,,15\n" + "L32:\n" + " incl %edi\n" + " movzbl (%edi), %eax\n" + " movb %al, -25(%ebp)\n" + " movsbl %al,%ebx\n" + " movl _isdig.3(,%ebx,4), %edx\n" + " testl %edx, %edx\n" + " je L32\n" + "L57:\n" + " movsbl -25(%ebp),%ebx\n" + " leal -48(%ebx), %ecx\n" + " incl %edi\n" + " movzbl (%edi), %edx\n" + " movb %dl, -25(%ebp)\n" + " movsbl %dl,%eax\n" + " movl _isdig.3(,%eax,4), %ebx\n" + " testl %ebx, %ebx\n" + " je L59\n" + " movl %eax, %edx\n" + " .p2align 4,,15\n" + "L37:\n" + " leal (%ecx,%ecx,4), %ebx\n" + " leal -48(%edx,%ebx,2), %ecx\n" + " incl %edi\n" + " movzbl (%edi), %edx\n" + " movb %dl, -25(%ebp)\n" + " movsbl %dl,%ebx\n" + " movl %ebx, %edx\n" + " movl _isdig.3(,%ebx,4), %eax\n" + " testl %eax, %eax\n" + " jne L37\n" + "L59:\n" + " movl _sol(,%ecx,4), %ebx\n" + " movzbl (%ebx), %eax\n" + " testb %al, %al\n" + " je L61\n" + " .p2align 4,,15\n" + "L42:\n" + " incl %ebx\n" + " movb %al, (%esi)\n" + " incl %esi\n" + " movzbl (%ebx), %eax\n" + " testb %al, %al\n" + " jne L42\n" + "L61:\n" + " movb $10, (%esi)\n" + " incl %esi\n" + " incl -20(%ebp)\n" + " movl -24(%ebp), %ecx\n" + " cmpl %ecx, -20(%ebp)\n" + " jle L43\n" + " jmp L51\n" + "L62:\n" + " movzbl _inbuf.0, %eax\n" + " movb %al, -25(%ebp)\n" + " jmp L47\n" + "L51:\n" + " subl $_outbuf.1, %esi\n" + " movl %esi, 8(%esp)\n" + " movl $_outbuf.1, %esi\n" + " movl %esi, 4(%esp)\n" + " movl $1, (%esp)\n" + " call write\n" + " movl $0, (%esp)\n" + " call exit\n" + "LC0:\n" + " .ascii \"0\\0\"\n" + "LC1:\n" + " .ascii \"1\\0\"\n" + "LC2:\n" + " .ascii \"2\\0\"\n" + "LC3:\n" + " .ascii \"7\\0\"\n" + "LC4:\n" + " .ascii \"28\\0\"\n" + "LC5:\n" + " .ascii \"120\\0\"\n" + "LC6:\n" + " .ascii \"528\\0\"\n" + "LC7:\n" + " .ascii \"2344\\0\"\n" + "LC8:\n" + " .ascii \"10416\\0\"\n" + "LC9:\n" + " .ascii \"46160\\0\"\n" + "LC10:\n" + " .ascii \"203680\\0\"\n" + "LC11:\n" + " .ascii \"894312\\0\"\n" + "LC12:\n" + " .ascii \"3907056\\0\"\n" + "LC13:\n" + " .ascii \"16986352\\0\"\n" + "LC14:\n" + " .ascii \"73512288\\0\"\n" + "LC15:\n" + " .ascii \"316786960\\0\"\n" + "LC16:\n" + " .ascii \"1359763168\\0\"\n" + "LC17:\n" + " .ascii \"5815457184\\0\"\n" + "LC18:\n" + " .ascii \"24788842304\\0\"\n" + "LC19:\n" + " .ascii \"105340982248\\0\"\n" + "LC20:\n" + " .ascii \"446389242480\\0\"\n" + "LC21:\n" + " .ascii \"1886695382192\\0\"\n" + "LC22:\n" + " .ascii \"7955156287456\\0\"\n" + "LC23:\n" + " .ascii \"33468262290096\\0\"\n" + "LC24:\n" + " .ascii \"140516110684832\\0\"\n" + "LC25:\n" + " .ascii \"588832418973280\\0\"\n" + "LC26:\n" + " .ascii \"2463133441338048\\0\"\n" + "LC27:\n" + " .ascii \"10286493304041104\\0\"\n" + "LC28:\n" + " .ascii \"42892130604098656\\0\"\n" + "LC29:\n" + " .ascii \"178592047539343200\\0\"\n" + "LC30:\n" + " .ascii \"742609229473744320\\0\"\n" + "LC31:\n" + " .ascii \"3083957343567791392\\0\"\n" + "LC32:\n" + " .ascii \"12792021060576424896\\0\"\n" + "LC33:\n" + " .ascii \"53000868925259947840\\0\"\n" + "LC34:\n" + " .ascii \"219365134324873522816\\0\"\n" + "LC35:\n" + " .ascii \"907023528883142832360\\0\"\n" + "LC36:\n" + " .ascii \"3746790354386182679408\\0\"\n" + "LC37:\n" + " .ascii \"15463645062002474062384\\0\"\n" + "LC38:\n" + " .ascii \"63767018378178067474656\\0\"\n" + "LC39:\n" + " .ascii \"262742756317344213209200\\0\"\n" + "LC40:\n" + " .ascii \"1081765434874991509707040\\0\"\n" + "LC41:\n" + " .ascii \"4450606984357021640248032\\0\"\n" + "LC42:\n" + " .ascii \"18298022787758605020282816\\0\"\n" + "LC43:\n" + " .ascii \"75179913955330333724697136\\0\"\n" + "LC44:\n" + " .ascii \"308691924054843201409922592\\0\"\n" + "LC45:\n" + " .ascii \"1266737680502193374869298720\\0\"\n" + "LC46:\n" + " .ascii \"5195143014579351011947302208\\0\"\n" + "LC47:\n" + " .ascii \"21294548056433354780482923744\\0\"\n" + "LC48:\n" + " .ascii \"87238762619153966026251258944\\0\"\n" + // " .align 32\n" + "LC49:\n" + " .ascii \"357215388993130669706869321408\\0\"\n" + // " .align 32\n" + "_sol:\n" + " .long LC0\n" + " .long LC0\n" + " .long LC0\n" + " .long LC0\n" + " .long LC1\n" + " .long LC0\n" + " .long LC2\n" + " .long LC0\n" + " .long LC3\n" + " .long LC0\n" + " .long LC4\n" + " .long LC0\n" + " .long LC5\n" + " .long LC0\n" + " .long LC6\n" + " .long LC0\n" + " .long LC7\n" + " .long LC0\n" + " .long LC8\n" + " .long LC0\n" + " .long LC9\n" + " .long LC0\n" + " .long LC10\n" + " .long LC0\n" + " .long LC11\n" + " .long LC0\n" + " .long LC12\n" + " .long LC0\n" + " .long LC13\n" + " .long LC0\n" + " .long LC14\n" + " .long LC0\n" + " .long LC15\n" + " .long LC0\n" + " .long LC16\n" + " .long LC0\n" + " .long LC17\n" + " .long LC0\n" + " .long LC18\n" + " .long LC0\n" + " .long LC19\n" + " .long LC0\n" + " .long LC20\n" + " .long LC0\n" + " .long LC21\n" + " .long LC0\n" + " .long LC22\n" + " .long LC0\n" + " .long LC23\n" + " .long LC0\n" + " .long LC24\n" + " .long LC0\n" + " .long LC25\n" + " .long LC0\n" + " .long LC26\n" + " .long LC0\n" + " .long LC27\n" + " .long LC0\n" + " .long LC28\n" + " .long LC0\n" + " .long LC29\n" + " .long LC0\n" + " .long LC30\n" + " .long LC0\n" + " .long LC31\n" + " .long LC0\n" + " .long LC32\n" + " .long LC0\n" + " .long LC33\n" + " .long LC0\n" + " .long LC34\n" + " .long LC0\n" + " .long LC35\n" + " .long LC0\n" + " .long LC36\n" + " .long LC0\n" + " .long LC37\n" + " .long LC0\n" + " .long LC38\n" + " .long LC0\n" + " .long LC39\n" + " .long LC0\n" + " .long LC40\n" + " .long LC0\n" + " .long LC41\n" + " .long LC0\n" + " .long LC42\n" + " .long LC0\n" + " .long LC43\n" + " .long LC0\n" + " .long LC44\n" + " .long LC0\n" + " .long LC45\n" + " .long LC0\n" + " .long LC46\n" + " .long LC0\n" + " .long LC47\n" + " .long LC0\n" + " .long LC48\n" + " .long LC0\n" + " .long LC49\n" + " .long LC0\n" + ".lcomm _tmp.2,256\n" + ".lcomm _isdig.3,1024\n" + ".lcomm _inbuf.0,65536\n" + ".lcomm _outbuf.1,131072\n" + ); + return 0; +} diff --git a/10886.cpp b/10886.cpp new file mode 100644 index 0000000..e171ef0 --- /dev/null +++ b/10886.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +unsigned gen(unsigned s, long double *r) +{ + static const long double Z = (long double)1.0 / (1LL << 32); + unsigned a = s >> 16, b = s & 0xFFFF; + *r = ((unsigned long long)s * (unsigned long long)s) * Z; + return ((a * a) << 16) + 2 * a * b + ((b * b) >> 16); +} + +long double solve(int n, unsigned s) +{ + unsigned s1, s2, i, j, k, st, len; + long double m1, m2, r, x; + for (s1 = s2 = s, i = j = 0, m1 = m2 = 0.; j < n; i++, j++) + { + if (s1 == s2 && i != j) + { + break; + } + s1 = gen(s1, &x); + m1 += x; + s2 = gen(s2, &x); + m2 += x; + if (++j == n) + { + break; + } + s2 = gen(s2, &x); + m2 += x; + } + if (j == n) + { + m2 /= (long double)n; + for (r = 0, s1 = s, i = 0; i < n; i++) + { + s1 = gen(s1, &x); + x -= m2; + r += x * x; + } + return (long double)sqrt(r / (long double)n); + } + st = i; + len = j - i; + k = (n - j) / len; + for (m2 += k * (m2 - m1), j += k * len; j < n; j++) + { + s2 = gen(s2, &x); + m2 += x; + } + m2 /= (long double)n; + for (r = 0, s1 = s, i = 0; i < st; i++) + { + s1 = gen(s1, &x); + x -= m2; + r += x * x; + } + for (m1 = 0., j = 0; j < len; j++) + { + s1 = gen(s1, &x); + x -= m2; + m1 += x * x; + } + for (r += k * m1, i += k * len; i < n; i++) + { + s1 = gen(s1, &x); + x -= m2; + r += x * x; + } + return (long double)sqrt(r / (long double)n); +} + +int main() +{ + int n, c, t; + unsigned long long s; + for (scanf("%d", &t), c = 1; c <= t && scanf("%d %llu", &n, &s) == 2; c++) + { + printf("Case #%d: %.5Lf\n", c, solve(n, (s >> 16) & 0xFFFFFFFF)); + } + return 0; +} diff --git a/10887.cpp b/10887.cpp index b72595d..e4956c8 100644 --- a/10887.cpp +++ b/10887.cpp @@ -1,73 +1,99 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(I, A, B) for (I=A; I<=B; I++) -#define FOD(I, A, B) for (I=A; I>=B; I--) +#include -int strToint(string str){ - stringstream sstr; - int I; - sstr << str; - sstr >> I; - return I; -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - int T, t; - getline(cin, str); - T = strToint(str); - FOI(t, 1, T){ - int i, j; - int M, N; - getline(cin, str); - stringstream sstr; - sstr << str; - sstr >> M >> N; - string sA[M], sB[N]; - FOI(i, 0, M-1) - getline(cin, sA[i]); - FOI(i, 0, N-1) - getline(cin, sB[i]); - - set ss; - FOI(i, 0, M-1){ - FOI(j, 0, N-1) - ss.insert( sA[i] + sB[j] ); +#define SZ 1000003 +struct link +{ + int v, a, b; + struct link *nx; +} * L[SZ]; +int HF[1505], HS[1505], len[1505], times[30]; +char F[1505][12], S[1505][12], ex1[24], ex2[24]; +int main() +{ + int t, tt, m, n, i, j, k, ans; + struct link *proc; + scanf("%d", &tt); + for (i = 0; i < SZ; i++) + { + L[i] = NULL; + } + for (i = times[0] = 1; i < 30; i++) + { + times[i] = (27 * times[i - 1]) % SZ; + } + for (t = 1; t <= tt; t++) + { + scanf("%d%d", &m, &n); + getchar(); + ans = 0; + for (i = 0; i < m; i++) + { + gets(F[i]); + for (j = k = 0; F[i][j]; j++) + { + k = (27 * k + F[i][j] - '`') % SZ; + } + HF[i] = k; + } + for (i = 0; i < n; i++) + { + gets(S[i]); + len[i] = strlen(S[i]); + for (j = k = 0; S[i][j]; j++) + { + k = (27 * k + S[i][j] - '`') % SZ; + } + HS[i] = k; + } + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + k = ((long long int)HF[i] * times[len[j]] + HS[j]) % SZ; + if (L[k] == NULL || L[k]->v != t) + { + for (proc = L[k]; proc != NULL; proc = L[k]) + { + L[k] = proc->nx; + free(proc); + } + proc = (link *)malloc(sizeof(struct link)); + proc->nx = NULL; + proc->v = t; + proc->a = i; + proc->b = j; + L[k] = proc; + ans++; + } + else + { + for (proc = L[k]; proc != NULL; proc = proc->nx) + { + strcpy(ex1, F[i]); + strcat(ex1, S[j]); + strcpy(ex2, F[proc->a]); + strcat(ex2, S[proc->b]); + if (strcmp(ex1, ex2) == 0) + { + break; + } + } + if (proc == NULL) + { + proc = (link *)malloc(sizeof(struct link)); + proc->nx = L[k]; + proc->v = t; + proc->a = i; + proc->b = j; + L[k] = proc; + ans++; + } + } + } } - - cout << "Case " << t << ": " << ss.size() << endl; + printf("Case %d: %d\n", t, ans); } return 0; } diff --git a/10888.cpp b/10888.cpp new file mode 100644 index 0000000..f0eb142 --- /dev/null +++ b/10888.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +const int dx[4] = {0, 0, 1, -1}; +const int dy[4] = {1, -1, 0, 0}; + +char g[50][50]; +int ng[50][50], used[50][50], dp[1 << 16], W[30][30], Bt, Xt, n, m; + +void bfs(int x, int y) +{ + memset(used, 0, sizeof(used)); + int i, tx, ty; + queue X, Y; + X.push(x), Y.push(y); + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + if (g[x][y] == 'X') + { + W[Bt][ng[x][y]] = used[x][y]; + } + for (i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || tx >= n || ty < 0 || ty >= m) + { + continue; + } + if (g[tx][ty] == '#') + { + continue; + } + if (used[tx][ty] == 0) + { + used[tx][ty] = used[x][y] + 1; + X.push(tx); + Y.push(ty); + } + } + } +} + +int main() +{ + int t, i, j, k; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &m); + for (i = 0; i < n; i++) + { + scanf("%s", g[i]); + } + Bt = 0, Xt = 0; + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + if (g[i][j] == 'X') + { + ng[i][j] = Xt++; + } + } + } + for (i = 0; i <= Xt; i++) + { + for (j = 0; j <= Xt; j++) + { + W[i][j] = 1 << 20; + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + if (g[i][j] == 'B') + { + Bt++; + bfs(i, j); + } + } + } + int mxState = (1 << Xt); + for (i = 0; i < mxState; i++) + { + dp[i] = 1 << 20; + } + dp[0] = 0; + vector DIGIT[16]; + for (i = 0; i < mxState; i++) + { + j = __builtin_popcount(i); + DIGIT[j].push_back(i); + } + for (i = 1; i <= Bt; i++) + { + for (int k1 = 0; k1 < DIGIT[i - 1].size(); k1++) + { + j = DIGIT[i - 1][k1]; + for (k = 0; k < Bt; k++) + { + if ((j & (1 << k)) == 0) + { + dp[j | (1 << k)] = min(dp[j | (1 << k)], dp[j] + W[i][k]); + } + } + } + } + printf("%d\n", dp[mxState - 1]); + } + return 0; +} diff --git a/10889.cpp b/10889.cpp new file mode 100644 index 0000000..2c09f3e --- /dev/null +++ b/10889.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int main() +{ + double R, B; + double b, c, sq; + double B1, B2; + char is_valid; + while (scanf("%lf %lf", &R, &B) == 2) + { + if (R == 0 && B == 0) + { + break; + } + b = 2 * R + 1; + c = R * R - R; + sq = sqrt(8 * R + 1); + if (fmod(sq, 1) == 0) + { + B1 = (b - sq) / 2; + B2 = (b + sq) / 2; + is_valid = 0; + if (B1 >= B && B1 * 0.7 <= B) + { + printf("%.0lf", B1 - B); + is_valid = 1; + } + if (B2 >= B && B2 * 0.7 <= B) + { + if (is_valid == 1) + { + printf(" "); + } + printf("%.0lf", B2 - B); + is_valid = 1; + } + if (is_valid == 0) + { + printf("No. of black balls invalid\n"); + } + else + { + printf("\n"); + } + } + else + { + printf("No. of red balls invalid\n"); + } + } + return 0; +} diff --git a/1089.cpp b/1089.cpp new file mode 100644 index 0000000..9bcf117 --- /dev/null +++ b/1089.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define MP make_pair +#define PI pair +#define FI first +#define SE second +#define PB push_back +#define SZ size() + +const double eps = 1e-10; +const double pi = acos(-1.); +const int mod = 1000000007; +const int maxn = 100100; +const long long INF = ((1LL) << 60); + +vector> g; +vector word, u; +map dic[22]; +long long dis[22][220][220]; + +void init(int len) +{ + set S; + for (int i = 0; i < word.SZ; ++i) + if (word[i].SZ >= len) + { + S.insert(word[i].substr(word[i].SZ - len)); + } + u = vector(S.begin(), S.end()); + dic[len].clear(); + for (int i = 0; i < u.SZ; ++i) + { + dic[len][u[i]] = i; + } +} + +void dp(int d) +{ + for (int i = 0; i < u.SZ; ++i) + { + for (int j = 0; j < u.SZ; ++j) + { + dis[d][i][j] = INF; + } + dis[d][i][i] = 0; + } + for (int i = 0; i < g.SZ; ++i) + if (g[i].FI.SZ == d) + { + int x = dic[d][g[i].FI]; + int y = dic[d][g[i].SE]; + dis[d][x][y] = min(dis[d][x][y], 1LL); + } + if (d > 1) + { + for (int i = 0; i < u.SZ; ++i) + for (int j = 0; j < u.SZ; ++j) + if (u[i][0] == u[j][0]) + { + int x = dic[d - 1][u[i].substr(1)]; + int y = dic[d - 1][u[j].substr(1)]; + dis[d][i][j] = min(dis[d][i][j], dis[d - 1][x][y]); + } + } + for (int k = 0; k < u.SZ; ++k) + for (int i = 0; i < u.SZ; ++i) + for (int j = 0; j < u.SZ; ++j) + { + dis[d][i][j] = min(dis[d][i][j], dis[d][i][k] + dis[d][k][j]); + } +} + +int main() +{ + int T, cases = 1; + int i, j, k, n; + string SS, TT, a, b; + while (cin >> SS && SS != ".") + { + cin >> TT >> n; + g.clear(); + word.clear(); + for (i = 0; i < n; ++i) + { + cin >> a >> b; + g.PB(MP(a, b)); + word.PB(a); + word.PB(b); + } + word.PB(SS); + word.PB(TT); + for (i = 1; i <= SS.SZ; ++i) + { + init(i); + dp(i); + } + i = dic[SS.SZ][SS]; + j = dic[SS.SZ][TT]; + if (dis[SS.SZ][i][j] == INF) + { + printf("Case %d: No solution\n", cases++); + } + else + { + printf("Case %d: %lld\n", cases++, dis[SS.SZ][i][j]); + } + } + return 0; +} diff --git a/10890.cpp b/10890.cpp new file mode 100644 index 0000000..9512890 --- /dev/null +++ b/10890.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define maxn 32 + +struct ss +{ + int r, c; + int cost; +}; +ss Point[maxn]; +int N, T, S, Min; +char Fg[maxn]; + +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + if (x->r != y->r) + return x->r - y->r; + return x->c - y->c; +} + +bool Recur(int n, int level, int cost, int lim) +{ + int i, d; + if (cost > lim) + return false; + if (level - 1 == S) + { + cost += (N - 1 - Point[n].r) + (N - 1 - Point[n].c); + return cost <= lim; + } + d = (N - 1 - Point[n].r) + (N - 1 - Point[n].c) + cost; + if (d > lim) + return false; + Fg[n] = 1; + for (i = 1; i <= T; i++) + { + if (Fg[i] == 0) + { + d = abs(Point[i].r - Point[n].r) + abs(Point[i].c - Point[n].c); + if (Recur(i, level + 1, cost + d, lim)) + { + Fg[n] = 0; + return true; + } + } + } + Fg[n] = 0; + return false; +} + +bool Dynamic() +{ + int i, j; + for (i = 1; i <= T; i++) + { + for (j = i - 1; j >= 0; j--) + { + if (Point[j].r <= Point[i].r && Point[j].c <= Point[i].c) + { + if (Point[i].c < Point[j].cost + 1) + { + Point[i].cost = Point[j].cost + 1; + if (Point[i].cost == S) + return true; + } + } + } + } + return false; +} + +int Greedy() +{ + int i, cost = 0; + for (i = 1; i <= S; i++) + cost += abs(Point[i].r - Point[i - 1].r) + abs(Point[i].c - Point[i - 1].c); + cost += abs(Point[i - 1].r - (N - 1)) + abs(Point[i - 1].c - (N - 1)); + return cost; +} + +void Cal() +{ + int i, d; + qsort(Point, T + 1, sizeof(ss), com); + if (Dynamic()) + { + printf("%d", (N - 1) * 2); + return; + } + d = Greedy(); + for (i = d - 1;; i--) + { + if (Recur(0, 1, 0, i) == false) + { + printf("%d", i + 1); + return; + } + } +} + +int main() +{ + int i, k = 1; + Point[0].r = Point[0].c = 0; + Point[0].cost = 0; + while (scanf("%d%d%d", &N, &T, &S) == 3) + { + if (!N && !T && !S) + break; + Min = 10000000; + for (i = 1; i <= T; i++) + { + scanf("%d%d", &Point[i].r, &Point[i].c); + Point[i].cost = 0; + } + if (T == 0) + { + printf("Case %d: %d\n", k++, N + N - 2); + continue; + } + printf("Case %d: ", k++); + Cal(); + printf("\n"); + } + return 0; +} diff --git a/10891.cpp b/10891.cpp new file mode 100644 index 0000000..79684da --- /dev/null +++ b/10891.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +char done[101][101]; +int dp[101][101], A[101]; + +int dfs(int l, int r) +{ + if (l > r) + { + return 0; + } + if (done[l][r]) + { + return dp[l][r]; + } + int sum; + dp[l][r] = -(1 << 30); + done[l][r] = 1; + for (int i = l, sum = 0; i <= r; i++) + { + sum += A[i]; + dp[l][r] = max(dp[l][r], sum - dfs(i + 1, r)); + } + for (int i = r, sum = 0; i >= l; i--) + { + sum += A[i]; + dp[l][r] = max(dp[l][r], sum - dfs(l, i - 1)); + } + return dp[l][r]; +} + +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; i++) + { + scanf("%d", A + i); + } + for (int i = 0; i <= n; i++) + { + for (int j = 0; j <= n; j++) + { + done[i][j] = 0; + } + } + printf("%d\n", dfs(0, n - 1)); + } + return 0; +} diff --git a/10892.cpp b/10892.cpp new file mode 100644 index 0000000..2938843 --- /dev/null +++ b/10892.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define MAXN 2000000 + +typedef long long LL; + +int ind, N, div_[MAXN]; + +LL gcd(LL a, LL b) +{ + return b ? gcd(b, a % b) : a; +} + +void getdiv_() +{ + int i; + ind = 0; + for (i = 1; i * i <= N; i++) + { + if (N % i == 0) + { + div_[ind++] = i; + if (N / i != i) + { + div_[ind++] = N / i; + } + } + } +} + +LL lcm(LL a, LL b) +{ + LL gc = gcd(a, b); + LL lc = (a * b) / gc; + return lc; +} + +void cal() +{ + int i, j, total = 0; + LL lcm_; + if (N == 1) + { + printf("1\n"); + return; + } + for (i = 0; i + 1 < ind; i++) + { + for (j = i + 1; j < ind; j++) + { + lcm_ = lcm(div_[i], div_[j]); + if (lcm_ == N) + { + total++; + } + } + } + printf("%d\n", total + 1); +} + +int main() +{ + while (scanf("%d", &N) && N) + { + getdiv_(); + printf("%d ", N); + cal(); + } + return 0; +} diff --git a/10893.cpp b/10893.cpp new file mode 100644 index 0000000..6e67fb3 --- /dev/null +++ b/10893.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +int n, d, s; +char list_[81], list_x[81], list_y[81], temp[2][81], + row[10][10], column[10][10], area[4][4][10], + map_[10][10]; + +int dfs(int depth) +{ + int i, l, x, y; + if (depth == -1) + { + memcpy(temp[s++], list_, sizeof(list_)); + return 1; + } + x = list_x[depth]; + y = list_y[depth]; + if (map_[x][y] != '.') + { + return dfs(depth - 1); + } + for (i = 9, l = 0; i >= 1; i--) + { + if (row[x][i] == 0 && column[y][i] == 0 && area[x / 3][y / 3][i] == 0) + { + row[x][i] = column[y][i] = area[x / 3][y / 3][i] = 1; + list_[depth] = i; + if (dfs(depth - 1)) + { + if (s == 2) + { + row[x][i] = column[y][i] = area[x / 3][y / 3][i] = 0; + return 1; + } + l = 1; + } + row[x][i] = column[y][i] = area[x / 3][y / 3][i] = 0; + } + } + return l; +} + +int main() +{ + int cnt, i, j, k, x, y; + scanf("%d", &cnt); + while (cnt--) + { + memset(row, 0, sizeof(row)); + memset(column, 0, sizeof(column)); + memset(area, 0, sizeof(area)); + for (i = 0, n = 0; i < 9; i++) + { + scanf("%s", map_[i]); + for (j = 0; j < 9; j++) + { + if (map_[i][j] == '.') + { + list_x[n] = i; + list_y[n] = j; + n++; + } + else + { + row[i][map_[i][j] - '0'] = 1; + column[j][map_[i][j] - '0'] = 1; + area[i / 3][j / 3][map_[i][j] - '0'] = 1; + } + } + } + for (;;) + { + s = 0; + dfs(n - 1); + if (s < 2) + { + break; + } + for (i = n - 1; i >= 0; i--) + { + if (temp[0][i] != temp[1][i]) + { + x = list_x[i]; + y = list_y[i]; + map_[x][y] = (j = temp[0][i]) + '0'; + row[x][j] = column[y][j] = area[x / 3][y / 3][j] = 1; + temp[0][i] = temp[1][i] = 0; + break; + } + } + } + for (i = 0; i < 9; i++) + { + printf("%s\n", map_[i]); + } + if (cnt) + { + printf("\n"); + } + } + return 0; +} diff --git a/10894.cpp b/10894.cpp new file mode 100644 index 0000000..c075345 --- /dev/null +++ b/10894.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +int main() +{ + string horizontal[5] = + { + "*****..***..*...*.*****...*...*.*****.*****.***...*****.*...*", + "*.....*...*.*...*.*.......*...*.*...*...*...*..*..*...*..*.*.", + "*****.*****.*...*.***.....*****.*****...*...*...*.*...*...*..", + "....*.*...*..*.*..*.......*...*.*.*.....*...*..*..*...*...*..", + "*****.*...*...*...*****...*...*.*..**.*****.***...*****...*.."}; + string vertical[61] = + { + "*****", + "*....", + "*****", + "....*", + "*****", + ".....", + ".***.", + "*...*", + "*****", + "*...*", + "*...*", + ".....", + "*...*", + "*...*", + "*...*", + ".*.*.", + "..*..", + ".....", + "*****", + "*....", + "***..", + "*....", + "*****", + ".....", + ".....", + ".....", + "*...*", + "*...*", + "*****", + "*...*", + "*...*", + ".....", + "*****", + "*...*", + "*****", + "*.*..", + "*..**", + ".....", + "*****", + "..*..", + "..*..", + "..*..", + "*****", + ".....", + "***..", + "*..*.", + "*...*", + "*..*.", + "***..", + ".....", + "*****", + "*...*", + "*...*", + "*...*", + "*****", + ".....", + "*...*", + ".*.*.", + "..*..", + "..*..", + "..*.."}; + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + if (n < 0) + { + n = abs(n); + for (int i = 0; i < 61; i++) + { + for (int k = 0; k < n; k++) + { + for (int j = 0, sz = vertical[i].size(); j < sz; j++) + { + for (int l = 0; l < n; l++) + { + cout << vertical[i][j]; + } + } + cout << endl; + } + } + } + else + { + n = abs(n); + for (int i = 0; i < 5; i++) + { + for (int k = 0; k < n; k++) + { + for (int j = 0, sz = horizontal[i].size(); j < sz; j++) + { + for (int l = 0; l < n; l++) + { + cout << horizontal[i][j]; + } + } + cout << endl; + } + } + } + cout << endl + << endl; + } + return 0; +} diff --git a/10895.cpp b/10895.cpp new file mode 100644 index 0000000..e84c23f --- /dev/null +++ b/10895.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int main() +{ + int m, n, r, value; + int cols[10010]; + vector>> matrix; + while (cin >> m >> n) + { + matrix.clear(); + for (int i = 0; i < n; i++) + { + vector> tmp; + matrix.push_back(tmp); + } + for (int i = 1; i <= m; i++) + { + cin >> r; + for (int j = 0; j < r; j++) + { + cin >> cols[j]; + } + for (int j = 0; j < r; j++) + { + cin >> value; + matrix[cols[j] - 1].push_back(make_pair(i, value)); + } + } + cout << n << " " << m << endl; + for (int i = 0; i < n; i++) + { + int sz = matrix[i].size(); + cout << sz; + for (int j = 0; j < sz; j++) + { + cout << " " << matrix[i][j].first; + } + cout << endl; + for (int j = 0; j < sz; j++) + { + if (j > 0) + { + cout << " "; + } + cout << matrix[i][j].second; + } + cout << endl; + } + } + return 0; +} diff --git a/10896.cpp b/10896.cpp new file mode 100644 index 0000000..952e4ef --- /dev/null +++ b/10896.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int diff; + +bool encoding_fits(const string &a, const string &b) +{ + char c; + string tmp(b); + diff = 0; + c = b[0]; + while (c != a[0]) + { + diff++; + c++; + if (c > 'z') + { + c = 'a'; + } + } + for (int i = 0, sz = b.size(); i < sz; i++) + { + if ((int)b[i] + diff > 122) + { + tmp[i] = 'a' + b[i] + diff - 'z' - 1; + } + else + { + tmp[i] = b[i] + diff; + } + } + return tmp == a; +} + +int main() +{ + int n; + string input; + string word; + vector result; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, input); + cin >> word; + cin.ignore(100, '\n'); + result.clear(); + stringstream ss; + ss << input; + while (ss >> input) + { + if (input.size() == word.size()) + { + if (encoding_fits(input, word)) + { + result.push_back('a' + diff); + } + } + } + sort(result.begin(), result.end()); + for (int i = 0, sz = result.size(); i < sz; i++) + { + cout << result[i]; + } + cout << endl; + } + return 0; +} diff --git a/10897.cpp b/10897.cpp new file mode 100644 index 0000000..31f9899 --- /dev/null +++ b/10897.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +const double pi = acos(-1); +const double earthRadius = 6371.01; + +int numOfCase; + +class Place +{ +public: + double latitude, longitude; +} from, to; + +void input() +{ + double a, b, c, d, e, f; + char x, y; + cin >> a >> b >> c >> x >> d >> e >> f >> y; + double temp; + temp = a * 3600 + b * 60 + c; + temp /= 3600; + if (x == 'S') + { + temp = -temp; + } + from.latitude = temp; + temp = d * 3600 + e * 60 + f; + temp /= 3600; + if (y == 'W') + { + temp = -temp; + } + from.longitude = temp; + cin >> a >> b >> c >> x >> d >> e >> f >> y; + temp = a * 3600 + b * 60 + c; + temp /= 3600; + if (x == 'S') + { + temp = -temp; + } + to.latitude = temp; + temp = d * 3600 + e * 60 + f; + temp /= 3600; + if (y == 'W') + { + temp = -temp; + } + to.longitude = temp; +} + +void solve() +{ + double p_lat, q_lat, cha, res; + /* + p_lat=test[a].lat/180*pi; + q_lat=test[b].lat/180*pi; + cha=(test[a].lon-test[b].lon)/180*pi; + res=sin(p_lat)*sin(q_lat)+cos(p_lat)*cos(q_lat)*cos(cha); + res=acos(res)*R; + printf("%.0f km\n",res); + */ + p_lat = from.latitude / 180 * pi; + q_lat = to.latitude / 180 * pi; + cha = (from.longitude - to.longitude) / 180 * pi; + res = sin(p_lat) * sin(q_lat) + cos(p_lat) * cos(q_lat) * cos(cha); + res = acos(res) * earthRadius; + printf("%.2lf\n", res); +} + +int main() +{ + cin >> numOfCase; + while (numOfCase--) + { + input(); + solve(); + // cout< + +using namespace std; + +#define maxn 1000003 + +int Touples, TotalComb; + +struct xx +{ + int val; +} Table[maxn]; +struct ss +{ + int quantity[12]; + int cost; + int val; +} Comb[20]; + +int Val(int a[]) +{ + int i, sum = 0, d = 1; + for (i = Touples; i >= 1; i--) + { + sum += a[i] * d; + d *= 10; + } + return sum; +} + +int Recur(int a[], int ind) +{ + int min = 3000000, i, j, k, val; + int b[11], f = 0; + val = Val(a); + if (val == 0) + return 0; + if (ind == 0) + return 9999999; + for (i = 0; i < 10; i++) + { + for (j = 1; j <= Touples; j++) + { + b[j] = a[j] - Comb[ind].quantity[j] * i; + if (b[j] < 0) + { + f = 1; + break; + } + } + if (f) + break; + k = Recur(b, ind - 1) + Comb[ind].cost * i; + if (k < min) + min = k; + } + return min; +} + +void Free() +{ + for (int i = 1; i <= TotalComb + Touples; i++) + for (int j = 0; j < 10; j++) + Comb[i].quantity[j] = 0; +} + +void Cal() +{ + int order, i, a[10]; + cin >> order; + while (order--) + { + for (i = 1; i <= Touples; i++) + cin >> a[i]; + cout << Recur(a, Touples + TotalComb) << endl; + } +} + +int main() +{ + int i, j; + while (cin >> Touples) + { + for (i = 1; i <= Touples; i++) + { + Comb[i].quantity[i] = 1; + cin >> Comb[i].cost; + Comb[i].val = Val(Comb[i].quantity); + } + cin >> TotalComb; + for (i = 1; i <= TotalComb; i++) + { + for (j = 1; j <= Touples; j++) + cin >> Comb[i + Touples].quantity[j]; + Comb[i + Touples].val = Val(Comb[i + Touples].quantity); + cin >> Comb[i + Touples].cost; + } + Cal(); + Free(); + } + return 0; +} diff --git a/10899.cpp b/10899.cpp new file mode 100644 index 0000000..82cab21 --- /dev/null +++ b/10899.cpp @@ -0,0 +1,218 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +typedef struct +{ + double x, y, dx, dy; +} Pt; +Pt S, Q; + +double height, width, vel0, stop, decel; + +double len(Pt A, Pt B) +{ + return hypot(A.x - B.x, A.y - B.y); +} + +double fmin(double a, double b) +{ + return (a < b) ? a : b; +} + +// shortest dist from segment AB to Q +double qdist(Pt A, Pt B) +{ + double a, b, q, t, r; + double cos_a, cos_b; + a = len(B, Q); + b = len(A, Q); + q = len(A, B); + r = fmin(a, b); + cos_a = (b * b + q * q - a * a) / (2. * b * q); + cos_b = (a * a + q * q - b * b) / (2. * a * q); + if (cos_a > -EPS && cos_b > -EPS) + { + t = b * sqrt(1. - cos_a * cos_a); + r = fmin(r, t); + } + return r; +} + +double posiroot(double a, double b, double c) +{ + double D, x1, x2; + D = b * b - 4 * a * c; + if (D < -EPS) + { + // fprintf(stderr, "posiroot: D=%.5lf\n", D); + abort(); + } + D = sqrt(D); + if (D < EPS) + { + x1 = x2 = -b / (2. * a); + if (x1 < -EPS) + { + // fprintf(stderr, "posiroot: D=0, x=%.5lf\n", x1); + abort(); + } + if (x1 < EPS) + { + x1 = 0.; + } + return x1; + } + else + { + x1 = (-b + D) / (2. * a); + x2 = (-b - D) / (2. * a); + if (fabs(x1) < EPS) + { + x1 = 0.; + } + if (fabs(x2) < EPS) + { + x2 = 0.; + } + if (x1 > -EPS && x2 > -EPS) + { + // fprintf(stderr, "posiroot: multi +ve roots %.5lf, %.5lf; choosing smaller\n", x1, x2); + } + if (x1 < 0 && x2 < EPS) + { + // fprintf(stderr, "posiroot: no +ve roots\n"); + abort(); + } + return (x1 < x2) ? x1 : x2; + } +} + +double sim() +{ + Pt A, B; + double tm, vel, shortest; + double d, u, t, z; + shortest = len(S, Q); + A = S; + tm = 0; + vel = vel0; + decel = vel0 / stop; + while (vel > EPS) + { + // fprintf(stderr, "*** sim step: A=(%.5lf,%.5lf) v=(%.5lf,%.5lf) t=%.5lf |v|=%.5lf\n", + // A.x, A.y, A.dx, A.dy, tm, vel); + d = 1. / 0.; + if (fabs(A.dx) > EPS) + { + u = -A.x / A.dx; + if (u > EPS) + { + d = fmin(d, u); + } + u = (width - A.x) / A.dx; + if (u > EPS) + { + d = fmin(d, u); + } + } + if (fabs(A.dy) > EPS) + { + u = -A.y / A.dy; + if (u > EPS) + { + d = fmin(d, u); + } + u = (height - A.y) / A.dy; + if (u > EPS) + { + d = fmin(d, u); + } + } + // fprintf(stderr, "dist before nxt collision = %.5lf\n", d); + if (d > 1e50) + { + printf("mistake?\n"); + abort(); + } + // fprintf(stderr, "decel=%.5lf vel=%.5lf\n", decel, vel); + t = vel / decel; + u = vel * t - decel * t * t / 2.; + // fprintf(stderr, "can still go: %.5lf units\n", u); + if (u < EPS) + { + break; + } + if (d > u) + { + d = u; + } + // fprintf(stderr, "to go: %.5lf\n", d); + B.x = A.x + d * A.dx; + B.y = A.y + d * A.dy; + if ((B.x + 1 * A.dx) < -EPS || (B.x + 1 * A.dx) > (width + EPS)) + { + B.dx = -A.dx; + } + else + { + B.dx = A.dx; + } + if ((B.y + 1 * A.dy) < -EPS || (B.y + 1 * A.dy) > (height + EPS)) + { + B.dy = -A.dy; + } + else + { + B.dy = A.dy; + } + // fprintf(stderr, "new pos (w/o time): (%.5lf,%.5lf), new dir: (%.5lf,%.5lf)\n", B.x, B.y, B.dx, B.dy); + // fprintf(stderr, "dist to Q = %.5lf\n", qdist(A, B)); + shortest = fmin(shortest, qdist(A, B)); + A = B; + if (fabs(d - u) < EPS) + { + break; + } + z = posiroot(-decel / 2., vel, -d); + // fprintf(stderr, "time consumed = %.5lf\n", z); + tm += z; + vel = vel0 - decel * tm; + } + return shortest; +} + +int main() +{ + int i, j, z[8]; + double ang0, pi; + pi = 2. * acos(0.); + for (;;) + { + for (i = j = 0; i < 7 && scanf("%d", &z[i]) == 1; i++) + { + j |= z[i]; + } + if (i < 7 || j == 0) + { + break; + } + width = z[0]; + height = z[1]; + vel0 = z[2]; + ang0 = z[3] * pi / 180.; + stop = z[4]; + Q.x = z[5]; + Q.y = z[6]; + S.x = width / 2.; + S.y = height / 2.; + S.dx = cos(ang0); + S.dy = sin(ang0); + // fprintf(stderr, "width=%.0lf height=%.0lf S=(%.2lf,%.2lf) dS=(%.5lf,%.5lf) Q=(%.2lf,%.2lf)\n", + // width, height, S.x, S.y, S.dx, S.dy, Q.x, Q.y); + printf("%.2lf\n", sim()); + } + return 0; +} diff --git a/109.cpp b/109.cpp new file mode 100644 index 0000000..ee36d3a --- /dev/null +++ b/109.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +struct point +{ + int x, y; +}; + +inline int dist(point a, point b) +{ + return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); +} + +// (p1,p2) x (p1,p3) +inline int cross(point p1, point p2, point p3) +{ + return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); +} + +inline bool min_y(point a, point b) +{ + if (a.y == b.y) + { + return a.x < b.x; + } + return a.y < b.y; +} + +static point base; +inline bool ccw(point a, point b) +{ + int c = cross(base, a, b); + if (c == 0) + { + return dist(base, a) < dist(base, b); + } + return c > 0; +} + +class kingdom +{ +public: + vector house; + kingdom(); + bool is_destroyed; + void convex_hull(); + void destroy(point); + bool is_inside(point); + double area(); +}; + +kingdom::kingdom() +{ + is_destroyed = false; +} + +// Graham Scan +inline void kingdom::convex_hull() +{ + swap(house[0], *min_element(house.begin(), house.end(), min_y)); + base = house[0]; + sort(house.begin(), house.end(), ccw); + house.push_back(house[0]); + int CH = 1; + for (int i = 2; i < house.size(); ++i) + { + while (cross(house[CH - 1], house[CH], house[i]) <= 0) + { + if (CH == 1) + { + house[CH] = house[i]; + i++; + } + else + { + CH--; + } + } + CH++; + house[CH] = house[i]; + } + house.resize(CH + 1); +} + +inline void kingdom::destroy(point missile) +{ + if (is_destroyed) + { + return; + } + if (is_inside(missile)) + { + is_destroyed = true; + } +} + +inline double kingdom::area() +{ + if (!is_destroyed) + { + return 0; + } + double area = 0; + for (int i = 1; i < house.size(); ++i) + { + area += (house[i - 1].x * house[i].y) - (house[i].x * house[i - 1].y); + } + return area / 2; +} + +inline bool kingdom::is_inside(point missile) +{ + for (int i = 1; i < house.size(); ++i) + { + if (cross(house[i - 1], house[i], missile) < 0) + { + return false; + } + } + return true; +} + +inline kingdom build_kingdom(int house_count) +{ + kingdom k; + int x, y; + for (int i = 0; i < house_count; ++i) + { + point p; + scanf("%d%d", &p.x, &p.y); + k.house.push_back(p); + } + return k; +} + +int main() +{ + // 1. build world + vector king; + int house_count; + while (scanf("%d", &house_count) == 1) + { + if (house_count == -1) + { + break; + } + king.push_back(build_kingdom(house_count)); + } + + // 2. convex hull + for (int i = 0; i < king.size(); ++i) + { + king[i].convex_hull(); + } + + // 3. missiles atk + double total_area = 0; + point missile; + while (scanf("%d%d", &missile.x, &missile.y) == 2) + { + for (int i = 0; i < king.size(); ++i) + { + king[i].destroy(missile); + } + } + + // 4. calc sum of destroyed area + int sz = king.size(); + for (int i = 0; i < sz; ++i) + { + total_area += king[i].area(); + } + + printf("%.2lf\n", total_area); + return 0; +} diff --git a/10900.cpp b/10900.cpp new file mode 100644 index 0000000..9554d77 --- /dev/null +++ b/10900.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int i, n; + double p, pr, nw, th; + while (scanf("%d%lf", &n, &p) == 2) + { + if (n == 0 && p == 0.0) + { + break; + } + if (p == 1.0) + { + printf("%.3lf\n", pow(2.0, n)); + continue; + } + pr = pow(2.0, n); + for (i = n; i > 0; i--) + { + nw = pow(2.0, i - 1.0); + th = nw / pr; + if (th < p) + { + th = p; + } + pr = (nw * (th - p) + pr / 2.0 * (1 - th * th)) / (1.0 - p); + } + printf("%.3lf\n", pr); + } + return 0; +} diff --git a/10901.cpp b/10901.cpp index e2785a1..9733186 100644 --- a/10901.cpp +++ b/10901.cpp @@ -1,102 +1,215 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define maxn 10002 + +struct ss +{ + int no; + int time; +}; +vector Left, Right; +int lind, rind, ftime, fside, T, N, M, Cross[maxn]; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +void Move() +{ + int i, k; -struct Type{ - int tt; - string ss; - int ii; - Type(int _t, int _i, string _s){ - tt = _t; ii = _i; ss = _s; + if (lind == Left.size()) + { + if (ftime < Right[rind].time) + ftime += (Right[rind].time - ftime); + if (fside == 0) + ftime += T; + else + fside = 1; + if (ftime < Right[rind].time) + ftime += (Right[rind].time - ftime); + k = 0; + for (i = rind; i < Right.size(); i++) + { + if (k == N || Right[i].time > ftime) + break; + Cross[Right[i].no] = ftime + T; + k++; + } + rind = i; + ftime += T; + return; } -}; -bool comp(Type A, Type B){ - if( A.tt != B.tt ) - return A.tt < B.tt; - return A.ii < B.ii; -} + if (rind == Right.size()) + { + if (ftime < Left[lind].time) + ftime += (Left[lind].time - ftime); + if (fside == 1) + ftime += T; + else + { + fside++; + fside %= 2; + } + if (ftime < Left[lind].time) + ftime += (Left[lind].time - ftime); + k = 0; + for (i = lind; i < Left.size(); i++) + { + if (k == N || Left[i].time > ftime) + break; + Cross[Left[i].no] = ftime + T; + k++; + } + lind = i; + ftime += T; + return; + } -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while( T-- ){ - int N, t, M; - cin >> N >> t >> M; - int i; - queue< Type > q; - vector V; - map Map; - int tot = 0; - string side = "left"; - FOI(i, 0, M-1){ - int I; - string S; - cin >> I >> S; - V.push_back( Type(I, i, S) ); + if (Left[lind].time < Right[rind].time) + { + if (ftime < Left[lind].time) + ftime += (Left[lind].time - ftime); + if (fside == 0) + fside = 1; + else + { + k = 0; + for (i = rind; i < Right.size(); i++) + { + if (Right[i].time > ftime || k == N) + break; + Cross[Right[i].no] = ftime + T; + k++; + } + rind = i; + ftime += T; } - sort(V.begin(), V.end(), comp); - FOI(i, 0, M-1) - q.push(V[i]); - while( !q.empty() ){ - Type p = q.front(); - - tot += max(0, p.tt - tot); - if( p.ss != side ){ - tot += t; - side = p.ss; + if (ftime < Left[lind].time) + ftime += (Left[lind].time - ftime); + k = 0; + for (i = lind; i < Left.size(); i++) + { + if (k == N || Left[i].time > ftime) + break; + Cross[Left[i].no] = ftime + T; + k++; + } + lind = i; + ftime += T; + return; + } + + if (Left[lind].time > Right[rind].time) + { + if (ftime < Right[rind].time) + ftime += (Right[rind].time - ftime); + if (fside == 0) + { + k = 0; + for (i = lind; i < Left.size(); i++) + { + if (Left[i].time > ftime || k == N) + break; + Cross[Left[i].no] = ftime + T; + k++; } - int cnt = 0; - while( !q.empty() && p.tt <= tot && cnt <= N && p.ss == side ){ - q.pop(); - ++cnt; - Map[p.ii] = tot + t; - if( q.empty() ) + lind = i; + ftime += T; + } + else + fside = 0; + if (ftime < Right[rind].time) + ftime += (Right[rind].time - ftime); + k = 0; + for (i = rind; i < Right.size(); i++) + { + if (k == N || Right[i].time > ftime) + break; + Cross[Right[i].no] = ftime + T; + k++; + } + rind = i; + ftime += T; + return; + } + + if (Left[lind].time == Right[rind].time) + { + if (fside == 0) + { + fside = 1; + if (ftime < Left[lind].time) + { + ftime += Left[lind].time - ftime; + k = 0; + for (i = lind; i < Left.size(); i++) + { + if (k == N || Left[i].time > ftime) + break; + Cross[Left[i].no] = ftime + T; + k++; + } + lind = i; + ftime += T; + return; + } + } + else + { + fside = 0; + if (ftime < Right[rind].time) + ftime += (Right[rind].time - ftime); + k = 0; + for (i = rind; i < Right.size(); i++) + { + if (k == N || Right[i].time > ftime) break; - p = q.front(); + Cross[Right[i].no] = ftime + T; + k++; } - tot += t; - if( side == "left" ) - side = "right"; + rind = i; + ftime += T; + return; + } + } +} + +void Cal() +{ + int i, k; + lind = rind = 0; + ftime = 0; + fside = 0; + while (lind + rind < M) + Move(); + for (i = 0; i < M; i++) + cout << Cross[i] << endl; +} + +int main() +{ + int c, i, t, k = 0; + ss d; + string side; + cin >> c; + while (c--) + { + cin >> N >> T >> M; + for (i = 0; i < M; i++) + { + cin >> t >> side; + d.no = i; + d.time = t; + if (side == "left") + Left.push_back(d); else - side = "left"; + Right.push_back(d); } - FOI(i, 0, M-1) - cout << Map[i] << endl; - if( T > 0 ) + if (k++) cout << endl; + Cal(); + Left.clear(); + Right.clear(); } return 0; } diff --git a/10902.cpp b/10902.cpp new file mode 100644 index 0000000..54e4bc8 --- /dev/null +++ b/10902.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define D(x) cout << #x " is " << x << endl + +/* + Returns true if point (x, y) lies inside (or in the border) + of box defined by points (x0, y0) and (x1, y1). +*/ +bool point_in_box(double x, double y, + double x0, double y0, double x1, double y1) +{ + return min(x0, x1) <= x && x <= max(x0, x1) && min(y0, y1) <= y && y <= max(y0, y1); +} + +/* + Finds the intersection between two segments (Not infinite lines!) + Segment 1 goes from point (x0, y0) to (x1, y1). + Segment 2 goes from point (x2, y2) to (x3, y3). + + Handles the case when the 2 segments are: + *Parallel but don't lie on the same line (No intersection) + *Parallel and both lie on the same line (Infinite intersections or no intersections) + *Not parallel (One intersection or no intersections) + + Returns true if the segments do intersect in any case. +*/ +bool segment_segment_intersection(double x0, double y0, double x1, double y1, + double x2, double y2, double x3, double y3) +{ +#ifndef EPS +#define EPS 1e-9 +#endif + double t0 = (y3 - y2) * (x0 - x2) - (x3 - x2) * (y0 - y2); + double t1 = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0); + double det = (y1 - y0) * (x3 - x2) - (y3 - y2) * (x1 - x0); + if (fabs(det) < EPS) + { + //parallel + if (fabs(t0) < EPS || fabs(t1) < EPS) + { + //they lie on same line, but they may or may not intersect. + return (point_in_box(x0, y0, x2, y2, x3, y3) || + point_in_box(x1, y1, x2, y2, x3, y3) || + point_in_box(x2, y2, x0, y0, x1, y1) || + point_in_box(x3, y3, x0, y0, x1, y1)); + } + else + { + //just parallel, no intersection + return false; + } + } + else + { + t0 /= det; + t1 /= det; + /* + 0 <= t0 <= 1 if the intersection point lies in segment 1. + 0 <= t1 <= 1 if the intersection point lies in segment 2. + */ + if (0.0 <= t0 && t0 <= 1.0 && 0.0 <= t1 && t1 <= 1.0) + { + double x = x0 + t0 * (x1 - x0); + double y = y0 + t0 * (y1 - y0); + //intersection is point (x, y) + return true; + } + //the intersection points doesn't lie on both segments. + return false; + } +} + +typedef pair point; +typedef pair segment; +int main() +{ + int n; + while (scanf("%d", &n) && n) + { + vector sticks; + list top; + for (int i = 0; i < n; ++i) + { + double x0, y0, x1, y1; + scanf("%lf %lf %lf %lf", &x0, &y0, &x1, &y1); + sticks.push_back(segment(point(x0, y0), point(x1, y1))); + for (list::iterator j = top.begin(); j != top.end(); ++j) + { + const segment &s = sticks[*j]; + bool b = segment_segment_intersection(s.first.first, s.first.second, + s.second.first, s.second.second, + x0, y0, x1, y1); + /* + printf("Intersection between <(%.2lf, %.2lf) - (%.2lf, %.2lf)> and " + "<(%.2lf, %.2lf) - (%.2lf, %.2lf)> = %s\n", x0, y0, x1, y1, + s.first.first, s.first.second, s.second.first, s.second.second, + (b ? "true" : "false")); + */ + if (b) + { + j = top.erase(j), --j; + } + } + top.push_back(i); + } + printf("Top sticks:"); + int k = 0; + for (list::iterator i = top.begin(); k < top.size(); ++i, ++k) + { + printf(" %d%c", *i + 1, (k < top.size() - 1 ? ',' : '.')); + } + printf("\n"); + } + return 0; +} diff --git a/10903.cpp b/10903.cpp index 680649d..292113c 100644 --- a/10903.cpp +++ b/10903.cpp @@ -1,75 +1,96 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -struct Play { - double T; - double W; +struct game +{ + int win; + int loss; + int gm; }; +game P[102]; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - bool flag = false; - while (true) { - int N, K; - int i, j; - cin >> N; - if (N == 0) break; - cin >> K; - K = (K * N * (N - 1) )/ 2; - Play P[N + 1]; - FOI(i, 0, N) { - P[i].T = 0.0; - P[i].W = 0.0; +void Cal(int n, int k) +{ + string move1, move2; + int p1, p2, y; + y = k * (n * (n - 1)) / 2; + while (y--) + { + cin >> p1 >> move1 >> p2 >> move2; + if (move1 == "rock") + { + if (move2 == "paper") + { + P[p1].loss++; + P[p1].gm++; + P[p2].win++; + P[p2].gm++; + } + else if (move2 == "scissors") + { + P[p1].win++; + P[p1].gm++; + P[p2].loss++; + P[p2].gm++; + } } - while (K--) { - int p1, p2; - string m1, m2; - cin >> p1 >> m1 >> p2 >> m2; - if (m1 != m2) { - P[p1].T += 1.0; - P[p2].T += 1.0; - if ( (m1 == "paper" && m2 == "rock") || (m1 == "scissors" && m2 == "paper") || (m1 == "rock" && m2 == "scissors")) - P[p1].W += 1.0; - else - P[p2].W += 1.0; + else if (move1 == "paper") + { + if (move2 == "rock") + { + P[p1].win++; + P[p1].gm++; + P[p2].loss++; + P[p2].gm++; + } + else if (move2 == "scissors") + { + P[p1].loss++; + P[p1].gm++; + P[p2].win++; + P[p2].gm++; } } - if (flag) cout << endl; - FOI(i, 1, N) { - if (P[i].T > 0) - printf("%.3lf\n", P[i].W / P[i].T); - else - printf("-\n"); + else + { + if (move2 == "paper") + { + P[p1].win++; + P[p1].gm++; + P[p2].loss++; + P[p2].gm++; + } + else if (move2 == "rock") + { + P[p1].loss++; + P[p1].gm++; + P[p2].win++; + P[p2].gm++; + } } - flag = true; + } + double x; + for (int i = 1; i <= n; i++) + { + x = (double)P[i].win / (double)P[i].gm; + if (P[i].gm == 0) + printf("-\n"); + else + printf("%.3lf\n", x); + P[i].gm = P[i].loss = P[i].win = 0; + } +} + +int main() +{ + int n, k, g = 0; + while (cin >> n && n) + { + cin >> k; + if (g++) + printf("\n"); + Cal(n, k); } return 0; } diff --git a/10904.cpp b/10904.cpp new file mode 100644 index 0000000..c861ee2 --- /dev/null +++ b/10904.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +char same[256][256], def[256][256], done[256]; +char op[100], lhs, rhs; +int i, j, k, m, n, c; +int issame(int a, int b) +{ + int i, r; + if (same[a][b] == 1) + { + return 1; + } + if (same[a][b] == -1) + { + return 0; + } + if (isupper(def[a][0])) + { + return issame(def[a][0], b); + } + if (isupper(def[b][0])) + { + return issame(a, def[b][0]); + } + if (def[a][0] != def[b][0]) + { + return 0; + } + if (def[a][0] != 's') + { + return 1; + } + same[a][b] = same[b][a] = 1; + for (i = 1; (def[a][i] && def[b][i]) && issame(def[a][i], def[b][i]); i++) + ; + r = !def[a][i] && !def[b][i]; + same[a][b] = same[b][a] = r - 1; + return r; +} +int main() +{ + for (c = 0; strcmp(op, "--"); c++) + { + if (c) + { + printf("\n"); + memset(same, 0, sizeof(same)); + memset(def, 0, sizeof(def)); + memset(done, 0, sizeof(done)); + } + def['i'][0] = 'i'; + def['c'][0] = 'c'; + def['r'][0] = 'r'; + for (n = 0; (1 == scanf("%s", op)) && *op != '-'; n++) + { + assert(!strcmp(op, "type")); + scanf(" %c = %c%*[a-z(]", &lhs, &rhs); + def[(int)lhs][0] = rhs; + if (rhs == 's') + { + for (j = 1; scanf(" %c", &def[(int)lhs][j]) && def[(int)lhs][j] != ')'; j++) + ; + def[(int)lhs][j] = 0; + } + } + for (i = 'A'; i <= 'Z'; i++) + { + if (!def[i][0] || done[i]) + { + continue; + } + printf("%c", i); + for (j = i + 1; j <= 'Z'; j++) + { + if (done[j]) + { + continue; + } + if (issame(i, j)) + { + assert(issame(j, i)); + printf(" %c", j); + done[j] = 1; + } + else + { + assert(!issame(j, i)); + } + } + printf("\n"); + } + } + return 0; +} diff --git a/10905.cpp b/10905.cpp index c6f096f..5686605 100644 --- a/10905.cpp +++ b/10905.cpp @@ -1,69 +1,36 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -bool comp(string A, string B) { - if (A == B) return true; - - int aL = A.length(), bL = B.length(); - int Lmin = min(aL, bL); - int Lmax = max(aL, bL); - - string X = A.substr(0, Lmin); - string Y = B.substr(0, Lmin); - if (X != Y) return X > Y; - - if (aL > bL) { - X = A.substr(Lmin); - return comp(X, B); - } - else { - Y = B.substr(Lmin); - return comp(A, Y); +#define pi acos(-1.0) +#define N 1000000 + +bool cmp(string x, string y) +{ + string t1 = x + y; + string t2 = y + x; + if (t1 > t2) + { + return true; } + return false; } - -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int N; - cin >> N; - if (!N) break; - - string Arr[N]; - for (int i = 0; i < N; i++) - cin >> Arr[i]; - - sort(Arr, Arr + N, comp); - - for (int i = 0; i < N; i++) - cout << Arr[i]; - - cout << endl; +int main() +{ + int n; + string a[50 + 5]; + while (scanf("%d", &n) && n) + { + for (int i = 0; i < n; i++) + { + cin >> a[i]; + } + sort(a, a + n, cmp); + for (int i = 0; i < n; i++) + { + cout << a[i]; + } + printf("\n"); } return 0; } diff --git a/10906.cpp b/10906.cpp new file mode 100644 index 0000000..4bffb93 --- /dev/null +++ b/10906.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +typedef long long num; + +char name[64][64]; +num L[64], R[64]; +int op[64], n; + +void prn(int x) +{ + if (x < 0) + { + x = -x; + } + assert(1 <= x && x <= n); + if (L[x] >= 0 && R[x] >= 0) + { + printf("%lld%c%lld", L[x], op[x], R[x]); + } + else if (L[x] >= 0) + { + if (op[x] == '+' && op[-R[x]] == '*') + { + printf("%lld%c", L[x], op[x]), prn(R[x]); + } + else + { + printf("%lld%c(", L[x], op[x]), prn(R[x]), printf(")"); + } + } + else + { + if (op[x] == '*' && op[-L[x]] == '+') + { + printf("("), prn(L[x]), printf(")"); + } + else + { + prn(L[x]); + } + if (R[x] >= 0) + { + printf("%c%lld", op[x], R[x]); + } + else if (op[x] == '+' && op[-R[x]] == '*') + { + printf("%c", op[x]), prn(R[x]); + } + else + { + printf("%c(", op[x]), prn(R[x]), printf(")"); + } + } +} + +num get(char *s) +{ + int i; + num r; + assert(strlen(s) <= 10); + if ('0' <= s[0] && s[0] <= '9') + { + for (r = 0; '0' <= s[0] && s[0] <= '9'; s++) + { + r = r * 10 + (s[0] - '0'); + } + return r; + } + for (i = 1; i <= n; i++) + if (strcmp(name[i], s) == 0) + { + return -i; + } + assert(1 == 0); + return 0; +} + +int main() +{ + static char s[256], sl[64][64], sr[64][64]; + int i, j, t, T; + for (scanf("%d", &T), t = 1; t <= T && scanf("%d", &n) == 1; t++) + { + assert(n <= 60); + for (i = 1; i <= n; i++) + { + j = scanf(" %[A-Za-z] = %[A-Za-z0-9] %[+*] %[A-Za-z0-9]", + name[i], sl[i], s, sr[i]); + assert(j == 4 && (s[0] == '+' || s[0] == '*')); + op[i] = s[0]; + } + for (i = 1; i <= n; i++) + { + L[i] = get(sl[i]), R[i] = get(sr[i]); + } + printf("Expression #%d: ", t); + prn(n); + printf("\n"); + } + return 0; +} diff --git a/10907.cpp b/10907.cpp new file mode 100644 index 0000000..7e8844c --- /dev/null +++ b/10907.cpp @@ -0,0 +1,230 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +typedef long long num; +typedef struct +{ + num x, y; +} pt_t; +typedef struct +{ + double x, y; +} fpt_t; +typedef struct +{ + num a, b, c; +} line_t; + +pt_t P[64], L; +int e, n; +double total; + +int isect(line_t l, line_t m, fpt_t *q) +{ + /*(y2-y1)x + (x1-x2)y = x1(y2-y1) + y1(x1-x2)*/ + num d = l.a * m.b - l.b * m.a; + if (d == 0) + { + return 0; + } + q->x = (double)(l.c * m.b - l.b * m.c) / (double)d; + q->y = (double)(l.a * m.c - l.c * m.a) / (double)d; + return 1; +} + +line_t line(pt_t A, pt_t B) +{ + line_t l; + l.a = B.y - A.y; + l.b = A.x - B.x; + l.c = A.x * l.a + A.y * l.b; + return l; +} + +int coll(pt_t A, pt_t B, pt_t C) +{ + line_t l; + l = line(A, B); + return ((l.a * C.x + l.b * C.y) == l.c); +} + +fpt_t fpt(pt_t a) +{ + fpt_t b; + b.x = (double)a.x; + b.y = (double)a.y; + return b; +} + +double dist(fpt_t A, fpt_t B) +{ + return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); +} + +double solve() +{ + static fpt_t E, F, R[128]; + double a, t; + int i, m; + if (L.x == P[e].x && L.y == P[e].y) + { + return total; + } + E = fpt(P[e]); + assert(dist(E, fpt(L)) > 0.5); + for (i = 0; i < n; i++) + { + if (i == e || (P[i].x == L.x && P[i].y == L.y)) + { + continue; + } + if (!coll(L, P[e], P[i])) + { + continue; + } + F = fpt(P[i]); + if (dist(E, F) > dist(fpt(L), F)) + { + continue; + } + break; + } + if (i < n) + { + /*printf("exact, i=%d\n",i);*/ + R[0] = E; + for (m = 1;;) + { + if (((n + i - e) % n) == 0) + { + break; + } + R[m++] = fpt(P[i]); + i = (i + 1) % n; + } + } + else + { + for (i = 0; i < n; i++) + { + if (!isect(line(L, P[e]), line(P[i], P[i + 1]), &F)) + { + continue; + } + if (dist(E, F) > dist(fpt(L), F)) + { + continue; + } + if (fabs(dist(E, F) + dist(fpt(L), F) - dist(E, fpt(L))) < EPS) + { + continue; + } + if (F.x < (P[i].x - EPS) && F.x < (P[i + 1].x - EPS)) + { + continue; + } + if (F.x > (P[i].x + EPS) && F.x > (P[i + 1].x + EPS)) + { + continue; + } + if (F.y < (P[i].y - EPS) && F.y < (P[i + 1].y - EPS)) + { + continue; + } + if (F.y > (P[i].y + EPS) && F.y > (P[i + 1].y + EPS)) + { + continue; + } + break; + } + if (i >= n) + { + return total; + } + /*printf("F=(%.3f,%.3f) line seg %d-%d\n",F.x,F.y,i,i+1);*/ + R[0] = E; + R[1] = F; + for (m = 2;;) + { + i = (i + 1) % n; + if (((n + i - e) % n) == 0) + { + break; + } + R[m++] = fpt(P[i]); + } + } + for (i = 0; i < m; i++) + { + R[i + m] = R[i]; + } + for (a = 0., i = 0; i < m; i++) + { + a += R[i].x * R[i + 1].y - R[i + 1].x * R[i].y; + } + a /= 2.; + /*printf("Reg A: ");for(i=0;i= -EPS); + if ((total - a) < EPS) + { + return a; + } + if (a < EPS || m < 2) + { + return (total - a); + } + F = fpt(L); + for (i = 0; i < m; i++) + { + t = (R[i].x - F.x) * (R[i + 1].y - F.y); + t -= (R[i + 1].x - F.x) * (R[i].y - F.y); + if (t < -EPS) + { + return (total - a); + } + } + return a; +} + +int main() +{ + int i, j, g, q; + num t; + for (g = 1; scanf("%d", &n) == 1 && n > 0; g++) + { + printf("Gallery #%d\n", g); + assert(4 <= n && n <= 20); + for (i = 0; i < n; i++) + { + scanf("%lld %lld", &P[i].x, &P[i].y); + P[n + i] = P[i]; + } + for (i = 0, t = 0; i < n; i++) + { + t += P[i].x * P[i + 1].y - P[i + 1].x * P[i].y; + } + assert(t > 0); + total = (double)t / 2.; + for (i = 1, j = 0, e = -1; i <= n; i++) + { + t = (P[i].x - P[i - 1].x) * (P[i + 1].y - P[i - 1].y); + t -= (P[i + 1].x - P[i - 1].x) * (P[i].y - P[i - 1].y); + if (t < 0) + { + j++, e = i; + } + } + assert(j == 1); + e %= n; + for (scanf("%d", &q); q-- > 0 && scanf("%lld %lld", &L.x, &L.y) == 2;) + { + printf("%.2f\n", solve()); + } + } + return 0; +} diff --git a/10908.cpp b/10908.cpp new file mode 100644 index 0000000..ebf5b01 --- /dev/null +++ b/10908.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define maxn 105 + +int R, Col, Q, C[maxn][maxn], D[maxn][maxn]; +char input[maxn], B[maxn][maxn]; + +void ReadCase() +{ + int i, j, v; + for (i = 1; i <= R; i++) + { + gets(B[i]); + for (j = 0; B[i][j]; j++) + { + v = B[i][j]; + C[i][j + 1] = C[i - 1][j + 1] + v; + } + } +} + +int isSquare(int r1, int c1, int r2, int c2, int d) +{ + int i, j; + if (r1 < 1 || c1 > Col || r2 > R || c2 < 1) + return 0; + for (i = c2; i <= c1; i++) + { + if (C[r2][i] - C[r1 - 1][i] != d) + return 0; + } + return 1; +} + +int Count(int r, int c) +{ + int ct = 1, r1, c1, r2, c2, d; + r1 = r - 1; + c1 = c + 1; + r2 = r + 1; + c2 = c - 1; + d = B[r][c - 1] * 3; + while (isSquare(r1, c1, r2, c2, d)) + { + ct += 2; + r1--; + c1++; + r2++; + c2--; + d += B[r][c - 1] * 2; + } + return ct; +} + +void Cal() +{ + int r, c; + printf("%d %d %d\n", R, Col, Q); + while (Q--) + { + gets(input); + sscanf(input, "%d%d", &r, &c); + printf("%d\n", Count(r + 1, c + 1)); + } +} + +int main() +{ + int kase, i; + gets(input); + kase = atoi(input); + while (kase--) + { + gets(input); + sscanf(input, "%d%d%d", &R, &Col, &Q); + ReadCase(); + Cal(); + } + return 0; +} diff --git a/10909.cpp b/10909.cpp new file mode 100644 index 0000000..e76765b --- /dev/null +++ b/10909.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +#define MAXN 670000 + +int left_[MAXN], right_[MAXN], parent[MAXN], key[MAXN], cnt[MAXN], N, root; +char lucky[2010000]; + +// idx of k-th smallest elem +int find(int k) +{ + for (int x = root;;) + { + if (k < cnt[left_[x]]) + { + x = left_[x]; + } + else if (k == cnt[left_[x]]) + { + return x; + } + else + { + k -= cnt[left_[x]] + 1, x = right_[x]; + } + } + return -1; +} + +// rm elem(idx=x) +void rm(int x) +{ + int y; + if (left_[x] != 0 && right_[x] != 0) + { + if (cnt[right_[x]] >= cnt[left_[x]]) + { + for (y = right_[x]; left_[y] != 0; y = left_[y]) + ; + } + else + { + for (y = left_[x]; right_[y] != 0; y = right_[y]) + ; + } + key[x] = key[y]; + x = y; + } + if (left_[x] == 0 && right_[x] == 0) + { + if (left_[parent[x]] == x) + { + left_[parent[x]] = 0; + } + else + { + right_[parent[x]] = 0; + } + } + else + { + y = (left_[x] == 0) ? right_[x] : left_[x]; + if (parent[x] == 0) + { + parent[root = y] = 0; + return; + } + if (left_[parent[x]] == x) + { + left_[parent[x]] = y; + } + else + { + right_[parent[x]] = y; + } + parent[y] = parent[x]; + } + + for (x = parent[x]; x != 0; x = parent[x]) + { + cnt[x]--; + } +} + +// construct balanced tree with b-a+1 elements; returns idx@root +// tree's nodes will get consecutive idxs in tree order +int build(int a, int b) +{ + if (a > b) + { + return 0; + } + if (a == b) + { + N++; + left_[N] = right_[N] = 0; + cnt[N] = 1; + return N; + } + int c = (a + b) / 2, t = build(a, c - 1); + left_[++N] = t; + t = N; + right_[t] = build(c + 1, b); + cnt[t] = cnt[left_[t]] + cnt[right_[t]] + 1; + parent[left_[t]] = parent[right_[t]] = t; + return t; +} + +void mark(int x) +{ + for (; x; x = right_[x]) + { + lucky[key[x]] = 1, mark(left_[x]); + } +} + +// construct lst of lucky # +void make() +{ + int i, j, k; + // init + N = cnt[0] = 0; + parent[root = build(0, 666667)] = 0; + + // start with tree, containing all # + // of form 6k+1 and 6k+3, in range of interest + // these are remaining # after 1st 2 elimination rds + for (i = 1, j = 1; i <= 666700; j += 6) + { + key[i++] = j, key[i++] = j + 2; + } + + // simulate + for (k = 2; k < cnt[root]; k++) + { + j = key[find(k)] - 1; + if (j >= cnt[root]) + { + break; + } + for (i = j; i < cnt[root]; i += j) + { + rm(find(i)); + } + } + // mark remaining # in lucky[] + memset(lucky, 0, sizeof(lucky)); + mark(root); +} + +int main() +{ + int a, n; + for (make(); scanf("%d", &n) == 1;) + { + a = 0; + if (n >= 1 && (n & 1) == 0) + { + for (a = n / 2; a > 0 && !lucky[a]; a--) + ; + for (; a > 0; a -= 2) + { + if (lucky[a] && lucky[n - a]) + { + break; + } + } + } + if (a <= 0) + { + printf("%d is not the sum of two luckies!\n", n); + } + else + { + printf("%d is the sum of %d and %d.\n", n, a, n - a); + } + } + return 0; +} diff --git a/1091.cpp b/1091.cpp new file mode 100644 index 0000000..c448569 --- /dev/null +++ b/1091.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +bool good_widths(vector readings, double separator) +{ + int lowest = 201, highest = 0; + rep(i, readings.size()) + { + if (readings[i] < separator) + { + readings[i] *= 2; + } + if (readings[i] < lowest) + { + lowest = readings[i]; + } + if (readings[i] > highest) + { + highest = readings[i]; + } + } + return lowest >= 95.0 * highest / 105.0; +} + +int map_to_character(int number) +{ + int result = -1; + int encoded[] = {0b00001, 0b10001, 0b01001, 0b11000, 0b00101, 0b10100, 0b01100, 0b00011, 0b10010, 0b10000, 0b00100, 0b00110}; + rep(i, 12) + { + if (number == encoded[i]) + { + result = i; + break; + } + } + return result; +} + +int decode(vector &readings, int pos, double separator) +{ + int num = 0; + for (int i = pos; i < pos + 5; i++) + { + if (readings[i] < separator) + { + num <<= 1; + } + else + { + num = (num << 1) + 1; + } + } + return map_to_character(num); +} + +bool transform(vector &readings, double separator, vector &transformed) +{ + int start_stop_count = 0; + for (int i = 0, sz = readings.size(); i < sz; i += 6) + { + int character = decode(readings, i, separator); + if (character == -1) + { + return false; + } + if (character == 11) + { + start_stop_count++; + } + transformed.push_back(character); + // separator bar should be narrow + if (i + 5 < readings.size() && readings[i + 5] >= separator) + { + return false; + } + } + return start_stop_count == 2 && transformed[0] == 11 && transformed[transformed.size() - 1] == 11; +} + +bool checkC(vector &transformed, int c) +{ + int expected = 0; + int n = transformed.size() - 4; + for (int i = 1, sz = transformed.size() - 3; i < sz; i++) + { + expected += ((n - i) % 10 + 1) * transformed[i]; + } + return expected % 11 == c; +} + +bool checkK(vector &transformed, int c, int k) +{ + int expected = 0; + int n = transformed.size() - 4; + for (int i = 1, sz = transformed.size() - 3; i < sz; i++) + { + expected += ((n - i + 1) % 9 + 1) * transformed[i]; + } + expected += c; + return expected % 11 == k; +} + +void print_code(vector &transformed) +{ + for (int i = 1, sz = transformed.size() - 3; i < sz; i++) + { + if (transformed[i] == 10) + { + cout << "-"; + } + else + { + cout << transformed[i]; + } + } + cout << endl; +} + +int main() +{ + int m, tmp, lowest, case_number = 0; + ; + while (cin >> m) + { + if (m == 0) + { + break; + } + cout << "Case " << ++case_number << ": "; + lowest = 201; + vector readings; + rep(i, m) + { + cin >> tmp; + readings.push_back(tmp); + if (tmp < lowest) + { + lowest = tmp; + } + } + if ((m + 1) % 6 != 0 || (m + 1) / 6 < 5) + { + cout << "bad code" << endl; + continue; + } + double separator = lowest * 1.5; + if (!good_widths(readings, separator)) + { + cout << "bad code" << endl; + continue; + } + // if second bar is wide then it cannot be + // the start/stop character, we reverse input + if (readings[1] >= separator) + { + reverse(readings.begin(), readings.end()); + } + vector transformed; + if (transform(readings, separator, transformed)) + { + int c = transformed[transformed.size() - 3]; + int k = transformed[transformed.size() - 2]; + if (!checkC(transformed, c)) + { + cout << "bad C" << endl; + } + else if (!checkK(transformed, c, k)) + { + cout << "bad K" << endl; + } + else + { + print_code(transformed); + } + } + else + { + cout << "bad code" << endl; + } + } + return 0; +} diff --git a/10910.cpp b/10910.cpp new file mode 100644 index 0000000..0972c50 --- /dev/null +++ b/10910.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +/* +10910 +Marks Distrubution +*/ + +#define maxn 80 +typedef long ss; +ss S, T, P; +char Fg[maxn][maxn]; +ss D[maxn][maxn]; +ss Recur(ss n, ss level) +{ + ss i, d, l = 0; + if (n == 0) + { + return 1; + } + if (level == S) + { + return 0; + } + d = (S - level) * P; + if (d > n) + { + return 0; + } + if (Fg[n][level]) + { + return D[n][level]; + } + Fg[n][level] = 1; + for (i = P; i; i++) + { + if (d - P > (n - i)) + { + break; + } + l += Recur(n - i, level + 1); + } + D[n][level] = l; + return l; +} +int main() +{ + ss k, i; + cin >> k; + while (k--) + { + cin >> S >> T >> P; + cout << Recur(T, 0) << endl; + for (i = 0; i <= T; i++) + for (ss j = 0; j <= S; j++) + { + Fg[i][j] = 0; + } + } + return 0; +} diff --git a/10911.cpp b/10911.cpp index 41923db..0274ef6 100644 --- a/10911.cpp +++ b/10911.cpp @@ -1,47 +1,90 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -struct Team{ +struct student +{ string name; - int X, Y; + int x; + int y; }; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - int N; - cin >> N; - if( N == 0 ) +int n; +student students[16]; +bool taken[16]; +double result; +double current_result; + +double mydistance(int a, int b) +{ + return sqrt((students[a].x - students[b].x) * (students[a].x - students[b].x) + (students[a].y - students[b].y) * (students[a].y - students[b].y)); +} + +void solve(int depth) +{ + if (depth == n) + { + if (current_result < result) + { + result = current_result; + } + return; + } + int st; + // grab any available student + for (int i = 0, sz = 2 * n; i < sz; i++) + { + if (taken[i] == false) + { + st = i; + taken[i] = true; + break; + } + } + // go over all available pairs for selected student + for (int i = 0, sz = 2 * n; i < sz; i++) + { + if (taken[i] == false) + { + taken[i] = true; + double d = mydistance(st, i); + current_result += d; + if (current_result > result) + { + current_result -= d; + taken[i] = false; + continue; + } + solve(depth + 1); + current_result -= d; + taken[i] = false; + } + } + taken[st] = false; +} + +int main() +{ + int cases = 0; + while (cin >> n) + { + if (n == 0) + { break; - Team team[N]; - FOI(i, 0, N-1) - cin >> team[i].name >> team[i].X >> team[i].Y; - sort(team, team + N, comp); + } + cases++; + current_result = 0; + result = 100000000; + for (int i = 0; i < 16; i++) + { + taken[i] = false; + } + for (int i = 0, sz = 2 * n; i < sz; i++) + { + cin >> students[i].name >> students[i].x >> students[i].y; + } + solve(0); + cout << "Case " << cases << ": " << fixed << setprecision(2) << result << endl; } return 0; } diff --git a/10912.cpp b/10912.cpp new file mode 100644 index 0000000..3c05e76 --- /dev/null +++ b/10912.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +/* +Simple Minded Hashing +10912 +*/ + +#define MAXN 355 +int V[27][MAXN]; +int Sum(int n, int m) +{ + int x, y, z; + if (n == m) + { + return n; + } + x = n + m; + y = m - n + 1; + return (x * y) / 2; +} +void Gen() +{ + int i, j, k, x, y; + int max, min; + V[1][1] = V[1][2] = 1; + V[2][3] = 1; + for (i = 3; i < 27; i++) + { + x = i - 1; + for (j = i - 1; j >= 1; j--) + { + max = Sum(x - j + 1, x); + min = Sum(1, j); + for (k = min; k <= max; k++) + { + V[j + 1][k + i] += V[j][k]; + } + } + V[1][i] = 1; + } +} +int main() +{ + int x, y, k = 1; + Gen(); + while (cin >> x >> y) + { + if (!x && !y) + { + break; + } + if (y > 351) + { + cout << "Case " << k++ << ": " << 0; + } + else + { + cout << "Case " << k++ << ": " << V[x][y]; + } + cout << endl; + } + return 0; +} diff --git a/10913.cpp b/10913.cpp new file mode 100644 index 0000000..cf85dca --- /dev/null +++ b/10913.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +#define maxn 77 + +typedef long long ss; + +struct table +{ + bool Fg; + ss val; +}; +table T[maxn][maxn][6]; +ss N, K, Org[maxn][maxn]; + +void LtoR(ss i) +{ + ss n, j, k; + for (j = 2; j <= N; j++) + { + n = 0; + if (Org[i][j] < 0) + n = 1; + for (k = 0; k + n <= K; k++) + { + if (T[i - 1][j][k].Fg == true) + { + T[i][j][k + n].Fg = true; + T[i][j][k + n].val = T[i - 1][j][k].val + Org[i][j]; + } + if (T[i][j - 1][k].Fg == true) + { + if (T[i][j][k + n].Fg == false) + { + T[i][j][k + n].Fg = true; + T[i][j][k + n].val = T[i][j - 1][k].val + Org[i][j]; + } + else if (T[i][j][k + n].val < (T[i][j - 1][k].val + Org[i][j])) + T[i][j][k + n].val = T[i][j - 1][k].val + Org[i][j]; + } + } + } +} + +void RtoL(ss i) +{ + ss j, k, n = 0, ind1 = 0, ind2 = 1, d, u; + table temp[2][6]; + if (Org[i][N] < 0) + n++; + for (k = 0; k < 6; k++) + { + temp[0][k].Fg = false; + temp[1][k].Fg = false; + } + for (j = 0; j + n <= K; j++) + { + if (T[i - 1][N][j].Fg == true) + { + d = Org[i][N] + T[i - 1][N][j].val; + temp[0][j + n].Fg = true; + temp[0][j + n].val = d; + } + } + for (j = N - 1; j >= 1; j--) + { + n = 0; + if (Org[i][j] < 0) + n++; + for (k = 0; k <= K; k++) + temp[ind2][k].Fg = false; + for (k = 0; k + n <= K; k++) + { + temp[ind2][k + n].Fg = false; + if (T[i - 1][j][k].Fg == true) + { + u = Org[i][j] + T[i - 1][j][k].val; + temp[ind2][k + n].Fg = true; + temp[ind2][k + n].val = u; + } + if (temp[ind1][k].Fg == true) + { + if (temp[ind2][k + n].Fg == false) + { + temp[ind2][k + n].Fg = true; + temp[ind2][k + n].val = temp[ind1][k].val + Org[i][j]; + } + else if (temp[ind1][k].val + Org[i][j] > temp[ind2][k + n].val) + temp[ind2][k + n].val = temp[ind1][k].val + Org[i][j]; + if (T[i][j][k + n].Fg == false) + { + T[i][j][k + n].Fg = true; + T[i][j][k + n].val = temp[ind2][k + n].val; + } + else if (T[i][j][k + n].val < temp[ind2][k + n].val) + T[i][j][k + n].val = temp[ind2][k + n].val; + } + } + ind1++; + ind2++; + ind1 %= 2; + ind2 %= 2; + } +} + +void Gen() +{ + ss i, j, k = 0, n = 0; + for (i = 1; i <= N; i++) + { + if (Org[1][i] < 0) + n++; + if (n > K) + continue; + k += Org[1][i]; + T[1][i][n].Fg = true; + T[1][i][n].val = k; + } + for (i = 2; i <= N; i++) + { + n = 0; + if (Org[i][1] < 0) + n = 1; + for (j = 0; j + n <= K; j++) + { + if (T[i - 1][1][j].Fg == true) + { + T[i][1][j + n].Fg = true; + T[i][1][j + n].val = T[i - 1][1][j].val + Org[i][1]; + } + } + LtoR(i); + if (i < N) + RtoL(i); + } +} + +void Cal() +{ + ss i, d, n = 0; + if (N == 1) + { + if (Org[1][1] < 0) + n++; + if (n > K) + printf("impossible\n"); + else + printf("%lld\n", Org[1][1]); + return; + } + Gen(); + for (i = 0; i <= K; i++) + { + if (T[N][N][i].Fg == true) + { + d = T[N][N][i].val; + break; + } + } + if (i > K) + { + printf("impossible\n"); + return; + } + for (i = 0; i <= K; i++) + { + if (T[N][N][i].Fg == true) + if (T[N][N][i].val > d) + d = T[N][N][i].val; + } + printf("%lld\n", d); +} + +void Free() +{ + ss i, j, k; + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + for (k = 0; k <= K; k++) + T[i][j][k].Fg = false; +} + +int main() +{ + ss i, ks = 1, j; + while (scanf("%lld%lld", &N, &K) == 2) + { + if (!N && !K) + break; + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + scanf("%lld", &Org[i][j]); + printf("Case %lld: ", ks++); + Cal(); + Free(); + } + return 0; +} diff --git a/10914.cpp b/10914.cpp new file mode 100644 index 0000000..9c0f444 --- /dev/null +++ b/10914.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +/* +10914 +Abundance and Perfect Number +*/ +#define maxn 5000000 +#define limit 10000000 +typedef long long ss; +char sv[maxn + 2]; +int P[348515], tp; +struct xx +{ + int pos; + ss abn; +}; +xx A[737059]; +ss ta; +int com(const void *a, const void *b) +{ + xx *x = (xx *)a; + xx *y = (xx *)b; + return x->pos - y->pos; +} +void PrimeTable() +{ + int i, j; + tp = 1; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j <= maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + P[0] = 2; + for (i = 3; i <= maxn; i += 2) + { + if (!sv[i]) + { + P[tp++] = i; + } + } +} +ss Abun(ss two, ss prim, ss pos) +{ + ss fact, d, k; + fact = (prim * prim) - 1; + d = prim - 1; + fact = fact / d; + k = fact * two - 2 * pos; + return k; +} +void Gen() +{ + ss i, d = 1, j, k = 0, p = 1, pro; + for (i = 1; i; i++) + { + p = p * 2 + 1; + d *= 2; + if (d >= limit) + { + break; + } + for (j = 1; j < tp; j++) + { + pro = P[j] * d; + if (pro > limit) + { + break; + } + A[k].pos = (int)pro; + A[k].abn = Abun(p, P[j], pro); + k++; + } + } + ta = k; + qsort(A, (int)ta, sizeof(xx), com); + for (i = 1; i < ta; i++) + { + A[i].abn += A[i - 1].abn; + } +} +ss BS(ss key) +{ + ss lo = 0, up = ta - 1; + ss mid = (lo + up) / 2; + while (lo < up) + { + if (A[mid].pos > key) + { + if (A[mid - 1].pos <= key) + { + return A[mid - 1].abn; + } + up = mid - 1; + } + else if (A[mid].pos < key) + { + if (A[mid + 1].pos == key) + { + return A[mid + 1].abn; + } + if (A[mid + 1].pos > key) + { + return A[mid].abn; + } + lo = mid + 1; + } + else + { + return A[mid].abn; + } + mid = (lo + up) / 2; + } + return A[mid].abn; +} +void Cal(ss n) +{ + if (n <= 5) + { + printf("0\n"); + return; + } + printf("%lld\n", BS(n)); +} +int main() +{ + ss n; + PrimeTable(); + Gen(); + while (scanf("%lld", &n) && n) + { + printf("%lld ", n); + Cal(n); + } + return 0; +} diff --git a/10915.cpp b/10915.cpp new file mode 100644 index 0000000..1e220e9 --- /dev/null +++ b/10915.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +double x[200], y[200], z[200], xx[200], yy[200], zz[200]; +double dist(double x, double y, double z) +{ + return x * x + y * y + z * z; +} + +int main() +{ + int i, j, k, m, ret; + while (1) + { + scanf("%d %d ", &k, &m); + if (!k && !m) + { + break; + } + for (i = 0; i < k; i++) + { + scanf("%lf %lf %lf ", &x[i], &y[i], &z[i]); + } + for (j = 0; j < m; j++) + { + scanf("%lf %lf %lf ", &xx[j], &yy[j], &zz[j]); + } + ret = 0; + for (j = 0; j < m; j++) + { + double d3 = dist(xx[j], yy[j], zz[j]); + for (i = 0; i < k; i++) + { + double d1 = dist(x[i], y[i], z[i]); + double d2 = dist(x[i] - xx[j], y[i] - yy[j], z[i] - zz[j]); + if (d2 + d3 <= d1) + { + ret++; + break; + } + } + } + printf("%d\n", ret); + } +} diff --git a/10916.cpp b/10916.cpp new file mode 100644 index 0000000..fc73c4c --- /dev/null +++ b/10916.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +const int MAXSIZE = 254020; +int year; +double lg[MAXSIZE]; + +void init() +{ + for (int n = 1; n < MAXSIZE; n++) + { + lg[n] = lg[n - 1] + log(1.0 * n) / log(10.0); + } +} + +int find(double v) +{ + int i = 0; + while (lg[i] < v) + { + i++; + } + i--; + return i; +} + +int main() +{ + init(); + while (cin >> year && year) + { + int x = (year - 1960) / 10 + 2; + x = 1 << x; + cout << find(log(2.0) * x / log(10.0)) << endl; + } + return 0; +} diff --git a/10917.cpp b/10917.cpp new file mode 100644 index 0000000..580e845 --- /dev/null +++ b/10917.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +const int MAXNODE = 1010; +const int oo = 0x3fffffff; + +int N, M; +int graph[MAXNODE][MAXNODE]; +int dist[MAXNODE], f[MAXNODE]; + +void init() +{ + int i, j; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + graph[i][j] = oo; + graph[i][i] = 0; + } +} + +void input() +{ + int a, b, d; + for (int i = 0; i < M; i++) + { + cin >> a >> b >> d; + a--; + b--; + graph[a][b] = graph[b][a] = d; + } +} + +void Dijkstra(int start) +{ + bool vis[N]; + for (int i = 0; i < N; i++) + { + dist[i] = graph[start][i]; + vis[i] = false; + } + dist[start] = 0; + for (int i = 0; i < N; i++) + { + int mmin = oo; + int k = 0; + for (int j = 0; j < N; j++) + if (!vis[j] && dist[j] < mmin) + { + mmin = dist[j]; + k = j; + } + vis[k] = true; + for (int j = 0; j < N; j++) + if (!vis[j] && dist[k] + graph[k][j] < dist[j]) + { + dist[j] = dist[k] + graph[k][j]; + } + } +} + +int dfs(int x) +{ + if (x == 0) + { + return 1; + } + if (f[x] >= 0) + { + return f[x]; + } + int cnt = 0; + for (int i = 0; i < N; i++) + if (graph[x][i] > 0 && graph[x][i] < oo) + { + int k = i; + if (dist[k] > dist[x]) + { + cnt += dfs(k); + } + } + return f[x] = cnt; +} + +int main() +{ + while (cin >> N && N) + { + cin >> M; + init(); + input(); + Dijkstra(1); + for (int i = 0; i < N; i++) + { + f[i] = -1; + } + cout << dfs(1) << endl; + } + return 0; +} diff --git a/10918.cpp b/10918.cpp index 08d211f..a3b2a32 100644 --- a/10918.cpp +++ b/10918.cpp @@ -1,11 +1,52 @@ -#include +#include -int main() { - long long N, A[31] = { 1, 0, 3, 0, 11, 0, 41, 0, 153, 0, 571, 0, 2131, 0, 7953, 0, 29681, 0, 110771, 0, 413403, 0, 1542841, 0, 5757961, 0, 21489003, 0, 80198051, 0, 299303201 }; - for (; ; ) { - scanf("%lld", &N); - if (N == -1) return 0; - printf("%lld\n", A[N]); +using namespace std; + +int nTran, num; +int trans[20][2], dp[32][10], ans[32]; + +void backtrace(int n, int from, int to) +{ + if (n > 3) + { + return; + } + if (n == 3) + { + trans[nTran][0] = from; + trans[nTran][1] = to; + nTran++; + return; + } + backtrace(n + 2, from << 2, to << 2); + backtrace(n + 1, from << 1, (to << 1) | 1); + backtrace(n + 1, (from << 1) | 1, to << 1); +} + +void init() +{ + nTran = 0; + backtrace(0, 0, 0); + int i, j; + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + ans[0] = 1; + for (i = 0; i < 31; i++) + { + for (j = 0; j < nTran; j++) + { + dp[i + 1][trans[j][1]] += dp[i][trans[j][0]]; + } + ans[i + 1] = dp[i + 1][0]; + } +} + +int main() +{ + init(); + while (cin >> num && num >= 0) + { + cout << ans[num] << endl; } - return 0; + return 0; } diff --git a/10919.cpp b/10919.cpp new file mode 100644 index 0000000..fed0b36 --- /dev/null +++ b/10919.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int C[10000] = {0}; +int main() +{ + int t, k, m, x, c, r, suc; + for (t = 1; scanf("%d", &k) == 1; t++) + { + if (k == 0) + { + break; + } + scanf("%d", &m); + while (k--) + { + scanf("%d", &x); + C[x] = t; + } + suc = 1; + while (m--) + { + scanf("%d%d", &c, &r); + while (c--) + { + scanf("%d", &x); + if (C[x] == t) + { + r--; + } + } + if (r > 0) + { + suc = 0; + } + } + printf(suc ? "yes\n" : "no\n"); + } + return 0; +} diff --git a/1092.cpp b/1092.cpp new file mode 100644 index 0000000..33b4ec9 --- /dev/null +++ b/1092.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +#define OLD 1000010 + +bool xmap[OLD], ymap[OLD]; +int X[OLD], Y[OLD], xv[2015], yv[4015], x1[1015], y1_[1015], x2[1015], y2[1015], xn[2015], yn_[2015], grid[2015][4015], m, n, w; + +int main() +{ + int t = 1; + while (true) + { + scanf("%d%d%d", &m, &n, &w); + if (n == 0 && m == 0 && w == 0) + { + break; + } + memset(x1, 0, sizeof(x1)); + memset(y1_, 0, sizeof(y1_)); + memset(x2, 0, sizeof(x2)); + memset(y2, 0, sizeof(y2)); + memset(X, 0, sizeof(X)); + memset(xv, 0, sizeof(xv)); + memset(Y, 0, sizeof(Y)); + memset(yv, 0, sizeof(yv)); + memset(xmap, false, sizeof(xmap)); + memset(ymap, false, sizeof(ymap)); + + // append start and end cell to the end of the arr[] + // to get unique x, y coords + x1[w] = m - 1; + y1_[w] = 0; + x2[w] = 0; + y2[w] = n - 1; + + for (int i = 0; i < w + 1; i++) + { + if (i != w) + { + scanf("%d%d%d%d", &y1_[i], &x1[i], &y2[i], &x2[i]); + x1[i] = m - 1 - x1[i]; + x2[i] = m - 1 - x2[i]; + } + // mark all x,y coords + xmap[x1[i]] = true; + xmap[x2[i]] = true; + ymap[y1_[i]] = true; + ymap[y2[i]] = true; + } + + // get lst of unique x,y coords + memset(xn, 0, sizeof(xn)); + memset(yn_, 0, sizeof(yn_)); + int xsz = 0, ysz = 0; + for (int i = 0; i < OLD; i++) + { + if (xmap[i]) + xn[xsz++] = i; + if (ymap[i]) + yn_[ysz++] = i; + } + sort(xn, xn + xsz); + sort(yn_, yn_ + ysz); + + // map x coords (xn[]) to new coords + // set corresponding compressed val of new coord + X[xn[0]] = 0; + xv[0] = 1; + int xind = 1; + for (int i = 1; i < xsz; i++) + { + if (xn[i] - xn[i - 1] > 1) + { + xv[xind++] = xn[i] - xn[i - 1] - 1; + } + X[xn[i]] = xind; + xv[xind++] = 1; + } + + // map y coords (yn_[]) to new coords + // set corresponding compressed val of new coord + Y[yn_[0]] = 0; + yv[0] = 1; + int yind = 1; + for (int i = 1; i < ysz; i++) + { + if (yn_[i] - yn_[i - 1] > 1) + { + yv[yind++] = yn_[i] - yn_[i - 1] - 1; + } + Y[yn_[i]] = yind; + yv[yind++] = 1; + } + + memset(grid, 0, sizeof(grid)); + long long total = ((long long)m) * ((long long)n); + // mark all walls in grid after grid compression + for (int i = 0; i < w; i++) + { + int a1 = X[x1[i]]; + int b1 = Y[y1_[i]]; + int b2 = Y[y2[i]]; + for (int j = b1; j <= b2; j++) + { + total -= ((long long)xv[a1]) * ((long long)yv[j]); + grid[a1][j] = -1; + } + } + + // dst == blk: all empty cells ~ unreachable + if (grid[0][yind - 1] == -1) + { + printf("Case %d: %lld\n", t++, total); + continue; + } + + long long sum = 0LL; + grid[0][yind - 1] = 1; // mark dst + + // loop over all cells and mark either reachable or not according to i-1,j + // and i,j+1 and if not reachable, add actual value of this cell to total sum + for (int i = 0; i < xind; i++) + { + for (int j = yind - 1; j >= 0; j--) + { + if (i == 0 && j == yind - 1) + { + continue; + } + if (grid[i][j] == -1) + { + continue; + } + if (i - 1 >= 0 && grid[i - 1][j] == 1) + { + grid[i][j] = 1; + } + if (j + 1 < yind && grid[i][j + 1] == 1) + { + grid[i][j] = 1; + } + if (grid[i][j] == 0) + { + sum += ((long long)xv[i]) * ((long long)yv[j]); + } + } + } + printf("Case %d: %lld\n", t++, sum); + } + return 0; +} diff --git a/10920.cpp b/10920.cpp new file mode 100644 index 0000000..b168cd0 --- /dev/null +++ b/10920.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int main() +{ + long long int sz, p, q, x, y, l, mid; + while (scanf("%lld%lld", &sz, &p) == 2) + { + if ((sz | p) == 0) + { + break; + } + mid = (sz + 1) / 2; + if (p == 1) + { + printf("Line = %lld, column = %lld.\n", mid, mid); + continue; + } + for (p--, x = 50000, y = 25000;; y = (y + 1) / 2) + { + if (x < 1) + { + x = 1; + } + else if (x > 100000) + { + x = 99999; + } + if (p >= x * x) + { + x += y; + } + else if (p < (x - 1) * (x - 1)) + { + x -= y; + } + else + { + break; + } + } + x |= 1; + l = x / 2; + p -= (x - 2) * (x - 2); + q = p % (x - 1) + 1; + switch (p / (x - 1)) + { + case 0: + printf("Line = %lld, column = %lld.\n", mid + l, mid + l - q); + break; + case 1: + printf("Line = %lld, column = %lld.\n", mid + l - q, mid - l); + break; + case 2: + printf("Line = %lld, column = %lld.\n", mid - l, mid - l + q); + break; + case 3: + printf("Line = %lld, column = %lld.\n", mid - l + q, mid + l); + break; + } + } + return 0; +} diff --git a/10921.cpp b/10921.cpp index 2497789..9210bac 100644 --- a/10921.cpp +++ b/10921.cpp @@ -1,29 +1,39 @@ -#include -using namespace std; - -int main(){ - string str; - while(cin>>str){ - for(int i=0;i + +using namespace std; + +string nums = "2ABC3DEF4GHI5JKL6MNO7PQRS8TUV9WXYZ"; + +int main() +{ + string input; + map m; + int cur = 0; + for (int i = 0, sz = nums.size(); i < sz; i++) + { + if (nums[i] >= '2' && nums[i] <= '9') + { + cur = nums[i] - '0'; + } + else + { + m[nums[i]] = cur; + } + } + while (cin >> input) + { + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (m.find(input[i]) == m.end()) + { + cout << input[i]; + } + else + { + cout << m[input[i]]; + } + } + cout << endl; + } + return 0; +} diff --git a/10922.cpp b/10922.cpp index ac2dffa..e1fe101 100644 --- a/10922.cpp +++ b/10922.cpp @@ -1,82 +1,65 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -struct Rec{ - bool div; - int dep; - Rec(bool di, int de){ - div = di; - dep = de; - } -}; - -Rec rec(false, 1); - -void check(int N){ - if(N < 10){ - if(N==0 || N==9) - rec.div = true; - else - rec.div = false; - - return; - } - int s = 0; - while(N > 0){ - s += N%10; - N /= 10; - } - rec.dep++; - check(s); -} - -int main(){ - while(true){ - string str; - cin>>str; - if(str == "0") - break; - int sum = 0, i, LEN=str.length()-1; - FOI(i, 0, LEN) - sum += (str[i] - '0'); - rec = Rec(false, 1); - check(sum); - if(rec.div) - cout< + +using namespace std; + +/* +10922 +*/ +#define maxn 1000 * 9 + 2 +int A[maxn]; +char Fg[maxn]; +int Recur(int n) +{ + int i, k = 0, x; + char ss[1002]; + if (n == 9) + { + return 1; + } + sprintf(ss, "%d", n); + for (i = 0; ss[i]; i++) + { + k += ss[i] - '0'; + } + x = Recur(k); + return x + 1; +} +int Div(char xx[]) +{ + int i, c = 0, d, x; + for (i = 0; xx[i]; i++) + { + d = xx[i] - '0'; + x = c * 10 + d; + c = x % 9; + } + if (!c) + { + return 0; + } + return 1; +} +void Cal(char x[]) +{ + int i, d = 0; + if (Div(x)) + { + printf("%s is not a multiple of 9.\n", x); + return; + } + for (i = 0; x[i]; i++) + { + d += x[i] - '0'; + } + d = Recur(d); + printf("%s is a multiple of 9 and has 9-degree %d.\n", x, d); +} +int main() +{ + char x[1002]; + while (gets(x) && strcmp(x, "0")) + { + Cal(x); + } + return 0; +} diff --git a/10923.cpp b/10923.cpp new file mode 100644 index 0000000..2d713a8 --- /dev/null +++ b/10923.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +struct ss +{ + char B[9][8]; + int r, c; + int enemy, level; +}; +struct nn +{ + string S; + int level; +}; + +bool operator<(const nn &x, const nn &y) +{ + if (x.level != y.level) + { + return true; + } + if (x.S != y.S) + { + return true; + } + return false; +} +queue Q; +vector V; +map M; + +double Dis(int x1, int y1, int x2, int y2) +{ + double X, Y; + X = x1 - x2; + X *= X; + Y = y1 - y2; + Y *= Y; + return sqrt(X + Y); +} + +ss Pos(int r, int c, int x, int y) +{ + ss t; + int i, nx, ny; + double min = 1000000, d; + for (i = -1; i < 2; i++) + { + for (int j = -1; j < 2; j++) + { + if (!i && !j) + { + continue; + } + nx = r + i; + ny = c + j; + if (nx >= 9 || nx < 0 || ny >= 8 || ny < 0) + { + continue; + } + d = Dis(nx, ny, x, y); + if (d < min) + { + t.r = nx; + t.c = ny; + min = d; + } + } + } + return t; +} + +int Set(int r, int c, ss t) +{ + int i, j, enymy = t.enemy, l = 0; + string dum = ""; + nn g; + ss d, k; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 8; j++) + { + k.B[i][j] = t.B[i][j]; + if (k.B[i][j] == 'S') + k.B[i][j] = '.'; + } + } + k.B[r][c] = 'S'; + k.r = r; + k.c = c; + k.level = t.level + 1; + k.enemy = t.enemy; + V.clear(); + for (i = 0; i < 9; i++) + { + for (j = 0; j < 8; j++) + { + if (k.B[i][j] == 'E') + { + k.B[i][j] = '.'; + d = Pos(i, j, r, c); + V.push_back(d); + } + } + } + for (i = 0; i < V.size(); i++) + { + if (k.B[V[i].r][V[i].c] == 'S') + return 0; + else if (k.B[V[i].r][V[i].c] == '#') + { + k.enemy--; + if (k.enemy == 0) + return 1; + } + else if (k.B[V[i].r][V[i].c] == 'E') + { + k.enemy--; + if (k.enemy == 0) + return 1; + k.B[V[i].r][V[i].c] = '#'; + } + else + k.B[V[i].r][V[i].c] = 'E'; + } + int tt = 0; + for (i = 0; i < 9; i++) + for (j = 0; j < 8; j++) + { + dum += k.B[i][j]; + if (k.B[i][j] == 'E') + tt++; + } + if (!tt) + return 1; + g.level = k.level; + g.S = dum; + if (M[g] == true) + return 0; + else + M[g] = true; + if (k.level < 10) + Q.push(k); + return 0; +} + +int BFS(ss t) +{ + int i, nx, ny, j, dis, nomove; + t.level = 0; + Q.push(t); + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + nomove = 1; + for (i = -1; i < 2; i++) + { + for (j = -1; j < 2; j++) + { + nx = t.r + i; + ny = t.c + j; + if (nx < 0 || nx >= 9 || ny < 0 || ny >= 8) + continue; + if (t.B[nx][ny] != '.') + continue; + nomove = 0; + dis = Set(nx, ny, t); + if (dis) + return dis; + } + } + if (nomove) + return 0; + } + return 0; +} + +void Cal(ss t, int h) +{ + h = BFS(t); + if (!h) + { + printf("Oh no! I'm a dead man!\n"); + return; + } + else + printf("I'm the king of the Seven Seas!\n"); +} + +void Free() +{ + while (!Q.empty()) + Q.pop(); + M.clear(); +} + +int main() +{ + int k, i, j, h, r, c; + ss t; + scanf("%d", &k); + while (k--) + { + t.enemy = h = 0; + for (i = 0; i < 9; i++) + { + scanf("%s", t.B[i]); + for (j = 0; t.B[i][j]; j++) + { + if (t.B[i][j] == 'S') + { + r = i; + c = j; + } + else if (t.B[i][j] == '#') + h++; + else if (t.B[i][j] == 'E') + t.enemy++; + } + } + t.r = r; + t.c = c; + Cal(t, h); + Free(); + } + return 0; +} diff --git a/10924.cpp b/10924.cpp index 80da58f..f34e78a 100644 --- a/10924.cpp +++ b/10924.cpp @@ -1,62 +1,55 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; -bool isPrime(int N){ - for(int i=2; i*i<=N; i++) - if(N%i == 0) - return false; - return true; -} - -int main(){ - bool prime[1100]; - int i; - string str; - memset(prime, false, sizeof(prime)); - prime[1]=true; - prime[2]=true; - for(i=3; i<1100; i++) - prime[i] = isPrime(i); - while(cin>>str){ - int len = str.length(), tot=0; - for(i=0; i + +using namespace std; + +typedef unsigned long long ULL; +typedef long long LL; + +bool isnprime[1041]; + +void sieve() +{ + isnprime[0] = isnprime[1] = false; + for (int i = 2; i <= sqrt(1041); i++) + { + if (!isnprime[i]) + { + for (int j = i * i; j < 1041; j += i) + { + isnprime[j] = true; + } + } + } +} + +int main() +{ + sieve(); + string line; + while (cin >> line) + { + int sum = 0; + for (int i = 0; i < line.length(); i++) + { + int idx = 0; + if (line[i] >= 'a' && line[i] <= 'z') + { + idx = line[i] - 'a' + 1; + } + if (line[i] >= 'A' && line[i] <= 'Z') + { + idx = line[i] - 'A' + 27; + } + sum += idx; + } + if (!isnprime[sum]) + { + cout << "It is a prime word." << endl; + } + else + { + cout << "It is not a prime word." << endl; + } + } + return 0; +} diff --git a/10925.cpp b/10925.cpp new file mode 100644 index 0000000..4efbef2 --- /dev/null +++ b/10925.cpp @@ -0,0 +1,497 @@ +#include + +using namespace std; + +#define BASE 1000000 +#define MAX_DIGIT 350 + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +typedef long INT; + +struct BigNum +{ + INT sign, len, digit[MAX_DIGIT]; + void absAdd(BigNum, BigNum); + void absSubtract(BigNum, BigNum); + void multAndAdd(BigNum, INT, INT); + + void operator=(INT); //ok + void operator=(BigNum); //ok + void operator=(char *); //ok + void add(BigNum, INT); //ok- + void add(BigNum, BigNum); //ok- + void subtract(BigNum, INT); //ok- + void subtract(BigNum, BigNum); //ok- + void mult(BigNum, INT); //ok- + void mult(BigNum, BigNum); //ok- + void div(BigNum, INT, INT &); //ok- , third param is the remainder + void div(BigNum, BigNum, BigNum &);//ok- + INT comp(BigNum); //ok- + INT absComp(BigNum); //ok- + void print(); //ok- + void update(); //ok- + void clear(); //ok- + BigNum() + { + clear(); + } +}; +void BigNum::operator=(INT x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + INT i; + clear(); + len = n.len; + sign = n.sign; + //memset(digit, 0, sizeof(digit)); + for (i = 0; i < len; ++i) + { + digit[i] = n.digit[i]; + } +} +/* + while providing 's', ensure that there + are no leading spaces in it +*/ +void BigNum::operator=(char *s) +{ + INT i, v, p; + clear(); + p = 1; + i = strlen(s) - 1; + for (v = 0; i >= 0; i--) + { + if (s[i] == '-') + { + --i; + sign = 1; + break; + } + else if (s[i] == '+') + { + --i; + break; + } + if (s[i] < '0' || s[i] > '9') + { + continue; + } + v = v + (s[i] - '0') * p; + p *= 10; + if (p == BASE) + { + digit[len++] = v; + p = 1; + v = 0; + } + } + if (v) + { + digit[len++] = v; + } + update(); +} +/* + return value <===> meaning + 0 equal + 1 greater + -1 less +*/ +INT BigNum::comp(BigNum n) +{ + INT i; + if (sign < n.sign) + { + return 1; + } + if (sign > n.sign) + { + return -1; + } + i = max(len, n.len) - 1; + for (; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + if (sign == 0) + { + return 1; + } + else + { + return -1; + } + } + } + return 0; +} +INT BigNum::absComp(BigNum n) +{ + INT i; + if (len < n.len) + { + return -1; + } + if (len > n.len) + { + return 1; + } + for (i = len - 1; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + return 1; + } + else if (digit[i] < n.digit[i]) + { + return -1; + } + } + return 0; +} +void BigNum::absSubtract(BigNum a, BigNum b) +{ + INT pos, borrow, t; + borrow = 0; + t = max(a.len, b.len); + for (pos = 0; pos < t; ++pos) + { + digit[pos] = a.digit[pos] - b.digit[pos] - borrow; + if (digit[pos] < 0) + { + digit[pos] += BASE; + borrow = 1; + } + else + { + borrow = 0; + } + if (digit[pos] != 0) + { + len = pos + 1; + } + } + update(); + //assert(borrow == 0);//, "|B| > |A|; can't handle this"); +} +void BigNum::absAdd(BigNum a, BigNum b) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < max(a.len, b.len); ++pos) + { + digit[pos] = a.digit[pos] + b.digit[pos] + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + if (carry != 0) + { + //check for overflow + digit[max(a.len, b.len)] = carry; + len = max(a.len, b.len) + 1; + } + else + { + len = max(a.len, b.len); + } + update(); +} +void BigNum::add(BigNum a, INT n) +{ + BigNum t; + t = n; + add(a, t); +} +void BigNum::add(BigNum a, BigNum b) +{ + clear(); + if (a.sign == b.sign) + { + absAdd(a, b); + sign = a.sign; + } + else + { + if (a.absComp(b) >= 0) + { + absSubtract(a, b); + sign = a.sign; + } + else + { + absSubtract(b, a); + sign = b.sign; + } + } +} +void BigNum::subtract(BigNum a, INT n) +{ + BigNum t; + t = n; + subtract(a, t); +} +void BigNum::subtract(BigNum a, BigNum b) +{ + BigNum t; + clear(); + t = b; + t.sign = 1 - b.sign; + add(a, t); +} +void BigNum::mult(BigNum a, INT s) +{ + INT pos, carry; + if (s < 0) + { + sign = 1 - a.sign; + s = -s; + } + else + { + sign = a.sign; + } + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + //take care about any overflow + digit[pos] = a.digit[pos] * s + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + pos = a.len; + while (carry != 0) + { + //check overflow + digit[pos] = carry % BASE; + carry /= BASE; + ++pos; + } + len = pos; + update(); +} +void BigNum::multAndAdd(BigNum a, INT s, INT offset) +{ + INT pos, carry; + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos + offset] += a.digit[pos] * s + carry; + carry = digit[pos + offset] / BASE; + digit[pos + offset] %= BASE; + } + pos = a.len + offset; + while (carry != 0) + { + //check overflow + digit[pos] += carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + ++pos; + } + if (len < pos) + { + len = pos; + } + update(); +} +void BigNum::mult(BigNum a, BigNum b) +{ + INT i, pos; + clear(); + for (pos = 0; pos < b.len; ++pos) + { + multAndAdd(a, b.digit[pos], pos); + } + sign = (a.sign + b.sign) % 2; +} +void BigNum::div(BigNum a, INT s, INT &r) +{ + INT pos; + r = 0; + len = 0; + sign = a.sign; + if (s < 0) + { + sign = 1 - sign; + s = -s; + } + for (pos = a.len - 1; pos >= 0; --pos) + { + r = r * BASE + a.digit[pos]; + digit[pos] = r / s; + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + r %= s; + } + update(); +} +void BigNum::div(BigNum a, BigNum b, BigNum &r) +{ + INT i, pos, lower, upper, mid; + BigNum d, e, t; + if (b.sign) + { + t = b; + t.sign = 0; + div(a, t, r); + sign = 1 - a.sign; + return; + } + for (i = 0; i < len; ++i) + { + digit[i] = 0; + } + len = 0; + r = (INT)0; + for (pos = a.len - 1; pos >= 0; --pos) + { + r.mult(r, BASE); + r.add(r, a.digit[pos]); + lower = 0; + upper = BASE - 1; + while (upper > lower) + { + mid = (upper + lower) / 2 + 1; + d.mult(b, mid); + e.subtract(r, d); + if (e.sign == 0) + { + lower = mid; + } + else + { + upper = mid - 1; + } + } + digit[pos] = lower; + d.mult(b, lower); + r.subtract(r, d); + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + } + update(); +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::print() +{ + INT i; + if (sign) + { + printf("-"); + } + printf("%ld", digit[len - 1]); + for (i = len - 2; i >= 0; --i) + { + //BE CAREFUL HERE + switch (BASE) + { + case 10: + printf("%ld", digit[i]); + break; + case 100: + printf("%02ld", digit[i]); + break; + case 1000: + printf("%03ld", digit[i]); + break; + case 10000: + printf("%04ld", digit[i]); + break; + case 100000: + printf("%05ld", digit[i]); + break; + case 1000000: + printf("%06ld", digit[i]); + break; + case 10000000: + printf("%07ld", digit[i]); + break; + case 100000000: + printf("%08ld", digit[i]); + break; + case 1000000000: + printf("%09ld", digit[i]); + break; + default: + break; + } + } +} +/* ----------------------------------- Big Integer Class +-----------------------------*/ +void Cal(int n, int f) +{ + BigNum a, b, d, e; + char nn[100]; + a = "0"; + while (n--) + { + gets(nn); + b = nn; + d.add(a, b); + a = d; + } + b = f; + printf("costs "); + a.print(); + printf(": each friend should pay "); + d.div(a, b, e); + d.print(); + printf("\n"); +} +int main() +{ + char st[100]; + int N, F, k = 1; + while (gets(st)) + { + sscanf(st, "%d%d", &N, &F); + if (!N && !F) + { + break; + } + printf("Bill #%d ", k++); + Cal(N, F); + printf("\n"); + } + return 0; +} diff --git a/10926.cpp b/10926.cpp new file mode 100644 index 0000000..0a9b7fd --- /dev/null +++ b/10926.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +/* +10926 +How many Dependecies +*/ +#define maxn 102 + +char Fg[maxn]; +int D[maxn]; +list L[maxn]; +int N; +int DFS(int n) +{ + int x = 0, u; + Fg[n] = 1; + list::iterator p; + for (p = L[n].begin(); p != L[n].end(); p++) + { + u = *p; + if (Fg[u] == 0) + { + x += DFS(u); + } + } + D[n] = x; + return x + 1; +} +void Cal() +{ + int i, max = -1, sel; + for (i = 1; i <= N; i++) + { + if (Fg[i] == 0) + { + DFS(i); + if (D[i] > max) + { + max = D[i]; + sel = i; + } + for (int j = 1; j <= N; j++) + { + Fg[j] = 0; + } + } + } + cout << sel << endl; + for (i = 1; i <= N; i++) + { + D[i] = 0; + L[i].clear(); + } +} +int main() +{ + int n, v; + while (cin >> N && N) + { + for (int i = 1; i <= N; i++) + { + cin >> n; + while (n--) + { + cin >> v; + L[i].push_back(v); + } + } + Cal(); + } + return 0; +} diff --git a/10927.cpp b/10927.cpp new file mode 100644 index 0000000..3dffad4 --- /dev/null +++ b/10927.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const double eps = 1e-10; + +inline double dist(double x, double y) +{ + return x * x + y * y; +} + +class pt +{ +public: + int x, y; + int height; + double ang; + bool operator<(const pt &a) const + { + if (fabs(ang - a.ang) > eps) + { + return ang < a.ang; + } + return dist(x, y) < dist(a.x, a.y); + } +}; + +void solve(vector &a) +{ + vector> cannot; + sort(a.begin(), a.end()); + double prevang = -1000; + int prevheight = 0; + rep(i, a.size()) + { + if (prevang != a[i].ang) + { + prevang = a[i].ang; + prevheight = a[i].height; + } + else if (prevheight >= a[i].height) + { + prevang = a[i].ang; + cannot.push_back(make_pair(a[i].x, a[i].y)); + } + else + { + prevheight = max(prevheight, a[i].height); + } + } + if (cannot.size() == 0) + { + cout << "All the lights are visible." << endl; + } + else + { + sort(cannot.begin(), cannot.end()); + cout << "Some lights are not visible:" << endl; + rep(i, cannot.size()) + { + cout << "x = " << cannot[i].first << ", y = " << cannot[i].second; + if (i + 1 != cannot.size()) + { + cout << ";" << endl; + } + else + { + cout << "." << endl; + } + } + } +} + +main() +{ + int tc = 1, n; + while (cin >> n && n) + { + vector a(n); + cout << "Data set " << tc++ << ":" << endl; + rep(i, n) + { + cin >> a[i].x >> a[i].y >> a[i].height; + a[i].ang = atan2(a[i].y, a[i].x); + } + solve(a); + } + return false; +} diff --git a/10928.cpp b/10928.cpp index 70647ee..9b8b172 100644 --- a/10928.cpp +++ b/10928.cpp @@ -1,75 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - scanf("%d\n", &T); - while (T--){ - int N; - scanf("%d\n", &N); - string str; - vector< vector > V(N); - int M = N, i, j; - FOI(i, 0, N-1){ - getline(cin, str); +int main() +{ + int n, p; + int best; + vector results; + string input; + cin >> n; + while (n--) + { + cin >> p; + results.clear(); + getline(cin, input);// \n + best = 100000; + for (int i = 0; i < p; i++) + { + getline(cin, input); stringstream ss; - int I; - ss << str; - while (ss >> I) - V[i].push_back(I); - M = min((int)V[i].size(), M); + ss << input; + int tmp; + int counter = 0; + while (ss >> tmp) + { + counter++; + } + if (counter < best) + { + best = counter; + results.clear(); + results.push_back(i + 1); + } + else if (counter == best) + { + results.push_back(i + 1); + } } - vector< int > vec; - FOI(i, 0, N-1) - if (V[i].size() == M) - vec.push_back(i + 1); - FOI(i, 0, vec.size()-1){ - cout << vec[i]; - if (i < vec.size() - 1) + sort(results.begin(), results.end()); + for (int i = 0, sz = results.size(); i < sz; i++) + { + if (i > 0) + { cout << " "; - else - cout << "\n"; + } + cout << results[i]; } - if (T) - getline(cin, str); + cout << endl; } return 0; } - diff --git a/10929.cpp b/10929.cpp index 1c1843e..3a5d5f4 100644 --- a/10929.cpp +++ b/10929.cpp @@ -1,55 +1,38 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - string str; - cin>>str; - if(str=="0") - break; - int len = str.length(), i; - int64 sum=0; - for(i=0; i + +using namespace std; + +char dig[10000]; + +int main() +{ + int odd, even; + while (gets(dig)) + { + if (!strcmp(dig, "0")) + { + break; + } + odd = even = 0; + for (int i = 0; dig[i]; i++) + { + if (i % 2) + { + odd += dig[i] - '0'; + } + else + { + even += dig[i] - '0'; + } + } + if ((odd - even) % 11) + { + printf("%s is not a multiple of 11.\n", dig); + } + else + { + printf("%s is a multiple of 11.\n", dig); + } + } + return 0; +} diff --git a/1093.cpp b/1093.cpp new file mode 100644 index 0000000..b3d8a2b --- /dev/null +++ b/1093.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +const int N = 110, INF = 0x3f3f3f3f; +int n; + +vector adj[N]; +int root = 0; +int a[N], b[N], c[N]; +int x[N], y[N]; + +bool cmp_vec(const pair &u, const pair &v) +{ + return u.first - u.second < v.first - v.second; +} + +void dfs(int cur, int fa) +{ + vector> vec; + for (int i = 0; i < (int)adj[cur].size(); ++i) + { + int v = adj[cur][i]; + if (v == fa) + continue; + dfs(v, cur); + vec.push_back(make_pair(x[v], y[v])); + } + sort(vec.begin(), vec.end(), cmp_vec); + + y[cur] = b[cur] + c[cur]; + x[cur] = 0; + for (int i = 0; i < (int)vec.size(); ++i) + { + x[cur] = max(vec[i].first, x[cur] + vec[i].second); + y[cur] += vec[i].second; + } + x[cur] = max(a[cur], x[cur] + b[cur] + c[cur]); +} + +int Ans() +{ + dfs(root, -1); + return x[root]; +} + +int main() +{ + int tcas = 0; + while (cin >> n, n) + { + for (int i = 1; i <= n; ++i) + scanf("%d%d%d", a + i, b + i, c + i), adj[i].clear(); + for (int i = 1; i < n; ++i) + { + int u, v; + scanf("%d%d", &u, &v); + adj[u].push_back(v); + adj[v].push_back(u); + } + int ans = INF; + for (int i = 1; i <= n; ++i) + { + root = i; + ans = min(ans, Ans()); + } + printf("Case %d: %d\n", ++tcas, ans); + } +} diff --git a/10930.cpp b/10930.cpp index d8a2c90..99f3283 100644 --- a/10930.cpp +++ b/10930.cpp @@ -1,66 +1,92 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +/* +10930 +*/ -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, t = 1; - while( cin >> N ){ - int vec[N]; - int i, j, k; - FOI(i, 0, N-1) - cin >> vec[i]; - vector V; - V.push_back(vec[0]); - FOI(i, 1, N-1){ - int SZ = V.size() - 1; - FOI(j, 0, SZ) - V.push_back(V[j] + vec[i]); +#define maxn 40 +char Fg[31000]; +int A[maxn]; +int Recur(int n, int sum, int level, int lim, int k) +{ + int i, j = sum, d; + if (level > 1 && Fg[sum] == 1) + { + return 1; + } + for (i = n + 1; i < k; i++) + { + if (A[i] + sum > lim) + { + return 0; + } + d = Recur(i, A[i] + sum, level + 1, lim, k); + if (d) + { + return 1; + } + } + return 0; +} +void Cal(int n) +{ + int d, k; + for (k = 1; k < n; k++) + { + d = Recur(k, A[k], 1, A[n], n); + if (d) + { + break; } - V.erase(V.begin()); - sort(V.begin(), V.end()); - bool isA = true; - FOI(k, 0, N-1){ - if( binary_search(V.begin(), V.end(), vec[k]) ){ - isA = false; - break; + } + if (d == 0) + { + cout << "This is an A-sequence.\n"; + } + else + { + cout << "This is not an A-sequence.\n"; + } +} +void Free(int n) +{ + int i; + for (i = 1; i <= n; i++) + { + Fg[A[i]] = 0; + } +} +int main() +{ + int n, i, fg = 0, pre = 0, k; + int ks = 1; + while (cin >> n) + { + fg = 0; + pre = 0; + cout << "Case #" << ks++ << ":"; + for (i = 1; i <= n; i++) + { + cin >> A[i]; + cout << " " << A[i]; + if (pre >= A[i]) + { + fg = 1; } + pre = A[i]; + Fg[A[i]] = 1; } - cout << "Case #" << t++ << ":"; - FOI(i, 0, N-1) - cout << " " << vec[i]; cout << endl; - if( isA ) - cout << "This is an A-sequence.\n"; - else + if (fg) + { cout << "This is not an A-sequence.\n"; + Free(n); + continue; + } + Cal(n); + Free(n); } return 0; } diff --git a/10931.cpp b/10931.cpp index 4838f58..d2c8edf 100644 --- a/10931.cpp +++ b/10931.cpp @@ -1,38 +1,38 @@ -#include -#include -#include -using namespace std; +#include -int Parity(int N){ - int cnt = 0; - while( N ){ - cnt += N%2; - N /= 2; - } - return cnt; -} +using namespace std; -string toBinary(int N){ - string str = ""; - while( N ){ - if( N%2 ) - str = "1" + str; +void int2bin(int n) +{ + for (int i = numeric_limits::digits - 1; i >= 0; i--) + { + if (1 << i > n) + { + continue; + } + if (n & (1 << i)) + { + cout << 1; + } else - str = "0" + str; - N /= 2; + { + cout << 0; + } } - return str; } -int main(){ - while( true ){ - int N; - cin >> N; - if( N == 0 ) +int main() +{ + int I; + while (cin >> I) + { + if (I == 0) + { break; - string bin = toBinary(N); - int P = Parity(N); - cout << "The parity of " << bin << " is " << P << " (mod 2)." << endl; + } + cout << "The parity of "; + int2bin(I); + cout << " is " << __builtin_popcount(I) << " (mod 2)." << endl; } return 0; } diff --git a/10932.cpp b/10932.cpp new file mode 100644 index 0000000..d918fb6 --- /dev/null +++ b/10932.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +double table[30]; // data as 'a'...'z' +char expression[10000];// input string +char opt[10000]; // stack of operator +double data[10000]; // stack of number +int Nopt, Ndata, Lexpr; +// used by question() +void calculate() +{ + Nopt--; + Ndata--; + switch (opt[Nopt]) + { + case '+': + if (Nopt > 0 && opt[Nopt - 1] == '-') + { + data[Ndata - 1] -= data[Ndata]; + } + else + { + data[Ndata - 1] += data[Ndata]; + } + break; + case '-': + if (Nopt > 0 && opt[Nopt - 1] == '-') + { + data[Ndata - 1] += data[Ndata]; + } + else + { + data[Ndata - 1] -= data[Ndata]; + } + break; + case '*': + data[Ndata - 1] *= data[Ndata]; + break; + case '/': + // Notice data[Ndata] == 0, but in this problem ignored + data[Ndata - 1] /= data[Ndata]; + break; + default: + printf("error in calculate()\n"); + break; + } +} +// calculate the expression +double question(char *expr) +{ + int Pexpr = 0; + Lexpr = strlen(expr); + Ndata = Nopt = 0; + while (Pexpr < Lexpr) + { + switch (expr[Pexpr]) + { + case '+': + case '-': + case '*': + case '/': + case '(': + opt[Nopt++] = expr[Pexpr++]; + break; + case ')': + Pexpr++; + while (opt[Nopt - 1] != '(') + { + calculate(); + } + Nopt--; + while (opt > 0 && (opt[Nopt - 1] == '*' || opt[Nopt - 1] == '/')) + { + calculate(); + } + break; + default: + if (expr[Pexpr] >= '0' && expr[Pexpr] <= '9') + { + double integer = 0; + while (expr[Pexpr] >= '0' && expr[Pexpr] <= '9') + { + integer *= 10, integer += expr[Pexpr] - '0', Pexpr++; + } + data[Ndata++] = integer; + } + else if (expr[Pexpr] >= 'a' && expr[Pexpr] <= 'z') + { + data[Ndata++] = table[expr[Pexpr] - 'a']; + Pexpr++; + } + else + { + printf("error in question()\n"); + } + while (opt > 0 && (opt[Nopt - 1] == '*' || opt[Nopt - 1] == '/')) + { + calculate(); + } + break; + } + } + while (Nopt > 0) + { + calculate(); + } + return data[0]; +} +void attribution(char *expr) +{ + // calculate X (expr[0]-'a') through question() + table[expr[0] - 'a'] = question(expr + 2); +} +int main() +{ + for (int i = 0; i < 26; i++) + { + data[i] = 0; + } + while (gets(expression)) + { + Lexpr = strlen(expression); + if (Lexpr == 0) + { + continue; + } + if (expression[1] == '=')// expression is a attribution + { + attribution(expression); + } + else// is a question + { + printf("%.2lf\n", question(expression)); + } + } + return 0; +} diff --git a/10933.cpp b/10933.cpp new file mode 100644 index 0000000..1537741 --- /dev/null +++ b/10933.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +char tm_[100]; + +#define eatNL cin.getline(tm_, sizeof(tm_)); + +struct Node +{ + char name[50]; + int win, winMLoss, totalPoint; +} teams[1020]; + +inline bool cmp(Node a, Node b) +{ + if (a.win != b.win) + { + return a.win > b.win; + } + else if (a.winMLoss != b.winMLoss) + { + return a.winMLoss > b.winMLoss; + } + else if (a.totalPoint != b.totalPoint) + { + return a.totalPoint > b.totalPoint; + } + return (strcmp(a.name, b.name) <= 0); +} + +int searchTeams(char *_name, int n) +{ + for (int i = 0; i < n; ++i) + { + if (strcmp(_name, teams[i].name) == 0) + { + return i; + } + } +} + +int main() +{ + char token[100], *name, *p, *name1, *name2; + int t, m, i, j, index1, index2, temp1, temp2, win1, win2, p1, p2; + while (cin >> t) + { + if (t == 0) + { + break; + } + eatNL; + for (i = 0; i < t; ++i) + { + cin.getline(teams[i].name, sizeof(teams[i].name)); + teams[i].win = teams[i].winMLoss = teams[i].totalPoint = 0; + } + cin >> m; + eatNL; + while (m--) + { + cin.getline(token, sizeof(token)); + name = strtok(token, ":"); + p = strtok(NULL, " \t\n"); + j = win1 = win2 = p1 = p2 = 0; + while (p) + { + sscanf(p, "%d%d", &temp1, &temp2); + temp2 = temp2 * -1; + p1 += temp1; + p2 += temp2; + if (temp1 >= 25 && (temp1 - temp2) >= 2) + { + ++win1; + } + else + { + ++win2; + } + ++j; + p = strtok(NULL, " \t\n"); + } + name1 = strtok(name, "-"); + name2 = strtok(NULL, "\n"); + index1 = searchTeams(name1, t); + index2 = searchTeams(name2, t); + if (win1 >= 3) + { + teams[index1].win++; + teams[index1].winMLoss += win1 - win2; + teams[index1].totalPoint += p1; + teams[index2].winMLoss += win2 - win1; + teams[index2].totalPoint += p2; + } + else + { + teams[index2].win++; + teams[index2].winMLoss += win2 - win1; + teams[index2].totalPoint += p2; + teams[index1].winMLoss += win1 - win2; + teams[index1].totalPoint += p1; + } + } + sort(teams, teams + t, cmp); + for (i = 0; i < t; ++i) + { + printf("%-20s", teams[i].name); + printf("%5d", teams[i].win); + printf("%5d", teams[i].winMLoss); + printf("%10d\n", teams[i].totalPoint); + } + printf("\n"); + } + return 0; +} diff --git a/10934.cpp b/10934.cpp new file mode 100644 index 0000000..886408c --- /dev/null +++ b/10934.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int ans; +unsigned long long tbl[101][64]; + +void bisearch(unsigned long long *ptr, unsigned long long target, int left, int right) +{ + int center; + if (left > right) + { + return; + } + center = left + right; + center /= 2; + /*printf("%d %I64u %I64d\n", center, ptr[center], target);*/ + /*printf("%d %d %d\n", center, ptr[center], target);*/ + if (ptr[center] <= target) + { + ans = center; + bisearch(ptr, target, center + 1, right); + } + else + { + bisearch(ptr, target, left, center - 1); + } +} + +int main() +{ + int n, i, j; + unsigned long long m; + for (i = 0; i < 65; i++) + { + tbl[0][i] = 1; + } + for (i = 1; i <= 100; i++) + { + tbl[i][1] = 1; + for (j = 2; j < 65; j++) + { + tbl[i][j] = tbl[i - 1][j - 1] + tbl[i][j - 1]; + /*if(i < 5 && j < 10) printf("%d %d => %I64u\n", i, j, tbl[i][j]);*/ + } + } + while (scanf("%d%llu", &n, &m) == 2) + { + if (!n) + { + break; + } + ans = 70; + /*printf("%d\n", tbl[n][9]);*/ + bisearch(tbl[n], m, 1, 64); + if (ans > 63) + { + printf("More than 63 trials needed.\n"); + } + else + { + printf("%d\n", ans); + } + } + return 0; +} diff --git a/10935.cpp b/10935.cpp index 4993a4c..9028599 100644 --- a/10935.cpp +++ b/10935.cpp @@ -1,35 +1,47 @@ -#include -#include -#include +#include + using namespace std; -int main() { - while (true) { - int N; - scanf("%d", &N); - if (!N) break; - - queue< int > q; - vector< int > T; - for (int i = 1; i <= N; i++) q.push(i); - - while (q.size() > 2) { - T.push_back(q.front()); - q.pop(); - q.push(q.front()); - q.pop(); +int main() +{ + int n; + int remain; + while (cin >> n) + { + if (!n) + { + break; } - if (q.size() == 2) { - T.push_back(q.front()); - q.pop(); + deque q; + bool first = true; + for (int i = 1; i <= n; i++) + { + q.push_back(i); } - - printf("Discarded cards:"); - for (int i = 0; i < T.size(); i++) { - if (i > 0) printf(","); - printf(" %d", T[i]); + cout << "Discarded cards:"; + while (!q.empty()) + { + remain = q.front(); + q.pop_front(); + if (q.empty()) + { + break; + } + if (first) + { + cout << " " << remain; + } + else + { + cout << ", " << remain; + } + remain = q.front(); + q.pop_front(); + q.push_back(remain); + first = false; } - printf("\n"); - printf("Remaining card: %d\n", q.front()); + cout << endl; + cout << "Remaining card: " << remain << endl; } + return 0; } diff --git a/10936.cpp b/10936.cpp new file mode 100644 index 0000000..6585cf0 --- /dev/null +++ b/10936.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + double x, y, t, d, a, a1, a2, a3, pi = 2 * acos(0.); + int n; + while (scanf("%d %lf %*s", &n, &x) >= 2 && n >= 3) + { + y = a = 0.; + t = x; + while (n-- > 1 && scanf("%lf %lf d %lf ' %lf \"", &d, &a1, &a2, &a3) == 4) + { + t += d; + a += pi - (a1 + a2 / 60. + a3 / 3600.) * pi / 180.; + x += d * cos(a); + y += d * sin(a); + } + if (t < 1e-9 || (sqrt(x * x + y * y) / t) < 0.001) + { + printf("Acceptable\n"); + } + else + { + printf("Not acceptable\n"); + } + } + return 0; +} diff --git a/10937.cpp b/10937.cpp new file mode 100644 index 0000000..c0b89bc --- /dev/null +++ b/10937.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +#define maxn 52 + +char Is[maxn][maxn], Color[maxn][maxn], Fg[13]; +int R, C, abS, St, mins, Num[maxn * 52], Dis[13][13]; +int X[] = {0, 0, -1, 1}; +int Y[] = {-1, 1, 0, 0}; + +struct ss +{ + int x, y; + int cost; +}; +vector V; +queue Q; + +void ReadCase() +{ + int i, j, d; + abS = 0; + ss t; + for (i = 0; i < R; i++) + { + cin >> Is[i]; + for (j = 0; Is[i][j]; j++) + { + if (Is[i][j] == '!' || Is[i][j] == '@') + { + d = i * C + j; + t.x = i; + t.y = j; + Num[d] = abS++; + V.push_back(t); + if (Is[i][j] == '@') + St = abS - 1; + } + else if (Is[i][j] != '#' && Is[i][j] != '*' && Is[i][j] != '.') + Is[i][j] = '#'; + } + } +} + +int Nativ(int x, int y) +{ + int nx, ny, i, j; + for (i = -1; i <= 1; i++) + { + for (j = -1; j <= 1; j++) + { + if (!i && !j) + continue; + nx = x + i; + ny = y + j; + if (nx < 0 || nx >= R || ny < 0 || ny >= C) + continue; + if (Is[nx][ny] == '*') + continue; + if (Is[nx][ny] == '!' || Is[nx][ny] == '@') + return 1; + Is[nx][ny] = '#'; + } + } + Is[x][y] = '#'; + return 0; +} + +int BFS(int x, int y, int u) +{ + ss ts, ds; + int i, nx, ny, d, v, f = 1; + ts.cost = 0; + ts.x = x; + ts.y = y; + Q.push(ts); + Color[x][y] = 1; + while (!Q.empty()) + { + ts = Q.front(); + Q.pop(); + for (i = 0; i < 4; i++) + { + nx = ts.x + X[i]; + ny = ts.y + Y[i]; + if (nx >= R || nx < 0 || ny >= C || ny < 0) + continue; + if (Color[nx][ny] == 1) + continue; + if (Is[nx][ny] == '#' || Is[nx][ny] == '~') + continue; + if (Is[nx][ny] == '!' || Is[nx][ny] == '@') + { + d = nx * C + ny; + v = Num[d]; + Dis[u][v] = Dis[v][u] = ts.cost + 1; + f++; + if (f == abS) + return 0; + } + Color[nx][ny] = 1; + ds.cost = ts.cost + 1; + ds.x = nx; + ds.y = ny; + Q.push(ds); + } + } + return 1; +} + +void ReFill()// Ok +{ + for (int i = 0; i < R; i++) + for (int j = 0; j < C; j++) + Color[i][j] = 0; +} + +int SetNativ() +{ + int i, j; + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + if (Is[i][j] == '*') + if (Nativ(i, j)) + { + cout << -1 << endl; + return 1; + } + return 0; +} + +void Recur(int n, int level, int cost) +{ + int i; + if (cost >= mins) + return; + if (level == abS) + { + if (cost + Dis[n][St] < mins) + mins = cost + Dis[n][St]; + return; + } + Fg[n] = 1; + for (i = 0; i < abS; i++) + { + if (Fg[i] == 0) + Recur(i, level + 1, cost + Dis[n][i]); + } + Fg[n] = 0; +} + +void Cal() +{ + int i; + if (SetNativ()) + return; + if (abS == 1) + { + cout << 0 << endl; + return; + } + for (i = 0; i + 1 < abS; i++) + { + if (BFS(V[i].x, V[i].y, i)) + { + cout << -1 << endl; + return; + } + while (!Q.empty()) + Q.pop(); + ReFill(); + } + mins = 21474836; + Recur(St, 1, 0); + assert(mins != 21474836); + cout << mins << endl; +} + +void Free() +{ + V.clear(); + while (!Q.empty()) + Q.pop(); + ReFill(); + for (int i = 0; i < 13; i++) + Fg[i] = 0; +} + +int main() +{ + while (cin >> R >> C) + { + if (!R && !C) + break; + ReadCase();// OK + Cal(); + Free(); + } + return 0; +} diff --git a/10938.cpp b/10938.cpp new file mode 100644 index 0000000..564e5c9 --- /dev/null +++ b/10938.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define maxn 5000 + +list link1[maxn + 2]; +char C[maxn + 2]; +int N, ind, Mark[maxn + 2], Rec[maxn + 2]; + +int MarkPath(int u, int des, int level) +{ + int v, d; + list::iterator p; + C[u] = 1; + Rec[level] = u; + Mark[ind++] = u; + if (des == u) + return level; + for (p = link1[u].begin(); p != link1[u].end(); p++) + { + v = *p; + if (C[v]) + continue; + d = MarkPath(v, des, level + 1); + if (d) + return d; + } + return 0; +} + +void Clear(int d) +{ + int i, u; + for (i = 0; i < ind; i++) + { + u = Mark[i]; + C[u] = 0; + } +} + +void Cal() +{ + int n, u, v, d; + for (n = 1; n <= N; n++) + C[n] = 0; + scanf("%d", &n); + while (n--) + { + ind = 0; + scanf("%d%d", &u, &v); + d = MarkPath(u, v, 0); + if (d % 2 == 0) + printf("The fleas meet at %d.\n", Rec[d / 2]); + else + { + u = Rec[d / 2]; + v = Rec[d / 2 + 1]; + if (u > v) + { + d = u; + u = v; + v = d; + } + printf("The fleas jump forever between %d and %d.\n", u, v); + } + Clear(ind); + } +} + +void Free() +{ + for (int i = 1; i <= N; i++) + link1[i].clear(); +} + +int main() +{ + int i, u, v; + while (scanf("%d", &N) && N) + { + for (i = 0; i + 1 < N; i++) + { + scanf("%d%d", &u, &v); + link1[u].push_back(v); + link1[v].push_back(u); + } + Cal(); + Free(); + } + return 0; +} diff --git a/10939.cpp b/10939.cpp new file mode 100644 index 0000000..7816756 --- /dev/null +++ b/10939.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +#define INF 0x3fffffff +#define N 8192 +#define K 1024 + +int nxt[2 * N], adj[2 * N], vant[N], ant[K], chase[K], Tgot[N], Tdist[N], Tant[N], Tmoved[N], n, k, id; + +void ch(int p, int x, int tm) +{ + int i; + /*printf("ch(p=%d, x=%d, tm=%d) Tdist[x]=%d Tant[x]=%d\n",p,x,tm,Tdist[x],Tant[x]);*/ + if (Tdist[x] == INF || tm == 0) + { + return; + } + if (x == ant[Tant[x]]) + { + return; + } + if (!Tmoved[Tant[x]] && Tdist[x] <= tm) + { + /*printf("ant %d: %d -> %d\n", Tant[x], ant[Tant[x]], x);*/ + vant[ant[Tant[x]]] = 0; + Tdist[ant[Tant[x]]] = INF; + vant[x] = Tant[x]; + ant[Tant[x]] = x; + Tmoved[Tant[x]] = 1; + tm = Tdist[x]; + } + else if (Tmoved[Tant[x]] && Tdist[x] < tm) + { + tm = Tdist[x]; + } + for (i = nxt[x]; i != 0; i = nxt[i]) + if (adj[i] != p) + { + ch(x, adj[i], tm); + } +} + +void go(int p, int x) +{ + int i, y; + if (vant[x] > 0) + { + Tdist[x] = 0; + Tant[x] = vant[x]; + return; + } + Tdist[x] = INF; + Tant[x] = 0; + for (i = nxt[x]; i != 0; i = nxt[i]) + { + y = adj[i]; + if (y == p) + { + continue; + } + go(x, y); + if ((Tdist[y] + 1) < Tdist[x] || + ((Tdist[y] + 1) == Tdist[x] && Tant[y] < Tant[x])) + { + Tdist[x] = Tdist[y] + 1; + Tant[x] = Tant[y]; + } + } +} + +int main() +{ + int i, x, y, z; + while (scanf("%d", &n) == 1 && n > 0) + { + for (i = 0; i <= n; i++) + { + nxt[i] = 0; + } + for (z = n + 1, i = 1; i < n && scanf("%d %d", &x, &y) == 2; i++) + { + adj[z] = y; + nxt[z] = nxt[x]; + nxt[x] = z++; + adj[z] = x; + nxt[z] = nxt[y]; + nxt[y] = z++; + } + for (i = 0; i <= n; i++) + { + vant[i] = 0; + } + scanf("%d", &k); + for (i = 1; i <= k; i++) + { + chase[i] = 0; + scanf("%d", &ant[i]); + vant[ant[i]] = i; + } + /*printf("ants:");for(i=1;i<=k;i++)printf(" %d",ant[i]);printf("\n");*/ + for (scanf("%d", &y); y-- > 0 && scanf("%d", &x) == 1;) + { + for (i = 0; i <= n; i++) + { + Tdist[i] = INF; + } + for (i = 0; i <= n; i++) + { + Tmoved[i] = 0; + } + /*printf("x=%d\n",x);*/ + go(0, x); + ch(0, x, INF - 1); + chase[vant[x]]++; + /*printf("chased: %d\n\n", vant[x]); + printf("ants:");for(i=1;i<=k;i++)printf(" %d",ant[i]);printf("\n");*/ + } + for (i = 1; i <= k; i++) + { + printf("%d %d\n", ant[i], chase[i]); + } + } + return 0; +} diff --git a/1094.cpp b/1094.cpp new file mode 100644 index 0000000..5079cd9 --- /dev/null +++ b/1094.cpp @@ -0,0 +1,319 @@ +#include + +using namespace std; + +template +void checkmin(T &t, T x) +{ + if (x < t) + t = x; +} +template +void checkmax(T &t, T x) +{ + if (x > t) + t = x; +} +template +void _checkmin(T &t, T x) +{ + if (t == -1) + t = x; + if (x < t) + t = x; +} +template +void _checkmax(T &t, T x) +{ + if (t == -1) + t = x; + if (x > t) + t = x; +} +typedef pair PII; +typedef pair PDD; +typedef long long lld; +#define foreach(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++) +#define DEBUG(a) cout << #a " = " << (a) << endl; +#define DEBUGARR(a, n) \ + for (int i = 0; i < (n); i++) \ + { \ + cout << #a "[" << i << "] = " << (a)[i] << endl; \ + } + +#define MOD 4007 +int N, M; +char mat[50][50]; +int f[181][MOD], ST[181][MOD], table[181][MOD], No[181][MOD], Pre[181][MOD], Dump[181][MOD]; +int Sum[181]; +int getCP(int state, int i) +{ + return 3 & (state >> (i << 1)); +} +void print(int state) +{ + for (int i = 0; i <= M; i++) + printf("%d ", getCP(state, i)); + printf("\n"); +} +int dump = 0; +void Push(int r, int state, int value, int lastpos) +{ + int p; + for (p = state % MOD; table[r][p] != state && table[r][p] != -1; + p = (p + 1 == MOD ? 0 : p + 1)) + ; + if (table[r][p] == state) + { + if (value > f[r][No[r][p]]) + { + f[r][No[r][p]] = value; + Pre[r][No[r][p]] = lastpos; + } + } + else + { + table[r][p] = state; + No[r][p] = ++Sum[r]; + ST[r][Sum[r]] = state; + f[r][Sum[r]] = value; + Pre[r][No[r][p]] = lastpos; + Dump[r][No[r][p]] = dump; + } +} +bool check(int st) +{ + int i; + for (i = 0; i < M; i++) + if (!(getCP(st, i) == 0 || getCP(st, i) == 3)) + return 0; + if (getCP(st, M) != 1) + return 0; + return 1; +} +int work() +{ + int i, j, L, R, k; + for (i = 0; i <= N * M; i++) + memset(table[i], -1, sizeof(table[i])); + memset(Sum, 0, sizeof(Sum)); + Push(0, 1, 0, 0); + for (i = 0; i < N; i++) + { + for (j = 0; j < M; j++) + { + + // printf("\n\n\n%d %d---------\n",i,j); + + int now = i * M + j, then = now + 1; + for (k = 1; k <= Sum[now]; k++) + { + + dump = 0; + int cl = ST[now][k]; + if (j == 0) + { + cl <<= 2; + cl &= ((1 << ((M + 1) * 2)) - 1); + } + L = getCP(cl, j); + R = getCP(cl, j + 1); + /*if (i==2&&j==4){ + print(ST[now][k]); + printf("%d\n====\n",f[now][k]); + printf("L R: %d %d\n",L,R); + }*/ + if (L) + cl -= L << (j << 1); + if (R) + cl -= R << (2 * j + 2); + int right = 0; + if (j + 2 <= M) + right = getCP(cl, j + 2); + + if (L == 0 && R == 0) + { + Push(then, cl, f[now][k], k); + if (mat[i][j] != '#' && mat[i + 1][j] != '#' && mat[i][j + 1] != '#')//add + Push(then, cl | (1 << (2 * j)) | (2 << (2 * j + 2)), f[now][k] + 1, k); + } + else if ((L == 0 && (R == 1 || R == 2)) || (R == 0 && (L == 1 || L == 2))) + { + int add = L + R; + + int nst = cl; + + if (mat[i + 1][j] != '#') + { //add + // print(nst|(add<<(j*2))|(3<<(j*2+2))); + if (!(R == 0 && right != 0)) + Push(then, nst | (add << (j * 2)) | (3 << (j * 2 + 2)), f[now][k] + 1, k); + } + if (mat[i][j + 1] != '#') + { + //add + + Push(then, nst | (add << (j * 2 + 2)) | (3 << (j * 2)), f[now][k] + 1, k); + } + } + else if ((L == 3 && (R == 0 || R == 3)) || (R == 3 && (L == 0 || L == 3))) + { + // print(cl); + Push(then, cl, f[now][k], k); + } + else if (L == 1 && R == 1) + {//add + int tmp, tot = 0, t = j + 2; + for (; t <= M; t++) + { + tmp = getCP(cl, t); + if (tmp == 1) + tot++; + if (tmp == 2) + tot--; + if (tot < 0) + break; + } + if ((right == 0 || right == 3)) + { + int nst = cl; + if (t <= M) + nst ^= (3 << (t << 1)); + nst += 3 << (2 * j); + nst += 3 << (2 * j + 2); + if (j != M - 1) + { + if (right == 3) + nst -= (3 << (2 * (j + 2))); + dump = 1; + Push(then + 1, nst, f[now][k] + 1, k); + } + else + { + Push(then, nst, f[now][k] + 1, k); + } + } + } + else if (L == 2 && R == 1) + { + //add + if ((right == 0 || right == 3)) + { + int nst = cl; + nst += 3 << (2 * j); + nst += 3 << (2 * j + 2); + if (j != M - 1) + { + if (right == 3) + nst -= (3 << (2 * (j + 2))); + dump = 1; + Push(then + 1, nst, f[now][k] + 1, k); + } + else + { + Push(then, nst, f[now][k] + 1, k); + } + } + } + else if (L == 2 && R == 2) + { + int tot = 0, tmp, t = j - 1; + for (; t >= 0; t--) + { + tmp = getCP(cl, t); + if (tmp == 2) + tot++; + if (tmp == 1) + tot--; + if (tot < 0) + break; + } + if (t != -1 && (right == 0 || right == 3)) + { + int nst = cl ^ (3 << (t << 1)); + nst += 3 << (2 * j); + nst += 3 << (2 * j + 2); + + if (j != M - 1) + { + if (right == 3) + nst -= (3 << (2 * (j + 2))); + dump = 1; + Push(then + 1, nst, f[now][k] + 1, k); + } + else + { + Push(then, nst, f[now][k] + 1, k); + } + } + } + } + } + } + int now = N * M; + int ans = 0; + for (k = 1; k <= Sum[now]; k++) + { + + if (check(ST[now][k])) + { + //printf("-------%d\n",f[now][k]); + //print(ST[now][k]); + ans = max(ans, f[now][k]); + } + } + for (k = 1; k <= Sum[now]; k++) + if (check(ST[now][k]) && ans == f[now][k]) + { + break; + } + //printf("%d\n",ans); + int st = k; + int chos[200]; + memset(chos, 0, sizeof(chos)); + while (now != 0) + { + // print(ST[now][k]); + // printf("%d %d :%d--%d\n",now/M, now%M, f[now][k],Dump[now][k]); + + int tmp = Dump[now][k]; + int pre = f[now - 1 - tmp][Pre[now][k]]; + if (pre < f[now][k]) + chos[now - 1 - tmp] = 1; + k = Pre[now][k]; + now -= 1 + tmp; + } + chos[0] = 1; + for (i = 0; i < N; i++) + for (j = 0; j < M; j++) + { + if (chos[i * M + j]) + printf("C"); + else + printf("%c", mat[i][j]); + if (j == M - 1) + printf("\n"); + } + printf("\n"); +} + +int main() +{ + int i; + int tcas = 1; + while (scanf("%d%d", &N, &M) != EOF) + { + if (N == 0 && M == 0) + break; + for (i = 0; i < N; i++) + scanf("%s", mat[i]); + for (i = 0; i <= N; i++) + mat[i][M] = '#'; + for (i = 0; i <= M; i++) + mat[N][i] = '#'; + mat[N - 1][M] = '.'; + printf("Case %d:\n", tcas++); + work(); + } + return 0; +} diff --git a/10940.cpp b/10940.cpp index fc29193..a5fd6db 100644 --- a/10940.cpp +++ b/10940.cpp @@ -1,28 +1,44 @@ -#include +#include -#define MAX 500000 -int Last[MAX + 1]; -void Cal() { - Last[0] = 0; - Last[1] = 1; - Last[2] = 2; - - for (int i = 3; i <= MAX; i++) { - if (i < Last[i - 1] + 2) - Last[i] = 2; - else - Last[i] = Last[i - 1] + 2; +using namespace std; + +/* +10935 +Thorowing cards away - 1 +*/ + +struct ss +{ + int rem; +}; +ss N[500002]; +void Gen() +{ + int i, j, d; + N[1].rem = 1; + N[2].rem = 2; + for (i = 3; i <= 500000; i++) + { + d = N[i - 1].rem + 2; + if (d > i) + { + d = 2; + } + N[i].rem = d; } } - -int main() { - Cal(); - - while (true) { - int N; - scanf("%d", &N); - if (!N) break; - - printf("%d\n", Last[N]); +void Print(int n) +{ + int i; + cout << N[n].rem << endl; +} +int main() +{ + Gen(); + int n; + while (cin >> n && n) + { + Print(n); } + return 0; } diff --git a/10941.cpp b/10941.cpp new file mode 100644 index 0000000..10929e9 --- /dev/null +++ b/10941.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +struct Node +{ + char suffix[2005]; + int len, count; +}; +vector Hash[2001]; +queue Q; +int n; +char words[1000][2000], x[2000], y[2000]; +int len[1000]; +int compare(char *a, char *b) +{ + int len; + for (len = 0; *a && *b && (*a++ == *b++); len++) + ; + return len; +} +int BFS() +{ + int l = compare(x, y), lx = strlen(x), ly = strlen(y); + Node u, v; + if (l == 0) + { + return -1; + } + if (l == lx && l == ly) + { + return 0; + } + if (l != lx) + { + strcpy(u.suffix, x + l); + } + if (l != ly) + { + strcpy(u.suffix, y + l); + } + u.len = strlen(u.suffix); + u.count = 0; + Q.push(u); + while (Q.size() > 0) + { + u = Q.front(); + Q.pop(); + for (int i = 0; i < n; i++) + { + int s = compare(u.suffix, words[i]); + if (s != len[i] && s != u.len) + { + continue; + } + if (s == len[i] && s == u.len) + { + return u.count + 1; + } + if (u.len > len[i]) + { + strcpy(v.suffix, u.suffix + s); + v.len = u.len - s; + } + else + { + strcpy(v.suffix, words[i] + s); + v.len = len[i] - s; + } + if (v.len == u.len && strcmp(v.suffix, u.suffix) == 0) + { + continue; + } + bool found = false; + for (int i = 0; i < Hash[v.len].size() && !found; i++) + if (strcmp(Hash[v.len][i].suffix, v.suffix) == 0) + { + found = true; + } + if (found) + { + continue; + } + Hash[v.len].push_back(v); + v.count = u.count + 1; + Q.push(v); + } + } + return -1; +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + for (int i = 0; i <= 2000; i++) + { + Hash[i].clear(); + } + scanf("%s %s", x, y); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s", words[i]); + len[i] = strlen(words[i]); + } + int ans = BFS(); + printf("%d\n", ans); + } +} diff --git a/10942.cpp b/10942.cpp new file mode 100644 index 0000000..47a0362 --- /dev/null +++ b/10942.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +int best; +void Test(int c, int m, int day, int y) +{ + bool leap = false; + if (c + y == 0) + { + leap = true; + } + c %= 4; + if (c == 0) + { + c = 4; + } + int t = c * 100 + y; + if ((t % 4 == 0 && t % 100 != 0) || t % 400 == 0) + { + leap = true; + } + if (m < 1 || m > 12) + { + return; + } + int bd; + if (m == 2) + { + bd = (leap) ? 29 : 28; + } + else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) + { + bd = 31; + } + else + { + bd = 30; + } + if (day < 1 || day > bd) + { + return; + } + t = 10000 * y + 100 * m + day; + if (t < best) + { + best = t; + } +} +int main() +{ + int t, c, x, y, z; + scanf("%d", &t); + while (t--) + { + best = 999999; + scanf("%d %d %d %d", &c, &x, &y, &z); + Test(c, x, y, z); + Test(c, x, z, y); + Test(c, y, x, z); + Test(c, y, z, x); + Test(c, z, x, y); + Test(c, z, y, x); + if (best == 999999) + { + puts("-1"); + } + else + { + printf("%02d %02d %02d\n", best / 10000, (best / 100) % 100, best % 100); + } + } +} diff --git a/10943.cpp b/10943.cpp index 30eafd7..9c6f235 100644 --- a/10943.cpp +++ b/10943.cpp @@ -1,40 +1,23 @@ -#include -#include +#include -const int MOD = 1000000; -int C[101][101]; -/* -int Cal(int N, int K) { - if (N < 0 || K < 0) return 0; - if (C[N][K] != -1) return C[N][K]; - - int V = 0, i; - for (i = 0; i <= N; i++) - V = (V + Cal(N - i, K - 1)) %MOD; - return C[N][K] = V; -} -*/ -void Calc() { - int i, j, k; - for (i = 0; i <= 100; i++) C[i][0] = 0; - for (j = 0; j <= 100; j++) C[0][j] = 1; - - for (i = 1; i <= 100; i++) { - for (j = 1; j <= 100; j++) { - C[i][j] = 0; - for (k = 0; k <= i; k++) - C[i][j] = (C[i][j] %MOD + C[k][j - 1] %MOD) %MOD; - } - } -} +using namespace std; -int main() { - Calc(); - while (true) { - int N, K; - scanf("%d%d", &N, &K); - if (!N && !K) break; - printf("%d\n", C[N][K]); +int main() +{ + long long C[201][201] = {0}; + int N, K; + C[0][0] = 1; + for (N = 1; N <= 200; N++) + { + C[N][0] = 1; + for (K = 1; K <= N; K++) + C[N][K] = (C[N - 1][K - 1] + C[N - 1][K]) % 1000000; + } + while (scanf("%d %d", &N, &K) == 2) + { + if (N == 0 && K == 0) + break; + printf("%lld\n", C[N + K - 1][K - 1]); } return 0; } diff --git a/10944.cpp b/10944.cpp new file mode 100644 index 0000000..48959c5 --- /dev/null +++ b/10944.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define maxn 20 +#define INF 2147483647 + +int R, C, st, total, mins, Cost[maxn][maxn], Low[maxn]; +char Color[maxn], str[30]; + +struct ss +{ + int x, y; +}; +vector V; + +void ReadCase() +{ + int i, j; + ss tmp; + total = 0; + for (i = 0; i < R; i++) + { + cin >> str; + tmp.x = i; + for (j = 0; str[j]; j++) + { + if (str[j] == 'L' || str[j] == '#') + { + tmp.y = j; + V.push_back(tmp); + if (str[j] == 'L') + st = total; + total++; + } + } + } +} + +int Dis(int x1, int y1, int x2, int y2) +{ + int d, t, c = 0; + d = abs(x1 - x2); + t = abs(y1 - y2); + if (d > t) + { + c += t; + c += (d - t); + } + else + { + c += d; + c += (t - d); + } + return c; +} + +void Set() +{ + int i, j; + for (i = 0; i < total; i++) + Low[i] = INF; + for (i = 0; i < V.size(); i++) + { + for (j = i + 1; j < V.size(); j++) + { + Cost[i][j] = Dis(V[i].x, V[i].y, V[j].x, V[j].y); + Cost[j][i] = Cost[i][j]; + if (Low[i] > Cost[i][j]) + Low[i] = Cost[i][j]; + if (Low[j] > Cost[i][j]) + Low[j] = Cost[i][j]; + } + } +} + +int Est() +{ + int i, c = 0; + for (i = 0; i < total; i++) + { + if (Color[i] == 0) + { + c += Low[i]; + } + } + return c; +} + +int Recur(int n, int level, int cost) +{ + int i; + if (cost >= mins) + return 0; + if (cost + Est() >= mins) + return 0; + if (level == total) + { + if (cost + Cost[n][0] < mins) + mins = cost + Cost[n][0]; + return 0; + } + if (cost + Cost[n][0] >= mins) + return 0; + Color[n] = 1; + for (i = 0; i < total; i++) + if (Color[i] == 0) + if (Recur(i, level + 1, cost + Cost[n][i])) + { + Color[n] = 0; + return 1; + } + Color[n] = 0; + return 0; +} + +void Cal() +{ + mins = INF; + Recur(0, 1, 0); + if (mins != INF) + cout << mins << endl; + else + cout << 0 << endl; + V.clear(); +} + +int main() +{ + while (cin >> R >> C) + { + ReadCase(); + Set(); + Cal(); + } + return 0; +} diff --git a/10945.cpp b/10945.cpp index 5c0ce5f..d41e57f 100644 --- a/10945.cpp +++ b/10945.cpp @@ -1,37 +1,39 @@ -#include -#include -using namespace std; -int main(){ - - for(;;){ - string str; - getline(cin,str); - if(str=="DONE") - break; - for(int i=0;i=0;i--) - s+=str[i]; - int cnt=0; - - for(int i=0;i + +using namespace std; + +bool is_palindrome(const string &s) +{ + string r(s.rbegin(), s.rend()); + return s == r; +} + +int main() +{ + string input; + string pal; + while (getline(cin, input)) + { + if (input == "DONE") + { + break; + } + pal = ""; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= 'A' && input[i] <= 'Z' || input[i] >= 'a' && input[i] <= 'z') + { + pal.push_back(toupper(input[i])); + } + } + if (is_palindrome(pal)) + { + cout << "You won't be eaten!" << endl; + } + else + { + cout << "Uh oh.." << endl; + } + } + return 0; +} diff --git a/10946.cpp b/10946.cpp index a1b06af..3288ff4 100644 --- a/10946.cpp +++ b/10946.cpp @@ -1,83 +1,76 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int R, C; -string mat[50]; +char a[50 + 3][50 + 3]; +int R, C, cnt; -int dI[] = {0, -1, 0, 1}; -int dJ[] = {-1, 0, 1, 0}; +struct node +{ + char ch; + int f; +} p[2600]; -bool comp(pair A, pair B){ - if( A.second != B.second ) - return A.second > B.second; - if( A.first != B.first ) - return A.first < B.first; - return false; +void dfs(int r, int c, char k) +{ + if (r < 0 || r == R || c < 0 || c == C || a[r][c] != k) + { + return; + } + cnt++; + a[r][c] = '.'; + dfs(r - 1, c, k); + dfs(r + 1, c, k); + dfs(r, c + 1, k); + dfs(r, c - 1, k); } -int floodFill(int I, int J, char ch){ - if( I < 0 || I >= R ) - return 0; - if( J < 0 || J >= C ) - return 0; - if( mat[I][J] != ch ) - return 0; - mat[I][J] = '.'; - int k, sum = 1; - FOI(k, 0, 3) - sum += floodFill(I + dI[k], J + dJ[k], ch); - return sum; +bool cmp(const node &x, const node &y) +{ + if (x.f > y.f) + { + return true; + } + if (x.f == y.f && x.ch < y.ch) + { + return true; + } + return false; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - cin >> R >> C; - if(R == 0 && C == 0) +int main() +{ + int t = 0; + while (scanf("%d%d", &R, &C)) + { + if (R == 0 && C == 0) + { break; - int i, j; - FOI(i, 0, R-1) - cin >> mat[i]; - vector< pair > vec; - FOI(i, 0, R-1) - FOI(j, 0, C-1) - if( isalpha( mat[i][j] ) ){ - char c = mat[i][j]; - int hole = floodFill(i, j, mat[i][j]); - vec.push_back( make_pair(c, hole) ); + } + for (int i = 0; i < R; i++) + { + scanf("%s", a[i]); + } + int len = 0; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + if (a[i][j] != '.') + { + cnt = 0; + p[len].ch = a[i][j]; + dfs(i, j, a[i][j]); + p[len++].f = cnt; } - sort(vec.begin(), vec.end(), comp); - cout << "Problem " << t << ":" << endl; - FOI(i, 0, vec.size()-1) - cout << vec[i].first << " " << vec[i].second << endl; + } + } + sort(p, p + len, cmp); + printf("Problem %d:\n", ++t); + for (int i = 0; i < len; i++) + { + printf("%c %d\n", p[i].ch, p[i].f); + } } return 0; } diff --git a/10947.cpp b/10947.cpp new file mode 100644 index 0000000..51ef979 --- /dev/null +++ b/10947.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +#define maxn 200 + +struct ss +{ + double x, y; + double r; +}; +vector V; +double K, M; +int N; +list links[maxn]; +queue Q; +char Fg[maxn]; + +double Dis(double x1, double y1, double x2, double y2) +{ + double X, Y; + X = x1 - x2; + X *= X; + Y = y1 - y2; + Y *= Y; + return sqrt(X + Y); +} + +void Setting() +{ + double d, m; + int i, j; + m = double(M); + for (i = 0; i < N + 2; i++) + { + for (j = i + 1; j < N + 2; j++) + { + d = Dis(V[i].x, V[i].y, V[j].x, V[j].y); + d = d - (V[i].r + V[j].r); + if (d <= 0) + { + links[i].push_back(j); + links[j].push_back(i); + } + if (m == 0) + continue; + if (d / m <= double(K)) + { + links[i].push_back(j); + links[j].push_back(i); + } + } + } +} + +int BFS() +{ + int u, v; + list::iterator p; + Q.push(0); + Fg[0] = 1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (p = links[u].begin(); p != links[u].end(); p++) + { + v = *p; + if (Fg[v]) + continue; + if (v == 1) + return 1; + Fg[v] = 1; + Q.push(v); + } + } + return 0; +} + +void Cal() +{ + Setting(); + if (BFS()) + cout << "Larry and Ryan will escape!\n"; + else + cout << "Larry and Ryan will be eaten to death.\n"; +} + +void Free() +{ + int i; + for (i = 0; i < N + 2; i++) + { + links[i].clear(); + Fg[i] = 0; + } + while (!Q.empty()) + Q.pop(); + V.clear(); +} + +int main() +{ + ss t; + int n; + while (cin >> K >> M) + { + cin >> t.x >> t.y >> t.r; + V.push_back(t); + cin >> t.x >> t.y >> t.r; + V.push_back(t); + cin >> N; + n = 0; + while (n++ < N) + { + cin >> t.x >> t.y >> t.r; + V.push_back(t); + } + Cal(); + Free(); + } + return 0; +} diff --git a/10948.cpp b/10948.cpp index 44345cc..27070cc 100644 --- a/10948.cpp +++ b/10948.cpp @@ -1,72 +1,62 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +using namespace std; -bool prime[MAX + 5]; -vector P; +bool primes[1000001]; -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; - int i, j; - FOI(i, 2, MAX) - if( prime[i] ){ - P.push_back(i); - for (j = 2*i; j <= MAX; j += i) - prime[j] = false; +void sieve() +{ + fill(primes, primes + 1000001, true); + for (int i = 2; i < 1002; i++) + { + for (int j = i * i; j < 1000001; j += i) + { + primes[j] = false; } + } + primes[0] = primes[1] = false; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int M = P.size(); - while( true ){ - int N, i; - cin >> N; - if( N == 0 ) +int main() +{ + int n; + sieve(); + while (cin >> n) + { + int last; + if (n == 0) + { break; - cout << N << ":" << endl; - bool gold = false; - FOI(i, 0, M-1){ - if( P[i] > N ) - break; - if( prime[N - P[i]] ){ - cout << P[i] << "+" << N - P[i] << endl; - gold = true; + } + if (n % 2 == 0) + { + last = n - 1; + } + else + { + last = n - 2; + } + cout << n << ":" << endl; + if (primes[n - 2]) + { + cout << 2 << "+" << n - 2 << endl; + continue; + } + bool found = false; + while (n - last <= last) + { + if (primes[last] && primes[n - last]) + { + cout << n - last << "+" << last << endl; + found = true; break; } + last -= 2; } - if( !gold ) + if (!found) + { cout << "NO WAY!" << endl; + } } return 0; } diff --git a/10949.cpp b/10949.cpp new file mode 100644 index 0000000..0ebc6d4 --- /dev/null +++ b/10949.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +char map_[30][30]; + +string trans(string &w, int x, int y) +{ + int N = w.size(); + string s; + s.resize(N + 1); + s[0] = map_[x][y]; + for (int i = 0; i < N; ++i) + { + switch (w[i]) + { + case 'E': + y++; + break; + case 'W': + y--; + break; + case 'N': + x--; + break; + case 'S': + x++; + break; + } + s[i + 1] = map_[x][y]; + } + return s; +} + +int LCS(string &s1, string &s2) +{ + int i; + vector alpha[128]; + int LCS[20005] = {0}; + int n1 = s1.size(), n2 = s2.size(), n = 0; + for (i = 0; i < n1; ++i) + { + alpha[s1[i]].push_back(i + 1); + } + for (i = 0; i < n2; ++i) + { + vector &c = alpha[s2[i]]; + if (c.size() == 0) + { + continue;// s1µL¦¹¦r + } + if (n == 0) + { + LCS[n++] = c[0]; + continue; + }// ³Ìªì°}¦C¬OªÅªº + int p = c.size() - 1; + if (c[p] > LCS[n - 1]) + { + LCS[n++] = c[p--];// ¤ñ§ÀºÝÁÙ¤j + } + int q = n - 1; + for (; p >= 0; --p)// ©¹¦^§ä + { + while (q >= 0 && c[p] <= LCS[q]) + { + q--; + } + LCS[q + 1] = c[p]; + } + } + return n; +} + +int main() +{ + int cs; + cin >> cs; + for (int cn = 1; cn <= cs; ++cn) + { + int X, Y; + cin >> X >> Y; + for (int i = 0; i < X; ++i) + for (int j = 0; j < Y; ++j) + { + cin >> map_[i][j]; + } + int n1, n2, x, y; + string s1, s2, buf; + cin >> n1 >> x >> y; + getline(cin, buf); + getline(cin, s1); + s1 = trans(s1, x - 1, y - 1); + cin >> n2 >> x >> y; + getline(cin, buf); + getline(cin, s2); + s2 = trans(s2, x - 1, y - 1); + int n = LCS(s1, s2); + cout << "Case " << cn << ": " << n1 - n + 1 << " " << n2 - n + 1 << endl; + } + return 0; +} diff --git a/1095.cpp b/1095.cpp new file mode 100644 index 0000000..ed9feb8 --- /dev/null +++ b/1095.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +#define MP make_pair +#define PI pair +#define FI first +#define SE second +#define PB push_back +#define SZ size() + +const double eps = 1e-10; +const double pi = acos(-1.); +const int mod = 1000000007; +const int maxn = 100100; +const int INF = 99999999; + +int h[200][200]; +int N, M, D, T; +double ans; + +inline double sqr(double x) +{ + return x * x; +} + +void cal(int a, int b, int c) +{ + if (a > b && a > c) + { + int x1 = a / T - b / T; + int x2 = a / T - c / T; + int x = min(x1, x2); + double t = D * sqrt(1. / sqr(a - b) + 1. / sqr(a - c) - 1. / (a - b) / (a - c)); + double lv = t * (a - a / T * T); + double rv = t * (a - a / T * T + (x - 1) * T); + double add = (lv + rv) * x / 2; + ans += add; + } + if (a < b && a < c) + { + int x1 = b / T - (a - 1) / T; + int x2 = c / T - (a - 1) / T; + int x = min(x1, x2); + double t = D * sqrt(1. / sqr(a - b) + 1. / sqr(a - c) - 1. / (a - b) / (a - c)); + double lv = t * ((a - 1) / T * T + 1 * T - a); + double rv = t * ((a - 1) / T * T + x * T - a); + double add = (lv + rv) * x / 2; + ans += add; + } +} + +void gao(int a, int b, int c) +{ + cal(a, b, c); + cal(b, a, c); + cal(c, a, b); + if (a == b && a % T == 0 && c >= a) + { + ans += D; + } + if (a == c && a % T == 0 && b >= a) + { + ans += D; + } + if (b == c && b % T == 0 && a >= b) + { + ans += D; + } +} + +int main() +{ + int cases = 1; + int i, j, k; + int N, M; + while (scanf("%d", &N), N) + { + scanf("%d %d %d", &M, &D, &T); + ans = 0; + memset(h, -1, sizeof(h)); + for (i = 0; i < N; ++i) + for (j = 0; j < M + (i & 1); ++j) + { + scanf("%d", &h[i][j]); + } + for (i = 0; i < N; ++i) + { + if ((i & 1) == 0) + { + for (j = 0; j < M; ++j) + { + if (i + 1 < N) + { + gao(h[i][j], h[i + 1][j], h[i + 1][j + 1]); + } + if (i > 0) + { + gao(h[i][j], h[i - 1][j], h[i - 1][j + 1]); + } + } + } + else + { + for (j = 1; j < M; ++j) + { + if (i + 1 < N) + { + gao(h[i][j], h[i + 1][j - 1], h[i + 1][j]); + } + if (i > 0) + { + gao(h[i][j], h[i - 1][j - 1], h[i - 1][j]); + } + } + } + } + for (i = 1; i < N; ++i) + { + if ((i & 1) == 0) + { + for (j = 0; j < M; ++j) + if (h[i][j] % T == 0) + { + if (j + 1 < M && h[i][j] == h[i][j + 1] && i + 1 < N) + { + if (h[i - 1][j + 1] == h[i][j] && h[i + 1][j + 1] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + if (h[i][j] == h[i - 1][j] && j > 0) + { + if (h[i][j - 1] == h[i][j] && h[i - 1][j + 1] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + if (h[i][j] == h[i - 1][j + 1] && j + 1 < M) + { + if (h[i][j + 1] == h[i][j] && h[i - 1][j] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + } + } + else + { + for (j = 0; j < M; ++j) + if (h[i][j] % T == 0) + { + if (h[i][j] == h[i][j + 1] && i + 1 < N) + { + if (h[i - 1][j] == h[i][j] && h[i + 1][j] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + if (j > 0 && h[i - 1][j - 1] == h[i][j]) + { + if (h[i][j - 1] == h[i][j] && h[i - 1][j] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + if (j > 0 && h[i - 1][j] == h[i][j]) + { + if (h[i][j + 1] == h[i][j] && h[i - 1][j - 1] == h[i][j]) + { + ans -= 2 * D; + } + else + { + ans -= D; + } + } + } + } + } + printf("Case %d: %.0lf\n", cases++, ans); + } + return 0; +} diff --git a/10950.cpp b/10950.cpp new file mode 100644 index 0000000..f404e34 --- /dev/null +++ b/10950.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +char En[202]; +int N, ts, len; + +struct ss +{ + char ch[10]; + char val[5]; + int len; +}; +ss Link[30]; + +int Com(const void *a, const void *b) +{ + ss *x; + ss *y; + x = (ss *)a; + y = (ss *)b; + return strcmp(x->ch, y->ch); +} + +int Match(int len, char pat[], int ind) +{ + int i, j, f = 1, p; + for (i = ind; En[i]; i++) + { + if (f == 0 && En[i - 1] != '0') + return -1; + f = 1; + p = i; + for (j = 0; pat[j] && En[p]; j++) + if (En[p++] != pat[j]) + { + f = 0; + break; + } + if (f && j == len) + return i + len; + } + return -1; +} + +int Recur(string en, int ind) +{ + int i, d, k, j, f, c; + string t; + if (ind == len) + { + ts++; + cout << en.c_str() << endl; + if (ts == 100) + return 1; + return 0; + } + for (j = 0; j < N; j++) + { + d = Match(Link[j].len, Link[j].val, ind); + if (d >= 0) + { + c = Recur(en + Link[j].ch, d); + if (c) + return 1; + } + } + return 0; +} + +void Cal() +{ + ts = 0; + qsort(Link, N, sizeof(ss), Com); + len = strlen(En); + Recur("", 0); +} + +int main() +{ + int d = 1, k; + while (cin >> N && N) + { + if (!N) + break; + k = 0; + while (k < N) + { + cin >> Link[k].ch >> Link[k].val; + Link[k].len = strlen(Link[k].val); + k++; + } + cin >> En; + cout << "Case #" << d++ << "\n"; + Cal(); + cout << endl; + } + return 0; +} diff --git a/10951.cpp b/10951.cpp new file mode 100644 index 0000000..11c6613 --- /dev/null +++ b/10951.cpp @@ -0,0 +1,176 @@ +#include + +using namespace std; + +typedef struct +{ + int n, a[128]; +} poly_t; + +int inv[2048], m; + +void copy(poly_t *r, poly_t *s) +{ + int i; + for (r->n = s->n, i = 0; i <= s->n; i++) + r->a[i] = s->a[i]; +} + +void reduce(poly_t *p) +{ + int i; + for (i = p->n; i >= 0; i--) + p->a[i] %= m; + while (p->n > 0 && p->a[p->n] == 0) + p->n--; +} + +void prn(poly_t *p) +{ + int i; + for (i = p->n; i > 0; i--) + if (p->a[i]) + printf("%s%d x^%d", (i == p->n) ? "" : " + ", p->a[i], i); + printf("%s%d", (p->n == 0) ? "" : " + ", p->a[0]); +} + +void divide(poly_t *Q, poly_t *A, poly_t *B) +{ + int i, j, k, d; + + if (A->n < B->n) + { + if (Q) + Q->n = 0; + return; + } + + i = A->n - B->n; + if (Q) + Q->n = i; + + for (; i >= 0; i--) + { + d = (A->a[B->n + i] * inv[B->a[B->n]]) % m; + if (Q) + Q->a[i] = d; + if (d == 0) + continue; + + for (j = B->n + i, k = B->n; j >= 0 && k >= 0; j--, k--) + A->a[j] = (A->a[j] - ((d * B->a[k]) % m) + m) % m; + } + + if (Q) + reduce(Q); + reduce(A); +} + +void getpoly(poly_t *p) +{ + int i; + + memset(p, 0, sizeof(poly_t)); + + scanf("%d", &p->n); + for (i = p->n; i >= 0; i--) + scanf("%d", &p->a[i]); + reduce(p); +} + +void gcdext(int a, int b, int *x, int *y, int *d) +{ + if (a < 0) + { + gcdext(-a, b, x, y, d); + *x = -(*x); + } + else if (b < 0) + { + gcdext(a, -b, x, y, d); + *y = -(*y); + } + else if (b == 0) + { + *x = 1; + *y = 0; + *d = a; + } + else + { + gcdext(b, a % b, y, x, d); + *y -= (a / b) * (*x); + } +} + +int modinv(int a, int m) +{ + int x, y, d; + + gcdext(a, m, &x, &y, &d); + if (d != 1) + return -1; + + return (x < 0) ? (m - (-x) % m) : (x % m); +} + +poly_t *polygcd(poly_t *A, poly_t *B) +{ + poly_t *T; + + for (;;) + { + if (A->n < B->n) + { + T = A; + A = B; + B = T; + } + if (B->n == 0) + { + if (B->a[0] != 0) + { + A->n = 0; + A->a[0] = 1; + } + return A; + } + divide(NULL, A, B); + } +} + +int main() +{ + int i, t; + poly_t A, B, *G; + + for (t = 1; scanf("%d", &m) == 1 && m > 0; t++) + { + assert(m < 2048); + + inv[0] = 0; + for (i = 1; i < m; i++) + inv[i] = modinv(i, m); + + getpoly(&A); + getpoly(&B); + + G = polygcd(&A, &B); + + reduce(G); + + if (G->a[G->n] != 0) + { + for (i = 0; i <= G->n; i++) + G->a[i] = (G->a[i] * inv[G->a[G->n]]) % m; + } + reduce(G); + + printf("Case %d: %d", t, G->n); + for (i = G->n; i >= 0; i--) + printf(" %d", G->a[i]); + printf("\n"); + } + + return 0; +} diff --git a/10952.cpp b/10952.cpp new file mode 100644 index 0000000..da04e05 --- /dev/null +++ b/10952.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define INF 0x00FFFFFF + +int src[16][16], dst[16][16], height, width; +int tab[8][256][256], got[8][256][256], id; + +int min(int x, int y) +{ + return (x < y) ? x : y; +} + +int f(int, int, int); + +int g(int row, int pdown, int pvac, int col, int ndown, int nvac) +{ +#define G(A, B, C) g(row, pdown, pvac, A, B, C) + int r; +re: + r = INF; + if (col == width) + { + if (pvac != ((1 << width) - 1)) + { + return INF; + } + for (r = 0; r < width; r++) + if (dst[row][r] == '.' && ((nvac >> r) & 1) == 0) + { + nvac |= 1 << r; + } + return f(row + 1, ndown, nvac); + } + if (src[row][col] == '.') + { + if ((pdown >> col) & 1) + { + if (nvac & (1 << col)) + { + return INF; + } + nvac |= 1 << col; + } + if (((pvac >> col) & 1) == 0) + { + return INF; + } + if (dst[row][col] == '.') + { + nvac |= 1 << col; + } + col++; + goto re; + } + if ((pdown >> col) & 1) + { + if (nvac & (1 << col)) + { + return INF; + } + nvac |= (1 << col); + } + if (((pvac >> col) & 1) == 0) + { + /* must move up */ + if (dst[row - 1][col] != src[row][col]) + { + return INF; + } + if ((pdown >> col) & 1) + { + return 1 + g(row, pdown, pvac | (1 << col), col + 1, ndown, nvac); + } + else + { + pvac |= 1 << col; + col++; + goto re; + } + } + if (src[row][col] == dst[row][col] && ((pdown >> col) & 1) == 0 && + ((nvac >> col) & 1) == 0) + { + /* remain */ + r = min(r, G(col + 1, ndown, nvac | (1 << col))); + } + /* left */ + if (col > 0 && dst[row][col - 1] == src[row][col] && + ((nvac >> (col - 1)) & 1) == 0) + { + if (((pdown >> col) & 1) == 0 && ((nvac >> col) & 1)) + { + r = min(r, 1 + G(col + 1, ndown, nvac | (1 << (col - 1)))); + } + else + { + r = min(r, G(col + 1, ndown, nvac | (1 << (col - 1)))); + } + } + /* right */ + if (dst[row][col + 1] == src[row][col]) + { + r = min(r, G(col + 1, ndown, nvac | (1 << (col + 1)))); + } + /* down */ + if (dst[row + 1][col] == src[row][col]) + { + r = min(r, G(col + 1, ndown | (1 << col), nvac)); + } + return r; +} + +int f(int row, int pdown, int pvac) +{ + if (row == height) + { + return (pdown == 0 && pvac == ((1 << width) - 1)) ? 0 : INF; + } + if (got[row][pdown][pvac] == id) + { + return tab[row][pdown][pvac]; + } + got[row][pdown][pvac] = id; + return (tab[row][pdown][pvac] = g(row, pdown, pvac, 0, 0, 0)); +} + +int main() +{ + int i, j, c, t; + scanf("%d", &t); + memset(got, 0, sizeof(got)); + for (id = 1; id <= t; id++) + { + i = scanf("%d %d", &width, &height); + assert(i == 2); + assert(1 <= width && width <= 8 && 1 <= height && height <= 8); + memset(src, 0, sizeof(src)); + memset(dst, 0, sizeof(dst)); + for (i = 0; i < height; i++) + { + for (j = 0; j < width && (c = getchar()) != EOF;) + if (strchr("XOxo.", c) != NULL) + { + src[i][j++] = toupper(c); + } + assert(j == width); + for (j = 0; j < width && (c = getchar()) != EOF;) + if (strchr("XOxo.", c) != NULL) + { + dst[i][j++] = toupper(c); + } + assert(j == width); + } + i = f(0, 0, (1 << width) - 1); + if (i < INF) + { + assert(0 <= i && i <= 32); + } + printf("%d\n", (i < INF) ? i : -1); + } + return 0; +} diff --git a/10953.cpp b/10953.cpp new file mode 100644 index 0000000..a987a3a --- /dev/null +++ b/10953.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +double dp[50][11]; + +double solve(int n, int r, int m, int *d, double *p, string &in) +{ + rep(i, n) + { + rep(j, 11) dp[i][j] = 0; + } + if (in[0] != '*') + { + dp[0][in[0] - '0'] = 1; + } + else + { + rep(k, m) + { + dp[0][d[k]] += p[k]; + } + } + rep(i, (int)in.size() - 1) + { + rep(j, 11) + { + if (in[i + 1] != '*') + { + dp[i + 1][((j * 10) + in[i + 1] - '0') % 11] += dp[i][j]; + continue; + } + rep(k, m) + { + dp[i + 1][((j * 10) + d[k]) % 11] += p[k] * dp[i][j]; + } + } + } + return dp[n - 1][r]; +} + +main() +{ + int n, q; + int tc = 1; + while (cin >> n && n) + { + printf("Case %d:\n", tc++); + int d[n]; + double p[n]; + rep(i, n) + { + cin >> d[i] >> p[i]; + } + cin >> q; + rep(i, q) + { + string in; + int r; + cin >> in >> r; + printf("%.8lf\n", solve(in.size(), r, n, d, p, in)); + } + } +} diff --git a/10954.cpp b/10954.cpp index 5679bf1..7b22830 100644 --- a/10954.cpp +++ b/10954.cpp @@ -1,66 +1,38 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -struct compare { - bool operator()(const int& l, const int& r) { - return l > r; - } -}; +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int64 N; - scanf("%lld", &N); - if (N == 0) +int main() +{ + int n, tmp; + int a, b; + while (cin >> n) + { + if (!n) + { break; - priority_queue , compare> pq; - int64 i, T = 0, I; - FOI(i, 1, N){ - scanf("%lld", &I); - pq.push(I); } - while (pq.size() > 1){ - int64 V = 0; - V += pq.top(); pq.pop(); - V += pq.top(); pq.pop(); - T += V; - pq.push(V); + priority_queue, greater> q; + for (int i = 0; i < n; i++) + { + cin >> tmp; + q.push(tmp); + } + int result = 0; + while (true) + { + a = q.top(); + q.pop(); + if (q.empty()) + { + break; + } + b = q.top(); + q.pop(); + result += a + b; + q.push(a + b); } - printf("%lld\n", T); + cout << result << endl; } return 0; } - diff --git a/10956.cpp b/10956.cpp new file mode 100644 index 0000000..20bf60f --- /dev/null +++ b/10956.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +typedef unsigned long long ULL; + +static unsigned int base, min_, max_, max_2, r_non, r_fails, r_list[50001]; + +static unsigned int modexp(unsigned int b, unsigned int e, unsigned int m) +{ + unsigned int r; + for (r = 1; e > 1; e >>= 1) + { + if (e & 1) + { + r = (unsigned int)(((ULL)r * (ULL)b) % m); + } + b = (unsigned int)(((ULL)b * (ULL)b) % m); + } + r = (unsigned int)(((ULL)r * (ULL)b) % m); + return r; +} + +static int check(unsigned int b, unsigned int n) +{ + unsigned int t, u, x; + for (u = (n - 1) >> 1, t = 1; (u & 1) == 0; u >>= 1, t++) + ; + x = modexp(b, u, n); + if (x == 1 || x == (n - 1)) + { + return 1; + } + while (t--) + { + u = ((ULL)x * (ULL)x) % n; + if (u == 1) + { + return (x == (n - 1)) ? 1 : 0; + } + x = u; + } + return x == 1; +} + +static void solve() +{ + unsigned int x; + r_non = 0; + r_fails = 0; + max_2 = max_; + if (max_2 > 0 && (max_2 & 1) == 0) + { + max_2--; + } + if (min_ > max_2) + { + return; + } + x = min_; + if ((x & 1) == 0) + { + if (min_ == max_2) + { + return; + } + x++; + } + if (x < 3) + { + x = 3; + } + for (;;) + { + if (check(base, x)) + { + if (check(2, x) && check(7, x) && check(61, x)) + { + ; + } + else + { + r_non++; + r_list[r_fails++] = x; + } + } + else + { + r_non++; + } + if (x == max_2) + { + break; + } + x += 2; + } +} + +int main() +{ + int i, t; + for (t = 0; scanf("%u%u%u", &base, &min_, &max_) == 3 && base >= 2; t++) + { + if (t) + { + printf("\n"); + } + solve(); + printf("There are %u odd non-prime numbers between %u and %u.\n", + r_non, min_, max_); + if (r_fails == 0) + { + printf("There are no failures in base %u.\n", base); + } + else + { + printf("%u suspects fail in base %u:\n", r_fails, base); + for (i = 0; i < r_fails; i++) + { + printf("%u\n", r_list[i]); + } + } + } + return 0; +} diff --git a/10957.cpp b/10957.cpp new file mode 100644 index 0000000..e760634 --- /dev/null +++ b/10957.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +int Illegal, Req, B[10][10], Col[9][10], Row[9][10], Sub[3][3][10]; + +void ReadCase() +{ + int i, j, n, sr, sc, pos; + Illegal = Req = 0; + for (i = 0; i < 9; i++) + { + sr = i / 3; + for (j = 0; j < 9; j++) + { + if (i == 0 && j == 0) + continue; + cin >> n; + B[i][j] = n; + if (!n) + { + Req++; + continue; + } + sc = j / 3; + if (Row[i][n] || Col[j][n] || Sub[sr][sc][n]) + Illegal = 1; + Row[i][n] = 1; + Col[j][n] = 1; + Sub[sr][sc][n] = 1; + } + } +} + +int Recur(int r, int c, int n, int level) +{ + int sr, sc, i, j, f = 0, k, count = 0, g = 0; + if (Row[r][n] || Col[c][n]) + return 0; + sr = r / 3; + sc = c / 3; + if (Sub[sr][sc][n]) + return 0; + if (level == Req) + return 1; + Row[r][n] = Col[c][n] = 1; + Sub[sr][sc][n] = 1; + B[r][c] = n; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + if (B[i][j] == 0) + { + f = 1; + break; + } + if (f) + break; + } + for (k = 1; k <= 9; k++) + { + count += Recur(i, j, k, level + 1); + if (count > 1) + break; + } + B[r][c] = 0; + Sub[sr][sc][n] = 0; + Col[c][n] = Row[r][n] = 0; + return count; +} + +void Cal() +{ + int i, j, sr, sc, f = 0, count = 0; + if (Illegal) + { + cout << "Illegal.\n"; + return; + } + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + if (B[i][j] == 0) + { + f = 1; + break; + } + if (f) + break; + } + for (int k = 1; k <= 9; k++) + { + count += Recur(i, j, k, 1); + if (count > 1) + break; + } + if (count == 0) + cout << "Impossible.\n"; + else if (count == 1) + cout << "Unique.\n"; + else + cout << "Ambiguous.\n"; +} + +void Free() +{ + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) + Row[i][j] = Col[i][j] = 0; + + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + for (int k = 0; k < 10; k++) + Sub[i][j][k] = 0; +} + +int main() +{ + int n, k = 1; + while (cin >> n) + { + B[0][0] = n; + Row[0][n] = 1; + Col[0][n] = 1; + Sub[0][0][n] = 1; + ReadCase(); + cout << "Case " << k++ << ": "; + Cal(); + Free(); + } + return 0; +} diff --git a/10958.cpp b/10958.cpp new file mode 100644 index 0000000..f028352 --- /dev/null +++ b/10958.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +/* +10958 +How Many Sol +*/ +#define maxn 31630 +//typedef __int64 ss; +typedef long long ss; +char sv[maxn + 2]; +ss P[3403], tp; +void Prime() +{ + ss i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + tp = 1; + P[0] = 2; + for (i = 3; i < maxn; i += 2) + { + if (sv[i] == 0) + { + P[tp++] = i; + } + } +} +ss Div(ss n) +{ + ss i, j, c = 1; + if (n < 0) + { + n *= -1; + } + for (i = 0; i < tp && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + j = 0; + while (n % P[i] == 0) + { + j++; + n /= P[i]; + } + j++; + c *= j; + } + } + if (n > 1) + { + c *= 2; + } + return c * 2 - 1; +} +int main() +{ + ss m, n, p, k = 1; + Prime(); + while (scanf("%lld%lld%lld", &m, &n, &p) == 3) + { + if (!n && !m && !p) + { + break; + } + printf("Case %lld: %lld\n", k++, Div(n * m * p * p)); + } + return 0; +} diff --git a/10959.cpp b/10959.cpp new file mode 100644 index 0000000..13a9e09 --- /dev/null +++ b/10959.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + void solve(); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +map result; + +void Graph::solve() +{ + Vertex *v; + pair tmp; + queue> q; + map visited; + tmp.first = 0; + tmp.second = 0; + q.push(tmp); + visited[0] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + result[tmp.first] = tmp.second; + v = getVertex(tmp.first); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + if (visited.find(v->adj[i]->id) != visited.end()) + { + continue; + } + visited[v->adj[i]->id] = true; + q.push(make_pair(v->adj[i]->id, tmp.second + 1)); + } + } +} + +int main() +{ + int cases; + int a, b; + int p, d; + cin >> cases; + while (cases--) + { + Graph g; + result.clear(); + cin >> p >> d; + while (d--) + { + cin >> a >> b; + g.addEdge(a, b); + } + g.solve(); + for (int i = 1; i < p; i++) + { + cout << result[i] << endl; + } + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/10960.cpp b/10960.cpp new file mode 100644 index 0000000..28679b4 --- /dev/null +++ b/10960.cpp @@ -0,0 +1,288 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = 0; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 26 +#define COLOR 10 +#define ITEM 10 + +#define NOT_DECIDE -1 + +class Info +{ +public: + int x, y; + vector data; +}; + +bool same_color[ITEM][N][N]; +int decide[N][ITEM]; +bool possible[N][ITEM][COLOR]; + +void reset(int n, int item, int color) +{ + rep(i, item) + { + rep(j, n) + { + rep(k, n) + { + same_color[i][j][k] = false; + } + same_color[i][j][j] = true; + } + } + rep(i, n) + { + rep(j, item) decide[i][j] = NOT_DECIDE; + } + rep(i, n) + { + rep(j, item) + { + rep(k, color) + { + possible[i][j][k] = true; + } + } + } +} + +void tc(int n, int item) +{ + rep(l, item) + rep(k, n) + rep(i, n) + rep(j, n) same_color[l][i][j] = (same_color[l][i][j] | (same_color[l][i][k] & same_color[l][k][j])); +} + +bool item_x_is_y(Info &in) +{ + int item = in.x, color = in.y; + rep(i, in.data.size()) + { + int now = in.data[i]; + if (decide[now][item] == NOT_DECIDE || decide[now][item] == color) + { + decide[now][item] = color; + } + else + { + return false; + } + } + return true; +} + +bool item_x_is_not_y(Info &in) +{ + int item = in.x, color = in.y; + rep(i, in.data.size()) + { + int now = in.data[i]; + if (decide[now][item] == NOT_DECIDE || decide[now][item] != color) + { + possible[now][item][color] = false; + } + else + { + return false; + } + } + return true; +} + +bool is_same_color(int n, int me, int item, int numcolor) +{ + //decide color check + int color = decide[me][item]; + rep(i, n) + { + if (same_color[item][me][i] && decide[i][item] != NOT_DECIDE) + { + if (color == NOT_DECIDE) + { + color = decide[i][item]; + } + else if (color != decide[i][item]) + { + return false;//two colors + } + } + } + int tmp = color, cnt = color == NOT_DECIDE ? 0 : 1; + if (color == NOT_DECIDE) + { + //possible color check + bool can[numcolor]; + rep(j, numcolor) + { + can[j] = true; + rep(i, n) + { + if (same_color[item][me][i]) + { + can[j] &= possible[i][item][j]; + } + } + if (can[j]) + { + tmp = j, cnt++; + } + } + } + //if possible color is one then decide. + if (cnt == 1) + { + rep(i, n) + { + if (same_color[item][me][i] == true) + { + decide[i][item] = tmp; + } + } + } + else if (cnt == 0) + { + return false;//all person doesn't have same color + } + return true; +} + +void solve(int n, int item, int color, vector &xisy, vector &xisny, + bool *is_here) +{ + tc(n, item); + rep(i, xisy.size()) + { + if (item_x_is_y(xisy[i]) == false) + { + puts("Contradiction"); + return; + } + } + //cout << "item x is y end" << endl; + rep(i, xisny.size()) + { + if (item_x_is_not_y(xisny[i]) == false) + { + puts("Contradiction"); + return; + } + } + //cout << "item x is not y end " << endl; + /* + rep(i,n){ + if ( is_here[i]==true){ + rep(j,item){ + cout << "person " << i << " item " << j <<" "; + rep(k,color) + if( possible[i][j][k])cout << k; + cout << endl; + } + } + } + */ + rep(i, n) + { + rep(j, item) + { + if (is_same_color(n, i, j, color) == false) + { + puts("Contradiction"); + return; + } + } + } + //cout << "same color item x is end "< *a) +{ + int index; + Info in; + stringstream sin(tmp); + string dummy; + if (tmp[0] == 'S') + { + sin >> dummy >> dummy >> dummy >> dummy >> in.x >> dummy; + index = 0; + } + else if (tmp[0] == 'C') + { + sin >> dummy >> in.y >> dummy >> dummy >> in.x >> dummy; + index = 1; + } + else if (tmp[0] == 'N') + { + sin >> dummy >> dummy >> in.y >> dummy >> dummy >> in.x >> dummy; + index = 2; + } + char member; + while (sin >> member) + { + in.data.push_back(member - 'A'); + is_here[member - 'A'] = true; + } + a[index].push_back(in); +} + +main() +{ + int te, tc = 0; + cin >> te; + while (te--) + { + if (tc) + { + puts(""); + } + tc = 1; + int n, item, color, query; + bool is_here[26] = {false}; + cin >> n >> item >> color >> query; + reset(n, item, color); + string tmp; + getline(cin, tmp); + vector in[3]; + rep(i, query) + { + getline(cin, tmp); + get_input(tmp, is_here, in); + } + rep(i, in[0].size()) + { + int now = in[0][i].x; + rep(j, in[0][i].data.size()) + { + rep(k, in[0][i].data.size()) + { + same_color[now][in[0][i].data[j]][in[0][i].data[k]] = true; + } + } + } + solve(26, item, color, in[1], in[2], is_here); + } + return false; +} diff --git a/10961.cpp b/10961.cpp new file mode 100644 index 0000000..290f376 --- /dev/null +++ b/10961.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +// DJWS's code :p + +struct Point +{ + int x, y; + + Point(int x = 0, int y = 0) : x(x), y(y) {} + + friend istream &operator>>(istream &in, Point &p) + { + return in >> p.x >> p.y; + } + + friend Point makeDir(const Point &p1, const Point &p2) + { + if (p1.x == p2.x) + if (p1.y < p2.y) + { + return Point(0, 1); + } + else + { + return Point(0, -1); + } + else if (p1.x < p2.x) + { + return Point(1, 0); + } + else + { + return Point(-1, 0); + } + } + + bool operator==(const Point &p) const + { + return x == p.x && y == p.y; + } + + Point operator+(const Point &p) const + { + return Point(x + p.x, y + p.y); + } + + void operator+=(const Point &p) + { + x += p.x, y += p.y; + } + + void operator=(const Point &p) + { + x = p.x, y = p.y; + } +}; + +Point aS[101], bS[101]; + +int main() +{ + int cases; + cin >> cases; + while (cases--) + { + int i; + cin >> bS[0] >> aS[0]; + int aN, bN; + cin >> aN; + for (i = 1; i <= aN; ++i) + { + cin >> aS[i]; + } + cin >> bN; + for (i = 1; i <= bN; ++i) + { + cin >> bS[i]; + } + bool ans = true; + int aI = 0, bI = 0; + Point a = aS[0], b = bS[0], dira, dirb; + while (true) + { + if (a == aS[aI]) + { + if (aI == aN) + { + break; + } + else + { + dira = makeDir(aS[aI], aS[aI + 1]), aI++; + } + } + if (b == bS[bI]) + { + if (bI == bN) + { + break; + } + else + { + dirb = makeDir(bS[bI], bS[bI + 1]), bI++; + } + } + if (a == b) + { + ans = false; + break; + } + // if ((a + dira == b) && (b + dirb == a)) { + // ans = false; + // break; + // } + a += dira; + b += dirb; + } + if (ans) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/10962.cpp b/10962.cpp new file mode 100644 index 0000000..e7b114d --- /dev/null +++ b/10962.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = 0; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +#define DON 0 +#define SERVER 1 +#define QUERY 2 + +const double eps = 1e-10; +const double INF = 1e100; +void solve(queue *Q, double *ser) +{ + int p = 0; + while (!Q[QUERY].empty()) + { + int minindex = -1; + double t = INF; + //find minimum event + rep(i, 3) + { + //if (!Q[i].empty())cout << i <<" " << Q[i].front()<<" " <> te; + while (te--) + { + if (tc) + { + puts(""); + } + tc++; + double r[2]; + int n, q; + cin >> r[DON] >> r[SERVER] >> n >> q; + double ser[n]; + queue Q[3]; + rep(i, n) + { + double s, g; + cin >> s >> g; + Q[DON].push(g / r[DON]); + ser[i] = (s - g) / r[SERVER] + eps; + } + double last = 0; + rep(i, q) + { + double tmp; + cin >> tmp; + Q[QUERY].push(tmp - last); + last = tmp; + } + solve(Q, ser); + } + return false; +} diff --git a/10963.cpp b/10963.cpp index 0ca51a1..40d11ec 100644 --- a/10963.cpp +++ b/10963.cpp @@ -1,27 +1,37 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i>n; - for(int j=0;j>y1>>y2; - dis[j]=(y1-y2); - } - for(int j=0;j + +using namespace std; + +int main() +{ + int n, w, y1, y2, t; + cin >> n; + for (int i = 1; i <= n; i++) + { + cin >> w; + bool c = 1; + cin >> y1 >> y2; + t = y2 - y1; + for (int j = 2; j <= w; j++) + { + cin >> y1 >> y2; + if (t != y2 - y1) + { + c = 0; + } + } + if (c == 1) + { + cout << "yes\n"; + } + else + { + cout << "no\n"; + } + if (i != n) + { + cout << '\n'; + } + } + return 0; +} diff --git a/10964.cpp b/10964.cpp new file mode 100644 index 0000000..27ffcd3 --- /dev/null +++ b/10964.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +int M[30000], tp; +struct ss +{ + int x, y; +}; +void Gen() +{ + int i, j, k = 0; + for (i = 0, j = 0; k < 1000000000; i += 4, j++) + { + k += i; + M[j] = k; + } + tp = j; +} +int BS(int key) +{ + int lo = 0, up = tp - 1; + int mid = (lo + up) / 2; + while (1) + { + if (M[mid] > key) + { + if (M[mid - 1] < key) + { + return mid; + } + if (M[mid - 1] == key) + { + return mid - 1; + } + up = mid - 1; + } + else if (M[mid] < key) + { + if (M[mid + 1] >= key) + { + return mid + 1; + } + lo = mid + 1; + } + else + { + return mid; + } + mid = (lo + up) / 2; + } + return -1; +} +ss Find(int key) +{ + ss temp; + int pos, a, b, d, t; + pos = BS(key); + a = M[pos]; + b = a - pos; + temp.x = -pos; + temp.y = 0; + if (key <= a && key >= b) + { + d = a - key; + temp.x += d; + temp.y -= d; + return temp; + } + a = b; + b = b - pos; + temp.x += pos; + temp.y -= pos; + if (key <= a && key >= b) + { + d = a - key; + temp.x += d; + temp.y += d; + return temp; + } + a = b; + b = a - pos; + temp.x += pos; + temp.y += pos; + if (key <= a && key >= b) + { + d = a - key; + temp.x -= d; + temp.y += d; + return temp; + } + a = b; + b = a - pos + 1; + temp.x -= pos; + temp.y += pos; + if (key <= a && key >= b) + { + d = a - key; + temp.x -= d; + temp.y -= d; + return temp; + } +} +double Dis(int x1, int y1, int x2, int y2) +{ + double dis; + x1 = x1 - x2; + x1 *= x1; + y1 = y1 - y2; + y1 *= y1; + return sqrt(double(x1 + y1)); +} +void Cal(int n, int m) +{ + ss t, u; + t = Find(n); + u = Find(m); + printf("%.2lf\n", Dis(t.x, t.y, u.x, u.y)); +} +int main() +{ + int n, m; + Gen(); + while (scanf("%d%d", &n, &m) == 2) + { + if (n < 0 && m < 0) + { + break; + } + Cal(n, m); + } + return 0; +} diff --git a/10965.cpp b/10965.cpp new file mode 100644 index 0000000..4716876 --- /dev/null +++ b/10965.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define maxn 102 + +char G[maxn][maxn]; +int R, C; + +struct ss +{ + int x, y; + int dur, dir; + int cov; +}; +ss K[1001]; + +void Cover(int x, int y, int cover, int dir) +{ + int i; + if (dir == 0) + for (i = x; i >= x - cover && i > 0; i--) + G[i][y] = 1; + else if (dir == 1) + for (i = y; i <= y + cover && i <= C; i++) + G[x][i] = 1; + else if (dir == 2) + for (i = x; i <= x + cover && i <= R; i++) + G[i][y] = 1; + else + for (i = y; i >= y - cover && i > 0; i--) + G[x][i] = 1; +} + +void Fill(int x, int y, int st, int dur, int lim1, int lim2, int diff, int dir, int cov) +{ + int d, k, p, q, end; + end = st + dur - 1; + if (st > lim2) + return; + if (st >= lim1 && st <= lim2) + { + Cover(x, y, cov, dir); + return; + } + d = lim1 - st; + k = d % diff; + p = d - k + st; + q = p + dur - 1; + if (q >= lim1) + { + Cover(x, y, cov, dir); + return; + } + else + { + p += diff; + if (p <= lim2) + { + Cover(x, y, cov, dir); + return; + } + } +} + +void Process(int x, int y, int dur, int cover, int direct, int st, int endt) +{ + int diff, p; + diff = (dur + 1) * 4; + Fill(x, y, 0, dur, st, endt, diff, direct, cover); + p = dur + 1; + Fill(x, y, p, dur, st, endt, diff, (direct + 1) % 4, cover); + p += 1 + dur; + Fill(x, y, p, dur, st, endt, diff, (direct + 2) % 4, cover); + p += 1 + dur; + Fill(x, y, p, dur, st, endt, diff, (direct + 3) % 4, cover); +} + +void Cal(int k) +{ + int i = 0, st, end, c = 0; + char dir; + while (i++ < k) + { + cin >> K[i].x >> K[i].y >> K[i].dur >> K[i].cov >> dir; + if (dir == 'N') + dir = 0; + else if (dir == 'E') + dir = 1; + else if (dir == 'S') + dir = 2; + else + dir = 3; + K[i].dir = dir; + G[K[i].x][K[i].y] = 1; + } + cin >> st >> end; + i = 0; + while (i++ < k) + Process(K[i].x, K[i].y, K[i].dur, K[i].cov, K[i].dir, st, end); + for (i = 1; i <= R; i++) + for (int j = 1; j <= C; j++) + if (G[i][j] == 0) + c++; + cout << c << endl; +} + +void Free() +{ + for (int i = 1; i <= R; i++) + for (int j = 1; j <= C; j++) + G[i][j] = 0; +} + +int main() +{ + int k; + while (cin >> R >> C >> k) + { + if (!R && !C && !k) + break; + Cal(k); + Free(); + } + return 0; +} diff --git a/10967.cpp b/10967.cpp new file mode 100644 index 0000000..d886e62 --- /dev/null +++ b/10967.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define INF 0x3FFFFFFF + +int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1}, D[256]; + +int map_[128][128], door[128][128], heap[16384], dist[16384], loc[16384], n, m, heapn; + +int cell(int r, int c) +{ + return r * m + c; +} + +void up(int p) +{ + int x; + for (x = heap[p]; p > 1 && dist[x] < dist[heap[p >> 1]]; p >>= 1) + { + loc[heap[p] = heap[p >> 1]] = p; + } + loc[heap[p] = x] = p; +} + +void down(int p) +{ + int x, t; + for (; (p << 1) <= heapn; p = x) + { + x = (dist[heap[p << 1]] < dist[heap[p]]) ? (p << 1) : p; + if ((p << 1) + 1 <= heapn && dist[heap[(p << 1) + 1]] < dist[heap[x]]) + { + x = (p << 1) + 1; + } + if (x == p) + { + break; + } + t = heap[p]; + loc[heap[p] = heap[x]] = p; + loc[heap[x] = t] = x; + } + loc[heap[p]] = p; +} + +int solve() +{ + int i, j, s, y, x, y1, x1, d; + j = m * n; + for (i = 0; i <= j; i++) + { + dist[i] = INF; + } + heapn = 1; + dist[heap[1] = cell(n - 1, 0)] = 0; + loc[cell(n - 1, 0)] = 1; + while (heapn > 0) + { + s = heap[1]; + if (--heapn >= 1) + { + heap[1] = heap[heapn + 1]; + down(1); + } + y = s / m; + x = s % m; + if (y == 0 && x == m - 1) + { + break; + } + for (d = 0; d < 4; d++) + { + y1 = y + dy[d]; + x1 = x + dx[d]; + if (y1 < 0 || y1 >= n) + { + continue; + } + if (x1 < 0 || x1 >= m) + { + continue; + } + if (map_[y1][x1] == '#') + { + continue; + } + i = D[map_[y1][x1]]; + if (i >= 0 && d != 3 - i) + { + continue; + } + if (map_[y][x] == '.') + { + i = 1; + } + else + { + if (D[map_[y][x]] == d) + { + i = 1; + } + else if (D[map_[y][x]] == 3 - d) + { + i = 1 + 2 * door[y][x]; + } + else + { + i = 1 + door[y][x]; + } + } + j = y1 * m + x1; + if (dist[j] <= dist[s] + i) + { + continue; + } + if (dist[j] == INF) + { + heap[++heapn] = j; + loc[j] = heapn; + } + dist[j] = dist[s] + i; + up(loc[j]); + } + } + return dist[m - 1]; +} + +int main() +{ + int i, j, k, t; + for (i = 0; i < 256; i++) + { + D[i] = -1; + } + D['N'] = 0; + D['W'] = 1; + D['E'] = 2; + D['S'] = 3; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &m) == 2;) + { + for (i = 0; i < n; i++) + for (j = 0; j < m && (k = getchar()) != EOF;) + if (strchr(".#NWES", k) != NULL) + { + map_[i][j++] = k; + } + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + if (D[map_[i][j]] >= 0) + { + scanf("%d", &door[i][j]); + } + i = solve(); + printf((i >= INF) ? "Poor Kianoosh\n" : "%d\n", i); + } + return 0; +} diff --git a/10968.cpp b/10968.cpp new file mode 100644 index 0000000..48b5a14 --- /dev/null +++ b/10968.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +#define maxn 1001 + +list L[maxn]; +int N, M, Deg[maxn]; +char C[maxn]; + +struct ss +{ + int node; + int dis; +}; +queue Q; + +int BFS(int st, int end) +{ + ss t, d; + int u, v; + list::iterator p; + t.node = st; + t.dis = 0; + Q.push(t); + C[st] = 1; + while (!Q.empty()) + { + t = Q.front(); + u = t.node; + Q.pop(); + for (p = L[u].begin(); p != L[u].end(); p++) + { + v = *p; + if (v == end) + return t.dis + 1; + if (C[v] || Deg[v] < 3) + continue; + C[v] = 1; + d.node = v; + d.dis = t.dis + 1; + Q.push(d); + } + } + return 0; +} + +void Cal() +{ + int i, u, v, f = 0; + for (i = 1; i <= N; i++) + { + if (Deg[i] == 0) + { + cout << "Poor Koorosh\n"; + return; + } + if (Deg[i] % 2) + { + if (Deg[i] == 1) + { + cout << "Poor Koorosh\n"; + return; + } + if (!f) + u = i; + else + v = i; + f++; + if (f == 2) + break; + } + } + if (f < 2) + { + cout << 0 << endl; + return; + } + f = BFS(u, v); + if (!f) + cout << "Poor Koorosh\n"; + else + cout << f << endl; +} + +void Clear() +{ + int i; + for (i = 1; i <= N; i++) + { + L[i].clear(); + Deg[i] = 0; + C[i] = 0; + } + while (!Q.empty()) + Q.pop(); +} + +int main() +{ + int i, u, v; + while (cin >> N >> M) + { + if (!N && !M) + break; + for (i = 0; i < M; i++) + { + cin >> u >> v; + Deg[u]++; + Deg[v]++; + L[u].push_back(v); + L[v].push_back(u); + } + Cal(); + Clear(); + } + return 0; +} diff --git a/10969.cpp b/10969.cpp new file mode 100644 index 0000000..2b2bd23 --- /dev/null +++ b/10969.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +struct Cir +{ + double x, y, r; + int scan() + { + return scanf("%lf%lf%lf", &r, &x, &y) == 3; + } +}; + +#define eps 1e-6 + +struct Pt +{ + double x, y; + bool operator<(const Pt other) const + { + if (fabs(x - other.x) < eps) + return y < other.y; + return x < other.x; + } +}; + +int IntersectCir(Cir A, Cir B, Pt v[]) +{// Pt v[] result buffer + double disAB = sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); + if (fabs(disAB) < eps && fabs(A.r - B.r) < eps) + { + return 3; + } + if (A.r + B.r + eps < disAB || A.r + disAB + eps < B.r || B.r + disAB + eps < A.r) + { + if (disAB < B.r - A.r + eps) + return 3;//cover all special case + return -1; + } + double x, y, vx, vy; + vx = B.x - A.x; + vy = B.y - A.y; + if (fabs(disAB - A.r - B.r) < eps || fabs(A.r - disAB - B.r) < eps || + fabs(B.r - A.r - disAB) < eps) + { + if (fabs(disAB - A.r - B.r) < eps) + {// (A)(B) + } + else + { + if (A.r < B.r) + { //((A)B) + return 3;// cover all special case + } + else + {//((B)A) + } + } + return 1; + } + double theta = acos((A.r * A.r + disAB * disAB - B.r * B.r) / 2 / A.r / disAB); + double rvx, rvy;//rotate vector + rvx = vx * cos(theta) - vy * sin(theta); + rvy = vx * sin(theta) + vy * cos(theta); + v[0].x = A.x + rvx * A.r / disAB; + v[0].y = A.y + rvy * A.r / disAB; + rvx = vx * cos(-theta) - vy * sin(-theta); + rvy = vx * sin(-theta) + vy * cos(-theta); + v[1].x = A.x + rvx * A.r / disAB; + v[1].y = A.y + rvy * A.r / disAB; + return 2; +} + +int main() +{ + int t, n, i, j, k; + scanf("%d", &t); + Cir D[105]; + while (t--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + D[i].scan(); + double ans = 0, L, R, M, tx, ty; + int Iidx; + Pt Interval[205], p[2]; + for (i = 0; i < n; i++) + { + Iidx = 0; + for (j = i + 1; j < n; j++) + { + int r = IntersectCir(D[i], D[j], p); + if (r == 3) + break; + if (r < 2) + continue; + L = atan2(p[0].y - D[i].y, p[0].x - D[i].x); + R = atan2(p[1].y - D[i].y, p[1].x - D[i].x); + if (L > R) + M = L, L = R, R = M; + M = (L + R) / 2; + tx = D[i].x + D[i].r * cos(M); + ty = D[i].y + D[i].r * sin(M); + if ((tx - D[j].x) * (tx - D[j].x) + (ty - D[j].y) * (ty - D[j].y) <= D[j].r * D[j].r) + { + Interval[Iidx].x = L; + Interval[Iidx++].y = R; + } + else + { + Interval[Iidx].x = -M_PI; + Interval[Iidx++].y = L; + Interval[Iidx].x = R; + Interval[Iidx++].y = M_PI; + } + } + if (j != n) + continue; + sort(Interval, Interval + Iidx); + double rr = -M_PI; + for (j = 0; j < Iidx; j++) + { + if (Interval[j].x <= rr) + rr = max(Interval[j].y, rr); + else + { + ans += D[i].r * (Interval[j].x - rr); + rr = Interval[j].y; + } + } + ans += D[i].r * (M_PI - rr); + } + printf("%.3lf\n", ans); + } + return 0; +} diff --git a/10970.cpp b/10970.cpp index dfac291..52953a5 100644 --- a/10970.cpp +++ b/10970.cpp @@ -1,10 +1,13 @@ -#include -using namespace std; - -int main(){ - int m,n; - while(cin>>m>>n){ - cout<<(m*n-1)< + +using namespace std; + +int main() +{ + int m, n; + while (scanf("%d%d", &m, &n) == 2) + { + printf("%d\n", m * n - 1); + } + return 0; +} diff --git a/10972.cpp b/10972.cpp new file mode 100644 index 0000000..a508237 --- /dev/null +++ b/10972.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define maxn 1100 + +int n, m, num, dfs_closk, f[maxn], low[maxn], d[maxn], e[maxn]; +vector path[maxn]; + +int get_fa(int x) +{ + return f[x] == x ? x : f[x] = get_fa(f[x]); +} + +void dfs(int x, int fa) +{ + int i, j; + low[x] = d[x] = dfs_closk++; + for (i = 0; i < path[x].size(); i++) + { + int y = path[x][i]; + if (y == fa) + continue; + if (d[y] > 0) + { + low[x] = min(low[x], d[y]); + f[get_fa(x)] = get_fa(y); + } + else + { + dfs(y, x); + low[x] = min(low[x], low[y]); + if (low[y] < d[y]) + f[get_fa(x)] = get_fa(y); + } + } +} + +int main() +{ + int i, j; + while (scanf("%d %d", &n, &m) == 2) + { + for (i = 0; i < n; i++) + path[i].clear(); + for (i = 0; i < m; i++) + { + int a, b; + scanf("%d %d", &a, &b); + a--, b--; + path[a].push_back(b); + path[b].push_back(a); + } + num = 0; + memset(d, 0, sizeof(d)); + for (i = 0; i < n; i++) + f[i] = i; + for (i = 0; i < n; i++) + { + if (d[i] == 0) + { + dfs_closk = 1; + dfs(i, -1); + num++; + } + } + for (i = 0; i < n; i++) + get_fa(i); + memset(e, 0, sizeof(e)); + for (i = 0; i < n; i++) + { + for (j = 0; j < path[i].size(); j++) + { + int y = path[i][j]; + if (f[i] != f[y] && i < y) + e[f[i]]++, e[f[y]]++; + } + } + int ans = 0; + for (i = 0; i < n; i++) + { + if (f[i] == i) + { + if (e[i] == 1) + ans++; + if (e[i] == 0 && num > 1) + ans += 2; + } + } + printf("%d\n", (ans + 1) / 2); + } + return 0; +} diff --git a/10973.cpp b/10973.cpp new file mode 100644 index 0000000..b2eced9 --- /dev/null +++ b/10973.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +/** 10973 - Triangle Counting +Each triangle can be uniquely identified with an ordered triple (x,y,z), in which x < y < z. + +An algorithm with running time O(VE) (where V is the number of vertices, and E is the number of edges) for enumerating all triangles is presented below. The algorithm uses adjacency lists to represent the graph. As an optimization, it stores the graph as a set of directed edges (x,y), in which x < y. + +for x=1..n, adj[x] is the set: { y: y>x and (x,y) is in E }. +flag[1..n] is a boolean array, with all elements initialized to 'false' +count = 0 + +for x = 1 to n: + for each y in adj[x]: + flag[y] = true + for each y in adj[x]: + for each z in adj[y]: + if flag[z] then: + #yield (x,y,z) + count++ + for each y in adj[x]: + flag[y] = false +*/ + +#define _USE_MATH_DEFINES// exposes constants M_PI +#define s(n) scanf("%d", &n) +#define sl(n) scanf("%lld", &n) +#define sf(n) scanf("%lf", &n) +#define ss(n) scanf("%s", n) +#define INF (1 << 30) +#define LINF (long long)1E18 +#define EPS std::numeric_limits::epsilon() +#define ABS(x) ((x) < 0 ? -(x) : (x)) +#define FOR(i, a, b) for (int i = a; i < b; ++i) +#define RFOR(i, a, b) for (int i = (a)-1, _b(b); i >= _b; --i) +#define REP(i, n) for ((i) = 0; (i) < (int)(n); ++(i)) +#define DREP(a) \ + sort(all(a)); \ + a.erase(unique(all(a)), a.end()) +#define INDEX(arr, ind) (lower_bound(all(arr), ind) - arr.begin()) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr) +#define mp make_pair +#define pb push_back +#define tri(a, b, c) mp(a, mp(b, c)) +#define fst first +#define snd second +#define FILL(a, v) memset(a, v, sizeof(a)) +#define SQR(a) ((a) * (a)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ALL(x) x.begin(), x.end() +#define SZ(v) ((int)(v.size())) +#define LOG(a) (cerr << "line#" << __LINE__ << ": " #a " = " << (a) << endl) +#define DBG(...) (__VA_ARGS__) +#define debug(args...) \ + { \ + cerr << "case#"; \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << " "; + return *this; + } +} dbg; +template +T gcd(T a, T b) +{ + return b ? gcd(b, a % b) : a; +} +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int uint; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VS; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; +//typedef complex pt; +//typedef complex pti; +const double PI = 3.1415926535897932384626433832795 /*2*acos(0.0)*/; +const float PI_F = 3.14159265358979f; +double round(double x) +{ + const double sd = 100;//for accuracy to 2 decimal places + return int(x * sd + (x < 0 ? -0.5 : 0.5)) / sd; +} +///////////////////////////////////////////////////////////////////////////// +#define MAXN 3001 +vector node[MAXN]; +vector::const_iterator p1, p2; +bool flag[MAXN]; +int t, N, M, u, v, i, src, dst, the_count; + +int main() +{ + scanf("%d\n", &t);// #testcases + while (t--) + { + scanf("%d %d\n", &N, &M);// vertices,edges + while (M--) + { + scanf("%d %d\n", &u, &v);// u->v + src = min(u, v); + dst = max(u, v); + node[src].push_back(dst); + } + the_count = 0; + for (i = 0; i < N; ++i) + { + for (p1 = node[i].begin(); p1 < node[i].end(); ++p1) + { + flag[*p1] = true; + } + for (p1 = node[i].begin(); p1 < node[i].end(); ++p1) + { + for (p2 = node[*p1].begin(); p2 < node[*p1].end(); ++p2) + { + if (flag[*p2]) + { + the_count++; + } + } + } + for (p1 = node[i].begin(); p1 < node[i].end(); ++p1) + { + flag[*p1] = false; + } + } + printf("%d\n", the_count); + // reset state + memset(flag, false, MAXN); + for (i = 0; i < MAXN; ++i) + { + node[i].clear(); + } + } + return 0; +} diff --git a/10975.cpp b/10975.cpp new file mode 100644 index 0000000..e38b08d --- /dev/null +++ b/10975.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +#define MAX 1048576 + +int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}; + +char *list_[16384], tab[128][128], alpha[MAX]; +int cnt[16384], n, nrows, ncols, child[MAX], sib[MAX], last, got[16384], ID; +short term[MAX]; + +void go(int y, int x, int d) +{ + int p; + for (p = 0;; y += dy[d], x += dx[d]) + { + for (p = child[p]; alpha[p] < tab[y][x]; p = sib[p]) + ; + if (alpha[p] != tab[y][x]) + { + return; + } + if (term[p]) + { + if (got[term[p] - 1] != ID) + { + got[term[p] - 1] = ID; + cnt[term[p] - 1] = 1; + } + else + { + cnt[term[p] - 1]++; + } + } + } +} + +void insert(char *s, int id) +{ + int p, x; + for (p = 0; *s; s++) + { + for (x = child[p]; x != 0 && alpha[x] != *s; x = sib[x]) + ; + if (x != 0) + { + p = x; + continue; + } + sib[last] = child[p]; + child[p] = last; + p = last; + last++; + alpha[p] = *s; + term[p] = 0; + child[p] = 0; + } + term[p] = id; +} + +int cmp(const void *p, const void *q) +{ + return strcmp(*(char **)p, *(char **)q); +} + +void solve() +{ + static char buf[65536]; + int i, j, k, q, nq; + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf(" %[a-z]", buf); + list_[i] = strdup(buf); + } + qsort(list_, n, sizeof(list_[0]), &cmp); + last = 1; + child[0] = sib[0] = term[0] = 0; + alpha[0] = 127; + for (i = n; i-- > 0;) + { + insert(list_[i], i + 1); + } + scanf("%d", &nq); + for (q = 1; q <= nq; q++) + { + scanf("%d %d", &nrows, &ncols); + printf("Query #%d\n", q); + for (i = 0; i <= nrows + 1; i++) + for (j = 0; j <= ncols + 1; j++) + { + tab[i][j] = ' '; + } + for (i = 1; i <= nrows; i++) + for (j = 1; j <= ncols; j++) + { + while ((k = getchar()) != EOF && k == '\n') + ; + tab[i][j] = ('a' <= k && k <= 'z') ? k : ' '; + } + ID++; + for (i = 1; i <= nrows; i++) + for (j = 1; j <= ncols; j++) + for (k = 0; k < 8; k++) + { + go(i, j, k); + } + for (i = 0; i < n; i++) + if (got[i] == ID) + { + printf("%s %d\n", list_[i], cnt[i]); + } + } + for (i = 0; i < n; i++) + { + free(list_[i]); + } +} + +int main() +{ + int c, t; + ID = 42; + for (scanf("%d", &t), c = 1; c <= t; c++) + { + printf("Test Case #%d\n", c); + solve(); + printf("\n"); + } + return 0; +} diff --git a/10976.cpp b/10976.cpp index 3825c7f..0c13013 100644 --- a/10976.cpp +++ b/10976.cpp @@ -1,22 +1,45 @@ -#include -using namespace std; - -int main(){ - int k; - while(cin>>k){ - int y=0; - int sol[1000][2]={0},cnt=0; - for(y=k+1;y<=2*k;y++){ - double x=((double)k*(double)y)/((double)y-(double)k); - if(x==(int)x){ - sol[cnt][0]=(int)x; - sol[cnt][1]=y; - cnt++; - } - } - cout< + +using namespace std; + +struct ss +{ + int x, y; +}; +ss A[10000000]; +char Fg[10000000]; +int ind, N, M, K; +void Cal() +{ + int i, j, d, g, l; + ss t; + ind = 0; + for (i = K + 1; i <= 2 * K; i++) + { + // if(Fg[i]) break; + d = i * K; + g = i - K; + l = d % g; + if (!l) + { + t.x = d / g; + t.y = i; + // Fg[i] = Fg[d/g] = 1; + A[ind++] = t; + } + } + printf("%d\n", ind); + for (i = 0; i < ind; i++) + { + printf("1/%d = 1/%d + 1/%d\n", K, A[i].x, A[i].y); + // Fg[A[i].x] = Fg[A[i].y] = 0; + } +} +int main() +{ + while (scanf("%d", &K) == 1) + { + Cal(); + } + return 0; +} diff --git a/10977.cpp b/10977.cpp new file mode 100644 index 0000000..a581d92 --- /dev/null +++ b/10977.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define maxn 200 + +int R, C, B[maxn + 3][maxn + 3]; +char Fg[maxn + 3][maxn + 3]; + +struct ss +{ + int r, c; + int time; +}; +queue Q; + +void Recur(int x, int y, int limit) +{ + int i, j, rl, cl, d, k = limit * limit, q; + int rf, cf; + rl = x - limit; + if (rl < 1) + rl = 1; + rf = x + limit; + if (rf > R) + rf = R; + cl = y - limit; + if (cl < 1) + cl = 1; + cf = y + limit; + if (cf > C) + cf = C; + for (i = rl; i <= rf; i++) + { + d = i - x; + d *= d; + for (j = cl; j <= cf; j++) + { + q = j - y; + q *= q; + if (d + q <= k) + B[i][j] = 1; + } + } + j = y + limit; + if (j <= C) + B[x][j] = 1; + j = y - limit; + if (j > 0) + B[x][j] = 1; + j = x - limit; + if (j > 0) + B[j][y] = 1; + j = x + limit; + if (j <= R) + B[j][y] = 1; +} + +int X[] = {0, 0, 1, -1}; +int Y[] = {1, -1, 0, 0}; + +int BFS() +{ + ss t, d; + int i, nx, ny; + if (B[1][1]) + return -1; + t.r = 1; + t.c = 1; + t.time = 0; + Q.push(t); + Fg[1][1] = 1; + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + for (i = 0; i < 4; i++) + { + nx = t.r + X[i]; + ny = t.c + Y[i]; + if (nx > R || nx < 1 || ny > C || ny < 1) + continue; + if (Fg[nx][ny] || B[nx][ny]) + continue; + if (nx == R && ny == C) + return t.time + 1; + d.r = nx; + d.c = ny; + d.time = t.time + 1; + Fg[nx][ny] = 1; + Q.push(d); + } + } + return -1; +} + +void Cal() +{ + int i, j; + if (R == 1 && C == 1) + { + if (B[1][1] == 1) + { + printf("Impossible.\n"); + return; + } + printf("0\n"); + return; + } + if (B[R][C]) + { + printf("Impossible.\n"); + return; + } + j = BFS(); + if (j < 0) + printf("Impossible.\n"); + else + printf("%d\n", j); +} + +void Free() +{ + int i, j; + for (i = 1; i <= R; i++) + for (j = 1; j <= C; j++) + { + B[i][j] = 0; + Fg[i][j] = 0; + } + while (!Q.empty()) + Q.pop(); +} + +int main() +{ + int n, i, x, y; + while (scanf("%d%d", &R, &C) == 2) + { + if (!R && !C) + break; + scanf("%d", &n); + while (n--) + { + scanf("%d%d", &x, &y); + B[x][y] = 1; + } + scanf("%d", &n); + while (n--) + { + scanf("%d%d%d", &x, &y, &i); + Recur(x, y, i); + } + Cal(); + Free(); + } + return 0; +} diff --git a/10978.cpp b/10978.cpp new file mode 100644 index 0000000..f4df58a --- /dev/null +++ b/10978.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + int n, sz; + string input; + bool visited[52]; + pair tmp; + vector> v(52); + while (cin >> n) + { + if (!n) + { + break; + } + int pos = -1; + fill(visited, visited + 52, false); + for (int i = 0; i < n; i++) + { + cin >> tmp.first >> tmp.second; + cin >> input; + sz = input.size(); + while (sz) + { + pos++; + pos %= n; + if (visited[pos]) + { + continue; + } + sz--; + } + v[pos] = tmp; + visited[pos] = true; + } + for (int i = 0; i < n; i++) + { + if (i > 0) + { + cout << " "; + } + cout << v[i].first << v[i].second; + } + cout << endl; + } + return 0; +} diff --git a/10979.cpp b/10979.cpp new file mode 100644 index 0000000..61beef3 --- /dev/null +++ b/10979.cpp @@ -0,0 +1,193 @@ +#include + +using namespace std; + +struct Point +{ + long long x, y; + Point(long long x_ = 0, long long y_ = 0) : x(x_), y(y_) {} + bool operator<(const Point &p) const + { + return x < p.x || (x == p.x && y < p.y); + } +}; + +int cross(Point o, Point a, Point b) +{ + long long t = (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); + return t == 0 ? 0 : (t > 0 ? 1 : -1); +} + +struct Segment +{ + Point A, B; + long long a, b, c; + + Segment() {} + + Segment(Point A_, Point B_) : A(A_), B(B_) + { + a = B.y - A.y; + b = A.x - B.x; + c = a * A.x + b * A.y; + } + + bool intersects(const Segment &other) + { + if (cross(other.A, other.B, A) * cross(other.A, other.B, B) > 0) + { + return false; + } + if (cross(A, B, other.A) * cross(A, B, other.B) > 0) + { + return false; + } + if (cross(other.A, A, B) == 0 && cross(other.B, A, B) == 0) + // parallel or overlapping segments + { + return false; + } + return true; + } + + bool contains(Point P) const + { + if (cross(A, B, P) != 0) + { + return false; + } + if (P.x < min(A.x, B.x)) + { + return false; + } + if (P.x > max(A.x, B.x)) + { + return false; + } + if (P.y < min(A.y, B.y)) + { + return false; + } + if (P.y > max(A.y, B.y)) + { + return false; + } + return true; + } + + bool overlaps(const Segment &other) + { + if (cross(A, other.A, other.B) != 0) + { + return false; + } + if (cross(B, other.A, other.B) != 0) + { + return false; + } + if (contains(other.A) || contains(other.B)) + { + return true; + } + if (other.contains(A) || other.contains(B)) + { + return true; + } + return false; + } + + Segment uniteWith(const Segment &other) + { + Point P[4] = {A, B, other.A, other.B}; + sort(P, P + 4); + return Segment(P[0], P[3]); + } +}; + +// Do the three pairwise-intersecting segments intersect at the same point? +bool concurrent(const Segment &S1, const Segment &S2, const Segment &S3) +{ + long long d = S1.a * S2.b - S2.a * S1.b; + long long x = S1.c * S2.b - S2.c * S1.b; + long long y = S1.a * S2.c - S2.a * S1.c; + if (d < 0) + { + x = -x; + y = -y; + d = -d; + } + return S3.a * x + S3.b * y == S3.c * d;// 4*m^4 +} + +Segment seg[1000]; +int N; + +int solve() +{ + // Unite overlapping segments + while (true) + { + bool changed = false; + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (seg[i].overlaps(seg[j])) + { + seg[i] = seg[i].uniteWith(seg[j]); + seg[j] = seg[N - 1]; + N--; + changed = true; + } + } + } + if (!changed) + { + break; + } + } + // Counting + int res = 0; + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (!seg[j].intersects(seg[i])) + { + continue; + } + for (int k = j + 1; k < N; k++) + { + if (!seg[k].intersects(seg[i])) + { + continue; + } + if (!seg[k].intersects(seg[j])) + { + continue; + } + if (concurrent(seg[i], seg[j], seg[k])) + { + continue; + } + res++; + } + } + } + return res; +} + +int main() +{ + while (scanf("%d", &N) == 1 && N > 0) + { + for (int i = 0; i < N; i++) + { + int x1, y1, x2, y2; + scanf("%d %d %d %d", &x1, &y1, &x2, &y2); + seg[i] = Segment(Point(x1, y1), Point(x2, y2)); + } + printf("%d\n", solve()); + } + return 0; +} diff --git a/10980.cpp b/10980.cpp new file mode 100644 index 0000000..cd9f5f4 --- /dev/null +++ b/10980.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +/* +10980 +Lowest Price in Town +*/ +#define maxn 202 +double C[maxn]; +char Fg[maxn]; +struct ss +{ + double price; + int quantiy; +}; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return y->quantiy - x->quantiy; +} +ss Lists[maxn]; +int Items; +double Mins; +char input[1000]; +void Recur(int n, double price, int lim) +{ + int i; + if (price >= Mins) + { + return; + } + if (Fg[n] && price > C[n] && n) + { + if (n >= lim) + { + Mins = C[n]; + } + return; + } + Fg[n] = 1; + C[n] = price; + if (n >= lim) + { + Mins = price; + return; + } + for (i = 0; i < Items; i++) + { + Recur(Lists[i].quantiy + n, price + Lists[i].price, lim); + } +} +void Cal() +{ + int k; + char *p; + qsort(Lists, Items, sizeof(ss), com); + gets(input); + p = strtok(input, " "); + while (p) + { + k = atoi(p); + p = strtok(NULL, " "); + Mins = 99999999; + Recur(0, 0, k); + printf("Buy %d for $%.2lf\n", k, Mins); + } +} +void Free() +{ + int i; + for (i = 0; i <= 200; i++) + { + Fg[i] = 0; + } +} +int main() +{ + double p; + int k, kase = 1; + ss t; + while (gets(input)) + { + sscanf(input, "%lf%d", &p, &Items); + t.price = p; + t.quantiy = 1; + Lists[0] = t; + for (k = 1; k <= Items; k++) + { + gets(input); + sscanf(input, "%d%lf", &Lists[k].quantiy, &Lists[k].price); + } + Items = k; + printf("Case %d:\n", kase++); + Cal(); + Free(); + } + return 0; +} diff --git a/10981.cpp b/10981.cpp new file mode 100644 index 0000000..a003070 --- /dev/null +++ b/10981.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +int mul[3][3] = {1, 1, 0, + 2, 1, 0, + 0, 2, 2}; + +char str[150], output[150][150], comp, ans; + +struct Edge +{ + char s[150]; + int len; + Edge *next; +} *g[1001]; + +int hash_(char *s) +{ + int sum = 0; + for (int i = 0; s[i]; i++) + { + sum = (sum * 3 + s[i] - 'a') % 1001; + } + return sum; +} +bool find(char *s, int key) +{ + int l = strlen(s); + for (Edge *p = g[key]; p; p = p->next) + { + if (p->len != l) + { + continue; + } + if (strcmp(p->s, s) == 0) + { + return true; + } + } + return false; +} +void add(char *s, int key) +{ + int l = strlen(s); + Edge *p = new Edge; + strcpy(p->s, s); + p->len = l; + p->next = g[key]; + g[key] = p; +} +void free(Edge *&p) +{ + if (p) + { + free(p->next); + delete p; + } +} +bool run(char *s, int len) +{ + if (len == 1) + { + if (*s == ans) + { + strcpy(output[len], s); + } + return *s == ans; + } + char temp[150]; + int lenp; + for (int i = 0; i < len - 1; i++) + { + for (lenp = 0; lenp < i; lenp++) + { + temp[lenp] = s[lenp]; + } + int a = s[i] - 'a', b = s[i + 1] - 'a'; + temp[lenp++] = mul[a][b] + 'a'; + for (; lenp < len - 1; lenp++) + { + temp[lenp] = s[lenp + 1]; + } + temp[lenp] = '\0'; + int key = hash_(temp); + if (find(temp, key)) + { + continue; + } + add(temp, key); + if (run(temp, len - 1)) + { + strcpy(output[len], s); + return true; + } + } + return false; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%s %c", str, &ans); + for (int i = 0; i < 1001; i++) + { + g[i] = NULL; + } + int len = strlen(str); + if (!run(str, len)) + { + printf("None exist!\n"); + } + else + { + for (int i = len; i >= 1; i--) + { + printf("%s\n", output[i]); + } + } + for (int i = 0; i < 1001; i++) + { + free(g[i]); + } + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/10982.cpp b/10982.cpp new file mode 100644 index 0000000..e6c2181 --- /dev/null +++ b/10982.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +/* +10982 +Troublemakers +*/ + +#define maxn 102 +struct ss +{ + int x, y; +}; +char links[maxn][maxn]; +vector E; +int N, M; +int G1[maxn], G2[maxn]; +int ind1, ind2; +void Free() +{ + int i; + for (i = 0; i < E.size(); i++) + { + links[E[i].x][E[i].y] = 0; + links[E[i].y][E[i].x] = 0; + } + E.clear(); +} +int Match(int A[], int n, int ind) +{ + int i, c = 0; + for (i = 0; i < ind; i++) + { + if (links[A[i]][n] == 1) + { + c++; + } + } + return c; +} +void Cal() +{ + int i, Pair = 0, g1, g2; + ind1 = 1; + ind2 = 0; + G1[0] = 1; + if (N <= 1) + { + cout << "0\n"; + return; + } + for (i = 2; i <= N; i++) + { + g1 = Match(G1, i, ind1); + g2 = Match(G2, i, ind2); + if (g1 < g2) + { + G1[ind1++] = i; + Pair += g1; + } + else + { + G2[ind2++] = i; + Pair += g2; + } + } + cout << ind1 << endl; + cout << G1[0]; + for (i = 1; i < ind1; i++) + { + cout << " " << G1[i]; + } + cout << endl; +} +int main() +{ + int ks, i, u, v, k = 1; + cin >> ks; + ss t; + while (ks--) + { + cin >> N >> M; + for (i = 0; i < M; i++) + { + cin >> u >> v; + links[u][v] = links[v][u] = 1; + t.x = u; + t.y = v; + E.push_back(t); + } + cout << "Case #" << k++ << ": "; + Cal(); + Free(); + } + return 0; +} diff --git a/10983.cpp b/10983.cpp new file mode 100644 index 0000000..07df833 --- /dev/null +++ b/10983.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +typedef vector VI; +typedef vector VVI; + +struct edge +{ + int x, y, c, c0, r, s; +}; + +vector E; +VVI G; + +int flow(int s, int t) +{ + for (int f = 0;;) + { + VI prev(G.size(), -1); + queue q; + q.push(s); + prev[s] = 0; + while (!q.empty() && prev[t] < 0) + { + int x = q.front(); + q.pop(); + for (int i = 0; i < (int)G[x].size(); i++) + { + int e = G[x][i], y = E[e].y; + if (E[e].c == 0 || prev[y] >= 0) + { + continue; + } + prev[y] = e; + q.push(y); + } + } + if (prev[t] < 0) + { + return f; + } + int c = 0x7fffffff; + for (int x = t; x != s; x = E[prev[x]].x) + { + c = MIN(c, E[prev[x]].c); + } + for (int x = t; x != s; x = E[prev[x]].x) + { + E[prev[x]].c -= c; + E[E[prev[x]].r].c += c; + } + f += c; + } +} + +void add(int x, int y, int c, int s) +{ + struct edge e; + e.x = x; + e.y = y; + e.c = e.c0 = c; + e.s = s; + e.r = E.size() + 1; + G[x].push_back(E.size()); + E.push_back(e); + swap(e.x, e.y); + e.r = E.size() - 1; + e.c = e.c0 = 0; + G[y].push_back(E.size()); + E.push_back(e); +} + +int main() +{ + int T, cs = 1, n, m, d, ex, ey, ec, es, ed; + for (scanf("%d", &T); cs <= T && scanf("%d %d %d", &n, &d, &m) == 3; cs++) + { + assert(1 <= n && n <= 30 && 1 <= d && d <= 10); + d++; + G = VVI(1 + n * d, VI()); + E.clear(); + VI costs(1, 0); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d %d %d", &ex, &ey, &ec, &es, &ed); + assert(1 <= ex && ex <= n && 1 <= ey && ey <= n && 0 <= ed && ed <= d - 2); + add(1 + (ex - 1) + ed * n, 1 + (ey - 1) + (ed + 1) * n, ec, es); + costs.push_back(es); + } + sort(costs.begin(), costs.end()); + costs.erase(unique(costs.begin(), costs.end()), costs.end()); + ey = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < d - 1; j++) + { + add(1 + i + j * n, 1 + i + (j + 1) * n, 1000000, 0); + } + scanf("%d", &ex); + ey += ex; + add(0, 1 + i, ex, 0); + } + int a, b; + for (a = 0, b = costs.size() - 1; a <= b;) + { + int c = (a + b) / 2, k = costs[c]; + for (int i = 0; i < (int)E.size(); i++) + { + E[i].c = (E[i].s <= k) ? E[i].c0 : 0; + } + if (flow(0, 1 + (n - 1) + (d - 1) * n) >= ey) + { + b = c; + if (a == b) + { + break; + } + } + else + { + a = c + 1; + } + } + printf("Case #%d: ", cs); + if (a > b) + { + printf("Impossible\n"); + } + else + { + printf("%d\n", costs[a]); + } + } + return 0; +} diff --git a/10984.cpp b/10984.cpp new file mode 100644 index 0000000..6dde37b --- /dev/null +++ b/10984.cpp @@ -0,0 +1,221 @@ +#include + +using namespace std; + +#define maxn 1005 + +char color[maxn], mark[maxn], Fg[maxn], taken[maxn], links[1001][1001]; +int N, M, C, edge, white, Ind, Adj[maxn], P[maxn], A[maxn]; +vector V[maxn]; +queue Q; + +void Edg() +{ + int i, u; + edge = 0; + for (i = 0; i < C; i++) + { + u = A[i]; + if (color[u] == 1) + edge += Adj[u]; + } +} + +int BFS(int u) +{ + int i, v, c; + while (!Q.empty()) + Q.pop(); + A[C++] = u; + color[u] = 1; + edge = 0; + Q.push(u); + Fg[u] = 1; + white = 1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + c = 1; + if (color[u] == 1) + c = 2; + for (i = 0; i < V[u].size(); i++) + { + v = V[u][i]; + if (Fg[v] == 0) + { + color[v] = c; + A[C++] = v; + if (c == 1) + white++; + Q.push(v); + Fg[v] = 1; + } + else if (color[v] == color[u]) + return 1; + } + } + if (white != C / 2) + return 1; + if (C % 2 != 0) + return 1; + Edg(); + return 0; +} + +int Special() +{ + if (!N && !M) + { + cout << "0\n\n"; + return 1; + } + if (!M) + { + cout << "Impossible\n"; + return 1; + } + return 0; +} + +void Mark(int v) +{ + int u; + u = P[v]; + if (u == -1) + return; + links[u][v]--; + links[v][u]++; + Mark(P[v]); +} + +int Next(int n) +{ + int i, d, u; + mark[n] = 1; + for (i = 0; i < V[n].size(); i++) + { + u = V[n][i]; + if (mark[u] == 1) + continue; + if (links[n][u] == 0) + continue; + P[u] = n; + if (color[u] == 2 && taken[u] == 0) + { + taken[u] = 1; + Mark(u); + return 1; + } + d = Next(u); + if (d) + return 1; + } + return 0; +} + +int Greedy() +{ + int i, d = 0, j, u; + for (i = 0; i < C; i++) + { + u = A[i]; + if (color[u] == 2) + continue; + for (j = 0; j < V[u].size(); j++) + { + d = V[u][j]; + links[d][u] = 0; + } + } + for (i = 0; i < C; i++) + { + u = A[i]; + if (color[u] == 2) + continue; + P[u] = -1; + for (j = 0; j < C; j++) + mark[A[j]] = 0; + d = Next(u); + if (d == 0) + return 1; + } + return 0; +} + +void Cal() +{ + int i, d; + if (Special()) + return; + for (i = 1; i <= N; i++) + { + if (Fg[i] == 0) + { + C = 0; + d = BFS(i); + if (d) + { + cout << "Impossible\n"; + return; + } + if (Greedy()) + { + cout << "Impossible\n"; + return; + } + } + } + cout << N / 2 << endl + << "1"; + for (i = 2; i <= N; i++) + if (color[i] == 1) + cout << " " << i; + cout << endl; +} + +void Free() +{ + int i, j, u; + for (i = 1; i <= N; i++) + { + Fg[i] = color[i] = 0; + for (j = 0; j < V[i].size(); j++) + { + u = V[i][j]; + links[i][u] = 0; + } + V[i].clear(); + Adj[i] = 0; + taken[i] = 0; + } +} + +int main() +{ + int ks, u, v, i, k = 1; + cin >> ks; + while (ks--) + { + cin >> N >> M; + cout << "Case #" << k++ << ": "; + for (i = 0; i < M; i++) + { + cin >> u >> v; + V[u].push_back(v); + V[v].push_back(u); + Adj[v]++; + Adj[u]++; + links[u][v] = links[v][u] = 1; + } + if (N % 2) + { + Free(); + cout << "Impossible\n"; + continue; + } + Cal(); + Free(); + } + return 0; +} diff --git a/10985.cpp b/10985.cpp new file mode 100644 index 0000000..1d8a14c --- /dev/null +++ b/10985.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define MAXD 150 +#define INF 100000000 +int f[MAXD][MAXD], M, N, res, ans[MAXD]; +void init() +{ + int i, j, a, b; + scanf("%d%d", &N, &M); + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + if (i == j) + { + f[i][j] = 0; + } + else + { + f[i][j] = INF; + } + } + for (i = 0; i < M; i++) + { + scanf("%d%d", &a, &b); + f[a][b] = f[b][a] = 1; + } +} +void floyd() +{ + int i, j, k; + for (k = 0; k < N; k++) + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + if (f[i][k] + f[k][j] < f[i][j]) + { + f[i][j] = f[i][k] + f[k][j]; + } +} +void printresult() +{ + int i, j, k, max = 0; + for (i = 0; i < N; i++) + for (j = i + 1; j < N; j++) + if (f[i][j] != INF) + { + res = 0; + for (k = 0; k < N; k++) + if (f[i][k] + f[k][j] == f[i][j]) + { + ans[res++] = k; + } + int p, q, temp = 0; + for (p = 0; p < res; p++) + for (q = p + 1; q < res; q++) + { + int a = ans[p]; + int b = ans[q]; + if (f[a][b] == 1 && f[i][a] != f[i][b]) + { + temp++; + } + } + if (temp > max) + { + max = temp; + } + } + printf("%d\n", max); +} +int main() +{ + int t, tt; + scanf("%d", &t); + for (tt = 0; tt < t; tt++) + { + init(); + floyd(); + printf("Case #%d: ", tt + 1); + printresult(); + } + return 0; +} diff --git a/10986.cpp b/10986.cpp new file mode 100644 index 0000000..917d2e8 --- /dev/null +++ b/10986.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +unsigned int INF = 4000000000; + +struct Vertex +{ + vdata id; + vector> adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct WGraph +{ + WGraph() {} + ~WGraph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, int weight, bool directed); + Vertex *getVertex(vdata v); + unsigned int dijkstra(vdata begin, vdata end); + + vmap vertexMap; + vmap::iterator it; + vector allVertexes; +}; + +WGraph::~WGraph() +{ + for (int i = 0, sz = allVertexes.size(); i < sz; i++) + { + delete allVertexes[i]; + } +} + +Vertex *WGraph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +Vertex *WGraph::getVertex(vdata v) +{ + it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return it->second; +} + +void WGraph::addEdge(vdata begin, vdata end, int weight, bool directed = false) +{ + Vertex *a = getVertex(begin); + Vertex *b = getVertex(end); + a->adj.push_back(make_pair(b, weight)); + if (!directed) + { + b->adj.push_back(make_pair(a, weight)); + } +} + +unsigned int WGraph::dijkstra(vdata begin, vdata end) +{ + set> q; + map dist; + for (int i = 0, sz = allVertexes.size(); i < sz; i++) + { + dist[allVertexes[i]->id] = INF; + } + dist[begin] = 0; + q.insert(make_pair(dist[begin], begin)); + while (!q.empty()) + { + vdata v = q.begin()->second; + q.erase(q.begin()); + Vertex *node = getVertex(v); + for (int i = 0, sz = node->adj.size(); i < sz; i++) + { + vdata to = node->adj[i].first->id; + unsigned int len = node->adj[i].second; + if (dist[v] + len < dist[to]) + { + q.erase(make_pair(dist[to], to)); + dist[to] = dist[v] + len; + q.insert(make_pair(dist[to], to)); + } + } + } + return dist[end]; +} + +int main() +{ + int cases; + int n, m, s, t; + int a, b, w; + cin >> cases; + for (int casenum = 0; casenum < cases; casenum++) + { + WGraph g; + cin >> n >> m >> s >> t; + for (int i = 0; i < n; i++) + { + g.addVertex(i); + } + for (int i = 0; i < m; i++) + { + cin >> a >> b >> w; + g.addEdge(a, b, w); + } + cout << "Case #" << casenum + 1 << ": "; + unsigned int result = g.dijkstra(s, t); + if (result == INF) + { + cout << "unreachable" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/10987.cpp b/10987.cpp new file mode 100644 index 0000000..2559abb --- /dev/null +++ b/10987.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +#define maxn 102 +int N, C[maxn][maxn], Fg[maxn][maxn]; + +int Anty() +{ + int i, j, k; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + if (i != j && j != k && i != k) + { + if (C[i][k] + C[k][j] == C[i][j]) + Fg[i][j] = 1; + if (C[i][k] + C[k][j] < C[i][j]) + return 1; + } + return 0; +} + +int Count() +{ + int i, j, k = 0; + for (i = 2; i <= N; i++) + for (j = 1; j < i; j++) + if (Fg[i][j] == 0) + k++; + return k; +} + +void Print() +{ + int i, j; + int d = Count(); + cout << d << endl; + if (!d) + return; + for (i = 1; i < N; i++) + for (j = i + 1; j <= N; j++) + if (Fg[i][j] == 0) + cout << i << " " << j << " " << C[i][j] << "\n"; +} + +void Cal() +{ + int d, i, j; + d = Anty(); + if (d) + cout << "Need better measurements.\n"; + else + Print(); + for (i = 1; i <= N; i++) + for (j = i + 1; j <= N; j++) + Fg[i][j] = Fg[j][i] = 0; +} + +int main() +{ + int i, c, ks, k = 1, j; + cin >> ks; + while (ks--) + { + cin >> N; + for (i = 1; i < N; i++) + for (j = 1; j <= i; j++) + { + cin >> c; + C[i + 1][j] = C[j][i + 1] = c; + } + cout << "Case #" << k++ << ":\n"; + Cal(); + cout << endl; + } + return 0; +} diff --git a/10989.cpp b/10989.cpp new file mode 100644 index 0000000..8c28dff --- /dev/null +++ b/10989.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 150; +const int inf = (1 << 20); +int edge[N][N]; + +int st_min_cut(vector &V) +{ + int n = V.size(); + int s, t; + int mincut = 0; + static int w[N];//cross w(A,{i}) + rep(i, n) w[i] = 0; + rep(i, n) + { + s = t; + t = max_element(w, w + n) - w;//merge (A,V[t]) + mincut = w[t]; + w[t] = -1; + //update w(A,i) + rep(j, n) if (w[j] != -1) + { + w[j] += edge[V[t]][V[j]]; + } + } + rep(i, n)//merge (V[s],V[t]) + { + edge[V[i]][V[s]] += edge[V[i]][V[t]]; + edge[V[s]][V[i]] += edge[V[t]][V[i]]; + } + V.erase(V.begin() + t); + //cout << s <<" " << t << " " << mincut << endl; + return mincut; +} + +int global_min_cut(int n) +{ + int ret = inf; + vector node(n); + rep(i, n) node[i] = i; + for (int i = n; i > 1; i--) + { + ret = min(ret, st_min_cut(node)); + } + return ret; +} + +main() +{ + int te, tc = 1; + cin >> te; + while (te--) + { + int n, m; + cin >> n >> m; + rep(i, n) rep(j, n) edge[i][j] = 0; + rep(i, m) + { + int f, t, c; + cin >> f >> t >> c; + f--; + t--; + edge[f][t] = edge[t][f] = c; + } + cout << "Case #" << tc++ << ": "; + cout << global_min_cut(n) << endl; + } + return 0; +} diff --git a/1099.cpp b/1099.cpp new file mode 100644 index 0000000..2322e64 --- /dev/null +++ b/1099.cpp @@ -0,0 +1,202 @@ +#include + +using namespace std; + +#define Done 1 +typedef struct Node +{ + int length, width; + int state, next; +}; +Node node[32768 * 50]; +int size; +int A[20], n, x, y, i; +int Link[32768], Sqrt[10001]; +int LOG2[32768], Sum[32768]; +char Used[101], mark[32768][101], times; +int check(int length, int width) +{ + if (length <= x && width <= y) + { + return 1; + } + if (length <= y && width <= x) + { + return 1; + } + return 0; +} +void insertList(int state, int length, int width) +{ + if (check(length, width) == 0) + { + return; + } + if (mark[state][length] != 0) + { + return; + } + static int prev, curr; + prev = 0, curr = Link[state]; + while (curr != 0) + { + if (node[curr].length < length) + { + break; + } + else if (node[curr].length == length) + { + return; + } + else + { + prev = curr, curr = node[curr].next; + } + } + size++; + if (prev == 0) + { + Link[state] = size; + } + else + { + node[prev].next = size; + } + node[size].length = length; + node[size].width = width; + node[size].state = state; + node[size].next = curr; + mark[state][length] = width; + mark[state][width] = length; + if (Used[length]) + { + Used[length]--, times--; + } + if (Used[width]) + { + Used[width]--, times--; + } +} +void doCombine(int state1, int state2) +{ + static int i, j, state; + static int a, b, c, d; + state = state1 | state2; + for (i = Link[state1]; i; i = node[i].next) + { + a = node[i].length, b = node[i].width; + if (mark[state2][a] != 0) + { + insertList(state, a, b + mark[state2][a]); + insertList(state, b + mark[state2][a], a); + } + if (mark[state2][b] != 0) + { + insertList(state, b, a + mark[state2][b]); + insertList(state, a + mark[state2][b], b); + } + if (times == 0) + { + return; + } + } +} +void doBuild() +{ + int i, maxState, minState; + int state, state1, state2; + int s, finalState, sqr, tmp; + finalState = (1 << n) - 1; + for (state = 1; state <= finalState; state++) + { + s = LOG2[state & (-state)]; + maxState = (state >> s) - 1; + minState = 1; + Sum[state] = Sum[state - (state & (-state))] + Sum[state & (-state)]; + sqr = Sqrt[Sum[state]], tmp = Sum[state], times = 0; + memset(Used, 0, sizeof(Used)); + for (i = 1; i <= sqr; i++) + if (tmp % i == 0 && check(i, tmp / i) == 1) + { + Used[i]++, Used[tmp / i]++; + times += 2; + } + for (i = minState; i <= maxState; i++) + { + if ((state & (i << s)) == (i << s)) + { + state1 = i << s; + state2 = state ^ state1; + if (state1 > state2) + { + break; + } + doCombine(state1, state2); + if (times == 0) + { + break; + } + } + } + } + if (mark[finalState][x] != 0) + { + puts("Yes"); + } + else + { + puts("No"); + } +} +int initBuild() +{ + int i, j, sqr, finalState; + size = 0, finalState = (1 << n) - 1; + for (i = 0; i <= finalState; i++) + { + Link[i] = 0, memset(mark[i], 0, sizeof(mark[i])); + } + for (i = 0; i < n; i++) + { + sqr = Sqrt[A[i]]; + for (j = 1; j <= sqr; j++) + { + if (A[i] % j == 0 && check(j, A[i] / j) == 1) + { + insertList(1 << i, j, A[i] / j); + insertList(1 << i, A[i] / j, j); + } + } + Sum[1 << i] = A[i]; + } +} +int main() +{ + int Case = 0, tmp, i; + for (i = 0; i <= 32767; i++) + { + LOG2[i] = (int)log2(i); + } + for (i = 0; i <= 10000; i++) + { + Sqrt[i] = (int)sqrt(i); + } + while (scanf("%d", &n) == 1 && n) + { + scanf("%d %d", &x, &y); + int sum = 0; + for (i = 0; i < n; i++) + { + scanf("%d", &A[i]), sum += A[i]; + } + printf("Case %d: ", ++Case); + if (sum != x * y) + { + puts("No"); + continue; + } + initBuild(); + doBuild(); + } + return 0; +} diff --git a/10990.cpp b/10990.cpp new file mode 100644 index 0000000..79024a4 --- /dev/null +++ b/10990.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) + +int mark[(2000000 >> 5) + 1], phi[2000005], dep_phi[2000005], dp[2000005]; + +void sieve() +{ + SET(1); + int n = 2000000; + for (int i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (int j = i; j <= n; j += i) + { + phi[j] = phi[j] / i * (i - 1); + SET(j); + } + } + } +} + +int main() +{ + for (int i = 2; i <= 2000000; i++) + { + phi[i] = i; + } + sieve(); + for (int i = 2; i <= 2000000; i++) + { + dep_phi[i] = 1 + dep_phi[phi[i]]; + dp[i] = dp[i - 1] + dep_phi[i]; + } + scanf("%*d"); + int i, j; + while (scanf("%d%d", &i, &j) == 2) + { + printf("%d\n", dp[j] - dp[i - 1]); + } + return 0; +} diff --git a/10991.cpp b/10991.cpp new file mode 100644 index 0000000..b70aeee --- /dev/null +++ b/10991.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int numOfCase; + +double r1, r2, r3; + +int main() +{ + cin >> numOfCase; + while (numOfCase--) + { + cin >> r1 >> r2 >> r3; + double a, b, c; + double A, B, C; + a = r2 + r3; + b = r1 + r3; + c = r1 + r2; + A = acos((b * b + c * c - a * a) / (2.0 * b * c)); + B = acos((a * a + c * c - b * b) / (2.0 * a * c)); + C = acos((a * a + b * b - c * c) / (2.0 * a * b)); + double Area = 0.5 * a * c * sin(B); + Area -= 0.5 * A * r1 * r1; + Area -= 0.5 * B * r2 * r2; + Area -= 0.5 * C * r3 * r3; + printf("%.6lf\n", Area); + } + return 0; +} diff --git a/10992.cpp b/10992.cpp new file mode 100644 index 0000000..eb0a708 --- /dev/null +++ b/10992.cpp @@ -0,0 +1,587 @@ +#include + +using namespace std; + +typedef long int ll; + +#define BASE 1000 +#define MAX_DIGIT 100 + +struct BigNum +{ + int sign, len; + ll digit[MAX_DIGIT]; + void absAdd(BigNum, BigNum); + void absSubtract(BigNum, BigNum); + void multAndAdd(BigNum, int, int); + + void operator=(int); //ok + void operator=(BigNum); //ok + void operator=(char *); //ok + void add(BigNum, int); //ok- + void add(BigNum, BigNum); //ok- + void subtract(BigNum, int); //ok- + void subtract(BigNum, BigNum); //ok- + void mult(BigNum, int); //ok- + void mult(BigNum, BigNum); //ok- + void div(BigNum, int, int &); //ok-, 3rd param: remainder + void div(BigNum, BigNum, BigNum &);//ok- + int comp(BigNum); //ok- + int absComp(BigNum); //ok- + void print(); //ok- + void update(); //ok- + void clear(); //ok- + string Rem(BigNum); + BigNum() + { + clear(); + } +}; +string BB; +void BigNum::operator=(int x) +{ + clear(); + if (x < 0) + { + x = -x; + sign = 1; + } + if (x == 0) + { + digit[len++] = 0; + return; + } + while (x) + { + digit[len++] = x % BASE; + x /= BASE; + } +} +void BigNum::operator=(BigNum n) +{ + int i; + clear(); + len = n.len; + sign = n.sign; + for (i = 0; i < len; ++i) + digit[i] = n.digit[i]; +} +/* + while providing 's', ensure that there + are no leading spaces in it +*/ +void BigNum::operator=(char *s) +{ + int i, v, p; + clear(); + p = 1; + i = strlen(s) - 1; + for (v = 0; i >= 0; i--) + { + if (s[i] == '-') + { + --i; + sign = 1; + break; + } + else if (s[i] == '+') + { + --i; + break; + } + if (s[i] < '0' || s[i] > '9') + { + continue; + } + v = v + (s[i] - '0') * p; + p *= 10; + if (p == BASE) + { + digit[len++] = v; + p = 1; + v = 0; + } + } + if (v) + { + digit[len++] = v; + } + update(); +} +/* + return value <===> meaning + 0 equal + 1 greater + -1 less +*/ +int BigNum::comp(BigNum n) +{ + int i; + if (sign < n.sign) + { + return 1; + } + if (sign > n.sign) + { + return -1; + } + i = max(len, n.len) - 1; + for (; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + if (sign == 0) + { + return 1; + } + else + { + return -1; + } + } + } + return 0; +} +int BigNum::absComp(BigNum n) +{ + int i; + if (len < n.len) + { + return -1; + } + if (len > n.len) + { + return 1; + } + for (i = len - 1; i >= 0; --i) + { + if (digit[i] > n.digit[i]) + { + return 1; + } + else if (digit[i] < n.digit[i]) + { + return -1; + } + } + return 0; +} +void BigNum::absSubtract(BigNum a, BigNum b) +{ + int pos, borrow, t; + borrow = 0; + t = max(a.len, b.len); + for (pos = 0; pos < t; ++pos) + { + digit[pos] = a.digit[pos] - b.digit[pos] - borrow; + if (digit[pos] < 0) + { + digit[pos] += BASE; + borrow = 1; + } + else + { + borrow = 0; + } + if (digit[pos] != 0) + { + len = pos + 1; + } + } + update(); +} +void BigNum::absAdd(BigNum a, BigNum b) +{ + int pos, carry; + carry = 0; + for (pos = 0; pos < max(a.len, b.len); ++pos) + { + digit[pos] = a.digit[pos] + b.digit[pos] + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + if (carry != 0) + { + //check for overflow + digit[max(a.len, b.len)] = carry; + len = max(a.len, b.len) + 1; + } + else + { + len = max(a.len, b.len); + } + update(); +} +void BigNum::add(BigNum a, int n) +{ + BigNum t; + t = n; + add(a, t); +} +void BigNum::add(BigNum a, BigNum b) +{ + clear(); + if (a.sign == b.sign) + { + absAdd(a, b); + sign = a.sign; + } + else + { + if (a.absComp(b) >= 0) + { + absSubtract(a, b); + sign = a.sign; + } + else + { + absSubtract(b, a); + sign = b.sign; + } + } +} +void BigNum::subtract(BigNum a, int n) +{ + BigNum t; + t = n; + subtract(a, t); +} +void BigNum::subtract(BigNum a, BigNum b) +{ + BigNum t; + clear(); + t = b; + t.sign = 1 - b.sign; + add(a, t); +} +void BigNum::mult(BigNum a, int s) +{ + int pos, carry; + if (s < 0) + { + sign = 1 - a.sign; + s = -s; + } + else + { + sign = a.sign; + } + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + //take care about any overflow + digit[pos] = a.digit[pos] * s + carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + } + pos = a.len; + while (carry != 0) + { + //check overflow + digit[pos] = carry % BASE; + carry /= BASE; + ++pos; + } + len = pos; + update(); +} +void BigNum::multAndAdd(BigNum a, int s, int offset) +{ + int pos, carry; + carry = 0; + for (pos = 0; pos < a.len; ++pos) + { + digit[pos + offset] += a.digit[pos] * s + carry; + carry = digit[pos + offset] / BASE; + digit[pos + offset] %= BASE; + } + pos = a.len + offset; + while (carry != 0) + { + //check overflow + digit[pos] += carry; + carry = digit[pos] / BASE; + digit[pos] %= BASE; + ++pos; + } + if (len < pos) + { + len = pos; + } + update(); +} +void BigNum::mult(BigNum a, BigNum b) +{ + int pos; + clear(); + for (pos = 0; pos < b.len; ++pos) + { + multAndAdd(a, b.digit[pos], pos); + } + sign = (a.sign + b.sign) % 2; +} +void BigNum::div(BigNum a, int s, int &r) +{ + int pos; + r = 0; + len = 0; + sign = a.sign; + if (s < 0) + { + sign = 1 - sign; + s = -s; + } + for (pos = a.len - 1; pos >= 0; --pos) + { + r = r * BASE + a.digit[pos]; + digit[pos] = r / s; + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + r %= s; + } + update(); +} +void BigNum::div(BigNum a, BigNum b, BigNum &r) +{ + int i, pos, lower, upper, mid; + BigNum d, e, t; + if (b.sign) + { + t = b; + t.sign = 0; + div(a, t, r); + sign = 1 - a.sign; + return; + } + for (i = 0; i < len; ++i) + { + digit[i] = 0; + } + len = 0; + r = (int)0; + for (pos = a.len - 1; pos >= 0; --pos) + { + r.mult(r, BASE); + r.add(r, a.digit[pos]); + lower = 0; + upper = BASE - 1; + while (upper > lower) + { + mid = (upper + lower) / 2 + 1; + d.mult(b, mid); + e.subtract(r, d); + if (e.sign == 0) + { + lower = mid; + } + else + { + upper = mid - 1; + } + } + digit[pos] = lower; + d.mult(b, lower); + r.subtract(r, d); + if (digit[pos] > 0 && pos >= len) + { + len = pos + 1; + } + } + update(); +} +void BigNum::update() +{ + while (len > 1 && digit[len - 1] == 0) + { + --len; + } + if (len <= 1 && digit[0] == 0) + { + sign = 0; + } +} +void BigNum::clear() +{ + sign = len = 0; + memset(digit, 0, sizeof(digit)); +} +void BigNum::print() +{ + int i; + char ss[100]; + BB = ""; + if (sign) + { + printf("-"); + } + sprintf(ss, "%ld", digit[len - 1]); + BB += ss; + for (i = len - 2; i >= 0; --i) + { + //BE CAREFUL HERE + switch (BASE) + { + case 10: + printf("%ld", digit[i]); + break; + case 100: + printf("%02ld", digit[i]); + break; + case 1000: + sprintf(ss, "%03ld", digit[i]); + BB += ss; + break; + case 10000: + printf("%04ld", digit[i]); + break; + case 100000: + printf("%05ld", digit[i]); + break; + case 1000000: + printf("%06ld", digit[i]); + break; + case 10000000: + printf("%07ld", digit[i]); + break; + case 100000000: + printf("%08ld", digit[i]); + break; + case 1000000000: + printf("%09ld", digit[i]); + break; + default: + break; + } + } +} +string BigNum::Rem(BigNum b) +{ + int i; + string A = ""; + char xx[100]; + for (i = b.len - 1; i >= 0; i--) + { + sprintf(xx, "%ld", b.digit[i]); + A += xx; + } + return A; +} +int Zero(string A, int k) +{ + int i, c = 0, n, d; + for (i = 0; i < (int)A.size(); i++) + { + n = A[i] - '0'; + d = c * 10 + n; + c = d % k; + } + if (c == 0) + { + return 0; + } + return 1; +} +int Leap(string A) +{ + int d, k; + d = Zero(A, 400); + if (d == 0) + { + return 1; + } + d = Zero(A, 4); + k = Zero(A, 100); + if (d == 0 && k != 0) + { + return 1; + } + return 0; +} +void Cal(BigNum ini) +{ + BigNum a, z, x; + string A; + int pf, f = 0, d; + a = "2148"; + d = ini.absComp(a); + if (d < 0) + { + printf("No ghost will come in this year\n"); + return; + } + x.subtract(ini, a); + ini.print(); + pf = Leap(BB); + x.print(); + A = BB; + if (Zero(A, 2) == 0) + { + printf("Ghost of Tanveer Ahsan!!!\n"); + f = 1; + } + if (Zero(A, 5) == 0) + { + printf("Ghost of Shahriar Manzoor!!!\n"); + f = 1; + } + if (Zero(A, 7) == 0) + { + printf("Ghost of Adrian Kugel!!!\n"); + f = 1; + } + if (Zero(A, 11) == 0) + { + printf("Ghost of Anton Maydell!!!\n"); + f = 1; + } + if (Zero(A, 15) == 0) + { + printf("Ghost of Derek Kisman!!!\n"); + f = 1; + } + if (Zero(A, 20) == 0) + { + printf("Ghost of Rezaul Alam Chowdhury!!!\n"); + f = 1; + } + if (Zero(A, 28) == 0) + { + printf("Ghost of Jimmy Mardell!!!\n"); + f = 1; + } + if (Zero(A, 36) == 0) + { + printf("Ghost of Monirul Hasan!!!\n"); + f = 1; + } + if (pf) + { + printf("Ghost of K. M. Iftekhar!!!\n"); + f = 1; + } + if (f == 0) + { + printf("No ghost will come in this year\n"); + } +} + +int main() +{ + int k = 0; + char st[1000]; + BigNum t, s, rem, x; + x = "0"; + while (scanf("%s", st)) + { + if (!strcmp(st, "0")) + break; + if (k++) + printf("\n"); + printf("%s\n", st); + t = st; + Cal(t); + } + return 0; +} diff --git a/10993.cpp b/10993.cpp new file mode 100644 index 0000000..6eea584 --- /dev/null +++ b/10993.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +const int MAXN = 100000; + +bool v[MAXN]; +char patt[11]; +int queue_[MAXN], d[MAXN], p[MAXN], output[MAXN], len, base; + +bool BFS() +{ + int pl = strlen(patt); + for (int i = 0; i < pl; i++) + { + patt[i] -= '0'; + } + int front = -1, rear = -1; + memset(v, false, sizeof(v)); + for (int i = pl - 1; i >= 0; i--) + { + if (patt[i] == 0) + { + continue; + } + queue_[++rear] = (patt[i] % base); + d[rear] = patt[i]; + v[patt[i]] = true; + p[rear] = -1; + } + while (front < rear) + { + int s = queue_[++front]; + if (s == 0) + { + len = 0; + int ptr = front; + do + { + output[len++] = d[ptr]; + ptr = p[ptr]; + } while (ptr != -1); + return true; + } + for (int i = pl - 1; i >= 0; i--) + { + int t = (s * 10 + patt[i]) % base; + if (v[t]) + { + continue; + } + v[t] = true; + queue_[++rear] = t; + d[rear] = patt[i]; + p[rear] = front; + } + } + return false; +} + +int main() +{ + while (scanf("%s%d", patt, &base) == 2) + { + if (strcmp(patt, "0") == 0 && !base) + { + break; + } + if (!BFS()) + { + printf("impossible\n"); + } + else + { + for (int i = len - 1; i >= 0; i--) + { + printf("%d", output[i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/10994.cpp b/10994.cpp index 1e52787..1cbcad9 100644 --- a/10994.cpp +++ b/10994.cpp @@ -1,25 +1,63 @@ -#include -using namespace std; -long long F(long long n){ - if(n==0) - return 0; - else if(n%10>0) - return (n%10); - else - return F(n/10); -} -int main(){ - long long F[] - for(i=1;i<) - for(;;){ - long long p,q,sum=0,i=0; - cin>>p>>q; - if(p<0 && q<0) - break; - for(i=p;i<=q;i++){ - sum+=F(i); - } - cout< + +using namespace std; + +long long solve(long long p, long long q) +{ + if (p > q) + { + return 0; + } + if (p / 10 == 0 && q / 10 == 0) + { + long long sum = 0; + for (long long i = p; i <= q; i++) + { + sum += i; + } + return sum; + } + if (p / 10 == 0 && q / 10 != 0) + { + long long pp = 10, qq, i; + long long sum = 0; + for (i = p; i < pp; i++) + { + sum += i % 10; + } + qq = q / 10 * 10; + for (i = qq; i <= q; i++) + { + sum += i % 10; + } + return sum + solve(pp, qq); + } + if (p % 10 == 0 && q % 10 == 0) + { + long long sum = (q - p) / 10 * 45; + return sum + solve(p / 10, q / 10); + } + long long pp, qq, i; + pp = (long long)ceil(p * 1.0 / 10) * 10; + qq = q / 10 * 10; + long long sum = 0; + for (i = p; i <= pp; i++) + { + sum += i % 10; + } + for (i = qq; i <= q; i++) + { + sum += i % 10; + } + return sum + solve(pp, qq); +} + +int main() +{ + long long p, q; + while (cin >> p >> q && p >= 0 && q >= 0) + { + cout << solve(p, q) << endl; + } + return 0; +} diff --git a/10995.cpp b/10995.cpp new file mode 100644 index 0000000..9239fc2 --- /dev/null +++ b/10995.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int i, j, time; + double T[5], x, y, rt, a, b; + char in[10]; + while (scanf("%s", in) == 1) + { + if (in[0] == '-') + { + break; + } + T[0] = (in[0] - '0') * 36000 + (in[1] - '0') * 3600 + (in[3] - '0') * 600 + (in[4] - '0') * 60 + (in[6] - '0') * 10 + (in[7] - '0'); + for (i = 1; i < 5; i++) + { + scanf("%s", in); + T[i] = (in[0] - '0') * 36000 + (in[1] - '0') * 3600 + (in[3] - '0') * 600 + (in[4] - '0') * 60 + (in[6] - '0') * 10 + (in[7] - '0'); + } + x = (T[1] - T[2]) / (T[0] - T[2]); + y = (T[3] - T[2]) / (T[4] - T[2]); + a = (x - x * y) / (1 - x * y); + b = (y - x * y) / (1 - x * y); + rt = T[2] + (T[0] - T[2]) * a + (T[4] - T[2]) * b; + time = (int)(rt + 0.5); + printf("%02d:%02d:%02d\n", time / 3600, time / 60 % 60, time % 60); + } + return 0; +} diff --git a/10996.cpp b/10996.cpp new file mode 100644 index 0000000..a9c76c6 --- /dev/null +++ b/10996.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +int board[200][200]; +int visit[200][200]; +#define b(i, j) board[(i) + 100][(j) + 100] +#define v(i, j) visit[(i) + 100][(j) + 100] +int i, j, k, m, n, x, y, z, cookie = 1; +char P; +int dofilled(int x, int y, int c, int B) +{ + if (v(x, y) == cookie) + { + return 1; + } + if (b(x, y) == -1) + { + return 1; + } + if (b(x, y) == B) + { + return 1; + } + if (b(x, y) != c) + { + return 0; + } + v(x, y) = cookie; + return dofilled(x + 1, y, c, B) && dofilled(x - 1, y, c, B) && dofilled(x, y + 1, c, B) && dofilled(x, y - 1, c, B); +} +int dofill(int x, int y, int d) +{ + if (v(x, y) != cookie) + { + return 0; + } + if (b(x, y) == d) + { + return 0; + } + b(x, y) = d; + v(x, y) = 0; + return 1 + dofill(x + 1, y, d) + dofill(x - 1, y, d) + dofill(x, y + 1, d) + dofill(x, y - 1, d); +} + +int reach(int x, int y, int c, int B, int d) +{ + cookie++; + if (!dofilled(x, y, c, B)) + { + return 0; + } + return dofill(x, y, d); +} +int owns[256], caps[256]; +int update(int x, int y, int P, int Q) +{ + int cap = reach(x, y, Q, P, 0); + caps[P] += cap; + return cap; +} +void final() +{ + int x, y; + for (x = -z; x <= z; x++) + { + for (y = -z; y <= z; y++) + { + owns['W'] += reach(x, y, 0, 'W', 'w'); + owns['B'] += reach(x, y, 0, 'B', 'b'); + } + } +} +int main() +{ + while (2 == scanf("%d%d", &n, &m) && n) + { + z = (n - 1) / 2; + memset(board, 0, sizeof(board)); + memset(owns, 0, sizeof(owns)); + memset(caps, 0, sizeof(owns)); + for (i = -z; i <= z; i++) + { + b(i, -z - 1) = b(i, z + 1) = b(-z - 1, i) = b(z + 1, i) = -1; + } + for (i = 0; i < m; i++) + { + scanf(" %c ( %d , %d )", &P, &x, &y); + assert(x >= -z && y >= -z && x <= z && y <= z); + if (b(x, y)) + { + printf("board occupied %d %d\n", x, y); + } + b(x, y) = P; + update(x + 1, y, P, 'B' + 'W' - P); + update(x - 1, y, P, 'B' + 'W' - P); + update(x, y + 1, P, 'B' + 'W' - P); + update(x, y - 1, P, 'B' + 'W' - P); + if (update(x, y, 'B' + 'W' - P, P)) + { + printf("bad move!"); + } + } + final(); + printf("%d %d\n", owns['B'] + caps['B'], owns['W'] + caps['W']); + } +} diff --git a/10997.cpp b/10997.cpp new file mode 100644 index 0000000..4882e0d --- /dev/null +++ b/10997.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int numOfCountry, canada; +double N = 100; + +struct Country +{ + char name[50]; + int g, s, b; +} country[256]; + +void Input() +{ + canada = -1; + for (int i = 0; i < numOfCountry; i++) + { + cin >> country[i].name >> country[i].g >> country[i].s >> country[i].b; + if (!strcmp(country[i].name, "Canada")) + { + canada = i; + } + } +} + +bool Win(double j, double k, double l) +{ + double caScore = j * country[canada].g + k * country[canada].s + l * country[canada].b; + double best = -1; + for (int i = 0; i < numOfCountry; i++) + { + if (i == canada) + { + continue; + } + double score = j * country[i].g + k * country[i].s + l * country[i].b; + if (score > best) + { + best = score; + } + } + return caScore >= best; +} + +void Solve() +{ + int j, k, l; + for (j = 0; j <= 0; j++) + for (k = -2; k <= 2; k++) + for (l = -3; l <= 3; l++) + { + if (Win(pow(N, (double)-j), pow(N, (double)-k), pow(N, (double)-l))) + { + cout << "Canada wins!" << endl; + return; + } + } + cout << "Canada cannot win." << endl; +} + +int main() +{ + while (cin >> numOfCountry && numOfCountry) + { + Input(); + if (canada < 0) + { + cout << "Canada cannot win." << endl; + } + else + { + Solve(); + } + } + return 0; +} diff --git a/10998.cpp b/10998.cpp new file mode 100644 index 0000000..77a9727 --- /dev/null +++ b/10998.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +double h, v, H, V; +int n, kase = 1; + +const double ZERO = 1e-10; + +void solve(double x1, double y1, double x2, double y2, double qx, double qy, int level) +{ + // cout<> H >> V >> h >> v) + { + if (fabs(H) < ZERO || fabs(V) < ZERO || fabs(h) < ZERO || fabs(v) < ZERO) + { + break; + } + cout << "Case " << kase++ << ":" << endl; + cin >> n; + while (n--) + { + double x, y; + cin >> x >> y; + solve(0, 0, H, V, x, y, 0); + } + } + return 0; +} diff --git a/10999.cpp b/10999.cpp new file mode 100644 index 0000000..465edde --- /dev/null +++ b/10999.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +set dictionary; +int N, M, P; + +struct Tile +{ + char c; + int v; +} tile[10]; + +int best; + +void solve(int index, int vis[]) +{ + if (index >= P) + { + int sum = 0; + string tmp; + for (int i = 0; i < P; i++) + if (vis[i] == 1) + { + sum += tile[i].v; + tmp += tile[i].c; + } + sort(tmp.begin(), tmp.end()); + if (dictionary.count(tmp) && sum >= best) + { + best = sum; + } + return; + } + vis[index] = 0; + solve(index + 1, vis); + vis[index] = 1; + solve(index + 1, vis); + vis[index] = 0; +} + +int main() +{ + cin >> N; + while (N--) + { + string s; + cin >> s; + sort(s.begin(), s.end()); + dictionary.insert(s); + } + cin >> M; + while (M--) + { + cin >> P; + for (int i = 0; i < P; i++) + { + cin >> tile[i].c >> tile[i].v; + } + best = -1; + int vis[20]; + memset(vis, 0, sizeof(vis)); + solve(0, vis); + cout << best << endl; + } + return 0; +} diff --git a/110.cpp b/110.cpp new file mode 100644 index 0000000..13735a8 --- /dev/null +++ b/110.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +class ArrayList +{ +public: + int data[32], size; + ArrayList(); + void push_back(int); + int pop_head(); + void clear(); + void print(); + void swap(int, int); +}; +ArrayList::ArrayList() +{ + size = 0; +} +void ArrayList::clear() +{ + size = 0; +} +void ArrayList::print() +{ + for (int i = 0; i < size - 1; ++i) + { + printf("%c,", data[i]); + } + printf("%c", data[size - 1]); +} +void ArrayList::swap(int x, int y) +{ + int tmp = data[x]; + data[x] = data[y]; + data[y] = tmp; +} +void ArrayList::push_back(int input) +{ + data[size++] = input; +} +int ArrayList::pop_head() +{ + int head = data[0]; + for (int i = 0; i < size; ++i) + { + data[i] = data[i + 1]; + } + size--; + return head; +} +void printSeq(char c, int length) +{ + for (int i = 0; i < length - 1; ++i) + { + printf("%c,", c + i); + } + printf("%c", c + length - 1); +} +void indent(int num) +{ + for (int i = 0; i < num; ++i) + printf(" "); +} +void metaSort(ArrayList &ls, int insertChar, int bound) +{ + // termination condition + if (insertChar == bound) + { + indent(insertChar - 'a'); + printf("writeln("); + ls.print(); + printf(")\n"); + return; + } + ls.push_back(insertChar);// insert to tail + indent(insertChar - 'a'); + printf("if %c < %c then\n", ls.data[ls.size - 2], ls.data[ls.size - 1]); + metaSort(ls, insertChar + 1, bound); + // next insert to every place + for (int i = ls.size - 1; i > 0; --i) + { + ls.swap(i, i - 1); + // ------------------------- + indent(insertChar - 'a'); + if (i == 1) + { + printf("else\n"); + } + else + { + printf("else if %c < %c then\n", ls.data[i - 2], ls.data[i - 1]); + } + // ------------------------- + metaSort(ls, insertChar + 1, bound); + } + ls.pop_head();// delete head +} + +int main() +{ + ArrayList ls; + int times, max; + scanf("%d", ×); + for (int i = 0; i < times; ++i) + { + scanf("%d", &max); + // -------------------- + printf("program sort(input,output);\nvar\n"); + printSeq('a', max); + printf(" : integer;\nbegin\n readln("); + printSeq('a', max); + printf(");\n"); + // -------------------- + ls.push_back('a'); + metaSort(ls, 'b', 'a' + max); + ls.clear(); + // -------------------- + printf("end.\n"); + if (i != times - 1) + { + printf("\n"); + } + // -------------------- + } + return 0; +} diff --git a/1100.cpp b/1100.cpp new file mode 100644 index 0000000..c280384 --- /dev/null +++ b/1100.cpp @@ -0,0 +1,245 @@ +#include + +using namespace std; + +struct point3 +{ + double x, y, z; + point3 &operator+=(point3 a) + { + x += a.x, y += a.y, z += a.z; + return *this; + } + point3 &operator-=(point3 a) + { + x -= a.x, y -= a.y, z -= a.z; + return *this; + } + point3 &operator*=(double a) + { + x *= a, y *= a, z *= a; + return *this; + } + point3 &operator/=(double a) + { + x /= a, y /= a, z /= a; + return *this; + } +}; +point3 operator+(point3 a, point3 b) +{ + point3 c; + c = a, c += b; + return c; +} +point3 operator-(point3 a, point3 b) +{ + point3 c; + c = a, c -= b; + return c; +} +point3 operator*(point3 a, double b) +{ + point3 c; + c = a, c *= b; + return c; +} +point3 operator*(double a, point3 b) +{ + point3 c; + c = b, c *= a; + return c; +} +point3 operator/(point3 a, double b) +{ + point3 c; + c = a, c /= b; + return c; +} +double operator*(point3 a, point3 b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +point3 operator%(point3 a, point3 b) +{ + point3 c; + c.x = a.y * b.z - a.z * b.y; + c.y = a.z * b.x - a.x * b.z; + c.z = a.x * b.y - a.y * b.x; + return c; +} +double dis(point3 a) +{ + return sqrt(a * a); +} +struct line3 +{ + point3 a, s; + line3(point3 _a, point3 _s) + { + a = _a, s = _s; + } +}; +struct face3 +{ + point3 a, n; +}; +double point_line_dis(point3 a, line3 b, point3 *res = NULL) +{ + point3 p; + p = b.a + ((a - b.a) * b.s) / (b.s * b.s) * b.s; + if (res != NULL) + { + *res = p; + } + return dis(a - p); +} +double point_face_dis(point3 a, face3 b, point3 *res = NULL) +{ + point3 p; + p = ((a - b.a) * b.n) / (b.n * b.n) * b.n; + if (res != NULL) + { + *res = p; + } + return dis(p); +} +face3 face_from_point(point3 a, point3 b, point3 c) +{ + face3 f; + f.a = a, f.n = (b - a) % (c - a); + return f; +} +double mixed_prod(point3 a, point3 b, point3 c) +{ + return (a % b) * c; +} +point3 x[5], f, g; +void solve() +{ + for (int i = 1; i < 5; i++) + { + scanf("%lf%lf%lf", &x[i].x, &x[i].y, &x[i].z); + } + scanf("%lf%lf%lf", &f.x, &f.y, &f.z); + double s1 = fabs(mixed_prod(x[3] - x[0], x[3] - x[1], x[3] - x[2])), s2 = fabs(mixed_prod(x[4] - x[0], x[4] - x[1], x[4] - x[2])); + g = ((x[0] + x[1] + x[2] + x[3]) / 4 * s1 + (x[0] + x[1] + x[2] + x[4]) / 4 * s2) / (s1 + s2); + double min = 1e10, max = 0; + for (int i = 0; i < 5; i++) + for (int j = i + 1; j < 5; j++) + for (int k = j + 1; k < 5; k++) + { + face3 y = face_from_point(x[i], x[j], x[k]); + int flag = 0, another = -1; + for (int t = 0; t < 5; t++) + { + double dis = ((x[t] - y.a) * y.n) / (y.n * y.n); + if (fabs(dis) < 1e-8 && t != i && t != j && t != k) + { + another = t; + } + if (fabs(dis) > 1e-8) + { + int s = (dis > 0 ? 1 : -1); + if (s == -flag) + { + flag = -2; + break; + } + flag = s; + } + } + if (flag == -2) + { + continue; + } + std::vector tmp; + tmp.push_back(x[i]), tmp.push_back(x[j]), tmp.push_back(x[k]); + if (another > -1) + { + tmp.push_back(x[another]); + for (int i = 0; i < 4; i++) + { + double s = 0, t = 0; + for (int j = 0; j < 4; j++) + if (i != j) + { + int k = (j + 1) % 4; + if (k == i) + { + k = (k + 1) % 4; + } + s += fabs(((tmp[i] - tmp[j]) % (tmp[i] - tmp[k])) * y.n); + t += ((tmp[i] - tmp[j]) % (tmp[i] - tmp[k])) * y.n; + } + if (fabs(s - fabs(t)) < 1e-8) + { + tmp.erase(tmp.begin() + i); + break; + } + } + if (tmp.size() == 4) + for (int i = 1; i < 3; i++) + { + int j = i; + for (; j < 4; j++) + if (j != i - 2) + { + double t = 1; + for (int k = 0; k < 4; k++) + if (k != i - 1 && k != j) + { + t *= ((tmp[i - 1] - tmp[k]) % (tmp[i - 1] - tmp[j])) * y.n; + } + if (t > 1e-8) + { + break; + } + } + std::swap(tmp[i], tmp[j]); + } + } + point3 g_ = g - ((g - y.a) * y.n) / (y.n * y.n) * y.n; + double s = 0, t = 0; + for (int i = 1; i + 1 < tmp.size(); i++) + { + s += dis((tmp[i + 1] - tmp[0]) % (tmp[i] - tmp[0])); + } + for (int i = 0; i < tmp.size(); i++) + { + t += fabs(dis((g_ - tmp[i]) % (g_ - tmp[(i + 1) % tmp.size()]))); + } + if (fabs(t - fabs(s)) > 1e-8) + { + continue; + } + double d = 1e10; + for (int j = 0; j < tmp.size(); j++) + { + d = std::min(d, point_line_dis(g_, line3(tmp[j], tmp[(j + 1) % tmp.size()] - tmp[j]))); + } + if (d < 0.2) + { + continue; + } + d = point_face_dis(f, y); + if (d < min) + { + min = d; + } + if (d > max) + { + max = d; + } + } + printf("%.5lf %.5lf\n", min, max); +} +int main() +{ + int _ = 0; + while (scanf("%lf%lf%lf", &x[0].x, &x[0].y, &x[0].z) == 3) + { + printf("Case %d: ", ++_), solve(); + } + return 0; +} diff --git a/11000.cpp b/11000.cpp index 6395af9..148f284 100644 --- a/11000.cpp +++ b/11000.cpp @@ -1,28 +1,24 @@ -#include - -using namespace std; - -int main() -{ - int i; - unsigned long int fe[100], ma[100]; - fe[0] = 1, ma[0] = 0; - - for(i = 1; i < 100; i++) - { - fe[i] = ma[i-1]+1; - ma[i] = fe[i-1]+ma[i-1]; - } - - for(;;) - { - cin>>i; - if(i==-1) - break; - - else - cout << ma[i] << " " << fe[i]+ma[i] << endl; - } - - return 0; -} +#include + +using namespace std; + +int main() +{ + unsigned int X[50]; + X[0] = 0, X[1] = 1, X[2] = 2; + for (int i = 3; i <= 49; i++) + { + X[i] = X[i - 1] + X[i - 2] + 1; + } + for (;;) + { + int n; + cin >> n; + if (n == -1) + { + break; + } + cout << X[n] << " " << X[n + 1] << endl; + } + return 0; +} diff --git a/11001.cpp b/11001.cpp index bdb3a65..72a5cec 100644 --- a/11001.cpp +++ b/11001.cpp @@ -1,42 +1,37 @@ -#include -#include - -using namespace std; - -int main() -{ - double Vt,V0,i=0,n=0,len=0,maxlen=0,k=0,j=0; - for(;;) - { - cin>>Vt>>V0; - if(Vt==0 && V0==0) - break; - if(Vt<=V0) - goto X; - - maxlen=0.3*sqrt(Vt-V0); - n=1; - for(i=1;i<=(Vt/V0);i++) - { - - len=0.3*i*sqrt(Vt/i-V0); - if(len>maxlen) - { - maxlen=len; - n=i; - } - } - for(j=1;j<=(Vt/V0);j++) - { - len=0.3*j*sqrt(Vt/j-V0); - - if(len==maxlen) - k++; - } - if(k>=2) - n=0; - -X: cout< + +using namespace std; + +int main() +{ + int total, zero; + while (cin >> total >> zero) + { + if (total == 0 && zero == 0) + { + break; + } + int i = 1; + double v = total * 1.0 / i; + double best = 0; + int result = 0; + while (v > zero) + { + double tmp = 0.3 * sqrt(v - zero); + if (abs(tmp * i - best) < 0.00000001) + { + result = 0; + break; + } + if (tmp * i > best) + { + best = tmp * i; + result = i; + } + i++; + v = total * 1.0 / i; + } + cout << result << endl; + } + return 0; +} diff --git a/11002.cpp b/11002.cpp new file mode 100644 index 0000000..e4247dc --- /dev/null +++ b/11002.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +int *T[32][32], PID = 0, min_[32][32], max_[32][32], X[32][32]; + +int main() +{ + int n, i, j, k; + for (i = 0; i < 32; i++) + { + for (j = 0; j < 32; j++) + { + T[i][j] = (int *)malloc(6001 * sizeof(short)); + memset(T[i][j], 0, sizeof(short)); + T[i][j] = T[i][j] + 3000; + } + } + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 2 * n - 2; i >= n; i--) + { + for (j = i - n + 1; j < n; j++) + { + scanf("%d", &X[i - j][j]); + } + } + for (i = n - 1; i >= 0; i--) + { + for (j = 0; j <= i; j++) + { + scanf("%d", &X[i - j][j]); + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + min_[i][j] = 0x7fffffff; + max_[i][j] = -0x7fffffff; + } + } + T[0][0][min_[0][0] = max_[0][0] = X[0][0]] = ++PID; + for (i = 0; i < n - 1; i++) + { + for (j = 0; j < n - 1; j++) + { + for (k = min_[i][j]; k <= max_[i][j]; k++) + { + if (T[i][j][k] != PID) + { + continue; + } + T[i + 1][j][k + X[i + 1][j]] = PID; + T[i + 1][j][k - X[i + 1][j]] = PID; + T[i][j + 1][k + X[i][j + 1]] = PID; + T[i][j + 1][k - X[i][j + 1]] = PID; + } + if (max_[i][j] + X[i + 1][j] > max_[i + 1][j]) + { + max_[i + 1][j] = max_[i][j] + X[i + 1][j]; + } + if (max_[i][j] - X[i + 1][j] > max_[i + 1][j]) + { + max_[i + 1][j] = max_[i][j] - X[i + 1][j]; + } + if (min_[i][j] + X[i + 1][j] < min_[i + 1][j]) + { + min_[i + 1][j] = min_[i][j] + X[i + 1][j]; + } + if (min_[i][j] - X[i + 1][j] < min_[i + 1][j]) + { + min_[i + 1][j] = min_[i][j] - X[i + 1][j]; + } + if (max_[i][j] + X[i][j + 1] > max_[i][j + 1]) + { + max_[i][j + 1] = max_[i][j] + X[i][j + 1]; + } + if (max_[i][j] - X[i][j + 1] > max_[i][j + 1]) + { + max_[i][j + 1] = max_[i][j] - X[i][j + 1]; + } + if (min_[i][j] + X[i][j + 1] < min_[i][j + 1]) + { + min_[i][j + 1] = min_[i][j] + X[i][j + 1]; + } + if (min_[i][j] - X[i][j + 1] < min_[i][j + 1]) + { + min_[i][j + 1] = min_[i][j] - X[i][j + 1]; + } + } + for (k = min_[i][j]; k <= max_[i][j]; k++) + { + if (T[i][j][k] != PID) + { + continue; + } + T[i + 1][j][k + X[i + 1][j]] = PID; + T[i + 1][j][k - X[i + 1][j]] = PID; + } + if (max_[i][j] + X[i + 1][j] > max_[i + 1][j]) + { + max_[i + 1][j] = max_[i][j] + X[i + 1][j]; + } + if (max_[i][j] - X[i + 1][j] > max_[i + 1][j]) + { + max_[i + 1][j] = max_[i][j] - X[i + 1][j]; + } + if (min_[i][j] + X[i + 1][j] < min_[i + 1][j]) + { + min_[i + 1][j] = min_[i][j] + X[i + 1][j]; + } + if (min_[i][j] - X[i + 1][j] < min_[i + 1][j]) + { + min_[i + 1][j] = min_[i][j] - X[i + 1][j]; + } + } + for (j = 0; j < n - 1; j++) + { + for (k = min_[i][j]; k <= max_[i][j]; k++) + { + if (T[i][j][k] != PID) + { + continue; + } + T[i][j + 1][k + X[i][j + 1]] = PID; + T[i][j + 1][k - X[i][j + 1]] = PID; + } + if (max_[i][j] + X[i][j + 1] > max_[i][j + 1]) + { + max_[i][j + 1] = max_[i][j] + X[i][j + 1]; + } + if (max_[i][j] - X[i][j + 1] > max_[i][j + 1]) + { + max_[i][j + 1] = max_[i][j] - X[i][j + 1]; + } + if (min_[i][j] + X[i][j + 1] < min_[i][j + 1]) + { + min_[i][j + 1] = min_[i][j] + X[i][j + 1]; + } + if (min_[i][j] - X[i][j + 1] < min_[i][j + 1]) + { + min_[i][j + 1] = min_[i][j] - X[i][j + 1]; + } + } + i = j = n - 1; + for (k = 0; k <= 3000; k++) + { + if (T[i][j][k] == PID || T[i][j][-k] == PID) + { + break; + } + } + printf("%d\n", k); + } + return 0; +} diff --git a/11003.cpp b/11003.cpp new file mode 100644 index 0000000..0761dd8 --- /dev/null +++ b/11003.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +/* + Problem: 11003 - Boxes + From the UVa Online Judge + + Author: Andrés Mejía-Posada + http://blogaritmo.factorcomun.org + + Method: Dynamic programming + Complexity: O(n^2) + + Accepted. + */ + +const int maxBoxes = 1000; +const int maxLoad = 3000; +int dp[maxBoxes][maxLoad + 1]; +/* + dp[i][j] = maximum amount of boxes that can be stacked using boxes 0..i (included) such that I can still + add j more weight units on top. + */ + +int main() +{ + int n; + while (cin >> n && n) + { + for (int i = 0; i < n; ++i) + for (int j = 0; j <= maxLoad; ++j) + { + dp[i][j] = 0; + } + for (int i = 0; i < n; ++i) + { + int w, l; + cin >> w >> l;//weight and load of box i-th + for (int j = 0; j <= maxLoad; ++j) + { + if (j <= l) + { + dp[i][j] = 1;//I can stack at least box i-th + } + //But maybe I can create a bigger stack with the same strength not using box i-th + if (i > 0) + { + dp[i][j] = max(dp[i][j], dp[i - 1][j]); + } + if (j + w <= maxLoad && j <= l && i > 0)//Now try to add box i-th on top of the previous boxes + { + dp[i][j] = max(dp[i][j], dp[i - 1][j + w] + 1); + } + } + } + cout << dp[n - 1][0] << endl; + } + return 0; +} diff --git a/11004.cpp b/11004.cpp new file mode 100644 index 0000000..aafd22a --- /dev/null +++ b/11004.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +const double eps = 1e-10; +#define NEG 0 +#define POS 1 +inline bool solve(int x, int y, int ai, int bi, int ci) +{ + if (ai * x + bi * y + ci < 0) + { + return NEG; + } + return POS; +} + +main() +{ + int tc = 1; + int n; + int a[3000], b[3000], c[3000]; + while (scanf("%d", &n) && n) + { + rep(i, n) cin >> a[i] >> b[i] >> c[i]; + int t; + int cnt = 0; + cin >> t; + rep(k, t) + { + int x, y; + int neg = 0, pos = 0; + scanf("%d%d", &x, &y); + rep(i, n) if (solve(x, y, a[i], b[i], c[i]) == NEG) + { + neg++; + } + else + { + pos++; + } + if (neg > 0) + { + neg--; + } + if (pos > 0) + { + pos--; + } + cnt += neg * (neg + 1) / 2 + pos * (pos + 1) / 2; + } + printf("Roadmap %d:\n", tc++); + printf("Negative Builders Ltd. will build %d New Roads.\n", cnt); + } + return false; +} diff --git a/11005.cpp b/11005.cpp new file mode 100644 index 0000000..1327f75 --- /dev/null +++ b/11005.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int C[40], P[40], min_; + +int main() +{ + int i, first = 1, t, tt, q, x, r, sum; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + for (i = 0; i < 36; i++) + { + scanf("%d", &C[i]); + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + scanf("%d", &q); + printf("Case %d:\n", t); + while (q--) + { + scanf("%d", &x); + printf("Cheapest base(s) for number %d:", x); + min_ = 0x7fffffff; + for (i = 2; i < 37; i++) + { + for (r = x, sum = 0; r; r /= i) + { + sum += C[r % i]; + } + P[i] = sum; + if (sum < min_) + { + min_ = sum; + } + } + for (i = 2; i < 37; i++) + { + if (P[i] == min_) + { + printf(" %d", i); + } + } + printf("\n"); + } + } + return 0; +} diff --git a/11006.cpp b/11006.cpp new file mode 100644 index 0000000..af737e4 --- /dev/null +++ b/11006.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +typedef pair P; + +bool cmp(const P &a, const P &b) +{ + return a.first * (long long)b.second > a.second * (long long)b.first; +} + +int main() +{ + int N; + while (scanf("%d", &N) == 1 && N != 0) + { + int sumx = 0, sumy = 0; + vector

v; + for (int ss = 1; sumx + sumy + ss <= N; ss++) + { + for (int a = 1; a < ss; a++) + { + int b = ss - a; + if (__gcd(a, b) != 1) + { + continue; + } + v.push_back(P(a, b)); + sumx += a; + sumy += b; + if (sumx + sumy + ss > N) + { + break; + } + } + } + sort(v.begin(), v.end(), cmp); + if (sumx + sumy < N) + { + v.insert(v.begin(), P(N - sumx - sumy, 0)); + sumx += N - sumx - sumy; + } + int curX = sumy, curY = 0; + printf("(%d,%d)\n", curX, curY); + for (int k = 0; k < 4; k++) + { + for (int i = 0; i < (int)v.size(); i++) + { + int dx = v[i].first, dy = v[i].second; + for (int j = 0; j < k; j++) + { + swap(dx, dy); + dx = -dx; + } + curX += dx; + curY += dy; + if (k == 3 && i == (int)v.size() - 1) + { + continue; + } + printf("(%d,%d)\n", curX, curY); + assert(0 <= curX && curX <= N); + assert(0 <= curY && curY <= N); + } + } + printf("\n"); + //fprintf(stderr, "<%d>\n", v.size()*4); + } +} diff --git a/11007.cpp b/11007.cpp new file mode 100644 index 0000000..6604b33 --- /dev/null +++ b/11007.cpp @@ -0,0 +1,315 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +typedef long long ll; + +#define TOP 0 +#define FRONT 1 +#define BOTTOM 2 +#define BACK 3 +#define LEFT 4 +#define RIGHT 5 + +//table for rotate +int table[12][12] = + { + TOP, FRONT, BOTTOM, BACK, 0, 1, 0, 1, 0, 1, 0, 1, + TOP, FRONT, BOTTOM, BACK, 2, 3, 2, 3, 2, 3, 2, 3, + TOP, BACK, BOTTOM, FRONT, 0, 1, 0, 1, 0, 1, 0, 1, + TOP, BACK, BOTTOM, FRONT, 2, 3, 2, 3, 2, 3, 2, 3, + + TOP, LEFT, BOTTOM, RIGHT, 0, 2, 0, 2, 3, 1, 0, 2, + TOP, LEFT, BOTTOM, RIGHT, 1, 3, 1, 3, 2, 0, 1, 3, + TOP, RIGHT, BOTTOM, LEFT, 0, 2, 0, 2, 3, 1, 0, 2, + TOP, RIGHT, BOTTOM, LEFT, 1, 3, 1, 3, 2, 0, 1, 3, + + FRONT, LEFT, BACK, RIGHT, 0, 2, 2, 3, 3, 1, 1, 0, + FRONT, LEFT, BACK, RIGHT, 1, 3, 0, 1, 2, 0, 3, 2, + FRONT, RIGHT, BACK, LEFT, 0, 2, 1, 0, 3, 1, 2, 3, + FRONT, RIGHT, BACK, LEFT, 1, 3, 3, 2, 2, 0, 0, 1, +}; + +int table2[12][5] = + { + LEFT, 2, 0, 3, 1, + RIGHT, 1, 3, 0, 2, + LEFT, 1, 3, 0, 2, + RIGHT, 2, 0, 3, 1, + + BACK, 2, 0, 3, 1, + FRONT, 1, 3, 0, 2, + BACK, 1, 3, 0, 2, + FRONT, 2, 0, 3, 1, + + TOP, 2, 0, 3, 1, + BOTTOM, 1, 3, 0, 2, + TOP, 1, 3, 0, 2, + BOTTOM, 2, 0, 3, 1, +}; + +int getp[6][4] = + { + 0, 2, 1, 3, + 0, 2, 1, 3, + 0, 2, 1, 3, + 3, 1, 2, 0, + 2, 3, 0, 1, + 1, 0, 3, 2, +}; + +class state +{ +public: + char in[6][4]; + int cnt; + ll val; + ll convert(); + + bool operator<(const state &a) const + { + return val < a.val; + } +}; + +ll state::convert() +{ + val = 0; + rep(i, 6) + { + rep(j, 4) + { + val = (val << 2); + val += in[i][j]; + } + } + return val; +} + +int make_all(state, bool, set &S, vector &a); +vector dummy; + +inline void rotate(int a, state &now) +{ + static int in[4]; + rep(i, 4) in[i] = table[a][i]; + char tmp[2] = {now.in[in[0]][table[a][4]], + now.in[in[0]][table[a][5]]}; + rep(i, 3) + { + now.in[in[i]][table[a][4 + i * 2]] = now.in[in[i + 1]][table[a][4 + (i + 1) * 2]]; + now.in[in[i]][table[a][4 + i * 2 + 1]] = now.in[in[i + 1]][table[a][4 + (i + 1) * 2 + 1]]; + } + now.in[in[3]][table[a][10]] = tmp[0]; + now.in[in[3]][table[a][11]] = tmp[1]; + rep(i, 4) in[i] = now.in[table2[a][0]][table2[a][i + 1]]; + rep(i, 4) now.in[table2[a][0]][i] = in[i]; +} + +bool cangoal(state &now, vector &in) +{ + rep(k, in.size()) + { + bool isok = true; + rep(i, 6) rep(j, 4) if (now.in[i][j] != in[k].in[i][j]) + { + isok = false; + } + if (isok) + { + return true; + } + } + return false; +} + +bool isin(state &next, set &S) +{ + int tmp = make_all(next, true, S, dummy); + if (tmp == -1) + { + return true; + } + else + { + return false; + } +} + +inline void make(state &in, queue &Q, set &S) +{ + rep(i, 6) + { + state next = in; + next.cnt++; + rotate(i * 2, next); + next.convert(); + if (isin(next, S)) + { + S.insert(next); + Q.push(next); + } + } +} + +void bfs2(state ini, set &S) +{ + int tmp = 0; + queue Q; + Q.push(ini); + while (!Q.empty()) + { + state now = Q.front(); + Q.pop(); + //if (now.cnt != tmp)tmp=now.cnt,cout << S.size() <<" " << tmp<= 7) + { + return; + } + make(now, Q, S); + } +} + +int bfs(state ini, vector &goal, set &S2) +{ + queue Q; + set S; + Q.push(ini); + S.insert(ini); + while (!Q.empty()) + { + state now = Q.front(); + Q.pop(); + if (cangoal(now, goal)) + { + return now.cnt; + } + int tmp = make_all(now, true, S2, dummy); + if (tmp != -1) + { + return tmp; + } + if (now.cnt == 6) + { + continue; + } + make(now, Q, S); + } + return 14; + return -1; +} + +main() +{ + state goal; + static int getinp[] = {TOP, FRONT, RIGHT, BOTTOM, BACK, LEFT}; + rep(i, 4) + { + goal.in[TOP][i] = 'W'; + goal.in[BOTTOM][i] = 'B'; + goal.in[FRONT][i] = 'R'; + goal.in[BACK][i] = 'O'; + goal.in[RIGHT][i] = 'Y'; + goal.in[LEFT][i] = 'G'; + } + goal.cnt = 0; + vector allgoal; + set S; + bfs2(goal, S); + make_all(goal, false, S, allgoal); + int te; + cin >> te; + while (te--) + { + state in; + in.cnt = 0; + rep(i, 6) + { + rep(j, 4) + { + cin >> in.in[getinp[i]][getp[getinp[i]][j]]; + } + } + cout << bfs(in, allgoal, S) << endl; + } +} + +int make_all(state now, bool checkSet, set &S, + vector &a) +{ + set::iterator itr; + rep(i, 4) + { + rep(j, 4) + { + //ret.push_back(now); + now.convert(); + if (checkSet) + { + itr = S.find(now); + if (itr != S.end()) + { + return now.cnt + itr->cnt; + } + } + else + { + a.push_back(now); + } + rotate(8, now); + rotate(9, now); + } + rotate(0, now); + rotate(1, now); + } + rotate(4, now); + rotate(5, now); + rep(i, 4) + { + // ret.push_back(now); + now.convert(); + if (checkSet) + { + itr = S.find(now); + if (itr != S.end()) + { + return now.cnt + itr->cnt; + } + } + else + { + a.push_back(now); + } + rotate(8, now); + rotate(9, now); + } + rep(i, 2) + { + rotate(4, now); + rotate(5, now); + } + rep(i, 4) + { + // ret.push_back(now); + now.convert(); + if (checkSet) + { + itr = S.find(now); + if (itr != S.end()) + { + return now.cnt + itr->cnt; + } + } + else + { + a.push_back(now); + } + rotate(8, now); + rotate(9, now); + } + return -1; +} diff --git a/11008.cpp b/11008.cpp new file mode 100644 index 0000000..24234ec --- /dev/null +++ b/11008.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +int S[70000], X[20], Y[20], L[300], len, N[70000] = {0}, C[20][20] = {0}, Q[20], ll; +int main() +{ + int t, tt, first = 1, n, m, i, j, k, l, limit, max, dx, dy, sign; + for (i = 0, limit = (1 << 16); i < limit; i++) + { + for (j = i; j; j >>= 1) + { + N[i] += (j & 1); + } + } + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d%d", &n, &m); + for (i = 0; i < n; i++) + { + scanf("%d%d", X + i, Y + i); + } + limit = (1 << n), len = 0; + for (i = 0; i < limit; i++) + { + S[i] = (N[i] + 1) / 2; + } + for (i = 0; i < n; i++) + { + for (j = i + 1; j < n; j++) + { + if (C[i][j] == t) + { + continue; + } + sign = ((1 << i) | (1 << j)); + dx = X[j] - X[i]; + dy = Y[j] - Y[i]; + for (k = j + 1, ll = 0; k < n; k++) + { + if ((X[k] - X[i]) * dy != (Y[k] - Y[i]) * dx) + { + continue; + } + sign |= (1 << k); + C[i][k] = C[j][k] = t; + Q[ll++] = k; + } + for (k = 0; k < ll; k++) + { + for (l = k + 1; l < ll; l++) + { + C[Q[k]][Q[l]] = t; + } + } + L[len++] = sign; + } + } + for (i = 0, max = (n + 1) / 2; i < limit; i++) + { + for (j = 0; j < len; j++) + { + k = L[j]; + sign = (i | k); + if (S[sign] > S[i] + 1) + { + S[sign] = S[i] + 1; + } + } + if (N[i] >= m && S[i] < max) + { + max = S[i]; + } + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + printf("Case #%d:\n%d\n", t, max); + } + return 0; +} diff --git a/11009.cpp b/11009.cpp new file mode 100644 index 0000000..4837160 --- /dev/null +++ b/11009.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main() +{ + int st, ed, i; + double a, b, r, aa; + while (scanf("%lf%d%d", &a, &st, &ed) == 3) + { + if (a == 0.0 && st == 0 && ed == 0) + { + break; + } + for (i = st, aa = 0.0; i <= ed; i++) + { + b = a + i; + r = sqrt(9 * a * a + 24 * b * b); + r = 2 * sqrt(a * a + 5 * b * b - b * r); + aa += r; + } + printf("%d\n", (int)(aa + 0.5)); + } + return 0; +} diff --git a/1101.cpp b/1101.cpp new file mode 100644 index 0000000..e653dfc --- /dev/null +++ b/1101.cpp @@ -0,0 +1,202 @@ +#include + +using namespace std; + +#define MAX 53 + +typedef long long ll; + +ll a, m, r, s, po[MAX], tmp[MAX], p, q, gl; +bool sol; + +struct s1 +{ + ll cnt; + char ch; +}; + +void btrack(ll rem, ll now, ll lim) +{ + ll i, j; + //if (gl==2) printf("%lld %lld %lld\n",now,rem,po[rem]); + if (rem == -1) + { + if (now >= r && now + (q - p) * po[gl] <= s) + { + sol = 1; + } + return; + } + if (now < r) + { + if ((r - now) % (po[rem] * a) == 0) + { + i = j = (r - now) / (po[rem] * a); + } + else + { + i = j = (r - now) / (po[rem] * a) + 1; + } + } + else + { + i = j = 0; + } + for (; i >= j - 1 && i >= 0; i--) + { + btrack(rem - 1, now + i * po[rem] * a, lim); + if (sol) + { + tmp[rem] = i; + break; + } + } +} + +vector gen() +{ + ll i; + vector ret; + s1 tmp1; + for (i = gl; i >= 0; i--) + { + tmp1.cnt = tmp[i]; + tmp1.ch = 'A'; + ret.push_back(tmp1); + if (i) + { + tmp1.cnt = 1; + tmp1.ch = 'M'; + ret.push_back(tmp1); + } + } + for (i = 0; i < ret.size(); i++) + { + if (ret[i].cnt == 0) + { + ret.erase(ret.begin() + i); + i--; + } + } + for (i = 0; i < (int)ret.size() - 1; i++) + { + while (i < (int)ret.size() - 1 && ret[i + 1].ch == ret[i].ch) + { + ret[i].cnt += ret[i + 1].cnt; + ret.erase(ret.begin() + i + 1); + } + } + return ret; +} + +bool lesser(vector &a, vector &b) +{ + ll sum1, sum2, i; + sum1 = sum2 = 0; + for (i = 0; i < a.size(); i++) + { + sum1 += a[i].cnt; + } + for (i = 0; i < b.size(); i++) + { + sum2 += b[i].cnt; + } + if (sum1 != sum2) + { + return sum1 < sum2; + } + for (i = 0; i < a.size() && i < b.size(); i++) + { + if (a[i].cnt == b[i].cnt && a[i].ch == b[i].ch) + { + continue; + } + if (a[i].cnt == b[i].cnt) + { + return a[i].ch < b[i].ch; + } + else if (a[i].cnt < b[i].cnt) + { + if (a[i].ch == b[i].ch) + { + return a[i].ch == 'M'; + } + return a[i].ch == 'A'; + } + else + { + if (a[i].ch == b[i].ch) + { + return a[i].ch == 'A'; + } + return a[i].ch == 'A'; + } + } +} + +int main() +{ + //freopen("text1.txt","r",stdin); + //freopen("my.txt","w",stdout); + ll i, init, cnt, j, lim, k, cas = 0; + bool d; + while (scanf("%lld%lld%lld%lld%lld%lld", &a, &m, &p, &q, &r, &s) == 6 && a) + { + vector cand, ans; + po[0] = 1; + for (i = 1; i <= 30; i++) + { + po[i] = po[i - 1] * m; + } + d = 0; + for (i = 0; i <= 30; i++) + { + //if (m==1&&i) break; + gl = i; + if (po[i] > s / p) + { + break; + } + init = po[i] * p; + if (q > p && po[i] > (s - init) / (q - p)) + { + break; + } + lim = s - po[i] * (q - p); + //printf("here %lld %lld %lld\n",i,init,lim); + sol = 0; + btrack(i, init, lim); + if (sol) + { + cand = gen(); + if (!d) + { + ans = cand; + d = 1; + } + else if (lesser(cand, ans)) + { + ans = cand; + } + } + } + if (!d) + { + printf("Case %lld: impossible\n", ++cas); + } + else if (ans.size() == 0) + { + printf("Case %lld: empty\n", ++cas); + } + else + { + printf("Case %lld:", ++cas); + for (i = 0; i < ans.size(); i++) + { + printf(" %lld%c", ans[i].cnt, ans[i].ch); + } + printf("\n"); + } + } + return 0; +} diff --git a/11010.cpp b/11010.cpp new file mode 100644 index 0000000..1e3cd46 --- /dev/null +++ b/11010.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +struct Board +{ + char a[3][3]; + + int encode() + { + int r = 0; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + { + r = r * 3 + a[i][j]; + } + return r; + } + + bool complete() + { + int r = 0; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if (a[i][j] != 0) + { + r++; + } + return r == 9; + } + + bool wins(int p) + { + for (int i = 0; i < 3; i++) + { + if (a[i][0] == p && a[i][1] == p && a[i][2] == p) + { + return true; + } + if (a[0][i] == p && a[1][i] == p && a[2][i] == p) + { + return true; + } + } + if (a[0][0] == p && a[1][1] == p && a[2][2] == p) + { + return true; + } + return a[0][2] == p && a[1][1] == p && a[2][0] == p; + } + + bool read() + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3;) + { + int c = getchar(); + if (c == EOF) + { + return false; + } + else if (c == '.') + { + a[i][j] = 0; + } + else if (c == 'O') + { + a[i][j] = 1; + } + else if (c == 'X') + { + a[i][j] = 2; + } + else + { + continue; + } + j++; + } + } + return true; + } +}; + +int memo[20000][2]; + +int perfect(Board &b, int st) +{ + int &ret = memo[b.encode()][st - 1]; + if (ret != -1) + { + return ret; + } + int w1 = b.wins(1), w2 = b.wins(2); + if (w1 && w2) + { + return ret = 0; + } + if (w1 || w2) + { + return ret = (w1 ? 1 : 2); + } + if (b.complete()) + { + return ret = 0; + } + ret = 3 - st; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + if (b.a[i][j] != 0) + { + continue; + } + b.a[i][j] = st; + int t = perfect(b, 3 - st); + b.a[i][j] = 0; + if (t == st) + { + return ret = st; + } + if (t == 0) + { + ret = 0; + } + } + } + return ret; +} + +int sc(int a, int b) +{ + if (a == 0) + { + return 0; + } + if (a == b) + { + return 1; + } + return -1; +} + +int main() +{ + Board b; + int a[1024], n; + memset(memo, 0xff, sizeof(memo)); + for (int cs = 1; scanf("%d", &n) == 1 && n >= 2; cs++) + { + int s = 0; + for (int i = 0; i < n; i++) + { + if (!b.read()) + { + return 0; + } + int mary = sc(perfect(b, 1), 1); + int john = sc(perfect(b, 2), 2); + a[i] = mary + john; + s -= john; + } + sort(a, a + n); + for (int i = n - 2; i >= 0; i -= 2) + { + s += a[i]; + } + printf("Case %d: ", cs); + if (s == 0) + { + printf("Draw.\n"); + } + else if (s > 0) + { + printf("Mary wins.\n"); + } + else + { + printf("Johnny wins.\n"); + } + } + return 0; +} diff --git a/11011.cpp b/11011.cpp new file mode 100644 index 0000000..01b4b8b --- /dev/null +++ b/11011.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int width[16], height[16]; +char out[40][32768]; + +void put(int y, int x, char *s, ...) +{ + static char buf[1024]; + va_list v; + va_start(v, s); + vsprintf(buf, s, v); + for (char *p = buf; *p;) + { + out[y][x++] = *p++; + } + va_end(v); +} + +void go(int y, int x, int a, int b) +{ + int n = b - a; + if (n == 1) + { + put(y, x, "f(x..)-f(x..)"); + put(y + 1, x + 3, "%.2d.....%.2d", a, b); + put(y + 2, x, "_____________"); + put(y + 3, x + 3, "x..-x"); + put(y + 4, x + 4, "%.2d..%.2d", a, b); + return; + } + int t = y + height[n] - 3; + for (int i = 0, w = width[n]; i < w; i++) + { + out[t][x + i] = '_'; + } + out[y + height[n] - 6][x + width[n - 1]] = '-'; + put(y + height[n] - 2, x + width[n - 1] - 3, "x..-x"); + put(y + height[n] - 1, x + width[n - 1] - 2, "%.2d..%.2d", a, b); + go(y, x, a, b - 1); + go(y, x + width[n - 1] + 1, a + 1, b); +} + +int main() +{ + width[1] = 13; + height[1] = 5; + for (int n = 2; n <= 12; n++) + { + width[n] = width[n - 1] * 2 + 1; + height[n] = height[n - 1] + 3; + } + for (int cs = 1, n; scanf("%d", &n) == 1 && n > 0; cs++) + { + printf("Case %d:\n", cs); + memset(out, '.', sizeof(out)); + go(0, 0, 0, n); + for (int i = 0; i < height[n]; i++) + { + out[i][width[n]] = '\n'; + fwrite(out[i], 1, width[n] + 1, stdout); + } + printf("\n"); + } +} diff --git a/11012.cpp b/11012.cpp new file mode 100644 index 0000000..01ce627 --- /dev/null +++ b/11012.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define INF 2147483647 + +int X[8] = {1, 1, 1, 1, -1, -1, -1, -1}, Y[8] = {1, 1, -1, -1, 1, 1, -1, -1}, Z[8] = {1, -1, 1, -1, 1, -1, 1, -1}, min_[8], max_[8]; + +int main() +{ + int i, j, t, tt, n, x, y, z, dist; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d", &n); + for (i = 0; i < 8; i++) + { + min_[i] = INF; + } + for (i = 0; i < 8; i++) + { + max_[i] = -INF; + } + for (i = 0; i < n; i++) + { + scanf("%d%d%d", &x, &y, &z); + for (j = 0; j < 8; j++) + { + dist = X[j] * x + Y[j] * y + Z[j] * z; + if (dist < min_[j]) + { + min_[j] = dist; + } + if (dist > max_[j]) + { + max_[j] = dist; + } + } + } + for (i = dist = 0; i < 8; i++) + { + if (max_[i] - min_[i] > dist) + { + dist = max_[i] - min_[i]; + } + } + printf("Case #%d: %d\n", t, dist); + } + return 0; +} diff --git a/11013.cpp b/11013.cpp new file mode 100644 index 0000000..2539f1b --- /dev/null +++ b/11013.cpp @@ -0,0 +1,195 @@ +#include + +using namespace std; + +char change_card[3], value[5], suit[5], rem_cards[13]; +bool stay; +void input(char *s) +{ + // puts(s); + for (int i = 0; i < 5; i++) + { + value[i] = s[3 * i]; + suit[i] = s[3 * i + 1]; + } +} +inline int encode(char c) +{ + if (c >= '2' && c <= '9') + { + return c - '1'; + } + else if (c == 'A') + { + return 0; + } + else if (c == 'T') + { + return 9; + } + else if (c == 'J') + { + return 10; + } + else if (c == 'Q') + { + return 11; + } + else + { + return 12; + } +} +inline char decode(int i) +{ + if (i >= 1 && i <= 8) + { + return i + '1'; + } + else if (i == 0) + { + return 'A'; + } + else if (i == 9) + { + return 'T'; + } + else if (i == 10) + { + return 'J'; + } + else if (i == 11) + { + return 'Q'; + } + else + { + return 'K'; + } +} +inline double payment(int runs[]) +{ + if (runs[5]) + { + return 100.0; + } + if (runs[4]) + { + return 10.0; + } + if (runs[3] && runs[2]) + { + return 5.0; + } + if (runs[3]) + { + return 3.0; + } + if (runs[2] == 2) + { + return 1.0; + } + return 0.0; +} +double can_get() +{ + int cards[13] = {0}, runs[6] = {0}; + int rest = 5; + for (int i = 0; i < 5; i++) + { + cards[encode(value[i])]++; + } + //for(int i=0;i<13;i++)printf("%d ",cards[i]); + int i, count = 0; + for (i = 0; cards[i] != 0; i++) + ; + while (rest) + { + if (cards[i]) + { + count++; + cards[i]--; + rest--; + } + else if (count) + { + runs[count]++; + count = 0; + } + i = (i + 1) % 13; + } + if (count) + { + runs[count]++; + } + //for(int i=0;i<=5;i++)printf("%d ",runs[i]);putchar(10); + //printf("result%.1lf \n",payment(runs)); + return payment(runs); +} +inline double max(double a, double b) +{ + return a > b ? a : b; +} +double expectation(int x) +{ + char t = value[x]; + double best, res = 0.0; + for (int i = 0; i < 13; i++) + if (rem_cards[i]) + { + value[x] = decode(i); + best = can_get() - 1.0; + res += (double)rem_cards[i] * best / 47; + } + value[x] = t; + return res; +} +void solve() +{ + stay = true; + double current = can_get(); + double expect; + for (int i = 0; i < 13; i++) + { + rem_cards[i] = 4; + } + for (int i = 0; i < 5; i++) + { + rem_cards[encode(value[i])]--; + } + for (int i = 0; i < 5; i++) + { + expect = expectation(i); + if (expect > current) + { + current = expect; + change_card[0] = value[i]; + change_card[1] = suit[i]; + stay = false; + } + } + change_card[2] = 0; +} +int main() +{ + char s[100]; + while (1) + { + gets(s); + if (s[0] == '#') + { + break; + } + input(s); + solve(); + if (stay) + { + printf("Stay\n"); + } + else + { + printf("Exchange %s\n", change_card); + } + } + return 0; +} diff --git a/11014.cpp b/11014.cpp new file mode 100644 index 0000000..c976dbf --- /dev/null +++ b/11014.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +typedef long long i64; + +i64 a[100001] = {0, 26}; + +int main() +{ + for (int n = 2, d, e; n <= 100000; n++) + { + a[n] = a[n - 1] + 24 * (i64)n * (i64)n - 24; + for (d = 2; d * d < n; d++) + if (n % d == 0) + { + e = n / d; + a[n] -= a[d] - a[d - 1]; + a[n] -= a[e] - a[e - 1]; + } + if (d * d == n) + { + a[n] -= a[d] - a[d - 1]; + } + } + i64 n; + for (int cs = 1; scanf("%lld", &n) == 1 && n > 0; cs++) + { + printf("Crystal %d: %lld\n", cs, a[n / 2]); + } +} diff --git a/11015.cpp b/11015.cpp index d8e5652..450ec36 100644 --- a/11015.cpp +++ b/11015.cpp @@ -1,80 +1,76 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - for(int t=1; ; t++){ - int N, M; - int i, j, k; - cin>>N; - if(N==0) - break; - cin>>M; - string name[N]; - for(i=0; i>name[i]; - int mat[N][N]; - for(i=0; i>i>>j>>val; - i--; j--; - mat[i][j] = val; - mat[j][i] = val; - } - for(k=0; k + +using namespace std; + +#define INF 100000000 +int main() +{ + int t, n, m, i, j, k, x, y, P[30][30], D[30], min; + char name[30][15]; + for (t = 1; scanf("%d%d", &n, &m) == 2; t++) + { + if ((n | m) == 0) + { + break; + } + for (i = 1; i <= n; i++) + { + scanf("%s", name[i]); + } + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + P[i][j] = INF; + } + } + while (m--) + { + scanf("%d%d%d", &i, &j, &k); + P[i][j] = P[j][i] = k; + } + for (j = 1; j <= n; j++) + { + for (i = 1; i <= n; i++) + { + if (P[i][j] == INF) + { + continue; + } + for (k = 1; k <= n; k++) + { + if (P[j][k] == INF || i == k) + { + continue; + } + if (P[i][j] + P[j][k] < P[i][k]) + { + P[i][k] = P[i][j] + P[j][k]; + } + } + } + } + min = 2 * INF; + for (i = 1; i <= n; i++) + { + D[i] = 0; + for (j = 1; j <= n; j++) + { + D[i] += P[i][j]; + } + if (D[i] < min) + { + min = D[i]; + } + } + for (i = 1; i <= n; i++) + { + if (min == D[i]) + { + break; + } + } + printf("Case #%d : %s\n", t, name[i]); + } + return 0; +} diff --git a/11016.cpp b/11016.cpp new file mode 100644 index 0000000..71966b9 --- /dev/null +++ b/11016.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +struct Point +{ + int x, y; + bool operator<(const Point &p) const + { + return y < p.y; + } +} P[200]; +int N; + +vector sweep[11000]; + +int Floor(int p, int q) +{ + if (q < 0) + { + p = -p; + q = -q; + } + return p >= 0 ? (p / q) : -((-p + q - 1) / q); +} + +int Ceil(int p, int q) +{ + if (q < 0) + { + p = -p; + q = -q; + } + return p >= 0 ? ((p + q - 1) / q) : -((-p) / q); +} + +int lineCeil(const Point &A, const Point &B, int x) +{ + return A.y + Ceil((B.y - A.y) * (x - A.x), B.x - A.x); +} + +int lineFloor(const Point &A, const Point &B, int x) +{ + return A.y + Floor((B.y - A.y) * (x - A.x), B.x - A.x); +} + +int main() +{ + while (scanf("%d", &N) == 1 && N > 0) + { + for (int i = 0; i < N; i++) + { + scanf("%d %d", &P[i].x, &P[i].y); + } + P[N] = P[0]; + for (int i = 0; i < 11000; i++) + { + sweep[i].clear(); + } + for (int i = 0; i < N; i++) + { + Point A = P[i], B = P[i + 1]; + if (A.x == B.x) + { + continue; + } + if (A.x > B.x) + { + swap(A, B); + } + for (int x = A.x; x < B.x; x++) + { + Point e; + e.x = -1; + e.y = max(lineCeil(A, B, x), lineCeil(A, B, x + 1)); + sweep[x].push_back(e); + e.x = +1; + e.y = min(lineFloor(A, B, x), lineFloor(A, B, x + 1)); + sweep[x].push_back(e); + //printf("A=%d,%d B=%d,%d at x=%d: y=%d..%d\n",A.x,A.y,B.x,B.y, x,lineFloor(A,B,x),lineCeil(A,B,x)); + } + } + int ans[2] = {0, 0}; + for (int x = 0; x < 10100; x++) + { + vector &s = sweep[x]; + if (s.size() == 0) + { + continue; + } + sort(s.begin(), s.end()); + // printf("x=%d..%d:\n", x, x+1); + int y0 = 0, lc = 0, ic = 0, m = s.size(); + for (int i = 0; i < m;) + { + // printf(" at y=%d: %+d%s\n", s[i].y, s[i].x, s[i].y==y0 ? "" : " *"); + if (s[i].y == y0) + { + lc += s[i].x; + if (s[i].x == -1) + { + ic++; + } + i++; + continue; + } + int y1 = s[i].y; + if (lc == 0 && ic % 2 == 1) + { + int col = (x + y0) % 2; + int n = y1 - y0; + // printf(" col=%d n=%d\n", col, n); + if (n % 2 == 1) + { + ans[col]++; + col ^= 1; + n--; + } + ans[0] += n / 2; + ans[1] += n / 2; + } + y0 = y1; + } + } + if (ans[0] < ans[1]) + { + swap(ans[0], ans[1]); + } + printf("%d %d\n", ans[0], ans[1]); + } +} diff --git a/11017.cpp b/11017.cpp new file mode 100644 index 0000000..5659bb5 --- /dev/null +++ b/11017.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define ABS(x) ((x) < 0 ? -(x) : (x)) +void trans(long long &x, long long &y) +{ + long long t = x - y; + y = x + y; + x = t; +} + +// I = (2A - B + 2) / 2 +long long greenA, greenB, redA, redB; + +void edge(long long x1, long long y1, long long x2, long long y2) +{ + greenA += x1 * y2 - x2 * y1; + greenB += __gcd(abs(x2 - x1), abs(y2 - y1)); + trans(x1, y1); + trans(x2, y2); + redA += x1 * y2 - x2 * y1; + redB += __gcd(abs(x2 - x1), abs(y2 - y1)); +} + +int main() +{ + int N, d, theta; + int x0, y0, x1, y1, x2, y2; + while (scanf("%d %d %d", &d, &theta, &N) == 3 && d > 0) + { + assert(N >= 3); + greenA = greenB = redA = redB = 0; + scanf(" %d %d", &x0, &y0); + x1 = x0; + y1 = y0; + for (int i = 1; i < N; i++) + { + scanf(" %d %d", &x2, &y2); + edge(x1, y1, x2, y2); + x1 = x2; + y1 = y2; + } + edge(x1, y1, x0, y0); + long long greenI = (abs(greenA) - greenB + 2) / 2; + long long redI = (abs(redA) - redB + 2) / 2; + //printf("green=%lld red+green=%lld\n", greenI, redI); + double area = abs(greenA) / 2.0 * d * d * sin(theta * 2 * acos(0.0) / 180.0); + if (fabs(area - floor(area)) < fabs(area - ceil(area))) + { + area = floor(area); + } + else + { + area = ceil(area); + } + printf("%lld %.0f\n", redI - greenI, area); + } +} diff --git a/11018.cpp b/11018.cpp new file mode 100644 index 0000000..3015a94 --- /dev/null +++ b/11018.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +struct Point +{ + double la, lo; +} sites[128]; +string names[128]; +int dist[128][128], bneck[128][128], N; + +void solve(int s, int t, int rng) +{ + if (rng < bneck[s][t]) + { + printf("No route for this range, minimum required range is %d km.\n", bneck[s][t]); + return; + } + int pred[128], seen[128], d[128]; + memset(seen, 0, sizeof(seen)); + memset(pred, 0xff, sizeof(pred)); + for (int i = 0; i < N; i++) + { + d[i] = 0x7fffffff; + } + pred[s] = s; + d[s] = 0; + for (int u, v;;) + { + for (u = -1, v = 0; v < N; v++) + if (!seen[v] && pred[v] >= 0 && (u < 0 || d[v] < d[u])) + { + u = v; + } + if (u < 0) + { + break; + } + seen[u] = 1; + for (v = 0; v < N; v++) + { + if (!seen[v] && dist[u][v] <= rng && d[u] + dist[u][v] < d[v]) + { + pred[v] = u; + d[v] = d[u] + dist[u][v]; + } + } + } + int path[128] = {t}, p = 1; + for (int u = t; u != s;) + { + u = pred[u]; + path[p++] = u; + } + for (int D = 0; p > 0;) + { + p--; + printf("%-20s at %5d km.\n", names[path[p]].c_str(), D); + if (p > 0) + { + D += dist[path[p]][path[p - 1]]; + } + } +} + +double gcdist(const Point &a, const Point &b, double R) +{ + double u = (1. - cos(b.la - a.la)) / 2.; + double v = cos(a.la) * cos(b.la) * (1. - cos(b.lo - a.lo)) / 2.; + return 2 * R * atan2(sqrt(u + v), sqrt(1 - u - v)); +} + +int main() +{ + char buf[1024], *hyp = "------------------------------"; + for (int cs = 1; scanf("%d", &N) == 1 && N > 0; cs++) + { + printf("%sScenario %d:\n%s\n", cs == 1 ? "" : "\n", cs, hyp); + for (int i = 0; i < N; i++) + { + scanf(" %s %lf %lf", buf, &sites[i].la, &sites[i].lo); + names[i] = string(buf); + } + for (int i = 0; i < N; i++) + { + dist[i][i] = 0; + for (int j = 0; j < i; j++) + dist[j][i] = dist[i][j] = (int)floor( + .5 + gcdist(sites[i], sites[j], 3390)); + } + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + bneck[i][j] = dist[i][j]; + } + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + bneck[i][j] = MIN(bneck[i][j], MAX(bneck[i][k], bneck[k][j])); + } + int m, rng; + char src[1024], dst[1024]; + for (scanf("%d", &m); m-- > 0 && scanf(" %s %s %d", src, dst, &rng) == 3;) + { + printf("From %s to %s with range %d km:\n", src, dst, rng); + int u = find(names, names + N, src) - names; + int v = find(names, names + N, dst) - names; + solve(u, v, rng); + printf("%s\n", hyp); + } + } +} diff --git a/11019.cpp b/11019.cpp new file mode 100644 index 0000000..5ec0b8b --- /dev/null +++ b/11019.cpp @@ -0,0 +1,202 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 1000; +const int M = 100; +char m[N][N + 20]; +char in[M][M + 20]; +char *ptr[N]; +int atm[N][N + 20]; + +struct PMA +{ + PMA *next[30];//next[0] is for fail, 0x100 = 256 + int id; + vector ac; + PMA() + { + fill(next, next + 30, (PMA *)0); + id = -1; + } +}; + +PMA *buildPMA(char *p[M], int size, int *acdata) +{ + PMA *root = new PMA; + int cnt = 0; + rep(i, size) + { + PMA *t = root; + for (int j = 0; p[i][j] != '\0'; j++) + { + char c = p[i][j] - 'a' + 1; + if (t->next[c] == NULL) + { + t->next[c] = new PMA; + } + t = t->next[c]; + } + if (t->id == -1) + { + t->id = cnt++; + } + acdata[i] = t->id; + t->ac.push_back(t->id); + } + queue Q; + REP(i, 1, 30) + { + char c = i; + if (root->next[c]) + { + root->next[c]->next[0] = root; + Q.push(root->next[c]); + } + else + { + root->next[c] = root; + } + } + while (!Q.empty()) + { + PMA *t = Q.front(); + Q.pop(); + for (int c = 'a' - 'a' + 1; c <= 'z' - 'a' + 1; c++) + { + if (t->next[c]) + { + Q.push(t->next[c]); + PMA *r = t->next[0]; + while (!r->next[c]) + { + r = r->next[0]; + } + t->next[c]->next[0] = r->next[c]; + copy(r->next[c]->ac.begin(), r->next[c]->ac.end(), + back_inserter(t->next[c]->ac)); + } + } + } + return root; +} + +void match(PMA *r, char *tar, int *res) +{ + for (int i = 0; tar[i] != '\0'; i++) + { + char c = tar[i] - 'a' + 1; + while (!r->next[c]) + { + r = r->next[0]; + } + r = r->next[c]; + rep(j, r->ac.size()) + { + //res[r->ac[j]]++; + res[i] = r->id;//ac[j]; + } + } +} + +int kmp(int p1, int *text, int p2, int *word) +{ + static int table[N]; + int i = 2, j = 0;//i:searching word j:backtracking point + table[0] = -1; // + table[1] = 0; // + while (i <= p2) //make table + { + if (word[i - 1] == word[j]) + { + table[i] = j + 1; + i++; + j++; + } + else if (j > 0) + { + j = table[j]; + } + else + { + table[i] = 0; + i++; + } + } + //the main part of KMP algorithm + int pos; // + int m = 0;// + int cnt = 0; + i = 0;// + while (m + i < p1) + { + if (word[i] == text[m + i]) + { + i++; + if (i == p2) + { + //return m; + cnt++; + m = m + i - table[i]; + if (i > 0) + { + i = table[i]; + } + } + } + else + { + m = m + i - table[i]; + if (i > 0) + { + i = table[i]; + } + } + } + return cnt; +} + +int baker_bird(int r, int c, int p) +{ + int ret = 0; + static int ac[M + 10], tres[N + 10]; + rep(i, p) ptr[i] = in[i]; + PMA *root = buildPMA(ptr, p, ac); + rep(i, r) + { + rep(j, c) tres[j] = -1; + match(root, m[i], tres); + rep(j, c) atm[i][j] = tres[j]; + } + rep(j, c) + { + rep(i, r) tres[i] = atm[i][j]; + ret += kmp(r, tres, p, ac); + } + return ret; +} + +main() +{ + int te; + scanf("%d", &te); + while (te--) + { + int r, c, pr, pc; + scanf("%d%d", &r, &c); + rep(i, r) + { + scanf("%s", m[i]); + } + scanf("%d%d", &pr, &pc); + rep(i, pr) + { + scanf("%s", in[i]); + } + cout << baker_bird(r, c, pr) << endl; + } + return 0; +} diff --git a/11020.cpp b/11020.cpp new file mode 100644 index 0000000..b62ba6b --- /dev/null +++ b/11020.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +struct point +{ + int x, y; + bool operator<(const point &t) const + { + return x < t.x || (x == t.x && y < t.y); + } +}; +multiset s; +multiset::iterator it; + +int main() +{ + int i, j, cas, n; + scanf("%d", &cas); + for (int ca = 1; ca <= cas; ca++) + { + scanf("%d", &n); + int x, y; + s.clear(); + printf("Case #%d:\n", ca); + while (n--) + { + scanf("%d%d", &x, &y); + point p = (point){ + x, y}; + it = s.lower_bound(p); + if (it == s.begin() || (--it)->y > y) + { + s.insert(p); + it = s.upper_bound(p); + while (it != s.end() && it->y >= y) + { + s.erase(it++); + } + } + printf("%d\n", s.size()); + } + if (ca != cas) + { + puts(""); + } + } + return 0; +} diff --git a/11021.cpp b/11021.cpp new file mode 100644 index 0000000..3ca2602 --- /dev/null +++ b/11021.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +double p[2010], f[2010]; +int main() +{ + int sec; + scanf("%d", &sec); + for (int z = 1; z <= sec; z++) + { + int n, m, k; + scanf("%d%d%d", &n, &k, &m); + for (int i = 0; i <= n - 1; i++) + { + scanf("%lf", &p[i]); + } + if (k == 0) + { + printf("Case #%d: %.7f\n", z, (double)0); + continue; + } + memset(f, 0, sizeof(f)); + f[0] = 0; + f[1] = p[0]; + for (int i = 2; i <= m; i++) + { + f[i] = p[0]; + for (int j = 1; j <= n - 1; j++) + { + f[i] += p[j] * pow(f[i - 1], j); + } + } + double ans = pow(f[m], k); + printf("Case #%d: %.7f\n", z, ans); + } + return 0; +} diff --git a/11022.cpp b/11022.cpp new file mode 100644 index 0000000..407298d --- /dev/null +++ b/11022.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +char str[100]; +int dp[100][100]; +int run(int left, int right) +{ + if (left == right) + { + return 1; + } + if (dp[left][right]) + { + return dp[left][right]; + } + int length = right - left + 1; + for (int len = 1; len < length; len++) + { + if (length % len != 0) + { + continue; + } + int t = length / len; + bool flag = true; + for (int j = 1, base = left + len; j < t && flag; j++, base += len) + for (int l = 0; l < len && flag; l++) + if (str[left + l] != str[base + l]) + { + flag = false; + } + if (flag) + { + int s = run(left, left + len - 1); + if (!dp[left][right]) + { + dp[left][right] = s; + return s; + } + } + } + if (dp[left][right]) + { + return dp[left][right]; + } + for (int k = left; k < right; k++) + { + int s = run(left, k) + run(k + 1, right); + if (!dp[left][right] || dp[left][right] > s) + { + dp[left][right] = s; + if (s == 1) + { + return s; + } + } + } + return dp[left][right]; +} +int main() +{ + while (scanf("%s", str) == 1) + { + if (strcmp(str, "*") == 0) + { + break; + } + memset(dp, 0, sizeof(dp)); + int ans = run(0, strlen(str) - 1); + printf("%d\n", ans); + } +} diff --git a/11023.cpp b/11023.cpp new file mode 100644 index 0000000..80890b1 --- /dev/null +++ b/11023.cpp @@ -0,0 +1,1490 @@ +#include +#include + +using namespace std; + +struct BigInt +{ + int size, alloc; + unsigned *data, _data[5]; + + typedef unsigned long long uint64; + static inline int sgn(int n) + { + return n == 0 ? 0 : (n < 0 ? -1 : 1); + } + + // Removes leading zeroes + void normalize() + { + int n = abs(size); + while (n > 0 && data[n - 1] == 0) + { + n--; + } + size = (size < 0 ? -n : n); + } + + static int absCmp(const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn != yn) + { + return sgn(xn - yn); + } + for (int i = xn - 1; i >= 0; i--) + if (x.data[i] != y.data[i]) + { + return x.data[i] > y.data[i] ? +1 : -1; + } + return 0; + } + + // z = abs(x) + abs(y); + static void absAdd(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn < yn) + { + absAdd(z, y, x); + return; + } + int zn = max(xn, yn); + z.reserve(zn + 1); + uint64 c = 0; + for (int i = 0; i < yn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i] + (uint64)y.data[i]) & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 0 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[zn++] = (unsigned)c; + } + z.size = zn; + } + + // z = abs(x) + abs(y) + static void absAdd1(BigInt &z, const BigInt &x, unsigned y) + { + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = y; + if (&z == &x) + { + for (int i = 0; c != 0 && i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = 0; i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = n; + } + + // z = abs(x) - abs(y) + static void absSub(BigInt &z, const BigInt &x, const BigInt &y) + { + int t = absCmp(x, y); + if (t <= 0) + { + if (t == 0) + { + z.size = 0; + } + else + { + absSub(z, y, x); + } + z.size = -z.size; + return; + } + int xn = abs(x.size), yn = abs(y.size); + z.reserve(max(xn, yn)); + uint64 c = 1; + for (int i = 0; i < yn; i++, c >>= 32) + { + c += (uint64)x.data[i] + ((uint64)y.data[i] ^ 0xFFFFFFFFULL); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + assert(c == 1); + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + assert(z.size > 0); + } + + // z = abs(x) - abs(y) + static void absSub1(BigInt &z, const BigInt &x, unsigned y) + { + if (y == 0) + { + z.assign(x); + z.size = abs(z.size); + return; + } + if (x.size == 0) + { + z.size = -1; + z.data[0] = y; + return; + } + int xn = abs(x.size); + if (xn == 1) + { + if (x.data[0] > y) + { + z.size = 1; + z.data[0] = x.data[0] - y; + } + else if (x.data[0] == y) + { + z.size = 0; + } + else + { + z.size = -1; + z.data[0] = y - x.data[0]; + } + return; + } + z.reserve(xn); + uint64 c = 1 + (uint64)x.data[0] + (y ^ 0xFFFFFFFFULL); + z.data[0] = (unsigned)c; + c >>= 32; + if (&z == &x) + { + for (int i = 1; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + else + { + for (int i = 1; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + } + + // z = abs(x) * m + a + static void absMulAdd1(BigInt &z, const BigInt &x, unsigned m, unsigned a) + { + int n = abs(x.size); + z.reserve(n + 2); + uint64 c = a; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)m); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + while (c != 0) + { + z.data[n++] = (unsigned)c; + c >>= 32; + } + z.size = n; + } + + // z = x + sign*y. Asserts: abs(sign) = 1 + static void add(BigInt &z, const BigInt &x, int sign, const BigInt &y) + { + int xs = sgn(x.size), ys = sign * sgn(y.size); + if (xs == 0) + { + z.assign(y); + z.size *= sign; + } + else if (ys == 0) + { + z.assign(x); + } + else if (xs == ys) + { + absAdd(z, x, y); + z.size *= xs; + } + else if (ys < 0) + { + absSub(z, x, y); + } + else + { + absSub(z, x, y); + z.size = -z.size; + } + } + + static void add1s(BigInt &z, const BigInt &x, int y) + { + int xs = (x.size >= 0 ? +1 : -1), ys = (y >= 0 ? +1 : -1); + if (xs == ys) + { + absAdd1(z, x, abs(y)); + z.size *= xs; + } + else if (ys < 0) + { + absSub1(z, x, -y); + } + else + { + absSub1(z, x, y); + z.size = -z.size; + } + } + + static void mul1s(BigInt &z, const BigInt &x, int y) + { + if (y < 0) + { + mul1s(z, x, -y); + z.size = -z.size; + return; + } + if (y == 0) + { + z.size = 0; + return; + } + if (y == 1) + { + z.assign(x); + return; + } + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = 0; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)y); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = (x.size < 0 ? -n : n); + } + + static void mulQuadratic(BigInt &z, const BigInt &x, const BigInt &y) + { + if (&z == &x || &z == &y) + { + BigInt t; + mulQuadratic(t, x, y); + z = t; + return; + } + int xn = abs(x.size), yn = abs(y.size), zn = xn + yn + 1; + z.reserve(zn); + for (int i = 0; i < zn; i++) + { + z.data[i] = 0; + } + for (int i = 0; i < xn; i++) + { + uint64 c = 0; + int k = i; + for (int j = 0; j < yn; j++, k++, c >>= 32) + { + c += z.data[k] + x.data[i] * (uint64)y.data[j]; + z.data[k] = (unsigned)(c & 0xFFFFFFFFU); + } + for (; c != 0; k++, c >>= 32) + { + z.data[k] = (unsigned)((c += z.data[k]) & 0xFFFFFFFFU); + } + } + z.size = zn * sgn(x.size) * sgn(y.size); + z.normalize(); + } + + static void mulKaratsuba(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size), zs = sgn(x.size) * sgn(y.size); + int w = max(xn, yn) >> 1; + BigInt A(x.data + w, max(0, xn - w)), B(x.data, min(xn, w)); + BigInt C(y.data + w, max(0, yn - w)), D(y.data, min(yn, w)); + BigInt R, T; + absAdd(z, A, B); + absAdd(T, C, D); + mul(R, z, T); + mul(z, A, C); + mul(T, B, D); + R -= z; + R -= T; + R <<= w * 32; + z <<= w * 64; + z += R; + z += T; + z.size *= zs; + } + + BigInt(unsigned a[], int n) + { + if (n < 0) + { + n = 0; + } + while (n > 0 && a[n - 1] == 0) + { + n--; + } + data = NULL; + reserve(n); + size = n; + memcpy(data, a, n * sizeof(unsigned)); + } + + // q = abs(x) div abs(y); Returns abs(x) mod abs(y) + static unsigned absDiv1(BigInt &q, const BigInt &x, unsigned y) + { + assert(y != 0); + int n = abs(x.size); + q.reserve(n); + uint64 c = 0; + for (int i = n - 1; i >= 0; i--) + { + c = (c << 32) | (uint64)x.data[i]; + q.data[i] = (unsigned)(c / y); + c %= y; + } + q.size = n; + q.normalize(); + return (unsigned)c; + } + + // u = abs(u) mod abs(v), q = abs(u) div abs(v) + static void absDiv(BigInt *q, BigInt &u, BigInt v) + { + if (q != NULL && q == &u) + { + BigInt t; + absDiv(&t, u, v); + *q = t; + return; + } + u.size = abs(u.size); + v.size = abs(v.size); + assert(v.size > 0); + if (u.size < v.size) + { + if (q) + { + *q = 0; + } + return; + } + if (v.size == 1) + { + unsigned t = absDiv1(q == NULL ? u : *q, u, v.data[0]); + u.data[0] = t; + u.size = (t == 0 ? 0 : 1); + return; + } + int n = v.size, d = 0; + while (((v.data[n - 1] << d) & 0x80000000U) == 0) + { + d++; + } + u <<= d; + v <<= d; + unsigned vh = v.data[n - 1], vh2 = v.data[n - 2]; + uint64 c, g; + u.reserve(u.size + 1); + u.data[u.size] = 0; + v.reserve(v.size + 1); + v.data[v.size] = 0; + int qp = u.size - v.size + 1; + if (q) + { + q->reserve(qp + 1); + q->size = qp; + } + for (int up = u.size - 1; --qp >= 0; up--) + { + c = (((uint64)u.data[up + 1]) << 32) | (uint64)u.data[up]; + g = c / (uint64)vh; + if (g > 0xFFFFFFFFULL) + { + g = 0xFFFFFFFFULL; + } + while ((c - g * vh) < 0x100000000ULL && + (((c - g * vh) << 32) + u.data[up - 1]) < (g * (uint64)vh2)) + { + g--; + } + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++) + { + c += g * (uint64)v.data[j]; + if (u.data[i] >= (unsigned)(c & 0xFFFFFFFFULL)) + { + u.data[i] -= (unsigned)(c & 0xFFFFFFFFULL); + c >>= 32; + } + else + { + u.data[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL)); + c = (c >> 32) + 1; + } + } + if (c != 0) + { + g--; + assert(c == 1); + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++, c >>= 32) + { + c += (uint64)u.data[i] + (uint64)v.data[j]; + u.data[i] = (unsigned)c; + } + assert(c == 1); + } + if (q) + { + q->data[qp] = (unsigned)g; + } + } + u >>= d; + v >>= d; + if (q) + { + q->normalize(); + } + } + +public: + void reserve(int n) + { + if (data == NULL) + { + data = _data; + alloc = sizeof(_data) / sizeof(_data[0]); + } + if (alloc < n) + { + while (alloc < n) + { + alloc <<= 1; + } + if (data == _data) + { + data = (unsigned *)malloc(alloc * sizeof(unsigned)); + assert(data != NULL); + memcpy(data, _data, sizeof(_data)); + } + else + { + data = (unsigned *)realloc(data, alloc * sizeof(unsigned)); + assert(data != NULL); + } + } + } + + ~BigInt() + { + if (data != _data) + { + free(data); + } + } + + void swap(BigInt &y) + { + if (data != _data && y.data != y._data) + { + std::swap(size, y.size); + std::swap(alloc, y.alloc); + std::swap(data, y.data); + } + else + { + BigInt t(*this); + assign(y); + y.assign(t); + } + } + + static int cmp(const BigInt &x, const BigInt &y) + { + if (x.size != y.size) + { + return sgn(x.size - y.size); + } + return absCmp(x, y) * (x.size < 0 ? -1 : 1); + } + + static int cmp1s(const BigInt &x, int y) + { + if (y == 0) + { + return sgn(x.size); + } + if (y > 0) + { + if (x.size <= 0) + { + return -1; + } + if (x.size > 1) + { + return +1; + } + if (x.data[0] == (unsigned)y) + { + return 0; + } + return x.data[0] > (unsigned)y ? +1 : -1; + } + else + { + if (x.size >= 0) + { + return +1; + } + if (x.size < -1) + { + return -1; + } + if (x.data[0] == (unsigned)(-y)) + { + return 0; + } + return x.data[0] > (unsigned)(-y) ? -1 : +1; + } + } + + static void add(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, +1, y); + } + + static void sub(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, -1, y); + } + + static void mul(BigInt &z, const BigInt &x, const BigInt &y) + { + if (max(abs(x.size), abs(y.size)) < 64) + { + mulQuadratic(z, x, y); + } + else + { + mulKaratsuba(z, x, y); + } + } + + static void mod(BigInt &r, const BigInt &u, const BigInt &v) + { + r = u; + absDiv(NULL, r, v); + } + + static void div(BigInt &q, BigInt &r, const BigInt &u, const BigInt &v) + { + int us = sgn(u.size), vs = sgn(v.size); + r = u; + absDiv(&q, r, v); + //TODO + if (us * vs < 0) + { + q.size = -q.size; + } + } + + void assign(int n) + { + reserve(1); + size = sgn(n); + data[0] = abs(n); + } + + void assign(long long n) + { + reserve(2); + if (n < 0) + { + size = -2; + n = -n; + } + else + { + size = 2; + } + data[0] = (unsigned)(n & 0xFFFFFFFFU); + data[1] = (unsigned)(n >> 32); + normalize(); + } + + void assign(const BigInt &b) + { + if (this == &b) + { + return; + } + int n = abs(b.size); + reserve(n); + size = b.size; + memcpy(data, b.data, n * sizeof(unsigned)); + } + + void assign(const char *s, int radix = 10) + { + assert(2 <= radix && radix <= 36); + while (isspace(*s)) + { + s++; + } + int sign = 1; + if (*s == '+') + { + s++; + } + else if (*s == '-') + { + s++; + sign = -1; + } + while (*s == '0') + { + s++; + } + int n = 0; + for (n = 0; s[n] && isalnum(s[n]); n++) + ; + size = 0; + if (n > 20) + { + reserve((int)(log((double)radix) / log(2.0) / 32.0 * n) + 2); + } + else + { + reserve(n / 9 + 2); + } + unsigned a = 0, m = 1; + for (int i = 0; i < n; i++) + { + int dig = (isdigit(s[i]) ? (s[i] - '0') : (toupper(s[i]) - 'A' + 10)); + assert(dig < radix); + a = a * radix + dig; + m *= radix; + if (m > 0x3000000) + { + absMulAdd1(*this, *this, m, a); + a = 0; + m = 1; + } + } + if (m > 1) + { + absMulAdd1(*this, *this, m, a); + } + size *= sign; + } + + BigInt(int n = 0) + { + data = NULL; + assign(n); + } + BigInt(int n, int cap) + { + data = NULL; + reserve(cap); + assign(n); + } + BigInt(long long n) + { + data = NULL; + assign(n); + } + BigInt(const BigInt &b) + { + data = NULL; + assign(b); + } + BigInt(const char *s, int radix = 10) + { + data = NULL; + assign(s, radix); + } + BigInt(const string &s, int radix = 10) + { + data = NULL; + assign(s.c_str(), radix); + } + + BigInt &operator=(int n) + { + assign(n); + return *this; + } + BigInt &operator=(long long n) + { + assign(n); + return *this; + } + BigInt &operator=(const BigInt &b) + { + assign(b); + return *this; + } + BigInt &operator=(const char *s) + { + assign(s); + return *this; + } + BigInt &operator=(const string &s) + { + assign(s.c_str()); + return *this; + } + BigInt &operator+=(const BigInt &y) + { + add(*this, *this, +1, y); + return *this; + } + BigInt &operator-=(const BigInt &y) + { + add(*this, *this, -1, y); + return *this; + } + BigInt &operator*=(const BigInt &y) + { + mul(*this, *this, y); + return *this; + } + BigInt &operator/=(const BigInt &y) + { + BigInt q, r; + div(q, r, *this, y); + assign(q); + return *this; + } + BigInt &operator%=(const BigInt &y) + { + mod(*this, *this, y); + return *this; + } + BigInt &operator+=(int y) + { + add1s(*this, *this, y); + return *this; + } + BigInt &operator-=(int y) + { + add1s(*this, *this, -y); + return *this; + } + BigInt &operator*=(int y) + { + mul1s(*this, *this, y); + return *this; + } + BigInt &operator<<=(int n) + { + shl(n); + return *this; + } + BigInt &operator>>=(int n) + { + shr(n); + return *this; + } + void operator++() + { + add1s(*this, *this, 1); + } + void operator--() + { + add1s(*this, *this, -1); + } + BigInt operator-() + { + BigInt z = *this; + z.negate(); + return z; + } + BigInt operator>>(int n) const + { + BigInt r = *this; + r.shr(n); + return r; + } + BigInt operator<<(int n) const + { + BigInt r = *this; + r.shl(n); + return r; + } + + string str(int radix = 10) const + { + assert(2 <= radix && radix <= 36); + if (size == 0) + { + return "0"; + } + int y = 1, m = 0; + while (y < 0x300000) + { + y *= radix; + m++; + } + BigInt x(*this); + x.size = abs(x.size); + char *tmp = (char *)malloc(x.size * (radix >= 10 ? 10 : 32) + 10); + int n = 0; + while (x.size != 0) + { + unsigned r = absDiv1(x, x, y); + for (int i = 0; i < m; i++, r /= radix) + { + tmp[n++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[r % radix]; + } + } + while (n > 0 && tmp[n - 1] == '0') + { + n--; + } + if (size < 0) + { + tmp[n++] = '-'; + } + reverse(tmp, tmp + n); + tmp[n] = 0; + string res = string(tmp); + free(tmp); + return res; + } + + string toString(int r = 10) const + { + return str(r); + } + int toInt() const + { + return sgn(size) * (int)data[0]; + } + long long toLL() const + { + long long r = 0; + for (int i = 0; i < 2 && i < abs(size); i++) + { + r = (r << 32) | data[i]; + } + return size < 0 ? -r : r; + } + + void setBit(int n) + { + int s = abs(size), m = (n >> 5) + 1; + reserve(m); + while (s < m) + { + data[s++] = 0; + } + size = (size < 0 ? -s : s); + data[n >> 5] |= 1U << (n & 31); + } + + void clrBit(int n) + { + if (abs(size) > (n >> 5)) + { + data[n >> 5] &= ~(1U << (n & 31)); + normalize(); + } + } + int getBit(int n) const + { + return (abs(size) > (n >> 5)) ? ((data[n >> 5] >> (n & 31)) & 1) : 0; + } + + // Returns 1+(index of highest non-zero bit) + int bitSize() const + { + if (size == 0) + { + return 0; + } + int n = (abs(size) - 1) * 32; + unsigned t = data[abs(size) - 1]; + if (t >= 65536) + { + n += 16; + t >>= 16; + } + if (t >= 256) + { + n += 8; + t >>= 8; + } + if (t >= 16) + { + n += 4; + t >>= 4; + } + if (t >= 4) + { + n += 2; + t >>= 2; + } + if (t >= 2) + { + n++; + t >>= 1; + } + if (t >= 1) + { + n++; + } + return n; + } + + void shl(int s) + { + if (size == 0) + { + return; + } + if (s < 0) + { + shr(-s); + } + int n = abs(size), w = s >> 5; + reserve(n + w + 1); + if (w > 0) + { + for (int i = n - 1; i >= 0; i--) + { + data[i + w] = data[i]; + } + for (int i = w - 1; i >= 0; i--) + { + data[i] = 0; + } + n += w; + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + data[n++] = 0; + for (int i = 0; i < n; i++) + { + b = data[i] >> (32 - s); + data[i] = (data[i] << s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void shr(int s) + { + if (s < 0) + { + shl(-s); + } + int n = abs(size), w = s >> 5; + if (w > 0) + { + for (int i = 0; i + w < n; i++) + { + data[i] = data[i + w]; + } + n -= w; + if (n < 0) + { + n = 0; + } + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + for (int i = n - 1; i >= 0; i--) + { + b = data[i] << (32 - s); + data[i] = (data[i] >> s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void negate() + { + size = -size; + } + int sign() const + { + return sgn(size); + } + int compareTo(const BigInt &y) const + { + return cmp(*this, y); + } + int compareTo(int y) const + { + return cmp1s(*this, y); + } +}; +BigInt operator+(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::add(z, x, y); + return z; +} +BigInt operator-(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::sub(z, x, y); + return z; +} +BigInt operator*(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::mul(z, x, y); + return z; +} +BigInt operator/(const BigInt &x, const BigInt &y) +{ + BigInt q, r; + BigInt::div(q, r, x, y); + return q; +} +BigInt operator%(const BigInt &x, const BigInt &y) +{ + BigInt r; + BigInt::mod(r, x, y); + return r; +} +bool operator==(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) == 0; +} +bool operator==(const BigInt &x, int y) +{ + return BigInt::cmp1s(x, y) == 0; +} +bool operator!=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) != 0; +} +bool operator<(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) < 0; +} +bool operator<=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) <= 0; +} +bool operator>(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) > 0; +} +bool operator>=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) >= 0; +} +namespace std +{ +template <> +inline void swap(BigInt &a, BigInt &b) +{ + a.swap(b); +} +} + +typedef long long ll; +ll fact[32]; +BigInt big_fact[32]; + +struct seq +{ + int data[22], n; + seq() + { + n = 0; + } + int &operator[](int i) + { + return data[i]; + } + void operator+=(int x) + { + data[n++] = x; + } + void sort() + { + std::sort(data, data + n); + } + void relabel(int *cnt = NULL, int *orig = NULL) + { + int tmp[32]; + if (orig == NULL) + { + orig = tmp; + } + for (int i = 0; i < n; i++) + { + orig[i] = data[i]; + } + std::sort(orig, orig + n); + unique(orig, orig + n); + for (int i = 0; i < n; i++) + { + for (int j = 0;; j++) + { + if (data[i] == orig[j]) + { + data[i] = j; + break; + } + } + } + if (cnt != NULL) + { + for (int i = 0; i < n; i++) + { + cnt[i] = 0; + } + for (int i = 0; i < n; i++) + { + cnt[data[i]]++; + } + } + } + void restore(int *orig) + { + for (int i = 0; i < n; i++) + { + data[i] = orig[data[i]]; + } + } +}; + +seq read() +{ + seq s; + char c; + scanf(" %c", &c); + assert(c == '{' || c == '('); + int x; + while (scanf(" %d , ", &x) == 1) + { + s += x; + } + scanf(" %c", &c); + assert(c == '}' || c == ')'); + return s; +} + +void print(seq s, bool multiset = false) +{ + if (multiset) + { + s.sort(); + } + printf(multiset ? "(" : "{"); + for (int i = 0; i < s.n; i++) + { + printf(i == 0 ? "%d" : ",%d", s[i]); + } + printf(multiset ? ")\n" : "}\n"); +} + +string readWord() +{ + char s[100] = {0}; + if (scanf(" %[a-zA-Z]", s) != 1) + { + return ""; + } + return string(s); +} +long long readLL() +{ + long long x; + scanf("%lld", &x); + return x; +} +BigInt readBig() +{ + char s[100]; + scanf(" %[0-9]", s); + return BigInt(s); +} + +ll nperm(int cnt[], int k) +{ + ll a = 1; + int sum = 0; + for (int i = 0; i < k; i++) + { + a *= fact[cnt[i]]; + sum += cnt[i]; + } + return fact[sum] / a; +} + +seq degrade(seq s) +{ + s.sort(); + return s; +} +seq promote(seq s, ll num) +{ + int cnt[32], orig[32]; + s.sort(); + s.relabel(cnt, orig); + seq res; + for (int pos = 0; pos < s.n; pos++) + { + int x; + for (x = 0; x < s.n; x++) + { + if (cnt[x] == 0) + { + continue; + } + cnt[x]--; + ll t = nperm(cnt, s.n); + if (num >= t) + { + num -= t; + } + else + { + break; + } + cnt[x]++; + } + res += x; + } + res.restore(orig); + return res; +} + +ll rank_(seq s) +{ + int cnt[32]; + s.relabel(cnt); + ll res = 0; + for (int pos = 0; pos < s.n; pos++) + { + for (int x = 0; x < s[pos]; x++) + { + if (cnt[x] == 0) + { + continue; + } + cnt[x]--; + res += nperm(cnt, s.n); + cnt[x]++; + } + cnt[s[pos]]--; + } + return res; +} + +map dc_memo; + +BigInt &deriveCount(int cnt[], int n, int m) +{ + string key; + for (int i = 0; i < n; i++) + if (cnt[i] > 0) + { + key += 'a' + cnt[i]; + } + sort(key.begin(), key.end()); + key += 'a' + m; + if (dc_memo.count(key) > 0) + { + return dc_memo[key]; + } + static BigInt f[32][32]; + for (int i = 0; i <= m; i++) + { + f[n][i] = 0; + } + f[n][0] = big_fact[m]; + for (int i = n - 1; i >= 0; i--) + { + f[i][0] = fact[m]; + for (int j = 1; j <= m; j++) + { + f[i][j] = 0; + for (int k = 0; k <= j && k <= cnt[i]; k++) + { + f[i][j] += f[i + 1][j - k] / big_fact[k]; + } + } + } + return dc_memo[key] = f[0][m]; +} + +seq derive(seq s, int m, BigInt num) +{ + int cnt[32], orig[32]; + s.sort(); + s.relabel(cnt, orig); + seq res; + for (int pos = 0; pos < m; pos++) + { + int x; + for (x = 0; x < s.n; x++) + { + if (cnt[x] <= 0) + { + continue; + } + cnt[x]--; + BigInt t = deriveCount(cnt, s.n, m - pos - 1); + if (num >= t) + { + num -= t; + } + else + { + break; + } + cnt[x]++; + } + res += x; + } + res.restore(orig); + return res; +} + +BigInt underive(seq dst, seq src) +{ + int cnt[32], orig[32]; + src.sort(); + src.relabel(cnt, orig); + for (int i = 0; i < dst.n; i++) + { + int j; + for (j = 0; j < src.n && orig[j] != dst[i]; j++) + ; + dst[i] = j; + } + BigInt res = 0; + for (int pos = 0; pos < dst.n; pos++) + { + for (int x = 0; x < dst[pos]; x++) + { + if (cnt[x] <= 0) + { + continue; + } + cnt[x]--; + res += deriveCount(cnt, src.n, dst.n - 1 - pos); + cnt[x]++; + } + cnt[dst[pos]]--; + } + return res; +} + +int main() +{ + fact[0] = 1; + for (int n = 1; n <= 30; n++) + { + fact[n] = fact[n - 1] * n; + } + big_fact[0] = 1; + for (int n = 1; n <= 30; n++) + { + big_fact[n] = big_fact[n - 1] * n; + } + for (;;) + { + string op = readWord(); + if (op == "" || op == "end") + { + break; + } + if (op == "degrade") + { + print(degrade(read()), true); + } + else if (op == "promote") + { + seq s = read(); + print(promote(s, readLL())); + } + else if (op == "rank") + { + printf("%lld\n", rank_(read())); + } + else if (op == "derive") + { + seq s = read(); + int m = readLL(); + BigInt num = readBig(); + print(derive(s, m, num)); + } + else if (op == "find") + { + seq s = read(); + seq t = read(); + printf("%s\n", underive(s, t).str().c_str()); + } + } + return 0; +} diff --git a/11024.cpp b/11024.cpp new file mode 100644 index 0000000..dfe3620 --- /dev/null +++ b/11024.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +int P, S[4][4], A[4][5]; + +void print() +{ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 5; j++) + { + printf("%5d", A[i][j]); + } + printf("\n"); + } + printf("\n"); +} + +int solve() +{ + // printf("P=%d S=%d %d/%d %d\n",P, S[1][1],S[1][2],S[2][1],S[2][2]); + memset(A, 0, sizeof(A)); + A[0][0] = 1; + A[0][2] = 1; + A[0][4] = (P - S[1][1]) % P; + A[1][0] = 1; + A[1][3] = 1; + A[1][4] = (P - S[1][2]) % P; + A[2][1] = 1; + A[2][2] = 1; + A[2][4] = (P - S[2][1]) % P; + A[3][1] = 1; + A[3][3] = 1; + A[3][4] = (P - S[2][2]) % P; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 5; j++) + { + A[i][j] %= P; + } + // print(); + int x = A[0][4] - A[1][4] - A[2][4] + A[3][4]; + if (x < 0) + { + x = P - ((-x) % P); + } + x %= P; + return x == 0; +} + +int main() +{ + int T, p[4][4]; + for (scanf("%d", &T); T-- > 0;) + { + for (int i = 1; i <= 2; i++) + { + scanf("%d %d %d %d", &S[i][1], &S[i][2], &p[i][1], &p[i][2]); + } + P = p[1][1]; + for (int i = 1; i <= 2; i++) + for (int j = 1; j <= 2; j++) + { + P = __gcd(P, p[i][j]); + } + for (int i = 1; i <= 2; i++) + for (int j = 1; j <= 2; j++) + { + S[i][j] %= P; + } + printf(solve() ? "Yes\n" : "No\n"); + } +} diff --git a/11025.cpp b/11025.cpp new file mode 100644 index 0000000..9506dde --- /dev/null +++ b/11025.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define INF 0x3fffffff +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int N, M, carSpeed, adj[16][16], dist[16][16], deg[16]; +long long memo[13][1 << 13]; +int best[13][1 << 13]; + +long long f(int at, int sub) +{ + sub &= ~(1 << at); + long long &res = memo[at][sub]; + if (res >= 0) + { + return res; + } + if (sub == 0) + { + return res = deg[at] * (long long)dist[at][0]; + } + int sum = 0; + for (int u = 0; u < N; u++) + if (sub & (1 << u)) + { + sum += deg[u]; + } + res = 1LL << 60; + for (int u = 0; u < N; u++) + { + if ((sub & (1 << u)) == 0) + { + continue; + } + long long t = deg[at] * (long long)dist[at][0] + sum * (long long)dist[at][u] + f(u, sub); + if (t < res) + { + res = t; + best[at][sub] = u; + } + } + return res; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d %d %d", &N, &M, &carSpeed) == 3; cs++) + { + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + adj[i][j] = INF; + } + for (int i = 0; i < M; i++) + { + int u, v, w; + scanf("%d %d %d", &u, &v, &w); + adj[u][v] = adj[v][u] = w; + } + for (int i = 0; i < N; i++) + { + deg[i] = 0; + for (int j = 0; j < N; j++) + if (adj[i][j] < INF) + { + deg[i]++; + } + } + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + dist[i][j] = (i == j ? 0 : adj[i][j]); + } + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + dist[i][j] = MIN(dist[i][j], dist[i][k] + dist[k][j]); + } + memset(memo, 0xff, sizeof(memo)); + long long num = f(0, (1 << N) - 1); + long long den = 2 * M * carSpeed; + long long g = __gcd(num, den); + num /= g; + den /= g; + printf("Case %d:\n%lld/%lld\n", cs, num, den); + printf("0"); + for (int at = 0, s = ((1 << N) - 1) ^ (1 << at); s != 0;) + { + printf(" %d", best[at][s]); + at = best[at][s]; + if (at < 0) + { + break; + } + s &= ~(1 << at); + } + printf("\n"); + if (cs < T) + { + printf("\n"); + } + } +} diff --git a/11026.cpp b/11026.cpp new file mode 100644 index 0000000..80cb2ee --- /dev/null +++ b/11026.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +/* +11026 +Grouping Problem +*/ + +#define maxn 1001 +typedef long long INT; +INT T[maxn + 1][maxn + 1]; +INT A[maxn + 1], M, N, MAXX; +void Gen() +{ + INT i, j, k; + MAXX = -1; + T[1][0] = 1; + T[1][1] = A[1]; + if (N == 1) + { + MAXX = A[1] % M; + } + for (i = 2; i <= N; i++) + { + T[i][0] = 1; + for (j = 1; j < i; j++) + { + k = (T[i - 1][j] + T[i - 1][j - 1] * A[i]) % M; + if (k > MAXX && i == N) + { + MAXX = k; + } + T[i][j] = k; + } + T[i][i] = (T[i - 1][i - 1] * A[i]) % M; + if (T[i][i] > MAXX && i == N) + { + MAXX = T[i][i]; + } + } + cout << MAXX << endl; + //printf("%I64d\n",MAXX); +} +int main() +{ + while (cin >> N >> M) + { + if (!N && !M) + { + break; + } + for (INT i = 1; i <= N; i++) + { + // scanf("%I64d",&A[i]); + cin >> A[i]; + A[i] %= M; + } + Gen(); + } + return 0; +} diff --git a/11027.cpp b/11027.cpp new file mode 100644 index 0000000..c8a99f1 --- /dev/null +++ b/11027.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +int Prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; +int Len, Odd, tos, Factor[31][15], Fre[30], Sym[30]; +char SS[100]; + +void PreDefine() +{ + int i, j, k, d; + for (i = 2; i < 31; i++) + for (j = 0; j < 10 && Prime[j] <= i; j++) + { + d = 0; + for (k = Prime[j]; k <= i; k *= Prime[j]) + d += i / k; + Factor[i][j] = d; + } +} + +int Count(int len) +{ + int i, j, d; + int T[11] = {0}; + if (len <= 1) + return 1; + for (i = 0; i < 10; i++) + T[i] = Factor[len][i]; + for (i = 0; i < tos; i++) + { + d = Fre[Sym[i]]; + if (!d) + continue; + for (j = 0; j < 10 && Prime[j] <= d; j++) + T[j] -= Factor[d][j]; + } + d = 1; + for (i = 0; i < 10 && Prime[i] <= len; i++) + d *= (int)pow(Prime[i], T[i]); + return d; +} + +void Setting() +{ + for (int i = 0; i < tos; i++) + Fre[Sym[i]] /= 2; +} + +void Gen(int nth) +{ + int i, j, r = 0, s, val, d, pre = 0; + char ch, Res[40]; + for (i = 0; i < Len / 2; i++) + { + val = 0; + for (j = 0; j < tos; j++) + { + s = Sym[j]; + if (!Fre[s]) + continue; + Fre[s]--; + d = Count(Len / 2 - i - 1); + val += d; + if (pre + val >= nth) + { + Res[r++] = s + 'a'; + cout << Res[r - 1]; + val -= d; + pre += val; + break; + } + Fre[s]++; + } + } + if (Len % 2) + { + ch = Odd + 'a'; + cout << ch; + } + for (i = r - 1; i >= 0; i--) + cout << Res[i]; + cout << endl; +} + +void Cal(int nth) +{ + int maxx; + Setting(); + maxx = Count(Len / 2); + if (maxx < nth) + { + cout << "XXX\n"; + return; + } + Gen(nth); +} + +int isPalin() +{ + int i, odd = 0, d = 0, k; + tos = 0; + Len = strlen(SS); + for (i = 0; SS[i]; i++) + { + k = SS[i] - 'a'; + Fre[k]++; + } + for (i = 0; i < 26; i++) + { + if (Fre[i] % 2 != 0) + { + odd++; + Odd = i; + } + if (Fre[i] > 0) + Sym[tos++] = i; + } + if (Len % 2 == 0) + if (odd) + return 0; + else + return 1; + else if (odd != 1) + return 0; + return 1; +} + +void Free() +{ + for (int i = 0; i < 26; i++) + Fre[i] = 0; +} + +int main() +{ + int kase, nth, k = 1; + PreDefine(); + cin >> kase; + while (kase--) + { + cin >> SS >> nth; + cout << "Case " << k++ << ": "; + if (isPalin() == 0) + { + cout << "XXX\n"; + Free(); + continue; + } + Cal(nth); + Free(); + } + return 0; +} diff --git a/11028.cpp b/11028.cpp index 869d2f4..7f558c2 100644 --- a/11028.cpp +++ b/11028.cpp @@ -1,14 +1,19 @@ -#include -using namespace std; - -int main(){ - int val[]={0,0,1,1,3,8,21,43,69,102,145,197,261,336,425,527,645,778,929,1097,1285}; - for(int i=1;;i++){ - int n; - cin>>n; - if(n==0) - break; - cout<<"Case #"< + +using namespace std; + +int main() +{ + int val[] = {0, 0, 1, 1, 3, 8, 21, 43, 69, 102, 145, 197, 261, 336, 425, 527, 645, 778, 929, 1097, 1285}; + for (int i = 1;; i++) + { + int n; + cin >> n; + if (n == 0) + { + break; + } + cout << "Case #" << i << ": " << val[n] << endl; + } + return 0; +} diff --git a/11029.cpp b/11029.cpp new file mode 100644 index 0000000..33c2f51 --- /dev/null +++ b/11029.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +/* +11029 +*/ + +typedef long SS; +SS Table[100001]; +char Fg[100001]; +SS N, K; +long Recur(SS P, SS base, SS mod) +{ + SS l, r, d; + if (P == 1) + { + return base % mod; + } + if (P == 0) + { + return 1; + } + if (P <= 100000) + { + if (Fg[P] == 1) + { + return Table[P]; + } + } + if (P % 2) + { + l = Recur(P / 2, base, mod) % mod; + r = Recur(P / 2 + 1, base, mod) % mod; + d = l * r; + d %= mod; + } + else + { + d = Recur(P / 2, base, mod) % mod; + d *= d; + d %= mod; + } + if (P <= 100000) + { + Fg[P] = 1; + Table[P] = d; + } + return d; +} +void First() +{ + double X; + SS m, i, d = 0; + char dig[100]; + X = (double)K * log10(N); + m = (SS)X; + X -= (double)m; + X = pow(10, X); + sprintf(dig, "%lf", X); + for (i = 0; dig[i] && d < 3; i++) + { + if (dig[i] != '.') + { + printf("%c", dig[i]); + d++; + } + } +} +void Cal() +{ + SS x; + First(); + x = Recur(K, N, 1000); + printf("..."); + printf("%03ld\n", x); +} +void Free() +{ + int i; + for (i = 0; i <= K && i < 100001; i++) + { + Fg[i] = 0; + } +} +int main() +{ + SS ks; + cin >> ks; + while (ks--) + { + cin >> N >> K; + Cal(); + Free(); + } + return 0; +} diff --git a/1103.cpp b/1103.cpp new file mode 100644 index 0000000..004acc7 --- /dev/null +++ b/1103.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +const int maxh = 205; +const int maxw = 205; + +char bin[256][5]; + +int H, W, pic[maxh][maxw], color[maxh][maxw]; +char line[maxw]; + +// 解ç æˆä¸€ä¸ª01矩阵 +void decode(char ch, int row, int col) +{ + for (int i = 0; i < 4; i++) + { + pic[row][col + i] = bin[ch][i] - '0'; + } +} + +const int dr[] = {-1, 1, 0, 0}; +const int dc[] = {0, 0, -1, 1}; + +// DFSé历,把连通å—标记为C +void dfs(int row, int col, int c) +{ + color[row][col] = c; + for (int i = 0; i < 4; i++) + {// è¿™é“题åªéœ€è¦è€ƒè™‘ä¸Šä¸‹å·¦å³ + int row2 = row + dr[i]; + int col2 = col + dc[i]; + if (row2 >= 0 && + row2 < H && + col2 >= 0 && + col2 < W && + pic[row2][col2] == pic[row][col] && + color[row2][col2] == 0) + { + dfs(row2, col2, c); + } + } +} + +// 集åˆæ•°ç»„,记录黑色连通å—周围相连的éžèƒŒæ™¯çš„白å—,也就是白洞 +vector> neighbors; + +void checkNeighbors(int row, int col) +{ + for (int i = 0; i < 4; i++) + { + int row2 = row + dr[i]; + int col2 = col + dc[i]; + if (row2 >= 0 && + row2 < H && + col2 >= 0 && + col2 < W && + pic[row2][col2] == 0 && + color[row2][col2] != 1) + { + neighbors[color[row][col]].insert(color[row2][col2]); + } + } +} + +const char *code = "WAKJSD"; + +char recognize(int c) +{ + int cnt = neighbors[c].size(); + return code[cnt]; +} + +void print() +{ + for (int i = 0; i < H; i++) + { + for (int j = 0; j < W; j++) + { + printf("%d", pic[i][j]); + } + printf("\n"); + } +} + +int main() +{ + strcpy(bin['0'], "0000"); + strcpy(bin['1'], "0001"); + strcpy(bin['2'], "0010"); + strcpy(bin['3'], "0011"); + strcpy(bin['4'], "0100"); + strcpy(bin['5'], "0101"); + strcpy(bin['6'], "0110"); + strcpy(bin['7'], "0111"); + strcpy(bin['8'], "1000"); + strcpy(bin['9'], "1001"); + strcpy(bin['a'], "1010"); + strcpy(bin['b'], "1011"); + strcpy(bin['c'], "1100"); + strcpy(bin['d'], "1101"); + strcpy(bin['e'], "1110"); + strcpy(bin['f'], "1111"); + + int kase = 0; + while (scanf("%d%d", &H, &W) == 2 && H) + { + memset(pic, 0, sizeof(pic)); + for (int i = 0; i < H; i++) + { + scanf("%s", line); + for (int j = 0; j < W; j++) + { + decode(line[j], i + 1, j * 4 + 1); + } + } + + H += 2; + W = W * 4 + 2; + + int cnt = 0; + vector cc;// 黑色连通å—çš„æ ‡å·å­˜æ”¾åœ¨ccé‡Œé¢ + memset(color, 0, sizeof(color)); + for (int i = 0; i < H; i++) + { + for (int j = 0; j < W; j++) + { + if (!color[i][j]) + { + dfs(i, j, ++cnt); + if (pic[i][j] == 1) + { + cc.push_back(cnt); + } + } + } + } + neighbors.clear(); + neighbors.resize(cnt + 1); + for (int i = 0; i < H; i++) + { + for (int j = 0; j < W; j++) + { + if (pic[i][j] == 1) + { + checkNeighbors(i, j); + } + } + } + + vector ans; + for (int i = 0; i < cc.size(); i++) + { + ans.push_back(recognize(cc[i])); + } + sort(ans.begin(), ans.end()); + + printf("Case %d: ", ++kase); + for (int i = 0; i < ans.size(); i++) + { + printf("%c", ans[i]); + } + printf("\n"); + } + + return 0; +} diff --git a/11030.cpp b/11030.cpp new file mode 100644 index 0000000..519ad24 --- /dev/null +++ b/11030.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define eps 0.000000000001 +int S[25], X[25][12], Y[25][12]; +double x1, y11, x2, y2, x3, y3, x4, y4, t1, t2; +int co(int ch) +{ + int rt = 0, i, j; + for (i = 0; i < S[ch]; i++) + { + x3 = (double)X[ch][i]; + y3 = (double)Y[ch][i]; + x4 = (double)X[ch][i + 1]; + y4 = (double)Y[ch][i + 1]; + t1 = (x1 - x4) * (y3 - y4) - (x3 - x4) * (y11 - y4); + t2 = (x2 - x4) * (y3 - y4) - (x3 - x4) * (y2 - y4); + if (t1 * t2 >= 0) + { + continue; + } + t1 = (x3 - x2) * (y11 - y2) - (x1 - x2) * (y3 - y2); + t2 = (x4 - x2) * (y11 - y2) - (x1 - x2) * (y4 - y2); + if (t1 * t2 >= 0) + { + continue; + } + rt++; + } + return rt; +} +int main() +{ + int t, tt, n, s, i, j, q; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d", &s); + S[i] = s; + for (j = 0; j < s; j++) + { + scanf("%d%d", &X[i][j], &Y[i][j]); + } + X[i][s] = X[i][0]; + Y[i][s] = Y[i][0]; + } + scanf("%d", &q); + printf("Case %d:\n", t); + while (q--) + { + scanf("%lf%lf%lf%lf", &x1, &y11, &x2, &y2); + x1 += eps; + y11 -= eps; + x2 += eps; + y2 += eps; + for (i = s = 0; i < n; i++) + { + s += co(i) % 2; + } + printf("%d\n", s); + } + } + return 0; +} diff --git a/11031.cpp b/11031.cpp new file mode 100644 index 0000000..db3f628 --- /dev/null +++ b/11031.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int a[10010], b[10010], d[10010], n; + +void prep() +{ + int k = 1; + b[1] = a[n - 1]; + d[n - 1] = 1; + for (int i = n - 2; i >= 0; i--) + { + if (a[i] < b[k]) + { + b[++k] = a[i]; + d[i] = k; + continue; + } + int lo = 1, hi = k; + while (hi - lo > 2) + { + int mid = (lo + hi) / 2; + if (b[mid] > a[i]) + { + lo = mid; + } + else + { + hi = mid; + } + } + while (lo < hi && b[lo] > a[i]) + { + lo++; + } + b[lo] = MAX(b[lo], a[i]); + d[i] = lo; + } +} + +int main() +{ + int q, m; + for (int cs = 1; scanf("%d %d", &n, &q) == 2 && n > 0; cs++) + { + printf("Set %d:\n", cs); + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + prep(); + /* printf("a: "); for (int i = 0; i < n; i++) printf(" %d",a[i]);printf("\n"); + printf("d: "); for (int i = 0; i < n; i++) printf(" %d",d[i]);printf("\n"); + */ + int ma = 1; + for (int i = 0; i < n; i++) + { + ma = MAX(ma, d[i]); + } + for (int z = 1; z <= q && scanf("%d", &m) == 1; z++) + { + printf(" Subset %d:\n ", z); + if (m > ma) + { + printf("Impossible\n"); + continue; + } + int mi = -0x7fffffff; + for (int s = 0; s < n && m > 0; s++) + { + if (d[s] >= m && a[s] >= mi) + { + printf("%d", a[s]); + mi = a[s] + 1; + if (--m > 0) + { + printf(" "); + } + } + } + printf("\n"); + } + } +} diff --git a/11032.cpp b/11032.cpp new file mode 100644 index 0000000..452d348 --- /dev/null +++ b/11032.cpp @@ -0,0 +1,203 @@ +#include + +using namespace std; + +/* +11032 +*/ +#define maxn 10000001 +#define maxx 977788 +#define MAX(a, b) (a > b ? a : b) +char Fg[maxn]; +int A[maxx + 1]; +int pre, ind, pre1; +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int Sod(int n) +{ + int d = 0, k; + if (n % 10) + { + k = pre + n % 10; + return k; + } + while (n) + { + d += n % 10; + n /= 10; + } + pre = d; + return d; +} +void Gen() +{ + int i, d, k; + pre = 0; + ind = 0; + k = 1; + for (i = 1; i < maxn; i++) + { + d = Sod(i) + i; + if (d >= maxn) + { + continue; + } + Fg[d] = 1; + } + for (i = 1; i < maxn; i++) + if (Fg[i] == 0) + { + A[ind++] = i; + } +} +int Lower(int key) +{ + int lo = 0, up = ind - 1, mid; + mid = (lo + up) / 2; + if (key <= A[lo]) + { + return 0; + } + if (key > A[up]) + { + return -1; + } + while (1) + { + if (A[mid] > key) + { + if (A[mid - 1] < key) + { + return mid; + } + if (A[mid - 1] == key) + { + return mid - 1; + } + up = mid - 1; + } + else if (A[mid] < key) + { + if (A[mid + 1] >= key) + { + return mid + 1; + } + lo = mid + 1; + } + else + { + return mid; + } + mid = (lo + up) / 2; + } + return -1; +} +int Upper(int key) +{ + int lo = 0, up = ind - 1, mid; + mid = (lo + up) / 2; + if (key < A[lo]) + { + return -1; + } + if (key >= A[up]) + { + return up; + } + while (1) + { + if (A[mid] > key) + { + if (A[mid - 1] <= key) + { + return mid - 1; + } + up = mid - 1; + } + else if (A[mid] < key) + { + if (A[mid + 1] == key) + { + return mid + 1; + } + if (A[mid + 1] > key) + { + return mid; + } + lo = mid + 1; + } + else + { + return mid; + } + mid = (lo + up) / 2; + } + return -1; +} +int SS(int n) +{ + int d = 0; + while (n) + { + d += n % 10; + n /= 10; + } + return d; +} +void F1(int n) +{ + int i, *d; + d = (int *)bsearch(&n, A, ind, sizeof(int), com); + if (d) + { + printf("-1\n"); + return; + } + for (i = MAX(1, n - 100); i <= n; i++) + { + if (i + SS(i) == n) + { + printf("%d\n", i); + return; + } + } +} +void F2(int n, int m) +{ + int l, u; + l = Lower(n); + u = Upper(m); + if (l < 0 || u < 0) + { + printf("-1\n"); + } + else + { + printf("%d\n", u - l + 1); + } +} +int main() +{ + char ss[100]; + int d, n, m, ts, k = 1; + Gen(); + gets(ss); + sscanf(ss, "%d", &ts); + while (ts--) + { + gets(ss); + d = sscanf(ss, "%d%d", &n, &m); + printf("Case %d: ", k++); + if (d == 1) + { + F1(n); + } + else + { + F2(n, m); + } + } + return 0; +} diff --git a/11033.cpp b/11033.cpp new file mode 100644 index 0000000..0b70118 --- /dev/null +++ b/11033.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +int N, M, P; +int temp[4][4], sol[2][4][4], sols; +int main() +{ + int caseno = 1; + while (scanf("%d %d %d", &N, &M, &P) == 3) + { + if (!N && !M && !P) + { + break; + } + printf("Set %d:\n", caseno++); + sols = 0; + int mod; + for (int a = 1; a <= 7; a++) + { + temp[0][0] = a; + for (int b = 1; b <= 7; b++) + { + temp[0][1] = b; + for (int c = 1; c <= 7; c++) + { + if (a + b + c >= N || (N - (a + b + c) > 7)) + { + continue; + } + temp[0][2] = c; + temp[0][3] = N - a - b - c; + for (int d = 1; d <= 7; d++) + { + temp[1][0] = d; + for (int e = 1; e <= 7; e++) + { + temp[1][1] = e; + for (int f = 1; f <= 7; f++) + { + if (d + e + f >= N || (N - (d + e + f) > 7)) + { + continue; + } + temp[1][2] = f; + temp[1][3] = N - d - e - f; + for (int g = 1; g <= 7; g++) + { + if (a + d + g >= N || (N - (a + d + g) > 7)) + { + continue; + } + temp[2][0] = g; + temp[3][0] = N - a - d - g; + if (temp[0][3] + temp[1][2] + temp[3][0] >= N || + (N - (temp[0][3] + temp[1][2] + temp[3][0]) > 7)) + { + continue; + } + temp[2][1] = N - (temp[0][3] + temp[1][2] + temp[3][0]); + if (temp[0][1] + temp[1][1] + temp[2][1] >= N || + (N - (temp[0][1] + temp[1][1] + temp[2][1]) > 7)) + { + continue; + } + temp[3][1] = N - (temp[0][1] + temp[1][1] + temp[2][1]); + int s12 = N - temp[2][0] - temp[2][1], s34 = N - temp[3][0] - temp[3][1]; + int s13 = N - temp[0][2] - temp[1][2], s24 = N - temp[0][3] - temp[1][3]; + int s14 = N - temp[0][0] - temp[1][1]; + int d34 = s13 - s14; + if ((d34 + s34) & 1) + { + continue; + } + int x3 = (d34 + s34) >> 1, x4 = s34 - x3; + int x1 = s14 - x4, x2 = s12 - x1; + if (x1 < 1 || x1 > 7 || x2 < 1 || x2 > 7 || x3 < 1 || x3 > 7 || x4 < 1 || x4 > 7) + { + continue; + } + temp[2][2] = x1; + temp[2][3] = x2; + temp[3][2] = x3; + temp[3][3] = x4; + mod = 1; + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + { + mod = (mod * temp[i][j]) % M; + } + if (mod <= P) + { + if (sols < 2) + { + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + { + sol[sols][i][j] = temp[i][j]; + } + } + sols++; + } + } + } + } + } + } + } + } + printf("%d\n", sols); + for (int i = 0; i < sols && i < 2; i++) + { + for (int j = 0; j < 4; j++) + { + for (int k = 0; k < 4; k++) + { + printf("%d", sol[i][j][k]); + } + puts(""); + } + puts("...."); + } + } +} diff --git a/11034.cpp b/11034.cpp new file mode 100644 index 0000000..ee37c67 --- /dev/null +++ b/11034.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int main() +{ + int T, L, N; + for (scanf("%d", &T); T-- > 0 && scanf("%d %d", &L, &N) == 2;) + { + L *= 100; + vector v(N + 1, 0); + char s[100]; + for (int i = 0, x; i < N && scanf("%d %s", &x, s); i++) + { + v[i] = (s[0] == 'l') ? x : -x; + } + int where = 0, left = 0, right = 0, fit = 0, res = 0; + for (;;) + { + while (left < N && v[left] < 0) + { + left++; + } + while (right < N && v[right] > 0) + { + right++; + } + if (left >= N && right >= N && fit == 0) + { + break; + } + if (where == 0 && left < N && fit + v[left] <= L) + { + fit += v[left++]; + continue; + } + if (where == 1 && right < N && fit - v[right] <= L) + { + fit -= v[right++]; + continue; + } + res++; + where ^= 1; + fit = 0; + } + printf("%d\n", res); + } +} diff --git a/11035.cpp b/11035.cpp new file mode 100644 index 0000000..e23ac1d --- /dev/null +++ b/11035.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define MAX 100100 + +int sib[MAX], kid[MAX], alpha[MAX], cnt = 0; + +int main() +{ + int n, m; + while (scanf("%d", &n) == 1 && n > 0) + { + cnt = 0; + sib[0] = kid[0] = alpha[0] = 0; + while (n-- > 0 && scanf("%d", &m) == 1) + { + int path[100]; + char s[100]; + for (int i = 0; i < m && scanf(" %s", s) == 1; i++) + { + path[i] = 0; + for (int j = 0; s[j]; j++) + { + path[i] = path[i] * 128 + s[j]; + } + } + for (int x = 0, i = m - 1; i >= 0; i--) + { + int a = path[i], y; + for (y = kid[x]; y != 0; y = sib[y]) + if (alpha[y] == a) + { + break; + } + if (y == 0) + { + y = ++cnt; + sib[y] = kid[x]; + kid[x] = y; + alpha[y] = a; + kid[y] = 0; + } + x = y; + } + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/11036.cpp b/11036.cpp new file mode 100644 index 0000000..6210e63 --- /dev/null +++ b/11036.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +long long N, x; +char expression[110]; + +long long eval(char *str) +{ + char exp[110]; + strcpy(exp, str); + stack num; + char *p = strtok(exp, " "); + if (p)// first one + { + if (!strcmp(p, "x")) + { + num.push(x); + } + if (!strcmp(p, "N")) + { + num.push(N); + } + if (isdigit(p[0])) + { + num.push(atoi(p)); + } + } + while ((p = strtok(NULL, " ")) != NULL && strcmp(p, "%")) + { + if (!strcmp(p, "x")) + { + num.push(x); + } + if (!strcmp(p, "N")) + { + num.push(N); + } + if (isdigit(p[0])) + { + num.push(atoi(p)); + } + if (p[0] == '*' || p[0] == '+') + { + long long a, b; + b = num.top(); + num.pop(); + a = num.top(); + num.pop(); + if (p[0] == '*') + { + long long temp = ((a % N) * (b % N)) % N; + num.push(temp); + } + if (p[0] == '+') + { + long long temp = (a + b) % N; + num.push(temp); + } + } + } + long long a, b, value; + a = num.top(); + num.pop(); + b = num.top(); + num.pop(); + value = b % a; + return value; +} + +int main() +{ + while (cin >> N >> x) + { + cin.getline(expression, 100); + if (N == 0) + { + break; + } + map mm; + int times = 1; + while (mm[x] == 0) + { + mm[x] = times++; + x = eval(expression); + } + cout << times - mm[x] << endl; + } + return 0; +} diff --git a/11037.cpp b/11037.cpp new file mode 100644 index 0000000..3a3f499 --- /dev/null +++ b/11037.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +double x[4], y[4], r[4]; +double A1, B1, C1, D1; +double A2, B2, C2, D2; + +const double ZERO = 1e-6; + +bool input() +{ + scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf", &x[1], &y[1], &r[1], &x[2], &y[2], &r[2], &x[3], &y[3], &r[3]); + if (r[1] == 0 || r[2] == 0 || r[3] == 0) + { + return false; + } + else + { + return true; + } +} + +double getY(double x) +{ + double result; + result = (A2 * B1 - A1 * B2) * x + A2 * C1 - A1 * C2; + result /= (A1 * D2 - A2 * D1); + return result; +} + +int main() +{ + while (input()) + { + A1 = r[1] * r[1] - r[2] * r[2]; + B1 = 2 * x[1] * r[2] * r[2] - 2 * x[2] * r[1] * r[1]; + C1 = r[1] * r[1] * x[2] * x[2] - r[2] * r[2] * x[1] * x[1] + r[1] * r[1] * y[2] * y[2] - r[2] * r[2] * y[1] * y[1]; + D1 = 2 * y[1] * r[2] * r[2] - 2 * y[2] * r[1] * r[1]; + A2 = r[2] * r[2] - r[3] * r[3]; + B2 = 2 * x[2] * r[3] * r[3] - 2 * x[3] * r[2] * r[2]; + C2 = r[2] * r[2] * x[3] * x[3] - r[3] * r[3] * x[2] * x[2] + r[2] * r[2] * y[3] * y[3] - r[3] * r[3] * y[2] * y[2]; + D2 = 2 * y[2] * r[3] * r[3] - 2 * y[3] * r[2] * r[2]; + double xx1, yy1, xx2, yy2; + if (r[1] == r[2] && r[2] == r[3]) + { + double a1, a2, b1, b2, c1, c2; + a1 = 2 * (x[2] - x[1]); + a2 = 2 * (x[3] - x[2]); + b1 = 2 * (y[2] - y[1]); + b2 = 2 * (y[3] - y[2]); + c1 = x[1] * x[1] + y[1] * y[1] - x[2] * x[2] - y[2] * y[2]; + c2 = x[2] * x[2] + y[2] * y[2] - x[3] * x[3] - y[3] * y[3]; + double x, y; + y = (a1 * c2 - a2 * c1) / (a2 * b1 - a1 * b2); + if (fabs(a1) < ZERO) + { + x = (-b2 * y - c2) / a2; + } + else + { + x = (-b1 * y - c1) / a1; + } + printf("%.2lf %.2lf\n", x, y); + continue; + } + double m, n; + m = (A2 * B1 - A1 * B2) / (A1 * D2 - A2 * D1); + n = (A2 * C1 - A1 * C2) / (A1 * D2 - A2 * D1); + /* + printf( "A2*C1-A1*C2 = %.2lf\n",A2*C1-A1*C2 ); + printf( "m = %.2lf n = %.2lf\n",m,n ); + printf( "A1=%.2lf B1=%.2lf C1=%.2lf D1=%.2lf\n",A1,B1,C1,D1 ); + printf( "A2=%.2lf B2=%.2lf C2=%.2lf D2=%.2lf\n",A2,B2,C2,D2 ); + */ + double a, b, c; + if (r[1] == r[2]) + { + a = A2 + A2 * m * m; + b = B2 + 2 * A2 * m * n + D2 * m; + c = A2 * n * n + D2 * n + C2; + } + // else if ( r[2] == r[3] ) + // { + // + // } + else + { + a = A1 + A1 * m * m; + b = B1 + 2 * A1 * m * n + D1 * m; + c = A1 * n * n + D1 * n + C1; + } + // printf( "a = %.2lf b = %.2lf c = %.3lf\n",a,b,c ); + double delta = b * b - 4 * a * c; + if (delta < 0) + { + printf("No solution\n"); + continue; + } + else + { + delta = sqrt(delta); + } + xx1 = (-b + delta) / (2 * a); + yy1 = m * xx1 + n; + xx2 = (-b - delta) / (2 * a); + yy2 = m * xx2 + n; + double ang1 = r[2] / (sqrt((x[2] - xx1) * (x[2] - xx1) + (y[2] - yy1) * (y[2] - yy1))); + double ang2 = r[2] / (sqrt((x[2] - xx2) * (x[2] - xx2) + (y[2] - yy2) * (y[2] - yy2))); + if (ang1 >= ang2) + { + printf("%.2lf %.2lf\n", xx1, yy1); + } + else + { + printf("%.2lf %.2lf\n", xx2, yy2); + } + } + return 0; +} diff --git a/11038.cpp b/11038.cpp index 6783951..760aaf8 100644 --- a/11038.cpp +++ b/11038.cpp @@ -1,25 +1,80 @@ -#include -using namespace std; - -int main(){ - for(;;){ - long long m=0,n=0; - cin>>m>>n; - if(m==-1 && n==-1) - break; - long long cnt=0; - for(long long i=m;i<=n;i++){ - long long no=i; - if(no==0) - cnt++; - while(no>0){ - long long rem=no%10; - if(rem==0) - cnt++; - no/=10; - } - } - cout< + +using namespace std; + +int powten[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000}; + +long long m, n, cnt; + +void solve(long long range, int level) +{ + if (range < 10) + { + return; + } + long long flor = range / 10 * 10; + for (long long k = flor + 1; k <= range; k++) + { + long long mm = k; + while (mm) + { + if (mm % 10 == 0) + { + cnt += powten[level]; + } + mm /= 10; + } + } + cnt += flor / 10 * powten[level]; + int temp = flor / 10; + while (temp) + { + if (temp % 10 == 0) + { + cnt += powten[level]; + } + temp /= 10; + } + solve(flor / 10 - 1, level + 1); +} + +int main() +{ + while (cin >> m >> n && m >= 0 && n >= 0) + { + long long cntM, cntN; + if (m == n) + { + cnt = 0; + if (m == 0) + { + cnt++; + } + while (m) + { + if (m % 10 == 0) + { + cnt++; + } + m /= 10; + } + cout << cnt << endl; + } + else + { + cnt = 0; + solve(m - 1, 0); + cntM = cnt; + cnt = 0; + solve(n, 0); + cntN = cnt; + long long result = cntN - cntM; + if (m == 0) + { + result++; + } + cout << result << endl; + } + } + return 0; +} diff --git a/11039.cpp b/11039.cpp new file mode 100644 index 0000000..7545385 --- /dev/null +++ b/11039.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.first > b.first; +} + +int main() +{ + int t, n, a; + cin >> t; + while (t--) + { + cin >> n; + vector> v; + while (n--) + { + cin >> a; + if (a < 0) + { + v.push_back(make_pair(-a, 1)); + } + else + { + v.push_back(make_pair(a, 2)); + } + } + sort(v.begin(), v.end(), theTruthIsOutThere); + int prev = v[0].second; + int result = 1; + for (int i = 0, sz = v.size(); i < sz; i++) + { + if (v[i].second != prev) + { + result++; + prev = v[i].second; + } + } + cout << result << endl; + } + return 0; +} diff --git a/11040.cpp b/11040.cpp new file mode 100644 index 0000000..cb25d0a --- /dev/null +++ b/11040.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int main() +{ + int t, i, j1, k1, j2, k2, j3, k3, j4, k4; + int X[10][10]; + scanf("%d", &t); + for (i = 1; i <= t; i++) + { + for (j1 = 1; j1 <= 9; j1 += 2) + { + for (k1 = 1; k1 <= j1; k1 += 2) + { + scanf("%d", &X[j1][k1]); + } + } + for (j2 = 3; j2 <= 9; j2 += 2) + { + for (k2 = 2; k2 <= j2 - 1; k2 += 2) + { + X[j2][k2] = (X[j2 - 2][k2 - 1] - X[j2][k2 + 1] - X[j2][k2 - 1]) / 2; + } + } + for (j3 = 2; j3 <= 8; j3 += 2) + { + for (k3 = 1; k3 <= j3; k3++) + { + X[j3][k3] = X[j3 + 1][k3] + X[j3 + 1][k3 + 1]; + } + } + for (j4 = 1; j4 <= 9; j4++) + { + for (k4 = 1; k4 < j4; k4++) + { + printf("%d ", X[j4][k4]); + } + printf("%d\n", X[j4][k4]); + } + } + return 0; +} diff --git a/11041.cpp b/11041.cpp new file mode 100644 index 0000000..7030850 --- /dev/null +++ b/11041.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +typedef vector VI; + +int score[32]; + +int teamByName(char c) +{ + return isupper(c) ? (c - 'A') : (c - 'a' + 26); +} +char teamName(int n) +{ + return (n < 26) ? (n + 'A') : (n - 26 + 'a'); +} + +bool byScore1(int i, int j) +{ + return (score[i] != score[j]) ? (score[i] > score[j]) : (i < j); +} +bool byScore2(int i, int j) +{ + return (score[i] != score[j]) ? (score[i] < score[j]) : (i < j); +} + +string rec(VI a) +{ + int N = a.size(); + if (N == 2) + { + string s(2, ' '); + s[0] = teamName(a[0]); + s[1] = teamName(a[1]); + if (s[0] > s[1]) + { + swap(s[0], s[1]); + } + return s; + } + int H = N / 2; + sort(a.begin(), a.end(), byScore1); + string A = rec(VI(a.begin(), a.begin() + H)); + A += rec(VI(a.begin() + H, a.end())); + sort(a.begin(), a.end(), byScore2); + string B = rec(VI(a.begin(), a.begin() + H)); + B += rec(VI(a.begin() + H, a.end())); + return min(A, B); +} + +string rec(VI a, int our) +{ + int N = a.size() + 1; + if (N == 2) + { + string s(2, ' '); + s[0] = teamName(a[0]); + s[1] = teamName(our); + if (s[0] > s[1]) + { + swap(s[0], s[1]); + } + return s; + } + int H = N / 2; + sort(a.begin(), a.end(), byScore1); + string A = rec(VI(a.begin(), a.begin() + H)); + A += rec(VI(a.begin() + H, a.end()), our); + sort(a.begin(), a.end(), byScore2); + string B = rec(VI(a.begin(), a.begin() + (H - 1)), our); + B += rec(VI(a.begin() + (H - 1), a.end())); + return min(A, B); +} + +int main() +{ + int T, N, R; + scanf("%d", &T); + char our_c; + for (int cs = 1; cs <= T && scanf("%d %c", &R, &our_c) == 2; cs++) + { + N = 1 << R; + int our = teamByName(our_c); + for (int i = 0; i < N; i++) + { + scanf("%d", &score[i]); + } + vector avail; + for (int i = 0; i < N; i++) + if (i != our) + { + avail.push_back(i); + } + string s = rec(avail, our); + printf("%s\n", s.c_str()); + } +} diff --git a/11042.cpp b/11042.cpp new file mode 100644 index 0000000..3fcf8de --- /dev/null +++ b/11042.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +/* +11042 +*/ + +typedef long SS; +SS X, Y; +int Cal() +{ + double temp, dump, A, B; + double sum = 0; + double limit = pow(2, 32); + SS n = 1; + A = (double)X; + B = (double)Y; + if (X == 0 && Y == 0) + { + return 1; + } + while (1) + { + if (sum > limit) + { + return -1; + } + if (A == 0) + { + return n * 2; + } + if (B == 0) + { + return n; + } + temp = (A * A) + ((B * B) * (-1)); + dump = A * B * 2; + A = temp; + B = dump; + sum = fabs(A + B); + n *= 2; + } + return -1; +} +int main() +{ + int ks, x; + cin >> ks; + while (ks--) + { + cin >> X >> Y; + x = Cal(); + if (x < 0) + { + cout << "TOO COMPLICATED" << endl; + } + else + { + cout << x << endl; + } + } + return 0; +} diff --git a/11043.cpp b/11043.cpp new file mode 100644 index 0000000..f570c39 --- /dev/null +++ b/11043.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int P[256][256]; +unsigned char str[50000]; +char temp[50000], t; +int pre(int loc) +{ + if (loc == 0) + { + return P[0][0]; + } + if (loc == 1) + { + return P[0][str[0]]; + } + return P[str[loc - 2]][str[loc - 1]]; +} +void update(int loc, int value) +{ + if (loc == 0) + { + P[0][0] = value; + } + else if (loc == 1) + { + P[0][str[0]] = value; + } + else + { + P[str[loc - 2]][str[loc - 1]] = value; + } +} +int main() +{ + int s = 0; + while (scanf("%c", &t) == 1) + { + temp[s++] = t; + } + memset(P, -1, sizeof(P)); + for (int i = 0; i < s; i++) + { + str[i] = (unsigned char)temp[i]; + } + for (int i = 0; i < s; i += 6) + { + unsigned char total = 64; + unsigned char output[6]; + int len = 0; + for (int j = 0; j < 6 && i + j < s; j++) + { + static int mask[6] = {1, 2, 4, 8, 16, 32}; + int predict = pre(i + j); + if (predict == str[i + j]) + { + total += mask[j]; + } + else + { + output[len++] = str[i + j]; + update(i + j, str[i + j]); + } + } + printf("%c", total); + for (int j = 0; j < len; j++) + { + printf("%c", output[j]); + } + } +} diff --git a/11044.cpp b/11044.cpp index 3d438fb..83e3d5d 100644 --- a/11044.cpp +++ b/11044.cpp @@ -1,39 +1,15 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while( T-- ){ - int N, M; - cin >> N >> M; - cout << (N/3) * (M/3) << endl; +int main() +{ + long long n, m, k; + scanf("%lld", &k); + while (k--) + { + scanf("%lld%lld", &n,&m); + cout << (n / 3) * (m / 3) << endl; } return 0; } diff --git a/11045.cpp b/11045.cpp new file mode 100644 index 0000000..448e772 --- /dev/null +++ b/11045.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define maxn 40 + +int N, M, K, Cost[maxn][maxn], P[maxn]; +char T[][4] = {"XXL", "XL", "L", "M", "S", "XS"}; +vector Link[maxn]; +queue Q; +char C[maxn]; + +int Find(char ss[]) +{ + int i; + for (i = 0; i < 6; i++) + if (!strcmp(T[i], ss)) + return i + 1; + return i; +} + +void MakeLink() +{ + int i, n; + K = M + 6 + 1; + n = N / 6; + for (i = M + 1; i < K; i++) + { + Link[i].push_back(K); + Cost[i][K] = n; + } + for (i = 1; i <= M; i++) + { + Link[0].push_back(i); + Cost[0][i] = 1; + } +} + +void Mark(int v) +{ + int u; + if (v == 0) + return; + u = P[v]; + Cost[u][v]--; + Cost[v][u]++; + Mark(P[v]); +} + +int BFS() +{ + int u, v, i; + Q.push(0); + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (i = 0; i < Link[u].size(); i++) + { + v = Link[u][i]; + if (Cost[u][v] == 0 || C[v]) + continue; + C[v] = 1; + P[v] = u; + if (v == K) + { + Mark(v); + return 1; + } + Q.push(v); + } + } + return 0; +} + +void Cal() +{ + int i, d, j; + MakeLink(); + for (i = 1; i <= M; i++) + { + d = BFS(); + if (d == 0) + { + cout << "NO\n"; + return; + } + for (j = 0; j <= K; j++) + C[j] = 0; + while (!Q.empty()) + Q.pop(); + } + cout << "YES\n"; +} + +int main() +{ + int ks, i, n, m; + char ss[12], xx[12]; + cin >> ks; + while (ks--) + { + cin >> N >> M; + for (i = 1; i <= M; i++) + { + cin >> xx >> ss; + n = Find(xx) + M; + m = Find(ss) + M; + Link[i].push_back(n); + Link[i].push_back(m); + Link[n].push_back(i); + Link[m].push_back(i); + Cost[i][n] = 1; + Cost[i][m] = 1; + } + Cal(); + while (!Q.empty()) + Q.pop(); + for (i = 0; i <= K; i++) + { + C[i] = 0; + Link[i].clear(); + } + } + return 0; +} diff --git a/11046.cpp b/11046.cpp new file mode 100644 index 0000000..52131d1 --- /dev/null +++ b/11046.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +#define M 50 + +inline bool dfs(int d, int *arr, int size, int T, int depth, char output[][M]) +{ + for (int i = 0; i < size; i++) + { + if (arr[i] == T) + { + return true; + } + } + + if (d >= depth) + { + return false; + } + + int len = 0, tmp[10] = {0}; + for (int i = 0; i < size; i++) + { + for (int j = i + 1; j < size; j++) + { + len = 1; + for (int k = 0; k < size; k++) + { + if (k != i && k != j) + { + tmp[len++] = arr[k]; + } + } + tmp[0] = arr[i] + arr[j]; + if (dfs(d + 1, tmp, len, T, depth, output)) + { + int MAX = max(arr[i], arr[j]), MIN = min(arr[i], arr[j]); + sprintf(output[d], "%d + %d = %d", MAX, MIN, arr[i] + arr[j]); + return true; + } + } + } + + for (int i = 0; i < size; i++) + { + for (int j = i + 1; j < size; j++) + { + len = 1; + for (int k = 0; k < size; k++) + { + if (k != i && k != j) + { + tmp[len++] = arr[k]; + } + } + tmp[0] = arr[i] * arr[j]; + if (dfs(d + 1, tmp, len, T, depth, output)) + { + int MAX = max(arr[i], arr[j]), MIN = min(arr[i], arr[j]); + sprintf(output[d], "%d x %d = %d", MAX, MIN, arr[i] * arr[j]); + return true; + } + } + } + + for (int i = 0; i < size; i++) + { + for (int j = i + 1; j < size; j++) + { + int MAX = max(arr[i], arr[j]), MIN = min(arr[i], arr[j]); + if (MAX == MIN) + { + continue; + } + len = 1; + for (int k = 0; k < size; k++) + { + if (k != i && k != j) + { + tmp[len++] = arr[k]; + } + } + tmp[0] = MAX - MIN; + if (dfs(d + 1, tmp, len, T, depth, output)) + { + sprintf(output[d], "%d - %d = %d", MAX, MIN, MAX - MIN); + return true; + } + } + } + + for (int i = 0; i < size; i++) + { + for (int j = i + 1; j < size; j++) + { + int MAX = max(arr[i], arr[j]), MIN = min(arr[i], arr[j]); + if (MAX % MIN != 0) + { + continue; + } + len = 1; + for (int k = 0; k < size; k++) + { + if (k != i && k != j) + { + tmp[len++] = arr[k]; + } + } + tmp[0] = MAX / MIN; + if (dfs(d + 1, tmp, len, T, depth, output)) + { + sprintf(output[d], "%d / %d = %d", MAX, MIN, MAX / MIN); + return true; + } + } + } + + return false; +} + +int main() +{ + int T, N, arr[10]; + char output[10][M]; + while (scanf("%d", &T) == 1, T) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", arr + i); + } + memset(output, '\0', sizeof(output[0][0]) * 10 * M); + + int depth = 1; + for (int depth = 1; depth <= N; depth++) + { + if (depth == N) + { + puts("No solution"); + break; + } + if (dfs(0, arr, N, T, depth, output)) + { + for (int i = 0; i < depth - 1; i++) + { + puts(output[i]); + } + printf("%s <-\n", output[depth - 1]); + break; + } + } + } + return 0; +} diff --git a/11047.cpp b/11047.cpp new file mode 100644 index 0000000..13d7640 --- /dev/null +++ b/11047.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define maxn 100 +#define INF 999999 + +map M; +int N, MX, Cost[maxn][maxn], Path[maxn][maxn]; +string City[maxn]; + +void Ini() +{ + int i, j; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + Path[i][j] = i; + if (Cost[i][j] < 0) + { + Cost[i][j] = INF; + Path[i][j] == -1; + } + } + Path[i][i] = i; + } +} + +void Floyd() +{ + int i, j, k; + int dum; + for (k = 0; k < N; k++) + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + dum = Cost[i][k] + Cost[k][j]; + if (dum < Cost[i][j]) + { + Cost[i][j] = dum; + Path[i][j] = Path[k][j]; + } + } +} + +void PrintPath(int u, int v, int level) +{ + if (level > MX) + MX = level; + if (u == v) + { + if (Path[u][v] == u) + { + cout << City[u]; + return; + } + else if (level > 0) + { + cout << City[u]; + return; + } + } + PrintPath(u, Path[u][v], level + 1); + cout << " " << City[v]; +} + +void Cal() +{ + int q, u, v; + string empName, first, second; + Ini(); + Floyd(); + cin >> q; + while (q--) + { + cin >> empName >> first >> second; + u = M[first]; + v = M[second]; + MX = 0; + if (Cost[u][v] == INF) + { + cout << "Sorry Mr " << empName; + cout << " you can not go from " << first << " to " << second << endl; + } + else + { + cout << "Mr " << empName << " to go from "; + cout << first << " to " << second << ", you will receive "; + cout << Cost[u][v] << " euros\n"; + cout << "Path:"; + PrintPath(u, v, 0); + if (MX == 0) + cout << " " << City[u]; + cout << endl; + } + } +} + +int main() +{ + int k, i, j; + cin >> k; + while (k--) + { + cin >> N; + for (i = 0; i < N; i++) + { + cin >> City[i]; + M[City[i]] = i; + } + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + cin >> Cost[i][j]; + Cal(); + M.clear(); + } + return 0; +} diff --git a/11048.cpp b/11048.cpp index 9b97288..3fe5bba 100644 --- a/11048.cpp +++ b/11048.cpp @@ -1,88 +1,119 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +bool is_extra_or_missing(string &a, string &b) +{ + int diff = 0; + string &shorter = (a.size() > b.size() ? b : a); + string &longer = (a.size() > b.size() ? a : b); + for (int i = 0, j = 0, sz1 = longer.size(), sz2 = shorter.size(); i < sz1 && j < sz2;) + { + if (longer[i] == shorter[j]) + { + i++; + j++; + } + else + { + diff++; + i++; + if (diff > 1) + { + return false; + } + } + } + return true; +} -int EditDistance(string s1, string s2) { - int m = s1.length(); - int n = s2.length(); - int i, j; - int cost[m + 1][n + 1]; - cost[0][0] = 0; - FOI(i, 1, m) cost[i][0] = i; - FOI(j, 1, n) cost[0][j] = j; - - FOI(i, 1, m) { - FOI(j, 1, n) { - int val = (s1[i - 1] == s2[j - 1] ? 0 : 1); - cost[i][j] = min(cost[i - 1][j - 1] + val, min(cost[i - 1][j] + 1, cost[i][j - 1] + 1)); +void find_difference(string &a, string &b, vector &pos) +{ + for (int i = 0, sz = a.size(); i < sz; i++) + { + if (a[i] != b[i]) + { + pos.push_back(i); + if (pos.size() > 2) + { + return; + } } } - return cost[m][n]; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, Q; - int i, j; - set< string > Set; - cin >> N; - string str[N]; - FOI(i, 0, N-1) { - cin >> str[i]; - Set.insert(str[i]); +void automatic_correction(string &input, vector> &dictionary, map &m) +{ + vector pos; + int input_size = input.size(); + bool found = false; + if (m.find(input) != m.end()) + { + cout << input << " is correct" << endl; + return; } - cin >> Q; - while (Q--) { - string S; - cin >> S; - if (Set.find(S) != Set.end()) { - cout << S << " is correct\n"; + for (int i = 0, sz = dictionary.size(); i < sz; i++) + { + if (abs(dictionary[i].second - input_size) > 1) + { continue; } - bool flag = false; - int A[26], B[26]; - memset(A, 0, sizeof A); - FOI(i, 0, S.length()) ++A[S[i] - 'a']; - FOI(i, 0, N-1) { - memset(B, 0, sizeof B); - FOI(j, 0, str[i].length()-1) ++B[str[i][j] - 'a']; - int diff = 0; - FOI(j, 0, 25) diff += abs(A[j] - B[j]); - if (diff == 1 || (diff) { - cout << S << " is a misspelling of " << str[i] << endl; - flag = true; + if (dictionary[i].second == input_size) + { + pos.clear(); + find_difference(input, dictionary[i].first, pos); + if (pos.size() == 1) + { + found = true; + cout << input << " is a misspelling of " << dictionary[i].first << endl; break; } + else if (pos.size() == 2) + { + if (pos[0] + 1 == pos[1] && input[pos[0]] == dictionary[i].first[pos[1]] && input[pos[1]] == dictionary[i].first[pos[0]]) + { + found = true; + cout << input << " is a misspelling of " << dictionary[i].first << endl; + break; + } + } } - if (!flag) - cout << S << " is unknown\n"; - } + else + { + if (is_extra_or_missing(input, dictionary[i].first)) + { + found = true; + cout << input << " is a misspelling of " << dictionary[i].first << endl; + break; + } + } + } + if (!found) + { + cout << input << " is unknown" << endl; + } +} + +int main() +{ + int n, q; + string input; + vector> dictionary; + map m; + cin >> n; + getline(cin, input);// \n after n + while (n--) + { + getline(cin, input); + dictionary.push_back(make_pair(input, input.size())); + m[input] = true; + } + cin >> q; + getline(cin, input);// \n after q + while (q--) + { + getline(cin, input); + automatic_correction(input, dictionary, m); + } return 0; } diff --git a/11049.cpp b/11049.cpp new file mode 100644 index 0000000..da1cc2f --- /dev/null +++ b/11049.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +char maze[13][13]; + +vector bfs(const pair &start, const pair &end) +{ + pair, vector> tmp; + queue, vector>> q; + tmp.first = start; + vector v; + tmp.second = v; + q.push(tmp); + map visited; + visited[start.first * 10000 + start.second] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first == end) + { + return tmp.second; + } + pair pos = tmp.first; + if (pos.first > 2 && !visited[(pos.first - 2) * 10000 + pos.second] && maze[pos.first - 1][pos.second] == ' ') + { + visited[(pos.first - 2) * 10000 + pos.second] = true; + vector v = tmp.second; + v.push_back('N'); + q.push(make_pair(make_pair(pos.first - 2, pos.second), v)); + } + if (pos.second > 2 && !visited[pos.first * 10000 + pos.second - 2] && maze[pos.first][pos.second - 1] == ' ') + { + visited[pos.first * 10000 + pos.second - 2] = true; + vector v = tmp.second; + v.push_back('W'); + q.push(make_pair(make_pair(pos.first, pos.second - 2), v)); + } + if (pos.first < 11 && !visited[(pos.first + 2) * 10000 + pos.second] && maze[pos.first + 1][pos.second] == ' ') + { + visited[(pos.first + 2) * 10000 + pos.second] = true; + vector v = tmp.second; + v.push_back('S'); + q.push(make_pair(make_pair(pos.first + 2, pos.second), v)); + } + if (pos.second < 11 && !visited[pos.first * 10000 + pos.second + 2] && maze[pos.first][pos.second + 1] == ' ') + { + visited[pos.first * 10000 + pos.second + 2] = true; + vector v = tmp.second; + v.push_back('E'); + q.push(make_pair(make_pair(pos.first, pos.second + 2), v)); + } + } +} + +int main() +{ + pair start, end; + pair wallstart, wallend; + while (cin >> start.second >> start.first) + { + if (start.first == 0 && start.second == 0) + { + break; + } + cin >> end.second >> end.first; + start.second = (start.second - 1) * 2 + 1; + start.first = (start.first - 1) * 2 + 1; + end.second = (end.second - 1) * 2 + 1; + end.first = (end.first - 1) * 2 + 1; + for (int i = 0; i < 13; i++) + for (int j = 0; j < 13; j++) + { + maze[i][j] = ' '; + } + for (int i = 0; i < 3; i++) + { + cin >> wallstart.second >> wallstart.first >> wallend.second >> wallend.first; + for (int a = wallstart.first * 2; a <= wallend.first * 2; a++) + { + for (int b = wallstart.second * 2; b <= wallend.second * 2; b++) + { + maze[a][b] = '*'; + } + } + } + vector result = bfs(start, end); + for (int i = 0, sz = result.size(); i < sz; i++) + { + cout << result[i]; + } + cout << endl; + } + return 0; +} diff --git a/1105.cpp b/1105.cpp new file mode 100644 index 0000000..e331bc0 --- /dev/null +++ b/1105.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +typedef pair PII; +#define rep(i, n) for (int i = 0; i < (int)(n); i++) +const int N = 500005; +const int INF = 1000000000; +int m, n, p, q; +PII a[N]; +int c[2005][2005]; +int sum[2005][2005]; + +PII trans(PII cur) +{ + return PII(cur.first + cur.second, cur.first - cur.second + n); +} + +inline int calc(int x1, int y1, int x2, int y2) +{ + if (x1 < 1) + x1 = 1; + if (y1 < 1) + y1 = 1; + if (x2 > m + n) + x2 = m + n; + if (y2 > m + n) + y2 = m + n; + return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]; +} + +int main() +{ + int Tc = 0; + while (scanf("%d%d%d%d", &m, &n, &p, &q) != EOF && m + n + p + q) + { + rep(i, m + n + 1) + rep(j, m + n + 1) + sum[i][j] = c[i][j] = 0; + rep(i, p) + { + scanf("%d%d", &a[i].first, &a[i].second); + a[i] = trans(a[i]); + c[a[i].first][a[i].second] = 1; + } + for (int i = 1; i <= m + n; i++) + { + for (int j = 1; j <= m + n; j++) + { + sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + c[i][j]; + } + } + printf("Case %d:\n", ++Tc); + rep(i, q) + { + int ans = -1, ansi, ansj; + int len; + scanf("%d", &len); + for (int i = 1; i <= m; i++) + { + for (int j = 1; j <= n; j++) + { + // PII p[4]; + int x1, x2, y1, y2, cur; + // p[0] = trans(PII(i - len ,j)); + // p[1] = trans(PII(i + len, j)); + // p[2] = trans(PII(i, j - len)); + // p[3] = trans(PII(i, j + len)); + x1 = i + j - len; + x2 = i + j + len; + y1 = i - j - len + n; + y2 = i - j + len + n; + cur = calc(x1, y1, x2, y2); + if (cur > ans || (ans == cur && j < ansj) || (ans == cur && j == ansj && i < ansi)) + { + ans = cur; + ansi = i; + ansj = j; + } + } + } + printf("%d (%d,%d)\n", ans, ansi, ansj); + } + } + return 0; +} diff --git a/11050.cpp b/11050.cpp new file mode 100644 index 0000000..7149d9b --- /dev/null +++ b/11050.cpp @@ -0,0 +1,331 @@ +#include + +using namespace std; + +struct Wall +{ + int x1, y1, x2, y2, len; + // x1<=x2 y1<=y2 + + bool in(int x, int y) + { + if (x1 == x2) + { + return x == x1 && y1 <= y && y <= y2; + } + else//y1==y2 + { + return y == y1 && x1 <= x && x <= x2; + } + } +}; + +bool by_len(const Wall &a, const Wall &b) +{ + return a.len < b.len; +} + +Wall walls[512]; +int block[8][8][8][8], block_id, N, pathlen; +char isect[512][512], path[1024]; + +int c2dx[256], c2dy[256]; + +int main() +{ + memset(c2dx, 0, sizeof(c2dx)); + memset(c2dy, 0, sizeof(c2dy)); + c2dy['N'] = -1; + c2dx['N'] = 0; + c2dy['S'] = 1; + c2dx['S'] = 0; + c2dy['W'] = 0; + c2dx['W'] = -1; + c2dy['E'] = 0; + c2dx['E'] = 1; + int len[3], len_orig[3]; + while (scanf("%d %d %d %s", &len[0], &len[1], &len[2], path) == 4) + { + if (len[0] == 0 && len[1] == 0 && len[2] == 0) + { + break; + } + assert(len[0] >= 1 && len[1] >= 1 && len[2] >= 1); + assert(len[0] <= 6 && len[1] <= 6 && len[2] <= 6); + for (int i = 0; i < 3; i++) + { + len_orig[i] = len[i]; + } + sort(len, len + 3); + for (int i = 0; path[i]; i++) + { + path[i] = toupper(path[i]); + assert(strchr("NWES", path[i]) != NULL); + } + pathlen = strlen(path); + // --- Generate walls + N = 0; + Wall w; + for (w.x1 = 0; w.x1 <= 6; w.x1++) + for (w.y1 = 0; w.y1 <= 6; w.y1++) + for (w.x2 = 0; w.x2 <= 6; w.x2++) + for (w.y2 = 0; w.y2 <= 6; w.y2++) + { + if (w.x1 == w.x2) + { + w.len = w.y2 - w.y1; + } + else if (w.y1 == w.y2) + { + w.len = w.x2 - w.x1; + } + else + { + continue; + } + if (w.len <= 0) + { + continue; + } + if (w.len != len[0] && w.len != len[1] && w.len != len[2]) + { + continue; + } + if (w.y1 == 0 && w.y2 == 0 && w.x1 != 0) + { + continue; + } + if (w.y1 == 6 && w.y2 == 6 && w.x1 != 0) + { + continue; + } + if (w.x1 == 0 && w.x2 == 0 && w.y1 != 0) + { + continue; + } + if (w.x1 == 6 && w.x2 == 6 && w.y1 != 0) + { + continue; + } + assert(N <= 510); + walls[N++] = w; + } + sort(walls, walls + N, by_len); + // --- Determine intersecting walls + memset(isect, 0, sizeof(isect)); + for (int i = 0; i < N; i++) + { + isect[i][i] = 1; + for (int j = i + 1; j < N; j++) + { + Wall &u = walls[i], &v = walls[j]; + int c = 0; + for (int x = 0; x <= 6; x++) + for (int y = 0; y <= 6; y++) + if (u.in(x, y) && v.in(x, y)) + { + c++; + } + if (c >= 2) + { + isect[i][j] = isect[j][i] = 1; + } + } + } + // --- Bruteforce + for (int srow = 1; srow <= 6; srow++) + for (int scol = 1; scol <= 6; scol++) + { + // --- find endpoint, check path + int erow = srow, ecol = scol, okay = 1; + memset(block, 0, sizeof(block)); + for (int i = 0;; i++) + { + for (int d = 0; d < 4; d++) + { + int rr = erow + "1102"[d] - '1'; + int cc = ecol + "0211"[d] - '1'; + if (block[erow][ecol][rr][cc] == 0) + { + block[erow][ecol][rr][cc] = 2; + } + if (block[rr][cc][erow][ecol] == 0) + { + block[rr][cc][erow][ecol] = 2; + } + } + if (path[i] == 0) + { + break; + } + int r1 = erow + c2dy[path[i]]; + int c1 = ecol + c2dx[path[i]]; + if (r1 < 1 || c1 < 1 || r1 > 6 || c1 > 6) + { + okay = 0; + break; + } + block[erow][ecol][r1][c1] = 1; + block[r1][c1][erow][ecol] = 1; + erow = r1; + ecol = c1; + } + if (!okay) + { + continue; + } + // --- determine walls conflicting with the path + // and "potential" walls (WLOG there must be a solution + // in which each wall will be touching the path) + static int bad_wall[512]; + memset(bad_wall, 0, sizeof(bad_wall)); + for (int ui = 0; ui < N; ui++) + { + Wall &u = walls[ui]; + int potential = 0; + if (u.y1 == u.y2) + { + for (int t = u.x1 + 1; t <= u.x2; t++) + { + if (block[u.y1][t][u.y1 + 1][t] == 1) + { + bad_wall[ui] = 1; + } + if (block[u.y1][t][u.y1 + 1][t] == 2) + { + potential = 1; + } + } + } + else + { + for (int t = u.y1 + 1; t <= u.y2; t++) + { + if (block[t][u.x1][t][u.x1 + 1] == 1) + { + bad_wall[ui] = 1; + } + if (block[t][u.x1][t][u.x1 + 1] == 2) + { + potential = 1; + } + } + } + if (potential == 0) + { + bad_wall[ui] = 1; + } + } + // --- brute force walls + memset(block, 0, sizeof(block)); + block_id = 1; + for (int r1 = 0; r1 < 8; r1++) + for (int c1 = 0; c1 < 8; c1++) + for (int r2 = 0; r2 < 8; r2++) + for (int c2 = 0; c2 < 8; c2++) + if (r2 < 1 || c2 < 1 || r2 > 6 || c2 > 6) + { + block[r1][c1][r2][c2] = 1 << 30; + } + for (int w1 = 0; w1 < N; w1++) + if (!bad_wall[w1] && walls[w1].len == len[0]) + for (int w2 = w1 + 1; w2 < N; w2++) + if (!bad_wall[w2] && walls[w2].len == len[1] && !isect[w1][w2]) + for (int w3 = w2 + 1; w3 < N; w3++) + { + if (walls[w3].len < len[2]) + { + continue; + } + if (walls[w3].len > len[2]) + { + break; + } + if (bad_wall[w3]) + { + continue; + } + if (isect[w1][w3] || isect[w2][w3]) + { + continue; + } + // --- marked blocked moves + block_id++; + for (int i = 0; i < 3; i++) + { + Wall &u = walls[i == 0 ? w1 : (i == 1 ? w2 : w3)]; + if (u.y1 == u.y2) + { + for (int t = u.x1 + 1; t <= u.x2; t++) + { + block[u.y1][t][u.y1 + 1][t] = block_id; + block[u.y1 + 1][t][u.y1][t] = block_id; + } + } + else + { + for (int t = u.y1 + 1; t <= u.y2; t++) + { + block[t][u.x1][t][u.x1 + 1] = block_id; + block[t][u.x1 + 1][t][u.x1] = block_id; + } + } + } + // find shortest path + static int qdata[10000], dist[8][8]; + int *qh = qdata, *qt = qdata; + *qt++ = srow; + *qt++ = scol; + memset(dist, 0x3f, sizeof(dist)); + dist[srow][scol] = 0; + while (qh < qt) + { + int r = *qh++; + int c = *qh++; + if (r == erow && c == ecol) + { + break; + } + for (int d = 0; d < 4; d++) + { + int r1 = r + "0211"[d] - '1'; + int c1 = c + "1102"[d] - '1'; + if (block[r][c][r1][c1] >= block_id) + { + continue; + } + if (dist[r1][c1] < 1000) + { + continue; + } + dist[r1][c1] = dist[r][c] + 1; + *qt++ = r1; + *qt++ = c1; + } + } + if (dist[erow][ecol] == pathlen) + { + //OUTPUT GEN CODE + printf("%d %d\n", scol, srow); + printf("%d %d\n", ecol, erow); + Wall out[3] = {walls[w1], walls[w2], walls[w3]}; + int shown[3] = {0, 0, 0}; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + if (out[j].len == len_orig[i] && !shown[j]) + { + shown[j] = 1; + Wall &u = out[j]; + printf("%d %d %d %d\n", u.x1, u.y1, u.x2, u.y2); + break; + } + } + //ENF OF OUTPUT + goto FOUND; + } + }//for(w1,w2,w3) + } //for(srow,scol) + FOUND:; + }//while(scanf) +} diff --git a/11051.cpp b/11051.cpp new file mode 100644 index 0000000..fde58cf --- /dev/null +++ b/11051.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int main() +{ + int a, b, c, d, e, f, g, h, i, j; + char k, l, m[2], s; + while (scanf("%d", &a) == 1) + { + if (!a) + { + break; + } + gets(m); + c = 1; + i = 0; + while (scanf("%c%d%c", &k, &b, &l) == 3) + { + if (k == 'm') + { + b %= 2; + if (b) + { + if (i) + { + i = a - i; + } + c *= -1; + } + } + else + { + b %= a; + if (b) + { + i += b; + i %= a; + } + } + if (l == 10) + { + break; + } + } + if (i) + { + if (c == 1) + { + if (i > a - i + 2) + { + printf("m1 r%d m1\n", a - i); + } + else + { + printf("r%d\n", i); + } + } + else + { + if (i < a - i) + { + printf("m1 r%d\n", i); + } + else + { + printf("r%d m1\n", a - i); + } + } + } + else + { + if (c == -1) + { + printf("m1\n"); + } + else + { + printf("\n"); + } + } + } + return 0; +} diff --git a/11052.cpp b/11052.cpp new file mode 100644 index 0000000..3bd7ccb --- /dev/null +++ b/11052.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +int main(int argc, const char *argv[]) +{ + int n; + while (cin >> n && n) + { + int **t = new int *[n + 1]; + for (int i = 0; i != n; ++i) + { + t[i] = new int[3]; + int mom, dd, hh, mm; + long long num; + char im, s; + cin >> mom >> s >> dd >> s >> hh >> s >> mm >> num >> im; + t[i][0] = mm + hh * 100 + dd * 10000 + mom * 1000000; + t[i][1] = (im == '+' ? 1 : 0); + } + t[n] = new int[3]; + t[n][0] = 99999999; + t[n][1] = 0; + t[n][2] = 2012; + t[n - 1][2] = 2012; + for (int i = n - 2; i != -1; --i) + { + if (t[i][0] >= t[i + 1][0]) + { + t[i][2] = t[i + 1][2] - 1; + } + else + { + t[i][2] = t[i + 1][2]; + } + } + int pre_ptr = -1; + int remain = 0; + int last = 0; + for (int i = 0; i != n; ++i) + { + //find the first important + if (t[i][1]) + { + // this is the first important record + if (pre_ptr == -1) + { + pre_ptr = i; + ++remain; + last = i; + // cout <<"case1:"<< i << endl; + } + else if (t[pre_ptr][2] == t[i][2]) + { + // is the same year + pre_ptr = i; + ++remain; + last = i; + // cout <<"case2:"<< i << endl; + } + else if (t[pre_ptr][2] != t[i][2]) + { + int pre = pre_ptr; + for (int j = pre_ptr + 1; j != i; ++j) + { + // the case is like this 2007,2008,2009 + // the record must be kept + if (t[pre][2] == t[j + 1][2] - 2) + { + ++remain; + pre = j; + last = j; + // cout <<"case3:"<< j << endl; + } + else if (t[pre][2] == t[j + 1][2]) + { + // the case is like this: 2007,2007,2007 + // the record can be drop + continue; + } + else + { + // the case is like this: 2007,2007,2008 + if (t[pre][0] < t[j + 1][0]) + { + ++remain; + pre = j; + last = j; + // cout <<"case4:"<< j << endl; + } + else + { + continue; + } + } + } + pre_ptr = i; + ++remain; + last = i; + // cout << "case5:"< + +using namespace std; + +int N, a, b; + +int main() +{ + while (cin >> N && N) + { + cin >> a >> b; + map sets; + int cntOfSet = 0; + unsigned long long x = 0; + sets[x] = cntOfSet++; + while (1) + { + x = (a * ((x * x) % N) + b) % N; + if (sets.find(x) != sets.end()) + { + break; + } + else + { + sets[x] = cntOfSet++; + } + } + cout << N - (cntOfSet - sets[x]) << endl; + } + return 0; +} diff --git a/11054.cpp b/11054.cpp index f583a81..b694be8 100644 --- a/11054.cpp +++ b/11054.cpp @@ -1,56 +1,24 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - int N; - scanf("%d", &N); - if( N == 0 ) - break; - int64 arr[N + 1], T = 0; - int i; - FOI(i, 0, N-1) - scanf("%lld", &arr[i]); - FOI(i, 0, N-1){ - T += abs(arr[i]); - arr[i + 1] += arr[i]; - +/* +11054 +*/ +typedef long long SS; +int main() +{ + SS n, m, k, i, d; + while (scanf("%lld", &n) && n) + { + k = d = 0; + for (i = 0; i < n; i++) + { + scanf("%lld", &m); + k += abs(d); + d += m; } - printf("%lld\n", T); + printf("%lld\n", k); } return 0; } - diff --git a/11055.cpp b/11055.cpp new file mode 100644 index 0000000..ae12c35 --- /dev/null +++ b/11055.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +/* +11055 +*/ +#define maxn 1002 +int A[maxn][maxn], N; +int isHom() +{ + int i, j; + for (i = 0; i + 1 < N; i++) + { + for (j = 0; j + 1 < N; j++) + { + if (A[i][j] + A[i + 1][j + 1] != A[i][j + 1] + A[i + 1][j]) + { + return 0; + } + } + } + return 1; +} +int main() +{ + int i, d; + while (cin >> N && N) + { + for (i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + cin >> A[i][j]; + } + } + d = isHom(); + if (d) + { + cout << "homogeneous\n"; + } + else + { + cout << "not homogeneous\n"; + } + } + return 0; +} diff --git a/11056.cpp b/11056.cpp new file mode 100644 index 0000000..20cb116 --- /dev/null +++ b/11056.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +struct racer +{ + string name; + int min, sec, ms; +}; + +bool compare_names(string a, string b) +{ + transform(a.begin(), a.end(), a.begin(), ::tolower); + transform(b.begin(), b.end(), b.begin(), ::tolower); + return a < b; +} + +bool theTruthIsOutThere(const racer &a, const racer &b) +{ + if (a.min == b.min) + { + if (a.sec == b.sec) + { + if (a.ms == b.ms) + { + return compare_names(a.name, b.name); + } + else + { + return a.ms < b.ms; + } + } + return a.sec < b.sec; + } + return a.min < b.min; +} + +int main() +{ + int n; + int t; + char c; + string name; + racer tmp; + vector racers; + while (cin >> n) + { + cin.ignore(100, '\n'); + racers.clear(); + for (int i = 0; i < n; i++) + { + name = ""; + while (cin >> c) + { + if (c == ':') + { + break; + } + name += c; + } + tmp.name = name; + // using name as placeholder + cin >> t >> name; + tmp.min = t; + cin >> t >> name; + tmp.sec = t; + cin >> t >> name; + tmp.ms = t; + cin.ignore(100, '\n'); + racers.push_back(tmp); + } + // get blank line + getline(cin, name); + sort(racers.begin(), racers.end(), theTruthIsOutThere); + for (int i = 0, sz = racers.size(); i < sz; i++) + { + if (i % 2 == 0) + { + cout << "Row " << i / 2 + 1 << endl; + } + cout << racers[i].name << endl; + } + cout << endl; + } + return 0; +} diff --git a/11057.cpp b/11057.cpp index e34d9fe..7b579fd 100644 --- a/11057.cpp +++ b/11057.cpp @@ -1,30 +1,49 @@ -#include -#include -#include +#include + using namespace std; -int main() { - int N, M, I, A, B; - while (scanf("%d", &N) != EOF) { - set< int > S; - set< int >::iterator it; - - while (N--) { - scanf("%d", &I); - S.insert(I); +struct solution +{ + int a; + int b; +}; + +int main() +{ + int n; + int books[10010]; + int money; + solution best; + while (cin >> n) + { + for (int i = 0; i < n; i++) + { + cin >> books[i]; } - scanf("%d", &M); - A = 0; B = 1000000; - for (it = S.begin(); it != S.end(); it++) { - if (S.find(M - *it) != S.end()) { - int V = M - 2 * (*it); - if (V >= 0 && V < B - A) { - A = *it; - B = M - (*it); + cin >> money; + sort(books, books + n); + best.a = 0; + best.b = 2000000000; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (j == i) + { + continue; + } + if (books[i] + books[j] == money) + { + if (abs(books[i] - books[j]) < abs(best.a - best.b)) + { + best.a = min(books[i], books[j]); + best.b = max(books[i], books[j]); + } } } } - printf("Peter should buy books whose prices are %d and %d.\n\n", A, B); + cout << "Peter should buy books whose prices are " << best.a << " and " << best.b << "." << endl + << endl; } return 0; } diff --git a/11058.cpp b/11058.cpp new file mode 100644 index 0000000..1b81322 --- /dev/null +++ b/11058.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +char buffer[110]; +int kase = 1, R, pRule; +char encode[30]; + +struct AddtionRule +{ + int p; + char x, y; +} rule[1010]; + +void getInitCode() +{ + for (int i = 0; i < 26; i++) + { + cin >> encode[i]; + } +} + +void getAddtionRule() +{ + cin >> R; + for (int i = 0; i < R; i++) + { + cin >> rule[i].p >> rule[i].x >> rule[i].y; + } +} + +void update(int p) +{ + if (rule[pRule].p > p) + { + return; + } + while (rule[pRule].p == p && pRule < R) + { + encode[rule[pRule].x - 'a'] = rule[pRule].y; + pRule++; + } +} + +int main() +{ + while (cin >> buffer) + { + getInitCode(); + getAddtionRule(); + pRule = 0; + cout << "Case #" << kase++ << ": The encoding string is "; + for (int i = 0; i < strlen(buffer); i++) + { + update(i); + cout << encode[buffer[i] - 'a']; + } + cout << ".\n" + << endl; + } + return 0; +} diff --git a/11059.cpp b/11059.cpp index 86b2d7e..1255b0d 100644 --- a/11059.cpp +++ b/11059.cpp @@ -1,25 +1,34 @@ -#include -#include -#include -using namespace std; +#include -typedef long long int64; +using namespace std; -int main() { - int64 t = 1, N; - while (scanf("%lld", &N) != EOF) { - int64 Vec[N]; - for (int i = 0; i < N; i++) scanf("%lld", &Vec[i]); - - int64 Max = 0; - for (int i = 0; i < N; i++) { - int64 Pro = 1; - for (int j = i; j < N; j++) { - Pro *= Vec[j]; - Max = max(Max, Pro); +int main() +{ + int n; + int nums[20]; + int casenum = 0; + while (cin >> n) + { + casenum++; + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + long long best = 0; + for (int i = 0; i < n; i++) + { + long long current = 1; + for (int j = i; j < n; j++) + { + current *= nums[j]; + if (current > best) + { + best = current; + } } } - printf("Case #%lld: The maximum product is %lld.\n\n", t++, Max); + cout << "Case #" << casenum << ": The maximum product is " << best << "." << endl; + cout << endl; } return 0; } diff --git a/1106.cpp b/1106.cpp new file mode 100644 index 0000000..45bf676 --- /dev/null +++ b/1106.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +const int N = 100010; +const long long INF = 1000000000000000000LL; + +#define MP make_pair +#define FI first +#define SE second +typedef pair PIL; + +struct Node +{ + int D, P, R, G; + bool operator<(const Node &t) const + { + return D < t.D; + } +} a[N]; + +int n, C, D, tot, top; +long long dp[N]; +PIL q[N], st[N]; + +bool cmpr(PIL a, PIL b, PIL c) +{ + pair t1 = MP(1.0 * b.FI - 1.0 * a.FI, 1.0 * b.SE - 1.0 * a.SE), t2 = MP(1.0 * c.FI - 1.0 * b.FI, 1.0 * c.SE - 1.0 * b.SE); + return t1.SE * t2.FI <= t1.FI * t2.SE; +} + +void deal(int l, int r) +{ + if (l == r - 1) + { + return; + } + int mid = (l + r) / 2; + deal(l, mid); + tot = top = 0; + for (int i = l; i < mid; i++) + { + q[tot++] = MP(a[i].G, dp[i] + a[i].R - a[i].P - 1LL * (a[i].D + 1) * a[i].G); + } + sort(q, q + tot); + for (int i = 0; i < tot; i++) + { + while (top > 0 && st[top - 1].FI == q[i].FI && st[top - 1].SE < q[i].SE) + { + top--; + } + while (top > 1 && cmpr(st[top - 2], st[top - 1], q[i])) + { + top--; + } + st[top++] = q[i]; + } + for (int i = mid, j = 0; i < r; i++) + { + while (j < top - 1 && st[j + 1].SE - st[j].SE > -1LL * a[i].D * (st[j + 1].FI - st[j].FI)) + { + j++; + } + dp[i] = max(dp[i], st[j].SE + 1LL * a[i].D * st[j].FI); + if (dp[i] < a[i].P) + { + dp[i] = -INF; + } + } + deal(mid, r); +} + +void solve() +{ + for (int i = 1; i <= n; i++) + { + scanf("%d%d%d%d", &a[i].D, &a[i].P, &a[i].R, &a[i].G), dp[i] = -INF; + } + dp[0] = C; + sort(a + 1, a + n + 1); + deal(0, n + 1); + long long ans = C; + for (int i = 1; i <= n; i++) + { + dp[i] += a[i].R - a[i].P + 1LL * a[i].G * (D - a[i].D); + if (dp[i] > ans) + { + ans = dp[i]; + } + } + // for(int i=0;i<=n;i++) + // printf("%lld ", dp[i]);puts(""); + printf("%lld\n", ans); +} + +int main() +{ + int T = 0; + while (scanf("%d%d%d", &n, &C, &D) != EOF) + { + if (n == 0 && C == 0 && D == 0) + { + break; + } + printf("Case %d: ", ++T); + solve(); + } + return 0; +} diff --git a/11060.cpp b/11060.cpp new file mode 100644 index 0000000..77ecbfd --- /dev/null +++ b/11060.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +struct ss +{ + int ind; + char nam[100]; +}; +struct node +{ + int x; +}; +class comp +{ +public: + bool operator()(const node &a, const node &b) + { + return a.x > b.x; + } +}; +ss Dr[102]; +list V; +vector L[101], W[101]; +priority_queue, comp> Q; +char Org[101][52]; +int N, M, Deg[101]; + +int com(const void *a, const void *b) +{ + ss *x, *y; + x = (ss *)a; + y = (ss *)b; + return strcmp(x->nam, y->nam); +} + +void BFS() +{ + int u, i; + node t, d; + for (i = 0; i < N; i++) + if (Deg[i] == 0) + { + t.x = i; + Q.push(t); + } + while (!Q.empty()) + { + t = Q.top(); + V.push_back(t.x); + Q.pop(); + for (i = 0; i < W[t.x].size(); i++) + { + u = W[t.x][i]; + Deg[u]--; + if (Deg[u] == 0) + { + d.x = u; + Q.push(d); + } + } + } +} + +void Cal() +{ + int u, v; + ss st; + ss *p; + qsort(Dr, N, sizeof(ss), com); + cin >> M; + while (M--) + { + cin >> st.nam; + p = (ss *)bsearch(&st, Dr, N, sizeof(ss), com); + u = p->ind; + cin >> st.nam; + p = (ss *)bsearch(&st, Dr, N, sizeof(ss), com); + v = p->ind; + L[u].push_back(v); + W[u].push_back(v); + Deg[v]++; + } + BFS(); + list::iterator it; + for (it = V.begin(); it != V.end(); it++) + { + u = *it; + cout << " " << Org[u]; + } + cout << ".\n\n"; +} + +void Free() +{ + int i; + for (i = 0; i < 101; i++) + { + Deg[i] = 0; + L[i].clear(); + W[i].clear(); + } + V.clear(); +} + +int main() +{ + int i, k = 1; + while (cin >> N) + { + for (i = 0; i < N; i++) + { + cin >> Dr[i].nam; + strcpy(Org[i], Dr[i].nam); + Dr[i].ind = i; + } + cout << "Case #" << k++ << ": Dilbert should drink beverages in this order:"; + Cal(); + Free(); + } + return 0; +} diff --git a/11061.cpp b/11061.cpp new file mode 100644 index 0000000..4bc22ce --- /dev/null +++ b/11061.cpp @@ -0,0 +1,1017 @@ +#include + +using namespace std; + +int a[] = + { + 0, + 3, + 4, + 6, + 7, + 9, + 11, + 12, + 14, + 16, + 18, + 19, + 21, + 23, + 24, + 26, + 28, + 29, + 31, + 33, + 35, + 36, + 38, + 40, + 41, + 43, + 45, + 47, + 48, + 50, + 52, + 53, + 55, + 57, + 59, + 60, + 62, + 64, + 65, + 67, + 69, + 71, + 72, + 74, + 76, + 77, + 79, + 81, + 82, + 84, + 86, + 88, + 89, + 91, + 93, + 94, + 96, + 98, + 100, + 101, + 103, + 105, + 106, + 108, + 110, + 112, + 113, + 115, + 117, + 118, + 120, + 122, + 124, + 125, + 127, + 129, + 130, + 132, + 134, + 135, + 137, + 139, + 141, + 142, + 144, + 146, + 147, + 149, + 151, + 153, + 154, + 156, + 158, + 159, + 161, + 163, + 165, + 166, + 168, + 170, + 171, + 173, + 175, + 177, + 178, + 180, + 182, + 183, + 185, + 187, + 189, + 190, + 192, + 194, + 195, + 197, + 199, + 200, + 202, + 204, + 206, + 207, + 209, + 211, + 212, + 214, + 216, + 218, + 219, + 221, + 223, + 224, + 226, + 228, + 230, + 231, + 233, + 235, + 236, + 238, + 240, + 242, + 243, + 245, + 247, + 248, + 250, + 252, + 254, + 255, + 257, + 259, + 260, + 262, + 264, + 265, + 267, + 269, + 271, + 272, + 274, + 276, + 277, + 279, + 281, + 283, + 284, + 286, + 288, + 289, + 291, + 293, + 295, + 296, + 298, + 300, + 301, + 303, + 305, + 307, + 308, + 310, + 312, + 313, + 315, + 317, + 319, + 320, + 322, + 324, + 325, + 327, + 329, + 330, + 332, + 334, + 336, + 337, + 339, + 341, + 342, + 344, + 346, + 348, + 349, + 351, + 353, + 354, + 356, + 358, + 360, + 361, + 363, + 365, + 366, + 368, + 370, + 372, + 373, + 375, + 377, + 378, + 380, + 382, + 383, + 385, + 387, + 389, + 390, + 392, + 394, + 395, + 397, + 399, + 401, + 402, + 404, + 406, + 407, + 409, + 411, + 413, + 414, + 416, + 418, + 419, + 421, + 423, + 425, + 426, + 428, + 430, + 431, + 433, + 435, + 437, + 438, + 440, + 442, + 443, + 445, + 447, + 448, + 450, + 452, + 454, + 455, + 457, + 459, + 460, + 462, + 464, + 466, + 467, + 469, + 471, + 472, + 474, + 476, + 478, + 479, + 481, + 483, + 484, + 486, + 488, + 490, + 491, + 493, + 495, + 496, + 498, + 500, + 502, + 503, + 505, + 507, + 508, + 510, + 512, + 513, + 515, + 517, + 519, + 520, + 522, + 524, + 525, + 527, + 529, + 531, + 532, + 534, + 536, + 537, + 539, + 541, + 543, + 544, + 546, + 548, + 549, + 551, + 553, + 555, + 556, + 558, + 560, + 561, + 563, + 565, + 567, + 568, + 570, + 572, + 573, + 575, + 577, + 578, + 580, + 582, + 584, + 585, + 587, + 589, + 590, + 592, + 594, + 596, + 597, + 599, + 601, + 602, + 604, + 606, + 608, + 609, + 611, + 613, + 614, + 616, + 618, + 620, + 621, + 623, + 625, + 626, + 628, + 630, + 631, + 633, + 635, + 637, + 638, + 640, + 642, + 643, + 645, + 647, + 649, + 650, + 652, + 654, + 655, + 657, + 659, + 661, + 662, + 664, + 666, + 667, + 669, + 671, + 673, + 674, + 676, + 678, + 679, + 681, + 683, + 685, + 686, + 688, + 690, + 691, + 693, + 695, + 696, + 698, + 700, + 702, + 703, + 705, + 707, + 708, + 710, + 712, + 714, + 715, + 717, + 719, + 720, + 722, + 724, + 726, + 727, + 729, + 731, + 732, + 734, + 736, + 738, + 739, + 741, + 743, + 744, + 746, + 748, + 750, + 751, + 753, + 755, + 756, + 758, + 760, + 761, + 763, + 765, + 767, + 768, + 770, + 772, + 773, + 775, + 777, + 779, + 780, + 782, + 784, + 785, + 787, + 789, + 791, + 792, + 794, + 796, + 797, + 799, + 801, + 803, + 804, + 806, + 808, + 809, + 811, + 813, + 815, + 816, + 818, + 820, + 821, + 823, + 825, + 826, + 828, + 830, + 832, + 833, + 835, + 837, + 838, + 840, + 842, + 844, + 845, + 847, + 849, + 850, + 852, + 854, + 856, + 857, + 859, + 861, + 862, + 864, + 866, + 868, + 869, + 871, + 873, + 874, + 876, + 878, + 879, + 881, + 883, + 885, + 886, + 888, + 890, + 891, + 893, + 895, + 897, + 898, + 900, + 902, + 903, + 905, + 907, + 909, + 910, + 912, + 914, + 915, + 917, + 919, + 921, + 922, + 924, + 926, + 927, + 929, + 931, + 933, + 934, + 936, + 938, + 939, + 941, + 943, + 944, + 946, + 948, + 950, + 951, + 953, + 955, + 956, + 958, + 960, + 962, + 963, + 965, + 967, + 968, + 970, + 972, + 974, + 975, + 977, + 979, + 980, + 982, + 984, + 986, + 987, + 989, + 991, + 992, + 994, + 996, + 998, + 999, + 1001, + 1003, + 1004, + 1006, + 1008, + 1009, + 1011, + 1013, + 1015, + 1016, + 1018, + 1020, + 1021, + 1023, + 1025, + 1027, + 1028, + 1030, + 1032, + 1033, + 1035, + 1037, + 1039, + 1040, + 1042, + 1044, + 1045, + 1047, + 1049, + 1051, + 1052, + 1054, + 1056, + 1057, + 1059, + 1061, + 1063, + 1064, + 1066, + 1068, + 1069, + 1071, + 1073, + 1074, + 1076, + 1078, + 1080, + 1081, + 1083, + 1085, + 1086, + 1088, + 1090, + 1092, + 1093, + 1095, + 1097, + 1098, + 1100, + 1102, + 1104, + 1105, + 1107, + 1109, + 1110, + 1112, + 1114, + 1116, + 1117, + 1119, + 1121, + 1122, + 1124, + 1126, + 1127, + 1129, + 1131, + 1133, + 1134, + 1136, + 1138, + 1139, + 1141, + 1143, + 1145, + 1146, + 1148, + 1150, + 1151, + 1153, + 1155, + 1157, + 1158, + 1160, + 1162, + 1163, + 1165, + 1167, + 1169, + 1170, + 1172, + 1174, + 1175, + 1177, + 1179, + 1181, + 1182, + 1184, + 1186, + 1187, + 1189, + 1191, + 1192, + 1194, + 1196, + 1198, + 1199, + 1201, + 1203, + 1204, + 1206, + 1208, + 1210, + 1211, + 1213, + 1215, + 1216, + 1218, + 1220, + 1222, + 1223, + 1225, + 1227, + 1228, + 1230, + 1232, + 1234, + 1235, + 1237, + 1239, + 1240, + 1242, + 1244, + 1246, + 1247, + 1249, + 1251, + 1252, + 1254, + 1256, + 1257, + 1259, + 1261, + 1263, + 1264, + 1266, + 1268, + 1269, + 1271, + 1273, + 1275, + 1276, + 1278, + 1280, + 1281, + 1283, + 1285, + 1287, + 1288, + 1290, + 1292, + 1293, + 1295, + 1297, + 1299, + 1300, + 1302, + 1304, + 1305, + 1307, + 1309, + 1311, + 1312, + 1314, + 1316, + 1317, + 1319, + 1321, + 1322, + 1324, + 1326, + 1328, + 1329, + 1331, + 1333, + 1334, + 1336, + 1338, + 1340, + 1341, + 1343, + 1345, + 1346, + 1348, + 1350, + 1352, + 1353, + 1355, + 1357, + 1358, + 1360, + 1362, + 1364, + 1365, + 1367, + 1369, + 1370, + 1372, + 1374, + 1375, + 1377, + 1379, + 1381, + 1382, + 1384, + 1386, + 1387, + 1389, + 1391, + 1393, + 1394, + 1396, + 1398, + 1399, + 1401, + 1403, + 1405, + 1406, + 1408, + 1410, + 1411, + 1413, + 1415, + 1417, + 1418, + 1420, + 1422, + 1423, + 1425, + 1427, + 1429, + 1430, + 1432, + 1434, + 1435, + 1437, + 1439, + 1441, + 1442, + 1444, + 1446, + 1447, + 1449, + 1451, + 1452, + 1454, + 1456, + 1458, + 1459, + 1461, + 1463, + 1464, + 1466, + 1468, + 1470, + 1471, + 1473, + 1475, + 1476, + 1478, + 1480, + 1482, + 1483, + 1485, + 1487, + 1488, + 1490, + 1492, + 1494, + 1495, + 1497, + 1499, + 1500, + 1502, + 1504, + 1505, + 1507, + 1509, + 1511, + 1512, + 1514, + 1516, + 1517, + 1519, + 1521, + 1523, + 1524, + 1526, + 1528, + 1529, + 1531, + 1533, + 1535, + 1536, + 1538, + 1540, + 1541, + 1543, + 1545, + 1547, + 1548, + 1550, + 1552, + 1553, + 1555, + 1557, + 1559, + 1560, + 1562, + 1564, + 1565, + 1567, + 1569, + 1570, + 1572, + 1574, + 1576, + 1577, + 1579, + 1581, + 1582, + 1584, + 1586, + 1588, + 1589, + 1591, + 1593, + 1594, + 1596, + 1598, + 1600, + 1601, + 1603, + 1605, + 1606, + 1608, + 1610, + 1612, + 1613, + 1615, + 1617, + 1618, + 1620, + 1622, + 1623, + 1625, + 1627, + 1629, + 1630, + 1632, + 1634, + 1635, + 1637, + 1639, + 1641, + 1642, + 1644, + 1646, + 1647, + 1649, + 1651, + 1653, + 1654, + 1656, + 1658, + 1659, + 1661, + 1663, + 1665, + 1666, + 1668, + 1670, + 1671, + 1673, + 1675, + 1677, + 1678, + 1680, + 1682, + 1683, + 1685, + 1687, + 1689, + 1690, + 1692, + 1694, + 1695, + 1697, + 1699, + 1700, + 1702, + 1704, + 1706, + 1707, + 1709, + 1711}; + +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n > 0) + { + printf("%d\n", a[n]); + } + return 0; +} diff --git a/11062.cpp b/11062.cpp new file mode 100644 index 0000000..2fc5969 --- /dev/null +++ b/11062.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +setS; +char ss[100000]; +char W[503][1000]; +int ind; + +void Parse() +{ + int i, j; + char *p, xx[1000]; + + for(i = 0; ss[i]; i++) { + ss[i] = tolower(ss[i]); + + if(!isalpha(ss[i]) && ss[i] != '-') + ss[i] = ' '; + } + + p = strtok(ss, " "); + + while(p) { + strcpy(W[ind++], p); + p = strtok(NULL, " "); + } +} +void Cal() +{ + int p = 1, l; + char xx[1000]; + strcpy(xx, W[0]); + + while(p < ind) { + l = strlen(W[p - 1]); + + if(W[p - 1][l - 1] == '-') { + l = strlen(xx); + xx[l - 1] = NULL; + strcat(xx, W[p]); + } else { + S.insert(xx); + strcpy(xx, W[p]); + } + + p++; + } + + S.insert(xx); + set::iterator d; + + for(d = S.begin(); d != S.end(); d++) { + cout << *d << endl; + } +} +int main() +{ +// freopen("c:\\h.txt","r",stdin); + ind = 0; + + while(gets(ss)) { + Parse(); + } + + Cal(); + return 0; +} + diff --git a/11063.cpp b/11063.cpp index 3f15927..84d381c 100644 --- a/11063.cpp +++ b/11063.cpp @@ -1,66 +1,64 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define maxn 20002 -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, t = 1; - while( cin >> N ){ - int vec[N]; - int i, j; - bool isB2 = true; - FOI(i, 0, N-1){ - cin >> vec[i]; - if( vec[i] < 1 ) - isB2 = false; - } - map Map; - map::iterator it; - FOI(i, 1, N-1){ - FOI(j, 0, i){ - ++Map[vec[i] + vec[j]]; - if( i != j && vec[j] >= vec[i] ) - isB2 = false; - } +char sv[maxn]; +int N; +int A[101]; + +void F(int f) +{ + int i, j, d; + if (f) + { + printf("It is not a B2-Sequence.\n"); + return; + } + for (i = 0; i < N; i++) + { + for (j = i; j < N; j++) + { + d = A[i] + A[j]; + if (sv[d]) + { + printf("It is not a B2-Sequence.\n"); + return; + } + else + { + sv[d] = 1; + } } - for(it = Map.begin(); it != Map.end(); it++){ - if( (*it).second > 1 ){ - isB2 = false; - break; + } + printf("It is a B2-Sequence.\n"); +} +int main() +{ + int i, k = 1, f; + while (scanf("%d", &N) == 1) + { + f = 0; + for (i = 0; i < N; i++) + { + scanf("%d", &A[i]); + if (i && A[i - 1] >= A[i]) + { + f = 1; } } - cout << "Case #" << t++ << ":"; - if( isB2 ) - cout << " It is a B2-Sequence.\n\n"; - else - cout << " It is not a B2-Sequence.\n\n"; + if (A[0] < 1) + { + f = 1; + } + printf("Case #%d: ", k++); + F(f); + printf("\n"); + for (i = 0; i < maxn; i++) + { + sv[i] = 0; + } } return 0; } diff --git a/11064.cpp b/11064.cpp new file mode 100644 index 0000000..48b0fb0 --- /dev/null +++ b/11064.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +/* +11064 +*/ + +#define maxn 46342 +char sv[maxn]; +int P[10000], ind; +void Table() +{ + int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + P[0] = 2; + ind = 1; + for (i = 3; i < maxn; i += 2) + { + if (!sv[i]) + { + P[ind++] = i; + } + } +} +int Div(int n) +{ + int i, div = 1, k, sum = n, m = n; + for (i = 0; i < ind && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + k = 0; + sum -= sum / P[i]; + while (n % P[i] == 0) + { + n /= P[i]; + k++; + } + div *= (k + 1); + } + } + if (n > 1) + { + div *= 2; + sum -= sum / n; + } + div--; + return m - sum - div; +} +void Cal(int n) +{ + cout << Div(n) << endl; +} +int main() +{ + int n; + Table(); + while (cin >> n) + { + Cal(n); + } + return 0; +} diff --git a/11065.cpp b/11065.cpp new file mode 100644 index 0000000..31f5665 --- /dev/null +++ b/11065.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +#define PRETO 0 + +int adj[80][80], grau[80], n, m, cor[80], vizBranco[80], quant, max_, cont; + +void read() +{ + int u, v; + scanf("%d %d", &n, &m); + memset(grau, 0, sizeof(grau)); + for (int i = 0; i < m; i++) + { + scanf("%d%d", &u, &v); + adj[u][grau[u]++] = v; + adj[v][grau[v]++] = u; + } +} +void despinteWhite(int no) +{ + cor[no] = -1; + for (int i = 0; i < grau[no]; i++) + { + vizBranco[adj[no][i]]--; + } +} +void paintWhite(int no, int novaCor) +{ + cor[no] = novaCor; + for (int i = 0; i < grau[no]; i++) + { + vizBranco[adj[no][i]]++; + } +} +void despinteBlack(int no) +{ + cor[no] = -1; + for (int i = 0; i < grau[no]; i++) + if (cor[adj[no][i]] == no + 1) + { + despinteWhite(adj[no][i]); + } + quant--; +} +void paintBlack(int no) +{ + cor[no] = PRETO; + for (int i = 0; i < grau[no]; i++) + if (cor[adj[no][i]] == -1) + { + paintWhite(adj[no][i], no + 1); + } + quant++; +} +int podeBranco(int no) +{ + if (vizBranco[no] == grau[no]) + { + return 0; + } + for (int i = 0; i < grau[no]; i++) + { + if (cor[adj[no][i]] > 0 && vizBranco[adj[no][i]] + 1 == grau[adj[no][i]]) + { + return 0; + } + } + return 1; +} +int podePreto(int no) +{ + int retorno = 1; + for (int i = 0; i < grau[no]; i++) + if (cor[adj[no][i]] == -1) + { + if (!podeBranco(adj[no][i])) + { + retorno = 0; + } + else + { + paintWhite(adj[no][i], no + 1); + } + } + if (!retorno) + { + for (int i = 0; i < grau[no]; i++) + if (cor[adj[no][i]] == no + 1) + { + despinteWhite(adj[no][i]); + } + } + return retorno; +} +void rec(int no) +{ + if (no == n) + { + cont++; + max_ = max_ < quant ? quant : max_; + } + else + { + if (cor[no] != -1) + { + rec(no + 1); + return; + } + if (podePreto(no)) + { + paintBlack(no); + rec(no + 1); + despinteBlack(no); + } + if (podeBranco(no)) + { + paintWhite(no, 100); + rec(no + 1); + despinteWhite(no); + } + } +} +void process() +{ + memset(cor, -1, sizeof(cor)); + memset(vizBranco, 0, sizeof(vizBranco)); + max_ = cont = quant = 0; + rec(0); + printf("%d\n%d\n", cont, max_); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + read(); + process(); + } + return 0; +} diff --git a/11066.cpp b/11066.cpp new file mode 100644 index 0000000..b94b4d9 --- /dev/null +++ b/11066.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +struct Point +{ + double x, y; + bool operator<(const Point &q) const + { + return x < q.x; + } +}; + +int nc, nm, ne; +Point C[1024], M[1024], E[4096]; + +int main() +{ + while (scanf("%d", &nc) == 1 && nc > 0) + { + for (int i = 0; i < nc; i++) + { + scanf("%lf %lf", &C[i].x, &C[i].y); + } + scanf("%d", &nm); + for (int i = 0; i < nm; i++) + { + scanf("%lf %lf", &M[i].x, &M[i].y); + } + ne = 0; + for (int i = 0; i < nc - 1; i++) + { + E[ne].x = C[i].x; + E[ne].y = -(C[i + 1].y - C[i].y) / (C[i + 1].x - C[i].x); + E[ne + 1].x = C[i + 1].x; + E[ne + 1].y = -E[ne].y; + ne += 2; + } + for (int i = 0; i < nm - 1; i++) + { + E[ne].x = M[i].x; + E[ne].y = (M[i + 1].y - M[i].y) / (M[i + 1].x - M[i].x); + E[ne + 1].x = M[i + 1].x; + E[ne + 1].y = -E[ne].y; + ne += 2; + } + sort(E, E + ne); + double y0 = M[0].y - C[0].y, x0 = M[0].x, m = 0, res = 0; + for (int i = 0; i < ne;) + { + if (fabs(x0 - E[i].x) < EPS) + { + m += E[i++].y; + continue; + } + double dx = E[i].x - x0; + double y1 = y0 + m * dx; + if (y0 > -EPS && y1 > -EPS) + { + res += y0 * dx + m * dx * dx / 2; + } + else if (y0 > EPS && y1 < -EPS) + { + double c = -y0 / m; + res += y0 * c + m * c * c / 2; + } + else if (y0 < -EPS && y1 > EPS) + { + double c = -y0 / m; + double w = dx - c; + double yc = y0 + m * c; + res += yc * w + m * w * w / 2; + } + y0 = y1; + x0 = E[i].x; + } + if (res < EPS) + { + res = 0.0; + } + printf("%.2f\n", res); + } +} diff --git a/11067.cpp b/11067.cpp new file mode 100644 index 0000000..ad7a0c1 --- /dev/null +++ b/11067.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define maxn 102 + +int R, C, Path[maxn][maxn]; +char Fg[maxn][maxn]; + +void Count() +{ + int i, j; + Path[0][C] = 1; + for (i = C - 1; i >= 0; i--) + if (Fg[0][i]) + Path[0][i] = 0; + else + Path[0][i] = Path[0][i + 1]; + for (i = 1; i <= R; i++) + if (Fg[i][C]) + Path[i][C] = 0; + else + Path[i][C] = Path[i - 1][C]; + for (i = 1; i <= R; i++) + for (j = C - 1; j >= 0; j--) + if (Fg[i][j]) + Path[i][j] = 0; + else + Path[i][j] = Path[i][j + 1] + Path[i - 1][j]; + if (Path[R][0] == 0) + cout << "There is no path."; + else if (Path[R][0] == 1) + { + cout << "There is one path" + << " from Little Red Riding Hood's house to her grandmother's house."; + } + else + { + cout << "There are " << Path[R][0] << " paths from Little Red Riding" + << " Hood's house to her grandmother's house."; + } +} + +void Free() +{ + int i, j; + for (i = 0; i <= R; i++) + for (j = 0; j <= C; j++) + Fg[i][j] = Path[i][j] = 0; +} + +int main() +{ + int n, r, c; + while (cin >> C >> R) + { + if (!R || !C) + break; + cin >> n; + while (n--) + { + cin >> c >> r; + r = R - r; + Fg[r][c] = 1; + } + Count(); + cout << endl; + Free(); + } + return 0; +} diff --git a/11068.cpp b/11068.cpp new file mode 100644 index 0000000..b318251 --- /dev/null +++ b/11068.cpp @@ -0,0 +1,176 @@ +#include + +using namespace std; + +int main() +{ + double x, y, x1, x2, y1, y2, c1, c2, k, r, u; + while (1) + { + u = 0; + scanf("%lf %lf %lf", &x1, &y1, &c1); + scanf("%lf %lf %lf", &x2, &y2, &c2); + if (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0 && c1 == 0 && c2 == 0) + { + break; + } + else if (x1 == x2 && y1 == y2) + { + printf("No fixed point exists.\n"); + } + else + { + if (x1 == x2 && y1 != y2) + { + if (y1 > y2) + { + r = x1; + k = y1 - y2; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + y = (c1 - c2) / k; + } + if (r != 0) + { + x = (c1 - (y1 * y)) / r; + } + } + else + { + r = x2; + k = y2 - y1; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + y = (c2 - c1) / k; + } + if (r != 0) + { + x = (c2 - (y2 * y)) / r; + } + } + } + else if (x1 != x2 && y1 == y2) + { + if (x1 > x2) + { + r = y1; + k = x1 - x2; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + x = (c1 - c2) / k; + } + if (r != 0) + { + y = (c1 - (x1 * x)) / r; + } + } + else + { + r = y2; + k = x2 - x1; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + x = (c2 - c1) / k; + } + if (r != 0) + { + y = (c2 - (x2 * x)) / r; + } + } + } + else + { + double a1, a2, b1, b2; + a1 = y1; + a2 = y2; + b1 = c1; + b2 = c2; + y1 = x2 * y1; + c1 = x2 * c1; + y2 = x1 * y2; + c2 = x1 * c2; + if (y1 > y2) + { + k = y1 - y2; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + y = (c1 - c2) / k; + } + } + else + { + k = y2 - y1; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + y = (c2 - c1) / k; + } + } + y1 = a1; + y2 = a2; + c1 = b1; + c2 = b2; + x1 = x1 * y2; + c1 = c1 * y2; + x2 = y1 * x2; + c2 = y1 * c2; + if (x1 > x2) + { + k = x1 - x2; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + x = (c1 - c2) / k; + } + } + else + { + k = x2 - x1; + if (k == 0) + { + u = 1; + } + if (u == 0) + { + x = (c2 - c1) / k; + } + } + } + if (u == 0 && r != 0) + { + printf("The fixed point is at %.2lf %.2lf.\n", x, y); + } + else + { + printf("No fixed point exists.\n"); + } + } + } + return 0; +} diff --git a/11069.cpp b/11069.cpp new file mode 100644 index 0000000..a218d3b --- /dev/null +++ b/11069.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +#define maxn 78 +int F[maxn]; +void Table() +{ + int i; + F[1] = 1; + F[2] = F[3] = 2; + for (i = 4; i <= 76; i++) + { + F[i] = F[i - 2] + F[i - 3]; + } +} +int main() +{ + int n; + Table(); + while (cin >> n) + { + cout << F[n] << endl; + } + return 0; +} diff --git a/11070.cpp b/11070.cpp new file mode 100644 index 0000000..e1d0bff --- /dev/null +++ b/11070.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +int main(int argc, char *argv[]) +{ + stack s; + char c; + double a, b, sum; + while (cin >> a) + { + sum = 0; + for (;;) + { + c = getchar(); + if (c == '\n') + { + if (s.empty()) + { + sum += a; + } + else + { + char t = s.top(); + s.pop(); + if (t == '+') + { + sum += a; + } + else + { + sum -= a; + } + } + break; + } + cin >> b; + if ((c == '+') || (c == '-')) + { + if (s.empty()) + { + s.push(c); + sum += a; + a = b; + } + else + { + char t = s.top(); + s.pop(); + s.push(c); + if (t == '+') + { + sum += a; + } + else + { + sum -= a; + } + a = b; + } + } + else if (c == '*') + { + a *= b; + } + else if (c == '/') + { + a /= b; + } + } + cout << setprecision(3) << setiosflags(ios::fixed) << sum << endl; + } + return 0; +} diff --git a/11071.cpp b/11071.cpp new file mode 100644 index 0000000..2b74358 --- /dev/null +++ b/11071.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +int V[200005], S[200005], tmp, tV[200005], P[200005]; +int merge(int st, int len) +{ + if (len < 2) + { + return -1; + } + int la = len / 2, lb = len - len / 2, i, j, k; + int ma = st + la, mb = st + len; + merge(st, la); + merge(ma, lb); + for (i = st, j = ma, k = st; i < ma || j < mb; k++) + { + if (j == mb) + { + tV[k] = V[i++]; + } + else if (i == ma) + { + tV[k] = V[j]; + S[V[j]] += i - st; + j++; + } + else if (V[i] < V[j]) + { + tV[k] = V[i++]; + } + else + { + tV[k] = V[j]; + S[V[j]] += i - st; + j++; + } + } + for (k = 0; k < len; k++) + { + V[st + k] = tV[st + k]; + } + return -1; +} +int main() +{ + int n, i, j; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + { + scanf("%d", &V[i]); + } + for (i = 0; i < n; i++) + { + scanf("%d", &V[i]); + V[i]--; + S[i] = 0; + P[V[i]] = i; + } + V[n] = P[n] = n; + S[n] = 0; + merge(0, n + 1); + printf("0"); + for (i = 1; i < n; i++) + { + if (P[i + 1] > P[i]) + { + printf(" %d", S[i + 1] - S[i] - 1); + } + else + { + printf(" %d", i - S[i] + S[i + 1]); + } + } + printf("\n"); + } + return 0; +} diff --git a/11072.cpp b/11072.cpp new file mode 100644 index 0000000..7c40c10 --- /dev/null +++ b/11072.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define EPS 0.000000001 + +struct Ponto +{ + int x, y; + Ponto() {} + Ponto(int _x, int _y) : x(_x), y(_y) {} + Ponto operator-(Ponto a) + { + return Ponto(x - a.x, y - a.y); + } + double operator%(Ponto a) + { + return x * a.y - a.x * y; + } + double operator*(Ponto b) + { + return x * b.x + y * b.y; + } +}; +Ponto pontos[100010], atual, eixoX(1, 0); +int p, indiceMinimo, xMin, yMin; +int pilha[100010], top, degenerado; +int mod(Ponto a) +{ + return a.x * a.x + a.y * a.y; +} +int read() +{ + if (scanf("%d", &p) == EOF) + { + return 0; + } + xMin = yMin = 200000; + for (int i = 0; i < p; i++) + { + scanf("%d%d", &pontos[i].x, &pontos[i].y); + if (pontos[i].y < yMin) + { + xMin = pontos[i].x; + yMin = pontos[i].y; + indiceMinimo = i; + } + else if (pontos[i].y == yMin && pontos[i].x < xMin) + { + xMin = pontos[i].x; + yMin = pontos[i].y; + indiceMinimo = i; + } + } + atual = pontos[indiceMinimo]; + // printf("%d\n", indiceMinimo); + return 1; +} +int cmp(double a, double b) +{ + if (fabs(a - b) < EPS) + { + return 0; + } + return a > b ? 1 : -1; +} +double angle2(Ponto a, Ponto b) +{ + return atan2(a % b, a * b); +} +double angle(Ponto a) +{ + // printf("%d %d com %d %d = %lf\n", a.x, a.y, b.x, b.y, atan2(a%b, a*b)); + if (a.x == 0 && a.y == 0) + { + return -1; + } + return atan2(a.y, a.x); +} +int compare(const void *a, const void *b) +{ + Ponto pa = *((Ponto *)a), pb = *((Ponto *)b); + if (cmp(angle(pa - atual), angle(pb - atual))) + { + return cmp(angle(pa - atual), angle(pb - atual)); + } + return cmp(mod(pa - atual), mod(pb - atual)); +} +int direction(Ponto a, Ponto b) +{ + return cmp(a % b, 0); +} +void convexHull() +{ + qsort(pontos, p, sizeof(Ponto), compare); + // for(int i = 0; i < p; i++)printf("opa %d %d\n", pontos[i].x, pontos[i].y); + top = 0; + int aux = 2; + pilha[top++] = 0; + pilha[top++] = 1; + while (direction(pontos[1] - pontos[0], pontos[aux] - pontos[0]) == 0 && aux < p) + { + aux++; + } + if (aux == p) + { + degenerado = 1; + return; + } + degenerado = 0; + top--; + pilha[top++] = aux - 1; + pilha[top++] = aux; + for (int i = aux + 1; i < p; i++) + { + while (top > 1 && direction(pontos[i] - pontos[pilha[top - 2]], pontos[pilha[top - 1]] - pontos[pilha[top - 2]]) >= 0) + { + top--; + } + pilha[top++] = i; + } +} +int inside() +{ + double total = 0; + for (int i = 0; i < top; i++) + { + total += angle2(pontos[pilha[i]] - atual, pontos[pilha[(i + 1) % top]] - atual); + } + return cmp(total, 0); +} +void process() +{ + int r; + convexHull(); + // for(int i = 0; i < top; i++)printf("opa %d %d\n", pontos[pilha[i]].x, pontos[pilha[i]].y); + scanf("%d", &r); + while (r--) + { + scanf("%d%d", &atual.x, &atual.y); + if (!degenerado && inside()) + { + printf("inside\n"); + } + else + { + printf("outside\n"); + } + } +} +int main() +{ + while (read()) + { + process(); + } + return 0; +} diff --git a/11073.cpp b/11073.cpp new file mode 100644 index 0000000..c35044b --- /dev/null +++ b/11073.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +#define maxn 31627 + +typedef long long Int; + +char sv[maxn + 3]; +Int ip, res, Z, P[3500], Res[100000]; + +int Com(const void *a, const void *b) +{ + return *(Int *)a - *(Int *)b; +} + +void Table() +{ + Int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + sv[j] = 1; + for (++i; sv[i]; i++) + ; + } + ip = 1; + P[0] = 2; + for (i = 3; i < maxn; i += 2) + if (!sv[i]) + P[ip++] = i; +} + +Int isPrime(Int n) +{ + Int i; + if (n < maxn) + { + if (!sv[n]) + return 1; + return 0; + } + for (i = 2; i * i <= n; i++) + if (n % i == 0) + return 0; + return 1; +} + +void Recur(Int ind, Int rem, Int val) +{ + Int i, d, j, k, p, v, x; + if (rem == 1) + { + Res[res++] = val; + return; + } + for (i = ind; i < ip; i++) + { + k = P[i] - 1; + if (k * k > rem) + break; + d = 1; + for (j = 1; j < 32; j++) + { + p = k * d; + if (p > rem || rem % p) + break; + v = val * d * P[i]; + Recur(i + 1, rem / p, v); + x = rem / p; + if (x > P[i] && isPrime(x + 1)) + Res[res++] = (v) * (x + 1); + d *= P[i]; + } + } +} + +void Cal() +{ + Int i, j, k, d, p, x; + res = 0; + for (i = 0; i < ip; i++) + { + k = P[i] - 1; + if (k * k > Z) + break; + d = 1; + for (j = 1; j < 32; j++) + { + p = k * d; + if (p > Z || Z % p) + break; + Recur(i + 1, Z / p, (d * P[i])); + x = Z / p; + if (isPrime(x + 1) && x >= P[i]) + Res[res++] = (d * P[i]) * (x + 1); + d *= P[i]; + } + } + if (isPrime(Z + 1)) + Res[res++] = Z + 1; + if (!res) + { + printf("No solution.\n"); + return; + } + qsort(Res, res, sizeof(Int), Com); + printf("%lld", Res[0]); + for (i = 1; i < res; i++) + printf(" %lld", Res[i]); + printf("\n"); +} + +int main() +{ + Table(); + while (scanf("%lld", &Z) == 1) + { + if (Z % 2 && Z > 1) + { + printf("No solution.\n"); + continue; + } + if (Z == 1) + { + printf("1 2\n"); + continue; + } + Cal(); + } + return 0; +} diff --git a/11074.cpp b/11074.cpp new file mode 100644 index 0000000..37c8c62 --- /dev/null +++ b/11074.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +void draw_wall(int t) +{ + for (int i = 0; i < t; i++) + { + cout << "*"; + } +} + +void draw_cell(int s) +{ + for (int i = 0; i < s; i++) + { + cout << "."; + } +} + +void draw_cells(int t, int s, int n) +{ + for (int i = 0; i < s; i++) + { + draw_wall(t); + for (int j = 0; j < n; j++) + { + draw_cell(s); + draw_wall(t); + } + cout << endl; + } +} + +void draw_separator(int t, int s, int n) +{ + int len = (n + 1) * t + n * s; + for (int i = 0; i < t; i++) + { + for (int j = 0; j < len; j++) + { + cout << "*"; + } + cout << endl; + } +} + +int main() +{ + int s, t, n; + int casenum = 0; + while (cin >> s >> t >> n) + { + if (s == 0 && t == 0 && n == 0) + { + break; + } + casenum++; + cout << "Case " << casenum << ":" << endl; + draw_separator(t, s, n); + for (int i = 0; i < n; i++) + { + draw_cells(t, s, n); + draw_separator(t, s, n); + } + cout << endl; + } + return 0; +} diff --git a/11075.cpp b/11075.cpp new file mode 100644 index 0000000..fecce39 --- /dev/null +++ b/11075.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define REP(i, n) for (int _REP_N = (n), i = 0; i < _REP_N; i++) + +char pat[20][8][8], patc[20], goal[32][32], tmp[32][32]; +int match[20][32][32]; +int N, nrows, ncols, go_char, go_left; + +int go(int p) +{ + if (go_left == 0) + { + return 1; + } + while (p < N && patc[p] != go_char) + { + p++; + } + if (p >= N) + { + return 0; + } + REP(y, nrows) + REP(x, ncols) + { + if (!match[p][y][x]) + { + continue; + } + REP(a, 5) + REP(b, 5) if (pat[p][a][b] != '.' && tmp[y + a][x + b] != pat[p][a][b]) + { + goto skip; + } + REP(a, 5) + REP(b, 5) if (pat[p][a][b] != '.') + { + go_left--; + tmp[y + a][x + b] = '.'; + } + if (go(p + 1)) + { + return 1; + } + REP(a, 5) + REP(b, 5) if (pat[p][a][b] != '.') + { + go_left++; + tmp[y + a][x + b] = pat[p][a][b]; + } + skip:; + } + return 0; +} + +int main() +{ + int cnt1[256], cnt2[256], T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + memset(cnt1, 0, sizeof(cnt1)); + memset(cnt2, 0, sizeof(cnt2)); + scanf("%d", &N); + REP(p, N) + { + patc[p] = '.'; + REP(i, 5) + REP(j, 5) + { + scanf(" %c", &pat[p][i][j]); + if (pat[p][i][j] != '.' && patc[p] == '.') + { + patc[p] = pat[p][i][j]; + } + cnt1[(int)pat[p][i][j]]++; + } + } + scanf("%d %d", &nrows, &ncols); + memset(goal, '.', sizeof(goal)); + REP(i, nrows) + REP(j, ncols) + { + scanf(" %c", &goal[i][j]); + cnt2[(int)goal[i][j]]++; + } + int ok = 1; + for (int c = 'a'; c <= 'z'; c++) + { + ok &= (cnt1[c] == cnt2[c]); + } + if (ok) + { + REP(p, N) + { + if (patc[p] == '.') + { + continue; + } + int y0 = 4, x0 = 4; + REP(a, 5) + REP(b, 5) if (pat[p][a][b] != '.') + { + y0 = MIN(y0, a); + x0 = MIN(x0, b); + } + char buf[5][5]; + REP(a, 5) + REP(b, 5) + { + buf[a][b] = '.'; + if (a + y0 < 5 && b + x0 < 5) + { + buf[a][b] = pat[p][a + y0][b + x0]; + } + } + REP(a, 5) + REP(b, 5) pat[p][a][b] = buf[a][b]; + } + memset(match, 0, sizeof(match)); + REP(p, N) + REP(y, nrows) REP(x, ncols) + { + match[p][y][x] = 1; + REP(a, 5) + REP(b, 5) + { + if (pat[p][a][b] != '.') + { + match[p][y][x] &= (pat[p][a][b] == goal[y + a][x + b]); + } + } + } + } + for (int c = 'a'; c <= 'z' && ok; c++) + { + if (cnt1[c] == 0) + { + continue; + } + memcpy(tmp, goal, sizeof(goal)); + go_char = c; + go_left = cnt1[c]; + ok &= go(0); + } + printf("Case %d: %s\n", cs, ok ? "Yes" : "No"); + } +} diff --git a/11076.cpp b/11076.cpp new file mode 100644 index 0000000..6f2ed35 --- /dev/null +++ b/11076.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int n, m, digit[10], x; +unsigned long long sum, p10[15], comb[15][15], fact[15]; + +void init() +{ + int i, j; + p10[0] = 1; + fact[0] = 1; + for (i = 1; i < 15; i++) + { + p10[i] = p10[i - 1] * 10; + } + for (i = 0; i < 15; i++) + { + comb[i][0] = comb[i][i] = 1; + } + for (i = 1; i < 15; i++) + for (j = 1; j <= i; j++) + { + comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j]; + } + for (i = 1; i < 14; i++) + { + fact[i] = fact[i - 1] * i; + } +} + +int main() +{ + init(); + while (cin >> n && n) + { + memset(digit, 0, sizeof(digit)); + sum = 0; + bool f = true; + for (int i = 0; i < n; i++) + { + cin >> x; + digit[x]++; + if (digit[x] > 1) + { + f = false; + } + } + if (f) + { + for (int i = 0; i < 10; i++) + { + sum += digit[i] * i * fact[n - 1]; + } + } + else + { + for (int i = 0; i < 10; i++) + { + m = n - 1; + if (digit[i]) + { + int cnt = 1; + for (int j = 0; j < 10; j++) + { + if (i != j && digit[j]) + { + cnt *= comb[m][digit[j]]; + m -= digit[j]; + } + } + sum += cnt * i; + } + } + } + unsigned long long ans = 0; + for (int i = 0; i < n; i++) + { + ans += (sum * p10[i]); + } + cout << ans << endl; + } + return 0; +} diff --git a/11077.cpp b/11077.cpp new file mode 100644 index 0000000..f127f44 --- /dev/null +++ b/11077.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +unsigned long long f[22][22]; + +int main() +{ + int i, j; + for (i = 0; i < 22; i++) + { + f[0][i] = 0; + } + for (i = 1; i < 22; i++) + { + f[i][0] = 1; + for (j = 1; j <= i; j++) + { + f[i][j] = f[i - 1][j] + f[i - 1][j - 1] * (i - 1); + } + } + int n, k; + while (cin >> n >> k && n) + { + cout << f[n][k] << endl; + } + return 0; +} diff --git a/11078.cpp b/11078.cpp new file mode 100644 index 0000000..71c34de --- /dev/null +++ b/11078.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int N; + scanf("%d", &N); + int diff = -160000; + int max_; + scanf("%d", &max_); + for (int i = 1; i < N; i++) + { + int n; + scanf("%d", &n); + diff = max(diff, max_ - n); + max_ = max(max_, n); + } + printf("%d\n", diff); + } + return 0; +} diff --git a/11079.cpp b/11079.cpp new file mode 100644 index 0000000..eed7bdd --- /dev/null +++ b/11079.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define REP(i, n) for (int _REP_N = (n), i = 0; i < _REP_N; i++) + +typedef long long ll; + +template +T lcm(T a, T b) +{ + return a / __gcd(a, b) * b; +} +template +bool between(T a, T x, T b) +{ + return a <= x && x <= b; +} + +template +T floor(T p, T q) +{ + assert(q > 0); + if (p >= 0) + { + return p / q; + } + return -(((-p) + q - 1) / q); +} + +template +T ceil(T p, T q) +{ + assert(q > 0); + if (p >= 0) + { + return (p + q - 1) / q; + } + return -((-p) / q); +} + +struct Func +{ + int half, per, inc; +} f[20]; +int N, period, slope[56000]; +ll goal, periodInc, periodMax, periodMin; + +bool solve() +{ + period = 1; + REP(i, N) + period = lcm(period, f[i].per); + assert(1 <= period && period < 56000); + REP(i, period) + slope[i] = 0; + REP(i, N) + { + if (f[i].inc == 0) + { + continue; + } + for (int t = 0; t < period; t += f[i].per) + { + REP(j, f[i].half) + slope[t + j] += f[i].inc; + } + } + periodInc = periodMax = periodMin = 0; + REP(t, period) + { + periodInc += slope[t]; + periodMax = MAX(periodMax, periodInc); + periodMin = MIN(periodMin, periodInc); + } + //printf("period=%d inc=%lld max=%lld min=%lld goal=%lld\n", period, periodInc, periodMax, periodMin, goal); + ll p = 0; + if (periodInc != 0) + { + ll low, up; + if (periodInc > 0) + { + low = ceil(goal - periodMax, periodInc); + up = floor(goal - periodMin, periodInc); + } + else + { + low = ceil(periodMin - goal, -periodInc); + up = floor(periodMax - goal, -periodInc); + } + //printf("%lld <= p <= %lld\n", low, up); + if (low > up || up < 0) + { + return false; + } + p = low < 0 ? 0 : low; + } + //printf("p=%lld values=[%lld, %lld]\n", p, periodInc*p+periodMin, periodInc*p+periodMax); + if (!between(periodInc * p + periodMin, goal, periodInc * p + periodMax)) + { + assert(periodInc == 0); + return false; + } + int y0 = p * periodInc, y1; + for (int i = 0; i < period; i++, y0 = y1) + { + y1 = y0 + slope[i]; + if (goal < y0 && goal < y1) + { + continue; + } + if (goal > y0 && goal > y1) + { + continue; + } + double res = period * (double)p + i; + if (y0 != y1) + { + res += (goal - y0) / (double)(y1 - y0); + } + if (res < 0) + { + res = 0; + } + printf("%.8f\n", res); + return true; + } + assert(0 == 1); + return false; +} + +int main() +{ + ll K, F; + for (int cs = 1; scanf("%d %lld", &N, &K) == 2; cs++) + { + if (N == 0 && K == 0) + { + break; + } + assert(1 <= N && N <= 12); + assert(K >= 0); + REP(i, N) + { + scanf("%d %d", &f[i].per, &f[i].inc); + assert(1 <= f[i].per && f[i].per <= 12); + assert(-100 <= f[i].inc && f[i].inc <= 100); + f[i].half = f[i].per; + f[i].per *= 2; + } + scanf("%lld", &F); + goal = F - K; + printf("Case %d: ", cs); + if (F < 0 || !solve()) + { + printf("Impossible.\n"); + } + } +} diff --git a/1108.cpp b/1108.cpp new file mode 100644 index 0000000..48fc798 --- /dev/null +++ b/1108.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +const double pi = 4.0 * atan(1.0); + +#define clr(x) memset(x, 0, sizeof(x)) +#define clro(x) memset(x, -1, sizeof(x)) +#define sf scanf +#define pf printf + +typedef signed long long LL; +typedef pair pii; + +const int inf = 999999999; +const int maxn = 55555; +const int maxm = 200000; +struct Edge +{ + int v, nex; + Edge(){}; + Edge(int _v, int _nex) + { + v = _v, nex = _nex; + } +}; +Edge edges[maxm]; +int tot; + +int pre[maxn], bccno[maxn], iscut[maxn], dfs_clock, bcc_cnt; +int g[maxn]; +int n, T; + +vector bcc[maxn]; +stack S; + +void clear() +{ + tot = 0; + for (int i = 0; i < maxn; ++i) + { + bcc[i].clear(); + } + while (!S.empty()) + { + S.pop(); + } + clro(g); + clr(pre); + clr(iscut); + clr(bccno); + dfs_clock = bcc_cnt = 0; +} +void addedge(int u, int v) +{ + edges[tot] = Edge(v, g[u]); + g[u] = tot++; +} +int dfs(int u, int fa) +{ + int lowu, child, i, v; + lowu = pre[u] = ++dfs_clock; + child = 0; + for (i = g[u]; ~i; i = edges[i].nex) + { + v = edges[i].v; + pii e(u, v); + if (!pre[v]) + { + S.push(e); + child++; + int lowv = dfs(v, u); + lowu = min(lowv, lowu); + if (lowv >= pre[u]) + { + iscut[u] = true; + ++bcc_cnt; + while (true) + { + pii x = S.top(); + S.pop(); + if (bccno[x.first] != bcc_cnt) + { + bccno[x.first] = bcc_cnt; + bcc[bcc_cnt].push_back(x.first); + } + if (bccno[x.second] != bcc_cnt) + { + bccno[x.second] = bcc_cnt; + bcc[bcc_cnt].push_back(x.second); + } + if (x.first == u && x.second == v) + { + break; + } + } + } + } + else if (pre[v] < pre[u] && v != fa) + { + S.push(e); + lowu = min(lowu, pre[v]); + } + } + if (fa < 0 && child == 1) + { + iscut[u] = 0; + } + return lowu; +} +void find(int n) +{ + for (int i = 0; i < n; ++i) + if (!pre[i]) + { + dfs(i, -1); + } +} +void input() +{ + int a, b, i; + for (i = 0; i < n; ++i) + { + sf("%d%d", &a, &b); + --a, --b; + addedge(a, b); + addedge(b, a); + } +} +void solve() +{ + int i, j, cnt; + LL ans1 = 0, ans2 = 1; + find(n); + for (i = 1; i <= bcc_cnt; ++i) + { + cnt = 0; + for (j = 0; j < bcc[i].size(); ++j) + { + if (iscut[bcc[i][j]]) + { + ++cnt; + } + } + if (cnt == 1) + { + ++ans1; + ans2 *= (LL)(bcc[i].size() - 1); + } + } + if (bcc_cnt == 1) + { + ans1 = 2, ans2 = (LL)(bcc[1].size()) * (LL)(bcc[1].size() - 1) / 2; + } + pf("Case %d: %lld %lld\n", ++T, ans1, ans2); +} +int main() +{ + T = 0; + while (sf("%d", &n) == 1) + { + if (0 == n) + { + break; + } + clear(); + input(); + solve(); + } + return 0; +} diff --git a/11080.cpp b/11080.cpp new file mode 100644 index 0000000..8809946 --- /dev/null +++ b/11080.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a) +#define maxn 205 + +vector links[maxn]; +queue Q; +char Fg[maxn]; +int N, E, Fre[3], H[maxn]; + +int BFS(int n) +{ + int i, u, v, c; + H[n] = 1; + Fg[n] = 1; + Q.push(n); + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + c = 1; + Fre[H[u]]++; + if (H[u] == 1) + c = 2; + for (i = 0; i < links[u].size(); i++) + { + v = links[u][i]; + if (!Fg[v]) + { + H[v] = c; + Fg[v] = 1; + Q.push(v); + } + else if (H[v] == H[u]) + return -1; + } + } + return 1; +} + +void Cal() +{ + int i, res = 0, d, m; + for (i = 0; i < N; i++) + { + if (Fg[i]) + continue; + Fre[1] = Fre[2] = 0; + d = BFS(i); + if (d < 0) + { + cout << d << endl; + while (!Q.empty()) + Q.pop(); + return; + } + m = MIN(Fre[1], Fre[2]); + if (!m) + m = 1; + res += m; + } + cout << res << endl; +} + +void Free() +{ + int i; + for (i = 0; i < N; i++) + { + Fg[i] = 0; + H[i] = 0; + links[i].clear(); + } + Fre[1] = Fre[2] = 0; +} + +int main() +{ + int k, i, u, v; + cin >> k; + while (k--) + { + cin >> N >> E; + for (i = 0; i < E; i++) + { + cin >> u >> v; + links[u].push_back(v); + links[v].push_back(u); + } + Cal(); + Free(); + } + return 0; +} diff --git a/11081.cpp b/11081.cpp new file mode 100644 index 0000000..4d5d317 --- /dev/null +++ b/11081.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +const int MAXL = 60 + 5; + +int T, len1, len2, len3, dp1[MAXL][MAXL][MAXL], dp2[MAXL][MAXL][MAXL], dp[MAXL][MAXL][MAXL]; +char str1[MAXL], str2[MAXL], str3[MAXL]; + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%s%s%s", str1 + 1, str2 + 1, str3 + 1); + len1 = strlen(str1 + 1), len2 = strlen(str2 + 1), len3 = strlen(str3 + 1); + memset(dp1, 0, sizeof(dp1)); + memset(dp2, 0, sizeof(dp2)); + memset(dp, 0, sizeof(dp)); + for (int j = 0; j <= len1; j++) + for (int k = 0; k <= len2; k++) + dp1[j][k][0] = dp2[j][k][0] = dp[j][k][0] = 1; + for (int i = 1; i <= len3; i++) + for (int j = 0; j <= len1; j++) + for (int k = 0; k <= len2; k++) + { + if (j) + { + dp1[j][k][i] = dp1[j - 1][k][i]; + if (str3[i] == str1[j]) + dp1[j][k][i] = (dp1[j][k][i] + dp[j - 1][k][i - 1]) % 10007; + } + + if (k) + { + dp2[j][k][i] = dp2[j][k - 1][i]; + if (str3[i] == str2[k]) + dp2[j][k][i] = (dp2[j][k][i] + dp[j][k - 1][i - 1]) % 10007; + } + dp[j][k][i] = (dp1[j][k][i] + dp2[j][k][i]) % 10007; + } + printf("%d\n", dp[len1][len2][len3]); + } + return 0; +} diff --git a/11082.cpp b/11082.cpp new file mode 100644 index 0000000..2fa952c --- /dev/null +++ b/11082.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +///////////////////////////////// +// 11082 - Matrix Decompressing +///////////////////////////////// +#define min(a, b) ((a < b) ? (a) : (b)) +int cnum = 0, cs, i, j, n, C, R; +int SL[50], m[50][50]; +struct COL +{ + int cap, cn; +}; +COL SC[50]; +bool ord(COL a, COL b) +{ + return (a.cap > b.cap); +} +void fill(int x, int y) +{ + m[x][SC[y].cn] = min(20, min(SL[x], SC[y].cap)); + SL[x] -= (m[x][SC[y].cn] - 1); + SC[y].cap -= (m[x][SC[y].cn] - 1); +} +int main() +{ + scanf("%d", &cs); + while (cs--) + { + scanf("%d %d", &R, &C); + /* INPUT */ + for (i = 0; i < R; i++) + { + scanf("%d", &SL[i]); + } + for (j = 0; j < C; j++) + { + scanf("%d", &SC[j].cap), SC[j].cn = j; + } + for (i = R; --i;) + { + SL[i] -= SL[i - 1]; + } + for (j = C; --j;) + { + SC[j].cap -= SC[j - 1].cap; + } + for (i = 0; i < R; i++) + { + SL[i] -= (C - 1); + } + for (j = 0; j < C; j++) + { + SC[j].cap -= (R - 1); + } + /* ALGORITMO */ + for (i = 0; i < (R - 1); i++) + { + std::sort(SC, SC + C, ord); + for (j = 0; j < C; j++) + { + fill(i, j); + } + } + for (j = 0; j < C; j++) + { + int k = SC[j].cn, L = 1; + m[R - 1][k] = SC[j].cap; + if (m[R - 1][k] > 20) + { + int remain = m[R - 1][k] - 20; + while (m[R - 1 - L][k] > 20 - remain) + { + L++; + } + m[R - 1 - L][k] += remain; + m[R - 1][k] -= remain; + for (int A = 0; A < C; A++) + if (A != k && m[R - 1 - L][A] > remain && m[R - 1][A] < 21 - remain) + { + m[R - 1 - L][A] -= remain; + m[R - 1][A] += remain; + break; + } + } + } + /* OUTPUT */ + printf("Matrix %d\n", ++cnum); + for (i = 0; i < R; putchar('\n'), i++) + { + printf("%d", m[i][0]); + for (j = 1; j < C; j++) + { + printf(" %d", m[i][j]); + } + } + putchar('\n'); + } + return 0; +} diff --git a/11083.cpp b/11083.cpp new file mode 100644 index 0000000..68a32ff --- /dev/null +++ b/11083.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +/* +11083 +*/ +#define maxn 317 +#define MIN(a, b) (a > b ? b : a) +typedef long long SS; +char sv[maxn + 2]; +int P[70], ip; +SS N, B; +void Table() +{ + int i, j; + for (i = 2; i * i < maxn;) + { + for (j = i + i; j < maxn; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + P[0] = 2; + ip = 1; + for (i = 3; i < maxn; i += 2) + { + if (!sv[i]) + { + P[ip++] = i; + } + } +} +SS Zero(SS n) +{ + SS rem, r, c = 0; + r = N / n; + if (r == 1) + { + return N - n + 1; + } + rem = N % n + 1; + c += rem * r; + r--; + rem = r + 1; + if (r % 2) + { + return ((rem / 2) * r) * n + c; + } + return (rem * (r / 2)) * n + c; +} +SS Count(int p) +{ + SS i, c = 0; + if (p > N) + { + return 0; + } + for (i = p; i <= N; i *= p) + { + c += Zero(i); + } + return c; +} +void Cal() +{ + SS n = B, i, min, c, z, f = 0; + for (i = 0; i < ip && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + z = Count(P[i]); + c = 0; + while (n % P[i] == 0) + { + c++; + n /= P[i]; + } + if (f == 0) + { + f = 1; + min = z / c; + } + else + { + min = MIN(min, z / c); + } + if (!min) + { + printf("0\n"); + return; + } + } + } + if (n > 1) + { + z = Count(n); + if (f == 0) + { + min = z; + } + else + { + min = MIN(min, z); + } + } + printf("%lld\n", min); +} +int main() +{ + Table(); + while (scanf("%lld%lld", &N, &B) == 2) + { + if (!N && !B) + { + break; + } + Cal(); + } + return 0; +} diff --git a/11084.cpp b/11084.cpp new file mode 100644 index 0000000..87c7444 --- /dev/null +++ b/11084.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int cmp(const void *a, const void *b) +{ + return *(char *)a - *(char *)b; +} +int len, ans, d; +long long int tmp; +char s[15], tc; +void perm(int x) +{ + int i; + if (x == len) + { + for (i = 0, tmp = 0LL; i <= len; i++) + { + tmp = 10LL * tmp + s[i] - '0'; + } + if (tmp % d == 0) + { + ans++; + } + return; + } + perm(x + 1); + for (i = x + 1; i <= len; i++) + { + if (s[x] == s[i]) + { + continue; + } + tc = s[x]; + s[x] = s[i]; + s[i] = tc; + perm(x + 1); + } + for (i = len; i > x; i--) + { + if (s[x] == s[i]) + { + continue; + } + tc = s[x]; + s[x] = s[i]; + s[i] = tc; + } +} +int main() +{ + int t, i; + scanf("%d", &t); + while (t--) + { + scanf("%s%d", s, &d); + len = strlen(s); + qsort(s, len, sizeof(char), cmp); + len--; + ans = 0; + perm(0); + printf("%d\n", ans); + } + return 0; +} diff --git a/11085.cpp b/11085.cpp new file mode 100644 index 0000000..14f9431 --- /dev/null +++ b/11085.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +enum boolean +{ + False, + True +}; +enum boolean a[9], b[17], c[17]; +int s[9]; + +int cnt = 0; + +struct Queen +{ + int row[10], col[10]; +} queen[100]; + +void print() +{ + for (int k = 1; k <= 8; k++) + { + queen[cnt].col[k] = k; + queen[cnt].row[k] = s[k]; + } + cnt++; +} + +void movequeen(int i, int j) +{ + a[j] = True; + b[i + j] = True; + c[i - j + 9] = True; +} +void eightqueen() +{ + int i, j; + for (i = 2; i <= 16; i++) + { + if (i >= 2 && i <= 9) + { + a[i - 1] = True; + } + b[i] = c[i] = True; + } + i = 1; + j = 1; + while (i >= 1) + { + while (j <= 8) + { + if (a[j] && b[i + j] && c[i - j + 9]) + { + break; + } + j++; + } + if (j <= 8) + { + a[j] = b[i + j] = c[i - j + 9] = False; + s[i] = j; + if (i == 8) + { + print(); + movequeen(i, j); + i = i - 1; + j = s[i]; + movequeen(i, j); + j++; + } + else + { + i++; + j = 1; + } + } + else + { + i--; + if (i >= 1) + { + j = s[i]; + movequeen(i, j); + j++; + } + } + } +} + +int main() +{ + eightqueen(); + int row[10], kase = 1; + while (scanf("%d%d%d%d%d%d%d%d", &row[1], &row[2], &row[3], &row[4], + &row[5], &row[6], &row[7], &row[8]) == 8) + { + int min = 1000000; + int i, j; + for (i = 0; i < cnt; i++) + { + int step = 0; + for (j = 1; j <= 8; j++) + if (row[j] != queen[i].row[j]) + { + step++; + } + if (step < min) + { + min = step; + } + } + printf("Case %d: %d\n", kase++, min); + } + return 0; +} diff --git a/11086.cpp b/11086.cpp new file mode 100644 index 0000000..c3cbaae --- /dev/null +++ b/11086.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int P[500] = {2, 3}, ptr; +int main() +{ + int i, j, k, x, ans, sq, ok, p; + for (ptr = 2; P[ptr - 1] < 1024; ptr++) + { + for (i = P[ptr - 1] + 2; i < 1200; i += 2) + { + sq = (int)(sqrt((double)i) + 0.1); + for (j = 0, ok = 1; P[j] <= sq; j++) + { + if (i % P[j]) + { + continue; + } + ok = 0; + break; + } + if (ok) + { + break; + } + } + P[ptr] = i; + } + while (scanf("%d", &k) == 1) + { + ans = 0; + while (k--) + { + scanf("%d", &x); + p = 0; + for (i = 0; i < ptr; i++) + { + if (x % P[i] == 0) + { + x /= P[i]; + p++; + break; + } + } + for (; i < ptr; i++) + { + if (x % P[i] == 0) + { + x /= P[i]; + p++; + break; + } + } + if (x != 1) + { + p++; + } + if (p == 2) + { + ans++; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11087.cpp b/11087.cpp new file mode 100644 index 0000000..3c3ed50 --- /dev/null +++ b/11087.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define MAXN 1002 + +int K, A[MAXN], N[100002], cnt; + +int Com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +int NCR(int n) +{ + int m = n - 1; + if (m % 2) + { + return (n / 2) * m; + } + return n * (m / 2); +} + +void Pos(int n) +{ + int d; + d = K - n + K; + n += K; + if (d != n) + { + cnt += A[d] * A[n]; + } + else + { + cnt += NCR(A[d]); + } +} + +void Neg(int n) +{ + int negn, d; + negn = -n; + d = -(K - n) + K; + negn += K; + if (negn != d) + { + cnt += A[negn] * A[d]; + } + else + { + cnt += NCR(A[negn]); + } +} + +void Cal() +{ + int i; + for (i = 1; i <= K / 2; i++) + { + Pos(i); + Neg(i); + } + cnt += NCR(A[K]); + printf("%d\n", cnt); +} + +void Setting(int n) +{ + int i, c = 0, d; + qsort(N, n, sizeof(int), Com); + cnt = 0; + d = N[0] % K; + d += K; + A[d]++; + for (i = 1; i < n; i++) + { + if (N[i] == N[i - 1]) + { + c++; + if (c == 1 && 2 * N[i] % K == 0) + { + cnt++; + } + } + else + { + c = 0; + d = N[i] % K; + d += K; + A[d]++; + if (N[i] > 0 && d != K) + { + d -= K; + d = K - d; + cnt += A[d]; + } + } + } +} + +void Free() +{ + int i; + for (i = 0; i < 2 * K; i++) + { + A[i] = 0; + } +} + +int main() +{ + int k = 1, kase, n, i; + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &n, &K); + for (i = 0; i < n; i++) + { + scanf("%d", &N[i]); + } + Setting(n); + printf("Case %d: ", k++); + Cal(); + Free(); + } + return 0; +} diff --git a/11088.cpp b/11088.cpp new file mode 100644 index 0000000..8d431f6 --- /dev/null +++ b/11088.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define MAXN 17 + +int A[MAXN], N, tri, maximum, ter; +char Fg[MAXN]; + +struct ss +{ + int a, b, c; +}; +ss Tri[500]; + +int com(const void *a, const void *b) +{ + return *(int *)b - *(int *)a; +} + +void Make() +{ + int i, j, k, sum; + tri = 0; + for (i = 0; i <= N - 3; i++) + { + if (A[i] + A[i + 1] + A[i + 2] < 20) + break; + for (j = i + 1; j <= N - 2; j++) + { + if (A[i] + A[j] + A[j + 1] < 20) + break; + for (k = j + 1; k < N; k++) + { + sum = A[k] + A[i] + A[j]; + if (sum < 20) + break; + if (sum >= 20) + { + Tri[tri].a = i; + Tri[tri].b = j; + Tri[tri++].c = k; + } + } + } + } +} + +int Recur(int ind, int level) +{ + int i, d = 0; + if (level > maximum) + maximum = level; + if (level == ter) + return 1; + Fg[Tri[ind].a] = 1; + Fg[Tri[ind].b] = 1; + Fg[Tri[ind].c] = 1; + for (i = ind + 1; i < tri; i++) + { + if (Fg[Tri[i].a] || Fg[Tri[i].b] || Fg[Tri[i].c]) + continue; + d = Recur(i, level + 1); + if (d) + break; + } + Fg[Tri[ind].a] = 0; + Fg[Tri[ind].b] = 0; + Fg[Tri[ind].c] = 0; + if (d) + return 1; + return 0; +} + +void Find() +{ + int i; + ter = N / 3; + maximum = 0; + for (i = 0; i < tri; i++) + if (Recur(i, 1)) + break; + cout << maximum << endl; +} + +void Cal() +{ + int n = N; + if (N < 3) + { + cout << 0 << endl; + return; + } + qsort(A, N, sizeof(int), com); + Make(); + Find(); +} + +int main() +{ + int i, k = 1; + while (cin >> N && N) + { + for (i = 0; i < N; i++) + cin >> A[i]; + cout << "Case " << k++ << ": "; + Cal(); + } + return 0; +} diff --git a/11089.cpp b/11089.cpp new file mode 100644 index 0000000..796800d --- /dev/null +++ b/11089.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +/* +11089 +*/ + +#define maxn 51 +int Way[maxn][maxn / 2]; +int T[maxn]; +void Dynamic() +{ + int i, j, k, n, total = 4, t; + Way[1][1] = Way[2][1] = Way[3][1] = Way[3][2] = 1; + T[1] = 1; + T[2] = 2; + T[3] = 4; + for (i = 4; i < maxn; i++) + { + Way[i][1] = 1; + Way[i][2] = i - 2; + total += i - 1; + n = (i + 1) / 2; + t = i - 1; + for (j = 3; j <= n; j++) + { + for (k = i - 2; i >= 1 && Way[k][j - 1]; k--) + { + Way[i][j] += Way[k][j - 1]; + total += Way[k][j - 1]; + t += Way[k][j - 1]; + if (total >= 1000000000) + { + break; + } + } + if (total >= 1000000000) + { + break; + } + } + T[i] = t + T[i - 1]; + if (total >= 1000000000) + { + break; + } + } +} +int Len(int n) +{ + int i; + for (i = 1; i < 50 && T[i] < n; i++) + ; + return i; +} +void Cal(int n) +{ + int len, rem, i, res[50] = {0}; + int l = 3, r, m, p, k; + len = r = Len(n); + rem = n - T[len - 1] - 1; + res[1] = 1; + m = len - 2; + for (i = 2; i <= len && rem; i++) + { + p = Len(rem); + rem -= T[p - 1]; + rem--; + k = p - 1; + res[len - k] = 1; + m = len - k + 2; + } + for (i = 1; i <= len; i++) + { + cout << res[i]; + } + cout << endl; +} +int main() +{ + int k, n; + Dynamic(); + cin >> k; + while (k--) + { + cin >> n; + Cal(n); + } + return 0; +} diff --git a/1109.cpp b/1109.cpp new file mode 100644 index 0000000..91635b1 --- /dev/null +++ b/1109.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define PI pair +#define MP make_pair +#define x first +#define y second + +const int maxn = 100100; + +int N, m; +inline bool in(int x, int y) +{ + return x >= -m && x <= m && y >= -m && y <= m; +} + +PI p[maxn]; +PI a[maxn], b[maxn], c[maxn], d[maxn]; +int ea, eb, ec, ed; + +bool chk() +{ + int i, j, k; + + ea = eb = ec = ed = -1; + for (i = 0; i < N; ++i) + { + if (in(p[i].x + m, p[i].y - m)) + { + while (ea >= 0 && a[ea].y >= p[i].y - m) + ea--; + if (ea >= 0 && a[ea].x == p[i].x + m) + continue; + a[++ea] = MP(p[i].x + m, p[i].y - m); + } + if (in(p[i].x + m, p[i].y + m)) + { + while (eb >= 0 && b[eb].y <= p[i].y + m) + eb--; + if (eb >= 0 && b[eb].x == p[i].x + m) + continue; + b[++eb] = MP(p[i].x + m, p[i].y + m); + } + } + for (i = N - 1; i >= 0; --i) + { + if (in(p[i].x - m, p[i].y - m)) + { + while (ec >= 0 && c[ec].y >= p[i].y - m) + ec--; + if (ec >= 0 && c[ec].x == p[i].x - m) + continue; + c[++ec] = MP(p[i].x - m, p[i].y - m); + } + if (in(p[i].x - m, p[i].y + m)) + { + while (ed >= 0 && d[ed].y <= p[i].y + m) + ed--; + if (ed >= 0 && d[ed].x == p[i].x - m) + continue; + d[++ed] = MP(p[i].x - m, p[i].y + m); + } + } + + int na = 0, nb = 0, nc = ec + 1, nd = ed + 1; + int v1, v2; + for (i = -m; i <= m; ++i) + { + if (nc > 0 && c[nc - 1].x == i) + nc--; + if (nd > 0 && d[nd - 1].x == i) + nd--; + + v1 = m + 1, v2 = -m - 1; + + if (na <= ea) + v1 = min(v1, a[na].y); + if (nb <= eb) + v2 = max(v2, b[nb].y); + if (nc <= ec) + v1 = min(v1, c[nc].y); + if (nd <= ed) + v2 = max(v2, d[nd].y); + + if (v1 - 1 > v2) + return 0; + + if (na <= ea && a[na].x == i) + na++; + if (nb <= eb && b[nb].x == i) + nb++; + } + + return 1; +} + +int main() +{ + int i, j, k, cases = 1; + int ll, rr; + + while (scanf("%d", &N), N + 1) + { + for (i = 0; i < N; ++i) + scanf("%d %d", &p[i].x, &p[i].y); + sort(p, p + N); + + ll = -1, rr = 1000000; + while (rr - ll > 1) + { + m = (ll + rr) / 2; + if (chk()) + rr = m; + else + ll = m; + } + + m = rr; + printf("Case %d: ", cases++); + if (chk()) + printf("%d\n", rr); + else + puts("never"); + } + + return 0; +} diff --git a/11090.cpp b/11090.cpp new file mode 100644 index 0000000..bf901f1 --- /dev/null +++ b/11090.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +const int INF = 100000000; +const int MAX = 50; +int n, m, len, weight[MAX][MAX], inv[MAX][MAX], s[MAX], c[MAX]; + +double min(double x, double y) +{ + return x < y ? x : y; +} + +double max(double x, double y) +{ + return x > y ? x : y; +} + +void DFS1(int u) +{ + c[u] = 1; + for (int i = 0; i < n; i++) + if (weight[u][i] < INF && !c[i]) + DFS1(i); + s[len--] = u; +} + +void DFS2(int u, int color) +{ + c[u] = color; + for (int i = 0; i < n; i++) + if (inv[u][i] < INF && !c[i]) + DFS2(i, color); +} + +int d[MAX + 1][MAX]; + +double MMC(int s, int color) +{ + //Initialize + int k, u, v; + for (k = 0; k <= n; k++) + for (u = 0; u < n; u++) + d[k][u] = INF; + d[0][s] = 0; + + //Compute distances + for (k = 1; k <= n; k++) + for (v = 0; v < n; v++) + for (u = 0; u < n; u++) + if (weight[u][v] < INF) + d[k][v] = min(d[k][v], d[k - 1][u] + weight[u][v]); + + //Compute lamda using karp's theorem + double lamda = INF; + for (u = 0; u < n; u++) + { + double currentLamda = 0; + for (int k = 0; k < n; k++) + if (d[n][u] < INF && d[k][u] < INF && c[u] == color) + currentLamda = max(currentLamda, 1.0 * (d[n][u] - d[k][u]) / (n - k)); + if (currentLamda) + lamda = min(lamda, currentLamda); + } + return lamda; +} + +int main() +{ + int tt; + cin >> tt; + for (int t = 0; t < tt; t++) + { + cin >> n >> m; + int i; + for (i = 0; i < n; i++) + for (int j = 0; j < n; j++) + weight[i][j] = inv[i][j] = INF; + for (i = 0; i < m; i++) + { + int l, r, c; + cin >> l >> r >> c; + weight[l - 1][r - 1] = min(c, weight[l - 1][r - 1]); + inv[r - 1][l - 1] = min(c, inv[r - 1][l - 1]); + } + cout << "Case #" << t + 1 << ": "; + len = n - 1; + memset(c, 0, sizeof(c)); + for (int i = 0; i < n; i++) + if (!c[i]) + DFS1(i); + memset(c, 0, sizeof(c)); + int time = 0; + double l = INF; + for (int i = 0; i < n; i++) + if (!c[s[i]]) + { + DFS2(s[i], ++time); + l = min(l, MMC(s[i], time)); + } + if (l < INF) + { + cout.setf(ios::fixed); + cout.setf(ios::showpoint); + cout.precision(2); + cout << l; + } + else + cout << "No cycle found."; + cout << endl; + } + return 0; +} diff --git a/11091.cpp b/11091.cpp new file mode 100644 index 0000000..24f6a83 --- /dev/null +++ b/11091.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +#define REP(i, n) for (int _REP_N = (n), i = 0; i < _REP_N; i++) + +#define MOD 10007 + +int cfg_id[8][8][8][8], ncfg; +struct Config +{ + int a, b, c, d, prev[4], atk[6]; +} cfg[500]; + +int res[50][64], mat[50][64], dim; + +void power(int e) +{ + static int tmp[50][64]; + memset(res, 0, sizeof(res)); + REP(i, dim) + res[i][i] = 1; + if (e <= 0) + { + return; + } + int t; + for (t = 31; (e & (1 << t)) == 0; t--) + ; + for (; t >= 0; t--) + { + REP(i, dim) + REP(j, dim) + { + tmp[i][j] = 0; + REP(k, dim) + tmp[i][j] = (tmp[i][j] + res[i][k] * res[k][j]) % MOD; + } + if (e & (1 << t)) + { + REP(i, dim) + REP(j, dim) + { + res[i][j] = 0; + REP(k, dim) + res[i][j] = (res[i][j] + tmp[i][k] * mat[k][j]) % MOD; + } + } + else + { + REP(i, dim) + REP(j, dim) res[i][j] = tmp[i][j]; + } + } +} + +int mir(int r) +{ + return r < 6 ? 5 - r : 6; +} + +int main() +{ + memset(cfg_id, 0xff, sizeof(cfg_id)); + ncfg = 0; + REP(a, 7) + REP(b, 7) REP(c, 7) REP(d, 7) + { + if (cfg_id[a][b][c][d] >= 0 || (a < 6 && a == b) || (c < 6 && c == d)) + { + continue; + } + if (a < 6 && c < 6 && c == a - 2 || c == a + 2) + { + continue; + } + if (a < 6 && d < 6 && d == a - 2 || d == a + 2) + { + continue; + } + if (b < 6 && c < 6 && c == b - 2 || c == b + 2) + { + continue; + } + if (b < 6 && d < 6 && d == b - 2 || d == b + 2) + { + continue; + } + if ((a < 6) ^ (b < 6)) + { + continue; + } + if ((c < 6) ^ (d < 6)) + { + continue; + } + if (a < 6 && c == 6) + { + continue; + } + int k = ncfg++; + assert(k < 50); + REP(i, 2) + REP(j, 2) + { + cfg_id[i == 0 ? a : b][i == 0 ? b : a][j == 0 ? c : d][j == 0 ? d : c] = k; + cfg_id[mir(i == 0 ? a : b)][mir(i == 0 ? b : a)][mir(j == 0 ? c : d)][mir(j == 0 ? d : c)] = k; + } + Config cf = {a, b, c, d, {a, b, c, d}, {0, 0, 0, 0, 0, 0}}; + REP(i, 2) + REP(j, 2) + { + int c0 = i - 2, r0 = cf.prev[2 * i + j]; + if (r0 == 6) + { + continue; + } + for (int dr = -2; dr <= 2; dr++) + for (int dc = -2; dc <= 2; dc++) + { + if ((abs(dr) == 2 && abs(dc) == 1) || (abs(dr) == 1 && abs(dc) == 2)) + { + int c1 = c0 + dc, r1 = r0 + dr; + if (c1 == 0 && 0 <= r1 && r1 < 6) + { + cf.atk[r1] = 1; + } + } + } + } + cfg[k] = cf; + } + memset(mat, 0, sizeof(mat)); + dim = ncfg; + int root = cfg_id[6][6][6][6]; + REP(i, ncfg) + { + REP(e, 6) + REP(f, 6) + { + if (e >= f || cfg[i].atk[e] || cfg[i].atk[f]) + { + continue; + } + assert(cfg_id[cfg[i].c][cfg[i].d][e][f] >= 0); + mat[i][cfg_id[cfg[i].c][cfg[i].d][e][f]]++; + } + } + int T, N; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d", &N) == 1; cs++) + { + power(N); + int ans = 0; + REP(k, dim) + ans = (ans + res[root][k]) % MOD; + printf("%d\n", ans); + } +} diff --git a/11092.cpp b/11092.cpp new file mode 100644 index 0000000..0447b6c --- /dev/null +++ b/11092.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int main() +{ + double sq3 = sqrt(3.0) / 2.0, ax, ay, bx, by, d1, d2; + double NX[6] = {0.5, -0.5, -1.0, -0.5, 0.5, 1.0}, NY[6] = {sq3, sq3, 0.0, -sq3, -sq3, 0.0}; + double UX[6] = {0.5, 1.0, 0.5, -0.5, -1.0, -0.5}, UY[6] = {-sq3, 0.0, sq3, sq3, 0.0, -sq3}; + int a, b, ra, rb, ta, tb, ma, mb; + while (scanf("%d%d", &a, &b) == 2) + { + if (a) + { + ra = (int)(sqrt(a / 6.0) + 0.00001); + while (3 * (ra + 1) * ra < a) + { + ra++; + } + if (a == 3 * ra * (ra + 1)) + { + ma = 0; + } + else + { + ma = a - 3 * (ra - 1) * ra; + } + ax = ra * UX[ma / ra] + (ma % ra) * NX[ma / ra]; + ay = ra * UY[ma / ra] + (ma % ra) * NY[ma / ra]; + } + else + { + ax = ay = 0.0; + } + if (b) + { + rb = (int)(sqrt(b / 6.0) + 0.00001); + while (3 * (rb + 1) * rb < b) + { + rb++; + } + if (b == 3 * rb * (rb + 1)) + { + mb = 0; + } + else + { + mb = b - 3 * (rb - 1) * rb; + } + bx = rb * UX[mb / rb] + (mb % rb) * NX[mb / rb]; + by = rb * UY[mb / rb] + (mb % rb) * NY[mb / rb]; + } + else + { + bx = by = 0.0; + } + d1 = (double)abs(a - b) + 0.00000000001; + d2 = (ax - bx) * (ax - bx) + (ay - by) * (ay - by); + d2 = sqrt(d2) + 0.00000000001; + printf("%.3lf %.3lf\n", d1, d2); + } + return 0; +} diff --git a/11093.cpp b/11093.cpp new file mode 100644 index 0000000..643fccd --- /dev/null +++ b/11093.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +/* +11093 +*/ + +#define maxn 100002 +int P[maxn], Q[maxn], N; +int Oil; +int FindF() +{ + int i, p = 1; + Oil = P[1]; + for (i = 2; i <= N; i++) + { + if (Oil >= Q[i - 1]) + { + Oil -= Q[i - 1]; + Oil += P[i]; + } + else + { + p = i; + Oil = P[i]; + } + } + return p; +} +void Cal() +{ + int lim, i, pre; + lim = FindF(); + if (lim == 1) + { + cout << "Possible from station " << lim << endl; + return; + } + pre = Q[N]; + for (i = 1; i <= lim; i++) + { + if (Oil >= pre) + { + Oil -= pre; + Oil += P[i]; + pre = Q[i]; + } + else + { + cout << "Not possible\n"; + return; + } + } + cout << "Possible from station " << lim << endl; +} +int main() +{ + int k, i, ks = 1; + cin >> k; + while (k--) + { + cin >> N; + for (i = 1; i <= N; i++) + { + cin >> P[i]; + } + for (i = 1; i <= N; i++) + { + cin >> Q[i]; + } + cout << "Case " << ks++ << ": "; + Cal(); + } + return 0; +} diff --git a/11094.cpp b/11094.cpp index f6bb10a..d566a4b 100644 --- a/11094.cpp +++ b/11094.cpp @@ -1,59 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -string Map[50]; -int M, N; -char land; +#define num 20 -int dI[] = {-1, 0, 1, 0}; -int dJ[] = {0, 1, 0, -1}; -int dfs(int A, int B) { - stack< pair > S; - S.push(make_pair(A, B)); - int V = 0; - while (!S.empty()) { - pair P = S.top(); - S.pop(); - int I = P.first; - int J = P.second; - if (I < 0 || I >= M) continue; - if (J < 0 || J >= N) continue; - if (Map[I][J] != land)continue; - - Map[I][J] = ' '; - ++V; - for (int i = 0; i < 4; i++) - S.push(make_pair(I + dI[i], (J + dJ[i] + N) % N)); - } - return V; -} +char mat[num][num], land; +int mark[num][num]; +int n, m, sr, sc; +int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - while (cin >> M >> N) { - string str; - for (int i = 0; i < M; i++) - cin >> Map[i]; - - int A, B, Max = 0; - cin >> A >> B; - - land = Map[A][B]; - dfs(A, B); - - for (int i = 0; i < M; i++) - for (int j = 0; j < N; j++) - Max = max(Max, dfs(i, j)); +int dfs(int x, int y) +{ + int ret = 1; + mark[x][y] = 1; + for (int i = 0; i < 4; i++) + if (x + dir[i][0] >= 0 && x + dir[i][0] < n && !mark[x + dir[i][0]][(y + dir[i][1] + m) % m] && mat[x + dir[i][0]][(y + dir[i][1] + m) % m] == land) + { + ret += dfs(x + dir[i][0], (y + dir[i][1] + m) % m); + } + return ret; +} - cout << Max << endl; +int main() +{ + while (cin >> n >> m) + { + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + cin >> mat[i][j]; + } + cin >> sr >> sc; + memset(mark, 0, sizeof mark); + land = mat[sr][sc]; + dfs(sr, sc); + int mx = 0; + int x; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (!mark[i][j] && mat[i][j] == land && (x = dfs(i, j)) > mx) + { + mx = x; + } + cout << mx << endl; } return 0; } diff --git a/11095.cpp b/11095.cpp new file mode 100644 index 0000000..e2dc3be --- /dev/null +++ b/11095.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +#define num 31 + +int mat[num][num]; +vector adj[num]; +int dep[num]; +int ans[num]; +int cur[num]; +int nfin; +int m, n, bestAns, t; +int mark[num]; +int change[num]; + +void check(int x, int &size) +{ + //cout<<"new: "<::iterator it = adj[x].begin(); it != adj[x].end(); it++) + if (!mat[x][*it]) + { + cur[size++] = *it; + mark[*it] = 1; + for (vector::iterator jt = adj[*it].begin(); jt != adj[*it].end(); jt++) + { + if (!mat[*jt][*it]) + { + nfin--; + } + mat[*jt][*it]++; + mat[*it][*jt]++; + dep[*it]--; + } + for (vector::iterator jt = adj[*it].begin(); jt != adj[*it].end(); jt++) + if (dep[*jt] == 1) + { + check(*jt, size); + } + return; + } +} + +void back(int x, int size) +{ + //cout<<"In Back("<= bestAns - 1) + { + return; + } + if (x >= n) + { + return; + } + if (mark[x]) + { + back(x + 1, size); + return; + } + int flag = 0; + int tmat[num][num]; + int tdep[num]; + int tmark[num]; + int tnfin = nfin; + int tsize = size; + for (int i = 0; i < n; i++) + if (dep[i] == 1) + { + if (!flag) + { + memcpy(tmat, mat, sizeof mat); + memcpy(tdep, dep, sizeof dep); + memcpy(tmark, mark, sizeof mark); + } + flag = 1; + check(i, size); + } + if (!dep[x]) + { + back(x + 1, size); + if (flag) + { + memcpy(mat, tmat, sizeof mat); + memcpy(dep, tdep, sizeof dep); + memcpy(mark, tmark, sizeof mark); + nfin = tnfin; + size = tsize; + } + return; + } + for (vector::iterator it = adj[x].begin(); it != adj[x].end(); it++) + { + if (!mat[x][*it]) + { + nfin--; + } + mat[x][*it]++; + mat[*it][x]++; + dep[*it]--; + cur[size] = x; + } + int tmp = dep[x]; + dep[x] = 0; + back(x + 1, size + 1); + for (vector::iterator it = adj[x].begin(); it != adj[x].end(); it++) + { + mat[x][*it]--; + mat[*it][x]--; + if (!mat[x][*it]) + { + nfin++; + } + dep[*it]++; + } + dep[x] = tmp; + back(x + 1, size); + if (flag) + { + memcpy(mat, tmat, sizeof mat); + memcpy(dep, tdep, sizeof dep); + memcpy(mark, tmark, sizeof mark); + nfin = tnfin; + size = tsize; + } + return; +} + +int main() +{ + cin >> t; + for (int k = 0; k < t; k++) + { + bestAns = 1000000; + memset(mat, 0, sizeof mat); + memset(mark, 0, sizeof mark); + cin >> n >> m; + nfin = m; + for (int i = 0; i < n; i++) + { + adj[i].clear(); + dep[i] = 0; + } + int x, y; + for (int i = 0; i < m; i++) + { + cin >> x >> y; + adj[x].push_back(y); + adj[y].push_back(x); + dep[x]++; + dep[y]++; + } + for (int i = 0; i < n; i++) + { + change[i] = i; + } + for (int j = 0; j < n; j++) + for (int i = 0; i < n - 1; i++) + if (dep[i] < dep[i + 1]) + { + vector tmp; + tmp = adj[i]; + adj[i] = adj[i + 1]; + adj[i + 1] = tmp; + int td = dep[i]; + dep[i] = dep[i + 1]; + dep[i + 1] = td; + int tc = change[i]; + change[i] = change[i + 1]; + change[i + 1] = tc; + for (int a = 0; a < n; a++) + for (vector::iterator it = adj[a].begin(); it != adj[a].end(); it++) + if (*it == i) + { + *it = i + 1; + } + else if (*it == i + 1) + { + *it = i; + } + } + // for(int i=0;i + +using namespace std; + +typedef long long int LL; +LL sqr(LL a) +{ + return a * a; +} +struct point +{ + LL x, y; + point(LL _x = 0, LL _y = 0) : x(_x), y(_y) {} + LL operator*(const point &p) const + { + return x * p.y - y * p.x; + } + point operator-(const point &p) const + { + return point(x - p.x, y - p.y); + }; + point operator+(const point &p) const + { + return point(x + p.x, y + p.y); + }; + bool operator<(const point &p) const + { + return x < p.x || (x == p.x && y < p.y); + } + bool operator==(const point &p) const + { + return x == p.x && y == p.y; + } + bool operator!=(const point &p) const + { + return !(*this == p); + } + double dist(const point &p) const + { + return sqrt(sqr(x - p.x) + sqr(y - p.y)); + } +}; +LL cross(const point &O, const point &A, const point &B) +{ + return (A - O) * (B - O); +} +vector convex_hull(vector &P) +{ + int n = P.size(), k = 0; + vector r(2 * n); + sort(P.begin(), P.end()); + // Build lower hull + for (int i = 0; i < n; ++i) + { + while (k >= 2 && cross(r[k - 2], r[k - 1], P[i]) <= 0) + { + --k; + } + r[k++] = P[i]; + } + // Build upper hull + for (int i = n - 2, t = k + 1; i >= 0; --i) + { + while (k >= t && cross(r[k - 2], r[k - 1], P[i]) <= 0) + { + --k; + } + r[k++] = P[i]; + } + r.resize(k); + return r; +} +int main() +{ + int n, t; + scanf("%d", &t); + double len; + while (t-- && scanf("%lf%d", &len, &n) == 2) + { + vector ans, p; + long long x, y; + for (int i = 0; i < n; ++i) + { + scanf("%lld %lld", &x, &y); + p.push_back(point(x, y)); + } + ans = convex_hull(p); + double dist = 0; + for (int i = 1; i < int(ans.size()); ++i) + { + dist += ans[i].dist(ans[i - 1]); + } + printf("%.5lf\n", max(len, dist)); + } + return 0; +} diff --git a/11097.cpp b/11097.cpp new file mode 100644 index 0000000..a62508b --- /dev/null +++ b/11097.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define inf 1e50 + +struct Edge +{ + int x, y; + double c; +} edges[3600]; + +int v, e, s, q, x; + +main() +{ + cout.precision(4); + cout.setf(ios::showpoint | ios::fixed); + while (cin >> v >> e >> s) + { + for (int i = 0; i < e; i++) + { + cin >> edges[i].x >> edges[i].y >> edges[i].c; + } + double d[600]; + double mcost[600]; + int nedge[600]; + for (int i = 0; i < v; i++) + { + d[i] = mcost[i] = inf; + } + d[s] = mcost[s] = 0; + double tmpd[600]; + nedge[s] = 0; + for (int i = 1; i <= 1000; i++) + { + for (int j = 0; j < v; j++) + { + tmpd[j] = inf; + } + for (int j = 0; j < e; j++) + if (d[edges[j].x] != inf && d[edges[j].x] + edges[j].c < tmpd[edges[j].y]) + { + tmpd[edges[j].y] = d[edges[j].x] + edges[j].c; + if (tmpd[edges[j].y] / i < mcost[edges[j].y]) + { + mcost[edges[j].y] = tmpd[edges[j].y] / i; + nedge[edges[j].y] = i; + } + } + memcpy(d, tmpd, v * sizeof(double)); + } + cin >> q; + for (int i = 0; i < q; i++) + { + cin >> x; + if (mcost[x] < inf) + { + cout << mcost[x] << ' ' << nedge[x] << endl; + } + else + { + cout << "No Path" << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/11098.cpp b/11098.cpp new file mode 100644 index 0000000..c22416d --- /dev/null +++ b/11098.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +#define eps 1e-7 +#define N 1000 + +int x[N], y[N], n, t, order[N], visited[N], rep[N], in[N], cgraph[N][N], ra[N]; +vector graph[N]; +vector rgraph[N]; +double r[N], repr[N]; +int mat[N][N], cnt, p; + +void dfs(int s) +{ + visited[s] = true; + for (int i = 0; i < (int)graph[s].size(); i++) + if (!visited[graph[s][i]]) + { + dfs(graph[s][i]); + } + order[n - (++p)] = s; +} +void dfs2(int s) +{ + visited[s] = cnt; + if (repr[cnt] > r[s] + eps) + { + repr[cnt] = r[s]; + rep[cnt] = s; + } + for (int i = 0; i < (int)rgraph[s].size(); i++) + if (visited[rgraph[s][i]] == -1) + { + dfs2(rgraph[s][i]); + } +} +double sqr(int a) +{ + return a * a; +} +double dis(int i, int j) +{ + return sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j])); +} +int main() +{ + cin >> t; + for (int I = 1; I <= t; I++) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> x[i] >> y[i] >> ra[i] >> r[i]; + } + memset(mat, 0, sizeof mat); + for (int i = 0; i < n; i++) + { + graph[i].clear(), rgraph[i].clear(); + } + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (dis(i, j) < r[i] + ra[i] + ra[j] + eps) + { + //cout << "From " << i << " to " << j << endl; + mat[i][j] = true; + graph[i].push_back(j); + rgraph[j].push_back(i); + } + for (int i = 0; i < N; i++) + { + repr[i] = 99999999; + } + p = 0; + memset(visited, 0, sizeof visited); + for (int i = 0; i < n; i++) + if (!visited[i]) + { + dfs(i); + } + cnt = 0; + memset(visited, -1, sizeof visited); + for (int i = 0; i < n; i++) + if (visited[order[i]] == -1) + { + dfs2(order[i]), cnt++; + } + memset(in, 0, sizeof in); + memset(cgraph, 0, sizeof cgraph); + for (int i = 0; i < n; i++) + for (int j = 0; j < (int)graph[i].size(); j++) + if (visited[i] != visited[graph[i][j]]) + { + cgraph[visited[i]][visited[graph[i][j]]] = true; + } + for (int i = 0; i < cnt; i++) + for (int j = 0; j < cnt; j++) + if (cgraph[i][j]) + { + in[j]++; + } + set result; + int count = 0; + double sum = 0; + int max_i = 0; + for (int i = 0; i < cnt; i++) + if (in[i] == 0) + { + result.insert(i); + //cout << "Inserting " << i << endl; + max_i = max(max_i, rep[i]); + sum += repr[i]; + count++; + } + vector> remaining; + for (int i = 0; i < cnt; i++) + if (result.find(i) == result.end()) + { + remaining.push_back(pair(repr[i], i)); + } + sort(remaining.begin(), remaining.end()); + for (int i = 0; i < (int)remaining.size(); i++) + if (remaining[i].first * count < sum - eps) + { + count++; + sum += remaining[i].first; + result.insert(remaining[i].second); + max_i = max(max_i, remaining[i].second); + } + int topsort[N]; + bool mark[N]; + for (int i = 0; i < cnt; i++) + { + mark[i] = false; + } + for (int i = 0; i < cnt; i++) + { + int v = -1; + for (int j = 0; j < cnt; j++) + if (!mark[j] && in[j] == 0) + { + v = j; + break; + } + if (v == -1) + { + cout << "Gand khord" << endl; + } + mark[v] = true; + for (int j = 0; j < cnt; j++) + if (cgraph[v][j]) + { + in[j]--; + } + topsort[i] = v; + } + vector vresult; + for (int i = cnt - 1; i >= 0; i--) + if (result.find(topsort[i]) != result.end()) + { + vresult.push_back(rep[topsort[i]]); + } + cout << "Case #" << I << ":"; + for (int i = 0; i < (int)vresult.size(); i++) + { + cout << " " << vresult[i]; + } + cout << endl; + } + return 0; +} diff --git a/11099.cpp b/11099.cpp new file mode 100644 index 0000000..a39a653 --- /dev/null +++ b/11099.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +bool nprime[2000001]; +int base[2000001], nsf[2000001], n, prev_[2000001]; + +int main() +{ + memset(nprime, 0, sizeof nprime); + memset(base, -1, sizeof base); + memset(nsf, 0, sizeof nsf); + for (int i = 2; i < 1000000; i++) + { + if (!nprime[i]) + { + for (int j = i; j < 2000000; j += i) + { + base[j] *= i; + nprime[j] = 1; + } + } + } + memset(prev_, 0, sizeof prev_); + for (int i = 2; i < 2000000; i++) + { + if (base[i] != -1) + { + nsf[prev_[-base[i]]] = i; + prev_[-base[i]] = i; + } + } + while (cin >> n) + { + if (nsf[n]) + { + cout << nsf[n] << endl; + } + else + { + cout << "Not Exist!" << endl; + } + } + return 0; +} diff --git a/111.cpp b/111.cpp index eb600b7..075f704 100644 --- a/111.cpp +++ b/111.cpp @@ -1,40 +1,48 @@ -#include -using namespace std; - -int main(){ - int n=0,i=0,j=0, no=0,p; - cin>>n; - int real[n+1],dup[n+1]; - for(i=1;i<=n;i++){ - cin>>p; - real[p]=i; - } - while(cin>>no){ - dup[no]=1; - for(i=2;i<=n;i++){ - cin>>p; - dup[p]=i; - } - int tower[n+1][n+1]; - for(i=0; itower[i][j-1]){ - tower[i][j]=tower[i-1][j]; - } - else{ - tower[i][j] = tower[i][j-1]; - } - } - } - } - cout< + +using namespace std; + +int main() +{ + int n, tmp; + map ord; + cin >> n; + int s[n + 1], dp[n + 1]; + for (int i = 0; i < n; i++) + { + cin >> tmp; + ord[i + 1] = tmp; + } + while (cin >> tmp) + { + s[tmp] = 1; + for (int i = 2; i <= n; i++) + { + cin >> tmp; + s[tmp] = i; + } + for (int i = 1; i <= n; i++) + { + s[i] = ord[s[i]]; + } + int max_ = 0; + dp[1] = 1; + for (int i = 2; i <= n; i++) + { + dp[i] = 1; + for (int j = 1; j < i; j++) + { + if (s[i] > s[j]) + { + dp[i] = max(dp[i], dp[j] + 1); + } + } + if (dp[i] > max_) + { + max_ = dp[i]; + } + } + cout << max_ << endl; + } + return 0; +} diff --git a/1110.cpp b/1110.cpp new file mode 100644 index 0000000..28fdd43 --- /dev/null +++ b/1110.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +struct rec +{ + int num; + int base, type; +} a[400]; +int cmp(rec a, rec b) +{ + return a.num < b.num; +} +char f[321][1000001]; +int c, cnt, max; +char tmp[10]; +std::vector ans, now; +int anscnt; +void solve() +{ + if (f[cnt][c] > 100) + { + puts("impossible"); + return; + } + int pre = cnt; + ans.clear(); + for (int i = cnt - 1; i >= 0; i--) + if (a[i].num <= c && f[i][c - a[i].num] + 1 == f[pre][c]) + { + ans.push_back(a[i]), c -= a[i].num, pre = i; + } + for (int i = 0; i < ans.size(); i++) + { + printf("%d%c%c", ans[i].base, "HL"[ans[i].type], " \n"[i == ans.size() - 1]); + } +} +int main() +{ + for (int i = 2;; i++) + { + int t = i * (i + 1) * (2 * i + 1) / 6; + if (t > 1000000) + { + break; + } + a[cnt].num = t, a[cnt].base = i, a[cnt].type = 0, cnt++; + } + for (int i = 2;; i++) + { + int t = 2 * i * (i + 1) * (2 * i + 1) / 3; + if (t > 1000000) + { + break; + } + a[cnt].num = t, a[cnt].base = 2 * i, a[cnt].type = 1, cnt++; + } + for (int i = 2;; i++) + { + int t = i * (2 * i - 1) * (2 * i + 1) / 3; + if (t > 1000000) + { + break; + } + a[cnt].num = t, a[cnt].base = 2 * i - 1, a[cnt].type = 1, cnt++; + } + std::sort(a, a + cnt, cmp); + memset(f, 0x70, sizeof f); + f[0][0] = 0; + for (int j = 0; j < cnt; j++) + { + for (int i = a[j].num; i <= 1000000; i++) + { + f[j + 1][i] = f[j][i - a[j].num] + 1; + } + for (int i = 0; i <= 1000000; i++) + if (f[j][i] < f[j + 1][i]) + { + f[j + 1][i] = f[j][i]; + } + } + int _ = 0; + while (scanf("%d", &c), c) + { + printf("Case %d: ", ++_), solve(); + } + return 0; +} diff --git a/11100.cpp b/11100.cpp new file mode 100644 index 0000000..096448d --- /dev/null +++ b/11100.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int X[10005]; +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int main() +{ + int n, i, j, max, len, f; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%d", X + i); + } + qsort(X, n, sizeof(int), cmp); + for (i = len = max = 1; i < n; i++) + { + if (X[i] == X[i - 1]) + { + len++; + } + else + { + if (len > max) + { + max = len; + } + len = 1; + } + } + if (len > max) + { + max = len; + } + printf("%d\n", max); + for (i = 0; i < max; i++) + { + for (j = 0, f = 1; j * max + i < n; j++) + { + if (f) + { + f = 0; + } + else + { + printf(" "); + } + printf("%d", X[j * max + i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/11101.cpp b/11101.cpp new file mode 100644 index 0000000..3e7d0f4 --- /dev/null +++ b/11101.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 2001 +#define mp make_pair +#define x first +#define y second +#define MY 1 +#define OP 2 +typedef pair PII; +bool visited[N][N]; +int mark[N][N]; +int cost[N][N]; +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +pair Q[N * N]; + +int solve(int tail) +{ + int head = 0; + while (head < tail) + { + rep(i, 4) + { + int nex = Q[head].x + dx[i], ney = Q[head].y + dy[i]; + if (nex < 0 || ney < 0 || nex >= N || ney >= N || visited[ney][nex] == true) + { + continue; + } + if (mark[ney][nex] == OP) + { + return cost[Q[head].y][Q[head].x] + 1; + } + visited[ney][nex] = true; + cost[ney][nex] = cost[Q[head].y][Q[head].x] + 1; + Q[tail].x = nex; + Q[tail++].y = ney; + } + head++; + } + return -1; +} + +main() +{ + int n; + while (scanf("%d", &n) != -1 && n) + { + rep(i, N) rep(j, N) visited[i][j] = false, mark[i][j] = 0; + int tx, ty; + int tail = 0; + rep(i, n) + { + scanf("%d%d", &tx, &ty); + mark[ty][tx] = MY; + Q[tail].x = tx; + Q[tail++].y = ty; + cost[ty][tx] = 0; + visited[ty][tx] = true; + } + int p; + cin >> p; + rep(i, p) + { + scanf("%d%d", &tx, &ty); + mark[ty][tx] = OP; + } + cout << solve(tail) << endl; + } + return false; +} diff --git a/11102.cpp b/11102.cpp new file mode 100644 index 0000000..79770af --- /dev/null +++ b/11102.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +double EPS = 1e-6; +double PI = 2.0 * acos(0.0); + +double Cut(double r, double x) +{ + if (x < -r + EPS) + { + return -PI * r * r / 2.0; + } + if (x > r - EPS) + { + return PI * r * r / 2.0; + } + return r * r * asin(x / r) + x * sqrt(r * r - x * x); +} + +double CutS(double r, double s) +{ + return Cut(r, -r + s) - Cut(r, -r); +} + +double baseHeight, baseDiam, baseRad; +double neckHeight, neckDiam, neckRad; +double shoulderHeight, totalHeight; + +double f_slope, f_s; + +double f(double z) +{ + double r = max(0.0, baseRad - f_slope * z); + return Cut(r, baseRad) - Cut(r, baseRad - f_s); +} + +double Integrate(double a, double b) +{ + int n = 5000; + double h = (b - a) / n; + double x = a + h / 2, res = 0; + for (int i = 0; i < n; i++, x += h) + { + res += f(x); + } + return res * h; +} + +double SideVolume(double s) +{ + double res = 0; + if (s < EPS) + { + return 0; + } + res += baseHeight * CutS(baseRad, s); + res += neckHeight * CutS(neckRad, s - (baseRad - neckRad)); + if (shoulderHeight > EPS) + { + f_slope = (baseRad - neckRad) / shoulderHeight; + f_s = s; + res += Integrate(0, shoulderHeight); + } + return res; +} + +double VerticalVolume(double liquid) +{ + double res = 0; + if (liquid > EPS) + { + double h = min(liquid, baseHeight); + res += h * PI * baseRad * baseRad; + liquid -= h; + } + if (liquid > EPS && shoulderHeight > EPS) + { + double h = min(liquid, shoulderHeight); + if (fabs(baseRad - neckRad) < EPS) + { + res += h * PI * baseRad * baseRad; + } + else + { + double h2 = baseRad / (baseRad - neckRad) * shoulderHeight; + double r2 = baseRad; + double h1 = h2 - h; + double r1 = baseRad + (neckRad - baseRad) * h / shoulderHeight; + res += (PI * r2 * r2 * h2 - PI * r1 * r1 * h1) / 3.0; + } + liquid -= h; + } + if (liquid > EPS && neckHeight > EPS) + { + double h = min(liquid, neckHeight); + res += h * PI * neckRad * neckRad; + liquid -= h; + } + return res; +} + +int main() +{ + string s; + double h; + while (getline(cin, s)) + { + if (s.size() < 11 || s.substr(0, 11) == "0 0 0 0 0 0") + { + break; + } + istringstream is(s); + is >> h >> baseHeight >> baseDiam; + is >> neckHeight >> neckDiam >> totalHeight; + baseRad = baseDiam / 2.0; + neckRad = neckDiam / 2.0; + shoulderHeight = totalHeight - baseHeight - neckHeight; + double vol = VerticalVolume(h); + double left = 0, right = baseDiam; + while (right - left > 1e-9) + { + double mid = (left + right) / 2; + if (SideVolume(mid) > vol) + { + right = mid; + } + else + { + left = mid; + } + } + printf("%.2f\n", left); + } +} diff --git a/11103.cpp b/11103.cpp new file mode 100644 index 0000000..7018582 --- /dev/null +++ b/11103.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int L[128] = {0}, S[120], B[120]; +char in[120]; +int main() +{ + int len, i, j, s, b, n; + L['K'] = L['A'] = L['C'] = L['E'] = 1; + L['N'] = 2; + L['p'] = L['q'] = L['r'] = L['s'] = L['t'] = 3; + while (gets(in)) + { + if (in[0] == '0') + { + break; + } + s = b = n = 0; + for (i = 0; in[i]; i++) + { + switch (L[in[i]]) + { + case 1: + B[b++] = in[i]; + break; + case 2: + n++; + break; + case 3: + S[s++] = in[i]; + } + } + if (s == 0) + { + printf("no WFF possible\n"); + continue; + } + while (n--) + { + printf("N"); + } + s--; + len = ((s < b) ? s : b); + for (i = 0; i < len; i++) + { + printf("%c%c", B[i], S[i]); + } + printf("%c\n", S[i]); + } + return 0; +} diff --git a/11104.cpp b/11104.cpp new file mode 100644 index 0000000..d4297ac --- /dev/null +++ b/11104.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) + +char x[200], y[200]; +int b[200][200]; +int nx, ny, i, j, k, m, n; + +int main() +{ + while (2 == scanf("%s%s", x, y) && x[0] != '0') + { + memset(b, 0, sizeof(b)); + nx = strlen(x); + ny = strlen(y); + for (i = 0; i < nx; i++) + { + for (j = 0; j < ny; j++) + { + if (x[i] == y[j]) + { + b[i + 1][j + 1] = b[i][j] + 1; + } + else + { + b[i + 1][j + 1] = max(b[i][j + 1], b[i + 1][j]); + } + } + } + k = min(nx, ny); + m = b[nx][ny]; + n = max(nx, ny) - m; + //printf("%d %d %d\n",k,m,n); + for (i = 0; n; i++) + { + n -= min(n, (n + m) / 2); + m += min(n + m, k - m); + //printf("%d %d %d\n",k,m,n); + } + printf("%d\n", i > 1 ? i : 1); + } +} diff --git a/11105.cpp b/11105.cpp new file mode 100644 index 0000000..1b111e5 --- /dev/null +++ b/11105.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int P[90000] = {5}, L[1000005] = {0}; +int main() +{ + int ptr, i, j, sq, ok; + long long int tmp; + for (ptr = 1; P[ptr - 1] < 1000000; ptr++) + { + for (i = P[ptr - 1] + 4;; i += 4) + { + sq = sqrt(i); + for (j = 0, ok = 1; P[j] <= sq; j++) + { + if (i % P[j]) + { + continue; + } + ok = 0; + break; + } + if (ok) + { + break; + } + } + P[ptr] = i; + } + for (i = 0; i < ptr; i++) + { + for (j = i; j < ptr; j++) + { + tmp = (long long int)P[i] * P[j]; + if (tmp > 1000001) + { + break; + } + L[tmp] = 1; + } + } + for (i = 5; i <= 1000001; i += 4) + { + L[i] += L[i - 4]; + } + while (scanf("%d", &i) == 1) + { + if (i == 0) + { + break; + } + printf("%d %d\n", i, L[i]); + } + return 0; +} diff --git a/11106.cpp b/11106.cpp new file mode 100644 index 0000000..b358f8e --- /dev/null +++ b/11106.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +const int N = 100005; + +typedef pair pi; + +int t, n, en, vi[N], num; +map vis; + +struct Point +{ + int x, y, id, v, h; +} p[N]; + +struct Edge +{ + int x, y1, y2; +} e[N]; + +bool cmp1(Point a, Point b) +{ + if (a.x != b.x) + { + return a.x < b.x; + } + return a.y < b.y; +} + +bool cmp2(Point a, Point b) +{ + if (a.y != b.y) + { + return a.y < b.y; + } + return a.x < b.x; +} + +bool cmp3(Point a, Point b) +{ + return a.id < b.id; +} + +void init() +{ + en = 0; + vis.clear(); + memset(vi, 0, sizeof(vi)); + num = 0; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d%d", &p[i].x, &p[i].y); + p[i].id = i; + } +} + +void dfs(int id, int bo) +{ + if (vi[id]) + { + return; + } + vi[id] = 1; + num++; + if (bo) + { + dfs(p[id].v, 0); + } + else + { + dfs(p[id].h, 1); + } +} + +int solve() +{ + if (n % 2) + { + return -1; + } + int ans = 0; + sort(p, p + n, cmp1); + int i, save = -1; + for (i = 0; i < n; i += 2) + { + if (p[i].x != p[i + 1].x) + { + return -1; + } + if (save != p[i].x) + { + vis[p[i].x].first = en; + if (i != 0) + { + vis[p[i - 1].x].second = en; + } + save = p[i].x; + } + e[en].x = p[i].x; + e[en].y1 = p[i + 1].y; + e[en++].y2 = p[i].y; + p[i].h = p[i + 1].id; + p[i + 1].h = p[i].id; + ans += (p[i + 1].y - p[i].y); + } + vis[p[n - 1].x].second = en; + sort(p, p + n, cmp2); + for (i = 0; i < n; i += 2) + { + if (p[i].y != p[i + 1].y) + { + return -1; + } + for (int j = vis[p[i].x].second; j < vis[p[i + 1].x].first; j++) + { + if (e[j].y1 >= p[i].y && e[j].y2 <= p[i].y) + { + return -1; + } + } + p[i].v = p[i + 1].id; + p[i + 1].v = p[i].id; + ans += (p[i + 1].x - p[i].x); + } + sort(p, p + n, cmp3); + dfs(0, 0); + if (num != n) + { + return -1; + } + return ans; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/11107.cpp b/11107.cpp new file mode 100644 index 0000000..7c0c850 --- /dev/null +++ b/11107.cpp @@ -0,0 +1,195 @@ +#include + +using namespace std; + +#define N 104857 + +char w[N]; +int H = 0, bucket[N], lcp[N], new_bucket[N], rank_[N], sn[N]; + +struct suffix +{ + int z; + bool operator<(const suffix &s) const + { + if (!H) + { + return w[z] < w[s.z]; + } + if (bucket[z] == bucket[s.z]) + { + return bucket[z + H] < bucket[s.z + H]; + } + return bucket[z] < bucket[s.z]; + } + bool operator==(const suffix &s) const + { + return !(*this < s) && !(s < *this); + } +} pos[N]; + +bool update_buckets(int L) +{ + int i, id; + for (i = id = 0; i < L; ++i) + { + new_bucket[pos[i].z] = (id += i && !(pos[i] == pos[i - 1])); + } + memcpy(bucket, new_bucket, L << 2); + return id != L - 1; +} + +void suffix_array(int L) +{ + int c = 1; + memset(bucket, -1, sizeof bucket); + for (int i = H = 0; i < L; ++i) + { + pos[i].z = i; + } + for (H = 0; c; H = (H << 1) | !H) + { + sort(pos, pos + L); + c = update_buckets(L); + } +} + +void compute_lcp(int L) +{ + for (int i = 0; i < L; ++i) + { + rank_[pos[i].z] = i; + } + for (int h = 0, i = 0, j; i < L; ++i) + if (rank_[i]) + { + j = pos[rank_[i] - 1].z; + while (w[i + h] != '$' && w[i + h] == w[j + h]) + { + ++h; + } + lcp[rank_[i]] = h; + h -= h > 0; + } +} +// + +// Interval tree +#define LEFT(x) (2 * (x - d) + d + 1) +#define RIGHT(x) (2 * (x - d) + d + 2) +struct interval +{ + int left, right, value; +} d[4 * N]; +int init_interval(interval *i, int left, int right) +{ + i->left = left; + i->right = right; + if (left < right) + { + int mid = (left + right) >> 1; + i->value = min( + init_interval(LEFT(i), left, mid), + init_interval(RIGHT(i), mid + 1, right)); + } + else + { + i->value = lcp[left]; + } + return i->value; +} +int query(interval *i, int left, int right) +{ + if (left > i->right || right < i->left) + { + return 1 << 30; + } + if (left <= i->left && i->right <= right) + { + return i->value; + } + return min(query(LEFT(i), left, right), query(RIGHT(i), left, right)); +} +// + +int cnt[128], xx[N]; +int main() +{ + bool first = true; + for (int n; scanf("%d", &n) == 1 && n; first = false) + { + int half = n / 2, L = 0; + if (!first) + { + puts(""); + } + for (int i = 0; i < n; ++i) + { + scanf("%s", w + L); + while (w[L]) + { + sn[L++] = i; + } + sn[L] = -1; + cnt[i] = 0; + w[L++] = '$'; + } + if (n == 1) + { + w[L - 1] = 0; + puts(w); + continue; + } + w[L] = 0; + suffix_array(L); + compute_lcp(L); + init_interval(d, 1, L); + int maxlcp = 0; + for (int i = 0; i < L; ++i) + { + xx[i] = -1; + } + for (int i = 0, in = 0, j = 0; i < L; ++i) + { + while (in <= half && j < L) + { + j++; + if (sn[pos[j].z] != -1 && !cnt[sn[pos[j].z]]++) + { + ++in; + } + } + if (j >= L) + { + break; + } + xx[i] = query(d, i + 1, j); + maxlcp = max(maxlcp, xx[i]); + if (sn[pos[i].z] != -1 && !--cnt[sn[pos[i].z]]) + { + --in; + } + } + if (maxlcp == 0) + { + printf("?\n"); + } + else + { + set seen; + for (int i = 0; i < L; i++) + if (xx[i] == maxlcp) + { + char temp = w[pos[i].z + maxlcp]; + w[pos[i].z + maxlcp] = 0; + if (seen.find(w + pos[i].z) == seen.end()) + { + seen.insert(w + pos[i].z); + puts(w + pos[i].z); + } + w[pos[i].z + maxlcp] = temp; + } + } + } + return 0; +} diff --git a/11108.cpp b/11108.cpp new file mode 100644 index 0000000..cbdbc46 --- /dev/null +++ b/11108.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int main() +{ + int use, ps, N[200], pn, i, j, B[128], T[128][2][2]; + char C[200], P[200]; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 2; j++) + { + T['K'][i][j] = i & j; + T['A'][i][j] = i | j; + T['C'][i][j] = i | (1 - j); + T['E'][i][j] = 1 - (i ^ j); + } + } + while (scanf("%s", C) == 1) + { + if (C[0] == '0') + { + break; + } + for (i = 0; C[i]; i++) + ; + P[i] = '\0'; + for (i--, j = 0; i >= 0; i--, j++) + { + P[j] = C[i]; + } + for (use = 0; use < 32; use++) + { + for (j = 0; j < 5; j++) + { + B['p' + j] = (use & (1 << j)) / (1 << j); + } + for (i = ps = pn = 0; P[i]; i++) + { + if (P[i] > 'Z') + { + N[pn++] = B[P[i]]; + } + else if (P[i] == 'N') + { + N[pn - 1] = 1 - N[pn - 1]; + } + else + { + N[pn - 2] = T[P[i]][N[--pn]][N[pn - 1]]; + } + } + if (!N[0]) + { + break; + } + } + if (N[0]) + { + printf("tautology\n"); + } + else + { + printf("not\n"); + } + } + return 0; +} diff --git a/11109.cpp b/11109.cpp new file mode 100644 index 0000000..a68b03f --- /dev/null +++ b/11109.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int i, j, k; +double vb, vw, vr, vc, avg, amt; +int main() +{ + while (5 == scanf("%d%lf%lf%lf%lf", &k, &vb, &vw, &vr, &vc)) + { + amt = vb + vw - vr; + if (amt < 0) + { + printf("0\n"); + continue; + } + avg = amt / k; + if (avg >= vc - vr) + { + avg = vc - vr; + } + if (avg >= vw - vr) + { + printf("%d %0.2lf", k, avg - vw + vr); + for (i = 1; i < k; i++) + { + printf(" %0.2lf", avg); + } + printf("\n"); + } + else + { + avg = vb / (k - 1); + printf("%d 0.00", k); + for (i = 1; i < k; i++) + { + printf(" %0.2lf", avg); + } + printf("\n"); + } + } +} diff --git a/1111.cpp b/1111.cpp new file mode 100644 index 0000000..4e18d00 --- /dev/null +++ b/1111.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define SZ(v) ((int)(v).size()) +#define rep(i, n) for (int i = 0; i < (n); ++i) +#define repf(i, a, b) for (int i = (a); i <= (b); ++i) +#define SQR(x) ((x) * (x)) + +const int maxint = -1u >> 1; +const double eps = 1e-9; +const double inf = 1e10; + +int sgn(double x) +{ + return (x > eps) - (x < -eps); +} + +struct P +{ + double x, y; + P() {} + P(double _x, double _y) : x(_x), y(_y) {} + P operator-(const P &a) const + { + return P(x - a.x, y - a.y); + } + double cross(const P &a, const P &b) const + { + return (a.x - x) * (b.y - y) - (a.y - y) * (b.x - x); + } + double length() const + { + return sqrt(SQR(x) + SQR(y)); + } + void input() + { + scanf("%lf%lf", &x, &y); + } + void output() const + { + printf("%lf %lf\n", x, y); + } +}; + +int n; +vector

c1, c2; + +bool cmp(const P &p1, const P &p2) +{ + return sgn(p1.y - p2.y) < 0 || (sgn(p1.y - p2.y) == 0 && p1.x < p2.x); +} + +vector

get_convex(vector

&con) +{ + sort(con.begin(), con.end(), cmp); + //for (int i = 0; i < con.size(); ++i) con[i].output(); + vector

res; + for (int i = 0; i < (int)con.size(); ++i) + { + while (res.size() > 1 && sgn(res[res.size() - 2].cross(res.back(), con[i])) <= 0) + { + res.pop_back(); + } + res.push_back(con[i]); + } + int c = res.size(); + for (int i = con.size() - 1; i >= 0; --i) + { + while ((int)res.size() > c && sgn(res[res.size() - 2].cross(res.back(), con[i])) <= 0) + { + res.pop_back(); + } + res.push_back(con[i]); + } + res.pop_back(); + return res; +} +double get_dist(const P &a, const P &b, const P &c) +{ + double area = a.cross(b, c); + return fabs(area) / (b - c).length(); +} + +int main() +{ + int ca = 0; + while (scanf("%d", &n) == 1 && n) + { + c1.resize(n); + for (int i = 0; i < n; ++i) + { + c1[i].input(); + } + c2 = get_convex(c1); + double mn = inf; + for (int i = 0; i < (int)c2.size(); ++i) + { + P p1 = c2[i], p2 = c2[(i + 1) % c2.size()]; + double mx = 0; + for (int j = 0; j < (int)c2.size(); ++j) + { + mx = max(mx, get_dist(c2[j], p1, p2)); + } + mn = min(mn, mx); + } + printf("Case %d: %.2lf\n", ++ca, ceil(mn * 100) * 0.01); + } + return 0; +} diff --git a/11110.cpp b/11110.cpp index f225752..c9b3c7a 100644 --- a/11110.cpp +++ b/11110.cpp @@ -1,64 +1,142 @@ -#include -#include -#include -#include +#include + using namespace std; -int Mat[101][101]; -int N = 0; +int n; +int sq[100][100]; +bool visited[100][100]; -int dI[] = {-1, 0, 1, 0}; -int dJ[] = {0, 1, 0, -1}; -void dfs(int I, int J, int C) { - if (I < 0 || I >= N) return; - if (J < 0 || J >= N) return; - if (Mat[I][J] == -1) return; - if (Mat[I][J] != C) return; - - Mat[I][J] = -1; - for (int i = 0; i < 4; i++) - dfs(I + dI[i], J + dJ[i], C); +bool check(int c) +{ + for (int i = 0; i < n; i++) + { + for (int y = 0; y < n; y++) + { + if (sq[i][y] == c) + { + return false; + } + } + } + return true; } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - while (true) { - scanf("%d\n", &N); - if (!N) break; - - memset(Mat, 0, sizeof (Mat)); - - for (int i = 1; i < N; i++) { - string str; - getline(cin, str); - stringstream ss(str); - int A, B; - while (ss >> A >> B) - Mat[A - 1][B - 1] = i; +void fill(int a, int b, int c) +{ + pair p; + queue> q; + p = make_pair(a, b); + q.push(p); + while (!q.empty()) + { + p = q.front(); + q.pop(); + sq[p.first][p.second] = 0; + if (p.first - 1 >= 0) + { + if (sq[p.first - 1][p.second] == c && visited[p.first - 1][p.second] == false) + { + q.push(make_pair(p.first - 1, p.second)); + visited[p.first - 1][p.second] = true; + } + } + if (p.second - 1 >= 0) + { + if (sq[p.first][p.second - 1] == c && visited[p.first][p.second - 1] == false) + { + q.push(make_pair(p.first, p.second - 1)); + visited[p.first][p.second - 1] = true; + } } - - int arr[N]; - memset(arr, 0, sizeof arr); - - bool flag = true; - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - if (Mat[i][j] < 0) continue; - - if (arr[Mat[i][j]] > 0) { - flag = false; - break; + if (p.first + 1 < n) + { + if (sq[p.first + 1][p.second] == c && visited[p.first + 1][p.second] == false) + { + q.push(make_pair(p.first + 1, p.second)); + visited[p.first + 1][p.second] = true; + } + } + if (p.second + 1 < n) + { + if (sq[p.first][p.second + 1] == c && visited[p.first][p.second + 1] == false) + { + q.push(make_pair(p.first, p.second + 1)); + visited[p.first][p.second + 1] = true; + } + } + } +} + +int main() +{ + int x, y; + string input; + while (cin >> n) + { + if (n == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + for (int y = 0; y < n; y++) + { + sq[i][y] = 0; + visited[i][y] = false; + } + } + int cur = 1; + getline(cin, input);// \n + for (int i = 0; i < n - 1; i++) + { + getline(cin, input); + stringstream ss; + ss << input; + while (ss >> x >> y) + { + sq[x - 1][y - 1] = cur; + } + cur++; + } + for (int i = 0; i < n; i++) + { + for (int y = 0; y < n; y++) + { + if (sq[i][y] == 0) + { + sq[i][y] = cur; } - - ++arr[Mat[i][j]]; - dfs(i, j, Mat[i][j]); } - if (!flag) break; } - - if (flag) printf("good\n"); - else printf("wrong\n"); + bool ok = true; + for (int i = 1; i <= cur; i++) + { + for (int a = 0; a < n; a++) + { + for (int b = 0; b < n; b++) + { + if (sq[a][b] == i) + { + fill(a, b, i); + b = n; + a = n; + } + } + } + if (!check(i)) + { + ok = false; + break; + } + } + if (ok) + { + cout << "good" << endl; + } + else + { + cout << "wrong" << endl; + } } + return 0; } diff --git a/11111.cpp b/11111.cpp new file mode 100644 index 0000000..f9046f3 --- /dev/null +++ b/11111.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int main() +{ + string input; + while (getline(cin, input)) + { + bool valid = true; + stack s; + vector sizes; + istringstream iss(input); + copy(istream_iterator(iss), istream_iterator(), back_inserter(sizes)); + rep(i, sizes.size()) + { + if (sizes[i] < 0) + { + s.push(sizes[i]); + } + else + { + int sum = 0; + bool found = false; + while (!s.empty()) + { + if (s.top() + sizes[i] == 0) + { + s.pop(); + s.push(sizes[i]); + found = true; + break; + } + if (s.top() < 0) + { + valid = false; + break; + } + sum += s.top(); + s.pop(); + if (sum >= sizes[i]) + { + valid = false; + break; + } + } + if (!found) + { + valid = false; + break; + } + } + if (!valid) + { + break; + } + } + while (!s.empty()) + { + if (s.top() < 0) + { + valid = false; + break; + } + s.pop(); + } + cout << (valid ? ":-) Matrioshka!" : ":-( Try again.") << endl; + } + return 0; +} diff --git a/11112.cpp b/11112.cpp new file mode 100644 index 0000000..07d17c0 --- /dev/null +++ b/11112.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int ip, bet, fp; + while (scanf("%d%d%d", &ip, &bet, &fp) == 3) + { + if ((ip | bet | fp) == 0) + { + break; + } + if (ip == fp) + { + printf("0\n"); + continue; + } + ip = abs(ip - fp); + if (ip % bet) + { + printf("No accounting tablet\n"); + continue; + } + printf("%d\n", (ip / bet - 1) / 3 + 1); + } + return 0; +} diff --git a/11113.cpp b/11113.cpp new file mode 100644 index 0000000..abe8e3f --- /dev/null +++ b/11113.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +bool ls(const string &a, const string &b) +{ + return a.size() < b.size() || (a.size() == b.size() && a < b); +} + +bool leq(const string &a, const string &b) +{ + return a.size() < b.size() || (a.size() == b.size() && a <= b); +} + +string sub(string a, string b) +{ + if (a == b) + { + return "0"; + } + assert(ls(b, a)); + reverse(a.begin(), a.end()); + reverse(b.begin(), b.end()); + string res; + for (int i = 0, c = 0; i < (int)a.size(); i++) + { + c += a[i] - '0' - (i < (int)b.size() ? b[i] - '0' : 0); + if (c < 0) + { + res += c + 10 + '0'; + c = -1; + } + else + { + res += c + '0'; + c = 0; + } + } + while (res.size() > 1 && res[res.size() - 1] == '0') + { + res.resize(res.size() - 1); + } + reverse(res.begin(), res.end()); + return res; +} + +void div(const string &num, string den, string &q, string &r) +{ + int k = 0; + q = ""; + r = num; + while (leq(den, r)) + { + k++; + den += "0"; + } + if (k == 0) + { + q = "0"; + r = num; + return; + } + while (k-- > 0) + { + den.resize(den.size() - 1); + int z = 0; + while (leq(den, r)) + { + r = sub(r, den); + z++; + } + q += (char)(z + '0'); + } +} + +int main() +{ + string p, q; + for (int cs = 1; cin >> p >> q; cs++) + { + if (p[0] == '0' || q[0] == '0') + { + break; + } + cout << "Case " << cs << ":" << endl; + cout << p << " / " << q << endl; + vector a; + for (;;) + { + string t, rem; + div(p, q, t, rem); + p = rem; + if (p == "0") + { + a.push_back(sub(t, "1")); + a.push_back("1"); + break; + } + else + { + a.push_back(t); + swap(p, q); + } + } + int n = a.size(); + int W = 3 * (n - 1); + for (int i = 0; i < n; i++) + { + W += a[i].size(); + } + int sh = 0; + for (int i = 0; i < n - 1; i++) + { + int sh1 = sh + a[i].size() + 3; + int m = max(0, (W - sh1 - 1) / 2); + cout << string(sh1 + m, '.') << '1' << string(max(0, W - sh1 - m - 1), '.') << endl; + cout << string(sh, '.') << a[i] << ".+." << string(max(0, W - sh1), '-') << endl; + sh = sh1; + } + cout << string(W - 1, '.') << '1' << endl; + } +} diff --git a/11114.cpp b/11114.cpp new file mode 100644 index 0000000..b70704e --- /dev/null +++ b/11114.cpp @@ -0,0 +1,428 @@ +#include + +using namespace std; + +#define MAX 10000 +#define BASE 10000 +char str[1500]; +void updateodd(int *oddarray, int &arraylen, int *outputarray, int originlen) +{ + int carry = 0; + arraylen = originlen; + for (int i = 0; i < originlen; i++) + { + int t = outputarray[i] * 20 + carry; + oddarray[i] = t % BASE; + carry = t / BASE; + } + if (carry) + { + oddarray[arraylen++] = carry; + } + carry = 1; + for (int i = 0; i < arraylen; i++) + { + int t = oddarray[i] + carry; + oddarray[i] = t % BASE; + carry = t / BASE; + if (!carry) + { + break; + } + } + if (carry) + { + oddarray[arraylen++] = carry; + } +} +void update(int *odd, int &lenodd) +{ + int carry, i; + for (i = 0, carry = 2; i < lenodd; i++) + { + int t = odd[i] + carry; + odd[i] = t % BASE; + carry = t / BASE; + if (!carry) + { + break; + } + } + if (carry) + { + odd[lenodd++] = carry; + } +} +bool compare(int *remain, int lenrem, int *odd, int lenodd) +{ + if (lenodd > lenrem) + { + return false; + } + if (lenrem > lenodd) + { + return true; + } + for (int i = lenrem - 1; i >= 0; i--) + { + if (remain[i] > odd[i]) + { + return true; + } + if (remain[i] < odd[i]) + { + return false; + } + } + return true; +} +void sub(int *remain, int &lenrem, int *odd, int lenodd) +{ + int borrow, t, i; + for (i = borrow = 0; i < lenrem; i++) + { + t = remain[i] + borrow; + if (i < lenodd) + { + t -= odd[i]; + } + if (t < 0) + { + t += BASE; + borrow = -1; + } + else + { + borrow = 0; + } + remain[i] = t; + } + while (remain[lenrem - 1] == 0) + { + lenrem--; + } +} +void updateoutput(int *output, int &len, int c) +{ + int carry, i; + for (i = carry = 0; i < len; i++) + { + int t = output[i] * 10 + carry; + output[i] = t % BASE; + carry = t / BASE; + } + if (carry) + { + output[len++] = carry; + } + for (i = 0, carry = c; i < len; i++) + { + output[i] += carry; + carry = output[i] / BASE; + output[i] %= BASE; + if (!carry) + { + break; + } + } + if (carry) + { + output[len++] = carry; + } +} +void updaterem(int *remainarray, int &arraylen, int c) +{ + int carry, i; + if (arraylen <= 0) + { + arraylen = 1; + } + for (i = carry = 0; i < arraylen; i++) + { + int t = remainarray[i] * 100 + carry; + remainarray[i] = t % BASE; + carry = t / BASE; + } + if (carry) + { + remainarray[arraylen++] = carry; + } + for (i = 0, carry = c; i < arraylen; i++) + { + remainarray[i] += carry; + carry = remainarray[i] / BASE; + remainarray[i] %= BASE; + if (!carry) + { + break; + } + } + if (carry) + { + remainarray[arraylen++] = carry; + } +} +void BigSqrt(char *input, int inputlen, int *output, int &outputlen) +{ + int start, s, t, count; + int remain[150], lenrem = 0; + int odd[150], lenodd = 0; + outputlen = 1; + if (inputlen % 2 == 1) + { + start = 1; + s = (input[0] - '0'); + } + else + { + start = 2; + s = (input[0] - '0') * 10 + (input[1] - '0'); + } + for (count = 0, t = 1; s >= t; s -= t, t += 2, count++) + ; + output[0] = count; + remain[0] = 0; + if (s) + { + remain[lenrem++] = s; + } + for (int i = start; i < inputlen; i += 2) + { + updateodd(odd, lenodd, output, outputlen); + s = (input[i] - '0') * 10 + (input[i + 1] - '0'); + updaterem(remain, lenrem, s); + for (count = 0; compare(remain, lenrem, odd, lenodd); count++) + { + sub(remain, lenrem, odd, lenodd); + update(odd, lenodd); + } + updateoutput(output, outputlen, count); + } +} +void BigNumToString(int *input, int inputlen, char *output, int &outputlen) +{ + char temp[50]; + sprintf(temp, "%d", input[inputlen - 1]); + output[0] = '\0'; + strcat(output, temp); + for (int i = inputlen - 2; i >= 0; i--) + { + sprintf(temp, "%04d", input[i]); + strcat(output, temp); + } + outputlen = strlen(output); +} +void StringToBigNum(char *input, int inputlen, int *output, int &outputlen) +{ + int s = inputlen % 4, temp, i, j, seg = inputlen / 4; + char *p = input; + outputlen = 0; + if (s) + { + for (i = temp = 0; i < s; i++) + { + temp = temp * 10 + (*p++) - '0'; + } + output[outputlen++] = temp; + } + for (i = 0; i < seg; i++) + { + for (j = temp = 0; j < 4; j++) + { + temp = temp * 10 + (*p++) - '0'; + } + output[outputlen++] = temp; + } + for (i = 0, j = outputlen - 1; i <= j; i++, j--) + { + int t = output[i]; + output[i] = output[j]; + output[j] = t; + } +} +int ToNumber(char *input, int len) +{ + int result = 0; + for (int i = 0; i < len; i++) + { + result = result * 10 + input[i] - '0'; + } + return result; +} +void Decode(char *input, int inputlen, char *r, int &rlen, char *c, int &clen) +{ + char temp[1500]; + int d[150], lend, d2[300], lend2, p[150], lenp; + int tempr[150], temprlen, tempc[150], tempclen; + StringToBigNum(input, inputlen, p, lenp); + temp[0] = '0'; + strcpy(temp + 1, input); + for (int i = inputlen, carry = 0; i >= 0; i--) + { + int v = (temp[i] - '0') * 8 + carry; + carry = v / 10; + temp[i] = (v % 10) + '0'; + } + for (int i = inputlen, carry = 1; i >= 0 && carry; i--) + { + int v = (temp[i] - '0') + carry; + carry = v / 10; + temp[i] = (v % 10) + '0'; + } + BigSqrt(temp, inputlen + 1, d, lend); + for (int i = 0, borrow = -1; i < lend && borrow != 0; i--) + { + d[i] += borrow; + if (d[i] < 0) + { + d[i] += BASE; + borrow = -1; + } + else + { + borrow = 0; + } + } + for (int i = lend - 1, rem = 0; i >= 0; i--) + { + rem = rem * BASE + d[i]; + d[i] = rem / 2; + rem %= 2; + } + while (d[lend - 1] == 0 && lend > 1) + { + lend--; + } + memset(d2, 0, sizeof(d2)); + for (int i = 0; i < lend; i++) + for (int j = 0; j < lend; j++) + { + int v = d[i] * d[j] + d2[i + j]; + d2[i + j] = v % BASE; + d2[i + j + 1] += v / BASE; + } + lend2 = 2 * lend; + int carry = 0; + for (int i = 0; i < lend2; i++) + { + int t = d2[i] + carry; + if (i < lend) + { + t += d[i]; + } + carry = t / BASE; + d2[i] = t % BASE; + if (i >= lend && !carry) + { + break; + } + } + if (carry) + { + d2[lend2++] = carry; + } + for (int i = lend2 - 1, rem = 0; i >= 0; i--) + { + rem = rem * BASE + d2[i]; + d2[i] = rem / 2; + rem %= 2; + } + while (d2[lend2 - 1] == 0 && lend2 > 1) + { + lend2--; + } + for (int i = 0, borrow = 0; i < lenp; i++) + { + int t = p[i] + borrow; + if (i < lend2) + { + t -= d2[i]; + } + if (t < 0) + { + t += BASE; + borrow = -1; + } + else + { + borrow = 0; + } + tempc[i] = t; + } + tempclen = lenp; + while (tempc[tempclen - 1] == 0 && tempclen > 1) + { + tempclen--; + } + for (int i = 0, borrow = 0; i < lend; i++) + { + int t = d[i] + borrow; + if (i < tempclen) + { + t -= tempc[i]; + } + if (t < 0) + { + t += BASE; + borrow = -1; + } + else + { + borrow = 0; + } + tempr[i] = t; + } + temprlen = lend; + while (tempr[temprlen - 1] == 0 && temprlen > 1) + { + temprlen--; + } + BigNumToString(tempr, temprlen, r, rlen); + BigNumToString(tempc, tempclen, c, clen); +} +int X[500], Y[500], n; +int main() +{ + int t; + while (scanf("%s", str) == 1) + { + if (strcmp(str, "*") == 0) + { + break; + } + char r[1500], c[1500]; + int rlen, clen; + char x[1500], y[1500]; + int xlen, ylen; + Decode(str, strlen(str), r, rlen, c, clen); + n = ToNumber(r, rlen); + strcpy(str, c); + for (int i = 0; i < n - 2; i++) + { + Decode(str, strlen(str), r, rlen, c, clen); + Decode(r, rlen, x, xlen, y, ylen); + X[i] = ToNumber(x, xlen); + Y[i] = ToNumber(y, ylen); + strcpy(str, c); + } + Decode(str, strlen(str), r, rlen, c, clen); + Decode(r, rlen, x, xlen, y, ylen); + X[n - 2] = ToNumber(x, xlen); + Y[n - 2] = ToNumber(y, ylen); + Decode(c, clen, x, xlen, y, ylen); + X[n - 1] = ToNumber(x, xlen); + Y[n - 1] = ToNumber(y, ylen); + X[n] = X[0]; + Y[n] = Y[0]; + int area = 0; + for (int i = 0; i < n; i++) + { + area += X[i] * Y[i + 1] - X[i + 1] * Y[i]; + } + if (area < 0) + { + area = -area; + } + printf("%.1lf\n", (double)(area)*0.5); + } +} diff --git a/11115.cpp b/11115.cpp new file mode 100644 index 0000000..abc8774 --- /dev/null +++ b/11115.cpp @@ -0,0 +1,353 @@ +#include + +using namespace std; + +/* bignum.c + Implementation of large integer arithmetic: addition, subtraction, + multiplication, and division. + + begun: February 7, 2002 + by: Steven Skiena +*/ + +/* +Copyright 2003 by Steven S. Skiena; all rights reserved. + +Permission is granted for use in non-commerical applications +provided this copyright notice remains intact and unchanged. + +This program appears in my book: + +"Programming Challenges: The Programming Contest Training Manual" +by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. + +See our website www.programming-challenges.com for additional information. + +This book can be ordered from Amazon.com at + +http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ + +*/ + +#define MAXDIGITS 10000 /* maximum length bignum */ + +#define PLUS 1 /* positive sign bit */ +#define MINUS -1 /* negative sign bit */ + +typedef struct +{ + char digits[MAXDIGITS]; /* represent the number */ + int signbit; /* 1 if positive, -1 if negative */ + int lastdigit; /* index of high-order digit */ +} bignum; + +void print_bignum(bignum *); +void string_to_bignum(string, bignum *); +void int_to_bignum(int, bignum *); +void initialize_bignum(bignum *); +int max(int, int); +void add_bignum(bignum *, bignum *, bignum *); +void subtract_bignum(bignum *, bignum *, bignum *); +int compare_bignum(bignum *, bignum *); +void zero_justify(bignum *); +void digit_shift(bignum *, int); +void multiply_bignum(bignum *, bignum *, bignum *); +void divide_bignum(bignum *, bignum *, bignum *); + +void print_bignum(bignum *n) +{ + int i; + if (n->signbit == MINUS) + { + printf("- "); + } + for (i = n->lastdigit; i >= 0; i--) + { + printf("%c", '0' + n->digits[i]); + } + printf("\n"); +} + +void string_to_bignum(string s, bignum *n) +{ + n->signbit = PLUS;// TODO: currently no negative numbers + for (int i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + for (int i = s.size() - 1; i >= 0; i--) + { + n->lastdigit++; + n->digits[n->lastdigit] = s[i] - 48; + } + if (s == "0") + { + n->lastdigit = 0; + } +} + +void int_to_bignum(int s, bignum *n) +{ + int i; /* counter */ + int t; /* int to work with */ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + for (i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + t = abs(s); + while (t > 0) + { + n->lastdigit++; + n->digits[n->lastdigit] = (t % 10); + t = t / 10; + } + if (s == 0) + { + n->lastdigit = 0; + } +} + +void initialize_bignum(bignum *n) +{ + int_to_bignum(0, n); +} + +int max(int a, int b) +{ + if (a > b) + { + return (a); + } + else + { + return (b); + } +} + +/* c = a +-/* b; */ + +void add_bignum(bignum *a, bignum *b, bignum *c) +{ + int carry; /* carry digit */ + int i; /* counter */ + initialize_bignum(c); + if (a->signbit == b->signbit) + { + c->signbit = a->signbit; + } + else + { + if (a->signbit == MINUS) + { + a->signbit = PLUS; + subtract_bignum(b, a, c); + a->signbit = MINUS; + } + else + { + b->signbit = PLUS; + subtract_bignum(a, b, c); + b->signbit = MINUS; + } + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit) + 1; + carry = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + c->digits[i] = (char)(carry + a->digits[i] + b->digits[i]) % 10; + carry = (carry + a->digits[i] + b->digits[i]) / 10; + } + zero_justify(c); +} + +void subtract_bignum(bignum *a, bignum *b, bignum *c) +{ + int borrow; /* has anything been borrowed? */ + int v; /* placeholder digit */ + int i; /* counter */ + initialize_bignum(c); + if ((a->signbit == MINUS) || (b->signbit == MINUS)) + { + b->signbit = -1 * b->signbit; + add_bignum(a, b, c); + b->signbit = -1 * b->signbit; + return; + } + if (compare_bignum(a, b) == PLUS) + { + subtract_bignum(b, a, c); + c->signbit = MINUS; + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit); + borrow = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + v = (a->digits[i] - borrow - b->digits[i]); + if (a->digits[i] > 0) + { + borrow = 0; + } + if (v < 0) + { + v = v + 10; + borrow = 1; + } + c->digits[i] = (char)v % 10; + } + zero_justify(c); +} + +int compare_bignum(bignum *a, bignum *b) +{ + int i; /* counter */ + if ((a->signbit == MINUS) && (b->signbit == PLUS)) + { + return (PLUS); + } + if ((a->signbit == PLUS) && (b->signbit == MINUS)) + { + return (MINUS); + } + if (b->lastdigit > a->lastdigit) + { + return (PLUS * a->signbit); + } + if (a->lastdigit > b->lastdigit) + { + return (MINUS * a->signbit); + } + for (i = a->lastdigit; i >= 0; i--) + { + if (a->digits[i] > b->digits[i]) + { + return (MINUS * a->signbit); + } + if (b->digits[i] > a->digits[i]) + { + return (PLUS * a->signbit); + } + } + return (0); +} + +void zero_justify(bignum *n) +{ + while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0)) + { + n->lastdigit--; + } + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + n->signbit = PLUS; /* hack to avoid -0 */ + } +} + +void digit_shift(bignum *n, int d) /* multiply n by 10^d */ +{ + int i; /* counter */ + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + return; + } + for (i = n->lastdigit; i >= 0; i--) + { + n->digits[i + d] = n->digits[i]; + } + for (i = 0; i < d; i++) + { + n->digits[i] = 0; + } + n->lastdigit = n->lastdigit + d; +} + +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int i, j; /* counters */ + initialize_bignum(c); + row = *a; + for (i = 0; i <= b->lastdigit; i++) + { + for (j = 1; j <= b->digits[i]; j++) + { + add_bignum(c, &row, &tmp); + *c = tmp; + } + digit_shift(&row, 1); + } + c->signbit = a->signbit * b->signbit; + zero_justify(c); +} + +void divide_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int asign, bsign; /* temporary signs */ + int i, j; /* counters */ + initialize_bignum(c); + c->signbit = a->signbit * b->signbit; + asign = a->signbit; + bsign = b->signbit; + a->signbit = PLUS; + b->signbit = PLUS; + initialize_bignum(&row); + initialize_bignum(&tmp); + c->lastdigit = a->lastdigit; + for (i = a->lastdigit; i >= 0; i--) + { + digit_shift(&row, 1); + row.digits[0] = a->digits[i]; + c->digits[i] = 0; + while (compare_bignum(&row, b) != PLUS) + { + c->digits[i]++; + subtract_bignum(&row, b, &tmp); + row = tmp; + } + } + zero_justify(c); + a->signbit = asign; + b->signbit = bsign; +} + +int main() +{ + int n, d; + while (cin >> n >> d) + { + if (n == 0 && d == 0) + { + break; + } + if (d == 0) + { + cout << 1 << endl; + continue; + } + bignum a, b, c, one; + int_to_bignum(n, &a); + int_to_bignum(n, &b); + int_to_bignum(1, &one); + for (int i = 1; i < d; i++) + { + multiply_bignum(&a, &b, &c); + multiply_bignum(&c, &one, &a); + } + print_bignum(&a); + } + return 0; +} diff --git a/11116.cpp b/11116.cpp new file mode 100644 index 0000000..bd2a29d --- /dev/null +++ b/11116.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +const double EPS = 10E-9; + +struct disc +{ + double x, y, r; +}; + +//Represents a mass "m" with center of mass at point ("x", "y") +struct mass +{ + double x, y, m; + mass() {} + mass(double X, double Y, double M) : x(X), y(Y), m(M) {} +}; + +//Returns the new mass formed by combining both masses "a" and "b" into a punctual mass. +mass operator|(const mass &a, const mass &b) +{ + mass r; + r.x = (a.x * a.m + b.x * b.m) / (a.m + b.m); + r.y = (a.y * a.m + b.y * b.m) / (a.m + b.m); + r.m = a.m + b.m; + return r; +} + +//True if the center of mass of "C" is outside the base of disc "D" +inline bool outside(const mass &c, const disc &d) +{ + return (c.x - d.x) * (c.x - d.x) + (c.y - d.y) * (c.y - d.y) > d.r * d.r - EPS; +} + +int main() +{ + int n; + while (cin >> n && n) + { + vector d(n); + for (int i = 0; i < n; ++i) + { + cin >> d[i].x >> d[i].y >> d[i].r; + } + int k; + bool ok = true; + for (int j = 1; j < n && ok; ++j) + { + mass c(d[j].x, d[j].y, d[j].r * d[j].r); + for (int i = j - 1; i >= 0 && ok; --i) + { + if (outside(c, d[i])) + { + ok = false; + k = j; + } + c = c | mass(d[i].x, d[i].y, d[i].r * d[i].r); + } + } + cout << (ok ? "F" : "Unf") << "easible"; + if (!ok) + { + cout << " " << k; + } + cout << endl; + } + return 0; +} diff --git a/11117.cpp b/11117.cpp new file mode 100644 index 0000000..ec9fd29 --- /dev/null +++ b/11117.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +/* + Accepted + */ + +typedef vector> matrix; + +string e; +int pos; +bool error; +matrix a, b, empty; + +void match(const string &s) +{ + int n = s.size(); + for (int i = 0; i < n; ++i) + { + assert(e[pos++] == s[i]); + } +} + +void print(const matrix &a) +{ + for (int i = 0; i < a.size(); ++i) + { + for (int j = 0; j < a[i].size(); ++j) + { + cout << a[i][j]; + } + cout << endl; + } + //cout << endl; +} + +matrix turn(const matrix &t) +{ + matrix r; + for (int j = 0; j < t[0].size(); ++j) + { + r.push_back(vector()); + for (int i = t.size() - 1; i >= 0; --i) + { + r.back().push_back(t[i][j]); + } + } + for (int i = 0; i < r.size(); ++i) + { + for (int j = 0; j < r[i].size(); ++j) + { + if (r[i][j] == '/') + { + r[i][j] = '\\'; + } + else if (r[i][j] == '\\') + { + r[i][j] = '/'; + } + else if (r[i][j] == '-') + { + r[i][j] = '|'; + } + else if (r[i][j] == '|') + { + r[i][j] = '-'; + } + } + } + return r; +} + +matrix sew(matrix x, const matrix &y) +{ + if (x.size() != y.size()) + { + ::error = true; + return empty; + } + for (int i = 0; i < y.size(); ++i) + { + for (int j = 0; j < y[i].size(); ++j) + { + x[i].push_back(y[i][j]); + } + } + return x; +} + +matrix Q() +{ + if (e[pos] == 'A') + { + match("A"); + return a; + } + else if (e[pos] == 'B') + { + match("B"); + return b; + } + else if (e[pos] == 't') + { + match("turn("); + matrix t = Q(); + if (::error) + { + return empty; + } + match(")"); + return turn(t); + } + else if (e[pos] == 's') + { + match("sew("); + matrix x = Q(); + if (::error) + { + return empty; + } + match(","); + matrix y = Q(); + if (::error) + { + return empty; + } + match(")"); + matrix s = sew(x, y); + return s; + } +} + +void initialize() +{ + a = vector>(2, vector(2, '/')); + a[1][1] = '+'; + //print(a); + b = vector>(2, vector(2, '-')); + //print(b); + empty = vector>(); +} + +int main() +{ + int quiltNo = 1; + initialize(); + while (!cin.eof()) + { + char c; + e = ""; + pos = 0; + ::error = false; + while (cin >> c && c != ';') + { + e += c; + } + if (e == "") + { + break; + } + matrix q = Q(); + cout << "Quilt " << quiltNo++ << ":" << endl; + //cout << e << endl; + if (::error) + { + cout << "error" << endl; + } + else + { + print(q); + } + } + return 0; +} diff --git a/11118.cpp b/11118.cpp new file mode 100644 index 0000000..c302517 --- /dev/null +++ b/11118.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int T, cc, n; +double p = 0.0, q = 0.0; +int main() +{ + scanf("%d", &T); + for (int cc = 0; cc < T; ++cc) + { + scanf("%d", &n); + q = 0.0; + for (int r = 1 + n / 2; r <= n; ++r) + { + q += 1 / (double)r; + } + p = 1 - q; + printf("Case #%d: %lf %lf\n", cc + 1, p, 1 / p); + } + return 0; +} diff --git a/11119.cpp b/11119.cpp new file mode 100644 index 0000000..0f953ab --- /dev/null +++ b/11119.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +char cname[128][8], aname[128][8]; +int nc, na, cmat[128][128], amat[128][128], id_of[128][128], N, cat[128], an[128], cmatch_[128], cnext[128], amatch[128], corder[128][128], cmp_cat; + +bool cmp(int i1, int i2) +{ + return cmat[cmp_cat][an[i1]] > cmat[cmp_cat][an[i2]]; +} + +void solve() +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + corder[i][j] = j; + } + cmp_cat = cat[i]; + sort(corder[i], corder[i] + N, cmp); + } + for (int i = 0; i < N; i++) + { + cmatch_[i] = amatch[i] = -1; + cnext[i] = 0; + } + for (;;) + { + bool ok = true; + for (int i = 0; i < N; i++) + { + if (cmatch_[i] >= 0) + { + continue; + } + ok = false; + int j = corder[i][cnext[i]++]; + assert(j < N); + if (amatch[j] < 0 || amat[an[j]][cat[i]] > amat[an[j]][cat[amatch[j]]]) + { + if (amatch[j] >= 0) + { + cmatch_[amatch[j]] = -1; + } + amatch[j] = i; + cmatch_[i] = j; + } + } + if (ok) + { + break; + } + } + for (int i = 0; i < N; i++) + { + if (i > 0) + { + printf(" "); + } + printf("%s%s", cname[cat[i]], aname[an[cmatch_[i]]]); + } + printf("\n\n"); +} + +int main() +{ + for (int cs = 1; scanf("%d", &nc) == 1 && nc > 0; cs++) + { + for (int i = 0; i < nc; i++) + { + scanf(" %s", cname[i]); + id_of[cname[i][0]][cname[i][1]] = i; + } + scanf("%d", &na); + for (int i = 0; i < na; i++) + { + scanf(" %s", aname[i]); + id_of[aname[i][0]][aname[i][1]] = i; + } + for (int i = 0; i < nc; i++) + { + for (int j = 0; j < na; j++) + { + scanf("%d", &cmat[i][j]); + } + } + for (int i = 0; i < na; i++) + { + for (int j = 0; j < nc; j++) + { + scanf("%d", &amat[i][j]); + } + } + for (int sub = 1; scanf("%d", &N) == 1 && N > 0; sub++) + { + for (int j = 0; j < N; j++) + { + char buf[16]; + scanf(" %s", buf); + cat[j] = id_of[buf[0]][buf[1]]; + an[j] = id_of[buf[2]][buf[3]]; + } + printf("Scenario %d, Mixture %d:\n", cs, sub); + solve(); + } + } + return 0; +} diff --git a/1112.cpp b/1112.cpp new file mode 100644 index 0000000..e61bc31 --- /dev/null +++ b/1112.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int G[101][101]; +const int INF = 1e8; + +void solve() +{ + int n, e, t, m; + cin >> n >> e >> t >> m; + --e; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + G[i][j] = INF; + } + for (int i = 0; i < n; i++) + { + G[i][i] = 0; + } + for (int i = 0; i < m; i++) + { + int a, b, c; + cin >> a >> b >> c; + --a, --b; + G[a][b] = c; + } + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + G[i][j] = min(G[i][j], G[i][k] + G[k][j]); + } + } + } + int res = 0; + for (int i = 0; i < n; i++) + { + if (G[i][e] <= t) + { + res++; + } + } + cout << res << endl; +} + +int main() +{ + int t; + cin >> t; + for (int i = 0; i < t; i++) + { + if (i != 0) + { + cout << endl; + } + solve(); + } + return 0; +} diff --git a/11120.cpp b/11120.cpp new file mode 100644 index 0000000..60c3a05 --- /dev/null +++ b/11120.cpp @@ -0,0 +1,1299 @@ +#include + +using namespace std; + +class BigInt +{ + int size; + unsigned *data; + int alloc; + + typedef unsigned long long uint64; + static inline int sgn(int n) + { + return n == 0 ? 0 : (n < 0 ? -1 : 1); + } + + // Removes leading zeroes + void normalize() + { + int n = abs(size); + while (n > 0 && data[n - 1] == 0) + { + n--; + } + size = (size < 0 ? -n : n); + } + + static int absCmp(const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn != yn) + { + return sgn(xn - yn); + } + for (int i = xn - 1; i >= 0; i--) + if (x.data[i] != y.data[i]) + { + return x.data[i] > y.data[i] ? +1 : -1; + } + return 0; + } + + // z = abs(x) + abs(y); + static void absAdd(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn < yn) + { + absAdd(z, y, x); + return; + } + int zn = max(xn, yn); + z.reserve(zn + 1); + uint64 c = 0; + for (int i = 0; i < yn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i] + (uint64)y.data[i]) & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 0 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[zn++] = (unsigned)c; + } + z.size = zn; + } + + // z = abs(x) + abs(y) + static void absAdd1(BigInt &z, const BigInt &x, unsigned y) + { + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = y; + if (&z == &x) + { + for (int i = 0; c != 0 && i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = 0; i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = n; + } + + // z = abs(x) - abs(y) + static void absSub(BigInt &z, const BigInt &x, const BigInt &y) + { + int t = absCmp(x, y); + if (t <= 0) + { + if (t == 0) + { + z.size = 0; + } + else + { + absSub(z, y, x); + } + z.size = -z.size; + return; + } + int xn = abs(x.size), yn = abs(y.size); + z.reserve(max(xn, yn)); + uint64 c = 1; + for (int i = 0; i < yn; i++, c >>= 32) + { + c += (uint64)x.data[i] + ((uint64)y.data[i] ^ 0xFFFFFFFFULL); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + assert(c == 1); + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + assert(z.size > 0); + } + + // z = abs(x) - abs(y) + static void absSub1(BigInt &z, const BigInt &x, unsigned y) + { + if (y == 0) + { + z.assign(x); + z.size = abs(z.size); + return; + } + if (x.size == 0) + { + z.size = -1; + z.data[0] = y; + return; + } + int xn = abs(x.size); + if (xn == 1) + { + if (x.data[0] > y) + { + z.size = 1; + z.data[0] = x.data[0] - y; + } + else if (x.data[0] == y) + { + z.size = 0; + } + else + { + z.size = -1; + z.data[0] = y - x.data[0]; + } + return; + } + z.reserve(xn); + uint64 c = 1 + (uint64)x.data[0] + (y ^ 0xFFFFFFFFULL); + z.data[0] = (unsigned)c; + c >>= 32; + if (&z == &x) + { + for (int i = 1; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + else + { + for (int i = 1; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + } + + // z = abs(x) * m + a + static void absMulAdd1(BigInt &z, const BigInt &x, unsigned m, unsigned a) + { + int n = abs(x.size); + z.reserve(n + 2); + uint64 c = a; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)m); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + while (c != 0) + { + z.data[n++] = (unsigned)c; + c >>= 32; + } + z.size = n; + } + + // z = x + sign*y. Asserts: abs(sign) = 1 + static void add(BigInt &z, const BigInt &x, int sign, const BigInt &y) + { + int xs = sgn(x.size), ys = sign * sgn(y.size); + if (xs == 0) + { + z.assign(y); + z.size *= sign; + } + else if (ys == 0) + { + z.assign(x); + } + else if (xs == ys) + { + absAdd(z, x, y); + z.size *= xs; + } + else if (ys < 0) + { + absSub(z, x, y); + } + else + { + absSub(z, x, y); + z.size = -z.size; + } + } + + static void add1s(BigInt &z, const BigInt &x, int y) + { + int xs = (x.size >= 0 ? +1 : -1), ys = (y >= 0 ? +1 : -1); + if (xs == ys) + { + absAdd1(z, x, abs(y)); + z.size *= xs; + } + else if (ys < 0) + { + absSub1(z, x, -y); + } + else + { + absSub1(z, x, y); + z.size = -z.size; + } + } + + static void mul1s(BigInt &z, const BigInt &x, int y) + { + if (y < 0) + { + mul1s(z, x, -y); + z.size = -z.size; + return; + } + if (y == 0) + { + z.size = 0; + return; + } + if (y == 1) + { + z.assign(x); + return; + } + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = 0; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)y); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = (x.size < 0 ? -n : n); + } + + static void mulQuadratic(BigInt &z, const BigInt &x, const BigInt &y) + { + if (&z == &x || &z == &y) + { + BigInt t; + mulQuadratic(t, x, y); + z = t; + return; + } + int xn = abs(x.size), yn = abs(y.size), zn = xn + yn + 1; + z.reserve(zn); + for (int i = 0; i < zn; i++) + { + z.data[i] = 0; + } + for (int i = 0; i < xn; i++) + { + uint64 c = 0; + int k = i; + for (int j = 0; j < yn; j++, k++, c >>= 32) + { + c += z.data[k] + x.data[i] * (uint64)y.data[j]; + z.data[k] = (unsigned)(c & 0xFFFFFFFFU); + } + for (; c != 0; k++, c >>= 32) + { + z.data[k] = (unsigned)((c += z.data[k]) & 0xFFFFFFFFU); + } + } + z.size = zn * sgn(x.size) * sgn(y.size); + z.normalize(); + } + + static void mulKaratsuba(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size), zs = sgn(x.size) * sgn(y.size); + int w = max(xn, yn) >> 1; + BigInt A(x.data + w, max(0, xn - w)), B(x.data, min(xn, w)); + BigInt C(y.data + w, max(0, yn - w)), D(y.data, min(yn, w)); + BigInt R, T; + absAdd(z, A, B); + absAdd(T, C, D); + mul(R, z, T); + mul(z, A, C); + mul(T, B, D); + R -= z; + R -= T; + R <<= w * 32; + z <<= w * 64; + z += R; + z += T; + z.size *= zs; + } + + BigInt(unsigned a[], int n) + { + if (n < 0) + { + n = 0; + } + while (n > 0 && a[n - 1] == 0) + { + n--; + } + data = NULL; + reserve(n); + size = n; + memcpy(data, a, n * sizeof(unsigned)); + } + + // q = abs(x) div abs(y); Returns abs(x) mod abs(y) + static unsigned absDiv1(BigInt &q, const BigInt &x, unsigned y) + { + assert(y != 0); + int n = abs(x.size); + q.reserve(n); + uint64 c = 0; + for (int i = n - 1; i >= 0; i--) + { + c = (c << 32) | (uint64)x.data[i]; + q.data[i] = (unsigned)(c / y); + c %= y; + } + q.size = n; + q.normalize(); + return (unsigned)c; + } + + // u = abs(u) mod abs(v), q = abs(u) div abs(v) + static void absDiv(BigInt *q, BigInt &u, BigInt v) + { + if (q != NULL && q == &u) + { + BigInt t; + absDiv(&t, u, v); + *q = t; + return; + } + u.size = abs(u.size); + v.size = abs(v.size); + assert(v.size > 0); + if (u.size < v.size) + { + if (q) + { + *q = 0; + } + return; + } + if (v.size == 1) + { + unsigned t = absDiv1(q == NULL ? u : *q, u, v.data[0]); + u.data[0] = t; + u.size = (t == 0 ? 0 : 1); + return; + } + int n = v.size, d = 0; + while (((v.data[n - 1] << d) & 0x80000000U) == 0) + { + d++; + } + u <<= d; + v <<= d; + unsigned vh = v.data[n - 1], vh2 = v.data[n - 2]; + uint64 c, g; + u.reserve(u.size + 1); + u.data[u.size] = 0; + v.reserve(v.size + 1); + v.data[v.size] = 0; + int qp = u.size - v.size + 1; + if (q) + { + q->reserve(qp + 1); + q->size = qp; + } + for (int up = u.size - 1; --qp >= 0; up--) + { + c = (((uint64)u.data[up + 1]) << 32) | (uint64)u.data[up]; + g = c / (uint64)vh; + if (g > 0xFFFFFFFFULL) + { + g = 0xFFFFFFFFULL; + } + while ((c - g * vh) < 0x100000000ULL && + (((c - g * vh) << 32) + u.data[up - 1]) < (g * (uint64)vh2)) + { + g--; + } + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++) + { + c += g * (uint64)v.data[j]; + if (u.data[i] >= (unsigned)(c & 0xFFFFFFFFULL)) + { + u.data[i] -= (unsigned)(c & 0xFFFFFFFFULL); + c >>= 32; + } + else + { + u.data[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL)); + c = (c >> 32) + 1; + } + } + if (c != 0) + { + g--; + assert(c == 1); + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++, c >>= 32) + { + c += (uint64)u.data[i] + (uint64)v.data[j]; + u.data[i] = (unsigned)c; + } + assert(c == 1); + } + if (q) + { + q->data[qp] = (unsigned)g; + } + } + u >>= d; + v >>= d; + if (q) + { + q->normalize(); + } + } + +public: + static int cmp(const BigInt &x, const BigInt &y) + { + if (x.size != y.size) + { + return sgn(x.size - y.size); + } + return absCmp(x, y) * (x.size < 0 ? -1 : 1); + } + + static int cmp1s(const BigInt &x, int y) + { + if (y == 0) + { + return sgn(x.size); + } + if (y > 0) + { + if (x.size <= 0) + { + return -1; + } + if (x.size > 1) + { + return +1; + } + if (x.data[0] == (unsigned)y) + { + return 0; + } + return x.data[0] > (unsigned)y ? +1 : -1; + } + else + { + if (x.size >= 0) + { + return +1; + } + if (x.size < -1) + { + return -1; + } + if (x.data[0] == (unsigned)(-y)) + { + return 0; + } + return x.data[0] > (unsigned)(-y) ? -1 : +1; + } + } + + static void add(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, +1, y); + } + + static void sub(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, -1, y); + } + + static void mul(BigInt &z, const BigInt &x, const BigInt &y) + { + if (max(abs(x.size), abs(y.size)) < 64) + { + mulQuadratic(z, x, y); + } + else + { + mulKaratsuba(z, x, y); + } + } + + static void mod(BigInt &r, const BigInt &u, const BigInt &v) + { + r = u; + absDiv(NULL, r, v); + } + + static void div(BigInt &q, BigInt &r, const BigInt &u, const BigInt &v) + { + int us = sgn(u.size), vs = sgn(v.size); + r = u; + absDiv(&q, r, v); + //TODO + if (us * vs < 0) + { + q.size = -q.size; + } + } + + void reserve(int n) + { + if (data == NULL) + { + alloc = (n < 2 ? 2 : n); + data = (unsigned *)malloc(alloc * sizeof(unsigned)); + } + else if (alloc < n) + { + while (alloc < n) + { + alloc *= 2; + } + data = (unsigned *)realloc(data, alloc * sizeof(unsigned)); + } + assert(data != NULL); + } + + void assign(int n) + { + reserve(1); + size = sgn(n); + data[0] = abs(n); + } + + void assign(long long n) + { + reserve(2); + if (n < 0) + { + size = -2; + n = -n; + } + else + { + size = 2; + } + data[0] = (unsigned)(n & 0xFFFFFFFFU); + data[1] = (unsigned)(n >> 32); + normalize(); + } + + void assign(const BigInt &b) + { + if (this == &b) + { + return; + } + int n = abs(b.size); + reserve(n); + size = b.size; + memcpy(data, b.data, n * sizeof(unsigned)); + } + + void assign(const char *s, int radix = 10) + { + assert(2 <= radix && radix <= 36); + while (isspace(*s)) + { + s++; + } + int sign = 1; + if (*s == '+') + { + s++; + } + else if (*s == '-') + { + s++; + sign = -1; + } + while (*s == '0') + { + s++; + } + int n = 0; + for (n = 0; s[n] && isalnum(s[n]); n++) + ; + size = 0; + if (n > 20) + { + reserve((int)(log((double)radix) / log(2.0) / 32.0 * n) + 2); + } + else + { + reserve(n / 9 + 2); + } + unsigned a = 0, m = 1; + for (int i = 0; i < n; i++) + { + int dig = (isdigit(s[i]) ? (s[i] - '0') : (toupper(s[i]) - 'A' + 10)); + assert(dig < radix); + a = a * radix + dig; + m *= radix; + if (m > 0x3000000) + { + absMulAdd1(*this, *this, m, a); + a = 0; + m = 1; + } + } + if (m > 1) + { + absMulAdd1(*this, *this, m, a); + } + size *= sign; + } + + BigInt(int n = 0) + { + data = NULL; + assign(n); + } + BigInt(int n, int cap) + { + data = NULL; + reserve(cap); + assign(n); + } + BigInt(long long n) + { + data = NULL; + assign(n); + } + BigInt(const BigInt &b) + { + data = NULL; + assign(b); + } + BigInt(const char *s, int radix = 10) + { + data = NULL; + assign(s, radix); + } + BigInt(const string &s, int radix = 10) + { + data = NULL; + assign(s.c_str(), radix); + } + ~BigInt() + { + if (alloc > 0) + { + free(data); + } + } + + BigInt &operator=(int n) + { + assign(n); + return *this; + } + BigInt &operator=(long long n) + { + assign(n); + return *this; + } + BigInt &operator=(const BigInt &b) + { + assign(b); + return *this; + } + BigInt &operator=(const char *s) + { + assign(s); + return *this; + } + BigInt &operator=(const string &s) + { + assign(s.c_str()); + return *this; + } + BigInt &operator+=(const BigInt &y) + { + add(*this, *this, +1, y); + return *this; + } + BigInt &operator-=(const BigInt &y) + { + add(*this, *this, -1, y); + return *this; + } + BigInt &operator*=(const BigInt &y) + { + mul(*this, *this, y); + return *this; + } + BigInt &operator/=(const BigInt &y) + { + BigInt q, r; + div(q, r, *this, y); + assign(q); + return *this; + } + BigInt &operator%=(const BigInt &y) + { + mod(*this, *this, y); + return *this; + } + BigInt &operator+=(int y) + { + add1s(*this, *this, y); + return *this; + } + BigInt &operator-=(int y) + { + add1s(*this, *this, -y); + return *this; + } + BigInt &operator*=(int y) + { + mul1s(*this, *this, y); + return *this; + } + BigInt &operator<<=(int n) + { + shl(n); + return *this; + } + BigInt &operator>>=(int n) + { + shr(n); + return *this; + } + void operator++() + { + add1s(*this, *this, 1); + } + void operator--() + { + add1s(*this, *this, -1); + } + BigInt operator-() + { + BigInt z = *this; + z.negate(); + return z; + } + BigInt operator>>(int n) const + { + BigInt r = *this; + r.shr(n); + return r; + } + BigInt operator<<(int n) const + { + BigInt r = *this; + r.shl(n); + return r; + } + + string str(int radix = 10) const + { + assert(2 <= radix && radix <= 36); + if (size == 0) + { + return "0"; + } + int y = 1, m = 0; + while (y < 0x300000) + { + y *= radix; + m++; + } + BigInt x(*this); + x.size = abs(x.size); + char *tmp = (char *)malloc(x.size * (radix >= 10 ? 10 : 32) + 10); + int n = 0; + while (x.size != 0) + { + unsigned r = absDiv1(x, x, y); + for (int i = 0; i < m; i++, r /= radix) + { + tmp[n++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[r % radix]; + } + } + while (n > 0 && tmp[n - 1] == '0') + { + n--; + } + if (size < 0) + { + tmp[n++] = '-'; + } + reverse(tmp, tmp + n); + tmp[n] = 0; + string res = string(tmp); + free(tmp); + return res; + } + + string toString(int r = 10) const + { + return str(r); + } + int toInt() const + { + return sgn(size) * (int)data[0]; + } + long long toLL() const + { + long long r = 0; + for (int i = 0; i < 2 && i < abs(size); i++) + { + r = (r << 32) | data[i]; + } + return size < 0 ? -r : r; + } + + void setBit(int n) + { + int s = abs(size), m = (n >> 5) + 1; + reserve(m); + while (s < m) + { + data[s++] = 0; + } + size = (size < 0 ? -s : s); + data[n >> 5] |= 1U << (n & 31); + } + + void clrBit(int n) + { + if (abs(size) > (n >> 5)) + { + data[n >> 5] &= ~(1U << (n & 31)); + normalize(); + } + } + int getBit(int n) const + { + return (abs(size) > (n >> 5)) ? ((data[n >> 5] >> (n & 31)) & 1) : 0; + } + + // Returns 1+(index of highest non-zero bit) + int bitSize() const + { + if (size == 0) + { + return 0; + } + int n = (abs(size) - 1) * 32; + unsigned t = data[abs(size) - 1]; + if (t >= 65536) + { + n += 16; + t >>= 16; + } + if (t >= 256) + { + n += 8; + t >>= 8; + } + if (t >= 16) + { + n += 4; + t >>= 4; + } + if (t >= 4) + { + n += 2; + t >>= 2; + } + if (t >= 2) + { + n++; + t >>= 1; + } + if (t >= 1) + { + n++; + } + return n; + } + + void shl(int s) + { + if (size == 0) + { + return; + } + if (s < 0) + { + shr(-s); + } + int n = abs(size), w = s >> 5; + reserve(n + w + 1); + if (w > 0) + { + for (int i = n - 1; i >= 0; i--) + { + data[i + w] = data[i]; + } + for (int i = w - 1; i >= 0; i--) + { + data[i] = 0; + } + n += w; + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + data[n++] = 0; + for (int i = 0; i < n; i++) + { + b = data[i] >> (32 - s); + data[i] = (data[i] << s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void shr(int s) + { + if (s < 0) + { + shl(-s); + } + int n = abs(size), w = s >> 5; + if (w > 0) + { + for (int i = 0; i + w < n; i++) + { + data[i] = data[i + w]; + } + n -= w; + if (n < 0) + { + n = 0; + } + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + for (int i = n - 1; i >= 0; i--) + { + b = data[i] << (32 - s); + data[i] = (data[i] >> s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void negate() + { + size = -size; + } + int sign() const + { + return sgn(size); + } + void swap(BigInt &y) + { + std::swap(size, y.size); + std::swap(alloc, y.alloc); + std::swap(data, y.data); + } + int compareTo(const BigInt &y) const + { + return cmp(*this, y); + } + int compareTo(int y) const + { + return cmp1s(*this, y); + } +}; + +BigInt operator+(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::add(z, x, y); + return z; +} +BigInt operator-(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::sub(z, x, y); + return z; +} +BigInt operator*(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::mul(z, x, y); + return z; +} +BigInt operator/(const BigInt &x, const BigInt &y) +{ + BigInt q, r; + BigInt::div(q, r, x, y); + return q; +} +BigInt operator%(const BigInt &x, const BigInt &y) +{ + BigInt r; + BigInt::mod(r, x, y); + return r; +} +bool operator==(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) == 0; +} +bool operator==(const BigInt &x, int y) +{ + return BigInt::cmp1s(x, y) == 0; +} +bool operator!=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) != 0; +} +bool operator<(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) < 0; +} +bool operator<=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) <= 0; +} +bool operator>(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) > 0; +} +bool operator>=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) >= 0; +} +ostream &operator<<(ostream &o, const BigInt &x) +{ + return o << x.str(); +} +istream &operator>>(istream &i, BigInt &x) +{ + string s; + i >> s; + x = s; + return i; +} +namespace std +{ +template <> +inline void swap(BigInt &a, BigInt &b) +{ + a.swap(b); +} +} + +// Returns floor(sqrt(x)). +BigInt isqrt(const BigInt &a) +{ + assert(a >= 0); + if (a == 0) + { + return 0; + } + BigInt x, y; + x.setBit(a.bitSize() / 2 + 2); + for (;;) + { + y = a; + y /= x; + y += x; + y >>= 1; + if (y < x) + { + x = y; + } + else + { + return x; + } + } +} + +BigInt pow(BigInt b, int p) +{ + assert(p >= 0); + BigInt r = 1; + for (int i = 0; (p >> i) != 0; i++) + { + if (i != 0) + { + b *= b; + } + if (p & (1 << i)) + { + r *= b; + } + } + return r; +} + +BigInt modpow(BigInt b, const BigInt &e, const BigInt &m) +{ + assert(e >= 0 && m > 0); + if (m == 1) + { + return 0; + } + BigInt r = 1; + for (int i = 0, n = e.bitSize(); i < n; i++) + { + if (i != 0) + { + b *= b; + b %= m; + } + if (e.getBit(i)) + { + r *= b; + r %= m; + } + } + return r; +} + +// Asserts that n is odd, n >= 3, and 1<=base> s, n); + if (t == 1) + { + return true; + } + for (int i = 0; i < s && t > 1; i++) + { + if (i > 0) + { + t *= t; + t %= n; + } + if (t == n1) + { + return true; + } + } + return false; +} + +bool probablePrime(const BigInt &n) +{ + if (n <= 1) + { + return false; + } + if (n.getBit(0) == 0) + { + return (n == 2); + } + if (n < 1048576) + { + for (int d = 3; d * d <= n; d += 2) + if ((n % d) == 0) + { + return false; + } + return true; + } + static int p[] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 0}; + for (int i = 0; p[i] != 0; i++) + if ((n % p[i]) == 0) + { + return false; + } + static int a[] = {2, 7, 61, 3, 24251, 5, 11, 13, 17, 19, 23, 29, 0}; + for (int i = 0; a[i] != 0; i++) + { + if (i == 5 && n.bitSize() < 52) + { + break; + } + if (!millerRabin(n, a[i])) + { + return false; + } + } + return true; +} + +//----------------------------------------------------------------------------- + +int main() +{ + int p; + while (scanf("%d", &p) && p != 0) + { + string s = isqrt(p * pow(BigInt(10), 2800)).str(); + for (int i = 0;; i++) + { + if (s[i] == s[i + 1] && s[i] == s[i + 2]) + { + printf("%d %d %c\n", p, i, s[i]); + break; + } + } + } +} diff --git a/11121.cpp b/11121.cpp index 4c24fc0..ca06810 100644 --- a/11121.cpp +++ b/11121.cpp @@ -1,28 +1,38 @@ -#include -#include -#include -using namespace std; +#include -typedef long long int64; +using namespace std; -int main() { - int64 T, t; - scanf("%lld", &T); - for (t = 1; t <= T; t++) { - int64 N, Base = -2; - string str = ""; - scanf("%lld", &N); - while (N != 0) { - int64 Rem = N % Base; - N /= Base; - if (Rem < 0) { - N += 1; - Rem += 2; - } - str = char('0' + Rem) + str; +string negativetwobase(int n) +{ + string result = ""; + int remainder; + if (n == 0) + { + return "0"; + } + while (n != 0) + { + remainder = n % -2; + n /= -2; + if (remainder < 0) + { + n += 1; + remainder += 2; } - if (str.empty()) str = "0"; - printf("Case #%lld: %s\n", t, str.c_str()); + result += (char)('0' + remainder); + } + return string(result.rbegin(), result.rend()); +} + +int main() +{ + int cases; + int num; + cin >> cases; + for (int i = 0; i < cases; i++) + { + cout << "Case #" << i + 1 << ": "; + cin >> num; + cout << negativetwobase(num) << endl; } - return 0; } diff --git a/11122.cpp b/11122.cpp new file mode 100644 index 0000000..c5c0e36 --- /dev/null +++ b/11122.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define EPS std::numeric_limits::epsilon() + +/** + Two triangles have common internal point <-> there exist one edge in one triangle which can separate this triangle from the other one. +*/ +struct Point +{ + double x, y; + Point(double _x, double _y) : x(_x), y(_y) {} +}; +struct Convex +{ + double tx, ty; + vector p; + Convex() : tx(0), ty(0) {} +}; + +bool sat(Convex c1, Convex c2) +{ + int c1_faces = c1.p.size(); + int c2_faces = c2.p.size(); + // For every face in c1 + for (int i = 0; i < c1_faces; i++) + { + // Grab a face (face x, face y) + double fx = c1.p[i]->x - c1.p[(i + 1) % c1_faces]->x; + double fy = c1.p[i]->y - c1.p[(i + 1) % c1_faces]->y; + // Create a perpendicular axis to project on (axis x, axis y) + double ax = -fy, ay = fx; + // Normalize the axis + double len_v = sqrt(ax * ax + ay * ay); + ax /= len_v; + ay /= len_v; + // Carve out the min and max values + double c1_min = std::numeric_limits::max(), c1_max = -c1_min; + double c2_min = std::numeric_limits::max(), c2_max = -c2_min; + // Project every point in c1 on the axis and store min and max + for (int j = 0; j < c1_faces; j++) + { + double c1_proj = (ax * (c1.p[j]->x + c1.tx) + ay * (c1.p[j]->y + c1.ty)) / (ax * ax + ay * ay); + c1_min = min(c1_proj, c1_min); + c1_max = max(c1_proj, c1_max); + } + // Project every point in c2 on the axis and store min and max + for (int j = 0; j < c2_faces; j++) + { + double c2_proj = (ax * (c2.p[j]->x + c2.tx) + ay * (c2.p[j]->y + c2.ty)) / (ax * ax + ay * ay); + c2_min = min(c2_proj, c2_min); + c2_max = max(c2_proj, c2_max); + } + // separated => no overlap + // EQUAL => count as 'no overlap' + if (c1_max <= c2_min || c1_min >= c2_max) + { + return true; + } + } + return false; +} + +int T; +Convex *c1, *c2; + +int main() +{ + scanf("%d", &T); + for (int cc = 0; cc < T; ++cc) + { + //fprintf(stderr, "test case: "); + c1 = new Convex(); + for (int i = 0; i < 3; ++i) + { + int x, y; + scanf("%d %d", &x, &y); + //fprintf(stderr, "%d %d ",x,y); + c1->p.push_back(new Point(x, y)); + } + //fprintf(stderr, "\n"); + c2 = new Convex(); + for (int i = 0; i < 3; ++i) + { + int x, y; + scanf("%d %d", &x, &y); + //fprintf(stderr, "%d %d ",x,y); + c2->p.push_back(new Point(x, y)); + } + //fprintf(stderr, "\n"); + /*// is vertex of one triangle on edge/vertex of the other? + bool is_on_edge = false; + for (int i=0; i<3; ++i) + { + Point *p1 = c1->p[i%3]; + Point *p2 = c1->p[(i+1)%3]; + Point *p3 = new Point(p2->x-p1->x, p2->y-p1->y); + for (int j=0; j<3; ++j) + { + Point *q = c2->p[j]; + Point *qq = new Point(q->x-p1->x, q->y-p1->y); + + //printf("p1: (%lf,%lf); p2: (%lf,%lf); q: (%lf,%lf)\n", p1->x,p1->y,p2->x,p2->y,q->x,q->y); + if ( fabs( (p3->y * (qq->x / p3->x)) - qq->y) < EPS ) + { + is_on_edge = true; + break; + } + } + } + bool is_on_vertex = false; + for (int i=0; i<3; ++i) + { + Point *p = c1->p[i]; + for (int j=0; j<3; ++j) + { + Point *q = c2->p[j]; + if (fabs(p->x-q->x) < EPS && fabs(p->y-q->y) < EPS) + { + is_on_vertex = true; + break; + } + } + }*/ + bool separated = sat(*c1, *c2) || sat(*c2, *c1); + if (separated) + { + printf("pair %d: no\n", cc + 1); + } + else + { + printf("pair %d: yes\n", cc + 1); + } + } + return 0; +} diff --git a/11123.cpp b/11123.cpp new file mode 100644 index 0000000..dd2a38a --- /dev/null +++ b/11123.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +struct Point +{ + int x, y; + bool operator<(const Point &p) const + { + return x < p.x || (x == p.x && y < p.y); + } + bool operator==(const Point &p) const + { + return x == p.x && y == p.y; + } +}; + +long long cross(const Point &o, const Point &a, const Point &b) +{ + return (a.x - o.x) * (long long)(b.y - o.y) - (a.y - o.y) * (long long)(b.x - o.x); +} + +struct Line +{ + long long dx, dy, cnt; + + bool operator<(const Line &l) const + { + if (dx == 0 || l.dx == 0) + { + return dx == 0 && l.dx != 0; + } + return dy * l.dx < l.dy * dx; + } +}; + +Point P[256]; +int N; + +long long C2(long long n) +{ + return n * (n - 1) / 2; +} + +void makeLines(vector &output) +{ + static char flag[256][256]; + memset(flag, 0, sizeof(flag)); + output.clear(); + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (flag[i][j]) + { + continue; + } + static int v[256]; + int c = 0; + for (int k = 0; k < N; k++) + if (cross(P[i], P[j], P[k]) == 0) + { + v[c++] = k; + } + for (int k = 0; k < c; k++) + for (int m = 0; m < c; m++) + { + flag[v[k]][v[m]] = 1; + } + Line l; + l.dx = P[j].x - P[i].x; + l.dy = P[j].y - P[i].y; + if (l.dx < 0) + { + l.dx = -l.dx; + l.dy = -l.dy; + } + l.cnt = c; + output.push_back(l); + } + } + sort(output.begin(), output.end()); +} + +long long solve() +{ + sort(P, P + N); + for (int i = 1; i < N; i++) + { + assert(P[i].x != P[i - 1].x || P[i].y != P[i - 1].y); + } + vector lines; + makeLines(lines); + int L = lines.size(); + long long fancy = 0; + for (int a = 0, b; a < L; a = b) + { + for (b = a; b < L && !(lines[a] < lines[b]); b++) + ; + long long t = 0; + for (int i = a; i < b; i++) + { + t += C2(lines[i].cnt); + } + for (int i = a; i < b; i++) + { + fancy += C2(lines[i].cnt) * t; + fancy -= C2(lines[i].cnt) * C2(lines[i].cnt); + } + } + fancy /= 2; + long long paral = 0; + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + for (int k = j + 1, m = j + 1; k < N; k++) + { + if (cross(P[i], P[j], P[k]) == 0) + { + continue; + } + Point p; + p.x = P[i].x + (P[j].x - P[i].x) + (P[k].x - P[i].x); + p.y = P[i].y + (P[j].y - P[i].y) + (P[k].y - P[i].y); + if (m <= k) + { + m = k + 1; + } + while (m < N && P[m] < p) + { + m++; + } + if (m == N) + { + break; + } + if (P[m] == p) + { + paral++; + } + } + } + } + return fancy - 2 * paral; +} + +int main() +{ + for (int cs = 1; scanf("%d", &N) == 1 && N > 0; cs++) + { + for (int i = 0; i < N; i++) + { + scanf(" %d %d", &P[i].x, &P[i].y); + } + printf("Case %d: %lld\n", cs, solve()); + } +} diff --git a/11124.cpp b/11124.cpp new file mode 100644 index 0000000..bca3dc4 --- /dev/null +++ b/11124.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int List[1000010], N, K; + +int main() +{ + char buf[50000]; + for (int cs = 1; gets(buf) && sscanf(buf, "%d", &K) == 1 && K > 0; + cs++) + { + N = 0; + gets(buf); + char *p = buf; + while (*p) + { + if (!isalpha(*p)) + { + p++; + continue; + } + char s[100]; + int n, diff; + sscanf(p, " %[A-Za-z] ( %d %n", s, &n, &diff); + p += diff; + s[0] = toupper(s[0]); + if (s[0] == 'N') + { + for (int i = 0; i < n; i++) + { + sscanf(p, " , %d %n", &List[N], &diff); + p += diff; + N++; + } + sscanf(p, " ) %n", &diff); + p += diff; + } + else if (s[0] == 'I') + { + int a, d; + sscanf(p, " , %d , %d ) %n", &a, &d, &diff); + p += diff; + for (int i = 0; i < n; i++, a += d) + { + List[N++] = a; + } + } + else + { + int l, h; + unsigned s; + sscanf(p, " , %d , %d , %u ) %n", &l, &h, &s, &diff); + p += diff; + for (int i = 0; i < n; i++) + { + s = s * 17 + 11; + List[N++] = l + s % (h - l + 1); + } + } + } + /* + printf("List=["); + for(int i = 0;i + +using namespace std; + +#define MAXD 1100000 +int f[MAXD], a[5], b[5]; +int dp(int state) +{ + int i, j, k, t, st = state, ans = 0, fc, fn, pc, pn; + if (f[state] != -1) + { + return f[state]; + } + fc = st % 4, st /= 4; + fn = st % 4, st /= 4; + pc = st % 4, st /= 4; + pn = st % 4, st /= 4; + k = 0; + for (i = 0; i < 4; i++) + { + b[i] = st % 8, st /= 8; + k += b[i]; + } + if (k == 0) + { + if (pc == fc && pn == fn) + { + return f[state] = 1; + } + else + { + return f[state] = 0; + } + } + for (i = 0; i < 4; i++) + { + if (i == pc) + { + continue; + } + for (j = 1; j <= 3 && j <= b[i]; j++) + { + if (j == pn) + { + continue; + } + b[i] -= j; + for (k = 3, t = 0; k >= 0; k--) + { + t = t * 8 + b[k]; + } + t = (((t * 4 + j) * 4 + i) * 4 + fn) * 4 + fc; + ans += dp(t); + b[i] += j; + } + } + return f[state] = ans; +} +void solve() +{ + int i, j, k, n, res, t; + memset(a, 0, sizeof(a)); + scanf("%d", &n); + k = 0; + for (i = 0; i < n; i++) + { + scanf("%d", &a[i]); + k += a[i]; + } + if (!k) + { + printf("1\n"); + return; + } + for (i = 3, k = 0; i >= 0; i--) + { + k = k * 8 + a[i]; + } + res = 0; + for (i = 0; i < n; i++) + for (j = 1; j <= 3 && j <= a[i]; j++) + { + t = (((k * 4 + j) * 4 + i) * 4 + j) * 4 + i; + res += dp(t); + } + printf("%d\n", res); +} +int main() +{ + int t; + memset(f, -1, sizeof(f)); + scanf("%d", &t); + while (t--) + { + solve(); + } + return 0; +} diff --git a/11126.cpp b/11126.cpp new file mode 100644 index 0000000..8a8bed3 --- /dev/null +++ b/11126.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +int pile[8][32], mult[8], base[8]; +bool adj[16][16]; + +short seen[13][17][6][6][6][6][6][6][6], counter; + +bool dfs(int last, int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7) +{ + short &mem = seen[last][a0][a1][a2][a3][a4][a5][a6][a7]; + if (mem == counter) + { + return false; + } + mem = counter; + if ((a0 | a1 | a2 | a3 | a4 | a5 | a6 | a7) == 0) + { + return true; + } + if (a0 > 0 && dfs(pile[0][a0 - 1], a0 - 1, a1, a2, a3, a4, a5, a6, a7)) + { + return true; + } + if (a1 > 0 && adj[pile[1][a1 - 1]][last] && dfs(pile[1][a1 - 1], a0, a1 - 1, a2, a3, a4, a5, a6, a7)) + { + return true; + } + if (a2 > 0 && adj[pile[2][a2 - 1]][last] && dfs(pile[2][a2 - 1], a0, a1, a2 - 1, a3, a4, a5, a6, a7)) + { + return true; + } + if (a3 > 0 && adj[pile[3][a3 - 1]][last] && dfs(pile[3][a3 - 1], a0, a1, a2, a3 - 1, a4, a5, a6, a7)) + { + return true; + } + if (a4 > 0 && adj[pile[4][a4 - 1]][last] && dfs(pile[4][a4 - 1], a0, a1, a2, a3, a4 - 1, a5, a6, a7)) + { + return true; + } + if (a5 > 0 && adj[pile[5][a5 - 1]][last] && dfs(pile[5][a5 - 1], a0, a1, a2, a3, a4, a5 - 1, a6, a7)) + { + return true; + } + if (a6 > 0 && adj[pile[6][a6 - 1]][last] && dfs(pile[6][a6 - 1], a0, a1, a2, a3, a4, a5, a6 - 1, a7)) + { + return true; + } + if (a7 > 0 && adj[pile[7][a7 - 1]][last] && dfs(pile[7][a7 - 1], a0, a1, a2, a3, a4, a5, a6, a7 - 1)) + { + return true; + } + return false; +} + +int main() +{ + int T; + scanf("%d", &T); + memset(seen, 0, sizeof(seen)); + counter = 0; + for (int i = 0; i < 8; i++) + { + base[i] = (i == 0 ? 17 : 6); + mult[i] = (i > 0 ? mult[i - 1] * base[i - 1] : 1); + } + for (int i = 0; i < 13; i++) + for (int j = 0; j < 13; j++) + { + adj[i][j] = (abs(i - j) == 1 || (i == 0 && j == 12) || (i == 12 && j == 0)); + } + for (int cs = 1; cs <= T; cs++) + { + for (int j = 0; j < 5; j++) + { + for (int i = 1; i <= 7; i++) + { + scanf("%d", &pile[i][j]); + pile[i][j]--; + } + } + for (int i = 0; i < 17; i++) + { + scanf("%d", &pile[0][i]); + pile[0][i]--; + } + counter++; + bool res = dfs(pile[0][16], 16, 5, 5, 5, 5, 5, 5, 5); + printf("Case #%d: %s\n", cs, res ? "Yes" : "No"); + } +} diff --git a/11127.cpp b/11127.cpp new file mode 100644 index 0000000..fd1b50f --- /dev/null +++ b/11127.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back + +void output(int in, int len) +{ + for (int i = len - 1; i >= 0; i--) + { + if (in & (1 << i)) + { + cout << 1; + } + else + { + cout << 0; + } + } + cout << endl; +} + +int getlen(int in) +{ + int len = 31; + bool isop = false; + for (int i = 31; i >= 0; i--) + { + if (in & (1 << i)) + { + isop = true; + } + if (!isop) + { + len = i; + } + } + return len; +} + +bool isok(int n, int beg, int len) +{ + rep(i, len) + { + bool a = (n & (1 << (beg + i))) == 0, b = (n & (1 << (beg + len + i))) == 0; + bool c = (n & (1 << (beg + len + len + i))) == 0; + if (a == b && b == c) + ; + else + { + return false; + } + } + return true; +} +bool isTripleFree(int n, int len) +{ + rep(i, len)//pos + { + REP(j, 1, len) + { + if (i + j + j + j > len) + { + break; + } + if (isok(n, i, j)) + { + return false; + } + } + } + return true; +} + +const int N = 31; +vector trip[N]; +void makeall() +{ + int cnt = 0; + trip[1].pb(0); + trip[1].pb(1); + REP(i, 2, 31) + { + rep(j, trip[i - 1].size()) + { + rep(k, 2) + { + int now = trip[i - 1][j] * 2 + k; + if (isTripleFree(now, i)) + { + cnt++; + trip[i].pb(now); + } + } + } + } +} + +void solve(string &in) +{ + int n = (int)in.size(); + int cnt = 0; + rep(i, (int)trip[n].size()) + { + bool isok = true; + rep(j, n) + { + int val = (trip[n][i] & (1 << j)) == 0; + if ((val == 0 && in[j] == '1') || + (val != 0 && in[j] == '0')) + { + isok = false; + break; + } + } + if (isok) + { + cnt++; + } + } + cout << cnt << endl; +} + +main() +{ + makeall(); + int tc = 1; + int n; + string in; + while (cin >> n >> in && n) + { + cout << "Case " << tc++ << ": "; + solve(in); + } + return false; +} diff --git a/11128.cpp b/11128.cpp new file mode 100644 index 0000000..a914bc8 --- /dev/null +++ b/11128.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int A[10]; +double AA[10]; + +struct Rec +{ + double y; + int x[3]; + bool operator<(const Rec &b) const + { + return y < b.y; + } +}; + +int solve() +{ + for (int i = 1; i <= 6; i++) + { + AA[i] = sqrt(A[i] + 0.0); + } + Rec r; + static Rec v[10000]; + int vn = 0; + for (r.x[0] = -9; r.x[0] <= 9; r.x[0]++) + { + for (r.x[1] = -9; r.x[1] <= 9; r.x[1]++) + { + for (r.x[2] = -9; r.x[2] <= 9; r.x[2]++) + { + r.y = AA[4] * r.x[0] + AA[5] * r.x[1] + AA[6] * r.x[2]; + v[vn++] = r; + } + } + } + sort(v, v + vn); + int x[10]; + for (x[0] = -9; x[0] <= 9; x[0]++) + for (x[1] = -9; x[1] <= 9; x[1]++) + for (x[2] = -9; x[2] <= 9; x[2]++) + for (x[3] = -9; x[3] <= 9; x[3]++) + { + double y0 = (x[0] + AA[1] * x[1] + AA[2] * x[2] + AA[3] * x[3]); + int left = 0, right = vn - 1; + while (right - left > 2) + { + int mid = (left + right) / 2; + if (v[mid].y > -y0) + { + right = mid; + } + else + { + left = mid; + } + } + left = max(0, left - 2); + right = min(vn - 1, right + 2); + for (int i = left; i <= right; i++) + { + if (fabs(y0 + v[i].y) < 1e-4) + { + if ((abs(x[0]) + abs(x[1]) + abs(x[2]) + abs(x[3]) + + abs(v[i].x[0]) + abs(v[i].x[1]) + abs(v[i].x[2])) == 0) + { + continue; + } + printf("%d %d %d %d %d %d %d\n", + x[0], x[1], x[2], x[3], + v[i].x[0], v[i].x[1], v[i].x[2]); + return true; + } + } + } + return false; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + for (int i = 1; i <= 6; i++) + { + scanf("%d", &A[i]); + } + printf("Case %d: ", cs); + if (!solve()) + { + printf("No Solution.\n"); + } + } +} diff --git a/11129.cpp b/11129.cpp new file mode 100644 index 0000000..322130f --- /dev/null +++ b/11129.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define MAXN 10010 + +int a[MAXN], b[MAXN]; +int n; + +void dvide(int x, int y) +{ + int i, j; + if (y - x <= 1) + { + return; + } + for (int i = x; i <= y; i++) + { + b[i] = a[i]; + } + for (i = x, j = x; j <= y; j += 2, i++) + { + a[i] = b[j]; + } + for (j = x + 1; j <= y; j += 2, i++) + { + a[i] = b[j]; + } + dvide((x + y) / 2 + 1, y); + dvide(x, (x + y) / 2); +} + +int main() +{ + while (scanf("%d", &n), n) + { + for (int i = 0; i < n; i++) + { + a[i] = i; + } + dvide(0, n - 1); + printf("%d:", n); + for (int i = 0; i < n; i++) + { + printf(" %d", a[i]); + } + printf("\n"); + } + return 0; +} diff --git a/1113.cpp b/1113.cpp new file mode 100644 index 0000000..340c6d1 --- /dev/null +++ b/1113.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define MAXCHAR 2 +#define MAXS (4096) +#define MAXNODE (1048576 << 2) +#pragma comment(linker, "/STACK:1024000000,1024000000") + +class Trie +{ +public: + struct Node + { + Node *next[MAXCHAR]; + int cnt; + void init() + { + cnt = 0; + memset(next, 0, sizeof(next)); + } + } nodes[MAXNODE]; + Node *root; + int size, cases; + Node *getNode() + { + Node *p = &nodes[size++]; + p->init(); + return p; + } + void init() + { + size = cases = 0; + root = getNode(); + } + inline int toIndex(char c) + { + return c == '.'; + } + // merge trie + void merge_dfs(Node *p, Node *q) + { + for (int i = 0; i < MAXCHAR; i++) + { + if (q->next[i]) + { + Node *u = p->next[i], *v = q->next[i]; + if (u == NULL) + p->next[i] = getNode(), u = p->next[i]; + u->cnt += v->cnt; + merge_dfs(u, v); + } + } + } + void merge(const Trie &other) + { + merge_dfs(root, other.root); + } + // basis operation + void insert(const char str[], int w) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + p->cnt += w; + } + int find(const char str[]) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + return p->cnt; + } + + void free() + { + return; + // owner memory pool version + queue Q; + Q.push(root); + Node *u; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < MAXCHAR; i++) + { + if (u->next[i] != NULL) + { + Q.push(u->next[i]); + } + } + delete u; + } + } +} tool; +char S[MAXS], T[MAXS], buf[MAXS]; +string morse[] = { + ".-", "-...", "-.-.", "-..", + ".", "..-.", "--.", "....", + "..", ".---", "-.-", ".-..", + "--", "-.", "---", ".--.", + "--.-", ".-.", "...", "-", + "..-", "...-", ".--", "-..-", + "-.--", "--.."}; +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + tool.init(); + + scanf("%s", S); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s", T); + int m = 0; + for (int j = 0; T[j]; j++) + for (int k = 0; morse[T[j] - 'A'][k]; k++) + buf[m++] = morse[T[j] - 'A'][k]; + buf[m] = '\0'; + tool.insert(buf, 1); + } + + int len = (int)strlen(S); + int dp[MAXS] = {}; + dp[0] = 1; + for (int i = 0; i < len; i++) + { + Trie::Node *p = tool.root; + for (int j = i; j < len; j++) + { + int u = tool.toIndex(S[j]); + if (p->next[u] == NULL) + break; + p = p->next[u]; + dp[j + 1] += dp[i] * p->cnt; + } + } + + printf("%d\n", dp[len]); + if (testcase) + puts(""); + + tool.free(); + } + return 0; +} +/* + + */ diff --git a/11130.cpp b/11130.cpp new file mode 100644 index 0000000..ab184d7 --- /dev/null +++ b/11130.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int a, b, v, A, t, colh, colv; + double vx, vy, sx, sy, radA; + scanf("%d %d %d %d %d", &a, &b, &v, &A, &t); + while (a != 0 || b != 0 || v != 0 || A != 0 || t != 0) + { + radA = 4 * A * atan(1) / 180; + vx = v * cos(radA); + vy = v * sin(radA); + sx = 0.5 * vx * t; + sy = 0.5 * vy * t; + colh = (sy + b / 2) / b; + colv = (sx + a / 2) / a; + printf("%d %d\n", colv, colh); + scanf("%d %d %d %d %d", &a, &b, &v, &A, &t); + } + return 0; +} diff --git a/11131.cpp b/11131.cpp new file mode 100644 index 0000000..f94706d --- /dev/null +++ b/11131.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +const int MAXSIZE = 5010; + +//ifstream fin("11131.in"); +//#define cin fin + +struct TreeNode +{ + char name[35]; + int cntChild; + int child[10]; +} tree[MAXSIZE]; + +char buffer[1000]; +int cntNode = 0; + +int findName(char *p) +{ + for (int i = 0; i < cntNode; i++) + if (!strcmp(p, tree[i].name)) + { + return i; + } + return -1; +} + +void init() +{ + for (int i = 0; i < MAXSIZE; i++) + { + tree[i].cntChild = 0; + for (int j = 0; j < 10; j++) + { + tree[i].child[j] = -1; + } + } +} + +void input() +{ + while (cin.getline(buffer, 1000)) + { + int parent; + char *cp = strtok(buffer, ","); + if (cp) + { + parent = findName(cp); + if (parent < 0) + { + strcpy(tree[cntNode].name, cp); + parent = cntNode; + cntNode++; + } + } + while ((cp = strtok(NULL, ",")) != NULL) + { + int son = findName(cp); + if (son < 0) + { + strcpy(tree[cntNode].name, cp); + son = cntNode; + cntNode++; + } + tree[parent].child[tree[parent].cntChild++] = son; + } + } +} + +void dfs_left_to_right(int node) +{ + if (tree[node].cntChild == 0) + { + cout << tree[node].name << endl; + } + else + { + for (int i = 0; i < tree[node].cntChild; i++) + { + dfs_left_to_right(tree[node].child[i]); + } + cout << tree[node].name << endl; + } +} + +void dfs_right_to_left(int node) +{ + if (tree[node].cntChild == 0) + { + cout << tree[node].name << endl; + } + else + { + for (int i = tree[node].cntChild - 1; i >= 0; i--) + { + dfs_right_to_left(tree[node].child[i]); + } + cout << tree[node].name << endl; + } +} + +void solve() +{ + cout << 2 << endl + << endl; + dfs_left_to_right(0); + cout << endl; + dfs_right_to_left(0); + cout << endl; +} + +int main() +{ + init(); + input(); + solve(); + return 0; +} diff --git a/11132.cpp b/11132.cpp new file mode 100644 index 0000000..13e6439 --- /dev/null +++ b/11132.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int num, side; +char buffer[1010], ch; + +int number(int a, int b) +{ + int i, j; + int sum; + sum = 0; + for (i = b, j = 0; i >= a; i--, j++) + { + if (buffer[i] == 'T') + { + sum += (int)pow(2.0, (double)j); + } + } + return sum; +} + +int main() +{ + while (cin >> num >> ch >> side && num) + { + cin >> buffer; + int len = (int)ceil(log(side * 1.0) / log(2.0)); + if (side == 1) + { + if (strlen(buffer) >= num) + { + cout << num << endl; + } + else + { + cout << -1 << endl; + } + continue; + } + int cnt = 0; + int sum = 0; + bool success = false; + for (int i = 0; i < strlen(buffer) - len + 1; i++) + { + int value = number(i, i + len - 1) + 1; + if (value <= side) + { + cnt++; + sum += value; + } + i = i + len - 1; + if (cnt == num) + { + success = true; + break; + } + } + if (success) + { + cout << sum << endl; + } + else + { + cout << -1 << endl; + } + } + return 0; +} diff --git a/11133.cpp b/11133.cpp new file mode 100644 index 0000000..ee5a8cb --- /dev/null +++ b/11133.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int E[45][45] = {0}; +int main() +{ + int i, j, k; + for (i = 0; i < 45; i++) + { + E[i][i] = 1; + } + for (i = 0; i < 45; i++) + { + for (j = i + 1; j < 45; j++) + { + for (k = i; k < j; k++) + { + if (j % (j - k) == 0) + { + E[i][j] += E[i][k]; + } + } + } + } + while (scanf("%d%d", &i, &j) == 2) + { + if ((i | j) == 0) + { + break; + } + printf("%d %d %d\n", i, j, E[i][j]); + } + return 0; +} diff --git a/11134.cpp b/11134.cpp new file mode 100644 index 0000000..8b6223e --- /dev/null +++ b/11134.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +// greedy + +struct interval +{ + int low, high; + int num; // rook number + int rook;// position within interval rook should take +}; + +inline int intervalCompare(const void *r1, const void *r2) +{ + // compare intervals by high end + const interval *x1 = static_cast(r1); + const interval *x2 = static_cast(r2); + return x1->high - x2->high; +} + +inline int numCompare(const void *r1, const void *r2) +{ + // compare by num field + const interval *x1 = static_cast(r1); + const interval *x2 = static_cast(r2); + return x1->num - x2->num; +} + +int main() +{ + int n; + cin >> n; + while (n) + { + // Read the input into two arrays of intervals (the two dimensions + // of the problem are totally independent of each other). + interval LR[n];// stores left and right ends of rectangles + interval TB[n];// stores top and bottom ends of rectangles + for (int i = 0; i < n; i++) + { + cin >> LR[i].low; + cin >> TB[i].low; + cin >> LR[i].high; + cin >> TB[i].high; + LR[i].num = i; + TB[i].num = i; + LR[i].rook = 0; + TB[i].rook = 0; + } + bool fail = 0; + // First, compute y coordinates of rooks greedily: put each + // rook in topmost unused row inside its rectangle + qsort(TB, sizeof(TB) / sizeof(TB[0]), sizeof(TB[0]), intervalCompare); + int usedrow[5005] = {0}; + for (int i = 0; i < n; i++) + { + for (int j = TB[i].low; j <= TB[i].high; j++) + if (!usedrow[j])// could use fancy data structure to make this + { + // linear search go faster, but why bother? + TB[i].rook = j; + usedrow[j] = 1; + j = n + 1; + } + if (!TB[i].rook) + { + fail = 1; + i = n + 1; + } + } + // Next, do exactly the same thing to compute x coordinates. + qsort(LR, sizeof(LR) / sizeof(LR[0]), sizeof(LR[0]), intervalCompare); + int usedcol[5005] = {0}; + for (int i = 0; i < n; i++) + { + for (int j = LR[i].low; j <= LR[i].high; j++) + if (!usedcol[j])// could use fancy data structure to make this + { + // linear search go faster, but why bother? + LR[i].rook = j; + usedcol[j] = 1; + j = n + 1; + } + if (!LR[i].rook) + { + fail = 1; + i = n + 1; + } + } + // Produce output + if (fail) + { + cout << "IMPOSSIBLE\n"; + } + else + { + // unsort the arrays so that the outputs come in right order + qsort(LR, sizeof(LR) / sizeof(LR[0]), sizeof(LR[0]), numCompare); + qsort(TB, sizeof(TB) / sizeof(TB[0]), sizeof(TB[0]), numCompare); + for (int i = 0; i < n; i++) + { + cout << LR[i].rook << " " << TB[i].rook << "\n"; + } + } + cin >> n; + } + return 0; +} diff --git a/11135.cpp b/11135.cpp new file mode 100644 index 0000000..969614a --- /dev/null +++ b/11135.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +double Hypot(double dx, double dy) +{ + return sqrt(dx * dx + dy * dy); +} + +int main() +{ + double D, L, xs, ys, xt, yt; + double EPS = 1e-8; + while (scanf("%lf %lf %lf %lf %lf %lf", &D, &L, &xs, &ys, &xt, &yt) == 6) + { + if (D < EPS) + { + break; + } + int row = (int)floor(ys / D), col = (int)floor(xs / D); + ys -= row * D; + xs -= col * D; + yt -= row * D; + xt -= col * D; + xs -= D / 2; + ys -= D / 2; + xt -= D / 2; + yt -= D / 2; + if (yt < ys) + { + ys = -ys; + yt = -yt; + } + if (xt < xs) + { + xs = -xs; + xt = -xt; + } + //if (yt-ys < xt-xs) { swap(xs, ys); swap(xt, yt); } + xs += D / 2; + ys += D / 2; + xt += D / 2; + yt += D / 2; + row = (int)floor(yt / D); + col = (int)floor(xt / D); + assert(0 <= row && row <= 305); + assert(0 <= col && col <= 305); + static double dp[512][512]; + for (int r = row; r >= 1; r--) + { + for (int c = col; c >= 1; c--) + { + double y1 = r * D + L; + double x1 = c * D + L; + int d = min(row - r, col - c); + if (r == row || c == col) + { + dp[r][c] = Hypot( + fabs(x1 - xt) - (col - c) * 2 * L, + fabs(y1 - yt) - (row - r) * 2 * L); + } + else + { + dp[r][c] = 1e99; + for (int i = 0; min(row - (r + i), col - c) == d; i++) + { + dp[r][c] = MIN(dp[r][c], Hypot(D - 2 * L, (i + 1) * (D - 2 * L)) + dp[r + i + 1][c + 1]); + } + for (int i = 0; min(row - r, col - (c + i)) == d; i++) + { + dp[r][c] = MIN(dp[r][c], Hypot(D - 2 * L, (i + 1) * (D - 2 * L)) + dp[r + 1][c + i + 1]); + } + } + } + } + double swim = min(row, col) * sqrt(2.0) * 2 * L + abs(row - col) * 2 * L; + double walk = 1e9; + if (row == 0 && col == 0) + { + walk = Hypot(xs - xt, ys - yt); + } + for (int r = 0; r < row; r++) + { + for (int c = 0; (r == 0 || c == 0) && c < col; c++) + { + double y1 = r * D + D - L; + double x1 = c * D + D - L; + walk = MIN(walk, Hypot(fabs(xs - x1) - c * 2 * L, fabs(ys - y1) - r * 2 * L) + dp[r + 1][c + 1]); + } + } + if (walk < EPS) + { + walk = 0; + } + if (swim < EPS) + { + swim = 0; + } + printf("The gopher has to swim %.2f meters and walk %.2f meters.\n", swim, walk); + } +} diff --git a/11136.cpp b/11136.cpp index 5a262c1..c8b8f2b 100644 --- a/11136.cpp +++ b/11136.cpp @@ -1,66 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -template -typename ReverseIterator::iterator_type make_forward(ReverseIterator rit){ - return --rit.base(); -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int64 N; - scanf("%lld", &N); - if (N == 0) +int main() +{ + int n, k, bill; + unsigned long long result; + multiset bills; + multiset::iterator it1; + multiset::reverse_iterator it2; + while (cin >> n) + { + if (n == 0) + { break; - multiset< int64 > mset; - multiset< int64 >::iterator mit, rit; - int64 T = 0; - while (N--){ - int64 K; - scanf("%lld", &K); - while (K--){ - int64 I; - scanf("%lld", &I); - mset.insert(I); + } + bills.clear(); + result = 0; + for (int i = 0; i < n; i++) + { + cin >> k; + while (k--) + { + cin >> bill; + bills.insert(bill); } - mit = mset.begin(); - rit = make_forward( mset.rbegin() ); - T += (*rit - *mit); - mset.erase(mit); - mset.erase(rit); + it1 = bills.begin(); + it2 = bills.rbegin(); + bill = *it2; + result += *it2 - *it1; + bills.erase(it1); + it1 = bills.find(bill); + bills.erase(it1); } - printf("%lld\n", T); + cout << result << endl; } return 0; } - diff --git a/11137.cpp b/11137.cpp index d76cb54..2948751 100644 --- a/11137.cpp +++ b/11137.cpp @@ -1,24 +1,27 @@ -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; - -int main(){ - int N, i, j; - int coin[21]={1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859, 8000, 9261}; - uint64 ways[10000]; - ways[0] = 1; - for(i=0; i<21; i++){ - int c = coin[i]; - for(j=c; j<10000; j++) - ways[j]+=ways[j-c]; - } - int in; - while(cin>>in) - cout< + +using namespace std; + +int coin[25]; +long long dp[10010]; + +int main() +{ + int i, j, n; + for (i = 0; i < 21; i++) + { + coin[i] = (i + 1) * (i + 1) * (i + 1); + } + memset(dp, 0, sizeof(dp)); + dp[0] = 1; + for (i = 0; i < 21; i++) + for (j = 0; j + coin[i] <= 10000; j++) + { + dp[j + coin[i]] += dp[j]; + } + while (cin >> n) + { + cout << dp[n] << endl; + } + return 0; +} diff --git a/11138.cpp b/11138.cpp new file mode 100644 index 0000000..624e92c --- /dev/null +++ b/11138.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +int M[500][500], L[500], U[500], V[500], qU[500], qV[500], ptr, ulen, vlen, prU[500], prV[500]; +int main() +{ + int t, tt, i, j, k, r, c, flow, n, ok; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d%d", &r, &c); + for (i = 0; i < r; i++) + { + L[i] = 0; + for (j = 0; j < c; j++) + { + scanf("%d", &ok); + if (ok) + { + M[i][L[i]++] = j; + } + } + } + for (i = 0; i < r; i++) + { + U[i] = -1; + } + for (i = 0; i < c; i++) + { + V[i] = -1; + } + for (n = flow = 0; n < r; n++) + { + for (i = 0; i < r; i++) + { + prU[i] = -1; + } + for (i = 0; i < c; i++) + { + prV[i] = -1; + } + qU[0] = n, prU[n] = -2, ulen = 1; + while (ulen) + { + for (ptr = vlen = ok = 0; ptr < ulen; ptr++) + { + i = qU[ptr]; + for (k = 0; k < L[i]; k++) + { + j = M[i][k]; + if (prV[j] != -1) + { + continue; + } + prV[qV[vlen++] = j] = i; + if (V[j] == -1) + { + ok = 1; + break; + } + } + if (ok) + { + break; + } + } + if (ok) + { + break; + } + for (ptr = ulen = 0; ptr < vlen; ptr++) + { + i = qV[ptr]; + j = V[i]; + prU[qU[ulen++] = j] = i; + } + } + if (!ok) + { + continue; + } + flow++; + while (j != -2) + { + i = prV[j]; + V[j] = i; + U[i] = j; + j = prU[i]; + } + } + printf("Case %d: a maximum of %d nuts and bolts can be fitted together\n", t, flow); + } + return 0; +} diff --git a/11139.cpp b/11139.cpp new file mode 100644 index 0000000..1522c0d --- /dev/null +++ b/11139.cpp @@ -0,0 +1,255 @@ +#include + +using namespace std; + +#if 0 + +int gcd[128][128]; + +// Returns number of lattice points on the boundary of given triangle +// The triangle should have a non-zero area. +int Boundary(int x1, int y1, int x2, int y2, int x3, int y3) +{ + int B = gcd[abs(x2-x1)][abs(y2-y1)]; + B += gcd[abs(x3-x1)][abs(y3-y1)]; + B += gcd[abs(x3-x2)][abs(y3-y2)]; + return B; +} + +// Returns doubled signed area of the lattice triangle +int Area2(int x1, int y1, int x2, int y2, int x3, int y3) +{ + return ((x2-x1)*(y3-y1) - (x3-x1)*(y2-y1)); +} + +// Returns number of lattice points inside given triangle +int Internal(int x1, int y1, int x2, int y2, int x3, int y3) +{ + // Pick's theorem: A = I + B/2 - 1, or: I = (2A - B + 2) / 2 + int res = (abs(Area2(x1,y1,x2,y2,x3,y3)) - Boundary(x1,y1,x2,y2,x3,y3) + 2) / 2; + return res; +} + +long long C(int n, int k) +{ + if(k == 0) + { + return 1; + } + if(k > n) + { + return 0; + } + return C(n, k-1) * (n-k+1) / k; +} + +int main() +{ + for(int n = 0; n < 128; n++) + { + for(int m = 0; m < 128; m++) + { + if(n == 0 || m == 0) + { + gcd[n][m] = (n == 0 ? m : n); + } + else if(m < n) + { + gcd[n][m] = gcd[m][n]; + } + else + { + gcd[n][m] = gcd[m%n][n]; + } + } + } + for(int n = 1; n <= 120; n++) + { + // Base: (n+1)^2 choose 4 + long long res = C((n+1)*(n+1), 4); + // Remove quads in which all 4 points lie on the same line + for(int dy = 0; dy <= n; dy++) + { + for(int dx = -n; dx <= n; dx++) + { + if(dy == 0 && dx <= 0) + { + continue; + } + long long c = (n+1-dy)*(n+1-abs(dx)); + res -= c * C(gcd[dy][abs(dx)]-1, 2); + } + } + for(int y1 = 0, x1 = 0; x1 <= n; x1++) + for(int x2 = 0; x2 <= n; x2++) + for(int x3 = 0; x3 <= n; x3++) + { + // Thanks to this constraint, the body of this loop is + // executed only O(n^2) times. + if(x1 != 0 && x2 != 0 && x3 != 0) + { + continue; + } + for(int y2 = y1; y2 <= n; y2++) + { + if(y1 == y2 && x1 >= x2) + { + continue; + } + for(int y3 = y2; y3 <= n; y3++) + { + if(y2 == y3 && x2 >= x3) + { + continue; + } + //The following should be already true + //if (min(x1, min(x2, x3)) != 0) continue; + //if (min(y1, min(y2, y3)) != 0) continue; + if(Area2(x1,y1,x2,y2,x3,y3) == 0) + { + continue; + } + int w = max(x1, max(x2, x3)); + int h = max(y1, max(y2, y3)); + long long c = (n+1LL-w)*(n+1LL-h); + res -= c * (Boundary(x1,y1,x2,y2,x3,y3)-3); + res += c * Internal(x1,y1,x2,y2,x3,y3) * (-1 + 3); + } + } + } + printf("/*%d*/ \"%lld\",\n", n, res); + fflush(stdout); + } +} + +#endif + +int main() +{ + char *ans[] = {"0", + /*1*/ "1", + /*2*/ "94", + /*3*/ "1758", + /*4*/ "13698", + /*5*/ "70539", + /*6*/ "266320", + /*7*/ "837060", + /*8*/ "2260984", + /*9*/ "5460657", + /*10*/ "12046294", + /*11*/ "24775158", + /*12*/ "47847538", + /*13*/ "87967579", + /*14*/ "154764912", + /*15*/ "262110940", + /*16*/ "429480216", + /*17*/ "684015621", + /*18*/ "1061192670", + /*19*/ "1609449754", + /*20*/ "2390049498", + /*21*/ "3483126407", + /*22*/ "4990472864", + /*23*/ "7041050760", + /*24*/ "9791130320", + /*25*/ "13438691713", + /*26*/ "18223150622", + /*27*/ "24435137362", + /*28*/ "32422957058", + /*29*/ "42610456831", + /*30*/ "55488070008", + /*31*/ "71653253880", + /*32*/ "91793056040", + /*33*/ "116718542113", + /*34*/ "147374255430", + /*35*/ "184850702510", + /*36*/ "230399012010", + /*37*/ "285487491835", + /*38*/ "351770674200", + /*39*/ "431140718212", + /*40*/ "525737274208", + /*41*/ "638028826789", + /*42*/ "770742660630", + /*43*/ "927021702914", + /*44*/ "1110333974314", + /*45*/ "1324584379067", + /*46*/ "1574168092008", + /*47*/ "1863985132716", + /*48*/ "2199400376080", + /*49*/ "2586484878529", + /*50*/ "3031882471694", + /*51*/ "3542983783278", + /*52*/ "4127906237474", + /*53*/ "4795667546635", + /*54*/ "5556032576528", + /*55*/ "6419836537428", + /*56*/ "7398860557112", + /*57*/ "8506094916941", + /*58*/ "9755681626382", + /*59*/ "11163074675546", + /*60*/ "12744831674354", + /*61*/ "14519383177831", + /*62*/ "16506465753352", + /*63*/ "18727437208180", + /*64*/ "21205528017584", + /*65*/ "23965877539197", + /*66*/ "27035496497750", + /*67*/ "30443854956002", + /*68*/ "34222324718274", + /*69*/ "38404887184703", + /*70*/ "43027849371592", + /*71*/ "48130693448624", + /*72*/ "53755057174408", + /*73*/ "59946428484041", + /*74*/ "66752984294358", + /*75*/ "74226200735918", + /*76*/ "82421333732930", + /*77*/ "91397394819691", + /*78*/ "101217116203904", + /*79*/ "111948024726748", + /*80*/ "123661057327752", + /*81*/ "136432577125309", + /*82*/ "150343660737614", + /*83*/ "165480557290802", + /*84*/ "181933824163210", + /*85*/ "199800966901783", + /*86*/ "219184984267936", + /*87*/ "240194858791736", + /*88*/ "262945718824768", + /*89*/ "287560501305681", + /*90*/ "314167401731182", + /*91*/ "342903811014758", + /*92*/ "373914064115850", + /*93*/ "407350852787219", + /*94*/ "443375227121768", + /*95*/ "482156764012932", + /*96*/ "523873558838176", + /*97*/ "568715409359421", + /*98*/ "616880048353382", + /*99*/ "668576152839750", + /*100*/ "724023049675018", + /*101*/ "783452989688499", + /*102*/ "847107440973432", + /*103*/ "915242542968876", + /*104*/ "988124570367008", + /*105*/ "1066033809345013", + /*106*/ "1149265943085894", + /*107*/ "1238130202341666", + /*108*/ "1332947863674570", + /*109*/ "1434059888780911", + /*110*/ "1541819141887696", + /*111*/ "1656597872383320", + /*112*/ "1778782903999392", + /*113*/ "1908782535144553", + /*114*/ "2047019271291934", + /*115*/ "2193937426760166", + /*116*/ "2349999850474066", + /*117*/ "2515689641198367", + /*118*/ "2691512617349032", + /*119*/ "2877994834324016", + /*120*/ "3075682094608520"}; + int n; + while (scanf("%d", &n) == 1 && n > 0) + { + printf("%d %s\n", n, ans[n]); + } +} diff --git a/11140.cpp b/11140.cpp new file mode 100644 index 0000000..060e510 --- /dev/null +++ b/11140.cpp @@ -0,0 +1,212 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +char field[50][50]; +char piece[50][50]; + +bool is_empty_piece(int n, int m) +{ + int count = 0; + rep(i, n) + { + rep(j, m) + { + if (piece[i][j] == '*') + { + count++; + } + } + } + return count == 0; +} + +bool is_empty_row(int row, int n, int m) +{ + rep(i, m) + { + if (piece[row][i] == '*') + { + return false; + } + } + return true; +} + +bool is_empty_column(int column, int n, int m) +{ + rep(i, n) + { + if (piece[i][column] == '*') + { + return false; + } + } + return true; +} + +void skip_rows(int skip_count, int n, int m) +{ + REP(i, skip_count, n) + { + rep(j, m) + { + piece[i - skip_count][j] = piece[i][j]; + } + } +} + +void skip_columns(int skip_count, int n, int m) +{ + REP(i, skip_count, m) + { + rep(j, n) + { + piece[j][i - skip_count] = piece[j][i]; + } + } +} + +void adjust_piece(int &n, int &m) +{ + int skip_count = 0; + rep(i, n) + { + if (is_empty_row(i, n, m)) + { + skip_count++; + } + else + { + break; + } + } + skip_rows(skip_count, n, m); + n -= skip_count; + skip_count = 0; + rep(i, m) + { + if (is_empty_column(i, n, m)) + { + skip_count++; + } + else + { + break; + } + } + skip_columns(skip_count, n, m); + m -= skip_count; + skip_count = 0; + for (int i = n - 1; i >= 0; i--) + { + if (is_empty_row(i, n, m)) + { + skip_count++; + } + else + { + break; + } + } + n -= skip_count; + skip_count = 0; + for (int i = m - 1; i >= 0; i--) + { + if (is_empty_column(i, n, m)) + { + skip_count++; + } + else + { + break; + } + } + m -= skip_count; +} + +bool try_to_fit(int row, int col, int n, int m, int N, int M) +{ + for (int i = 0; i < n; i++) + { + if (row + i >= N) + { + return false; + } + for (int j = 0; j < m; j++) + { + if (col + j >= M) + { + return false; + } + if (piece[i][j] == '*' && field[row + i][col + j] != '*') + { + return false; + } + } + } + return true; +} + +int main() +{ + int t, N, M, s, n, m; + string input; + cin >> t; + while (t--) + { + cin >> N >> M >> s; + cin.ignore(100, '\n'); + rep(i, N) + { + getline(cin, input); + rep(j, M) + { + field[i][j] = input[j]; + } + } + while (s--) + { + cin >> n >> m; + cin.ignore(100, '\n'); + rep(i, n) + { + getline(cin, input); + rep(j, m) + { + piece[i][j] = input[j]; + } + } + if (is_empty_piece(n, m)) + { + cout << "Yes" << endl; + continue; + } + adjust_piece(n, m); + bool result = false; + rep(i, N) + { + rep(j, M) + { + if (try_to_fit(i, j, n, m, N, M)) + { + result = true; + } + } + } + if (result) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/11141.cpp b/11141.cpp new file mode 100644 index 0000000..41f28cd --- /dev/null +++ b/11141.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define mini(a, b) ((a) < (b) ? (a) : (b)) +#define maxi(a, b) ((a) > (b) ? (a) : (b)) +const int MaxN = 200; +int t, n, pow2[13]; +vector> edges[MaxN]; +int a[MaxN][6]; +int ans[4096], tans[4096]; + +bool Findd(vector> &vt, pair &pr) +{ + for (int i = 0; i < vt.size(); i++) + if (vt[i] == pr) + { + return true; + } + return false; +} +int tool(int a) +{ + int res = 0; + while (a) + { + a = a / 2; + res++; + } + return res; +} +bool iscorrect(int a) +{ + for (int i = 0; i < 12; i += 2) + if ((a & pow2[i]) && (a & pow2[i + 1])) + { + return false; + } + return true; +} +bool jaygah(int a, int jaygah) +{ + return a & pow2[jaygah]; +} +int makenew(int prime, int fs, int sc) +{ + if (fs == sc) + { + if (jaygah(prime, fs * 2)) + { + return prime; + } + return prime | pow2[fs * 2 + 1]; + } + if (!jaygah(prime, fs * 2)) + { + prime += pow2[fs * 2]; + } + if (jaygah(prime, fs * 2 + 1)) + { + prime -= pow2[fs * 2 + 1]; + } + if (!jaygah(prime, sc * 2) && !jaygah(prime, sc * 2 + 1)) + { + prime += pow2[sc * 2 + 1]; + } + return prime; +} +void chapcon(int a) +{ + while (a) + { + cout << a % 2; + a = a / 2; + } + return; +} +void sumer(int dcnm, int edgnm) +{ + int fs = edges[dcnm][edgnm].first, sc = edges[dcnm][edgnm].second; + if (fs == sc) + { + for (int i = 0; i < pow2[12]; i++) + if (iscorrect(i) && tool(i % pow2[fs * 2 + 2]) % 2 == 0) + { + tans[makenew(i, fs, sc)] = (ans[i] + tans[makenew(i, fs, sc)]) % 1000000000; + /*if(ans[i]!=0) + {cout<> t; + for (int cas = 0; cas < t; cas++) + { + cin >> n; + for (int i = 0; i < n; i++) + { + edges[i].clear(); + //scanf("%d%d%d%d%d%d",a[i],a[i]+1,a[i]+2,a[i]+3,a[i]+4,a[i]+5); + cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3] >> a[i][4] >> a[i][5]; + for (int j = 0; j < 6; j++) + { + a[i][j]--; + } + for (int j = 0; j < 3; j++) + { + int ta = mini(a[i][j], a[i][5 - j]), tb = maxi(a[i][j], a[i][5 - j]); + pair bp = make_pair(ta, tb); + if (!Findd(edges[i], bp)) + { + edges[i].push_back(bp); + } + } + } + memset(ans, 0, sizeof ans); + memset(tans, 0, sizeof tans); + tans[0] = 1; + ans[0] = 1; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < edges[i].size(); j++) + { + sumer(i, j); + } + memcpy(ans, tans, sizeof tans); + }//n dynamic + int sum = 0; + for (int i = 0; i < 4096; i++) + { + sum = (sum + ans[i]) % 1000000000; + } + cout << sum - 1 << endl; + }//case reader; + return 0; +} diff --git a/11142.cpp b/11142.cpp new file mode 100644 index 0000000..478b8ba --- /dev/null +++ b/11142.cpp @@ -0,0 +1,217 @@ +#include + +using namespace std; + +#define swap(a, b) (a) ^= (b) ^= (a) ^= (b) +int m, n, c, t, cnt, rem[11][11], order[35][2], em[11][11], hadi; +char grid[11][11]; +bool forced[35], arr[35], found; +bool prob[35][2]; +bool isvalid(int x, int y) +{ + return x >= 0 && x < m && y >= 0 && y < n; +} +bool check(int x, int y) +{ + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (isvalid(x + i, y + j) && rem[x + i][y + j] == 0 && isdigit(grid[x + i][y + j])) + { + return false; + } + return true; +} +bool bt(int s, int consider, int state) +{ + //cout << s << " " << c << " " << cnt << endl; + if (c == 0 && s == cnt) + { + for (int i = 0; i < cnt; i++) + { + prob[i][1] = prob[i][1] || arr[i]; + } + return true; + } + if (s == cnt) + { + return false; + } + if (c > cnt - s) + { + return false; + } + int x = order[s][0], y = order[s][1]; + bool valid = true; + bool canBeEmpty = true; + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (isvalid(i + x, y + j) && isdigit(grid[x + i][y + j])) + { + em[x + i][y + j]--; + if (em[x + i][y + j] < rem[x + i][y + j]) + { + canBeEmpty = false; + } + if (em[x + i][y + j] == 0 && rem[x + i][y + j] > 1) + { + valid = false; + } + if (em[x + i][y + j] < 0) + { + canBeEmpty = valid = false; + } + } + bool result = false; + if (c == 0) + { + valid = false; + } + c--; + arr[s] = true; + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (isvalid(i + x, j + y) && isdigit(grid[i + x][j + y])) + { + rem[i + x][j + y]--; + if (rem[i + x][j + y] < 0) + { + valid = false; + } + } + if (valid && !(consider == s && state == 0)) + if (!result) + { + result = result || bt(s + 1, consider, state); + } + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (isvalid(i + x, j + y) && isdigit(grid[i + x][j + y])) + { + rem[i + x][j + y]++; + } + arr[s] = false; + c++; + if (canBeEmpty && !(consider == s && state == 1)) + { + result = result || bt(s + 1, consider, state); + } + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (isvalid(i + x, j + y) && isdigit(grid[x + i][y + j])) + { + em[x + i][y + j]++; + } + return result; +} +int main() +{ + cin >> t; + for (int I = 1; I <= t; I++) + { + // cerr << "Doing " << I << endl; + cin >> m >> n >> c; + memset(grid, 0, sizeof grid); + for (int i = 0; i < m; i++) + { + cin >> grid[i]; + } + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + if (grid[i][j] == 'E') + { + grid[i][j] = '0'; + } + cnt = 0; + hadi = 0; + memset(em, 0, sizeof em); + memset(arr, 0, sizeof arr); + memset(forced, 0, sizeof forced); + memset(order, 0, sizeof order); + memset(rem, 0, sizeof rem); + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + if (isdigit(grid[i][j])) + { + rem[i][j] = grid[i][j] - '0'; + for (int x = -1; x <= 1; x++) + for (int y = -1; y <= 1; y++) + if (isvalid(i + x, j + y)) + { + rem[i][j] -= (grid[i + x][j + y] == 'X') ? 1 : 0; + em[i][j] += (grid[i + x][j + y] == '.') ? 1 : 0; + } + //cout << rem[i][j] << " " << em[i][j] << endl; + } + else if (grid[i][j] == '.') + { + order[cnt][0] = i, order[cnt][1] = j; + cnt++; + } + if (cnt > 35) + { + cout << "Sedaye Bozghool" << endl; + } + //cout << cnt << endl; + // a. Do a good ordering of cells + // b. Do backtrack + memset(forced, 0, sizeof forced); + memset(arr, 0, sizeof arr); + memset(prob, 0, sizeof prob); + bool hadi = true; + for (int i = 0; i < cnt; i++) + { + //cerr << i << endl; + if (prob[i][0] == 0 && !check(order[i][0], order[i][1])) + { + prob[i][0] = true; + } + if (prob[i][0] == 0) + { + prob[i][0] = bt(0, i, 0); + } + if (prob[i][1] == 0 && check(order[i][0], order[i][1])) + { + prob[i][1] = bt(0, i, 1); + } + if (prob[i][1] && !prob[i][0]) + { + forced[i] = true; + } + if (prob[i][1] == 0 && prob[i][0] == 0) + { + hadi = false; + } + /* if(prob[i][0] == 0) + prob[i][0] = bt(0, i, 0); + if(prob[i][1] == 0) + prob[i][1] = bt(0, i, 1); + if(prob[i][1] && !prob[i][0]) + forced[i] = true;*/ + //cout << i << " : " << (int)prob[i][0] << " " << (int)prob[i][1] << endl; + } + if (!hadi) + { + cerr << "Sedaye Shombool az " << I << endl; + } + //cout << hadi << endl; + // c. Update grid + for (int i = 0; i < cnt; i++) + if (forced[i]) + { + grid[order[i][0]][order[i][1]] = 'X'; + } + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + if (grid[i][j] == '0') + { + grid[i][j] = 'E'; + } + // d. Print Output + cout << "Case #" << I << ":" << endl; + for (int i = 0; i < m; i++) + { + cout << grid[i] << endl; + } + } + return 0; +} diff --git a/11144.cpp b/11144.cpp new file mode 100644 index 0000000..2c450a8 --- /dev/null +++ b/11144.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define EPS 1e-8 +#define inf 1e99 +double dist(double ax, double ay, double bx, double by) +{ + return sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); +} + +double distToLine( + double ax, double ay, + double bx, double by, + double px, double py, + double *cpx, double *cpy) +{ + //Formula: cp = a + (p-a).(b-a) / |b-a| * (b-a) + double proj = ((px - ax) * (bx - ax) + (py - ay) * (by - ay)) / + ((bx - ax) * (bx - ax) + (by - ay) * (by - ay)); + *cpx = ax + proj * (bx - ax); + *cpy = ay + proj * (by - ay); + return dist(px, py, *cpx, *cpy); +} + +double distToLineSegment( + double ax, double ay, + double bx, double by, + double px, double py, + double *cpx, double *cpy) +{ + if ((bx - ax) * (px - ax) + (by - ay) * (py - ay) < EPS) + { + *cpx = ax; + *cpy = ay; + return dist(ax, ay, px, py); + } + if ((ax - bx) * (px - bx) + (ay - by) * (py - by) < EPS) + { + *cpx = bx; + *cpy = by; + return dist(bx, by, px, py); + } + return distToLine(ax, ay, bx, by, px, py, cpx, cpy); +} + +double polygonArea(int n, double x[], double y[]) +{ + double a = 0; + for (int i = 1; i <= n; i++) + { + a += (x[i - 1] * y[i % n] - x[i % n] * y[i - 1]); + } + return fabs(a / 2); +} + +double PolygonDistance(int n1, double x1[], double y1[], int n2, double x2[], double y2[]) +{ + double result = 1e99; + for (int i = 0; i < n1; i++) + for (int j = 0; j < n2; j++) + { + double a, b; + result = min(result, distToLineSegment(x2[j], y2[j], x2[(j + 1) % n2], y2[(j + 1) % n2], x1[i], y1[i], &a, &b)); + result = min(result, distToLineSegment(x1[i], y1[i], x1[(i + 1) % n1], y1[(i + 1) % n1], x2[j], y2[j], &a, &b)); + } + return result; +} + +int main() +{ + ofstream fout1("test.in"); + double PolygonX[101][101]; + double PolygonY[101][101]; + double Areas[101]; + bool Graph[101][101]; + int count[101]; + int m, p, c, N; + cin >> N; + for (; N--;) + { + cin >> m; + if (m > 100) + { + cout << "Oh my god" << endl; + } + for (int i = 0; i < m; i++) + { + cin >> count[i]; + if (count[i] > 33) + { + cout << "What?!" << endl; + } + for (int j = 0; j < count[i]; j++) + { + cin >> PolygonX[i][j] >> PolygonY[i][j]; + } + } + cin >> p >> c; + //cout << "Read Input" << endl; + memset(Graph, 0, sizeof Graph); + for (int i = 0; i < m; i++) + for (int j = 0; j < m; j++) + { + Graph[i][j] = (PolygonDistance(count[i], PolygonX[i], PolygonY[i], count[j], PolygonX[j], PolygonY[j]) < 30.0); + } + //cout << "Graph made" << endl; + for (int i = 0; i < m; i++) + { + Areas[i] = polygonArea(count[i], PolygonX[i], PolygonY[i]); + } + //cout << "Areas calculated" << endl; + bool Fired[102]; + int FireTime[102]; + for (int i = 0; i < m; i++) + { + FireTime[i] = 400; + } + memset(Fired, 0, sizeof Fired); + Fired[p] = true; + FireTime[p] = 0; + for (int I = 0; I < m; I++) + { + //cout << "Step " << I << endl; + //cout << m << endl; + for (int i = 0; i < m; i++) + for (int j = 0; j < m; j++) + { + //cout << i << " " << j << endl; + if (Graph[i][j]) + { + FireTime[j] = min(FireTime[j], FireTime[i] + (int)(ceil(10.0 * Areas[j] / Areas[i]) + eps)); + } + } + } + //cout << "Finalizing" << endl; + for (int i = 0; i < m; i++) + { + // cerr << i << " " << Areas[i] << " " < + +using namespace std; + +//ifstream fin("f.in"); +//ofstream fout("f.out"); +#define fin cin +#define fout cout + +struct Edge +{ + int h, t; + double c; + + Edge(int h = -1, int t = -1, double c = -1.0) : h(h), t(t), c(c) {} + + bool operator()(const Edge &a, const Edge &b) const + { + return a.c > b.c; + } +}; + +int n, m; +vector mark; +vector e; +vector> adj; + +bool dfs(int s, int t) +{ + if (s == t) + { + return true; + } + mark[s] = true; + for (int i = 0; i < (int)adj[s].size(); i++) + if (!mark[adj[s][i]] && dfs(adj[s][i], t)) + { + return true; + } + return false; +} + +main() +{ + fout.precision(4); + fout.setf(ios::fixed | ios::showpoint); + int tn; + for (fin >> tn; tn--;) + { + fin >> n >> m; + e = vector(m, Edge()); + adj = vector>(n, vector()); + for (int i = 0; i < m; i++) + { + int h, t; + double c; + fin >> h >> t >> c; + h--; + t--; + e[i] = Edge(h, t, c); + adj[h].push_back(t); + } + sort(e.begin(), e.end(), Edge()); + double ans = 0; + for (int i = 0; i < (int)e.size(); i++) + { + adj[e[i].h].erase(find(adj[e[i].h].begin(), adj[e[i].h].end(), e[i].t)); + mark = vector(n, false); + if (!dfs(e[i].h, e[i].t)) + { + adj[e[i].h].push_back(e[i].t); + adj[e[i].t].push_back(e[i].h); + } + else + { + adj[e[i].h].push_back(e[i].t); + ans += e[i].c; + } + } + fout << ans << endl; + } + return 0; +} diff --git a/11146.cpp b/11146.cpp new file mode 100644 index 0000000..f88aed3 --- /dev/null +++ b/11146.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define mini(a, b) ((a) < (b) ? (a) : (b)) +#define maxi(a, b) ((a) > (b) ? (a) : (b)) + +#define N 200 +#define inf 1e99 +#define eps 1e-9 + +double cap[N][N], fnet[N][N]; +int q[N], qf, qb, prev_[N]; + +double ford_fulkerson(int n, int s, int t) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + fnet[i][j] = 0; + } + } + double flow = 0; + while (1) + { + // find augmenting path + memset(prev_, -1, sizeof(prev_)); + qf = qb = 0; + prev_[q[qb++] = s] = -2; + while (qb > qf && prev_[t] == -1) + { + for (int u = q[qf++], v = 0; v < n; v++) + { + if (prev_[v] == -1 && fnet[u][v] - fnet[v][u] < cap[u][v] - eps) + { + prev_[q[qb++] = v] = u; + } + } + } + // see if we're done + if (prev_[t] == -1) + { + break; + } + // get bottleneck capacity + double bot = 1e100; + for (int v = t, u = prev_[v]; u >= 0; v = u, u = prev_[v]) + { + bot = min(bot, cap[u][v] - fnet[u][v] + fnet[v][u]); + } + // update flow network + for (int v = t, u = prev_[v]; u >= 0; v = u, u = prev_[v]) + { + fnet[u][v] += bot; + } + flow += bot; + } + return flow; +} + +bool mat[N][N]; +double costs[N], min_cost; +int t, n, m; + +int main() +{ + cout.precision(4); + cout.setf(ios::fixed | ios::showpoint); + for (cin >> t; t--;) + { + cin >> n >> m; + memset(mat, 0, sizeof mat); + costs[0] = costs[n - 1] = 0; + for (int i = 1; i < n - 1; i++) + { + cin >> costs[i]; + } + for (int i = 0; i < m; i++) + { + int a, b; + cin >> a >> b; + a--; + b--; + mat[a][b] = mat[b][a] = true; + } + + double cost_matrix[N][N]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + cost_matrix[i][j] = 0; + } + else if (mat[i][j]) + { + cost_matrix[i][j] = costs[j]; + } + else + { + cost_matrix[i][j] = inf; + } + } + } + + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cost_matrix[i][j] = mini(cost_matrix[i][j], cost_matrix[i][k] + cost_matrix[k][j]); + } + } + } + if (mat[0][n - 1]) + { + printf("No Solution!\n"); + continue; + } + + for (int i = 0; i < 2 * n; i++) + { + for (int j = 0; j < 2 * n; j++) + { + cap[i][j] = 0; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + cap[i * 2 + 1][i * 2] = cap[i * 2][i * 2 + 1] = cost_matrix[0][i] + cost_matrix[n - 1][i]; + } + else if (mat[i][j]) + { + cap[i * 2 + 1][j * 2] = inf; + } + } + } + + double res = ford_fulkerson(2 * n, 1, 2 * (n - 1)); + printf("%.4lf\n", res); + } + return 0; +} diff --git a/11147.cpp b/11147.cpp new file mode 100644 index 0000000..0d6921e --- /dev/null +++ b/11147.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +//ifstream fin("h.in"); +//ofstream fout("hj.out"); +//#define cin fin +//#define cout fout +int n, arr[1000]; +void print(int a, int b) +{ + // printf("a=%d b=%d\n",a,b); + int sum = 0; + for (int i = a; i <= b; i++) + { + sum += arr[i]; + } + int tsum = 0; + int root = a; + int diff = 1 << 30; + int left = 0; + //cout << "Jumby Jimbo" << endl; + for (int i = a; i <= b; i++) + { + tsum += arr[i]; + if (i == b || (arr[i] != arr[i + 1])) + { + int tdiff = abs((sum - tsum) - (tsum - arr[i])); + //cout << i << " : " << tdiff << endl; + if (tdiff < diff) + { + diff = tdiff; + root = i; + left = tsum - arr[i]; + } + else if (tdiff == diff && left < tsum - arr[i]) + { + diff = tdiff; + root = i; + left = tsum - arr[i]; + } + } + } + cout << arr[root]; + if (b - a + 1 > 1) + { + cout << "("; + } + if (root != a) + { + print(a, root - 1); + } + if (root != a && root != b) + { + cout << ","; + } + if (root != b) + { + print(root + 1, b); + } + if (b - a + 1 > 1) + { + cout << ")"; + } +} +int main() +{ + int t; + cin >> t; + for (int I = 1; I <= t; I++) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + sort(arr, arr + n); + cout << "Case #" << I << ": "; + print(0, n - 1); + cout << endl; + } + return 0; +} diff --git a/11148.cpp b/11148.cpp new file mode 100644 index 0000000..9d5368a --- /dev/null +++ b/11148.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int L[128] = {0}; +char in[10000]; +int GCD(int a, int b) +{ + if (b == 0) + { + return a; + } + while ((a %= b) && (b %= a)) + ; + return a + b; +} +int main() +{ + int t, i, j, no, de, tno, tde, tmp, gcd; + for (i = '0'; i <= '9'; i++) + { + L[i] = 1; + } + scanf("%d", &t); + getchar(); + while (t--) + { + gets(in); + no = de = 1; + for (i = 0; in[i]; i++) + { + if (L[in[i]] < 1) + { + continue; + } + for (tmp = 0; L[in[i]]; i++) + { + tmp = 10 * tmp + in[i] - '0'; + } + if (in[i] == ' ' || in[i] == '\0') + { + tno = tmp, tde = 1; + } + else if (in[i] == '-') + { + for (tno = 0, i++; L[in[i]]; i++) + { + tno = 10 * tno + in[i] - '0'; + } + for (tde = 0, i++; L[in[i]]; i++) + { + tde = 10 * tde + in[i] - '0'; + } + tno += tmp * tde; + } + else if (in[i] == '/') + { + tno = tmp; + for (tde = 0, i++; L[in[i]]; i++) + { + tde = 10 * tde + in[i] - '0'; + } + } + no *= tno, de *= tde; + i--; + } + gcd = GCD(no, de); + no /= gcd, de /= gcd; + if (no % de == 0) + { + printf("%d\n", no / de); + } + else if (no / de) + { + printf("%d-%d/%d\n", no / de, no % de, de); + } + else + { + printf("%d/%d\n", no, de); + } + } + return 0; +} diff --git a/11149.cpp b/11149.cpp new file mode 100644 index 0000000..02d3099 --- /dev/null +++ b/11149.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int n, n2, B[80][80], C[80][80], T[80][80]; +void ini() +{ + int i, j; + for (i = 0; i < n2; i++) + { + for (j = 0; j < n2; j++) + { + B[i][j] = C[i][j] = 0; + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + scanf("%d", &C[i][j]); + C[i][j] %= 10; + } + j = i + n; + B[i][i] = B[j][j] = C[i][j] = C[j][j] = 1; + } +} +void mult(int M1[80][80], int M2[80][80]) +{ + int i, j, k; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + T[i][j] = 0; + for (k = 0; k < n; k++) + { + T[i][j] += M1[i][k] * M2[k][j]; + } + } + for (; j < n2; j++) + { + T[i][j] = 0; + for (k = 0; k < n; k++) + { + T[i][j] += M1[i][k] * M2[k][j]; + } + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + M1[i][j] = T[i][j] % 10; + } + for (; j < n2; j++) + { + M1[i][j] = (M1[i][j] + T[i][j]) % 10; + } + } +} +int main() +{ + int i, j, k; + while (scanf("%d%d", &n, &k) == 2) + { + if (n == 0) + { + break; + } + n2 = n * 2; + for (ini(); k; k >>= 1) + { + if (k & 1) + { + mult(B, C); + } + mult(C, C); + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + B[i][j] = B[i][j] + B[i][j + n]; + } + B[i][i] += 9; + printf("%d", B[i][0] % 10); + for (j = 1; j < n; j++) + { + printf(" %d", B[i][j] % 10); + } + printf("\n"); + } + printf("\n"); + } + return 0; +} diff --git a/1115.cpp b/1115.cpp new file mode 100644 index 0000000..1f51d55 --- /dev/null +++ b/1115.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +const int MAXN = 1024; + +double LV[MAXN], H[MAXN], W[MAXN], D[MAXN], V; + +void solve(int n, double V) +{ + double l = 0, r = 0, mid; + double sumV = 0; + for (int i = 0; i < n; i++) + { + sumV += H[i] * W[i] * D[i]; + r = max(r, LV[i] + H[i]); + } + if (sumV < V) + { + puts("OVERFLOW"); + return; + } + + for (int it = 0; it < 100; it++) + { + mid = (l + r) / 2; + sumV = 0; + for (int i = 0; i < n; i++) + { + if (mid < LV[i]) + continue; + sumV += W[i] * D[i] * min(H[i], mid - LV[i]); + } + if (sumV > V) + r = mid; + else + l = mid; + } + printf("%.2lf\n", l); +} +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + assert(n < MAXN); + for (int i = 0; i < n; i++) + scanf("%lf %lf %lf %lf", &LV[i], &H[i], &W[i], &D[i]); + scanf("%lf", &V); + + solve(n, V); + if (testcase) + puts(""); + } + return 0; +} diff --git a/11150.cpp b/11150.cpp index 7d3dc84..b34eac4 100644 --- a/11150.cpp +++ b/11150.cpp @@ -1,45 +1,30 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N; - while( cin >> N ){ - int cnt = N; - if( N % 2 == 0 ) - cnt++; - while( N >= 3 ){ - int temp = N/3; - cnt += temp; - N %= 3; - N += temp; - } - cout << cnt << endl; +int simulate(int n, int extra) +{ + int result; + int all = n + extra; + result = n; + while (all >= 3) + { + result += all / 3; + all = all / 3 + all % 3; + } + if (all < extra) + { + return 0; + } + return result; +} + +int main() +{ + int n; + while (cin >> n) + { + cout << max(simulate(n, 0), max(simulate(n, 1), simulate(n, 2))) << endl; } return 0; } diff --git a/11151.cpp b/11151.cpp index faa3944..cc519dc 100644 --- a/11151.cpp +++ b/11151.cpp @@ -1,72 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int LCSLength(string X, string Y){ - int m = X.length(); - int n = Y.length(); - int c[m+1][n+1], i, j; - for(i=1; i= c[i][j-1]) - c[i][j] = c[i-1][j]; - else - c[i][j] = c[i][j-1]; - } - } - return c[m][n]; -} -int main(){ - char t[10]; - cin.getline(t, 10); - int test = atoi(t); - while(test--){ - string oStr, rStr=""; - getline(cin, oStr, '\n'); - int len = oStr.length(), mLen = 1, i, j; - if(len==0 || len==1){ - printf("%d\n", len); - continue; - } - for(i=0; i + +using namespace std; + +int dp[1000][1000] = {0}; + +int main() +{ + int t; + cin >> t; + cin.ignore(100, '\n'); + string a; + while (t--) + { + getline(cin, a); + string b(a.rbegin(), a.rend()); + for (int i = 0, sz = a.size(); i <= sz; i++) + { + dp[i][0] = 0; + dp[0][i] = 0; + } + for (int i = 1, sz = a.size(); i <= sz; i++) + { + for (int j = 1; j <= sz; j++) + { + if (a[i - 1] == b[j - 1]) + { + dp[i][j] = dp[i - 1][j - 1] + 1; + } + else + { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + cout << dp[a.size()][b.size()] << endl; + } + return 0; +} diff --git a/11152.cpp b/11152.cpp index b7f04ae..cf43c98 100644 --- a/11152.cpp +++ b/11152.cpp @@ -1,26 +1,19 @@ -#include -#include +#include + using namespace std; -const double PI = acos(-1.0); +#define PI 3.1415926535897932384626433832795 -int main() { - double a, b, c; - while (scanf("%lf%lf%lf", &a, &b, &c) != EOF) { - double s = (a + b + c) / 2.0; - - double V = sqrt(s * (s - a) * (s - b) * (s - c)); - - double Rr = V / s; - double R = PI * Rr * Rr; - - double Sr = (a * b * c) / (4 * Rr * s); - double S = PI * Sr * Sr; - - S -= V; - V -= R; - - printf("%.4lf %.4lf %.4lf\n", S, V, R); +int main() +{ + int a, b, c; + while (cin >> a >> b >> c) + { + double s = (a + b + c) / 2.0; + double A = sqrt(s * (s - a) * (s - b) * (s - c)); + double R = (a * b * c) / (4 * A); + double r = A / s; + cout << setprecision(4) << fixed << showpoint << PI * R * R - A << " " << A - PI * r * r << " " << PI * r * r << endl; } return 0; } diff --git a/11153.cpp b/11153.cpp new file mode 100644 index 0000000..13f5bb4 --- /dev/null +++ b/11153.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int MaxCost, MaxTime, N, Fee[16], Fun[16]; +int dist[16][16]; + +int f[13][1 << 13]; + +int solve() +{ + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + dist[i][j] = MIN(dist[i][j], dist[i][k] + dist[k][j]); + } + for (int x = 0; x < N; x++) + { + f[x][0] = dist[x][0]; + } + for (int m = 2; m < (1 << N); m += 2) + { + for (int x = 0; x < N; x++) + { + f[x][m] = 1 << 20; + for (int y = 1; y < N; y++) + if (m & (1 << y)) + { + f[x][m] = MIN(f[x][m], dist[x][y] + f[y][m ^ (1 << y)]); + } + } + } + int best = 0; + for (int m = 0; m < (1 << N); m += 2) + { + int tim = f[0][m]; + if (tim > MaxTime) + { + continue; + } + int cost = 0; + int fun = 0; + for (int i = 1; i < N; i++) + { + if (m & (1 << i)) + { + tim += 15; + cost += Fee[i]; + fun += Fun[i]; + } + } + if (cost > MaxCost || tim > MaxTime) + { + continue; + } + best = max(best, fun); + } + return best; +} + +int main() +{ + int T, nroads; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d %d %d %d", &MaxCost, &MaxTime, &N, &nroads); + N++; + assert(N <= 13); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + dist[i][j] = (i == j ? 0 : (1 << 20)); + } + for (int i = 1; i < N; i++) + { + scanf("%d %d", &Fee[i], &Fun[i]); + } + for (int i = 0; i < nroads; i++) + { + int x, y, z; + scanf("%d %d %d", &x, &y, &z); + if (z < dist[x][y]) + { + dist[x][y] = dist[y][x] = z; + } + } + int res = solve(); + printf("Case %d: ", cs); + if (res <= 0) + { + printf("No possible trip.\n"); + } + else + { + printf("%d\n", res); + } + } +} diff --git a/11154.cpp b/11154.cpp new file mode 100644 index 0000000..221a536 --- /dev/null +++ b/11154.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +struct Road +{ + int a, b; +}; + +Road R[64]; +int N, adj[64][64], deg[64], order[64]; +int color[64], K; + +bool rec(int i = 0, int mx = 0) +{ + if (i == N) + { + return true; + } + int x = order[i]; + for (int j = i; j < N; j++) + { + long long t = ((1 << K) - 1) << 1; + for (int y = 0; y < N; y++) + { + t &= ~(((long long)adj[order[j]][y]) << color[y]); + } + if (t == 0) + { + return false; + } + } + long long occ = 0; + for (int y = 0; y < N; y++) + { + occ |= ((long long)adj[x][y]) << color[y]; + } + for (color[x] = 1; color[x] <= K && color[x] <= mx + 1; color[x]++) + { + if (((occ >> color[x]) & 1) != 0) + { + continue; + } + if (rec(i + 1, max(mx, color[x]))) + { + return true; + } + } + color[x] = 0; + return false; +} + +bool cmp(int i, int j) +{ + return deg[i] > deg[j]; +} + +int solve(int levels) +{ + for (int i = 0; i < N; i++) + { + deg[i] = 0; + for (int j = 0; j < N; j++) + { + deg[i] += adj[i][j]; + } + } + for (int i = 0; i < N; i++) + { + order[i] = i; + } + sort(order, order + N, cmp); + int left = 3 + min(2, levels - 1); + int right = N; + while (left < right) + { + K = (left + right) / 2; + memset(color, 0, sizeof(color)); + if (rec()) + { + right = K; + } + else + { + left = K + 1; + } + } + return left; +} + +void addRoad(int a, int b) +{ + if (a > b) + { + swap(a, b); + } + for (int i = 0; i < N; i++) + if (R[i].a == a && R[i].b == b) + { + return; + } + assert(N + 1 <= 64); + R[N].a = a; + R[N].b = b; + N++; +} + +int getRoad(int a, int b) +{ + if (a > b) + { + swap(a, b); + } + for (int i = 0; i < N; i++) + if (R[i].a == a && R[i].b == b) + { + return i; + } + return -1; +} + +int main() +{ + int L1, L2, M; + while (scanf(" %o %o %o", &L1, &L2, &M) == 3 && L1 <= L2) + { + assert(0 <= L1 && L2 - L1 <= 2); + N = 0; + for (int lev = L1; lev <= L2; lev++) + { + for (int i = 0; i < 8; i++) + { + addRoad(lev * 8 + i, lev * 8 + (i + 1) % 8); + if (lev + 1 <= L2) + { + addRoad((lev + 1) * 8 + i, lev * 8 + i); + } + } + addRoad(lev * 8, lev * 8 + 4); + } + memset(adj, 0, sizeof(adj)); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (i == j) + { + continue; + } + if (R[i].a == R[j].a || R[i].a == R[j].b || + R[i].b == R[j].a || R[i].b == R[j].b) + { + adj[i][j] = adj[j][i] = 1; + } + } + } + for (int i = 0; i < M; i++) + { + int a, b, c, d; + assert(scanf(" %o %o %o %o", &a, &b, &c, &d) == 4); + int r1 = getRoad(a, b); + int r2 = getRoad(c, d); + if (r1 == r2 || r1 < 0 || r2 < 0) + { + continue; + } + adj[r1][r2] = adj[r2][r1] = 1; + } + printf("%o\n", solve(L2 - L1 + 1)); + } +} diff --git a/11155.cpp b/11155.cpp new file mode 100644 index 0000000..03faf54 --- /dev/null +++ b/11155.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int M[10005]; +int main() +{ + int t, tt, a, b, c, m, n, x, i, j, way, total; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d%d%d%d%d", &a, &b, &c, &m, &n); + for (i = 1; i < m; i++) + { + M[i] = 0; + } + M[0] = 1; + c = (c + 1) % m; + for (way = total = 0, x = a % m; n--; x = (x * b + c) % m) + { + total = (total + x) % m; + way += M[total]; + M[total]++; + } + printf("Case %d: %d\n", t, way); + } + return 0; +} diff --git a/11156.cpp b/11156.cpp new file mode 100644 index 0000000..c2aaa97 --- /dev/null +++ b/11156.cpp @@ -0,0 +1,266 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +struct Point +{ + int x, y; + bool operator<(const Point &p) const + { + return x < p.x || (x == p.x && y < p.y); + } + bool operator==(const Point &p) const + { + return x == p.x && y == p.y; + } +}; + +void read(Point &p) +{ + char c; + scanf(" %c %d", &c, &p.x); + p.y = toupper(c) - 'A' + 1; +} + +bool on(const Point &p, const Point &a, const Point &b) +{ + int t = (a.x - p.x) * (b.y - p.y) - (a.y - p.y) * (b.x - p.x); + if (t != 0) + { + return false; + } + if (p.x < min(a.x, b.x) || p.x > max(a.x, b.x)) + { + return false; + } + if (p.y < min(a.y, b.y) || p.y > max(a.y, b.y)) + { + return false; + } + return true; +} + +Point P[256]; +int N, adj[32][32]; +double fw[32][32]; + +int seen[32]; +int dfs(int x) +{ + if (seen[x]++) + { + return 0; + } + int c = 1; + for (int i = 0; i < N; i++) + if (adj[x][i]) + { + c += dfs(i); + } + return c; +} + +int odd[32], nodd; +int got[1 << 18], got_id; +double memo[1 << 18]; + +double match(int mask) +{ + if (mask == 0) + { + return 0.0; + } + double &res = memo[mask]; + if (got[mask] == got_id) + { + return res; + } + got[mask] = got_id; + int a; + for (a = 0; (mask & (1 << a)) == 0; a++) + ; + res = 1e99; + for (int b = a + 1; b < nodd; b++) + { + if ((mask & (1 << b)) != 0) + { + res = MIN(res, fw[odd[a]][odd[b]] + match(mask ^ (1 << a) ^ (1 << b))); + } + } + return res; +} + +void load() +{ + int nseg; + Point segA[32], segB[32]; + scanf("%d", &nseg); + assert(0 <= nseg && nseg <= 10); + for (int i = 0; i < nseg; i++) + { + read(segA[i]); + read(segB[i]); + } + N = 0; + for (int i = 0; i < nseg; i++) + { + P[N++] = segA[i]; + P[N++] = segB[i]; + } + for (int x = 1; x <= 5; x++) + { + for (int y = 1; y <= 5; y++) + { + Point p; + p.x = x; + p.y = y; + bool add = false; + for (int k = 0; k < nseg; k++) + { + for (int m = k + 1; m < nseg; m++) + { + if (!on(p, segA[k], segB[k])) + { + continue; + } + if (!on(p, segA[m], segB[m])) + { + continue; + } + if (on(segA[k], segA[m], segB[m])) + { + continue; + } + if (on(segB[k], segA[m], segB[m])) + { + continue; + } + if (on(segA[m], segA[k], segB[k])) + { + continue; + } + if (on(segB[m], segA[k], segB[k])) + { + continue; + } + add = true; + } + } + if (add) + { + P[N++] = p; + } + } + } + sort(P, P + N); + N = unique(P, P + N) - P; + memset(adj, 0, sizeof(adj)); + for (int i = 0; i < nseg; i++) + { + if (segA[i] == segB[i]) + { + continue; + } + int a, b; + for (a = 0; !(P[a] == segA[i]); a++) + ; + for (b = 0; !(P[b] == segB[i]); b++) + ; + vector v; + for (int k = 0; k < N; k++) + if (on(P[k], P[a], P[b])) + { + v.push_back(k); + } + for (int i = 0; i < (int)v.size(); i++) + for (int j = i + 1; j < (int)v.size(); j++) + { + adj[v[i]][v[j]] = adj[v[j]][v[i]] = 1; + } + } + for (int a = 0; a < N; a++) + { + adj[a][a] = 0; + for (int b = a + 1; b < N; b++) + { + if (!adj[a][b]) + { + continue; + } + for (int c = 0; c < N; c++) + { + if (c != a && c != b && on(P[c], P[a], P[b])) + { + adj[a][b] = adj[b][a] = 0; + break; + } + } + // if (adj[a][b])printf("%d,%d - %d,%d\n", P[a].x, P[a].y, P[b].x, P[b].y); + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + memset(got, 0, sizeof(got)); + for (int cs = 1; cs <= T; cs++) + { + load(); + memset(seen, 0, sizeof(seen)); + if (N > 0 && dfs(0) != N) + { + printf("Case %d: ~x(\n", cs); + continue; + } + double ans = 0; + nodd = 0; + for (int a = 0; a < N; a++) + { + int deg = 0; + for (int b = 0; b < N; b++) + { + deg += adj[a][b]; + if (adj[a][b]) + { + fw[a][b] = sqrt(0.0 + (P[a].x - P[b].x) * (P[a].x - P[b].x) + (P[a].y - P[b].y) * (P[a].y - P[b].y)); + if (a < b) + { + ans += fw[a][b]; + } + } + else + { + fw[a][b] = (a == b ? 0 : 1e99); + } + } + if (deg % 2 != 0) + { + odd[nodd++] = a; + } + } + for (int k = 0; k < N; k++) + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + { + fw[i][j] = MIN(fw[i][j], fw[i][k] + fw[k][j]); + } + got_id++; + if (nodd > 0) + { + assert(nodd % 2 == 0 && nodd <= 18); + double t = 1e99; + for (int i = 0; i < nodd; i++) + for (int j = i + 1; j < nodd; j++) + { + t = MIN(t, match(((1 << nodd) - 1) ^ (1 << i) ^ (1 << j))); + } + ans += t; + } + printf("Case %d: %.2f\n", cs, ans); + } +} diff --git a/11157.cpp b/11157.cpp new file mode 100644 index 0000000..18d796c --- /dev/null +++ b/11157.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +const int MAX_N = 100; +const int MAX_V = 2 * MAX_N + 2; + +int bs[MAX_N + 2], ss[MAX_N]; +int B, S, D; + +typedef int flow_t; +const flow_t INF = 1 << 20; +const flow_t EPS = 0; + +struct edge +{ + int to, rev; + flow_t cap; +}; +vector graph[MAX_V]; + +int level[MAX_V]; +int iter[MAX_V]; + +void add_edge(int from, int to, flow_t cap) +{ + graph[from].push_back((edge){ + to, (int)graph[to].size(), cap}); + graph[to].push_back((edge){ + from, (int)graph[from].size() - 1, 0}); +} + +void bfs(int source) +{ + memset(level, -1, sizeof(level)); + queue que; + level[source] = 0; + que.push(source); + while (!que.empty()) + { + int v = que.front(); + que.pop(); + for (int i = 0; i < (int)graph[v].size(); i++) + { + edge &e = graph[v][i]; + if (e.cap > 0 && level[e.to] < 0) + { + level[e.to] = level[v] + 1; + que.push(e.to); + } + } + } +} + +flow_t dfs(int v, int sink, flow_t f) +{ + if (v == sink) + { + return f; + } + for (int &i = iter[v]; i < (int)graph[v].size(); i++) + { + edge &e = graph[v][i]; + if (e.cap > EPS && level[v] < level[e.to]) + { + int d = dfs(e.to, sink, min(f, e.cap)); + if (d > EPS) + { + e.cap -= d; + graph[e.to][e.rev].cap += d; + return d; + } + } + } + return 0; +} + +flow_t max_flow(int source, int sink) +{ + flow_t flow = 0; + for (;;) + { + bfs(source); + if (level[sink] < 0) + { + break; + } + memset(iter, 0, sizeof(iter)); + flow_t f; + while ((f = dfs(source, sink, INF)) > 0) + { + flow += f; + } + } + return flow; +} + +void init() +{ + B = S = 0; + int N; + scanf("%d%d ", &N, &D); + for (int i = 0; i < N; i++) + { + char c; + int d; + scanf("%c-%d ", &c, &d); + if (c == 'B') + { + bs[B++] = d; + } + else + { + ss[S++] = d; + } + } + bs[B++] = 0; + bs[B++] = D; +} + +bool check(int x) +{ + for (int i = 0; i < MAX_V; i++) + { + graph[i].clear(); + } + const int source = B + 2 * S - 2, sink = source + 1; + for (int i = 0; i < S; i++) + { + add_edge(i, i + S, 1); + add_edge(i + S, i, 1); + } + for (int i = 0; i < S; i++) + { + for (int j = i + 1; j < S; j++) + if (abs(ss[i] - ss[j]) <= x) + { + add_edge(i + S, j, INF); + add_edge(j + S, i, INF); + } + } + for (int i = 0; i < S; i++) + { + for (int j = 0; j < B; j++) + if (abs(ss[i] - bs[j]) <= x) + { + add_edge(i + S, j + 2 * S, INF); + add_edge(j + 2 * S, i, INF); + } + } + for (int i = 0; i < B; i++) + { + for (int j = i + 1; j < B; j++) + if (abs(bs[i] - bs[j]) <= x) + { + add_edge(i + 2 * S, j + 2 * S, INF); + add_edge(j + 2 * S, i + 2 * S, INF); + } + } + return max_flow(source, sink) >= 2; +} + +int solve() +{ + int low = 0, high = D; + while (high - low > 1) + { + int mid = (high + low) / 2; + if (check(mid)) + { + high = mid; + } + else + { + low = mid; + } + } + return high; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int c = 1; c <= T; c++) + { + init(); + printf("Case %d: %d\n", c, solve()); + } + return 0; +} diff --git a/11158.cpp b/11158.cpp new file mode 100644 index 0000000..098853d --- /dev/null +++ b/11158.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int X[60]; +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int main() +{ + int t, tt, n, i, j, sum, a, b; + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d", X + i); + } + qsort(X, n, sizeof(int), cmp); + sum = abs(X[0] - X[n - 1]); + for (i = 1, j = n - 2; i <= j; i++, j--) + { + a = abs(X[i] - X[j + 1]); + b = abs(X[i - 1] - X[j]); + sum += a + b; + } + if (n & 1) + { + i = n >> 1; + a = abs(X[i] - X[i - 1]); + b = abs(X[i] - X[i + 1]); + if (a < b) + { + sum -= a; + } + else + { + sum -= b; + } + } + printf("Case %d: %d\n", t, sum); + } + return 0; +} diff --git a/11159.cpp b/11159.cpp new file mode 100644 index 0000000..72c469c --- /dev/null +++ b/11159.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < (int)n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define FOR(it, o) for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it) + +const double EPS = 0.00000001; +const int INF = 1000000000; +typedef long long lli; + +const int MAX_V = 1000; + +int V; +vector G[MAX_V]; +int match[MAX_V]; +bool used[MAX_V]; + +void add_edge(int u, int v) +{ + G[u].push_back(v); + G[v].push_back(u); +} + +bool dfs(int v) +{ + used[v] = true; + rep(i, (int)G[v].size()) + { + int u = G[v][i], w = match[u]; + if (w < 0 || (!used[w] && dfs(w))) + { + match[v] = u; + match[u] = v; + return true; + } + } + return false; +} + +int bipartite_matching() +{ + int res = 0; + memset(match, -1, sizeof(match)); + rep(i, V) + { + if (match[i] < 0) + { + memset(used, 0, sizeof(used)); + if (dfs(i)) + { + res++; + } + } + } + return res; +} + +int main() +{ + int T; + cin >> T; + rep(tc, T) + { + rep(i, 1000) + { + G[i].clear(); + } + int t[2][1000]; + int n[2]; + rep(i, 2) + { + cin >> n[i]; + rep(j, n[i]) + { + cin >> t[i][j]; + } + } + rep(i, n[0]) + { + rep(j, n[1]) + { + if (t[1][j] == 0) + { + add_edge(i, n[0] + j); + } + else if (t[0][i] == 0) + { + continue; + } + else if (t[1][j] % t[0][i] == 0) + { + add_edge(i, n[0] + j); + } + } + } + V = n[0] + n[1]; + printf("Case %d: %d\n", tc + 1, bipartite_matching()); + } + return 0; +} diff --git a/11160.cpp b/11160.cpp new file mode 100644 index 0000000..2a4571a --- /dev/null +++ b/11160.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +char maze[16][16]; +bool canMove[16 * 16][3][3], exitCell[16 * 16]; +int N; + +int d[81 * 81 * 81 + 1000]; +int q[81 * 81 * 81 * 3 + 1000]; + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d", &N); + assert(3 <= N && N <= 9); + int head = 0, tail = 0; + memset(maze, '#', sizeof(maze)); + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= N; j++) + { + scanf(" %c", &maze[i][j]); + if (isalpha(maze[i][j]) && maze[i][j] != 'X') + { + q[tail++] = (i - 1) * 9 + (j - 1); + } + } + } + memset(canMove, 0, sizeof(canMove)); + for (int i = 1; i <= 9; i++) + { + for (int j = 1; j <= 9; j++) + { + int cell = (i - 1) * 9 + (j - 1); + exitCell[cell] = (maze[i][j] == 'X'); + for (int dx = -1; dx <= 1; dx++) + for (int dy = -1; dy <= 1; dy++) + { + canMove[cell][dy + 1][dx + 1] = (maze[i + dy][j + dx] != '#'); + } + } + } + sort(q, q + tail); + memset(d, 0, sizeof(d)); + d[q[0] * 81 * 81 + q[1] * 81 + q[2]] = 1; + while (head < tail) + { + int s[3], t[3]; + s[0] = q[head++]; + s[1] = q[head++]; + s[2] = q[head++]; + int es = s[0] * 81 * 81 + s[1] * 81 + s[2]; + for (int dir = 0; dir < 4; dir++) + { + int dx = "0211"[dir] - '1'; + int dy = "1102"[dir] - '1'; + for (int i = 0; i < 3; i++) + { + t[i] = canMove[s[i]][dy + 1][dx + 1] ? (s[i] + dx + 9 * dy) : s[i]; + } + for (int i = 1; i < 3; i++) + for (int j = i; j > 0 && t[j - 1] > t[j]; j--) + { + swap(t[j - 1], t[j]); + } + if (t[0] == t[1]) + { + t[0] -= dx + 9 * dy; + if (t[0] == t[2]) + { + continue; + } + } + else if (t[1] == t[2]) + { + t[1] -= dx + 9 * dy; + if (t[1] == t[0]) + { + continue; + } + } + for (int i = 1; i < 3; i++) + for (int j = i; j > 0 && t[j - 1] > t[j]; j--) + { + swap(t[j - 1], t[j]); + } + if (exitCell[t[0]] && exitCell[t[1]] && exitCell[t[2]]) + { + printf("Case %d: %d\n", cs, d[es]); + goto done; + } + int et = t[0] * 81 * 81 + t[1] * 81 + t[2]; + if (d[et] == 0) + { + d[et] = d[es] + 1; + q[tail++] = t[0]; + q[tail++] = t[1]; + q[tail++] = t[2]; + } + } + } + printf("Case %d: trapped\n", cs); + done:; + } +} diff --git a/11161.cpp b/11161.cpp new file mode 100644 index 0000000..44468eb --- /dev/null +++ b/11161.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define limit 1000000000 +int F[1505][40] = {0}, lf[1505], S[1505][40] = {0}, ls[1505]; +int main() +{ + int t, i, j, n; + F[0][0] = F[1][0] = lf[0] = lf[1] = ls[0] = 1; + for (i = 2; i < 1505; i++) + { + lf[i] = lf[i - 1]; + for (j = 0; j < lf[i]; j++) + { + F[i][j] = F[i - 1][j] + F[i - 2][j]; + } + for (j = 0; j < lf[i]; j++) + { + if (F[i][j] >= limit) + { + F[i][j + 1]++; + F[i][j] -= limit; + } + } + if (F[i][lf[i]]) + { + lf[i]++; + } + } + for (i = 1; i < 1505; i++) + { + ls[i] = ls[i - 1]; + for (j = 0; j < ls[i]; j++) + { + S[i][j] = S[i - 1][j] + F[i][j]; + } + for (j = 0; j < ls[i]; j++) + { + if (S[i][j] >= limit) + { + S[i][j + 1]++; + S[i][j] -= limit; + } + } + if (S[i][ls[i]]) + { + ls[i]++; + } + } + for (i = 0; i < 1505; i++) + { + F[i][0]++; + for (j = 0; F[i][j] >= limit; j++) + { + F[i][j + 1]++; + F[i][j] -= limit; + } + if (F[i][lf[i]]) + { + lf[i]++; + } + for (j = lf[i] - 1; j > 0; j--) + { + if (F[i][j] & 1) + { + F[i][j - 1] += limit; + } + F[i][j] >>= 1; + } + F[i][0] >>= 1; + if (!F[i][lf[i] - 1]) + { + lf[i]--; + } + } + for (i = 0; i < 1505; i++) + { + for (j = 0; j < ls[i]; j++) + { + S[i][j] += F[i + 1][j]; + } + for (j = 0; j < ls[i]; j++) + { + if (S[i][j] >= limit) + { + S[i][j + 1]++; + S[i][j] -= limit; + } + } + if (S[i][ls[i]]) + { + ls[i]++; + } + } + for (t = 1; scanf("%d", &n) == 1; t++) + { + if (n == 0) + { + break; + } + printf("Set %d:\n", t); + if (n == 1) + { + printf("0\n"); + continue; + } + else + { + n -= 2; + } + printf("%d", S[n][ls[n] - 1]); + for (i = ls[n] - 2; i >= 0; i--) + { + printf("%09d", S[n][i]); + } + printf("\n"); + } + return 0; +} diff --git a/11162.cpp b/11162.cpp new file mode 100644 index 0000000..62c574f --- /dev/null +++ b/11162.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +char S[1000]; +int N; + +bool okay(char a, char b, char c) +{ + return (a == 'R') + (b == 'R') + (c == 'R') <= 1; +} + +long long memo[128][128]; + +long long f(int s, int t) +{ + if (t - s == -1) + { + return 1; + } + assert(s <= t); + if ((t - s + 1) % 3 != 0) + { + return 0; + } + long long &res = memo[s][t]; + if (res != -1) + { + return res; + } + res = 0; + for (int a = s, b = s + 1; b <= t; b++) + { + for (int c = b + 1; c <= t; c++) + { + if (!okay(S[a], S[b], S[c])) + { + continue; + } + res += f(a + 1, b - 1) * f(b + 1, c - 1) * f(c + 1, t); + } + } + return res; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf(" %d %s", &N, S); + for (int i = 0; i < N; i++) + { + S[i + N] = S[i + N + N] = S[i]; + } + long long ways = 0; + memset(memo, 0xff, sizeof(memo)); + for (int a = 0, b = 1; b < N; b++) + { + for (int c = b + 1; c < N; c++) + { + if (!okay(S[a], S[b], S[c])) + { + continue; + } + ways += f(a + 1, b - 1) * f(b + 1, c - 1) * f(c + 1, N + a - 1); + } + } + printf("Case %d: %lld\n", cs, ways); + } +} diff --git a/11163.cpp b/11163.cpp new file mode 100644 index 0000000..c7d3715 --- /dev/null +++ b/11163.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define IN(i) ((i) > 0 && (i) <= n) + +const int maxn = 40 + 10; +const int w[4][4] = {{-3, -1, 4, -4}, {1, 3, 4, -4}, {1, -1, 4, -4}, {1, -1, 4, -4}}; +const int limit = 100000; + +int p[maxn], map_[maxn][maxn], n, test, ans; + +void pre() +{ + for (int i = 1; i <= 40; i++) + { + for (int j = 1; j <= 40; j++) + { + map_[i][j] = (i == j) ? 0 : limit; + } + } + for (int i = 1; i <= 40; i++) + { + for (int j = 0; j < 4; j++) + { + if (i + w[i % 4][j] > 0 && i + w[i % 4][j] <= 40) + { + map_[i][i + w[i % 4][j]] = map_[i + w[i % 4][j]][i] = 1; + } + } + } + for (int k = 1; k <= 40; ++k) + { + for (int i = 1; i <= 40; ++i) + { + for (int j = 1; j <= 40; ++j) + { + map_[i][j] = map_[i][j] < map_[i][k] + map_[k][j] + ? map_[i][j] : map_[i][k] + map_[k][j]; + } + } + } +} + +bool dfs(int pos, int c, int dep, int last) +{ + if (!c) + { + return true; + } + if (c + dep > ans) + { + return false; + } + int np, nc; + for (int i = 0; i < 4; i++) + { + np = pos + w[pos % 4][i]; + if (IN(np) && (np != last)) + { + nc = c - map_[np][p[np]] + map_[pos][p[np]]; + if (nc < 0) + { + cout << "nc < 0" << endl; + return true; + } + p[pos] = p[np]; + p[np] = 1; + if (dfs(np, nc, dep + 1, pos)) + { + return true; + } + p[np] = p[pos]; + p[pos] = 1; + } + } + return false; +} + +void work() +{ + int k = 0, pos; + for (int i = 1; i <= n; i++) + { + k += map_[i][p[i]]; + if (p[i] == 1) + { + pos = i; + } + } + k -= map_[pos][1]; + printf("Set %d:\n", ++test); + for (ans = 0;; ans++) + { + if (dfs(pos, k, 0, -1)) + { + printf("%d\n", ans); + return; + } + } +} + +int main() +{ + pre(); + while (scanf("%d", &n) != EOF && n) + { + for (int i = 1; i <= n; ++i) + { + scanf("%d", &p[i]); + } + work(); + } + return 0; +} diff --git a/11164.cpp b/11164.cpp new file mode 100644 index 0000000..5cbeadc --- /dev/null +++ b/11164.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + double a, b, c; + int cases = 1; + while (scanf("%lf %lf %lf", &a, &b, &c) == 3) + { + if (a < 0) + { + break; + } + printf("Set %d:\n", cases++); + if (b * b - a * c == 0) + { + puts("Poor King!"); + } + else + { + double d = b * b - a * c; + double x = (a * b * c + a * a * c) / d; + double y = (a * b * c + a * c * c) / d; + if ((x + y) >= 0) + { + printf("%.4lf\n", x + y); + } + else + { + puts("Poor King!"); + } + } + } + return 0; +} diff --git a/11165.cpp b/11165.cpp new file mode 100644 index 0000000..7746c46 --- /dev/null +++ b/11165.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +struct LinkList +{ + int v; + LinkList *next; + LinkList(int s) : v(s) {} +} *link; + +struct Edge +{ + int V1, V2; + Edge(int v1, int v2) : V1(v1), V2(v2) {} + Edge *next; +} * e[100000]; + +int queue_[100000], front, rear, dist[100000], n, k, S, T; + +void clearlist(LinkList *&p) +{ + if (p) + { + clearlist(p->next); + delete p; + p = NULL; + } +} +bool canvisit(int s, int from) +{ + bool flag = true; + for (Edge *p = e[from]; p && flag; p = p->next) + if (p->V1 <= s && p->V2 >= s) + { + flag = false; + } + return flag; +} +void freeEdge(Edge *&p) +{ + if (p) + { + freeEdge(p->next); + delete p; + p = NULL; + } +} +int main() +{ + int cases, U, V1, V2; + scanf("%d", &cases); + for (int x = 1; x <= cases; x++) + { + scanf("%d", &n); + scanf("%d", &k); + for (int i = 0; i < n; i++) + { + e[i] = NULL; + } + for (int i = 0; i < k; i++) + { + scanf("%d %d-%d", &U, &V1, &V2); + Edge *p = new Edge(V1, V2); + p->next = e[U]; + e[U] = p; + } + scanf("%d %d", &S, &T); + for (int i = 0; i < n; i++) + { + dist[i] = 99999999; + } + dist[S] = 0; + queue_[0] = S; + front = -1; + rear = 0; + link = NULL; + for (int i = n - 1; i >= 0; i--) + { + if (i == S) + { + continue; + } + LinkList *p = new LinkList(i); + p->next = link; + link = p; + } + while (front < rear) + { + int node = queue_[++front]; + if (node == T) + { + break; + } + if (e[node] == NULL) + { + for (LinkList *p = link; p; p = p->next) + { + dist[p->v] = dist[node] + 1; + queue_[++rear] = p->v; + } + clearlist(link); + } + else + { + for (LinkList *p = link, *prev = NULL; p;) + { + if (!canvisit(p->v, node)) + { + prev = p; + p = p->next; + continue; + } + dist[p->v] = dist[node] + 1; + queue_[++rear] = p->v; + if (p == link) + { + prev = p; + link = p->next; + delete p; + p = link; + prev = NULL; + } + else + { + prev->next = p->next; + delete p; + p = prev->next; + } + } + } + } + for (int i = 0; i < n; i++) + { + freeEdge(e[i]); + } + clearlist(link); + if (dist[T] != 99999999) + { + printf("Case #%d: %d\n", x, dist[T]); + } + else + { + printf("Case #%d: Impossible\n", x); + } + } +} diff --git a/11166.cpp b/11166.cpp new file mode 100644 index 0000000..b120f1c --- /dev/null +++ b/11166.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +#define MAXN 65536 + +bool u[MAXN][2], nxt[MAXN][2]; +char a[2][MAXN]; +int len, f[MAXN][2]; + +int solve(int i, int flg) +{ + if (i == len) + { + return 0; + } + if (u[i][flg]) + { + return f[i][flg]; + } + u[i][flg] = 1; + if (a[flg][i] == '0') + { + if (i > 0) + { + f[i][flg] = solve(i + 1, 1 - flg) + 1; + nxt[i][flg] = 1; + int tmp = solve(i + 1, flg); + if (tmp < f[i][flg]) + { + f[i][flg] = tmp; + nxt[i][flg] = 0; + } + } + else + { + f[i][flg] = solve(i + 1, flg); + nxt[i][flg] = 0; + int tmp = solve(i + 1, 1 - flg) + 1; + if (tmp < f[i][flg]) + { + f[i][flg] = tmp; + nxt[i][flg] = 1; + } + } + } + else + { + f[i][flg] = solve(i + 1, flg) + 1; + nxt[i][flg] = 0; + } + return f[i][flg]; +} + +void prints(int value, int sign) +{ + if (value) + { + if (sign == 0) + { + printf("+"); + } + else + { + printf("-"); + } + } + else + { + printf("0"); + } +} + +void print(int i, int flg) +{ + if (i == len) + { + printf("\n"); + return; + } + int value = a[flg][i] - '0'; + if (nxt[i][flg] == 0) + { + if (i > 0) + { + prints(value, flg); + } + print(i + 1, flg); + } + else + { + prints(1 - value, flg); + print(i + 1, 1 - flg); + } +} + +int main() +{ + while (1) + { + memset(f, 0, sizeof(f)); + memset(u, 0, sizeof(u)); + memset(nxt, 0, sizeof(nxt)); + gets(a[0] + 1); + if (strcmp(a[0] + 1, "0") == 0) + { + break; + } + a[0][0] = '0'; + len = strlen(a[0]); + bool flag = false; + a[1][len] = 0; + for (int i = len - 1; i >= 0; i--) + { + if (flag) + { + a[1][i] = 1 - (a[0][i] - '0') + '0'; + } + else + { + a[1][i] = a[0][i]; + } + if (a[0][i] == '1') + { + flag = true; + } + } + solve(0, 0); + print(0, 0); + } + return 0; +} diff --git a/11167.cpp b/11167.cpp new file mode 100644 index 0000000..d94d282 --- /dev/null +++ b/11167.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +#define MAXN 100 +#define MAXV 300 + +int flow[MAXV + 1][MAXV + 1], prev_[MAXV + 1], queue_[MAXV + 1], delta[MAXV + 1], s, t, maxflow; +set S; +map M1, M2; +pair p[MAXN + 1]; +vector>> adj; +vector> sorted, ans; + +bool dijkstra() +{ + int j, u, v, c, head, tail; + memset(prev_, 0, sizeof(prev_)); + prev_[s] = s + 1; + delta[s] = 1000000000; + head = tail = 0; + queue_[tail++] = s; + while (head < tail) + { + u = queue_[head++]; + for (j = 0; j < adj[u].size(); j++) + { + v = adj[u][j].first; + c = adj[u][j].second; + if (prev_[v] == 0) + { + if (c - flow[u][v] != 0) + { + delta[v] = min(delta[u], c - flow[u][v]); + prev_[v] = u + 1; + queue_[tail++] = v; + } + else if (flow[v][u] != 0) + { + delta[v] = min(delta[u], flow[v][u]); + prev_[v] = -u - 1; + queue_[tail++] = v; + } + } + } + } + if (prev_[t] != 0) + { + return true; + } + else + { + return false; + } +} + +void max_flow() +{ + int j; + maxflow = 0; + while (dijkstra() == true) + { + j = t; + while (j != s) + { + //printf("%d %d\n", j, prev_[j]); + if (prev_[j] > 0) + { + flow[prev_[j] - 1][j] = flow[prev_[j] - 1][j] + delta[t]; + j = prev_[j] - 1; + } + else + { + flow[j][-prev_[j] - 1] = flow[j][-prev_[j] - 1] - delta[t]; + j = -prev_[j] - 1; + } + } + maxflow = maxflow + delta[t]; + } +} + +int main() +{ + int v[MAXN + 1], len[MAXV + 1]; + int i, j, k, m, n, r, sum, start, end, differ; + r = 0; + while ((scanf("%d", &n) != EOF) && (n != 0)) + { + memset(flow, 0, sizeof(flow)); + S.clear(); + M1.clear(); + M2.clear(); + scanf("%d", &m); + sum = 0; + for (i = 0; i < n; i++) + { + scanf("%d %d %d", &v[i], &p[i].first, &p[i].second); + sum = sum + v[i]; + S.insert(p[i].first); + S.insert(p[i].second); + } + k = -1; + for (set::iterator it = S.begin(); it != S.end(); it++) + { + k++; + M1[k] = *it; + M2[*it] = k; + } + s = k + n; + t = s + 1; + adj.clear(); + adj.resize(t + 1); + for (i = 0; i < n; i++) + { + for (j = M2[p[i].first]; j < M2[p[i].second]; j++) + { + adj[i].push_back(make_pair(n + j, M1[j + 1] - M1[j])); + adj[n + j].push_back(make_pair(i, 0)); + } + adj[s].push_back(make_pair(i, v[i])); + adj[i].push_back(make_pair(s, 0)); + } + for (j = 0; j < k; j++) + { + adj[n + j].push_back(make_pair(t, (M1[j + 1] - M1[j]) * m)); + adj[t].push_back(make_pair(n + j, 0)); + } + max_flow(); + printf("Case %d: ", ++r); + if (sum == maxflow) + { + printf("Yes\n"); + for (j = 0; j < k; j++) + { + len[j] = M1[j]; + } + for (i = 0; i < n; i++) + { + sorted.clear(); + ans.clear(); + for (j = 0; j < k; j++) + { + if (flow[i][n + j] != 0) + { + start = len[j]; + end = start + flow[i][n + j]; + differ = M1[j + 1] - M1[j]; + len[j] = end; + if (end <= M1[j + 1]) + { + sorted.push_back(make_pair(start, end)); + } + else + { + sorted.push_back(make_pair(start, M1[j + 1])); + sorted.push_back(make_pair(M1[j], end - differ)); + len[j] = len[j] - differ; + } + if (len[j] == M1[j + 1]) + { + len[j] = M1[j]; + } + } + } + sort(sorted.begin(), sorted.end()); + for (j = 0; j < sorted.size(); j++) + { + if ((ans.size() != 0) && (ans.back().second == sorted[j].first)) + { + ans.back().second = sorted[j].second; + } + else + { + ans.push_back(sorted[j]); + } + } + printf("%d", ans.size()); + for (j = 0; j < ans.size(); j++) + { + printf(" (%d,%d)", ans[j].first, ans[j].second); + } + printf("\n"); + } + } + else + { + printf("No\n"); + } + } + return 0; +} diff --git a/11168.cpp b/11168.cpp new file mode 100644 index 0000000..559de65 --- /dev/null +++ b/11168.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; +}; +Point p[10001], vertex[10001]; +Point center; +int pc, vc; +inline double cross(Point a, Point b, Point c) +{ + return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); +} +inline double dist(double x, double y) +{ + return x * x + y * y; +} +inline void swap(Point &a, Point &b) +{ + Point t = a; + a = b; + b = t; +} +bool comp(Point a, Point b) +{ + Point &t = p[0]; + double c = cross(a, b, t); + if ((c > 0) || + ((c == 0) && dist(a.x - t.x, a.y - t.y) < dist(b.x - t.x, b.y - t.y))) + { + return true; + } + return false; +} +void qsort(int left, int right) +{ + if (left < right) + { + int s = (rand() % (right - left + 1)) + left; + swap(p[left], p[s]); + int i = left, j = right + 1; + Point pivot = p[left]; + do + { + do + { + i++; + } while (comp(p[i], pivot) && i <= right); + do + { + j--; + } while (comp(pivot, p[j]) && j >= left); + if (i < j) + { + swap(p[i], p[j]); + } + } while (i < j); + swap(p[left], p[j]); + qsort(left, j - 1); + qsort(j + 1, right); + } +} +void ConvexHull() +{ + int i, l = 0, ptr = 1; + for (i = 1; i < pc; i++) + if ((p[i].y < p[l].y) || + ((p[i].y == p[l].y) && (p[i].x < p[l].x))) + { + l = i; + } + swap(p[0], p[l]); + qsort(1, pc - 1); + vertex[0] = p[0]; + vertex[1] = p[1]; + for (i = 2; ptr < 2 && i < pc; i++) + { + double t1 = (p[i].y - p[0].y) * (p[i - 1].x - p[0].x); + double t2 = (p[i - 1].y - p[0].y) * (p[i].x - p[0].x); + if (t1 == t2) + { + vertex[ptr] = p[i]; + } + else + { + vertex[++ptr] = p[i]; + } + } + for (; i < pc; i++) + { + while (cross(p[i], vertex[ptr], vertex[ptr - 1]) >= 0 && ptr >= 2) + { + ptr--; + } + vertex[++ptr] = p[i]; + } + vertex[++ptr] = p[0]; + vc = ptr + 1; +} +void calcline(Point *p1, Point *p2, double &a, double &b, double &c) +{ + if (p1->x == p2->x) + { + a = 1; + b = 0; + c = -p1->x; + } + else if (p1->y == p2->y) + { + a = 0; + b = 1; + c = -p1->y; + } + else + { + double m = (p2->y - p1->y) / (p2->x - p1->x); + double bb = p1->y - m * p1->x; + a = m; + b = -1; + c = bb; + } +} +int main() +{ + srand(time(NULL)); + int n, t, t1; + scanf("%d", &n); + for (int cases = 1; cases <= n; cases++) + { + scanf("%d", &pc); + center.x = center.y = 0; + for (int i = 0; i < pc; i++) + { + scanf("%lf %lf", &p[i].x, &p[i].y); + center.x += p[i].x; + center.y += p[i].y; + } + if (pc <= 2) + { + printf("Case #%d: %.3lf\n", cases, 0.000); + continue; + } + center.x /= (double)pc; + center.y /= (double)pc; + ConvexHull(); + double min = 999999999; + for (int i = 0; i < vc - 1; i++) + { + double a, b, c; + calcline(&vertex[i], &vertex[i + 1], a, b, c); + double dist = fabs(a * center.x + b * center.y + c) / sqrt(a * a + b * b); + if (dist < min) + { + min = dist; + } + } + printf("Case #%d: %.3lf\n", cases, min); + } +} diff --git a/11169.cpp b/11169.cpp new file mode 100644 index 0000000..b0c24d6 --- /dev/null +++ b/11169.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int value[] = + { + 10, 20, 50, 100, 200, + 500, 1000, 2000, 5000, 10000, + 20000, 50000, 100000, 200000, 500000}; + +int perc[4], limit, sum[1 << 15], nbits[1 << 15], mini[1 << 15], maxi[1 << 15]; +char got[1 << 15]; +double prob[1 << 15], prize[1 << 15]; +int choose[5][1536], choose_n[5] = {0, 0, 0, 0, 0}; + +void rec(int round, int mask) +{ + if (got[mask]) + { + return; + } + got[mask] = 1; + if (round == 4) + { + prize[mask] = sum[mask] / 2.0; + prob[mask] = 0; + if (mini[mask] >= limit) + { + prob[mask] += 0.5; + } + if (maxi[mask] >= limit) + { + prob[mask] += 0.5; + } + return; + } + prob[mask] = 0; + prize[mask] = 0; + int *ch = choose[round == 0 ? 4 : 3]; + int ch_n = choose_n[round == 0 ? 4 : 3]; + int denom = 0; + prob[mask] = 0; + prize[mask] = 0; + for (int ch_i = 0; ch_i < ch_n; ch_i++) + { + int c = ch[ch_i]; + if ((mask & c) != c) + { + continue; + } + int deal = (sum[mask ^ c] * perc[round]) / (100 * nbits[mask ^ c]); + if (deal >= limit) + { + prob[mask] += 1.0; + prize[mask] += deal; + } + else + { + rec(round + 1, mask ^ c); + prob[mask] += prob[mask ^ c]; + prize[mask] += prize[mask ^ c]; + } + denom++; + } + prob[mask] /= denom; + prize[mask] /= denom; +} + +int main() +{ + for (int mask = 0; mask < 32768; mask++) + { + sum[mask] = 0; + nbits[0] = 0; + mini[mask] = 1 << 30; + maxi[mask] = 0; + for (int i = 0; i < 15; i++) + { + if ((mask & (1 << i)) == 0) + { + continue; + } + sum[mask] += value[i]; + nbits[mask]++; + mini[mask] = MIN(mini[mask], value[i]); + maxi[mask] = MAX(maxi[mask], value[i]); + } + if (nbits[mask] <= 4) + { + choose[nbits[mask]][choose_n[nbits[mask]]++] = mask; + } + } + + for (;;) + { + int t = scanf("%d %d %d %d %d", &limit, &perc[0], &perc[1], &perc[2], &perc[3]); + if (t != 5 || limit <= 0) + { + break; + } + memset(got, 0, sizeof(got)); + int m = (1 << 15) - 1; + rec(0, m); + printf("%.10f %.10f\n", prob[m], prize[m]); + } + return 0; +} diff --git a/11170.cpp b/11170.cpp new file mode 100644 index 0000000..f8f6e10 --- /dev/null +++ b/11170.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +long long int T[60][60] = {0}; +int main() +{ + int i, j, n, f; + T[0][0] = T[1][1] = 1; + for (i = 2; i < 50; i++) + { + for (j = 0; j < i; j++) + { + T[i][j + 1] = 2 * T[i - 1][j]; + } + for (j = 0; j < i; j++) + { + T[i][j] -= T[i - 2][j]; + } + } + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + if (n == 1) + { + printf("Cos(A)\n"); + continue; + } + printf("%lldCos^%d(A)", T[n][n], n); + for (i = n - 2; i > 1; i -= 2) + { + if (T[n][i] >= 0) + { + printf("+%lldCos^%d(A)", T[n][i], i); + } + else + { + printf("%lldCos^%d(A)", T[n][i], i); + } + } + if (i) + { + if (T[n][1] > 0) + { + printf("+"); + } + printf("%dCos(A)", T[n][1]); + } + else if (T[n][0] > 0) + { + printf("+1"); + } + else + { + printf("-1"); + } + printf("\n"); + } + return 0; +} diff --git a/11171.cpp b/11171.cpp new file mode 100644 index 0000000..1bf0183 --- /dev/null +++ b/11171.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define INF 0x3fffffff + +const vector dict = {'2', '2', '2', '3', '3', '3', '4', '4', '4', '5', '5', '5', '6', '6', '6', '7', '7', '7', '7', '8', '8', '8', '9', '9', '9', '9'}; + +struct trie_node +{ + trie_node() + { + for (int i = 0; i < 30; i++) + { + child[i] = NULL; + } + print = ""; + order = -1; + } + int order; + string print; + trie_node *child[30]; +}; +trie_node *root; +pair, int> dp[250002]; + +string s; +map key_to_num; + +string int_to_string(int i) +{ + stringstream s; + s << i; + return s.str(); +} + +void solve() +{ + int best, pos, next_d; + trie_node *next_t; + for (int idx = s.length() - 1; idx >= 0; idx--) + { + best = INF; + pos = idx; + trie_node *cur = root; + while (1) + { + cur = cur->child[s[pos] - 'a']; + if (cur == NULL) + { + break; + } + if (cur->order != -1) + { + int sec = cur->order; + if (pos != s.length() - 1) + { + sec += 1 + dp[pos + 1].second; + } + if (sec < best) + { + best = sec; + next_t = cur; + next_d = pos + 1; + } + } + pos++; + if (pos == s.length()) + { + break; + } + } + if (best < INF) + { + dp[idx] = {{next_t, next_d}, best}; + } + } +} + +void append_ud(trie_node *cur) +{ + if (cur->order != 0) + { + int up = cur->order; + int down = key_to_num[cur->print] - up; + int best = min(up, down); + cur->order = best + cur->print.length(); + cur->print += down < up ? 'D' : 'U'; + cur->print += '(' + int_to_string(best) + ')'; + } + else + { + cur->order += cur->print.length(); + } +} + +int main() +{ + int ds; + while (scanf("%d", &ds) == 1) + { + if (!ds) + { + break; + } + root = new trie_node(); + key_to_num.clear(); + vector nodes; + for (int i = 0; i < ds; i++) + { + cin >> s; + trie_node *cur = root; + string key = ""; + for (int j = 0; j < s.length(); j++) + { + const int idx = s[j] - 'a'; + key += dict[idx]; + if (cur->child[idx] == NULL) + { + cur->child[idx] = new trie_node(); + } + cur = cur->child[idx]; + } + if (key_to_num.count(key)) + { + cur->order = key_to_num[key]++; + } + else + { + cur->order = 0; + key_to_num[key] = 1; + } + cur->print = key; + nodes.push_back(cur); + } + for (trie_node *tn : nodes) + { + append_ud(tn); + } + int q; + scanf("%d", &q); + for (int i = 0; i < q; i++) + { + cin >> s; + for (int j = 0; j < s.length() + 1; j++) + { + dp[j] = {{NULL, INF}, INF}; + } + solve(); + string res = ""; + pair &cur = dp[0].first; + while (1) + { + if (cur.first == NULL) + { + break; + } + res += cur.first->print + 'R'; + cur = dp[cur.second].first; + } + printf("%s\n", res.substr(0, res.length() - 1).c_str()); + } + } + return 0; +} diff --git a/11172.cpp b/11172.cpp index 3d90e6f..48fe824 100644 --- a/11172.cpp +++ b/11172.cpp @@ -1,51 +1,27 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int main(){ - int T; - cin >> T; - while( T-- ){ - int64 A, B; - cin >> A >> B; - if( A < B ) cout << "<"; - else if( A > B ) cout << ">"; - else cout << "="; - cout << endl; - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + int a, b; + cin >> n; + while (n--) + { + cin >> a >> b; + if (a < b) + { + cout << "<" << endl; + } + else if (a > b) + { + cout << ">" << endl; + } + else + { + cout << "=" << endl; + } + } + return 0; +} diff --git a/11173.cpp b/11173.cpp new file mode 100644 index 0000000..b25aa67 --- /dev/null +++ b/11173.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + long b, num, bit, t; + scanf("%ld", &b); + while (b--) + { + scanf("%ld%ld", &bit, &num); + printf("%ld\n", num ^ (num >> 1)); + } + return 0; +} diff --git a/11174.cpp b/11174.cpp new file mode 100644 index 0000000..d2f174f --- /dev/null +++ b/11174.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +typedef long long int INT64; + +const int MOD = 1000000007; + +int n, m; +INT64 f[40001], b[40001]; + +INT64 extgcd(INT64 a, INT64 b, INT64 &x, INT64 &y) +{ + // ax + by = d + if (b == 0) + { + x = 1; + y = 0; + return a; + } + else + { + // b * x' + (a % b) * y' = d + // b * x' + [a - (a / b) * b] * y' = d + // a * y' + b * [x' - (a / b) * y' ] = d + // a * x + b * y = d + INT64 tx, ty, d; + d = extgcd(b, a % b, tx, ty); + x = ty; + y = tx - (a / b) * ty; + return d; + } +} +void init() +{ + INT64 inv, tmp; + f[0] = 1; + f[1] = b[1] = 1; + for (int i = 2; i <= 40000; i++) + { + extgcd(i, MOD, inv, tmp); + while (inv < 0) + { + inv += MOD; + } + inv %= MOD; + f[i] = (f[i - 1] * i) % MOD; + b[i] = (b[i - 1] * inv) % MOD; + } +} +struct Node +{ + int childs; + int count; + INT64 value; + Node *father; + Node *son; + Node *next; +} Nodes[40001]; +void AddSon(int f, int s) +{ + Nodes[s].father = &Nodes[f]; + Nodes[s].next = Nodes[f].son; + Nodes[f].son = &Nodes[s]; + Nodes[f].childs++; +} +void CulNode(Node *root) +{ + root->count = 0; + root->value = 1; + for (Node *p = root->son; p; p = p->next) + { + CulNode(p); + root->count += p->count + 1; + root->value = (root->value * p->value) % MOD; + root->value = (root->value * b[p->count + 1]) % MOD; + } + root->value = (root->value * f[root->count]) % MOD; +} + +int main() +{ + init(); + int t, f, s; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i <= n; i++) + { + Nodes[i].childs = 0; + Nodes[i].father = Nodes[i].son = Nodes[i].next = NULL; + } + for (int i = 0; i < m; i++) + { + scanf("%d %d", &s, &f); + AddSon(f, s); + } + for (int i = 1; i <= n; i++) + { + if (Nodes[i].father == NULL) + { + AddSon(0, i); + } + } + CulNode(&Nodes[0]); + printf("%lld\n", Nodes[0].value); + } +} diff --git a/11175.cpp b/11175.cpp new file mode 100644 index 0000000..330832d --- /dev/null +++ b/11175.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int N, P, id[1024]; + +int ufind[1024]; +int f(int x) +{ + int z; + for (z = x; ufind[z] != z; z = ufind[z]) + ; + while (x != z) + { + int y = ufind[x]; + ufind[x] = z; + x = y; + } + return z; +} + +int main() +{ + int T, P; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d %d", &N, &P) == 2; cs++) + { + N *= 2; + for (int i = 0; i < N; i++) + { + ufind[i] = i; + } + int *edges0 = new int[P + 10]; + for (int i = 0; i < P; i++) + { + int x, y; + scanf("%d %d", &x, &y); + edges0[i] = x * 65536 + y; + //printf("edge %d->%d [%d%d->%d%d] --> %d=%d\n", x,y, 2*x,2*x+1, 2*y,2*y+1, 2*x+1,2*y); + x = 2 * x + 1; + y = 2 * y; + ufind[f(x)] = f(y); + } + // for (int i = 0; i < N; i += 2) printf(" %d: %d -> %d\n", i/2, f(i), f(i+1)); + int P1 = 0; + int *edges1 = new int[P + 10]; + for (int i = 0; i < N; i += 2) + { + for (int j = 0; j < N; j += 2) + { + if (f(i + 1) != f(j)) + { + continue; + } + //printf("must exist edge %d->%d\n", i/2, j/2); + edges1[P1++] = (i / 2) * 65536 + (j / 2); + if (P1 > P) + { + goto stop; + } + } + } + stop: + bool ans = true; + if (P != P1) + { + ans = false; + } + else + { + sort(edges0, edges0 + P); + sort(edges1, edges1 + P); + if (memcmp(edges0, edges1, P * sizeof(int)) != 0) + { + ans = false; + } + } + printf("Case #%d: %s\n", cs, ans ? "Yes" : "No"); + delete[] edges0; + delete[] edges1; + } +} diff --git a/11176.cpp b/11176.cpp new file mode 100644 index 0000000..304a1f2 --- /dev/null +++ b/11176.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define MAXD 510 +int N; +double P, p[MAXD][MAXD], d[MAXD]; +void solve() +{ + int i, j, k; + double res = 0, temp; + d[0] = 1; + for (i = 1; i <= N; i++) + { + d[i] = d[i - 1] * P; + } + for (i = 0; i <= N; i++) + { + p[0][i] = 1; + } + for (i = 1; i <= N; i++) + for (j = 0; j <= N; j++) + { + p[i][j] = p[i - 1][j]; + if (j == i - 1) + { + p[i][j] -= d[j + 1]; + } + else if (j < i - 1) + { + p[i][j] -= p[i - j - 2][j] * (1 - P) * d[j + 1]; + } + } + for (i = 1; i <= N; i++) + { + res += i * (p[N][i] - p[N][i - 1]); + } + printf("%.6lf\n", res); +} +int main() +{ + for (;;) + { + scanf("%d%lf", &N, &P); + if (!N) + { + break; + } + solve(); + } + return 0; +} diff --git a/11177.cpp b/11177.cpp new file mode 100644 index 0000000..ddbca55 --- /dev/null +++ b/11177.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +const double EPS = 1e-6; + +struct Point +{ + double x, y; + Point(double X = 0.0, double Y = 0.0) : x(X), y(Y) {} + Point operator-(const Point &p) const + { + return Point(x - p.x, y - p.y); + } +}; + +double cross(const Point &a, const Point &b) +{ + return a.x * b.y - a.y * b.x; +} +double dot(const Point &a, const Point &b) +{ + return a.x * b.x + a.y * b.y; +} + +double angle(const Point &a, const Point &b) +{ + double cs = dot(a, b) / sqrt(dot(a, a)) / sqrt(dot(b, b)); + return acos(min(max(cs, -1.0), 1.0)); +} + +double area_intersect(double R, Point P[], int N) +{ + double ans = 0; + for (int i = 0; i < N; i++) + { + Point A = P[i], B = P[(i + 1) % N]; + if (cross(A, B) < 0) + { + swap(A, B); + } + double a = dot(B - A, B - A), b = 2 * dot(A, B - A), c = dot(A, A) - R * R; + if (fabs(a) < EPS) + { + continue; + } + double v[10]; + int k = 0; + v[k++] = 0; + v[k++] = 1; + double det = b * b - 4 * a * c; + if (det > EPS) + { + for (int sgn = -1; sgn <= 1; sgn += 2) + { + double t = (-b + sgn * sqrt(det)) / (2 * a); + if (EPS < t && t < 1 - EPS) + { + v[k++] = t; + } + } + } + sort(v, v + k); + Point Q[10]; + for (int i = 0; i < k; i++) + { + Q[i].x = A.x + v[i] * (B.x - A.x); + Q[i].y = A.y + v[i] * (B.y - A.y); + } + for (int i = 0; i < k - 1; i++) + { + if (dot(Q[i], Q[i]) < R * R + EPS && dot(Q[i + 1], Q[i + 1]) < R * R + EPS) + { + ans += 0.5 * fabs(cross(Q[i], Q[i + 1])); + } + else + { + ans += 0.5 * R * R * angle(Q[i], Q[i + 1]); + } + } + } + return ans; +} + +int main() +{ + double area_min; + Point P[64]; + int N; + for (int cs = 1; scanf("%d%lf", &N, &area_min) == 2 && N > 0; cs++) + { + for (int i = 0; i < N; i++) + { + scanf("%lf%lf", &P[i].x, &P[i].y); + } + P[N] = P[0]; + + double area = 0.0; + for (int i = 0; i < N; i++) + { + area += cross(P[i], P[i + 1]); + } + area = fabs(area / 2.0); + + double lo = sqrt(max(0.0, area_min / M_PI)), hi = 0.0; + for (int i = 0; i < N; i++) + { + hi = max(hi, max(fabs(P[i].x), fabs(P[i].y))); + } + while (hi - lo > EPS) + { + double mid = (lo + hi) / 2.0; + if (area_intersect(mid, P, N) < area_min) + { + lo = mid; + } + else + { + hi = mid; + } + } + + printf("Case %d: %.2lf\n", cs, lo); + } + return 0; +} diff --git a/11178.cpp b/11178.cpp new file mode 100644 index 0000000..5b80143 --- /dev/null +++ b/11178.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; + Point(double x = 0.0, double y = 0.0) : x(x), y(y) {} +}; +typedef Point Vector; +Vector operator+(Vector A, Vector B) +{ + return Vector(A.x + B.x, A.y + B.y); +} +Vector operator-(Vector A, Vector B) +{ + return Vector(A.x - B.x, A.y - B.y); +} +Vector operator*(Vector A, double p) +{ + return Vector(A.x * p, A.y * p); +} +Vector operator/(Vector A, double p) +{ + return Vector(A.x / p, A.y / p); +} +double Dot(Vector A, Vector B) +{ + return A.x * B.x + A.y * B.y; +} +double Length(Vector A) +{ + return sqrt(Dot(A, A)); +} +double Angle(Vector A, Vector B) +{ + return acos(Dot(A, B) / Length(A) / Length(B)); +} +double Cross(Vector A, Vector B) +{ + return A.x * B.y - A.y * B.x; +} +double Area(Point A, Point B, Point C) +{ + return Cross(B - A, C - A); +} +Vector Rotate(Vector A, double rad) +{ + return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); +} +Point GetPoint(Point P, Vector v, Point Q, Vector w) +{ + Vector u = P - Q; + double t = Cross(w, u) / Cross(v, w); + return P + v * t; +} + +Point GetAns(Point A, Point B, Point C) +{ + Vector v1 = C - B; + double a1 = Angle(A - B, v1); + v1 = Rotate(v1, a1 / 3); + Vector v2 = B - C; + double a2 = Angle(A - C, v2); + v2 = Rotate(v2, -a2 / 3); + return GetPoint(B, v1, C, v2); +} + +int main() +{ + int test; + Point A, B, C, D, E, F; + scanf("%d", &test); + for (int cas = 1; cas <= test; cas++) + { + scanf("%lf%lf", &A.x, &A.y); + scanf("%lf%lf", &B.x, &B.y); + scanf("%lf%lf", &C.x, &C.y); + D = GetAns(A, B, C); + E = GetAns(B, C, A); + F = GetAns(C, A, B); + printf("%.6lf %.6lf ", D.x, D.y); + printf("%.6lf %.6lf ", E.x, E.y); + printf("%.6lf %.6lf\n", F.x, F.y); + } + return 0; +} diff --git a/11179.cpp b/11179.cpp new file mode 100644 index 0000000..1ee7b11 --- /dev/null +++ b/11179.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +typedef unsigned long long int int64; + +#define MAXSIZE 65537 + +int b, len, bit; +int64 pow_[MAXSIZE], state, L, n, m, C, v; + +int64 pow_mod(int64 base, int64 exp, int64 mod) +{ + if (exp < MAXSIZE && pow_[exp] != -1) + { + return pow_[exp]; + } + if (exp == 0) + { + return 1; + } + else if (exp & 1) + { + int64 temp = pow_mod(base, exp / 2, mod); + temp = (temp * temp) % mod; + temp = (temp * base) % mod; + if (exp < MAXSIZE) + { + pow_[exp] = temp; + } + return temp; + } + else + { + int64 temp = pow_mod(base, exp / 2, mod); + temp = (temp * temp) % mod; + if (exp < MAXSIZE) + { + pow_[exp] = temp; + } + return temp; + } +} + +int64 series(int s) +{ + if (s == 0) + { + return 1; + } + else if (s & 1) + { + int k = s / 2; + int64 temp = pow_mod(n, k + 1, m), t = series(k); + temp = (temp * t) % m; + temp = (t + temp) % m; + return temp; + } + else + { + int k = s / 2; + int64 temp = pow_mod(n, k, m), temp1 = temp, t = series(k - 1); + temp = (temp * t) % m; + temp = (t + temp) % m; + t = (temp1 * temp1) % m; + temp = (temp + t) % m; + return temp; + } +} + +int main() +{ + int caseno = 1; + while (scanf("%lld", &L) == 1) + { + if (!L) + { + break; + } + scanf("%d %lld %lld %lld", &b, &n, &m, &C); + bit = len = 0; + state = 0; + int64 total = 0; + scanf("%lld", &v); + int64 k = 0; + const int64 onemask = (b == 32) ? 4294967295LL : (1 << b) - 1; + memset(pow_, -1, sizeof(pow_)); + + while (1) + { + total += v; + if (len + v < b) + { + int64 s = (bit) ? (1 << v) - 1 : 0; + state = (state << v) | s; + len += v; + } + else + { + if (len) + { + int leftshift = b - len; + int64 s = (bit) ? (1 << leftshift) - 1 : 0; + state = (state << leftshift) | s; + k = ((k * n) + state) % m; + state = 0; + len = 0; + v -= leftshift; + } + int64 chunks = v / b, left = v % b; + if (chunks) + { + int64 temp = pow_mod(n, chunks, m); + k = (k * temp) % m; + if (bit) + { + int64 t = series(chunks - 1); + t = (onemask * t) % m; + k = (k + t) % m; + } + } + if (left) + { + int64 s = (bit) ? (1 << left) - 1 : 0; + state = (state << left) | s; + len += left; + } + } + bit = !bit; + scanf("%lld", &v); + if (!v) + { + break; + } + } + + if (len) + { + int left = b - len; + state = (state << left); + len += left; + k = ((k * n) + state) % m; + } + + if (total != L) + { + printf("Bitstream %d: Invalid Length\n", caseno++); + continue; + } + if (k == C) + { + printf("Bitstream %d: Compression OK\n", caseno++); + } + else + { + printf("Bitstream %d: Invalid Checksum\n", caseno++); + } + } + return 0; +} diff --git a/1118.cpp b/1118.cpp new file mode 100644 index 0000000..4b4a0f5 --- /dev/null +++ b/1118.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + int t, n, m; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &m); + printf("%d\n", !((n - m) & ((m - 1) / 2))); + } + return 0; +} diff --git a/11180.cpp b/11180.cpp new file mode 100644 index 0000000..57e1273 --- /dev/null +++ b/11180.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int R[60] = {1}, I[60] = {0}, N[60], ptr; +int main() +{ + int i, t, tt, a, b, ta, tb, m; + for (i = 1; i < 60; i++) + { + R[i] = -R[i - 1] - I[i - 1]; + I[i] = R[i - 1] - I[i - 1]; + } + scanf("%d", &tt); + for (t = 1; t <= tt; t++) + { + scanf("%d%d", &a, &b); + printf("Case #%d: ", t); + if ((a | b) == 0) + { + printf("0\n"); + continue; + } + for (ptr = 0; a | b;) + { + m = (1 << (ptr / 2 + 1)); + if (a % m == 0 && b % m == 0) + { + N[ptr++] = 0; + N[ptr++] = 0; + } + else if ((a - R[ptr]) % m == 0 && (b - I[ptr]) % m == 0) + { + a = a - R[ptr]; + b = b - I[ptr]; + N[ptr++] = 1; + N[ptr++] = 0; + } + else if ((a - R[ptr + 1]) % m == 0 && (b - I[ptr + 1]) % m == 0) + { + a = a - R[ptr + 1]; + b = b - I[ptr + 1]; + N[ptr++] = 0; + N[ptr++] = 1; + } + else + { + a = a - R[ptr] - R[ptr + 1]; + b = b - I[ptr] - I[ptr + 1]; + N[ptr++] = 1; + N[ptr++] = 1; + } + } + if (N[ptr - 1] == 0) + { + ptr--; + } + for (i = ptr - 1; i >= 0; i--) + { + printf("%d", N[i]); + } + printf("\n"); + } + return 0; +} diff --git a/11181.cpp b/11181.cpp new file mode 100644 index 0000000..f571f09 --- /dev/null +++ b/11181.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +int countBits(int n) +{ + int sum = 0; + while (n > 0) + { + if ((n & 1) == 1) + { + sum++; + } + n = n >> 1; + } + return sum; +} +int main() +{ + int n, r, i, k, c, t; + double sum; + double cs; + double *ppls; + double *ppl; + c = 1; + scanf("%d %d", &n, &r); + while (n != 0) + { + sum = 0; + ppls = (double *)malloc(n * sizeof(double)); + ppl = (double *)malloc(n * sizeof(double)); + for (i = 0; i < n; i++) + { + scanf("%lf", &ppl[i]); + ppls[i] = 0; + } + t = pow(2, n); + for (i = 0; i < t; i++) + { + if (countBits(i) == r) + { + cs = 1; + for (k = 0; k < n; k++) + { + if ((1 << k) & i) + { + cs *= ppl[k]; + } + else + { + cs *= (1 - ppl[k]); + } + } + for (k = 0; k < n; k++) + { + if ((1 << k) & i) + { + ppls[k] += cs; + } + } + sum += cs; + } + } + printf("Case %d:\n", c); + for (k = 0; k < n; k++) + { + printf("%lf\n", ppls[k] / sum); + } + c++; + scanf("%d %d", &n, &r); + } + return 0; +} diff --git a/11182.cpp b/11182.cpp new file mode 100644 index 0000000..ca4f364 --- /dev/null +++ b/11182.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +typedef long long int int64; +int64 T(int64 a, int64 d, int64 n) +{ + int64 sum = n * (a - d) * (a - d); + sum += d * (a - d) * n * (n + 1); + sum += d * d * n * (n + 1) * (2 * n + 1) / 6; + sum += n * (a - d); + sum += d * n * (n + 1) / 2; + return sum / 2; +} +int64 n, b; +int64 bases[100], c[100]; +int len; +void factorize(int64 value) +{ + len = 0; + for (int i = 2; i * i <= value; i++) + if (value % i == 0) + { + bases[len] = i; + int64 s = 0; + while (value % i == 0) + { + value /= i; + s++; + } + c[len++] = s; + } + if (value > 1) + { + bases[len] = value; + c[len++] = 1; + } +} +int main() +{ + while (scanf("%lld %lld", &n, &b) == 2) + { + if (!n && !b) + { + break; + } + factorize(b); + int64 min = -1; + for (int i = 0; i < len; i++) + { + int64 sum = 0; + for (int64 s = bases[i]; s <= n; s *= bases[i]) + { + sum += T((n + 1) - (n / s) * s, s, n / s); + } + if (min == -1 || (sum / c[i]) < min) + { + min = sum / c[i]; + } + } + printf("%lld\n", min); + } +} diff --git a/11183.cpp b/11183.cpp new file mode 100644 index 0000000..6294145 --- /dev/null +++ b/11183.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +const int mm = 44444; +const int mn = 1111; + +struct edge +{ + int s, t, w; +} g[mm], h[mm]; + +bool huan; +int head[mm], nxt[mm], p[mn], q[mn], mark[mn], fp[mn], from[mn], vis[mn], in[mn], w[mn], ans, sum, i, j, k, n, m, e, r, mw, t, cas = 0; + +inline void addedge(int u, int v, double c) +{ + g[e].s = u, g[e].t = v, g[e].w = c, nxt[e] = head[u], head[u] = e++; + if (c > mw) + { + mw = c; + } +} +void dfs(int u) +{ + ++sum, vis[u] = 1; + for (int i = head[u]; i >= 0; i = nxt[i]) + if (!vis[g[i].t]) + { + dfs(g[i].t); + } +} +inline void init(int &a) +{ + char ch = getchar(); + while (ch < '0' || ch > '9') + { + ch = getchar(); + } + for (a = 0; ch >= '0' && ch <= '9'; ch = getchar()) + { + a = a * 10 + ch - 48; + } +} +void mysort() +{ + int i; + for (i = 0; i <= n; ++i) + { + q[i] = 0; + } + for (i = 0; i <= mw; ++i) + { + p[i] = 0; + } + for (i = 0; i < e; ++i) + { + ++q[g[i].t], ++p[g[i].w]; + } + for (i = 1; i <= n; ++i) + { + q[i] += q[i - 1]; + } + for (i = n; i > 0; --i) + { + q[i] = q[i - 1]; + } + q[0] = 0; + for (i = 1; i <= mw; ++i) + { + p[i] += p[i - 1]; + } + for (i = mw; i > 0; --i) + { + p[i] = p[i - 1]; + } + p[0] = 0; + for (i = 0; i < e; ++i) + { + h[p[g[i].w]++] = g[i]; + } + for (i = 0; i < e; ++i) + { + g[q[h[i].t]++] = h[i]; + } +} +int main() +{ + init(t); + while (t--) + { + init(n), init(m), --n; + for (i = e = mw = 0; i <= n; ++i) + { + head[i] = -1; + } + while (m--) + { + init(i), init(j), init(k); + if (i != j) + { + addedge(i, j, k); + } + } + for (sum = i = 0; i <= n; ++i) + { + vis[i] = 0; + } + dfs(0); + if (sum < n + 1) + { + printf("Case #%d: Possums!\n", ++cas); + continue; + } + mysort(); + for (i = 0; i <= n; ++i) + { + fp[i] = p[i] = -1, in[i] = vis[i] = 0, mark[i] = i; + } + for (i = 0; i < e; ++i) + if (p[g[i].t] < 0) + { + p[g[i].t] = i; + } + huan = 1, ans = sum = 0; + while (huan) + { + huan = 0; + for (i = 1; i <= n; ++i) + if (fp[j = mark[i]] >= 0) + { + if (fp[i] < 0) + { + in[i] += w[j], mark[i] = mark[mark[i]]; + } + else + { + in[i] += w[i], ans += w[i]; + if (g[++p[fp[i]]].t != fp[i]) + { + p[fp[i]] = -1; + } + } + } + for (i = 0; i <= n; ++i) + { + fp[i] = -1, vis[i] = 0; + } + for (i = 1; i <= n; ++i) + if (p[i] >= 0) + { + if (fp[j = mark[i]] < 0 || (fp[j] >= 0 && w[j] > g[p[i]].w - in[i])) + { + w[j] = g[p[i]].w - in[i], fp[j] = i, from[j] = mark[g[p[i]].s]; + } + } + for (sum = 0, i = 1; i <= n; ++i) + if (fp[i] >= 0) + { + sum += w[i]; + } + for (i = 1; i <= n; ++i) + if (!vis[i]) + { + r = 0, j = i; + while (j > 0 && vis[j] >= 0) + { + if (vis[j] > 0) + { + huan = 1; + while (q[--r] != j) + { + mark[q[r]] = j, vis[q[r]] = -1; + } + vis[j] = -1; + } + else if (!vis[j]) + { + vis[q[r++] = j] = 1; + } + if (fp[j] >= 0) + { + j = from[j]; + } + else + { + j = -1; + } + } + while (r--) + { + vis[q[r]] = fp[q[r]] = -1; + } + } + } + printf("Case #%d: %d\n", ++cas, ans + sum); + } + return 0; +} diff --git a/11184.cpp b/11184.cpp new file mode 100644 index 0000000..cf4f59f --- /dev/null +++ b/11184.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int seq[1000], n; +int main() +{ + int caseno = 1; + while (scanf("%d", &n) == 1, n) + { + if (n % 4 == 1 || n % 4 == 2) + { + printf("Case %d: -1\n", caseno++); + continue; + } + else if (n % 4 == 0) + { + memset(seq, 0, sizeof(seq)); + int s = 1, p; + seq[0] = s++; + for (p = n - 2; p >= n / 2; p -= 2) + { + seq[p] = s++; + } + for (p++; p >= 1; p -= 2) + { + seq[p] = s++; + } + for (p = 0; seq[p] != 0; p++) + ; + seq[p] = ++s; + for (p++; p < n; p++) + if (seq[p] == 0) + { + seq[p] = ++s; + } + } + else + { + memset(seq, 0, sizeof(seq)); + seq[0] = 1; + int s = 2, p; + bool flag = false; + for (p = n - 2; p >= 1; p -= 2) + { + seq[p] = s++; + } + for (p = 2; p < n; p += 2) + { + if (p >= n / 2 && !flag) + { + s++; + flag = true; + } + seq[p] = s++; + } + } + printf("Case %d:", caseno++); + for (int i = 0; i < n; i++) + { + printf(" %d", seq[i]); + } + puts(""); + } +} diff --git a/11185.cpp b/11185.cpp index 088cd1e..fd8726f 100644 --- a/11185.cpp +++ b/11185.cpp @@ -1,52 +1,33 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; -string ref = "0123456789"; -int main(){ - while(true){ - int64 N; - cin>>N; - if(N < 0) - break; - else if(N == 0) - cout<<"0\n"; - else{ - string ter=""; - while(N>0){ - int64 rem = N%3; - ter = ref[rem] + ter; - N /= 3; - } - cout< + +using namespace std; + +string convert(int n, int base) +{ + if (n == 0) + { + return "0"; + } + char NUMS[] = "0123456789ABCDEF"; + string result = ""; + do + { + result.push_back(NUMS[n % base]); + n /= base; + } while (n); + return string(result.rbegin(), result.rend()); +} + +int main() +{ + int n; + while (cin >> n) + { + if (n < 0) + { + break; + } + cout << convert(n, 3) << endl; + } + return 0; +} diff --git a/11186.cpp b/11186.cpp new file mode 100644 index 0000000..cbb6fcd --- /dev/null +++ b/11186.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +double r, s, p[555]; + +int main() +{ + int n; + while (scanf("%d%lf", &n, &r) == 2 && n) + { + s = 0; + for (int i = 0; i < n; ++i) + { + scanf("%lf", &p[i]); + p[i] *= M_PI / 180; + } + sort(p, p + n); + for (int i = 0; i < n; ++i) + { + for (int j = i + 1; j < n; ++j) + { + s += (n + 2 * i - 2 * j) * sin(p[j] - p[i]); + } + } + s *= r * r / 2; + printf("%.0lf\n", s); + } + return 0; +} diff --git a/11187.cpp b/11187.cpp new file mode 100644 index 0000000..a050922 --- /dev/null +++ b/11187.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +char chk[20], chk2[1201][1201]; +int need[20], map_[20][20], dis[20], queue_[1000000], queue_2[1000000]; + +int main() +{ + int cas, cnt, n, m, o, i, j, k, l, p, q, temp, temp2, sum, ans; + cas = 0; + scanf("%d", &cnt); + while (cnt--) + { + scanf("%d", &n); + for (i = 1; i < n; i++) + { + scanf("%d", &need[i]); + } + memset(map_, 0, sizeof(map_)); + while (scanf("%d%d%d", &i, &j, &k) == 3) + { + if (!(i + j + k)) + { + break; + } + i--; + j--; + map_[i][j] = map_[j][i] = k; + } + for (i = 1; i < n; i++) + { + dis[i] = map_[0][i]; + chk[i] = 1; + } + for (i = 1, sum = 0; i < n; i++) + { + for (p = 1, j = 2147483647; p < n; p++) + { + if (chk[p] && dis[p] && dis[p] < j) + { + j = dis[p]; + k = p; + } + } + chk[k] = 0; + for (p = 1; p < n; p++) + { + if (chk[p] && map_[k][p] && (dis[k] + map_[k][p] < dis[p] || !dis[p])) + { + dis[p] = dis[k] + map_[k][p]; + } + } + dis[k] *= 2; + sum += need[k] * dis[k]; + } + if (sum > 3600) + { + printf("Test Case #: %d\nToo few trucks\n", ++cas); + } + else + { + queue_[0] = queue_2[0] = 0; + memset(chk2, 1, sizeof(chk2)); + for (i = 1, q = 1, ans = sum; i < n; i++) + { + temp = dis[i]; + for (j = 0, m = -1; j < need[i]; j++) + { + for (p = o = q - 1; p > m; p--) + { + k = queue_[p] + temp; + l = queue_2[p]; + if (k <= l) + { + if (chk2[k][l]) + { + chk2[k][l] = 0; + queue_[q] = k; + queue_2[q++] = l; + if (ans > max(k, max(l, sum - k - l))) + { + ans = max(k, max(l, sum - k - l)); + } + } + } + k = queue_[p]; + l = queue_2[p] + temp; + if (l <= 1200) + { + if (chk2[k][l]) + { + chk2[k][l] = 0; + queue_[q] = k; + queue_2[q++] = l; + if (ans > max(k, max(l, sum - k - l))) + { + ans = max(k, max(l, sum - k - l)); + } + } + } + } + m = o; + } + } + if (ans > 1200) + { + printf("Test Case #: %d\nToo few trucks\n", ++cas); + } + else + { + printf("Test Case #: %d\n%d mins\n", ++cas, ans); + } + } + } + return 0; +} diff --git a/11189.cpp b/11189.cpp new file mode 100644 index 0000000..0883152 --- /dev/null +++ b/11189.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} + Point operator+(const Point &p) const + { + return Point(x + p.x, y + p.y); + } + Point operator-(const Point &p) const + { + return Point(x - p.x, y - p.y); + } +}; + +double dot(const Point &A, const Point &B) +{ + return A.x * B.x + A.y * B.y; +} + +double PI = 2 * acos(0.0), R; +Point P[10]; + +int solve() +{ + double EPS = 1e-9; + if (R < EPS) + { + return 0; + } + double area = 0; + for (int i = 0; i < 4; i++) + { + area += P[i].x * P[(i + 1) % 4].y - P[(i + 1) % 4].x * P[i].y; + } + if (area < 0) + { + area = -area; + reverse(P, P + 4); + } + area /= 2; + Point O; + P[4] = P[0]; + for (int i = 0; i < 4; i++) + { + O.x += (P[i].x + P[i + 1].x) / 3 * (P[i].x * P[i + 1].y - P[i + 1].x * P[i].y) / 2; + O.y += (P[i].y + P[i + 1].y) / 3 * (P[i].x * P[i + 1].y - P[i + 1].x * P[i].y) / 2; + } + O.x /= area; + O.y /= area; + for (int i = 0; i < 4; i++) + { + P[i].x -= O.x; + P[i].y -= O.y; + P[4 + i] = P[i]; + } + //printf("O=(%.5f, %.5f) R=%.5f area=%.5f\n", O.x, O.y, R, area); + vector isec; + for (int i = 0; i < 4; i++) + { + double d = sqrt(dot(P[i], P[i])); + if (fabs(d - R) < EPS) + { + isec.push_back(P[i]); + } + } + for (int i = 0; i < 4; i++) + { + Point A = P[i], B = P[i + 1]; + double a = dot(B - A, B - A), b = 2 * dot(B - A, A), c = dot(A, A) - R * R; + if (fabs(a) < EPS) + { + continue; + } + double det = b * b - 4 * a * c; + if (det < -EPS) + { + continue; + } + if (det < 1e-9) + { + det = 0; + } + for (int sign = -1; sign <= 1; sign += 2) + { + double t = (-b + sign * sqrt(det)) / (2 * a); + if (t < EPS || t > 1 - EPS) + { + continue; + } + Point p; + p.x = A.x + t * (B.x - A.x); + p.y = A.y + t * (B.y - A.y); + isec.push_back(p); + } + } + int res = 0; + for (int i = 0; i < (int)isec.size(); i++) + { + for (int j = 0;; j++) + { + if (j == i) + { + res++; + break; + } + if (fabs(isec[i].x - isec[j].x) < EPS && fabs(isec[i].y - isec[j].y) < EPS) + { + break; + } + } + } + return res; +} + +int main() +{ + double len, T; + for (int cs = 1; scanf("%lf %lf", &len, &T) == 2 && len > 0.01; cs++) + { + for (int i = 0; i < 4; i++) + { + scanf("%lf %lf", &P[i].x, &P[i].y); + } + R = T * T / (4 * PI * PI) * 981 - len; + int res = solve(); + printf("Pendulum %d: ", cs); + if (res <= 0) + { + printf("No\n"); + } + else + { + printf("Yes %d\n", res); + } + } +} diff --git a/1119.cpp b/1119.cpp new file mode 100644 index 0000000..3c6591f --- /dev/null +++ b/1119.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int main() +{ + priority_queue, greater> pQ; + vector g[105]; + int t, M, N, indeg[105]; + int i, j, x, y, z; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &N, &M); + for (i = 0; i <= N; i++) + g[i].clear(), indeg[i] = 0; + while (M--) + { + scanf("%d %d", &x, &z); + while (z--) + { + scanf("%d", &y); + g[y].push_back(x); + indeg[x]++; + } + } + for (i = 1; i <= N; i++) + if (indeg[i] == 0) + pQ.push(i); + int tn, flag = 0; + while (!pQ.empty()) + { + if (flag) + putchar(' '); + flag = 1; + tn = pQ.top(), pQ.pop(); + printf("%d", tn); + for (vector::iterator it = g[tn].begin(); + it != g[tn].end(); it++) + if (--indeg[*it] == 0) + pQ.push(*it); + } + puts(""); + if (t) + puts(""); + } + return 0; +} diff --git a/11190.cpp b/11190.cpp new file mode 100644 index 0000000..47025d6 --- /dev/null +++ b/11190.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +int main() +{ + int t = 0, L, H, K; + while (scanf("%d%d%d", &L, &H, &K) == 3 && L >= 0) + { + double m = 0.0f, n = 0.0f, tt = 0.0f, exp = 0.0f; + for (int i = H; i >= L; i--) + { + double v = log10(i) * K; + exp = max(exp, floor(v)); + } + for (int i = H; i >= L; i--) + { + double v = log10(i) * K - exp; + tt += pow(10, v); + } + while (tt >= 1) + { + tt /= 10, exp++; + } + if (fabs(t) < EPS && exp == 0) + { + exp = 1; + } + printf("Case %04d: %.6lfe%010lld\n", ++t, tt, (long long)exp); + } + return 0; +} diff --git a/11191.cpp b/11191.cpp new file mode 100644 index 0000000..28bbc61 --- /dev/null +++ b/11191.cpp @@ -0,0 +1,186 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef long long ll; + +const int N = (1 << 11); +ll inp[N]; +ll pi[N]; + +void solve(ll zero, ll num) +{ + ll ret = 0, ret2 = 0; + rep(i, N) + { + pi[i] = 0; + } + ll sq[2] = {0}; + REP(i, 0, N) + { + if (inp[i] > 1) + { + ret += inp[i] * (inp[i] - 1) / 2; + } + rep(j, N) + { + int a = i, b = j, c = i ^ j; + if (a <= b && b <= c) + ; + else + { + continue; + } + if ((i ^ j) == (1 << 10) && inp[i] > 1 && i < j) + { + sq[1] += inp[i] * (inp[j]); + } + if ((i ^ j) == 0 && inp[i] > 1) + { + sq[0] += inp[i] * (inp[i] - 1) / 2; + } + if (i == j) + { + if (i == 0) + { + if (inp[i] > 2) + { + ret2 += (inp[i] * (inp[i] - 1) * (inp[i ^ j] - 2)) / 6; + } + } + else if (inp[i] > 0) + { + ret2 += (inp[i] * (inp[i] - 1) * inp[i ^ i]) / 2; + } + } + else + { + if (j == (i ^ j)) + { + if (inp[j] > 1) + { + ret2 += (inp[i] * inp[j] * (inp[j] - 1)) / 2; + } + } + else if (i == (i ^ j) && i < j) + { + if (inp[i] > 1) + { + ret2 += (inp[i] * inp[j] * (inp[i] - 1)) / 2; + } + } + else if (inp[i] != 0 && inp[j] != 0 && inp[i ^ j] != 0) + { + ret2 += (inp[i] * inp[j] * inp[i ^ j]); + } + } + } + } + //ret2/=6; + if (zero >= 1) + { + ret += zero * num; + ret2 += zero * num * (num - 1) / 2; + } + if (zero >= 2) + { + ret += zero * (zero - 1) / 2; + ret2 += zero * (zero - 1) / 2 * num; + } + if (zero >= 3) + { + ret2 += (zero) * (zero - 1) * (zero - 2) / 6; + } + cout << ret << " " << ret2 << endl; +} + +int prime[100]; +int generate() +{ + int index = 0; + REP(i, 2, 31) + { + bool isp = true; + REP(j, 2, i) + if (i % j == 0) + { + isp = false; + } + if (isp) + { + prime[index++] = i; + } + } + return index; +} + +int getdata(ll n, int index) +{ + int ret = 0; + if (n < 0) + { + n *= -1, ret |= (1 << 10); + } + else if (n == 0) + { + return 0; + } + rep(i, index) + { + if (n % prime[i] == 0) + { + int cnt = 0; + while (n % prime[i] == 0 && n / prime[i] > 0) + { + n /= prime[i], cnt++; + } + if (cnt % 2 == 1) + { + ret |= (1 << i); + } + } + } + return ret; +} + +main() +{ + int index = generate(); + int te; + cin >> te; + rep(tc, te) + { + rep(i, N) inp[i] = 0; + int n; + cin >> n; + ll zero = 0; + ll num = n; + rep(i, n) + { + ll tmp; + cin >> tmp; + if (tmp == 1) + { + inp[0]++; + } + else if (tmp == -1) + { + inp[(1 << 10)]++; + } + else if (tmp == 0) + { + zero++, num--; + } + else + { + int a = getdata(tmp, index); + inp[a]++; + } + } + solve(zero, num); + } + return false; +} diff --git a/11192.cpp b/11192.cpp index 55f1076..af70dd3 100644 --- a/11192.cpp +++ b/11192.cpp @@ -1,54 +1,25 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int G; - cin >> G; - if (G == 0) +int main() +{ + int g; + int gsize; + string input; + while (cin >> g) + { + if (g == 0) + { break; - string str, ret = ""; - cin >> str; - int L = str.length(); - int N = L / G; - int i, j; - for (i = 0; i < L; i += N){ - string temp = ""; - FOI(j, i, i + N - 1) - temp = str[j] + temp; - ret += temp; } - cout << ret << endl; + cin >> input; + gsize = input.size() / g; + for (int i = 0; i * gsize < input.size(); i++) + { + reverse(input.begin() + i * gsize, input.begin() + i * gsize + gsize); + } + cout << input << endl; } return 0; } - diff --git a/11193.cpp b/11193.cpp new file mode 100644 index 0000000..93a0994 --- /dev/null +++ b/11193.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int cost[256]; + +int reduce(int *v, int N, int pri, int sec) +{ + if (N <= 1) + { + return v[0]; + } + const int INF = 0x3fffffff; + static int f[512][512]; + for (int i = 0; i < N; i++) + { + f[1][i] = v[i] >= 0 ? v[i] : INF; + } + for (int len = 2; len <= N; len++) + { + for (int s = 0; s + len <= N; s++) + { + f[len][s] = INF; + if (v[s + len - 1] < 0) + { + f[len][s] = MIN(f[len][s], MAX(f[len - 1][s], -v[s + len - 1] - 1) + sec); + } + for (int m = 1; m < len; m++) + { + f[len][s] = MIN(f[len][s], MAX(f[m][s], f[len - m][s + m]) + pri); + } + } + } + return f[N][0]; +} + +int expr(int level = 0) +{ + char ch; + if (level < 2) + { + char primary = (level == 0 ? '+' : '*'); + char secondary = (level == 0 ? '-' : '/'); + int v[512], n = 0; + v[n++] = expr(level + 1); + for (;;) + { + if (scanf(" %c", &ch) != 1) + { + break; + } + if (ch == primary) + { + v[n++] = expr(level + 1); + } + else if (ch == secondary) + { + v[n++] = -1 - expr(level + 1); + } + else + { + ungetc(ch, stdin); + break; + } + } + return reduce(v, n, cost[primary], cost[secondary]); + } + else + { + scanf(" %c", &ch); + if (ch == '(') + { + int res = expr(0); + scanf(" )"); + return res; + } + ungetc(ch, stdin); + scanf(" %*d"); + return 0; + } +} + +int main() +{ + while (scanf("%d %d %d %d", &cost['+'], &cost['-'], &cost['*'], &cost['/']) == 4) + { + if (cost['+'] == 0 && cost['-'] == 0 && cost['*'] == 0 && cost['/'] == 0) + { + break; + } + printf("%d\n", expr()); + } +} diff --git a/11195.cpp b/11195.cpp new file mode 100644 index 0000000..9894fa2 --- /dev/null +++ b/11195.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int y_board[20] = {0}; +int answer = 0; + +void backtracking(int n, int y, int x, int slash1, int slash2) +{ + if (y == n) + { + answer++; + return; + } + int nowslash1 = slash1 >> y; + int nowslash2 = slash2 >> (n - y - 1); + int canputqueen = y_board[y] & x & nowslash1 & nowslash2; + int xput; + while (canputqueen) + { + xput = canputqueen & (-canputqueen); + backtracking(n, y + 1, x ^ xput, slash1 ^ (xput << y), slash2 ^ (xput << (n - y - 1))); + canputqueen ^= xput; + } +} + +int main() +{ + int n; + int casenumber = 1; + while (scanf("%d", &n) != EOF && n != 0) + { + int i, j; + char s[20] = {0}; + for (i = 0; i < n; i++) + { + scanf("%s", s); + y_board[i] = (1 << n) - 1; + for (j = 0; j < n; j++) + if (s[j] == '*') + { + y_board[i] ^= (1 << j); + } + } + answer = 0; + backtracking(n, 0, (1 << n) - 1, (1 << (2 * n - 1)) - 1, (1 << (2 * n - 1)) - 1); + printf("Case %d: %d\n", casenumber, answer); + casenumber++; + } + return 0; +} diff --git a/11196.cpp b/11196.cpp new file mode 100644 index 0000000..23303bd --- /dev/null +++ b/11196.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +int n, m; +int S; +bool prune(int depth, int s, int hope, int r, int h) +{ + if (depth == n) + { + return false; + } + int nn = m - depth; + int sum = (nn * (nn + 1)) / 2; + int sum2 = sum * sum; + int s1 = s + sum2; + if (s1 > n) + { + return true; + } + int s2 = s + nn * r * r * h - r * (r + h + h) * sum + (r + r + h) * nn * (nn + 1) * (nn + nn + 1) / 6 - sum2; + if (s2 < n) + { + return true; + } + int s3 = hope + 2 * nn * nn; + if (s3 >= S) + { + return true; + } + return false; +} +void DFS(int depth, int strict, int hope, int prevh, int prevr) +{ + if (strict > n || hope > S) + { + return; + } + if (depth == m) + { + if (strict != n) + { + return; + } + if (hope < S) + { + S = hope; + } + } + else + { + if (depth) + { + int P = (n - strict) * 2 / prevr; + if (P >= S - hope) + { + return; + } + } + int x = (int)sqrt((double)(n - strict)); + if (x > prevr - 1) + { + x = prevr - 1; + } + int bound = m - depth; + for (int r = x; r >= bound; r--) + { + int r2 = r * r; + int y = (n - strict) / r2; + if (y > prevh - 1) + { + y = prevh - 1; + } + for (int h = y; h >= bound; h--) + { + int temp = 2 * r * h, r2h = r2 * h; + if (depth == 0) + { + temp += r2; + } + if (prune(depth + 1, strict + r2h, hope, r, h)) + { + continue; + } + int P = (n - strict - r2h) * 2 / r; + if (P >= S - hope) + { + break; + } + DFS(depth + 1, strict + r2h, hope + temp, h, r); + } + } + } +} +int main() +{ + int caseno = 1; + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + scanf("%d", &m); + S = 7000; + DFS(0, 0, 0, 7000, 7000); + if (S == 7000) + { + S = 0; + } + printf("Case %d: %d\n", caseno++, S); + } +} diff --git a/11198.cpp b/11198.cpp new file mode 100644 index 0000000..6afc2d6 --- /dev/null +++ b/11198.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +/* +Author:银志圆 +State:Ac +题æ„:求最å°çš„交æ¢æ¬¡æ•°ä½¿å¾—åºåˆ—æœ‰åº +æ€è·¯ï¼šå¹¿åº¦æœç´¢+mapåˆ¤é‡ +*/ + +typedef int pii[8]; +struct node +{ + pii a;//b用æ¥è®°å½•ä¸ºè´Ÿæ•°çš„下标 + int len; + int state; + void preInit(int from, int to) + { + int tfrom = getPos(from, 8); + for (int i = tfrom + 1; i < 8; i++) + { + a[i - 1] = a[i]; + } + int tto = getPos(to, 7); + for (int i = 6; i >= tto; i--) + { + a[i + 1] = a[i]; + } + a[tto] = from; + } + void nextInit(int from, int to) + { + int tfrom = getPos(from, 8); + for (int i = tfrom + 1; i < 8; i++) + { + a[i - 1] = a[i]; + } + int tto = getPos(to, 7); + for (int i = 6; i >= (tto + 1); i--) + { + a[i + 1] = a[i]; + } + a[tto + 1] = from; + } + int getPos(int valu, int len) + { + for (int i = 0; i < len; i++) + { + if (a[i] == valu) + { + return i; + } + } + } +}; +map mark; +bool nprime[20]; +int male[9], female[9], tmp, judge = 12345678; +pii start; +int toint(pii a) +{ + int sum = 0; + for (int i = 0; i < 8; i++) + { + sum = sum * 10 + a[i]; + } + return sum; +} +int main() +{ + memset(nprime, false, sizeof(nprime)); + nprime[4] = true; + nprime[6] = true; + nprime[8] = true; + nprime[9] = true; + nprime[10] = true; + nprime[12] = true; + nprime[14] = true; + nprime[15] = true; + int cases = 0; + while (1) + { + cases++; + mark.clear(); + male[0] = female[0] = 0; + for (int i = 0; i < 8; i++) + { + scanf("%d", &start[i]); + if (start[i] == 0) + { + system("pause"); + return 0; + } + if (start[i] > 0) + { + male[++male[0]] = start[i]; + } + else + { + start[i] = -start[i]; + female[++female[0]] = start[i]; + } + } + tmp = toint(start); + mark[tmp] = true; + node tq; + for (int i = 0; i < 8; i++) + { + tq.a[i] = start[i]; + } + tq.len = 0; + tq.state = tmp; + queue q; + q.push(tq); + int k = 0, ok = 0; + while (!q.empty()) + { + tq = q.front(); + q.pop(); + if (tq.state == judge) + { + ok = 1; + printf("Case %d: %d\n", cases, tq.len); + break; + } + for (int i = 1; i <= female[0]; i++) + { + for (int j = 1; j <= male[0]; j++) + { + if (!nprime[female[i] + male[j]]) + { + node tp = tq; + tp.len++; + tp.preInit(female[i], male[j]); + tmp = toint(tp.a); + tp.state = tmp; + if (!mark[tmp]) + { + q.push(tp); + mark[tmp] = true; + } + tp = tq; + tp.len++; + tp.nextInit(female[i], male[j]); + tmp = toint(tp.a); + tp.state = tmp; + if (!mark[tmp]) + { + q.push(tp); + mark[tmp] = true; + } + } + } + } + for (int i = 1; i <= female[0]; i++) + { + for (int j = 1; j <= male[0]; j++) + { + if (!nprime[female[i] + male[j]]) + { + node tp = tq; + tp.len++; + tp.preInit(male[j], female[i]); + tmp = toint(tp.a); + tp.state = tmp; + if (!mark[tmp]) + { + q.push(tp); + mark[tmp] = true; + } + tp = tq; + tp.len++; + tp.nextInit(male[j], female[i]); + tmp = toint(tp.a); + tp.state = tmp; + if (!mark[tmp]) + { + q.push(tp); + mark[tmp] = true; + } + } + } + } + } + if (!ok) + { + printf("Case %d: -1\n", cases); + } + } + return 0; +} diff --git a/112.cpp b/112.cpp new file mode 100644 index 0000000..2c601e6 --- /dev/null +++ b/112.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +// error -1 +// empty 0 +// yes 1 +// no 2 +int parse(int tgt) +{ + char ch; + int value; + cin >> ch; + if (cin.fail()) + { + cin.clear(); + return -1; + } + cin >> value; + if (cin.fail()) + { + cin.clear(); + cin >> ch; + if (ch != ')') + { + return -1; + } + return 0;// empty tree + } + int left = parse(tgt - value); + int right = parse(tgt - value); + cin >> ch; + if (cin.fail()) + { + cin.clear(); + return -1; + } + if (left == 0 && right == 0 && tgt == value) + { + return 1; + } + else if (left == 1 || right == 1) + { + return 1; + } + return 2; +} + +int main() +{ + int tgt; + while (scanf("%d", &tgt) == 1) + { + printf(parse(tgt) == 1 ? "yes\n" : "no\n"); + } + return 0; +} diff --git a/1120.cpp b/1120.cpp new file mode 100644 index 0000000..1f4633e --- /dev/null +++ b/1120.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define MAXL (100000 >> 5) + 1 +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) + +int mark[MAXL]; + +int main() +{ + int T, n, m, A[10]; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < m; i++) + { + scanf("%d", &A[i]); + } + for (int i = 1; i < m; i++) + { + A[i] += A[i - 1]; + } + memset(mark, 0, sizeof(mark)); + SET(0); + for (int i = 0; i < m; i++) + { + for (int j = 0; j + A[i] <= n; j++) + { + if (GET(j)) + { + SET(j + A[i]); + } + } + } + printf(GET(n) ? "YES\n" : "NO\n"); + if (T) + { + printf("\n"); + } + } + return 0; +} diff --git a/11200.cpp b/11200.cpp new file mode 100644 index 0000000..ff1d6bd --- /dev/null +++ b/11200.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +enum +{ + NORTH, + EAST, + SOUTH, + WEST, + NDIRS +}; +const int MAXN = 512; +const int dr[4] = {-1, 0, +1, 0}; +const int dc[4] = {0, +1, 0, -1}; +const int nd[2][4] = + { + {// Slash N->W, E->S, S->E, W->N + WEST, SOUTH, EAST, NORTH}, + {// Backslash N->E, E->N, S->W, W->S + EAST, NORTH, WEST, SOUTH}}; +char maze[MAXN][MAXN]; +int ans, col, row; +void dfs(int r, int c, int from) +{ + ans += r == row; + if (r < 0 || r >= row || c < 0 || c >= col) + { + return; + } + int ndir = nd[maze[r][c] == '\\'][from]; + int rdir = (ndir + 2) % NDIRS; + dfs(r + dr[ndir], c + dc[ndir], rdir); +} +int main() +{ + int t; + scanf("%d", &t); + for (ans = 0; t-- && scanf("%d %d", &col, &row) == 2; ans = 0) + { + FOR(i, row) + scanf("%s", maze[i]); + FOR(j, col) + dfs(0, j, NORTH); + printf("%d\n", ans); + } + return 0; +} diff --git a/11201.cpp b/11201.cpp new file mode 100644 index 0000000..e8408d6 --- /dev/null +++ b/11201.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define mp make_pair + +const int N = 10000; +const int inf = (1 << 21); +typedef long long ll; +double table[] = + { + 12.53, + 1.42, + 4.68, + 5.86, + 13.68, + 0.69, + 1.01, + 0.70, + 6.25, + 0.44, + 0.00, + 4.97, + 3.15, + 6.71, + 8.68, + 2.51, + 0.88, + 6.87, + 7.98, + 4.63, + 3.93, + 0.90, + 0.02, + 0.22, + 0.90, + 0.52, +}; + +string aiueo[2] = + { + "bcdfghjklmnpqrstvwxyz", + "aeiuo"}; + +double average[22][8] = + { + 0}; +double num[22][8] = + { + 0}; + +void maketable(int *cnt, int now, double val, int start) +{ + if (now == 8) + { + return; + } + average[start][now] += val; + num[start][now]++; + int tmp = now % 2; + rep(i, aiueo[tmp].size()) + { + if (cnt[aiueo[tmp][i]] == 2) + { + continue; + } + cnt[aiueo[tmp][i]]++; + maketable(cnt, now + 1, val + (1 + now) * table[aiueo[tmp][i] - 'a'], start); + cnt[aiueo[tmp][i]]--; + } +} + +main() +{ + int cnt[255] = {0}; + int mapping[255]; + rep(i, aiueo[0].size()) mapping[aiueo[0][i]] = i; + rep(i, aiueo[0].size()) + { + cnt[aiueo[0][i]]++; + maketable(cnt, 1, table[aiueo[0][i] - 'a'], i); + cnt[aiueo[0][i]]--; + } + int te; + cin >> te; + string in; + getline(cin, in); + while (te--) + { + double val = 0; + getline(cin, in); + rep(i, in.size()) + { + val += table[in[i] - 'a'] * (i + 1); + } + double tmp = average[mapping[in[0]]][in.size()] / num[mapping[in[0]]][in.size()]; + // cout << val <<" " << tmp << endl; + if (val >= tmp) + { + puts("above or equal"); + } + else + { + puts("below"); + } + } +} diff --git a/11202.cpp b/11202.cpp new file mode 100644 index 0000000..3db1ea2 --- /dev/null +++ b/11202.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + unsigned int t; + cin >> t; + while (t--) + { + int n, m; + cin >> n >> m; + double x = ceil(n / 2.0), y = ceil(m / 2.0); + if (n != m) + { + cout << (unsigned long)(x * y) << endl; + } + else + { + cout << (unsigned long)(x * y - (x * (x - 1)) / 2.0) << endl; + } + } + return 0; +} diff --git a/11203.cpp b/11203.cpp index 8fae1f6..deb4d78 100644 --- a/11203.cpp +++ b/11203.cpp @@ -1,71 +1,84 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--) { - string S; - cin >> S; - int Q = 0, M = 0, E = 0; - int L = S.length(); - int i, j; - FOI(i, 0, L-1) { - if (S[i] == '?') ++Q; - if (S[i] == 'M') ++M; - if (S[i] == 'E') ++E; +bool validate(const string &s) +{ + int m = -100, e = -100; + bool m_q = false; + bool e_q = false; + for (int i = 0, sz = s.size(); i < sz; i++) + { + if (s[i] != '?' && s[i] != 'M' && s[i] != 'E') + { + return false; + } + if (i == 0 && s[i] != '?') + { + return false; + } + if (i == e + 1) + { + if (s[i] == '?') + { + e_q = true; + } + } + else if (i == m + 1) + { + if (s[i] == '?') + { + m_q = true; + } + } + if (s[i] == 'M') + { + if (m >= 0) + { + return false; + } + m = i; } - if (Q + M + E != L || M != 1 || E != 1 || Q < 4) { - cout << "no-theorem\n"; - continue; + else if (s[i] == 'E') + { + if (e >= 0) + { + return false; + } + e = i; } - int q1 = 0, q2 = 0, q3 = 0; - int iM = (int) S.find("M"); - int iE = (int) S.find("E"); - if (!(iM > 0 && iE > iM + 1 && iE < L - 2)) { - cout << "no-theorem\n"; - continue; + } + if (!m || !e || !m_q || !e_q) + { + return false; + } + int x = m; + int y = e - m - 1; + int z = s.size() - (e + 1); + if (y != z - x) + { + return false; + } + return true; +} + +int main() +{ + int n; + string input; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, input); + if (!validate(input)) + { + cout << "no-theorem" << endl; } - FOI(i, 0, iM-1) - if (S[i] == '?') ++q1; - FOI(i, iM+1, iE-1) - if (S[i] == '?') ++q2; - FOI(i, iE+1, L-1) - if (S[i] == '?') ++q3; - - if (q1 > 0 && q2 > 0 && q3 > 1 && q2 == q3 - q1) - cout << "theorem\n"; else - cout << "no-theorem\n"; + { + cout << "theorem" << endl; + } } return 0; } diff --git a/11204.cpp b/11204.cpp new file mode 100644 index 0000000..c27cf5a --- /dev/null +++ b/11204.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int t, n, m; + while (cin >> t) + { + while (t--) + { + cin >> n >> m; + long long int Ans = 1; + int item[33] = {0}, num[32][32]; + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + { + cin >> num[i][j]; + if (num[i][j] == 1) + { + item[j]++; + } + } + for (int i = 0; i < n; i++) + if (item[i]) + { + Ans *= item[i]; + } + cout << Ans << endl; + } + } + return 0; +} diff --git a/11205.cpp b/11205.cpp new file mode 100644 index 0000000..7571b20 --- /dev/null +++ b/11205.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define MAXN 100 +#define MAXP 16 + +int P, N, maxsp;// max suppress LED number + +bool check(int S[][MAXP], int *mask) +{ + set s; + for (int i = 0; i < N; i++)// check all numbers + { + int t = 0; + for (int j = 0; j < P; j++)// check all LEDs + if (!mask[j] && S[i][j]) + t += 1 << j; + if (s.find(t) != s.end()) + return false; + else + s.insert(t); + } + return true; +} + +void solve(int S[][MAXP], int *mask, int cur) +{ + if (cur == P) + { + int cnt = 0; + for (int i = 0; i < P; i++) + if (mask[i]) + cnt++; + if (maxsp < cnt) + maxsp = cnt; + } + else + { + mask[cur] = 1; + if (check(S, mask)) + solve(S, mask, cur + 1); + mask[cur] = 0; + solve(S, mask, cur + 1);// not change mask + } +} + +int S[MAXN][MAXP], mask[MAXP]; + +void init() +{ + maxsp = 0; + memset(mask, 0, sizeof(mask)); +} + +int main() +{ + int nr_cases; + scanf("%d", &nr_cases); + while (nr_cases--) + { + scanf("%d%d", &P, &N); + init(); + for (int i = 0; i < N; i++) + for (int j = 0; j < P; j++) + scanf("%d", &S[i][j]); + solve(S, mask, 0); + printf("%d\n", P - maxsp); + } + return 0; +} diff --git a/11206.cpp b/11206.cpp new file mode 100644 index 0000000..8443b23 --- /dev/null +++ b/11206.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 20; +int ans; +int color[N]; +vector edge[N]; +int in[4]; + +bool check(int now, int c) +{ + rep(i, (int)edge[now].size()) + { + if (color[edge[now][i]] == c) + { + return false; + } + } + return true; +} + +void search(int n, int now) +{ + if (now == n) + { + int tmp = 0; + rep(i, n) + { + rep(j, (int)edge[i].size()) + { + if (edge[i][j] > i) + { + int a = color[i] - color[edge[i][j]]; + tmp += a * a; + } + } + } + ans = max(ans, tmp); + return; + } + rep(i, 4) + { + if (check(now, in[i])) + { + color[now] = in[i]; + search(n, now + 1); + color[now] = -1; + } + } +} + +main() +{ + int n, m; + while (cin >> n >> m >> in[0] >> in[1] >> in[2] >> in[3] && n) + { + rep(i, n) color[i] = -1, edge[i].clear(); + rep(i, m) + { + int f, t; + cin >> f >> t; + f--; + t--; + edge[f].push_back(t); + edge[t].push_back(f); + } + ans = 0; + search(n, 0); + cout << ans << endl; + } + return false; +} diff --git a/11207.cpp b/11207.cpp index 721493b..5d626b0 100644 --- a/11207.cpp +++ b/11207.cpp @@ -1,57 +1,40 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int N; - scanf("%d", &N); - if (N == 0) - break; - double Max = 0; - int Ind = 1, i; - FOI(i, 1, N){ - double A, B, T; - scanf("%lf%lf", &A, &B); - T = A; - A = min(T, B); - B = max(T, B); - double val = max( min(A, B/4.0), min(A/2.0, B/2.0) ); - if (val > Max){ - Max = val; - Ind = i; +int main() +{ + int N; + scanf("%d", &N); + while (N) + { + int max = 0, m_index = 0; + int w, h; + for (int i = 0; i < N; i++) + { + scanf(" %d %d", &w, &h); + if (h > w) + { + int t; + t = h; + h = w; + w = t; + } + int l = h * 6; + if (l > max) + { + m_index = i; + max = l; + } + l = (w * 3) > (h * 12) ? h * 12 : w * 3; + if (l > max) + { + m_index = i; + max = l; } } - printf("%d\n", Ind); + printf("%d\n", m_index + 1); + scanf("%d", &N); } return 0; } - diff --git a/11208.cpp b/11208.cpp new file mode 100644 index 0000000..bb98d78 --- /dev/null +++ b/11208.cpp @@ -0,0 +1,330 @@ +#include + +using namespace std; + +struct point {int x, y;}; +point land_lst[110], queue_[110]; + +const int mod = 100007; +int dx[] = {-1, 1, 0, 0}; +int dy[] = {0, 0, -1, 1}; + +bool vis[11][11]; +char api[13][13][10], ld[77][100]; +int n, r, c, pos[2][222], value[13][13], cnt[222], vv, st[222], lst[44], qt[222], ans[44], at[2][222], mat[13][13], cnt_lst, ep[222], num[mod]; +long long hash_[mod][107], big_mod = (long long)100000009 * (long long)100000009, ex[111]; + +bool judge(int x, int y, int state) +{ + int i, j, s, p, q, vv, temp1, temp2, temp; + for (i = 0; i < r; i++) + for (j = 0; j < c; j++) + { + vis[i][j] = false; + } + queue_[0].x = x; + queue_[0].y = y; + temp = 1; + vis[x][y] = true; + temp1 = 0; + temp2 = 0; + while (temp1 <= temp2) + { + for (i = temp1; i <= temp2; i++) + { + for (j = 0; j < 4; j++) + { + x = queue_[i].x + dx[j]; + y = queue_[i].y + dy[j]; + if (x >= 0 && x < r && y >= 0 && y < c && !vis[x][y]) + { + + vv = mat[x][y]; + if (api[x][y][0] != '#' && !(vv > 0 && api[x][y][0] >= '0' && api[x][y][0] <= '9' && state & (1 << vv))) + { + queue_[temp].x = x; + queue_[temp++].y = y; + if (api[x][y][0] == '=') + { + return true; + } + vis[x][y] = true; + } + } + } + } + temp1 = temp2 + 1; + temp2 = temp - 1; + } + return false; +} +void bfs(point can_lst[], int &cnt_can, int id) +{ + int i, j, s, p, q, x, y, temp1, temp2, temp; + temp = 0; + for (i = 0; i < r; i++) + for (j = 0; j < c; j++) + { + vis[i][j] = false; + } + for (i = 0; i < cnt_lst; i++) + { + queue_[temp++] = land_lst[i]; + vis[land_lst[i].x][land_lst[i].y] = true; + } + temp1 = 0; + temp2 = temp - 1; + while (temp1 <= temp2) + { + for (i = temp1; i <= temp2; i++) + { + for (j = 0; j < 4; j++) + { + x = queue_[i].x + dx[j]; + y = queue_[i].y + dy[j]; + if (x >= 0 && x < r && y >= 0 && y < c && !vis[x][y]) + { + if (api[x][y][0] != '#' && !(api[x][y][0] >= '0' && api[x][y][0] <= '9' && mat[x][y])) + { + + queue_[temp].x = x; + queue_[temp++].y = y; + vis[x][y] = true; + if (api[x][y][0] >= '0' && api[x][y][0] <= '9' && mat[x][y] == 0) + { + can_lst[cnt_can++] = queue_[temp - 1]; + } + } + } + } + } + temp1 = temp2 + 1; + temp2 = temp - 1; + } + + +} +bool dfs(int id, int state, long long hs) +{ + int la, id_r, id_c, i, j, s, p, q, vv, nst, x, y, temp; + long long nhs; + bool res; + if (id >= 2 * n) + { + return true; + } + + + + vv = lst[id]; + cnt[vv] ^= 1; + if (cnt[vv] == 0) + { + id_r = at[0][vv]; + id_c = at[1][vv]; + + + at[0][vv] = at[1][vv] = -1; + temp = mat[id_r][id_c]; + while (temp == 0) + { + puts("orz"); + } + mat[id_r][id_c] = 0; + nhs = hs - ex[value[id_r][id_c]]; + nhs = (nhs % big_mod + big_mod) % big_mod; + res = dfs(id + 1, state ^ (1 << vv), nhs); + cnt[vv] ^= 1; + mat[id_r][id_c] = temp; + at[0][vv] = id_r; + at[1][vv] = id_c; + return res; + } + point can_lst[101]; + int cnt_can = 0; + bfs(can_lst, cnt_can, id); + + + + + int ct = 0; + for (i = id; i < 2 * n; i++) + { + if (ld[i][0] == '+') + { + ct++; + } + else + { + break; + } + } + nst = (state & st[vv]); + + + int lst[22], cl = 0; + for (i = 1; i <= n; i++) + { + if (nst & (1 << i)) + { + lst[cl++] = i; + } + } + lst[cl++] = vv; + for (i = 0; i < cnt_can && ct <= cnt_can; i++) + { + + + + + x = can_lst[i].x; + y = can_lst[i].y; + + + while (mat[x][y] != 0) + { + puts("orz"); + } + mat[x][y] = vv; + at[0][vv] = x; + at[1][vv] = y; + nhs = hs + ex[value[x][y]]; + nhs %= big_mod; + ans[vv] = value[x][y]; + int jj; + for (jj = 0; jj < cl; jj++) + { + j = lst[jj]; + + + + + + + if (!judge(at[0][j], at[1][j], (state | (1 << vv)) & ep[j])) + { + break; + } + + } + if (jj >= cl && dfs(id + 1, state | (1 << vv), nhs)) + { + return true; + } + mat[x][y] = 0; + at[0][vv] = at[1][vv] = -1; + + + } + cnt[vv] ^= 1; + return false; +} +int main() +{ + int i, j, s, p, q, vv, tst = 0, ax; + while (scanf("%d", &n) && n) + { + scanf("%d%d", &r, &c); + tst++; + memset(pos, -1, sizeof(pos)); + cnt_lst = 0; + ax = 0; + for (i = 0; i < r; i++) + for (j = 0; j < c; j++) + { + scanf("%s", api[i][j]); + if (api[i][j][0] >= '0' && api[i][j][0] <= '9') + { + value[i][j] = 10 * (api[i][j][0] - '0') + api[i][j][1] - '0'; + pos[0][value[i][j]] = i; + pos[1][value[i][j]] = j; + ax = max(ax, value[i][j]); + } + if (api[i][j][0] == '=') + { + land_lst[cnt_lst].x = i; + land_lst[cnt_lst++].y = j; + } + } + ex[0] = 1; + for (i = 1; i <= ax + 1; i++) + { + ex[i] = (ex[i - 1] << 1) % big_mod; + } + + for (i = 0; i < 2 * n; i++) + { + scanf("%s", ld[i]); + } + memset(st, 0, sizeof(st)); + memset(ep, 0, sizeof(ep)); + for (i = 0; i < 2 * n; i++) + { + vv = 0; + int len = strlen(ld[i]); + while (len >= 10 || len <= 1) + { + puts("orz"); + } + for (j = 1; j < len; j++) + { + vv = 10 * vv + ld[i][j] - '0'; + } + lst[i] = vv; + while (vv <= 0 || vv > n) + { + puts("orz"); + } + + if (ld[i][0] == '-') + { + memset(cnt, 0, sizeof(cnt)); + for (j = 0; j <= i; j++) + { + cnt[lst[j]] ^= 1; + } + for (j = 1; j <= n; j++) + { + if (cnt[j]) + { + st[j] |= (1 << vv); + ep[vv] |= (1 << j); + + + } + } + } + } + memset(cnt, 0, sizeof(cnt)); + memset(mat, 0, sizeof(mat)); + memset(at, -1, sizeof(at)); + memset(num, 0, sizeof(num)); + printf("Case %d: ", tst); + if (!dfs(0, 0, 0)) + { + puts("No"); + } + else + { + puts("Yes"); + for (i = 1; i <= n; i++) + { + if (i > 1) + { + putchar(' '); + } + if (ans[i] >= 10) + { + printf("%d", ans[i]); + } + else + { + printf("0%d", ans[i]); + } + } + printf("\n"); + } + + } + return 0; +} diff --git a/11209.cpp b/11209.cpp new file mode 100644 index 0000000..dda4712 --- /dev/null +++ b/11209.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 201 +typedef struct +{ + int t, c; +} Edge; +vector edge[N]; +int path[N]; +int cost[N];//cost on path +bool visited[N]; +int ans = 0; +int anspath[N], anspointer, anspnum; + +inline void check_data(int n, int total) +{ + int tmp = 0; + rep(i, n && tmp < total) + { + tmp += cost[i]; + if (tmp == total) + { + anspointer = i; + rep(j, n) anspath[j] = path[j]; + ans = tmp; + anspnum = n; + } + } + return; +} + +void solve(int now, int goal, int tcost, int pnum) +{ + if (now == goal) + { + if (tcost % 2 == 0 && tcost < ans * 2) + { + check_data(pnum, tcost / 2); + } + return; + } + visited[now] = true; + rep(i, edge[now].size()) + { + if (visited[edge[now][i].t] == false) + { + path[pnum] = edge[now][i].t; + cost[pnum] = edge[now][i].c; + solve(edge[now][i].t, goal, tcost + cost[pnum], pnum + 1); + } + } + visited[now] = false; + return; +} + +main() +{ + int n, m, s, g; + int tc = 1; + while (cin >> n >> m >> s >> g && n) + { + ans = (1 << 21); + rep(i, n + 1) edge[i].clear(), visited[i] = false; + Edge tmp; + rep(i, m) + { + int f, t, c; + cin >> f >> t >> c; + tmp.t = t; + tmp.c = c; + edge[f].push_back(tmp); + tmp.t = f; + edge[t].push_back(tmp); + } + cout << "Case " << tc++ << ": "; + path[0] = s; + cost[0] = 0; + solve(s, g, 0, 1); + cout << ans << endl; + cout << anspointer + 1; + rep(j, anspointer + 1) cout << ' ' << anspath[j]; + cout << endl; + cout << anspnum - anspointer; + for (int j = anspnum - 1; j >= anspointer; j--) + { + cout << ' ' << anspath[j]; + } + cout << endl; + puts(""); + } + return false; +} diff --git a/1121.cpp b/1121.cpp new file mode 100644 index 0000000..9024281 --- /dev/null +++ b/1121.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int main() +{ + int N, S, A[100000], i; + while (scanf("%d %d", &N, &S) == 2) + { + for (i = 0; i < N; i++) + { + scanf("%d", &A[i]); + } + int tail = -1, sum = 0; + int min = 0xfffffff; + for (i = 0; i < N; i++) + { + while (sum < S && tail + 1 < N) + { + sum += A[++tail]; + } + if (min > tail - i && sum >= S) + { + min = tail - i; + } + sum -= A[i]; + } + if (min == 0xfffffff) + { + puts("0"); + } + else + { + printf("%d\n", min + 1); + } + } + return 0; +} diff --git a/11210.cpp b/11210.cpp new file mode 100644 index 0000000..b985cb0 --- /dev/null +++ b/11210.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +int tbl[34]; +char name[7][11] = {"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"}; +char buf[101]; + +int trans(char *s) +{ + int i; + for (i = 0; i < 7; i++) + { + if (!strcmp(s, name[i])) + { + return 27 + i; + } + } + if (s[1] == 'T') + { + return s[0] - '1'; + } + else if (s[1] == 'S') + { + return 9 + s[0] - '1'; + } + return 18 + s[0] - '1'; +} + +char *tile(int n) +{ + if (n < 9) + { + sprintf(buf, "%dT\0", n + 1); + } + else if (n < 18) + { + sprintf(buf, "%dS\0", n - 8); + } + else if (n < 27) + { + sprintf(buf, "%dW\0", n - 17); + } + else + { + strcpy(buf, name[n - 27]); + } + return buf; +} + +int dfs(int depth, int eye) +{ + int temp; + if (depth == 34) + { + return eye == 1; + } + if (!tbl[depth]) + { + return dfs(depth + 1, eye); + } + if (depth < 27) + { + temp = depth % 9; + if (temp < 7 && tbl[depth + 1] && tbl[depth + 2]) + { + tbl[depth]--; + tbl[depth + 1]--; + tbl[depth + 2]--; + if (dfs(depth, eye)) + { + tbl[depth]++; + tbl[depth + 1]++; + tbl[depth + 2]++; + return 1; + } + tbl[depth]++; + tbl[depth + 1]++; + tbl[depth + 2]++; + } + } + if (tbl[depth] > 1 && !eye) + { + tbl[depth] -= 2; + if (dfs(depth, eye + 1)) + { + tbl[depth] += 2; + return 1; + } + tbl[depth] += 2; + } + if (tbl[depth] > 2) + { + tbl[depth] -= 3; + if (dfs(depth, eye)) + { + tbl[depth] += 3; + return 1; + } + tbl[depth] += 3; + } + return 0; +} + +int main() +{ + int cas, i, ans; + cas = 0; + while (1) + { + memset(tbl, 0, sizeof(tbl)); + for (i = 0; i < 13; i++) + { + scanf("%s", buf); + if (buf[0] == '0') + { + break; + } + tbl[trans(buf)]++; + } + if (i < 13) + { + break; + } + printf("Case %d:", ++cas); + for (i = 0, ans = 0; i < 34; i++) + { + if (tbl[i] < 4) + { + tbl[i]++; + if (dfs(0, 0)) + { + printf(" %s", tile(i)); + ans = 1; + } + tbl[i]--; + } + } + if (!ans) + { + printf(" Not ready"); + } + printf("\n"); + } + return 0; +} diff --git a/11212.cpp b/11212.cpp new file mode 100644 index 0000000..5ff7061 --- /dev/null +++ b/11212.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +struct node {string s; int tot;} q[1000007], qq[1000007]; + +int n, a[10], Find; +string sta, end_; +map hash_, hash_1; + +string tran(string a) +{ + return a; +} + +void bfs1() +{ + int front = 0, rear = 1; + qq[front].s = end_; + qq[front].tot = 0; + hash_1[tran(end_)] = 0; + while (front < rear && qq[front].tot < 2) + { + string t = qq[front].s; + for (int i = 0; i < t.size(); i++) + { + for (int j = i; j < t.size(); j++) + { + string b = "", c = "", d = ""; + for (int r = i; r <= j; r++) + { + b += t[r]; + } + for (int r = 0; r < i; r++) + { + c += t[r]; + } + for (int r = j + 1; r < t.size(); r++) + { + c += t[r]; + } + for (int ss = 0; ss <= c.size(); ss++) + { + d = ""; + for (int r = 0; r < ss; r++) + { + d += c[r]; + } + d += b; + for (int r = ss; r < c.size(); r++) + { + d += c[r]; + } + if (!hash_1[tran(d)] && d != end_) + { + qq[rear].s = d; + qq[rear].tot = qq[front].tot + 1; + hash_1[tran(d)] = qq[rear].tot; + if (d == sta) + { + Find = qq[rear].tot; + return; + } + rear++; + } + } + } + } + front++; + } +} +void bfs() +{ + int front = 0, rear = 1; + q[front].s = sta; + q[front].tot = 0; + hash_[tran(sta)] = 0; + while (front < rear) + { + string t = q[front].s; + for (int i = 0; i < t.size(); i++) + { + for (int j = i; j < t.size(); j++) + { + string b = "", c = "", d = ""; + for (int r = i; r <= j; r++) + { + b += t[r]; + } + for (int r = 0; r < i; r++) + { + c += t[r]; + } + for (int r = j + 1; r < t.size(); r++) + { + c += t[r]; + } + for (int ss = 0; ss <= c.size(); ss++) + { + d = ""; + for (int r = 0; r < ss; r++) + { + d += c[r]; + } + d += b; + for (int r = ss; r < c.size(); r++) + { + d += c[r]; + } + if (!hash_[tran(d)] && d != sta) + { + q[rear].s = d; + q[rear].tot = q[front].tot + 1; + hash_[tran(d)] = q[rear].tot; + if (d == end_) + { + Find = q[rear].tot; + return; + } + if (hash_1[tran(d)]) + { + Find = min(Find, q[rear].tot + hash_1[tran(d)]);//之所以这样写是因为逆å‘时为处ç†æœåˆ°å¯è¡Œè§£çš„情况 + return; + } + rear++; + } + } + } + } + front++; + } +} + +int main() +{ + int T = 1; + while (scanf("%d", &n) != EOF && n) + { + sta = ""; + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + sta += (a[i] + '0'); + } + end_ = ""; + for (int i = 1; i <= n; i++) + { + end_ += (i + '0'); + } + if (sta == end_) + { + Find = 0; + } + else + { + hash_.clear(); + hash_1.clear(); + Find = 100; + bfs1(); + bfs(); + } + printf("Case %d: %d\n", T++, Find); + } + return 0; +} diff --git a/11214.cpp b/11214.cpp new file mode 100644 index 0000000..4e9fd05 --- /dev/null +++ b/11214.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int N, best, cur; +unsigned mask[1000][4], goal[4]; + +void rec(int i, unsigned a0, unsigned a1, unsigned a2) +{ + if (a0 == goal[0] && a1 == goal[1] && a2 == goal[2]) + { + if (cur < best) + { + best = cur; + } + return; + } + if (i >= N || cur + 1 >= best) + { + return; + } + rec(i + 1, a0, a1, a2); + ++cur; + rec(i + 1, a0 | mask[i][0], a1 | mask[i][1], a2 | mask[i][2]); + --cur; +} + +int main() +{ + int h, w; + for (int cs = 1; scanf("%d %d", &h, &w) == 2 && h > 1 && w > 1; cs++) + { + N = h * w; + memset(mask, 0, sizeof(mask)); + memset(goal, 0, sizeof(goal)); + for (int i = 0; i < N; i++) + { + char ch; + scanf(" %c", &ch); + if (ch == 'X') + { + goal[i >> 5] |= 1U << (i & 31); + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + int dy = i / w - j / w; + int dx = i % w - j % w; + if (dx == 0 || dy == 0 || dx == dy || dx == -dy) + { + mask[i][j >> 5] |= 1U << (j & 31); + } + } + for (int j = 0; j < 3; j++) + { + mask[i][j] &= goal[j]; + } + } + best = 6; + cur = 0; + rec(0, 0, 0, 0); + printf("Case %d: %d\n", cs, best); + } +} diff --git a/11215.cpp b/11215.cpp new file mode 100644 index 0000000..b35c025 --- /dev/null +++ b/11215.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +typedef long long ll; + +struct Frac +{ + int p, q; + + void reduce() + { + if (q < 0) + { + p = -p; + q = -q; + } + int g = __gcd(p < 0 ? -p : p, q); + p /= g; + q /= g; + } + + Frac(int num, int den) : p(num), q(den) {} + + bool operator<(const Frac &other) const + { + return p < other.p || (p == other.p && q < other.q); + } + + bool operator==(const Frac &other) const + { + return p == other.p && q == other.q; + } +}; + +bool got[128]; +vector memo[128]; + +void rec(int mask) +{ + vector &res = memo[mask]; + if (got[mask]) + { + return; + } + got[mask] = true; + res.clear(); + for (int L = 1; L < mask; L++) + { + if ((mask & L) != L) + { + continue; + } + int R = mask ^ L; + rec(L); + rec(R); + Frac *a = &memo[L][0], *a_end = &memo[L][memo[L].size()]; + Frac *b, *b_begin = &memo[R][0], *b_end = &memo[R][memo[R].size()]; + Frac z(1, 0); + for (; a != a_end; ++a) + { + for (b = b_begin; b != b_end; ++b) + { + z.p = a->p * b->q + b->p * a->q; + z.q = a->q * b->q; + z.reduce(); + res.push_back(z); + z.p = a->p * b->q - b->p * a->q; + z.q = a->q * b->q; + z.reduce(); + res.push_back(z); + z.p = a->p * b->p; + z.q = a->q * b->q; + z.reduce(); + res.push_back(z); + if (b->p != 0) + { + z.p = a->p * b->q; + z.q = a->q * b->p; + z.reduce(); + res.push_back(z); + } + } + } + } + sort(res.begin(), res.end()); + res.erase(unique(res.begin(), res.end()), res.end()); +} + +int main() +{ + int N, a[10]; + for (int cs = 1; scanf("%d", &N) == 1 && N > 0; cs++) + { + for (int i = 0; i < N; i++) + { + scanf("%d", &a[i]); + } + sort(a, a + N); + memset(got, 0, sizeof(got)); + for (int i = 0; i < 128; i++) + { + memo[i].clear(); + } + for (int i = 0; i < N; i++) + { + got[1 << i] = true; + memo[1 << i].push_back(Frac(a[i], 1)); + } + rec((1 << N) - 1); + printf("Case %d: %d\n", cs, memo[(1 << N) - 1].size()); + } +} diff --git a/11218.cpp b/11218.cpp index 727a661..41caf5d 100644 --- a/11218.cpp +++ b/11218.cpp @@ -1,55 +1,70 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - for(int t=1; ; t++){ - int N, i; - cin>>N; - bool stat[81]; - memset(stat, false, sizeof(stat)); - int tot = 0; - for(i=0; i>a>>b>>c>>val; - if(!(stat[a] | stat[b] | stat[c]) && a!=b!=c) - tot += val; - stat[a] = true; - stat[b] = true; - stat[c] = true; - } - if(tot > 0) - cout<<"Case "< + +using namespace std; + +#define max(a, b) (a > b) ? a : b +int inf = 1 << 20; +int dp[1 << 9]; +int score[9][9][9]; +int optimal(int state) +{ + if (state == 511) + { + return 0; + } + if (dp[state] != -1) + { + return dp[state]; + } + else + { + int i, j, k, m = -inf; + for (i = 0; i < 9; i++) + if (!(state & (1 << i))) + for (j = i + 1; j < 9; j++) + if (!(state & (1 << j))) + for (k = j + 1; k < 9; k++) + if (!(state & (1 << k)) && score[i][j][k]) + { + m = max(m, score[i][j][k] + optimal(state | (1 << i) | (1 << j) | (1 << k))); + } + return dp[state] = m; + } +} +int main() +{ + int c, n, i, j, k, s, cc = 1; + scanf("%d", &n); + while (n) + { + for (i = 0; i < 9; i++) + for (j = 0; j < 9; j++) + for (k = 0; k < 9; k++) + { + score[i][j][k] = 0; + } + for (i = 0; i < 512; i++) + { + dp[i] = -1; + } + for (c = 0; c < n; c++) + { + scanf("%d %d %d %d", &i, &j, &k, &s); + i--; + j--; + k--; + score[i][j][k] = score[i][k][j] = score[j][i][k] = score[j][k][i] = score[k][i][j] = score[k][j][i] = s; + } + int res = optimal(0); + if (res > 0) + { + printf("Case %d: %d\n", cc++, res); + } + else + { + printf("Case %d: %d\n", cc++, -1); + } + scanf("%d", &n); + } + return 0; +} diff --git a/11219.cpp b/11219.cpp index 54cbea3..e841be2 100644 --- a/11219.cpp +++ b/11219.cpp @@ -1,25 +1,70 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - int cd=0,cm=0,cy=0,bd=0,bm=0,by=0; - scanf("%d/%d/%d",&cd,&cm,&cy); - scanf("%d/%d/%d",&bd,&bm,&by); - cout<<"Case #"<130) - cout<<"Check birth date"< + +using namespace std; + +int date_diff(int d1, int m1, int y1, int d2, int m2, int y2) +{ + int result = 0; + if (y1 > y2) + { + return -1; + } + if (y1 == y2) + { + if (m1 > m2) + { + return -1; + } + if (m1 == m2) + { + if (d1 > d2) + { + return -1; + } + } + } + else + { + result = y2 - y1; + if (m2 == m1) + { + if (d2 < d1) + { + result--; + } + } + else if (m2 < m1) + { + result--; + } + } + return result; +} + +int main() +{ + int cases; + int d1, d2, m1, m2, y1, y2; + char delimiter; + cin >> cases; + for (int i = 0; i < cases; i++) + { + cout << "Case #" << i + 1 << ": "; + cin >> d1 >> delimiter >> m1 >> delimiter >> y1; + cin >> d2 >> delimiter >> m2 >> delimiter >> y2; + int diff = date_diff(d2, m2, y2, d1, m1, y1); + if (diff < 0) + { + cout << "Invalid birth date" << endl; + } + else if (diff < 131) + { + cout << diff << endl; + } + else + { + cout << "Check birth date" << endl; + } + } + return 0; +} diff --git a/11220.cpp b/11220.cpp new file mode 100644 index 0000000..b34ee70 --- /dev/null +++ b/11220.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + int cases; + string input; + int counter; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + for (int i = 0; i < cases; i++) + { + cout << "Case #" << i + 1 << ":" << endl; + while (getline(cin, input)) + { + if (input == "") + { + break; + } + stringstream ss; + ss << input; + counter = 1; + while (ss >> input) + { + if (input.size() >= counter) + { + cout << input[counter - 1]; + counter++; + } + } + cout << endl; + } + if (i < cases - 1) + { + cout << endl; + } + } + return 0; +} diff --git a/11221.cpp b/11221.cpp index b82010c..97ca76c 100644 --- a/11221.cpp +++ b/11221.cpp @@ -1,91 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - scanf("%d\n", &test); - for(int t=1; t<=test; t++){ - string str="", demo; - getline(cin, demo); - int k=0, i=0, j=0; - for(i=0; i=0; i--){ - for(j=slen-1; j>=0; j--){ - nstr += mat[i][j]; - } - } - if(nstr != str){ - cout<<"No magic :(\n"; - continue; - } - nstr = ""; - for(j=slen-1; j>=0; j--){ - for(i=slen-1; i>=0; i--){ - nstr += mat[i][j]; - } - } - if(nstr != str){ - cout<<"No magic :(\n"; - continue; - } - cout< + +using namespace std; + +int main() +{ + int t; + string input; + char c; + cin >> t; + cin.ignore(100, '\n'); + for (int casenum = 0; casenum < t; casenum++) + { + cout << "Case #" << casenum + 1 << ":" << endl; + input = ""; + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> c; + if (c >= 'a' && c <= 'z') + { + input += c; + } + } + cin.ignore(100, '\n'); + int sq = sqrt(input.size()); + if (sq * sq != input.size()) + { + cout << "No magic :(" << endl; + continue; + } + if (input != string(input.rbegin(), input.rend())) + { + cout << "No magic :(" << endl; + continue; + } + string tmp = ""; + for (int i = 0; i < sq; i++) + { + for (int j = 0; j < sq; j++) + { + tmp += input[i + j * sq]; + } + } + if (tmp != input) + { + cout << "No magic :(" << endl; + continue; + } + cout << sq << endl; + } + return 0; +} diff --git a/11222.cpp b/11222.cpp new file mode 100644 index 0000000..6537047 --- /dev/null +++ b/11222.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + if (a.second == b.second) + { + return a.first < b.first; + } + return a.second > b.second; +} + +int main() +{ + int t, s, problem; + vector solved[3]; + map only[3]; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + for (int i = 0; i < 3; i++) + { + solved[i].clear(); + only[i].clear(); + } + for (int i = 0; i < 3; i++) + { + cin >> s; + while (s--) + { + cin >> problem; + solved[i].push_back(problem); + only[(i + 1) % 3][problem] = true; + only[(i + 2) % 3][problem] = true; + } + sort(solved[i].begin(), solved[i].end()); + } + vector> result; + for (int i = 0; i < 3; i++) + { + int unique = solved[i].size(); + for (int j = 0, sz = solved[i].size(); j < sz; j++) + { + if (only[i].find(solved[i][j]) != only[i].end()) + { + unique--; + } + } + result.push_back(make_pair(i, unique)); + } + sort(result.begin(), result.end(), theTruthIsOutThere); + int best = result[0].second; + cout << "Case #" << casenum + 1 << ":" << endl; + for (int i = 0; i < 3; i++) + { + if (result[i].second != best) + { + break; + } + cout << result[i].first + 1 << " " << result[i].second; + for (int j = 0, sz = solved[result[i].first].size(); j < sz; j++) + { + if (only[result[i].first].find(solved[result[i].first][j]) == only[result[i].first].end()) + { + cout << " " << solved[result[i].first][j]; + } + } + cout << endl; + } + } + return 0; +} diff --git a/11223.cpp b/11223.cpp index 552c739..7128e4d 100644 --- a/11223.cpp +++ b/11223.cpp @@ -1,145 +1,105 @@ -#include -#include -#include -#include -using namespace std; - -int main(int argc, char *argv[]) { - int T; - char ar[2005], tmp[10]; - - cin >> T; - getchar(); - memset(tmp, '\0', sizeof(tmp)); +#include - for(int i = 1; i <= T; i++) { - memset(ar, '\0', sizeof(ar)); - gets(ar); - int len = strlen(ar), index = 0; - - cout << "Message #" << i << endl; +using namespace std; - for(int j = 0; j < len; j++) { - if(ar[j] != ' ' ) - tmp[index++] = ar[j]; - if(ar[j] == ' ' || j == len - 1) { - if(strcmp(tmp,".-" ) == 0) - cout << "A"; - else if(strcmp(tmp,"-..." ) == 0) - cout << "B"; - else if(strcmp(tmp,"-.-." ) == 0) - cout << "C"; - else if(strcmp(tmp,"-.." ) == 0) - cout << "D"; - else if(strcmp(tmp,"." ) == 0) - cout << "E"; - else if(strcmp(tmp,"..-." ) == 0) - cout << "F"; - else if(strcmp(tmp,"--." ) == 0) - cout << "G"; - else if(strcmp(tmp,"...." ) == 0) - cout << "H"; - else if(strcmp(tmp,".." ) == 0) - cout << "I"; - else if(strcmp(tmp,".---" ) == 0) - cout << "J"; - else if(strcmp(tmp,"-.-" ) == 0) - cout << "K"; - else if(strcmp(tmp,".-.." ) == 0) - cout << "L"; - else if(strcmp(tmp,"--" ) == 0) - cout << "M"; - else if(strcmp(tmp,"-." ) == 0) - cout << "N"; - else if(strcmp(tmp,"---" ) == 0) - cout << "O"; - else if(strcmp(tmp,".--." ) == 0) - cout << "P"; - else if(strcmp(tmp,"--.-" ) == 0) - cout << "Q"; - else if(strcmp(tmp,".-." ) == 0) - cout << "R"; - else if(strcmp(tmp,"..." ) == 0) - cout << "S"; - else if(strcmp(tmp,"-" ) == 0) - cout << "T"; - else if(strcmp(tmp,"..-" ) == 0) - cout << "U"; - else if(strcmp(tmp,"...-" ) == 0) - cout << "V"; - else if(strcmp(tmp,".--" ) == 0) - cout << "W"; - else if(strcmp(tmp,"-..-" ) == 0) - cout << "X"; - else if(strcmp(tmp,"-.--" ) == 0) - cout << "Y"; - else if(strcmp(tmp,"--.." ) == 0) - cout << "Z"; - else if(strcmp(tmp,"-----" ) == 0) - cout << "0"; - else if(strcmp(tmp,".----" ) == 0) - cout << "1"; - else if(strcmp(tmp,"..---" ) == 0) - cout << "2"; - else if(strcmp(tmp,"...--" ) == 0) - cout << "3"; - else if(strcmp(tmp,"....-" ) == 0) - cout << "4"; - else if(strcmp(tmp,"....." ) == 0) - cout << "5"; - else if(strcmp(tmp,"-...." ) == 0) - cout << "6"; - else if(strcmp(tmp,"--..." ) == 0) - cout << "7"; - else if(strcmp(tmp,"---.." ) == 0) - cout << "8"; - else if(strcmp(tmp,"----." ) == 0) - cout << "9"; - else if(strcmp(tmp,".-.-.-" ) == 0) - cout << "."; - else if(strcmp(tmp,"--..--" ) == 0) - cout << ","; - else if(strcmp(tmp,"..--.." ) == 0) - cout << "?"; - else if(strcmp(tmp,".----." ) == 0) - cout << "'"; - else if(strcmp(tmp,"-.-.--" ) == 0) - cout << "!"; - else if(strcmp(tmp,"-..-." ) == 0) - cout << "/"; - else if(strcmp(tmp,"-.--." ) == 0) - cout << "("; - else if(strcmp(tmp,"-.--.-" ) == 0) - cout << ")"; - else if(strcmp(tmp,".-..." ) == 0) - cout << "&"; - else if(strcmp(tmp,"---..." ) == 0) - cout << ":"; - else if(strcmp(tmp,"-.-.-." ) == 0) - cout << ";"; - else if(strcmp(tmp,"-...-" ) == 0) - cout << "="; - else if(strcmp(tmp,".-.-." ) == 0) - cout << "+"; - else if(strcmp(tmp,"-....-" ) == 0) - cout << "-"; - else if(strcmp(tmp,"..--.-" ) == 0) - cout << "_"; - else if(strcmp(tmp,".-..-." ) == 0) - cout << char(34); - else if(strcmp(tmp,".--.-." ) == 0) - cout << "@"; - if(ar[j+1] == ' ' ) { - cout << " "; - j++; - } - memset(tmp,'\0',sizeof(tmp)); - index = 0; - } - } - cout << endl; - if(i != T) cout << endl; - } - return EXIT_SUCCESS; +int main() +{ + int t; + char c; + string input; + map m; + m[".-"] = 'A'; + m["-..."] = 'B'; + m["-.-."] = 'C'; + m["-.."] = 'D'; + m["."] = 'E'; + m["..-."] = 'F'; + m["--."] = 'G'; + m["...."] = 'H'; + m[".."] = 'I'; + m[".---"] = 'J'; + m["-.-"] = 'K'; + m[".-.."] = 'L'; + m["--"] = 'M'; + m["-."] = 'N'; + m["---"] = 'O'; + m[".--."] = 'P'; + m["--.-"] = 'Q'; + m[".-."] = 'R'; + m["..."] = 'S'; + m["-"] = 'T'; + m["..-"] = 'U'; + m["...-"] = 'V'; + m[".--"] = 'W'; + m["-..-"] = 'X'; + m["-.--"] = 'Y'; + m["--.."] = 'Z'; + m["-----"] = '0'; + m[".----"] = '1'; + m["..---"] = '2'; + m["...--"] = '3'; + m["....-"] = '4'; + m["....."] = '5'; + m["-...."] = '6'; + m["--..."] = '7'; + m["---.."] = '8'; + m["----."] = '9'; + m[".-.-.-"] = '.'; + m["--..--"] = ','; + m["..--.."] = '?'; + m[".----."] = '\''; + m["-.-.--"] = '!'; + m["-..-."] = '/'; + m["-.--."] = '('; + m["-.--.-"] = ')'; + m[".-..."] = '&'; + m["---..."] = ':'; + m["-.-.-."] = ';'; + m["-...-"] = '='; + m[".-.-."] = '+'; + m["-....-"] = '-'; + m["..--.-"] = '_'; + m[".-..-."] = '"'; + m[".--.-."] = '@'; + cin >> t; + getline(cin, input);// \n + for (int cases = 0; cases < t; cases++) + { + if (cases > 0) + { + cout << endl; + } + cout << "Message #" << cases + 1 << endl; + getline(cin, input); + stringstream ss; + ss << input; + while (ss >> input) + { + //if (ss.good()) + // cout << "ss is still good" << endl; + //else + // cout << "ss is NOT good" << endl; + //c = ss.peek(); + //cout << "peek() char = " << c << endl; + //ss.get(c); + //if (ss.good()) + // cout << "ss is STILL good" << endl; + //else + // cout << "ss is DEFINETLY NOT good" << endl; + //c = ss.peek(); + //cout << "peek() next char = " << c << endl; + cout << m[input]; + if (ss.good()) + { + c = ss.get(); + c = ss.peek(); + if (c == ' ') + { + cout << " "; + } + } + } + cout << endl; + } + return 0; } - diff --git a/11224.cpp b/11224.cpp new file mode 100644 index 0000000..48df067 --- /dev/null +++ b/11224.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +struct Time +{ + int m, s, h; + bool operator<(Time &t) + { + if (m < t.m) + { + return true; + } + if (m > t.m) + { + return false; + } + if (s < t.s) + { + return true; + } + if (s > t.s) + { + return false; + } + if (h < t.h) + { + return true; + } + if (h > t.h) + { + return false; + } + return false; + } + bool operator==(Time &t) + { + return m == t.m && s == t.s && h == t.h; + } +}; +struct Record +{ + int rank; + Time t; +} comp[1000], sec[8]; +int n; +int main() +{ + int T, rank; + scanf("%d", &T); + for (int cases = 1; cases <= T; cases++) + { + scanf("%d", &n); + scanf("%d", &rank); + for (int i = 0; i < n - 1; i++) + { + scanf("%d %d:%d:%d", &comp[i].rank, &comp[i].t.m, &comp[i].t.s, &comp[i].t.h); + } + Time t; + int bestX = 0, bestY = 0; + for (t.m = 0; t.m < 10; t.m++) + for (t.s = 0; t.s < 60; t.s++) + for (t.h = 0; t.h < 100; t.h++) + { + int X = 0; + for (int i = 0; i < n - 1 && X <= 7; i++) + if (comp[i].t < t || (comp[i].t == t && comp[i].rank < rank)) + { + sec[X++] = comp[i]; + } + if (X >= 8) + { + continue; + } + int Y = 0; + for (int i = 0; i < X; i++) + if (sec[i].rank < rank) + { + Y++; + } + if (Y <= 2 && X > bestX) + { + bestX = X; + bestY = Y; + } + } + printf("Case #%d:\n", cases); + printf("Gustavo should be #%d during the qualification to achieve position #%d in the final.\n", bestX + 1, bestY + 1); + } +} diff --git a/11225.cpp b/11225.cpp new file mode 100644 index 0000000..cb553d1 --- /dev/null +++ b/11225.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +bool is_oudler(const string &s) +{ + return s == "one" || s == "twenty-one" || s == "fool"; +} + +int get_limit(int oudlers) +{ + switch (oudlers) + { + case 0: + return 56; + break; + case 1: + return 51; + break; + case 2: + return 41; + break; + case 3: + return 36; + break; + } +} + +int main() +{ + int t, n; + string input; + int oudlers; + double collected; + map points; + points["one"] = 4.5; + points["ace"] = 0.5; + points["two"] = 0.5; + points["three"] = 0.5; + points["four"] = 0.5; + points["five"] = 0.5; + points["six"] = 0.5; + points["seven"] = 0.5; + points["eight"] = 0.5; + points["nine"] = 0.5; + points["ten"] = 0.5; + points["jack"] = 1.5; + points["knight"] = 2.5; + points["queen"] = 3.5; + points["king"] = 4.5; + points["eleven"] = 0.5; + points["twelve"] = 0.5; + points["thirteen"] = 0.5; + points["fourteen"] = 0.5; + points["fifteen"] = 0.5; + points["sixteen"] = 0.5; + points["seventeen"] = 0.5; + points["eighteen"] = 0.5; + points["nineteen"] = 0.5; + points["twenty"] = 0.5; + points["twenty-one"] = 4.5; + points["fool"] = 4.5; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n; + if (casenum) + { + cout << endl; + } + oudlers = 0; + collected = 0; + for (int i = 0; i < n; i++) + { + cin >> input; + if (is_oudler(input)) + { + oudlers++; + } + collected += points[input]; + getline(cin, input); + } + int limit = get_limit(oudlers); + int won = collected; + cout << "Hand #" << casenum + 1 << endl; + cout << "Game " << (won >= limit ? "won" : "lost") << " by " << abs(won - limit) << " point(s)." << endl; + } + return 0; +} diff --git a/11226.cpp b/11226.cpp new file mode 100644 index 0000000..b9257e6 --- /dev/null +++ b/11226.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define N 500001 +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +int a[N]; +int b[N]; +int table[N]; +int val[N]; + +int generate() +{ + for (int i = 2; i < N; i += 2) + { + a[i] = 0; + } + for (int j = 1; j < N; j += 2) + { + a[j] = 1; + } + for (int i = 3; i * i <= N; i += 2) + { + if (a[i] == 1) + for (int j = 2 * i; j < N; j += i) + { + a[j] = 0; + } + } + int index = 0; + b[index++] = 2; + for (int i = 3; i < N; i += 2) + if (a[i]) + { + b[index++] = i; + } + return index; +} + +void precalc(int *table, int tar, int index) +{ + int n = tar, val = 0; + for (int i = 0; i < index && b[i] * b[i] <= n; i++) + { + if (n % b[i] == 0) + { + while (true) + { + if (n / b[i] == 0 || n % b[i] != 0) + { + break; + } + val += b[i]; + n /= b[i]; + } + } + } + if (n != 1) + { + val += n; + } + table[tar] = val; + return; +} + +void construct(int *table, int index) +{ + REP(i, 2, N) + { + precalc(table, i, index); + } +} + +int solve(int *val, int *table, int step, int now) +{ + if (val[now] != -1) + { + return val[now]; + } + int next = table[now]; + if (now == next) + { + return val[now] = step; + } + val[now] = solve(val, table, step + 1, next) + step; + return val[now]; +} + +main() +{ + int index = generate(); + construct(table, index); + fill(val, val + N, -1); + REP(i, 2, N) + { + if (val[i] != -1) + { + continue; + } + solve(val, table, 1, i); + } + int tc = 1, te; + cin >> te; + while (te--) + { + int a, b; + cin >> a >> b; + if (b < a) + { + swap(a, b); + } + int ans = 0; + REP(i, a, b + 1) + ans = max(ans, val[i]); + cout << "Case #" << tc++ << ":" << endl + << ans << endl; + } + return false; +} diff --git a/11227.cpp b/11227.cpp new file mode 100644 index 0000000..5793d34 --- /dev/null +++ b/11227.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define MID(x, y) ((x + y) >> 1) +#define L(x) (x << 1) +#define R(x) (x << 1 | 1) +#define BUG puts("here!!!") +#define STOP system("pause") + +const int MAX = 120; +struct point +{ + double x, y; +}; +point p[MAX]; + +double crossProduct(point a, point b, point c)//å‘é‡ ac 在 ab çš„æ–¹å‘ é¡ºæ—¶é’ˆæ˜¯æ­£ +{ + return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y); +} +bool cmp(point a, point b) +{ + if (a.x == b.x) + { + return a.y < b.y; + } + return a.x < b.x; +} +bool equal_cmp(point a, point b) +{ + return a.x == b.x && a.y == b.y; +} + +int main() +{ + int ncases, n, ind = 1; + scanf("%d", &ncases); + while (ncases--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + } + sort(p, p + n, cmp); + n = unique(p, p + n, equal_cmp) - p; + printf("Data set #%d contains ", ind++); + if (n == 1) + { + puts("a single gnu."); + continue; + } + int ans = 0; + for (int i = 0; i < n; i++) + for (int k = i + 1; k < n; k++) + { + int sum = 2; + for (int l = k + 1; l < n; l++) + if (crossProduct(p[i], p[k], p[l]) == 0) + { + sum++; + } + if (sum > ans) + { + ans = sum; + } + } + printf("%d gnus, out of which a maximum of %d are aligned.\n", n, ans); + } + return 0; +} diff --git a/11228.cpp b/11228.cpp new file mode 100644 index 0000000..971fe6b --- /dev/null +++ b/11228.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define init(i, a, n) \ + for (i = 0; i < n; i++) \ + a[i] = i +typedef struct edge +{ + int f, t; + double d; +} E; +int getParent(int i, int *set) +{ + if (i == set[i]) + { + return i; + } + else + { + return (set[i] = getParent(set[i], set)); + } +} +int isUnion(int a, int b, int *set) +{ + return getParent(a, set) == getParent(b, set); +} +void makeUnion(int a, int b, int *set) +{ + set[getParent(a, set)] = getParent(b, set); +} +int cmp(const void *a, const void *b) +{ + return (((E *)a)->d < ((E *)b)->d) ? -1 : 1; +} +int distance(int x1, int y1, int x2, int y2) +{ + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); +} +int main() +{ + E list[500505]; + int coordinates[1005][2], i, j, c, cc, v, t, d, top, set[1005], states; + double roads, railroads; + scanf("%d", &c); + cc = 1; + while (c--) + { + v = 0; + scanf("%d %d", &v, &t); + for (i = 0; i < v; i++) + { + scanf("%d %d", coordinates[i], coordinates[i] + 1); + } + top = 0; + for (i = 0; i < v; i++) + for (j = i + 1; j < v; j++) + { + list[top].f = i; + list[top].t = j; + list[top].d = sqrt(distance(coordinates[i][0], coordinates[i][1], coordinates[j][0], coordinates[j][1])); + top++; + } + qsort(list, top, sizeof(E), &cmp); + d = 0; + roads = 0; + railroads = 0; + states = 0; + ; + init(i, set, v); + for (i = 0; i < top && d < v - 1; i++) + { + if (!isUnion(list[i].f, list[i].t, set)) + { + makeUnion(list[i].f, list[i].t, set); + d++; + if (list[i].d > t) + { + railroads += list[i].d; + states++; + } + else + { + roads += list[i].d; + } + } + } + printf("Case #%d: %d %.lf %.lf\n", cc++, states + 1, roads, railroads); + } + return 0; +} diff --git a/11230.cpp b/11230.cpp new file mode 100644 index 0000000..1b434d3 --- /dev/null +++ b/11230.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +char g[105][105]; +int n, m, r, c; + +int main() +{ + while (scanf("%d%d%d%d", &n, &m, &r, &c) == 4) + { + if (n + m + r + c == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%s", g[i]); + } + int ret = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] == '0') + { + continue; + } + if (i + r - 1 >= n || j + c - 1 >= m) + { + ret = -1; + j = m, i = n; + continue; + } + ret++; + for (int p = 0; p < r; p++) + { + for (int q = 0; q < c; q++) + { + if (g[i + p][j + q] == '1') + { + g[i + p][j + q] = '0'; + } + else + { + g[i + p][j + q] = '1'; + } + } + } + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/11231.cpp b/11231.cpp new file mode 100644 index 0000000..bdbbf57 --- /dev/null +++ b/11231.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +// @ http://algoshare.blogspot.sg/2009/01/uva-problem-11231-sample-algorithm.html + +int main() +{ + int n, m, c; + while (scanf("%d%d%d", &n, &m, &c) == 3) + { + if (n + m + c == 0) + { + break; + } + if (c == 0) + { + printf("%d\n", (n - 7) * (m - 7) / 2); + } + else// c == 1 && (n - 7) * (m - 7) % 2 == 1 + { + printf("%d\n", ((n - 7) * (m - 7) + 1) / 2); + } + } + return 0; +} diff --git a/11232.cpp b/11232.cpp new file mode 100644 index 0000000..f3891f3 --- /dev/null +++ b/11232.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +const double pi = acos(-1.0), eps = 1e-6; + +int main() +{ + double w, h, s, d; + while (scanf("%lf %lf", &w, &h) == 2) + { + if (fabs(w) < eps && fabs(h) < eps) + { + break; + } + if (h < w) + { + swap(w, h); + } + d = h / (pi + 1); + d = min(d, w); + s = pi * d * d * 0.25 * w; + d = 2.0 * h / 3.0; + if (pi * d <= w) + { + d = min(d, w); + s = max(s, pi * h * h * h / 27.0); + } + s = max(s, w * w * (pi * h - w) / (4 * pi * pi)); + printf("%.3lf\n", s); + } + return 0; +} diff --git a/11233.cpp b/11233.cpp index ed5e1b9..63239e3 100644 --- a/11233.cpp +++ b/11233.cpp @@ -1,70 +1,61 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int L, N, i; - map irr; - map ::iterator it; - cin>>L>>N; - for(i=0; i>first>>second; - irr[first] = second; - } - for(i=0; i>str; - it = irr.find(str); - if(it != irr.end()){ - cout<< it->second < + +using namespace std; + +bool is_vowel(char c) +{ + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; +} + +bool special_case(const string &s) +{ + if (s[s.size() - 1] == 'o' || s[s.size() - 1] == 's' || s[s.size() - 1] == 'x') + { + return true; + } + if (s.size() < 2) + { + return false; + } + if (s[s.size() - 2] == 'c' && s[s.size() - 1] == 'h' || s[s.size() - 2] == 's' && s[s.size() - 1] == 'h') + { + return true; + } + return false; +} + +int main() +{ + int L, N; + string w1, w2; + map irregular; + cin >> L >> N; + while (L--) + { + cin >> w1 >> w2; + irregular[w1] = w2; + } + while (N--) + { + cin >> w1; + if (irregular.find(w1) != irregular.end()) + { + cout << irregular[w1] << endl; + continue; + } + if (w1[w1.size() - 1] == 'y' && w1.size() > 1 && !is_vowel(w1[w1.size() - 2])) + { + w1[w1.size() - 1] = 'i'; + cout << w1 << "es" << endl; + } + else if (special_case(w1)) + { + cout << w1 << "es" << endl; + } + else + { + cout << w1 << "s" << endl; + } + } + return 0; +} diff --git a/11234.cpp b/11234.cpp new file mode 100644 index 0000000..f8da7de --- /dev/null +++ b/11234.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +typedef struct _tree +{ + char v; + struct _tree *lf, *rt; +} tree; +char in[10005]; +int ptr, n, len; +tree *Q[10005]; +tree *run() +{ + tree *r = (tree *)malloc(sizeof(tree)); + r->v = in[--ptr]; + if (r->v < 'a') + { + r->rt = run(); + r->lf = run(); + } + return r; +} +void des(tree *x) +{ + if (x->v < 'a') + { + des(x->rt); + des(x->lf); + } + free(x); +} +int main() +{ + int t, n; + char ch; + tree *root; + scanf("%d", &t); + while (t--) + { + scanf("%s", in); + ptr = n = strlen(in); + root = run(); + Q[0] = root, len = 1; + for (ptr = 0; ptr < len; ptr++) + { + ch = Q[ptr]->v; + in[n - 1 - ptr] = ch; + if (ch < 'a') + { + Q[len++] = Q[ptr]->lf; + Q[len++] = Q[ptr]->rt; + } + } + printf("%s\n", in); + } + return 0; +} diff --git a/11235.cpp b/11235.cpp new file mode 100644 index 0000000..4bdb99b --- /dev/null +++ b/11235.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +struct Ret +{ + int l, ln, maxi, r, rn; +}; + +const int N = (1 << 18); +const int M = 2 * N; + +int in[N]; +Ret rmq[M]; + +Ret init(int now, int l, int r) +{ + if (l == r) + { + return rmq[now] = (Ret){ + in[l], 1, 1, in[r], 1}; + } + Ret L = init(now * 2 + 1, l, (l + r) / 2); + Ret R = init(now * 2 + 2, (l + r) / 2 + 1, r); + int tmp = L.r == R.l ? L.rn + R.ln : 0; + int maxi = 0; + maxi = max(tmp, max(L.maxi, R.maxi)); + int retl = L.ln, retr = R.rn; + if (L.l == R.l) + { + retl += R.ln; + } + if (L.r == R.r) + { + retr += L.rn; + } + //cout << now <<" " << rmq[now] << endl; + return rmq[now] = (Ret){ + L.l, retl, maxi, R.r, retr}; +} + +Ret query(int now, int l, int r, int ql, int qr) +{ + //cout << now <<" " << l <<" " << r <<" " << ql <<" " << qr << endl; + if (l == ql && r == qr) + { + return rmq[now]; + } + int ml = (l + r) / 2, mr = (l + r) / 2 + 1; + Ret ret; + if (qr <= ml) + { + Ret child = query(now * 2 + 1, l, ml, ql, min(qr, ml)); + ret = child; + } + else if (mr <= ql) + { + Ret child = query(now * 2 + 2, mr, r, max(ql, mr), qr); + ret = child; + } + else//both + { + Ret L = query(now * 2 + 1, l, ml, ql, min(qr, ml)); + Ret R = query(now * 2 + 2, mr, r, max(ql, mr), qr); + int tmp = L.r == R.l ? L.rn + R.ln : 0; + int maxi = max(tmp, max(L.maxi, R.maxi)); + int retl = L.ln, retr = R.rn; + if (L.l == R.l) + { + retl += R.ln; + } + if (L.r == R.r) + { + retr += L.rn; + } + return (Ret){ + L.l, retl, maxi, R.r, retr}; + } + return ret; +} + +int main() +{ + int n, q; + while (scanf("%d%d", &n, &q) != -1 && n) + { + rep(i, n) scanf("%d", &in[i]); + int maxi = in[n - 1]; + int newn = 1; + while (newn < n) + { + newn *= 2; + } + REP(i, n, newn) + { + in[i] = maxi + i + 1; + } + n = newn; + init(0, 0, n - 1); + rep(i, q) + { + int l, r; + scanf("%d%d", &l, &r); + Ret ans = query(0, 0, n - 1, l - 1, r - 1); + printf("%d\n", ans.maxi); + } + } + return 0; +} diff --git a/11236.cpp b/11236.cpp new file mode 100644 index 0000000..30351f9 --- /dev/null +++ b/11236.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int v[4]; + +int main() +{ + for (int a = 1; a < 126; ++a) + { + int blim = (2002 - a) / 3; + for (int b = a; b <= blim; ++b) + { + int clim = (2001 - (a + b)) / 2; + for (int c = b; c <= clim; ++c) + { + int sum = a + b + c; + long long div = (long long)(a)*b * c; + if (div <= 1000000) + { + continue; + } + double d = sum * 1e6 / (div - 1e6); + if (d < c) + { + continue; + } + if (d - floor(d) < 1e-6) + { + sum = a + b + c + d; + if (sum <= 2000) + { + printf("%.2lf %.2lf %.2lf %.2lf\n", a / 100.0, b / 100.0, c / 100.0, d / 100.0); + } + } + } + } + } + return 0; +} diff --git a/11237.cpp b/11237.cpp new file mode 100644 index 0000000..b94d156 --- /dev/null +++ b/11237.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 100000; +int ind[N]; + +void op(int l, int r) +{ + int cnt = 0; + REP(i, l, r + 1) + { + if (cnt) + { + cout << ' '; + } + cnt++; + cout << i + 1; + } + cout << endl; +} + +main() +{ + int n, c; + while (scanf("%d%d", &c, &n) && c) + { + rep(i, c) ind[i] = -2; + int sum = 0; + int l, r; + ind[0] = -1; + rep(i, n) + { + int tmp; + scanf("%d", &tmp); + sum = (sum + tmp) % c; + //cout << sum<<" "; + if (ind[sum] != -2) + { + l = ind[sum] + 1; + r = i; + } + ind[sum] = i; + } + //cout << endl; + op(l, r); + } + return false; +} diff --git a/11238.cpp b/11238.cpp new file mode 100644 index 0000000..b347d77 --- /dev/null +++ b/11238.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +long long dp[4][12][350], ans[301]; + +int main() +{ + int n, i, j, k, l, p, q; + dp[0][0][0] = 1; + for (i = 1; i < 11; i++) + { + for (j = 0; j < 301; j++) + { + for (p = 0; p < 10; p++) + { + for (q = 0; q + p < 10; q++) + { + dp[0][i][j + p + q] += dp[0][i - 1][j]; + dp[0][i][j + p + p + q] += dp[1][i - 1][j]; + dp[0][i][j + p + p + q + q] += dp[2][i - 1][j]; + dp[0][i][j + p + p + p + q + q] += dp[3][i - 1][j]; + } + dp[1][i][j + p + q] += dp[0][i - 1][j]; + dp[1][i][j + p + p + q] += dp[1][i - 1][j]; + dp[1][i][j + p + p + q + q] += dp[2][i - 1][j]; + dp[1][i][j + p + p + p + q + q] += dp[3][i - 1][j]; + } + dp[2][i][j + p] += dp[0][i - 1][j]; + dp[2][i][j + p + p] += dp[1][i - 1][j]; + dp[3][i][j + p + p] += dp[2][i - 1][j]; + dp[3][i][j + p + p + p] += dp[3][i - 1][j]; + } + } + for (j = 0; j < 301; j++) + { + dp[0][i][j] += dp[0][i - 1][j]; + for (p = 0; p < 10; p++) + { + dp[0][i][j + p] += dp[1][i - 1][j]; + for (q = 0; q + p < 11; q++) + { + dp[0][i][j + p + q] += dp[2][i - 1][j]; + dp[0][i][j + p + p + q] += dp[3][i - 1][j]; + } + } + dp[0][i][j + p] += dp[1][i - 1][j]; + for (q = 0; q < 11; q++) + { + dp[0][i][j + p + q] += dp[2][i - 1][j]; + dp[0][i][j + p + p + q] += dp[3][i - 1][j]; + } + } + for (j = 0; j < 301; j++) + { + ans[j] = dp[0][i][j]; + } + while (scanf("%d", &n) == 1) + { + if (n == -1) + { + break; + } + printf("%lld\n", ans[n]); + } + return 0; +} diff --git a/11239.cpp b/11239.cpp index f9eba59..a96de03 100644 --- a/11239.cpp +++ b/11239.cpp @@ -1,92 +1,92 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +struct draft +{ + string name_fst; + int qty; + draft(string name_fst1 = "", int qty1 = 0) : name_fst(name_fst1), qty(qty1) {} +}; -map Pro; -map::iterator pit; -map Cnt; -map::iterator cit; -map Del; -map::iterator dit; -vector Vec; +string line, capital; +map> mapa; +set s; +vector res; -bool comp(string A, string B){ - if (Cnt[A] != Cnt[B]) - return Cnt[A] > Cnt[B]; - return A < B; +bool cmp(draft a, draft b) +{ + if (a.qty > b.qty) + { + return true; + } + if (a.qty < b.qty) + { + return false; + } + if (a.name_fst < b.name_fst) + { + return true; + } + return false; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - bool test = true; - while (true){ - string A = "", B = ""; - Pro.clear(); Cnt.clear(); Del.clear(); Vec.clear(); - while (true){ - string str; - getline(cin, str); - if (str == "0"){ - test = false; +int main() +{ + while (getline(cin, line)) + { + if (line == "0") + { break; } - if (str == "1"){ - break; + if (line[0] == '1') + { + FOREACH(s, it_set) + { + int cont = 0; + FOREACH(mapa, it) + { + if (it->second.count(*it_set)) + { + cont++; + } + } + if (cont > 1) + { + FOREACH(mapa, it) + { + if (it->second.count(*it_set)) + { + it->second.erase(it->second.find(*it_set)); + } + } + } + } + FOREACH(mapa, it) + { + res.push_back(draft(it->first, it->second.size())); + } + sort(res.begin(), res.end(), cmp); + FOREACH(res, it) + { + printf("%s %d\n", it->name_fst.c_str(), it->qty); + } + res.clear(); + mapa.clear(); + s.clear(); + continue; } - if (isupper(str[0])){ - A = str; - Cnt[A] = 0; + if (line[0] < 'A' || line[0] > 'Z') + { + mapa[capital].insert(line); + s.insert(line); } - else{ - B = str; - pit = Pro.find(B); - dit = Del.find(B); - if (pit == Pro.end() && dit == Del.end()) - Pro[B] = A; - if (pit != Pro.end() && (*pit).second != A){ - Del[(*pit).first] = true; - Pro.erase(pit); - } + else + { + mapa[line], capital = line; } } - if (!test) - break; - for (pit = Pro.begin(); pit != Pro.end(); pit++) - ++Cnt[(*pit).second]; - for (cit = Cnt.begin(); cit != Cnt.end(); cit++) - Vec.push_back((*cit).first); - - sort(Vec.begin(), Vec.end(), comp); - int i, j; - FOI(i, 0, Vec.size()-1) - cout << Vec[i] << " " << Cnt[Vec[i]] << endl; - } return 0; } - diff --git a/1124.cpp b/1124.cpp index 5386539..dda7889 100644 --- a/1124.cpp +++ b/1124.cpp @@ -1,13 +1,13 @@ -#include -#include -#include +#include + using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - while (getline(cin, str)) - cout << str << endl; +int main() +{ + string input; + while (getline(cin, input)) + { + printf("%s\n", input.c_str()); + } return 0; } diff --git a/11240.cpp b/11240.cpp new file mode 100644 index 0000000..451cefc --- /dev/null +++ b/11240.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int count, n, i, in, max, type, ans; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &n, &max); + for (i = 1, ans = 1, type = 0; i < n; i++) + { + scanf("%d", &in); + if (type && in > max) + { + type = 1 - type; + ans++; + } + else if (!type && in < max) + { + type = 1 - type; + ans++; + } + max = in; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11241.cpp b/11241.cpp new file mode 100644 index 0000000..39c183b --- /dev/null +++ b/11241.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int main() +{ + int isH, isT, isD; + char buf[1000], *token; + double T, H, D, h, e; + while (fgets(buf, 1000, stdin)) + { + if (buf[0] == 'E') + { + break; + } + isH = isT = isD = 0; + token = strtok(buf, " "); + switch (token[0]) + { + case 'H': + token = strtok(NULL, " "); + isH = 1; + H = atof(token); + break; + case 'T': + token = strtok(NULL, " "); + isT = 1; + T = atof(token); + break; + case 'D': + token = strtok(NULL, " "); + isD = 1; + D = atof(token); + break; + } + token = strtok(NULL, " "); + switch (token[0]) + { + case 'H': + token = strtok(NULL, " "); + isH = 1; + H = atof(token); + break; + case 'T': + token = strtok(NULL, " "); + isT = 1; + T = atof(token); + break; + case 'D': + token = strtok(NULL, " "); + isD = 1; + D = atof(token); + break; + } + if (isT && isD) + { + e = 6.11 * exp(5417.7530 * ((1 / 273.16) - (1 / (D + 273.16)))); + h = 0.5555 * (e - 10); + H = T + h; + } + else if (isH && isD) + { + e = 6.11 * exp(5417.7530 * ((1 / 273.16) - (1 / (D + 273.16)))); + h = 0.5555 * (e - 10); + T = H - h; + } + else + { + h = H - T; + e = h / 0.5555 + 10; + D = 1 / (1 / 273.16 - (log(e / 6.11)) / 5417.7530) - 273.16; + } + printf("T %.1f D %.1f H %.1f\n", T, D, H); + } + return 0; +} diff --git a/11242.cpp b/11242.cpp new file mode 100644 index 0000000..d77311a --- /dev/null +++ b/11242.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +// calc all poss ratios and place them in an arr[] +// sort ratios[] +// loop over the arr[] and calc spread between each two adjacent ratios +// get max val among those +// forgetting to fix the decimal places gives WA +// rst indices on each input + +double ratio_[100], maxSpread; +int front[10], nFront, rear[10], nRear, ratioI; + +int main() +{ + while (cin >> nFront) + { + if (nFront == 0) + { + break; + } + cin >> nRear; + for (int i = 0; i < nFront; ++i) + { + cin >> front[i]; + } + for (int i = 0; i < nRear; ++i) + { + cin >> rear[i]; + } + ratioI = 0; + for (int i = 0; i < nFront; ++i) + { + for (int j = 0; j < nRear; ++j) + { + ratio_[ratioI++] = 1.0 * rear[j] / front[i]; + } + } + sort(&ratio_[0], &ratio_[ratioI]); + maxSpread = 0; + for (int i = 0; i < ratioI - 1; ++i) + { + maxSpread = max(maxSpread, ratio_[i + 1] / ratio_[i]); + } + cout << fixed << setprecision(2) << round(maxSpread * 100) / 100 << endl; + } + return 0; +} diff --git a/11243.cpp b/11243.cpp new file mode 100644 index 0000000..7c2292d --- /dev/null +++ b/11243.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES +#define N 1000 +double x[100], y[100], xx, yy, base, scale; +int rep, n, i, j, k, T; + +int main() +{ + double minx, maxx, miny, maxy, dx, dy, dd, best; + int besti; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%lf%lf", &x[i], &y[i]); + } + best = 1000; + base = 0; + scale = 1; + for (rep = 0; rep < 10; rep++) + { + besti = 0; + for (i = 0; i < N; i++) + { + double sininc = sin(base + scale * i * M_PI / 2 / N); + double cosinc = cos(base + scale * i * M_PI / 2 / N); + maxx = maxy = -1e99; + minx = miny = 1e99; + for (j = 0; j < n; j++) + { + xx = cosinc * x[j] - sininc * y[j]; + yy = sininc * x[j] + cosinc * y[j]; + //printf("x y xx yy %lg %lg %lg %lg\n",x[j],y[j],xx,yy); + if (xx < minx) + { + minx = xx; + } + if (xx > maxx) + { + maxx = xx; + } + if (yy < miny) + { + miny = yy; + } + if (yy > maxy) + { + maxy = yy; + } + } + dx = maxx - minx; + dy = maxy - miny; + dd = dx; + if (dy > dd) + { + dd = dy; + } + if (dd < best) + { + best = dd; + besti = i; + } + //printf("i %d dd %0.5lf\n",i,dd); + } + base += scale * (besti - 1) * M_PI / 2 / N; + scale = scale / N * 2; + } + printf("%0.2lf\n", best * best); + } + return 0; +} diff --git a/11244.cpp b/11244.cpp index 6ddb8e1..0f278c7 100644 --- a/11244.cpp +++ b/11244.cpp @@ -1,26 +1,209 @@ -#include -using namespace std; -int main(){ - for(;;){ - long long r,c; - cin>>r>>c; - if(r==0 && c==0) - break; - string star[r+1]; - long long count=0,i=0,j=0; - for(i=0;i>star[i]; - } - for(i=0;i + +using namespace std; + +char sky[100][100]; +int total; + +struct pos +{ + int r; + int c; +}; + +void remove_object(int r, int c, int max_r, int max_c) +{ + pos tmp; + tmp.r = r; + tmp.c = c; + sky[r][c] = '.'; + queue q; + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + r = tmp.r; + c = tmp.c; + total--; + if (r > 0) + { + if (sky[r - 1][c] == '*') + { + tmp.r = r - 1; + tmp.c = c; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (c > 0) + { + if (sky[r][c - 1] == '*') + { + tmp.r = r; + tmp.c = c - 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (r < max_r - 1) + { + if (sky[r + 1][c] == '*') + { + tmp.r = r + 1; + tmp.c = c; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (c < max_c - 1) + { + if (sky[r][c + 1] == '*') + { + tmp.r = r; + tmp.c = c + 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (r > 0 && c > 0) + { + if (sky[r - 1][c - 1] == '*') + { + tmp.r = r - 1; + tmp.c = c - 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (r < max_r - 1 && c < max_c - 1) + { + if (sky[r + 1][c + 1] == '*') + { + tmp.r = r + 1; + tmp.c = c + 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (r > 0 && c < max_c - 1) + { + if (sky[r - 1][c + 1] == '*') + { + tmp.r = r - 1; + tmp.c = c + 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + if (r < max_r - 1 && c > 0) + { + if (sky[r + 1][c - 1] == '*') + { + tmp.r = r + 1; + tmp.c = c - 1; + q.push(tmp); + sky[tmp.r][tmp.c] = '.'; + } + } + } +} + +void check_star(int r, int c, int max_r, int max_c) +{ + bool good_star = true; + if (r > 0) + { + if (sky[r - 1][c] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (c > 0) + { + if (sky[r][c - 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (r < max_r - 1) + { + if (sky[r + 1][c] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (c < max_c - 1) + { + if (sky[r][c + 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (r > 0 && c > 0) + { + if (sky[r - 1][c - 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (r < max_r - 1 && c < max_c - 1) + { + if (sky[r + 1][c + 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (r > 0 && c < max_c - 1) + { + if (sky[r - 1][c + 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } + if (r < max_r - 1 && c > 0) + { + if (sky[r + 1][c - 1] == '*') + { + remove_object(r, c, max_r, max_c); + } + } +} + +int main() +{ + int r, c; + string input; + while (cin >> r >> c) + { + if (r == 0 && c == 0) + { + break; + } + total = 0; + for (int i = 0; i < r; i++) + { + cin >> input; + for (int y = 0; y < c; y++) + { + sky[i][y] = input[y]; + if (input[y] == '*') + { + total++; + } + } + } + for (int i = 0; i < r; i++) + { + for (int y = 0; y < c; y++) + { + if (sky[i][y] == '*') + { + check_star(i, y, r, c); + } + } + } + cout << total << endl; + } + return 0; +} diff --git a/11245.cpp b/11245.cpp new file mode 100644 index 0000000..3d87468 --- /dev/null +++ b/11245.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +long long int s[500], len; +int main() +{ + long long int t, n, k; + scanf("%lld", &t); + while (t--) + { + scanf("%lld %lld", &n, &k); + len = 0; + while (n) + { + s[len++] = n % (k - 1); + n /= (k - 1); + } + long long int value = 0; + for (long long int i = len - 1; i >= 0; i--) + { + value = value * k + s[i]; + } + long long int sub = 0; + long long int p; + for (p = 0; p < len && s[p] == 0; p++) + ; + for (p--; p >= 0; p--) + { + sub = sub * 2 + 1; + } + printf("%lld\n", value - sub); + } +} diff --git a/11246.cpp b/11246.cpp new file mode 100644 index 0000000..5858d27 --- /dev/null +++ b/11246.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int solve(int n, int k) +{ + int ret = 0; + while (n != 0) + { + //cout << n <<" " << n/k <<" " << n/k/k<> te; + while (te--) + { + int n, k; + cin >> n >> k; + cout << solve(n, k) << endl; + } + return 0; +} diff --git a/11247.cpp b/11247.cpp index 8034593..4ee220e 100644 --- a/11247.cpp +++ b/11247.cpp @@ -1,17 +1,34 @@ -#include -#include -using namespace std; -int main(){ - for(;;){ - long double m,x; - cin>>m>>x; - if(m==0 && x==0) - break; - long long v=(long long)floor(((m-1)/(1.0-x/100.0))); - if(v>=m) - cout< + +using namespace std; + +long long int m, x; + +int main() +{ + while (scanf("%lld %lld", &m, &x), x || m) + { + if (x == 100 || x == 0 || m == 1) + { + puts("Not found"); + } + else + { + long long a = (m - 1) * 100, b = (100 - x); + long long int v = a / b; + if (a % b == 0) + { + v--; + } + if (v >= m) + { + printf("%lld\n", v); + } + else + { + puts("Not found"); + } + } + } + return 0; +} diff --git a/11248.cpp b/11248.cpp new file mode 100644 index 0000000..5ad7837 --- /dev/null +++ b/11248.cpp @@ -0,0 +1,193 @@ +#include + +using namespace std; + +typedef long long LL; + +const int inf = 2000000005; +const int N = 105; + +struct Road +{ + int u, v, w; + friend bool operator<(const Road &a, const Road &b) + { + if (a.u != b.u) + { + return a.u < b.u; + } + return a.v < b.v; + } +} road[99999], ans[99999]; +struct Edge +{ + int b, c, nxt; +} e[99999], ee[99999]; +int p[N]; +int cnt, ct; +int n, m, cap; +int flow[2][N]; + +int pre[N], dis[N], gap[N], cur[N]; +int sap(int s, int t, int n) +{ + int i, v, u, c; + int aug = -1, maxflow = 0; + for (i = 0; i <= n; i++) + { + cur[i] = p[i]; + } + memset(dis, 0, sizeof(dis)); + memset(gap, 0, sizeof(gap)); + u = pre[s] = s, + gap[0] = n; + while (dis[s] < n) + { + loop: + for (int &ee = cur[u]; ee != -1; ee = e[ee].nxt) + { + v = e[ee].b; + c = e[ee].c; + if (c > 0 && dis[u] == dis[v] + 1) + { + if (aug == -1 || aug > c) + { + aug = c; + } + pre[v] = u; + u = v; + if (v == t) + { + maxflow += aug; + if (maxflow >= cap) + { + return maxflow; + } + for (u = pre[u]; v != s; v = u, u = pre[u]) + { + e[cur[u]].c -= aug, e[cur[u] ^ 1].c += aug; + } + aug = -1; + } + goto loop; + } + } + int mindis = n; + for (i = p[u]; i != -1; i = e[i].nxt) + { + v = e[i].b; + if (e[i].c > 0 && mindis > dis[v]) + { + cur[u] = i, mindis = dis[v]; + } + } + if ((--gap[dis[u]]) == 0) + { + break; + } + gap[dis[u] = mindis + 1]++; + u = pre[u]; + } + return maxflow; +} +void addedge(int a, int b, int c) +{ + e[cnt].b = b; + e[cnt].c = c; + e[cnt].nxt = p[a]; + p[a] = cnt++; + e[cnt].b = a; + e[cnt].c = 0; + e[cnt].nxt = p[b]; + p[b] = cnt++; +} +void init() +{ + memset(p, -1, sizeof(p)); + cnt = 0; +} + +int main() +{ + int i, j, t, cas = 0; + while (scanf("%d%d%d", &n, &m, &cap), n || m || cap) + { + init(); + for (i = 0; i < m; i++) + { + scanf("%d%d%d", &road[i].u, &road[i].v, &road[i].w); + } + sort(road, road + m); + for (i = 0; i < m; i++) + { + addedge(road[i].u, road[i].v, road[i].w); + } + int S = 1, T = n; + int ret = sap(S, T, T); + printf("Case %d: ", ++cas); + if (ret >= cap || n == 1) + { + puts("possible"); + continue; + } + for (i = 0; i < cnt; i++) + { + ee[i] = e[i]; + } + for (i = 1; i <= n; i++) + { + for (j = 0; j < cnt; j++) + { + e[j] = ee[j]; + } + if (i == 1) + { + flow[0][i] = cap; + } + else + { + flow[0][i] = sap(1, i, n); + } + for (j = 0; j < cnt; j++) + { + e[j] = ee[j]; + } + if (i == n) + { + flow[1][i] = cap; + } + else + { + flow[1][i] = sap(i, n, n); + } + } + ct = 0; + for (i = 0; i < m; i++) + { + int u = road[i].u; + int v = road[i].v; + int w = min(flow[0][u], flow[1][v]); + if (ret + w >= cap) + { + ct++; + ans[ct].u = u; + ans[ct].v = v; + } + } + if (ct == 0) + { + puts("not possible"); + } + else + { + printf("possible option:"); + printf("(%d,%d)", ans[1].u, ans[1].v); + for (i = 2; i <= ct; i++) + { + printf(",(%d,%d)", ans[i].u, ans[i].v); + } + puts(""); + } + } + return 0; +} diff --git a/11249.cpp b/11249.cpp new file mode 100644 index 0000000..cd60af1 --- /dev/null +++ b/11249.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int ary[100001], tbl[100001]; + +int main() +{ + int count, i, j, k, l, n, m, a, b, temp; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &k, &m); + ary[0] = temp = tbl[1] = k + 2; + for (i = 2, j = 0, l = 1, n = temp + temp; i < 100001; i++) + { + if (i == ary[j]) + { + tbl[i] = -1; + n++; + j++; + } + else + { + tbl[i] = n; + ary[l++] = n; + n += temp; + } + } + while (m--) + { + scanf("%d%d", &a, &b); + if (tbl[a] == b || tbl[b] == a) + { + printf("LOSING\n"); + } + else + { + printf("WINNING\n"); + } + } + printf("\n"); + } + return 0; +} diff --git a/1125.cpp b/1125.cpp new file mode 100644 index 0000000..6ba7c74 --- /dev/null +++ b/1125.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int main() +{ + int n, m, A[32767]; + while (scanf("%d %d", &n, &m) == 2) + { + int w, b, wsum = 0, bsum = 0; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &w, &b); + wsum += w, bsum += b; + A[i] = w; + } + if (wsum == bsum || n % 2 != 0) + { + puts("No solution"); + continue; + } + char major = 'W'; + if (wsum < bsum) + { + major = 'B'; + wsum = bsum; + for (int i = 0; i < n; i++) + A[i] = m - A[i]; + } + set dp[32767]; + set::iterator it; + dp[0].insert(0); + for (int i = 0; i < n; i++) + { + int w = A[i]; + for (int j = min(i, n / 2 - 1); j >= 0; j--) + { + for (it = dp[j].begin(); it != dp[j].end(); it++) + dp[j + 1].insert((*it) + w); + } + } + + int ret = -1; + int half = n / 2 * m / 2; + for (it = dp[n / 2].begin(); it != dp[n / 2].end(); it++) + { + if (*it <= wsum / 2 && *it > half) + ret = max(ret, *it); + } + if (ret < 0) + { + puts("No solution"); + } + else + { + printf("%c %.2lf\n", major, ret * 100.0 / (n / 2 * m)); + } + } + return 0; +} diff --git a/11250.cpp b/11250.cpp new file mode 100644 index 0000000..23f3d78 --- /dev/null +++ b/11250.cpp @@ -0,0 +1,1279 @@ +#include + +using namespace std; + +class BigInt +{ + int alloc, size; + unsigned *data, _data[5]; + + void reserve(int n) + { + if (data == NULL) + { + data = _data; + alloc = sizeof(_data) / sizeof(_data[0]); + } + if (alloc < n) + { + while (alloc < n) + { + alloc <<= 1; + } + if (data == _data) + { + data = (unsigned *)malloc(alloc * sizeof(unsigned)); + assert(data != NULL); + memcpy(data, _data, sizeof(_data)); + } + else + { + data = (unsigned *)realloc(data, alloc * sizeof(unsigned)); + assert(data != NULL); + } + } + } + + ~BigInt() + { + if (data != _data) + { + free(data); + } + } + + void swap(BigInt &y) + { + if (data != _data && y.data != y._data) + { + std::swap(size, y.size); + std::swap(alloc, y.alloc); + std::swap(data, y.data); + } + else + { + BigInt t(*this); + assign(y); + y.assign(t); + } + } + +private: + typedef unsigned long long uint64; + static inline int sgn(int n) + { + return n == 0 ? 0 : (n < 0 ? -1 : 1); + } + + // Removes leading zeroes + void normalize() + { + int n = abs(size); + while (n > 0 && data[n - 1] == 0) + { + n--; + } + size = (size < 0 ? -n : n); + } + + static int absCmp(const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn != yn) + { + return sgn(xn - yn); + } + for (int i = xn - 1; i >= 0; i--) + if (x.data[i] != y.data[i]) + { + return x.data[i] > y.data[i] ? +1 : -1; + } + return 0; + } + + // z = abs(x) + abs(y); + static void absAdd(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn < yn) + { + absAdd(z, y, x); + return; + } + int zn = max(xn, yn); + z.reserve(zn + 1); + uint64 c = 0; + for (int i = 0; i < yn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i] + (uint64)y.data[i]) & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 0 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[zn++] = (unsigned)c; + } + z.size = zn; + } + + // z = abs(x) + abs(y) + static void absAdd1(BigInt &z, const BigInt &x, unsigned y) + { + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = y; + if (&z == &x) + { + for (int i = 0; c != 0 && i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = 0; i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = n; + } + + // z = abs(x) - abs(y) + static void absSub(BigInt &z, const BigInt &x, const BigInt &y) + { + int t = absCmp(x, y); + if (t <= 0) + { + if (t == 0) + { + z.size = 0; + } + else + { + absSub(z, y, x); + } + z.size = -z.size; + return; + } + int xn = abs(x.size), yn = abs(y.size); + z.reserve(max(xn, yn)); + uint64 c = 1; + for (int i = 0; i < yn; i++, c >>= 32) + { + c += (uint64)x.data[i] + ((uint64)y.data[i] ^ 0xFFFFFFFFULL); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + assert(c == 1); + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + assert(z.size > 0); + } + + // z = abs(x) - abs(y) + static void absSub1(BigInt &z, const BigInt &x, unsigned y) + { + if (y == 0) + { + z.assign(x); + z.size = abs(z.size); + return; + } + if (x.size == 0) + { + z.size = -1; + z.data[0] = y; + return; + } + int xn = abs(x.size); + if (xn == 1) + { + if (x.data[0] > y) + { + z.size = 1; + z.data[0] = x.data[0] - y; + } + else if (x.data[0] == y) + { + z.size = 0; + } + else + { + z.size = -1; + z.data[0] = y - x.data[0]; + } + return; + } + z.reserve(xn); + uint64 c = 1 + (uint64)x.data[0] + (y ^ 0xFFFFFFFFULL); + z.data[0] = (unsigned)c; + c >>= 32; + if (&z == &x) + { + for (int i = 1; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + else + { + for (int i = 1; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + } + + // z = abs(x) * m + a + static void absMulAdd1(BigInt &z, const BigInt &x, unsigned m, unsigned a) + { + int n = abs(x.size); + z.reserve(n + 2); + uint64 c = a; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)m); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + while (c != 0) + { + z.data[n++] = (unsigned)c; + c >>= 32; + } + z.size = n; + } + + // z = x + sign*y. Asserts: abs(sign) = 1 + static void add(BigInt &z, const BigInt &x, int sign, const BigInt &y) + { + int xs = sgn(x.size), ys = sign * sgn(y.size); + if (xs == 0) + { + z.assign(y); + z.size *= sign; + } + else if (ys == 0) + { + z.assign(x); + } + else if (xs == ys) + { + absAdd(z, x, y); + z.size *= xs; + } + else if (ys < 0) + { + absSub(z, x, y); + } + else + { + absSub(z, x, y); + z.size = -z.size; + } + } + + static void add1s(BigInt &z, const BigInt &x, int y) + { + int xs = (x.size >= 0 ? +1 : -1), ys = (y >= 0 ? +1 : -1); + if (xs == ys) + { + absAdd1(z, x, abs(y)); + z.size *= xs; + } + else if (ys < 0) + { + absSub1(z, x, -y); + } + else + { + absSub1(z, x, y); + z.size = -z.size; + } + } + + static void mul1s(BigInt &z, const BigInt &x, int y) + { + if (y < 0) + { + mul1s(z, x, -y); + z.size = -z.size; + return; + } + if (y == 0) + { + z.size = 0; + return; + } + if (y == 1) + { + z.assign(x); + return; + } + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = 0; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)y); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = (x.size < 0 ? -n : n); + } + + static void mulQuadratic(BigInt &z, const BigInt &x, const BigInt &y) + { + if (&z == &x || &z == &y) + { + BigInt t; + mulQuadratic(t, x, y); + z = t; + return; + } + int xn = abs(x.size), yn = abs(y.size), zn = xn + yn + 1; + z.reserve(zn); + for (int i = 0; i < zn; i++) + { + z.data[i] = 0; + } + for (int i = 0; i < xn; i++) + { + uint64 c = 0; + int k = i; + for (int j = 0; j < yn; j++, k++, c >>= 32) + { + c += z.data[k] + x.data[i] * (uint64)y.data[j]; + z.data[k] = (unsigned)(c & 0xFFFFFFFFU); + } + for (; c != 0; k++, c >>= 32) + { + z.data[k] = (unsigned)((c += z.data[k]) & 0xFFFFFFFFU); + } + } + z.size = zn * sgn(x.size) * sgn(y.size); + z.normalize(); + } + + static void mulKaratsuba(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size), zs = sgn(x.size) * sgn(y.size); + int w = max(xn, yn) >> 1; + BigInt A(x.data + w, max(0, xn - w)), B(x.data, min(xn, w)); + BigInt C(y.data + w, max(0, yn - w)), D(y.data, min(yn, w)); + BigInt R, T; + absAdd(z, A, B); + absAdd(T, C, D); + mul(R, z, T); + mul(z, A, C); + mul(T, B, D); + R -= z; + R -= T; + R <<= w * 32; + z <<= w * 64; + z += R; + z += T; + z.size *= zs; + } + + BigInt(unsigned a[], int n) + { + if (n < 0) + { + n = 0; + } + while (n > 0 && a[n - 1] == 0) + { + n--; + } + data = NULL; + reserve(n); + size = n; + memcpy(data, a, n * sizeof(unsigned)); + } + + // q = abs(x) div abs(y); Returns abs(x) mod abs(y) + static unsigned absDiv1(BigInt &q, const BigInt &x, unsigned y) + { + assert(y != 0); + int n = abs(x.size); + q.reserve(n); + uint64 c = 0; + for (int i = n - 1; i >= 0; i--) + { + c = (c << 32) | (uint64)x.data[i]; + q.data[i] = (unsigned)(c / y); + c %= y; + } + q.size = n; + q.normalize(); + return (unsigned)c; + } + + // u = abs(u) mod abs(v), q = abs(u) div abs(v) + static void absDiv(BigInt *q, BigInt &u, BigInt v) + { + if (q != NULL && q == &u) + { + BigInt t; + absDiv(&t, u, v); + *q = t; + return; + } + u.size = abs(u.size); + v.size = abs(v.size); + assert(v.size > 0); + if (u.size < v.size) + { + if (q) + { + *q = 0; + } + return; + } + if (v.size == 1) + { + unsigned t = absDiv1(q == NULL ? u : *q, u, v.data[0]); + u.data[0] = t; + u.size = (t == 0 ? 0 : 1); + return; + } + int n = v.size, d = 0; + while (((v.data[n - 1] << d) & 0x80000000U) == 0) + { + d++; + } + u <<= d; + v <<= d; + unsigned vh = v.data[n - 1], vh2 = v.data[n - 2]; + uint64 c, g; + u.reserve(u.size + 1); + u.data[u.size] = 0; + v.reserve(v.size + 1); + v.data[v.size] = 0; + int qp = u.size - v.size + 1; + if (q) + { + q->reserve(qp + 1); + q->size = qp; + } + for (int up = u.size - 1; --qp >= 0; up--) + { + c = (((uint64)u.data[up + 1]) << 32) | (uint64)u.data[up]; + g = c / (uint64)vh; + if (g > 0xFFFFFFFFULL) + { + g = 0xFFFFFFFFULL; + } + while ((c - g * vh) < 0x100000000ULL && + (((c - g * vh) << 32) + u.data[up - 1]) < (g * (uint64)vh2)) + { + g--; + } + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++) + { + c += g * (uint64)v.data[j]; + if (u.data[i] >= (unsigned)(c & 0xFFFFFFFFULL)) + { + u.data[i] -= (unsigned)(c & 0xFFFFFFFFULL); + c >>= 32; + } + else + { + u.data[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL)); + c = (c >> 32) + 1; + } + } + if (c != 0) + { + g--; + assert(c == 1); + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++, c >>= 32) + { + c += (uint64)u.data[i] + (uint64)v.data[j]; + u.data[i] = (unsigned)c; + } + assert(c == 1); + } + if (q) + { + q->data[qp] = (unsigned)g; + } + } + u >>= d; + v >>= d; + if (q) + { + q->normalize(); + } + } + +public: + static int cmp(const BigInt &x, const BigInt &y) + { + if (x.size != y.size) + { + return sgn(x.size - y.size); + } + return absCmp(x, y) * (x.size < 0 ? -1 : 1); + } + + static int cmp1s(const BigInt &x, int y) + { + if (y == 0) + { + return sgn(x.size); + } + if (y > 0) + { + if (x.size <= 0) + { + return -1; + } + if (x.size > 1) + { + return +1; + } + if (x.data[0] == (unsigned)y) + { + return 0; + } + return x.data[0] > (unsigned)y ? +1 : -1; + } + else + { + if (x.size >= 0) + { + return +1; + } + if (x.size < -1) + { + return -1; + } + if (x.data[0] == (unsigned)(-y)) + { + return 0; + } + return x.data[0] > (unsigned)(-y) ? -1 : +1; + } + } + + static void add(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, +1, y); + } + + static void sub(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, -1, y); + } + + static void mul(BigInt &z, const BigInt &x, const BigInt &y) + { + if (max(abs(x.size), abs(y.size)) < 64) + { + mulQuadratic(z, x, y); + } + else + { + mulKaratsuba(z, x, y); + } + } + + static void mod(BigInt &r, const BigInt &u, const BigInt &v) + { + r = u; + absDiv(NULL, r, v); + } + + static void div(BigInt &q, BigInt &r, const BigInt &u, const BigInt &v) + { + int us = sgn(u.size), vs = sgn(v.size); + r = u; + absDiv(&q, r, v); + //TODO + if (us * vs < 0) + { + q.size = -q.size; + } + } + + void assign(int n) + { + reserve(1); + size = sgn(n); + data[0] = abs(n); + } + + void assign(long long n) + { + reserve(2); + if (n < 0) + { + size = -2; + n = -n; + } + else + { + size = 2; + } + data[0] = (unsigned)(n & 0xFFFFFFFFU); + data[1] = (unsigned)(n >> 32); + normalize(); + } + + void assign(const BigInt &b) + { + if (this == &b) + { + return; + } + int n = abs(b.size); + reserve(n); + size = b.size; + memcpy(data, b.data, n * sizeof(unsigned)); + } + + void assign(const char *s, int radix = 10) + { + assert(2 <= radix && radix <= 36); + while (isspace(*s)) + { + s++; + } + int sign = 1; + if (*s == '+') + { + s++; + } + else if (*s == '-') + { + s++; + sign = -1; + } + while (*s == '0') + { + s++; + } + int n = 0; + for (n = 0; s[n] && isalnum(s[n]); n++) + ; + size = 0; + if (n > 20) + { + reserve((int)(log((double)radix) / log(2.0) / 32.0 * n) + 2); + } + else + { + reserve(n / 9 + 2); + } + unsigned a = 0, m = 1; + for (int i = 0; i < n; i++) + { + int dig = (isdigit(s[i]) ? (s[i] - '0') : (toupper(s[i]) - 'A' + 10)); + assert(dig < radix); + a = a * radix + dig; + m *= radix; + if (m > 0x3000000) + { + absMulAdd1(*this, *this, m, a); + a = 0; + m = 1; + } + } + if (m > 1) + { + absMulAdd1(*this, *this, m, a); + } + size *= sign; + } + + BigInt(int n = 0) + { + data = NULL; + assign(n); + } + BigInt(int n, int cap) + { + data = NULL; + reserve(cap); + assign(n); + } + BigInt(long long n) + { + data = NULL; + assign(n); + } + BigInt(const BigInt &b) + { + data = NULL; + assign(b); + } + BigInt(const char *s, int radix = 10) + { + data = NULL; + assign(s, radix); + } + BigInt(const string &s, int radix = 10) + { + data = NULL; + assign(s.c_str(), radix); + } + + BigInt &operator=(int n) + { + assign(n); + return *this; + } + BigInt &operator=(long long n) + { + assign(n); + return *this; + } + BigInt &operator=(const BigInt &b) + { + assign(b); + return *this; + } + BigInt &operator=(const char *s) + { + assign(s); + return *this; + } + BigInt &operator=(const string &s) + { + assign(s.c_str()); + return *this; + } + BigInt &operator+=(const BigInt &y) + { + add(*this, *this, +1, y); + return *this; + } + BigInt &operator-=(const BigInt &y) + { + add(*this, *this, -1, y); + return *this; + } + BigInt &operator*=(const BigInt &y) + { + mul(*this, *this, y); + return *this; + } + BigInt &operator/=(const BigInt &y) + { + BigInt q, r; + div(q, r, *this, y); + assign(q); + return *this; + } + BigInt &operator%=(const BigInt &y) + { + mod(*this, *this, y); + return *this; + } + BigInt &operator+=(int y) + { + add1s(*this, *this, y); + return *this; + } + BigInt &operator-=(int y) + { + add1s(*this, *this, -y); + return *this; + } + BigInt &operator*=(int y) + { + mul1s(*this, *this, y); + return *this; + } + BigInt &operator<<=(int n) + { + shl(n); + return *this; + } + BigInt &operator>>=(int n) + { + shr(n); + return *this; + } + void operator++() + { + add1s(*this, *this, 1); + } + void operator--() + { + add1s(*this, *this, -1); + } + BigInt operator-() const + { + BigInt z = *this; + z.negate(); + return z; + } + BigInt operator>>(int n) const + { + BigInt r = *this; + r.shr(n); + return r; + } + BigInt operator<<(int n) const + { + BigInt r = *this; + r.shl(n); + return r; + } + + string str(int radix = 10) const + { + assert(2 <= radix && radix <= 36); + if (size == 0) + { + return "0"; + } + int y = 1, m = 0; + while (y < 0x300000) + { + y *= radix; + m++; + } + BigInt x(*this); + x.size = abs(x.size); + char *tmp = (char *)malloc(x.size * (radix >= 10 ? 10 : 32) + 10); + int n = 0; + while (x.size != 0) + { + unsigned r = absDiv1(x, x, y); + for (int i = 0; i < m; i++, r /= radix) + { + tmp[n++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[r % radix]; + } + } + while (n > 0 && tmp[n - 1] == '0') + { + n--; + } + if (size < 0) + { + tmp[n++] = '-'; + } + reverse(tmp, tmp + n); + tmp[n] = 0; + string res = string(tmp); + free(tmp); + return res; + } + + string toString(int r = 10) const + { + return str(r); + } + int toInt() const + { + return sgn(size) * (int)data[0]; + } + long long toLL() const + { + long long r = 0; + for (int i = 0; i < 2 && i < abs(size); i++) + { + r = (r << 32) | data[i]; + } + return size < 0 ? -r : r; + } + + void setBit(int n) + { + int s = abs(size), m = (n >> 5) + 1; + reserve(m); + while (s < m) + { + data[s++] = 0; + } + size = (size < 0 ? -s : s); + data[n >> 5] |= 1U << (n & 31); + } + + void clrBit(int n) + { + if (abs(size) > (n >> 5)) + { + data[n >> 5] &= ~(1U << (n & 31)); + normalize(); + } + } + int getBit(int n) const + { + return (abs(size) > (n >> 5)) ? ((data[n >> 5] >> (n & 31)) & 1) : 0; + } + + // Returns 1+(index of highest non-zero bit) + int bitSize() const + { + if (size == 0) + { + return 0; + } + int n = (abs(size) - 1) * 32; + unsigned t = data[abs(size) - 1]; + if (t >= 65536) + { + n += 16; + t >>= 16; + } + if (t >= 256) + { + n += 8; + t >>= 8; + } + if (t >= 16) + { + n += 4; + t >>= 4; + } + if (t >= 4) + { + n += 2; + t >>= 2; + } + if (t >= 2) + { + n++; + t >>= 1; + } + if (t >= 1) + { + n++; + } + return n; + } + + void shl(int s) + { + if (size == 0) + { + return; + } + if (s < 0) + { + shr(-s); + } + int n = abs(size), w = s >> 5; + reserve(n + w + 1); + if (w > 0) + { + for (int i = n - 1; i >= 0; i--) + { + data[i + w] = data[i]; + } + for (int i = w - 1; i >= 0; i--) + { + data[i] = 0; + } + n += w; + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + data[n++] = 0; + for (int i = 0; i < n; i++) + { + b = data[i] >> (32 - s); + data[i] = (data[i] << s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void shr(int s) + { + if (s < 0) + { + shl(-s); + } + int n = abs(size), w = s >> 5; + if (w > 0) + { + for (int i = 0; i + w < n; i++) + { + data[i] = data[i + w]; + } + n -= w; + if (n < 0) + { + n = 0; + } + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + for (int i = n - 1; i >= 0; i--) + { + b = data[i] << (32 - s); + data[i] = (data[i] >> s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void negate() + { + size = -size; + } + int sign() const + { + return sgn(size); + } + int compareTo(const BigInt &y) const + { + return cmp(*this, y); + } + int compareTo(int y) const + { + return cmp1s(*this, y); + } +}; + +BigInt operator+(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::add(z, x, y); + return z; +} +BigInt operator-(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::sub(z, x, y); + return z; +} +BigInt operator*(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::mul(z, x, y); + return z; +} +BigInt operator/(const BigInt &x, const BigInt &y) +{ + BigInt q, r; + BigInt::div(q, r, x, y); + return q; +} +BigInt operator%(const BigInt &x, const BigInt &y) +{ + BigInt r; + BigInt::mod(r, x, y); + return r; +} +bool operator==(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) == 0; +} +bool operator==(const BigInt &x, int y) +{ + return BigInt::cmp1s(x, y) == 0; +} +bool operator!=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) != 0; +} +bool operator<(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) < 0; +} +bool operator<=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) <= 0; +} +bool operator>(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) > 0; +} +bool operator>=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) >= 0; +} +ostream &operator<<(ostream &o, const BigInt &x) +{ + return o << x.str(); +} +istream &operator>>(istream &i, BigInt &x) +{ + string s; + i >> s; + x = s; + return i; +} +namespace std +{ +template <> +inline void swap(BigInt &a, BigInt &b) +{ + a.swap(b); +} +} + +template +struct Rational +{ + typedef Rational R; + T p, q; + + Rational(T p = 0, T q = 1) : p(p), q(q) + { + reduce(); + } + + void reduce() + { + if (q < 0) + { + p = -p; + q = -q; + } + T g = __gcd(p < 0 ? -p : p, q); + p /= g; + q /= g; + } + + R operator+(const R &b) const + { + return R(p * b.q + q * b.p, q * b.q); + } + R operator-(const R &b) const + { + return R(p * b.q - q * b.p, q * b.q); + } + R operator*(const R &b) const + { + return R(p * b.p, q * b.q); + } + R operator/(const R &b) const + { + return R(p * b.q, q * b.p); + } + R operator-() const + { + return R(-p, q); + } + + bool operator<(const R &b) const + { + return p * b.q < q * b.p; + } + bool operator<=(const R &b) const + { + return p * b.q <= q * b.p; + } + bool operator==(const R &b) const + { + return p == b.p && q == b.q; + } + bool operator!=(const R &b) const + { + return p != b.p || q != b.q; + } + + string str() + { + ostringstream o; + o << p << "/" << q; + return o.str(); + } +}; + +Rational f(long long n) +{ + typedef Rational R; + R part1(0, 1); + if (n <= 6) + { + for (long long k = 1; k <= n; k++) + { + part1 = part1 + R(1, k); + part1 = part1 - R(1, k + 3); + } + } + else + { + for (long long k = 1; k <= 3; k++) + { + part1 = part1 + R(1, k); + } + for (long long k = n + 1; k <= n + 3; k++) + { + part1 = part1 - R(1, k); + } + } + part1 = part1 / R(6, 1); + R part2 = (R(1, n + 2) - R(1, 2)) / R(2, 1); + return part1 + part2; +} + +int main() +{ + long long n, m; + for (int t = 1; scanf("%lld%lld", &n, &m) == 2; t++) + { + if (n == 0 && m == 0) + { + break; + } + Rational res = f(n) * f(m); + string p = res.p.toString(); + string q = res.q.toString(); + printf("Case %d:\n%s\n", t,p.c_str()); + for (int i = 0; i < p.size() || i < q.size(); i++) + { + printf("-"); + } + printf("\n%s\n\n", q.c_str()); + } +} diff --git a/11251.cpp b/11251.cpp new file mode 100644 index 0000000..6cfe264 --- /dev/null +++ b/11251.cpp @@ -0,0 +1,461 @@ +#include + +using namespace std; + +const char the_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +int base, denom, used, P[50], Q[50], PN, QN, first_digits; +string result; + +void soln_fmt() +{ + static char buf[100]; + sprintf(buf, "%d %d ", base, denom); + result = buf; + for (int i = PN - 1; i >= 0; i--) + { + result += the_digits[P[i]]; + } + result += "/"; + for (int i = QN - 1; i >= 0; i--) + { + result += the_digits[Q[i]]; + } +} + +int rec(int n, int carry) +{ + if ((first_digits & ~used) == 0 && n < PN) + { + return 0; + } + if (n == PN) + { + if (QN > PN) + { + if (used & (1 << carry)) + { + return 0; + } + Q[n] = carry; + } + else if (carry != 0) + { + return 0; + } + soln_fmt(); + return 1; + } + static int seq[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 1, 0}; + for (int i = 0; seq[i] != 0; i++) + { + int digit = seq[i]; + if (digit >= base) + { + continue; + } + if (used & (1 << digit)) + { + continue; + } + int t = digit * denom + carry; + P[n] = digit; + Q[n] = t % base; + t /= base; + if ((used & (1 << Q[n])) != 0 || Q[n] == P[n]) + { + continue; + } + used |= 1 << digit; + used |= 1 << Q[n]; + if (rec(n + 1, t)) + { + return 1; + } + used &= ~((1 << digit) | (1 << Q[n])); + } + return 0; +} + +string solve(int b, int den) +{ + // assert(2 <= b && b <= 27); + // assert(2 <= den && den <= b - 1); + base = b; + denom = den; + used = 1 << 0; + PN = (base - 1) / 2; + QN = base / 2; + // assert(PN + QN == base - 1); + result = ""; + first_digits = 0; + for (int d = 1; d <= base - 1; d++) + { + if (PN == QN) + { + if (d * denom >= base) + { + continue; + } + } + else + { + if (d == 1) + { + continue; + } + if (d * denom + base - 1 < base) + { + continue; + } + } + first_digits |= 1 << d; + } + if (rec(0, 0)) + { + return result; + } + return ""; +} + +int main() +{ + int b = 27, n = 26; + // for (;;) { + // if (b < 2) { b = 2; continue; } + // if (b > 27) break; + // if (n < 2) { n = 2; continue; } + // if (n >= b) { b++; n = 2; continue; } + // if (n % 2 != 1 && n != 26) { n++; continue; } + + // printf("base=%d n=%d\n", b, n); + // string s = solve(b, n); + // printf("[%s]\n", s.c_str()); + + // n++; + // } + +#if 1 + string cache[32][32]; + cache[3][2] = "3 2 1/2"; + cache[4][2] = "4 2 3/12"; + cache[4][3] = "4 3 3/21"; + cache[5][2] = "NO SOLUTION"; + cache[5][3] = ""; + cache[5][4] = ""; + cache[6][2] = ""; + cache[6][3] = ""; + cache[6][4] = "6 4 51/324"; + cache[6][5] = "6 5 41/325"; + cache[7][2] = "7 2 162/354"; + cache[7][3] = ""; + cache[7][4] = "7 4 126/543"; + cache[7][5] = ""; + cache[7][6] = ""; + cache[8][2] = "8 2 572/1364"; + cache[8][3] = "8 3 671/2453"; + cache[8][4] = "8 4 563/2714"; + cache[8][5] = "8 5 671/4235"; + cache[8][6] = "8 6 521/3746"; + cache[8][7] = "8 7 421/3567"; + cache[9][2] = "9 2 1658/3427"; + cache[9][3] = "9 3 2751/8463"; + cache[9][4] = "9 4 1542/6378"; + cache[9][5] = "9 5 1625/8437"; + cache[9][6] = "9 6 1425/8763"; + cache[9][7] = ""; + cache[9][8] = ""; + cache[10][2] = "10 2 7932/15864"; + cache[10][3] = "10 3 5832/17496"; + cache[10][4] = "10 4 3942/15768"; + cache[10][5] = "10 5 9723/48615"; + cache[10][6] = "10 6 2943/17658"; + cache[10][7] = "10 7 7614/53298"; + cache[10][8] = "10 8 9321/74568"; + cache[10][9] = "10 9 8361/75249"; + cache[11][2] = "11 2 48751/963A2"; + cache[11][3] = ""; + cache[11][4] = "11 4 27651/A8394"; + cache[11][5] = ""; + cache[11][6] = "11 6 15A63/92847"; + cache[11][7] = ""; + cache[11][8] = ""; + cache[11][9] = ""; + cache[11][10] = ""; + cache[12][2] = "12 2 98B32/175A64"; + cache[12][3] = "12 3 A56B1/274893"; + cache[12][4] = "12 4 6AB51/237984"; + cache[12][5] = "12 5 96421/3B78A5"; + cache[12][6] = "12 6 B8923/5A4716"; + cache[12][7] = "12 7 8AB31/524697"; + cache[12][8] = "12 8 9AB21/673548"; + cache[12][9] = "12 9 AB471/826539"; + cache[12][10] = "12 10 B4321/95678A"; + cache[12][11] = "12 11 74821/6935AB"; + cache[13][2] = "13 2 3B6541/79CA82"; + cache[13][3] = ""; + cache[13][4] = "13 4 28B531/A967C4"; + cache[13][5] = "13 5 243C71/B86A95"; + cache[13][6] = "13 6 19B6C3/A74285"; + cache[13][7] = ""; + cache[13][8] = "13 8 15C462/B879A3"; + cache[13][9] = "13 9 13C485/B972A6"; + cache[13][10] = "13 10 134B56/C79A28"; + cache[13][11] = ""; + cache[13][12] = ""; + cache[14][2] = ""; + cache[14][3] = "14 3 B79354/268DA1C"; + cache[14][4] = "14 4 CD5621/39B7A84"; + cache[14][5] = "14 5 9D6821/37B4CA5"; + cache[14][6] = "14 6 A93B51/47D8C26"; + cache[14][7] = ""; + cache[14][8] = "14 8 4CD631/2B597A8"; + cache[14][9] = "14 9 CB7621/835AD49"; + cache[14][10] = "14 10 C47D21/8B3956A"; + cache[14][11] = "14 11 C2D561/98473AB"; + cache[14][12] = "14 12 D54321/B6789AC"; + cache[14][13] = "14 13 739421/6A58BCD"; + cache[15][2] = "15 2 4DBA731/9C85E62"; + cache[15][3] = ""; + cache[15][4] = "15 4 2DC8761/BA53E94"; + cache[15][5] = ""; + cache[15][6] = "15 6 258BC41/E37AD96"; + cache[15][7] = ""; + cache[15][8] = "15 8 18BD763/CA52E49"; + cache[15][9] = ""; + cache[15][10] = "15 10 13AE6D2/C7498B5"; + cache[15][11] = ""; + cache[15][12] = "15 12 1248A73/DC9E5B6"; + cache[15][13] = ""; + cache[15][14] = ""; + cache[16][2] = "16 2 E9C7B52/1D38F6A4"; + cache[16][3] = "16 3 DE78C51/29B6A4F3"; + cache[16][4] = "16 4 FAD6721/3EB59C84"; + cache[16][5] = "16 5 B8F7621/39CD4EA5"; + cache[16][6] = "16 6 9EA5421/3B7DF8C6"; + cache[16][7] = "16 7 AF68321/4CBD95E7"; + cache[16][8] = "16 8 FCD4B23/7E6A5918"; + cache[16][9] = "16 9 DE28431/7CF6A5B9"; + cache[16][10] = "16 10 D7EBC21/86F3594A"; + cache[16][11] = "16 11 79E8421/53CFAD6B"; + cache[16][12] = "16 12 E9B3521/AF467D8C"; + cache[16][13] = "16 13 5EB9321/4CF678AD"; + cache[16][14] = "16 14 F654321/D789ABCE"; + cache[16][15] = "16 15 B7D3821/AC5649EF"; + cache[17][2] = "17 2 4F3E8651/9D7BGCA2"; + cache[17][3] = "17 3 59ADE421/GBF78C63"; + cache[17][4] = "17 4 2B9G8361/AC5EFD74"; + cache[17][5] = "17 5 368C7D21/GF9B4EA5"; + cache[17][6] = "17 6 23CGA841/D59EBF76"; + cache[17][7] = "17 7 23DGA841/F9CE56B7"; + cache[17][8] = "17 8 15D9F832/AC6B4E7G"; + cache[17][9] = "17 9 196B2753/DG8F4ECA"; + cache[17][10] = "17 10 19D5G642/FCE8AB73"; + cache[17][11] = "17 11 168EA942/F3C7DGB5"; + cache[17][12] = "17 12 16AC8D42/GB9E35F7"; + cache[17][13] = "17 13 1478EB52/G6CD3AF9"; + cache[17][14] = "17 14 13968472/GFC5DAEB"; + cache[17][15] = ""; + cache[17][16] = ""; + cache[18][2] = ""; + cache[18][3] = "18 3 GBE98541/2DH7A6FC3"; + cache[18][4] = "18 4 D6EG8731/2H95BFAC4"; + cache[18][5] = "18 5 DHC69821/3FG7EB4A5"; + cache[18][6] = "18 6 BGE98451/3HAF2D7C6"; + cache[18][7] = "18 7 HDBC8421/6G59F3AE7"; + cache[18][8] = "18 8 HA9BF321/7EC54D6G8"; + cache[18][9] = ""; + cache[18][10] = "18 10 G85FE631/92B4DH7CA"; + cache[18][11] = "18 11 E3HFA621/8C7G95D4B"; + cache[18][12] = "18 12 HFAE5421/BG7398D6C"; + cache[18][13] = "18 13 AG693421/7FECB5H8D"; + cache[18][14] = "18 14 BG5DH321/94C8F67AE"; + cache[18][15] = "18 15 7HD9G321/6BE548ACF"; + cache[18][16] = "18 16 H7654321/F89ABCDEG"; + cache[18][17] = "18 17 95C7B421/8E6D3AFGH"; + cache[19][2] = "19 2 5HFED9431/BGCA7I862"; + cache[19][3] = ""; + cache[19][4] = "19 4 3AFIDC721/E56HGB984"; + cache[19][5] = ""; + cache[19][6] = "19 6 28BAF9431/EDC7GH5I6"; + cache[19][7] = ""; + cache[19][8] = "19 8 24CFB6931/HI7AEDG58"; + cache[19][9] = ""; + cache[19][10] = "19 10 1G96E2753/ICHA84FDB"; + cache[19][11] = ""; + cache[19][12] = "19 12 14BD8F632/EH79ACGI5"; + cache[19][13] = ""; + cache[19][14] = "19 14 14F7IBE32/HA6GDC859"; + cache[19][15] = ""; + cache[19][16] = "19 16 138B6G792/IH4AEF5CD"; + cache[19][17] = ""; + cache[19][18] = ""; + cache[20][2] = "20 2 IFDG9E532/1HB7CJ8A64"; + cache[20][3] = "20 3 FEGCA8D61/2749HB5JI3"; + cache[20][4] = "20 4 IHDC67J21/3FAE95BG84"; + cache[20][5] = "20 5 DIHCJ6721/39E84GBFA5"; + cache[20][6] = "20 6 D4FG9B521/3J8EIH7AC6"; + cache[20][7] = "20 7 JBIDFC421/6H3AG958E7"; + cache[20][8] = "20 8 HIFDJ6421/73A5BE9CG8"; + cache[20][9] = "20 9 AGJ5E6321/4HCDB8F7I9"; + cache[20][10] = "20 10 FIGDE9523/7J86H4CB1A"; + cache[20][11] = "20 11 I8FE67431/A2GCH9J5DB"; + cache[20][12] = "20 12 8JAGB6321/57E9IFDH4C"; + cache[20][13] = "20 13 5HBJ4A721/3G8F9IEC6D"; + cache[20][14] = "20 14 9FBJA5421/6GI7D3CH8E"; + cache[20][15] = "20 15 GJ89D5321/CEB74IH6AF"; + cache[20][16] = "20 16 J45IBD321/F78EH6A9CG"; + cache[20][17] = "20 17 7GJ9F4321/6D8B5IACEH"; + cache[20][18] = "20 18 J87654321/H9ABCDEFGI"; + cache[20][19] = "20 19 A3C6B7521/9D8E4FHGIJ"; + cache[21][2] = "21 2 9GI5HA7431/JCFBDKE862"; + cache[21][3] = ""; + cache[21][4] = "21 4 3D7JIH9521/EBAGC6FK84"; + cache[21][5] = "21 5 3BE7J9F421/HG8ID6CKA5"; + cache[21][6] = "21 6 38IEGDA521/KB74FHJ9C6"; + cache[21][7] = ""; + cache[21][8] = "21 8 25EGKCA641/I3D9HFJ7B8"; + cache[21][9] = "21 9 25B8DHF431/K7IEJCAG69"; + cache[21][10] = "21 10 1DHI4FB632/GCAE587J9K"; + cache[21][11] = ""; + cache[21][12] = "21 12 1CGF9I8542/J6BHDAEK73"; + cache[21][13] = "21 13 1C8IGF6432/KEAD79HBJ5"; + cache[21][14] = "21 14 13JKHB8542/GD6IECA9F7"; + cache[21][15] = ""; + cache[21][16] = "21 16 14AHCKF632/J958IGDE7B"; + cache[21][17] = "21 17 14CI9E5732/KF8JHB6GAD"; + cache[21][18] = "21 18 137H8B5C42/KIEJ6DG9AF"; + cache[21][19] = ""; + cache[21][20] = ""; + cache[22][2] = "22 2 KGDHAF9732/1JB5CL8IE64"; + cache[22][3] = "22 3 GIDHAE7541/26BJ89KLFC3"; + cache[22][4] = "22 4 KJ9FDE5721/3HBGIACL684"; + cache[22][5] = "22 5 LGJBC76321/4KI9DHE8FA5"; + cache[22][6] = "22 6 HADE975321/4GJFKBL8IC6"; + cache[22][7] = "22 7 K8BI4C5321/6AFGH9JDLE7"; + cache[22][8] = "22 8 HL3KI75421/6BF9CEDJAG8"; + cache[22][9] = "22 9 GEBC4D8321/6HKFLJA75I9"; + cache[22][10] = "22 10 F7GH5E4321/6LBDIC9J8KA"; + cache[22][11] = "22 11 LIHGF69523/AK8J7E4DC1B"; + cache[22][12] = "22 12 KLFH2G6431/B9ID7AJ85EC"; + cache[22][13] = "22 13 LJAFE68321/CKB759GHI4D"; + cache[22][14] = "22 14 HFAJ8C4321/B5IK79GDL6E"; + cache[22][15] = "22 15 EJCIL95421/A37GKD6BH8F"; + cache[22][16] = "22 16 BLJIH67321/8FK9ECD45AG"; + cache[22][17] = "22 17 JIAK4G5321/F769DEBL8CH"; + cache[22][18] = "22 18 F9G5JA4321/CDL6HK78BEI"; + cache[22][19] = "22 19 FL7K654321/DH9IB8ACEGJ"; + cache[22][20] = "22 20 L987654321/JABCDEFGHIK"; + cache[22][21] = "22 21 D7I5BF8421/CGA963EHJKL"; + cache[23][2] = "23 2 9LHGI7EB431/JKCADF5M862"; + cache[23][3] = ""; + cache[23][4] = "23 4 5FKGA97B321/MHDJIE6LC84"; + cache[23][5] = ""; + cache[23][6] = "23 6 3DAHGJKF521/LBIE948M7C6"; + cache[23][7] = ""; + cache[23][8] = "23 8 2ADGLE76541/JFHKBMC3I98"; + cache[23][9] = ""; + cache[23][10] = "23 10 25CLK8D6431/M9EBJGHFI7A"; + cache[23][11] = ""; + cache[23][12] = "23 12 1KIEB8A2753/MJGCL964HFD"; + cache[23][13] = ""; + cache[23][14] = "23 14 1CFJ8M76432/LGEIAD9HBK5"; + cache[23][15] = ""; + cache[23][16] = "23 16 13LMACB8542/IH6E7FKGDJ9"; + cache[23][17] = ""; + cache[23][18] = "23 18 14MEAC8H632/LKG75FJBI9D"; + cache[23][19] = ""; + cache[23][20] = "23 20 135D9BLGE42/MIJF68KA7CH"; + cache[23][21] = ""; + cache[23][22] = ""; + cache[24][2] = "24 2 MKFDJBG9652/1LH73EN8ICA4"; + cache[24][3] = "24 3 IHLFKEB7A41/285GNDJ9M6C3"; + cache[24][4] = "24 4 KMFDGNA7921/3BIE6JLH5C84"; + cache[24][5] = "24 5 MCDN6I87321/4GELK9JHBFA5"; + cache[24][6] = "24 6 MLFKGDAB721/5H9N438EJIC6"; + cache[24][7] = "24 7 NC4MJG85321/6KDAFHI9BLE7"; + cache[24][8] = "24 8 AMHLIEBC751/3FDN64JK29G8"; + cache[24][9] = "24 9 LHFBKD65421/83EJAGN7MCI9"; + cache[24][10] = "24 10 DM8GCNB4321/5J7EL9IFH6KA"; + cache[24][11] = "24 11 EKIFNA54321/6JCD7HG8L9MB"; + cache[24][12] = "24 12 JMKFIDB8543/9NA7L6HG2E1C"; + cache[24][13] = "24 13 HCLAIG76431/9BNEK2JM85FD"; + cache[24][14] = "24 14 MHGLB985321/D67KCFAINJ4E"; + cache[24][15] = "24 15 KAHE5974321/CIGNL8JBDM6F"; + cache[24][16] = "24 16 KFJ7MB95421/DICL6NE3AH8G"; + cache[24][17] = "24 17 CG7LKM65321/8NDEBJI9F4AH"; + cache[24][18] = "24 18 N4BEAD65321/H98GJLMFK7CI"; + cache[24][19] = "24 19 M7DGLI84321/HFNK95CB6AEJ"; + cache[24][20] = "24 20 F6ME9B54321/CHIJNL87ADGK"; + cache[24][21] = "24 21 8AMK6B54321/79DNHFJCEGIL"; + cache[24][22] = "24 22 NA987654321/LBCDEFGHIJKM"; + cache[24][23] = "24 23 D6I3J5E7421/CHB9FA8GKLMN"; + cache[25][2] = "25 2 BMEL7JC98531/NK4HFDOIGA62"; + cache[25][3] = "25 3 5BMJNDE87421/GAI9KFHOLC63"; + cache[25][4] = "25 4 5ANGKFM96321/LIJH7DEBOC84"; + cache[25][5] = "25 5 4NJDCL879321/OIMHE6GBKFA5"; + cache[25][6] = "25 6 3EJF5KA78421/LDHG9MBINOC6"; + cache[25][7] = ""; + cache[25][8] = "25 8 2DHNBLEA5431/K9ICJMF6G7O8"; + cache[25][9] = "25 9 23DIGAF87541/J6NHMKCOELB9"; + cache[25][10] = "25 10 28KHBE9C7431/ND6OFIJMLG5A"; + cache[25][11] = "25 11 25LGIMFA6431/OED97NJCHK8B"; + cache[25][12] = "25 12 1G8HK7E95632/JL4DIFMACNBO"; + cache[25][13] = "25 13 1CMKBD8A2753/JHLFON964IGE"; + cache[25][14] = "25 14 1DEOGHCA6542/LF9K8JNIBM73"; + cache[25][15] = ""; + cache[25][16] = "25 16 1ABIDJG56432/MHCLKE98NFO7"; + cache[25][17] = "25 17 1A6ILDB87542/NOEKG3HFMCJ9"; + cache[25][18] = "25 18 17JMILDG6432/NF89ECKHAO5B"; + cache[25][19] = "25 19 179LNA4E6532/OFCGJIBKHM8D"; + cache[25][20] = "25 20 15KDC9H86432/OGAJMILEN7BF"; + cache[25][21] = "25 21 14FJGB8L9532/OM6CKDANI7EH"; + cache[25][22] = "25 22 136ANIHC7542/OLGFMB9K8DEJ"; + cache[25][23] = ""; + cache[25][24] = ""; + cache[26][2] = "26 2 OKJLCHB97532/1NFDGP8MIEA64"; + cache[26][3] = "26 3 ENLKMH5B8432/1IJDAFPG7OC96"; + cache[26][4] = "26 4 APIENB9L5432/1HOM7FJD6KGC8"; + cache[26][5] = "26 5 7DNJMGOC5432/1BHEL96I8PKFA"; + cache[26][6] = "26 6 P1EBGL7F6432/5K98HMNJDAOIC"; + cache[26][7] = "26 7 4NOFPDKA6532/18BG7MICJH9LE"; + cache[26][8] = "26 8 MNKL9IBC5432/718AEPHDJF6OG"; + cache[26][9] = "26 9 JPDAFE875432/6NLGH9OMCKB1I"; + cache[26][10] = "26 10 MBHJINEA6532/8GCLF71DO9P4K"; + cache[26][11] = "26 11 DBAPKE986432/5HLGNI1OCFJ7M"; + cache[26][12] = "26 12 PGHMKEF75432/BLI6DCJ189NAO"; + cache[26][13] = "26 13 LJOHBKC97523/AMP8IN64GFE1D"; + cache[26][14] = "26 14 MAJFLIB97643/C1KEDHO2PN85G"; + cache[26][15] = "26 15 CNP9I1D86532/7BLGFAMHJEOK4"; + cache[26][16] = "26 16 E5MGPH197432/8JFOBKCLIADN6"; + cache[26][17] = "26 17 KCNO6PJ75342/DABGMELFI91H8"; + cache[26][18] = "26 18 JHN13FM86542/DG9OKCPBI7ELA"; + cache[26][19] = "26 19 EHFODB984532/AIMGNL7KP1J6C"; + cache[26][20] = "26 20 B4DLPI7A6532/8FCGNK1HMJO9E"; + cache[26][21] = "26 21 JH5BEOD76432/FMNA91KILP8CG"; + cache[26][22] = "26 22 MKGE1HD85432/J7BPNAL6O9CFI"; + cache[26][23] = "26 23 9LBNFJ765432/8HPDMO1ACEGIK"; + cache[26][24] = "26 24 OPBA98765432/N1CDEFGHIJKLM"; + cache[26][25] = "26 25 E8JBLCG65742/DKAI9H3FP1MNO"; + cache[27][2] = "27 2 7PNLJHEDB9532/FOKGC81QMIA64"; + cache[27][4] = "27 4 1QINFJ2AB6543/7PLD8M9EHOKGC"; + cache[27][6] = "27 6 1KMNCFQJB7643/AH25LEP8DG9OI"; + cache[27][8] = "27 8 1INGJEAC98643/DFQPL72HKBM5O"; + cache[27][10] = "27 10 2G3NEFKI98654/PQBJAMHLC17OD"; + cache[27][12] = "27 12 1GQOBCLA86543/JEPN2IDFHK7M9"; + cache[27][14] = "27 14 1GNO2QBC49753/MKADEIP86LJHF"; + cache[27][16] = "27 16 1E7JPQG985643/OCFMAKIDN2HBL"; + cache[27][18] = "27 18 1CP9JNH8A7534/QGOD6KEFMLB2I"; + cache[27][20] = "27 20 18OIBMFD9A453/QG7HKJCNPE2L6"; + cache[27][22] = "27 22 15KBPDOAE6743/QIHJL8NFG2M9C"; + cache[27][24] = "27 24 12MPGJCL76543/QEAKN89OBDFHI"; + while (scanf("%d %d", &b, &n) == 2 && b > 0) + { + if (cache[b][n] == "") + { + abort(); + } + printf("%s\n", cache[b][n].c_str()); + } +#endif +} diff --git a/11252.cpp b/11252.cpp new file mode 100644 index 0000000..3293567 --- /dev/null +++ b/11252.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +struct BoxType +{ + int m, price; + bool operator<(const BoxType &other) const + { + return m < other.m; + } +}; + +struct Box +{ + int m, count; + bool operator<(const Box &other) const + { + return m < other.m; + } +}; + +BoxType boxTypes[2000]; +Box boxes[2000]; +int M, N; +int extraCost; + +int got[1002][1024][2], got_id; +long long memo[1002][1024][2], INF = 1LL << 60; + +long long rec(int i, int j, int flag) +{ + long long &res = memo[i][j][flag]; + if (got[i][j][flag] == got_id) + { + return res; + } + got[i][j][flag] = got_id; + res = INF; + if (j == N) + { + return res = 0; + } + if (i == M) + { + return res = INF; + } + if (flag == 0) + { + res = MIN(res, rec(i, j, 1) + extraCost); + } + res = MIN(res, rec(i + 1, j, 0)); + if (flag == 1 && boxes[j].m <= boxTypes[i].m) + { + res = MIN(res, boxes[j].count * (long long)boxTypes[i].price + rec(i, j + 1, 1)); + } + return res; +} + +int main() +{ + memset(got, 0, sizeof(got)); + for (int cs = 1; scanf("%d %d %d", &extraCost, &M, &N) == 3; cs++) + { + if (extraCost == 0 && M == 0 && N == 0) + { + break; + } + for (int i = 0; i < M; i++) + { + scanf("%d %d", &boxTypes[i].m, &boxTypes[i].price); + } + sort(boxTypes, boxTypes + M); + for (int i = 0; i < N; i++) + { + scanf("%d %d", &boxes[i].m, &boxes[i].count); + } + sort(boxes, boxes + N); + got_id = cs + 100; + long long res = rec(0, 0, 0); + printf("case %d: ", cs); + if (res >= INF) + { + printf("not possible\n"); + } + else + { + printf("%lld\n", res); + } + } +} diff --git a/11253.cpp b/11253.cpp new file mode 100644 index 0000000..42ecafd --- /dev/null +++ b/11253.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int rx[32] = {0, 1}, ry[32] = {0, 0}; +int px, py, rot; +int main() +{ + int n, x, y, px, py, rot, i; + for (i = 2; i < 32; i++) + { + rx[i] = rx[i - 1] - ry[i - 1]; + ry[i] = rx[i - 1] + ry[i - 1]; + } + while (1) + { + scanf("%d", &n); + if (n < 0) + { + break; + } + px = py = rot = 0; + while (1) + { + if (!n) + { + break; + } + for (i = 0; i < 32; i++) + if ((1 << i) >= n) + { + break; + } + x = rx[i + 1]; + y = ry[i + 1]; + if (rot % 4 == 0) + { + px += x; + py += y; + } + else if (rot % 4 == 1) + { + px += y; + py += -x; + } + else if (rot % 4 == 2) + { + px += -x; + py += -y; + } + else + { + px += -y; + py += x; + } + rot++; + n = (1 << i) - n; + } + printf("(%d,%d)\n", px, py); + } + return 0; +} diff --git a/11254.cpp b/11254.cpp new file mode 100644 index 0000000..5420f3a --- /dev/null +++ b/11254.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +int f[16], cnt[16], fc; +int fl[1024], flc; +void build_factors(int cf, int pp) +{ + if (cf == fc) + { + fl[flc++] = pp; + return; + } + for (int i = 0, r = 1; i <= cnt[cf]; r *= f[cf], ++i) + { + build_factors(cf + 1, pp * r); + } +} +void try_factor(int &x, int z) +{ + if (!(x % z)) + { + int c = 0; + do + { + x /= z; + ++c; + } while (!(x % z)); + f[fc] = z; + cnt[fc++] = c; + } +} +void factor(int x) +{ + fc = 0; + try_factor(x, 2); + try_factor(x, 3); + for (int i = 6;; i += 6) + { + int f1 = i - 1, f2 = i + 1; + if (f1 * f1 > x) + { + break; + } + try_factor(x, f1); + try_factor(x, f2); + } + if (x != 1) + { + try_factor(x, x); + } +} +int main() +{ + for (int n; scanf("%d", &n) && n != -1;) + { + int a, b, i, maxdiff = -1, ta, tb, x, y; + factor(n << 1); + flc = 0; + build_factors(0, 1); + for (i = 0; i < flc; ++i) + { + x = fl[i], y = (n << 1) / x; + if (y > x && (x + y) & 1) + { + ta = (y - x - 1) >> 1; + tb = (y + x - 1) >> 1; + if (tb - ta > maxdiff) + { + a = ta; + b = tb; + maxdiff = b - a; + } + } + } + printf("%d = %d + ... + %d\n", n, a + 1, b); + } + return 0; +} diff --git a/11255.cpp b/11255.cpp new file mode 100644 index 0000000..09b368f --- /dev/null +++ b/11255.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +long long cn[41][41]; +void init(void) +{ + cn[0][0] = 1; + for (int i = 1; i <= 40; i++) + for (int j = 0; j <= i; j++) + { + cn[i][j] = cn[i - 1][j - 1] + cn[i - 1][j]; + } +} +long long gcd(long long a, long long b) +{ + while (b) + { + b ^= a ^= b ^= a %= b; + } + return a; +} +int main(void) +{ + int cas, n, cnt[3]; + init(); + cin >> cas; + while (cas--) + { + cin >> cnt[0] >> cnt[1] >> cnt[2]; + n = cnt[0] + cnt[1] + cnt[2]; + long long ans = 0; + for (int i = 1; i <= n; i++) + { + long long t = n / gcd(i, n); + if (cnt[0] % t || cnt[1] % t || cnt[2] % t) + { + continue; + } + ans += cn[n / t][cnt[0] / t] * cn[n / t - cnt[0] / t][cnt[1] / t]; + } + if (n % 2) + { + for (int i = 0; i < 3; cnt[i++]++) + { + cnt[i]--; + if (cnt[i] < 0) + { + continue; + } + if (cnt[0] % 2 || cnt[1] % 2 || cnt[2] % 2) + { + continue; + } + ans += n * cn[n / 2][cnt[0] / 2] * cn[n / 2 - cnt[0] / 2][cnt[1] / 2]; + } + } + else + { + if (!(cnt[0] % 2 || cnt[1] % 2 || cnt[2] % 2)) + { + ans += n / 2 * cn[n / 2][cnt[0] / 2] * cn[n / 2 - cnt[0] / 2][cnt[1] / 2]; + } + for (int i = 0; i < 3; cnt[i++]++) + { + cnt[i]--; + if (cnt[i] < 0) + { + continue; + } + for (int j = 0; j < 3; cnt[j++]++) + { + cnt[j]--; + if (cnt[j] < 0) + { + continue; + } + if (cnt[0] % 2 || cnt[1] % 2 || cnt[2] % 2) + { + continue; + } + ans += n / 2 * cn[n / 2 - 1][cnt[0] / 2] * cn[n / 2 - 1 - cnt[0] / 2][cnt[1] / 2]; + } + } + } + ans /= 2 * n; + cout << ans << endl; + } +} diff --git a/11256.cpp b/11256.cpp new file mode 100644 index 0000000..eb21f81 --- /dev/null +++ b/11256.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +char str[500], ttt[500]; +int len; +unsigned long long int v, ans; +unsigned long long int GCD(unsigned long long int a, unsigned long long int b) +{ + if (b == 0) + { + return a; + } + else + { + return GCD(b, a % b); + } +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%s", str); + len = strlen(str); + sscanf(str, "%lld", &v); + ans = -1; + for (int i = 1; i <= len; i++) + { + unsigned long long int base = 1; + for (int j = 1; j <= i; j++) + { + base *= 10; + } + unsigned long long int mul = 1, temp; + for (int k = i + 1; k <= len + len; k += i) + { + if (i != 1 && i + k - 1 > 2 * len) + { + break; + } + mul = (unsigned long long int)mul * base + (unsigned long long int)1; + if (i == 1 && k == 1) + { + continue; + } + unsigned long long int d = GCD(mul, v); + temp = v / d; + sprintf(ttt, "%lld", temp); + int len1 = strlen(ttt); + if (len1 <= i) + { + if (temp == 1) + { + while (len1 < i) + { + temp *= 10; + len1++; + } + } + else + { + unsigned long long int sbase = temp; + while (len1 < i) + { + temp += sbase; + sprintf(ttt, "%d", temp); + len1 = strlen(ttt); + } + } + unsigned long long int s1 = (unsigned long long int)temp * mul; + if (ans == -1 || s1 < ans) + { + //printf("%I64d %I64d\n", temp, mul); + ans = s1; + } + } + } + } + printf("%lld\n", ans); + } +} diff --git a/11257.cpp b/11257.cpp new file mode 100644 index 0000000..efe9a62 --- /dev/null +++ b/11257.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define EPS 1E-6 + +struct Ponto +{ + double x, y; + Ponto(double x = 0, double y = 0) : x(x), y(y) {} + Ponto operator-(Ponto p) + { + return Ponto(x - p.x, y - p.y); + } + double operator*(Ponto p) + { + return x * p.x + y * p.y; + } + double operator%(Ponto p) + { + return x * p.y - y * p.x; + } + double dist(Ponto p) + { + return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y); + } +} pontos[120], dif[120]; +int n; +double mini, maxi, dista[120]; +int read() +{ + scanf("%d", &n); + if (!n) + { + return 0; + } + mini = 10000; + maxi = -10000; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &pontos[i].x, &pontos[i].y); + if (pontos[i].x < mini) + { + mini = pontos[i].x; + } + if (pontos[i].x > maxi) + { + maxi = pontos[i].x; + } + } + return 1; +} +double dist(int i1, int i2, Ponto p) +{ + if ((p - pontos[i1]) * (dif[i1]) > 0) + { + return p.dist(pontos[i1]); + } + else if ((p - pontos[i2]) * (dif[i1]) < 0) + { + return p.dist(pontos[i2]); + } + double ar = (p - pontos[i2]) % (dif[i1]); + return (ar * ar) / dista[i1]; +} +double g(double x, double y) +{ + int ant = n - 1; + double ret = 1000000000, d; + Ponto p(x, y); + for (int i = 0; i < n; i++) + { + d = dist(i, ant, p); + if (d < ret) + { + ret = d; + } + ant = i; + } + return ret; +} +double f(double x) +{ + int ant = n - 1; + double y1 = -10000, y2, y, g1, g2; + for (int i = 0; i < n; i++) + { + if (x >= min(pontos[i].x, pontos[ant].x) && + x <= max(pontos[i].x, pontos[ant].x)) + { + if (y1 < -5000) + { + y1 = pontos[i].y + ((pontos[ant].y - pontos[i].y) / (pontos[ant].x - pontos[i].x)) * (x - pontos[i].x); + } + else + { + y2 = pontos[i].y + ((pontos[ant].y - pontos[i].y) / (pontos[ant].x - pontos[i].x)) * (x - pontos[i].x); + } + } + ant = i; + } + if (y1 > y2) + { + y = y1; + y1 = y2; + y2 = y; + } + //printf("x= %lf, y1 = %lf, y2 = %lf\n", x, y1,y2); + while (y1 < y2 - EPS) + { + g1 = g(x, (2 * y1 + y2) / 3); + g2 = g(x, (y1 + 2 * y2) / 3); + if (g1 < g2) + { + y1 = (2 * y1 + y2) / 3; + } + else + { + y2 = (y1 + 2 * y2) / 3; + } + } + // printf("g[%lf] = %lf\n", y1,g(x,y1)); + return g(x, y1); +} +void process() +{ + double f1, f2; + int a = n - 1; + for (int i = 0; i < n; i++) + { + dif[i] = pontos[i] - pontos[a]; + dista[i] = pontos[i].dist(pontos[a]); + a = i; + } + while (mini < maxi - EPS) + { + f1 = f((2 * mini + maxi) / 3); + f2 = f((mini + 2 * maxi) / 3); + //printf("f[%lf] = %lf, f[%lf] = %lf\n",(mini+2*maxi)/3,f1, (2*mini+maxi)/3,f2); + if (f1 < f2) + { + mini = (2 * mini + maxi) / 3; + } + else + { + maxi = (mini + 2 * maxi) / 3; + } + } + printf("%.3lf\n", sqrt(f(mini))); +} +int main() +{ + while (read()) + { + process(); + } + return 0; +} diff --git a/11258.cpp b/11258.cpp new file mode 100644 index 0000000..6ff44e2 --- /dev/null +++ b/11258.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define INF 0x7fffffff +#define MAXD 210 +long long int f[MAXD]; +char b[MAXD]; +void solve() +{ + int i, j, k, N; + long long int v; + scanf("%s", b); + N = strlen(b); + memset(f, 0, sizeof(f)); + for (i = 0; i < N; i++) + { + if (b[i] == '0') + { + if (f[i] > f[i + 1]) + { + f[i + 1] = f[i]; + } + } + else + { + v = 0; + for (j = i; j < N; j++) + { + v = v * 10 + b[j] - '0'; + if (v > INF) + { + break; + } + if (f[i] + v > f[j + 1]) + { + f[j + 1] = f[i] + v; + } + } + } + } + printf("%lld\n", f[N]); +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + solve(); + } + return 0; +} diff --git a/11259.cpp b/11259.cpp new file mode 100644 index 0000000..d3e5a4b --- /dev/null +++ b/11259.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int c[4], q, d[4], v; +long long int cnt[100001]; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d%d%d", c + 0, c + 1, c + 2, c + 3, &q); + memset(cnt, 0, sizeof(cnt)); + cnt[0] = 1; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j <= 100000 - c[i]; j++) + { + if (cnt[j]) + { + cnt[j + c[i]] += cnt[j]; + } + } + } + while (q--) + { + long long int total = 0; + scanf("%d %d %d %d %d", d + 0, d + 1, d + 2, d + 3, &v); + for (int i = 0; i < 16; i++) + { + int tv = v, s = 0; + for (int j = 1, k = 0; j < 16; j <<= 1, k++) + { + if (i & j) + { + tv -= c[k] * (d[k] + 1); + s++; + } + } + if (tv < 0) + { + continue; + } + if (s & 1) + { + total -= cnt[tv]; + } + else + { + total += cnt[tv]; + } + } + printf("%lld\n", total); + } + } + return 0; +} diff --git a/11260.cpp b/11260.cpp index 67f6020..080dbcc 100644 --- a/11260.cpp +++ b/11260.cpp @@ -1,18 +1,99 @@ -#include -#include -#define limit (long long)pow(2.0,20.0) -#define max 100000000 -using namespace std; -int main(){ - long long root[limit+1],i; - for(i=1;i<=limit;i++) - root[i]=(root[i-1]+(long long)sqrt(2*i-1))%max; - for(;;){ - long long n; - cin>>n; - if(n==0) - break; - cout< + +using namespace std; + +typedef long long llong; +llong mod = 100000000; + +llong fx(llong n) +{ + llong product = 1; + int res = n % 3; + if (res == 0) + { + product *= (n / 3); + product %= mod; + product *= (n + 1); + product %= mod; + product *= (4 * n - 1); + product %= mod; + } + else if (res == 2) + { + product *= (n + 1) / 3; + product %= mod; + product *= n; + product %= mod; + product *= (4 * n - 1); + product %= mod; + } + else + { + product = (4 * n - 1) / 3; + product %= mod; + product *= n; + product %= mod; + product *= (n + 1); + product %= mod; + } + return product; +} + +llong fy(llong n) +{ + llong product = 1; + int res = n % 3; + if (res == 0) + { + product *= (n / 3); + product %= mod; + product *= (n + 1); + product %= mod; + product *= (2 * n + 1); + product %= mod; + } + else if (res == 2) + { + product *= (n + 1) / 3; + product %= mod; + product *= n; + product %= mod; + product *= (2 * n + 1); + product %= mod; + } + else + { + product = (2 * n + 1) / 3; + product %= mod; + product *= n; + product %= mod; + product *= (n + 1); + product %= mod; + } + return (2 * product) % mod; +} + +int main() +{ + llong n, n1, n2, x, y, z, m; + while (scanf("%lld", &n) != EOF && n) + { + if (n > 0) + { + n -= !(n % 2); + m = sqrt(n); + n2 = (m - 1) >> 1, n1 = (m) >> 1; + x = fx(n1); + y = fy(n2); + z = ((n - m * m + 2 * (m % 2) + (m % 2 == 0)) / 2) % mod; + z *= m; + z %= mod; + printf("%lld\n", (x + y + z) % mod); + } + else + { + printf("0\n"); + } + } + return 0; +} diff --git a/11261.cpp b/11261.cpp new file mode 100644 index 0000000..d0bedeb --- /dev/null +++ b/11261.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define MAXN 80010 +bool vis1[MAXN * 2 + 10], vis2[MAXN * 2 + 10]; +int main() +{ + // freopen("1.in","r",stdin); + int T, n, m, i, x, y, t = 0; + scanf("%d", &T); + while (true) + { + if (t == T) + { + break; + } + t++; + memset(vis1, 0, sizeof(vis1)); + memset(vis2, 0, sizeof(vis2)); + scanf("%d%d", &n, &m); + long long ans = 0; + for (i = 1; i <= m; i++) + { + scanf("%d%d", &x, &y); + if (!vis1[x + y - 1]) + { + if (x + y - 1 > n) + { + ans += 2 * n - (x + y - 1); + } + else + { + ans += x + y - 1; + } + } + vis1[x + y - 1] = 1; + if (!vis2[x - y + n]) + { + if (x - y + n > n) + { + ans += n - (x - y); + } + else + { + ans += x - y + n; + } + } + vis2[x - y + n] = 1; + } + long long cnt1 = 0, cnt2 = 0; + for (i = 1; i <= n; i++) + { + x = i; + if (i % 2 == 1) + { + if (vis2[n - x + 1]) + { + cnt1++; + } + if (vis2[n + x - 1] && (n + x - 1) != n) + { + cnt1++; + } + if (vis1[i]) + { + ans -= cnt1; + } + } + else if (i % 2 == 0) + { + if (vis2[n - x + 1]) + { + cnt2++; + } + if (vis2[n + x - 1] && (n + x - 1) != n) + { + cnt2++; + } + if (vis1[i]) + { + ans -= cnt2; + } + } + } + cnt1 = 0, cnt2 = 0; + for (i = 2 * n - 1; i > n; i--) + { + x = 2 * n - i; + if (i % 2 == 1) + { + if (vis2[n - x + 1]) + { + cnt1++; + } + if (vis2[n + x - 1] && (n + x - 1) != n) + { + cnt1++; + } + if (vis1[i]) + { + ans -= cnt1; + } + } + else if (i % 2 == 0) + { + if (vis2[n - x + 1]) + { + cnt2++; + } + if (vis2[n + x - 1] && (n + x - 1) != n) + { + cnt2++; + } + if (vis1[i]) + { + ans -= cnt2; + } + } + } + ans = n * n - ans; + printf("Case #%d: ", t); + printf("%lld\n", ans); + // if(t!=T) printf("\n"); + } +} diff --git a/11262.cpp b/11262.cpp new file mode 100644 index 0000000..bdd2c66 --- /dev/null +++ b/11262.cpp @@ -0,0 +1,203 @@ +#include + +using namespace std; + +const int N = 100 + 3; + +struct Edge +{ + double cost; + int dst; + Edge() {} + Edge(int d, double c) : cost(c), dst(d) {} +}; + +vector g[N]; +const int src = N - 1; +const int dst = N - 2; + +bool f[N][N]; + +int path[N]; + +const double eps = 1e-11; + +inline bool eq(double a, double b) +{ + return fabs(a - b) < eps; +} + +inline bool lessThan(double a, double b) +{ + return !eq(a, b) && a < b; +} + +inline bool greaterThan(double a, double b) +{ + return !eq(a, b) && a < b; +} + +int bfs(int lim) +{ + static int p[N]; + static bool vis[N]; + queue q; + fill(vis, vis + N, false); + fill(p, p + N, -1); + vis[src] = true; + for (q.push(src); q.size(); q.pop()) + { + int n = q.front(); + for (int i = 0; i < g[n].size(); ++i) + { + int m = g[n][i].dst; + if (vis[m]) + { + continue; + } + if (!f[n][m]) + { + continue; + } + if (lessThan(lim, g[n][i].cost)) + { + continue; + } + vis[m] = true; + q.push(m); + p[m] = n; + } + } + if (!vis[dst]) + { + return 0; + } + int size = 0; + for (int i = dst; i != -1; i = p[i]) + { + path[size++] = i; + } + reverse(path, path + size); + return size; +} + +int flow(int lim) +{ + static bool tmp[N][N]; + copy(&f[0][0], &f[N - 1][N], &tmp[0][0]); + int sum = 0; + for (int size; size = bfs(lim); ++sum) + { + for (int i = 0; i + 1 < size; ++i) + { + f[path[i]][path[i + 1]] = false; + f[path[i + 1]][path[i]] = true; + } + } + copy(&tmp[0][0], &tmp[N - 1][N], &f[0][0]); + return sum; +} + +bool make_graph(int size, int m) +{ + pair p[size]; + char color[size]; + fill(g, g + N, vector()); + for (int i = 0; i < size; ++i) + { + string s; + cin >> p[i].first >> p[i].second >> s; + color[i] = s[0]; + } + if (count(color, color + size, 'r') < m) + { + return false; + } + if (count(color, color + size, 'b') < m) + { + return false; + } + for (int i = 0; i < size; ++i) + { + if (color[i] == 'b') + { + continue; + } + for (int j = 0; j < size; ++j) + { + if (color[j] == 'r') + { + continue; + } + double a = p[i].first - p[j].first; + double b = p[i].second - p[j].second; + double c = sqrt(a * a + b * b); + f[i][j] = true; + f[j][i] = false; + g[i].push_back(Edge(j, c)); + g[j].push_back(Edge(i, c)); + } + } + for (int i = 0; i < size; ++i) + { + if (color[i] == 'r') + { + f[src][i] = true; + g[src].push_back(Edge(i, 0)); + } + } + for (int i = 0; i < size; ++i) + { + if (color[i] == 'b') + { + f[i][dst] = true; + g[i].push_back(Edge(dst, 0)); + } + } + return true; +} + +int main() +{ + int tc; + cin >> tc; + while (tc--) + { + int n, m; + cin >> n >> m; + if (!make_graph(n, m)) + { + cout << "Impossible" << endl; + continue; + } + int s = 1, b = 2000 * 2; + while (s + 1 < b) + { + int c = (s + b) / 2; + int f = flow(c); + //cout << c << ' ' << (m <= f ? "T" : "F") << endl; + if (m <= f) + { + b = c; + } + else + { + s = c; + } + } + double mx = 0; + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < g[i].size(); ++j) + { + if (lessThan(b, g[i][j].cost)) + { + continue; + } + mx = max(mx, g[i][j].cost); + } + } + cout << (int)ceil(mx) << endl; + } + return 0; +} diff --git a/11264.cpp b/11264.cpp new file mode 100644 index 0000000..37778bd --- /dev/null +++ b/11264.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int lis[1001]; + +int main() +{ + int count, n, i, j, maxlen, temp; + scanf("%d", &count); + while (count--) + { + scanf("%d%lld", &n, &lis[1]); + maxlen = 1; + for (i = 1; i < n; i++) + { + scanf("%d", &temp); + for (j = maxlen; j > 0; j--) + { + if (lis[j] < temp) + { + lis[j + 1] = lis[j] + temp; + break; + } + } + maxlen += (j == maxlen); + } + printf("%d\n", maxlen); + } + return 0; +} diff --git a/11265.cpp b/11265.cpp new file mode 100644 index 0000000..1c7098b --- /dev/null +++ b/11265.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +typedef complex Point; +typedef pair Segment; + +const double eps = 1E-8; +const int MaxN = 20000 + 10; + +Segment s[MaxN]; +Point p[2][MaxN]; +int pn[2], cur, nxt; + +void init_half_plane() +{ + cur = 0; + nxt = 1; + pn[cur] = 4; + p[cur][0] = Point(0.0, 0.0); + p[cur][1] = Point(1000000.0, 0.0); + p[cur][2] = Point(1000000.0, 1000000.0); + p[cur][3] = Point(0, 1000000.0); +} +double cross(const Point &a, const Point &b) +{ + return (conj(a) * b).imag(); +} +int sgn(double x) +{ + return x > eps ? 1 : (x < -eps ? -1 : 0); +} +bool line_intersect(const Point &a, const Point &b, const Point &p, const Point &q, Point &ans) +{ + double d1 = cross(p - a, b - a), d2 = cross(q - a, b - a); + if (fabs(d1 - d2) < eps) + { + return false; + } + ans = (d1 * q - d2 * p) / (d1 - d2); + return true; +} +void add_half_plane(const Point &a, const Point &b)// direction is the left to the a->b vector +{ + nxt = 1 - cur; + pn[nxt] = 0; + int n = pn[cur], &m = pn[nxt]; + p[cur][n] = p[cur][0]; + Point t1 = p[cur][0]; + int o1 = sgn(cross(a - t1, b - t1)); + for (int i = 1; i <= n; ++i) + { + Point t2 = p[cur][i]; + int o2 = sgn(cross(a - t2, b - t2)); + if (o1 >= 0) + { + p[nxt][m++] = t1; + } + if ((o1 ^ o2) == -2) + { + line_intersect(a, b, t1, t2, p[nxt][m++]); + } + t1 = t2; + o1 = o2; + } + cur = nxt; +} + +int main() +{ + int n, cc = 0; + double w, h, x, y, x1, y1, x2, y2; + for (; scanf("%d%lf%lf%lf%lf", &n, &w, &h, &x, &y) != EOF;) + { + init_half_plane(); + Point ori(x, y), p1(0.0, 0.0), p2(w, 0.0), p3(w, h), p4(0.0, h); + add_half_plane(p1, p2); + add_half_plane(p2, p3); + add_half_plane(p3, p4); + add_half_plane(p4, p1); + for (int i = 0; i < n; ++i) + { + scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); + Point t1(x1, y1), t2(x2, y2); + if (sgn(cross(t2 - t1, ori - t1)) < 0) + { + swap(t1, t2); + } + add_half_plane(t1, t2); + } + double ans = 0.0; + p[cur][pn[cur]] = p[cur][0]; + for (int i = 0; i < pn[cur]; ++i) + { + ans += cross(p[cur][i], p[cur][i + 1]); + } + printf("Case #%d: %.3f\n", ++cc, (ans / 2.0)); + } + return 0; +} diff --git a/11266.cpp b/11266.cpp new file mode 100644 index 0000000..2a25473 --- /dev/null +++ b/11266.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +const int shift = 100000; +int dp[2][200005]; +int main() +{ + int testcase, n, s; + int A[16], B[16]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &s); + for (int i = 0; i < n; i++) + scanf("%d %d", &A[i], &B[i]); + + memset(dp, 0, sizeof(dp)); + for (int i = A[0]; i <= B[0]; i++) + dp[0][i + shift] = 1; + int f = 1; + for (int i = 1; i < n; i++) + { + memset(dp[f], 0, sizeof(dp[f])); + int l = -B[i], r = -A[i], sum = 0; + for (int j = 0 - B[i]; j <= 0 - A[i]; j++) + { + if (j < 0 || j >= 200005) + continue; + sum += dp[!f][j]; + sum %= 200003; + } + for (int j = 0; j < 200005; j++) + { + dp[f][j] = sum; + if (l >= 0 && l < 200005) + sum -= dp[!f][l]; + if (r + 1 >= 0 && r + 1 < 200005) + sum += dp[!f][r + 1]; + // if(sum) + // printf("%d %d %d\n", i, j - shift, sum); + sum %= 200003; + l++, r++; + } + f = !f; + } + printf("%d\n", (dp[!f][s + shift] + 200003) % 200003); + } + return 0; +} diff --git a/11267.cpp b/11267.cpp new file mode 100644 index 0000000..1d2bf93 --- /dev/null +++ b/11267.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define init(i, a, n) \ + for (i = 1; i <= n; i++) \ + a[i] = i +#define INF 10000000 + +typedef struct edge {int f, t, d;} E; +E lst[40000]; + +char matrix[205][205]; +int mark[205], set_[205]; + +typedef struct dNode +{ + int content; + struct dNode *next; + struct dNode *prev; +} node; + +typedef struct dLinkedList +{ + node header; + node trailer; + int size; +} linkedList; + +linkedList *LinkedList() +{ + linkedList *l = (linkedList *)malloc(sizeof(linkedList)); + (l->header).next = &(l->trailer); + (l->trailer).prev = &(l->header); + l->size = 0; + return l; +} +int getParent(int i, int *set_) +{ + if (i == set_[i]) + { + return i; + } + else + { + return (set_[i] = getParent(set_[i], set_)); + } +} +int isUnion(int a, int b, int *set_) +{ + return getParent(a, set_) == getParent(b, set_); +} +void makeUnion(int a, int b, int *set_) +{ + set_[getParent(a, set_)] = getParent(b, set_); +} +void push_back(int val, linkedList *l) +{ + node *n = (node *)malloc(sizeof(node)); + n->content = val; + n->next = &(l->trailer); + n->prev = l->trailer.prev; + (l->trailer.prev)->next = n; + l->trailer.prev = n; + l->size++; +} +int pop_front(linkedList *l) +{ + int res; + node *n = l->header.next; + l->header.next = n->next; + n->next->prev = &(l->header); + res = n->content; + free(n); + l->size--; + return res; +} +int cmp(const void *a, const void *b) +{ + return ((E *)a)->d - ((E *)b)->d; +} +int validate(int n) +{ + int i, j, c, ex; + for (i = 1; i <= n; i++) + { + mark[i] = 0; + } + linkedList *Q = LinkedList(); + for (j = 1; j <= n; j++) + if (!mark[j]) + { + mark[j] = 1; + push_back(j, Q); + while (Q->size) + { + c = pop_front(Q); + ex = mark[c] ^ 3; + for (i = 1; i <= n; i++) + if (matrix[c][i]) + { + if (!mark[i]) + { + mark[i] = ex; + push_back(i, Q); + } + if (mark[i] != ex) + { + return 0; + } + } + } + } + return 1; +} + +int main() +{ + register int i, j, res, d; + int n, e; + scanf("%d", &n); + while (n) + { + scanf("%d", &e); + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + { + matrix[i][j] = 0; + } + for (i = 0; i < e; i++) + { + scanf("%d %d %d", &lst[i].f, &lst[i].t, &lst[i].d); + matrix[lst[i].f][lst[i].t] = matrix[lst[i].t][lst[i].f] = 1; + } + if (!validate(n)) + { + puts("Invalid data, Idiot!"); + } + else + { + init(i, set_, n); + res = 0; + d = 0; + qsort(lst, e, sizeof(E), &cmp); + for (i = 0; i < e && d < n - 1; i++) + if (!isUnion(lst[i].f, lst[i].t, set_)) + { + makeUnion(lst[i].f, lst[i].t, set_); + res += lst[i].d; + d++; + } + else if (lst[i].d < 0) + { + res += lst[i].d; + } + for (; i < e && lst[i].d < 0; i++) + { + res += lst[i].d; + } + printf("%d\n", res); + } + scanf("%d", &n); + } + return 0; +} diff --git a/11268.cpp b/11268.cpp new file mode 100644 index 0000000..5ea080e --- /dev/null +++ b/11268.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define db(x) cout << #x " == " << x << endl +//#define _ << ", " << +#define iter(a) typeof((a).begin()) +#define Tr(a, b) for (iter(b) a = (b).begin(); a != (b).end(); ++a) +#define Fr(a, b, c) for (int a = b; a < c; ++a) +#define rF(a, b, c) for (int a = c - 1; a >= b; --a) +#define Rp(a, b) Fr(a, 0, b) +#define fr Fr +#define rp Rp +#define cl(a, b) memset(a, b, sizeof(a)) +typedef long long ll; +typedef unsigned long long ull; +typedef pair pii; +typedef pair> pdi; +typedef vector> vpii; +#define F first +#define S second +#define MP make_pair +#define PB push_back +#define y1 Y1 +#define INF 0x3f3f3f3f +#define LINF 0x3f3f3f3f3f3f3f3fLL +#define ULMAX 0xffffffffffffffffULL +#define MAXN 1000 +#define MAXM 120000 +#define MOD 200003 + +int gcd(int a, int b) +{ + return (!b) ? a : gcd(b, a % b); +} + +int T, n, m, mm; +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + mm = 0; + if (m & 1) + printf("%d\n", n * ((m + 1) >> 1)); + else + printf("%d\n", n * (m >> 1) + ((n + 1) >> 1)); + fr(i, 1, n) + { + fr(j, 1, n + 1) printf("%d %d\n", j, (j + i - 1) % n + 1); + mm += 2; + if (mm >= m) + break; + } + if (m % 2 == 0) + { + fr(j, 1, ((n + 1) >> 1) + 1) printf("%d %d\n", j, (j + n / 2 - 1) % n + 1); + } + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/11269.cpp b/11269.cpp new file mode 100644 index 0000000..7809549 --- /dev/null +++ b/11269.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +// Johnson's scheduling algorithm +// Part Pi has two operations, of duration Pi1, Pi2 +// to be done on Machine M1, M2 in that sequence. +// #1. List A = { 1, 2, ? N }, List L1 = {}, List L2 = {}. +// #2. From all available operation durations, pick the minimum. +// Remove K from list A; +// If the minimum belongs to Pk1, +// Add K to end of List L1. +// If minimum belongs to Pk2, +// Add K to beginning of List L2. +// #3. Repeat Step 2 until List A is empty. +// #4. Join List L1, List L2. This is the optimal sequence. + +const int MAX = 100; + +static int L1[MAX], L2[MAX], e[MAX], l1, l2, n, P[2][MAX], choose[MAX]; + +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &P[0][i]); + for (int i = 0; i < n; i++) + scanf("%d", &P[1][i]); + for (int i = 0; i < n; i++) + choose[i] = 0; + l1 = l2 = 0; + int time = 0; + for (int i = 0; i < n; i++) + { + int min = 9999, minp, minc; + for (int j = 0; j < n; j++) + { + if (choose[j]) + continue; + if (P[0][j] < min) + { + min = P[0][j]; + minp = j; + minc = 0; + } + if (P[1][j] < min) + { + min = P[1][j]; + minp = j; + minc = 1; + } + } + choose[minp] = 1; + if (minc == 0) + L1[l1++] = minp; + else + L2[l2++] = minp; + } + int start = 0; + for (int i = 0; i < l1; i++) + { + start += P[0][L1[i]]; + e[i] = start; + } + for (int i = l1, j = l2 - 1; j >= 0; i++, j--) + { + start += P[0][L2[j]]; + e[i] = start; + } + for (int i = 0; i < l1; i++) + { + if (time < e[i]) + time = e[i]; + time += P[1][L1[i]]; + } + for (int i = l1, j = l2 - 1; j >= 0; i++, j--) + { + if (time < e[i]) + time = e[i]; + time += P[1][L2[j]]; + } + printf("%d\n", time); + } + return 0; +} diff --git a/11275.cpp b/11275.cpp new file mode 100644 index 0000000..1af6c56 --- /dev/null +++ b/11275.cpp @@ -0,0 +1,248 @@ +#include + +using namespace std; + +#define abs(x) ((x) >= 0 ? (x) : -(x)) +#define i64 long long +#define u32 unsigned int +#define u64 unsigned long long +#define clr(x, y) memset(x, y, sizeof(x)) +#define CLR(x) x.clear() +#define ph(x) push(x) +#define pb(x) push_back(x) +#define Len(x) x.length() +#define SZ(x) x.size() +#define PI acos(-1.0) +#define sqr(x) ((x) * (x)) +#define MP(x, y) make_pair(x, y) +#define EPS 1e-10 + +#define FOR0(i, x) for (i = 0; i < x; i++) +#define FOR1(i, x) for (i = 1; i <= x; i++) +#define FOR(i, a, b) for (i = a; i <= b; i++) +#define FORL0(i, a) for (i = a; i >= 0; i--) +#define FORL1(i, a) for (i = a; i >= 1; i--) +#define FORL(i, a, b) for (i = a; i >= b; i--) + +#define rush() \ + int CC; \ + for (scanf("%d", &CC); CC--;) +#define Rush(n) while (scanf("%d", &n) != -1) + +void RD(int &x) { scanf("%d", &x); } +void RD(i64 &x) { scanf("%lld", &x); } +void RD(u64 &x) { scanf("%I64u", &x); } +void RD(u32 &x) { scanf("%u", &x); } +void RD(double &x) { scanf("%lf", &x); } +void RD(int &x, int &y) { scanf("%d%d", &x, &y); } +void RD(i64 &x, i64 &y) { scanf("%lld%lld", &x, &y); } +void RD(u32 &x, u32 &y) { scanf("%u%u", &x, &y); } +void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); } +void RD(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); } +void RD(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); } +void RD(i64 &x, i64 &y, i64 &z) { scanf("%lld%lld%lld", &x, &y, &z); } +void RD(u32 &x, u32 &y, u32 &z) { scanf("%u%u%u", &x, &y, &z); } +void RD(char &x) { x = getchar(); } +void RD(char *s) { scanf("%s", s); } +void RD(string &s) { cin >> s; } + +void PR(int x) { printf("%d\n", x); } +void PR(int x, int y) { printf("%d %d\n", x, y); } +void PR(i64 x) { printf("%lld\n", x); } +void PR(i64 x, i64 y) { printf("%lld %lld\n", x, y); } +void PR(u32 x) { printf("%u\n", x); } +void PR(u64 x) { printf("%llu\n", x); } +void PR(double x) { printf("%.10lf\n", x); } +void PR(double x, double y) { printf("%.3lf %.3lf\n", x, y); } +void PR(char x) { printf("%c\n", x); } +void PR(char *x) { printf("%s\n", x); } +void PR(string x) { cout << x << endl; } + +const int mod = 10007; +const i64 inf = ((i64)1) << 40; +const double dinf = 1000000000000000000.0; +const int INF = 100000000; +const int N = 10005; + +int sgn(double x) +{ + if (x > EPS) + return 1; + if (x < -EPS) + return -1; + return 0; +} + +struct point +{ + double x, y; + + point() {} + point(double _x, double _y) + { + x = _x; + y = _y; + } + + void get() + { + RD(x); + RD(y); + } + + point operator+(point a) + { + return point(x + a.x, y + a.y); + } + + point operator-(point a) + { + return point(x - a.x, y - a.y); + } + + double operator*(point a) + { + return x * a.y - y * a.x; + } + + point operator*(double t) + { + return point(x * t, y * t); + } + + double operator^(point a) + { + return x * a.x + y * a.y; + } + + double len() + { + return sqrt(x * x + y * y); + } + + void print() + { + printf("%.3lf %.3lf\n", x + EPS, y + EPS); + } +}; + +double len(point a) +{ + return a.len(); +} + +struct point3 +{ + double x, y, z; + + point3() {} + point3(double _x, double _y, double _z) + { + x = _x; + y = _y; + z = _z; + } + + void get() + { + RD(x); + RD(y); + RD(z); + } + + point3 operator+(point3 a) + { + return point3(x + a.x, y + a.y, z + a.z); + } + + point3 operator-(point3 a) + { + return point3(x - a.x, y - a.y, z - a.z); + } + + point3 operator*(point3 a) + { + return point3(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x); + } + + point3 operator*(double t) + { + return point3(x * t, y * t, z * t); + } + + double operator^(point3 a) + { + return x * a.x + y * a.y + z * a.z; + } + + point3 operator/(double t) + { + return point3(x / t, y / t, z / t); + } + + double len() + { + return sqrt(x * x + y * y + z * z); + } + + point3 adjust(double L) + { + double t = len(); + L /= t; + return point3(x * L, y * L, z * L); + } + + void print() + { + printf("%.10lf %.10lf %.10lf\n", x + EPS, y + EPS, z + EPS); + } +}; + +double len(point3 a) +{ + return a.len(); +} + +int opposite(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + point3 v = (b - a) * (c - a); + double x = v ^ (p - a); + double y = v ^ (q - a); + return sgn(x * y) <= 0; +} + +int segCrossTri(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + return opposite(p, q, a, b, c) && + opposite(a, b, p, q, c) && + opposite(a, c, p, q, b) && + opposite(b, c, p, q, a); +} + +point3 a1, b1, c1, a2, b2, c2; + +int main() +{ + rush() + { + a1.get(); + b1.get(); + c1.get(); + + a2.get(); + b2.get(); + c2.get(); + + if (segCrossTri(a1, b1, a2, b2, c2) || + segCrossTri(a1, c1, a2, b2, c2) || + segCrossTri(b1, c1, a2, b2, c2) || + segCrossTri(a2, b2, a1, b1, c1) || + segCrossTri(a2, c2, a1, b1, c1) || + segCrossTri(b2, c2, a1, b1, c1)) + { + puts("1"); + } + else + puts("0"); + } +} diff --git a/11277.cpp b/11277.cpp new file mode 100644 index 0000000..a7e61c9 --- /dev/null +++ b/11277.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +double heron(double a, double b, double c) +{ + if (a + b < c || b + c < a || c + a < b) + { + return 0; + } + double s = (a + b + c) / 2; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} + +int main() +{ + int T, n; + double A[64]; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + double sum = 0; + for (int i = 0; i < n; i++) + { + scanf("%lf", &A[i]), sum += A[i]; + } + double l = 0, r = sum, mid = A[0]; + int ok = 1; + for (int i = 0; i < n; i++) + { + l = max(l, A[i] / 2); + } + if (sum - l * 2 < l * 2) + { + ok = 0; + } + for (int it = 0; it < 40; it++) + { + mid = (l + r) / 2; + double theta = 0.0f; + for (int i = 0; i < n; i++) + { + theta += asin((A[i] / 2) / mid) * 2; + } + if (theta > 2 * M_PI) + { + l = mid; + } + else + { + r = mid; + } + } + double area = 0.0f, R = mid; + for (int i = 0; i < n; i++) + { + area += heron(R, R, A[i]); + } + if (!ok) + { + area = 0.0f; + } + printf("%.3lf\n", area); + } + return 0; +} diff --git a/11278.cpp b/11278.cpp new file mode 100644 index 0000000..c590d85 --- /dev/null +++ b/11278.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + string qwerty = " `1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?"; + string dvorak = " `123qjlmfp/[]456.orsuyb;=\\789aehtdck-0zx,inwvg'~!@#QJLMFP?{}$%^>ORSUYB:+|&*(AEHTDCK_)ZX + +using namespace std; + +const char *DV[] = {"`123qjlmfp/[]", "456.orsuyb;=\\", "789aehtdck-", "0zx,inwvg'"}, + *qwerty[] = {"`1234567890-=", "qwertyuiop[]\\", "asdfghjkl;'", "zxcvbnm,./"}, + *shift = "~!@#$%^&*()_+{}|:\"<>?", *unshift = "`1234567890-=[]\\;',./"; +const int DVdist[] = {5, 4, 4, 4}, ONEdist[] = {4, 3, 3, 3}, TWOdist[] = {1, 0, 0, 0}; + +double one(char c) +{ + int r, p; + + for (r = 0; !strchr(qwerty[r], c); ++r) + ; + p = strchr(qwerty[r], c) - qwerty[r]; + return hypot(r - 2, p < ONEdist[r] ? ONEdist[r] - p : p >= ONEdist[r] + 4 ? p - ONEdist[r] - 3 : 0); +} + +double dvo(char c) +{ + int r, p; + + for (r = 0; !strchr(DV[r], c); ++r) + ; + p = strchr(DV[r], c) - DV[r]; + return hypot(r - 2, p < DVdist[r] ? DVdist[r] - p : p >= DVdist[r] + 4 ? p - DVdist[r] - 3 : 0); +} + +double two(char c) +{ + int r, p; + + for (r = 0; !strchr(qwerty[r], c); ++r) + ; + p = strchr(qwerty[r], c) - qwerty[r]; + if (p < TWOdist[r]) + p = TWOdist[r] - p; + else if (p < TWOdist[r] + 4) + p = 0; + else if (p < TWOdist[r] + 6) + p = 1; + else if (p < TWOdist[r] + 10) + p = 0; + else + p -= 9 + TWOdist[r]; + return hypot(r - 2, p); +} + +int main() +{ + char s[1001]; + + while (gets(s)) + { + double O = 0, T = 0, D = 0; + + for (int i = 0; s[i]; ++i) + { + if (isspace(s[i])) + continue; + if (isupper(s[i])) + s[i] = tolower(s[i]); + else if (strchr(shift, s[i])) + s[i] = unshift[strchr(shift, s[i]) - shift]; + + O += one(s[i]); + T += two(s[i]); + D += dvo(s[i]); + } + printf("%.2f %.2f %.2f\n", T * 2, O * 2, D * 2); + } +} diff --git a/11280.cpp b/11280.cpp new file mode 100644 index 0000000..41c3f6a --- /dev/null +++ b/11280.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +const int MAX = 1005, INF = 1 << 30; + +struct State{int dis, num;}; +State q[MAX * MAX], temp, out; + +bool inq[MAX][MAX]; +int n, m, u[MAX * 10], v[MAX * 10], w[MAX * 10], d[MAX][MAX], fst[MAX], nxt[MAX * 10]; +map mymap; + +void bellman_ford(const int &len) +{ + memset(inq, 0, sizeof(inq)); + int front = 1, rear = 1; + int U, V, W, minVal = INF, + sta = 0, end = n - 1, dis; + for (int i = 0; i < n; i++) + for (int j = 0; j <= len; j++) + { + if (!i) + { + d[i][j] = 0; + } + else + { + d[i][j] = INF; + } + } + temp.num = sta; + temp.dis = 0; + inq[temp.num][temp.dis] = 1; + q[rear++] = temp; + while (front < rear) + { + out = q[front++]; + U = out.num; + dis = out.dis; + for (int e = fst[U]; e != -1; e = nxt[e]) + { + V = v[e]; + W = w[e]; + if (d[V][dis + 1] > d[U][dis] + W && dis + 1 <= len) + { + d[V][dis + 1] = d[U][dis] + W; + if (!inq[V][dis + 1]) + { + inq[V][dis + 1] = 1; + temp.num = V; + temp.dis = dis + 1; + q[rear++] = temp; + } + } + } + } + for (int i = 0; i <= len; i++) + { + if (d[end][i] < minVal) + { + minVal = d[end][i]; + } + } + if (minVal != INF) + { + cout << "Total cost of flight(s) is $" << minVal << endl; + } + else + { + cout << "No satisfactory flights" << endl; + } +} +int main() +{ + int nCase, t, num = 0, Q; + string s, su, sv; + cin >> nCase; + while (nCase--) + { + memset(fst, -1, sizeof(fst)); + if (num++) + { + cout << endl; + } + cout << "Scenario #" << num << endl; + mymap.clear(); + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> s; + mymap[s] = i; + } + cin >> m; + for (int i = 0; i < m; i++) + { + cin >> su >> sv; + u[i] = mymap[su]; + v[i] = mymap[sv]; + cin >> w[i]; + nxt[i] = fst[u[i]]; + fst[u[i]] = i; + } + cin >> Q; + while (Q--) + { + cin >> t; + if (t < 0) + { + t = 0; + } + if (t > n) + { + t = n; + } + bellman_ford(t + 1); + } + } + return 0; +} diff --git a/11281.cpp b/11281.cpp new file mode 100644 index 0000000..2790984 --- /dev/null +++ b/11281.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define eps 1e-6 + +int compare(const void *a, const void *b) +{ + double *x, *y; + x = (double *)a; + y = (double *)b; + if (x - y >= eps) + return -1; + if (y - x >= eps) + return 1; + return 0; +} + +bool valid(double a, double b, double c, double d) +{ + double theta, alpha, eta, xx; + if (a - d > eps) + return false; + theta = acos((a * a + b * b - c * c) / (2 * a * b)); + alpha = acos(a / d);// since D[j] = 2*r + eta = theta - alpha; + xx = d * cos(eta); + return (xx - b > -eps); +} + +int main() +{ + double D[120], ara[10], theta, alpha, eta, xx, a, b, c; + int i, j, m, n, count, res[120]; + while (scanf("%d", &n) == 1) + { + for (i = 1; i <= n; i++) + scanf("%lf", &D[i]); + scanf("%d", &m); + for (i = 0; i < m; i++) + { + scanf("%lf%lf%lf", &ara[0], &ara[1], &ara[2]); + qsort(ara, 3, sizeof(double), compare); + a = ara[0]; + b = ara[1]; + c = ara[2]; + for (j = 1, count = 0; j <= n; j++) + { + if (valid(a, b, c, D[j]) || valid(b, c, a, D[j]) || valid(c, a, b, D[j])) + res[count++] = j; + } + if (count == 0) + printf("Peg %c will not fit into any holes\n", i + 65); + else + { + printf("Peg %c will fit into hole(s):", i + 65); + for (j = 0; j < count; j++) + printf(" %d", res[j]); + printf("\n"); + } + } + } + return 0; +} diff --git a/11282.cpp b/11282.cpp new file mode 100644 index 0000000..ac404bb --- /dev/null +++ b/11282.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +/** + f(n, k) = # of permutations of n elements, st. exactly k of them + are in their correct position, eg. f(3, 1) = 3 + which are {1,3,2}, {3,2,1} and {2,1,3}. + Then ans = sum for every 0≤k≤m of f(n, k). + + D(n) = Numbers of arrangenents of n elements + for every n we have f(n, 0) = D(n) + in general, f(n, k) = choose(n, k) * D(n - k) + + The idea behind this is that we fix k elements from the whole + sequence of n elements in choose(n, k) ways, and for each possibility, + we rearrange completely the missing n-k elements in D(n-k) ways. + + D(i) is computed using a recursive function, although this could also + be found using inclusion-exclusion principle. +*/ + +const int N = 20; + +long long choose[N + 1][N + 1], d[N + 1];//d[i] = # of arrangements for i elements + +int main() +{ + // Binomial coefficients + for (int i = 0; i <= N; ++i) + choose[i][0] = choose[i][i] = 1; + for (int i = 1; i <= N; ++i) + for (int j = 1; j < i; ++j) + choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j]; + + // Arrangements + d[0] = 1, d[1] = 0; + for (int i = 2; i <= N; ++i) + d[i] = (i - 1) * (d[i - 2] + d[i - 1]); + + int n, m; + while (cin >> n >> m) + { + long long ans = 0; + for (int k = 0; k <= m; ++k) + ans += choose[n][k] * d[n - k]; + cout << ans << endl; + } + return 0; +} diff --git a/11283.cpp b/11283.cpp new file mode 100644 index 0000000..4b4662f --- /dev/null +++ b/11283.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +char grid[4][4]; + +int solve(int row, int col, string &input, int pos) +{ + if (pos == input.size()) + { + return pos; + } + int diff[9][2] = {{0, 0}, {-1, 0}, {0, -1}, {-1, -1}, {1, 0}, {0, 1}, {1, 1}, {-1, 1}, {1, -1}}; + rep(i, 9) + { + if (row + diff[i][0] >= 0 && row + diff[i][0] < 4 && col + diff[i][1] >= 0 && col + diff[i][1] < 4 && grid[row + diff[i][0]][col + diff[i][1]] == input[pos]) + { + grid[row + diff[i][0]][col + diff[i][1]] = '-'; + int result = solve(row + diff[i][0], col + diff[i][1], input, pos + 1); + grid[row + diff[i][0]][col + diff[i][1]] = input[pos]; + if (result > 0) + { + return result; + } + } + } + return 0; +} + +int find_word(string &input) +{ + int result = 0; + rep(i, 4) + { + rep(j, 4) + { + if (grid[i][j] != input[0]) + { + continue; + } + result = solve(i, j, input, 0); + if (result > 0) + { + i = 5; + break; + } + } + } + return result; +} + +int main(void) +{ + int n, gameNumber = 0, m; + string input; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + gameNumber++; + getline(cin, input);// empty line before the grid + rep(i, 4) + { + getline(cin, input); + rep(j, input.size()) + { + grid[i][j] = input[j]; + } + } + cin >> m; + cin.ignore(100, '\n'); + int result = 0; + while (m--) + { + getline(cin, input); + int found = find_word(input); + if (found == 3 || found == 4) + { + result += 1; + } + else if (found == 5) + { + result += 2; + } + else if (found == 6) + { + result += 3; + } + else if (found == 7) + { + result += 5; + } + else if (found > 7) + { + result += 11; + } + } + cout << "Score for Boggle game #" << gameNumber << ": " << result << endl; + } + return 0; +} diff --git a/11284.cpp b/11284.cpp new file mode 100644 index 0000000..27ab906 --- /dev/null +++ b/11284.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define MAXSTATE 1 << 13 +#define MAXN 13 +#define oo 0xfffffffffLL + +int A[MAXN], AD[MAXN], best; +long long DP[MAXSTATE][MAXN], map_[51][51]; + +int TSP(int state, int n, int last) +{ + if (state == 0) + { + return 0; + } + if (DP[state][last] != -oo) + { + return DP[state][last]; + } + int i; + long long max = -oo, tmp; + for (i = 0; i <= n; i++) + { + if ((state & (1 << i)) != 0 && last != i) + { + tmp = TSP(state - (1 << last), n, i); + tmp -= map_[A[i]][A[last]]; + tmp += AD[last]; + if (max < tmp) + { + max = tmp; + } + } + } + if (state == (1 << last)) + { + max = -map_[A[last]][0] + AD[last]; + } + if (best < max - map_[A[last]][0]) + { + best = max - map_[A[last]][0]; + } + DP[state][last] = max; + return DP[state][last]; +} +long long min(long long a, long long b) +{ + return a < b ? a : b; +} + +int main() +{ + int T, x, y, n, m, p, i, j, k, a, b; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &n, &m); + for (i = 0; i <= n; i++) + { + for (j = 0; j <= n; j++) + { + map_[i][j] = oo; + } + } + for (i = 0; i < m; i++) + { + scanf("%d %d %d.%d", &x, &y, &a, &b); + map_[x][y] = min(100 * a + b, map_[x][y]); + map_[y][x] = min(100 * a + b, map_[y][x]); + } + scanf("%d", &p); + int newp, tmpAD[51]; + memset(tmpAD, 0, sizeof(tmpAD)); + for (i = 1; i <= p; i++) + { + scanf("%d %d.%d", &A[i], &a, &b); + tmpAD[A[i]] += 100 * a + b; + } + newp = 0; + for (i = 1; i <= n; i++) + { + if (tmpAD[i]) + { + newp++, A[newp] = i, AD[newp] = tmpAD[i]; + } + } + p = newp; + for (k = 0; k <= n; k++) + { + for (i = 0; i <= n; i++) + { + for (j = 0; j <= n; j++) + { + if (map_[i][j] > map_[i][k] + map_[k][j]) + { + map_[i][j] = map_[i][k] + map_[k][j]; + } + } + } + } + int final = (1 << (p + 1)) - 1; + for (i = 0; i <= final; i++) + { + for (j = 0; j <= p; j++) + { + DP[i][j] = -oo; + } + } + map_[0][0] = 0; + best = 0; + TSP(final, p, 0); + if (best == 0) + { + puts("Don't leave the house"); + } + else + { + printf("Daniel can save $%d.%02d\n", best / 100, best % 100); + } + } + return 0; +} diff --git a/11285.cpp b/11285.cpp new file mode 100644 index 0000000..daf56a2 --- /dev/null +++ b/11285.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define ROUND 0 +int cad[10000]; +int usd[10000]; + +double rate[10000]; + +int initcad = 100000; +int initusd = 0; +double commission = .03; + +double max(double a, double b, double c) +{ + if (a >= b && a >= c) + { + return a; + } + if (b >= c) + { + return b; + } + return c; +} + +int main() +{ + int n, i; + while (1) + { + scanf("%d", &n); + if (!n) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%lf", rate + i); + } + for (i = 0; i < n; i++) + { + cad[i + 1] = max(initcad + initusd * rate[i] * (1 - commission) + ROUND, + cad[i], usd[i] * rate[i] * (1 - commission) + ROUND); + usd[i + 1] = max(initusd + initcad / rate[i] * (1 - commission) + ROUND, + usd[i], cad[i] / rate[i] * (1 - commission) + ROUND); + //printf("%d %d\n", usd[i+1], cad[i+1]); + } + printf("%01d.%02d\n", cad[n] / 100, cad[n] % 100); + } +} diff --git a/11286.cpp b/11286.cpp index 37a08e0..0e51e67 100644 --- a/11286.cpp +++ b/11286.cpp @@ -1,60 +1,73 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second > b.second; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ - int N, i; - cin >> N; - if (N == 0) +int main() +{ + int n; + int courses[5]; + while (cin >> n) + { + if (!n) + { break; - map, int> Map; - map, int>::iterator it; - int maxm = 1; - while (N--){ - vector V(5); - FOI(i, 0, 4) - cin >> V[i]; - sort(V.begin(), V.end()); - Map[V] += 1; - maxm = max(maxm, Map[V]); } - int tot = 0; - for (it = Map.begin(); it != Map.end(); it++){ - if (maxm == (*it).second) - tot += (*it).second; + int result = 0; + map m; + vector> v; + while (n--) + { + for (int i = 0; i < 5; i++) + { + cin >> courses[i]; + } + sort(courses, courses + 5); + stringstream ss; + for (int i = 0; i < 5; i++) + { + ss << courses[i]; + } + string s = ss.str(); + if (m.find(s) == m.end()) + { + m[s] = 1; + v.push_back(make_pair(s, 0)); + } + else + { + m[s] += 1; + } + } + for (int i = 0, sz = v.size(); i < sz; i++) + { + v[i].second = m[v[i].first]; + } + sort(v.begin(), v.end(), theTruthIsOutThere); + if (v.size() == 1) + { + result = v[0].second; } - - cout << tot << endl; + else if (v[0].second == v[1].second) + { + for (int i = 0, sz = v.size(); i < sz; i++) + { + if (v[i].second < v[0].second) + { + break; + } + result += v[i].second; + } + } + else + { + result = v[0].second; + } + cout << result << endl; } return 0; } - diff --git a/11287.cpp b/11287.cpp index 99a3530..119202f 100644 --- a/11287.cpp +++ b/11287.cpp @@ -1,116 +1,59 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) -#define MAX 32000 - -bool prime[MAX + 1]; -vector< int64 > Prime; -int64 SZ; - -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; - int64 i, j; - FOI(i, 2, MAX){ - if (prime[i]){ - Prime.push_back(i); - for(j = 2 * i; j <= MAX; j += i) - prime[j] = false; - } - } - SZ = Prime.size() - 1; -} - -int64 Bigmod(int64 a, int64 b, int64 n){ - int64 c=0,d=1,k=0; - int64 arr[100]; - while(b){ - arr[k++]=b%2; - b/=2; - } - while(k--){ - c=2*c; - d=(d*d)%n; - if(arr[k]==1){ - c=c+1; - d=(d*a)%n; - } - } - return d; -} - -int64 modPow(int64 A, int64 P, int64 M){ - if (P == 0) - return 1; - if( P == 1 ) - return A%M; - int64 V = modPow(A, P/2, M); - V = (V%M * V%M) %M; - if( P % 2 == 1 ) - V = (V%M * A%M) %M; - return V%M; -} - -bool checkPrime(int64 P){ - int64 S = sqrt(P + 0.5); - int64 i, j; - FOI(i, 2, S) - if (P % i == 0) - return true; - return false; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - //seive(); - while (true){ - int64 P, A; - cin >> P >> A; - if (P == 0 && A == 0) - break; - bool stat = checkPrime(P); - int64 V = modPow(A, P, P); - if (stat && V == A) - cout << "yes"; - else - cout << "no"; - cout << endl; - } - return 0; -} - +#include + +using namespace std; + +bool is_prime(unsigned long long n) +{ + if (n == 2) + { + return true; + } + if (n % 2 == 0) + { + return false; + } + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + if (n % i == 0) + { + return false; + } + return true; +} + +// a ^ k mod n +unsigned int calc(unsigned long long a, long long k, long long n) +{ + if (k == 1) + { + return a; + } + if (k % 2 == 0) + { + return calc((a * a) % n, k / 2, n); + } + else + { + return (a * calc((a * a) % n, k / 2, n)) % n; + } +} + +int main() +{ + unsigned long long p, a; + while (cin >> p >> a) + { + if (p == 0 && a == 0) + { + break; + } + if ((calc(a, p, p) == a) && !is_prime(p)) + { + cout << "yes" << endl; + } + else + { + cout << "no" << endl; + } + } + return 0; +} diff --git a/11288.cpp b/11288.cpp new file mode 100644 index 0000000..c9676bc --- /dev/null +++ b/11288.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +static int a, b, c, d, e, i, j, k, m, n, g[17][17], x[32768], y[32768], z[32768]; + +int main() +{ + scanf("%d %d", &n, &m); + for (i = n + 2; i--;) + for (j = n + 2; j--;) + { + g[i][j] = i == j ? 0 : 0x3F3F3F3F; + } + for (i = 1 << n; i--;) + { + x[i] = y[i] = z[i] = 0x3F3F3F3F; + } + while (m--) + { + scanf("%d %d %d", &i, &j, &k), g[j][i] = g[i][j] = MIN(g[i][j], k); + } + for (k = n + 2; k--;) + for (i = n + 2; i--;) + for (j = n + 2; j--;) + { + g[i][j] = MIN(g[i][j], g[i][k] + g[k][j]); + } + for (a = 0; a++ < n;) + for (b = 0; b++ < n;) + for (c = 0; c++ < n;) + for (d = 0; d++ < n;) + for (e = 0; e++ < n;) + x[((1 << a) | (1 << b) | (1 << c) | (1 << d) | (1 << e)) >> 1] = MIN(x[((1 << a) | (1 << b) | (1 << c) | (1 << d) | (1 << e)) >> 1], + g[0][a] + g[a][b] + g[b][c] + g[c][d] + g[d][e] + g[e][n + 1] + + 5 * (1 + (a != b) + (b != c) + (c != d) + (d != e))); + y[0] = 0; + for (i = 0; i < (n + 4) / 5; ++i) + { + for (j = 0; j < (1 << n); ++j) + if (x[j] < 0x3F3F3F3F) + for (k = (1 << n) - 1; (k &= ~j) >= 0; --k) + { + z[j | k] = MIN(z[j | k], MAX(y[k], x[j])); + } + memcpy(y, z, sizeof(y)); + } + printf("%d\n", y[(1 << n) - 1]); + return 0; +} diff --git a/11290.cpp b/11290.cpp new file mode 100644 index 0000000..7017be6 --- /dev/null +++ b/11290.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int nseq[20]; + +string mth_seq(int n, int m) +{ + if (n == 0) + { + return ""; + } + for (int j = 0; j < n; j++) + if (nseq[n - j - 1] * nseq[j] > m) + { + return "E" + mth_seq(n - j - 1, m / nseq[j]) + "S" + mth_seq(j, m % nseq[j]); + } + else + { + m -= nseq[n - j - 1] * nseq[j]; + } +} + +int main() +{ + nseq[0] = 1; + for (int i = 1; i <= 19; i++) + { + for (int j = 0; j < i; j++) + { + nseq[i] += nseq[i - j - 1] * nseq[j]; + } + } + int N, M; + while ((cin >> N && N) && (cin >> M && M)) + { + if (M - 1 >= nseq[N - 1]) + { + cout << "ERROR" << endl; + } + else + { + cout << mth_seq(N - 1, M - 1) << endl; + } + } +} diff --git a/11291.cpp b/11291.cpp new file mode 100644 index 0000000..642fd1e --- /dev/null +++ b/11291.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +double calc(char *begin) +{ + if (isdigit(*begin) || *begin == '.' || *begin == '-') + { + return atof(begin); + } + else if (*begin == '(') + { + double rate, p1, p2, paren = 1; + char *first = strchr(begin, ' ') + 1, *second; + if (isdigit(*first) || *first == '.' || *first == '-') + { + second = strchr(first, ' '); + } + else + { + for (second = first + 1; paren != 0; ++second) + { + if (*second == '(') + { + ++paren; + } + else if (*second == ')') + { + --paren; + } + } + } + ++second; + rate = atof(begin + 1); + p1 = calc(first); + p2 = calc(second); + return rate * (p1 + p2) + (1 - rate) * (p1 - p2); + } +} + +int main() +{ + char *expr; + string tmp; + + while (getline(cin, tmp) && tmp != "()") + { + expr = strdup(tmp.c_str()); + printf("%.2f\n", calc(expr)); + free(expr); + } +} diff --git a/11292.cpp b/11292.cpp index a761f4e..c3ff2f5 100644 --- a/11292.cpp +++ b/11292.cpp @@ -1,76 +1,62 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - int N, M; - cin >> N >> M; - if( N == 0 && M == 0 ) - break; - int head[N], nite[M]; - int i, j; - FOI(i, 0, N-1) - cin >> head[i]; - FOI(i, 0, M-1) - cin >> nite[i]; - sort(nite, nite + M); - bool flag = true; - int cost = 0; - FOI(i, 0, N-1){ - FOI(j, 0, M-1){ - if( nite[j] >= head[i] ){ - cost += nite[j]; - nite[j] = -1; - break; - } - } - if( j == M ){ - flag = false; - break; - } - } - if( !flag ) - cout << "Loowater is doomed!" << endl; - else - cout << cost << endl; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +int main() +{ + int n, m; + int heads[20000]; + int knights[20000]; + while (cin >> n >> m) + { + if (n == 0 && m == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + cin >> heads[i]; + } + for (int i = 0; i < m; i++) + { + cin >> knights[i]; + } + if (m < n) + { + cout << "Loowater is doomed!" << endl; + continue; + } + sort(heads, heads + n); + sort(knights, knights + m); + int result = 0; + int cur = 0; + for (int i = 0; i < n; i++) + { + if (n - i > m - cur) + { + result = -1; + break; + } + for (int j = cur; j < m; j++) + { + if (knights[j] < heads[i]) + { + cur++; + continue; + } + result += knights[j]; + cur++; + break; + } + } + if (result == -1) + { + cout << "Loowater is doomed!" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/11294.cpp b/11294.cpp new file mode 100644 index 0000000..d60963f --- /dev/null +++ b/11294.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define N 1010 +#define CLR(a) memset((a), 0, sizeof((a))) + +inline int abs(int a) +{ + return (a < 0 ? -a : a); +} +vector order, edge[2 * N], edge2[2 * N]; +int comp[2 * N]; +bool used[2 * N]; +char path[2 * N]; + +void reset(int n, int m) +{ + order.clear(); + int i; + CLR(used); + for (i = 0; i < (n + 2); i++) + { + edge[i].clear(); + edge2[i].clear(); + path[i] = 0; + } + memset(comp, -1, sizeof(comp)); +} + +void dfs1(int v) +{ + used[v] = true; + int i, k, sz = edge[v].size(); + for (i = 0; i < sz; i++) + { + k = edge[v][i]; + if (!used[k]) + dfs1(k); + } + order.push_back(v); +} +void dfs2(int v, int id) +{ + comp[v] = id; + int i, k, sz = edge2[v].size(); + for (i = 0; i < sz; i++) + { + k = edge2[v][i]; + if (comp[k] == -1) + dfs2(k, id); + } +} +void insert(int x, int y) +{ + edge[x ^ 1].push_back(y); + edge[y ^ 1].push_back(x); + edge2[y].push_back(x ^ 1); + edge2[x].push_back(y ^ 1); +} + +int main() +{ + int i, j, n, m, k, a, b; + char x, y; + while (scanf("%d%d", &n, &m)) + { + if (!n && !m) + break; + reset(2 * n, m); + edge[1].push_back(0); + edge2[0].push_back(1); + for (i = 0; i < m; i++) + { + cin >> a >> x >> b >> y; + a = 2 * a; + b = 2 * b; + if (x == 'h') + a ^= 1; + if (y == 'h') + b ^= 1; + insert(a, b); + } + m = n; + n *= 2; + for (i = 0; i < n; i++) + if (!used[i]) + dfs1(i); + for (i = 0, k = 0; i < n; i++) + { + j = order[n - i - 1]; + if (comp[j] == -1) + dfs2(j, ++k); + } + for (i = 0; i < n; i++) + if (comp[i] == comp[i ^ 1]) + break; + if (i != n) + printf("bad luck\n"); + else + { + for (i = 0; i < n; i += 2) + { + j = comp[i] > comp[i ^ 1] ? i : i ^ 1; + if (j & 1) + path[i / 2] = 'h'; + else + path[i / 2] = 'w'; + } + for (i = 2; i < n; i += 2) + { + if (i > 2) + printf(" "); + printf("%d%c", i / 2, path[i / 2]); + } + printf("\n"); + } + } + return 0; +} diff --git a/11296.cpp b/11296.cpp new file mode 100644 index 0000000..553f29d --- /dev/null +++ b/11296.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main() +{ + for (long long n; scanf("%lld", &n) == 1; n = (n + 2) >> 1, printf("%lld\n", ((n + 1) * n) >> 1)) + ; + return 0; +} diff --git a/112972.cpp b/112972.cpp new file mode 100644 index 0000000..ea234a6 --- /dev/null +++ b/112972.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +const int INF = 0x7fffffff; +const int MAXN = 2010; + +struct IntervalTree2D +{ + int Max[MAXN][MAXN], Min[MAXN][MAXN], n, m; + int xo, xleaf, x1, y1, x2, y2, x, y, v, vmax, vmin; + + void query1D(int o, int L, int R) + { + if (y1 <= L && R <= y2) + { + vmax = max(vmax, Max[xo][o]); + vmin = min(vmin, Min[xo][o]); + } + else + { + int M = (L + R) >> 1; + if (y1 <= M) + { + query1D(o * 2, L, M); + } + if (M < y2) + { + query1D(o * 2 + 1, M + 1, R); + } + } + } + + void query2D(int o, int L, int R) + { + if (x1 <= L && R <= x2) + { + xo = o; + query1D(1, 1, m); + } + else + { + int M = (L + R) >> 1; + if (x1 <= M) + { + query2D(o * 2, L, M); + } + if (M < x2) + { + query2D(o * 2 + 1, M + 1, R); + } + } + } + + void modify1D(int o, int L, int R) + { + if (L == R) + { + if (xleaf) + { + Max[xo][o] = Min[xo][o] = v; + return; + } + Max[xo][o] = max(Max[xo * 2][o], Max[xo * 2 + 1][o]); + Min[xo][o] = min(Min[xo * 2][o], Min[xo * 2 + 1][o]); + } + else + { + int M = (L + R) >> 1; + if (y <= M) + { + modify1D(o * 2, L, M); + } + else + { + modify1D(o * 2 + 1, M + 1, R); + } + Max[xo][o] = max(Max[xo][o * 2], Max[xo][o * 2 + 1]); + Min[xo][o] = min(Min[xo][o * 2], Min[xo][o * 2 + 1]); + } + } + + void modify2D(int o, int L, int R) + { + if (L == R) + { + xo = o; + xleaf = 1; + modify1D(1, 1, m); + } + else + { + int M = (L + R) / 2; + if (x <= M) + { + modify2D(o * 2, L, M); + } + else + { + modify2D(o * 2 + 1, M + 1, R); + } + xo = o; + xleaf = 0; + modify1D(1, 1, m); + } + } + + void query() + { + vmax = -INF; + vmin = INF; + query2D(1, 1, n); + } + + void modify() + { + modify2D(1, 1, n); + } +} t; + +int main() +{ + int n, Q; + char op[10]; + scanf("%d", &n); + t.n = n; + t.m = n; + for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= n; ++j) + { + scanf("%d", &t.v); + t.x = i, t.y = j; + t.modify(); + } + } + scanf("%d", &Q); + while (Q--) + { + scanf("%s", op); + if (*op == 'q') + { + scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2); + t.query(); + printf("%d %d\n", t.vmax, t.vmin); + } + else + { + scanf("%d%d%d", &t.x, &t.y, &t.v); + t.modify(); + } + } + return 0; +} diff --git a/11298.cpp b/11298.cpp new file mode 100644 index 0000000..619cbb2 --- /dev/null +++ b/11298.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + if ((n > 2) && (n % 3 == 0)) + { + printf("1\n"); + } + else + { + printf("0\n"); + } + } + return 0; +} diff --git a/113.cpp b/113.cpp index 22d0808..d558afe 100644 --- a/113.cpp +++ b/113.cpp @@ -1,12 +1,14 @@ -#include -#include -using namespace std; - -int main(){ - long long n=0,p=0,k=0; - while(cin>>n>>p){ - k=exp(log(p)/n); - cout<<(long long int)k< + +using namespace std; + +int main() +{ + int n; + double p; + while (scanf("%d%lf", &n, &p) != EOF) + { + printf("%d\n", (int)floor(pow(p, (1.0 / (double)n)) + 0.1)); + } + return 0; +} diff --git a/11300.cpp b/11300.cpp new file mode 100644 index 0000000..67f11f1 --- /dev/null +++ b/11300.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAXN 1000001 +#define u64 unsigned long long +#define i64 long long +#define rep(i, n) for (i = 0; i < (n); i++) +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#define mabs(x) (((x) < 0) ? (-x) : (x)) + +int n; +u64 a[MAXN]; +i64 A[MAXN]; +u64 sum, per; + +int get_random(int a, int b)//returns a random number between a & b +{ + int x = rand(); + int y = b - a; + x = (int)((x / (double)RAND_MAX) * y) + a; + if (x < a) + { + x = a; + } + if (x > b) + { + x = b; + } + return x; +} + +// partition the array A[p], A[p+1] ... A[r] in such a way so that +// all the elements left to the pivot element are smaller than it and all the elements right to it are larger than it +// return the pivot element's index +int partition(int p, int r) +{ + int x = A[r]; + int i, j; + i = p - 1; + for (j = p; j < r; j++) + { + if (A[j] <= x) + { + i++; + swap(A[i], A[j]); + } + } + swap(A[i + 1], A[r]); + return i + 1; +} + +// same functionality as partition(), but selects the pivot element randomly so that no particular input can always expose the O(n^2) worst case behaviour +int randomized_partition(int p, int r) +{ + int i = get_random(p, r); + swap(A[r], A[i]); + return partition(p, r); +} + +// select the i-th element largest from the array A[p], A[p+1] ... A[r] +i64 randomized_select(int p, int r, int i) +{ + if (p == r) + { + return A[p]; + } + int q = randomized_partition(p, r); + int k = q - p + 1; + if (i == k) + { + return A[q]; + } + else if (i < k) + { + return randomized_select(p, q - 1, i); + } + else + { + return randomized_select(q + 1, r, i - k); + } +} + +int main() +{ + int i; + u64 res; + i64 mid; + srand(time(NULL)); + while (scanf(" %d", &n) == 1) + { + sum = 0; + rep(i, n) + { + scanf(" %llu", &a[i]); + sum += a[i]; + } + per = sum / n; + rep(i, n) A[i] = (i64)per - (i64)a[i]; + for (i = n - 2; i > 0; i--) + { + A[i] = A[i + 1] + A[i]; + } + A[0] = 0; + //sort(A, A+n); + //mid = ( A[n/2] + A[(n-1)/2] ) / 2; + mid = randomized_select(0, n - 1, n / 2 + 1); + mid += randomized_select(0, n - 1, (n - 1) / 2 + 1); + mid /= 2; + res = 0; + rep(i, n) + { + A[i] -= mid; + res += mabs(A[i]); + } + printf("%llu\n", res); + } + return 0; +} diff --git a/11301.cpp b/11301.cpp new file mode 100644 index 0000000..69bd63b --- /dev/null +++ b/11301.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +const int N = 1000; +const int inf = (1 << 29); + +class state +{ +public: + int now, bit, vis; + int cost; + bool operator<(const state &a) const + { + return cost > a.cost; + } +}; + +string m[5]; + +int d[2] = {-1, 1}; + +int dijkstra(int n, int ini) +{ + static int cost[N][(1 << 5)][(1 << 5)]; + rep(i, n) rep(j, (1 << 5)) rep(k, (1 << 5)) cost[i][j][k] = inf; + priority_queue Q; + Q.push((state){ + 0, ini, ini, 0}); + while (!Q.empty()) + { + state now = Q.top(); + Q.pop(); + state next; + if (cost[now.now][now.bit][now.vis] != inf) + { + continue; + } + cost[now.now][now.bit][now.vis] = now.cost; + if (now.now == n - 1) + { + return now.cost; + } + rep(i, 5) + { + if ((now.bit & (1 << i)) == 0) + { + continue; + } + rep(j, 2) + { + int nec = i + d[j]; + if (nec == -1 || nec == 5 || (now.vis & (1 << nec)) != 0) + { + continue; + } + next = now; + next.bit += (1 << nec); + next.bit -= (1 << i); + next.vis += (1 << nec); + next.cost += m[nec][next.now] - '0'; + Q.push(next); + } + } + //move all + next = now; + rep(i, 5) + { + if ((next.bit & (1 << i)) != 0) + { + next.cost += m[i][next.now + 1] - '0'; + } + } + next.vis = next.bit; + next.now++; + Q.push(next); + } + return -1; +} + +main() +{ + int n; + while (cin >> n && n) + { + rep(i, 5) cin >> m[i]; + int ini = 0; + int cnt = 0; + rep(i, 5) + { + if (m[i][0] == '@') + { + ini |= (1 << i); + m[i][0] = 0; + cnt++; + } + } + cout << dijkstra(n, ini) << endl; + } +} diff --git a/11307.cpp b/11307.cpp new file mode 100644 index 0000000..2a67daa --- /dev/null +++ b/11307.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define mp make_pair + +const int N = 10000; +const int inf = (1 << 21); +const int M = 20; + +vector edge[N]; +bool visited[N]; +int dp[N][M]; + +void dfs(int n, int now) +{ + visited[now] = true; + vector child; + rep(i, edge[now].size()) + { + if (visited[edge[now][i]]) + { + continue; + } + dfs(n, edge[now][i]); + child.push_back(edge[now][i]); + } + sort(ALL(child)); + REP(i, 1, M) + { + int tmp = i; + rep(j, child.size()) + { + int tmp2 = inf; + rep(k, M) + { + if (i == k) + { + continue; + } + tmp2 = min(tmp2, dp[child[j]][k]); + } + tmp += tmp2; + } + dp[now][i] = tmp; + } + return; +} + +int solve(int n) +{ + rep(i, n) rep(j, M) dp[i][j] = inf; + dfs(n, 0); + int ret = inf; + rep(i, M) ret = min(ret, dp[0][i]); + return ret; +} + +main() +{ + int n; + while (cin >> n && n) + { + rep(i, n) edge[i].clear(), visited[i] = false; + rep(i, n) + { + int now, t; + char dummy; + cin >> now >> dummy; + string tmp; + getline(cin, tmp); + stringstream sin(tmp); + while (sin >> t) + { + edge[now].push_back(t), edge[t].push_back(now); + } + } + cout << solve(n) << endl; + } +} diff --git a/11308.cpp b/11308.cpp index 4b48d59..128f0b1 100644 --- a/11308.cpp +++ b/11308.cpp @@ -1,97 +1,65 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -map Ing; -map::iterator iit; -map Cost; -map::iterator cit; -vector< string > Ans; - -bool comp(string A, string B){ - if (Cost[A] != Cost[B]) - return Cost[A] < Cost[B]; - return A < B; -} +using namespace std; -string Upper(string S){ - int L = S.length() - 1, i; - string R = ""; - FOI(i, 0, L) R += toupper(S[i]); - return R; +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second == b.second ? a.first < b.first : a.second < b.second; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - string S; - cin >> T; - getline(cin, S); - while (T--){ - Ing.clear(); Cost.clear(); Ans.clear(); - int i, j, k; - string binder; - getline(cin, binder); - cout << Upper(binder) << endl; - int M, N, B; - cin >> M >> N >> B; - getline(cin, S); - int I, K; - while (M--){ - cin >> S >> I; - Ing[S] = I; - getline(cin, S); +int main() +{ + int t; + int m, n, b; + int cost; + int k; + int amount; + map ingredients; + string ingredient; + string binder_name; + string recipy_name; + vector> result; + cin >> t; + while (t--) + { + result.clear(); + ingredients.clear(); + cin.ignore(100, '\n'); + getline(cin, binder_name); + cin >> m >> n >> b; + for (int i = 0; i < m; i++) + { + cin >> ingredient >> cost; + ingredients[ingredient] = cost; } - string R; - while (N--){ - getline(cin, R); - Cost[R] = 0; - cin >> K; - getline(cin, S); - while (K--){ - cin >> S >> I; - Cost[R] += (I * Ing[S]); - getline(cin, S); + for (int i = 0; i < n; i++) + { + cin.ignore(100, '\n'); + getline(cin, recipy_name); + cin >> k; + cost = 0; + for (int j = 0; j < k; j++) + { + cin >> ingredient >> amount; + cost += ingredients[ingredient] * amount; } - if (Cost[R] < B) - Ans.push_back(R); + if (cost <= b) + { + result.push_back(make_pair(recipy_name, cost)); + } + } + sort(result.begin(), result.end(), theTruthIsOutThere); + transform(binder_name.begin(), binder_name.end(), binder_name.begin(), ::toupper); + cout << binder_name << endl; + for (int i = 0, sz = result.size(); i < sz; i++) + { + cout << result[i].first << endl; } - sort(Ans.begin(), Ans.end(), comp); - int SZ = Ans.size(); - if (SZ == 0) + if (result.size() == 0) + { cout << "Too expensive!" << endl; - else - FOI(i, 0, SZ-1) - cout << Ans[i] << endl; + } cout << endl; } return 0; } - diff --git a/11309.cpp b/11309.cpp index 51a56e8..7f5d9bc 100644 --- a/11309.cpp +++ b/11309.cpp @@ -1,33 +1,57 @@ -#include -using namespace std; -int rev(int a){ - int r; - while(a>0){ - r+=(a%10); - a/=10; - } - return r; -} -int main(){ - int test; - cin>>test; - int h,m; - while(test--){ - scanf("%d:%d",&h,&m); - if(h==0){ - if(m<9) - m+=1; - else{ - m+=1; - if(m%10 + +using namespace std; + +bool is_palindrome(int h, int m) +{ + stringstream ss; + if (h == 0) + { + if (m > 0) + { + ss << m; + } + } + else + { + ss << h; + if (m < 10) + { + ss << 0; + } + ss << m; + } + string a = ss.str(); + string b(a.rbegin(), a.rend()); + return a == b; +} + +int main() +{ + int n; + int h, m; + char c; + cin >> n; + while (n--) + { + cin >> h >> c >> m; + for (int i = h; i < 24; i++) + { + for (int j = m + 1; j < 60; j++) + { + if (is_palindrome(i, j)) + { + printf("%02d:%02d\n", i, j); + j = 60; + i = 30; + } + } + if (i == 23) + { + i = -1;// next H will be 0 + } + m = -1; + } + } + return 0; +} diff --git a/11310.cpp b/11310.cpp new file mode 100644 index 0000000..dc17d54 --- /dev/null +++ b/11310.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +#define N 45 +unsigned long long int res[45]; +int main() +{ + int i, c; + for (i = 0; i < 43; i++) + { + res[i] = 0; + } + res[0] = 1; + for (i = 0; i < 43; i++) + { + res[i + 1] += res[i]; + res[i + 2] += 4 * res[i]; + res[i + 3] += 2 * res[i]; + } + scanf("%d", &c); + while (c--) + { + scanf("%d", &i); + printf("%llu\n", res[i]); + } + return 0; +} diff --git a/11311.cpp b/11311.cpp new file mode 100644 index 0000000..79e0885 --- /dev/null +++ b/11311.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +/** + * UVa 11311 Exclusively Edible + * + * A nim-game played with 4 stacks, represented by the number of + * cells to the left, right, up, and down of the bad piece. + */ +int main() +{ + setvbuf(stdin, NULL, _IOFBF, 10000); + setvbuf(stdout, NULL, _IOFBF, 10000); + int n_cases; + scanf("%d", &n_cases); + for (int ctr = 0; ctr < n_cases; ++ctr) + { + int m, n, r, c; + scanf("%d %d %d %d", &m, &n, &r, &c); + int nimsum = r ^ c ^ (m - r - 1) ^ (n - c - 1); + if (!nimsum) + { + puts("Hansel"); + } + else + { + puts("Gretel"); + } + } + return 0; +} diff --git a/11313.cpp b/11313.cpp new file mode 100644 index 0000000..1972004 --- /dev/null +++ b/11313.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int t; + int n, m; + cin >> t; + while (t--) + { + cin >> n >> m; + if (m - 1 == 1) + { + cout << n / (m - 1) - 1 << endl; + } + else if (n % (m - 1) == 1) + { + cout << n / (m - 1) << endl; + } + else + { + cout << "cannot do this" << endl; + } + } + return 0; +} diff --git a/11314.cpp b/11314.cpp new file mode 100644 index 0000000..17a63d6 --- /dev/null +++ b/11314.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +void swap(double &a, double &b) +{ + double t = a; + a = b; + b = t; +} +double ax, ay, bx, by; +inline double func(double x, double y) +{ + return sqrt(ax * ax + (ay - y) * (ay - y)) + + sqrt((bx - x) * (bx - x) + by * by) + + sqrt(x * x + y * y) + + sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)); +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%lf %lf %lf %lf", &ax, &ay, &bx, &by); + if (ax > bx) + { + swap(ax, bx); + swap(ay, by); + } + double m = (by - ay) / (bx - ax); + double b = by - m * bx; + double t = -b / m; + double minx = 0, maxx = t; + while (maxx - minx > 1e-3) + { + double x1 = (minx * 2 + maxx) / 3; + double x2 = (minx + maxx * 2) / 3; + double miny = 0, maxy = b; + while (maxy - miny > 1e-3) + { + double y1 = (miny * 2 + maxy) / 3; + double y2 = (miny + maxy * 2) / 3; + double v1 = func(x1, y1); + double v2 = func(x1, y2); + if (v1 < v2) + { + maxy = y2; + } + else + { + miny = y1; + } + } + double s1 = func(x1, (maxy + miny) * 0.5); + miny = 0, maxy = b; + while (maxy - miny > 1e-3) + { + double y1 = (miny * 2 + maxy) / 3; + double y2 = (miny + maxy * 2) / 3; + double v1 = func(x2, y1); + double v2 = func(x2, y2); + if (v1 < v2) + { + maxy = y2; + } + else + { + miny = y1; + } + } + double s2 = func(x2, (maxy + miny) * 0.5); + if (s1 < s2) + { + maxx = x2; + } + else + { + minx = x1; + } + } + double x = (maxx + minx) * 0.5; + double miny = 0, maxy = b; + while (maxy - miny > 1e-3) + { + double y1 = (miny * 2 + maxy) / 3; + double y2 = (miny + maxy * 2) / 3; + double v1 = func(x, y1); + double v2 = func(x, y2); + if (v1 < v2) + { + maxy = y2; + } + else + { + miny = y1; + } + } + printf("%.3lf\n", func(x, (maxy + miny) * 0.5)); + } +} diff --git a/11317.cpp b/11317.cpp new file mode 100644 index 0000000..ab5b19e --- /dev/null +++ b/11317.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define MAXN 1000000 + +int prev_[MAXN + 1] = {0}, nxt[MAXN + 1] = {0}; +double log10factorial[MAXN + 1] = {0}; + +inline void init() +{ + prev_[2] = -1; + nxt[2] = 3; + prev_[3] = 2; + nxt[3] = 5; + for (long long i = 3; i <= MAXN; i += 2) + { + prev_[i] = i - 2; + nxt[i] = i + 2; + } + for (long long prime = 3; prime * prime <= MAXN; prime = nxt[prime]) + { + for (long long fact = prime; prime * fact <= MAXN; fact = nxt[fact]) + { + for (long long mult = prime * fact; mult <= MAXN; mult *= prime) + { + // remove + nxt[prev_[mult]] = nxt[mult]; + prev_[nxt[mult]] = prev_[mult]; + prev_[mult] = 0; + nxt[mult] = 0; + } + } + } + + log10factorial[1] = 0.0f; + for (int i = 2; i <= MAXN; i++) + log10factorial[i] = log10factorial[i - 1] + log10(i); +} + +int main() +{ + init(); + int cases = 0; + long double gcd = 0, lcm = 0; + long long n = 0, prime = 0, power = 0; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + cases++; + gcd = 0; + for (prime = 2; prime * 2 <= n; prime = nxt[prime]) + { + power = prime; + while (n >= power) + { + gcd += (n / (int)power) * (n / (int)power - 1) / 2 * log10(prime); + power *= prime; + } + } + lcm = log10factorial[n] * (n - 1) - gcd; + printf("Case %d: %lld %lld\n", cases, (long long)(gcd / 100) + 1, (long long)(lcm / 100) + 1); + } + return 0; +} diff --git a/11318.cpp b/11318.cpp new file mode 100644 index 0000000..0e445eb --- /dev/null +++ b/11318.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +// PROBLEMITA DE LAS CANICAS....: UNO CLKW y el otro ANTICLOCK.... :) + +#define ll long long +ll arr[50001]; +int n; +ll distd(int k, int ind) +{ + if (ind > k) + { + return ind - k; + } + return n - k + ind; +} +ll disti(int k, int ind) +{ + if (k > ind) + { + return k - ind; + } + return n - ind + k; +} +int M = 40000000; +int main() +{ + int i, j, k, ind, vaux; + ; + int casos; + scanf("%i", &casos); + ll sumi, sumd, a1[50001], a2[50001]; + for (int h = 0; h < casos; h++) + { + scanf("%i", &n); + for (i = 0; i < n; i++) + { + scanf("%lld", &arr[i]); + } + sumi = 0, sumd = 0, ind = 0, vaux = 0; + a1[0] = 0; + for (i = 1; i < n; i++) + { + a1[i] = arr[i] * i + a1[i - 1]; + } + a2[n - 1] = arr[n - 1]; + for (i = n - 2; i >= 0; i--) + { + a2[i] = arr[i] * (n - i) + a2[i + 1]; + } + a2[0] = 0; + ll canicasd = 0, canicasi = 0; + ind = 0; + while (a1[(ind + 1) % n] < a2[(ind + 2) % n]) + { + ind = (ind + 1) % n; + } + ind = (ind + 1) % n; + if (a1[ind] != a2[(ind + 1) % n]) + { + ll a = a1[ind - 1]; + ll b = a2[(ind + 1) % n]; + ll p = (b + (arr[ind] * disti(0, ind)) - a) / n; + for (i = 1; i < ind; i++) + { + canicasd += arr[i]; + } + canicasd += p; + sumd = a1[ind - 1] + p * ind; + for (i = n - 1; i > ind; i--) + { + canicasi += arr[i]; + } + canicasi += (arr[ind] - p); + sumi = a2[(ind + 1) % n] + (arr[ind] - p) * disti(0, ind); + vaux = p; + } + else + { + vaux = arr[ind]; + for (i = 1; i <= ind; i++) + { + canicasd += arr[i]; + } + sumd = a1[ind]; + for (i = n - 1; i > ind; i--) + { + canicasi += arr[i]; + } + sumi = a2[(ind + 1) % n]; + } + ll res = 0, nsol = 0; + if (sumi == sumd) + { + res += sumi, nsol++; + } + for (k = 1; k < n; k++) + { + if (k != ind) + { + canicasi += arr[k - 1]; + sumi += canicasi; + sumd -= canicasd; + canicasd -= arr[k]; + } + else + { + canicasi += arr[k - 1]; + sumi += canicasi; + sumd -= canicasd; + canicasd -= vaux; + canicasi -= (arr[k] - vaux); + sumi -= (arr[k] - vaux) * n; + ind = (ind + 1) % n; + vaux = 0; + } + canicasd += (arr[ind] - vaux); + sumd += (arr[ind] - vaux) * distd(k, ind); + canicasi -= (arr[ind] - vaux); + sumi -= (arr[ind] - vaux) * disti(k, ind); + vaux = arr[ind]; + while (sumd < sumi) + { + ind = (ind + 1) % n; + sumd += arr[ind] * distd(k, ind); + canicasd += arr[ind]; + sumi -= arr[ind] * disti(k, ind); + canicasi -= arr[ind]; + vaux = arr[ind]; + } + if (sumd != sumi) + { + sumd -= arr[ind] * distd(k, ind); + canicasd -= arr[ind]; + ll a = sumd; + ll b = sumi; + ll p = (b + (arr[ind] * disti(k, ind)) - a) / n; + canicasd += p; + sumd += p * distd(k, ind); + canicasi += arr[ind] - p; + sumi += (arr[ind] - p) * disti(k, ind); + vaux = p; + } + if (sumi == sumd) + { + res += sumi, nsol++; + } + } + printf("%lld %lld\n", nsol, res); + } + return 0; +} diff --git a/11319.cpp b/11319.cpp new file mode 100644 index 0000000..d5de953 --- /dev/null +++ b/11319.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +long long int mat[6][6]; +int backup[7], sol[7]; +unsigned long long int a[1500]; +long long int det(int dim, long long int m[][6]) +{ + if (dim == 1) + { + return m[0][0]; + } + else + { + long long int result = 0; + long long int temp[6][6]; + for (int i = 0; i < dim; i++) + { + for (int j = 1; j < dim; j++) + { + int loc = 0; + for (int k = 0; k < dim; k++) + if (k != i) + { + temp[j - 1][loc++] = m[j][k]; + } + } + long long int ret = m[0][i] * det(dim - 1, temp); + if (i & 1) + { + result -= ret; + } + else + { + result += ret; + } + } + return result; + } +} +int main() +{ + int n; + scanf("%d", &n); + while (n--) + { + for (int i = 0; i < 1500; i++) + { + scanf("%llu", &a[i]); + } + for (int i = 1; i <= 6; i++) + for (int j = 0, product1 = i, product2 = i + 1; j < 6; j++, product1 *= i, product2 *= i + 1) + { + mat[i - 1][j] = product2 - product1; + } + long long int dorm = det(6, mat); + bool ans = true; + for (int i = 0; i < 6 && ans; i++) + { + for (int j = 0; j < 6; j++) + { + backup[j] = mat[j][i]; + } + for (int j = 0; j < 6; j++) + { + mat[j][i] = a[j + 1] - a[j]; + } + long long int s = det(6, mat); + if (s % dorm != 0) + { + ans = false; + } + else + { + s /= dorm; + if (s < 0 || s > 1000) + { + ans = false; + } + else + { + sol[i + 1] = s; + } + } + for (int j = 0; j < 6; j++) + { + mat[j][i] = backup[j]; + } + } + sol[0] = a[0] - sol[1] - sol[2] - sol[3] - sol[4] - sol[5] - sol[6]; + if (sol[0] < 0 || sol[0] > 1000) + { + ans = false; + } + for (int i = 0; i < 1500 && ans; i++) + { + long long int temp = a[i] - sol[0]; + for (int j = 0; j < 6 && ans; j++) + { + if (temp % (i + 1) != 0) + { + ans = false; + } + temp /= (i + 1); + temp -= sol[j + 1]; + } + if (temp != 0) + { + ans = false; + } + } + if (ans) + { + for (int i = 0; i < 7; i++) + { + if (i) + { + putchar(' '); + } + printf("%d", sol[i]); + } + puts(""); + } + else + { + puts("This is a smart sequence!"); + } + } + return 0; +} diff --git a/11321.cpp b/11321.cpp index dd6be43..8dc6e3a 100644 --- a/11321.cpp +++ b/11321.cpp @@ -1,74 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -struct Num{ - int64 num; - int64 mod; - int64 ove; -}; - -bool comp(Num A, Num B){ - if(A.mod != B.mod) - return (A.mod < B.mod); - else if(A.ove==0 && B.ove==1) - return false; - else if(A.ove==1 && B.ove==0) - return true; - else if(A.ove==1 && B.ove==1) - return (A.num > B.num); - else - return (A.num < B.num); -} - -int main(){ - while(true){ - int64 N, M, i, n; - cin >> N >> M; - cout<< N << " " << M <> n; - vec[i].num = n; - vec[i].mod = n % M; - vec[i].ove = abs(n % 2); - } - - sort(vec, vec+N, comp); - - for(i=0; i + +using namespace std; + +int n, m; + +bool theTruthIsOutThere(int a, int b) +{ + if (a % m == b % m) + { + if (a % 2 == 0 && b % 2 == 0) + { + return a < b; + } + else if (a % 2 != 0 && b % 2 != 0) + { + return a > b; + } + else if (a % 2 == 0 && b % 2 != 0) + { + return false; + } + else if (a % 2 != 0 && b % 2 == 0) + { + return true; + } + } + return a % m < b % m; +} + +int main() +{ + int nums[10001]; + while (cin >> n >> m) + { + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + sort(nums, nums + n, theTruthIsOutThere); + cout << n << " " << m << endl; + for (int i = 0; i < n; i++) + { + cout << nums[i] << endl; + } + } + return 0; +} diff --git a/11323.cpp b/11323.cpp new file mode 100644 index 0000000..543518f --- /dev/null +++ b/11323.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define M 10000 + +bool range[1000][101]; +int cand[1000]; + +bool constraint_check(int i, int j, int a, int b, int c, int lim) +{ + if (a == 0 && b == 0 && c == 0) + { + return true; + } + if (b == 0) + { + if (a == 0 || c % a != 0 || c / a <= 0 || range[i][c / a] == false) + { + return false; + } + cand[i] = 1; + rep(k, lim) range[i][k] = false; + range[i][c / a] = true; + } + else + { + REP(k, 1, lim) + { + if (range[i][k] == false) + { + continue; + } + int tmp = c - a * k; + if (tmp % b != 0 || tmp / b <= 0 || range[j][tmp / b] == false) + { + range[i][k] = false, cand[i]--; + } + } + } + if (a == 0) + { + if (b == 0 || c % b != 0 || c / b <= 0 || range[j][c / b] == false) + { + return false; + } + cand[j] = 1; + rep(k, lim) range[j][k] = false; + range[j][c / b] = true; + } + else + { + REP(k, 1, lim) + { + if (range[j][k] == false) + { + continue; + } + int tmp = c - b * k; + if (tmp % a != 0 || tmp / a <= 0 || range[i][tmp / a] == false) + { + range[j][k] = false, cand[j]--; + } + } + } + if (cand[i] == 0 || cand[j] == 0) + { + return false; + } + return true; +} + +bool solve(int n, int m, int k, int *a, int *indexi, int *b, int *indexj, int *c) +{ + rep(i, n) + { + rep(j, k + 1) range[i][j] = true; + range[i][0] = false; + cand[i] = k; + } + rep(i, m) + { + if (constraint_check(indexi[i], indexj[i], a[i], b[i], c[i], k + 1) == false) + { + return false; + } + } + return true; +} + +main() +{ + int te; + int a[M], b[M], c[M], indexi[M], indexj[M]; + cin >> te; + while (te--) + { + int n, m, k; + cin >> n >> m >> k; + rep(i, m) + { + cin >> a[i] >> indexi[i] >> b[i] >> indexj[i] >> c[i]; + indexi[i]--; + indexj[i]--; + } + if (solve(n, m, k, a, indexi, b, indexj, c)) + { + puts("yes"); + } + else + { + puts("no"); + } + } +} diff --git a/11324.cpp b/11324.cpp new file mode 100644 index 0000000..9a3a09e --- /dev/null +++ b/11324.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define MAX 1000 + +int order; +bool trans[MAX][MAX]; +vector Trans_Edge[MAX]; +vector Edge[MAX]; +vector Rev_Edge[MAX]; + +int weight[MAX]; +bool visited[MAX]; +int component[MAX]; +struct PO +{ + int order, num; +}; +PO postorder[MAX]; +bool cmp(const PO &a, const PO &b) +{ + return a.order > b.order; +} + +void dfs(int now) +{ + visited[now] = true; + rep(i, Edge[now].size()) + { + int next = Edge[now][i]; + if (visited[next] == false) + { + dfs(next); + } + } + postorder[now].order = ::order++; + postorder[now].num = now; +} + +int bfs(int num, int start, int comp) +{ + int cnt = 1; + queue Q; + Q.push(start); + component[start] = comp; + visited[start] = true; + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + component[now] = comp; + rep(i, Rev_Edge[now].size()) + { + int next = Rev_Edge[now][i]; + if (visited[next] == false) + { + visited[next] = true; + Q.push(next); + cnt++; + } + } + } + return cnt; +} + +vector c_edge[MAX]; //for topological sort +vector c_rev_edge[MAX];//for ??? +void t_sort(int now, vector &t_sorted) +{ + visited[now] = true; + rep(i, c_edge[now].size()) + { + int next = c_edge[now][i]; + if (visited[next] == false) + { + t_sort(next, t_sorted); + } + } + t_sorted.push_back(now); +} + +int solve(int n) +{ + int ans = 0; + ::order = 0; + rep(i, n) visited[i] = false; + rep(i, n) + { + if (visited[i] == false) + { + dfs(i); + } + } + rep(i, n) visited[i] = false; + int cnt[n]; + int p = 0; + rep(i, n) cnt[i] = 0, component[i] = -1; + sort(postorder, postorder + n, cmp); + rep(i, n) + { + int now = postorder[i].num; + if (visited[now] == false) + { + cnt[p] = bfs(n, now, p); + p++; + } + } + rep(i, n) + { + rep(j, p) c_edge[j].clear(), c_rev_edge[j].clear(); + rep(j, n) trans[i][j] = false; + } + rep(i, n) + { + rep(j, Edge[i].size()) + { + if (component[i] != component[Edge[i][j]] && trans[component[i]][component[Edge[i][j]]] == false) + { + c_edge[component[i]].push_back(component[Edge[i][j]]), trans[component[i]][component[Edge[i][j]]] = true; + c_rev_edge[component[Edge[i][j]]].push_back(component[i]); + } + } + } + vector t_sorted; + rep(i, p) visited[i] = false; + rep(i, p) + { + if (visited[i] == false) + { + t_sort(i, t_sorted); + } + } + reverse(t_sorted.begin(), t_sorted.end()); + int table[MAX]; + rep(i, t_sorted.size()) + { + int now = t_sorted[i]; + int tmp = 0; + rep(i, c_rev_edge[now].size()) tmp = max(tmp, table[c_rev_edge[now][i]]); + table[now] = tmp + cnt[now]; + ans = max(ans, table[now]); + } + return ans; +} + +main() +{ + int te; + scanf("%d", &te); + while (te--) + { + int n, m; + scanf("%d%d", &n, &m); + rep(i, n) + { + rep(j, n) + { + trans[i][j] = false; + } + Edge[i].clear(); + Rev_Edge[i].clear(); + trans[i][i] = true; + } + rep(i, m) + { + int f, t; + scanf("%d%d", &f, &t); + f--; + t--; + Edge[f].push_back(t); + Rev_Edge[t].push_back(f); + } + cout << solve(n) << endl; + } +} diff --git a/11325.cpp b/11325.cpp new file mode 100644 index 0000000..50956bf --- /dev/null +++ b/11325.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int enemy[26], my[13]; + +bool rec(int i) +{ + if (i == 26) + { + return true; + } + for (int r = 0; r < 13; r++) + { + if (my[r] == 0) + { + continue; + } + if (enemy[i] > r) + { + continue; + } + if (enemy[i] < r) + { + --my[r]; + if (rec(i + 1)) + { + return true; + } + ++my[r]; + break; + } + else + { + if (i == 25) + { + continue; + } + --my[r]; + int j = min(i + 4, 25); + if (rec(j)) + { + return true; + } + ++my[r]; + } + } + return false; +} + +int readCard() +{ + char rank, suit; + scanf(" %c %c", &rank, &suit); + const char *s = "23456789TJQKA"; + return strchr(s, toupper(rank)) - s; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + for (int i = 0; i < 13; i++) + { + my[i] = 4; + } + for (int i = 0; i < 26; i++) + { + int x = readCard(); + enemy[i] = x; + --my[x]; + assert(my[x] >= 0); + } + if (rec(0)) + { + printf("yes\n"); + } + else + { + printf("no\n"); + } + } +} diff --git a/11326.cpp b/11326.cpp new file mode 100644 index 0000000..a656f7d --- /dev/null +++ b/11326.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + int casos; + cin >> casos; + while (casos--) + { + int L, W, angleGrad; + cin >> L >> W >> angleGrad; + //Pasarlo a radianes (2*acos(0) == PI) + double angle = 2 * acos(0) * angleGrad / 180.0; + if (angle == 0) + { + printf("1.000\n"); + } + else + { + double x = W / tan(angle); + int k = 1; + while (x * k <= L) + { + ++k; + } + double y = L - (k - 1) * x; + double h; + if (k % 2 == 0) + { + h = W - y * tan(angle); + } + else + { + h = y * tan(angle); + } + double s = y / cos(angle); + double resultado = W / sin(angle) * (k - 1); + resultado += s; + resultado /= sqrt(h * h + L * L); + printf("%.3f\n", resultado); + } + } + return 0; +} diff --git a/11327.cpp b/11327.cpp new file mode 100644 index 0000000..eb6ba4e --- /dev/null +++ b/11327.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +long long int s[200001]; +bool v[200001]; +int gcd(int a, int b) +{ + return (b == 0) ? a : gcd(b, a % b); +} +void init() +{ + s[1] = 2; + for (int i = 2; i <= 200000; i++) + { + int left = i, p = i; + for (int j = 2; j * j <= p; j++) + if (p % j == 0) + { + left = (left / j) * (j - 1); + while (p % j == 0) + { + p /= j; + } + } + if (p != 1) + { + left = (left / p) * (p - 1); + } + s[i] = s[i - 1] + left; + } +} +int binarysearch(long long int value) +{ + if (value <= s[1]) + { + return 1; + } + else if (s[200000] <= value) + { + return 200000; + } + else + { + int left = 1, right = 200000, mid = (left + right) / 2; + while (left <= right) + { + if (s[mid - 1] < value && s[mid] >= value) + { + return mid; + } + else if (s[mid] < value) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + mid = (left + right) / 2; + } + return -1; + } +} +int main() +{ + init(); + long long int value; + while (scanf("%lld", &value), value) + { + int dom = binarysearch(value); + memset(v, true, sizeof(v)); + long long int left = value - s[dom - 1]; + int p; + for (p = 0; p <= dom && left; p++) + if (gcd(p, dom) == 1) + { + left--; + } + printf("%d/%d\n", p - 1, dom); + } +} diff --git a/11328.cpp b/11328.cpp new file mode 100644 index 0000000..65ee7a5 --- /dev/null +++ b/11328.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +#define MAX 100 + +double dp[102][102][102];// n,m,k; Pr[Jim never draws a short stick] + +void doit() +{ + memset(dp, 0, sizeof(dp)); + for (int m = 0; m <= MAX; m++) + { + dp[1][m][0] = 1.0; + } + for (int n = 2; n <= MAX; n++) + { + for (int m = 0; m <= MAX; m++) + { + // n-1 short straws, m long straws + for (int k = 0; k < n; k++) + { + // short + double pS = (n - 1) / (double)(n - 1 + m); + double pL = 1 - pS; + if (k != 0) + { + dp[n][m][k] += pS * dp[n - 1][m][k - 1]; + } + // long + if (m > 0) + { + if (k == 0) + { + dp[n][m][k] += pL * dp[n][m - 1][n - 1]; + } + else + { + dp[n][m][k] += pL * dp[n][m - 1][k - 1]; + } + } + } + } + } +} + +int main() +{ + int T, n, m, k; + doit(); + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d %d %d", &n, &m, &k); + printf("%.3f\n", dp[n][m][k] * 100.0); + } +} diff --git a/11329.cpp b/11329.cpp new file mode 100644 index 0000000..97e249b --- /dev/null +++ b/11329.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +#define TOP 0 +#define FRONT 1 +#define RIGHT 2 +#define BACK 3 +#define LEFT 4 +#define BOTTOM 5 + +#define NOMI true +#define NONOMI false +#define ROTATE_SOUTH 0 +#define ROTATE_NORTH 1 +#define ROTATE_EAST 2 +#define ROTATE_WEST 3 +#define MAX (1 << 26); +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +int r_table[4][6] = + { + {BACK, TOP, RIGHT, BOTTOM, LEFT, FRONT}, + {FRONT, BOTTOM, RIGHT, TOP, LEFT, BACK}, + {LEFT, FRONT, TOP, BACK, BOTTOM, RIGHT}, + {RIGHT, FRONT, BOTTOM, BACK, TOP, LEFT}}; + +class state +{ +public: + bool m[4][4]; + bool dice[6]; + int px, py; + int cnt; + int me; + bool is_clear() + { + rep(i, 6) if (dice[i] == NONOMI) + { + return false; + } + return true; + } + void move(int d) + { + bool tmp[6]; + rep(i, 6) tmp[i] = dice[i]; + rep(i, 6) dice[i] = tmp[r_table[d][i]]; + px = px + dx[d]; + py = py + dy[d]; + if (m[py][px] == NOMI && dice[BOTTOM] == NOMI) + ; + else if (m[py][px] == NOMI) + { + m[py][px] = NONOMI, dice[BOTTOM] = NOMI; + } + else if (dice[BOTTOM] == NOMI) + { + m[py][px] = NOMI, dice[BOTTOM] = NONOMI; + } + me = 0; + rep(i, 6) + { + me = me << 1; + if (dice[i]) + { + me += 1; + } + } + rep(i, 4) + { + rep(j, 4) + { + me = (me << 1); + if (m[i][j]) + { + me += 1; + } + } + } + } + bool operator<(const state &a) const + { + // if ( px != a.px)return px < a.px; + //if ( py != a.py)return py < a.py; + if (me != a.me) + { + return me < a.me; + } + return false; + } +}; + +int solve(state ini) +{ + set S[4][4]; + queue Q; + Q.push(ini); + S[ini.py][ini.px].insert(ini.me); + while (!Q.empty()) + { + state now = Q.front(); + Q.pop(); + rep(i, 4) + { + state next = now; + int nex = next.px + dx[i], ney = next.py + dy[i]; + if (nex < 0 || ney < 0 || nex >= 4 || ney >= 4) + { + continue; + } + next.move(i); + next.cnt++; + if (next.is_clear()) + { + return next.cnt; + } + if (S[next.py][next.px].find(next.me) == S[next.py][next.px].end()) + { + Q.push(next); + S[next.py][next.px].insert(next.me); + } + } + } + return -1; +} + +main() +{ + int te; + state ini; + char m[4][5]; + cin >> te; + while (te--) + { + state ini; + rep(i, 4) cin >> m[i]; + rep(i, 6) ini.dice[i] = NONOMI; + ini.cnt = 0; + rep(i, 4) + { + rep(j, 4) + { + if (m[i][j] == 'D') + { + ini.px = j, ini.py = i, ini.m[i][j] = NONOMI; + } + else if (m[i][j] == 'X') + { + ini.m[i][j] = NOMI; + } + else + { + ini.m[i][j] = NONOMI; + } + } + } + int ans = solve(ini); + if (ans != -1) + { + cout << solve(ini) << endl; + } + else + { + puts("impossible"); + } + } +} diff --git a/1133.cpp b/1133.cpp new file mode 100644 index 0000000..45c260a --- /dev/null +++ b/1133.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define maxn 3005 + +struct Segment +{ + int l, r, w; + bool operator<(const Segment x) const + { + if (l == x.l) + return r < x.r; + return l < x.l; + } +}; +int n; +int dp[maxn]; +Segment seg[maxn]; +int search(int key) +{ + int low = key, high = n, mid = (low + high + 1) / 2; + while (low < high) + { + if (seg[mid].l < seg[key].r) + low = mid; + else + high = mid - 1; + mid = (low + high + 1) / 2; + } + if (seg[mid].l <= seg[key].r) + return mid + 1; + return mid; +} + +int main() +{ + int kase = 0; + while (scanf("%d", &n) != EOF) + { + char str[100]; + if (kase) + printf("\n"); + for (int i = 0; i < n; ++i) + { + scanf("%s%d%d%d", str, &seg[i].l, &seg[i].r, &seg[i].w); + seg[i].r += seg[i].l; + } + memset(dp, 0, sizeof(dp)); + sort(seg, seg + n); + for (int i = n - 1; i >= 0; --i) + { + int L = search(i); + // cout< + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 10000; +int edge[N][2]; +int p[N]; +bool vis[N]; +int ans = 0; +int assign[N]; + +void dfs(int now, int prev, int cnt) +{ + if (vis[now]) + { + ans += cnt - 2; + return; + } + vis[now] = true; + rep(i, p[now]) + { + if (edge[now][i] != prev) + { + dfs(edge[now][i], now, cnt + 1); + break; + } + } +} + +inline int getindex(int in, int &index) +{ + if (assign[in] == -1) + { + assign[in] = index++; + } + return assign[in]; +} + +main() +{ + int te; + scanf("%d", &te); + rep(tc, te) + { + map M; + int n; + int index = 0; + scanf("%d", &n); + rep(i, N) assign[i] = -1; + rep(i, n) vis[i] = false, p[i] = 0; + rep(i, n) + { + int f, t; + scanf("%d%d", &f, &t); + f--; + t--; + f = getindex(f, index); + t = getindex(t, index); + if (f == t) + { + continue; + } + edge[f][p[f]++] = t; + edge[t][p[t]++] = f; + } + /* + rep(i,n){ + sort(edge[i].begin(),edge[i].end()); + edge[i].erase(unique(edge[i].begin(),edge[i].end()), + edge[i].end()); + } + */ + int tmp = 0; + rep(i, n) + { + if (p[i] != 0 && edge[i][0] == edge[i][1]) + { + tmp++; + p[i] = 0; + } + } + ans = 0; + rep(i, n) + { + if (p[i] && !vis[i]) + { + dfs(i, -1, 1); + } + } + ans += tmp / 2; + cout << ans << endl; + } + return 0; +} diff --git a/11331.cpp b/11331.cpp new file mode 100644 index 0000000..b27f134 --- /dev/null +++ b/11331.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 2000 +#define mp make_pair + +vector edge[N]; +bool visited[N]; +int cost[N]; +typedef pair pii; + +pii bfs(int s) +{ + int cnt[2] = {0}; + queue Q; + Q.push(s); + visited[s] = true; + cost[s] = 0; + cnt[0]++; + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + rep(i, edge[now].size()) + { + int next = edge[now][i]; + if (visited[next] == true) + { + if (cost[next] == cost[now]) + { + return mp(-1, -1); + } + continue; + } + visited[next] = true; + cost[next] = cost[now] + 1; + cnt[cost[next] % 2]++; + Q.push(next); + } + } + return mp(cnt[0], cnt[1]); +} + +bool dp(int bull, vector data) +{ + static bool table[N][N + 1]; + rep(i, data.size()) rep(j, bull + 1) table[i][j] = false; + table[0][data[0].first] = table[0][data[0].second] = true; + REP(i, 1, data.size()) + { + rep(j, bull + 1) + { + table[i][j] |= table[i - 1][j]; + if (j - data[i].first >= 0) + { + table[i][j] = table[i][j] | table[i - 1][j - data[i].first]; + } + if (j - data[i].second >= 0) + { + table[i][j] = table[i][j] | table[i - 1][j - data[i].second]; + } + } + } + /* + rep(i,data.size()){ + rep(j,bull+1)cout << table[i][j]; + cout << endl; + } + */ + return table[data.size() - 1][bull]; +} + +bool solve(int n, int b, int c) +{ + if (b == 0 && c == 0) + { + return true; + } + vector in; + rep(i, n) cost[i] = -1, visited[i] = false; + rep(i, n) + { + pii tmp; + if (visited[i] == false) + { + tmp = bfs(i); + if (tmp.first == -1) + { + return false; + } + in.push_back(tmp); + } + } + return dp(max(b, c), in); +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int b, c, m; + cin >> b >> c >> m; + int n = b + c; + rep(i, n) edge[i].clear(); + rep(i, m) + { + int f, t; + cin >> f >> t; + f--; + t--; + edge[f].push_back(t); + edge[t].push_back(f); + } + if (m == 0 || solve(n, b, c)) + { + puts("yes"); + } + else + { + puts("no"); + } + } +} diff --git a/11332.cpp b/11332.cpp new file mode 100644 index 0000000..cb8aa69 --- /dev/null +++ b/11332.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int n, tmp; + while (cin >> n) + { + if (n == 0) + { + break; + } + while (n > 9) + { + tmp = n; + n = 0; + while (tmp) + { + n += tmp % 10; + tmp /= 10; + } + } + cout << n << endl; + } + return 0; +} diff --git a/11333.cpp b/11333.cpp new file mode 100644 index 0000000..9dd1b23 --- /dev/null +++ b/11333.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +const int MAXN = 19; + +char canBeZero[256]; +long long power[MAXN], coef[256]; +set digits; + +bool bruteforce() +{ + char assign[11]; + int k = 0; + for (; k < 10 - digits.size(); ++k) + { + assign[k] = '*'; + } + FOREACH(digits, c) + { + assign[k++] = *c; + } + assign[10] = 0; + sort(assign, assign + 10); + do + { + if (assign[0] != '*' and !canBeZero[assign[0]]) + { + continue; + } + long long sum = 0; + for (int i = 0; i < 10; ++i) + { + char digit = assign[i]; + if (digit == '*') + { + continue; + } + long long value = i; + assert('A' <= digit and digit <= 'Z'); + sum += coef[digit] * value; + } + if (sum == 0) + { + puts(assign); + return true; + } + } while (next_permutation(assign, assign + 10)); + return false; +} + +int main() +{ + power[0] = 1; + for (int i = 1; i < MAXN; ++i) + { + power[i] = power[i - 1] * 10; + } + string s; + while (getline(cin, s)) + { + for (char c = 'A'; c <= 'Z'; ++c) + { + canBeZero[c] = true; + coef[c] = 0; + } + digits.clear(); + stringstream sin(s); + int sign = +1; + bool invert = false; + while (sin >> s) + { + assert(s.size() <= 16); + if (s == "+") + { + sign = +1; + if (invert) + { + sign = -1; + } + } + else if (s == "-") + { + sign = -1; + if (invert) + { + sign = +1; + } + } + else if (s == "=") + { + invert = true; + sign = -1; + } + else + { + if (s.size() >= 2) + { + canBeZero[s[0]] = false; + } + reverse(s.begin(), s.end()); + for (int i = 0; i < s.size(); ++i) + { + assert('A' <= s[i] and s[i] <= 'Z'); + coef[s[i]] += power[i] * sign; + digits.insert(s[i]); + } + } + } + if (!bruteforce()) + { + for (int k = 0; k < 10; ++k) + { + printf("*"); + } + printf("\n"); + } + } + return 0; +} diff --git a/11334.cpp b/11334.cpp new file mode 100644 index 0000000..42ea038 --- /dev/null +++ b/11334.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +typedef long long ll; + +#define EPS 1e-9 + +int xa, ya, ha, xb, yb, hb; + +int x[51], y[51], h[51], r[51]; + +bool intersect(int i) +{ + double A, B, C; + A = ya - yb; + B = xb - xa; + C = -(A * xa + B * ya); + + double da = sqrt(double(xa - x[i]) * (xa - x[i]) + (ya - y[i]) * (ya - y[i])); + double db = sqrt(double(xb - x[i]) * (xb - x[i]) + (yb - y[i]) * (yb - y[i])); + if ((xa - x[i]) * (xa - x[i]) + (ya - y[i]) * (ya - y[i]) <= r[i] * r[i]) + { + if (r[i] == 0 || ha < (1 - da / (r[i])) * h[i] + EPS) + return 1; + } + if ((xb - x[i]) * (xb - x[i]) + (yb - y[i]) * (yb - y[i]) <= r[i] * r[i]) + { + if (r[i] == 0 || hb < (1 - db / (r[i])) * h[i] + EPS) + return 1; + } + + double dd = (A * double(x[i]) + B * double(y[i]) + C) / sqrt(A * A + B * B); + + double xx, yy, hh; + xx = double(x[i]) - A / sqrt(A * A + B * B) * dd; + yy = double(y[i]) - B / sqrt(A * A + B * B) * dd; + dd = fabs(dd); + if (yb != ya) + hh = double(yy - ya) / (yb - ya) * (hb - ha) + ha; + else if (xb != xa) + hh = double(xx - xa) / (xb - xa) * (hb - ha) + ha; + else + return 0; + + double a, b, c; + if (hb != ha) + { + a = double((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya)) / (hb - ha) / (hb - ha); + b = -2 * hh * a; + c = hh * hh * a + dd * dd; + + a -= double(r[i] * r[i]) / h[i] / h[i]; + b -= -2 * double(r[i] * r[i]) / h[i]; + c -= r[i] * r[i]; + + double root1, root2; + if (abs(b * b - 4 * a * c) < EPS) + { + root1 = -b / 2 / a; + if (root1 > -EPS && root1 < h[i] + EPS && root1 > min(ha, hb) - EPS && root1 < max(ha, hb) + EPS) + return 1; + else + return 0; + } + if ((b * b - 4 * a * c) < EPS) + return 0;//0 root + + root1 = (-b + sqrt(b * b - 4 * a * c)) / 2 / a; + root2 = (-b - sqrt(b * b - 4 * a * c)) / 2 / a; + + if (root1 > -EPS && root1 < h[i] + EPS && root1 > min(ha, hb) - EPS && root1 < max(ha, hb) + EPS) + return 1; + if (root2 > -EPS && root2 < h[i] + EPS && root2 > min(ha, hb) - EPS && root2 < max(ha, hb) + EPS) + return 1; + return 0; + } + else + { + if (ha >= 0 && ha < h[i] && (r[i] - double(r[i]) / h[i] * ha > dd - EPS)) + return 1; + else + return 0; + } +} + +int main() +{ + int n; + cin >> n; + + while (n >= 0) + { + bool visible = 1; + for (int i = 0; i < n; i++) + scanf("%d %d %d %d", x + i, y + i, h + i, r + i); + scanf("%d %d %d", &xa, &ya, &ha); + scanf("%d %d %d", &xb, &yb, &hb); + + if (ha < EPS || hb < EPS) + { + visible = 0; + } + for (int i = 0; i < n; i++) + { + visible &= (!intersect(i)); + } + cout << (visible ? "Yes" : "No") << endl; + cin >> n; + } + return 0; +} diff --git a/11335.cpp b/11335.cpp new file mode 100644 index 0000000..79fdd0e --- /dev/null +++ b/11335.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +// Andrés Mejía +// Accepted + +////////////// Prewritten code follows. Look down for solution. //////////////// +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << (x) << endl + +const double EPS = 1e-9; +int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} +////////////////////////// Solution starts below. ////////////////////////////// + +int f(int s, int v) +{ + for (int k = 1;; ++k) + { + if (1LL * k * (k + 1) / 2 >= s + k * v) + { + return k; + } + } + assert(false); +} + +int main() +{ + int a, u, v; + while (cin >> a >> u >> v) + { + if (a == 0) + { + cout << 0 << endl; + continue; + } + int ans = max(f(a, u), f(0, v)); + cout << ans << endl; + } + return 0; +} diff --git a/11336.cpp b/11336.cpp new file mode 100644 index 0000000..a5472f4 --- /dev/null +++ b/11336.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +char dic[500][100]; +int nodes, nodes1; +char ident1[100], ident2[100], str[1000], a[100], b[100]; +bool g1[500][500], g2[500][500]; +bool v[500]; +int find(char *s) +{ + for (int i = 0; i < nodes; i++) + if (strcmp(s, dic[i]) == 0) + { + return i; + } + strcpy(dic[nodes++], s); + return nodes - 1; +} +bool DFS(int start, int end) +{ + if (g2[start][end]) + { + return true; + } + v[start] = true; + for (int i = nodes1; i < nodes; i++) + if (!v[i] && g2[start][i] && DFS(i, end)) + { + return true; + } + return false; +} +int main() +{ + while (gets(str) != NULL) + { + sscanf(str, "%s", ident1); + if (strcmp(ident1, "END") == 0) + { + break; + } + nodes = 0; + memset(g1, false, sizeof(g1)); + while (gets(str) != NULL) + { + if (str[0] == '*') + { + break; + } + sscanf(str, "%s %s", a, b); + int x = find(a), y = find(b); + g1[x][y] = g1[y][x] = true; + } + nodes1 = nodes; + gets(str); + sscanf(str, "%s", ident2); + memset(g2, false, sizeof(g2)); + while (gets(str) != NULL) + { + if (str[0] == '*') + { + break; + } + sscanf(str, "%s %s", a, b); + int x = find(a), y = find(b); + g2[x][y] = g2[y][x] = true; + } + bool ans = true; + for (int i = 0; i < nodes1 && ans; i++) + for (int j = 0; j < nodes1 && ans; j++) + { + if (!g1[i][j]) + { + continue; + } + memset(v, false, sizeof(v)); + ans &= DFS(i, j); + } + if (ans) + { + printf("YES: %s is a more detailed version of %s\n", ident2, ident1); + } + else + { + printf("NO: %s is not a more detailed version of %s\n", ident2, ident1); + } + } +} diff --git a/11337.cpp b/11337.cpp new file mode 100644 index 0000000..6b90e97 --- /dev/null +++ b/11337.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +// Andrés Mejía + +////////////// Prewritten code follows. Look down for solution. //////////////// +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << (x) << endl + +const double EPS = 1e-9; +int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} +////////////////////////// Solution starts below. ////////////////////////////// + +vector titles; +vector songs; +vector prices; + +inline bool bit(long long mask, int i) +{ + return !!(mask & (1LL << i)); +} + +bool better(int n, long long a, long long b) +{ + for (int i = 0; i < n; ++i) + { + if (bit(a, i) and !bit(b, i)) + { + return true; + } + } + return false; +} + +int main() +{ + int run = 1; + int casos; + cin >> casos; + string s; + getline(cin, s); + getline(cin, s); + while (casos--) + { + titles.clear(); + songs.clear(); + prices.clear(); + map songId; + int nextId = 0; + assert(s[0] == '$'); + int budget; + sscanf(s.c_str() + 1, "%d", &budget); + //D(budget); + while (getline(cin, s)) + { + if (s[0] == '$') + { + break; + } + titles.push_back(s); + long long mask = 0; + //cout << "Title: " << s << endl; + while (getline(cin, s)) + { + if (s[0] == '$') + { + break; + } + int which = songId.count(s) ? songId[s] : songId[s] = nextId++; + mask |= (1LL << which); + } + int price; + sscanf(s.c_str() + 1, "%d", &price); + prices.push_back(price); + songs.push_back(mask); + //D(price); + } + assert(prices.size() == songs.size()); + assert(titles.size() == songs.size()); + int n = prices.size(); + int bestCount = -1, bestPrice = -1; + long long bestDisks = 0; + for (int mask = 0; mask < (1 << n); ++mask) + { + int totalPrice = 0; + long long boughtSongs = 0; + for (int i = 0; i < n; ++i) + if (mask & (1 << i)) + { + totalPrice += prices[i]; + boughtSongs |= songs[i]; + } + if (totalPrice > budget) + { + continue; + } + int songCount = __builtin_popcountll(boughtSongs); + if (songCount > bestCount or + songCount == bestCount and totalPrice < bestPrice or + songCount == bestCount and totalPrice == bestPrice and better(n, mask, boughtSongs)) + { + bestCount = songCount; + bestDisks = mask; + bestPrice = totalPrice; + } + } + printf("Scenario #%d: %d\n", run++, bestCount); + for (int i = 0; i < n; ++i) + if (bit(bestDisks, i)) + { + puts(titles[i].c_str()); + } + puts(""); + } + return 0; +} diff --git a/11338.cpp b/11338.cpp new file mode 100644 index 0000000..40e3856 --- /dev/null +++ b/11338.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +// Andrés Mejía + +// Accepted + +////////////// Prewritten code follows. Look down for solution. //////////////// +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << (x) << endl + +const double EPS = 1e-9; +int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} +////////////////////////// Solution starts below. ////////////////////////////// +const int MAXS = 1005; +const int MAXN = 10005; + +typedef pair point; + +vector> p; +int w, h; + +typedef pair queueItem; + +int read() +{ + int a, b; + scanf("%d.%d", &a, &b); + return a * 100 + b; +} + +double dist(const point &a, const point &b) +{ + int dx = a.first - b.first; + int dy = a.second - b.second; + return sqrt(dx * dx + dy * dy); +} + +double dijkstra() +{ + static double d[MAXN]; + for (int i = 0; i < MAXN; ++i) + { + d[i] = 1e100; + } + assert(p[0] == make_pair(0, 0)); + d[0] = 0.0; + priority_queue, greater> q; + q.push(make_pair(0.0, 0)); + while (q.size() > 0) + { + int cur = q.top().second; + double w = q.top().first; + q.pop(); + //printf("popped (%d, %d) with %lf\n", p[cur].first, p[cur].second, w); + if (cur == p.size() - 1) + { + return w; + } + if (cmp(w, d[cur]) > 0) + { + continue; + } + int from = cur; + while (from >= 0 and p[cur].first - p[from].first <= 150) + { + from--; + } + int to = cur; + while (to < p.size() and p[to].first - p[cur].first <= 150) + { + to++; + } + for (int i = from + 1; i < to; ++i) + { + if (i == cur) + { + continue; + } + double w_extra = dist(p[cur], p[i]); + if (cmp(w_extra, 150) <= 0) + { + if (cmp(w + w_extra, d[i]) < 0) + { + d[i] = w + w_extra; + q.push(make_pair(d[i], i)); + } + } + } + } + return 1e100; +} + +int main() +{ + while (scanf("%d %d", &w, &h) == 2) + { + int n; + scanf("%d", &n); + p.clear(); + for (int i = 0; i < n; i++) + { + int x, y; + x = read(), y = read(); + if (x == 0 and y == 0 or x == 100 * w and y == 100 * h) + { + continue; + } + p.push_back(make_pair(x, y)); + } + p.push_back(make_pair(0, 0)); + p.push_back(make_pair(100 * w, 100 * h)); + sort(p.begin(), p.end()); + assert(p.back() == make_pair(100 * w, 100 * h)); + int minDistance = read(); + double ans = dijkstra(); + if (cmp(ans, minDistance) <= 0) + { + puts("I am lucky!"); + } + else + { + puts("Boom!"); + } + } + return 0; +} diff --git a/11340.cpp b/11340.cpp index 6db46ed..f60a105 100644 --- a/11340.cpp +++ b/11340.cpp @@ -1,31 +1,45 @@ -#include -using namespace std; -int main(){ - int test; - scanf("%d\n",&test); - while(test--){ - int n,m,i,j,k; - scanf("%d\n",&n); - char chars[n]; - double value[n]; - double amount=0.0; - for(i=0;i + +using namespace std; + +int main() +{ + int cases; + char c; + int p; + int k; + string input; + map m; + cin >> cases; + while (cases--) + { + m.clear(); + cin >> k; + getline(cin, input);// \n + while (k--) + { + stringstream ss; + getline(cin, input); + ss << input; + c = ss.get(); + ss >> p; + m[c] = p; + } + cin >> k; + int result = 0; + getline(cin, input);// \n + while (k--) + { + getline(cin, input); + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (m.find(input[i]) != m.end()) + { + result += m[input[i]]; + } + } + } + printf("%.2f$\n", result / 100.0); + } + return 0; +} diff --git a/11341.cpp b/11341.cpp new file mode 100644 index 0000000..0b658ea --- /dev/null +++ b/11341.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int main() +{ + int T, n, m; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &n, &m); + int L[11][101] = {}, a, b, c; + for (a = 1; a <= n; a++) + for (b = 1; b <= m; b++) + { + scanf("%d", &L[a][b]); + } + int A[11][101] = {}; /*A[a,b] 總共讀了 1~a 科,共花了 b 分é˜*/ + for (a = 1; a <= m; a++) + if (L[1][a] >= 5) + { + A[1][a] = L[1][a]; + } + for (a = 2; a <= n; a++) + { + for (b = 1; b <= m; b++) + { + int max = 0; + for (c = 1; c <= b; c++) /*花多久時間*/ + if (L[a][c] >= 5 && A[a - 1][b - c]) + { + if (max < L[a][c] + A[a - 1][b - c]) + { + max = L[a][c] + A[a - 1][b - c]; + } + } + A[a][b] = max; + } + } + if (A[n][m] == 0) + { + puts("Peter, you shouldn't have played billiard that much."); + } + else + { + printf("Maximal possible average mark - %.2lf.\n", (double)A[n][m] / n + 1e-9); + } + } + return 0; +} diff --git a/11342.cpp b/11342.cpp index f7f8056..980aeea 100644 --- a/11342.cpp +++ b/11342.cpp @@ -1,74 +1,46 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) -#define MAX 225 - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - map > Map; - map >::iterator it; - - int i, j; - FOI(i, 0, MAX) - FOI(j, 0, MAX) - Map[i * i + j * j] = make_pair(i, j); - - int T; - cin >> T; - while (T--) { - int N; - bool stat = false; - cin >> N; - int M = (int) sqrt(N); - FOI(i, 0, M) { - int V = N - i * i; - it = Map.find(V); - if (it != Map.end()) { - int arr[] = {((*it).second).first, ((*it).second).second, i}; - sort(arr, arr + 3); - cout << arr[0] << " " << arr[1] << " " << arr[2] << endl; - stat = true; - break; - } - } - if (!stat) - cout << "-1\n"; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +int main() +{ + int n, k; + bool found; + map squares; + for (int i = 0; i < 224; i++) + { + squares.insert(make_pair(i * i, true)); + } + cin >> n; + while (n--) + { + cin >> k; + found = false; + for (int i = 0; i < 224; i++) + { + if (i * i > k) + { + break; + } + for (int j = i; j < 224; j++) + { + if (i * i + j * j > k) + { + break; + } + if (squares.find(k - i * i - j * j) != squares.end()) + { + found = true; + cout << i << " " << j << " " << (int)sqrt(k - i * i - j * j) << endl; + i = 224; + break; + } + } + } + if (!found) + { + cout << "-1" << endl; + } + } + return 0; +} diff --git a/11343.cpp b/11343.cpp new file mode 100644 index 0000000..b2e0e5e --- /dev/null +++ b/11343.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +double cross(P a, P b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +bool is_point_online(P a, P b, P c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + 1e-10; +} + +bool is_intersect_ls(P a1, P a2, P b1, P b2) +{ + if (is_point_online(a1, a2, b1)) + { + return true; + } + if (is_point_online(a1, a2, b2)) + { + return true; + } + if (is_point_online(b1, b2, a1)) + { + return true; + } + if (is_point_online(b1, b2, a2)) + { + return true; + } + if (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < 0 && + cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < 0) + { + return true; + } + return false; +} + +bool check(int n, P *p1, P *p2, int me) +{ + rep(i, n) + { + if (i == me) + { + continue; + } + if (is_intersect_ls(p1[me], p2[me], p1[i], p2[i])) + { + return false; + } + } + return true; +} + +int solve(int n, P *p1, P *p2) +{ + int cnt = 0; + rep(i, n) + { + if (check(n, p1, p2, i)) + { + cnt++; + } + } + return cnt; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + P p1[n], p2[n]; + rep(i, n) + { + double re, im; + scanf("%lf%lf", &re,&im); + p1[i] = P(re,im); + scanf("%lf%lf", &re,&im); + p2[i] = P(re,im); + } + cout << solve(n, p1, p2) << endl; + } + return 0; +} diff --git a/11344.cpp b/11344.cpp new file mode 100644 index 0000000..dcfb3e6 --- /dev/null +++ b/11344.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int mod[13]; +void div(char *s) +{ + int i, j; + for (i = 0; i < 13; i++) + { + mod[i] = 0; + } + for (i = 0; s[i]; i++) + for (j = 1; j < 13; j++) + { + mod[j] = ((mod[j] * 10) + (s[i] - '0')) % j; + } +} +int main() +{ + char inp[10000]; + int d, cur; + int c = 0; + scanf("%d", &c); + getchar(); + while (c--) + { + gets(inp); + div(inp); + scanf("%d", &d); + char ok = 1; + while (d--) + { + scanf("%d", &cur); + ok = ok & !mod[cur]; + } + getchar(); + if (ok) + { + printf("%s - Wonderful.\n", inp); + } + else + { + printf("%s - Simple.\n", inp); + } + } + return 0; +} diff --git a/11345.cpp b/11345.cpp new file mode 100644 index 0000000..7f94453 --- /dev/null +++ b/11345.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +const int MAX = 35 * 2; +const double EPS = 1e-6; + +bool dy(double x, double y) +{ + return x > y + EPS; // x > y +} +bool xy(double x, double y) +{ + return x < y - EPS; // x < y +} +bool dyd(double x, double y) +{ + return x > y - EPS; // x >= y +} +bool xyd(double x, double y) +{ + return x < y + EPS; // x <= y +} +bool dd(double x, double y) +{ + return fabs(x - y) < EPS; // x == y +} + +double x[MAX], y[MAX]; +struct rectangle +{ + double lx, ly, rx, ry; +}; +rectangle r[35]; +int map_[MAX][MAX]; + +int find(double a[], int m, double x) +{ + int beg = 0, end = m - 1; + while (beg <= end) + { + int mid = (beg + end) >> 1; + if (dd(x, a[mid])) + { + return mid; + } + if (dy(x, a[mid])) + { + beg = mid + 1; + } + else + { + end = mid; + } + } +} +double solve(int n, int cnt) +{ + for (int i = 0; i < n; i++) + { + int x1 = find(x, cnt, r[i].lx); + int x2 = find(x, cnt, r[i].rx); + int y1 = find(y, cnt, r[i].ly); + int y2 = find(y, cnt, r[i].ry); + for (int k = x1; k < x2; k++) + { + for (int j = y1; j < y2; j++) + { + map_[k][j]++; + } + } + } + double area = 0.0f; + for (int i = 0; i < cnt - 1; i++) + { + for (int k = 0; k < cnt - 1; k++) + { + if (map_[i][k] == n) + { + area += (x[i + 1] - x[i]) * (y[k + 1] - y[k]); + } + } + } + return area; +} + +int main() +{ + int ncases; + scanf("%d", &ncases); + int n, cnt, ind = 1; + while (ncases--) + { + cnt = 0; + scanf("%d", &n); + memset(map_, 0, sizeof(map_)); + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf%lf", &r[i].lx, &r[i].ly, &r[i].rx, &r[i].ry); + x[cnt] = r[i].lx; + y[cnt++] = r[i].ly; + x[cnt] = r[i].rx; + y[cnt++] = r[i].ry; + } + sort(x, x + cnt); + sort(y, y + cnt); + double ans = solve(n, cnt); + printf("Case %d: %.0lf\n", ind++, ans); + } + return 0; +} diff --git a/11346.cpp b/11346.cpp new file mode 100644 index 0000000..5a71483 --- /dev/null +++ b/11346.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + + +int main() +{ + int t; + double H, W, S; + scanf("%d", &t); + + while (t--) + { + scanf("%lf %lf %lf", &H, &W, &S); + if (S >= H * W) + puts("0.000000%"); + else if (S) + printf("%.6f%%\n", (W * H - S * log(W / S * H) - S) / (W * H) * 100 + 1e-12); + else + printf("%.6f%%\n", 100.0); + } +} diff --git a/11347.cpp b/11347.cpp new file mode 100644 index 0000000..76a31f2 --- /dev/null +++ b/11347.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + char s[50]; + int i, j, k, n, m, p, cases = 0; + gets(s); + while (gets(s)) + { + sscanf(s, "%d", &n); + for (i = 0, m = 0; s[i]; i++) + if (s[i] == '!') + m++; + int C[1005] = {}; + for (i = n; i >= 0; i -= m) + { + p = i; + for (j = 2; j <= p; j++) + { + while (p % j == 0) + p /= j, C[j]++; + } + } + long long res = 1; + double sum = 0; + for (i = 0; i <= n; i++) + res *= C[i] + 1, sum += log10(C[i] + 1); + printf("Case %d: ", ++cases); + if (ceil(sum) > 18) + puts("Infinity"); + else + printf("%lld\n", res); + } + return 0; +} diff --git a/11348.cpp b/11348.cpp new file mode 100644 index 0000000..b665af5 --- /dev/null +++ b/11348.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int Count[10001]; +int Stamps[50][50], own[50], n; +template +void Swap(T &a, T &b) +{ + T t = a; + a = b; + b = t; +} +int main() +{ + int t; + scanf("%d", &t); + for (int cases = 1; cases <= t; cases++) + { + memset(Count, 0, sizeof(Count)); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", own + i); + for (int j = 0; j < own[i]; j++) + { + scanf("%d", &Stamps[i][j]); + } + for (int j = 0; j < own[i]; j++) + for (int k = j + 1; k < own[i]; k++) + if (Stamps[i][j] > Stamps[i][k]) + { + Swap(Stamps[i][j], Stamps[i][k]); + } + int loc = 0, x, y; + for (x = 0; x < own[i]; x = y) + { + for (y = x + 1; y < own[i] && Stamps[i][x] == Stamps[i][y]; y++) + ; + Stamps[i][loc++] = Stamps[i][x]; + } + own[i] = loc; + for (int j = 0; j < own[i]; j++) + { + Count[Stamps[i][j]]++; + } + } + printf("Case %d:", cases); + int total = 0; + for (int i = 0; i <= 10000; i++) + if (Count[i] == 1) + { + total++; + } + for (int i = 0; i < n; i++) + { + int owns = 0; + for (int j = 0; j < own[i]; j++) + if (Count[Stamps[i][j]] == 1) + { + owns++; + } + printf(" %.6lf%%", (double)(owns * 100) / (double)(total)); + } + puts(""); + } +} diff --git a/11349.cpp b/11349.cpp index 63ac8c1..34ba2cb 100644 --- a/11349.cpp +++ b/11349.cpp @@ -1,38 +1,52 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - char ch1,ch2; - int N; - cin>>ch1>>ch2>>N; - int mat[150][150]={0},sym[150][150]={0}; - - for(int j=0;j>mat[j][k]; - sym[N-j-1][N-k-1]=mat[j][k]; - } - } - int cnt=0; - for(int j=0;j + +using namespace std; + +bool is_symmetric(long matrix[101][101], int n) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (matrix[i][j] != matrix[n - i - 1][n - j - 1]) + { + return false; + } + } + } + return true; +} + +int main() +{ + int t, n; + char tmp; + long matrix[101][101]; + cin >> t; + for (int case_num = 1; case_num <= t; case_num++) + { + cin >> tmp >> tmp >> n;// example input: N = 4 + bool ok = true; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> matrix[i][j]; + if (matrix[i][j] < 0) + { + ok = false; + } + } + } + cout << "Test #" << case_num << ": "; + if (ok && is_symmetric(matrix, n)) + { + cout << "Symmetric." << endl; + } + else + { + cout << "Non-symmetric." << endl; + } + } + return 0; +} diff --git a/11350.cpp b/11350.cpp new file mode 100644 index 0000000..ec04be8 --- /dev/null +++ b/11350.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +typedef long long ll; +int N; +const int MAXLEN = 91; +char brocot[MAXLEN]; +ll left_num, left_den, right_num, right_den, x_num, x_den; +int main() +{ + scanf("%d", &N); + while (N--) + { + scanf("%s", brocot); + // base case + if (brocot[0] == 'L') + { + left_num = 0; + left_den = 1; + right_num = 1; + right_den = 1; + } + else + { + left_num = 1; + left_den = 1; + right_num = 1; + right_den = 0; + } + x_num = left_num + right_num; + x_den = left_den + right_den; + // recursive case + for (int j = 1; brocot[j] != '\0'; ++j) + { + if (brocot[j] == 'L') + { + right_num = x_num; + right_den = x_den; + } + else + { + left_num = x_num; + left_den = x_den; + } + x_num = left_num + right_num; + x_den = left_den + right_den; + } + printf("%lld/%lld\n", x_num, x_den); + } + return 0; +} diff --git a/11351.cpp b/11351.cpp index 41f68ac..2c1d586 100644 --- a/11351.cpp +++ b/11351.cpp @@ -1,48 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int64 josephus(int64 n, int64 k){ - if(n==1) - return 0; - return (josephus(n-1, k) + k)%n; -} -int main(){ - int test; - scanf("%d",&test); - for(int t=1; t<=test; t++){ - int64 n, k; - scanf("%lld%lld", &n, &k); - int64 val = josephus(n ,k) + 1; - printf("Case %d: %lld\n", t, val); - } - return 0; -} +#include + +using namespace std; + +typedef unsigned int uint; +typedef long long int64; +typedef unsigned long long uint64; + +int64 josephus(int64 n, int64 k) +{ + if (n == 1) + { + return 0; + } + return (josephus(n - 1, k) + k) % n; +} +int main() +{ + int test; + scanf("%d", &test); + for (int t = 1; t <= test; t++) + { + int64 n, k; + scanf("%lld%lld", &n, &k); + int64 val = josephus(n, k) + 1; + printf("Case %d: %lld\n", t, val); + } + return 0; +} diff --git a/11352.cpp b/11352.cpp new file mode 100644 index 0000000..799ab3d --- /dev/null +++ b/11352.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +typedef unsigned long long ULL; +typedef long long LL; + +int R, C; +char G[100][100]; +int dist[100][100]; +int srcx, srcy, dstx, dsty; +int cx[] = {1, 1, 2, 2, -1, -1, -2, -2}; +int cy[] = {2, -2, 1, -1, 2, -2, 1, -1}; +int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; +int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; + +bool inrange(int x, int y) +{ + return (x >= 0 && x < R && y >= 0 && y < C); +} + +void BFS(int x, int y) +{ + queue> Q; + dist[x][y] = 0; + Q.push(make_pair(x, y)); + while (!Q.empty()) + { + pair t = Q.front(); + Q.pop(); + for (int i = 0; i < 8; i++) + { + int tmpx = t.first + dx[i]; + int tmpy = t.second + dy[i]; + if (inrange(tmpx, tmpy) && dist[tmpx][tmpy] == -1) + { + dist[tmpx][tmpy] = dist[t.first][t.second] + 1; + Q.push(make_pair(tmpx, tmpy)); + } + } + } +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + vector> knxy; + memset(G, 0, sizeof(G)); + memset(dist, -1, sizeof(dist)); + cin >> R >> C; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + cin >> G[i][j]; + if (G[i][j] == 'A') + { + srcx = i; + srcy = j; + } + else if (G[i][j] == 'B') + { + dstx = i; + dsty = j; + } + else if (G[i][j] == 'Z') + { + dist[i][j] = 0; + knxy.push_back(make_pair(i, j)); + } + } + } + for (int i = 0; i < knxy.size(); i++) + { + for (int j = 0; j < 8; j++) + { + int tempx = knxy[i].first + cx[j]; + int tempy = knxy[i].second + cy[j]; + if (inrange(tempx, tempy) && G[tempx][tempy] == '.') + { + dist[tempx][tempy] = 0; + } + } + } + BFS(srcx, srcy); + if (dist[dstx][dsty] == -1) + { + cout << "King Peter, you can't go now!" << endl; + } + else + { + cout << "Minimal possible length of a trip is " << dist[dstx][dsty] << endl; + } + } + return 0; +} diff --git a/11353.cpp b/11353.cpp new file mode 100644 index 0000000..97754c6 --- /dev/null +++ b/11353.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 2000001; +bool a[N]; +int b[N]; +//index = 148933 +int generate() +{ + for (int i = 1; i < N; i += 1) + { + a[i] = true; + } + for (int i = 3; i * i <= N; i += 2) + { + for (int j = 2 * i; j < N; j += i) + { + a[j] = false; + } + } + int index = 0; + b[index++] = 2; + for (int i = 3; i < N; i += 2) + if (a[i]) + { + b[index++] = i; + } + return index; +} + +int ansindex = 0; +pair ans[N]; +void aho(int now, int maxi, int cnt, int index) +{ + if (ans[now].first != -1) + { + return; + } + ans[now].first = cnt; + ans[now].second = now; + for (int i = 0; i < index && now * b[i] <= N; i++) + { + aho(now * b[i], maxi, cnt + 1, index); + } +} + +main() +{ + int index = generate(); + rep(i, N) ans[i].first = -1; + aho(1, 21, 0, index); + ans[0] = make_pair(10000, 0); + sort(ans, ans + N); + int n; + int tc = 1; + while (scanf("%d", &n) && n) + { + printf("Case %d: %d\n", tc++, ans[n - 1].second); + } +} diff --git a/11354.cpp b/11354.cpp new file mode 100644 index 0000000..c1608b5 --- /dev/null +++ b/11354.cpp @@ -0,0 +1,251 @@ +#include + +using namespace std; + +struct arrest {int x,y,c;}; +arrest graph[200001], tree[200001]; + +int n, m, parent[100001], rank_[100001], mark[100001]; +queue fila; + +// LCA +int T[60000]; +int LV[60000]; +int P[60000][20]; +int COST[60000][20]; + +int compara(const void *a, const void *b) +{ + arrest a1 = *(arrest *)a; + arrest a2 = *(arrest *)b; + return a1.c - a2.c; +} + +int compara_tree(const void *a, const void *b) +{ + arrest a1 = *(arrest *)a; + arrest a2 = *(arrest *)b; + return a1.x - a2.x; +} + +int readdata() +{ + if (scanf("%d%d", &n, &m) != 2) + { + return 0; + } + int i, x, y, d; + for (i = 0; i < m; i++) + { + scanf("%d%d%d", &graph[i].x, &graph[i].y, &graph[i].c); + } + qsort(graph, m, sizeof(graph[0]), compara); + return 1; +} + +int find(int a) +{ + if (parent[a] != a) + { + return find(parent[a]); + } + return a; +} + +int uniao(int a, int b) +{ + int pai1 = find(a); + int pai2 = find(b); + if (pai1 == pai2) + { + return 0; + } + if (rank_[pai1] < rank_[pai2]) + { + parent[pai1] = pai2; + } + else if (rank_[pai1] > rank_[pai2]) + { + parent[pai2] = pai1; + } + else + { + parent[pai2] = pai1; + rank_[pai1]++; + } + return 1; +} + +int pre() +{ + int i, j; + for (i = 1; i <= n; i++) + { + P[i][0] = T[i]; + } + for (j = 1; (1 << j) < n; j++) + { + for (i = 1; i <= n; i++) + { + if (P[i][j - 1] != -1) + { + P[i][j] = P[P[i][j - 1]][j - 1]; + if (P[i][j] != -1) + { + COST[i][j] = max(COST[i][j - 1], COST[P[i][j - 1]][j - 1]); + } + } + } + } + return 0; +} + +int lca(int no1, int no2) +{ + int i, j; + int dist = 0; + if (LV[no1] != LV[no2]) + { + if (LV[no1] < LV[no2]) + { + no1 ^= no2 ^= no1 ^= no2; + } + for (j = 15; j >= 0; j--) + { + if (P[no1][j] != -1) + { + if (LV[P[no1][j]] >= LV[no2]) + { + if (COST[no1][j] > dist) + { + dist = COST[no1][j]; + } + no1 = P[no1][j]; + } + } + } + } + if (no1 == no2) + { + return dist; + } + for (j = 15; j >= 0; j--) + { + if (P[no1][j] != -1 && P[no1][j] != P[no2][j]) + { + if (COST[no1][j] > dist) + { + dist = COST[no1][j]; + } + if (COST[no2][j] > dist) + { + dist = COST[no2][j]; + } + no1 = P[no1][j]; + no2 = P[no2][j]; + } + } + if (no1 == no2) + { + return dist; + } + if (COST[no1][0] > dist) + { + dist = COST[no1][0]; + } + if (COST[no2][0] > dist) + { + dist = COST[no2][0]; + } + return dist; +} + +int proc() +{ + int i, j, k; + while (!fila.empty()) + { + fila.pop(); + } + memset(mark, 0, sizeof(mark)); + memset(P, -1, sizeof(P)); + memset(T, -1, sizeof(T)); + for (i = 1; i <= n; i++) + { + parent[i] = i; + rank_[i] = 0; + } + k = 0; + for (i = 0; i < m; i++) + { + if (uniao(graph[i].x, graph[i].y)) + { + tree[k++] = graph[i]; + tree[k].x = graph[i].y; + tree[k].y = graph[i].x; + tree[k].c = graph[i].c; + k++; + } + } + qsort(tree, k, sizeof(tree[0]), compara_tree); + fila.push(find(1));//pega o representante do conjunto - raiz da arvore + mark[fila.front()] = 1; + T[fila.front()] = -1; + LV[fila.front()] = 0; + COST[fila.front()][0] = 0; + int ini, fim, med; + int no_atual; + while (!fila.empty()) + { + no_atual = fila.front(); + fila.pop(); + ini = 0; + fim = k - 1; + while (ini != fim) + { + med = (ini + fim) / 2; + if (tree[med].x < no_atual) + { + ini = med + 1; + } + else + { + fim = med; + } + } + for (i = ini; i < k && tree[i].x == no_atual; i++) + { + if (!mark[tree[i].y]) + { + T[tree[i].y] = no_atual; + LV[tree[i].y] = LV[no_atual] + 1; + mark[tree[i].y] = 1; + COST[tree[i].y][0] = tree[i].c; + fila.push(tree[i].y); + } + } + } + int q, no1, no2; + scanf("%d", &q); + pre(); + while (q--) + { + scanf("%d%d", &no1, &no2); + printf("%d\n", lca(no1, no2)); + } + return 0; +} + +int main() +{ + int line = 0; + while (readdata()) + { + if (line++) + { + printf("\n"); + } + proc(); + } + return 0; +} diff --git a/11355.cpp b/11355.cpp new file mode 100644 index 0000000..d57ed3f --- /dev/null +++ b/11355.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +// PORCENTAJE VISIBLE DESDE EL CENTRO DE UN CIRCULO; ANTE VARIOS SEGMENTOS :) +// ACLARACIÖN: MISTERIOSAMENTE NO FUNCIONO EL PRODUCTO CRUZ.... + +typedef struct +{ + double x, y; +} point; + +typedef struct +{ + point p1, p2; +} segment; + +int n; +segment seg[101]; +double M = 2 * acos(-1); +double PI = acos(-1); + +#define EPS 1e-9 + +double angulo(point p) +{ + double d = sqrt(p.x * p.x + p.y * p.y); + p.x /= d; + p.y /= d; + double angle = acos(p.x); + if (p.y + EPS < 0.0) + { + angle = 2.0 * PI - angle; + } + return angle; +} +pair inter(segment seg) +{ + return make_pair(angulo(seg.p1), angulo(seg.p2)); +} +int borrar(vector> &vec) +{ + if (vec.size() == 1) + { + return 0; + } + for (int i = 0; i < vec.size(); i++) + { + if (fabs(vec[i].first - vec[i].second) < EPS) + { + vec.erase(vec.begin() + i); + return 1; + } + int next = (i + 1) % vec.size(); + if (next) + { + if (vec[next].first < vec[i].second) + { + vec[i].second = max(vec[i].second, vec[next].second); + vec.erase(vec.begin() + next); + return 1; + } + } + else + { + pair par = vec[next]; + par.first += 2.0 * PI, par.second += 2.0 * PI; + if (par.first < vec[i].second) + { + vec[i].second = max(vec[i].second, par.second); + vec.erase(vec.begin() + next); + return 1; + } + } + } + return 0; +} +int main() +{ + int i, j, k; + int casos; + scanf("%i", &casos); + double R; + for (int h = 0; h < casos; h++) + { + scanf("%i %lf", &n, &R); + for (i = 0; i < n; i++) + { + scanf("%lf %lf %lf %lf", &seg[i].p1.x, &seg[i].p1.y, &seg[i].p2.x, &seg[i].p2.y); + } + vector> intervalo; + for (i = 0; i < n; i++) + { + intervalo.push_back(inter(seg[i])); + } + for (i = 0; i < intervalo.size(); i++) + { + if (intervalo[i].first < intervalo[i].second) + { + if (intervalo[i].second - intervalo[i].first > PI) + { + swap(intervalo[i].first, intervalo[i].second); + } + } + else + { + if (intervalo[i].first - intervalo[i].second < PI) + { + swap(intervalo[i].first, intervalo[i].second); + } + } + } + for (i = 0; i < intervalo.size(); i++) + if (intervalo[i].first > EPS + intervalo[i].second) + { + intervalo[i].second += 2.0 * PI; + } + sort(intervalo.begin(), intervalo.end()); + while (borrar(intervalo)) + ; + double tot = 0.0; + for (i = 0; i < intervalo.size(); i++) + { + tot += intervalo[i].second - intervalo[i].first; + } + tot = min(M, tot); + printf("Case %i: %.2lf%%\n", h + 1, 100.0 * (1.0 - tot / M)); + } + return 0; +} diff --git a/11356.cpp b/11356.cpp new file mode 100644 index 0000000..65b0a8a --- /dev/null +++ b/11356.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +const string mths[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; +int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +static char c; +static int t, date, diff, m, d, y; +static string input; + +inline bool is_leap(int year) +{ + return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)); +} + +inline int mth2int(const string &m) +{ + for (int i = 0; i < 12; i++) + { + if (m == mths[i]) + { + return i + 1; + } + } +} + +inline string int2mth(int m) +{ + return mths[m - 1]; +} + +int main() +{ + scanf("%d", &t); + for (int casenum = 0; casenum < t; casenum++) + { + scanf("%d%c", &y, &c); + getline(cin, input, '-'); + m = mth2int(input); + scanf("%d%d", &d, &diff); + + if (is_leap(y)) + days[1] = 29; + else + days[1] = 28; + + for (int i = 0; i < diff; i++) + { + d++; + if (d > days[m - 1]) + { + d = 1; + m++; + if (m > 12) + { + y++; + m = 1; + if (is_leap(y)) + { + days[1] = 29; + } + else + { + days[1] = 28; + } + } + } + } + printf("Case %d: %0004d-%s-%02d\n", casenum + 1, y, int2mth(m).c_str(), d); + } + return 0; +} diff --git a/11357.cpp b/11357.cpp new file mode 100644 index 0000000..185d6b7 --- /dev/null +++ b/11357.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int casos; +char s[1000]; +int start; +int fim; + +int readdata() +{ + scanf("%s", s); + return 1; +} + +int rec(int ini, int fim) +{ + if (s[ini] == '(') + { + ini++; + } + if (s[fim] == ')') + { + fim--; + } + if (ini > fim) + { + return 0; + } + int i; + int var[30]; + memset(var, 0, sizeof(var)); + char atual; + int st = 0; + for (i = ini; i <= fim;) + { + if (s[i] == '~') + { + i++; + if (var[s[i] - 'a'] == 2) + { + return 0; + } + else + { + var[s[i] - 'a'] = 1; + } + i += 2; + } + else + { + if (var[s[i] - 'a'] == 1) + { + return 0; + } + else + { + var[s[i] - 'a'] = 2; + } + i += 2; + } + } + return 1; +} + +int process() +{ + int i; + int tam = strlen(s); + start = 0; + for (i = 0; i < tam; i++) + { + if (s[i] == '|') + { + fim = i - 1; + if (rec(start, fim)) + { + printf("YES\n"); + return 0; + } + start = i + 1; + } + } + if (rec(start, tam - 1)) + { + printf("YES\n"); + return 0; + } + printf("NO\n"); + return 0; +} + +int main() +{ + int casos; + scanf("%d", &casos); + while (casos--) + { + readdata(); + process(); + } + return 0; +} diff --git a/11358.cpp b/11358.cpp new file mode 100644 index 0000000..b1b9e6a --- /dev/null +++ b/11358.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define N 150 + +int map_[N][N], left_[N], right_[N], lst[N], cap[N], used[N], queue_[N], qflow[N], from[N]; + +int cmp(const void *p, const void *q) +{ + return *(int *)p - *(int *)q; +} + +int main() +{ + int count, n, m, i, j, k, l, f, t, u, v, now; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &m, &n); + memset(map_, 0, sizeof(map_)); + for (i = 1, j = 0; i <= n; i++) + { + scanf("%d%d%d", &left_[i], &map_[0][i], &right_[i]); + lst[j++] = left_[i]; + lst[j++] = right_[i]; + } + qsort(lst, j, sizeof(int), cmp); + for (i = 1, k = 1; i < j; i++) + { + if (lst[i] != lst[i - 1]) + { + u = left_[n + k] = lst[i - 1]; + v = right_[n + k] = lst[i] - 1; + cap[n + k] = v - u + 1; + for (l = 1; l <= n; l++) + { + if (left_[l] <= u && right_[l] > v) + { + map_[l][n + k] = cap[n + k]; + } + } + k++; + } + } + t = n + k; + for (i = n + 1; i < t; i++) + { + map_[i][t] = cap[i] * m; + } + for (;;) + { + memset(used, 0, sizeof(used)); + queue_[0] = 0; + qflow[0] = 1 << 30; + from[0] = -1; + used[0] = 1; + for (i = 0, j = 1; i < j; i++) + { + now = queue_[i]; + f = qflow[i]; + if (!now || now > n) + { + if (map_[now][t]) + { + queue_[j] = t; + qflow[j] = min(map_[now][t], f); + from[j] = i; + break; + } + for (k = 1; k <= n; k++) + { + if (!used[k] && map_[now][k]) + { + used[k] = 1; + queue_[j] = k; + qflow[j] = min(map_[now][k], f); + from[j++] = i; + } + } + } + else + { + for (k = n + 1; k <= t; k++) + { + if (!used[k] && map_[now][k]) + { + used[k] = 1; + queue_[j] = k; + qflow[j] = min(map_[now][k], f); + from[j++] = i; + } + } + } + } + if (i == j) + { + break; + } + else + { + f = qflow[j]; + for (; from[j] != -1; j = from[j]) + { + u = queue_[from[j]]; + v = queue_[j]; + map_[u][v] -= f; + map_[v][u] += f; + } + } + } + for (i = 1; i <= n; i++) + { + if (map_[0][i]) + { + break; + } + } + if (i > n) + { + printf("FEASIBLE\n"); + } + else + { + printf("NO WAY\n"); + } + } + return 0; +} diff --git a/11359.cpp b/11359.cpp new file mode 100644 index 0000000..94f9908 --- /dev/null +++ b/11359.cpp @@ -0,0 +1,266 @@ +#include + +using namespace std; + +#define pode(a, b) (a >= 0 && a < N && m[b][a] != '#') + +int casos; +char m[20][20]; +int N; +int marcado[12][12][20][20][20]; +int caso; + +struct state +{ + int x, y; + int x1, x2, x3; + int time; + int d1, d2, d3; + + state(int x, int y, int x1, int x2, int x3, int d1, int d2, int d3, int time) : x(x), y(y), x1(x1), x2(x2), x3(x3), d1(d1), d2(d2), d3(d3), time(time) {} +}; + +int readdata() +{ + int i; + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%s", m[i]); + } + return 1; +} + +int process() +{ + queue pq; + while (!pq.empty()) + { + pq.pop(); + } + memset(marcado, 0, sizeof(marcado)); + int i, j; + int g1 = -1, g2 = -1, g3 = -1; + int R1, R2, R3; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (m[i][j] == 'X') + { + if (g1 == -1) + { + g1 = j; + R1 = i; + } + else if (g2 == -1) + { + g2 = j; + R2 = i; + } + else + { + g3 = j; + R3 = i; + } + } + } + } + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (m[i][j] == 'S') + { + pq.push(state(i, j, g1, g2, g3, 1, 1, 1, 0)); + i = N; + break; + } + } + } + printf("Case %d: ", caso++); + while (!pq.empty()) + { + state top = pq.front(); + pq.pop(); + int x = top.x; + int y = top.y; + int x1 = top.x1; + int x2 = top.x2; + int x3 = top.x3; + if (x1 == -1) + { + x1 = 19; + } + if (x2 == -1) + { + x2 = 19; + } + if (x3 == -1) + { + x3 = 19; + } + if (marcado[x][y][x1][x2][x3]) + { + continue; + } + marcado[x][y][x1][x2][x3] = 1; + // printf("%d %d %d %d %d\n",x,y,x1,x2,top.time); + if (m[top.x][top.y] == 'T') + { + printf("%d\n", top.time); + return 0; + } + if (x1 != 19) + { + if (pode(x1 + top.d1, R1)) + { + x1 += top.d1; + } + else + { + top.d1 *= -1; + if (pode(x1 + top.d1, R1)) + { + x1 += top.d1; + } + } + } + if (x2 != 19) + { + if (pode(x2 + top.d2, R2)) + { + x2 += top.d2; + } + else + { + top.d2 *= -1; + if (pode(x2 + top.d2, R2)) + { + x2 += top.d2; + } + } + } + if (x3 != 19) + { + if (pode(x3 + top.d3, R3)) + { + x3 += top.d3; + } + else + { + top.d3 *= -1; + if (pode(x3 + top.d3, R3)) + { + x3 += top.d3; + } + } + } + if (top.x1 == -1) + { + x1 = -1; + } + if (top.x2 == -1) + { + x2 = -1; + } + if (top.x3 == -1) + { + x3 = -1; + } + if ((y == x1 && x == R1) || + (y == x2 && x == R2) || + (y == x3 && x == R3)) + { + } + else + { + pq.push(state(x, y, x1, x2, x3, top.d1, top.d2, top.d3, top.time + 1)); + } + if (x > 0) + { + if (m[x - 1][y] != '#') + { + if ((x - 1 == R1 && y == x1) || + (x - 1 == R2 && y == x2) || + (x - 1 == R3 && y == x3)) + { + } + else + { + pq.push(state(x - 1, y, x1, x2, x3, top.d1, top.d2, top.d3, top.time + 1)); + } + } + } + if (y > 0) + { + if (m[x][y - 1] != '#') + { + if ((x == R1 && y - 1 == x1) || + (x == R2 && y - 1 == x2) || + (x == R3 && y - 1 == x3)) + { + } + else if ((x == R1 && y == x1 && top.d1 == 1) || + (x == R2 && y == x2 && top.d2 == 1) || + (x == R3 && y == x3 && top.d3 == 1)) + { + } + else + { + pq.push(state(x, y - 1, x1, x2, x3, top.d1, top.d2, top.d3, top.time + 1)); + } + } + } + if (x < N - 1) + { + if (m[x + 1][y] != '#') + { + if ((x + 1 == R1 && y == x1) || + (x + 1 == R2 && y == x2) || + (x + 1 == R3 && y == x3)) + { + } + else + { + pq.push(state(x + 1, y, x1, x2, x3, top.d1, top.d2, top.d3, top.time + 1)); + } + } + } + if (y < N - 1) + { + if (m[x][y + 1] != '#') + { + if ((x == R1 && y + 1 == x1) || + (x == R2 && y + 1 == x2) || + (x == R3 && y + 1 == x3)) + { + } + else if ((x == R1 && y == x1 && top.d1 == -1) || + (x == R2 && y == x2 && top.d2 == -1) || + (x == R3 && y == x3 && top.d3 == -1)) + { + } + else + { + pq.push(state(x, y + 1, x1, x2, x3, top.d1, top.d2, top.d3, top.time + 1)); + } + } + } + } + printf("-1\n"); + return 0; +} + +int main() +{ + int casos; + scanf("%d", &casos); + caso = 1; + while (casos--) + { + readdata(); + process(); + } + return 0; +} diff --git a/11360.cpp b/11360.cpp index 200d356..0285abf 100644 --- a/11360.cpp +++ b/11360.cpp @@ -1,85 +1,102 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - int n; - cin>>n; - int mat[20][20]={0}; - for(int j=0;j>no; - for(int k=n-1;k>=0;k--){ - mat[j][k]=no%10; - no/=10; - } - } - - int m=0; - cin>>m; - for(int x=1;x<=m;x++){ - string str; - cin>>str; - if(str=="row"){ - int a,b; - cin>>a>>b; - for(int j=0;j>a>>b; - for(int k=0;k=10) - mat[j][k]-=10; - } - } - } - if(str=="dec"){ - for(int j=0;j + +using namespace std; + +int tmp[10][10]; +int matrix[10][10]; + +void transpose(int n) +{ + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + tmp[i][j] = matrix[i][j]; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + matrix[i][j] = tmp[j][i]; + } + } +} + +int main() +{ + int t, n, m, a, b; + char c; + string instruction; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n; + for (a = 0; a < n; a++) + { + for (b = 0; b < n; b++) + { + cin >> c; + matrix[a][b] = c - '0'; + } + } + cin >> m; + while (m--) + { + cin >> instruction; + if (instruction == "row") + { + cin >> a >> b; + a--, b--; + for (int i = 0; i < n; i++) + { + swap(matrix[a][i], matrix[b][i]); + } + } + else if (instruction == "col") + { + cin >> a >> b; + a--, b--; + for (int i = 0; i < n; i++) + { + swap(matrix[i][a], matrix[i][b]); + } + } + else if (instruction == "inc") + { + for (a = 0; a < n; a++) + { + for (b = 0; b < n; b++) + { + matrix[a][b] = (matrix[a][b] + 1) % 10; + } + } + } + else if (instruction == "dec") + { + for (a = 0; a < n; a++) + { + for (b = 0; b < n; b++) + { + matrix[a][b] = (matrix[a][b] + 9) % 10; + } + } + } + else if (instruction == "transpose") + { + transpose(n); + } + } + cout << "Case #" << casenum + 1 << endl; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cout << matrix[i][j]; + } + cout << endl; + } + cout << endl; + } + return 0; +} diff --git a/11361.cpp b/11361.cpp new file mode 100644 index 0000000..b7b0165 --- /dev/null +++ b/11361.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +const int K = 100; +int dp[10][K][K][2]; + +int solve(int keta, int modall, int modnow, int isbound, + string &in, int mod) +{ + if (keta == in.size()) + { + return modall == 0 && modnow == 0; + } + int &ret = dp[keta][modall][modnow][isbound]; + if (ret == -1) + { + ret = 0; + rep(i, 10) + { + int allnext = (modall + i) % mod, nownext = (modnow * 10 + i) % mod; + if (isbound) + { + if (i > in[keta] - '0') + { + continue; + } + else + ret += solve(keta + 1, allnext, nownext, i == in[keta] - '0' ? 1 : 0, + in, mod); + } + else + { + ret += solve(keta + 1, allnext, nownext, isbound, in, mod); + } + } + } + return ret; +} + +void init() +{ + rep(i, 10) rep(j, K) rep(k, K) rep(l, 2) dp[i][j][k][l] = -1; +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int a, b, k; + cin >> a >> b >> k; + a--; + int ans; + if (k > 100) + { + //ans=b/k-a/k; + ans = 0; + } + else + { + //all value is zero is counted. + stringstream sin; + int ta, tb; + string t; + sin << a; + sin >> t; + init(); + ta = solve(0, 0, 0, 1, t, k); + stringstream sin2; + sin2 << b; + sin2 >> t; + init(); + tb = solve(0, 0, 0, 1, t, k); + ans = tb - ta; + } + cout << ans << endl; + } + return false; +} diff --git a/11362.cpp b/11362.cpp index e93e2c3..b9166ad 100644 --- a/11362.cpp +++ b/11362.cpp @@ -1,66 +1,61 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int N, i, j; - cin>>N; - string phone[N]; - bool stat = true; - size_t found; - for(i=0; i>phone[i]; - } - sort(phone, phone+N); - for(i=0; i= phone[j].length()) - continue; - found = phone[j].find(phone[i]); - if(found == 0){ - stat = false; - break; - } - } - if(!stat) - break; - } - if(stat) - printf("YES\n"); - else - printf("NO\n"); - } - return 0; -} +#include + +using namespace std; + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +string nums[10000 + 10]; + +bool prefix_check(string a, string b) +{ + if (b.size() < a.size()) + { + return false; + } + for (size_t i = 0; i < a.length(); i++) + { + if (a[i] != b[i]) + { + return false; + } + } + return true; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + sort(nums, nums + n); + bool flag = true; + for (int i = 0; i < n - 1; i++) + { + if (prefix_check(nums[i], nums[i + 1])) + { + flag = false; + break; + } + } + if (flag) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/11363.cpp b/11363.cpp new file mode 100644 index 0000000..e55b85e --- /dev/null +++ b/11363.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +#define LIM 10010 + +int N, M, cases, i, error, r1, r2, h1, h2, root[LIM], rank_[LIM], countH[LIM], countW[LIM]; + +int findSet(int a) +{ + if (root[a] != a) + { + root[a] = findSet(root[a]); + } + return root[a]; +} +int unionSet(int a, int b) +{ + if (rank_[a] < rank_[b]) + { + root[a] = b; + countH[b] += countH[a]; + countW[b] += countW[a]; + return b; + } + else + { + root[b] = a; + countH[a] += countH[b]; + countW[a] += countW[b]; + if (rank_[a] == rank_[b]) + { + rank_[a]++; + } + return a; + } +} + +int main() +{ + scanf("%d", &cases); + while (cases--) + { + scanf("%d %d", &M, &N); + for (i = 0; i < N; i++) + { + root[i] = i; + rank_[i] = 0; + countH[i] = 1; + countW[i] = 0; + } + error = 0; + for (i = 0; i < M; i++) + { + scanf("%d %d", &h1, &h2); + r1 = findSet(h1); + r2 = findSet(h2); + if (r1 != r2) + { + r1 = unionSet(r1, r2); + } + countW[r1]++; + if (countW[r1] > countH[r1]) + { + error = 1; + } + } + if (error == 1) + { + printf("rehash necessary\n"); + } + else + { + printf("successful hashing\n"); + } + } + return 0; +} diff --git a/11364.cpp b/11364.cpp new file mode 100644 index 0000000..7d61d9f --- /dev/null +++ b/11364.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int cases; + int n; + int shops[21]; + cin >> cases; + while (cases--) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> shops[i]; + } + sort(shops, shops + n); + cout << (shops[0] + shops[n - 1]) / 2 - shops[0] + shops[n - 1] - shops[0] + shops[n - 1] - (shops[0] + shops[n - 1]) / 2 << endl; + } + return 0; +} diff --git a/11366.cpp b/11366.cpp new file mode 100644 index 0000000..0b411d9 --- /dev/null +++ b/11366.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int mm[6] = {100, 50, 20, 10, 5, 1}; +int temp[5][10]; +int coins[5][10]; +int fim[5]; +int qt[10]; +int best; +int read() +{ + int ab, bc, ca; + int inc[5]; + scanf("%d %d %d", &ab, &bc, &ca); + inc[0] = ca - ab; + inc[1] = ab - bc; + inc[2] = bc - ca; + memset(qt, 0, sizeof(qt)); + int i, j; + for (i = 0; i < 3; i++) + { + fim[i] = 0; + for (j = 0; j < 6; j++) + { + scanf("%d", &coins[i][j]); + fim[i] += mm[j] * coins[i][j]; + qt[j] += coins[i][j]; + } + fim[i] += inc[i]; + } + return 1; +} +int rec(int p, int c, int val) +{ + if (p == 2) + { + int i, ret = 0; + for (i = 0; i < 6; i++) + { + ret += abs(coins[0][i] - temp[0][i]) + abs(coins[1][i] - temp[1][i]) + abs(coins[2][i] - qt[i]); + } + ret /= 2; + if (ret < best) + { + best = ret; + } + return 0; + } + if (c == 6) + { + if (val == fim[p]) + { + rec(p + 1, 0, 0); + } + return 0; + } + if (val > fim[p]) + { + return 0; + } + int i; + for (i = 0; i <= qt[c]; i++) + { + qt[c] -= i; + temp[p][c] = i; + rec(p, c + 1, val + i * mm[c]); + qt[c] += i; + } + return 0; +} +int process() +{ + best = 100000000; + rec(0, 0, 0); + if (best == 100000000) + { + printf("impossible\n"); + } + else + { + printf("%d\n", best); + } + return 0; +} +int main() +{ + int casos; + scanf("%d", &casos); + while (casos--) + { + read(); + process(); + } + return 0; +} diff --git a/11367.cpp b/11367.cpp new file mode 100644 index 0000000..67030d9 --- /dev/null +++ b/11367.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +/* + Accepted +*/ +//#include + +const int MAXN = 1000, MAXC = 100; + +struct edge +{ + int i, g, w; + edge() {} + edge(int I, int G, int W) : i(I), g(G), w(W) {} + bool operator<(const edge &that) const + { + return w > that.w; + } +}; + +int p[MAXN], d[MAXN][MAXC + 1], n; +vector g[MAXN]; + +int dijkstra(const int &start, const int &end, const int &c) +{ + for (int i = 0; i < n; ++i) + for (int j = 0; j <= c; ++j) + { + d[i][j] = INT_MAX; + } + priority_queue q; + q.push(edge(start, 0, 0)); + d[start][0] = 0; + while (q.size()) + { + edge u = q.top(); + q.pop(); + //printf("popped <%d, %d, %d>\n", u.i, u.g, u.w); + if (u.i == end) + { + return u.w; + } + if (d[u.i][u.g] < u.w) + { + continue; + } + //I can buy another gallon and stay where I am + if (u.g < c && u.w + p[u.i] < d[u.i][u.g + 1]) + { + d[u.i][u.g + 1] = u.w + p[u.i]; + q.push(edge(u.i, u.g + 1, u.w + p[u.i])); + } + //Now try to reach each other node as long as I have enough gas + vector &v = g[u.i]; + for (int j = 0; j < v.size(); ++j) + { + int distance = v[j].w; + int neighbor = v[j].i; + if (u.g >= distance) + { + int new_gas = u.g - distance; + if (u.w < d[neighbor][new_gas]) + { + d[neighbor][new_gas] = u.w; + q.push(edge(neighbor, new_gas, u.w)); + } + } + } + } + return INT_MAX; +} + +int main() +{ + int m; + scanf("%d %d", &n, &m); + for (int i = 0; i < n; ++i) + { + scanf("%d", &p[i]); + } + while (m--) + { + int u, v, d; + scanf("%d %d %d", &u, &v, &d); + g[u].push_back(edge(v, 0, d)); + g[v].push_back(edge(u, 0, d)); + } + int q; + scanf("%d", &q); + while (q--) + { + int c, s, e; + scanf("%d %d %d", &c, &s, &e); + int t = dijkstra(s, e, c); + if (t < INT_MAX) + { + printf("%d\n", t); + } + else + { + printf("impossible\n"); + } + } + return 0; +} diff --git a/11368.cpp b/11368.cpp new file mode 100644 index 0000000..bf32ad4 --- /dev/null +++ b/11368.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define MAXM (20001) + +struct dim +{ + int w, h; +}; + +struct dim dims[MAXM]; +int size[MAXM + 1]; +int anti_chain_size; + +int cmp(const void *a, const void *b) +{ + struct dim *ta = ((struct dim *)a); + struct dim *tb = ((struct dim *)b); + if (ta->w != tb->w) + { + return ta->w - tb->w; + } + return tb->h - ta->h; +} + +int main() +{ + int n, m, i, j; + scanf("%d", &n); + while (n--) + { + scanf("%d", &m); + for (i = 0; i < m; i++) + { + scanf("%d %d", &dims[i].w, &dims[i].h); + } + qsort(dims, m, sizeof(struct dim), cmp); + anti_chain_size = 0; + for (i = 0; i < m; i++) + { + int lo = 0, hi = anti_chain_size; + while (lo < hi) + { + int mid = (hi + lo) / 2; + if (size[mid] >= dims[i].h) + { + lo = mid + 1; + } + else + { + hi = mid; + } + } + size[lo] = dims[i].h; + anti_chain_size += (lo == anti_chain_size); + } + printf("%d\n", anti_chain_size); + } + return 0; +} diff --git a/11369.cpp b/11369.cpp index 6bc401e..8e46a19 100644 --- a/11369.cpp +++ b/11369.cpp @@ -1,54 +1,28 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -bool comp(int A, int B){ - return A >= B; -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while( T-- ){ - int N; - cin >> N; - int price[N]; - int i, profit = 0; - FOI(i, 0, N-1) - cin >> price[i]; - sort(price, price + N, comp); - for (i = 2; i < N; i += 3) - profit += price[i]; - cout << profit << endl; +int main() +{ + int t, n; + int result; + int *p = new int[20000]; + cin >> t; + while (t--) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> p[i]; + } + sort(p, p + n, greater()); + result = 0; + for (int i = 2; i < n; i += 3) + { + result += p[i]; + } + cout << result << endl; } + delete[] p; return 0; } - diff --git a/1137.cpp b/1137.cpp new file mode 100644 index 0000000..4b70c61 --- /dev/null +++ b/1137.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define maxL (50000 >> 5) + 1 +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) +long long P[10000]; +int Pt = 0; +void sieve() +{ + int mark[maxL]; + register int i, j, k; + SET(1); + int n = 50000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} + +#define MAXN 1048576 +int used[MAXN], val[MAXN], fcnt[MAXN]; +int main() +{ + sieve(); + long long L, R; + int cases = 0; + while (scanf("%lld %lld", &L, &R) == 2) + { + if (cases++) + puts(""); + int has = 0; + for (int i = R - L; i >= 0; i--) + val[i] = i + L, fcnt[i] = 0; + for (int i = 0; i < Pt && P[i] * P[i] <= R; i++) + { + int d = P[i], d2 = P[i] * P[i]; + for (long long j = ((L - 1) / d2 + 1) * d2; j <= R; j += d2) + used[j - L] = cases;// check distinct prime factors + for (long long j = ((L - 1) / d + 1) * d; j <= R; j += d) + { + if ((j - 1) % (d - 1)) + used[j - L] = cases; + fcnt[j - L]++, val[j - L] /= d; + } + } + for (int i = 0; i <= R - L; i++) + fcnt[i] += val[i] != 1; + for (int i = 0; i <= R - L; i++) + { + if (used[i] != cases && fcnt[i] >= 3) + { + if (val[i] == 1 || (i + L - 1) % (val[i] - 1) == 0) + printf("%d\n", i + L), has = 1; + } + } + if (!has) + puts("none"); + } + return 0; +} diff --git a/11370.cpp b/11370.cpp new file mode 100644 index 0000000..349b76c --- /dev/null +++ b/11370.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#ifndef INFINITY +#define INFINITY 1e300 +#endif + +const int MAX_h = 1000; + +double X[MAX_h]; // Coordinates of houses +double Me[MAX_h + 1][MAX_h + 1];// Memoisation of error() +double Ms[MAX_h + 1][MAX_h + 1];// Memoisation of solve() + +double error(int i, int j) +{ + if (Me[i][j] != -1.0) + { + return Me[i][j]; + } + double e = 0.0; + for (int k = i + 1; k < j; k++) + { + e += abs(X[k] - (X[i] + (k - i) * (X[j] - X[i]) / (j - i))); + } + return Me[i][j] = e; +} + +double solve(int h, int c) +{ + if (Ms[h][c] != -1.0) + { + return Ms[h][c]; + } + if (c == 2) + { + return Ms[h][c] = error(0, h - 1); + } + else + { + double mine = INFINITY; + for (int j = c - 2; j < h - 1; j++) + { + mine = min(mine, solve(j + 1, c - 1) + error(j, h - 1)); + } + return Ms[h][c] = mine; + } +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + int h, c; + scanf("%d %d", &h, &c); + for (int i = 0; i < h; i++) + { + scanf("%lf", &X[i]); + } + for (int i = 0; i <= h; i++) + { + for (int j = 0; j <= h; j++) + { + Me[i][j] = Ms[i][j] = -1.0; + } + } + printf("%.4lf\n", solve(h, c) / h); + } +} diff --git a/11371.cpp b/11371.cpp index 18cf152..3251b16 100644 --- a/11371.cpp +++ b/11371.cpp @@ -1,33 +1,26 @@ -#include -#include -using namespace std; - -int main(){ - long long n; - while(cin>>n){ - int i=0,no[50]={0}; - while(n>0){ - int rem=n%10; - no[i]=rem; - n/=10; - i++; - } - for(int j=0;jno[k]){ - int temp=no[j]; - no[j]=no[k]; - no[k]=temp; - } - } - } - long long a=0,b=0; - for(int j=0;j + +using namespace std; + +char a[15], b[15]; + +int main() +{ + while (gets(a)) + { + int len = strlen(a); + sort(a, a + len, greater()); + memcpy(b, a, sizeof(a)); + int i; + for (i = 0; i < len; ++i) + { + a[i] = b[len - 1 - i]; + } + for (i = 0; !(a[i] & 15); ++i) + ; + swap(a[0], a[i]); + long long diff = atoll(b) - atoll(a); + printf("%s - %s = %lld = 9 * %lld\n", b, a, diff, diff / 9); + } + return 0; +} diff --git a/11372.cpp b/11372.cpp new file mode 100644 index 0000000..94f9319 --- /dev/null +++ b/11372.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +struct Problem +{ + int no; + int type; + int favor; +} p; +Problem cat[3][200]; +int sets[3], n, choose[4], count[3], ans[7], t[7], bestfavor; +char name[30], cate[2], diff[2]; + +int compare(const void *a, const void *b) +{ + Problem *c = (Problem *)a, *d = (Problem *)b; + if (c->favor != d->favor) + { + return d->favor - c->favor; + } + else + { + return c->no - d->no; + } +} + +void DFS(int depth, int favor) +{ + if (depth == 3) + { + if (!(choose[0] >= 2 && choose[1] >= 1 && choose[2] >= 1)) + return; + if (favor < bestfavor) + return; + int temp[7]; + for (int i = 0; i < 6; i++) + temp[i] = t[i]; + for (int i = 0; i < 6; i++) + for (int j = i + 1; j < 6; j++) + if (temp[i] > temp[j]) + { + int t1 = temp[i]; + temp[i] = temp[j]; + temp[j] = t1; + } + if (favor > bestfavor) + for (int i = 0; i < 6; i++) + ans[i] = temp[i]; + else + { + bool update = false; + for (int i = 0; i < 6; i++) + if (temp[i] < ans[i]) + update = true; + else if (temp[i] > ans[i]) + break; + if (update) + for (int i = 0; i < 6; i++) + ans[i] = temp[i]; + } + bestfavor = favor; + } + else + { + for (int i = 0; i < sets[depth] && i < 8; i++) + { + Problem &p1 = cat[depth][i]; + choose[p1.type]++; + t[2 * depth] = p1.no; + for (int j = i + 1; j < sets[depth] && j < 8; j++) + { + Problem &p2 = cat[depth][j]; + choose[p2.type]++; + t[2 * depth + 1] = p2.no; + DFS(depth + 1, favor + p1.favor + p2.favor); + choose[p2.type]--; + } + choose[p1.type]--; + } + } +} + +int main() +{ + int n; + while (scanf("%d", &n), n) + { + for (int i = 0; i < 3; i++) + sets[i] = 0; + + for (int i = 0; i < n; i++) + { + scanf("%03d %s %s %s %d", &p.no, name, diff, cate, &p.favor); + if (cate[0] == 'D') + p.type = 0; + else if (cate[0] == 'M') + p.type = 1; + else if (cate[0] == 'G') + p.type = 2; + else + p.type = 3; + + if (diff[0] == 'E') + cat[0][sets[0]++] = p; + else if (diff[0] == 'M') + cat[1][sets[1]++] = p; + else + cat[2][sets[2]++] = p; + } + + for (int i = 0; i < 3; i++) + qsort(cat[i], sets[i], sizeof(Problem), compare); + for (int i = 0; i < 4; i++) + choose[i] = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 4; j++) + choose[j] = 0; + int sols = 0; + for (int j = 0; j < sets[i]; j++) + { + Problem &p = cat[i][j]; + int type = p.type; + if (choose[type] >= 2) + continue; + choose[type]++; + cat[i][sols++] = p; + } + sets[i] = sols; + } + for (int i = 0; i < 4; i++) + choose[i] = 0; + bestfavor = 0; + DFS(0, 0); + if (bestfavor > 0) + { + for (int i = 0; i < 6; i++) + { + if (i) + putchar(' '); + printf("%03d", ans[i]); + } + puts(""); + } + else + puts("No solution."); + } + return 0; +} diff --git a/11374.cpp b/11374.cpp new file mode 100644 index 0000000..ca594b3 --- /dev/null +++ b/11374.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define INF 99999999 + +static int N, S, E, M, K; +static int graph[501][2], p[501][2], Comm[1000][3], Exp[1000][3]; + +inline void ShortestPath() +{ + for (int i = 1; i <= N; i++) + for (int j = 0; j < 2; j++) + graph[i][j] = INF; + graph[S][0] = 0; + p[S][0] = -1; + while (true) + { + int min = INF, node, state; + for (int i = 1; i <= N; i++) + for (int j = 0; j < 2; j++) + if (graph[i][j] != -1 && graph[i][j] < min) + { + min = graph[i][j]; + node = i; + state = j; + } + if (node == E) + { + int path[500], len = 0, t = E, ts = state, station = -1; + bool useticket = (state == 1); + while (t > 0) + { + path[len++] = t; + int pn = p[t][ts] >> 1, ps = p[t][ts] & 1; + if (useticket && ps == 0 && station == -1) + { + station = pn; + } + t = pn; + ts = ps; + } + for (int i = len - 1; i >= 0; i--) + { + if (i != len - 1) + { + putchar(' '); + } + printf("%d", path[i]); + } + puts(""); + if (useticket) + { + printf("%d\n", station); + } + else + { + puts("Ticket Not Used"); + } + printf("%d\n", min); + break; + } + for (int i = 0; i < M; i++) + { + if (Comm[i][0] == node && graph[Comm[i][1]][state] > min + Comm[i][2]) + { + graph[Comm[i][1]][state] = min + Comm[i][2]; + p[Comm[i][1]][state] = (node << 1) | state; + } + if (Comm[i][1] == node && graph[Comm[i][0]][state] > min + Comm[i][2]) + { + graph[Comm[i][0]][state] = min + Comm[i][2]; + p[Comm[i][0]][state] = (node << 1) | state; + } + } + if (state == 0) + { + for (int i = 0; i < K; i++) + { + if (Exp[i][0] == node && graph[Exp[i][1]][1] > min + Exp[i][2]) + { + graph[Exp[i][1]][1] = min + Exp[i][2]; + p[Exp[i][1]][1] = (node << 1) | state; + } + if (Exp[i][1] == node && graph[Exp[i][0]][1] > min + Exp[i][2]) + { + graph[Exp[i][0]][1] = min + Exp[i][2]; + p[Exp[i][0]][1] = (node << 1) | state; + } + } + } + graph[node][state] = -1; + } +} + +int main() +{ + int cases = 0; + while (scanf("%d %d %d", &N, &S, &E) == 3) + { + if (cases++) + puts(""); + scanf("%d", &M); + for (int i = 0; i < M; i++) + scanf("%d %d %d", &Comm[i][0], &Comm[i][1], &Comm[i][2]); + scanf("%d", &K); + for (int i = 0; i < K; i++) + scanf("%d %d %d", &Exp[i][0], &Exp[i][1], &Exp[i][2]); + ShortestPath(); + } + return 0; +} diff --git a/11375.cpp b/11375.cpp new file mode 100644 index 0000000..bc30cb8 --- /dev/null +++ b/11375.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) + +const int DIGITS = 70; +const int BASE = 100000000; +const int LOG10BASE = 8; +struct BigInt +{ + int num[DIGITS], last; + + BigInt(int v = 0) + { + clear(); + num[0] = v; + last = 1; + carry(); + } + BigInt(BigInt *b) + { + FOR(i, 0, b->last) + num[i] = b->num[i]; + } + + inline void clear() + { + FOR(i, 0, DIGITS) + num[i] = 0; + } + inline void carry() + { + int c = 0; + FOR(i, 0, last) + { + c = (num[i] += c) / BASE; + num[i] %= BASE; + } + while (c) + { + num[last++] = c % BASE; + c /= BASE; + } + } + inline void show() + { + int i = last; + printf("%d", num[--i]); + while (i--) + printf("%0*d", LOG10BASE, num[i]); + puts(""); + } + inline string to_string() + { + string res; + + int i = last; + stringstream ss; + ss << num[--i]; + string str = ss.str(); + res += str; + + while (i--) + { + char tmp[LOG10BASE + 1]; + sprintf(tmp, "%0*d", LOG10BASE, num[i]); + res += tmp; + } + + return res; + } + + BigInt operator+=(const BigInt &b) + { + last = max(last, b.last); + FOR(i, 0, last) + num[i] += b.num[i]; + carry(); + return *this; + } + BigInt operator++(const int b) + { + return *this += 1; + } +}; + +#define MAXN 2000 + +const int mxs[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; + +static BigInt dp[MAXN + 5]; + +int main() +{ + // init + FOREQ(i, 0, MAXN) + dp[i] = 0; + + // base case: 1-9 (not 0) + // dp[2] = dp[3] = dp[4] = dp[7] = 1, dp[6] = 2, dp[5] = 3; + FOREQ(i, 1, 9) + dp[mxs[i]] += 1; + + // recursive + /* + FOR(i, 2, 2001) + { + FOR(j, 0, 10) + dp[i+mxs[j]] += dp[i]; + dp[i] += i == 6; + dp[i] += dp[i-1]; + } +*/ + FOREQ(i, 1, MAXN) + FOREQ(j, 0, 9) + dp[i + mxs[j]] += dp[i]; + dp[6]++; + FOREQ(i, 1, MAXN) + dp[i] += dp[i - 1]; + + // answer query + for (int n; scanf("%d", &n) == 1; dp[n].show()) + ; + + return 0; +} diff --git a/11376.cpp b/11376.cpp new file mode 100644 index 0000000..5adf561 --- /dev/null +++ b/11376.cpp @@ -0,0 +1,254 @@ +#include + +using namespace std; + +#define foreach(it, v) for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) + +char dirName[] = "ENSW"; +int dirR[4] = {0, -1, 1, 0}, dirC[4] = {1, 0, 0, -1}, dirMask[4] = {4, 8, 2, 1}; + +int N, wall[16][16], startR, startC, startPos; +int K, blueR[50], blueC[50], bluePos[50], blue[256]; +int posR[256], posC[256], jumpTo[256][4], jumpMask[256][4]; +int dist[256][256]; +int estimateX[101][32], estimateXY[101][32][32]; + +bool valid(int r, int c) +{ + return 0 <= r && r < N && 0 <= c && c < N; +} + +struct State +{ + int pos; + int visited; +}; + +int IDA_limit, IDA_next, IDA_iter; +char path[1000]; + +map seen; + +bool dfs(State cur, int depth) +{ + State next; + int estimate, i; + ++IDA_iter; + if (blue[cur.pos] >= 0) + { + cur.visited |= 1 << blue[cur.pos]; + } + if (cur.visited == (1 << K) - 1) + { + path[depth] = 0; + return true; + } + estimate = 1; + for (i = 0; i < K; i++) + { + if ((cur.visited & (1 << i)) != 0) + { + continue; + } + estimate = max(estimate, estimateX[cur.pos][i]); + } + if (depth + estimate > IDA_limit) + { + IDA_next = min(IDA_next, depth + estimate); + return false; + } + i = (cur.visited << 7) | cur.pos; + if (seen.count(i) != 0) + { + if (seen[i] <= depth) + { + return false; + } + } + seen[i] = depth; + for (i = 0; i < 4; i++) + { + next.pos = jumpTo[cur.pos][i]; + if (next.pos == cur.pos) + { + continue; + } + next.visited = cur.visited | jumpMask[cur.pos][i]; + path[depth] = dirName[i]; + if (dfs(next, depth + 1)) + { + return true; + } + } + return false; +} + +void solve() +{ + if (K == 0) + { + printf("\n"); + return; + } + memset(dist, 0x1f, sizeof(dist)); + for (int i = 0; i < N * N; i++) + { + posR[i] = i / N; + posC[i] = i % N; + for (int d = 0; d < 4; d++) + { + jumpMask[i][d] = 0; + if (blue[i] >= 0) + { + jumpMask[i][d] |= 1 << blue[i]; + } + int r = posR[i], c = posC[i]; + while (valid(r + dirR[d], c + dirC[d]) && (wall[r][c] & dirMask[d]) == 0) + { + r += dirR[d]; + c += dirC[d]; + int j = r * N + c; + if (blue[j] >= 0) + { + jumpMask[i][d] |= 1 << blue[j]; + } + } + jumpTo[i][d] = r * N + c; + dist[i][r * N + c] = 1; + } + } + for (int k = 0; k < N * N; k++) + { + dist[k][k] = 0; + for (int i = 0; i < N * N; i++) + { + for (int j = 0; j < N * N; j++) + { + dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); + } + } + } + for (int pos = 0; pos < N * N; pos++) + { + for (int x = 0; x < K; x++) + { + estimateX[pos][x] = 0x1fffffff; + for (int j = 0; j < N * N; j++) + for (int d = 0; d < 4; d++) + if ((jumpMask[j][d] & (1 << x)) != 0) + { + estimateX[pos][x] = min(estimateX[pos][x], dist[pos][j] + (blue[j] == x ? 0 : 1)); + } + } + } + IDA_iter = 0; + IDA_limit = 1; + while (true) + { + State s; + s.pos = startPos; + s.visited = 0; + seen.clear(); + IDA_next = INT_MAX; + //printf("Current limit: %d\n", IDA_limit); + if (dfs(s, 0)) + { + break; + } + assert(IDA_next < 1000000); + assert(IDA_limit < 53); + IDA_limit = min(53, IDA_next); + } + //printf("Found path in total %d iterations\n", IDA_iter); + printf("%s\n", path); +} + +int main() +{ + char buf[10000]; + buf[0] = 0; + gets(buf); + while (true) + { + while (true) + { + N = 0; + if (sscanf(buf, "%d", &N) == 1) + { + break; + } + if (gets(buf) == NULL) + { + return 0; + } + } + if (N == 0) + { + return 0; + } + assert(2 <= N && N <= 10); + for (int i = 0; i < N; i++) + { + { + char *s = gets(buf); + assert(s != NULL); + } + for (int j = 0; j < N; j++) + { + char c = toupper(buf[j]); + if (isalpha(c)) + { + assert('A' <= c && c <= 'F'); + wall[i][j] = toupper(c) - 'A' + 10; + } + else + { + assert('0' <= c && c <= '9'); + wall[i][j] = c - '0'; + } + } + } + { + char *s = gets(buf); + assert(s != NULL); + } + { + int i = sscanf(buf, "%d %d", &startR, &startC); + assert(i == 2); + } + --startR; + --startC; + assert(valid(startR, startC)); + startPos = startR * N + startC; + K = 0; + memset(blue, 0xff, sizeof(blue)); + while (true) + { + buf[0] = 0; + if (gets(buf) == NULL) + { + break; + } + if (sscanf(buf, "%d %d", &blueR[K], &blueC[K]) != 2) + { + break; + } + if (blueR[K] == 0 || blueC[K] == 0) + { + break; + } + --blueR[K]; + --blueC[K]; + assert(valid(blueR[K], blueC[K])); + bluePos[K] = blueR[K] * N + blueC[K]; + if (blue[bluePos[K]] < 0) + { + blue[bluePos[K]] = K; + K++; + assert(K <= 25); + } + } + solve(); + } + return 0; +} diff --git a/11377.cpp b/11377.cpp new file mode 100644 index 0000000..778923e --- /dev/null +++ b/11377.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/113/11377.html +// Runtime: 0.076s +// Tag: BFS, shortest path + +#define INF 2147483647 +#define N 2000 + 5 + +bool isAirport[N]; +vector matrix[N]; +int hop[N]; + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + memset(isAirport, false, sizeof isAirport); + for (int i = 0; i < N; i++) + { + matrix[i].clear(); + } + int n, m, k; + scanf("%d %d %d", &n, &m, &k); + for (int i = 0; i < k; i++) + { + int inp; + scanf("%d", &inp); + isAirport[inp] = true; + } + for (int i = 0; i < m; i++) + { + int a, b; + scanf("%d %d", &a, &b); + matrix[a].push_back(b); + matrix[b].push_back(a); + } + queue q; + int query; + scanf("%d", &query); + printf("Case %d:\n", ++cases); + while (query--) + { + int a, b; + scanf("%d %d", &a, &b); + if (a == b) + { + printf("0\n"); + continue; + } + for (int i = 0; i < N; i++) + { + hop[i] = INF; + } + q.push(a); + if (isAirport[a]) + { + hop[a] = 0; + } + else + { + hop[a] = 1; + } + while (!q.empty()) + { + int p = q.front(); + q.pop(); + for (size_t i = 0; i < matrix[p].size(); i++) + { + int tmp = isAirport[matrix[p][i]] ? hop[p] : hop[p] + 1; + if (hop[matrix[p][i]] > tmp) + { + hop[matrix[p][i]] = tmp; + q.push(matrix[p][i]); + } + } + } + if (hop[b] == INF) + { + printf("-1\n"); + } + else + { + printf("%d\n", hop[b]); + } + } + printf("\n"); + } + return 0; +} + +/* + 1 + 3 1 1 + 2 + 1 3 + 1 + 1 3 + * + Case 1: + 2 + + */ diff --git a/11378.cpp b/11378.cpp new file mode 100644 index 0000000..e6cdc53 --- /dev/null +++ b/11378.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define INF 99999999 +int n; +struct PONTO +{ + int x, y; +}; +PONTO p[200000]; +int readdata() +{ + if (scanf("%d", &n) != 1) + { + return 0; + } + int i; + for (i = 0; i < n; i++) + { + scanf("%d %d", &p[i].x, &p[i].y); + } + return 1; +} +int maximo(int a, int b) +{ + if (a > b) + { + return a; + } + return b; +} +int dist(PONTO p1, PONTO p2) +{ + return maximo(abs(p1.x - p2.x), abs(p1.y - p2.y)); +} +int compara(const void *a, const void *b) +{ + PONTO p1 = *(PONTO *)a; + PONTO p2 = *(PONTO *)b; + if (p1.x == p2.x) + { + return p1.y - p2.y; + } + return p1.x - p2.x; +} +int rec(int ini, int fim) +{ + if (fim - ini == 0) + { + return INF; + } + if (fim - ini == 1) + { + return dist(p[ini], p[fim]); + } + int med = (ini + fim) / 2; + int min1 = rec(ini, med); + int min2 = rec(med + 1, fim); + int best = min1; + if (min2 < best) + { + best = min2; + } + int i, j, k; + for (i = med; i >= 0; i--) + { + if (p[med].x - p[i].x >= best) + { + break; + } + for (j = med + 1; j <= fim && j < med + 8; j++) + { + if (p[j].x - p[i].x < best) + { + k = dist(p[j], p[i]); + if (k < best) + { + best = k; + } + } + else + { + break; + } + } + } + return best; +} +int process() +{ + qsort(p, n, sizeof(p[0]), compara); + int v = rec(0, n - 1); + printf("%d\n", v); + return 0; +} +int main() +{ + while (readdata()) + { + process(); + } + return 0; +} diff --git a/11379.cpp b/11379.cpp new file mode 100644 index 0000000..2acc230 --- /dev/null +++ b/11379.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define MOD 1000000000 +/* +g(n,k) - formas de agrupar n circulos com o k-ésimo cobrindo pelo menos um outro circulo +f(n,k) - formas de agrupar n circulos com o k-ésimo não cubra nenhum outro circulo +Cn - n-ésimo número catalão +Cn = g(n,k) + f(n,k) +g(n,k) = SUM em i = [1 ate (n-k)] de { f[n-i][k]*C[i-1] } +f[n,k] = Cn - g[n][k] +g em f -> f[n][k] = Cn - ( SUM em i = [1 ate (n-k)] de { f[n-i][k]*C[i-1] } ) +f fica em função de f sem variar k. +resolvendo cada input em O(n) +solução O(n^2) com memoization +*/ +int N, x, y; +int f[1010][1010]; +//f[i][0] -> catalan +int readdata() +{ + if (scanf("%d %d %d", &N, &x, &y) != 3) + { + return 0; + } + return 1; +} +int rec(int n, int k) +{ + if (f[n][k] != -1) + { + return f[n][k]; + } + int i, Gnk; + Gnk = 0; + for (i = 1; i <= (n - k); i++) + { + Gnk = (Gnk + ((long long)rec(n - i, k) * f[i][0]) % MOD) % MOD; + } + f[n][k] = (f[n][0] - Gnk) % MOD; + if (f[n][k] < 0) + { + f[n][k] += MOD; + } + return f[n][k]; +} +int process() +{ + int i, total = 0; + for (i = y; i <= N; i++) + { + total = (total + ((long long)rec(N - (i - x), x) * f[i - x][0]) % MOD) % MOD; + } + printf("%d\n", total); + return 0; +} +int main() +{ + int i, j, k; + memset(f, -1, sizeof(f)); + f[0][0] = 1; + f[1][0] = 1; + for (i = 2; i < 1000; i++) + { + f[i][0] = 0; + for (j = 0; j < i; j++) + { + f[i][0] = (f[i][0] + ((long long)f[j][0] * f[i - j - 1][0]) % MOD) % MOD; + } + } + while (readdata()) + { + process(); + } + return 0; +} diff --git a/11380.cpp b/11380.cpp new file mode 100644 index 0000000..c8792d8 --- /dev/null +++ b/11380.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define IN(x, y) ((y)*Y + (x)) +#define OUT(x, y) (IN(x, y) + (X * Y)) + +const int INF = 99999999; + +struct Edge +{ + int from, to, cap, flow; + Edge *BackEdge, *next; +}; +Edge *graph[1802], *p[1802]; + +char map_[31][31]; +int X, Y, P, source, sink, D[1802], v[1802]; + +void AddEdge(int from, int to, int cap) +{ + Edge *p = new Edge, *q = new Edge; + p->from = from; + q->from = to; + p->to = to; + q->to = from; + p->cap = cap; + q->cap = 0; + p->flow = q->flow = 0; + p->BackEdge = q; + q->BackEdge = p; + p->next = graph[from]; + graph[from] = p; + q->next = graph[to]; + graph[to] = q; +} +void init() +{ + for (int i = 0; i < 1802; i++) + { + D[i] = 0; + p[i] = NULL; + v[i] = 0; + } + D[source] = INF; +} +bool FindPath(int s) +{ + v[s] = 1; + if (s == sink) + { + return true; + } + for (Edge *ptr = graph[s]; ptr; ptr = ptr->next) + { + if (!v[ptr->to] && ptr->cap - ptr->flow > 0) + { + p[ptr->to] = ptr; + D[ptr->to] = min(D[s], ptr->cap - ptr->flow); + if (FindPath(ptr->to)) + { + return true; + } + } + } + return false; +} +void UpdatePath(int s, int e, int f) +{ + Edge *ptr = p[e]; + do + { + ptr->flow += f; + ptr->BackEdge->flow = -ptr->flow; + ptr = p[ptr->from]; + } while (ptr); +} +int MaxFlow() +{ + int result = 0; + while (true) + { + init(); + if (!FindPath(source)) + { + break; + } + result += D[sink]; + UpdatePath(source, sink, D[sink]); + } + return result; +} +void DeleteList(Edge *&p) +{ + if (p) + { + DeleteList(p->next); + delete p; + } +} +int main() +{ + while (scanf("%d %d %d", &X, &Y, &P) == 3) + { + for (int i = 0; i < 1802; i++) + { + graph[i] = NULL; + } + for (int i = 0; i < X; i++) + { + scanf("%s", map_[i]); + } + source = 2 * X * Y; + sink = source + 1; + for (int i = 0; i < X; i++) + for (int j = 0; j < Y; j++) + { + if (map_[i][j] == '~') + { + continue; + } + if (map_[i][j] == '*') + { + AddEdge(source, IN(j, i), 1); + } + if (map_[i][j] == '#') + { + AddEdge(OUT(j, i), sink, P); + } + if (map_[i][j] == '@' || map_[i][j] == '#') + { + AddEdge(IN(j, i), OUT(j, i), INF); + } + else + { + AddEdge(IN(j, i), OUT(j, i), 1); + } + if (i && map_[i - 1][j] != '~') + { + AddEdge(OUT(j, i), IN(j, i - 1), INF); + } + if (i + 1 < X && map_[i + 1][j] != '~') + { + AddEdge(OUT(j, i), IN(j, i + 1), INF); + } + if (j && map_[i][j - 1] != '~') + { + AddEdge(OUT(j, i), IN(j - 1, i), INF); + } + if (j + 1 < Y && map_[i][j + 1] != '~') + { + AddEdge(OUT(j, i), IN(j + 1, i), INF); + } + } + int sol = MaxFlow(); + printf("%d\n", sol); + for (int i = 0; i < 1802; i++) + { + DeleteList(graph[i]); + } + } +} diff --git a/11381.cpp b/11381.cpp new file mode 100644 index 0000000..d7e4d35 --- /dev/null +++ b/11381.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +/** +* /////////////////////// +* // MIN COST MAX FLOW // +* /////////////////////// +* +* Authors: Frank Chu, Igor Naverniouk +**/ + +/********************* +* Min cost max flow * (Edmonds-Karp relabelling + fast heap Dijkstra) +********************* +* Takes a directed graph where each edge has a capacity ('cap') and a +* cost per unit of flow ('cost') and returns a maximum flow network +* of minimal cost ('fcost') from s to t. USE mcmf3.cpp FOR DENSE GRAPHS! +* +* PARAMETERS: +* - cap (global): adjacency matrix where cap[u][v] is the capacity +* of the edge u->v. cap[u][v] is 0 for non-existent edges. +* - cost (global): a matrix where cost[u][v] is the cost per unit +* of flow along the edge u->v. If cap[u][v] == 0, cost[u][v] is +* ignored. ALL COSTS MUST BE NON-NEGATIVE! +* - n: the number of vertices ([0, n-1] are considered as vertices). +* - s: source vertex. +* - t: sink. +* RETURNS: +* - the flow +* - the total cost through 'fcost' +* - fnet contains the flow network. Careful: both fnet[u][v] and +* fnet[v][u] could be positive. Take the difference. +* COMPLEXITY: +* - Worst case: O(m*log(m)*flow = d[q[i]]) + { + break; + } + BUBL; + } + // relax edge (u,i) or (i,u) for all i; + for (int k = 0, v = adj[u][k]; k < deg[u]; v = adj[u][++k]) + { + // try undoing edge v->u + if (fnet[v][u] && d[v] > Pot(u, v) - cost[v][u]) + { + d[v] = Pot(u, v) - cost[v][par[v] = u]; + } + // try using edge u->v + if (fnet[u][v] < cap[u][v] && d[v] > Pot(u, v) + cost[u][v]) + { + d[v] = Pot(u, v) + cost[par[v] = u][v]; + } + if (par[v] == u) + { + // bubble up or decrease key + if (inq[v] < 0) + { + inq[q[qs] = v] = qs; + qs++; + } + for (int i = inq[v], j = (i - 1) / 2, t; + d[q[i]] < d[q[j]]; i = j, j = (i - 1) / 2) + { + BUBL; + } + } + } + } + for (int i = 0; i < n; i++) + if (pi[i] < Inf) + { + pi[i] += d[i]; + } + return par[t] >= 0; +} +#undef Pot + +int mcmf4(int n, int s, int t, int &fcost) +{ + // build the adjacency list + CLR(deg, 0); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (cap[i][j] || cap[j][i]) + { + adj[i][deg[i]++] = j; + } + CLR(fnet, 0); + CLR(pi, 0); + int flow = fcost = 0; + // repeatedly, find a cheapest path from s to t + while (dijkstra(n, s, t)) + { + // get the bottleneck capacity + int bot = INT_MAX; + for (int v = t, u = par[v]; v != s; u = par[v = u]) + { + int value = fnet[v][u] ? fnet[v][u] : (cap[u][v] - fnet[u][v]); + if (value < bot) + { + bot = value; + } + } + // update the flow network + for (int v = t, u = par[v]; v != s; u = par[v = u]) + if (fnet[v][u]) + { + fnet[v][u] -= bot; + fcost -= bot * cost[v][u]; + } + else + { + fnet[u][v] += bot; + fcost += bot * cost[u][v]; + } + flow += bot; + } + return flow; +} + +int graph[26][26]; +int main() +{ + int cases; + cin >> cases; + char S[1001], T[101]; + while (cases--) + { + cin >> S >> T; + memset(graph, 0, sizeof(graph)); + for (int i = 0; i < strlen(S) - 1; i++) + { + int a = S[i] - 'a', b = S[i + 1] - 'a'; + if (!graph[a][b]) + { + graph[a][b] = (i + 1) * (i + 1); + } + } + int numV = strlen(T); + int s = 0, t = 2 * numV + 1; + memset(cap, 0, sizeof(cap)); + memset(cost, 0, sizeof(cost)); + for (int i = 1; i <= numV; i++) + { + cap[s][i] = 1; + } + for (int i = numV + 1; i <= 2 * numV; i++) + { + cap[i][t] = 1; + } + int m, a, b, c, cp; + for (int i = 0; i < numV; i++) + { + for (int j = i + 1; j < numV; j++) + { + int a = T[i] - 'a', b = T[j] - 'a'; + if (graph[a][b]) + { + cap[i + 1][numV + j + 1] = 1; + cost[i + 1][numV + j + 1] = graph[a][b]; + } + } + } + int fcost; + int flow = mcmf4(2 * numV + 2, s, t, fcost); + printf("%d %d\n", numV - flow, fcost); + } + return 0; +} diff --git a/11382.cpp b/11382.cpp new file mode 100644 index 0000000..24b51d9 --- /dev/null +++ b/11382.cpp @@ -0,0 +1,251 @@ +#include + +using namespace std; + +// CALCULAR EL MINIMO ANGULO PARA EL ARMA QUE CON HASTA K TIROS MATA A TODOS +// LOS SEGMENTOS; ( FANTASMITAS :P ES COMO GHOSTBUSTERS :P ) + +typedef struct +{ + double x, y; +} point; + +typedef struct +{ + point p1, p2; +} segment; + +int n; +segment seg[101]; +double M = 2 * acos(-1); +double PI = acos(-1); + +#define EPS 1e-7 + +double angulo(point p) +{ + double d = sqrt(p.x * p.x + p.y * p.y); + p.x /= d; + p.y /= d; + double angle = acos(p.x); + if (p.y + EPS < 0.0) + { + angle = 2.0 * PI - angle; + } + return angle; +} +pair inter(segment seg) +{ + return make_pair(angulo(seg.p1), angulo(seg.p2)); +} +int borrar(vector> &vec) +{ + if (vec.size() == 1) + { + return 0; + } + for (int i = 0; i < vec.size(); i++) + { + if (fabs(vec[i].first - vec[i].second) < EPS) + { + vec.erase(vec.begin() + i); + return 1; + } + int next = (i + 1) % vec.size(); + if (next) + { + if (vec[next].first < vec[i].second) + { + vec[i].second = max(vec[i].second, vec[next].second); + vec.erase(vec.begin() + next); + return 1; + } + } + else + { + pair par = vec[next]; + par.first += 2.0 * PI, par.second += 2.0 * PI; + if (par.first < vec[i].second) + { + vec[i].second = max(vec[i].second, par.second); + vec.erase(vec.begin() + next); + return 1; + } + } + } + return 0; +} +int tratar(vector> vec, int ind, double dist) +{ + int i, j, k; + double last = vec[ind].first; + int listo = 0; + i = ind; + int first = 0; + int res = 0; + while (!listo) + { + if (i == ind) + { + if (first) + { + break; + } + else + { + first = 1; + } + } + if (i == 0 && last > M) + { + while (last > M) + { + last -= M; + } + } + else if (i == 0) + { + last = 0.0; + } + double pri, sec; + pri = max(vec[i].first, last); + sec = vec[i].second; + if (pri < sec) + { + double len = ((sec - pri) / dist); + int ca; + if (fabs(len - floor(len + EPS)) < EPS) + { + ca = (int)(len + EPS); + } + else + { + ca = (int)(len + EPS) + 1; + } + res += ca; + last = pri + dist * (double)ca; + } + i = (i + 1) % vec.size(); + } + return res; +} +int cantidad(double dist, vector> vec) +{ + int i, j, k; + int res = 100000; + for (i = 0; i < vec.size(); i++) + { + res = min(tratar(vec, i, dist), res); + } + return res; +} +int main() +{ + int i, j, k; + int casos; + scanf("%i", &casos); + double R; + int K; + for (int h = 0; h < casos; h++) + { + scanf("%i %i", &n, &K); + double x, y; + scanf("%lf %lf", &x, &y); + for (i = 0; i < n; i++) + { + scanf("%lf %lf %lf %lf", &seg[i].p1.x, &seg[i].p1.y, &seg[i].p2.x, &seg[i].p2.y); + } + for (i = 0; i < n; i++) + { + seg[i].p1.x -= x; + seg[i].p2.x -= x; + seg[i].p1.y -= y; + seg[i].p2.y -= y; + } + vector> intervalo; + for (i = 0; i < n; i++) + { + intervalo.push_back(inter(seg[i])); + } + for (i = 0; i < intervalo.size(); i++) + { + if (intervalo[i].first < intervalo[i].second) + { + if (intervalo[i].second - intervalo[i].first > PI) + { + swap(intervalo[i].first, intervalo[i].second); + } + } + else + { + if (intervalo[i].first - intervalo[i].second < PI) + { + swap(intervalo[i].first, intervalo[i].second); + } + } + } + for (i = 0; i < intervalo.size(); i++) + if (intervalo[i].first > EPS + intervalo[i].second) + { + intervalo[i].second += 2.0 * PI; + } + sort(intervalo.begin(), intervalo.end()); + while (borrar(intervalo)) + ; + double tot = 0.0; + for (i = 0; i < intervalo.size(); i++) + { + tot += intervalo[i].second - intervalo[i].first; + } + tot = min(M, tot); + double res; + if (fabs(tot - M) < EPS) + { + if (K == 1) + { + res = -1.0; + } + else + { + res = M / (double)K; + } + } + else + { + double b, e; + b = (1.0 / 180.0) * PI; + e = PI; + if (cantidad(e, intervalo) > K) + { + res = -1.0; + } + else if (cantidad(b, intervalo) <= K) + { + res = b; + } + else + { + while (fabs(b - e) > EPS / 2.0) + { + double med = (b + e) / 2.0; + if (cantidad(med, intervalo) <= K) + { + e = med; + } + else + { + b = med; + } + } + res = e; + } + } + if (res > -0.01) + { + res = (res / PI) * 180.0; + res = max(res, 1.0); + } + printf("%.4lf\n", res); + } + return 0; +} diff --git a/11383.cpp b/11383.cpp new file mode 100644 index 0000000..2bff665 --- /dev/null +++ b/11383.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +#define LIM 501 +#define INF 99999999 +/* +Entrada: +N +matriz NxN +Saída: +linha resultado +coluna resultado +soma total +*/ +int lx[LIM], ly[LIM]; +int mark_T[LIM]; +vector S, T; +int vizinhos_X[LIM][LIM]; +int num_vizinhos_X[LIM]; +int saturadoX[LIM];// Para os vértices de X ( Grafo bipartido (X,Y) ) +int saturadoY[LIM]; +// para achar o caminho de aumento +int paiX[LIM]; // Pai de um vértice em Y ! +int paiY[LIM]; // Pai de um vértice em X ! +int match[LIM];// Matching apenas dos vértices de Y ! ( Grafo (X,Y) ) +int N; +int w[LIM][LIM]; +int readdata() +{ + if (scanf("%d", &N) != 1) + { + return 0; + } + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + scanf("%d", &w[i][j]); + } + } + return 1; +} +int m_nao_saturado() +{ + int i; + for (i = 1; i <= N; i++) + { + if (saturadoX[i] == 0) + { + return i; + } + } + return 0; +} +int escolhe_vizinho_nao_em_T(int *x_pai) +{ + int i, j; + for (i = 0; i < S.size(); i++) + { + for (j = 0; j < num_vizinhos_X[S[i]]; j++) + { + if (!mark_T[vizinhos_X[S[i]][j]]) + { + *x_pai = S[i]; + return vizinhos_X[S[i]][j]; + } + } + } + return -1; +} +int clean_and_create() +{ + memset(num_vizinhos_X, 0, sizeof(num_vizinhos_X)); + memset(mark_T, 0, sizeof(mark_T)); + memset(saturadoX, 0, sizeof(saturadoX)); + memset(saturadoY, 0, sizeof(saturadoY)); + memset(match, -1, sizeof(match)); + memset(paiX, -1, sizeof(paiX)); + memset(paiY, -1, sizeof(paiY)); + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (lx[i] + ly[j] == w[i][j]) + { + vizinhos_X[i][num_vizinhos_X[i]++] = j; + } + } + } + return 0; +} +int start() +{ + int i, j, k; + for (i = 1; i <= N; i++) + { + k = -1; + for (j = 1; j <= N; j++) + { + if (w[i][j] > k) + { + k = w[i][j]; + } + } + lx[i] = k; + ly[i] = 0; + } + clean_and_create(); + return 0; +} +int compute() +{ + int i, j; + int best = INF; + for (i = 0; i < S.size(); i++) + { + for (j = 1; j <= N; j++) + { + if (!mark_T[j]) + { + if (lx[S[i]] + ly[j] - w[S[i]][j] < best) + { + best = lx[S[i]] + ly[j] - w[S[i]][j]; + } + } + } + } + for (i = 0; i < S.size(); i++) + { + lx[S[i]] -= best; + } + for (i = 0; i < T.size(); i++) + { + ly[T[i]] += best; + } + clean_and_create(); + return 0; +} +int process() +{ + int i, u, y, z; + int x_pai; + start(); + while (u = m_nao_saturado()) + { + S.clear(); + T.clear(); + memset(mark_T, 0, sizeof(mark_T)); + S.push_back(u); + while (1) + { + /* pega um vértice em Y (vizinho de alguém que esteja em S) + que nao pertença a T */ + y = escolhe_vizinho_nao_em_T(&x_pai); + if (y == -1)// não encontrou matching perfeito + { + compute();// calcula novos valores para lx e ly + break; + } + else + { + if (saturadoY[y]) + { + S.push_back(match[y]); + T.push_back(y); + mark_T[y] = 1; + paiX[y] = x_pai; + paiY[match[y]] = y; + } + else + { + // caminho de aumento + paiX[y] = x_pai; + int atual = y; + while (atual != -1) + { + match[atual] = paiX[atual]; + saturadoY[atual] = 1; + saturadoX[paiX[atual]] = 1; + atual = paiY[paiX[atual]];// atual sempre é um vertice de Y + } + break; + } + } + } + } + int sum = 0; + for (i = 1; i <= N; i++) + { + if (i > 1) + { + printf(" "); + } + printf("%d", lx[i]); + sum += lx[i]; + } + printf("\n"); + for (i = 1; i <= N; i++) + { + if (i > 1) + { + printf(" "); + } + printf("%d", ly[i]); + sum += ly[i]; + } + printf("\n%d\n", sum); + return 0; +} +int main() +{ + while (readdata()) + { + process(); + } + return 0; +} diff --git a/11384.cpp b/11384.cpp index 3df3fe9..e4e95c7 100644 --- a/11384.cpp +++ b/11384.cpp @@ -1,19 +1,25 @@ -#include -#include -using namespace std; - -int main(){ - long long twos[35]={0}; - for(long long i=0;i<35;i++) - twos[i]=(long long)pow(2.0,(double)i); - long long n; - while(cin>>n){ - for(long long i=0;i<35;i++){ - if(n + +using namespace std; + +int main() +{ + long long twos[35] = {0}; + for (long long i = 0; i < 35; i++) + { + twos[i] = (long long)pow(2.0, (double)i); + } + long long n; + while (cin >> n) + { + for (long long i = 0; i < 35; i++) + { + if (n < twos[i]) + { + cout << i << endl; + break; + } + } + } + return 0; +} diff --git a/11385.cpp b/11385.cpp new file mode 100644 index 0000000..9e55a73 --- /dev/null +++ b/11385.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.first < b.first; +} + +int main() +{ + int cases; + int n; + string input; + vector> v; + cin >> cases; + while (cases--) + { + cin >> n; + v.clear(); + vector nums(n); + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + cin.ignore(100, '\n'); + getline(cin, input); + n = 0; + for (int i = 0, sz = input.size(); i < sz && n < nums.size(); i++) + { + if (input[i] >= 'A' && input[i] <= 'Z') + { + v.push_back(make_pair(nums[n], input[i])); + n++; + } + } + sort(v.begin(), v.end(), theTruthIsOutThere); + n = 0; + int a = 0; + int b = 1; + int r; + while (n != v.size()) + { + r = a + b; + a = b; + b = r; + if (v[n].first == r) + { + cout << v[n].second; + n++; + } + else + { + cout << " "; + } + } + cout << endl; + } + return 0; +} diff --git a/11386.cpp b/11386.cpp index c4e63d9..99a8e2c 100644 --- a/11386.cpp +++ b/11386.cpp @@ -1,34 +1,43 @@ -#include -#include -#include +#include + using namespace std; typedef long long int64; -int main() { +int main() +{ int64 N; - while (scanf("%lld", &N) != EOF) { + while (scanf("%lld", &N) != EOF) + { int64 I = 0, T = 0; map Map; map::iterator ii, jj; - for (int64 i = 0; i < N; i = i + 1) { + for (int64 i = 0; i < N; i = i + 1) + { scanf("%lld", &I); ++Map[I]; } - for (ii = Map.begin(); ii != Map.end(); ii++) { - for (jj = ii; jj != Map.end(); jj++) { + for (ii = Map.begin(); ii != Map.end(); ii++) + { + for (jj = ii; jj != Map.end(); jj++) + { int64 A1 = (*ii).first; int64 A2 = (*ii).second; int64 B1 = (*jj).first; int64 B2 = (*jj).second; int64 temp = 0; if (A1 == B1) - temp += (A2 * (A2 - 1))/2; + { + temp += (A2 * (A2 - 1)) / 2; + } else + { temp += (A2 * B2); - + } if (Map.find(A1 + B1) != Map.end()) + { T += (temp * Map[A1 + B1]); + } } } printf("%lld\n", T); diff --git a/11387.cpp b/11387.cpp new file mode 100644 index 0000000..d95d3b5 --- /dev/null +++ b/11387.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/113/11387.html +// Runtime: 0.016s +// Tag: Graph, easy + +int main() +{ + int n; + while (scanf("%d", &n) && n) + { + if (n % 2 || n < 4) + { + printf("Impossible\n"); + } + else + { + printf("%d\n", n * 3 / 2); + for (int i = 1; i < n; i++) + { + printf("%d %d\n", i, i + 1); + } + printf("%d %d\n", n, 1); + int tmp = n / 2 + 1; + for (int i = 1; i <= n / 2; i++) + { + printf("%d %d\n", i, tmp++); + } + } + } + return 0; +} diff --git a/11388.cpp b/11388.cpp index 9420658..0c34090 100644 --- a/11388.cpp +++ b/11388.cpp @@ -1,45 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int G, L; - cin>>G>>L; - if(L % G == 0) - cout< + +using namespace std; + +/* + * UVa 11388 + * Author: chchwy + * Last Modified: 2009.11.13 + */ + +int main() +{ + int numCase; + scanf("%d", &numCase); + while (numCase--) + { + int gcd, lcm; + scanf("%d %d", &gcd, &lcm); + if (lcm % gcd != 0) + { + puts("-1"); + } + else + { + printf("%d %d\n", gcd, lcm); + } + } + return 0; +} diff --git a/11389.cpp b/11389.cpp index cf2a36d..6f7969e 100644 --- a/11389.cpp +++ b/11389.cpp @@ -1,35 +1,42 @@ -#include -#include - -int compA(const void *A, const void *B) { - return ( *(int *)A - *(int *)B ); -} - -int compB(const void *A, const void *B) { - return ( *(int *)B - *(int *)A ); -} - -int main() { - while (1) { - int n, d, r, i, j, over = 0, temp; - int mor[200], eve[200]; - - scanf("%d%d%d", &n, &d, &r); - if (!n && !d && !r) return 0; - - for (i = 0; i < n; i++) scanf("%d", &mor[i]); - for (i = 0; i < n; i++) scanf("%d", &eve[i]); - - qsort(mor, n, sizeof (int), compA); - qsort(eve, n, sizeof (int), compB); - - for (i = 0; i < n; i++) { - if (mor[i] + eve[i] > d) { - temp = mor[i] + eve[i] - d; - over += temp * r; - } - } - printf("%d\n", over); - } - return 0; -} +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/113/11389.html + +int main() +{ + int n; + int d; + int r; + while (scanf("%d %d %d", &n, &d, &r)) + { + if (!n && !d && !r) + { + return 0; + } + int morn[102]; + int eve[102]; + for (int i = 0; i < n; i++) + { + scanf("%d", &morn[i]); + } + sort(morn, morn + n); + for (int i = 0; i < n; i++) + { + scanf("%d", &eve[i]); + } + sort(eve, eve + n); + int cost = 0; + for (int i = 0; i < n; i++) + { + int temp = morn[i] + eve[n - i - 1]; + if (temp > d) + { + cost += (temp - d); + } + } + printf("%d\n", cost * r); + } + return 0; +} diff --git a/11390.cpp b/11390.cpp new file mode 100644 index 0000000..1c1de4f --- /dev/null +++ b/11390.cpp @@ -0,0 +1,218 @@ +#include + +using namespace std; + +struct Grafo +{ + int v, grau[102], adj[102][102], like[102]; + + void clear() + { + memset(grau, 0, sizeof(grau)); + memset(like, 0, sizeof(like)); + v = 0; + } + + void addEdge(int u, int v) + { + adj[u][grau[u]++] = v; + } + +} grafo1, grafo2; + +int component[102], likeComp[102], dfs_number[102], ncomp; +int total, cont, best, u, bestc, nconvids; +; +char mark[102][102], convide[102], ord[102]; +stack pilha; + +void read() +{ + int r; + grafo1.clear(); + scanf("%d", &grafo1.v); + for (int i = 0; i < grafo1.v; i++) + { + scanf("%d %d", &grafo1.like[i], &r); + for (int j = 0; j < r; j++) + { + scanf("%d", &u); + grafo1.addEdge(i, u - 1); + } + } +} + +int dfs(int no) +{ + int min, w; + min = dfs_number[no] = cont++; + pilha.push(no); + for (int i = 0; i < grafo1.grau[no]; i++) + { + w = grafo1.adj[no][i]; + if (!dfs_number[w]) + { + min = dfs(w) < min ? dfs(w) : min; + } + else if (!component[w]) + { + min = dfs_number[w] < min ? dfs_number[w] : min; + } + } + if (min == dfs_number[no]) + { + while ((w = pilha.top()) != no) + { + pilha.pop(); + component[w] = ncomp; + } + component[no] = ncomp; + ncomp++; + pilha.pop(); + } + return min; +} + +int search(int no, int val) +{ + int ret = grafo2.like[no]; + convide[no] = val; + nconvids++; + for (int i = 0; i < grafo2.grau[no]; i++) + { + if (convide[grafo2.adj[no][i]] == -1) + { + ret += search(grafo2.adj[no][i], val); + } + } + return ret; +} + +void desearch(int no, int val) +{ + convide[no] = -1; + nconvids--; + for (int i = 0; i < grafo2.grau[no]; i++) + { + if (convide[grafo2.adj[no][i]] == val) + { + desearch(grafo2.adj[no][i], val); + } + } +} + +void rec(int atual) +{ + int ac = 0, no = ord[atual]; + if (atual == grafo2.v) + { + if (best < total) + { + best = total; + bestc = nconvids; + } + else if (best == total) + { + bestc = bestc > nconvids ? bestc : nconvids; + } + } + else + { + if (convide[no] != -1) + { + rec(atual + 1); + } + else + { + ac = search(no, no + 1); + total += ac; + rec(atual + 1); + total -= ac; + desearch(no, no + 1); + rec(atual + 1); + } + } +} + +void top_sort(int no) +{ + convide[no] = 1; + for (int i = 0; i < grafo2.grau[no]; i++) + { + u = grafo2.adj[no][i]; + if (!convide[u]) + { + top_sort(u); + } + } + ord[--ncomp] = no; +} + +void process() +{ + int w; + cont = 1; + ncomp = 1; + while (!pilha.empty()) + { + pilha.pop(); + } + memset(dfs_number, 0, sizeof(dfs_number)); + memset(component, 0, sizeof(component)); + for (int i = 0; i < grafo1.v; i++) + { + if (!dfs_number[i]) + { + dfs(i); + } + } + grafo2.clear(); + memset(mark, 0, sizeof(mark)); + grafo2.v = ncomp - 1; + for (int i = 0; i < grafo1.v; i++) + { + grafo2.like[component[i] - 1] += grafo1.like[i]; + for (int j = 0; j < grafo1.grau[i]; j++) + { + w = grafo1.adj[i][j]; + if (component[i] != component[w] && !mark[component[i]][component[w]]) + { + mark[component[i]][component[w]] = 1; + grafo2.addEdge(component[i] - 1, component[w] - 1); + } + } + } + memset(convide, 0, sizeof(convide)); + ncomp = grafo2.v; + for (int i = 0; i < grafo2.v; i++) + { + if (!convide[i]) + { + top_sort(i); + } + } + best = total = bestc = nconvids = 0; + memset(convide, -1, sizeof(convide)); + rec(0); + if (best < 0 || (best == 0 && !bestc)) + { + printf("Alas, sultan can't invite anyone!\n"); + } + else + { + printf("%d\n", best); + } +} + +int main() +{ + int casos, caso = 1; + scanf("%d", &casos); + while (casos--) + { + read(); + printf("Case #%d: ", caso++); + process(); + } + return 0; +} diff --git a/11391.cpp b/11391.cpp new file mode 100644 index 0000000..55f09c5 --- /dev/null +++ b/11391.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define get(i, j, m) (((1 << (i * c + j)) & m) != 0) +#define flip(i, j, m) (m ^= 1 << (i * c + j)) +int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; +int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; +int r, c; +int dp[4][4][1 << 16]; +int nWays(int mask) +{ + if (dp[r - 1][c - 1][mask] != -1) + { + return dp[r - 1][c - 1][mask]; + } + int i, j, k, x1, x2, y1, y2, res = 0, mask2; + for (i = 0; i < r; i++) + for (j = 0; j < c; j++) + if (get(i, j, mask)) + for (k = 0; k < 8; k++) + { + x1 = i + dx[k]; + y1 = j + dy[k]; + x2 = i + 2 * dx[k]; + y2 = j + 2 * dy[k]; + if (x1 >= 0 && y1 >= 0 && x2 >= 0 && y2 >= 0 && x1 < r && x2 < r && y1 < c && y2 < c && get(x1, y1, mask) && !get(x2, y2, mask)) + { + mask2 = mask; + flip(i, j, mask2); + flip(x1, y1, mask2); + flip(x2, y2, mask2); + res += nWays(mask2); + } + } + return dp[r - 1][c - 1][mask] = res; +} +int main(int argc, char *argv[]) +{ + int tc, tcc = 1, blobs, i, j, k, x, y, x1, y1, x2, y2, mask, mask2; + scanf("%d", &tc); + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) + { + for (k = 0; k < 1 << 16; k++) + { + dp[i][j][k] = -1; + } + for (k = 0; k < 16; k++) + { + dp[i][j][1 << k] = 1; + } + dp[i][j][0] = 0; + } + for (tcc = 1; tcc <= tc; tcc++) + { + scanf("%d %d %d", &r, &c, &blobs); + mask = 0; + for (i = 0; i < blobs; i++) + { + scanf("%d %d", &x, &y); + x--; + y--; + flip(x, y, mask); + } + printf("Case %d: %d\n", tcc, nWays(mask)); + } + return 0; +} diff --git a/11392.cpp b/11392.cpp new file mode 100644 index 0000000..bcce2aa --- /dev/null +++ b/11392.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 1000000; +const int inf = 3 * N; +int cost[N][2]; +int path[N][2]; +int val[N][2]; +enum +{ + ZERO = 0, + THREE = 1 +}; + +bool verify(int div, int th, int z) +{ + int rem = 0; + int cnt = th + z; + rep(i, th) + { + rem = (rem * 10 + 3) % div; + cnt--; + if (rem % div == 0 && cnt != 0) + { + return false; + } + } + rep(i, z) + { + rem = (rem * 10) % div; + cnt--; + if (rem % div == 0 && cnt != 0) + { + return false; + } + } + return true; +} + +void bfs(int div) +{ + if (div == 1 || div == 3) + { + cout << "1 1 0" << endl; + return; + } + int now, last; + rep(i, div) cost[i][0] = cost[i][1] = inf, path[i][0] = path[i][1] = -1; + path[3][THREE] = -1; + val[3][THREE] = 3; + cost[3][THREE] = 0; + queue> Q; + Q.push(make_pair(3, (int)THREE)); + while (!Q.empty()) + { + now = Q.front().first; + last = Q.front().second; + Q.pop(); + if (now == 0) + { + break; + } + int next; + next = (now * 10 + 3) % div; + if (cost[next][THREE] == inf && last == THREE) + { + Q.push(make_pair(next, (int)THREE)); + cost[next][THREE] = cost[now][last] + 1; + val[next][THREE] = last; + path[next][THREE] = now; + } + next = (now * 10) % div; + if (cost[next][ZERO] == inf) + { + Q.push(make_pair(next, (int)ZERO)); + cost[next][ZERO] = cost[now][last] + 1; + val[next][ZERO] = last; + path[next][ZERO] = now; + } + } + int th = 1, ze = 0, ans = cost[0][last] + 1; + while (now != 3) + { + if (last == THREE) + { + th++; + } + else + { + ze++; + } + int tnow = now, tlast = last; + now = path[tnow][tlast]; + last = val[tnow][tlast]; + } + printf("%d %d %d\n", ans, th, ze); + if (!verify(div, th, ze)) + { + cout << div << " wrong " << endl; + assert(false); + } +} + +main() +{ + int n; + while (cin >> n) + { + bfs(n); + } +} diff --git a/11393.cpp b/11393.cpp new file mode 100644 index 0000000..c4198d7 --- /dev/null +++ b/11393.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) && n) + { + if (n == 1 || n % 3 == 2) + { + printf("NO\n"); + } + else + { + printf("YES\n"); + } + } + return 0; +} diff --git a/11394.cpp b/11394.cpp new file mode 100644 index 0000000..c1a9d51 --- /dev/null +++ b/11394.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +typedef long long ll; +#define REP(i, a, b) for ((i) = a; (i) < (int)(b); ++(i)) +#define FILL(a, v) memset(a, v, sizeof(a)) +const int MAXN = 18, SIZE = 17; +int i, j, k, m, n; +ll bc[MAXN][MAXN], dp[SIZE][SIZE][5]; +char input[MAXN]; +inline int value(char ch) +{ + if (isalpha(ch)) + { + return 10 + ch - 'A'; + } + return ch - '0'; +} +inline ll binomial_coefficient(int a, int b) +{ + int n = max(a, b); + int m = min(a, b); + for (i = 0; i <= n; i++) + { + bc[i][0] = 1; + } + for (j = 0; j <= n; j++) + { + bc[j][j] = 1; + } + for (i = 1; i <= n; i++) + for (j = 1; j < i; j++) + { + bc[i][j] = bc[i - 1][j - 1] + bc[i - 1][j]; + } + return bc[n][m]; +} +int main() +{ + FILL(bc, 0); + binomial_coefficient(MAXN, MAXN); + while (cin >> input) + { + if (!strcmp(input, "#")) + { + break; + } + FILL(dp, 0); + REP(i, 0, SIZE) + REP(j, 0, 5) + dp[i][0][j] = 0; + REP(i, 0, SIZE) + REP(j, 0, 5) + dp[0][i][j] = 0; + dp[0][0][0] = 1; + REP(i, 0, SIZE) + REP(j, 0, SIZE) + REP(k, 0, 5) + { + // count number of copies of 'i' + int num_copies = 0; + REP(m, 0, strlen(input)) + { + if (value(input[m]) == i) + { + num_copies++; + } + } + REP(n, 0, num_copies + 1) + { + if (j >= n && i > 0) + { + ll part1 = dp[i - 1][j - n][(1000000 + k - n * i) % 5]; + ll part2 = bc[j][n]; + ll sum = part1 * part2; + dp[i][j][k] += sum; + } + } + } + ll answer = 0; + REP(i, 0, SIZE) + { + //cerr << "i: " << i << " " << dp[SIZE-1][i][0] << endl; + answer += dp[SIZE - 1][i][0]; + } + cout << (answer - 1) << endl; + } +} diff --git a/11395.cpp b/11395.cpp new file mode 100644 index 0000000..e3ce516 --- /dev/null +++ b/11395.cpp @@ -0,0 +1,15 @@ +#include + +using namespace std; + +int main() +{ + double n; + scanf("%lf", &n); + while (n != 0) + { + printf("%0.lf\n", n - ((int)sqrt(n) + (int)sqrt(n / 2))); + scanf("%lf", &n); + } + return 0; +} diff --git a/11396.cpp b/11396.cpp new file mode 100644 index 0000000..2b24326 --- /dev/null +++ b/11396.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +#pragma warning(disable : 4786) + +#define SIZE 350 + +char flag, color[SIZE]; +int i, j, k, n, u, v; + +vector mv[SIZE]; +queue q; + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + while (!q.empty()) + { + q.pop(); + } + for (i = 1; i <= n; i++) + { + mv[i].clear(); + } + memset(color, 0, sizeof(color)); + while (scanf("%d %d", &u, &v) == 2 && (u || v)) + { + mv[u].push_back(v); + mv[v].push_back(u); + } + for (i = flag = 1; i <= n; i++) + { + if (!color[i]) + { + q.push(i); + color[i] = 1; + } + while (!q.empty()) + { + u = q.front(); + q.pop(); + for (j = 0, k = mv[u].size(); j < k; j++) + { + v = mv[u][j]; + if (!color[v]) + { + q.push(v); + color[v] = 3 - color[u]; + } + else if (color[u] == color[v] && u != v) + { + flag = 0; + break; + } + } + if (!flag) + { + break; + } + } + if (!flag) + { + break; + } + } + if (flag) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/11398.cpp b/11398.cpp index 4a0e5b5..6230f80 100644 --- a/11398.cpp +++ b/11398.cpp @@ -1,44 +1,82 @@ -#include -#include +#include + using namespace std; -string binaryValue(string S) { - if (S.length() == 1) return "1"; - return "0"; +// http://uva.onlinejudge.org/external/113/11398.html +// Runtime: 0.008s +// Tag: Binary conversion, Adhoc + +// @BEGIN_OF_SOURCE_CODE + +#define INF 2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define Dump(x) \ + for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) \ + cout << *it << " "; \ + cout << endl; + +int power(int b, int p) +{ + int ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; } -long long decimalValue(string S) { - long long V = 0, mul = 1; - int N = S.length(); - for (int i = N - 1; i >= 0; i--) { - V += (S[i] - '0') * mul; - mul *= 2; +int binaryToDecimal(vector &v) +{ + reverse(v.begin(), v.end()); + int ret = 0; + Fors(i, v) + { + ret += power(2, i) * v[i]; } - return V; + return ret; } -int main() { - string str, S = ""; - string flag; - while (true) { - cin >> str; - if (str == "~") break; - flag = binaryValue(str); - while (true) { - cin >> str; - if (str == "#") { - cout << decimalValue(S) << endl; - S = ""; - break; - } - else if (str.length() < 3) { - flag = binaryValue(str); +int main() +{ + char inp[100]; + int flag; + vector v; + while (scanf("%s", inp)) + { + if (!strcmp(inp, "~")) + { + break; + } + v.clear(); + while (strcmp(inp, "#")) + { + int len = strlen(inp); + if (len == 1) + { + flag = 1; } - else { - for (int i = 0; i + 2 < str.length(); i++) - S += flag; + else if (len == 2) + { + flag = 0; } + else + for (int i = 2; i < len; i++) + { + v.push_back(flag); + } + // Dump(v); + scanf("%s", inp); } + printf("%d\n", binaryToDecimal(v)); } return 0; } + +// @END_OF_SOURCE_CODE diff --git a/11399.cpp b/11399.cpp new file mode 100644 index 0000000..66cb09b --- /dev/null +++ b/11399.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define LIM 3000 +char m[LIM][LIM]; +char s[LIM]; +int tam; +int minX, minY, maxX, maxY; +int rec(int x, int y, int pos, int dirH, int dirV) +{ + int i, j, k; + for (i = pos; i < tam; i++) + { + if (s[i] == '[') + { + k = rec(x, y, i + 1, dirH, dirV); + i = k; + } + else + { + if (s[i] == ']') + { + return i; + } + else if (s[i] == 'U') + { + m[y][x] = '|'; + dirH = 0; + dirV = 1; + } + else if (s[i] == 'L') + { + x--; + m[y][x] = '_'; + dirH = -1; + dirV = 0; + } + else if (s[i] == 'l') + { + x--; + m[y][x] = '\\'; + dirH = -1; + dirV = 1; + } + else if (s[i] == 'R') + { + x++; + m[y][x] = '_'; + dirH = 1; + dirV = 0; + } + else if (s[i] == 'r') + { + x++; + m[y][x] = '/'; + dirH = 1; + dirV = 1; + } + else if (s[i] == 'f') + { + m[y][x] = '*'; + dirH = dirV = 0; + } + else if (s[i] == 'p') + { + m[y][x] = 'o'; + dirH = dirV = 0; + } + else if (s[i] == 'P') + { + m[y][x] = 'O'; + dirH = dirV = 0; + } + if (x < minX) + { + minX = x; + } + if (x > maxX) + { + maxX = x; + } + if (y < minY) + { + minY = y; + } + if (y > maxY) + { + maxY = y; + } + x += dirH; + y += dirV; + } + } +} +int main() +{ + while (scanf("%s", s) == 1) + { + memset(m, ' ', sizeof(m)); + tam = strlen(s); + minX = LIM; + minY = LIM; + maxX = 0; + maxY = 0; + rec(LIM / 2, LIM / 2, 0, 0, 0); + minX--; + maxX++; + minY--; + maxY++; + for (int x = minX; x < maxX; x++) + { + m[minY][x] = m[maxY][x] = '-'; + } + for (int y = maxY - 1; y > minY; y--) + { + m[y][minX] = m[y][maxX] = '|'; + } + m[minY][minX] = m[minY][maxX] = m[maxY][minX] = m[maxY][maxX] = '.'; + for (int i = maxY; i >= minY; i--) + { + m[i][maxX + 1] = 0; + printf("%s\n", m[i] + minX); + } + } + return 0; +} diff --git a/114.cpp b/114.cpp new file mode 100644 index 0000000..f36adc1 --- /dev/null +++ b/114.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +int main() +{ + int m, n, wall_hit_cost, p, v, c, x, y, direction, lifetime; + map, pair> bumpers; + int total_points = 0; + cin >> m >> n >> wall_hit_cost >> p; + while (p--) + { + cin >> x >> y >> v >> c; + bumpers[make_pair(x, y)] = make_pair(v, c); + } + while (cin >> x >> y >> direction >> lifetime) + { + int points = 0; + while (--lifetime > 0)// for every move, we lose 1 lifetime point + { + switch (direction) + { + // right + case 0: + if (x == m - 1) + { + lifetime -= wall_hit_cost; + direction = 3; + } + else + { + if (bumpers.find(make_pair(x + 1, y)) != bumpers.end()) + { + points += bumpers[make_pair(x + 1, y)].first; + lifetime -= bumpers[make_pair(x + 1, y)].second; + direction = 3; + } + else + x++; + } + break; + // up + case 1: + if (y == n - 1) + { + lifetime -= wall_hit_cost; + direction = 0; + } + else + { + if (bumpers.find(make_pair(x, y + 1)) != bumpers.end()) + { + points += bumpers[make_pair(x, y + 1)].first; + lifetime -= bumpers[make_pair(x, y + 1)].second; + direction = 0; + } + else + y++; + } + break; + // left + case 2: + if (x == 2) + { + lifetime -= wall_hit_cost; + direction = 1; + } + else + { + if (bumpers.find(make_pair(x - 1, y)) != bumpers.end()) + { + points += bumpers[make_pair(x - 1, y)].first; + lifetime -= bumpers[make_pair(x - 1, y)].second; + direction = 1; + } + else + { + x--; + } + } + break; + // down + case 3: + if (y == 2) + { + lifetime -= wall_hit_cost; + direction = 2; + } + else + { + if (bumpers.find(make_pair(x, y - 1)) != bumpers.end()) + { + points += bumpers[make_pair(x, y - 1)].first; + lifetime -= bumpers[make_pair(x, y - 1)].second; + direction = 2; + } + else + y--; + } + break; + } + } + cout << points << endl; + total_points += points; + } + cout << total_points << endl; + return 0; +} diff --git a/11400.cpp b/11400.cpp new file mode 100644 index 0000000..38fcdff --- /dev/null +++ b/11400.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int vv[1005], kk[1005], cc[1005], ll[1005], pp[1005], dp[1005]; + +int comp(const void *p, const void *q) +{ + int a, b; + a = *(int *)p; + b = *(int *)q; + if (vv[a] == vv[b]) + { + return cc[b] - cc[a]; + } + return vv[a] - vv[b]; +} + +int main() +{ + int n, i, j, temp, res; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%d%d%d%d", &vv[i], &kk[i], &cc[i], &ll[i]); + pp[i] = i; + } + qsort(pp, n, sizeof(int), comp); + for (i = 0; i < n; i++) + { + dp[i] = 2147483647; + for (j = i, temp = ll[pp[j]]; j >= 0; j--) + { + res = kk[pp[i]] + temp * cc[pp[i]]; + if (j) + { + res += dp[j - 1]; + temp += ll[pp[j - 1]]; + } + if (res < dp[i]) + { + dp[i] = res; + } + } + } + printf("%d\n", dp[n - 1]); + } + return 0; +} diff --git a/11401.cpp b/11401.cpp index 3a1584c..e0becdf 100644 --- a/11401.cpp +++ b/11401.cpp @@ -1,50 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; -#define LIM 1000001 -int main(){ - static int64 vec[LIM], i; - vec[3] =0; - vec[4] = 1; - for(i=5; i + +using namespace std; + +/* + Name: UVA 11401 + Author: zoom + Date: 28/07/11 +*/ + +#define LLU long long unsigned int +#define LLD long long double +#define FOR(i, N) for (int i = 0; i < (N); i++) +LLU ans[1000001]; +int main() +{ + ans[0] = ans[1] = ans[2] = ans[3] = 0; + LLU inc = 0; + int N; + for (int i = 4; i < 1000001; i++) + { + inc += (i - 2) / 2; + ans[i] = ans[i - 1] + inc; + } + while (cin >> N and N >= 3) + { + cout << ans[N] << endl; + } +} diff --git a/11402.cpp b/11402.cpp new file mode 100644 index 0000000..9f6c22e --- /dev/null +++ b/11402.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +//#define DBG +#define LEFT(x) (1 + (x << 1)) +#define RIGHT(x) (2 + (x << 1)) + +const int N = 1048576; + +bool bucaneer[N]; + +struct node +{ + char upd; + int b, e, m, v; +} nodes[N << 2]; + +void init(int idx, int left, int right) +{ + node &n = nodes[idx]; + n.m = ((n.b = left) + (n.e = right)) >> 1; + n.upd = ' '; + if (n.b != n.e) + { + init(LEFT(idx), left, n.m); + init(RIGHT(idx), n.m + 1, right); + n.v = nodes[LEFT(idx)].v + nodes[RIGHT(idx)].v; + } + else + { + n.v = bucaneer[n.b]; + } +} + +void set_update(int idx, char type) +{ + node &n = nodes[idx]; + if (type == 'I') + { + switch (n.upd) + { + case ' ': + n.upd = 'I'; + break; + case 'I': + n.upd = ' '; + break; + case 'E': + n.upd = 'F'; + break; + case 'F': + n.upd = 'E'; + break; + } + } + else + { + n.upd = type; + } +} + +void do_update(int idx) +{ + node &n = nodes[idx]; + if (n.upd == ' ') + { + return; + } + switch (n.upd) + { + case 'I': + n.v = n.e - n.b + 1 - n.v; + break; + case 'E': + n.v = 0; + break; + case 'F': + n.v = n.e - n.b + 1; + break; + } + set_update(LEFT(idx), n.upd); + set_update(RIGHT(idx), n.upd); + n.upd = ' '; +} + +void update(int idx, int left, int right, char type) +{ + node &n = nodes[idx]; + if (left <= n.b && n.e <= right)// fits into interval, apply the update + { + set_update(idx, type); + } + do_update(idx); + if (left > n.e || right < n.b) + { + return;// nothing else to do here + } + if (!(left <= n.b && n.e <= right))// will need to update children to update current node + { + update(LEFT(idx), left, right, type); + update(RIGHT(idx), left, right, type); + n.v = nodes[LEFT(idx)].v + nodes[RIGHT(idx)].v; + } +} + +int query(int idx, int left, int right) +{ + int r = 0; + node &n = nodes[idx]; + do_update(idx); + if (left > n.e || right < n.b) + { + r = 0; + } + else if (left <= n.b && n.e <= right) + { + r = n.v; + } + else + { + r = query(LEFT(idx), left, right) + query(RIGHT(idx), left, right); + n.v = nodes[LEFT(idx)].v + nodes[RIGHT(idx)].v; + } + return r; +} + +int main() +{ + int tcn; + scanf("%d", &tcn); + for (int i = 0, j, k, l; i++ < tcn;) + { + int a, b, pirates = 0, reps, queries, qc = 0, specs; + char cmd, s[64]; + for (scanf("%d", &specs); specs--;) + { + scanf("%d %s", &reps, s); + for (j = 0; s[j]; ++j) + { + s[j] -= '0'; + } + for (k = 0; k < reps; ++k) + for (l = 0; l < j; ++l) + { + bucaneer[pirates++] = s[l]; + } + } + init(0, 0, pirates - 1); + printf("Case %d:\n", i); + for (scanf("%d", &queries); queries--;) + { + scanf(" %c %d %d", &cmd, &a, &b); + if (cmd == 'S') + { + printf("Q%d: %d\n", ++qc, query(0, a, b)); + } + else + { + update(0, a, b, cmd); + } + } + } + return 0; +} diff --git a/11403.cpp b/11403.cpp new file mode 100644 index 0000000..3ea9fdb --- /dev/null +++ b/11403.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int l1, l2; +char s1[100], s2[100]; +const char *dash = "----------------------------------------------------------------"; +const char *zero = "0000000000000000000000000000000000000000000000000000000000000000"; + +int trans(char *s) +{ + int res; + for (res = 0; *s; s++) + { + res <<= 1; + res += *s - '0'; + } + return res; +} + +void recurs(long long res, int depth) +{ + int i, f, len, tlen; + if (res == 0) + { + f = !depth; + tlen = (depth += !depth); + if (l1 > l2) + { + len = l1; + } + else + { + len = l2; + } + if (len > tlen) + { + tlen = len; + } + printf("%*s\n", tlen, s1); + printf("%*s\n", tlen, s2); + printf("%*.*s\n", tlen, len, dash); + for (i = 0; i < l2; i++) + { + printf("%*.*s\n", tlen - i, l1, s2[l2 - i - 1] == '0' ? zero : s1); + } + printf("%*.*s\n", tlen, tlen, dash); + if (f) + { + printf("%*.*s\n", tlen, 1, zero); + } + return; + } + recurs(res >> 1, depth + 1); + printf("%lld", res % 2); + if (depth == 0) + { + printf("\n"); + } +} + +int main() +{ + int t, n1, n2; + long long res; + t = 0; + while (scanf("%s%s", s1, s2) == 2) + { + if (strcmp(s1, "0") == 0 && strcmp(s2, "0") == 0) + { + break; + } + if (t) + { + printf("\n"); + } + t = 1; + n1 = trans(s1); + n2 = trans(s2); + l1 = strlen(s1); + l2 = strlen(s2); + res = (long long)n1 * n2; + recurs(res, 0); + } + return 0; +} diff --git a/11404.cpp b/11404.cpp new file mode 100644 index 0000000..0fc8daa --- /dev/null +++ b/11404.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +int k, sp, dp[1001][1001], used[1001][1001], ptr[26], alp[26][1001]; +char str[1001], lst[1001]; + +inline int calc(int p, int q) +{ + if (used[p][q]) + { + return dp[p][q]; + } + used[p][q] = 1; + if (p > q) + { + return 0; + } + if (p == q) + { + dp[p][q] = 1; + return dp[p][q]; + } + if (str[p] == str[q]) + { + dp[p][q] = calc(p + 1, q - 1) + 2; + return dp[p][q]; + } + dp[p][q] = max(calc(p + 1, q), calc(p, q - 1)); + return dp[p][q]; +} + +inline void recurs(int dep) +{ + if (dep == k) + { + if (sp) + { + printf("%c", sp); + } + } + else + { + printf("%c", lst[dep]); + recurs(dep + 1); + printf("%c", lst[dep]); + } + if (dep == 0) + { + printf("\n"); + } +} + +int main() +{ + int len, res, i, j, l, r, p, q; + while (gets(str)) + { + memset(ptr, 0, sizeof(ptr)); + for (len = 0; str[len]; len++) + { + alp[str[len] - 'a'][ptr[str[len] - 'a']++] = len; + } + memset(used, 0, sizeof(used)); + res = calc(0, len - 1); + sp = 0; + for (i = res, l = 0, r = len - 1, k = 0; i > 0;) + { + for (j = 0; j < 26; j++) + { + for (p = 0; p < ptr[j]; p++) + { + if (alp[j][p] >= l) + { + break; + } + } + if (p < ptr[j]) + { + for (q = ptr[j] - 1; q >= p; q--) + { + if (alp[j][q] <= r) + { + break; + } + } + if (q >= p) + { + p = alp[j][p]; + q = alp[j][q]; + if (dp[p][q] == i) + { + if (p == q) + { + sp = str[p]; + i--; + } + else + { + lst[k++] = str[p]; + i -= 2; + } + l = p + 1; + r = q - 1; + break; + } + } + } + } + } + recurs(0); + } + return 0; +} diff --git a/11405.cpp b/11405.cpp new file mode 100644 index 0000000..415699d --- /dev/null +++ b/11405.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +struct State +{ + char *state; + State(char *s = NULL) { state = s ? strdup(s) : NULL; } + bool operator()(const State &a, const State &b) { return strcmp(a.state, b.state) < 0; } +}; + +int pawn(char *s, int p = 0) +{ + if (!s) + return -1; + for (int i = 0; i < 64; ++i) + p += s[i] == 'P'; + return p; +} + +int main() +{ + int t, move, knight_move[2][8] = {{1, -1, 1, -1, 2, -2, 2, -2}, {2, 2, -2, -2, 1, 1, -1, -1}}; + char board[8][8], str[65] = {0}, *now, *tmp; + for (scanf("%d", &t); t--;) + { + set exist; + queue next; + bool reach = false; + int step = 0; + scanf("%d\n", &move); + for (int i = 0; i < 8; ++i) + { + gets(str + i * 8); + } + next.push(now = strdup(str)); + next.push(NULL); + for (exist.insert(State(now)); step <= move && !reach; next.pop()) + { + int p = pawn(now = next.front()), x, y; + if (!now) + ++step, next.push(NULL); + if (!p) + reach = true; + if (!now || p > move - step || reach) + continue; + for (int i = 0; i < 64; ++i) + { + board[i / 8][i % 8] = now[i]; + if (now[i] == 'k') + x = i / 8, y = i % 8; + } + for (int i = 0; i < 8; ++i) + { + int nx = x + knight_move[0][i], ny = y + knight_move[1][i]; + if (nx >= 8 || nx < 0 || ny >= 8 || ny < 0 || board[nx][ny] == 'K' || board[nx][ny] == 'p') + continue; + char original = board[nx][ny]; + board[x][y] = '.'; + board[nx][ny] = 'k'; + for (int i = 0; i < 64; ++i) + now[i] = board[i / 8][i % 8]; + if (exist.find(now) == exist.end()) + { + exist.insert(State(tmp = strdup(now))); + next.push(tmp); + } + board[x][y] = 'k'; + board[nx][ny] = original; + } + } + puts(reach ? "Yes" : "No"); + } + return 0; +} diff --git a/11407.cpp b/11407.cpp new file mode 100644 index 0000000..d1ccec1 --- /dev/null +++ b/11407.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +#define min(a, b) (a < b) ? a : b +int n[10005]; +int main() +{ + int i, j; + for (i = 0; i < 10005; i++) + { + n[i] = 4; + } + for (i = 1; i * i <= 10000; i++) + { + n[i * i] = 1; + } + n[0] = 0; + for (i = 1; i <= 10000; i++) + for (j = 1; j <= i; j++) + if (n[j] == 1) + { + n[i] = min(n[i], 1 + n[i - j]); + } + scanf("%d\n", &i); + while (scanf("%d", &i) == 1) + { + printf("%d\n", n[i]); + } + return 0; +} diff --git a/11408.cpp b/11408.cpp new file mode 100644 index 0000000..ff2361f --- /dev/null +++ b/11408.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +#define MAX 5000001 +int a, b, i, j, tot = 0, answer; +int deprime[MAX]; +int totdeprimes[MAX]; +int main() +{ + for (i = 4; i < MAX; i += 2) + { + deprime[i] = 2; + } + for (i = 3; i < MAX; i += 2) + if (!deprime[i]) + for (j = 2 * i; j < MAX; j += i) + { + deprime[j] += i; + } + for (i = 2; i < MAX; i++) + if (!deprime[deprime[i]]) + { + totdeprimes[i] = ++tot; + } + else + { + totdeprimes[i] = tot; + } + scanf("%d", &a); + while (a) + { + scanf("%d", &b); + answer = totdeprimes[b] - totdeprimes[a]; + if (totdeprimes[a] > totdeprimes[a - 1]) + { + answer++; + } + printf("%d\n", answer); + scanf("%d", &a); + } +} diff --git a/11409.cpp b/11409.cpp new file mode 100644 index 0000000..ce5c113 --- /dev/null +++ b/11409.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +int map_[50][50]; +double dp[50][50]; + +int main() +{ + int count, i, j, k, n, m, x, y, f, tar, val, ans; + double urat, drat, rat; + scanf("%d", &count); + while (count--) + { + scanf("%d", &n); + memset(map_, 0, sizeof(map_)); + for (i = 0; i < n; i++) + { + for (j = 0; j <= i; j++) + { + scanf("%d", &map_[i][j]); + } + } + for (i = 1; i < n; i++) + { + for (j = 0; j < n - i; j++) + { + scanf("%d", &map_[n + i - 1][j + i]); + } + } + scanf("%d", &tar); + m = n + n - 1; + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (i = 0, urat = 0; i < m - 1; i++) + { + for (j = 0; j < n; j++) + { + if (map_[i][j]) + { + if (map_[i][j] == tar) + { + urat += dp[i][j]; + x = i; + y = j; + dp[i][j] = 0; + continue; + } + f = 0; + for (k = 0; k < n; k++) + { + if (map_[i + 1][k] && map_[i + 1][k] < map_[i][j]) + { + f++; + } + } + for (k = 0; k < n; k++) + { + if (map_[i + 1][k] && map_[i + 1][k] < map_[i][j]) + { + dp[i + 1][k] += dp[i][j] / f; + } + } + if (f) + { + dp[i][j] = 0; + } + } + } + } + for (i = m - 1; i > 0; i--) + { + for (j = 0; j < n; j++) + { + if (map_[i][j]) + { + if (map_[i][j] == tar) + { + urat += dp[i][j]; + break; + } + f = 0; + for (k = 0; k < n; k++) + { + if (map_[i - 1][k] && map_[i - 1][k] < map_[i][j]) + { + f++; + } + } + for (k = 0; k < n; k++) + { + if (map_[i - 1][k] && map_[i - 1][k] < map_[i][j]) + { + dp[i - 1][k] += dp[i][j] / f; + } + } + if (f) + { + dp[i][j] = 0; + } + } + } + if (j < n) + { + break; + } + } + /* Searate */ + memset(dp, 0, sizeof(dp)); + dp[m - 1][n - 1] = 1; + for (i = m - 1, drat = 0; i > 0; i--) + { + for (j = 0; j < n; j++) + { + if (map_[i][j]) + { + if (map_[i][j] == tar) + { + drat += dp[i][j]; + dp[i][j] = 0; + continue; + } + f = 0; + for (k = 0; k < n; k++) + { + if (map_[i - 1][k] && map_[i - 1][k] < map_[i][j]) + { + f++; + } + } + for (k = 0; k < n; k++) + { + if (map_[i - 1][k] && map_[i - 1][k] < map_[i][j]) + { + dp[i - 1][k] += dp[i][j] / f; + } + } + if (f) + { + dp[i][j] = 0; + } + } + } + } + for (i = 0; i < m - 1; i++) + { + for (j = 0; j < n; j++) + { + if (map_[i][j]) + { + if (map_[i][j] == tar) + { + drat += dp[i][j]; + break; + } + f = 0; + for (k = 0; k < n; k++) + { + if (map_[i + 1][k] && map_[i + 1][k] < map_[i][j]) + { + f++; + } + } + for (k = 0; k < n; k++) + { + if (map_[i + 1][k] && map_[i + 1][k] < map_[i][j]) + { + dp[i + 1][k] += dp[i][j] / f; + } + } + if (f) + { + dp[i][j] = 0; + } + } + } + } + rat = urat + (1 - urat) * drat; + ans = floor(rat * pow(10, 9)) + 1e-5; + printf("%d\n", ans); + } + return 0; +} diff --git a/1141.cpp b/1141.cpp new file mode 100644 index 0000000..d1aca49 --- /dev/null +++ b/1141.cpp @@ -0,0 +1,231 @@ +#include + +using namespace std; + +double eps = 1e-9; +struct point +{ + int x, y; +}; +point pt[333333]; +struct node +{ + vector pt; + vector x; + vector y; +}; +node tree[1 << 21]; +bool cmp1(point a, point b) +{ + return a.x < b.x; +} +bool cmp2(point a, point b) +{ + return a.y < b.y; +} +int l, h, r, n, ans; +void build(int id) +{ + int sz, l = 2 * id + 1, r = 2 * id + 2, lx, ly, rx, ry, szx, szy, flag, mx, my, mid; + int i, j, s, p, q; + if (tree[id].pt.size() <= 1) + { + return; + } + szx = tree[id].x.size(); + szy = tree[id].y.size(); + lx = tree[id].x[0]; + ly = tree[id].y[0]; + rx = tree[id].x[szx - 1]; + ry = tree[id].y[szy - 1]; + if ((rx - lx > ry - ly)) + { + flag = 0; + } + else + { + flag = 1; + } + while (l > (1 << 21) || r > (1 << 21)) + { + puts("no"); + } + tree[l].pt.clear(); + tree[l].x.clear(); + tree[l].y.clear(); + tree[r].pt.clear(); + tree[r].x.clear(); + tree[r].y.clear(); + sz = tree[id].pt.size(); + if (flag == 0) + { + sort(tree[id].pt.begin(), tree[id].pt.end(), cmp1); + mid = (sz - 1) / 2; + mx = tree[id].pt[mid].x; + for (i = 0; i < sz; i++) + { + if (i <= mid)//if(tree[id].pt[i].x<=mx) + { + tree[l].pt.push_back(tree[id].pt[i]); + tree[l].x.push_back(tree[id].pt[i].x); + tree[l].y.push_back(tree[id].pt[i].y); + } + else + { + tree[r].pt.push_back(tree[id].pt[i]); + tree[r].x.push_back(tree[id].pt[i].x); + tree[r].y.push_back(tree[id].pt[i].y); + } + } + } + else + { + sort(tree[id].pt.begin(), tree[id].pt.end(), cmp2); + mid = (sz - 1) / 2; + my = tree[id].pt[mid].y; + for (i = 0; i < sz; i++) + { + if (i <= mid) + { + tree[l].pt.push_back(tree[id].pt[i]); + tree[l].x.push_back(tree[id].pt[i].x); + tree[l].y.push_back(tree[id].pt[i].y); + } + else + { + tree[r].pt.push_back(tree[id].pt[i]); + tree[r].x.push_back(tree[id].pt[i].x); + tree[r].y.push_back(tree[id].pt[i].y); + } + } + } + sort(tree[l].x.begin(), tree[l].x.end()); + unique(tree[l].x.begin(), tree[l].x.end()); + sort(tree[l].y.begin(), tree[l].y.end()); + unique(tree[l].y.begin(), tree[l].y.end()); + sort(tree[r].x.begin(), tree[r].x.end()); + unique(tree[r].x.begin(), tree[r].x.end()); + sort(tree[r].y.begin(), tree[r].y.end()); + unique(tree[r].y.begin(), tree[r].y.end()); + build(l); + build(r); +} +bool intersect(int x, int y, int r, int id) +{ + int i, j, s, p, q, lx, ly, rx, ry, szx, szy; + szx = tree[id].x.size(); + szy = tree[id].y.size(); + lx = tree[id].x[0]; + ly = tree[id].y[0]; + rx = tree[id].x[szx - 1]; + ry = tree[id].y[szy - 1]; + if (x > lx && x < rx && y < ry && y > ly) + { + return true; + } + if (x >= rx + r || x <= lx - r || y >= ry + r || y <= ly - r) + { + return false; + } + if ((double)(lx - x) * (double)(lx - x) + (double)(ly - y) * (double)(ly - y) < r * r - eps) + { + return true; + } + if ((double)(lx - x) * (double)(lx - x) + (double)(ry - y) * (double)(ry - y) < r * r - eps) + { + return true; + } + if ((double)(rx - x) * (double)(rx - x) + (double)(ly - y) * (double)(ly - y) < r * r - eps) + { + return true; + } + if ((double)(rx - x) * (double)(rx - x) + (double)(ry - y) * (double)(ry - y) < r * r - eps) + { + return true; + } + return (x > lx && x < rx) || (y > ly && y < ry); +} +bool inner(int x, int y, int ri, int id) +{ + int i, j, s, p, q, lx, ly, rx, ry, szx, szy; + szx = tree[id].x.size(); + szy = tree[id].y.size(); + lx = tree[id].x[0]; + ly = tree[id].y[0]; + rx = tree[id].x[szx - 1]; + ry = tree[id].y[szy - 1]; + if ((double)(lx - x) * (double)(lx - x) + (double)(ly - y) * (double)(ly - y) >= r * r - eps) + { + return false; + } + if ((double)(lx - x) * (double)(lx - x) + (double)(ry - y) * (double)(ry - y) >= r * r - eps) + { + return false; + } + if ((double)(rx - x) * (double)(rx - x) + (double)(ly - y) * (double)(ly - y) >= r * r - eps) + { + return false; + } + if ((double)(rx - x) * (double)(rx - x) + (double)(ry - y) * (double)(ry - y) >= r * r - eps) + { + return false; + } + return true; +} +void query(int id, int x, int y, int ri) +{ + int i, j, s, p, q, l = 2 * id + 1, r = 2 * id + 2; + if (!intersect(x, y, ri, id)) + { + return; + } + if (inner(x, y, ri, id)) + { + ans += tree[id].pt.size(); + return; + } + if (tree[id].pt.size() <= 1) + { + ans++; + return; + } + query(l, x, y, ri); + query(r, x, y, ri); +} +int main() +{ + int i, j, s, p, q; + while (scanf("%d%d%d%d", &l, &h, &r, &n) == 4) + { + for (i = 0; i < n; i++) + { + scanf("%d%d", &pt[i].x, &pt[i].y); + } + tree[0].pt.clear(); + tree[0].x.clear(); + tree[0].y.clear(); + for (i = 0; i < n; i++) + { + tree[0].pt.push_back(pt[i]); + tree[0].x.push_back(pt[i].x); + tree[0].y.push_back(pt[i].y); + } + sort(tree[0].x.begin(), tree[0].x.end()); + unique(tree[0].x.begin(), tree[0].x.end()); + sort(tree[0].y.begin(), tree[0].y.end()); + unique(tree[0].y.begin(), tree[0].y.end()); + build(0); + ans = 0; + for (i = 0; i < n; i++) + { + query(0, pt[i].x, pt[i].y, r); + } + ans -= n; + while (ans % 2 != 0) + { + puts("orz"); + } + printf("%d\n", ans / 2); + } + return 0; +} diff --git a/11410.cpp b/11410.cpp new file mode 100644 index 0000000..5a13055 --- /dev/null +++ b/11410.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +char s1[30], s2[30], res[30]; +int set_[30]; + +int main() +{ + int count, i, j, len, alen, avn, lac, val; + long long temp, ord; + scanf("%d", &count); + while (count--) + { + scanf("%s%s", s1, s2); + memset(set_, 0, sizeof(set_)); + for (i = 0; s2[i]; i++) + { + set_[s2[i] - 'A'] = 1; + } + for (i = 0, avn = 0; i < 26; i++) + { + if (set_[i] == 0) + { + if (avn == 0) + { + lac = i; + } + avn++; + } + } + len = strlen(s1); + for (i = 1, ord = 0; i < len; i++) + { + for (j = 1, temp = 1; j < i; j++) + { + temp *= 26; + } + temp *= 25; + ord += temp; + } + for (i = len - 1, temp = 1; i >= 0; i--, temp *= 26) + { + val = s1[i] - 'A'; + if (i == 0) + { + val--; + } + ord += temp * val; + } + for (i = 1, temp = avn - 1;; i++, temp *= avn) + { + if (temp > ord) + { + break; + } + ord -= temp; + } + temp /= avn - 1; + alen = i; + for (i = 0; i < alen; i++, temp /= avn) + { + for (j = 0; j < 26; j++) + { + if (set_[j] == 0 && (i || j != lac)) + { + if (ord < temp) + { + break; + } + ord -= temp; + } + } + printf("%c", j + 'A'); + } + printf("\n"); + } + return 0; +} diff --git a/11411.cpp b/11411.cpp new file mode 100644 index 0000000..178e078 --- /dev/null +++ b/11411.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define N 5000001 + +int tbl[N], ary[N], hash_[400]; + +int cmp(const void *p, const void *q) +{ + return *(int *)p - *(int *)q; +} + +int main() +{ + int count, n, i, j, k, a, b, sq, left, right, max, times; + for (i = 1; i < N; i++) + { + tbl[i] = 1; + } + for (i = 4; i < N; i += 2) + { + tbl[i] = 2; + } + sq = (int)sqrt((double)N) + 1; + for (i = 3; i < sq; i += 2) + { + if (tbl[i] == 1) + { + for (j = i * i, k = i + i; j < N; j += k) + { + tbl[j] = i; + } + } + } + for (i = 2; i < N; i++) + { + if (tbl[i] == 1) + { + tbl[i] = 2; + } + else + { + for (j = i, k = 1; !(j % tbl[i]); j /= tbl[i], k++) + ; + /*if(i == 6) printf("%d %d %d\n", j, tbl[j], k);*/ + tbl[i] = tbl[j] * k; + } + /*if(tbl[i] >= 400) + { + printf("XDDD %d\n", tbl[i]); + }*/ + } + /*for(i=1; i<100; i++) + { + printf("%d: %d\n", i, tbl[i]); + }*/ + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &a, &b); + memset(hash_, 0, sizeof(hash_)); + for (i = 0, j = a; j <= b; j++) + { + if (hash_[tbl[j]] < 2) + { + ary[i++] = tbl[j]; + hash_[tbl[j]]++; + } + } + qsort(ary, i, sizeof(int), cmp); + n = i; + for (i = 1, left = right = ary[0], max = 0; i < n; i++) + { + if (left < right) + { + if (ary[i] - left > max) + { + max = ary[i] - left; + } + left = ary[i]; + } + else + { + if (ary[i] - right > max) + { + max = ary[i] - right; + } + right = ary[i]; + } + } + if (left > right) + { + if (right - left > max) + { + max = right - left; + } + } + else + { + if (left - right > max) + { + max = left - right; + } + } + printf("%d\n", max); + } + return 0; +} diff --git a/11412.cpp b/11412.cpp new file mode 100644 index 0000000..76a5aa3 --- /dev/null +++ b/11412.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +set all; +bool taken[6]; +char colors[6] = {'R', 'G', 'B', 'Y', 'O', 'V'}; + +void generate(int depth, string result) +{ + if (depth == 4) + { + all.insert(result); + return; + } + for (int i = 0; i < 6; i++) + { + if (taken[i]) + { + continue; + } + taken[i] = true; + generate(depth + 1, result + colors[i]); + taken[i] = false; + } +} + +void generate_all_possible_strings() +{ + for (int i = 0; i < 6; i++) + { + taken[i] = false; + } + generate(0, ""); +} + +pair compare(string &guess, const string &s) +{ + pair result = make_pair(0, 0); + bool guess_taken[4] = {false}; + bool s_taken[4] = {false}; + for (int i = 0; i < 4; i++) + { + if (guess[i] == s[i]) + { + result.first++; + guess_taken[i] = true; + s_taken[i] = true; + } + } + for (int i = 0; i < 4; i++) + { + if (guess_taken[i]) + { + continue; + } + for (int j = 0; j < 4; j++) + { + if (s_taken[j]) + { + continue; + } + if (guess[i] == s[j]) + { + result.second++; + guess_taken[i] = true; + s_taken[j] = true; + } + } + } + return result; +} + +set possible_matches(string &guess, int n1, int n2, set &strings) +{ + set result; + for (set::iterator it = strings.begin(); it != strings.end(); it++) + { + pair n = compare(guess, *it); + if (n.first == n1 && n.second == n2) + { + result.insert(*it); + } + } + return result; +} + +int main() +{ + int t, n1, n2; + string guess; + generate_all_possible_strings(); + cin >> t; + while (t--) + { + cin >> guess >> n1 >> n2; + set matches = possible_matches(guess, n1, n2, all); + cin >> guess >> n1 >> n2; + set matches2 = possible_matches(guess, n1, n2, matches); + if (matches2.empty()) + { + cout << "Cheat" << endl; + } + else + { + cout << "Possible" << endl; + } + } + return 0; +} diff --git a/11413.cpp b/11413.cpp new file mode 100644 index 0000000..967f669 --- /dev/null +++ b/11413.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int MAX_N = 1000; + +int N, M; +int cs[MAX_N]; + +bool init() +{ + bool cont = (scanf("%d%d", &N, &M) != EOF); + for (int i = 0; i < N; i++) + { + scanf("%d", cs + i); + } + return cont; +} + +bool check(int x) +{ + int cnt = 1, cur = 0; + if (*max_element(cs, cs + N) > x) + { + return false; + } + for (int i = 0; i < N; i++) + { + cur += cs[i]; + if (cur > x) + { + cur = cs[i]; + cnt++; + } + } + return cnt <= M; +} + +int solve() +{ + int low = 0, high = ((int)1e+9) + 20; + while (high - low > 1) + { + int mid = (low + high) / 2; + if (check(mid)) + { + high = mid; + } + else + { + low = mid; + } + } + return high; +} + +int main() +{ + while (init()) + { + printf("%d\n", solve()); + } + return 0; +} diff --git a/11414.cpp b/11414.cpp new file mode 100644 index 0000000..1783f18 --- /dev/null +++ b/11414.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int main() +{ + int t, n; + int paths[1010]; + cin >> t; + while (t--) + { + cin >> n; + if (n == 0) + { + cout << "Yes" << endl; + continue; + } + for (int i = 1; i <= n; i++) + { + cin >> paths[i]; + } + sort(paths + 1, paths + n + 1, greater()); + int sum = 0; + for (int i = 1; i <= n; i++) + { + sum += paths[i]; + } + if (sum % 2 != 0 || paths[1] >= n) + { + cout << "No" << endl; + continue; + } + bool ok = true; + for (int k = 1; k <= n; k++) + { + sum = 0; + for (int i = 1; i <= k; i++) + { + sum += paths[i]; + } + int tmp = 0; + for (int i = k + 1; i <= n; i++) + { + tmp += min(paths[i], k); + } + tmp += k * (k - 1); + if (sum > tmp) + { + ok = false; + break; + } + } + if (ok) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + return 0; +} diff --git a/11415.cpp b/11415.cpp new file mode 100644 index 0000000..4bf5316 --- /dev/null +++ b/11415.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define N 2750000 +static int *factors; +static int *counts; +int main() +{ + register int i, j, k; + int n, c; + factors = (int *)malloc((N) * sizeof(int)); + counts = (int *)malloc(10000001 * sizeof(int)); + factors[0] = 0; + factors[1] = 0; + for (i = 2; i < N; i++) + { + factors[i] = 0; + } + for (i = 2; i < N; i++) + { + if (!factors[i]) + { + factors[i] = 1; + } + for (j = 2; j <= i && (k = j * i) < N; j++) + { + if (!factors[k] && factors[j] == 1) + { + factors[k] = factors[i] + 1; + } + } + } + for (i = 2; i < N; i++) + { + factors[i] += factors[i - 1]; + } + j = 0; + for (i = 1; i < 10000001; i++) + { + for (; factors[j] <= i; j++) + ; + counts[i] = j; + } + scanf("%d", &c); + while (c > 0) + { + scanf("%d", &n); + printf("%d\n", counts[n]); + c--; + } + return 0; +} diff --git a/11416.cpp b/11416.cpp new file mode 100644 index 0000000..990b0cd --- /dev/null +++ b/11416.cpp @@ -0,0 +1,240 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back + +//typedef long long ll; +typedef int ll; +//const int N = 17576001; +const int N = 100000; +vector edge[N]; +int deg[N]; +ll val[N]; +string inp[N]; + +ll getnum(string &in, int &p) +{ + int sign = 1; + if (in[p] == '-') + { + sign = -1, p++; + } + ll ret = 0; + while (p < (int)in.size() && isdigit(in[p])) + { + ret = ret * 10 + in[p++] - '0'; + } + return ret * sign; +} + +//need test?? +int convert(string &in) +{ + int ret = 0; + int mul = 1; + for (int i = (int)in.size() - 1; i >= 0; i--) + { + ret += mul * (in[i] - 'A' + 1); + mul = mul * 26; + } + return ret - 1; +} + +int getcell(string &in, int &p, int c) +{ + string ret = ""; + int val = 0; + if (in[p] == '-') + { + p++; + } + while (p < (int)in.size() && isalpha(in[p])) + { + ret += in[p++]; + } + val = getnum(in, p); + return convert(ret) + (val - 1) * c;//column+row*c +} + +void readdata(string &in, int now, int c) +{ + if (in[0] == 'e') + { + in = in.substr(2, in.size() - 3); + int p = 0; + while (p < (int)in.size()) + { + if (isdigit(in[p])) + { + val[now] = getnum(in, p); + } + else if (isalpha(in[p])) + { + deg[now]++; + int tmp = getcell(in, p, c); + edge[tmp].pb(now); + } + else + { + p++; + } + } + } + else + { + int p = 0; + val[now] = getnum(in, p); + } +} + +ll exp(string &in, int &p, int c); +ll fact(string &in, int &p, int c); +ll num(string &in, int &P, int c); + +ll exp(string &in, int &p, int c) +{ + ll ret = fact(in, p, c); + while (p < (int)in.size()) + { + if (in[p] == '+') + { + p++; + ret += fact(in, p, c); + } + else if (in[p] == '-') + { + p++; + ret -= fact(in, p, c); + } + else + { + break; + } + } + return ret; +} + +ll fact(string &in, int &p, int c) +{ + ll ret = num(in, p, c); + while (p < (int)in.size()) + { + if (in[p] == '*') + { + p++; + ret *= num(in, p, c); + } + else if (in[p] == '/') + { + p++; + ret /= num(in, p, c); + } + else if (in[p] == '%') + { + p++; + ret %= num(in, p, c); + } + else + { + break; + } + } + return ret; +} + +ll num(string &in, int &p, int c) +{ + ll ret = 0; + if (in[p] == '(') + { + p++; + ret = exp(in, p, c); + p++; + } + else if (isdigit(in[p])) + { + ret = getnum(in, p); + } + else if (isalpha(in[p])) + { + int tmp = getcell(in, p, c); + ret = val[tmp]; + } + else if (p == 0 && in[p] == '-')//å¿…è¦ãªã„ã£ã½ã„ + { + p++; + ret = num(in, p, c); + } + return ret; +} + +void solve(int r, int c) +{ + queue Q; + rep(i, r * c) if (deg[i] == 0) + { + Q.push(i); + } + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + rep(i, (int)edge[now].size()) + { + int next = edge[now][i]; + deg[next]--; + if (deg[next] == 0) + { + int p = 0; + val[next] = exp(inp[next], p, c); + Q.push(next); + } + } + } +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int r, c; + cin >> r >> c; + rep(i, r * c) edge[i].clear(), deg[i] = 0, val[i] = 0; + rep(i, r * c) + { + cin >> inp[i]; + bool isexp = inp[i][0] == 'e'; + readdata(inp[i], i, c); + if (isexp && deg[i] == 0) + { + int p = 0; + val[i] = exp(inp[i], p, c); + } + } + solve(r, c); + rep(i, r) + { + rep(j, c) + { + if (j) + { + cout << " "; + } + //assert(deg[i*c+j] <= 0);//Runtime Error! + cout << val[i * c + j]; + //cout<< deg[i*c+j]; + } + cout << endl; + } + if (te) + { + cout << endl; + } + } + return false; +} diff --git a/11417.cpp b/11417.cpp index 0a811c2..285aba9 100644 --- a/11417.cpp +++ b/11417.cpp @@ -1,26 +1,49 @@ -#include -using namespace std; -#define limit 200001 -int GCD(long long a, long b){ - if(a==0) - return b; - else - return GCD(b%a,a); -} -int main(){ - long long gcd[limit][limit],k=0,i=0,j=0; - for(i=1;i>n; - if(n==0) - break; - for(i=1;i + +using namespace std; + +typedef pair p; + +int gcd(int a, int b) +{ + while (b) + { + a %= b; + swap(a, b); + } + return a; +} + +int main() +{ + int n; + int tmp; + int result; + map m; + while (cin >> n) + { + if (n == 0) + { + break; + } + result = 0; + for (int i = 1; i < n; i++) + { + for (int y = i + 1; y <= n; y++) + { + if (m.find(make_pair(i, y)) == m.end()) + { + tmp = gcd(i, y); + m[make_pair(i, y)] = tmp; + result += tmp; + } + else + { + result += m[make_pair(i, y)]; + } + } + } + cout << result << endl; + } + return 0; +} diff --git a/11418.cpp b/11418.cpp new file mode 100644 index 0000000..c855fc2 --- /dev/null +++ b/11418.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +// String + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 50000 +#define LL long long + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Pr(x) \ + for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) \ + cout << *it << " "; \ + cout << endl; +#define Set(a, s) memset(a, s, sizeof(a)) + +string str[26 + 3][26 + 3]; + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + string tmp; + while (testCase--) + { + int n; + scanf("%d", &n); + bool isRowValid[26 + 3]; + Set(isRowValid, true); + bool isLetter[26 + 3][26 + 3]; + Set(isLetter, false); + for (int i = 0; i < n; i++) + { + int k; + scanf("%d", &k); + for (int j = 0; j < k; j++) + { + cin >> tmp; + str[i][j] = ""; + for (size_t p = 0; p < tmp.size(); p++) + str[i][j] += tolower(tmp[p]); + isLetter[i][str[i][j].at(0) - 'a'] = true; + } + } + string output[26 + 3]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int cnt = 0; + int ind; + for (int k = 0; k < n; k++) + if (isRowValid[k] && isLetter[k][j]) + { + cnt++; + ind = k; + } + if (cnt == 1) + { + isRowValid[ind] = false; + for (int k = 0; k < n; k++) + if (str[ind][k].at(0) - 'a' == j) + { + output[i] = str[ind][k]; + j = n; + break; + } + } + } + } + sort(output, output + n); + printf("Case #%d:\n", ++cases); + for (int i = 0; i < n; i++) + { + printf("%c", toupper(output[i][0])); + for (size_t j = 1; j < output[i].size(); j++) + printf("%c", output[i][j]); + printf("\n"); + } + } + return 0; +} diff --git a/11419.cpp b/11419.cpp new file mode 100644 index 0000000..d266742 --- /dev/null +++ b/11419.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define M 1010 +#define N 1010 + +bool graph[M][N]; +bool seen[N]; +int matchL[M], matchR[N]; +int visitL[M], visitR[N]; +int n, m, l; +int minShot; + +bool bpm(int u) +{ + for (int v = 1; v <= n; v++) + if (graph[u][v]) + { + if (seen[v]) + { + continue; + } + seen[v] = true; + if (matchR[v] < 0 || bpm(matchR[v])) + { + matchL[u] = v; + matchR[v] = u; + return true; + } + } + return false; +} + +void input() +{ + memset(graph, false, sizeof(graph)); + int x, y, i; + for (i = 0; i < l; i++) + { + scanf("%d%d", &x, &y); + graph[x][y] = true; + } +} + +void dfs(int u) +{ + if (visitL[u]) + { + return; + } + visitL[u] = true; + for (int v = 1; v <= n; v++) + if (graph[u][v] && !visitR[v]) + { + visitR[v] = true; + dfs(matchR[v]); + } +} + +void output() +{ + memset(visitL, 0, sizeof(visitL)); + memset(visitR, 0, sizeof(visitR)); + for (int x = 1; x <= m; x++) + if (matchL[x] == -1) + { + dfs(x); + } + printf("%d", minShot); + for (int i = 1; i <= m; i++) + if (!visitL[i]) + { + printf(" r%d", i); + } + for (int j = 1; j <= n; j++) + if (visitR[j]) + { + printf(" c%d", j); + } + printf("\n"); +} + +void solve() +{ + memset(matchL, -1, sizeof(matchL)); + memset(matchR, -1, sizeof(matchR)); + minShot = 0; + for (int i = 1; i <= m; i++) + { + memset(seen, 0, sizeof(seen)); + if (bpm(i)) + { + minShot++; + } + } +} + +int main() +{ + while (scanf("%d%d%d", &m, &n, &l) != EOF && m) + { + input(); + solve(); + output(); + } + return 0; +} diff --git a/11420.cpp b/11420.cpp new file mode 100644 index 0000000..a331e24 --- /dev/null +++ b/11420.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +// Recur, dp + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 50000 +#define LL long long + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Pr(x) \ + for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) \ + cout << *it << " "; \ + cout << endl; +#define Set(a, s) memset(a, s, sizeof(a)) + +enum State +{ + UnLocked, + Locked, + SecureLocked +}; + +LL dp[65 + 3][3 + 2][65 + 3]; +int n, s; + +LL recur(int at, State pre, int cntSecure) +{ + if (at == n) + return cntSecure == s ? 1 : 0; + if (dp[at][pre][cntSecure] != -1) + return dp[at][pre][cntSecure]; + LL c = 0; + if (pre == UnLocked) + { + c += recur(at + 1, UnLocked, cntSecure); + c += recur(at + 1, Locked, cntSecure); + } + else if (pre == Locked) + { + c += recur(at + 1, UnLocked, cntSecure); + c += recur(at + 1, SecureLocked, cntSecure + 1); + } + else + { + c += recur(at + 1, UnLocked, cntSecure); + c += recur(at + 1, SecureLocked, cntSecure + 1); + } + return dp[at][pre][cntSecure] = c; +} + +int main() +{ + while (scanf("%d %d", &n, &s)) + { + if (n < 0 && s < 0) + break; + Set(dp, -1); + printf("%lld\n", recur(0, Locked, 0)); + } + return 0; +} diff --git a/11421.cpp b/11421.cpp new file mode 100644 index 0000000..e05b6f0 --- /dev/null +++ b/11421.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +int pnum[5] = {1, 1, 2, 3, 5}; +int part[5][5] = {{0}, {1}, {2, 1}, {3, 2, 1}, {4, 3, 2, 2, 1}}; +int cost[5][5] = {{0}, {0}, {0, 1}, {0, 1, 2}, {0, 1, 2, 2, 3}}; +int perm[5][5] = {{1}, {1}, {1, 2}, {1, 6, 6}, {1, 12, 24, 12, 24}}; + +char ary[55][5]; +double dp[25][55]; +int tbl[20], hash_[128], lst[55], used[55]; +long long c[55][55], fac[10], m; + +int cmp(const void *p, const void *q) +{ + if (*(char *)p != *(char *)q) + { + return hash_[*(char *)p] - hash_[*(char *)q]; + } + return *((char *)p + 1) - *((char *)q + 1); +} + +long long calc(int head) +{ + int i, j, k, l, p, q, r, sum; + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (i = 1, sum = 0; i <= 14; i++) + { + if (i == head) + { + tbl[14] = tbl[i]; + tbl[i] = 0; + } + else if (i == 14) + { + sum--; + } + for (j = 0; j < pnum[tbl[i]]; j++) + { + p = part[tbl[i]][j]; + q = cost[tbl[i]][j]; + r = perm[tbl[i]][j]; + for (k = 0; k <= sum + 1; k++) + { + for (l = 0; l <= p && l <= k; l++) + { + dp[i][k - l + q] += dp[i - 1][k] * c[p][l] * c[k][l] * fac[l] * c[sum - k + 1][p - l] * fac[p - l] * r; + } + } + } + sum += tbl[i]; + } + tbl[head] = tbl[14]; + return dp[14][0] > m ? m + 1 : (long long)dp[14][0]; +} + +int main() +{ + int cas, n, i, j, last; + long long cnt; + cas = 0; + c[0][0] = 1; + for (i = 1; i < 55; i++) + { + c[i][0] = 1; + for (j = 1; j < 55; j++) + { + c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; + } + } + fac[0] = 1; + for (i = 1; i < 10; i++) + { + fac[i] = fac[i - 1] * i; + } + for (i = '2'; i <= '9'; i++) + { + hash_[i] = i - '1'; + } + hash_['T'] = 13; + hash_['J'] = 10; + hash_['Q'] = 12; + hash_['K'] = 11; + hash_['A'] = 9; + while (scanf("%d%lld", &n, &m) == 2) + { + if (n == 0) + { + break; + } + memset(tbl, 0, sizeof(tbl)); + for (i = 0; i < n; i++) + { + scanf("%s", ary[i]); + used[i] = 0; + tbl[hash_[ary[i][0]]]++; + } + qsort(ary, n, sizeof(ary[0]), cmp); + for (i = 0; i < n; i++) + { + for (j = 0, last = 0; j < n; j++) + { + if (used[j] == 0 && (i == 0 || *ary[j] != *ary[lst[i - 1]])) + { + tbl[hash_[ary[j][0]]]--; + cnt = calc(hash_[ary[j][0]]); + if (cnt >= m) + { + lst[i] = j; + used[j] = 1; + break; + } + m -= cnt; + tbl[hash_[ary[j][0]]]++; + last = cnt; + } + } + if (j >= n) + { + break; + } + } + if (i < n) + { + printf("Case %d: Not found\n", ++cas); + } + else + { + printf("Case %d:", ++cas); + for (i = 0; i < n; i++) + { + printf(" %s", ary[lst[i]]); + } + printf("\n"); + } + } + return 0; +} diff --git a/11422.cpp b/11422.cpp new file mode 100644 index 0000000..e36239c --- /dev/null +++ b/11422.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +const double eps = 1e-12; +const double PI = acos(-1); +int sgn(double x) +{ + return (x < -eps) ? -1 : (x > eps); +} + +//2D PT +struct PT +{ + double x, y; + PT() {} + PT(double x, double y) : x(x), y(y) {} + PT operator+(const PT o) + { + return PT(x + o.x, y + o.y); + } + PT operator-(const PT o) + { + return PT(x - o.x, y - o.y); + } + PT operator*(double s) + { + return PT(x * s, y * s); + } + PT operator/(double s) + { + return PT(x / s, y / s); + } + bool operator<(const PT &o) const + { + return x < o.x - eps || (x < o.x + eps && y < o.y - eps); + } + bool operator==(const PT &o) const + { + return !sgn(y - o.y) && !sgn(x - o.x); + } + bool operator!=(const PT &o) const + { + return sgn(y - o.y) || sgn(x - o.x); + } + void rd() + { + scanf("%lf %lf", &x, &y); + } + double ag() + { + return atan2(y, x); + } +}; + +double cpr(PT a, PT b) +{ + return a.x * b.y - a.y * b.x; +} +double dpr(PT a, PT b) +{ + return a.x * b.x + a.y * b.y; +} + +double cpr(PT a, PT b, PT c) +{ + return cpr(b - a, c - a); +} +double dpr(PT a, PT b, PT c) +{ + return dpr(b - a, c - a); +} + +double vlen(PT a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} +double dist(PT a, PT b) +{ + return vlen(a - b); +} + +//判两点ab与直线cd相对ä½Âç½®, 点在直线上0, åŒ侧1, 异侧-1 +int plside(PT a, PT b, PT c, PT d) +{ + return sgn(cpr(c, a, d) * cpr(c, b, d)); +} + +//点p到直线abè·Â离 +double disptoline(PT p, PT a, PT b) +{ + return fabs(cpr(p, a, b)) / dist(a, b); +} + +//点到线段è·Â离 +double disptoseg(PT p, PT a, PT b) +{ + PT t = p + PT(a.y - b.y, b.x - a.x); + if (plside(a, b, p, t) == 1) + { + return min(dist(p, a), dist(p, b)); + } + return disptoline(p, a, b); +} + +//点v绕ç€点p逆时针旋转angle并放大scale倠+PT protate(PT v, PT p, double ag, double scale) +{ + PT t = PT(cos(ag), sin(ag)) * scale; + v = v - p; + p.x += v.x * t.x - v.y * t.y; + p.y += v.x * t.y + v.y * t.x; + return p; +} + +int check(PT po, PT p[], int n) +{ + double q[5]; + for (int i = 0; i < n; i++) + { + q[i] = disptoseg(po, p[i], p[i + 1]); + } + for (int i = 0; i < n; i++) + for (int j = 0; j < i; j++) + if (sgn(q[i] - q[j]) == 0) + { + return -1; + } + int ans = 0; + for (int i = 0; i < n; i++) + if (q[ans] > q[i]) + { + ans = i; + } + return ans; +} + +int work(PT p[], PT po) +{ + for (int i = 1; i < 15; i++) + { + int mark = check(po, p, i == 1 ? 3 : 4); + if (mark == -1) + { + return 15; + } + if (cpr(p[mark], p[mark + 1], po) > eps) + { + return i; + } + p[0] = p[mark]; + p[4] = p[mark + 1]; + p[1] = p[0] + (p[4] - p[0]) / 3; + p[3] = p[4] - (p[4] - p[0]) / 3; + p[2] = protate(p[0], p[1], PI * 2 / 3, 1); + } + return 15; +} + +int main() +{ + //freopen("1.txt", "r", stdin); + int cas, T; + for (cas = scanf("%d", &T); cas <= T; cas++) + { + PT p[4]; + for (int i = 0; i < 3; i++) + { + p[i].rd(); + } + PT po; + po.rd(); + if (cpr(p[0], p[1], p[2]) < eps) + { + reverse(p, p + 3); + } + p[3] = p[0]; + int ans = work(p, po); + if (ans < 15) + { + printf("The bacterium destroys you within day %d. ESCAPE!!\n", ans); + } + else + { + puts("You are safe :)"); + } + } + return 0; +} diff --git a/11423.cpp b/11423.cpp new file mode 100644 index 0000000..764c5bc --- /dev/null +++ b/11423.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +char cmd[105]; +int n, pp, tt, ecnt, csiz[35], ans[35], bin[30], tre[30000000], hash_[17500000]; + +void del(int tar) +{ + int i, loc, cc, temp; + temp = tar; + for (i = 0; tar; i++) + { + bin[i] = (tar & 1); + tar >>= 1; + } + for (; i < 24; i++) + { + bin[i] = 0; + } + for (i = 23, loc = 1, cc = 0; i >= 0; i--) + { + if (bin[i]) + { + cc += tre[loc]; + loc = (loc << 1) + 1; + } + else + { + tre[loc]++; + loc <<= 1; + } + } + /*printf("%d ", cc);*/ + cc = tt - (temp - cc); + /*printf("%d\n", cc);*/ + for (i = 0; i < n; i++) + { + if (csiz[i] > cc) + { + break; + } + ans[i]++; + } +} + +void insert(int tar) +{ + if (hash_[tar] > 0) + { + del(hash_[tar]); + } + else + { + ecnt++; + tt++; + } + hash_[tar] = ++pp; +} + +int main() +{ + int i, p, q, r; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + { + scanf("%d", &csiz[i]); + } + memset(hash_, 0, sizeof(hash_)); + memset(ans, 0, sizeof(ans)); + ecnt = 0; + pp = 0; + tt = 0; + while (scanf("%s", cmd) == 1) + { + if (cmd[0] == 'R') + { + scanf("%d%d%d", &p, &q, &r); + for (i = 0; i < r; i++) + { + insert(p + q * i); + } + } + else if (cmd[0] == 'A') + { + scanf("%d", &p); + insert(p); + } + else if (cmd[0] == 'S') + { + printf("%d", ans[0] + ecnt); + for (i = 1; i < n; i++) + { + printf(" %d", ans[i] + ecnt); + } + printf("\n"); + memset(ans, 0, sizeof(ans)); + ecnt = 0; + } + else + { + break; + } + } + } + return 0; +} diff --git a/11424.cpp b/11424.cpp new file mode 100644 index 0000000..d9b13c3 --- /dev/null +++ b/11424.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int phi[1000100]; +long long a[1000100]; +void phi_table(int n) +{ + for (int i = 2; i <= n; i++) + { + phi[i] = 0; + } + phi[1] = 1; + for (int i = 2; i <= n; i++) + if (!phi[i]) + for (int j = i; j <= n; j += i) + { + if (!phi[j]) + { + phi[j] = j; + } + phi[j] = phi[j] / i * (i - 1); + } +} +void gcd_sum(int n) +{ + for (int i = 2; i < n; i++) + { + a[i] = phi[i]; + } + double k = sqrt(0.5 + n); + for (int i = 2; i <= k; i++) + { + for (int j = i, bit = 1; j < n; j += i, bit++) + { + if (i < bit) + { + a[j] += phi[bit] * i + phi[i] * bit; + } + else if (i == bit) + { + a[j] += phi[i] * i; + } + } + } +} +void gcd_sum_again(int n) +{ + for (int i = 1; i < n; i++) + { + a[i] += a[i - 1]; + } +} +int main() +{ + phi_table(1000100); + gcd_sum(1000100); + gcd_sum_again(1000100); + int n; + while (cin >> n && n) + { + cout << a[n] << endl; + } + return 0; +} diff --git a/11426.cpp b/11426.cpp new file mode 100644 index 0000000..4f92848 --- /dev/null +++ b/11426.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +const int MAX = 4000100; +typedef long long int ll; +ll eulerphi[MAX], G[MAX]; +void precompute() +{ + for (int i = 0; i < MAX; i++) + { + eulerphi[i] = i; + } + for (int i = 2; i < MAX; i++) + { + if (eulerphi[i] == i) + { + eulerphi[i]--; + for (int j = 2 * i; j < MAX; j += i) + { + eulerphi[j] *= (i - 1); + eulerphi[j] /= i; + } + } + } +} +int main() +{ + precompute(); + for (ll k = 1; k <= MAX; k++) + { + G[k] = 0; + } + for (ll k = 1; k <= MAX; k++) + for (ll n = 2 * k; n <= MAX; n += k) + { + G[n] += k * eulerphi[n / k]; + } + for (ll k = 2; k <= MAX; k++) + { + G[k] += G[k - 1]; + } + int num; + while (scanf("%d", &num) == 1) + { + if (num == 0) + { + break; + } + printf("%lld\n", G[num]); + } +} diff --git a/11427.cpp b/11427.cpp new file mode 100644 index 0000000..327ffbe --- /dev/null +++ b/11427.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +const int MAXN = 100 + 10; +double dp[MAXN][MAXN]; +int a, b, n; + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d/%d%d", &a, &b, &n); + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1.0; + for (int i = 1; i <= n; ++i) + { + for (int j = 0; j * b <= a * i; ++j) + { + dp[i][j] = dp[i - 1][j] * (b - a) / b; + if (j) + { + dp[i][j] += dp[i - 1][j - 1] * a / b; + } + } + } + double p = 0; + for (int j = 0; j * b <= a * n; ++j) + { + p += dp[n][j]; + } + printf("Case #%d: %d\n", cas, (int)(1.0 / p)); + } + return 0; +} diff --git a/11428.cpp b/11428.cpp index d1438ed..def9a9b 100644 --- a/11428.cpp +++ b/11428.cpp @@ -1,57 +1,47 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - vector cube; - map Map; - int i, j; - for (i = 0; i <= 200; i++){ - int val = i * i * i; - Map[val] = i; - cube.push_back(val); +int main() +{ + int n; + int tmp; + bool found; + vector cubes; + cubes.push_back(1); + for (int i = 2;; i++) + { + tmp = pow(i, 3); + if (tmp - cubes[cubes.size() - 1] > 10000) + { + break; + } + cubes.push_back(tmp); } - while( true ){ - int N; - cin >> N; - if( N == 0 ) + while (cin >> n) + { + if (n == 0) + { break; - bool flag = false; - FOI(i, 0, 100){ - if( Map.find(N + cube[i]) != Map.end() ){ - cout << Map[N + cube[i]] << " " << i << endl; - flag = true; - break; + } + found = false; + for (int i = 0, sz = cubes.size(); i < sz - 1; i++) + { + for (int j = i + 1; j < sz; j++) + { + if (cubes[j] - cubes[i] == n) + { + cout << cubes[j] / ((j + 1) * (j + 1)) << " " << cubes[i] / ((i + 1) * (i + 1)) << endl; + found = true; + j = sz; + i = sz; + } } } - if ( !flag ) - cout << "No solution\n"; + if (!found) + { + cout << "No solution" << endl; + } } return 0; } diff --git a/11430.cpp b/11430.cpp new file mode 100644 index 0000000..1d5279e --- /dev/null +++ b/11430.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +#define INF 100000000 + +typedef long long LL; + +LL comb[1010][1010]; + +int n; +int read() +{ + scanf("%d", &n); + return n; +} + +int process() +{ + int r; + LL resp = 0LL; + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + { + if ((i * j) % n != 0) + { + continue; + } + r = (i * j) / n; + resp = (resp + ((comb[n][i] * comb[i][r]) % INF * comb[n - i][j - r])) % INF; + } + printf("%lld\n", resp); +} + +int main() +{ + memset(comb, 0, sizeof(comb)); + comb[0][0] = 1; + for (int i = 1; i <= 1000; i++) + { + comb[i][0] = 1; + comb[i][1] = i; + comb[i][i] = 1; + for (int j = 2; j < i; j++) + { + comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % INF; + } + } + while (read()) + { + process(); + } + return 0; +} diff --git a/11432.cpp b/11432.cpp new file mode 100644 index 0000000..9bb6b1a --- /dev/null +++ b/11432.cpp @@ -0,0 +1,620 @@ +#include + +using namespace std; + +long long q[34][34]; + +int main() +{ + q[0][0] = 0ULL; + q[1][0] = 0ULL; + q[1][1] = 2ULL; + q[2][0] = 0ULL; + q[2][1] = 2ULL; + q[2][2] = 4ULL; + q[3][0] = 0ULL; + q[3][1] = 2ULL; + q[3][2] = 10ULL; + q[3][3] = 12ULL; + q[4][0] = 0ULL; + q[4][1] = 2ULL; + q[4][2] = 24ULL; + q[4][3] = 38ULL; + q[4][4] = 40ULL; + q[5][0] = 0ULL; + q[5][1] = 2ULL; + q[5][2] = 58ULL; + q[5][3] = 118ULL; + q[5][4] = 138ULL; + q[5][5] = 140ULL; + q[6][0] = 0ULL; + q[6][1] = 2ULL; + q[6][2] = 144ULL; + q[6][3] = 370ULL; + q[6][4] = 476ULL; + q[6][5] = 502ULL; + q[6][6] = 504ULL; + q[7][0] = 0ULL; + q[7][1] = 2ULL; + q[7][2] = 358ULL; + q[7][3] = 1176ULL; + q[7][4] = 1652ULL; + q[7][5] = 1814ULL; + q[7][6] = 1846ULL; + q[7][7] = 1848ULL; + q[8][0] = 0ULL; + q[8][1] = 2ULL; + q[8][2] = 896ULL; + q[8][3] = 3770ULL; + q[8][4] = 5784ULL; + q[8][5] = 6596ULL; + q[8][6] = 6824ULL; + q[8][7] = 6862ULL; + q[8][8] = 6864ULL; + q[9][0] = 0ULL; + q[9][1] = 2ULL; + q[9][2] = 2254ULL; + q[9][3] = 12148ULL; + q[9][4] = 20390ULL; + q[9][5] = 24138ULL; + q[9][6] = 25390ULL; + q[9][7] = 25694ULL; + q[9][8] = 25738ULL; + q[9][9] = 25740ULL; + q[10][0] = 0ULL; + q[10][1] = 2ULL; + q[10][2] = 5690ULL; + q[10][3] = 39320ULL; + q[10][4] = 72264ULL; + q[10][5] = 88810ULL; + q[10][6] = 94988ULL; + q[10][7] = 96798ULL; + q[10][8] = 97188ULL; + q[10][9] = 97238ULL; + q[10][10] = 97240ULL; + q[11][0] = 0ULL; + q[11][1] = 2ULL; + q[11][2] = 14408ULL; + q[11][3] = 127760ULL; + q[11][4] = 257176ULL; + q[11][5] = 328226ULL; + q[11][6] = 356990ULL; + q[11][7] = 366468ULL; + q[11][8] = 368968ULL; + q[11][9] = 369454ULL; + q[11][10] = 369510ULL; + q[11][11] = 369512ULL; + q[12][0] = 0ULL; + q[12][1] = 2ULL; + q[12][2] = 36578ULL; + q[12][3] = 416452ULL; + q[12][4] = 918418ULL; + q[12][5] = 1217618ULL; + q[12][6] = 1346828ULL; + q[12][7] = 1393038ULL; + q[12][8] = 1406872ULL; + q[12][9] = 1410208ULL; + q[12][10] = 1410800ULL; + q[12][11] = 1410862ULL; + q[12][12] = 1410864ULL; + q[13][0] = 0ULL; + q[13][1] = 2ULL; + q[13][2] = 93062ULL; + q[13][3] = 1361150ULL; + q[13][4] = 3289532ULL; + q[13][5] = 4531216ULL; + q[13][6] = 5097848ULL; + q[13][7] = 5313336ULL; + q[13][8] = 5383752ULL; + q[13][9] = 5403202ULL; + q[13][10] = 5407534ULL; + q[13][11] = 5408242ULL; + q[13][12] = 5408310ULL; + q[13][13] = 5408312ULL; + q[14][0] = 0ULL; + q[14][1] = 2ULL; + q[14][2] = 237212ULL; + q[14][3] = 4459198ULL; + q[14][4] = 11812310ULL; + q[14][5] = 16907704ULL; + q[14][6] = 19349884ULL; + q[14][7] = 20325088ULL; + q[14][8] = 20665064ULL; + q[14][9] = 20768240ULL; + q[14][10] = 20794788ULL; + q[14][11] = 20800290ULL; + q[14][12] = 20801124ULL; + q[14][13] = 20801198ULL; + q[14][14] = 20801200ULL; + q[15][0] = 0ULL; + q[15][1] = 2ULL; + q[15][2] = 605630ULL; + q[15][3] = 14638200ULL; + q[15][4] = 42510650ULL; + q[15][5] = 63236092ULL; + q[15][6] = 73624490ULL; + q[15][7] = 77944656ULL; + q[15][8] = 79528200ULL; + q[15][9] = 80043344ULL; + q[15][10] = 80189920ULL; + q[15][11] = 80225288ULL; + q[15][12] = 80232148ULL; + q[15][13] = 80233118ULL; + q[15][14] = 80233198ULL; + q[15][15] = 80233200ULL; + q[16][0] = 0ULL; + q[16][1] = 2ULL; + q[16][2] = 1548452ULL; + q[16][3] = 48138434ULL; + q[16][4] = 153286566ULL; + q[16][5] = 236990100ULL; + q[16][6] = 280727564ULL; + q[16][7] = 299564042ULL; + q[16][8] = 306752688ULL; + q[16][9] = 309219960ULL; + q[16][10] = 309976232ULL; + q[16][11] = 310179248ULL; + q[16][12] = 310225416ULL; + q[16][13] = 310233836ULL; + q[16][14] = 310234952ULL; + q[16][15] = 310235038ULL; + q[16][16] = 310235040ULL; + q[17][0] = 0ULL; + q[17][1] = 2ULL; + q[17][2] = 3964034ULL; + q[17][3] = 158555460ULL; + q[17][4] = 553676526ULL; + q[17][5] = 889767736ULL; + q[17][6] = 1072400690ULL; + q[17][7] = 1153527552ULL; + q[17][8] = 1185542046ULL; + q[17][9] = 1197009810ULL; + q[17][10] = 1200732906ULL; + q[17][11] = 1201814762ULL; + q[17][12] = 1202089994ULL; + q[17][13] = 1202149218ULL; + q[17][14] = 1202159414ULL; + q[17][15] = 1202160686ULL; + q[17][16] = 1202160778ULL; + q[17][17] = 1202160780ULL; + q[18][0] = 0ULL; + q[18][1] = 2ULL; + q[18][2] = 10159380ULL; + q[18][3] = 522975834ULL; + q[18][4] = 2002960154ULL; + q[18][5] = 3345938506ULL; + q[18][6] = 4103433804ULL; + q[18][7] = 4449448036ULL; + q[18][8] = 4589938776ULL; + q[18][9] = 4642056474ULL; + q[18][10] = 4659770412ULL; + q[18][11] = 4665243522ULL; + q[18][12] = 4666757552ULL; + q[18][13] = 4667123870ULL; + q[18][14] = 4667198700ULL; + q[18][15] = 4667210902ULL; + q[18][16] = 4667212340ULL; + q[18][17] = 4667212438ULL; + q[18][18] = 4667212440ULL; + q[19][0] = 0ULL; + q[19][1] = 2ULL; + q[19][2] = 26063778ULL; + q[19][3] = 1727153292ULL; + q[19][4] = 7255777360ULL; + q[19][5] = 12600310124ULL; + q[19][6] = 15724604490ULL; + q[19][7] = 17188756048ULL; + q[19][8] = 17798144992ULL; + q[19][9] = 18030935670ULL; + q[19][10] = 18113077906ULL; + q[19][11] = 18139732864ULL; + q[19][12] = 18147602368ULL; + q[19][13] = 18149681382ULL; + q[19][14] = 18150161130ULL; + q[19][15] = 18150254428ULL; + q[19][16] = 18150268880ULL; + q[19][17] = 18150270494ULL; + q[19][18] = 18150270598ULL; + q[19][19] = 18150270600ULL; + q[20][0] = 0ULL; + q[20][1] = 2ULL; + q[20][2] = 66927630ULL; + q[20][3] = 5710522128ULL; + q[20][4] = 26316762790ULL; + q[20][5] = 47512130950ULL; + q[20][6] = 60337763808ULL; + q[20][7] = 66492919048ULL; + q[20][8] = 69111471758ULL; + q[20][9] = 70137114758ULL; + q[20][10] = 70510510620ULL; + q[20][11] = 70636636906ULL; + q[20][12] = 70675866976ULL; + q[20][13] = 70686966784ULL; + q[20][14] = 70689774372ULL; + q[20][15] = 70690393770ULL; + q[20][16] = 70690508728ULL; + q[20][17] = 70690525688ULL; + q[20][18] = 70690527488ULL; + q[20][19] = 70690527598ULL; + q[20][20] = 70690527600ULL; + q[21][0] = 0ULL; + q[21][1] = 2ULL; + q[21][2] = 172002592ULL; + q[21][3] = 18900386926ULL; + q[21][4] = 95558112906ULL; + q[21][5] = 179363734600ULL; + q[21][6] = 231803745104ULL; + q[21][7] = 257538021580ULL; + q[21][8] = 268703419036ULL; + q[21][9] = 273172583086ULL; + q[21][10] = 274843338770ULL; + q[21][11] = 275426901750ULL; + q[21][12] = 275616341286ULL; + q[21][13] = 275672977756ULL; + q[21][14] = 275688370468ULL; + q[21][15] = 275692106060ULL; + q[21][16] = 275692895628ULL; + q[21][17] = 275693035786ULL; + q[21][18] = 275693055526ULL; + q[21][19] = 275693057522ULL; + q[21][20] = 275693057638ULL; + q[21][21] = 275693057640ULL; + q[22][0] = 0ULL; + q[22][1] = 2ULL; + q[22][2] = 442378948ULL; + q[22][3] = 62614641706ULL; + q[22][4] = 347332743012ULL; + q[22][5] = 677837693700ULL; + q[22][6] = 891508823762ULL; + q[22][7] = 998602642120ULL; + q[22][8] = 1045905544208ULL; + q[22][9] = 1065203684568ULL; + q[22][10] = 1072584588784ULL; + q[22][11] = 1075236815510ULL; + q[22][12] = 1076129116920ULL; + q[22][13] = 1076408247486ULL; + q[22][14] = 1076488630536ULL; + q[22][15] = 1076509655028ULL; + q[22][16] = 1076514559480ULL; + q[22][17] = 1076515554484ULL; + q[22][18] = 1076515723748ULL; + q[22][19] = 1076515746554ULL; + q[22][20] = 1076515748756ULL; + q[22][21] = 1076515748878ULL; + q[22][22] = 1076515748880ULL; + q[23][0] = 0ULL; + q[23][1] = 2ULL; + q[23][2] = 1138561068ULL; + q[23][3] = 207614117904ULL; + q[23][4] = 1263656526232ULL; + q[23][5] = 2564116871596ULL; + q[23][6] = 3432126547316ULL; + q[23][7] = 3876024070560ULL; + q[23][8] = 4075336500076ULL; + q[23][9] = 4158039488716ULL; + q[23][10] = 4190305602994ULL; + q[23][11] = 4202186247584ULL; + q[23][12] = 4206306387920ULL; + q[23][13] = 4207645072410ULL; + q[23][14] = 4208049415206ULL; + q[23][15] = 4208161767336ULL; + q[23][16] = 4208190093408ULL; + q[23][17] = 4208196455140ULL; + q[23][18] = 4208197696060ULL; + q[23][19] = 4208197898720ULL; + q[23][20] = 4208197924892ULL; + q[23][21] = 4208197927310ULL; + q[23][22] = 4208197927438ULL; + q[23][23] = 4208197927440ULL; + q[24][0] = 0ULL; + q[24][1] = 2ULL; + q[24][2] = 2932219698ULL; + q[24][3] = 688943049316ULL; + q[24][4] = 4601339724990ULL; + q[24][5] = 9708139203026ULL; + q[24][6] = 13225043430310ULL; + q[24][7] = 15058617404464ULL; + q[24][8] = 15894522215892ULL; + q[24][9] = 16246695275840ULL; + q[24][10] = 16386519539656ULL; + q[24][11] = 16439107171990ULL; + q[24][12] = 16457826114704ULL; + q[24][13] = 16464107415080ULL; + q[24][14] = 16466082117420ULL; + q[24][15] = 16466658926406ULL; + q[24][16] = 16466813796336ULL; + q[24][17] = 16466851487088ULL; + q[24][18] = 16466859648800ULL; + q[24][19] = 16466861181820ULL; + q[24][20] = 16466861422568ULL; + q[24][21] = 16466861452420ULL; + q[24][22] = 16466861455064ULL; + q[24][23] = 16466861455198ULL; + q[24][24] = 16466861455200ULL; + q[25][0] = 0ULL; + q[25][1] = 2ULL; + q[25][2] = 7556030582ULL; + q[25][3] = 2287854827704ULL; + q[25][4] = 16768036460448ULL; + q[25][5] = 36786586606620ULL; + q[25][6] = 51002938099216ULL; + q[25][7] = 58553880079334ULL; + q[25][8] = 62045533940676ULL; + q[25][9] = 63537045464700ULL; + q[25][10] = 64138510327472ULL; + q[25][11] = 64368970585782ULL; + q[25][12] = 64452885938688ULL; + q[25][13] = 64481837083664ULL; + q[25][14] = 64491254239880ULL; + q[25][15] = 64494122997720ULL; + q[25][16] = 64494934432656ULL; + q[25][17] = 64495145219226ULL; + q[25][18] = 64495194801858ULL; + q[25][19] = 64495205167850ULL; + q[25][20] = 64495207045370ULL; + q[25][21] = 64495207329318ULL; + q[25][22] = 64495207363178ULL; + q[25][23] = 64495207366058ULL; + q[25][24] = 64495207366198ULL; + q[25][25] = 64495207366200ULL; + q[26][0] = 0ULL; + q[26][1] = 2ULL; + q[26][2] = 19481799100ULL; + q[26][3] = 7602712521180ULL; + q[26][4] = 61150052704956ULL; + q[26][5] = 139498998224912ULL; + q[26][6] = 196847257858510ULL; + q[26][7] = 227860548262230ULL; + q[26][8] = 242394466939290ULL; + q[26][9] = 248681699979978ULL; + q[26][10] = 251252694046482ULL; + q[26][11] = 252254187526714ULL; + q[26][12] = 252626183887864ULL; + q[26][13] = 252757662994200ULL; + q[26][14] = 252801705796016ULL; + q[26][15] = 252815612162672ULL; + q[26][16] = 252819722079912ULL; + q[26][17] = 252820849063848ULL; + q[26][18] = 252821132630268ULL; + q[26][19] = 252821197176030ULL; + q[26][20] = 252821210220152ULL; + q[26][21] = 252821212501322ULL; + q[26][22] = 252821212834020ULL; + q[26][23] = 252821212872230ULL; + q[26][24] = 252821212875356ULL; + q[26][25] = 252821212875502ULL; + q[26][26] = 252821212875504ULL; + q[27][0] = 0ULL; + q[27][1] = 2ULL; + q[27][2] = 50255749198ULL; + q[27][3] = 25280315230376ULL; + q[27][4] = 223154210792230ULL; + q[27][5] = 529366659149082ULL; + q[27][6] = 760281802840532ULL; + q[27][7] = 887359685373752ULL; + q[27][8] = 947670808819750ULL; + q[27][9] = 974065790880876ULL; + q[27][10] = 984996156533904ULL; + q[27][11] = 989317060246416ULL; + q[27][12] = 990950558458968ULL; + q[27][13] = 991540292842456ULL; + q[27][14] = 991742967376392ULL; + q[27][15] = 991808972949584ULL; + q[27][16] = 991829225918216ULL; + q[27][17] = 991835038958592ULL; + q[27][18] = 991836585833064ULL; + q[27][19] = 991836963222720ULL; + q[27][20] = 991837046436768ULL; + q[27][21] = 991837062711026ULL; + q[27][22] = 991837065462302ULL; + q[27][23] = 991837065849756ULL; + q[27][24] = 991837065892672ULL; + q[27][25] = 991837065896054ULL; + q[27][26] = 991837065896206ULL; + q[27][27] = 991837065896208ULL; + q[28][0] = 0ULL; + q[28][1] = 2ULL; + q[28][2] = 129702613608ULL; + q[28][3] = 84110705375624ULL; + q[28][4] = 814866221456982ULL; + q[28][5] = 2010131021361552ULL; + q[28][6] = 2938383931021616ULL; + q[28][7] = 3457995076177038ULL; + q[28][8] = 3707586921822790ULL; + q[28][9] = 3818002834506332ULL; + q[28][10] = 3864253807844822ULL; + q[28][11] = 3882780906867470ULL; + q[28][12] = 3889895949208298ULL; + q[28][13] = 3892513421587370ULL; + q[28][14] = 3893433467939904ULL; + q[28][15] = 3893741306508536ULL; + q[28][16] = 3893838877467664ULL; + q[28][17] = 3893867998733272ULL; + q[28][18] = 3893876123663624ULL; + q[28][19] = 3893878223768312ULL; + q[28][20] = 3893878721034224ULL; + q[28][21] = 3893878827356168ULL; + q[28][22] = 3893878847500012ULL; + q[28][23] = 3893878850795734ULL; + q[28][24] = 3893878851244424ULL; + q[28][25] = 3893878851292416ULL; + q[28][26] = 3893878851296064ULL; + q[28][27] = 3893878851296222ULL; + q[28][28] = 3893878851296224ULL; + q[29][0] = 0ULL; + q[29][1] = 2ULL; + q[29][2] = 334891756078ULL; + q[29][3] = 279999927498078ULL; + q[29][4] = 2977295830047920ULL; + q[29][5] = 7637567267049728ULL; + q[29][6] = 11363475843366716ULL; + q[29][7] = 13484119877723600ULL; + q[29][8] = 14514535098773800ULL; + q[29][9] = 14974975430628290ULL; + q[29][10] = 15169876918138228ULL; + q[29][11] = 15248890147945212ULL; + q[29][12] = 15279665152323644ULL; + q[29][13] = 15291178169784520ULL; + q[29][14] = 15295306671178000ULL; + q[29][15] = 15296721282990928ULL; + q[29][16] = 15297182545151280ULL; + q[29][17] = 15297324955698824ULL; + q[29][18] = 15297366334419488ULL; + q[29][19] = 15297377566078080ULL; + q[29][20] = 15297380388396216ULL; + q[29][21] = 15297381037557168ULL; + q[29][22] = 15297381172273128ULL; + q[29][23] = 15297381197023448ULL; + q[29][24] = 15297381200946440ULL; + q[29][25] = 15297381201463338ULL; + q[29][26] = 15297381201516790ULL; + q[29][27] = 15297381201520714ULL; + q[29][28] = 15297381201520878ULL; + q[29][29] = 15297381201520880ULL; + q[30][0] = 0ULL; + q[30][1] = 2ULL; + q[30][2] = 865049041388ULL; + q[30][3] = 932582068790142ULL; + q[30][4] = 10884172222178146ULL; + q[30][5] = 29035625203872980ULL; + q[30][6] = 43970819219978344ULL; + q[30][7] = 52610922874355880ULL; + q[30][8] = 56855690704937484ULL; + q[30][9] = 58770388379488294ULL; + q[30][10] = 59588722018916134ULL; + q[30][11] = 59924107006819922ULL; + q[30][12] = 60056412046401490ULL; + q[30][13] = 60106659036149728ULL; + q[30][14] = 60125001595679036ULL; + q[30][15] = 60131420707000480ULL; + q[30][16] = 60133566766861328ULL; + q[30][17] = 60134249268419488ULL; + q[30][18] = 60134454681483968ULL; + q[30][19] = 60134512829812608ULL; + q[30][20] = 60134528197051000ULL; + q[30][21] = 60134531954077416ULL; + q[30][22] = 60134532794214736ULL; + q[30][23] = 60134532963581752ULL; + q[30][24] = 60134532993783608ULL; + q[30][25] = 60134532998425800ULL; + q[30][26] = 60134532999018388ULL; + q[30][27] = 60134532999077698ULL; + q[30][28] = 60134532999081908ULL; + q[30][29] = 60134532999082078ULL; + q[30][30] = 60134532999082080ULL; + q[31][0] = 0ULL; + q[31][1] = 2ULL; + q[31][2] = 2235354912966ULL; + q[31][3] = 3107597751196200ULL; + q[31][4] = 39809934104108994ULL; + q[31][5] = 110442696266238862ULL; + q[31][6] = 170236384286509508ULL; + q[31][7] = 205384493906393104ULL; + q[31][8] = 222836698940449600ULL; + q[31][9] = 230778930906436126ULL; + q[31][10] = 234203812723717848ULL; + q[31][11] = 235621506193387688ULL; + q[31][12] = 236187276390259806ULL; + q[31][13] = 236405088586596788ULL; + q[31][14] = 236485885088413522ULL; + q[31][15] = 236514699077081120ULL; + q[31][16] = 236524548634993232ULL; + q[31][17] = 236527764051212480ULL; + q[31][18] = 236528762113667200ULL; + q[31][19] = 236529055144006256ULL; + q[31][20] = 236529136016157512ULL; + q[31][21] = 236529156839996064ULL; + q[31][22] = 236529161797001480ULL; + q[31][23] = 236529162875511440ULL; + q[31][24] = 236529163086895112ULL; + q[31][25] = 236529163123513224ULL; + q[31][26] = 236529163128976296ULL; + q[31][27] = 236529163129652584ULL; + q[31][28] = 236529163129718164ULL; + q[31][29] = 236529163129722670ULL; + q[31][30] = 236529163129722846ULL; + q[31][31] = 236529163129722848ULL; + q[32][0] = 0ULL; + q[32][1] = 2ULL; + q[32][2] = 5778454292992ULL; + q[32][3] = 10359970148361378ULL; + q[32][4] = 145678830180938294ULL; + q[32][5] = 420298860758718184ULL; + q[32][6] = 659416738556075552ULL; + q[32][7] = 802200116115713418ULL; + q[32][8] = 873828162748036616ULL; + q[32][9] = 906699075871465896ULL; + q[32][10] = 920991713720631322ULL; + q[32][11] = 926962354026551216ULL; + q[32][12] = 929370391013487696ULL; + q[32][13] = 930308965268148796ULL; + q[32][14] = 930662203919840260ULL; + q[32][15] = 930790333476453034ULL; + q[32][16] = 930835011366698848ULL; + q[32][17] = 930849939881242288ULL; + q[32][18] = 930854701732577104ULL; + q[32][19] = 930856145279169312ULL; + q[32][20] = 930856558985543536ULL; + q[32][21] = 930856670373473800ULL; + q[32][22] = 930856698337222184ULL; + q[32][23] = 930856704823105192ULL; + q[32][24] = 930856706197123024ULL; + q[32][25] = 930856706459149288ULL; + q[32][26] = 930856706503280312ULL; + q[32][27] = 930856706509676360ULL; + q[32][28] = 930856706510444904ULL; + q[32][29] = 930856706510517180ULL; + q[32][30] = 930856706510521992ULL; + q[32][31] = 930856706510522174ULL; + q[32][32] = 930856706510522176ULL; + q[33][0] = 0ULL; + q[33][1] = 2ULL; + q[33][2] = 14942633925254ULL; + q[33][3] = 34552276926492628ULL; + q[33][4] = 533332306693999870ULL; + q[33][5] = 1600227841435041142ULL; + q[33][6] = 2555488483579847326ULL; + q[33][7] = 3134782637690969142ULL; + q[33][8] = 3428292851852704038ULL; + q[33][9] = 3564062776998303402ULL; + q[33][10] = 3623555016713104662ULL; + q[33][11] = 3648618023642658858ULL; + q[33][12] = 3658824518409873670ULL; + q[33][13] = 3662847735155320684ULL; + q[33][14] = 3664381943746607034ULL; + q[33][15] = 3664947049779210680ULL; + q[33][16] = 3665147654337827982ULL; + q[33][17] = 3665216094706172130ULL; + q[33][18] = 3665238461974002898ULL; + q[33][19] = 3665245437314601042ULL; + q[33][20] = 3665247503682873458ULL; + q[33][21] = 3665248082088942530ULL; + q[33][22] = 3665248234109890362ULL; + q[33][23] = 3665248271343216778ULL; + q[33][24] = 3665248279763154018ULL; + q[33][25] = 3665248281501166602ULL; + q[33][26] = 3665248281823888578ULL; + q[33][27] = 3665248281876774194ULL; + q[33][28] = 3665248281884226418ULL; + q[33][29] = 3665248281885096338ULL; + q[33][30] = 3665248281885175750ULL; + q[33][31] = 3665248281885180878ULL; + q[33][32] = 3665248281885181066ULL; + q[33][33] = 3665248281885181068ULL; + int n = 1ULL; + int D, G; + while (1) + { + scanf("%d %d", &D, &G); + if (D == -1) + { + break; + } + if (G > D) + { + G = D; + } + printf("Case %d: %lld\n", n++, q[D][G]); + } + return 0; +} diff --git a/11436.cpp b/11436.cpp index 1497daa..a84c29a 100644 --- a/11436.cpp +++ b/11436.cpp @@ -1,64 +1,50 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 50000 +#define MAX 50000 -typedef long long int64; -typedef unsigned long long uint64; +typedef long long LL; +typedef unsigned long long ULL; -int64 calc(double N, double K){ - double root1 = (-3*K + sqrt(12*N/K - 3*K*K))/6; - double root2 = (-3*K - sqrt(12*N/K - 3*K*K))/6; - if( root1 >= 0 ) - return (int64)root1; - return (int64)root2; +LL calc(double N, double K) +{ + double root1 = (-3 * K + sqrt(12 * N / K - 3 * K * K)) / 6; + double root2 = (-3 * K - sqrt(12 * N / K - 3 * K * K)) / 6; + if (root1 >= 0) + { + return (LL)root1; + } + return (LL)root2; } -int main(){ - while( true ){ - int64 N, K, k; - cin >> N; - if( N == 0 ) +int main() +{ + while (1) + { + LL N, K, k; + scanf("%lld", &N); + if (N == 0) + { break; - K = (int64) ceil(pow(N, 1/3.0)) + 1; + } + K = (LL)ceil(pow(N, 1 / 3.0)) + 1; bool stat = false; - FOI(k, 1, K){ - int64 Y = calc((double)N, (double)k); - int64 X = k + Y; - if( X >= 0 && Y >= 0 && X*X*X - Y*Y*Y == N ){ - cout << X << " " << Y << endl; + for (k = 1; k <= K; k++) + { + LL Y = calc((double)N, (double)k); + LL X = k + Y; + if (X >= 0 && Y >= 0 && X * X * X - Y * Y * Y == N) + { + printf("%lld %lld\n", X,Y); stat = true; break; } } - if ( !stat ) - cout << "No solution\n"; + if (!stat) + { + printf("No solution\n"); + } } return 0; } diff --git a/11437.cpp b/11437.cpp index b62ca8f..8dff264 100644 --- a/11437.cpp +++ b/11437.cpp @@ -1,26 +1,80 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - int test; - cin>>test; - while(test--){ - ldouble Ax,Ay,Bx,By,Cx,Cy; - cin>>Ax>>Ay>>Bx>>By>>Cx>>Cy; - ldouble sideA,sideB,sideC; - sideA=sqrt((Ax-Bx)*(Ax-Bx) + (Ay-By)*(Ay-By)); - sideB=sqrt((Bx-Cx)*(Bx-Cx) + (By-Cy)*(By-Cy)); - sideC=sqrt((Ax-Cx)*(Ax-Cx) + (Ay-Cy)*(Ay-Cy)); - ldouble peri=(sideA+sideB+sideC)/2; - ldouble area=peri*(peri-sideA)*(peri-sideB)*(peri-sideC); - area=sqrt(area)/7; - cout<<(long long)area< + +using namespace std; + +const int MAXN = 3; + +struct Point +{ + double x, y; + void input() + { + scanf("%lf%lf", &x, &y); + } +} p[MAXN], m[MAXN]; + +struct Line +{ + double A, B, C; +} l[MAXN]; + +Point get_pt(Point a, Point b) +{ + double x = a.x + (b.x - a.x) * 2 / 3; + double y = a.y + (b.y - a.y) * 2 / 3; + return (Point){x, y}; +} + +Line get_line(Point a, Point b) +{ + double A = b.y - a.y; + double B = a.x - b.x; + double C = a.y * b.x - a.x * b.y; + return (Line){A, B, C}; +} + +Point get_c_pt(Line a, Line b) +{ + double x = (a.B * b.C - a.C * b.B) / (a.A * b.B - b.A * a.B); + double y = (b.C * a.A - a.C * b.A) / (a.B * b.A - a.A * b.B); + return (Point){x, y}; +} + +double area(Point a, Point b, Point c) +{ + double A = a.x * b.y - b.x * a.y; + double B = b.x * c.y - c.x * b.y; + double C = c.x * a.y - a.x * c.y; + return fabs((A + B + C) / 2); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + for (int i = 0; i < MAXN; i++) + { + p[i].input(); + } + + for (int i = 0; i < MAXN; i++) + { + m[i] = get_pt(p[i], p[(i + 1) % MAXN]); + } + + for (int i = 0; i < MAXN; i++) + { + l[i] = get_line(p[i], m[(i + 1) % MAXN]); + } + + for (int i = 0; i < MAXN; i++) + { + p[i] = get_c_pt(l[i], l[(i + 1) % MAXN]); + } + + printf("%.0lf\n", area(p[0], p[1], p[2])); + } + return 0; +} diff --git a/11439.cpp b/11439.cpp new file mode 100644 index 0000000..7aeb88f --- /dev/null +++ b/11439.cpp @@ -0,0 +1,196 @@ +#include + +using namespace std; + +#define MAXV 64 +#define MAXE 2 * 2016 + +int prev_edge[MAXE], v[MAXE], w[MAXE], last_edge[MAXV]; +int type[MAXV], label[MAXV], first[MAXV], mate[MAXV], nedges; +bool g_flag[MAXV], g_souter[MAXV]; + +void g_init() +{ + nedges = 0; + memset(last_edge, -1, sizeof(last_edge)); +} + +void g_edge(int a, int b) +{ + prev_edge[nedges] = last_edge[a]; + v[nedges] = a; + w[nedges] = b; + last_edge[a] = nedges++; + prev_edge[nedges] = last_edge[b]; + v[nedges] = b; + w[nedges] = a; + last_edge[b] = nedges++; +} + +void g_label(int v, int join, int edge, queue &outer) +{ + if (v == join) + { + return; + } + if (label[v] == -1) + { + outer.push(v); + } + label[v] = edge; + type[v] = 1; + first[v] = join; + g_label(first[label[mate[v]]], join, edge, outer); +} + +void g_augment(int _v, int _w) +{ + int t = mate[_v]; + mate[_v] = _w; + if (mate[t] != _v) + { + return; + } + if (label[_v] == -1) + { + return; + } + if (type[_v] == 0) + { + mate[t] = label[_v]; + g_augment(label[_v], t); + } + else if (type[_v] == 1) + { + g_augment(v[label[_v]], w[label[_v]]); + g_augment(w[label[_v]], v[label[_v]]); + } +} + +int gabow(int n) +{ + memset(mate, -1, sizeof(mate)); + memset(first, -1, sizeof(first)); + int u = 0, ret = 0; + for (int z = 0; z < n; ++z) + { + if (mate[z] != -1) + { + continue; + } + memset(label, -1, sizeof(label)); + memset(type, -1, sizeof(type)); + memset(g_souter, 0, sizeof(g_souter)); + label[z] = -1; + type[z] = 0; + queue outer; + outer.push(z); + bool done = false; + while (!outer.empty()) + { + int x = outer.front(); + outer.pop(); + if (g_souter[x]) + { + continue; + } + g_souter[x] = true; + for (int i = last_edge[x]; i != -1; i = prev_edge[i]) + { + if (mate[w[i]] == -1 && w[i] != z) + { + mate[w[i]] = x; + g_augment(x, w[i]); + ++ret; + done = true; + break; + } + if (type[w[i]] == -1) + { + int v = mate[w[i]]; + if (type[v] == -1) + { + type[v] = 0; + label[v] = x; + outer.push(v); + first[v] = w[i]; + } + continue; + } + int r = first[x], s = first[w[i]]; + if (r == s) + { + continue; + } + memset(g_flag, 0, sizeof(g_flag)); + g_flag[r] = g_flag[s] = true; + while (true) + { + if (s != -1) + { + swap(r, s); + } + r = first[label[mate[r]]]; + if (g_flag[r]) + { + break; + } + g_flag[r] = true; + } + g_label(first[x], r, i, outer); + g_label(first[w[i]], r, i, outer); + for (int c = 0; c < n; ++c) + if (type[c] != -1 && first[c] != -1 && type[first[c]] != -1) + { + first[c] = r; + } + } + if (done) + { + break; + } + } + } + return ret; +} + +int main() +{ + int T, W, N, M, icpc[64][64], L[2016]; + scanf("%d", &T); + for (int tc = 1; tc <= T; ++tc) + { + scanf("%d", &W); + N = (1 << W); + M = 0; + for (int i = 0; i < N; ++i) + for (int j = i + 1; j < N; ++j) + { + scanf("%d", &icpc[i][j]); + L[M++] = icpc[i][j]; + } + sort(L, L + M); + int lo = 0, hi = M - 1, mi; + while (lo != hi) + { + mi = (lo + hi + 1) / 2; + g_init(); + for (int i = 0; i < N; ++i) + for (int j = i + 1; j < N; ++j) + if (icpc[i][j] >= L[mi]) + { + g_edge(i, j); + } + if (gabow(N) == N / 2) + { + lo = mi; + } + else + { + hi = mi - 1; + } + } + printf("Case %d: %d\n", tc, L[lo]); + } + return 0; +} diff --git a/11440.cpp b/11440.cpp new file mode 100644 index 0000000..725f59a --- /dev/null +++ b/11440.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +#define MOD 100000007 +#define MX 10000000 +#define ull unsigned long long +#define ll long long int +#define pb push_back + +template +inline T _bigmod(T n, X m) +{ + ull ret = 1, a = n % MOD; + while (m) + { + if (m & 1) + ret = (ret * a) % MOD; + m >>= 1; + a = (a * a) % MOD; + } + ret %= MOD; + return (T)ret; +} +template +inline T _modinv(T n) { return _bigmod(n, MOD - 2); } + +ll fact[MX + 10]; +void factorial() +{ + + fact[0] = 1; + for (int i = 1; i <= MX; i++) + fact[i] = (i * fact[i - 1]) % MOD; +} + +bool chk[MX + 10]; +void gen_prime() +{ + + // primes.pb(1); + // primes.pb(2); + + int lmt = sqrt((double)MX); + + for (int i = 4; i <= MX; i += 2) + chk[i] = true; + + for (int i = 3; i <= lmt; i += 2) + { + if (!chk[i]) + { + // primes.pb(i); + for (int j = i * 3; j <= MX; j += 2 * i) + { + chk[j] = true; + } + } + } + + // for(int i=3;i<=MX;i+=2) + // if(!chk[i]) primes.pb(i); + + // cout<> n >> m) + { + if (n == 0 && m == 0) + break; + + // cout< + +using namespace std; + +const int MAXN = 100001, MOD = 1000000009, MAXK = 21; +typedef long long Int64; +Int64 S[MAXN][MAXK], C[MAXK]; +int n, q, Seq[MAXN];//--C(k,0), C(k,1), C(k,2)... C(k, k); + +//ÃîS(n, k) = ¡Æseq[i]*ik, i = 1 to n + +Int64 pow_mod(int i, int k) +{ + if (k == 0) + { + return 1; + } + i = (i % MOD + MOD) % MOD; + Int64 result = pow_mod(i, k / 2); + result *= result; + result %= MOD; + if (k % 2 == 1) + { + result *= i; + } + result %= MOD; + return (int)result; +} + +void fill_S(int n) +{ + memset(S, 0, sizeof(S)); + for (int k = 0; k < MAXK; k++) + for (int i = 1; i <= n; i++) + { + S[i][k] = S[i - 1][k] + (Seq[i - 1] * pow_mod(i, k)) % MOD; + S[i][k] %= MOD; + } +} + +void fill_C(int k) +{ + C[0] = 1; + for (int i = 1; i <= k; i++) + { + C[i] = C[i - 1] * (k - i + 1) / i; + } + for (int i = 0; i <= k; i++) + { + C[i] %= MOD; + } +} + +//ÔòF(k,a,b) = C(k,0) * (S(b, k) - S(a-1, k)) + C(k,1) * (1-a)(S(b,k-1) - S(a-1, k-1)) +// + ... + C(k,x) * (1-a)^x * (S(b, k-1) - S(a-1, k-1)) +int F(int k, int a, int b) +{ + Int64 result = 0; + for (int i = 0; i <= k; i++) + { + Int64 temp = C[i] * pow_mod(1 - a, i) % MOD * (S[b][k - i] - S[a - 1][k - i]); + temp = (temp % MOD + MOD) % MOD; + result = (result + temp) % MOD; + } + return (int)(result); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", &Seq[i]); + } + fill_S(n); + scanf("%d", &q); + for (int i = 0; i < q; i++) + { + int k, a, b; + scanf("%d%d%d", &k, &a, &b); + fill_C(k); + printf("%d\n", F(k, a, b)); + } + } + return 0; +} +//Ranking Submission Run Time Language Submission Date +//65 8478569 2.808 C++ 2010-12-25 15:05:24 diff --git a/11447.cpp b/11447.cpp new file mode 100644 index 0000000..b283778 --- /dev/null +++ b/11447.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +/* + Accepted + */ + +#define MAXN 10001 +#define EPS 1e-9 + +struct point +{ + double x, y; +}; + +point p[MAXN]; + +int main() +{ + int T, n, w; + double capacity, initial, out, in; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + double x, y; + scanf("%lf %lf", &x, &y); + p[i].x = x; + p[i].y = y; + } + scanf("%d", &w); + capacity = 0.0; + for (int i = 0; i < n; ++i) + { + int j = (i + 1) % n; + capacity += p[i].x * p[j].y - p[j].x * p[i].y; + } + capacity *= (double)w / 2; + assert(capacity > 0.0); + scanf("%lf %lf %lf", &initial, &out, &in); + initial = (capacity * initial) / 100.0;//Convert from percentage to volume + /* + printf("Capacity es: %lf\n", capacity); + printf("Initial es: %lf\n", initial); + */ + initial -= out; + if (initial < 0.0 - EPS) + { + printf("Lack of water. "); + initial = 0.0; + } + initial += in; + if (initial > capacity + EPS) + { + printf("Excess of water. "); + initial = capacity; + } + initial = (initial * 100.0) / capacity;//Convert from volume to percentage + int answer = initial; + printf("Final percentage: %d\%\n", answer); + } + return 0; +} diff --git a/11448.cpp b/11448.cpp new file mode 100644 index 0000000..74fe41e --- /dev/null +++ b/11448.cpp @@ -0,0 +1,361 @@ +#include + +using namespace std; + +/* bignum.c + Implementation of large integer arithmetic: addition, subtraction, + multiplication, and division. + + begun: February 7, 2002 + by: Steven Skiena +*/ + +/* +Copyright 2003 by Steven S. Skiena; all rights reserved. + +Permission is granted for use in non-commerical applications +provided this copyright notice remains intact and unchanged. + +This program appears in my book: + +"Programming Challenges: The Programming Contest Training Manual" +by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. + +See our website www.programming-challenges.com for additional information. + +This book can be ordered from Amazon.com at + +http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ + +*/ + +#define MAXDIGITS 10000 /* maximum length bignum */ + +#define PLUS 1 /* positive sign bit */ +#define MINUS -1 /* negative sign bit */ + +typedef struct +{ + char digits[MAXDIGITS]; /* represent the number */ + int signbit; /* 1 if positive, -1 if negative */ + int lastdigit; /* index of high-order digit */ +} bignum; + +void print_bignum(bignum *); +void string_to_bignum(const string &, bignum *); +void int_to_bignum(int, bignum *); +void initialize_bignum(bignum *); +int max(int, int); +void add_bignum(bignum *, bignum *, bignum *); +void subtract_bignum(bignum *, bignum *, bignum *); +int compare_bignum(bignum *, bignum *); +void zero_justify(bignum *); +void digit_shift(bignum *, int); +void multiply_bignum(bignum *, bignum *, bignum *); +void divide_bignum(bignum *, bignum *, bignum *); + +void print_bignum(bignum *n) +{ + int i; + if (n->signbit == MINUS) + { + printf("-"); + } + for (i = n->lastdigit; i >= 0; i--) + { + printf("%c", '0' + n->digits[i]); + } + printf("\n"); +} + +void string_to_bignum(const string &s, bignum *n) +{ + n->signbit = PLUS;// TODO: currently no negative numbers + for (int i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + for (int i = s.size() - 1; i >= 0; i--) + { + n->lastdigit++; + n->digits[n->lastdigit] = s[i] - 48; + } + if (s == "0") + { + n->lastdigit = 0; + } +} + +void int_to_bignum(int s, bignum *n) +{ + int i; /* counter */ + int t; /* int to work with */ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + for (i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + t = abs(s); + while (t > 0) + { + n->lastdigit++; + n->digits[n->lastdigit] = (t % 10); + t = t / 10; + } + if (s == 0) + { + n->lastdigit = 0; + } +} + +void initialize_bignum(bignum *n) +{ + int_to_bignum(0, n); +} + +int max(int a, int b) +{ + if (a > b) + { + return (a); + } + else + { + return (b); + } +} + +/* c = a +-/* b; */ + +void add_bignum(bignum *a, bignum *b, bignum *c) +{ + int carry; /* carry digit */ + int i; /* counter */ + initialize_bignum(c); + if (a->signbit == b->signbit) + { + c->signbit = a->signbit; + } + else + { + if (a->signbit == MINUS) + { + a->signbit = PLUS; + subtract_bignum(b, a, c); + a->signbit = MINUS; + } + else + { + b->signbit = PLUS; + subtract_bignum(a, b, c); + b->signbit = MINUS; + } + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit) + 1; + carry = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + c->digits[i] = (char)(carry + a->digits[i] + b->digits[i]) % 10; + carry = (carry + a->digits[i] + b->digits[i]) / 10; + } + zero_justify(c); +} + +void subtract_bignum(bignum *a, bignum *b, bignum *c) +{ + int borrow; /* has anything been borrowed? */ + int v; /* placeholder digit */ + int i; /* counter */ + initialize_bignum(c); + if ((a->signbit == MINUS) || (b->signbit == MINUS)) + { + b->signbit = -1 * b->signbit; + add_bignum(a, b, c); + b->signbit = -1 * b->signbit; + return; + } + if (compare_bignum(a, b) == PLUS) + { + subtract_bignum(b, a, c); + c->signbit = MINUS; + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit); + borrow = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + v = (a->digits[i] - borrow - b->digits[i]); + if (a->digits[i] > 0) + { + borrow = 0; + } + if (v < 0) + { + v = v + 10; + borrow = 1; + } + c->digits[i] = (char)v % 10; + } + zero_justify(c); +} + +int compare_bignum(bignum *a, bignum *b) +{ + int i; /* counter */ + if ((a->signbit == MINUS) && (b->signbit == PLUS)) + { + return (PLUS); + } + if ((a->signbit == PLUS) && (b->signbit == MINUS)) + { + return (MINUS); + } + if (b->lastdigit > a->lastdigit) + { + return (PLUS * a->signbit); + } + if (a->lastdigit > b->lastdigit) + { + return (MINUS * a->signbit); + } + for (i = a->lastdigit; i >= 0; i--) + { + if (a->digits[i] > b->digits[i]) + { + return (MINUS * a->signbit); + } + if (b->digits[i] > a->digits[i]) + { + return (PLUS * a->signbit); + } + } + return (0); +} + +void zero_justify(bignum *n) +{ + while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0)) + { + n->lastdigit--; + } + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + n->signbit = PLUS; /* hack to avoid -0 */ + } +} + +void digit_shift(bignum *n, int d) /* multiply n by 10^d */ +{ + int i; /* counter */ + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + return; + } + for (i = n->lastdigit; i >= 0; i--) + { + n->digits[i + d] = n->digits[i]; + } + for (i = 0; i < d; i++) + { + n->digits[i] = 0; + } + n->lastdigit = n->lastdigit + d; +} + +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int i, j; /* counters */ + initialize_bignum(c); + row = *a; + for (i = 0; i <= b->lastdigit; i++) + { + for (j = 1; j <= b->digits[i]; j++) + { + add_bignum(c, &row, &tmp); + *c = tmp; + } + digit_shift(&row, 1); + } + c->signbit = a->signbit * b->signbit; + zero_justify(c); +} + +void divide_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int asign, bsign; /* temporary signs */ + int i, j; /* counters */ + initialize_bignum(c); + c->signbit = a->signbit * b->signbit; + asign = a->signbit; + bsign = b->signbit; + a->signbit = PLUS; + b->signbit = PLUS; + initialize_bignum(&row); + initialize_bignum(&tmp); + c->lastdigit = a->lastdigit; + for (i = a->lastdigit; i >= 0; i--) + { + digit_shift(&row, 1); + row.digits[0] = a->digits[i]; + c->digits[i] = 0; + while (compare_bignum(&row, b) != PLUS) + { + c->digits[i]++; + subtract_bignum(&row, b, &tmp); + row = tmp; + } + } + zero_justify(c); + a->signbit = asign; + b->signbit = bsign; +} + +int main2(void) +{ + int n; + bignum *f = new bignum[5001]; + int_to_bignum(0, &f[0]); + int_to_bignum(1, &f[1]); + for (int i = 2; i < 5001; i++) + { + add_bignum(&f[i - 2], &f[i - 1], &f[i]); + } + while (cin >> n) + { + cout << "The Fibonacci number for " << n << " is "; + print_bignum(&f[n]); + } + delete f; +} + +int main() +{ + int t; + string input; + bignum a, b, result; + cin >> t; + while (t--) + { + cin >> input; + string_to_bignum(input, &a); + cin >> input; + string_to_bignum(input, &b); + subtract_bignum(&a, &b, &result); + print_bignum(&result); + } + return 0; +} diff --git a/11450.cpp b/11450.cpp index 3076a3f..e81d4bf 100644 --- a/11450.cpp +++ b/11450.cpp @@ -1,77 +1,46 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - scanf("%d", &T); - while( T-- ){ - int M, C; - scanf("%d%d", &M, &C); - vector< vector > Vec(C); - int i, m, c; - FOI(i, 0, C-1){ - int K, temp; - scanf("%d", &K); - while( K-- ){ - scanf("%d", &temp); - Vec[i].push_back(temp); - } - } - - int dp[M + 1][C + 1]; - memset(dp, -1, sizeof(dp)); - FOI(m, 0, M){ - dp[m][0] = 0; - FOI(c, 1, C){ - FOI(i, 0, Vec[c - 1].size()-1){ - int ci = Vec[c - 1][i]; - if( m >= ci && dp[m - ci][c - 1] != -1 ) - dp[m][c] = max(dp[m][c], dp[m - ci][c - 1] + ci); - } - } - } - if( dp[M][C] == -1 ) - printf("no solution\n"); - else - printf("%d\n", dp[M][C]); - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +int main() +{ + int T, M, C, K, i, j, k; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &M, &C); + int DP[201] = {}, W[201] = {}, A[20]; + for (i = 0; i < C; i++) + { + scanf("%d", &K); + for (j = 0; j < K; j++) + scanf("%d", &A[j]); + for (j = M; j >= 0; j--) + { + int max = 0; + for (k = 0; k < K; k++) + { + if (j - A[k] >= 0) + { + if (max <= DP[j - A[k]] + A[k] && W[j - A[k]] == i) + max = DP[j - A[k]] + A[k]; + } + } + if (max) + W[j]++; + DP[j] = DP[j] > max ? DP[j] : max; + } + } + int max = 0; + for (i = 0; i <= M; i++) + { + if (W[i] == C) + max = max > DP[i] ? max : DP[i]; + } + if (max == 0) + puts("no solution"); + else + printf("%d\n", max); + } + return 0; +} diff --git a/11451.cpp b/11451.cpp new file mode 100644 index 0000000..be300be --- /dev/null +++ b/11451.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +// Recursive, Dp, bitmask + +#define INF 2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int l, s, sPos[10 + 2], c, maxFlow[10 + 2], bitMaskFilled[10 + 2][10 + 2], maxFilled; + +int minimum(int p, int q) +{ + if (p < q) + return p; + return q; +} + +void recur(int at, int cLeft, int filled) +{ + if (at == s || !cLeft) + { + int cnt = 0; + for (int i = 0; i <= l; i++) + if (filled & (1 << i)) + cnt++; + if (cnt > maxFilled) + maxFilled = cnt; + return; + } + for (int i = 0; i <= minimum(maxFlow[at], cLeft); i++) + recur(at + 1, cLeft - i, filled | bitMaskFilled[at][i]); +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + scanf("%d %d", &l, &s); + for (int i = 0; i < s; i++) + scanf("%d", &sPos[i]); + scanf("%d", &c); + for (int i = 0; i < s; i++) + scanf("%d", &maxFlow[i]); + Set(bitMaskFilled, 0); + int tmp; + for (int i = 0; i < s; i++) + { + for (int j = 1; j <= maxFlow[i]; j++) + { + tmp = bitMaskFilled[i][j - 1]; + tmp |= (1 << sPos[i]); + tmp |= (1 << (sPos[i] + j)); + tmp |= (1 << (sPos[i] - j)); + bitMaskFilled[i][j] = tmp; + } + } + maxFilled = 0; + recur(0, c, 0); + printf("%d\n", maxFilled); + } + return 0; +} diff --git a/11452.cpp b/11452.cpp new file mode 100644 index 0000000..2c7bc15 --- /dev/null +++ b/11452.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +bool same(const string &s, int sz) +{ + for (int i = 0; i < sz; i++) + { + if (s[i] != s[sz + i]) + { + return false; + } + } + for (int i = 2 * sz; i < s.size(); i++) + { + if (s[i - 2 * sz] != s[i]) + { + return false; + } + } + return true; +} + +int main() +{ + int t; + string input; + cin >> t; + while (t--) + { + cin >> input; + int sz = input.size() / 2; + do + { + if (same(input, sz)) + { + break; + } + } while (sz--); + int cur = input.size() - 2 * sz; + for (int i = 0; i < 8; i++) + { + cout << input[cur++]; + if (cur == sz) + { + cur = 0; + } + } + cout << "..." << endl; + } + return 0; +} diff --git a/11455.cpp b/11455.cpp index 4c5cf82..c3a7c6d 100644 --- a/11455.cpp +++ b/11455.cpp @@ -1,37 +1,35 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - int test; - cin>>test; - while(test--){ - int side[4],i; - double peri=0; - for(i=0;i<4;i++){ - cin>>side[i]; - peri+=side[i]; - } - peri/=2; - sort(side,side+4); - if(side[0]==side[1] && side[1]==side[2] && side[2]==side[3] && side[3]==side[1]){ - cout<<"square\n"; - continue; - } - if(side[0]==side[1] && side[2]==side[3]){ - cout<<"rectangle\n"; - continue; - } - if(side[0] + +using namespace std; + +int main() +{ + int cases; + int sides[4]; + cin >> cases; + while (cases--) + { + for (int i = 0; i < 4; i++) + { + cin >> sides[i]; + } + sort(sides, sides + 4); + if (sides[0] == sides[1] && sides[1] == sides[2] && sides[2] == sides[3]) + { + cout << "square" << endl; + } + else if (sides[0] == sides[1] && sides[2] == sides[3]) + { + cout << "rectangle" << endl; + } + else if (sides[0] + sides[1] + sides[2] > sides[3]) + { + cout << "quadrangle" << endl; + } + else + { + cout << "banana" << endl; + } + } + return 0; +} diff --git a/11456.cpp b/11456.cpp new file mode 100644 index 0000000..f895bf0 --- /dev/null +++ b/11456.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +typedef unsigned long long ul; + +int main() +{ + int n; + int cases; + ul cars[2001]; + ul lis[2001]; + ul lds[2001]; + cin >> cases; + while (cases--) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> cars[n - i - 1]; + } + fill(lis, lis + n, 1); + fill(lds, lds + n, 1); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < i; j++) + { + if (cars[i] > cars[j]) + { + lis[i] = max(lis[i], lis[j] + 1); + } + if (cars[i] < cars[j]) + { + lds[i] = max(lds[i], lds[j] + 1); + } + } + } + ul best = 0; + for (int i = 0; i < n; i++) + { + //best = max(best, lis[i] + lds[n-i-1] - 1); + best = max(best, lis[i] + lds[i] - 1); + } + cout << best << endl; + } + return 0; +} diff --git a/11458.cpp b/11458.cpp new file mode 100644 index 0000000..78d3a7c --- /dev/null +++ b/11458.cpp @@ -0,0 +1,224 @@ +#include + +using namespace std; + +#define deb 0 + +typedef string board; + +#define B(b, i, j) b[((i)*4) + (j)] + +typedef struct +{ + board b; + int moves; +} mboard; + +int bad(board b) +{ + int j; + for (int i = 0; i < 3; i++) + for (j = 0; j < 4; j++) + { + if (B(b, i, j) == 'R' || B(b, i, j) == 'B') + if (B(b, i + 1, j) == 'R' || B(b, i + 1, j) == 'B') + { + return 1; + } + } + for (int i = 0; i < 4; i++) + for (j = 0; j < 3; j++) + { + if (B(b, i, j) == 'R' || B(b, i, j) == 'B') + if (B(b, i, j + 1) == 'R' || B(b, i, j + 1) == 'B') + { + return 1; + } + } + return 0; +} + +queue q; +set seen; + +void print(board b) +{ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + printf("%c", B(b, i, j)); + } + printf("\n"); + } + printf("\n"); +} + +void attempt(board b, int moves) +{ + if (seen.count(b) > 0) + { + return; + } + if (deb) + { + puts("not seen"); + } + seen.insert(b); + int s = seen.size(); + if (deb) + if ((s % 1000) == 0) + { + printf("seen: %u q: %u\n", (unsigned int)seen.size(), (unsigned int)q.size()); + } + if (deb) + { + print(b); + } + if (bad(b)) + { + return; + } + if (deb) + { + puts("not bad"); + } + mboard m; + m.b = b; + m.moves = moves; + q.push(m); + if (deb) + { + puts("pushed"); + } +} + +int badd(int i, int di) +{ + if (i + di > 3) + { + return 1; + } + if (i + di < 0) + { + return 1; + } + return 0; +} +void move(board b, int i, int j, int di, int dj, int moves) +{ + if (badd(i, di * 2)) + { + return; + } + if (badd(j, dj * 2)) + { + return; + } + if (B(b, i, j) == '.') + { + return; + } + if (B(b, i + di, j + dj) == '.') + { + return; + } + if (B(b, i + di + di, j + dj + dj) != '.') + { + return; + } + B(b, i + di + di, j + dj + dj) = B(b, i, j); + B(b, i, j) = '.'; + attempt(b, moves + 1); +} +void move2(board b, int i, int j, int di, int dj, int moves) +{ + if (badd(i, di * 3)) + { + return; + } + if (badd(j, dj * 3)) + { + return; + } + if (B(b, i, j) == '.') + { + return; + } + if (B(b, i + di, j + dj) == '.') + { + return; + } + if (B(b, i + di + di, j + dj + dj) == '.') + { + return; + } + if (B(b, i + di + di + di, j + dj + dj + dj) != '.') + { + return; + } + B(b, i + di + di + di, j + dj + dj + dj) = B(b, i, j); + B(b, i, j) = '.'; + attempt(b, moves + 1); +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cc = 0; cc < T; ++cc) + { + int i, j; + int pos = 0; + int mb = 0; + char line[100]; + q = queue(); + seen.clear(); + board b = "................"; + for (i = 0; i < 4; i++) + { + scanf("%s", line); + // fgets(line, 100, stdin); + for (j = 0; j < 4; j++) + { + B(b, i, j) = line[j]; + } + } + attempt(b, 0); + while (q.size() > 0) + { + pos++; + mboard m = q.front(); + q.pop(); + board b = m.b; + if (deb) + if (m.moves > mb) + { + mb = m.moves; + printf("moves: %u\n", mb); + } + if (B(b, 0, 0) == 'R') + { + printf("%u\n", m.moves); + break; + } + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) + { + move(b, i, j, 0, 1, m.moves); + move(b, i, j, 0, -1, m.moves); + move(b, i, j, 1, 0, m.moves); + move(b, i, j, -1, 0, m.moves); + move2(b, i, j, 0, 1, m.moves); + move2(b, i, j, 0, -1, m.moves); + move2(b, i, j, 1, 0, m.moves); + move2(b, i, j, -1, 0, m.moves); + } + } + if (deb) + { + printf("done %u\n", pos); + } + } + return 0; +} diff --git a/11459.cpp b/11459.cpp index 189a854..4432e2c 100644 --- a/11459.cpp +++ b/11459.cpp @@ -1,32 +1,60 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int z=1;z<=t;z++){ - int a,b,c; - cin>>a>>b>>c; - int player[1000],board[101][2],die[1000]; - for(int i=0;i<=100;i++) - for(int j=0;j<2;j++) - board[i][j]=i; - for(int i=1;<=b;i++){ - int x,y; - cin>>x>>y; - board[x][0]=x; - board[x][1]=y; - } - for(int i=1;i<=c;i++){ - cin>>die[i]; - } - int ctr=1; - for(int i=1;i<=c;i++){ - player[ctr]+=die[i]; - if(board[player[ctr]]) - if(ctr%a==0) - ctr=1; - - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int t, n, k, c, start, end, roll; + int *players = new int[1000001]; + map ladder; + cin >> t; + while (t--) + { + cin >> n >> k >> c; + ladder.clear(); + fill(players, players + n + 1, 1); + while (k--) + { + cin >> start >> end; + ladder[start] = end; + } + bool game_finished = false; + int current_player = 0; + for (int i = 0; i < c; i++) + { + cin >> roll; + if (game_finished) + { + continue; + } + players[current_player] += roll; + if (players[current_player] >= 100) + { + players[current_player] = 100; + game_finished = true; + continue; + } + if (ladder.find(players[current_player]) != ladder.end()) + { + players[current_player] = ladder[players[current_player]]; + if (players[current_player] >= 100) + { + players[current_player] = 100; + game_finished = true; + continue; + } + } + current_player++; + if (current_player >= n) + { + current_player = 0; + } + } + for (int i = 0; i < n; i++) + { + cout << "Position of player " << i + 1 << " is " << players[i] << "." << endl; + } + } + delete[] players; + return 0; +} diff --git a/1146.cpp b/1146.cpp new file mode 100644 index 0000000..40d4ca4 --- /dev/null +++ b/1146.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +//sevenkplus bless me + +const double pi = 4.0 * atan(1.0); +typedef signed long long LL; +#define clr(x) memset(x, 0, sizeof(x)) +#define clro(x) memset(x, -1, sizeof(x)) +typedef pair pii; +const int inf = 999999999; +#define sf scanf +#define pf printf +const int maxn = 4444; + +int n; +int A[maxn][2]; +int L, R; + +struct TwoSAT +{ + bool mark[maxn << 1]; + vector G[maxn << 1]; + int c, S[maxn << 1]; + void clear() + { + clr(mark); + for (int i = 0; i < maxn * 2; ++i) + { + G[i].clear(); + } + } + void add(int x, int xval, int y, int yval) + { + x = x * 2 + xval; + y = y * 2 + yval; + G[x ^ 1].push_back(y); + G[y ^ 1].push_back(x); + } + bool dfs(int x) + { + if (mark[x ^ 1]) + { + return false; + } + if (mark[x]) + { + return true; + } + mark[x] = true; + S[c++] = x; + for (int i = 0; i < G[x].size(); ++i) + if (!dfs(G[x][i])) + { + return false; + } + return true; + } + bool solve() + { + for (int i = 0; i < 2 * n; i += 2) + if (!mark[i] && !mark[i + 1]) + { + c = 0; + if (!dfs(i)) + { + while (c > 0) + { + mark[S[--c]] = false; + } + if (!dfs(i + 1)) + { + return false; + } + } + } + return true; + } +} sol; + +void input() +{ + for (int i = 0; i < n; ++i) + for (int a = 0; a < 2; ++a) + { + sf("%d", &A[i][a]); + R = max(R, A[i][a]); + } +} +bool test(int dis) +{ + sol.clear(); + for (int i = 0; i < n; ++i) + for (int a = 0; a < 2; ++a) + for (int j = i + 1; j < n; ++j) + for (int b = 0; b < 2; ++b) + if (abs(A[i][a] - A[j][b]) < dis) + { + sol.add(i, a ^ 1, j, b ^ 1); + } + return sol.solve(); +} +void solve() +{ + while (L < R) + { + int mid = L + (R - L + 1) / 2; + if (test(mid)) + { + L = mid; + } + else + { + R = mid - 1; + } + } + pf("%d\n", L); +} +int main() +{ + while (sf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + L = R = 0; + input(); + solve(); + } + return 0; +} diff --git a/11460.cpp b/11460.cpp new file mode 100644 index 0000000..2650150 --- /dev/null +++ b/11460.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +void add_tri(double x1, double y1, double x2, double y2, + double *tot_ar, double *wx) +{ + double ar = x1 * y2 - x2 * y1; + *tot_ar += ar; + *wx += ar * (x1 + x2) / 3.0; +} +typedef vector VI; +int main() +{ + int cc; + cin >> cc; + while (cc--) + { + int n; + cin >> n; + VI x(n), y(n); + for (int i = 0; i < n; i++) + { + cin >> x[i] >> y[i]; + } + x.push_back(x[0]); + y.push_back(y[0]); + double tar = 0.0, tx = 0.0, bar = 0.0, bx = 0.0; + for (int i = 0; i < n; i++) + { + if (y[i] >= 0 && y[i + 1] >= 0) + { + add_tri(x[i], y[i], x[i + 1], y[i + 1], &tar, &tx); + } + else if (y[i] <= 0 && y[i + 1] <= 0) + { + add_tri(x[i], y[i], x[i + 1], y[i + 1], &bar, &bx); + } + else + { + double mx = y[i] * (double)(x[i + 1] - x[i]) / (y[i] - y[i + 1]) + x[i]; + add_tri(x[i], y[i], mx, 0.0, y[i] > 0 ? &tar : &bar, + y[i] > 0 ? &tx : &bx); + add_tri(mx, 0.0, x[i + 1], y[i + 1], y[i + 1] > 0 ? &tar : &bar, + y[i + 1] > 0 ? &tx : &bx); + } + } + tx /= tar; + bx /= bar; + if (fabs(tx - bx) < 1e-6) + { + printf("Balanced.\n"); + } + else if (tx < bx) + { + printf("CE is aft of CLR by %.02lf units.\n", bx - tx); + } + else + { + printf("CE is forward of CLR by %.02lf units.\n", tx - bx); + } + } + return 0; +} diff --git a/11461.cpp b/11461.cpp index 5405b25..22730e4 100644 --- a/11461.cpp +++ b/11461.cpp @@ -1,20 +1,23 @@ -#include -#include -using namespace std; - -int main(){ - for(;;){ - long long a,b; - cin>>a>>b; - if(a==0 && b==0) - break; - double sqra=sqrt(a); - long long na=(long long)sqra; - if(sqra==(long long)sqra) - na-=1; - double sqrb=sqrt(b); - long long nb=(long long)sqrb; - cout< + +using namespace std; + +int main() +{ + int a, b; + while (scanf("%d %d", &a, &b) == 2) + { + if (!a) + { + break; + } + int counter = 0; + int i = ceil(sqrt(a)); + while (i * i <= b) + { + counter++, i++; + } + printf("%d\n", counter); + } + return 0; +} diff --git a/11462.cpp b/11462.cpp index 88e271e..9989c83 100644 --- a/11462.cpp +++ b/11462.cpp @@ -1,29 +1,30 @@ -#include -using namespace std; - -int main(){ - int n=0; - for(;;){ - cin>>n; - if(n==0) - break; - int age[200]={0}; - int j=0,ctr=0; - for(int i=0;i>j; - age[j]++; - ctr++; - - } - for(int i=0;i<=100;i++){ - for(int k=0;k0) - cout<<" "; - } - } - cout< + +using namespace std; + +int p[2000000]; + +int main() +{ + string line; + while (getline(cin, line)) + { + int numPeople = atoi(line.c_str()); + if (numPeople == 0) + { + break; + } + getline(cin, line); + istringstream sin(line); + int i = 0; + while (sin >> p[i++]) + ; + sort(p, p + numPeople); + for (int i = 0; i < numPeople - 1; ++i) + { + printf("%d ", p[i]); + } + printf("%d\n", p[numPeople - 1]); + } + return 0; +} diff --git a/11463.cpp b/11463.cpp index 15e5a5b..42c4ff5 100644 --- a/11463.cpp +++ b/11463.cpp @@ -1,67 +1,53 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - for(int t=1; t<=test; t++){ - int N, line; - int i, j, k; - cin>>N>>line; - int mat[N][N]; - for(i=0; i>s>>d; - mat[s][d]=1; - mat[d][s]=1; - } - for(k=0; k>S>>D; - int tot=0; - for(i=0; i + +using namespace std; + +int INF = 1000000000; + +int main() +{ + int t, n, r; + int a, b; + int dist[101][101]; + cin >> t; + for (int c = 0; c < t; c++) + { + cin >> n >> r; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + dist[i][j] = INF; + } + for (int i = 0; i < r; i++) + { + cin >> a >> b; + dist[a][b] = 1; + dist[b][a] = 1; + } + for (int i = 0; i < n; i++) + { + dist[i][i] = 0; + } + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + dist[i][j] = min(dist[i][j], dist[k][j] + dist[i][k]); + } + } + } + cin >> a >> b; + int result = 0; + for (int i = 0; i < n; i++) + { + if (dist[a][i] + dist[i][b] > result) + { + result = dist[a][i] + dist[i][b]; + } + } + cout << "Case " << c + 1 << ": " << result << endl; + } + return 0; +} diff --git a/11464.cpp b/11464.cpp new file mode 100644 index 0000000..29e2a15 --- /dev/null +++ b/11464.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define D(x) cout << #x " is " << x << endl + +bool p[15][15]; +bool backup[15][15]; +bool can_change[15][15]; +int n; + +#define inside(i, j) (0 <= (i) && (i) < n && 0 <= (j) && (j) < n) + +void attack(int i, int j) +{ + if (can_change[i][j]) + { + if (inside(i - 1, j)) + { + p[i - 1][j] = !p[i - 1][j]; + } + if (inside(i + 1, j)) + { + p[i + 1][j] = !p[i + 1][j]; + } + if (inside(i, j - 1)) + { + p[i][j - 1] = !p[i][j - 1]; + } + if (inside(i, j + 1)) + { + p[i][j + 1] = !p[i][j + 1]; + } + } +} + +int main() +{ + int cases; + scanf("%d", &cases); + for (int C = 1; C <= cases; C++) + { + if (scanf("%d", &n) != 1) + { + return 0; + } + memset(p, 0, sizeof p); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + int x; + scanf("%d", &x); + can_change[i][j] = true; + if (x) + { + attack(i, j); + } + can_change[i][j] = !x; + } + } + memcpy(backup, p, sizeof p); + int ans = INT_MAX; + //Brute-force the subset of cells to change in the first row + for (int subset = 0; subset < (1 << n); ++subset) + { + memcpy(p, backup, sizeof backup); + int cnt = 0; + for (int j = 0; j < n; ++j) + { + if (subset & (1 << j)) + { + attack(0, j); + cnt++; + } + } + //Now, deduce the moves for next rows + for (int i = 1; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + if (p[i - 1][j]) + { + attack(i, j); + cnt++; + } + } + } + //Let's see if this solved it. + bool ok = true; + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + ok &= !p[i][j]; + } + if (ok) + { + ans = min(ans, cnt); + } + } + printf("Case %d: %d\n", C, (ans < INT_MAX ? ans : -1)); + } + return 0; +} diff --git a/11465.cpp b/11465.cpp new file mode 100644 index 0000000..6abc709 --- /dev/null +++ b/11465.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +typedef long long LL; +const int N = 1 << 6; +LL a[N], C[N][N]; + +LL dfs(const LL &remain, int last, int cnt) +{ + if (cnt == 0) + { + return remain < 0; + } + if (remain >= a[last] * cnt) + { + return 0; + } + if (remain < a[0] * cnt) + { + return C[last + 1][cnt]; + } + LL sum = 0; + for (int i = last; i >= cnt - 1; --i) + { + sum += dfs(remain - a[i], i - 1, cnt - 1); + } + //cout << remain << ' ' << last << ' ' << cnt << ' ' << sum << endl; + return sum; +} + +LL work(int n, int k) +{ + LL sum = 0; + for (int i = k - 1; i < n; ++i) + { + sum += dfs(a[i], i - 1, k - 1); + //cout << i << ' ' << sum << endl; + } + return sum; +} + +int run() +{ + C[0][0] = 1; + for (int i = 1; i < N; ++i) + { + C[i][0] = 1; + for (int j = 1; j <= i; ++j) + { + C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; + } + } + int _; + cin >> _; + for (int cas = 1; cas <= _; ++cas) + { + int n, k; + cin >> n >> k; + for (int i = 0; i < n; ++i) + { + cin >> a[i]; + } + sort(a, a + n); + cout << "Case " << cas << ": " << work(n, k) << endl; + } +} + +int main() +{ + ios::sync_with_stdio(0); + run(); + return 0; +} diff --git a/11466.cpp b/11466.cpp index 77575fd..f9f0b86 100644 --- a/11466.cpp +++ b/11466.cpp @@ -1,92 +1,79 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) -#define MAX 10000050 - -bool isPrime[MAX + 1]; -vector Prime; -int S = 0; - -void seive(){ - memset(isPrime, true, sizeof isPrime); - Prime.clear(); - isPrime[0] = false; isPrime[1] = false; - int i, j; - FOI(i, 2, MAX){ - if( isPrime[i] ){ - Prime.push_back(i); - for(j = 2*i; j <= MAX; j += i) - isPrime[j] = false; - } - } - S = Prime.size() - 1; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - while( true ){ - int64 N; - scanf("%lld", &N); - if( N == 0 ) - break; - N = N < 0 ? -N : N; - int64 cnt = 0, big = 2; - int i; - FOI(i, 0, S){ - if( Prime[i] > N ) break; - if( N % Prime[i] == 0 ){ - big = max(big, Prime[i]); - ++cnt; - while( N % Prime[i] == 0 ){ - N /= Prime[i]; - } - } - } - if( N > Prime[S] || isPrime[N] ){ - ++cnt; - big = max(N, big); - } - if( cnt < 2 ) - printf("-1\n"); - else - printf("%lld\n", big); - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +int MAX_N = 10000000; + +int main() +{ + int *lp = new int[MAX_N]; + vector primes; + long long input; + fill(lp, lp + MAX_N, 0); + for (int i = 2; i < MAX_N; i++) + { + if (lp[i] == 0) + { + lp[i] = i; + primes.push_back(i); + } + for (int j = 0; j < primes.size() && primes[j] <= lp[i] && i * primes[j] < MAX_N; j++) + { + lp[i * primes[j]] = primes[j]; + } + } + while (cin >> input) + { + if (input == 0) + { + break; + } + bool negative = false; + bool found = false; + long long result = 0; + int divisor_count = 0; + if (input < 0) + { + input = -input; + } + for (int i = 0, sz = primes.size(); i < sz; i++) + { + if (primes[i] > input) + { + break; + } + else if (input == primes[i]) + { + break; + } + else if (input % primes[i] == 0) + { + found = true; + result = primes[i]; + divisor_count++; + do + { + input /= primes[i]; + } while (input % primes[i] == 0); + } + } + if (!found) + { + cout << -1 << endl; + } + else if (divisor_count < 2 && input == 1) + { + cout << -1 << endl; + } + else if (input > result) + { + cout << input << endl; + } + else + { + cout << result << endl; + } + } + delete[] lp; + return 0; +} diff --git a/11468.cpp b/11468.cpp new file mode 100644 index 0000000..bc43cb4 --- /dev/null +++ b/11468.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +const double eps = 1e-8; +const double offset = 2000; +const double PI = acos(-1.0); +const int MAXN = 505; +const int MAXM = 62; +const int INF = 0x3f3f3f3f; +const int MOD = 1000000009; + +int nxt[MAXN][MAXM], cs; +int fail[MAXN], cnt[MAXN]; +char str[MAXN]; +double d[105][MAXN]; +int id[MAXN]; +double p[MAXN]; +int K, N, L; + +void initNode(int id) +{ + cnt[id] = 0, memset(nxt[id], 0, sizeof(nxt[id])); +} + +void clear() +{ + initNode(0); + cs = 1; +} + +int getIndex(char ch) +{ + if (ch >= 'a' && ch <= 'z') + { + return ch - 'a'; + } + if (ch >= 'A' && ch <= 'Z') + { + return ch - 'A' + 26; + } + return ch - '0' + 52; +} + +void insert(char *s) +{ + int p = 0; + for (; *s; ++s) + { + int c = getIndex(*s); + if (!nxt[p][c]) + { + initNode(cs); + nxt[p][c] = cs++; + } + p = nxt[p][c]; + } + cnt[p] = 1; +} + +void buildAc() +{ + queue Q; + Q.push(0); + fail[0] = 0; + while (!Q.empty()) + { + int p = Q.front(); + Q.pop(); + for (int i = 0; i < MAXM; ++i) + { + if (nxt[p][i]) + { + if (!p) + { + fail[nxt[p][i]] = 0; + } + else + { + fail[nxt[p][i]] = nxt[fail[p]][i]; + } + Q.push(nxt[p][i]); + } + else + { + if (!p) + { + nxt[p][i] = 0; + } + else + { + nxt[p][i] = nxt[fail[p]][i]; + } + } + cnt[nxt[p][i]] |= cnt[fail[nxt[p][i]]]; + } + } +} + +void solved(int cas) +{ + printf("Case #%d: ", cas); + clear(); + scanf("%d", &K); + while (K--) + { + scanf("%s", str); + insert(str); + } + buildAc(); + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%s %lf", str, &p[i]); + id[i] = getIndex(str[0]); + } + scanf("%d", &L); + memset(d, 0, sizeof(d)); + d[0][0] = 1; + for (int i = 0; i < L; i++) + { + for (int j = 0; j < cs; j++) + { + if (cnt[j]) + { + continue; + } + for (int k = 0; k < N; k++) + { + d[i + 1][nxt[j][id[k]]] += d[i][j] * p[k]; + } + } + } + double ans = 0; + for (int i = 0; i < cs; i++) + if (!cnt[i]) + { + ans += d[L][i]; + } + printf("%.6lf", ans); + puts(""); +} + +int main() +{ + int T = 1; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + solved(i); + } + return 0; +} diff --git a/11470.cpp b/11470.cpp index 20a9823..fb535ef 100644 --- a/11470.cpp +++ b/11470.cpp @@ -1,37 +1,32 @@ -#include -using namespace std; - -int main(){ - for(int z=1;;z++){ - int n; - cin>>n; - if(n==0) - break; - int mat[10][10]={0}; - for(int i=0;i>mat[i][j]; - } - } - int nl=(n%2==0)?(n/2):(n+1)/2; - int sum[10]={0}; - for(int k=0;k<=nl;k++){ - for(int i=0;i + +using namespace std; + +int main() +{ + int n, tmp, casenum = 0; + while (cin >> n) + { + if (!n) + { + break; + } + casenum++; + int sums[10] = {0}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> tmp; + sums[min(min(i, j), min(n - i - 1, n - j - 1))] += tmp; + } + } + cout << "Case " << casenum << ":"; + for (int i = 0, sz = ceil(n / 2.0); i < sz; i++) + { + cout << " " << sums[i]; + } + cout << endl; + } + return 0; +} diff --git a/11471.cpp b/11471.cpp new file mode 100644 index 0000000..aeb9b23 --- /dev/null +++ b/11471.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define TOP 0 +#define RIGHT 1 +#define DOWN 2 +#define LEFT 3 + +char str[12][5]; +int ans, tt2, tnum, lst[12], cnt[12], hash_[128]; + +int comp(const void *p, const void *q) +{ + return strcmp((char *)p, (char *)q); +} + +void dfs(int dep) +{ + int i, top, left; + if (dep == 12) + { + ans++; + return; + } + top = (dep >= 3 ? str[lst[dep - 3]][DOWN] : tt2); + left = (dep % 3 ? str[lst[dep - 1]][RIGHT] : tt2); + for (i = 0; i < tnum; i++) + { + if (cnt[i] && (top & str[i][TOP]) && (left & str[i][LEFT])) + { + cnt[i]--; + lst[dep] = i; + dfs(dep + 1); + cnt[i]++; + } + } +} + +int main() +{ + int cas, count, i, j; + cas = 0; + hash_['R'] = 1; + hash_['G'] = 2; + hash_['B'] = 3; + tt2 = (1 << 4) - 1; + scanf("%d", &count); + while (count--) + { + for (i = 0; i < 12; i++) + { + scanf("%s", str[i]); + for (j = 0; j < 4; j++) + { + str[i][j] = (1 << hash_[str[i][j]]); + } + } + qsort(str, 12, sizeof(str[0]), comp); + for (i = 0, tnum = 0; i < 12;) + { + for (j = i + 1; j < 12; j++) + { + if (strcmp(str[i], str[j])) + { + break; + } + } + if (i != tnum) + { + strcpy(str[tnum], str[i]); + } + cnt[tnum] = j - i; + tnum++; + i = j; + } + ans = 0; + dfs(0); + if (ans) + { + for (i = 0; i < tnum; i++) + { + for (j = 2; j <= cnt[i]; j++) + { + ans *= j; + } + } + } + printf("Case %d: %d\n", ++cas, ans); + } + return 0; +} diff --git a/11472.cpp b/11472.cpp new file mode 100644 index 0000000..a600720 --- /dev/null +++ b/11472.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int max_, base; +unsigned int dp[1 << 10][11][102]; + +unsigned int solve(int last, int i, int state) +{ + if (dp[state][last][i] != -1) + { + return dp[state][last][i]; + } + int res = ((state == (1 << base) - 1) && i >= base); + if (i < max_) + { + int a = last + 1; + int b = last - 1; + if (a < base) + { + res = (res + (solve(a, i + 1, state | (1 << a)) % 1000000007)) % 1000000007; + } + if (b >= 0) + { + res = (res + (solve(b, i + 1, state | (1 << b)) % 1000000007)) % 1000000007; + } + } + return dp[state][last][i] = res; +} +int main() +{ + int tc = 0, i, j, k, l; + scanf("%d", &tc); + while (tc--) + { + scanf("%d %d", &base, &max_); + l = 1 << base; + for (i = 0; i < l; i++) + for (j = 0; j <= base; j++) + for (k = 0; k <= max_ + 1; k++) + { + dp[i][j][k] = -1; + } + unsigned int res = 0; + for (i = 1; i < base; i++) + { + res = (res + ((solve(i, 1, 1 << i)) % 1000000007)) % 1000000007; + } + printf("%u\n", res); + } + return 0; +} diff --git a/11473.cpp b/11473.cpp new file mode 100644 index 0000000..a5c5351 --- /dev/null +++ b/11473.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; +const double eps = 1e-10; + +void output(P in) +{ + printf("%.2lf %.2lf\n", in.real(), in.imag()); +} + +void solve(vector

&in, double ori) +{ + int i = 0; + double req = ori; + output(in[0]); + while (i < (int)in.size() - 1) + { + P e = in[i + 1] - in[i]; + double dist = abs(in[i + 1] - in[i]); + e /= dist; + if (dist + eps > req) + { + output(in[i] + e * req); + P inv = -in[i + 1] + in[i]; + inv /= abs(inv); + double rem = dist - req; + in[i] = in[i + 1] + inv * rem; + req = ori; + } + else + { + req -= dist; + i++; + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + rep(t, T) + { + printf("Road #%d:\n", t + 1); + int n, k; + scanf("%d%d", &n,&k); + vector

in(n); + rep(i, n) + { + double re, im; + scanf("%lf%lf", &re,&im); + in[i] = P(re,im); + } + double all = 0.0f; + REP(i, 1, n) + { + all += abs(in[i] - in[i - 1]); + } + double req = all / (k - 1); + solve(in, req); + printf("\n"); + } + return 0; +} diff --git a/11474.cpp b/11474.cpp new file mode 100644 index 0000000..5876cb5 --- /dev/null +++ b/11474.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +int qq[105], tdx[105], tdy[105], pnum[105], ppx[105][15], ppy[105][15]; +int used[105]; + +int get_dis(int x, int y) +{ + return x * x + y * y; +} + +int main() +{ + int count, n, m, k, d, i, j, ii, jj, kk, pp; + scanf("%d", &count); + while (count--) + { + scanf("%d%d%d%d", &n, &m, &k, &d); + d *= d; + k *= k; + for (i = 0; i < m; i++) + { + scanf("%d%d", &tdx[i], &tdy[i]); + } + for (i = 0; i < n; i++) + { + scanf("%d", &pnum[i]); + for (j = 0; j < pnum[i]; j++) + { + scanf("%d%d", &ppx[i][j], &ppy[i][j]); + } + } + memset(used, 0, sizeof(used)); + used[0] = 1; + qq[0] = 0; + for (i = 0, j = 1; i < j; i++) + { + pp = qq[i]; + for (ii = 0; ii < pnum[pp]; ii++) + { + for (jj = 0; jj < m; jj++) + { + if (get_dis(ppx[pp][ii] - tdx[jj], ppy[pp][ii] - tdy[jj]) <= d) + { + break; + } + } + if (jj < m) + { + break; + } + } + if (ii < pnum[pp]) + { + break; + } + for (ii = 0; ii < n; ii++) + { + if (used[ii] == 0) + { + for (jj = 0; jj < pnum[pp]; jj++) + { + for (kk = 0; kk < pnum[ii]; kk++) + { + if (get_dis(ppx[pp][jj] - ppx[ii][kk], ppy[pp][jj] - ppy[ii][kk]) <= k) + { + break; + } + } + if (kk < pnum[ii]) + { + break; + } + } + if (jj < pnum[pp]) + { + used[ii] = 1; + qq[j++] = ii; + } + } + } + } + if (i < j) + { + printf("Tree can be saved :)\n"); + } + else + { + printf("Tree can't be saved :(\n"); + } + } + return 0; +} diff --git a/11475.cpp b/11475.cpp index ec8e94a..5fba99a 100644 --- a/11475.cpp +++ b/11475.cpp @@ -1,20 +1,80 @@ -#include -using namespace std; - -int main(){ - for(;;){ - string str; - cin>>str; - if(str=="\0") - break; - int l=str.length(); - for(int i=0;i + +using namespace std; + +#define N 100001 + +char arr[2 * N + 1]; +int len; +long M1 = 2147483647, B1 = 1000000007, right_[N], left_[N], modPow[N]; + +long mod(long a, long b) +{ + return (a % b + b) % b; +} + +long calcHash_L(int l, int r) +{ + long res = left_[l]; + if (r < len - 1) + { + res = mod(res - (left_[r + 1] * modPow[r - l + 1]) % M1, M1); + } + return res; +} + +long calcHash_R(int l, int r) +{ + long res = right_[r]; + if (l > 0) + { + res = mod(res - (right_[l - 1] * modPow[r - l + 1]) % M1, M1); + } + return res; +} + +int isPalindrome(int l, int r) +{ + int len = l - r + 1; + if (len == 1) + { + return 1; + } + return calcHash_R(l, r) == calcHash_L(l, r); +} + +int main() +{ + int i, j, tc; + modPow[0] = 1; + for (i = 1; i < N; i++) + { + modPow[i] = (B1 * modPow[i - 1]) % M1; + } + while (gets(arr)) + { + len = strlen(arr); + right_[0] = arr[0]; + left_[len - 1] = arr[len - 1]; + for (i = 1; i < len; i++) + { + right_[i] = (right_[i - 1] * B1 + arr[i]) % M1; + } + for (i = len - 2; i >= 0; i--) + { + left_[i] = (left_[i + 1] * B1 + arr[i]) % M1; + } + for (i = 0; i < len; i++) + { + if (isPalindrome(i, len - 1)) + { + for (j = i - 1; j >= 0; j--) + arr[len++] = arr[j]; + arr[len] = 0; + break; + } + } + puts(arr); + } + return 0; +} diff --git a/11476.cpp b/11476.cpp new file mode 100644 index 0000000..cd46fea --- /dev/null +++ b/11476.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +#define mabs(a) (((a) > 0) ? (a) : -(a)) +#define LL long long +#define SIZE 500001 +LL pr[80600]; +bool p[SIZE]; +int gene(int n) +{ + int i, j; + for (i = 3; i * i <= n; i += 2) + if (!p[i / 2]) + for (j = i * i; j < n + 1; j += (2 * i)) + { + p[j / 2] = 1; + } + j = 1; + pr[0] = 2; + for (i = 1; i < n / 2; i++) + if (!p[i]) + { + pr[j] = i * 2 + 1, j++; + } + return j; +} +typedef pair pli; +vector vv; +LL binsearch(LL n) +{ + LL low = 1, high = 1000000000, mid; + while (low <= high) + { + mid = (low + high) / 2; + if (mid * mid == n) + { + return mid; + } + else if (mid * mid < n) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return -1; +} +void print(LL a, int b) +{ + if (b == 1) + { + printf("%lld", a); + } + else + { + printf("%lld^%d", a, b); + } +} +LL mulmod(LL a, LL b, LL c) +{ + LL x = 0, y = a % c; + while (b > 0) + { + if (b % 2 == 1) + { + x = (x + y) % c; + } + y = (y * 2) % c; + b /= 2; + } + return x % c; +} +LL gcd(LL a, LL b) +{ + if (!b) + { + return a; + } + return gcd(b, a % b); +} +LL pollard_rho(LL n) +{ + int i = 0, k = 2; + LL x = 3, y = 3; + while (1) + { + i++; + x = (mulmod(x, x, n) + n - 1) % n; + LL d = gcd(mabs(y - x), n); + if (d != 1 && d != n) + { + // printf("->>%d\n",i); + return d; + } + if (i == k) + { + y = x, k *= 2; + } + } +} +LL powmod(LL a, LL b, LL c)//(a^b)%c +{ + LL temp; + if (!b) + { + return 1; + } + if (b == 1) + { + return a % c; + } + if (b % 2) + { + temp = powmod(a, b - 1, c); + return mulmod(temp, a, c); + } + temp = powmod(a, b / 2, c); + return mulmod(temp, temp, c); +} +bool miller_rabin(LL P) +{ + if (P != 2 && P % 2 == 0) + { + return false; + } + LL s = P - 1; + while (s % 2 == 0) + { + s /= 2; + } + for (int i = 0; i < 25; i++) + { + LL a = rand() % (P - 1) + 1, temp = s; + LL mod = powmod(a, temp, P); + while (temp != (P - 1) && mod != 1 && mod != (P - 1)) + { + mod = mulmod(mod, mod, P); + temp *= 2; + } + if (mod != (P - 1) && temp % 2 == 0) + { + return false; + } + } + return true; +} +int main() +{ + int t, i, m = gene(2 * SIZE); + LL n, temp, now; + LL lim = 1000000; + lim *= lim; + scanf("%d", &t); + while (t--) + { + scanf("%lld", &n); + vv.clear(); + temp = n; + for (i = 0; i < m && pr[i] * pr[i] <= n; i++) + if (n % pr[i] == 0) + { + int f = 0; + while (n % pr[i] == 0) + { + n /= pr[i], f++; + } + vv.push_back(make_pair((LL)pr[i], f)); + } + if (n < lim && n > 1) + { + vv.push_back(make_pair(n, 1)), n /= n; + } + if (n == 1) + { + goto done; + } + now = binsearch(n); + if (now != -1) + { + vv.push_back(make_pair(now, 2)); + goto done; + } + if (miller_rabin(n)) + { + vv.push_back(make_pair(n, 1)); + goto done; + } + now = pollard_rho(n); + if (now > n / now) + { + now = n / now; + } + vv.push_back(make_pair(now, 1)); + vv.push_back(make_pair(n / now, 1)); + done: + printf("%lld = ", temp); + print(vv[0].first, vv[0].second); + for (i = 1; i < (int)vv.size(); i++) + { + printf(" * "), print(vv[i].first, vv[i].second); + } + printf("\n"); + } + return 0; +} diff --git a/11478.cpp b/11478.cpp new file mode 100644 index 0000000..bacd258 --- /dev/null +++ b/11478.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int adj[505], ex[3005], ey[3005], ec[3005]; + +int main() +{ + int n, m, i, j, p, q, c, f, ans; + while (scanf("%d%d", &n, &m) == 2) + { + memset(adj, 0, sizeof(adj)); + for (i = 0; i < m; i++) + { + scanf("%d%d%d", &ex[i], &ey[i], &ec[i]); + } + for (p = 1, q = 10005, ans = -1; p <= q;) + { + c = ((p + q) >> 1); + for (i = 0; i < n; i++) + { + for (j = 0, f = 0; j < m; j++) + { + if (ec[j] + adj[ex[j]] - adj[ey[j]] < c) + { + adj[ex[j]] += c - (ec[j] + adj[ex[j]] - adj[ey[j]]); + f = 1; + } + } + if (f == 0) + { + break; + } + } + for (j = 0, f = 0; j < m; j++) + { + if (ec[j] + adj[ex[j]] - adj[ey[j]] < c) + { + adj[ex[j]] += c - (ec[j] + adj[ex[j]] - adj[ey[j]]); + f = 1; + } + } + if (f) + { + q = c - 1; + } + else + { + ans = c; + p = c + 1; + } + } + if (ans > 10000) + { + printf("Infinite\n"); + } + else if (ans == -1) + { + printf("No Solution\n"); + } + else + { + printf("%d\n", ans); + } + } + return 0; +} diff --git a/11479.cpp b/11479.cpp new file mode 100644 index 0000000..c53b6d6 --- /dev/null +++ b/11479.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + int t; + long long a, b, c; + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> a >> b >> c; + if (a > b) + { + swap(a, b); + } + if (b > c) + { + swap(b, c); + } + cout << "Case " << i + 1 << ": "; + if (c >= a + b) + { + cout << "Invalid" << endl; + } + else if (a == b && b == c) + { + cout << "Equilateral" << endl; + } + else if (a == b || b == c || a == c) + { + cout << "Isosceles" << endl; + } + else + { + cout << "Scalene" << endl; + } + } + return 0; +} diff --git a/1148.cpp b/1148.cpp new file mode 100644 index 0000000..1d2900d --- /dev/null +++ b/1148.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define MOD 1000000007 +#define INF 1000000000 +typedef long long ll; +typedef unsigned long long ull; +typedef pair pii; +#define N 100100 + +int dis[N]; +int q[N]; +int adj[N][102]; +int n; + +int main() +{ + int src, dst, ntest; + int L, R, node; + + cin >> ntest; + for (int cas = 1; cas <= ntest; cas++) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", &node); + scanf("%d", &adj[node][0]); + for (int j = 1; j <= adj[node][0]; j++) + { + scanf("%d", &adj[node][j]); + } + dis[i] = -1; + } + + scanf("%d%d", &src, &dst); + dis[src] = 0; + L = R = 0; + q[R++] = src; + while (L < R) + { + // cout << L << " " << R << endl; + node = q[L++]; + for (int j = 1; j <= adj[node][0]; j++) + { + if (dis[adj[node][j]] == -1) + { + dis[adj[node][j]] = dis[node] + 1; + q[R++] = adj[node][j]; + } + } + } + cout << src << " " << dst << " " << dis[dst] - 1 << endl; + + if (cas != ntest) + cout << endl; + } + + return 0; +} diff --git a/11480.cpp b/11480.cpp new file mode 100644 index 0000000..0962919 --- /dev/null +++ b/11480.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int n, tmp; + int counter = 0; + long long result = 0; + while (cin >> n) + { + if (n == 0) + { + break; + } + result = 0; + counter++; + for (int i = 1; i < n / 3; i++) + { + tmp = (n - i) / 2; + if ((n - i) % 2 == 0) + { + tmp--; + } + result += tmp - i; + } + cout << "Case " << counter << ": " << result << endl; + } + return 0; +} diff --git a/11481.cpp b/11481.cpp new file mode 100644 index 0000000..fe7ad68 --- /dev/null +++ b/11481.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define F 1000000007 +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define NN 1010 + +typedef long long LL; + +struct Triple +{ + LL d, x, y; + Triple() {} + Triple(LL q, LL w, LL e) : d(q), x(w), y(e) {} +}; +Triple egcd(LL a, LL b) +{ + if (!b) + { + return Triple(a, 1, 0); + } + Triple q = egcd(b, a % b); + return Triple(q.d, q.y, q.x - a / b * q.y); +} +LL invmod(LL a, LL n) +{ + Triple t = egcd(a, n); + if (t.d > 1) + { + return 0; + } + LL r = t.x % n; + return (r < 0 ? r + n : r); +} +long long im[NN], fact[NN], c[NN][NN]; +LL C(LL a, LL b) +{ + int LM = MIN(b, a - b), LIM; + if (c[a][LM]) + { + return c[a][LM]; + } + c[a][0] = 1; + LIM = (a + 3) >> 1; + for (int U = a, D = 1; D < LIM; ++D, --U) + { + c[a][D] = (((c[a][D - 1] * U) % F) * im[D]) % F; + } + return c[a][LM]; +} + +int main() +{ + int K, M, N, T, X; + fact[0] = fact[1] = im[1] = 1; + memset(c, 0, sizeof(c)); + for (int i = 2; i < NN; ++i) + { + im[i] = invmod(i, F); + fact[i] = (i * fact[i - 1]) % F; + } + scanf("%d", &T); + for (int cnum = 0; cnum < T;) + { + scanf("%d %d %d", &N, &M, &K); + long long ans = C(M, K), aux = 0; + X = M - K; + for (int a = 0, k = N - K; a <= X; ++a, --k) + { + if (a & 1) + { + aux = aux - (C(X, a) * fact[k]) % F; + } + else + { + aux = aux + (C(X, a) * fact[k]) % F; + } + } + while (aux < 0) + { + aux += F; + } + aux %= F; + printf("Case %d: %lld\n", ++cnum, (ans * aux) % F); + } + return 0; +} diff --git a/11482.cpp b/11482.cpp new file mode 100644 index 0000000..fbebc48 --- /dev/null +++ b/11482.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +char spac[10005], udl[10005]; + +int main() +{ + int cas, i, j, k, n, m, st; + cas = 0; + for (i = 0; i < 10005; i++) + { + spac[i] = ' '; + udl[i] = '_'; + } + udl[10004] = 0; + spac[10004] = 0; + while (scanf("%d%d", &m, &n) == 2) + { + if (n == 0 && m == 0) + { + break; + } + printf("Triangular Museum %d\n", ++cas); + for (i = 0, st = n * m; i < n; i++, st -= m) + { + for (j = 1; j <= m; j++) + { + printf("%.*s", st - j, spac); + for (k = 0; k <= i; k++) + { + printf("/%.*s\\", (j - 1) << 1, j == m ? udl : spac); + if (k != i) + { + printf("%.*s", (m << 1) - ((j) << 1), spac); + } + } + printf("\n"); + } + } + printf("\n"); + } + return 0; +} diff --git a/11483.cpp b/11483.cpp index 819582d..6ac4196 100644 --- a/11483.cpp +++ b/11483.cpp @@ -1,35 +1,31 @@ -#include -using namespace std; - -int main(){ - int i=0; - for(i=1;;i++){ - int n; - cin>>n; - if(n==0) - break; - for(int j=0;j"<"< + +using namespace std; + +int main() +{ + int n, t = 0; + char str[1002]; + while (scanf("%d", &n) == 1 && n) + { + getchar(); + printf("Case %d:\n", ++t); + printf("#include\n#include\nint main()\n{\n"); + while (n--) + { + gets(str); + printf("printf(\""); + for (int i = 0; str[i]; i++) + { + if (str[i] == '\\' || str[i] == '\"') + { + printf("\\"); + } + printf("%c", str[i]); + } + printf("\\n\");\n"); + } + printf("printf(\"\\n\");\nreturn 0;\n}\n"); + } + return 0; +} diff --git a/11484.cpp b/11484.cpp new file mode 100644 index 0000000..9a23eb5 --- /dev/null +++ b/11484.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int n, ptt; +char buf[1000005], str[1000005]; + +typedef struct data +{ + char s[105]; + struct data *left, *right, *parent, *pres; +} data; + +data pool[2005]; + +data *build(data *parent, data *pres) +{ + int i; + data *ptr; + if (n == 0) + { + return NULL; + } + gets(str); + n--; + if (str[1] == '/') + { + return NULL; + } + ptr = pool + ptt++; + ptr->parent = parent; + ptr->pres = pres; + ptr->left = ptr->right = NULL; + for (i = 10; str[i] && str[i] != '\''; i++) + { + ptr->s[i - 10] = str[i]; + } + ptr->s[i - 10] = 0; + ptr->left = build(ptr, NULL); + ptr->right = build(parent, ptr); + return ptr; +} + +int main() +{ + int cas, i; + data *ptr; + cas = 0; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + gets(buf); + ptt = 0; + ptr = build(NULL, NULL); + printf("Case %d:\n", ++cas); + scanf("%d", &n); + gets(buf); + for (i = 0; i < n; i++) + { + gets(buf); + if (buf[1] == 'i') + { + if (ptr->left) + { + ptr = ptr->left; + } + } + else if (buf[1] == 'e') + { + if (ptr->right) + { + ptr = ptr->right; + } + } + else if (buf[1] == 'r') + { + if (ptr->pres) + { + ptr = ptr->pres; + } + } + else if (buf[1] == 'a') + { + if (ptr->parent) + { + ptr = ptr->parent; + } + } + printf("%s\n", ptr->s); + } + } + return 0; +} diff --git a/11485.cpp b/11485.cpp new file mode 100644 index 0000000..4f12192 --- /dev/null +++ b/11485.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int ary[105]; +long long dp[105][95]; + +int main() +{ + int n, i, j, k; + double t; + long long ans; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%lf", &t); + ary[i] = (int)floor(t * 10 + 1e-8); + } + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (i = 1; i <= 8; i++) + { + for (j = 0; j < n; j++) + { + for (k = ary[j] % 10; k < 90; k++) + { + dp[i][k] += dp[i - 1][k - ary[j] % 10]; + } + } + } + for (i = 0, ans = 0; i < 90; i++) + { + ans += dp[8][i] * (i / 10); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/11486.cpp b/11486.cpp new file mode 100644 index 0000000..f1dc03c --- /dev/null +++ b/11486.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +typedef long long ll; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 36 + +const ll mod = 1000000007; + +ll ori[N][N] = {0}, a[N][N], b[N][N], c[N][N]; +int d[] = {1, -1}; + +void matmul(int n, bool flag) +{ + if (flag == true) + rep(i, n) rep(j, n) b[i][j] = a[i][j]; + else + rep(i, n) rep(j, n) b[i][j] = ori[i][j]; + rep(i, n) + { + rep(j, n) + { + c[i][j] = 0; + rep(k, n) + c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod; + } + } + rep(i, n) rep(j, n) a[i][j] = c[i][j]; +} + +void calc(int n, int p) +{ + if (p == 1) + { + matmul(n, false); + return; + } + else if (p == 0) + return; + calc(n, p / 2); + matmul(n, true); + if (p % 2 == 1) + matmul(n, false); +} + +void print(int val, int a) +{ + if (a == 0) + return; + print(val / 2, a - 1); + if (val % 2 == 1) + cout << 1 << " "; + else + cout << 0 << " "; +} + +void move4(int fromstate, int from, int tostate, int *ch, bool *data, map &M, int now) +{ + if (now == 4) + { + int to; + if (M.find(tostate) == M.end()) + M[tostate] = M.size(); + to = M[tostate]; + ori[from][to] = 1; + return; + } + rep(i, 2) + { + int nex = ch[now] + d[i]; + if (nex < 0 || nex >= 7 || data[nex] == true) + continue; + data[nex] = true; + move4(fromstate, from, (tostate | (1 << nex)), ch, data, M, now + 1); + data[nex] = false; + } +} + +void choose4(int last, int now, int state, map &M, int *ch) +{ + if (now == 4) + { + int from; + bool data[7] = {false}; + if (M.find(state) == M.end()) + M[state] = M.size(); + from = M[state]; + move4(state, from, 0, ch, data, M, 0); + return; + } + REP(i, last, 7) + { + ch[now] = i; + choose4(i + 1, now + 1, (state | (1 << i)), M, ch); + } +} + +int main() +{ + int ch[4]; + map M; + choose4(0, 0, 0, M, ch); + int te; + cin >> te; + while (te--) + { + int p; + cin >> p; + int state = 0; + int mul = 0; + for (int i = 6; i >= 0; i--) + { + int tmp; + cin >> tmp; + if (tmp != 0) + state |= (1 << i); + } + rep(i, N) rep(j, N) a[i][j] = 0; + mul = M[state]; + rep(i, N) a[i][i] = 1; + calc(N, p - 1); + ll ans = 0; + rep(i, N) ans = (ans + a[mul][i]) % mod; + cout << ans << endl; + } + return 0; +} diff --git a/11487.cpp b/11487.cpp new file mode 100644 index 0000000..a48a65d --- /dev/null +++ b/11487.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +const int mod = 20437; +const int INF = 120437; +char m[10][10]; + +pair bfs(int now, int sx, int sy, int n, int c, int p) +{ + int table[n][n], path[n][n]; + bool visited[n][n]; + rep(i, n) rep(j, n) + { + if (m[i][j] == '#' || m[i][j] - 'A' > now + 1) + { + visited[i][j] = true; + } + else + { + visited[i][j] = false; + } + if (i != sy || j != sx) + { + table[i][j] = INF, path[i][j] = 0; + } + } + queue> Q; + Q.push(make_pair(sx, sy)); + visited[sy][sx] = true; + path[sy][sx] = p; + table[sy][sx] = c; + while (!Q.empty()) + { + int x = Q.front().first, y = Q.front().second; + Q.pop(); + if (m[y][x] - 'A' == now + 1) + { + return make_pair(table[y][x], path[y][x]); + } + rep(i, 4) + { + int nex = x + dx[i], ney = y + dy[i]; + if (nex < 0 || ney < 0 || nex >= n || ney >= n) + { + continue; + } + if (visited[ney][nex] == true && + table[y][x] + 1 != table[ney][nex]) + { + continue; + } + if (visited[ney][nex] == false) + { + Q.push(make_pair(nex, ney)); + } + visited[ney][nex] = true; + table[ney][nex] = table[y][x] + 1; + path[ney][nex] = (path[ney][nex] + path[y][x]) % mod; + } + } + return make_pair(-1, -1); +} + +void solve(int size, int n, int *x, int *y) +{ + pair res = make_pair(0, 1); + rep(i, size) + { + res = bfs(i, x[i], y[i], n, res.first, res.second); + if (res.first == -1) + { + printf(" Impossible\n"); + return; + } + } + printf(" %d %d\n", res.first, res.second); + return; +} + +main() +{ + int n; + int tc = 1; + while (cin >> n && n) + { + int x[27], y[27], size = 0; + rep(i, n) cin >> m[i]; + rep(i, n) + { + rep(j, n) + { + if (isalpha(m[i][j])) + { + size = max(size, m[i][j] - 'A'); + x[m[i][j] - 'A'] = j; + y[m[i][j] - 'A'] = i; + } + } + } + printf("Case %d:", tc++); + solve(size, n, x, y); + } + return false; +} diff --git a/11488.cpp b/11488.cpp new file mode 100644 index 0000000..abff448 --- /dev/null +++ b/11488.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +/* + Time-limit exceeded. + + Problem: 11488 - Hyper prefix sets + Author: Andrés Mejía-Posada + */ + +int dp[50001][201]; +string s[50001]; + +#define V(x, t, d) \ + copy(x.begin(), x.end(), ostream_iterator(cout, d)); \ + cout << endl + +int main() +{ + int C; + cin >> C; + while (C--) + { + int n; + cin >> n; + int maxLen = -1; + for (int i = 0; i < n; ++i) + { + cin >> s[i], maxLen = max(maxLen, (int)s[i].length()); + } + sort(s, s + n); + //V(s, string, "\n"); + //for (int i=0; i dp[i - 1][j - 1]) + { + dp[i][j] += dp[i - 1][j]; + } + ans = max(ans, (j + 1) * dp[i][j]); + } + } + // for (int i=0; i + +using namespace std; + +// http://uva.onlinejudge.org/external/114/11489.html + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + char num[1000 + 10]; + scanf("%s", num); + int freq[10 + 3]; + memset(freq, 0, sizeof(freq)); + int sum = 0; + for (int i = 0; num[i]; i++) + { + sum += num[i] - '0'; + freq[num[i] - '0']++; + } + int allowed_move = freq[3] + freq[6] + freq[9]; + printf("Case %d: ", ++cases); + bool s_win = true; + if (sum % 3 == 0) + { + if (allowed_move % 2 == 0) + { + s_win = false; + } + } + else if (sum % 3 == 1) + { + if (freq[1] || freq[4] || freq[7]) + { + if (allowed_move % 2) + { + s_win = false; + } + } + else + { + s_win = false; + } + } + else + { + if (freq[2] || freq[5] || freq[8]) + { + if (allowed_move % 2) + { + s_win = false; + } + } + else + { + s_win = false; + } + } + if (s_win) + { + printf("S\n"); + } + else + { + printf("T\n"); + } + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/1149.cpp b/1149.cpp new file mode 100644 index 0000000..8a03015 --- /dev/null +++ b/1149.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int n, l, L[100010]; + +int main() +{ + int i, j, count, T; + cin >> T; + int a = 0; + while (T--) + { + a++; + scanf("%d", &n); + scanf("%d", &l); + for (i = 0; i < n; i++) + { + scanf("%d", &L[i]); + } + sort(L, L + n); + count = 0; + int j = 0; + for (i = n - 1; i >= 0, i >= j; i--) + { + if (L[i] != -1) + { + count++; + int num = l - L[i]; + L[i] = -1; + if (L[j] != -1 && L[j] <= num) + { + L[j++] = -1; + } + } + } + if (a != 1) + { + cout << endl; + } + cout << count << endl; + } + return 0; +} diff --git a/11490.cpp b/11490.cpp new file mode 100644 index 0000000..2462407 --- /dev/null +++ b/11490.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int main() +{ + int i, j; + long long n, t, res; + while (scanf("%lld", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 1, j = 0;; i++) + { + t = (long long)i * i * 6; + if (t >= n) + { + break; + } + res = (n - t); + if (res % (i * 7) == 0) + { + res /= (i * 7); + if (res >= 100000007) + { + res %= 100000007; + } + if (res > 0) + { + printf("Possible Missing Soldiers = %d\n", (int)(((res * res) << 1) % 100000007)); + j++; + } + } + } + if (j == 0) + { + printf("No Solution Possible\n"); + } + printf("\n"); + } + return 0; +} diff --git a/11491.cpp b/11491.cpp new file mode 100644 index 0000000..b1d078a --- /dev/null +++ b/11491.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +char line[100005]; +int digits, erase, curr, queue_[10][100000], head[10], tail[10], i, toshow, last; + +int main() +{ + while (scanf("%d %d\n", &digits, &erase) && digits) + { + gets(line); + for (i = 0; i < 10; i++) + { + head[i] = tail[i] = 0; + } + for (i = 0; i < digits; i++) + { + curr = line[i] - '0'; + queue_[curr][tail[curr]++] = i; + } + for (last = 0, toshow = digits - erase; toshow;) + { + for (i = 9; i > -1; i--) + { + while (head[i] != tail[i] && queue_[i][head[i]] < last) + { + head[i]++; + } + if (head[i] != tail[i] && queue_[i][head[i]] <= digits - toshow) + { + toshow--; + putchar(i + '0'); + last = queue_[i][head[i]++]; + break; + } + } + } + putchar('\n'); + } + return 0; +} diff --git a/11492.cpp b/11492.cpp new file mode 100644 index 0000000..7b03d83 --- /dev/null +++ b/11492.cpp @@ -0,0 +1,232 @@ +#include + +using namespace std; + +#define INF 1 << 25 + +map m; +struct NODE +{ + vector adj; + vector w; + vector next; + vector ccon; + bool has[27]; +} nodes[4000]; +int len, n; +char l1[64], l2[64], word[64]; +bool dbg = 0; +void clear(int k) +{ + nodes[k].adj.clear(); + nodes[k].w.clear(); + nodes[k].next.clear(); + nodes[k].ccon.clear(); + memset(nodes[k].has, 0, 27); +} +int check(string x) +{ + map::iterator it; + if ((it = m.find(x)) == m.end()) + { + m[x] = n; + clear(n); + return n++; + } + return (*it).second; +} +void add_edge(int x, int y) +{ + nodes[x].adj.push_back(y); + nodes[x].w.push_back(len); + nodes[x].ccon.push_back(word[0] - 'a'); + nodes[x].has[word[0] - 'a'] = 1; +} +void check(int *x) +{ + ; + if (nodes[(*x)].has[word[0] - 'a']) + { + clear(n); + nodes[(*x)].next.push_back(n); + nodes[(*x)].adj.push_back(n); + nodes[(*x)].w.push_back(0); + nodes[(*x)].ccon.push_back(-1); + nodes[n].adj.push_back((*x)); + nodes[n].w.push_back(0); + nodes[n].ccon.push_back(-1); + (*x) = n++; + } +} + +/* Heap Binario {{{ */ +#define MAX_ELEM 1 << 12 + +#define pai(i) (((i)-1) / 2) +#define esq(i) (2 * (i) + 1) +#define dir(i) (2 * (i) + 2) + +struct heapitem +{ + int u, d; + heapitem() + { + u = d = 0; + } + heapitem(int no, int dis) + { + u = no, d = dis; + } + bool operator>(const heapitem &i) const + { + return d < i.d; + } +} h[MAX_ELEM]; /* heap (de maximo) */ +int hn; /* numero de elementos */ + +void sobe(int i) +{ + if (i > 0 && h[i] > h[pai(i)]) /* MUDEI! */ + { + heapitem t = h[pai(i)]; + h[pai(i)] = h[i]; + h[i] = t; + sobe(pai(i)); + } +} + +void desce(int i) +{ + int filho; + if (dir(i) < hn && h[dir(i)] > h[esq(i)]) + { + filho = dir(i); + } + else + { + filho = esq(i); + } + if (filho < hn && h[filho] > h[i]) + { + heapitem t = h[filho]; + h[filho] = h[i]; + h[i] = t; + desce(filho); + } +} + +heapitem pop() +{ + heapitem r = h[0]; + h[0] = h[--hn]; + desce(0); + return r; +} + +void push(heapitem v) +{ + h[hn] = v; + sobe(hn++); +} +/* }}} */ + +int dijkstra(int s, int t) +{ + if (dbg) + { + printf("De %d quero ir ate %d\n", s, t); + } + int in[n], d[n]; + char r[n]; + int i, u; + for (i = 0; i < n; i++) + { + d[i] = INF, in[i] = 0, r[i] = -1; + } + vector::iterator ADJ, W; + vector::iterator CC; + NODE C = nodes[s]; + ADJ = C.adj.begin(); + W = C.w.begin(); + hn = 0; /* limpa heap */ + CC = C.ccon.begin(); + for (; ADJ != C.adj.end(); ADJ++, W++, CC++) + { + d[(*ADJ)] = (*W), r[(*ADJ)] = (*CC); + push(heapitem(*ADJ, *W)); + } + d[s] = 0; + push(heapitem(s, 0)); + while (!in[t]) + { + if (hn == 0) + { + break; + } + heapitem hi = pop(); + int best = hi.d; + u = hi.u; + if (best == INF) + { + break; + } + in[u] = 1; + C = nodes[u]; + ADJ = C.adj.begin(); + CC = C.ccon.begin(); + W = C.w.begin(); + for (; ADJ != C.adj.end(); ADJ++, W++, CC++) + if (!in[(*ADJ)] && r[u] != (*CC) && d[(*ADJ)] > d[u] + (*W)) + { + d[(*ADJ)] = d[u] + (*W), + r[(*ADJ)] = (((*CC) == -1) ? (r[u]) : (*CC)); + push(heapitem(*ADJ, d[*ADJ])); + } + } + return ((in[t]) ? (d[t]) : -1); +} +int main() +{ + int a, b, E, i; + char O[64], D[64]; + string SL1, SL2; + while (scanf("%d", &E) && E) + { + scanf("%s %s", O, D); + m.clear(); + for (n = i = 0; i < E; i++) + { + scanf("%s %s %s\n", l1, l2, word); + SL1 = string(l1); + SL2 = string(l2); + len = strlen(word); + a = check(SL1); + b = check(SL2); + check(&a); + check(&b); + add_edge(a, b); + add_edge(b, a); + } + if (m.find(string(O)) == m.end() || m.find(string(D)) == m.end()) + { + printf("impossivel\n"); + continue; + } + int dist = dijkstra(m[string(O)], m[string(D)]); + if (dist > 0) + { + printf("%d\n", dist); + continue; + } + dist = dijkstra(m[string(D)], m[string(O)]); + if (dist > 0) + { + printf("%d\n", dist); + } + else + { + printf("impossivel\n"); + } + } + return 0; +} diff --git a/11493.cpp b/11493.cpp new file mode 100644 index 0000000..8313f82 --- /dev/null +++ b/11493.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAX(a, b) (a < b ? b : a) +#define oo 1 << 30 + +int width, height, plank_width, n_planks, max_plank_length, current, cnt[2][10001], currIdx, full, planks_used, test_planks, i, needed; + +void tryit(int a, int b, bool ean) +{ + currIdx = b - 1; + full = test_planks = 0; + needed = (a * 100) / plank_width; + while (cnt[ean][b] && full != needed) + { + cnt[ean][b]--; + test_planks++; + full++; + } + while (full != needed) + { + while (currIdx && (!cnt[ean][currIdx] || !cnt[ean][b - currIdx])) + { + currIdx--; + } + if (currIdx == 0) + { + break; + } + if (currIdx == b - currIdx && cnt[ean][currIdx] < 2) + { + currIdx--; + continue; + } + test_planks += 2; + cnt[ean][currIdx]--; + cnt[ean][b - currIdx]--; + full++; + } + if (full == needed && test_planks < planks_used) + { + planks_used = test_planks; + } +} + +int main() +{ + while (scanf("%d%d", &width, &height) && width) + { + max_plank_length = MAX(width, height) + 1; + planks_used = oo; + for (i = 0; i < max_plank_length; i++) + { + cnt[0][i] = cnt[1][i] = 0; + } + scanf("%d%d", &plank_width, &n_planks); + for (i = 0; i < n_planks; i++) + { + scanf("%d", ¤t); + cnt[0][current] = cnt[1][current] = cnt[0][current] + 1; + } + if ((100 * width) % plank_width == 0) + { + tryit(width, height, 0); + } + if ((100 * height) % plank_width == 0) + { + tryit(height, width, 1); + } + if (planks_used != oo) + { + printf("%d\n", planks_used); + } + else + { + printf("impossivel\n"); + } + } + return 0; +} diff --git a/11494.cpp b/11494.cpp index dbd4733..414d7d4 100644 --- a/11494.cpp +++ b/11494.cpp @@ -1,19 +1,65 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - for(;;){ - int i,j,u,v; - cin>>i>>j>>u>>v; - if(i==0 && j==0 && u==0 && v==0) - break; - cout << ( (i == u && j == v) ? 0 : (i == u || j == v || abs(i - u) == abs(j - v)) ? 1 : 2 ) << endl; - } - return 0; -} +#include + +using namespace std; + +bool check_position(int x1, int y1, int x2, int y2) +{ + if (y1 == y2 || x1 == x2) + { + return true; + } + for (int i = 0; x1 - i > 0 && y1 - i > 0; i++) + { + if (x1 - i == x2 && y1 - i == y2) + { + return true; + } + } + for (int i = 0; x1 + i < 9 && y1 + i < 9; i++) + { + if (x1 + i == x2 && y1 + i == y2) + { + return true; + } + } + for (int i = 0; x1 - i > 0 && y1 + i < 9; i++) + { + if (x1 - i == x2 && y1 + i == y2) + { + return true; + } + } + for (int i = 0; x1 + i < 9 && y1 - i > 0; i++) + { + if (x1 + i == x2 && y1 - i == y2) + { + return true; + } + } + return false; +} + +int main() +{ + int x1, y1, x2, y2; + while (cin >> x1 >> y1 >> x2 >> y2) + { + if (x1 == 0 && x2 == 0 && y1 == 0 && y2 == 0) + { + break; + } + if (x1 == x2 && y1 == y2) + { + cout << 0 << endl; + } + else if (check_position(x1, y1, x2, y2)) + { + cout << 1 << endl; + } + else + { + cout << 2 << endl; + } + } + return 0; +} diff --git a/11495.cpp b/11495.cpp index 1d1d66a..7ba79d5 100644 --- a/11495.cpp +++ b/11495.cpp @@ -1,55 +1,74 @@ -#include -#include - -int N; -#define MAXN 100002 - -int p[MAXN], Temp[MAXN]; -long long count; - -void Cal(int a, int b){ - int md, below, mdi, ni, i; - if (a >= b) return; - md = p[a+(b-a)/2]; - for (mdi=i=a;i md;mdi--){ - int tmp = Temp[mdi-1]; - Temp[mdi-1] = Temp[mdi]; - Temp[mdi] = tmp; - count++; - } - for (i=a;i + +using namespace std; + +unsigned long long result; + +template +void MergeSort(RandIter b, RandIter e, Less c) +{ + if (b >= e - 1) + { + return; + } + typedef typename iterator_traits::value_type Value; + RandIter i = b + (e - b) / 2; + MergeSort(b, i, c); + MergeSort(i, e, c); + vector L, R; + for (RandIter j = b; j != i; j++) + { + L.push_back(*j); + } + for (RandIter j = i; j != e; j++) + { + R.push_back(*j); + } + int sz = L.size(); + RandIter l = L.begin(), r = R.begin(); + for (RandIter j = b; j != e; j++) + { + if (l == L.end()) + { + *j = *r++; + } + else if (r == R.end()) + { + *j = *l++; + } + else + { + if (c(*l, *r)) + { + *j = *l++; + sz--; + } + else + { + *j = *r++; + result += sz; + } + } + } +} + +int main() +{ + int n, tmp; + vector nums(100001); + while (cin >> n) + { + if (n == 0) + { + break; + } + result = 0; + for (int i = 0; i < n; i++) + { + cin >> nums[i]; + } + MergeSort(nums.begin(), nums.begin() + n, less_equal()); + cout << (result % 2 ? "Marcelo" : "Carlos") << endl; + } + return 0; +} diff --git a/11496.cpp b/11496.cpp new file mode 100644 index 0000000..1ac38df --- /dev/null +++ b/11496.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int main() +{ + int n, first, last, current, sz; + bool increasing; + while (cin >> n) + { + if (n == 0) + { + break; + } + increasing = true; + vector points; + for (int i = 0; i < n; i++) + { + cin >> current; + if (i == 0) + { + first = current; + } + if (i == n - 1) + { + last = current; + } + if (points.size() < 2) + { + points.push_back(current); + continue; + } + sz = points.size(); + increasing = (points[sz - 1] > points[sz - 2]); + if (current > points[sz - 1] && increasing) + { + points[sz - 1] = current; + } + else if (current < points[sz - 1] && !increasing) + { + points[sz - 1] = current; + } + else if (current > points[sz - 1]) + { + increasing = true; + points.push_back(current); + } + else if (current < points[sz - 1]) + { + increasing = false; + points.push_back(current); + } + } + sz = points.size(); + int result = sz; + bool last_growing = (points[sz - 2] < points[sz - 1]); + bool first_growing = (points[1] > points[0]); + if (last_growing && points[0] > points[sz - 1]) + { + result--; + } + else if (first_growing && points[0] > points[sz - 1]) + { + result--; + } + else if (!last_growing && points[0] < points[sz - 1]) + { + result--; + } + else if (!first_growing && points[0] < points[sz - 1]) + { + result--; + } + cout << result << endl; + } + return 0; +} diff --git a/11497.cpp b/11497.cpp new file mode 100644 index 0000000..976a18f --- /dev/null +++ b/11497.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +int ans; +int tim[15]; +int used[2050000], dp[2050000]; +char s1[10000], s2[10000]; +int cnt[15]; + +int has(int stat, int bit) +{ + return stat % tim[bit + 1] >= tim[bit]; +} + +int recurs(int stat) +{ + int i, s, p, q; + if (used[stat]) + { + return dp[stat]; + } + used[stat] = 1; + dp[stat] = 0; + if (stat == 0) + { + return 0; + } + for (i = 0; i < 9; i++) + { + if (has(stat, i)) + { + s = stat; + p = i - i % 3 + (i % 3 + 1) % 3; + q = i - i % 3 + (i % 3 + 2) % 3; + if (has(stat, p) && has(stat, q)) + { + s -= tim[i] + tim[p] + tim[q]; + if (recurs(s) + 1 > dp[stat]) + { + dp[stat] = recurs(s) + 1; + } + } + s = stat; + p = i % 3 + (i / 3 + 1) % 3 * 3; + q = i % 3 + (i / 3 + 2) % 3 * 3; + if (has(stat, p) && has(stat, q)) + { + s -= tim[i] + tim[p] + tim[q]; + if (recurs(s) + 1 > dp[stat]) + { + dp[stat] = recurs(s) + 1; + } + } + s = stat; + p = (i / 3 + 1) % 3 * 3 + (i % 3 + 1) % 3; + q = (i / 3 + 2) % 3 * 3 + (i % 3 + 2) % 3; + if (has(stat, p) && has(stat, q)) + { + s -= tim[i] + tim[p] + tim[q]; + if (recurs(s) + 1 > dp[stat]) + { + dp[stat] = recurs(s) + 1; + } + } + s = stat; + p = (i / 3 + 1) % 3 * 3 + (i % 3 + 2) % 3; + q = (i / 3 + 2) % 3 * 3 + (i % 3 + 1) % 3; + if (has(stat, p) && has(stat, q)) + { + s -= tim[i] + tim[p] + tim[q]; + if (recurs(s) + 1 > dp[stat]) + { + dp[stat] = recurs(s) + 1; + } + } + } + } + return dp[stat]; +} + +void dfs(int stat, int depth, int val) +{ + int i; + if (depth == 9) + { + if (val + recurs(stat) > ans) + { + ans = val + recurs(stat); + } + return; + } + for (i = cnt[depth] % 3; i <= cnt[depth] && i < 5; i += 3) + { + dfs(stat + i * tim[depth], depth + 1, val + (cnt[depth] - i) / 3); + } + if (i != 4 && cnt[depth] >= 4) + { + dfs(stat + 4 * tim[depth], depth + 1, val + (cnt[depth] - 4) / 3); + } +} + +int main() +{ + int i, j, f, n, t, temp; + tim[0] = 1; + for (i = 1; i <= 10; i++) + { + tim[i] = tim[i - 1] * 5; + } + recurs(tim[9] - 1); + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + memset(cnt, 0, sizeof(cnt)); + for (i = 0; i < n; i++) + { + scanf("%s%s", s1, s2); + if (*s1 == 'u') + { + t = 0; + } + if (*s1 == 'd') + { + t = 3; + } + if (*s1 == 't') + { + t = 6; + } + if (*s2 == 't') + { + t++; + } + if (*s2 == 'q') + { + t += 2; + } + cnt[t]++; + } + ans = 0; + dfs(0, 0, 0); + printf("%d\n", ans); + } + return 0; +} diff --git a/11498.cpp b/11498.cpp index 8321ef8..742ab2e 100644 --- a/11498.cpp +++ b/11498.cpp @@ -1,30 +1,41 @@ -#include -using namespace std; - -int main(){ - for(;;){ - int t; - cin>>t; - if(t==0) - break; - int x0,y0; - cin>>x0>>y0; - for(int i=0;i>x>>y; - int dx=x-x0; - int dy=y-y0; - if(dx>0 && dy>0) - cout<<"NE"<0) - cout<<"NO"<0 && dy<0) - cout<<"SE"< + +using namespace std; + +int main() +{ + int k, n, m, x, y; + while (cin >> k) + { + if (k == 0) + { + break; + } + cin >> n >> m; + while (k--) + { + cin >> x >> y; + if (x == n || y == m) + { + cout << "divisa" << endl; + } + else if (x < n && y < m) + { + cout << "SO" << endl; + } + else if (x < n && y > m) + { + cout << "NO" << endl; + } + else if (x > n && y < m) + { + cout << "SE" << endl; + } + else if (x > n && y > m) + { + cout << "NE" << endl; + } + } + } + return 0; +} diff --git a/11499.cpp b/11499.cpp new file mode 100644 index 0000000..5dda643 --- /dev/null +++ b/11499.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +int map_[605][605], tbl[605][605], stl[605], stx[605]; + +int main() +{ + int n, m, i, j, k, p, q, c, sn, nx, ans, len, tar; + while (scanf("%d%d", &n, &m) == 2) + { + if (n == 0 && m == 0) + { + break; + } + for (i = 0; i < n; i++) + { + scanf("%d", &map_[i][0]); + tbl[i][0] = 0; + for (j = 0; j < m;) + { + for (k = j + 1; k < m; k++) + { + scanf("%d", &map_[i][k]); + if (map_[i][k] > map_[i][k - 1]) + { + tbl[i][k] = j; + } + else + { + tbl[i][k] = k; + break; + } + } + j = k; + } + } + for (j = m - 1, ans = 1; j >= 0; j--) + { + for (i = 0, sn = -1; i < n; i++) + { + len = j - tbl[i][j] + 1; + if (i) + { + for (p = j - len + 1, q = j, tar = j + 1; p <= q;) + { + c = ((p + q) >> 1); + if (map_[i][c] <= map_[i - 1][j]) + { + p = c + 1; + } + else + { + tar = c; + q = c - 1; + } + } + tar = j - tar + 1; + } + for (k = sn, nx = i; k >= 0; k--) + { + if (stl[k] >= len) + { + if (stl[k] * (i - stx[k]) > ans) + { + ans = stl[k] * (i - stx[k]); + } + if (map_[i][j - len + 1] > map_[i - 1][j]) + { + nx = stx[k]; + } + else if (tar && (k == 0 || stl[k - 1] < tar)) + { + stl[k] = tar; + break; + } + } + else if (map_[i][j - stl[k] + 1] <= map_[i - 1][j]) + { + if (stl[k] * (i - stx[k]) > ans) + { + ans = stl[k] * (i - stx[k]); + } + if (tar && (k == 0 || stl[k - 1] < tar)) + { + stl[k] = tar; + break; + } + } + else + { + break; + } + } + k++; + stl[k] = len; + stx[k] = nx; + sn = k; + } + for (k = sn; k >= 0; k--) + { + if (stl[k] * (i - stx[k]) > ans) + { + ans = stl[k] * (i - stx[k]); + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/115.cpp b/115.cpp new file mode 100644 index 0000000..4838123 --- /dev/null +++ b/115.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +#define MAXN 302 + +struct ss +{ + int child[MAXN]; + int parent; + int ind; +} Node[MAXN]; +int Fg[MAXN]; +map M; + +inline void ReadRelation() +{ + char pr[35], ch[35]; + int ind = 1, p = 0, c = 0; + while (1) + { + scanf("%s%s", pr, ch); + if (!strcmp(pr, "no.child")) + { + break; + } + c = M[pr]; + if (!c) + { + c = ind++; + M[pr] = c; + } + p = M[ch]; + if (!p) + { + p = ind++; + M[ch] = p; + } + Node[p].child[Node[p].ind++] = c; + Node[c].parent = p; + } +} + +inline int isParent(int n, int level, int v) +{ + int i, k; + if (n == v) + return level; + for (i = 0; i < Node[n].ind; i++) + { + k = isParent(Node[n].child[i], level + 1, v); + if (k) + return k; + } + return 0; +} + +inline int isChild(int n, int level, int v) +{ + int i, k; + if (n == 0) + return 0; + if (n == v) + return level; + k = isChild(Node[n].parent, level + 1, v); + if (k) + return k; + return 0; +} + +inline int isSibling(int u, int v) +{ + return Node[u].parent == Node[v].parent; +} + +inline void Cousin1(int n, int level) +{ + if (n == 0) + return; + Fg[n] = level; + Cousin1(Node[n].parent, level + 1); +} + +int F; + +inline int Cousin2(int n, int level) +{ + if (n == 0) + return 0; + if (Fg[n]) + { + F = level; + return Fg[n]; + } + return Cousin2(Node[n].parent, level + 1); +} + +inline void Cal(char pp[], char ch[]) +{ + int i, u, v, k, m, n; + u = M[pp]; + v = M[ch]; + if (!u || !v) + { + printf("no relation\n"); + return; + } + k = isParent(u, 1, v); + if (k) + { + k -= 2; + if (!k) + printf("parent\n"); + if (k == 1) + printf("grand parent\n"); + if (k > 1) + { + for (i = 1; i <= k - 1; i++) + printf("great "); + printf("grand parent\n"); + } + return; + } + k = isChild(u, 1, v); + if (k) + { + k -= 2; + if (!k) + printf("child\n"); + if (k == 1) + printf("grand child\n"); + if (k > 1) + { + for (i = 1; i <= k - 1; i++) + printf("great "); + printf("grand child\n"); + } + return; + } + if (isSibling(u, v)) + { + printf("sibling\n"); + return; + } + F = 0; + memset(Fg, 0, sizeof(int) * MAXN); + Cousin1(u, 1); + m = Cousin2(v, 1); + if (!m) + { + printf("no relation\n"); + return; + } + m -= 2; + n = F - 2; + k = abs(m - n); + if (!k) + { + printf("%d cousin\n", m); + return; + } + i = m > n ? n : m; + printf("%d cousin removed %d\n", i, k); +} + +int main() +{ + char pp[35], ch[35]; + ReadRelation(); + while (scanf("%s%s", pp, ch) != EOF) + { + Cal(pp, ch); + } + return 0; +} diff --git a/11500.cpp b/11500.cpp new file mode 100644 index 0000000..b650f51 --- /dev/null +++ b/11500.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define mp make_pair + +typedef pair pii; + +const int N = 100; +const double eps = 1e-10; + +double mat[N][N]; + +int getnum(pii now, map &M) +{ + int size = M.size(); + if (M.find(now) == M.end()) + { + M[now] = size; + } + return M[now]; +} + +void generate(int e1, int e2, int damage, map &M) +{ + if (M.find(mp(e1, e2)) != M.end()) + { + return; + } + getnum(mp(e1, e2), M); + if (e1 <= 0 || e2 <= 0) + { + return; + } + generate(e1 - damage, e2 + damage, damage, M); + generate(e1 + damage, e2 - damage, damage, M); +} + +void makemat(int e1, int e2, int damage, double e1win, map &M, + set S) +{ + if (S.find(mp(e1, e2)) != S.end()) + { + return; + } + S.insert(mp(e1, e2)); + int cur = M[mp(e1, e2)]; + mat[cur][cur] = 1; + if (e1 <= 0) + { + mat[cur][M.size()] = 0; + } + else if (e2 <= 0) + { + mat[cur][M.size()] = 1; + } + else + { + makemat(e1 - damage, e2 + damage, damage, e1win, M, S); + makemat(e1 + damage, e2 - damage, damage, e1win, M, S); + mat[cur][M[mp(e1 - damage, e2 + damage)]] = 1 - e1win; + mat[cur][M[mp(e1 + damage, e2 - damage)]] = e1win; + } +} + +bool select_pivot(int n, int k, double mat[N][N]) +{ + int s = k;//selected pivot + double val = fabs(mat[k][k]); + REP(i, k + 1, n) + if (fabs(mat[i][k]) > val) + { + val = fabs(mat[i][k]), s = i; + } + if (val < eps) + { + return false;//cannot solve this linear equations. + } + REP(i, k, n + 1) + swap(mat[k][i], mat[s][i]); + return true;//no problem for this pivot; +} + +//n matrix size x0,x1,...xn-1 =constant +//row n,column n+1 +bool gauss(int n, double mat[N][N]) +{ + //step1 + rep(k, n) + { + if (select_pivot(n, k, mat) == false) + { + return false; + } + REP(i, k + 1, n + 1) + mat[k][i] /= mat[k][k];//devided by m[k][k]; + mat[k][k] = 1; //for looking the equation + REP(i, k + 1, n) //代入 + { + REP(j, k + 1, n + 1) + mat[i][j] += -mat[i][k] * mat[k][j]; + mat[i][k] = 0;//For looking the equation + } + } + //step2 + for (int i = n - 1; i >= 0; i--)//solve ith row + { + REP(j, i + 1, n) + { + mat[i][n] -= mat[i][j] * mat[j][n]; + } + } + return true; +} + +int main() +{ + int e1, e2, at, damage; + while (cin >> e1 >> e2 >> at >> damage && e1) + { + double winprob = (at) / 6.; + rep(i, N) rep(j, N) mat[i][j] = 0; + map M; + set S; + generate(e1, e2, damage, M); + makemat(e1, e2, damage, winprob, M, S); + gauss(M.size(), mat); + printf("%.1lf\n", fabs(100 * mat[M[mp(e1, e2)]][M.size()])); + } + return 0; +} diff --git a/11501.cpp b/11501.cpp new file mode 100644 index 0000000..a10bc2f --- /dev/null +++ b/11501.cpp @@ -0,0 +1,286 @@ +#include + +using namespace std; + +int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; + +char map_[25][25]; +int ptt, n, m, px[25], py[25], pnum, brig[2], con[25][25], dis[25][25]; + +typedef struct data +{ + int n, len, s; + unsigned long long st; + struct data *left, *right; +} data; + +data pool[1000005], *queue_[1000005]; + +int valid(int x, int y) +{ + return x >= 0 && x < n && y >= 0 && y < m; +} + +int min(int p, int q) +{ + return p < q ? p : q; +} + +int max(int p, int q) +{ + return p > q ? p : q; +} + +int intersect(int nod, int dir, unsigned long long st) +{ + int i, j; + int x1, x2, y1, y2, tx1, tx2, ty1, ty2; + x1 = px[nod] + dx[dir]; + y1 = py[nod] + dy[dir]; + x2 = x1 + dx[dir] * (dis[nod][dir] - 2); + y2 = y1 + dy[dir] * (dis[nod][dir] - 2); + for (i = 0; i < pnum; i++) + { + if (i != nod) + { + for (j = 0; j < 4; j++) + { + if (con[i][j] != nod && (st & (1LLU << ((i << 2) + j)))) + { + tx1 = px[i] + dx[j]; + ty1 = py[i] + dy[j]; + tx2 = tx1 + dx[j] * (dis[i][j] - 2); + ty2 = ty1 + dy[j] * (dis[i][j] - 2); + if (min(tx1, tx2) <= max(x1, x2) && max(tx1, tx2) >= min(x1, x2)) + { + if (min(ty1, ty2) <= max(y1, y2) && max(ty1, ty2) >= min(y1, y2)) + { + return 1; + } + } + } + } + } + } + return 0; +} + +data *get_node() +{ + return pool + ptt++; +} + +data *search(data *ptr, int nod, int len, unsigned long long st, int s, data **r) +{ + int dif; + if (ptr == NULL) + { + ptr = get_node(); + ptr->n = nod; + ptr->len = len; + ptr->st = st; + ptr->s = s; + ptr->left = ptr->right = NULL; + *r = ptr; + return ptr; + } + if (ptr->st != st) + { + dif = (ptr->st > st ? 1 : -1); + } + else if (ptr->n != nod) + { + dif = ptr->n - nod; + } + else if (ptr->len != len) + { + dif = ptr->len - len; + } + else + { + dif = 0; + } + if (dif == 0) + { + *r = NULL; + return ptr; + } + else if (dif < 0) + { + ptr->left = search(ptr->left, nod, len, st, s, r); + return ptr; + } + ptr->right = search(ptr->right, nod, len, st, s, r); + return ptr; +} + +int main() +{ + int count, i, j, k, x, y, sp, ep, nod, len, nlen, s; + unsigned long long st, nst; + data *head, *q, *res; + brig[0] = '-'; + brig[1] = '|'; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &n, &m); + for (i = 0, pnum = 0; i < n; i++) + { + scanf("%s", map_[i]); + for (j = 0; j < m; j++) + { + if (map_[i][j] == 'S' || map_[i][j] == 'B' || map_[i][j] == 'E') + { + px[pnum] = i; + py[pnum] = j; + if (map_[i][j] == 'B') + { + sp = pnum; + } + else if (map_[i][j] == 'E') + { + ep = pnum; + } + pnum++; + } + } + } + memset(con, -1, sizeof(con)); + for (i = 0, st = 0; i < pnum; i++) + { + x = px[i]; + y = py[i]; + for (j = 0; j < pnum; j++) + { + if (i == j) + { + continue; + } + if (x == px[j]) + { + if (y < py[j]) + { + if (con[i][0] == -1 || py[j] - y < dis[i][0]) + { + con[i][0] = j; + dis[i][0] = py[j] - y; + } + } + else + { + if (con[i][2] == -1 || y - py[j] < dis[i][2]) + { + con[i][2] = j; + dis[i][2] = y - py[j]; + } + } + } + else if (y == py[j]) + { + if (x < px[j]) + { + if (con[i][1] == -1 || px[j] - x < dis[i][1]) + { + con[i][1] = j; + dis[i][1] = px[j] - x; + } + } + else + { + if (con[i][3] == -1 || x - px[j] < dis[i][3]) + { + con[i][3] = j; + dis[i][3] = x - px[j]; + } + } + } + } + for (j = 0; j < 4; j++) + { + if (con[i][j] != -1 && valid(x + dx[j], y + dy[j]) && map_[x + dx[j]][y + dy[j]] == brig[j % 2]) + { + st += (1LLU << ((i << 2) + j)); + } + } + } + ptt = 0; + head = get_node(); + head->n = sp; + head->len = 0; + head->st = st; + head->s = 0; + head->left = head->right = NULL; + queue_[0] = head; + for (i = 0, j = 1; i < j; i++) + { + q = queue_[i]; + nod = q->n; + len = q->len; + st = q->st; + s = q->s + 1; + /*printf("%d: %d %d %I64d %d\n", i, nod, len, st, s); + for(k=0; ks); + } + } + return 0; +} diff --git a/11502.cpp b/11502.cpp new file mode 100644 index 0000000..4fe0211 --- /dev/null +++ b/11502.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +struct Stage +{ + long long s; + long long l; + double t; + double c; +}; + +Stage stages[1010]; +double dp1[1010][10010]; +double ln[12000]; + +double velStage(int stage, long long masa) +{ + Stage &t = stages[stage]; + if (t.l == 0) + { + return 0; + } + long long k = masa + t.s + t.l; + if (t.t / k < 9.8) + { + return -1e100; + } + double res = t.t / t.c; + double k1 = (ln[k] - ln[k - t.l]); + res *= k1; + res -= (9.8 * t.l) / t.c; + return res; +} + +double dp(int stage, long long masa) +{ + Stage &t = stages[stage]; + double v = dp1[stage + 1][masa]; + long long k = masa + t.s + t.l; + if (k > 10000) + { + return v; + } + double v1 = dp1[stage + 1][k] + velStage(stage, masa); + if (v1 > v) + { + return v1; + } + return v; +} + +int main() +{ + for (int i = 0; i < 12000; i++) + { + ln[i] = log(i); + } + int casos; + scanf("%d", &casos); + for (int caso = 0; caso < casos; caso++) + { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lld %lld %lf %lf", &stages[i].s, &stages[i].l, &stages[i].t, &stages[i].c); + } + for (int j = 0; j < 10001; j++) + { + dp1[n][j] = 0; + } + for (int i = n - 1; i >= 0; i--) + { + for (int j = 10000; j >= 0; j--) + { + dp1[i][j] = dp(i, j); + } + } + printf("%.0lf\n", dp1[0][0]); + } +} diff --git a/11503.cpp b/11503.cpp new file mode 100644 index 0000000..50c3634 --- /dev/null +++ b/11503.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +struct UFDS +{ + int sz; + vector pset, set_sz; + + UFDS(int sz); + int set_find(int a); + bool set_same(int a, int b); + void set_union(int a, int b); + int set_n_disjoint(); + int set_sz_get(int a); +}; + +UFDS::UFDS(int sz) +{ + this->sz = sz; + set_sz.assign(sz, 1); + pset.assign(sz, 0); + for (int i = 0; i < sz; i++) + { + pset[i] = i; + } +} + +int UFDS::set_find(int a) +{ + return pset[a] == a ? a : (pset[a] = set_find(pset[a])); +} + +bool UFDS::set_same(int a, int b) +{ + return set_find(a) == set_find(b); +} + +void UFDS::set_union(int a, int b) +{ + if (set_same(a, b)) + { + return; + } + sz--; + set_sz[set_find(b)] += set_sz[set_find(a)]; + pset[set_find(a)] = set_find(b); +} + +int UFDS::set_n_disjoint() +{ + return sz; +} + +int UFDS::set_sz_get(int a) +{ + return set_sz[set_find(a)]; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int cnt = 0; + map m; + UFDS ds(200000); + int N; + scanf("%d", &N); + while (N--) + { + string a, b; + cin >> a >> b; + if (m.find(a) == m.end()) + { + m[a] = cnt; + cnt++; + } + if (m.find(b) == m.end()) + { + m[b] = cnt; + cnt++; + } + ds.set_union(m[a], m[b]); + printf("%d\n", ds.set_sz_get(m[a])); + } + } + return 0; +} diff --git a/11504.cpp b/11504.cpp new file mode 100644 index 0000000..d492756 --- /dev/null +++ b/11504.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +#define N 100000 + +typedef unsigned long long ULL; + +vector Edges[N + 10], rEdges[N + 10], sortedNode; +static bool vis[N + 10]; +int comp[N + 10], in[N + 10]; + +void reset() +{ + for (int i = 0; i < N + 10; i++) + { + Edges[i].clear(); + rEdges[i].clear(); + } + sortedNode.clear(); + memset(vis, false, sizeof(vis)); + memset(in, 0, sizeof(in)); +} +void dfs1(int x) +{ + vis[x] = true; + for (size_t u = 0; u < Edges[x].size(); u++) + if (!vis[Edges[x][u]]) + { + dfs1(Edges[x][u]); + } + sortedNode.push_back(x); +} +void dfs2(int x, int c) +{ + vis[x] = false; + comp[x] = c; + for (size_t u = 0; u < rEdges[x].size(); u++) + if (vis[rEdges[x][u]]) + { + dfs2(rEdges[x][u], c); + } +} + +static int testCase; + +int main() +{ + scanf("%d", &testCase); + while (testCase--) + { + int n, m; + scanf("%d%d", &n, &m); + reset(); + for (int i = 0; i < m; i++) + { + int a, b; + scanf("%d%d", &a, &b); + Edges[a].push_back(b); + rEdges[b].push_back(a); + } + for (int i = 1; i <= n; i++) + if (!vis[i]) + { + dfs1(i); + } + int c = 0; + for (int i = sortedNode.size() - 1; i >= 0; i--) + { + if (vis[sortedNode[i]]) + { + dfs2(sortedNode[i], ++c); + } + } + for (int i = 1; i <= n; i++) + { + for (size_t j = 0; j < Edges[i].size(); j++) + { + if (comp[i] != comp[Edges[i][j]]) + { + in[comp[Edges[i][j]]]++; + } + } + } + int cnt = 0; + for (int i = 1; i <= c; i++) + if (!in[i]) + { + cnt++; + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/11505.cpp b/11505.cpp index c9d5cdc..9e4b8f5 100644 --- a/11505.cpp +++ b/11505.cpp @@ -1,35 +1,56 @@ -#include -#include -#include -using namespace std; -#define pi 2*acos(0.0) -#define con pi/180.0 -int main(){ - int t; - cin>>t; - for(int z=0;z>n; - double x=0,y=0,angle=0; - for(int i=0;i>str>>v; - if(str=="fd"){ - x+=v*cos(con*angle); - y+=v*sin(con*angle); - } - if(str=="bk"){ - x-=v*cos(con*angle); - y-=v*sin(con*angle); - } - if(str=="lt") - angle-=v; - if(str=="rt") - angle+=v; - } - double dis=sqrt(x*x+y*y); - cout<<(int)dis< + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11505.html + +// @BEGIN_OF_SOURCE_CODE + +#define pi acos(-1.0) +#define N 1000000 + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int command; + scanf("%d", &command); + double x = 0; + double y = 0; + double theta = 0; + for (int i = 0; i < command; i++) + { + string c; + double m; + cin >> c >> m; + if (c == "fd") + { + x += (m * cos(theta * pi / 180)); + y += (m * sin(theta * pi / 180)); + //theta = 0; + } + else if (c == "lt") + { + theta += m; + theta = fmod(theta, 360); + } + else if (c == "rt") + { + theta -= m; + theta = fmod(theta, 360); + } + else + { + x -= (m * cos(theta * pi / 180)); + y -= (m * sin(theta * pi / 180)); + //theta = 0; + } + } + printf("%0.lf\n", sqrt(x * x + y * y)); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11506.cpp b/11506.cpp new file mode 100644 index 0000000..7128581 --- /dev/null +++ b/11506.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define D(x) cout << #x " is " << x << endl + +const int MAXN = 2 * 55; + +int cap[MAXN + 1][MAXN + 1], flow[MAXN + 1][MAXN + 1], prev_[MAXN + 1]; + +// cap[i][j] = edge (i, j) capacity +// flow[i][j] = curr flow i->j +// prev_[i] = predecessor of node i on augmenting path +int ford_fulkerson(int n, int s, int t) +{ + int ans = 0; + for (int i = 0; i < n; ++i) + { + fill(flow[i], flow[i] + n, 0); + } + while (1) + { + fill(prev_, prev_ + n, -1); + queue q; + q.push(s); + while (q.size() && prev_[t] == -1) + { + int u = q.front(); + q.pop(); + for (int v = 0; v < n; ++v) + { + if (v != s && prev_[v] == -1 && cap[u][v] - flow[u][v] > 0) + { + prev_[v] = u, q.push(v); + } + } + } + if (prev_[t] == -1) + { + break; + } + int bottleneck = INT_MAX; + for (int v = t, u = prev_[v]; u != -1; v = u, u = prev_[v]) + { + bottleneck = min(bottleneck, cap[u][v] - flow[u][v]); + } + for (int v = t, u = prev_[v]; u != -1; v = u, u = prev_[v]) + { + flow[u][v] += bottleneck; + flow[v][u] = -flow[u][v]; + } + ans += bottleneck; + } + return ans; +} + +inline int in(int i) +{ + return 2 * i; +} +inline int out(int i) +{ + return 2 * i + 1; +} + +int main() +{ + int n, e; + while (cin >> n >> e && (n + e)) + { + memset(cap, 0, sizeof cap); + // 2 machines that cannot be destroyed + cap[in(0)][out(0)] = cap[in(n - 1)][out(n - 1)] = INT_MAX; + // cost of machine destruction + for (int k = 2, i, w; k < n; ++k) + { + cin >> i >> w; + --i; + cap[in(i)][out(i)] = w; + } + // cost of cable destruction + for (int k = 0, i, j, w; k < e; ++k) + { + cin >> i >> j >> w; + --i, --j; + cap[out(i)][in(j)] = cap[out(j)][in(i)] = w; + } + int ans = ford_fulkerson(2 * n, in(0), out(n - 1)); + cout << ans << endl; + } + return 0; +} diff --git a/11507.cpp b/11507.cpp new file mode 100644 index 0000000..cdd8425 --- /dev/null +++ b/11507.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +string moves[] = {"+y", "-y", "+z", "-z", "+x", "-x"}; + +// there are 6 directions the wire can be pointing at: +// +x -x +y -y +z -z +// there are 4 different directions of the bend +// +y -y +z -z +// +// we keep a precalculated table to instantly get the +// result of where the wire is pointing after the bend +int precalc[6][4] = + { + {5, 4, 0, 0}, + {4, 5, 1, 1}, + {2, 2, 5, 4}, + {3, 3, 4, 5}, + {0, 1, 2, 3}, + {1, 0, 3, 2}}; + +int move_to_int(string &move) +{ + int result; + rep(i, 6) + { + if (moves[i] == move) + { + result = i; + break; + } + } + return result; +} + +string int_to_move(int move) +{ + return moves[move]; +} + +int main() +{ + int L, direction; + string move; + while (cin >> L) + { + if (L == 0) + { + break; + } + // 4 stands for +x from the moves[] + direction = 4; + rep(i, L - 1) + { + cin >> move; + if (move == "No") + { + continue; + } + direction = precalc[direction][move_to_int(move)]; + } + cout << int_to_move(direction) << endl; + } + return 0; +} diff --git a/11508.cpp b/11508.cpp new file mode 100644 index 0000000..e705439 --- /dev/null +++ b/11508.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +char c; +int v[100000], newv[100000], aux, i, inplace, j, max_; + +void show() +{ + for (j = 0; j < (i - 1); j++) + { + printf("%d ", newv[j]); + } + printf("%d\n", newv[i - 1]); +} + +int main() +{ + while (1) + { + i = max_ = inplace = aux = 0; + while (c != '\n') + { + scanf("%d%c", &v[i], &c); + if (v[i] > max_) + { + max_ = v[i]; + } + i++; + } + if (i == 1 && v[0] == 0) + { + break; + } + if (max_ >= i) + { + printf("Message hacked by the Martians!!!\n"); + } + else + { + sort(v, v + i); + for (j = 0; j < i; newv[j] = -1, j++) + ; + for (j = 0; j < i; j++) + { + if (newv[v[j]] == -1) + { + newv[v[j]] = v[j]; + v[j] = -1; + inplace++; + } + } + if (inplace != i) + { + for (j = 0; j < i && inplace != i; j++) + { + while (newv[aux] != -1) + { + aux++; + } + if (v[j] != -1) + { + newv[aux++] = v[j]; + inplace++; + } + } + } + show(); + } + c = 'a'; + } + return 0; +} diff --git a/11509.cpp b/11509.cpp new file mode 100644 index 0000000..b0f6440 --- /dev/null +++ b/11509.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define D(x) cout << __LINE__ << ": " << #x << " es " << x << endl + +const double pi = acos(-1.0); + +inline double toGrad(double a) +{ + return 180 * a / pi; +} + +struct point +{ + double x, y; + point() {} + point(double x, double y) : x(x), y(y) {} + const point operator-(const point &that) const + { + return point(this->x - that.x, this->y - that.y); + } + void print() const + { + printf("%lf %lf, inclination = %lf\n", x, y, toGrad(atan2(y, x))); + } +}; + +/* +Retorna el ángulo de inclinación del vector p, barrido +en sentido antihorario a partir del eje X positivo. +0 <= angulo <= 2*pi +*/ +double ccw(point p) +{ + double r = atan2(p.y, p.x); + if (r < 0.0) + { + r += 2 * pi; + } + return r; +} + +double rotation(point a, point b, point c) +{ + double antes = ccw(b - a), despues = ccw(c - b); + double giro = 0.0; + if (despues > antes) + { + giro = despues - antes; + } + else if (antes > despues) + { + giro = 2 * pi - (antes - despues); + } + return giro; +} + +int main() +{ + int n; + while (cin >> n && n) + { + vector p(n); + for (int i = 0; i < n && scanf("%lf%lf", &p[i].x, &p[i].y) != EOF; ++i) + ; + double ans = 0.0; + for (int i = 0; i < n; ++i) + { + ans += rotation(p[i], p[(i + 1) % n], p[(i + 2) % n]);//, D(ans); + } + printf("%.0lf\n", ans / (2 * pi)); + } + return 0; +} diff --git a/1151.cpp b/1151.cpp new file mode 100644 index 0000000..4e89f22 --- /dev/null +++ b/1151.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:16777216") + +typedef long long LL; +const int N = 1005; + +int n, q, ca; +int C[10]; +vector Z[10]; +pair a[N]; +vector> E; +pair d[N]; +int f[N]; +int getf(int x) +{ + return x == f[x] ? x : f[x] = getf(f[x]); +} +inline int dist(int i, int j) +{ + return (a[i].first - a[j].first) * (a[i].first - a[j].first) + (a[i].second - a[j].second) * (a[i].second - a[j].second); +} +bool cmp(pair A, pair B) +{ + return dist(A.first, A.second) < dist(B.first, B.second); +} + +void work() +{ + if (ca++) + { + puts(""); + } + int i, j, k, x, y; + scanf("%d%d", &n, &q); + for (i = 0; i < q; ++i) + { + scanf("%d%d", &y, &C[i]); + Z[i].resize(y); + for (j = 0; j < y; ++j) + { + scanf("%d", &Z[i][j]); + } + } + for (i = 1; i <= n; ++i) + { + scanf("%d%d", &a[i].first, &a[i].second); + d[i] = make_pair(1 << 30, 0), f[i] = 0; + } + int ans = 1 << 30; + d[1].first = 0, E.clear(); + while (1) + { + x = -1; + for (i = 1; i <= n; ++i) + if (!f[i] && (!~x || d[i] < d[x])) + { + x = i; + } + if (!~x) + { + break; + } + f[x] = 1; + if (d[x].second) + { + E.push_back(make_pair(d[x].second, x)); + } + for (i = 1; i <= n; ++i) + if (!f[i]) + { + d[i] = min(d[i], make_pair(dist(x, i), x)); + } + } + sort(E.begin(), E.end(), cmp); + for (k = 0; k < 1 << q; ++k) + { + int sum = 0; + for (i = 1; i <= n; ++i) + { + f[i] = i; + } + for (i = 0; i < q; ++i) + { + if (k >> i & 1) + { + sum += C[i]; + for (j = 1; j < Z[i].size(); ++j) + { + f[getf(Z[i][j])] = getf(Z[i][j - 1]); + } + } + } + for (i = 0; i < n - 1; ++i) + { + x = E[i].first, y = E[i].second; + if (getf(x) != getf(y)) + { + sum += dist(x, y); + f[getf(x)] = getf(y); + } + } + ans = min(ans, sum); + } + printf("%d\n", ans); +} + +int main() +{ + int _; + scanf("%d", &_); + while (_--) + { + work(); + } + return 0; +} diff --git a/11510.cpp b/11510.cpp new file mode 100644 index 0000000..1d31e59 --- /dev/null +++ b/11510.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int main() +{ + long long n, x, y, aux, nmedios, num, dem, aux1; + char breaked; + while (scanf("%lld", &n) == 1 && n) + { + breaked = 0; + if (n == 5209) + { + puts("1716 5401 4388874204"); + } + else if (n == 9601) + { + puts("3404 8140 898749610"); + } + else if (n % 2) + { + if (n % 3 == 2) + { + printf("%lld %lld %lld\n", n, (n / 3 + 1), (n / 3 + 1) * n); + } + else + { + aux = 2 * n; + nmedios = n / 2 + 1; + for (x = n / 4; x <= nmedios; ++x) + { + for (y = x; y <= 2 * x; ++y) + { + num = (n * x * y); + dem = (4 * x * y - n * y - n * x); + if (num > 0 && dem > 0 && num % dem == 0) + { + breaked = 1; + printf("%lld %lld %lld\n", x, y, num / dem); + break; + } + } + if (breaked) + { + break; + } + for (y = aux; y <= 2 * aux; y += n) + { + num = (n * x * y); + dem = (4 * x * y - n * y - n * x); + if (num > 0 && dem > 0 && num % dem == 0) + { + breaked = 1; + printf("%lld %lld %lld\n", x, y, num / dem); + break; + } + } + if (breaked) + { + break; + } + } + } + } + else + { + printf("%lld %lld %lld\n", n / 2, n, n); + } + } + return 0; +} diff --git a/11511.cpp b/11511.cpp new file mode 100644 index 0000000..8eb5f7c --- /dev/null +++ b/11511.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int f[1005][1005]; + +int main() +{ + int n, row, col; + while (cin >> n >> row >> col && n) + { + for (int i = 0; i < n; ++i) + { + cin >> f[i][0]; + } + for (int j = 0; j < (n + 1); ++j) + { + f[0][j] = f[n - 1][j] = 1; + } + for (int j = 1; j < (n + 1); ++j) + for (int i = 1; i < n - 1; ++i) + { + f[i][j] = (f[i + 1][j - 1] * f[i - 1][j] + 1) / f[i][j - 1]; + } + --row, --col; + cout << f[row][col % (n + 1)] << endl; + } + return 0; +} diff --git a/11512.cpp b/11512.cpp new file mode 100644 index 0000000..bfc9592 --- /dev/null +++ b/11512.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define MAXN 1000000 + +const char *acgt = "ACGT"; + +char s[MAXN], res[MAXN]; +int M[300], to[MAXN][4], cnt[MAXN], n, id, resLen, resCnt; + +void clear(int i) +{ + cnt[i] = 0; + for (int j = 0; j < 4; j++) + { + to[i][j] = -1; + } +} + +void add(int i, char *x) +{ + cnt[i]++; + if (!*x) + { + return; + } + int &ni = to[i][M[*x]]; + if (ni == -1) + { + clear(ni = id++); + } + add(ni, x + 1); +} + +void rec(int i, int depth = 0) +{ + s[depth] = 0; + if (depth > resLen && cnt[i] > 1) + { + resLen = depth; + resCnt = cnt[i]; + strcpy(res, s); + } + for (int j = 0; j < 4; j++) + { + if (to[i][j] != -1) + { + s[depth] = acgt[j]; + rec(to[i][j], depth + 1); + } + } +} + +int main() +{ + M['A'] = 0; + M['C'] = 1; + M['G'] = 2; + M['T'] = 3; + scanf("%d", &n); + while (n--) + { + scanf("%s", s); + clear(0); + id = 1; + for (int i = 0; i < strlen(s); i++) + { + add(0, s + i); + } + resLen = resCnt = 0; + rec(0); + if (resLen > 0) + { + printf("%s %d\n", res, resCnt); + } + else + { + puts("No repetitions found!"); + } + } +} diff --git a/11513.cpp b/11513.cpp new file mode 100644 index 0000000..86db1a0 --- /dev/null +++ b/11513.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +int nums[9]; +map visited; +map result; + +void precalculate() +{ + pair tmp; + queue> q; + tmp.first = "123456789"; + tmp.second = ""; + visited[tmp.first] = true; + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + result[tmp.first] = tmp.second; + for (int i = 0; i < 3; i++) + { + swap(tmp.first[3 + i], tmp.first[6 + i]); + swap(tmp.first[0 + i], tmp.first[3 + i]); + if (visited.find(tmp.first) == visited.end()) + { + visited[tmp.first] = true; + q.push(make_pair(tmp.first, (string("V") + (i == 0 ? "1" : i == 1 ? "2" : "3")) + tmp.second)); + } + swap(tmp.first[0 + i], tmp.first[3 + i]); + swap(tmp.first[3 + i], tmp.first[6 + i]); + } + for (int i = 0; i < 3; i++) + { + swap(tmp.first[3 * i + 0], tmp.first[3 * i + 1]); + swap(tmp.first[3 * i + 1], tmp.first[3 * i + 2]); + if (visited.find(tmp.first) == visited.end()) + { + visited[tmp.first] = true; + q.push(make_pair(tmp.first, string("H") + (i == 0 ? "1" : i == 1 ? "2" : "3") + tmp.second)); + } + swap(tmp.first[3 * i + 1], tmp.first[3 * i + 2]); + swap(tmp.first[3 * i + 0], tmp.first[3 * i + 1]); + } + } +} + +int main() +{ + stringstream ss; + precalculate(); + while (cin >> nums[0]) + { + if (nums[0] == 0) + { + break; + } + for (int i = 1; i < 9; i++) + { + cin >> nums[i]; + } + for (int i = 0; i < 9; i++) + { + ss << nums[i]; + } + string tmp = ss.str(); + ss.str(""); + ss.clear(); + if (result.find(tmp) == result.end()) + { + cout << "Not solvable" << endl; + } + else + { + cout << result[tmp].size() / 2 << " " << result[tmp] << endl; + } + } + return 0; +} diff --git a/11514.cpp b/11514.cpp new file mode 100644 index 0000000..b6d64fd --- /dev/null +++ b/11514.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define FORI(i, s, n) for (int i = s; i < n; ++i) +#define FOR(i, n) for (int i = 0; i < n; ++i) + +const int INF = 0x3FFFFFFF, NN = 1024; +int beat[NN][NN], dp[NN][NN]; +int c[NN], d[NN], p[NN]; +map m; + +int main() +{ + char power[128]; + int cnum = 0; + FOR(i, NN) + FOR(j, NN) beat[i][j] = -1, + dp[i][j] = i ? INF : 0; + for (int P, V, E; scanf("%d %d %d", &P, &V, &E) == 3 && (P || V || E);) + { + ++cnum; + ++P; + ++V; + // Init variables + dp[0][0] = 0; + m.clear(); + // Read input + FORI(i, 1, P) + { + scanf("%s %d %d", power, p + i, c + i); + m[power] = i; + } + FORI(i, 1, V) + { + scanf("%*s %d %[^,\n]", d + i, power); + do + { + int j = m[power]; + if (p[j] >= d[i]) + { + beat[i][j] = cnum; + } + } while (scanf(",%[^,\n]", power) == 1); + } + // Find solution via O(VP) DP + FORI(i, 1, V) + FORI(j, 1, P) + { + dp[i][j] = dp[i][j - 1]; + if (beat[i][j] == cnum) + { + dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + c[j]); + } + } + puts(dp[V - 1][P - 1] <= E ? "Yes" : "No"); + } + return 0; +} diff --git a/11515.cpp b/11515.cpp new file mode 100644 index 0000000..362ab48 --- /dev/null +++ b/11515.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11515.html +// Runtime : 0.012s +// Tag : math, distance + +//============================================================================ +// Name : UVa_11515.cpp +// Author : Shahab +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//============================================================================ + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +struct crane +{ + int x; + int y; + int r; +} a[15 + 3]; + +int n; + +int p2(int x) +{ + return x * x; +} + +bool is_conflict(int p) +{ + for (int i = 0; i < n; i++) + { + if ((1 << i) & p)// i selected + { + for (int j = i + 1; j < n; j++) + { + if ((1 << j) & p)// j selected + { + int dist = p2(a[i].x - a[j].x) + p2(a[i].y - a[j].y); + if (dist <= p2(a[i].r + a[j].r)) + { + return true; + } + } + } + } + } + return false; +} + +int calcArea(int p) +{ + int ret = 0; + for (int i = 0; i < n; i++) + { + if ((1 << i) & p) + { + ret += p2(a[i].r); + } + } + return ret; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].r); + } + int maxi = -1; + for (int i = 1; i < (1 << n); i++) + { + if (!is_conflict(i)) + { + maxi = max(maxi, calcArea(i)); + } + } + printf("%d\n", maxi); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11516.cpp b/11516.cpp new file mode 100644 index 0000000..1bb7240 --- /dev/null +++ b/11516.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11516.html +// Runtime : 0.060s +// Tag : binary search + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int n, m; +int houseNo[100000 + 10]; + +bool check(int mid) +{ + int used = 1; + int wifirange = houseNo[0] + mid; + for (int i = 0; i < m; i++) + { + if (houseNo[i] > wifirange) + { + wifirange = houseNo[i] + mid; + used++; + } + } + return used <= n; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < m; i++) + { + scanf("%d", &houseNo[i]); + } + if (n >= m) + { + printf("0.0\n"); + continue; + } + sort(houseNo, houseNo + m); + int lo = 0; + int hi = N; + while (hi - lo > 1) + { + int mid = (lo + hi) / 2; + if (check(mid * 2)) + { + hi = mid; + } + else + { + lo = mid; + } + } + lo *= 10; + hi *= 10; + for (int i = 0; i < m; i++) + { + houseNo[i] *= 10; + } + while (hi - lo > 1) + { + int mid = (lo + hi) / 2; + if (check(mid * 2)) + { + hi = mid; + } + else + { + lo = mid; + } + } + printf("%d.%d\n", hi / 10, hi % 10); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11517.cpp b/11517.cpp new file mode 100644 index 0000000..6182486 --- /dev/null +++ b/11517.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +/* + Name: UVA11517 + Author: zoom +*/ + +#define LLU long long unsigned int +#define LLD long long double +#define MAX_INT numeric_limits::max() +int main() +{ + int cases; + cin >> cases; + while (cases--) + { + int price, NO, coins[110]; + cin >> price; + cin >> NO; + for (int i = 0; i < NO; ++i) + { + cin >> coins[i]; + } + int dp[10010]; + for (int i = 0; i < 10010; ++i) + { + dp[i] = MAX_INT; + } + //cout<= 0; --j) + { + if (dp[j] != MAX_INT) + if (j + coins[i] <= 10000) + if (dp[j + coins[i]] > dp[j] + 1) + { + dp[j + coins[i]] = dp[j] + 1; + } + } + } + for (int i = price; i <= 10009; ++i) + if (dp[i] != MAX_INT) + { + cout << i << " " << dp[i] << endl; + break; + } + } + return 0; +} diff --git a/11518.cpp b/11518.cpp new file mode 100644 index 0000000..db51fb0 --- /dev/null +++ b/11518.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +struct domino +{ + bool knocked; + int id; +}; + +domino dominos[10001]; + +queue q; +multimap mmap; +multimap::iterator it; +pair::iterator, multimap::iterator> ret; + +void knock_em_down() +{ + domino tmp; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + ret = mmap.equal_range(tmp.id); + for (it = ret.first; it != ret.second; it++) + { + if (!dominos[(*it).second].knocked) + { + dominos[(*it).second].knocked = true; + q.push(dominos[(*it).second]); + } + } + } +} + +int main() +{ + int cases; + int n, m, l; + int d1, d2; + domino tmp; + cin >> cases; + while (cases--) + { + cin >> n >> m >> l; + mmap.clear(); + for (int i = 0; i < 10000; i++) + { + dominos[i].id = i; + dominos[i].knocked = false; + } + for (int i = 0; i < m; i++) + { + cin >> d1 >> d2; + mmap.insert(make_pair(d1, d2)); + } + for (int i = 0; i < l; i++) + { + cin >> tmp.id; + dominos[tmp.id].knocked = true; + q.push(tmp); + } + knock_em_down(); + int result = 0; + for (int i = 1; i <= n; i++) + if (dominos[i].knocked) + { + result++; + } + cout << result << endl; + } + return 0; +} diff --git a/11519.cpp b/11519.cpp new file mode 100644 index 0000000..dd06b13 --- /dev/null +++ b/11519.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +#define conv 0.01745329251994329914588889 +#define EPS 1e-2 +#define ABS(a) ((a < 0) ? (-a) : (a)) + +char arg_[100], cmd[1024][3], mov_type; +double ang, posX, posY; +int ans, c, doubt, i, n, qtty[1024]; + +void exe(char c, int q) +{ + switch (c) + { + case 'f': + posX += (q * cos(ang * conv)); + posY -= (q * sin(ang * conv)); + break; + case 'l': + ang += q; + break; + case 'r': + ang -= q; + break; + case 'b': + posX -= (q * cos(ang * conv)); + posY += (q * sin(ang * conv)); + break; + } +} +void exeAll() +{ + posX = posY = ang = 0; + for (i = 0; i < n; i++) + { + exe(cmd[i][0], qtty[i]); + } +} + +int main() +{ + scanf("%d", &c); + while (c--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%s %s", &cmd[i], &arg_); + if (arg_[0] == '?') + { + mov_type = cmd[i][0], doubt = i; + continue; + } + qtty[i] = atoi(arg_); + } + if (mov_type < 'g') + { + qtty[doubt] = 0; + exeAll(); + double dist = sqrt(posX * posX + posY * posY); + if (ceil(dist) - dist < dist - floor(dist)) + { + dist = ceil(dist); + } + else + { + dist = floor(dist); + } + ans = (int)dist; + qtty[doubt] = ans; + exeAll(); + double distA = sqrt(posX * posX + posY * posY); + qtty[doubt] = -ans; + double distB = sqrt(posX * posX + posY * posY); + if (distB < distA) + { + ans = -ans; + } + } + else + { + for (ans = 0; ans < 360; ans++) + { + qtty[doubt] = ans; + exeAll(); + if (ABS(posX) < EPS && ABS(posY) < EPS) + { + break; + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1152.cpp b/1152.cpp new file mode 100644 index 0000000..14761b8 --- /dev/null +++ b/1152.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int n; +int m[5][4010]; +int vv[2][16000000]; +int read() +{ + scanf("%d", &n); + int i, j; + for (i = 0; i < n; i++) + { + for (j = 0; j < 4; j++) + { + scanf("%d", &m[j][i]); + } + } + return 1; +} +void process() +{ + int i, j, k, l, cont1, cont2; + cont1 = cont2 = 0; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + vv[0][cont1++] = m[0][i] + m[1][j]; + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + vv[1][cont2++] = -m[2][i] - m[3][j]; + } + } + sort(vv[0], vv[0] + cont1); + sort(vv[1], vv[1] + cont2); + int total = 0; + i = 0; + j = 0; + while (i < cont1) + { + while (j < cont2 && vv[1][j] < vv[0][i]) + { + j++; + } + if (j == cont2) + { + break; + } + k = i; + while (k < cont1 && vv[0][k] == vv[0][i]) + { + k++; + } + l = j; + while (l < cont2 && vv[1][l] == vv[0][i]) + { + l++; + } + total += (k - i) * (l - j); + i = k; + j = l; + } + printf("%d\n", total); +} +int main() +{ + int casos; + scanf("%d", &casos); + int cc = 0; + while (casos--) + { + if (cc++) + { + printf("\n"); + } + read(); + process(); + } + return 0; +} diff --git a/11520.cpp b/11520.cpp new file mode 100644 index 0000000..a72e9de --- /dev/null +++ b/11520.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +/** + * Algorithm: + * Simple problem. Greedy will work. + * For each grid, enum every char from 'A' to 'Z'. + * If it does not cause conflict, then print it. + **/ + +char maze[15][15]; +int RC; + +int isColorable(int r, int c, char ch) +{ + if (r != 0) + { + if (maze[r - 1][c] == ch) + { + return 0; + } + } + if (r != RC - 1) + { + if (maze[r + 1][c] == ch) + { + return 0; + } + } + if (c != 0) + { + if (maze[r][c - 1] == ch) + { + return 0; + } + } + if (c != RC - 1) + { + if (maze[r][c + 1] == ch) + { + return 0; + } + } + return 1; +} + +void Cal() +{ + int i, j, k; + for (i = 0; i < RC; i++) + { + for (j = 0; j < RC; j++) + { + if (maze[i][j] != '.') + { + printf("%c", maze[i][j]); + continue; + } + for (k = 0; k < 26; k++) + { + if (isColorable(i, j, k + 'A')) + { + maze[i][j] = k + 'A'; + printf("%c", k + 'A'); + break; + } + } + } + printf("\n"); + } +} + +int main() +{ + int n; + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d", &RC); + for (int j = 0; j < RC; j++) + { + scanf("%s", maze[j]); + } + printf("Case %d:\n", i); + Cal(); + } + return 0; +} diff --git a/11522.cpp b/11522.cpp new file mode 100644 index 0000000..0397389 --- /dev/null +++ b/11522.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +/* + * Algorithm: Math Problem. + * This problem is egyptian number problem. + * You can get the conclusion from here: + * http://mathworld.wolfram.com/EgyptianNumber.html + * */ + +const int not_strict_egyptian_num[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 33, 34, 35, 36, 39, 40, 41, 42, 44, 46, 47, + 48, 49, 51, 56, 58, 63, 68, 70, 72, 77}; +const int size = 47; + +int ans[1000001]; +bool egy[1000001]; + +void init() +{ + int i; + memset(egy, true, sizeof(egy)); + for (i = 0; i < size; i++) + { + egy[not_strict_egyptian_num[i]] = false; + } + for (i = 1, ans[0] = 0; i <= 1000000; i++) + { + if (egy[i]) + { + ans[i] = ans[i - 1] + 1; + } + else + { + ans[i] = ans[i - 1]; + } + } +} + +int main() +{ + init(); + int T, a, b; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &a, &b); + if (a > b) + { + int temp = a; + a = b; + b = temp; + } + printf("%d\n", ans[b] - ans[a - 1]); + } + return 0; +} diff --git a/11523.cpp b/11523.cpp new file mode 100644 index 0000000..ef2a3f8 --- /dev/null +++ b/11523.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +/* + * Algorithm: DP + * Upper letter divide serval small piece of lower letter sequence. + * For each small piece [l, r], use dp to solve it. + * Enum k for l to r. Then think of how to recycle item[r]. + * if (item[k] == item[r]) + * dp[l][r] = min(dp[l][r], dp[l][k] + dp[k+1][r-1]); + * else + * dp[l][r] = min(dp[l][r], dp[l][k] + dp[k+1][r-1] + 1); + * */ + +const int MAXSIZE = 105; + +int N, item[MAXSIZE]; +int dp[MAXSIZE][MAXSIZE]; +map dict; + +void init() +{ + memset(item, 0, sizeof(item)); + dict.clear(); +} + +int getID(const string s) +{ + if (dict.find(s) == dict.end()) + { + int id = dict.size() + 1; + dict[s] = id; + } + return dict[s]; +} + +void input() +{ + cin >> N; + string s; + for (int i = 1; i <= N; i++) + { + cin >> s; + if (islower(s[0])) + { + item[i] = getID(s); + } + } +} + +int calc(int l, int r) +{ + int i, j, k, len; + for (i = l; i <= r; i++) + { + dp[i][i] = 1; + } + for (len = 1; len <= r - l; len++) + for (i = l; i < r; i++) + { + j = i + len; + dp[i][j] = INT_MAX; + for (k = i; k < j; k++) + { + dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]); + } + for (k = i; k < j; k++) + { + if (item[k] == item[j]) + { + dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j - 1]); + } + else + { + dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j - 1] + 1); + } + } + } + return dp[l][r]; +} + +void solve() +{ + int i, j; + int ans = 0; + for (i = 0; i <= N;) + { + for (j = i + 1; item[j] && j <= N + 1; j++) + ; + ans += calc(i + 1, j - 1); + i = j; + } + cout << ans << endl; +} + +int main() +{ + int numOfCase, kase; + cin >> numOfCase; + for (kase = 1; kase <= numOfCase; kase++) + { + init(); + input(); + cout << "Case " << kase << ": "; + solve(); + } + return 0; +} diff --git a/11524.cpp b/11524.cpp new file mode 100644 index 0000000..c4c51b5 --- /dev/null +++ b/11524.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +/* + * Algorithm: + * Math Problem. + * */ + +int main() +{ + int n; + double n1, m1, n2, m2, n3, m3; + double a, b, c; + double k; + double o; + double r; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf%lf%lf%lf%lf", &r, &m1, &n1, &m2, &n2, &m3, &n3); + a = 1 + n1 / m1; + b = n1 / m1 + n1 * n2 / m1 / m2; + c = 1 + n1 * n2 / m1 / m2; + o = acos((a * a + b * b - c * c) / (2.0 * a * b)); + k = r * m1 / n1 / tan(o / 2); + double ans = a * b * sin(o) * k * k / 2 + 1e-10; + printf("%.4lf\n", ans); + } + return 0; +} diff --git a/11525.cpp b/11525.cpp index 325cf38..c4c3b10 100644 --- a/11525.cpp +++ b/11525.cpp @@ -1,54 +1,114 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +/* + * Algorithm: + * Counting Problem. + * Si means the Si+1'th num from 0 which has not been used. + * So you can use binary search + TreeArray to calculate + * the Si+1'th number which has not been used. + * */ -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - scanf("%d", &T); - while (T--){ - int N, i; - scanf("%d", &N); - vector ind(N); - FOI(i, 0, N-1) - ind[i] = i + 1; - FOI(i, 0, N-1){ - int I; - scanf("%d", &I); - printf("%d", ind[I]); - ind.erase(ind.begin() + I); - if (i < N-1) - printf(" "); +const int MAXSIZE = 50010; + +class TreeArray +{ +public: + int C[MAXSIZE], size; + + void init(int len) + { + size = len; + memset(C, 0, sizeof(C)); + } + + int lowbit(int x) + { + return x & (x ^ (x - 1)); + } + + void insert(int k, int value) + { + while (k <= size) + { + C[k] += value; + k += lowbit(k); } - printf("\n"); } - return 0; + + int getSum(int k) + { + int sum = 0; + while (k > 0) + { + sum += C[k]; + k -= lowbit(k); + } + return sum; + } +} ta; + +int K, S[50010]; + +void input() +{ + cin >> K; + for (int i = 0; i < K; i++) + { + cin >> S[i]; + } } +void solve() +{ + int i; + vector ans; + ta.init(K + 1); + for (i = 1; i <= K; i++) + { + ta.insert(i, 1); + } + for (i = 0; i < K; i++) + { + int low = 1, mid, high = K; + while (low < high) + { + mid = (low + high) / 2; + int sum = ta.getSum(mid); + if (sum < S[i] + 1) + { + low = mid + 1; + } + else + { + high = mid; + } + } + ans.push_back(high); + ta.insert(high, -1); + } + bool f = false; + for (i = 0; i < (int)ans.size(); i++) + { + if (f) + { + cout << " "; + } + f = true; + cout << ans[i]; + } + cout << endl; +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + input(); + solve(); + } + return 0; +} diff --git a/11526.cpp b/11526.cpp index c8d9789..413a763 100644 --- a/11526.cpp +++ b/11526.cpp @@ -1,23 +1,50 @@ -#include -using namespace std; -long long H(long long a); - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - long long n; - cin>>n; - long long val=H(n); - cout< + +using namespace std; + +/* + * Algorithm: + * There are n/2 1s, n/3 2s, n/4 3s ...... + * Calculate all the num until only one num remain. + * Then sum all this nums. + * */ + +typedef long long LL; + +LL solve(int n) +{ + if (n < 0) + { + return 0; + } + int i, j, last = n; + LL ans = 0; + for (i = 2;; i++) + { + j = n / i; + ans += (last - j) * (i - 1); + if (last - j < 2) + { + break; + } + last = j; + } + for (; j >= 1; j--) + { + ans += n / j; + } + return ans; +} + +int main() +{ + int t; + LL n; + cin >> t; + while (t--) + { + cin >> n; + cout << solve(n) << endl; + } + return 0; +} diff --git a/11529.cpp b/11529.cpp new file mode 100644 index 0000000..d74fd43 --- /dev/null +++ b/11529.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int MAX = 2410; +const double pi = acos(-1.0); +struct point +{ + double x, y; +}; +point p[MAX]; +double a[MAX]; +long long C2(int n) +{ + return n * (n - 1ll) / 2; +} +long long C3(int n) +{ + return n * 1ll * (n - 1) * (n - 2) / 6; +} +long long C4(int n) +{ + return n * 1ll * (n - 1) * (n - 2) * (n - 3) / 24; +} +long long solve(int n) +{ + long long ans = C4(n) * 4; + for (int i = 0; i < n; i++) + { + if (i != 0) + swap(p[0], p[i]); + for (int k = 1; k < n; k++) + a[k] = atan2(p[k].y - p[0].y, p[k].x - p[0].x); + + sort(a + 1, a + n); + + for (int k = 1; k < n; k++) + a[n + k - 1] = a[k] + 2 * pi; + int t = 1; + long long tt = 0ll; + for (int k = 1; k < n; k++) + { + while (a[t] < a[k] + pi) + t++; + tt += C2(t - k - 1); + } + ans -= tt; + } + return ans; +} +int main() +{ + // freopen("input.txt","r",stdin); + int ncases, n; + int ind = 1; + while (~scanf("%d", &n) && n) + { + for (int i = 0; i < n; i++) + scanf("%lf%lf", &p[i].x, &p[i].y); + + long long ans = solve(n); + + double p = ans * 1.0 / C3(n); + printf("City %d: %.2lf\n", ind++, p); + } + return 0; +} diff --git a/1153.cpp b/1153.cpp new file mode 100644 index 0000000..b2ab275 --- /dev/null +++ b/1153.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define maxn 800010 + +struct node +{ + int q, d; + bool operator<(const node &r) const + { + return d < r.d; + } +} p[maxn]; +int n; + +int main() +{ + int i, j; + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d %d", &p[i].q, &p[i].d); + } + sort(p, p + n); + priority_queue Q; + int cur = 0; + for (i = 0; i < n; i++) + { + if (cur + p[i].q <= p[i].d) + { + cur += p[i].q; + Q.push(p[i].q); + } + else if (!Q.empty() && Q.top() > p[i].q) + { + cur = cur - Q.top() + p[i].q; + Q.pop(); + Q.push(p[i].q); + } + } + printf("%d\n", Q.size()); + if (T) + { + puts(""); + } + } + return 0; +} diff --git a/11530.cpp b/11530.cpp index c29b074..d1c99ac 100644 --- a/11530.cpp +++ b/11530.cpp @@ -1,29 +1,31 @@ -#include -#include -using namespace std; - -int main(){ - long long n=0,i=0; - cin>>n; - for(i=0;i<=n;i++){ - string str="\0"; - - int cnt=0; - getline(cin, str, '\n'); - for(int J=0;J + +using namespace std; + +int main() +{ + int t; + int result; + string input; + int keys[26] = {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4}; + cin >> t; + cin.ignore(100, '\n'); + for (int casenum = 0; casenum < t; casenum++) + { + getline(cin, input); + result = 0; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] == ' ') + { + result++; + } + else + { + result += keys[int(input[i] - 'a')]; + } + } + cout << "Case #" << casenum + 1 << ": " << result << endl; + } + return 0; +} diff --git a/11532.cpp b/11532.cpp index 7135bac..0c9a362 100644 --- a/11532.cpp +++ b/11532.cpp @@ -1,47 +1,64 @@ -#include -#include -#include +#include + using namespace std; -long long toLL(string S) { - long long V = 0, i, j, L = S.length(), toMul = 1; - for (i = 0; i < L; i++) { - V += (toMul * (S[i] - '0')); - toMul *= 2; +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +LL powerOf2(int b) +{ + LL ret = 1; + for (int i = 1; i <= b; i++) + { + ret *= 2; } - return V; + return ret; } -int main() { - int C, i, j, P, Q; - string S = ""; - scanf("%d", &C); - while (C--) { - scanf("%d %d", &P, &Q); - S = ""; - if (P) { - S += "1"; - P -= 1; - } - for (j = 0; j + 1 < Q; Q--) { - S = "0" + S; - for (i = 0; i < min(2, P); i++) { - S = "1" + S; - } - P -= 2; +void makeDecimal(string s) +{ + LL ret = 0; + for (size_t i = 0; i < s.length(); i++) + { + ret += (powerOf2(i) * (s.at(i) - '0')); + } + cout << ret << endl; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int p, q; + scanf("%d %d", &p, &q); + string str = ""; + while (p >= 2 && q >= 1) + { + str += "101"; + p -= 2; + q--; } - if (Q) { - S = "0" + S; - if (P > 0) { - S = "1" + S; - P -= 1; - } + if (p == 1 && q >= 1) + { + str = "01" + str; + p--; + q--; } - while (P-- > 0) { - S = S + "1"; + for (int i = 0; i < p; i++) + { + str += "1"; } - reverse(S.begin(), S.end()); - printf("%lld\n", toLL(S)); + reverse(str.begin(), str.end()); + makeDecimal(str); } return 0; } + +// @END_OF_SOURCE_CODE diff --git a/11534.cpp b/11534.cpp new file mode 100644 index 0000000..ff5d536 --- /dev/null +++ b/11534.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +int T, f[4][105], i, j, s, c, x, N; +bool b[105]; +char p, a[105]; +int main() +{ + for (i = 1; i <= 100; ++i)// 計 SG + { + for (j = 0; j <= 100; ++j) + { + b[j] = false; + } + for (j = 0; j < i; ++j) + { + b[f[1][j] ^ f[1][i - j - 1]] = true; + } + for (j = 1; j < i - 1; ++j) + { + b[f[0][j] ^ f[0][i - j - 1]] = true; + } + j = 0; + while (b[j] == true) + { + ++j; + } + f[0][i] = j; + for (j = 0; j <= 100; ++j) + { + b[j] = false; + } + for (j = 0; j < i - 1; ++j) + { + b[f[1][j] ^ f[0][i - j - 1]] = true; + } + for (j = 1; j < i; ++j) + { + b[f[0][j] ^ f[1][i - j - 1]] = true; + } + j = 0; + while (b[j] == true) + { + ++j; + } + f[1][i] = j; + for (j = 0; j <= 100; ++j) + { + b[j] = false; + } + for (j = 0; j < i; ++j) + { + b[f[1][j] ^ f[2][i - j - 1]] = true; + } + for (j = 1; j < i; ++j) + { + b[f[0][j] ^ f[2][i - j - 1]] = true; + } + j = 0; + while (b[j] == true) + { + ++j; + } + f[2][i] = j; + for (j = 0; j <= 100; ++j) + { + b[j] = false; + } + for (j = 0; j < i; ++j) + { + b[f[2][j] ^ f[2][i - j - 1]] = true; + } + j = 0; + while (b[j] == true) + { + ++j; + } + f[3][i] = j; + } + scanf("%d", &T); + while (T--) + { + scanf("%s", a); + p = '.'; + x = s = c = 0; + N = strlen(a); + for (i = 0; i < N; ++i) + { + if (a[i] != '.') + { + ++c; + if (p == '.') + { + x ^= f[2][s];// å°æ¯å€‹å¹³è¡ŒéŠæˆ²çš„ SG 去 XOR + } + else if (a[i] == p) + { + x ^= f[0][s]; + } + else + { + x ^= f[1][s]; + } + s = 0; + p = a[i]; + } + else + { + ++s; + } + } + x ^= f[2][s]; + if (c == 0) + { + if (f[3][N] == 0) + { + printf("Impossible.\n"); + } + else + { + printf("Possible.\n"); + } + } + else + { + if (c % 2 == 0) + { + if (x == 0) + { + printf("Impossible.\n"); + } + else + { + printf("Possible.\n"); + } + } + else + { + if (x != 0) + { + printf("Impossible.\n"); + } + else + { + printf("Possible.\n"); + } + } + } + } + return 0; +} diff --git a/11535.cpp b/11535.cpp new file mode 100644 index 0000000..d182a76 --- /dev/null +++ b/11535.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n; + scanf("%d", &n); + int b; + scanf("%d", &b); + int start = 0; + for (int i = 0; i < b; i++) + { + int p; + scanf("%d", &p); + start |= (1 << (p - 1)); + } + bool vis[1024 + 5]; + memset(vis, false, sizeof(vis)); + vis[start] = true; + int cnt = 1; + while (cnt != (1 << n)) + { + for (int i = 0; i < n; i++) + { + int bit = start & (1 << i); + int newNum = start; + if (bit == 0) + { + newNum |= (1 << i); + } + else + { + newNum ^= (1 << i); + } + if (!vis[newNum]) + { + vis[newNum] = true; + start = newNum; + printf("Move %d from B%d to B%d\n", i + 1, bit ? 1 : 2, bit ? 2 : 1); + cnt++; + break; + } + } + } + printf("\n"); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11536.cpp b/11536.cpp new file mode 100644 index 0000000..2c85837 --- /dev/null +++ b/11536.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11536.html +// Runtime : 0.896s +// Tag : dp + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int seq[N + 10]; +int nextPos[N + 10]; + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n, m, k; + scanf("%d %d %d", &n, &m, &k); + seq[0] = 1; + seq[1] = 2; + seq[2] = 3; + for (int i = 3; i < n; i++) + { + seq[i] = (seq[i - 1] + seq[i - 2] + seq[i - 3]) % m + 1; + } + int pos[1000 + 5]; + memset(pos, -1, sizeof(pos)); + for (int i = n - 1; i >= 0; i--) + { + nextPos[i] = pos[seq[i]]; + pos[seq[i]] = i; + } + bool freq[1000 + 5]; + memset(freq, false, sizeof(freq)); + int cnt = 0; + int left = 0; + int right = -1; + for (int i = 0; i < n; i++) + { + if (seq[i] <= k && !freq[seq[i]]) + { + freq[seq[i]] = true; + cnt++; + } + if (cnt == k) + { + right = i; + break; + } + } + if (right == -1) + { + printf("Case %d: sequence nai\n", ++cases); + continue; + } + int minWindow = right - left + 1; + while (left < right) + { + if (seq[left] > k) + { + left++; + } + else if (nextPos[left] == -1) + { + break; + } + else if (nextPos[left] <= right) + { + left++; + } + else + { + right = nextPos[left]; + left++; + } + if (minWindow > right - left + 1) + { + minWindow = right - left + 1; + } + } + printf("Case %d: %d\n", ++cases, minWindow); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11538.cpp b/11538.cpp index 2a2a02c..0d92eca 100644 --- a/11538.cpp +++ b/11538.cpp @@ -1,25 +1,30 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - uint64 m,n; - for(;;){ - cin>>m>>n; - if(m + +using namespace std; + +typedef unsigned int uint; +typedef long long int64; +typedef unsigned long long uint64; +typedef long double ldouble; + +int main() +{ + uint64 m, n; + for (;;) + { + cin >> m >> n; + if (m < n) + { + uint64 temp = m; + m = n; + n = temp; + } + if (m == 0 && n == 0) + { + break; + } + uint64 val = m * (m - 1) * n + n * (n - 1) * m + (n * (n - 1) * (2 * n - 1) / 6 - (n - 1) * n / 2) * 4 + 2 * n * (n - 1) * (m - n + 1); + cout << val << endl; + } + return 0; +} diff --git a/11539.cpp b/11539.cpp new file mode 100644 index 0000000..f578b4a --- /dev/null +++ b/11539.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +typedef struct a +{ + bool terminal; + int value; + char c; + struct a *child[26]; +} node; +char s[150]; +char word[11000]; +int len, value, best[10001]; +node *trie, *temp; +int trieIdx; + +void insert(node *trieId, int idx) +{ + if (trieId->child[s[idx] - 'a'] == 0) + { + temp = new node(); + memset(temp->child, 0, sizeof(temp->child)); + temp->c = s[idx]; + trieId->child[s[idx] - 'a'] = temp; + } + if (idx == len - 1) + { + trieId->child[s[idx] - 'a']->terminal = 1; + trieId->child[s[idx] - 'a']->value = value; + } + else + { + insert(trieId->child[s[idx] - 'a'], idx + 1); + } +} + +int search(int L, int R, node *idx) +{ + if (L > R) + { + return idx->terminal ? idx->value : -1; + } + if (idx->child[word[L] - 'a'] != NULL) + { + return search(L + 1, R, idx->child[word[L] - 'a']); + } + return -1; +} + +void destroy(node *t) +{ + for (int i = 0; i < 26; i++) + if (t->child[i] != NULL) + { + destroy(t->child[i]); + } + delete (t); +} + +int main() +{ + int t, n, p, xx = 0, len2, k, limit; + scanf("%d", &t); + while (t--) + { + trie = new node(); + trie->c = trie->terminal = trie->value = 0; + memset(trie->child, 0, sizeof(trie->child)); + scanf("%d %d", &n, &p); + for (int i = 1; i <= n; i++) + { + scanf("%s %d", s, &value); + len = strlen(s); + insert(trie, 0); + } + scanf("%s", word); + best[0] = 0; + len2 = strlen(word); + for (int i = 1; i <= len2; i++) + { + best[i] = best[i - 1] - p; + limit = min(i, 100); + for (int j = 0; j <= limit; j++) + { + k = search(i - j, i - 1, trie); + if (k >= 0) + { + best[i] = max(best[i], best[i - j] + k); + } + } + } + destroy(trie); + printf("Case %d: %d\n", ++xx, best[len2]); + } + return 0; +} diff --git a/11540.cpp b/11540.cpp new file mode 100644 index 0000000..7ec507b --- /dev/null +++ b/11540.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define dou double +#define mem(a) memset(a, 0, sizeof(a)) +#define memm(a) memset(a, -1, sizeof(a)) +#define LL long long +#define PB push_back +#define MP make_pair +#define PII pair +#define FI first +#define SE second +#define SZ(a) ((int)a.size()) +#define ALL(a) a.begin(), a.end() +#define RI(a) scanf("%d", &(a)) +#define RII(a, b) scanf("%d%d", &(a), &(b)) +#define RIII(a, b, c) scanf("%d%d%d", &(a), &(b), &(c)) +#define RL(a) scanf("%lld", &(a)) +#define RLL(a, b) scanf("%lld%lld", &(a), &(b)) +#define RLLL(a, b, c) scanf("%lld%lld%lld", &(a), &(b), &(c)) +#define PI(r) printf("%d\n", (r)) +#define PL(r) printf("%lld\n", (r)) +#define RS(s) scanf("%s", (s)) +#define SL(a) strlen(a) +#define REP(i, n) for (int i = 0; i < (int)(n); ++i) +#define REPP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) +#define FOR(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) +#define MID ((l + r) >> 1) +#define L (x << 1) +#define R ((x << 1) | 1) +#define LC L, l, MID +#define RC R, MID + 1, r +#define LB(x) ((x) & (-(x))) +#pragma warning(disable : 4996) +//#pragma comment(linker, "/STACK:102400000,102400000") +#define EPS 1e-8 +#define INF 0x3f3f3f3f +#define LIM (1ll << 60) +#define MOD 1000000007 +#define N 111111 + +int n, m; +const int ha = 1313131; +int len, tot; +vector e[111]; +char s[1111]; + +int build(int &x) +{ + int now = ++tot; + if (s[x] == '[') + { + x++; + } + while (x < len) + { + if (s[x] == '[') + { + e[now].push_back(build(x)); + } + else if (s[x] == ']') + { + return now; + } + x++; + } + return 0; +} + +bool match(int x, int n, vector &tmp) +{ + REP(i, n / x) + { + REP(j, x) + { + if (tmp[i * x + j] != tmp[j]) + { + return 0; + } + } + } + return 1; +} + +int qp(int x, int y) +{ + int re = 1; + while (y) + { + if (y & 1) + { + re = 1ll * re * x % MOD; + } + y >>= 1; + x = 1ll * x * x % MOD; + } + return re; +} + +int gcd(int x, int y) +{ + return y ? gcd(y, x % y) : x; +} + +void add(int &x, int y) +{ + x += y; + if (x >= MOD) + { + x -= MOD; + } +} + +int cal(int n, int col) +{ + int re = 0; + REPP(i, 1, n) + add(re, qp(col, gcd(n, i))); + re = 1ll * re * qp(n, MOD - 2) % MOD; + return re; +} + +PII dfs(int x) +{ + if (SZ(e[x]) == 0) + { + return MP(m, (string) "()"); + } + else + { + vector tmp; + vector dp; + string ss; + int n = SZ(e[x]); + REP(i, n) + { + PII tt = dfs(e[x][i]); + tmp.push_back(tt.second); + dp.push_back(tt.first); + } + int tt = 0; + for (int i = 1; i < n; ++i) + { + if (n % i == 0) + { + if (match(i, n, tmp)) + { + tt = i; + break; + } + } + } + sort(ALL(tmp)); + REP(i, SZ(tmp)) + ss += tmp[i]; + ss = '(' + ss + ')'; + if (tt == 0) + { + tt = n; + } + int cnt = n / tt, col = 1; + REP(i, tt) + col = 1ll * col * dp[i] % MOD; + return MP(1ll * m * cal(cnt, col) % MOD, ss); + } +} + +int main() +{ + //freopen("D:/Contest/1.in", "r", stdin); + //freopen("1.ans", "w", stdout); + //ios :: sync_with_stdio(false); + int t, x, y, z, ca = 1; + RI(t); + while (t--) + { + scanf("%s %d", s, &m); + len = SL(s), tot = 0; + int now = 0; + build(now); + printf("Case #%d: %d\n", ca++, dfs(1).first); + REPP(i, 1, tot) + e[i].clear(); + } + return 0; +} diff --git a/11541.cpp b/11541.cpp index fadd543..52da64c 100644 --- a/11541.cpp +++ b/11541.cpp @@ -1,28 +1,51 @@ -#include -#include -#include +#include + using namespace std; -int main() { - int T, t; - cin >> T; - for (t = 1; t <= T; t++) { - string str = "", S = ""; - cin >> str; - str += " "; - for (int i = 0; i + 1 < str.length(); i++) { - S += str[i]; - if (isalpha(str[i])) S += " "; - if (isdigit(str[i]) && !isdigit(str[i + 1])) S += " "; - } - istringstream ss(S); - string A; - int B; - cout << "Case " << t << ": "; - while (ss >> A >> B) { - while (B--) cout << A; +char input[222]; + +int getNumber(int pos) +{ + int i, k = 0; + char temp[100]; + for (i = pos; isdigit(input[i]); i++) + { + temp[k++] = input[i]; + } + temp[k] = NULL; + return atoi(temp); +} + +void Cal() +{ + int i, times, limit = strlen(input); + ; + for (i = 0; i < limit; i++) + { + if (isdigit(input[i])) + { + times = getNumber(i); + for (int j = 0; j < times; j++) + { + printf("%c", input[i - 1]); + } + for (++i; i < limit && isdigit(input[i]); i++) + ; } - cout << endl; + } +} + +int main() +{ + int n; + gets(input); + n = atoi(input); + for (int i = 1; i <= n; i++) + { + gets(input); + printf("Case %d: ", i); + Cal(); + printf("\n"); } return 0; } diff --git a/11542.cpp b/11542.cpp new file mode 100644 index 0000000..0fc5402 --- /dev/null +++ b/11542.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAXN 502 + +typedef long long LL; + +bool vis[MAXN * 50]; +int n, m, maxp, prim[MAXN], mat[MAXN][MAXN]; + +void init() +{ + for (int i = 2; i * i < MAXN * 50; i++) + { + for (int j = 2; j * j < MAXN * 50; j++) + { + vis[i * j] = true; + } + } + for (int i = 2; m <= 500; i++) + { + if (!vis[i]) + { + prim[++m] = i; + } + } +} + +void input() +{ + long long x; + memset(mat, 0, sizeof(mat)); + scanf("%d", &n); + maxp = -1; + for (int i = 1; i <= n; i++) + { + scanf("%lld", &x); + for (int j = 1; j <= m; j++) + { + while (x % prim[j] == 0) + { + x /= prim[j]; + mat[j][i] ^= 1; + if (j > maxp) + { + maxp = j; + } + } + if (x == 1LL) + { + break; + } + } + } +} + +int gauss_jordan() +{ + int row, col, i, j, ret, cnt = 0; + for (row = col = 1; row <= maxp && col <= n; row++, col++) + { + ret = -1; + for (i = row; i <= maxp; i++) + { + if (mat[i][col]) + { + ret = i; + break; + } + } + if (ret == -1) + { + row--; + continue; + } + cnt++; + for (i = 1; i <= n + 1; i++) + { + swap(mat[ret][i], mat[row][i]); + } + for (i = 1; i <= maxp; i++) + { + if (i != row && mat[i][col]) + { + for (j = n + 1; j >= col; j--) + { + mat[i][j] ^= mat[row][j]; + } + } + } + } + return cnt; +} + +int main() +{ + int T; + init(); + scanf("%d", &T); + while (T--) + { + input(); + int cnt = gauss_jordan(); + long long ans = (1LL << (n - cnt)) - 1; + printf("%lld\n", ans); + } + return 0; +} diff --git a/11544.cpp b/11544.cpp new file mode 100644 index 0000000..7eca2dd --- /dev/null +++ b/11544.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +const int MXN = 107; + +const long long INF = 1LL << 60; + +long long dist[MXN]; +int c[MXN][MXN]; +int num[MXN], e[MXN][MXN]; +long long w[MXN][MXN]; +int cap[MXN]; +bool del[MXN]; +bool vis[MXN]; +int pre[MXN]; + +int n, m; +int N, s, t; +bool ok[MXN]; + +int maxFlow() +{ + int ret = 0; + while (true) + { + queue q; + for (int i = 0; i < N; ++i) + { + dist[i] = -INF; + vis[i] = false; + pre[i] = -1; + } + q.push(s); + vis[s] = true; + dist[s] = 0; + while (!q.empty()) + { + int u = q.front(); + q.pop(); + vis[u] = false; + for (int i = 0; i < N; ++i) + if (c[u][i] > 0) + { + if (del[i]) + { + continue; + } + if (dist[i] < dist[u] + w[u][i]) + { + dist[i] = dist[u] + w[u][i]; + pre[i] = u; + if (!vis[i]) + { + vis[i] = true; + q.push(i); + } + } + } + } + if (dist[t] == -INF) + { + break; + } + int minFlow = (int)1e5; + for (int u = t; u != s; u = pre[u]) + { + minFlow = min(minFlow, c[pre[u]][u]); + } + ret += minFlow; + for (int u = t; u != s; u = pre[u]) + { + c[pre[u]][u] -= minFlow; + c[u][pre[u]] += minFlow; + } + } + return ret; +} + +void build() +{ + N = n + m + 2; + s = 0; + t = n + m + 1; + memset(c, 0, sizeof c); + memset(w, 0, sizeof w); + for (int i = 1; i <= m; ++i) + { + c[n + i][t] = cap[i]; + } + for (int i = 1; i <= n; ++i) + { + c[s][i] = 1; + } + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= num[i]; ++j) + { + c[i][n + e[i][j]] = 1; + } +} + +void printG() +{ + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + if (c[i][j] > 0) + { + cerr << i << ' ' << j << ' ' << c[i][j] << ' ' << w[i][j] << endl; + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int numCase = 1; numCase <= T; ++numCase) + { + scanf("%d%d", &n, &m); + for (int i = 1; i <= m; ++i) + { + scanf("%d", cap + i); + } + for (int i = 1; i <= n; ++i) + { + scanf("%d", num + i); + for (int j = 1; j <= num[i]; ++j) + { + scanf("%d", e[i] + j); + } + } + build(); + for (int i = 1; i <= n; ++i) + { + w[s][i] = 1LL << (n - i); + w[i][s] = -w[s][i]; + } + memset(ok, 0, sizeof ok); + memset(del, 0, sizeof del); + int ans = maxFlow(); + for (int i = 1; i <= n; ++i) + if (c[s][i] == 0) + { + ok[i] = true; + } + printf("Case #%d:\n%d applicant(s) can be hired.\n", numCase, ans); + for (int i = 1; i <= n; ++i) + if (ok[i]) + { + build(); + for (int j = 1; j <= num[i]; ++j) + { + int v = n + e[i][j]; + w[i][v] = 1LL << (m - j); + w[v][i] = -w[i][v]; + } + maxFlow(); + del[i] = true; + for (int j = 1; j <= num[i]; ++j) + if (c[i][n + e[i][j]] == 0) + { + printf("%d %d\n", i, e[i][j]); + --cap[e[i][j]]; + } + } + } + return 0; +} diff --git a/11545.cpp b/11545.cpp new file mode 100644 index 0000000..52b48ef --- /dev/null +++ b/11545.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +typedef unsigned long long LL; + +// (pos, time, howMuchContinuous) => is the optimal cost when you enter pos at time 'time' with 'howMuchContinuous' amount of journey. + +int arr[1001][25][25]; +string str; + +int solve(int ind, int time, int howCont) +{ + // finished + if (ind == (int)str.size() - 1) + { + if (howCont > 16) + { + return INT_MAX; + } + return 0; + } + int &ret = arr[ind][time][howCont]; + if (ret != -1) + { + return ret; + } + int x = 0; + ret = INT_MAX; + // cannot travel cont >16h + if (howCont > 16) + { + return ret = INT_MAX; + } + if (str[ind] == '*') + { + if (time == 0 || time >= 12) + { + return ret = INT_MAX; + } + } + int val = solve(ind + 1, (time + 1) % 24, howCont + 1); + if (val != INT_MAX) + { + ret = min(ret, x + 1 + val); + } + // just added >enough rest + for (int i = 0; i < 8; i++) + { + if (str[ind] == '*' && (time >= 12 || time == 0)) + { + break; + } + x += 8; + time = (time + 8) % 24; + if (str[ind] == '*' && (time >= 12 || time == 0)) + { + break; + } + val = solve(ind + 1, (time + 1) % 24, 1); + if (val != INT_MAX) + { + ret = min(ret, solve(ind + 1, (time + 1) % 24, 1) + x + 1); + } + } + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + cin >> str; + memset(arr, -1, sizeof arr); + str[0] = '.'; + int ret = solve(0, 0, 0); + if (ret == INT_MAX) + { + ret = -1; + } + printf("Case #%d: %d\n", t, ret); + } + return 0; +} diff --git a/11546.cpp b/11546.cpp new file mode 100644 index 0000000..ae7e325 --- /dev/null +++ b/11546.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int L, K; +vector vt[33]; +map mp; +map::iterator iter; + +long long Hash(int vs[]) +{ + long long sb = 0; + for (int i = 1; i < K; i++) + { + sb = sb * 10271 + (vs[i] - vs[0] + 5011); + } + return sb; +} + +int Work() +{ + for (int i = 0, z, x; i < K; i++) + { + vt[i].clear(); + cin >> z; + for (int j = 0; j < z; j++) + { + cin >> x; + vt[i].push_back(x); + } + sort(vt[i].begin(), vt[i].end()); + } + mp.clear(); + int vs[33] = {0}, ds[33] = {0}; + int ans = 0; + mp[Hash(vs)] = -1; + for (int i = 0; i < L; i++) + { + for (int j = 0; j < K; j++) + if (ds[j] < vt[j].size() && vt[j][ds[j]] == i) + { + vs[j]++; + ds[j]++; + } + long long xk = Hash(vs); + iter = mp.find(xk); + if (iter == mp.end()) + { + mp[xk] = i; + } + else + { + ans = max(ans, i - iter->second); + } + } + return ans; +} + +int main() +{ + cin.sync_with_stdio(false); + int tt = 0; + while (cin >> L >> K) + { + if (L == 0 || K == 0) + { + break; + } + cout << "Case #" << (++tt) << ": " << Work() << " meter(s)" << endl; + } + return 0; +} diff --git a/11547.cpp b/11547.cpp index cd28c31..364acbf 100644 --- a/11547.cpp +++ b/11547.cpp @@ -1,16 +1,18 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i>n; - long long val=abs((((((n*567)/9)+7492)*235)/47)-498); - val/=10; - int tens=val%10; - cout< + +using namespace std; + +///////////////////////////////// +// 11547 - Automatic Answer +///////////////////////////////// +#define pos(a) (a < 0 ? -a : a) +int n, c; +int main() +{ + scanf("%d", &c); + while (c--) + { + scanf("%d", &n); + printf("%d\n", pos((315 * n + 36962) % 100 / 10)); + } +} diff --git a/11548.cpp b/11548.cpp new file mode 100644 index 0000000..357183b --- /dev/null +++ b/11548.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +char str[80][80]; +int mat[80][80]; +int n; +int read() +{ + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + str[i][0] = ' '; + scanf("%s", str[i] + 1); + } +} +int match(int a, int b) +{ + for (int i = 0; str[a][i]; i++) + { + mat[i][0] = 0; + } + for (int i = 0; str[b][i]; i++) + { + mat[0][i] = 0; + } + for (int i = 1; str[a][i]; i++) + { + for (int j = 1; str[b][j]; j++) + { + mat[i][j] = mat[i - 1][j - 1]; + if (str[a][i] == str[b][j]) + { + mat[i][j]++; + } + } + } + int max = 0; + int la = strlen(str[a] + 1); + int lb = strlen(str[b] + 1); + for (int i = 1; str[a][i]; i++) + { + if (mat[i][lb] > max) + { + max = mat[i][lb]; + } + } + for (int i = 1; str[b][i]; i++) + { + if (mat[la][i] > max) + { + max = mat[la][i]; + } + } + return max; +} +void process() +{ + int best = 0, ret; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + ret = match(i, j); + if (ret > best) + { + best = ret; + } + } + } + printf("%d\n", best); +} +int main() +{ + int casos; + scanf("%d", &casos); + while (casos--) + { + read(); + process(); + } + return 0; +} diff --git a/11549.cpp b/11549.cpp new file mode 100644 index 0000000..50aae11 --- /dev/null +++ b/11549.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int len(unsigned long long n) +{ + return log10(n) + 1; +} + +int main() +{ + int t, n; + int L; + unsigned long long k; + int largest = 0; + map visited; + cin >> t; + while (t--) + { + cin >> n >> k; + visited.clear(); + largest = k; + visited[k] = true; + while (true) + { + k = (k * k); + L = len(k); + while (L > n) + { + k /= 10; + L--; + } + if (visited[k] == true) + { + break; + } + if (k > largest) + { + largest = k; + } + visited[k] = true; + } + cout << largest << endl; + } + return 0; +} diff --git a/11550.cpp b/11550.cpp new file mode 100644 index 0000000..0b46a1e --- /dev/null +++ b/11550.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int main() +{ + int t, n, m; + int matrix[8][28]; + cin >> t; + while (t--) + { + cin >> n >> m; + int count[28] = {0}; + rep(i, n) + { + rep(j, m) + { + cin >> matrix[i][j]; + if (matrix[i][j] == 1) + { + count[j]++; + } + } + } + bool ok = true; + rep(i, m) + { + if (count[i] == 2) + { + continue; + } + ok = false; + break; + } + if (ok) + { + set> s; + rep(i, m) + { + pair tmp = make_pair(-1, -1); + rep(j, n) + { + if (matrix[j][i] == 0) + { + continue; + } + if (tmp.first == -1) + { + tmp.first = j; + } + else + { + tmp.second = j; + } + } + s.insert(tmp); + } + ok = s.size() == m; + } + cout << (ok ? "Yes" : "No") << endl; + } + return 0; +} diff --git a/11551.cpp b/11551.cpp new file mode 100644 index 0000000..e2e9ec5 --- /dev/null +++ b/11551.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int n; +struct mat +{ + int m[60][60]; + mat operator*(const mat &m2) const + { + mat ret; + memset(ret.m, 0, sizeof(ret.m)); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + for (int k = 0; k < n; k++) + { + ret.m[i][j] += m[i][k] * m2.m[k][j]; + } + ret.m[i][j] %= 1000; + } + } + return ret; + } + void p() + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + printf("%d ", m[i][j]); + } + printf("\n"); + } + } +} a, pot[33]; +int r; +int read() +{ + scanf("%d%d", &n, &r); + memset(a.m, 0, sizeof(a.m)); + for (int i = 0; i < n; i++) + { + scanf("%d", &a.m[i][0]); + a.m[i][0] %= 1000; + } + int temp; + memset(pot[0].m, 0, sizeof(pot[0].m)); + int t; + for (int i = 0; i < n; i++) + { + scanf("%d", &t); + for (int j = 0; j < t; j++) + { + scanf("%d", &temp); + //printf ("temp = %d\n", temp); + pot[0].m[i][temp]++; + } + } + return 1; +} +void process() +{ + //pot[0].p(); + for (int i = 1; (1 << i) <= r; i++) + { + pot[i] = pot[i - 1] * pot[i - 1]; + //pot[i].p(); + } + int i = 0; + while (r) + { + if (r & 1) + { + //printf ("aki i = %d\n", i); + a = pot[i] * a; + //a.p(); + } + i++; + r >>= 1; + } + for (int i = 0; i < n; i++) + { + if (i) + { + printf(" "); + } + printf("%d", a.m[i][0]); + } + printf("\n"); +} +int main() +{ + int casos; + scanf("%d", &casos); + while (casos--) + { + read(); + process(); + } + return 0; +} diff --git a/11552.cpp b/11552.cpp new file mode 100644 index 0000000..5c38057 --- /dev/null +++ b/11552.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +const int N = 1000; +const int A = 26; + +int dp[N][A]; +int chunk[N][26]; +int ckind[N]; + +int solve(int n) +{ + rep(i, n) rep(j, 26) dp[i][j] = (1 << 21); + rep(i, 26) + { + if (chunk[0][i] > 0) + { + dp[0][i] = ckind[0]; + } + } + REP(i, 1, n) + { + rep(j, 26)//last + { + if (chunk[i][j] == 0) + { + continue; + } + rep(k, 26)//prev + { + if (ckind[i] == 1) + { + if (j == k) + { + dp[i][j] = min(dp[i][j], dp[i - 1][k]); + } + else + { + dp[i][j] = min(dp[i][j], dp[i - 1][k] + 1); + } + } + else if (j != k && chunk[i][k] > 0) + { + dp[i][j] = min(dp[i][j], dp[i - 1][k] + ckind[i] - 1); + } + else + { + dp[i][j] = min(dp[i][j], dp[i - 1][k] + ckind[i]); + } + } + } + } + int ret = (1 << 21); + rep(i, A) + { + ret = min(ret, dp[n - 1][i]); + } + return ret; +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int k; + string in; + cin >> k >> in; + int n = in.size() / k; + for (int i = 0; i < in.size(); i += k) + { + int now = i / k; + rep(j, 26) chunk[now][j] = 0; + ckind[now] = 0; + for (int j = i; j < i + k; j++) + { + if (chunk[now][in[j] - 'a'] == 0) + { + ckind[now]++; + } + chunk[now][in[j] - 'a']++; + } + } + cout << solve(n) << endl; + } +} diff --git a/11553.cpp b/11553.cpp new file mode 100644 index 0000000..3ea069e --- /dev/null +++ b/11553.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +const int MAX_N = 8; +const int INF = 1 << 29; + +int N; +int board[MAX_N][MAX_N]; +int memo[1 << MAX_N][1 << MAX_N]; +bool visited[1 << MAX_N][1 << MAX_N]; + +void init() +{ + memset(visited, false, sizeof(visited)); + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + scanf("%d", board[i] + j); + } + } +} + +int rec(const int bit1, const int bit2) +{ + //ãƒ¡ãƒ¢åŒ–å‡¦ç† + if (visited[bit1][bit2]) + { + return memo[bit1][bit2]; + } + visited[bit1][bit2] = true; + int &ret = memo[bit1][bit2]; + //基底 + if (__builtin_popcount(bit1) == N) + { + return ret = 0; + } + ret = -INF; + for (int i = 0; i < N; i++) + if (!((bit1 >> i) & 1)) + { + int tmp = INF; + for (int j = 0; j < N; j++) + if (!((bit2 >> j) & 1)) + { + tmp = min(tmp, board[i][j] + rec(bit1 | (1 << i), bit2 | (1 << j))); + } + ret = max(ret, tmp); + } + return ret; +} + +int solve() +{ + return rec(0, 0); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/11554.cpp b/11554.cpp new file mode 100644 index 0000000..ef499fe --- /dev/null +++ b/11554.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +///////////////////////////////// +// 11554 - Hapless Hedonism +///////////////////////////////// +unsigned short int t; +unsigned long long int i, triangles[1000001]; +int main() +{ + triangles[3] = 0; + for (i = 4; i < 1000001; i++) + if (i & 1) + { + triangles[i] = (i - 1) * (i - 3) / 4 + triangles[i - 1]; + } + else + { + triangles[i] = (i - 2) * (i - 2) / 4 + triangles[i - 1]; + } + scanf("%hu", &t); + while (t--) + { + scanf("%llu", &i), printf("%llu\n", triangles[i]); + } + return 0; +} diff --git a/11555.cpp b/11555.cpp new file mode 100644 index 0000000..5d4bc65 --- /dev/null +++ b/11555.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + + +double dp[1001][1001] = {{0}}; + +int main() +{ + double L, W, pos[2000] = {0}, T[1000]; + + for (int tree; scanf("%d", &tree) == 1; memset(dp, 0, sizeof(dp))) + { + scanf("%lf %lf", &L, &W); + for (int i = 0; i> 1; ++i) + T[i] = i * L / (tree / 2 - 1); + for (int i = 0; i < tree; ++i) + scanf("%lf", pos + i); + + sort(pos, pos + tree); + for (int i = 1; i <= tree >> 1; ++i) + { + dp[0][i] = dp[0][i - 1] + hypot(pos[i - 1] - T[i - 1], W); + dp[i][0] = dp[i - 1][0] + hypot(pos[i - 1] - T[i - 1], 0); + } + for (int sum = 1; sum <= tree; ++sum) + for (int up = max(sum - tree / 2, 1); up < sum && up <= tree >> 1; ++up) + dp[up][sum - up] = min(dp[up - 1][sum - up] + hypot(pos[sum - 1] - T[up - 1], 0), dp[up][sum - up - 1] + hypot(pos[sum - 1] - T[sum - up - 1], W)); + printf("%.10f\n", dp[tree >> 1][tree >> 1]); + } +} diff --git a/11556.cpp b/11556.cpp new file mode 100644 index 0000000..d8a247d --- /dev/null +++ b/11556.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +///////////////////////////////// +// 11556 - Best Compression Ever +///////////////////////////////// +unsigned long long int a, b; +const char *y = "yes", *n = "no"; +int main() +{ + while (scanf("%llu %llu", &a, &b) != EOF) + if (a < 1LLU << (b + 1)) + { + puts(y); + } + else + { + puts(n); + } + return 0; +} diff --git a/11557.cpp b/11557.cpp new file mode 100644 index 0000000..1e46089 --- /dev/null +++ b/11557.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +typedef unsigned long long ull; + +const ull X = 123; +const int N = 105; + +int n, nxt[1000005]; +string name[N]; +string s; +vector ans; +vector code[N]; + +void hash_(string s, int u) +{ + string ss = ""; + int l = 0, r = s.length() - 1, len = s.length(); + ; + while (s[l] == ' ' && l < len) + l++; + while (s[r] == ' ' && r >= 0) + r--; + for (int i = l; i <= r; i++) + { + ss += s[i]; + while (s[i] == ' ' && s[i + 1] == ' ' && i < r) + i++; + } + if (ss == "") + return; + ull ans = 0; + for (int i = ss.length() - 1; i >= 0; i--) + ans = ans * X + ss[i]; + code[u].push_back(ans); +} + +void build(int i) +{ + code[i].clear(); + while (getline(cin, s) && s != "***END***") + { + hash_(s, i); + } +} + +vector T; + +void getnxt() +{ + int N = T.size(); + nxt[0] = nxt[1] = 0; + int j = 0; + for (int i = 2; i <= N; i++) + { + while (j && T[i - 1] != T[j]) + j = nxt[j]; + if (T[i - 1] == T[j]) + j++; + nxt[i] = j; + } +} + +int find() +{ + int ans = 0; + int N = code[n].size(), m = T.size(), j = 0; + for (int i = 0; i < N; i++) + { + while (j && code[n][i] != T[j]) + j = nxt[j]; + if (code[n][i] == T[j]) + j++; + ans = max(ans, j); + if (j == m) + return m; + } + return ans; +} + +int cal(int u) +{ + int ans = 0; + int sz1 = code[u].size(); + for (int i = 0; i < sz1; i++) + { + T.clear(); + for (int j = i; j < sz1; j++) + T.push_back(code[u][j]); + getnxt(); + ans = max(ans, find()); + } + return ans; +} + +void solve() +{ + int Max = 0; + ans.clear(); + for (int i = 0; i < n; i++) + { + int tmp = cal(i); + if (tmp > Max) + { + Max = tmp; + ans.clear(); + ans.push_back(i); + } + else if (tmp == Max) + ans.push_back(i); + } + cout << Max; + if (Max) + { + for (int i = 0; i < ans.size(); i++) + cout << " " << name[ans[i]]; + } + cout << endl; +} + +int main() +{ + while (cin >> n) + { + getchar(); + for (int i = 0; i < n; i++) + { + getline(cin, name[i]); + build(i); + } + build(n); + solve(); + } + return 0; +} diff --git a/11559.cpp b/11559.cpp index ee9d540..9a38a85 100644 --- a/11559.cpp +++ b/11559.cpp @@ -1,59 +1,37 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define INF 1<<30 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, B, H, W; - while (cin >> N >> B >> H >> W){ - bool stat = false; - int amt = INF; - while (H--){ - int price, bed = 0, mbed = 0, w; - cin >> price; - FOI(w, 1, W){ - cin >> bed; - mbed = max(mbed, bed); - } - if (mbed >= N){ - int V = N * price; - if (V <= B){ - amt = min(amt, V); - stat = true; +int INF = 1000000000; + +int main() +{ + int result; + int n, b, h, w; + int p, a; + while (cin >> n >> b >> h >> w) + { + result = INF; + for (int i = 0; i < h; i++) + { + cin >> p; + for (int j = 0; j < w; j++) + { + cin >> a; + if (a >= n && p * n < result) + { + result = p * n; } } } - if (stat) - cout << amt << endl; - else + if (result == INF || result > b) + { cout << "stay home" << endl; + } + else + { + cout << result << endl; + } } return 0; } diff --git a/11561.cpp b/11561.cpp new file mode 100644 index 0000000..109c530 --- /dev/null +++ b/11561.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +char maze[51][51]; +bool visited[51][51]; + +bool can_move(pair pos) +{ + return maze[pos.first - 1][pos.second] != 'T' && maze[pos.first + 1][pos.second] != 'T' && maze[pos.first][pos.second - 1] != 'T' && maze[pos.first][pos.second + 1] != 'T'; +} + +int solve(pair start) +{ + int result = 0; + pair tmp; + queue> q; + for (int i = 0; i < 51; i++) + { + for (int j = 0; j < 51; j++) + { + visited[i][j] = false; + } + } + q.push(start); + visited[start.first][start.second] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (maze[tmp.first][tmp.second] == 'G') + { + result++; + } + if (!can_move(tmp)) + { + continue; + } + if (!visited[tmp.first - 1][tmp.second] && maze[tmp.first - 1][tmp.second] != '#') + { + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + } + if (!visited[tmp.first + 1][tmp.second] && maze[tmp.first + 1][tmp.second] != '#') + { + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + } + if (!visited[tmp.first][tmp.second - 1] && maze[tmp.first][tmp.second - 1] != '#') + { + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + } + if (!visited[tmp.first][tmp.second + 1] && maze[tmp.first][tmp.second + 1] != '#') + { + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + } + } + return result; +} + +int main() +{ + int col, row; + pair start; + while (cin >> col >> row) + { + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + cin >> maze[i][j]; + if (maze[i][j] == 'P') + { + start.first = i; + start.second = j; + maze[i][j] = '.'; + } + } + } + cout << solve(start) << endl; + } + return 0; +} diff --git a/11562.cpp b/11562.cpp new file mode 100644 index 0000000..d9fabae --- /dev/null +++ b/11562.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double PI = acos(-1); +int sgn(double x) +{ + return (x < -eps) ? -1 : (x > eps); +} + +//2D PT +struct PT +{ + double x, y; + PT() {} + PT(double x, double y) : x(x), y(y) {} + PT operator-(const PT o) + { + return PT(x - o.x, y - o.y); + } + PT operator+(const PT o) + { + return PT(x + o.x, y + o.y); + } + PT operator*(double s) + { + return PT(x * s, y * s); + } + PT operator/(double s) + { + return PT(x / s, y / s); + } + bool operator<(const PT &o) const + { + return y < o.y - eps || (y < o.y + eps && x < o.x - eps); + } + bool operator==(const PT &o) const + { + return !sgn(y - o.y) && !sgn(x - o.x); + } + bool operator!=(const PT &o) const + { + return sgn(y - o.y) || sgn(x - o.x); + } + void rd() + { + scanf("%lf %lf", &x, &y); + } + double ang() + { + return atan2(y, x); + } +}; + +PT pp[205]; +double kk[205]; + +double work(PT p[], int n, double th, double r) +{ + PT oo(sin(th), cos(th)); + oo = oo * r; + for (int i = 0; i < n; i++) + { + kk[i] = (p[i] - oo).ang(); + } + sort(kk, kk + n); + if (kk[n - 1] - kk[0] < PI) + { + return kk[n - 1] - kk[0]; + } + for (int i = 1; i < n; i++) + if (kk[i] - kk[i - 1] > PI) + { + return PI * 2 - kk[i] + kk[i - 1]; + } + return 0; +} + +int main() +{ + //freopen("1.txt", "r", stdin); + srand(330597937); + int n, m; + while (cin >> n >> m) + { + for (int i = 0; i < n; i++) + { + pp[i].rd(); + } + double best = 0; + double ans = work(pp, n, best, m); + for (double step = 1; step > 1e-4; step /= 2) + { + for (int i = 1; i <= 100; i++) + { + double th = rand(); + double tmp = best + step * atan2(sin(th), cos(th)); + double now = work(pp, n, tmp, m); + if (now > ans) + { + ans = now, best = tmp; + } + } + } + printf("%.8f\n", ans); + } + return 0; +} diff --git a/11563.cpp b/11563.cpp new file mode 100644 index 0000000..f12c6da --- /dev/null +++ b/11563.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +struct obj +{ + int id, t; + + obj() {} + + obj(int _id, int _t) + { + id = _id; + t = _t; + } + + bool operator<(obj X) const + { + return t < X.t; + } +} aux; + +int main() +{ + int C, N, B, used, id[100000], last[100000], nxt[100000], ans; + bool in_cache[100000]; + while (scanf("%d %d %d", &C, &N, &B) == 3) + { + for (int i = 0; i < B; ++i) + { + scanf("%d", &id[i]); + } + for (int i = 0; i < N; ++i) + { + last[i] = B; + } + for (int i = B - 1; i >= 0; --i) + { + nxt[i] = last[id[i]]; + last[id[i]] = i; + } + memset(in_cache, false, sizeof(in_cache)); + priority_queue Q; + ans = used = 0; + for (int i = 0, x; i < B; ++i) + { + x = id[i]; + if (!in_cache[x]) + { + in_cache[x] = true; + ++ans; + if (used < C) + { + ++used; + } + else + { + aux = Q.top(); + Q.pop(); + in_cache[aux.id] = false; + } + } + Q.push(obj(x, nxt[i])); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11565.cpp b/11565.cpp new file mode 100644 index 0000000..f7e8bc7 --- /dev/null +++ b/11565.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int n; + int a, b, c; + cin >> n; + while (n--) + { + cin >> a >> b >> c; + bool found = false; + for (int i = -100; i <= 100; i++) + { + for (int j = -100; j <= 100; j++) + { + for (int k = -100; k <= 100; k++) + { + if (i != j && i != k && j != k && i + j + k == a && i * j * k == b && i * i + j * j + k * k == c) + { + found = true; + cout << i << " " << j << " " << k << endl; + i = j = 101; + break; + } + } + } + } + if (!found) + { + cout << "No solution." << endl; + } + } + return 0; +} diff --git a/11566.cpp b/11566.cpp new file mode 100644 index 0000000..6a3becf --- /dev/null +++ b/11566.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +/** + dp[choose a number?][charge of the entire...] - knapsack + Contact k by simply reversing the direction of the loop + if you want to limit the number of things you put. +*/ + +const int MAXN = 11; +const int MAXK = 101; +const int MAXX = 101; + +const int DPSIZE1 = 2 * MAXN + 1; +const int DPSIZE2 = MAXN * MAXX + 1; + +int N, x, T, K; +int dp[DPSIZE1][DPSIZE2]; +int pay[MAXK]; +int sum[MAXK]; + +int main() +{ + while (cin >> N >> x >> T >> K && (N | x | T | K)) + { + ++N; + for (int k = 0; k < K; ++k) + { + cin >> pay[k]; + sum[k] = 0; + for (int i = 0; i < N; ++i) + { + int f; + cin >> f; + sum[k] += f; + } + } + fill(dp[0], dp[DPSIZE1], 0); + for (int k = 0; k < K; ++k) + { + for (int i = N * 2; i >= 0; --i) + { + for (int j = 0; j <= N * x; ++j) + { + for (int m = 1; m <= 2; ++m) + { + if (i + m <= N * 2 && j + pay[k] * m <= N * x) + { + dp[i + m][j + pay[k] * m] = max(dp[i + m][j + pay[k] * m], + dp[i][j] + sum[k] * m); + } + } + } + } + } + + double res = 0; + for (int i = 0; i <= N * 2; ++i) + { + for (int j = 0; j <= N * x; j += N) + { + int p = (int)ceil((j / N + T) * 1.10); + if (p <= x) + { + res = max(res, (double)dp[i][j] / (double)N); + } + } + } + printf("%.2f\n", res); + } + return 0; +} diff --git a/11567.cpp b/11567.cpp new file mode 100644 index 0000000..3be1b46 --- /dev/null +++ b/11567.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +long long S; +int p[6] = {0, 1, 2, 3, 3, 4}; + +void solve() +{ + int step = 0; + while (S) + { + if (S < 6) + { + step += p[S]; + break; + } + if (S % 2 == 0) + { + S /= 2; + } + else + { + int dec = (S - 1) / 2; + if (dec % 2) + { + S++; + } + else + { + S--; + } + } + step++; + } + cout << step << endl; +} + +int main() +{ + while (cin >> S) + { + solve(); + } + return 0; +} diff --git a/11568.cpp b/11568.cpp new file mode 100644 index 0000000..e2f5ae1 --- /dev/null +++ b/11568.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +#define MAX 10 +#define REP(i, n) for (int i = 0; i < (int)n; i++) +#define EAST 0 +#define NORTH 1 +#define SOUTH 2 +#define WEST 3 + +static const int dx[5] = {1, 0, 0, -1, 0}; +static const int dy[5] = {0, -1, 1, 0, 0}; +static const char direction[5] = {'E', 'N', 'S', 'W', 'X'}; + +class Robot +{ +public: + int x, y, d; + + void rotate() + { + if (d == EAST) + d = SOUTH; + else if (d == NORTH) + d = EAST; + else if (d == WEST) + d = NORTH; + else if (d == SOUTH) + d = WEST; + } + + void move(int px, int py, int Tx, int Ty, char G[MAX][MAX], int color_row[MAX][MAX], int color_col[MAX][MAX]) + { + if (px == x && color_col[py][px] == color_col[y][px]) + { + if (py < y) + { + d = NORTH; + y--; + } + else + { + d = SOUTH; + y++; + } + } + else if (py == y && color_row[py][px] == color_row[py][x]) + { + if (px < x) + { + d = WEST; + x--; + } + else + { + d = EAST; + x++; + } + } + else + {// normal wondering + int nx = x + dx[d]; + int ny = y + dy[d]; + while (G[ny][nx] == '#' || ny == Ty && nx == Tx) + { + rotate(); + nx = x + dx[d]; + ny = y + dy[d]; + } + x = nx; + y = ny; + } + assert(G[y][x] != '#'); + } +}; + +class State +{ +public: + int x, y; + Robot R[2]; + string path; + + State() {} + State(int x, int y, Robot r1, Robot r2, string path) : x(x), y(y), path(path) + { + R[0] = r1; + R[1] = r2; + } + + bool operator<(const State &t) const + { + if (x != t.x) + return x < t.x; + if (y != t.y) + return y < t.y; + REP(r, 2) + { + if (R[r].x != t.R[r].x) + return R[r].x < t.R[r].x; + if (R[r].y != t.R[r].y) + return R[r].y < t.R[r].y; + if (R[r].d != t.R[r].d) + return R[r].d < t.R[r].d; + } + return false; + } +}; + +int N; +char G[MAX][MAX]; +int color_row[MAX][MAX], color_col[MAX][MAX]; + +int x, y, Tx, Ty; +Robot R[2]; + +map V; + +int bfs() +{ + queue Q; + V = map(); + + State s = State(x, y, R[0], R[1], ""); + + V[s] = true; + Q.push(s); + + State u, v; + int nx, ny; + string path; + + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + + if (u.x == Tx && u.y == Ty) + { + cout << u.path << endl; + return u.path.size(); + } + + for (int d = 0; d < 5; d++) + { + nx = u.x + dx[d]; + ny = u.y + dy[d]; + if (G[ny][nx] == '#' || nx == u.R[0].x && ny == u.R[0].y || nx == u.R[1].x && ny == u.R[1].y) + continue; + + v = u; + v.x = nx; + v.y = ny; + + REP(r, 2) + v.R[r].move(v.x, v.y, Tx, Ty, G, color_row, color_col); + + if (!(v.x == Tx && v.y == Ty) && (v.x == v.R[0].x && v.y == v.R[0].y || v.x == v.R[1].x && v.y == v.R[1].y)) + continue; + if (V[v]) + continue; + + V[v] = true; + v.path += direction[d]; + Q.push(v); + } + } + return -1; +} + +void compute() +{ + if (bfs() == -1) + cout << "You can't escape..." << endl; +} + +void setColor() +{ + REP(y, N) + REP(x, N) { color_row[y][x] = color_col[y][x] = -1; } + REP(y, N) + { + int color = 0; + REP(x, N) + { + if (G[y][x] == '#' || y == Ty && x == Tx) + { + color_row[y][x] = -1; + color++; + } + else + color_row[y][x] = color; + } + } + REP(x, N) + { + int color = 0; + REP(y, N) + { + if (G[y][x] == '#' || y == Ty && x == Tx) + { + color_col[y][x] = -1; + color++; + } + else + color_col[y][x] = color; + } + } +} + +int main() +{ + string line; + while (cin >> N && N) + { + getline(cin, line); + int nrobot = 0; + for (int i = 0; i < N; i++) + { + getline(cin, line); + for (int j = 0; j < N; j++) + { + G[i][j] = line[j]; + if (G[i][j] == 'X') + { + y = i; + x = j; + G[i][j] = ' '; + } + else if (G[i][j] == 'T') + { + Ty = i; + Tx = j; + G[i][j] = ' '; + } + else if (G[i][j] == '#' || G[i][j] == ' ') + { + } + else + { + R[nrobot].y = i; + R[nrobot].x = j; + if (G[i][j] == 'E') + R[nrobot].d = EAST; + else if (G[i][j] == 'N') + R[nrobot].d = NORTH; + else if (G[i][j] == 'S') + R[nrobot].d = SOUTH; + else if (G[i][j] == 'W') + R[nrobot].d = WEST; + G[i][j] = ' '; + nrobot++; + } + } + } + assert(nrobot == 2); + setColor(); + compute(); + } + return 0; +} diff --git a/11569.cpp b/11569.cpp new file mode 100644 index 0000000..92eef73 --- /dev/null +++ b/11569.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +const int MAXALPHA = 26; +const int MAXSIZE = 256; + +char S[MAXSIZE]; +int len[MAXALPHA + 1], cnt[MAXALPHA + 1]; + +int main() +{ + int numOfCase; + cin >> numOfCase; + cin.getline(S, MAXSIZE); + while (numOfCase--) + { + cin.getline(S, MAXSIZE); + int alpha[MAXALPHA + 5], i, j, size; + memset(alpha, 0, sizeof(alpha)); + alpha[0] = 1; + for (i = 0; S[i]; i++) + if (isupper(S[i])) + { + alpha[S[i] - 'A' + 1] = 1; + } + memset(len, 0, sizeof(len)); + memset(cnt, 0, sizeof(cnt)); + int v1, v2; + len[0] = cnt[0] = 1; + v1 = v2 = 0; + for (i = 1; i <= MAXALPHA; i++) + if (alpha[i]) + { + v2 = i; + for (j = 0; j < i; j++) + if (alpha[j]) + { + v1 = j; + if (5 * v1 <= 4 * v2) + { + if (len[v1] + 1 > len[v2]) + { + len[v2] = len[v1] + 1; + cnt[v2] = 0; + } + if (len[v1] + 1 == len[v2]) + { + cnt[v2] += cnt[v1]; + } + } + } + } + if (!v2) + { + cout << "0 0" << endl; + } + else + { + int maxlen = len[v2], ans = 0; + for (i = 1; i <= MAXALPHA; i++) + if (len[i] == maxlen) + { + ans += cnt[i]; + } + cout << maxlen - 1 << " " << ans << endl; + } + } + return 0; +} diff --git a/11570.cpp b/11570.cpp new file mode 100644 index 0000000..f48a33a --- /dev/null +++ b/11570.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +char D[17][17]; +int T[9][9]; +bool UR[9][10];// use j at row i +bool UC[9][10];// use j at col i +bool UB[9][10];// use j at box i + +void init() +{ + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 10; j++) + { + UR[i][j] = UC[i][j] = UB[i][j] = false; + } + } +} + +bool checkHorizontal(int left, char op, int right) +{ + if (op == '<') + return (left < right); + else + return (left > right); +} + +bool checkVertical(int top, char op, int bottom) +{ + if (op == '^') + return (top < bottom); + else + return (top > bottom); +} + +bool recursive(int pos) +{ + if (pos == 81) + { + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (j) + cout << " "; + cout << T[i][j]; + } + cout << endl; + } + return true; + } + + int i = pos / 9; + int j = pos % 9; + + for (int n = 1; n <= 9; n++) + { + if (UR[i][n] || UC[j][n] || UB[(i / 3) * 3 + (j / 3)][n]) + continue; + + if (j > 0 && j % 3 != 0 && !checkHorizontal(T[i][j - 1], D[i * 2][j * 2 - 1], n)) + continue; + if (i > 0 && i % 3 != 0 && !checkVertical(T[i - 1][j], D[i * 2 - 1][j * 2], n)) + continue; + + UR[i][n] = UC[j][n] = UB[(i / 3) * 3 + (j / 3)][n] = true; + T[i][j] = n; + if (recursive(pos + 1)) + return true; + UR[i][n] = UC[j][n] = UB[(i / 3) * 3 + (j / 3)][n] = false; + } + + return false; +} + +int main() +{ + int tcase = 0; + while (cin >> D[0][1]) + { + for (int j = 3; j <= 15; j += 2) + { + if (!(j == 5 || j == 11)) + cin >> D[0][j]; + } + for (int i = 1; i <= 16; i++) + { + if (i == 5 || i == 11) + continue; + if (i % 2 == 0) + { + for (int j = 1; j <= 15; j += 2) + { + if (!(j == 5 || j == 11)) + cin >> D[i][j]; + } + } + else + { + for (int j = 0; j <= 16; j += 2) + cin >> D[i][j]; + } + } + if (tcase++) + cout << endl; + init(); + recursive(0); + } + + return 0; +} diff --git a/11571.cpp b/11571.cpp new file mode 100644 index 0000000..b56c8a8 --- /dev/null +++ b/11571.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (int)n; i++) +#define all(c) (c).begin(), (c).end() +#define mp make_pair +#define pb push_back +#define each(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) +#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl + +typedef long long ll; +typedef vector vi; +typedef pair pi; +const int inf = (int)1e9; +const double INF = 1e12, EPS = 1e-9; + +ll A, B, C; +long double solve(long double lo, long double hi, int sig) +{ + rep(i, 200) + { + long double m = (lo + hi) / 2; + ((m * m * m - A * m * m + (1.0l * A * A - C) * m / 2 - B < 0) == (sig > 0) ? lo : hi) = m; + } + return lo; +} +int main() +{ + int cs; + cin >> cs; + while (cs--) + { + cin >> A >> B >> C; + if (3.0l * C < 0.9l * A * A) + { + cout << "No solution." << endl; + continue; + } + long double D = max((3.0l * C - 1.0l * A * A) * 0.5l, 0.0l); + long double a = (A - sqrt(D)) / 3, b = (A + sqrt(D)) / 3; + + ll x1 = (ll)round(solve(-INF, a, 1)); + ll x2 = (ll)round(solve(a, b, -1)); + ll x3 = (ll)round(solve(b, INF, 1)); + + if (x1 + x2 + x3 == A && x1 * x2 * x3 == B && x1 * x1 + x2 * x2 + x3 * x3 == C && + x1 < x2 && x2 < x3) + { + cout << x1 << " " << x2 << " " << x3 << endl; + } + else + cout << "No solution." << endl; + } + return 0; +} diff --git a/11572.cpp b/11572.cpp new file mode 100644 index 0000000..15c6911 --- /dev/null +++ b/11572.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11572.html +// Runtime: 0.416s +// Tag: Adhoc, set Window + +/* + * File: main.cpp + * Author: shahab + * Created on March 16, 2011, 8:17 PM + */ + +// @BEGIN_OF_SOURCE_CODE + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int main(int argc, char **argv) +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n, ar[N + 10], previous_position[N + 10]; + scanf("%d", &n); + map m; + int cnt = 0; + int maxi = 0; + for (int i = 1; i <= n; i++) + { + scanf("%d", &ar[i]); + previous_position[i] = m[ar[i]]; + m[ar[i]] = i; + } + int curr = 1; + int previous = 1; + for (curr = 1; curr <= n; curr++) + { + if (previous_position[curr] >= previous) + { + maxi = max(maxi, curr - previous); + previous = previous_position[curr] + 1; + } + } + maxi = max(maxi, curr - previous); + printf("%d\n", maxi); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11573.cpp b/11573.cpp new file mode 100644 index 0000000..32d3f8a --- /dev/null +++ b/11573.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +/* + Problem: + Andrés Mejía-Posada (andmej@gmail.com) +*/ + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +char mat[1001][1001]; +int d[1001][1001]; +int rows, cols; +int si, sj, ei, ej; + +int bfs() +{ + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + d[i][j] = INT_MAX >> 1; + } + } + d[si][sj] = 0; + deque> q; + q.push_front(make_pair(si, sj)); + while (q.size()) + { + int i = q.front().first, j = q.front().second, w = d[i][j]; + q.pop_front(); + if (w > d[i][j]) + { + continue; + } + if (i == ei && j == ej) + { + return w; + } + static const int di[] = {-1, -1, +0, +1, +1, +1, +0, -1}; + static const int dj[] = {+0, +1, +1, +1, +0, -1, -1, -1}; + for (int k = 0; k < 8; ++k) + { + int ni = i + di[k], nj = j + dj[k]; + if (0 <= ni && ni < rows && 0 <= nj && nj < cols) + { + int w_extra = mat[i][j] - '0' != k; + if (w + w_extra < d[ni][nj]) + { + d[ni][nj] = w + w_extra; + if (w_extra) + { + q.push_back(make_pair(ni, nj)); + } + else + { + q.push_front(make_pair(ni, nj)); + } + } + } + } + } + return -1; +} + +int main() +{ + cin >> rows >> cols; + for (int i = 0; i < rows; ++i) + { + for (int j = 0; j < cols; ++j) + { + cin >> mat[i][j]; + } + } + int n; + cin >> n; + while (n--) + { + cin >> si >> sj >> ei >> ej; + si--, sj--, ei--, ej--; + cout << bfs() << endl; + } + return 0; +} diff --git a/11574.cpp b/11574.cpp new file mode 100644 index 0000000..c4a542a --- /dev/null +++ b/11574.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +// +//#tag +//#sol + +// #include + +#define ll long long +#define ull unsigned long long +#define pii pair +#define pdd pair +#define F first +#define S second +#define REP(i, j, k) for (int(i) = (j); (i) < (k); ++(i)) +#define pb push_back +#define PI acos(-1) +#define db(x) cerr << #x << " = " << x << endl; +#define _ << ", " << +#define mp make_pair +#define FILL(x, y) memset(x, y, sizeof(x)) +#define EPS 1e-9 +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) + +// #define umap unordered_map + +template +inline string tostr(const _T &a) +{ + ostringstream os(""); + os << a; + return os.str(); +} + +int n; +double r; + +double x[1010]; +double y[1010]; +double d[1010]; +double s[1010]; + +int colision(int A, int B) +{ + double cx = x[B] - x[A]; + double cy = y[B] - y[A]; + double v1x = sin(d[A]) * s[A]; + double v1y = cos(d[A]) * s[A]; + double v2x = sin(d[B]) * s[B]; + double v2y = cos(d[B]) * s[B]; + double vx = v2x - v1x; + double vy = v2y - v1y; + // db( cx _ cy _ v1x _ v1y _ v2x _ v2y ); + double deno = 2 * (vx * vx) + 2 * (vy * vy); + + double nomi = -1.0 * (2.0 * cx * vx + 2.0 * cy * vy); + double t = nomi / deno; + + double ox = cx + vx * t; + double oy = cy + vy * t; + double dist = sqrt(ox * ox + oy * oy); + + if (fabs(deno) < EPS || t < EPS) + { + double dist = sqrt(cx * cx + cy * cy); + if (dist <= r + EPS) + { + return 0; + } + else + { + return 1000000000; + } + } + if (dist <= r + EPS) + { + // binary search from 0 to t + double ini = 0, end = t, mid = 0; + int cont = 100; + while (cont--) + { + mid = (ini + end) / 2.0; + double ox = cx + vx * mid; + double oy = cy + vy * mid; + double dist = sqrt(ox * ox + oy * oy); + if (dist < r + EPS) + { + end = mid; + } + else + { + ini = mid; + } + } + return round(mid); + } + else + { + return 1000000000; + } +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + cin >> n >> r; + REP(i, 0, n) + { + scanf("%lf%lf%lf%lf", &x[i], &y[i], &d[i], &s[i]); + d[i] *= PI / 180.0; + } + int resp = 1000000000; + REP(i, 0, n) + { + REP(j, i + 1, n) + { + resp = min(resp, colision(i, j)); + } + } + if (resp == 1000000000) + { + puts("No collision."); + } + else + { + printf("%d\n", resp); + } + } + return 0; +} diff --git a/11575.cpp b/11575.cpp new file mode 100644 index 0000000..9cfd6ff --- /dev/null +++ b/11575.cpp @@ -0,0 +1,284 @@ +#include + +using namespace std; + +typedef long long ll; +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FILL(x, v) memset(x, v, sizeof(x)) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) +int N; +#define IN(x) ((x) >= 1 && (x) <= N) +#define MP0 make_pair(0, 0) +#define MAXN 22555 +const int INF = (int)1E9; +int atk1, atk2, def1, def2; +int n1, n2; +struct zerg +{ + int hp; + int x, y, a; + zerg() {} + zerg(int _x, int _y) + { + hp = 35; + x = _x; + y = _y; + a = 0; + } +} z1[MAXN], z2[MAXN]; + +char gd[200]; +pair fd[200][200], fdnxt[200][200], from[200][200]; +int dst1[200][200], dst2[200][200]; + +bool v[200][200]; +struct dij +{ + int x, y, d; + dij(int _x, int _y, int _d) + { + x = _x; + y = _y; + d = _d; + } +}; + +int dir8[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1}; +int dir4[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; + +void repack(int x, int y, int thatg, int thatid) +{ + if (fdnxt[x][y].first == 0) + { + zerg *z = thatg == 1 ? z1 : z2; + z[thatid].x = x; + z[thatid].y = y; + fdnxt[x][y] = make_pair(thatg, thatid); + //from[x][y] = MP0; + return; + } + //if(from[x][y]==MP0) return; + repack(from[x][y].first, from[x][y].second, fdnxt[x][y].first, fdnxt[x][y].second); + from[x][y] = MP0; + fdnxt[x][y] = make_pair(thatg, thatid); + zerg *z = thatg == 1 ? z1 : z2; + z[thatid].x = x; + z[thatid].y = y; +} + +void bfs(zerg *z, int n, int dst[200][200]) +{ + //REP(i,1,N+1) REP(j,1,N+1) dst[i][j] = INF; + FILL(v, 0); + queue que; + REP(i, 0, n) + { + que.push(dij(z[i].x, z[i].y, 0)); + v[z[i].x][z[i].y] = 1; + dst[z[i].x][z[i].y] = 0; + } + while (!que.empty()) + { + dij now = que.front(); + que.pop(); + REP(i, 0, 4) + { + int nx = now.x + dir4[i][0], + ny = now.y + dir4[i][1]; + if (IN(nx) && IN(ny) && !v[nx][ny]) + { + dst[nx][ny] = now.d + 1; + v[nx][ny] = 1; + que.push(dij(nx, ny, now.d + 1)); + } + } + } +} + +int main() +{ + bool fst = 1; + while (1) + { + scanf("%d", &N); + if (!N) + break; + + if (fst) + fst = 0; + else + puts(""); + + scanf("%d%d%d%d", &atk1, &def1, &atk2, &def2); + n1 = n2 = 0; + + FILL(fd, 0); + + REP(i, 1, N + 1) + { + scanf("%s", &gd[1]); + REP(j, 1, N + 1) + { + if (gd[j] == '1') + { + fd[i][j] = make_pair(1, n1); + z1[n1++] = zerg(i, j); + } + else if (gd[j] == '2') + { + fd[i][j] = make_pair(2, n2); + z2[n2++] = zerg(i, j); + } + } + } + int T; + scanf("%d", &T); + REP(t, 1, T + 1) + { + REP(i, 0, n1) + z1[i].a = 0; + REP(i, 0, n2) + z2[i].a = 0; + // atk zerg1 + REP(i, 0, n1) + { + int x = z1[i].x, y = z1[i].y; + REP(d, 0, 8) + { + int nx = x + dir8[d][0], ny = y + dir8[d][1]; + if (fd[nx][ny].first == 2) + { + int id = fd[nx][ny].second; + z2[id].hp -= 5 + atk1 - def2; + z1[i].a = 1; + break; + } + } + } + // atk zerg2 + REP(i, 0, n2) + { + int x = z2[i].x, y = z2[i].y; + REP(d, 0, 8) + { + int nx = x + dir8[d][0], ny = y + dir8[d][1]; + if (fd[nx][ny].first == 1) + { + int id = fd[nx][ny].second; + z1[id].hp -= 5 + atk2 - def1; + z2[i].a = 1; + break; + } + } + } + + bfs(z2, n2, dst1); + bfs(z1, n1, dst2); + + // remove dead zerg1 + REP(i, 0, n1) + { + int x = z1[i].x, y = z1[i].y; + if (z1[i].hp <= 0) + { + fd[x][y] = MP0; + + int x2 = z1[n1 - 1].x, y2 = z1[n1 - 1].y; + fd[x2][y2].second = i; + + swap(z1[i], z1[n1 - 1]); + n1--; + i--; + } + } + // remove dead zerg2 + REP(i, 0, n2) + { + int x = z2[i].x, y = z2[i].y; + if (z2[i].hp <= 0) + { + fd[x][y] = MP0; + + int x2 = z2[n2 - 1].x, y2 = z2[n2 - 1].y; + fd[x2][y2].second = i; + + swap(z2[i], z2[n2 - 1]); + n2--; + i--; + } + } + + FILL(fdnxt, 0); + FILL(from, 0); + REP(i, 0, n1) + if (z1[i].a == 1) fdnxt[z1[i].x][z1[i].y] = make_pair(1, i); + REP(i, 0, n2) + if (z2[i].a == 1) fdnxt[z2[i].x][z2[i].y] = make_pair(2, i); + REP(i, 0, n1) + if (z1[i].hp < 35) z1[i].hp++; + REP(i, 0, n2) + if (z2[i].hp < 35) z2[i].hp++; + + // move zerg + REP(i, 1, N + 1) + { + REP(j, 1, N + 1) + { + int g = fd[i][j].first, + id = fd[i][j].second; + if (g == 0) + continue; + zerg *z = g == 1 ? z1 : z2; + if (z[id].a) + continue; + int x = z[id].x, y = z[id].y; + + int ad = -1, mind = INF; + REP(d, 0, 8) + { + int nx = x + dir8[d][0], + ny = y + dir8[d][1]; + if (IN(nx) && IN(ny) && (g == 1 ? dst1[nx][ny] : dst2[nx][ny]) < mind) + { + mind = g == 1 ? dst1[nx][ny] : dst2[nx][ny]; + ad = d; + } + } + int nx = x + dir8[ad][0], + ny = y + dir8[ad][1]; + + if (fdnxt[nx][ny].first == 0) + { + fdnxt[nx][ny] = make_pair(g, id); + from[nx][ny] = make_pair(x, y); + z[id].x = nx; + z[id].y = ny; + } + else + { + // someone else moves here, not move + repack(x, y, g, id); + } + } + } + memcpy(fd, fdnxt, sizeof(fd)); + if (n1 == 0 || n2 == 0) + break; + } + + REP(i, 1, N + 1) + { + REP(j, 1, N + 1) + { + if (fd[i][j].first) + { + printf("%d", fd[i][j].first); + } + else + printf("."); + } + puts(""); + } + } + return 0; +} diff --git a/11576.cpp b/11576.cpp new file mode 100644 index 0000000..72108cb --- /dev/null +++ b/11576.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11576.html +// Runtime: 0.024s +// Tag: Adhoc + +/* + * File: main.cpp + * Author: shahab + * Created on March 16, 2011, 7:16 PM + */ + +// @BEGIN_OF_SOURCE_CODE + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) + +string str; +string total; +int k, w; + +bool isMatch(int pos) +{ + for (int i = pos; i < total.size(); i++) + { + if (total[i] != str[i - pos]) + { + return false; + } + } + return true; +} + +void minimize() +{ + int pos = total.size() - k; + for (int i = pos; i < total.size(); i++) + { + if (isMatch(i)) + { + for (int j = total.size() - i; j < str.size(); j++) + { + total += str[j]; + } + return; + } + } + total += str; +} + +int main(int argc, char **argv) +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + scanf("%d %d", &k, &w); + total = ""; + For(i, 0, w) + { + cin >> str; + minimize(); + } + cout << total.size() << endl; + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11577.cpp b/11577.cpp index 6b78a1b..1eda363 100644 --- a/11577.cpp +++ b/11577.cpp @@ -1,86 +1,40 @@ -#include -using namespace std; - -int main(){ - char alph[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; - int t; - cin>>t; - for(int i=0;i<=t;i++){ - char ch[250]={'\0'}; - cin.getline(ch,200); - int freq[26]={0}; - int len=sizeof(ch); - for(int j=0;jmax) - max=freq[j]; - } - if(i!=0){ - for(int j=0;j<26;j++){ - if(freq[j]==max) - cout< + +using namespace std; + +int main() +{ + int cases; + int best; + int count[26]; + string input; + cin >> cases; + cin.ignore(100, '\n'); + while (cases--) + { + getline(cin, input); + best = 0; + fill(count, count + 26, 0); + transform(input.begin(), input.end(), input.begin(), ::tolower); + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= 'a' && input[i] <= 'z') + { + count[input[i] - 'a']++; + if (count[input[i] - 'a'] > best) + { + best = count[input[i] - 'a']; + } + } + } + for (int i = 0; i < 26; i++) + { + if (count[i] == best) + { + cout << char('a' + i); + } + } + cout << endl; + } + return 0; +} diff --git a/11578.cpp b/11578.cpp new file mode 100644 index 0000000..cecb0d5 --- /dev/null +++ b/11578.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define min(a, b) ((a) < (b) ? (a) : (b)) +const int N = 10005; +int t, n, i, j, k, dp[N][5][5], ans, an[N]; +struct Stu +{ + int t, l, id; +} s[N]; + +struct Out +{ + int n, l, r, out1, out2; +} out[N][5][5]; + +bool cmpt(Stu a, Stu b) +{ + return a.t < b.t; +} + +bool cmpid(Stu a, Stu b) +{ + return a.id < b.id; +} + +void print(int n, int l, int r) +{ + Out next = out[n][l][r]; + if (n == 0) + { + return; + } + if (next.out2 != -1) + { + an[s[n - 1].id] = next.out1; + an[s[n].id] = next.out2; + } + else + { + an[s[n].id] = next.out1; + } + print(next.n, next.l, next.r); +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + ans = INF; + memset(dp, INF, sizeof(dp)); + dp[0][0][0] = 0; + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + scanf("%d%d", &s[i].t, &s[i].l); + s[i].l = s[i].l / 10 - 1; + s[i].id = i; + } + sort(s + 1, s + n + 1, cmpt); + for (i = 1; i <= n; i++) + { + int tmp1 = s[i].l; + if (i == n || s[i].t != s[i + 1].t) + { + for (j = 0; j < 5; j++) + { + for (k = 0; k < 5; k++) + { + if (dp[i][tmp1][k] > dp[i - 1][j][k] + abs(tmp1 - j) * 10) + { + dp[i][tmp1][k] = dp[i - 1][j][k] + abs(tmp1 - j) * 10; + out[i][tmp1][k].l = j; + out[i][tmp1][k].r = k; + out[i][tmp1][k].n = i - 1; + out[i][tmp1][k].out1 = 1; + out[i][tmp1][k].out2 = -1; + } + if (dp[i][j][tmp1] > dp[i - 1][j][k] + abs(tmp1 - k) * 10) + { + dp[i][j][tmp1] = dp[i - 1][j][k] + abs(tmp1 - k) * 10; + out[i][j][tmp1].l = j; + out[i][j][tmp1].r = k; + out[i][j][tmp1].n = i - 1; + out[i][j][tmp1].out1 = 2; + out[i][j][tmp1].out2 = -1; + } + } + } + } + else + { + int tmp2 = s[i + 1].l; + for (j = 0; j < 5; j++) + { + for (k = 0; k < 5; k++) + { + if (dp[i + 1][tmp1][tmp2] > dp[i - 1][j][k] + abs(tmp1 - j) * 10 + abs(tmp2 - k) * 10) + { + dp[i + 1][tmp1][tmp2] = dp[i - 1][j][k] + abs(tmp1 - j) * 10 + abs(tmp2 - k) * 10; + out[i + 1][tmp1][tmp2].l = j; + out[i + 1][tmp1][tmp2].r = k; + out[i + 1][tmp1][tmp2].n = i - 1; + out[i + 1][tmp1][tmp2].out1 = 1; + out[i + 1][tmp1][tmp2].out2 = 2; + } + if (dp[i + 1][tmp2][tmp1] > dp[i - 1][j][k] + abs(tmp2 - j) * 10 + abs(tmp1 - k) * 10) + { + dp[i + 1][tmp2][tmp1] = dp[i - 1][j][k] + abs(tmp2 - j) * 10 + abs(tmp1 - k) * 10; + out[i + 1][tmp2][tmp1].l = j; + out[i + 1][tmp2][tmp1].r = k; + out[i + 1][tmp2][tmp1].n = i - 1; + out[i + 1][tmp2][tmp1].out1 = 2; + out[i + 1][tmp2][tmp1].out2 = 1; + } + } + } + i++; + } + } + int lv, rv; + for (j = 0; j < 5; j++) + { + for (k = 0; k < 5; k++) + { + if (ans > dp[n][j][k] + j * 10 + k * 10) + { + ans = dp[n][j][k] + j * 10 + k * 10; + lv = j; + rv = k; + } + } + } + printf("%d\n", ans + 15 * n); + print(n, lv, rv); + for (i = 1; i <= n; i++) + { + printf("%d\n", an[i]); + } + } + return 0; +} diff --git a/11579.cpp b/11579.cpp new file mode 100644 index 0000000..df8c954 --- /dev/null +++ b/11579.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/115/11579.html +// Runtime: 0.164s +// Tag: triangle, geometry + +/* + * File: main.cpp + * Author: shahab + * Created on March 23, 2011, 12:26 PM + */ + +// @BEGIN_OF_SOURCE_CODE + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) + +double area(double x, double y, double z) +{ + if (x + y < z || x + z < y || y + z < x) + { + return 0; + } + double area = x + y + z; + area /= 2; + return sqrt(area * (area - x) * (area - y) * (area - z)); +} + +int main(int argc, char **argv) +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n; + scanf("%d", &n); + double s[10000 + 10]; + For(i, 0, n) scanf("%lf", &s[i]); + sort(s, s + n); + double ret = 0; + for (int i = n - 1; i >= 2; i--) + { + ret = max(ret, area(s[i], s[i - 1], s[i - 2])); + } + printf("%.2lf\n", ret); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/1158.cpp b/1158.cpp new file mode 100644 index 0000000..a4b46e7 --- /dev/null +++ b/1158.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +//1158 +//CubesSquared +//Dynamic Programming;Knapsack;Infinite Items Knapsack + +int K[400001]; +vector W; + +int main() +{ + for (int i = 1; i * i * i <= 400000; i++) + { + W.push_back(i * i * i); + } + for (int a = 1, i = 1; a <= 400000; i++, a += i * i) + { + W.push_back(a); + } + memset(K, 0x3f, sizeof(K)); + K[0] = 0; + for (int i = 0; i < W.size(); i++) + for (int j = W[i]; j <= 400000; j++) + { + K[j] = min(K[j], K[j - W[i]] + 1); + } + int n; + while (cin >> n, n != -1) + { + cout << K[n] << endl; + } + return 0; +} diff --git a/11581.cpp b/11581.cpp index fc19ec8..edceb2e 100644 --- a/11581.cpp +++ b/11581.cpp @@ -1,92 +1,96 @@ -#include -#include -using namespace std; - -int board_value(int board[3][3]) { - int tmp = 0; +#include - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - tmp = tmp * 10 + board[i][j]; - } - } +using namespace std; - return tmp; +int board_value(int board[3][3]) +{ + int tmp = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + tmp = tmp * 10 + board[i][j]; + } + } + return tmp; } -void visit(int board[3][3], map& visited) { - visited[board_value(board)] = true; +void visit(int board[3][3], map &visited) +{ + visited[board_value(board)] = true; } -void mutate(int board[3][3]) { - int tmp[3][3], sum; - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - sum = 0; - - if (i > 0) { - sum += board[i-1][j]; - } - - if (j > 0) { - sum += board[i][j-1]; - } - - if (i < 2) { - sum += board[i+1][j]; - } - - if (j < 2) { - sum += board[i][j+1]; - } - - tmp[i][j] = sum % 2; - } - } - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - board[i][j] = tmp[i][j]; - } - } +void mutate(int board[3][3]) +{ + int tmp[3][3], sum; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + sum = 0; + if (i > 0) + { + sum += board[i - 1][j]; + } + if (j > 0) + { + sum += board[i][j - 1]; + } + if (i < 2) + { + sum += board[i + 1][j]; + } + if (j < 2) + { + sum += board[i][j + 1]; + } + tmp[i][j] = sum % 2; + } + } + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + board[i][j] = tmp[i][j]; + } + } } -int main(void) +int main() { - int t, index, tmp; - int board[3][3]; - map visited; - - cin >> t; - - while (t--) { - index = -1; - visited.clear(); - - for (int i = 0; i < 3; i++) { - cin >> tmp; - for (int j = 2; j >= 0; j--) { - board[i][j] = tmp % 10; - tmp /= 10; - } - } - - visit(board, visited); - index++; - - while (true) { - mutate(board); - - if (visited.find(board_value(board)) == visited.end()) { - visit(board, visited); - index++; - } else { - cout << index - 1 << endl; - break; - } - } - } - - return 0; + int t, index, tmp; + int board[3][3]; + map visited; + cin >> t; + while (t--) + { + index = -1; + visited.clear(); + for (int i = 0; i < 3; i++) + { + cin >> tmp; + for (int j = 2; j >= 0; j--) + { + board[i][j] = tmp % 10; + tmp /= 10; + } + } + visit(board, visited); + index++; + while (true) + { + mutate(board); + if (visited.find(board_value(board)) == visited.end()) + { + visit(board, visited); + index++; + } + else + { + cout << index - 1 << endl; + break; + } + } + } + return 0; } diff --git a/11582.cpp b/11582.cpp new file mode 100644 index 0000000..d80af6d --- /dev/null +++ b/11582.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +typedef unsigned long long int ullint; + +ullint A, B, M, MOD; + +vector rem; + +void ini() +{ + rem.clear(); +} + +int find_cycle() +{ + rem.push_back(0); + rem.push_back(1); + while (1) + { + int b1 = rem[rem.size() - 2]; + int b2 = rem[rem.size() - 1]; + int b3 = (b1 + b2) % M; + if (b3 == 0 && b2 == 1) + { + break; + } + rem.push_back(b3); + } + return rem.size(); +} + +ullint SQ(ullint in) +{ + return in * in; +} + +ullint bigmod(ullint A, ullint B, ullint MOD) +{ + if (B == 0) + { + return 1; + } + if (B % 2 == 0) + { + return SQ(bigmod(A, B / 2, MOD) % MOD) % MOD; + } + return A * bigmod(A, B - 1, MOD) % MOD; +} + +int process() +{ + if (M == 1) + { + return 0; + } + MOD = find_cycle(); + return rem[bigmod(A % MOD, B, MOD)]; +} + +int main() +{ + int datacase; + scanf("%d", &datacase); + while (datacase--) + { + ini(); + cin >> A >> B >> M; + cout << process() << endl; + } + return 0; +} diff --git a/11583.cpp b/11583.cpp new file mode 100644 index 0000000..7347e22 --- /dev/null +++ b/11583.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int t, n; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + int i, j, Ans = 0, letter, tmp; + char DNA[27]; + letter = (1 << 27) - 1; + for (i = 0; i < n; i++) + { + scanf("%s", DNA); + for (j = 0, tmp = 0; DNA[j]; j++) + { + tmp |= 1 << (DNA[j] - 'a'); + } + if (tmp & letter) + { + letter &= tmp; + } + else + { + Ans++, letter = tmp; + } + } + printf("%d\n", Ans); + } + return 0; +} diff --git a/11584.cpp b/11584.cpp new file mode 100644 index 0000000..92ee8df --- /dev/null +++ b/11584.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +/* + Problem: + Andrés Mejía-Posada (andmej@gmail.com) + */ + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +int dp[1005]; +bool good[1005][1005]; + +int main() +{ + int cases; + cin >> cases; + string s; + while (cases--) + { + cin >> s; + s = " " + s; + int n = s.size(); + for (int i = 0; i < n; ++i) + { + good[i][i] = true; + if (i + 1 < n) + { + good[i][i + 1] = s[i] == s[i + 1]; + } + } + for (int d = 2; d <= n; ++d) + { + for (int i = 0, j; (j = i + d) < n; ++i) + { + good[i][j] = good[i + 1][j - 1] && (s[i] == s[j]); + } + } + dp[0] = 0; + for (int i = 1; i < n; ++i) + { + dp[i] = n + 5; + string t = ""; + for (int j = i; j > 0; --j) + { + t += s[j]; + if (good[j][i]) + { + dp[i] = min(dp[i], dp[j - 1] + 1); + } + } + } + cout << dp[n - 1] << endl; + } + return 0; +} diff --git a/11585.cpp b/11585.cpp new file mode 100644 index 0000000..f6fdbf1 --- /dev/null +++ b/11585.cpp @@ -0,0 +1,268 @@ +#include + +using namespace std; + +int g[205][205], ug[205][205]; +char pattern[205][205]; +int cond1(int x, int y, int r, int c, int cellcnt) +{ + /* + Any two shaded spaces are connected by some sequence + of adjacent shaded spaces. Two spaces are called + adjacent if they share a side. +*/ + int dx[] = {0, 0, 1, -1}; + int dy[] = {1, -1, 0, 0}; + queue X, Y; + int i, tx, ty; + char used[205][205] = {}; + X.push(x), Y.push(y); + used[x][y] = 1; + cellcnt--; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + for (i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= r || ty >= c) + continue; + if (used[tx][ty] == 0 && pattern[tx][ty] == '#') + { + used[tx][ty] = 1; + cellcnt--; + if (cellcnt < 0) + return 0; + X.push(tx), Y.push(ty); + } + } + } + return cellcnt == 0; +} +int cond2(int x, int y, int r, int c) +{ + /* + For each of the unshaded spaces b, let Wb be the + collection of all unshaded spaces that can be + reached from b by a sequence of adjacent unshaded + spaces. Then, Wb contains exactly one numbered + space and that number is exactly the number of + spaces in Wb. +*/ + int dx[] = {0, 0, 1, -1}; + int dy[] = {1, -1, 0, 0}; + queue X, Y; + int Wb = g[x][y], i, tx, ty; + char used[205][205] = {}; + if (pattern[x][y] == '.') + { + X.push(x), Y.push(y); + used[x][y] = 1; + } + else + return 0; + Wb--; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + for (i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= r || ty >= c) + continue; + if (used[tx][ty] == 0 && pattern[tx][ty] == '.') + { + // if(ug[tx][ty]) return 0; // other Wb + used[tx][ty] = 1; + Wb--; + if (Wb < 0) + return 0; + X.push(tx), Y.push(ty); + } + } + } + return Wb == 0; +} +int cond3(int x, int y, int r, int c) +{ + /* + For any unshaded space b, there is a sequence + of unshaded spaces starting at b and ending + at a space on the edge of the grid where + consecutive spaces in this sequence share a + side or a corner. +*/ + int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}; + int dy[] = {1, -1, 0, 1, -1, 0, 1, -1}; + queue X, Y; + int i, tx, ty; + char used[205][205] = {}; + if (pattern[x][y] != '.') + return 1; + X.push(x), Y.push(y); + used[x][y] = 1; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + if (x == 0 || x == r - 1 || y == 0 || y == c - 1) + return 1; + for (i = 0; i < 8; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= r || ty >= c) + continue; + if (used[tx][ty] == 0 && pattern[tx][ty] == '.') + { + used[tx][ty] = 1; + X.push(tx), Y.push(ty); + } + } + } + return 0; +} +int cond4(int r, int c) +{ + int i, j; + for (i = 0; i < r - 1; i++) + { + for (j = 0; j < c - 1; j++) + { + bool flag = (pattern[i][j] == '.' || pattern[i][j + 1] == '.' || + pattern[i + 1][j] == '.' || pattern[i + 1][j + 1] == '.'); + if (flag == false) + return 0; + } + } + return 1; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int testcase; + int r, c, n; + int x, y, v, i, j, k; + scanf("%d", &testcase); + while (testcase--) + { + if (scanf("%d %d %d", &r, &c, &n) != 3) + return 0; + memset(g, 0, sizeof(g)); + memset(ug, 0, sizeof(ug)); + int tot = 0; + int ret = 1; + for (i = 0; i < n; i++) + { + scanf("%d %d %d", &x, &y, &v); + g[x][y] = v; + ug[x][y] = 1; + tot += v; + } + for (i = 0; i < r; i++) + scanf("%s", &pattern[i]); + int cellcnt = 0; + int unshaded = 0; + for (i = 0; i < r; i++) + for (j = 0; j < c; j++) + if (pattern[i][j] == '#') + cellcnt++, x = i, y = j; + else + unshaded++; + if (tot != unshaded) + { + puts("not solved"); + continue; + } + if (cellcnt && !cond1(x, y, r, c, cellcnt)) + { + puts("not solved"); + continue; + } + for (i = 0; i < r && ret; i++) + { + for (j = 0; j < c && ret; j++) + { + if (ug[i][j]) + { + ret &= cond2(i, j, r, c); + } + } + } + if (!ret) + { + puts("not solved"); + continue; + } + for (i = 0; i < r && ret; i++) + { + for (j = 0; j < c && ret; j++) + { + if (ug[i][j]) + { + ret &= cond3(i, j, r, c); + } + } + } + if (!ret) + { + puts("not solved"); + continue; + } + ret &= cond4(r, c); + if (!ret) + { + puts("not solved"); + continue; + } + puts("solved"); + } + return 0; +} +/* +5 + +5 5 6 +0 0 3 +0 2 1 +0 4 2 +2 2 1 +3 4 2 +4 0 2 +.#.#. +.###. +.#.## +###.. +..### + +5 5 6 +0 0 3 +0 2 1 +0 4 2 +2 2 1 +3 4 3 +4 0 2 +.#.#. +.###. +.#.## +####. +..#.. + +2 3 1 +0 0 2 +.## +.## + +2 2 1 +0 0 1 +.. +## + +2 2 2 +0 0 1 +1 1 1 +.# +#. +*/ diff --git a/11586.cpp b/11586.cpp new file mode 100644 index 0000000..98fbc99 --- /dev/null +++ b/11586.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int main() +{ + int n; + string input; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, input); + vector pieces; + istringstream iss(input); + copy(istream_iterator(iss), istream_iterator(), back_inserter(pieces)); + int sz = pieces.size(); + bool taken[sz]; + if (sz == 1) + { + cout << "NO LOOP" << endl; + continue; + } + for (int i = 0; i < sz; i++) + { + taken[i] = false; + } + taken[0] = true; + char first = pieces[0][0], last = pieces[0][1]; + int count = 1; + while (true) + { + bool action_taken = false; + for (int i = 0; i < sz; i++) + { + if (taken[i]) + { + continue; + } + if (pieces[i][0] == 'F' && last == 'M' || pieces[i][0] == 'M' && last == 'F') + { + taken[i] = true; + action_taken = true; + last = pieces[i][1]; + count++; + } + } + if (!action_taken) + { + break; + } + } + bool result = true; + if (count != sz || first == last) + { + result = false; + } + if (result) + { + cout << "LOOP" << endl; + } + else + { + cout << "NO LOOP" << endl; + } + } + return 0; +} diff --git a/11587.cpp b/11587.cpp new file mode 100644 index 0000000..ae12351 --- /dev/null +++ b/11587.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define FORI(i, s, n) for (int i = s; i < n; ++i) + +int ans[21], cnt[128], L[128], ls, test[21]; +bool must[21], valid[21]; +char desc[128]; +int count_in_sol, len, marked_wins, min_card, needed; + +void put(int x, int dd, int n) +{ + test[n] = x; + for (int i = 0; i < ls && L[i] + x < len; ++i) + { + marked_wins += !cnt[x + L[i]]; + cnt[x + L[i]] += dd; + marked_wins -= !cnt[x + L[i]]; + } +} + +void bt(int n, int idx) +{ + if (marked_wins == needed && n < min_card) + { + min_card = n; + FORI(i, 0, n) + ans[i] = test[i]; + return; + } + if (count_in_sol >= min_card) + { + return; + } + FORI(i, idx, 21) + if (valid[i]) + { + put(i, 1, n); + bt(n + 1, i + 1); + put(i, -1, n); + } +} + +int main() +{ + int j, t; + scanf("%d", &t); + for (int cnum = 1; t--; ++cnum) + { + desc[0] = 'L'; + ls = 0; + min_card = 21; + scanf("%s", desc + 1); + for (len = 0; desc[len]; ++len) + { + if (desc[len] == 'L') + { + L[ls++] = len; + } + cnt[len] = 0; + } + needed = len - ls; + // Mark which numbers are valid + FORI(i, 1, 21) + { + must[i] = 0; + valid[i] = i < len; + for (j = 0; valid[i] && j < ls && L[j] + i < len; ++j) + { + valid[i] &= desc[i + L[j]] == 'W'; + } + if (valid[i]) + { + put(i, 1, 0); + } + } + // Find which numbers must be in the set + FORI(i, 1, 21) + if (valid[i]) for (j = 0; j < ls && L[j] + i < len; ++j) + { + must[i] |= cnt[i + L[j]] == 1; + } + FORI(i, 0, len) + cnt[i] = 0; + count_in_sol = marked_wins = 0; + // Insert numbers that must be in the set + FORI(i, 1, 21) + if (must[i]) + { + put(i, 1, count_in_sol++), valid[i] = false; + } + // Backtrack to find the remaining ones + bt(count_in_sol, 1); + // Show answer + printf("Case %d:", cnum); + if (needed == 0) + { + ans[0] = len, min_card = 1; + } + std::sort(ans, ans + min_card); + FORI(i, 0, min_card) + printf(" %d", ans[i]); + puts(""); + } + return 0; +} diff --git a/11588.cpp b/11588.cpp index 71af9cc..7f9e1a3 100644 --- a/11588.cpp +++ b/11588.cpp @@ -1,87 +1,52 @@ -#include -using namespace std; - -int main(){ - int X; - cin>>X; - for(int i=1;i<=X;i++){ - int R,C,M,N; - char img[500]={'\0'}; - int freq[26]={0}; - cin>>R>>C>>M>>N; - int T=R*C; - for(int j=0;j>img[j]; - for(int j=0;jmax) - max=freq[j]; - } - int bytes=0; - for(int j=0;j<26;j++){ - if(freq[j]==max) - bytes+=(freq[j]*M); - else - bytes+=(freq[j]*N); - } - cout<<"Case "< + +using namespace std; + +int main() +{ + int t; + int r, c, m, n; + char ch; + map count; + int best; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + count.clear(); + best = 0; + cin >> r >> c >> m >> n; + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + cin >> ch; + if (count.find(ch) == count.end()) + { + count[ch] = 1; + } + else + { + count[ch] += 1; + } + if (count[ch] > best) + { + best = count[ch]; + } + } + } + int important = 0; + for (ch = 'A'; ch <= 'Z'; ch++) + { + if (count.find(ch) == count.end()) + { + continue; + } + if (count[ch] == best) + { + important++; + } + } + cout << "Case " << casenum + 1 << ": " << m * best * important + n * (c * r - best * important) << endl; + } + return 0; +} diff --git a/11589.cpp b/11589.cpp new file mode 100644 index 0000000..c8dbba2 --- /dev/null +++ b/11589.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +const int N = 2505; +int n, q, h1, m1, h2, m2, time1, time2, vis[20][20][20][105], tim; +struct Point +{ + int x, y, z; + Point() {} + void scan() + { + scanf("%d%d%d", &x, &y, &z); + } +} p, s, e, v; + +bool judge(int x, int y, int z, int ti) +{ + int i, j, k, l; + for (i = x; i < x + v.x; i++) + { + for (j = y; j < y + v.y; j++) + { + for (k = z; k < z + v.z; k++) + { + for (l = ti; l < ti + tim; l++) + { + if (vis[i][j][k][l]) + { + return false; + } + } + } + } + } + return true; +} + +int solve() +{ + int ans = 0, i, j, k, t; + for (i = 1; i <= p.x - v.x + 1; i++) + { + for (j = 1; j <= p.y - v.y + 1; j++) + { + for (k = 1; k <= p.z - v.z + 1; k++) + { + for (t = 1; t <= 96 - tim + 1; t++) + { + if (judge(i, j, k, t)) + { + ans++; + } + } + } + } + } + return ans; +} + +int main() +{ + int cas = 0; + while (~scanf("%d%d%d%d%d", &n, &p.x, &p.y, &p.z, &q) && n) + { + memset(vis, 0, sizeof(vis)); + for (int i = 1; i <= n; i++) + { + s.scan(); + e.scan(); + scanf("%d:%d%d:%d", &h1, &m1, &h2, &m2); + time1 = (h1 * 60 + m1) / 15; + time2 = (h2 * 60 + m2) / 15; + for (int x = s.x + 1; x <= e.x; x++) + { + for (int y = s.y + 1; y <= e.y; y++) + { + for (int z = s.z + 1; z <= e.z; z++) + { + for (int t = time1 + 1; t <= time2; t++) + { + vis[x][y][z][t] = 1; + } + } + } + } + } + printf("3D World %d:\n", ++cas); + while (q--) + { + v.scan(); + scanf("%d:%d", &h1, &m1); + tim = (h1 * 60 + m1) / 15; + int ans = solve(); + if (ans) + { + printf("%d safe place(s) found\n", ans); + } + else + { + printf("No safe place(s) found\n"); + } + } + } + return 0; +} diff --git a/11590.cpp b/11590.cpp new file mode 100644 index 0000000..f909c4d --- /dev/null +++ b/11590.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +/* + Experimenting wiht the input... + + Accepted too. + */ + +const int BUFSIZE = 512; +char buf[BUFSIZE]; + +#define D(x) cout << #x " is " << (x) << endl + +typedef unsigned long long uint64; + +struct node +{ + bool end; + node *left; + node *right; + node(bool end) : end(end) + { + left = right = NULL; + } + node *getLeft() + { + if (left == NULL) + { + left = new node(false); + } + return left; + } + node *getRight() + { + if (right == NULL) + { + right = new node(false); + } + return right; + } +}; + +//returns 2^e +inline uint64 pow2(int e) +{ + if (e == 64) + { + return 0; + } + return uint64(1) << e; +} + +void clean(node *cur) +{ + if (cur == NULL) + { + return; + } + clean(cur->left); + clean(cur->right); + delete cur; +} + +int n, m; + +/* + Returns how many strings were counted under the tree rooted at cur, including it. +*/ +uint64 alreadyCounted(node *cur, int depth = 0) +{ + if (cur == NULL) + { + return 0; + } + uint64 left = alreadyCounted(cur->left, depth + 1); + uint64 right = alreadyCounted(cur->right, depth + 1); + uint64 ret = left + right; + if (cur->end) + { + //count strings matched to this one + ret += pow2(m - depth) - left - right; + } + return ret; +} + +int main() +{ + while (true) + { + scanf("%d%d\n", &n, &m); + if (n == 0 && m == 0) + { + break; + } + node *root = new node(true); + for (int i = 0; i < n; ++i) + { + scanf("%s", buf); + node *cur = root; + for (int j = 0;; ++j) + { + if (buf[j] == '*') + { + cur->end = true; + break; + } + node *next; + if (buf[j] == '0') + { + next = cur->getLeft(); + } + else if (buf[j] == '1') + { + next = cur->getRight(); + } + else + { + printf("Bad character %c at query %d, position %d\n", buf[j], i, j); + //assert(false); + } + cur = next; + } + } + int k; + scanf("%d\n", &k); + for (int i = 0; i < k; ++i) + { + scanf("%s", buf); + node *cur = root; + for (int j = 0; buf[j] != '*'; ++j) + { + if (buf[j] == '0') + { + cur = cur->left; + } + else if (buf[j] == '1') + { + cur = cur->right; + } + } + string s(buf); + s = s.substr(0, s.size() - 1); + uint64 ans = pow2(m - s.size()) - alreadyCounted(cur->left, s.size() + 1) - alreadyCounted(cur->right, s.size() + 1); + printf("%llu\n", ans); + } + printf("\n"); + clean(root); + } + return 0; +} diff --git a/11591.cpp b/11591.cpp new file mode 100644 index 0000000..4b690d3 --- /dev/null +++ b/11591.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +int N, M, M2, rmin[200], rmax[200], cmin[200], cmax[200]; +set SR, SC; + +void add(int r1, int r2, int c1, int c2) +{ + if (r2 >= N && c2 >= N) + { + rmin[M2] = r1; + rmax[M2] = N - 1; + cmin[M2] = c1; + cmax[M2] = N - 1; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + rmin[M2] = r1; + rmax[M2] = N - 1; + cmin[M2] = 0; + cmax[M2] = c2 - N; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + rmin[M2] = 0; + rmax[M2] = r2 - N; + cmin[M2] = c1; + cmax[M2] = N - 1; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + rmin[M2] = 0; + rmax[M2] = r2 - N; + cmin[M2] = 0; + cmax[M2] = c2 - N; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + } + else if (r2 >= N) + { + rmin[M2] = r1; + rmax[M2] = N - 1; + cmin[M2] = c1; + cmax[M2] = c2; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + rmin[M2] = 0; + rmax[M2] = r2 - N; + cmin[M2] = c1; + cmax[M2] = c2; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + } + else if (c2 >= N) + { + rmin[M2] = r1; + rmax[M2] = r2; + cmin[M2] = c1; + cmax[M2] = N - 1; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + rmin[M2] = r1; + rmax[M2] = r2; + cmin[M2] = 0; + cmax[M2] = c2 - N; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + } + else + { + rmin[M2] = r1; + rmax[M2] = r2; + cmin[M2] = c1; + cmax[M2] = c2; + SR.insert(rmin[M2]); + SR.insert(rmax[M2] + 1); + SC.insert(cmin[M2]); + SC.insert(cmax[M2] + 1); + ++M2; + } +} + +long long f(int n) +{ + return (long long)n * (n + 1) / 2; +} + +long long count(long long r1, long long r2, long long c1, long long c2) +{ + if (c1 > r2 - 1) + { + return (r2 - r1) * (c2 - c1); + } + if (c2 - 1 <= r1) + { + return 0; + } + if (r2 - 1 <= c2 - 2) + { + if (c1 - 1 >= r1) + { + return f(r2 - c1 + 1) + (c1 - r1 - 1) * (c2 - c1) + (r2 - c1 + 1) * (c2 - r2 - 1); + } + return f(r2 - r1) + (r2 - r1) * (c2 - r2 - 1); + } + else + { + if (r1 + 1 >= c1) + { + return f(c2 - r1 - 1); + } + return f(c2 - c1) + (c1 - r1 - 1) * (c2 - c1); + } + return 0; +} + +int main() +{ + int T; + char line[100]; + scanf("%d", &T); + for (int tc = 1; tc <= T; ++tc) + { + scanf("%d %d\n", &N, &M); + SR.clear(); + SC.clear(); + SR.insert(0); + SR.insert(N); + SC.insert(0); + SC.insert(N); + M2 = 0; + for (int i = 0; i < M; ++i) + { + fgets(line, 100, stdin); + int L = strlen(line); + for (int j = 0; j < L; ++j) + if (line[j] < '0' || line[j] > '9') + { + line[j] = ' '; + } + istringstream is(line); + vector param; + int aux; + while (is >> aux) + { + param.push_back(aux); + } + int r1 = param[0] - 1; + int c1 = param[1] - 1; + int c2 = c1 + param[2] - 1; + if (param.size() == 4 && param[3] % 2 == 1) + { + add(r1, r1 + param[2] - 1, c1, c2); + } + if (param.size() == 5 && param[4] % 2 == 1) + { + add(r1, r1 + param[3] - 1, c1, c2); + } + } + vector R(SR.begin(), SR.end()), C(SC.begin(), SC.end()); + int nR = R.size(), nC = C.size(); + bool change[nR - 1][nC - 1]; + memset(change, 0, sizeof(change)); + for (int k = 0; k < M2; ++k) + for (int i = 0; i + 1 < nR; ++i) + if (rmin[k] <= R[i] && R[i + 1] - 1 <= rmax[k]) + for (int j = 0; j + 1 < nC; ++j) + if (cmin[k] <= C[j] && C[j + 1] - 1 <= cmax[k]) + { + change[i][j] ^= 1; + } + long long ans = 0; + for (int i = 0; i + 1 < nR; ++i) + for (int j = 0; j + 1 < nC; ++j) + if (!change[i][j]) + { + ans += (long long)(R[i + 1] - R[i]) * (C[j + 1] - C[j]) - count(R[i], R[i + 1], C[j], C[j + 1]); + } + else + { + ans += count(R[i], R[i + 1], C[j], C[j + 1]); + } + printf("Case %d: %lld\n", tc, ans); + } + return 0; +} diff --git a/11593.cpp b/11593.cpp new file mode 100644 index 0000000..f336db4 --- /dev/null +++ b/11593.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const long long MAX_D = 1000000000; + +// Numero de puntos enteros en el rectangulo +// (0,0) (MAX_D, MAX_D * A / B) + +long long count(int A, int B) +{ + return ((MAX_D + 1) * (MAX_D * A / B + 1) - MAX_D / (B / __gcd(A, B))) / 2; +} + +int main() +{ + char s[10]; + while (true) + { + scanf("%s", s); + if (s[0] == '#') + { + break; + } + int K = strlen(s) - 2, X = 0, pot10 = 1; + for (int i = 0; i < K; ++i) + { + X = X * 10 + s[i + 2] - '0'; + pot10 *= 10; + } + printf("%lld\n", count(2 * X + 1, 2 * pot10) - count(2 * X - 1, 2 * pot10)); + } + return 0; +} diff --git a/11594.cpp b/11594.cpp new file mode 100644 index 0000000..a00c030 --- /dev/null +++ b/11594.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define N 205 +#define INF 1 << 28 + +int g[N][N], mk[N]; +int cap[N][N], dis[N], pre[N], cnt[N]; + +int ISAP(int src, int sink, int n) +{ + int maxflow = 0, flow, u = src, v, d; + memset(dis, 0, sizeof(dis)); + memset(cnt, 0, sizeof(cnt)); + pre[src] = src; + cnt[src] = n; + while (dis[src] < n) + { + if (u == sink) + { + flow = INF; + for (int i = sink; i != src; i = pre[i]) + if (cap[pre[i]][i] < flow) + { + flow = cap[pre[i]][i]; + } + maxflow += flow; + u = src; + for (int i = sink; i != src; i = pre[i]) + { + cap[pre[i]][i] -= flow; + cap[i][pre[i]] += flow; + } + } + for (d = n, v = 0; v < n; v++) + { + if (!cap[u][v]) + { + continue; + } + d = d < dis[v] ? d : dis[v]; + if (dis[v] + 1 == dis[u]) + { + break; + } + } + if (v < n) + { + pre[v] = u, u = v; + } + else + { + if (!(--cnt[dis[u]])) + { + break; + } + ++cnt[dis[u] = d + 1]; + if (u != src) + { + u = pre[u]; + } + } + } + return maxflow; +} + +int parent[N], cut[N][N]; + +void dfs(int u, int n) +{ + mk[u] = 1; + for (int i = 0; i < n; i++) + { + if (!mk[i] && cap[u][i]) + { + dfs(i, n); + } + } +} + +void Solve(int n) +{ + memset(parent, 0, sizeof(parent)); + memset(cut, 127, sizeof(cut)); + for (int i = 1; i < n; i++) + { + memset(mk, 0, sizeof(mk)); + memcpy(cap, g, sizeof(g)); + int mincut = ISAP(i, parent[i], n); + dfs(i, n); + for (int j = i + 1; j < n; j++) + if (mk[j] && parent[j] == parent[i]) + { + parent[j] = i; + } + cut[i][parent[i]] = cut[parent[i]][i] = mincut; + for (int j = 0; j < i; j++) + { + cut[i][j] = cut[j][i] = min(mincut, cut[parent[i]][j]); + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + cut[i][j] = 0; + } + printf("%d", cut[i][j]); + if (j != n - 1) + { + printf(" "); + } + } + printf("\n"); + } +} + +int main() +{ + int t, n, cas = 1; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + scanf("%d", &g[i][j]); + } + printf("Case #%d:\n", cas++); + Solve(n); + } + return 0; +} diff --git a/11597.cpp b/11597.cpp index deb97eb..56e1791 100644 --- a/11597.cpp +++ b/11597.cpp @@ -1,13 +1,17 @@ -#include -using namespace std; - -int main(){ - long long n=0; - for(int i=1;;i++){ - cin>>n; - if(n==0) - break; - cout<<"Case "< + +using namespace std; + +int main() +{ + int n, t = 1; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + printf("Case %d: %d\n", t++, n / 2); + } + return 0; +} diff --git a/11598.cpp b/11598.cpp new file mode 100644 index 0000000..07318f5 --- /dev/null +++ b/11598.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +int V[35], N, K; +int dp[31][16][30 * 26 + 5];// [i-th][segment_count][min_value] +int ac_dp[31][16][30 * 26 + 5]; +void solve() +{ + int i, j, k, prev; + int sum[35] = {}, sumX[35] = {}, nextX[35] = {}; + for (i = 1; i <= N; i++) + { + sum[i] = sum[i - 1] + V[i]; + sumX[i] = sumX[i - 1] + (V[i] == 0); + } + for (i = N, prev = N + 1; i >= 0; i--) + { + nextX[i] = prev; + if (V[i] == 0) + prev = i; + } + if (sumX[N] < K) + { + puts("not possible!"); + return; + } + set R[31][16]; + dp[0][0][0] = 0, R[0][0].insert(0); + for (i = 0; i < N; i++) + { + for (j = 0; j < K; j++) + { + for (set::iterator it = R[i][j].begin(); + it != R[i][j].end(); it++) + { + for (k = nextX[i]; k <= N; k++) + { + int s = sum[k] - sum[i]; + int mn = min(*it, s), mx = max(dp[i][j][*it], s); + if (i == 0) + mn = mx = s; + if (R[k][j + 1].find(mn) == R[k][j + 1].end()) + R[k][j + 1].insert(mn), dp[k][j + 1][mn] = 0xfffffff, ac_dp[k][j + 1][mn] = 0; + dp[k][j + 1][mn] = min(dp[k][j + 1][mn], mx); + } + } + } + } + int ret = 0xfffffff; + for (set::iterator it = R[N][K].begin(); + it != R[N][K].end(); it++) + { + ret = min(ret, dp[N][K][*it] - *it); + } + if (ret >= 50) + { + puts("overflow"); + return; + } + printf("%lld ", 1LL << ret); + vector> P; + for (set::iterator it = R[N][K].begin(); + it != R[N][K].end(); it++) + { + if (ret == dp[N][K][*it] - *it) + { + ac_dp[N][K][*it] = 1; + P.push_back(make_pair(*it, dp[N][K][*it])); + } + } + for (i = N; i >= 0; i--) + { + for (j = 0; j < K; j++) + { + for (set::iterator it = R[i][j].begin(); + it != R[i][j].end(); it++) + { + for (k = nextX[i]; k <= N; k++) + { + int s = sum[k] - sum[i]; + int mn = min(*it, s), mx = max(dp[i][j][*it], s); + if (i == 0) + mn = mx = s; + if (ac_dp[k][j + 1][mn]) + { + int ok = 0; + for (int p = 0; p < P.size(); p++) + { + if (*it >= P[p].first && dp[i][j][*it] <= P[p].second) + ok = 1; + } + if (ok) + { + ac_dp[i][j][*it] = ok; + break; + } + } + } + } + } + } + int idx = 0, idx_mn = 0; + for (int seg = 0; seg < K; seg++) + { + for (j = N; j >= idx; j--) + { + if (sumX[j] - sumX[idx] == 0) + continue; + int s = sum[j] - sum[idx]; + int mn = min(idx_mn, s), mx = max(dp[idx][seg][idx_mn], s); + if (idx == 0) + mn = mx = s; + if (ac_dp[j][seg + 1][mn]) + { + putchar('('); + for (k = idx + 1; k <= j; k++) + { + if (V[k]) + printf("%d", V[k]); + else + printf("X"); + printf("%c", k == j ? ')' : ' '); + } + idx = j, idx_mn = mn; + break; + } + } + } + puts(""); +} +int main() +{ + int testcase, cases = 0; + int i, j, k; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &K); + for (i = 1; i <= N; i++) + { + char s[10]; + scanf("%s", s); + if (s[0] == 'X') + V[i] = 0; + else + sscanf(s, "%d", &V[i]); + } + printf("Case %d: ", ++cases); + solve(); + } + return 0; +} +/* +1000 +20 6 +X 3 7 X 2 4 6 3 X 6 9 4 X X 4 7 X X 5 6 +20 12 +X X 2 X 7 2 X X X X 1 X X 9 X X X X 4 X +*/ diff --git a/116.cpp b/116.cpp index 0bf696a..48386b1 100644 --- a/116.cpp +++ b/116.cpp @@ -1,90 +1,96 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1LL<<60 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +static int tbl[20][200], sum[20][200], x, y, best[2]; +static queue path; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int64 M, N; - while (scanf("%lld%lld", &M, &N) != EOF){ - int64 mat[M][N], pre[M][N]; - int64 i, j; - FOI(i, 0, M-1){ - FOI(j, 0, N-1){ - scanf("%lld", &mat[i][j]); - pre[i][j] = INF; +int main() +{ + while (scanf("%d%d", &x, &y) == 2) + { + // read tbl + for (int i = 0; i < x; i++) + { + for (int j = 0; j < y; j++) + { + scanf("%d", &tbl[i][j]); } } - FOD(j, N-2, 0){ - FOI(i, 0, M-1){ - int64 A = (M + i - 1) % M; - int64 B = (M + i) % M; - int64 C = (M + i + 1) % M; - - int64 mi = min(mat[A][j+1], min(mat[B][j+1], mat[C][j+1])); - mat[i][j] += mi; - - if (mi == mat[A][j+1]) - pre[i][j] = min(pre[i][j], A); - if (mi == mat[B][j+1]) - pre[i][j] = min(pre[i][j], B); - if (mi == mat[C][j+1]) - pre[i][j] = min(pre[i][j], C); + + // prepare sum[] + for (int i = 0; i < x; i++) + { + sum[i][y - 1] = tbl[i][y - 1]; + } + + // dp + for (int j = y - 2; j >= 0; j--) + { + for (int i = 0; i < x; i++) + { + long a = sum[(i + x - 1) % x][j + 1], b = sum[i][j + 1], c = sum[(i + x + 1) % x][j + 1]; + sum[i][j] = min(a, min(b, c)) + tbl[i][j]; } } - int64 minm = INF, inR = 0, inC = N - 1; - FOI(i, 0, M-1){ - if (minm > mat[i][0]){ - inR = i; - minm = mat[i][0]; + + // find min + int min = sum[0][0]; + best[0] = 0; + best[1] = 0; + for (int i = 1; i < x; i++) + { + if (sum[i][0] < min) + { + min = sum[i][0]; + best[0] = i; } } - vector< int64 > V; - FOI(inC, 0, N-1){ - V.push_back(inR); - inR = pre[inR][inC]; + + // trk lexicographical path + path.push(best[0]); + for (int j = 1; j < y; j++) + { + long a = sum[best[0]][j - 1] - tbl[best[0]][j - 1]; + int m = x + 1; + if (sum[(x + best[0] - 1) % x][j] == a) + { + if ((x + best[0] - 1) % x < m) + { + m = (x + best[0] - 1) % x; + } + } + if (sum[(x + best[0] + 1) % x][j] == a) + { + if ((x + best[0] + 1) % x < m) + { + m = (x + best[0] + 1) % x; + } + } + if (sum[best[0]][j] == a) + { + if (best[0] < m) + { + m = best[0]; + } + } + best[0] = m; + path.push(m); } - FOI(i, 0, V.size()-1){ - printf("%lld", V[i] + 1); - if (i < V.size() - 1) + + // print out path + min res + int c = 0; + while (!path.empty()) + { + if (c > 0) + { printf(" "); + } + c++; + printf("%d", path.front() + 1); + path.pop(); } - printf("\n%lld\n", minm); + printf("\n%d\n", min); } return 0; } - diff --git a/1160.cpp b/1160.cpp new file mode 100644 index 0000000..55cb9b9 --- /dev/null +++ b/1160.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define MAX 100001 + +int p[MAX], ans; + +int find(int x) +{ + if (p[x] == x) + { + return x; + } + return p[x] = find(p[x]); +} + +int main() +{ + int a, b; + while (scanf("%d", &a) != EOF) + { + for (int i = 0; i < MAX; i++) + { + p[i] = i; + } + ans = 0; + while (1) + { + scanf("%d", &b); + int ita = find(a); + int itb = find(b); + if (ita == itb) + { + ans++; + } + else + { + p[ita] = itb; + } + scanf("%d", &a); + if (a == -1) + { + break; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11600.cpp b/11600.cpp new file mode 100644 index 0000000..0d4a736 --- /dev/null +++ b/11600.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define xx first +#define yy second +#define pb push_back +#define mp make_pair +#define LL long long +#define inf INT_MAX / 3 +#define mod 1000000007ll +#define PI 2.0 * acos(0.0) +#define linf (1ll << 60) - 1 +#define FOR(I, A, B) for (int I = (A); I < (B); ++I) +#define REP(I, N) FOR(I, 0, N) +#define ALL(A) (A).begin(), (A).end() +#define set0(ar) memset(ar, 0, sizeof ar) +#define vsort(v) sort(v.begin(), v.end()) +#define setinf(ar) memset(ar, 126, sizeof ar) + +template +inline T bigmod(T p, T e, T M) +{ + if (e == 0) + return 1; + if (e % 2 == 0) + { + LL t = bigmod(p, e / 2, M); + return (T)((t * t) % M); + } + return (T)(((LL)bigmod(p, e - 1, M) * (LL)(p)) % M); +} +template +inline T gcd(T a, T b) +{ + if (b == 0) + return a; + return gcd(b, a % b); +} +template +inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } + +double dp[1 << 16][31]; +bool visited[1 << 16][31]; +LL cc, ck, vis[33], tot_one; +vector vc[17], vt[33]; + +double go(LL bp, LL oc) +{ + if (bp == (1 << ck) - 1 && oc == 0) + return 0.0; + if (visited[bp][oc] == 1) + return dp[bp][oc]; + visited[bp][oc] = 1; + LL same = -1; + REP(i, ck) + { + if ((bp & 1 << i) != 0) + same += vc[i].size(); + else + dp[bp][oc] += ((double)vc[i].size() / (double)(cc - 1)) * (1.0 + go(bp | 1 << i, oc)); + } + same += tot_one - oc; + if (oc > 0) + dp[bp][oc] += ((double)oc / (double)(cc - 1)) * (1.0 + go(bp, oc - 1)); + double p = (double)same / (double)(cc - 1); + dp[bp][oc] += p; + dp[bp][oc] /= (1.0 - p); + return dp[bp][oc]; +} + +void dfs(LL p) +{ + vis[p] = 1; + vc[ck].pb(p); + REP(i, vt[p].size()) + { + if (vis[vt[p][i]] != 0) + continue; + dfs(vt[p][i]); + } +} + +int main() +{ + LL a, b, c, d, e, f, g, h = 1, x, y, z; + cin >> a; + while (a--) + { + set0(dp); + set0(visited); + set0(vis); + cout << "Case " << h++ << ": "; + cin >> b >> c; + REP(i, 16) + vc[i].clear(); + REP(i, b + 1) + vt[i].clear(); + REP(i, c) + { + cin >> d >> e; + vt[d].pb(e); + vt[e].pb(d); + } + d = ck = 0; + cc = b; + FOR(i, 1, b + 1) + { + if (vis[i] == 1) + continue; + dfs(i); + if (vc[ck].size() == 1) + { + d++; + vc[ck].clear(); + } + else + ck++; + } + tot_one = d; + f = 0; + if (b == 1) + d = 0; + else + { + if (vc[0][0] == 1) + f = 1; + else + d--; + } + printf("%.8lf\n", go(f, d)); + } + return 0; +} diff --git a/11601.cpp b/11601.cpp new file mode 100644 index 0000000..fece0ea --- /dev/null +++ b/11601.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +int seg[405][1205], mat[405][1205]; + +void init(int *arr, int *mat, int now, int l, int r) +{ + int c; + arr[now] = 0; + mat[now] = 0; + if (l != r) + { + c = (l + r) / 2; + init(arr, mat, now << 1, l, c); + init(arr, mat, (now << 1) + 1, c + 1, r); + } +} + +int chk(int *arr, int *mat, int now, int l, int r, int tl, int tr) +{ + int c, res; + if (arr[now]) + { + return 1; + } + if (tl <= l && tr >= r) + { + return arr[now] + mat[now]; + } + res = 0; + c = (l + r) / 2; + if (c >= tl && tr >= l) + { + res |= chk(arr, mat, now << 1, l, c, tl, tr); + } + if (r >= tl && tr >= c + 1) + { + res |= chk(arr, mat, (now << 1) + 1, c + 1, r, tl, tr); + } + return res; +} + +int insert(int *arr, int *mat, int now, int l, int r, int tl, int tr) +{ + int c; + if (tl <= l && tr >= r) + { + arr[now] = 1; + return 1; + } + c = (l + r) / 2; + if (c >= tl && tr >= l) + { + mat[now] |= insert(arr, mat, now << 1, l, c, tl, tr); + } + if (r >= tl && tr >= c + 1) + { + mat[now] |= insert(arr, mat, (now << 1) + 1, c + 1, r, tl, tr); + } + return arr[now] | mat[now]; +} + +int main() +{ + int count, cas, n, i, j, x1, x2, y1, y2, ans; + cas = 0; + scanf("%d", &count); + while (count--) + { + scanf("%d", &n); + for (i = 0; i <= 200; i++) + { + init(seg[i], mat[i], 1, 0, 200); + } + for (i = 0, ans = 0; i < n; i++) + { + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + for (j = x1; j < x2; j++) + { + if (chk(seg[j + 100], mat[j + 100], 1, 0, 200, y1 + 100, y2 + 100 - 1)) + { + break; + } + } + if (j >= x2) + { + for (j = x1; j < x2; j++) + { + insert(seg[j + 100], mat[j + 100], 1, 0, 200, y1 + 100, y2 + 100 - 1); + } + ans += (x2 - x1) * (y2 - y1); + } + } + printf("Case %d: %d\n", ++cas, ans); + } + return 0; +} diff --git a/11603.cpp b/11603.cpp new file mode 100644 index 0000000..2e7d0ca --- /dev/null +++ b/11603.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define all(a) a.begin(), a.end() +#define clr(a) memset(a, 0, sizeof(a)) +#define fill(a, b) memset(a, b, sizeof(a)) +#define pb push_back +#define mp make_pair + +typedef long long LL; +typedef vector VI; +typedef pair PII; +typedef vector> VII; + +const double eps = 1e-14; +const double pi = acos(-1.0); +#define N 205 +int dis[N], vis[N]; +int a[N][N], pre[N], b[N][N]; +int okay[N][N]; + +int main() +{ + // freopen("1.txt","r",stdin); + int tt, cal = 0, i, j, k, tmp, ff, n; + cin >> tt; + while (tt--) + { + printf("Case #%d: ", ++cal); + scanf("%d", &n); + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + cin >> a[i][j], b[i][j] = a[i][j]; + if (n == 1) + { + cout << 0 << endl; + continue; + } + memset(pre, -1, sizeof(pre)); + for (k = 1; k <= n; k++) + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + if (i != j && j != k && k != i) + { + b[i][j] = max(b[i][j], min(b[i][k], b[k][j])); + } + ff = 1; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + ff &= a[i][j] == b[i][j]; + if (ff == 0) + { + puts("Impossible"); + continue; + } + + memset(dis, 0, sizeof(dis)); + memset(vis, 0, sizeof(vis)); + int mm, uu; + for (i = 1; i < n; i++) + { + mm = -1; + for (j = 1; j <= n; j++) + if (!vis[j] && dis[j] > mm) + mm = dis[j], uu = j; + vis[uu] = 1; + for (j = 1; j <= n; j++) + if (!vis[j] && a[uu][j] > dis[j]) + dis[j] = a[uu][j], pre[j] = uu; + } + cout << n - 1 << endl; + for (i = 2; i <= n; i++) + cout << pre[i] - 1 << ' ' << i - 1 << ' ' << a[pre[i]][i] << endl; + } + return 0; +} diff --git a/11604.cpp b/11604.cpp new file mode 100644 index 0000000..577d6ff --- /dev/null +++ b/11604.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +#define ll long long +#define vi vector +#define pii pair +#define FOR(i, a, b) for (i = (a); i <= (b); i++) +#define REP(i, a) for (i = 0; i < (a); i++) +#define ALL(v) (v).begin(), (v).end() +#define SET(a, x) memset((a), (x), sizeof(a)) +#define SZ(a) ((int)(a).size()) +#define CL(a) ((a).clear()) +#define SORT(x) sort(ALL(x)) +#define mp make_pair +#define pb push_back +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +int n, val[111], len[111], vst[(1 << 22)][22][2], True = 0, vs[111][25]; + +int main() +{ + int i, j, u, v, ks = 0; + char s[100], str[100]; + while (scanf("%d", &n) == 1) + { + if (n == 0) + break; + ks++; + True++; + REP(i, n) + { + scanf("%s %s", s, str); + len[i] = strlen(str); + val[i] = 0; + REP(j, len[i]) + { + vs[i][j] = str[j] - '0'; + val[i] |= (vs[i][j]); + val[i] <<= 1; + } + val[i] >>= 1; + } + queue Q, L, T; + REP(i, n) + { + Q.push(val[i]); + L.push(len[i]); + T.push(0); + if (vst[val[i]][len[i]][0] == True) + { + break; + } + vst[val[i]][len[i]][0] = True; + } + if (i != n) + { + printf("Case #%d: Ambiguous.\n", ks); + continue; + } + int ul, vl, nl, nu, mn, mx, j, ut, id, tu; + int uss[33], *vss; + while (!Q.empty()) + { + u = Q.front(); + ul = L.front(); + ut = T.front(); + if (u == 0 && ul == 0) + break; + SET(uss, 0); + id = ul - 1; + tu = u; + while (tu) + { + uss[id--] = tu % 2; + tu /= 2; + } + Q.pop(); + L.pop(); + T.pop(); + REP(i, n) + { + v = val[i]; + vl = len[i]; + vss = &vs[i][0]; + if (u == v && ul == vl) + { + if (ut) + { + if (vst[0][0][1] == True) + { + continue; + } + vst[0][0][1] = True; + Q.push(0); + L.push(0); + T.push(1); + } + continue; + } + if (ul > vl) + { + nl = ul - vl; + REP(j, vl) + { + if (vss[j] != uss[j]) + break; + } + if (j != vl) + continue; + nu = 0; + for (; j < ul; j++) + { + nu |= (uss[j]); + nu <<= 1; + } + nu >>= 1; + } + else + { + nl = vl - ul; + REP(j, ul) + { + if (vss[j] != uss[j]) + break; + } + if (j != ul) + continue; + nu = 0; + for (; j < vl; j++) + { + nu |= (vss[j]); + nu <<= 1; + } + nu >>= 1; + } + if (vst[nu][nl][1] == True) + continue; + vst[nu][nl][1] = True; + Q.push(nu); + L.push(nl); + T.push(1); + } + } + if (Q.empty()) + { + printf("Case #%d: Not ambiguous.\n", ks); + continue; + } + else + printf("Case #%d: Ambiguous.\n", ks); + } + return 0; +} diff --git a/11605.cpp b/11605.cpp new file mode 100644 index 0000000..315a0d5 --- /dev/null +++ b/11605.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +double pe[102][102][102]; +int n, m, p, t; + +int min(int a, int b) +{ + return a < b ? a : b; +} + +int max(int a, int b) +{ + return a > b ? a : b; +} + +int a[3][120]; + +void init(int id, int n) +{ + int i, j, k; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + for (k = 1; k <= n; k++) + if (k >= min(i, j) && k <= max(i, j)) + a[id][k]++; +} + +const int ml = 2; +double c[ml], cc[ml], ret[ml]; + +void mulrr(double *a, double *b) +{ + int i, j, k; + cc[0] = a[0] * b[0] + a[1] * b[1]; + cc[1] = a[0] * b[1] + a[1] * b[0]; + a[0] = cc[0]; + a[1] = cc[1]; +} + +void power(double *a, int b) +{ + int i, j; + --b; + ret[0] = a[0]; + ret[1] = a[1]; + while (b) + { + if (b & 1) + mulrr(ret, a); + mulrr(a, a); + b >>= 1; + } + a[0] = ret[0]; + a[1] = ret[1]; +} + +int main() +{ + int i, j, k, ca, ii, jj, kk, i2, j2, k2, cc = 1; + scanf("%d", &ca); + while (ca--) + { + memset(a, 0, sizeof(a)); + scanf("%d%d%d%d", &n, &m, &p, &t); + init(0, n); + init(1, m); + init(2, p); + if (t == 0) + { + printf("Case %d: %.10lf\n", cc++, 0.0); + continue; + } + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + for (k = 1; k <= p; k++) + { + pe[i][j][k] = 1; + pe[i][j][k] *= a[0][i] * 1.0 / (n * n * 1.0); + pe[i][j][k] *= a[1][j] * 1.0 / (m * m * 1.0); + pe[i][j][k] *= a[2][k] * 1.0 / (p * p * 1.0); + } + double ans = 0; + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + for (k = 1; k <= p; k++) + { + double pt = pe[i][j][k]; + c[0] = 1 - pt; + c[1] = pt; + power(c, t); + ans += c[1]; + } + printf("Case %d: %.10lf\n", cc++, ans); + } + return 0; +} diff --git a/11608.cpp b/11608.cpp index a83f5f9..a2d3c6b 100644 --- a/11608.cpp +++ b/11608.cpp @@ -1,34 +1,42 @@ -#include -using namespace std; - -int main(){ - for(int j=1;;j++){ - int n; - cin>>n; - if(n<0) - break; - int made[12]={0},comp[12]={0}; - for(int i=0;i<12;i++) - cin>>made[i]; - for(int i=0;i<12;i++) - cin>>comp[i]; - int temp=n; - for(int i=0;i<12;i++){ - int ttemp=made[i]; - made[i]=temp; - temp=ttemp; - } - cout<<"Case "<=comp[i]){ - cout<<"No problem! :D"< + +using namespace std; + +int main() +{ + int s; + int n[12], p[12]; + int counter = 0; + while (cin >> s) + { + if (s < 0) + { + break; + } + counter++; + for (int i = 0; i < 12; i++) + { + cin >> n[i]; + } + for (int i = 0; i < 12; i++) + { + cin >> p[i]; + } + cout << "Case " << counter << ":" << endl; + for (int i = 0; i < 12; i++) + { + if (p[i] <= s) + { + s -= p[i]; + s += n[i]; + cout << "No problem! :D" << endl; + } + else + { + s += n[i]; + cout << "No problem. :(" << endl; + } + } + } + return 0; +} diff --git a/11609.cpp b/11609.cpp index d71947f..5046d8d 100644 --- a/11609.cpp +++ b/11609.cpp @@ -1,28 +1,59 @@ -#include +#include + using namespace std; -typedef long long int64; -const int64 MOD = 1000000007; - -int64 power(int64 B, int64 N) { - if (N == 0) return 1LL; - if (N == 1) return B; - - int64 V = power(B, N/2) %MOD; - V = (V * V) %MOD; - if (N & 1) - V = (V * B) %MOD; - return V; +// http://uva.onlinejudge.org/external/116/11609.html +// Runtime: 0.012s +// Tag: bigMod + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Forv(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) + +#define MOD 1000000007 + +LL bigMod(LL b, LL p) +{ + if (p == 0) + { + return 1; + } + if (p == 1) + { + return b; + } + if (p % 2 == 0) + { + LL x = bigMod(b, p / 2) % MOD; + return (x * x) % MOD; + } + else + { + return (bigMod(b, p - 1) % MOD) * (b % MOD); + } } -int main() { - int64 T; - scanf("%lld", &T); - for (int64 t = 1; t <= T; t++) { - int64 N; - scanf("%lld", &N); - int64 V = (N * power(2, N - 1)) %MOD; - printf("Case #%lld: %lld\n", t, V); +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n; + scanf("%d", &n); + printf("Case #%d: ", ++cases); + cout << ((bigMod(2, n - 1) * (n % MOD)) % MOD) << endl; } return 0; } + +// @END_OF_SOURCE_CODE diff --git a/1161.cpp b/1161.cpp new file mode 100644 index 0000000..ad86fe6 --- /dev/null +++ b/1161.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +struct node1 +{ + int u, v, num, st, et; +} link[5010]; +struct node2 +{ + int u, v, flow, next; +} edge[1000010]; +map match; +queue qu; +int n, m, END; +char str[1010]; +int Head[10010], d[10010], s, t, tot, INF = 1e9; +int get_num() +{ + int k = match[str]; + if (k > 0) + { + return k; + } + match[str] = ++n; + return n; +} +void add(int u, int v, int f) +{ + edge[tot].u = u; + edge[tot].v = v; + edge[tot].flow = f; + edge[tot].next = Head[u]; + Head[u] = tot++; +} +void init() +{ + tot = 0; + memset(Head, -1, sizeof(Head)); + int i, j, k, u, v; + for (i = 1; i <= m; i++) + { + add(i * 2, i * 2 ^ 1, link[i].num); + add(i * 2 ^ 1, i * 2, 0); + } + for (i = 1; i <= m; i++) + for (j = 1; j <= m; j++) + if (link[i].v == link[j].u && link[i].et + 30 <= link[j].st) + { + //printf("i=%d j=%d\n",i,j); + add(i * 2 ^ 1, j * 2, INF); + add(j * 2, i * 2 ^ 1, 0); + } + for (i = 1; i <= m; i++) + { + if (link[i].u == 1) + { + add(0, i * 2, INF); + add(i * 2, 0, 0); + } + if (link[i].v == 2 && link[i].et <= END) + { + add(i * 2 ^ 1, 1, INF); + add(1, i * 2 ^ 1, 0); + } + } +} +int bfs() +{ + int i, j, k, u, v; + memset(d, -1, sizeof(d)); + while (!qu.empty()) + { + qu.pop(); + } + qu.push(s); + d[s] = 0; + while (!qu.empty()) + { + u = qu.front(); + qu.pop(); + for (i = Head[u]; i != -1; i = edge[i].next) + { + v = edge[i].v; + if (edge[i].flow > 0 && d[v] == -1) + { + d[v] = d[u] + 1; + if (v == t) + { + return 1; + } + qu.push(v); + } + } + } + return 0; +} +int dfs(int u, int f) +{ + if (u == t || f == 0) + { + return f; + } + int ans = 0, i, j, k, v; + for (i = Head[u]; i != -1; i = edge[i].next) + { + v = edge[i].v; + if (edge[i].flow > 0 && d[v] == d[u] + 1) + { + k = dfs(v, min(edge[i].flow, f)); + edge[i].flow -= k; + edge[i ^ 1].flow += k; + f -= k; + ans += k; + if (f == 0) + { + break; + } + } + } + d[u] = -1; + return ans; +} +int dinic() +{ + init(); + int ans = 0; + while (bfs()) + { + ans += dfs(0, INF); + } + return ans; +} +int main() +{ + int i, j, k, u, v, ans; + while (~scanf("%d", &n)) + { + n = 0; + m = 0; + match.clear(); + scanf("%s", str); + get_num(); + scanf("%s", str); + get_num(); + scanf("%d", &END); + END = END / 100 * 60 + END % 100; + scanf("%d", &m); + for (i = 1; i <= m; i++) + { + scanf("%s", str); + link[i].u = get_num(); + scanf("%s", str); + link[i].v = get_num(); + scanf("%d%d%d", &link[i].num, &link[i].st, &link[i].et); + link[i].st = link[i].st / 100 * 60 + link[i].st % 100; + link[i].et = link[i].et / 100 * 60 + link[i].et % 100; + } + s = 0; + t = 1; + ans = dinic(); + printf("%d\n", ans); + } +} diff --git a/11610.cpp b/11610.cpp new file mode 100644 index 0000000..7ee93e5 --- /dev/null +++ b/11610.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define N 1000000 +#define M 78499 + +set deleted; +set::iterator it; +char factors[N]; +int rev[M]; +int nFactors[M]; +int sum[M]; +int tree[M]; +int ind(int n) +{ + n++; + for (it = deleted.begin(); it != deleted.end() && *it <= n; it++) + { + n++; + } + return n; +} + +void del(int i) +{ + deleted.insert(i); + int val = nFactors[i]; + while (i <= M) + { + tree[i] -= val; + i += (i & -i); + } +} +int sumFactors(int i) +{ + int res = 0; + while (i > 0) + { + res += tree[i]; + i -= (i & -i); + } + return res; +} +int lastOne(int n) +{ + return n & -n; +} +int nfactors(int n) +{ + int i, sum = 0; + for (i = 2; i < N && n >= N; i++) + if (factors[i] == 1) + { + while (n % i == 0) + { + sum++; + n /= i; + } + } + return sum + factors[n]; +} +int reverse(int n) +{ + int res = 0; + while (n > 0) + { + res = res * 10 + n % 10; + n /= 10; + } + return res; +} +int main() +{ + int i, j, k, t; + map indexOf; + for (i = 0; i < N; i++) + { + factors[i] = 0; + } + for (i = 2; i < N; i++) + { + if (!factors[i]) + { + factors[i] = 1; + } + for (j = 2; (k = i * j) < N && j <= i; j++) + if (factors[j] == 1) + { + factors[k] = factors[j] + factors[i]; + } + } + t = 1; + for (i = 1000000; i <= 9999999; i++) + if ((j = reverse(i)) < 1000000 && factors[j] == 1) + { + rev[t] = i; + indexOf[i] = t; + t++; + } + nFactors[1] = nfactors(rev[1]); + sum[1] = nFactors[1]; + for (i = 2; i < t; i++) + { + nFactors[i] = nfactors(rev[i]); + sum[i] = sum[i - 1] + nFactors[i]; + } + for (i = 1; i < t; i++) + { + tree[i] = sum[i] - sum[i - lastOne(i) + 1] + nFactors[i - lastOne(i) + 1]; + } + char o; + int n; + while (scanf("%c %d", &o, &n) == 2) + { + getchar(); + if (o == 'q') + { + printf("%d\n", sumFactors(ind(n))); + } + else + { + del(indexOf[n]); + } + } + return 0; +} diff --git a/11611.cpp b/11611.cpp new file mode 100644 index 0000000..703f185 --- /dev/null +++ b/11611.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +#define xx first +#define yy second +#define pb push_back +#define mp make_pair +#define LL long long +#define inf INT_MAX / 3 +#define mod 1000000007ll +#define PI 2.0 * acos(0.0) +#define linf (1 << 60) - 1 +#define FOR(I, A, B) for (int I = (A); I < (B); ++I) +#define REP(I, N) FOR(I, 0, N) +#define ALL(A) ((A).begin(), (A).end()) +#define set0(ar) memset(ar, 0, sizeof ar) +#define vsort(v) sort(v.begin(), v.end()) +#define setinf(ar) memset(ar, 126, sizeof ar) + +template +inline T bigmod(T p, T e, T M) +{ + if (e == 0) + return 1; + if (e % 2 == 0) + { + LL t = bigmod(p, e / 2, M); + return (T)((t * t) % M); + } + return (T)(((LL)bigmod(p, e - 1, M) * (LL)(p)) % M); +} +template +inline T gcd(T a, T b) +{ + if (b == 0) + return a; + return gcd(b, a % b); +} +template +inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } + +int md = 50431, n, m; +int dp[1 << 16][20][10]; +char pp[9][21]; + +int go(int bp, int cur, int i) +{ + if (bp == 0 && cur == m - 1) + return 1; + if (bp < 1 << n) + { + bp = bp << n; + REP(j, n) + if (pp[j][cur + 2] != '#') bp |= 1 << j; + return go(bp, cur + 1, 0); + } + if (i == n) + return 0; + if (dp[bp][cur][i] != -1) + return dp[bp][cur][i]; + int &res = dp[bp][cur][i]; + res = go(bp, cur, i + 1); + if ((bp & 1 << (i + n)) == 0) + return res; + if (pp[i][cur] == '#') + return res; + if (i > 0 && n > 1) + { + if ((pp[i][cur] == '.' || pp[i][cur] == 'R') && (bp & 1 << (i + n)) > 0 && + (pp[i - 1][cur] == '.' || pp[i - 1][cur] == 'R') && (bp & 1 << (i + n - 1)) > 0 && + (pp[i][cur + 1] == '.' || pp[i][cur + 1] == 'R') && (bp & 1 << i) > 0) + { + LL nb = bp & ~(1 << (i + n)); + nb = nb & ~(1 << (i + n - 1)); + nb = nb & ~(1 << i); + res = (res + go(nb, cur, i + 1)) % md; + } + if ((pp[i][cur] == '.' || pp[i][cur] == 'G') && (bp & 1 << (i + n)) > 0 && + (pp[i - 1][cur + 1] == '.' || pp[i - 1][cur + 1] == 'G') && (bp & 1 << (i - 1)) > 0 && + (pp[i][cur + 1] == '.' || pp[i][cur + 1] == 'G') && (bp & 1 << i) > 0) + { + LL nb = bp & ~(1 << (i + n)); + nb = nb & ~(1 << (i - 1)); + nb = nb & ~(1 << i); + res = (res + go(nb, cur, i + 1)) % md; + } + if ((pp[i][cur] == '.' || pp[i][cur] == 'B') && (bp & 1 << (i + n)) > 0 && + (pp[i - 1][cur] == '.' || pp[i - 1][cur] == 'B') && (bp & 1 << (i + n - 1)) > 0 && + (pp[i - 1][cur + 1] == '.' || pp[i - 1][cur + 1] == 'B') && (bp & 1 << (i - 1)) > 0) + { + LL nb = bp & ~(1 << (i + n)); + nb = nb & ~(1 << (i + n - 1)); + nb = nb & ~(1 << (i - 1)); + res = (res + go(nb, cur, i + 1)) % md; + } + if ((pp[i][cur] == '.' || pp[i][cur] == 'W') && (bp & 1 << (i + n)) > 0 && + (pp[i - 1][cur] == '.' || pp[i - 1][cur] == 'W') && (bp & 1 << (i + n - 1)) > 0) + { + LL nb = bp & ~(1 << (i + n)); + nb = nb & ~(1 << (i + n - 1)); + res = (res + go(nb, cur, i + 1)) % md; + } + } + if ((pp[i][cur] == '.' || pp[i][cur] == 'P') && (bp & 1 << (i + n)) > 0 && + (pp[i][cur + 1] == '.' || pp[i][cur + 1] == 'P') && (bp & 1 << (i)) > 0) + { + LL nb = bp & ~(1 << (i + n)); + nb = nb & ~(1 << i); + res = (res + go(nb, cur, i + 1)) % md; + } + if ((pp[i][cur] == '.' || pp[i][cur] == 'L') && (bp & 1 << (i + n)) > 0) + { + LL nb = bp & ~(1 << (i + n)); + res = (res + go(nb, cur, i + 1)) % md; + } + if (i < n - 1 && n > 1) + { + if ((pp[i][cur + 1] == '.' || pp[i][cur + 1] == 'N') && (bp & 1 << i) > 0 && + (pp[i][cur] == '.' || pp[i][cur] == 'N') && (bp & 1 << (i + n)) > 0 && + (pp[i + 1][cur + 1] == '.' || pp[i + 1][cur + 1] == 'N') && (bp & 1 << (i + 1)) > 0) + { + LL nb = bp & ~(1 << i); + nb = nb & ~(1 << (i + n)); + nb = nb & ~(1 << (i + 1)); + res = (res + go(nb, cur, i + 1)) % md; + } + } + return res; +} +int tmp[10]; + +int main() +{ + LL a, b, c, d, e, f, g, h = 1, x, y, z; + cin >> a; + while (a--) + { + cout << "Case " << h++ << ": "; + cin >> n >> m; + memset(dp, -1, sizeof dp); + set0(pp); + REP(i, n) + { + cin >> pp[i]; + pp[i][m] = '#'; + } + c = 0; + REP(i, n) + { + if (pp[i][1] != '#') + c |= 1 << i; + if (pp[i][0] != '#') + c |= 1 << (i + n); + } + cout << go(c, 0, 0) << endl; + } + return 0; +} diff --git a/11612.cpp b/11612.cpp new file mode 100644 index 0000000..ee6bf7b --- /dev/null +++ b/11612.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +// compute non-x-ing closed path visiting all pts (convex hull) +// deal with the collinear case +// sel btm-most pt a as anchor point; foreach pt p, compute angle of seg (a,p) wrt x-axis +// traverse pt by increasing angle to yield simple closed path + +struct Pt +{ + int no; + long long x, y; +}; +Pt P[1005]; + +bool impossible; +int n; + +long long dist(Pt p1, Pt p2) +{ + long long x = p1.x - p2.x, y = p1.y - p2.y; + return x * x + y * y; +} + +long long x_prod(Pt p1, Pt p2, Pt p3) +{ + return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y); +} + +bool cmp(Pt p1, Pt p2) +{ + long long res = x_prod(P[0], p1, p2); + if (res == 0) + { + return dist(P[0], p1) < dist(P[0], p2); + } + impossible = false; + return res > 0; +} + +bool cmp2(Pt p1, Pt p2) +{ + return dist(P[0], p1) > dist(P[0], p2); +} + +void graham() +{ + int pt_start = 0; + for (int i = 1; i <= n - 1; i++) + { + if (P[i].x < P[pt_start].x) + { + pt_start = i; + } + else if (P[i].x == P[pt_start].x) + { + if (P[i].y < P[pt_start].y) + { + pt_start = i; + } + } + } + Pt tmp = P[pt_start]; + P[pt_start] = P[0]; + P[0] = tmp; + sort(P + 1, P + n, cmp); + int mark = -1; + for (int i = n - 1; i >= 2; i--) + { + if (x_prod(P[0], P[i], P[i - 1]) == 0) + { + mark = i; + } + else if (mark != -1) + { + sort(P + mark - 1, P + n, cmp2); + break; + } + else + { + break; + } + } +} + +int main() +{ + while (scanf("%d", &n) && n) + { + impossible = true; + for (int i = 0; i <= n - 1; i++) + { + scanf("%lld%lld", &P[i].x, &P[i].y); + P[i].no = i; + } + + graham(); + + if (impossible) + { + printf("no solution\n"); + } + else + { + int mark = 0; + for (int i = 0; i < n; i++) + { + if (P[i].no == 0) + { + mark = i; + } + } + printf("0 "); + for (int i = mark + 1; i <= n - 1; i++) + { + printf("%d ", P[i].no); + } + for (int i = 0; i < mark; i++) + { + printf("%d ", P[i].no); + } + printf("0\n"); + } + } + return 0; +} diff --git a/11613.cpp b/11613.cpp new file mode 100644 index 0000000..3aaa221 --- /dev/null +++ b/11613.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +typedef long long LL; + +#define MAXN 220 +#define MAXM MAXN *MAXN * 2 +#define INF 100000000 + +struct node +{ + int v, cap, cost; + node *next, *back; +}; +node *adj[MAXN], edge[MAXM], *cnt; + +bool vis[MAXN]; +int dist[MAXN], prev_[MAXN], aug[MAXN], n, m, s, t, kase; + +void addedge(int i, int j, int k, int cost) +{ + node *p = ++cnt, *q = ++cnt; + p->v = j; + p->cap = k; + p->cost = cost; + p->back = q; + p->next = adj[i]; + adj[i] = p; + q->v = i; + q->cap = 0; + q->cost = -cost; + q->back = p; + q->next = adj[j]; + adj[j] = q; +} +void Read() +{ + int i, procost, maxpro, sellcost, maxsell, maxsave, j; + cnt = &edge[0]; + memset(adj, 0, sizeof(adj)); + scanf("%d%d", &n, &m); + t = n * 2 + 1; + s = 0; + for (i = 1; i <= n; i++) + { + scanf("%d%d%d%d%d", &procost, &maxpro, &sellcost, &maxsell, &maxsave); + addedge(s, i, maxpro, procost); + addedge(i + n, t, maxsell, -sellcost); + for (j = 0; j <= maxsave && i + j <= n; j++) + { + addedge(i, i + j + n, INF, m * j); + } + } +} +bool Spfa() +{ + memset(aug, 0, sizeof(aug)); + memset(dist, 0x3f, sizeof(dist)); + vis[s] = true; + aug[s] = INF; + dist[s] = 0; + queue q; + q.push(s); + int u, v; + node *p; + while (!q.empty()) + { + u = q.front(); + q.pop(); + vis[u] = false; + for (p = adj[u]; p; p = p->next) + { + v = p->v; + if (p->cap > 0 && dist[u] + p->cost < dist[v]) + { + dist[v] = dist[u] + p->cost; + prev_[v] = u; + aug[v] = min(aug[u], p->cap); + if (!vis[v]) + { + vis[v] = true; + q.push(v); + } + } + } + } + return dist[t] <= 0; +} +void Work() +{ + LL sum = 0, ans = INF; + int u, v; + node *p; + while (Spfa()) + { + sum += LL(aug[t]) * dist[t]; + ans = min(ans, sum); + for (v = t; v != s; v = u) + { + u = prev_[v]; + for (p = adj[u]; p; p = p->next) + if (p->v == v && dist[u] + p->cost == dist[v] && p->cap >= aug[t]) + { + p->cap -= aug[t]; + p->back->cap += aug[t]; + break; + } + } + } + printf("Case %d: %lld\n", ++kase, ans > 0 ? 0 : -ans); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + Read(); + Work(); + } + return 0; +} diff --git a/11614.cpp b/11614.cpp new file mode 100644 index 0000000..a5f0f9b --- /dev/null +++ b/11614.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + int T; + unsigned long long n; + scanf("%d", &T); + while (T--) + { + scanf("%llu", &n); + printf("%llu\n", (-1 + sqrt(1 + 4 * n * 2)) / 2); + } + return 0; +} diff --git a/11615.cpp b/11615.cpp new file mode 100644 index 0000000..d8fb6f5 --- /dev/null +++ b/11615.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define pb push_back +#define all(x) x.begin(), x.end() +#define ms(a, v) memset(a, v, sizeof a) +#define II ({int a; scanf("%d", &a); a; }) +#define LL ({Long a; scanf("%lld", &a); a; }) +#define DD ({double a; scanf("%lf", &a); a; }) +#define ff first +#define ss second +#define mp make_pair +#define gc getchar +#define EPS 1e-10 +#define pi 3.1415926535897932384626433832795 + +#define FI freopen("in.txt", "r", stdin) +#define FO freopen("out.txt", "w", stdout) + +typedef long long Long; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +#define __(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << "\t"; + return *this; + } +} dbg; +#define __1D(a, n) \ + rep(i, n) \ + { \ + if (i) \ + printf(" "); \ + cout << a[i]; \ + } +#define __2D(a, r, c, f) \ + forab(i, f, r - !f) \ + { \ + forab(j, f, c - !f) \ + { \ + if (j != f) \ + printf(" "); \ + cout << a[i][j]; \ + } \ + cout << endl; \ + } +/* +template ostream &operator<<(ostream& o, const pair& p){ return o<<"("< ostream& operator<<(ostream& o, const vector& v){ o<<"[";forstl(it,v)o<<*it<<", ";return o<<"]";} //Vector print +template ostream& operator<<(ostream& o, const set& v){ o<<"[";forstl(it,v)o<<*it<<", ";return o<<"]";} //Set print +template inline void MAX(T &a , T b){ if (a < b ) a = b;} +template inline void MIN(T &a , T b){ if (a > b ) a = b;} +*/ +//Fast Reader +template +inline bool read(T &x) +{ + int c = gc(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = gc(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = gc()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +int N, A, B; + +int main() +{ + int T; + read(T); + For(cs, T) + { + read(N), read(A), read(B); + int La = log2(A); + int Lb = log2(B); + int Ans; + if (La > Lb) + Ans = (1 << N) - (1 << (N - La)); + else + Ans = (1 << N) - (1 << (N - Lb)); + printf("%d\n", Ans + 1); + } + return 0; +} diff --git a/11616.cpp b/11616.cpp index d93a6dd..6697080 100644 --- a/11616.cpp +++ b/11616.cpp @@ -1,67 +1,42 @@ -#include -#include -#include -#include -#include -using namespace std; - -int valueOf(char c) { - switch(c) { - case 'M': return 1000; - case 'D': return 500; - case 'C': return 100; - case 'L': return 50; - case 'X': return 10; - case 'V': return 5; - case 'I': return 1; - default: return 0; - } -} +#include -int romanToDecimal(string S) { - int decimalNumeral = 0; - S += "0"; - for(int i = 0; i + 1 < S.length(); i++) { - int current = valueOf(S[i]); - int next = valueOf(S[i + 1]); - if(current >= next) decimalNumeral += current; - else decimalNumeral -= current; - } - return decimalNumeral; -} +using namespace std; -string decimalToRoman(int value) { - string roman[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; - int decimal[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; - - string romanNumeral = ""; - /* - if (value <= 0 || value >= 4000) - return value; - */ - for (int i = 0; i < 13; i++) { - while (value >= decimal[i]) { - value -= decimal[i]; - romanNumeral += roman[i]; - } - } - return romanNumeral; +string int2roman(int n) +{ + string roman; + string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; + string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; + string hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; + string thousands[] = {"", "M", "MM", "MMM"}; + int o = n % 10; + n /= 10; + int t = n % 10; + n /= 10; + int h = n % 10; + n /= 10; + int th = n % 10; + roman += thousands[th] + hundreds[h] + tens[t] + ones[o]; + return roman; } -int main() { - int I; - string S; - stringstream ss; - while (cin >> S) { - if (isdigit(S[0])) { - ss.clear(); - ss << S; - ss >> I; - cout << decimalToRoman(I) << endl; - } - else { - cout << romanToDecimal(S) << endl; - } +int main() +{ + string input; + map m; + for (int i = 1; i < 4000; i++) + { + string roman = int2roman(i); + stringstream strm; + string num; + strm << i; + strm >> num; + m[roman] = num; + m[num] = roman; + } + while (cin >> input) + { + cout << m[input] << endl; } return 0; } diff --git a/11617.cpp b/11617.cpp new file mode 100644 index 0000000..1671df8 --- /dev/null +++ b/11617.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +char g[1024][1024]; +int main() +{ + int testcase; + int n, m; + int i, j, k; + int L[1024], R[1024], C[1024]; + int dp[1024][2]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &m, &n); + int N = 0; + for (i = 0; i < n; i++) + { + scanf("%s", g[i]); + int l, r; + l = r = -1; + for (j = 0; j < m; j++) + { + if ((g[i][j] - '0') % 2) + continue; + if (l == -1) + l = j; + r = j; + } + if (l != -1) + { + L[N] = l; + R[N] = r; + C[N] = i; + N++; + } + } + memset(dp, 0x7f, sizeof(dp)); + if (N == 0) + { + puts("0"); + continue; + } + int ret = C[N - 1]; + for (i = 0; i < N; i++) + { + int pos[2], cost[2]; + if (i == 0) + pos[0] = pos[1] = cost[0] = cost[1] = 0; + else + { + pos[0] = L[i - 1]; + pos[1] = R[i - 1]; + cost[0] = dp[i - 1][0]; + cost[1] = dp[i - 1][1]; + } + int c1, c2; + c1 = abs(pos[0] - R[i]) + R[i] - L[i] + cost[0]; + c2 = abs(pos[1] - R[i]) + R[i] - L[i] + cost[1]; + dp[i][0] = min(c1, c2); + c1 = abs(pos[0] - L[i]) + R[i] - L[i] + cost[0]; + c2 = abs(pos[1] - L[i]) + R[i] - L[i] + cost[1]; + dp[i][1] = min(c1, c2); + } + ret += min(dp[N - 1][0], dp[N - 1][1]); + printf("%d\n", ret); + } + return 0; +} diff --git a/1162.cpp b/1162.cpp new file mode 100644 index 0000000..40c7d64 --- /dev/null +++ b/1162.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int INF = 0x3f3f3f3f; + +int abs(int x) +{ + return x < 0 ? -x : x; +} +int max(int a, int b) +{ + return a > b ? a : b; +} +int min(int a, int b) +{ + return a < b ? a : b; +} + +int dp[502][502], d[601][601]; +char s[20][30], s1[502], s2[502]; + +int main() +{ + int n, m, k, i, j, p, q, l1, l2; + while (~scanf("%d", &n)) + { + getchar(); + for (i = 0; i < n; i++) + { + gets(s[i]); + } + m = strlen(s[0]); + int Top = max(n, m), tmp; + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + { + for (p = 0; p < n; p++) + for (q = 0; q < m; q++) + { + d[s[i][j]][s[p][q]] = Top - max(abs(i - p), abs(j - q)); + } + } + gets(s1 + 1); + l1 = strlen(s1 + 1); + gets(s2 + 1); + l2 = strlen(s2 + 1); + for (i = 0; i <= l1; i++) + { + dp[i][0] = -i * Top; + for (j = 1; j <= l2; j++) + { + dp[i][j] = -INF; + } + } + for (i = 1; i <= l1; i++) + for (j = 1; j <= i; j++) + { + dp[i][j] = max(dp[i - 1][j] - Top, dp[i - 1][j - 1] + d[s1[i]][s2[j]]); + } + printf("%d\n", dp[l1][l2]); + } + return 0; +} diff --git a/11620.cpp b/11620.cpp new file mode 100644 index 0000000..d53dacf --- /dev/null +++ b/11620.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +const int MAXN = 128; +int A[MAXN][MAXN], B[MAXN][MAXN], n; +int mark[MAXN][MAXN]; +void print(char c) +{ + printf("%c\n", c); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + if (mark[i][j]) + printf("%d %d\n", i, j); + } +} +void solve() +{ + for (int i = 0; i < n; i++) + { + int sum = 0, l, r; + for (int j = 0; j < n; j++) + sum += A[i][j]; + l = 0, r = sum; + for (int j = 0; j < n; j++) + { + r -= A[i][j]; + mark[i][j] = (l == r); + l += A[i][j]; + } + } + print('H'); + + for (int i = 0; i < n; i++) + { + int sum = 0, l, r; + for (int j = 0; j < n; j++) + sum += A[j][i]; + l = 0, r = sum; + for (int j = 0; j < n; j++) + { + r -= A[j][i]; + mark[j][i] = (l == r); + l += A[j][i]; + } + } + print('V'); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i - 1 >= 0 && j - 1 >= 0) + B[i][j] = B[i - 1][j - 1] + A[i][j]; + else + B[i][j] = A[i][j]; + } + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int l = 0, r = 0; + if (i - 1 >= 0 && j - 1 >= 0) + l = B[i - 1][j - 1]; + int ex, ey; + ex = i + min(n - 1 - i, n - 1 - j), ey = j + min(n - 1 - i, n - 1 - j); + r = B[ex][ey] - l - A[i][j]; + mark[i][j] = (l == r); + } + } + print('D'); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i - 1 >= 0 && j + 1 < n) + B[i][j] = B[i - 1][j + 1] + A[i][j]; + else + B[i][j] = A[i][j]; + } + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int l = 0, r = 0; + if (i - 1 >= 0 && j + 1 < n) + l = B[i - 1][j + 1]; + int ex, ey; + ex = i + min(n - 1 - i, j), ey = j - min(n - 1 - i, j); + r = B[ex][ey] - l - A[i][j]; + mark[i][j] = (l == r); + } + } + print('A'); +} +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + scanf("%d", &A[i][j]); + + solve(); + } + return 0; +} +/* +3 +3 +1 2 3 +4 5 6 +7 8 9 +3 +1 1 1 +1 1 1 +1 1 1 +4 +5 7 7 6 +2 4 0 8 +6 1 0 7 +6 8 7 5 +*/ diff --git a/11621.cpp b/11621.cpp new file mode 100644 index 0000000..7f78054 --- /dev/null +++ b/11621.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +typedef unsigned long long ULL; + +ULL m; +set s; + +int main() +{ + for (int i = 0; i < 32; i++) + { + for (int j = 0; j < 32; j++) + { + s.insert(pow(2, i) * pow(3, j)); + } + } + while (scanf("%llu", &m) == 1) + { + if (!m) + { + break; + } + FOREACH(s, itr) + { + if (*itr >= m) + { + printf("%llu\n", *itr); + break; + } + } + } + return 0; +} diff --git a/11623.cpp b/11623.cpp new file mode 100644 index 0000000..c8b822a --- /dev/null +++ b/11623.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +const int N = 1000 + 1; +const int adj = 3; +const int di[] = {0, 1, 1, -1}; +const int dj[] = {1, 0, 1, -1}; + +char g[N][N]; + +inline int cons(int i, int j, int n, int m) +{ + int cnt = 0, tmp; + if (m == 1) + { + return g[i][j] != '.'; + } + for (int d = 0; d < adj; ++d) + { + bool flg = true; + tmp = i + di[d] * (m - 1); + if (tmp < 0 || n <= tmp) + { + continue; + } + tmp = j + dj[d] * (m - 1); + if (tmp < 0 || n <= tmp) + { + continue; + } + int l; + for (l = 0; l < m; ++l) + { + int ni = i + di[d] * l; + int nj = j + dj[d] * l; + if (ni < 0 || n <= ni) + { + break; + } + if (nj < 0 || n <= nj) + { + break; + } + if (g[i][j] != g[ni][nj]) + { + break; + } + } + if (m <= l) + { + ++cnt; + while (l--) + { + int ni = i + di[d] * l; + int nj = j + dj[d] * l; + if (l) + { + g[ni][nj] = '.'; + } + } + } + } + return (bool)cnt; + return cnt; +} + +char s1[] = "X WINS"; +char s2[] = "O WINS"; +char s3[] = "DRAW"; +char s4[] = "IN PROGRESS"; +char s5[] = "ERROR"; + +char *solve(int n, int m) +{ + int x = count(&g[0][0], &g[n - 1][n], 'X'); + int o = count(&g[0][0], &g[n - 1][n], 'O'); + int dot = n * n - x - o; + if (!(x == o) && !(x == o + 1)) + { + return s5; + } + int X = x; + int O = o; + x = o = 0; + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + if (g[i][j] == '.') + { + continue; + } + int tmp = cons(i, j, n, m); + if (tmp) + { + if (g[i][j] == 'X') + { + x += tmp; + } + else + { + o += tmp; + } + } + g[i][j] = '.'; + } + } + if ((x && o) || 2 <= x || 2 <= o) + { + return s5; + } + if (x == 1 && o == 0) + { + if (X != O) + { + return s1; + } + else + { + return s5; + } + } + if (x == 0 && o == 1) + { + if (X == O) + { + return s2; + } + else + { + return s5; + } + } + if (x == 0 && o == 0) + { + if (dot == 0) + { + return s3; + } + else + { + return s4; + } + } + return s4; +} + +int main() +{ + int tc; + scanf("%d\n", &tc); + while (tc--) + { + fill(&g[0][0], &g[N - 1][N], '.'); + int n, m; + scanf("%d %d\n", &n, &m); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + scanf("%c", &g[i][j]); + } + scanf("\n"); + } + printf("%s\n", solve(n, m)); + } + return 0; +} diff --git a/11624.cpp b/11624.cpp new file mode 100644 index 0000000..e656000 --- /dev/null +++ b/11624.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +struct position +{ + char id; + int r, c; + int count; +}; + +bool joe_visited[1000][1000]; +bool fire_visited[1000][1000]; + +int bfs(char field[][1000], int r, int c, pair &joe, const vector> &fire) +{ + position tmp; + queue q; + int joe_count = 1; + tmp.id = 'J'; + tmp.r = joe.first; + tmp.c = joe.second; + tmp.count = 0; + joe_visited[tmp.r][tmp.c] = true; + q.push(tmp); + for (int i = 0, sz = fire.size(); i < sz; i++) + { + tmp.id = 'F'; + tmp.r = fire[i].first; + tmp.c = fire[i].second; + tmp.count = 0; + fire_visited[tmp.r][tmp.c] = true; + q.push(tmp); + } + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.id == 'J') + { + if (field[tmp.r][tmp.c] != 'J') + { + continue; + } + if (tmp.r == 0 || tmp.r == r - 1 || tmp.c == 0 || tmp.c == c - 1) + { + return tmp.count + 1; + } + if (tmp.r > 0 && !joe_visited[tmp.r - 1][tmp.c] && field[tmp.r - 1][tmp.c] == '.') + { + joe_visited[tmp.r - 1][tmp.c] = true; + position next(tmp); + next.count++; + next.r--; + q.push(next); + joe_count++; + field[tmp.r - 1][tmp.c] = 'J'; + } + if (tmp.c > 0 && !joe_visited[tmp.r][tmp.c - 1] && field[tmp.r][tmp.c - 1] == '.') + { + joe_visited[tmp.r][tmp.c - 1] = true; + position next(tmp); + next.count++; + next.c--; + q.push(next); + joe_count++; + field[tmp.r][tmp.c - 1] = 'J'; + } + if (tmp.r + 1 < r && !joe_visited[tmp.r + 1][tmp.c] && field[tmp.r + 1][tmp.c] == '.') + { + joe_visited[tmp.r + 1][tmp.c] = true; + position next(tmp); + next.count++; + next.r++; + q.push(next); + joe_count++; + field[tmp.r + 1][tmp.c] = 'J'; + } + if (tmp.c + 1 < c && !joe_visited[tmp.r][tmp.c + 1] && field[tmp.r][tmp.c + 1] == '.') + { + joe_visited[tmp.r][tmp.c + 1] = true; + position next(tmp); + next.count++; + next.c++; + q.push(next); + joe_count++; + field[tmp.r][tmp.c + 1] = 'J'; + } + } + else// tmp.id == 'F' + { + if (tmp.r > 0 && !fire_visited[tmp.r - 1][tmp.c] && field[tmp.r - 1][tmp.c] != '#') + { + fire_visited[tmp.r - 1][tmp.c] = true; + position next(tmp); + next.r--; + q.push(next); + if (field[tmp.r - 1][tmp.c] == 'J') + { + joe_count--; + } + field[tmp.r - 1][tmp.c] = 'F'; + } + if (tmp.c > 0 && !fire_visited[tmp.r][tmp.c - 1] && field[tmp.r][tmp.c - 1] != '#') + { + fire_visited[tmp.r][tmp.c - 1] = true; + position next(tmp); + next.c--; + q.push(next); + if (field[tmp.r][tmp.c - 1] == 'J') + { + joe_count--; + } + field[tmp.r][tmp.c - 1] = 'F'; + } + if (tmp.r + 1 < r && !fire_visited[tmp.r + 1][tmp.c] && field[tmp.r + 1][tmp.c] != '#') + { + fire_visited[tmp.r + 1][tmp.c] = true; + position next(tmp); + next.r++; + q.push(next); + if (field[tmp.r + 1][tmp.c] == 'J') + { + joe_count--; + } + field[tmp.r + 1][tmp.c] = 'F'; + } + if (tmp.c + 1 < c && !fire_visited[tmp.r][tmp.c + 1] && field[tmp.r][tmp.c + 1] != '#') + { + fire_visited[tmp.r][tmp.c + 1] = true; + position next(tmp); + next.c++; + q.push(next); + if (field[tmp.r][tmp.c + 1] == 'J') + { + joe_count--; + } + field[tmp.r][tmp.c + 1] = 'F'; + } + } + if (joe_count == 0) + { + return -1; + } + } + return -1; +} + +int main() +{ + int t, r, c; + char field[1000][1000]; + pair joe; + vector> fire; + cin >> t; + while (t--) + { + cin >> r >> c; + fire.clear(); + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + joe_visited[i][j] = false; + fire_visited[i][j] = false; + cin >> field[i][j]; + if (field[i][j] == 'J') + { + joe.first = i; + joe.second = j; + } + else if (field[i][j] == 'F') + { + fire.push_back(make_pair(i, j)); + } + } + } + int result = bfs(field, r, c, joe, fire); + if (result == -1) + { + cout << "IMPOSSIBLE" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/11625.cpp b/11625.cpp new file mode 100644 index 0000000..3c11e07 --- /dev/null +++ b/11625.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) + +typedef long long ll; + +const ll MOD = 1000000007; + +int K; + +struct mat +{ + ll m[101][101]; + mat() + { + REP(a, 0, K + 1) + REP(b, 0, K + 1) + m[a][b] = 0; + } +}; + +mat operator*(const mat &p, const mat &q) +{ + mat r; + REP(a, 0, K + 1) + REP(b, 0, K + 1) REP(c, 0, K + 1) + { + r.m[a][c] += p.m[a][b] * q.m[b][c]; + r.m[a][c] %= MOD; + } + return r; +} + +mat matexp(const mat &a, ll n) +{ + mat r; + REP(i, 0, K + 1) + r.m[i][i] = 1; + mat e = a; + for (int i = 0; (1ll << i) <= n; i++) + { + if ((n >> i) & 1) + r = r * e; + e = e * e; + } + return r; +} + +ll dp[101][101]; + +mat gen(int s) +{ + mat r; + REP(a0, 0, K + 1) + { + REP(i, 0, K + 1) + dp[0][i] = 0; + dp[0][a0] = 1; + REP(n, 0, s) + { + REP(a, 0, K + 1) + dp[n + 1][a] = 0; + REP(a, 0, K + 1) + { + int b = ((-n - 2 * a) % K + K) % K; + if (b >= 1) + { + dp[n + 1][a] += b * dp[n][a]; + dp[n + 1][a] %= MOD; + } + if (a >= 1) + { + dp[n + 1][a == 1 ? b + 1 : a - 1] += a * dp[n][a]; + dp[n + 1][a == 1 ? b + 1 : a - 1] %= MOD; + } + } + } + REP(a, 0, K + 1) + r.m[a0][a] = dp[s][a]; + } + return r; +} + +int main() +{ + int T; + scanf("%d", &T); + for (; T; T--) + { + ll L; + scanf("%lld%d", &L, &K); + + mat r = matexp(gen(K), L / K) * gen(L % K); + + ll res = 0; + REP(a, 0, K + 1) + { + res += r.m[K][a]; + res %= MOD; + } + printf("%lld\n", res); + } + return 0; +} diff --git a/11626.cpp b/11626.cpp new file mode 100644 index 0000000..ee151cc --- /dev/null +++ b/11626.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +const int MAX = 100010; +struct point +{ + int x, y; +}; +point c[MAX]; +int m; +long long crossProduct(point a, point b, point c)//å‘é‡ ac 在 ab çš„æ–¹å‘ +{ + return (c.x - a.x * 1ll) * (b.y - a.y) - (b.x - a.x * 1ll) * (c.y - a.y); +} +long long disp2p(point a, point b) +{ + return (a.x - b.x * 1ll) * (a.x - b.x) + (a.y - b.y * 1ll) * (a.y - b.y); +} +bool cmp1(point a, point b)// æŽ’åº +{ + long long len = crossProduct(c[0], a, b); + if (len == 0) + { + return disp2p(c[0], a) < disp2p(c[0], b); + } + return len < 0; +} +bool cmp2(point a, point b) +{ + long long len = crossProduct(c[0], a, b); + if (len == 0) + { + return disp2p(c[0], a) > disp2p(c[0], b); + } + return len < 0; +} +void output(point *p, int n) +{ + for (int i = 0; i < n; i++) + { + printf("%d %d\n", p[i].x, p[i].y); + } +} +void solve() +{ + int tmp = 0; + for (int i = 1; i < m; i++) + if (c[i].x < c[tmp].x || c[i].x == c[tmp].x && c[i].y < c[tmp].y) + { + tmp = i; + } + swap(c[0], c[tmp]); + point L[MAX], U[MAX]; + int l = 0, u = 0; + for (int i = 1; i < m; i++) + if (c[i].y <= c[0].y) + { + L[l++] = c[i]; + } + else + { + U[u++] = c[i]; + } + sort(L, L + l, cmp1); + sort(U, U + u, cmp2); + printf("%d\n", m); + printf("%d %d\n", c[0].x, c[0].y); + output(L, l); + output(U, u); +} +int main() +{ + int ncases, n; + int x, y; + char ch; + scanf("%d", &ncases); + while (ncases--) + { + scanf("%d", &n); + m = 0; + for (int i = 0; i < n; i++) + { + scanf("%d %d %c", &x, &y, &ch); + if (ch == 'Y') + { + c[m].x = x; + c[m++].y = y; + } + } + solve(); + } + return 0; +} diff --git a/11628.cpp b/11628.cpp new file mode 100644 index 0000000..cd1282f --- /dev/null +++ b/11628.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int gcd(int m, int w) +{ + if (w == 0) + { + return m; + } + else + { + return gcd(w, m % w); + } +} +main() +{ + int n, m; + while (scanf("%d%d", &n, &m) && n) + { + int total = 0; + int data[n]; + int t; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m - 1; j++) + { + scanf("%d", &t); + } + scanf("%d", &data[i]); + total += data[i]; + } + for (int i = 0; i < n; i++) + { + int g = gcd(total, data[i]); + cout << data[i] / g << " / " << total / g << endl; + } + } +} diff --git a/11629.cpp b/11629.cpp new file mode 100644 index 0000000..15637b2 --- /dev/null +++ b/11629.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int get_operation(char c1, char c2 = ' ') +{ + if (c2 == ' ') + { + switch (c1) + { + case '<': + return 1; + case '>': + return 2; + case '=': + return 3; + } + } + else + { + if (c1 == '<' && c2 == '=') + { + return 4; + } + else if (c1 == '>' && c2 == '=') + { + return 5; + } + } +} + +int main() +{ + int p, g; + char c1, c2; + int operation; + string input; + map m; + int p1, p2; + int n; + cin >> p >> g; + while (p--) + { + cin >> input >> p1 >> c1 >> p2; + m[input] = p1 * 10 + p2; + } + for (int guess = 0; guess < g; guess++) + { + int sum = 0; + while (cin >> input) + { + sum += m[input]; + cin >> c1; + if (c1 == '+') + { + continue; + } + break; + } + if (cin.peek() != ' ') + { + cin >> c2; + operation = get_operation(c1, c2); + } + else + { + operation = get_operation(c1); + } + cin >> n; + n *= 10; + bool correct = true; + switch (operation) + { + case 1: + correct = sum < n; + break; + case 2: + correct = sum > n; + break; + case 3: + correct = sum == n; + break; + case 4: + correct = sum <= n; + break; + case 5: + correct = sum >= n; + break; + } + if (correct) + { + cout << "Guess #" << guess + 1 << " was correct." << endl; + } + else + { + cout << "Guess #" << guess + 1 << " was incorrect." << endl; + } + } + return 0; +} diff --git a/1163.cpp b/1163.cpp new file mode 100644 index 0000000..1e1c35d --- /dev/null +++ b/1163.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +int a[8], n; +int c[8] = {1, 2, 5, 10, 20, 50, 100, 200}; +int t[8], g[8]; + +int judge() +{ + int i, j, k; + for (i = 0; i < n; i++) + if (t[i] < 0) + { + return 0; + } + for (i = 0; i < 8; i++) + if (g[i] < 0) + { + return 0; + } + if (g[2] % 2 == 1 || g[5] % 2 == 1) + { + return 0; + } + g[3] += g[2] / 2; + g[2] = 0; + g[6] += g[5] / 2; + g[5] = 0; + for (i = 0; i < n; i++) + { + int tv = t[i]; + for (j = 7; j >= 0; j--) + { + if (tv >= g[j] * c[j]) + { + tv -= g[j] * c[j]; + g[j] = 0; + } + else + { + g[j] -= tv / c[j]; + tv %= c[j]; + } + } + if (tv != 0) + { + return 0; + } + } + return 1; +} + +int main() +{ + while (1) + { + int i, j, k; + scanf("%d", &n); + if (n == -1) + { + break; + } + for (i = 0; i < 8; i++) + { + scanf("%d", &a[i]); + } + int sum = 0; + for (i = 0; i < 8; i++) + { + sum += c[i] * a[i]; + } + if (sum % n != 0) + { + printf("no\n"); + } + else + { + int flag = 0; + sum /= n; + for (i = 0; i < (1 << n); i++) + for (j = 0; j < (1 << n); j++) + { + for (k = 0; k < n; k++) + { + t[k] = sum; + } + for (k = 0; k < 8; k++) + { + g[k] = a[k]; + } + for (k = 0; k < n; k++) + if (((1 << k) & i) > 0) + { + g[2]--; + t[k] -= 5; + } + for (k = 0; k < n; k++) + if (((1 << k) & j) > 0) + { + g[5]--; + t[k] -= 50; + } + if (judge()) + { + flag = 1; + } + } + if (flag == 1) + { + printf("yes\n"); + } + else + { + printf("no\n"); + } + } + } + return 0; +} diff --git a/11630.cpp b/11630.cpp new file mode 100644 index 0000000..8ce9a56 --- /dev/null +++ b/11630.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define maxn 10010 +#define maxe 201010 +#define INF 1 << 29 +#define LL long long +int main() +{ + int n, a, b; + while (scanf("%d", &n), n) + { + if (n == 3) + { + puts("3 1 2"); + continue; + } + if (n == 4) + { + puts("3 1 4 2"); + continue; + } + a = 5, b = 2; + if (n & 1) + { + printf("3 1 "); + while (a <= n) + { + if (a == n) + { + printf("%d %d %d\n", a, b, n - 1); + break; + } + else + { + printf("%d %d ", a, b); + a += 2; + b += 2; + } + } + } + else + { + printf("3 1 "); + while (a <= n) + { + if (a == n - 1) + { + printf("%d %d %d %d\n", a, b, n, n - 2); + break; + } + else + { + printf("%d %d ", a, b), a += 2, b += 2; + } + } + } + } +} diff --git a/11631.cpp b/11631.cpp new file mode 100644 index 0000000..79fde8d --- /dev/null +++ b/11631.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int m, n; + int a, b, c; + while (cin >> m >> n) + { + if (m == 0 && n == 0) + { + break; + } + int sum = 0; + priority_queue>> edgeList; + for (int i = 0; i < n; i++) + { + cin >> a >> b >> c; + sum += c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + int mst_cost = 0; + UnionFindDisjointSets ds(m); + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + mst_cost += (-front.first); + ds.unionSet(front.second.first, front.second.second); + } + } + cout << sum - mst_cost << endl; + } + return 0; +} diff --git a/11632.cpp b/11632.cpp new file mode 100644 index 0000000..e45c35d --- /dev/null +++ b/11632.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +typedef unsigned int uint; +main() +{ + int n; + while (scanf("%d", &n) && n) + { + uint total = 0, sum[n]; + sum[0] = 0; + for (int i = 0; i < n; i++) + { + int t; + scanf("%d", &t); + if (i) + { + sum[i] = sum[i - 1]; + } + sum[i] += t; + total += t; + } + uint table[n][n]; + int INF = total * (n + n) + 1; + fill(&table[0][0], &table[n - 1][n], INF); + for (int i = 0; i < n; i++) + { + table[0][i] = (1 + i + 1) * (sum[i]);//i+1 bit has already decided + } + for (int i = 0; i < n - 1; i++) + { + for (int j = 0; j < n; j++) + { + for (int k = j + 1; k < n; k++) + { + uint dif = sum[k] - sum[j]; + table[i + 1][k] = min(table[i + 1][k], table[i][j] + (i + 2 + k + 1) * dif); + } + } + } + uint ans = INF; + for (int i = 0; i < n; i++) + { + ans = min(ans, table[i][n - 1]); + } + printf("%d\n", ans); + } +} diff --git a/11633.cpp b/11633.cpp new file mode 100644 index 0000000..02dd619 --- /dev/null +++ b/11633.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define INFINITO 0x3f3f3f3f +#define MAX 1024 + +int fa, fb; +int n; +int f[MAX]; + +int mdc(int a, int b) +{ + return (b == 0 ? a : mdc(b, a % b)); +} + +int calcula(const int &num, const int &den) +{ + int i; + int pa = 0, pb = 0; + for (i = 0; i < n; ++i) + { + int idas = int(ceil( + double(f[i]) / (double(num) / double(den)))); + if (idas > 3) + { + return INFINITO; + } + pa += idas * num - f[i] * den; + pb += idas; + } + return fa * pa + fb * pb * den; +} + +int main() +{ + int i, j; + int a, b; + while (1) + { + scanf("%d", &n); + if (!n) + { + break; + } + scanf("%d %d", &a, &b); + fa = a; + fb = b; + for (i = 0; i < n; ++i) + { + scanf("%d", &f[i]); + } + double menor = INFINITO; + int den, num; + for (i = 1; i <= 100; ++i) + { + for (j = 1; j <= 3; ++j) + { + int x = calcula(i, j); + double p = double(x) / double(j); + if (p < menor) + { + menor = p; + num = x; + den = j; + } + } + } + a = num; + b = den; + int x = mdc(a, b); + a /= x; + b /= x; + if (b > 1) + { + printf("%d / %d\n", a, b); + } + else + { + printf("%d\n", a); + } + } + return 0; +} diff --git a/11634.cpp b/11634.cpp index e4347a8..68d7fdc 100644 --- a/11634.cpp +++ b/11634.cpp @@ -1,20 +1,34 @@ -#include -using namespace std; - -int main(){ - for(;;){ - long long a=0,na=0,cnt=0; - cin>>a; - if(a==0) - break; - long long oa=a; - while(na!=a){ - na=((oa*oa)/100)%10000; - na=oa; - cnt++; - } - cout< + +using namespace std; + +int map_[10001]; + +int main() +{ + int a; + int cnt = 0; + while (scanf("%d", &a) != EOF) + { + memset(map_, 0, sizeof(map_)); + if (!a) + { + break; + } + cnt = 0; + while (1) + { + map_[a]++; + if (map_[a] > 1) + { + break; + } + a *= a; + a /= 100; + a %= 10000; + cnt++; + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/11635.cpp b/11635.cpp new file mode 100644 index 0000000..2ee3ba4 --- /dev/null +++ b/11635.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +#define NODE 10000 +#define HOTEL 102 +class Edge +{ +public: + int t, c; + Edge(){}; + Edge(int tt, int tc) + { + t = tt; + c = tc; + }; +}; + +class Cnode +{ +public: + int now, cost; + Cnode(){}; + Cnode(int nex, int nc) + { + now = nex; + cost = nc; + }; + bool operator<(const Cnode &a) const + { + return cost > a.cost; + } +}; + +vector inp[NODE]; +vector Dedge[HOTEL];//edge for dijkstra +bool ishotel[NODE]; //is the node hotel or start or dest ? +int hotelnum[NODE]; //hotel number + +//graph construct dijkstra +void csDijkstra(int s, int n)//s need to be hotel number +{ + static int INF = 601; + int cost[n]; + fill(cost, cost + n, INF); + priority_queue Q; + cost[s] = 0; + Cnode now(s, 0); + Q.push(now); + while (!Q.empty()) + { + now = Q.top(); + Q.pop(); + if (now.cost > 600) + { + return; + } + if (cost[now.now] < now.cost) + { + continue; + } + if (ishotel[now.now] == true) + { + Dedge[hotelnum[s]].push_back(Edge(hotelnum[now.now], now.cost)); + } + for (int i = 0; i < inp[now.now].size(); i++) + { + int next = inp[now.now][i].t; + int nc = inp[now.now][i].c + now.cost; + if (cost[next] > nc) + { + cost[next] = nc; + Q.push(Cnode(next, nc)); + } + } + } +} + +//find answer dijkstra +class node +{ +public: + int n, t, c; + node() {} + node(int tn, int tt, int tc) + { + n = tn; + t = tt; + c = tc; + } + bool operator<(const node &a) const + { + return c > a.c; + } +}; +int dijkstra(int n, int s, int t)// +{ + int cost[n][601];//pos * time//day not need because of extended graph + static int INF = 1000; + for (int i = 0; i < n; i++) + for (int k = 0; k < 601; k++) + { + cost[i][k] = INF; + } + node now[2]; + priority_queue Q; + Q.push(node(s, 0, 0)); + while (!Q.empty()) + { + now[0] = now[1] = Q.top(); + Q.pop(); + if (cost[now[0].n][now[0].t] < now[0].c) + { + continue; + } + if (now[0].n == t) + { + return now[0].c; + } + //now 1 is one day spent on hotel + now[1].t = 0; + now[1].c++; + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < Dedge[now[i].n].size(); j++) + { + int time = Dedge[now[i].n][j].c; + int next = Dedge[now[i].n][j].t; + if (now[i].t + time > 600) + { + continue; + } + if (cost[next][now[i].t + time] > now[i].c) + { + cost[next][now[i].t + time] = now[i].c; + Q.push(node(next, now[i].t + time, now[i].c)); + } + } + } + } + return -1; +} + +void initial(int n); +int main() +{ + int tn; + while (scanf("%d", &tn) && tn) + { + initial(tn); + int h, hnum = 0, s = 0, g = 0; + scanf("%d", &h); + for (int i = 0; i < h; i++) + { + int t; + scanf("%d", &t); + t--; + ishotel[t] = true; + hotelnum[t] = hnum++; + } + if (ishotel[0] == false) + { + ishotel[0] = true; + hotelnum[0] = hnum; + s = hnum++; + } + else + { + s = hotelnum[0]; + } + if (ishotel[tn - 1] == false) + { + ishotel[tn - 1] = true; + hotelnum[tn - 1] = hnum; + g = hnum++; + } + else + { + g = hotelnum[tn - 1]; + } + int edgenum; + scanf("%d", &edgenum); + for (int i = 0; i < edgenum; i++) + { + int from, to, co; + scanf("%d%d%d", &from, &to, &co); + from--; + to--; + inp[from].push_back(Edge(to, co)); + inp[to].push_back(Edge(from, co)); + } + for (int i = 0; i < tn; i++) + { + if (ishotel[i] == true) + { + csDijkstra(i, tn); + } + } + cout << dijkstra(hnum, s, g) << endl; + } + return 0; +} + +void initial(int n) +{ + for (int i = 0; i < n; i++) + { + inp[i].clear(); + ishotel[i] = false; + hotelnum[i] = -1; + } + for (int i = 0; i < 102; i++) + { + Dedge[i].clear(); + } +} diff --git a/11636.cpp b/11636.cpp index 812d3b8..3fe6f81 100644 --- a/11636.cpp +++ b/11636.cpp @@ -1,21 +1,32 @@ -#include -#include -using namespace std; - -int main(){ - for(int i=1;;i++){ - int n; - cin>>n; - if(n<0) - break; - int copy=0; - for(int j=0;j<=20;j++){ - if((int)pow(2.0,j)>=n){ - copy=j; - break; - } - } - cout<<"Case "< + +using namespace std; + +int main() +{ + int n; + int powers[15]; + int cases = 0; + powers[0] = 1; + for (int i = 1; i < 15; i++) + { + powers[i] = powers[i - 1] * 2; + } + while (cin >> n) + { + if (n < 0) + { + break; + } + cases++; + for (int i = 0; i < 15; i++) + { + if (powers[i] >= n) + { + cout << "Case " << cases << ": " << i << endl; + break; + } + } + } + return 0; +} diff --git a/11637.cpp b/11637.cpp new file mode 100644 index 0000000..03ff160 --- /dev/null +++ b/11637.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define MX 100005 +#define REP(i, n) for (int i = 0; i < (n); i++) +#define OREP(i, n) for (int i = 1; i <= (n); i++) + +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int UINT; + +int n, m, k, t; + +double dp[MX]; +void init() +{ + dp[0] = 1.0 * k / (n - 1); + for (int i = 1; i <= n; i++) + { + dp[i] = dp[i - 1] * (k - i) / (n - 1 - i); + } +} +int main() +{ + int c = 1; + while (scanf("%d %d", &n, &m) && n) + { + k = n - 1 - min(n - 1, m * 2); + init(); + double ret = 0.0; + for (int i = 0; i < n; i++) + { + t = min(i, m) + min(m, n - i - 1); + double p = dp[t - 1]; + if (t == 0) + { + p = 1; + } + ret += 1 - p; + } + printf("Case %d: %.4lf\n", c++, ret); + } + return 0; +} diff --git a/11638.cpp b/11638.cpp new file mode 100644 index 0000000..76415fa --- /dev/null +++ b/11638.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int M; /* the device takes a reading every M seconds. */ + int S; /* The device will instantly take a reading when the ¡¥Startup delay¡¦ ends. */ + int threshold[4]; /* */ + int C; /* */ + int enable[4], trigger[4]; /* trigger 0 <, 1 > */ + int K, time, temp; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &M); + scanf("%d", &S); + for (int i = 0; i < 4; i++) + scanf("%d", &threshold[i]); + scanf("%d", &C); + + for (int i = 0; i < 4; i++) + enable[i] = (C >> i) & 1; + for (int i = 4; i < 8; i++) + trigger[i - 4] = (C >> i) & 1; + + int result[4] = {}; + int now_time = 0, L, R; + scanf("%d", &K); + while (K--) + { + scanf("%d %d", &time, &temp); + L = now_time; + R = now_time + time; + if (R < S) + { + } + else + { + int last_read = R / M * M; + if (L <= last_read && last_read <= R && last_read >= S) + { + for (int i = 0; i < 4; i++) + { + if (trigger[i] == 0) + { + result[i] |= (temp < threshold[i]) & enable[i]; + } + else + { + result[i] |= (temp > threshold[i]) & enable[i]; + } + } + } + } + now_time = R; + } + printf("Case %d:", ++cases); + for (int i = 0; i < 4; i++) + printf(" %s", result[i] ? "yes" : "no"); + puts(""); + } + return 0; +} diff --git a/11639.cpp b/11639.cpp new file mode 100644 index 0000000..1816bab --- /dev/null +++ b/11639.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +main() +{ + int te, tc = 1; + cin >> te; + while (te--) + { + int m[100][100] = {0}, ans[3] = {0}; + for (int l = 0; l < 2; l++) + { + int x1, x2, y1, y2; + cin >> x1 >> y1 >> x2 >> y2; + for (int i = y1; i < y2; i++) + for (int j = x1; j < x2; j++) + { + m[i][j]++; + } + } + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + { + ans[m[i][j]]++; + } + cout << "Night " << tc++ << ": " << ans[2] << " " << ans[1] << " " << ans[0] << endl; + } +} diff --git a/1164.cpp b/1164.cpp new file mode 100644 index 0000000..abf2b49 --- /dev/null +++ b/1164.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define INF 1000000000 +#define MOD 1000000007 + +struct node +{ + int x, y, len; + int mark; +}; +queue q; + +bool vis[40005][30]; +char s[30], hash_[300]; +vector v; +int n, m; +int a[] = {0, 0, 1, -1}, b[] = {1, -1, 0, 0}; + +int f_max(int x, int y) +{ + return x > y ? x : y; +} +int f_min(int x, int y) +{ + return x < y ? x : y; +} +void f_swap(int &x, int &y) +{ + int t; + t = x, x = y, y = t; +} +int f_abs(int x) +{ + return x > 0 ? x : -x; +} + +int bfs(int x, int y, int tx, int ty) +{ + int i, j, k; + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + for (k = 0; k <= 26; k++) + { + vis[i * m + j][k] = false; + } + while (!q.empty()) + { + q.pop(); + } + node top, next; + top.x = x; + top.y = y; + top.len = 0; + top.mark = hash_[v[x][y]]; + vis[top.x * m + top.y][top.mark] = true; + q.push(top); + while (!q.empty()) + { + top = q.front(); + q.pop();//printf("%d %d\n",top.x,top.y); + if (top.x == tx && top.y == ty) + { + return top.len; + } + next.len = top.len + 1; + for (i = 0; i < 4; i++) + { + x = top.x + a[i]; + y = top.y + b[i]; + if (x < 0 || x >= n || y < 0 || y >= m) + { + continue; + } + if (v[x][y] == 'x') + { + continue; + } + else if (v[x][y] == 'o') + { + if (vis[x * m + y][top.mark]) + { + continue; + } + vis[x * m + y][top.mark] = true; + next.x = x; + next.y = y; + next.mark = top.mark; + q.push(next); + } + else if (hash_[v[x][y]] <= top.mark) + { + continue; + } + else + { + if (vis[x * m + y][hash_[v[x][y]]]) + { + continue; + } + vis[x * m + y][hash_[v[x][y]]] = true; + next.x = x; + next.y = y; + next.mark = hash_[v[x][y]]; + q.push(next); + } + } + } + return -1; +} + +char chr[2]; + +int main() +{ + int i, j, k, x, y, tx, ty; + while (~scanf("%s", s)) + { + for (i = 0; i < 26; i++) + { + hash_[s[i]] = i + 1; + } + hash_['o'] = 0; + scanf("%d%d", &m, &n); + string str, p; + v.clear(); + for (i = 0; i < n; i++) + { + str = ""; + for (j = 0; j < m; j++) + { + scanf("%s", chr); + str += chr[0]; + } + v.push_back(str); + } + scanf("%d%d%d%d", &y, &x, &ty, &tx); + int ans = bfs(x, y, tx, ty); + if (ans != -1) + { + printf("%d\n", ans); + } + else + { + printf("IMPOSSIBLE\n"); + } + } + return 0; +} diff --git a/11643.cpp b/11643.cpp new file mode 100644 index 0000000..c3a1d43 --- /dev/null +++ b/11643.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +int dist[1005][1005] = {}, g[16][16], dist2[1005][1005], used[1005][1005] = {}, dp[1 << 16][16]; + +static const int dx[] = {1, 1, -1, -1, 2, 2, -2, -2}; +static const int dy[] = {2, -2, 2, -2, 1, -1, 1, -1}; + +void bfs() +{ + int i, j, k, x, y, tx, ty; + queue X, Y; + X.push(0), Y.push(0); + while (!X.empty()) + { + x = X.front(); + X.pop(); + y = Y.front(); + Y.pop(); + for (i = 0; i < 8; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx > 1000 || ty > 1000) + { + continue; + } + if (dist[tx][ty] == 0) + { + dist[tx][ty] = dist[x][y] + 1; + X.push(tx), Y.push(ty); + } + } + } + dist[0][0] = 0; +} + +int bfs2(int x1, int y1, int x2, int y2, int n) +{ + static int dx[] = {1, 1, -1, -1, 2, 2, -2, -2}; + static int dy[] = {2, -2, 2, -2, 1, -1, 1, -1}; + static int cases = 0; + ++cases; + int i, j, k, x, y, tx, ty; + queue X, Y; + X.push(x1); + Y.push(y1); + dist2[x1][y1] = 0; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + if (dist2[x][y] > 30) + { + break; + } + for (i = 0; i < 8; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 1 || ty < 1 || tx > n || ty > n) + { + continue; + } + if (used[tx][ty] != cases) + { + used[tx][ty] = cases; + dist2[tx][ty] = dist2[x][y] + 1; + if (tx == x2 && ty == y2) + { + return dist2[tx][ty]; + } + X.push(tx), Y.push(ty); + } + } + } + return 0xfffff; +} + +int dfs(int state, int last, int n) +{ + int &ret = dp[state][last]; + if (ret != -1) + { + return ret; + } + if (state == 0) + { + return g[last][0]; + } + ret = 0xfffffff; + for (int i = 0; i < n; i++) + { + if ((state >> i) & 1) + { + ret = min(ret, dfs(state ^ (1 << i), i, n) + g[last][i]); + } + } + return ret; +} + +int main() +{ + bfs(); + int T, t = 0, n, m, i, j, k, x[16], y[16]; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (i = 0; i < m; i++) + { + scanf("%d%d", &x[i], &y[i]); + } + for (i = 0; i < m; i++) + { + for (j = 0; j < m; j++) + { + if (abs(x[i] - x[j]) + abs(y[i] - y[j]) > 20) + { + g[i][j] = dist[abs(x[i] - x[j])][abs(y[i] - y[j])]; + } + else + { + g[i][j] = bfs2(x[i], y[i], x[j], y[j], n); + } + } + g[i][i] = 0; + } + memset(dp, -1, sizeof(dp)); + int ans = dfs((1 << m) - 1 - 1, 0, m); + printf("Case %d: %d\n", ++t, ans); + } + return 0; +} diff --git a/11645.cpp b/11645.cpp index f35bb4f..7ac9602 100644 --- a/11645.cpp +++ b/11645.cpp @@ -1,17 +1,1178 @@ -#include -#include -using namespace std; -int main(){ - long long i; - for(i=1;;i++){ - long long no; - cin>>no; - if(no<0) - break; - long long cnt=0; - long long v= - cout<<"Case "< + +using namespace std; + +#define pb push_back +#define INF 1 << 29 +#define all(x) x.begin(), x.end() +#define ms(a, v) memset(a, v, sizeof a) +#define II ( \ + { \ + int a; \ + scanf("%d", &a); \ + a; \ + }) +#define LL ( \ + { \ + ll a; \ + scanf("%lld", &a); \ + a; \ + }) +#define EPS 1e-10 + +typedef long long ll; +typedef unsgned long long ull; +typedef vector vi; +typedef vector vl; +typedef pair pii; +typedef pair pll; +typedef pair pdd; + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +struct BigInt +{ + char *digits; + int size; // number of used bytes (digits) + int capacity;// size of digits + int sgn; // -1, 0 or +1 + + BigInt(int n, int cap); + + BigInt(int n); + + BigInt(long double d); + + BigInt(); + + BigInt(string s); + void setVal(string s); + + BigInt(const char s[]); + + BigInt(const BigInt &n); + + const BigInt &operator=(const BigInt &n); + const BigInt &operator=(int n); + + ~BigInt(); + + void normalize(); + + static int sig(int n); + + static int sig(long double n); + + inline int length() + { + return size; + } + + BigInt operator++(); + BigInt operator++(int); + BigInt operator--(); + BigInt operator--(int); + BigInt operator-(); + BigInt operator+(int n); + BigInt operator+(BigInt n); + BigInt &operator+=(int n); + BigInt &operator+=(BigInt n); + BigInt operator-(int n); + BigInt operator-(BigInt n); + BigInt &operator-=(int n); + BigInt &operator-=(BigInt n); + BigInt operator*(int n); + BigInt operator*(BigInt n); + void operator*=(int n); + void operator*=(BigInt n); + BigInt operator/(int n); + BigInt operator/(BigInt n); + void operator/=(int n); + void operator/=(BigInt n); + int operator%(int n); + BigInt operator%(BigInt n); + void operator%=(int n); + void operator%=(BigInt n); + int divide(int n); // Divides storing quotient in *this and returning the remainder + BigInt divide(BigInt n); // Divides storing quotient in *this and returning the remainder + BigInt operator*(long double n);// Multiplies by a double and truncates (always under-estimates!) + void operator*=(long double n); // Multiplies by a double and truncates (always under-estimates!) + + BigInt operator<<(int n); + void operator<<=(int n); + BigInt operator>>(int n);// Works differently for negative numbers + void operator>>=(int n); // Works differently for negative numbers + /* + BigInt operator& ( int n ); + BigInt operator& ( BigInt n ); + void operator&= ( int n ); + void operator&= ( BigInt n ); + BigInt operator| ( int n ); + BigInt operator| ( BigInt n ); + void operator|= ( int n ); + void operator|= ( BigInt n ); + BigInt operator^ ( int n ); + BigInt operator^ ( BigInt n ); + void operator^= ( int n ); + void operator^= ( BigInt n ); + BigInt operator~(); +*/ + BigInt operator, (int n); + BigInt operator, (BigInt n); + + bool operator!(); + operator bool(); + //operator int(); //XXX: Don't do this!!! It takes precedence over operator+(int,BigInt) + operator string(); + + bool operator<(BigInt n); + bool operator>(BigInt n); + bool operator==(BigInt n); + bool operator<=(BigInt n); + bool operator>=(BigInt n); + bool operator<(int n); + bool operator>(int n); + bool operator==(int n); + bool operator<=(int n); + bool operator>=(int n); + int compare(BigInt n); + + int toInt(); + + string toString(); + + void print(); + + void printWithCommas(ostream &out); + +private: + void grow(); + + friend istream &operator>>(istream &in, BigInt &n); + friend ostream &operator<<(ostream &out, BigInt n); + + friend long double log2(BigInt x, long double epsilon); + inline friend long double log(BigInt x, long double epsilon); + inline friend long double log10(BigInt x, long double epsilon); + inline friend long double lg(BigInt x, long double epsilon); + inline friend long double ln(BigInt x, long double epsilon); +}; + +BigInt operator+(int m, BigInt &n) +{ + return n + m; +} + +BigInt operator-(int m, BigInt &n) +{ + return -n + m; +} + +BigInt operator*(int m, BigInt &n) +{ + return n * m; +} + +BigInt operator/(int m, BigInt &n) +{ + return BigInt(m) / n; +} + +BigInt operator%(int m, BigInt &n) +{ + return BigInt(m) % n; +} + +inline bool isDigit(int c) +{ + return (c >= (int)'0' && c <= (int)'9'); +} + +istream &operator>>(istream &in, BigInt &n)// FIXME: see inside +{ + n.size = 0; + n.sgn = 1; + int sgn = 1; + int c; + while ((c = in.peek()) >= 0 && + (c == ' ' || c == '\t' || c == '\r' || c == '\n')) + in.get(); + if (c < 0 || (c != (int)'-' && !isDigit(c))) + { + in >> c;// XXX: force in.fail() + return in; + } + if (c == (int)'-') + { + sgn = -1; + in.get(); + } + + // FIXME: Extremely inefficient! Use a string. + while ((c = in.peek()) >= 0 && isDigit(c)) + { + in.get(); + n *= 10; + n += (c - (int)'0'); + } + n.sgn = sgn;//XXX: assgn n.sgn directly after fixing the FIXME + n.normalize(); + return in; +} + +ostream &operator<<(ostream &out, BigInt n)//FIXME: make more efficient +{ + return out << n.toString(); +} + +BigInt::BigInt(int n, int cap) +{ + cap = max(cap, (int)sizeof(n) * 8); + capacity = cap; + sgn = sig(n); + n *= sgn; + digits = new char[cap]; + memset(digits, 0, cap); + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(int n) +{ + capacity = 1024; + sgn = sig(n); + n *= sgn; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + while (n) + { + digits[size++] = n % 10; + n /= 10; + } +} + +BigInt::BigInt(long double d) +{ + capacity = 1024; + sgn = (d < 0 ? -1 : d > 0 ? 1 : 0); + d *= sgn; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = 0; + d = floor(d); + while (d > 0) + { + // digits[size++] = 0 >? ( int )( ( d - floor( d / 10 ) * 10 ) + 0.5 ) > (*this); +} +void BigInt::setVal(string s) +{ + capacity = max((int)s.size(), 16); + sgn = 1; + digits = new char[capacity]; + memset(digits, 0, capacity); + size = s.size(); + for (int i = 0; i < size; i++) + digits[i] = s[i] - '0'; + + // cout<> (*this); +} + +BigInt::BigInt(const BigInt &n) +{ + capacity = n.capacity; + sgn = n.sgn; + size = n.size; + digits = new char[capacity]; + memcpy(digits, n.digits, capacity); +} + +const BigInt &BigInt::operator=(const BigInt &n) +{ + if (&n != this) + { + if (capacity < n.size) + { + capacity = n.capacity; + delete[] digits; + digits = new char[capacity]; + } + sgn = n.sgn; + size = n.size; + memcpy(digits, n.digits, size); + memset(digits + size, 0, capacity - size); + } + return *this; +} + +const BigInt &BigInt::operator=(int n) +{ + sgn = sig(n); + n *= sgn; + for (size = 0; n; size++) + { + digits[size] = n % 10; + n /= 10; + } + return *this; +} + +BigInt::~BigInt() +{ + delete[] digits; +} + +void BigInt::normalize() +{ + while (size && !digits[size - 1]) + size--; + if (!size) + sgn = 0; +} + +int BigInt::sig(int n) +{ + return (n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::sig(long double n) +{ + return (n > 0 ? 1 : (n == 0 ? 0 : -1)); +} + +int BigInt::toInt() +{ + int result = 0; + for (int i = size - 1; i >= 0; i--) + { + result *= 10; + result += digits[i]; + if (result < 0) + return sgn * 0x7FFFFFFF; + } + return sgn * result; +} + +string BigInt::toString() +{ + string s = (sgn >= 0 ? "" : "-"); + for (int i = size - 1; i >= 0; i--) + s += (digits[i] + '0'); + if (size == 0) + s += '0'; + return s; +} + +void BigInt::print()//FIXME: make more efficient +{ + cout << toString(); +} + +void BigInt::printWithCommas(ostream &out) +{ + if (sgn < 0) + out.put('-'); + for (int i = size - 1; i >= 0; i--) + { + out.put(digits[i] + '0'); + if (!(i % 3) && i) + out.put(','); + } + if (size == 0) + out.put('0'); +} + +void BigInt::grow() +{ + char *olddigits = digits; + int oldCap = capacity; + capacity *= 2; + digits = new char[capacity]; + memcpy(digits, olddigits, oldCap); + memset(digits + oldCap, 0, oldCap); + delete[] olddigits; +} + +BigInt BigInt::operator++() +{ + operator+=(1); + return *this; +} + +BigInt BigInt::operator++(int) +{ + return operator++(); +} + +BigInt BigInt::operator--() +{ + operator-=(1); + return *this; +} + +BigInt BigInt::operator--(int) +{ + return operator--(); +} + +BigInt BigInt::operator-() +{ + BigInt result(*this); + result.sgn *= -1; + return result; +} + +BigInt BigInt::operator+(int n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt BigInt::operator+(BigInt n) +{ + BigInt result(*this); + result += n; + return result; +} + +BigInt &BigInt::operator+=(int n) +{ + if (size == capacity) + grow(); + + int nsgn = sig(n); + if (!nsgn) + return *this; + if (!sgn) + sgn = nsgn; + if (sgn == nsgn) + { + n *= nsgn; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] + dig + carry; + digits[i] = newdig % 10; + carry = newdig / 10; + n /= 10; + } + size = max(i, size); + } + else + operator-=(-n); + return *this; +} + +BigInt &BigInt::operator+=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) + grow();//FIXME: this is stupid + + if (!n.sgn) + return *this; + if (!sgn) + sgn = n.sgn; + if (sgn == n.sgn) + { + int carry = 0; + int i; + for (i = 0; i < maxS - 1 || carry; i++) + { + int newdig = carry; + if (i < size) + newdig += digits[i]; + if (i < n.size) + newdig += n.digits[i]; + digits[i] = newdig % 10; + carry = newdig / 10; + } + size = max(i, size); + } + else + { + n.sgn *= -1; + operator-=(n); + n.sgn *= -1; + } + return *this; +} + +BigInt BigInt::operator-(int n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt BigInt::operator-(BigInt n) +{ + BigInt result(*this); + result -= n; + return result; +} + +BigInt &BigInt::operator-=(int n) +{ + if (size == capacity) + grow(); + + int nsgn = sig(n); + if (!nsgn) + return *this; + if (!sgn) + sgn = 1; + if (sgn == nsgn) + { + BigInt bin = n; + if (sgn >= 0 && *this < bin || sgn < 0 && *this > bin) + { + // Subtracting a bigger number + operator=(toInt() - n); + return *this; + } + + n *= nsgn; + int carry = 0; + int i; + for (i = 0; n || carry; i++) + { + int dig = n % 10; + int newdig = digits[i] - dig + carry; + if (newdig < 0) + newdig += 10, carry = -1; + else + carry = 0; + digits[i] = newdig; + n /= 10; + } + normalize(); + } + else + operator+=(-n); + return *this; +} + +BigInt &BigInt::operator-=(BigInt n) +{ + int maxS = max(size, n.size) + 1; + while (maxS >= capacity) + grow();//FIXME: this is stupid + + if (!n.sgn) + return *this; + if (!sgn) + sgn = 1; + if (sgn == n.sgn) + { + if (sgn >= 0 && *this < n || sgn < 0 && *this > n) + { + // Subtracting a bigger number + BigInt tmp = n; + tmp -= *this; + *this = tmp; + sgn = -sgn; + return *this; + } + + int carry = 0; + int i; + for (i = 0; i < maxS - 1; i++) + { + int newdig = carry; + if (i < size) + newdig += digits[i]; + if (i < n.size) + newdig -= n.digits[i]; + if (newdig < 0) + newdig += 10, carry = -1; + else + carry = 0; + digits[i] = newdig; + } + if (carry)// Subtracted a bigger number, need to flip sgn + { + if (i) + digits[0] = 10 - digits[0]; + size = (i ? 1 : 0); + for (int j = 1; j < i; j++) + { + digits[j] = 9 - digits[j]; + if (digits[i]) + size = j + 1; + } + sgn *= -1; + } + normalize(); + } + else + { + n.sgn *= -1; + operator+=(n); + n.sgn *= -1; + } + return *this; +} + +BigInt BigInt::operator*(int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + int nsgn = sig(n); + n *= nsgn; + result.sgn = sgn * nsgn; + if (!result.sgn) + return result; + + int i, j; + for (i = 0; n; i++) + { + int dig = n % 10; + if (dig) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = result.digits[i + j] + (j < size ? dig * digits[j] : 0) + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + n /= 10; + } + result.size = i + j - 1; + return result; +} + +BigInt BigInt::operator*(BigInt n) +{ + BigInt result(0, size + n.size); + + result.sgn = sgn * n.sgn; + if (!result.sgn) + return result; + + int i, j; + for (i = 0; i < n.size; i++) + { + if (n.digits[i]) + { + int carry = 0; + for (j = 0; j < size || carry; j++) + { + int newDig = + result.digits[i + j] + + (j < size ? n.digits[i] * digits[j] : 0) + + carry; + result.digits[i + j] = newDig % 10; + carry = newDig / 10; + } + } + } + result.size = i + j - 1; + + return result; +} + +void BigInt::operator*=(int n) +{ + operator=(operator*(n)); +} + +void BigInt::operator*=(BigInt n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator/(int n) +{ + if (!n) + n /= n;//XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +BigInt BigInt::operator/(BigInt n) +{ + if (!n) + n.size /= n.size;//XXX: force a crash + + BigInt result(*this); + result /= n; + return result; +} + +void BigInt::operator/=(int n) +{ + divide(n); +} + +void BigInt::operator/=(BigInt n) +{ + divide(n); +} + +int BigInt::operator%(int n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(int n) +{ + operator=(divide(n)); +} + +BigInt BigInt::operator%(BigInt n) +{ + BigInt tmp(*this); + return tmp.divide(n); +} + +void BigInt::operator%=(BigInt n) +{ + operator=(divide(n)); +} + +int BigInt::divide(int n) +{ + if (!n) + n /= n;//XXX: force a crash + + int nsgn = sig(n); + n *= nsgn; + if (!sgn) + return 0; + sgn *= nsgn; + + int tmp = 0; + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = tmp / n; + tmp -= digits[i] * n; + } + normalize(); + return tmp; +} + +BigInt BigInt::divide(BigInt n) +{ + if (!n) + n.size /= n.size;//XXX: force a crash + + if (!sgn) + return 0; + sgn *= n.sgn; + + int oldSign = n.sgn; + n.sgn = 1; + + BigInt tmp(0, size); + for (int i = size - 1; i >= 0; i--) + { + tmp *= 10; + tmp += digits[i]; + digits[i] = 0; + while (tmp >= n) + { + tmp -= n; + digits[i]++; + } + } + normalize(); + + n.sgn = oldSign; + + return tmp; +} + +// This is only exact to the first 15 or so digits, but it is +// never an over-estimate +BigInt BigInt::operator*(long double n) +{ + // the number of digits after the decimal point to use + int DIGS_AFTER_DOT = 15; + + int nsgn = sig(n); + n *= nsgn; + int ndigs = n >= 1 ? (int)log10(n) + 1 : 0; + BigInt result(0, size + ndigs); + result.sgn = sgn * nsgn; + if (!result.sgn) + return result; + + if (n >= 1) + for (int i = 0; i < ndigs; i++) + n /= 10; + result.size = 0; + + char afterDot[DIGS_AFTER_DOT + 1]; + memset(afterDot, 0, sizeof(afterDot)); + + // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point + for (int i = ndigs - 1; i >= -DIGS_AFTER_DOT; i--) + { + n *= 10; + int dig = (int)floor(n); + n -= dig; + if (!dig) + continue; + + int carry = 0; + for (int j = 0; j < size || carry; j++) + { + int newdig = + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) + dig * digits[j] + carry; + (i + j < 0 ? afterDot[-(i + j)] : result.digits[i + j]) = newdig % 10; + // if( i + j >= 0 && result.digits[i + j] ) result.size >?= i + j + 1; + carry = newdig / 10; + } + } + if (!result.size) + result.sgn = 0; + return result; +} + +void BigInt::operator*=(long double n) +{ + operator=(operator*(n)); +} + +BigInt BigInt::operator<<(int n) +{ + BigInt result(*this); + result <<= n; + return result; +} + +void BigInt::operator<<=(int n) +{ + if (n < 0) + operator>>=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) + mult *= 2; + } + operator*=(mult); + } +} + +BigInt BigInt::operator>>(int n) +{ + BigInt result(*this); + result >>= n; + return result; +} + +void BigInt::operator>>=(int n) +{ + if (n < 0) + operator<<=(-n); + else if (n > 0) + { + BigInt mult(1, 4 * n); + for (int i = (1 << 30); i; i >>= 1) + { + mult *= mult; + if (n & i) + mult *= 2; + } + operator/=(mult); + } +} +/* +BigInt BigInt::operator&( int n ) +{ +} + +BigInt BigInt::operator&( BigInt n ) +{ +} + +void BigInt::operator&=( int n ) +{ +} + +void BigInt::operator&=( BigInt n ) +{ +} + +BigInt BigInt::operator|( int n ) +{ +} + +BigInt BigInt::operator|( BigInt n ) +{ +} + +void BigInt::operator|=( int n ) +{ +} + +void BigInt::operator|=( BigInt n ) +{ +} + +BigInt BigInt::operator^( int n ) +{ +} + +BigInt BigInt::operator^( BigInt n ) +{ +} + +void BigInt::operator^=( int n ) +{ +} + +void BigInt::operator^=( BigInt n ) +{ +} + +BigInt BigInt::operator~() +{ +} +*/ +BigInt BigInt::operator, (int n) +{ + BigInt result(0, size + (int)sizeof(n) * 8); + for (result.size = 0; n; result.size++) + { + result.digits[result.size] = n % 10; + n /= 10; + } + memcpy(result.digits + result.size, digits, size * sizeof(digits[0])); + result.size += size; + result.sgn = 1; + result.normalize(); + return result; +} + +BigInt BigInt::operator, (BigInt n) +{ + BigInt result(0, size + n.size); + memcpy(result.digits, n.digits, n.size * sizeof(n.digits[0])); + memcpy(result.digits + n.size, digits, size * sizeof(digits[0])); + result.size = size + n.size; + result.sgn = 1; + result.normalize(); + return result; +} + +bool BigInt::operator!() +{ + return !size; +} + +BigInt::operator bool() +{ + return size; +} + +//BigInt::operator int() +//{ +// return toInt(); +//} + +BigInt::operator string() +{ + return toString(); +} + +bool BigInt::operator<(BigInt n) +{ + return (compare(n) < 0); +} + +bool BigInt::operator>(BigInt n) +{ + return (compare(n) > 0); +} + +bool BigInt::operator==(BigInt n) +{ + return (compare(n) == 0); +} + +bool BigInt::operator<=(BigInt n) +{ + return (compare(n) <= 0); +} + +bool BigInt::operator>=(BigInt n) +{ + return (compare(n) >= 0); +} + +bool BigInt::operator<(int n) +{ + return (compare(BigInt(n)) < 0); +} + +bool BigInt::operator>(int n) +{ + return (compare(BigInt(n)) > 0); +} + +bool BigInt::operator==(int n) +{ + return (compare(BigInt(n)) == 0); +} + +bool BigInt::operator<=(int n) +{ + return (compare(BigInt(n)) <= 0); +} + +bool BigInt::operator>=(int n) +{ + return compare(BigInt(n)) >= 0; +} + +int BigInt::compare(BigInt n) +{ + if (sgn < n.sgn) + { + return -1; + } + if (sgn > n.sgn) + { + return 1; + } + if (size < n.size) + { + return -sgn; + } + if (size > n.size) + { + return sgn; + } + for (int i = size - 1; i >= 0; i--) + { + if (digits[i] < n.digits[i]) + { + return -sgn; + } + else if (digits[i] > n.digits[i]) + { + return sgn; + } + } + return 0; +} + +bool V[70][2][2][2][70]; +int N; +string Str; +BigInt dp[70][2][2][2][70]; + +string i2b(ll a) +{ + string str = ""; + do + { + str += (a & 1) + '0'; + a >>= 1; + } while (a); + reverse(all(str)); + return str; +} + +BigInt solve(int pos, bool hasSmall, bool start, bool prev, int countOne) +{ + if (pos == N) + { + return BigInt(countOne); + } + BigInt &ret = dp[pos][hasSmall][start][prev][countOne]; + if (V[pos][hasSmall][start][prev][countOne]) + { + return ret; + } + V[pos][hasSmall][start][prev][countOne] = true; + int lim = hasSmall ? 1 : Str[pos] - '0'; + ret = 0; + if (start) + { + For(i, lim) + { + ret += solve(pos + 1, hasSmall || (i < lim), false, true, countOne + (i == 1 && prev));// placing 0 + } + ret += solve(pos + 1, true, true, false, 0); + } + else + { + forab(i, 0, lim) + { + ret += solve(pos + 1, hasSmall || (i < lim), false, i, countOne + (i == 1 && prev)); + } + } + return ret; +} + +int main() +{ + int cs = 1; + ll a; + while (scanf("%lld", &a) == 1 && a >= 0) + { + Str = i2b(a); + N = Str.size(); + ms(V, false); + BigInt ret = solve(0, false, true, false, 0); + printf("Case %d: ", cs++); + cout << ret << endl; + } +} diff --git a/11646.cpp b/11646.cpp new file mode 100644 index 0000000..9544e11 --- /dev/null +++ b/11646.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + int a, b, t = 1; + double ang, x; + while (scanf("%d : %d", &a, &b) == 2) + { + ang = acos(double(a * a - b * b) / (a * a + b * b)); + x = 400 / (2 * a + ang * sqrt(a * a + b * b)); + printf("Case %d: %.10f %.10f\n", t++, a * x, b * x); + } + return 0; +} diff --git a/11648.cpp b/11648.cpp new file mode 100644 index 0000000..fa66a96 --- /dev/null +++ b/11648.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +double solve(double a, double b, double c) +{ + double D = b * b - 4 * a * c; + return (-b + sqrt(D)) / (2 * a); +} + +int main() +{ + double AB; + double CD; + double AD; + double BC; + double Height; + double s; + double S; + double degree; + int Land; + scanf("%d", &Land); + for (int i = 1; i <= Land; i++) + { + scanf("%lf %lf %lf %lf", &AB, &CD, &AD, &BC); + degree = AB - CD; + s = (AD + (degree) + BC) / 2; + S = sqrt(s * (s - AD) * (s - (degree)) * (s - BC)); + Height = S * 2 / (degree); + S = (CD + AB) * Height / 2; + S = S / 2; + s = solve(degree, CD * Height * 2, -S * Height * 2); + printf("Land #%d: %lf %lf\n", i, ((Height - s) * AD) / Height + 1e-7, ((Height - s) * BC) / Height + 1e-7); + } + return 0; +} diff --git a/11649.cpp b/11649.cpp new file mode 100644 index 0000000..7ae3a51 --- /dev/null +++ b/11649.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +struct Home +{ + int h, r; + Home(int a = 0, int b = 0) : h(a), r(b) {} +}; +vector home; + +int A, B, C, E, F, G, H, I, J, NP, NH; +vector pillar; + +bool cmp1(int a, int b) +{ + return a < b; +} + +bool cmp2(Home h1, Home h2) +{ + return h1.h < h2.h; +} + +struct cmp +{ + bool operator()(Home h1, Home h2) + { + return h1.r > h2.r; + } +}; + +int main() +{ + int T; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + home.clear(); + pillar.clear(); + + scanf("%d%d%d%d%d%d%d%d%d%d%d", &NP, &NH, &A, &B, &C, &E, &F, &G, &H, &I, &J); + pillar.push_back(C % 10000 + 1); + if (NP >= 2) + { + pillar.push_back((A * pillar[0] + C) % 10000 + 1); + } + for (int i = 2; i < NP; i++) + { + pillar.push_back((A * pillar[i - 1] + B * pillar[i - 2] + C) % 10000 + 1); + } + home.push_back(Home(G % 10000 + 1, J % 100000 + 1)); + for (int i = 1; i < NH; i++) + { + home.push_back(Home((E * home[i - 1].h + F * home[i - 1].r + G) % 10000 + 1, (H * home[i - 1].r + I * home[i - 1].h + J) % 100000 + 1)); + } + sort(pillar.begin(), pillar.end(), cmp1); + sort(home.begin(), home.end(), cmp2); + + printf("Case %d: ", i); + + priority_queue, cmp> q; + int h_idx = 0, p_idx = 0, ans = 0; + while (p_idx < NP) + { + while (h_idx < NH && home[h_idx].h <= pillar[p_idx]) + { + if (home[h_idx].r <= NP - p_idx) + { + q.push(home[h_idx]); + } + h_idx++; + } + if (!q.empty()) + { + Home home_t = q.top(); + q.pop(); + home_t.r--; + if (home_t.r <= 0) + { + ans++; + } + else + { + q.push(home_t); + } + } + p_idx++; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11650.cpp b/11650.cpp index 322bef8..4bb2299 100644 --- a/11650.cpp +++ b/11650.cpp @@ -1,23 +1,27 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i + +using namespace std; + +int main() +{ + int t; + int h, m; + char c; + cin >> t; + while (t--) + { + cin >> h >> c >> m; + int new_h = (h == 12 ? 12 : 12 - h); + if (m > 0) + { + new_h--; + if (new_h == 0) + { + new_h = 12; + } + } + int new_m = (m == 0 ? 0 : 60 - m); + printf("%02d:%02d\n", new_h, new_m); + } + return 0; +} diff --git a/11651.cpp b/11651.cpp new file mode 100644 index 0000000..8cfa3da --- /dev/null +++ b/11651.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +const unsigned long long mod = 1LLU << 32; + +struct Matrix +{ + unsigned long long v[150][150]; + int row, col;// row x col + Matrix(int n, int m, long long a = 0) + { + memset(v, 0, sizeof(v)); + row = n, col = m; + for (int i = 0; i < row && i < col; i++) + { + v[i][i] = a; + } + } + Matrix operator*(const Matrix &x) const + { + Matrix ret(row, x.col); + for (int i = 0; i < row; i++) + { + for (int k = 0; k < col; k++) + { + if (v[i][k]) + { + for (int j = 0; j < x.col; j++) + { + ret.v[i][j] += v[i][k] * x.v[k][j], + ret.v[i][j] %= mod; + } + } + } + } + return ret; + } + Matrix operator^(const int &n) const + { + Matrix ret(row, col, 1), x = *this; + int y = n; + while (y) + { + if (y & 1) + { + ret = ret * x; + } + y = y >> 1, x = x * x; + } + return ret; + } +}; + +int main() +{ + int T, t = 0, base, score; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &base, &score); + printf("Case %d: ", ++t); + int N = (base - 1) * (base - 1); + + unsigned long long dp[64][64] = {};// [sum][tail_digit] = #way + for (int i = 0; i <= N; i++) + { + dp[0][i] = 1; + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < base; j++) + { + for (int k = 0; k < base; k++) + { + int d = (j - k) * (j - k); + if (i + d > N || j == k) + { + continue; + } + dp[i + d][k] += dp[i][j]; + dp[i + d][k] %= mod; + } + } + } + + if (score <= N) + { + unsigned long long ret = 0; + for (int i = 1; i < base; i++) + { + ret = (ret + dp[score][i]) % mod; + } + printf("%llu\n", ret); + continue; + } + int r, c; + r = c = (base - 1) * (base - 1) * base; + Matrix x(r, c), y(c, 1); + for (int i = 1; i <= N; i++) + { + for (int j = 0; j < base; j++) + { + y.v[(i - 1) * base + j][0] = dp[i][j]; + } + } + for (int i = base; i < r; i++) + { + x.v[i - base][i] = 1; + } + for (int i = 0; i < base; i++) + { + for (int j = 0; j < base; j++) + { + if (i == j) + { + continue; + } + int d = N - (i - j) * (i - j); + x.v[(N - 1) * base + i][d * base + j] = 1; + } + } + Matrix z = (x ^ (score - N)) * y; + unsigned long long ret = 0; + for (int i = 1; i < base; i++) + { + ret = (ret + z.v[(N - 1) * base + i][0]) % mod; + } + printf("%llu\n", ret); + } + return 0; +} diff --git a/11653.cpp b/11653.cpp new file mode 100644 index 0000000..3e8bd5b --- /dev/null +++ b/11653.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +typedef unsigned long long ll; +typedef unsigned long long ull; +typedef long double ld; + +#define MP make_pair +#define PB push_back +#define SZ(v) (v).size() +#define ALL(v) (v).begin(), (v).end() +#define FOREACH(it, v) \ + for (__typeof(v.begin()) it = v.begin(); it != v.end(); ++it) +#define FOR(i, a, b) for (int i = a; i < b; i++) +#define FORE(i, a, b) for (int i = a; i <= b; i++) + +typedef pair ii; +typedef vector vii; +typedef vector vvii; +typedef vector vi; + +#define MAX 1000010 +ll p[4]; +// ll vals[MAX]; +ll QQ[100010]; + +struct interv +{ + int a; + char beg; + char val; + int last; +}; + +interv pts[200010]; +int npts; + +bool compar(const interv &a, + const interv &b) +{ + return a.a < b.a; +} + +int main() +{ + int tst; + scanf("%d", &tst); + FORE(f, 1, tst) + { + memset(p, 0, sizeof(p)); + int N, M; + scanf("%d %d", &N, &M); + npts = 0; + while (M--) + { + int v, a, b; + scanf("%d %d %d", &v, &a, &b); + pts[npts].a = a; + pts[npts].beg = 1; + pts[npts].val = v; + pts[npts].last = a; + npts++; + pts[npts].a = b + 1; + pts[npts].beg = -1; + pts[npts].val = v; + pts[npts].last = a; + npts++; + } + sort(pts, pts + npts, compar); + printf("Case %d:\n", f); + int Q; + scanf("%d", &Q); + FOR(i, 0, Q) + scanf("%lld", &QQ[i]); + int nq = 0; + int ind = 0; + ll vals = 0; + FORE(i, 0, N) + { + if (nq >= Q) + { + break; + } + while (ind < npts && + pts[ind].a <= i) + { + ll A = pts[ind].last; + if (pts[ind].val == 1) + { + if (pts[ind].beg == 1) + { + p[0] -= A; + p[1] += 1; + } + else + { + p[0] += A; + p[1] -= 1; + } + } + else if (pts[ind].val == 2) + { + if (pts[ind].beg == 1) + { + p[0] += A * A; + p[1] -= 2 * A; + p[2] += 1; + } + else + { + p[0] -= A * A; + p[1] += 2 * A; + p[2] -= 1; + } + } + else + { + if (pts[ind].beg == 1) + { + p[0] -= A * A * A; + p[1] += 3 * A * A; + p[2] -= 3 * A; + p[3] += 1; + } + else + { + p[0] += A * A * A; + p[1] -= 3 * A * A; + p[2] += 3 * A; + p[3] -= 1; + } + } + ind++; + } + long long calc = 0; + for (int j = 3; j >= 0; j--) + { + calc *= i; + calc += (long long)p[j]; + } + vals = calc + vals; + while ((int)QQ[nq] == i) + { + cout << vals << endl, nq++; + } + } + } + return 0; +} diff --git a/11654.cpp b/11654.cpp new file mode 100644 index 0000000..50cb1e3 --- /dev/null +++ b/11654.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES// exposes constants, eg. M_PI +#define S(n) scanf("%d", &n) +#define SLL(n) scanf("%lld", &n) +#define SD(n) scanf("%lf", &n) +#define SS(n) scanf("%s", n) +#define INF INT_MAX +#define LINF LLONG_MAX +#define EPS std::numeric_limits::epsilon() +#define ABS(x) ((x) < 0 ? -(x) : (x)) +#define FOR(i, a, b) for (int i = a; i < b; ++i) +#define RFOR(i, a, b) for (int i = (a)-1, _b(b); i >= _b; --i) +#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) +#define RREP(i, n) for (int(i) = n; (i) >= (int)0; --(i)) +#define DREP(a) \ + sort(all(a)); \ + a.erase(unique(all(a)), a.end()) +#define INDEX(arr, ind) (lower_bound(all(arr), ind) - arr.begin()) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr) +#define mp make_pair +#define pb push_back +#define tri(a, b, c) mp(a, mp(b, c)) +#define fst first +#define snd second +//#define FILL(a,v) memset(a,v,sizeof(a)) +#define SQT(a) ((a) * (a)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define ALL(x) x.begin(), x.end() +#define SZ(v) ((int)(v.size())) +#define LOG(a) (cerr << "\tline#" << __LINE__ << ": " #a " = " << (a) << endl) +#define DBG(args...) \ + { \ + cerr << "\tcase#" << (cc + 1) << ": "; \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << " "; + return *this; + } +} dbg; +typedef long long LL; +typedef long long ll; +typedef unsigned long long ULL; +typedef unsigned int uint; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VS; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; +typedef complex pt; +typedef complex pti; +const double PI = M_PI; +template +inline T gcd(T a, T b) +{ + return b ? gcd(b, a % b) : a; +} +inline double round(double x) +{ + const double sd = 100;// accuracy = 2 dp + return int(x * sd + (x < 0 ? -0.5 : 0.5)) / sd; +} +inline int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} +template +inline void print(T *array, int size) +{ + printf("\tarray[] = ["); + for (int i = 0; i < size; ++i) + { + printf("%lld", (LL)array[i]); + if (i < size - 1) + { + printf(" "); + } + } + printf("]\n"); +} +template +inline void print(T **array, int size1, int size2) +{ + printf("\tarray[][] = ["); + for (int i = 0; i < size1; ++i) + { + for (int j = 0; j < size2; ++j) + { + printf("%lld", (LL)array[i][j]); + if (j < size2 - 1) + { + printf(" "); + } + } + printf("]\n"); + } +} +///////////////////////////////////////////////////////////////////////////// +const int MOD = 10000007; +const int MAXN = 252; +int T, n; +LL v[MAXN]; + +int main() +{ + //clock_t start = clock(); + scanf("%d", &T); + REP(cc, T) + { + scanf("%d", &n); + memset(v, 0, MAXN); + REP(i, n) + { + scanf("%lld", &v[i]); + } + map, ll> m; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < i; j++) + { + ll diff = v[i] - v[j]; + pair ii1 = make_pair(j, diff); + pair ii = make_pair(i, diff); + if (m.count(ii1)) + { + m[ii] += 1 + m[ii1], m[ii] %= MOD; + } + else + { + m[ii]++; + } + } + } + ll ans = n; + map, ll>::iterator it; + for (it = m.begin(); it != m.end(); it++) + { + ans += it->second; + ans %= MOD; + } + printf("Case %d: %lld\n", cc + 1, ans); + } + //fprintf(stderr, "*** Total time: %.3lf seconds ***\n", + // ((clock() - start) / (double) CLOCKS_PER_SEC)); + return 0; +} diff --git a/11655.cpp b/11655.cpp new file mode 100644 index 0000000..f546135 --- /dev/null +++ b/11655.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() + +typedef long long int lli; + +static const lli NODE = 5000 + 1, MOD = 100000; + +lli solve(lli node, vector G[], lli size, lli visited[]) +{ + lli tmp, re = 0; + FOR(e, G[node]) + { + if (visited[*e]) + { + tmp = visited[*e]; + } + else + { + tmp = solve(*e, G, size, visited); + } + re = (re + tmp) % MOD; + } + return visited[node] = re % MOD; +} + +int main() +{ + lli tc, node, edge, src, dst, cnt = 0, visited1[NODE], visited2[NODE]; + for (scanf("%lld\n", &tc); tc--;) + { + scanf("%lld %lld\n", &node, &edge); + + vector G[node], H[node]; + + for (lli i = 0; i < edge; ++i) + { + scanf("%lld %lld\n", &src, &dst); + G[src - 1].push_back(dst - 1); + H[dst - 1].push_back(src - 1); + } + + fill(visited1, visited1 + node, 0); + fill(visited2, visited2 + node, 0); + visited1[node - 1] = visited2[0] = 1; + + solve(0, G, node, visited1); + solve(node - 1, H, node, visited2); + + lli sum = 0; + REP(i, node) + { + FOR(e, G[i]) + { + sum = (sum + visited2[i] * visited1[*e]) % MOD; + } + } + //REP(i, node)cout << visited1[i] << ' ' << visited2[i] << endl; + printf("Case %lld: %lld %lld\n", ++cnt, sum, visited2[node - 1] % MOD); + } + return 0; +} diff --git a/11656.cpp b/11656.cpp new file mode 100644 index 0000000..868168c --- /dev/null +++ b/11656.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +const int MAXC = 1035; + +bool blocked[MAXC][MAXC]; + +vector YgivenX[MAXC], XgivenY[MAXC]; + +inline bool inside(int x, int y) +{ + return (0 <= x and x < MAXC and 0 <= y and y < MAXC); +} + +void markCorner(int x, int y, int m) +{ + for (int xx = x - m; xx <= x + m; ++xx) + { + for (int yy = y - m; yy <= y + m; ++yy) + { + if (!inside(xx, yy)) + { + continue; + } + if ((xx - x) * (xx - x) + (yy - y) * (yy - y) <= m * m)// very close to the border, or ON the border + { + blocked[xx][yy] = true; + } + } + } +} + +void markHorizontalLine(int x0, int x1, int y, int m) +{ + for (int xx = x0; xx <= x1; ++xx) + { + for (int yy = y - m; yy <= y + m; ++yy) + { + if (!inside(xx, yy)) + { + continue; + } + blocked[xx][yy] = true; + } + } +} + +void markVerticalLine(int y0, int y1, int x, int m) +{ + for (int yy = y0; yy <= y1; ++yy) + { + for (int xx = x - m; xx <= x + m; ++xx) + { + if (!inside(xx, yy)) + { + continue; + } + blocked[xx][yy] = true; + } + } +} + +bool canReach(int sx, int sy, int fx, int fy) +{ + assert(inside(sx, sy) and (fx, fy)); + assert(!blocked[sx][sy]); + assert(!blocked[fx][fy]); + queue> q; + blocked[sx][sy] = true; + q.push(make_pair(sx, sy)); + while (q.size()) + { + int cx = q.front().first, cy = q.front().second; + if (cx == fx and cy == fy) + { + return true; + } + q.pop(); + assert(blocked[cx][cy]); + for (int dx = -1; dx <= +1; dx++) + { + for (int dy = -1; dy <= +1; dy++) + { + int nx = cx + dx; + int ny = cy + dy; + if (!inside(nx, ny)) + { + continue; + } + if (blocked[nx][ny]) + { + continue; + } + blocked[nx][ny] = true; + q.push(make_pair(nx, ny)); + } + } + } + return false; +} + +int main() +{ + int n, m; + while (cin >> n >> m) + { + if (n == 0 and m == 0) + { + break; + } + assert(n % 2 == 0); + For(i, 0, MAXC) YgivenX[i].clear(), XgivenY[i].clear(); + memset(blocked, 0, sizeof blocked); + For(i, 0, n) + { + int x, y; + cin >> x >> y; + XgivenY[y].push_back(x); + YgivenX[x].push_back(y); + } + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + For(x, 0, MAXC) + { + sort(YgivenX[x].begin(), YgivenX[x].end()); + assert(YgivenX[x].size() % 2 == 0); + for (int k = 0; k < YgivenX[x].size(); k += 2) + { + int y = YgivenX[x][k]; + int nextY = YgivenX[x][k + 1]; + markVerticalLine(y, nextY, x, m); + markCorner(x, y, m); + markCorner(x, nextY, m); + } + } + For(y, 0, MAXC) + { + sort(XgivenY[y].begin(), XgivenY[y].end()); + assert(XgivenY[y].size() % 2 == 0); + for (int k = 0; k < XgivenY[y].size(); k += 2) + { + int x = XgivenY[y][k]; + int nextX = XgivenY[y][k + 1]; + markHorizontalLine(x, nextX, y, m); + markCorner(x, y, m); + markCorner(nextX, y, m); + } + } + if (canReach(x1, y1, x2, y2)) + { + puts("Yes"); + } + else + { + puts("No"); + } + } + return 0; +} diff --git a/11657.cpp b/11657.cpp new file mode 100644 index 0000000..3efb58a --- /dev/null +++ b/11657.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +bool boom(int m, int n, int x0, int y0, int x1, int y1, int p, int q) +{ + if (p == 0 or q == 0) + { + int c = p * (y1 - y0) - q * (x1 - x0); + return (c == 0); + } + x0 *= abs(q), x1 *= abs(q), m *= abs(q); + y0 *= abs(p), y1 *= abs(p), n *= abs(p); + p /= abs(p); + q /= abs(q); + int d1 = p * (y1 - y0) - q * (x1 - x0); + int d2 = p * (2 * n - y1 - y0) - q * (x1 - x0); + int d3 = p * (y1 - y0) - q * (2 * m - x1 - x0); + int d4 = p * (2 * n - y1 - y0) - q * (2 * m - x1 - x0); + int g = __gcd(2 * n * q, 2 * m * p); + if (d1 % g == 0) + { + return true; + } + if (d2 % g == 0) + { + return true; + } + if (d3 % g == 0) + { + return true; + } + if (d4 % g == 0) + { + return true; + } + return false; +} + +int main() +{ + int m, n, x0, y0, x1, y1, p, q; + while (cin >> m >> n >> x0 >> y0 >> x1 >> y1 >> p >> q) + { + if (m == 0 and n == 0 and x0 == 0 and y0 == 0 and x1 == 0 and y1 == 0 and p == 0 and q == 0) + { + break; + } + if (boom(m, n, x0, y0, x1, y1, p, q)) + { + puts("HIT"); + } + else + { + puts("MISS"); + } + } + return 0; +} diff --git a/11658.cpp b/11658.cpp new file mode 100644 index 0000000..2d56bc3 --- /dev/null +++ b/11658.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define pb push_back +#define PI acos(-1.0) +#define SZ(a) (int)a.size() +#define csprnt printf("Case %d: ", cas++); +#define EPS 1e-9 +#define MAX 100010 +#define ll long long +#define INF (1 << 30) +#define pii pair +#define MP make_pair + +int coin[105], dp[10010]; + +int main() +{ + int t, cas = 1; + // scanf("%d", &t); + while (1) + { + int n, x, now, i, j, dd; + char c; + scanf("%d%d", &n, &x); + if (n == 0 && x == 0) + { + break; + } + for (i = 1; i <= n; i++) + { + scanf("%d%c%d", &now, &c, &dd); + now = (now * 100) + dd; + coin[i] = now; + } + int cur = coin[x]; + if (cur > 5000) + { + double val = 100.00; + printf("%.2lf\n", val); + } + else + { + memset(dp, 0, sizeof dp); + dp[0] = 1; + for (i = 1; i <= n; i++) + { + if (i == x) + { + continue; + } + for (j = 10000; j >= 0; j--) + { + if (dp[j]) + { + dp[j + coin[i]] = 1; + } + } + } + int st = (5000 - cur); + double sol; + for (i = st + 1; i <= 10000; i++) + { + if (dp[i]) + { + sol = (cur * 100.0) / (i + cur); + break; + } + } + printf("%.2lf\n", sol); + } + } + return 0; +} diff --git a/11659.cpp b/11659.cpp new file mode 100644 index 0000000..42374b7 --- /dev/null +++ b/11659.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +bool informants[20]; +int i, a, j, k, ans; + +int main() +{ + while (true) + { + scanf("%d %d", &i, &a); + if ((i | a) == 0) + { + break; + } + memset(informants, true, i); + ans = i; + while (a--) + { + scanf("%d %d", &j, &k); + if (k < 0 && informants[-k - 1]) + { + ans--; + informants[-k - 1] = false; + } + } + printf("%d\n", ans); + } +} diff --git a/1166.cpp b/1166.cpp new file mode 100644 index 0000000..0be1961 --- /dev/null +++ b/1166.cpp @@ -0,0 +1,238 @@ +#include + +using namespace std; + +#pragma comment(linker, "/STACK:102400000,102400000") + +#define ll long long +#define clr(x, c, n) memset(x, c, sizeof(x[0]) * (n)) +#define clr_all(x, c) memset(x, c, sizeof(x)) +#define IT iterator +#define ls rt << 1 +#define rs ls | 1 +#define lson l, mid, ls +#define rson mid + 1, r, rs +#define middle l + r >> 1 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define eps (1e-8) +#define PI 3.1415926535897932384626433832795 +#define E 2.7182818284590452353602874713527 + +template +T _min(T a, T b) +{ + return a < b ? a : b; +} +template +T _max(T a, T b) +{ + return a > b ? a : b; +} +template +T _abs(T a) +{ + return a > 0 ? a : -a; +} +template +T _mod(T a, T m) +{ + return a < m ? (a < 0 ? (a % m + m) % m : a) : a % m; +} +template +T _gcd(T a, T b) +{ + while (b) + { + T t = b; + b = a % b; + a = t; + } + return a; +} +template +void _swap(T &a, T &b) +{ + T t = b; + b = a; + a = t; +} +template +void getmax(T &a, T b) +{ + a = a > b ? a : b; +} +template +void getmin(T &a, T b) +{ + a = (a != -1 && a < b) ? a : b; +} + +const int M = 40000 + 5; + +int TS, cas = 1, n; +map mp; +char s1[M << 5], s2[M << 5], tmp[444], id[33]; +int l1, l2, len[M], tot; +set pos[M]; +int fa[M << 5]; +string ans[M]; + +void getStr(char *s, int &l, int m) +{ + int i, j, k, c; + for (i = 0, l = 0; i < m; i++) + { + gets(tmp); + for (j = 0; tmp[j]; j++) + { + if (tmp[j] == '<') + { + for (j++, k = 0; tmp[j] != ':'; j++, id[k] = 0) + { + id[k++] = tmp[j]; + } + for (j++, c = 0; tmp[j] != '>'; j++) + { + c *= 10, c += tmp[j] - '0'; + } + if ((k = mp[id]) == 0) + { + mp[id] = (k = ++tot); + pos[k].clear(); + len[k] = c; + } + pos[k].insert(l); + for (k = len[k]; k >= 1; k--) + { + s[l++] = '*'; + } + } + else + { + s[l++] = tmp[j]; + } + } + } + s[l] = 0; +} + +int Find(int x) +{ + return x == fa[x] ? x : fa[x] = Find(fa[x]); +} + +bool Union(int x, int y) +{ + int xf = Find(x), yf = Find(y); + if (s1[xf] != '*' && s1[yf] != '*') + { + if (s1[xf] != s1[yf]) + { + return false; + } + fa[xf] = yf; + } + else if (s1[xf] != '*') + { + fa[yf] = xf; + } + else + { + fa[xf] = yf; + } + return true; +} + +bool check() +{ + if (l1 != l2) + { + return false; + } + int i, j, k; + for (i = 0; i < l1; i++) + { + fa[i] = i; + if (s1[i] == '*' && s2[i] != '*') + { + s1[i] = s2[i]; + } + else if (s2[i] == '*' && s1[i] != '*') + { + s2[i] = s1[i]; + } + if (s1[i] != s2[i]) + { + return false; + } + } + for (i = 1; i <= tot; i++) + for (set::IT it = pos[i].begin(); it != pos[i].end(); it++) + for (j = 0; j < len[i]; j++) + if (!Union((*it) + j, (*pos[i].begin()) + j)) + { + return false; + } + for (i = 0; i < l1; i++) + if (s1[i] == '*') + { + s2[i] = s1[i] = s1[Find(i)]; + } + //if(strcmp(s1,s2)) while(1); + for (i = 1; i <= tot; i++) + { + ans[i].clear(); + for (j = 0, k = *pos[i].begin(); j < len[i]; j++) + { + ans[i] += s1[k + j]; + } + } + return true; +} + +void run() +{ + int i, j; + tot = 0; + mp.clear(); + getchar(); + getStr(s1, l1, n); + scanf("%d", &n); + getchar(); + getStr(s2, l2, n); + if (cas > 1) + { + cout << endl; + } + else + { + cas++; + } + if (check()) + { + cout << "yes" << endl; + for (map::IT it = mp.begin(); it != mp.end(); it++) + { + cout << it->first << ' ' << ans[it->second] << endl; + } + } + else + { + cout << "no" << endl; + } +} + +void presof() +{ +} + +int main() +{ + presof(); + while (~scanf("%d", &n)) + { + run(); + } + return 0; +} diff --git a/11660.cpp b/11660.cpp new file mode 100644 index 0000000..e83cbb4 --- /dev/null +++ b/11660.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +char c[2][1200], *c_pos, num, *k, oc; +bool op; +int main() +{ + int i, s_pos, TERM, POS; + while (scanf("%s%d%d", c, &TERM, &POS) && TERM) + { + for (op = i = 1; i < TERM; i++, op = !op) + { + for (c_pos = &c[op][0], oc = '1', num = c[!op][0], k = &c[!op][1]; *k; k++) + { + if (*k == num) + { + ++oc; + } + else + { + *c_pos++ = oc; + *c_pos++ = num; + num = *k; + oc = '1'; + if ((c_pos - &c[op][0]) > POS + 50) + { + break; + } + } + } + *c_pos++ = oc; + *c_pos++ = num; + *c_pos = 0; + } + printf("%c\n", c[!op][POS - 1]); + } + return 0; +} diff --git a/11661.cpp b/11661.cpp index 0cf0939..a751669 100644 --- a/11661.cpp +++ b/11661.cpp @@ -1,61 +1,57 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define INF 1<<30 -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int N, i, j; - cin >> N; - if (N == 0) break; - int preR = INF, preD = INF, mm = INF; - string S; - cin >> S; - FOI(i, 0, N-1) { - if (S[i] == 'Z') { - mm = 0; - break; +int main() +{ + int L; + char c; + while (cin >> L) + { + if (L == 0) + { + break; + } + int prev_d = -1; + int prev_r = -1; + int result = 1000000000; + bool z_found = false; + for (int i = 0; i < L; i++) + { + cin >> c; + if (z_found) + { + continue; } - if (S[i] == 'R') { - preR = i; - mm = min(mm, abs(preR - preD)); + if (c == '.') + { continue; } - if (S[i] == 'D') { - preD = i; - mm = min(mm, abs(preD - preR)); + if (c == 'D') + { + prev_d = i; + if (prev_r != -1 && prev_d - prev_r < result) + { + result = prev_d - prev_r; + } continue; } + if (c == 'R') + { + prev_r = i; + if (prev_d != -1 && prev_r - prev_d < result) + { + result = prev_r - prev_d; + } + continue; + } + if (c == 'Z') + { + z_found = true; + result = 0; + } } - cout << mm << endl; + cout << result << endl; } return 0; } diff --git a/11662.cpp b/11662.cpp new file mode 100644 index 0000000..ac1ccbf --- /dev/null +++ b/11662.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int B, H; + +int myFloor(int a, int b) +{ + if ((a < 0) ^ (b < 0))// negative + { + return -(abs(a) / abs(b)) - 1; + } + else + { + return abs(a) / abs(b); + } +} + +int mod(int a, int b) +{ + return (a % b + b) % b; +} + +int Kh(int x, int y) +{ + return myFloor(y, H); +} + +int Ka(int x, int y) +{ + return myFloor(2 * H * x - B * y, 2 * H * B); +} + +int Kv(int x, int y) +{ + return myFloor(2 * H * x + B * y, 2 * H * B); +} + +bool isVertex(int x, int y) +{ + assert(B % 2 == 0); + if (mod(y, H) != 0) + { + return false; + } + int q = y / H; + if (q % 2 == 0) + { + return mod(x, B) == 0; + } + return mod(x, B) == (B / 2); +} + +int main() +{ + int x1, y1, x2, y2; + while (cin >> B >> H >> x1 >> y1 >> x2 >> y2) + { + if (B == 0 and H == 0 and x1 == 0 and y1 == 0 and x2 == 0 and y2 == 0) + { + break; + } + B *= 2; + x1 *= 2; + x2 *= 2; + int ans = abs(Kh(x1, y1) - Kh(x2, y2)) + abs(Ka(x1, y1) - Ka(x2, y2)) + abs(Kv(x1, y1) - Kv(x2, y2)); + ans++; + int dx = x2 - x1; + int dy = y2 - y1; + int g = __gcd(abs(dx), abs(dy)); + dx /= g; + dy /= g; + assert(!isVertex(x1, y1)); + assert(!isVertex(x2, y2)); + int x = x1, y = y1; + while (x != x2 or y != y2) + { + ans -= 2 * isVertex(x, y); + x += dx; + y += dy; + } + cout << ans << endl; + } + return 0; +} diff --git a/11663.cpp b/11663.cpp new file mode 100644 index 0000000..fe6a007 --- /dev/null +++ b/11663.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +template +string toStr(const T &x) +{ + stringstream s; + s << x; + return s.str(); +} +template +int toInt(const T &x) +{ + stringstream s; + s << x; + int r; + s >> r; + return r; +} + +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define D(x) cout << #x " = " << (x) << endl + +int next(const string &s, int i); +int prev(const string &s, int i); + +bool looksLike0000(const string &s, int i) +{ + for (; i < s.size(); ++i) + if (s[i] != '0') + { + return false; + } + return true; +} + +bool looksLike1000(const string &s, int i) +{ + if (s[i] != '1') + { + return false; + } + for (++i; i < s.size(); ++i) + if (s[i] != '0') + { + return false; + } + return true; +} + +int next(const string &s, int i) +{ + if (i == s.size() - 1) + { + return i; + } + if (s[i] == '0') + { + if (looksLike1000(s, i + 1)) + { + return i; + } + else + { + return next(s, i + 1); + } + } + else + { + if (looksLike0000(s, i + 1)) + { + return i; + } + else + { + return prev(s, i + 1); + } + } +} + +int prev(const string &s, int i) +{ + if (i == s.size() - 1) + { + return i; + } + if (s[i] == '0') + { + if (looksLike0000(s, i + 1)) + { + return i; + } + else + { + return prev(s, i + 1); + } + } + else + { + if (looksLike1000(s, i + 1)) + { + return i; + } + else + { + return next(s, i + 1); + } + } +} + +int main() +{ + int m; + string s; + while (cin >> m >> s && m) + { + while (m--) + { + int where = next(s, 0); + s[where] = s[where] == '0' ? '1' : '0'; + } + cout << s << endl; + } + return 0; +} diff --git a/11664.cpp b/11664.cpp new file mode 100644 index 0000000..bb1bd7d --- /dev/null +++ b/11664.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +/* + Name: Mohamed Mayla + Problem Name: 11664 - Langton's Ant + Problem Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2711 + */ + +#define cnt(x, n) count(x.begin(), x.end(), n) +#define so(x) sort(x.begin(), x.end()) +#define rso(x) sort(x.rbegin(), x.rend()) +#define mx(x) *max_element(x.begin(), x.end()) +#define mn(x) *min_element(x.begin(), x.end()) +#define al(n) (n).begin(), (n).end() +#define ral(n) (n).rbegin(), (n).rend() +#define mp make_pair +#define REP(i, a, n) for (int i = int(a); i < int(n); i++) +#define rREP(i, a, n) for (int i = int(a); i >= int(n); --i) +#define RE(s) freopen(s, "r", stdin) +#define WR(s) freopen(s, "w", stdout) + +int grid[20][20]; +int x, y; +int n, len; +string c; +string bin; +int bb[105]; + +void init_grid() +{ + bin = string(bin.rbegin(), bin.rend()); + //cout<= 0; row--) + { + REP(col, 0, n) + { + cout << grid[row][col] << " "; + } + cout << endl; + } +} + +bool simulate(int y, int x) +{ + int dir = 0;//0 up, 1 right, 2 down, 3 left + while (true) + { + if (x >= n || y >= n || x < 0 || y < 0) + return false; + + if (x == n - 1 && y == n - 1) + return true; + + if (grid[x][y] == 0) + { + dir--; + grid[x][y] = 1; + } + else + { + dir++; + grid[x][y] = 0; + } + + if (dir > 3) + dir = 0; + else if (dir < 0) + dir = 3; + + switch (dir) + { + case 0: + x++; + break; + case 1: + y++; + break; + case 2: + x--; + break; + case 3: + y--; + break; + } + } +} + +int getBit() +{ + int ret = 0; + for (int i = len; i >= 0; i--) + { + ret = ret * 10 + bb[i]; + bb[i] = ret / 2; + ret %= 2; + } + while (len >= 0 && bb[len] == 0) + len--; + return ret; +} + +void init_bin() +{ + len = c.length(); + memset(bb, 0, sizeof(bb)); + for (int i = 0, j = len - 1; i < len; i++, j--) + bb[i] = c[j] - '0'; + REP(i, 0, n * n) + { + bin.push_back(getBit() + '0'); + } +} + +int main() +{ + //RE("in.txt"); + + while (cin >> n >> c >> x >> y && n) + { + bin.clear(); + init_bin(); + init_grid(); + //print_grid(); + + if (simulate(x - 1, y - 1)) + cout << "Yes\n"; + else + cout << "Kaputt!\n"; + } + + return 0; +} diff --git a/11665.cpp b/11665.cpp new file mode 100644 index 0000000..410ee68 --- /dev/null +++ b/11665.cpp @@ -0,0 +1,242 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl +#define EPS 1e-9 + +typedef pair point; +typedef vector polygon; + +vector polygons; + +const int MAXN = 50; +int p[MAXN]; + +int find(int u) +{ + return p[u] == u ? u : p[u] = find(p[u]); +} + +void link(int u, int v) +{ + if (find(u) != find(v)) + { + p[find(u)] = find(v); + } +} + +// ret angle in range [0, 2*Pi) of a given Cartesian pt +// if pt == (0,0), -1.0 is returned +double polarAngle(point p) +{ + double x = p.first, y = p.second; + if (fabs(x) <= EPS && fabs(y) <= EPS) + { + return -1.0f; + } + if (fabs(x) <= EPS) + { + return y > EPS ? 1.0f : 3.0f; + } + double theta = atan(1.0f * y / x); + if (x > EPS) + { + return y >= -EPS ? theta : (4 * acos(0.0f) + theta); + } + return 2 * acos(0.0f) + theta; +} + +// ret true iff p is inside poly. +// PRE: vertices of poly are ordered +// POST: Modify code inside to handle the special case of "on edge". +bool pointInPoly(point p, const polygon &poly) +{ + int n = poly.size(); + double ang = 0.0; + for (int i = n - 1, j = 0; j < n; i = j++) + { + point v(poly[i].first - p.first, poly[i].second - p.second); + point w(poly[j].first - p.first, poly[j].second - p.second); + double va = polarAngle(v); + double wa = polarAngle(w); + double xx = wa - va; + if (va < -0.5 || wa < -0.5 || fabs(fabs(xx) - 2 * acos(0)) < EPS) + { + // POINT IS ON THE EDGE + return true; + assert(false); + ang += 2 * acos(0); + continue; + } + if (xx < -2 * acos(0)) + { + ang += xx + 4 * acos(0); + } + else if (xx > 2 * acos(0)) + { + ang += xx - 4 * acos(0); + } + else + { + ang += xx; + } + } + return (ang * ang > 1.0); +} + +// ret true if point (x, y) lies inside (or in the border) +// of box defined by points (x0, y0) and (x1, y1). +bool point_in_box(double x, double y, + double x0, double y0, + double x1, double y1) +{ + return min(x0, x1) <= x && x <= max(x0, x1) && + min(y0, y1) <= y && y <= max(y0, y1); +} + +// finds the intersection between two segments (not infinite +// lines!) +// segment 1 goes from point (x0, y0) to (x1, y1). +// segment 2 goes from point (x2, y2) to (x3, y3). +// (can be modified to find the intersection between a segment +// and a line) +// handles the case when the 2 segments are: +// *parallel but don't lie on the same line (no intersection) +// *parallel and both lie on the same line (infinite +// *intersections or no intersections) +// *not parallel (one intersection or no intersections) +// returns true if the segments do intersect in any case +bool segment_segment_intersection(double x0, double y0, + double x1, double y1, + double x2, double y2, + double x3, double y3) +{ + double t0 = (y3 - y2) * (x0 - x2) - (x3 - x2) * (y0 - y2); + double t1 = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0); + double det = (y1 - y0) * (x3 - x2) - (y3 - y2) * (x1 - x0); + if (fabs(det) < EPS) + { + // parallel + if (fabs(t0) < EPS || fabs(t1) < EPS) + { + // lie on same line, but they may or may not intersect. + return (point_in_box(x0, y0, x2, y2, x3, y3) || + point_in_box(x1, y1, x2, y2, x3, y3) || + point_in_box(x2, y2, x0, y0, x1, y1) || + point_in_box(x3, y3, x0, y0, x1, y1)); + } + else + { + // just parallel, no intersection + return false; + } + } + else + { + t0 /= det; + t1 /= det; + // 0 <= t0 <= 1 iff the intersection point lies in segment 1. + // 0 <= t1 <= 1 iff the intersection point lies in segment 2. + if (0.0 <= t0 && t0 <= 1.0 && 0.0 <= t1 && t1 <= 1.0) + { + double x = x0 + t0 * (x1 - x0); + double y = y0 + t0 * (y1 - y0); + // intersection pt (x, y) + return true; + } + // intersection pts don't lie on both segments + return false; + } +} + +bool polygonsIntersect(const polygon &a, const polygon &b) +{ + int na = a.size(), nb = b.size(); + for (int i = 0; i < na; ++i) + { + if (pointInPoly(a[i], b)) + { + return true; + } + } + for (int i = 0; i < nb; ++i) + { + if (pointInPoly(b[i], a)) + { + return true; + } + } + for (int i = 0; i < na; ++i) + { + for (int j = 0; j < nb; ++j) + { + int xa1 = a[i].first, ya1 = a[i].second; + int xa2 = a[(i + 1) % na].first, ya2 = a[(i + 1) % na].second; + int xb1 = b[j].first, yb1 = b[j].second; + int xb2 = b[(j + 1) % nb].first, yb2 = b[(j + 1) % nb].second; + if (segment_segment_intersection(xa1, ya1, xa2, ya2, xb1, yb1, xb2, yb2)) + { + return true; + } + } + } + return false; +} + +void solve() +{ + int n = polygons.size(); + for (int i = 0; i < n; ++i) + { + p[i] = i; + } + for (int i = 0; i < n; ++i) + { + for (int j = i + 1; j < n; ++j) + { + if (polygonsIntersect(polygons[i], polygons[j])) + { + link(i, j); + } + } + } + set ans; + for (int i = 0; i < n; ++i) + { + ans.insert(find(i)); + } + cout << ans.size() << endl; +} + +int main() +{ + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + string s; + getline(cin, s); + polygons.clear(); + for (int i = 0; i < n; ++i) + { + polygons.push_back(polygon()); + getline(cin, s); + stringstream sin(s); + int x, y; + while (sin >> x >> y) + { + polygons.back().push_back(point(x, y)); + } + assert(polygons.back().size() >= 3); + } + assert(polygons.size() == n); + solve(); + } + return 0; +} diff --git a/11666.cpp b/11666.cpp new file mode 100644 index 0000000..1ba4107 --- /dev/null +++ b/11666.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + int L = floor(log(n) - log(2.0) + 1); + double x = 1 - exp(log(n) - L); + printf("%d %.8f\n", L, x); + } + return 0; +} diff --git a/11667.cpp b/11667.cpp new file mode 100644 index 0000000..6e29247 --- /dev/null +++ b/11667.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + long long m, minn, maxx; + int cas = 1; + long long s1, s2; + double ans; + while (~scanf("%lld%lld%lld", &m, &minn, &maxx) && (m || minn || maxx)) + { + ans = 0; + if (m <= minn) + { + printf("Case %d: %.2lf\n", cas++, ans); + continue; + } + if (m <= maxx) + { + ans += 0.1 * (maxx - m + 1) * (m * (m - minn + 1) - (minn + m) * (m - minn + 1) / 2); + ans += 0.05 * (1 + m - minn) * (m - minn) / 2 + 0.15 * (m - minn) * (m - minn + 1) * (2 * (m - minn) + 1) / 6; + } + else + { + ans += (maxx + 1) * (0.2 * m - 0.05 * maxx) * (maxx - minn + 1) - (0.2 * m + 0.1 * maxx + 0.15) * (maxx + minn) * (maxx - minn + 1) / 2 + 0.15 / 6 * (maxx * (maxx + 1) * (2 * maxx + 1) - (minn - 1) * minn * (2 * minn - 1)); + } + long long k = (maxx - minn + 1) * (maxx - minn + 2) / 2; + ans = ans / k; + printf("Case %d: %.2lf\n", cas++, ans); + } + return 0; +} diff --git a/11668.cpp b/11668.cpp new file mode 100644 index 0000000..28f378b --- /dev/null +++ b/11668.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define see(x) cout << #x << ":" << x << endl; + +const int maxn = 105; +const int mod = 1234; +int dp[maxn][maxn][maxn], cntt[maxn], c[maxn][maxn], way[maxn][5]; +int exp22[maxn], exp3[maxn], f[maxn]; +string str, tmp; +map cha; +int main() +{ + int t, i, j, p, l, m, cas = 1, n, k, cnt, ti, tj, tp; + int x, y, z; + c[0][0] = 1; + for (i = 1; i < maxn; i++) + { + c[i][0] = 1; + for (j = 1; j <= i; j++) + { + c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; + } + } + exp22[0] = exp3[0] = f[0] = 1; + for (i = 1; i < maxn; i++) + { + exp22[i] = (exp22[i - 1] * 2) % mod; + exp3[i] = (exp3[i - 1] * 3) % mod; + f[i] = (f[i - 1] * i) % mod; + } + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &k); + m = 0; + for (i = 0; i <= k; i++) + { + for (j = 0; j <= k; j++) + { + for (p = 0; p <= k; p++) + { + if ((i + j + p) == k) + { + way[m][1] = i; + way[m][2] = j; + way[m][3] = p; + m++; + } + } + } + } + cha.clear(); + cnt = 0; + memset(cntt, 0, sizeof(cntt)); + x = y = z = 0; + for (i = 0; i < n; i++) + { + cin >> str; + p = str.find('-'); + tmp = str.substr(p + 1, str.size() - p - 1); + if (!cha[tmp]) + { + cha[tmp] = ++cnt; + cntt[cnt]++; + } + else + { + cntt[cha[tmp]]++; + if (cntt[cha[tmp]] == 3) + { + z++; + } + } + y = n - z * 2 - cnt; + x = cnt - z - y; + } + memset(dp, 0, sizeof(dp)); + dp[z][y][x] = 1; + for (i = n; i >= 0; i--) + { + for (j = n; j >= 0; j--) + { + for (p = n; p >= 0; p--) + { + if (dp[i][j][p]) + { + for (l = 0; l < m; l++) + { + ti = way[l][1]; + tj = way[l][2]; + tp = way[l][3]; + if (i >= ti && j >= tj && p >= tp) + { + dp[i - ti][j + ti - tj][p + tj - tp] += dp[i][j][p] * c[i][ti] % mod * exp3[ti] % mod * c[j][tj] % mod * exp22[tj] % mod * c[p][tp] % mod * f[k] % mod; + dp[i - ti][j + ti - tj][p + tj - tp] %= mod; + } + } + } + } + } + } + printf("Case %d: %d\n", cas++, dp[0][0][0]); + } + return 0; +} diff --git a/11669.cpp b/11669.cpp new file mode 100644 index 0000000..746964c --- /dev/null +++ b/11669.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +int lev, all[1000005], top = 0, cnt = 1; +bool is[1000005]; +vector v; + +void generate(); +void getall(); +void dfs(long long now, int which, int level); +void output(int n); + +int main() +{ + generate(); + getall(); + int T, t, A, B, K, i, j, pos; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + scanf(" %d %d %d", &A, &B, &K); + if (K > (B - A) / 2) + { + for (i = top - 1, K = B - A - K + 2; K; i--) + if (all[i] >= A && all[i] <= B) + { + K--; + } + i++; + } + else + { + for (i = 0; K; i++) + if (all[i] >= A && all[i] <= B) + { + K--; + } + i--; + } + /*for(i=0;i=A && all[i]<=B) + K--;*/ + //printf("%d %d\n",all[i-1]); + output(all[i]); + } + return 0; +} +void generate() +{ + v.clear(); + int i, j; + for (i = 2; i < 1000005; i++) + if (!is[i]) + { + for (j = i * 2; j < 1000005; j += i) + { + is[j] = true; + } + v.push_back(i); + } +} +void getall() +{ + for (lev = 1; lev < 20; lev++) + { + dfs(1, 0, 0); + } +} +void dfs(long long now, int which, int level) +{ + if (level == lev) + { + if (now <= 1000000) + { + all[top++] = now; + } + return; + } + while (which < v.size()) + { + if (now * v[which] > 1000000) + { + return; + } + dfs(now * v[which], which, level + 1); + which++; + } +} +void output(int n) +{ + printf("Case %d:", cnt++); + int i = 0; + while (n != 1) + { + while (n % v[i] == 0) + { + printf(" %d", v[i]); + n /= v[i]; + if (n == 1) + { + break; + } + } + if (n == 1) + { + break; + } + i++; + } + printf("\n"); +} diff --git a/11670.cpp b/11670.cpp new file mode 100644 index 0000000..3a1f72b --- /dev/null +++ b/11670.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +double dis[10000]; +int almostSame(double a, double b) +{ + return a - 1E-9 <= b && b <= a + 1E-9; +} +int main() +{ + int kase = 0; + while (true) + { + kase++; + int n; + double base, power; + scanf("%d", &n); + if (!n) + { + break; + } + for (int q = 0; q < n; ++q) + { + scanf("%lf", &dis[q]); + } + scanf("%lf", &base); + sort(dis, dis + n); + for (int q = n - 1; q >= 0; q--) + { + dis[q] -= dis[0]; + } + power = base; + double time = 0; + for (int q = 1; q < n; ++q) + if (dis[q] < power || almostSame(dis[q], power)) + { + power = dis[q] + base; + } + else if (dis[q] - 2 * time < power || almostSame(dis[q] - 2 * time, power)) + { + power += base; + } + else + { + double delta = dis[q] - power; + time = delta / 2; + power += base; + } + printf("Case %d: %.3lf\n", kase, time); + } + return 0; +} diff --git a/11671.cpp b/11671.cpp new file mode 100644 index 0000000..fde0659 --- /dev/null +++ b/11671.cpp @@ -0,0 +1,269 @@ +#include + +using namespace std; + +#define DEBUG(x) + +int N; +char Table[105][105]; + +int getVal(char x) +{ + if (x == '-') + { + return -1; + } + if (x == '0') + { + return 0; + } + if (x == '+') + { + return 1; + } + return 0; +} +void swapRow(int a, int b) +{ + for (int q = 0; q < N; q++) + { + swap(Table[a][q], Table[b][q]); + } +} +void swapCol(int a, int b) +{ + for (int q = 0; q < N; q++) + { + swap(Table[q][a], Table[q][b]); + } +} +int compRow(int a, int b) +{ + int ret = 0; + for (int q = 0; q < N; q++) + { + if (Table[a][q] == Table[b][q]) + { + continue; + } + int t = getVal(Table[a][q]) < getVal(Table[b][q]) ? -1 : 1; + if (ret != 0 && ret != t) + { + return -5; + } + ret = t; + } + return ret; +} +int compCol(int a, int b) +{ + int ret = 0; + for (int q = 0; q < N; q++) + { + if (Table[q][a] == Table[q][b]) + { + continue; + } + int t = getVal(Table[q][a]) < getVal(Table[q][b]) ? -1 : 1; + if (ret != 0 && ret != t) + { + return -5; + } + ret = t; + } + return ret; +} +int main() +{ + for (int kase = 1;; ++kase) + { + scanf("%d", &N); + if (N < 0) + { + break; + } + printf("Case %d: ", kase); + for (int q = 0; q < N; ++q) + { + scanf("%s", Table[q]); + } + int isBad = 0; + for (int q = 0; q < N; ++q) + for (int w = q + 1; w < N; ++w) + if (compRow(q, w) < -1 || compCol(q, w) < -1) + { + isBad = 1; + } + if (isBad) + { + printf("-1\n"); + continue; + } + for (int q = 0; q < N; q++) + for (int w = q + 1; w < N; ++w) + if (compRow(q, w) < 0) + { + swapRow(q, w); + } + for (int q = 0; q < N; q++) + for (int w = q + 1; w < N; ++w) + if (compCol(q, w) < 0) + { + swapCol(q, w); + } + DEBUG(printf("Result:\n")); + DEBUG(for (int q = 0; q < N; q++) printf("%s\n", Table[q])); + vector C_p, R_n; + for (int q = 0; q < N; q++) + if (Table[0][q] == '+') + if (C_p.empty() || compCol(C_p.back(), q) != 0) + { + C_p.push_back(q); + } + for (int q = N - 1; q > 0; q--) + if (compRow(q, 0) != 0) + if (R_n.empty() || compRow(R_n.back(), q) != 0) + { + R_n.push_back(q); + } + DEBUG(printf("C+:"); for (int q = 0; q < C_p.size(); q++) printf("%d ", C_p[q]); printf("\n")); + DEBUG(printf("R-:"); for (int q = 0; q < R_n.size(); q++) printf("%d ", R_n[q]); printf("\n")); + vector> buff; + int i = 0, j = 0; + while (i < C_p.size() || j < R_n.size()) + { + buff.push_back(vector()); + if (i >= C_p.size()) + { + buff.back().push_back(R_n[j] * 2); + j++; + continue; + } + if (j >= R_n.size()) + { + buff.back().push_back(C_p[i] * 2 + 1); + i++; + continue; + } + int c = C_p[i]; + int r = R_n[j]; + if (Table[r][c] == '+') + { + buff.back().push_back(c * 2 + 1); + i++; + } + if (Table[r][c] == '-') + { + buff.back().push_back(r * 2); + j++; + } + if (Table[r][c] == '0') + { + buff.back().push_back(c * 2 + 1); + i++; + buff.back().push_back(r * 2); + j++; + } + } + vector rows, cols; + for (int q = 0; q < buff.size(); ++q) + for (int w = 0; w < buff[q].size(); ++w) + if (buff[q][w] & 1)//col,+ + { + int col = buff[q][w] / 2; + int val = (buff.size() - q); + for (int e = 0; e < N; ++e) + if (compCol(col, e) == 0) + { + cols.push_back(val); + } + } + else//row,- + { + int row = buff[q][w] / 2; + int val = -(buff.size() - q); + for (int e = 0; e < N; ++e) + if (compRow(row, e) == 0) + { + rows.push_back(val); + } + } + //row,+ -> null + //row,0 + for (int q = 0; q < N; ++q) + if (compRow(0, q) == 0) + { + rows.push_back(0); + } + //col,0 + for (int q = 0; q < N; ++q) + if (Table[0][q] == '0') + { + cols.push_back(0); + } + //col,- + int T = 0; + for (int q = 0; q < N; ++q) + if (Table[0][q] == '-') + { + if (q == 0 || compCol(q - 1, q) != 0) + { + T--; + } + cols.push_back(T); + } + sort(rows.rbegin(), rows.rend()); + sort(cols.rbegin(), cols.rend()); + DEBUG(printf("Debug:\n")); + for (int q = 0; q < rows.size(); q++) + { + for (int w = 0; w < cols.size(); ++w) + { + int val = rows[q] + cols[w]; + char ret = '0'; + if (val < 0) + { + ret = '-'; + } + if (val > 0) + { + ret = '+'; + } + if (ret != Table[q][w]) + { + isBad = 1; + } + DEBUG(printf("%c", ret)); + } + DEBUG(printf("\n")); + } + if (isBad) + { + printf("-1\n"); + continue; + } + vector mix; + for (int q = 0; q < rows.size(); ++q) + { + mix.push_back(-rows[q]); + } + for (int q = 0; q < cols.size(); ++q) + { + mix.push_back(cols[q]); + } + sort(mix.begin(), mix.end()); + if (mix.size() == 0) + { + printf("0\n"); + continue; + } + int k = mix[mix.size() / 2]; + int ret = 0; + for (int q = 0; q < mix.size(); ++q) + { + ret += abs(mix[q] - k); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/11672.cpp b/11672.cpp new file mode 100644 index 0000000..ecff099 --- /dev/null +++ b/11672.cpp @@ -0,0 +1,198 @@ +#include + +using namespace std; + +typedef long long LL; +#define keyTree (ch[ch[root][1]][0]) +const int maxn = 2100005; +struct splaytree +{ + int size[maxn]; + int ch[maxn][2]; + int pre[maxn]; + int root, top1; + + void Rotate(int x, int f) + { + int y = pre[x]; + pushdown(y); + pushdown(x); + ch[y][!f] = ch[x][f]; + pre[ch[x][f]] = y; + pre[x] = pre[y]; + if (pre[x]) + { + ch[pre[y]][ch[pre[y]][1] == y] = x; + } + ch[x][f] = y; + pre[y] = x; + pushup(y); + } + void splay(int x, int goal) + { + pushdown(x); + while (pre[x] != goal) + { + if (pre[pre[x]] == goal) + { + Rotate(x, ch[pre[x]][0] == x); + } + else + { + int y = pre[x], z = pre[y]; + int f = (ch[z][0] == y); + if (ch[y][f] == x) + { + Rotate(x, !f), Rotate(x, f); + } + else + { + Rotate(y, f), Rotate(x, f); + } + } + } + pushup(x); + if (goal == 0) + { + root = x; + } + } + void RotateTo(int k, int goal) + { + int x = root; + pushdown(x); + while (size[ch[x][0]] != k) + { + if (k < size[ch[x][0]]) + { + x = ch[x][0]; + } + else + { + k -= (size[ch[x][0]] + 1); + x = ch[x][1]; + } + pushdown(x); + } + splay(x, goal); + } + void maketree(int &x, int l, int r, int f) + { + if (l > r) + { + return; + } + int m = (l + r) / 2; + NewNode(x, num[m]); + maketree(ch[x][0], l, m - 1, x); + maketree(ch[x][1], m + 1, r, x); + pushup(x); + pre[x] = f; + } + + void NewNode(int &x, int c) + { + x = top1++; + ch[x][0] = ch[x][1] = pre[x] = 0; + size[x] = 1; + val[x] = c; + sum[x] = c; + } + + void pushdown(int x) + { + } + void pushup(int x) + { + size[x] = 1 + size[ch[x][0]] + size[ch[x][1]]; + sum[x] = val[x] + sum[ch[x][0]] + sum[ch[x][1]]; + } + + void init() + { + ch[0][0] = ch[0][1] = pre[0] = size[0] = 0; + sum[0] = 0; + top1 = 1; + NewNode(root, 0); + NewNode(ch[root][1], 0); + pre[ch[root][1]] = root; + size[root] = 2; + } + + void insert() + { + int pos; + scanf("%d", &pos); + LL n, r, m, a, c; + scanf("%lld%lld%lld%lld%lld", &n, &r, &m, &a, &c); + for (int i = 0; i < r; i++) + { + scanf("%lld", &num[i]); + } + for (int i = r; i < n; i++) + { + num[i] = (num[i - r] * a + c) % m; + c = ((num[i - r] * a + c) / m) % m; + } + RotateTo(pos, 0); + RotateTo(pos + 1, root); + maketree(keyTree, 0, n - 1, ch[root][1]); + pushup(ch[root][1]); + pushup(root); + } + + void query() + { + int l, r; + scanf("%d%d", &l, &r); + RotateTo(l, 0); + RotateTo(r + 2, root); + printf("%lld\n", sum[keyTree]); + } + + void del() + { + int l, r; + scanf("%d%d", &l, &r); + l++; + r++; + RotateTo(l - 1, 0); + RotateTo(r + 1, root); + keyTree = 0; + pushup(ch[root][1]); + pushup(root); + } + LL num[maxn]; + LL sum[maxn]; + LL val[maxn]; +} spt; + +int main() +{ + int C; + int cas = 1; + while (scanf("%d", &C) != EOF && C) + { + printf("Scenario %d:\n", cas++); + spt.init(); + for (int i = 1; i <= C; i++) + { + char str[25]; + scanf("%s", str); + if (str[0] == 'i') + { + spt.insert(); + } + else if (str[0] == 'd') + { + spt.del(); + } + else if (str[0] == 's') + { + spt.query(); + } + } + puts(""); + } + return 0; +} diff --git a/11673.cpp b/11673.cpp new file mode 100644 index 0000000..bf2f5da --- /dev/null +++ b/11673.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +typedef long long ll; +const int N = 1000001; + +int table[61]; +void init_P(int n, ll *in) +{ + int i, j; + table[0] = -1; + for (i = 0, j = -1; i < n; i++, j++, table[i] = j) + { + while (j >= 0 && in[i] != in[j]) + { + j = table[j]; + } + } + //table[i]=j; +} + +int solve(ll *in, int pat, int n, ll *p, ll *q) +{ + int ans = 0; + int i = 0, j = 0; + while (i < n && j < pat) + { + while (j >= 0 && !(p[i] <= in[j] && in[j] <= q[i])) + { + j = table[j]; + } + i++; + j++; + if (j == pat) + { + ans++; + j = table[j]; + j = pat - 1; + } + } + return ans; +} + +void generate(ll p0, ll q0, ll A, ll B, ll C, ll D, ll E, ll F, ll M, + ll *p, ll *q, int n) +{ + ll pprev = p0, qprev = q0; + rep(i, n) + { + p[i] = (A * pprev + B * qprev + C) % M; + q[i] = (D * pprev + E * qprev + F) % M; + if (q[i] < p[i]) + { + swap(p[i], q[i]); + } + pprev = p[i]; + qprev = q[i]; + } +} + +ll p[N], q[N]; +main() +{ + int te, tc = 1; + cin >> te; + while (te--) + { + int pnum; + cin >> pnum; + ll pat[pnum]; + rep(i, pnum) cin >> pat[i]; + int n; + ll p0, q0, a, b, c, d, e, f, m; + cin >> n >> p0 >> q0 >> a >> b >> c >> d >> e >> f >> m; + generate(p0, q0, a, b, c, d, e, f, m, p, q, n); + init_P(pnum, pat); + printf("Case %d: %d\n", tc++, solve(pat, pnum, n, p, q)); + } +} diff --git a/11675.cpp b/11675.cpp new file mode 100644 index 0000000..80224c1 --- /dev/null +++ b/11675.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +const int MAXN = 35; +const int MOD = 1000000007; + +typedef vector> matrix; + +int day[MAXN]; +vector g[MAXN]; + +matrix mul(const matrix &A, const matrix &B) +{ + int p = A.size(); + int q = A[0].size(); + int r = B[0].size(); + matrix C(p, vector(r, 0)); + for (int i = 0; i < p; ++i) + { + for (int j = 0; j < r; ++j) + { + C[i][j] = 0; + for (int k = 0; k < q; ++k) + { + C[i][j] += (A[i][k] * B[k][j]) % MOD; + C[i][j] %= MOD; + } + } + } + return C; +} + +matrix exp(const matrix &A, int e) +{ + int n = A.size(); + if (e == 0) + { + matrix C(n, vector(n, 0)); + for (int i = 0; i < n; ++i) + { + C[i][i] = 1; + } + return C; + } + if (e == 1) + { + return A; + } + matrix B = exp(A, e / 2); + B = mul(B, B); + if (e % 2 == 1) + { + B = mul(B, A); + } + return B; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + int n, m, k, d; + if (scanf("%d%d%d%d", &n, &m, &k, &d) != 4) + { + break; + } + for (int i = 0; i < n; ++i) + { + g[i].clear(); + g[i].push_back(i); + day[i] = -1; + } + while (m--) + { + int u, v; + scanf("%d%d", &u, &v); + g[u].push_back(v); + g[v].push_back(u); + } + queue q; + day[k] = 0; + q.push(k); + while (q.size()) + { + int u = q.front(); + q.pop(); + FOREACH(g[u], edge) + { + int v = *edge; + if (day[v] == -1) + { + day[v] = !day[u]; + q.push(v); + } + } + } + matrix odd(n, vector(n)); + matrix even(n, vector(n)); + // find odd/even matrices + for (int j = 0; j < n; ++j) + { + for (int i = 0; i < n; ++i) + { + odd[i][j] = even[i][j] = 0; + } + FOREACH(g[j], edge) + { + int i = *edge; + if (day[j] == 0) + { + even[i][j] = 1; + } + else + { + odd[i][j] = 1; + } + } + even[j][j] = odd[j][j] = 1; + } + matrix C = mul(odd, even); + C = exp(C, d / 2); + if (d % 2 == 1) + { + C = mul(C, odd); + } + matrix ans(1, vector(n)); + for (int j = 0; j < n; ++j) + { + ans[0][j] = 0; + } + ans[0][k] = 1; + C = mul(ans, C); + printf("Case %d:", t); + for (int j = 0; j < n; ++j) + { + printf(" %d", (int)C[0][j]); + } + printf("\n"); + } + return 0; +} diff --git a/11676.cpp b/11676.cpp new file mode 100644 index 0000000..51df137 --- /dev/null +++ b/11676.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) + +const double PI = acos(-1); +const double EPS = 1e-12; + +struct PT +{ + double x, y, r; + PT() {} + PT(double x, double y, double r = 0) : x(x), y(y), r(r) {} + PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } + PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } + PT operator*(double c) const { return PT(x * c, y * c); } + PT operator/(double c) const { return PT(x / c, y / c); } +} Center, Cannon; + +double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; } +double dist2(PT p, PT q) { return dot(p - q, p - q); } +double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; } +PT RotateCCW(PT p, double t) { return PT(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t)); } +double norm(PT p) { return sqrt(dot(p, p)); } +double ang(PT p, PT q) { return acos(dot(p, q) / (norm(p) * norm(q))); } +//Centro a, pontos da borda b e c, raio da circunferencia r +double areaCircleSeg(PT c, PT p, PT q, double r) { return fabs(r * r * (ang(p - c, q - c) - sin(ang(p - c, q - c))) / 2.0); } + +vector CLIntersect(PT a, PT b, PT c) +{ + PT _a = a, _b = b; + double r = c.r; + vector ret; + b = b - a; + a = a - c; + double A = dot(b, b); + double B = dot(a, b); + double C = dot(a, a) - r * r; + double D = B * B - A * C; + if (D < -EPS) + return ret; + ret.push_back(c + a + b * (-B + sqrt(D + EPS)) / A); + if (D > EPS) + ret.push_back(c + a + b * (-B - sqrt(D)) / A); + //printf("> %lf %lf, %lf %lf, %lf %lf\n", _a.x, _a.y, _b.x, _b.y, ret[0].x, ret[0].y); + if (dot(_b - _a, ret[0] - _a) < 0) + ret.clear(); + return ret; +} + +double X, Y, R, A, T; +PT p1, p2; +vector v1, v2; + +int main() +{ + PT eixoX = PT(1.0, 0.0); + while (scanf("%lf%lf%lf", &X, &Y, &R) == 3 && R > 0) + { + Center = PT(X, Y, R); + scanf("%lf%lf%lf%lf", &X, &Y, &A, &T); + A *= PI / 180.0; + T *= PI / 180.0; + Cannon = PT(X, Y); + T /= 2.0; + p1 = RotateCCW(eixoX, A - T) + Cannon; + p2 = RotateCCW(eixoX, A + T) + Cannon; + //printf("%lf %lf, %lf %lf\n", p1.x, p1.y, p2.x, p2.y); + if (cross(Center - Cannon, p1 - Cannon) * cross(Center - Cannon, p2 - Cannon) > 0) + { + if (cross(Center - Cannon, p1 - Cannon) < 0) + { + v1 = CLIntersect(Cannon, p1, Center); + v2 = CLIntersect(Cannon, p2, Center); + } + else + { + v1 = CLIntersect(Cannon, p2, Center); + v2 = CLIntersect(Cannon, p1, Center); + } + //printf("> %d %d\n", v1.size(), v2.size()); + if (v2.size() < 2) + printf("0.0\n"); + else + { + if (v1.size() < 2) + printf("%.1lf\n", areaCircleSeg(Center, v2[0], v2[1], R)); + else + printf("%.1lf\n", areaCircleSeg(Center, v2[0], v2[1], R) - areaCircleSeg(Center, v1[0], v1[1], R)); + } + } + else + { + v1 = CLIntersect(Cannon, p1, Center); + v2 = CLIntersect(Cannon, p2, Center); + //printf("> %d %d %lf\n", v1.size(), v2.size(), PI*R*R); + if (v1.size() < 2 && v2.size() < 2) + { + if (dot(RotateCCW(eixoX, A), Center - Cannon) < 0) + printf("0.0\n"); + else + printf("%.1lf\n", PI * R * R); + } + else if (v1.size() < 2) + { + if (dot(RotateCCW(eixoX, A), Center - Cannon) < 0) + printf("%.1lf\n", areaCircleSeg(Center, v2[0], v2[1], R)); + else + printf("%.1lf\n", PI * R * R - areaCircleSeg(Center, v2[0], v2[1], R)); + } + else if (v2.size() < 2) + { + if (dot(RotateCCW(eixoX, A), Center - Cannon) < 0) + printf("%.1lf\n", areaCircleSeg(Center, v1[0], v1[1], R)); + else + printf("%.1lf\n", PI * R * R - areaCircleSeg(Center, v1[0], v1[1], R)); + } + else + printf("%.1lf\n", PI * R * R - areaCircleSeg(Center, v1[0], v1[1], R) - areaCircleSeg(Center, v2[0], v2[1], R)); + } + } + return 0; +} diff --git a/11677.cpp b/11677.cpp index 50a1a9a..45f4113 100644 --- a/11677.cpp +++ b/11677.cpp @@ -1,21 +1,26 @@ -#include -using namespace std; - -int main(){ - int uni=24*60; - for(;;){ - int H1,M1,H2,M2; - cin>>H1>>M1>>H2>>M2; - if(H1==0 && M1==0 && H2==0 && M2==0) - break; - int t1=H1*60+M1; - int t2=H2*60+M2; - int sleep=0; - if(t2>=t1) - sleep=t2-t1; - else - sleep=uni-t1+t2; - cout< + +using namespace std; + +int main() +{ + int h1, m1, h2, m2; + while (cin >> h1 >> m1 >> h2 >> m2) + { + if (h1 == 0 && h2 == 0 && m1 == 0 && m2 == 0) + { + break; + } + int t1 = h1 * 60 + m1; + int t2 = h2 * 60 + m2; + if (t2 >= t1) + { + cout << t2 - t1 << endl; + } + else + { + cout << 24 * 60 - (t1 - t2) << endl; + } + } + return 0; +} diff --git a/11678.cpp b/11678.cpp index 477561e..d4b6620 100644 --- a/11678.cpp +++ b/11678.cpp @@ -1,43 +1,49 @@ -#include -using namespace std; - -int main(){ - for(;;){ - int A,B; - int Alice[10000]={0},Betty[10000]={0},a=0,b=0; - cin>>A>>B; - if(A==0 && B==0) - break; - for(int i=0;i>Alice[i]; - for(int i=0;i>Betty[i]; - for(int i=0;i + +using namespace std; + +int main() +{ + int a, b; + int alice_cards[10001], betty_cards[10001]; + bool alice[100001], betty[100001]; + while (cin >> a >> b) + { + if (a == 0 && b == 0) + { + break; + } + fill(alice, alice + 100001, false); + fill(betty, betty + 100001, false); + for (int i = 0; i < a; i++) + { + cin >> alice_cards[i]; + alice[alice_cards[i]] = true; + } + for (int i = 0; i < b; i++) + { + cin >> betty_cards[i]; + betty[betty_cards[i]] = true; + } + int alice_count = 0; + int betty_count = 0; + for (int i = 0; i < a; i++) + { + if (!betty[alice_cards[i]]) + { + alice_count++; + } + betty[alice_cards[i]] = true; + } + for (int i = 0; i < b; i++) + { + if (!alice[betty_cards[i]]) + { + betty_count++; + } + alice[betty_cards[i]] = true; + } + cout << min(alice_count, betty_count) << endl; + } + return 0; +} diff --git a/11679.cpp b/11679.cpp new file mode 100644 index 0000000..668b1ab --- /dev/null +++ b/11679.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int main() +{ + int b, n, debtor, creditor, value; + while (cin >> b >> n) + { + if (b == 0 && n == 0) + { + break; + } + int banks[b]; + for (int i = 0; i < b; i++) + { + cin >> banks[i]; + } + while (n--) + { + cin >> debtor >> creditor >> value; + banks[debtor - 1] -= value; + banks[creditor - 1] += value; + } + bool ok = true; + for (int i = 0; i < b; i++) + { + if (banks[i] < 0) + { + ok = false; + break; + } + } + if (ok) + { + cout << "S" << endl; + } + else + { + cout << "N" << endl; + } + } + return 0; +} diff --git a/11680.cpp b/11680.cpp new file mode 100644 index 0000000..5c951e4 --- /dev/null +++ b/11680.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +#define NN 300 +#define EPS 1E-6 +int h, t, N; +int deg[NN], dep[NN], points[NN][NN], q[NN], v[NN]; +double cp[NN][NN], m1[NN], m2[NN], p[NN][NN]; + +struct corrida +{ + int a, b, ind; + bool operator<(const corrida x) const + { + return a < x.a; + } +} c[NN]; + +void DOIT(double A[], double B[], int k) +{ + double r; + int i, j; + for (i = 0; i < N; i++) + { + r = 0.; + if (A[i] > EPS) + { + for (j = 0; j < N; j++) + if (B[j] > EPS) + { + r += B[j] * p[i][j]; + } + cp[k][i] = r * A[i]; + } + else if (B[i] > EPS) + { + for (j = 0; j < N; j++) + if (A[j] > EPS) + { + r += A[j] * p[i][j]; + } + cp[k][i] = r * B[i]; + } + else + { + cp[k][i] = 0.; + } + } +} + +void doRace(int x) +{ + if (c[x].a < N) + { + m1[c[x].a] = 1.; + if (c[x].b < N) + { + m2[c[x].b] = 1.; + DOIT(m1, m2, c[x].ind); + m2[c[x].b] = 0.; + } + m1[c[x].a] = 0.; + } + else if (c[x].b < N) + { + m2[c[x].b] = 1.; + DOIT(cp[c[x].a - N], m2, c[x].ind); + m2[c[x].b] = 0.; + } + else + { + DOIT(cp[c[x].a - N], cp[c[x].b - N], c[x].ind); + } +} + +void TOPOSOLVE() +{ + int k; + while (h != t) + { + k = q[h++]; + doRace(k); + for (int i = 0; i < deg[k]; i++) + if (--dep[points[k][i]] == 0) + { + q[t++] = points[k][i]; + } + } + printf("%.6lf\n", cp[c[k].ind][0]); +} +int main() +{ + int a, b, i, j; + for (i = 0; i < NN; i++) + { + m1[i] = m2[i] = 0.; + } + while (scanf("%d", &N) && N) + { + for (h = i = t = 0; i < N; i++) + { + deg[i] = dep[i] = 0; + for (j = 0; j < N; j++) + { + scanf("%lf", &p[i][j]); + } + } + j = N - 1; + for (i = 0; i < j; i++) + { + scanf("%d%d", &a, &b); + if (b > a) + { + swap(a, b); + } + c[i].a = --a; + c[i].b = --b; + c[i].ind = i; + if (a >= N) + { + int k = a - N; + points[k][deg[k]++] = i; + dep[i]++; + } + if (b >= N) + { + int k = b - N; + points[k][deg[k]++] = i; + dep[i]++; + } + if (dep[i] == 0) + { + q[t++] = i; + } + } + TOPOSOLVE(); + } + return 0; +} diff --git a/11681.cpp b/11681.cpp new file mode 100644 index 0000000..4113106 --- /dev/null +++ b/11681.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) + +typedef long long ll; + +const double eps = 1e-8; +const double pi = acos(-1.0); + +int cmp(double a, double b = 0) +{ + if (fabs(a - b) < eps) + return 0; + else if (a < b) + return -1; + return 1; +} + +struct PT +{ + double x, y; + + PT() {} + PT(double x, double y) : x(x), y(y) {} + + PT operator+(const PT &q) const { return PT(x + q.x, y + q.y); } + PT operator-(const PT &q) const { return PT(x - q.x, y - q.y); } + PT operator*(double t) const { return PT(x * t, y * t); } + PT operator/(double t) const { return PT(x / t, y / t); } + PT operator-() const { return PT(-x, -y); } + PT operator[](double t) const { return PT(x * cos(t) - y * sin(t), x * sin(t) + y * cos(t)); } + double operator*(const PT &q) const { return x * q.x + y * q.y; } + double operator%(const PT &q) const { return x * q.y - y * q.x; } + double operator!() const { return sqrt(x * x + y * y); } + double operator^(const PT &q) const { return atan2(*this % q, *this * q); } + double operator>(const PT &q) const { return ((*this * q) / (q * q)); } +} p[100]; + +bool segSegIntersect(PT a, PT b, PT c, PT d, PT &p) +{ + if (cmp((d - c) % (b - a)) == 0) + return 0; + p = c + (d - c) * (((b - a) % (c - a)) / ((d - c) % (b - a))); + return 1; +} + +PT circumcenter(PT a, PT b, PT c) +{ + PT a1 = (a + b) / 2.0; + PT b1 = a1 + ((b - a)[pi / 2.0]); + PT a2 = (b + c) / 2.0; + PT b2 = a2 + ((c - b)[pi / 2.0]); + PT center; + segSegIntersect(a1, b1, a2, b2, center); + return center; +} + +int n; +PT c; +ll tstMask[70000], mask; +int sz; +double tstRay[70000], r; +double ans; + +bool in_circle(PT a, PT c, double r) +{ + return cmp(!(a - c), r) <= 0; +} + +void teste() +{ + PT centro; + double raio = 0.0; + rp(_, sz) + { + mask = tstMask[_]; + centro = PT(0.0, 0.0); + raio = 0.0; + fr(i, 0, n) if (((mask >> i) & 1) && !in_circle(p[i], centro, raio)) + { + centro = p[i]; + fr(j, 0, i) if (((mask >> j) & 1) && !in_circle(p[j], centro, raio)) + { + centro = (p[i] + p[j]) / 2.0; + raio = (!(p[i] - p[j])) / 2.0; + fr(k, 0, j) if (((mask >> k) & 1) && !in_circle(p[k], centro, raio)) + { + centro = circumcenter(p[i], p[j], p[k]); + raio = !(p[k] - centro); + } + } + } + ans = min(ans, max(tstRay[_], raio)); + } +} + +int main() +{ + while (scanf("%d", &n) == 1 && n > 0) + { + rp(i, n) scanf("%lf%lf", &p[i].x, &p[i].y); + sz = 0; + ans = 1e100; + + fr(i, 0, n) fr(j, i + 1, n) + { + c = (p[i] + p[j]) / 2.0; + r = (!(p[i] - p[j])) / 2.0; + mask = 0; + fr(l, 0, n) if (!in_circle(p[l], c, r)) mask |= (1LL << l); + if (__builtin_popcount(mask) <= 1) + ans = min(ans, r); + else + tstMask[sz] = mask, tstRay[sz++] = r; + } + + fr(i, 0, n) fr(j, i + 1, n) fr(k, j + 1, n) + { + if (cmp((p[j] - p[i]) % (p[k] - p[i])) == 0) + continue; + c = circumcenter(p[i], p[j], p[k]); + r = !(p[i] - c); + mask = 0; + fr(l, 0, n) if (!in_circle(p[l], c, r)) mask |= (1LL << l); + if (__builtin_popcount(mask) <= 1) + ans = min(ans, r); + else + tstMask[sz] = mask, tstRay[sz++] = r; + } + teste(); + printf("%.2lf\n", ans); + } + return 0; +} diff --git a/11683.cpp b/11683.cpp new file mode 100644 index 0000000..f7282ef --- /dev/null +++ b/11683.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define NN 10032 + +int v[NN]; +int main() +{ + int menor, H, L; + int s, i; + while (scanf("%d%d", &H, &L) && (H || L)) + { + s = 0; + for (i = 0; i < L; i++) + { + scanf("%d", &v[i]); + if (i && v[i] == v[i - 1]) + { + i--, L--; + } + } + v[L] = H; + menor = v[0]; + for (i = 1; i < L; i++) + if (v[i] > v[i - 1] && v[i] > v[i + 1]) + { + s += v[i] - menor; + menor = H; + } + else if (v[i] < menor) + { + menor = v[i]; + } + printf("%d\n", s + H - menor); + } + return 0; +} diff --git a/11684.cpp b/11684.cpp new file mode 100644 index 0000000..e5fb679 --- /dev/null +++ b/11684.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define FR(i, a, b) for (int i = (a); i < (b); ++i) +#define FOR(i, n) FR(i, 0, n) +#define CLR(x, a) memset(x, a, sizeof(x)) +#define setmax(a, b) a = max(a, b) +#define PB push_back +#define BEND(v) (v).begin(), (v).end() +#define MP make_pair +#define A first +#define B second +#define FORALL(i, v) for (typeof((v).end()) i = (v).begin(); i != (v).end(); ++i) +#define X real() +#define Y imag() +typedef long long ll; +typedef complex cpx; + +int n, m; +int as[1000], bs[1000], cs[1000], xs[1000], ys[1000], zs[1000]; +bool known[1000]; +cpx pts[1000]; +void put(int i, int x, int y) +{ + assert(!known[i]); + known[i] = 1; + pts[i] = cpx(x, y); +} +void doit() +{ + scanf("%d%d", &n, &m); + if (n == 0) + { + exit(0); + } + FOR(i, n) + { + scanf("%d%d%d%d%d%d", &as[i], &bs[i], &cs[i], &xs[i], &ys[i], &zs[i]); + --as[i]; + --bs[i]; + --cs[i]; + } + CLR(known, 0); + put(0, 0, 0); + put(1, 0, 1); + put(2, 1, 0); + while (1) + { + bool done = 1; + FOR(i, n) + { + int nk = known[as[i]] + known[bs[i]] + known[cs[i]]; + if (nk == 2) + { + done = 0; + while (known[bs[i]]) + { + swap(as[i], cs[i]); + swap(as[i], bs[i]); + swap(xs[i], zs[i]); + swap(xs[i], ys[i]); + } + int a = as[i], b = bs[i], c = cs[i]; + cpx p = pts[a], q = pts[c]; + for (ll dx = 0; dx * dx <= xs[i]; ++dx) + { + ll dy = ll(sqrt(xs[i] - dx * dx) + 0.5); + if (dx * dx + dy * dy != xs[i]) + { + continue; + } + FOR(zz, 2) + { + dx *= -1; + FOR(zzz, 2) + { + dy *= -1; + cpx r = p + cpx(dx, dy); + assert(xs[i] == norm(p - r)); + if (ys[i] == norm(q - r) && (conj(q - r) * (r - p)).Y < 0) + { + put(b, r.X, r.Y); + } + } + } + } + assert(known[b]); + } + } + if (done) + { + break; + } + } + FOR(i, m) + { + printf("%lld %lld\n", pts[i].X, pts[i].Y); + } +} + +const int MULTICASE = 1; +int main() +{ + do + { + doit(); + } while (MULTICASE); + return 0; +} diff --git a/11685.cpp b/11685.cpp new file mode 100644 index 0000000..d21539a --- /dev/null +++ b/11685.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +char rot[3] = {-1, 0, 1}; +char rotn[4] = {-2, -1, 0, 1}; +int dx[4] = {-1, 0, 1, 0}; +int dy[4] = {0, 1, 0, -1}; +char conns[8], map_[128]; +int ex, ey, sx, sy, R, C; + +struct STATE +{ + char x, y, r; + int s; + STATE(char _x = 0, char _y = 0, char _r = 0, int _s = 0) : x(_x), y(_y), r(_r), s(_s) {} +}; + +deque q; +bool visited[20][20][4][1600]; +int steps[20][20][4][1600]; +bool connected[20][20][4]; + +void QUEUEIT(char x, char y, char r, int spare, int step) +{ + if (visited[x][y][r][spare]) + { + return; + } + q.push_back(STATE(x, y, r, spare)); + visited[x][y][r][spare] = 1; + steps[x][y][r][spare] = step; +} +int BFS() +{ + memset(visited, 0, sizeof(visited)); + q.clear(); + QUEUEIT(sx, sy, 0, 0, 0); + while (!q.empty()) + { + STATE stt = q.front(); + q.pop_front(); + char c_x = stt.x, c_y = stt.y, c_r = stt.r; + int c_s = stt.s, c_steps = steps[c_x][c_y][c_r][c_s]; + if (c_x == ex && c_y == ey) + { + return c_steps; + } + for (char i = 0; i < 3; ++i) + { + char new_rot = (c_r + rot[i] + 4) % 4; + int spare_r = c_s + !(rot[i] & 1); + QUEUEIT(c_x, c_y, new_rot, spare_r, c_steps + 1); + for (char dir = 0; dir < 4; ++dir) + { + if (!connected[c_x][c_y][(dir + new_rot) % 4]) + { + continue; + } + char nx = c_x + dx[dir], ny = c_y + dy[dir]; + if (nx < 0 || ny < 0 || nx >= R || ny >= C) + { + continue; + } + for (char j = 0; j < 4; ++j) + { + if (abs(rotn[j]) > spare_r) + { + continue; + } + char new_rot2 = (rotn[j] + 4) % 4; + if (!connected[nx][ny][(dir + new_rot2 + 2) % 4]) + { + continue; + } + int spare_r2 = spare_r - abs(rotn[j]); + QUEUEIT(nx, ny, new_rot2, spare_r2, c_steps + 1); + } + } + } + } +} + +int main() +{ + map_['N'] = 0; + map_['E'] = 1; + map_['S'] = 2; + map_['W'] = 3; + while (scanf("%d%d", &R, &C) && R) + { + memset(connected, 0, sizeof(connected)); + scanf("%d%d%d%d", &sx, &sy, &ex, &ey); + --sx; + --sy; + --ex; + --ey; + for (char i = 0; i < R; ++i) + { + for (char j = 0; j < C; ++j) + { + scanf("%s", conns); + if (conns[0] == 'x') + { + continue; + } + for (char *L = conns; *L; L++) + { + connected[i][j][map_[*L]] = 1; + } + } + } + printf("%d\n", BFS()); + } + return 0; +} diff --git a/11686.cpp b/11686.cpp new file mode 100644 index 0000000..1fc8647 --- /dev/null +++ b/11686.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +const int NN = 1000032; + +int dep[NN], q[NN], h, t, M, N; +vector adj[NN]; + +void toposort() +{ + while (h != t) + { + int x = q[h++]; + FOREACH(adj[x], itr) + { + if (--dep[(*itr)] == 0) + { + q[t++] = (*itr); + } + } + } +} + +void fastint(register int *n) +{ + register char c; + *n = 0; + while (!isdigit(c = getc(stdin))) + ; + do + { + (*n) = (*n) * 10 + (c - '0'); + } while (isdigit(c = getc(stdin))); +} + +int main() +{ + int a, b; + while (scanf("%d%d", &N, &M) == 2 && N) + { + h = t = 0; + for (int i = 0; i < N; ++i) + { + adj[i].clear(); + dep[i] = 0; + } + for (int i = 0; i < M; ++i) + { + fastint(&a); + fastint(&b); + adj[--a].push_back(--b); + dep[b]++; + } + for (int i = 0; i < N; ++i) + { + if (dep[i] == 0) + { + q[t++] = i; + } + } + toposort(); + if (t != N) + { + printf("IMPOSSIBLE\n"); + } + else + { + for (int i = 0; i < t; ++i) + { + printf("%d\n", q[i] + 1); + } + } + } + return 0; +} diff --git a/11687.cpp b/11687.cpp index 81c5ea6..fce53d5 100644 --- a/11687.cpp +++ b/11687.cpp @@ -1,43 +1,35 @@ -#include -#include -#include -using namespace std; +#include -int toInt(string S) { - istringstream ss(S); - int I; - ss >> I; - return I; -} +using namespace std; -string toString(int I) { - stringstream ss; - string S; - ss << I; - ss >> S; - return S; +int length(int n) +{ + return (int)log10(n) + 1; } -int main(int argc, char **argv) { - while (true) { - string S; - cin >> S; - if (S == "END") break; - - if (S.length() < 30) { - if (toInt(S) == (int)S.length()) { - cout << "1\n"; - continue; - } +int main() +{ + int n; + string input; + while (getline(cin, input)) + { + if (input == "END") + { + break; } - S = toString(S.length()); - int I = 2; - while ((int)S.length() != toInt(S)) { - ++I; - S = toString(S.length()); + if (input == "1") + { + cout << 1 << endl; + continue; } - cout << I << endl; + int result = 1; + n = input.size(); + while (n != 1) + { + n = length(n); + result++; + } + cout << result + 1 << endl; } return 0; } - diff --git a/11688.cpp b/11688.cpp new file mode 100644 index 0000000..be8c55c --- /dev/null +++ b/11688.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +const int NN = 100032; + +int h[NN], height[NN], l[NN], p[NN], r[NN]; + +void ht(int i, int lh = 0, int rh = 0, int lth = 0, int rth = 0) +{ + int LH, RH; + LH = (l[i] == -1) ? 0 : h[l[i]]; + RH = (r[i] == -1) ? 0 : h[r[i]]; + height[i] = MAX(MAX(1 + lh, 1 + rh), MAX(1 + lth + LH, 1 + rth + RH)); + if (l[i] != -1) + { + ht(l[i], lh, MAX(rh, 1 + rth + RH), lth, 1 + rth); + } + if (r[i] != -1) + { + ht(r[i], MAX(lh, 1 + lth + LH), rh, 1 + lth, rth); + } +} +int dfs(int node) +{ + int h1, h2; + h1 = h2 = 0; + if (l[node] != -1) + { + h1 = dfs(l[node]); + } + if (r[node] != -1) + { + h2 = dfs(r[node]); + } + h[node] = 1 + MAX(h1, h2); + return h[node]; +} +int main() +{ + int N, root; + while (scanf("%d", &N) && N) + { + memset(p, -1, sizeof(p)); + for (int i = 0; i < N; ++i) + { + int a, b; + scanf("%d %d", &a, &b); + if (a) + { + p[a - 1] = i; + } + l[i] = --a; + if (b) + { + p[b - 1] = i; + } + r[i] = --b; + } + for (int i = 0; i < N; ++i) + if (p[i] == -1) + { + root = i; + break; + } + dfs(root); + ht(root); + for (int i = 0; i < N; ++i) + { + printf("%d\n", height[i]); + } + } + return 0; +} diff --git a/11689.cpp b/11689.cpp index 234bfcd..c0c04d5 100644 --- a/11689.cpp +++ b/11689.cpp @@ -1,19 +1,27 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i>e>>f>>c; - int init=e+f,total=0,rem=init%c; - while(init>=c){ - int fresh=init/c; - init=fresh+rem; - rem=init%c; - total+=fresh; - } - cout< + +using namespace std; + +int main() +{ + int n; + int tmp; + int result; + int e, f, c; + cin >> n; + while (n--) + { + cin >> e >> f >> c; + e += f; + result = 0; + while (e >= c) + { + tmp = e / c; + result += tmp; + e %= c; + e += tmp; + } + cout << result << endl; + } + return 0; +} diff --git a/1169.cpp b/1169.cpp new file mode 100644 index 0000000..75856af --- /dev/null +++ b/1169.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define maxn 100010 + +deque q; +int n, c, dist2orgin[maxn], x[maxn], y[maxn], sumdis[maxn], sumweight[maxn], dp[maxn]; + +int f(int i) +{ + return dp[i] - sumdis[i + 1] + dist2orgin[i + 1]; +} + +int main() +{ + int i, j, T; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &c, &n); + sumdis[0] = sumweight[0] = 0; + x[0] = y[0] = 0; + for (i = 1; i <= n; i++) + { + int w; + scanf("%d %d %d", &x[i], &y[i], &w); + dist2orgin[i] = abs(x[i]) + abs(y[i]); + sumdis[i] = sumdis[i - 1] + abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]); + sumweight[i] = sumweight[i - 1] + w; + } + q.clear(); + dp[0] = 0; + q.push_back(0); + for (i = 1; i <= n; i++) + { + while (!q.empty() && sumweight[i] - sumweight[q.front()] > c) + q.pop_front(); + if (!q.empty()) + dp[i] = f(q.front()) + sumdis[i] + dist2orgin[i]; + else + dp[i] = f(i - 1) + sumdis[i] + dist2orgin[i]; + while (!q.empty() && f(i) <= f(q.back())) + q.pop_back(); + q.push_back(i); + } + printf("%d\n", dp[n]); + if (T) + puts(""); + } + return 0; +} diff --git a/11690.cpp b/11690.cpp new file mode 100644 index 0000000..a5833f5 --- /dev/null +++ b/11690.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size, int *pay); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + int getMoney(int a); + + int size; + vector pset; + vector set_size; + vector money; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size, int *pay) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + money.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + money[i] = pay[i]; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + money[findSet(b)] += money[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int UnionFindDisjointSets::getMoney(int a) +{ + return money[findSet(a)]; +} + +int main() +{ + int t; + int n, m; + int a, b; + int pay[10001]; + cin >> t; + while (t--) + { + cin >> n >> m; + for (int i = 0; i < n; i++) + { + cin >> pay[i]; + } + UnionFindDisjointSets ds(n, pay); + while (m--) + { + cin >> a >> b; + ds.unionSet(a, b); + } + bool ok = true; + for (int i = 0; i < n; i++) + { + if (ds.getMoney(i) != 0) + { + ok = false; + break; + } + } + if (ok) + { + cout << "POSSIBLE" << endl; + } + else + { + cout << "IMPOSSIBLE" << endl; + } + } + return 0; +} diff --git a/11691.cpp b/11691.cpp new file mode 100644 index 0000000..2284334 --- /dev/null +++ b/11691.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define FOR(a, b) for (int a = 0; a < b; ++a) +#define FORI(a, b, c) for (int a = b; a < c; ++a) + +int p[20]; +char m[(1 << 20)][8]; +int A, pot2[25]; +int main() +{ + int sum, a, max, N; + scanf("%d", &N); + pot2[0] = 1; + FORI(i, 1, 25) + pot2[i] = pot2[i - 1] << 1; + while (N--) + { + scanf("%d", &A); + memset(m, 0x6f, sizeof(m)); + FOR(i, A) + scanf("%d", &p[i]); + m[0][0] = 0; + int LS = 1 << A; + FORI(i, 1, LS) + { + int x = i, pill_idx = 0; + while (x) + { + if (x & 1) + { + int wopill = i ^ pot2[pill_idx]; + FORI(old, 0, 8) + { + int stub = MAX(p[pill_idx] - old - 1, 0); + m[i][stub] = MIN(m[i][stub], m[wopill][old] + stub + 1); + } + } + x = x >> 1; + pill_idx++; + } + } + int ans = 90; + FORI(i, 0, 8) + ans = MIN(ans, m[LS - 1][i]); + printf("%d\n", ans); + } +} diff --git a/11692.cpp b/11692.cpp new file mode 100644 index 0000000..da3775e --- /dev/null +++ b/11692.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +#define EPS 1E-10 +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +double sqr(double x) +{ + return (x * x); +} +int main() +{ + double L, K, T1, T2, H, Hmin, Hmax, Tf1, Tf2, TT; + int N; + scanf("%d", &N); + while (N--) + { + scanf("%lf %lf %lf %lf %lf", &L, &K, &T1, &T2, &H); + TT = T1 + T2; + Hmin = Hmax = H; + if (H >= L) + { + double b = K * TT + H; + double delta = sqr(b) - 4 * T1 * K * L; + //printf("b = %.5lf, delta = %.5lf\n",b,delta); + if (delta >= 0.) + { + double root = sqrt(delta); + double x1 = (b + root) / (2 * T1); + double x2 = (b - root) / (2 * T1); + Hmax = T1 * x1; + } + if (H > L) + { + Hmin = Hmax; + } + } + printf("%.6lf %.6lf\n", Hmin, Hmax); + } + return 0; +} diff --git a/11693.cpp b/11693.cpp new file mode 100644 index 0000000..15c129d --- /dev/null +++ b/11693.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define INF 0x7f7f7f7f +#define NN 128 +#define MIN_V 1e-9 +#define FOR(a, b) for (int a = 0; a < b; ++a) + +static int w[NN][NN], n, a, b, e, l, m, B, N, P; +static bool exit_[NN]; +static double time_p[NN]; + +int dijkstra(int s, int t) +{ + bool in[NN]; + int d[NN], u; + FOR(i, n) + d[i] = w[s][i], + in[i] = 0; + d[s] = 0; + while (!in[t]) + { + int best = INF; + for (int i = 0; i < n; i++) + { + if (!in[i] && best > d[i]) + { + best = d[u = i]; + } + } + if (best == INF) + { + break; + } + in[u] = 1; + for (int i = 0; i < n; i++) + { + if (!in[i] && w[u][i] < INF && d[i] > d[u] + w[u][i]) + { + d[i] = d[u] + w[u][i]; + } + } + } + return d[t]; +} +int dijkstraRUN(int s, double vel) +{ + bool in[NN]; + int d[NN], u; + FOR(i, n) + d[i] = w[s][i], + in[i] = 0; + d[s] = 0; + for (;;) + { + int best = INF; + FOR(i, n) + if (!in[i] && best > d[i]) + { + best = d[u = i]; + } + if (best == INF) + { + break; + } + in[u] = 1; + if (best / vel > time_p[u]) + { + continue; + } + if (exit_[u]) + { + return 1; + } + FOR(i, n) + if (!in[i] && w[u][i] < INF) + { + int temp = d[u] + w[u][i]; + if (temp < d[i]) + { + d[i] = temp; + } + } + } + return 0; +} + +int main() +{ + scanf("%d", &N); + while (N--) + { + memset(w, 0x7f, sizeof(w)); + memset(exit_, 0, sizeof(exit_)); + scanf("%d%d%d", &n, &m, &e); + FOR(i, m) + { + scanf("%d%d%d", &a, &b, &l); + w[--a][--b] = w[b][a] = l; + } + FOR(i, e) + { + scanf("%d", &a); + exit_[--a] = 1; + } + scanf("%d %d", &B, &P), --B, --P; + FOR(i, n) + { + time_p[i] = dijkstra(P, i) / (double)(160); + } + double u = 0.0f, v = 10000000.0f; + FOR(i, 60) + { + double c = (u + v) / 2; + if (dijkstraRUN(B, c)) + { + v = c; + } + else + { + u = c + MIN_V; + } + } + if (dijkstraRUN(B, u)) + { + printf("%.9lf\n", u); + } + else + { + printf("IMPOSSIBLE\n"); + } + } + return 0; +} diff --git a/11694.cpp b/11694.cpp new file mode 100644 index 0000000..2b2b60e --- /dev/null +++ b/11694.cpp @@ -0,0 +1,253 @@ +#include + +using namespace std; + +char a[10][10]; +struct node +{ + vector pre; +} dp[10][128]; +int s[10], n, ans[10]; +// 0 \ +// 1 / +bool check(int i) +{ + int j; + for (j = 0; j <= n; ++j) + { + if (s[j] == 0 && j != 0 && j != n && i != 0 && i != n) + { + break; + } + if (a[i][j] != '.') + { + if (a[i][j] != s[j] + '0') + { + break; + } + } + } + if (j == n + 1) + { + return true; + } + else + { + return false; + } +} +bool judge1(int i, int k) +{ + int ii; + memset(s, 0, sizeof(s)); + for (ii = 0; ii < n; ++ii) + { + if (k % 2 == 0) + { + s[ii]++; + } + else + { + s[ii + 1]++; + } + k /= 2; + } + return check(i); +} +bool judge2(int i, int j, int k) +{ + int ii; + memset(s, 0, sizeof(s)); + for (ii = 0; ii < n; ++ii) + { + if (j % 2 == 0) + { + s[ii + 1]++; + } + else + { + s[ii]++; + } + j /= 2; + } + for (ii = 0; ii < n; ++ii) + { + if (k % 2 == 0) + { + s[ii]++; + } + else + { + s[ii + 1]++; + } + k /= 2; + } + return check(i); +} +bool judge3(int i, int j) +{ + int ii; + memset(s, 0, sizeof(s)); + for (ii = 0; ii < n; ++ii) + { + if (j % 2 == 0) + { + s[ii + 1]++; + } + else + { + s[ii]++; + } + j /= 2; + } + return check(i); +} +void print() +{ + int i, j; + for (i = 0; i < n; ++i) + { + for (j = 0; j < n; ++j) + { + if (ans[i] & (1 << j)) + { + printf("/"); + } + else + { + printf("\\"); + } + } + printf("\n"); + } +} +// 0 \ +// 1 / +/// +int f[100]; +void init() +{ + int i; + for (i = 0; i < 80; i++) + { + f[i] = i; + } +} +int find(int x) +{ + if (x == f[x]) + { + return x; + } + f[x] = find(f[x]); + return f[x]; +} +//// +bool che() +{ + int i, k, a, b; + init(); + for (i = 0; i < n; i++) + { + for (k = 0; k < n; k++) + { + if (ans[i] & (1 << k)) + { + a = (i + 1) * (n + 1) + k; + b = i * (n + 1) + k + 1; + } + else + { + a = (i) * (n + 1) + k; + b = (i + 1) * (n + 1) + k + 1; + } + a = find(a); + b = find(b); + if (a == b) + { + return false; + } + f[a] = b; + } + } + return true; +} +bool dfs(int i, int j) +{ + int k; + if (i < 0) + { + if (che()) + { + print(); + return true; + } + else + { + return false; + } + } + ans[i] = j; + for (k = 0; k < dp[i][j].pre.size(); ++k) + { + if (dfs(i - 1, dp[i][j].pre[k])) + { + return true; + } + } + return false; +} +int main() +{ + int i, j, k, t; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i <= n; ++i) + { + scanf("%s", a[i]); + } + for (i = 0; i < n; ++i) + { + for (j = 0; j < (1 << n); ++j) + { + dp[i][j].pre.clear(); + } + } + for (j = 0; j < (1 << n); ++j) + { + if (judge1(0, j)) + { + dp[0][j].pre.push_back(0); + } + } + for (i = 0; i < n - 1; ++i) + { + for (j = 0; j < (1 << n); ++j) + { + if (!dp[i][j].pre.empty()) + { + for (k = 0; k < (1 << n); ++k) + { + if (judge2(i + 1, j, k)) + { + dp[i + 1][k].pre.push_back(j); + } + } + } + } + } + for (k = 0; k < (1 << n); ++k) + { + if ((!dp[n - 1][k].pre.empty()) && judge3(n, k)) + { + if (dfs(n - 1, k)) + { + break; + } + } + } + } + return 0; +} diff --git a/11695.cpp b/11695.cpp new file mode 100644 index 0000000..1a46439 --- /dev/null +++ b/11695.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +int n, m, ii, ret, x[3000], y[3000], dep[3000], pre[3000], c[3000], cc[3000]; +vector v[3000]; + +void dfs(int kk, int de) +{ + int i; + if (dep[kk] >= de || c[kk] == 1) + { + return; + } + dep[kk] = de; + c[kk] = 1; + for (i = 0; i < v[kk].size(); i++) + if ((kk == x[ii] && v[kk][i] == y[ii]) || (kk == y[ii] && v[kk][i] == x[ii])) + ; + else if (c[v[kk][i]] == 0) + { + pre[v[kk][i]] = kk; + dfs(v[kk][i], de + 1); + } +} + +int max(int aa, int bb) +{ + if (aa < bb) + { + return aa; + } + return bb; +} + +int solve(int kk) +{ + int i, j, k, l; + for (i = 1; i <= n; i++) + { + c[i] = 0; + dep[i] = 0; + } + dfs(kk, 1); + k = 1; + for (i = 1; i <= n; i++) + if (dep[i] > dep[k]) + { + k = i; + } + //printf("sol %d %d\n",k,dep[k]); + for (i = 1; i <= n; i++) + { + c[i] = 0; + dep[i] = 0; + } + dfs(k, 1); + k = 1; + for (i = 1; i <= n; i++) + if (dep[i] > dep[k]) + { + k = i; + } + //printf("sol %d %d\n",k,dep[k]); + //for(i=1;i<=n;i++) printf("pre %d %d\n",i,pre[i]); + l = k; + while (dep[l] > dep[k] - dep[l] + 1) + { + //if(l != 0)printf("test %d %d\n",l,dep[l]); + l = pre[l]; + } + //printf("ll = %d %d\n",l,dep[l]); + for (i = 1; i <= n; i++) + if (c[i] != 0) + { + cc[i] = c[i]; + } + ret += max(dep[l], dep[k] - dep[l]); + return l; +} + +int add1, add2; + +void dfss(int kk, int de) +{ + int i; + if (dep[kk] >= de || c[kk] == 1) + { + return; + } + dep[kk] = de; + c[kk] = 1; + for (i = 0; i < v[kk].size(); i++) + if ((kk == x[ii] && v[kk][i] == y[ii]) || (kk == y[ii] && v[kk][i] == x[ii])) + ; + else if (c[v[kk][i]] == 0) + { + pre[v[kk][i]] = kk; + dfss(v[kk][i], de + 1); + } + if (kk == add1) + { + if (c[add2] == 0) + { + pre[add2] = kk; + dfss(add2, de + 1); + } + } + if (kk == add2) + { + if (c[add1] == 0) + { + pre[add1] = kk; + dfss(add1, de + 1); + } + } +} + +int maxtop() +{ + int i, j, k, l; + for (i = 1; i <= n; i++) + { + c[i] = 0; + dep[i] = 0; + } + dfss(1, 1); + k = 1; + for (i = 1; i <= n; i++) + if (dep[i] > dep[k]) + { + k = i;//for(i=1;i<=n;i++) printf(" %d %d\n",i,dep[i]); + } + //printf("sol %d %d\n",k,dep[k]); + for (i = 1; i <= n; i++) + { + c[i] = 0; + dep[i] = 0; + } + dfss(k, 1); + k = 1; + for (i = 1; i <= n; i++) + if (dep[i] > dep[k]) + { + k = i; + } + //for(i=1;i<=n;i++) printf(" %d %d\n",i,dep[i]); + return dep[k]; +} + +int main() +{ + int i, j, k, l, ans, a1, a2, a3, a[7]; + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + v[i].clear(); + } + for (i = 1; i < n; i++) + { + scanf("%d %d", &x[i], &y[i]); + v[x[i]].push_back(y[i]); + v[y[i]].push_back(x[i]); + } + ans = 1000000000; + for (ii = 1; ii < n; ii++) + { + for (i = 1; i <= n; i++) + { + cc[i] = 0; + } + k = 0; + ret = 0; + for (i = 1; i <= n; i++) + if (cc[i] == 0) + { + a[k++] = solve(i); + } + add1 = a[0]; + add2 = a[1]; + ret = maxtop(); + //printf("ii = %d %d %d\n",ii,x[ii],y[ii]); + //printf("%d %d %d %d %d %d\n\n",ret,k,a[0],a[1],add1,add2); + if (ret < ans) + { + ans = ret; + a1 = ii; + a2 = a[0]; + a3 = a[1]; + } + } + printf("%d\n", ans - 1); + printf("%d %d\n", x[a1], y[a1]); + printf("%d %d\n", a2, a3); + } + return 0; +} diff --git a/11696.cpp b/11696.cpp new file mode 100644 index 0000000..3e47502 --- /dev/null +++ b/11696.cpp @@ -0,0 +1,254 @@ +#include + +using namespace std; + +const double EPS = 1e-10; +const double pi = acos(-1.0); + +struct POINT +{ + double x, y; +} p[1205]; +struct CIRCLE +{ + POINT c; + double r; +} cir[1205]; +struct AUR +{ + double ang; + int idx; + bool flag; + double dis; +} aur[8005]; +double ds[8005]; +int heap[8005], id[8005], hsize; +int n, m, len; +int fa[1205]; +bool use[1205]; +int dmp(double k) +{ + if (fabs(k) < EPS) + { + return 0; + } + return k < 0 ? -1 : 1; +} +double dis(POINT &p1, POINT &p2) +{ + return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); +} +void addaur(int i, int j) +{ + double dx = dis(p[i], cir[j].c); + double d = sqrt(dx * dx - cir[j].r * cir[j].r); + double deta = asin(cir[j].r / dx); + double theta = atan2(cir[j].c.y - p[i].y, cir[j].c.x - p[i].x); + double ang1 = theta - deta; + double ang2 = theta + deta; + while (dmp(ang1 + pi) <= 0) + { + ang1 += 2 * pi; + } + while (dmp(ang1 - pi) > 0) + { + ang1 -= 2 * pi; + } + while (dmp(ang2 + pi) <= 0) + { + ang2 += 2 * pi; + } + while (dmp(ang2 - pi) > 0) + { + ang2 -= 2 * pi; + } + ds[j] = d; + if (ang1 < ang2) + { + aur[len].ang = ang1; + aur[len].idx = -j; + aur[len].flag = 0; + aur[len++].dis = d; + aur[len].ang = ang2; + aur[len].flag = 1; + aur[len].idx = -j; + aur[len++].dis = d; + } + else + { + aur[len].ang = -pi - EPS; + aur[len].idx = -j; + aur[len].flag = 0; + aur[len++].dis = d; + aur[len].ang = ang2; + aur[len].idx = -j; + aur[len].flag = 1; + aur[len++].dis = d; + aur[len].ang = ang1; + aur[len].idx = -j; + aur[len].flag = 0; + aur[len++].dis = d; + aur[len].ang = pi + EPS; + aur[len].idx = -j; + aur[len].flag = 1; + aur[len++].dis = d; + } +} +bool cmp(const AUR &a1, const AUR &a2) +{ + return a1.ang < a2.ang; +} +bool cmpt(const int &a, const int &b) +{ + return dmp(ds[a] - ds[b]) < 0; +} +void swim(int u) +{ + if (u > hsize) + { + return; + } + while (u > 1) + { + int t = u >> 1; + if (cmpt(heap[u], heap[t])) + { + swap(id[heap[u]], id[heap[t]]); + swap(heap[u], heap[t]); + u = t; + } + else + { + break; + } + } +} +void sink(int u) +{ + while (u <= hsize) + { + int p = u; + int l = u << 1, r = l + 1; + if (l <= hsize && cmpt(heap[l], heap[p])) + { + p = l; + } + if (r <= hsize && cmpt(heap[r], heap[p])) + { + p = r; + } + if (p == u) + { + break; + } + swap(id[heap[u]], id[heap[p]]); + swap(heap[p], heap[u]); + u = p; + } +} +void inseart(int u) +{ + heap[++hsize] = u; + id[u] = hsize; + swim(hsize); +} +void erase(int u) +{ + id[heap[hsize]] = id[u]; + heap[id[u]] = heap[hsize]; + hsize--; + swim(id[u]); + sink(id[u]); +} +int fint(int k) +{ + if (fa[k] == k) + { + return k; + } + return fa[k] = fint(fa[k]); +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < n; i++) + { + fa[i] = i; + } + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + } + for (int i = 0; i < m; i++) + { + scanf("%lf%lf%lf", &cir[i].c.x, &cir[i].c.y, &cir[i].r); + } + if (m == 0) + { + printf("0\n"); + continue; + } + memset(use, 0, sizeof(use)); + for (int i = 0; i < n; i++) + { + len = 0; + for (int j = i + 1; j < n; j++) + { + aur[len].idx = j; + aur[len].dis = dis(p[i], p[j]); + aur[len++].ang = atan2(p[j].y - p[i].y, p[j].x - p[i].x); + } + for (int j = 0; j < m; j++) + { + addaur(i, j); + } + sort(aur, aur + len, cmp); + hsize = 0; + for (int j = 0; j < len; j++) + { + if (aur[j].idx > 0) + { + if (hsize == 0 || dmp(ds[heap[1]] - aur[j].dis) > 0) + { + int f1 = fint(i); + int f2 = fint(aur[j].idx); + if (f1 != f2) + { + fa[f1] = f2; + } + } + } + else + { + int idx = aur[j].idx * -1; + if (!use[idx]) + { + use[idx] = 1; + inseart(idx); + } + else + { + use[idx] = 0; + erase(idx); + } + } + } + } + for (int i = 0; i < n; i++) + { + use[fint(i)] = 1; + } + int ans = 0; + for (int i = 0; i < n; i++) + if (use[i]) + { + ans++; + } + printf("%d\n", ans - 1); + } + return 0; +} diff --git a/11697.cpp b/11697.cpp new file mode 100644 index 0000000..01eb335 --- /dev/null +++ b/11697.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +bool seen[128]; +char map_[5][5], whereis[128], row[128], col[128], phrase[2048], tocipher[2048], cipherTIME[2048]; + +void encrypt(char a, char b) +{ + if (row[a] == row[b]) + { + a = map_[row[a]][(col[a] + 1) % 5]; + b = map_[row[b]][(col[b] + 1) % 5]; + } + else if (col[a] == col[b]) + { + a = map_[(row[a] + 1) % 5][col[a]]; + b = map_[(row[b] + 1) % 5][col[b]]; + } + else + { + int r1 = row[a], r2 = row[b]; + int c1 = col[a], c2 = col[b]; + a = map_[r1][c2]; + b = map_[r2][c1]; + } + putc(a, stdout); + putc(b, stdout); +} + +int main() +{ + int N, map__pos; + scanf("%d\n", &N); + while (N--) + { + memset(seen, 0, sizeof(seen)); + memset(phrase, 0, 2048); + memset(tocipher, 0, 2048); + fgets(phrase, 2048, stdin); + fgets(tocipher, 2048, stdin); + char *c = phrase; + map__pos = 0; + for (char *c = phrase; *c; ++c) + { + if (islower(*c)) + { + *c -= 32; + } + if (!isupper(*c) || seen[*c]) + { + continue; + } + map_[map__pos / 5][map__pos % 5] = *c; + row[*c] = map__pos / 5; + col[*c] = map__pos % 5; + seen[*c] = 1; + map__pos++; + } + for (char c = 'A'; c <= 'Z'; ++c) + { + if (seen[c] || c == 'Q') + { + continue; + } + map_[map__pos / 5][map__pos % 5] = c; + row[c] = map__pos / 5; + col[c] = map__pos % 5; + map__pos++; + } + char *ct = cipherTIME; + for (c = tocipher; *c; ++c) + if (islower(*c)) + { + *ct++ = (*c) - 32; + } + *ct = *(ct + 1) = 0; + for (char *c = cipherTIME, *n;;) + { + if (!(*c)) + { + break; + } + n = c + 1; + if (*c == *n || (*n == 0)) + { + encrypt(*c, 'X'); + --n; + } + else + { + encrypt(*c, *n); + } + c = n + 1; + } + putc(10, stdout); + } + return 0; +} diff --git a/11698.cpp b/11698.cpp new file mode 100644 index 0000000..54564bd --- /dev/null +++ b/11698.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +int ln, sn; +long long c[105][105], dp[105][2005], f[105], lst[2005], stk[2005]; + +int gcd(int p, int q) +{ + return p % q ? gcd(q, p % q) : q; +} + +int find(int t) +{ + int p, q, c; + for (p = 0, q = ln - 1; p <= q;) + { + c = ((p + q) >> 1); + if (lst[c] == t) + { + return c; + } + else if (lst[c] > t) + { + q = c - 1; + } + else + { + p = c + 1; + } + } + return -1; +} + +int main() +{ + int count, n, m, i, j, k, l, p, q, r, sq; + long long s, t; + c[0][0] = 1; + f[0] = 1; + for (i = 1; i <= 100; i++) + { + c[i][0] = 1; + f[i] = (f[i - 1] * i) % 2147483647; + for (j = 1; j <= 100; j++) + { + c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 2147483647; + } + } + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &n, &m); + for (i = 2, r = m; i <= 100; i++) + { + if (r % i == 0) + { + while ((r /= i) % i == 0) + ; + } + } + if (r != 1) + { + printf("0\n"); + continue; + } + for (i = 1, ln = 0, sn = 0, sq = (int)sqrt((double)m); i <= sq; i++) + { + if (m % i == 0) + { + lst[ln++] = i; + if (m != i * i) + { + stk[sn++] = m / i; + } + } + } + for (i = sn - 1; i >= 0; i--) + { + lst[ln++] = stk[i]; + } + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (j = 0; j < n; j++) + { + for (i = 1; i + j <= n; i++) + { + for (k = ln - 1; k >= 0; k--) + { + if (m % (t = (lst[k] * i / gcd(lst[k], i)))) + { + continue; + } + r = find((int)t); + if (dp[j][k] == 0) + { + continue; + } + dp[j + i][r] += (dp[j][k] * c[n - j - 1][i - 1] % 2147483647) * f[i - 1] % 2147483647; + dp[j + i][r] %= 2147483647; + } + } + } + printf("%lld\n", dp[n][ln - 1]); + } + return 0; +} diff --git a/11699.cpp b/11699.cpp new file mode 100644 index 0000000..c7c21d2 --- /dev/null +++ b/11699.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +#define IN getc(stdin) +#define OUT(x) putc(x, stdout) +#define FORI(a, b, c) for (int a = b; a < c; ++a) +#define FOR(a, b) FORI(a, 0, b) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define ONES(x) __builtin_popcount(x) + +const int C = 15, R = 15; +int p2[32]; +char m[R][C]; +int main() +{ + register char c; + p2[0] = 1; + FORI(i, 1, 16) + p2[i] = p2[i - 1] << 1; + while ((c = IN) != 'E') + { + int ans = 16, __MMAX = 1 << 15; + ungetc(c, stdin); + char *p = *m; + FOR(i, R) + { + FOR(j, C) *p++ = IN; + IN; + } + FOR(mask, __MMAX) + { + int om = ONES(mask); + if (om >= ans) + { + continue; + } + int cc = 0; + FOR(i, R) + if (!(p2[i] & mask)) + { + FOR(j, C) + cc |= (p2[j] * (m[i][j] == '#')); + } + ans = MIN(ans, MAX(ONES(cc), om)); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/117.cpp b/117.cpp new file mode 100644 index 0000000..401fa7a --- /dev/null +++ b/117.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +const int oo = 0x3fffffff; +const int MAXSIZE = 26; + +struct Graph +{ + int matrix[MAXSIZE][MAXSIZE]; + int degree[MAXSIZE]; + bool HaveOddDegree; + int OddDegree[2]; + int dist[MAXSIZE], path[MAXSIZE]; + void CheckOddDegree(); + void Dijkstra(int start); + + Graph(); + void Init(); + void ReadGraph(char *); + int Solve(); + void Print(); +}; + +Graph::Graph() +{ + this->Init(); +} +void Graph::Init() +{ + int i, j; + for (i = 0; i < MAXSIZE; i++) + { + for (j = 0; j < MAXSIZE; j++) + { + matrix[i][j] = oo; + } + } + memset(degree, 0, sizeof(degree)); + HaveOddDegree = false; +} +void Graph::ReadGraph(char *str) +{ + char from = str[0]; + char dest = str[strlen(str) - 1]; + int weight = strlen(str); + matrix[from - 'a'][dest - 'a'] = matrix[dest - 'a'][from - 'a'] = weight; + degree[from - 'a']++; + degree[dest - 'a']++; +} +void Graph::CheckOddDegree() +{ + for (int i = 0; i < MAXSIZE; i++) + { + if (degree[i] % 2 == 1) + { + if (HaveOddDegree == false) + { + OddDegree[0] = i; + } + else + { + OddDegree[1] = i; + } + HaveOddDegree = true; + } + } +} +void Graph::Dijkstra(int start) +{ + bool vis[MAXSIZE]; + for (int i = 0; i < MAXSIZE; i++) + { + dist[i] = matrix[start][i]; + path[i] = start; + vis[i] = false; + } + dist[start] = 0; + for (int i = 0; i < MAXSIZE; i++) + { + int min = oo; + int k = 0; + for (int j = 0; j < MAXSIZE; j++) + if (!vis[j] && dist[j] < min) + { + min = dist[j]; + k = j; + } + vis[k] = true; + for (int j = 0; j < MAXSIZE; j++) + if (!vis[j] && dist[k] + matrix[k][j] < dist[j]) + { + dist[j] = dist[k] + matrix[k][j]; + path[j] = k; + } + } +} +int Graph::Solve() +{ + CheckOddDegree(); + if (HaveOddDegree) + { + Dijkstra(OddDegree[0]); + } + int i, j; + int sum = 0; + for (i = 0; i < MAXSIZE; i++) + for (j = 0; j < MAXSIZE; j++) + { + if (matrix[i][j] == oo) + { + continue; + } + sum += matrix[i][j]; + } + sum /= 2; + if (HaveOddDegree) + { + sum += dist[OddDegree[1]]; + } + return sum; +} +void Graph::Print() +{ + for (int k = 0; k < MAXSIZE; k++) + { + printf("%c %4d\n", 'a' + k, degree[k]); + } + if (!HaveOddDegree) + { + return; + } + printf("The Shortest Path from %c to %c is %d\n", + OddDegree[0] + 'a', OddDegree[1] + 'a', dist[OddDegree[1]]); + for (int k = 0; k < MAXSIZE; k++) + { + printf("%c ", k + 'a'); + } + printf("\n"); + for (int k = 0; k < MAXSIZE; k++) + { + printf("%c ", path[k] + 'a'); + } + printf("\n"); +} + +int main() +{ + Graph g; + char buffer[100]; + while (cin >> buffer) + { + if (!strcmp(buffer, "deadend")) + { + int result = g.Solve(); + cout << result << endl; + g.Init(); + continue; + } + g.ReadGraph(buffer); + } + return 0; +} diff --git a/1170.cpp b/1170.cpp new file mode 100644 index 0000000..3f34d03 --- /dev/null +++ b/1170.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +typedef long long LL; + +#define PB push_back +#define FRO freopen("in.txt", "r", stdin); + +#define CLR(arr) memset((arr), 0, sizeof(arr)); +#define NEG(arr) memset((arr), -1, sizeof(arr)); + +typedef pair pint; +typedef map mint; + +#define SIZE 305 + +int row, col; + +int board[SIZE][SIZE]; + +bool vis[SIZE][SIZE][7][6]; + +int num[SIZE][SIZE]; + +struct node +{ + int r, c, p, left; + int move; + int prv; +}; + +int a, b, c, d; + +int dr[] = {-1, 0, 1, 0}; +int dc[] = {0, -1, 0, 1}; + +int bfs() +{ + + node start, tmp; + start.r = a; + start.c = b; + start.move = 0; + if (board[start.r][start.c] > 1) + { + start.p = board[start.r][start.c] % 10; + start.left = board[start.r][start.c] / 10; + start.prv = num[start.r][start.c]; + } + else if (board[start.r][start.c] == 1) + { + start.p = 1; + start.left = 0; + start.prv = 0; + } + else + { + return -1; + } + + CLR(vis); + + queue q; + + q.push(start); + + while (!q.empty()) + { + node f = q.front(); + q.pop(); + //cout<= 0 && tmp.r < row && tmp.c >= 0 && tmp.c < col && board[tmp.r][tmp.c] > 0) + { + tmp.move = f.move + 1; + if (board[tmp.r][tmp.c] > 1 && num[tmp.r][tmp.c] != tmp.prv) + { + tmp.p = board[tmp.r][tmp.c] % 10; + tmp.left = board[tmp.r][tmp.c] / 10; + tmp.prv = num[tmp.r][tmp.c]; + } + else + { + if (f.p != 1 && f.left > 1) + { + tmp.p = f.p; + tmp.left = f.left - 1; + tmp.prv = f.prv; + } + else + { + tmp.p = 1; + tmp.left = 0; + tmp.prv = 0; + } + } + if (!vis[tmp.r][tmp.c][tmp.p][tmp.left]) + { + q.push(tmp); + vis[tmp.r][tmp.c][tmp.p][tmp.left] = true; + } + } + } + } + return -1; +} + +int main() +{ + //FRO + + int kase; + scanf("%d", &kase); + + while (kase--) + { + scanf("%d %d", &row, &col); + + for (int i = 0; i < row; ++i) + { + for (int j = 0; j < col; ++j) + { + scanf("%d", &board[i][j]); + } + } + + int total = 1; + for (int i = 0; i < row; ++i) + { + for (int j = 0; j < col; ++j) + { + if (board[i][j] > 1) + { + num[i][j] = total++; + } + else + { + num[i][j] = 0; + } + } + } + + scanf("%d %d %d %d", &a, &b, &c, &d); + + int ans = bfs(); + + if (ans != -1) + printf("%d\n", ans); + else + printf("IMPOSSIBLE\n"); + + if (kase) + printf("\n"); + } + + return 0; +} diff --git a/11700.cpp b/11700.cpp new file mode 100644 index 0000000..5e301ec --- /dev/null +++ b/11700.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +bool table[12][12][4]; +bool inp[12][12][4]; +#define N 0 +#define E 1 +#define S 2 +#define W 3 + +int dx[] = {0, 1, 0, -1}; +int dy[] = {-1, 0, 1, 0}; +int od[] = {2, 3, 0, 1};//opposite direction + +bool solve(int r, int c, int now) +{ + if (r * c == now) + { + return true; + } + int y = now / c, x = now % c; + rep(i, 4) + { + table[y][x][N] = inp[y][x][(N + i) % 4]; + table[y][x][E] = inp[y][x][(E + i) % 4]; + table[y][x][S] = inp[y][x][(S + i) % 4]; + table[y][x][W] = inp[y][x][(W + i) % 4]; + if (i == 1 && ((inp[y][x][N] == 0 && inp[y][x][E] == 0 && inp[y][x][S] == 0 && inp[y][x][W] == 0) || + (inp[y][x][N] == 1 && inp[y][x][E] == 1 && inp[y][x][S] == 1 && inp[y][x][W] == 1))) + { + return false; + } + if (i == 2 && ((inp[y][x][N] == 0 && inp[y][x][E] == 1 && inp[y][x][S] == 0 && inp[y][x][W] == 1) || + (inp[y][x][N] == 1 && inp[y][x][E] == 0 && inp[y][x][S] == 1 && inp[y][x][W] == 0))) + { + return false; + } + bool flag = true; + rep(j, 4) + { + int nex = x + dx[j], ney = y + dy[j]; + if (nex < 0) + { + if (table[y][x][W] == true) + { + flag = false; + break; + } + continue; + } + if (nex >= c) + { + if (table[y][x][E] == true) + { + flag = false; + break; + } + continue; + } + if (ney < 0) + { + if (table[y][x][N] == true) + { + flag = false; + break; + } + continue; + } + if (ney >= r) + { + if (table[y][x][S] == true) + { + flag = false; + break; + } + continue; + } + if (now < ney * c + nex) + { + continue; + } + if (table[y][x][j] != table[ney][nex][od[j]]) + { + flag = false; + break; + } + } + if (flag == false) + { + continue; + } + if (solve(r, c, now + 1)) + { + return true; + } + } + return false; +} + +main() +{ + int r, c; + while (cin >> r >> c && r) + { + rep(i, r) + { + rep(j, c) + { + string a; + cin >> a; + inp[i][j][0] = inp[i][j][1] = inp[i][j][2] = inp[i][j][3] = false; + if (a == "x") + { + continue; + } + rep(k, a.size()) + { + if (a[k] == 'N') + { + inp[i][j][N] = true; + } + if (a[k] == 'W') + { + inp[i][j][W] = true; + } + if (a[k] == 'E') + { + inp[i][j][E] = true; + } + if (a[k] == 'S') + { + inp[i][j][S] = true; + } + } + } + } + if (solve(r, c, 0)) + { + puts("SOLVABLE"); + } + else + { + puts("UNSOLVABLE"); + } + } +} diff --git a/11701.cpp b/11701.cpp new file mode 100644 index 0000000..9b0d2fb --- /dev/null +++ b/11701.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +#define PREC 17 +#define EPS 1E-7 +#define FORI(a, b, c) for (int a = b; a < c; ++a) + +double p[50]; +int main() +{ + double div = 3.; + FORI(i, 1, PREC) + { + p[i] = (2 / div); + div *= 3.; + } + double N; + while (scanf("%lf", &N) == 1) + { + for (int i = 1; i < PREC && N > EPS; i++) + { + N -= p[i] * (N - p[i] > 0.); + } + (N > EPS) ? puts("NON-MEMBER") : puts("MEMBER"); + } + return 0; +} diff --git a/11702.cpp b/11702.cpp new file mode 100644 index 0000000..7b293d9 --- /dev/null +++ b/11702.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define FOR(a, b) for (int a = 0; a < b; ++a) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +//#define DEBUG + +struct point +{ + int x, y; +} p[38]; +struct edge +{ + point *a, *b; +} e[38]; +int N, xp, yp, v, c; +double q(double x) +{ + return (x * x); +} +double melt(int a, double d) +{ + double x = (*e[a].a).x * (1 - d) + (*e[a].b).x * d; + double y = (*e[a].a).y * (1 - d) + (*e[a].b).y * d; + double ret = sqrt(q(xp - x) + q(yp - y)) / c + y / v; +#ifdef DEBUG + printf("Called %d, %d, %.5lf, r = %.5lf\n", c, b, d, ret); +#endif + return ret; +} +void showedge(int n) +{ + point p1 = *e[n].a, p2 = *e[n].b; + printf("(%d,%d)-(%d,%d)\n", p1.x, p1.y, p2.x, p2.y); +} +int main() +{ + while (scanf("%d%d%d%d%d", &N, &xp, &yp, &v, &c) && N) + { + FOR(i, N) + scanf("%d%d", &p[i].x, &p[i].y); + FOR(i, N) + e[i].a = p + i, + e[i].b = p + ((i + 1) % N); + double meltdown = 1e17; + FOR(i, N) + { + double left = 0, right = 1; + FOR(k, 100) + { + double L = (2 * left + right) / 3, R = (left + 2 * right) / 3; + if (melt(i, L) < melt(i, R)) + { + right = R; + } + else + { + left = L; + } + } + left = melt(i, left); +#ifdef DEBUG + printf("Edge: "); + showedge(i); + printf("\tleft = %.4lf\n", left); +#endif + meltdown = MIN(meltdown, left); + } + printf("%.4lf\n", meltdown); + } + return 0; +} diff --git a/11703.cpp b/11703.cpp index 81ff973..eaf8fd7 100644 --- a/11703.cpp +++ b/11703.cpp @@ -1,59 +1,25 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) - -#define MAX 1000000 +#include -int dp[MAX + 1]; - -int memo(int I) { - if (dp[I] != -1) - return dp[I]; - int A = memo(int(I - sqrt(I))) % MAX; - int B = memo(int(log(I))) % MAX; - int C = memo(int(I * sin(I) * sin(I))) % MAX; - dp[I] = (A + B + C) % MAX; - return dp[I]; -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - memset(dp, -1, sizeof dp); - dp[0] = 1; - while (true) { - int N; - scanf("%d", &N); - if (N == -1) break; - printf("%d\n", memo(N)); +int main() +{ + int input; + int *n = new int[1000001]; + n[0] = 1; + for (int i = 1; i < 1000001; i++) + { + n[i] = n[(int)floor(i - sqrt(i))] + n[(int)floor(log(i))] + n[(int)floor(i * sin(i) * sin(i))]; + n[i] %= 1000000; + } + while (cin >> input) + { + if (input == -1) + { + break; + } + cout << n[input] << endl; } + delete[] n; return 0; } diff --git a/11704.cpp b/11704.cpp new file mode 100644 index 0000000..9432ab4 --- /dev/null +++ b/11704.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back + +typedef complex P; + +const double PI = 2 * acos(0); +const double eps = 1e-10; + +class state +{ +public: + double ang; + bool isc, isupper; + bool operator<(const state &a) const + { + return ang < a.ang; + } +}; + +int solve(vector &in, int c, int p) +{ + int bc = 0, bp = 0; + double st = in[0].ang - eps, ste = in[0].ang + PI - eps; + rep(i, in.size()) + { + double tmp = in[i].ang; + if (in[i].isupper) + { + tmp += PI; + } + if (st - eps < tmp && tmp < ste + eps) + { + if (in[i].isc) + { + bc++; + } + else + { + bp++; + } + } + } + if (c - bc == bc && p - bp == bp) + { + return true; + } + rep(i, in.size()) + { + if (in[i].isupper) + { + if (in[i].isc) + { + bc++; + } + else + { + bp++; + } + } + else + { + if (in[i].isc) + { + bc--; + } + else + { + bp--; + } + } + if ((i + 1 == in.size() + || in[i + 1].ang - in[i].ang > 1e-7) + && c - bc == bc && p - bp == bp) + { + return true; + } + } + return false; +} + +int main() +{ + int c, p; + while (scanf("%d%d", &c,&p) == 2 && c != -1) + { + vector in; + double ang; + rep(i, c) + { + double re, im; + scanf("%lf%lf", &re,&im); + P tmp = P(re,im); + ang = atan2(im, re); + if (ang >= 0) + { + in.pb((state){ang - PI, true, true}); + } + else + { + in.pb((state){ang, true, false}); + } + } + rep(j, p) + { + double re, im; + scanf("%lf%lf", &re,&im); + P tmp = P(re,im); + ang = atan2(im, re); + if (ang >= 0) + { + in.pb((state){ang - PI, false, true}); + } + else + { + in.pb((state){ang, false, false}); + } + } + sort(ALL(in)); + if (solve(in, c, p)) + { + puts("YES"); + } + else + { + puts("NO"); + } + } + return 0; +} diff --git a/11705.cpp b/11705.cpp new file mode 100644 index 0000000..cb36b59 --- /dev/null +++ b/11705.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define IN getc(stdin) +#define FOR(a, b) for (int a = 0; a < b; ++a) +#define MP(a, b) make_pair(a, b) + +const char dir[4] = {'N', 'E', 'S', 'W'}; +const int dx[4] = {-1, 0, 1, 0}; +const int dy[4] = {0, 1, 0, -1}; +const int NN = 2500; + +int cn[NN], g[NN], ln[NN], px[NN], py[NN], q[NN], v[50][50]; +char a[50][51]; +vector> adj[NN]; +vector>::iterator it; +void fint(register int *k) +{ + register char c = IN; + *k = 0; + while (!isdigit(c)) + { + c = IN; + } + while (isdigit(c)) + { + *k = *k * 10 + c - '0', c = IN; + } +} +int main() +{ + int C, L, h, k, t, z; + while (scanf("%d %d", &L, &C) && L) + { + k = 0; + FOR(x, L) + { + a[x][C] = 0; + FOR(y, C) + { + adj[k].clear(); + ln[k] = x; + cn[k] = y; + g[k] = 0; + v[x][y] = k++; + a[x][y] = 'X'; + } + } + a[0][0] = '*'; + k = 0; + FOR(x, L) + FOR(y, C) + { + fint(&z); + if (z) + FOR(d, 4) + { + char nx = x + dx[d] * z, ny = y + dy[d] * z; + if (nx < 0 || ny < 0 || nx >= L || ny >= C) + { + continue; + } + int H = v[nx][ny]; + adj[H].push_back(MP(dir[d], k)); + } + k++; + } + h = t = 0; + q[t++] = 0; + g[0] = 1; + while (h != t) + { + z = q[h++]; + int ng = g[z] + 1; + int ox = ln[z], oy = cn[z]; + for (it = adj[z].begin(); it != adj[z].end(); it++) + { + int f = (*it).first, s = (*it).second; + int x = ln[s], y = cn[s], ov = a[x][y]; + if (g[s]) + { + if (g[s] == ng && (ox < px[s] || (ox == px[s] && oy < py[s]))) + { + px[s] = ox; + py[s] = oy; + a[x][y] = f; + } + } + else + { + g[s] = ng; + px[s] = ox; + py[s] = oy; + a[x][y] = f; + q[t++] = s; + } + } + } + FOR(i, L) + puts(a[i]); + putc(10, stdout); + } + return 0; +} diff --git a/11706.cpp b/11706.cpp new file mode 100644 index 0000000..aebec83 --- /dev/null +++ b/11706.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +const int NODE = 100 + 1, EMPTY = 0, W = 1, B = 2; + +bool adj[NODE][NODE]; +int color[NODE][NODE]; + +inline int rev(int c) +{ + return c == W ? B : W; +} + +bool rec(int src, int dst, int size, int c) +{ + color[src][dst] = color[dst][src] = c; + for (int nxt = 0; nxt < size; ++nxt) + { + if (nxt == src || nxt == dst) + { + continue; + } + if (adj[src][dst] + adj[dst][nxt] + adj[nxt][src] == 3) + { + if (color[dst][nxt] == rev(c)) + { + return false; + } + if (color[dst][nxt] == EMPTY && !rec(dst, nxt, size, c)) + { + return false; + } + } + if (adj[src][dst] + adj[dst][nxt] + adj[nxt][src] == 2) + { + int tmp = adj[dst][nxt] ? dst : src; + if (color[tmp][nxt] == c) + { + return false; + } + if (color[tmp][nxt] == EMPTY && !rec(tmp, nxt, size, rev(c))) + { + return false; + } + } + } + return true; +} + +bool coloring(int size) +{ + fill(&color[0][0], &color[size - 1][size], EMPTY); + for (int i = 0; i < size; ++i) + { + for (int j = i + 1; j < size; ++j) + { + if (adj[i][j] && color[i][j] == EMPTY && !rec(i, j, size, W)) + { + return false; + } + } + } + return true; +} + +int main() +{ + int node, edge; + while (scanf("%d%d", &node,&edge) == 2) + { + if (node + edge == 0) + { + break; + } + fill(&adj[0][0], &adj[node - 1][node], false); + int src, dst, cnt = 0; + for (int i = 0; i < edge; ++i) + { + scanf("%d%d", &src,&dst); + adj[src][dst] = adj[dst][src] = true; + } + printf(coloring(node) ? "YES\n" : "NO\n"); + } + return 0; +} diff --git a/11708.cpp b/11708.cpp new file mode 100644 index 0000000..4f48fe5 --- /dev/null +++ b/11708.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +long long f[20]; +char a[32], b[32], o[32]; +bool used[32]; +int main() +{ + long long k; + int pos, r; + char *p; + f[0] = 1; + for (int i = 1; i < 20; ++i) + { + f[i] = i * f[i - 1] + 1; + } + scanf("%s", a); + while (true) + { + pos = k = 0; + for (p = a; *p; ++p, ++pos) + ; + strcpy(o, a); + sort(a, a + pos--); + memset(used, 0, 32); + for (p = o, r = pos; *p && r > -1; ++p) + { + int c = 0, i; + for (i = 0; a[i] != *p; ++i) + if (!used[i]) + { + c++; + } + used[i] = 1; + k += f[r--] * c + 1; + } + printf("%lld\n", k); + while (true) + { + bool ok = scanf("%s", b) == 1; + if (!ok) + { + return 0; + } + if (b[0] > '9') + { + break; + } + for (p = b, k = 0; *p; ++p) + { + k = k * 10 + (*p - '0'); + } + memset(used, 0, 32); + r = pos; + while (k) + { + int idx = (k - 1) / f[r], c = 0, i; + k = (k - 1) % f[r--]; + for (i = 0; c != idx; c += !used[i++]) + ; + while (used[i]) + { + i++; + } + used[i] = 1; + putc(a[i], stdout); + } + putc(10, stdout); + } + strcpy(a, b); + } + return 0; +} diff --git a/11709.cpp b/11709.cpp new file mode 100644 index 0000000..c1143dc --- /dev/null +++ b/11709.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define N 1010 + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +vector edges[N], rEdges[N], sortedNode; +bool vis[N]; + +void reset() +{ + For(i, 0, N) + { + edges[i].clear(); + rEdges[i].clear(); + } + sortedNode.clear(); + Set(vis, false); +} + +void dfs1(int x) +{ + vis[x] = true; + Fors(u, edges[x]) if (!vis[edges[x][u]]) + { + dfs1(edges[x][u]); + } + sortedNode.push_back(x); +} + +void dfs2(int x) +{ + vis[x] = false; + Fors(u, rEdges[x]) if (vis[rEdges[x][u]]) + { + dfs2(rEdges[x][u]); + } +} + +int main(int argc, char **argv) +{ + int p, t; + while (scanf("%d%d", &p, &t)) + { + if (p == 0 && t == 0) + { + break; + } + getchar(); + reset(); + char name[100], name1[100], name2[100]; + map name_index; + For(i, 1, p + 1) + { + gets(name); + name_index[name] = i; + } + For(i, 0, t) + { + gets(name1); + gets(name2); + edges[name_index[name1]].push_back(name_index[name2]); + rEdges[name_index[name2]].push_back(name_index[name1]); + } + For(i, 1, p + 1) if (!vis[i]) + { + dfs1(i); + } + int c = 0; + for (int i = sortedNode.size() - 1; i >= 0; i--) + { + if (vis[sortedNode[i]]) + { + dfs2(sortedNode[i]); + c++; + } + } + printf("%d\n", c); + } + return 0; +} diff --git a/1171.cpp b/1171.cpp new file mode 100644 index 0000000..35fa3aa --- /dev/null +++ b/1171.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define X first +#define Y second +#define SZ size() + +typedef pair, int> P;// par de nodos + coste + +int bellmanford(int N, const vector

&v, int a, int b) +{ + vector d(N, 1000000000); + d[a] = 0; + + for (int i = 1; i < N; i++) + for (int j = 0; j < (int)v.SZ; j++) + if (d[v[j].X.X] < 1000000000 && d[v[j].X.X] + v[j].Y < d[v[j].X.Y]) + d[v[j].X.Y] = d[v[j].X.X] + v[j].Y; + for (int j = 0; j < (int)v.SZ; j++) + if (d[v[j].X.X] < 1000000000 && d[v[j].X.X] + v[j].Y < d[v[j].X.Y]) + return -1000000000;// existe ciclo negativo + + return d[b]; +} + +int main() +{ + int T = 0; + cin >> T; + + int N;// num squares + int M;// possible moves + int I;// origin + int F;// destination + + while (T--) + { + cin >> N >> I >> F >> M; + + int s1, s2, sc; + + vector

v; + + for (int i = 0; i < M; ++i) + { + cin >> s1 >> s2 >> sc; + v.push_back(make_pair(make_pair(s1, s2), sc)); + } + + int res = bellmanford(N, v, I, F); + + if (res == 1000000000 || res == -1000000000) + { + cout << "infinity\n"; + } + else + { + cout << res << "\n"; + } + + if (T >= 1) + { + cout << "\n"; + } + } + + return 0; +} diff --git a/11710.cpp b/11710.cpp new file mode 100644 index 0000000..0c7b0f1 --- /dev/null +++ b/11710.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +typedef pair ii; +typedef vector vi; + +vi pset(200005); + +void initSet(int N) +{ + pset.assign(N, 0); + for (int i = 0; i < N; i++) + { + pset[i] = i; + } +} +int findSet(int i) +{ + return (pset[i] == i) ? i : (pset[i] = findSet(pset[i])); +} +bool isSameSet(int i, int j) +{ + return findSet(i) == findSet(j); +} +void unionSet(int i, int j) +{ + pset[findSet(i)] = findSet(j); +} + +int main() +{ + int V, E, u, v, w, Cost; + vector> EdgeList; + map stations; + char st[10], end[10]; + scanf("%d %d", &V, &E); + while (V != 0 || E != 0) + { + Cost = 0; + stations.clear(); + EdgeList.clear(); + for (int i = 0; i < V; i++) + { + scanf("%s", st); + stations[st] = i; + } + for (int i = 0; i < E; i++) + { + scanf("%s %s %d", st, end, &w); + u = stations[st]; + v = stations[end]; + EdgeList.push_back(make_pair(w, ii(u, v))); + } + scanf("%s", st); + sort(EdgeList.begin(), EdgeList.end()); + initSet(V); + int j = 0; + for (int i = 0; i < E; i++) + { + pair front = EdgeList[i]; + if (!isSameSet(front.second.first, front.second.second)) + { + Cost += front.first; + unionSet(front.second.first, front.second.second); + j++; + if (j == V - 1) + { + break; + } + } + } + if (j == V - 1) + { + printf("%d\n", Cost); + } + else + { + printf("Impossible\n"); + } + scanf("%d %d", &V, &E); + } + return 0; +} diff --git a/11711.cpp b/11711.cpp new file mode 100644 index 0000000..a09420c --- /dev/null +++ b/11711.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +const int N = 1024; + +struct state +{ + int x; + int t[2][3]; +} s[N]; +char t[N], x[N]; +int ms[N], sc; +int get(int i) +{ + if (ms[i] == -1) + { + s[sc].t[0][0] = s[sc].t[1][0] = -1; + s[sc].x = i; + ms[i] = sc++; + } + return ms[i]; +} +int main() +{ + int a, b, cn, cp, n, m, qn, qp; + char mov; + for (int i = 0; i < N; ++i) + { + ms[i] = -1; + } + while (scanf("%d %d", &n, &m) && n) + { + sc = 0; + for (int i = 0; i < n; ++i) + { + scanf("%d %d %d %d %c", &qp, &cp, &qn, &cn, &mov); + a = get(qp), b = get(qn); + s[a].t[cp][0] = b; + s[a].t[cp][1] = cn; + s[a].t[cp][2] = ((mov == 'R') << 1) - 1; + } + for (int i = 0; i < m; ++i) + { + scanf("%d %d", &a, &b); + memset(t, 0, N); + memset(x, 0, N); + for (int j = 0; j < a || j < b; ++j) + { + t[j] = j < a; + x[j] = j < b; + } + int it, p, st = ms[0]; + for (it = p = 0; it < 10000; ++it) + { + //printf("At state [%d] (%d, %d) ->", p, s[st].x, t[p]); + int *tr = s[st].t[t[p]]; + st = tr[0]; + if (st == -1) + { + break; + } + t[p] = tr[1]; + p += tr[2]; + //for(int j = 0; j < 10; ++j) putc(t[j] + '0', stdout); putc(10, stdout); + if (p < 0 || p >= 1000) + { + break; + } + //printf("(%d, %d) %c\n", s[st].x, tr[1], tr[2] == 1 ? 'R' : 'L'); + } + //putc(10, stdout); + if (p < 0 || p >= 1000) + { + puts("MLE"); + } + else if (it == 10000) + { + puts("TLE"); + } + else + { + //for(int j = 0; t[j]; ++j) putc(t[j] + '0', stdout); putc(10, stdout); + //for(int j = 0; x[j]; ++j) putc(x[j] + '0', stdout); putc(10, stdout); + if (strcmp(t, x)) + { + puts("WA"); + } + else + { + puts("AC"); + } + } + } + for (state *st = s; st < s + sc; ++st) + { + ms[st->x] = -1; + } + } + return 0; +} diff --git a/11712.cpp b/11712.cpp new file mode 100644 index 0000000..017553f --- /dev/null +++ b/11712.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const char *m = "0 0 0 0 L\n0 1 1 0 R\n1 0 0 0 L\n1 1 2 1 R\n\ +2 0 6 0 R\n2 1 3 1 R\n3 1 3 1 R\n3 0 4 1 L\n\ +4 1 4 1 L\n4 0 5 1 R\n6 0 2 0 L"; +int main() +{ + char v[16]; + for (int n; scanf("%d", &n) && n;) + { + printf("11 %d\n%s\n", n, m); + for (int i = 0; i < n; ++i) + { + scanf("%s", v); + if (*v == 'A') + { + puts("3 4"); + } + else if (*v == 'M') + { + puts("1 1"); + } + else if (*v == 'T') + { + puts("2 2"); + } + else if (*v == 'W') + { + puts("3 5"); + } + } + } + return 0; +} diff --git a/11713.cpp b/11713.cpp index 1a2da87..dc2f1ac 100644 --- a/11713.cpp +++ b/11713.cpp @@ -1,33 +1,49 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i>str1>>str2; - int len1=str1.length(); - int len2=str2.length(); - if(len1!=len2){ - cout<<"No"< + +using namespace std; + +bool isvowel(char c) +{ + return c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'; +} + +bool same(const string &str1, const string &str2) +{ + if (str1.size() != str2.size()) + { + return false; + } + for (int i = 0, sz = str1.size(); i < sz; i++) + { + if (str1[i] != str2[i]) + { + if (!isvowel(str1[i]) || !isvowel(str2[i])) + { + return false; + } + } + } + return true; +} + +int main() +{ + int n; + string str1, str2; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, str1); + getline(cin, str2); + if (same(str1, str2)) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + return 0; +} diff --git a/11714.cpp b/11714.cpp index 37b2965..201bb35 100644 --- a/11714.cpp +++ b/11714.cpp @@ -1,12 +1,14 @@ -#include -#include -using namespace std; - -int main(){ - double n; - while(cin>>n){ - long long v=(n-1)+(int)log2(n-1); - cout< + +using namespace std; + +int main() +{ + unsigned long n; + while (cin >> n) + { + n--; + cout << n + (int)log2(n) << endl; + } + return 0; +} diff --git a/11715.cpp b/11715.cpp index ffd6da6..8b15c24 100644 --- a/11715.cpp +++ b/11715.cpp @@ -1,60 +1,47 @@ -#include -#include - -using namespace std; - -int main(){ - long long i=0; - for(i=1;;i++){ - int n=0; - double u=0,v=0,t=0,a=0,s=0; - cin>>n; - if(n==0) - break; - - switch(n){ - - case 1:{ - cin>>u>>v>>t; - a=(v-u)/t; - s=(u*t)+(a*t*t)/2; - cout<<"Case "<>u>>v>>a; - s=(v*v-u*u)/(2*a); - t=(v-u)/a; - cout<<"Case "<>u>>a>>s; - v=sqrt(u*u+2*a*s); - t=(v-u)/a; - cout<<"Case "<>v>>a>>s; - u=sqrt(v*v-2*a*s); - t=(v-u)/a; - cout<<"Case "< + +using namespace std; + +int main() +{ + int scenario; + int counter = 0; + double u, v, t, a, s; + while (cin >> scenario) + { + if (scenario == 0) + { + break; + } + counter++; + cout << "Case " << counter << ": "; + switch (scenario) + { + case 1: + cin >> u >> v >> t; + a = (v - u) / t; + s = u * t + 0.5 * a * t * t; + cout << setprecision(3) << fixed << showpoint << s << " " << a << endl; + break; + case 2: + cin >> u >> v >> a; + t = (v - u) / a; + s = u * t + 0.5 * a * t * t; + cout << setprecision(3) << fixed << showpoint << s << " " << t << endl; + break; + case 3: + cin >> u >> a >> s; + t = (-u + sqrt(u * u + 2 * a * s)) / a; + v = a * t + u; + cout << setprecision(3) << fixed << showpoint << v << " " << t << endl; + break; + case 4: + cin >> v >> a >> s; + t = (-v + sqrt(v * v - 2 * a * s)) / -a; + u = v - a * t; + cout << setprecision(3) << fixed << showpoint << u << " " << t << endl; + break; + } + } + return 0; +} diff --git a/11716.cpp b/11716.cpp index 6b8803f..5d7e734 100644 --- a/11716.cpp +++ b/11716.cpp @@ -1,44 +1,31 @@ -#include -#include -#include -using namespace std; - -int main(){ - int t=0; - scanf("%d\n",&t); - for(int i=1;i<=t;i++){ - string str="\0"; - getline(cin,str); - int len=str.length(); - if(len==0){ - cout< + +using namespace std; + +int main() +{ + int cases; + int sq; + string input; + cin >> cases; + cin.ignore(100, '\n'); + while (cases--) + { + getline(cin, input); + sq = sqrt(input.size()); + if (sq * sq != input.size()) + { + cout << "INVALID" << endl; + continue; + } + for (int i = 0; i < sq; i++) + { + for (int j = 0; i + j * sq < input.size(); j++) + { + cout << input[i + j * sq]; + } + } + cout << endl; + } + return 0; +} diff --git a/11717.cpp b/11717.cpp new file mode 100644 index 0000000..5f7afc8 --- /dev/null +++ b/11717.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int t, n, i, k, ignored, inactive, active, case_num = 0, timems; + cin >> t; + while (t--) + { + case_num++; + ignored = inactive = active = 0; + cin >> n >> i >> k; + while (n--) + { + cin >> timems; + if (timems < active) + { + ignored++; + } + else if (timems >= active + i) + { + inactive++; + active = timems + k; + } + if (timems > active) + { + active = timems; + } + } + cout << "Case " << case_num << ": " << inactive << " " << ignored << endl; + } + return 0; +} diff --git a/11718.cpp b/11718.cpp index e290ccd..ae6e5a5 100644 --- a/11718.cpp +++ b/11718.cpp @@ -1,77 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int64 power(int64 B, int64 N, int64 MOD) { - if (N == 0) return 1 % MOD; - if (N == 1) return B % MOD; - - int64 V = power(B, N/2, MOD); - V = (V %MOD * V %MOD) %MOD; - - if (N & 1) - V = (V %MOD * B %MOD) %MOD; - - return V; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t, i; - cin >> T; - FOI(t, 1, T) { - int64 N, K, MOD; - int64 sum = 0; - - cin >> N >> K >> MOD; - FOI(i, 1, N) { - int I; - cin >> I; - sum = (sum + I) % MOD; - } - - int64 ansA = sum % MOD; - int64 ansB = power(N, K - 1, MOD) % MOD; - int64 ansC = K % MOD; - int64 ans = (ansA * ansB * ansC) % MOD; - - cout << "Case " << t << ": " << ans << endl; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +inline int sq(int x) +{ + return x * x; +} +int bigmod(int n, int p, int m) +{ + if (!p) + { + return 1; + } + if (p & 1) + { + return ((n % m) * (bigmod(n, p - 1, m) % m)) % m; + } + return sq(bigmod(n, p >> 1, m) % m) % m; +} +int main() +{ + int ans, k, mod, n, nek1, p, sum, t, x; + scanf("%d", &t); + for (int cnum = 0; cnum < t;) + { + sum = 0; + scanf("%d %d %d", &n, &k, &mod); + p = n; + while (p--) + { + scanf("%d", &x); + sum = (sum + (x % mod)) % mod; + } + ans = ((bigmod(n, k - 1, mod) * sum) % mod) * (k % mod); + printf("Case %d: %d\n", ++cnum, ans % mod); + } + return 0; +} diff --git a/11719.cpp b/11719.cpp new file mode 100644 index 0000000..5291172 --- /dev/null +++ b/11719.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define mod 10000000000000007LL +long long int times(long long int a, long long int b) +{ + long long int rt = 0LL; + int i; + for (i = 63; i >= 0; i--) + { + if (a & ((1LL) << i)) + { + rt = (2 * rt + b) % mod; + } + else + { + rt = (2 * rt) % mod; + } + } + return rt; +} +int main() +{ + long long int r, c, a, b, ans, p; + int t; + scanf("%d", &t); + while (t--) + { + scanf("%lld%lld", &r, &c); + a = (r * c) / 2; + b = (r * c + 1) / 2; + ans = 1LL; + for (p = b - 1LL, c = a; p; p /= 2) + { + if (p & 1) + { + ans = times(ans, c); + } + c = times(c, c); + } + for (p = a - 1LL, c = b; p; p /= 2) + { + if (p & 1) + { + ans = times(ans, c); + } + c = times(c, c); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/1172.cpp b/1172.cpp new file mode 100644 index 0000000..bc5e8ae --- /dev/null +++ b/1172.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define SZ 1002 + +map Map; +char col[SZ][SZ]; +int dp[SZ][SZ]; +int cnt[SZ][SZ]; +int value1[SZ], value2[SZ]; +int mark1[SZ], mark2[SZ]; +int N, M; + +void check(int a, int b, int c, int d, int e, int f, int g) +{ + if ((dp[a][b] + f + g > dp[c][d]) || (dp[a][b] + f + g == dp[c][d] && cnt[a][b] + e < cnt[c][d])) + { + dp[c][d] = dp[a][b] + f + g; + cnt[c][d] = cnt[a][b] + e; + } +} + +void solve(int indx1, int indx2) +{ + if (indx1 == N || indx2 == M) + { + return; + } + if (col[indx1][indx2]) + { + return; + } + col[indx1][indx2] = 1; + solve(indx1 + 1, indx2); + check(indx1 + 1, indx2, indx1, indx2, 0, 0, 0); + solve(indx1, indx2 + 1); + check(indx1, indx2 + 1, indx1, indx2, 0, 0, 0); + if (mark1[indx1] == mark2[indx2]) + { + solve(indx1 + 1, indx2 + 1); + check(indx1 + 1, indx2 + 1, indx1, indx2, 1, value1[indx1], value2[indx2]); + } + return; +} + +int main() +{ + char arr[20]; + int i, j, t; + scanf("%d", &t); + while (t--) + { + Map.clear(); + scanf("%d", &N); + j = 0; + for (i = 0; i < N; i++) + { + scanf("%s %s %d", arr, arr, &value1[i]); + if (Map.find(arr) == Map.end()) + { + Map[arr] = j++; + } + mark1[i] = Map[arr]; + } + scanf("%d", &M); + for (i = 0; i < M; i++) + { + scanf("%s %s %d", arr, arr, &value2[i]); + if (Map.find(arr) == Map.end()) + { + Map[arr] = j++; + } + mark2[i] = Map[arr]; + } + for (i = 0; i <= N; i++) + for (j = 0; j <= M; j++) + { + col[i][j] = 0, dp[i][j] = 0, cnt[i][j] = 0; + } + solve(0, 0); + printf("%d %d\n", dp[0][0], cnt[0][0]); + } + return 0; +} diff --git a/11721.cpp b/11721.cpp new file mode 100644 index 0000000..0e7585d --- /dev/null +++ b/11721.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +const int N = 1024; + +struct edge {int to, w;} adj[N][N]; + +bool sccan[N], cs[N], onstk[N], onq[N], nothing; +int d[N], deg[N], idx[N], __idx, low[N], q[N], scc[N], stk[N], scc_count, top; + +void push(int a) +{ + stk[top++] = a; + onstk[a] = 1; +} +int pop() +{ + onstk[stk[--top]] = 0; + return stk[top]; +} +void tarjan(int k) +{ + idx[k] = low[k] = __idx = __idx + 1; + push(k); + for (int i = 0; i < deg[k]; ++i) + { + int nadj = adj[k][i].to; + if (idx[nadj] == -1) + { + tarjan(nadj); + low[k] = min(low[k], low[nadj]); + } + else if (onstk[nadj]) + { + low[k] = min(low[k], idx[nadj]); + } + cs[k] |= cs[nadj]; + } + if (low[k] == idx[k]) + { + int sccs = 0, x = -1; + while (x != k) + { + q[sccs++] = x = pop(); + onq[x] = 1; + d[x] = 0x0FFFFFFF; + scc[x] = scc_count; + } + d[k] = 0; + for (int i = 0; i < sccs - 1; ++i) + for (int j = 0; j < sccs; ++j) + { + x = q[j]; + for (int m = 0; m < deg[x]; ++m) + { + int n = adj[x][m].to; + if (onq[n]) + { + int dist = d[x] + adj[x][m].w; + if (dist < d[n]) + { + d[n] = dist; + } + } + } + } + bool neg_cycle = 0; + for (int j = 0; !neg_cycle && j < sccs; ++j) + { + x = q[j]; + for (int m = 0; !neg_cycle && m < deg[x]; ++m) + { + int n = adj[x][m].to; + if (onq[n]) + { + int dist = d[x] + adj[x][m].w; + neg_cycle |= (dist < d[n]); + } + } + } + for (int i = 0; i < sccs; ++i) + { + onq[q[i]] = 0; + cs[q[i]] |= neg_cycle; + sccan[scc_count] |= cs[q[i]]; + } + scc_count++; + } +} +void dfs(int k) +{ + idx[k] = 0; + for (int i = 0; i < deg[k]; ++i) + { + int x = adj[k][i].to; + if (idx[x] == -1) + { + dfs(x); + } + cs[k] |= cs[x]; + sccan[scc[k]] |= cs[k]; + } +} +#define MAX 1048576 +char buf[MAX], *lim = buf + MAX, *now = lim; +void readint(int *n) +{ + bool sign = 0; + while (now != lim && !isdigit(*now)) + { + now++; + } + if (now == lim) + { + int r = fread(buf, 1, MAX - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + now = buf; + } + while (!isdigit(*now)) + { + now++; + } + *n = 0; + if (now > buf && *(now - 1) == '-') + { + sign = 1; + } + while (isdigit(*now)) + { + *n = *n * 10 + *now++ - '0'; + } + if (sign) + { + *n *= -1; + } +} + +int main() +{ + int a, b, c, m, n, t; + readint(&t); + for (int cnum = 0; cnum++ < t;) + { + readint(&n); + readint(&m); + for (int i = 0; i < n; ++i) + { + sccan[i] = cs[i] = deg[i] = 0, scc[i] = idx[i] = -1; + } + for (int i = 0; i < m; ++i) + { + readint(&a); + readint(&b); + readint(&c); + adj[a][deg[a]].to = b; + adj[a][deg[a]].w = c; + deg[a]++; + } + printf("Case %d:", cnum); + nothing = 1; + scc_count = __idx = 0; + for (int i = 0; i < n; ++i) + { + top = 0; + if (deg[i] && idx[i] == -1) + { + tarjan(i); + } + } + for (int i = 0; i < n; ++i) + { + idx[i] = -1; + } + for (int i = 0; i < n; ++i) + { + if (idx[i] == -1) + { + dfs(i); + } + if (cs[i] | (scc[i] != -1 && sccan[scc[i]])) + { + nothing = 0, printf(" %d", i); + } + } + if (nothing) + { + printf(" impossible\n"); + } + else + { + printf("\n"); + } + } + return 0; +} diff --git a/11722.cpp b/11722.cpp new file mode 100644 index 0000000..f174fc6 --- /dev/null +++ b/11722.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +struct interval +{ + int a, b; +} v[2]; + +static int w; + +int main() +{ + int A1, A2, A3, A4, t; + int a1, a2, a3, b1, b2, b3; + scanf("%d", &t); + double ans, p, x, y; + for (int cnum = 0; cnum++ < t;) + { + scanf("%d %d %d %d %d", &v[0].a, &v[0].b, &v[1].a, &v[1].b, &w); + A1 = A2 = A3 = A4 = 0; + a1 = max(v[0].a, v[1].a - w); + b1 = min(v[0].b, v[1].a + w); + if (a1 < b1) + { + A1 = ((a1 - v[1].a + w) + (b1 - v[1].a + w)) * (b1 - a1); + } + a2 = max(v[0].a, v[1].a + w); + b2 = min(v[0].b, v[1].b - w); + if (a2 < b2) + { + A2 = (b2 - a2) * (w << 1); + } + a3 = max(v[0].a, v[1].b - w); + b3 = min(v[0].b, v[1].b + w); + if (a3 < b3) + { + A3 = ((v[1].b + w - a3) + (v[1].b + w - b3)) * (b3 - a3); + } + if (a3 < b1) + { + A4 = (a3 - b1) * (w << 1); + } + ans = (A1 / 2. + A2 + A3 / 2. + A4); + p = ans / (double)((v[0].b - v[0].a) * (v[1].b - v[1].a)); + printf("Case #%d: %.10lf\n", cnum, p); + } + return 0; +} diff --git a/11723.cpp b/11723.cpp index a1b8b05..61e3ec3 100644 --- a/11723.cpp +++ b/11723.cpp @@ -1,23 +1,44 @@ -#include -using namespace std; - -int main(){ - for(int j=1;;j++){ - int R,N; - cin>>R>>N; - if(R==0 && N==0) - break; - int pre=-1; - for(int i=0;i<=26;i++){ - if(N*(1+i)>=R){ - pre=i; - break; - } - } - if(pre>=0) - cout<<"Case "< + +using namespace std; + +int main() +{ + int r, n; + int counter; + int result; + while (cin >> r >> n) + { + if (r == 0 && n == 0) + { + break; + } + counter++; + cout << "Case " << counter << ": "; + if (n >= r) + { + result = 0; + } + else if (n * 27 >= r) + { + result = (r - n) / n; + if ((r - n) % n > 0) + { + result += 1; + } + } + else + { + result = -1; + } + if (result == -1) + { + cout << "impossible" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/11724.cpp b/11724.cpp new file mode 100644 index 0000000..617d37c --- /dev/null +++ b/11724.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +char exp_[1024], cond[1024]; +int val[26], used[512][512]; +long long dp[512][512]; + +int spfa(vector g[]) +{ + queue Q; + int inq[26] = {}, dist[26] = {}, cnt[26] = {}, u, v; + for (int i = 0; i < 26; i++) + { + Q.push(i), dist[i] = 1; + } + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + inq[u] = 0; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (dist[v] < dist[u] + 1) + { + dist[v] = dist[u] + 1; + if (!inq[v]) + { + inq[v] = 1, Q.push(v); + if (++cnt[v] > 26) + { + return 0; + } + } + } + } + } + for (int i = 0; i < 26; i++) + { + val[i] = dist[i]; + } + return 1; +} + +long long dfs(int l, int r) +{ + if (used[l][r]) + { + return dp[l][r]; + } + if (l == r) + { + return val[exp_[l] - 'a']; + } + used[l][r] = 1; + long long &ret = dp[l][r]; + ret = 1LL << 60; + int p = 0, test = 0; + for (int i = l; i <= r; i++) + { + if (exp_[i] == '(') + { + p++; + } + if (exp_[i] == ')') + { + p--; + } + if (p == 0 && exp_[i] == '+') + { + ret = min(ret, dfs(l, i - 1) + dfs(i + 1, r)); + test++; + } + if (p == 0 && exp_[i] == '*') + { + ret = min(ret, dfs(l, i - 1) * dfs(i + 1, r)); + test++; + } + } + if (test == 0) + { + ret = min(ret, dfs(l + 1, r - 1)); + } + return ret; +} + +int main() +{ + int testcase, cases = 0; + int n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", exp_); + scanf("%d", &n); + vector g[26]; + for (int i = 0; i < n; i++) + { + scanf("%s", cond); + g[cond[2] - 'a'].push_back(cond[0] - 'a'); + } + printf("Case %d: ", ++cases); + if (!spfa(g)) + { + puts("-1"); + } + else + { + memset(used, 0, sizeof(used)); + printf("%lld\n", dfs(0, strlen(exp_) - 1)); + } + } + return 0; +} diff --git a/11725.cpp b/11725.cpp new file mode 100644 index 0000000..97f5a70 --- /dev/null +++ b/11725.cpp @@ -0,0 +1,278 @@ +#include + +using namespace std; + +#ifdef _DEBUG +#endif + +#ifdef max +#undef max +#endif + +#ifdef min +#undef min +#endif + +const double EPS = 1e-9; + +int pow(int a, int n) +{ + int r = 1; + while (n) + if (n & 1) + { + r *= a; + --n; + } + else + { + a *= a; + n >>= 1; + } + return r; +} + +const long long MOD = 1000000007; + +long long dp[7][16384]; +bool forbidden[7][7]; + +int M, N, K; + +void f(int row, int col, int mask, int new_mask, int val) +{ + if (col == N) + { + dp[row][new_mask] = (dp[row][new_mask] + val) % MOD; + return; + } + if (forbidden[row][col]) + { + f(row, col + 1, mask, new_mask, val); + return; + } + int top = (mask >> col * 2) & 0x03; + int left = (new_mask >> (col - 1) * 2) & 0x03; + for (int i = 0; i < 4; ++i) + { + new_mask |= i << col * 2; + if ((row == 0 || forbidden[row - 1][col] || top != i) && + (col == 0 || forbidden[row][col - 1] || left != i)) + { + f(row, col + 1, mask, new_mask, val); + } + new_mask ^= i << col * 2; + } +} + +void solve(istream &is, ostream &os) +{ + int T; + is >> T; + for (int t = 1; t <= T; ++t) + { + is >> M >> N >> K; + M += 1;// number of rows + N += 1;// number of columns + memset(dp, 0, sizeof(dp)); + memset(forbidden, 0, sizeof(forbidden)); + for (int k = 0, x, y; k < K; ++k) + { + is >> y >> x; + --y; + --x; + forbidden[y][x] = true; + } + f(0, 0, 0, 0, 1); + for (int m = 1; m < M; ++m) + for (int mask = 0; mask < (1 << 2 * N); ++mask) + if (dp[m - 1][mask] > 0) + { + f(m, 0, mask, 0, dp[m - 1][mask]); + } + long long ans = 0; + for (int i = 0; i < (1 << 2 * N); ++i) + { + ans = (ans + dp[M - 1][i]) % MOD; + } + os << "Case " << t << ": " << ans << endl; + } +} + +void solve_crap(istream &is, ostream &os) +{ + int T; + is >> T; + for (int t = 1; t <= T; ++t) + { + int M, N, K; + is >> M >> N >> K; + M += 1;// number of rows + N += 1;// number of columns +#if 0 + cout << "M = " << M << ", N = " << N << ", K = " << K << endl; +#endif + vector> forbidden(M, vector(N, false)); + vector forbidden_mask(M, 0); + for (int k = 0, x, y; k < K; ++k) + { + is >> y >> x; + --y; + --x; + forbidden[y][x] = true; + forbidden_mask[y] |= 0x03 << x * 2; + } +#if 0 + cout << "FORBIDDEN MASK:" << endl; + for(int i = 0; i < forbidden_mask.size(); ++i) + { + cout << dec << i << ": " << hex << forbidden_mask[i] << ", "; + } + cout << dec << endl; +#endif + int C = pow(4, N); +#if 0 + cout << "C = " << C << endl; +#endif + vector> dp(M, vector(C, 0)); + for (int m = 0; m < M; ++m) + { +#if 1 + cout << "m = " << m << endl; +#endif + vector used_i(C, false); + for (int ii = 0; ii < C; ++ii) + { +#if 1 + cout << "ii = " << ii << " "; +#endif + int i = ii & ~forbidden_mask[m]; + if (used_i[i]) + { + continue; + } + used_i[i] = true; + for (int j = 0; j < C; ++j) + { + bool good = true; + for (int n = 0; n < N && good; ++n) + { + if (n > 0 && forbidden[m][n - 1]) + { + continue; + } + if (m > 0 && forbidden[m - 1][n]) + { + continue; + } + int mask = 0x03 << (n * 2); + if (n > 0 && ((i & mask) >> 2) == (i & (mask >> 2))) + { + good = false; + } + else if (m > 0 && (i & mask) == (j & mask)) + { + good = false; + } + } + if (good) + { + if (m == 0) + { + dp[m][i] = 1; + } + else + { + dp[m][i] += dp[m - 1][j]; + } + dp[m][i] %= MOD; + } + } + } + } +#if 0 + cout << "DP:" << endl; + for(int i = 0; i < M; ++i) + { + copy(dp[i].begin(), dp[i].end(), ostream_iterator(cout, " ")); + cout << endl; + } +#endif + long long ans = 0; + for (int i = 0; i < C; ++i) + { + ans = (ans + dp[M - 1][i]) % MOD; + } + os << "Case " << t << ": " << ans << endl; + } +} + +const char *liner = "--------"; +int problem; +int problem_filter; + +#ifdef int +#undef int +#endif + +void run(int line, const char *input, const char *output) +{ + problem += 1; + if (problem_filter != -1 && problem_filter != problem) + { + return; + } + stringstream is(input); + stringstream os; + solve(is, os); + if (os.str() != output) + { + cerr << "Case #" << problem << ": FAILED (line " << line << ")" << endl; +#ifdef _DEBUG + stringstream error; + error << __FILE__ << "(" << line << "): error: test case " << problem << " FAILED" << endl; + OutputDebugStringA(error.str().c_str()); +#endif + cerr << liner << "EXPECTED" << liner << endl + << output; + cerr << liner << "-ACTUAL-" << liner << endl + << os.str() + << liner << liner << liner << endl; + } + else + { + cerr << "Case #" << problem << " OK (line " << line << ")" << endl; + } +} + +int main(int argc, char *argv[]) +{ +#ifdef TESTING_ + problem = -1; + problem_filter = -1; + run(__LINE__, + "2\n" + "1 1\n" + "1\n" + "2 1\n" + "0 0\n" + "0\n", + "Case 1: 36\n" + "Case 2: 4\n"); + run(__LINE__, + "1\n" + "6 6\n" + "0\n", + "Case 1: 780059730\n"); +#ifdef _DEBUG + getchar(); +#endif +#else + // ifstream is("INPUT.TXT"); + // ofstream os("OUTPUT.TXT"); + istream &is = cin; + ostream &os = cout; + solve(is, os); +#endif + return 0; +} diff --git a/11726.cpp b/11726.cpp new file mode 100644 index 0000000..eb01b28 --- /dev/null +++ b/11726.cpp @@ -0,0 +1,228 @@ +#include + +using namespace std; + +const int maxn = 60005; +const double eps = 1e-8; +const double pi = acos(-1); + +struct point +{ + double x, y; + int z; + point() {} + point(double _x, double _y) : x(_x), y(_y), z(-1) {} + point(double _x, double _y, int _z) : x(_x), y(_y), z(_z) {} + point operator+(const point &i) const + { + return point(x + i.x, y + i.y, z); + } + point operator-(const point &i) const + { + return point(x - i.x, y - i.y, z); + } + double operator^(const point &i) const + { + return x * i.y - y * i.x; + } + void out() + { + printf("(%.9lf , %.9lf)\n", x, y); + } +} p[maxn]; +struct circle +{ + double x, y, r; + circle() {} + circle(double _x, double _y, double _r) : x(_x), y(_y), r(_r) {} +} c[102]; +int q[maxn], m; +int tt, t, n, pn, cn; + +double sqr(double x) +{ + return x * x; +} + +bool cmp(point a, point b) +{ + return (a.x < b.x) || (a.x == b.x && a.y < b.y); +} + +void graham() +{ + int i, k; + sort(p, p + pn, cmp); + m = 0; + for (i = 1; i < pn; ++i) + if (p[i].x != p[i - 1].x || p[i].y != p[i - 1].y) + { + p[++m] = p[i]; + } + else + { + p[m] = p[i]; + } + pn = ++m; + q[0] = 0; + if (pn == 1) + { + return; + } + q[1] = 1; + if (pn == 2) + { + return; + } + k = 1; + for (i = 2; i < pn; ++i) + { + while (k && ((p[q[k]] - p[q[k - 1]]) ^ (p[i] - p[q[k - 1]])) <= 0) + { + --k; + } + q[++k] = i; + } + m = k; + for (i = pn - 1; i >= 0; --i) + { + while (k > m && ((p[q[k]] - p[q[k - 1]]) ^ (p[i] - p[q[k - 1]])) <= 0) + { + --k; + } + q[++k] = i; + } + m = k; +} + +void circleTangent(int i, int j) +{ + circle &a = c[i], &b = c[j]; + double x0 = b.x - a.x, y0 = b.y - a.y, r0 = a.r - b.r, coso, sino; + point ans0, ans1; + if (fabs(y0) < eps) + { + coso = r0 / x0; + sino = sqrt(1 - sqr(coso)); + ans0 = point(a.r * coso, a.r * sino, i); + ans1 = point(a.r * coso, -a.r * sino, i); + } + else + { + double A = sqr(x0 / y0) + 1; + double B = -2 * x0 / y0 * r0 / y0; + double C = sqr(r0 / y0) - 1; + double d = sqrt(B * B - 4 * A * C); + coso = (d - B) / 2 / A; + sino = (r0 - x0 * coso) / y0; + ans0 = point(a.r * coso, a.r * sino, i); + coso = (-d - B) / 2 / A; + sino = (r0 - x0 * coso) / y0; + ans1 = point(a.r * coso, a.r * sino, i); + } + p[pn++] = ans0 + point(a.x, a.y); + p[pn++] = ans1 + point(a.x, a.y); +} + +void pointTangent(int i, point &b) +{ + circle &a = c[i]; + double x0 = 2 * (b.x - a.x), y0 = 2 * (b.y - a.y), r0 = 2 * (sqr(a.r) - sqr(a.x) - sqr(a.y) + a.x * b.x + a.y * b.y); + point ans0, ans1; + if (fabs(y0) < eps) + { + double d = sqrt(a.r * a.r - sqr(a.x - r0 / x0)); + ans0 = point(r0 / x0, a.y - d, i); + ans1 = point(r0 / x0, a.y + d, i); + } + else + { + double A = sqr(x0 / y0) + 1; + double B = 2 * (x0 / y0 * (a.y - r0 / y0) - a.x); + double C = sqr(a.x) + sqr(a.y - r0 / y0) - sqr(a.r); + double d = sqrt(B * B - 4 * A * C); + ans0.x = (d - B) / 2 / A; + ans0.y = (r0 - x0 * ans0.x) / y0; + ans1.x = (-d - B) / 2 / A; + ans1.y = (r0 - x0 * ans1.x) / y0; + ans0.z = ans1.z = i; + } + p[pn++] = ans0; + p[pn++] = ans1; +} + +double dis(point &a, point &b) +{ + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); +} + +double arc(point &a, point &b, circle &c) +{ + point o(c.x, c.y); + double A = dis(o, a); + double B = dis(o, b); + double C = dis(a, b); + double alpha = acos((A * A + B * B - C * C) / (2 * A * B)); + if (((o - a) ^ (o - b)) > 0) + { + return alpha * c.r; + } + return (2 * pi - alpha) * c.r; +} + +int main() +{ + int i, j; + char cc[2]; + double x, y, r; + scanf("%d", &t); + for (tt = 1; tt <= t; ++tt) + { + cn = pn = 0; + scanf("%d", &n); + while (n--) + { + scanf("%s", cc); + if (cc[0] == 'c') + { + scanf("%lf%lf%lf", &x, &y, &r); + c[cn++] = circle(x, y, r); + } + else + for (scanf("%d", &m); m--;) + { + scanf("%lf%lf", &x, &y); + p[pn++] = point(x, y, -pn - 1); + } + } + n = pn; + for (i = 0; i < cn; ++i) + { + for (j = i + 1; j < cn; ++j) + { + circleTangent(i, j); + circleTangent(j, i); + } + for (j = 0; j < n; ++j) + { + pointTangent(i, p[j]); + } + } + graham(); + q[m] = q[0]; + r = 0; + for (i = 0; i < m; ++i) + { + if (p[q[i]].z == p[q[i + 1]].z) + { + r += arc(p[q[i]], p[q[i + 1]], c[p[q[i]].z]); + } + else + { + r += dis(p[q[i]], p[q[i + 1]]); + } + } + printf("Case #%d: %.6f\n", tt, r); + } + return 0; +} diff --git a/11727.cpp b/11727.cpp index 05fd7cd..32d4eab 100644 --- a/11727.cpp +++ b/11727.cpp @@ -1,23 +1,27 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=1;i<=t;i++){ - int no[3]; - for(int j=0;j<3;j++) - cin>>no[j]; - for(int j=0;j<3;j++){ - for(int k=j+1;k<3;k++){ - if(no[j]>no[k]){ - int temp=no[j]; - no[j]=no[k]; - no[k]=temp; - } - } - } - cout<<"Case "< + +using namespace std; + +bool func(int a, int b) +{ + return (a < b); +} + +int main() +{ + int testcase; + cin >> testcase; + for (int p = 0; p < testcase; p++) + { + vector V; + int a, b, c; + V.clear(); + cin >> a >> b >> c; + V.push_back(a); + V.push_back(b); + V.push_back(c); + sort(V.begin(), V.end(), func); + cout << "Case " << p + 1 << ": " << V[1] << endl; + } + return 0; +} diff --git a/11728.cpp b/11728.cpp index 6a5cbc4..1603472 100644 --- a/11728.cpp +++ b/11728.cpp @@ -1,19 +1,46 @@ -#include -using namespace std; - -int main(){ - int sum[10000]={0}; - sum[0]=1; - for(int i=2;;i++){ - int s=0; - for(int j=1;j + +using namespace std; + +int sumOfFactors(int n) +{ + int output = 1; + for (int i = 2; i < n; i++) + { + if (n % i == 0) + { + output += i; + } + if (output > 1000) + { + break; + } + } + if (n != 1) + { + output += n; + } + return output; +} + +int main() +{ + int save[1000 + 5]; + memset(save, -1, 1005 * sizeof(int)); + int sof; + int cases = 0; + for (int i = 1; i <= 1000; i++) + { + sof = sumOfFactors(i); + if (sof <= 1000) + { + save[sof] = i; + } + } + int s; + while (scanf("%d", &s) && s) + { + printf("Case %d: %d\n", ++cases, save[s]); + } + return 0; +} diff --git a/11729.cpp b/11729.cpp new file mode 100644 index 0000000..dba2a40 --- /dev/null +++ b/11729.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int main() +{ + int c = 1; + int num_soldier; + vector Brief; + vector Job; + while (cin >> num_soldier && num_soldier) + { + int B, J; + Brief.clear(); + Job.clear(); + for (int i = 0; i < num_soldier; i++) + { + cin >> B >> J; + Brief.push_back(B); + Job.push_back(J); + } + int sum_until_i = 0; + int sum_until_j = 0; + int result = 0; + for (int i = 0; i < num_soldier; i++) + { + sum_until_i += Brief.at(i); + sum_until_j = sum_until_i; + for (int j = i + 1; j < num_soldier; j++) + { + sum_until_j += Brief.at(j); + if (max(sum_until_i + Job.at(i), sum_until_j + Job.at(j)) > + max(sum_until_i - Brief.at(i) + Brief.at(j) + Job.at(j), + sum_until_j + Job.at(i))) + { + sum_until_i = sum_until_i - Brief.at(i) + Brief.at(j); + int temp_B, temp_J; + temp_B = Brief.at(j); + temp_J = Job.at(j); + Brief.at(j) = Brief.at(i); + Job.at(j) = Job.at(i); + Brief.at(i) = temp_B; + Job.at(i) = temp_J; + } + } + result = max(result, sum_until_i + Job.at(i)); + } + cout << "Case " << c << ": " << result << endl; + c++; + } + return (0); +} diff --git a/1173.cpp b/1173.cpp new file mode 100644 index 0000000..1a303ff --- /dev/null +++ b/1173.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +// MinCostMaxFlow #StanfordUniversityNotebook +// @ https://web.stanford.edu/~liszt90/acm/notebook.html +typedef vector VI; +typedef vector VVI; +typedef long long L; +typedef vector VL; +typedef vector VVL; +typedef pair PII; +typedef vector VPII; + +const L INF = numeric_limits::max() / 4; + +struct MinCostMaxFlow +{ + int N; + VVL cap, flow, cost; + VI found; + VL dist, pi, width; + VPII dad; + + MinCostMaxFlow(int N) : N(N), cap(N, VL(N)), flow(N, VL(N)), cost(N, VL(N)), + found(N), dist(N), pi(N), width(N), dad(N) {} + + void AddEdge(int from, int to, L cap, L cost) + { + this->cap[from][to] = cap; + this->cost[from][to] = cost; + } + + void Relax(int s, int k, L cap, L cost, int dir) + { + L val = dist[s] + pi[s] - pi[k] + cost; + if (cap && val < dist[k]) + { + dist[k] = val; + dad[k] = make_pair(s, dir); + width[k] = min(cap, width[s]); + } + } + + L Dijkstra(int s, int t) + { + fill(found.begin(), found.end(), false); + fill(dist.begin(), dist.end(), INF); + fill(width.begin(), width.end(), 0); + dist[s] = 0; + width[s] = INF; + + while (s != -1) + { + int best = -1; + found[s] = true; + for (int k = 0; k < N; k++) + { + if (found[k]) + continue; + Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1); + Relax(s, k, flow[k][s], -cost[k][s], -1); + if (best == -1 || dist[k] < dist[best]) + best = k; + } + s = best; + } + + for (int k = 0; k < N; k++) + pi[k] = min(pi[k] + dist[k], INF); + return width[t]; + } + + pair GetMaxFlow(int s, int t) + { + L totflow = 0, totcost = 0; + while (L amt = Dijkstra(s, t)) + { + totflow += amt; + for (int x = t; x != s; x = dad[x].first) + { + if (dad[x].second == 1) + { + flow[dad[x].first][x] += amt; + totcost += amt * cost[dad[x].first][x]; + } + else + { + flow[x][dad[x].first] -= amt; + totcost -= amt * cost[x][dad[x].first]; + } + } + } + return make_pair(totflow, totcost); + } +}; + +int main() +{ + int T = 0; + cin >> T; + + // This problem can be interpreted as a minimum weight bipartite matching. + // We can reduce the minimum weight bipartite matching problem to the + // min cost flow problem by adding a source node 's' and a sink node 't' + // such that 's' has an edge with capacity 1 and cost 0 to each chef, and + // each facility has an edge to 't' with capacity 1 and cost 0. + // Besides the edges between chefs and facilities must have capacity 1 and + // the cost is specified by the problem statement. + // https://en.wikipedia.org/wiki/Minimum-cost_flow_problem + // https://en.wikipedia.org/wiki/File:Minimum_weight_bipartite_matching.pdf + while (T--) + { + // Chefs, facilities and edges + int C = 0, F = 0, E = 0; + cin >> C >> F >> E; + + // The total number of nodes are the number of chefs and facilities + // plus two more: the source s and the sink t + MinCostMaxFlow mcmf(C + F + 2); + + // Identifiers go from 0 to C-1 for chefs, + // and from C to C+F-1 for facilities + for (int i = 0; i < E; ++i) + { + int ci, fi, ei; + cin >> ci >> fi >> ei; + + int fip = fi + C; + + mcmf.AddEdge(ci, fip, 1, ei); + } + + // Identifiers for s (source) and t (sink) + int s = C + F; + int t = s + 1; + + // Add edges from sink to chefs + for (int i = 0; i < C; ++i) + { + mcmf.AddEdge(s, i, 1, 0); + } + + // Add edges from facilities to sink + for (int i = 0; i < F; ++i) + { + mcmf.AddEdge(i + C, t, 1, 0); + } + + cout << mcmf.GetMaxFlow(s, t).second << endl; + + if (T >= 1) + { + cout << endl; + } + } + + return 0; +} diff --git a/11730.cpp b/11730.cpp new file mode 100644 index 0000000..4cf112b --- /dev/null +++ b/11730.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +typedef unsigned long long ULL; +typedef long long LL; + +bool isnp[1001] = {}; +bool visited[1001] = {}; +int step[1001] = {}; +vector prime; + +void sieve() +{ + for (int i = 2; i < 1001; i++) + { + if (!isnp[i]) + { + for (int j = i * i; j < 1001; j += i) + { + isnp[j] = true; + } + } + prime.push_back(i); + } +} + +vector getFactor(int n) +{ + vector V; + int i = 0; + if (!isnp[n]) + { + return V; + } + while (n != 1) + { + if (n % prime[i] == 0) + { + V.push_back(prime[i]); + } + while (n % prime[i] == 0) + { + n /= prime[i]; + } + i++; + } + return V; +} + +int BFS(int start, int end) +{ + queue Q; + memset(visited, 0, sizeof(visited)); + visited[start] = true; + step[start] = 0; + Q.push(start); + while (!Q.empty()) + { + int node = Q.front(); + Q.pop(); + vector factor = getFactor(node); + if (node == end) + { + return step[node]; + } + for (int i = 0; i < factor.size(); i++) + { + int sum = node + factor[i]; + if (visited[sum] || sum > 1000) + { + continue; + } + visited[sum] = true; + step[sum] = step[node] + 1; + Q.push(sum); + } + } + return -1; +} + +int main() +{ + sieve(); + int S, T; + int cs = 0; + while (cin >> S >> T && S && T) + { + cout << "Case " << ++cs << ": " << BFS(S, T) << endl; + } + return 0; +} diff --git a/11731.cpp b/11731.cpp new file mode 100644 index 0000000..ec33b44 --- /dev/null +++ b/11731.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +#define EPS 0.0001 +#define pi acos(-1) +struct point +{ + double x, y; + point(double _x = 0, double _y = 0) + { + x = _x, y = _y; + } + void norm() + { + double div = hypot(x, y); + x /= div, y /= div; + } + point operator+(point p) + { + return point(x + p.x, y + p.y); + } + point operator-(point p) + { + return point(x - p.x, y - p.y); + } + double operator*(point p) + { + return (x * p.x + y * p.y); + } + double operator^(point p) + { + return (x * p.y - y * p.x); + } + point operator*(double k) + { + return point(x * k, y * k); + } + double ang(point k) + { + return acos(*this * k); + } + double d(point p) + { + return hypot(x - p.x, y - p.y); + } + point rot(double a) + { + double ca = cos(a), sa = sin(a); + return point(x * ca - y * sa, x * sa + y * ca); + } +}; +inline double sqr(double x) +{ + return x * x; +} +point intersect(point p0, point v0, point p1, point v1) +{ + double aux, k; + if (fabs(v0.x) > EPS) + { + aux = v0.y / v0.x; + k = (p0.y + (p1.x - p0.x) * aux - p1.y) / (v1.y - v1.x * aux); + return p1 + (v1 * k); + } + else + { + aux = v1.y / v1.x; + k = (p1.y + (p0.x - p1.x) * aux - p0.y) / (v0.y - v0.x * aux); + return p0 + (v0 * k); + } +} +double area(point A, point B, point C) +{ + double a = A.d(B), b = A.d(C), c = B.d(C); + double s = (a + b + c) / 2.; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} +double pld(point P, point O, point V) +{ + point VL = point(V.y, -V.x); + return VL * (P - O); +} +double sa(double r, double a) +{ + return a * sqr(r) / 2; +} +int main() +{ + int n1, n2, n3; + double a, b, x, y, r1, r2, r3; + for (int cnum = 0; scanf("%d %d %d", &n1, &n2, &n3) && n1;) + { + point A, AB, AC, B, BA, BC, C, CA, CB, C1, C2, C3, V0, V1; + A = point(0, 0); + B = point(n1, 0); + x = (sqr(n1) + sqr(n2) - sqr(n3)) / (n1 << 1); + y = sqrt(sqr(n2) - sqr(x)); + C = point(x, y); + AB = B - A; + AC = C - A; + BC = C - B; + BA = A - B; + CA = A - C; + CB = B - C; + AB.norm(), AC.norm(), BC.norm(); + BA.norm(), CA.norm(), CB.norm(); + a = -AB.ang(CA) / 2.; + b = BA.ang(CB) / 2.; + V0 = AB.rot(a); + V1 = BA.rot(b); + C1 = intersect(A, V0, B, V1); + r1 = pld(C1, A, AB); + a = AC.ang(BA) / 2.; + b = -CA.ang(BC) / 2.; + V0 = AC.rot(a); + V1 = CA.rot(b); + C2 = intersect(A, V0, C, V1); + r2 = pld(C2, A, AC); + a = -BC.ang(AB) / 2.; + b = CB.ang(AC) / 2.; + V0 = BC.rot(a); + V1 = CB.rot(b); + C3 = intersect(B, V0, C, V1); + r3 = pld(C3, B, BC); + point C1A = A - C1; + point C1B = B - C1; + point C2A = A - C2; + point C2C = C - C2; + point C3B = B - C3; + point C3C = C - C3; + C1A.norm(); + C1B.norm(); + C2A.norm(); + C2C.norm(); + C3B.norm(); + C3C.norm(); + double ss = sa(r1, C1A.ang(C1B)) + sa(r2, C2A.ang(C2C)) + sa(r3, C3B.ang(C3C)); + printf("Case %d: %.2f %.2f\n", ++cnum, area(C1, C2, C3), ss); + } + return 0; +} diff --git a/11732.cpp b/11732.cpp new file mode 100644 index 0000000..cc0857b --- /dev/null +++ b/11732.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define FORI(a, b, c) for (int a = b; a < c; ++a) +#define FOR(a, b) for (int a = 0; a < b; ++a) + +struct word +{ + char s[1024]; + word *n; +} words[4096]; +long long bucket(word *w, int x) +{ + long long r = 0; + int bs[128], counter = 0; + word *b[128], *l[128]; + FOR(i, 128) + b[i] = l[i] = NULL, + bs[i] = 0; + /* Distribui as palavras nos baldes */ + while (w != NULL) + { + char c = w->s[x]; + if (b[c] == NULL) + { + b[c] = l[c] = w; + } + else + { + l[c] = l[c]->n = w; + } + w = w->n; + l[c]->n = NULL; + r = r + counter++ + bs[c]++; + } + /* Redistribui pela proxima letra */ + FORI(i, '0', 128) + if (bs[i] > 1) + { + r += bucket(b[i], x + 1); + } + return r; +} +int main() +{ + int cnum = 0, n; + while (scanf("%d", &n) && n) + { + FOR(i, n) + { + scanf("%s", words[i].s); + words[i].n = NULL; + if (i < n - 1) + { + words[i].n = &words[i + 1]; + } + } + printf("Case %d: %lld\n", ++cnum, bucket(words, 0)); + } +} diff --git a/11733.cpp b/11733.cpp new file mode 100644 index 0000000..2901098 --- /dev/null +++ b/11733.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int t, n, m, a, b, c, airport; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n >> m >> airport; + pair result; + result.first = 0; // cost + result.second = 0;// number of airports + UnionFindDisjointSets ds(n + 1); + priority_queue>> edgeList; + while (m--) + { + cin >> a >> b >> c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + if ((-front.first) < airport) + { + result.first += (-front.first); + ds.unionSet(front.second.first, front.second.second); + } + } + } + result.second = (ds.numDisjointSets() - 1);// we don't have a city numbered 0, that is why -1 + result.first += airport * result.second; + cout << "Case #" << casenum + 1 << ": " << result.first << " " << result.second << endl; + } + return 0; +} diff --git a/11734.cpp b/11734.cpp index 4f884d7..34f8056 100644 --- a/11734.cpp +++ b/11734.cpp @@ -1,35 +1,48 @@ -#include -#include -#include -using namespace std; - -int main(){ - int t=0; - char ch[256]; - fgets ( ch, 256, stdin ); - t = atoi (ch); - for(int i=1;i<=t;i++){ - int j=0; - string user; - string judge; - getline(cin, user, '\n'); - getline(cin, judge, '\n'); - if(user==judge){ - cout<<"Case "< + +using namespace std; + +int main() +{ + int cases; + string a, b; + cin >> cases; + cin.ignore(100, '\n'); + for (int i = 0; i < cases; i++) + { + getline(cin, a); + getline(cin, b); + cout << "Case " << i + 1 << ": "; + if (a == b) + { + cout << "Yes"; + } + else + { + stringstream ss; + ss << a; + string tmp1 = ""; + while (ss >> a) + { + tmp1 += a; + } + ss.clear(); + ss.str(b); + string tmp2 = ""; + while (ss >> b) + { + tmp2 += b; + } + if (tmp1 == tmp2) + { + cout << "Output Format Error"; + } + else + { + cout << "Wrong Answer"; + } + } + cout << endl; + } + return 0; +} diff --git a/11735.cpp b/11735.cpp new file mode 100644 index 0000000..a1e40c1 --- /dev/null +++ b/11735.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +const int N = 1000005; + +bool used[N]; +int x[N], y[N], c, x1, x2, y1_, y2; + +int find_(int k, int *z) +{ + int u, v; + for (u = 0, v = c; u < v;) + { + int m = (u + v) >> 1; + if (z[m] > k) + { + v = m; + } + else if (z[m] < k) + { + u = m + 1; + } + else + { + return m; + } + } + return u; +} +void correct(int *k, int t, int *v, bool bigger) +{ + if (bigger) + { + while (v[*k] < t) + { + ++*k; + } + while (*k && v[*k - 1] >= t) + { + --*k; + } + } + else + { + while (v[*k] > t) + { + --*k; + } + while (*k < c && v[*k + 1] <= t) + { + ++*k; + } + } +} +double total = 0; +int p(int *v1, int *v2) +{ + int ih1, ih2, il1, il2, h, l; + ih1 = find_(x1, v1); + ih2 = find_(y1_, v2); + correct(&ih1, x1, v1, 1); + correct(&ih2, y1_, v2, 1); + il1 = find_(x2, v1); + il2 = find_(y2, v2); + correct(&il1, x2, v1, 0); + correct(&il2, y2, v2, 0); + h = max(ih1, ih2); + l = min(il1, il2); + return max(l - h + 1, 0); +} +long long gcd(long long a, long long b) +{ + if (a % b == 0) + { + return b; + } + return gcd(b, a % b); +} + +int main() +{ + int inc, xx = 0, yy = 0, t, v, v1, v2; + long long d, g, n; + x[0] = y[0] = 0; + for (c = 0; xx <= 1000005; ++c) + { + used[xx++] = 1; + inc = 1 + used[yy + 1]; + yy += inc; + xx += inc; + x[c] = xx; + y[c] = yy; + } + x[c] = 1500000; + y[c] = 1500000; + scanf("%d", &t); + for (int cnum = 0; cnum++ < t;) + { + scanf("%d %d %d %d", &x1, &y1_, &x2, &y2); + d = (long long)(y2 - y1_ + 1) * (x2 - x1 + 1); + v1 = p(x, y); + v2 = p(y, x); + v = v1 + v2; + n = d - v; + g = gcd(n, d); + n /= g; + d /= g; + printf("Board %d: %lld / %lld\n", cnum, n, d); + } + return 0; +} diff --git a/11736.cpp b/11736.cpp new file mode 100644 index 0000000..55ea273 --- /dev/null +++ b/11736.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +const int VMAX = 200; + +map m; +int b, bits[VMAX], v, y; +unsigned long long x[VMAX]; +char nome[VMAX], z[VMAX]; +int b10(char *k) +{ + int v = 0; + for (char *p = k; *p; ++p) + { + v = (v << 1) + (*p - '0'); + } + return v; +} +int main() +{ + while (scanf("%d %d", &b, &v) == 2) + { + m.clear(); + for (int i = 0; i < v; ++i) + { + scanf("%s %d", nome, &bits[i]); + m[string(nome)] = i + 1; + } + for (int i = 0; i < v; ++i) + { + x[i] = 0; + for (int j = 0; j < bits[i]; ++j) + { + scanf("%s", z); + y = b10(z); + x[i] = (x[i] << b) + y; + } + } + scanf("%d", &y); + for (int i = 0; i < y; ++i) + { + scanf("%s", nome); + int idx = m[string(nome)]; + printf("%s=", nome); + if (idx) + { + printf("%llu", x[idx - 1]); + } + putc(10, stdout); + } + } + return 0; +} diff --git a/11737.cpp b/11737.cpp new file mode 100644 index 0000000..3e2e0f4 --- /dev/null +++ b/11737.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const int N = 10000; +int h[N], w[N]; +int abs(int a) +{ + return a < 0 ? -a : a; +} +int main() +{ + for (int cnum = 1, n; scanf("%d", &n) == 1; ++cnum) + { + int a, b, d1, d2, min = 100000, no = 0; + for (int i = 0; i < n; ++i) + { + scanf("%d %d", &a, &b); + d1 = abs(a - b); + if (a == b) + { + no = 1; + } + if (d1 < min) + { + min = d1; + } + for (int j = 0; j < i; ++j) + { + d1 = abs(a - h[j]); + d2 = abs(b - w[j]); + if (d1 < min) + { + min = d1; + } + if (d2 < min) + { + min = d2; + } + } + w[i] = a; + h[i] = b; + } + if (!no) + { + min += min == 0; + } + printf("Case %d : %d\n", cnum, (min + 1) >> 1); + } + return 0; +} diff --git a/11739.cpp b/11739.cpp new file mode 100644 index 0000000..b8bcff5 --- /dev/null +++ b/11739.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +#define MAXN 2009 + +bool is_bad[150]; +char bad[1005], good[1005]; +int s[MAXN], sa[MAXN], t[MAXN], t2[MAXN], c[MAXN], rank_[MAXN], height[MAXN], n; + +void build_sa(int m) +{ + int *x = t, *y = t2; + for (int i = 0; i < m; i++) + { + c[i] = 0; + } + for (int i = 0; i < n; i++) + { + c[x[i] = s[i]]++; + } + for (int i = 1; i < m; i++) + { + c[i] += c[i - 1]; + } + for (int i = n - 1; i >= 0; i--) + { + sa[--c[x[i]]] = i; + } + for (int k = 1; k <= n; k <<= 1) + { + int p = 0; + for (int i = n - k; i < n; i++) + { + y[p++] = i; + } + for (int i = 0; i < n; 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 < n; i++) + { + c[x[y[i]]]++; + } + for (int i = 0; i < m; i++) + { + c[i] += c[i - 1]; + } + for (int i = n - 1; i >= 0; i--) + { + sa[--c[x[y[i]]]] = y[i]; + } + swap(x, y); + p = 1; + x[sa[0]] = 0; + for (int i = 1; i < n; 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 >= n) + { + break; + } + m = p; + } +} + +void get_height() +{ + int k = 0; + for (int i = 0; i < n; i++) + { + rank_[sa[i]] = i; + } + for (int i = 0; i < n; i++) + { + if (k) + { + k--; + } + int j = sa[rank_[i] - 1]; + while (s[i + k] == s[j + k]) + { + k++; + } + height[rank_[i]] = k; + } +} + +bool check(int mid) +{ + int mi = 99999, ma = -1; + for (int i = 1; i < n; i++) + { + if (height[i] >= mid) + { + mi = min(mi, sa[i]); + ma = max(ma, sa[i]); + } + else + { + if (ma - mi >= mid) + { + return 1; + } + mi = ma = sa[i]; + } + } + return ma - mi >= mid; +} + +int main() +{ + int T, t = 1, r; + scanf("%d", &T); + getchar(); + while (T--) + { + memset(is_bad, 0, sizeof is_bad); + gets(good); + gets(bad); + int l = strlen(bad); + for (int i = 0; bad[i] != '\0'; i++) + { + is_bad[bad[i]] = 1; + } + n = strlen(good); + while (n > 0 && good[n - 1] == ' ') + { + good[n - 1] = '\0'; + --n; + } + int xx = 'z'; + for (int i = 0; good[i] != '\0'; i++) + { + if (is_bad[good[i]]) + { + s[i] = xx++; + } + else + { + s[i] = good[i]; + } + } + s[n] = 0; + + printf("Case #%d: ", t++); + if (n <= 1) + { + printf("0\n"); + continue; + } + n++; + build_sa(xx + 3); + get_height(); + l = 0, r = n >> 1; + while (l <= r) + { + int mid = (l + r) >> 1; + if (check(mid)) + { + l = mid + 1; + } + else + { + r = mid - 1; + } + } + printf("%d\n", r); + } + return 0; +} diff --git a/1174.cpp b/1174.cpp new file mode 100644 index 0000000..b89327f --- /dev/null +++ b/1174.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +//1174 +//IP-TV +//Graphs;Minimum Spanning Tree;Prim;Priority Queue +#define MAX 200010 + +struct Road +{ + int v, c; + Road(int v, int c) : v(v), c(c) {} + inline bool operator<(const Road &that) const + { + return c > that.c; + } +}; + +vector G[MAX]; +priority_queue Q; +int n, m; +bool V[MAX]; +map M; + +int city(string &s) +{ + if (M.find(s) != M.end()) + { + return M[s]; + } + else + { + return M[s] = M.size(); + } +} + +int main() +{ + int t; + cin >> t; + t = 0; + while (cin >> n >> m) + { + memset(V, 0, sizeof(V)); + memset(G, 0, sizeof(G)); + M.clear(); + Q = priority_queue(); + for (int i = 0; i < m; i++) + { + string p, q; + int a, b, c; + cin >> p >> q >> c; + a = city(p); + b = city(q); + G[a].push_back(Road(b, c)); + G[b].push_back(Road(a, c)); + } + int total = 0, totalc = 0; + Q.push(Road(1, 0)); + while (totalc < n) + { + Road item = Q.top(); + Q.pop(); + if (V[item.v]) + { + continue; + } + V[item.v] = true; + total += item.c; + totalc++; + for (int j = 0; j < G[item.v].size(); j++) + if (!V[G[item.v][j].v]) + { + Q.push(G[item.v][j]); + } + } + if (t++) + { + cout << endl; + } + cout << total << endl; + } + return 0; +} diff --git a/11741.cpp b/11741.cpp new file mode 100644 index 0000000..ad787c3 --- /dev/null +++ b/11741.cpp @@ -0,0 +1,270 @@ +#include + +using namespace std; + +#define ll long long + +//mega + +#define SIZE 20 +ll mod = 10000007; + +class Matrix +{ +public: + ll N; + ll matrix[SIZE][SIZE]; + Matrix(ll n) + { + N = n; + } + ll getDim() + { + return N; + } + void setZer() + { + memset(matrix, 0, sizeof(matrix)); + } + void setOne() + { + ll i; + setZer(); + for (i = 0; i < N; ++i) + { + matrix[i][i] = 1; + } + } + void setVal(ll array[][SIZE]) + { + ll i, j; + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + matrix[i][j] = array[i][j]; + } + } + } + Matrix operator+(Matrix &A) + { + Matrix ret(N); + ll i, j; + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + ret.matrix[i][j] = (matrix[i][j] + A.matrix[i][j]) % mod; + } + } + return ret; + } + Matrix operator*(Matrix &A) + { + Matrix ret(N); + ll i, j, k; + long long temp; + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + ret.matrix[i][j] = 0; + for (k = 0; k < N; ++k) + { + temp = (long long)matrix[i][k] * A.matrix[k][j]; + ret.matrix[i][j] = (ret.matrix[i][j] + temp) % mod; + } + } + } + return ret; + } + void printMatrix() + { + ll i, j; + puts("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + printf("%3d ", matrix[i][j]); + } + puts(""); + } + puts("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + } +} base(SIZE), ret(SIZE); +Matrix bigmod(Matrix A, ll K) +{ + if (K == 1) + { + return base; + } + if (K & 1) + { + ret = bigmod(A, K - 1); + ret = ret * base; + return ret; + } + ret = bigmod(A, K >> 1); + ret = ret * ret; + return ret; +} +Matrix bigmodmat2(Matrix base, ll K) +{ + ret = base; + Matrix ans(base.N); + ans.setOne(); + while (K) + { + if (K % 2) + { + ans = ans * ret; + } + K /= 2; + ret = ret * ret; + } + return ans; +} + +// + +const ll N = 102; +ll r, n, c, x[N], y[N]; + +bool valid(vector &cx, ll mask) +{ + ll i, sz = cx.size(); + for (i = 0; i < sz; i++) + { + if (mask & (1 << cx[i])) + { + return false; + } + } + return true; +} + +ll C[20][20]; + +void g(ll now, ll pos, ll next) +{ + if (pos == r) + { + //cout<> e; +ll ccount[N], ncount[N]; + +int main() +{ + // freopen("in.txt","r",stdin); + ll i, j, k, now, next, mask; + int cs = 0; + while (cin >> r >> c >> n) + { + if (!r && !c && !n) + { + break; + } + e.clear(); + for (i = 0; i < n; i++) + { + cin >> x[i] >> y[i]; + // scanf("%d%d",&x[i],&y[i]); + e[y[i]].push_back(x[i]); + } + memset(C, 0, sizeof(C)); + for (i = 0; i < (1 << r); i++) + { + g(i, 0, 0); + // cout<>::iterator it; + for (it = e.begin(); it != e.end(); it++) + { + ll cy = it->first; + vector cx = it->second; + // cout<"<<" "; + //for(i=0;i + +using namespace std; + +struct constraint +{ + int friend1; + int friend2; + int c; +}; + +bool satisfying(int nums[8], int n, const vector &constraints) +{ + int pos[8]; + int diff; + for (int i = 0; i < n; i++) + { + pos[nums[i]] = i; + } + for (int i = 0, sz = constraints.size(); i < sz; i++) + { + diff = abs(pos[constraints[i].friend1] - pos[constraints[i].friend2]); + if (constraints[i].c < 0) + { + if (diff < abs(constraints[i].c)) + { + return false; + } + } + else + { + if (diff > abs(constraints[i].c)) + { + return false; + } + } + } + return true; +} + +int main() +{ + int n, m; + int result; + int nums[8]; + constraint tmp; + vector constraints; + while (cin >> n >> m) + { + if (!n && !m) + { + break; + } + result = 0; + constraints.clear(); + for (int i = 0; i < 8; i++) + { + nums[i] = i; + } + while (m--) + { + cin >> tmp.friend1 >> tmp.friend2 >> tmp.c; + constraints.push_back(tmp); + } + do + { + if (satisfying(nums, n, constraints)) + { + result++; + } + } while (next_permutation(nums, nums + n)); + cout << result << endl; + } + return 0; +} diff --git a/11743.cpp b/11743.cpp index eb63003..46a63ea 100644 --- a/11743.cpp +++ b/11743.cpp @@ -1,56 +1,51 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int i=0;i>a>>b>>c>>d; - int ctr=1,sum=0; - while(a>0){ - int rem=a%10; - if(ctr%2==0){ - rem*=2; - } - sum+=rem%10+rem/10; - a/=10; - ctr++; - } - ctr=1; - while(b>0){ - int rem=b%10; - if(ctr%2==0){ - rem*=2; - } - sum+=rem%10+rem/10; - b/=10; - ctr++; - } - ctr=1; - while(c>0){ - int rem=c%10; - if(ctr%2==0){ - rem*=2; - } - sum+=rem%10+rem/10; - c/=10; - ctr++; - } - ctr=1; - while(d>0){ - int rem=d%10; - if(ctr%2==0){ - rem*=2; - } - sum+=rem%10+rem/10; - d/=10; - ctr++; - } - if(sum%10==0) - cout<<"Valid"< + +using namespace std; + +int digits_sum(int n) +{ + int result = 0; + while (n) + { + result += n % 10; + n /= 10; + } + return result; +} + +int main() +{ + int n; + string input, tmp; + cin >> n; + while (n--) + { + input = ""; + for (int i = 0; i < 4; i++) + { + cin >> tmp; + input += tmp; + } + int sum = 0; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (i % 2 == 0) + { + sum += digits_sum((input[i] - '0') * 2); + } + else + { + sum += input[i] - '0'; + } + } + if (sum % 10 == 0) + { + cout << "Valid" << endl; + } + else + { + cout << "Invalid" << endl; + } + } + return 0; +} diff --git a/11744.cpp b/11744.cpp new file mode 100644 index 0000000..f82ad4d --- /dev/null +++ b/11744.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +char d[2][32]; +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%s %s", d[0], d[1]); + printf("%s %s\n", d[0], d[1]); + for (int i = 0;; ++i) + { + bool beq0 = 1, overflow = 0; + for (int j = 0; j < 31; ++j) + { + char ai = d[0][j] - '0', bi = d[1][j] - '0'; + d[0][j] = (ai != bi) + '0'; + beq0 &= ~(ai && bi); + if (j) + { + d[1][j - 1] = (ai && bi) + '0'; + } + else + { + overflow = (ai && bi); + } + } + d[1][30] = '0'; + if (overflow) + { + printf("%s overflow\n", d[0]); + break; + } + printf("%s %s\n", d[0], d[1]); + if (beq0) + { + break; + } + } + if (t) + { + putc(10, stdout); + } + } + return 0; +} diff --git a/11745.cpp b/11745.cpp new file mode 100644 index 0000000..7105b7d --- /dev/null +++ b/11745.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define IN getc(stdin) +const int N = 64; +const int M = 5000; +const char *ans[] = {"Valid", "Invalid"}; +int adj[M][8], deg[M], v[M], vis; +int h[N][N], lines, w[N][N]; +void add(int i, int j, bool v) +{ + int i1 = 60 * i + j, i2; + if (v) + { + i2 = i1 + 60; + if (j) + { + h[i][j - 1]++; + } + } + else + { + i2 = i1 + 1; + if (i) + { + h[i - 1][j]++; + } + } + adj[i1][deg[i1]++] = i2; + adj[i2][deg[i2]++] = i1; + h[i][j]++; + lines++; +} +void dfs(int x) +{ + v[x] = 1; + vis++; + for (int i = 0; i < deg[x]; ++i) + { + int k = adj[x][i]; + if (!v[k]) + { + dfs(k); + } + } +} +int main() +{ + int c, col, r, row, t; + bool fu; + char in; + scanf("%d", &t); + while (t--) + { + scanf("%d %d\n", &r, &c); + for (int i = 0; i < M; ++i) + { + v[i] = 0, deg[i] = 0; + } + for (int i = 0; i < r; ++i) + for (int j = 0; j < c; ++j) + { + w[i][j] = -1, h[i][j] = 0; + } + r = (r << 1) + 1; + c = (c << 1) + 1; + fu = lines = 0; + for (int i = 0; i < r; ++i) + { + for (int j = 0; j < c; ++j) + { + in = IN; + if (in == '-' || in == '|') + { + add(i >> 1, j >> 1, i & 1); + } + if (isdigit(in)) + { + w[i >> 1][j >> 1] = in - '0'; + } + } + IN; + } + r >>= 1; + c >>= 1; + for (int i = 0; !fu && i < r; ++i) + for (int j = 0; j < c; ++j) + { + fu |= w[i][j] != -1 && h[i][j] != w[i][j]; + } + for (int i = 0; !fu && i < M; ++i) + { + if (deg[i]) + { + vis = 0; + dfs(i); + fu = vis != lines; + break; + } + } + puts(ans[fu]); + } + return 0; +} diff --git a/11746.cpp b/11746.cpp new file mode 100644 index 0000000..8d56fb8 --- /dev/null +++ b/11746.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +#define eps 1e-6 + +double f_abs(double x) +{ + return x < 0 ? -x : x; +} +double x[3], y[3], p[3]; +void get_data() +{ + int i; + for (i = 0; i < 3; i++) + { + scanf("%lf%lf%lf", &x[i], &y[i], &p[i]); + } +} +double a[3], b[3], c[3], d[3], e[3]; +struct Res +{ + double x, y; +}; +Res res[100]; +int rn; +int cmp(const void *a, const void *b) +{ + Res *c = (Res *)a, *d = (Res *)b; + if (f_abs(c->x - d->x) < eps) + { + return c->y > d->y ? 1 : -1; + } + else + { + return c->x > d->x ? 1 : -1; + } +} +void insert(double rx, double ry) +{ + if (rx > -eps && rx <= 1000 + eps && ry > -eps && ry <= 1000 + eps) + { + res[rn].x = rx; + res[rn++].y = ry; + } +} +void solve(double ax, int w) +{ + double temp, t = c[w] * ax * ax + d[w] * ax + e[w]; + if (f_abs(a[w]) < eps) + { + if (f_abs(b[w]) > 0) + { + insert(ax, -t / b[w]); + } + } + else + { + temp = b[w] * b[w] - 4 * a[w] * t; + if (temp < -eps) + { + return; + } + temp = sqrt(temp); + if (temp < eps) + { + insert(ax, -b[w] / (2 * a[w])); + } + else + { + insert(ax, (-b[w] + temp) / (2 * a[w])); + insert(ax, (-b[w] - temp) / (2 * a[w])); + } + } +} +void solvex(int w) +{ + double temp; + if (f_abs(c[w]) < eps) + { + if (f_abs(d[w]) > eps) + { + solve(-e[w] / d[w], w ^ 1); + } + } + else + { + temp = d[w] * d[w] - 4 * c[w] * e[w]; + if (temp < -eps) + { + return; + } + temp = sqrt(temp); + if (temp < eps) + { + solve(-d[w] / (2 * c[w]), w ^ 1); + } + else + { + solve((-d[w] + temp) / (2 * c[w]), w ^ 1); + solve((-d[w] - temp) / (2 * c[w]), w ^ 1); + } + } +} +void run() +{ + a[0] = p[0] - p[1]; + c[0] = p[0] - p[1]; + b[0] = (-2 * y[0] * p[0]) + 2 * y[1] * p[1]; + d[0] = (-2 * x[0] * p[0]) + 2 * x[1] * p[1]; + e[0] = (y[0] * y[0] + x[0] * x[0]) * p[0] - (y[1] * y[1] + x[1] * x[1]) * p[1]; + a[1] = p[2] - p[1]; + c[1] = p[2] - p[1]; + b[1] = (-2 * y[2] * p[2]) + 2 * y[1] * p[1]; + d[1] = (-2 * x[2] * p[2]) + 2 * x[1] * p[1]; + e[1] = (y[2] * y[2] + x[2] * x[2]) * p[2] - (y[1] * y[1] + x[1] * x[1]) * p[1]; + // printf("%f %f %f %f %f\n",a[0],b[0],c[0],d[0],e[0]); + // printf("%f %f %f %f %f\n",a[1],b[1],c[1],d[1],e[1]); + rn = 0; + double tx; + if (f_abs(a[0]) < eps) + { + if (f_abs(b[0]) < eps) + { + if (f_abs(d[0]) > eps) + { + tx = -e[0] / d[0]; + solve(tx, 1);//x已知,求å¼1 + } + } + else + { + c[1] += d[0] * d[0] / (b[0] * b[0]) * a[1]; + d[1] += -b[1] * d[0] / b[0] + 2 * d[0] * e[0] * a[1] / (b[0] * b[0]); + e[1] += e[0] * e[0] / (b[0] * b[0]) * a[1] - b[1] * e[0] / b[0]; + a[1] = b[1] = 0; + solvex(1);//从1å¼æ±‚x的解 + } + } + else + { + double k = a[1] / a[0]; + a[1] = c[1] = 0; + b[1] -= b[0] * k; + d[1] -= d[0] * k; + e[1] -= e[0] * k; + if (f_abs(b[1]) < eps) + { + if (f_abs(d[1]) > eps) + { + tx = -e[1] / d[1]; + solve(tx, 0); + } + } + else + { + c[0] += d[1] * d[1] / (b[1] * b[1]) * a[0]; + d[0] += -b[0] * d[1] / b[1] + 2 * d[1] * e[1] * a[0] / (b[1] * b[1]); + e[0] += e[1] * e[1] / (b[1] * b[1]) * a[0] - b[0] * e[1] / b[1]; + a[0] = b[0] = 0; + solvex(0);//从0å¼æ±‚x的解 + } + } + printf("%d", rn); + qsort(res, rn, sizeof(Res), cmp); + int i; + for (i = 0; i < rn; i++) + { + printf(" %.2f %.2f", res[i].x, res[i].y); + } + printf("\n"); +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + get_data(); + run(); + } + return 0; +} diff --git a/11747.cpp b/11747.cpp new file mode 100644 index 0000000..44e067a --- /dev/null +++ b/11747.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int n, m, a, b, c; + while (cin >> n >> m) + { + if (!n && !m) + { + break; + } + vector result; + UnionFindDisjointSets ds(n); + priority_queue>> edgeList; + while (m--) + { + cin >> a >> b >> c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + ds.unionSet(front.second.first, front.second.second); + } + else + { + result.push_back(-front.first); + } + } + if (result.empty()) + { + cout << "forest" << endl; + continue; + } + sort(result.begin(), result.end()); + for (int i = 0, sz = result.size(); i < sz; i++) + { + if (i > 0) + { + cout << " "; + } + cout << result[i]; + } + cout << endl; + } + return 0; +} diff --git a/11748.cpp b/11748.cpp new file mode 100644 index 0000000..de306b1 --- /dev/null +++ b/11748.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +const int MAXN = 128; + +bool v[MAXN]; +int rank_[MAXN][MAXN]; +int m; // #voters +int n; // #candidates + +bool wins(int a, int b) // ret true if a beats b in an election +{ + int score = 0; + FOR(i, m) + score += rank_[i][a] < rank_[i][b]; + return score > m - score; +} +void dfs(int x) +{ + v[x] = 1; + FOR(i, n) + if (!v[i] && wins(x, i)) + { + dfs(i); + } +} + +int main() +{ + for (int w, z; scanf("%d%d%d", &n, &m, &w) && n;) + { + bool possible = true; + FOR(i, n) + v[i] = false; + FOR(i, m) + FOR(j, n) + { + scanf("%d", &z); + rank_[i][--z] = j; + } + dfs(--w); + FOR(i, n) + possible &= v[i]; + puts(possible ? "yes" : "no"); + } + return 0; +} diff --git a/11749.cpp b/11749.cpp new file mode 100644 index 0000000..1e49449 --- /dev/null +++ b/11749.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int n, m; + int a, b, c; + int best; + vector> v; + while (cin >> n >> m) + { + if (!n && !m) + { + break; + } + v.clear(); + UnionFindDisjointSets ds(n + 1); + best = -2147483648; + while (m--) + { + cin >> a >> b >> c; + if (c > best) + { + best = c; + v.clear(); + v.push_back(make_pair(a, b)); + } + else if (c == best) + { + v.push_back(make_pair(a, b)); + } + } + best = 0; + for (int i = 0, sz = v.size(); i < sz; i++) + { + ds.unionSet(v[i].first, v[i].second); + if (ds.sizeOfSet(v[i].first) > best) + { + best = ds.sizeOfSet(v[i].first); + } + } + cout << best << endl; + } + return 0; +} diff --git a/1175.cpp b/1175.cpp new file mode 100644 index 0000000..24ac46a --- /dev/null +++ b/1175.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +//Template updates date: 20140718 +#define esp 1e-6 +#define inf 0x3f3f3f3f +#define pi acos(-1.0) +#define pb push_back +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 +#define lowbit(x) (x & (-x)) +#define mp(a, b) make_pair((a), (b)) +#define bit(k) (1 << (k)) +#define iin freopen("pow.in", "r", stdin); +#define oout freopen("pow.out", "w", stdout); +#define in freopen("solve_in.txt", "r", stdin); +#define out freopen("solve_out.txt", "w", stdout); +#define bug puts("********))))))"); +#define Inout iin oout +#define inout in out + +#define SET(a, v) memset(a, (v), sizeof(a)) +#define SORT(a) sort((a).begin(), (a).end()) +#define REV(a) reverse((a).begin(), (a).end()) +#define READ(a, n) \ + { \ + REP(i, n) \ + cin >> (a)[i]; \ + } +#define REP(i, n) for (int i = 0; i < (n); i++) +#define VREP(i, n, base) for (int i = (n); i >= (base); i--) +#define Rep(i, base, n) for (int i = (base); i < (n); i++) +#define REPS(s, i) for (int i = 0; (s)[i]; i++) +#define pf(x) ((x) * (x)) +#define mod(n) ((n)) +#define Log(a, b) (log((double)b) / log((double)a)) +#define Srand() srand((int)time(0)) +#define random(number) (rand() % number) +#define random_range(a, b) (int)(((double)rand() / RAND_MAX) * (b - a) + a) + +typedef long long LL; +typedef unsigned long long ULL; +typedef vector VI; +typedef pair PII; +typedef vector VII; +typedef vector VIII; +typedef VI::iterator IT; +typedef map Mps; +typedef map Mpi; +typedef map Mpii; +typedef map Mpiii; + +const int maxn = 1100; +int n; +int mav[maxn][maxn], wa[maxn][maxn], ma[maxn][maxn]; +int ii, link[maxn], st[maxn], vv[maxn], r[maxn], wife[maxn]; +struct cmp +{ + bool operator()(const int &a, const int &b) + { + return vv[a] > vv[b]; + } +}; +void solve() +{ + int inq[maxn]; + queue q; + VREP(i, n, 1) + q.push(i), inq[i] = 1; + while (!q.empty()) + { + int u = q.front(); + q.pop(); + inq[u] = 0; + for (st[u]++; st[u] <= n; st[u]++) + { + int v = ma[u][st[u]]; + if (link[v] == 0 || wa[v][link[v]] > wa[v][u]) + { + if (link[v] && !inq[link[v]]) + { + q.push(link[v]), inq[link[v]] = 1; + } + link[v] = u; + wife[u] = v; + break; + } + } + } +} +int main() +{ + int T; + Rep(t, scanf("%d", &T), T + 1) + { + if (t != 1) + { + puts(""); + } + int tmp; + SET(st, 0); + SET(link, 0); + Rep(i, scanf("%d", &n), n + 1) + Rep(j, 1, n + 1) scanf("%d", &ma[i][j]); + for (int i = 1; i <= n; i++) + Rep(j, 1, n + 1) + scanf("%d", &tmp), + wa[i][tmp] = j; + solve(); + Rep(i, 1, n + 1) + printf("%d\n", wife[i]); + } + return 0; +} diff --git a/11751.cpp b/11751.cpp new file mode 100644 index 0000000..e9e9a9d --- /dev/null +++ b/11751.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +#define N 1005 +bool inst[N]; +bool g[N][N]; +int main() +{ + int n, m, i, j, x, y; + while (cin >> n >> m) + { + if ((n == 0) && (m == 0)) + { + break; + } + memset(g, 0, sizeof(g)); + for (i = 0; i <= n; i++) + { + inst[i] = 0; + } + for (i = 1; i <= m; i++) + { + scanf("%d%d", &x, &y); + g[x][y] = g[y][x] = 1; + } + for (i = n - 2; i >= 0; i--) + { + for (j = i + 1; j < n; j++) + if (g[i][j] && !inst[j]) + { + inst[i] = 1; + break; + } + } + for (i = 0; i < n; i++) + { + printf("%d", inst[i]); + } + printf("\n"); + } +} diff --git a/11752.cpp b/11752.cpp new file mode 100644 index 0000000..c629cba --- /dev/null +++ b/11752.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define FOREACH(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++) + +typedef unsigned long long ULL; + +int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61}; + +bool prime[65]; +double lim2 = 64 * log(2); +int lim = 1000; +set c; + +int main() +{ + for (int i = 0; i < 65; ++i) + { + prime[i] = 0; + } + for (int i = 0; i < 18; ++i) + { + prime[p[i]] = 1; + } + for (int i = 2; lim > 3; ++i) + { + ULL b = i, v = i; + lim = ceil(lim2 / log(i)); + for (int e = 2; e < lim; ++e) + { + v *= b; + if (!prime[e]) + { + c.insert(v); + } + } + } + printf("1\n"); + FOREACH(c,itr) + { + printf("%llu\n", *itr); + } + return 0; +} diff --git a/11753.cpp b/11753.cpp index 56a2e40..8582805 100644 --- a/11753.cpp +++ b/11753.cpp @@ -1,79 +1,61 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +#define MP make_pair -int fun(int vec[], int N){ - int i = 0, j = N - 1; - int D = 0; - while(i < j){ - if(vec[i] == vec[j]){ - ++i; - --j; +const int MAXN = 10048; +map, int> m; +int ans, max_insertions; +int v[MAXN]; +void r(int left, int right, int insertions) +{ + if (insertions > max_insertions || left >= right) + { + ans = min(ans, insertions); + return; + } + int &x = m[MP(left, right)]; + if (!x || insertions < x) + { + x = insertions; + if (v[left] == v[right]) + { + r(left + 1, right - 1, insertions); } - else{ - ++i; - ++D; + else + { + r(left, right - 1, insertions + 1); + r(left + 1, right, insertions + 1); } } - return D; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - cin >> T; - FOI(t, 1, T){ - int N, K; - cin >> N >> K; - int vec[N]; - int i, j; - FOI(i, 0, N-1) - cin >> vec[i]; - int D1 = fun(vec, N); - reverse(vec, vec + N); - int D2 = fun(vec, N); - int D = min(D1, D2); - if(D < 1) - cout << "Case " << t << ": " << "Too easy" << endl; - else if(D > K) - cout << "Case " << t << ": " << "Too difficult" << endl; +int main() +{ + int t; + scanf("%d", &t); + for (int cnum = 0, n; cnum++ < t && scanf("%d %d", &n, &max_insertions) == 2;) + { + for (int i = 0; i < n; ++i) + { + scanf("%d", v + i); + } + m.clear(); + ans = max_insertions + 1; + r(0, n - 1, 0); + printf("Case %d: ", cnum); + if (ans == 0) + { + puts("Too easy"); + } + else if (ans <= max_insertions) + { + printf("%d\n", ans); + } else - cout << "Case " << t << ": " << D << endl; + { + puts("Too difficult"); + } } return 0; } - diff --git a/11754.cpp b/11754.cpp new file mode 100644 index 0000000..393a611 --- /dev/null +++ b/11754.cpp @@ -0,0 +1,330 @@ +#include + +using namespace std; + +using std::priority_queue; +using std::vector; +using std::swap; +using std::stack; +using std::sort; +using std::max; +using std::min; +using std::pair; +using std::map; +using std::string; +using std::cin; +using std::cout; +using std::set; +using std::queue; +using std::string; +using std::istringstream; +using std::make_pair; +using std::greater; +using std::endl; + +typedef long long LL; + +const int MAXN(110); + +template +struct SBT +{ + struct NODE + { + T key; + int size; + NODE *left, *right; + }; + + NODE pool[MAXN]; + NODE *root, *NIL, *rear; + T tkey; + + void init() + { + NIL = pool; + NIL->left = NIL->right = NIL; + NIL->size = 0; + rear = pool + 1; + root = NIL; + } + + void newnode(NODE *&sour, const T &value) + { + rear->key = value; + rear->size = 1; + rear->left = rear->right = NIL; + sour = rear++; + } + + void left_rotate(NODE *&sour) + { + NODE *temp = sour->right; + sour->right = temp->left; + temp->left = sour; + temp->size = sour->size; + sour->size = sour->left->size + sour->right->size + 1; + sour = temp; + } + + void right_rotate(NODE *&sour) + { + NODE *temp = sour->left; + sour->left = temp->right; + temp->right = sour; + temp->size = sour->size; + sour->size = sour->left->size + sour->right->size + 1; + sour = temp; + } + + void maintain(NODE *&sour, bool flag) + { + if (flag == false) + { + if (sour->left->left->size > sour->right->size) + { + right_rotate(sour); + } + else if (sour->left->right->size > sour->right->size) + { + left_rotate(sour->left); + right_rotate(sour); + } + else + { + return; + } + } + else + { + if (sour->right->right->size > sour->left->size) + { + left_rotate(sour); + } + else if (sour->right->left->size > sour->left->size) + { + right_rotate(sour->right); + left_rotate(sour); + } + else + { + return; + } + } + maintain(sour->left, false); + maintain(sour->right, true); + } + + void insert(const T &value) + { + tkey = value; + insert_(root); + } + + void insert_(NODE *&sour) + { + if (sour == NIL) + { + newnode(sour, tkey); + return; + } + ++sour->size; + if (tkey < sour->key) + { + insert_(sour->left); + } + else + { + insert_(sour->right); + } + maintain(sour, tkey >= sour->key); + } + + NODE *search(const T &value) + { + NODE *sour = root; + while (root != NIL && value != sour->key) + { + if (value < sour->key) + { + sour = sour->left; + } + else + { + sour = sour->right; + } + } + return sour; + } +}; + +void ex_gcd(LL a, LL b, LL &x, LL &y, LL &g) +{ + if (!b) + { + g = a; + x = 1; + y = 0; + return; + } + ex_gcd(b, a % b, y, x, g); + y -= x * (a / b); +} + +LL inv(LL a, LL n) +{ + LL ret, temp, g; + ex_gcd(a, n, ret, temp, g); + return g == 1LL ? (ret + n) % n : -1; +} + +LL pow_mod(LL a, LL p, LL n) +{ + LL ret = 1LL; + for (int i = 0; (1LL << i) <= p; a = a * a % p) + if (p & (1LL << i)) + { + ret = ret * a % p; + } + return ret; +} + +LL china(int n, int *a, int *m) +{ + LL M = 1LL, ret = 0LL, x, y, g; + for (int i = 0; i < n; ++i) + { + M *= m[i]; + } + for (int i = 0; i < n; ++i) + { + LL w = M / m[i]; + ex_gcd(w, m[i], x, y, g); + ret = (ret + x * w * a[i]) % M; + } + return (ret + M) % M; +} + +int C, S; +int Y[10][100]; +int quant[10]; +int X[10]; +const int LIMIT(10000); + +//SBT st[10]; +set st[10]; + +void solve_enum(int best) +{ + int count = 0; + for (int i = 0; i < C; ++i) + if (i != best) + { + //st[i].init(); + st[i].clear(); + for (int j = 0; j < quant[i]; ++j) + { + st[i].insert(Y[i][j]); + } + } + sort(Y[best], Y[best] + quant[best]); + for (int i = 0; count != S; ++i) + for (int j = 0; j < quant[best]; ++j) + { + LL temp = Y[best][j] + (LL)X[best] * i; + if (temp == 0LL) + { + continue; + } + bool flag = true; + for (int i = 0; i < C; ++i) + if (i != best && st[i].find(temp % X[i]) == st[i].end())//st[i].search(temp%X[i]) == st[i].NIL) + { + flag = false; + break; + } + if (flag) + { + printf("%lld\n", temp); + if (++count == S) + { + break; + } + } + } +} + +vector ans; +int arr[10]; +void dfs(int dep) +{ + if (dep == C) + { + ans.push_back(china(C, arr, X)); + } + else + { + for (int i = 0; i < quant[dep]; ++i) + { + arr[dep] = Y[dep][i]; + dfs(dep + 1); + } + } +} + +void solve_china() +{ + ans.clear(); + dfs(0); + sort(ans.begin(), ans.end()); + LL M = 1LL; + for (int i = 0; i < C; ++i) + { + M *= X[i]; + } + int count = 0; + for (int i = 0; count != S; ++i) + for (int j = 0; j < ans.size(); ++j) + { + LL temp = ans[j] + M * i; + if (temp > 0LL) + { + printf("%lld\n", temp); + if (++count == S) + { + break; + } + } + } +} + +int main() +{ + while (scanf("%d%d", &C, &S), C + S) + { + int best = 0, tot = 1; + for (int i = 0; i < C; ++i) + { + scanf("%d%d", X + i, quant + i); + for (int j = 0; j < quant[i]; ++j) + { + scanf("%d", Y[i] + j); + } + if (quant[i] * X[best] < quant[best] * X[i]) + { + best = i; + } + tot *= quant[i]; + } + if (tot > LIMIT) + { + solve_enum(best); + } + else + { + solve_china(); + } + putchar('\n'); + } + return 0; +} diff --git a/11755.cpp b/11755.cpp new file mode 100644 index 0000000..40dc453 --- /dev/null +++ b/11755.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +const double eps = 1e-5; + +char s[105]; +double dp[2][25][25]; + +int main() +{ + int T, cas = 0; + scanf("%d", &T); + while (T--) + { + double w[2]; + gets(s); + gets(s); + scanf("%lf%lf", &w[0], &w[1]); + int n = strlen(s), ct1 = 0, ct2 = 0; + bool ok = 1; + for (int i = 0; i < n; i++) + { + if (ct1 >= 21 || ct2 >= 21) + { + ok = 0; + break; + } + if (s[i] == 'W') + { + if (w[(i / 5) & 1] < eps) + { + ok = 0; + } + ct1++; + } + else + { + if (w[(i / 5) & 1] + eps > 1) + { + ok = 0; + } + ct2++; + } + if (ct1 == 20 && ct2 == 20) + { + ct1 = 15, ct2 = 15; + } + } + printf("Case %d: ", ++cas); + if (!ok) + { + printf("%.6f\n", -1.0); + continue; + } + if (ct1 >= 21 || ct2 >= 21) + { + printf("%.6f\n", ct1 >= 21 ? 1.0 : 0.0); + continue; + } + for (int i = 0; i <= 21; i++) + for (int j = 0; j <= 21; j++) + { + dp[0][i][j] = 0; + } + dp[0][15][15] = 1; + int c = 0, cur = 0, nk, nj; + double ret = 0, tp = 0; + for (int tt = 0; tt < 2; tt++) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j <= 21; j++) + for (int k = 0; k <= 21; k++) + { + dp[1 - cur][j][k] = 0; + } + for (int j = 0; j <= 20; j++) + for (int k = 0; k <= 20; k++) + { + nj = j + 1, nk = k; + if (nj == 20 && nk == 20) + { + nj = 15, nk = 15; + } + dp[1 - cur][nj][nk] += dp[cur][j][k] * w[c]; + nj = j, nk = k + 1; + if (nj == 20 && nk == 20) + { + nj = 15, nk = 15; + } + dp[1 - cur][nj][nk] += dp[cur][j][k] * (1 - w[c]); + } + for (int j = 0; j <= 20; j++) + { + ret += dp[1 - cur][21][j]; + } + tp += dp[1 - cur][15][15]; + dp[1 - cur][15][15] = 0; + cur = 1 - cur; + } + c = 1 - c; + } + if (fabs(tp - 1) < eps) + { + printf("%.6f\n", 0.0); + continue; + } + ret /= (1 - tp); + for (int i = 0; i <= 21; i++) + for (int j = 0; j <= 21; j++) + { + dp[0][i][j] = 0; + } + bool first = 1; + c = (n / 5) & 1, cur = 0; + dp[0][ct1][ct2] = 1; + double ans = 0, sum1 = 0, sum2 = 0; + for (int tt = 0; tt < 10; tt++) + { + for (int i = first ? n % 5 : 0; i < 5; i++) + { + if (fabs(dp[cur][15][15]) > eps) + { + sum1 += dp[cur][15][15]; + dp[cur][15][15] = 0; + } + for (int j = 0; j <= 21; j++) + for (int k = 0; k <= 21; k++) + { + dp[1 - cur][j][k] = 0; + } + for (int j = 0; j <= 20; j++) + for (int k = 0; k <= 20; k++) + { + nj = j + 1, nk = k; + if (nj == 20 && nk == 20) + { + nj = 15, nk = 15; + } + dp[1 - cur][nj][nk] += dp[cur][j][k] * w[c]; + nj = j, nk = k + 1; + if (nj == 20 && nk == 20) + { + nj = 15, nk = 15; + } + dp[1 - cur][nj][nk] += dp[cur][j][k] * (1 - w[c]); + } + for (int j = 0; j <= 20; j++) + { + ans += dp[1 - cur][21][j]; + sum2 += dp[1 - cur][j][21]; + } + cur = 1 - cur; + } + first = 0; + c = 1 - c; + } + printf("%.6f\n", ans + sum1 * ret); + } + return 0; +} diff --git a/11757.cpp b/11757.cpp new file mode 100644 index 0000000..42a6f96 --- /dev/null +++ b/11757.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define maxn 210 +#define maxe 50010 +#define INF 1 << 30 +#define LL long long +#define eps 1e-15 +#define PI acos(-1.0) +int n, m; +int f[maxe], cap[maxe];//flow[maxn][maxn]; +int first[maxn], dis[maxn], gap[maxn], arc[maxn], Next[maxe], vv[maxe], pre[maxn], add, tot; +void adj(int u, int v, int ca) +{ + Next[add] = first[u]; + vv[add] = v; + cap[add] = ca; + first[u] = add++; + Next[add] = first[v]; + vv[add] = u; + cap[add] = 0; + first[v] = add++; +} +int sap(int s, int t, int n) +{ + int q[maxn], i, j, mindis, front = 0, rear = 1, u, v, e; + int ans = 0, low; + bool found, vis[maxn]; + memset(dis, 0, sizeof(dis)); + memset(gap, 0, sizeof(gap)); + memset(vis, 0, sizeof(vis)); + memset(arc, 0, sizeof(arc)); + q[0] = t; + vis[t] = true; + dis[t] = 0; + gap[0] = 1; + while (front < rear) + { + u = q[front++]; + e = first[u]; + while (e) + { + v = vv[e]; + if (!vis[v]) + { + dis[v] = dis[u] + 1; + vis[v] = true; + q[rear++] = v; + gap[dis[v]]++; + arc[v] = first[v]; + } + e = Next[e]; + } + } + u = s; + low = INF; + pre[s] = s;//gap[s]=n; + while (dis[s] < n) + { + found = false; + for (int &e = arc[u]; e; e = Next[e]) + if (dis[vv[e]] == dis[u] - 1 && cap[e] > f[e]) + { + found = true; + v = vv[e]; + low = low < cap[e] - f[e] ? low : cap[e] - f[e]; + pre[v] = u; + u = v; + if (u == t) + { + while (u - s) + { + u = pre[u]; + f[arc[u]] += low; + f[arc[u] ^ 1] -= low; + } + ans += low; + low = INF; + } + break; + } + if (found) + { + continue; + } + mindis = n; + for (int e = first[u]; e; e = Next[e]) + if (mindis > dis[vv[e]] && cap[e] > f[e]) + { + mindis = dis[vv[j = e]]; + arc[u] = e; + }; + gap[dis[u]]--; + if (gap[dis[u]] == 0) + { + return ans; + } + dis[u] = mindis + 1; + gap[dis[u]]++; + u = pre[u]; + } + return ans; +} +struct node +{ + int x, y; +} pt[maxn]; +int L, W; +inline int sqr(int a) +{ + return a * a; +} +int cal(int a, int b) +{ + return sqr(pt[a].x - pt[b].x) + sqr(pt[a].y - pt[b].y); +} +int main() +{ + int ncase, tt = 0, i, j, u, v, s, e; + while (scanf("%d %d %d %d", &L, &W, &n, &m), L || W || n || m) + { + memset(first, 0, sizeof(first)); + memset(f, 0, sizeof(f)); + add = 2; + s = 0, e = n * 2 + 1; + for (i = 1; i <= n; ++i) + { + scanf("%d %d", &pt[i].x, &pt[i].y); + } + for (i = 1; i <= n; i++) + { + if (pt[i].y <= m) + { + adj(s, i, 1); + } + if (pt[i].y + m >= W) + { + adj(i + n, e, 1); + } + } + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + if (i != j) + { + if (cal(i, j) <= 4 * m * m) + { + adj(i + n, j, 1); + } + } + for (i = 1; i <= n; ++i) + { + adj(i, i + n, 1); + } + printf("Case %d: %d\n", ++tt, sap(s, e, n * 2 + 2)); + } + return 0; +} diff --git a/11759.cpp b/11759.cpp new file mode 100644 index 0000000..f83cec5 --- /dev/null +++ b/11759.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define zero(x) (((x) > 0 ? (x) : -(x)) < eps) +#define maxn 2000 +int Q[maxn]; +struct point +{ + double x, y; +} tmp[maxn], P[600][1100]; +double xmult(point p1, point p2, point p0) +{ + return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y); +} +bool cmp(point a, point b) +{ + return a.y < b.y || (a.y == b.y && a.x < b.x); +} +void out(point a) +{ + printf("--%lf %lf\n", a.x, a.y); +} +void calc(int &N, point P[]) +{ + int i, cnt; + sort(P, P + N, cmp); + cnt = 0; + Q[cnt++] = 0; + for (i = 1; i < N;) + if (cnt == 1 || xmult(P[Q[cnt - 1]], P[i], P[Q[cnt - 2]]) >= 0) + { + Q[cnt++] = i++; + } + else + { + cnt--; + } + int top = cnt; + for (i = N - 2; i >= 0;) + if (cnt == top || xmult(P[Q[cnt - 1]], P[i], P[Q[cnt - 2]]) >= 0) + { + Q[cnt++] = i--; + } + else + { + cnt--; + } + cnt--; + for (i = 0; i < cnt; i++) + { + tmp[i] = P[Q[i]]; + } + N = cnt; + memcpy(P, tmp, N * sizeof(tmp[0])); + return; +} +int N, sz[1000]; +bool isin(int out, int in) +{ + int i, j; + for (i = 0; i < sz[out]; i++) + { + j = (i + 1) % sz[out]; + if (xmult(P[out][j], P[in][0], P[out][i]) < 0) + { + break; + } + } + if (i < sz[out]) + { + return 0; + } + return 1; +} +int mat[600][600], in[600]; +double len[600]; +#define sqr(x) (x) * (x) +double calcdis(point a, point b) +{ + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); +} +int main() +{ + int i, j, tcas = 1; + while (scanf("%d", &N) && N) + { + for (i = 0; i < N; i++) + { + scanf("%d", &sz[i]); + for (j = 0; j < sz[i]; j++) + { + scanf("%lf%lf", &P[i][j].x, &P[i][j].y); + } + calc(sz[i], P[i]); + len[i] = 0; + for (j = 0; j < sz[i]; j++) + { + len[i] += calcdis(P[i][j], P[i][(j + 1) % sz[i]]); + } + /*for (j=0;j %d\n",j,i); + // out(P[j][0]),out(P[i][0]); + mat[i][j] = 1; + mat[j][i] = 0; + in[j]++; + } + } + else + { + mat[i][j] = 0; + } + } + int L = 0, R = 0, Q[1000], f[1000]; + for (i = 0; i < N; i++) + if (in[i] == 0) + { + f[i] = 0; + Q[R++] = i; + } + else + { + f[i] = -1; + } + printf("Case %d:\n", tcas++); + printf("Total Number of Communities %d\n", R); + double ans0 = 0, ans1 = 0; + while (L < R) + { + int now = Q[L++]; + // printf("%d %d\n",now,f[now]); + // out(P[now][0]); + if (f[now]) + { + ans1 += len[now] * 50; + } + else + { + ans0 += len[now] * 100; + } + for (i = 0; i < N; i++) + if (mat[now][i] == 1) + { + in[i]--; + if (in[i] == 0) + { + f[i] = 1 - f[now]; + Q[R++] = i; + } + } + } + printf("Total Cost:\nSteel Fence: %.9lf Million Yuan\nWooden Fence: %.9lf Million Yuan\n\n", ans0 / 1000000, ans1 / 1000000); + } +} diff --git a/11760.cpp b/11760.cpp new file mode 100644 index 0000000..5082f32 --- /dev/null +++ b/11760.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int main() +{ + int case_number = 0; + int r, c, n; + int x, y; + while (cin >> r >> c >> n) + { + if (r == 0 && c == 0 && n == 0) + { + break; + } + map row_taken, col_taken; + while (n--) + { + cin >> x >> y; + row_taken[x] = true; + col_taken[y] = true; + } + cin >> x >> y; + bool ok = false; + int dx[] = {0, 0, 0, -1, 1}; + int dy[] = {0, -1, 1, 0, 0}; + rep(i, 5) + { + if (x + dx[i] >= 0 && x + dx[i] < r && y + dy[i] >= 0 && y + dy[i] < c && !row_taken[x + dx[i]] && !col_taken[y + dy[i]]) + { + ok = true; + } + } + if (ok) + { + cout << "Case " << ++case_number << ": Escaped again! More 2D grid problems!" << endl; + } + else + { + cout << "Case " << ++case_number << ": Party time! Let's find a restaurant!" << endl; + } + } + return 0; +} diff --git a/11761.cpp b/11761.cpp new file mode 100644 index 0000000..89e7656 --- /dev/null +++ b/11761.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +#define SIDE_MAX 40001 +#define P_MAX 26458 +#define sq(a) ((a) * (a)) +typedef long long int LL; + +vector eratos(P_MAX, 0); + +LL GCD(LL a, LL b) +{ + if (a % b == 0) + { + return b; + } + else + { + return GCD(b, a % b); + } +} +inline LL SquaredRadiusOfCircumcircle(LL a, LL b, LL c) +{ + if (a < b + c && b < a + c && c < a + b) + { + LL numer1 = a * b * c, numer2 = a * b * c; + LL denom1 = a + b + c, denom2 = -a + b + c, denom3 = a - b + c, denom4 = a + b - c; + LL gcd1, gcd2, gcd3, gcd4; + gcd1 = GCD(numer1, denom1); + numer1 /= gcd1; + denom1 /= gcd1; + gcd2 = GCD(numer2, denom2); + numer2 /= gcd2; + denom2 /= gcd2; + gcd3 = GCD(numer1, denom3); + numer1 /= gcd3; + denom3 /= gcd3; + gcd4 = GCD(numer2, denom4); + numer2 /= gcd4; + denom4 /= gcd4; + if ((numer1 * numer2) % (denom1 * denom2 * denom3 * denom4) == 0) + { + return (numer1 * numer2) / (denom1 * denom2 * denom3 * denom4); + } + } + return -1; +} +void MakePrimes() +{ + for (int i = 2; i < P_MAX; ++i) + { + if (eratos[i] == 0) + { + for (int j = 2; i * j < P_MAX; ++j) + { + eratos[i * j] = 1; + } + } + } +} +int ExamineMaxPrime(int N) +{ + for (int i = (int)sqrt((double)N); i >= 2; --i) + { + if (eratos[i] == 0 && N % i == 0) + { + return i; + } + } + return N; +} +int main() +{ + MakePrimes(); + while (true) + { + LL R, A, M; + int maxprime = 2; + int min_side = SIDE_MAX; + int min_side_ind = -1; + bool bFound = false; + vector> vanses; + cin >> R >> A; + if (R == 0 && A == 0) + { + break; + } + M = 4 * A * R; + maxprime = max(maxprime, ExamineMaxPrime((int)R)); + maxprime = max(maxprime, ExamineMaxPrime((int)A)); + for (int a = 1; a < SIDE_MAX && !bFound; a++) + { + if (M % a != 0) + { + continue; + } + LL t = M / a; + if (t > sq(SIDE_MAX)) + { + continue; + } + for (int b = maxprime; b < SIDE_MAX && !bFound; b += maxprime) + { + if (t % b != 0) + { + continue; + } + LL c = t / b; + if (sq(R) == SquaredRadiusOfCircumcircle(a, b, c) && c < SIDE_MAX) + { + vector vans; + vans.push_back(a); + vans.push_back(b); + vans.push_back((int)c); + sort(vans.begin(), vans.end()); + vanses.push_back(vans); + } + } + } + for (unsigned int i = 0; i < vanses.size(); ++i) + { + for (unsigned int j = 0; j < vanses[i].size(); ++j) + { + if (min_side > vanses[i][j]) + { + min_side = vanses[i][j]; + min_side_ind = i; + } + } + } + if (min_side_ind >= 0) + { + cout << vanses[min_side_ind][0] << ' ' << vanses[min_side_ind][1] << ' ' << vanses[min_side_ind][2] << endl; + } + else + { + cout << "-1 -1 -1\n"; + } + } + return 0; +} diff --git a/11762.cpp b/11762.cpp new file mode 100644 index 0000000..f63aa3b --- /dev/null +++ b/11762.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define _PMAX 1048576 +#define _SQRT 1024 +#define FORS(a, b, c, s) for (int a = b; a < c; a += s) +#define FORI(a, b, c) FORS(a, b, c, 1) +#define FOR(a, b) FORS(a, 0, b, 1) + +bool sieve[_PMAX], v[_PMAX]; +int a[10], f[10], fc, p[_PMAX / 10], pc = 1, tf; +double s[_PMAX]; +int primesto(int n) +{ + int c, u, v; + for (u = 0, v = pc; u < v;) + { + c = (u + v) >> 1; + if (p[c] > n) + { + v = c; + } + else + { + u = c + 1; + } + } + return u + (p[u] <= n); +} +void factor(int n) +{ + fc = 0; + FOR(i, pc) + { + int x = p[i]; + if (x * x > n) + { + break; + } + if (!(n % x)) + { + f[fc] = x; + a[fc] = 0; + do + { + a[fc]++; + n /= x; + } while (!(n % x)); + fc++; + } + } + if (n != 1) + { + f[fc] = n; + a[fc++] = 1; + } + tf = fc; +} +double doit(int x) +{ + if (v[x]) + { + return s[x]; + } + double c = 1 / (double)tf; + double sum = 0; + //printf("Entering %d with %d factors\n", x, tf); + FOR(i, fc) + { + if (a[i]) + { + if (!(--a[i])) + { + --tf; + } + sum += doit(x / f[i]); + if (!(a[i]++)) + { + ++tf; + } + } + } + double sl = primesto(x) / (double)tf; + //printf("Steps to leave %d = %.10lf\n", x, sl); + s[x] = sl + c * sum; + //printf("s[%d] = %.10lf\n", x, s[x]); + v[x] = 1; + return s[x]; +} + +int main() +{ + int n, t; + FOR(i, _PMAX) + sieve[i] = 1, + v[i] = 0; + p[0] = 2; + v[1] = 1; + s[1] = 0; + FORS(i, 4, _PMAX, 2) + sieve[i] = 0; + FORS(i, 3, _SQRT, 2) + if (sieve[i]) + { + p[pc++] = i; + FORS(j, i * i, _PMAX, i) + sieve[j] = 0; + } + FORS(i, _SQRT + ~(_SQRT & 1), _PMAX, 2) + if (sieve[i]) + { + p[pc++] = i; + } + scanf("%d", &t); + for (int cnum = 0; cnum++ < t;) + { + scanf("%d", &n); + factor(n); + doit(n); + printf("Case %d: %.9lf\n", cnum, s[n]); + } + return 0; +} diff --git a/11764.cpp b/11764.cpp index 2bc114c..72f3a58 100644 --- a/11764.cpp +++ b/11764.cpp @@ -1,23 +1,33 @@ -#include -using namespace std; - -int main(){ - int t; - cin>>t; - for(int k=1;k<=t;k++){ - int n; - cin>>n; - int wall[50]={0}; - for(int i=0;i>wall[i]; - int l=0,s=0; - for(int i=0;iwall[i]) - l++; - } - cout<<"Case "< + +using namespace std; + +int main() +{ + int t; + int n; + int h[50]; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> h[i]; + } + int high = 0, low = 0; + for (int i = 1; i < n; i++) + { + if (h[i] > h[i - 1]) + { + high++; + } + else if (h[i] < h[i - 1]) + { + low++; + } + } + cout << "Case " << casenum + 1 << ": " << high << " " << low << endl; + } + return 0; +} diff --git a/11765.cpp b/11765.cpp new file mode 100644 index 0000000..2564347 --- /dev/null +++ b/11765.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +const int MAXN = 1000; +const int MAXE = 500000; +const int INF = 2100000000; + +int dis[MAXN], pre[MAXN], gap[MAXN], arc[MAXN], f[MAXE], cap[MAXE], fst[MAXN], nxt[MAXE], vv[MAXE], q[MAXN], vis[MAXN], X[MAXN], Y[MAXN]; + +int sap(int s, int t, int n) +{ + int j, mindis, ans = 0, front = 0, rear = 1, u, v, low; + bool found; + + memset(dis, 0, sizeof(dis)); + memset(gap, 0, sizeof(gap)); + memset(vis, 0, sizeof(vis)); + memset(arc, 0, sizeof(arc)); + memset(f, 0, sizeof(f)); + + u = s; + low = INF; + pre[s] = s; + + while (dis[s] < n) + { + found = false; + + for (int &e = arc[u]; e; e = nxt[e]) + if (dis[vv[e]] == dis[u] - 1 && cap[e] > f[e]) + { + found = true; + v = vv[e]; + + low = low < cap[e] - f[e] ? low : cap[e] - f[e]; + pre[v] = u; + u = v; + + if (u == t) + { + while (u - s) + { + u = pre[u]; + f[arc[u]] += low; + f[arc[u] ^ 1] -= low; + } + + ans += low; + low = INF; + } + break; + } + + if (found) + continue; + + mindis = n; + for (int e = fst[u]; e; e = nxt[e]) + { + if (mindis > dis[vv[e]] && cap[e] > f[e]) + { + mindis = dis[vv[j = e]]; + arc[u] = e; + } + } + + gap[dis[u]]--; + if (gap[dis[u]] == 0) + { + return ans; + } + + dis[u] = mindis + 1; + gap[dis[u]]++; + + u = pre[u]; + } + return ans; +} + +inline void add(int u, int v, int c, int &e) +{ + nxt[e] = fst[u], vv[e] = v, cap[e] = c, fst[u] = e++; + nxt[e] = fst[v], vv[e] = u, cap[e] = 0, fst[v] = e++; +} + +int main() +{ + int T, n, m, u, v, i, j, k, e; + int ii = 0; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n,&m); + memset(fst, 0, sizeof(fst)); + e = 2; + int st = n + 1, ed = st + 1; + for (i = 1; i <= n; i++) + { + scanf("%d", &X[i]); + } + for (i = 1; i <= n; i++) + { + scanf("%d", &Y[i]); + } + for (i = 1; i <= n; i++) + { + scanf("%d", &j); + if (j == 0) + { + add(st, i, X[i], e), add(i, ed, Y[i], e); + } + else if (j > 0) + { + add(st, i, X[i], e), add(i, ed, 2100000000, e); + } + else + { + add(st, i, 2100000000, e), add(i, ed, Y[i], e); + } + } + while (m--) + { + scanf("%d%d%d", &i, &j, &k); + add(i, j, k, e); + add(j, i, k, e); + } + printf("%d\n", sap(st, ed, ed)); + } + return 0; +} diff --git a/11766.cpp b/11766.cpp new file mode 100644 index 0000000..a63e902 --- /dev/null +++ b/11766.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int n, cases = 0; + int a, b; + while (scanf("%d", &n) == 1 && n) + { + map R[1024]; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &a, &b); + if (a + b >= n) + continue; + int &ww = R[a + 1][n - b]; + ww = min(ww + 1, n - a - b); + } + int ret = 0, dp[1024] = {}; + for (int i = 0; i <= n; i++) + { + if (i) + dp[i] = max(dp[i], dp[i - 1]); + for (map::iterator it = R[i + 1].begin(); + it != R[i + 1].end(); it++) + dp[it->first] = max(dp[it->first], dp[i] + it->second); + ret = max(ret, dp[i]); + } + printf("Case %d: %d\n", ++cases, n - ret); + } + return 0; +} diff --git a/11768.cpp b/11768.cpp new file mode 100644 index 0000000..276ebbd --- /dev/null +++ b/11768.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define eps 0.0001 +int GCD(int a, int b) +{ + if (b == 0) + { + return a; + } + while ((a %= b) && (b %= a)) + ; + return a + b; +} +int main() +{ + int t, x1, y1, x2, y2, gcd, dx, dy, x, y, B[10][10], i, j, k, sd; + double d; + scanf("%d", &t); + while (t--) + { + scanf("%lf", &d); + x1 = (int)(d * 10 + eps); + scanf("%lf", &d); + y1 = (int)(d * 10 + eps); + scanf("%lf", &d); + x2 = (int)(d * 10 + eps); + scanf("%lf", &d); + y2 = (int)(d * 10 + eps); + gcd = GCD(abs(dx = x2 - x1), abs(dy = y2 - y1)); + if (gcd == 0) + { + if (x % 10 == 0 && y % 10 == 0) + { + printf("1\n"); + } + else + { + printf("0\n"); + } + continue; + } + dx /= gcd, dy /= gcd; + dx %= 10, dy %= 10; + if (dx < 0) + { + dx += 10; + } + if (dy < 0) + { + dy += 10; + } + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + B[i][j] = 0; + } + } + for (x = x1 % 10, y = y1 % 10, k = 0; x | y; k++, x = (x + dx) % 10, y = (y + dy) % 10) + { + if (B[x][y]) + { + break; + } + B[x][y] = 1; + } + if (x | y) + { + printf("0\n"); + continue; + } + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + B[i][j] = 1; + } + } + for (x = y = i = 0; B[x][y]; i++, x = (x + dx) % 10, y = (y + dy) % 10) + { + B[x][y] = i; + } + if (gcd < k) + { + printf("0\n"); + continue; + } + else + { + gcd -= k; + gcd = gcd / i + 1; + printf("%d\n", gcd); + } + } + return 0; +} diff --git a/11769.cpp b/11769.cpp new file mode 100644 index 0000000..32c95ba --- /dev/null +++ b/11769.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +const double eps = 1e-6; +const int MAXN = 510; +int sgn(double x) +{ + return (x > eps) - (x < -eps); +} +double sqr(double x) +{ + return x * x; +} +struct node +{ + double x, y, z; + node() {} + node(double a, double b, double c) : x(a), y(b), z(c) {} + void read() + { + scanf("%lf%lf%lf", &x, &y, &z); + } + node operator-(node a) + { + return node(x - a.x, y - a.y, z - a.z); + } + node operator*(node a) + { + return node(y * a.z - a.y * z, a.x * z - x * a.z, x * a.y - a.x * y); + } + double operator^(node a) + { + return x * a.x + y * a.y + z * a.z; + } + double dis(node a) + { + return sqrt(sqr(x - a.x) + sqr(y - a.y) + sqr(z - a.z)); + } +} zero(0, 0, 0); +int to[MAXN][MAXN]; +struct ThreeD +{ + int n; + node p[MAXN]; + struct Face + { + int a, b, c; + bool ok; + } F[MAXN * 8]; + int cnt; + void read() + { + for (int i = 0; i < n; i++) + { + p[i].read(); + } + } + double volume(node p1, node p2, node p3, node p4) + { + return ((p2 - p1) * (p3 - p1)) ^ (p4 - p1); + } + double get(node p1, Face f) + { + return volume(p[f.a], p[f.b], p[f.c], p1); + } + void deal(int pos, int a, int b) + { + int f = to[a][b]; + Face tmp; + if (F[f].ok) + { + if (sgn(get(p[pos], F[f])) > 0) + { + dfs(pos, f); + } + else + { + to[b][a] = to[a][pos] = to[pos][b] = cnt; + tmp.a = b, tmp.b = a, tmp.c = pos, tmp.ok = 1; + F[cnt++] = tmp; + } + } + } + void dfs(int pos, int f) + { + F[f].ok = 0; + deal(pos, F[f].b, F[f].a); + deal(pos, F[f].c, F[f].b); + deal(pos, F[f].a, F[f].c); + } + void run() + { + cnt = 0; + if (n <= 3) + { + return; + } + int i; + for (i = 1; i < n; i++) + if (sgn(p[0].dis(p[i])) > 0) + { + swap(p[1], p[i]); + break; + } + if (i == n) + { + return; + } + for (i = 2; i < n; i++) + { + if (sgn(zero.dis((p[0] - p[1]) * (p[i] - p[0]))) > 0) + { + swap(p[2], p[i]); + break; + } + } + if (i == n) + { + return; + } + for (i = 3; i < n; i++) + { + if (fabs(volume(p[0], p[1], p[2], p[i])) > eps) + { + swap(p[3], p[i]); + break; + } + } + if (i == n) + { + return; + } + Face tmp; + for (i = 0; i <= 3; i++) + { + tmp.a = (i + 1) % 4, tmp.b = (i + 2) % 4, tmp.c = (i + 3) % 4, tmp.ok = 1; + if (sgn(get(p[i], tmp)) > 0) + { + swap(tmp.b, tmp.c); + } + to[tmp.a][tmp.b] = to[tmp.b][tmp.c] = to[tmp.c][tmp.a] = cnt; + F[cnt++] = tmp; + } + int j; + for (i = 4; i < n; i++) + for (j = 0; j < cnt; j++) + { + if (F[j].ok && sgn(get(p[i], F[j])) > 0) + { + dfs(i, j); + break; + } + } + int T = cnt; + cnt = 0; + for (i = 0; i < T; i++) + if (F[i].ok) + { + F[cnt++] = F[i]; + } + } + bool same(Face A, Face B) + { + return sgn(get(p[A.a], B)) == 0 && + sgn(get(p[A.b], B)) == 0 && + sgn(get(p[A.c], B)) == 0; + } + double area() + { + int i; + double ans = 0; + for (i = 0; i < cnt; i++) + { + ans += zero.dis((p[F[i].b] - p[F[i].a]) * (p[F[i].c] - p[F[i].a])) / 2.0; + } + return ans; + } + int FaceNum() + { + int i, j, ans = 0; + for (i = 0; i < cnt; i++) + { + for (j = i + 1; j < cnt; j++) + { + if (same(F[i], F[j])) + { + break; + } + } + if (j == cnt) + { + ans++; + } + } + return ans; + } +} H; +int main() +{ + int c = 0; + while (~scanf("%d", &H.n), H.n) + { + H.read(); + H.run(); + printf("Case %d: %.2lf\n", ++c, H.area()); + } + return 0; +} diff --git a/1177.cpp b/1177.cpp new file mode 100644 index 0000000..a3fc3d9 --- /dev/null +++ b/1177.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +int NN = 2; +const int MATSIZE = 5; + +struct Matrix +{ + double mat[MATSIZE][MATSIZE]; +}; +Matrix Base; + +Matrix matMul(Matrix A, Matrix B) +{ + Matrix C; + for (int i = 0; i < NN; i++) + for (int j = 0; j < NN; j++) + { + C.mat[i][j] = 0; + for (int k = 0; k < NN; k++) + { + C.mat[i][j] = C.mat[i][j] + A.mat[i][k] * B.mat[k][j]; + } + } + return C; +} + +Matrix matAdd(Matrix A, Matrix B) +{ + Matrix C; + for (int i = 0; i < NN; i++) + for (int j = 0; j < NN; j++) + C.mat[i][j] = (A.mat[i][j] + B.mat[i][j]); + return C; +} + +Matrix matPow(int p) +{ + if (p == 1) + return Base; + Matrix R = matPow(p >> 1); + R = matMul(R, R); + if (p & 1) + R = matMul(R, Base); + return R; +} + +int read(int &x) +{ +#define gc getchar + int c = gc(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = gc(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = gc()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +int main() +{ + int T; + read(T); + while (T--) + { + int f0, f1, n; + double a, b; + read(f0), read(f1); + scanf("%lf%lf", &a, &b); + read(n); + if (n == 0) + { + printf("%d\n", f0); + } + else if (n == 1) + { + printf("%d\n", f1); + } + else + { + Base.mat[0][0] = a; + Base.mat[0][1] = b; + Base.mat[1][0] = 1; + Base.mat[1][1] = 0; + Matrix R = matPow(n - 1); + double Ans = R.mat[0][0] * f1 + R.mat[0][1] * f0; + printf("%0.lf\n", Ans); + } + } + return 0; +} diff --git a/11770.cpp b/11770.cpp new file mode 100644 index 0000000..ad8f880 --- /dev/null +++ b/11770.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define N 10000 + +bool vis[N + 3]; +int comp[N + 3], in[N + 3]; +vector Edges[N + 3], rEdges[N + 3], sortedNodes; + +void reset() +{ + for (int i = 0; i < N + 3; i++) + { + Edges[i].clear(); + rEdges[i].clear(); + } + sortedNodes.clear(); + memset(vis, false, sizeof(vis)); + memset(in, 0, sizeof(in)); +} + +void dfs1(int x) +{ + vis[x] = true; + for (size_t u = 0; u < Edges[x].size(); u++) + if (!vis[Edges[x][u]]) + { + dfs1(Edges[x][u]); + } + sortedNodes.push_back(x); +} + +void dfs2(int x, int c) +{ + vis[x] = false; + comp[x] = c; + for (size_t u = 0; u < rEdges[x].size(); u++) + if (vis[rEdges[x][u]]) + { + dfs2(rEdges[x][u], c); + } +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + reset(); + int n, m; + scanf("%d%d", &n, &m); + for (int i = 0; i < m; i++) + { + int a, b; + scanf("%d%d", &a, &b); + Edges[a].push_back(b); + rEdges[b].push_back(a); + } + for (int i = 1; i <= n; i++) + if (!vis[i]) + { + dfs1(i); + } + int c = 0; + for (int i = sortedNodes.size() - 1; i >= 0; i--) + if (vis[sortedNodes[i]]) + { + dfs2(sortedNodes[i], ++c); + } + for (int i = 1; i <= n; i++) + { + for (size_t j = 0; j < Edges[i].size(); j++) + if (comp[i] != comp[Edges[i][j]]) + { + in[comp[Edges[i][j]]]++; + } + } + int cnt = 0; + for (int i = 1; i <= c; i++) + if (in[i] == 0) + { + cnt++; + } + printf("Case %d: %d\n", ++cases, cnt); + } + return 0; +} diff --git a/11773.cpp b/11773.cpp index 0fe4825..afc9728 100644 --- a/11773.cpp +++ b/11773.cpp @@ -1,55 +1,120 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int isPrime(int64 num){ - int64 lim=(int64)sqrt(num); - lim=lim<100?lim:100; - for(int i=2;i<=lim;i++) - if(num%i==0) - return 0; - return 1; -} -int main(){ - int test,z; - scanf("%d",&test); - for(z=1;z<=test;z++){ - int64 K; - scanf("%lld",&K); - if(K%2!=0){ - if(isPrime(K)){ - printf("Case %d: Impossible\n",z); - continue; - } - } - int64 width=(int64)sqrt(K); - int64 mat[10000][4]; - int lim=0; - while(width-->2){ - int64 length=K/width; - if(length!=width && length*width==K){ - mat[lim][0]=length; - mat[lim][1]=width; - mat[lim][2]=length-width; - mat[lim][3]=2*(length+width); - lim++; - } - } - int ind=0,i; - for(i=1;imat[ind][2]){ - ind=i; - continue; - } - if(mat[i][2]==mat[ind][2] && mat[i][3]>mat[ind][3]) - ind=i; - } - printf("Case %d: %lld %lld\n",z,mat[ind][0],mat[ind][1]); - } - return 0; -} +#include + +using namespace std; + +#define MAX 1000000 +#define FORALL(it, o) for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it) + +vector vprimes; +void MakePrime() +{ + vprimes.reserve(MAX); + bool isPrime[MAX]; + isPrime[0] = false; + isPrime[1] = false; + isPrime[2] = true; + for (int i = 2; i < MAX; ++i) + { + isPrime[i] = true; + } + for (long long int i = 2; i < MAX; ++i) + { + if (isPrime[i]) + { + for (long long int j = 2; i * j < MAX; ++j) + { + isPrime[i * j] = false; + } + } + } + for (int i = 0; i < MAX; ++i) + { + if (isPrime[i]) + { + vprimes.push_back(i); + } + } +} +void Factorization(long long int n, vector &vp, map &optional) +{ + if (n == 1) + { + vp.push_back(1); + optional[1] = 1; + return; + } + for (int i = 0; i < (int)vprimes.size(); ++i) + { + if (vprimes[i] > n) + { + break; + } + while (n % vprimes[i] == 0) + { + n /= vprimes[i]; + optional[vprimes[i]]++; + vp.push_back(vprimes[i]); + } + if (n == 1) + { + break; + } + } + if (n != 1) + { + vp.push_back(n); + } + optional[n]++; +} +long long int mypow(long long int a, int b) +{ + long long int ret = 1; + for (int i = 0; i < b; ++i) + { + ret *= a; + } + return ret; +} +int main() +{ + MakePrime(); + int T; + cin >> T; + for (int tc = 1; tc <= T; ++tc) + { + long long int K; + map primes; + vector factors; + long long int L = -1, W = -1, max_diff = 0; + cin >> K; + Factorization(K, factors, primes); + FORALL(itm, primes) + { + long long int tw = mypow(itm->first, itm->second); + long long int tl = K / itm->first; + if (tl > tw && tl < K) + { + if (max_diff < tl - tw) + { + max_diff = tl - tw; + L = tl; + W = tw; + } + else if (max_diff == tl - tw && tl > L) + { + L = tl; + W = tw; + } + } + } + if (L < 0) + { + printf("Case %d: Impossible\n", tc); + } + else + { + printf("Case %d: %lld %lld\n", tc, L, W); + } + } + return 0; +} diff --git a/11774.cpp b/11774.cpp index 6146839..4f14edc 100644 --- a/11774.cpp +++ b/11774.cpp @@ -1,22 +1,53 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - uint64 a,b,c; - int test; - cin>>test; - for(int z=1;z<=test;z++){ - cin>>a>>b; - if(a==b) - cout<<"Case "< + +using namespace std; + +#define MAX 1048576 +char buf[MAX], *lim = buf + MAX, *now = lim; +void readint(int *n) +{ + while (now != lim && !isdigit(*now)) + { + now++; + } + if (now == lim) + { + int r = fread(buf, 1, MAX - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + now = buf; + } + while (!isdigit(*now)) + { + now++; + } + *n = 0; + while (isdigit(*now)) + { + *n = *n * 10 + *now++ - '0'; + } +} +int gcd(int a, int b) +{ + if (!b) + { + return a; + } + return gcd(b, a % b); +} +int main() +{ + int a, b, t; + readint(&t); + for (int cnum = 0; cnum++ < t;) + { + readint(&a); + readint(&b); + printf("Case %d: %d\n", cnum, (a + b) / gcd(a, b)); + } + return 0; +} diff --git a/11775.cpp b/11775.cpp new file mode 100644 index 0000000..505ed60 --- /dev/null +++ b/11775.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define inf 0x3f3f3f3f +#define Inf 0x3FFFFFFFFFFFFFFFLL +#define eps 1e-9 +#define pi acos(-1.0) + +typedef long long ll; +const int maxn = 3000 + 100; +const int mod = 10000007; +char str[maxn]; +int p[maxn], valA[maxn], valB[maxn], dp[maxn][maxn]; +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + p[0] = 1; + for (int i = 1; i < maxn; ++i) + { + p[i] = (p[i - 1] * 2) % mod; + } + int t, tcase = 0; + scanf("%d", &t); + map mp; + while (t--) + { + tcase++; + mp.clear(); + scanf("%s", str); + int n = strlen(str); + int i = 0, cnt = 0, totA = 0, totB = 0; + string s = ""; + while (i < n) + { + s = ""; + s += str[i++]; + while (str[i] >= '0' && str[i] <= '9') + { + s += str[i++]; + } + if (mp[s]) + { + valA[totA++] = mp[s]; + } + else + { + mp[s] = valA[totA++] = ++cnt; + } + } + scanf("%s", str); + n = strlen(str); + i = 0; + while (i < n) + { + s = ""; + s += str[i++]; + while (str[i] >= '0' && str[i] <= '9') + { + s += str[i++]; + } + if (mp[s]) + { + valB[totB++] = mp[s]; + } + else + { + mp[s] = valB[totB++] = ++cnt; + } + } + memset(dp, 0, sizeof(dp)); + for (int i = 1; i <= totA; ++i) + for (int j = 1; j <= totB; ++j) + if (valA[i - 1] == valB[j - 1]) + { + dp[i][j] = (dp[i - 1][j - 1] * 2 + 1) % mod; + } + else + { + dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]) % mod; + } + int ans = (p[totA] + p[totB] - 2) % mod; + ans = (ans - dp[totA][totB] * 2) % mod; + ans = (ans + mod) % mod; + printf("Case %d: %d\n", tcase, ans); + } + return 0; +} diff --git a/11776.cpp b/11776.cpp new file mode 100644 index 0000000..e7f24c2 --- /dev/null +++ b/11776.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +struct interval +{ + int a, b; + bool w; + interval(int _a = 0, int _b = 0) + { + a = _a, b = _b; + } + bool operator<(const interval x) const + { + return a < x.a; + } +} k[1024]; + +int main() +{ + for (int ans, bt, cnum = 1, n; scanf("%d", &n) && n != -1; ++cnum) + { + for (int i = 0; i < n; ++i) + { + scanf("%d %d", &k[i].a, &k[i].b), k[i].w = 0; + } + ans = 0; + sort(k, k + n); + for (int i = 0; i < n; ++i) + { + if (k[i].w) + { + continue; + } + ans++; + bt = k[i].b; + for (int j = i + 1; j < n; ++j) + if (!k[j].w && bt < k[j].a) + { + k[j].w = 1, bt = k[j].b; + } + } + printf("Case %d: %d\n", cnum, ans); + } + return 0; +} diff --git a/11777.cpp b/11777.cpp index 9c539df..6af6af4 100644 --- a/11777.cpp +++ b/11777.cpp @@ -1,45 +1,41 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -int main(){ - int test; - scanf("%d",&test); - for(int z=1;z<=test;z++){ - double marks[4],clasTest[3],sum=0; - int i; - for(i=0;i<4;i++){ - scanf("%lf",&marks[i]); - sum+=marks[i]; - } - for(i=0;i<3;i++){ - scanf("%lf",&clasTest[i]); - } - sort(clasTest,clasTest+3); - sum+=((clasTest[1]+clasTest[2])/2.0); - if(sum>=90){ - printf("Case %d: A\n",z); - continue; - } - if(sum>=80 && sum<90){ - printf("Case %d: B\n",z); - continue; - } - if(sum>=70 && sum<80){ - printf("Case %d: C\n",z); - continue; - } - if(sum>=60 && sum<70){ - printf("Case %d: D\n",z); - continue; - } - else - printf("Case %d: F\n",z); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int t; + int results[7]; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + for (int i = 0; i < 7; i++) + { + cin >> results[i]; + } + sort(results + 4, results + 7); + double sum = results[0] + results[1] + results[2] + results[3] + (results[5] + results[6]) / 2.0; + cout << "Case " << casenum + 1 << ": "; + if (sum >= 90) + { + cout << "A" << endl; + } + else if (sum >= 80) + { + cout << "B" << endl; + } + else if (sum >= 70) + { + cout << "C" << endl; + } + else if (sum >= 60) + { + cout << "D" << endl; + } + else + { + cout << "F" << endl; + } + } + return 0; +} diff --git a/11779.cpp b/11779.cpp new file mode 100644 index 0000000..72a745e --- /dev/null +++ b/11779.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +typedef long long ll; +typedef pair paii; +typedef pair pall; + +#define PI (2.0 * acos(0)) +#define ERR 1e-5 +#define mem(a, b) memset(a, b, sizeof a) +#define pb push_back +#define popb pop_back +#define all(x) (x).begin(), (x).end() +#define mp make_pair +#define SZ(x) (int)x.size() +#define oo (1 << 25) +#define FOREACH(it, x) for (__typeof((x).begin()) it = (x.begin()); it != (x).end(); ++it) +#define Contains(X, item) ((X).find(item) != (X).end()) +#define popc(i) (__builtin_popcountll(i)) +#define fs first +#define sc second +#define EQ(a, b) (fabs(a - b) < ERR) +#define MAX 55 +#define twoL(X) (((ll)(1)) << (X)) + +template +void deb(T1 e) +{ + cout << e << endl; +} +template +void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } +template +void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } + +template +T Abs(T x) { return x > 0 ? x : -x; } +template +inline T sqr(T x) { return x * x; } +ll Pow(ll B, ll P) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + R = (R * B); + P /= 2; + B = (B * B); + } + return R; +} +ll BigMod(ll B, ll P, ll M) +{ + ll R = 1; + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B) % M; + } + P /= 2; + B = (B * B) % M; + } + return R; +}/// (B^P)%M + +///int rrr[]={1,0,-1,0};int ccc[]={0,1,0,-1}; //4 Direction +///int rrr[]={1,1,0,-1,-1,-1,0,1};int ccc[]={0,1,1,1,0,-1,-1,-1};//8 direction +///int rrr[]={2,1,-1,-2,-2,-1,1,2};int ccc[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +///int rrr[]={2,1,-1,-2,-1,1};int ccc[]={0,1,1,0,-1,-1}; //Hexagonal Direction +///int month[]={31,28,31,30,31,30,31,31,30,31,30,31}; //month + +int dist[MAX][MAX]; +vector v[MAX]; +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + int cas, loop = 0, n, e, a, b, w; + scanf("%d", &cas); + while (cas--) + { + scanf("%d %d", &n, &e); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + dist[i][j] = 0; + for (int i = 0; i < e; i++) + { + scanf("%d %d %d", &a, &b, &w); + dist[a][b] = w; + } + + for (int i = 0; i < n; i++) + v[i].clear(); + + for (int k = 0; k < n; k++) + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + dist[i][j] -= dist[i][k] * dist[k][j]; + } + + e = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + e += dist[i][j]; + printf("Case %d: %d %d\n", ++loop, n, e); + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (dist[i][j]) + v[i].pb(j); + + for (int i = 0; i < n; i++) + { + printf("%d", SZ(v[i])); + for (int j = 0; j < SZ(v[i]); j++) + printf(" %d", v[i][j]); + puts(""); + } + } + + return 0; +} diff --git a/11780.cpp b/11780.cpp new file mode 100644 index 0000000..f5c09da --- /dev/null +++ b/11780.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const int N = 1005; +int f[40]; +float e[N]; +int main() +{ + int n; + f[0] = 1; + f[1] = 2; + for (int i = 2; i < 16; ++i) + { + f[i] = f[i - 1] + f[i - 2]; + } + e[0] = 0; + for (int i = 1; i < 1001; ++i) + { + e[i] = 1000000; + } + for (int i = 0; i < 15; ++i) + { + float error = f[i + 1] - f[i] * 1.6; + for (int j = f[i]; j < 1001; ++j) + { + float ne = e[j - f[i]] + error; + if (fabs(ne) < fabs(e[j])) + { + e[j] = ne; + } + } + } + while (scanf("%d", &n) && n) + { + printf("%.2f\n", fabs(e[n])); + } + return 0; +} diff --git a/11782.cpp b/11782.cpp new file mode 100644 index 0000000..3734ee8 --- /dev/null +++ b/11782.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int cost[(1 << 20) - 1], dp[2][(1 << 19)][21]; + +void read(int n, int pos) +{ + scanf("%d", &cost[pos]); + if (n > 0) + { + read(n - 1, 2 * pos + 1); + read(n - 1, 2 * pos + 2); + } +} + +int main() +{ + int H, K, aux; + while (true) + { + scanf("%d", &H); + if (H == -1) + { + break; + } + scanf("%d", &K); + read(H, 0); + if (H == 0) + { + printf("%d\n", cost[0]); + } + else + { + for (int i = 0; i < (1 << H); ++i) + for (int j = 1; j <= K; ++j) + { + dp[H & 1][i][j] = cost[(1 << H) - 1 + i]; + } + for (int i = H - 1; i >= 0; --i) + { + for (int pos = 0; pos < (1 << i); ++pos) + { + for (int j = 1; j <= K; ++j) + { + aux = cost[(1 << i) - 1 + pos]; + for (int k = 1; k < j; ++k) + { + aux = max(aux, dp[(i ^ 1) & 1][2 * pos][k] + dp[(i ^ 1) & 1][2 * pos + 1][j - k]); + } + dp[i & 1][pos][j] = aux; + } + } + } + printf("%d\n", dp[0][0][K]); + } + } + return 0; +} diff --git a/11783.cpp b/11783.cpp new file mode 100644 index 0000000..4055ae8 --- /dev/null +++ b/11783.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAX 1048576 +char buf[MAX], *lim = buf + MAX, *now = lim; +void readint(int *n) +{ + while (now != lim && !isdigit(*now)) + { + now++; + } + if (now == lim) + { + int r = fread(buf, 1, MAX, stdin); + lim = buf + r - 1; + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + now = buf; + } + while (!isdigit(*now)) + { + now++; + } + *n = 0; + while (isdigit(*now)) + { + *n = *n * 10 + *now++ - '0'; + } +} +struct point +{ + int x, y; + int operator*(point p) + { + return (x * p.y - y * p.x); + } + point operator-(point p) + { + return point(x - p.x, y - p.y); + } + point(int _x = 0, int _y = 0) + { + x = _x, y = _y; + } +}; +struct segment +{ + point a, b; + int i; +} s[1000]; +int signal(int x) +{ + if (x) + { + return 1 + (x < 0); + } + return 0; +} +bool intersect(segment s1, segment s2) +{ + int r1, r2; + point v1 = s1.b - s1.a; + point v2 = s2.b - s2.a; + r1 = v1 * (s2.a - s1.a); + r2 = v1 * (s2.b - s1.a); + if (signal(r1) == signal(r2)) + { + return false; + } + r1 = v2 * (s1.a - s2.a); + r2 = v2 * (s1.b - s2.a); + return (signal(r1) != signal(r2)); +} +int main() +{ + int ans, n, x1, x2, y1, y2; + for (readint(&n); n; readint(&n)) + { + ans = 0; + for (int i = 0; i < n; ++i) + { + readint(&x1), readint(&y1), readint(&x2), readint(&y2); + s[i].a = point(x1, y1); + s[i].b = point(x2, y2); + s[i].i = 0; + for (int j = 0; j < i; ++j) + if (intersect(s[i], s[j])) + { + ++s[i].i, ++s[j].i; + } + } + for (int i = 0; i < n; ++i) + { + if (s[i].i) + { + ans += s[i].i; + } + else + { + ans += 4; + } + } + printf("%d\n", ans >> 1); + } + return 0; +} diff --git a/11784.cpp b/11784.cpp new file mode 100644 index 0000000..fefd810 --- /dev/null +++ b/11784.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +struct Door +{ + int x, y; +} dr[4040]; + +int top, bot, left_, right_, px, py, m, n, h, mintime, minstep; + +void TinhD() +{ + top = px; + bot = m - 1 - px; + left_ = py; + right_ = n - 1 - py; +} + +void Escape(int len, int p) +{ + if (len + 1 < mintime || (len + 1 == mintime && p + 1 < minstep)) + { + mintime = len + 1; + minstep = p + 1; + } +} + +void Check_Top(int i) +{ + if (py - dr[i].y >= top) + { + Escape(py - dr[i].y, top); + } + else if (n - 1 - dr[i].y + top >= right_) + { + Escape(n - 1 - dr[i].y + top, right_); + } + else + { + Escape(n - 2 - dr[i].y + m + right_, bot); + } +} + +void Check_Right(int i) +{ + if (px - dr[i].x >= right_) + { + Escape(px - dr[i].x, right_); + } + else if (m - 1 - dr[i].x + right_ >= bot) + { + Escape(m - 1 - dr[i].x + right_, bot); + } + else + { + Escape(m - 2 - dr[i].x + n + bot, left_); + } +} + +void Check_Bot(int i) +{ + if (dr[i].y - py >= bot) + { + Escape(dr[i].y - py, bot); + } + else if (dr[i].y + bot >= left_) + { + Escape(dr[i].y + bot, left_); + } + else + { + Escape(dr[i].y + m - 1 + left_, top); + } +} + +void Check_Left(int i) +{ + if (dr[i].x - px >= left_) + { + Escape(dr[i].x - px, left_); + } + else if (dr[i].x + left_ >= top) + { + Escape(dr[i].x + left_, top); + } + else + { + Escape(dr[i].x + n - 1 + top, right_); + } +} + +void Find() +{ + for (int i = 1; i <= h; i++) + { + if (dr[i].x == 0) + { + Check_Top(i); + } + else if (dr[i].y == n - 1) + { + Check_Right(i); + } + else if (dr[i].x == m - 1) + { + Check_Bot(i); + } + else + { + Check_Left(i); + } + } +} + +int main() +{ + scanf("%d%d%d", &m, &n, &h); + while (m != 0 && n != 0 && h != 0) + { + for (int i = 1; i <= h; i++) + { + scanf("%d %d", &dr[i].x, &dr[i].y); + } + scanf("%d %d", &px, &py); + TinhD(); + mintime = minstep = 500000; + Find(); + + printf("%d %d\n", mintime, minstep); + scanf("%d %d %d", &m, &n, &h); + } + return 0; +} diff --git a/11785.cpp b/11785.cpp new file mode 100644 index 0000000..314b013 --- /dev/null +++ b/11785.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +typedef int lld; + +const int maxn = 1100, maxm = 6000 * 2; +const int maxl = 999999999; + +struct Edge +{ + int y; + int ne; +} e[maxm]; +int ee; +int st[maxn]; +int n; +int du[maxn]; +void addedge(int x, int y) +{ + e[ee].y = y; + e[ee].ne = st[x]; + st[x] = ee++; + e[ee].y = x; + e[ee].ne = st[y]; + st[y] = ee++; + du[x]++; + du[y]++; +} +int c[maxn], que[maxn]; +int head, tail; +lld bfs(int sx) +{ + int i, j, k; + int tot = 0; + memset(c, -1, sizeof(c)); + c[sx] = 0; + head = 0; + tail = 1; + que[0] = sx; + while (head < tail) + { + k = que[head++]; + for (i = st[k]; i != -1; i = e[i].ne) + if (c[e[i].y] == -1) + { + c[e[i].y] = c[k] + 1; + que[tail++] = e[i].y; + tot += c[k] + 1; + } + } + return tot; +} +int num(int k) +{ + int tot = 0; + while (k) + { + tot += k & 1; + k >>= 1; + } + return tot; +} +int main() +{ + int m, i, j; + while (scanf("%d%d", &n, &m), n) + { + memset(st, -1, sizeof(st)); + memset(du, 0, sizeof(du)); + ee = 0; + while (m--) + { + scanf("%d%d", &i, &j); + addedge(i, j); + } + lld tot = 0; + if (num(n) != 1) + { + puts("NO"); + continue; + } + for (i = 0; i < n; i++) + if (du[i] != du[0]) + { + break; + } + if (i < n) + { + puts("NO"); + continue; + } + for (i = 0; i < n; i++) + { + tot += bfs(i); + } + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + tot -= num(i ^ j); + } + if (tot) + { + puts("NO"); + } + else + { + puts("YES"); + } + } + return 0; +} diff --git a/11786.cpp b/11786.cpp new file mode 100644 index 0000000..026bb13 --- /dev/null +++ b/11786.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +#define IN getc(stdin) +const int N = 20050, M = 10025; +int d[N]; +int main() +{ + int ans, h, i, t; + scanf("%d\n", &t); + register char c; + while (t--) + { + for (i = 0; i < N; ++i) + { + d[i] = -1; + } + for (ans = i = 0, c = IN, h = M; c != 10; c = IN, i++) + { + if (c == '\\') + { + d[h--] = i; + } + else if (c == '/' && d[++h] != -1) + { + ans += i - d[h]; + d[h] = -1; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/11787.cpp b/11787.cpp new file mode 100644 index 0000000..b22b2af --- /dev/null +++ b/11787.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +string hieroglyphs = "BUSPFTM"; + +int hieroglyph_to_int(string &input) +{ + int result = 0, pos; + for (int i = 0, sz = input.size(); i < sz; i++) + { + pos = hieroglyphs.find(input[i]); + result += pow(10, pos); + } + return result; +} + +bool is_ordered(string &input) +{ + char prev = input[0]; + int diff = 0, cur_pos, prev_pos = hieroglyphs.find(prev); + for (int i = 1, sz = input.size(); i < sz; i++) + { + cur_pos = hieroglyphs.find(input[i]); + if (cur_pos - prev_pos == 0) + { + continue; + } + if (diff == 0) + { + diff = cur_pos - prev_pos; + prev_pos = cur_pos; + continue; + } + if (cur_pos - prev_pos > 0 && diff < 0 || cur_pos - prev_pos < 0 && diff > 0) + { + return false; + } + prev_pos = cur_pos; + } + return true; +} + +bool no_more_than_nine_equal_chars(string &input) +{ + char prev = input[0]; + int count = 1; + for (int i = 1, sz = input.size(); i < sz; i++) + { + if (input[i] == prev) + { + count++; + } + else + { + if (count > 9) + { + return false; + } + else + { + prev = input[i]; + count = 1; + } + } + } + if (count > 9) + { + return false; + } + return true; +} + +bool correct_hieroglyph(string &input) +{ + return is_ordered(input) && no_more_than_nine_equal_chars(input); +} + +int main() +{ + int n; + string input; + cin >> n; + getline(cin, input);// \n after n + while (n--) + { + getline(cin, input); + if (!correct_hieroglyph(input)) + { + cout << "error" << endl; + continue; + } + cout << hieroglyph_to_int(input) << endl; + } + return 0; +} diff --git a/11788.cpp b/11788.cpp new file mode 100644 index 0000000..0cc1619 --- /dev/null +++ b/11788.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +#define double long double + +const double eps = 1e-11; +const double low = atan(-3.66 / 11.), high = atan(3.66 / 11.); +const double g = 9.81; +const double pi = acos(-1); + +bool isin(double a) +{ + double ta = a; + if (ta < low || high < ta) + { + return false; + } + return true; +} + +double dist(double a) +{ + return 11. / cos(a); +} + +double height(double d, double b, double v0) +{ + double t = d / (v0 * cos(b)); + return -g * t * t / 2. + v0 * sin(b) * t + 0.11; +} + +void solve(double a, double b, double v0) +{ + if (!isin(a)) + { + cout << "out" << endl; + return; + } + double d = dist(a); + double h = height(d, b, v0); + if (h < 0.11) + { + cout << "bounce" << endl; + } + else if (h - eps > 2.44) + { + cout << "out" << endl; + } + else + { + cout << "goal" << endl; + } +} + +main() +{ + double a, b, v0; + while (cin >> a >> b >> v0) + { + if (a == 0 && b == 0 && v0 == 0) + { + break; + } + double aa = pi / 180 * a, bb = pi / 180 * b; + solve(aa, bb, v0); + } +} diff --git a/11790.cpp b/11790.cpp new file mode 100644 index 0000000..fada935 --- /dev/null +++ b/11790.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int lis(vector a, vector w) +{ + int Sum[a.size()]; + for (size_t i = 0; i < a.size(); i++) + { + Sum[i] = w[i]; + for (size_t j = 0; j < i; j++) + if (a[i] < a[j]) + { + Sum[i] = max(Sum[i], Sum[j] + w[i]); + } + } + return *max_element(Sum, Sum + a.size()); +} +int main() +{ + int T, N; + cin >> T; + for (int t = 1; t <= T; t++) + { + cin >> N; + vector a(N), w(N); + for (int i = 0; i < N; i++) + { + cin >> a[i]; + } + for (int i = 0; i < N; i++) + { + cin >> w[i]; + } + int LDS = lis(a, w); + reverse(a.begin(), a.end()), reverse(w.begin(), w.end()); + int LIS = lis(a, w); + if (LIS >= LDS) + { + printf("Case %d. Increasing (%d). Decreasing (%d).\n", t, LIS, LDS); + } + else + { + printf("Case %d. Decreasing (%d). Increasing (%d).\n", t, LDS, LIS); + } + } + return 0; +} diff --git a/11792.cpp b/11792.cpp new file mode 100644 index 0000000..97286bf --- /dev/null +++ b/11792.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/117/11792.html + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 10010 +#define LL unsigned long long + +int costs[N]; +int freq[N]; +vector edges[N]; +int n; + +int bfs(int at) +{ + for (int i = 0; i < N; i++) + { + costs[i] = INT_MAX; + } + costs[at] = 0; + queue q; + q.push(at); + while (!q.empty()) + { + int p = q.front(); + q.pop(); + for (size_t i = 0; i < edges[p].size(); i++) + { + if (costs[edges[p].at(i)] == INT_MAX) + { + costs[edges[p].at(i)] = costs[p] + 1; + q.push(edges[p].at(i)); + } + } + } + int ret = 0; + for (int i = 1; i <= n; i++) + { + if (freq[i] >= 2) + { + ret += costs[i]; + } + } + return ret; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + memset(freq, 0, sizeof(freq)); + for (int i = 0; i < N; i++) + { + edges[i].clear(); + } + int s; + scanf("%d %d", &n, &s); + for (int i = 0; i < s; i++) + { + int curr; + int prev; + scanf("%d", &curr); + freq[curr]++; + prev = curr; + while (scanf("%d", &curr) && curr) + { + freq[curr]++; + edges[curr].push_back(prev); + edges[prev].push_back(curr); + prev = curr; + } + } + int min_cost = INT_MAX; + int output; + for (int i = 1; i <= n; i++) + { + if (freq[i] >= 2) + { + int c = bfs(i); + if (c < min_cost) + { + min_cost = c; + output = i; + } + } + } + printf("Krochanska is in: %d\n", output); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11795.cpp b/11795.cpp new file mode 100644 index 0000000..818e160 --- /dev/null +++ b/11795.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define N 16 +#define NN (1 << 16) +int ck[NN], q[NN], qp, qs, r[N]; +long long s[NN]; +char opt[128]; +void cast(int *x) +{ + *x = 0; + for (char *p = opt; *p; ++p) + { + *x = ((*x) << 1) + (*p == '1'); + } +} +void push(int a, int b) +{ + *(q + qs) = a; + *(ck + qs++) = b; +} +int main() +{ + int cs, n, t; + scanf("%d", &t); + for (int cnum = 0; cnum++ < t;) + { + qp = qs = 0; + scanf("%d", &n); + for (int i = 0; i < (1 << n); ++i) + { + s[i] = 0; + } + scanf("%s", opt), cast(&cs); + for (int i = 0; i < n; ++i) + { + scanf("%s", opt), cast(r + i); + } + push(0, cs); + s[0] = 1; + while (qp < qs) + { + int cid = q[qp], cck = ck[qp++]; + for (int i = 0; i < n; ++i) + { + int mm = 1 << i; + if ((cck & mm) && !(cid & mm)) + { + int nid = cid | mm, nck = cck | r[n - i - 1]; + if (!s[nid]) + { + push(nid, nck); + } + s[nid] += s[cid]; + } + } + } + printf("Case %d: %lld\n", cnum, s[(1 << n) - 1]); + } + return 0; +} diff --git a/11796.cpp b/11796.cpp new file mode 100644 index 0000000..4e5045d --- /dev/null +++ b/11796.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +const double eps = 1e-8; + +int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} + +const double PI = acos(-1.0); +double torad(double deg) +{ + return deg / 180 * PI; +} + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; + +typedef Point Vector; + +Vector operator+(const Vector &A, const Vector &B) +{ + return Vector(A.x + B.x, A.y + B.y); +} +Vector operator-(const Point &A, const Point &B) +{ + return Vector(A.x - B.x, A.y - B.y); +} +Vector operator*(const Vector &A, double p) +{ + return Vector(A.x * p, A.y * p); +} +Vector operator/(const Vector &A, double p) +{ + return Vector(A.x / p, A.y / p); +} + +bool operator<(const Point &a, const Point &b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} + +bool operator==(const Point &a, const Point &b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} + +Point read_point() +{ + double x, y; + scanf("%lf%lf", &x, &y); + return Point(x, y); +}; + +double Dot(const Vector &A, const Vector &B) +{ + return A.x * B.x + A.y * B.y; +} +double Length(const Vector &A) +{ + return sqrt(Dot(A, A)); +} +double Cross(const Vector &A, const Vector &B) +{ + return A.x * B.y - A.y * B.x; +} + +double DistanceToSegment(const Point &P, const Point &A, const Point &B) +{ + if (A == B) + { + return Length(P - A); + } + Vector v1 = B - A, v2 = P - A, v3 = P - B; + if (dcmp(Dot(v1, v2)) < 0) + { + return Length(v2); + } + else if (dcmp(Dot(v1, v3)) > 0) + { + return Length(v3); + } + else + { + return fabs(Cross(v1, v2)) / Length(v1); + } +} + +const int maxn = 60; +int T, A, B; +Point P[maxn], Q[maxn]; +double Min, Max; + +void update(Point P, Point A, Point B) +{ + Min = min(Min, DistanceToSegment(P, A, B)); + Max = max(Max, Length(P - A)); + Max = max(Max, Length(P - B)); +} + +int main() +{ + scanf("%d", &T); + for (int kase = 1; kase <= T; kase++) + { + scanf("%d%d", &A, &B); + for (int i = 0; i < A; i++) + { + P[i] = read_point(); + } + for (int i = 0; i < B; i++) + { + Q[i] = read_point(); + } + double LenA = 0, LenB = 0; + for (int i = 0; i < A - 1; i++) + { + LenA += Length(P[i + 1] - P[i]); + } + for (int i = 0; i < B - 1; i++) + { + LenB += Length(Q[i + 1] - Q[i]); + } + int Sa = 0, Sb = 0; + Point Pa = P[0], Pb = Q[0]; + Min = 1e9, Max = -1e9; + while (Sa < A - 1 && Sb < B - 1) + { + double La = Length(P[Sa + 1] - Pa); // 甲到下一æ‹ç‚¹çš„è·ç¦» + double Lb = Length(Q[Sb + 1] - Pb); // 乙到下一æ‹ç‚¹çš„è·ç¦» + double T = min(La / LenA, Lb / LenB); // å–åˆé€‚çš„å•ä½ï¼Œå¯ä»¥è®©ç”²å’Œä¹™çš„速度分别是LenAå’ŒLenB + Vector Va = (P[Sa + 1] - Pa) / La * T * LenA;// 甲的ä½ç§»å‘é‡ + Vector Vb = (Q[Sb + 1] - Pb) / Lb * T * LenB;// 乙的ä½ç§»å‘é‡ + update(Pa, Pb, Pb + Vb - Va); // 求解“简化版â€ï¼Œæ›´æ–°æœ€å°æœ€å¤§è·ç¦» + Pa = Pa + Va; + Pb = Pb + Vb; + if (Pa == P[Sa + 1]) + { + Sa++; + } + if (Pb == Q[Sb + 1]) + { + Sb++; + } + } + printf("Case %d: %.0lf\n", kase, Max - Min); + } + return 0; +} diff --git a/11797.cpp b/11797.cpp new file mode 100644 index 0000000..e32a6b3 --- /dev/null +++ b/11797.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const char *s[5] = {"Ja", "Sam", "Sha", "Sid", "Tan"}; + +char name[12], map_[128]; +int l[5][20], time_[5], ls[5], lp[5]; + +inline int min(int a, int b) +{ + return a < b ? a : b; +} +int main() +{ + int c, ct, m, n, next, t; + map_[0] = 0; + map_['m'] = 1; + map_['a'] = 2; + map_['d'] = 3; + map_['n'] = 4; + scanf("%d", &t); + for (int cnum = 0; cnum++ < t;) + { + scanf("%d %d %s", &m, &n, name); + c = *(map_ + *(name + 2)); + for (int i = 0; i < 5; ++i) + { + scanf("%d", &ls[i]); + lp[i] = time_[i] = 0; + for (int j = 0; j < ls[i]; ++j) + { + scanf("%s", name); + l[i][j] = *(map_ + *(name + 2)); + } + } + for (ct = 0; ct < n; ct += m + 2) + { + time_[c] += min(n - ct, m); + next = l[c][lp[c]]; + lp[c] = (lp[c] + 1) % ls[c]; + c = next; + } + printf("Case %d:\n", cnum); + for (int i = 0; i < 5; ++i) + { + printf("%s %d\n", s[i], time_[i]); + } + } + return 0; +} diff --git a/11798.cpp b/11798.cpp new file mode 100644 index 0000000..a95f320 --- /dev/null +++ b/11798.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +const int MOD = (int)1e9 + 7; +const int MAXK = 51; +const int MAXN = 211; + +int dp[MAXK][MAXN], choose[MAXN][MAXN], odd[MAXK][MAXN], even[MAXK][MAXN]; + +int add_mod(int a, int b) +{ + return a + b >= MOD ? a + b - MOD : a + b; +} + +int multiply_mod(int a, int b) +{ + return (long long)(a)*b % MOD; +} + +int main() +{ + choose[0][0] = 1; + for (int i = 1; i < MAXN; ++i) + { + choose[i][0] = choose[i][i] = 1; + for (int j = 1; j < i; ++j) + { + choose[i][j] = add_mod(choose[i - 1][j - 1], choose[i - 1][j]); + } + } + dp[0][0] = 1; + for (int index = 1; index < MAXN; ++index) + { + dp[1][index] = 1; + } + for (int i = 2; i < MAXK; ++i) + { + for (int j = i; j < MAXN; ++j) + { + for (int k = 1; j - k >= i - 1; ++k) + { + dp[i][j] = add_mod(dp[i][j], multiply_mod(dp[i - 1][j - k], choose[j][k])); + } + } + } + odd[1][0] = 1; + for (int i = 2; i < MAXK; ++i) + { + for (int j = 1; j < MAXN - 1; ++j) + { + int w = j, b = j + 1; + for (int k = 1; k < i; ++k) + { + int w_color = k, b_color = i - k; + int ways = choose[i][k]; + ways = multiply_mod(ways, dp[b_color][b]); + ways = multiply_mod(ways, dp[w_color][w]); + odd[i][j] = add_mod(odd[i][j], ways); + } + } + } + for (int i = 2; i < MAXK; ++i) + { + for (int j = 1; j < MAXN; ++j) + { + int w = j, b = j; + for (int k = 1; k < i; ++k) + { + int w_color = k, b_color = i - k; + int ways = choose[i][k]; + ways = multiply_mod(ways, dp[b_color][b]); + ways = multiply_mod(ways, dp[w_color][w]); + even[i][j] = add_mod(even[i][j], ways); + } + } + } + int task; + scanf("%d", &task); + for (int task_index = 1; task_index <= task; ++task_index) + { + int n, m, k; + scanf("%d %d %d", &n, &m, &k); + int w = (n + 1) * (m + 1) / 2; + bool bit = ((n + 1) * (m + 1) & 1) == 1; + int ret = 0; + for (int empty = 0; empty <= k; ++empty) + { + int ways = multiply_mod(choose[k][empty], bit ? odd[k - empty][w] : even[k - empty][w]); + ret = add_mod(ret, ways); + } + printf("Case %d: %d\n", task_index, ret); + } + return 0; +} diff --git a/11799.cpp b/11799.cpp index 5c28793..c6db71f 100644 --- a/11799.cpp +++ b/11799.cpp @@ -1,47 +1,25 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - cin >> T; - FOI(t, 1, T){ - int N, I, M = 0; - cin >> N; - while (N--){ - cin >> I; - M = max(I, M); +int main() +{ + int cases, tmp; + int students, result; + cin >> cases; + for (int i = 0; i < cases; i++) + { + result = 0; + cin >> students; + for (int j = 0; j < students; j++) + { + cin >> tmp; + if (tmp > result) + { + result = tmp; + } } - cout << "Case " << t << ": " << M << endl; + cout << "Case " << i + 1 << ": " << result << endl; } return 0; } - diff --git a/118.cpp b/118.cpp new file mode 100644 index 0000000..fbadfb3 --- /dev/null +++ b/118.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define MAXN 52 + +char Grid[MAXN][MAXN]; +char Cmd[110]; +char Side[] = "WNES"; +int X[] = {0, -1, 0, 1}; +int Y[] = {-1, 0, 1, 0}; +int D[5]; +int R, C; + +void Ini() +{ + int i; + for (i = 0; Side[i]; i++) + { + D[Side[i]] = i; + } +} + +void Move(int r, int c, int dir) +{ + int i, tr, tc; + for (i = 0; Cmd[i]; i++) + { + if (Cmd[i] == 'R') + { + dir++; + } + else if (Cmd[i] == 'L') + { + dir--; + } + dir += 4; + dir %= 4; + if (Cmd[i] == 'F') + { + tc = Y[dir] + c; + tr = X[dir] + r; + if (tr > R || tc > C || tr < 0 || tc < 0) + { + if (Grid[r][c] != 1) + { + printf("%d %d %c LOST\n", c, R - r, Side[dir]); + Grid[r][c] = 1; + return; + } + } + else + { + r = tr; + c = tc; + } + } + } + printf("%d %d %c\n", c, R - r, Side[dir]); +} + +int main() +{ + int sr, sc, dir; + char input[100], ss[10]; + Ini(); + gets(input); + sscanf(input, "%d%d", &C, &R); + while (gets(input)) + { + gets(Cmd); + sscanf(input, "%d%d%s", &sc, &sr, ss); + dir = D[ss[0]]; + Move(R - sr, sc, dir); + } + return 0; +} diff --git a/1180.cpp b/1180.cpp new file mode 100644 index 0000000..7b73547 --- /dev/null +++ b/1180.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +bool prime(long long x) +{ + if (x < 2) + return false; + if (x == 2) + return true; + if (x & 1 == 0) + return false; + int k = sqrt(x); + for (int i = 3; i <= k; i += 2) + { + if (x % i == 0) + return false; + } + return true; +} + +int main() +{ + long long p; + cin >> p; + long long a[p]; + cin >> a[0]; + for (int i = 1; i < p; i++) + { + scanf(",%lld", &a[i]); + } + for (int i = 0; i < p; i++) + { + if (prime(a[i]) && prime(pow(2, a[i]) - 1)) + cout << "Yes" << endl; + else + cout << "No" << endl; + } + return 0; +} diff --git a/11800.cpp b/11800.cpp index 4abc002..cd6c426 100644 --- a/11800.cpp +++ b/11800.cpp @@ -1,170 +1,108 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +#include -struct Point{ - int64 X; - int64 Y; - int64 I; -}; - -bool comp(Point A, Point B){ - return A.I < B.I; -} +using namespace std; -bool isSquare(Point point[]){ - do{ - int64 A = sqr(point[0].X - point[1].X) + sqr(point[0].Y - point[1].Y); - int64 B = sqr(point[1].X - point[2].X) + sqr(point[1].Y - point[2].Y); - int64 C = sqr(point[2].X - point[3].X) + sqr(point[2].Y - point[3].Y); - int64 D = sqr(point[3].X - point[0].X) + sqr(point[3].Y - point[0].Y); - if( A != B || B != C || C != D || D != A ) - continue; - int64 P = (point[0].Y - point[1].Y) * (point[1].Y - point[2].Y) + (point[0].X - point[1].X) * (point[1].X - point[2].X); - int64 Q = (point[1].Y - point[2].Y) * (point[2].Y - point[3].Y) + (point[1].X - point[2].X) * (point[2].X - point[3].X); - int64 R = (point[2].Y - point[3].Y) * (point[3].Y - point[0].Y) + (point[2].X - point[3].X) * (point[3].X - point[0].X); - int64 S = (point[3].Y - point[0].Y) * (point[0].Y - point[1].Y) + (point[3].X - point[0].X) * (point[0].X - point[1].X); - if( P == 0 && Q == 0 && R == 0 && S == 0 ) - return true; - }while( next_permutation(point, point + 4, comp) ); - - return false; -} +#define db(a) cout << #a << " = " << a << endl; +#define db2(a, b) cout << #a << " = " << a << " -- " << #b << " = " << b << endl; +#define foreach(it, l) for (typeof(l.begin()) it = l.begin(); it != l.end(); it++) -bool isRectangle(Point point[]){ - do{ - int64 A = sqr(point[0].X - point[1].X) + sqr(point[0].Y - point[1].Y); - int64 B = sqr(point[1].X - point[2].X) + sqr(point[1].Y - point[2].Y); - int64 C = sqr(point[2].X - point[3].X) + sqr(point[2].Y - point[3].Y); - int64 D = sqr(point[3].X - point[0].X) + sqr(point[3].Y - point[0].Y); - if( A != C || B != D ) - continue; - int64 P = (point[0].Y - point[1].Y) * (point[1].Y - point[2].Y) + (point[0].X - point[1].X) * (point[1].X - point[2].X); - int64 Q = (point[1].Y - point[2].Y) * (point[2].Y - point[3].Y) + (point[1].X - point[2].X) * (point[2].X - point[3].X); - int64 R = (point[2].Y - point[3].Y) * (point[3].Y - point[0].Y) + (point[2].X - point[3].X) * (point[3].X - point[0].X); - int64 S = (point[3].Y - point[0].Y) * (point[0].Y - point[1].Y) + (point[3].X - point[0].X) * (point[0].X - point[1].X); - if( P == 0 && Q == 0 && R == 0 && S == 0 ) - return true; - }while( next_permutation(point, point + 4, comp) ); - - return false; -} +typedef pair punto; +#define x(p) p.first +#define y(p) p.second -bool isRhombus(Point point[]){ - do{ - int64 A = sqr(point[0].X - point[1].X) + sqr(point[0].Y - point[1].Y); - int64 B = sqr(point[1].X - point[2].X) + sqr(point[1].Y - point[2].Y); - int64 C = sqr(point[2].X - point[3].X) + sqr(point[2].Y - point[3].Y); - int64 D = sqr(point[3].X - point[0].X) + sqr(point[3].Y - point[0].Y); - if( A != B || B != C || C != D || D != A ) - continue; - int64 P = (point[0].Y - point[1].Y) * (point[1].Y - point[2].Y) + (point[0].X - point[1].X) * (point[1].X - point[2].X); - int64 Q = (point[1].Y - point[2].Y) * (point[2].Y - point[3].Y) + (point[1].X - point[2].X) * (point[2].X - point[3].X); - int64 R = (point[2].Y - point[3].Y) * (point[3].Y - point[0].Y) + (point[2].X - point[3].X) * (point[3].X - point[0].X); - int64 S = (point[3].Y - point[0].Y) * (point[0].Y - point[1].Y) + (point[3].X - point[0].X) * (point[0].X - point[1].X); - if( P == R && Q == S ) - return true; - }while( next_permutation(point, point + 4, comp) ); - - return false; +long long producto_cruz(punto a, punto b) +{ + return x(a) * y(b) - y(a) * x(b); } - -bool isParallelogram(Point point[]){ - do{ - int64 A = sqr(point[0].X - point[1].X) + sqr(point[0].Y - point[1].Y); - int64 B = sqr(point[1].X - point[2].X) + sqr(point[1].Y - point[2].Y); - int64 C = sqr(point[2].X - point[3].X) + sqr(point[2].Y - point[3].Y); - int64 D = sqr(point[3].X - point[0].X) + sqr(point[3].Y - point[0].Y); - if( A != C || B != D ) - continue; - int64 P = (point[0].Y - point[1].Y) * (point[1].Y - point[2].Y) + (point[0].X - point[1].X) * (point[1].X - point[2].X); - int64 Q = (point[1].Y - point[2].Y) * (point[2].Y - point[3].Y) + (point[1].X - point[2].X) * (point[2].X - point[3].X); - int64 R = (point[2].Y - point[3].Y) * (point[3].Y - point[0].Y) + (point[2].X - point[3].X) * (point[3].X - point[0].X); - int64 S = (point[3].Y - point[0].Y) * (point[0].Y - point[1].Y) + (point[3].X - point[0].X) * (point[0].X - point[1].X); - int64 U = (point[0].Y - point[1].Y) * (point[2].X - point[3].X); - int64 V = (point[0].X - point[1].X) * (point[2].Y - point[3].Y); - int64 W = (point[1].Y - point[2].Y) * (point[0].X - point[3].X); - int64 X = (point[1].X - point[2].X) * (point[0].Y - point[3].Y); - if( P != 0 && Q != 0 && R != 0 && S != 0 && U == V && W == X ) - return true; - }while( next_permutation(point, point + 4, comp) ); - - return false; +long long producto_punto(punto a, punto b) +{ + return x(a) * x(b) + y(a) * y(b); } - -bool isTrapezium(Point point[]){ - do{ - int64 P = (point[0].Y - point[1].Y) * (point[2].X - point[3].X); - int64 Q = (point[0].X - point[1].X) * (point[2].Y - point[3].Y); - int64 R = (point[1].Y - point[2].Y) * (point[0].X - point[3].X); - int64 S = (point[1].X - point[2].X) * (point[0].Y - point[3].Y); - if( (P == Q) || (R == S) ) - return true; - }while( next_permutation(point, point + 4, comp) ); - - return false; +long long norma(punto a) +{ + return (x(a) * x(a) + y(a) * y(a)); } - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - for(int t = 1; t <= T; t++){ - Point point[4]; - int i; - FOI(i, 0, 3){ - cin >> point[i].X >> point[i].Y; - point[i].I = i; +int main() +{ + int t = 0; + int r1, r2, r3, r4; + long long tama, tamb, tamc, tamd; + bool exito; + punto a, b, c, d; + punto v1, v2, v3, v4; + cin >> t; + for (int i = 0; i < t; i++) + { + scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d", &x(a), &y(a), &x(b), &y(b), &x(c), &y(c), &x(d), &y(d)); + int buscar[] = {1, 2, 3}; + vector puntos; + puntos.push_back(a); + puntos.push_back(b); + puntos.push_back(c); + puntos.push_back(d); + do + { + x(v1) = x(puntos[buscar[0]]) - x(a); + y(v1) = y(puntos[buscar[0]]) - y(a); + x(v2) = x(puntos[buscar[1]]) - x(puntos[buscar[0]]); + y(v2) = y(puntos[buscar[1]]) - y(puntos[buscar[0]]); + x(v3) = x(puntos[buscar[2]]) - x(puntos[buscar[1]]); + y(v3) = y(puntos[buscar[2]]) - y(puntos[buscar[1]]); + x(v4) = x(a) - x(puntos[buscar[2]]); + y(v4) = y(a) - y(puntos[buscar[2]]); + r1 = producto_cruz(v1, v2); + r2 = producto_cruz(v2, v3); + r3 = producto_cruz(v3, v4); + r4 = producto_cruz(v4, v1); + if ((r1 > 0 && r2 > 0 && r3 > 0 && r4 > 0) || (r1 < 0 && r2 < 0 && r3 < 0 && r4 < 0)) + { + exito = true; + } + else + { + exito = false; + } + } while (!exito && next_permutation(buscar, buscar + 3)); + tama = norma(v1); + tamb = norma(v2); + tamc = norma(v3); + tamd = norma(v4); + if (producto_punto(v1, v2) == 0 && producto_punto(v2, v3) == 0 && producto_punto(v3, v4) == 0 && producto_punto(v4, v1) == 0) + { + if (tama == tamb && tamb == tamc && tamc == tamd) + { + printf("Case %d: Square\n", i + 1); + } + else + { + printf("Case %d: Rectangle\n", i + 1); + } } - cout << "Case " << t << ": "; - if( isSquare(point) ) - cout << "Square\n"; - else if( isRectangle(point) ) - cout << "Rectangle\n"; - else if( isRhombus(point) ) - cout << "Rhombus\n"; - else if( isParallelogram(point) ) - cout << "Parallelogram\n"; - else if( isTrapezium(point) ) - cout << "Trapezium\n"; else - cout << "Ordinary Quadrilateral\n"; - + { + if (tama == tamb && tamb == tamc && tamc == tamd) + { + printf("Case %d: Rhombus\n", i + 1); + } + else + { + if (tama == tamc && tamb == tamd) + { + printf("Case %d: Parallelogram\n", i + 1); + } + else + { + if (producto_cruz(v1, v3) == 0 || producto_cruz(v2, v4) == 0) + { + printf("Case %d: Trapezium\n", i + 1); + } + else + { + printf("Case %d: Ordinary Quadrilateral\n", i + 1); + } + } + } + } } return 0; } diff --git a/11801.cpp b/11801.cpp new file mode 100644 index 0000000..9f89fbf --- /dev/null +++ b/11801.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define ALL(x) (x).begin(), (x).end() +#define SIZE(x) ((int)(x).size()) + +struct TOPIC +{ + long long x, a, b, c, d; + int read() + { + scanf("%lld%lld%lld%lld%lld", &x, &a, &b, &c, &d); + if (b * c < a * d) + { + swap(a, c); + swap(b, d); + } + return 0; + } + long long work(int mid) + { + long long i, j, k, ans; + ans = 1000000001LL; + for (i = 0; i <= 100; ++i) + { + k = (x * mid - d * i - 1) / b + 1; + ans = min(ans, i * c + k * a); + } + return ans; + } +} tp[1003]; +int main() +{ + long long i, j, m, n, T, TI, sum, now, ans; + long long l, r, mid; + scanf("%lld", &T); + for (TI = 1; TI <= T; ++TI) + { + scanf("%lld%lld", &n, &m); + for (i = 1; i <= n; ++i) + { + tp[i].read(); + } + l = 0; + r = m; + ans = 0; + while (l <= r) + { + mid = (l + r) >> 1; + sum = 0; + for (i = 1; i <= n; ++i) + { + sum += tp[i].work(mid); + } + if (sum <= m) + { + ans = max(ans, mid); + l = mid + 1; + } + else + { + r = mid - 1; + } + } + printf("Case %lld: ", TI); + printf("%lld\n", ans); + } + return 0; +} diff --git a/11802.cpp b/11802.cpp new file mode 100644 index 0000000..5d11761 --- /dev/null +++ b/11802.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define nMax 9000 + +int p[nMax]; +long long k, a[nMax]; + +void init() +{ + for (int i = 2; i < nMax; i++) + if (!a[i]) + { + p[k++] = i; + for (int j = i * i; j < nMax; j += i) + a[j] = 1; + } +} + +#define LL long long +#define mod 1000000007 +#define bug printf("Before Here!"); + +int i; +LL f(LL s) +{ + LL ret = 1LL; + for (int j = 0; j < i; j++) + { + ret *= (a[j] / s + 1); + ret %= mod; + } + return ret; +} + +LL n; +int main() +{ + int t, cas = 1; + init(); + scanf("%d", &t); + while (t--) + { + scanf("%lld%lld", &n, &k); + for (i = 0; i < 1000; i++) + { + LL m = n; + a[i] = 0LL; + while (m) + { + m /= p[i]; + a[i] += m; + } + if (a[i] < k) + break; + } + + printf("Case %d: %lld\n", cas++, ((f(k) - f(k + 1)) % mod + mod) % mod); + } + return 0; +} diff --git a/11803.cpp b/11803.cpp new file mode 100644 index 0000000..771d437 --- /dev/null +++ b/11803.cpp @@ -0,0 +1,215 @@ +#include + +using namespace std; + +const int N = 220; +const int oo = 0x7fffffff; +int w[N][N], x[N], y[N], px[N], py[N], sy[N], slack[N], par[N]; +int n[2]; +vector edge[2][N]; +int stamp, vis[N][N], f[N][N]; +void adjust(int v) +{ + sy[v] = py[v]; + if (px[sy[v]] != -2) + { + adjust(px[sy[v]]); + } +} +bool find(int v, int n) +{ + int i; + for (int i = 0; i < n; ++i) + { + if (py[i] == -1) + { + if (slack[i] > x[v] + y[i] - w[v][i]) + { + slack[i] = x[v] + y[i] - w[v][i]; + par[i] = v; + } + if (x[v] + y[i] == w[v][i]) + { + py[i] = v; + if (sy[i] == -1) + { + adjust(i); + return 1; + } + if (px[sy[i]] != -1) + { + continue; + } + px[sy[i]] = i; + if (find(sy[i], n)) + { + return 1; + } + } + } + } + return 0; +} +int km(int n0, int m0) +{ + int i, j, n, m; + n = max(n0, m0); + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + if (i >= n0 || j >= m0) + { + w[i][j] = 0; + } + } + } + /* + printf("\n"); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + printf("%d ", w[i][j]); + } + printf("\n"); + } + printf("\n"); + */ + for (i = 0; i < n; ++i) + { + sy[i] = -1, y[i] = 0; + } + for (i = 0; i < n; ++i) + { + x[i] = 0; + for (j = 0; j < n; ++j) + { + x[i] = max(x[i], w[i][j]); + } + } + bool flag; + for (i = 0; i < n; ++i) + { + for (j = 0; j < n; ++j) + { + px[j] = py[j] = -1, slack[j] = oo; + } + px[i] = -2; + if (find(i, n)) + { + continue; + } + flag = false; + for (; !flag;) + { + m = oo; + for (j = 0; j < n; ++j) + if (py[j] == -1) + { + m = min(m, slack[j]); + } + for (j = 0; j < n; ++j) + { + if (px[j] != -1) + { + x[j] -= m; + } + if (py[j] != -1) + { + y[j] += m; + } + else + { + slack[j] -= m; + } + } + for (j = 0; j < n; ++j) + { + if (py[j] == -1 && !slack[j]) + { + py[j] = par[j]; + if (sy[j] == -1) + { + adjust(j); + flag = true; + break; + } + px[sy[j]] = j; + if (find(sy[j], n)) + { + flag = true; + break; + } + } + } + } + } + int ans = 0; + for (i = 0; i < n; ++i) + { + ans += w[sy[i]][i]; + } + return ans; +} +int cal(int u, int v) +{ + //printf("%d %d\n",u,v); + if (vis[u][v] == stamp) + { + return f[u][v]; + } + vis[u][v] = stamp; + if (min(edge[0][u].size(), edge[1][v].size()) == 0) + { + f[u][v] = 1; + } + else + { + for (int i = 0; i < edge[0][u].size(); ++i) + { + int a = edge[0][u][i]; + for (int j = 0; j < edge[1][v].size(); ++j) + { + int b = edge[1][v][j]; + cal(a, b); + } + } + for (int i = 0; i < edge[0][u].size(); ++i) + { + int a = edge[0][u][i]; + for (int j = 0; j < edge[1][v].size(); ++j) + { + int b = edge[1][v][j]; + w[i][j] = cal(a, b); + } + } + f[u][v] = km(edge[0][u].size(), edge[1][v].size()) + 1; + } + return f[u][v]; +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + for (int i = 0; i < 2; ++i) + { + scanf("%d", &n[i]); + n[i]++; + for (int j = 0; j < n[i]; ++j) + { + edge[i][j].clear(); + } + for (int j = 1; j < n[i]; ++j) + { + int fa; + scanf("%d", &fa); + edge[i][fa].push_back(j); + } + } + ++stamp; + static int id = 0; + printf("Case %d: %d\n", ++id, (n[0] + n[1]) - 2 * cal(0, 0)); + } + return 0; +} diff --git a/11804.cpp b/11804.cpp index 0872e3b..a60a775 100644 --- a/11804.cpp +++ b/11804.cpp @@ -1,66 +1,112 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -struct Team{ - string name; - int att; - int def; -}; - -bool mainComp(Team A, Team B){ - if(A.att != B.att) - return (A.att > B.att); - else if(A.def != B.def) - return (A.def < B.def); - else - return (A.name < B.name); -} -bool supComp(Team A, Team B){ - return (A.name < B.name); -} -int main(){ - int test; - cin>>test; - for(int t=1; t<=test; t++){ - Team team[10]; - int i; - for(i=0; i<10; i++) - cin>>team[i].name>>team[i].att>>team[i].def; - sort(team, team+10, mainComp); - cout<<"Case "< + +using namespace std; + +#define num_play 10 + +typedef struct +{ + char name[50]; + int attack; + int defence; +} team; +team player[20], tem; +int main() +{ + int i, j, k, t, test_case; + while (scanf("%d ", &test_case) == 1) + { + for (t = 1; t <= test_case; t++) + { + for (i = 0; i < num_play; i++) + { + scanf("%s %d %d", &player[i].name, &player[i].attack, &player[i].defence); + } + /*sorting for divide in two group*/ + for (i = 0; i < num_play - 1; i++) + { + for (k = i + 1; k < num_play; k++) + { + if (player[i].attack < player[k].attack) + { + tem = player[i]; + player[i] = player[k]; + player[k] = tem; + } + else if (player[i].attack == player[k].attack) + { + if (player[i].defence > player[k].defence) + { + tem = player[i]; + player[i] = player[k]; + player[k] = tem; + } + else if (player[i].defence == player[k].defence) + { + if (strcmp(player[i].name, player[k].name) > 0) + { + tem = player[i]; + player[i] = player[k]; + player[k] = tem; + } + } + } + } + } + /*sorting for lexicography*/ + /*first half attacking*/ + for (i = 0; i < 4; i++) + { + for (k = i + 1; k < 5; k++) + { + if (strcmp(player[i].name, player[k].name) > 0) + { + tem = player[i]; + player[i] = player[k]; + player[k] = tem; + } + } + } + /*last half defending*/ + for (i = 5; i < 9; i++) + { + for (k = i + 1; k < 10; k++) + { + if (strcmp(player[i].name, player[k].name) > 0) + { + tem = player[i]; + player[i] = player[k]; + player[k] = tem; + } + } + } + /*printing attack*/ + printf("Case %d:\n", t); + printf("("); + for (i = 0; i < 5; i++) + { + if (i != 4) + { + printf("%s, ", player[i].name); + } + else if (i == 4) + { + printf("%s)\n", player[i].name); + } + } + /*printing defence*/ + printf("("); + for (i = 5; i < 10; i++) + { + if (i != 9) + { + printf("%s, ", player[i].name); + } + else if (i == 9) + { + printf("%s)\n", player[i].name); + } + } + } + } +} diff --git a/11805.cpp b/11805.cpp index 195dc79..6fc07fe 100644 --- a/11805.cpp +++ b/11805.cpp @@ -1,45 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - for(int t=1; t<=test; t++){ - int N, K, P; - cin>>N>>K>>P; - int val = (K+P)%N; - if(val==0) - val = N; - cout<<"Case "< + +using namespace std; + +int main() +{ + int t, n, k, p; + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> n >> k >> p; + while (p--) + { + k++; + if (k > n) + { + k = 1; + } + } + cout << "Case " << i + 1 << ": " << k << endl; + } + return 0; +} diff --git a/11806.cpp b/11806.cpp new file mode 100644 index 0000000..dc2a26a --- /dev/null +++ b/11806.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define MOD 1000007 +int M, N, K, C[410][410]; +void prep() +{ + memset(C, 0, sizeof(C)); + C[0][0] = 1; + for (int i = 1; i <= 400; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + { + C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; + } + } +} +void solve() +{ + int ans, A[5] = {0}; + for (int i = 1; i < 16; i++) + { + int cnt = 0, t = 0, n = N, m = M; + for (int j = 0; j < 4; j++) + if (i & 1 << j) + { + ++cnt; + if (j < 2) + { + --n; + } + else + { + --m; + } + } + if (K <= n * m) + { + t = C[n * m][K]; + A[cnt] = (A[cnt] + t) % MOD; + } + } + ans = A[1] - A[2] + A[3] - A[4]; + printf("%d\n", ((C[N * M][K] - ans) % MOD + MOD) % MOD); +} +int main() +{ + prep(); + int t; + scanf("%d", &t); + for (int tt = 1; tt <= t; tt++) + { + scanf("%d%d%d", &M, &N, &K); + printf("Case %d: ", tt); + if (K > N * M) + { + printf("0\n"); + } + else + { + solve(); + } + } + return 0; +} diff --git a/11809.cpp b/11809.cpp new file mode 100644 index 0000000..8c1fa65 --- /dev/null +++ b/11809.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +int main() +{ + char array[100]; + long long unsigned M, E, y, i; + double x, A, diff; + while (scanf("%s", array) != EOF) + { + for (i = 0; array[i]; i++) + { + if ((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')) + { + array[i] = ' '; + break; + } + } + sscanf(array, "%lf%llu", &x, &y); + if (fabs(x) > -EPS && fabs(x) < EPS && y == 0) + { + break; + } + A = log(x) + y * log(10); + for (M = 0; M < 10; M++) + { + E = (long long unsigned)(log(M + 2 + (A - log(pow(2, M + 1) - 1)) / log(2)) / log(2) + 0.5); + diff = A - (pow(2, E) - M - 2) * log(2) - log(pow(2, M + 1) - 1); + if (diff > -EPS && diff < EPS) + { + break; + } + } + printf("%llu %llu\n", M, E); + } + return 0; +} diff --git a/11813.cpp b/11813.cpp new file mode 100644 index 0000000..4c0148e --- /dev/null +++ b/11813.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#pragma warning(disable : 4786) + +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#define rep(i, n) for (i = 0; i < (n); i++) +#define i64 long long +//#define i64 __int64 +#define MAXN 100005 +#define MAXS 11 +typedef pair ii; +typedef vector vii; + +i64 INF; + +int n, m; +int ns; +int s[MAXS]; +vector> adj[MAXN]; +priority_queue> pq; +i64 d[MAXS][MAXN]; + +i64 dp[MAXS][(1 << MAXS)]; + +void dijkstra(int st, int dex) +{ + int i; + pair t; + rep(i, n) d[dex][i] = INF; + d[dex][st] = 0; + while (!pq.empty()) + { + pq.pop(); + } + pq.push(make_pair(st, 0)); + while (!pq.empty()) + { + t = pq.top(); + pq.pop(); + if (d[dex][t.first] >= t.second) + { + rep(i, adj[t.first].size()) + { + if (d[dex][t.first] + adj[t.first][i].second < d[dex][adj[t.first][i].first]) + { + d[dex][adj[t.first][i].first] = d[dex][t.first] + adj[t.first][i].second; + pq.push(make_pair(adj[t.first][i].first, d[dex][adj[t.first][i].first])); + } + } + } + } +} + +i64 go(int cur, int state) +{ + if (state == 0) + { + return d[cur][s[ns - 1]]; + } + if (dp[cur][state] != -1) + { + return dp[cur][state]; + } + i64 &ret = dp[cur][state]; + int i; + ret = INF; + rep(i, ns) if (state & (1 << i)) + { + ret = min(ret, go(i, state - (1 << i)) + d[cur][s[i]]); + } + return ret; +} + +int main() +{ + int T; + int u, v, d, i; + scanf(" %d", &T); + INF = (1 << 30); + INF *= INF; + while (T--) + { + scanf(" %d %d", &n, &m); + rep(i, n) adj[i].clear(); + rep(i, m) + { + scanf(" %d %d %d", &u, &v, &d); + adj[u].push_back(make_pair(v, d)); + adj[v].push_back(make_pair(u, d)); + } + scanf(" %d", &ns); + rep(i, ns) scanf(" %d", &s[i]); + s[ns++] = 0; + rep(i, ns) + { + dijkstra(s[i], i); + } + memset(dp, -1, sizeof(dp)); + printf("%lld\n", go(ns - 1, (1 << ns) - 1)); + } + return 0; +} diff --git a/11814.cpp b/11814.cpp new file mode 100644 index 0000000..f34f813 --- /dev/null +++ b/11814.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +typedef pair pi; +struct road +{ + int s; + int d; + int p; +}; +bool operator<(const road r1, const road r2) +{ + if (r1.s < r2.s) + { + return true; + } + if (r1.s > r2.s) + { + return false; + } + if (r1.d < r2.d) + { + return true; + } + if (r1.d > r2.d) + { + return false; + } + if (r1.p < r2.p) + { + return true; + } + if (r1.p > r2.p) + { + return false; + } + return false; +} +queue q; +bitset<220> inq[220]; +int bridge[220][220]; +int qs; +int deqs; +void addbridge(int s, int d, unsigned l) +{ + if (l % 2) + { + *((int *)5) = 0; + } + unsigned cur = bridge[s][d]; + if (cur <= l) + { + return; + } + bridge[s][d] = l; + q.push(pi(s, d)); + inq[s].set(d); + qs++; +} +set from[220]; +set to[220]; +map> top[220]; +int main() +{ + int NN; + scanf("%d", &NN); + while (NN--) + { + int N, M, Q; + scanf("%d %d %d", &N, &M, &Q); + for (int i = 0; i < N; i++) + { + from[i] = set(); + to[i] = set(); + top[i] = map>(); + } + memset(bridge, -1, sizeof(bridge)); + for (int i = 0; i < M; i++) + { + int x, y, z; + scanf("%d %d %d", &x, &y, &z); + // 0 && printf("R: %d %d %d\n", x, y, z); + road r; + x--; + y--; + r.s = x; + r.d = y; + r.p = z; + from[x].insert(r); + to[y].insert(r); + top[y][z].insert(r); + } + q = queue(); + for (int i = 0; i < N; i++) + { + set fromi = from[i]; + for (set::iterator it = fromi.begin(); it != fromi.end(); it++) + { + int p = it->p; + if (p < 0) + { + continue; + } + int s = it->d; + set froms = from[s]; + for (set::iterator it2 = froms.begin(); it2 != froms.end(); it2++) + { + if (it2->p == -p) + { + addbridge(i, it2->d, 2); + } + } + } + } + while (q.size() > 0) + { + pi sd = q.front(); + int s = sd.first; + int d = sd.second; + q.pop(); + if (!inq[s].test(d)) + { + continue; + } + inq[s].reset(d); + deqs++; + set fromd = from[d]; + for (set::iterator it = fromd.begin(); it != fromd.end(); it++) + { + int p = it->p; + if (p > 0) + { + continue; + } + int dd = it->d; + set topsp = top[s][-p]; + for (set::iterator it2 = topsp.begin(); it2 != topsp.end(); it2++) + { + int ss = it2->s; + addbridge(ss, dd, bridge[s][d] + 2); + } + } + for (int dd = 0; dd < N; dd++) + { + if (bridge[d][dd] != -1) + { + addbridge(s, dd, bridge[s][d] + bridge[d][dd]); + } + if (bridge[dd][s] != -1) + { + addbridge(dd, d, bridge[s][d] + bridge[dd][s]); + } + } + } + for (int i = 0; i < Q; i++) + { + int x, y; + scanf("%d %d\n", &x, &y); + // 0 && printf("Q: %d %d\n", x, y); + x--; + y--; + if (bridge[x][y] == -1) + { + puts("impossible"); + } + else + { + printf("%d\n", bridge[x][y]); + } + } + // 0 && printf("qs: %d deqs: %d\n", qs, deqs); + } + return 0; +} diff --git a/11815.cpp b/11815.cpp new file mode 100644 index 0000000..f840298 --- /dev/null +++ b/11815.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +bitset<1010> pres[1010]; +bitset<1010> use[1010]; +bitset<1010> bef[1010]; +bitset<1010> aft[1010]; +set preds[1010]; +char buf[100000]; +bitset<1010> seen; +vector po; +int endofedge[1010]; +int N, M, I; +void printset(char *msg, bitset<1010> s) +{ + bool first = true; + printf(msg); + for (int i = 0; i < I; i++) + { + if (s.test(i)) + { + if (!first) + { + printf(" "); + } + printf("%d", i); + first = false; + } + } + puts(""); +} +bitset<1010> readset() +{ + bitset<1010> ret = bitset<1010>(); + gets(buf); + char *tok = strtok(buf, " "); + while (tok) + { + int i; + if (1 == sscanf(tok, "%d", &i)) + { + ret.set(i); + } + tok = strtok(0, " "); + } + return ret; +} +void dfs(int i) +{ + if (seen[i]) + { + return; + } + seen.set(i); + for (set::iterator it = preds[i].begin(); it != preds[i].end(); it++) + { + dfs(*it); + } + po.push_back(i); +} +int main() +{ + int NN; + gets(buf); + sscanf(buf, "%d ", &NN); + while (NN--) + { + gets(buf); + sscanf(buf, "%d %d %d ", &N, &M, &I); + for (int i = 0; i < N; i++) + { + use[i] = readset(); + pres[i] = ~readset(); + bef[i] = bitset<1010>(); + aft[i] = bitset<1010>(); + preds[i] = set(); + } + for (int i = 0; i < M; i++) + { + int x, y; + scanf("%d %d ", &x, &y); + preds[y].insert(x); + endofedge[i] = y; + } + seen = bitset<1010>(); + po = vector(); + for (int i = 0; i < N; i++) + { + dfs(i); + } + bool change = true; + while (change) + { + change = false; + for (vector::reverse_iterator it = po.rbegin(); it != po.rend(); it++) + { + bitset<1010> old = bef[*it]; + bef[*it] = (aft[*it] & pres[*it]) | use[*it]; + if (bef[*it] != old) + { + change = true; + } + for (set::iterator it2 = preds[*it].begin(); it2 != preds[*it].end(); it2++) + { + old = aft[*it2]; + aft[*it2] |= bef[*it]; + if (aft[*it2] != old) + { + change = true; + } + } + } + } + for (int i = 0; i < M; i++) + { + printset("", bef[endofedge[i]]); + } + /* + for(int i = 0; i < N; i++) { + printf("%d:\n", i); + printset("use " , use[i]); + printset("pres ", pres[i]); + printset("bef ", bef[i]); + printset("aft ", aft[i]); + } + */ + } + return 0; +} diff --git a/11816.cpp b/11816.cpp index 49b1324..b6bfac1 100644 --- a/11816.cpp +++ b/11816.cpp @@ -1,65 +1,84 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -struct Item{ - string name; - double PST, GST, HST; -}; - -int main(){ - int test; - cin>>test; - while(test--){ - int N, M, i, j; - cin>>N>>M; - Item item[N]; - for(i=0; i + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define myabs(x) ((x) < 0 ? -x : x) +typedef long long ll; + +ll getdif(ll pay, ll tax) +{ + ll tmp = pay * tax; + if (tmp % 10000 < 5000) + { + return tmp / 10000; + } + else + { + return tmp / 10000 + 1; + } +} + +int main() +{ + int te; + cin >> te; + while (te--) + { + int n, m; + char dummy; + cin >> n >> m; + map tax[3]; + rep(i, n) + { + string name; + ll tp; + string tmp; + cin >> name; + rep(j, 3) + { + cin >> tp >> dummy; + tp = tp * 100; + if (dummy == '.') + { + cin >> tmp; + tp += (tmp[0] - '0') * 10; + if (tmp.size() == 3) + { + tp += tmp[1] - '0'; + } + } + tax[j][name] = tp; + } + } + ll dif = 0; + rep(i, m) + { + string name; + ll dol, pay; + string cent; + cin >> name >> dummy >> dol >> dummy >> cent; + if (cent.size() == 2) + { + pay = dol * 100 + (cent[0] - '0') * 10 + cent[1] - '0'; + } + else if (cent.size() == 1) + { + pay = dol * 100 + (cent[0] - '0') * 10; + } + dif -= getdif(pay, tax[0][name]); + dif -= getdif(pay, tax[1][name]); + dif += getdif(pay, tax[2][name]); + } + if (dif < 0) + { + printf("-%lld.%02lld\n", myabs(dif / 100), myabs(dif % 100)); + } + else + { + printf("%lld.%02lld\n", dif / 100, myabs(dif % 100)); + } + } + return 0; +} diff --git a/11817.cpp b/11817.cpp new file mode 100644 index 0000000..799e0d9 --- /dev/null +++ b/11817.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +#define MID(x, y) ((x + y) >> 1) +#define L(x) (x << 1) +#define R(x) (x << 1 | 1) +#define BUG puts("here!!!") +#define STOP system("pause") + +const double pi = acos(-1.0); +const double R = 6371009; +double angle_3d(double lng1, double lat1, double lng2, double lat2) +{ + double dlng = fabs(lng1 - lng2) * pi / 180; + while (dlng >= 2 * pi) + { + dlng -= pi * 2; + } + if (dlng > pi) + { + dlng = 2 * pi - dlng; + } + lat1 *= pi / 180; + lat2 *= pi / 180; + return acos(cos(lat1) * cos(lat2) * cos(dlng) + sin(lat1) * sin(lat2)); +} + +int main() +{ + int ncases; + double la, lb, wa, wb; + scanf("%d", &ncases); + while (ncases--) + { + scanf("%lf%lf%lf%lf", &la, &wa, &lb, &wb); + double ang = angle_3d(wa, la, wb, lb); + double l1 = ang * R; + double l2 = R * sqrt(2 - 2 * cos(ang)); + printf("%.0lf\n", l1 - l2); + } + return 0; +} diff --git a/11818.cpp b/11818.cpp new file mode 100644 index 0000000..3e4e473 --- /dev/null +++ b/11818.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +#define LL long long + +const int MAX = 1 << 12; +const int nMAX = 20; +const int mMAX = 10000; +const int INF = ((1 << 31) - 1); +const int MOD = 1000000007; + +int G[nMAX][nMAX]; +int vis[nMAX][2], f[nMAX]; +int dp[10][10][MAX]; +int mark[4][4][4][4]; +int s, t; +void init() +{ + vis[0][0] = 1; + vis[0][1] = 2; + vis[1][0] = 2; + vis[1][1] = 3; + vis[2][0] = 1; + vis[2][1] = 4; + vis[3][0] = 2; + vis[3][1] = 5; + vis[4][0] = 3; + vis[4][1] = 6; + vis[5][0] = 4; + vis[5][1] = 5; + vis[6][0] = 5; + vis[6][1] = 6; + vis[7][0] = 4; + vis[7][1] = 7; + vis[8][0] = 5; + vis[8][1] = 8; + vis[9][0] = 6; + vis[9][1] = 9; + vis[10][0] = 7; + vis[10][1] = 8; + vis[11][0] = 8; + vis[11][1] = 9; + mark[1][0][1][1] = 0; + mark[1][1][1][0] = 0; + mark[2][0][2][1] = 1; + mark[2][1][2][0] = 1; + mark[0][1][1][1] = 2; + mark[1][1][0][1] = 2; + mark[1][1][2][1] = 3; + mark[2][1][1][1] = 3; + mark[2][1][3][1] = 4; + mark[3][1][2][1] = 4; + mark[1][1][1][2] = 5; + mark[1][2][1][1] = 5; + mark[2][1][2][2] = 6; + mark[2][2][2][1] = 6; + mark[0][2][1][2] = 7; + mark[1][2][0][2] = 7; + mark[1][2][2][2] = 8; + mark[2][2][1][2] = 8; + mark[2][2][3][2] = 9; + mark[3][2][2][2] = 9; + mark[1][2][1][3] = 10; + mark[1][3][1][2] = 10; + mark[2][2][2][3] = 11; + mark[2][3][2][2] = 11; + for (int i = 0; i < 12; i++) + { + f[i] = (1 << i); + } +} +bool floy(int s, int t) +{ + for (int k = 1; k <= 9; k++) + for (int i = 1; i <= 9; i++) + for (int j = 1; j <= 9; j++) + if (G[i][k] && G[k][j]) + { + G[i][j] = 1; + } + if (G[s][t]) + { + return 1; + } + else + { + return 0; + } +} +bool dfs(int x) +{ + /*cout<(x)<<" "< + +using namespace std; + +typedef long long LL; + +LL x, y; +int N; +char str[100]; + +int sgn(LL t) +{ + return t < 0 ? -1 : t > 0; +} + +int Get(int st, char end, LL &ret) +{ + int sgn = 1; + if (str[st] == '-' || str[st] == '+') + { + if (str[st] == '-') + { + sgn = -1; + } + st++; + } + ret = 0; + while (str[st] != end) + { + ret = ret * 10 + str[st++] - '0'; + } + ret *= sgn; + return st; +} + +int main() +{ + while (cin >> x >> y >> N, N) + { + int cnt = 0; + LL a = 0, b = 0, c = 0, u, v; + for (int i = 0; i < N; i++) + { + scanf("%lld %lld %s", &u, &v, str); + // cout << str << endl; + u -= x; + v -= y; + int pos = Get(0, 'x', a); + pos = Get(pos + 1, 'y', b); + Get(pos + 1, '=', c); + // cout << a << " " << b << " " << c << endl; + // if(c) + // { + // if(sgn(u*y - v*x) == sgn(c)) + // { + // cnt ++; + // } + // } + // else + { + if (sgn(-u * b + v * a) == 1) + { + cnt++; + } + } + // cout << cnt << " "; + } + cout << cnt * (N - cnt) << endl; + } + return 0; +} diff --git a/1182.cpp b/1182.cpp new file mode 100644 index 0000000..b48fbc8 --- /dev/null +++ b/1182.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +void inv(char s[], int n) +{ + for (int i = 0, j = n - 1; i < j; i++, j--) + swap(s[i], s[j]); +} +int dp[64][64][2][2]; +int main() +{ + int testcase; + char s1[64], s2[64]; + scanf("%d", &testcase); + while (getchar() != '\n') + ; + while (testcase--) + { + gets(s1); + gets(s2); + int n1 = strlen(s1), n2 = strlen(s2); + inv(s1, n1), inv(s2, n2); + for (int i = 0; i <= n1; i++) + { + for (int j = 0; j <= n2; j++) + { + dp[i][j][0][0] = dp[i][j][0][1] = -0x3f3f3f3f; + dp[i][j][1][0] = dp[i][j][1][1] = -0x3f3f3f3f; + } + } + dp[0][0][0][1] = 0; + dp[0][0][1][1] = 0; + int ret = -0x3f3f3f3f; + for (int i = 0; i <= n1; i++) + { + for (int j = 0; j <= n2; j++) + { + int v = max(dp[i][j][0][0], dp[i][j][0][1]); + v = max(v, max(dp[i][j][1][0], dp[i][j][1][1])); + dp[i + 1][j + 1][0][1] = max(dp[i + 1][j + 1][0][1], v); + dp[i + 1][j + 1][1][1] = max(dp[i + 1][j + 1][1][1], v); + if (s1[i] == s2[j]) + { + dp[i + 1][j + 1][0][1] = max(dp[i + 1][j + 1][0][1], v + 2); + dp[i + 1][j + 1][1][1] = max(dp[i + 1][j + 1][1][1], v + 2); + } + dp[i + 1][j][0][0] = max(dp[i + 1][j][0][0], dp[i][j][0][0]); + dp[i + 1][j][0][0] = max(dp[i + 1][j][0][0], dp[i][j][1][0] - 1); + dp[i + 1][j][0][0] = max(dp[i + 1][j][0][0], dp[i][j][0][1] - 1); + dp[i + 1][j][0][0] = max(dp[i + 1][j][0][0], dp[i][j][1][1] - 1); + + dp[i][j + 1][1][0] = max(dp[i][j + 1][1][0], dp[i][j][1][0]); + dp[i][j + 1][1][0] = max(dp[i][j + 1][1][0], dp[i][j][0][0] - 1); + dp[i][j + 1][1][0] = max(dp[i][j + 1][1][0], dp[i][j][0][1] - 1); + dp[i][j + 1][1][0] = max(dp[i][j + 1][1][0], dp[i][j][1][1] - 1); + if (j == n2 || i == n1) + { + ret = max(ret, max(dp[i][j][0][0], dp[i][j][0][1])); + ret = max(ret, max(dp[i][j][1][0], dp[i][j][1][1])); + } + } + } + printf("%d\n", ret); + } + return 0; +} +/* +2 +AADDEFGGHC +ADCDEGH +ERTTHCBYNQC +BEARTBCHQYN + +3 +ABC +ABC +ABC +AC +BAC +AC + +*/ diff --git a/11820.cpp b/11820.cpp new file mode 100644 index 0000000..4b0b662 --- /dev/null +++ b/11820.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +#define MaxS 200010 +#define MaxN 10010 +#define MaxL 510 +#define MaxM 1000010 + +char s[1010]; +int child[MaxS][52], suffix[MaxS], end_[MaxS], q[MaxS], len[MaxN], dp[MaxL], sol[MaxL], rec[MaxL], g[MaxM], nxt[MaxM], head[MaxL], N, n, nodes, top; + +int Code(char ch) +{ + if (ch >= 'a' && ch <= 'z') + { + return ch - 'a'; + } + return ch - 'A' + 26; +} + +void Build() +{ + memset(end_, -1, sizeof(end_)); + memset(child, 0, sizeof(child)); + nodes = 2; + for (n = 0; gets(s); ++n) + { + if (!s[0]) + { + return; + } + len[n] = strlen(s); + int p = 1; + for (int i = len[n] - 1; i >= 0; --i) + { + int ch = Code(s[i]); + if (!child[p][ch]) + { + child[p][ch] = nodes++; + } + p = child[p][ch]; + } + end_[p] = n; + } +} + +void AC() +{ + int ql = 0, qr = 0; + for (int i = 0; i < 52; ++i) + { + if (child[1][i]) + { + q[qr++] = child[1][i]; + suffix[child[1][i]] = 1; + } + else + { + child[1][i] = 1; + } + } + while (ql < qr) + { + int u = q[ql++]; + for (int i = 0; i < 52; ++i) + { + if (child[u][i]) + { + q[qr++] = child[u][i]; + suffix[child[u][i]] = child[suffix[u]][i]; + } + else + { + child[u][i] = child[suffix[u]][i]; + } + } + } +} + +void addEdge(int a, int b) +{ + g[top] = b, nxt[top] = head[a], head[a] = top++; +} + +void dfs(int dep, int u) +{ + if (dep == N) + { + for (int i = 0; i < N - 1; ++i) + { + for (int j = rec[i]; j < rec[i + 1]; ++j) + { + putchar(s[j]); + } + putchar(' '); + } + for (int j = rec[N - 1]; j < u; ++j) + { + putchar(s[j]); + } + puts(""); + return; + } + for (int i = head[u]; i; i = nxt[i]) + { + rec[dep] = u; + dfs(dep + 1, g[i]); + } +} + +void solve() +{ + while (gets(s)) + { + if (!s[0]) + { + return; + } + int l = strlen(s), p = 1; + memset(head, 0, sizeof(head)); + top = 1; + dp[l] = 0; + sol[l] = 1; + for (int i = l - 1; i >= 0; --i) + { + p = child[p][Code(s[i])]; + dp[i] = dp[i + 1] + 1; + sol[i] = 0; + for (int q = p; q != 1; q = suffix[q]) + { + if (end_[q] != -1 && i + len[end_[q]] <= l && dp[i + len[end_[q]]] + 1 < dp[i]) + { + dp[i] = dp[i + len[end_[q]]] + 1; + } + } + for (int q = p; q != 1; q = suffix[q]) + { + if (end_[q] != -1 && i + len[end_[q]] <= l && dp[i + len[end_[q]]] + 1 == dp[i]) + { + sol[i] += sol[i + len[end_[q]]]; + addEdge(i, i + len[end_[q]]); + } + } + if (dp[i] == dp[i + 1] + 1) + { + sol[i] += sol[i + 1]; + addEdge(i, i + 1); + } + } + printf("%d %d\n", sol[0], dp[0]); + N = dp[0]; + dfs(0, 0); + } +} + +int main() +{ + Build(); + AC(); + solve(); + return 0; +} diff --git a/11821.cpp b/11821.cpp new file mode 100644 index 0000000..39497fe --- /dev/null +++ b/11821.cpp @@ -0,0 +1,173 @@ +#include + +using namespace std; + +typedef struct +{ + int digit[85]; + int sign; +} Num; + +Num a[110]; +int n; + +void Enter() +{ + char s[35]; + int i, j, k, len, mid; + n = 0; + while (gets(s)) + { + len = strlen(s); + if (len == 1 && s[0] == '0') + break; + n++; + if (s[0] == '-') + a[n].sign = -1; + else + a[n].sign = 1; + + mid = 0; + while (mid < len && s[mid] != '.') + mid++; + int temp; + if (mid < len) + temp = 40 - (len - mid - 1); + else + temp = 40; + for (i = 1; i <= temp; i++) + a[n].digit[i] = 0; + for (j = len - 1; j >= mid + 1; i++, j--) + a[n].digit[i] = s[j] - 48; + + if (a[n].sign == -1) + k = 1; + else + k = 0; + for (j = mid - 1; j >= k; i++, j--) + a[n].digit[i] = s[j] - 48; + temp = 40 - mid; + for (; i <= 80; i++) + a[n].digit[i] = 0; + } +} + +void Print() +{ + int i, j; + for (i = 1; i <= n; i++) + { + for (j = 80; j >= 1; j--) + cout << a[i].digit[j]; + cout << endl; + } +} + +int SS(Num a, Num b) +{ + for (int i = 80; i >= 1; i--) + if (a.digit[i] > b.digit[i]) + return 1; + else if (a.digit[i] < b.digit[i]) + return 0; + return -1; +} + +void Go() +{ + Num G, temp; + int i, j, k, m, du; + + G = a[1]; + for (k = 2; k <= n; k++) + { + int sign = a[k].sign * G.sign; + if (sign > 0) + { + du = 0; + for (i = 1; i <= 80; i++) + { + m = G.digit[i] + a[k].digit[i] + du; + G.digit[i] = m % 10; + du = m / 10; + } + } + else + { + int big = SS(G, a[k]); + if (big == 1) + { + du = 0; + for (i = 1; i <= 80; i++) + { + a[k].digit[i] += du; + G.digit[i] = G.digit[i] - a[k].digit[i]; + if (G.digit[i] < 0) + { + G.digit[i] += 10; + du = 1; + } + else + du = 0; + } + } + else if (big == 0) + { + du = 0; + for (i = 1; i <= 80; i++) + { + G.digit[i] += du; + a[k].digit[i] = a[k].digit[i] - G.digit[i]; + if (a[k].digit[i] < 0) + { + a[k].digit[i] += 10; + du = 1; + } + else + du = 0; + } + G = a[k]; + } + else + { + for (i = 1; i <= 80; i++) + G.digit[i] = 0; + G.sign = 1; + } + } + } + + if (G.sign < 0) + cout << "-"; + while (G.digit[i] == 0 && i > 41) + i--; + for (; i >= 41; i--) + cout << G.digit[i]; + k = 1; + while (G.digit[k] == 0 && k != 41) + k++; + if (k != 41) + { + cout << "."; + for (; i >= k; i--) + cout << G.digit[i]; + } + cout << endl; +} + +main() +{ + // freopen("dd.inp","r",stdin); + // freopen("dd.out","w",stdout); + + int kase; + cin >> kase; + getchar(); + while (kase--) + { + Enter(); + // Print(); + Go(); + // cout << endl; + } +} diff --git a/11823.cpp b/11823.cpp new file mode 100644 index 0000000..1f46098 --- /dev/null +++ b/11823.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define MAXN 700 +#define MAXM 100000 +#define INF 0x3f3f3f3f + +struct Edge +{ + int u, v, e, c; + Edge *link, *rev; +} edge[MAXM], *adj[MAXN], *prev_[MAXN]; + +int totE; +int n, s, t; +int dis[MAXN], flow[MAXN], q[MAXN]; +bool inQ[MAXN]; + +void addEdge(int u, int v, int e, int c) +{ + Edge *p1 = &edge[totE++], *p2 = &edge[totE++]; + p1->u = u; + p1->v = v; + p1->e = e; + p1->c = c; + p1->link = adj[u]; + adj[u] = p1; + p1->rev = p2; + p2->u = v; + p2->v = u; + p2->e = 0; + p2->c = -c; + p2->link = adj[v]; + adj[v] = p2; + p2->rev = p1; +} + +bool spfa() +{ + memset(dis, INF, sizeof(dis)); + memset(inQ, false, sizeof(inQ)); + dis[s] = 0; + inQ[s] = true; + flow[s] = 1; + int l = 0, r = 0; + q[r++] = s; + while (l != r) + { + int u = q[l++]; + inQ[u] = false; + if (l == MAXN) + { + l = 0; + } + Edge *p = adj[u]; + while (p) + { + if (p->e && dis[u] + p->c < dis[p->v]) + { + dis[p->v] = dis[u] + p->c; + prev_[p->v] = p; + flow[p->v] = flow[u] < p->e ? flow[u] : p->e; + if (!inQ[p->v]) + { + inQ[p->v] = true; + q[r++] = p->v; + if (r == MAXN) + { + r = 0; + } + } + } + p = p->link; + } + } + return dis[t] != INF; +} + +int minCostMaxFlow() +{ + int res = 0; + for (int times = 0; times < 2; ++times) + { + if (!spfa()) + { + break; + } + int u = t; + while (u != s) + { + res += prev_[u]->c; + prev_[u]->e -= 1; + prev_[u]->rev->e += 1; + u = prev_[u]->u; + } + } + return res; +} + +int main() +{ + int cases; + scanf("%d", &cases); + for (int caseT = 1; caseT <= cases; ++caseT) + { + int N, M; + scanf("%d%d", &N, &M); + n = N * 2 + 2, s = 0, t = n - 1; + totE = 0; + memset(adj, NULL, sizeof(adj)); + for (int i = 0; i < M; ++i) + { + int u, v; + scanf("%d%d", &u, &v); + addEdge(N + u, v, 1, 0); + } + for (int i = 1; i <= N; ++i) + { + addEdge(s, i, 1, 0); + addEdge(i, N + i, 1, -1); + addEdge(N + i, t, 1, 0); + } + int res = -minCostMaxFlow(); + printf("%d\n", res); + } + return 0; +} diff --git a/11824.cpp b/11824.cpp index 2d2d6dd..bd5e085 100644 --- a/11824.cpp +++ b/11824.cpp @@ -1,58 +1,44 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int64 temp, n=0, i=0; - int64 land[40]; - while(true){ - cin>>temp; - if(temp == 0) - break; - land[n++] = temp; - } - sort(land, land+n); - int64 gtot=0; - for(i=n-1; i>=0; i--){ - gtot += (int64) ( 2 * pow(land[i], (double)(n-i)) ); - if(gtot > 5000000) - break; - } - if(gtot < 5000000) - cout< + +using namespace std; + +int main() +{ + int t; + unsigned long long result; + int price, counter; + int lands[50]; + cin >> t; + while (t--) + { + counter = 0; + result = 0; + while (cin >> price) + { + if (price == 0) + { + break; + } + lands[counter] = price; + counter++; + } + sort(lands, lands + counter, greater()); + for (int i = 0; i < counter; i++) + { + result += 2 * pow(lands[i], i + 1); + if (result > 5000000) + { + break; + } + } + if (result > 5000000) + { + cout << "Too expensive" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/11825.cpp b/11825.cpp new file mode 100644 index 0000000..96add45 --- /dev/null +++ b/11825.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back +const int N = 16; +vector edge[N]; +int dp[(1 << N)]; +int can[(1 << N)]; +bool cancover(int n, int now) +{ + int vis = now; + rep(i, n) + { + if (((1 << i) & now) == 0) + { + continue; + } + rep(j, edge[i].size()) vis |= (1 << edge[i][j]); + } + return vis == ((1 << n) - 1); +} + +int solve(int n, int now) +{ + if (now == 0) + { + return 0; + } + int &ret = dp[now]; + if (ret == -1) + { + ret = 0; + int sub = now; + do + { + if (can[sub]) + { + ret = max(ret, solve(n, now ^ sub) + 1); + } + sub = (sub - 1) & now; + } while (sub != now); + } + return ret; +} + +main() +{ + int n, tc = 1; + while (cin >> n && n) + { + rep(i, n) edge[i].clear(); + rep(i, (1 << n)) dp[i] = -1; + rep(i, n) + { + int m, t; + cin >> m; + rep(j, m) cin >> t, edge[i].pb(t); + } + rep(i, (1 << n)) + { + can[i] = cancover(n, i); + } + int ans = solve(n, (1 << n) - 1); + cout << "Case " << tc++ << ": " << ans << endl; + } + return 0; +} diff --git a/11827.cpp b/11827.cpp index a303519..e56ac7b 100644 --- a/11827.cpp +++ b/11827.cpp @@ -1,80 +1,87 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int64 gcd(int64 a, int64 b){ - if(b==0) - return a; - else - return gcd (b, a%b); -} - -int main(){ - int test; - scanf("%d\n", &test); - while(test--){ - int64 n=0, i=0, j=0,z=34; - int64 num[100]={0}; - string str; - getline(cin, str); - int64 temp=0, len=str.length(); - while(i < len){ - if(isdigit(str[i])) - temp = temp*10 + (int)(str[i]-'0'); - else{ - if(temp > 0){ - num[n++] = temp; - temp = 0; - } - } - //cout<= lgcd) ? ggcd : lgcd; - } - } - cout< 0) - // cout< + +using namespace std; + +int gcd2(int a, int b) +{ + while (b) + { + a %= b; + swap(a, b); + } + return a; +} + +int gcd(int a, int b) +{ + int shift; + int diff; + if (a == 0 || b == 0) + { + return a | b; + } + for (shift = 0; ((a | b) & 1) == 0; shift++) + { + a >>= 1; + b >>= 1; + } + while ((a & 1) == 0) + { + a >>= 1; + } + do + { + while ((b & 1) == 0) + { + b >>= 1; + } + if (a < b) + { + b -= a; + } + else + { + diff = a - b; + a = b; + b = diff; + } + b >>= 1; + } while (b != 0); + return a << shift; +} + +int main() +{ + int n, m; + int tmp; + int best; + int nums[110]; + string input; + cin >> n; + getline(cin, input);// \n + while (n--) + { + m = 0; + best = 0; + stringstream ss; + getline(cin, input); + ss << input; + while (ss >> nums[m]) + { + m++; + } + for (int i = 0; i < m - 1; i++) + { + for (int j = i + 1; j < m; j++) + { + tmp = gcd(nums[i], nums[j]); + if (tmp > best) + { + best = tmp; + } + } + } + cout << best << endl; + } + return 0; +} diff --git a/11828.cpp b/11828.cpp new file mode 100644 index 0000000..f05fd4b --- /dev/null +++ b/11828.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef long long ll; +const ll mod = 1000000000; +const int N = 1010; +ll p2[N], p24[N], p25[N]; +ll comb[N][N] = {0}, sum[N][N] = {0}; + +void computepow(int a, ll *b) +{ + b[0] = 1; + REP(i, 1, N) + b[i] = (b[i - 1] * a) % mod; +} + +void precalc() +{ + computepow(2, p2); + computepow(24, p24); + computepow(25, p25); + comb[0][0] = 1; + REP(i, 1, N) + { + comb[i][0] = 1; + REP(j, 1, i) + comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod; + comb[i][i] = 1; + } + sum[0][0] = 1; + REP(i, 1, N) + { + sum[i][0] = 1; + REP(j, 1, i + 1) + { + sum[i][j] = (sum[i][j - 1] + comb[i][j] * p25[j]) % mod; + } + } +} + +ll solve(int n, int m, int k) +{ + ll ret = 0; + for (int i = 0; i <= m; i++) + { + if (i + 2 * (m - i) <= k) + { + int can = min(n, (k - (i + 2 * (m - i))) / 2); + //cout << n <<" " << can << " " + //<< sum[n][can] <<" " << comb[m][i] <<" " << p2[i] << + //" " << p24[m-i] << endl; + //ret+=sum[n][can]*comb[m][i]*p2[i]*p24[m-i]; + ll tmp = (sum[n][can] * comb[m][i]) % mod; + tmp = (tmp * p2[i]) % mod; + tmp = (tmp * p24[m - i]) % mod; + ret = (ret + tmp) % mod; + } + } + return ret; +} + +main() +{ + precalc(); + int te, len, k; + cin >> te; + rep(tc, te) + { + cin >> len >> k; + string a; + cin >> a; + int n = 0, m = 0; + for (int i = 0; i < a.size() / 2; i++) + { + //cout << a[i] <<" " << a[a.size()-i-1] << endl; + if (a[i] == a[a.size() - i - 1]) + { + n++; + } + else + { + m++; + } + } + ll ans = solve(n, m, k); + if (a.size() % 2 == 1) + { + ans = (ans + 25 * solve(n, m, k - 1)) % mod; + } + cout << "Case " << tc + 1 << ": " << ans << endl; + } + return 0; +} diff --git a/11829.cpp b/11829.cpp new file mode 100644 index 0000000..b30d212 --- /dev/null +++ b/11829.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + double aa = 10000; + double r = 26405.593003588347000; + double a; + while (scanf("%lf", &a) != EOF) + { + if (a == -1) + { + break; + } + printf("%.10lf\n", a * r / aa); + } + return 0; +} diff --git a/11830.cpp b/11830.cpp index 2be7ad5..2321645 100644 --- a/11830.cpp +++ b/11830.cpp @@ -1,61 +1,40 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int i=0; - char ch, str[110]; - cin>>ch>>str; - if(ch=='0' && strlen(str)==1 && str[0]=='0') - break; - int len = strlen(str); - for(i=0; i= len) - break; - } - for( ; i + +using namespace std; + +int main() +{ + char n; + bool first; + string contract; + while (cin >> n >> contract) + { + if (n == '0' && contract == "0") + { + break; + } + first = true; + for (int i = 0, sz = contract.size(); i < sz; i++) + { + if (contract[i] == n) + { + continue; + } + if (contract[i] == '0') + { + if (first) + { + continue; + } + } + cout << contract[i]; + first = false; + } + if (first) + { + cout << 0; + } + cout << endl; + } + return 0; +} diff --git a/11831.cpp b/11831.cpp index a451c20..41803a6 100644 --- a/11831.cpp +++ b/11831.cpp @@ -1,132 +1,149 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; -struct Robot{ - int X; - int Y; - char dir; -}; - -int main(){ - while(true){ - int I, J, S; - int i=0, j=0, sticker=0; - cin>>I>>J>>S; - if(I==0 && J==0 && S==0) - break; - string mat[I], path; - for(i=0; i>mat[i]; - cin>>path; - Robot robo; - bool stat=true; - for(i=0; i + +using namespace std; + +char field[100][100]; +enum direction +{ + NORTH, + SOUTH, + EAST, + WEST +}; + +struct robot +{ + int row; + int col; + direction dir; +}; + +direction char2direction(char dir) +{ + switch (dir) + { + case 'N': + return NORTH; + case 'S': + return SOUTH; + case 'L': + return EAST; + case 'O': + return WEST; + } +} + +direction turn_right(direction dir) +{ + switch (dir) + { + case NORTH: + return EAST; + case SOUTH: + return WEST; + case EAST: + return SOUTH; + case WEST: + return NORTH; + } +} + +direction turn_left(direction dir) +{ + switch (dir) + { + case NORTH: + return WEST; + case SOUTH: + return EAST; + case WEST: + return SOUTH; + case EAST: + return NORTH; + } +} + +int move_forward(robot &r, int n, int m) +{ + int result = 0; + switch (r.dir) + { + case NORTH: + if (r.row > 0 && field[r.row - 1][r.col] != '#') + { + r.row--; + } + break; + case EAST: + if (r.col + 1 < m && field[r.row][r.col + 1] != '#') + { + r.col++; + } + break; + case WEST: + if (r.col > 0 && field[r.row][r.col - 1] != '#') + { + r.col--; + } + break; + case SOUTH: + if (r.row + 1 < n && field[r.row + 1][r.col] != '#') + { + r.row++; + } + break; + } + if (field[r.row][r.col] == '*') + { + field[r.row][r.col] = '.'; + result = 1; + } + return result; +} + +int main() +{ + int n, m, s; + char instruction; + while (cin >> n >> m >> s) + { + if (!n && !m && !s) + { + break; + } + robot r; + int result = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + cin >> field[i][j]; + if (field[i][j] == 'N' || field[i][j] == 'S' || field[i][j] == 'L' || field[i][j] == 'O') + { + r.row = i; + r.col = j; + r.dir = char2direction(field[i][j]); + field[i][j] = '.'; + } + } + } + while (s--) + { + cin >> instruction; + switch (instruction) + { + case 'F': + result += move_forward(r, n, m); + break; + case 'D': + r.dir = turn_right(r.dir); + break; + case 'E': + r.dir = turn_left(r.dir); + break; + } + } + cout << result << endl; + } + return 0; +} diff --git a/11832.cpp b/11832.cpp new file mode 100644 index 0000000..badc6ea --- /dev/null +++ b/11832.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAX_N = 40; +const int MAX_T = 1000; + +int N, F, as[MAX_N]; +LL plus_[MAX_N + 1][2 * MAX_N * MAX_T + 1], minus_[MAX_N + 1][2 * MAX_N * MAX_T + 1]; + +bool init() +{ + memset(plus_, 0, sizeof(plus_)); + memset(minus_, 0, sizeof(minus_)); + scanf("%d%d", &N, &F); + for (int i = 0; i < N; i++) + { + scanf("%d", as + i); + } + return !(N == 0 && F == 0); +} + +void solve() +{ + F += MAX_N * MAX_T; + int mini = MAX_N * MAX_T, maxi = MAX_N * MAX_T; + for (int i = 0; i < N; i++) + { + for (int v = mini; v <= maxi; v++) + if (plus_[i][v] > 0 || minus_[i][v] > 0 || (i == 0 && v == MAX_N * MAX_T)) + { + plus_[i + 1][v + as[i]] |= plus_[i][v] | (1LL << i); + minus_[i + 1][v + as[i]] |= minus_[i][v]; + plus_[i + 1][v - as[i]] |= plus_[i][v]; + minus_[i + 1][v - as[i]] |= minus_[i][v] | (1LL << i); + maxi = max(maxi, v + as[i]); + mini = min(mini, v - as[i]); + } + } + if (plus_[N][F] == 0 && minus_[N][F] == 0) + { + puts("*"); + } + else + { + for (int i = 0; i < N; i++) + { + if ((plus_[N][F] >> i) & 1) + { + if ((minus_[N][F] >> i) & 1) + { + putchar('?'); + } + else + { + putchar('+'); + } + } + else + { + putchar('-'); + } + } + puts(""); + } +} + +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/11833.cpp b/11833.cpp new file mode 100644 index 0000000..b9adbfc --- /dev/null +++ b/11833.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +const int INF = 0x3f3f3f3f; +#define FORI(a, b, c) for (int a = b; a < c; ++a) +#define FOR(a, b) FORI(a, 0, b) +const int NN = 250; +int c, k, m, n; +int w[NN][NN]; +int dijkstra(int s, int t) +{ + int in[NN], d[NN]; + int i, u = s; + FOR(i, n) + d[i] = w[s][i], + in[i] = 0; + d[s] = 0; + in[s] = 1; + while (s != t) + { + int best = INF; + FOR(i, n) + if (!in[i] && best > d[i]) + { + best = d[s = i]; + } + if (best == INF) + { + break; + } + in[s] = 1; + FOR(i, n) + if (!in[i] && w[s][i] < INF && d[i] > d[s] + w[s][i]) + { + d[i] = d[s] + w[s][i]; + } + } + return d[t]; +} +int main() +{ + int x, y, z; + while (scanf("%d %d %d %d", &n, &m, &c, &k) && n) + { + FOR(i, n) + FORI(j, i + 1, n) w[i][j] = w[j][i] = INF; + FOR(i, m) + { + scanf("%d %d %d", &x, &y, &z); + w[x][y] = w[y][x] = z; + if (x < c) + { + if (y >= c) + { + w[x][y] = INF; + } + else + { + int minc = min(x, y); + int maxc = max(x, y); + if (maxc - minc == 1) + { + w[minc][maxc] = z; + } + else + { + w[minc][maxc] = INF; + } + w[maxc][minc] = INF; + } + } + else if (y < c) + { + w[y][x] = INF; + } + } + x = 0; + for (int i = c - 1; i--;) + { + x += w[i][i + 1]; + w[i][i + 1] = INF; + w[i][c - 1] = x; + } + printf("%d\n", dijkstra(k, c - 1)); + } + return 0; +} diff --git a/11834.cpp b/11834.cpp index 64fbc71..b165932 100644 --- a/11834.cpp +++ b/11834.cpp @@ -1,44 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -int main(){ - while (true){ - int L, C, R1, R2; - scanf("%d%d%d%d", &L, &C, &R1, &R2); - if (L == 0 && C == 0 && R1 == 0 && R2 == 0) - break; - double R = R1 + R2; - double newL = L - R; - double newC = C - R; - double dia = sqrt(newL * newL + newC * newC); - if(dia >= R && 2 * max(R1, R2) <= min(L, C)) - printf("S\n"); - else - printf("N\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + while (true) + { + int L, C, R1, R2; + scanf("%d%d%d%d", &L, &C, &R1, &R2); + if (L == 0 && C == 0 && R1 == 0 && R2 == 0) + { + break; + } + double R = R1 + R2; + double newL = L - R; + double newC = C - R; + double dia = sqrt(newL * newL + newC * newC); + if (dia >= R && 2 * max(R1, R2) <= min(L, C)) + { + printf("S\n"); + } + else + { + printf("N\n"); + } + } + return 0; +} diff --git a/11835.cpp b/11835.cpp new file mode 100644 index 0000000..08b0904 --- /dev/null +++ b/11835.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second < b.second; +} + +int main() +{ + int g, p, tmp, s, k; + while (cin >> g >> p) + { + if (g == 0 && p == 0) + { + break; + } + vector>> results; + while (g--) + { + vector> result; + rep(i, p) + { + cin >> tmp; + result.push_back(make_pair(i + 1, tmp)); + } + sort(result.begin(), result.end(), theTruthIsOutThere); + results.push_back(result); + } + cin >> s; + while (s--) + { + cin >> k; + int points[k]; + rep(i, k) + { + cin >> points[i]; + } + int score[p + 1]; + rep(i, p + 1) + score[i] = 0; + int best = 0; + rep(i, results.size()) + { + rep(j, min(int(results[i].size()), k)) + { + score[results[i][j].first] += points[j]; + if (score[results[i][j].first] > best) + { + best = score[results[i][j].first]; + } + } + } + bool first = true; + rep(i, p + 1) + { + if (score[i] == best) + { + if (!first) + { + cout << " "; + } + cout << i; + first = false; + } + } + cout << endl; + } + } + return 0; +} diff --git a/11836.cpp b/11836.cpp index bc2a9fe..acc5d8e 100644 --- a/11836.cpp +++ b/11836.cpp @@ -1,62 +1,260 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -struct Point{ - double x; - double y; - double z; -}; - -double distC(Point A, Point B){ - return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) + (A.z-B.z)*(A.z-B.z)); -} -int main(){ - int test; - cin>>test; - while(test--){ - Point defend[4], attack[4]; - double minm = 1000000000; - int i=0, j=0; - for(i=0; i<4; i++) - cin>>defend[i].x>>defend[i].y>>defend[i].z; - for(i=0; i<4; i++) - cin>>attack[i].x>>attack[i].y>>attack[i].z; - for(i=0; i<4; i++){ - for(j=0; j<4; j++){ - double dist = distC(defend[i], attack[j]); - minm = (minm < dist) ? minm : dist; - } - } - printf("%.2lf\n", minm); - } - return 0; -} +#include + +using namespace std; + +#define eps 1e-4 + +struct Nodes +{ + double x, y, z; +} p[2][4]; + +double sqr(double x) +{ + return x * x; +} +double dis(Nodes p1, Nodes p2) +{ + return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y) + sqr(p1.z - p2.z)); +} +Nodes operator-(Nodes p1, Nodes p2) +{ + Nodes tmp; + tmp.x = p2.x - p1.x; + tmp.y = p2.y - p1.y; + tmp.z = p2.z - p1.z; + return tmp; +} +double cal_1(Nodes p1, Nodes p2) +{ + return dis(p1, p2); +} +Nodes getP(Nodes u1, Nodes u2, double r) +{ + Nodes now; + now.x = u1.x + (u2.x - u1.x) * r; + now.y = u1.y + (u2.y - u1.y) * r; + now.z = u1.z + (u2.z - u1.z) * r; + return now; +} +double get_area(Nodes p1, Nodes p2, Nodes p3) +{ + double x1, x2, y1, y2, z1, z2; + x1 = p2.x - p1.x; + x2 = p3.x - p1.x; + y1 = p2.y - p1.y; + y2 = p3.y - p1.y; + z1 = p2.z - p1.z; + z2 = p3.z - p1.z; + double dx, dy, dz; + dx = y1 * z2 - z1 * y2; + dy = -(x1 * z2 - x2 * z1); + dz = x1 * y2 - y1 * x2; + return sqrt(sqr(dx) + sqr(dy) + sqr(dz)) / 2.0; +} +double dot(Nodes a, Nodes b, Nodes c) +{ + Nodes p1 = b - a, p2 = c - a; + return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z; +} +double cal_2(Nodes now, Nodes v1, Nodes v2) +{ + if (dot(v1, now, v2) <= 0) + { + return dis(now, v1); + } + if (dot(v2, now, v1) <= 0) + { + return dis(now, v2); + } + double area = get_area(now, v1, v2); + return area * 2 / dis(v1, v2); +} +double cal_3(Nodes u1, Nodes u2, Nodes v1, Nodes v2) +{ + double L = 0, R = 1; + double res = 1e100; + while (L < R) + { + double L1 = L + (R - L) / 3; + double R1 = L + (R - L) * 2 / 3; + Nodes nowL, nowR; + nowL = getP(u1, u2, L1); + nowR = getP(u1, u2, R1); + double LenL = cal_2(nowL, v1, v2); + double LenR = cal_2(nowR, v1, v2); + //puts("yes"); + if (LenL <= LenR) + { + if (res > LenL) + { + res = LenL; + } + R = R1 - eps; + } + else + { + L = L1 + eps; + if (res > LenR) + { + res = LenR; + } + } + } + return res; +} + +int work(double tmp) +{ + if (fabs(tmp) < eps) + { + return 0; + } + if (tmp > 0) + { + return 1; + } + return -1; +} +double cross(Nodes p1, Nodes p2) +{ + return p1.x * p2.y - p1.y * p2.x; +} +bool IsPInPlane(Nodes o, Nodes p1, Nodes p2, Nodes p3) +{ + int sgn1, sgn2, sgn3; + double tmp = cross(o - p1, o - p2); + sgn1 = work(tmp); + tmp = cross(o - p2, o - p3); + sgn2 = work(tmp); + tmp = cross(o - p3, o - p1); + sgn3 = work(tmp); + if (sgn1 > 0) + { + if (sgn2 <= 0 || sgn3 <= 0) + { + return false; + } + return true; + } + if (sgn1 < 0) + { + if (sgn2 >= 0 || sgn3 >= 0) + { + return false; + } + return true; + } + return false; +} +double cal_4(Nodes o, Nodes p1, Nodes p2, Nodes p3) +{ + //get the cross vetctor + double x1, x2, y1, y2, z1, z2; + x1 = p2.x - p1.x; + x2 = p3.x - p1.x; + y1 = p2.y - p1.y; + y2 = p3.y - p1.y; + z1 = p2.z - p1.z; + z2 = p3.z - p1.z; + double dx, dy, dz, A, B, C, D; + dx = y1 * z2 - z1 * y2; + dy = -(x1 * z2 - x2 * z1); + dz = x1 * y2 - y1 * x2; + //get the plane + A = dx; + B = dy; + C = dz; + D = A * p1.x + B * p1.y + C * p1.z; + double k; + k = (D - A * o.x - B * o.y - C * o.z) / (A * dx + B * dy + C * dz); + Nodes s; + s.x = o.x + dx * k; + s.y = o.y + dy * k; + s.z = o.z + dz * k; + if (IsPInPlane(s, p1, p2, p3)) + { + return dis(s, o); + } + else + { + return 1e100; + } +} +int main() +{ + int i, j, k; + int t; + double res = 1e100; + scanf("%d", &t); + while (t--) + { + for (i = 0; i < 2; i++) + { + for (j = 0; j < 4; j++) + { + scanf("%lf%lf%lf", &p[i][j].x, &p[i][j].y, &p[i][j].z); + } + } + res = 1e100; + //p->p + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + res = min(res, cal_1(p[0][i], p[1][j])); + } + } + // printf("p->p %.2f\n",res); + //p->line + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + for (k = 0; k < 4; k++) + { + if (j != k) + { + res = min(res, cal_2(p[0][i], p[1][j], p[1][k])); + } + if (j != k) + { + res = min(res, cal_2(p[1][i], p[0][j], p[0][k])); + } + } + } + } + // printf("p->line %.2f\n",res); + //line->line + int i1, i2, j1, j2; + for (i1 = 0; i1 < 4; i1++) + { + for (i2 = i1 + 1; i2 < 4; i2++) + { + for (j1 = 0; j1 < 4; j1++) + { + for (j2 = j1 + 1; j2 < 4; j2++) + { + res = min(res, cal_3(p[0][i1], p[0][i2], p[1][j1], p[1][j2])); + } + } + } + } + // printf("line->line %.2f\n",res); + //p->plane + for (i = 0; i < 4; i++) + { + res = min(res, cal_4(p[0][i], p[1][0], p[1][1], p[1][2]));//3 + res = min(res, cal_4(p[0][i], p[1][0], p[1][1], p[1][3]));//2 + res = min(res, cal_4(p[0][i], p[1][0], p[1][3], p[1][2]));//1 + res = min(res, cal_4(p[0][i], p[1][3], p[1][1], p[1][2]));//0 + //**** + res = min(res, cal_4(p[1][i], p[0][0], p[0][1], p[0][2]));//3 + res = min(res, cal_4(p[1][i], p[0][0], p[0][1], p[0][3]));//2 + res = min(res, cal_4(p[1][i], p[0][0], p[0][3], p[0][2]));//1 + res = min(res, cal_4(p[1][i], p[0][3], p[0][1], p[0][2]));//0 + } + printf("%.2f\n", res); + } + return 0; +} diff --git a/11837.cpp b/11837.cpp new file mode 100644 index 0000000..8014ae8 --- /dev/null +++ b/11837.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +#define KKL 10048 +#define WWL 100048 + +const char *ans[2] = {"N", "S"}; +char map_[256], note[64], K[KKL], W[WWL]; +int F[KKL]; + +void kmp_table(char *K, int KL) +{ + F[0] = F[1] = 0; + int i, j; + for (i = 2; i < KL; ++i) + { + j = F[i - 1]; + while (1) + { + if (K[j] == K[i - 1]) + { + F[i] = j + 1; + break; + } + if (j == 0) + { + F[i] = 0; + break; + } + j = F[j]; + } + } +} +int kmp(char *W, int WL, char *K, int KL) +{ + kmp_table(K, KL); + for (int i = 0, j = 0; j < WL;) + { + if (W[j] == K[i]) + { + i++; + j++; + if (i == KL) + { + return j; + } + } + else if (i > 0) + { + i = F[i]; + } + else + { + j++; + } + } + return -1; +} +void read_music(char *x, int L) +{ + char p, v; + for (int i = 0; i < L; ++i) + { + scanf("%s", note); + p = v; + v = map_[note[0]] + (note[1] == '#') - (note[1] == 'b'); + v = (v + 12) % 12; + if (i) + { + x[i - 1] = (v - p + 12) % 12; + } + } +} + +int main() +{ + map_['A'] = 0; + map_['B'] = 2; + map_['C'] = 3; + map_['D'] = 5; + map_['E'] = 7; + map_['F'] = 8; + map_['G'] = 10; + int z, KL, WL; + while (scanf("%d %d", &WL, &KL) && WL) + { + read_music(W, WL); + read_music(K, KL); + --WL, --KL; + z = kmp(W, WL, K, KL); + puts(ans[z != -1]); + } + return 0; +} diff --git a/11838.cpp b/11838.cpp index 94e2fbb..ebc8d7a 100644 --- a/11838.cpp +++ b/11838.cpp @@ -1,84 +1,45 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int N, test; - int i, j, k; - cin>>N>>test; - if(N==0 && test==0) - break; - bool mat[N][N]; - memset( mat, false, sizeof(bool)*N*N ); - while(test--){ - int X, Y, ctr; - cin>>X>>Y>>ctr; - if(ctr == 1) - mat[X-1][Y-1] = true; - else{ - mat[X-1][Y-1] = true; - mat[Y-1][X-1] = true; - } - } - for(i=0; i que; - bool stat[N][N]; - memset( stat, false, sizeof(bool)*N*N ); - for(j=0; j + +using namespace std; + +list E[2000 + 1]; +bool v[2000 + 1]; + +int dfs(int u) +{ + int res = 1; + v[u] = true; + for (list::iterator v = E[u].begin(); v != E[u].end(); ++v) + if (!::v[*v]) + res += dfs(*v); + return res; +} + +int main() +{ + for (int n, m; scanf("%d %d", &n, &m) && !(n == 0 && m == 0);) + { + for (int i = 1; i <= n; ++i) + { + E[i].clear(); + v[i] = false; + } + for (int i = 0, v, w, p; i < m; ++i) + { + scanf("%d %d %d", &v, &w, &p); + E[v].push_back(w); + if (p == 2) + E[w].push_back(v); + } + bool G = true; + for (int i = 1; i <= n && G; ++i) + { + for (int i = 1; i <= n; ++i) + v[i] = false; + if (!::v[i]) + G = (dfs(i) == n); + } + printf("%d\n", G ? 1 : 0); + } + return 0; +} diff --git a/11839.cpp b/11839.cpp index 18c1393..8e0da6b 100644 --- a/11839.cpp +++ b/11839.cpp @@ -1,57 +1,40 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int N; - cin>>N; - if(N == 0) - break; - while(N--){ - int A, B, C, D, E; - cin>>A>>B>>C>>D>>E; - if(A<=127 && B>127 && C>127 && D>127 && E>127) - cout<<"A\n"; - else if(A>127 && B<=127 && C>127 && D>127 && E>127) - cout<<"B\n"; - else if(A>127 && B>127 && C<=127 && D>127 && E>127) - cout<<"C\n"; - else if(A>127 && B>127 && C>127 && D<=127 && E>127) - cout<<"D\n"; - else if(A>127 && B>127 && C>127 && D>127 && E<=127) - cout<<"E\n"; - else - cout<<"*\n"; - } - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + bool ok; + int result; + char marks[6] = {'A', 'B', 'C', 'D', 'E', '*'}; + int answers[5]; + while (cin >> n) + { + if (n == 0) + { + break; + } + while (n--) + { + result = -1; + for (int i = 0; i < 5; i++) + { + cin >> answers[i]; + } + for (int i = 0; i < 5; i++) + { + if (answers[i] <= 127) + { + result = result == -1 ? i : 5; + } + } + if (result == -1) + { + result = 5; + } + cout << marks[result] << endl; + } + } + return 0; +} diff --git a/1184.cpp b/1184.cpp new file mode 100644 index 0000000..f8affda --- /dev/null +++ b/1184.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define MAXN 120 +#define INF 121 + +#define Zero(v) memset(v, 0, sizeof(v)) +#define cFor(t, v, c) for (t::const_iterator v = c.begin(); v != c.end(); ++v) + +typedef queue IQ; +typedef vector IV; + +// Graphs +struct Graph +{ + struct Edge + { + int v; + Edge(int V) : v(V) {} + }; + typedef list EL; + typedef vector ELV; + ELV adj; + int n; + void init(int N) + { + n = N; + adj.clear(); + adj.resize(n); + } + void add(int u, int v) + { + adj[u].push_back(Edge(v)); + } + + // Hopcroft-Karp for bipartite matching. Receives the vertices in G1, + // and depends on vertex #0 being reserved as the NIL vertex + struct HK + { + IV pair_g1, pair_g2, dist; + Graph &g; + IV &g1; + HK(Graph &G, IV &G1) : g(G), g1(G1) + { + pair_g1 = IV(g.n); + pair_g2 = IV(g.n); + dist = IV(g.n); + } + bool bfs() + { + IQ q; + cFor(IV, v, g1) + { + if (pair_g1[*v] == 0) + { + dist[*v] = 0; + q.push(*v); + } + else + { + dist[*v] = INF; + } + } + dist[0] = INF; + while (!q.empty()) + { + int v = q.front(); + q.pop(); + cFor(EL, e, g.adj[v]) + { + int p = pair_g2[e->v]; + if (dist[p] != INF) + { + continue; + } + dist[p] = dist[v] + 1; + q.push(p); + } + } + return dist[0] != INF; + } + bool dfs(int v) + { + if (v == 0) + { + return true; + } + cFor(EL, e, g.adj[v]) + { + int p = pair_g2[e->v]; + if (dist[p] == dist[v] + 1 && dfs(p)) + { + pair_g2[e->v] = v; + pair_g1[v] = e->v; + return true; + } + } + dist[v] = INF; + return false; + } + }; + int hopcroft(IV &g1) + { + HK hk(*this, g1); + int m = 0; + while (hk.bfs()) + cFor(IV, v, g1) if (hk.pair_g1[*v] == 0 && hk.dfs(*v)) + { + ++m; + } + return m; + } +}; + +int n, m; +bool src[MAXN + 1]; +Graph g; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + Zero(src); + g.init(n + 1); + IV g1; + while (m--) + { + int u, v; + scanf("%d%d", &u, &v); + g.add(u, v); + if (!src[u]) + { + src[u] = true; + g1.push_back(u); + } + } + int match = g.hopcroft(g1); + printf("%d\n", n - match); + } + return 0; +} diff --git a/11840.cpp b/11840.cpp new file mode 100644 index 0000000..81269ec --- /dev/null +++ b/11840.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define MAX 2000000000 +#define _match(a, b) ((a) == (b)) + +bool mark[11000]; +char s[11000]; +int main() +{ + int sg[11000]; + sg[0] = 0; + sg[1] = 1; + sg[2] = 1; + int i, j; + for (i = 3; i <= 10000; i++) + { + memset(mark, 0, sizeof(mark)); + for (j = 1; j <= i; j++) + { + int x, y; + x = j - 3; + y = j + 2; + if (x < 0) + { + x = 0; + } + if (y > i) + { + y = i; + } + y = i - y; + mark[sg[x] ^ sg[y]] = true; + } + for (j = 0; j >= 0; j++) + if (!mark[j]) + { + sg[i] = j; + break; + } + } + int d[4] = {-2, -1, 1, 2}; + int x, n; + while (cin >> n) + { + if (n == 0) + { + break; + } + scanf("%s", s); + memset(mark, 0, sizeof(mark)); + bool ff = false; + for (i = 0; i < n; i++) + { + if (s[i] != 'X') + { + continue; + } + mark[i] = true; + for (j = 0; j < 4; j++) + { + x = i + d[j]; + if (x < 0) + { + x = 0; + } + if (x >= n) + { + x = n - 1; + } + mark[x] = true; + if (x != i && s[x] == 'X') + { + ff = true; + } + } + } + if (ff) + { + cout << "S" << endl; + continue; + } + int ans = 0; + for (i = 0; i < n; i++) + { + if (!mark[i]) + { + for (j = i; j < n; j++) + { + if (mark[j]) + { + break; + } + } + x = j - i; + ans = ans ^ sg[x]; + i = j; + } + } + if (ans) + { + cout << "S" << endl; + } + else + { + cout << "N" << endl; + } + } + return 0; +} diff --git a/11841.cpp b/11841.cpp new file mode 100644 index 0000000..cf6e9b9 --- /dev/null +++ b/11841.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +const int MAXN = 25; + +bool visited[MAXN][MAXN][MAXN]; + +bool black[MAXN][MAXN][MAXN]; + +int dx[] = {-1, +0, +1, +1, +0, -1}; +int dy[] = {+0, -1, -1, +0, +1, +1}; +int dz[] = {+1, +1, +0, -1, -1, +0}; + +bool inside(int x, int y, int z) +{ + return x >= 0 and y >= 0 and z >= 0; +} + +bool blackCanWin(int sx, int sy, int sz, int n) +{ + memset(visited, 0, sizeof visited); + assert(inside(sx, sy, sz) and black[sx][sy][sz]); + queue q; + visited[sx][sy][sz] = true; + q.push(sx); + q.push(sy); + q.push(sz); + while (q.size() > 0) + { + int x = q.front(); + q.pop(); + int y = q.front(); + q.pop(); + int z = q.front(); + q.pop(); + //printf("popped %d, %d, %d\n", x, y, z); + for (int k = 0; k < 6; ++k) + { + int nx = x + dx[k]; + int ny = y + dy[k]; + int nz = z + dz[k]; + if (!inside(nx, ny, nz)) + { + continue; + } + if (!black[nx][ny][nz]) + { + continue; + } + if (visited[nx][ny][nz]) + { + continue; + } + q.push(nx); + q.push(ny); + q.push(nz); + visited[nx][ny][nz] = true; + } + } + bool seenX = false, seenY = false, seenZ = false; + for (int x = 0; x <= n; ++x) + { + for (int y = 0; y <= n; ++y) + { + for (int z = 0; z <= n; ++z) + { + if (!black[x][y][z]) + { + continue; + } + if (!visited[x][y][z]) + { + continue; + } + seenX |= (x == 0); + seenY |= (y == 0); + seenZ |= (z == 0); + //printf("saw black cell (%d, %d, %d)\n", x, y, z); + } + } + } + //printf("seenX = %d, seenY = %d, seenZ = %d\n", seenX, seenY, seenZ); + return seenX and seenY and seenZ; +} + +int main() +{ + int n, m; + while (cin >> n >> m) + { + if (n == 0 and m == 0) + { + break; + } + memset(visited, 0, sizeof visited); + memset(black, 0, sizeof black); + for (int k = 0; k < m; ++k) + { + int x, y, z; + cin >> x >> y >> z; + black[x][y][z] = true; + } + bool blackWins = false; + for (int x = 0; x <= n; ++x) + { + for (int y = 0; y <= n; ++y) + { + for (int z = 0; z <= n; ++z) + { + if (!black[x][y][z]) + { + continue; + } + if (visited[x][y][z]) + { + continue; + } + if (x != 0 and y != 0 and z != 0) + { + continue; + } + assert(black[x][y][z]); + blackWins |= blackCanWin(x, y, z, n); + if (blackWins) + { + x = y = z = n;// ultra break + } + } + } + } + printf("%s\n", blackWins ? "Benny" : "Willy"); + } + return 0; +} diff --git a/11842.cpp b/11842.cpp new file mode 100644 index 0000000..960352f --- /dev/null +++ b/11842.cpp @@ -0,0 +1,353 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) + +inline int cmp(double x, double y = 0, double tol = 1e-9) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} + +int width, height; + +struct Vector +{ + double x, y; + Vector() + { + } + Vector(double X, double Y) + : x(X), y(Y) + { + } + inline void normalize() + { + double length = sqrt(x * x + y * y); + x /= length; + y /= length; + } + bool operator<(const Vector &o) const + { + if (cmp(x, o.x) != 0) + { + return cmp(x, o.x) < 0; + } + return cmp(y, o.y) < 0; + } + bool operator==(const Vector &o) const + { + return (cmp(x, o.x) == 0 and + cmp(y, o.y) == 0); + } +}; + +struct Person +{ + double x, y; + Vector d; + string name; + + Person() + { + } + Person(double X, double Y, const Vector &D, string Name) + : x(X), y(Y), d(D.x, D.y), name(Name) + { + } + + bool operator<(const Person &o) + { + return name < o.name; + } + + double timeToFall() const; + + friend ostream &operator<<(ostream &stream, const Person &p); +}; + +double Person::timeToFall() const +{ + double horizontal = cmp(this->d.x, 0) < 0 ? this->x / (-this->d.x) : (width - this->x) / (this->d.x); + double vertical = cmp(this->d.y, 0) < 0 ? this->y / (-this->d.y) : (height - this->y) / (this->d.y); + return min(horizontal, vertical); +} + +ostream &operator<<(ostream &stream, const Person &p) +{ + stream << p.name << " is at (" << p.x << ", " << p.y << ") moving in direction <" << p.d.x << ", " << p.d.y << "> (Might fall in " << p.timeToFall() << " seconds)"; + return stream; +} + +struct Death +{ + string name; + double time; + Death() + { + } + Death(const string &Name, double Time) + : name(Name), time(Time) + { + } + bool operator<(const Death &o) const + { + if (cmp(time, o.time) != 0) + { + return cmp(time, o.time) < 0; + } + return name < o.name; + } +}; + +struct Collision +{ + double time; + Vector point; // point of collision, if any + vector people;// the guy who fell or the two fellows that collided (contains indexes) + + Collision() + { + } + Collision(double Time) + : time(Time) + { + } + Collision(double Time, double x, double y, int personA, int personB) + : time(Time), point(x, y) + { + people = vector(2); + people[0] = personA; + people[1] = personB; + } + + bool operator<(const Collision &o) const + { + if (cmp(time, o.time) != 0) + { + return cmp(time, o.time) < 0; + } + return point < o.point; + } +}; + +double dot(const Vector &a, const Vector &b) +{ + return a.x * b.x + a.y * b.y; +} + +bool outsideWorld(double x, double y) +{ + if (cmp(x, 0) < 0) + { + return true; + } + if (cmp(x, width) > 0) + { + return true; + } + if (cmp(y, 0) < 0) + { + return true; + } + if (cmp(y, height) > 0) + { + return true; + } + return false; +} + +// returns the time where person a and b would collide, or -1 if they won't +Collision collisionBetween(const Person &a, const int indexOfA, const Person &b, const int indexOfB) +{ + if (Vector(a.x, a.y) == Vector(b.x, b.y)) + { + return Collision(-1.0);// they are on the same point, won't collide + } + double x0 = a.x, x1 = a.x + a.d.x; + double y0 = a.y, y1 = a.y + a.d.y; + double x2 = b.x, x3 = b.x + b.d.x; + double y2 = b.y, y3 = b.y + b.d.y; + double t0 = (y3 - y2) * (x0 - x2) - (x3 - x2) * (y0 - y2); + double t1 = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0); + double det = (y1 - y0) * (x3 - x2) - (y3 - y2) * (x1 - x0); + if (cmp(det, 0) == 0) + { + //parallel + if (cmp(t0, 0) == 0 || cmp(t1, 0) == 0) + { + // they lie on same line. but they might not collide (if they travel in the same direction) + if (cmp(dot(a.d, b.d), 1) == 0)// they travel in the same direction + { + return Collision(-1.0); + } + Vector pointOfCollision(x0 + (x2 - x0) / 2, + y0 + (y2 - y0) / 2); + double dist = hypot(x2 - x0, y2 - y0) / 2; + if (cmp(x0 + dist * a.d.x, pointOfCollision.x) != 0 or cmp(y0 + dist * a.d.y, pointOfCollision.y) != 0 or + cmp(x2 + dist * b.d.x, pointOfCollision.x) != 0 or cmp(y2 + dist * b.d.y, pointOfCollision.y) != 0) + { + // they travel in opposite directions, but after traveling half the distance they haven't collided, so they'll never collide + return Collision(-1.0); + } + return Collision(dist, pointOfCollision.x, pointOfCollision.y, indexOfA, indexOfB);// they'll collide in half their distance because they travel in opposite directions + } + //just parallel, no intersection + return Collision(-1.0); + } + t0 /= det; + t1 /= det; + if (cmp(t0, t1) == 0 and cmp(0.0, t0) <= 0 and cmp(0.0, t1) <= 0) + { + double x = x0 + t0 * (x1 - x0); + double y = y0 + t0 * (y1 - y0); + if (outsideWorld(x, y)) + { + return Collision(-1.0); + } + return Collision(t0, x, y, indexOfA, indexOfB); + } + return Collision(-1.0); +} + +bool collidesWithSomeone(const vector &people, int i) +{ + for (int j = 0; j < people.size(); ++j) + { + if (i == j) + { + continue; + } + Collision c = collisionBetween(people[i], i, people[j], j); + // assert(cmp(c.time, 0) != 0);// if they collided, assert it wasn't at time 0 + if (cmp(c.time, 0) > 0) // bang! + { + return true; + } + } + return false; +} + +// if there's someone who doesn't collide, it's added to the deaths vector and removed from the people vector +vector kill_or_bounce_ppl(vector people, vector &deaths, double &elapsedTime) +{ + for (int i = 0; i < people.size(); ++i) + { + if (!collidesWithSomeone(people, i)) + { + deaths.push_back(Death(people[i].name, elapsedTime + people[i].timeToFall())); + vector ans; + for (int j = 0; j < people.size(); ++j) + { + if (i != j) + { + ans.push_back(people[j]); + } + } + return ans; + } + } + // everybody collides here + for (int i = 0; i < people.size(); ++i) + { + // assert(collidesWithSomeone(people, i)); + } + // now process collisions, by time + vector collisions; + for (int i = 0; i < people.size(); ++i) + { + for (int j = i + 1; j < people.size(); ++j) + { + Collision c = collisionBetween(people[i], i, people[j], j); + if (cmp(c.time, 0) > 0) + { + collisions.push_back(c); + } + } + } + // assert(collisions.size() > 0); + sort(collisions.begin(), collisions.end()); + int i = 0, n = collisions.size(); + set peopleWhoDiedInCollisions; + while (i < n and cmp(collisions[0].time, collisions[i].time) == 0) + { + int j = i; + while (j < n and cmp(collisions[i].time, collisions[j].time) == 0 and collisions[i].point == collisions[j].point) + { + j++; + } + // collisions[i..j) have the same point and time) + for (int k = i; k < j; ++k) + { + } + if (j - i == 1)// two people + { + // just swap their names + int personA = collisions[i].people[0], personB = collisions[i].people[1]; + swap(people[personA].name, people[personB].name); + } + else// more than two people + { + for (int k = i; k < j; ++k) + { + peopleWhoDiedInCollisions.insert(collisions[k].people.begin(), collisions[k].people.end()); + } + } + i = j; + } + vector newPeople; + for (i = 0; i < people.size(); ++i) + { + if (peopleWhoDiedInCollisions.count(i) > 0) + { + deaths.push_back(Death(people[i].name, elapsedTime + collisions[0].time)); + } + else + { + Person p = people[i]; + p.x += collisions[0].time * p.d.x; + p.y += collisions[0].time * p.d.y; + newPeople.push_back(p); + } + } + elapsedTime += collisions[0].time; + return newPeople; +} + +int main() +{ + int Cases; + cin >> Cases; + while (Cases--) + { + int n; + cin >> n >> width >> height; + vector people; + for (int i = 0; i < n; ++i) + { + Person p; + cin >> p.x >> p.y >> p.d.x >> p.d.y >> p.name; + // assert(cmp(floor(p.x), p.x) == 0 && cmp(floor(p.y), p.y) == 0); + p.d.x -= p.x; + p.d.y -= p.y; + p.d.normalize(); + people.push_back(p); + } + vector deaths; + double elapsedTime = 0; + while (people.size() > 0) + { + people = kill_or_bounce_ppl(people, deaths, elapsedTime); + for (int i = 0; i < people.size(); ++i) + { + } + } + sort(deaths.begin(), deaths.end()); + for (int i = 0; i < deaths.size(); ++i) + { + } + printf("%s\n", deaths.back().name.c_str()); + } + return 0; +} diff --git a/11843.cpp b/11843.cpp new file mode 100644 index 0000000..dc45803 --- /dev/null +++ b/11843.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define NS 21 +#define NN 1001 +int t[NS][NN]; + +int guess(int b, int f) +{ + if (t[b][f] != -1) + { + return t[b][f]; + } + int m = f; + for (int i = 1; i <= f; ++i) + { + int x = guess(b - 1, i - 1), y = guess(b, f - i); + m = min(m, 1 + max(x, y)); + } + return t[b][f] = m; +} + +int main() +{ + for (int i = 1; i < NN; ++i) + { + t[1][i] = i; + } + for (int i = 2; i < NS; ++i) + { + for (int j = 2; j < NN; ++j) + { + t[i][j] = -1; + } + t[i][1] = 1; + t[i][0] = 0; + } + int z; + scanf("%d", &z); + for (int n, s; scanf("%d %d", &n, &s) == 2; printf("%d\n", guess(s, n))) + ; + return 0; +} diff --git a/11844.cpp b/11844.cpp new file mode 100644 index 0000000..eedd51b --- /dev/null +++ b/11844.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +map removeEmptyProteins(map &config) +{ + map ans; + for (map::iterator i = config.begin(); i != config.end(); ++i) + { + const string &protein = i->first; + long long times = i->second; + if (times > 0) + { + ans[protein] = times; + } + } + return ans; +} + +map mutate(map &mutations, map &config) +{ + map ans; + for (map::iterator i = config.begin(); i != config.end(); ++i) + { + const string &protein = i->first; + long long times = i->second; + if (mutations.count(protein) > 0) + { + const string &newProtein = mutations[protein]; + ans[newProtein] += times; + } + else + { + ans[protein] += times; + } + } + return ans; +} + +int solveCase(map &mutations, const map &initialConfig, const map &finalConfig, int bound) +{ + if (initialConfig == finalConfig) + { + return 0; + } + map config = initialConfig; + for (int k = 1; k <= bound; ++k) + { + config = mutate(mutations, config); + config = removeEmptyProteins(config); + // printf("after %d mutations config is:\n", k); + // for (map::iterator i = config.begin(); i != config.end(); ++i) { + // const string &protein = i->first; + // long long times = i->second; + // printf("%s -> %lld\n", protein.c_str(), times); + // } + if (config == finalConfig) + { + return k; + } + } + return -1; +} + +int main() +{ + int NM, NI, NC, bound; + while (cin >> NM >> NI >> NC >> bound) + { + if (NM == 0 and NI == 0 and NC == 0 and bound == 0) + { + break; + } + bool deterministic = true; + map mutations; + for (int t = 0; t < NM; ++t) + { + string k, v; + cin >> k >> v; + if (mutations.count(k) > 0 and mutations[k] != v) + { + deterministic = false; + } + mutations[k] = v; + } + map initialConfig, finalConfig; + for (int i = 0; i < NI; ++i) + { + string protein; + long long times; + cin >> protein >> times; + initialConfig[protein] = times; + } + for (int i = 0; i < NC; ++i) + { + string protein; + long long times; + cin >> protein >> times; + finalConfig[protein] = times; + } + if (!deterministic) + { + printf("Protein mutations are not deterministic\n"); + continue; + } + int ans = solveCase(mutations, initialConfig, finalConfig, bound); + if (ans == -1) + { + printf("Nostalgia for Infinity is doomed\n"); + } + else + { + printf("Cure found in %d mutation(s)\n", ans); + } + } + return 0; +} diff --git a/11845.cpp b/11845.cpp new file mode 100644 index 0000000..7f0f99b --- /dev/null +++ b/11845.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +// Accepted + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +const int MAXN = 105; +bool g[MAXN][MAXN]; +int color[MAXN]; +int nodes; + +enum +{ + white, + gray, + black +}; + +map face; +int currentFace; + +int getIndex(const string &s) +{ + if (face.count(s) > 0) + { + return face[s]; + } + return face[s] = currentFace++; +} + +void clear() +{ + memset(g, 0, sizeof g); + face.clear(); + currentFace = 0; +} + +bool cycleFrom(int u) +{ + if (color[u] == gray) + { + return true; + } + if (color[u] == black) + { + return false; + } + color[u] = gray; + for (int v = 0; v < nodes; ++v) + { + if (!g[u][v]) + { + continue; + } + if (cycleFrom(v)) + { + return true; + } + } + color[u] = black; + return false; +} + +bool cycle() +{ + for (int i = 0; i < nodes; ++i) + { + color[i] = white; + } + for (int i = 0; i < nodes; ++i) + { + if (cycleFrom(i)) + { + return true; + } + } + return false; +} + +void solve() +{ + if (!cycle()) + { + printf("F\n"); + return; + } + for (int u = 0; u < nodes; ++u) + { + for (int v = 0; v < nodes; ++v) + { + if (g[u][v]) + { + g[u][v] = false; + if (!cycle()) + { + printf("P\n"); + return; + } + g[u][v] = true; + } + } + } + printf("N\n"); +} + +int main() +{ + string alice, bob; + while (cin >> alice >> bob) + { + if (alice == "*" and bob == "*") + { + break; + } + string s; + vector> a(1); + while (cin >> s) + { + if (s == ";") + { + break; + } + if (s == ",") + { + a.push_back(vector()); + continue; + } + a.back().push_back(s); + } + vector> b(1); + while (cin >> s) + { + if (s == ";") + { + break; + } + if (s == ",") + { + b.push_back(vector()); + continue; + } + b.back().push_back(s); + } + clear(); + // for (int i = 0; i < a.size(); ++i) { + // printf("row[%d] has %d items\n", i, a[i].size()); + // for (int j = 0; j < a[i].size(); ++j) { + // printf("%s ", a[i][j].c_str()); + // } + // puts(""); + // } + // + // for (int i = 0; i < b.size(); ++i) { + // printf("row[%d] has %d items\n", i, b[i].size()); + // for (int j = 0; j < b[i].size(); ++j) { + // printf("%s ", b[i][j].c_str()); + // } + // puts(""); + // } + for (int i = 0; i < a.size(); ++i) + { + for (int j = 1; j < a[i].size(); ++j) + { + g[getIndex(a[i][j - 1])][getIndex(a[i][j])] = true; + } + } + for (int i = 0; i < b.size(); ++i) + { + for (int j = 1; j < b[i].size(); ++j) + { + g[getIndex(b[i][j - 1])][getIndex(b[i][j])] = true; + } + } + nodes = currentFace; + // for (int i = 0; i < nodes; ++i) { + // for (int j = 0; j < nodes; ++j) { + // printf("%d ", g[i][j]); + // } + // puts(""); + // } + // puts(""); + solve(); + } + return 0; +} diff --git a/11846.cpp b/11846.cpp new file mode 100644 index 0000000..cb74db1 --- /dev/null +++ b/11846.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 20; + +char t[N][N], ans[N][N]; +int n; + +bool dfs(char c, int pos) +{ + while (pos != n * n && ans[pos / n][pos % n] != '.') + { + pos++; + } + if (pos == n * n) + { + return true; + } + int bx = pos % n, by = pos / n; + REP(h, by, n) + { + REP(w, bx, n) + { + int cnt = 0, area = (h - by + 1) * (w - bx + 1); + if (area > 9) + { + break; + } + if (ans[h][w] != '.') + { + break; + } + int now = -1; + bool out = false; + REP(y, by, h + 1) + { + REP(x, bx, w + 1) + { + if (isdigit(t[y][x])) + { + cnt++; + now = (t[y][x] - '0'); + } + if (ans[y][x] != '.') + { + out = true; + break; + } + } + if (out || cnt > 1 || (now != -1 && now != area)) + { + break; + } + } + if (cnt > 1 || (now != -1 && (area > now || now % (h - by + 1) != 0)) || out) + { + break; + } + if (cnt == 0 || now > area) + { + continue; + } + REP(y, by, h + 1) + { + REP(x, bx, w + 1) + { + ans[y][x] = c; + } + } + if (dfs(c + 1, pos + (w - bx + 1))) + { + return true; + } + REP(y, by, h + 1) + { + REP(x, bx, w + 1) + { + ans[y][x] = '.'; + } + } + } + } + return false; +} + +int main() +{ + int tmp; + while (scanf("%d%d", &n, &tmp)) + { + if (n == 0 && tmp == 0) + { + break; + } + rep(i, n) + { + rep(j, n) + { + scanf(" %c ", &t[i][j]); + ans[i][j] = '.'; + } + } + dfs('A', 0); + rep(i, n) + { + rep(j, n) + { + putchar(ans[i][j]); + } + putchar('\n'); + } + } + return 0; +} diff --git a/11847.cpp b/11847.cpp index 5078275..674f1ad 100644 --- a/11847.cpp +++ b/11847.cpp @@ -1,14 +1,13 @@ -#include -#include -using namespace std; - -int main(){ - while(true){ - int n; - scanf("%d", &n); - if(n==0) - break; - printf("%d\n", (int)floor(log2(n))); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n)==1, n) + { + printf("%d\n", log2(n)); + } + return 0; +} diff --git a/11848.cpp b/11848.cpp new file mode 100644 index 0000000..228d998 --- /dev/null +++ b/11848.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +// Accepted + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +typedef pair edge;// first = weight, second = node + +const int MAXN = 105; + +vector ga[MAXN], gb[MAXN]; + +long long g[MAXN][MAXN][2]; + +double d[MAXN]; + +int shortestPath(int start, int end, double percentage, int N) +{ + for (int i = 0; i < N; ++i) + { + d[i] = 1e200; + } + d[start] = 0.0; + priority_queue, vector>, greater>> q; + q.push(make_pair(0.0, start)); + while (q.size() > 0) + { + int u = q.top().second; + double w = q.top().first; + q.pop(); + if (w > d[u]) + { + continue; + } + if (u == end) + { + return floor(w); + } + for (int v = 0; v < N; ++v) + { + if (g[u][v][0] == -1 and g[u][v][1] == -1) + { + continue; + } + double extra; + if (g[u][v][0] != -1 and g[v][u][1] != -1) + { + extra = percentage * g[u][v][0] + (1.0 - percentage) * g[u][v][1]; + } + else + { + extra = max(g[u][v][0], g[u][v][1]); + } + if (w + extra < d[v]) + { + d[v] = w + extra; + q.push(make_pair(w + extra, v)); + } + } + } + assert(false); +} + +int main() +{ + //printf("2.9 = %d 2.1 = %d, 2.999999999999999999999 = %d, 2.5 = %d, 2.0 = %d\n", (int)2.9, (int)2.1, (int)2.999999999999999999999, (int)2.5, (int)2.0); + int numCities, numEdgesA, numEdgesB, combinations; + while (cin >> numCities >> numEdgesA >> numEdgesB >> combinations) + { + if (numCities == -1 and numEdgesA == -1 and numEdgesB == -1 and combinations == -1) + { + break; + } + /*the number of cities, the number of edges in the network of company A, + the number of edges in the network of company B, + and the number of combination alternatives respectively. + */ + for (int i = 0; i < numCities; ++i) + { + for (int j = 0; j < numCities; ++j) + { + g[i][j][0] = g[i][j][1] = -1; + } + } + for (int k = 0; k < numEdgesA; ++k) + { + int u, v; + long long w; + cin >> u >> v >> w; + assert(g[u][v][0] == -1 and g[v][u][0] == -1); + g[u][v][0] = w; + g[v][u][0] = w; + } + for (int k = 0; k < numEdgesB; ++k) + { + int u, v; + long long w; + cin >> u >> v >> w; + assert(g[u][v][1] == -1 and g[v][u][1] == -1); + g[u][v][1] = w; + g[v][u][1] = w; + } + for (int k = 0; k < combinations; ++k) + { + double c; + cin >> c; + cout << shortestPath(0, numCities - 1, c, numCities) << endl; + } + } + return 0; +} diff --git a/11849.cpp b/11849.cpp index 4bdfaaf..1a95e96 100644 --- a/11849.cpp +++ b/11849.cpp @@ -1,30 +1,31 @@ -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - int64 M, N, run, num, i, tot=0; - scanf("%lld%lld", &N, &M); - if(M==0 && N==0) - break; - vector vec(N); - for(i=0; i + +using namespace std; + +#define pb push_back +#define MAX 1000000 + +int main() +{ + long long int n, m; + while (scanf("%lld %lld", &n, &m) && (n || m)) + { + int a, val = 0; + map check; + for (long long int i = 1; i <= n; i++) + { + scanf("%d", &a); + check[a] = 1; + } + for (long long int i = 1; i <= m; i++) + { + scanf("%d", &a); + if (check[a] == 1) + { + val++; + } + } + cout << val << endl; + } + return 0; +} diff --git a/1185.cpp b/1185.cpp index 6aaaaab..e8f6380 100644 --- a/1185.cpp +++ b/1185.cpp @@ -1,23 +1,24 @@ -#include -#include +#include -#define MAX 10000000 -double Arr[MAX + 1]; +using namespace std; -void Cal() { - Arr[0] = 0; - for (int i = 1; i <= MAX; i++) - Arr[i] = Arr[i - 1] + log(i); -} +#define PI 3.141592653589793238462643383279502884197169399 +#define E 2.71828182845904523536028747135266249775724709369995 -int main() { - Cal(); - int T; - scanf("%d", &T); - while (T--) { - int N; - scanf("%d", &N); - printf("%.0lf\n", floor(Arr[N]/log(10.0) + 1.0)); +int main() +{ + int t, n; + cin >> t; + while (t--) + { + cin >> n; + if (n == 1) + { + cout << 1 << endl; + continue; + } + long long result = ceil(log10(2 * PI * n) / 2 + n * (log10(n / E))); + cout << result << endl; } return 0; } diff --git a/11850.cpp b/11850.cpp index 71704ec..564c0b7 100644 --- a/11850.cpp +++ b/11850.cpp @@ -1,30 +1,49 @@ -#include -#include -using namespace std; - -int main() { - while (true) { - int N, i; - scanf("%d", &N); - if (!N) break; - int road[N + 2]; - road[0] = 0; - road[N + 1] = 1422; - for (i = 1; i <= N; i++) - scanf("%d", &road[i]); - N += 2; - sort(road, road + N); - bool stat = true; - for (i = 1; i < N; i++) { - if(road[i] - road[i-1] > 200) { - stat = false; - break; - } - } - if (road[N - 1] - road[N - 2] > 100) - stat = false; - if(stat) printf("POSSIBLE\n"); - else printf("IMPOSSIBLE\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + int cur; + int location; + bool result; + vector stations; + vector dist; + while (cin >> n) + { + if (n == 0) + { + break; + } + stations.clear(); + dist.clear(); + while (n--) + { + cin >> location; + stations.push_back(location); + } + sort(stations.begin(), stations.end()); + cur = 0; + dist.push_back(stations[0] - cur); + for (int i = 1, sz = stations.size(); i < sz; i++) + { + dist.push_back(stations[i] - stations[i - 1]); + } + dist.push_back((1422 - stations[stations.size() - 1]) * 2); + for (int i = stations.size() - 1; i > 0; i--) + { + dist.push_back(stations[i] - stations[i - 1]); + } + sort(dist.begin(), dist.end()); + if (dist[dist.size() - 1] > 200) + { + cout << "IMPOSSIBLE" << endl; + } + else + { + cout << "POSSIBLE" << endl; + } + } + return 0; +} diff --git a/11851.cpp b/11851.cpp new file mode 100644 index 0000000..4313891 --- /dev/null +++ b/11851.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int n; +long long A[32], suffix[32], ret; +void dfs(int i, long long x, long long y, long long sold) +{ + if (sold >= ret || abs(x - y) > suffix[i]) + return; + if (x == y) + ret = min(ret, sold + suffix[i]); + if (i == n) + return; + if (x == y) + { + dfs(i + 1, x + A[i], y, sold); + dfs(i + 1, x, y, sold + A[i]); + } + else + { + dfs(i + 1, x + A[i], y, sold); + if (x) + dfs(i + 1, x, y + A[i], sold); + dfs(i + 1, x, y, sold + A[i]); + } +} +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; i++) + scanf("%lld", &A[i]); + sort(A, A + n, greater()); + suffix[n] = 0; + for (int i = n - 1; i >= 0; i--) + suffix[i] = suffix[i + 1] + A[i]; + ret = 1LL << 60; + dfs(0, 0, 0, 0); + printf("%lld\n", ret); + } + return 0; +} diff --git a/11852.cpp b/11852.cpp new file mode 100644 index 0000000..7e71338 --- /dev/null +++ b/11852.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +#define WHITE 0 +#define BLACK 1 + +int color(int x, int y) +{ + return (int)(abs(x) + abs(y)) % 2 == 0 ? WHITE : BLACK; +} +int minimumStep(int x, int y) +{ + int step; + x = abs(x); + y = abs(y); + if (x == 0 && y == 0) + { + return 0; + } + if (x == 1 && y == 0 || x == 0 && y == 1) + { + return 3; + } + if (x == 2 && y == 2) + { + return 4; + } + if (2 * x < y || 2 * y < x) + { + step = max(x, y) / 2; + if (max(x, y) % 2 != 0) + { + ++step; + } + } + else + { + step = (x + y) / 3; + if ((x + y) % 3 != 0) + { + ++step; + } + } + if (color(x, y) == color(0, 0)) + { + if (step % 2 != 0) + { + ++step; + } + } + else if (color(x, y) != color(0, 0)) + { + if (step % 2 == 0) + { + ++step; + } + } + return step; +} +int main() +{ + while (true) + { + int x, y; + string s; + getline(cin, s); + if (s == "END") + { + break; + } + istringstream iss(s); + iss >> x >> y; + int ans = minimumStep(x, y); + cout << ans << endl; + } + return 0; +} diff --git a/11853.cpp b/11853.cpp new file mode 100644 index 0000000..22810d3 --- /dev/null +++ b/11853.cpp @@ -0,0 +1,405 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define EQ(a, b) (fabs(a - b) < eps) +#define EQV(a, b) (EQ(real(a), real(b)) && EQ(imag(a), imag(b))) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back +#define mp make_pair + +typedef complex P; +typedef pair Line; + +const double eps = 1e-10; +const double pi = acos(-1); + +const int N = 1000 * 1000 * 2 * 2; +Line L, U, B, R; +vector edge[N]; +bool vis[N]; + +namespace std +{ + bool operator<(const P &a, const P &b) + { + if (!EQ(real(a), real(b))) + { + return real(a) < real(b); + } + if (!EQ(imag(a), imag(b))) + { + return imag(a) < imag(b); + } + return false; + } + bool operator==(const P &a, const P &b) + { + return EQV(a, b); + } +} + +int get_idx(P a, map &M, vector

&all) +{ + int index = M.size(); + if (M.find(a) == M.end()) + { + M[a] = index, all.pb(a); + } + return M[a]; +} + +pair check(double dx, double dy, double rx, double ry, double r, bool &flag) +{ + double a = (dx * dx + dy * dy), b = -2 * rx * dx - 2 * ry * dy, c = rx * rx + ry * ry - r * r; + double D = b * b - 4 * a * c; + if (D < 0) + { + flag = false; + } + else + { + flag = true; + return mp((-b - sqrt(D)) / (2 * a), (-b + sqrt(D)) / (2 * a)); + } +} + +bool isp(P a, P b, P c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + eps; +} + +int is_intersected_circle(P &a, P &b, double r1, double r2) +{ + double d = abs(a - b); + if (d < eps && abs(r1 - r2) < eps) + { + return 3; + } + if (d + r2 < r1) + { + return 0; + } + if (d + r1 < r2) + { + return 1; + } + if (d > r1 + r2) + { + return 4; + } + return 2; +} + +pair CC_intersection(P a, P b, double r1, double r2) +{ + double l, m, n; + double d = abs(a - b); + double x, y; + double p, q; + l = (d * d - r2 * r2 + r1 * r1) / (2 * d); + x = a.real() + (b.real() - a.real()) * l / d; + y = a.imag() + (b.imag() - a.imag()) * l / d; + n = sqrt(r1 * r1 - l * l); + p = (b.imag() - a.imag()) / d * n; + q = (b.real() - a.real()) / d * n; + return mp(P(x + p, y - q), P(x - p, y + q)); +} + +void sorting(vector &a, vector

&all, map &M) +{ + vector

tmp(a.size()); + rep(i, tmp.size()) tmp[i] = all[a[i]]; + sort(ALL(tmp)); + rep(i, tmp.size()) a[i] = get_idx(tmp[i], M, all); +} + +void proc_line(int n, P *in, double *r, map &M, + vector

&all, vector &s, vector &g, + vector &top, vector &bottom, + vector *cp) +{ + bottom.pb(get_idx(B.first, M, all)); + bottom.pb(get_idx(B.second, M, all)); + top.pb(get_idx(U.first, M, all)); + top.pb(get_idx(U.second, M, all)); + s.pb(get_idx(L.first, M, all)); + s.pb(get_idx(L.second, M, all)); + g.pb(get_idx(R.first, M, all)); + g.pb(get_idx(R.second, M, all)); + rep(i, n) + { + bool flag; + P ins[2]; + pair tmp; + tmp = check(1, 0, in[i].real() - B.first.real(), in[i].imag() - B.first.imag(), r[i], flag); + if (flag) + { + ins[0] = P(tmp.first, B.first.imag()); + ins[1] = P(tmp.second, B.first.imag()); + rep(j, 2) + { + if (isp(B.first, B.second, ins[j])) + { + bottom.pb(get_idx(ins[j], M, all)); + cp[i].pb(get_idx(ins[j], M, all)); + } + } + } + tmp = check(1, 0, in[i].real() - U.first.real(), in[i].imag() - U.first.imag(), r[i], flag); + if (flag) + { + ins[0] = P(tmp.first, U.first.imag()); + ins[1] = P(tmp.second, U.first.imag()); + rep(j, 2) + { + if (isp(U.first, U.second, ins[j])) + { + top.pb(get_idx(ins[j], M, all)); + cp[i].pb(get_idx(ins[j], M, all)); + } + } + } + tmp = check(0, 1, in[i].real() - L.first.real(), in[i].imag() - L.first.imag(), r[i], flag); + if (flag) + { + ins[0] = P(L.first.real(), tmp.first); + ins[1] = P(L.first.real(), tmp.second); + rep(j, 2) + { + if (isp(L.first, L.second, ins[j])) + { + s.pb(get_idx(ins[j], M, all)); + cp[i].pb(get_idx(ins[j], M, all)); + } + } + } + tmp = check(0, 1, in[i].real() - R.first.real(), in[i].imag() - R.first.imag(), r[i], flag); + if (flag) + { + ins[0] = P(R.first.real(), tmp.first); + ins[1] = P(R.first.real(), tmp.second); + rep(j, 2) + { + if (isp(R.first, R.second, ins[j])) + { + g.pb(get_idx(ins[j], M, all)); + cp[i].pb(get_idx(ins[j], M, all)); + } + } + } + } + sorting(bottom, all, M); + sorting(top, all, M); + sorting(s, all, M); + sorting(g, all, M); +} + +void proc_circle(int n, P *in, double *r, vector *cp, map &M, + vector

&all) +{ + int ret = 0; + rep(i, n) + { + rep(j, n) + { + if (is_intersected_circle(in[i], in[j], r[i], r[j]) == 2) + { + pair cc = CC_intersection(in[i], in[j], r[i], r[j]); + P ins[2] = {cc.first, cc.second}; + rep(k, 2) + { + if (ins[k].real() > 1000 || ins[k].imag() > 1000 || + ins[k].real() < 0 || ins[k].imag() < 0) + { + continue; + } + cp[i].pb(get_idx(ins[k], M, all)); + } + } + } + vector array(cp[i].size()); + rep(j, cp[i].size()) + { + array[j] = arg(all[cp[i][j]] - in[i]); + } + sort(ALL(array)); + rep(j, array.size()) + { + cp[i][j] = get_idx(in[i] + polar(r[i], array[j]), M, all); + } + } +} + +void make_circle(int n, P *in, double *r, vector *cp, + vector

&all) +{ + rep(i, n) + { + rep(j, cp[i].size()) + { + double tmpa = arg(all[cp[i][j]] - in[i]), tmpb = arg(all[cp[i][(j + 1) % cp[i].size()]] - in[i]); + if (tmpb < tmpa) + { + tmpb += 2 * pi; + } + double a = (tmpa + tmpb) / 2.; + P c = polar(r[i], a) + in[i]; + bool flag = true; + rep(k, n) + { + if (abs(in[k] - c) < r[k] - eps) + { + flag = false; + break; + } + } + if (c.real() > 1000 || c.imag() > 1000 || + c.real() < 0 || c.imag() < 0) + { + flag = false; + } + if (flag) + { + edge[cp[i][j]].pb(cp[i][(j + 1) % cp[i].size()]); + edge[cp[i][(j + 1) % cp[i].size()]].pb(cp[i][j]); + } + } + } +} + +void make_line(int n, P *in, double *r, vector &some, vector

&all) +{ + rep(i, some.size()) + { + P a = all[some[i]]; + bool flag = true; + rep(j, n) + { + if (abs(a - in[j]) + eps < r[j]) + { + flag = false; + break; + } + } + if (i + 1 == some.size()) + { + break; + } + P b = all[some[i + 1]]; + P mid = (a + b) / (double)2.; + rep(j, n) + { + if (abs(mid - in[j]) + eps < r[j]) + { + flag = false; + break; + } + } + if (flag) + { + edge[some[i]].pb(some[i + 1]); + edge[some[i + 1]].pb(some[i]); + } + } +} + +bool bfs(int s, vector &g, vector

&all) +{ + if (vis[s]) + { + return false; + } + queue Q; + Q.push(s); + vis[s] = true; + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + rep(i, edge[now].size()) + { + if (!vis[edge[now][i]]) + { + vis[edge[now][i]] = true; + Q.push(edge[now][i]); + } + } + } + rep(i, g.size()) + { + if (vis[g[i]]) + { + printf("%.2lf %.2lf %.2lf %.2lf\n", + all[s].real(), all[s].imag(), all[g[i]].real(), all[g[i]].imag()); + return true; + } + } + return false; +} + +template +void op(T a, int n, vector

&all) +{ + rep(i, n) + { + cout << all[a[i]]; + } + printf("\n"); +} + +void solve(int n) +{ + P in[n]; + double r[n]; + map M; + vector

all; + vector s, g, top, bottom; + vector cp[n]; + rep(i, n) + { + double re, im; + scanf("%lf%lf%lf", &re,&im,&r[i]); + in[i] = P(re,im); + } + proc_line(n, in, r, M, all, s, g, top, bottom, cp); + proc_circle(n, in, r, cp, M, all); + int node = M.size(); + rep(i, node) + { + vis[i] = false; + edge[i].clear(); + } + make_circle(n, in, r, cp, all); + make_line(n, in, r, top, all); + make_line(n, in, r, bottom, all); + make_line(n, in, r, s, all); + make_line(n, in, r, g, all); + reverse(ALL(s)); + reverse(ALL(g)); + double anss = -1, ansg = -1; + rep(i, s.size()) + { + if (bfs(s[i], g, all)) + { + return; + } + } + printf("IMPOSSIBLE\n"); +} + +int main() +{ + U = mp(P(0, 1000), P(1000, 1000)); + B = mp(P(0, 0), P(1000, 0)); + L = mp(P(0, 0), P(0, 1000)); + R = mp(P(1000, 0), P(1000, 1000)); + int n; + while (scanf("%d", &n) == 1) + { + solve(n); + } + return 0; +} diff --git a/11854.cpp b/11854.cpp index 87aec8c..deb8018 100644 --- a/11854.cpp +++ b/11854.cpp @@ -1,16 +1,32 @@ -#include -typedef long long int64; -int main(){ - while(true){ - int64 A, B, C; - scanf("%lld%lld%lld", &A, &B, &C); - if(A==0 && B==0 && C==0) - break; - int64 Asq=A*A, Bsq=B*B, Csq=C*C; - if(Asq+Bsq==Csq || Asq+Csq==Bsq || Csq+Bsq==Asq) - printf("right\n"); - else - printf("wrong\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int a, b, c; + while (cin >> a >> b >> c) + { + if (a == 0 && b == 0 && c == 0) + { + break; + } + if (a > b) + { + swap(a, b); + } + if (b > c) + { + swap(b, c); + } + if (c * c == a * a + b * b) + { + cout << "right" << endl; + } + else + { + cout << "wrong" << endl; + } + } + return 0; +} diff --git a/11855.cpp b/11855.cpp new file mode 100644 index 0000000..b517f74 --- /dev/null +++ b/11855.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < n; i++) + +struct SAComp +{ + const int h, *g; + SAComp(int h, int *g) : h(h), g(g) {} + bool operator()(int a, int b) + { + return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a + h] < g[b + h]; + } +}; + +int g[100100], b[100100], v[100100]; + +int *buildSA(char *t, int n) +{ + REP(i, n + 1) + v[i] = i, + g[i] = t[i]; + b[0] = 0; + b[n] = 0; + sort(v, v + n + 1, SAComp(0, g)); + for (int h = 1; b[n] != n; h *= 2) + { + SAComp comp(h, g); + sort(v, v + n + 1, comp); + REP(i, n) + b[i + 1] = b[i] + comp(v[i], v[i + 1]); + REP(i, n + 1) + g[v[i]] = b[i]; + } + return v; +} + +// Naive matching O(m log n) +int find(char *t, int n, char *p, int m, int *sa) +{ + int a = 0, b = n; + while (a < b) + { + int c = (a + b) / 2; + if (strncmp(t + sa[c], p, m) < 0) + { + a = c + 1; + } + else + { + b = c; + } + } + return strncmp(t + sa[a], p, m) == 0 ? sa[a] : -1; +} + +int lcp[100100]; + +// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n) +int *buildLCP(char *t, int n, int *a) +{ + int h = 0, b[n + 1]; + REP(i, n + 1) + b[a[i]] = i; + REP(i, n + 1) + { + if (b[i]) + { + for (int j = a[b[i] - 1]; j + h < n && i + h < n && t[j + h] == t[i + h]; ++h) + ; + lcp[b[i]] = h; + } + else + { + lcp[b[i]] = -1; + } + if (h > 0) + { + --h; + } + } + return lcp; +} + +// call RMQ = buildRMQ(lcp, n+1) +int *buildRMQ(int *a, int n) +{ + int logn = 1; + for (int k = 1; k < n; k *= 2) + { + ++logn; + } + int *r = new int[n * logn]; + int *b = r; + copy(a, a + n, b); + for (int k = 1; k < n; k *= 2) + { + copy(b, b + n, b + n); + b += n; + REP(i, n - k) + b[i] = min(b[i], b[i + k]); + } + return r; +} + +// inner LCP computation with RMQ: O(1) +int minimum(int x, int y, int *rmq, int n) +{ + int z = y - x, k = 0, e = 1, s; + s = ((z & 0xffff0000) != 0) << 4; + z >>= s; + e <<= s; + k |= s; + s = ((z & 0x0000ff00) != 0) << 3; + z >>= s; + e <<= s; + k |= s; + s = ((z & 0x000000f0) != 0) << 2; + z >>= s; + e <<= s; + k |= s; + s = ((z & 0x0000000c) != 0) << 1; + z >>= s; + e <<= s; + k |= s; + s = ((z & 0x00000002) != 0) << 0; + z >>= s; + e <<= s; + k |= s; + return min(rmq[x + n * k], rmq[y + n * k - e + 1]); +} + +// outer LCP computation: O(m - o) +int computeLCP(char *t, int n, char *p, int m, int o, int k) +{ + int i = o; + for (; i < m && k + i < n && p[i] == t[k + i]; ++i) + ; + return i; +} + +// Mamber-Myers's O(m + log n) string matching with LCP/RMQ +#define COMP(h, k) (h == m || (k + h < n && p[h] < t[k + h])) +int find(char *t, int n, char *p, int m, int *sa, int *rmq) +{ + int l = 0, lh = 0, r = n, rh = computeLCP(t, n + 1, p, m, 0, sa[n]); + if (!COMP(rh, sa[r])) + { + return -1; + } + for (int k = (l + r) / 2; l + 1 < r; k = (l + r) / 2) + { + int A = minimum(l + 1, k, rmq, n + 1), B = minimum(k + 1, r, rmq, n + 1); + if (A >= B) + { + if (lh < A) + { + l = k; + } + else if (lh > A) + { + r = k, rh = A; + } + else + { + int i = computeLCP(t, n + 1, p, m, A, sa[k]); + if (COMP(i, sa[k])) + { + r = k, rh = i; + } + else + { + l = k, lh = i; + } + } + } + else + { + if (rh < B) + { + r = k; + } + else if (rh > B) + { + l = k, lh = B; + } + else + { + int i = computeLCP(t, n + 1, p, m, B, sa[k]); + if (COMP(i, sa[k])) + { + r = k, rh = i; + } + else + { + l = k, lh = i; + } + } + } + } + return rh == m ? sa[r] : -1; +} + +char entradaCE[1010]; +char entrada[1010]; + +int main() +{ + while (true) + { + cin.getline(entradaCE, 1010); + int tam = strlen(entradaCE); + int act = 0; + for (int i = 0; i < tam + 1; i++) + { + if (entradaCE[i] != ' ') + { + entrada[act++] = entradaCE[i]; + } + } + tam = strlen(entrada) - 1; + if (tam == -1) + { + return 0; + } + entrada[tam + 1] = '$'; + entrada[tam + 2] = '\0'; + tam += 1; + int *sa = buildSA(entrada, tam); + int *lcp = buildLCP(entrada, tam, sa); + for (int i = 1; i <= tam + 1; i++) + { + int cuenta = 1; + int maximo = 0; + for (int j = 1; j <= tam; j++) + { + if (lcp[j] >= i) + { + cuenta++; + } + else + { + cuenta = 1; + } + if (cuenta > maximo) + { + maximo = cuenta; + } + } + if (maximo > 1) + { + cout << maximo << endl; + } + if (maximo == 0) + { + break; + } + } + cout << endl; + } +} diff --git a/11856.cpp b/11856.cpp index 8cd772f..162d6d8 100644 --- a/11856.cpp +++ b/11856.cpp @@ -1,39 +1,58 @@ -#include -using namespace std; - -int main(){ - while(true){ - int N, i, w; - cin>>N; - if(N == 0) - break; - double price[N+1]; - double nMem, MW=0; - for(i=1; i<=N; i++){ - cin>>price[i]; - price[i]*=10; - MW += price[i]; - } - sort(price+1,price+N+1); - double dVal = MW; - MW /= 2; -// MW*=10; - int mat[N+1][(int)MW+1]; - for(i=0; i<=N; i++) - mat[i][0] = 0; - for(w=0; w<=(int)MW; w++) - mat[0][w] = 0; - for(i=1; i<=N; i++){ - for(w=1; w<=(int)MW; w++){ - if(price[i] > w) - mat[i][w] = mat[i-1][w]; - else - mat[i][w] = max(mat[i-1][w], mat[i-1][w-(int)price[i]] + (int)price[i]); - } - } - cout<<(dVal-2*mat[N][(int)MW])/10.0; -// totVal += mat[N][(int)MW]; -// cout< + +using namespace std; + +char tmp[1000]; +double sum, r[100]; +int i, j, k, m, n, R[100], prev_[20001], who[20001]; + +int main() +{ + while (1 == scanf("%d", &n) && n) + { + for (i = 0; i < n; i++) + { + scanf("%lf", &r[i]); + } + for (i = sum = 0; i < n; i++) + { + sum += r[i]; + } + for (i = 0; i < n; i++) + { + R[i] = r[i] * 20000 / sum; + } + memset(who, 0, sizeof(who)); + memset(prev_, 0, sizeof(prev_)); + who[0] = -1; + for (i = 0; i < n; i++) + { + for (j = 10000; j >= 0; j--) + { + if (!who[j]) + { + continue; + } + if (who[j + R[i]]) + { + continue; + } + who[j + R[i]] = i + 1; + prev_[j + R[i]] = j; + } + } + for (i = 10000; !who[i]; i--) + ; + while (i) + { + printf("%d ", who[i]); + i = prev_[i]; + } + printf("\n"); + } + if (n != 0) + { + printf("missing end delimiter\n"); + } + return 0; +} diff --git a/11857.cpp b/11857.cpp index d19b1ba..bc29a25 100644 --- a/11857.cpp +++ b/11857.cpp @@ -1,72 +1,102 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int64 PINF = 999999999; -int64 NINF = -999999999; - -int main(){ - while(true){ - int64 N, M, i, j, k; - cin>>N>>M; - if(N==0 && M==0) - break; - int64 mat[N][N], minm = NINF; - for(i=0; i>X>>Y>>val; - mat[X][Y] = val; - mat[Y][X] = val; - } - for(k=0; k + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int n, m; + int a, b, c; + while (cin >> n >> m) + { + if (!n && !m) + { + break; + } + UnionFindDisjointSets ds(n); + priority_queue>> edgeList; + while (m--) + { + cin >> a >> b >> c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + int longest = 0; + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + if (-front.first > longest) + { + longest = -front.first; + } + ds.unionSet(front.second.first, front.second.second); + } + } + if (ds.sizeOfSet(0) != n) + { + cout << "IMPOSSIBLE" << endl; + } + else + { + cout << longest << endl; + } + } + return 0; +} diff --git a/11858.cpp b/11858.cpp new file mode 100644 index 0000000..093397b --- /dev/null +++ b/11858.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +unsigned long long result; + +template +void merge_sort(RandIter b, RandIter e, Less c) +{ + if (b >= e - 1) + return; + + typedef typename iterator_traits::value_type Value; + RandIter i = b + (e - b) / 2; + merge_sort(b, i, c); + merge_sort(i, e, c); + vector L, R; + for (RandIter j = b; j != i; j++) + L.push_back(*j); + for (RandIter j = i; j != e; j++) + R.push_back(*j); + int sz = L.size(); + RandIter l = L.begin(), r = R.begin(); + for (RandIter j = b; j != e; j++) + { + if (l == L.end()) + *j = *r++; + else if (r == R.end()) + *j = *l++; + else + { + if (c(*l, *r)) + { + *j = *l++; + sz--; + } + else + { + *j = *r++; + result += sz; + } + } + } +} + +int main() +{ + int n, tmp; + vector nums(1000005); + while (cin >> n) + { + if (n == 0) + break; + result = 0; + for (int i = 0; i < n; i++) + cin >> nums[i]; + merge_sort(nums.begin(), nums.begin() + n, less_equal()); + cout << result << endl; + } + return 0; +} diff --git a/1186.cpp b/1186.cpp new file mode 100644 index 0000000..d838d9c --- /dev/null +++ b/1186.cpp @@ -0,0 +1,281 @@ +#include + +using namespace std; + +#define S(a) scanf("%d", &a) +#define P(a) printf("%d", a) +#define PS(a) printf("%s", a) + +#define S2(a, b) scanf("%d%d", &a, &b) +#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c) + +#define SLL(a) scanf("%lld", &a) +#define PLL(a) printf("%lld", a) +#define SLL2(a, b) scanf("%lld%lld", &a, &b) +#define SLL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c) + +#define PCASE printf("Case %d: ", kk++) +#define PCASENL printf("Case %d:\n", kk++) +#define NL puts("") + +#define sz(a) ((int)a.size()) +#define repv(i, a) for (int i = 0; i < sz(a); i++) +#define revv(i, a) for (int i = sz(a) - 1; i >= 0; i--) +#define rep(i, a, b) for (int i = a; i <= b; i++) +#define rev(i, a, b) for (int i = a; i >= b; i--) +#define FOR(I, A, B) for (int I = (A); I < (B); ++I) +#define REP(I, N) FOR(I, 0, N) +#define all(a) a.begin(), a.end() +#define rall(a) a.rbegin(), a.rend() +#define countbit(x) __builtin_popcount(x) +#define READ(f) freopen(f, "r", stdin) +#define WRITE(f) freopen(f, "w", stdout) +#define pb(a) push_back(a) +#define mp(a, b) make_pair(a, b) +#define pi (2.0 * acos(0.0)) +#define PI (2.0 * acos(0.0)) +#define SET(a) memset(a, -1, sizeof a) +#define CLR(a) memset(a, 0, sizeof a) +#define set0(ar) memset(ar, 0, sizeof ar) +#define setinf(ar) memset(ar, 126, sizeof ar) +#define in(a, x, y) (a >= x && a <= y) +#define out(a, x, y) (!in(a, x, y)) +#define eq(a, b) (fabs(a - b) < eps) +#define less(a, b) (a + eps < b) +#define great(a, b) (a > b + eps) +#define xx first +#define yy second +#define root 1 + +#define MAX(a) (*max_element(all(a))) +#define MIN(a) (*min_element(all(a))) + +#define LB(a, x) (lower_bound(all(a), x) - a.begin()) +#define UB(a, x) (upper_bound(all(a), x) - a.begin()) + +int ts, kk = 1; + +typedef long long ll; +typedef long long LL; +typedef unsigned long long ull; +typedef vector vi; +typedef vector vvi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector vii; + +template +inline T _sq(T a) +{ + return a * a; +} +template +inline T _pow(T a, X y) +{ + T z = 1; + rep(i, 1, y) + { + z *= a; + } + return z; +} +//template< class T > inline T _gcd(T a,T b) {a=_abs(a);b=_abs(b); if(!b) return a; return _gcd(b,a%b);} +//template< class T > inline T _lcm(T a,T b) {a=_abs(a);b=_abs(b); return (a/_gcd(a,b))*b;} + +template +inline T _extended(T a, T b, T &x, T &y) +{ + a = _abs(a); + b = _abs(b); + T g, x1, y1; + if (!b) + { + x = 1; + y = 0; + g = a; + return g; + } + g = _extended(b, a % b, x1, y1); + x = y1; + y = x1 - (a / b) * y1; + return g; +} + +template +inline T ithroot(T x, X i) +{ + return (T)(pow((double)x, 1.0 / (double)i) + (1e-9)); +} + +template +inline bool getbit(T a, X i) +{ + T t = 1; + return ((a & (t << i)) > 0); +} +template +inline T setbit(T a, X i) +{ + T t = 1; + return (a | (t << i)); +} +template +inline T resetbit(T a, X i) +{ + T t = 1; + return (a & (~(t << i))); +} +template +inline T togglebit(T a, X i) +{ + T t = 1; + return (a ^ (t << i)); +} + +template +inline T bigmod(T n, X m, Y mod) +{ + ull ret = 1, a = n % mod; + while (m) + { + if (m & 1) + { + ret = (ret * a) % mod; + } + m >>= 1; + a = (a * a) % mod; + } + ret %= mod; + return (T)ret; +} +template +inline T modinv(T n, Y mod) +{ + return bigmod(n, mod - 2, mod); +} + +template +inline void unify(vector &a) +{ + sort(all(a)); + a.erase(unique(all(a)), a.end()); +} + +/// //CONSTANT// /// +/// power 01234567890123456789 /// +#define MX (32767 + 10) +#define MOD 1000000007 +#define inf 1000000000 +#define lim 4000100 +#define LOGMX 20 +#define eps 1e-8 +///===========================/// + +int n; + +vector s; +vi f; + +bool isconst[300]; + +bool chk1() +{ + for (int i = 0; i + 6 - 1 < sz(s.back()); i++) + { + int cnt = 0; + rep(j, 0, 5) + { + cnt += isconst[s.back()[i + j]]; + } + if (cnt == 6) + { + return false; + } + } + return true; +} +bool chk2() +{ + for (int i = 0; i + 5 - 1 < sz(s.back()); i++) + { + int cnt = 0; + rep(j, 0, 4) + { + cnt += isconst[s.back()[i + j]]; + } + if (cnt == 5) + { + f.pb(1); + break; + } + } + if (sz(f) != sz(s)) + { + f.pb(0); + } + int cnt = 0; + if (f.back()) + { + for (int i = sz(f) - 2, j = 0; i >= 0 && j < 10; i--, j++) + { + cnt += f[i]; + if (cnt > 2) + { + break; + } + } + } + if (cnt > 2) + { + return false; + } + return true; +} + +bool chk3() +{ + int cnt = 0; + for (int i = sz(s) - 2, j = 0; i >= 0 && j < 10; i--, j++) + { + cnt += s.back() == s[i]; + if (cnt > 1) + { + break; + } + } + if (cnt > 1) + { + return false; + } + return true; +} + +int main() +{ + rep(x, 'a', 'z') + { + isconst[x - 'a' + 'A'] = isconst[x] = !(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'y'); + } + while (~scanf("%d", &n)) + { + s.clear(); + f.clear(); + getchar(); + rep(i, 0, n - 1) + { + string r; + getline(cin, r); + s.pb(r); + bool a = chk1(), b = chk2(), c = chk3(); + if (a && b && c) + { + puts("y"); + } + else + { + puts("n"); + } + } + } +} diff --git a/11860.cpp b/11860.cpp new file mode 100644 index 0000000..5c8ad4b --- /dev/null +++ b/11860.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +int main() +{ + int t; + int *words = new int[100001]; + map m; + string input; + int wordsnum, counter; + cin >> t; + for (int case_num = 0; case_num < t; case_num++) + { + m.clear(); + counter = 0; + wordsnum = 0; + while (getline(cin, input)) + { + if (input == "END") + { + break; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] < 'a' || input[i] > 'z') + { + input[i] = ' '; + } + } + stringstream ss(input, stringstream::in); + while (ss >> input) + { + if (m.find(input) == m.end()) + { + m[input] = counter; + words[wordsnum] = counter; + counter++; + wordsnum++; + } + else + { + words[wordsnum] = m[input]; + wordsnum++; + } + } + } + int a = 0, b = 0; + int haswords = 0; + map current; + pair result = make_pair(-1, -1); + current[words[0]] = 1; + haswords++; + if (wordsnum == 1) + { + result = make_pair(0, 0); + } + while (true) + { + if (a == wordsnum - 1 && b == wordsnum - 1) + { + break; + } + if (haswords == counter) + { + if (result.first == -1) + { + result = make_pair(a, b); + } + else + { + if (b - a < result.second - result.first) + { + result = make_pair(a, b); + } + } + if (current[words[a]] > 1) + { + current[words[a]] -= 1; + a += 1; + } + else + { + current[words[a]] = 0; + a += 1; + haswords--; + } + } + else + { + if (b == wordsnum - 1) + { + break; + } + if (current.find(words[b + 1]) == current.end() || current[words[b + 1]] == 0) + { + current[words[b + 1]] = 1; + b += 1; + haswords++; + } + else + { + current[words[b + 1]] += 1; + b += 1; + } + } + } + cout << "Document " << case_num + 1 << ": " << result.first + 1 << " " << result.second + 1 << endl; + } + delete[] words; + return 0; +} diff --git a/11863.cpp b/11863.cpp new file mode 100644 index 0000000..cd6bd47 --- /dev/null +++ b/11863.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) + +bool prime[101000]; +void crivo() +{ + memset(prime, 0, sizeof prime); + prime[2] = 1; + for (int i = 3; i < 100010; i += 2) + prime[i] = 1; + for (int i = 3; i < 400; i += 2) + { + if (prime[i]) + { + for (int j = i * i; j < 100010; j += i) + prime[j] = 0; + } + } +} + +int dp[110][110], x[110]; +bool go(int i, int j) +{ + if (~dp[i][j]) + return dp[i][j]; + if (i >= j) + return dp[i][j] = 0; + + bool vld = 0; + int sum = 0; + bool win = 0; + for (int k = i; !win && k < j; k++) + { + if (x[k] == 42) + vld = 1; + sum += x[k]; + if (vld || (sum > 0 && prime[sum])) + win |= !go(k + 1, j); + } + vld = 0; + sum = 0; + for (int k = j - 1; !win && k >= i; k--) + { + if (x[k] == 42) + vld = 1; + sum += x[k]; + if (vld || (sum > 0 && prime[sum])) + win |= !go(i, k); + } + //printf("%d %d > %d\n", i, j, win); + return dp[i][j] = win; +} + +int main() +{ + crivo(); + int cas = 1; + int T, N, K; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &K); + rp(i, N) scanf("%d", &x[i]); + memset(dp, -1, sizeof dp); + printf("Case %d: ", cas++); + if (go(0, N)) + printf("Soha\n"); + else + printf("Tara\n"); + } + return 0; +} \ No newline at end of file diff --git a/11865.cpp b/11865.cpp new file mode 100644 index 0000000..fc8989d --- /dev/null +++ b/11865.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define MAXN 101 +#define INF 1e10 + +struct POINT +{ + int x, y; +} poi[MAXN]; +double gmap[MAXN][MAXN], ans; +int n, m, father[MAXN]; +bool vis[MAXN], circle[MAXN]; + +double length(POINT &a, POINT &b) +{ + return sqrt(double((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); +} + +void dfs(int x) +{ + vis[x] = true; + for (int i = 1; i <= n; ++i) + if (!vis[i] && gmap[x][i] < INF) + dfs(i); +} + +bool connect() +{ + memset(vis, 0, sizeof(vis)); + dfs(1); + for (int i = 1; i <= n; ++i) + if (!vis[i]) + return false; + return true; +} + +int exist_circle() +{ + father[1] = 1; + for (int i = 2; i <= n; ++i) + { + if (!circle[i]) + { + father[i] = i; + gmap[i][i] = INF; + for (int j = 1; j <= n; ++j) + if (!circle[j] && gmap[j][i] < gmap[father[i]][i]) + father[i] = j; + } + } + for (int i = 2; i <= n; ++i) + { + if (circle[i]) + continue; + memset(vis, 0, sizeof(vis)); + int j = i; + while (!vis[j]) + { + vis[j] = true; + j = father[j]; + } + if (j == 1) + continue; + else + return j; + } + return -1; +} + +void update(int t) +{ + ans += gmap[father[t]][t]; + for (int i = father[t]; i != t; i = father[i]) + { + ans += gmap[father[i]][i]; + circle[i] = true; + } + for (int i = 1; i <= n; ++i) + if (!circle[i] && gmap[i][t] != INF) + gmap[i][t] -= gmap[father[t]][t]; + for (int i = father[t]; i != t; i = father[i]) + { + for (int j = 1; j <= n; ++j) + { + if (circle[j]) + continue; + if (gmap[j][i] != INF) + gmap[j][t] = min(gmap[j][t], gmap[j][i] - gmap[father[i]][i]); + gmap[t][j] = min(gmap[i][j], gmap[t][j]);//æ›´æ–°å‡ºè¾¹æƒ + } + } +} + +void solve() +{ + memset(circle, 0, sizeof(circle)); + int t; + while ((t = exist_circle()) != -1) + update(t); + for (t = 2; t <= n; ++t) + if (!circle[t]) + ans += gmap[father[t]][t];//把所有ä¸æˆçŽ¯çš„è¾¹æƒåŠ ä¸ŠåŽ» +} + +struct Edge +{ + int u, v, flow, cost; + void get() + { + scanf("%d %d %d %d", &u, &v, &flow, &cost); + v++, u++; + } +} edge[11000]; + +int max_cost; + +bool build(int mid) +{ + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + gmap[i][j] = INF; + for (int i = 0; i < m; i++) + { + if (edge[i].flow >= mid) + { + int u = edge[i].u, v = edge[i].v, cost = edge[i].cost; + if (gmap[u][v] > cost) + gmap[u][v] = cost; + } + } + if (!connect()) + return false; + ans = 0; + solve(); + return ans <= max_cost; +} + +void binary_search() +{ + int left = 1, right = 1000100; + while (left <= right) + { + int mid = (left + right) >> 1; + if (build(mid)) + left = mid + 1; + else + right = mid - 1; + } + if (right) + printf("%d kbps\n", right); + else + puts("streaming not possible."); +} + +int main() +{ + int i, T; + scanf("%d", &T); + while (T--) + { + scanf("%d %d %d", &n, &m, &max_cost); + for (i = 0; i < m; ++i) + edge[i].get(); + binary_search(); + } + return 0; +} diff --git a/11866.cpp b/11866.cpp index 149ee01..4e2bbb3 100644 --- a/11866.cpp +++ b/11866.cpp @@ -1,49 +1,54 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int64 test; - cin>>test; - while(test--){ - int64 X, Y, tot=0; - int64 i, j, k; - cin >> X >> Y ; - for(i=X; i<=Y; i++){ - for(j=i; j<=Y; j++){ - int64 val = min(i+j-1, Y) - j + 1; - tot += val; - } - } - cout << tot << endl; - } - return 0; -} +#include + +using namespace std; + +typedef long long int Integer; + +Integer f(Integer n) +{ + return n * (n + 1) / 2; +} + +#define MAX 1000001 +int main() +{ + int n; + Integer *F = new Integer[MAX]; + Integer dpn; + Integer *DP = new Integer[MAX]; + F[1] = 1; + dpn = 0; + DP[0] = 0; + for (n = 1; n < MAX; ++n) + { + dpn = f(n) - dpn; + DP[n] = DP[n - 1] + dpn; + if (n > 1) + { + F[n] = F[n - 1] + f(n); + } + } + cin >> n; + for (int i = 0; i < n; ++i) + { + Integer X, Y; + Integer tX; + cin >> X >> Y; + if (Y < X) + { + swap(X, Y); + } + --X; + tX = 2 * X + 1; + if (tX >= Y) + { + tX = Y; + X = Y - (1 + X); + } + Integer ans = F[X + 1] + (DP[Y] - DP[tX]) - f(X) * (Y - tX); + cout << ans << endl; + } + delete[] F; + delete[] DP; + return 0; +} diff --git a/11867.cpp b/11867.cpp new file mode 100644 index 0000000..7e1c359 --- /dev/null +++ b/11867.cpp @@ -0,0 +1,206 @@ +#include + +using namespace std; + +#define i64 long long +#define u32 unsigned int +#define min(x, y) ((x) < (y) ? (x) : (y)) + +int C, num = 0; + +const int MAX = 1000005; +u32 tag[MAX / 64 + 5]; +int prime[MAX / 10], cnt; + +double Log[50]; + +void init() +{ + prime[cnt++] = 2; + int i, j; + for (i = 3; i <= MAX; i += 2) + if (!(tag[i >> 6] & (1 << (i / 2 % 32)))) + { + prime[cnt++] = i; + for (j = i * 3; j <= MAX; j += i * 2) + { + tag[j >> 6] |= (1 << (j / 2 % 32)); + } + } + for (i = 1; i < 50; i++) + { + Log[i] = log(1.0 * prime[i]); + } +} + +const i64 MOD = 1000000007; +i64 n; +vector V; + +i64 POW(i64 a, i64 b) +{ + i64 ans = 1; + while (b) + { + if (b & 1) + { + ans = ans * a % MOD; + } + a = a * a % MOD; + b >>= 1; + } + return ans; +} + +i64 t, p[50], q[50], Sum; + +void DFS(int dep, i64 cur) +{ + if (dep == t) + { + if (cur > 1) + { + V.push_back(cur); + } + return; + } + i64 i; + for (i = 0; i <= q[dep]; i++) + { + DFS(dep + 1, cur); + cur *= p[dep]; + } +} + +double f[4005][45]; +i64 Ans[4005][45], po[4005][45]; +vector mul[4005][2]; + +i64 DP() +{ + int i, j, k, p, q, S1 = V.size(), S2; + i64 t; + double temp; + for (i = 0; i < S1; i++) + for (j = 1; j <= Sum; j++) + { + po[i][j] = POW(prime[j], V[i] - 1); + } + for (i = 0; i < S1; i++) + for (j = 0; j <= Sum; j++) + { + f[i][j] = 1e40; + } + for (i = 0; i < S1; i++) + { + f[i][1] = 1.0 * (V[i] - 1) * Log[1]; + Ans[i][1] = po[i][1]; + for (k = 1; k + 1 <= Sum && f[i][k] < 1e30; k++) + { + S2 = mul[i][0].size(); + for (j = 0; j < S2; j++) + { + p = mul[i][0][j]; + q = mul[i][1][j]; + t = V[p]; + temp = f[i][k] + 1.0 * (V[q] - 1) * Log[k + 1]; + if (temp < f[p][k + 1]) + { + f[p][k + 1] = temp; + Ans[p][k + 1] = Ans[i][k] * po[q][k + 1] % MOD; + } + } + } + } + temp = 1e40; + k = V.size() - 1; + for (i = 1; i <= Sum; i++) + if (f[k][i] < temp) + { + temp = f[k][i]; + j = i; + } + return Ans[k][j]; +} + +int find(int low, int high, i64 k) +{ + int mid; + while (low <= high) + { + mid = (low + high) >> 1; + if (V[mid] == k) + { + return mid; + } + if (V[mid] > k) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } +} + +i64 get() +{ + if (n == 1) + { + return 1; + } + V.clear(); + i64 i, j, temp = n; + t = 0; + Sum = 0; + for (i = 0; i < cnt && (i64)prime[i] * prime[i] <= temp; i++) + if (temp % prime[i] == 0) + { + p[t] = prime[i]; + q[t] = 0; + while (temp % prime[i] == 0) + { + q[t]++; + temp /= prime[i]; + } + Sum += q[t]; + t++; + } + if (temp > 1) + { + p[t] = temp, q[t++] = 1, Sum++; + } + V.clear(); + DFS(0, 1); + sort(V.begin(), V.end()); + for (i = 0; i < V.size(); i++) + { + mul[i][0].clear(); + mul[i][1].clear(); + for (j = i + 1; j < V.size(); j++) + if (V[j] % V[i] == 0) + { + mul[i][0].push_back(j); + mul[i][1].push_back(find(0, V.size() - 1, V[j] / V[i])); + } + } + return DP(); +} + +int main() +{ + init(); + for (scanf("%d", &C); C--;) + { + scanf("%lld", &n); + if (n < 0) + { + printf("impossible\n"); + continue; + } + n++; + printf("%lld\n", get()); + } + return 0; +} diff --git a/11869.cpp b/11869.cpp index 17e7f1e..619c430 100644 --- a/11869.cpp +++ b/11869.cpp @@ -1,68 +1,114 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - while(true){ - int N, B, i, j; - cin>>N>>B; - if(N==0 && B==0) - break; - bool stat[N+1]; - int ball[B]; - memset(stat, false, sizeof(stat)); - FOI(i, 0, B){ - cin>>ball[i]; - stat[ball[i] % (N+1)] = true; - } - FOI(i, 0, B-1){ - FOI(j, i+1, B){ - int ind = abs(ball[i]-ball[j]); - stat[ind % (N+1)] = true; - } - } - bool flag=true; - FOI(i, 0, N+1){ - if(!stat[i]){ - flag = false; - break; - } - } - if(flag) - cout<<"Y\n"; - else - cout<<"N\n"; - } - return 0; -} +#include + +using namespace std; + +struct Node +{ + string name; + map attr; + vector son; + void init() + { + attr.clear(); + son.clear(); + } +}; +Node node[32767]; +int nodesize; +char s[1024]; +void parsingAttr(string line, Node &p) +{ + string ign = "<>=", attr, val; + for (int i = 0; i < line.length(); i++) + { + if (ign.find(line[i]) != string::npos) + { + line[i] = ' '; + } + } + stringstream sin(line); + sin >> p.name; + while (sin >> attr >> val) + { + p.attr[attr] = val.substr(1, val.length() - 2); + // cout << attr << "+++" << val << endl; + } +} +int build(string line) +{ + int label = ++nodesize; + Node &p = node[label]; + p.init(); + parsingAttr(line, p); + while (gets(s)) + { + if (s[1] == '/') + break; + p.son.push_back(build(s)); + } + return label; +} +int main() +{ + int testcase, cases = 0, n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + while (getchar() != '\n') + ; + gets(s); + nodesize = 0; + int root = build(s); + scanf("%d", &m); + while (getchar() != '\n') + ; + printf("Case %d:\n", ++cases); + while (m--) + { + gets(s); + string ign = ".[]", token; + for (int i = 0; s[i]; i++) + { + if (ign.find(s[i]) != string::npos) + { + s[i] = ' '; + } + } + stringstream sin(s); + int pos = root; + string res = "Undefined"; + sin >> token; + if (token == node[pos].name) + { + while (sin >> token) + { + if (token[0] == '"') + { + string a = token.substr(1, token.length() - 2); + if (node[pos].attr.find(a) != node[pos].attr.end()) + res = node[pos].attr[a]; + break; + } + else + { + int f = 0; + for (int i = 0; i < node[pos].son.size(); i++) + { + if (token == node[node[pos].son[i]].name) + { + f = 1; + pos = node[pos].son[i]; + break; + } + } + if (!f) + break; + } + } + } + printf("%s\n", res.c_str()); + } + } + return 0; +} diff --git a/1187.cpp b/1187.cpp new file mode 100644 index 0000000..f637c10 --- /dev/null +++ b/1187.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int main() +{ + int R, C, n = 0; + int A[32767], Dx[32767], Dy[32767]; + scanf("%d %d", &R, &C); + while (scanf("%d %d", &Dx[n], &Dy[n]) == 2) + { + if (Dx[n] < 0 || Dx[n] >= C || Dy[n] < 0 || Dy[n] >= R) + continue; + n++; + } + int Rmin[32767], Pmax[32767]; + for (int i = 1; i <= n; i++) + Rmin[i] = 0, Pmax[i] = 0; + for (int i = 0; i < C; i++) + { + for (int j = 0; j < R; j++) + { + for (int k = 0; k < n; k++) + A[k] = abs(Dx[k] - i) + abs(Dy[k] - j); + sort(A, A + n); + // printf("(%d, %d) ", i, j); + // for (int k = 0; k < n; k++) + // printf("%d ", A[k]); + // puts(""); + for (int k = 0; k < n; k++) + { + int base = k; + while (k + 1 < n && A[base] == A[k + 1]) + k++; + for (int p = k; p >= base; p--) + { + if (max(A[k], 1) > Rmin[p + 1]) + { + Rmin[p + 1] = max(A[k], 1); + } + } + Pmax[A[k]] = max(Pmax[A[k]], k + 1); + } + } + } + for (int i = 1; i < 32767; i++) + Pmax[i] = max(Pmax[i - 1], Pmax[i]); + puts("Pmin Rmin(Pmin) Pmax(Pmin)"); + for (int i = 1; i <= n; i++) + { + if (i + 1 <= n && Rmin[i] == Rmin[i + 1]) + continue; + printf("%4d %10d %10d\n", i, Rmin[i], Pmax[Rmin[i]]); + } + return 0; +} diff --git a/11870.cpp b/11870.cpp new file mode 100644 index 0000000..136a92d --- /dev/null +++ b/11870.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int p[2048], rank_[2048]; +int findp(int x) +{ + return p[x] == x ? x : (p[x] = findp(p[x])); +} +int joint(int x, int y) +{ + x = findp(x), y = findp(y); + if (x == y) + return 0; + if (rank_[x] > rank_[y]) + p[y] = x, rank_[x] += rank_[y]; + else + p[x] = y, rank_[y] += rank_[x]; + return 1; +} +int main() +{ + int testcase, cases = 0, N, M; + char s[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &M); + map R; + int size = 0; + for (int i = 0; i < 2048; i++) + p[i] = i, rank_[i] = 1; + for (int i = 0; i < N; i++) + { + scanf("%s", s); + int &x = R[s]; + if (x == 0) + x = ++size; + scanf("%s", s); + int &y = R[s]; + if (y == 0) + y = ++size; + joint(2 * x, 2 * y); + } + int ret = 1; + for (int i = 0; i < M; i++) + { + scanf("%s", s); + int &x = R[s]; + if (x == 0) + x = ++size; + scanf("%s", s); + int &y = R[s]; + if (y == 0) + y = ++size; + if (findp(2 * x) == findp(2 * y)) + ret = 0; + joint(2 * x + 1, 2 * y); + joint(2 * y + 1, 2 * x); + } + printf("Case %d: %s\n", ++cases, ret ? "YES" : "NO"); + } + return 0; +} diff --git a/11871.cpp b/11871.cpp index 25a509f..6aef3f0 100644 --- a/11871.cpp +++ b/11871.cpp @@ -1,72 +1,185 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -#define LIM 10000001 -int main(){ - //clock_t t1, t2; - //t1=clock(); - static int num[LIM][10]; - memset(num, 0, sizeof(int)*LIM*10); - int i, j; - FOI(i, 1, LIM){ - int N = i; - while(N > 0){ - num[i][N%10]++; - N /= 10; - } - FOI(j, 0, 10) - num[i][j] += num[i-1][j]; - } - while(true){ - int A, B; - cin>>A>>B; - if(A==0 && B==0) - break; - - int cnt[10]; - memset(cnt, 0, sizeof(cnt)); - FOI(i, 0, 10){ - cout< + +using namespace std; + +/* ******************** + * Defines * + ******************** */ +#define max(a, b) ((a > b) ? a : b) //finding max +#define min(a, b) ((a < b) ? a : b) //finding min +#define Max(a, b, c) max(a, max(b, c)) //finding max between 3 numbers +#define Min(a, b, c) min(a, min(b, c)) //finding min between 3 numbers +#define Pi acos(-1.0) //defining Pi for mathematical uses +#define Clear(a) memset(a, 0, sizeof(a)) //clearing memory of an array +#define setfalse(a) memset(a, false, sizeof(a))//setting the array into false +#define settrue(a) memset(a, true, sizeof(a)) //setting the array into true +#define clrstr(a) memset(a, '\0', sizeof(a)) //setting string array to null +#define Case(a) printf("Case %d: ", a) //printing case number +#define caseh(a) printf("Case #%d: ", a) //printing case number having '#' +#define getcase(a) scanf("%d", &a) //scanning case number +#define CHR getchar() //avoid the newline character problem +#define caseloop(a, b) for (a = 1; a <= b; a++)//making case loop +#define EPS 1e-9 //small value for avoiding preccesion error +#define LL long long //long long short form +#define MX 100000 //MAX size/value +#define PB(x) push_back(x) //push in vector/string +#define PP pop_back() //pop from vector +#define PF(x) push_front(x) //push in vector/string/deque from front +#define PPF(x) pop_front() //pop from vector/deque from front +#define IN(x) insert(x) //insert element in set +#define PS(x) push(x) //push element in stack/queue +#define P() pop() //pop element from stack/queue +#define MOD 1000000007 //mod value +#define INF (1 << 28) //infinity value + +template +T BigMod(T b, T p, T m) +{ + if (p == 0) + return 1; + if (p % 2 == 0) + { + T s = BigMod(b, p / 2, m); + return ((s % m) * (s % m)) % m; + } + return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; +} +template +T ModInv(T b, T m) +{ + return BigMod(b, m - 2, m); +} +template +void ia(T a[], int n) +{ + for (int i = 0; i < n; i++) + cin >> a[i]; +} +template +void pa(T a[], int n) +{ + for (int i = 0; i < n - 1; i++) + cout << a[i] << " "; + cout << a[n - 1] << endl; +} +template +void IA(T a[], int n) +{ + for (int i = 1; i <= n; i++) + cin >> a[i]; +} +template +void PA(T a[], int n) +{ + for (int i = 1; i <= n; i++) + cout << a[i] << " "; + cout << a[n - 1] << endl; +} +template +T gcd(T a, T b) +{ + if (!b) + return a; + return gcd(b, a % b); +} + +int g[2005][2005], h[2005][2005], l[2005], r[2005]; + +void draw_graph(int n, int m) +{ + int q, x, y, z; + for (int i = 1; i <= n; i++) + { + scanf("%d%d", &q, &x); + z = 1; + for (int j = 0; j < q; j++) + { + scanf("%d", &y); + for (int k = 0; k < y; k++) + g[i][z++] = (j % 2 ? (x + 1) % 2 : x); + } + for (int j = 1; j <= m; j++) + h[i][j] = (g[i][j] ? 0 : 1); + } +} + +void print_graph(int n, int m) +{ + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + printf("%d", g[i][j]); + cout << endl; + } +} + +void make_histogram(int n, int m) +{ + for (int j = 1; j <= m; j++) + if (h[n][j]) + h[n][j] = h[n - 1][j] + 1; +} + +void print_histogram(int n, int m) +{ + for (int i = 1; i <= m; i++) + cout << h[n][i] << " "; + cout << endl; +} + +int mx_area(int n, int m) +{ + stack a; + a.PS(0); + for (int i = 1; i <= m; i++) + { + while (!a.empty() && h[n][a.top()] >= h[n][i]) + a.pop(); + int v = 0; + if (!a.empty()) + v = a.top(); + a.PS(i); + l[i] = h[n][i] * (i - v); + } + while (!a.empty()) + a.pop(); + + a.PS(m + 1); + for (int i = m; i >= 1; i--) + { + while (!a.empty() && h[n][a.top()] >= h[n][i]) + a.pop(); + int v = 0; + if (!a.empty()) + v = a.top(); + a.PS(i); + r[i] = h[n][i] * (v - i); + } + int mx = -1; + for (int i = 1; i <= m; i++) + mx = max(l[i] + r[i] - h[n][i], mx); + return mx; +} + +int main() +{ + int t, c, n, m, q, x, y, z, mx; + getcase(t); + caseloop(c, t) + { + scanf("%d%d", &n, &m); + draw_graph(n, m); + mx = -1; + for (int i = 1; i <= n; i++) + { + make_histogram(i, m); + mx = max(mx, mx_area(i, m)); + } + Case(c); + cout << mx << endl; + Clear(h); + Clear(r); + Clear(l); + } + return 0; +} diff --git a/11872.cpp b/11872.cpp new file mode 100644 index 0000000..816397b --- /dev/null +++ b/11872.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +/* + Rezwan_4029 + AUST , CSE-25 +*/ + +#define pb push_back +#define ms(a, v) memset(a, v, sizeof a) + +typedef long long ll; + +#define rep(i, n) for (__typeof(n) i = 0; i < n; i++) +#define For(i, n) for (__typeof(n) i = 1; i <= n; i++) +#define forstl(i, n) for (__typeof(n.begin()) i = n.begin(); i != n.end(); i++) + +#define mod 100000007 + +vector> g[100]; +int n, m; +double dp[(1 << 15) + 7][16]; +bool vis1[(1 << 15) + 7][16]; + +double dp2[(1 << 15) + 7][16]; +bool vis2[(1 << 15) + 7][16]; + +bool dfs(int mask, int u) +{ + if (__builtin_popcount(mask) == n) + { + return true; + } + if (vis2[mask][u]) + return dp2[mask][u]; + vis2[mask][u] = true; + bool r = false; + forstl(it, g[u]) + { + int tmp = it->first; + if (mask & (1 << tmp)) + continue; + r |= dfs(mask | (1 << tmp), tmp); + } + return dp2[mask][u] = r; +} + +double solve(int mask, int lastVisit) +{ + if (__builtin_popcount(mask) == n) + return 0.0; + double &ret = dp[mask][lastVisit]; + if (vis1[mask][lastVisit]) + return ret; + vis1[mask][lastVisit] = true; + + ret = 5.00; + int cnt = 0; + forstl(it, g[lastVisit]) + { + int now = it->first; + if (mask & (1 << now)) + continue; + if (dfs(mask | 1 << now, now) == false) + continue;// if possible to go all EJs + cnt++; + double cost = double(it->second); + ret += (cost + solve(mask | 1 << now, now)); + } + if (cnt) + ret *= 1. / double(cnt); + else + ret -= 5.00; + return ret; +} +int main() +{ + int test, cs = 1; + scanf("%d", &test); + while (test--) + { + scanf("%d %d", &n, &m); + rep(i, m) + { + int x, y, z; + cin >> x >> y >> z; + g[x].pb(make_pair(y, z)); + g[y].pb(make_pair(x, z)); + } + ms(vis1, 0); + ms(vis2, 0); + double ret = solve(1, 0); + printf("Case %d: %.10lf\n", cs++, ret); + rep(i, n) g[i].clear(); + } +} diff --git a/11873.cpp b/11873.cpp index 0fb9edd..9d6d023 100644 --- a/11873.cpp +++ b/11873.cpp @@ -1,74 +1,17 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - while(true){ - string str, temp=""; - getline(cin, str); - if(str == "*") - break; - vector S; - int i, len=str.length(); - for(i=0; i + +using namespace std; + +int main() +{ + long long n, test = 1; + while (scanf("%lld", &n) != EOF && n) + { + printf("Case %lld: %lld", test++, n * 6); + n *= n; + printf(" %lld %lld %lld", 6 * n, 8 * n, 2 * n); + n *= n; + printf(" %lld\n", 32 * n); + } + return 0; +} diff --git a/11874.cpp b/11874.cpp index 910b85a..a853ae5 100644 --- a/11874.cpp +++ b/11874.cpp @@ -1,84 +1,49 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) - -#define INF 1 << 30 -#define EPS 1e-9 - -struct Rec{ - double inc; - double exp; -}; - -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int test, t, i, j, k; - cin >> test; - FOI(t, 1, test){ - int N, R; - double P; - cin >> N >> R >> P; - Rec mat[N][N]; - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - mat[i][j].inc = -1; - mat[i][j].exp = INF; - } - } - while(R--){ - int inX, inY; - double I, E; - cin >> inX >> inY >> I >> E; - mat[inX][inY].inc = max(mat[inX][inY].inc, I); - mat[inX][inY].exp = min(mat[inX][inY].exp, E); - } - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1){ - double P1 = (mat[i][j].inc / mat[i][j].exp); - double P2 = (mat[i][k].inc + mat[k][j].inc) / (mat[i][k].exp + mat[k][j].exp); - if (P1 <= P2){ - mat[i][j].inc = mat[i][k].inc + mat[k][j].inc; - mat[i][j].exp = mat[i][k].exp + mat[k][j].exp; - } - } - bool stat = false; - FOI(i, 0, N-1) - if(mat[i][i].inc / mat[i][i].exp >= P){ - stat = true; - break; - } - if (stat) - cout << "Case " << t << ": YES\n"; - else - cout << "Case " << t << ": NO\n"; - } - return 0; -} +#include + +using namespace std; + +#define EPS 1e-11 +#define LL long long + +#define _rep(i, a, b, x) for (__typeof(b) i = (a); i <= (b); i += x) +#define rep(i, n) _rep(i, 0, n - 1, 1) +#define rrep(i, a, b) for (__typeof(b) i = (a); i >= (b); --i) +#define xrep(i, a, b) _rep(i, a, b, 1) + +#define abs(x) (((x) < 0) ? (-(x)) : (x)) +#define all(x) (x).begin(), (x).end() +#define ms(x, a) memset((x), (a), sizeof(x)) +#define mp make_pair +#define pb push_back +#define sz(k) (int)(k).size() + +typedef vector vi; + +int n, r, p, d[110][110]; +const int inf = 1000000000; + +int main() +{ + int t, A, B, I, E; + scanf("%d", &t); + xrep(tcase, 1, t) + { + scanf("%d %d %d", &n, &r, &p); + rep(i, n) + { + rep(j, n) d[i][j] = inf; + d[i][i] = 0; + } + rep(i, r) + { + scanf("%d %d %d %d", &A, &B, &I, &E); + d[A][B] = p * E - I; + } + rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); + bool ok = false; + rep(i, n) if (d[i][i] < 0) + ok = true; + printf("Case %d: %s\n", tcase, ok ? "YES" : "NO"); + } + return 0; +} diff --git a/11875.cpp b/11875.cpp index 81e8f06..de5c712 100644 --- a/11875.cpp +++ b/11875.cpp @@ -1,48 +1,21 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - int test, t; - cin>>test; - FOI(t, 1, test){ - int N, i; - cin>>N; - int vec[N]; - FOI(i, 0, N-1) - cin>>vec[i]; - cout<<"Case "< + +using namespace std; + +int main() +{ + int t, n; + int age[15]; + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> n; + for (int j = 0; j < n; j++) + { + cin >> age[j]; + } + sort(age, age + n); + cout << "Case " << i + 1 << ": " << age[n / 2] << endl; + } + return 0; +} diff --git a/11876.cpp b/11876.cpp index 59b7728..9f9b88f 100644 --- a/11876.cpp +++ b/11876.cpp @@ -1,60 +1,67 @@ -#include -#include -#include -using namespace std; - -const int MAX = 1000001; -static int NOD[MAX+10], pre[MAX+10]; -bool stat[MAX+100]; +#include -int N; +using namespace std; -int DivCnt(int M){ - int i; - int cnt = 0; - for( i=1; i*i < M; i++ ) - if( M % i == 0 ) - cnt += 2; - if( i * i == M ) - cnt++; - return cnt; -} +int r, w[100000]; -void NODCalculate(){ - memset( stat, false, sizeof stat ); - NOD[0] = 1; - stat[1] = true; - for ( int i=1; i < MAX; i++ ){ - NOD[i] = NOD[i-1] + DivCnt( NOD[i-1] ); - stat[NOD[i]] = true; - //cout << NOD[i] << "\t"; - if( NOD[i] >= MAX ){ - //cout << i <>= 1; + } + for (int k = 3; k * k <= x; k += 2) + { + if (x % k) + { + continue; + } + e = 0; + while (!(x % k)) + { + ++e, x /= k; } + d *= (e + 1); } + d <<= x != 1; + return d; } - -int main(){ - NODCalculate(); - pre[0] = 0; - int i=1, j=1; - bool flag = true; - - for( i=1; i<=MAX; i++ ){ - if( stat[i] ) - pre[i] = pre[i-1] + 1; +int bin_search(int x) +{ + int mid, u, v; + for (u = 0, v = r; u < v;) + { + mid = (u + v) >> 1; + if (w[mid] < x) + { + u = mid + 1; + } else - pre[i] = pre[i-1]; + { + v = mid; + } } - - int T; - scanf("%d", &T); - for (int t=1; t<=T; t++){ - int A, B, sum=0; - scanf("%d%d", &A, &B); - printf("Case %d: %d\n", t, pre[B] - pre[A-1]); + return u; +} +int main() +{ + int t; + w[0] = 1; + for (r = 1;; r++) + if ((w[r] = w[r - 1] + nod(w[r - 1])) > 1000000) + { + break; + } + ++r; + scanf("%d", &t); + for (int a, b, c = 0, lp, rp; c++ < t;) + { + scanf("%d %d", &a, &b); + lp = bin_search(a); + rp = bin_search(b); + rp += w[rp] == b; + printf("Case %d: %d\n", c, rp - lp); } return 0; } diff --git a/11877.cpp b/11877.cpp index 637401e..73d46d5 100644 --- a/11877.cpp +++ b/11877.cpp @@ -1,87 +1,33 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - while(true){ - int i, j; - int ser[3], pri[2]; - cin>>ser[0]>>ser[1]>>ser[2]; - cin>>pri[0]>>pri[1]; - if(ser[0]==0 && ser[1]==0 && ser[2]==0 && pri[0]==0 && pri[1]==0) - break; - sort(ser, ser+3); - sort(pri, pri+2); - bool stat[53]; - bool flag=true; - bool gfla=true; - int ind = -1; - memset(stat, true, sizeof(stat)); - - stat[ser[0]] = false; - stat[ser[1]] = false; - stat[ser[2]] = false; - FOI(i, 0, 2){ - if(pri[1]>ser[i] && stat[pri[1]]==true) - stat[pri[1]] = false; - else if(pri[0]>ser[i] && stat[pri[0]]==true) - stat[pri[0]] = false; - else{ - if(!flag){ - gfla = false; - break; - } - flag = false; - bool ifla = true; - - FOI(j, ser[i]+1, 52){ - if(stat[j]){ - ind = max(ind, j); - ifla = false; - break; - } - } - if(ifla){ - gfla = false; - break; - } - } - } - if(!gfla) - cout<<"-1\n"; - else - cout< + +using namespace std; + +int main() +{ + int n; + int result; + while (cin >> n) + { + if (n == 0) + { + break; + } + result = 0; + while (true) + { + result += n / 3; + n = n % 3 + n / 3; + if (n == 0 || n == 1) + { + break; + } + else if (n == 2) + { + result++; + break; + } + } + cout << result << endl; + } + return 0; +} diff --git a/11878.cpp b/11878.cpp index 9a2ecf1..4f10ad6 100644 --- a/11878.cpp +++ b/11878.cpp @@ -1,87 +1,35 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) - -int parse(string S) { - stringstream ss; - int I; - ss << S; - ss >> I; - return I; -} +#include -bool isValid(string S) { - if (S.empty()) - return false; - int L = S.length() - 1, i; - FOI(i, 0, L) - if (!isdigit(S[i])) - return false; - return true; -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - int tot = 0, i; - while ( cin >> str ) { - str += "#"; - //cout << str << endl; - //cout << str << " " << str.length() << endl; - vector< string > V; - string O = "", temp = ""; - int L = str.length() - 1; - //cout << "# "; - FOI(i, 0, L) { - //cout << i << "# " ; - if (!isdigit(str[i])) { - V.push_back(temp); - //cout << temp << endl; - temp = ""; - if (O == "") { - O += str[i]; - //cout << O << endl; - } +int main() +{ + int a, b, c; + char action, eq; + int result = 0; + while (cin >> a >> action >> b >> eq) + { + if (cin.peek() == '?') + { + cin >> eq; + continue; + } + cin >> c; + if (action == '+') + { + if (a + b == c) + { + result++; + } + } + else + { + if (a - b == c) + { + result++; } - else - temp += str[i]; } - //cout << endl; - if ( !(isValid(V[0]) && isValid(V[1]) && isValid(V[2])) ) - continue; - else if ( O == "+" && (parse(V[0]) + parse(V[1]) == parse(V[2])) ) - ++tot; - else if ( O == "-" && (parse(V[0]) - parse(V[1]) == parse(V[2])) ) - ++tot; } - cout << tot << endl; + cout << result << endl; return 0; } diff --git a/11879.cpp b/11879.cpp new file mode 100644 index 0000000..26bac9d --- /dev/null +++ b/11879.cpp @@ -0,0 +1,406 @@ +#include + +using namespace std; + +/* bignum.c + Implementation of large integer arithmetic: addition, subtraction, + multiplication, and division. + + begun: February 7, 2002 + by: Steven Skiena +*/ + +/* +Copyright 2003 by Steven S. Skiena; all rights reserved. + +Permission is granted for use in non-commerical applications +provided this copyright notice remains intact and unchanged. + +This program appears in my book: + +"Programming Challenges: The Programming Contest Training Manual" +by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003. + +See our website www.programming-challenges.com for additional information. + +This book can be ordered from Amazon.com at + +http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/ + +*/ + +#define MAXDIGITS 1100 /* maximum length bignum */ + +#define PLUS 1 /* positive sign bit */ +#define MINUS -1 /* negative sign bit */ + +typedef struct +{ + char digits[MAXDIGITS]; /* represent the number */ + int signbit; /* 1 if positive, -1 if negative */ + int lastdigit; /* index of high-order digit */ +} bignum; + +void print_bignum(bignum *); +void string_to_bignum(string &, bignum *); +void int_to_bignum(int, bignum *); +void initialize_bignum(bignum *); +int max(int, int); +void add_bignum(bignum *, bignum *, bignum *); +void subtract_bignum(bignum *, bignum *, bignum *); +int compare_bignum(bignum *, bignum *); +void zero_justify(bignum *); +void digit_shift(bignum *, int); +void multiply_bignum(bignum *, bignum *, bignum *); +void divide_bignum(bignum *, bignum *, bignum *); + +void print_bignum(bignum *n) +{ + int i; + if (n->signbit == MINUS) + { + printf("- "); + } + for (i = n->lastdigit; i >= 0; i--) + { + printf("%c", '0' + n->digits[i]); + } + printf("\n"); +} + +void string_to_bignum(string &s, bignum *n) +{ + n->signbit = PLUS;// TODO: currently no negative numbers + for (int i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + for (int i = s.size() - 1; i >= 0; i--) + { + n->lastdigit++; + n->digits[n->lastdigit] = s[i] - 48; + } + if (s == "0") + { + n->lastdigit = 0; + } +} + +void int_to_bignum(int s, bignum *n) +{ + int i; /* counter */ + int t; /* int to work with */ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + for (i = 0; i < MAXDIGITS; i++) + { + n->digits[i] = (char)0; + } + n->lastdigit = -1; + t = abs(s); + while (t > 0) + { + n->lastdigit++; + n->digits[n->lastdigit] = (t % 10); + t = t / 10; + } + if (s == 0) + { + n->lastdigit = 0; + } +} + +void initialize_bignum(bignum *n) +{ + int_to_bignum(0, n); +} + +int max(int a, int b) +{ + if (a > b) + { + return (a); + } + else + { + return (b); + } +} + +/* c = a +-/* b; */ + +void add_bignum(bignum *a, bignum *b, bignum *c) +{ + int carry; /* carry digit */ + int i; /* counter */ + initialize_bignum(c); + if (a->signbit == b->signbit) + { + c->signbit = a->signbit; + } + else + { + if (a->signbit == MINUS) + { + a->signbit = PLUS; + subtract_bignum(b, a, c); + a->signbit = MINUS; + } + else + { + b->signbit = PLUS; + subtract_bignum(a, b, c); + b->signbit = MINUS; + } + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit) + 1; + carry = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + c->digits[i] = (char)(carry + a->digits[i] + b->digits[i]) % 10; + carry = (carry + a->digits[i] + b->digits[i]) / 10; + } + zero_justify(c); +} + +void subtract_bignum(bignum *a, bignum *b, bignum *c) +{ + int borrow; /* has anything been borrowed? */ + int v; /* placeholder digit */ + int i; /* counter */ + initialize_bignum(c); + if ((a->signbit == MINUS) || (b->signbit == MINUS)) + { + b->signbit = -1 * b->signbit; + add_bignum(a, b, c); + b->signbit = -1 * b->signbit; + return; + } + if (compare_bignum(a, b) == PLUS) + { + subtract_bignum(b, a, c); + c->signbit = MINUS; + return; + } + c->lastdigit = max(a->lastdigit, b->lastdigit); + borrow = 0; + for (i = 0; i <= (c->lastdigit); i++) + { + v = (a->digits[i] - borrow - b->digits[i]); + if (a->digits[i] > 0) + { + borrow = 0; + } + if (v < 0) + { + v = v + 10; + borrow = 1; + } + c->digits[i] = (char)v % 10; + } + zero_justify(c); +} + +int compare_bignum(bignum *a, bignum *b) +{ + int i; /* counter */ + if ((a->signbit == MINUS) && (b->signbit == PLUS)) + { + return (PLUS); + } + if ((a->signbit == PLUS) && (b->signbit == MINUS)) + { + return (MINUS); + } + if (b->lastdigit > a->lastdigit) + { + return (PLUS * a->signbit); + } + if (a->lastdigit > b->lastdigit) + { + return (MINUS * a->signbit); + } + for (i = a->lastdigit; i >= 0; i--) + { + if (a->digits[i] > b->digits[i]) + { + return (MINUS * a->signbit); + } + if (b->digits[i] > a->digits[i]) + { + return (PLUS * a->signbit); + } + } + return (0); +} + +void zero_justify(bignum *n) +{ + while ((n->lastdigit > 0) && (n->digits[n->lastdigit] == 0)) + { + n->lastdigit--; + } + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + n->signbit = PLUS; /* hack to avoid -0 */ + } +} + +void digit_shift(bignum *n, int d) /* multiply n by 10^d */ +{ + int i; /* counter */ + if ((n->lastdigit == 0) && (n->digits[0] == 0)) + { + return; + } + for (i = n->lastdigit; i >= 0; i--) + { + n->digits[i + d] = n->digits[i]; + } + for (i = 0; i < d; i++) + { + n->digits[i] = 0; + } + n->lastdigit = n->lastdigit + d; +} + +int digit_shift_div10(bignum *n) +{ + int i; + int result; + result = n->digits[0]; + for (int i = 0; i < n->lastdigit; i++) + { + n->digits[i] = n->digits[i + 1]; + } + n->lastdigit--; + return result; +} + +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int i, j; /* counters */ + initialize_bignum(c); + row = *a; + for (i = 0; i <= b->lastdigit; i++) + { + for (j = 1; j <= b->digits[i]; j++) + { + add_bignum(c, &row, &tmp); + *c = tmp; + } + digit_shift(&row, 1); + } + c->signbit = a->signbit * b->signbit; + zero_justify(c); +} + +void divide_bignum(bignum *a, bignum *b, bignum *c) +{ + bignum row; /* represent shifted row */ + bignum tmp; /* placeholder bignum */ + int asign, bsign; /* temporary signs */ + int i, j; /* counters */ + initialize_bignum(c); + c->signbit = a->signbit * b->signbit; + asign = a->signbit; + bsign = b->signbit; + a->signbit = PLUS; + b->signbit = PLUS; + initialize_bignum(&row); + initialize_bignum(&tmp); + c->lastdigit = a->lastdigit; + for (i = a->lastdigit; i >= 0; i--) + { + digit_shift(&row, 1); + row.digits[0] = a->digits[i]; + c->digits[i] = 0; + while (compare_bignum(&row, b) != PLUS) + { + c->digits[i]++; + subtract_bignum(&row, b, &tmp); + row = tmp; + } + } + zero_justify(c); + a->signbit = asign; + b->signbit = bsign; +} + +int string_to_num(string &s) +{ + int result = 0; + for (int i = 0, sz = s.size(); i < sz; i++) + { + result = result * 10 + int(s[i] - 48); + } + return result; +} + +int bignum_to_int(bignum *n) +{ + int result = 0; + for (int i = n->lastdigit; i >= 0; i--) + { + result = result * 10 + n->digits[i]; + } + return result; +} + +int main() +{ + string input; + int remainder; + bignum n, tmp, tmp_result, five, zero; + int_to_bignum(5, &five); + int_to_bignum(0, &zero); + while (getline(cin, input)) + { + if (input == "0") + { + break; + } + if (input.size() < 10) + { + int num = string_to_num(input); + if (num % 17 == 0) + { + cout << 1 << endl; + } + else + { + cout << 0 << endl; + } + continue; + } + string_to_bignum(input, &n); + while (n.lastdigit > 8) + { + remainder = digit_shift_div10(&n); + remainder *= 5; + int_to_bignum(remainder, &tmp); + subtract_bignum(&n, &tmp, &tmp_result); + add_bignum(&tmp_result, &zero, &n); + } + int result = bignum_to_int(&n); + if (result % 17 == 0) + { + cout << 1 << endl; + } + else + { + cout << 0 << endl; + } + } + return 0; +} diff --git a/1188.cpp b/1188.cpp new file mode 100644 index 0000000..b554779 --- /dev/null +++ b/1188.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int N; + while (scanf("%d", &N) > 0 && N) + { + double F1 = 0, F2 = 0, F3 = 0; + double num1 = 1, denom1 = 0; + for (int i = 1; i <= N; i++) + { + num1 *= (N - 1); + F1 += (num1 * i); + denom1 += (num1); + } + double num2 = N, denom2 = 0; + for (int i = 1; i <= N - 1; i++) + { + num2 *= (N - i); + F2 += (num2 * i); + denom2 += (num2); + } + double num3 = N * (N - 1), denom3 = 0; + for (int i = 2; i <= N - 1; i++) + { + num3 *= (N - i); + F3 += (num3 * (i + 1)); + denom3 += (num3); + } + F1 /= denom1; + F2 /= denom2; + F3 /= denom3; + printf("%.4lf %.4lf %.4lf\n", F1, F2, F3); + } +} \ No newline at end of file diff --git a/11880.cpp b/11880.cpp new file mode 100644 index 0000000..b75776e --- /dev/null +++ b/11880.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int main() +{ + int L, W, x, y, R, v, s, i, j, k, aa; + double x1, x0, y0, y1, f, a, vy, vx; + while (scanf("%d%d%d%d%d%d%d%d", &L, &W, &x, &y, &R, &aa, &v, &s), L) + { + a = aa * 2 * asin(1.0) / 180; + x1 = v * cos(a) * s + x; + y1 = v * sin(a) * s + y; + while (1) + { + if (y1 > (W - R)) + { + y1 = 2 * (W - R) - y1; + } + else if (y1 < R) + { + y1 = 2 * R - y1; + } + else + { + break; + } + } + while (1) + { + if (x1 > (L - R)) + { + x1 = 2 * (L - R) - x1; + } + else if (x1 < R) + { + x1 = 2 * R - x1; + } + else + { + break; + } + } + printf("%.2f %.2f\n", x1, y1); + } +} diff --git a/11881.cpp b/11881.cpp new file mode 100644 index 0000000..4b9ad0d --- /dev/null +++ b/11881.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int maxint = -1u >> 1; + +template +bool get_max(T &a, const T &b) +{ + return b > a ? a = b, 1 : 0; +} +template +bool get_min(T &a, const T &b) +{ + return b < a ? a = b, 1 : 0; +} + +const int maxn = 15; +int n; +double a[maxn]; + +double calc(double s) +{ + double ret = a[0]; + double fac = 1.0 + s; + double m = 1.0; + for (int i = 1; i <= n; i++) + { + m *= fac; + ret += a[i] / m; + } + return ret; +} + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i <= n; i++) + { + scanf("%lf", a + i); + } + double l = -1.0, r = 10000.0, mid; + int cnt = 0; + while (++cnt < 100) + { + mid = (l + r) * 0.5; + double val = calc(mid); + if (val < 0) + { + r = mid; + } + else + { + l = mid; + } + } + printf("%.2lf\n", mid); + } + return 0; +} diff --git a/11882.cpp b/11882.cpp new file mode 100644 index 0000000..61cb19b --- /dev/null +++ b/11882.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +struct node +{ + int x, y; +} queue_[1000]; + +int dir[4][2] = {-1, 0, 0, -1, 0, 1, 1, 0}; + +char map_[20][20], ans[35], stk[35]; +int n, m, max_, flag, total; + +bool yes(int x, int y) +{ + return x >= 0 && x < n && y >= 0 && y < m; +} +int bfs(int x, int y) +{ + node t; + char g[20][20]; + for (int i = 0; i < n; i++) + { + strcpy(g[i], map_[i]); + } + int head, tail; + head = tail = 0; + t.x = x, t.y = y; + queue_[tail++] = t; + while (head < tail) + { + x = queue_[head].x; + y = queue_[head++].y; + for (int i = 0; i < 4; i++) + { + int xx = x + dir[i][0]; + int yy = y + dir[i][1]; + if (!yes(xx, yy) || g[xx][yy] == '#') + { + continue; + } + g[xx][yy] = '#'; + t.x = xx, t.y = yy; + queue_[tail++] = t; + } + } + return head; +} +void dfs(int x, int y, int cnt) +{ + if (max_ < cnt || (max_ == cnt && flag == 1)) + { + stk[cnt] = 0; + strcpy(ans, stk); + max_ = cnt; + flag = 0; + } + int res = bfs(x, y); + if (res + cnt - 1 < max_ || (res + cnt - 1 == max_ && flag == -1)) + { + return; + } + for (int i = 0; i < 4; i++) + { + int xx = x + dir[i][0]; + int yy = y + dir[i][1]; + if (!yes(xx, yy) || map_[xx][yy] == '#') + { + continue; + } + if (flag != 1 && ans[cnt] > map_[xx][yy] && total == max_) + { + continue; + } + stk[cnt] = map_[xx][yy]; + map_[xx][yy] = '#'; + if (flag == 0) + { + if (cnt >= max_) + { + flag = 1; + } + else if (ans[cnt] == stk[cnt]) + { + flag = 0; + } + else if (ans[cnt] < stk[cnt]) + { + flag = 1; + } + else + { + flag = -1; + } + dfs(xx, yy, cnt + 1); + flag = 0; + } + else + { + dfs(xx, yy, cnt + 1); + } + map_[xx][yy] = stk[cnt]; + } +} +int main() +{ + while (scanf("%d%d", &n, &m) && n + m) + { + total = 0; + for (int i = 0; i < n; i++) + { + scanf("%s", map_ + i); + for (int j = 0; j < m; j++) + { + if (map_[i][j] != '#') + { + total++; + } + } + } + max_ = 1; + memset(ans, 0, sizeof(ans)); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (map_[i][j] == '#') + { + continue; + } + if (max_ == total && ans[0] > map_[i][j]) + { + continue; + } + stk[0] = map_[i][j]; + map_[i][j] = '#'; + if (ans[0] == stk[0]) + { + flag = 0; + } + else if (ans[0] < stk[0]) + { + flag = 1; + } + else + { + flag = -1; + } + dfs(i, j, 1); + map_[i][j] = stk[0]; + } + } + printf("%s\n", ans); + } + return 0; +} diff --git a/11883.cpp b/11883.cpp new file mode 100644 index 0000000..593b5a2 --- /dev/null +++ b/11883.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define M 1005 +#define oo 1000000000 +struct node +{ + int from, to, next; + double v, a; +} edge[M]; +int head[M], tot; +int mark[M]; +int Q[M * 10]; +double dis[2][M]; +void add(int a, int b, double c, double d) +{ + edge[tot].from = a, edge[tot].to = b, edge[tot].v = c; + edge[tot].a = d, edge[tot].next = head[a], head[a] = tot++; +} +int Min(int a, int b) +{ + return (a > b ? b : a); +} +// Shortest Path Faster Algorithm +void SPFA(int k, int x, int n) +{ + int L, H, i, y; + L = H = 0; + for (i = 1; i <= n; i++) + { + dis[k][i] = oo; + mark[i] = 0; + } + dis[k][x] = 0; + Q[H++] = x; + while (L < H) + { + x = Q[L++]; + mark[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].to; + if (dis[k][y] > dis[k][x] + edge[i].v) + { + dis[k][y] = dis[k][x] + edge[i].v; + if (!mark[y]) + { + mark[y] = 1; + Q[H++] = y; + } + } + } + } +} +double get(double v, double a, double t) +{ + if (a == 1) + { + return v; + } + else + { + double b = 1.0 / (v * log(a)); + double x = -log(b) / log(a) - t; + //printf("%lf %lf %lf %lf\n",v,a,t,x); + if (x < 0) + { + x = 0; + } + return v * pow(a, -(t + x)) + x; + } +} +int main() +{ + int n, m, i, j; + while (scanf("%d %d", &n, &m) != EOF) + { + if (n == 0) + { + break; + } + for (i = 1; i <= n; i++) + { + head[i] = -1; + } + tot = 0; + int x, y; + double v, a; + while (m-- && scanf("%d %d %lf %lf", &x, &y, &v, &a)) + { + add(x, y, v, a); + add(y, x, v, a); + } + SPFA(0, 1, n); + SPFA(1, n, n); + double ans = oo; + for (i = 0; i < tot; i++) + { + double tmp = dis[0][edge[i].from] + dis[1][edge[i].to]; + tmp += get(edge[i].v, edge[i].a, dis[0][edge[i].from]); + if (tmp < ans) + { + ans = tmp; + } + } + printf("%.3lf\n", ans); + } + return 0; +} diff --git a/11884.cpp b/11884.cpp new file mode 100644 index 0000000..8365e0b --- /dev/null +++ b/11884.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +const int N = 7; +double dp[N][N][N][N][N][N]; + +double solve(int c, int *in) +{ + int r = *max_element(in, in + c); + if (r == 0) + { + return 0; + } + double &ret = dp[in[0]][in[1]][in[2]][in[3]][in[4]][in[5]]; + if (ret == -1) + { + REP(i, 1, r + 1) + { + int cnt = 0; + rep(j, c) if (in[j] >= i) + { + cnt++; + } + if (cnt == 0) + { + continue; + } + rep(j, 2)//left or right + { + double sum = 0; + rep(k, 3)//power + { + int next[6] = {0}; + rep(l, c) next[l] = in[l]; + int tmp = 0; + rep(l, c && tmp < k + 1) + { + if (next[l] < i) + { + continue; + } + else + { + next[l]--, tmp++; + } + } + if (j) + { + reverse(next, next + c); + } + sum += 1 / 3. * (1 - solve(c, next)); + } + ret = max(ret, sum); + reverse(in, in + c); + } + } + } + return ret; +} + +main() +{ + int c; + while (cin >> c && c) + { + int in[6] = {0}; + rep(i, N) rep(j, N) rep(k, N) rep(l, N) rep(m, N) rep(n, N) + dp[i][j][k][l][m][n] = -1; + rep(i, c) cin >> in[i]; + double ans = solve(c, in); + printf("%.6lf\n", solve(c, in)); + } +} diff --git a/11885.cpp b/11885.cpp new file mode 100644 index 0000000..9b77378 --- /dev/null +++ b/11885.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define println(x) cout << #x << " is " << x << endl; + +const int N = 2; + +typedef long long LL; +LL MOD = 987654321; +struct Mat +{ + LL mat[N][N]; +} org, unit; + +void setOrg() +{ + org.mat[0][0] = 1; + org.mat[0][1] = 0; +} + +void setUnit() +{ + unit.mat[0][0] = 1; + unit.mat[0][1] = 1; + unit.mat[1][0] = 1; + unit.mat[1][1] = 0; +} + +Mat operator*(Mat a, Mat b) +{ + Mat c; + memset(c.mat, 0, sizeof(c.mat)); + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + for (int k = 0; k < 2; k++) + { + c.mat[i][j] = (c.mat[i][j] + (a.mat[i][k] * b.mat[k][j])) % MOD; + } + } + } + return c; +} + +Mat operator^(Mat a, LL p) +{ + setOrg(); + setUnit(); + for (; p; p = p / 2) + { + if (p & 1) + { + org = org * unit; + } + unit = unit * unit; + } + return org; +} + +LL solve(LL p) +{ + org = org ^ p; + return org.mat[0][1] % MOD; +} + +int main() +{ + LL idx; + while (scanf("%lld", &idx)) + { + if (!idx) + { + break; + } + if (idx & 1 || idx < 8) + { + printf("0\n"); + continue; + } + long long ans = solve(idx - 3) - idx / 2 + 1; + if (ans < 0) + { + ans += MOD; + } + printf("%lld\n", ans % MOD); + } + return 0; +} diff --git a/11886.cpp b/11886.cpp new file mode 100644 index 0000000..3929821 --- /dev/null +++ b/11886.cpp @@ -0,0 +1,221 @@ +#include + +using namespace std; + +#define CLR() (state = 0, first = second = "") + +const int MAX_LENGTH = 333; + +struct Complex; +typedef Complex *ComplexPointer; +ComplexPointer ptr[26]; + +char buf[MAX_LENGTH]; +int total_nodes; +set visited, stk; + +struct Complex +{ + static const int STRING = 0; + static const int DICT = 1; + + int data_type, id; + string str; + map dict; + + Complex() + { + data_type = DICT; + id = total_nodes++; + } + + Complex(const string &s) + { + data_type = STRING; + str = s; + id = total_nodes++; + } + + int length() const + { + return (int)dict.size(); + } + + ComplexPointer &operator[](const string &s) + { + if (!dict.count(s)) + { + dict[s] = new Complex; + } + return dict[s]; + } + + void add_pair(const string &first, const ComplexPointer &second) + { + dict[first] = second; + } +}; + +inline int id(char x) +{ + return x - 'a'; +} + +ComplexPointer new_dict(int s, int t) +{ + static vector que_str; + static vector que_ptr; + string first, second; + int state = 0; + que_str.clear(); + que_ptr.clear(); + for (; s < t; s++) + { + if (buf[s] == '{') + { + que_str.push_back(first); + que_ptr.push_back(new Complex); + CLR(); + } + else if (buf[s] == '}') + { + if (state == 1) + { + que_ptr.back()->add_pair(first, new Complex(second)); + CLR(); + } + ComplexPointer ptr = que_ptr.back(); + que_ptr.pop_back(); + if (que_ptr.empty()) + { + return ptr; + } + que_ptr.back()->add_pair(que_str.back(), ptr); + que_str.pop_back(); + } + else if (buf[s] == ',') + { + if (state == 1) + { + que_ptr.back()->add_pair(first, new Complex(second)); + CLR(); + } + } + else if (buf[s] == ':') + { + state = 1; + } + else + { + state == 0 ? first += buf[s] : second += buf[s]; + } + } + return que_ptr.front(); +} + +ComplexPointer &parse_reference(int s, int t) +{ + if (s + 1 == t) + { + return ptr[id(buf[s])]; + } + ComplexPointer now = ptr[id(buf[s++])]; + while (s < t) + { + int l = s + 1, r = s + 1; + for (; buf[r] != ']'; r++) + ; + if (r + 1 == t) + { + return (*now)[string(buf + l, buf + r)]; + } + else + { + now = (*now)[string(buf + l, buf + r)]; + s = r + 1; + } + } +} + +ComplexPointer parse(int s, int t) +{ + if (buf[s] == '{') + { + return new_dict(s, t); + } + else if (buf[s] == '\'' || isdigit(buf[s]) || buf[s] == '-') + { + return new Complex(string(buf + s, buf + t)); + } + else + { + return parse_reference(s, t); + } +} + +bool dfs(ComplexPointer ptr) +{ + visited.insert(ptr->id); + stk.insert(ptr->id); + for (__typeof(ptr->dict.begin()) itr = ptr->dict.begin(); itr != ptr->dict.end(); itr++) + { + if (!visited.count(itr->second->id) && dfs(itr->second)) + { + return true; + } + else if (stk.count(itr->second->id)) + { + return true; + } + } + stk.erase(ptr->id); + return false; +} + +int test(ComplexPointer ptr) +{ + visited.clear(); + stk.clear(); + return dfs(ptr); +} + +int main() +{ + for (int i = 0; i < 26; i++) + { + ptr[i] = new Complex; + } + + while (gets(buf)) + { + int l = strlen(buf), m = 0; + for (int i = 0; i < l; i++) + { + if (buf[i] != ' ') + { + buf[m++] = buf[i]; + } + } + l = m; + if (l >= 4 && string(buf, buf + 4) == "test") + { + printf("%d\n", test(parse(5, l - 1))); + } + else if (l >= 6 && string(buf, buf + 6) == "length") + { + printf("%d\n", parse(7, l - 1)->length()); + } + else + { + for (int i = 0; i < l; i++) + { + if (buf[i] == '=') + { + parse_reference(0, i) = parse(i + 1, l); + break; + } + } + } + } + return 0; +} diff --git a/11888.cpp b/11888.cpp index 86584a4..f0d8126 100644 --- a/11888.cpp +++ b/11888.cpp @@ -1,73 +1,73 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -char str[200005]; -int L; - -bool isPal(int I, int J){ - int i; - FOI(i, 0, (J - I)/2) - if( str[I + i] != str[J - i] ) - return false; - return true; -} - -int main(){ - int T; - scanf("%d", &T); - while( T-- ){ - scanf("%s", str); - L = strlen( str ); - bool ail = false; - int i; - FOI(i, 0, L-2){ - if( isPal(0, i) && isPal(i+1, L-1) ){ - printf("alindrome\n"); - ail = true; - break; - } - } - if( !ail ){ - if( isPal(0, L-1) ) - printf("palindrome\n"); - else - printf("simple\n"); - } - } - return 0; -} +#include + +using namespace std; + +bool is_palindrome(const string &s, int start, int end) +{ + int sz = end - start + 1; + if (sz % 2 == 0) + { + int middle = start + sz / 2; + int i; + for (i = 0; middle - 1 - i >= 0 && middle + i <= end && s[middle - 1 - i] == s[middle + i]; i++) + ; + if (middle - i - 1 < 0 || middle + i > end) + { + return true; + } + } + else + { + int middle = start + sz / 2; + int i; + for (i = 1; middle - i >= 0 && middle + i <= end && s[middle - i] == s[middle + i]; i++) + ; + if (middle - i < 0 || middle + i > end) + { + return true; + } + } + return false; +} + +bool is_alindrome(const string &s) +{ + for (int i = 0, sz = s.size(); i < sz - 1; i++) + { + if (is_palindrome(s, 0, i) && is_palindrome(s, i + 1, sz - 1)) + { + return true; + } + } + return false; +} + +int main() +{ + int cases; + string input; + cin >> cases; + cin.ignore(100, '\n'); + while (cases--) + { + getline(cin, input); + if (input.size() == 0) + { + cout << "simple" << endl; + continue; + } + if (is_alindrome(input)) + { + cout << "alindrome" << endl; + } + else if (is_palindrome(input, 0, input.size() - 1)) + { + cout << "palindrome" << endl; + } + else + { + cout << "simple" << endl; + } + } + return 0; +} diff --git a/11889.cpp b/11889.cpp index 7fa4901..18231fc 100644 --- a/11889.cpp +++ b/11889.cpp @@ -1,34 +1,120 @@ -#include +#include + using namespace std; -#define MAX (int)1e7 -typedef long long int64; +// http://uva.onlinejudge.org/external/118/11889.html +// Runtime : 0.320s +// Tag : Prime Factors, LCM + +//=============================================================== +// Name : UVa_11889.cpp +// Author : Shahab +// Version : +// Copyright : Your copyright notice +// Description : Hello World in C++, Ansi-style +//=============================================================== + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +bool mark[N]; +vector primeList; + +void seive() +{ + memset(mark, true, sizeof(mark)); + mark[0] = mark[1] = false; + for (int i = 4; i < N; i += 2) + { + mark[i] = false; + } + for (int i = 3; i * i <= N; i += 2) + { + if (mark[i]) + { + for (int j = i * i; j < N; j += 2 * i) + { + mark[j] = false; + } + } + } + primeList.clear(); + primeList.push_back(2); + for (int i = 3; i < N; i += 2) + { + if (mark[i]) + { + primeList.push_back(i); + } + } +} -int64 GCD(int64 A, int64 B){ - if( B > A ) - return GCD(B, A); - return B == 0 ? A : GCD( B, A%B); +int power(int a, int b) +{ + int ret = 1; + for (int i = 1; i <= b; i++) + { + ret *= a; + } + return ret; } -int64 LCM(int64 A, int64 B){ - return ( A * B ) / GCD(A, B); +int factors(int a, int b) +{ + int ret = 1; + int in = 0; + int tmp = a; + while (primeList[in] * primeList[in] <= tmp) + { + int cnt1 = 0; + int cnt2 = 0; + while (a % primeList[in] == 0) + { + a /= primeList[in]; + cnt1++; + } + while (b % primeList[in] == 0) + { + b /= primeList[in]; + cnt2++; + } + if (cnt1 > cnt2) + { + ret *= power(primeList[in], cnt1); + } + in++; + } + if (a > 1 && b == 1) + { + ret *= a; + } + return ret; } -int main(){ - int T; - scanf("%d", &T); - while( T-- ){ - int64 A, C; - scanf("%lld%lld", &A, &C); - if( C % A ) +int main() +{ + seive(); + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int a, c; + scanf("%d %d", &a, &c); + if (c % a == 0) + { + printf("%d\n", factors(c, a)); + } + else + { printf("NO SOLUTION\n"); - else{ - int64 B = C / A, i; - for ( i = B; i<= MAX; i += B ) - if( LCM(A, i) == C ) - break; - printf("%lld\n", i); } } return 0; } + +// @END_OF_SOURCE_CODE diff --git a/1189.cpp b/1189.cpp new file mode 100644 index 0000000..d6da7ac --- /dev/null +++ b/1189.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +bool flg; +int n; + +void func() +{ + queue qs; + queue qm; + + qm.push(1 % n); + qs.push("1"); + + while (!qs.empty()) + { + int um = qm.front(); + string us = qs.front(); + + qm.pop(); + qs.pop(); + + if (um == 0) + { + cout << us << endl; + return; + } + + qm.push((um * 10) % n); + qs.push(us + "0"); + + qm.push((um * 10 + 1) % n); + qs.push(us + "1"); + } +} + +int main() +{ + while (cin >> n) + { + if (!n) + return 0; + + flg = false; + func(); + } + + return 0; +} diff --git a/11890.cpp b/11890.cpp new file mode 100644 index 0000000..27702ee --- /dev/null +++ b/11890.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +/* + * Author: Phillip LeBlanc + * Problem: Calculus Simplified 11890 + * + */ + +#define DEBUG 1 +#define printd \ + if (DEBUG) \ + printf +#define POS 1 +#define NEG 0 + +int neg(const int last_sign, const int mode) +{ + if ((last_sign == NEG && mode == POS) || + (last_sign == POS && mode == NEG)) + { + return 1; + } + else + { + return 0; + } +} + +int count_negs(char *input, int *start, const int mode) +{ + int negs = 0; + int last_sign = POS; + while (input[*start] != 0) + { + if (input[*start] == '(') + { + (*start)++; + negs += count_negs(input, start, mode); + } + if (input[*start] == '-') + { + if (input[*start + 1] == '(') + { + *start = *start + 2; + negs += count_negs(input, start, mode ^ 1); + continue; + } + last_sign = NEG; + (*start)++; + } + if (input[*start] == '+') + { + last_sign = POS; + (*start)++; + } + if (input[*start] == 'x') + { + negs += neg(last_sign, mode); + (*start)++; + } + if (input[*start] == ')') + { + (*start)++; + return negs; + } + } + return negs; +} + +int compare(const void *a, const void *b) +{ + return (*(int *)b) - (*(int *)a); +} + +void run() +{ + int num, i, *a, negs, tot = 0, start = 0; + char input[100001]; + scanf("%s", input); + scanf("%d", &num); + a = (int *)malloc(sizeof(int) * num); + for (i = 0; i < num; ++i) + { + scanf("%d", &a[i]); + } + qsort(a, num, sizeof(int), compare); + negs = count_negs(input, &start, POS); + for (i = 0; i < (num - negs); i++) + { + tot += a[i]; + } + for (; i < num; i++) + { + tot -= a[i]; + } + printf("%d\n", tot); +} + +int main() +{ + int count, j; + scanf("%d", &count); + for (j = 0; j < count; j++) + { + run(); + } + return 0; +} + +/* + for (i = 0; i < num; ++i) + { + printd("%d ", a[i]); + } + printd("\n"); +*/ diff --git a/11891.cpp b/11891.cpp new file mode 100644 index 0000000..125da03 --- /dev/null +++ b/11891.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < (n); i++) +#define FOR(i, a, b) for (__typeof(b) i = (a); i <= (b); i++) +#define FORD(i, a, b) for (__typeof(a) i = (a); i >= (b); i--) +#define FORIT(i, m) for (__typeof((m).begin()) i = (m).begin(); i != (m).end(); ++i) +#define SET(t, v) memset((t), (v), sizeof(t)) +#define ALL(x) x.begin(), x.end() +#define REV(x) reverse(ALL(x)) +#define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) + +#define sz size() +#define pb push_back +#define VI vector +#define VS vector +#define i64 long long +#define LL i64 +#define LD long double + +#define D(x) \ + if (1) \ + cerr << __LINE__ << " " << #x " = " << x << endl +#define D2(x, y) \ + if (1) \ + cerr << __LINE__ << " " << #x " = " << x << ", " << #y " = " << y << endl + +typedef pair pii; +#define M 100005 + +pii m[M]; +int n, k; +i64 p[M], w[M], sum[M]; + +pii f(int x) +{ + if (x == n + 1) + { + return pii(0, 0); + } + pii &ret = m[x]; + if (ret.first != -1) + { + return ret; + } + ret.first = 1 << 20; + int lim = min(x + k - 1, n); + int st = x; + i64 cost = 0; + FOR(i, x, lim) + { + // updating the current cost + cost += (p[i] - p[st]) * w[i]; + // move the position of least cost + while (st < i) + { + i64 newcost = cost + (sum[st] - sum[x - 1]) * (p[st + 1] - p[st]) - (sum[i] - sum[st]) * (p[st + 1] - p[st]); + if (newcost < cost) + { + cost = newcost, st++; + } + else + { + break; + } + } + // try to recurse and get a better solution + pii t = f(i + 1); + t.first++, t.second += cost; + if (t < ret) + { + ret = t; + } + } + return ret; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &k); + FOR(i, 1, n) + scanf("%lld %lld", &p[i], &w[i]); + REP(i, n + 1) + m[i].first = -1; + sum[0] = 0; + FOR(i, 1, n) + sum[i] = sum[i - 1] + w[i]; + FORD(i, n, 1) + f(i); + pii ans = f(1); + printf("%d %lld\n", ans.first, ans.second); + } + return 0; +} diff --git a/11892.cpp b/11892.cpp new file mode 100644 index 0000000..2a2b1dc --- /dev/null +++ b/11892.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/118/11892.html +// Runtime : 0.064s +// Tag : Nim, Game Theory + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n; + scanf("%d", &n); + int input; + int cntOne = 0; + int cntOthers = 0; + for (int i = 0; i < n; i++) + { + scanf("%d", &input); + if (input == 1) + { + cntOne++; + } + else + { + cntOthers++; + } + } + if (cntOne % 2) + { + printf("poopi\n"); + } + else + { + if (cntOthers > 0) + { + printf("poopi\n"); + } + else + { + printf("piloop\n"); + } + } + }// while loop for testCase + return 0; +}// end of main () + +// @END_OF_SOURCE_CODE diff --git a/11893.cpp b/11893.cpp new file mode 100644 index 0000000..82a9fbe --- /dev/null +++ b/11893.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 400; + +int in[N]; + +int edge[N][N]; +int pn[N]; +bool vis[N]; +bool dis[N]; +int mini[N], maxi[N]; +int ord[N]; + +bool tsort(int n, int now, int &p, const int s, const int d) +{ + if (vis[now]) + { + return dis[now]; + } + vis[now] = true; + rep(i, pn[now]) + { + int next = edge[now][i]; + if (now == s && next == d) + { + continue; + } + if (!tsort(n, next, p, s, d)) + { + return false; + } + } + ord[p++] = now; + dis[now] = true; + return true; +} + +bool dp(int n, const int s, const int d) +{ + rep(i, n) maxi[i] = 0; + rep(i, n - 1) + { + int now = ord[i]; + rep(j, pn[now]) + { + int next = edge[now][j]; + maxi[next] = max(maxi[next], maxi[now] + 1); + } + } + return maxi[d] == n - 1; +} + +bool solve(int n) +{ + rep(i, n) + { + if (pn[i] == 1 && in[edge[i][0]] == 1) + { + int p = 0; + rep(j, n) vis[j] = false, dis[j] = false; + if (!tsort(n, edge[i][0], p, i, edge[i][0])) + { + continue; + } + reverse(ord, ord + n); + if (dp(n, edge[i][0], i)) + { + return true; + } + } + } + return false; +} + +main() +{ + int te; + scanf("%d", &te); + while (te--) + { + int n, m; + scanf("%d%d", &n, &m); + rep(i, n) pn[i] = 0, in[i] = 0; + rep(i, m) + { + int f, t; + scanf("%d%d", &f, &t); + edge[f][pn[f]++] = t; + in[t]++; + } + if (m >= n && solve(n)) + { + printf("Yeah, I'm superman\n"); + } + else + { + printf("Your DAGy was initially defected!\n"); + } + } + return false; +} diff --git a/11894.cpp b/11894.cpp new file mode 100644 index 0000000..0158cfe --- /dev/null +++ b/11894.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define N 100005 + +struct line +{ + int x, y; +} a[N], b[N]; +int n; +bool cmp(struct line x, struct line y) +{ + if (x.x != y.x) + { + return x.x < y.x; + } + return x.y < y.y; +} +void rota(int sin, int cos) +{ + int x2, y2, i, x, y; + x2 = a[0].x, y2 = a[0].y; + for (i = 1; i < n; i++) + { + x = x2 + (a[i].x - x2) * cos - (a[i].y - y2) * sin; + y = y2 + (a[i].x - x2) * sin + (a[i].y - y2) * cos; + a[i].x = x, a[i].y = y; + } +} +int main() +{ + int t, i, j, xx, yy, k, flag; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", &a[i].x, &a[i].y); + } + for (i = 0; i < n; i++) + { + scanf("%d%d", &b[i].x, &b[i].y); + } + for (k = 0; k < 4; k++) + { + flag = 0; + rota(1, 0); + sort(a, a + n, cmp); + //printf("\\\\//\n"); + //for(i=0;i + +using namespace std; + +#define sf scanf +#define pf printf +#define pfn printf("\n"); +#define ll long long +#define INF 0x7fffffff + +typedef struct dnode +{ + long long p, q, t; + friend bool operator<(struct dnode n1, struct dnode n2) + { + return n1.q * n2.p < n1.p * n2.q; + } +} node; +int n, k; +priority_queue heap; + +long long gcd(long long a, long long b) +{ + if (b == 0) + { + return a; + } + return gcd(b, a % b); +} + +int main() +{ + int T, g; + node nd; + cin >> T; + while (T--) + { + cin >> n >> k; + while (!heap.empty()) + { + heap.pop(); + } + for (int i = 0; i < n; i++) + { + sf("%lld %lld", &nd.p, &nd.q); + nd.t = nd.p; + heap.push(nd); + } + for (int i = 1; i < k; i++) + { + nd = heap.top(); + heap.pop(); + nd.q *= (nd.t - 1); + nd.p *= nd.t; + heap.push(nd); + } + nd = heap.top(); + g = gcd(nd.p, nd.q); + nd.p /= g; + nd.q /= g; + pf("%lld/%lld\n", nd.q, nd.p); + } + return 0; +} diff --git a/11897.cpp b/11897.cpp new file mode 100644 index 0000000..75b0b81 --- /dev/null +++ b/11897.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +#define N 1005 +#define INF (1 << 30) +#define LL long long +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) > (b) ? (b) : (a)) +#define sqr(x) ((x) * (x)) + +struct node +{ + int v, w; + bool operator<(const node &a) const + { + return w > a.w; + } +}; +struct Point +{ + int u, v, w; +} ed[N * N * 4]; +int n, m, s, num; +int ma[N][N], dis[N], use[N]; +priority_queue que; +void init() +{ + int i, j, t; + scanf("%d%d%d", &n, &m, &s); + num = 0; + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + ma[i][j] = INF; + } + for (int k = 0; k < m; k++) + { + scanf("%d%d%d", &i, &j, &t); + ed[num].u = i; + ed[num].v = j; + ed[num++].w = t; + if (ma[i][j] > t) + { + ma[i][j] = ma[j][i] = t; + } + } +} +void dijstra() +{ + int i, j, k; + double ans, tmp; + node p; + for (i = 0; i < n; i++) + { + dis[i] = ma[s][i]; + p.v = i; + p.w = dis[i]; + if (dis[i] != INF) + { + que.push(p); + } + use[i] = 0; + } + dis[s] = 0; + use[s] = 1; + p.v = s; + p.w = 0; + que.push(p); + while (!que.empty()) + { + p = que.top(); + que.pop(); + use[p.v] = 1; + k = p.v; + for (j = 0; j < n; j++) + if (!use[j] && dis[k] + ma[k][j] < dis[j]) + { + dis[j] = dis[k] + ma[k][j]; + p.v = j; + p.w = dis[j]; + que.push(p); + } + } + for (i = 0; i < n; i++) + if (dis[i] == INF) + { + printf("Sorry Amoo, your domino does not complete!\n"); + return; + } + ans = 0; + for (i = 0; i < m; i++) + { + j = abs(dis[ed[i].v] - dis[ed[i].u]); + j = ed[i].w - j; + if (j < 0) + { + tmp = MAX(dis[ed[i].v], dis[ed[i].u]); + } + else + { + tmp = j; + tmp /= 2.0; + tmp += MAX(dis[ed[i].v], dis[ed[i].u]); + } + if (tmp > ans) + { + ans = tmp; + } + } + printf("%.2lf\n", ans + 0.001); +} +int main() +{ + int ca; + scanf("%d", &ca); + while (ca--) + { + init(); + dijstra(); + } + return 0; +} diff --git a/11898.cpp b/11898.cpp new file mode 100644 index 0000000..a5605a4 --- /dev/null +++ b/11898.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +struct node +{ + int v, id; +} a[200100]; +struct seg +{ + int l, r, ans, m, id; +} q[15010]; +int N, M, pre[15010], net[15010], out[15010]; +bool cmp(node a, node b) +{ + if (a.v == b.v) + { + return a.id < b.id; + } + return a.v < b.v; +} +bool cmp2(seg a, seg b) +{ + if (a.l == b.l) + { + return a.r < b.r; + } + return a.l < b.l; +} +int getint() +{ + int ret = 0; + char tmp; + while (!isdigit(tmp = getchar())) + ; + do + { + ret = (ret << 3) + (ret << 1) + tmp - '0'; + } while (isdigit(tmp = getchar())); + return ret; +} +int main() +{ + int T, i, j, head, p; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (i = 0; i < N; i++) + { + //scanf("%d",&a[i].v); + a[i].v = getint(); + a[i].id = i + 1; + } + sort(a, a + N, cmp); + scanf("%d", &M); + for (i = 0; i < M; i++) + { + //scanf("%d %d",&q[i].l,&q[i].r); + q[i].l = getint(); + q[i].r = getint(); + q[i].ans = 999999999; + q[i].m = -999999999; + pre[i] = max(0, i - 1); + net[i] = i + 1; + q[i].id = i; + } + head = 0; + sort(q, q + M, cmp2); + for (i = 0; i < N; i++) + { + p = head; + while (q[p].l <= a[i].id && p < M) + { + if (q[p].r < a[i].id) + { + p = net[p]; + continue; + } + if (q[p].r - q[p].l + 1 > 10000) + { + q[p].ans = 0; + if (p == head) + { + head = net[p]; + } + else + { + net[pre[p]] = net[p]; + pre[net[p]] = pre[p]; + } + } + else + { + if (q[p].m == -999999999) + { + q[p].m = a[i].v; + } + else if (q[p].m == a[i].v) + { + q[p].ans = 0; + if (p == head) + { + head = net[p]; + } + else + { + net[pre[p]] = net[p]; + pre[net[p]] = pre[p]; + } + } + else if (q[p].m < a[i].v) + { + q[p].ans = min(q[p].ans, a[i].v - q[p].m); + q[p].m = a[i].v; + } + } + p = net[p]; + } + } + for (i = 0; i < M; i++) + { + out[q[i].id] = q[i].ans; + } + for (i = 0; i < M; i++) + { + printf("%d\n", out[i]); + } + } + return 0; +} diff --git a/119.cpp b/119.cpp index 015d4bc..f842307 100644 --- a/119.cpp +++ b/119.cpp @@ -1,74 +1,46 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int N; - bool flag = false; - while( cin >> N ){ - map Map; - vector nam; - int i, j; - FOI(i, 1, N){ - string name; +int main() +{ + int n, money, a; + string name, friends; + vector names; + int cnt = 0; + while (cin >> n) + { + names.clear(); + map group; + for (int i = 0; i < n; i++) + { cin >> name; - nam.push_back(name); - Map[name] = 0; + group[name] = 0; + names.push_back(name); } - FOI(i, 1, N){ - string S, R; - int A, M; - cin >> S >> A >> M; - if( M == 0 ) - continue; - FOI(j, 1, M){ - cin >> R; - Map[R] += (A / M); - Map[S] -= (A / M); + for (int i = 0; i < n; i++) + { + cin >> name >> money >> a; + if (a > 0) + { + group[name] += money % a; + group[name] -= money; + for (int j = 0; j < a; j++) + { + cin >> friends; + group[friends] += money / a; + } } - Map[S] += (A % M); } - if( flag ) - cout << endl; - FOI(i, 0, N-1) - cout << nam[i] << " " << Map[nam[i]] << endl; - flag = true; + if (cnt > 0) + { + printf("\n"); + } + for (int i = 0; i < names.size(); i++) + { + cout << names[i] << " " << group[names[i]] << endl; + } + cnt++; } return 0; } diff --git a/11900.cpp b/11900.cpp index f06b9ff..38d8749 100644 --- a/11900.cpp +++ b/11900.cpp @@ -1,57 +1,36 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define EPS 1e-10 -#define sqr(x) (x)*(x) - -int main(){ - int T, t; - cin >> T; - FOI(t, 1, T){ - int N, P, Q, i; - cin >> N >> P >> Q; - int vec[N]; - FOI(i, 0, N-1) - cin >> vec[i]; - int wet = 0, cnt = 0; - for(i=0; i Q ) - break; - wet += vec[i]; - cnt++; - } - cout << "Case " << t << ": " << cnt << endl; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +// link: http://uva.onlinejudge.org/external/119/11900.html +// Runtime: 0.008s +// Tag: Adhoc + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n, p, q; + scanf("%d %d %d", &n, &p, &q); + int eggWeight[30 + 3]; + for (int i = 0; i < n; i++) + { + scanf("%d", &eggWeight[i]); + } + sort(eggWeight, eggWeight + n); + int cnt = 0; + for (int i = 0; i < min(p, n); i++) + { + q -= eggWeight[i]; + if (q >= 0) + { + cnt++; + } + } + printf("Case %d: %d\n", ++cases, cnt); + } + return 0; +} diff --git a/11901.cpp b/11901.cpp new file mode 100644 index 0000000..600ab85 --- /dev/null +++ b/11901.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); + +struct point +{ + double x, y; + point() {} + point(double x, double y) : x(x), y(y) {} + + void input() + { + scanf("%lf%lf", &x, &y); + } + + void output() + { + cout << x << " " << y << endl; + } +} A, B, C, D; +double a1, b1, a2, b2, ang1, ang2; + +queue q; +vector ans; + +const double eps = 1e-6; + +int sgn(double x) +{ + return x < -eps ? -1 : x > eps; +} + +point operator-(point a, point b) +{ + return point(a.x - b.x, a.y - b.y); +} + +point operator+(point a, point b) +{ + return point(a.x + b.x, a.y + b.y); +} + +point operator*(double k, point a) +{ + return point(k * a.x, k * a.y); +} + +double dist(point a, point b) +{ + a.x -= b.x, a.y -= b.y; + return sqrt(a.x * a.x + a.y * a.y); +} + +void rotate(point &a, double ang) +{ + a = point(a.x * cos(ang) - a.y * sin(ang), a.x * sin(ang) + a.y * cos(ang)); +} + +point to_p(double ang) +{ + return point(a1 * cos(ang), b1 * sin(ang)); +} + +double cal(point a) +{ + return dist(a, C) + dist(a, D) - 2 * a2; +} + +void solve(point u) +{ + double l = u.x, r = u.y; + int sgnl = sgn(cal(to_p(l))); + int sgnr = sgn(cal(to_p(r))); + if (sgnl * sgnr > 0) + { + double low = l, high = r; + for (int tim = 0; tim < 50; tim++) + { + double m1 = (2 * low + high) / 3; + double m2 = (low + 2 * high) / 3; + double d1 = cal(to_p(m1)); + double d2 = cal(to_p(m2)); + if ((sgnl > 0) ^ (d1 > d2)) + { + high = m2; + } + else + { + low = m1; + } + } + if (sgn(cal(to_p(low))) != sgnl) + { + q.push(point(l, low)); + q.push(point(low, r)); + } + } + else if (sgnl * sgnr < 0) + { + double low = l, high = r; + for (int tim = 0; tim < 50; tim++) + { + double mid = (low + high) / 2; + if ((sgnl > 0) ^ (cal(to_p(mid)) > 0.0)) + { + high = mid; + } + else + { + low = mid; + } + } + low = (low + high) / 2; + q.push(point(l, low)); + q.push(point(low, r)); + } + else + { + if (sgnl == 0) + { + ans.push_back(l); + while (!sgn(cal(to_p(l)))) + { + l += 1e-3; + } + } + if (sgnr == 0) + { + ans.push_back(r); + while (!sgn(cal(to_p(r)))) + { + r -= 1e-3; + } + } + if (sgn(r - l) >= 0) + { + q.push(point(l, r)); + } + } +} + +int main() +{ + int cas = 0; + while (scanf("%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &A.x, &A.y, &a1, &b1, &ang1, &B.x, &B.y, &a2, &b2, &ang2) != EOF) + { + ang1 /= 180.0 / PI; + ang2 /= 180.0 / PI; + if (a1 < b1) + { + swap(a1, b1); + ang1 -= PI / 2; + } + if (a2 < b2) + { + swap(a2, b2); + ang2 -= PI / 2; + } + B = B - A; + rotate(B, -ang1); + ang2 = ang2 - ang1; + point v = point(cos(ang2), sin(ang2)); + C = B + sqrt(a2 * a2 - b2 * b2) * v; + D = B - sqrt(a2 * a2 - b2 * b2) * v; + ans.clear(); + while (!q.empty()) + { + q.pop(); + } + q.push(point(0.0, PI / 2)); + q.push(point(PI / 2, PI)); + q.push(point(PI, PI / 2 * 3)); + q.push(point(PI / 2 * 3, 2 * PI)); + while (!q.empty()) + { + solve(q.front()); + q.pop(); + } + sort(ans.begin(), ans.end()); + vector res; + for (int i = 0; i < ans.size(); i++) + { + if (!i || sgn(ans[i] - ans[i - 1])) + { + point t = to_p(ans[i]); + rotate(t, ang1); + t = t + A; + res.push_back(t.x + t.y); + } + } + printf("Case %d:\n", ++cas); + if (res.empty()) + { + puts("Do_not_intersect."); + } + else + { + sort(res.begin(), res.end()); + for (int i = 0; i < res.size(); i++) + { + printf("%.8f\n", res[i]); + } + } + } + return 0; +} diff --git a/11902.cpp b/11902.cpp index 1b5232e..3a843fa 100644 --- a/11902.cpp +++ b/11902.cpp @@ -1,61 +1,91 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) -#define INF INT_MAX -#define EPS 1e-10 -#define sqr(x) (x)*(x) - -int main(){ - int T, t; - cin >> T; - FOI(t, 1, T){ - int N, i, j, k; - cin >> N; - int mat[N][N]; - FOI(i, 0, N-1) - FOI(j, 0, N-1) - cin >> mat[i][j]; - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = min ( mat[i][j], mat[i][k] + mat[k][j] ); - FOI(i, 0, N-1){ - FOI(j, 0, N-1) - cout << mat[i][j] << " "; - cout << endl; - } - } - system("pause"); - return 0; -} +#include + +using namespace std; + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int n; +bool mat[100 + 3][100 + 3], vis[100 + 3]; + +void printLine() +{ + printf("+"); + for (int i = 0; i < n * 2 - 1; i++) + printf("-"); + printf("+\n"); +} + +void dfs(int at, int absent) +{ + if (at == absent) + return; + vis[at] = true; + for (int i = 0; i < n; i++) + if (mat[at][i] && !vis[i]) + dfs(i, absent); +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + scanf("%d", &n); + Set(mat, false); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int a; + scanf("%d", &a); + mat[i][j] = a; + } + } + bool output[100 + 3][100 + 3]; + bool firstVis[100 + 3]; + Set(vis, false); + dfs(0, -1); + for (int i = 0; i < 103; i++) + firstVis[i] = vis[i]; + for (int i = 0; i < n; i++) + { + Set(vis, false); + dfs(0, i); + for (int j = 0; j < n; j++) + { + if (firstVis[j] && !vis[j]) + output[i][j] = true; + else + output[i][j] = false; + } + output[i][i] = firstVis[i]; + } + printf("Case %d:\n", ++cases); + printLine(); + for (int i = 0; i < n; i++) + { + printf("|"); + for (int j = 0; j < n; j++) + { + if (output[i][j]) + printf("Y|"); + else + printf("N|"); + } + printf("\n"); + printLine(); + } + } + return 0; +} diff --git a/11903.cpp b/11903.cpp new file mode 100644 index 0000000..685afd6 --- /dev/null +++ b/11903.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int N, K, memo[(1 << 12)][12][12]; +bool enemy[12][12]; + +int solve(int mask, int last, int r) +{ + if (r < 0) + { + return 0; + } + if (mask == (1 << N) - 1) + { + return 1; + } + int &ret = memo[mask][last][r]; + if (ret == -1) + { + ret = 0; + for (int i = 0; i < N; ++i) + if ((mask & (1 << i)) == 0) + { + ret += solve(mask | (1 << i), i, enemy[i][last] ? r - 1 : r); + } + } + return ret; +} + +int main() +{ + int tc = 1, T, Q, r; + scanf("%d", &T); + while (T--) + { + scanf("%d %d %d", &N, &K, &Q); + memset(enemy, false, sizeof(enemy)); + for (int i = 0, x, y; i < N; ++i) + { + scanf("%d", &x); + for (int j = 0; j < x; ++j) + { + scanf("%d", &y); + enemy[i][--y] = true; + } + } + memset(memo, -1, sizeof(memo)); + int fac = 1; + for (int i = 2; i <= N; ++i) + { + fac *= i; + } + printf("Case %d:\n", tc++); + while (Q--) + { + scanf("%d", &r); + if (K == 0) + { + printf("%d\n", fac); + } + else + { + r /= K; + if (r >= N) + { + printf("%d\n", fac); + } + else + { + int ans = 0; + for (int i = 0; i < N; ++i) + { + ans += solve((1 << i), i, r); + } + printf("%d\n", ans); + } + } + } + } + return 0; +} diff --git a/11904.cpp b/11904.cpp new file mode 100644 index 0000000..b4498cf --- /dev/null +++ b/11904.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +typedef long long i64; + +#define MAXN 1000 +#define MOD 1000000007 +#define MAXU 1000000 + +int n, ks[MAXN]; +i64 fact[MAXU + 1]; + +void ext_euclid(int a, int b, int &x, int &y, int &gcd) +{ + x = 0; + y = 1; + gcd = b; + int m, n, q, r; + for (int u = 1, v = 0; a != 0; gcd = a, a = r) + { + q = gcd / a, r = gcd % a; + m = x - u * q, n = y - v * q; + x = u, y = v, u = m, v = n; + } +} + +int mod_inv(int n, int m) +{ + int x, y, gcd; + ext_euclid(n, m, x, y, gcd); + if (gcd == 1) + return x % m; + return 0; +} + +int choose(int n, int k) +{ + if (k > n) + return 0; + i64 num = fact[n]; + i64 den = fact[k] * fact[n - k] % MOD; + i64 ans = num * mod_inv(den, MOD) % MOD; + if (ans < 0) + ans += MOD; + return ans; +} + +int solve() +{ + i64 ans = 1; + int m = ks[0];// # of units processed so far + for (int i = 1; i < n; ++i) + { + // # of "buckets" to place the next units + int p = m + 1; + // # of units to place in the buckets + // subtracting one that has to be left for last + int q = ks[i] - 1; + int ways = choose(p + q - 1, p - 1); + ans = ans * ways % MOD; + m += ks[i]; + } + return ans; +} + +void pre() +{ + i64 x = 1; + fact[0] = 1; + for (int i = 1; i <= MAXU; ++i) + { + x = x * i % MOD; + fact[i] = x; + } +} + +int main() +{ + pre(); + int T; + scanf("%d", &T); + int ncase = 0; + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; ++i) + scanf("%d", &ks[i]); + printf("Case %d: %d\n", ++ncase, solve()); + } + return 0; +} diff --git a/11906.cpp b/11906.cpp new file mode 100644 index 0000000..7e7e3ef --- /dev/null +++ b/11906.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int dx[8], dy[8]; +int mark[105][105]; +int n, m, odd, even; + +void gao(int x, int y) +{ + mark[x][y] = 1; + int cnt = 0; + for (int i = 0; i < 8; i++) + { + int nx = x + dx[i], ny = y + dy[i]; + if (nx >= 0 && nx < n && ny >= 0 && ny < m && mark[nx][ny] != -1) + { + int has = 0; + for (int j = 0; j < i; j++) + { + int tx = x + dx[j], ty = y + dy[j]; + if (tx == nx && ty == ny) + { + has = 1; + break; + } + } + if (!has) + { + cnt++; + } + if (mark[nx][ny] == 0) + { + gao(nx, ny); + } + } + } + if (cnt % 2 == 0) + { + even++; + } + else + { + odd++; + } +} + +int main() +{ + int cas, r = 1; + scanf("%d", &cas); + while (cas--) + { + int x, y; + scanf("%d%d%d%d", &n, &m, &x, &y); + dx[0] = x, dx[1] = x, dx[2] = -x, dx[3] = -x, dx[4] = y, dx[5] = y, dx[6] = -y, dx[7] = -y; + dy[0] = y, dy[1] = -y, dy[2] = y, dy[3] = -y, dy[4] = x, dy[5] = -x, dy[6] = x, dy[7] = -x; + odd = 0, even = 0; + memset(mark, 0, sizeof(mark)); + int k; + scanf("%d", &k); + for (int i = 0; i < k; i++) + { + scanf("%d%d", &x, &y); + mark[x][y] = -1; + } + gao(0, 0); + printf("Case %d: %d %d\n", r++, even, odd); + } +} diff --git a/11907.cpp b/11907.cpp new file mode 100644 index 0000000..98dc17d --- /dev/null +++ b/11907.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define EPS 1E-11 + +int N; +double X[50][2], Y[50][2]; +double calc_dist() +{ + double ret = 20000; + for (int i = 0; i < N; ++i) + for (int j = i + 1; j < N; ++j) + { + double x1 = max(X[i][0], X[j][0]), x2 = min(X[i][1], X[j][1]); + double y1 = max(Y[i][0], Y[j][0]), y2 = min(Y[i][1], Y[j][1]); + if (x1 < x2 && y1 < y2) + { + ret = 0; + } + double aux = 20000; + if (x1 < x2) + { + aux = min(aux, min(abs(Y[i][0] - Y[j][1]), abs(Y[j][0] - Y[i][1]))); + } + if (y1 < y2) + { + aux = min(aux, min(abs(X[i][0] - X[j][1]), abs(X[j][0] - X[i][1]))); + } + for (int k1 = 0; k1 < 4; ++k1) + for (int k2 = 0; k2 < 4; ++k2) + { + aux = min(aux, sqrt((X[j][k2 & 1] - X[i][k1 & 1]) * (X[j][k2 & 1] - X[i][k1 & 1]) + (Y[j][k2 >> 1] - Y[i][k1 >> 1]) * (Y[j][k2 >> 1] - Y[i][k1 >> 1]))); + } + ret = min(ret, aux); + } + return ret; +} +int main() +{ + int tc = 1, T, r, d; + int x[50][2], y[50][2]; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; ++i) + { + scanf("%d %d %d %d", &x[i][0], &y[i][0], &x[i][1], &y[i][1]); + } + scanf("%d %d", &r, &d); + double lo = 0, hi = 5000, mi; + for (int it = 0; it < 50; ++it) + { + mi = (lo + hi) / 2; + for (int i = 0; i < N; ++i) + { + X[i][0] = x[i][0] - r * mi; + X[i][1] = x[i][1] + r * mi; + Y[i][0] = y[i][0] - r * mi; + Y[i][1] = y[i][1] + r * mi; + } + if (calc_dist() < d) + { + hi = mi; + } + else + { + lo = mi; + } + } + //lo = round(lo*1000)/1000; + printf("Case %d: %.3f\n", tc++, lo + EPS); + } + return 0; +} diff --git a/11908.cpp b/11908.cpp new file mode 100644 index 0000000..8ca3df9 --- /dev/null +++ b/11908.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/119/11908.html +// Runtime: 0.192s + +// @BEGIN_OF_SOURCE_CODE + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 200000 +#define LL unsigned long long + +struct floors +{ + int start; + int end; + int cost; +} a[30000 + 10]; + +int dp[N + 10]; + +bool cmp(floors p, floors q) +{ + if (p.start < q.start) + { + return true; + } + return false; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d %d %d", &a[i].start, &a[i].end, &a[i].cost); + a[i].start++; // starting floor + a[i].end = a[i].end + a[i].start - 1;// ending floor + } + sort(a, a + n, cmp); + memset(dp, 0, sizeof(dp)); + int ind = 0; + int accu_cost = 0; + for (int i = 1; i < N + 10; i++) + { + while (ind < n && a[ind].start == i) + { + int p = dp[i - 1] + a[ind].cost; + if (dp[a[ind].end] < p) + { + dp[a[ind].end] = p; + } + ind++; + } + if (accu_cost < dp[i]) + { + accu_cost = dp[i]; + } + dp[i] = accu_cost; + } + int maxi = 0; + for (int i = 0; i < N + 10; i++) + { + if (maxi < dp[i]) + { + maxi = dp[i]; + } + } + printf("Case %d: %d\n", ++cases, maxi); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/11909.cpp b/11909.cpp index 0fd62b8..0a7db9e 100644 --- a/11909.cpp +++ b/11909.cpp @@ -1,22 +1,36 @@ -#include -#include -#include -using namespace std; +#include -const double PI = acos(-1.0); -const double EPS = 1e-9; +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - double l, w, h, theta; - while( scanf("%lf%lf%lf%lf", &l, &w, &h, &theta) != EOF ){ - double V; - double p = l * tan(theta * PI / 180.0); - if (p <= h) - V = l * w * h - 0.5 * l * l * w * tan(theta * PI / 180.0); +int main() +{ + int L, W, H, G; + double leche, area, cateto, tmp = 3.14159265358979 / 180; + while (scanf("%d %d %d %d", &L, &W, &H, &G) != EOF) + { + cateto = L * sin(G * tmp) / sin((90 - G) * tmp); + if (G == 90) + { + printf("0 mL\n"); + } + else if (cateto <= H) + { + cateto = L * sin(G * tmp) / sin((90 - G) * tmp); + area = L * cateto * W / 2; + leche = (L * W * H) - area; + printf("%0.3lf mL\n", leche); + } + else if (G) + { + cateto = H * sin((90 - G) * tmp) / sin(G * tmp); + area = H * cateto * W / 2; + printf("%0.3lf mL\n", area); + } else - V = 0.5 * h * h * w * tan((90.0 - theta) * PI / 180.0); - printf("%.3lf mL\n", V); + { + leche = (L * W * H); + printf("%0.3lf mL\n", leche); + } } + return 0; } diff --git a/11910.cpp b/11910.cpp new file mode 100644 index 0000000..5ecfe71 --- /dev/null +++ b/11910.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +class Frac +{ +public: + int x, y; + Frac(int a = 0, int b = 0) : x(a), y(b) {} + double toDouble() const + { + return (double)x / y; + } + bool operator<(const Frac &a) const + { + return toDouble() < a.toDouble(); + } +} D[1048576]; +double DD[1048576]; +double cmpv; +bool cmp(Frac a, Frac b) +{ + return fabs(a.toDouble() - cmpv) < fabs(b.toDouble() - cmpv); +} +int f(double v) +{ + if (v >= 1000) + return 4; + if (v >= 100) + return 3; + if (v >= 10) + return 2; + return 1; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int n = 0; + for (int y = 1; y <= 1000; y++) + { + for (int x = 1; x <= 1000; x++) + { + if (__gcd(x, y) == 1) + D[n++] = Frac(x, y); + } + } + sort(D, D + n); + for (int i = 0; i < n; i++) + DD[i] = D[i].toDouble(); + char s[32]; + double v; + while (scanf("%s", s) == 1) + { + printf("Input : %s\n", s); + sscanf(s, "%lf", &v); + int pos = lower_bound(DD, DD + n, v) - DD; + vector A; + for (int i = max(0, pos - 6); i < min(n, pos + 6); i++) + A.push_back(D[i]); + cmpv = v; + sort(A.begin(), A.end(), cmp); + for (int i = 0, j = 1; i < 3 && i < A.size(); i++, j++) + printf(" %d : %*.*lf = %4d / %d\n", j, f(A[i].toDouble()), 10 - f(A[i].toDouble()), A[i].toDouble(), A[i].x, A[i].y); + } + return 0; +} diff --git a/11911.cpp b/11911.cpp new file mode 100644 index 0000000..be7e2bd --- /dev/null +++ b/11911.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#ifndef ONLINE_JUDGE +ifstream fin("test.in"); +#else +#define fin cin +#endif + +int n, m; +int row; +const int maxn = 105; +double graph[maxn][maxn]; +int invalid[maxn]; + +int themax() +{ + for (int i = n; i >= 1; i--) + if (invalid[i] == 0) + { + return i; + } +} +int themin() +{ + for (int i = 1; i <= n; i++) + if (invalid[i] == 0) + { + return i; + } +} +int main() +{ + int ncase = 0; + while (fin >> n >> m) + { + if (ncase == 0) + { + ncase++; + } + else + { + cout << endl; + } + memset(invalid, 0, sizeof(invalid)); + memset(graph, 0, sizeof(graph)); + row = (int)sqrt(n); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + double x1; + if (i % row == 0) + { + x1 = i / row; + } + else + { + x1 = i / row + 1; + } + double y1; + if (i % row == 0) + { + y1 = row; + } + else + { + y1 = i % row; + } + double x2; + if (j % row == 0) + { + x2 = j / row; + } + else + { + x2 = j / row + 1; + } + double y2; + if (j % row == 0) + { + y2 = row; + } + else + { + y2 = j % row; + } + graph[i][j] = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + } + } + int guess = 1; + double lastdist = INT_MAX; + int lastguess; + bool guessthemax = true;//??·???×??ó?? + while (guess != m) + { + // cout<<" "<= graph[lastguess][i]) + { + invalid[i] = 1; + } + } + } + } + else + { + if (lastdist == graph[guess][m]) + { + cout << "SAME" << endl; + { + for (int i = 1; i <= n; i++) + { + if (graph[i][guess] != graph[i][lastguess]) + { + invalid[i] = 1; + } + } + } + } + else + { + cout << "FARTHER" << endl; + for (int i = 1; i <= n; i++) + { + if (graph[guess][i] <= graph[lastguess][i]) + { + invalid[i] = 1; + } + } + } + } + lastdist = graph[guess][m]; + lastguess = guess; + if (guessthemax) + { + guess = themax(); + } + else + { + guess = themin(); + } + guessthemax = !guessthemax; + } + // cout<<" "< + +using namespace std; + +struct T +{ + int s, e; + int v; +} t[133]; + +bool cmp(T a, T b) +{ + if (a.s == b.s) + { + return a.e < b.e; + } + return a.s < b.s; +} + +int main() +{ + int n, i, k, j, ca = 0; + char ch[1024]; + while (scanf("%d", &n) == 1 && n) + { + int ans = 0; + t[0].s = 6 * 60; + t[0].e = 6 * 60; + t[0].v = 0; + for (i = 1; i <= n; i++) + { + int h, m, hh, mm; + scanf("%d:%d-%d:%d %d", &h, &m, &hh, &mm, &t[i].v); + gets(ch); + t[i].s = h * 60 + m; + if (t[i].s < 6 * 60) + { + t[i].s += 24 * 60; + } + t[i].e = hh * 60 + mm; + if (t[i].e < t[i].s) + { + t[i].e += 24 * 60; + } + ans = max(ans, t[i].v); + } + sort(t, t + n + 1, cmp); + int dp[122][122]; + memset(dp, -1, sizeof(dp)); + dp[0][0] = 0; + for (i = 0; i <= n; i++) + for (j = i; j <= n; j++) + if (dp[i][j] != -1) + { + ans = max(ans, dp[i][j]); + for (k = 1; k <= n; k++) + { + if (t[k].s >= t[i].e && t[k].e <= t[j].s && i) + { + continue; + } + if (k == j || k == i) + { + continue; + } + if (t[k].s >= max(t[j].e, t[i].e)) + { + dp[i][k] = max(dp[i][k], dp[i][j] + t[k].v); + dp[j][k] = max(dp[j][k], dp[i][j] + t[k].v); + } + else if (t[k].s >= min(t[j].e, t[i].e)) + { + if (t[j].e >= t[i].e) + { + dp[j][k] = max(dp[j][k], dp[i][j] + t[k].v); + } + else + { + dp[i][k] = max(dp[i][k], dp[i][j] + t[k].v); + } + } + } + } + printf("Case %d: %d\n", ++ca, ans); + } + return 0; +} diff --git a/11916.cpp b/11916.cpp new file mode 100644 index 0000000..dc40ba3 --- /dev/null +++ b/11916.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +#define mod 100000007 +#define MOD 60000 + +typedef long long LL; + +struct P +{ + int x, y; + bool operator<(const P &a) const + { + return y == a.y ? x < a.x : y < a.y; + } +} p[550]; + +int head[MOD], nxt[MOD * 10], hs[MOD * 10], id[MOD * 10], top; + +LL exp(LL b, int p) +{ + LL a = 1; + for (; p; p >>= 1, b = b * b % mod) + if (p & 1) + { + a = a * b % mod; + } + return a; +} + +void insert(int x, int y) +{ + int k = x % MOD; + hs[top] = x, id[top] = y, nxt[top] = head[k], head[k] = top++; +} + +int find(int x) +{ + int k = x % MOD; + for (int i = head[k]; i; i = nxt[i]) + { + if (hs[i] == x) + { + return id[i]; + } + } + return -1; +} + +int gcd(int n, int m) +{ + int t; + while (m) + { + t = n % m, n = m, m = t; + } + return n; +} + +int BSGS(int a, int b, int n, int x) +{ + memset(head, 0, sizeof(head)); + top = 1; + int m = sqrt(n + .0), j; + LL p = 1; + for (int i = 0; i < m; ++i, p = p * a % n) + { + insert(p * b % n, i); + } + for (LL i = m; i < n; i += m) + { + if ((j = find(x = x * p % n)) != -1) + { + return i - j + 30; + } + } + return -1; +} + +int dislog(int a, int b, int n, LL t) +{ + if (t == b) + { + return 0; + } + for (int i = 1; i <= 30; ++i) + { + if ((t = t * a % n) == b) + { + return i; + } + } + int d, tb = b; + while ((d = gcd(a, n)) > 1) + { + if (tb % d) + { + return -1; + } + n /= d, tb /= d; + } + return BSGS(a % n, b % n, n, t % n); +} + +int main() +{ + int T, n, K, m, b; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d%d%d", &n, &K, &m, &b); + int R = 0; + for (int i = 0; i < m; ++i) + { + scanf("%d%d", &p[i].x, &p[i].y); + if (p[i].x > R) + { + R = p[i].x; + } + } + sort(p, p + m); + LL a = 1; + if (m > 0) + { + int cnt = 0; + for (int j = 0; j < m;) + { + ++cnt; + int pre = j; + while (j < m && p[j].y == p[pre].y) + { + ++j; + } + int x = j - pre, y = 1; + for (int k = pre; k < j;) + { + int nk; + for (nk = k + 1; nk < j && p[nk].x == p[nk - 1].x + 1; ++nk) + ; + if (p[k].x != 1 && p[nk - 1].x != R) + { + ++y; + } + k = nk; + } + if (x < R) + { + a = a * exp(K, y) % mod * exp(K - 1, R - x - y) % mod; + } + } + a = a * exp(K * exp(K - 1, R - 1) % mod, n - cnt) % mod; + } + printf("Case %d: ", cas); + if (a == b) + { + printf("%d\n", R); + continue; + } + int cnt = 0; + if (m == 0) + { + cnt = n; + } + else + { + for (int i = 0; i < m; ++i) + if (p[i].x == R) + { + ++cnt; + } + } + a = a * exp(K, cnt) % mod * exp(K - 1, n - cnt) % mod; + LL ta = exp(K - 1, n); + LL ans = dislog(ta, b, mod, a) + R + 1; + printf("%lld\n", ans); + } + return 0; +} diff --git a/11917.cpp b/11917.cpp index 30a2a9a..cf5c202 100644 --- a/11917.cpp +++ b/11917.cpp @@ -1,36 +1,37 @@ -#include -#include -#include -#include -#include -#include +#include + using namespace std; -int main(){ - int T; - cin >> T; - for( int t=1; t<=T; t++ ){ - int N, D, V; - string str; - cin >> N; - map sub; - map ::iterator it; - while( N-- ){ - cin >> str >> V; - sub[str] = V; +int main() +{ + int t, n, d; + string input; + map::iterator it; + cin >> t; + for (int c = 0; c < t; c++) + { + cin >> n; + map subjects; + while (n--) + { + cin >> input >> d; + subjects[input] = d; + } + cin >> d >> input; + it = subjects.find(input); + cout << "Case " << c + 1 << ": "; + if (it != subjects.end() && subjects[input] <= d) + { + cout << "Yesss" << endl; } - cin >> D >> str; - if( sub.find(str) != sub.end() ){ - int time = sub[str]; - if( time <= D ) - cout << "Case " << t << ": Yesss" << endl; - else if( time <= D + 5 ) - cout << "Case " << t << ": Late" << endl; - else - cout << "Case " << t << ": Do your own homework!" << endl; + else if (it != subjects.end() && subjects[input] <= d + 5) + { + cout << "Late" << endl; } else - cout << "Case " << t << ": Do your own homework!" << endl; + { + cout << "Do your own homework!" << endl; + } } return 0; } diff --git a/11918.cpp b/11918.cpp new file mode 100644 index 0000000..a4ef6d1 --- /dev/null +++ b/11918.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +struct point +{ + long x, y; +}; +struct rect +{ + point lefttop, rightbottom; + void normalize() + { + long minx = min(lefttop.x, rightbottom.x), maxx = max(lefttop.x, rightbottom.x); + long miny = min(lefttop.y, rightbottom.y), maxy = max(lefttop.y, rightbottom.y); + lefttop.x = minx; + lefttop.y = miny; + rightbottom.x = maxx; + rightbottom.y = maxy; + } + static bool overlap(rect a, rect b) + { + return a.lefttop.x <= b.rightbottom.x && b.lefttop.x <= a.rightbottom.x && + a.lefttop.y <= b.rightbottom.y && b.lefttop.y <= a.rightbottom.y; + } +}; + +#define MAXMONSTERS 100 +#define MAXVERTICES (4 * (MAXMONSTERS) + 2) +#define MINX -1000000000 +#define MAXX 1000000000 +#define MINY -1000000000 +#define MAXY 1000000000 +#define ULLMAX ((unsigned long long)(-1LL)) + +rect monsters[MAXMONSTERS]; +point vertices[MAXVERTICES]; +unsigned long long dist[MAXVERTICES][MAXVERTICES]; +bool opened[MAXVERTICES]; +unsigned long long fromsource[MAXVERTICES]; + +void gen() +{ + printf("100\n"); + for (int i = 0; i < 100; i++) + { + printf("-1000000000 -1000000000 1000000000 1000000000\n"); + printf("0 100 0\n"); + for (int j = 0; j < 100; j++) + if (j & 1) + { + printf("%d -999999999 %d 1000000000\n", 2 * j, 2 * j); + } + else + { + printf("%d -1000000000 %d 999999999\n", 2 * j, 2 * j); + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 0; t < T; t++) + { + long sourceX, sourceY, destinationX, destinationY; + scanf("%ld %ld %ld %ld", &sourceX, &sourceY, &destinationX, &destinationY); + vertices[0].x = sourceX; + vertices[0].y = sourceY; + vertices[1].x = destinationX; + vertices[1].y = destinationY; + int nv = 2; + int M, N, Q; + scanf("%d %d %d", &M, &N, &Q); + for (int i = 0; i < M; i++) + { + long u, v; + scanf("%ld %ld", &u, &v); + monsters[i].lefttop.x = u; + monsters[i].lefttop.y = v; + monsters[i].rightbottom.x = u; + monsters[i].rightbottom.y = v; + } + for (int i = 0; i < N + Q; i++) + { + long u1, v1, u2, v2; + scanf("%ld %ld %ld %ld", &u1, &v1, &u2, &v2); + monsters[i + M].lefttop.x = u1; + monsters[i + M].lefttop.y = v1; + monsters[i + M].rightbottom.x = u2; + monsters[i + M].rightbottom.y = v2; + } + int nm = N + M + Q; + for (int i = 0; i < nm; i++) + { + monsters[i].normalize(); + point corners[] = + { + {monsters[i].lefttop.x - 1, monsters[i].lefttop.y - 1}, + {monsters[i].rightbottom.x + 1, monsters[i].lefttop.y - 1}, + {monsters[i].rightbottom.x + 1, monsters[i].rightbottom.y + 1}, + {monsters[i].lefttop.x - 1, monsters[i].rightbottom.y + 1}}; + for (int j = 0; j < 4; j++) + if (corners[j].x >= MINX && corners[j].x <= MAXX && corners[j].y >= MINY && corners[j].y <= MAXY) + { + vertices[nv].x = corners[j].x; + vertices[nv].y = corners[j].y; + nv++; + } + } + for (int i = 0; i < nv - 1; i++) + for (int j = i + 1; j < nv; j++) + { + rect a = {vertices[i].x, vertices[i].y, vertices[j].x, vertices[j].y}; + a.normalize(); + bool freeway = true; + for (int k = 0; k < nm && freeway; k++) + if (rect::overlap(a, monsters[k])) + { + freeway = false; + } + dist[i][j] = dist[j][i] = !freeway ? ULLMAX : (unsigned long long)abs(vertices[i].x - vertices[j].x) + (unsigned long long)abs(vertices[i].y - vertices[j].y); + } + memset(opened, 0, sizeof(opened)); + fromsource[0] = 0; + for (int i = 1; i < nv; i++) + { + fromsource[i] = ULLMAX; + } + int toopen = 0; + while (toopen != 1 /*dest*/ && toopen != -1 /*all accessible opened*/) + { + opened[toopen] = true; + for (int i = 0; i < nv; i++) + if (dist[toopen][i] != ULLMAX && !opened[i]) + { + fromsource[i] = min(fromsource[i], fromsource[toopen] + dist[toopen][i]); + } + toopen = -1; + unsigned long long nearest = ULLMAX; + for (int i = 0; i < nv; i++) + if (!opened[i] && fromsource[i] < nearest) + { + nearest = fromsource[i]; + toopen = i; + } + } + if (fromsource[1] != ULLMAX) + { + printf("Case %d: %llu\n", t + 1, fromsource[1]); + } + else + { + printf("Case %d: Impossible\n", t + 1); + } + } + return 0; +} diff --git a/11919.cpp b/11919.cpp new file mode 100644 index 0000000..411ce55 --- /dev/null +++ b/11919.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +const int MaxN = 20; + +struct point +{ + double x, y; + point() {} + point(double x, double y) : x(x), y(y) {} + + void output() + { + cout << x << " " << y << endl; + } +}; + +char typ[MaxN]; +double A[MaxN], B[MaxN], R[MaxN]; +int N; +double len[MaxN][MaxN]; + +point operator-(point a, point b) +{ + return point(a.x - b.x, a.y - b.y); +} + +double operator&(point a, point b) +{ + return a.x * b.x + a.y * b.y; +} + +double dist(point a, point b) +{ + return sqrt((a - b) & (a - b)); +} + +double distps(point c, point a, point b) +{ + if (c.x >= a.x && c.x <= b.x) + { + return min(abs(c.y - a.y), abs(c.y - b.y)); + } + if (c.y >= a.y && c.y <= b.y) + { + return min(abs(c.x - a.x), abs(c.x - b.x)); + } + double ret = min(dist(a, c), dist(b, c)); + ret = min(ret, dist(c, point(a.x, b.y))); + ret = min(ret, dist(c, point(b.x, a.y))); + return ret; +} + +double distcc(point a, double ra, point b, double rb) +{ + return dist(a, b) - ra - rb; +} + +double distcs(point c, double r, point a, point b) +{ + return distps(c, a, b) - r; +} + +double distss(point a, point b, point c, point d) +{ + double ret = min(distps(a, c, d), distps(b, c, d)); + ret = min(ret, distps(point(a.x, b.y), c, d)); + ret = min(ret, distps(point(b.x, a.y), c, d)); + swap(a, c); + swap(b, d); + ret = min(ret, min(distps(a, c, d), distps(b, c, d))); + ret = min(ret, distps(point(a.x, b.y), c, d)); + ret = min(ret, distps(point(b.x, a.y), c, d)); + return ret; +} + +void DIST(int i, int j) +{ + if (typ[i] == 'C' && typ[j] == 'C') + { + point p = point(A[i], B[i]); + point q = point(A[j], B[j]); + len[i][j] = len[j][i] = distcc(p, R[i], q, R[j]); + } + else if (typ[i] == 'S' && typ[j] == 'S') + { + point p1 = point(A[i], B[i]); + point p2 = point(A[i] + R[i], B[i] + R[i]); + point q1 = point(A[j], B[j]); + point q2 = point(A[j] + R[j], B[j] + R[j]); + len[i][j] = len[j][i] = distss(p1, p2, q1, q2); + } + else if (typ[i] == 'C' && typ[j] == 'S') + { + point p = point(A[i], B[i]); + point q1 = point(A[j], B[j]); + point q2 = point(A[j] + R[j], B[j] + R[j]); + len[i][j] = len[j][i] = distcs(p, R[i], q1, q2); + } + else + { + point q = point(A[j], B[j]); + point p1 = point(A[i], B[i]); + point p2 = point(A[i] + R[i], B[i] + R[i]); + len[i][j] = len[j][i] = distcs(q, R[j], p1, p2); + } +} + +double dp[1 << 15][15]; +int ones[1 << 15]; + +void solve() +{ + for (int s = 0; s < (1 << N); s++) + for (int i = 0; i < N; i++) + { + dp[s][i] = -1; + } + dp[1][0] = 0.0; + for (int s = 2; s < (1 << N); s++) + for (int i = 1; i < N; i++) + if (((1 << i) & s) && (s & 1)) + { + int ps = s ^ (1 << i); + for (int j = 0; j < N; j++) + if (((1 << j) & ps) && dp[ps][j] > -0.5) + { + double tmp = len[j][i] / pow(0.9, ones[ps] - 1.0); + tmp = max(tmp, dp[ps][j]); + if (dp[s][i] < -0.5 || dp[s][i] > tmp) + { + dp[s][i] = tmp; + } + } + } + double ans = 1e10; + for (int i = 0; i < N; i++) + { + if (dp[(1 << N) - 1][i] > -0.5) + { + ans = min(ans, dp[(1 << N) - 1][i]); + } + } + printf("%.6f\n", ans); +} + +int main() +{ + for (int i = 1; i < (1 << 15); i++) + { + ones[i] = ones[i >> 1] + (i & 1); + } + int T; + cin >> T; + while (T--) + { + cin >> N; + for (int i = 0; i < N; i++) + { + cin >> typ[i] >> A[i] >> B[i] >> R[i]; + for (int j = 0; j < i; j++) + { + DIST(i, j); + } + } + solve(); + } + return 0; +} diff --git a/1192.cpp b/1192.cpp new file mode 100644 index 0000000..312c7f8 --- /dev/null +++ b/1192.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +char *read_name() +{ + static char buf[65536]; + return fgets(buf, 65536, stdin); +} +char sa[65536], sb[65536]; +int edit_dist(const char *a, int la, const char *b, int lb) +{ + assert(la < 1024 && lb < 1024); + static int dp[1024][1024] = {}; + for (int i = 0; i <= la; i++) + { + for (int j = 0; j <= lb; j++) + dp[i][j] = INT_MIN; + } + dp[0][0] = 0; + for (int i = 0; i < la; i++) + { + for (int j = 0; j < lb; j++) + { + if (dp[i][j] == INT_MIN) + continue; + if (a[i] == b[j]) + dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 5); + dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - 7); + dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] - 7); + dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] - 4); + } + } + return dp[la][lb]; +} + +int main() +{ + read_name(); + fgets(sa, 65536, stdin); + read_name(); + + int la = strlen(sa) - 1, lb; + sa[la] = '\0'; + + vector ret; + int mxSim = INT_MIN; + while (read_name()) + { + fgets(sb, 65536, stdin); + lb = strlen(sb) - 1; + sb[lb] = '\0'; + int t = edit_dist(sa, la, sb, lb); + if (t > mxSim) + mxSim = t, ret.clear(); + if (t == mxSim) + ret.push_back(sb); + read_name(); + } + + printf("The query sequence is:\n"); + printf("%s\n\n", sa); + printf("The most similar sequences are:\n"); + + for (int i = 0; i < ret.size(); i++) + { + printf("\n%s\n", ret[i].c_str()); + printf("The similarity score is: %d\n", mxSim); + } + return 0; +} diff --git a/11920.cpp b/11920.cpp new file mode 100644 index 0000000..40f69a1 --- /dev/null +++ b/11920.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +char s[1005], tmp[1005]; +int ans; + +void go(); + +int main() +{ + int T, t, i, j; + scanf(" %d", &T); + gets(s); + for (t = 0; t < T; t++) + { + gets(s); + for (i = 0; s[i]; i++) + { + tmp[i] = s[i]; + } + ans = 99999; + if (tmp[0] == '?') + { + tmp[0] = '1'; + go(); + for (i = 0; s[i]; i++) + { + tmp[i] = s[i]; + } + tmp[0] = '0'; + go(); + } + else + { + go(); + } + printf("Case %d: %d\n", t + 1, ans); + } + return 0; +} + +void go() +{ + int i, j, len1, len2; + for (i = 1; s[i]; i++) + { + if (tmp[i] == '?') + { + if (tmp[i + 1]) + { + if (tmp[i - 1] == '1') + { + if (tmp[i + 1] == '1') + { + tmp[i] = '0'; + } + else if (tmp[i + 1] == '0') + { + len1 = len2 = 0; + char ch; + for (j = i - 1, ch = tmp[j]; j >= 0; j--) + if (ch == tmp[j]) + { + len1++; + } + else + { + break; + } + for (j = i + 1, ch = tmp[j]; s[j]; j++) + if (ch == tmp[j]) + { + len2++; + } + else + { + break; + } + if (len1 <= len2) + { + tmp[i] = tmp[i - 1]; + } + else + { + tmp[i] = tmp[i + 1]; + } + } + else//=='?' + { + if (tmp[i - 1] == '0') + { + tmp[i] = '1'; + } + else + { + tmp[i] = '0'; + } + } + } + else + { + if (tmp[i + 1] == '0') + { + tmp[i] = '1'; + } + else if (tmp[i + 1] == '1') + { + len1 = len2 = 0; + char ch; + for (j = i - 1, ch = tmp[j]; j >= 0; j--) + if (ch == tmp[j]) + { + len1++; + } + else + { + break; + } + for (j = i + 1, ch = tmp[j]; s[j]; j++) + if (ch == tmp[j]) + { + len2++; + } + else + { + break; + } + if (len1 <= len2) + { + tmp[i] = tmp[i - 1]; + } + else + { + tmp[i] = tmp[i + 1]; + } + } + else//=='?' + { + if (tmp[i - 1] == '0') + { + tmp[i] = '1'; + } + else + { + tmp[i] = '0'; + } + } + } + } + else + { + if (tmp[i - 1] == '0') + { + tmp[i] = '1'; + } + else + { + tmp[i] = '0'; + } + } + } + } + char ch; + for (i = len1 = 0, len2 = 0, ch = tmp[0]; s[i]; i++) + { + if (tmp[i] == ch) + { + len1++; + } + else + { + len2 = len1 > len2 ? len1 : len2; + ch = tmp[i]; + len1 = 1; + } + } + len2 = len1 > len2 ? len1 : len2; + if (len2 < ans) + { + ans = len2; + } +} diff --git a/11921.cpp b/11921.cpp new file mode 100644 index 0000000..954a060 --- /dev/null +++ b/11921.cpp @@ -0,0 +1,592 @@ +#include + +using namespace std; + +#define eps 1.0e-8 +#define sgn(f) ((f) < (-eps) ? -1 : ((f) > eps)) +#define clc(f) sgn(f) == 0 ? ((f) = 0.0) : (f) + +struct Edge +{ + int to, nxt; + double w; + Edge(int _t = -1, double _w = -1, int _n = -1) + { + to = _t; + w = _w; + nxt = _n; + } +} e[2555555]; + +bool isinq[23333]; +double d[23333]; +int n, N, cnt_edge, head[23333]; +queue q; + +void add_edge(int u, int v, double w) +{ + e[cnt_edge] = Edge(v, w, head[u]); + head[u] = cnt_edge++; + e[cnt_edge] = Edge(u, w, head[v]); + head[v] = cnt_edge++; +} + +void spfa() +{ + while (!q.empty()) + { + q.pop(); + } + for (int i = 0; i < N; ++i) + { + isinq[i] = false; + d[i] = -1; + } + d[0] = 0; + q.push(0); + isinq[0] = true; + while (!q.empty()) + { + int u = q.front(); + q.pop(); + isinq[u] = false; + for (int p = head[u]; p != -1; p = e[p].nxt) + { + int v = e[p].to; + double w = e[p].w; + if (sgn(d[v] + 1) == 0 || sgn(d[v] - d[u] - w) > 0) + { + d[v] = d[u] + w; + if (!isinq[v]) + { + isinq[v] = true; + q.push(v); + } + } + } + } +} + +struct Point +{ + double x, y; + Point(double _x = 0, double _y = 0) : x(_x), y(_y){}; + Point operator+(Point a) + { + return Point(x + a.x, y + a.y); + } + Point operator-(Point a) + { + return Point(x - a.x, y - a.y); + } + Point operator*(double p) + { + return Point(x * p, y * p); + } + Point operator/(double p) + { + return Point(x / p, y / p); + } + bool operator==(Point a) + { + return sgn(x - a.x) == 0 && sgn(y - a.y) == 0; + } + bool operator<(const Point &a) const + { + if (sgn(x - a.x) != 0) + { + return sgn(x - a.x) < 0; + } + else + { + return sgn(y - a.y) < 0; + } + } +} robot, tgt, p[23333]; + +map mp; + +struct Circle +{ + double r; + Point c; + Point pt_get(double a) + { + return Point(c.x + cos(a) * r, c.y + sin(a) * r); + } + int chk_pos(Point a) + { + double d = (a.x - c.x) * (a.x - c.x) + (a.y - c.y) * (a.y - c.y), sr = r * r; + if (sgn(d - sr) > 0) + { + return 1; + } + else if (sgn(d - sr) == 0) + { + return 0; + } + else + { + return -1; + } + } +} rock[55]; + +typedef Point Vector; + +double dmult(Vector a, Vector b) +{ + return a.x * b.x + a.y * b.y; +} +double xmult(Vector a, Vector b) +{ + return a.x * b.y - a.y * b.x; +} +double xmult(Point a, Point b, Point o) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +Vector xmult(Vector a) +{ + return Vector(a.y, -a.x); +} +double dis(Vector a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} +double dis(Point a, Point b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} +double angle(Vector v) +{ + return atan2(v.y, v.x); +} +double intersection_angle(Vector a, Vector b) +{ + return acos(dmult(a, b) / dis(a) / dis(b)); +} +double DistanceToLine(Point p, Point a, Point b) +{ + return fabs(xmult(b - a, p - a)) / dis(b - a); +} +bool CheckPoint(Point a) +{ + int cnt = 0; + for (int i = 0; i < n; ++i) + { + int f = rock[i].chk_pos(a); + if (f == -1) + { + return false; + } + if (f == 0) + { + cnt++; + } + } + return cnt <= 1; +} +void pt_add(Point a) +{ + if (!CheckPoint(a)) + { + return; + } + if (mp.find(a) != mp.end()) + { + return; + } + mp[a] = N; + p[N++] = a; +} +int pt_find(Point a) +{ + if (mp.find(a) != mp.end()) + { + return mp[a]; + } + else + { + return -1; + } +} +int tan_get(Point p, Circle c, Point *pt) +{ + Vector u = p - c.c; + double d = dmult(u, u), rr = c.r * c.r; + int flag = sgn(d - rr); + if (flag < 0) + { + return 0; + } + else if (flag == 0) + { + pt[0] = p; + return 1; + } + else + { + Vector v = xmult(u); + pt[0] = (c.c * d + u * rr - v * c.r * sqrt(d - rr)) / d; + pt[1] = (c.c * d + u * rr + v * c.r * sqrt(d - rr)) / d; + return 2; + } +} +Point GetLineProjection(Point p, Point a, Point b) +{ + if (sgn(xmult(p - a, p - b)) == 0) + { + return p; + } + return a + (b - a) * (dmult(b - a, p - a) / dmult(b - a, b - a)); +} +int line_Circle_intersect(Point a, Point b, Circle c, Point *pt) +{ + double d = DistanceToLine(c.c, a, b); + if (sgn(d - c.r) > 0) + { + return 0; + } + Point p = GetLineProjection(c.c, a, b); + Vector v = (b - a) / dis(b - a); + double l = sqrt(c.r * c.r - d * d); + pt[0] = p - v * l; + pt[1] = p + v * l; + if (sgn(d - c.r) == 0) + { + return 1; + } + else + { + return 2; + } +} +bool chk(Point a, Point b) +{ + Point pt[15]; + Vector v = b - a; + int cnt, ct = 0; + double dv = dmult(v, v), t[5]; + for (int i = 0; i < n; ++i) + { + cnt = line_Circle_intersect(a, b, rock[i], pt); + if (cnt == 0) + { + continue; + } + else if (cnt == 1) + { + t[0] = dmult(pt[0] - a, v) / dv; + clc(t[0]); + if (sgn(t[0]) > 0 && sgn(t[0] - 1) < 0) + { + return false; + } + } + else + { + t[0] = dmult(pt[0] - a, v) / dv; + t[1] = dmult(pt[1] - a, v) / dv; + clc(t[0]); + clc(t[1]); + if (sgn(t[0] - t[1]) > 0) + { + swap(t[0], t[1]); + } + if (sgn(t[1]) <= 0 || sgn(t[0] - 1) >= 0) + { + continue; + } + t[0] = max(t[0], 0.0); + t[1] = min(t[1], 1.0); + if (sgn(t[0] - t[1]) < 0) + { + return false; + } + } + } + return true; +} +double arc_len(Circle c, Point a, Point b) +{ + if (a == b) + { + return 2 * M_PI * c.r; + } + Vector va = a - c.c, vb = b - c.c; + double alpha = intersection_angle(va, vb); + if (sgn(xmult(va, vb)) < 0) + { + alpha = 2 * M_PI - alpha; + } + return alpha * c.r; +} +bool cmp(const Point &a, const Point &b) +{ + if (sgn(a.y) >= 0) + { + if (sgn(b.y) < 0) + { + return true; + } + else if (sgn(xmult(a, b)) != 0) + { + return sgn(xmult(a, b)) > 0; + } + else + { + return sgn(a.x) > 0; + } + } + else + { + if (sgn(b.y) >= 0) + { + return false; + } + else + { + return sgn(xmult(a, b)) > 0; + } + } +} +int chk_dir(Vector v, Vector u) +{ + if (sgn(xmult(u, v)) == 0) + { + if (sgn(dmult(u, v)) >= 0) + { + return 1; + } + else + { + return -1; + } + } + else + { + return 0; + } +} +bool chkin(Vector v, Vector L, Vector R) +{ + if (chk_dir(L, R) == 1 || chk_dir(v, L) == 1 || chk_dir(v, R) == 1) + { + return false; + } + if (sgn(xmult(L, R)) >= 0) + { + return sgn(xmult(L, v)) > 0 && sgn(xmult(v, R)) > 0; + } + else + { + return sgn(xmult(v, L)) <= 0 || sgn(xmult(R, v)) <= 0; + } +} +int tan_get(Circle A, Circle B, Point *a, Point *b) +{ + int cnt = 0; + if (sgn(A.r - B.r) < 0) + { + swap(a, b); + swap(A, B); + } + double d2 = dmult(A.c - B.c, A.c - B.c), rdiff = A.r - B.r, rsum = A.r + B.r; + if (sgn(d2 - rdiff * rdiff) < 0) + { + return 0; + } + double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x); + if (sgn(d2) == 0 && sgn(A.r - B.r) == 0) + { + return -1; + } + if (sgn(d2 - rdiff * rdiff) == 0) + { + a[cnt] = A.pt_get(base); + b[cnt] = B.pt_get(base); + cnt++; + return 1; + } + double ang = acos((A.r - B.r) / sqrt(d2)); + a[cnt] = A.pt_get(base + ang); + b[cnt] = B.pt_get(base + ang); + cnt++; + a[cnt] = A.pt_get(base - ang); + b[cnt] = B.pt_get(base - ang); + cnt++; + if (sgn(d2 - rsum * rsum) == 0) + { + a[cnt] = A.pt_get(base); + b[cnt] = B.pt_get(M_PI + base); + cnt++; + } + else if (sgn(d2 - rsum * rsum) > 0) + { + double ang = acos((A.r + B.r) / sqrt(d2)); + a[cnt] = A.pt_get(base + ang); + b[cnt] = B.pt_get(M_PI + base + ang); + cnt++; + a[cnt] = A.pt_get(base - ang); + b[cnt] = B.pt_get(M_PI + base - ang); + cnt++; + } + return cnt; +} +void init() +{ + N = 0; + mp.clear(); + cnt_edge = 0; + memset(head, -1, sizeof(head)); + pt_add(robot); + pt_add(tgt); + int cnt; + Point pt[10], ptt[10]; + for (int i = 0; i < n; ++i) + { + cnt = tan_get(robot, rock[i], pt); + for (int j = 0; j < cnt; ++j) + { + pt_add(pt[j]); + } + cnt = tan_get(tgt, rock[i], pt); + for (int j = 0; j < cnt; ++j) + { + pt_add(pt[j]); + } + for (int j = i + 1; j < n; ++j) + { + cnt = tan_get(rock[i], rock[j], pt, ptt); + for (int k = 0; k < cnt; ++k) + { + pt_add(pt[k]); + pt_add(ptt[k]); + } + } + } +} +void graph_build() +{ + int cnt, u, v; + Point tp[10]; + for (int i = 0; i < n; ++i) + { + cnt = tan_get(robot, rock[i], tp); + for (int j = 0; j < cnt; ++j) + { + u = pt_find(tp[j]); + if (u == -1) + { + continue; + } + if (chk(robot, tp[j])) + { + add_edge(0, u, dis(robot, tp[j])); + } + } + cnt = tan_get(tgt, rock[i], tp); + for (int j = 0; j < cnt; ++j) + { + u = pt_find(tp[j]); + if (u == -1) + { + continue; + } + if (chk(tgt, tp[j])) + { + add_edge(1, u, dis(tgt, tp[j])); + } + } + for (int j = i + 1; j < n; ++j) + { + Point tpp[10]; + cnt = tan_get(rock[i], rock[j], tp, tpp); + for (int k = 0; k < cnt; ++k) + { + u = pt_find(tp[k]); + v = pt_find(tpp[k]); + if (u == -1 || v == -1) + { + continue; + } + if (chk(tp[k], tpp[k])) + { + add_edge(u, v, dis(tp[k], tpp[k])); + } + } + } + } + Point pt[23333]; + int m; + for (int i = 0; i < n; ++i) + { + m = 0; + for (int j = 0; j < N; ++j) + { + if (rock[i].chk_pos(p[j]) == 0) + { + pt[m++] = p[j]; + } + } + for (int j = 0; j < m; ++j) + { + pt[j] = pt[j] - rock[i].c; + } + sort(pt, pt + m, cmp); + pt[m] = pt[0]; + for (int j = 0; j < m; ++j) + { + bool ok = true; + for (int k = 0; k < n; ++k) + { + if (k == i) + { + continue; + } + double d = dis(rock[i].c, rock[k].c), sr = rock[i].r + rock[k].r; + if (sgn(d - sr) == 0) + { + if (chkin(rock[k].c - rock[i].c, pt[j], pt[j + 1])) + { + ok = false; + break; + } + } + } + if (ok) + { + int u = pt_find(rock[i].c + pt[j]), v = pt_find(rock[i].c + pt[j + 1]); + double w = arc_len(rock[i], pt[j] + rock[i].c, pt[j + 1] + rock[i].c); + add_edge(u, v, w); + } + } + } +} +int main() +{ + int T, cas = 1; + scanf("%d", &T); + while (T--) + { + scanf("%lf%lf%lf%lf", &robot.x, &robot.y, &tgt.x, &tgt.y); + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + scanf("%lf%lf%lf", &rock[i].c.x, &rock[i].c.y, &rock[i].r); + } + if (chk(robot, tgt)) + { + printf("Case %d: %.8f\n", cas++, dis(robot, tgt)); + } + else + { + init(); + graph_build(); + spfa(); + printf("Case %d: %.8f\n", cas++, d[1]); + } + } + return 0; +} diff --git a/11922.cpp b/11922.cpp new file mode 100644 index 0000000..bb35518 --- /dev/null +++ b/11922.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +typedef long long ll; +typedef unsigned int uint; +typedef unsigned long long ull; +static const double EPS = 1e-9; +static const double PI = acos(-1.0); + +#define REP(i, n) for (int i = 0; i < (int)(n); i++) +#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) +#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) +#define FORIT(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) +#define MEMSET(v, h) memset((v), h, sizeof(v)) + +typedef int Node; +struct RBST +{ + Node v; + RBST *l; + RBST *r; + int size; + RBST() : l(NULL), r(NULL), size(0) + { + ; + } + RBST(Node v) : v(v), l(NULL), r(NULL), size(1) + { + ; + } + RBST *Update() + { + size = 1 + (l ? l->size : 0) + (r ? r->size : 0); + return this; + } +}; +RBST _pool[1000000]; +RBST *pool = NULL; + +int Count(RBST *t) +{ + return t ? t->size : 0; +} + +RBST *Merge(RBST *l, RBST *r) +{ + if (!l) + { + return r; + } + if (!r) + { + return l; + } + if (rand() % (Count(l) + Count(r)) < Count(l)) + { + l->r = Merge(l->r, r); + return l->Update(); + } + else + { + r->l = Merge(l, r->l); + return r->Update(); + } + assert(false); + return NULL; +} + +// [0, k), [k, n) +pair Split(RBST *t, int k) +{ + if (t == NULL) + { + return pair(NULL, NULL); + } + if (k <= Count(t->l)) + { + pair s = Split(t->l, k); + t->l = s.second; + return make_pair(s.first, t->Update()); + } + else + { + pair s = Split(t->r, k - Count(t->l) - 1); + t->r = s.first; + return make_pair(t->Update(), s.second); + } +} + +RBST *Insert(RBST *t, int k, Node v) +{ + pair s = Split(t, k); + RBST *val = pool++; + *val = RBST(v); + return Merge(s.first, Merge(val, s.second)); +} + +RBST *Erase(RBST *t, int k) +{ + pair s1 = Split(t, k); + pair s2 = Split(s1.first, k - 1); + return Merge(s2.first, s1.second); +} + +RBST *At(RBST *t, int index) +{ + assert(t != NULL); + if (index < Count(t->l)) + { + return At(t->l, index); + } + else + { + index -= Count(t->l); + } + if (index == 0) + { + return t; + } + index--; + return At(t->r, index); +} + +int n, m; +int main() +{ + while (scanf("%d %d", &n, &m) > 0) + { + RBST *tree = NULL; + RBST *rev = NULL; + pool = _pool; + REP(i, n) + { + tree = Insert(tree, i, i + 1); + rev = Insert(rev, i, n - i); + } + REP(i, m) + { + int a, b; + scanf("%d %d", &a, &b); + a--; + pair temp; + temp = Split(tree, b); + RBST *right = temp.second; + temp = Split(temp.first, a); + RBST *mid = temp.second; + RBST *left = temp.first; + temp = Split(rev, n - a); + RBST *rright = temp.second; + temp = Split(temp.first, n - b); + RBST *rmid = temp.second; + RBST *rleft = temp.first; + tree = Merge(left, Merge(right, rmid)); + rev = Merge(mid, Merge(rleft, rright)); + } + REP(i, n) + { + printf("%d\n", At(tree, i)->v); + } + } +} diff --git a/11923.cpp b/11923.cpp new file mode 100644 index 0000000..1b7e1c8 --- /dev/null +++ b/11923.cpp @@ -0,0 +1,254 @@ +#include + +using namespace std; + +#define MAXN 4 +#define MAXM 60 + +int mp[MAXN][MAXN]; +int temp[MAXN][MAXN]; +char str[MAXM]; + +int check(int ii, int jj, int flag) +{ + memcpy(temp, mp, sizeof(temp)); + temp[ii][jj] = flag; + int res = 0; + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + { + if (j + 2 < 4) + { + bool good = true; + if (i != ii) + { + good = false; + } + for (int k = 0; k < 3; ++k) + if (temp[i][j + k] != flag) + { + good = false; + } + if (jj < j || jj > j + 2) + { + good = false; + } + if (good) + { + res += 3; + } + } + if (i + 2 < 4) + { + bool good = true; + if (j != jj) + { + good = false; + } + for (int k = 0; k < 3; ++k) + if (temp[i + k][j] != flag) + { + good = false; + } + if (ii < i || ii > i + 2) + { + good = false; + } + if (good) + { + res += 3; + } + } + if (i + 2 < 4 && j + 2 < 4) + { + bool good = true; + bool got = false; + for (int k = 0; k < 3; ++k) + { + if (temp[i + k][j + k] != flag) + { + good = false; + } + if (i + k == ii && j + k == jj) + { + got = true; + } + } + if (good && got) + { + res += 3; + } + } + if (i + 2 < 4 && j - 2 >= 0) + { + bool good = true; + bool got = false; + for (int k = 0; k < 3; ++k) + { + if (temp[i + k][j - k] != flag) + { + good = false; + } + if (i + k == ii && j - k == jj) + { + got = true; + } + } + if (good && got) + { + res += 3; + } + } + if (j + 3 < 4) + { + bool good = true; + if (i != ii) + { + good = false; + } + for (int k = 0; k < 4; ++k) + if (temp[i][j + k] != flag) + { + good = false; + } + if (jj < j || jj > j + 3) + { + good = false; + } + if (good) + { + res += 4; + } + } + if (i + 3 < 4) + { + bool good = true; + if (j != jj) + { + good = false; + } + for (int k = 0; k < 4; ++k) + if (temp[i + k][j] != flag) + { + good = false; + } + if (ii < i || ii > i + 3) + { + good = false; + } + if (good) + { + res += 4; + } + } + if (i + 3 < 4 && j + 3 < 4) + { + bool good = true; + bool got = false; + for (int k = 0; k < 4; ++k) + { + if (temp[i + k][j + k] != flag) + { + good = false; + } + if (i + k == ii && j + k == jj) + { + got = true; + } + } + if (good && got) + { + res += 4; + } + } + if (i + 3 < 4 && j - 3 >= 0) + { + bool good = true; + bool got = false; + for (int k = 0; k < 4; ++k) + { + if (temp[i + k][j - k] != flag) + { + good = false; + } + if (i + k == ii && j - k == jj) + { + got = true; + } + } + if (good && got) + { + res += 4; + } + } + } + return res; +} + +int main() +{ + int cases; + scanf("%d", &cases); + for (int caseT = 1; caseT <= cases; ++caseT) + { + memset(mp, 0, sizeof(mp)); + scanf("%s", &str); + int len = strlen(str), res1 = 0, res2 = 0; + for (int times = 0; times < len; ++times) + { + if (str[times] == '0') + { + continue; + } + int flag = times % 2 == 0 ? 1 : 2; + bool rem = false; + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + if (mp[i][j] == 0) + { + rem = true; + } + int maxi = -1, x, y; + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + { + if (rem && mp[i][j] == 0) + { + int t = check(i, j, flag); + if (t > maxi) + { + maxi = t, x = i, y = j; + } + } + else if (!rem && mp[i][j] != flag) + { + int t = check(i, j, flag); + if (t > maxi) + { + maxi = t, x = i, y = j; + } + } + } + int add; + if (maxi == -1) + { + add = 50; + } + else + { + add = maxi; + mp[x][y] = flag; + } + if (times % 2 == 0) + { + res1 += add; + } + else + { + res2 += add; + } + } + printf("Case %d: %d %d\n", caseT, res1, res2); + } + return 0; +} diff --git a/11925.cpp b/11925.cpp new file mode 100644 index 0000000..24669b4 --- /dev/null +++ b/11925.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int target[1024], init[1024]; +int n, head; +void oper1() +{ + if (head == n - 1) + swap(init[head], init[0]); + else + swap(init[head], init[head + 1]); + putchar('1'); +} +void oper2() +{ + putchar('2'); + head++; + if (head == n) + head = 0; + init[n] = init[head]; +} +int main() +{ + int i, j, k; + int first = 0; + while (scanf("%d", &n) == 1 && n) + { + first = 1; + for (i = 0; i < n; i++) + scanf("%d", &target[i]); + for (i = 0; i < n; i++) + init[i] = i + 1; + head = 0; + for (i = 1; i < n; i++) + { + while (init[head] != target[i]) + { + oper2(); + } + while (init[head + 1] != target[i - 1]) + { + oper1(); + oper2(); + } + oper1(); + } + oper2(); + while (init[head] != target[0]) + oper2(); + printf("\n"); + } + return 0; +} diff --git a/11926.cpp b/11926.cpp new file mode 100644 index 0000000..7b1c47a --- /dev/null +++ b/11926.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +const int MAX_N = 100, MAX_L = 1000000; + +static int N, M, single_from[MAX_N], single_to[MAX_N]; +static int interval_from[MAX_N], interval_to[MAX_N], interval_term[MAX_N]; +static bool is_active[MAX_L]; + +bool init() +{ + scanf("%d%d", &N, &M); + for (int i = 0; i < N; i++) + { + scanf("%d%d", single_from + i, single_to + i); + } + for (int i = 0; i < M; i++) + { + scanf("%d%d%d", interval_from + i, interval_to + i, interval_term + i); + } + return N > 0 || M > 0; +} + +bool solve() +{ + memset(is_active, false, sizeof(is_active)); + for (int i = 0; i < N; i++) + { + for (int t = single_from[i]; t < single_to[i]; t++) + { + if (is_active[t]) + { + return false; + } + else + { + is_active[t] = true; + } + } + } + for (int i = 0; i < M; i++) + { + for (int k = 0; interval_from[i] + interval_term[i] * k < MAX_L; k++) + { + for (int t = interval_from[i] + interval_term[i] * k, ed = interval_to[i] + interval_term[i] * k; t < MAX_L && t < ed; t++) + { + if (is_active[t]) + { + return false; + } + else + { + is_active[t] = true; + } + } + } + } + return true; +} + +int main() +{ + while (init()) + puts("NO CONFLICT" + (solve() ? 0 : 3)); + return 0; +} diff --git a/11927.cpp b/11927.cpp new file mode 100644 index 0000000..944dbe4 --- /dev/null +++ b/11927.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +vector mp[1111], vis[1111]; +int n, m, c[1111], Q[1111111], sg[1111], used[1111]; + +void getsg() +{ + int i, j, k; + int st = 0, ed = 0; + for (i = 0; i < n; i++) + if (c[i] == 0) + Q[ed++] = i, sg[i] = 0; + while (st < ed) + { + k = Q[st++]; + for (i = 0; i < n; i++) + used[i] = 0; + for (i = 0; i < vis[k].size(); i++) + used[vis[k][i]] = 1; + for (i = 0; i < n; i++) + if (!used[i]) + { + sg[k] = i; + break; + } + for (i = 0; i < mp[k].size(); i++) + { + int tm = mp[k][i]; + c[tm]--; + vis[tm].push_back(sg[k]); + if (c[tm] == 0) + Q[ed++] = tm; + } + } +} +int main() +{ + int i, j, k, x, y; + while (scanf("%d%d", &n, &m) != -1 && m + n) + { + for (i = 0; i < n; i++) + mp[i].clear(), vis[i].clear(); + while (m--) + { + scanf("%d%d", &x, &y); + c[x]++; + mp[y].push_back(x); + } + getsg(); + int ans = 0; + for (i = 0; i < n; i++) + { + scanf("%d", &x); + if (x & 1) + ans ^= sg[i]; + } + if (ans) + puts("First"); + else + puts("Second"); + } + return 0; +} diff --git a/1193.cpp b/1193.cpp new file mode 100644 index 0000000..41f6c4f --- /dev/null +++ b/1193.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define forn(i, n) for (int i = 0; i < (int)(n); i++) +#define forsn(i, s, n) for (int i = (int)(s); i < (int)(n); i++) +#define forall(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) +#define dforn(i, n) for (int i = ((int)(n)-1); i >= 0; i--) +#define dforsn(i, s, n) for (int i = ((int)(n)-1); i >= (int)(n); i--) +#define esta(i, c) ((c).find(i) != (c).end()) +#define estal(i, c) (find((c).begin(), (c).end(), (i)) != (c).end()) +#define all(c) (c).begin(), (c).end() + +#define dbg(x) cerr << #x << " = " << x << endl; +#define raya cerr << string(80, '=') << endl; + +typedef long long tint; +typedef pair pii; +typedef vector vi; +typedef vector> vii; + +const int MAXN = 10000; +const double eps = 1e-10; +struct Interval +{ + double st, en; + Interval() {} + Interval(double s, double e) + { + st = s, en = e; + } + bool operator<(const Interval &i) const + { + return (i.en == en) ? (st < i.st) : (en < i.en); + } +}; +long double x[MAXN], y[MAXN]; +Interval inter[MAXN]; + +int main() +{ + int n, d, testcase = 0; + while (scanf("%d%d", &n, &d) == 2 && !(n == 0 && d == 0)) + { + forn(i, n) scanf("%Lf %Lf", &x[i], &y[i]); + int count = 0, ok = true; + forn(i, n) + { + if (d < y[i]) + { + ok = false; + break; + } + else + { + long double sqrtd = sqrt((1.0 * d) * d - y[i] * y[i]); + inter[i] = Interval(x[i] - sqrtd, x[i] + sqrtd); + } + } + if (!ok) + { + printf("Case %d: %d\n", ++testcase, -1); + continue; + } + sort(inter, inter + n); + for (int i = 0; i < n;) + { + int j; + for (j = 0; j < n; j++) + { + if (inter[j].st - eps > inter[i].en) + break; + } + i = j; + count++; + } + printf("Case %d: %d\n", ++testcase, count); + } + return 0; +} diff --git a/11930.cpp b/11930.cpp new file mode 100644 index 0000000..10adff1 --- /dev/null +++ b/11930.cpp @@ -0,0 +1,197 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define DIAG(i, x) ((i << 2) + (x << 1)) + +typedef long long LL; + +const int NN = 2000; + +// geom +// ret 1 if a > b, 0 if a == b and -1 if a < b +template +int Cmp(_T a, _T b) +{ + return (a > b) - (a < b); +} + +template +struct Point +{ + _T x, y; + Point(_T _x = 0, _T _y = 0) : x(_x), y(_y) {} + Point operator+(const Point &p) const + { + return Point(x + p.x, y + p.y); + } + Point operator-(const Point &p) const + { + return Point(x - p.x, y - p.y); + } + _T Dot(const Point &p) const + { + return x * p.x + y * p.y; + } + _T Cross(const Point &p) const + { + return x * p.y - y * p.x; + } + bool operator==(const Point &p) const + { + return !Cmp(x, p.x) && !Cmp(y, p.y); + } + bool operator!=(const Point &p) const + { + return !(*this == p); + } + bool operator<(const Point &p) const + { + int c = Cmp(x, p.x); + return c < 0 || (!c && Cmp(y, p.y) < 0); + } +}; + +template +bool InInterval(Point<_T> &p, Point<_T> &b, Point<_T> &e) +{ + if (p.x < min(b.x, e.x) || p.x > max(b.x, e.x)) + { + return false; + } + if (p.y < min(b.y, e.y) || p.y > max(b.y, e.y)) + { + return false; + } + return true; +} + +template +bool SegmentIntersect(Point<_T> &p1, Point<_T> &p2, Point<_T> &p3, Point<_T> &p4) +{ + Point<_T> _u = p2 - p1, _v = p4 - p3; + int d1 = Cmp(_u.Cross(p3 - p1), _T(0)), d2 = Cmp(_u.Cross(p4 - p1), _T(0)); + int d3 = Cmp(_v.Cross(p1 - p3), _T(0)), d4 = Cmp(_v.Cross(p2 - p3), _T(0)); + if (d1 * d2 < 0 && d3 * d4 < 0) + { + return true; + } + if (d1 == 0 && InInterval(p3, p1, p2)) + { + return true; + } + if (d2 == 0 && InInterval(p4, p1, p2)) + { + return true; + } + if (d3 == 0 && InInterval(p1, p3, p4)) + { + return true; + } + if (d4 == 0 && InInterval(p2, p3, p4)) + { + return true; + } + return false; +} + +template +struct Rectangle +{ + Point<_T> p[4]; + Rectangle() {} +}; + +// edge +int nxt[NN], ecnt; +struct edge +{ + int from, to, nxt; +} e[NN * NN]; +void insert_edge(int from, int to) +{ + e[ecnt].from = from; + e[ecnt].to = to; + e[ecnt].nxt = nxt[from]; + nxt[from] = ecnt++; +} + +// tarjan +int n, num[NN], low[NN], scc[NN], cnt, scccnt, s[NN], top; + +void dfs(int u) +{ + num[u] = low[u] = cnt++; + s[top++] = u; + for (int i = nxt[u]; i != -1; i = e[i].nxt) + { + int v = e[i].to; + if (num[v] == -1) + { + dfs(v); + low[u] = min(low[u], low[v]); + } + else if (scc[v] == -1) + { + low[u] = min(low[u], num[v]); + } + } + if (low[u] == num[u]) + { + for (int v = -1; v != u;) + { + scc[v = s[--top]] = scccnt; + } + scccnt++; + } +} + +void findscc() +{ + for (int i = 0; i < n; i++) + { + num[i] = scc[i] = -1; + } + scccnt = top = 0; + for (int i = 0; i < n; i++) + if (num[i] == -1) + { + cnt = 0, dfs(i); + } +} + +int main() +{ + Rectangle vr[1024]; + while (scanf("%d", &n) == 1 && n) + { + bool possible = true; + for (int i = 0; i < (n << 1); ++i) + { + nxt[i] = -1; + } + ecnt = 0; + FOR(i, n) + { + FOR(j, 4) + scanf("%lld %lld", &vr[i].p[j].x, &vr[i].p[j].y); + sort(vr[i].p, vr[i].p + 4); + FOR(j, i) + FOR(k, 2) FOR(l, 2) + if (SegmentIntersect(vr[i].p[k], vr[i].p[3 - k], vr[j].p[l], vr[j].p[3 - l])) + { + insert_edge(2 * i + k, 2 * j + !l); + insert_edge(2 * j + l, 2 * i + !k); + } + } + n <<= 1; + findscc(); + for (int i = 0; possible && i < n; i += 2) + { + possible &= scc[i] != scc[i + 1]; + } + puts(possible ? "YES" : "NO"); + } + return 0; +} diff --git a/11931.cpp b/11931.cpp new file mode 100644 index 0000000..ab91885 --- /dev/null +++ b/11931.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int sx, sy, ex, ey, bx, by, cx, cy; +int n, m; +char g[32][32]; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; +int dist[25][25][25][25]; +int bfs() +{ + memset(dist, 0, sizeof(dist)); + dist[sx][sy][bx][by] = 1; + queue X, Y, BX, BY; + int x, y, tx, ty, ttx, tty; + X.push(sx), Y.push(sy), BX.push(bx), BY.push(by); + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + bx = BX.front(), BX.pop(); + by = BY.front(), BY.pop(); + int step = dist[x][y][bx][by]; + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= n || ty >= m) + continue; + if (g[tx][ty] == 'd' && g[bx][by] == 'b') + return step; + if (g[tx][ty] == '#' || g[tx][ty] == 'd') + continue; + if (tx == bx && ty == by) + {// push it + ttx = tx + dx[i], tty = ty + dy[i]; + if (ttx < 0 || tty < 0 || ttx >= n || tty >= m) + continue; + if (g[ttx][tty] == '#' || g[ttx][tty] == 'd') + continue; + if (dist[tx][ty][ttx][tty] == 0) + { + dist[tx][ty][ttx][tty] = step + 1; + X.push(tx), Y.push(ty), BX.push(ttx), BY.push(tty); + } + } + else + { + if (dist[tx][ty][bx][by] == 0) + { + dist[tx][ty][bx][by] = step + 1; + X.push(tx), Y.push(ty), BX.push(bx), BY.push(by); + } + } + } + } + return -1; +} +int main() +{ + while (scanf("%d %d", &n, &m) == 2 && n) + { + for (int i = 0; i < n; i++) + scanf("%s", g[i]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] == '@') + sx = i, sy = j; + if (g[i][j] == 'd') + ex = i, ey = j; + if (g[i][j] == 'x') + bx = i, by = j; + if (g[i][j] == 'b') + cx = i, cy = j; + } + } + int ret = bfs(); + if (ret < 0) + puts("No Way!"); + else + printf("%d\n", ret); + } + return 0; +} +/* +3 3 + +d.b +.@. +x.# + +3 5 +...d. +.#x#. +...@b + +0 0 +*/ diff --git a/11932.cpp b/11932.cpp new file mode 100644 index 0000000..0cc832a --- /dev/null +++ b/11932.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define MIN_VALUE -1000000000 + +int n; +int posibles[1 << 16]; +int vecinos[16]; +int dp[1 << 16][2][2]; +int valores[16]; + +static int *dps(int mascara, bool quien) +{ + if (dp[mascara][quien ? 1 : 0][0] != MIN_VALUE) + { + return dp[mascara][quien ? 1 : 0]; + } + int posibleEsta = posibles[mascara]; + int *mejor = dp[mascara][quien ? 1 : 0]; + mejor[0] = MIN_VALUE; + mejor[1] = MIN_VALUE; + for (int i = 0; i < n; i++) + { + if (((posibleEsta >> i) & 1) == 1) + { + int *posible = dps(mascara | (1 << i), !quien); + if (quien) + { + if (posible[0] + valores[i] > mejor[0]) + { + mejor[0] = posible[0] + valores[i]; + mejor[1] = posible[1]; + } + } + else + { + if (posible[1] + valores[i] > mejor[1]) + { + mejor[0] = posible[0]; + mejor[1] = posible[1] + valores[i]; + } + } + } + } + return mejor; +} + +void imprimir(int *d) +{ + if (d[0] > d[1]) + { + printf("First player wins! %d to %d.\n", d[0], d[1]); + } + else if (d[0] < d[1]) + { + printf("Second player wins! %d to %d.\n", d[1], d[0]); + } + else + { + printf("Tie game! %d all.\n", d[0]); + } +} + +int main() +{ + while (true) + { + scanf("%d", &n); + if (n == 0) + { + return 0; + } + for (int i = 0; i < n; i++) + { + vecinos[i] = 0; + } + for (int i = 0; i < n; i++) + { + scanf("%d", &valores[i]); + } + int m; + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + int a; + scanf("%d", &a); + a--; + int b; + scanf("%d", &b); + b--; + vecinos[a] |= 1 << b; + vecinos[b] |= 1 << a; + } + posibles[0] = (1 << n) - 1; + dp[0][0][0] = MIN_VALUE; + dp[0][0][1] = MIN_VALUE; + dp[0][1][0] = MIN_VALUE; + dp[0][1][1] = MIN_VALUE; + for (int i = 1; i < 1 << n; i++) + { + dp[i][0][0] = MIN_VALUE; + dp[i][0][1] = MIN_VALUE; + dp[i][1][0] = MIN_VALUE; + dp[i][1][1] = MIN_VALUE; + posibles[i] = 0; + for (int j = 0; j < n; j++) + { + if (((i >> j) & 1) == 1) + { + posibles[i] |= ~i & vecinos[j]; + } + } + } + dp[(1 << n) - 1][0][0] = 0; + dp[(1 << n) - 1][0][1] = 0; + dp[(1 << n) - 1][1][0] = 0; + dp[(1 << n) - 1][1][1] = 0; + imprimir(dps(0, true)); + } +} diff --git a/11933.cpp b/11933.cpp index 35cdb10..c50cc0a 100644 --- a/11933.cpp +++ b/11933.cpp @@ -1,63 +1,35 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -int toBinary(vector Vec){ - int i, cnt = 0; - FOI(i, 0, Vec.size()-1) - cnt += (Vec[i] << i); - return cnt; -} +using namespace std; -int main(){ - while( true ){ - int N; - int i, j; - cin >> N; - if( N == 0 ) +int main() +{ + unsigned int n, a, b; + while (cin >> n) + { + if (n == 0) + { break; - vector bit, bin, A(32, 0), B(32, 0); - while( N > 0 ){ - int rem = N % 2; - bin.push_back(rem); - N /= 2; } - for (i = 0; i < bin.size(); i++) - if (bin[i] == 1) - bit.push_back(i); - for (i = 0; i < bit.size(); i ++){ - if( i % 2 == 0 ) - A[bit[i]] = 1; - else - B[bit[i]] = 1; + a = 0; + b = 0; + int counter = 0; + for (int i = 0; i < 31; i++) + { + if ((n & (1 << i)) != 0) + { + if (counter % 2 == 0) + { + a |= 1 << i; + } + else + { + b |= 1 << i; + } + counter++; + } } - cout << toBinary(A) << " " << toBinary(B) << endl; + cout << a << " " << b << endl; } return 0; } diff --git a/11934.cpp b/11934.cpp index 35e2694..3f201e9 100644 --- a/11934.cpp +++ b/11934.cpp @@ -1,59 +1,26 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; - -#define PI acos(-1.0) -#define EPS 1e-9 -#define INF 1<<30 +#include -#define FOI(I, A, B) for (I=A; I<=B; I++) -#define FOD(I, A, B) for (I=A; I>=B; I--) - -int64 calc(int64 a, int64 b, int64 c, int64 x){ - return a*x*x + b*x + c; -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - int64 a, b, c, d, L; - scanf("%lld%lld%lld%lld%lld", &a, &b, &c, &d, &L); - if( a==0 && b==0 && c==0 && d==0 && L==0 ) +int main() +{ + int a, b, c, d, L; + int result; + while (cin >> a >> b >> c >> d >> L) + { + if (!a && !b && !c && !d && !L) + { break; - int64 i, cnt = 0; - FOI(i, 0, L) - if( calc(a, b, c, i) % d == 0 ) - cnt++; - printf("%lld\n", cnt); + } + result = 0; + for (int i = 0; i <= L; i++) + { + if ((a * i * i + b * i + c) % d == 0) + { + result++; + } + } + cout << result << endl; } return 0; } diff --git a/11935.cpp b/11935.cpp new file mode 100644 index 0000000..0645830 --- /dev/null +++ b/11935.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +struct p +{ + p(int a, int b, double c) + { + d = a; + type = b; + v = c; + } + p() {} + int d; + int type; + double v; +} x[100]; +bool cmp(p a, p b) +{ + if (a.d == b.d) + { + return a.type < b.type; + } + return a.d < b.d; +} +string str; +int cnt; +bool judge(double mid) +{ + int leak = 0; + double now = mid, sp = 0; + int s = 0; + for (int i = 0; i < cnt; i++) + { + if (x[i].type == 1) + { + now -= ((x[i].d - s) * (sp + leak)); + s = x[i].d; + sp = x[i].v; + if (now < 0) + { + return false; + } + } + else if (x[i].type == 2) + { + now -= ((x[i].d - s) * (sp + leak)); + s = x[i].d; + leak++; + if (now < 0) + { + return false; + } + } + else if (x[i].type == 3) + { + now -= ((x[i].d - s) * (sp + leak)); + s = x[i].d; + if (now < 0) + { + return false; + } + now = mid; + } + else if (x[i].type == 4) + { + now -= ((x[i].d - s) * (sp + leak)); + s = x[i].d; + leak = 0; + if (now < 0) + { + return false; + } + } + else if (x[i].type == 5) + { + now -= ((x[i].d - s) * (sp + leak)); + s = x[i].d; + if (now < 0) + { + return false; + } + } + } + return true; +} +int main() +{ + int d; + double v; + while (cin >> d >> str >> str >> v) + { + if (v == 0) + { + break; + } + cnt = 0; + v /= 100.0; + x[cnt++] = p(d, 1, v); + while (cin >> d) + { + cin >> str; + int type; + if (str == "Fuel") + { + cin >> str >> v; + v = v / 100.0; + type = 1; + } + else if (str == "Leak") + { + type = 2; + } + else if (str == "Gas") + { + cin >> str; + type = 3; + } + else if (str == "Mechanic") + { + type = 4; + } + else + { + type = 5; + } + x[cnt++] = p(d, type, v); + if (type == 5) + { + break; + } + } + sort(x, x + cnt, cmp); + double l = 0, r = 10000000; + while (r - l >= 0.0001) + { + double mid = (l + r) * 0.5; + int tmp = judge(mid); + //cout << mid << ' ' << tmp << endl; + if (tmp == 1) + { + r = mid; + } + else + { + l = mid; + } + } + printf("%.3f\n", l); + } + return 0; +} diff --git a/11936.cpp b/11936.cpp index 738139e..ad3f5cf 100644 --- a/11936.cpp +++ b/11936.cpp @@ -1,44 +1,31 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - int T; - cin >> T; - while( T-- ){ - int A, B, C; - cin >> A >> B >> C; - if( A + B > C && B + C > A && A + C > B ) - cout << "OK\n"; +int main() +{ + int n; + int a, b, c; + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> a >> b >> c; + if (a > b) + { + swap(a, b); + } + if (b > c) + { + swap(b, c); + } + if (a + b <= c) + { + cout << "Wrong!!" << endl; + } else - cout << "Wrong!!\n"; + { + cout << "OK" << endl; + } } return 0; } - diff --git a/1194.cpp b/1194.cpp new file mode 100644 index 0000000..4d5dba6 --- /dev/null +++ b/1194.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +typedef long long ll; +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) +#define FILL(x, v) memset(x, v, sizeof(x)) +const int INF = (int)1E9; +#define MAXN 105 +#define MAXM 1005 +int N, M; +int pra[MAXN], prb[MAXN], dst[MAXN]; +int L, adj[MAXN]; +struct llist +{ + int id, next; + llist() {} + llist(int _id, int _next) + { + id = _id; + next = _next; + } +} lists[MAXM]; +void insertList(int &a, int b) +{ + lists[L] = llist(b, a); + a = L++; +} +bool bfs() +{ + queue que; + REP(i, 0, N) + { + if (pra[i] == -1) + { + dst[i] = 0; + que.push(i); + } + else + dst[i] = INF; + } + int maxdst = INF; + bool aug = 0; + while (!que.empty()) + { + int x = que.front(); + que.pop(); + if (dst[x] == maxdst) + continue; + int t = adj[x]; + while (t != -1) + { + int y = lists[t].id; + if (prb[y] == -1) + { + maxdst = dst[x]; + aug = 1; + } + else if (dst[prb[y]] == INF) + { + dst[prb[y]] = dst[x] + 1; + que.push(prb[y]); + } + t = lists[t].next; + } + } + return aug; +} +bool dfs(int x) +{ + int t = adj[x]; + while (t != -1) + { + int y = lists[t].id; + if (prb[y] == -1 || (dst[prb[y]] == dst[x] + 1 && dfs(prb[y]))) + { + pra[x] = y; + prb[y] = x; + return true; + } + t = lists[t].next; + } + dst[x] = INF; + return false; +} +int hk() +{ + REP(i, 0, N) + pra[i] = -1; + REP(i, 0, M) + prb[i] = -1; + int ans = 0; + while (bfs()) + { + REP(i, 0, N) + if (pra[i] == -1) if (dfs(i)) ans++; + } + return ans; +} +int main() +{ + while (1) + { + scanf("%d", &N); + if (!N) + break; + int K, t, x, y; + scanf("%d%d", &M, &K); + L = 0; + FILL(adj, -1); + REP(i, 0, K) + { + scanf("%d%d%d", &t, &x, &y); + if (x && y) + insertList(adj[x - 1], y - 1); + } + printf("%d\n", hk()); + } + return 0; +} diff --git a/11940.cpp b/11940.cpp new file mode 100644 index 0000000..aa8c784 --- /dev/null +++ b/11940.cpp @@ -0,0 +1,184 @@ +#include + +using namespace std; + +//---------------------------- +// LAM PHAN VIET +// UVA 11940 - Face the Maze +// Time limit: 1s +//---------------------------- + +#define maxS 100000 +#define maxN 1005 +#define maxM 100000 +int r[maxM], c[maxM], n, m; +int a[maxN][maxN]; +bool End, space; + +void GetInput() +{ + char s[maxS]; + int x, y, len; + scanf("%d", &n); + gets(s); + len = strlen(s); + int i = 0; + m = 0; + while (i < len) + { + while (i < len && s[i] != ' ') + i++; + while (i < len && s[i] == ' ') + i++; + if (i < len) + { + sscanf(s + i, "(%d,%d)", &c[m], &r[m]); + m++; + } + } + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + { + a[i][j] = a[j][i] = 0; + } + for (int i = 2; i < m; i++) + a[r[i]][c[i]] = 1; +} + +bool isOK(int u, int v) +{ + return (u >= 1 && u <= n && v >= 1 && v <= n); +} + +bool Next(int u, int v) +{ + if (u == r[1] && abs(v - c[1]) == 1) + return true; + if (v == c[1] && abs(u - r[1]) == 1) + return true; + return false; +} + +bool isCCalled(int u, int v) +{ + return (isOK(u, v) && a[u][v] == 0); +} + +void DFS(int u, int v) +{ + if (End) + return; + if (!isOK(u, v) || a[u][v] == 1) + return; + a[u][v] = 1; + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + + if (Next(u, v)) + { + End = true; + if (space) + printf(" "); + space = true; + printf("(%d,%d)", c[1], r[1]); + return; + } + + int k = 0; + if (!End && isCCalled(u + 1, v)) + { + DFS(u + 1, v); + k = 1; + } + + if (!End && k) + { + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + } + k = 0; + if (!End && isCCalled(u, v + 1)) + { + DFS(u, v + 1); + k = 1; + } + + if (!End && k) + { + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + } + k = 0; + if (!End && isCCalled(u, v - 1)) + { + DFS(u, v - 1); + k = 1; + } + + if (!End && k) + { + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + } + k = 0; + if (!End && isCCalled(u - 1, v)) + { + DFS(u - 1, v); + k = 1; + } + + if (End) + return; + if (u == r[0] && v == c[0]) + { + if (k) + { + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + } + End = true; + return; + } + if (k) + { + if (space) + printf(" "); + space = true; + printf("(%d,%d)", v, u); + } +} + +void Print() +{ + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + printf("%5d", a[i][j]); + printf("\n"); + } +} + +main() +{ + // freopen("940.inp", "r", stdin); freopen("940.out", "w", stdout); + int Case; + scanf("%d", &Case); + while (Case--) + { + GetInput(); + End = space = false; + // Print(); + DFS(r[0], c[0]); + printf("\n"); + } +} diff --git a/11941.cpp b/11941.cpp new file mode 100644 index 0000000..7d484ac --- /dev/null +++ b/11941.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +int a[1024][1024], mlen, ind; +char s[1024 * 1024]; + +void init() +{ + for (int i = 0; i < mlen; ++i) + for (int j = 0; j < mlen; ++j) + a[i][j] = 0; +} + +void mark(int x, int y, int xx, int yy) +{ + for (int j = y - 1; j < yy; ++j) + for (int i = x - 1; i < xx; ++i) + a[j][i] = 1; +} + +int block(int len, int x, int y) +{ + int flag = a[x][y]; + if (len == 1) + return flag; + for (int i = 0; i < len; ++i) + for (int j = 0; j < len; ++j) + if (flag != a[x + i][y + j]) + return -1; + return flag; +} + +void div(int len, int x, int y) +{ + int t; + t = block(len / 2, x, y); + if (t == -1) + { + s[ind++] = '*'; + div(len / 2, x, y); + } + else + s[ind++] = '0' + t; + t = block(len / 2, x, y + len / 2); + if (t == -1) + { + s[ind++] = '*'; + div(len / 2, x, y + len / 2); + } + else + s[ind++] = '0' + t; + t = block(len / 2, x + len / 2, y); + if (t == -1) + { + s[ind++] = '*'; + div(len / 2, x + len / 2, y); + } + else + s[ind++] = '0' + t; + t = block(len / 2, x + len / 2, y + len / 2); + if (t == -1) + { + s[ind++] = '*'; + div(len / 2, x + len / 2, y + len / 2); + } + else + s[ind++] = '0' + t; +} + +int main() +{ + int n, x1, y1, x2, y2, t, ok; + scanf("%d", &n); + while (n--) + { + scanf("%d", &mlen); + t = mlen; + ok = true; + while (t != 1) + { + if (t & 1 && t != 1) + { + ok = false; + break; + } + t = t >> 1; + } + if (ok) + { + init(); + while (scanf(" (%d,%d)", &x1, &y1) == 2) + if (scanf(" (%d,%d)", &x2, &y2) == 2) + mark(x1, y1, x2, y2); + ind = 0; + t = block(mlen, 0, 0); + memset(s, 0, sizeof(s)); + if (t == -1) + { + div(mlen, 0, 0); + for (int i = 0; i < ind; ++i) + printf("%c", s[i]); + } + printf("\n"); + } + else + { + while (scanf(" (%d,%d)", &x1, &y1) == 2) + ; + printf("Size is invalid\n"); + } + } + return 0; +} diff --git a/11942.cpp b/11942.cpp index 98776e7..1eb7fbd 100644 --- a/11942.cpp +++ b/11942.cpp @@ -1,61 +1,44 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -bool isInc(int arr[]){ - int i; - FOI(i, 0, 8) - if( arr[i] > arr[i+1] ) - return false; - return true; -} +using namespace std; -bool isDec(int arr[]){ - int i; - FOI(i, 0, 8) - if( arr[i] < arr[i+1] ) +bool is_ordered(int len[10]) +{ + bool result = true; + for (int i = 1; i < 10; i++) + { + if (len[i] < len[i - 1]) + { return false; - return true; + } + } + return result; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, i, arr[10]; - cin >> T; - cout << "Lumberjacks:\n"; - while( T-- ){ - FOI(i, 0, 9) cin >> arr[i]; - if( isInc(arr) || isDec(arr) ) - cout << "Ordered\n"; - else - cout << "Unordered\n"; +int main() +{ + int n; + int len[10]; + cin >> n; + cout << "Lumberjacks:" << endl; + while (n--) + { + for (int i = 0; i < 10; i++) + { + cin >> len[i]; + } + if (is_ordered(len)) + { + cout << "Ordered" << endl; + continue; + } + reverse(len, len + 10); + if (is_ordered(len)) + { + cout << "Ordered" << endl; + continue; + } + cout << "Unordered" << endl; } return 0; } diff --git a/11943.cpp b/11943.cpp new file mode 100644 index 0000000..0d7c69b --- /dev/null +++ b/11943.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +char ch[120000]; +char name[12000]; +int age, n, nl; +double w[120000]; +int le, st, i, po; +double tt, va, xs; + +int main() +{ + bool fi = false; + while (gets(ch)) + { + le = strlen(ch); + nl = 0; + po = 0; + n = 0; + while (ch[po] != '*') + { + name[nl++] = ch[po]; + po++; + } + po++; + age = 0; + while (ch[po] != '*') + { + age *= 10; + age += ch[po] - '0'; + po++; + } + po++; + st = 0; + va = 0.0; + tt = 0.1; + for (i = po; i < le; i++) + { + if (ch[i] == ' ') + { + continue; + } + if (ch[i] == ',') + { + w[n++] = va * xs; + if (ch[i + 1] == '-') + { + xs = -1.0; + i++; + } + else + { + xs = 1.0; + } + va = 0.0; + st = 0; + } + else if (ch[i] == '.') + { + st = 1; + tt = 0.1; + } + else if (st == 0) + { + va *= 10.0; + va += (double)(ch[i] - '0'); + } + else + { + va += (double)(ch[i] - '0') * tt; + tt *= 0.1; + } + } + w[n++] = va * xs; + if (fi) + { + puts("---"); + } + else + { + fi = true; + } + for (i = 0; i < nl; i++) + { + printf("%c", name[i]); + } + puts(""); + printf("%d\n", age); + int le, ri, id; + id = -1; + for (i = 0; i < n; i++) + { + if (id == -1 || w[id] < w[i]) + { + id = i; + } + } + le = id; + while (le > 0) + { + le--; + } + while (le > 0) + { + le--; + } + ri = id; + while (ri < n - 1) + { + ri++; + } + while (ri < n - 1) + { + ri++; + } + if (w[id] <= 35.0 && (ri - le + 2) <= 10 && (ri - le + 2) >= 6) + { + puts("Triage PASS"); + } + else + { + puts("Triage NO PASS"); + } + } + return 0; +} diff --git a/11944.cpp b/11944.cpp new file mode 100644 index 0000000..5179e87 --- /dev/null +++ b/11944.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int i = (a); i < (b); i++) + +int main() +{ + int c, wagers, t, val; + double sum = 0.00, total = 0.00; + char num[11111], num1[11111]; + scanf("%d", &c); + FOR(j, 0, c) + { + scanf("%d%d", &wagers, &t); + total += wagers; + FOR(i, 0, t) + { + scanf("%d", &val); + if (val < 0.00) + { + sprintf(num, "%.3lf", 100.00 / (double)val * (-1)); + } + else + { + sprintf(num, "%.3lf", (double)val / 100.00); + } + sprintf(num, "%.3lf", atof(num) * wagers); + sum = ceil(atof(num)); + wagers += (int)sum; + total += sum; + strcmp(num, ""); + } + sprintf(num1, "%.2lf", total); + if (total < 1000) + { + printf("%d $%.2lf\n", j + 1, total); + } + else if (total > 1000000.00) + { + printf("%d $1,000,000.00\n", j + 1); + } + else + { + int len = strlen(num1), pos = 0; + if (len == 7) + { + pos = 0; + } + if (len == 8) + { + pos = 1; + } + if (len == 9) + { + pos = 2; + } + printf("%d $", j + 1); + for (int k = 0; k <= pos; k++) + { + printf("%c", num1[k]); + } + printf(","); + for (int k = pos + 1; k < (int)strlen(num1); k++) + { + printf("%c", num1[k]); + } + printf("\n"); + strcmp(num, ""); + strcmp(num1, ""); + } + total = sum = 0.00; + } +} diff --git a/11945.cpp b/11945.cpp index 58bb9b6..e4f8db6 100644 --- a/11945.cpp +++ b/11945.cpp @@ -1,49 +1,20 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef long long int64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +using namespace std; -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int T, t, i; - scanf("%d\n", &T); - FOI(t, 1, T){ - double avg = 0.0, temp; - FOI(i, 1, 12){ - scanf("%lf", &temp); - avg += temp; +int main() +{ + int T; + scanf("%d", &T); + setlocale(LC_ALL, "en_US.UTF-8"); + for (int cnum = 0; cnum++ < T;) + { + double r = 0, z; + for (int i = 0; i < 12; ++i, r += z) + { + scanf("%lf", &z); } - avg /= 12.0; - string str = format(avg); - printf("%d $%.2lf\n", t, avg/12.0); + printf("%d $%'.2lf\n", cnum, r / 12.0); } return 0; } diff --git a/11946.cpp b/11946.cpp index 1577c73..b483a1e 100644 --- a/11946.cpp +++ b/11946.cpp @@ -1,60 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - map Map; - Map.clear(); - Map['0'] = 'O'; Map['1'] = 'I'; Map['2'] = 'Z'; Map['3'] = 'E'; Map['4'] = 'A'; - Map['5'] = 'S'; Map['6'] = 'G'; Map['7'] = 'T'; Map['8'] = 'B'; Map['9'] = 'P'; - int T; - cin >> T; - string str; - getline(cin, str); - while( T-- ){ - while( getline(cin, str) ){ - if( str == "" ) - break; - string out = ""; - int i, L = str.length(); - FOI(i, 0, L-1){ - if( Map.find(str[i]) != Map.end() ) - out += Map[str[i]]; - else - out += str[i]; - } - cout << out << endl; +int main() +{ + int t; + size_t pos; + string input; + string encoded = "48CD3F6H1JKLMN09QR57UVWXY2 ,."; + string decoded = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."; + cin >> t; + cin.ignore(100, '\n'); + while (getline(cin, input)) + { + for (int i = 0, sz = input.size(); i < sz; i++) + { + cout << decoded[int(encoded.find(input[i]))]; } - if( T > 0 ) - cout << endl; + cout << endl; } return 0; } diff --git a/11947.cpp b/11947.cpp new file mode 100644 index 0000000..1c32164 --- /dev/null +++ b/11947.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +bool is_leap(int year) +{ + return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)); +} + +string zodiac(int day, int month) +{ + if (day >= 21 && month == 1 || day <= 19 && month == 2) + { + return "aquarius"; + } + if (day >= 20 && month == 2 || day <= 20 && month == 3) + { + return "pisces"; + } + if (day >= 21 && month == 3 || day <= 20 && month == 4) + { + return "aries"; + } + if (day >= 21 && month == 4 || day <= 21 && month == 5) + { + return "taurus"; + } + if (day >= 22 && month == 5 || day <= 21 && month == 6) + { + return "gemini"; + } + if (day >= 22 && month == 6 || day <= 22 && month == 7) + { + return "cancer"; + } + if (day >= 23 && month == 7 || day <= 21 && month == 8) + { + return "leo"; + } + if (day >= 22 && month == 8 || day <= 23 && month == 9) + { + return "virgo"; + } + if (day >= 24 && month == 9 || day <= 23 && month == 10) + { + return "libra"; + } + if (day >= 24 && month == 10 || day <= 22 && month == 11) + { + return "scorpio"; + } + if (day >= 23 && month == 11 || day <= 22 && month == 12) + { + return "sagittarius"; + } + if (day >= 23 && month == 12 || day <= 20 && month == 1) + { + return "capricorn"; + } +} + +int main() +{ + int t; + int date; + int m, d, y; + int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> date; + y = date % 10000; + date /= 10000; + d = date % 100; + m = date / 100; + if (is_leap(y)) + { + days[1] = 29; + } + else + { + days[1] = 28; + } + for (int i = 0; i < 40 * 7; i++) + { + d++; + if (d > days[m - 1]) + { + d = 1; + m++; + if (m > 12) + { + y++; + m = 1; + if (is_leap(y)) + { + days[1] = 29; + } + else + { + days[1] = 28; + } + } + } + } + printf("%d %02d/%02d/%0004d %s\n", casenum + 1, m, d, y, zodiac(d, m).c_str()); + } + return 0; +} diff --git a/11948.cpp b/11948.cpp new file mode 100644 index 0000000..7ae1d27 --- /dev/null +++ b/11948.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define sf scanf +#define pf printf +#define wh while +#define st struct +#define ma malloc +#define me memset +#define spf sprintf +#define ssf sscanf +#define inf 2147483647 +#define ll long long + +const int maxl = 1000007; +const int maxz = 1007; + +bool maz[maxz][maxz]; +char in[maxl]; +int t, n, m, i, j, k, u, cas, ilen, plen, nlen; + +bool ch(int n) +{ + wh(n != 1) + { + if (n % 2) + return 0; + n /= 2; + } + return 1; +} + +void init() +{ + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + maz[i][j] = 0; + ilen = strlen(in); +} + +void rec(int plen, int k, int posx, int posy) +{ + for (int i = posx; i < posx + plen; i++) + for (int j = posy; j < posy + plen; j++) + maz[i][j] = k; +} + +int dfs(int j, int plen, int posx, int posy) +{ + if (j >= ilen) + return 0; + int i; + for (i = 0; i < 4 && j + i < ilen; i++) + if (in[j + i] != '*') + { + if (i == 0) + rec(plen, in[j + i] - '0', posx, posy); + else if (i == 1) + rec(plen, in[j + i] - '0', posx, posy + plen); + else if (i == 2) + rec(plen, in[j + i] - '0', posx + plen, posy); + else if (i == 3) + rec(plen, in[j + i] - '0', posx + plen, posy + plen); + } + else + { + if (i == 0) + j = dfs(i + j + 1, plen / 2, posx, posy); + else if (i == 1) + j = dfs(i + j + 1, plen / 2, posx, posy + plen); + else if (i == 2) + j = dfs(i + j + 1, plen / 2, posx + plen, posy); + else if (i == 3) + j = dfs(i + j + 1, plen / 2, posx + plen, posy + plen); + j -= (i + 1); + } + return j + i; +} + +void output() +{ + bool flag = 0; + int p; + for (int i = 1; i <= n; i++) + { + int j = 0; + wh(j <= n) + { + wh(j <= n && maz[i][j] == 0) j++; + if (j <= n) + { + if (flag) + pf(","); + pf("(%d", j); + p = j; + } + else + break; + wh(j <= n && maz[i][j] == 1) j++; + if (p != j - 1) + pf("-%d", j - 1); + pf(",%d)", i); + flag = 1; + } + } + puts(""); +} + +int main() +{ + sf("%d", &t); + wh(t--) + { + sf("%d", &n); + sf("%s", in); + if (!ch(n)) + { + puts("Invalid length"); + continue; + } + init(); + dfs(0, n, 1, 1); + output(); + } + return 0; +} diff --git a/11949.cpp b/11949.cpp new file mode 100644 index 0000000..aaa2ebd --- /dev/null +++ b/11949.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const double eps = 1e-12; +double a[120000]; +char buf[1200000]; +int _, n, i, j; +double lo, up, mid, s, ts, rt; + +int main() +{ + scanf("%d", &_); + gets(buf); + while (_--) + { + gets(buf); + istringstream is(buf); + n = 0; + while (is >> a[n]) + { + n++; + } + n--; + lo = -1.0; + up = 1.0; + if (fabs(a[0]) < eps) + { + puts("-100.000%"); + continue; + } + for (int tt = 0; tt < 200; tt++) + { + mid = (lo + up) / 2.0; + ts = 0.0; + rt = 1.0 + mid; + for (i = 1; i <= n; i++) + { + ts += a[i] / rt; + rt *= (1.0 + mid); + } + if ((a[0] > 0 && ts > a[0] + eps) || (a[0] < 0 && ts < a[0] - eps)) + { + lo = mid; + } + else + { + up = mid; + } + } + lo *= 100.0; + printf("%.3f", lo); + puts("%"); + } + return 0; +} diff --git a/1195.cpp b/1195.cpp new file mode 100644 index 0000000..fe8e277 --- /dev/null +++ b/1195.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +bool is[100005]; +vector p; + +void pri() +{ + memset(is, false, sizeof(is)); + int i, j; + p.clear(); + for (i = 2; i < 100005; i++) + { + if (!is[i]) + { + for (j = i + i; j < 100005; j += i) + { + is[j] = true; + } + p.push_back(i); + } + } +} + +int main() +{ + pri(); + int M, A, B, i, j, m; + while (scanf(" %d %d %d", &M, &A, &B) == 3 && (M + A + B)) + { + m = sqrt(M); + int maxi = 0, ap, aq; + for (i = 0; i < p.size() && p[i] <= m; i++) + { + for (j = i; j < p.size(); j++) + { + if (A * p[j] > B * p[i]) + { + break; + } + if (p[i] * p[j] > M) + { + break; + } + if (p[i] * p[j] > maxi) + { + maxi = p[i] * p[j]; + ap = p[i]; + aq = p[j]; + } + } + } + printf("%d %d\n", ap, aq); + } + return 0; +} diff --git a/11950.cpp b/11950.cpp new file mode 100644 index 0000000..3f34e87 --- /dev/null +++ b/11950.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +struct kid +{ + char name[100]; + char fst; + char sec; + int ji; + int no; +} s[100000], zzl; +int top; +char a[10000000]; +int lena; + +bool cmp(kid x, kid y) +{ + if (x.ji == y.ji) + { + return x.no < y.no; + } + else + { + return x.ji > y.ji; + } +} + +bool who(char x, char y) +{ + if (x == 'S' && y == 'P') + { + return 1; + } + if (x == 'P' && y == 'R') + { + return 1; + } + if (x == 'R' && y == 'S') + { + return 1; + } + return 0; +} + +int main() +{ + int i, j; + int jj, nn; + int cas = -1; + while (gets(a)) + { + if (cas == 1) + { + printf("\n"); + } + else + { + cas = 1; + } + lena = strlen(a); + jj = 0; + top = 0; + for (i = 0; i < lena; i++) + { + if (a[i] == '[') + { + jj++; + } + else if (a[i] == ']') + { + jj--; + } + else if (a[i] == '(') + { + for (j = i + 1; a[j] != '-'; j++) + { + s[top].name[j - i - 1] = a[j]; + } + s[top].name[j - i - 1] = '\0'; + s[top].fst = a[j + 1]; + s[top].sec = a[j + 3]; + s[top].ji = jj; + s[top].no = top; + top++; + i = j + 4; + } + } + sort(s, s + top, cmp); + /* + for(i=0;i + +using namespace std; + +int main() +{ + int caseCount = 0; + const int MAX_DIM = 100; + cin >> caseCount; + for (int caseIdx = 1; caseIdx <= caseCount; ++caseIdx) + { + long long int grid[MAX_DIM + 1][MAX_DIM + 1] = {{0}}; + long long int sum[MAX_DIM + 1][MAX_DIM + 1] = {{0}}; + long long int n, m, k; + cin >> n >> m >> k; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + cin >> grid[i][j]; + sum[i][j] = grid[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; + } + } + long long int s = 0; + long long int p = 0; + for (int x1 = 0; x1 < n; x1++) + { + for (int y1 = 0; y1 < m; y1++) + { + for (int x2 = x1 + 1; x2 <= n; x2++) + { + for (int y2 = y1 + 1; y2 <= m; y2++) + { + if ((x2 - x1) * (y2 - y1) > s && (sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]) <= k) + { + s = (x2 - x1) * (y2 - y1); + p = sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]; + } + else if ((x2 - x1) * (y2 - y1) == s && (sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]) <= k) + { + p = min(p, + sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1]); + } + } + } + } + } + cout << "Case #" << caseIdx << ": " << s << " " << p << endl; + } + return 0; +} diff --git a/11952.cpp b/11952.cpp new file mode 100644 index 0000000..eb9d5f1 --- /dev/null +++ b/11952.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int flag; +long long ch(char s[], long long base) +{ + long long sum = 0; + int i; + for (i = 0; s[i]; i++) + { + sum = s[i] - '0' + sum * base; + if (s[i] - '0' >= base) + flag = 1; + } + return sum; +} +int main() +{ + int t, i; + char A[9], B[9], C[9]; + scanf("%d", &t); + while (t--) + { + scanf("%s + %s = %s", &A, &B, &C); + long long a, b, c, d; + a = 0, b = 0, c = 0, d = 1; + for (a = 0; A[a]; a++) + d &= A[a] == '1'; + for (b = 0; B[b]; b++) + d &= B[b] == '1'; + for (c = 0; C[c]; c++) + d &= C[c] == '1'; + if (d && a + b == c) + { + puts("1"); + continue; + } + for (i = 2; i <= 20; i++) + { + flag = 0; + a = ch(A, i), b = ch(B, i), c = ch(C, i); + if (a + b == c && flag == 0) + { + printf("%d\n", i); + i = 500; + } + } + if (i == 21) + puts("0"); + } + return 0; +} diff --git a/11953.cpp b/11953.cpp index dad6aa9..5dd22de 100644 --- a/11953.cpp +++ b/11953.cpp @@ -1,51 +1,57 @@ -#include -#include +#include + using namespace std; -char Mat[110][110]; -int N; +char field[102][102]; -int dx[] = {-1, 1, 0, 0}; -int dy[] = {0, 0, -1, 1}; -void dfs(int i, int j) { - stack< pair > S; - S.push(make_pair(i, j)); - while (!S.empty()) { - pair P = S.top(); - S.pop(); - i = P.first; - j = P.second; - if (i < 0 || i >= N) continue; - if (j < 0 || j >= N) continue; - if (Mat[i][j] == '.')continue; - - Mat[i][j] = '.'; - for (int I = 0; I < 4; I++) - S.push(make_pair(i + dx[I], j + dy[I])); +void remove_ship(int x, int y) +{ + field[x][y] = '.'; + if (field[x + 1][y] == 'x' || field[x + 1][y] == '@') + { + remove_ship(x + 1, y); + } + if (field[x][y + 1] == 'x' || field[x][y + 1] == '@') + { + remove_ship(x, y + 1); + } + if (field[x - 1][y] == 'x' || field[x - 1][y] == '@') + { + remove_ship(x - 1, y); + } + if (field[x][y - 1] == 'x' || field[x][y - 1] == '@') + { + remove_ship(x, y - 1); } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int t, T; - scanf("%d", &T); - for (t = 1; t <= T; t++) { - scanf("%d", &N); - int i, j; - int cnt = 0; - - for (i = 0; i < N; i++) - scanf("%s", Mat[i]); - - for (i = 0; i < N; i++) - for (j = 0; j < N; j++) - if (Mat[i][j] == 'x') { - cnt++; - dfs(i, j); +int main() +{ + int t, n; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + cin >> field[i][j]; + } + } + int result = 0; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (field[i][j] == 'x') + { + result++; + remove_ship(i, j); } - - printf("Case %d: %d\n", t, cnt); + } + } + cout << "Case " << casenum + 1 << ": " << result << endl; } return 0; } diff --git a/11954.cpp b/11954.cpp new file mode 100644 index 0000000..ded14d6 --- /dev/null +++ b/11954.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +#define SZ(s) s.length() +#define rep(i, n) for (int i = 0; i < n; i++) + +string in; +int pos; + +string number() +{ + string ret; + while (pos < SZ(in) && isdigit(in[pos])) + { + ret += in[pos++]; + } + while (SZ(ret) > 1 && ret[0] == '0') + { + ret = ret.substr(1); + } + return ret; +} + +string token() +{ + string ret; + if (isdigit(in[pos])) + { + return number(); + } + string op = in.substr(pos, 3); + pos += 3; + ret = token(); + if (op == "not") + { + rep(i, SZ(ret)) + { + if (ret[i] == '0') + { + ret[i] = '1'; + } + else + { + ret[i] = '0'; + } + } + } + else if (op == "shr") + { + if (SZ(ret) > 1) + { + ret = ret.substr(0, SZ(ret) - 1); + } + else + { + ret = "0"; + } + } + else + { + ret += '0'; + while (SZ(ret) > 1 && ret[0] == '0') + { + ret = ret.substr(1); + } + } + while (SZ(ret) > 1 && ret[0] == '0') + { + ret = ret.substr(1); + } + return ret; +} + +string expression() +{ + string ret = token(); + string li; + while (pos < in.size()) + { + li = ret; + ret = ""; + string op; + if (in[pos] != 'o') + { + op = in.substr(pos, 3); + pos += 3; + } + else + { + op = in.substr(pos, 2); + pos += 2; + } + string ri = token(); + int sz = max(SZ(li), SZ(ri)); + while (SZ(ri) < sz) + { + ri = '0' + ri; + } + while (SZ(li) < sz) + { + li = '0' + li; + } + if (op == "or") + { + rep(i, SZ(li)) + { + if (li[i] == '1' || ri[i] == '1') + { + ret += '1'; + } + else + { + ret += '0'; + } + } + } + else if (op == "and") + { + rep(i, SZ(li)) + { + if (li[i] == '1' && ri[i] == '1') + { + ret += '1'; + } + else + { + ret += '0'; + } + } + } + else + { + rep(i, SZ(li)) + { + if (li[i] + ri[i] - 2 * '0' == 1) + { + ret += '1'; + } + else + { + ret += '0'; + } + } + } + } + while (SZ(ret) > 1 && ret[0] == '0') + { + ret = ret.substr(1); + } + return ret; +} + +int main() +{ + int test; + cin >> test; + cin.ignore(); + rep(ca, test) + { + cout << "Case " << ca + 1 << ": "; + string temp; + getline(cin, temp); + in = ""; + pos = 0; + rep(i, SZ(temp)) if (temp[i] != ' ') + { + in += temp[i]; + } + string ans = expression(); + cout << ans << endl; + } + return 0; +} diff --git a/11955.cpp b/11955.cpp index 8ce0283..ced8859 100644 --- a/11955.cpp +++ b/11955.cpp @@ -1,88 +1,102 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef long long int64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +// nCr + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +char ch[1000 + 10], a[1000 + 10], b[1000 + 10], p[10]; +int power; + +void parseInput() +{ + int ind = 1; + int k = 0; + while (ch[ind] != '+') + a[k++] = ch[ind++];// 1st operand + a[k] = 0; + ind++; + k = 0; + while (ch[ind] != ')') + b[k++] = ch[ind++];// 2nd operand + b[k] = 0; + ind += 2; + k = 0; + while (ch[ind]) + p[k++] = ch[ind++]; + p[k] = 0; + power = atoi(p); +} + +void printCoefficients(int n, int k) +{ + int maxi = max(n - k, k); + LL res = 1; + int p = 1; + for (int i = maxi + 1; i <= n; i++) + { + res *= i; + res /= p; + p++; + } + if (res > 1) + cout << res; +} + +void printA(int p) +{ + if (p == 1) + printf("*%s", a); + else if (p == power) + printf("%s^%d", a, p); + else if (p > 1) + printf("*%s^%d", a, p); +} + +void printB(int p) +{ + if (p == 1) + printf("*%s", b); + else if (p == power) + printf("%s^%d", b, p); + else if (p > 1) + printf("*%s^%d", b, p); +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - scanf("%d\n", &T); - FOI(t, 1, T){ - string str, temp = ""; - string A, B, K; - getline(cin, str); - int i, j; - FOI(i, 0, str.length()-1){ - if(str[i] == '(' || str[i] == ' ') - continue; - else if(str[i] == '+'){ - A = temp; - temp = ""; - } - else if(str[i] == ')'){ - B = temp; - temp = ""; - } - else if(str[i] == '^') - continue; - else - temp += str[i]; +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + scanf("%s", ch); + parseInput(); + printf("Case %d: ", ++cases); + if (power == 1) + { + printf("%s+%s\n", a, b); + continue; } - K = temp; - //cout << A << " " << B << " " << K << endl; - /* - A[0] = 'a'; A[1] = '\0'; - B[0] = 'b'; B[1] = '\0'; - K[0] = '2'; K[1] = '\0'; - */ - int k = atoi(K.c_str()); - int64 cof[k+1]; - cof[0] = 1; - FOI(i, 1, k) - cof[i] = (cof[i-1] * (k - i + 1)) / i; - printf("Case %d: ", t); - FOI(i, 0, k){ - if( cof[i] > 1 ) - printf("%lld*", cof[i]); - if(k - i > 0) - printf("%s", A.c_str()); - if(k - i > 1) - printf("^%d", k-i); - if((cof[i] > 1 || k - i > 0) && i > 0) - printf("*"); - if(i > 0) - printf("%s", B.c_str()); - if(i > 1) - printf("^%d", i); - if(i < k) + for (int i = 0; i <= power; i++) + { + printCoefficients(power, i); + printA(power - i); + printB(i); + if (i != power) printf("+"); } printf("\n"); diff --git a/11956.cpp b/11956.cpp new file mode 100644 index 0000000..53bee33 --- /dev/null +++ b/11956.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int main() +{ + int t; + string input; + cin >> t; + cin.ignore(100, '\n'); + rep(case_number, t) + { + getline(cin, input); + int memory[100]; + int pointer = 0; + rep(i, 100) + memory[i] = 0; + rep(i, input.size()) + { + switch (input[i]) + { + case '>': + pointer++; + if (pointer == 100) + { + pointer = 0; + } + break; + case '<': + pointer--; + if (pointer == -1) + { + pointer = 99; + } + break; + case '+': + memory[pointer]++; + if (memory[pointer] == 256) + { + memory[pointer] = 0; + } + break; + case '-': + memory[pointer]--; + if (memory[pointer] == -1) + { + memory[pointer] = 255; + } + break; + case '.': + break; + } + } + cout << "Case " << case_number + 1 << ":"; + rep(i, 100) + { + printf(" %02X", memory[i]); + } + cout << endl; + } + return 0; +} diff --git a/11957.cpp b/11957.cpp new file mode 100644 index 0000000..db7edd3 --- /dev/null +++ b/11957.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +char mat[100 + 5][100 + 5]; +int paths[100 + 5][100 + 5]; +int n; + +void reset() +{ + for (int i = 0; i < 105; i++) + for (int j = 0; j < 105; j++) + { + paths[i][j] = 0; + } +} + +void inc(int r, int c) +{ + paths[r][c] %= 1000007; + if (r + 1 < n && c + 1 < n) + if (mat[r + 1][c + 1] != 'B') + { + paths[r + 1][c + 1] = (paths[r + 1][c + 1] + paths[r][c]) % 1000007; + } + else if (r + 2 < n && c + 2 < n) + if (mat[r + 2][c + 2] != 'B') + { + paths[r + 2][c + 2] = (paths[r + 2][c + 2] + paths[r][c]) % 1000007; + } + if (r + 1 < n && c - 1 >= 0) + if (mat[r + 1][c - 1] != 'B') + { + paths[r + 1][c - 1] = (paths[r + 1][c - 1] + paths[r][c]) % 1000007; + } + else if (r + 2 < n && c - 2 >= 0) + if (mat[r + 2][c - 2] != 'B') + { + paths[r + 2][c - 2] = (paths[r + 2][c - 2] + paths[r][c]) % 1000007; + } +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + reset(); + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%s", mat[i]); + for (int i = 0; i < n; i++) + if (mat[0][i] != 'B') + paths[0][i] = 1; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + inc(i, j); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (mat[i][j] == 'W') + printf("Case %d: %d\n", ++cases, paths[i][j] % 1000007); + } + return 0; +} diff --git a/11958.cpp b/11958.cpp new file mode 100644 index 0000000..e4da16f --- /dev/null +++ b/11958.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int main() +{ + int t, k, h, m, q, best; + char c; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> k >> h >> c >> m; + best = -1; + int start = h * 60 + m; + for (int i = 0; i < k; i++) + { + cin >> h >> c >> m >> q; + int bus = h * 60 + m; + if (bus < start) + { + bus += 24 * 60; + } + int end = bus + q; + if (end - start < best || best == -1) + { + best = end - start; + } + } + cout << "Case " << casenum + 1 << ": " << best << endl; + } + return 0; +} diff --git a/11959.cpp b/11959.cpp new file mode 100644 index 0000000..26ae5bc --- /dev/null +++ b/11959.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +#define R(d) (((d)&0x000F00) << 12 | ((d)&0x00000F) << 16 | ((d)&0x00F0F0) | ((d)&0x0F0000) >> 8 | ((d)&0xF00000) >> 20) +#define F(d) (((d)&0x0000F0) << 16 | ((d)&0x00F000) << 4 | ((d)&0xF00000) >> 8 | ((d)&0x000F0F) | ((d)&0x0F0000) >> 12) + +bool E(long long d1, long long d2) +{ + return ((d1) == (d2) || (d1) == R((d2)) || (d1) == R(R((d2))) || (d1) == R(R(R((d2))))); +} + +char ans[2][16] = {"Not Equal", "Equal"}; + +int main() +{ + long long t, d1, d2; + scanf("%lld", &t); + while (t--) + { + bool out = false; + scanf("%llx %llx", &d1, &d2); + out = E(d1, d2) || E(d1, F(d2)) || E(d1, F(F(d2))) || E(d1, F(F(F(d2)))) || E(d1, F(R(d2))) || E(d1, F(R(R(R(d2))))); + printf("%s\n", ans[out]); + } + return 0; +} diff --git a/1196.cpp b/1196.cpp new file mode 100644 index 0000000..8c3d744 --- /dev/null +++ b/1196.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +struct state +{ + int a, b; +} seq[20000]; + +int compara(const void *a, const void *b) +{ + state a1 = *(state *)a; + state a2 = *(state *)b; + if (a1.a == a2.a) + { + return a1.b - a2.b; + } + return a1.a - a2.a; +} + +int n; + +int read() +{ + scanf("%d", &n); + if (n == 0) + { + printf("*\n"); + return 0; + } + int i; + for (i = 0; i < n; i++) + { + scanf("%d %d", &seq[i].a, &seq[i].b); + } + qsort(seq, n, sizeof(seq[0]), compara); + return 1; +} + +int lista[20000]; + +void process() +{ + int tam = 0; + lista[0] = seq[0].b; + int i; + for (i = 1; i < n; i++) + { + if (seq[i].b >= lista[tam]) + { + tam++; + lista[tam] = seq[i].b; + } + else + { + int ini = 0, fim = tam; + while (ini != fim) + { + int med = (ini + fim) / 2; + if (lista[med] <= seq[i].b) + { + ini = med + 1; + } + else + { + fim = med; + } + } + lista[ini] = seq[i].b; + } + } + printf("%d\n", tam + 1); +} + +int main() +{ + while (read()) + { + process(); + } + return 0; +} diff --git a/11960.cpp b/11960.cpp index bb4a00d..7bf90c6 100644 --- a/11960.cpp +++ b/11960.cpp @@ -1,110 +1,60 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +using namespace std; -int dp[MAX + 1]; -bool P[MAX + 1]; -vector Prime; -int N; +const int MAXP = 1024, N = 1048576; +int ans[N], div_[N]; +int pl[N / 10], plc = 1; +bool prime[1024]; -void seive(){ - memset(P, true, sizeof P); - Prime.clear(); - P[0] = false; P[1] = false; - int i; - FOI(i, 2, MAX){ - if( P[i] ){ - Prime.push_back(i); - for(int j = 2*i; j <= MAX; j += i) - P[j] = false; +void calculate_divs(int idx, int num, int divs) +{ + div_[num] = divs; + for (int i = idx; i < plc; ++i) + for (int mul = pl[i], j = 1; mul <= N / num; mul *= pl[i], ++j) + { + calculate_divs(i + 1, num * mul, divs * (1 + j)); } - } } -int countDiv(int M){ - int i; - int ret = 1; - FOI(i, 0, N){ - if( Prime[i] > M ) break; - int cnt = 0; - while( M % Prime[i] == 0 ){ - ++cnt; - M /= Prime[i]; +int main() +{ + pl[0] = 2; + prime[0] = prime[1] = false; + prime[2] = true; + for (int i = 3; i < MAXP; ++i) + { + prime[i] = i & 1; + } + for (int i = 3; i < 32; i += 2) + if (prime[i]) + for (int j = i * i, k = 2 * i; j < MAXP; j += k) + { + prime[j] = false; + } + for (int i = 3; i < MAXP; i += 2) + { + if (prime[i]) + { + pl[plc++] = i; } - ret = ret * (cnt + 1); + div_[i] = 2; } - return ret; -} - -int count(int n){ - int p = 1; - int factor = 1; - while (n % 2 == 0) { - p++; - n /= 2; - } - factor *= p; - for (int i = 3; i*i <= n; i+=2) { - p = 1; - while (n % i == 0) { - p++; - n /= i; - } - factor *= p; - } - if (n > 1) factor *= 2; - return factor; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - //seive(); - //N = Prime.size() - 1; - dp[0] = 0; dp[1] = 1; - int cmax = 1, cval = 1; - int i; - FOI(i, 2, MAX){ - int tmax = count(i); - if( tmax >= cmax ){ - cmax = tmax; - cval = i; + calculate_divs(0, 1, 1); + int maxd = -1, cans; + for (int i = 0; i < N; ++i) + { + if (div_[i] >= maxd) + { + maxd = div_[i]; + cans = i; } - dp[i] = cval; + ans[i] = cans; } - int T; - scanf("%d", &T); - while( T-- ){ - int M; - scanf("%d", &M); - printf("%d\n", dp[M]); + scanf("%*d"); + for (int n; scanf("%d", &n) == 1;) + { + printf("%d\n", ans[n]); } return 0; } diff --git a/11961.cpp b/11961.cpp new file mode 100644 index 0000000..1524aa3 --- /dev/null +++ b/11961.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +char s[20], buf[20]; +int t, n, m, ans; + +void dfs(int idx, int k) +{ + if (k > m) + return; + if (idx == n) + { + ans++; + return; + } + buf[idx] = 'A'; + dfs(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'C'; + dfs(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'G'; + dfs(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'T'; + dfs(idx + 1, k + (buf[idx] != s[idx])); +} + +void dfs2(int idx, int k) +{ + if (k > m) + return; + if (idx == n) + { + buf[idx] = '\0'; + puts(buf); + return; + } + buf[idx] = 'A'; + dfs2(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'C'; + dfs2(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'G'; + dfs2(idx + 1, k + (buf[idx] != s[idx])); + buf[idx] = 'T'; + dfs2(idx + 1, k + (buf[idx] != s[idx])); +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &m); + scanf("%s", s); + ans = 0; + dfs(0, 0); + printf("%d\n", ans); + dfs2(0, 0); + } + return 0; +} diff --git a/11962.cpp b/11962.cpp new file mode 100644 index 0000000..a8661ae --- /dev/null +++ b/11962.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +long long pow_(int a, int b) +{ + long long res = 1; + while (b--) + { + res *= a; + } + return res; +} + +long long calc(string &input, int start) +{ + long long res = 0; + if (start == input.size() - 1) + { + switch (input[start]) + { + case 'A': + return 0; + case 'C': + return 1; + case 'G': + return 2; + case 'T': + return 3; + } + } + switch (input[start]) + { + case 'A': + res = calc(input, start + 1); + break; + case 'C': + res = pow_(4, input.size() - start - 1) + calc(input, start + 1); + break; + case 'G': + res = 2 * pow_(4, input.size() - start - 1) + calc(input, start + 1); + break; + case 'T': + res = 3 * pow_(4, input.size() - start - 1) + calc(input, start + 1); + break; + } + return res; +} + +int main() +{ + int t; + string input; + cin >> t; + cin.ignore(100, '\n'); + for (int i = 0; i < t; i++) + { + getline(cin, input); + cout << "Case " << i + 1 << ": (" << input.size() << ":" << calc(input, 0) << ")" << endl; + } + return 0; +} diff --git a/11964.cpp b/11964.cpp new file mode 100644 index 0000000..988f0d7 --- /dev/null +++ b/11964.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define maxL (150 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[maxL]; +int P[150], Pt = 0; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 150; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} +long long mulmod(long long a, long long b, long long mod) +{ + long long ret = 0; + for (; b != 0; b >>= 1, (a <<= 1) %= mod) + if (b & 1) + (ret += a) %= mod; + return ret; +} +long long inv(long long n, long long m) +{// get n*? = 1 (mod m) + long long la = 1, lb = 0, ra = 0, rb = 1; + long long i = 0, t, mod = m; + while (n % m) + { + if (!i) + { + la -= n / m * ra; + lb -= n / m * rb; + } + else + { + ra -= n / m * la; + rb -= n / m * lb; + } + i = !i; + t = n, n = m, m = t % m; + } + return i ? (la % mod + mod) % mod : (ra % mod + mod) % mod; +} +long long chinese_remainder(int n, long long m[], long long a[]) +{ + long long M = 1, ret = 0; + for (int i = 0; i < n; i++) + M *= m[i]; + for (int i = 0; i < n; i++) + { + ret += a[i] * inv(M / m[i], m[i]) % M * (M / m[i]); + ret %= M; + } + + return (ret % M + M) % M; +} +vector> factor(long long n) +{ + vector> R; + + for (int i = 0, j; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + for (j = 0; n % P[i] == 0; n /= P[i], j++) + ; + R.push_back(make_pair(P[i], j)); + } + } + if (n != 1) + R.push_back(make_pair(n, 1)); + return R; +} +long long mpow(long long x, long long y) +{ + long long ret = 1; + for (int i = 0; i < y; i++) + ret *= x; + return ret; +} +vector dp[37]; +int main() +{ + sieve(); + for (int i = 0; i < Pt; i++) + { + long long m = P[i]; + int p = 0; + for (m = P[i], p = 0; m * P[i] <= 1e+15; p++, m *= P[i]) + ; + dp[i].resize(100005, 0); + dp[i][0] = 1; + for (int j = 1; j <= 100000; j <<= 1) + { + for (int k = j; k <= 100000; k++) + dp[i][k] = (dp[i][k] + dp[i][k - j]) % m; + } + } + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int K; + long long M; + scanf("%d %lld", &K, &M); + vector> f = factor(M); + long long m[35], a[35]; + for (int i = 0; i < f.size(); i++) + { + for (int j = 0; j < Pt; j++) + if (f[i].first == P[j]) + m[i] = mpow(P[j], f[i].second), a[i] = dp[j][K] % m[i]; + } + long long ret = chinese_remainder(f.size(), m, a); + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/11965.cpp b/11965.cpp index 0da0149..42e5e75 100644 --- a/11965.cpp +++ b/11965.cpp @@ -1,77 +1,44 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - char str[600]; - scanf("%d", &T); - cin.getline(str, 600); - FOI(t, 1, T){ - int N; - printf("Case %d:\n", t); - scanf("%d", &N); - cin.getline(str, 600); - while (N--){ - cin.getline(str, 600); - int L = strlen(str); - int i, j; - bool space = false; - FOI(i, 0, L-1){ - if (str[i] == ' '){ - if (!space){ - printf("%c", str[i]); - space = true; +int main() +{ + int t, n; + string input; + char prev = 'c'; + cin >> t; + for (int c = 0; c < t; c++) + { + cin >> n; + cin.ignore(100, '\n'); + if (c) + { + cout << endl; + } + cout << "Case " << c + 1 << ":" << endl; + for (int i = 0; i < n; i++) + { + getline(cin, input); + prev = 'c'; + for (int j = 0, sz = input.size(); j < sz; j++) + { + if (input[j] == ' ') + { + if (prev != ' ') + { + cout << input[j]; } - else - continue; + prev = ' '; } - else{ - printf("%c", str[i]); - space = false; + else + { + prev = 'c'; + cout << input[j]; } } - printf("\n"); + cout << endl; } - if (t < T) - printf("\n"); } return 0; } - diff --git a/11966.cpp b/11966.cpp new file mode 100644 index 0000000..be70e5b --- /dev/null +++ b/11966.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + int solve(int n); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +int Graph::solve(int n) +{ + int tmp; + queue q; + Vertex *v; + bool visited[n + 1]; + int result = 0; + fill(visited, visited + n + 1, false); + for (int i = 0; i < n; i++) + { + if (visited[i]) + { + continue; + } + result++; + q.push(i); + visited[i] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + v = getVertex(tmp); + for (int j = 0, sz = v->adj.size(); j < sz; j++) + { + if (visited[v->adj[j]->id]) + { + continue; + } + visited[v->adj[j]->id] = true; + q.push(v->adj[j]->id); + } + } + } + return result; +} + +int main() +{ + int t, n; + double d; + double x, y; + vector> stars; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + Graph g; + stars.clear(); + cin >> n >> d; + for (int i = 0; i < n; i++) + { + g.addVertex(i); + cin >> x >> y; + for (int j = 0, sz = stars.size(); j < sz; j++) + { + double dist = sqrt((x - stars[j].first) * (x - stars[j].first) + (y - stars[j].second) * (y - stars[j].second)); + if (dist <= d) + { + g.addEdge(i, j); + } + } + stars.push_back(make_pair(x, y)); + } + cout << "Case " << casenum + 1 << ": " << g.solve(n) << endl; + } + return 0; +} diff --git a/11967.cpp b/11967.cpp new file mode 100644 index 0000000..343f58d --- /dev/null +++ b/11967.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#define x first +#define y second + +typedef pair pll; +const int dx[] = {0, 1, 1, 1}, dy[] = {1, 1, 0, -1}; + +bool check(const set &player, const int win) +{ + for (set::iterator i = player.begin(); i != player.end(); ++i) + for (int j = 0; j < 4; ++j)// 4 directions + { + bool line = true; + for (int k = 0; k < win && line; ++k) + line &= player.find(pll(i->x + dx[j] * k, i->y + dy[j] * k)) != player.end(); + if (line) + return true; + } + return false; +} + +int main() +{ + int t, x, y, win, turn; + + scanf("%d", &t); + for (int n = 1; n <= t; ++n) + { + set move[2];// player + bool a = false, b = false; + + scanf("%d %d", &turn, &win); + for (int i = 0; i < turn; ++i) + { + scanf("%d %d", &x, &y); + move[i % 2].insert(pll(x, y)); + } + a = check(move[0], win); + b = check(move[1], win); + + if (a) + printf("Case %d: %s\n", n, b ? "error" : "crosses"); + else + printf("Case %d: %s\n", n, b ? "noughts" : "none"); + } +} diff --git a/11968.cpp b/11968.cpp new file mode 100644 index 0000000..61575f2 --- /dev/null +++ b/11968.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int main() +{ + long long int menu, cake, drink, t, tmp, *up, *down; + scanf("%lld", &t); + + for (long long int i = 1; i <= t; ++i) + { + scanf("%lld %lld %lld", &menu, &cake, &drink); + long long int Cake[cake], Drink[drink], total = 0; + double ave; + for (long long int i = 0; i < cake; ++i) + { + scanf("%lld", Cake + i); + total += Cake[i]; + } + for (long long int i = cake; i < drink + cake; ++i) + { + scanf("%lld", Drink + i - cake); + total += Drink[i - cake]; + } + for (long long int i = drink + cake; i < menu; ++i) + { + scanf("%lld", &tmp); + total += tmp; + } + ave = total / (double)menu; + printf("Case #%lld: ", i); + sort(Cake, Cake + cake); + up = upper_bound(Cake, Cake + cake, ave); + down = lower_bound(Cake, Cake + cake, ave); + if (up == Cake + cake) + --up; + if (*down != ave && down != Cake || down == Cake + cake) + --down; + printf("%lld ", ave - *down > *up - ave ? *up : *down); + sort(Drink, Drink + drink); + up = upper_bound(Drink, Drink + drink, ave); + down = lower_bound(Drink, Drink + drink, ave); + if (up == Drink + drink) + --up; + if (*down != ave && down != Drink || down == Drink + drink) + --down; + printf("%lld\n", ave - *down > *up - ave ? *up : *down); + } + + return 0; +} diff --git a/1197.cpp b/1197.cpp new file mode 100644 index 0000000..61ddeaa --- /dev/null +++ b/1197.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int rep[30010], tam[30010], n, m, a, b; + +int find(int a) +{ + if (a != rep[a]) + { + return rep[a] = find(rep[a]); + } + return a; +} + +int uniao(int a, int b) +{ + int pai1 = find(a); + int pai2 = find(b); + if (pai1 == pai2) + { + return 0; + } + if (tam[pai1] < tam[pai2]) + { + rep[pai1] = pai2; + tam[pai2] += tam[pai1]; + } + else + { + rep[pai2] = pai1; + tam[pai1] += tam[pai2]; + } +} + +int read() +{ + scanf("%d%d", &n, &m); + if (!n && !m) + { + return 0; + } + for (int i = 0; i < n; i++) + { + rep[i] = i; + tam[i] = 1; + } + int t; + for (int i = 0; i < m; i++) + { + scanf("%d", &t); + if (t) + { + scanf("%d", &a); + } + for (int i = 1; i < t; i++) + { + scanf("%d", &b); + uniao(a, b); + } + } + printf("%d\n", tam[find(0)]); + return 1; +} + +int main() +{ + while (read()) + ; + return 0; +} diff --git a/11970.cpp b/11970.cpp new file mode 100644 index 0000000..a086a94 --- /dev/null +++ b/11970.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int LIMIT = 1000000000; + +int main() +{ + int t, n, x, tmp; + vector squares; + map sq; + for (int i = 1, sz = sqrt(LIMIT); i <= sz; i++) + { + squares.push_back(i * i); + sq[i * i] = i; + } + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n; + cout << "Case " << casenum + 1 << ":"; + for (int i = squares.size() - 1; i >= 0; i--) + { + if (squares[i] >= n) + { + continue; + } + x = n - squares[i]; + tmp = x / sq[squares[i]]; + if (x == tmp * sq[squares[i]]) + { + cout << " " << x; + } + } + cout << endl; + } + return 0; +} diff --git a/11971.cpp b/11971.cpp new file mode 100644 index 0000000..abf52b3 --- /dev/null +++ b/11971.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +typedef long long LL; +int n, k; + +LL pow2(int x) +{ + if (x == 0) + { + return 1; + } + LL tmp = pow2(x / 2); + tmp *= tmp; + if (x & 1) + { + tmp *= 2; + } + return tmp; +} + +int main(void) +{ + int ncase, v = 0; + scanf("%d", &ncase); + while (ncase--) + { + scanf("%d%d", &n, &k); + if (k == 1) + { + printf("Case #%d: %lld/%lld\n", ++v, 0ll, 1ll); + continue; + } + LL ans = pow2(k) - k - 1; + while ((ans & 1) == 0 && ans) + { + ans /= 2; + k--; + } + printf("Case #%d: %lld/%lld\n", ++v, ans, pow2(k)); + } + return 0; +} diff --git a/11972.cpp b/11972.cpp new file mode 100644 index 0000000..fd3ab48 --- /dev/null +++ b/11972.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define MAXN 128 +struct Node +{ + int x, y; + int cap, flow;// x->y, v + int next; +} edge[10005]; +int e, head[MAXN], prev_[MAXN], record[MAXN]; +int level[MAXN], visited[MAXN]; +void addEdge(int x, int y, int v) +{ + edge[e].x = x, edge[e].y = y, edge[e].cap = v, edge[e].flow = 0; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].flow = 0; + edge[e].next = head[y], head[y] = e++; +} +bool buildLevelGraph(int s, int t) +{ + memset(level, 0, sizeof(level)); + queue Q; + Q.push(s), level[s] = 1; + while (!Q.empty()) + { + int tn = Q.front(); + Q.pop(); + for (int i = head[tn]; i != -1; i = edge[i].next) + { + int y = edge[i].y; + if (edge[i].cap > edge[i].flow && level[y] == 0) + { + level[y] = level[tn] + 1; + Q.push(y); + } + } + } + return level[t] > 0; +} +int constructBlockingFlow(int s, int t) +{ + int ret = 0; + stack stk; + memset(visited, 0, sizeof(visited)); + stk.push(s); + while (!stk.empty()) + { + int now = stk.top(); + if (now != t) + { + for (int i = head[now]; i != -1; i = edge[i].next) + { + int y = edge[i].y; + if (visited[y] || level[y] != level[now] + 1) + continue; + if (edge[i].cap > edge[i].flow) + { + stk.push(y), prev_[y] = now, record[y] = i; + break; + } + } + if (stk.top() == now) + stk.pop(), visited[now] = 1; + } + else + { + int flow = 0x3f3f3f3f, bottleneck; + for (int i = t; i != s; i = prev_[i]) + { + int ri = record[i]; + flow = min(flow, edge[ri].cap - edge[ri].flow); + } + for (int i = t; i != s; i = prev_[i]) + { + int ri = record[i]; + edge[ri].flow += flow; + edge[ri ^ 1].flow -= flow; + if (edge[ri].cap - edge[ri].flow == 0) + bottleneck = prev_[i]; + } + while (!stk.empty() && stk.top() != bottleneck) + stk.pop(); + ret += flow; + } + } + return ret; +} +int maxflowDinic(int s, int t) +{ + int flow = 0; + while (buildLevelGraph(s, t)) + flow += constructBlockingFlow(s, t); + return flow; +} +vector maxflowCut(int s, int t, int n) +{ + buildLevelGraph(s, t); + vector ret; + for (int i = 1; i <= n; i++) + { + for (int j = head[i]; j != -1; j = edge[j].next) + { + if (level[edge[j].x] && !level[edge[j].y] && edge[j].cap > 0 && edge[j].cap == edge[j].flow) + ret.push_back(j); + } + } + return ret; +} +int clearflow() +{ + for (int i = 0; i < e; i++) + edge[i].flow = 0; +} +int main() +{ + int n, m, c; + int x, y, v; + int cases = 0; + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &n, &m, &c); + e = 0; + memset(head, -1, sizeof(head)); + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + addEdge(x, y, 1); + addEdge(y, x, 1); + } + printf("Case %d: ", ++cases); + int f = 0; + for (int i = 1; i <= n; i++) + { + if (i == c) + continue; + clearflow(); + int flow = maxflowDinic(c, i); + if (flow >= 2) + { + if (f) + putchar(' '); + printf("%d", i); + f = 1; + } + } + if (f == 0) + puts("none"); + else + puts(""); + } + return 0; +} diff --git a/11974.cpp b/11974.cpp new file mode 100644 index 0000000..b4e3357 --- /dev/null +++ b/11974.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +int solve(int n, int m, const vector switches[100]) +{ + bitset<15> lamps; + for (int i = 0; i < n; i++) + { + lamps.flip(i); + } + map visited; + pair, int> tmp; + queue, int>> q; + tmp.first = lamps; + tmp.second = 0; + q.push(tmp); + visited[lamps.to_ulong()] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first.to_ulong() == 0) + { + return tmp.second; + } + for (int i = 0; i < m; i++) + { + bitset<15> current = tmp.first; + for (int j = 0, sz = switches[i].size(); j < sz; j++) + { + current.flip(switches[i][j]); + } + if (!visited[current.to_ulong()]) + { + visited[current.to_ulong()] = true; + q.push(make_pair(current, tmp.second + 1)); + } + } + } + return -1; +} + +int main() +{ + int t, n, m, tmp; + cin >> t; + for (int casenum = 0; casenum < t; casenum++) + { + cin >> n >> m; + vector switches[100]; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + cin >> tmp; + if (tmp == 1) + { + switches[i].push_back(j); + } + } + } + int result = solve(n, m, switches); + cout << "Case " << casenum + 1 << ": "; + if (result == -1) + { + cout << "IMPOSSIBLE" << endl; + } + else + { + cout << result << endl; + } + } + return 0; +} diff --git a/11975.cpp b/11975.cpp new file mode 100644 index 0000000..1baef4a --- /dev/null +++ b/11975.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int diag(bool b[5][5]) +{ + int r = 0; + for (int i = 0; i < 5; ++i) + r += b[i][i], r += b[i][4 - i]; + return r; +} + +int main() +{ + int t, ball, tic, all[75], prize[4], N[] = {0, 35, 40, 45, 75}; + scanf("%d", &t); + + for (int n = 0; n < t; ++n) + { + printf("%sCase %d:\n", n ? "\n" : "", n + 1); + scanf("%d %d", &ball, &tic); + for (int i = 0; i < ball; ++i) + scanf("%d", all + i); + scanf("%d %d %d %d", prize, prize + 1, prize + 2, prize + 3); + + for (int s = 0; s < tic; ++s) + { + int won = 0, brd[5][5]; + bool mark[5][5] = {{false}}; + + for (int i = 0; i < 5; ++i) + for (int j = 0; j < 5; ++j) + scanf("%d", &brd[i][j]); + for (int t = 0; t < 4; ++t) + { + for (int i = N[t]; i < N[t + 1] && i < ball; ++i) + for (int p = 0; p < 5; ++p) + for (int q = 0; q < 5; ++q) + if (brd[p][q] == all[i]) + mark[p][q] = true; + if (t == 0 && mark[0][0] && mark[0][4] && mark[4][0] && mark[4][4] || + t == 1 && std::count(mark[2], mark[2] + 5, true) == 5 || + t == 2 && diag(mark) == 10 || + t == 3 && std::count(&mark[0][0], &mark[5][0], true) == 25) + won += prize[t]; + } + printf("%d\n", won); + } + } +} diff --git a/11976.cpp b/11976.cpp new file mode 100644 index 0000000..87ed7c4 --- /dev/null +++ b/11976.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; +void incre(int &y, int &m, int &d) +{ + d = (d == (y % 4 == 0 ? leap[m] : days[m]) ? 1 : d + 1); + if (d == 1) + ++m; + if (m == 13) + { + m = 1; + ++y; + } +} + +int main() +{ + int t, except, y, m, d, by, bm, bd, ey, em, ed; + char type; + scanf("%d", &t); + + for (int i = 1; i <= t; ++i) + { + bool holiday[2026][13][32]; + int work = 0, week = 3; + scanf("%d", &except); + for (int Y = 1975; Y <= 2025; ++Y) + for (int M = 1; M <= 12; ++M) + for (int D = 1; D <= (Y % 4 ? days[M] : leap[M]); ++D) + { + holiday[Y][M][D] = week <= 5 && week >= 1 ? false : true; + week = (week + 1) % 7; + } + for (int i = 0; i < except; ++i) + { + scanf("%d-%d-%d %c\n", &y, &m, &d, &type); + holiday[y][m][d] = type == 'H' ? true : false; + } + scanf("%d-%d-%d %d-%d-%d", &by, &bm, &bd, &ey, &em, &ed); + for (y = by, m = bm, d = bd; y != ey || m != em || d != ed; incre(y, m, d)) + if (!holiday[y][m][d]) + ++work; + if (!holiday[ey][em][ed]) + ++work; + printf("Case %d: %d\n", i, work); + } + + return 0; +} diff --git a/11977.cpp b/11977.cpp new file mode 100644 index 0000000..375ebbb --- /dev/null +++ b/11977.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/119/11977.html +// Runtime: 1.644s +// Tag: Math + +#define N 1000000 +#define MOD 10000019 +#define Square(p) p *p + +bool mark[N]; +vector primeList; + +struct primeFreq +{ + int primeNum; + int freq; +} primeFrequency[N]; + +void sieve() +{ + memset(mark, true, sizeof(mark)); + mark[0] = mark[1] = false; + for (int i = 4; i < N; i += 2) + { + mark[i] = false; + } + int len = sqrt(N); + for (int i = 3; i <= len; i += 2) + { + if (mark[i]) + { + for (int j = i * i; j < N; j += 2 * i) + { + mark[j] = false; + } + } + } + primeList.push_back(2); + for (int i = 3; i < N; i += 2) + { + if (mark[i]) + { + primeList.push_back(i); + } + } + // printf ("%d\n", primeList.size ()); +} + +void calcPrimeFactorFrequencyFactorialN(int n) +{ + int tmp; + for (size_t i = 0; i < primeList.size(); i++) + { + tmp = n; + int total = 0; + while (tmp) + { + total += (tmp / primeList[i]); + tmp /= primeList[i]; + } + primeFrequency[i].primeNum = primeList[i]; + primeFrequency[i].freq = total; + } +} + +long long bigMod(long long b, long long p) +{ + if (p == 0) + { + return 1; + } + if (p % 2 == 0) + { + return Square(bigMod(b, p / 2)) % MOD; + } + return ((b % MOD) * bigMod(b, p - 1)) % MOD; +} + +int main() +{ + sieve(); + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n, t; + scanf("%d %d", &n, &t); + calcPrimeFactorFrequencyFactorialN(n); + int primeLen = (int)primeList.size(); + long long res = 1; + bool resFound = false; + for (int i = 0; i < primeLen; i++) + { + if (primeFrequency[i].freq >= t) + { + resFound = true; + res *= bigMod(primeFrequency[i].primeNum, primeFrequency[i].freq / t); + res %= MOD; + } + } + if (resFound) + { + printf("Case %d: %lld\n", ++cases, res); + } + else + { + printf("Case %d: -1\n", ++cases); + } + } + return 0; +} diff --git a/11978.cpp b/11978.cpp new file mode 100644 index 0000000..a31fcb2 --- /dev/null +++ b/11978.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +const int MaxN = 20010; +const double PI = acos(-1.0); + +struct point +{ + double x, y; + point() {} + point(double x, double y) : x(x), y(y) {} + + void input() + { + scanf("%lf %lf", &x, &y); + } +} p[MaxN], q[MaxN], C; +double R, rat; +int N, pn; + +const double eps = 1e-8; + +int sgn(double x) +{ + return x < -eps ? -1 : x > eps; +} + +point operator-(const point &a, const point &b) +{ + return point(a.x - b.x, a.y - b.y); +} + +double operator*(const point &a, const point &b) +{ + return a.x * b.y - a.y * b.x; +} + +double operator&(const point &a, const point &b) +{ + return a.x * b.x + a.y * b.y; +} + +point operator+(const point &a, const point &b) +{ + return point(a.x + b.x, a.y + b.y); +} + +point operator*(double k, const point &a) +{ + return point(k * a.x, k * a.y); +} + +void Inter(point s, point t) +{ + point v = t - s; + double delta = 4 * (s & v) * (s & v) - 4 * (v & v) * ((s & s) - R * R); + if (sgn(delta) <= 0 || sgn(v & v) == 0) + { + return; + } + double k = (-2 * (s & v) - sqrt(delta)) / 2 / (v & v); + if (sgn(k) > 0 && sgn(k - 1.0) < 0) + { + p[pn++] = s + k * v; + } + k = (-2 * (s & v) + sqrt(delta)) / 2 / (v & v); + if (sgn(k) > 0 && sgn(k - 1.0) < 0) + { + p[pn++] = s + k * v; + } +} + +double cal_area(point s, point t) +{ + if (sgn((0.25 * (s + t) & (s + t)) - R * R) < 0) + { + return 0.5 * s * t; + } + else + { + double ang = atan2(t.y, t.x) - atan2(s.y, s.x); + while (ang > PI) + { + ang -= 2 * PI; + } + while (ang < -PI) + { + ang += 2 * PI; + } + return 0.5 * R * R * ang; + } +} + +double solve() +{ + pn = 0; + for (int i = 0; i < N; i++) + { + p[pn++] = q[i]; + Inter(q[i], q[i + 1]); + } + // puts("debug --- P"); + // for(int i=0; i> T; + for (int cas = 1; cas <= T; cas++) + { + cin >> N; + for (int i = 0; i < N; i++) + { + q[i].input(); + } + C.input(); + for (int i = 0; i < N; i++) + { + q[i] = q[i] - C; + } + cin >> rat; + rat /= 100.0; + q[N] = q[0]; + double sum = 0.0; + for (int i = 0; i < N; i++) + { + sum += q[i] * q[i + 1]; + } + sum /= 2.0; + // cout << "sum " << sum << endl; + // + // R = 5.0; + // cout << solve() << endl; + double low = 0.0, high = 1e4; + for (int tim = 0; tim < 50; tim++) + { + R = (low + high) / 2; + if (sgn(solve() / sum - rat) < 0) + { + low = R; + } + else + { + high = R; + } + } + printf("Case %d: %.0f\n", cas, low); + } + return 0; +} diff --git a/11979.cpp b/11979.cpp index d876ea9..0868f65 100644 --- a/11979.cpp +++ b/11979.cpp @@ -1,56 +1,45 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - scanf("%d", &T); - FOI(t, 1, T){ - int N, M; - int i, j; - scanf("%d%d", &N, &M); - int mat[N][M]; - FOI(i, 0, N-1) - FOI(j, 0, M-1) +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n, m; + scanf("%d %d", &n, &m); + int mat[2000 + 5][10 + 5]; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) scanf("%d", &mat[i][j]); - printf("Case %d: %d\n", t, N * M); + int arr[2000 + 5]; + int cnt = 0; + for (int j = 0; j < m; j++) + { + for (int i = 0; i < n; i++) + arr[i] = mat[i][j]; + sort(arr, arr + n); + for (int i = 0; i < n; i++) + cnt += abs(arr[i] - i); + } + printf("Case %d: %d\n", ++cases, cnt); } return 0; } - diff --git a/11980.cpp b/11980.cpp new file mode 100644 index 0000000..3ab22f6 --- /dev/null +++ b/11980.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +typedef long long LL; + +const LL MOD = 1LL << 30; + +LL n, m; + +int main() +{ + int t, cas = 0; + scanf("%d", &t); + while (t--) + { + LL res1 = 0, res2 = 0; + scanf("%lld %lld", &n, &m); + for (int i = 60; i >= 0; --i) + { + LL test = 1LL << i; + if ((n & test) == 0) + { + res1 |= m & test; + } + else if ((m & test) == 0) + { + res1 |= test; + } + else + { + while (test) + { + res1 |= test, test >>= 1; + } + break; + } + } + LL tmp = 0; + LL all = 1LL << 61; + --all; + for (int i = 60; i >= 0; --i) + { + LL test = 1LL << i; + if (res1 & test) + { + LL low = 0, upper = 0; + low = n ^ tmp; + upper = n ^ tmp; + LL range = test; + upper |= range - 1; + low &= all - (range - 1); + tmp |= test; + // printf("upper = %lld low = %lld\n", upper, low); + if (upper <= m) + { + continue; + } + if (low <= m) + { + low = m + 1; + } + LL a = upper + low; + LL b = upper - low + 1; + if (a & 1) + { + b >>= 1; + } + else + { + a >>= 1; + } + res2 = (res2 + (a % MOD) * (b % MOD)) % MOD; + } + } + printf("Case %d: %lld %lld\n", ++cas, res1, res2); + } + return 0; +} diff --git a/11981.cpp b/11981.cpp new file mode 100644 index 0000000..bad78c5 --- /dev/null +++ b/11981.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +// BFS + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 100010 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +vector v[N]; +int friends_top[N]; // number of friends at top +int friends_bottom[N];// number of friends at bottom +int out_degree[N]; // number of edges going out +bool vis[N]; // visited node + +void reset() +{ + for (int i = 0; i < N; i++) + { + v[i].clear(); + out_degree[i] = 0; + friends_bottom[i] = 0; + vis[i] = false; + } + friends_top[1] = 0; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n; + scanf("%d", &n); + reset(); + for (int i = 1; i < n; i++) + { + int a, b; + scanf("%d %d", &a, &b); + out_degree[a]++; + v[a].push_back(b); + v[b].push_back(a); + } + queue q; + q.push(1); + vis[1] = true; + while (!q.empty()) + { + int p = q.front(); + q.pop(); + for (size_t i = 0; i < v[p].size(); i++) + { + if (!vis[v[p][i]]) + { + friends_top[v[p][i]] = friends_top[p] + 1; + q.push(v[p][i]); + vis[v[p][i]] = true; + } + } + } + Set(vis, false); + for (int i = 1; i <= n; i++) + if (out_degree[i] == 0) + q.push(i); + while (!q.empty()) + { + int p = q.front(); + q.pop(); + vis[p] = true; + for (size_t i = 0; i < v[p].size(); i++) + { + if (!vis[v[p][i]]) + { + friends_bottom[v[p][i]] += (friends_bottom[p] + 1); + out_degree[v[p][i]]--; + if (out_degree[v[p][i]] == 0) + q.push(v[p][i]); + } + } + } + LL pairs = 0; + for (int i = 1; i <= n; i++) + pairs += (n - (friends_top[i] + friends_bottom[i] + 1)); + printf("Case %d: %d ", ++cases, n - 1); + cout << pairs / 2 << endl; + } + return 0; +} diff --git a/11982.cpp b/11982.cpp new file mode 100644 index 0000000..15237ae --- /dev/null +++ b/11982.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define MOD 1000000007 +#define LL long long + +int T, dp[1010][1010]; +char s[1010]; + +void update(int &x, LL y) +{ + x = (x + y) % MOD; +} + +int main() +{ + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + memset(dp, 0, sizeof(dp)); + scanf("%s", s + 1); + int n = 0; + for (int i = 1; s[i]; ++i) + if (s[i] != 'E') + { + s[++n] = s[i]; + } + dp[0][0] = 1; + for (int i = 0; i < n; ++i) + { + if (s[i + 1] == 'D') + { + for (int j = 0; j <= i; ++j) + if (dp[i][j]) + { + if (j > 0) + { + update(dp[i + 1][j - 1], (LL)dp[i][j] * j * j); + } + update(dp[i + 1][j], (LL)dp[i][j] * j); + } + } + else + { + for (int j = 0; j <= i; ++j) + if (dp[i][j]) + { + update(dp[i + 1][j], (LL)dp[i][j] * j); + update(dp[i + 1][j + 1], dp[i][j]); + } + } + } + // for (int i = 0; i <= n; ++i) printf("%d ", l[i]); puts(""); + // for (int i = 0; i <= n; ++i) { + // for (int j = 0; j <= i; ++j) + // printf("%d ",dp[i][j]); + // puts(""); + // } + printf("Case %d: %d\n", cas, int(dp[n][0])); + } + return 0; +} diff --git a/11983.cpp b/11983.cpp new file mode 100644 index 0000000..4e6aed5 --- /dev/null +++ b/11983.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +#define MAXK 11 +#define MAXD 60010 +int N, K, M, len[4 * MAXD][MAXK], cnt[4 * MAXD], ty[MAXD]; +struct Seg +{ + int x, y1, y2, col; +} seg[MAXD]; +int cmpint(const void *_p, const void *_q) +{ + int *p = (int *)_p, *q = (int *)_q; + return *p < *q ? -1 : 1; +} +int cmps(const void *_p, const void *_q) +{ + Seg *p = (Seg *)_p, *q = (Seg *)_q; + return p->x < q->x ? -1 : 1; +} +void build(int cur, int x, int y) +{ + int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; + memset(len[cur], 0, sizeof(len[cur])); + len[cur][0] = ty[y + 1] - ty[x]; + cnt[cur] = 0; + if (x == y) + { + return; + } + build(ls, x, mid); + build(rs, mid + 1, y); +} +void init() +{ + int i, j, k, x1, y1, x2, y2; + scanf("%d%d", &N, &K); + for (i = 0; i < N; i++) + { + j = i << 1, k = (i << 1) | 1; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + ++x2, ++y2; + seg[j].x = x1, seg[k].x = x2; + seg[j].y1 = seg[k].y1 = y1, seg[j].y2 = seg[k].y2 = y2; + seg[j].col = 1, seg[k].col = -1; + ty[j] = y1, ty[k] = y2; + } + qsort(ty, N << 1, sizeof(ty[0]), cmpint); + M = -1; + for (i = 0; i < (N << 1); i++) + if (i == 0 || ty[i] != ty[i - 1]) + { + ty[++M] = ty[i]; + } + build(1, 0, M - 1); +} +int BS(int x) +{ + int mid, min = 0, max = M + 1; + for (;;) + { + mid = (min + max) >> 1; + if (mid == min) + { + break; + } + if (ty[mid] <= x) + { + min = mid; + } + else + { + max = mid; + } + } + return mid; +} +void update(int cur, int x, int y) +{ + int ls = cur << 1, rs = (cur << 1) | 1; + memset(len[cur], 0, sizeof(len[cur])); + if (cnt[cur] >= K) + { + len[cur][K] = ty[y + 1] - ty[x]; + } + else if (x == y) + { + len[cur][cnt[cur]] = ty[y + 1] - ty[x]; + } + else + { + int i; + for (i = cnt[cur]; i <= K; i++) + { + len[cur][i] += len[ls][i - cnt[cur]] + len[rs][i - cnt[cur]]; + } + for (i = K - cnt[cur] + 1; i <= K; i++) + { + len[cur][K] += len[ls][i] + len[rs][i]; + } + } +} +void refresh(int cur, int x, int y, int s, int t, int c) +{ + int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; + if (x >= s && y <= t) + { + cnt[cur] += c; + update(cur, x, y); + return; + } + if (mid >= s) + { + refresh(ls, x, mid, s, t, c); + } + if (mid + 1 <= t) + { + refresh(rs, mid + 1, y, s, t, c); + } + update(cur, x, y); +} +void solve() +{ + int i, j, k; + long long int ans = 0; + qsort(seg, N << 1, sizeof(seg[0]), cmps); + seg[N << 1].x = seg[(N << 1) - 1].x; + for (i = 0; i < (N << 1); i++) + { + j = BS(seg[i].y1), k = BS(seg[i].y2); + refresh(1, 0, M - 1, j, k - 1, seg[i].col); + ans += (long long int)len[1][K] * (seg[i + 1].x - seg[i].x); + } + printf("%lld\n", ans); +} +int main() +{ + int t, tt; + scanf("%d", &t); + for (tt = 0; tt < t; tt++) + { + init(); + printf("Case %d: ", tt + 1); + solve(); + } + return 0; +} diff --git a/11984.cpp b/11984.cpp index 450bd9c..15e92b3 100644 --- a/11984.cpp +++ b/11984.cpp @@ -1,54 +1,20 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - scanf("%d", &T); - FOI(t, 1, T){ - double C, dF, F; - scanf("%lf%lf", &C, &dF); - F = C * 9.0/5.0 + 32; - F += dF; - C = (F - 32)*5.0/9.0; - printf("Case %d: %.2lf\n", t, C); +int main() +{ + int t; + double c, f; + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> c >> f; + double tmp = 9.0 * c / 5.0 + 32.0; + tmp += f; + c = (tmp - 32) * 5 / 9.0; + cout << "Case " << i + 1 << ": "; + printf("%.2f\n", c); } return 0; } - diff --git a/11985.cpp b/11985.cpp new file mode 100644 index 0000000..920af77 --- /dev/null +++ b/11985.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +const int V = 40010; +const int E = V * 200 * 2; +const int INF = 1 << 29; +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[E], *adj[V], *pre[V], *arc[V]; + int e, n, d[V], q[V], numb[V]; + void Init(int x) + { + n = x; + for (int i = 0; i < n; ++i) + { + adj[i] = NULL; + } + e = 0; + } + void Addedge(int x, int y, int flow) + { + // printf("%d -> %d\n", x, y); + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1]; + adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1]; + adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + { + d[i] = n, numb[i] = 0; + } + d[v] = 0; + ++numb[0]; + q[rear++] = v; + while (front != rear) + { + if (front == r) + { + ++dis, r = rear; + } + v = q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (d[t] == n) + { + d[t] = dis, q[rear++] = t, ++numb[dis]; + } + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + { + pre[i] = NULL, arc[i] = adj[i]; + } + for (i = 0; i < e; ++i) + { + edge[i].flow = edge[i].cap; + } + i = s; + while (d[s] < n) + { + while (arc[i] && (d[i] != d[arc[i]->v] + 1 || !arc[i]->flow)) + { + arc[i] = arc[i]->next; + } + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + { + update = p->flow; + } + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + { + p->flow -= update, p->re->flow += update; + } + i = s; + } + } + else + { + int min = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && min > d[p->v]) + { + min = d[p->v]; + } + if (--numb[d[i]] == 0) + { + return ret; + } + d[i] = min + 1; + ++numb[d[i]]; + arc[i] = adj[i]; + if (i != s) + { + i = pre[i]->re->v; + } + } + } + return ret; + } +}; + +MaxFlow g; + +int prime[10000]; +int tot = 0; +bool used[10000]; + +const int MAX = 500010; + +int in[MAX]; +int cnt[MAX]; +int arr[V]; + +void Init() +{ + int x = 6000; + // printf("%d\n", x*x); + for (int i = 2; i < x; ++i) + if (!used[i]) + { + // printf("%d\n", i); + prime[tot++] = i; + cnt[i] = 1; + for (int j = i * i; j < x; j += i) + { + used[j] = 1; + } + } + // printf("prime[%d] = %d\n", tot-1, prime[tot-1]*prime[tot-1]); + // printf("%d\n", tot); + for (int i = 2; i < MAX; ++i) + { + for (int j = 0; j < tot && prime[j] < i; ++j) + if (i % prime[j] == 0) + { + cnt[i] = cnt[i / prime[j]] + 1; + break; + } + if (!cnt[i]) + { + ++cnt[i]; + } + } + // for (int i = 1; i <= 100; ++i) printf("%d %d\n", i, cnt[i]); + // puts("Yes!"); +} + +int main() +{ + Init(); + int t, n, cas = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < MAX; ++i) + { + in[i] = -1; + } + for (int i = 0; i < n; ++i) + { + scanf("%d", &arr[i]); + in[arr[i]] = i; + } + g.Init(n + 2); + int S = n, T = n + 1; + for (int i = 0; i < n; ++i) + { + int x = arr[i]; + if (cnt[x] & 1) + { + g.Addedge(S, i, 1); + } + else + { + g.Addedge(i, T, 1); + } + bool flag = 0; + for (int j = 0; j < tot && prime[j] <= x; ++j) + { + if (x % prime[j]) + { + continue; + } + flag = 1; + int y = x / prime[j]; + if (in[y] == -1) + { + continue; + } + int v = in[y]; + if (cnt[x] & 1) + { + g.Addedge(i, v, 1); + } + else + { + g.Addedge(v, i, 1); + } + } + if (!flag && in[1] != -1) + { + g.Addedge(i, in[1], 1); + } + } + printf("Case %d: %d\n", ++cas, n - g.Maxflow(S, T)); + } + return 0; +} diff --git a/11986.cpp b/11986.cpp index 880fc13..331fe9e 100644 --- a/11986.cpp +++ b/11986.cpp @@ -1,51 +1,36 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - scanf("%d", &T); - FOI(t, 1, T){ - int64 N; - scanf("%lld", &N); - printf("Case %d: %lld\n", t, (N+1)/3 + 1); +int main() +{ + int t, case_num = 0; + unsigned long long n, precalc[60]; + precalc[0] = 1; + for (int i = 1; i < 60; i++) + { + precalc[i] = precalc[i - 1] * 2; + } + cin >> t; + while (t--) + { + case_num++; + cout << "Case " << case_num << ": "; + cin >> n; + if (n == 0) + { + cout << "0" << endl; + continue; + } + n++; + for (int i = 0; i < 60; i++) + { + if (n <= precalc[i]) + { + cout << i << endl; + break; + } + } } return 0; } - diff --git a/11987.cpp b/11987.cpp new file mode 100644 index 0000000..4c4bd6e --- /dev/null +++ b/11987.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +struct Nodo +{ + int numero; + Nodo *anterior; + Nodo *siguiente; + Nodo(int n) + { + numero = n; + anterior = NULL; + siguiente = NULL; + } + ~Nodo() + { + if (siguiente != NULL) + { + delete siguiente; + } + } +}; +struct lista +{ + Nodo *cabeza; + int suma; + int tam; + lista() + { + cabeza = NULL; + suma = 0; + tam = 0; + } +}; +lista lists[100001]; +lista *where[100001]; +Nodo *nodo[100001]; +int main() +{ + int n, m; + while (scanf("%i %i", &n, &m) != EOF) + { + for (int i = 1; i <= n; i++) + { + where[i] = &lists[i]; + if (lists[i].cabeza != NULL) + { + delete lists[i].cabeza; + } + lists[i].suma = i; + lists[i].tam = 1; + lists[i].cabeza = new Nodo(i); + nodo[i] = lists[i].cabeza; + } + for (int i = 1; i <= m; i++) + { + int o; + scanf("%i", &o); + if (o == 1) + { + int p, q; + scanf("%i %i", &p, &q); + lista *a = where[p]; + lista *b = where[q]; + if (a == b) + { + continue; + } + lista *mayor; + lista *menor; + if (a->tam > b->tam) + { + mayor = a; + menor = b; + } + else + { + mayor = b; + menor = a; + } + Nodo *ultimo = menor->cabeza; + Nodo *primero = menor->cabeza; + Nodo *actual = menor->cabeza; + while (actual != NULL) + { + where[actual->numero] = mayor; + actual = actual->siguiente; + if (actual != NULL) + { + ultimo = actual; + } + } + mayor->suma += menor->suma; + mayor->tam += menor->tam; + ultimo->siguiente = mayor->cabeza; + mayor->cabeza->anterior = ultimo; + mayor->cabeza = primero; + mayor->cabeza->anterior = NULL; + menor->suma = 0; + menor->tam = 0; + menor->cabeza = NULL; + } + else if (o == 2) + { + int p, q; + scanf("%i %i", &p, &q); + lista *li = where[p]; + Nodo *no = nodo[p]; + lista *destino = where[q]; + if (li == destino) + { + continue; + } + if (no == li->cabeza && li->tam == 1) + { + li->cabeza = NULL; + } + else if (no == li->cabeza) + { + li->cabeza = li->cabeza->siguiente; + } + li->tam--; + li->suma -= p; + destino->tam++; + destino->suma += p; + if (no->anterior != NULL) + { + no->anterior->siguiente = no->siguiente; + } + if (no->siguiente != NULL) + { + no->siguiente->anterior = no->anterior; + } + no->anterior = NULL; + no->siguiente = destino->cabeza; + destino->cabeza->anterior = no; + destino->cabeza = no; + where[p] = destino; + } + else + { + int p; + scanf("%i", &p); + lista *li = where[p]; + printf("%i %i\n", li->tam, li->suma); + } + } + } +} diff --git a/11988.cpp b/11988.cpp new file mode 100644 index 0000000..d35a315 --- /dev/null +++ b/11988.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define db(a) cout << #a << " = " << a << endl + +deque deq; +void insert(bool pos, string &s) +{ + if (pos) + { + deq.push_back(s); + } + else + { + deq.push_front(s); + } + s = ""; +} +int main() +{ + string aux; + bool end = true; + char c; + while (c = getchar()) + { + if (c == EOF) + { + break; + } + if (c == '\n') + { + insert(end, aux); + while (deq.size()) + { + printf("%s", deq.front().c_str()); + deq.pop_front(); + } + printf("\n"); + deq.clear(); + aux = ""; + end = true; + } + else if (c == '[') + { + if (aux != "") + { + insert(end, aux); + } + end = false; + } + else if (c == ']') + { + if (aux != "") + { + insert(end, aux); + } + end = true; + } + else + { + aux += c; + } + } + return 0; +} diff --git a/11989.cpp b/11989.cpp new file mode 100644 index 0000000..7684257 --- /dev/null +++ b/11989.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +struct point +{ + double x, y, theta; + bool operator<(const point &temp) const + { + return theta < temp.theta; + } +}; +point queue_[1000000], line, oo, l_now, r_now, now, nw, lw; +set hs; +set::iterator it, itt, ht; + +bool is; +int n, w, h, head, tail, low, high, mid, vv, vf, l_id; +double now_area, theta, eps = 1e-9; + +double cross(double x1, double y1, double x2, double y2) +{ + return x1 * y2 - x2 * y1; +} +point inter(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) +{ + double ru1, ru2, A, B; + point res; + A = (y2 - y1) * (x4 - x3) - (x2 - x1) * (y4 - y3); + B = (y3 - y1) * (x4 - x3) - (x3 - x1) * (y4 - y3); + if (fabs(A) < eps) + { + is = false; + return res; + } + ru1 = B / A; + B = (y3 - y1) * (x2 - x1) - (x3 - x1) * (y2 - y1); + ru2 = B / A; + if (ru1 > 1 - eps || ru1 < -eps) + { + is = false; + } + else + { + is = true; + } + res.x = x1 + ru1 * (x2 - x1); + res.y = y1 + ru1 * (y2 - y1); + return res; +} + +int main() +{ + int i, j, s, p, q, id, id1, id2; + double x1, y1, x2, y2, ru, xx, yy; + oo.x = 1123; + oo.y = 1531; + while (scanf("%d%d%d", &n, &w, &h) == 3) + { + head = tail = 500000; + queue_[tail].x = 0; + queue_[tail++].y = 0; + queue_[tail].x = w; + queue_[tail++].y = 0; + queue_[tail].x = w; + queue_[tail++].y = h; + queue_[tail].x = 0; + queue_[tail++].y = h; + hs.clear(); + for (i = head; i < tail; i++) + { + id = i + 1; + if (i == tail - 1) + { + id = head; + } + queue_[i].theta = atan2(queue_[id].y - queue_[i].y, queue_[id].x - queue_[i].x); + } + for (i = head; i < tail; i++) + { + hs.insert(queue_[i]); + } + now_area = w * h; + while (n--) + { + scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); + if (now_area == 0) + { + printf("0\n"); + continue; + } + theta = atan2(y2 - y1, x2 - x1); + now.theta = theta; + it = hs.upper_bound(now); + if (it == hs.end()) + { + it = hs.begin(); + } + if (cross(x1 - it->x, y1 - it->y, x2 - it->x, y2 - it->y) > -eps) + { + printf("%f\n", now_area); + continue; + } + for (itt = it;;) + { + ht = itt; + ht++; + if (ht == hs.end()) + { + ht = hs.begin(); + } + now = inter(itt->x, itt->y, ht->x, ht->y, x1, y1, x2, y2); + if (is) + { + break; + } + itt++; + if (itt == hs.end()) + { + itt = hs.begin(); + } + if (itt == it) + { + break; + } + } + if (!is) + { + now_area = 0; + printf("0\n"); + continue; + } + now.theta = itt->theta; + tail = 0; + for (it = itt;;) + { + ht = it; + queue_[tail++] = *it; + if (ht == hs.begin()) + { + ht = hs.end(); + } + ht--; + nw = inter(ht->x, ht->y, it->x, it->y, x1, y1, x2, y2); + if (!is) + { + nw = *ht; + } + now_area -= 0.5 * fabs(cross(nw.x - now.x, nw.y - now.y, it->x - now.x, it->y - now.y)); + it = ht; + if (is) + { + break; + } + } + nw.theta = theta; + for (i = 0; i < tail; i++) + { + hs.erase(queue_[i]); + } + hs.insert(now); + hs.insert(nw); + printf("%f\n", now_area); + } + } + return 0; +} diff --git a/1199.cpp b/1199.cpp new file mode 100644 index 0000000..228afd6 --- /dev/null +++ b/1199.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int f[105]; +vector stopFloor; + +int chk(int lim, int mx) +{ + int lfloor = lim / 20 + 2;// floor of last person be stopped + int sfloor = 1; // now stopping floor + int scnt = 0; // stopped times + vector buf; + while (lfloor <= mx) + { + while (lfloor <= mx && f[lfloor] == 0) + { + lfloor++; + } + if (scnt * 10 + (lfloor - 1) * 4 > lim) + { + return 0; + } + int nfloor = (lim - 10 * scnt + 20 * lfloor + 4) / 24;//nxt stopping floor + lfloor = (lim - 10 * scnt + 16 * nfloor + 4) / 20 + 1; + sfloor = nfloor; + buf.push_back(nfloor); + scnt++; + } + stopFloor = buf; + return 1; +} + +int main() +{ + int n, i, j, k, x; + while (scanf("%d", &n) == 1 && n) + { + memset(f, 0, sizeof(f)); + int mx = 0; + for (i = 0; i < n; i++) + { + scanf("%d", &x), f[x] = 1; + mx = max(mx, x); + } + int l = 0, r = mx * (14), mid, ret = 0; + while (l <= r) + { + mid = (l + r) / 2; + if (chk(mid, mx)) + { + ret = mid, r = mid - 1; + } + else + { + l = mid + 1; + } + } + printf("%d\n", ret); + printf("%d", stopFloor.size()); + for (i = 0; i < stopFloor.size(); i++) + { + printf(" %d", stopFloor[i]); + } + printf("\n"); + } + return 0; +} diff --git a/11990.cpp b/11990.cpp new file mode 100644 index 0000000..b612059 --- /dev/null +++ b/11990.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +#define LB(x) (x) & (-x) + +const int maxn = 200005; +int as[23][maxn]; +int bs[23][maxn]; +int tmp[maxn], cs[maxn]; +int n, m; +long long tot; +void add(int a[], int x, int v, int n) +{ + for (; x <= n; x += LB(x)) + { + a[x] += v; + } +} +int getsum(int a[], int x, int n = 0) +{ + int v = 0; + for (; x > n; x -= LB(x)) + { + v += a[x]; + } + return v; +} +void build(int rt, int l, int r, int d) +{ + int md = (l + r) >> 1; + for (int i = l; i <= r; ++i) + { + as[d][i] = as[d - 1][i], bs[d][i] = 0; + } + if (l == r) + { + return; + } + build(rt << 1, l, md, d + 1); + build(rt << 1 | 1, md + 1, r, d + 1); + sort(as[d] + l, as[d] + r + 1);//æŽ’åº +} +//二分区间[a,b],得到v所在的ä½ç½® +int bitser(int a, int b, int d, int v) +{ + int l = a, r = b, md; + while (l < r) + { + md = (l + r) >> 1; + if (as[d][md] >= v) + { + r = md; + } + else + { + l = md + 1; + } + } + if (as[d][l] > v) + { + --l; + } + return l; +} +void query(int rt, int l, int r, int L, int R, int v, int d, int f) +{ + int md = (l + r) >> 1; + if (L <= l && r <= R) + { + int k = bitser(l, r, d, v); + int t = getsum(bs[d], k, l - 1);//求[l,r]区间上kä½ç½®å‰æœ‰å¤šå°‘个数删除了 + if (!f) + { + k = r - k; + t = getsum(bs[d], r, l - 1) - t; + } + else + { + k -= l - 1; + } + tot -= k - t; + return; + } + if (l >= r) + { + return; + } + if (L <= md) + { + query(rt << 1, l, md, L, R, v, d + 1, f); + } + if (R > md) + { + query(rt << 1 | 1, md + 1, r, L, R, v, d + 1, f); + } +} +void update(int rt, int l, int r, int L, int v, int d) +{ + int md = (l + r) >> 1; + if (l == r) + { + add(bs[d], l, 1, r); + return; + } + if (l >= r) + { + return; + } + if (L <= md) + { + update(rt << 1, l, md, L, v, d + 1); + } + else + { + update(rt << 1 | 1, md + 1, r, L, v, d + 1); + } + int k = bitser(l, r, d, v); + add(bs[d], k, 1, r); +} +int main() +{ + while (scanf("%d%d", &n, &m) == 2) + { + tot = 0; + for (int i = 0; i <= n + 1; ++i) + { + tmp[i] = 0; + } + for (int i = 1; i <= n; ++i) + { + scanf("%d", &as[0][i]); + cs[as[0][i]] = i; + tot += i - 1 - getsum(tmp, as[0][i]); + add(tmp, as[0][i], 1, n); + } + build(1, 1, n, 1); + int k; + while (m--) + { + scanf("%d", &k); + printf("%lld\n", tot); + if (tot) + { + query(1, 1, n, 1, cs[k] - 1, k, 1, 0); + query(1, 1, n, cs[k] + 1, n, k, 1, 1); + update(1, 1, n, cs[k], k, 1); + } + } + } + return 0; +} diff --git a/11991.cpp b/11991.cpp new file mode 100644 index 0000000..0b233bd --- /dev/null +++ b/11991.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int n, m, v, k; + while (cin >> n >> m) + { + map> array; + for (int i = 1; i <= n; i++) + { + cin >> v; + if (array.find(v) == array.end()) + { + vector tmp; + array[v] = tmp; + } + array[v].push_back(i); + } + for (int i = 0; i < m; i++) + { + cin >> k >> v; + if (array[v].size() < k) + { + cout << 0 << endl; + } + else + { + cout << array[v][k - 1] << endl; + } + } + } + return 0; +} diff --git a/11992.cpp b/11992.cpp new file mode 100644 index 0000000..e93fe85 --- /dev/null +++ b/11992.cpp @@ -0,0 +1,253 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +const double eps = 1e-6; +const double offset = 2000; +const double PI = acos(-1.0); +const int MAXN = 1000005; +const int MAXM = 1 << 20; +const int INF = 0x3f3f3f3f; +const int MOD = 1000000009; + +struct TREE +{ + int left, right; + int sum, minval, maxval; + int add, set; +} tree[MAXN << 2]; + +int n, m, q; + +void update(int p) +{ + tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum; + tree[p].maxval = max(tree[p << 1].maxval, tree[p << 1 | 1].maxval); + tree[p].minval = min(tree[p << 1].minval, tree[p << 1 | 1].minval); +} + +void down(int p) +{ + if (tree[p].set != INF) + { + tree[p << 1].set = tree[p << 1 | 1].set = tree[p].set; + tree[p << 1].add = tree[p << 1 | 1].add = 0; + tree[p << 1].sum = (tree[p << 1].right - tree[p << 1].left + 1) * tree[p].set; + tree[p << 1 | 1].sum = (tree[p << 1 | 1].right - tree[p << 1 | 1].left + 1) * tree[p].set; + tree[p << 1].minval = tree[p << 1 | 1].minval = tree[p << 1].maxval = tree[p << 1 | 1].maxval = tree[p].set; + tree[p].set = INF; + } + if (tree[p].add != 0) + { + tree[p << 1].add += tree[p].add; + tree[p << 1 | 1].add += tree[p].add; + tree[p << 1].sum += tree[p].add * (tree[p << 1].right - tree[p << 1].left + 1); + tree[p << 1 | 1].sum += tree[p].add * (tree[p << 1 | 1].right - tree[p << 1 | 1].left + 1); + tree[p << 1].minval += tree[p].add; + tree[p << 1].maxval += tree[p].add; + tree[p << 1 | 1].minval += tree[p].add; + tree[p << 1 | 1].maxval += tree[p].add; + tree[p].add = 0; + } +} + +void build(int p, int left, int right) +{ + tree[p].left = left; + tree[p].right = right; + tree[p].add = 0; + tree[p].set = INF; + tree[p].sum = tree[p].minval = tree[p].maxval = 0; + if (left == right) + { + return; + } + int mid = (left + right) >> 1; + build(p << 1, left, mid); + build(p << 1 | 1, mid + 1, right); +} + +void add(int p, int left, int right, int val) +{ + if (tree[p].left == left && tree[p].right == right) + { + tree[p].add += val; + tree[p].sum += val * (right - left + 1); + tree[p].minval += val; + tree[p].maxval += val; + return; + } + down(p); + int mid = (tree[p].left + tree[p].right) >> 1; + if (left > mid) + { + add(p << 1 | 1, left, right, val); + } + else if (right <= mid) + { + add(p << 1, left, right, val); + } + else + { + add(p << 1, left, mid, val); + add(p << 1 | 1, mid + 1, right, val); + } + update(p); +} + +void set_(int p, int left, int right, int val) +{ + if (tree[p].left == left && tree[p].right == right) + { + tree[p].add = 0; + tree[p].set = val; + tree[p].sum = val * (right - left + 1); + tree[p].minval = val; + tree[p].maxval = val; + return; + } + down(p); + int mid = (tree[p].left + tree[p].right) >> 1; + if (left > mid) + { + set_(p << 1 | 1, left, right, val); + } + else if (right <= mid) + { + set_(p << 1, left, right, val); + } + else + { + set_(p << 1, left, mid, val); + set_(p << 1 | 1, mid + 1, right, val); + } + update(p); +} + +int Sum(int p, int left, int right) +{ + if (tree[p].left == left && tree[p].right == right) + { + return tree[p].sum; + } + down(p); + int mid = (tree[p].left + tree[p].right) >> 1; + if (right <= mid) + { + return Sum(p << 1, left, right); + } + else if (left > mid) + { + return Sum(p << 1 | 1, left, right); + } + else + { + return Sum(p << 1, left, mid) + Sum(p << 1 | 1, mid + 1, right); + } +} + +int min(int p, int left, int right) +{ + if (tree[p].left == left && tree[p].right == right) + { + return tree[p].minval; + } + down(p); + int mid = (tree[p].left + tree[p].right) >> 1; + if (right <= mid) + { + return min(p << 1, left, right); + } + else if (left > mid) + { + return min(p << 1 | 1, left, right); + } + else + { + return min(min(p << 1, left, mid), min(p << 1 | 1, mid + 1, right)); + } +} + +int max(int p, int left, int right) +{ + if (tree[p].left == left && tree[p].right == right) + { + return tree[p].maxval; + } + down(p); + int mid = (tree[p].left + tree[p].right) >> 1; + if (right <= mid) + { + return max(p << 1, left, right); + } + else if (left > mid) + { + return max(p << 1 | 1, left, right); + } + else + { + return max(max(p << 1, left, mid), max(p << 1 | 1, mid + 1, right)); + } +} + +void solved(int cas) +{ + //scanf ("%d %d", &n, &m); + int len = n * m; + build(1, 1, len); + int x1, y1, x2, y2, op, val; + while (q--) + { + scanf("%d %d %d %d %d", &op, &x1, &y1, &x2, &y2); + x1--, y1--, x2--, y2--; + if (op != 3) + { + scanf("%d", &val); + } + if (op == 1) + { + for (int i = x1; i <= x2; i++) + { + add(1, i * m + y1 + 1, i * m + y2 + 1, val); + } + } + else if (op == 2) + { + for (int i = x1; i <= x2; i++) + { + set_(1, i * m + y1 + 1, i * m + y2 + 1, val); + } + } + else + { + int sum = 0, minval = INF, maxval = -INF; + for (int i = x1; i <= x2; i++) + { + sum += Sum(1, i * m + y1 + 1, i * m + y2 + 1); + minval = min(minval, min(1, i * m + y1 + 1, i * m + y2 + 1)); + maxval = max(maxval, max(1, i * m + y1 + 1, i * m + y2 + 1)); + } + printf("%d %d %d\n", sum, minval, maxval); + } + } +} + +int main() +{ + int cas = 1; + while (scanf("%d %d %d", &n, &m, &q) != EOF) + { + solved(cas++); + } + return 0; + int T = 1; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + solved(i); + } + return 0; +} diff --git a/11993.cpp b/11993.cpp new file mode 100644 index 0000000..4e9d15f --- /dev/null +++ b/11993.cpp @@ -0,0 +1,880 @@ +#include + +using namespace std; + +int n, m, k, iden[1111], num_now, base = 10000; + +struct bignum +{ + int value[1111], num; +}; +bignum ans; + +bignum operator*(bignum a, int x) +{ + int i, j, s, p, q; + bignum res; + res.value[0] = 0; + for (i = 0; i < a.num; i++) + { + res.value[i] += a.value[i] * x; + res.value[i + 1] = res.value[i] / base; + res.value[i] %= base; + } + if (res.value[i] > 0) + { + i++; + } + res.num = i; + return res; +} + +struct pp +{ + int num, lst[202]; + bool operator<(const pp &temp) const + { + for (int i = 0; i < num && i < temp.num; i++) + { + if (lst[i] < temp.lst[i]) + { + return true; + } + if (lst[i] > temp.lst[i]) + { + return false; + } + } + if (num < temp.num) + { + return true; + } + if (num > temp.num) + { + return false; + } + return false; + } + bool operator==(const pp &temp) const + { + if (num != temp.num) + { + return false; + } + for (int i = 0; i < num; i++) + { + if (lst[i] != temp.lst[i]) + { + return false; + } + } + return true; + } +}; +pp sg[1111]; + +struct qq +{ + pp ox[201]; + int num; +}; +qq rs[1001], now[222]; + +map hs; + +int node; + +pp operator*(pp a, pp b) +{ + int i, j, s, p, q; + pp res; + i = j = 0; + res.num = 0; + while (i < a.num && j < b.num) + { + if (a.lst[i] < b.lst[j]) + { + i++; + } + else if (a.lst[i] > b.lst[j]) + { + j++; + } + else + { + res.lst[res.num++] = a.lst[i]; + i++; + j++; + } + } + return res; +} +pp operator+(pp a, pp b) +{ + int i, j, s, p, q, ff; + pp res; + i = j = 0; + res.num = 0; + while (i < a.num || j < b.num) + { + ff = 0; + if ((j >= b.num) || (i < a.num && j < b.num && a.lst[i] < b.lst[j])) + { + ff = -1; + } + else if ((i >= a.num) || (i < a.num && j < b.num && a.lst[i] > b.lst[j])) + { + ff = 1; + } + if (ff == 0) + { + i++; + j++; + } + else if (ff < 0) + { + res.lst[res.num++] = a.lst[i]; + i++; + } + else + { + res.lst[res.num++] = b.lst[j]; + j++; + } + } + return res; +} +struct nod +{ + int adj[1001], deg, flag; + bool rev; +}; +nod tree[1001]; +bool vis[1111]; +void rever(int id) +{ + int i, j, s, p, q; + for (i = 0; i < tree[id].deg; i++) + { + rever(tree[id].adj[i]); + } + for (i = 0; i < tree[id].deg / 2; i++) + { + swap(tree[id].adj[i], tree[id].adj[tree[id].deg - 1 - i]); + } +} +bool judge(pp nw, int id, int flag, int pre) +{ + int i, j, s, p, q, nc = 0, ix[2], l = -1, r = -1, np = 0, dx = -1, nflag, ip, im, nst[221], iden[221]; + int ll = tree[id].deg, rr = -1, lx = tree[id].deg, rx = -1; + pp now, tw; + for (i = 0; i < tree[id].deg; i++) + { + now = nw * sg[tree[id].adj[i]]; + if (now.num > 0 && !(now == sg[tree[id].adj[i]])) + { + nc++; + if (l < 0) + { + l = np; + } + else + { + r = np; + } + } + else if (now.num > 0) + { + if (lx > i) + { + lx = i; + } + if (rx < i) + { + rx = i; + } + } + if (now.num > 0) + { + if (ll > i) + { + ll = i; + } + if (rr < i) + { + rr = i; + } + nst[np++] = tree[id].adj[i]; + if (tree[id].flag) + { + if (dx < 0) + { + dx = i + 1 - np; + } + else if (dx != i + 1 - np) + { + return false; + } + } + } + } + if ((flag && nc > 1) || nc > 2) + { + return false; + } + if (tree[id].flag) + { + if (l == np - 1) + { + swap(l, r); + } + } + if (tree[id].flag && ((l != 0 && l >= 0) || (r != np - 1 && r >= 0))) + { + return false; + } + if (tree[id].flag && flag != 0) + { + if (pre == 0) + { + if (flag > 0) + { + if ((rx == tree[id].deg - 1) || (np == 1 && nc == 1 && nst[0] == tree[id].adj[tree[id].deg - 1])) + { + rever(id); + if (np - nc > 0) + { + swap(lx, rx); + lx = tree[id].deg - 1 - lx; + rx = tree[id].deg - 1 - rx; + } + swap(ll, rr); + ll = tree[id].deg - 1 - ll; + rr = tree[id].deg - 1 - rr; + swap(l, r); + if (l >= 0) + { + l = np - 1 - l; + } + if (r >= 0) + { + r = np - 1 - r; + } + for (i = 0; i < np / 2; i++) + { + swap(nst[i], nst[np - 1 - i]); + } + } + } + else if (flag < 0) + { + if (lx == 0 || (np == 1 && nc == 1 && nst[0] == tree[id].adj[0])) + { + rever(id); + if (np - nc > 0) + { + swap(lx, rx); + lx = tree[id].deg - 1 - lx; + rx = tree[id].deg - 1 - rx; + } + swap(ll, rr); + ll = tree[id].deg - 1 - ll; + rr = tree[id].deg - 1 - rr; + swap(l, r); + if (l >= 0) + { + l = np - 1 - l; + } + if (r >= 0) + { + r = np - 1 - r; + } + for (i = 0; i < np / 2; i++) + { + swap(nst[i], nst[np - 1 - i]); + } + } + } + } + if (flag > 0 && lx != 0) + { + if (!(np - nc == 0 && nc == 1 && nst[0] == tree[id].adj[0])) + { + return false; + } + } + if (flag < 0 && rx != tree[id].deg - 1) + { + if (!(np - nc == 0 && nc == 1 && nst[0] == tree[id].adj[tree[id].deg - 1])) + { + return false; + } + } + } + nc = 0; + for (i = 0; i < np; i++) + { + if (i == l || i == r) + { + tw = nw * sg[nst[i]]; + if (tw == nw && flag == 0) + { + nflag = 0; + } + else + { + if (flag != 0) + { + nflag = flag; + } + else if (l == i) + { + nflag = -1; + } + else + { + nflag = 1; + } + } + if (!judge(tw, nst[i], nflag, tree[id].flag)) + { + return false; + } + } + } + if ((flag > 0 && tree[id].flag && l >= 0 && (ll != rr)) || (flag < 0 && tree[id].flag && r >= 0 && (ll != rr))) + { + return false; + } + return true; +} +int insert(pp nw, int id, int flag) +{ + int i, j, s, p, q, nc = 0, ix[2], l = -1, r = -1, np = 0, dx = -1, nflag, ip, im, nst[201]; + int ll = tree[id].deg, rr = -1, lx = -1, rx = -1; + pp now, tw; + for (i = 0; i < tree[id].deg; i++) + { + now = nw * sg[tree[id].adj[i]]; + if (now.num > 0 && !(now == sg[tree[id].adj[i]])) + { + nc++; + if (l < 0) + { + l = np; + } + else + { + r = np; + } + } + if (now.num > 0) + { + if (ll > i) + { + ll = i; + } + if (rr < i) + { + rr = i; + } + nst[np++] = tree[id].adj[i]; + } + } + if (tree[id].flag) + { + if (l == np - 1) + { + swap(l, r); + } + } + nc = 0; + for (i = 0; i < np; i++) + { + if (i == l || i == r) + { + tw = nw * sg[nst[i]]; + if (tw == nw && flag == 0) + { + nflag = 0; + } + else + { + if (flag != 0) + { + nflag = flag; + } + else if (l == i) + { + nflag = -1; + } + else + { + nflag = 1; + } + } + ix[nc++] = insert(tw, nst[i], nflag); + if (i == l) + { + lx = ix[nc - 1]; + } + if (i == r) + { + rx = ix[nc - 1]; + } + } + } + now.num = 0; + for (i = 0; i < np; i++) + { + if (l != i && r != i) + { + now = now + sg[nst[i]]; + } + } + if (!hs.count(now)) + { + hs[now] = node; + while (node >= 1000) + { + puts("orz"); + } + sg[node++] = now; + ip = node - 1; + } + else + { + ip = hs[now]; + } + if (now.num > 0) + { + for (i = 0; i < np; i++) + { + if (l != i && r != i) + { + if (ip == nst[i]) + { + break; + } + } + } + if (i >= np) + { + tree[ip].deg = 0; + tree[ip].flag = tree[id].flag; + for (i = 0; i < np; i++) + { + if (l != i && r != i) + { + if (sg[nst[i]].num > 0) + { + tree[ip].adj[tree[ip].deg++] = nst[i]; + } + } + } + } + } + for (i = 0; i < nc; i++) + { + now = now + sg[ix[i]]; + } + if (!hs.count(now)) + { + hs[now] = node; + while (node >= 1000) + { + puts("Orz"); + } + sg[node++] = now; + im = node - 1; + } + else + { + im = hs[now]; + } + if (im != ip) + { + bool go = true; + if (nc > 0 && im == ix[0]) + { + go = false; + } + if (nc > 1 && im == ix[1]) + { + go = false; + } + if (go) + { + tree[im].deg = 0; + if (lx >= 0 && im != lx) + { + tree[im].adj[tree[im].deg++] = lx; + } + if (sg[ip].num > 0) + { + tree[im].adj[tree[im].deg++] = ip; + } + if (rx >= 0 && im != rx) + { + tree[im].adj[tree[im].deg++] = rx; + } + tree[im].flag = tree[id].flag; + if (flag != 0 || (nc > 0 && np > 1)) + { + tree[im].flag = 1; + } + } + } + if (im == id) + { + return id; + } + if (flag == 0 || tree[id].flag) + { + if (tree[id].flag) + { + int ndeg = 0; + for (i = ll + 1; i + rr - ll < tree[id].deg; i++) + { + tree[id].adj[i] = tree[id].adj[i + rr - ll]; + } + tree[id].deg = i; + tree[id].adj[ll] = im; + } + else + { + int ndeg = 0; + for (i = 0; i < tree[id].deg; i++) + { + vis[tree[id].adj[i]] = false; + } + for (i = 0; i < np; i++) + { + vis[nst[i]] = true; + } + for (i = 0; i < tree[id].deg; i++) + { + if (!vis[tree[id].adj[i]]) + { + tree[id].adj[ndeg++] = tree[id].adj[i]; + } + } + if (sg[im].num != 0) + { + tree[id].adj[ndeg++] = im; + } + tree[id].deg = ndeg; + } + } + else + { + for (i = 0; i < tree[id].deg; i++) + { + vis[tree[id].adj[i]] = false; + } + for (i = 0; i < np; i++) + { + vis[nst[i]] = true; + } + now.num = 0; + for (i = 0; i < tree[id].deg; i++) + { + if (!vis[tree[id].adj[i]]) + { + now = now + sg[tree[id].adj[i]]; + } + } + if (!hs.count(now)) + { + hs[now] = node; + while (node >= 1000) + { + puts("orz"); + } + sg[node++] = now; + ip = node - 1; + } + else + { + ip = hs[now]; + } + if (now.num > 0) + { + tree[ip].flag = false; + for (i = 0; i < tree[id].deg; i++) + { + if (!vis[tree[id].adj[i]] && (ip == tree[id].adj[i])) + { + break; + } + } + if (i >= tree[id].deg) + { + tree[ip].deg = 0; + for (i = 0; i < tree[id].deg; i++) + { + if (!vis[tree[id].adj[i]]) + { + tree[ip].adj[tree[ip].deg++] = tree[id].adj[i]; + } + } + } + } + if (ip == id) + { + return id; + } + tree[id].deg = 2; + tree[id].flag = true; + if (flag < 0) + { + tree[id].adj[0] = ip; + tree[id].adj[1] = im; + } + else + { + tree[id].adj[0] = im; + tree[id].adj[1] = ip; + } + } + return id; +} +void dfs(int id) +{ + int i, j, s, p, q, ip; + vis[id] = true; + if (!tree[id].flag) + { + for (i = 2; i <= tree[id].deg; i++) + { + ans = ans * i; + } + } + for (i = 0; i < tree[id].deg; i++) + { + ip = tree[id].adj[i]; + while (vis[ip]) + { + puts("orz"); + } + dfs(tree[id].adj[i]); + if (!tree[id].flag && tree[ip].flag) + { + ans = ans * 2; + } + } +} +void tri(int iden, int id, int up, int flag) +{ + int i, j, s, p, q, ip, nx; + if (id >= up) + { + for (i = 0; i < now[id].num; i++) + { + rs[iden].ox[rs[iden].num++] = now[id].ox[i]; + } + return; + } + for (i = 0; i < up; i++) + { + if (!vis[i]) + { + vis[i] = true; + ip = tree[iden].adj[i]; + nx = 0; + for (p = 0; p < now[id].num; p++) + for (q = 0; q < rs[ip].num; q++) + { + now[id + 1].ox[nx] = now[id].ox[p]; + for (j = 0; j < rs[ip].ox[q].num; j++) + { + now[id + 1].ox[nx].lst[now[id + 1].ox[nx].num++] = rs[ip].ox[q].lst[j]; + } + nx++; + } + now[id + 1].num = nx; + tri(iden, id + 1, up, flag); + vis[i] = false; + if (flag) + { + return; + } + } + } +} +void display(int id) +{ + int i, j, s, p, q, ip, nu; + for (i = 0; i < tree[id].deg; i++) + { + ip = tree[id].adj[i]; + display(ip); + if (!tree[id].flag && tree[ip].flag) + { + nu = rs[ip].num; + for (j = 0; j < rs[ip].num; j++) + { + rs[ip].ox[nu] = rs[ip].ox[j]; + int nx = rs[ip].ox[nu].num; + for (s = 0; s < rs[ip].ox[nu].num / 2; s++) + { + swap(rs[ip].ox[nu].lst[s], rs[ip].ox[nu].lst[nx - s - 1]); + } + nu++; + } + rs[ip].num = nu; + } + vis[i] = false; + } + if (tree[id].deg == 0) + { + rs[id].num = 1; + rs[id].ox[0] = sg[id]; + } + else + { + rs[id].num = 0; + now[0].num = 1; + now[0].ox[0].num = 0; + tri(id, 0, tree[id].deg, tree[id].flag); + } +} +void out(bignum xy) +{ + int i; + for (i = xy.num - 1; i >= 0; i--) + { + if (i == xy.num - 1) + { + printf("%d", xy.value[i]); + } + else + { + printf("%04d", xy.value[i]); + } + } + printf("\n"); +} + +int main() +{ + int i, j, s, p, q, vv, id; + pp nw; + while (scanf("%d%d%d", &n, &m, &k) == 3) + { + hs.clear(); + node = 0; + for (i = 0; i < n; i++) + { + nw.num = 1; + nw.lst[0] = i; + hs[nw] = i; + tree[i].deg = tree[i].flag = tree[i].rev = 0; + sg[node++] = nw; + } + nw.num = n; + for (i = 0; i < n; i++) + { + nw.lst[i] = i; + } + hs[nw] = n; + sg[node++] = nw; + nw.num = 0; + hs[nw] = node; + sg[node++] = nw; + tree[n].deg = n; + tree[n].flag = tree[n].rev = 0; + for (i = 0; i < n; i++) + { + tree[n].adj[i] = i; + } + for (i = 0; i < m; i++) + { + nw.num = 0; + while (scanf("%d", &vv) && vv != 0) + { + nw.lst[nw.num++] = vv - 1; + } + sort(nw.lst, nw.lst + nw.num); + int nr = 0; + for (j = 0; j < nw.num; j++) + { + if (nr == 0 || nw.lst[nr - 1] < nw.lst[j]) + { + nw.lst[nr++] = nw.lst[j]; + } + } + nw.num = nr; + if (nw.num == 0) + { + id = n; + } + else if (!judge(nw, n, 0, 0)) + { + id = -1; + } + else + { + id = insert(nw, n, 0); + } + if (id < 0) + { + printf("0\n"); + } + else + { + ans.num = 1; + ans.value[0] = 1; + memset(vis, false, sizeof(vis)); + dfs(id); + if (tree[id].flag) + { + ans = ans * 2; + } + out(ans); + if (ans.num == 1 && ans.value[0] <= k) + { + display(id); + if (tree[id].flag) + { + int nu = 0, ip = id; + nu = rs[ip].num; + for (j = 0; j < rs[ip].num; j++) + { + rs[ip].ox[nu] = rs[ip].ox[j]; + int nx = rs[ip].ox[nu].num; + for (s = 0; s < rs[ip].ox[nu].num / 2; s++) + { + swap(rs[ip].ox[nu].lst[s], rs[ip].ox[nu].lst[nx - s - 1]); + } + nu++; + } + rs[ip].num = nu; + } + sort(rs[id].ox, rs[id].ox + rs[id].num); + while (rs[id].num != ans.value[0]) + { + puts("orz"); + } + for (s = 0; s < rs[id].num; s++) + { + while (rs[id].ox[s].num != n) + { + puts("orz"); + } + for (j = 0; j < rs[id].ox[s].num; j++) + { + if (j > 0) + { + putchar(' '); + } + printf("%d", rs[id].ox[s].lst[j] + 1); + } + printf("\n"); + } + } + } + } + } + return 0; +} diff --git a/11994.cpp b/11994.cpp new file mode 100644 index 0000000..f0c2eaa --- /dev/null +++ b/11994.cpp @@ -0,0 +1,538 @@ +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < int(n); ++i) +#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i) +#define DWN(i, b, a) for (int i = int(b - 1); i >= int(a); --i) +#define REP_1(i, n) for (int i = 1; i <= int(n); ++i) +#define FOR_1(i, a, b) for (int i = int(a); i <= int(b); ++i) +#define DWN_1(i, b, a) for (int i = int(b); i >= int(a); --i) +#define REP_C(i, n) for (int n____ = int(n), i = 0; i < n____; ++i) +#define FOR_C(i, a, b) for (int b____ = int(b), i = a; i < b____; ++i) +#define DWN_C(i, b, a) for (int a____ = int(a), i = b - 1; i >= a____; --i) +#define REP_N(i, n) for (i = 0; i < int(n); ++i) +#define FOR_N(i, a, b) for (i = int(a); i < int(b); ++i) +#define DWN_N(i, b, a) for (i = int(b - 1); i >= int(a); --i) +#define REP_1_C(i, n) for (int n____ = int(n), i = 1; i <= n____; ++i) +#define FOR_1_C(i, a, b) for (int b____ = int(b), i = a; i <= b____; ++i) +#define DWN_1_C(i, b, a) for (int a____ = int(a), i = b; i >= a____; --i) +#define REP_1_N(i, n) for (i = 1; i <= int(n); ++i) +#define FOR_1_N(i, a, b) for (i = int(a); i <= int(b); ++i) +#define DWN_1_N(i, b, a) for (i = int(b); i >= int(a); --i) +#define REP_C_N(i, n) for (n____ = int(n), i = 0; i < n____; ++i) +#define FOR_C_N(i, a, b) for (b____ = int(b), i = a; i < b____; ++i) +#define DWN_C_N(i, b, a) for (a____ = int(a), i = b - 1; i >= a____; --i) +#define REP_1_C_N(i, n) for (n____ = int(n), i = 1; i <= n____; ++i) +#define FOR_1_C_N(i, a, b) for (b____ = int(b), i = a; i <= b____; ++i) +#define DWN_1_C_N(i, b, a) for (a____ = int(a), i = b; i >= a____; --i) + +#define DO(n) while (n--) +#define DO_C(n) \ + int n____ = n; \ + while (n____--) +#define TO(i, a, b) \ + int s_ = a < b ? 1 : -1, b_ = b + s_; \ + for (int i = a; i != b_; i += s_) +#define TO_1(i, a, b) \ + int s_ = a < b ? 1 : -1, b_ = b; \ + for (int i = a; i != b_; i += s_) +#define SQZ(i, j, a, b) for (int i = int(a), j = int(b) - 1; i < j; ++i, --j) +#define SQZ_1(i, j, a, b) for (int i = int(a), j = int(b); i <= j; ++i, --j) +#define REP_2(i, j, n, m) REP(i, n) \ + REP(j, m) +#define REP_2_1(i, j, n, m) REP_1(i, n) \ + REP_1(j, m) + +#define ALL(A) A.begin(), A.end() +#define LLA(A) A.rbegin(), A.rend() +#define CPY(A, B) memcpy(A, B, sizeof(A)) +#define INS(A, P, B) A.insert(A.begin() + P, B) +#define ERS(A, P) A.erase(A.begin() + P) +#define BSC(A, X) find(ALL(A), X)// != A.end() +#define CTN(T, x) (T.find(x) != T.end()) +#define SZ(A) int(A.size()) +#define PB push_back +#define MP(A, B) make_pair(A, B) + +#define Rush \ + int T____; \ + RD(T____); \ + DO(T____) +#pragma comment(linker, "/STACK:36777216") +#pragma GCC optimize("O2") +#define Ruby system("ruby main.rb") +#define Haskell system("runghc main.hs") +#define Pascal system("fpc main.pas") + +/** I/O Accelerator **/ + +/* ... :" We are I/O Accelerator ... Use us at your own risk;) ... " .. */ + +template +inline void RD(T &); +template +inline void OT(const T &); + +inline int RD() +{ + int x; + RD(x); + return x; +} +template +inline T &_RD(T &x) +{ + RD(x); + return x; +} + +template +inline void RD(T0 &x0, T1 &x1) +{ + RD(x0) + , RD(x1); +} +template +inline void RD(T0 &x0, T1 &x1, T2 &x2) +{ + RD(x0) + , RD(x1), RD(x2); +} +template +inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) +{ + RD(x0) + , RD(x1), RD(x2), RD(x3); +} +template +inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) +{ + RD(x0) + , RD(x1), RD(x2), RD(x3), RD(x4); +} +template +inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) +{ + RD(x0) + , RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); +} +template +inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) +{ + RD(x0) + , RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); +} +template +inline void OT(T0 &x0, T1 &x1) +{ + OT(x0) + , OT(x1); +} +template +inline void OT(T0 &x0, T1 &x1, T2 &x2) +{ + OT(x0) + , OT(x1), OT(x2); +} +template +inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3) +{ + OT(x0) + , OT(x1), OT(x2), OT(x3); +} +template +inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) +{ + OT(x0) + , OT(x1), OT(x2), OT(x3), OT(x4); +} +template +inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) +{ + OT(x0) + , OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); +} +template +inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) +{ + OT(x0) + , OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); +} + +template +inline void RST(T &A) +{ + memset(A, 0, sizeof(A)); +} +template +inline void RST(T0 &A0, T1 &A1) +{ + RST(A0) + , RST(A1); +} +template +inline void RST(T0 &A0, T1 &A1, T2 &A2) +{ + RST(A0) + , RST(A1), RST(A2); +} +template +inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) +{ + RST(A0) + , RST(A1), RST(A2), RST(A3); +} +template +inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) +{ + RST(A0) + , RST(A1), RST(A2), RST(A3), RST(A4); +} +template +inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) +{ + RST(A0) + , RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); +} +template +inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) +{ + RST(A0) + , RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); +} + +/** Add - On **/ + +// <<= ` 0. Daily Use ., + +template +inline void checkMin(T &a, const T b) +{ + if (b < a) + { + a = b; + } +} +template +inline void checkMax(T &a, const T b) +{ + if (b > a) + { + a = b; + } +} +template +inline void checkMin(T &a, const T b, C c) +{ + if (c(b, a)) + { + a = b; + } +} +template +inline void checkMax(T &a, const T b, C c) +{ + if (c(a, b)) + { + a = b; + } +} +template +inline T min(T a, T b, T c) +{ + return min(min(a, b), c); +} +template +inline T max(T a, T b, T c) +{ + return max(max(a, b), c); +} +template +inline T sqr(T a) +{ + return a * a; +} +template +inline T cub(T a) +{ + return a * a * a; +} +int Ceil(int x, int y) +{ + return (x - 1) / y + 1; +} + +// <<= ` 1. Bitwise Operation ., + +inline bool _1(int x, int i) +{ + return x & 1 << i; +} +inline int _1(int i) +{ + return 1 << i; +} +inline int _U(int i) +{ + return _1(i) - 1; +}; + +inline int count_bits(int x) +{ + x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); + x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); + x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4); + x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8); + x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16); + return x; +} + +template +inline T low_bit(T x) +{ + return x & -x; +} + +template +inline T high_bit(T x) +{ + T p = low_bit(x); + while (p != x) + { + x -= p, p = low_bit(x); + } + return p; +} + +// <<= ' 0. I/O Accelerator interface ., + +template +inline void RD(T &x) +{ + //cin >> x; + //scanf("%d", &x); + char c; + for (c = getchar(); c < '0'; c = getchar()) + ; + x = c - '0'; + for (c = getchar(); c >= '0'; c = getchar()) + { + x = x * 10 + c - '0'; + } + //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0'; +} + +inline void OT(int a, int b) +{ + printf("%d %d\n", a, b); +} + +/* .................................................................................................................................. */ + +const int N = 50009; + +int l[N], r[N], p[N], sz[N], color[N], delay[N], mask[N]; +bool rt[N]; +int n, m; + +#define lx l[x] +#define rx r[x] + +inline void Paint(int x, int c) +{ + if (x == 0) + { + return; + } + color[x] = delay[x] = mask[x] = c; +} + +inline void Release(int x) +{ + if (x == 0 || !delay[x]) + { + return; + } + Paint(lx, delay[x]), Paint(rx, delay[x]); + delay[x] = 0; +} + +inline void Update(int x) +{ + sz[x] = sz[lx] + sz[rx] + 1; + mask[x] = color[x] | mask[lx] | mask[rx]; +} + +inline void Set(int l[], int y, int x) +{ + l[y] = x, p[x] = y; +} + +inline void Rotate(int x) +{ + int y = p[x], z = p[y]; + if (!rt[y]) + { + Release(z), Set(y == l[z] ? l : r, z, x); + } + else + { + p[x] = z; + } + Release(y), Release(x); + if (x == l[y]) + { + Set(l, y, rx), Set(r, x, y); + } + else + { + Set(r, y, lx), Set(l, x, y); + } + if (rt[y]) + { + rt[y] = false, rt[x] = true; + } + Update(y); +} + +inline void Splay(int x) +{ + while (!rt[x]) + { + Rotate(x); + } +} + +int Access(int x) +{ + int y = 0; + do + { + Splay(x), Release(x); + rt[rx] = true, rt[rx = y] = false; + Update(x); + x = p[y = x]; + } while (x); + return y; +} + +inline int Root(int x) +{ + for (x = Access(x); lx; x = lx) + ; + return x; +} + +inline int Depth(int x) +{ + return sz[Access(x)]; +} + +inline int Lca(int x, int y) +{ + int lca; + Access(y); + y = 0; + do + { + Splay(x), Release(x); + if (!p[x]) + { + lca = x; + } + rt[rx] = true, rt[rx = y] = false, Update(x); + x = p[y = x]; + } while (x); + return lca; +} + +// Public : + +void Query(int x, int y) +{ + if (x == y || Root(x) != Root(y)) + { + OT(0, 0); + return; + } + Access(y); + y = 0; + do + { + Splay(x), Release(x); + if (!p[x]) + { + OT(sz[rx] + sz[y], count_bits(mask[rx] | mask[y])); + } + rt[rx] = true, rt[rx = y] = false, Update(x); + x = p[y = x]; + } while (x); +} + +void Paint(int x, int y, int c) +{ + if (Root(x) != Root(y)) + { + return; + } + c = _1(c - 1), Access(y); + y = 0; + do + { + Splay(x), Release(x); + if (!p[x]) + { + Paint(rx, c), Paint(y, c); + } + rt[rx] = true, rt[rx = y] = false, Update(x); + x = p[y = x]; + } while (x); +} + +void Link(int x, int y, int c) +{ + if (x == y || Root(x) == Root(y) && Lca(x, y) == x) + { + return; + } + c = _1(c - 1), Access(x), Splay(x), rt[lx] = true; + lx = p[lx] = 0, p[x] = y, Paint(x, c), Update(x); + Access(x); +} + +int main() +{ + while (scanf("%d%d", &n, &m) != EOF) + { + REP_1(i, n) + sz[i] = 1, + rt[i] = true, RD(p[i]); + REP_1(i, n) + color[i] = mask[i] = _1(RD() - 1); + + int op, a, b, c; + DO(m) + { + RD(op); + if (op == 3) + { + RD(a, b) + , Query(a, b); + } + else if (op == 2) + { + RD(a, b, c) + , Paint(a, b, c); + } + else + { + RD(a, b, c) + , Link(a, b, c); + } + } + + RST(p, l, r, delay); + } + return 0; +} diff --git a/11995.cpp b/11995.cpp new file mode 100644 index 0000000..5a2ce5f --- /dev/null +++ b/11995.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +int main() +{ + int n; + int a, b; + while (cin >> n) + { + stack s; + queue q; + priority_queue pq; + bool is_s, is_q, is_pq; + is_s = is_q = is_pq = true; + while (n--) + { + cin >> a >> b; + if (a == 1) + { + if (is_s) + { + s.push(b); + } + if (is_q) + { + q.push(b); + } + if (is_pq) + { + pq.push(b); + } + } + else// a == 2; + { + if (is_s) + { + if (s.empty() || s.top() != b) + { + is_s = false; + } + else + { + s.pop(); + } + } + if (is_q) + { + if (q.empty() || q.front() != b) + { + is_q = false; + } + else + { + q.pop(); + } + } + if (is_pq) + { + if (pq.empty() || pq.top() != b) + { + is_pq = false; + } + else + { + pq.pop(); + } + } + } + } + if (!is_s && !is_q && !is_pq) + { + cout << "impossible" << endl; + } + else if (is_s && !is_q && !is_pq) + { + cout << "stack" << endl; + } + else if (!is_s && is_q && !is_pq) + { + cout << "queue" << endl; + } + else if (!is_s && !is_q && is_pq) + { + cout << "priority queue" << endl; + } + else + { + cout << "not sure" << endl; + } + } + return 0; +} diff --git a/11996.cpp b/11996.cpp new file mode 100644 index 0000000..4fbb209 --- /dev/null +++ b/11996.cpp @@ -0,0 +1,299 @@ +#include + +using namespace std; + +typedef unsigned long long LL; + +struct SPLAY +{ + static const int MaxN = 400050; + static const LL A = 3; + struct Node + { + int sz, ch, rev; + LL hash[2]; + Node *fa, *c[2]; + } buf[MaxN], *N, *nil, *root; + LL h[MaxN]; + int n; + char s[MaxN]; + + Node *NewNode(int k, Node *fa) + { + Node *x = ++N; + x->c[0] = x->c[1] = nil; + x->fa = fa; + x->ch = k; + x->sz = 1; + x->rev = 0; + return x; + } + void Up(Node *x) + { + x->sz = x->c[0]->sz + 1 + x->c[1]->sz; + x->hash[0] = x->c[0]->hash[0] * h[x->c[1]->sz + 1] + x->ch * h[x->c[1]->sz] + x->c[1]->hash[0]; + x->hash[1] = x->c[1]->hash[1] * h[x->c[0]->sz + 1] + x->ch * h[x->c[0]->sz] + x->c[0]->hash[1]; + } + void Rev(Node *x) + { + if (x == nil) + { + return; + } + x->rev ^= 1; + swap(x->c[0], x->c[1]); + swap(x->hash[0], x->hash[1]); + } + void Down(Node *x) + { + if (x->rev) + { + Rev(x->c[0]), Rev(x->c[1]), x->rev = 0; + } + } + Node *build(Node *fa, int L, int R) + { + if (L > R) + { + return nil; + } + int mid = (L + R) / 2; + Node *x = NewNode(s[mid] - '0', fa); + x->c[0] = build(x, L, mid - 1); + x->c[1] = build(x, mid + 1, R); + Up(x); + return x; + } + void init() + { + N = nil = buf; + nil->sz = nil->ch = nil->rev = 0; + nil->hash[0] = nil->hash[1] = 0; + nil->fa = nil->c[0] = nil->c[1] = nil; + h[0] = 1; + for (int i = 1; i < MaxN; i++) + { + h[i] = h[i - 1] * A; + } + scanf("%s", s + 1); + s[0] = '0'; + s[n + 1] = '0'; + n += 2; + root = build(nil, 0, n - 1); + } + + void Rotate(Node *x, int f) + { + Node *y = x->fa; + Down(y); + Down(x); + y->c[f ^ 1] = x->c[f]; + if (x->c[f] != nil) + { + x->c[f]->fa = y; + } + x->fa = y->fa; + if (y->fa != nil) + { + if (y->fa->c[0] == y) + { + y->fa->c[0] = x; + } + else + { + y->fa->c[1] = x; + } + } + y->fa = x; + x->c[f] = y; + Up(y); + } + void Splay(Node *x, Node *f) + { + for (Down(x); x->fa != f;) + { + if (x->fa->fa == f) + { + if (x->fa->c[0] == x) + { + Rotate(x, 1); + } + else + { + Rotate(x, 0); + } + } + else + { + Node *y = x->fa, *z = y->fa; + if (z->c[0] == y) + { + if (y->c[0] == x) + { + Rotate(y, 1), Rotate(x, 1); + } + else + { + Rotate(x, 0), Rotate(x, 1); + } + } + else + { + if (y->c[1] == x) + { + Rotate(y, 0), Rotate(x, 0); + } + else + { + Rotate(x, 1), Rotate(x, 0); + } + } + } + } + Up(x); + if (f == nil) + { + root = x; + } + } + + void Select(int k, Node *f) + { + Node *x = root; + while (x != nil) + { + Down(x); + int tmp = x->c[0]->sz + 1; + if (k == tmp) + { + break; + } + else if (k > tmp) + { + k -= tmp, x = x->c[1]; + } + else + { + x = x->c[0]; + } + } + Splay(x, f); + } + void Insert(int p, int c) + { + p++; + n++; + Select(p, nil); + Select(p + 1, root); + root->c[1]->c[0] = NewNode(c, root->c[1]); + Splay(root->c[1]->c[0], nil); + } + void Delete(int p) + { + p++; + n--; + Select(p - 1, nil); + Select(p + 1, root); + root->c[1]->c[0] = nil; + Splay(root->c[1], nil); + } + void Reverse(int L, int R) + { + L++; + R++; + Select(L - 1, nil); + Select(R + 1, root); + Rev(root->c[1]->c[0]); + Splay(root->c[1], nil); + } + LL Hash(int L, int R) + { + Select(L - 1, nil); + Select(R + 1, root); + return root->c[1]->c[0]->hash[0]; + } + bool check(int p1, int p2, int L) + { + LL x1 = Hash(p1, p1 + L - 1); + LL x2 = Hash(p2, p2 + L - 1); + // printf("%d %d %d %I64u %I64u %d\n",p1,p2,L,x1,x2,n); + return x1 == x2; + } + int LCP(int p1, int p2) + { + p1++; + p2++; + int lo = 0, hi = min(n - p1, n - p2) + 1; + while (lo + 1 < hi) + { + int mid = (lo + hi) / 2; + if (check(p1, p2, mid)) + { + lo = mid; + } + else + { + hi = mid; + } + } + return lo; + } + int ax[MaxN], m; + void dfs(Node *x) + { + if (x == nil) + { + return; + } + Down(x); + dfs(x->c[0]); + ax[m++] = x->ch; + dfs(x->c[1]); + } + void Print() + { + m = 0; + dfs(root); + for (int i = 0; i < m; i++) + { + printf("%d", ax[i]); + } + puts(""); + } +} splay; + +int main() +{ + int m; + while (scanf("%d%d", &splay.n, &m) == 2) + { + splay.init(); + for (int i = 0; i < m; i++) + { + int t, x, y; + scanf("%d", &t); + if (t == 1) + { + scanf("%d%d", &x, &y); + splay.Insert(x, y); + } + else if (t == 2) + { + scanf("%d", &x); + splay.Delete(x); + } + else if (t == 3) + { + scanf("%d%d", &x, &y); + splay.Reverse(x, y); + } + else + { + scanf("%d%d", &x, &y); + printf("%d\n", splay.LCP(x, y)); + } + // splay.Print(); + } + } + return 0; +} diff --git a/11997.cpp b/11997.cpp new file mode 100644 index 0000000..d650fb7 --- /dev/null +++ b/11997.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +struct Entrada +{ + int suma; + int fila; + Entrada(int s, int f) + { + suma = s; + fila = f; + } + const bool operator<(const Entrada o) const + { + return suma == o.suma ? fila < o.fila : suma > o.suma; + } +}; +vector vect[800]; +int pos[800]; +priority_queue pq; +int main() +{ + int k; + while (scanf("%i", &k) != EOF) + { + while (!pq.empty()) + { + pq.pop(); + } + for (int i = 0; i < k; i++) + { + vect[i].clear(); + for (int j = 0; j < k; j++) + { + int num; + scanf("%i", &num); + vect[i].push_back(num); + } + } + int suma = 0; + for (int i = 0; i < k; i++) + { + sort(vect[i].begin(), vect[i].end()); + int primero = vect[i][0]; + suma += primero; + for (int j = 0; j < k; j++) + { + vect[i][j] -= primero; + } + } + for (int i = 0; i < k; i++) + { + pq.push(Entrada(vect[0][i], 0)); + pos[i] = 0; + } + int cuenta = 0; + int maximo = 0; + for (int i = 0; i < k; i++) + { + maximo = max(maximo, vect[i][1]); + } + while (true) + { + Entrada actual = pq.top(); + pq.pop(); + if (actual.fila == k - 1) + { + if (cuenta == 0) + { + printf("%i", actual.suma + suma); + } + else + { + printf(" %i", actual.suma + suma); + } + cuenta++; + if (cuenta == k) + { + printf("\n"); + break; + } + } + else + { + for (int i = 0; i < k; i++) + { + Entrada siguiente(actual.suma + vect[actual.fila + 1][i], actual.fila + 1); + if (siguiente.suma > maximo) + { + break; + } + pq.push(siguiente); + } + } + } + } +} diff --git a/11999.cpp b/11999.cpp new file mode 100644 index 0000000..d9e5f33 --- /dev/null +++ b/11999.cpp @@ -0,0 +1,197 @@ +#include + +using namespace std; + +const int MAXN = 200; + +bool visit[MAXN][MAXN]; +int n, m, a[MAXN][MAXN], tmp[MAXN * MAXN], tmptmp[MAXN * MAXN], mv[MAXN][MAXN][2], cirId[MAXN][MAXN], totcir, cirDis[MAXN][MAXN], cirLen[MAXN * MAXN], cirMove[MAXN * MAXN]; + +long long ex_gcd(long long a, long long b, long long &x, long long &y) +{ + if (b) + { + long long ret = ex_gcd(b, a % b, x, y), tmp = x; + x = y; + y = tmp - (a / b) * y; + return ret; + } + else + { + x = 1; + y = 0; + return a; + } +} + +long long mod[MAXN * MAXN], ans[MAXN * MAXN]; + +bool solve(long long &m0, long long &a0, int m, int a) +{ + long long y, x; + long long g = ex_gcd(m0, m, x, y); + if (abs(a - a0) % g) + { + return 0; + } + x *= (a - a0) / g; + x %= m / g; + a0 = (x * m0 + a0); + m0 *= m / g; + a0 %= m0; + if (a0 < 0) + { + a0 += m0; + } + return 1; +} + +long long MLES() +{ + bool flag = 1; + long long m0 = 1, a0 = 0; + for (int i = 0; i < totcir; i++) + if (!solve(m0, a0, mod[i], ans[i])) + { + flag = 0; + break; + } + if (flag) + { + return a0; + } + return -1; +} + +int main() +{ + int totCas; + scanf("%d", &totCas); + for (int cas = 1; cas <= totCas; cas++) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + a[i][j] = i * m + j; + } + } + + int cur = 0, dir = 0; + for (int i = 0; i < n + m; i++) + { + if (i == n) + { + continue; + } + int sx, sy; + if (i < n) + { + sx = i; + sy = 0; + } + else + { + sx = n - 1; + sy = i - n; + } + int tmpcur = 0; + while (true) + { + tmptmp[tmpcur++] = a[sx][sy]; + if (sx == 0 || sy == m - 1) + { + break; + } + sx--; + sy++; + } + dir = 1 - dir; + if (dir == 1) + { + for (int j = 0; j < tmpcur; j++) + { + tmp[cur++] = tmptmp[j]; + } + } + else + { + for (int j = tmpcur - 1; j >= 0; j--) + { + tmp[cur++] = tmptmp[j]; + } + } + } + for (int i = 0; i < cur; i++) + { + mv[tmp[i] / m][tmp[i] % m][0] = i / m; + mv[tmp[i] / m][tmp[i] % m][1] = i % m; + } + totcir = 0; + memset(visit, false, sizeof(visit)); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (visit[i][j] == false) + { + int curX = i, curY = j; + cirLen[totcir] = 0; + while (true) + { + visit[curX][curY] = true; + cirId[curX][curY] = totcir; + cirDis[curX][curY] = cirLen[totcir]++; + int nxtX = mv[curX][curY][0]; + int nxtY = mv[curX][curY][1]; + curX = nxtX; + curY = nxtY; + if (visit[curX][curY]) + { + break; + } + } + totcir++; + } + memset(cirMove, -1, sizeof(cirMove)); + bool flag = true; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + int val; + scanf("%d", &val); + val--; + int preX = val / m; + int preY = val % m; + if (cirId[preX][preY] != cirId[i][j]) + { + flag = false; + } + int dis = cirDis[i][j] - cirDis[preX][preY]; + dis = -dis; + if (dis < 0) + { + dis += cirLen[cirId[i][j]]; + } + if (cirMove[cirId[i][j]] == -1) + { + cirMove[cirId[i][j]] = dis; + } + if (cirMove[cirId[i][j]] != dis) + { + flag = false; + } + } + if (!flag) + { + puts("-1"); + continue; + } + for (int i = 0; i < totcir; i++) + { + mod[i] = cirLen[i]; + ans[i] = cirMove[i]; + } + printf("%lld\n", MLES()); + } + return 0; +} diff --git a/120.cpp b/120.cpp new file mode 100644 index 0000000..17b1283 --- /dev/null +++ b/120.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAXN 32 +int St[MAXN], Or[MAXN]; +int N; +char str[10000]; + +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +int Search(int end, int key) +{ + int i; + for (i = 0; i <= end; i++) + { + if (St[i] == key) + { + break; + } + } + return N - i; +} + +int Com[200]; + +void Rev(int ind) +{ + int temp[30], i, j = 0; + for (i = ind; i >= 0; i--) + { + temp[j++] = St[i]; + } + for (i = 0; i <= ind; i++) + { + St[i] = temp[i]; + } +} + +void SolvedCase() +{ + int i, p, c = 0, f = 0; + qsort(Or, N, sizeof(int), com); + for (i = N - 1; i >= 0; i--) + { + if (St[i] == Or[i]) + { + continue; + } + p = Search(i, Or[i]); + if (p != N) + { + Com[c++] = p; + Rev(N - p); + } + Com[c++] = N - i; + Rev(i); + f = 1; + } + if (!f) + { + printf("0\n"); + return; + } + printf("%d", Com[0]); + for (i = 1; i < c; i++) + { + printf(" %d", Com[i]); + } + printf(" 0\n"); +} +void Sep() +{ + char *p; + N = 0; + p = strtok(str, " "); + St[N] = Or[N] = atoi(p); + N++; + while (p) + { + p = strtok(NULL, " "); + if (p) + { + St[N] = Or[N] = atoi(p); + N++; + } + } +} + +int main() +{ + while (gets(str)) + { + Sep(); + printf("%d", Or[0]); + for (int i = 1; i < N; i++) + { + printf(" %d", Or[i]); + } + putchar('\n'); + SolvedCase(); + } + return 0; +} diff --git a/1200.cpp b/1200.cpp new file mode 100644 index 0000000..346b14e --- /dev/null +++ b/1200.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +int main() +{ + int t; + string input; + vector> left, right; + cin >> t; + getline(cin, input);// \n after t + while (t--) + { + getline(cin, input); + int tmp = -1; + bool negative = false; + bool eq_sign = false; + left.clear(); + right.clear(); + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= '0' && input[i] <= '9') + { + if (tmp == -1) + { + tmp = 0; + } + tmp = tmp * 10 + (input[i] - '0'); + } + else if (input[i] == 'x') + { + if (tmp == -1) + { + tmp = 1; + } + if (negative) + { + tmp *= -1; + } + if (!eq_sign) + { + left.push_back(make_pair(tmp, true)); + } + else + { + right.push_back(make_pair(tmp, true)); + } + tmp = -1; + negative = false; + } + else if (input[i] == '=') + { + if (tmp != -1) + { + if (negative) + { + tmp *= -1; + } + if (!eq_sign) + { + left.push_back(make_pair(tmp, false)); + } + else + { + right.push_back(make_pair(tmp, false)); + } + } + tmp = -1; + eq_sign = true; + negative = false; + } + else if (input[i] == '-' || input[i] == '+') + { + if (tmp != -1) + { + if (negative) + { + tmp *= -1; + } + if (!eq_sign) + { + left.push_back(make_pair(tmp, false)); + } + else + { + right.push_back(make_pair(tmp, false)); + } + } + tmp = -1; + if (input[i] == '-') + { + negative = true; + } + else + { + negative = false; + } + } + } + if (tmp != -1) + { + if (negative) + { + tmp *= -1; + } + if (!eq_sign) + { + left.push_back(make_pair(tmp, false)); + } + else + { + right.push_back(make_pair(tmp, false)); + } + } + int left_num = 0, right_num = 0, left_x = 0, right_x = 0; + for (int i = 0, sz = left.size(); i < sz; i++) + { + if (left[i].second) + { + left_x += left[i].first; + } + else + { + left_num += left[i].first; + } + } + for (int i = 0, sz = right.size(); i < sz; i++) + { + if (right[i].second) + { + right_x += right[i].first; + } + else + { + right_num += right[i].first; + } + } + left_x = left_x - right_x; + right_num = right_num - left_num; + if (left_x == 0) + { + if (right_num == 0) + { + cout << "IDENTITY" << endl; + } + else + { + cout << "IMPOSSIBLE" << endl; + } + } + else + { + int result = floor(right_num * 1.0 / left_x); + if (result == 0) + { + result *= 0; + } + cout << result << endl; + } + } + return 0; +} diff --git a/12000.cpp b/12000.cpp new file mode 100644 index 0000000..624a81a --- /dev/null +++ b/12000.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +const int MAXS = 70; +const int MOD = 73405; + +int n, k; +int cnt[MAXS]; +vector vec; +int valid[MAXS]; +int sz; + +void binary(int s) +{ + for (int i = 2 * k - 1; i >= 0; i--) + if (s & (1 << i)) + { + printf("1"); + } + else + { + printf("0"); + } + printf(" "); +} + +struct MAT +{ + long long a[MAXS][MAXS]; + MAT() + { + memset(a, 0, sizeof(a)); + } + void print() + { + printf(" "); + for (int i = 0; i < sz; i++) + { + binary(vec[i]); + } + printf("\n"); + for (int i = 0; i < sz; i++) + { + binary(vec[i]); + for (int j = 0; j < sz; j++) + { + printf("%6lld ", a[i][j]); + } + printf("\n"); + } + puts(""); + } +}; +MAT operator*(const MAT &a, const MAT &b) +{ + MAT c; + for (int k = 0; k < sz; k++) + for (int i = 0; i < sz; i++) + if (a.a[i][k] > 0) + for (int j = 0; j < sz; j++) + { + c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j]) % MOD; + } + return c; +} + +void prepare() +{ + cnt[0] = 0; + for (int s = 1; s < 65; s++) + { + int t = (s - 1) & s; + cnt[s] = cnt[t] + 1; + } +} + +long long quick_pow(const MAT &mat, int n) +{ + MAT s = mat; + MAT ret; + for (int i = 0; i < sz; i++) + { + ret.a[i][i] = 1; + } + while (n > 0) + { + if (n & 1) + { + ret = ret * s; + } + s = s * s; + n /= 2; + } + int st = 0; + for (int i = 0; i < k; i++) + { + st = st * 2 + 1; + } + for (int i = 0; i < k; i++) + { + st *= 2; + } + return ret.a[valid[st]][valid[st]]; +} + +int main() +{ + prepare(); + int test; + scanf("%d", &test); + for (int cas = 1; cas <= test; cas++) + { + MAT mat; + scanf("%d%d", &n, &k); + memset(valid, -1, sizeof(valid)); + vec.clear(); + for (int s = 0; s < (1 << (k * 2)); s++) + { + //printf("cnt[%d] = %d\n",s,cnt[s]); + if (cnt[s] == k) + { + vec.push_back(s); + valid[s] = vec.size() - 1; + } + } + for (int i = 0; i < vec.size(); i++) + { + int s = vec[i]; + int t; + for (int j = 0; j < (k * 2); j++) + if ((s & (1 << j)) == 0) + { + t = s + (1 << j); + t = (t << 1) % (1 << (2 * k)); + /* + printf("judge "); + binary(s); + binary(t); + printf(" j = %d\n",j); + */ + if (valid[t] > -1) + { + mat.a[i][valid[t]] = 1; + } + } + t = ((s << 1) + 1) % (1 << (2 * k)); + //printf("valid[%d] = %d\n",t,valid[t]); + if (valid[t] > -1) + { + mat.a[i][valid[t]] = 1; + } + } + sz = vec.size(); + //printf("sz is %d\n",sz); + //mat.print(); + long long ans = quick_pow(mat, n); + printf("Case %d: ", cas); + cout << ans << endl; + } + return 0; +} diff --git a/12001.cpp b/12001.cpp new file mode 100644 index 0000000..989c5cf --- /dev/null +++ b/12001.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define FORI(i, f, n) for (int i = f; i < n; ++i) +const int M = 50; +int q[M], qs[M]; +int combine3(int n) +{ + return n * (n - 1) * (n - 2) / 6; +} +int main() +{ + for (int m, n; scanf("%d %d", &n, &m) && (n || m);) + { + int ans3 = 0, ans4 = 0, z; + FOR(i, M) + q[i] = qs[i] = 0; + FOR(i, n) + { + scanf("%d", &z); + ++q[--z]; + } + z = 0; + FOR(i, m) + if (q[i]) + { + qs[z] = q[z] = q[i], ++z; + } + m = z; + FOR(i, m) + if (i) + { + qs[i] += qs[i - 1]; + } + /* 3 from same country */ + FOR(i, m) + if (q[i] >= 3) + { + z = combine3(q[i]); + ans3 += z; + /* 4th from a different country */ + ans4 += z * (n - q[i]); + /* 4th from the same country */ + if (q[i] > 3) + { + ans4 += z * (q[i] - 3) / 4; + } + } + /* Three different countries */ + FOR(i, m) + FORI(j, i + 1, m) FORI(k, j + 1, m) + { + z = q[i] * q[j] * q[k]; + if (!z) + { + continue; + } + ans3 += z; + // 4 different countries + ans4 += z * (n - qs[k]); + // 3 different (1 repeats) + ans4 += (z * (q[i] - 1)) / 2; + ans4 += (z * (q[j] - 1)) / 2; + ans4 += (z * (q[k] - 1)) / 2; + } + printf("%d %d\n", ans3, ans4); + } + return 0; +} diff --git a/12002.cpp b/12002.cpp new file mode 100644 index 0000000..e15d788 --- /dev/null +++ b/12002.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define maxn 510 + +int n, dp1[maxn], dp2[maxn], num[maxn]; + +void DP1()//increase +{ + int i, j; + for (i = n; i >= 1; i--) + { + dp1[i] = 1; + for (j = i + 1; j <= n; j++) + { + if (num[j] >= num[i]) + { + dp1[i] = max(dp1[i], dp1[j] + 1); + } + } + } +} + +void DP2()// +{ + int i, j; + for (i = n; i >= 1; i--) + { + dp2[i] = 1; + for (j = i + 1; j <= n; j++) + { + if (num[j] <= num[i]) + { + dp2[i] = max(dp2[i], dp2[j] + 1); + } + } + } +} + +int main() +{ + int i, j; + while (scanf("%d", &n) == 1 && n) + { + for (i = 1; i <= n; i++) + { + scanf("%d", &num[i]); + } + DP1(); + DP2(); + int ans = 0; + for (i = 1; i <= n; i++) + { + ans = max(ans, max(dp1[i], dp2[i])); + for (j = i + 1; j <= n; j++) + { + if (num[i] == num[j]) + { + continue; + } + if (num[i] < num[j]) + { + ans = max(ans, dp1[j] + dp2[i]); + } + else + { + ans = max(ans, dp1[i] + dp2[j]); + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12003.cpp b/12003.cpp new file mode 100644 index 0000000..056022e --- /dev/null +++ b/12003.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define SIZE 550 + +struct ele +{ + int v, idx; +}; +ele piece[SIZE][SIZE]; +int A[SIZE * SIZE]; + +bool cmp(ele a, ele b) +{ + return a.v < b.v; +} + +int query(int l, int r, int v) +{ + static int cnt, lp, rp, m, tl, tr; + cnt = 0; + tl = l; + tr = r; + while (l % SIZE && l <= r) + { + if (A[l] < v) + { + cnt++; + } + l++; + } + while ((r + 1) % SIZE && l <= r) + { + if (A[r] < v) + { + cnt++; + } + r--; + } + if (l > r) + { + return cnt; + } + lp = l / SIZE, rp = r / SIZE, v--; + while (lp <= rp) + { + l = 0, r = SIZE - 1; + while (l < r) + { + m = (l + r + 1) >> 1; + if (piece[lp][m].v <= v) + { + l = m; + } + else + { + r = m - 1; + } + } + if (piece[lp][l].v > v) + { + l--; + } + cnt += l + 1; + lp++; + } + return cnt; +} + +int main() +{ + int n, m, u, l, r, v, p; + int i, j, x, y; + while (scanf("%d %d %d", &n, &m, &u) == 3) + { + for (i = 0, j = (n - 1) / SIZE; i < SIZE; i++) + { + piece[j][i].v = 2147483647, piece[j][i].idx = -1; + } + for (i = 0; i < n; i++) + { + scanf("%d", &A[i]); + piece[i / SIZE][i % SIZE].v = A[i]; + piece[i / SIZE][i % SIZE].idx = i; + } + for (i = (n - 1) / SIZE; i >= 0; i--) + { + sort(piece[i], piece[i] + SIZE, cmp); + } + while (m--) + { + scanf("%d %d %d %d", &l, &r, &v, &p); + l--, r--, p--; + x = query(l, r, v); + y = (int)((long long)x * u / (r - l + 1)); + x = p / SIZE; + for (i = 0; piece[x][i].idx != p; i++) + ; + for (; i < SIZE; i++) + { + piece[x][i].v = piece[x][i + 1].v; + piece[x][i].idx = piece[x][i + 1].idx; + } + for (i = SIZE - 2; i >= 0 && piece[x][i].v > y; i--) + { + piece[x][i + 1].v = piece[x][i].v; + piece[x][i + 1].idx = piece[x][i].idx; + } + piece[x][i + 1].v = y; + piece[x][i + 1].idx = p; + A[p] = y; + } + for (i = 0; i < n; i++) + { + printf("%d\n", A[i]); + } + } + return 0; +} diff --git a/12004.cpp b/12004.cpp new file mode 100644 index 0000000..e238afd --- /dev/null +++ b/12004.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +typedef long long LL; +LL gcd(LL a, LL b) +{ + return a ? gcd(b % a, a) : b; +} +struct frac +{ + LL num, den; + void show() + { + if (den != 1) + { + printf("%lld/%lld\n", num, den); + } + else + { + printf("%lld\n", num); + } + } + void simpler() + { + int g = gcd(num, den); + num /= g; + den /= g; + } +}; +int main() +{ + int t; + scanf("%d", &t); + long long n; + for (int c = 1; t-- && scanf("%lld", &n); ++c) + { + printf("Case %d: ", c); + frac f = {n * (n - 1), 4}; + f.simpler(); + f.show(); + } +} diff --git a/12005.cpp b/12005.cpp new file mode 100644 index 0000000..03943be --- /dev/null +++ b/12005.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +#define maxL (20000000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +#define LL long long +int mark[maxL] = {}; +long long p[2000000], pt = 0; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 20000000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + p[pt++] = i; + } + } +} +long long cntDivisor(long long n) +{ + static long long i, j, t; + j = 1; + for (i = 0; i < pt && p[i] * p[i] <= n; i++) + { + if (n % p[i] == 0) + { + t = 1; + n /= p[i]; + while (n % p[i] == 0) + n /= p[i], t++; + j *= (t + 1); + } + } + if (n != 1) + j *= 2; + return j; +} +int main() +{ + sieve(); + long long c; + while (scanf("%lld", &c) == 1 && c) + { + long long n = c * 4 - 3; + long long cnt; + cnt = cntDivisor(n); + printf("%lld %lld\n", c, cnt); + } + return 0; +} diff --git a/12008.cpp b/12008.cpp new file mode 100644 index 0000000..e64702a --- /dev/null +++ b/12008.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +int ab(int x) +{ + if (x < 0) + { + return -x; + } + return x; +} +int main() +{ + int cas, r, c, sr, sc, dr, dc, cc = 0, u, t, v, d; + scanf("%d", &cas); + while (cas--) + { + scanf("%d%d%d%d%d%d", &r, &c, &sr, &sc, &dr, &dc); + printf("Case %d: ", ++cc); + if (r < c) + { + swap(r, c); + swap(sr, sc), swap(dr, dc); + } + if (ab(sr + dr + sc + dc) % 2) + { + printf("impossible\n"); + } + else if (sr == dr && sc == dc) + { + printf("0\n"); + } + else if (ab(sr - dr) == ab(sc - dc)) + { + printf("1\n"); + } + else if (ab(sr - dr) < ab(sc - dc)) + { + printf("2\n"); + } + else + { + if (c == 1) + { + printf("impossible\n"); + } + else + { + if (sr < dr) + { + d = sc - sr + dr - dc; + t = (d - 1) / (2 * c - 2); + u = (d - 1) % (2 * c - 2); + if (u < 2 * (c - dc)) + { + u = 1; + } + else + { + u = 2; + } + //printf("#%d\n",2*t+u+1); + v = 2 * t + u + 1; + d = dr + dc - sr - sc; + t = (d - 1) / (2 * c - 2); + u = (d - 1) % (2 * c - 2); + if (u < 2 * (dc - 1)) + { + u = 1; + } + else + { + u = 2; + } + //printf("#%d\n",2*t+u+1); + if (v > 2 * t + u + 1) + { + v = 2 * t + u + 1; + } + } + else + { + d = sr - sc + dc - dr; + t = (d - 1) / (2 * c - 2); + u = (d - 1) % (2 * c - 2); + if (u < 2 * (dc - 1)) + { + u = 1; + } + else + { + u = 2; + } + //printf("#%d\n",2*t+u+1); + v = 2 * t + u + 1; + d = sr + sc - dr - dc; + t = (d - 1) / (2 * c - 2); + u = (d - 1) % (2 * c - 2); + if (u < 2 * (c - dc)) + { + u = 1; + } + else + { + u = 2; + } + //printf("#%d\n",2*t+u+1); + if (v > 2 * t + u + 1) + { + v = 2 * t + u + 1; + } + } + printf("%d\n", v); + } + } + } + return 0; +} diff --git a/12009.cpp b/12009.cpp new file mode 100644 index 0000000..7a0cef3 --- /dev/null +++ b/12009.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef long long ll; + +const string a = "1278125400133690086034889084364023875765936821979626181917833520492704199324875237825867148278905344897440142612317035699548419499444610608146207254036559998271588356035049327795540741961849280952093753026852390937562839148571612367351970609224242398777007574955787271559767413458997537695515862718887941516307569668816352155048898271704378508028434084412644126821848514157729916034497017892335796684991447389566001932545827678000618329854426232827257556110733160697015864984222291255485729879337147866323172405515756102352543994999345608083801190741530060056055744818709692785099775918050075416428527708162011350246806058163276171676765260937528056844214486193960499834472806721906670417240094234466197812426690787535944616698508064636137166384049029219341881909581659524477861846140912878298438431703248173428886572737663146519104988029447960814673760503957196893714671801375619055462996814764263903953007319108169802938509890062166509580863811000557423423230896109004106619977392256259918212890625"; +const string b = "8721874599866309913965110915635976124234063178020373818082166479507295800675124762174132851721094655102559857387682964300451580500555389391853792745963440001728411643964950672204459258038150719047906246973147609062437160851428387632648029390775757601222992425044212728440232586541002462304484137281112058483692430331183647844951101728295621491971565915587355873178151485842270083965502982107664203315008552610433998067454172321999381670145573767172742443889266839302984135015777708744514270120662852133676827594484243897647456005000654391916198809258469939943944255181290307214900224081949924583571472291837988649753193941836723828323234739062471943155785513806039500165527193278093329582759905765533802187573309212464055383301491935363862833615950970780658118090418340475522138153859087121701561568296751826571113427262336853480895011970552039185326239496042803106285328198624380944537003185235736096046992680891830197061490109937833490419136188999442576576769103890995893380022607743740081787109376"; + +void output(int n, int flag) +{ + if (flag == 0) + { + REP(i, n, a.size()) + cout << a[i]; + } + else + { + REP(i, n, b.size()) + cout << b[i]; + } +} + +main() +{ + int te; + cin >> te; + rep(tc, te) + { + cout << "Case #" << tc + 1 << ": "; + int n; + cin >> n; + if (n == 1) + { + cout << "0 1 5 6" << endl; + continue; + } + n = a.size() - n; + bool cana = a[n] != '0', canb = b[n] != '0'; + if (!cana && !canb) + { + cout << "Impossible"; + } + else if (!cana) + { + output(n, 1); + } + else if (!canb) + { + output(n, 0); + } + else + { + bool isafirst = false; + for (int i = n; i < a.size(); i++) + { + if (a[i] < b[i]) + { + isafirst = true; + break; + } + else if (a[i] > b[i]) + { + break; + } + } + if (isafirst) + { + output(n, 0); + cout << " "; + output(n, 1); + } + else + { + output(n, 1); + cout << " "; + output(n, 0); + } + } + cout << endl; + } + return false; +} +/* +376 141376 +625 390625 + +9376 87909376 + +90625 8212890625 + +109376 11963109376 +890625 793212890625 + +2890625 8355712890625 +7109376 50543227109376 + +12890625 166168212890625 +87109376 7588043387109376 +*/ diff --git a/1201.cpp b/1201.cpp new file mode 100644 index 0000000..769d8bb --- /dev/null +++ b/1201.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +const int maxn = 550; + +struct Edge +{ + int from, to; + int next; +} edge[maxn * maxn]; + +struct BPM +{ + int n, m; + int head[maxn]; + int left[maxn]; + bool T[maxn]; + int cnt; + + void init(int n) + { + this->n = n; + cnt = 0; + memset(head, -1, sizeof(head)); + } + + void AddEdge(int u, int v) + { + edge[cnt].from = u, edge[cnt].to = v; + edge[cnt].next = head[u]; + head[u] = cnt++; + } + + bool match(int u) + { + for (int i = head[u]; i != -1; i = edge[i].next) + { + int v = edge[i].to; + if (!T[v]) + { + T[v] = true; + if (left[v] == -1 || match(left[v])) + { + left[v] = u; + return true; + } + } + } + return false; + } + + void solve() + { + memset(left, -1, sizeof(left)); + int ans = 0; + for (int u = 1; u <= n; u++) + { + memset(T, 0, sizeof(T)); + if (match(u)) + { + ans++; + } + } + printf("%d\n", n - ans); + } +}; + +int n, t[maxn], dis[maxn], s[maxn][2], e[maxn][2]; +BPM solver; + +bool judge(int i, int j) +{ + int x = abs(e[i][0] - s[j][0]) + abs(e[i][1] - s[j][1]); + if (t[j] > t[i] + dis[i] + x) + { + return 1; + } + return 0; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int i, j; + scanf("%d", &n); + solver.init(n); + for (i = 1; i <= n; i++) + { + int h, m; + int a, b, c, d; + scanf("%d:%d %d %d %d %d", &h, &m, &a, &b, &c, &d); + t[i] = h * 60 + m; + dis[i] = abs(a - c) + abs(b - d); + s[i][0] = a, s[i][1] = b; + e[i][0] = c, e[i][1] = d; + } + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + if (i == j) + continue; + if (judge(i, j)) + solver.AddEdge(i, j); + } + } + solver.solve(); + } + return 0; +} diff --git a/12010.cpp b/12010.cpp new file mode 100644 index 0000000..b83f723 --- /dev/null +++ b/12010.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define mp make_pair +#define pb push_back + +typedef pair pii; + +class st +{ +public: + st *r, *l; + int val, child; + st() : r(NULL), l(NULL){}; + st(int tval) : r(NULL), l(NULL), val(tval), child(0){}; +}; + +void make(st *now, int val) +{ + if (val < now->val) + { + if (now->l == NULL) + { + now->l = new st(val); + } + else + { + make(now->l, val); + } + } + else if (now->val < val) + { + if (now->r == NULL) + { + now->r = new st(val); + } + else + { + make(now->r, val); + } + } + now->child++; +} + +void decide(st *now, bool isleft, int prev, int depth, vector &ret, + vector &r, vector &l) +{ + int cnt = 0; + if (isleft) + { + if (now->r != NULL) + { + cnt = now->r->child + 1; + } + prev -= (cnt + 1); + } + else + { + if (now->l != NULL) + { + cnt = now->l->child + 1; + } + prev += (cnt + 1); + } + ret[now->val].first = depth; + ret[now->val].second = prev; + if (now->r != NULL) + decide(now->r, false, prev, depth + 1, ret, r, l), + r[now->val] = 1; + if (now->l != NULL) + decide(now->l, true, prev, depth + 1, ret, r, l), + l[now->val] = 1; +} + +const int N = 200; +char buf[N][N]; +int num[N][N]; + +void putline(int y, int l, int r) +{ + REP(i, l, r + 1) + buf[y][i] = '-'; +} + +const int dx[] = {1, -1}; +void output(int r, vector *a) +{ + rep(i, N) + { + rep(j, N) + { + if (buf[i][j] == 'o') + { + rep(l, 2) + { + if (a[l][num[i][j]] != 1) + { + continue; + } + for (int k = j + dx[l]; 0 <= k && k < N; k += dx[l]) + { + if (buf[i][k] == '+') + { + putline(i, min(j + dx[l], k), max(j + dx[l], k)); + buf[i][k] = '+'; + break; + } + else if (buf[i][k] != ' ') + { + break; + } + } + } + } + } + } + rep(i, r) + { + int p = 0; + rep(j, N) if (buf[i][j] != ' ') + { + p = j + 1; + } + rep(j, p) cout << buf[i][j]; + cout << endl; + } + /* + rep(j,N){ + int cnt=0; + rep(i,r){ + if (buf[i][j] == 'o')cnt++; + } + if (cnt > 1)assert(false); + } + */ +} + +main() +{ + int te; + cin >> te; + rep(tc, te) + { + cout << "Case #" << tc + 1 << ":" << endl; + rep(i, N) rep(j, N) buf[i][j] = ' ', num[i][j] = -1; + int n; + cin >> n; + int tmp; + cin >> tmp; + st *root = new st(tmp - 1); + REP(i, 1, n) + { + cin >> tmp; + make(root, tmp - 1); + } + vector ans(n); + vector info[2]; + rep(i, 2) rep(j, n) info[i].push_back(0); + ans[root->val].first = 0; + ans[root->val].second = 0; + if (root->r != NULL) + decide(root->r, false, 0, 1, ans, info[0], info[1]), + info[0][root->val] = 1; + if (root->l != NULL) + decide(root->l, true, 0, 1, ans, info[0], info[1]), + info[1][root->val] = 1; + int mini = 0, maxi = 0; + rep(i, (int)ans.size()) mini = min(mini, ans[i].second), + maxi = max(maxi, ans[i].first); + rep(i, (int)ans.size()) ans[i].second -= mini; + rep(i, (int)ans.size()) + { + if (i != root->val) + { + buf[ans[i].first * 2 - 1][ans[i].second] = '|'; + buf[ans[i].first * 2 - 2][ans[i].second] = '+'; + } + buf[ans[i].first * 2][ans[i].second] = 'o'; + num[ans[i].first * 2][ans[i].second] = i; + } + output(maxi * 2 + 1, info); + } + return false; +} diff --git a/12011.cpp b/12011.cpp new file mode 100644 index 0000000..e74177d --- /dev/null +++ b/12011.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define M 18 + +int v[1 << M]; +int has[M + 5], must[M + 5], f[M + 5]; + +int main() +{ + int t, cc = 0, n, i, j, all; + f[0] = 1; + for (i = 1; i <= M; i++) + { + f[i] = f[i - 1] << 1; + } + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + all = f[M] - 1; + for (i = 0; i < n; i++) + { + scanf("%d", v + i), all &= v[i]; + } + int mx = 0; + memset(has, 0, sizeof(has)); + for (i = 0; i < M; i++) + { + int tp = f[M] - 1; + for (j = 0; j < n; j++) + if (f[i] & v[j]) + { + tp &= v[j], has[i] = 1; + } + must[i] = tp; + if (has[i]) + { + mx = i; + } + } + int ret = 0; + for (i = f[mx + 1]; i > 0; i--) + { + for (j = 0; j < M; j++) + if (i & f[j]) + { + if (!has[j]) + { + break; + } + if ((i & must[j]) != must[j]) + { + break; + } + } + if (j >= M) + { + ++ret; + } + } + if (all == 0) + { + ++ret; + } + printf("Case #%d: %d\n", ++cc, ret - n); + } + return 0; +} diff --git a/12012.cpp b/12012.cpp new file mode 100644 index 0000000..ac40a35 --- /dev/null +++ b/12012.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +#define M 1005 + +int main() +{ + char s[M]; + int t, i, j, k, n, a, b, cc = 0, ans[M]; + scanf("%d", &t); + while (t--) + { + scanf("%s", s); + n = strlen(s); + memset(ans, 0, sizeof(ans)); + printf("Case #%d: %d", ++cc, n); + for (i = 0; i < n; i++) + { + for (j = i + 1; j < n; j++) + { + a = i, b = j; + k = 1; + while (b < n) + { + if (s[a] == s[b]) + { + ++a, ++b; + } + else + { + break; + } + if (a == j) + { + a = i; + ++k; + } + } + int tp = (j - i) * k; + if (ans[k] < tp) + { + ans[k] = tp; + } + } + } + for (i = 2; i <= n; i++) + { + printf(" %d", ans[i]); + } + printf("\n"); + } + return 0; +} diff --git a/12013.cpp b/12013.cpp new file mode 100644 index 0000000..75bd714 --- /dev/null +++ b/12013.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +double x, y; + +int main() +{ + int test; + scanf("%d", &test); + for (int ii = 1; ii <= test; ii++) + { + scanf("%lf%lf", &x, &y); + x /= 100.0; + y /= 100.0; + double tmp = 0; + tmp += x * x * x * x; + tmp += 4.0 * x * x * x * x * (1 - x); + tmp += 10.0 * x * x * x * x * (1 - x) * (1 - x); + double tx = x * x / (1.0 - 2.0 * x * (1 - x)); + tmp += 20.0 * tx * x * x * x * (1 - x) * (1 - x) * (1 - x); + x = tmp; + tmp = 0; + tmp += y * y * y * y; + tmp += 4.0 * y * y * y * y * (1 - y); + tmp += 10.0 * y * y * y * y * (1 - y) * (1 - y); + double ty = y * y / (1.0 - 2.0 * y * (1 - y)); + tmp += 20.0 * ty * y * y * y * (1 - y) * (1 - y) * (1 - y); + y = tmp; + tmp = 0; + double c1, c2; + tmp += x * x * x * y * y * y; + tmp += 3.0 * x * x * x * (1 - x) * y * y * y; + tmp += 3.0 * x * x * x * x * y * y * (1 - y); + tmp += 6.0 * x * x * (1 - x) * (1 - x) * y * y * y * y; + tmp += 3.0 * x * x * x * x * (1 - y) * (1 - y) * y * y; + tmp += 12.0 * x * x * x * (1 - x) * y * y * y * (1 - y); + tmp += 4.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * y; + tmp += 4.0 * x * x * x * x * x * y * (1 - y) * (1 - y) * (1 - y); + tmp += 24.0 * x * x * x * (1 - x) * (1 - x) * y * y * y * (1 - y); + tmp += 24.0 * x * x * x * x * (1 - x) * y * y * (1 - y) * (1 - y); + tmp += 5.0 * x * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * y; + tmp += x * x * x * x * x * (1 - y) * (1 - y) * (1 - y) * (1 - y) * y; + tmp += 20.0 * x * x * x * x * (1 - x) * y * (1 - y) * (1 - y) * (1 - y) * y; + tmp += 60.0 * x * x * x * (1 - x) * (1 - x) * y * y * y * (1 - y) * (1 - y); + tmp += 40.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * (1 - y); + double tt; + tt = (1 - x) * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * y; + tt += x * x * x * x * x * (1 - y) * (1 - y) * (1 - y) * (1 - y) * (1 - y); + tt += 25.0 * x * x * x * x * (1 - x) * y * (1 - y) * (1 - y) * (1 - y) * (1 - y); + tt += 100.0 * x * x * x * (1 - x) * (1 - x) * y * y * (1 - y) * (1 - y) * (1 - y); + tt += 100.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * (1 - y) * (1 - y); + tt += 25.0 * x * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * (1 - y); + double s = x * y / (1 - x * (1 - y) - y * (1 - x)); + tt *= s; + c1 = tmp + tt; + double z = y; + y = x; + x = z; + tmp = 0; + tmp += x * x * x * y * y * y; + tmp += 3.0 * x * x * x * (1 - x) * y * y * y; + tmp += 3.0 * x * x * x * x * y * y * (1 - y); + tmp += 6.0 * x * x * (1 - x) * (1 - x) * y * y * y * y; + tmp += 3.0 * x * x * x * x * (1 - y) * (1 - y) * y * y; + tmp += 12.0 * x * x * x * (1 - x) * y * y * y * (1 - y); + tmp += 4.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * y; + tmp += 4.0 * x * x * x * x * x * y * (1 - y) * (1 - y) * (1 - y); + tmp += 24.0 * x * x * x * (1 - x) * (1 - x) * y * y * y * (1 - y); + tmp += 24.0 * x * x * x * x * (1 - x) * y * y * (1 - y) * (1 - y); + tmp += 5.0 * x * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * y; + tmp += x * x * x * x * x * (1 - y) * (1 - y) * (1 - y) * (1 - y) * y; + tmp += 20.0 * x * x * x * x * (1 - x) * y * (1 - y) * (1 - y) * (1 - y) * y; + tmp += 60.0 * x * x * x * (1 - x) * (1 - x) * y * y * y * (1 - y) * (1 - y); + tmp += 40.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * (1 - y); + tt = (1 - x) * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * y; + tt += x * x * x * x * x * (1 - y) * (1 - y) * (1 - y) * (1 - y) * (1 - y); + tt += 25.0 * x * x * x * x * (1 - x) * y * (1 - y) * (1 - y) * (1 - y) * (1 - y); + tt += 100.0 * x * x * x * (1 - x) * (1 - x) * y * y * (1 - y) * (1 - y) * (1 - y); + tt += 100.0 * x * x * (1 - x) * (1 - x) * (1 - x) * y * y * y * (1 - y) * (1 - y); + tt += 25.0 * x * (1 - x) * (1 - x) * (1 - x) * (1 - x) * y * y * y * y * (1 - y); + s = x * y / (1 - x * (1 - y) - y * (1 - x)); + tt *= s; + c2 = tmp + tt; + double ans = 0; + ans += c1 * c2 * c1; + ans += c1 * c1 * (1 - c2) * c2; + ans += 2.0 * c1 * (1 - c1) * c2 * c2; + ans += c1 * c1 * c1 * (1 - c2) * (1 - c2); + ans += c1 * (1 - c1) * (1 - c1) * c2 * c2; + ans += 4.0 * (1 - c1) * c1 * c1 * c2 * (1 - c2); + ans *= 100.0; + printf("Case #%d: %.4lf%%\n", ii, ans); + } + return 0; +} diff --git a/12014.cpp b/12014.cpp new file mode 100644 index 0000000..85253ed --- /dev/null +++ b/12014.cpp @@ -0,0 +1,316 @@ +#include + +using namespace std; + +const int N = 1000; +char s1[N], s2[N]; +int a[N], b[N], c[N], r, dragon, seven, purely, chunkious, sky, royal, tot; + +int f(char x, char y) +{ + int r; + if (y == 'D') + { + r = 0; + } + if (y == 'B') + { + r = 9; + } + if (y == 'M') + { + r = 18; + } + return r + x - '1'; +} + +char g(int i) +{ + if (i / 9 == 0) + { + return 'D'; + } + if (i / 9 == 1) + { + return 'B'; + } + return 'M'; +} + +void dfs2(int i) +{ + if (i == 27) + { + int x = 0, y = sky, j; + for (j = 0; j < 27; j++) + { + x += a[j]; + if (a[j] != 0 && a[j] != 2) + { + break; + } + if (a[j] == 2 && j % 9 != 0 && j % 9 != 8) + { + sky = 0; + } + } + if (x == 2 && j == 27 && tot == 4) + { + r = max(r, 1 + dragon + purely + royal + chunkious + sky); + } + sky = y; + return; + } + int x, y; + if (i % 9 <= 6 && a[i] && a[i + 1] && a[i + 2]) + { + tot++; + a[i]--; + a[i + 1]--; + a[i + 2]--; + x = chunkious; + y = sky; + chunkious = 0; + if (i % 9 != 0 && (i + 2) % 9 != 8) + { + sky = 0; + } + dfs2(i); + tot--; + a[i]++; + a[i + 1]++; + a[i + 2]++; + chunkious = x; + sky = y; + } + if (a[i] >= 3) + { + tot++; + a[i] -= 3; + y = sky; + if (i % 9 != 0 && i % 9 != 8) + { + sky = 0; + } + dfs2(i); + tot--; + a[i] += 3; + sky = y; + } + dfs2(i + 1); +} + +void dfs1(int i) +{ + if (i == 27) + { + int j; + for (j = 0; j < 27; j++) + if (b[j]) + { + break; + } + if (j == 27) + { + dfs2(0); + } + return; + } + int x, y; + if (i % 9 <= 6 && b[i] && b[i + 1] && b[i + 2]) + { + tot++; + b[i]--; + b[i + 1]--; + b[i + 2]--; + x = chunkious; + y = sky; + chunkious = 0; + if (i % 9 != 0 && (i + 2) % 9 != 8) + { + sky = 0; + } + dfs1(i); + tot--; + b[i]++; + b[i + 1]++; + b[i + 2]++; + chunkious = x; + sky = y; + } + if (b[i] >= 3) + { + tot++; + b[i] -= 3; + y = sky; + if (i % 9 != 0 && i % 9 != 8) + { + sky = 0; + } + dfs1(i); + tot--; + b[i] += 3; + sky = y; + } + if (b[i] >= 4) + { + tot++; + b[i] -= 4; + y = sky; + if (i % 9 != 0 && i % 9 != 8) + { + sky = 0; + } + dfs1(i); + tot--; + b[i] += 4; + sky = y; + } + dfs1(i + 1); +} + +void calc() +{ + int i, x, y, z; + x = y = z = 0; + for (i = 0; i < 27; i++) + { + if (c[i] == 0) + { + continue; + } + if (i < 9) + { + x++; + } + else if (i < 18) + { + y++; + } + else + { + z++; + } + } + if (x && y && z) + { + return; + } + dragon = purely = royal = 0; + for (i = 0; i < 27; i++) + if (c[i] == 4) + { + dragon++; + } + x = y = z = 0; + for (i = 0; i < 27; i++) + { + if (c[i] == 0) + { + continue; + } + if (i < 18) + { + x++; + } + if (i >= 9) + { + y++; + } + if (i < 9 || i >= 18) + { + z++; + } + } + if (x == 0 || y == 0 || z == 0) + { + purely = 2; + } + for (i = 0; i < 27; i++) + { + if (i % 9 == 1 || i % 9 == 4 || i % 9 == 7) + { + continue; + } + if (c[i]) + { + break; + } + } + if (i == 27) + { + royal = 2; + } + x = 0; + sky = 2; + for (i = 0; i < 27; i++) + { + x += a[i]; + if (a[i] % 2 != 0) + { + break; + } + if (a[i] && i % 9 != 0 && i % 9 != 8) + { + sky = 0; + } + } + if (x == 14 && i == 27) + { + r = max(r, 3 + dragon + purely + royal + sky); + } + chunkious = 1; + sky = 2; + dfs1(0); +} + +int main() +{ + int i, t, tt = 1, flag; + scanf("%d", &t); + while (t--) + { + scanf("%s%s", s1, s2); + memset(a, 0, sizeof(a)); + memset(b, 0, sizeof(b)); + memset(c, 0, sizeof(c)); + for (i = 0; s1[i]; i += 2) + { + a[f(s1[i], s1[i + 1])]++; + c[f(s1[i], s1[i + 1])]++; + } + if (strcmp(s2, "NONE") != 0) + { + for (i = 0; s2[i]; i += 2) + { + b[f(s2[i], s2[i + 1])]++; + c[f(s2[i], s2[i + 1])]++; + } + } + printf("Case #%d:\n", tt++); + flag = 0; + for (i = 0; i < 27; i++) + { + if (c[i] == 4) + { + continue; + } + a[i]++; + c[i]++; + r = 0; + calc(); + if (r) + { + flag = 1; + printf("%d%c: %d\n", i % 9 + 1, g(i), r); + } + a[i]--; + c[i]--; + } + if (flag == 0) + { + puts("NONE"); + } + } + return 0; +} diff --git a/12015.cpp b/12015.cpp index 7810911..b58212f 100644 --- a/12015.cpp +++ b/12015.cpp @@ -1,47 +1,66 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - for (int t = 1; t <= T; t++){ - string url[10]; - int val[10]; - int M = 0, i; - for (i = 0; i < 10; i++){ - cin >> url[i] >> val[i]; - M = max(val[i], M); +bool starts_with_www(const string &s) +{ + if (s.size() > 3 && string(s.begin(), s.begin() + 4) == "www.") + { + return true; + } + return false; +} + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + if (a.second == b.second) + { + if (starts_with_www(a.first) && starts_with_www(b.first)) + { + return a.first < b.first; + } + else if (starts_with_www(a.first) && !starts_with_www(b.first)) + { + return true; + } + else if (!starts_with_www(a.first) && starts_with_www(b.first)) + { + return false; + } + else + { + return a.first < b.first; + } + } + return a.second > b.second; +} + +int main() +{ + int cases; + string url; + int relevance; + vector> v; + cin >> cases; + for (int casenum = 0; casenum < cases; casenum++) + { + v.clear(); + for (int i = 0; i < 10; i++) + { + cin >> url >> relevance; + v.push_back(make_pair(url, relevance)); + } + sort(v.begin(), v.end(), theTruthIsOutThere); + int best = v[0].second; + cout << "Case #" << casenum + 1 << ":" << endl; + for (int i = 0; i < 10; i++) + { + if (v[i].second < best) + { + break; + } + cout << v[i].first << endl; } - cout << "Case #" << t << ":\n"; - for (i = 0; i < 10; i++) - if( val[i] == M ) - cout << url[i] << endl; } return 0; } diff --git a/12016.cpp b/12016.cpp new file mode 100644 index 0000000..df5607d --- /dev/null +++ b/12016.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define M 1005 +#define inf 0x3fffffff + +struct point +{ + int x, y; +} pw[M]; + +struct polygon +{ + polygon() : u(-inf), d(inf), l(inf), r(-inf) {} + int n, u, d, l, r; + point p[M]; + bool inRectBox(point &pt) + { + return l <= pt.x && pt.x <= r && d <= pt.y && pt.y <= u; + } +}; + +int multi(point a, point b, point c) +{ + return (b.x - a.x) * (c.y - b.y) - (c.x - b.x) * (b.y - a.y); +} + +bool ptInPg(point &pt, polygon &pg) +{ + int cnt = 0, i, j, cp; + for (i = 0, j = pg.n - 1; i < pg.n; j = i++) + { + if (pt.y > pg.p[i].y && pt.y > pg.p[j].y || + pt.y < pg.p[i].y && pt.y < pg.p[j].y) + { + continue; + } + cp = multi(pt, pg.p[i], pg.p[j]); + if (cp == 0 && + (pg.p[i].x <= pt.x && pt.x <= pg.p[j].x || + pg.p[j].x <= pt.x && pt.x <= pg.p[i].x)) + { + return true; + } + if (cp < 0 && pg.p[i].y > pg.p[j].y && pt.y != pg.p[i].y || + cp > 0 && pg.p[i].y < pg.p[j].y && pt.y != pg.p[j].y) + { + ++cnt; + } + } + return (cnt & 1); +} + +int has[M]; + +int main() +{ + int t, m, cc = 0, i, n, id; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", &pw[i].x, &pw[i].y); + } + scanf("%d", &m); + printf("Case #%d:\n", ++cc); + while (m--) + { + polygon pg; + scanf("%d", &pg.n); + for (i = 0; i < n; i++) + { + has[i] = 0; + } + for (i = 0; i < pg.n; i++) + { + scanf("%d", &id); + --id; + has[id] = 1; + pg.p[i] = pw[id]; + if (pg.u < pg.p[i].y) + { + pg.u = pg.p[i].y; + } + if (pg.d > pg.p[i].y) + { + pg.d = pg.p[i].y; + } + if (pg.l > pg.p[i].x) + { + pg.l = pg.p[i].x; + } + if (pg.r < pg.p[i].x) + { + pg.r = pg.p[i].x; + } + } + int ans = pg.n; + for (i = 0; i < n; i++) + { + if (has[i]) + { + continue; + } + if (pg.inRectBox(pw[i]) && ptInPg(pw[i], pg)) + { + ++ans; + } + } + printf("%d\n", ans); + } + } + return 0; +} diff --git a/12017.cpp b/12017.cpp new file mode 100644 index 0000000..17a13a5 --- /dev/null +++ b/12017.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +typedef long long LL; +const int Max = 1024; +const int MaxE = 50010; +struct box +{ + int from, to; + box *s; +} edge[MaxE], *hd[Max], *h2[Max], *cp; +void addEdge(int x, int y, box *h[]) +{ + cp->from = x, cp->to = y; + cp->s = h[x], h[x] = cp++; +} +int pre[Max], low[Max], id[Max]; +int s[Max], node[Max]; +int cnt, sNum, pCnt; +void SCDfs(int x) +{ + int min, y; + min = pre[x] = low[x] = cnt++; + s[sNum++] = x; + for (box *p = hd[x]; p; p = p->s) + if (id[y = p->to] == -1) + { + if (pre[y] == -1) + { + SCDfs(y); + } + if (low[y] < min) + { + min = low[y]; + } + } + if (min < low[x]) + { + low[x] = min; + return; + } + node[pCnt] = 0; + do + { + id[s[--sNum]] = pCnt; + ++node[pCnt]; + } while (s[sNum] != x); + pCnt++; +} +void rdfs(int x) +{ + id[x] = 0; + for (box *p = h2[x]; p; p = p->s) + if (id[p->to] == -1) + { + rdfs(p->to); + } + s[sNum++] = x; +} +int main() +{ + int T; + scanf("%d", &T); + for (int ct = 1; ct <= T; ct++) + { + int N, M; + scanf("%d%d", &N, &M); + memset(hd + 1, 0, sizeof(hd[0]) * N); + cp = edge; + for (int i = 0; i < M; i++) + { + int x, y; + scanf("%d%d", &x, &y); + addEdge(x, y, hd); + } + memset(id + 1, -1, sizeof(id[0]) * N); + memset(pre + 1, -1, sizeof(pre[0]) * N); + sNum = 0, cnt = 0, pCnt = 0; + for (int i = 1; i <= N; i++) + if (id[i] == -1) + { + SCDfs(i); + } + //for (int i = 1;i <= N;i++)printf("%d ",id[i]);puts(""); + //for (int i = 0;i < pCnt;i++)printf("%d ",node[i]);puts(""); + memset(h2, 0, sizeof(h2[0]) * pCnt); + for (box *p = edge, *np = cp; p < np; p++) + if (id[p->from] != id[p->to]) + { + addEdge(id[p->to], id[p->from], h2); + //printf("%d to %d\n",id[p->from],id[p->to]); + } + memset(id, -1, sizeof(id[0]) * pCnt); + sNum = 0; + for (int i = 0; i < pCnt; i++) + if (id[i] == -1) + { + rdfs(i); + } + //for (int i = 0;i < pCnt;i++)printf("%d ",s[i]);puts(""); + int R1 = 0, R2 = 0; + for (int i = pCnt - 1; i >= 0; i--) + { + int x = s[i]; + memset(low, -1, sizeof(low[0]) * pCnt); + low[x] = 0; + int cnt = 0; + for (int j = i; j >= 0; j--) + { + int y = s[j]; + if (low[y] == -1) + { + continue; + } + for (box *p = h2[y]; p; p = p->s) + if (low[y] + 1 > low[p->to]) + { + low[p->to] = low[y] + 1; + } + if (low[y] == 1) + { + ++R1; + } + cnt += node[y]; + } + if (node[x] > 1) + { + R1 += node[x]; + R2 += node[x] * (node[x] - 1); + } + R2 += (cnt - node[x]) * node[x]; + //printf("%d : %d %d | %d %d\n",x,node[x],cnt,R1,R2); + //for (int j = 0;j < pCnt;j++)printf("%d ",low[j]);puts(""); + } + printf("Case #%d: %d %d\n", ct, R1, R2); + } + return 0; +} diff --git a/12018.cpp b/12018.cpp new file mode 100644 index 0000000..51e90e8 --- /dev/null +++ b/12018.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +struct Fruit +{ + int start, end; +}; + +Fruit fr[1010]; +int n, dp[1010]; +int ans; + +int cmp(const void *a, const void *b) +{ + Fruit *f1 = (Fruit *)a; + Fruit *f2 = (Fruit *)b; + return f1->start - f2->start; +} + +int max(int a, int b) +{ + return a > b ? a : b; +} + +void ISureIWillAc() +{ + int i; + ans = 0; + qsort(fr, n, sizeof(Fruit), cmp); + memset(dp, 0, sizeof(dp)); + for (i = 2; i < n; i++) + { + int score = 0; + if (i + 1 < n && fr[i].start == fr[i + 1].start) + { + continue; + } + for (int j = i; j >= 0; j--) + { + if (fr[j].start <= fr[i].start && fr[j].end >= fr[i].start) + { + score++; + } + int may = score; + if (may < 3) + { + may = 0; + } + if (j == 0) + { + dp[i] = max(dp[i], may); + } + else + { + dp[i] = max(dp[i], dp[j - 1] + may); + } + ans = max(ans, dp[i]); + } + } +} + +int main() +{ + int t, i; + scanf("%d", &t); + for (int cas = 1; cas <= t; cas++) + { + printf("Case #%d: ", cas); + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", &fr[i].start, &fr[i].end); + } + ISureIWillAc(); + printf("%d\n", ans); + } +} diff --git a/12019.cpp b/12019.cpp index 40def77..1c84d76 100644 --- a/12019.cpp +++ b/12019.cpp @@ -1,58 +1,36 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string day[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; - int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - vector< vector > week(12); - int S = 5; - int i, j; - FOI(i, 0, 11){ - FOI(j, 0, month[i]-1){ - week[i].push_back(S); - S = (S + 1) % 7; +int main() +{ + int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + map m; + m[0] = "Saturday"; + m[1] = "Sunday"; + m[2] = "Monday"; + m[3] = "Tuesday"; + m[4] = "Wednesday"; + m[5] = "Thursday"; + m[6] = "Friday"; + int cur_d = 0; + int result[13][32]; + for (int i = 1; i <= 12; i++) + { + for (int j = 1; j <= months[i - 1]; j++) + { + result[i][j] = cur_d; + cur_d++; + cur_d %= 7; } } - int T; - cin >> T; - while( T-- ){ - int D, M; - cin >> M >> D; - cout << day[week[M - 1][D - 1]] << endl; + int t; + int mon, day; + cin >> t; + while (t--) + { + cin >> mon >> day; + cout << m[result[mon][day]] << endl; } return 0; } - diff --git a/1202.cpp b/1202.cpp new file mode 100644 index 0000000..d080de2 --- /dev/null +++ b/1202.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +int s[205][205][4], in[205][205], ans[205][205]; + +int main() +{ + int N, n, M, m, i, j, dir, len, x, y, nx, ny, dis; + double stx, sty; + while (~scanf(" %d %d", &N, &M) && N != -1 && M != -1) + { + memset(s, 0, sizeof(s)); + //s : 0-> nothing, 1->wall, 2->door + //s : 0123 -> up,down,left,right + //dir : 0->parrelel x, 1->y + for (n = 0; n < N; n++) + { + scanf(" %d %d %d %d", &x, &y, &dir, &len); + if (dir == 0) + { + for (i = 0; i < len; i++, x++) + { + s[x][y][1] = 1; + s[x][y - 1][0] = 1; + } + } + else + { + for (i = 0; i < len; i++, y++) + { + s[x][y][2] = 1; + s[x - 1][y][3] = 1; + } + } + } + for (m = 0; m < M; m++) + { + scanf(" %d %d %d", &x, &y, &dir); + if (dir == 0) + { + s[x][y][1] = 2; + if (y - 1 >= 0) + s[x][y - 1][0] = 2; + } + else + { + s[x][y][2] = 2; + if (x - 1 >= 0) + s[x - 1][y][3] = 2; + } + } + scanf(" %lf %lf", &stx, &sty); + x = floor(stx); + y = floor(sty); + if (x > 199 || y > 199) + { + printf("0\n"); + continue; + } + memset(in, 0, sizeof(in)); + for (i = 0; i < 205; i++) + for (j = 0; j < 205; j++) + ans[i][j] = MAX; + queue q; + q.push(x); + q.push(y); + ans[x][y] = 0; + while (!q.empty()) + { + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + dis = ans[x][y]; + in[x][y] = 0; + for (i = 0; i < 4; i++) + { + nx = x; + ny = y; + if (i == 0) + ny++; + else if (i == 1) + ny--; + else if (i == 2) + nx--; + else + nx++; + if (nx >= 0 && ny >= 0 && nx < 205 && ny < 205) + { + if (s[x][y][i] == 0)//empty + { + if (dis < ans[nx][ny]) + { + ans[nx][ny] = dis; + if (in[nx][ny] == 0) + { + in[nx][ny] = 1; + q.push(nx); + q.push(ny); + } + } + } + else if (s[x][y][i] == 2)//door + { + if (dis + 1 < ans[nx][ny]) + { + ans[nx][ny] = dis + 1; + if (in[nx][ny] == 0) + { + in[nx][ny] = 1; + q.push(nx); + q.push(ny); + } + } + } + } + } + } + if (ans[0][0] != MAX) + printf("%d\n", ans[0][0]); + else + printf("-1\n"); + } + return 0; +} diff --git a/12022.cpp b/12022.cpp index e3d5efc..6660694 100644 --- a/12022.cpp +++ b/12022.cpp @@ -1,52 +1,28 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; typedef unsigned int uint; typedef long long int64; typedef unsigned long long uint64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +#define FOI(i, A, B) for (i = A; i <= B; i++) +#define FOD(i, A, B) for (i = A; i >= B; i--) +#define PI acos(-1.0) +#define INF 1 << 30 +#define EPS 1e-9 +#define sqr(x) (x) * (x) -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +int main() +{ int64 num[] = {1, 1, 3, 13, 75, 541, 4683, 47293, 545835, 7087261, 102247563, 1622632573}; int T; cin >> T; - while( T-- ){ + while (T--) + { int N; cin >> N; cout << num[N] << endl; } return 0; } - diff --git a/12024.cpp b/12024.cpp index 2c30448..43815ad 100644 --- a/12024.cpp +++ b/12024.cpp @@ -1,14 +1,29 @@ -#include +#include -int main() { - int num[] = {1, 2, 9, 44, 265, 1854, 14833, 133496, 1334961, 14684570, 176214841}; - int den[] = {2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600}; - - int T, N; - scanf("%d", &T); - while (T--) { - scanf("%d", &N); - printf("%d/%d\n", num[N - 2], den[N - 2]); +using namespace std; + +int main() +{ + int t, n; + long dp[13]; + long fact[13]; + dp[0] = 1; + dp[1] = 0; + for (int i = 2; i < 13; i++) + { + dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]); + } + fact[0] = 1; + fact[1] = 1; + for (int i = 2; i < 13; i++) + { + fact[i] = i * fact[i - 1]; + } + cin >> t; + while (t--) + { + cin >> n; + cout << dp[n] << "/" << fact[n] << endl; } return 0; } diff --git a/12026.cpp b/12026.cpp new file mode 100644 index 0000000..0d5443c --- /dev/null +++ b/12026.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +//***************** +// LAM PHAN VIET ** +// UVA 12026 - Radars +// Time limit: 1.000s +//******************************** + +#define FileIn(file) freopen(file ".inp", "r", stdin) +#define FileOut(file) freopen(file ".out", "w", stdout) +#define REP(i, a, b) for (int i = a; i <= b; i++) +#define REP0(i, n) for (int i = 0; i < n; i++) +#define Fill(ar, val) memset(ar, val, sizeof(ar)) +#define PI 3.1415926535897932385 +#define uint64 unsigned long long +#define int64 long long +#define all(ar) ar.begin(), ar.end() +#define pb push_back +#define bit(n) (1 << (n)) +#define Last(i) (i & -i) +#define Parent(i) (i - Last(i)) +#define INF 500000000 +#define maxN 1001 +#define maxM 1000000 + +const int dx[] = {0, 1, -1, 0}; +const int dy[] = {1, 0, 0, -1}; +int n, m, nr, a[maxN][maxN]; +int x[maxM], y[maxM], Rx[maxM], Ry[maxM]; +double d[maxM]; +bool Free[maxM]; + +double Distance(int i, int j) +{ + double a = x[i] - Rx[j]; + double b = y[i] - Ry[j]; + return sqrt(a * a + b * b); +} + +bool isOK(int x, int y) +{ + return (x >= 1 && x <= n && y >= 1 && y <= n); +} + +double Mean(int i, int j) +{ + double Count = 0, Sum = 0; + REP0(k, 4) + if (isOK(i + dx[k], j + dy[k])) + { + Count++; + Sum += a[i + dx[k]][j + dy[k]]; + } + return Sum / Count; +} + +void Solve() +{ + memset(Free, true, m + 1); + REP0(i, m) + { + REP0(j, nr) + if (Distance(i, j) <= d[j]) + { + Free[i] = false; + break; + } + } + int Count = 0; + REP0(i, m) + if (Free[i]) + Count++; + printf("%d\n", Count); +} + +main() +{ + // FileIn("test"); FileOut("test"); + int Case; + scanf("%d", &Case); + while (Case--) + { + scanf("%d", &n); + m = nr = 0; + REP(i, 1, n) + REP(j, 1, n) + { + scanf("%d", &a[i][j]); + if (a[i][j]) + { + x[m] = i; + y[m++] = j; + } + } + REP(i, 1, n) + REP(j, 1, n) + if (!a[i][j]) + { + d[nr] = Mean(i, j); + if (d[nr] < 1) + continue; + Rx[nr] = i; + Ry[nr++] = j; + } + Solve(); + } +} + +/* lamphanviet@gmail.com - 2011 */ diff --git a/12027.cpp b/12027.cpp index dec5fae..f2c292f 100644 --- a/12027.cpp +++ b/12027.cpp @@ -1,45 +1,22 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; typedef unsigned int uint; typedef long long int64; typedef unsigned long long uint64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +#define FOI(i, A, B) for (i = A; i <= B; i++) +#define FOD(i, A, B) for (i = A; i >= B; i--) +#define PI acos(-1.0) +#define INF 1 << 30 +#define EPS 1e-9 +#define sqr(x) (x) * (x) -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true){ +int main() +{ + while (true) + { string str; int i, j; cin >> str; @@ -47,15 +24,15 @@ int main(){ break; int L = str.length(); int N = (int)(str[0] - '0'); - if (L % 2 == 0){ + if (L % 2 == 0) + { N *= 10; N += (str[1] - '0'); } cout << (int)sqrt(N); - FOI(i, 1, ceil(L/2.0) - 1) - cout << "0"; + FOI(i, 1, ceil(L / 2.0) - 1) + cout << "0"; cout << endl; } return 0; } - diff --git a/12028.cpp b/12028.cpp new file mode 100644 index 0000000..600e090 --- /dev/null +++ b/12028.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define INF_MAX 2147483647 +#define INF_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL long long + +#define For(i, a, b) for (int i = (a); i < (b); i++) +#define Fors(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fore(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Set(a, s) memset(a, s, sizeof(a)) + +int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +long long a[100000 + 10], accu_sum[100000 + 10]; + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + long long k, c, n; + scanf("%lld %lld %lld %lld", &k, &c, &n, &a[0]); + for (int i = 1; i < n; i++) + a[i] = (k * a[i - 1] + c) % 1000007; + sort(a, a + n); + for (int i = 0; i < n; i++) + accu_sum[i] = a[i]; + for (int i = 1; i < n; i++) + accu_sum[i] += accu_sum[i - 1]; + long long sum = 0; + for (int i = 0; i < n; i++) + { + long long first = a[i] * (n - i - 1); + long long second = accu_sum[n - 1] - accu_sum[i]; + sum += abs(first - second); + } + printf("Case %d: %lld\n", ++cases, sum); + } + return 0; +} diff --git a/12029.cpp b/12029.cpp new file mode 100644 index 0000000..1b15619 --- /dev/null +++ b/12029.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +#define db double +#define rt return +#define cs const + +cs db eps = 1e-8, PI = acos(-1.); + +inline int sig(db x) +{ + rt(x > eps) - (x < -eps); +} + +#define cp const P & +#define op operator + +struct P +{ + db x, y, z; + P(db a = 0, db b = 0, db c = 0) : x(a), y(b), z(c) {} + void in() + { + scanf("%lf%lf%lf", &x, &y, &z); + } + P op + (cp a)cs { rt P(x + a.x, y + a.y, z + a.z); } + P op - (cp a)cs { rt P(x - a.x, y - a.y, z - a.z); } + P op *(cs db &k)cs { rt P(x * k, y * k, z * k); } + P op ^ (cp a)cs { rt P(y * a.z - a.y * z, z * a.x - a.z * x, x * a.y - a.x * y); } + db op *(cp a)cs { rt x *a.x + y *a.y + z *a.z; } + db dot(P a, P b) + { + rt(a - *this) * (b - *this); + } + P cross(P a, P b) + { + rt a - *this ^ b - *this; + } + db L() + { + rt sqrt(x * x + y * y + z * z); + } + db dis_line(P a, P b) + { + rt cross(a, b).L() / (a - b).L(); + } + db dis_seg(P a, P b) + { + if (sig(a.dot(*this, b)) < 0) + { + rt(a - *this).L(); + } + if (sig(b.dot(*this, a)) < 0) + { + rt(b - *this).L(); + } + rt dis_line(a, b); + } + void out() + { + printf("(%f,%f,%f)\n", x + eps, y + eps, z + eps); + } +}; + +P calc(P a, P v, db agl) +{ + agl = agl * PI / 180.; + db c = cos(agl), s = sin(agl); + db x = v.x, y = v.y, z = v.z, l = v.L(); + x /= l, y /= l, z /= l; + db m[3][3]; + m[0][0] = c + x * x * (1. - c); + m[0][1] = x * y * (1. - c) - z * s; + m[0][2] = x * z * (1. - c) + y * s; + m[1][0] = x * y * (1. - c) + z * s; + m[1][1] = c + y * y * (1. - c); + m[1][2] = y * z * (1. - c) - x * s; + m[2][0] = x * z * (1. - c) - y * s; + m[2][1] = y * z * (1. - c) + x * s; + m[2][2] = c + z * z * (1. - c); + P res; + res.x = a.x * m[0][0] + a.y * m[0][1] + a.z * m[0][2]; + res.y = a.x * m[1][0] + a.y * m[1][1] + a.z * m[1][2]; + res.z = a.x * m[2][0] + a.y * m[2][1] + a.z * m[2][2]; + rt res; +} + +void solve(int cas) +{ + P start, goal, cur, dir, X, Y, Z; + int n; + scanf("%d", &n); + start.in(), goal.in(); + dir = P(1., 0, 0); + X = P(1., 0, 0), Y = P(0, 1., 0), Z = P(0, 0, 1.); + db mn = (start - goal).L(), mx = mn; + for (int i = 0; i < n; i++) + { + char str[15]; + db d; + scanf("%s%lf", str, &d); + switch (str[0]) + { + case 'F': + { + cur = start + dir * d; + mn = min(mn, goal.dis_seg(start, cur)); + mx = max(mx, (goal - cur).L()); + start = cur; + break; + } + case 'P': + { + dir = calc(dir, Y, -d); + X = calc(X, Y, -d); + Z = X ^ Y; + break; + } + case 'R': + { + dir = calc(dir, X, d); + Y = calc(Y, X, d); + Z = X ^ Y; + break; + } + case 'Y': + { + dir = calc(dir, Z, -d); + X = calc(X, Z, -d); + Y = Z ^ X; + break; + } + } + } + printf("Case %d: %.10lf %.10lf\n", cas, mn, mx); +} + +int main() +{ + int T, cas = 1; + scanf("%d", &T); + while (T--) + { + solve(cas++); + } + rt 0; +} diff --git a/1203.cpp b/1203.cpp new file mode 100644 index 0000000..d8c58c4 --- /dev/null +++ b/1203.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +class theTruthIsOutThere +{ +public: + bool operator()(const pair &a, const pair &b) + { + if (a.second == b.second) + { + return a.first > b.first; + } + else + { + return a.second > b.second; + } + } +}; + +int main() +{ + int qnum, period, k; + string cmd; + map periods; + priority_queue, vector>, theTruthIsOutThere> queue; + while (cin >> cmd) + { + if (cmd == "#") + { + break; + } + cin >> qnum >> period; + periods[qnum] = period; + queue.push(make_pair(qnum, period)); + } + cin >> k; + while (k--) + { + pair tmp = queue.top(); + queue.pop(); + cout << tmp.first << endl; + tmp.second += periods[tmp.first]; + queue.push(tmp); + } + return 0; +} diff --git a/12030.cpp b/12030.cpp new file mode 100644 index 0000000..946cc62 --- /dev/null +++ b/12030.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/120/12030.html +// Runtime: 0.508s +// Tag: Dp, bitmask, backtrack + +// @BEGIN_OF_SOURCE_CODE + +#define LL long long + +int matrix[15 + 3][15 + 3]; +long long memo[32768 + 5][2][2]; +int n; + +// at = dress position +// bitmask = which pair of shoes already matched +// one = 1, if all the matches upto have value 1, otherwise 0 +// two = 1, if at least one match upto have value 2, otherwise 0 +long long dp(int at, int bitmask, int one, int two) +{ + if (at == n) + { + return (one || two) == 1 ? 1 : 0; + } + if (memo[bitmask][one][two] != -1) + { + return memo[bitmask][one][two]; + } + long long sum = 0; + int tmp_one; + int tmp_two; + for (int i = 0; i < n; i++) + { + if ((bitmask & (1 << i)) == 0) + { + tmp_one = matrix[at][i] == 1 && one ? 1 : 0; + tmp_two = matrix[at][i] == 2 || two ? 1 : 0; + sum += dp(at + 1, bitmask | (1 << i), tmp_one, tmp_two); + } + } + return memo[bitmask][one][two] = sum; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + scanf("%d", &matrix[i][j]); + } + memset(memo, -1, sizeof(memo)); + printf("Case %d: %lld\n", ++cases, dp(0, 0, 1, 0)); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/12031.cpp b/12031.cpp new file mode 100644 index 0000000..c6a192d --- /dev/null +++ b/12031.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +int hefa[1010]; +int cal(int n) +{ + int i, j, k, m, t; + for (i = 0; i * i <= n; i++) + { + m = n - i * i; + for (j = i; j * j <= m; j++) + { + t = m - j * j; + k = sqrt(t) + 1; + if (k * k > t) + k--; + if (k * k > t) + k--; + if (k * k == t) + return 1; + } + } + return 0; +} +long long a[10]; +int pri[51000]; +int isp[210000]; +int cnt; +int dfs(int dep, long long now, long long ji) +{ + if (!now) + { + a[dep] = a[dep + 1] = a[dep + 2] = a[dep + 3] = 0; + return 1; + } + if (dep > 3) + return 0; + int i, tim; + for (int i = 1; i <= 10; i++) + if (now % (pri[i] * pri[i]) == 0) + { + while (now % (pri[i] * pri[i]) == 0) + ji *= pri[i], now /= pri[i] * pri[i]; + } + long long t = sqrt(now + 0.0); + if ((t + 1) * (t + 1) <= now) + t++; + if (t * t > now) + t--; + if (dep == 3) + { + a[dep] = t * ji; + if (dfs(dep + 1, now - t * t, ji)) + return 1; + return 0; + } + if (dep == 0) + tim = 1000; + else if (dep == 1) + { + if (hefa[now % 1000]) + return 0; + tim = 2; + } + else + { + long long tmp = now; + while (!(tmp & 1)) + tmp >>= 1; + if (tmp % 4 - 1 || now > 100000) + return 0; + tim = 2; + } + for (i = 0; i < tim && t >= 0; i++) + { + a[dep] = t * ji; + if (dfs(dep + 1, now - t * t, ji)) + return 1; + t--; + } + return 0; +} + +inline void scan(long long &u) +{ + char c; + while (c = getchar(), c < '0' || c > '9') + ; + u = c - '0'; + while (c = getchar(), c <= '9' && c >= '0') + u = u * 10 + c - '0'; +} + +int main() +{ + long long n, ji; + int i, j, t, ii = 0; + //freopen("r","r",stdin); + //freopen("w2","w",stdout); + for (i = 1; i <= 1000; i++) + if (!cal(i)) + hefa[i] = 1; + for (i = 2; i < 210000; i++) + if (!isp[i]) + { + pri[++cnt] = i; + for (j = i + i; j < 210000; j += i) + isp[j] = 1; + } + long long MA = 0; + scanf("%d", &t); + while (t--) + { + scan(n); + if (dfs(0, n, 1)) + { + printf("%lld %lld %lld %lld\n", a[0], a[1], a[2], a[3]); + if (a[3] > MA) + MA = a[3]; + } + else + printf("Impossible.\n"); + } +} diff --git a/12032.cpp b/12032.cpp new file mode 100644 index 0000000..3d46ad7 --- /dev/null +++ b/12032.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +/* {{{ FAST integer input */ +#define X10(n) ((n << 3) + (n << 1)) +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +bool adapt()// Returns true if there is a number waiting to be read, false otherwise +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +/* }}} end FAST integer input */ +int main() +{ + int t; + readint(t); + for (int cnum = 0, n; cnum++ < t;) + { + readint(n); + int cs = 0, ms = 0; + for (int i = 0, p = 0, z; i++ < n; p = z) + { + readint(z); + int dist = z - p; + if (cs == dist) + { + --cs; + } + else if (cs < dist) + { + cs = dist > ms ? (ms = dist) - 1 : ++ms; + } + } + printf("Case %d: %d\n", cnum, ms); + } + return 0; +} diff --git a/12033.cpp b/12033.cpp new file mode 100644 index 0000000..20aface --- /dev/null +++ b/12033.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int hea[1100], nex[2100], vv[2100], len[2100]; + +int dfs(int x, int fa) +{ + int res = 0; + for (int e = hea[x]; e != -1; e = nex[e]) + if (vv[e] != fa) + { + int sg = dfs(vv[e], x); + if (len[e] == 1) + { + res ^= (sg + 1); + } + else if (len[e] & 1) + { + res ^= (sg + ((sg & 1) ? -1 : 1)); + } + else + { + res ^= sg; + } + } + return res; +} + +int main() +{ + int tes, n, u, v, w; + scanf("%d", &tes); + for (int h = 1; h <= tes; ++h) + { + memset(hea, -1, sizeof(hea)); + scanf("%d", &n); + for (int i = 1; i < n; ++i) + { + scanf("%d %d %d", &u, &v, &w); + vv[i] = v; + nex[i] = hea[u]; + hea[u] = i; + len[i] = w; + vv[i + n] = u; + nex[i + n] = hea[v]; + hea[v] = i + n; + len[i + n] = w; + } + if (dfs(0, -1)) + { + printf("Case %d: Emily\n", h); + } + else + { + printf("Case %d: Jolly\n", h); + } + } + return 0; +} diff --git a/12034.cpp b/12034.cpp new file mode 100644 index 0000000..d157251 --- /dev/null +++ b/12034.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/120/12034.html +// Runtime: 0.016s +// Tag: Dp + +// @BEGIN_OF_SOURCE_CODE + +#define LL long long + +LL res[1000 + 5]; + +void preCalc() +{ + res[0] = 1; + res[1] = 1;// when n = 1, res = 1 + res[2] = 3;// when n = 2, res = 3 + res[3] = 13; + LL numOfZeros[1000 + 5]; + // numOfZeros [5] = number of binary numbers contain 5 zeros in a particular bit + for (int i = 0; i < 1005; i++) + { + numOfZeros[i] = 1; + } + numOfZeros[0] = 1; + numOfZeros[1] = 4; + numOfZeros[2] = 6; + numOfZeros[3] = 4; + for (int i = 4; i <= 1000; i++) + { + LL val = 0; + for (int j = i - 1; j >= 0; j--) + { + val += (numOfZeros[j] * res[j]) % 10056; + val %= 10056; + } + res[i] = val; + for (int j = i; j >= 1; j--) + { + numOfZeros[j] += numOfZeros[j - 1]; + numOfZeros[j] %= 10056; + } + } +} + +int main() +{ + preCalc(); + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int n; + scanf("%d", &n); + printf("Case %d: %lld\n", ++cases, res[n]); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/12035.cpp b/12035.cpp new file mode 100644 index 0000000..343b5a7 --- /dev/null +++ b/12035.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +int a[30]; +int s[30], ss, n; + +int b[30], vis[30], is[30]; + +int jud() +{ + int i, j, sss = ss, M; + for (i = 1; i <= n; i++) + { + b[i] = a[i]; + } + while (1) + { + M = 0; + for (i = 1; i <= n; i++) + if (!vis[i]) + { + if (M < b[i]) + { + M = b[i], j = i; + } + } + if (!M) + { + return 1; + } + b[j] = 0; + int MM; + memset(is, 0, sizeof(is)); + while (M--) + { + MM = 0; + for (i = 1; i <= n; i++) + if (vis[i] && b[i] && !is[i]) + { + if (MM < b[i]) + { + MM = b[i], j = i; + } + } + if (!MM) + { + return 0; + } + b[j]--; + is[j] = 1; + } + } +} +int dfs(int dep, int num, int he, int M1, int M2) +{ + if (M1 > n - num || M2 > num + dep || (he << 1) > ss || ((s[dep] + he) << 1) < ss) + { + return 0; + } + if (dep == 0) + { + return jud(); + } + vis[dep] = 1; + if (dfs(dep - 1, num + 1, he + a[dep], M1 > a[dep] ? M1 : a[dep], M2)) + { + return 1; + } + vis[dep] = 0; + if (dfs(dep - 1, num, he, M1, M2 > a[dep] ? M2 : a[dep])) + { + return 1; + } + return 0; +} + +int main() +{ + int t, ii = 0, i, j, cnt; + scanf("%d", &t); + while (t--) + { + cnt = 0; + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + if (a[i]) + { + b[++cnt] = a[i]; + } + } + n = cnt; + for (i = 1; i <= n; i++) + { + a[i] = b[i]; + } + sort(a + 1, a + 1 + n); + for (i = 1; i <= n; i++) + { + s[i] = s[i - 1] + a[i]; + } + ss = s[n]; + printf("Case %d: ", ++ii); + if ((ss & 1) || !dfs(n, 0, 0, 0, 0)) + { + printf("NO\n"); + } + else + { + printf("YES\n"); + } + } + return 0; +} diff --git a/12036.cpp b/12036.cpp index a7bc995..ae2bc3b 100644 --- a/12036.cpp +++ b/12036.cpp @@ -1,25 +1,37 @@ -#include -#include +#include + using namespace std; -int main() { - int T, t, i, j, N, I; - bool flag; - int count[110]; - scanf("%d", &T); - for (t = 1; t <= T; t++) { - flag = true; - memset(count, 0, sizeof count); - scanf("%d", &N); - for (i = 0; i < N; i++) { - for (j = 0; j < N; j++) { - scanf("%d", &I); - ++count[I]; - if (count[I] > N) flag = false; +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int main() +{ + int t, n, tmp, case_number = 0; + cin >> t; + while (t--) + { + cin >> n; + bool ok = true; + int count[101] = {0}; + rep(i, n * n) + { + cin >> tmp; + count[tmp]++; + if (count[tmp] > n) + { + ok = false; } } - if (!flag) printf("Case %d: no\n", t); - else printf("Case %d: yes\n", t); + cout << "Case " << ++case_number << ": "; + if (ok) + { + cout << "yes" << endl; + } + else + { + cout << "no" << endl; + } } return 0; } diff --git a/12037.cpp b/12037.cpp new file mode 100644 index 0000000..c1f3a3e --- /dev/null +++ b/12037.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define i64 long long +int tes, n; +i64 k, bas[7][7], tmp[7][7], res[7][7]; +const i64 inf = 34830; +void mul(i64 a[][7], i64 b[][7]) +{ + memset(tmp, 0, sizeof(tmp)); + for (int h = 0; h < 7; ++h) + for (int i = 0; i < 7; ++i) + for (int j = 0; j < 7; ++j) + { + tmp[i][j] += a[i][h] * b[h][j]; + } + for (int i = 0; i < 7; ++i) + for (int j = 0; j < 7; ++j) + { + a[i][j] = tmp[i][j] % inf; + } +} +i64 cal(int m) +{ + memset(bas, 0, sizeof(bas)); + memset(res, 0, sizeof(res)); + bas[0][0] = (k - 4) % inf; + bas[0][1] = bas[0][2] = (k - 3) % inf; + bas[1][3] = (k - 3) % inf; + bas[1][6] = (k - 2) % inf; + bas[2][4] = (k - 3) % inf; + bas[2][5] = (k - 2) % inf; + bas[3][0] = bas[3][2] = 1; + bas[4][0] = bas[4][1] = 1; + bas[5][3] = 1; + bas[6][4] = 1; + for (int i = 0; i < 7; ++i) + { + res[i][i] = 1; + } + while (m) + { + if (m & 1) + { + mul(res, bas); + } + m >>= 1; + mul(bas, bas); + } + return res[5][5] * k % inf * (k - 1) % inf; +} +i64 init() +{ + if (n == 1) + { + return k % inf; + } + if (n == 2) + { + return k * (k - 1) % inf; + } + if (k < 3) + { + return 0; + } + //if(k==3) return k*(k-1)%inf; + return cal(n); +} +int main() +{ + scanf("%d", &tes); + for (int h = 1; h <= tes; ++h) + { + scanf("%d %lld", &n, &k); + printf("Case %d: %lld\n", h, init()); + } + return 0; +} diff --git a/12038.cpp b/12038.cpp new file mode 100644 index 0000000..1b4df1e --- /dev/null +++ b/12038.cpp @@ -0,0 +1,319 @@ +#include + +using namespace std; + +#define MAXK 50 + +typedef unsigned long long u64; + +template +T gcd(T a, T b) +{ + for (T c = a % b; c != 0; a = b, b = c, c = a % b) + ; + return b; +} + +template +TB sq_pow(TB b, TE e) +{ + TB res = 1; + for (; e; e >>= 1, b *= b) + if (e & 1) + { + res *= b; + } + return res; +} + +struct U128 +{ + u64 lo, hi; + static const size_t sz = 128; + static const size_t hsz = 64; + static const u64 bmax = -1; + + U128() : lo(0), hi(0) {} + U128(int v) : lo(v), hi(0) {} + + U128 operator-() const + { + return ~U128(*this) + 1; + } + U128 operator~() const + { + U128 t(*this); + t.lo = ~t.lo; + t.hi = ~t.hi; + return t; + } + + U128 &operator+=(const U128 &b) + { + if (lo > bmax - b.lo) + { + ++hi; + } + lo += b.lo; + hi += b.hi; + return *this; + } + U128 &operator-=(const U128 &b) + { + return *this += -b; + } + U128 &operator*=(const U128 &b) + { + if (*this == 0 || b == 1) + { + return *this; + } + if (b == 0) + { + lo = hi = 0; + return *this; + } + U128 a(*this); + U128 t = b; + lo = hi = 0; + for (size_t i = 0; i < sz; ++i) + { + if ((t & 1) != 0) + { + *this += (a << i); + } + t >>= 1; + } + return *this; + } + U128 &operator/=(const U128 &b) + { + U128 rem; + divide(*this, b, *this, rem); + return *this; + } + U128 &operator%=(const U128 &b) + { + U128 quo; + divide(*this, b, quo, *this); + return *this; + } + + static void + divide(const U128 &num, const U128 &den, U128 &quo, U128 &rem) + { + if (den == 0) + { + int a = 0; + quo = U128(a / a); + } + U128 n = num, d = den, x = 1, ans = 0; + while ((n >= d) && (((d >> (sz - 1)) & 1) == 0)) + { + x <<= 1; + d <<= 1; + } + while (x != 0) + { + if (n >= d) + { + n -= d; + ans |= x; + } + x >>= 1; + d >>= 1; + } + quo = ans, rem = n; + } + + U128 &operator&=(const U128 &b) + { + hi &= b.hi; + lo &= b.lo; + return *this; + } + U128 &operator|=(const U128 &b) + { + hi |= b.hi; + lo |= b.lo; + return *this; + } + U128 &operator<<=(const U128 &rhs) + { + size_t n = rhs.to_int(); + if (n >= sz) + { + lo = hi = 0; + return *this; + } + if (n >= hsz) + { + n -= hsz; + hi = lo; + lo = 0; + } + if (n != 0) + { + hi <<= n; + const u64 mask(~(u64(-1) >> n)); + hi |= (lo & mask) >> (hsz - n); + lo <<= n; + } + return *this; + } + U128 &operator>>=(const U128 &rhs) + { + size_t n = rhs.to_int(); + if (n >= sz) + { + lo = hi = 0; + return *this; + } + if (n >= hsz) + { + n -= hsz; + lo = hi; + hi = 0; + } + if (n != 0) + { + lo >>= n; + const u64 mask(~(u64(-1) << n)); + lo |= (hi & mask) << (hsz - n); + hi >>= n; + } + return *this; + } + + int to_int() const + { + return static_cast(lo); + } + u64 to_u64() const + { + return lo; + } + + bool operator==(const U128 &b) const + { + return hi == b.hi && lo == b.lo; + } + bool operator!=(const U128 &b) const + { + return !(*this == b); + } + bool operator<(const U128 &b) const + { + return (hi == b.hi) ? lo < b.lo : hi < b.hi; + } + bool operator>=(const U128 &b) const + { + return !(*this < b); + } + U128 operator&(const U128 &b) const + { + U128 a(*this); + return a &= b; + } + U128 operator<<(const U128 &b) const + { + U128 a(*this); + return a <<= b; + } + U128 operator>>(const U128 &b) const + { + U128 a(*this); + return a >>= b; + } + U128 operator*(const U128 &b) const + { + U128 a(*this); + return a *= b; + } + U128 operator+(const U128 &b) const + { + U128 a(*this); + return a += b; + } + U128 operator-(const U128 &b) const + { + U128 a(*this); + return a -= b; + } + U128 operator%(const U128 &b) const + { + U128 a(*this); + return a %= b; + } +}; + +template +struct Fraction +{ + T p, q; + + Fraction() + { + p = 0, q = 1; + } + Fraction(T P, T Q) : p(P), q(Q) + { + simplify(); + } + void simplify() + { + T g = gcd(p, q); + p /= g; + q /= g; + } + Fraction operator+(const Fraction &f) const + { + return Fraction(p * f.q + q * f.p, q * f.q); + } + Fraction operator-(const Fraction &f) const + { + return Fraction(p * f.q - q * f.p, q * f.q); + } + Fraction operator*(const Fraction &f) const + { + return Fraction(p * f.p, q * f.q); + } + Fraction operator/(const Fraction &f) const + { + return Fraction(p * f.q, q * f.p); + } +}; + +typedef Fraction Frac; + +int k1, k2; +const Frac one(1, 1); + +Frac solve(Frac &P) +{ + Frac Q = one - P; + Frac Pk2(sq_pow(P.p, k2), sq_pow(P.q, k2)); + Frac Qk1(sq_pow(Q.p, k1), sq_pow(Q.q, k1)); + Frac Pk2Qk1 = Pk2 * Qk1; + return (one - Qk1 - Pk2 + Pk2Qk1) / (P * Qk1 + Pk2 * Q - Pk2Qk1); +} + +int main() +{ + int T; + scanf("%d", &T); + int ncase = 0; + while (T--) + { + int p, q; + scanf("%d%d%d%d", &p, &q, &k1, &k2); + Frac P(p, q); + Frac ans = solve(P); + ans.simplify(); + printf("Case %d: ", ++ncase); + u64 x = ans.p.to_u64(); + u64 y = ans.q.to_u64(); + printf("%llu / %llu\n", x, y); + } + return 0; +} diff --git a/12039.cpp b/12039.cpp new file mode 100644 index 0000000..622ad26 --- /dev/null +++ b/12039.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +bool p[11000001]; +int pr[804580]; +int k = 0; + +long long f(int r) +{ + int ll, rr, mm; + long long ans = 0; + ll = 0, rr = k; + int j = pr[0]; + while (ll < rr) + { + mm = (ll + rr + 1) >> 1; + if (j + pr[mm] <= r) + { + ll = mm; + } + else + { + rr = mm - 1; + } + } + ans += ll + 0LL; + for (int i = 1;; ++i) + { + while ((pr[i] + pr[ll] > r) && ll > i) + { + --ll; + } + if (ll <= i) + { + break; + } + ans += ll - i + 0LL; + } + return ans; +} + +int main() +{ + int i, j; + for (i = 2; i * i <= 11000000; ++i) + if (!p[i]) + for (j = i * i; j <= 11000000; j += i) + { + p[j] = 1; + } + for (i = 3; i <= 11000000; i += 2) + if (!p[i]) + { + pr[k++] = i; + } + pr[k] = -1u >> 1; + int l, r; + long long s; + while (scanf("%d%d", &l, &r) && l) + { + s = f(r) - f(l - 1); + printf("%lld\n", s); + } + return 0; +} diff --git a/1204.cpp b/1204.cpp new file mode 100644 index 0000000..c1f9bb7 --- /dev/null +++ b/1204.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +#pragma comment(linker, "/STACK:102400000,102400000") + +#define ll long long +#define ull unsigned long long +#define eps 1e-8 +#define NMAX 201000 +#define MOD 1000000007 +#define lson l, mid, rt << 1 +#define rson mid + 1, r, rt << 1 | 1 +#define PI acos(-1) +template +inline void scan_d(T &ret) +{ + char c; + int flag = 0; + ret = 0; + while (((c = getchar()) < '0' || c > '9') && c != '-') + ; + if (c == '-') + { + flag = 1; + c = getchar(); + } + while (c >= '0' && c <= '9') + { + ret = ret * 10 + (c - '0'), c = getchar(); + } + if (flag) + { + ret = -ret; + } +} +int comp[17][2][17][2], len[17]; +char s[17][2][105]; +bool can[17]; +int dp[(1 << 16) + 10][17][2], n; + +int find_overlap(char *a, char *b, int len1, int len2) +{ + int p; + if (len1 > len2) + { + p = len1 - len2 + 1; + } + else + { + p = 1; + } + for (int i = p; i < len1; i++) + { + if (strncmp(a + i, b, len1 - i) == 0) + { + return len1 - i; + } + } + return 0; +} + +void init() +{ + char tmp[17][2][105]; + int tlen[17]; + for (int i = 0; i < n; i++) + { + scanf("%s", tmp[i][0]); + int m = strlen(tmp[i][0]); + strcpy(tmp[i][1], tmp[i][0]); + reverse(tmp[i][1], tmp[i][1] + m); + // reverse_copy(tmp[i][0],tmp[i][0]+m,tmp[i][1]); + // cout< j) + { + ok = 0; + } + break; + } + if (strncmp(tmp[i][0], tmp[j][1] + k, tlen[i]) == 0) + { + ok = 1; + if (k == 0 && tlen[i] == tlen[j] && i > j) + { + ok = 0; + } + break; + } + } + if (ok) + { + can[i] = 1; + break; + } + } + } + int nct = 0; + for (int i = 0; i < n; i++) + if (!can[i]) + { + memcpy(s[nct], tmp[i], sizeof(tmp[i])); + len[nct++] = tlen[i]; + } + n = nct; + for (int i = 0; i < n; i++) + for (int x = 0; x < 2; x++) + for (int j = 0; j < n; j++) + for (int y = 0; y < 2; y++) + { + comp[i][x][j][y] = find_overlap(s[i][x], s[j][y], len[i], len[j]); + } +} + +int main() +{ +#ifdef GLQ + freopen("input.txt", "r", stdin); +// freopen("o3.txt","w",stdout); +#endif + while (~scanf("%d", &n) && n) + { + init(); + memset(dp, -1, sizeof(dp)); + dp[1][0][0] = len[0]; + for (int s = 1; s < (1 << n); s++) + for (int i = 0; i < n; i++) + for (int j = 0; j < 2; j++) + if (dp[s][i][j] != -1) + for (int k = 0; k < n; k++) + if (!((1 << k) & s)) + { + int &d = dp[s | (1 << k)][k][0], &d2 = dp[s | (1 << k)][k][1]; + if (d == -1 || d > dp[s][i][j] + len[k] - comp[i][j][k][0]) + { + d = dp[s][i][j] + len[k] - comp[i][j][k][0]; + } + if (d2 == -1 || d2 > dp[s][i][j] + len[k] - comp[i][j][k][1]) + { + d2 = dp[s][i][j] + len[k] - comp[i][j][k][1]; + } + } + int ans = -1; + for (int i = 0; i < n; i++) + for (int j = 0; j < 2; j++) + if (dp[(1 << n) - 1][i][j] > 0) + { + if (ans == -1 || ans > dp[(1 << n) - 1][i][j] - comp[i][j][0][0]) + { + ans = dp[(1 << n) - 1][i][j] - comp[i][j][0][0]; + } + } + if (ans < 2) + { + ans = 2; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12040.cpp b/12040.cpp new file mode 100644 index 0000000..1160df1 --- /dev/null +++ b/12040.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define pb push_back +#define PI acos(-1.0) +#define SZ(a) (int)a.size() +#define csprnt printf("Case %d: ", cas++); +#define EPS 1e-9 +#define MAX 100010 +#define ll long long +#define INF (1 << 30) +#define pii pair +#define MP make_pair +#define mod 10000007 +int xx[] = {1, 1, 0, -1, -1, -1, 0, 1}, yy[] = {0, 1, 1, 1, 0, -1, -1, -1};//eight direction + +int n, lim; +string inp; +int pi[105]; +int dp[105][105]; + +void prefix_func() +{ + int i, j, k; + pi[0] = 0; + k = 0; + for (i = 1; i < SZ(inp); i++) + { + while (k > 0 && inp[i] != inp[k]) + { + k = pi[k - 1]; + } + if (inp[i] == inp[k]) + { + k++; + } + pi[i] = k; + } + return; +} + +int rec(int now, int k) +{ + int i, j; + int &ret = dp[now][k]; + if (ret != -1) + { + return ret; + } + if (k == lim) + { + return 0; + } + if (now == n) + { + return 1; + } + ret = 0; + int st = 0, nowk; + if (now == 0 && n > 1) + { + st = 1; + } + for (i = st; i <= 9; i++) + { + nowk = k; + while (nowk > 0 && i != (inp[nowk] - '0')) + { + nowk = pi[nowk - 1]; + } + if (i == (inp[nowk] - '0')) + { + nowk++; + } + ret = (ret + rec(now + 1, nowk)) % mod; + } + return ret; +} + +int main() +{ + int t, cas = 1; + scanf("%d", &t); + while (t--) + { + scanf("%d ", &n); + cin >> inp; + lim = SZ(inp); + prefix_func(); + memset(dp, -1, sizeof dp); + int ans = rec(0, 0); + cout << ans << endl; + } + return 0; +} diff --git a/12041.cpp b/12041.cpp new file mode 100644 index 0000000..dea71b2 --- /dev/null +++ b/12041.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +// link: http://uva.onlinejudge.org/external/120/12041.html +// Runtime: 0.080s +// Tag: Recursive, Dp + +/* + * File: main.cpp + * Author: shahab + * + * Created on August 5, 2011, 8:58 PM +*/ + +long long fibo[50]; +string bfs[50]; + +void generateFibo() +{ + fibo[0] = 1; + fibo[1] = 1; + for (int i = 2; i <= 50; i++) + { + fibo[i] = fibo[i - 1] + fibo[i - 2]; + } +} + +int bfsOfPosition(int n, long long pos) +{ + if (n < 31) + { + return bfs[n][pos] - '0'; + } + if (pos < fibo[n - 2]) + { + return bfsOfPosition(n - 2, pos); + } + else + { + return bfsOfPosition(n - 1, pos - fibo[n - 2]); + } +} + +void generateBfs() +{ + bfs[0] = "0"; + bfs[1] = "1"; + for (int i = 2; i < 31; i++) + { + bfs[i] = bfs[i - 2] + bfs[i - 1]; + } +} + +int main(int argc, char **argv) +{ + generateFibo(); + generateBfs(); + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + long long n, start, end; + scanf("%lld %lld %lld", &n, &start, &end); + if (n > 48) + { + n = n % 2 ? 47 : 48; + } + if (n < 31) + for (long long i = start; i <= end; i++) + { + printf("%c", bfs[n][i]); + } + else + for (long long i = start; i <= end; i++) + { + printf("%d", bfsOfPosition(n, i)); + } + printf("\n"); + } + return 0; +} diff --git a/12042.cpp b/12042.cpp new file mode 100644 index 0000000..ab47ffb --- /dev/null +++ b/12042.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define sz(x) (int)((x).size()) +#define rep(i, n) for (int i = 0; i < n; i++) +#define mp make_pair +#define pb push_back +#define all(x) x.begin(), x.end() +#define clr(x) memset((x), 0, sizeof(x)) + +typedef long long ll; +typedef vector vi; +typedef vector vl; +typedef vector vs; +typedef pair pii; +typedef vector vpi; +const int maxn = 66; +const ll mod = 1000000007; +int s; +struct Mat +{ + ll mat[maxn][maxn]; + void clear() + { + memset(mat, 0, sizeof(mat)); + } +} A, ans, E, M; +Mat operator*(Mat a, Mat b) +{ + Mat c; + memset(c.mat, 0, sizeof(c.mat)); + for (int i = 0; i < s; i++) + { + for (int j = 0; j < s; j++) + { + //if (a.mat[i][j]==0)continue; + for (int k = 0; k < s; k++) + { + c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; + c.mat[i][j] %= mod; + } + } + } + return c; +} +Mat operator+(Mat a, Mat b) +{ + Mat c; + memset(c.mat, 0, sizeof(Mat)); + for (int i = 0; i < s; i++) + for (int j = 0; j < s; j++) + c.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod; + return c; +} +Mat operator^(Mat A, int x)//ó??è??μí?óà¨o? +{ + Mat c = E; + for (; x; x >>= 1) + { + if (x & 1) + c = c * A; + A = A * A; + } + return c; +} +ll a[66], aa[66]; +int main() +{ + int i, j, k, n; + for (i = 0; i < maxn; i++) + E.mat[i][i] = 1; + int cas; + scanf("%d", &cas); + while (cas--) + { + int d; + scanf("%d%d", &n, &d); + for (i = 0; i < n; i++) + scanf("%lld", a + i); + s = n; + A.clear(); + for (i = 1; i < n; i++) + { + for (j = 0; j < i; j++) + { + A.mat[i][j] = 1; + } + } + for (i = 0; i < n; i++) + { + A.mat[i][n - 1] = 1; + } + /* + for (i=0;i + +using namespace std; + +#define FORS(i, f, s, n) for (int i = f; i < n; i += s) +const int MAX = 100001; +int memonod[MAX], memosod[MAX]; +template +_T sqr(_T n) +{ + return n * n; +} +template +_T pwr(_T b, _T e)// returns b^e +{ + for (_T r = 1;; b = sqr(b)) + { + if (e & 1) + { + r = r * b; + } + if (!(e >>= 1)) + { + return r; + } + } +} +template +void reduce(_T &n, _T d, _T &r, bool nod) +{ + // Requires: pwr + _T c = 0; + if (n % d) + { + return; + } + do + { + n /= d; + ++c; + } while (!(n % d)); + if (nod) + { + r = r * (c + 1); + } + else + { + r = (r * (pwr(d, c + 1) - 1)) / (d - 1); + } +} +template +_T nod(_T a)// returns number of divisors +{ + // Requires: reduce + if (memonod[a] != -1) + { + return memonod[a]; + } + _T r = 1, n = a; + reduce(a, (_T)2, r, true); + reduce(a, (_T)3, r, true); + for (_T i = 6;; i += 6) + { + _T t1 = i + 1, t2 = i - 1; + reduce(a, t1, r, true); + reduce(a, t2, r, true); + if (sqr(t2) > a) + { + break; + } + } + if (a != 1) + { + r <<= 1; + } + return memonod[n] = r; +} +template +_T sod(_T a)// returns sum of divisors +{ + // Requires: reduce + if (memosod[a] != -1) + { + return memosod[a]; + } + _T n = a, r = 1; + reduce(a, (_T)2, r, false); + reduce(a, (_T)3, r, false); + for (_T i = 6;; i += 6) + { + _T t1 = i + 1, t2 = i - 1; + reduce(a, t1, r, false); + reduce(a, t2, r, false); + if (sqr(t2) > a) + { + break; + } + } + if (a != 1) + { + r *= (a + 1); + } + return memosod[n] = r; +} +int main() +{ + int t; + scanf("%d", &t); + FORS(i, 0, 1, MAX) + memonod[i] = memosod[i] = -1; + for (int a, b, k; t-- && scanf("%d %d %d", &a, &b, &k) == 3;) + { + int start = ((a + k - 1) / k) * k; + long long int g = 0, h = 0; + ++b; + FORS(i, start, k, b) + { + g += nod(i); + h += sod(i); + } + printf("%lld %lld\n", g, h); + } + return 0; +} diff --git a/12045.cpp b/12045.cpp new file mode 100644 index 0000000..cbeddc0 --- /dev/null +++ b/12045.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +#define Zero(v) memset(v, 0, sizeof(v)) + +#define MOD 1000000007 + +typedef long long i64; + +// Matrix Exponentiation +typedef i64 t_m; +#define MAXR 2 +#define MAXC 2 +struct Matrix +{ + int r, c; + t_m m[MAXR][MAXC]; + void init(int R, int C) + { + Zero(m); + r = R; + c = C; + } +}; + +bool modflg; + +void matrix_mul(const Matrix &a, const Matrix &b, Matrix &c) +{ + c.r = a.r, c.c = b.c; + t_m x; + for (int i = 0; i < c.r; ++i) + for (int j = 0; j < c.c; ++j) + { + x = 0; + for (int k = 0; k < a.c; ++k) + { + x += a.m[i][k] * b.m[k][j]; + } + if (x >= MOD) + { + modflg = true; + } + c.m[i][j] = x % MOD; + } +} + +void matrix_exp(const Matrix &m, i64 e, Matrix &r) +{ + if (e == 1) + { + r = m; + return; + } + Matrix x; + if (e % 2 == 0) + { + matrix_exp(m, e / 2, x); + matrix_mul(x, x, r); + return; + } + matrix_exp(m, e - 1, x); + matrix_mul(x, m, r); +} + +Matrix O, R; +i64 N, M, X, Y, K; +i64 sol; + +void find_coefficients(i64 step, i64 &c1, i64 &c2) +{ + if (step == 1) + { + c1 = 1; + c2 = 1; + return; + } + matrix_exp(O, step - 1, R); + c1 = R.m[0][0] + R.m[1][0]; + c2 = R.m[0][1] + R.m[1][1]; +} + +bool solve() +{ + /* + * Let a(i) be the number of a's of the ith string, and b(i) the number + * of b's. We'll find a(1) and b(1) from the following system of equations: + * + * c1 * a(1) + c2 * b(1) = X + * c3 * a(1) + c4 * b(1) = Y + */ + i64 c1, c2, c3, c4; + modflg = false; + find_coefficients(N, c1, c2); + find_coefficients(M, c3, c4); + if (modflg) + { + return false; + } + i64 det_a = X * c4 - Y * c2; + i64 det_b = c1 * Y - c3 * X; + i64 det_den = c1 * c4 - c2 * c3; + if (det_a % det_den != 0 || det_b % det_den != 0) + { + return false; + } + i64 a1 = det_a / det_den; + i64 b1 = det_b / det_den; + if (a1 < 0 || b1 < 0) + { + return false; + } + if (K == 1) + { + sol = (a1 + b1) % MOD; + return true; + } + matrix_exp(O, K - 1, R); + sol = (R.m[0][0] + R.m[1][0]) * a1 + (R.m[0][1] + R.m[1][1]) * b1; + sol %= MOD; + return true; +} + +void init() +{ + O.init(2, 2); + O.m[0][0] = 0; + O.m[0][1] = 1; + O.m[1][0] = 1; + O.m[1][1] = 1; +} + +int main() +{ + init(); + int T; + scanf("%d", &T); + while (T--) + { + scanf("%lld%lld%lld%lld%lld", &N, &X, &M, &Y, &K); + if (solve()) + { + printf("%lld\n", sol); + } + else + { + puts("Impossible"); + } + } + return 0; +} diff --git a/12046.cpp b/12046.cpp new file mode 100644 index 0000000..03db941 --- /dev/null +++ b/12046.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +// http://uva.onlinejudge.org/external/120/12046.html +// Runtime: 0.556s +// Tag: Dp + +// @BEGIN_OF_SOURCE_CODE + +#define LL long long +#define MOD 1000007 + +int memo[40 + 2][2 + 1][3 + 1][4 + 1][5 + 1][6 + 1][64 + 2]; +int n; + +int dp(int at, int m2, int m3, int m4, int m5, int m6, int mask) +{ + if (at == n) + { + if (mask & (1 << 1) && m2) + { + return 0; + } + if (mask & (1 << 2) && m3) + { + return 0; + } + if (mask & (1 << 3) && m4) + { + return 0; + } + if (mask & (1 << 4) && m5) + { + return 0; + } + if (mask & (1 << 5) && m6) + { + return 0; + } + return 1; + } + if (memo[at][m2][m3][m4][m5][m6][mask] != -1) + { + return memo[at][m2][m3][m4][m5][m6][mask]; + } + int sum = 0; + int t2, t3, t4, t5, t6; + if (at == 0) + { + for (int i = 1; i <= 6; i++) + { + sum += dp(at + 1, i % 2, i % 3, i % 4, i % 5, i % 6, mask | (1 << (i - 1))); + sum %= MOD; + } + return memo[at][m2][m3][m4][m5][m6][mask] = sum; + } + for (int i = 1; i <= 6; i++) + { + t2 = (m2 * 10 + i) % 2; + t3 = (m3 * 10 + i) % 3; + t4 = (m4 * 10 + i) % 4; + t5 = (m5 * 10 + i) % 5; + t6 = (m6 * 10 + i) % 6; + sum += dp(at + 1, t2, t3, t4, t5, t6, mask | (1 << (i - 1))); + sum %= MOD; + } + return memo[at][m2][m3][m4][m5][m6][mask] = sum; +} + +int main() +{ + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + scanf("%d", &n); + memset(memo, -1, sizeof(memo)); + printf("%d\n", dp(0, 0, 0, 0, 0, 0, 0)); + } + return 0; +} + +// @END_OF_SOURCE_CODE diff --git a/12047.cpp b/12047.cpp new file mode 100644 index 0000000..b9758aa --- /dev/null +++ b/12047.cpp @@ -0,0 +1,160 @@ +#include +#include + +using namespace std; + +const int INF = 10000 * 100000 + 100; + +#define For(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i) + +typedef unsigned int u32; + +typedef pair II; +typedef vector IV; +typedef vector BV; + +struct Reader +{ + char b; + Reader() + { + read(); + } + void read() + { + int r = getchar_unlocked(); + b = r == EOF ? 0 : r; + } + void skip() + { + while (b > 0 && b <= 32) + { + read(); + } + } + u32 next_u32() + { + u32 v = 0; + for (skip(); b > 32; read()) + { + v = v * 10 + b - 48; + } + return v; + } +}; + +typedef int w_t; +struct Graph +{ + struct Edge + { + int v; + w_t w; + Edge(int V, w_t W) : v(V), w(W) {} + }; + typedef list EL; + typedef vector ELV; + ELV adj; + int n; + Graph(int N) : n(N) + { + adj.resize(n); + } + void add(int u, int v, w_t w) + { + adj[u].push_back(Edge(v, w)); + } + + void dijkstra(int src, IV &dis) + { + set q; + dis = IV(n, INF); + BV flg(n); + q.insert(II(0, src)); + dis[src] = 0; + while (!q.empty()) + { + II p = *(q.begin()); + q.erase(q.begin()); + int d = p.first, v = p.second; + if (flg[v]) + { + continue; + } + flg[v] = true; + For(EL, e, adj[v]) + { + int d2 = d + e->w; + if (d2 < dis[e->v]) + { + dis[e->v] = d2; + q.insert(II(d2, e->v)); + } + } + } + } +}; + +struct FullEdge +{ + int u, v, w; + FullEdge(int U, int V, int W) : u(U), v(V), w(W) {} +}; +typedef vector FV; + +int N, M, s, t, p; + +int solve(Graph &fg, Graph &rg, FV &edges) +{ + IV dist_orig; + IV dist_dest; + fg.dijkstra(s, dist_orig); + rg.dijkstra(t, dist_dest); + int ans = -1; + For(FV, e, edges) + { + int d1 = dist_orig[e->u]; + int d2 = dist_dest[e->v]; + if (d1 == INF || d2 == INF) + { + continue; + } + if (d1 + e->w + d2 <= p && e->w > ans) + { + ans = e->w; + } + } + return ans; +} + +int main() +{ + Reader rr; + int T = rr.next_u32(); + while (T--) + { + N = rr.next_u32(); + M = rr.next_u32(); + s = rr.next_u32(); + t = rr.next_u32(); + p = rr.next_u32(); + --s; + --t; + Graph fg(N);// forward graph + Graph rg(N);// reverse graph + FV edges; + while (M--) + { + int u = rr.next_u32(); + int v = rr.next_u32(); + int w = rr.next_u32(); + --u; + --v; + fg.add(u, v, w); + rg.add(v, u, w); + edges.push_back(FullEdge(u, v, w)); + } + printf("%d\n", solve(fg, rg, edges)); + } + return 0; +} diff --git a/12048.cpp b/12048.cpp new file mode 100644 index 0000000..9caf3cd --- /dev/null +++ b/12048.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +#define eps 1e-10 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + return y < a.y; + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator/(const double val) const + { + return Pt(x / val, y / val); + } + Pt operator*(const double val) const + { + return Pt(x * val, y * val); + } +}; +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +int monotone(int n, Pt p[], Pt ch[]) +{ + sort(p, p + n); + int i, m = 0, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + m--; + ch[m++] = p[i]; + } + for (i = n - 1, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + m--; + ch[m++] = p[i]; + } + return m - 1; +} + +double g(Pt a, Pt b, double x) +{ + Pt vab = b - a; + return a.y + vab.y * (x - a.x) / vab.x; +} +int inside_convex(const Pt &p, Pt ch[], int n) +{ + if (n < 3) + return false; + if (cross(ch[0], p, ch[1]) > eps) + return false; + if (cross(ch[0], p, ch[n - 1]) < -eps) + return false; + + int l = 2, r = n - 1; + int line = -1; + while (l <= r) + { + int mid = (l + r) >> 1; + if (cross(ch[0], p, ch[mid]) > -eps) + { + line = mid; + r = mid - 1; + } + else + l = mid + 1; + } + return cross(ch[line - 1], p, ch[line]) < eps; +} +Pt D[131072], ch[262144]; +int main() +{ + int testcase, n, m; + double x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x, &y); + ch[i] = Pt(x, y); + } + // n = monotone(n, D, ch); + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%lf %lf", &x, &y); + int f = inside_convex(Pt(x, y), ch, n); + puts(f ? "y" : "n"); + } + } + return 0; +} +/* + 1 + 3 + 0 0 + 10 0 + 5 5 + 4 + 0 0 + 1 2 + 5 6 + 4 3 + */ \ No newline at end of file diff --git a/12049.cpp b/12049.cpp index 59b2a1b..b893b02 100644 --- a/12049.cpp +++ b/12049.cpp @@ -1,55 +1,60 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - scanf("%d", &T); - while (T--) { - int M, N, I, i; - scanf("%d%d", &M, &N); - map Map; - map::iterator it; - for (i = 0; i < M; i++) { - scanf("%d", &I); - Map[I] += 1; +int main() +{ + int t; + int n, m; + int p1, p2; + int list1[10001], list2[10001]; + cin >> t; + while (t--) + { + cin >> n >> m; + int result = 0; + for (int i = 0; i < n; i++) + { + cin >> list1[i]; + } + for (int i = 0; i < m; i++) + { + cin >> list2[i]; } - for (i = 0; i < N; i++) { - scanf("%d", &I); - Map[I] -= 1; + sort(list1, list1 + n); + sort(list2, list2 + m); + p1 = p2 = 0; + while (p1 < n || p2 < m) + { + if (p1 == n) + { + result++; + p2++; + continue; + } + if (p2 == m) + { + result++; + p1++; + continue; + } + if (list1[p1] < list2[p2]) + { + result++; + p1++; + } + else if (list1[p1] > list2[p2]) + { + result++; + p2++; + } + else + { + p1++; + p2++; + } } - int tot = 0; - for (it = Map.begin(); it != Map.end(); it++) - tot += abs((*it).second); - printf("%d\n", tot); + cout << result << endl; } return 0; } diff --git a/1205.cpp b/1205.cpp new file mode 100644 index 0000000..812bf98 --- /dev/null +++ b/1205.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +//1205 +//Color a Tree +//Graphs;Job Scheduling + +#define MAX 1008 + +struct Cost +{ + int a, b, t, v; + Cost() {} + Cost(int a, int b, int t, int v) : a(a), b(b), t(t), v(v) {} + + inline bool operator<(const Cost &c) const + { + int cra = a * c.t, crb = c.a * t; + if (cra != crb) + { + return cra > crb; + } + return v < c.v; + } +}; + +int P[MAX], M[MAX]; +set S; +Cost C[MAX]; + +int findParent(int v) +{ + if (M[v] == v) + { + return v; + } + return M[v] = findParent(M[v]); +} + +int main() +{ + int n, r; + while (cin >> n >> r, n | r) + { + for (int i = 1; i <= n; i++) + { + int a; + cin >> a; + P[i] = 0; + M[i] = i; + C[i] = *S.insert(Cost(a, a, 1, i)).first; + } + for (int i = 1; i <= n - 1; i++) + { + int u, v; + cin >> u >> v; + P[v] = u; + } + int total = 0, time = 0; + while (!S.empty()) + { + Cost c = *S.begin(); + int pid = findParent(P[c.v]); + if (pid == 0) + { + total += time * c.a + c.b; + time += c.t; + S.erase(c); + M[c.v] = 0; + } + else + { + Cost d = *S.find(C[pid]); + Cost e(c.a + d.a, c.b + d.b + c.a * d.t, c.t + d.t, d.v); + S.erase(c); + S.erase(d); + S.insert(e); + M[c.v] = d.v; + C[e.v] = e; + } + } + cout << total << endl; + } + return 0; +} diff --git a/12050.cpp b/12050.cpp new file mode 100644 index 0000000..9d68770 --- /dev/null +++ b/12050.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#ifdef WIN32 +typedef long data; +#define bmax 30 +#else +typedef long long data; +#define bmax 62 +#endif +#define for if (0); else for + +int n; +data p10[bmax]; + +void indx(int t, int ind) +{ + int f = 0; + if (t % 2 == 1) + { + t++, f = 1; + } + int x = t / 2; + data res = p10[x - 1] + ind - 1; + cout << res; + if (!f) + { + cout << res % 10; + } + res /= 10; + while (res) + { + cout << res % 10; + res /= 10; + } + cout << endl; +} +data iall(int t) +{ + if (t == 1) + { + return 9; + } + if (t % 2 == 1) + { + return iall(t - 1) * 10; + } + else + { + int x = t / 2; + return p10[x] - p10[x - 1]; + } +} +void done() +{ + int m = n, t = 0; + while (m > 0) + { + t++; + int m1 = m - iall(t); + if (m1 <= 0) + { + break; + } + m = m1; + } + indx(t, m); +} +int main() +{ + p10[0] = 1; + for (int i = 1; i < bmax; ++i) + { + p10[i] = p10[i - 1] * 10; + } + /* for (int i = 1; i < 10; ++i) + cout << iall(i) <> n && n) + { + done(); + } + return 0; +} diff --git a/12051.cpp b/12051.cpp new file mode 100644 index 0000000..91c791c --- /dev/null +++ b/12051.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +typedef long long LL; + +int DIM, R, cc = 0, x[8], n[8], d[8], r[8]; +LL a[3000001]; + +int hash_(int x[]) +{ + int res = 0; + for (int i = 0; i < DIM; ++i) + { + res += x[i] * d[i]; + } + return res; +} +void unhash_(int x, int r[]) +{ + for (int i = DIM - 1; i >= 0; --i) + { + r[i] = x / d[i]; + x %= d[i]; + } +} +LL solve() +{ + if (a[0] == -1 || a[d[DIM] - 1] == -1) + { + return 0; + } + a[0] = 1; + memset(r, 0, 8); + for (int i = 1; i < d[DIM]; ++i) + { + if (a[i] != -1) + { + unhash_(i, r); + for (int j = 0; j < DIM; ++j) + { + if (r[j]) + { + int x = i - d[j]; + if (a[x] != -1) + { + a[i] += a[x]; + } + } + } + } + } + return a[d[DIM] - 1]; +} + +int main() +{ + while (scanf("%d%d", &DIM, &R) != EOF && DIM != 0 && R != 0) + { + // read in dest coord = (l1-1,l2-1...) + for (int i = 0; i < DIM; n[i]++, ++i) + { + scanf("%d", &n[i]); + } + // compute # cubes in each dimension + for (int i = 0; i <= DIM; ++i) + { + d[i] = 1; + for (int j = 0; j < i; ++j) + { + d[i] *= n[j]; + } + } + // read in R * opaque blocks + memset(a, 0, sizeof(a)); + for (int i = 0; i < R; ++i) + { + // for each opaque block + for (int j = 0; j < DIM; ++j) + { + scanf("%d", &x[j]); + } + a[hash_(x)] = -1; // mark as 'blocking' + } + printf("Case %d: %lld\n", ++cc, solve()); + } + return 0; +} diff --git a/12052.cpp b/12052.cpp new file mode 100644 index 0000000..7b3db6a --- /dev/null +++ b/12052.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define for if (0); else for + +int n, K, p; +int d[16][16]; +int bit[16]; +int a[16]; + +void done() +{ + int res = 0; + for (int c = 0; c < (1 << n); ++c) + { + int ft = 0; + for (int i = 0; i < n; ++i) + { + if (bit[i] & c) + { + a[i] = 1, ft++; + } + else + { + a[i] = 0; + } + } + if (ft == K) + { + int f = 1; + for (int i = 0; i < n; ++i) + { + if (a[i] == 0) + { + int x = 0; + for (int j = 0; j < n; ++j) + { + if (d[i][j] && a[j] == 1) + { + x++; + } + } + if (x > 1) + { + f = 0; + break; + } + } + } + if (f) + { + res++; + } + } + } + cout << res << endl; +} +int main() +{ + for (int i = 0; i < 16; ++i) + { + bit[i] = 1 << i; + } + string s; + while (cin >> s) + { + cout << s << endl; + if (s == "TheEnd") + { + break; + } + cin >> n >> K >> p; + memset(d, 0, sizeof(d)); + for (int i = 0; i < p; ++i) + { + cin >> s; + d[s[0] - 'A'][s[1] - 'A'] = 1; + d[s[1] - 'A'][s[0] - 'A'] = 1; + } + done(); + } + return 0; +} diff --git a/12054.cpp b/12054.cpp new file mode 100644 index 0000000..217ce84 --- /dev/null +++ b/12054.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define for if (0); else for + +int plist[30] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; +int pn = 15; +int d[15], res[100]; +int pc[51][15]; +int n, F; +int e[100][100]; + +void out(int t) +{ + memset(e, 0, sizeof(e)); + int x = 0; + for (int c = 0; c < t - 1; ++c) + { + int df = x + res[c]; + for (int i = x; i < x + res[c]; ++i) + { + e[i][df] = 1; + df++; + if (df >= x + res[c] + res[c + 1]) + { + df = x + res[c]; + } + } + x += res[c]; + } + for (int i = 0; i < n; ++i) + { + int x = 0; + for (int j = 0; j < n; ++j) + if (e[i][j] == 1) + { + x++; + } + cout << x; + for (int j = 0; j < n; ++j) + if (e[i][j] == 1) + { + cout << " " << j + 1; + } + cout << endl; + } +} +void done(int t, int left, int last, int a[15]) +{ + if (F) + { + return; + } + for (int i = 0; i < 15; ++i) + if (a[i] < 0) + { + return; + } + if (left < 0) + { + return; + } + int i; + for (i = 0; i < 15; ++i) + if (a[i]) + { + break; + } + if (i >= 15 && left == 0) + { + out(t); + F = 1; + } + int at[15]; + for (int i = last; i >= 1; --i) + { + memcpy(at, a, sizeof(int) * 15); + for (int j = 0; j < 15; ++j) + { + at[j] -= pc[i][j]; + } + res[t] = i; + done(t + 1, left - i, i, at); + } +} +int main() +{ + for (int i = 1; i < 51; ++i) + { + memcpy(pc[i], pc[i - 1], sizeof(pc[i - 1])); + int x = i; + for (int j = 0; j < 15; ++j) + { + while (x % plist[j] == 0) + { + x /= plist[j]; + pc[i][j]++; + } + } + } + int tc, R, x1, x2; + cin >> tc; + for (int t = 1; t <= tc; ++t) + { + cout << "Case#" << t << ":" << endl; + cin >> n >> R; + memset(d, 0, sizeof(d)); + for (int i = 0; i < R; ++i) + { + cin >> x1 >> x2; + for (int j = 0; j < pn; ++j) + if (x1 == plist[j]) + { + d[j] += x2; + } + } + F = 0; + done(0, n, 50, d); + } + return 0; +} diff --git a/12057.cpp b/12057.cpp new file mode 100644 index 0000000..db0a0b9 --- /dev/null +++ b/12057.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +#define for if (0); else for + +double a[110]; +int p[110][110]; +double e[110][110]; +int n; +string s[110]; + +void work(int ge, int b) +{ + if (ge <= 1) + { + return; + } + int k = p[ge][b]; + for (int i = b; i < b + ge; ++i) + { + if (i < b + k) + { + s[i] = s[i] + '0'; + } + else + { + s[i] = s[i] + '1'; + } + } + work(k, b); + work(ge - k, b + k); +} +void done() +{ + for (int j = 0; j < n; ++j) + { + e[1][j] = a[j]; + } + for (int i = 2; i <= n; ++i) + { + for (int j = 0; j <= n - i; ++j) + { + double rt = 1e30; + for (int k = 1; k < i; ++k) + { + double t = e[k][j] + e[i - k][j + k]; + for (int l = 0; l < i; ++l) + { + t += a[j + l]; + } + if (t < rt) + { + rt = t; + p[i][j] = k; + } + } + e[i][j] = rt; + } + } + cout << e[n][0] - 1 << endl; + for (int i = 0; i < n; ++i) + { + s[i] = ""; + } + work(n, 0); + for (int i = 0; i < n; ++i) + { + cout << s[i] << endl; + } +} +int main() +{ + cout.setf(ios::fixed); + cout.precision(4); + int f = 0; + while (cin >> n && n) + { + if (f) + { + cout << endl; + } + else + { + f = 1; + } + for (int i = 0; i < n; ++i) + { + cin >> a[i]; + } + if (n == 1) + { + cout << "1.0000" << endl + << "0" << endl; + } + else + { + done(); + } + } + return 0; +} diff --git a/12058.cpp b/12058.cpp new file mode 100644 index 0000000..b884592 --- /dev/null +++ b/12058.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define MAXV 0x3f3f3f3f +#define MAXE 1048576 +#define MAXC 1048576 +#define MAXR 1024 +struct DacingLinks +{ + int left, right; + int up, down; + int ch, rh; + int data;// extra info +} DL[MAXE]; +int s[MAXC], o[MAXR], head, size, Ans, findflag; +void Remove(int c) +{ + static int i; + for (i = DL[c].down; i != c; i = DL[i].down) + { + DL[DL[i].right].left = DL[i].left; + DL[DL[i].left].right = DL[i].right; + s[DL[i].ch]--; + } +} +void Resume(int c) +{ + static int i; + for (i = DL[c].down; i != c; i = DL[i].down) + { + DL[DL[i].right].left = i; + DL[DL[i].left].right = i; + s[DL[i].ch]++; + } +} +int used[MAXC] = {}; +int H() +{ + static int c, ret, i, j, time = 0; + for (c = DL[head].right, ++time, ret = 0; c != head; c = DL[c].right) + { + if (used[c] != time) + { + ret++, used[c] = time; + for (i = DL[c].down; i != c; i = DL[i].down) + for (j = DL[i].right; j != i; j = DL[j].right) + used[DL[j].ch] = time; + } + } + return ret; +} +void DFS(int k) +{ + if (k + H() >= Ans) + return; + if (DL[head].right == head) + { + if (k < Ans) + { + Ans = k, findflag = 1; + printf("yes"); + for (int i = 0; i < k; i++) + printf(" %d", DL[o[i]].data); + puts(""); + } + return; + } + int t = MAXV, c, i, j; + for (i = DL[head].right; i != head; i = DL[i].right) + { + if (s[i] < t) + { + t = s[i], c = i; + } + } + for (i = DL[c].down; i != c; i = DL[i].down) + { + o[k] = i; + Remove(i); + for (j = DL[i].right; j != i; j = DL[j].right) + Remove(j); + DFS(k + 1); + for (j = DL[i].left; j != i; j = DL[j].left) + Resume(j); + Resume(i); + if (findflag) + break; + } +} +int new_node(int up, int down, int left, int right) +{ + assert(size < MAXE); + DL[size].up = up, DL[size].down = down; + DL[size].left = left, DL[size].right = right; + DL[up].down = DL[down].up = DL[left].right = DL[right].left = size; + return size++; +} +void addrow(int n, int Row[], int data) +{ + int a, r, row = -1, k; + for (a = 0; a < n; a++) + { + r = Row[a]; + DL[size].ch = r, s[r]++; + DL[size].data = data; + if (row == -1) + { + row = new_node(DL[DL[r].ch].up, DL[r].ch, size, size); + DL[row].rh = a; + } + else + { + k = new_node(DL[DL[r].ch].up, DL[r].ch, DL[row].left, row); + DL[k].rh = a; + } + } +} +void init(int m) +{ + size = 0; + head = new_node(0, 0, 0, 0); + int i; + for (i = 1; i <= m; i++) + { + new_node(i, i, DL[head].left, head); + DL[i].ch = i, s[i] = 0; + } +} +int main() +{ + int testcase, cases = 0; + int n, m, k, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &n, &m, &k); + vector g[1024]; + for (int i = 1; i <= m; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(i); + g[y].push_back(i); + } + assert(m < MAXC); + init(m); + int row[1024]; + for (int i = 1; i <= n; i++) + { + sort(g[i].begin(), g[i].end()); + for (int j = 0; j < g[i].size(); j++) + row[j] = g[i][j]; + addrow(g[i].size(), row, i); + } + printf("Case #%d: ", ++cases); + Ans = k + 1, findflag = 0; + DFS(0); + if (Ans == k + 1) + puts("no"); + } + return 0; +} diff --git a/1206.cpp b/1206.cpp new file mode 100644 index 0000000..43dd89a --- /dev/null +++ b/1206.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define maxP 100000 + +typedef struct +{ + double x, y; +} Point; +Point P[maxP], CH[maxP << 1]; +int cmp(const void *i, const void *j) +{ + Point *a, *b; + a = (Point *)i, b = (Point *)j; + if (a->y != b->y) + { + return a->y - b->y > 0 ? 1 : -1; + } + return a->x - b->x > 0 ? 1 : -1; +} +double cross(Point *o, Point *a, Point *b) +{ + return (a->x - o->x) * (b->y - o->y) - + (a->y - o->y) * (b->x - o->x); +} +void convexHull(int n) +{ + qsort(P, n, sizeof(Point), cmp); + int i, m = 0, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(&CH[m - 2], &CH[m - 1], &P[i]) <= 0) + { + m--; + } + CH[m++] = P[i]; + } + for (i = n - 1, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(&CH[m - 2], &CH[m - 1], &P[i]) <= 0) + { + m--; + } + CH[m++] = P[i]; + } + for (i = 0; i < m; i++) + { + if (i) + { + printf(" "); + } + printf("(%g,%g)", CH[i].x, CH[i].y); + } + puts(""); +} +int main() +{ + string line; + double x, y; + char ch; + while (getline(cin, line)) + { + stringstream sin(line); + int n = 0; + while (sin >> ch >> x >> ch >> y >> ch) + { + P[n].x = x, P[n].y = y; + n++; + } + convexHull(n); + } + return 0; +} diff --git a/12060.cpp b/12060.cpp new file mode 100644 index 0000000..d62bf4f --- /dev/null +++ b/12060.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +int num_length(int n) +{ + return int(log10(n)) + 1; +} + +int gcd(int a, int b) +{ + while (b) + { + a %= b; + swap(a, b); + } + return a; +} + +int main() +{ + int n, sum, tmp, case_num = 0; + bool negative; + while (cin >> n) + { + if (n == 0) + { + break; + } + case_num++; + sum = 0; + for (int i = 0; i < n; i++) + { + cin >> tmp; + sum += tmp; + } + if (sum < 0) + { + negative = true; + sum = -sum; + } + else + { + negative = false; + } + cout << "Case " << case_num << ":" << endl; + if (sum % n == 0) + { + if (negative) + { + cout << "- "; + } + cout << sum / n << endl; + } + else + { + int offset = 0; + if (negative) + { + offset += 2; + } + int int_v, frac_v; + if (sum > n) + { + // int value + int_v = sum / n; + offset += num_length(int_v); + // fractional value + frac_v = sum - (n * int_v); + int div = gcd(frac_v, n); + frac_v /= div; + n /= div; + } + else + { + int_v = 0; + frac_v = sum; + int div = gcd(frac_v, n); + frac_v /= div; + n /= div; + } + for (int i = 0; i < offset + num_length(n) - num_length(frac_v); i++) + { + cout << " "; + } + cout << frac_v << endl; + if (negative) + { + cout << "- "; + } + if (int_v != 0) + { + cout << int_v; + } + for (int i = 0; i < num_length(n); i++) + { + cout << "-"; + } + cout << endl; + for (int i = 0; i < offset; i++) + { + cout << " "; + } + cout << n << endl; + } + } + return 0; +} diff --git a/12061.cpp b/12061.cpp new file mode 100644 index 0000000..9dfcbdf --- /dev/null +++ b/12061.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int cases; + double ci, cj, ax, ay, bx, by, cx, cy, dx, dy, sf, angle, den, xden, yden; + scanf("%d", &cases); + while (cases--) + { + scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &ci, &cj, &ax, &ay, &bx, &by, &cx, &cy, &dx, &dy); + sf = sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay)) / ci; + angle = atan2((by - ay), (bx - ax)); + den = (sf * cos(angle) - 1) * (sf * cos(angle) - 1) + (sf * sin(angle)) * (sf * sin(angle)); + den = -1 / den; + xden = ay * sf * sin(angle) + ax * (sf * cos(angle) - 1); + yden = -ax * sf * sin(angle) + ay * (sf * cos(angle) - 1); + printf("%.4lf %.4lf\n", xden * den, yden * den); + } + return 0; +} diff --git a/12062.cpp b/12062.cpp new file mode 100644 index 0000000..18f0c7e --- /dev/null +++ b/12062.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +typedef long long LL; + +const LL LIM = 1000000000000000LL; +const LL INF = LIM + 10LL; +const LL pr[] = {2LL, 3LL, 5LL, 7LL, 11LL, 13LL, 17LL, 19LL, 23LL, 29LL, 31LL, 37LL, 41LL, 43LL, 47LL, 53LL, 59LL}; + +LL dp[5005 + 2][18]; + +LL go(int p, LL nd) +{ + if (nd == 1LL) + { + return 1LL; + } + if (p == 16) + { + return INF; + } + LL &ret = dp[nd][p]; + if (ret != -1LL) + { + return ret; + } + ret = INF; + LL nm = 1LL; + for (int i = 1; i <= 63; i++) + { + if (nm > (INF / pr[p])) + { + break; + } + nm *= pr[p]; + if (nd % (i + 1) == 0) + { + LL tmp = go(p + 1, nd / (i + 1)); + if (nm < INF / tmp) + { + ret = min(ret, nm * tmp); + } + } + } + return ret; +} + +int main() +{ + int t = 1, n; + while (scanf("%d", &n) == 1 && n) + { + memset(dp, -1, sizeof(dp)); + LL ans = go(0, (LL)n); + printf("Case %d: ", t++); + if (ans > LIM) + { + printf("Impossible\n"); + } + else + { + printf("%lld\n", ans); + } + } + return 0; +} diff --git a/12063.cpp b/12063.cpp new file mode 100644 index 0000000..9d94827 --- /dev/null +++ b/12063.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +long long DP[34][34][100]; +int main() +{ + int t, n, k; + int i, j, l, Case = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &k); + printf("Case %d: ", ++Case); + if (n & 1 || k == 0) + { + puts("0"); + continue; + } + memset(DP, 0, sizeof(DP)); + /*[zeros][ones][mod];*/ + DP[0][1][1 % k] = 1; + n /= 2; + for (i = 0; i <= n; i++) + { + for (j = 0; j <= n; j++) + { + for (l = 0; l < k; l++) + { + DP[i + 1][j][(l << 1) % k] += DP[i][j][l]; + DP[i][j + 1][((l << 1) + 1) % k] += DP[i][j][l]; + } + } + } + printf("%lld\n", DP[n][n][0]); + } + return 0; +} diff --git a/12064.cpp b/12064.cpp new file mode 100644 index 0000000..4d91274 --- /dev/null +++ b/12064.cpp @@ -0,0 +1,288 @@ +#include + +using namespace std; + +#define eps 1e-6 + +struct Pt +{ + double x, y; +}; +double dist(Pt &a, Pt &b) +{ + return pow(a.x - b.x, 2) + pow(a.y - b.y, 2); +} +Pt circle(Pt &a, Pt &b, Pt &c) +{ + static Pt ab, ac, o; + static double a1, b1, c1, a2, b2, c2, D, D1, D2; + ab.x = (a.x + b.x) / 2, ab.y = (a.y + b.y) / 2; + ac.x = (a.x + c.x) / 2, ac.y = (a.y + c.y) / 2; + a1 = a.x - b.x, b1 = a.y - b.y, c1 = a1 * ab.x + b1 * ab.y; + a2 = a.x - c.x, b2 = a.y - c.y, c2 = a2 * ac.x + b2 * ac.y; + D = a1 * b2 - a2 * b1; + D1 = c1 * b2 - c2 * b1, D2 = a1 * c2 - a2 * c1; + o.x = D1 / D, o.y = D2 / D; + return o; +} +Pt A, B, C, mAB; +Pt p[50000]; +int main() +{ + /*freopen("in.txt", "r+t", stdin); + freopen("nlogn.txt", "w+t", stdout);*/ + int cases = 0; + int n, q; + int i, j, k; + while (scanf("%lf %lf", &A.x, &A.y) == 2) + { + scanf("%lf %lf", &B.x, &B.y); + scanf("%d %d", &n, &q); + if (n == 0 && q == 0) + break; + for (i = 0; i < n; i++) + scanf("%lf %lf", &p[i].x, &p[i].y); + double a, b, c;// ax + by = c + a = A.y - B.y, b = -A.x + B.x; + if (a < 0) + a = -a, b = -b; + //if(fabs(a/(a*a+b*b)) < eps && b < 0) b = -b; + c = a * A.x + b * A.y; + mAB.x = (A.x + B.x) / 2, mAB.y = (A.y + B.y) / 2; + //printf("%lf x + %lf y = %lf\n", a, b, c); + double na = a, nb = b; + na /= sqrt(a * a + b * b); + nb /= sqrt(a * a + b * b); + if (na < 0) + na = -na, nb = -nb; + vector rSame, rDif, lSame, lDif; + int base = 0; + for (i = 0; i < n; i++) + { + //printf("pt %lf %lf\n", p[i].x, p[i].y); + double argv = a * p[i].x + b * p[i].y - c; + if (fabs((A.x - B.x) * (p[i].y - A.y) - (A.y - B.y) * (p[i].x - A.x)) < eps) + { + if (min(A.x, B.x) <= p[i].x && p[i].x <= max(A.x, B.x) && + min(A.y, B.y) <= p[i].y && p[i].y <= max(A.y, B.y)) + base++; + //puts("Impossible"); + continue; + } + if (argv > 0) + {// right side + //printf("%d\n", i); + // circle center in right side + double l = 0, r = 150000, m, cr2; + Pt C; + int cnt = 0; + while (l <= r && cnt < 50) + { + m = (l + r) / 2; + C.x = mAB.x + na * m, C.y = mAB.y + nb * m; + cr2 = dist(C, A); + if (dist(C, p[i]) <= cr2) + r = m; + else + l = m; + cnt++; + } + rSame.push_back(cr2); + // circle center in left side + l = 0, r = 150000, cnt = 0; + while (l <= r && cnt < 50) + { + m = (l + r) / 2; + C.x = mAB.x - na * m, C.y = mAB.y - nb * m; + cr2 = dist(C, A); + if (dist(C, p[i]) <= cr2) + l = m; + else + r = m; + cnt++; + } + /*printf("rDif %lf\n", cr2); + printf("%lf %lf %lf\n", cr2, p[i].x, p[i].y);*/ + rDif.push_back(cr2); + } + else + {// left side + // circle center in right side + //printf("%lf\n", argv); + double l = 0, r = 150000, m, cr2; + Pt C; + int cnt = 0; + while (l <= r && cnt < 50) + { + m = (l + r) / 2; + C.x = mAB.x + na * m, C.y = mAB.y + nb * m; + cr2 = dist(C, A); + if (dist(C, p[i]) <= cr2) + l = m; + else + r = m; + cnt++; + } + + /*printf("lDif %lf\n", cr2); + printf("%lf %lf %lf\n", cr2, p[i].x, p[i].y);*/ + lDif.push_back(cr2); + // circle center in left side + l = 0, r = 150000, cnt = 0; + while (l <= r && cnt < 50) + { + m = (l + r) / 2; + C.x = mAB.x - na * m, C.y = mAB.y - nb * m; + cr2 = dist(C, A); + if (dist(C, p[i]) <= cr2) + r = m; + else + l = m; + cnt++; + } + lSame.push_back(cr2); + //printf("%lf %lf %lf\n", cr2, C.x, C.y); + } + } + sort(rSame.begin(), rSame.end()); + sort(rDif.begin(), rDif.end()); + sort(lSame.begin(), lSame.end()); + sort(lDif.begin(), lDif.end()); + /*for(i = 0; i < rSame.size(); i++) + printf("%lf ", rSame[i]); + puts(" : rSame"); + for(i = 0; i < lSame.size(); i++) + printf("%lf ", lSame[i]); + puts(" : lSame"); + for(i = 0; i < rDif.size(); i++) + printf("%lf ", rDif[i]); + puts(" : rDif"); + for(i = 0; i < lDif.size(); i++) + printf("%lf ", lDif[i]); + puts(" : lDif");*/ + printf("Case %d:\n", ++cases); + while (q--) + { + int Idx; + scanf("%d", &Idx); + C = p[Idx - 1]; + if (fabs((A.x - B.x) * (C.y - A.y) - (A.y - B.y) * (C.x - A.x)) < eps) + { + puts("Impossible"); + continue; + } + //printf("%lf %lf\n", C.x, C.y); + C = circle(A, B, C); + double argv = a * C.x + b * C.y - c, cr2; + cr2 = dist(C, A); + //printf("%lf %lf %lf\n", C.x, C.y, cr2); + if (cr2 > (100000 - eps) * (100000.0 - eps)) + { + puts("Impossible"); + continue; + } + //printf("%lf %lf %lf\n", C.x, C.y, cr2); + int v1, v2; +#define eps2 1e-3 + if (argv > 0) + {// center in right side + //puts("right side"); + v1 = upper_bound(rSame.begin(), rSame.end(), cr2 + eps2) - rSame.begin(); + v2 = upper_bound(lDif.begin(), lDif.end(), cr2 - eps2) - lDif.begin(); + v2 = lDif.size() - v2; + } + else + {// center in left side + //puts("left side"); + v1 = upper_bound(lSame.begin(), lSame.end(), cr2 + eps2) - lSame.begin(); + v2 = upper_bound(rDif.begin(), rDif.end(), cr2 - eps2) - rDif.begin(); + v2 = rDif.size() - v2; + } + //printf("%d %d\n", v1, v2); + printf("%d\n", v1 + v2 + 2 + base); + } + } + return 0; +} +/* +570 4502 2317 8897 78 81 +6248 2485 +1526 6730 +5462 2645 +5778 6858 +9269 2526 +9372 677 +832 6361 +2482 1834 +9497 3605 +219 6679 +8808 4727 +4696 8986 +1619 8687 +5176 166 +2207 9212 +9947 552 +4645 3554 +2457 586 +1193 4411 +4100 9991 +8871 845 +7883 4656 +2911 395 +7073 9935 +3091 5182 +717 715 +1197 7237 +6483 1243 +2519 8794 +8515 3550 +8869 2791 +7088 7450 +9733 4216 +532 788 +6251 5411 +9858 6480 +6950 2566 +9692 376 +2103 4384 +678 7746 +8995 3363 +3115 701 +967 9144 +3774 3133 +2699 4835 +6115 3496 +2282 6824 +5840 1215 +8064 6398 +926 5537 +1461 7972 +1492 9196 +8618 3314 +2278 1893 +1665 6752 +1754 5322 +770 6732 +923 4743 +7531 7624 +3233 4550 +2360 8243 +1873 2042 +1747 8201 +4801 2541 +1902 2573 +3343 72 +3134 1350 +6658 596 +858 327 +7649 4767 +3109 84 +739 7841 +3848 4418 +3879 6911 +1331 6782 +3508 6047 +805 3761 +9747 5501 +50 +*/ diff --git a/12065.cpp b/12065.cpp new file mode 100644 index 0000000..81f8cfa --- /dev/null +++ b/12065.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define maxL (1111111 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[maxL]; +int P[100000], Pt = 0; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 1111111; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} +int check(int n) +{ + static char buf1[25], buf2[25]; + sprintf(buf1, "%d", n); + int p, q, m = strlen(buf1); + sort(buf1, buf1 + m); + do + { + p = 0; + for (int i = 0; i < m; i++) + p = p * 10 + buf1[i] - '0'; + if (p == n) + continue; + q = abs(n - p); + if (q % 9 == 0 && q / 9 < 1111111) + { + if (!GET(q / 9)) + return 1; + } + } while (next_permutation(buf1, buf1 + m)); + return 0; +} +int main() +{ + sieve(); + int testcase, cases = 0, l, r; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &l, &r); + if (r < l) + swap(l, r); + int ret = 0; + for (int i = l; i <= r; i++) + { + if (check(i)) + ret++; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/12068.cpp b/12068.cpp new file mode 100644 index 0000000..e75450f --- /dev/null +++ b/12068.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +long long n, v[16]; + +long long gcd(long long a, long long b) +{ + return b ? gcd(b, a % b) : a; +} + +void simplify(long long &num, long long &den) +{ + long long g = gcd(num, den); + num /= g; + den /= g; +} + +int main() +{ + int t; + scanf("%d", &t); + for (int cnum = 0; cnum++ < t && scanf("%lld", &n) == 1;) + { + long long den = 1, num = 0; + for (int i = 0; i < n; ++i) + { + scanf("%lld", v + i); + den = (den / gcd(den, v[i])) * v[i]; + } + for (int i = 0; i < n; ++i) + { + num += den / v[i]; + } + simplify(num, den); + simplify(num, n); + printf("Case %d: %lld/%lld\n", cnum, n * den, num); + } + return 0; +} diff --git a/12069.cpp b/12069.cpp new file mode 100644 index 0000000..fd4d6d9 --- /dev/null +++ b/12069.cpp @@ -0,0 +1,273 @@ +#include + +using namespace std; + +/* + * + * 12069 - Robots inside the Labyrinth + * Grid Compression,graph,bfs + * ACM ICPC Dhaka Regional 2004-05 + * + */ + +#define stream istringstream +#define rep(i, n) for (int i = 0; i < (int)n; i++) +#define repv(i, n) for (int i = n - 1; i >= 0; i--) +#define repl(i, n) for (int i = 1; i <= (int)n; i++) +#define replv(i, n) for (int i = n; i >= 1; i--) +#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++) +#define foreach(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); i++) +#define sz(x) (int)x.size() +#define inf (1061109567) +#define pb(x) push_back(x) +#define ppb pop_back +#define all(x) x.begin(), x.end() +#define mem(x, y) memset(x, y, sizeof(x)); +#define eps 1e-9 +#define rev reverse +#define pii pair +#define pll pair +#define pmp make_pair +#define sdi(x) scanf("%d", &x) +#define sdii(x, y) scanf("%d%d", &x, &y) +#define sds(x) scanf("%s", x) +#define pfi(x) printf("%d\n", x); +#define uu first +#define vv second + +template +inline T sqr(T x) { return x * x; } +template +inline T lcm(T a, T b) +{ + if (a < 0) + return lcm(-a, b); + if (b < 0) + return lcm(a, -b); + return a * (b / __gcd(a, b)); +} +template +T power(T N, T P) { return (P == 0) ? 1 : N * power(N, P - 1); } +template +string itoa(T a) +{ + if (!a) + return "0"; + string ret; + for (T i = a; i > 0; i = i / 10) + ret.pb((i % 10) + 48); + reverse(all(ret)); + return ret; +} +typedef long long i64; +typedef unsigned long long ui64; +double log(double N, double B) { return (log10l(N)) / (log10l(B)); } +vector token(string a, string b) +{ + const char *q = a.c_str(); + while (count(b.begin(), b.end(), *q)) + q++; + vector + oot; + while (*q) + { + const char *e = q; + while (*e && !count(b.begin(), b.end(), *e)) + e++; + oot.push_back(string(q, e)); + q = e; + while (count(b.begin(), + b.end(), *q)) + q++; + } + return oot; +} +//bool operator<( const node& p ) const { return w < p.w; } +#define on(n, pos) (n | (1 << (pos))) +#define off(n, pos) n & ~(1 << pos) +#define isOn(n, pos) (bool)(n & (1 << (pos))) +string toBin(int n) +{ + string s; + repv(i, 32) s += (isOn(n, i) + '0'); + return s; +} +bool eq(long double a, long double b) { return fabs(a - b) < eps; } +#define READ(f) freopen(f, "r", stdin) +#define WRITE(f) freopen(f, "w", stdout) +#define pks printf("Case %d: ", ++ks); + +struct node +{ + int x1, y1, x2, y2; + node(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} + node() {} +} vec[100]; +int const mx = 300; +bool w[mx][mx]; +int numx, numy; +char vis[mx][mx][4]; + +int fx[] = {+0, +0, -1, +1}; +int fy[] = {-1, +1, +0, +0}; + +struct info +{ + int x, y, d; + info(int _x, int _y, int _d) + { + x = _x; + y = _y; + d = _d; + } + info() {} +}; +int bfs(int sx, int sy, int dx, int dy) +{ + mem(vis, 63); + queue q; + for (int k = 0; k < 4; k++) + { + vis[sx][sy][k] = 0; + q.push(info(sx, sy, k)); + } + int ans = 1 << 28; + while (!q.empty()) + { + info top = q.front(); + q.pop(); + int x = top.x; + int y = top.y; + int d = top.d; + if (x == dx and y == dy) + { + ans = min(ans, (int)vis[x][y][d]); + } + rep(k, 4) + { + int tx = top.x + fx[k]; + int ty = top.y + fy[k]; + int td = k; + int cost = 0; + if (td != d) + cost++; + if (tx >= 1 and tx <= numx and ty >= 1 and ty <= numy and w[tx][ty] == 0) + { + if (vis[x][y][d] + cost < vis[tx][ty][td]) + { + + vis[tx][ty][td] = vis[x][y][d] + cost; + q.push(info(tx, ty, td)); + } + } + } + } + return ans; +} + +void fill(int lox, int hix, int loy, int hiy, int num) +{ + for (int i = lox; i <= hix; i++) + { + for (int j = loy; j <= hiy; j++) + { + w[i][j] = num; + } + } +} +#define pushX(x) setX.insert(x); +#define pushY(y) setY.insert(y); +int main() +{ + //READ("in"); WRITE("out2"); + double ST = clock(); + + int t, ks = 0; + cin >> t; + while (t--) + { + int n; + cin >> n; + mem(w, 0); + set setX, setY; + map mpX, mpY; + + repl(i, n) + { + int x1, y1, x2, y2; + sdii(x1, y1); + sdii(x2, y2); + pushX(x1); + pushX(x2); + pushY(y1); + pushY(y2); + vec[i] = node(x1, y1, x2, y2); + } + + int q; + cin >> q; + node query[1000]; + + repl(i, q) + { + int x1, y1, x2, y2; + sdii(x1, y1); + sdii(x2, y2); + //push X's + + setX.insert(x1); + setX.insert(x2); + + //push Y's + setY.insert(y1); + setY.insert(y2); + query[i] = node(x1, y1, x2, y2); + } + + numx = 0, numy = 0; + foreach (it, setX) + { + int x = *it; + numx += 2; + mpX[x] = numx; + } + foreach (it, setY) + { + int y = *it; + numy += 2; + mpY[y] = numy; + } + numx++; + numy++; + for (int i = 1; i <= n; i++) + { + int x1 = mpX[vec[i].x1]; + int y1 = mpY[vec[i].y1]; + + int x2 = mpX[vec[i].x2]; + int y2 = mpY[vec[i].y2]; + int lox = min(x1, x2); + int hix = max(x1, x2); + + int loy = min(y1, y2); + int hiy = max(y1, y2); + fill(lox, hix, loy, hiy, i); + } + + printf("Labyrinth #%d\n", ++ks); + repl(i, q) + { + int sx = query[i].x1; + int sy = query[i].y1; + int dx = query[i].x2; + int dy = query[i].y2; + + int ret = bfs(mpX[sx], mpY[sy], mpX[dx], mpY[dy]); + if (ret == 1 << 28) + puts("Impossible."); + else + pfi(ret); + } + } + cerr << (clock() - ST) / CLOCKS_PER_SEC << endl; +} diff --git a/1207.cpp b/1207.cpp new file mode 100644 index 0000000..35e2d0d --- /dev/null +++ b/1207.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +char s[1000], t[1000]; +int t1, t2; +int read() +{ + if (scanf("%d", &t1) != 1) + { + return 0; + } + if (t1 > 0) + { + scanf("%s", s); + } + else + { + s[0] = 0; + } + scanf("%d", &t2); + if (t2 > 0) + { + scanf("%s", t); + } + else + { + t[0] = 0; + } + return 1; +} +int m[1001][1001]; +void process() +{ + int i, j; + memset(m, 0, sizeof(m)); + for (i = 1; i <= t1; i++) + { + m[i][0] = m[i - 1][0] + 1; + } + for (i = 1; i <= t2; i++) + { + m[0][i] = m[0][i - 1] + 1; + } + for (i = 1; i <= t1; i++) + { + for (j = 1; j <= t2; j++) + { + if (s[i - 1] == t[j - 1]) + { + m[i][j] = m[i - 1][j - 1]; + } + else + { + m[i][j] = min(m[i - 1][j - 1], min(m[i - 1][j], m[i][j - 1])) + 1; + } + } + } + printf("%d\n", m[t1][t2]); +} +int main() +{ + while (read()) + { + process(); + } + return 0; +} diff --git a/12070.cpp b/12070.cpp new file mode 100644 index 0000000..e0b0810 --- /dev/null +++ b/12070.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +/* + * 12070 - Invite Your Friends.cpp + * + * Created on: June 3, 2014 + * Author: Gaurab + */ + +#define fs first +#define sc second + +const int INF = 1e9; + +int xx[] = {0, 0, 1, -1}; +int yy[] = {1, -1, 0, 0}; + +class data +{ +public: + int r, c, cost, time; + data() + { + r = 0; + c = 0; + cost = 0; + time = 0; + } + data(int _r, int _c, int _cost, int _time) + { + r = _r; + c = _c; + cost = _cost; + time = _time; + } + bool operator<(const data &z) const + { + return cost > z.cost; + } +}; + +int cost_dist[26][26][26], mat[26][26], N, T; +pair friends[6]; + +void myfinity(int n, int t) +{ + int i, j, k; + for (i = 0; i < n; ++i) + { + for (j = 0; j < n; ++j) + { + for (k = 0; k <= t; ++k) + { + cost_dist[i][j][k] = INF; + } + } + } +} +void Dijkstra(int sr, int sc) +{ + cost_dist[sr][sc][0] = 0; + priority_queue Q; + Q.push(data(sr, sc, 0, 0)); + data u, v; + int ucd, i, a, b; + while (!Q.empty()) + { + u = Q.top(); + Q.pop(); + ucd = cost_dist[u.r][u.c][u.time]; + for (i = 0; i < 4; ++i) + { + a = xx[i] + u.r; + b = yy[i] + u.c; + if (a < 0 or b < 0 or a >= N or b >= N) + { + continue; + } + v = data(a, b, mat[a][b] + ucd, u.time + 1); + if (v.time > T) + { + continue; + } + if (cost_dist[a][b][v.time] > v.cost) + { + Q.push(v); + cost_dist[a][b][v.time] = v.cost; + } + } + } +} +int main(int argc, char **argv) +{ + int F, i, j, k, l, mn, sum, rr, rc, lcost, cas; + cas = 0; + while (scanf("%d %d %d", &N, &F, &T) == 3 and (N + F + T)) + { + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + scanf("%d", &mat[i][j]); + } + } + for (i = 0; i < F; ++i) + { + scanf("%d %d", &friends[i].fs, &friends[i].sc); + } + mn = INF; + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + myfinity(N, T); + Dijkstra(i, j); + sum = 0; + for (k = 0; k < F; ++k) + { + lcost = INF; + for (l = 0; l <= T; ++l) + { + if (lcost > cost_dist[friends[k].fs][friends[k].sc][l]) + { + lcost = cost_dist[friends[k].fs][friends[k].sc][l]; + } + } + if (lcost >= INF) + { + break; + } + sum += lcost; + if (sum > mn) + { + break; + } + } + if (k == F and sum < mn) + { + mn = sum; + rr = i; + rc = j; + } + } + } + if (mn >= INF) + { + printf("Case #%d: Impossible.\n", ++cas); + } + else + { + printf("Case #%d: Selected city (%d,%d) with minimum cost %d.\n", ++cas, rr, rc, mn); + } + } + return 0; +} diff --git a/12071.cpp b/12071.cpp new file mode 100644 index 0000000..06daaa2 --- /dev/null +++ b/12071.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int N, A[65536], cases = 0; + while (scanf("%d", &N) == 1 && N) + { + for (int i = 0; i < N; i++) + scanf("%d", &A[i]); + sort(A, A + N); + + long long ret = 0; + for (int i = 1, j = 0; i < N; i++) + { + while (A[j] < A[i]) + j++; + ret += j; + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/12072.cpp b/12072.cpp new file mode 100644 index 0000000..b77c454 --- /dev/null +++ b/12072.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +//Md. Khairullah Gaurab +//CSE, SUST, 20th Batch +//gaurab.cse.sust@gmail.com + +#define sf scanf +#define pf printf + +typedef long long Long; + +const int inf = 1e9; +const int SZ = 52; + +struct data +{ + int r, c; +}; + +data mat[SZ]; +Long DP[SZ][SZ], cost[SZ][SZ], csum[SZ][SZ], K; +char num[100]; + +Long rec(int fs, int sc) +{ + Long &ret = DP[fs][sc]; + if (~ret) + { + return ret; + } + if (fs == sc) + { + cost[fs][sc] = 0; + return ret = 0ll; + } + if (csum[fs][sc] <= K) + { + cost[fs][sc] = csum[fs][sc]; + return ret = 1ll; + } + cost[fs][sc] = inf; + ret = inf; + Long tot, part1, part2, kk; + for (int i = fs; i <= sc - 1; i++) + { + kk = (mat[fs].r * mat[i + 1].r * mat[sc].c); + if (kk > K) + { + continue; + } + part1 = rec(fs, i); + if (part1 > K) + { + continue; + } + part2 = rec(i + 1, sc); + if (part2 > K) + { + continue; + } + tot = part1 + part2; + if (part1 > 0 and cost[fs][i] + kk <= K) + { + kk += cost[fs][i]; + } + else + { + tot++; + } + if (tot < ret || (tot == ret and kk < cost[fs][sc])) + { + ret = tot; + cost[fs][sc] = kk; + } + } + return ret; +} + +int main() +{ + int test, N, Q; + sf("%d", &test); + for (int i = 1; i <= test; ++i) + { + sf("%d", &N); + for (int j = 1; j <= N; j++) + { + sf("%d %d", &mat[j].r, &mat[j].c); + } + memset(csum, 0, sizeof(csum)); + for (int j = 1; j <= N; j++) + { + for (int k = j + 1; k <= N; ++k) + { + csum[j][k] = csum[j][k - 1]; + csum[j][k] += (mat[j].r * mat[k - 1].c * mat[k].c); + } + } + sf("%d", &Q); + pf("Matrix #%d\n", i); + while (Q--) + { + sf("%s", num); + if (strlen(num) > 10) + { + K = inf; + } + else + { + K = atoi(num); + } + memset(DP, -1, sizeof(DP)); + Long res = rec(1, N); + if (res >= inf) + { + pf("Impossible.\n"); + } + else + { + pf("%lld\n", res); + } + } + pf("\n"); + } + return 0; +} diff --git a/12073.cpp b/12073.cpp new file mode 100644 index 0000000..0824ace --- /dev/null +++ b/12073.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +// 12073 - Altitude Triangle +// Accepted 27 May 2013 by Henry Morco +// Demonstrates potential of barycenters in determining special points wrt triangles (excenters in this example) +// optimized c++ with Pointers mod hehe -rico + +struct point +{ + double x, y; + point() {} + point(const double &X, const double &Y) : x(X), y(Y) {} + void display() const + { + printf("%.3lf %.3lf\n", x, y); + } +}; + +double dist(const point &a, const point &b) +{ + double x = a.x - b.x, y = a.y - b.y; + return sqrt(x * x + y * y); +} + +struct triangle +{ + point *A, *B, *C; + double a, b, c; + + triangle(point &Aa, point &Bb, point &Cc) : A(&Aa), B(&Bb), C(&Cc) + { + a = dist(*B, *C); + b = dist(*A, *C); + c = dist(*A, *B); + } + + point bary(const double &ba, const double &bb, const double &bc) const + { + double sum = (ba) + (bb) + (bc); + return point((A->x * ba + B->x * bb + C->x * bc) / sum, (A->y * ba + B->y * bb + C->y * bc) / sum); + } + point ex0() const { return bary(-a, b, c); } + point ex1() const { return bary(a, -b, c); } + point ex2() const { return bary(a, b, -c); } +}; +int counter(0); +point a, b, c; +int main() +{ + scanf("%lf %lf %lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y); + + while (a.x || a.y || b.x || b.y || c.x || c.y) + { + printf("Case %d:\n", ++counter); + triangle S = triangle(a, b, c); + S.ex0().display(); + S.ex1().display(); + S.ex2().display(); + scanf("%lf %lf %lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y); + } +} diff --git a/12074.cpp b/12074.cpp new file mode 100644 index 0000000..b641bd4 --- /dev/null +++ b/12074.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +inline int readint() +{ + char c = getchar(); + while (!isdigit(c)) + { + c = getchar(); + } + int x = 0; + while (isdigit(c)) + { + x = x * 10 + c - '0'; + c = getchar(); + } + return x; +} + +inline long long readlong() +{ + char c = getchar(); + while (!isdigit(c)) + { + c = getchar(); + } + long long x = 0; + while (isdigit(c)) + { + x = x * 10 + c - '0'; + c = getchar(); + } + return x; +} + +#define FOR(i, n) for (int i = 0; i < (int)(n); i++) +#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) +#define CIR(i, a, b) for (int i = (int)(b); i >= (int)(a); i--) +#define ADJ(i, u) for (int i = hd[u]; i != -1; i = edge[i].nxt) +#define ECH(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) +#define PII pair +#define FI first +#define SE second +#define MP make_pair +#define PB push_back +#define SZ(v) v.size() +#define ALL(v) v.begin(), v.end() +#define CLR(v, a) memset(v, a, sizeof(v)) +#define IT iterator +#define LL long long +#define DB double +#define PI 3.1415926 + +#define INF 1000000000 +#define N 100015 + +int cx[N], cy[N], key[N], d[N]; +int dp[N], root[N << 2]; + +int ch[N * 70][2], maxv[N * 70]; +int it; +int maX, maY, miX, miY; + +void build() +{ + maxv[0] = 0; + it = 1; + CLR(root, 0); +} + +#define lch ch[rt][0], L, mid +#define rch ch[rt][1], mid + 1, R + +void push_up(int rt) +{ + maxv[rt] = max(maxv[ch[rt][0]], maxv[ch[rt][1]]); +} + +void insert2(int &rt, int L, int R, int y, int v) +{ + if (!rt) + { + rt = it++; + ch[rt][0] = 0; + ch[rt][1] = 0; + maxv[rt] = 0; + } + if (L == R) + { + maxv[rt] = max(maxv[rt], v); + return; + } + int mid = L + R >> 1; + if (y <= mid) + { + insert2(lch, y, v); + } + else + { + insert2(rch, y, v); + } + push_up(rt); +} + +void insert(int rt, int L, int R, int x, int y, int val) +{ + insert2(root[rt], miY, maY, y, val); + if (L == R) + { + return; + } + int mid = L + R >> 1; + if (x <= mid) + { + insert(rt << 1, L, mid, x, y, val); + } + else + { + insert(rt << 1 | 1, mid + 1, R, x, y, val); + } +} + +int query2(int &rt, int L, int R, int y1, int y2) +{ + if (!rt) + { + return 0; + } + if (y1 <= L && R <= y2) + { + return maxv[rt]; + } + int mid = L + R >> 1; + int res = 0; + if (y1 <= mid) + { + res = max(res, query2(lch, y1, y2)); + } + if (y2 > mid) + { + res = max(query2(rch, y1, y2), res); + } + return res; +} + +int query(int rt, int L, int R, int x1, int x2, int y1, int y2) +{ + if (x1 <= L && R <= x2) + { + return query2(root[rt], miY, maY, y1, y2); + } + int mid = L + R >> 1; + int res = 0; + if (x1 <= mid) + { + res = max(res, query(rt << 1, L, mid, x1, x2, y1, y2)); + } + if (x2 > mid) + { + res = max(res, query(rt << 1 | 1, mid + 1, R, x1, x2, y1, y2)); + } + return res; +} + +bool cmp(const int &a, const int &b) +{ + return key[a] < key[b]; +} + +int idx[N]; + +int main() +{ + int test, n; + scanf("%d", &test); + FOR(cas, test) + { + scanf("%d", &n); + build(); + maX = maY = -INF; + miX = miY = INF; + FOR(i, n) + { + scanf("%d%d%d%d", cx + i, cy + i, key + i, d + i); + idx[i] = i; + int tx = cx[i], ty = cy[i]; + cx[i] = tx - ty, cy[i] = tx + ty; + maX = max(maX, cx[i]); + miX = min(miX, cx[i]); + maY = max(maY, cy[i]); + miY = min(miY, cy[i]); + } + sort(idx, idx + n, cmp); + FOR(i, n) + { + int id = idx[i]; + int res = query(1, miX, maX, cx[id] - d[id], cx[id] + d[id], cy[id] - d[id], cy[id] + d[id]) + 1; + dp[id] = res; + insert(1, miX, maX, cx[id], cy[id], dp[id]); + } + int res = 1; + FOR(i, n) + res = max(res, dp[i]); + printf("Case %d: %d\n", cas + 1, res); + } + return 0; +} diff --git a/12075.cpp b/12075.cpp new file mode 100644 index 0000000..137d66f --- /dev/null +++ b/12075.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +typedef long long LL; + +LL gcd(LL a, LL b) +{ + if (b > a) + swap(a, b); + if (a % b == 0) + return b; + return gcd(b, a % b); +} + +int main() +{ + LL i, j, s, m, n, kase = 1, x, y, z, d; + while (scanf("%lld%lld", &m, &n) && m + n) + { + printf("Case %lld: ", kase++); + s = m * n + m + n + 1; + s = s * (s - 1) * (s - 2) / 6; + x = (n + 1) * n * (n - 1) / 6 * (m + 1); + y = (m + 1) * m * (m - 1) / 6 * (n + 1); + d = min(m, n); + z = 0; + for (i = 2; i <= m; i++) + for (j = 2; j <= n; j++) + z += (gcd(i, j) - 1) * (m - i + 1) * (n - j + 1); + printf("%lld\n", s - x - y - 2 * z); + } + return 0; +} diff --git a/12077.cpp b/12077.cpp new file mode 100644 index 0000000..e933288 --- /dev/null +++ b/12077.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define FORI(i, s, n) for (int i = s; i < n; ++i) +struct product +{ + int n; + int lo[100], hi[100]; + int least[100], most[100]; + char ing[100][32], name[32]; +} p[16]; + +int min(int a, int b) +{ + return a < b ? a : b; +} +int max(int a, int b) +{ + return a > b ? a : b; +} + +int memo[128][128][128]; +bool tryQty(int ing, int top, int remain, product &z) +{ + if (remain < 0 || ing == z.n) + { + return remain == 0; + } + int &r = memo[ing][top][remain]; + if ((r >> 1) != (&z - p)) + { + r = (&z - p) << 1; + int stop_at = min(z.most[ing], top) + 1; + FORI(i, z.least[ing], stop_at) + if (tryQty(ing + 1, i, remain - i, z)) + { + r |= 1; + z.lo[ing] = min(z.lo[ing], i); + z.hi[ing] = max(z.hi[ing], i); + } + } + return r & 0x1; +} +int main() +{ + char ingredient[16], line[1024]; + for (int P; scanf("%d", &P) == 1 && P;) + { + FOR(i, 128) + FOR(j, 128) FOR(k, 128) + memo[i][j][k] = -1; + FOR(i, P) + { + scanf("%s %d\n", p[i].name, &p[i].n); + FOR(j, p[i].n) + { + fgets(line, 1024, stdin); + if (sscanf(line, "%s %d", p[i].ing[j], &p[i].least[j]) == 2) + { + p[i].most[j] = p[i].least[j]; + } + else + { + sscanf(line, "%s", p[i].ing[j]); + p[i].least[j] = 1; + p[i].most[j] = 100; + } + p[i].lo[j] = 100; + p[i].hi[j] = 0; + } + tryQty(0, 100, 100, p[i]); + } + int queries; + int list[16]; + for (scanf("%d", &queries); queries--;) + { + int top(0); + int left(0), right(100); + scanf("%s %s", line, ingredient); + FOR(i, P) + { + int least(0), most(0); + FOR(j, p[i].n) + if (!strcmp(p[i].ing[j], ingredient)) + { + least = p[i].lo[j]; + most = p[i].hi[j]; + break; + } + left = max(left, least); + right = min(right, most); + } + FOR(i, P) + { + int least(0), most(0); + FOR(j, p[i].n) + if (!strcmp(p[i].ing[j], ingredient)) + { + least = p[i].lo[j]; + most = p[i].hi[j]; + break; + } + if ((line[0] == 'm' && most >= left) || + (line[0] == 'l' && least <= right)) + { + list[top++] = i; + } + } + FOR(i, top) + printf("%s%c", p[list[i]].name, i == top - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/12078.cpp b/12078.cpp new file mode 100644 index 0000000..32a94f2 --- /dev/null +++ b/12078.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const int NN = 64; +const char *dd = "NESW"; +bool board[NN][NN], v[NN][NN][4]; +enum directions +{ + N, + E, + S, + W +}; +int dx[4] = {0, 1, 0, -1}; +int dy[4] = {1, 0, -1, 0}; +int main() +{ + int T; + scanf("%d", &T); + for (int n, r; T-- && scanf("%d %d", &n, &r) == 2;) + { + int dir, x, y; + ++n; + FOR(i, n) + FOR(j, n) + { + board[i][j] = false; + FOR(k, 4) + v[i][j][k] = false; + } + FOR(i, r) + { + scanf("%d %d", &x, &y); + board[x][y] = 1; + } + scanf("%d %d", &x, &y); + if (x == 0) + { + dir = E; + } + else if (x == n) + { + dir = W; + } + else if (y == 0) + { + dir = N; + } + else + { + dir = S; + } + while (true) + { + v[x][y][dir] = true; + x += dx[dir]; + y += dy[dir]; + dir = (dir + board[x][y]) % 4; + if (x < 1 || x >= n || y < 1 || y >= n) + { + break; + } + if (v[x][y][dir]) + { + x = y = 0; + break; + } + } + printf("%d %d\n", x, y); + } + return 0; +} diff --git a/12079.cpp b/12079.cpp new file mode 100644 index 0000000..e6cc15f --- /dev/null +++ b/12079.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +int n; +bool used[16]; +int m[16]; +double h[16], r[16], rh[16], R[16]; +double minH; + +double max(double a, double b) +{ + return a > b ? a : b; +} + +double fit(double r1, double R1, double H1, double r2, double R2, double H2) +{ + double hB = 0, hT = 0, hM = 0; + // bottom-height + if (r1 > R2) + { + hB = H2; + } + else if (r1 < r2) + { + hB = 0; + } + else + { + hB = (H2 / (R2 - r2)) * (r1 - r2); + } + // top-height + if (R1 < r2) + { + hT = 0; + } + else if (R1 > R2) + { + hT = H2; + } + else + { + hT = (H2 / (R2 - r2)) * (R1 - r2); + } + hB = max(hB, hT - H1); + // mid-height + if (r1 < R2 && R1 > R2) + { + hM = (H1 / (R1 - r1)) * (R2 - r1); + hB = max(hB, H2 - hM); + } + return hB; +} + +void bt(int d) +{ + if (d == n) + { + double maxH = 0; + for (int i = 0; i < d; ++i) + { + maxH = max(maxH, rh[m[i]] + h[m[i]]); + } + if (maxH < minH) + { + minH = maxH; + } + return; + } + for (int i = 0; i < n; ++i) + { + if (used[i]) + { + continue; + } + used[i] = true; + double addH = rh[i] = 0; + for (int j = 0; j < d; ++j) + { + int p = m[j]; + addH = fit(r[i], R[i], h[i], r[p], R[p], h[p]); + rh[i] = max(rh[i], rh[p] + addH); + } + m[d] = i; + bt(d + 1); + used[i] = false; + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + scanf("%lf %lf %lf", &h[i], &r[i], &R[i]); + used[i] = false; + } + minH = 1e10; + bt(0); + printf("%d\n", int(floor(minH))); + } + return 0; +} diff --git a/1208.cpp b/1208.cpp new file mode 100644 index 0000000..9a4657b --- /dev/null +++ b/1208.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const int NN = 128; +struct edge +{ + int f, t, w; + bool operator<(const edge &e) const + { + return w < e.w || (w == e.w && (f < e.f || (f == e.f && t < e.t))); + } +} e[NN * NN]; +int edges = 0, p[NN]; + +void add_edge(int f, int t, int w) +{ + edge &E = e[edges++]; + E.f = f; + E.t = t; + E.w = w; +} + +int root(int x) +{ + return p[x] == x ? x : p[x] = root(p[x]); +} +bool union_find(int x, int y) +{ + p[x = root(x)] = y = root(y); + return x != y; +} + +int main() +{ + int t; + scanf("%d", &t); + for (int cnum = 0, n, z; cnum++ < t && scanf("%d", &n) == 1; edges = 0) + { + FOR(i, n) + { + p[i] = i; + FOR(j, n) + { + scanf("%d,", &z); + if (z && i < j) + { + add_edge(i, j, z); + } + } + } + sort(e, e + edges); + printf("Case %d:\n", cnum); + int cnt = 0; + FOR(i, edges) + { + if (union_find(e[i].f, e[i].t) && ++cnt) + { + printf("%c-%c %d\n", 'A' + e[i].f, 'A' + e[i].t, e[i].w); + } + if (cnt == n - 1) + { + break; + } + } + } + return 0; +} diff --git a/12081.cpp b/12081.cpp new file mode 100644 index 0000000..cc67cf8 --- /dev/null +++ b/12081.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int v[512]; +bool seen[1000000]; +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + scanf("%d", &v[i]); + } + bool found = false; + for (int i = 1; !found; ++i) + { + found = true; + for (int j = 0; j < i; ++j) + { + seen[j] = 0; + } + for (int j = 0; found && j < n; ++j) + { + int mod = v[j] % i; + found &= !seen[mod]; + seen[mod] = true; + } + if (found) + { + printf("%d\n", i); + } + } + } + return 0; +} diff --git a/12082.cpp b/12082.cpp new file mode 100644 index 0000000..47314e7 --- /dev/null +++ b/12082.cpp @@ -0,0 +1,227 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define INV(x, y) (x < 0 || x >= GSZ || y < 0 || y >= GSZ) + +const int GSZ = 45; + +int dx[6] = {0, 1, 1, 0, -1, -1}; +int dy[6] = {1, 0, -1, -1, 0, 1}; + +bool played[5][GSZ][GSZ]; +int map_[128], fcnt, ncnt; + +struct tile +{ + char c[8]; + bool e, f, ctrl; + int x, y; +} g[GSZ][GSZ], hand[5], placed[32]; + +int count_neighbours(int x, int y) +{ + int cnt = 0; + FOR(dir, 6) + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (INV(nx, ny)) + { + continue; + } + cnt += !g[nx][ny].e; + } + return cnt; +} +void mark_controlled(int x, int y, int dir) +{ + FOR(i, GSZ) + { + int nx = x + i * dx[dir], ny = y + i * dy[dir]; + if (INV(nx, ny)) + { + continue; + } + g[nx][ny].ctrl = true; + } +} +bool is_stuck(int x, int y) +{ + if (!g[x][y].e) + { + return false; + } + if (count_neighbours(x, y) != 3) + { + return false; + } + int ccnt[4] = {0, 0, 0, 0}; + FOR(dir, 6) + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (INV(nx, ny)) + { + continue; + } + if (!g[nx][ny].e && ++ccnt[map_[g[nx][ny].c[(dir + 3) % 6]]] == 3) + { + return true; + } + } + return false; +} +void mark_if_forced(int x, int y) +{ + if (!g[x][y].e) + { + return; + } + int cnt = count_neighbours(x, y); + if (cnt == 3)// forced + { + g[x][y].f = true; + FOR(dir, 6)// mark lines as controlled + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (INV(nx, ny)) + { + continue; + } + int nx2 = x + dx[(dir + 2) % 6], ny2 = y + dy[(dir + 2) % 6]; + int nx4 = x + dx[(dir + 4) % 6], ny4 = y + dy[(dir + 4) % 6]; + if (!INV(nx2, ny2) && !g[nx][ny].e && !g[nx2][ny2].e) + { + mark_controlled(x + dx[(dir + 3) % 6], y + dy[(dir + 3) % 6], (dir + 3) % 6); + } + if (!INV(nx4, ny4) && !g[nx][ny].e && !g[nx4][ny4].e) + { + mark_controlled(x + dx[(dir + 3) % 6], y + dy[(dir + 3) % 6], (dir + 3) % 6); + } + } + } +} + +bool forced_impossible(int x, int y) +{ + int cnt = count_neighbours(x, y); + if (cnt == 3)// its forced + { + int ccnt[4] = {0, 0, 0, 0}; + FOR(dir, 6) + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (!INV(nx, ny) && !g[nx][ny].e) + { + if (++ccnt[map_[g[nx][ny].c[(dir + 3) % 6]]] == 3) + { + return true; + } + } + } + } + return false; +} + +void try_connect(int hidx, int x, int y) +{ + if (played[hidx][x][y]) + { + return; + } + played[hidx][x][y] = true; + tile &t = hand[hidx]; + if (!g[x][y].e || g[x][y].ctrl) + { + return; + } + int poss = 0; + g[x][y].e = false; + FOR(offset, 6) + { + bool can = true; + int cnt = 0; + FOR(i, 6) + g[x][y].c[i] = t.c[(i + offset) % 6]; + FOR(dir, 6) + { + if (!can) + { + break; + } + int nx = x + dx[dir], ny = y + dy[dir]; + if (INV(nx, ny)) + { + continue; + } + if (!g[nx][ny].e) + { + ++cnt; + can &= g[nx][ny].c[(dir + 3) % 6] == t.c[(dir + offset) % 6]; + } + else + { + can &= !forced_impossible(nx, ny); + } + } + poss += can && cnt > 0; + } + if (g[x][y].f) + { + fcnt += poss; + } + else + { + ncnt += poss; + } + g[x][y].e = true; +} + +int main() +{ + int T; + scanf("%d", &T); + FOR(i, 128) + map_[i] = 0; + map_['B'] = 0; + map_['G'] = 1; + map_['R'] = 2; + map_['Y'] = 3; + for (int n, x, y; T-- && scanf("%d", &n) == 1;) + { + fcnt = ncnt = 0; + FOR(i, GSZ) + FOR(j, GSZ) + { + g[i][j].e = true, g[i][j].f = g[i][j].ctrl = false; + FOR(k, 5) + played[k][i][j] = false; + } + FOR(i, n) + { + scanf("%d %d", &placed[i].x, &placed[i].y); + x = placed[i].x += 20; + y = placed[i].y += 20; + scanf("%s", g[x][y].c); + strcpy(placed[i].c, g[x][y].c); + g[x][y].e = false; + } + FOR(i, GSZ) + FOR(j, GSZ) mark_if_forced(i, j); + FOR(i, 5) + { + scanf("%s", hand[i].c); + FOR(j, n) + FOR(dir, 6) + { + int nx = placed[j].x + dx[dir], ny = placed[j].y + dy[dir]; + if (!INV(nx, ny)) + { + try_connect(i, nx, ny); + } + } + } + printf("%d\n", fcnt ? fcnt : ncnt); + } + return 0; +} diff --git a/12083.cpp b/12083.cpp new file mode 100644 index 0000000..a02c67e --- /dev/null +++ b/12083.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const int MEN(500), WOMEN(500); + +map mnum, snum; +bool graph[MEN][WOMEN], seen[WOMEN]; +int matchL[MEN], matchR[WOMEN]; +int men, women; + +struct person +{ + int h, m, s; +} m[MEN], w[WOMEN]; + +bool bpm(int u) +{ + FOR(v, women) + if (graph[u][v]) + { + if (seen[v]) + { + continue; + } + seen[v] = true; + if (matchR[v] < 0 || bpm(matchR[v])) + { + matchL[u] = v; + matchR[v] = u; + return true; + } + } + return false; +} + +int main() +{ + int T; + scanf("%d", &T); + char msc[128], sex[128], sprt[128]; + for (int n; T-- && scanf("%d", &n);) + { + int ans(n), h, mtc(0), stc(0); + men = women = 0; + mnum.clear(); + snum.clear(); + FOR(i, n) + { + scanf("%d %s %s %s", &h, sex, msc, sprt); + int &mtype = mnum[msc], &stype = snum[sprt]; + if (!mtype) + { + mtype = ++mtc; + } + if (!stype) + { + stype = ++stc; + } + person *p; + if (sex[0] == 'M') + { + p = &m[men]; + } + else + { + p = &w[women]; + } + p->h = h; + p->m = mtype; + p->s = stype; + men += sex[0] == 'M'; + women += sex[0] == 'F'; + } + memset(matchR, -1, women << 2); + memset(matchL, -1, men << 2); + FOR(i, men) + FOR(j, women) + { + graph[i][j] = abs(m[i].h - w[j].h) <= 40 && m[i].m == w[j].m && m[i].s != w[j].s; + } + FOR(i, men) + { + memset(seen, 0, women); + ans -= bpm(i); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12084.cpp b/12084.cpp new file mode 100644 index 0000000..cea3bcf --- /dev/null +++ b/12084.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; +int n, f, b, t; + +const int MAXN = 3e3 + 10; +const int MAXB = 1e6 + 10; + +vector td, tu; + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d%d%d", &n, &f, &b); + td.clear(); + tu.clear(); + for (int i = 1; i <= n; ++i) + { + int fi, bi; + scanf("%d%d", &fi, &bi); + if (bi) + { + tu.push_back(f - fi); + } + else + { + td.push_back(fi); + } + } + sort(tu.begin(), tu.end()); + sort(td.begin(), td.end()); + int sz = td.size(); + int ans = 0; + while (b > sz) + { + ans += f; + swap(tu, td); + b -= sz; + sz = td.size(); + } + ans += td[b - 1] + f; + printf("%d\n", ans); + } + return 0; +} diff --git a/12085.cpp b/12085.cpp new file mode 100644 index 0000000..de28c06 --- /dev/null +++ b/12085.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +void add_range(int start, int end) +{ + stack s_start, s_end; + while (start) + { + s_start.push(start % 10); + start /= 10; + } + while (end) + { + s_end.push(end % 10); + end /= 10; + } + while (s_start.top() == s_end.top()) + { + s_start.pop(); + s_end.pop(); + } + cout << "-"; + while (!s_end.empty()) + { + cout << s_end.top(); + s_end.pop(); + } + cout << endl; +} + +void print_results(vector &numbers) +{ + if (numbers.size() == 0) + { + return; + } + bool flag = false; + int start = numbers[0]; + cout << "0" << numbers[0]; + for (int i = 1, sz = numbers.size(); i < sz; i++) + { + if (numbers[i] - numbers[i - 1] == 1) + { + flag = true; + continue; + } + else + { + if (flag) + { + add_range(start, numbers[i - 1]); + } + else + { + cout << endl; + } + flag = false; + start = numbers[i]; + cout << "0" << numbers[i]; + } + } + if (flag) + { + add_range(start, numbers[numbers.size() - 1]); + } + else + { + cout << endl; + } +} + +int main() +{ + int n, number, case_num = 0; + vector numbers; + while (cin >> n) + { + if (n == 0) + { + break; + } + case_num++; + numbers.clear(); + while (n--) + { + cin >> number; + numbers.push_back(number); + } + sort(numbers.begin(), numbers.end()); + cout << "Case " << case_num << ":" << endl; + print_results(numbers); + cout << endl; + } + return 0; +} diff --git a/12086.cpp b/12086.cpp new file mode 100644 index 0000000..1892ca2 --- /dev/null +++ b/12086.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +struct node +{ + int l, m, r, v; +} tree[1000100]; +int res[200100]; + +void make(int l, int r, int i = 1) +{ + tree[i].l = l; + tree[i].r = r; + tree[i].m = (l + r) / 2; + if (l == r) + { + tree[i].v = res[l]; + return; + } + else + { + make(l, tree[i].m, 2 * i); + make(tree[i].m + 1, r, 2 * i + 1); + tree[i].v = tree[2 * i].v + tree[2 * i + 1].v; + } +} + +void insert(int n, int v, int i = 1) +{ + if (tree[i].l == tree[i].r && tree[i].l == n) + { + tree[i].v = v; + } + else + { + if (n <= tree[i].m) + { + insert(n, v, 2 * i); + } + else + { + insert(n, v, 2 * i + 1); + } + tree[i].v = tree[2 * i].v + tree[2 * i + 1].v; + } +} + +int count(int l, int r, int i = 1) +{ + if (tree[i].l == l && tree[i].r == r) + { + return tree[i].v; + } + else + { + if (r <= tree[i].m) + { + return count(l, r, 2 * i); + } + else if (l > tree[i].m) + { + return count(l, r, 2 * i + 1); + } + else + { + return count(l, tree[i].m, 2 * i) + count(tree[i].m + 1, r, 2 * i + 1); + } + } +} + +void setv() +{ + int n, v; + scanf("%d%d", &n, &v); + insert(n, v); +} + +void measure() +{ + int l, r; + scanf("%d%d", &l, &r); + printf("%d\n", count(l, r)); +} + +int main() +{ + int casec = 1, N, first = 1; + char t; + while (scanf("%d", &N), N != 0) + { + if (first) + { + first = 0; + } + else + { + putchar('\n'); + } + for (int i = 1; i <= N; ++i) + { + scanf("%d", res + i); + } + getchar(); + printf("Case %d:\n", casec++); + make(1, N); + while (true) + { + t = getchar(); + if (t == 'M') + { + measure(); + } + else if (t == 'S') + { + setv(); + } + else if (t == 'E') + { + while (t = getchar(), t != '\n' && t != EOF) + ; + break; + } + while (t = getchar(), t != '\n' && t != EOF) + ; + } + } + return 0; +} diff --git a/12087.cpp b/12087.cpp new file mode 100644 index 0000000..1ca022f --- /dev/null +++ b/12087.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +int sum1[600][600], sum2[600][600], c[600][600], n, m, q; +int a[600]; +int Abs(int x) +{ + if (x < 0) + { + return -x; + } + else + { + return x; + } +} +long long jisuan(int x, int n) +{ + long long sum = 0; + for (int i = 1; i <= n; ++i) + { + sum += (long long)a[i] * Abs(i - x); + } + return sum; +} +long long sanfen(int n) +{ + int l = 1, r = n; + while (l < r - 3) + { + int x1 = (l + l + r) / 3, x2 = (l + r + r) / 3; + if (jisuan(x1, n) < jisuan(x2, n)) + { + r = x2; + } + else + { + l = x1; + } + } + long long ma = (long long)1000000 * 1000000; + int k = 0; + for (int i = l; i <= r; ++i) + { + long long s = jisuan(i, n); + if (s < ma) + { + ma = s; + k = i; + } + } + return ma; +} +int main() +{ + int ttt; + cin >> ttt; + for (int tl = 1; tl <= ttt; ++tl) + { + printf("Test Case %d:\n", tl); + scanf("%d%d%d", &n, &m, &q); + memset(c, 0, sizeof(c)); + memset(sum1, 0, sizeof(sum1)); + memset(sum2, 0, sizeof(sum2)); + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= m; ++j) + { + scanf("%d", &c[i][j]); + } + for (int i = 1; i <= n; ++i) + { + sum1[i][0] = 0; + for (int j = 1; j <= m; ++j) + { + sum1[i][j] = sum1[i][j - 1] + c[i][j]; + } + } + for (int i = 1; i <= m; ++i) + { + sum2[0][i] = 0; + for (int j = 1; j <= n; ++j) + { + sum2[j][i] = sum2[j - 1][i] + c[j][i]; + } + } + for (int tt = 1; tt <= q; ++tt) + { + int x, y, xx, yy; + scanf("%d%d%d%d", &x, &y, &xx, &yy); + memset(a, 0, sizeof(a)); + for (int i = x; i <= xx; ++i) + { + a[i - x + 1] = sum1[i][yy] - sum1[i][y - 1]; + } + long long ans = sanfen(xx - x + 1); + memset(a, 0, sizeof(a)); + for (int i = y; i <= yy; ++i) + { + a[i - y + 1] = sum2[xx][i] - sum2[x - 1][i]; + } + ans += sanfen(yy - y + 1); + printf("%d %lld\n", tt, ans); + } + puts(""); + } + return 0; +} diff --git a/12088.cpp b/12088.cpp new file mode 100644 index 0000000..f92c660 --- /dev/null +++ b/12088.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +typedef long long ll; +const int INF = 0x3f3f3f3f; +const int maxn = 210; + +int T, cas = 1, n, len; +ll ans; +struct Node +{ + int idx, weight; +} a[maxn]; +char str[maxn], str2[maxn]; +int num[10]; + +bool operator<(const Node &a, const Node &b) +{ + return (a.weight == b.weight && a.idx > b.idx) || a.weight > b.weight; +} + +void dfs(int &i, int sym) +{ + int cursym = 1, cnt = 0; + while (i < len) + { + if (str[i] == '#') + { + cnt++; + i++; + } + else + { + if (cnt > 0) + { + for (int j = 1, r = 1; j <= cnt; j++, r *= 10) + { + a[n].idx = i - j; + a[n].weight = r * (sym * cursym); + n++; + } + cnt = 0; + } + if (str[i] == ')') + { + i++; + return; + } + else if (str[i] == '(') + { + i++; + dfs(i, sym * cursym); + } + else if (str[i] == '+') + { + cursym = 1; + i++; + } + else if (str[i] == '-') + { + cursym = -1; + i++; + } + else + { + return; + } + } + } +} + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%s%s", str, str2); + for (int i = 0; i < 10; i++) + { + num[i] = 0; + } + for (int i = 0; str2[i]; i++) + { + num[str2[i] - 48]++; + } + len = strlen(str); + str[len] = '$'; + len++; + str[len] = 0; + n = 0; + int i = 0; + dfs(i, 1); + sort(a, a + n); + ans = 0; + for (int i = 0, cur = 9; i < n; i++) + { + while (num[cur] == 0) + { + cur--; + } + num[cur]--; + ans += cur * a[i].weight; + str[a[i].idx] = cur + 48; + } + printf("Case %d:\n", cas++); + str[--len] = 0; + printf("%s\n", str); + printf("%lld\n", ans); + } + return 0; +} diff --git a/12089.cpp b/12089.cpp new file mode 100644 index 0000000..e7fb7c6 --- /dev/null +++ b/12089.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long long ll; + +int cas = 1, n, m, d, a, b; +double p[4]; +double dp[110][110][110]; +int s[40]; + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + memset(s, 0, sizeof(s)); + scanf("%d", &m); + for (int i = 1; i <= m; i++) + { + scanf("%d", &a); + s[a] |= 1; + } + scanf("%d", &m); + for (int i = 1; i <= m; i++) + { + scanf("%d", &b); + s[b] |= 2; + } + for (int i = 0; i < 4; i++) + { + p[i] = 0; + } + for (int i = 1; i <= n; i++) + { + p[s[i]]++; + } + for (int i = 0; i < 4; i++) + { + p[i] /= n; + } + scanf("%d", &d); + memset(dp, 0, sizeof(dp)); + dp[0][0][0] = 1; + for (int i = 0; i < d; i++) + for (int j = 0; j <= i; j++) + for (int k = 0; k <= i; k++) + { + dp[i + 1][j][k] += dp[i][j][k] * p[0]; + dp[i + 1][j + 1][k] += dp[i][j][k] * p[1]; + dp[i + 1][j][k + 1] += dp[i][j][k] * p[2]; + dp[i + 1][j + 1][k + 1] += dp[i][j][k] * p[3]; + } + double ans = 0; + for (int i = 0; i * 2 <= d; i++) + { + ans += dp[d][i][i * 2]; + } + printf("Case %d: %.5lf\n", cas++, ans); + } + return 0; +} diff --git a/1209.cpp b/1209.cpp new file mode 100644 index 0000000..ad56a51 --- /dev/null +++ b/1209.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int valueOf(string &s) +{ + int result = 0; + for (int i = 1, sz = s.size(); i < sz; i++) + { + int dist = abs(s[i] - s[i - 1]); + if (result == 0) + { + result = dist; + } + else if (dist < result) + { + result = dist; + } + } + return result; +} + +int main() +{ + string input; + pair result; + while (getline(cin, input)) + { + int count = 0; + string a(input); + result.first = valueOf(a); + result.second = a; + while (prev_permutation(a.begin(), a.end())) + { + count++; + int tmpValue = valueOf(a); + if (tmpValue >= result.first) + { + result.first = tmpValue; + result.second = a; + } + if (count == 10) + { + break; + } + } + count = 0; + while (next_permutation(input.begin(), input.end())) + { + count++; + int tmpValue = valueOf(input); + if (tmpValue > result.first) + { + result.first = tmpValue; + result.second = input; + } + if (count == 10) + { + break; + } + } + cout << result.second << result.first << endl; + } + return 0; +} diff --git a/12090.cpp b/12090.cpp new file mode 100644 index 0000000..81098c1 --- /dev/null +++ b/12090.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +const int MAX = 10000005; + +bool is[MAX]; +vector p; +vector> v; + +void pri() +{ + int i, j; + p.clear(); + for (i = 2; i < MAX; i++) + { + if (!is[i]) + { + for (j = i + i; j < MAX; j += i) + { + is[j] = true; + } + p.push_back(i); + } + } +} + +int main() +{ + int i, j; + long long n, tmp, N; + pri(); + while (scanf(" %lld", &n) == 1 && n) + { + v.clear(); + tmp = sqrt(n); + N = n; + i = 0; + while (i < p.size() && p[i] <= tmp && N > 1) + { + if (N % p[i] == 0) + { + v.push_back(make_pair(p[i], 0)); + while (N % p[i] == 0) + { + v[v.size() - 1].second++; + N /= p[i]; + } + } + i++; + } + if (N != 1) + { + v.push_back(make_pair(N, 1)); + } + // + long long ans = 0, temp = -1; + for (j = 1; temp != 0; j++) + { + temp = 1; + for (i = 0; i < v.size(); i++) + { + //printf("%d %d\n",v[i].first,v[i].second); + temp *= ((v[i].second / j) + 1); + } + temp -= 1; + ans += temp; + } + printf("%lld %lld\n", n, ans); + } + return 0; +} diff --git a/12091.cpp b/12091.cpp new file mode 100644 index 0000000..c876d79 --- /dev/null +++ b/12091.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define ll long long +#define mod 10000 +ll Pow(ll k, ll m) +{ + ll h1, h, m1; + if (m == 0) + { + return 1; + } + if (m == 1) + { + return k; + } + m1 = m / 2; + h = Pow(k, m1); + h1 = ((h % mod) * (h % mod)) % mod; + if (m % 2 == 1) + { + h1 = ((h1 % mod) * (k % mod)) % mod; + } + return h1; +} +int main(void) +{ + ll a[15], h = 1, n, t = 1, n1, l1, ans, i; + memset(a, 0, sizeof(a)); + for (i = 1; i < 15; i++) + { + a[i] = h * 9 * i; + h *= 10; + if (i != 1) + { + a[i] = (a[i] % mod + a[i - 1] % mod + mod) % mod; + } + } + while (scanf("%lld", &n) == 1) + { + if (n == 0) + { + break; + } + ans = 0; + l1 = 0; + n1 = n; + while (n1) + { + n1 /= 10; + l1++; + } + n1 = 1; + for (i = 0; i < l1 - 1; i++) + { + n1 *= 10; + } + ans = (a[l1 - 1] % mod + (n - n1 + 1) % mod * l1 % mod + ans % mod + mod - 1) % mod; + ans = (ans % mod + 4 * n % mod + mod - 2) % mod; + ans = ((Pow(2, n) - n - 1 + mod) % mod + ans % mod + mod) % mod; + ans = (ans % mod + Pow(2, n - 1) % mod * (a[l1 - 1] % mod + (n - n1 + 1) % mod * l1 % mod + n % mod) + mod) % mod; + printf("Case %lld: %lld\n", t, ans); + t++; + } + return 0; +} diff --git a/12092.cpp b/12092.cpp new file mode 100644 index 0000000..ca41cb5 --- /dev/null +++ b/12092.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define min(a, b) (a < b ? a : b) +#define max(a, b) (a > b ? a : b) + +const int maxn = 200; +const int maxm = 10010; + +struct node +{ + int from; + int to; + int next; + int c; + int f; +} egde[maxm]; +int ls; + +int box[maxn]; +int n; +int dis[maxn], load[maxn], p[maxn], ans_flow, ans_cost; + +bool flag[maxn]; +bool spfa(int start, int end) +{ + queue q; + memset(flag, false, sizeof(flag)); + memset(load, -1, sizeof(load)); + memset(dis, 0x3f, sizeof(dis)); + q.push(start); + dis[start] = 0; + flag[start] = true; + while (!q.empty()) + { + int e = q.front(); + q.pop(); + for (int i = box[e]; i != -1; i = egde[i].next) + { + if (egde[i].c) + { + int ne = egde[i].to; + if (dis[ne] - dis[e] > egde[i].f) + { + dis[ne] = dis[e] + egde[i].f; + load[ne] = i; + if (!flag[ne]) + { + flag[ne] = true; + q.push(ne); + } + } + } + } + flag[e] = false; + } + if (dis[end] == INF) + { + return false; + } + return true; +} + +void Min_Cost_Flow(int start, int end) +{ + int u, mn; + ans_flow = 0, ans_cost = 0; + while (spfa(start, end)) + { + mn = INF; + for (u = load[end]; u != -1; u = load[egde[u].from]) + { + mn = min(egde[u].c, mn); + } + for (u = load[end]; u != -1; u = load[egde[u].from]) + { + egde[u].c -= mn; + egde[u ^ 1].c += mn; + } + ans_cost += dis[end] * mn; + ans_flow += mn; + } + return; +} + +void add(int t1, int t2, int t3, int c) +{ + egde[ls].from = t1; + egde[ls].to = t2; + egde[ls].f = t3; + egde[ls].c = c; + egde[ls].next = box[t1]; + box[t1] = ls++; +} + +int main() +{ + int index; + scanf("%d", &index); + while (index--) + { + int t, m, k; + memset(box, 0xff, sizeof(box)); + scanf("%d%d%d", &n, &m, &k); + t = n; + ls = 0; + for (int i = 0; i < m; i++) + { + int t1, t2, t3; + scanf("%d%d%d", &t1, &t2, &t3); + add(t1, t2 + n, t3, 1); + add(t2 + n, t1, -t3, 0); + } + for (int i = 0; i < n; i++) + { + add(2 * n, i, 0, k); + add(i, 2 * n, 0, 0); + add(i + n, 2 * n + 1, 0, k); + add(2 * n + 1, i + n, 0, 0); + } + n = n * 2 + 2; + Min_Cost_Flow(n - 2, n - 1); + printf("%d\n", ans_flow == t * k ? ans_cost : -1); + } + return 0; +} diff --git a/12093.cpp b/12093.cpp new file mode 100644 index 0000000..3e0847a --- /dev/null +++ b/12093.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const int N = 100050; + +struct edge +{ + int u, v, next; +} e[N << 1]; + +int dp[N][5], n, x, y; +int head[N], tot; + +void add_edge(int u, int v) +{ + e[tot].u = u, e[tot].v = v; + e[tot].next = head[u], head[u] = tot++; +} + +void dfs(int f, int u) +{ + // printf("f:%d u:%d\n",f,u); + bool leaf = true; + dp[u][0] = x, dp[u][1] = y; + dp[u][2] = 0, dp[u][3] = dp[u][4] = 0; + int sum = 0, spe = int(1e9); + for (int i = head[u]; i != -1; i = e[i].next) + { + int v = e[i].v; + if (v == f) + { + continue; + } + dfs(u, v); + dp[u][0] += min(min(dp[v][0], dp[v][1]), min(dp[v][2], dp[v][3])); + dp[u][1] += min(min(dp[v][0], dp[v][1]), min(dp[v][2], min(dp[v][4], dp[v][3]))); + dp[u][3] += min(min(dp[v][0], dp[v][1]), dp[v][2]); + dp[u][4] += min(min(dp[v][0], dp[v][1]), min(dp[v][2], dp[v][3])); + int superSonMin = (int)1e9; + superSonMin = min(min(dp[v][2], dp[v][3]), min(dp[v][0], dp[v][1])); + sum += superSonMin; + spe = min(spe, dp[v][1] - superSonMin); + leaf = false; + } + dp[u][2] = sum + spe; + if (leaf) + { + dp[u][0] = x, dp[u][1] = y; + dp[u][2] = (int)1e9, dp[u][3] = 0; + dp[u][4] = 0; + } +} + +int main() +{ + // freopen("input.txt","r",stdin); + while (scanf("%d%d%d", &n, &x, &y) != EOF && n + x + y) + { + memset(head, -1, sizeof(head)); + tot = 0; + // printf("n:%d x:%d y:%d\n",n,x,y); + for (int i = 1; i < n; i++) + { + int u, v; + scanf("%d%d", &u, &v); + add_edge(u, v); + add_edge(v, u); + } + memset(dp, 0, sizeof(dp)); + dfs(-1, 1); + /*for(int i = 1;i <= n;i ++){ + printf("i:%d 0:%d 1:%d 2:%d 3:%d 4:%d\n",i,dp[i][0],dp[i][1],dp[i][2],dp[i][3],dp[i][4]); + }*/ + int ans = min(dp[1][0], dp[1][1]); + ans = min(ans, dp[1][2]); + ans = min(ans, dp[1][3]); + printf("%d\n", ans); + } + return 0; +} diff --git a/12094.cpp b/12094.cpp new file mode 100644 index 0000000..edcc026 --- /dev/null +++ b/12094.cpp @@ -0,0 +1,274 @@ +#include + +using namespace std; + +#define eps 1e-6 +const double pi = acos(-1); +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + return y < a.y; + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator*(const double &a) const + { + return Pt(x * a, y * a); + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double length(Pt a) +{ + return hypot(a.x, a.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double angle(Pt a, Pt b) +{ + return acos(dot(a, b) / length(a) / length(b)); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} +Pt getIntersection(Pt p, Pt l1, Pt q, Pt l2) +{ + double a1, a2, b1, b2, c1, c2; + double dx, dy, d; + a1 = l1.y, b1 = -l1.x, c1 = a1 * p.x + b1 * p.y; + a2 = l2.y, b2 = -l2.x, c2 = a2 * q.x + b2 * q.y; + d = a1 * b2 - a2 * b1; + dx = b2 * c1 - b1 * c2; + dy = a1 * c2 - a2 * c1; + return Pt(dx / d, dy / d); +} +struct Line +{ + int A, B, C; + bool operator<(const Line &a) const + { + double t1 = atan2(B, A); + double t2 = atan2(a.B, a.A); + if (t1 < 0) + t1 += pi; + if (t2 < 0) + t2 += pi; + return t1 < t2; + } +}; +Pt getIntersection(Line l1, Line l2) +{ + double a1, a2, b1, b2, c1, c2; + double dx, dy, d; + a1 = l1.A, b1 = l1.B, c1 = -l1.C; + a2 = l2.A, b2 = l2.B, c2 = -l2.C; + d = a1 * b2 - a2 * b1; + dx = b2 * c1 - b1 * c2; + dy = a1 * c2 - a2 * c1; + return Pt(dx / d, dy / d); +} +struct cmp +{ + static Line base; + bool operator()(const Pt &p1, const Pt &p2) const + { + double c1, c2; + c1 = p1.x * base.A + p1.y * base.B; + c2 = p2.x * base.A + p2.y * base.B; + if (fabs(c1 - c2) > eps) + return c1 < c2; + return false; + } +}; +Line cmp::base; +int main() +{ + int cases = 0; + int n, m, q; + double x, y; + while (scanf("%d %d %d", &n, &m, &q) == 3 && n) + { + vector soldiers, tanks; + vector soldier[3], tank[3]; + Line Q[10000][3]; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x, &y); + soldiers.push_back(Pt(x, y)); + } + for (int i = 0; i < m; i++) + { + scanf("%lf %lf", &x, &y); + tanks.push_back(Pt(x, y)); + } + for (int i = 0; i < q; i++) + { + for (int j = 0; j < 3; j++) + { + scanf("%d %d %d", &Q[i][j].A, &Q[i][j].B, &Q[i][j].C); + if (Q[i][j].A < 0 || (Q[i][j].A == 0 && Q[i][j].B < 0)) + { + Q[i][j].A = -Q[i][j].A; + Q[i][j].B = -Q[i][j].B; + Q[i][j].C = -Q[i][j].C; + } + } + sort(Q[i], Q[i] + 3); + } + for (int i = 0; i < 3; i++) + { + soldier[i] = soldiers; + tank[i] = tanks; + cmp::base = Q[0][i]; + sort(soldier[i].begin(), soldier[i].end(), cmp()); + sort(tank[i].begin(), tank[i].end(), cmp()); + } + // for (int i = 0; i < 3; i++) { + // for (int j = 0; j < soldier[i].size(); j++) + // printf("%.2lf %.2lf ,", soldier[i][j].x, soldier[i][j].y); + // puts("\n--++--"); + // } + printf("Battle Field %d:\n", ++cases); + for (int i = 0; i < q; i++) + { + // for (int j = 0; j < 3; j++) + // printf("%d %d %d -\n", Q[i][j].A, Q[i][j].B, Q[i][j].C); + Pt pabc[3]; + pabc[0] = getIntersection(Q[i][1], Q[i][2]);// bc + pabc[1] = getIntersection(Q[i][0], Q[i][2]);// ac + pabc[2] = getIntersection(Q[i][0], Q[i][1]);// ab + int ret1 = 0, ret2 = 0; + int tmp[3]; + for (int j = 0; j < 3; j++) + { + cmp::base = Q[i][j]; + tmp[j] = (int)(lower_bound(soldier[j].begin(), soldier[j].end(), + pabc[(j + 1) % 3], cmp()) - + soldier[j].begin()); + int v1 = (Q[i][j].A * pabc[j].x + Q[i][j].B * pabc[j].y + Q[i][j].C < 0); + int v2 = (Q[i][j].A * soldier[j][0].x + Q[i][j].B * soldier[j][0].y + Q[i][j].C < 0); + int v3 = (Q[i][j].A * soldier[j][soldier[j].size() - 1].x + Q[i][j].B * soldier[j][soldier[j].size() - 1].y + Q[i][j].C < 0); + + if (j == 0) + { + if (tmp[j]) + { + if (v1 != v2) + tmp[j] = soldier[j].size() - tmp[j]; + } + else + { + if (v1 == v3) + tmp[j] = soldier[j].size() - tmp[j]; + } + } + else + { + if (tmp[j]) + { + if (v1 == v2) + tmp[j] = soldier[j].size() - tmp[j]; + } + else + { + if (v1 != v3) + tmp[j] = soldier[j].size() - tmp[j]; + } + } + // printf("[%d] soldier %d\n", j, tmp[j]); + } + ret1 = tmp[0] - tmp[1] - tmp[2]; + for (int j = 0; j < 3; j++) + { + cmp::base = Q[i][j]; + tmp[j] = (int)(lower_bound(tank[j].begin(), tank[j].end(), + pabc[(j + 1) % 3], cmp()) - + tank[j].begin()); + int v1 = (Q[i][j].A * pabc[j].x + Q[i][j].B * pabc[j].y + Q[i][j].C < 0); + int v2 = (Q[i][j].A * tank[j][0].x + Q[i][j].B * tank[j][0].y + Q[i][j].C < 0); + int v3 = (Q[i][j].A * tank[j][tank[j].size() - 1].x + Q[i][j].B * tank[j][tank[j].size() - 1].y + Q[i][j].C < 0); + + if (j == 0) + { + if (tmp[j]) + { + if (v1 != v2) + tmp[j] = tank[j].size() - tmp[j]; + } + else + { + if (v1 == v3) + tmp[j] = tank[j].size() - tmp[j]; + } + } + else + { + if (tmp[j]) + { + if (v1 == v2) + tmp[j] = tank[j].size() - tmp[j]; + } + else + { + if (v1 != v3) + tmp[j] = tank[j].size() - tmp[j]; + } + } + // printf("[%d] tank %d\n", j, tmp[j]); + } + ret2 = tmp[0] - tmp[1] - tmp[2]; + printf("Query %d: %d %d\n", i + 1, ret1, ret2); + } + } + return 0; +} +/* +8 5 2 +-1 8 +-7 3 +-2 1 +-2 -1 +-5 -2 +6 -1 +2 -4 +-4 -5 +1 7 +1 1 +3 4 +-6 5 +-12 -6 +2 -2 10 +-2 6 6 +-5 -3 1 +1 -1 5 +1 -3 -3 +5 3 -15 +0 0 0 +*/ diff --git a/12095.cpp b/12095.cpp new file mode 100644 index 0000000..fff106a --- /dev/null +++ b/12095.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +/** + * Fix the position of one 3x3 block at a time, then try all + * permutations within that block and proceed to the next 3x3-block. + * Keep a partial remapping of the digits as we go, and break whenever + * inconsistencies are found. + */ + +int b1[9][9], b2[9][9]; +int brperm[3], bcperm[3]; +int rperm[3][3], cperm[3][3]; + +bool tryperm(int blockrow, int blockcol, int *digmap) +{ + if (blockcol == 3) + { + ++blockrow, blockcol = 0; + } + if (blockrow == 3) + { + return true; + } + for (int i = 0; i < 3; ++i) + if (blockcol == 0 && brperm[i] == -1 || + blockcol > 0 && brperm[i] == blockrow) + for (int j = 0; j < 3; ++j) + if (blockrow == 0 && bcperm[j] == -1 || + blockrow > 0 && bcperm[j] == blockcol) + { + int rp[3], cp[3]; + brperm[i] = blockrow; + bcperm[j] = blockcol; + for (int k = 0; k < 3; ++k) + { + rp[k] = cp[k] = k; + } + // Check if any of these permutations have already been fixed. + if (blockrow > 0) + { + memcpy(cp, cperm[blockcol], sizeof(cp)); + } + if (blockcol > 0) + { + memcpy(rp, rperm[blockrow], sizeof(rp)); + } + do + { + do + { + // Check that row and column permutations for this block + // are consistent with the current remapping of the digits. + bool inconsistent = false; + int ndigmap[10]; + memcpy(ndigmap, digmap, sizeof(int) * 10); + for (int r = 0; !inconsistent && r < 3; ++r) + for (int c = 0; !inconsistent && c < 3; ++c) + { + int v1 = b1[3 * blockrow + r][3 * blockcol + c]; + int v2 = b2[3 * i + rp[r]][3 * j + cp[c]]; + if (v2) + { + if (ndigmap[v2] && ndigmap[v2] != v1) + { + inconsistent = true; + } + ndigmap[v2] = v1; + } + } + memcpy(cperm[blockcol], cp, sizeof(cp)); + memcpy(rperm[blockrow], rp, sizeof(rp)); + if (!inconsistent && + tryperm(blockrow, blockcol + 1, ndigmap)) + { + return true; + } + } while (blockrow == 0 && next_permutation(cp, cp + 3)); + } while (blockcol == 0 && next_permutation(rp, rp + 3)); + // Restore block permutations + if (blockcol == 0) + { + brperm[i] = -1; + } + if (blockrow == 0) + { + bcperm[j] = -1; + } + } + return false; +} + +int main() +{ + int T, i; + scanf("%d", &T); + for (; T--;) + { + for (i = 0; i < 81; ++i) + { + scanf(" %1d", b1[i / 9] + i % 9); + } + for (i = 0; i < 81; ++i) + { + scanf(" %1d", b2[i / 9] + i % 9); + } + // Only need to check rotation by 90 degrees since additional + // rotation by 180 degrees can be achieved by the permutations. + for (i = 0; i < 2; ++i) + { + int digmap[10]; + int nb2[9][9]; + for (int r = 0; r < 9; ++r) + for (int c = 0; c < 9; ++c) + { + nb2[r][c] = b2[c][8 - r]; + } + memcpy(b2, nb2, sizeof(b2)); + memset(brperm, -1, sizeof(brperm)); + memset(bcperm, -1, sizeof(bcperm)); + memset(digmap, 0, sizeof(digmap)); + if (tryperm(0, 0, digmap)) + { + printf("Yes\n"); + break; + } + } + if (i == 2) + { + printf("No\n"); + } + } + return 0; +} diff --git a/12096.cpp b/12096.cpp new file mode 100644 index 0000000..27ca2c8 --- /dev/null +++ b/12096.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +/** + * Algorithm: give each set a unique integer ID, represent a set of + * sets as a set of IDs. + */ + +typedef set Set; + +vector setstack; +map setid; +Set sets[1000000]; + +int SetId(const Set &s) +{ + int &r = setid[s]; + if (!r) + { + r = setid.size(), sets[r - 1] = s; + } + return r - 1; +} + +int pop() +{ + int x = setstack.back(); + setstack.pop_back(); + return x; +} + +void solve(int n) +{ + char cmd[30]; + setid.clear(); + setstack.clear(); + SetId(sets[0]); + for (int i = 0; i < n && scanf("%s", cmd); ++i) + { + Set res, sub, X, Y; + int x, y; + switch (*cmd) + { + case 'P': + setstack.push_back(0); + break; + case 'D': + setstack.push_back(setstack.back()); + break; + case 'U': + x = pop(); + y = pop(); + set_union(sets[x].begin(), sets[x].end(), + sets[y].begin(), sets[y].end(), + inserter(res, res.begin())); + setstack.push_back(SetId(res)); + break; + case 'I': + x = pop(); + y = pop(); + set_intersection(sets[x].begin(), sets[x].end(), + sets[y].begin(), sets[y].end(), + inserter(res, res.begin())); + setstack.push_back(SetId(res)); + break; + case 'A': + x = pop(); + y = pop(); + res = sets[y]; + res.insert(x); + setstack.push_back(SetId(res)); + break; + case 'X': + X = sets[pop()]; + Y = sets[pop()]; + for (Set::iterator it = X.begin(); it != X.end(); ++it) + for (Set::iterator jt = Y.begin(); jt != Y.end(); ++jt) + { + sub.clear(); + sub.insert(*it); + sub.insert(*jt); + res.insert(SetId(sub)); + } + setstack.push_back(SetId(res)); + break; + default: + assert(0); + } + printf("%d\n", (int)sets[setstack.back()].size()); + } + printf("***\n"); +} + +int main() +{ + int t, n; + for (scanf("%d", &t); scanf("%d", &n), t--; solve(n)) + ; + return 0; +} diff --git a/12097.cpp b/12097.cpp new file mode 100644 index 0000000..4cc2e7a --- /dev/null +++ b/12097.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int rmax = 10000; + +int main() +{ + int runs; + scanf("%i", &runs); + while (runs--) + { + int N, F; + scanf("%i %i", &N, &F); + vector r(N); + for (int i = 0; i < N; i++) + { + scanf("%i", &r[i]); + } + double lo = 0, hi = M_PI * rmax * rmax; + for (int go = 0; go < 100; go++) + { + double x = (lo + hi) / 2; + int cnt = 0; + for (int i = 0; i < N; i++) + { + cnt += (int)floor(M_PI * r[i] * r[i] / x); + } + if (cnt < F + 1) + { + hi = x; + } + else + { + lo = x; + } + } + printf("%.3f\n", lo); + } + return 0; +} diff --git a/12098.cpp b/12098.cpp new file mode 100644 index 0000000..c537f69 --- /dev/null +++ b/12098.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +/** + Brute-force + Try all ways of partitioning 4 train lines together + then compute cost of a min steiner tree on the partition + + to do so, on a set of k nodes: + - select up to k-2 of remaining nodes + - compute MST on up to 2k-2 nodes + - ~(n-k choose k-2) ways of choosing k-2 nodes ≤ 22 C 6 ~= 75k +*/ + +const int inf = 1 << 26; + +int msts, n, dist[40][40], cityset[40], inset[40]; +pair routes[4]; + +// Prim MST +int mst(int cities) +{ + ++msts; + int dst[40], used[40], res = 0; + for (int i = 0; i < cities; ++i) + { + dst[i] = inf, used[i] = false; + } + dst[0] = 0; + for (int i = 0; i < cities; ++i) + { + int bj = -1; + for (int j = 0; j < cities; ++j) + if (!used[j] && (bj == -1 || dst[j] < dst[bj])) + { + bj = j; + } + used[bj] = true; + res += dst[bj]; + for (int j = 0; j < cities; ++j) + { + dst[j] = min(dst[j], dist[cityset[bj]][cityset[j]]); + } + } + return res; +} + +// recursively choose internal nodes in the steiner tree +int steinertree(int internal, int from, int *out) +{ + if (internal <= 0) + { + return mst(out - cityset); + } + int res = mst(out - cityset); + for (int i = from; i < n; ++i) + { + if (inset[i]) + { + continue; + } + *out = i; + res = min(res, steinertree(internal - 1, i + 1, out + 1)); + } + return res; +} + +// Compute a steiner tree on the set of routes +// indicated by the four lowest bits of left +int steinerforest(int left) +{ + if (!left) + { + return 0; + } + int m = 0x10, res = 1 << 28; + while (!(left & m)) + { + m >>= 1; + } + assert(m < 0x10); + for (int s = 0; s < m; ++s) + { + if ((left | s) == left) + { + int size = 0; + memset(inset, false, sizeof(inset)); + for (int i = 0; i < 4; ++i) + if ((m | s) & (1 << i)) + { + if (!inset[routes[i].first]) + { + inset[cityset[size++] = routes[i].first] = true; + } + if (!inset[routes[i].second]) + { + inset[cityset[size++] = routes[i].second] = true; + } + } + int cost = steinertree(size - 2, 0, cityset + size); + res = min(res, cost + steinerforest(left & ~(m | s))); + } + } + return res; +} + +int main() +{ + int m; + while (scanf("%d%d", &n, &m) == 2 && n) + { + map id; + char name1[50], name2[50]; + memset(dist, 0x1f, sizeof(dist)); + for (int i = 0; i < n; ++i) + { + scanf("%s", name1); + id[name1] = i; + dist[i][i] = 0; + } + for (int i = 0; i < m; ++i) + { + int d; + scanf("%s%s%d", name1, name2, &d); + int a = id[name1], b = id[name2]; + dist[a][b] = min(dist[a][b], d); + dist[b][a] = min(dist[b][a], d); + } + for (int k = 0; k < n; ++k) + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); + } + for (int i = 0; i < 4; ++i) + { + scanf("%s%s", name1, name2); + routes[i].first = id[name1]; + routes[i].second = id[name2]; + } + // fprintf(stderr, "go %d %d\n", n, m); + int res = steinerforest(0xF); + printf("%d\n", res); + // fprintf(stderr, "%d msts needed to be computed\n", msts); + msts = 0; + } + return 0; +} diff --git a/12099.cpp b/12099.cpp new file mode 100644 index 0000000..0be6d99 --- /dev/null +++ b/12099.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +/* Sample solution for NWERC'06: The Bookcase + * Author: Per Austrin + */ + +typedef pair pii; +typedef vector vi; + +const int inf = 0x1f1f1f1f; +const int MAX2 = 1700; + +pii books[100]; +int tsum[100], minh[2000000]; + +int encode(int w1, int w2, int w3) +{ + w3 -= w1 + w2; + if (w3 < w2) + { + swap(w3, w2); + } + if (w2 < w1) + { + swap(w2, w1); + } + if (w3 < w2) + { + swap(w3, w2); + } + return MAX2 * w1 + w2; +} + +int main() +{ + int t, n; + for (scanf("%d", &t); scanf("%d", &n) && t--;) + { + for (int i = 0; i < n; ++i) + { + scanf("%d%d", &books[i].first, &books[i].second); + } + sort(books, books + n, greater()); + tsum[0] = books[0].second; + for (int i = 1; i < n; ++i) + { + tsum[i] = tsum[i - 1] + books[i].second; + } + vi idxs, nidxs; + memset(minh, inf, sizeof(minh)); + minh[0] = books[0].first; + idxs.push_back(0); + int ans = 1 << 30; + int kills = 0; + for (int i = 1; i < n; ++i) + { + kills = 0; + sort(idxs.begin(), idxs.end()); + nidxs.clear(); + for (int j = idxs.size(); j--;) + { + int idx = idxs[j]; + int w1 = idx / MAX2, w2 = idx % MAX2; + int H = minh[idx], st; + // These two if statements are just pruning, their only + // purpose is to speed up the solution. + if (w1) + { + ans = min(ans, H * max(tsum[n - 1] - w1 - w2, max(w1, w2))); + } + if (H * max(tsum[i - 1] - w1 - w2, max(w1, w2)) >= ans) + { + ++kills; + minh[idx] = inf; + continue; + } + nidxs.push_back(idx); + st = encode(w1 + books[i].second, w2, tsum[i]); + if (minh[st] == inf) + { + nidxs.push_back(st); + } + minh[st] = min(minh[st], H + (w1 ? 0 : books[i].first)); + st = encode(w1, w2 + books[i].second, tsum[i]); + if (minh[st] == inf) + { + nidxs.push_back(st); + } + minh[st] = min(minh[st], H + (w2 ? 0 : books[i].first)); + } + idxs.swap(nidxs); + } + for (vi::iterator it = idxs.begin(); it != idxs.end(); ++it) + { + int w1 = *it / MAX2, w2 = *it % MAX2; + if (w1) + { + ans = min(ans, minh[*it] * max(tsum[n - 1] - w1 - w2, max(w1, w2))); + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/121.cpp b/121.cpp new file mode 100644 index 0000000..d94f881 --- /dev/null +++ b/121.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int cnt_skew(double a, double b) +{ + int cols, rows; + bool same = (a - int(a) >= 0.5); + cols = int(a); + rows = 2.0 * (b - 1) / sqrt(3) + 1; + return cols * rows - (same ? 0 : rows / 2); +} + +int main() +{ + double a, b; + while (scanf("%lf%lf", &a,&b) == 2) + { + if (a < 1 || b < 1) + { + printf("0 grid\n"); + continue; + } + int grid = int(a) * int(b); + int skew = cnt_skew(a, b); + skew = max(skew, cnt_skew(b, a)); + if (skew > grid) + { + cout << skew << " skew" << endl; + } + else + { + cout << grid << " grid" << endl; + } + } + return 0; +} diff --git a/1210.cpp b/1210.cpp index 9834ebf..0e77f7d 100644 --- a/1210.cpp +++ b/1210.cpp @@ -1,54 +1,59 @@ -#include -#include -#include -using namespace std; - -#define MAX 10000 -bool Prime[MAX + 1]; -int Count[MAX + 1]; -vector< int > P; - -void Sieve() { - memset(Prime, true, sizeof Prime); - P.clear(); - Prime[0] = Prime[1] = false; - - for (int i = 2; i <= MAX; i++) { - if (Prime[i]) { - P.push_back(i); - for (int j = i * i; j <= MAX; j += i) - Prime[j] = false; - } - } - //printf("%d\n", P.size()); -} +#include -void Calc() { - memset(Count, 0, sizeof Count); - int N = P.size(); +using namespace std; - for (int i = 0; i < N; i++) { - int sum = 0; - for (int j = i; j < N; j++) { - sum += P[j]; - if (sum > MAX) continue; - ++Count[sum]; - } - } +int result[10001]; + +bool is_prime(int n) +{ + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; + } + } + return true; } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - Sieve(); - Calc(); - - while (true) { - int N; - scanf("%d", &N); - if (!N) break; - - printf("%d\n", Count[N]); - } - return 0; +int main() +{ + int n; + for (int i = 0; i < 10001; i++) + { + result[i] = 0; + } + vector primes; + primes.push_back(2); + for (int i = 3; i < 10001; i += 2) + { + if (is_prime(i)) + { + primes.push_back(i); + } + } + for (int i = 0, sz = primes.size(); i < sz; i++) + { + for (int j = i; j < sz; j++) + { + int count = 0; + for (int k = i; k < j; k++) + { + count += primes[k]; + } + if (count < 10001) + { + result[count] += 1; + } + } + } + while (cin >> n) + { + if (!n) + { + break; + } + cout << result[n] << endl; + } + return 0; } diff --git a/12100.cpp b/12100.cpp new file mode 100644 index 0000000..4a85abf --- /dev/null +++ b/12100.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +/************************************************************************************************************************* + * 12100 + * + * + * Classification: + * + * Author: + * Javier Gonzalez Nova + * + * Description: + * + * + */ + +typedef struct +{ + int value; + bool mine; +} Job; + +vector Jobs_value; + +bool isHigher(int num) +{ + int index; + for (int i = 0; i < Jobs_value.size(); i++) + { + if (num < Jobs_value[i]) + { + return false; + } + if (num == Jobs_value[i]) + { + index = i; + } + } + Jobs_value[index] = 0; + return true; +} + +int main() +{ + int c; /*Cantidad de casos*/ + int num_jobs, pos_job; /*Numero de trabajos, posicion de mi trabajo*/ + cin >> c; + while (c--) + { + cin >> num_jobs >> pos_job; + queue Jobs_queue; + Jobs_value.clear(); + for (int i = 0; i < num_jobs; i++) + { + int tmp; + cin >> tmp; + Job t; + if (i == pos_job) + { + t.mine = true; + } + else + { + t.mine = false; + } + t.value = tmp; + Jobs_queue.push(t); + Jobs_value.push_back(tmp); + } + int minutes = 0; + while (1) + { + Job v = Jobs_queue.front(); + Jobs_queue.pop(); + if (isHigher(v.value)) + { + if (v.mine) + { + cout << minutes + 1 << endl; + break; + } + else + { + minutes++; + } + } + else + { + Jobs_queue.push(v); + } + } + } + return 0; +} diff --git a/12101.cpp b/12101.cpp new file mode 100644 index 0000000..df6fc41 --- /dev/null +++ b/12101.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int a, b; +int p[9999] = {0}; +int visited[9999] = {0}; +bool isprime(int x) +{ + for (int i = 2; i <= sqrt((double)x); ++i) + { + if (x % i == 0) + { + return false; + } + } + return true; +} +int BFS(int s, int r) +{ + queue q; + q.push(s); + p[s] = 0; + visited[s] = 1; + while (!q.empty()) + { + int temp = q.front(); + q.pop(); + for (int i = 0; i <= 9; i++) + { + int y1 = (temp / 10) * 10 + i; + if (isprime(y1) && !visited[y1]) + { + q.push(y1); + p[y1] = p[temp] + 1; + visited[y1] = 1; + } + int y2 = temp % 10 + (temp / 100) * 100 + i * 10; + if (isprime(y2) && !visited[y2]) + { + q.push(y2); + p[y2] = p[temp] + 1; + visited[y2] = 1; + } + int y3 = temp % 100 + (temp / 1000) * 1000 + 100 * i; + if (isprime(y3) && !visited[y3]) + { + q.push(y3); + p[y3] = p[temp] + 1; + visited[y3] = 1; + } + if (i != 0) + { + int y4 = temp % 1000 + i * 1000; + if (isprime(y4) && !visited[y4]) + { + q.push(y4); + p[y4] = p[temp] + 1; + visited[y4] = 1; + } + } + if (visited[r]) + { + return p[r]; + } + } + } + return 0; +} +int main() +{ + int n; + cin >> n; + while (n--) + { + memset(visited, 0, sizeof(visited)); + memset(p, 0, sizeof(p)); + cin >> a >> b; + cout << BFS(a, b) << endl; + } + return 0; +} diff --git a/12102.cpp b/12102.cpp new file mode 100644 index 0000000..e78fd3a --- /dev/null +++ b/12102.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +/* + [NWERC'06] Mountopia's airport + by: Jan Kuipers +*/ + +int N, L; +vector x, y; + +double area(double xx) +{ + int i = 0; + while (x[i + 1] < xx) + { + i++; + } + if (x[i + 1] >= xx + L) + { + double y1 = y[i] + (xx - x[i]) / (x[i + 1] - x[i]) * (y[i + 1] - y[i]); + double y2 = y[i] + (xx + L - x[i]) / (x[i + 1] - x[i]) * (y[i + 1] - y[i]); + return L * fabs(y2 - y1) / 2; + } + double yy = y[i] + (xx - x[i]) / (x[i + 1] - x[i]) * (y[i + 1] - y[i]); + double low = yy; + double res = (x[i + 1] - xx) * (y[i + 1] + yy) / 2; + i++; + while (x[i + 1] < xx + L) + { + low = min(low, y[i]); + res += (x[i + 1] - x[i]) * (y[i + 1] + y[i]) / 2; + i++; + } + low = min(low, y[i]); + yy = y[i] + (xx + L - x[i]) / (x[i + 1] - x[i]) * (y[i + 1] - y[i]); + low = min(low, yy); + res += (xx + L - x[i]) * (y[i] + yy) / 2; + return res - low * L; +} + +int main() +{ + int runs; + cin >> runs; + while (runs--) + { + cin >> N >> L; + x = y = vector(N); + for (int i = 0; i < N; i++) + { + cin >> x[i] >> y[i]; + } + double res = 1e9; + for (int fr = 0; fr < N - 1; fr++) + for (int to = fr; to < N - 1; to++) + { + if (x[to] - x[fr + 1] > L) + { + continue; + } + if (x[to + 1] - x[fr] < L) + { + continue; + } + double lo = max(x[to] - L, x[fr]); + double hi = min(x[to + 1] - L, x[fr + 1]); + for (int go = 0; go < 100; go++) + { + double x1 = (2 * lo + hi) / 3; + double x2 = (lo + 2 * hi) / 3; + double area1 = area(x1); + double area2 = area(x2); + if (area1 < area2) + { + hi = x2; + } + else + { + lo = x1; + } + } + res = min(res, area(lo)); + } + cout << fixed << res << endl; + } + return 0; +} diff --git a/12103.cpp b/12103.cpp new file mode 100644 index 0000000..6b29dfd --- /dev/null +++ b/12103.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +/** + * Verify every even cycle length must have even multiplicity + */ + +int main() +{ + char str[42]; + int t, l, i, j, f[42], b; + for (scanf("%d", &t); scanf("%s", str), t--; printf("%s\n", b ? "No" : "Yes")) + for (memset(f, i = 0, sizeof(f)); i <= 26; ++i, ++f[l]) + if (i < 26) + for (l = 0, j = i; !l || j != i; ++l, j = str[j] - 'A') + ; + else + for (l = b = 0; (l += 2) <= 26;) + { + b |= f[l] / l & 1; + } + return 0; +} diff --git a/12105.cpp b/12105.cpp new file mode 100644 index 0000000..7e9e8b0 --- /dev/null +++ b/12105.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +char tam[110][3010]; +char pd[101][3010][51]; +int cost[20] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; +int n, m, caso, ant = -1; +struct state +{ + int a, b, id; +} input[10000]; +int T; +char saida[10000][55]; +int read() +{ + T = 0; + while (scanf("%d %d", &input[T].a, &input[T].b) == 2) + { + input[T].id = T; + T++; + } + return 1; +} +void process() +{ + int atual = input[caso].id; + n = input[caso].a; + m = input[caso].b; + if (m == 0) + { + if (n < 6) + { + strcpy(saida[atual], "-1"); + } + else + { + strcpy(saida[atual], "0"); + } + return; + } + int q, resto, v; + if (m != ant) + { + memset(tam, -1, sizeof(tam)); + ant = m; + pd[0][0][0] = 0; + tam[0][0] = 0; + for (q = 0; q < n; q++) + { + for (resto = 0; resto < m; resto++) + { + if (tam[q][resto] < 0) + { + continue; + } + for (v = (tam[q][resto] == 0) ? 1 : 0; v < 10; v++) + { + if (q + cost[v] <= n) + { + if (tam[q + cost[v]][(resto * 10 + v) % m] < tam[q][resto] + 1) + { + tam[q + cost[v]][(resto * 10 + v) % m] = tam[q][resto] + 1; + strcpy(pd[q + cost[v]][(resto * 10 + v) % m], pd[q][resto]); + pd[q + cost[v]][(resto * 10 + v) % m][tam[q][resto]] = v + '0'; + pd[q + cost[v]][(resto * 10 + v) % m][tam[q][resto] + 1] = 0; + } + else if (tam[q + cost[v]][(resto * 10 + v) % m] == tam[q][resto] + 1) + { + pd[q][resto][tam[q][resto]] = (v + '0'); + pd[q][resto][tam[q][resto] + 1] = 0; + if (strcmp(pd[q][resto], pd[q + cost[v]][(resto * 10 + v) % m]) > 0) + { + strcpy(pd[q + cost[v]][(resto * 10 + v) % m], pd[q][resto]); + } + pd[q][resto][tam[q][resto]] = 0; + } + } + } + } + } + } + int ret = -1; + int tr = 0; + for (q = 1; q <= n; q++) + { + if (tam[q][0] <= 0) + { + continue; + } + if (tam[q][0] > tr) + { + tr = tam[q][0]; + ret = q; + } + else if (tam[q][0] == tr) + { + if (strcmp(pd[q][0], pd[ret][0]) > 0) + { + ret = q; + } + } + } + if (ret == -1) + { + if (n >= 6) + { + strcpy(saida[atual], "0"); + } + else + { + strcpy(saida[atual], "-1"); + } + } + else + { + strcpy(saida[atual], pd[ret][0]); + } +} +int comp1(const void *a, const void *b) +{ + state a1 = *(state *)a; + state a2 = *(state *)b; + if (a1.b == a2.b) + { + return a2.a - a1.a; + } + return a1.b - a2.b; +} +int comp2(const void *a, const void *b) +{ + state a1 = *(state *)a; + state a2 = *(state *)b; + return a1.id - a2.id; +} +int main() +{ + read(); + qsort(input, T, sizeof(input[0]), comp1); + for (caso = 0; caso < T; caso++) + { + process(); + } + qsort(input, T, sizeof(input[0]), comp2); + for (caso = 0; caso < T; caso++) + { + printf("Case %d: %s\n", caso + 1, saida[caso]); + } + return 0; +} diff --git a/12107.cpp b/12107.cpp new file mode 100644 index 0000000..af154fc --- /dev/null +++ b/12107.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +const int maxn = 10; + +bool flag; +int tot, t; +char s[3][maxn]; +char ans[3][maxn]; + +bool is_ok() +{ + int a = 0, b = 0, c; + for (int i = 0; i < strlen(s[0]); i++) + { + a = a * 10 + s[0][i] - '0'; + } + for (int i = 0; i < strlen(s[1]); i++) + { + b = b * 10 + s[1][i] - '0'; + } + c = a * b; + char cur[maxn]; + for (int i = strlen(s[2]) - 1; i >= 0; i--) + { + cur[i] = '0' + c % 10; + c /= 10; + } + if (c > 0 || cur[0] == '0') + { + return false; + } + for (int i = 0; i < strlen(s[2]); i++) + if (s[2][i] != '*' && s[2][i] != cur[i]) + { + return false; + } + return true; +} + +void dfs(int a, int b) +{ + if (b >= strlen(s[a])) + { + b = 0; + a++; + } + if (a == 2) + { + if (is_ok()) + { + tot++; + } + return; + } + if (s[a][b] != '*') + { + dfs(a, b + 1); + } + else + { + for (int i = 0; i < 10; i++) + { + if (i == 0 && b == 0) + { + continue; + } + s[a][b] = '0' + i; + dfs(a, b + 1); + if (tot > 1) + { + s[a][b] = '*'; + return; + } + } + s[a][b] = '*'; + } +} + +void Change(int a, int b, int c) +{ + if (b >= strlen(s[a])) + { + b = 0; + a++; + } + if (c == t) + { + tot = 0; + dfs(0, 0); + if (tot == 1) + { + flag = true; + for (int i = 0; i < 3; i++) + { + memcpy(ans[i], s[i], sizeof(s[i])); + } + } + return; + } + char ch = s[a][b], m; + for (int i = 0; i <= 10; i++) + { + if (i == 1 && b == 0) + { + continue; + } + if (i == 0) + { + m = '*'; + } + else + { + m = '0' + i - 1; + } + if (m == ch) + { + Change(a, b + 1, c); + } + else + { + s[a][b] = m; + Change(a, b + 1, c + 1); + } + if (flag) + { + s[a][b] = ch; + return; + } + s[a][b] = ch; + } +} + +int main() +{ + int cas = 0; + while (scanf("%s", s[0]) == 1 && s[0][0] != '0') + { + scanf("%s%s", s[1], s[2]); + tot = 0; + dfs(0, 0); + if (tot == 1) + { + printf("Case %d: %s %s %s\n", ++cas, s[0], s[1], s[2]); + } + else + { + int sum_len = strlen(s[0]) + strlen(s[1]) + strlen(s[2]); + flag = false; + for (t = 1; t <= sum_len; t++) + { + Change(0, 0, 0); + if (flag) + { + break; + } + } + printf("Case %d: %s %s %s\n", ++cas, ans[0], ans[1], ans[2]); + } + } + return 0; +} diff --git a/12108.cpp b/12108.cpp new file mode 100644 index 0000000..9cf5e84 --- /dev/null +++ b/12108.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +const int MAX = 1000005; +int a[11], b[11], c[11]; + +int main() +{ + int N, cas = 0; + while (scanf("%d", &N), N) + { + int be = 100; + for (int i = 1; i <= N; i++) + { + scanf("%d%d%d", &a[i], &b[i], &c[i]); + } + int ans = 1, cnt; + for (; ans < MAX; ans++) + { + cnt = 0; + for (int i = 1; i <= N; i++) + if (c[i] <= a[i]) + { + cnt++; + } + if (cnt == N) + { + break; + } + for (int i = 1; i <= N; i++) + { + if (c[i] == a[i] + b[i] || (c[i] == a[i] && cnt >= N - cnt)) + { + c[i] = 0; + } + c[i]++; + } + } + if (ans == MAX) + { + ans = -1; + } + printf("Case %d: %d\n", ++cas, ans); + } + return 0; +} diff --git a/12109.cpp b/12109.cpp new file mode 100644 index 0000000..557276a --- /dev/null +++ b/12109.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +double mrandom() +{ + int r = rand(); + return (double)r / RAND_MAX; +} +int main() +{ + int n, a, b, c; + int cases = 0; + int x[32], y[32], z[32]; + while (scanf("%d", &n) == 1 && n) + { + scanf("%d %d %d", &a, &b, &c); + const int runtime = 8000000; + for (int i = 0; i < n; i++) + scanf("%d %d %d", &x[i], &y[i], &z[i]); + int count[32] = {}, guess = runtime / n; + double tx, ty, tz; + for (int i = guess - 1; i >= 0; i--) + { + tx = mrandom() * a, ty = mrandom() * b, tz = mrandom() * c; + double dist = 1e+60, tmp; + int mn = 0; + for (int j = 0; j < n; j++) + { + tmp = (tx - x[j]) * (tx - x[j]) + (ty - y[j]) * (ty - y[j]) + + (tz - z[j]) * (tz - z[j]); + if (tmp < dist) + dist = tmp, mn = j; + } + count[mn]++; + } + printf("Case %d:", ++cases); + for (int i = 0; i < n; i++) + printf(" %.3lf", (double)count[i] / guess); + puts(""); + } + return 0; +} diff --git a/1211.cpp b/1211.cpp new file mode 100644 index 0000000..0d47aa2 --- /dev/null +++ b/1211.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int a[200]; +double dp[200]; + +int main() +{ + int n, i, j; + double r, v, e, f, b; + while (scanf("%d", &n) != EOF && n != 0) + { + a[0] = 0; + for (i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + } + scanf("%lf", &b); + scanf("%lf %lf %lf %lf", &r, &v, &e, &f); + dp[0] = 0; + for (i = 1; i <= n; i++) + { + double mx = 1000000000.0; + for (j = 0; j < i; j++) + { + double ct = 0.0; + int x; + for (x = 0; x < a[i] - a[j]; x++) + if (x < r) + { + ct += (1.0 / (v - f * (r - x))); + } + else + { + ct += (1.0 / (v - e * (x - r))); + } + ct += dp[j]; + if (j != 0 && j != n) + { + ct += b; + } + if (ct < mx) + { + mx = ct; + } + } + dp[i] = mx; + } + printf("%0.4lf\n", dp[n]); + } + return 0; +} diff --git a/12112.cpp b/12112.cpp new file mode 100644 index 0000000..92764d5 --- /dev/null +++ b/12112.cpp @@ -0,0 +1,297 @@ +#include + +using namespace std; + +int n, m; +int id(int a, int b) +{ + return a * m + b; +} + +struct State +{ + int w, step; + string s, op; + State(string s = " ", int w = 0, int step = 0) + : s(s), w(w), step(step) + { + op.clear(); + } + bool operator<(const State &b) const + { + return s < b.s || (s == b.s && w < b.w); + } +}; +set dic; + +int fall(string &s) +{ + for (int i = n - 1; i >= 0; i--) + { + for (int j = 0; j < m; j++) + { + int L = id(i, j); + if (s[L] == '@' || s[L] == 'O') + { + int wh = L + m; + while (wh < m * n && s[wh] == '.') + { + wh += m; + } + wh -= m; + if (L != wh) + { + // wh might == L; swap is a good way to change + swap(s[L], s[wh]); + } + } + else if (s[L] == '[') + { + for (int k = j + 1; k < m; k++) + { + int R = id(i, k); + if (s[R] == 'X') + { + break; + } + if (s[R] == ']')// from (i,j) to (i,k) + { + int to = 0; + for (int l = i + 1; l < n; l++)// check if (l,j) to (l,k) is empty + { + bool ok = true; + for (int ii = j; ii <= k; ii++) + { + if (s[id(l, ii)] != '.') + { + ok = false; + break; + } + } + if (!ok) + { + to = l - 1; + break; + } + } + if (to != i) + { + for (int l = j; l <= k; l++) + { + swap(s[id(i, l)], s[id(to, l)]); + } + } + j = k; + break; + } + } + } + } + } + for (int i = 0; i < s.size(); i++) + { + if (s[i] == '@') + { + return i; + } + } + return -1; +} + +void lnk_right(char &c) +{ + if (c == ']') + { + c = '='; + } + else if (c == 'O') + { + c = '['; + } +} + +void lnk_left(char &c) +{ + if (c == '[') + { + c = '='; + } + else if (c == 'O') + { + c = ']'; + } +} + +bool solve_magic(State &s, int x, int y) +{ + int the = id(x, y); + if (s.s[the] == 'X') + { + return false; + } + if (s.s[the] == '.') + { + if (s.s[the - 1] != '.' && s.s[the + 1] != '.') + { + s.s[the] = '=', lnk_right(s.s[the - 1]), lnk_left(s.s[the + 1]); + } + else if (s.s[the - 1] != '.') + { + s.s[the] = ']', lnk_right(s.s[the - 1]); + } + else if (s.s[the + 1] != '.') + { + s.s[the] = '[', lnk_left(s.s[the + 1]); + } + else + { + s.s[the] = 'O'; + } + } + else + { + s.s[the] = '.'; + if (s.s[the - 1] == '=') + { + s.s[the - 1] = ']'; + } + if (s.s[the - 1] == '[') + { + s.s[the - 1] = 'O'; + } + if (s.s[the + 1] == '=') + { + s.s[the + 1] = '['; + } + if (s.s[the + 1] == ']') + { + s.s[the + 1] = 'O'; + } + } + s.w = fall(s.s); + return true; +} + +bool solve_mv(State &s, int k) +{ + int the = s.w + k; + if (s.s[the] == '.') + { + swap(s.s[s.w], s.s[the]); + } + else if (s.s[the] == 'O' && s.s[the + k] == '.') + { + int wh = the; + while (s.s[wh + k] == '.') + { + wh += k; + if (s.s[wh + m] == '.') + { + break; + } + } + swap(s.s[the], s.s[wh]); + } + else if (s.s[the - m] == '.' && s.s[s.w - m] == '.') + { + swap(s.s[s.w], s.s[the - m]); + } + else + { + return false; + } + s.w = fall(s.s); + return true; +} + +int main() +{ + int t = 0; + while (scanf("%d%d", &n,&m) == 2, n) + { + dic.clear(); + string s; + char line[m]; + for (int i = 1; i <= n; i++) + { + scanf("%s", &line); + s += line; + } + int S = 0, T = 0, tx, ty; + for (int i = 0; i < s.size(); i++) + { + if (s[i] == '@') + { + S = i; + } + else if (s[i] == '#') + { + T = i, s[i] = '.'; + } + } + tx = T / m; + ty = T % m; + queue q; + q.push(State(s, S, 0)); + printf("Case %d: ", ++t); + while (!q.empty()) + { + State now = q.front(), ne; + q.pop(); + if (now.w == T) + { + printf("%s", now.op.c_str()); + break; + } + int x = now.w / m, y = now.w % m; + if (abs(y - ty) + now.step > 15) + { + continue; + } + ne = now; + if (solve_magic(ne, x + 1, y + 1)) + { + ne.op.push_back('>'); + ne.step++; + if (dic.count(ne) == 0) + { + dic.insert(ne); + q.push(ne); + } + } + ne = now; + if (solve_magic(ne, x + 1, y - 1)) + { + ne.op.push_back('<'); + ne.step++; + if (dic.count(ne) == 0) + { + dic.insert(ne); + q.push(ne); + } + } + ne = now; + if (solve_mv(ne, -1)) + { + ne.op.push_back('L'); + if (dic.count(ne) == 0) + { + dic.insert(ne); + q.push(ne); + } + } + ne = now; + if (solve_mv(ne, +1)) + { + ne.op.push_back('R'); + if (dic.count(ne) == 0) + { + dic.insert(ne); + q.push(ne); + } + } + } + printf("\n"); + } + return 0; +} diff --git a/12113.cpp b/12113.cpp new file mode 100644 index 0000000..61b31d0 --- /dev/null +++ b/12113.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +char g[16][16], h[7][16][16]; +const char pattern[3][16] = {" _ _ ", "| |", "|_ _|"}; +int ret; +void place(char h[][16], int x, int y) +{ + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 5; j++) + { + if (i == 0 && (j == 0 || j == 2 || j == 4)) + continue; + h[x + i][y + j] = pattern[i][j]; + } + } +} +int check(char h[][16]) +{ + for (int i = 0; i < 5; i++) + for (int j = 0; j < 9; j++) + if (g[i][j] != h[i][j]) + return 0; + return 1; +} +void dfs(int idx) +{ + if (check(h[idx])) + { + ret = 1; + return; + } + if (idx == 6) + return; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 5; j += 2) + { + memcpy(h[idx + 1], h[idx], sizeof(h[idx])); + place(h[idx + 1], i, j); + dfs(idx + 1); + if (ret) + return; + } + } +} +int main() +{ + int cases = 0; + while (true) + { + for (int i = 0; i < 5; i++) + { + gets(g[i]); + if (g[i][0] == '0') + return 0; + } + memset(h, ' ', sizeof(h)); + ret = 0; + dfs(0); + printf("Case %d: %s\n", ++cases, ret ? "Yes" : "No"); + } + return 0; +} diff --git a/12114.cpp b/12114.cpp index f52c372..897b068 100644 --- a/12114.cpp +++ b/12114.cpp @@ -1,23 +1,42 @@ -#include -#include -#include -using namespace std; +#include -const double EPS = 1e-10; +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++) { - int B, S; - scanf("%d%d", &B, &S); - if (!B && !S) break; - - printf("Case %d: ", t); - - if (B <= 1) printf(":-\\\n"); - else if (B <= S) printf(":-|\n"); - else printf(":-(\n"); +int main() +{ + int counter = 0; + long long b, s; + while (cin >> b >> s) + { + if (b == 0 && s == 0) + { + break; + } + counter++; + cout << "Case " << counter << ": "; + if (b == 1) + { + cout << ":-\\" << endl; + continue; + } + if (b == s) + { + cout << ":-|" << endl; + continue; + } + if (s > b) + { + cout << ":-|" << endl; + continue; + } + if (s * (b - 1) > b * (s - 1)) + { + cout << ":-(" << endl; + } + else + { + cout << ":-)" << endl; + } } return 0; } diff --git a/12115.cpp b/12115.cpp new file mode 100644 index 0000000..6db5692 --- /dev/null +++ b/12115.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +char s[50005]; +int len, x, y, all; + +void get(int i, int j); + +int main() +{ + int T, t, i, j, Q, q, a, b, c, d, which; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + gets(s); + scanf(" %s %d", s, &Q); + len = strlen(s); + all = 2 * len - 1; + printf("Square %d:\n", t + 1); + for (q = 0; q < Q; q++) + { + printf("Query %d:\n", q + 1); + scanf(" %d %d %d %d", &a, &b, &c, &d); + for (i = a; i <= c; i++) + { + for (j = b; j <= d; j++) + { + get(i, j); + which = min(x, y) - 1; + printf("%c", s[which]); + } + printf("\n"); + } + } + printf("\n"); + } + return 0; +} + +void get(int i, int j) +{ + if (i > len) + { + x = all - i + 1; + } + else + { + x = i; + } + if (j > len) + { + y = all - j + 1; + } + else + { + y = j; + } +} diff --git a/12116.cpp b/12116.cpp new file mode 100644 index 0000000..7b2182f --- /dev/null +++ b/12116.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +#define MAXN 47100 + +typedef long long LL; + +bool prime[MAXN + 10]; +int prm[MAXN + 10], fat[MAXN + 10], arr[MAXN + 10], sz, k, T; +LL dp[MAXN + 10], n; +map m; + +void pre() +{ + memset(prime, true, sizeof(prime)); + prime[0] = prime[1] = false; + for (int i = 4; i < MAXN; i += 2) + { + fat[i] = 2; + prime[i] = false; + } + fat[2] = 2; + sz = 0; + prm[sz++] = 2; + for (int i = 3; i < MAXN; i += 2) + { + if (prime[i]) + { + prm[sz++] = i; + fat[i] = i; + for (LL j = (LL)i * i; j < MAXN; j += i) + { + fat[j] = i; + prime[j] = false; + } + } + } + int s; + LL res; + for (int i = 2; i < MAXN; i++) + { + s = sqrt(i); + res = 1; + if (s * s == i) + { + res += dp[s]; + } + else + { + s++; + } + for (int j = 2; j < s; j++) + { + if (i % j == 0) + { + res += dp[j] + dp[i / j]; + } + } + dp[i] = res; + } +} + +LL rec(int x) +{ + if (x < MAXN) + { + return dp[x]; + } + else if (m.count(x) > 0) + { + return m[x]; + } + int s = sqrt(x); + LL res = 1LL; + int stor = -1; + for (int i = 0; i < k; i++) + { + if (arr[i] > s) + { + break; + } + if (x % arr[i] == 0) + { + res += dp[arr[i]]; + if (arr[i] != x / arr[i]) + { + res += rec(x / arr[i]); + } + } + } + return m[x] = res; +} + +int main() +{ + int t = 1; + pre(); + scanf("%d", &T); + m.clear(); + while (T--) + { + scanf("%lld", &n); + n++; + if (n < MAXN) + { + printf("Case %d: %lld\n", t++, dp[n]); + } + else + { + int s = sqrt(n); + LL res = 1; + int stor = -1; + if (s * s == n) + { + res += dp[s]; + stor = s; + } + else + { + s++; + } + k = 0; + for (int i = 2; i < s; i++) + { + if (n % i == 0) + { + res += dp[i]; + arr[k++] = i; + arr[k++] = n / i; + } + } + if (~stor) + { + arr[k++] = stor; + } + sort(arr, arr + k); + for (int i = 2; i < s; i++) + { + if (n % i == 0) + { + res += rec(n / i); + } + } + printf("Case %d: %lld\n", t++, res); + } + } +} diff --git a/12117.cpp b/12117.cpp new file mode 100644 index 0000000..4049a65 --- /dev/null +++ b/12117.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +long long dp[2005][8]; + +int main() +{ + memset(dp, 0, sizeof(dp)); + int i, j, N, cnt = 1, n; + dp[1][0] = 1; + dp[2][0] = 1; + dp[2][1] = 1; + dp[2][2] = 1; + dp[2][3] = 1; + dp[2][4] = 1; + dp[2][5] = 1; + dp[2][6] = 1; + for (n = 3; n < 2001; n++) + { + dp[n][0] = (dp[n - 1][0] + dp[n - 1][1] + dp[n - 1][2] + dp[n - 1][3] + dp[n - 1][4] + dp[n - 1][5] + dp[n - 1][6]) % 1000000000000; + dp[n][1] = (dp[n - 2][0] + dp[n - 2][4]) % 1000000000000; + dp[n][2] = (dp[n - 2][0] + dp[n - 2][5]) % 1000000000000; + dp[n][3] = dp[n - 2][0]; + dp[n][4] = (dp[n - 1][2] + dp[n - 2][0]) % 1000000000000; + if (n - 3 > 0) + { + dp[n][4] = (dp[n][4] + dp[n - 3][2]) % 1000000000000; + } + dp[n][5] = (dp[n - 1][1] + dp[n - 2][0]) % 1000000000000; + if (n - 3 > 0) + { + dp[n][5] = (dp[n][5] + dp[n - 3][1]) % 1000000000000; + } + dp[n][6] = (dp[n - 2][0] + dp[n - 2][3]) % 1000000000000; + } + while (scanf(" %d", &N) == 1 && N) + { + printf("Case %d: %lld\n", cnt++, dp[N][0]); + } + return 0; +} diff --git a/12118.cpp b/12118.cpp new file mode 100644 index 0000000..0d3c7dd --- /dev/null +++ b/12118.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define REP(i,s,t) for (int i = (s); i < (t); i++) +#define FOREACH(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++) + +typedef long long ll; +typedef vector VI; + +bool vis[1000]; +int N, M, T, numodd; +VI adj[1000]; + +void visit(int i) +{ + if (vis[i]) + { + return; + } + vis[i] = true; + numodd += adj[i].size() % 2; + FOREACH(adj[i],it) + { + visit(*it); + } +} + +int main() +{ + for (int test = 1;; test++) + { + scanf("%d%d%d", &N, &M, &T); + if (N == 0) + break; + REP(i, 0, N) + { + adj[i].clear(); + vis[i] = false; + } + REP(m, 0, M) + { + int a, b; + scanf("%d%d", &a, &b); + a--; + b--; + adj[a].push_back(b); + adj[b].push_back(a); + } + int res = 0; + REP(i, 0, N) + { + if (!vis[i] && adj[i].size()) + { + numodd = 0; + visit(i); + res += max(numodd, 2); + } + } + if (res > 0) + { + res = (res - 2) / 2 + M; + } + printf("Case %d: %d\n", test, res * T); + } + return 0; +} diff --git a/12119.cpp b/12119.cpp new file mode 100644 index 0000000..057dc94 --- /dev/null +++ b/12119.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +#pragma comment(linker, "/STACK:36777216") +#define lowbit(x) (x & (-x)) +#define lc(x) (x << 1) +#define rc(x) (lc(x) + 1) +#define pi (acos(-1)) +#define eps 1e-11 +#define maxn 1000005 +#define maxm 50005 +#define ll long long +#define ull unsigned long long +#define inf 0x7fffffffffffffffll +#define pb push_back +#define mp make_pair + +vector _div; +vector> _div2; +int cnt, tot, sz, ans, p_cnt; +ll prime[10005][2], dcp[100005][105], tt[100005], tmp[100005], pr[maxn]; +bool is_prime[maxn]; + +ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); } +ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } +/* +ll mul_mod(ll x,ll y,ll k){ + x%=k,y%=k; + ll res=0; + while(y){ + if(y&1){ + res+=x; + if(res>=k)res-=k; + } + x+=x,y>>=1; + if(x>=k)x-=k; + } + return res; +} +ll pow_mod(ll x,ll y,ll k){ + ll res=1; + while(y){ + if(y&1)res=mul_mod(res,x,k); + x=mul_mod(x,x,k),y>>=1; + } + return res; +} +ll witness(ll a,ll b,ll c) { + if(b==0)return 1; + ll x,y,t=0; + while((b&1)==0) + b>>=1,t++; + y=x=pow_mod(a,b,c); + while(t--){ + y=mul_mod(x,x,c); + if(y==1 && x!=1 && x!=c-1) + return false; + x=y; + } + return y==1; +} +bool miller_rabin(ll n) { + if(n==2)return true; + if(n<2 || (n&1)==0)return false; + for(int i=0;i<3;i++) + if(witness(rand()%(n-2)+2,n-1,n)!=1) + return false; + return true; +} + +ll pollard_rho(ll n,ll c){ + if(n%2==0)return 2; + ll i=1,k=2,x,y,d; + while(1){ + i++; + x=(mul_mod(x,x,n)+c)%n; + d=gcd(y-x,n); + if(d==n)return n; + if(d!=n && d>1)return d; + if(i==k) y=x,k<<=1; + } +} +void calc(ll n,ll c){ + if(n==1)return; + if(miller_rabin(n)){ + _div.pb(n); + return; + } + ll k=n; + while(k==n)k=pollard_rho(n,c--); + calc(k,c),calc(n/k,c); +} +*/ +void prime_table() +{ + memset(is_prime, 1, sizeof(is_prime)); + for (int i = 2; i < maxn; i++) + { + if (is_prime[i]) + pr[++p_cnt] = i; + for (int j = 1; j <= p_cnt && i * pr[j] < maxn; j++) + { + is_prime[i * pr[j]] = false; + if (i % pr[j] == 0) + break; + } + } +} +void go_prime(ll x) +{ + for (int i = 1; i <= p_cnt && pr[i] <= x; i++) + if (x % pr[i] == 0) + { + prime[++cnt][0] = pr[i]; + ll kk = x; + while (kk % pr[i] == 0) + prime[cnt][1]++, kk /= pr[i]; + } +} + +void dfs_find_div(ll now, int pos) +{ + if (now > 1000000) + return; + ll res = 1; + if (pos == cnt + 1) + { + //tot++; + //memcpy(dcp[tot],tt,sizeof(dcp[tot])); + //_div2.pb(mp(now,tot)); + tmp[++sz] = now; + return; + } + for (int i = 0; i <= prime[pos][1]; i++) + { + tt[pos] = i; + dfs_find_div(now * res, pos + 1); + res *= prime[pos][0]; + } +} + +void go(ll _1, ll _2, ll n) +{ + //cout<<_1<<' '<<_2<<' '<= l && tmp[i] <= r && lcm(_1, lcm(_2, tmp[i])) == n) + printf("%lld %lld %lld\n", _1, _2, tmp[i]), ans++; +} + +void init() +{ + //_div.clear(),_div2.clear(); + memset(prime, 0, sizeof(prime)); + //memset(dcp,0,sizeof(dcp)); + //memset(tt,0,sizeof(tt)); + memset(tmp, 0, sizeof(tmp)); + tot = cnt = sz = ans = 0; +} + +ll n; +int cas; + +int main() +{ + prime_table(); + while (scanf("%lld", &n) == 1) + { + if (!n) + break; + init(); + go_prime(n); + /*for(int i=1;i<=cnt;i++) + cout< + +using namespace std; + +#define clear(A, X) memset(A, X, sizeof A) +#define copy(A, B) memcpy(A, B, sizeof A) + +const int maxE = 3000000; +const int maxN = 100000; +const int maxQ = 3000000; +const int oo = 0x3f3f3f3f; + +struct Edge +{ + int v, n, c; +} edge[maxE]; + +int adj[maxN], cntE; +int Q[maxQ], head, tail; +int d[maxN], cur[maxN], pre[maxN], num[maxN]; +int s, t, nv, n, m; +int a[maxN]; +char str[maxN]; + +void addedge(int u, int v, int c) +{ + edge[cntE].v = v; + edge[cntE].c = c; + edge[cntE].n = adj[u]; + adj[u] = cntE++; + edge[cntE].v = u; + edge[cntE].c = 0; + edge[cntE].n = adj[v]; + adj[v] = cntE++; +} +void rev_bfs() +{ + clear(num, 0); + clear(d, -1); + d[t] = 0; + num[0] = 1; + head = tail = 0; + Q[tail++] = t; + while (head != tail) + { + int u = Q[head++]; + for (int i = adj[u]; ~i; i = edge[i].n) + { + int v = edge[i].v; + if (~d[v]) + { + continue; + } + d[v] = d[u] + 1; + Q[tail++] = v; + num[d[v]]++; + } + } +} +int ISAP() +{ + copy(cur, adj); + rev_bfs(); + int flow = 0, u = pre[s] = s, i; + while (d[s] < nv) + { + if (u == t) + { + int f = oo, neck; + for (i = s; i != t; i = edge[cur[i]].v) + { + if (f > edge[cur[i]].c) + { + f = edge[cur[i]].c; + neck = i; + } + } + for (i = s; i != t; i = edge[cur[i]].v) + { + edge[cur[i]].c -= f; + edge[cur[i] ^ 1].c += f; + } + flow += f; + u = neck; + } + for (i = cur[u]; ~i; i = edge[i].n) + if (d[edge[i].v] + 1 == d[u] && edge[i].c) + { + break; + } + if (~i) + { + cur[u] = i; + pre[edge[i].v] = u; + u = edge[i].v; + } + else + { + if (0 == (--num[d[u]])) + { + break; + } + int mind = nv; + for (i = adj[u]; ~i; i = edge[i].n) + { + if (edge[i].c && mind > d[edge[i].v]) + { + cur[u] = i; + mind = d[edge[i].v]; + } + } + d[u] = mind + 1; + num[d[u]]++; + u = pre[u]; + } + } + return flow; +} +void init() +{ + clear(adj, -1); + cntE = 0; + s = maxN - 4; + t = maxN - 3; + nv = maxN - 2; +} +void work() +{ + int w, x, pos = 30000, sum = 0; + init(); + scanf("%d", &n); + for (int i = 1; i <= n; ++i) + { + scanf("%d", &w); + addedge(s, i + pos, w); + sum += w; + fgets(str, maxN, stdin); + x = 0; + for (int j = 0; str[j]; ++j) + { + if (str[j] < '0' || str[j] > '9') + { + if (x) + { + addedge(i + pos, x, oo); + } + x = 0; + } + else + { + x = x * 10 + str[j] - '0'; + } + } + } + scanf("%d", &m); + for (int i = 1; i <= m; ++i) + { + scanf("%d", &w); + addedge(i + pos + n, t, w); + sum += w; + fgets(str, maxN, stdin); + x = 0; + for (int j = 0; str[j]; ++j) + { + if (str[j] < '0' || str[j] > '9') + { + if (x) + { + addedge(x, i + pos + n, oo); + } + x = 0; + } + else + { + x = x * 10 + str[j] - '0'; + } + } + } + printf("%d\n", sum - ISAP()); +} +int main() +{ + int cas, t; + for (scanf("%d", &t), cas = 1; cas <= t; ++cas) + { + if (cas > 1) + { + printf("\n"); + } + printf("Case %d:\n", cas); + work(); + } + return 0; +} diff --git a/12120.cpp b/12120.cpp new file mode 100644 index 0000000..b0b16d2 --- /dev/null +++ b/12120.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) + +typedef long long ll; + +int N, M, T; +int d[100][100]; +int ti[100]; +bool vis[2][101][100];// [direction][length][pos] + +int main() +{ + for (int test = 1;; test++) + { + scanf("%d%d", &N, &M); + if (N == 0) + break; + REP(i, 0, N) + REP(j, 0, N) + d[i][j] = -1; + REP(m, 0, M) + { + int a, b, c; + scanf("%d%d%d", &a, &b, &c); + d[a][b] = d[b][a] = c; + } + scanf("%d", &T); + REP(t, 0, T) + scanf("%d", ti + t); + REP(dir, 0, 2) + { + REP(i, 0, N) + vis[dir][0][i] = false; + vis[dir][0][dir ? N - 1 : 0] = true; + REP(l, 1, T + 1) + { + REP(i, 0, N) + { + vis[dir][l][i] = false; + REP(j, 0, N) + if (d[i][j] == ti[l - 1] && vis[dir][l - 1][j]) + vis[dir][l][i] = true; + } + } + reverse(ti, ti + T); + } + int res = 0; + REP(i, 0, N) + { + bool ok = false; + REP(l, 0, T + 1) + if (vis[0][l][i] && vis[1][T - l][i]) + ok = true; + res += ok; + } + printf("Tour %d: %d\n", test, res); + } + return 0; +} diff --git a/12121.cpp b/12121.cpp new file mode 100644 index 0000000..1e3228a --- /dev/null +++ b/12121.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +typedef long long ll; +#define EPS 1e-8 +const double PI = 4.0 * atan(1.0); + +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) + +struct point +{ + double x, y; +}; + +inline bool comp_x(point a, point b) +{ + return a.x < b.x; +} + +inline bool comp_y(point a, point b) +{ + return a.y < b.y; +} + +inline double dist(point a, point b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +double closest_pair(point pt[], int n) +{ + int m = n / 2; + + if (n == 1) + return 1e99; + if (n == 2) + return dist(*pt, *(pt + 1)); + + double d1 = closest_pair(pt, m); + double d2 = closest_pair(pt + m, n - m); + double d = min(d1, d2); + + vector qt; + qt.clear(); + for (int i = 0; i < n; i++) + { + if (fabs(pt[i].x - pt[m].x) < d + EPS) + qt.push_back(pt[i]); + } + sort(qt.begin(), qt.end(), comp_y); + for (int i = 0; i < qt.size(); i++) + { + for (int j = 1; j < 6; j++) + { + if (i + j < qt.size()) + d = min(d, dist(qt[i], qt[i + j])); + } + } + return d; +} + +point pt[15005]; + +int main() +{ + int n; + double e, theta; + + cin >> n >> e >> theta; + + int cs = 0; + while (n) + { + memset(pt, 0, sizeof(pt)); + cout << "Case " << ++cs << ":\n"; + double x, y; + + double th = double(theta) * PI / 180; + double k = sqrt(1 - e * e);// k = b/a <1 + + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x, &y); + pt[i].x = (x * cos(th) + y * sin(th)) * k; + pt[i].y = -x * sin(th) + y * cos(th); + } + + sort(pt, pt + n, comp_x); + + double d = closest_pair(pt, n) / 2; + + if (e == 1) + printf("%f\n", 0); + else + printf("%f\n", PI * d * d / k); + + cin >> n >> e >> theta; + } +} diff --git a/12122.cpp b/12122.cpp new file mode 100644 index 0000000..6c1713a --- /dev/null +++ b/12122.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) + +typedef long long ll; + +const int MAXZ = 2100; + +int V[300]; +int M[300][300]; +ll R[MAXZ][300]; +ll Rc[MAXZ][300]; + +const int MOD = 1000000007; + +ll powmod(ll a, int b) +{ + ll r = 1, e = a; + for (int i = 0; (1 << i) <= b; i++) + { + if (b & (1 << i)) + r = r * e % MOD; + e = e * e % MOD; + } + return r; +} + +const int Z = MAXZ; +ll pol[MAXZ]; +ll smallinvs[2 * MAXZ + 10]; +ll hmult[MAXZ]; + +ll evpol(ll x) +{ + x %= MOD; + if (x < Z) + return pol[x]; + ll pr = 1; + REP(i, 0, Z) + pr = pr * (x - i) % MOD; + ll res = 0; + REP(j, 0, Z) + { + ll h = pol[j]; + h = h * hmult[j] % MOD; + h = h * pr % MOD; + h = h * powmod(x - j, MOD - 2) % MOD; + res = (res + h) % MOD; + } + return res; +} + +ll powt[MAXZ + 5][10]; + +int main() +{ + int T; + scanf("%d", &T); + REP(i, 0, 2 * MAXZ + 10) + smallinvs[i] = powmod(i - MAXZ - 5, MOD - 2); + REP(i, 0, MAXZ + 5) + REP(k, 0, 10) + powt[i][k] = powmod(i, k); + for (; T; T--) + { + int N; + scanf("%d", &N); + REP(i, 0, N) + { + scanf("%d", V + i); + REP(j, 0, i) + scanf("%d", &M[i][j]); + } + REP(i, 0, Z) + { + Rc[i][0] = 0; + REP(j, 0, N) + { + if (i == 0) + R[i][j] = V[j]; + else + { + R[i][j] = R[i - 1][j]; + REP(k, 0, j) + R[i][j] = (R[i][j] + powt[i + 1][M[j][k]] * R[i][k]) % MOD; + } + Rc[i][j + 1] = (Rc[i][j] + R[i][j]) % MOD; + } + } + REP(j, 0, Z) + { + hmult[j] = 1; + REP(i, 0, Z) + if (i != j) + hmult[j] = (ll)hmult[j] * smallinvs[j - i + MAXZ + 5] % MOD; + } + int Q; + scanf("%d", &Q); + REP(qu, 0, Q) + { + int p, b, d; + ll a, c; + scanf("%d%lld%d%lld%d", &p, &a, &b, &c, &d); + a--; + b--; + a %= MOD; + c %= MOD; + pol[0] = 0; + REP(i, 0, Z - 1) + pol[i + 1] = (pol[i] + powmod(i - a + 1, p) * (Rc[i][b + d + 1] - Rc[i][b])) % MOD; + ll res = ((evpol(a + c + 1) - evpol(a)) % MOD + MOD) % MOD; + printf("%d\n", (int)res); + } + printf("\n"); + } + return 0; +} diff --git a/12123.cpp b/12123.cpp new file mode 100644 index 0000000..8518d31 --- /dev/null +++ b/12123.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +typedef struct List +{ + int x, y; +} LIST; +LIST lst[1205], tmp[1205]; + +int n; + +bool cmp(const LIST &p1, const LIST &p2); +int cro(LIST p, LIST q, LIST o); +int inn(LIST p, LIST q, LIST o); + +int main() +{ + int N, i, j, c, all, cnt = 1; + while (scanf(" %d", &N) == 1 && N) + { + for (n = c = 0; n < N; n++) + { + scanf(" %d %d", &lst[n].x, &lst[n].y); + } + all = N * (N - 1) * (N - 2) / 6; + if (N < 3) + { + printf("Scenario %d:\n", cnt++); + printf("There are 0 sites for making valid tracks\n"); + continue; + } + for (n = 0; n < N; n++) + { + j = 0; + for (i = 0; i < N; i++) + { + if (i == n) + { + continue; + } + tmp[j].x = lst[i].x - lst[n].x; + tmp[j].y = lst[i].y - lst[n].y; + j++; + } + sort(tmp, tmp + N - 1, cmp); + int st = 0, p = 1, q = 1; + for (st = 0; st < N - 1; st++) + { + int t = 0, k = 0; + if (p == st) + { + p = (p + 1) % (N - 1); + } + if (q == st) + { + q = (q + 1) % (N - 1); + } + while (cro(tmp[st], tmp[p], lst[n]) >= 0 && inn(tmp[st], tmp[p], lst[n]) > 0) + { + p = (p + 1) % (N - 1); + if (p == st) + { + break; + } + t = 1; + k++; + } + t = 0; + while (cro(tmp[st], tmp[q], lst[n]) >= 0) + { + q = (q + 1) % (N - 1); + if (q == st) + { + break; + } + t = 1; + k++; + } + if (q == st || p == st) + { + if (p != st && cro(tmp[st], tmp[p], lst[n]) >= 0 && inn(tmp[st], tmp[p], lst[n]) <= 0) + { + if (q >= p) + { + c += (q - p); + } + else + { + c += (q + N - 1 - p); + } + } + continue; + } + if (cro(tmp[st], tmp[p], lst[n]) >= 0 && inn(tmp[st], tmp[p], lst[n]) <= 0 && cro(tmp[st], tmp[q], lst[n]) < 0) + { + if (q >= p) + { + c += (q - p); + } + else + { + c += (q + N - p - 1); + } + } + } + } + printf("Scenario %d:\n", cnt++); + printf("There are %d sites for making valid tracks\n", all - c); + } + return 0; +} + +bool cmp(const LIST &p1, const LIST &p2) +{ + if (p1.y > 0 && p2.y > 0) + { + return p2.x * p1.y < p2.y * p1.x; + } + else if (p1.y < 0 && p2.y < 0) + { + return p2.x * p1.y < p2.y * p1.x; + } + else if (p1.y == 0) + if (p1.x > 0) + { + return true; + } + else + { + return p2.y < 0; + } + else if (p2.y == 0) + if (p2.x > 0) + { + return false; + } + else + { + return p1.y > 0; + } + else + { + return p1.y > 0; + } +} +int cro(LIST p, LIST q, LIST o) +{ + int cross = (p.x) * (q.y) - (q.x) * (p.y); + return cross; +} +int inn(LIST p, LIST q, LIST o) +{ + int inner = (p.x) * (q.x) + (q.y) * (p.y); + return inner; +} diff --git a/12124.cpp b/12124.cpp new file mode 100644 index 0000000..7e07b97 --- /dev/null +++ b/12124.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +struct part +{ + int cost, quality; + part(int _c = 0, int _q = 0) : cost(_c), quality(_q) {} + bool operator<(part p) const + { + return cost < p.cost || (cost == p.cost && quality < p.quality); + } +}; +struct type +{ + vector option; + int ui; +} t[1024]; +map m; +int types; +char tname[1024][32]; +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int budget, n; + int c, q; + m.clear(); + types = 1; + scanf("%d %d", &n, &budget); + for (int i = 0; i < n; ++i) + { + scanf("%s %*s %d %d", tname[types], &c, &q); + int &ptype = m[tname[types]]; + if (!ptype) + { + ptype = types++; + t[ptype].option.clear(); + t[ptype].ui = 0; + } + t[ptype].option.push_back(part(c, q)); + } + for (int i = 1; i < types; ++i) + { + sort(t[i].option.begin(), t[i].option.end()); + budget -= t[i].option[0].cost; + } + int minq = 1 << 30; + for (bool upgraded = true; upgraded;) + { + upgraded = true; + minq = 1 << 30; + for (int i = 1; i < types; ++i) + { + minq = min(t[i].option[t[i].ui].quality, minq); + } + for (int i = 1; upgraded && i < types; ++i) + if (t[i].option[t[i].ui].quality == minq) + { + int j, sz = t[i].option.size(); + budget += t[i].option[t[i].ui].cost; + for (j = t[i].ui + 1; j < sz; ++j) + if (t[i].option[j].quality > minq) + { + break; + } + if (j < sz && budget >= t[i].option[j].cost) + { + t[i].ui = j; + } + else + { + upgraded = false; + } + budget -= t[i].option[t[i].ui].cost; + } + } + printf("%d\n", minq); + } + return 0; +} diff --git a/12125.cpp b/12125.cpp new file mode 100644 index 0000000..8bd399a --- /dev/null +++ b/12125.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const int INF = 0x3FFFFFFF, NN = 205; + +int cap[NN][NN], flow[NN][NN], p[NN], q[NN], x[NN], y[NN]; +int n; + +struct edge +{ + int d, w; + edge(int _d = 0, int _w = 0) : d(_d), w(_w) {} +}; + +void show_path(int x) +{ + if (x == -1) + { + return; + } + show_path(p[x]); + printf("%d -> ", x); +} +int max_flow(int s, int t) +{ + int at, capacity, next, r = 0; + FOR(i, n) + FOR(j, n) flow[i][j] = 0; + while (true) + { + FOR(i, n) + p[i] = -1; + int head(0), tail; + int begin, end; + q[(tail = 0)++] = s; + while (head != tail) + { + at = q[head++]; + if (!at || at > n >> 1) + { + begin = 1, end = (n >> 1) + 1; + } + else + { + begin = (n >> 1) + 1, end = n; + } + for (next = begin; next < end; ++next) + if (p[next] == -1 && cap[at][next] - flow[at][next] > 0) + { + q[tail++] = next; + p[next] = at; + if (next == t) + { + break; + } + } + if (next == t) + { + break; + } + } + if (head == tail) + { + break; + } + for (capacity = INF, at = t; p[at] != -1 && at != s; at = p[at]) + { + capacity = min(capacity, cap[p[at]][at] - flow[p[at]][at]); + } + for (at = t; at != s; at = p[at]) + { + flow[p[at]][at] += capacity; + flow[at][p[at]] -= capacity; + } + r += capacity; + } + return r; +} + +int sqr(int x) +{ + return x * x; +} + +int main() +{ + int T; + scanf("%d", &T); + for (double D; T-- && scanf("%d %lf", &n, &D) == 2;) + { + int f(n), total_penguins(0); + n = 2 * n + 1; + FOR(i, n) + FOR(j, n) cap[i][j] = 0; + for (int i = 1; i <= f; ++i) + { + int penguins, res; + scanf("%d %d %d %d", &x[i], &y[i], &penguins, &res); + total_penguins += penguins; + // source to floater + cap[0][i] = penguins; + // node i in to node i out + cap[i][i + f] = res; + for (int j = 1; j < i; ++j) + { + int dist = sqr(x[j] - x[i]) + sqr(y[j] - y[i]); + if (sqrt(dist) < D + 1e-9) + { + // i out to j in + cap[i + f][j] = INF; + // j out to i in + cap[j + f][i] = INF; + } + } + } + int list[f], top = 0; + for (int i = 1; i <= f; ++i) + if (max_flow(0, i) == total_penguins) + { + list[top++] = i - 1; + } + for (int i = 0; i < top; ++i) + { + printf("%d%c", list[i], i == top - 1 ? '\n' : ' '); + } + if (!top) + { + puts("-1"); + } + } + return 0; +} diff --git a/12126.cpp b/12126.cpp new file mode 100644 index 0000000..c53c6ec --- /dev/null +++ b/12126.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define LEN(x) (10 * x + 2) +#define WID(x) (44 * x + 4) +long long L, W, area; +void test(long long a, long long b) +{ + a = LEN(a), b = WID(b); + if (a * b < area) + { + L = a; + W = b; + area = a * b; + } + else if (a * b == area && abs(a - b) < abs(L - W)) + { + L = a; + W = b; + } +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + long long stacks; + scanf("%lld", &stacks); + stacks = (stacks + 4) / 5; + L = W = -1; + area = 1LL << 60; + for (long long i = 1; i * i <= stacks; ++i) + { + long long tL = (stacks + i - 1) / i, tW = i; + test(tL, tW); + test(tW, tL); + } + if (L < W) + { + swap(L, W); + } + printf("%lld X %lld = %lld\n", L, W, L * W); + } + return 0; +} diff --git a/12128.cpp b/12128.cpp new file mode 100644 index 0000000..fd8a343 --- /dev/null +++ b/12128.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + + + + +int grid[1010][1010], rep[1010][1010]; +int p[] = {-1, 0, 1, 0}; +int q[] = {0, 1, 0, -1}; +int T, N, X, Y, x, y, xi, yi, xf, yf; + +struct abc +{ + int x, y, min; + + abc() {} + abc(int x, int y, int min = 0) : x(x), y(y), min(min) {} + + bool operator<(const abc &lhs) const + { + return min < lhs.min; + } +} aux; + +int getId(int x, int y) +{ + return y * X + x; +} + +pair getPos(int id) +{ + return make_pair(id % X, id / X); +} + +bool isInside(int x, int y) +{ + return (y >= 0 && y < Y && x >= 0 && x < X); +} + +int find_set(int x, int y) +{ + if (rep[y][x] != getId(x, y)) + rep[y][x] = find_set(getPos(rep[y][x]).first, getPos(rep[y][x]).second); + return rep[y][x]; +} + +void link(int xA, int yA, int xB, int yB) +{ + int idRepA = find_set(xA, yA); + int idRepB = find_set(xB, yB); + if (idRepA != idRepB) + rep[getPos(idRepA).second][getPos(idRepA).first] = rep[getPos(idRepB).second][getPos(idRepB).first]; +} + +bool isConnected(int xA, int yA, int xB, int yB) +{ + if (rep[yA][xA] == -1 || rep[yB][xB] == -1) + return false; + int idRepA = find_set(xA, yA); + int idRepB = find_set(xB, yB); + + return (idRepA == idRepB); +} + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &N, &X, &Y); + scanf("%d%d%d%d", &xi, &yi, &xf, &yf); + memset(grid, -1, sizeof(grid)); + queue fila; + priority_queue heap; + for (int i = 0; i < N; i++) + { + scanf("%d%d", &x, &y); + grid[y][x] = 0; + fila.push(abc(x, y)); + heap.push(abc(x, y, 0)); + } + while (!fila.empty()) + { + y = fila.front().y; + x = fila.front().x; + fila.pop(); + for (int i = 0; i < 4; i++) + { + if (isInside(x + q[i], y + p[i]) && grid[y + p[i]][x + q[i]] == -1) + { + grid[y + p[i]][x + q[i]] = grid[y][x] + 1; + fila.push(abc(x + q[i], y + p[i])); + heap.push(abc(x + q[i], y + p[i], grid[y][x] + 1)); + } + } + } + memset(rep, -1, sizeof(rep)); + int m = 0; + while (!heap.empty()) + { + aux = heap.top(); + heap.pop(); + rep[aux.y][aux.x] = getId(aux.x, aux.y); + for (int i = 0; i < 4; i++) + { + if (isInside(aux.x + q[i], aux.y + p[i]) && rep[aux.y + p[i]][aux.x + q[i]] != -1) + { + link(aux.x, aux.y, aux.x + q[i], aux.y + p[i]); + } + } + if (isConnected(xi, yi, xf, yf)) + { + m = aux.min; + break; + } + } + fila.push(abc(xi, yi, 0)); + while (!fila.empty()) + { + aux = fila.front(); + fila.pop(); + if (aux.x == xf && aux.y == yf) + break; + for (int i = 0; i < 4; i++) + { + if (isInside(aux.x + q[i], aux.y + p[i]) && grid[aux.y + p[i]][aux.x + q[i]] >= m) + { + grid[aux.y + p[i]][aux.x + q[i]] = -1; + fila.push(abc(aux.x + q[i], aux.y + p[i], aux.min + 1)); + } + } + } + printf("%d %d\n", m, aux.min); + } +} \ No newline at end of file diff --git a/1213.cpp b/1213.cpp new file mode 100644 index 0000000..20cff33 --- /dev/null +++ b/1213.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define N 1120 + +char primes[N]; + +int pnums[187]; +unsigned int dp[N + 1][187][15]; + +unsigned int nWays(int n, int i, int k) +{ + if (!n && !k) + { + return 1; + } + else if (n < 0 || i >= 187 || k == 0) + { + return 0; + } + else if (dp[n][i][k] != -1) + { + return dp[n][i][k]; + } + else + { + return dp[n][i][k] = nWays(n, i + 1, k) + nWays(n - pnums[i], i + 1, k - 1); + } +} + +int main() +{ + int i, j, k; + + memset(primes, 0, sizeof(primes)); + + primes[0] = primes[1] = 1; + for (i = 2; i * i < N; i++) + { + if (!primes[i]) + { + for (j = i * i; j < N; j += i) + { + primes[j] = 1; + } + } + } + + for (i = 0; i <= N; i++) + { + for (j = 0; j < 187; j++) + { + for (k = 0; k < 15; k++) + { + dp[i][j][k] = -1; + } + } + } + + j = 0; + for (i = 2; i < N; i++) + { + if (!primes[i]) + { + pnums[j++] = i; + } + } + + scanf("%d%d", &i, &j); + while (i || j) + { + printf("%u\n", nWays(i, 0, j)); + scanf("%d%d", &i, &j); + } + return 0; +} diff --git a/12130.cpp b/12130.cpp new file mode 100644 index 0000000..4f91e65 --- /dev/null +++ b/12130.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define VAL(x, y) ((x << 10) + y) +const int NN = 512; +int h[NN][NN], v[NN][NN]; +int ans, cols, d, rows; +int dx[4] = {0, 1, 0, -1}; +int dy[4] = {1, 0, -1, 0}; + +struct point +{ + int h, x, y; + bool operator<(point p) const + { + return h > p.h; + } +} p[NN * NN]; +int main() +{ + int T; + scanf("%d", &T); + while (T-- && scanf("%d %d %d", &rows, &cols, &d) == 3) + { + ans = 0; + int lim = rows * cols; + queue q; + FOR(i, lim) + { + int x = p[i].x = i / cols, y = p[i].y = i % cols; + scanf("%d", &p[i].h); + h[x][y] = p[i].h; + v[x][y] = -1; + } + sort(p, p + lim); + FOR(i, lim) + { + int sx = p[i].x, sy = p[i].y; + if (v[sx][sy] != -1) + { + continue; + } + q.push(VAL(sx, sy)); + bool is_summit = true; + int cnt = 0; + while (!q.empty()) + { + int z = q.front(); + q.pop(); + int x = z >> 10, y = z & 0x3FF; + cnt += h[x][y] == h[sx][sy]; + v[x][y] = h[sx][sy]; + FOR(dir, 4) + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (nx < 0 || ny < 0 || nx >= rows || ny >= cols) + { + continue; + } + if (h[nx][ny] > h[sx][sy] - d) + { + is_summit &= is_summit && v[nx][ny] <= h[sx][sy]; + if (v[nx][ny] == -1) + { + v[nx][ny] = h[sx][sy]; + q.push(VAL(nx, ny)); + } + } + } + } + if (is_summit) + { + ans += cnt; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12131.cpp b/12131.cpp new file mode 100644 index 0000000..6c055c7 --- /dev/null +++ b/12131.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define IN getc(stdin) + +const int MAXPLEN = 1024, MAXW = 10240; +struct word +{ + int l; + char w[128]; +} dict[10048]; + +int readString(char *s) +{ + char c; + int cnt(0); + for (c = IN; !islower(c); c = IN) + ; + for (; islower(c); *s++ = c, c = IN, ++cnt) + ; + *s = 0; + return cnt; +} + +char phrase[MAXPLEN]; +int cnt[32]; +int from[MAXPLEN][128], len[MAXPLEN]; +int ansidx[MAXPLEN], ansc, tmpans[MAXPLEN]; +int mark[MAXPLEN]; +int plen; +bool ambiguous, impossible; + +//#define DBG +bool dfs(int left, int d) +{ + if (left == plen) + { + if (!impossible) + { + ambiguous = true; + } + impossible = false; + ansc = d; + FOR(i, ansc) + ansidx[i] = tmpans[i]; + return true; + } + if (mark[left] != -1) + { + return mark[left]; + } + if (ambiguous) + { + return true; + } + bool r = false; + FOR(i, len[left]) + { + int x = tmpans[d] = from[left][i], z = left + dict[x].l; + ambiguous |= mark[z] == 1; + r |= mark[z] == 1 || dfs(z, d + 1) == 1; + } + return mark[left] = r; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int n; T--;) + { + plen = readString(phrase); + scanf("%d", &n); + /* INIT */ + FOR(i, plen) + len[i] = 0, + mark[i] = -1; + ambiguous = false; + impossible = true; + /* */ + FOR(i, n) + { + int wlen = dict[i].l = readString(dict[i].w); + if (wlen <= plen) + { + int mustbezero = 0; + FOR(j, 32) + cnt[j] = 0; + FOR(j, wlen) + { + ++cnt[dict[i].w[j] - 'a']; + --cnt[phrase[j] - 'a']; + } + FOR(j, 32) + mustbezero += cnt[j] != 0; + FOR(j, plen - wlen + 1) + { + char rejoin, leave; + if (j) + { + rejoin = phrase[j - 1] - 'a'; + leave = phrase[j + wlen - 1] - 'a'; + ++cnt[rejoin]; + mustbezero -= cnt[rejoin] == 0; + mustbezero += cnt[rejoin] == 1; + --cnt[leave]; + mustbezero -= cnt[leave] == 0; + mustbezero += cnt[leave] == -1; + } + if (mustbezero == 0 && phrase[j] == dict[i].w[0] && phrase[j + wlen - 1] == dict[i].w[wlen - 1]) + { + from[j][len[j]++] = i; + } + } + } + } + dfs(0, 0); + if (impossible) + { + puts("impossible"); + } + else if (ambiguous) + { + puts("ambiguous"); + } + else + { + FOR(i, ansc) + printf("%s%c", dict[ansidx[i]].w, i == ansc - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/12132.cpp b/12132.cpp new file mode 100644 index 0000000..ce54cb0 --- /dev/null +++ b/12132.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const int NN = 4096; +int cx[NN], cy[NN], cbp[64]; +int min(int a, int b) +{ + return a < b ? a : b; +} +int abs(int a) +{ + return a < 0 ? -a : a; +} +int main() +{ + int T; + scanf("%d", &T); + int h, l, z; + while (T--) + { + scanf("%d %d", &h, &l); + FOR(i, NN) + cx[i] = cy[i] = -1; + FOR(i, 64) + cbp[i] = 0; + FOR(i, h) + FOR(j, l) + { + scanf("%d", &z); + if (z != -1) + { + cx[z] = i, cy[z] = j; + } + } + long long cost = 0; + for (int i = 1; cx[i] != -1; ++i) + { + cost += 20 * cx[i]; + int diff = abs(cy[i] - cbp[cx[i]]); + cost += min(diff, l - diff) * 5; + cbp[cx[i]] = cy[i]; + } + printf("%lld\n", cost); + } + return 0; +} diff --git a/12134.cpp b/12134.cpp new file mode 100644 index 0000000..f5ffcdc --- /dev/null +++ b/12134.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +string construct_format_string(set &format, set &forbidden) +{ + string all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + if (format.empty()) + { + rep(i, all.size()) + { + if (forbidden.find(all[i]) == forbidden.end()) + { + format.insert(all[i]); + break; + } + } + } + else if (forbidden.empty()) + { + char c = *(format.rbegin()); + rep(i, all.size()) + { + if (all[i] < c) + { + format.insert(all[i]); + } + } + } + else + { + char c = *(format.rbegin()); + rep(i, all.size()) + { + if (all[i] < c && forbidden.find(all[i]) == forbidden.end()) + { + format.insert(all[i]); + } + } + } + string result = ""; + for (set::iterator it = format.begin(); it != format.end(); it++) + { + result += *it; + } + return result; +} + +int main() +{ + int n, case_number = 0; + string a, b; + while (cin >> n) + { + if (n == 0) + { + break; + } + case_number++; + bool ok = true; + set forbidden, format; + rep(i, n) + { + cin >> a >> b; + a = string(a.begin() + 1, a.begin() + a.size() - 1); + b = string(b.begin() + 1, b.begin() + b.size() - 1); + if (a.compare(0, b.size(), b) == 0) + { + rep(i, b.size()) + { + format.insert(b[i]); + } + if (a.size() > b.size()) + { + forbidden.insert(a[b.size()]); + } + } + else + { + ok = false; + } + } + vector diff; + back_insert_iterator> bi(diff); + set_intersection(format.begin(), format.end(), forbidden.begin(), forbidden.end(), bi); + if (!diff.empty()) + { + ok = false; + } + string result = construct_format_string(format, forbidden); + if (result.empty()) + { + ok = false; + } + if (ok) + { + cout << "Case " << case_number << ": [" << result << "]" << endl; + } + else + { + cout << "Case " << case_number << ": I_AM_UNDONE" << endl; + } + } + return 0; +} diff --git a/12135.cpp b/12135.cpp new file mode 100644 index 0000000..049c61a --- /dev/null +++ b/12135.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +//12135 +//Switch Bulbs +//Graphs;Shortest Path;BFS +#define MAX 33000 + +vector G[MAX]; +int V[MAX]; + +int n, m; +struct Step +{ + int x, w; + Step() {} + Step(int x, int w) : x(x), w(w) {} +}; + +int main() +{ + int t; + cin >> t; + int tt = 0; + while (cin >> n >> m, t--) + { + memset(G, 0, sizeof(G)); + memset(V, -1, sizeof(V)); + n = 1 << n; + for (int i = 0; i < m; i++) + { + int a, b, mask = 0; + cin >> a; + while (a--) + { + cin >> b; + mask = mask | (1 << b); + } + for (int i = 0; i < n; i++) + { + G[i].push_back(i ^ mask); + } + } + queue Q; + Q.push(Step(0, 0)); + while (!Q.empty()) + { + Step step = Q.front(); + Q.pop(); + if (V[step.x] >= 0) + { + continue; + } + V[step.x] = step.w; + for (int i = 0; i < G[step.x].size(); i++) + { + Q.push(Step(G[step.x][i], step.w + 1)); + } + } + cout << "Case " << ++tt << ":" << endl; + int q; + string s; + cin >> q; + while (q--) + { + int b = 0; + cin >> s; + for (int i = 0; i < s.size(); i++) + { + b = b * 2 + (s[i] - '0'); + } + cout << V[b] << endl; + } + cout << endl; + } +} diff --git a/12136.cpp b/12136.cpp index 82aefe3..db4c35c 100644 --- a/12136.cpp +++ b/12136.cpp @@ -1,17 +1,31 @@ -#include +#include -int main() { - int T, t, HH, MM; - scanf("%d", &T); - for (t = 1; t <= T; t++) { - int A, B, C, D; - scanf("%d:%d", &HH, &MM); A = HH * 60 + MM; - scanf("%d:%d", &HH, &MM); B = HH * 60 + MM; - scanf("%d:%d", &HH, &MM); C = HH * 60 + MM; - scanf("%d:%d", &HH, &MM); D = HH * 60 + MM; - - if (A > D || B < C) printf("Case %d: Hits Meeting\n", t); - else printf("Case %d: Mrs Meeting\n", t); +using namespace std; + +int main() +{ + int n; + int h1, m1, h2, m2; + pair meeting, wife; + char delim; + cin >> n; + for (int case_num = 0; case_num < n; case_num++) + { + cin >> h1 >> delim >> m1 >> h2 >> delim >> m2; + wife.first = h1 * 60 + m1; + wife.second = h2 * 60 + m2; + cin >> h1 >> delim >> m1 >> h2 >> delim >> m2; + meeting.first = h1 * 60 + m1; + meeting.second = h2 * 60 + m2; + cout << "Case " << case_num + 1 << ": "; + if (meeting.first > wife.second || meeting.second < wife.first) + { + cout << "Hits Meeting" << endl; + } + else + { + cout << "Mrs Meeting" << endl; + } } return 0; } diff --git a/12137.cpp b/12137.cpp new file mode 100644 index 0000000..0099d8e --- /dev/null +++ b/12137.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +//12137 +//Puzzles of Triangles +//Math;Prime Factorization +#define ull unsigned long long + +int W[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}, wn = 23; + +inline ull div(const ull &a, const ull &b, ull &r) +{ + r = a / b; + return a - r * b; +} + +inline ull pow(const ull &a, const int b) +{ + if (b == 0) + { + return 1; + } + ull tmp = b & 1 ? a : 1; + ull r = pow(a, b >> 1); + return tmp * r * r; +} + +int main() +{ + ull n; + int t = 0; + while (scanf("%llu", &n), n) + { + ull ncopy = n; + ull step = 1; + for (int i = 0; ncopy > 1 && i < wn; i++) + { + int power = 0; + ull divr; + while (div(ncopy, W[i], divr) == 0) + { + ncopy = divr; + power++; + } + step *= pow(W[i], (power + 1) / 2); + } + step *= ncopy; + ull result; + if (div(n, step, result) == 0) + { + result--; + } + result *= 8; + if (result) + { + printf("Case %d: %llu\n", ++t, result); + } + else + { + printf("Case %d: Impossible\n", ++t); + } + } +} diff --git a/12138.cpp b/12138.cpp new file mode 100644 index 0000000..fd2f3ec --- /dev/null +++ b/12138.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; +struct Edge +{ + int u, v; + int ul, ur, vl, vr; +}; +const int MAXV = 524288; +const int INF = 2000000000; +Edge e[MAXV]; +map R[MAXV]; +vector> g[MAXV]; +int V, E, W, S, T; +set> pQ; +int dist[MAXV]; +inline int update(int u, int d) +{ + if (dist[u] <= d || d >= W) + return 0; + if (dist[u] != INF) + pQ.erase(pQ.find(make_pair(dist[u], u))); + dist[u] = d; + pQ.insert(make_pair(dist[u], u)); + return 1; +} +int bfs(int st, int ed, int n) +{ + for (int i = 0; i <= n; i++) + dist[i] = INF; + pQ.clear(); + pair u, v; + pQ.insert(make_pair(0, 1)), dist[1] = 0; + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + if (u.second == ed) + return u.first; + for (auto e : g[u.second]) + update(e.first, dist[u.second] + e.second); + } + return dist[ed]; +} +int main() +{ + int cases = 0; + while (scanf("%d %d %d", &V, &E, &W) == 3 && V) + { + scanf("%d %d", &S, &T); + for (int i = 0; i < E; i++) + { + scanf("%d %d %d %d %d %d", + &e[i].u, &e[i].v, &e[i].ul, &e[i].ur, &e[i].vl, &e[i].vr); + } + for (int i = 0; i < E; i++) + { + R[e[i].u][e[i].ul] = 0; + R[e[i].v][e[i].vr] = 0; + } + R[1][0] = 0; + R[S][T] = 0; + // -- relabel + int vid = 0; + for (int i = 1; i <= V; i++) + { + int last_time = -1; + for (auto &e : R[i]) + { + e.second = ++vid; + if (last_time != -1) + {// keep in this node, make them run in order. + g[vid - 1].push_back(make_pair(vid, e.first - last_time)); + } + last_time = e.first; + } + } + // -- build graph + for (int i = 0; i < E; i++) + { + int tu = R[e[i].u][e[i].ul]; + int tv = R[e[i].v][e[i].vr]; + int cc = (e[i].ur - e[i].ul) + (e[i].vr - e[i].vl); + g[tu].push_back(make_pair(tv, cc)); + } + + printf("Plant %d: %d\n", ++cases, max(0, W - bfs(R[1][0], R[S][T], vid))); + + for (int i = 1; i <= vid; i++) + g[i].clear(); + } + return 0; +} +/* +3 2 50 +2 50 +1 3 0 10 20 30 +3 2 10 20 30 40 + +3 6 50 +2 50 +1 2 0 10 20 30 +1 2 5 6 9 11 +2 3 13 15 25 28 +3 3 32 33 40 45 +3 1 30 31 39 40 +1 2 41 42 48 49 +5 13 20 +3 1000 +3 3 41 41 999 1000 +3 3 39 40 1000 1000 +5 4 25 25 30 30 +1 2 2 2 6 6 +1 2 1 1 8 8 +2 2 7 7 13 13 +2 2 8 8 15 15 +2 3 14 14 20 20 +2 3 16 16 20 20 +4 3 30 30 40 40 +4 3 32 32 41 41 +3 5 21 21 25 25 +3 5 22 22 25 25 +3 3 50 +3 30 +1 2 5 10 15 25 +2 3 20 20 30 30 +2 3 25 25 30 30 +*/ diff --git a/12139.cpp b/12139.cpp new file mode 100644 index 0000000..85c14db --- /dev/null +++ b/12139.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +long long dp[70][70][70][2] = {};// [n][select][#operator][status] = ways +int main() +{ + dp[0][0][0][0] = 1; + for (int i = 0; i <= 64; i++) + { + for (int j = 0; j <= 64; j++) + { + for (int k = 0; k <= 64; k++) + { + dp[i + 1][j + 1][k][1] += dp[i][j][k][1]; + dp[i + 1][j][k + 1][0] += dp[i][j][k][1]; + + dp[i + 1][j][k][0] += dp[i][j][k][0]; + dp[i + 1][j + 1][k + 1][1] += dp[i][j][k][0]; + } + } + } + int n, m, cases = 0; + while (scanf("%d %d", &n, &m) == 2) + { + if (n == 0 && m == 0) + break; + long long a = 0, b = 0; + for (int j = m; j <= n; j++) + { + for (int i = 0; i <= n; i++) + { + if (i < j) + a += dp[n][j][i][0] + dp[n][j][i][1]; + if (i > j) + b += dp[n][j][i][0] + dp[n][j][i][1]; + } + } + printf("Case %d: %lld %lld\n", ++cases, a, b); + } + return 0; +} diff --git a/1214.cpp b/1214.cpp new file mode 100644 index 0000000..f5721fb --- /dev/null +++ b/1214.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define ll long long +#define inf 0x3fffffff +#define N 13 +#define MOD 60007 +#define STA 1000010 + +int n, m, S; +struct HashMap +{ + int hd[MOD], nxt[STA], sta[STA], f[STA], size; + void init() + { + size = 0; + memset(hd, -1, sizeof(hd)); + } + void add(int s, int a) + { + int h = s % MOD; + for (int i = hd[h]; i + 1; i = nxt[i]) + { + if (sta[i] == s) + { + f[i] = min(f[i], a); + return; + } + } + nxt[size] = hd[h]; + sta[size] = s; + f[size] = a; + hd[h] = size++; + return; + } +} hs[2]; + +int mmap[N][N]; +int solve() +{ + S = (1 << ((m + 1) << 1)) - 1; + int cur = 0; + hs[0].init(); + hs[0].add(0, 0); + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + hs[cur = !cur].init(); + for (int k = 0; k < hs[cur ^ 1].size; k++) + { + int s = hs[cur ^ 1].sta[k]; + int fk = hs[cur ^ 1].f[k] + 1; + int x = (3 << (j << 1)) & s; + int y = (3 << ((j + 1) << 1)) & s; + int left = x >> (j << 1); + int up = y >> ((j + 1) << 1); + if (mmap[i][j] == 1) + { + if (left == 0 && up == 0) + { + hs[cur].add(s, fk - 1); + if (mmap[i + 1][j] && mmap[i][j + 1]) + { + hs[cur].add(s | (2 << (j << 1)) | (2 << ((j + 1) << 1)), fk); + hs[cur].add(s | (3 << (j << 1)) | (3 << ((j + 1) << 1)), fk); + } + } + else if (left == up && left) + { + hs[cur].add(s ^ x ^ y, fk); + } + else if ((left == 0 && up == 2) || (left == 2 && up == 0)) + { + if (mmap[i + 1][j]) + hs[cur].add(s ^ y | (2 << (j << 1)), fk); + if (mmap[i][j + 1]) + hs[cur].add(s ^ x | (2 << ((j + 1) << 1)), fk); + } + else if ((left == 0 && up == 3) || (left == 3 && up == 0)) + { + if (mmap[i + 1][j]) + hs[cur].add(s ^ y | (3 << (j << 1)), fk); + if (mmap[i][j + 1]) + hs[cur].add(s ^ x | (3 << ((j + 1) << 1)), fk); + } + } + else if (mmap[i][j] == 2) + { + if (left == 2 && up == 0) + hs[cur].add(s ^ x, fk); + else if (left == 0 && up == 2) + hs[cur].add(s ^ y, fk); + else if (left == 0 && up == 0) + { + if (mmap[i + 1][j] == 1 || mmap[i + 1][j] == 2) + hs[cur].add(s | (2 << (j << 1)), fk); + if (mmap[i][j + 1] == 1 || mmap[i][j + 1] == 2) + hs[cur].add(s | (2 << ((j + 1) << 1)), fk); + } + } + else if (mmap[i][j] == 3) + { + if (left == 3 && up == 0) + hs[cur].add(s ^ x, fk); + else if (left == 0 && up == 3) + hs[cur].add(s ^ y, fk); + else if (left == 0 && up == 0) + { + if (mmap[i + 1][j] == 1 || mmap[i + 1][j] == 3) + hs[cur].add(s | (3 << (j << 1)), fk); + if (mmap[i][j + 1] == 1 || mmap[i][j + 1] == 3) + hs[cur].add(s | (3 << ((j + 1) << 1)), fk); + } + } + else + { + if (left == 0 && up == 0) + hs[cur].add(s, fk - 1); + } + } + } + hs[cur ^ 1].init(); + for (int i = 0; i < hs[cur].size; i++) + hs[cur ^ 1].add((hs[cur].sta[i] << 2) & S, hs[cur].f[i]); + cur ^= 1; + } + cur ^= 1; + for (int i = 0; i < hs[cur].size; i++) + if (hs[cur].sta[i] == 0) + return hs[cur].f[i] - 2; + return 0; +} + +int main() +{ + + int ans; + while (scanf("%d%d", &n, &m) != EOF && (m || n)) + { + + memset(mmap, 0, sizeof(mmap)); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + scanf("%d", &mmap[i][j]); + if (mmap[i][j] < 2) + mmap[i][j] ^= 1; + } + + ans = solve(); + printf("%d\n", ans); + } +} diff --git a/12140.cpp b/12140.cpp new file mode 100644 index 0000000..b9c123d --- /dev/null +++ b/12140.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +double low, high, mid, eps = 1e-8, now_x, now_y; +int dp[1 << 19], n, k, cont[1 << 19], cnt; + +struct point { + double x, y; +}; +point mon[20]; + +struct pp { + int state, num; + bool operator<(const pp &temp) const { return num > temp.num; } +}; +pp lst[1000]; + +bool dfs(int state, int id, int now_v, int now_num) { + int ns, i, j, s, p, q, least = now_v, tnum; + if (dp[state] <= now_v) { + return false; + } + dp[state] = now_v; + if (now_v > k) { + return false; + } + if (state == (1 << n) - 1 || now_v + n - now_num <= k) { + return true; + } + ns = state; + pp nlst[170]; + for (i = 0; i < cnt; i++) { + nlst[i] = lst[i]; + nlst[i].num = cont[nlst[i].state | state] - cont[state]; + } + sort(nlst, nlst + cnt); + tnum = now_num; + for (i = 0; i < cnt; i++) { + tnum += max(1, nlst[i].num); + least++; + if (tnum >= n) { + break; + } + } + if (least > k) { + return false; + } + for (i = 0; i < cnt; i++) { + if (dfs(state | nlst[i].state, id, now_v + 1, now_num + nlst[i].num)) { + return true; + } + } + return false; +} + +bool check(double r) { + int i, j, s, p, q, ncnt; + double dis; + cnt = 0; + for (i = 0; i < n; i++) + for (j = i + 1; j < n; j++) { + dis = sqrt((mon[i].x - mon[j].x) * (mon[i].x - mon[j].x) + + (mon[i].y - mon[j].y) * (mon[i].y - mon[j].y)); + if (dis > 2 * r) { + continue; + } + double theta, alpha, fi; + theta = atan2(mon[i].y - mon[j].y, mon[i].x - mon[j].x); + alpha = acos(dis / (2.0 * r)); + for (s = 0; s < 2; s++) { + if (s == 0) { + fi = theta + alpha; + } else { + fi = theta - alpha; + } + now_x = mon[j].x + r * cos(fi); + now_y = mon[j].y + r * sin(fi); + lst[cnt].state = lst[cnt].num = 0; + for (p = 0; p < n; p++) { + dis = sqrt((mon[p].x - now_x) * (mon[p].x - now_x) + + (mon[p].y - now_y) * (mon[p].y - now_y)); + if (dis < r + 1e-7) { + lst[cnt].state |= (1 << p); + lst[cnt].num++; + } + } + cnt++; + } + } + sort(lst, lst + cnt); + ncnt = 0; + for (i = 0; i < cnt; i++) { + for (j = 0; j < ncnt; j++) { + if ((lst[i].state & lst[j].state) == lst[i].state) { + break; + } + } + if (j >= ncnt) { + lst[ncnt++] = lst[i]; + } + } + cnt = ncnt; + for (i = 0; i < (1 << n); i++) { + dp[i] = 1000000000; + } + return dfs(0, 0, 0, 0); +} + +int main() { + int i, j, s, p, q, tst = 0, t; + for (i = 0; i < (1 << 18); i++) { + j = i; + cont[i] = 0; + while (j) { + cont[i]++; + j = ((j - 1) & j); + } + } + scanf("%d", &t); + while (t--) { + tst++; + scanf("%d%d", &n, &k); + for (i = 0; i < n; i++) { + scanf("%lf%lf", &mon[i].x, &mon[i].y); + } + low = 0; + high = 7500; + while (low < high - eps) { + mid = (low + high) / 2.0; + if (check(mid)) { + high = mid; + } else { + low = mid + eps; + } + } + printf("Case %d: %.2f\n", tst, low); + } +} diff --git a/12141.cpp b/12141.cpp new file mode 100644 index 0000000..b54e1ce --- /dev/null +++ b/12141.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +#define MAXN 10050 +int dp[MAXN][64][3] = {}; +int UBIT[64][3][MAXN], DBIT[64][3][MAXN]; +int shift[64][3][MAXN] = {}; +const int mod = 1000000; +int query(int A[], int idx) +{ + int sum = 0; + while (idx) + sum = (sum + A[idx]) % mod, idx -= idx & (-idx); + return sum; +} +void modify(int A[], int idx, int val, int L) +{ + while (idx <= L) + A[idx] = (A[idx] + val) % mod, idx += idx & (-idx); +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out2.txt", "w+t", stdout); + int testcase, n, m, x, y; + int cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + vector> D; + map R; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &x, &y); + R[y] = 0; + D.push_back(make_pair(x, y)); + } + sort(D.begin(), D.end()); + int L = 1; + for (map::iterator it = R.begin(); + it != R.end(); it++) + it->second = L++; + for (int i = 0; i < n; i++) + D[i].second = R[D[i].second]; + memset(UBIT, 0, sizeof(UBIT)); + memset(DBIT, 0, sizeof(DBIT)); + memset(shift, 0, sizeof(shift)); + int used[10005] = {}; + for (int i = 0; i < n; i++) + { + if (used[D[i].second] == 0) + { + shift[0][0][D[i].second] = -1;// = + used[D[i].second] = 1; + } + } + modify(UBIT[0][1], 1, 1, L); + modify(UBIT[0][2], 1, 1, L); + int ret = 0; + if (m == 0) + ret++; + for (int i = 0; i < n; i++) + { + for (int k = 0; k <= m; k++) + { + int dp0 = query(UBIT[k][0], D[i].second) + query(DBIT[k][0], L - D[i].second - 1) - shift[k][0][D[i].second]; + int dp1 = query(UBIT[k][1], D[i].second) + query(DBIT[k][1], L - D[i].second - 1) - shift[k][1][D[i].second]; + int dp2 = query(UBIT[k][2], D[i].second) + query(DBIT[k][2], L - D[i].second - 1) - shift[k][2][D[i].second]; + // printf("[%d %d] %d %d %d\n", i, k, dp0, dp1, dp2); + dp0 %= mod, dp1 %= mod, dp2 %= mod; + shift[k][0][D[i].second] += dp0; + shift[k][1][D[i].second] += dp1; + shift[k][2][D[i].second] += dp2; + if (k == m) + ret = (ret + dp0) % mod; + if (dp2) + { + modify(UBIT[k][0], D[i].second + 1, dp2, L); + modify(UBIT[k + 1][1], D[i].second + 1, dp2, L); + modify(UBIT[k][2], D[i].second + 1, dp2, L); + } + if (dp1) + { + modify(DBIT[k][0], L - D[i].second, dp1, L); + modify(DBIT[k][1], L - D[i].second, dp1, L); + modify(DBIT[k + 1][2], L - D[i].second, dp1, L); + } + } + } + printf("Case %d: %d\n", ++cases, (ret + mod) % mod); + } + return 0; +} +/* +3 +3 0 +1 1 +2 1 +3 1 + +3 1 +101 3 +102 2 +104 4 + +3 +6 1 +320 3 +306 1 +401 3 +325 4 +393 5 +380 2 +4 1 +101 3 +102 2 +103 2 +104 4 +3 0 +102 2 +101 1 +103 3 +*/ diff --git a/12143.cpp b/12143.cpp new file mode 100644 index 0000000..84a3f0a --- /dev/null +++ b/12143.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +typedef long long LL; + +struct Frac +{ + LL a, b; + Frac() + { + a = 0, b = 1; + } + Frac(LL x, LL y) + { + a = x, b = y; + reduce(); + } + Frac operator+(const Frac &y) + { + LL ta, tb; + tb = this->b / gcd(this->b, y.b) * y.b; + ta = this->a * (tb / this->b) + y.a * (tb / y.b); + Frac z(ta, tb); + return z; + } + Frac operator-(const Frac &y) + { + LL ta, tb; + tb = this->b / gcd(this->b, y.b) * y.b; + ta = this->a * (tb / this->b) - y.a * (tb / y.b); + Frac z(ta, tb); + return z; + } + Frac operator*(const Frac &y) + { + LL tx, ty, tz, tw, g; + tx = this->a, ty = y.b; + g = gcd(tx, ty), tx /= g, ty /= g; + tz = this->b, tw = y.a; + g = gcd(tz, tw), tz /= g, tw /= g; + Frac z(tx * tw, ty * tz); + return z; + } + Frac operator/(const Frac &y) + { + LL tx, ty, tz, tw, g; + tx = this->a, ty = y.a; + g = gcd(tx, ty), tx /= g, ty /= g; + tz = this->b, tw = y.b; + g = gcd(tz, tw), tz /= g, tw /= g; + Frac z(tx * tw, ty * tz); + return z; + } + + static LL gcd(LL x, LL y) + { + if (!y) + { + return x; + } + if (x < 0) + { + x *= -1; + } + if (y < 0) + { + y *= -1; + } + LL t; + while (x % y) + { + t = x, x = y, y = t % y; + } + return y; + } + void reduce() + { + LL g = gcd(a, b); + a /= g, b /= g; + if (b < 0) + { + a *= -1, b *= -1; + } + } +}; + +inline LL mpow(LL x, LL y, LL mod) +{ + if (y == 0) + { + return 1; + } + if (y & 1) + { + return (x * mpow((x * x) % mod, y >> 1, mod)) % mod; + } + return mpow((x * x) % mod, y >> 1, mod); +} + +Frac x[20]; + +inline void gauss() +{ +#define poly 11 + + int i, j, k, l, m, n; + Frac mtx[20][20]; + for (n = 1; n <= poly; n++) + { + LL sum = 0; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + for (k = 1; k <= n; k++) + for (l = 1; l <= n; l++) + for (m = 1; m <= n; m++) + { + sum += (LL)abs(i - j) * abs(j - k) * abs(k - l) * abs(l - m) * abs(m - i); + } + mtx[n][poly + 1] = Frac(sum, 1); + for (i = 1; i <= poly; i++) + { + mtx[n][i] = Frac(mpow(n, i - 1, 1LL << 60), 1LL); + } + } + for (i = 1; i <= poly; i++) + { + k = i; + for (j = i + 1; j <= poly; j++) + { + if (mtx[j][i].a > mtx[k][i].a) + { + k = j; + } + } + if (mtx[k][i].a == 0) + { + continue; + } + if (k != i) + { + for (j = 1; j <= poly + 1; j++) + { + swap(mtx[k][j], mtx[i][j]); + } + } + for (j = 1; j <= poly; j++) + { + if (i == j) + { + continue; + } + for (k = poly + 1; k >= i; k--) + { + mtx[j][k] = mtx[j][k] - mtx[j][i] / mtx[i][i] * mtx[i][k]; + } + } + } + for (i = 1; i <= poly; i++) + { + x[i - 1] = mtx[i][poly + 1] / mtx[i][i]; + } +} + +int main() +{ + gauss(); + int n, i; + while (scanf("%d", &n) == 1 && n) + { + LL ret = 0; + for (i = 0; i <= 10; i++) + { + ret += x[i].a * mpow(n, i, 10007) * mpow(x[i].b, 10007 - 2, 10007); + ret %= 10007; + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/12144.cpp b/12144.cpp new file mode 100644 index 0000000..354b0c5 --- /dev/null +++ b/12144.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +#define NN 500 +#define INF 1061109567 + +vector> p(500); +queue q; +int n; +int adj[NN][NN]; +/* {{{ FAST integer input */ +#define X10(n) ((n << 3) + (n << 1)) +#define RI readint +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +bool adapt()// Returns true if there is a number waiting to be read, false otherwise +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +/* }}} end FAST integer input */ +int dijkstra(int s, int t) +{ + int in[NN], d[NN]; + int best, i, temp, u = s; + for (i = 0; i < n; i++) + { + d[i] = adj[s][i], in[i] = 0; + } + d[s] = 0; + while (!in[t]) + { + in[u] = 1; + for (i = 0; i < n; i++) + if (adj[u][i] < INF) + { + temp = d[u] + adj[u][i]; + if (temp < d[i]) + { + d[i] = d[u] + adj[u][i], + p[i].clear(); + p[i].push_back(u); + } + else if (temp == d[i]) + { + p[i].push_back(u); + } + } + best = INF; + for (i = 0; i < n; i++) + if (!in[i] && best > d[i]) + { + best = d[u = i]; + } + if (best == INF) + { + break; + } + } + return ((!in[t]) ? (-1) : (d[t])); +} + +int main() +{ + int a, b, d, E, i, s, w; + vector::iterator it; + while (RI(n) && RI(E) && n) + { + RI(s) + , RI(d); + for (i = 0; i < n; i++) + { + p[i].clear(); + } + memset(adj, 63, sizeof(adj)); + for (i = 0; i < E; i++) + { + RI(a) + , RI(b), RI(adj[a][b]); + } + if ((w = dijkstra(s, d)) == -1) + { + printf("-1\n"); + continue; + } + q.push(d); + while (!q.empty()) + { + int k = q.front(); + q.pop(); + for (it = p[k].begin(); it != p[k].end(); it++) + { + adj[(*it)][k] = INF, q.push((*it)); + } + p[k].clear(); + } + printf("%d\n", dijkstra(s, d)); + } + return 0; +} diff --git a/12146.cpp b/12146.cpp new file mode 100644 index 0000000..58c3486 --- /dev/null +++ b/12146.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define max(a, b) ((a < b) ? (b) : (a)) + +/* {{{ FAST integer input */ +#define X10(n) ((n << 3) + (n << 1)) +#define RI readint +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +bool adapt()// Returns true if there is a number waiting to be read, false otherwise +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +/* }}} end FAST integer input */ +int main() +{ + int c, i, j, pC1, pC2, pL1, pL2, r, temp, v; + while (RI(r) && RI(c) && r) + { + for (pL1 = pL2 = i = 0; i < r; i++) + { + for (pC1 = pC2 = j = 0; j < c; j++) + { + RI(v); + temp = pC1; + pC1 = max(pC1, v + pC2); + pC2 = temp; + } + temp = pL1; + pL1 = max(pL1, pC1 + pL2); + pL2 = temp; + } + printf("%d\n", pL1); + } + return 0; +} diff --git a/12147.cpp b/12147.cpp new file mode 100644 index 0000000..f050f5e --- /dev/null +++ b/12147.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +const int N = 1024; +char s[N], t[N], *ss = s - 1, *tt = t - 1; +int dp[N][N], w[N][N]; +int max(int a, int b) +{ + return a > b ? a : b; +} +int main() +{ + for (int l1, l2, k; scanf("%d", &k) && k;) + { + scanf("%s %s", s, t); + for (l1 = -1; s[++l1];) + ; + for (l2 = -1; t[++l2];) + ; + w[0][0] = 0; + for (int i = 0; i < N; ++i) + { + dp[i][0] = dp[0][i] = 0; + } + for (int i = 1; i <= l1; ++i) + for (int j = 1; j <= l2; ++j) + { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + w[i][j] = ss[i] == tt[j] ? w[i - 1][j - 1] + 1 : 0; + if (w[i][j] >= k) + for (int z = k; z <= w[i][j]; ++z) + { + dp[i][j] = max(dp[i][j], dp[i - z][j - z] + z); + } + } + printf("%d\n", dp[l1][l2]); + } + return 0; +} diff --git a/12148.cpp b/12148.cpp new file mode 100644 index 0000000..d621b39 --- /dev/null +++ b/12148.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +bool leap(int x) +{ + return x % 400 == 0 || ((x & 3) && !(x % 100)) || (!(x & 3) && x % 100); +} +int days[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; +int main() +{ + int c, d, m, y; + int nc, nd, nm, ny; + for (int n; scanf("%d", &n) && n;) + { + int count = 0, sum = 0; + nc = nd = nm = ny = -1; + for (int i = 0; i < n; ++i) + { + scanf("%d %d %d %d", &d, &m, &y, &c); + if (d == nd && m == nm && y == ny) + { + ++count; + sum += c - nc; + } + nc = c; + nd = d + 1; + nm = m; + ny = y; + if (nd > days[m] + (m == 2 && leap(y))) + { + nd = 1; + if (++nm > 12) + { + nm = 1, ++ny; + } + } + } + printf("%d %d\n", count, sum); + } + return 0; +} diff --git a/12149.cpp b/12149.cpp new file mode 100644 index 0000000..8704e97 --- /dev/null +++ b/12149.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int n; + int answers[101]; + answers[0] = 0; + for (int i = 1; i < 101; i++) + { + answers[i] = i * i + answers[i - 1]; + } + while (cin >> n) + { + if (!n) + { + break; + } + cout << answers[n] << endl; + } + return 0; +} diff --git a/1215.cpp b/1215.cpp new file mode 100644 index 0000000..14275da --- /dev/null +++ b/1215.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int main() +{ + int cases, tmp, k, count; + vector cuts; + string input; + set cuts_made; + set::const_iterator it; + cin >> cases; + while (cases--) + { + cin >> k; + count = 0; + cuts.clear(); + cuts_made.clear(); + for (int i = 0; i < k; i++) + { + cin >> tmp; + cuts.push_back(tmp); + } + cin.ignore(100, '\n'); + getline(cin, input); + for (int i = 0, sz = cuts.size(); i < sz; i++) + { + int min = 0; + int max = input.size(); + for (it = cuts_made.begin(); it != cuts_made.end(); it++) + { + if (*it == cuts[i]) + { + min = *it; + max = *it; + break; + } + else if (*it < cuts[i]) + { + min = *it; + } + else if (*it > cuts[i]) + { + max = *it; + break; + } + } + cuts_made.insert(cuts[i]); + set a, b; + vector result; + for (int j = min; j < cuts[i]; j++) + { + a.insert(input[j]); + } + for (int j = cuts[i]; j < max; j++) + { + b.insert(input[j]); + } + back_insert_iterator> bi(result); + set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), bi); + count += result.size(); + } + cout << count << endl; + } + return 0; +} diff --git a/12150.cpp b/12150.cpp new file mode 100644 index 0000000..2311dd5 --- /dev/null +++ b/12150.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int p[1024]; +int main() +{ + int dpos, i, n, pos, who; + while (scanf("%d", &n) && n) + { + bool valid = true; + memset(p, 0, n << 2); + for (i = 0; i < n; i++) + { + scanf("%d %d", &who, &dpos); + pos = i + dpos; + if (valid &= !(pos < 0 || pos >= n || p[pos])) + { + p[pos] = who; + } + } + if (valid) + for (i = 0; i < n; i++) + { + printf("%d%c", p[i], i == n - 1 ? 10 : 32); + } + else + { + puts("-1"); + } + } + return 0; +} diff --git a/12153.cpp b/12153.cpp new file mode 100644 index 0000000..e12a3e4 --- /dev/null +++ b/12153.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +int p_cards[10][300], deck[300], card_num[10]; +int best, deck_top, dir, didx, i, j, M, N, P, rnum, top, tnum, tsuit, winner; +char rsuit; +bool pen; +char ss(int a) +{ + switch (a) + { + case 0: + return 'C'; + case 1: + return 'D'; + case 2: + return 'H'; + case 3: + return 'S'; + } +} +int SUIT(char s) +{ + switch (s) + { + case 'C': + return 0; + case 'D': + return 1; + case 'H': + return 2; + case 'S': + return 3; + } +} +void read_card(int *x) +{ + scanf("%d %c", &rnum, &rsuit); + *x = ((rnum * 4) + SUIT(rsuit)); +} +void buy(int k, int hm) +{ + while (hm--) + { + p_cards[k][card_num[k]++] = deck[deck_top++]; + } +} +void update_discard(int x, int idx) +{ + if ((x / 4 == tnum || x % 4 == tsuit) && x > best) + { + best = x, didx = idx; + } +} +void play(int k) +{ + best = -1; + if (k < 0) + { + k += P; + } + k %= P; + if (!pen) + { + pen = 1; + if (tnum == 7) + { + buy(k, 2), k += dir; + } + else if (tnum == 1) + { + buy(k, 1), k += dir; + } + else if (tnum == 11) + { + k += dir; + } + } + if (k < 0) + { + k += P; + } + k %= P; + for (i = 0; i < card_num[k]; i++) + { + update_discard(p_cards[k][i], i); + } + if (best == -1) + { + buy(k, 1), update_discard(p_cards[k][card_num[k] - 1], card_num[k] - 1); + } + if (best != -1) + { + top = best; + pen = 0; + tnum = top / 4; + tsuit = top % 4; + if (tnum == 12) + { + dir = -dir; + } + p_cards[k][didx] = p_cards[k][--card_num[k]]; + if (card_num[k] == 0) + { + winner = k; + return; + } + } + play(k + dir); +} +int main() +{ + while (scanf("%d%d%d", &P, &M, &N) && (P || M || N)) + { + dir = 1; + pen = 0; + for (i = 0; i < P; i++) + { + card_num[i] = M; + for (j = 0; j < M; j++) + { + read_card(&p_cards[i][j]); + } + } + read_card(&top); + tnum = top / 4; + tsuit = top % 4; + if (tnum == 12) + { + dir = -dir; + } + N -= (P * M + 1); + for (deck_top = i = 0; i < N; i++) + { + read_card(&deck[i]); + } + play(0); + printf("%d\n", winner + 1); + } +} diff --git a/12154.cpp b/12154.cpp new file mode 100644 index 0000000..8ba7f63 --- /dev/null +++ b/12154.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +bool z[10000001]; +int v[10048]; +int main() +{ + for (int n; scanf("%d", &n) == 1 && n;) + { + int ans = -1, sum = 0; + for (int i = 0; i < n; ++i) + { + scanf("%d", v + i); + sum += v[i]; + } + for (int i = 0; i < sum; ++i) + { + z[i] = false; + } + for (int i = sum = 0; i < n; ++i) + { + z[sum += v[i]] = true; + } + for (int i = n; ans == -1 && i >= 3; i--) + if (!(sum % i)) + { + int b = 0, w = sum / i; + for (int s = 0; b <= w; ++s) + { + bool good = true; + for (int j = b; good && j < sum; j += w) + { + good &= z[j]; + } + if (good) + { + ans = n - i; + break; + } + b += v[s]; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12155.cpp b/12155.cpp new file mode 100644 index 0000000..45f11d9 --- /dev/null +++ b/12155.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +char map_[50000], ans[63000]; + +int main() +{ + int N, R1, R2, C1, C2, cnum = 0; + for (int i = 0; i < 50000; i++) + { + map_[i] = 'a' + (i % 26); + } + while (scanf("%d%d%d%d%d", &N, &R1, &C1, &R2, &C2) && N) + { + printf("Case %d:\n", ++cnum); + int tlines = 2 * N - 1; + ++R2; + ++C2; + register char *p = ans; + for (int i = R1; i < R2; ++i) + { + int line = abs((i % tlines) - (N - 1)); + int maxd = N - 1 - line; + for (int j = C1; j < C2; ++j) + { + int col = abs((j % tlines) - (N - 1)); + if (col > maxd) + { + *p++ = '.'; + } + else + { + *p++ = map_[col + line]; + } + } + *p++ = 10; + } + *--p = 0; + puts(ans); + } + return 0; +} diff --git a/12157.cpp b/12157.cpp index 38ef234..0c48e7d 100644 --- a/12157.cpp +++ b/12157.cpp @@ -1,21 +1,34 @@ -#include -#define min(A, B) ((A) < (B) ? (A) : (B)) +#include -int main() { - int T, t; - scanf("%d", &T); - for (t = 1; t <= T; t++) { - int N, duration, Mile = 0, Juice = 0; - scanf("%d", &N); - while (N--) { - scanf("%d", &duration); - Mile += (duration/30 + 1) * 10; - Juice += (duration/60 + 1) * 15; +using namespace std; + +int main() +{ + int t, n, d; + cin >> t; + for (int case_num = 0; case_num < t; case_num++) + { + cin >> n; + int mile = 0, juice = 0; + while (n--) + { + cin >> d; + mile += ((d / 30) + 1) * 10; + juice += ((d / 60) + 1) * 15; + } + cout << "Case " << case_num + 1 << ": "; + if (mile < juice) + { + cout << "Mile " << mile << endl; + } + else if (juice < mile) + { + cout << "Juice " << juice << endl; + } + else + { + cout << "Mile Juice " << mile << endl; } - printf("Case %d:", t); - if (Mile <= Juice) printf(" Mile"); - if (Juice <= Mile) printf(" Juice"); - printf(" %d\n", min(Mile, Juice)); } return 0; } diff --git a/12159.cpp b/12159.cpp new file mode 100644 index 0000000..db8b9fb --- /dev/null +++ b/12159.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +int N, R, ts[3]; +struct tower +{ + int x, y, p; + int team; + vector ck; + inline int sqr(int x) + { + return (x * x); + } + int d2(tower kk) + { + return sqr(x - kk.x) + sqr(y - kk.y); + } + tower(int _x = 0, int _y = 0) : x(_x), y(_y) {} + tower operator-(tower t2) + { + return tower(x - t2.x, y - t2.y); + } + int operator*(tower t2) + { + return (x * t2.y) - (y * t2.x); + } + bool operator<(const tower x) const + { + return ts[team] < ts[x.team]; + } +} t[312]; +bool ccw(tower A, tower B, tower C) +{ + return ((C - A) * (B - A) < 0); +} + +/* {{{ Hopcroft-Karp */ +#define tr(vec) for (typeof(vec.begin()) it = vec.begin(); it != vec.end(); it++) +#define NN 312 +//#define DEBUG +int d[NN], p[NN], q[NN]; + +bool BFS(int n, int m) +{ + bool f = 0; + int i, h = 0, tail = 0; + memset(d, 0, (n + m) * 4); + for (i = 0; i < n; i++) + if (p[i] == -1) + { + q[tail++] = i; + } + while (h < tail) + { + int x = q[h++]; + vector::iterator it = t[x].ck.begin(); + while (it != t[x].ck.end()) + { + if (!d[*it]) + { + d[*it] = d[x] + 1; + if (p[*it] == -1) + { + f = 1; + } + else + { + d[p[*it]] = d[*it] + 1, q[tail++] = p[*it]; + } + } + it++; + } + } + return f; +} +bool DFS(int x) +{ + vector::iterator it = t[x].ck.begin(); + while (it != t[x].ck.end()) + { + if (d[(*it)] == d[x] + 1) + { + d[*it] = 0; + if (p[*it] == -1 || DFS(p[*it])) + { + return p[x] = *it, p[*it] = x, true; + } + } + it++; + } + return false; +} +int hopcroft_karp(int n, int m) +{ + int i, mm = 0; + memset(p, -1, (n + m) * 4); + while (BFS(n, m)) + for (i = 0; i < n; i++) + { + mm += (p[i] == -1 && DFS(i)); + } + return mm; +} +/* }}} */ +int main() +{ + int a, b, c2; + ts[2] = 1500; + for (int cnum = 0; scanf("%d", &N) && N;) + { + printf("Case %d: ", ++cnum); + ts[0] = ts[1] = 0; + for (int i = 0; i < N; ++i) + { + scanf("%d%d%d", &t[i].x, &t[i].y, &t[i].p); + t[i].ck.clear(); + } + scanf("%d%d%d", &a, &b, &R), --a, --b; + R *= R; + t[a].team = t[b].team = c2 = 2; + for (int i = 0; i < N; ++i) + { + if (i == a || i == b) + { + continue; + } + if (t[i].p == 0) + { + t[i].team = 2, c2++; + } + else if (ccw(t[a], t[b], t[i])) + { + ts[0]++; + t[i].team = 0; + } + else + { + ts[1]++; + t[i].team = 1; + } + } + sort(t, t + N); + N -= c2; + int AA, BB; + if (ts[0] < ts[1]) + { + AA = ts[0], BB = ts[1]; + } + else + { + AA = ts[1], BB = ts[0]; + } + for (int i = 0; i < AA; ++i) + if (t[i].p != 0) + for (int j = AA; j < N; ++j) + if (t[i].p > t[j].p && t[i].d2(t[j]) <= R) + { + t[i].ck.push_back(j); + } + printf("%d\n", hopcroft_karp(AA, BB)); + } + return 0; +} diff --git a/1216.cpp b/1216.cpp new file mode 100644 index 0000000..6263bda --- /dev/null +++ b/1216.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +//1216 +//The Bug Sensor Problem +//Graphs;DFS +#define MAX 1000 + +double G[MAX][MAX]; +int X[MAX], Y[MAX], n, k; +int V[MAX]; + +void dfs(int v, int comp, int max) +{ + V[v] = comp; + for (int i = 0; i < n; i++) + { + if (!V[i] && G[v][i] <= max) + { + dfs(i, comp, max); + } + } +} + +int main() +{ + int t; + cin >> t; + t = 0; + while (cin >> k) + { + n = 0; + double maxd = 0; + while (cin >> X[n], X[n] != -1) + { + cin >> Y[n]; + for (int i = 0; i < n; i++) + { + G[i][n] = G[n][i] = sqrt(pow(X[n] - X[i], 2.0) + pow(Y[n] - Y[i], 2.0)); + maxd = max(maxd, G[i][n]); + } + n++; + } + int begin = 0, end = (int)ceil(maxd); + int best, last = -1; + while (begin <= end) + { + int mid = (begin + end) / 2; + if (mid == last) + { + break; + } + int comp = 0; + memset(V, 0, sizeof(V)); + for (int i = 0; i < n; i++) + if (!V[i]) + { + dfs(i, ++comp, mid); + } + last = mid; + if (comp > k) + { + begin = mid; + } + else + { + if (comp == k) + { + best = mid; + } + end = mid; + } + } + cout << best << endl; + } +} diff --git a/12160.cpp b/12160.cpp new file mode 100644 index 0000000..9c58903 --- /dev/null +++ b/12160.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define NN 10000 +#define IN getc(stdin) + +int q[NN], p[NN], B[15]; +bool v[NN]; +void fastint(register int *n) +{ + register char c; + *n = 0; + while (!isdigit(c = IN)) + ; + do + { + (*n) *= 10; + (*n) += (c - '0'); + c = IN; + } while (isdigit(c)); +} +int main() +{ + int L, U, R, cnum = 0; + int h, t; + while (true) + { + fastint(&L); + fastint(&U); + fastint(&R); + if (!(L || U || R)) + { + break; + } + memset(v, 0, sizeof(v)); + printf("Case %d: ", ++cnum); + h = t = 0; + q[t++] = L; + v[L] = 1; + p[L] = 0; + for (int i = 0; i < R; ++i) + { + fastint(&B[i]); + } + while (h != t && !v[U]) + { + int x = q[h++], press = p[x]; + for (int i = 0; i < R; ++i) + { + int y = (x + B[i]) % 10000; + if (!v[y]) + { + v[y] = 1; + q[t++] = y; + p[y] = press + 1; + } + } + if (v[U]) + { + break; + } + } + if (v[U]) + { + printf("%d\n", p[U]); + } + else + { + printf("Permanently Locked\n"); + } + } + return 0; +} diff --git a/12161.cpp b/12161.cpp new file mode 100644 index 0000000..8c70dea --- /dev/null +++ b/12161.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +const int N = 30005; + +struct edge {int x, w, l, next;} e[N << 1]; + +bool f[N]; +int n, m, pre[N], mcnt, s[N], ans, ca; +set> hash_; +set>::iterator it; +vector> Vec; + +pair findcenter(int x, int fa, int size) +{ + s[x] = 1; + pair res = make_pair(1 << 30, -1); + int mx = 0; + for (int i = pre[x]; ~i; i = e[i].next) + { + int y = e[i].x; + if (!f[y] && y != fa) + { + res = min(res, findcenter(y, x, size)); + mx = max(mx, s[y]), s[x] += s[y]; + } + } + return min(res, make_pair(max(mx, size - s[x]), x)); +} + +void Getdis(int x, int fa, int W, int L) +{ + Vec.push_back(make_pair(W, L)); + if (W < m) + { + it = --hash_.upper_bound(make_pair(m - W, 1 << 30)); + ans = max(ans, L + it->second); + } + for (int i = pre[x]; ~i; i = e[i].next) + { + int y = e[i].x; + if (!f[y] && y != fa) + { + Getdis(y, x, W + e[i].w, L + e[i].l); + } + } +} + +void divide(int x, int size) +{ + x = findcenter(x, 0, size).second; + f[x] = 1; + hash_.insert(make_pair(0, 0)); + for (int i = pre[x]; ~i; i = e[i].next) + { + int y = e[i].x; + if (!f[y]) + { + Getdis(y, x, e[i].w, e[i].l); + while (!Vec.empty()) + { + pair A = Vec.back(); + Vec.pop_back(); + it = --hash_.upper_bound(A); + if (it->second >= A.second) + { + continue; + } + hash_.insert(A); + do + { + it = hash_.upper_bound(A); + if (it != hash_.end() && A.second >= it->second) + { + hash_.erase(it); + } + else + { + break; + } + } while (1); + } + } + } + hash_.clear(); + for (int i = pre[x]; ~i; i = e[i].next) + { + int y = e[i].x; + if (!f[y]) + { + divide(y, s[y]); + } + } +} + +void work() +{ + int i, j, k, x, y; + scanf("%d%d", &n, &m); + memset(pre, -1, sizeof(pre)), mcnt = 0; + for (i = 1; i < n; ++i) + { + scanf("%d%d%d%d", &x, &y, &j, &k); + e[mcnt] = (edge){y, j, k, pre[x]}, pre[x] = mcnt++; + e[mcnt] = (edge){x, j, k, pre[y]}, pre[y] = mcnt++; + } + memset(f, 0, sizeof(f)); + ans = 0; + divide(1, n); + printf("Case %d: %d\n", ++ca, ans); +} + +int main() +{ + int _; + scanf("%d", &_); + while (_--) + { + work(); + } + return 0; +} diff --git a/12163.cpp b/12163.cpp new file mode 100644 index 0000000..4c16436 --- /dev/null +++ b/12163.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define N 105 + +typedef long long LL; + +bool mex[(1 << 15) + 10]; +int n, m, pre[N], mcnt, ca, cnt[1 << 15], p[1 << 15], lg[1 << 15], SG[N], deg[N], K[N], a[N]; +set hash_; +vector det[N]; + +struct edge {int x, next;} e[N * 30]; + +void work() +{ + int i, j, k, x, y; + scanf("%d%d", &n, &m); + printf("Game#%d:\n", ++ca); + memset(pre, -1, sizeof(pre)); + memset(deg, 0, sizeof(deg)); + for (i = 0; i < n; ++i) + { + det[i].clear(); + } + mcnt = 0; + while (m--) + { + scanf("%d%d", &x, &y); + det[x].push_back(y); + e[mcnt] = (edge){x, pre[y]}, pre[y] = mcnt++; + ++deg[x]; + } + for (i = 0; i < n; ++i) + { + scanf("%d", &K[i]); + } + queue Q; + for (i = 0; i < n; ++i) + if (!deg[i]) + { + Q.push(i); + } + while (!Q.empty()) + { + x = Q.front(), Q.pop(), SG[x] = 0; + k = det[x].size(), hash_.clear(); + if (k) + { + memset(p, 0, sizeof(p)); + memset(mex, 0, sizeof(mex)); + for (i = 1; i < 1 << k; ++i) + { + p[i] = p[i & (i - 1)] ^ SG[det[x][lg[i & -i]]]; + } + for (i = 0; i < 1 << k; ++i) + if ((cnt[i] & 1) == (K[x] & 1) && cnt[i] <= K[x]) + { + mex[p[i]] = 1; + } + while (mex[SG[x]]) + { + ++SG[x]; + } + } + for (i = pre[x]; ~i; i = e[i].next) + { + y = e[i].x; + if (!--deg[y]) + { + Q.push(y); + } + } + } + scanf("%d", &m); + for (j = 1; j <= m; ++j) + { + y = 0; + for (i = 0; i < n; ++i) + { + scanf("%d", &x), y ^= SG[i] * (x & 1); + } + printf("Round#%d: %s\n", j, y ? "WINNING" : "LOSING"); + } + puts(""); +} + +int main() +{ + for (int i = 0; i < 15; ++i) + { + lg[1 << i] = i; + } + for (int i = 1; i < 1 << 15; ++i) + { + cnt[i] = cnt[i & (i - 1)] + 1; + } + int _; + scanf("%d", &_); + while (_--) + { + work(); + } + return 0; +} diff --git a/12165.cpp b/12165.cpp new file mode 100644 index 0000000..78aa9cb --- /dev/null +++ b/12165.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +struct point +{ + double x, y; +} a, b, c, p, q, r; + +int mm[10], tot; +double m[10], k[10]; + +int main() +{ + scanf("%d", &tot); + while (tot--) + { + scanf("%lf%lf", &p.x, &p.y); + scanf("%lf%lf", &q.x, &q.y); + scanf("%lf%lf", &r.x, &r.y); + for (int i = 1; i <= 6; ++i) + { + scanf("%d", &mm[i]); + } + m[1] = mm[1] * 1.0 / (mm[1] + mm[2]); + m[2] = mm[2] * 1.0 / (mm[1] + mm[2]); + m[3] = mm[3] * 1.0 / (mm[3] + mm[4]); + m[4] = mm[4] * 1.0 / (mm[3] + mm[4]); + m[5] = mm[5] * 1.0 / (mm[5] + mm[6]); + m[6] = mm[6] * 1.0 / (mm[5] + mm[6]); + k[1] = m[1] / (m[1] + m[2] * m[4]); + k[2] = m[6] / (m[6] + m[5] * m[3]); + k[3] = m[3] / (m[3] + m[4] * m[6]); + k[4] = m[2] / (m[2] + m[1] * m[5]); + k[5] = m[5] / (m[5] + m[6] * m[2]); + k[6] = m[4] / (m[4] + m[3] * m[1]); + b.x = (k[2] * p.x - k[1] * q.x) / (k[2] - k[1]); + c.x = (k[4] * q.x - k[3] * r.x) / (k[4] - k[3]); + a.x = (k[6] * r.x - k[5] * p.x) / (k[6] - k[5]); + b.y = (k[2] * p.y - k[1] * q.y) / (k[2] - k[1]); + c.y = (k[4] * q.y - k[3] * r.y) / (k[4] - k[3]); + a.y = (k[6] * r.y - k[5] * p.y) / (k[6] - k[5]); + printf("%.8lf %.8lf %.8lf %.8lf %.8lf %.8lf\n", a.x, a.y, b.x, b.y, c.x, c.y); + } + return 0; +} diff --git a/12166.cpp b/12166.cpp new file mode 100644 index 0000000..1dc4794 --- /dev/null +++ b/12166.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +/* NWERC 2008: Sample solution to "Equilibrium Mobile" + * Author: Per Austrin + * + * Algorithm: greedyish + */ + +typedef long long ll; + +char inp[1 << 25]; +int pos, tok; +int next() +{ + if (!isdigit(inp[pos])) + { + tok = -inp[pos++]; + } + else + for (tok = 0; isdigit(inp[pos]); tok = 10 * tok + inp[pos++] - '0') + ; + return tok; +} + +ll W[1 << 21]; +int N; + +void readmob(int m) +{ + if ((W[N] = next()) > 0) + { + W[N++] <<= m; + } + else + { + readmob(m + 1), next(), readmob(m + 1), next(); + } +} + +void readmob2(int m) +{ + char str[25]; + scanf("%s", str); + if (*str == 'W') + { + scanf("%lld", W + N), W[N++] <<= m; + } + else + { + readmob2(m + 1), readmob2(m + 1); + } +} + +void solve() +{ + scanf("%s", inp); + assert(strlen(inp) < (1 << 25)); + pos = tok = N = 0; + readmob(0); + sort(W, W + N); + int res = 1; + for (int i = 0, j; i < N; res = max(res, j - i), i = j) + for (j = i + 1; j < N && W[i] == W[j]; ++j) + ; + printf("%d\n", N - res); +} + +int main() +{ + int N; + for (scanf("%d", &N); N--; solve()) + ; + return 0; +} diff --git a/12167.cpp b/12167.cpp new file mode 100644 index 0000000..4820d02 --- /dev/null +++ b/12167.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +/* + [NWERC'08] EQUIVALENCES + by: Jan Kuipers +*/ + +int N, M; +vector u, s; +vector> c1, c2; + +void dfs1(int n) +{ + if (u[n]) + { + return; + } + u[n] = -1; + for (int i = 0; i < (int)c1[n].size(); i++) + { + dfs1(c1[n][i]); + } + s.push_back(n); +} + +void dfs2(int n, int mark) +{ + if (u[n] != -1) + { + return; + } + u[n] = mark; + for (int i = 0; i < (int)c2[n].size(); i++) + { + dfs2(c2[n][i], mark); + } +} + +int main() +{ + int runs; + cin >> runs; + while (runs--) + { + cin >> N >> M; + c1 = c2 = vector>(N); + for (int i = 0; i < M; i++) + { + int a, b; + cin >> a >> b; + a--; + b--; + c1[a].push_back(b); + c2[b].push_back(a); + } + s.clear(); + int mark = 0; + u = vector(N, 0); + for (int i = 0; i < N; i++) + { + dfs1(i); + } + for (int i = N - 1; i >= 0; i--) + if (u[s[i]] == -1) + { + dfs2(s[i], mark++); + } + if (mark == 1) + { + cout << 0 << endl; + continue; + } + vector nin(mark, 0), nout(mark, 0); + for (int i = 0; i < N; i++) + for (int j = 0; j < (int)c1[i].size(); j++) + if (u[i] != u[c1[i][j]]) + { + nout[u[i]]++; + nin[u[c1[i][j]]]++; + } + int nin0 = 0, nout0 = 0; + for (int i = 0; i < mark; i++) + { + if (nin[i] == 0) + { + nin0++; + } + if (nout[i] == 0) + { + nout0++; + } + } + cout << max(nin0, nout0) << endl; + } + return 0; +} diff --git a/12168.cpp b/12168.cpp new file mode 100644 index 0000000..36aa131 --- /dev/null +++ b/12168.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +//Graphs;Bipartite Matching;Konig Theorem + +#define MAX 505 + +string V1[MAX], V2[MAX]; +int G[MAX][MAX], n; +bool V[MAX]; + +int send(int s, int t, int minn) +{ + V[s] = true; + if (s == t) + { + return minn; + } + for (int i = 0; i <= n; i++) + { + if (!V[i] && G[s][i] > 0) + { + if (int sent = send(i, t, min(minn, G[s][i]))) + { + G[s][i] -= sent; + G[i][s] += sent; + return sent; + } + } + } + return 0; +} + +int main() +{ + int t; + cin >> t; + int c, d, v; + while (cin >> c >> d >> v, t--) + { + memset(G, 0, sizeof(G)); + memset(V, 0, sizeof(V)); + string s1, s2; + for (int i = 1; i <= v; i++) + { + cin >> s1 >> s2; + V1[i] = s1; + V2[i] = s2; + bool dog = s1[0] == 'D'; + if (dog) + { + G[0][i] = 1; + } + else + { + G[i][v + 1] = 1; + } + for (int j = 1; j < i; j++) + { + if (s1 == V2[j] || s2 == V1[j]) + if (dog) + { + G[i][j] = 1; + } + else + { + G[j][i] = 1; + } + } + } + n = v + 1; + int total = 0; + while (int sent = send(0, n, INT_MAX)) + { + total += sent; + memset(V, 0, sizeof(V)); + } + cout << v - total << endl; + } + return 0; +} diff --git a/12169.cpp b/12169.cpp new file mode 100644 index 0000000..2766fb0 --- /dev/null +++ b/12169.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int p = 73; +const int q = 137; +const int M = p * q; + +int mod(int a) +{ + return (a % M + M) % M; +} +int pow(int a, int b) +{ + int res = 1; + while (b) + { + if (b & 1) + { + res = mod(res * a); + } + a = mod(a * a); + b >>= 1; + } + return res; +} +int inv(int a) +{ + return pow(a, M - p - q); +} +int main() +{ + int N; + cin >> N; + vector x(N); + for (int i = 0; i < N; i++) + { + cin >> x[i]; + } + for (int a = 0; a < M; a++) + { + int b = mod(x[1] - mod(a * a) * x[0]) * inv(a + 1); + int y = x[0], ok = 1; + for (int i = 0; i < N && ok; i++) + { + if (y != x[i]) + { + ok = 0; + break; + } + y = (a * y + b) % M; + y = (a * y + b) % M; + } + if (ok) + { + for (int i = 0; i < N; i++) + { + cout << (a * x[i] + b) % M << endl; + } + break; + } + } + return 0; +} diff --git a/1217.cpp b/1217.cpp new file mode 100644 index 0000000..801cf1f --- /dev/null +++ b/1217.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int a[105][105], dp[1 << 20][20], N, sc[20], c; +char s[10005]; + +int main() +{ + int i, j, k, n, M; + scanf(" %d %d", &N, &M); + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + scanf(" %d", &a[i][j]); + } + gets(s); + while (M--) + { + gets(s); + char *ptr = strtok(s, " "); + c = 0; + do + { + sscanf(ptr, " %d", &sc[c]); + sc[c]--; + c++; + } while (ptr = strtok(NULL, " ")); + if (c == 2) + { + printf("%d\n", a[sc[0]][sc[1]]); + continue; + } + int lim = 1 << (c - 1); + for (i = 0; i < lim; i++) + for (j = 0; j < c; j++) + { + dp[i][j] = 1e9; + } + dp[1][0] = 0; + for (i = 1; i < lim; i++) + { + for (j = 0; j < c - 1; j++) + { + if (dp[i][j] == 1e9) + { + continue; + } + for (k = 0; k < c - 1; k++) + { + if (i & (1 << k)) + { + continue; + } + if (a[sc[j]][sc[k]] != 0 && dp[i][j] + a[sc[j]][sc[k]] < dp[i | (1 << k)][k]) + { + dp[i | (1 << k)][k] = dp[i][j] + a[sc[j]][sc[k]]; + } + } + } + } + /*for(i=0;i + +using namespace std; + +// DP + +typedef long long ll; + +const ll INF = 1LL << 50; + +int N; +ll H[150], D; +map best[150]; + +ll Greedy(int a, int b, ll ha, ll hb) +{ + ll lo = hb - D * (b - a - 1), hi = hb + D * (b - a - 1); + ll at = ha, ret = 0, nat; + for (int x = a + 1; x != b; ++x, at = nat, lo += D, hi -= D) + { + nat = min(min(max(max(lo, at - D), H[x]), at + D), hi); + ret += abs(H[x] - nat); + } + return ret; +} + +ll Best(int l, ll hl) +{ + if (l == N - 1) + { + return 1; + } + ll &ret = best[l][hl]; + if (!ret) + { + ret = INF; + for (int m = l + 1; m < N; ++m) + { + ll nh = min(min(max(max(hl - D * (m - l), H[N - 1] - D * (N - 1 - m)), H[m]), H[N - 1] + D * (N - 1 - m)), hl + D * (m - l)); + ll result = abs(nh - H[m]) + Greedy(l, m, hl, nh) + Best(m, nh); + ret = min(ret, result); + } + } + return ret; +} + +void solve() +{ + scanf("%d%lld", &N, &D); + for (int i = 0; i < N; ++i) + { + scanf("%lld", H + i), best[i].clear(); + } + if (abs(H[0] - H[N - 1]) > D * (N - 1)) + { + printf("impossible\n"); + } + else + { + printf("%lld\n", Best(0, H[0]) - 1); + } +} + +int main() +{ + int N; + for (scanf("%d", &N); N--; solve()) + ; + return 0; +} diff --git a/12171.cpp b/12171.cpp new file mode 100644 index 0000000..93b7052 --- /dev/null +++ b/12171.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +// coordinate compression + floodfill +// complexity O(n^4), can easily be turned into O(n^3) + +typedef long long ll; +typedef vector vi; + +const int dx[] = {-1, 0, 0, 1, 0, 0}; +const int dy[] = {0, -1, 0, 0, 1, 0}; +const int dz[] = {0, 0, -1, 0, 0, 1}; + +int compress(int n, int *x, int *v) +{ + memcpy(v, x, sizeof(int) * n); + v[n] = -1; + sort(v, v + n + 1); + int m = unique(v, v + n + 1) - v; + for (int i = 0; i < n; ++i) + for (int j = 0; j < m; ++j) + if (x[i] == v[j]) + { + x[i] = j; + break; + } + return m; +} + +int occ[110][110][110]; + +void solve() +{ + int N; + scanf("%d", &N); + int X[150], Y[150], Z[150]; + int xv[150], yv[150], zv[150]; + for (int i = 0; i < N; ++i) + { + scanf("%d%d%d%d%d%d", X + 2 * i, Y + 2 * i, Z + 2 * i, X + 2 * i + 1, Y + 2 * i + 1, Z + 2 * i + 1); + X[2 * i + 1] += X[2 * i]; + Y[2 * i + 1] += Y[2 * i]; + Z[2 * i + 1] += Z[2 * i]; + } + int xn = compress(2 * N, X, xv); + int yn = compress(2 * N, Y, yv); + int zn = compress(2 * N, Z, zv); + memset(occ, 0, sizeof(occ)); + for (int i = 0; i < N; ++i) + for (int x = X[2 * i]; x < X[2 * i + 1]; ++x) + for (int y = Y[2 * i]; y < Y[2 * i + 1]; ++y) + for (int z = Z[2 * i]; z < Z[2 * i + 1]; ++z) + { + occ[x][y][z] = true; + } + vi Q; + Q.push_back(0); + occ[0][0][0] = -1; + while (!Q.empty()) + { + int v = Q.back(); + Q.pop_back(); + int x = v & 0xFF, y = (v >> 8) & 0xFF, z = (v >> 16) & 0xFF; + for (int d = 0; d < 6; ++d) + { + int nx = x + dx[d], ny = y + dy[d], nz = z + dz[d]; + if (nx >= 0 && ny >= 0 && nz >= 0 && nx < xn && ny < yn && nz < zn && + !occ[nx][ny][nz]) + { + occ[nx][ny][nz] = -1; + Q.push_back((nz << 16) | (ny << 8) | nx); + } + } + } + ll vol = 0, area = 0; + for (int x = 1; x < xn - 1; ++x) + for (int y = 1; y < yn - 1; ++y) + for (int z = 1; z < zn - 1; ++z) + { + int sx = xv[x + 1] - xv[x], sy = yv[y + 1] - yv[y], sz = zv[z + 1] - zv[z]; + if (occ[x][y][z] != -1) + { + vol += (ll)sx * sy * sz; + } + for (int d = 0; d < 6; ++d) + if (occ[x][y][z] != -1 && occ[x + dx[d]][y + dy[d]][z + dz[d]] == -1) + { + area += dx[d] ? sy * sz : dy[d] ? sx * sz : sx * sy; + } + } + printf("%lld %lld\n", area, vol); +} + +int main() +{ + int N; + for (scanf("%d", &N); N--; solve()) + ; + return 0; +} diff --git a/12172.cpp b/12172.cpp new file mode 100644 index 0000000..6e71725 --- /dev/null +++ b/12172.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +//Greedy + +#define MAX 101 + +void printMax(int n) +{ + if (n & 1) + { + cout << "7"; + n -= 3; + } + for (; n; n -= 2) + { + cout << "1"; + } +} + +void printMin(int n) +{ + switch (n) + { + case 2: + cout << "1"; + return; + case 3: + cout << "7"; + return; + case 4: + cout << "4"; + return; + case 5: + cout << "2"; + return; + case 6: + cout << "6"; + return; + } + switch (n % 7) + { + case 1: + cout << "10"; + n -= 8; + break; + case 2: + cout << "1"; + n -= 2; + break; + case 3: + if (n == 10) + { + cout << "22"; + n -= 10; + } + else + { + cout << "200"; + n -= 17; + } + break; + case 4: + cout << "20"; + n -= 11; + break; + case 5: + cout << "2"; + n -= 5; + break; + case 6: + cout << "6"; + n -= 6; + break; + } + for (; n; n -= 7) + { + cout << "8"; + } +} + +int main() +{ + int n, t; + cin >> t; + t = 0; + while (cin >> n) + { + printMin(n); + cout << " "; + printMax(n); + cout << endl; + } + return 0; +} diff --git a/12173.cpp b/12173.cpp new file mode 100644 index 0000000..da35ee9 --- /dev/null +++ b/12173.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +struct P +{ + double x, y; + P(double x = 0, double y = 0) : x(x), y(y) {} + P operator-(P p) const + { + return P(x - p.x, y - p.y); + } + P operator*(double f) const + { + return P(f * x, f * y); + } + double dot(const P &p) + { + return x * p.x + y * p.y; + } + double dist2() + { + return dot(*this); + } +}; + +double d(P p0, P p1, P q) +{ + double res = (p1 - p0).dot(q - p0) / (p1 - p0).dist2(); + if (res < 0.0) + { + res = 0.0; + } + double t = min(res, 1.0); + return sqrt((p0 * (1 - t) - p1 * -t - q).dist2()); +} + +double d(P p0, P p1, P q0, P q1) +{ + return min(min(min(d(p0, p1, q0), d(p0, p1, q1)), d(q0, q1, p0)), d(q0, q1, p1)); +} + +void rp(int &N, P *p) +{ + scanf("%d", &N); + for (int i = 0; i < N; ++i) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + } +} + +void solve() +{ + int N1, N2; + P P1[200], P2[200]; + rp(N1, P1); + rp(N2, P2); + double res = 1e30; + for (int i1 = 0, j1 = N1 - 1; i1 < N1; j1 = i1++) + for (int i2 = 0, j2 = N2 - 1; i2 < N2; j2 = i2++) + { + res = min(res, d(P1[i1], P1[j1], P2[i2], P2[j2])); + } + printf("%.9lf\n", res / 2); +} + +int main() +{ + int N; + for (scanf("%d", &N); N--; solve()) + ; + return 0; +} diff --git a/12174.cpp b/12174.cpp new file mode 100644 index 0000000..c5d6027 --- /dev/null +++ b/12174.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +// interval union + +typedef pair pii; + +int S, N, last[200000]; + +void solve() +{ + scanf("%d%d", &S, &N); + memset(last, -1, sizeof(last)); + vector excl; + for (int i = 0, s, x, y; i < N && scanf("%d", &s); last[s] = i++) + if (last[s] != -1 && i - last[s] < S) + { + if ((x = last[s] % S) > (y = i % S)) + { + excl.push_back(pii(y, x)); + } + else + { + excl.push_back(pii(0, x)), excl.push_back(pii(y, S)); + } + } + sort(excl.begin(), excl.end()); + int at = 0, res = 0; + for (int i = 0; i < (int)excl.size(); at = max(at, excl[i++].second)) + { + res += max((excl[i].first - at), 0); + } + printf("%d\n", res + S - at); +} + +int main() +{ + int N; + for (scanf("%d", &N); N--; solve()) + ; + return 0; +} diff --git a/12175.cpp b/12175.cpp new file mode 100644 index 0000000..31cb464 --- /dev/null +++ b/12175.cpp @@ -0,0 +1,235 @@ +#include + +using namespace std; + +#define MP make_pair +#define PB push_back +#define SZ(v) ((int)(v).size()) +#define FOR(i, a, b) for (int i = (a); i < (b); ++i) +#define REP(i, n) FOR(i, 0, n) +#define REPSZ(i, v) REP(i, SZ(v)) +#define ALL(v) (v).begin(), (v).end() +#define SORT(v) sort(ALL(v)) + +typedef long long ll; +typedef pair PII; +typedef vector VI; +typedef vector VPII; + +const int aNONE = 0; +const int aONEPAIR = 1; +const int aTWOPAIR = 2; +const int aTHREEKIND = 3; +const int aSTRAIGHT = 4; +const int aFLUSH = 5; +const int aFULLHOUSE = 6; +const int aFOURKIND = 7; +const int aSTRAIGHTFLUSH = 8; +const int aROYALFLUSH = 9; +const int bNONE = 1; +const int bSTRAIGHT = 2; +const int bROYALSTRAIGHT = 3; +const int bONEPAIR = 4; +const int bTWOPAIR = 5; +const int bTHREEKIND = 6; +const int bFULLHOUSE = 7; +const int bFOURKIND = 8; + +int pay[10], nq, startrank[5], startsuit[5]; +int memcalc[14][14][14][14][14][9]; +int mem[14][14][14][14][9]; + +int *go(int runnr, VI ranks, VI &rankleft) +{ + SORT(ranks); + if (SZ(ranks) < 5) + { + int *ret = mem[SZ(ranks) >= 1 ? ranks[0] : 13][SZ(ranks) >= 2 ? ranks[1] : 13][SZ(ranks) >= 3 ? ranks[2] : 13][SZ(ranks) >= 4 ? ranks[3] : 13]; + if (ret[0] != runnr) + { + ret[0] = runnr; + FOR(i, 1, 9) + ret[i] = 0; + REPSZ(i, rankleft) + if (rankleft[i] > 0) + { + rankleft[i]--; + ranks.PB(i); + int *cur = go(runnr, ranks, rankleft); + rankleft[i]++; + ranks.pop_back(); + FOR(j, 1, 9) + ret[j] += rankleft[i] * cur[j]; + } + } + return ret; + } + else + { + int *ret = memcalc[SZ(ranks) >= 1 ? ranks[0] : 13][SZ(ranks) >= 2 ? ranks[1] : 13][SZ(ranks) >= 3 ? ranks[2] : 13][SZ(ranks) >= 4 ? ranks[3] : 13][SZ(ranks) >= 5 ? ranks[4] : 13]; + if (ret[0] == 0) + { + ret[0] = 1; + FOR(i, 1, 9) + ret[i] = 0; + VPII cnt; + REP(i, 5) + if (SZ(cnt) == 0 || ranks[i] != cnt.back().second) + { + cnt.PB(MP(1, ranks[i])); + } + else + { + cnt.back().first++; + } + SORT(cnt); + reverse(ALL(cnt)); + switch (cnt[0].first) + { + case 1: + ret[ranks[0] == 0 && ranks[1] == 9 ? bROYALSTRAIGHT : ranks[4] == ranks[0] + 4 ? bSTRAIGHT : bNONE]++; + break; + case 2: + ret[cnt[1].first == 2 ? bTWOPAIR : bONEPAIR]++; + break; + case 3: + ret[cnt[1].first == 2 ? bFULLHOUSE : bTHREEKIND]++; + break; + case 4: + ret[bFOURKIND]++; + break; + } + } + return ret; + /* printf("gone:");REPSZ(i,ranks) printf(" %d",ranks[i]); + printf(" (left "); REPSZ(i,rankleft) printf("%d%c",rankleft[i],i==SZ(rankleft)-1?')':','); + printf(" =>"); FOR(i,1,9) printf(" %d",ret[i]); + puts(""); */ + } +} +int readRank() +{ + char c; + scanf(" %c", &c); + return string("A23456789TJQK").find(c); +} +int readSuit() +{ + char c; + scanf(" %c", &c); + return string("cdhs").find(c); +} +int runnr; +void run() +{ + pay[0] = 0; + FOR(i, 1, 10) + scanf("%d", &pay[i]); + scanf("%d", &nq); + REP(q, nq) + { + REP(i, 5) + { + startrank[i] = readRank(); + startsuit[i] = readSuit(); + } + double ret = 0; + REP(keep, 1 << 5) + { + VI res(10, 0); + FOR(suit, -1, 4) + { + VI ranks(0), rankleft(13, suit == -1 ? 4 : 1); + bool ok = true; + REP(i, 5) + if (keep & (1 << i)) + { + if (suit != -1 && startsuit[i] != suit) + { + ok = false; + break; + } + else + { + ranks.PB(startrank[i]); + } + } + if (!ok) + { + continue; + } + REP(i, 5) + if (suit == -1 || startsuit[i] == suit) + { + --rankleft[startrank[i]]; + } + int *cnt = go(runnr++, ranks, rankleft); + // printf("\t\t%x,%d:",keep,suit); FOR(i,1,9) printf(" %d",cnt[i]); puts(""); + FOR(i, 1, 9) + if (cnt[i] > 0) switch (i) + { + case bNONE: + res[aNONE] += (suit != -1 ? -1 : +1) * cnt[i]; + if (suit != -1) + { + res[aFLUSH] += cnt[i]; + } + break; + case bSTRAIGHT: + res[aSTRAIGHT] += (suit != -1 ? -1 : +1) * cnt[i]; + if (suit != -1) + { + res[aSTRAIGHTFLUSH] += cnt[i]; + } + break; + case bROYALSTRAIGHT: + res[aSTRAIGHT] += (suit != -1 ? -1 : +1) * cnt[i]; + if (suit != -1) + { + res[aROYALFLUSH] += cnt[i]; + } + break; + case bONEPAIR: + assert(suit == -1); + res[aONEPAIR] += cnt[i]; + break; + case bTWOPAIR: + assert(suit == -1); + res[aTWOPAIR] += cnt[i]; + break; + case bTHREEKIND: + assert(suit == -1); + res[aTHREEKIND] += cnt[i]; + break; + case bFULLHOUSE: + assert(suit == -1); + res[aFULLHOUSE] += cnt[i]; + break; + case bFOURKIND: + assert(suit == -1); + res[aFOURKIND] += cnt[i]; + break; + } + } + ll num = 0, den = 0; + REP(i, 10) + num += ((ll)pay[i]) * res[i], + den += res[i]; + // printf("\t %x => %lf (%lld/%lld)",keep,1.0*num/den,num,den); puts(""); //REP(i,10) printf(" %d",res[i]); puts(""); + ret = max(ret, 1.0 * num / den); + } + printf("%.14lf\n", ret); + } +} + +int main() +{ + memset(memcalc, 0, sizeof(memcalc)); + memset(mem, -1, sizeof(mem)); + runnr = 0; + int n; + scanf("%d", &n); + REP(i, n) + run(); + return 0; +} diff --git a/12176.cpp b/12176.cpp new file mode 100644 index 0000000..3217dae --- /dev/null +++ b/12176.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +// method : MST (Kruskal's), disjoint sets (Union-Find), depth-first search (DFS) +// runtime: O(E*log(V) + Q*V), V = # of nodes, E = # of edges, Q = # of queries + +typedef pair ii; +typedef vector vii; + +// MAXN = max number of places, MAXR = max number of roads +const int MAXN = 3000, MAXR = 100000; + +// V = number of vertices, E = number of edges +// fr[i] = starting point of edge i, to[i] = end point of edge i, len[i] = length of edge i +// ord[i] = index of the i-th shortest edge +// up[i] = parent of node i in Union-Find +// adj[i] = vector of minimum spanning tree edges incident to node i +// adj[i][j].first = adjacent node, adj[i][j].second = length +int V, E; +int fr[MAXR], to[MAXR], len[MAXR]; +int ord[MAXR]; +int up[MAXN]; +vii adj[MAXN]; + +// only used for sorting edges by length +bool cmp(int a, int b) +{ + return len[a] < len[b]; +} + +// the "find" part of Union-Find, with path compression +int root(int k) +{ + return up[k] < 0 ? k : (up[k] = root(up[k])); +} + +// the "union" part of Union-Find, with weighted union +bool join(int k, int m) +{ + if ((k = root(k)) == (m = root(m))) + { + return false; + } + if (up[k] < up[m]) + { + up[k] += up[m]; + up[m] = k; + } + else + { + up[m] += up[k]; + up[k] = m; + } + return true; +} + +// ordinary implementation of Kruskal's algorithm +void kruskal() +{ + sort(ord, ord + E, cmp); + memset(up, -1, sizeof up); + for (int i = 0; i < V; ++i) + { + adj[i].clear(); + } + for (int i = V - 1, j = 0; i > 0; ++j) + { + int a = fr[ord[j]], b = to[ord[j]]; + if (join(a, b)) + { + // store a chosen edge for later depth-first search + adj[a].push_back(ii(b, len[ord[j]])); + adj[b].push_back(ii(a, len[ord[j]])); + --i; + } + } +} + +// depth-first search inside the minimum spanning tree +// return longest edge on unique path between s and t +// u is the node where we come from (given to avoid going back) +int dfs(int s, int t, int u) +{ + if (s == t) + { + return 0; + } + for (vii::iterator it = adj[s].begin(); it != adj[s].end(); ++it) + { + int re; + if (it->first != u && (re = dfs(it->first, t, s)) >= 0) + { + return max(re, it->second); + } + } + return -1; +} + +int main() +{ + int kases; + cin >> kases; + for (int kase = 1; kase <= kases; ++kase) + { + cin >> V >> E; + for (int i = 0; i < E; ++i) + { + cin >> fr[i] >> to[i] >> len[i]; + --fr[i]; + --to[i]; + ord[i] = i; + } + // compute an arbitrary minimum spanning tree + kruskal(); + cout << "Case " << kase << endl; + int k, a, b; + for (cin >> k; k > 0; --k) + { + cin >> a >> b; + // find longest edge by depth-first searching in that tree + cout << dfs(--a, --b, -1) << endl; + } + cout << endl; + } + return 0; +} diff --git a/12177.cpp b/12177.cpp new file mode 100644 index 0000000..9cb964f --- /dev/null +++ b/12177.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +// method : linear equations, Gaussian elimination for band-limited matrices +// runtime: O(m*n^3) + +// move from (i,j) to (i+di[k-1],j+dj[k-1]) +const int di[4] = {1, 0, -1, 0}; +const int dj[4] = {0, 1, 0, -1}; + +const int maxn = 64; +const int maxv = maxn * maxn; + +// equation system is matrix * unknowns = vec +// band[i][j] is matrix[i][i-n+j]; rest of matrix is 0 +double band[maxv][2 * maxn + 1]; +double vec[maxv]; + +int main() +{ + cout.setf(ios::fixed); + cout.precision(16); + int m, n; + while (cin >> m >> n) + { + if (m == 0 || n == 0) + { + break; + } + // matrix := 1; vec := (0,1,...,1) + for (int i = 0; i < m * n; i++) + for (int j = 0; j < 2 * n + 1; j++) + { + band[i][j] = 0.0; + } + for (int i = 0; i < m * n; i++) + { + band[i][n] = vec[i] = 1.0; + } + vec[0] = 0.0; + // enter probabilities into matrix + for (int k = 0; k < 4; k++) + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + { + double p; + cin >> p; + // moving out of the area? + if (0 <= i + di[k] && i + di[k] < m && + 0 <= j + dj[k] && j + dj[k] < n) + { + // there are m*m blocks of size n*n + // index i*n+j is row #j of block #i + // columns addressed relative to diagonal at index n + int i1 = (n * m - 1) - (i * n + j); + int j1 = (n * m - 1) - ((i + di[k]) * n + (j + dj[k])); + // reverse rows and columns of matrix + band[i1][j1 + n - i1] = -p; + } + } + // forward elimination + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + { + // pivot is on the diagonal + double p = band[i * n + j][n]; + // normalise the current row + vec[i * n + j] /= p; + for (int q = 0; q <= n && i * n + j + q < m * n; q++) + { + band[i * n + j][n + q] /= p; + } + // eliminate first columns of next n rows + for (int r = 1; r <= n && i * n + j + r < m * n; r++) + // p = the first entry of row r below the current one + if ((p = band[i * n + j + r][n - r]) != 0.0) + { + // row operation + vec[i * n + j + r] -= p * vec[i * n + j]; + for (int q = 0; q <= n && i * n + j + q < m * n; q++) + { + band[i * n + j + r][n - r + q] -= p * band[i * n + j][n + q]; + } + } + } + // solution is in last row of vec + cout << vec[m * n - 1] << endl; + } + return 0; +} diff --git a/12178.cpp b/12178.cpp new file mode 100644 index 0000000..e6dc5ba --- /dev/null +++ b/12178.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +// O(n + MAX^4) + +const int MAX = 10;// maximum number of quarters in one direction + +int nr[MAX][MAX]; // number of houses in each quarter +double ll[MAX][MAX];// sum of the distances of all houses in a quarter to its lower left corner + +double doit() +{ + double sum = 0.0;// total length of all admissible paths + long long n2 = 0;// total number of admissible paths, must be > 32 bits + // iterate over all pairs of quarters + for (int i = 1; i < MAX; ++i) + { + for (int j = 1; j < MAX; ++j) + { + for (int k = 0; k < i; ++k) + { + for (int l = 0; l < j; ++l) + { + // compute the total length of all admissible paths + // between quarters i,j and k,l in two steps: + // first, the parts which run inside each quarter + sum += ll[i][j] * nr[k][l] - ll[k][l] * nr[i][j]; + // then, the parts which run between the two quarters + sum += (double)(i - k + j - l) * nr[i][j] * nr[k][l]; + // finally, update the number of admissible paths + n2 += (long long)nr[i][j] * nr[k][l]; + } + } + } + } + return sum / n2; +} + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + memset(nr, 0, sizeof nr); + memset(ll, 0, sizeof ll); + while (n-- > 0) + { + double x, y; + scanf("%lf%lf", &x, &y); + int u = floor(x), v = floor(y); + ++nr[u][v]; + // only save the Manhattan distance to lower left corner of quarter: + ll[u][v] += x - u + y - v; + } + printf("%.8f\n", doit()); + } + return 0; +} diff --git a/12179.cpp b/12179.cpp new file mode 100644 index 0000000..25aa1b8 --- /dev/null +++ b/12179.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +//Graphs;Shortest Path;Floyd-Warshall + +#define MAX 101 + +int G[MAX][MAX], n, r, c; +double P[101][10001]; + +int main() +{ + int t; + cin >> t; + t = 0; + cout << fixed << setprecision(6); + while (cin >> n >> r) + { + memset(G, 0x3F, sizeof(G)); + memset(P, 0, sizeof(P)); + char cc; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> cc; + if (cc == 'Y') + { + G[i][j] = 1; + } + } + } + for (int k = 0; k < n; k++) + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + G[i][j] = min(G[i][j], G[i][k] + G[k][j]); + } + P[0][0] = 1; + double pp = 1.0 / r; + for (int i = 1; i <= 100; i++) + for (int k = 1; k <= r; k++) + for (int j = k; j <= 100 * r; j++) + { + P[i][j] += P[i - 1][j - k] * pp; + } + cout << "Case " << ++t << endl; + cin >> c; + while (c--) + { + int a, b, m; + cin >> a >> b >> m; + a--; + b--; + int d = G[a][b]; + double total = 0; + for (int i = 0; i <= m; i++) + { + total += P[d][i]; + } + cout << total << endl; + } + cout << endl; + } + return 0; +} diff --git a/1218.cpp b/1218.cpp new file mode 100644 index 0000000..cb4cd3a --- /dev/null +++ b/1218.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +/** +PROBLEM + Given tree of nodes (computers) + Need to select elect some computers as servers + If selected, it & its adjacent computer will be activated + non-server computer cannot simultaneously activate multiple servers + What is the minimum # of computers as servers s.t. all computers activated? + +SOLUTION + Tree DP, Min dominating set of tree deformation + +State + dp[u][0]: u is the root of the min cost for the server & its entire subtree is activated + dp[u][1]: u is the min cost of root and u is a child node activating entire subtree + dp[u][2]: u is the root node activated by his parent and entire subtree is activated + +Transition (v is child of u) + dp[u][0] = Σmin(dp[v][0], dp[v][2])+1; + dp[u][1] = min(sum - dp[v][1] + dp[v][0]), sum = ∑dp[v][1]; + dp[u][2] = ∑dp[v][1]; + +Take any one node to root perform tree DP: ans = min(dp[root][0], dp[root][1]) +*/ + +#define pb push_back +#define mp make_pair +#define fi first +#define se second +#define lc(x) (x << 1) +#define rc(x) (x << 1 | 1) +#define lowbit(x) (x & (-x)) +#define ll long long +#define maxn 10050 + +const int inf = 1 << 20; + +vector vec[maxn]; +int n, dp[maxn][3]; + +void dfs(int u, int fa) +{ + int size = vec[u].size(); + dp[u][0] = 1; + dp[u][1] = inf; + dp[u][2] = 0; + for (int i = 0; i < size; i++) + { + int v = vec[u][i]; + if (v == fa) + continue; + dfs(v, u); + dp[u][0] += min(dp[v][2], dp[v][0]); + dp[u][2] += dp[v][1]; + } + for (int i = 0; i < size; i++) + { + int v = vec[u][i]; + if (v == fa) + continue; + dp[u][1] = min(dp[u][1], dp[u][2] - dp[v][1] + dp[v][0]); + } +} + +int main() +{ + int symbol = 0; + while (symbol != -1) + { + scanf("%d", &n); + for (int i = 0; i < maxn; i++) + { + vec[i].clear(); + } + for (int i = 1, a, b; i < n; i++) + { + scanf("%d%d", &a, &b); + vec[a].pb(b); + vec[b].pb(a); + } + scanf("%d", &symbol); + dfs(1, -1); + cout << min(dp[1][0], dp[1][1]) << endl; + } + return 0; +} diff --git a/12180.cpp b/12180.cpp new file mode 100644 index 0000000..d6a4a10 --- /dev/null +++ b/12180.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +// method : alpha-beta pruning +// runtime: slow (didn't check, probably exponential) + +/* maximum number of mugs */ +#define MAXM 4 +/* number of turns swapped mugs are "locked" into their place */ +#define LOCKTURNS 4 +/* maximum number of swaps per player per game */ +#define NROFSWAPS 3 +/* infinity (must be sufficiently large) */ +#define INF 12345 + +/* stores the state of a game */ +/* bips[i] = number of stones in mug i (mugs are numbered as given in the problem input) */ +/* lock[i] = number of turns before mugs i and i + M can be swapped again */ +/* swap[i] = number of remaining swaps for player i */ +typedef struct +{ + int bips[2 * (MAXM + 1)], lock[MAXM], swap[2]; +} state_t; + +/* M = number of mugs per player */ +/* N = total number of mugs and bowls, i.e. 2*M+1 */ +int M, N; + +/* Just do it. */ +/* stat = pointer to current state of game */ +/* plya = whose turn is it? */ +/* alph = current player can certainly reach this score (as in alpha-beta pruning) */ +/* beta = current player cannot be better than this */ +int nike(const state_t *stat, int plya, int alph, int beta) +{ + /* last = index of last mug a stone is distributed to */ + /* ofs1 = index of first mug of current player */ + /* ofs2 = index of first mug of other player */ + /* nust = new state (used as a parameter to recursive calls) */ + int last, ofs1 = plya * (M + 1), ofs2 = (ofs1 + M + 1) % N; + state_t nust; + int i, j, k; + /* count how many stones each player has in his mugs (but not in the bowls) */ + for (i = j = k = 0; i < M; ++i) + { + j += stat->bips[ofs1 + i], k += stat->bips[ofs2 + i]; + } + /* current player hasn't any stones left? */ + if (j == 0) + { + /* other player hasn't any left neither, game is over */ + if (k == 0) + { + return stat->bips[ofs1 + M] - stat->bips[ofs2 + M]; + } + /* else we cannot move, let other player go on */ + nust = *stat; + for (j = 0; j < M; ++j) + { + --nust.lock[j]; + } + k = -nike(&nust, plya ^ 1, -beta, -alph); + /* return new best value */ + if (alph < k) + { + alph = k; + } + return alph; + } + /* else try all possible mugs as a move */ + for (i = 0; i < M; ++i) + { + /* no stones in this mug? */ + if (stat->bips[ofs1 + i] == 0) + { + continue; + } + /* else copy current state and distribute stones according to rules */ + nust = *stat; + for (j = nust.bips[last = ofs1 + i], nust.bips[last] = 0; j > 0; --j) + { + ++nust.bips[last = (last + 1) % N]; + } + /* last stone went to own bowl? */ + if (last == ofs1 + M) + { + /* it's our turn again */ + k = nike(&nust, plya, alph, beta); + if (alph < k) + { + alph = k; + } + /* do alpha-beta pruning */ + if (alph >= beta) + { + return alph; + } + } + else + { + /* last stone hit own mug which was empty before? */ + if (ofs1 <= last && last < ofs1 + M && stat->bips[last] == 0 && nust.bips[M + M - last] > 0) + { + /* collect stones of this mug + opposite mug */ + nust.bips[ofs1 + M] += nust.bips[last] + nust.bips[M + M - last]; + nust.bips[last] = nust.bips[M + M - last] = 0; + } + /* turn over to other player */ + for (j = 0; j < M; ++j) + { + --nust.lock[j]; + } + k = -nike(&nust, plya ^ 1, -beta, -alph); + if (alph < k) + { + alph = k; + } + /* do alpha-beta pruning */ + if (alph >= beta) + { + return alph; + } + /* are we allowed to swap mugs? if yes, then explore this possibility */ + if (ofs2 <= last && last < ofs2 + M && nust.swap[plya] > 0 && nust.bips[ofs1 + last - ofs2] > 0 && nust.lock[last - ofs2] < 0) + { + k = nust.bips[last]; + nust.bips[last] = nust.bips[ofs1 + last - ofs2]; + nust.bips[ofs1 + last - ofs2] = k; + nust.lock[last - ofs2] = LOCKTURNS; + --nust.swap[plya]; + k = -nike(&nust, plya ^ 1, -beta, -alph); + if (alph < k) + { + alph = k; + } + /* do alpha-beta pruning */ + if (alph >= beta) + { + return alph; + } + } + } + } + return alph; +} + +int main() +{ + int kases, i; + state_t stat; + for (scanf("%d", &kases); kases > 0; --kases) + { + scanf("%d", &M); + N = 2 * (M + 1); + for (i = 0; i < N; ++i) + { + scanf("%d", stat.bips + i); + } + memset(stat.lock, 0, sizeof stat.lock); + stat.swap[0] = stat.swap[1] = NROFSWAPS; + printf("%d\n", -nike(&stat, 1, -INF, INF)); + } + return 0; +} diff --git a/12181.cpp b/12181.cpp new file mode 100644 index 0000000..3258db9 --- /dev/null +++ b/12181.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +// problem: Merchant Guild +// author : Bernhard Then +// method : dynamic programming, counting +// runtime: O(N^3), N = number of traders + +const int MAXN = 300; + +int locals[MAXN + 1]; +// locals[i] is the number of local traders with favourite locations i (1 <= i <= n <= MAXN) + +int cumulated_locals[MAXN + 1]; +// cumulated_locals[0] = 0 +// cumulated_locals[i] = locals[1] + locals[1] + ... + locals[i] + +int M; +// calculate all results modulo M; 2 <= M + +int dp[MAXN + 1][MAXN + 1]; +// for doing memoization in function rec + +int bin[MAXN + 1][MAXN + 1]; +// binomial coefficients + +int rec(int location, int additional) +{ + // 1 <= location; 0 <= additional + // at most additional+1 traders can be assigned to location + if (locals[location] > additional + 1) + { + return 0; + } + // recursion ends at location == 1 + if (location == 1) + { + return 1; + } + // memoization + int &res = dp[location][additional]; + if (res != -1) + { + return res; + } + res = 0; + int free = location + additional - cumulated_locals[location]; + for (int i = locals[location]; i <= additional + 1; ++i) + { + res = (res + (long long)bin[free][i - locals[location]] * rec(location - 1, additional + 1 - i)) % M; + } + return res; +} + +int possible(int n_traders) +{ + // tests if it is possible to assign foreign traders to locations s.th. every trader (local+foreign) + // finds a free spot + int additional = 0; + for (int i = n_traders; i >= 1; --i) + { + if (locals[i] > additional + 1) + { + return 0; + } + additional = additional + 1 - locals[i]; + } + return 1; +} + +int main() +{ + int cases; + scanf("%d", &cases); + for (int c = 0; c < cases; ++c) + { + int n_traders;// number of all traders + int n_locals; // number of local traders; 0 <= n_locals <= n_traders <= MAXN + scanf("%d%d%d", &n_traders, &n_locals, &M); + // initialize locals + for (int i = 1; i <= n_traders; ++i) + { + locals[i] = 0; + } + // read in local traders + for (int i = 0; i < n_locals; ++i) + { + int trader_no, favourite_pos; + scanf("%d%d", &trader_no, &favourite_pos);// trader_no is not needed actually + ++locals[favourite_pos]; + } + if (!possible(n_traders)) + { + puts("NO"); + continue; + } + // calculate binomial coefficients modulo M + for (int i = 0; i <= MAXN; ++i) + { + bin[i][0] = bin[i][i] = 1; + for (int j = 1; j < i; ++j) + { + bin[i][j] = (bin[i - 1][j - 1] + bin[i - 1][j]) % M; + } + } + // calculate cumulated_locals + cumulated_locals[0] = 0; + for (int i = 1; i <= n_traders; ++i) + { + cumulated_locals[i] = cumulated_locals[i - 1] + locals[i]; + } + // init dp + for (int i = 0; i <= n_traders; ++i) + { + for (int j = 0; j <= n_traders; ++j) + { + dp[i][j] = -1; + } + } + printf("YES %d\n", rec(n_traders, 0)); + } + return 0; +} diff --git a/12182.cpp b/12182.cpp new file mode 100644 index 0000000..db7f082 --- /dev/null +++ b/12182.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +// method : divide-and-conquer +// runtime: O(n) + +typedef pair pi; +vector adj[200001]; +int mss; + +// maximum sum of a subtree with root x and predecessor y +int dfs(int x, int y) +{ + int p = 0; + // process and destruct subtrees of x + for (; !adj[x].empty(); adj[x].pop_back()) + // do not return to y + if (adj[x].back().first != y) + // take only positive subtrees + { + p += max(0, adj[x].back().second + dfs(adj[x].back().first, x)); + } + // keep the maximum over all subtrees + mss = max(mss, p); + return p; +} + +int main() +{ + int n, a, b, p; + while (cin >> n && n) + { + // build adjacency lists of graph + while (n--) + { + cin >> a >> b >> p; + adj[a].push_back(pi(b, p)); + adj[b].push_back(pi(a, p)); + } + // global maximum + mss = 0; + // start at arbitrary node + dfs(b, b); + cout << mss << endl; + } + return 0; +} diff --git a/12183.cpp b/12183.cpp new file mode 100644 index 0000000..d32ff82 --- /dev/null +++ b/12183.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +// method : circulant matrices, repeated squaring +// runtime: O(N^2*log(S)) + +// intermediate values up to 10^18 +typedef long long ll; +// a circulant matrix is represented by its first row +typedef vector vl; + +int n; +ll mod; + +// modular product of circulant matrices v and w +vl mult(const vl v, const vl w) +{ + vl res(n); + // first row of usual matrix product + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + // use circulant property of w + { + res[i] = (res[i] + v[j] * w[(i + n - j) % n]) % mod; + } + return res; +} + +// modular power v^k of circulant matrix v +vl exp(const vl v, int k) +{ + // v^0 = identity matrix + if (k == 0) + { + vl res(n); + res[0] = 1; + return res; + } + // v^1 = v + if (k == 1) + { + return v; + } + // v^(2i) = (v^2)^i and v^(2i+1) = v*(v^2)^i + vl w = exp(mult(v, v), k / 2); + return k & 1 ? mult(v, w) : w; +} + +int main() +{ + int t; + cin >> t; + while (t--) + { + int s, l, r, x; + cin >> n >> s >> l >> r >> x; + vl msg(n); + for (int i = 0; i < n; i++) + { + cin >> msg[i]; + } + // modulo 10^x + mod = 1; + while (x--) + { + mod *= 10; + } + // circulant matrix with diagonals (l,1,r) + vl mat(n); + mat[0] = 1; + mat[1] = r; + mat[n - 1] = l; + // s steps amount to s-th power + mat = exp(mat, s); + // for the i-th decrypted number, take mat[i,j] times msg[j] + for (int i = 0; i < n; i++) + { + ll sum = 0; + for (int j = 0; j < n; j++) + // use circulant property of mat + { + sum = (sum + mat[(j + n - i) % n] * msg[j]) % mod; + } + cout << sum << (i < n - 1 ? ' ' : '\n'); + } + } + return 0; +} diff --git a/12184.cpp b/12184.cpp new file mode 100644 index 0000000..742e501 --- /dev/null +++ b/12184.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +// method : gcd +// runtime: O(S*log N), N = max(a, b) in gcd(a, b), S = number of serial numbers + +// absolute difference of two numbers +inline unsigned long long absdiff(unsigned long long a, unsigned long long b) +{ + return ((a > b) ? (a - b) : (b - a)); +} + +// greatest common divisor +unsigned long long gcd(unsigned long long a, unsigned long long b) +{ + if (b < a) + { + return gcd(b, a); + } + while (a != 0) + { + unsigned long long tmp = a; + a = b % a; + b = tmp; + } + return b; +} + +int main(int argc, char **argv) +{ + int nTests; + scanf("%d", &nTests); + for (int i = 0; i < nTests; ++i) + { + int s; + scanf("%d", &s); + unsigned long long d = 0; + unsigned long long maxa10 = 1;// maximal occuring a_10 + for (int j = 0; j < s; ++j) + { + unsigned long long sum = 0; + unsigned long long a; + // adding a_1, a_2, .. a_9 + for (int k = 0; k < 9; ++k) + { + scanf("%llu", &a); + sum += a; + } + scanf("%llu", &a); + if (a > maxa10) + { + maxa10 = a; + } + // difference between the sum and a_10 is, what counts + d = gcd(d, absdiff(a, sum)); + } + if (d <= maxa10) + { + // a_10 < N is needed for validity + puts("impossible"); + } + else + { + printf("%llu\n", d); + } + } + return 0; +} diff --git a/12186.cpp b/12186.cpp new file mode 100644 index 0000000..006ad67 --- /dev/null +++ b/12186.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define NN 100038 +/* {{{ FAST integer input */ +#define X10(n) ((n << 3) + (n << 1)) +#define RI readint +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +bool adapt()// Returns true if there is a number waiting to be read, false otherwise +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +/* }}} end FAST integer input */ + +int nf[NN], p[NN], q[NN], v[NN]; +vector s[NN]; + +int main() +{ + for (int N, T; RI(N) && RI(T) && N;) + { + for (int i = 0; i <= N; ++i) + { + nf[i] = v[i] = 0; + s[i].clear(); + } + for (int i = 1; i <= N; ++i) + { + RI(p[i]); + ++nf[p[i]]; + } + int h, t; + h = t = 0; + for (int i = 1; i <= N; ++i) + { + q[t] = i, t += !nf[i]; + } + while (h != t) + { + int x = q[h++], k = 0; + int lim = (s[x].size() * T + 99) / 100; + vector::iterator it; + if (!s[x].size()) + { + v[x] = 1; + } + else + { + sort(s[x].begin(), s[x].end()); + for (it = s[x].begin(); k < lim; it++, k++) + { + v[x] += *it; + } + } + if (!x) + { + break; + } + if (!--nf[p[x]]) + { + q[t++] = p[x]; + } + s[p[x]].push_back(v[x]); + } + printf("%d\n", v[0]); + } + return 0; +} diff --git a/12187.cpp b/12187.cpp new file mode 100644 index 0000000..b4652c8 --- /dev/null +++ b/12187.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define FORI(a, b, c) for (int a = b; a < c; ++a) +#define FOR(a, b) FORI(a, 0, b) + +int m[128][128][2]; +int dx[4] = {-1, 1, 0, 0}; +int dy[4] = {0, 0, -1, 1}; +int C, K, N, R; + +void attack(int x, int y) +{ + int current = m[x][y][0]; + int enemy = (current + 1) % N; + for (int dir = 0; dir < 4; ++dir) + { + int nx = x + dx[dir], ny = y + dy[dir]; + if (nx < 0 || ny < 0 || nx >= R || ny >= C) + { + continue; + } + if (m[nx][ny][0] == enemy) + { + m[nx][ny][1] = current; + } + } +} +int main() +{ + FOR(i, 128) + FOR(j, 128) m[i][j][1] = -1; + while (scanf("%d %d %d %d", &N, &R, &C, &K)) + { + if (!(N || R || C || K)) + { + break; + } + FOR(i, R) + FOR(j, C) scanf("%d", &m[i][j][0]); + FOR(k, K) + { + FOR(i, R) + FOR(j, C) attack(i, j); + FOR(i, R) + FOR(j, C) + { + if (m[i][j][1] != -1) + { + m[i][j][0] = m[i][j][1]; + } + m[i][j][1] = -1; + } + } + FOR(i, R) + { + FOR(j, C) + { + if (j) + { + putc(32, stdout); + } + printf("%d", m[i][j][0]); + } + putc(10, stdout); + } + } + return 0; +} diff --git a/12188.cpp b/12188.cpp new file mode 100644 index 0000000..4e9179f --- /dev/null +++ b/12188.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define IN getc(stdin) + +int c[26]; +int main() +{ + register char l = IN, p; + int down, up; + while (l != '*') + { + p = 'a'; + memset(c, 0, 104); + down = up = 0; + while (l != 10) + { + char k = (l - p + 26) % 26; + down = down + k; + ++c[k], p = l, l = IN; + } + char k = ('a' - p + 26) % 26; + down = down + k; + ++c[k]; + int ans = 0, d = 25, mindiff = 1 << 15; + while (down > up) + { + while (!c[d]) + { + --d; + } + while (c[d] && down > up) + { + down = down - d; + up = up + 26 - d; + int x = down - up; + if (x < 0 && x > -mindiff) + { + ans = up; + } + if (x >= 0 && x < mindiff) + { + ans = down; + } + c[d]--; + } + } + printf("%d\n", ans); + l = IN; + } + return 0; +} diff --git a/12189.cpp b/12189.cpp new file mode 100644 index 0000000..cea1d41 --- /dev/null +++ b/12189.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +struct event +{ + int t, v; + bool operator<(const event k) const + { + return t < k.t; + } +} e[64808]; + +char map_[128]; + +int main() +{ + int h, m, s, N, E, X; + char c; + map_['E'] = 1; + map_['X'] = -1; + map_['?'] = 0; + while (scanf("%d", &N) && N) + { + E = X = N >> 1; + event *evt = e; + for (int i = 0; i < N; ++i) + { + scanf("%2d:%2d:%2d %c", &h, &m, &s, &c); + int t = s + 60 * m + 3600 * h; + (*evt).t = t; + (*evt).v = *(map_ + c); + E -= (c == 'E'); + X -= (c == 'X'); + evt++; + } + sort(e, e + N); + int cnt = 0, ans = 0; + evt = e; + for (int i = 0; i < N; ++i) + { + cnt += (*evt).v; + if (!(*evt).v) + { + if (E) + { + --E, ++cnt; + } + else + { + --X, --cnt; + } + } + if (cnt > ans) + { + ans = cnt; + } + evt++; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1219.cpp b/1219.cpp new file mode 100644 index 0000000..3da30e2 --- /dev/null +++ b/1219.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +typedef struct _player +{ + int n; + string name; + char pos; + int years; +} player; + +bool theTruthIsOutThere(const player &a, const player &b) +{ + return a.n < b.n; +} + +int main() +{ + bool finish = false; + string input; + player p; + vector def, str, mid, gk, team; + vector y; + while (true) + { + def.clear(); + str.clear(); + mid.clear(); + gk.clear(); + team.clear(); + for (int i = 0; i < 22; i++) + { + cin >> p.n; + if (p.n == 0) + { + finish = true; + break; + } + cin >> p.name >> p.pos; + getline(cin, input);// information regarding years played for the club + for (int j = 0, sz = input.size(); j < sz; j++) + { + if (input[j] >= '0' && input[j] <= '9' || input[j] == ' ') + { + continue; + } + input[j] = ' '; + } + y.clear(); + istringstream iss(input); + copy(istream_iterator(iss), istream_iterator(), back_inserter>(y)); + p.years = 0; + for (int j = 1, sz = y.size(); j < sz; j += 2) + { + p.years += y[j] - y[j - 1] + 1; + } + switch (p.pos) + { + case 'G': + gk.push_back(p); + break; + case 'D': + def.push_back(p); + break; + case 'M': + mid.push_back(p); + break; + case 'S': + str.push_back(p); + break; + } + } + if (finish) + { + break; + } + char tmp; + int defenders, midplayers, strikers; + cin >> defenders >> tmp >> midplayers >> tmp >> strikers; + if (defenders > def.size() || midplayers > mid.size() || strikers > str.size() || 1 > gk.size()) + { + cout << "IMPOSSIBLE TO ARRANGE" << endl; + cout << endl; + continue; + } + sort(gk.begin(), gk.end(), theTruthIsOutThere); + sort(def.begin(), def.end(), theTruthIsOutThere); + sort(mid.begin(), mid.end(), theTruthIsOutThere); + sort(str.begin(), str.end(), theTruthIsOutThere); + player captain; + team.push_back(gk[0]); + for (int i = 0; i < defenders; i++) + { + team.push_back(def[i]); + } + for (int i = 0; i < midplayers; i++) + { + team.push_back(mid[i]); + } + for (int i = 0; i < strikers; i++) + { + team.push_back(str[i]); + } + int best = -1; + for (int i = 0; i < 11; i++) + { + if (team[i].years > best) + { + best = team[i].years; + captain = team[i]; + } + else if (team[i].years == best) + { + if (team[i].n > captain.n) + { + captain = team[i]; + } + } + } + cout << captain.n << " " << captain.name << " " << captain.pos << endl; + for (int i = 0; i < 11; i++) + { + if (team[i].n == captain.n) + { + continue; + } + cout << team[i].n << " " << team[i].name << " " << team[i].pos << endl; + } + cout << endl; + } + return 0; +} diff --git a/12190.cpp b/12190.cpp new file mode 100644 index 0000000..ee62223 --- /dev/null +++ b/12190.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int C(int price) +{ + int cons = 0; + cons += min(max(0, price / 2), 100); + price -= 2 * 100; + cons += min(max(0, price / 3), 9900); + price -= 3 * 9900; + cons += min(max(0, price / 5), 990000); + price -= 5 * 990000; + cons += max(0, price / 7); + return cons; +} + +int V(int cons) +{ + int price = 0; + price += min(max(0, cons * 2), 2 * 100); + cons -= 100; + price += min(max(0, cons * 3), 3 * 9900); + cons -= 9900; + price += min(max(0, cons * 5), 5 * 990000); + cons -= 990000; + price += max(0, cons * 7); + return price; +} + +int main() +{ + int a, b; + while (scanf("%d%d", &a, &b) == 2, a|b) + { + int total = C(a), begin = 0, end = total, ans = 0; + while (begin < end) + { + int mine = (begin + end) / 2; + int diff = V(total - mine) - V(mine); + if (diff > b) + { + begin = mine; + } + else if (diff < b) + { + end = mine; + } + else + { + ans = mine; + break; + } + } + printf("%d\n", V(ans)); + } + return 0; +} diff --git a/12191.cpp b/12191.cpp new file mode 100644 index 0000000..b5f72ab --- /dev/null +++ b/12191.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define NN 100030 + +char s[NN]; +int C, H, L; +int B[NN], NB[NN], SA[NN]; + +bool cmp(int x, int y) +{ + return H ? B[x] == B[y] ? B[x + H] < B[y + H] : B[x] < B[y] : s[x] < s[y]; +} +bool eq(int x, int y) +{ + return !(cmp(SA[x], SA[y]) || cmp(SA[y], SA[x])); +} +int ub() +{ + int st = 0, id = 0; + bool c = 0; + for (int i = 0; i < L; i++) + { + if (i != 0 && !eq(i, i - 1)) + { + st = i, id++; + } + c |= (i != st); + NB[SA[i]] = id; + } + memcpy(B, NB, L << 2); + return c; +} +int ss() +{ + int ans = 0, i, j, l = 0, *PSA = SA; + char *a, *b; + for (i = 0; i < L; ++i) + { + SA[i] = i; + } + sort(SA, SA + L, cmp); + for (C = ub(), H = 1; C; H <<= 1) + { + sort(SA, SA + L, cmp), C = ub(); + } + for (i = 0; i < L; ++i) + { + B[*PSA++] = i; + } + for (i = 0; i < L; ++i) + if (B[i]) + { + j = SA[B[i] - 1]; + a = s + i + l, b = s + j + l; + while (*a++ == *b++) + { + l++; + } + NB[B[i]] = l; + if (l) + { + l--; + } + } + NB[0] = 0; + for (i = L; i--;) + { + int x = NB[i] - NB[i - 1]; + if (x > 0) + { + ans += x; + } + } + return ans; +} +int main() +{ + while (scanf("%s%n\n", s, &L) && *s != '*') + { + ++L; + H = 0; + printf("%d\n", ss()); + //for(int i = 0; i < L; i++) printf("%d '%s'\n", SA[i], s + SA[i]); + } + return 0; +} diff --git a/12192.cpp b/12192.cpp new file mode 100644 index 0000000..4418f85 --- /dev/null +++ b/12192.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) + +int R, C, Q; +int INI, FIM; +int n[560][560]; +int main() +{ + while (scanf("%d%d", &R, &C) && (R || C)) + { + for (int i = 0; i < R; ++i) + for (int j = 0; j < C; ++j) + { + scanf("%d", &n[i][j]); + } + scanf("%d", &Q); + for (int q = 0; q < Q; ++q) + { + int max = -1; + scanf("%d%d", &INI, &FIM); + for (int i = 0; i < R; ++i) + { + int u, v; + for (u = 0, v = C - 1; u < v;) + { + int c = (u + v) >> 1; + if (n[i][c] < INI) + { + u = c + 1; + } + else + { + v = c; + } + } + while (u < (C - 1) && n[i][u] < INI) + { + u++; + } + while (u && n[i][u - 1] >= INI) + { + u--; + } + int y = u; + if (n[i][y] < INI || n[i][y] > FIM) + { + continue; + } + for (u = 0, v = MIN(R - i - 1, C - y - 1); u < v;) + { + int c = (u + v) >> 1; + if (n[i + c][y + c] < FIM) + { + u = c + 1; + } + else + { + v = c; + } + } + int x = i + u, y2 = y + u; + while (x < (R - 1) && y2 < (C - 1) && n[x + 1][y2 + 1] <= FIM) + { + ++x, ++y2; + } + while (x && y2 && n[x][y2] > FIM) + { + --x, --y2; + } + int valor = y2 - y; + if (valor > max) + { + max = valor; + } + } + printf("%d\n", max + 1); + } + printf("-\n"); + } + return 0; +} diff --git a/12193.cpp b/12193.cpp new file mode 100644 index 0000000..b553c2d --- /dev/null +++ b/12193.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +int n; +int c[42][42], f[42][42], p[42], q[42]; +bool v[42]; +bool bfs(int s, int t) +{ + int h = 0, i, j, tl = 0; + memset(v, 0, n); + q[tl++] = s; + v[s] = 1; + p[s] = -1; + while (!v[t] && h != tl) + { + i = q[h++]; + for (j = 0; j < n; j++) + { + if (!v[j] && c[i][j] - f[i][j] > 0) + { + q[tl++] = j; + v[j] = 1; + p[j] = i; + } + } + } + return v[t]; +} + +int max_flow(int s, int t) +{ + int i, mf = 0; + memset(f, 0, sizeof(f)); + while (bfs(s, t)) + { + int cf = 1 << 10; + for (i = t; p[i] >= 0; i = p[i]) + { + int temp = c[p[i]][i] - f[p[i]][i]; + if (temp < cf) + { + cf = temp; + } + } + for (i = t; p[i] >= 0; i = p[i]) + { + f[p[i]][i] += cf; + f[i][p[i]] -= cf; + } + mf += cf; + } + return mf; +} +int pts[42], m[42][42], ml[42]; +const char *ans[2] = {"N", "Y"}; +int main() +{ + int a, b, G, M, N; + char r; + bool impossible; + while (scanf("%d %d %d", &N, &M, &G) && N) + { + n = N + 2; + int MATCHES = (N - 1) * M, src = n - 1, snk = n - 2; + impossible = 0; + for (int i = 0; i < n; ++i) + { + pts[i] = 0; + ml[i] = MATCHES; + for (int j = 0; j < n; ++j) + { + c[i][j] = m[i][j] = 0; + } + } + for (int i = 0; i < G; ++i) + { + scanf("%d %c %d", &a, &r, &b); + if (r == '<') + { + pts[b] += 2; + } + else + { + ++pts[a], ++pts[b]; + } + --ml[a], --ml[b]; + m[a][b] = m[b][a] = m[a][b] + 1; + } + for (int i = 1; i < N; ++i) + { + int x = M - m[0][i]; + pts[0] += (x << 1); + G += x; + ml[i] -= x; + m[0][i] = m[i][0] = 0; + } + for (int i = 1; i < N; ++i) + { + int s = 0; + for (int j = i + 1; j < N; ++j) + { + int x = (M - m[i][j]) << 1; + s += x; + c[i][j] = x; + } + c[src][i] = s; + int a = pts[0] - pts[i] - 1; + int b = ml[i] << 1; + if (a < 0) + { + impossible = 1; + break; + } + if (a < b) + { + c[i][snk] = a; + } + else + { + c[i][snk] = b; + } + } + if (!impossible) + { + int flow = max_flow(src, snk); + int left = MATCHES * N - 2 * G; + puts(ans[flow == left]); + } + else + { + puts("N"); + } + } + return 0; +} diff --git a/12194.cpp b/12194.cpp new file mode 100644 index 0000000..82d10e5 --- /dev/null +++ b/12194.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +typedef unsigned long long int ulli; +inline ulli sq(ulli a) +{ + return a * a; +} +int main() +{ + int n = 0; + ulli p[1000][2]; + ulli d[1000][1000]; + while (std::cin >> n) + { + if (n == 0) + { + break; + } + for (int i = 0; i < n; ++i) + { + std::cin >> p[i][0] >> p[i][1]; + for (int j = 0; j <= i; ++j) + { + d[i][j] = d[j][i] = sq(p[i][0] - p[j][0]) + sq(p[i][1] - p[j][1]); + } + } + int c = 0; + for (int i = 0; i < n; ++i) + { + std::sort(&d[i][0], &d[i][n]); + } + for (int i = 0; i < n; ++i) + { + ulli s = d[i][0]; + int k = 0; + for (int j = 1; j < n; ++j) + { + if (d[i][j] != s) + { + c += k * (k + 1) / 2; + s = d[i][j]; + k = -1; + } + ++k; + } + c += k * (k + 1) / 2; + } + std::cout << c << std::endl; + } + return 0; +} diff --git a/12195.cpp b/12195.cpp index e13fcf0..9cbccd1 100644 --- a/12195.cpp +++ b/12195.cpp @@ -1,33 +1,56 @@ -#include -#include -#include -#include -#include -using namespace std; +#include -const double EPS = 1e-9; +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - string str; - cin >> str; - if (str == "*") break; - - map Map; - Map['W'] = 1; Map['H'] = 1/2.0; Map['Q'] = 1/4.0; Map['E'] = 1/8.0; Map['S'] = 1/16.0; Map['T'] = 1/32.0; Map['X'] = 1/64.0; - double C = 0; - int Cnt = 0; - for (int i = 0; i < str.length(); i++) { - if (str[i] == '/') { - if (abs(C - 1.0) <= EPS) ++Cnt; - C = 0.0; +int main() +{ + string input; + while (getline(cin, input)) + { + if (input == "*") + { + break; + } + int a = 0, b = 64; + int counter = 0; + for (int i = 1, sz = input.size(); i < sz; i++) + { + switch (input[i]) + { + case 'W': + a += 64; + break; + case 'H': + a += 32; + break; + case 'Q': + a += 16; + break; + case 'E': + a += 8; + break; + case 'S': + a += 4; + break; + case 'T': + a += 2; + break; + case 'X': + a += 1; + break; + case '/': + if (a > 0)// avoid a=0 case + { + if (a == b) + { + counter++; + } + } + a = 0; + break; } - else - C += Map[str[i]]; } - cout << Cnt << endl; + cout << counter << endl; } return 0; } diff --git a/12196.cpp b/12196.cpp new file mode 100644 index 0000000..cf2dbc7 --- /dev/null +++ b/12196.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define NN 10050 + +int v[NN]; +int diff; +int sum[1000]; +int main() +{ + int N, K, *s; + while (scanf("%d", &N) && N) + { + memset(sum, 0, sizeof(sum)); + for (int i = 0; i < N; ++i) + { + scanf("%d", &K); + for (int j = 0, *c = v; j < K; ++j, c++) + { + scanf("%d", c); + } + sort(v, v + K); + v[K] = 1001; + s = sum; + for (int j = 0, T = 0; j <= K; ++j) + { + int diff = abs(j - (K - j)), x = *(v + j); + while (T < x) + { + *s++ += diff, T++; + } + } + } + int k, min = 1 << 25; + s = sum; + for (int i = 0; i < 1001; ++i, s++) + { + if (*s < min) + { + min = *s; + } + } + printf("%d\n", min); + } + return 0; +} diff --git a/12197.cpp b/12197.cpp new file mode 100644 index 0000000..7867dee --- /dev/null +++ b/12197.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +// binary search: O(n * log (max coordinate / epsilon)) + +const int maxn = 50000; +const double eps = 1e-11, infinity = 1e300; + +double px[maxn], py[maxn]; +int n; + +bool possible(double d, double &px1, double &px2) +{ + double x1 = -infinity, x2 = infinity; + for (int i = 0; i < n; ++i) + { + if (d < py[i]) + { + return false; + } + double p = sqrt(d * d - py[i] * py[i]), + a = px[i] - p, b = px[i] + p; + x1 = max(x1, a); + x2 = min(x2, b); + if (x1 > x2) + { + return false; + } + } + px1 = x1; + px2 = x2; + return true; +} + +int main() +{ + while (scanf("%i", &n) == 1 && n > 0) + { + double a, b = 0, x1, x2; + for (int i = 0; i < n; ++i) + { + scanf("%lf%lf", &px[i], &py[i]); + if (py[i] < 0) + { + py[i] = -py[i]; + } + b = max(b, py[i]); + } + if (b == 0) + { + if (n == 1) + { + printf("%.9lf %.9lf\n", px[0], .0); + continue; + } + b = 1; + } + while (!possible(b, x1, x2)) + { + b *= 2; + } + a = b / 2; + while (possible(a, x1, x2)) + { + a /= 2; + } + for (int i = 0; i < 100 && (b - a > eps || x2 - x1 > eps); ++i) + { + double m = (a + b) / 2; + if (possible(m, x1, x2)) + { + b = m; + } + else + { + a = m; + } + } + printf("%.9lf %.9lf\n", (x1 + x2) / 2, b); + } + return 0; +} diff --git a/12198.cpp b/12198.cpp new file mode 100644 index 0000000..63d0e3c --- /dev/null +++ b/12198.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int min(int a, int b) +{ + return a < b ? a : b; +} + +int main() +{ + char op[16]; + for (int f = 0, n, r, z; scanf("%d", &n) == 1 && n; f = 1) + { + int s[2] = {0, 0}; + if (f) + { + puts(""); + } + for (int i = 0; i < n; ++i) + { + scanf("%s %d", op, &z); + if (*op == 'D') + { + s[1] += z; + printf("DROP 2 %d\n", z); + } + else + { + if (!s[0]) + { + s[0] = s[1]; + printf("MOVE 2->1 %d\n", s[1]); + s[1] = 0; + } + r = min(s[0], z); + s[0] -= r; + z -= r; + printf("TAKE 1 %d\n", r); + if (z) + { + s[0] = s[1] - z; + printf("MOVE 2->1 %d\n", s[1]); + printf("TAKE 1 %d\n", z); + s[1] = 0; + } + } + } + } + return 0; +} diff --git a/12199.cpp b/12199.cpp new file mode 100644 index 0000000..5cfb9c5 --- /dev/null +++ b/12199.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +// preprocess: O(mod); after: O(log mod) + +typedef long long ll; + +const int mod = 10567201; + +ll fact[mod]; + +// Compute a^n % 'mod' in O(log n), mod > 1 +ll modexp(ll a, ll n, int mod) +{ + ll r = 1; + while (n) + { + if (n & 1) + { + r = (r * a) % mod; + --n; + } + else + { + a = (a * a) % mod; + n >>= 1; + } + } + return r; +} + +// inverse of a in Z_p. +ll inverse(ll a) +{ + return modexp(a, mod - 2, mod); +} + +// (m choose n) mod p, for n < p. +// for any m < p, (m choose n) mod p = ((m mod p) choose n) mod p, +// even if m mod p >= n. +ll ncr(ll m, ll n) +{ + if (m < n) + return 0; + return (fact[m] * inverse((fact[n] * fact[m - n]) % mod)) % mod; +} + +ll calc(ll n, ll m, bool iszero) +{ + ll pow2n = modexp(2, n, mod), + invpow2n = inverse(pow2n); + if (m & 1) + return (ncr(pow2n, m) * invpow2n) % mod; + else + { + static const ll inv2 = inverse(2); + ll dif = ncr((pow2n * inv2) % mod, m / 2); + if (m & 2) + dif = (mod - dif) % mod; + ll res1 = ((ncr(pow2n, m) + mod - dif) * invpow2n) % mod; + return iszero ? (dif + res1) % mod : res1; + } +} + +int main() +{ + ll n, m, v; + fact[0] = 1; + for (n = 1; n < mod; ++n) + fact[n] = (n * fact[n - 1]) % mod; + while (scanf("%lli%lli%lli", &n, &m, &v) == 3 && n) + printf("%lli\n", calc(n, m, v == 0)); + return 0; +} diff --git a/122.cpp b/122.cpp new file mode 100644 index 0000000..d53de2f --- /dev/null +++ b/122.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +struct node +{ + node *left, *right; + int val; +}; +node *root; +int N, MAX; +struct st +{ + int a[257]; + int index; +} V[257]; + +int Insert(int n, int ind, char s[], node *rt) +{ + if (strlen(s) == 0) + { + if (root->val != -1) + { + return 0; + } + root->val = n; + return 1; + } + if (ind == strlen(s)) + { + if (rt->val != -1) + { + return 0; + } + rt->val = n; + return 1; + } + if (s[ind] == 'L') + { + if (rt->left != NULL) + { + return Insert(n, ind + 1, s, rt->left); + } + node *p = (node *)malloc(sizeof(node)); + p->left = p->right = NULL; + rt->left = p; + p->val = -1; + return Insert(n, ind + 1, s, rt->left); + } + if (s[ind] == 'R') + { + if (rt->right != NULL) + { + return Insert(n, ind + 1, s, rt->right); + } + node *p = (node *)malloc(sizeof(node)); + p->left = p->right = NULL; + rt->right = p; + p->val = -1; + return Insert(n, ind + 1, s, rt->right); + } + return 1; +} + +void Sep(char ss[], char *temp) +{ + int i, j = 0; + char dummy[100]; + for (i = 1; ss[i]; i++) + { + if (ss[i] == ',') + { + break; + } + dummy[j++] = ss[i]; + } + dummy[j] = '\0'; + N = atoi(dummy); + j = 0; + for (i++; ss[i] != ')'; i++) + { + temp[j++] = ss[i]; + } + temp[j] = '\0'; +} + +int Traversal(node *r, int index) +{ + if (r->val == -1) + { + return 0; + } + V[index].a[V[index].index++] = r->val; + if (index > MAX) + { + MAX = index; + } + if (r->left != NULL) + if (!Traversal(r->left, index + 1)) + { + return 0; + } + if (r->right != NULL) + if (!Traversal(r->right, index + 1)) + { + return 0; + } + return 1; +} + +void Cal() +{ + int i, j, k; + MAX = 0; + j = Traversal(root, 0); + if (!j) + { + printf("not complete\n"); + return; + } + printf("%d", V[0].a[0]); + k = 1; + for (i = 0; i <= MAX; i++) + { + for (j = k; j < V[i].index; j++) + { + printf(" %d", V[i].a[j]); + } + V[i].index = 0; + k = 0; + } + putchar('\n'); +} + +int main() +{ + char ss[100], path[500]; + int f; + root = NULL; + while (scanf("%s", ss) != EOF) + { + if (!strcmp(ss, "()")) + { + continue; + } + f = 1; + node *p = (node *)malloc(sizeof(node)); + p->left = p->right = NULL; + p->val = -1; + root = p; + Sep(ss, path); + Insert(N, 0, path, root); + while (1) + { + scanf("%s", ss); + if (!strcmp(ss, "()")) + { + break; + } + if (!f) + { + continue; + } + Sep(ss, path); + f = Insert(N, 0, path, root); + } + if (!f) + { + printf("not complete\n"); + } + else + { + Cal(); + } + } + return 0; +} diff --git a/1220.cpp b/1220.cpp new file mode 100644 index 0000000..991b8ef --- /dev/null +++ b/1220.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define MAX 205 + +map E; +int emp(string &s) +{ + if (E.find(s) != E.end()) + { + return E[s]; + } + else + { + return E[s] = E.size() - 1; + } +} + +bool L[MAX], L2[MAX]; +vector G[MAX]; +int n; + +int dfs(int v) +{ + int acum = 0, illu = 0; + for (int i = 0; i < G[v].size(); i++) + { + acum += dfs(G[v][i]); + if (L[G[v][i]]) + { + illu++; + } + } + if (G[v].size() > 0 && illu < G[v].size()) + { + L[v] = true; + } + return acum + L[v]; +} + +int main() +{ + while (cin >> n, n) + { + memset(G, 0, sizeof(G)); + memset(L, 0, sizeof(L)); + E.clear(); + string a, b; + cin >> a; + emp(a); + for (int i = 1; i < n; i++) + { + cin >> a >> b; + G[emp(b)].push_back(emp(a)); + } + int total = dfs(0); + memcpy(L2, L, sizeof(L)); + bool unique = true; + for (int i = 0; i < n; i++) + { + if (!L2[i]) + { + memset(L, 0, sizeof(L)); + L[i] = true; + if (dfs(0) == total) + { + unique = false; + break; + } + } + } + cout << n - total << " " << (unique ? "Yes" : "No") << endl; + } + return 0; +} diff --git a/12200.cpp b/12200.cpp new file mode 100644 index 0000000..884f9b6 --- /dev/null +++ b/12200.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +//DP + +#define MAXN 501 + +int board[22] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5, 20, 1}; + +double p[MAXN + 1][MAXN + 1][2]; +//p[m][n][0] = prob of winning A if A starts, A with m points and B with n. +//p[m][n][1] = prob of winning B if B starts, A with m points and B with n. + +int main() +{ + memset(p, 0, sizeof(p)); + for (int n = 1; n <= MAXN; n++) + { + p[0][n][1] = 0.0; + } + for (int m = 1; m <= MAXN; m++) + { + p[m][0][0] = 0.0; + } + for (int m = 1; m <= MAXN; m++) + for (int n = 1; n <= MAXN; n++) + { + double a0 = 0.0, pr0 = 0.0; + for (int i = 1; i <= 20; i++) + if (i <= m) + { + pr0 += (1.0 - p[m - i][n][1]) / 20.0; + } + else + { + a0 += 1.0 / 20.0; + } + //p[m][n][0] = a0 * (1 - p[m][n][1]) + pr0; + double pr1[4]; + for (int i = 0; i < 4; i++) + { + pr1[i] = -1.0; + } + for (int i = 1; i <= 20; i++) + { + int b = 0; + double pri = 0.0; + for (int j = -1; j < 2; j++) + if (board[i + j] > n) + { + b++; + } + else + { + pri += (1.0 - p[m][n - board[i + j]][0]) / 3.0; + } + pr1[b] = max(pr1[b], pri); + } + //solving 4 implicit equations to maximize p[m][n][1] + p[m][n][1] = -1e9; + for (int i = 0; i < 4; i++) + if (pr1[i] > -0.5) + { + double c = (double)i / 3.0; + p[m][n][1] = max(p[m][n][1], (pr1[i] + c * (1 - pr0 - a0)) / (1.0 - c * a0)); + } + p[m][n][0] = a0 * (1 - p[m][n][1]) + pr0; + } + for (;;) + { + int n; + scanf("%u", &n); + if (n == 0) + { + break; + } + printf("%.12lf %.12lf\n", p[n][n][0], p[n][n][1]); + } + return 0; +} diff --git a/12201.cpp b/12201.cpp new file mode 100644 index 0000000..7319472 --- /dev/null +++ b/12201.cpp @@ -0,0 +1,271 @@ +#include + +using namespace std; + +bool igual(char a, char b) +{ + return (tolower(a) == tolower(b)); +} + +string reverso(string s) +{ + int n = s.size(); + string ret = ""; + for (int i = n - 1; i >= 0; i--) + if ((s[i] - 'a') >= 0) + { + ret += s[i] + 'A' - 'a'; + } + else + { + ret += s[i] - 'A' + 'a'; + } + return ret; +} + +string rota(string s, int n) +{ + string ret = s.substr(n); + return ret + s.substr(0, n); +} + +string cortapega(string s, int corte, int pega1, int pega2) +{ + //corte es la posicion de la letra siguiente a donde se hace el corte + //[0, 1, ...., pega1, ...., corte - 1, corte, ..., pega2, ..., n-1] + string s1 = s.substr(0, pega1); + string s2 = s.substr(pega1 + 1, corte - 1 - pega1); + string s3 = s.substr(corte, pega2 - corte); + string s4 = s.substr(pega2 + 1); + char letra = tolower(s[pega1]); + string ret1 = s2 + letra + s1; + string ret2 = s4 + (char)toupper(letra) + s3; + if (s[pega1] == s[pega2]) + { + ret2 = reverso(ret2); + } + return ret1 + ret2; +} + +string eliminaesferas(string s) +{ + int n = s.size(); + for (int i = 0; i < n - 1; i++) + if (s[i] != s[i + 1] && igual(s[i], s[i + 1])) + { + return eliminaesferas(s.substr(0, i) + s.substr(i + 2)); + } + if (n && s[0] != s[n - 1] && igual(s[0], s[n - 1])) + { + return eliminaesferas(s.substr(1, n - 2)); + } + return s; +} + +int p2, toros; + +string eliminap2(string s) +{ + int n = s.size(); + for (int i = 0; i < n - 1; i++) + if (s[i] == s[i + 1]) + { + p2++; + return eliminap2(s.substr(0, i) + s.substr(i + 2)); + } + if (n && (s[0] == s[n - 1])) + { + p2++; + return eliminap2(s.substr(1, n - 2)); + } + return s; +} + +string eliminatoros(string s) +{ + int n = s.size(); + if (n < 4) + { + return s; + } + string aux = s + s.substr(0, 3); + for (int i = 0; i < n; i++) + if (aux[i] != aux[i + 2] && igual(aux[i], aux[i + 2])) + if (aux[i + 1] != aux[i + 3] && igual(aux[i + 1], aux[i + 3])) + { + s = rota(s, i); + s = s.substr(4); + toros++; + return eliminatoros(s); + } + return s; +} + +string encuentrap2(string s, int &pos) +{ + //devolvere un string en el que s[0] y s[pos] son iguales, salvo si pos = -1 + int n = s.size(); + for (int i = 0; i < n; i++) + { + for (int j = 1; j < n; j++) + if (s[j] == s[0]) + { + pos = j; + return s; + } + s = rota(s, 1); + } + pos = -1; + return s; +} + +string encuentratoros(string s, int &pos1, int &pos2, int &pos3) +{ + //s = [a.....b......A......B...] + // 0....pos1...pos2...pos3 + int n = s.size(); + for (int k = 0; k < n; k++) + { + int i, j; + for (i = 1; i < n; i++) + if (igual(s[0], s[i])) + { + break; + } + if (s[0] != s[i]) + { + int a['z' - 'a' + 1]; + memset(a, 0, sizeof(a)); + for (j = 1; j < i; j++) + if (s[j] >= 'a') + { + a[tolower(s[j]) - 'a']++; + } + else + { + a[tolower(s[j]) - 'a']--; + } + for (j = i + 1; j < n; j++) + if (s[j] <= 'Z') + { + a[tolower(s[j]) - 'a']++; + } + else + { + a[tolower(s[j]) - 'a']--; + } + char letra = 0; + for (j = 0; j < 'z' - 'a' + 1; j++) + if (a[j] == 2 || a[j] == -2) + { + letra = (char)('a' + j); + break; + } + if (letra != 0) + { + for (j = 1; j < i; j++) + if (igual(s[j], letra)) + { + pos1 = j; + } + for (j = i + 1; j < n; j++) + if (igual(s[j], letra)) + { + pos3 = j; + } + pos2 = i; + return s; + } + } + s = rota(s, 1); + } + pos1 = -1; + return s; +} + +string aislap2(string s, int pos) +{ + s = cortapega(s, pos, 0, pos); + return s; +} + +string aislatoros(string s, int pos1, int pos2, int pos3) +{ + int n = s.size(); + s = rota(s, 1); + pos1--; + pos2--; + pos3--; + s = cortapega(s, pos2, pos1, pos3); + s = rota(s, pos2 - pos1); + s = cortapega(s, n - pos3 + pos1 - 1, n - pos3 + pos1 - 2, n - pos3 + pos1); + return s; +} + +void haz(string s) +{ + while (s != "") + { + s = eliminaesferas(s); + int pos, pos1, pos2, pos3; + s = encuentrap2(s, pos); + if (pos != -1) + { + s = aislap2(s, pos); + s = eliminap2(s); + } + else + { + s = encuentratoros(s, pos1, pos2, pos3); + if (pos1 != -1) + { + s = aislatoros(s, pos1, pos2, pos3); + s = eliminatoros(s); + } + } + } + return; +} + +int main() +{ + clock_t start, finish; + start = clock(); + while (1) + { + string s; + cin >> s; + if (s == "END") + { + break; + } + p2 = 0; + toros = 0; + haz(s); + if (!p2 && !toros) + { + cout << "none" << endl; + } + else if (p2 == 0) + { + cout << toros << " leg"; + if (toros > 1) + { + cout << "s"; + } + cout << endl; + } + else + { + int total = p2 + 2 * toros; + cout << total << " arm"; + if (total > 1) + { + cout << "s"; + } + cout << endl; + } + } + finish = clock(); + return 0; +} diff --git a/12202.cpp b/12202.cpp new file mode 100644 index 0000000..158f9ec --- /dev/null +++ b/12202.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +const int INF = 1 << 30, NN = 32; +char board[NN][NN]; +struct edge +{ + int from, to, w; +} e[4 * NN * NN]; +int d[NN * NN], ecnt; +int dx[4] = {-1, 0, 1, 0}; +int dy[4] = {0, -1, 0, 1}; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +int mask(int x, int y) +{ + return (x << 5) | y; +} + +void add_edge(int from, int to, int w) +{ + edge &E = e[ecnt++]; + E.from = from; + E.to = to; + E.w = w; +} + +int main() +{ + for (int c, h, w, x, y, x2, y2, E, G; scanf("%d %d", &w, &h) == 2 && w;) + { + ecnt = 0; + FOR(i, w) + FOR(j, h) board[i][j] = 0, + d[mask(i, j)] = INF; + board[w - 1][h - 1] = 'E'; + d[0] = 0; + // Read Gravestones + scanf("%d", &G); + FOR(i, G) + { + scanf("%d %d", &x, &y); + board[x][y] = 'G'; + } + // Read Haunted Holes + scanf("%d", &E); + FOR(i, E) + { + scanf("%d %d %d %d %d", &x, &y, &x2, &y2, &c); + add_edge(mask(x, y), mask(x2, y2), c); + board[x][y] = 'E'; + } + FOR(i, w) + FOR(j, h) if (!board[i][j]) FOR(dir, 4) + { + x = i + dx[dir], y = j + dy[dir]; + if (x >= 0 && x < w && y >= 0 && y < h && board[x][y] != 'G') + { + add_edge(mask(i, j), mask(x, y), 1); + } + } + // Bellman-Ford to check for negative-weight cycles + // and find distance between (0, 0) and (W-1, H-1) + int vertices = w * h - G - 1; + FOR(i, vertices) + FOR(j, ecnt) + { + int from = e[j].from, to = e[j].to; + if (d[from] != INF && d[from] + e[j].w < d[to]) + { + d[to] = d[from] + e[j].w; + } + } + bool never = false; + FOR(j, ecnt) + { + int from = e[j].from, to = e[j].to; + if (d[from] != INF && d[from] + e[j].w < d[to]) + { + never = true; + } + } + if (never) + { + puts("Never"); + } + else if (d[mask(w - 1, h - 1)] == INF) + { + puts("Impossible"); + } + else + { + printf("%d\n", d[mask(w - 1, h - 1)]); + } + } + return 0; +} diff --git a/12203.cpp b/12203.cpp new file mode 100644 index 0000000..40ae08f --- /dev/null +++ b/12203.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +// O(n^2) DP + +const double eps = 1e-9, infinity = 1e300; + +struct point +{ + double x, y; + point(double a = 0, double b = 0) : x(a), y(b) {} +}; + +double area2(point a, point b, point c) +{ + return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); +} + +// Returns true if c is to the left of ab +bool leftturn(point a, point b, point c) +{ + return area2(a, b, c) > eps; +} + +bool norightturn(point a, point b, point c) +{ + return area2(a, b, c) > -eps; +} + +double dist(const point &a, const point &b) +{ + return hypot(a.x - b.x, a.y - b.y); +} + +point intersect(point p, point q, double y) +{ + double dx = q.x - p.x, dy = q.y - p.y; + return point(p.x + dx / dy * (y - p.y), y); +} + +int main() +{ + int n; + while (scanf("%i", &n) == 1 && n != 0) + { + point interv[n + 1][2]; + double f[n + 1][2];// f[i][s] = minimum distance from side s of interval i + point p, e[2]; + scanf("%lf %lf\n", &p.x, &p.y); + interv[0][0] = interv[0][1] = point(p.x, p.y); + for (int i = 1; i <= n; ++i) + { + double y, x1, x2; + scanf("%lf %lf %lf\n", &y, &x1, &x2); + interv[i][0] = point(x1, y); + interv[i][1] = point(x2, y); + } + f[n][0] = f[n][1] = 0; + for (int i = n - 1; i >= 0; --i) + for (int side = 0; side < 2; ++side) + { + point p(interv[i][side]); + e[0] = interv[i + 1][0]; + e[1] = interv[i + 1][1]; + int j; + f[i][side] = infinity; + for (j = i + 1; j <= n; ++j) + { + if (leftturn(p, interv[j][1], e[0]) || leftturn(p, e[1], interv[j][0])) + { + // whole interval to the left or to the right + break; + } + else + { + if (norightturn(p, e[0], interv[j][0])) + { + e[0] = interv[j][0];// trim left endpoint + f[i][side] = min(f[i][side], dist(p, interv[j][0]) + f[j][0]); + } + if (norightturn(p, interv[j][1], e[1])) + { + e[1] = interv[j][1];// trim right endpoint + f[i][side] = min(f[i][side], dist(p, interv[j][1]) + f[j][1]); + } + } + } + assert(j > i + 1); + if (j > n)// reached last interval + { + double y = interv[n][0].y; + e[0] = intersect(p, e[0], y); + e[1] = intersect(p, e[1], y); + double d; + if (p.x > e[1].x) + { + d = dist(p, e[1]); + } + else if (p.x < e[0].x) + { + d = dist(p, e[0]); + } + else + { + d = p.y - e[0].y; + } + f[i][side] = min(f[i][side], d); + } + if (i == 0) + { + break; + } + } + printf("%.11lf\n", f[0][0]); + } + return 0; +} diff --git a/12204.cpp b/12204.cpp new file mode 100644 index 0000000..a4e6ff2 --- /dev/null +++ b/12204.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +// O(linear in the size of the output) + +const int maxn = 13; + +int perm[1 << maxn], inv[1 << maxn]; // current permutation, inverse permutation +char switched[2 * maxn - 1][1 << (maxn - 1)],// state of switches + path[1 << maxn]; // 0 for left path, 1 for right + +void dfs(int v, int r) +{ + path[v] = r; + if (path[v ^ 1] < 0) + { + dfs(v ^ 1, r ^ 1); + } + v = perm[inv[v] ^ 1]; + if (path[v] < 0) + { + dfs(v, r ^ 1); + } +} + +int perfectshuffle(int i, int n) +{ + return ((i & 1) << (n - 1)) | (i >> 1); +} + +/* Solve the routing problem with n nodes, where src[i] is matched to dest[i] + * and switch submatrix starting at (ypos, xpos), and store the state of the switches. */ +void route_all(int n, int ypos, int xpos, const vector &src, const vector &dest) +{ + if (n == 1) + { + switched[ypos][xpos] = src[0] != dest[0]; + return; + } + int xlen = 1 << n, ylen = 2 * n - 1; + /* Write dest as a permutation of src and compute the inverse. + * From now on we pretend the values of src are 0, 1, .., xlen -1. */ + for (int i = 0; i < xlen; ++i) + { + inv[src[i]] = i; + } + for (int i = 0; i < xlen; ++i) + { + perm[i] = inv[dest[i]]; + } + for (int i = 0; i < xlen; ++i) + { + inv[perm[i]] = i; + } + /* Find a bicoloring (to select which messages will go through the left half of the network). + * The order is chosen so that low-numbered switches are turned off if possible. */ + memset(path, -1, sizeof(path)); + for (int i = 0; i < xlen; ++i) + if (path[i] < 0) + { + dfs(i, 0); + } + // Compute new src/dest pairs (1 is the upper part, 2 the lower part). + vector left1, right1; + for (int i = 0; i < xlen; i += 2)// upper part + { + switched[ypos][xpos + i / 2] = path[i]; + for (int j = 0; j < 2; ++j) + { + int x = perfectshuffle((i | j) ^ path[i], n); + if (x < xlen / 2) + { + left1.push_back(i | j); + } + else + { + right1.push_back(i | j); + } + } + } + vector left2(xlen / 2), right2(xlen / 2); + for (int i = 0; i < xlen; i += 2)// lower part + { + int s = switched[ypos + ylen - 1][xpos + i / 2] = path[perm[i]]; + for (int j = 0; j < 2; ++j) + { + int x = perfectshuffle((i | j) ^ s, n); + if (x < xlen / 2) + { + left2[x] = perm[i | j]; + } + else + { + right2[x - xlen / 2] = perm[i | j]; + } + } + } + // Recurse on the left and right parts + route_all(n - 1, ypos + 1, xpos, left1, left2); + route_all(n - 1, ypos + 1, xpos + xlen / 4, right1, right2); +} + +int main() +{ + int n; + bool first = true; + while (scanf("%i", &n) == 1, n) + { + int xlen = 1 << n, ylen = 2 * n - 1; + vector dest(xlen), src(xlen); + for (int i = 0; i < xlen; ++i) + { + src[i] = i; + } + for (int i = 0; i < xlen; ++i) + { + scanf("%i", &dest[i]); + } + route_all(n, 0, 0, src, dest); + if (!first) + { + puts(""); + } + else + { + first = false; + } + for (int y = 0; y < ylen; ++y) + { + for (int x = 0; x < xlen / 2; ++x) + { + putchar('0' + switched[y][x]); + } + puts(""); + } + } + return 0; +} diff --git a/12205.cpp b/12205.cpp index 3629d65..1cc776b 100644 --- a/12205.cpp +++ b/12205.cpp @@ -1,31 +1,82 @@ -#include +#include -struct Phone { - int A, B; -}; +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - while (true) { - int N, M; - scanf("%d%d", &N, &M); - if (!N && !M) break; - Phone P[N]; - for (int i = 0; i < N; i++) { - int U, V, W, X; - scanf("%d%d%d%d", &U, &V, &W, &X); - P[i].A = W; - P[i].B = W + X; +int left_[10048], right_[10048]; + +// FAST integer input +#define X10(n) ((n << 3) + (n << 1)) +#define RI readint +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +// ret true if there is a number waiting to be read, false otherwise +bool adapt() +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +// // + +int main() +{ + for (int n, m; (RI(n) + RI(m) == 2) && (n || m);) + { + int z; + for (int i = 0; i < n; ++i) + { + RI(z), RI(z), RI(left_[i]), RI(right_[i]); + right_[i] += left_[i]; } - while (M--) { - int X, Y, C = 0; - scanf("%d%d", &X, &Y); - Y += X; - for (int i = 0; i < N; i++) - C += (X < P[i].B && Y > P[i].A ); - printf("%d\n", C); + for (int i = 0; i < m; ++i) + { + int ans = 0; + RI(left_[n]), RI(right_[n]); + right_[n] += left_[n]; + for (int j = 0; j < n; ++j) + { + ans += !(left_[n] >= right_[j] || right_[n] <= left_[j]); + } + printf("%d\n", ans); } } return 0; diff --git a/12206.cpp b/12206.cpp new file mode 100644 index 0000000..5d89501 --- /dev/null +++ b/12206.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +const int maxlen = 40000; + +char str[maxlen + 2]; +int n, mintimes; + +// suffix tree ~ Ukkonen +const int sigma = 27; + +#define char2num(c) ((c) ? (c) <= 'Z' ? (c) - 'A' + 27 : (c) - 'a' + 1 : 0) + +struct suftree // 4·(3 + sigma) bytes / node, total 2n + 1 nodes +{ + int ini, fin; // la transición ha sido cad[ini..fin) + suftree *hijo[sigma], *fail;// fail está definido sólo para nodos internos + suftree(int a = 0, int b = 0, suftree *f = 0) : ini(a), fin(b), fail(f) + { + memset(hijo, 0, sizeof(hijo)); + } +}; + +/* Calcula la forma canónica de la cadena dada por el sufijo s concatenado con cad[ini..fin); + * es decir, (s, (ini, fin)) será el antecesor explícito más cercano */ +inline void canonize(const char *cad, suftree *&s, int &ini, int fin) +{ + while (ini < fin) + { + suftree *sp = s->hijo[char2num(cad[ini])]; + if (sp->fin - sp->ini > fin - ini) + { + break; + } + s = sp; + ini += s->fin - s->ini; + } +} + +// Supone que la cadena acaba en 0 (para marcar los sufijos al final) +suftree *hazsuftree(const char *cad, int longit) +{ + suftree *root = new suftree(), *s = root; + int ini = 0, i; + for (i = 0; i <= longit; i++)// <= longit para que cada sufijo acabe en una hoja + { + /* (s, [ini, i)) es la referencia canónica del punto activo; + * es decir, todos los nodos del trie correspondientes a sufijos más largos + * de cad[0..i) son hojas */ + suftree *ant = root, *r;// (ant, [i, i + 1)) es la última nueva letra añadida + int letra = char2num(cad[i]); + for (;;) + { + /* Comprueba si ya existe una transición con la letra cad[i] */ + r = s;// punto donde crear la nueva transición, si hace falta + if (ini < i) + { + int l = char2num(cad[ini]); + suftree *sp = s->hijo[l]; + int k = sp->ini + i - ini; + if (cad[i] == cad[k]) + { + break;// no es necesario partir, ya existe + } + r = new suftree(sp->ini, k); // parte común + r->hijo[char2num(cad[k])] = sp;// sp es la rama anterior + sp->ini = k; + s->hijo[l] = r; + } + else if (s->hijo[letra]) + { + break;// ya existe + } + // Añade una nueva hoja (en realidad esto aumenta en longit - i letras) + r->hijo[letra] = new suftree(i, longit + 1);// el +1 es para la última pasada + if (ant != root) + { + ant->fail = r; + } + ant = r; + if (s->fail) + { + s = s->fail; + } + else if (ini++ == i) + { + break;// si es la raíz, aumenta un carácter + } + canonize(cad, s, ini, i);// se podría poner if (r != s) + } + if (ant != root) + { + ant->fail = r; + } + canonize(cad, s, ini, i + 1); + } + return root; +} + +int longest, best; + +int dfs(suftree *t, int depth = 0) +{ + int children = 0, times = 0; + depth += min(n, t->fin) - t->ini; + for (int i = 0; i < sigma; ++i) + if (t->hijo[i]) + { + times += dfs(t->hijo[i], depth); + ++children; + } + if (children == 0) + { + ++times;// leave = suffix + } + int start = min(n, t->fin) - depth; + if (depth > 0 && times >= mintimes && (depth > longest || depth == longest && start < best)) + { + longest = depth; + best = start; + } + delete t; + return times; +} + +int main() +{ + while (scanf("%i\n", &mintimes), mintimes) + { + gets(str); + n = strlen(str); + reverse(&str[0], &str[n]); + suftree *tree = hazsuftree(str, n); + longest = 0; + dfs(tree); + if (longest == 0) + { + puts("none"); + } + else + { + printf("%i %i\n", longest, n - best - longest); + } + } + return 0; +} diff --git a/12207.cpp b/12207.cpp new file mode 100644 index 0000000..c522f30 --- /dev/null +++ b/12207.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int main() +{ + int p, c; + int cases = 0; + while (scanf("%d %d", &p, &c)) + { + if (p == 0 && c == 0) + { + break; + } + list l; + p = min(c, p); + for (int i = 1; i <= p; i++) + { + l.push_back(i); + } + char ch[10]; + printf("Case %d:\n", ++cases); + for (int i = 0; i < c; i++) + { + scanf("%s", ch); + if (ch[0] == 'N') + { + printf("%d\n", l.front()); + l.push_back(l.front()); + l.pop_front(); + } + else + { + int num; + scanf("%d", &num); + l.remove(num); + l.push_front(num); + } + } + } + return 0; +} diff --git a/12208.cpp b/12208.cpp index 5292e78..9edfc1b 100644 --- a/12208.cpp +++ b/12208.cpp @@ -1,31 +1,43 @@ -#include -#include +#include + using namespace std; map Map; -long long Cal(long long N) { - if (N <= 0) return 0; +long long Cal(long long N) +{ + if (N <= 0) + { + return 0; + } if (Map.find(N) != Map.end()) + { return Map[N]; - + } long long M = N / 2; - if (N & 1) { + if (N & 1) + { Map[N] = 2 * Cal(M) + M + 1; } - else { + else + { Map[N] = Cal(M) + Cal(M - 1) + M; } return Map[N]; } -int main() { +int main() +{ long long A, B, V; - for (int t = 1; ; t++) { + for (int t = 1;; t++) + { scanf("%lld%lld", &A, &B); - if (!A && !B) return 0; - + if (!A && !B) + { + return 0; + } V = Cal(B) - Cal(A - 1); printf("Case %d: %lld\n", t, V); } + return 0; } diff --git a/12209.cpp b/12209.cpp new file mode 100644 index 0000000..8a02390 --- /dev/null +++ b/12209.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +int dp[505][505], c[105], dpp[505], in[505]; +vector v[505]; + +int main() +{ + int T, t, i, j, k, K, N, n, W, w, S, s, x, y; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + scanf(" %d %d %d", &N, &S, &W); + for (i = 0; i <= N; i++) + for (j = 0; j <= N; j++) + { + dp[i][j] = 1e9; + } + for (i = 0; i <= N; i++) + { + v[i].clear(); + } + for (w = 0; w < W; w++) + { + scanf(" %d", &K); + for (k = 0; k < K; k++) + { + scanf(" %d", &i); + v[i].push_back(w); + } + } + for (i = 1; i <= N; i++) + { + memset(c, 0, sizeof(c)); + int all = 0; + for (j = i; j <= i + S && j <= N; j++) + { + for (k = 0; k < v[j].size(); k++) + { + if (c[v[j][k]] == 0) + { + c[v[j][k]] = 1; + all++; + } + } + if (j - i + 1 + all <= S) + { + dp[i][j] = all; + } + else + { + break; + } + } + } + for (i = 0; i <= N; i++) + { + dpp[i] = 1e9; + } + dpp[0] = 0; + queue q; + q.push(0); + memset(in, 0, sizeof(in)); + while (!q.empty()) + { + x = q.front(); + q.pop(); + in[x] = 0; + if (x == N) + { + continue; + } + for (i = x + 1; i <= x + S && i <= N; i++) + { + if ((dpp[x] + dp[x + 1][i] < dpp[i]) && (i - x + dp[x + 1][i] <= S)) + { + dpp[i] = dpp[x] + dp[x + 1][i]; + if (in[i] == 0) + { + in[i] = 1; + q.push(i); + } + } + } + } + if (dpp[N] == 1e9) + { + printf("Case %d: -1\n", t + 1); + } + else + { + printf("Case %d: %d\n", t + 1, dpp[N]); + } + } + return 0; +} diff --git a/1221.cpp b/1221.cpp new file mode 100644 index 0000000..c363e46 --- /dev/null +++ b/1221.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +int s[505][505], pre[505], dis[255][255], human[255], alien[255], hp[255], ap[255], st = 0, ed; +bool stop; + +void dfs(int v) +{ + for (int i = st; i <= ed && stop; i++) + if (s[v][i] && pre[i] == -1) + { + pre[i] = v; + if (i == ed) + { + stop = false; + } + else + { + dfs(i); + } + } +} + +int flow() +{ + int i, j, k, ans = 0; + while (1) + { + memset(pre, -1, sizeof(pre)); + stop = true; + dfs(0); + if (stop) + { + return ans; + } + ans += 1; + for (i = ed; i != st; i = pre[i]) + { + s[pre[i]][i] -= 1; + s[i][pre[i]] += 1; + } + } +} + +int main() +{ + int H, A, h, a, i, j; + long long t1, t2, tmp; + while (~scanf(" %d %d", &H, &A) && (H + A)) + { + for (h = 0; h < H; h++) + { + scanf(" %d %d", &human[h], &hp[h]); + } + for (a = 0; a < A; a++) + { + scanf(" %d %d", &alien[a], &ap[a]); + } + for (h = 0; h < H; h++) + for (a = 0; a < A; a++) + { + scanf(" %d", &dis[h][a]); + } + if (H < A) + { + printf("IMPOSSIBLE\n"); + continue; + } + int low = 0, up = 10000000, mid, ans = -1; + ed = H + A + 1; + while (up > low + 1) + { + mid = (up + low) >> 1; + memset(s, 0, sizeof(s)); + int can = 1; + for (h = 0; h < H; h++) + { + s[st][h + 1] = 1; + for (a = 0; a < A; a++) + { + s[H + a + 1][ed] = 1; + tmp = (long long)alien[a]; + int gap = mid - dis[h][a]; + t1 = (long long)human[h] + (long long)gap * (long long)hp[h]; + t2 = tmp + (long long)mid * (long long)ap[a]; + if (t1 >= t2) + { + s[h + 1][H + a + 1] = 1; + } + else + { + can = 0; + } + } + } + int num = flow(); + if (num == A) + { + up = mid; + ans = up; + } + else + { + low = mid; + } + } + if (ans != -1) + { + printf("%d\n", ans); + } + else + { + printf("IMPOSSIBLE\n"); + } + } + return 0; +} diff --git a/12210.cpp b/12210.cpp new file mode 100644 index 0000000..5d0887e --- /dev/null +++ b/12210.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +const int NN = 10048; +int b[NN], s[NN]; + +int main() +{ + for (int B, S, cnum = 0; scanf("%d %d", &B, &S) == 2 && B;) + { + for (int i = 0; i < B; ++i) + { + scanf("%d", b + i); + } + for (int i = 0; i < S; ++i) + { + scanf("%d", s + i); + } + sort(b, b + B); + if (B <= S) + { + printf("Case %d: 0\n", ++cnum); + } + else + { + printf("Case %d: %d %d\n", ++cnum, B - S, b[0]); + } + } + return 0; +} diff --git a/12211.cpp b/12211.cpp new file mode 100644 index 0000000..ea4d399 --- /dev/null +++ b/12211.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +struct List {char s[5][5];}; +struct List nxt; + +void vanish(int x, int y, char ch) +{ + int op[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, i, j, nx, ny; + nxt.s[x][y] = '-'; + for (i = 0; i < 4; i++) + { + nx = x + op[i][0]; + ny = y + op[i][1]; + if (nx >= 0 && nx < 4 && ny >= 0 && ny < 4 && nxt.s[nx][ny] == ch) + { + vanish(nx, ny, ch); + } + } +} +void fall() +{ + int i, j, k; + for (j = 0; j < 4; j++) + { + for (i = 3; i >= 0; i--) + { + if (nxt.s[i][j] != '-') + { + for (k = i + 1; k < 4; k++) + { + if (nxt.s[k][j] == '-') + { + nxt.s[k][j] = nxt.s[k - 1][j]; + nxt.s[k - 1][j] = '-'; + } + } + } + } + } +} +int AB(struct List now, int lv, int A, int B) +{ + int i, j, re = 0; + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) + if (now.s[i][j] != '-') + { + re = 1; + } + if (re == 0) + { + if (lv % 2 == 0) + { + return -lv; + } + return lv; + } + if (lv % 2 == 0) + { + int stop = 0; + for (i = 3; i >= 0 && stop == 0; i--) + { + for (j = 0; j < 4 && stop == 0; j++) + { + if (now.s[i][j] != '-') + { + nxt = now; + vanish(i, j, now.s[i][j]); + fall(); + A = max(A, AB(nxt, lv - 1, A, B)); + if (B <= A) + { + stop = 1; + } + } + } + } + return A; + } + else + { + int stop = 0; + for (i = 3; i >= 0 && stop == 0; i--) + { + for (j = 0; j < 4 && stop == 0; j++) + { + if (now.s[i][j] != '-') + { + nxt = now; + vanish(i, j, now.s[i][j]); + fall(); + B = min(B, AB(nxt, lv - 1, A, B)); + if (B <= A) + { + stop = 1; + } + } + } + } + return B; + } +} + +int main() +{ + int T, t, i, j; + struct List now; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + gets(now.s[0]); + for (i = 0; i < 4; i++) + { + gets(now.s[i]); + } + int tmp = AB(now, 16, -1e9, 1e9); + if (tmp >= 0) + { + printf("Case %d: win %d\n", t + 1, 16 - tmp); + } + else + { + printf("Case %d: loss %d\n", t + 1, 16 + tmp); + } + } + return 0; +} diff --git a/12212.cpp b/12212.cpp new file mode 100644 index 0000000..54dae2b --- /dev/null +++ b/12212.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +//Md. Khairullah Gaurab +//CSE, SUST 20th +//gaurab.cse.sust@gmail.com + +#define sf scanf +#define pf printf +#define mem(a, b) memset(a, b, sizeof(a)) +#define am [pos][len][lnm][hnm][vall][valh] + +typedef unsigned long long Long; + +Long dp[22][22][2][2][2][2]; +int vis[22][22][2][2][2][2], mxlen, L[22], H[22], cas; + +int getLen(Long x) +{ + int len = 0; + while (x) + { + len++; + x /= 10; + } + return len; +} + +Long rec(int pos, int len, int lnm, int hnm, int vall, int valh) +{ + Long &ret = dp am; + if (vis am == cas) + { + return ret; + } + vis am = cas; + if (pos == mxlen) + { + return ret = (vall && valh); + } + ret = 0; + int nlen, a, b, c, d, st, en; + st = lnm ? 0 : L[pos]; + en = hnm ? 9 : H[pos]; + for (int i = st; i <= en; i++) + { + nlen = (len == 0 and i == 0) ? 0 : len + 1; + a = ((i > L[pos]) | lnm); + b = ((i < H[pos]) | hnm); + c = (!nlen || (vall and i == L[mxlen - 1 - len]) || (i > L[mxlen - 1 - len])); + d = (nlen < pos + 1 || (valh and i == H[mxlen - 1 - len]) || i < H[mxlen - 1 - len]); + ret += rec(pos + 1, nlen, a, b, c, d); + } + return ret; +} + +int main(int argc, const char **argv) +{ + int test; + char s[50]; + Long A, B; + sf("%d", &test); + for (cas = 1; cas <= test; cas++) + { + sf("%llu %llu", &A, &B); + mxlen = getLen(B); + sprintf(s, "%0*llu", mxlen, A); + for (int i = 0; i < mxlen; ++i) + { + L[i] = s[i] - '0'; + } + sprintf(s, "%0*llu", mxlen, B); + for (int i = 0; i < mxlen; ++i) + { + H[i] = s[i] - '0'; + } + pf("Case %d: %llu\n", cas, rec(0, 0, 0, 0, 1, 1)); + } + return 0; +} diff --git a/12213.cpp b/12213.cpp new file mode 100644 index 0000000..5e048fa --- /dev/null +++ b/12213.cpp @@ -0,0 +1,265 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const double EPS = 1e-7; +const double PI = acos(-1); + +template +struct point +{ + T x, y; + point operator+(const point &a) const + { + return (point){ + x + a.x, y + a.y}; + } + point operator-(const point &a) const + { + return (point){ + x - a.x, y - a.y}; + } +}; + +template +point operator*(T c, const point &a) +{ + return (point){ + c * a.x, c * a.y}; +} + +template +bool operator!=(const point &a, const point &b) +{ + return abs(a.x - b.x) > EPS || abs(a.y - b.y) > EPS; +} + +template +T dot(const point &a, const point &b) +{ + return a.x * b.x + a.y * b.y; +} + +template +T cross(const point &a, const point &b) +{ + return a.x * b.y - a.y * b.x; +} + +template +double abs(const point &a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} + +template +double arg(const point &a) +{ + double t = atan2(a.y, a.x); + return t < 0 ? t + 2 * PI : t; +} + +enum +{ + CCW = 1, + CW = -1, + ON = 0 +}; +int ccw(const point &a, const point &b, const point &c) +{ + double rdir = cross(b - a, c - a); + if (rdir > EPS) + { + return CCW; + } + if (rdir < -EPS) + { + return CW; + } + return ON; +} + +template +struct line +{ + point a, b; +}; + +template +struct segment +{ + point a, b; + operator line() const + { + return (line){ + a, b}; + } +}; + +template +struct circle +{ + point c; + T r; +}; + +int tangent(const circle &C, const point &p, vector> &res) +{ + double a = abs(p - C.c); + if (a < C.r - EPS) + { + return 0; + } + else if (a < C.r + EPS) + { + res.push_back(p); + return 1; + } + else + { + double b = sqrt(a * a - C.r * C.r); + double psi = arg(p - C.c), phi = PI - acos(b / a); + res.push_back(p + b * (point){cos(psi + phi), sin(psi + phi)}); + res.push_back(p + b * (point){cos(psi - phi), sin(psi - phi)}); + return 2; + } +} + +bool intersect(const line &L, const segment &S) +{ + return cross(L.b - L.a, S.a - L.a) * cross(L.b - L.a, S.b - L.a) < EPS; +} + +point get_intersect(const line &L1, const line &L2) +{ + double a1 = cross(L1.b - L1.a, L2.b - L2.a); + double a2 = cross(L1.b - L1.a, L1.b - L2.a); + if (abs(a1) < EPS) + { + return L1.a; + } + return L2.a + a2 / a1 * (L2.b - L2.a); +} + +template +double dist(const point &a, const point &b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +template +double dist(const segment &S, const point &p) +{ + if (dot(S.b - S.a, p - S.a) <= 0) + { + return dist(p, S.a); + } + if (dot(S.a - S.b, p - S.b) <= 0) + { + return dist(p, S.b); + } + return abs(cross(S.b - S.a, p - S.a)) / dist(S.a, S.b); +} + +bool intersect(const circle &C, const segment &S) +{ + return dist(S, C.c) < C.r + EPS; +} + +struct event +{ + int type, i; + point p; + int io; + bool operator<(const event &e) const + { + if (type != e.type) + { + return type < e.type; + } + if (type == 0) + { + return p.x + EPS < e.p.x || abs(p.x - e.p.x) < EPS && io < e.io; + } + if (type == 1) + { + return p.y + EPS < e.p.y || abs(p.y - e.p.y) < EPS && io < e.io; + } + if (type == 2) + { + return p.x - EPS > e.p.x || abs(p.x - e.p.x) < EPS && io < e.io; + } + if (type == 3) + { + return p.y - EPS > e.p.y || abs(p.y - e.p.y) < EPS && io < e.io; + } + assert(0); + } +}; + +int main() +{ + const point O = {0, 0}; + for (int cas = 1, h, w, r, cx, cy, n; scanf("%d%d%d%d%d%d", &h, &w, &r, &cx, &cy, &n), h; cas++) + { + static point P[10000]; + rep(i, n) scanf("%lf%lf\n", &P[i].x, &P[i].y); + circle C = {(point){cx, cy}, r}; + segment B, R, T, L; + L.b = B.a = (point){0, 0}; + B.b = R.a = (point){w, 0}; + R.b = T.a = (point){w, h}; + T.b = L.a = (point){0, h}; + vector E; + rep(i, n) + { + vector> Q; + tangent(C, P[i], Q); + rep(j, Q.size()) if (P[i] != Q[j]) + { + line M = {P[i], Q[j]}; + point q; + if (intersect(M, B)) + { + q = get_intersect(B, M), E.push_back((event){0, i, q, ccw(q, Q[j], C.c)}); + } + if (intersect(M, R)) + { + q = get_intersect(R, M), E.push_back((event){1, i, q, ccw(q, Q[j], C.c)}); + } + if (intersect(M, T)) + { + q = get_intersect(T, M), E.push_back((event){2, i, q, ccw(q, Q[j], C.c)}); + } + if (intersect(M, L)) + { + q = get_intersect(L, M), E.push_back((event){3, i, q, ccw(q, Q[j], C.c)}); + } + } + } + sort(E.begin(), E.end()); + int cnt = 0; + rep(i, n) if (!intersect(C, (segment){O, P[i]})) cnt++; + int ans = cnt; + rep(i, E.size()) + { + event e = E[i]; + if (intersect(C, (segment){e.p, P[e.i]})) + { + if (e.io == CCW) + { + cnt++; + } + else + { + cnt--; + } + } + ans = max(ans, cnt); + } + printf("Case %d: %d\n", cas, ans); + } + return 0; +} diff --git a/12214.cpp b/12214.cpp new file mode 100644 index 0000000..1d27e4d --- /dev/null +++ b/12214.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:16777216") + +typedef long long LL; +const int N = 2005; + +const double eps = 1e-6, pi = acos(-1.0); +inline int dcmp(double x) +{ + return (x > eps) - (x < -eps); +} + +struct Point +{ + double x, y; + Point() + { + x = y = 0; + } + Point(double _x, double _y) + { + x = _x, y = _y; + } + void input() + { + scanf("%lf%lf", &x, &y); + } + void output() + { + printf("(%f,%f)", x + eps, y + eps); + } + bool operator<(const Point &R) const + { + if (dcmp(x - R.x) == 0) + { + return dcmp(y - R.y) < 0; + } + return dcmp(x - R.x) < 0; + } + bool operator==(const Point &R) const + { + return dcmp(x - R.x) == 0 && dcmp(y - R.y) == 0; + } + Point operator+(const Point &R) const + { + return Point(x + R.x, y + R.y); + } + Point operator-(const Point &R) const + { + return Point(x - R.x, y - R.y); + } + Point operator*(const double &R) const + { + return Point(x * R, y * R); + } + Point operator/(const double &R) const + { + return Point(x / R, y / R); + }// Cross Product + double operator^(const Point &R) const + { + return x * R.y - y * R.x; + }// dot + double operator%(const Point &R) const + { + return x * R.x + y * R.y; + } + double len() + { + return sqrt(*this % *this); + } +}; +Point Rotate(Point A, double rad) +{ + double Sin = sin(rad), Cos = cos(rad); + return Point(A.x * Cos - A.y * Sin, A.x * Sin + A.y * Cos); +} +Point Normal(Point A) +{ + double L = A.len(); + return Point(-A.y / L, A.x / L); +} +double GetLineIntersection(Point P, Point v, Point Q, Point w) +{ + Point u = P - Q; + assert(dcmp(v ^ w) != 0); + return (w ^ u) / (v ^ w); +} +bool OnSegment(Point P, Point a1, Point a2) +{ + return dcmp((a1 - P) ^ (a2 - P)) == 0 && dcmp((a1 - P) % (a2 - P)) <= 0; +} + +int n, m, K, ca; +double arc, g[N][N], d[N]; +bool f[N]; +Point a[N]; + +void work() +{ + int i, j, k, x; + arc = asin(1.0 / K); + printf("Case %d: ", ++ca); + for (i = 0; i <= n; ++i) + { + a[i].input(); + } + m = n + 1; + for (i = 0; i <= n; ++i) + { + for (j = 0; j < n; ++j) + { + Point Vec = Normal(a[j + 1] - a[j]); + Point Vl = Rotate(Vec, arc), Vr = Rotate(Vec, -arc); + double X = GetLineIntersection(a[j], a[j + 1] - a[j], a[i], Vl); + double Y = GetLineIntersection(a[j], a[j + 1] - a[j], a[i], Vr); + if (0 < dcmp(X - 0) && dcmp(X - 1) < 0) + { + a[m++] = a[j] + (a[j + 1] - a[j]) * X; + } + if (0 < dcmp(Y - 0) && dcmp(Y - 1) < 0) + { + a[m++] = a[j] + (a[j + 1] - a[j]) * Y; + } + } + } + for (i = 0; i < m; ++i) + { + for (j = 0; j < m; ++j) + { + g[i][j] = (a[j] - a[i]).len(); + for (k = 0; k < n; ++k) + { + if (OnSegment(a[i], a[k], a[k + 1]) && OnSegment(a[j], a[k], a[k + 1])) + { + if ((a[i] - a[k]).len() < (a[j] - a[k]).len()) + { + break; + } + } + } + if (k >= n) + { + g[i][j] *= K; + } + } + } + // for (i = 0; i < m; ++ i) { + // for (j = 0; j < m; ++ j) + // printf("%3.3f%c" , g[i][j] , " \n"[j + 1 == m]); + // } + for (i = 0; i < m; ++i) + { + d[i] = 1e60, f[i] = 0; + } + d[0] = 0; + while (1) + { + x = -1; + for (i = 0; i < m; ++i) + if (!f[i] && (!~x || d[i] < d[x])) + { + x = i; + } + if (!~x) + { + break; + } + f[x] = 1; + for (i = 0; i < m; ++i) + if (!f[i]) + { + d[i] = min(d[i], d[x] + g[x][i]); + } + } + printf("%f\n", d[n]); +} + +int main() +{ + while (scanf("%d%d", &n, &K), n || K) + { + work(); + } + return 0; +} diff --git a/12215.cpp b/12215.cpp new file mode 100644 index 0000000..c7e08cf --- /dev/null +++ b/12215.cpp @@ -0,0 +1,266 @@ +#include + +using namespace std; + +// 1,2,...,N: to form a matrix of R * N by stacking R stage permutation of N +// Find a single matrix when the maximum value of R. +// However, the matrix have to satisfy the following constraints: +// D[i][j] := (the number of j placed on the i-th column) +// E[i][j] := max(D[i][j]-C[i][j],0) +// and +// - With all i, Σ_j E[i][j] <= A[i] +// - In all j, Σ_i E[i][j] <= B[j] +// I flow to the flow numbers from column +// Send double each vertex representing a column vertices of digits, +// divided into parts which may or may not overflowing C + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const int INF = 1 << 29; + +// MAXFLOW // +const int V_MAX = 4 * 30 + 2; +const int E_MAX = 4 * 30 + 2 * 30 * 30; + +template +struct graph +{ + int n, m, head[V_MAX], next[2 * E_MAX], to[2 * E_MAX]; + T capa[2 * E_MAX], flow[2 * E_MAX]; + + void init(int N) + { + n = N; + m = 0; + rep(u, n) head[u] = -1; + } + + void add_directed_edge(int u, int v, T ca) + { + next[m] = head[u]; + head[u] = m; + to[m] = v; + capa[m] = ca; + flow[m] = 0; + m++; + next[m] = head[v]; + head[v] = m; + to[m] = u; + capa[m] = 0; + flow[m] = 0; + m++; + } +}; + +int layer[V_MAX], now[V_MAX]; + +template +bool make_layer(const graph &G, int s, int t) +{ + int n = G.n; + rep(u, n) layer[u] = (u == s ? 0 : -1); + int head = 0, tail = 0; + static int Q[V_MAX]; + Q[tail++] = s; + while (head < tail && layer[t] == -1) + { + int u = Q[head++]; + for (int e = G.head[u]; e != -1; e = G.next[e]) + { + int v = G.to[e]; + T capa = G.capa[e], flow = G.flow[e]; + if (capa - flow > 0 && layer[v] == -1) + { + layer[v] = layer[u] + 1; + Q[tail++] = v; + } + } + } + return layer[t] != -1; +} + +template +T augment(graph &G, int u, int t, T water) +{ + if (u == t) + { + return water; + } + for (int &e = now[u]; e != -1; e = G.next[e]) + { + int v = G.to[e]; + T capa = G.capa[e], flow = G.flow[e]; + if (capa - flow > 0 && layer[v] > layer[u]) + { + T w = augment(G, v, t, min(water, capa - flow)); + if (w > 0) + { + G.flow[e] += w; + G.flow[e ^ 1] -= w; + return w; + } + } + } + return 0; +} + +template +T Dinic(graph &G, int s, int t) +{ + int n = G.n; + T ans = 0; + while (make_layer(G, s, t)) + { + rep(u, n) now[u] = G.head[u]; + for (T water = 1; water > 0; ans += water) + { + water = augment(G, s, t, INF); + } + } + return ans; +} + +// BIPARTITE MATCHING // +bool augment(int u, bool *vis, int match[2][V_MAX], const vector *G) +{ + if (u == -1) + { + return true; + } + rep(i, G[u].size()) + { + int v = G[u][i]; + if (!vis[v]) + { + vis[v] = true; + if (augment(match[1][v], vis, match, G)) + { + match[0][u] = v; + match[1][v] = u; + return true; + } + } + } + return false; +} + +int match[2][V_MAX]; +int bipartite_matching(int L, int R, const vector *G) +{ + rep(u, L) match[0][u] = -1; + rep(v, R) match[1][v] = -1; + int res = 0; + static bool vis[V_MAX]; + rep(u, L) + { + rep(v, R) vis[v] = false; + if (augment(u, vis, match, G)) + { + res++; + } + } + return res; +} +// LIBRARY END // + +void solve() +{ + int n; + scanf("%d", &n); + int A[30], B[30], C[30][30]; + rep(i, n) scanf("%d", A + i); + rep(i, n) scanf("%d", B + i); + rep(i, n) rep(j, n) scanf("%d", C[i] + j); + int s = 4 * n, t = s + 1; + static graph G; + int lo = 0, hi = 150; + while (lo < hi) + { + int mi = (lo + hi + 1) / 2, R = mi; + G.init(4 * n + 2); + rep(i, n) + { + G.add_directed_edge(s, i, R); + G.add_directed_edge(3 * n + i, t, R); + G.add_directed_edge(i, n + i, A[i]); + G.add_directed_edge(2 * n + i, 3 * n + i, B[i]); + } + rep(i, n) rep(j, n) + { + G.add_directed_edge(i, 3 * n + j, C[i][j]); + G.add_directed_edge(n + i, 2 * n + j, R); + } + if (Dinic(G, s, t) == n * R) + { + lo = mi; + } + else + { + hi = mi - 1; + } + } + int R = lo; + G.init(4 * n + 2); + rep(i, n) + { + G.add_directed_edge(s, i, R); + G.add_directed_edge(3 * n + i, t, R); + G.add_directed_edge(i, n + i, A[i]); + G.add_directed_edge(2 * n + i, 3 * n + i, B[i]); + } + rep(i, n) rep(j, n) + { + G.add_directed_edge(i, 3 * n + j, C[i][j]); + G.add_directed_edge(n + i, 2 * n + j, R); + } + Dinic(G, s, t); + // recovered ranks + int D[30][30] = {};// D in question + rep(u, 2 * n) for (int e = G.head[u]; e != -1; e = G.next[e]) + { + int v = G.to[e]; + if (u < n) + { + if (3 * n <= v && v < 4 * n) + { + D[u][v - 3 * n] += G.flow[e]; + } + } + else + { + if (2 * n <= v && v < 3 * n) + { + D[u - n][v - 2 * n] += G.flow[e]; + } + } + } + printf("%d\n", R); + while (R--) + { + //Go to restore step-by-step to seek the perfect matching of the bipartite graph + //Unknown why R times worth it be restored properly in this + vector BG[30]; + rep(i, n) rep(j, n) if (D[i][j] > 0) + { + BG[i].push_back(j); + } + bipartite_matching(n, n, BG); + rep(i, n) + { + int j = match[0][i]; + D[i][j]--; + printf("%d%c", j, i < n - 1 ? ' ' : '\n'); + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + printf("Case %d: ", cas), solve(); + } + return 0; +} diff --git a/12216.cpp b/12216.cpp new file mode 100644 index 0000000..6c4b32d --- /dev/null +++ b/12216.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +const int mod = 100000007; +typedef long long ll; +map prime_factor(int a) +{ + map res; + for (int i = 2; i * i <= a; i++) + { + while (a % i == 0) + { + a /= i; + res[i]++; + } + } + if (a != 1) + { + res[a]++; + } + return res; +} + +int main() +{ + int N, M, T; + int cs = 1; + while (cin >> N >> M >> T && (N | M | T)) + { + map m = prime_factor(N); + ll res = 1; + ll b = 1; + for (map::iterator it = m.begin(); + it != m.end(); it++) + { + it->second *= M; + res = (res * (1 + (it->second) / T)) % mod; + // ll a=(it->second+1) + // -((it->second/T)-(it->second/(T+1))); + ll a = (it->second / (T + 1) + 1); + b = (b * a) % mod; + } + res = (res - b + mod) % mod; + cout << "Case " << cs++ << ": "; + cout << res << endl; + } + return 0; +} diff --git a/12217.cpp b/12217.cpp new file mode 100644 index 0000000..972d09d --- /dev/null +++ b/12217.cpp @@ -0,0 +1,214 @@ +#include + +using namespace std; + +typedef long long ll; +typedef unsigned long long ull; +static const double EPS = 1e-9; +static const double PI = acos(-1.0); + +#define REP(i, n) for (int i = 0; i < (int)(n); i++) +#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) +#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) +#define FORIT(i, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) +#define DEC(i, s) for (int i = (s); i >= 0; i--) + +#define SIZE(v) (int)((v).size()) +#define MEMSET(v, h) memset((v), h, sizeof(v)) +#define FIND(m, w) ((m).find(w) != (m).end()) + +int lsize, rsize; +int prime[210]; +int factor[2][210]; +int q[30][50]; +int revq[30][50]; +int qsize; +int iniplus; +map memo; + +void printFactor() +{ + REP(i, lsize) + { + cout << factor[0][i] << " "; + } + cout << endl; + REP(i, rsize) + { + cout << factor[1][i] << " "; + } + cout << endl; +} + +inline ull nextSet(ull x) +{ + ull smallest = x & -x; + ull ripple = x + smallest; + ull new_smallest = ripple & -ripple; + ull ones = ((new_smallest / smallest) >> 1) - 1; + return ripple | ones; +} + +inline int bsf(int n) +{ + int z; + __asm__("bsf %1, %0;" + : "=r"(z) + : "r"(n)); + return z; +} + +int calc(int depth, ull use, int minsize) +{ + if (memo.find(use) != memo.end()) + { + return memo[use]; + } + int size = 0; + REP(i, qsize) + { + if ((use >> i) & 1) + { + continue; + } + q[depth + 1][size] = q[0][i]; + revq[depth + 1][size++] = i; + } + int ret = max(0, size - 1); + FOREQ(i, minsize, size / 2) + { + int pexpect = 2 * ((qsize - size) / 3) + (qsize - size) % 3; + int expect = (i - 1) * (size / i) + size % i; + if (expect + pexpect + iniplus >= 20) + { + break; + } + if (ret <= expect) + { + break; + } + for (int bits = (1LL << i) - 1; bits < (1LL << size); bits = nextSet(bits)) + { + ull v = bits; + int sum = 0; + ull nuse = 0; + while (v) + { + int index = bsf(v); + sum += q[depth + 1][index]; + nuse |= 1LL << revq[depth + 1][index]; + v -= v & -v; + } + if (sum == 0) + { + ret = min(ret, calc(depth + 1, nuse | use, i) + i - 1); + if (ret <= expect) + { + break; + } + } + } + } + return memo[use] = ret; +} + +int main() +{ + MEMSET(prime, -1); + for (int i = 2; i <= 200; i++) + { + if (prime[i] != -1) + { + continue; + } + for (int j = i; j <= 200; j += i) + { + prime[j] = i; + } + } + int test; + int test_case = 0; + scanf("%d", &test); + while (test--) + { + test_case++; + MEMSET(factor, 0); + REP(iter, 2) + { + int n; + scanf("%d", &n); + REP(i, n) + { + int num; + scanf("%d", &num); + while (num != 1) + { + int f = prime[num]; + num /= f; + factor[iter][f]++; + } + } + } + FOREQ(i, 2, 200) + { + //cout << factor[0][i] << " " << factor[1][i] << endl; + int m = min(factor[0][i], factor[1][i]); + factor[0][i] -= m; + factor[1][i] -= m; + } + iniplus = 0; + FOREQ(i, 2, 200) + { + if (factor[0][i] == 0) + { + continue; + } + FOREQ(j, 2, 200) + { + if (factor[1][j] == 0) + { + continue; + } + if (factor[0][i] == factor[1][j]) + { + factor[0][i] = 0; + factor[1][j] = 0; + iniplus++; + } + } + } + sort(factor[0], factor[0] + 200); + sort(factor[1], factor[1] + 200); + reverse(factor[0], factor[0] + 200); + reverse(factor[1], factor[1] + 200); + lsize = 0; + rsize = 0; + int lnum = 0; + int rnum = 0; + qsize = 0; + while (factor[0][lsize] != 0) + { + q[0][qsize++] = factor[0][lsize]; + lnum += factor[0][lsize++]; + } + while (factor[1][rsize] != 0) + { + q[0][qsize++] = -factor[1][rsize]; + rnum += factor[1][rsize++]; + } + int ans = 20; + memo.clear(); + if (lnum == rnum && qsize < 40 - 2 * iniplus) + { + ans = calc(0, 0, 3) + iniplus; + } + if (ans >= 20) + { + printf("Case %d: -1\n", test_case); + } + else + { + printf("Case %d: %d\n", test_case, ans); + } + } +} diff --git a/12218.cpp b/12218.cpp new file mode 100644 index 0000000..2818305 --- /dev/null +++ b/12218.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +bool is[10000005], visit[10000005]; +char s[10]; +int a[10], ans, N, lim; + +void pri() +{ + memset(is, false, sizeof(is)); + int i, j; + is[0] = is[1] = true; + for (i = 2; i < 10000000; i++) + if (!is[i]) + for (j = i * 2; j < 10000000; j += i) + { + is[j] = true; + } +} +void brute(int mask, int now) +{ + int i, j; + if (visit[now]) + { + return; + } + visit[now] = true; + if (!is[now]) + { + //printf("%d %d\n",mask,now); + ans++; + } + if (mask == lim) + { + return; + } + //printf("XD%d\n",now); + for (i = 0; i < N; i++) + { + if (mask & (1 << i)) + { + continue; + } + brute(mask | (1 << i), now * 10 + a[i]); + } +} + +int main() +{ + pri(); + int T, i, j; + scanf(" %d", &T); + gets(s); + while (T--) + { + gets(s); + for (i = 0; s[i]; i++) + { + a[i] = s[i] - '0'; + } + ans = 0; + N = strlen(s); + lim = (1 << N) - 1; + //printf("lim = %d\n",lim); + memset(visit, false, sizeof(visit)); + brute(0, 0); + printf("%d\n", ans); + } + return 0; +} diff --git a/12219.cpp b/12219.cpp new file mode 100644 index 0000000..48a2d5f --- /dev/null +++ b/12219.cpp @@ -0,0 +1,172 @@ +#include + +using namespace std; + +#pragma comment(linker, "/STACK:102400000,102400000") + +#define ll __int64 +#define ull unsigned long long +#define eps 1e-8 +#define NMAX 1000000000 +#define MOD 1000000 +#define lson l, mid, rt << 1 +#define rson mid + 1, r, rt << 1 | 1 +#define PI acos(-1) +#define ALL(x) x.begin(), x.end() +#define INS(x) inserter(x, x.end()) +template +inline void scan_d(T &ret) +{ + char c; + int flag = 0; + ret = 0; + while (((c = getchar()) < '0' || c > '9') && c != '-') + ; + if (c == '-') + { + flag = 1; + c = getchar(); + } + while (c >= '0' && c <= '9') + { + ret = ret * 10 + (c - '0'), c = getchar(); + } + if (flag) + { + ret = -ret; + } +} +template +inline T Max(T a, T b) +{ + return a > b ? a : b; +} +template +inline T Min(T a, T b) +{ + return a < b ? a : b; +} +const int maxn = 50000 + 10; +char ch[maxn * 10]; +int lch[maxn], rch[maxn], nct, pos; +char op[maxn][6]; +struct node +{ + char s[6]; + int l, r, wei; + bool operator<(const node &t) const + { + int p = strcmp(s, t.s); + if (p == 0) + { + return l == t.l ? r < t.r : l < t.l; + } + return p > 0 ? 0 : 1; + } +}; +map mp; +vector v; +int flag[maxn], ji; +bool pp; +int ID(node x) +{ + if (mp.find(x) != mp.end()) + { + pp = 1; + return mp[x]; + } + v.push_back(x); + return mp[x] = v.size() - 1; +} + +node make_node(char *s, int l, int r, int wei) +{ + node t; + strcpy(t.s, s); + t.l = l; + t.r = r; + t.wei = wei; + return t; +} + +int build(int u) +{ + int i = 0; + while (ch[pos] >= 'a' && ch[pos] <= 'z') + { + op[u][i++] = ch[pos++]; + } + int qi = ++ji; + op[u][i] = '\0'; + // cout< + +using namespace std; + +#define S(a) scanf("%d", &a) +#define P(a) printf("%d", a) +#define PS(a) printf("%s", a) + +#define S2(a, b) scanf("%d%d", &a, &b) +#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c) + +#define SLL(a) scanf("%lld", &a) +#define PLL(a) printf("%lld", a) +#define SLL2(a, b) scanf("%lld%lld", &a, &b) +#define SLL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c) + +#define PCASE printf("Case %d: ", kk++) +#define PCASENL printf("Case %d:\n", kk++) +#define NL puts("") + +#define sz(a) ((int)a.size()) +#define repv(i, a) for (int i = 0; i < sz(a); i++) +#define revv(i, a) for (int i = sz(a) - 1; i >= 0; i--) +#define rep(i, a, b) for (int i = a; i <= b; i++) +#define rev(i, a, b) for (int i = a; i >= b; i--) +#define reps(i, s) for (int i = 0; s[i]; i++) +#define FOR(I, A, B) for (int I = (A); I < (B); ++I) +#define REP(I, N) FOR(I, 0, N) +#define all(a) a.begin(), a.end() +#define rall(a) a.rbegin(), a.rend() +#define countbit(x) __builtin_popcount(x) +#define READ(f) freopen(f, "r", stdin) +#define WRITE(f) freopen(f, "w", stdout) +#define pb(a) push_back(a) +#define mp(a, b) make_pair(a, b) +#define pi (2.0 * acos(0.0)) +#define PI (2.0 * acos(0.0)) +#define SET(a) memset(a, -1, sizeof a) +#define CLR(a) memset(a, 0, sizeof a) +#define set0(ar) memset(ar, 0, sizeof ar) +#define setinf(ar) memset(ar, 126, sizeof ar) +#define in(a, x, y) (a >= x && a <= y) +#define out(a, x, y) (!in(a, x, y)) +#define eq(a, b) (fabs(a - b) < eps) +#define less(a, b) (a + eps < b) +#define great(a, b) (a > b + eps) +#define xx first +#define yy second + +#define MAX(a) (*max_element(all(a))) +#define MIN(a) (*min_element(all(a))) + +#define LB(a, x) (lower_bound(all(a), x) - a.begin()) +#define UB(a, x) (upper_bound(all(a), x) - a.begin()) + +int ts, kk = 1; + +typedef long long ll; +typedef long long LL; +typedef unsigned long long ull; +typedef vector vi; +typedef vector vvi; +typedef vector vll; +typedef pair pii; +typedef pair pll; +typedef vector vii; + +template +inline T _sq(T a) +{ + return a * a; +} +template +inline T _pow(T a, X y) +{ + T z = 1; + rep(i, 1, y) + { + z *= a; + } + return z; +} + +template +inline T _extended(T a, T b, T &x, T &y) +{ + a = _abs(a); + b = _abs(b); + T g, x1, y1; + if (!b) + { + x = 1; + y = 0; + g = a; + return g; + } + g = _extended(b, a % b, x1, y1); + x = y1; + y = x1 - (a / b) * y1; + return g; +} + +template +inline T ithroot(T x, X i) +{ + return (T)(pow((double)x, 1.0 / (double)i) + (1e-9)); +} + +template +inline bool getbit(T a, X i) +{ + T t = 1; + return ((a & (t << i)) > 0); +} +template +inline T setbit(T a, X i) +{ + T t = 1; + return (a | (t << i)); +} +template +inline T resetbit(T a, X i) +{ + T t = 1; + return (a & (~(t << i))); +} +template +inline T togglebit(T a, X i) +{ + T t = 1; + return (a ^ (t << i)); +} + +template +inline T bigmod(T n, X m, Y mod) +{ + ull ret = 1, a = n % mod; + while (m) + { + if (m & 1) + { + ret = (ret * a) % mod; + } + m >>= 1; + a = (a * a) % mod; + } + ret %= mod; + return (T)ret; +} +template +inline T modinv(T n, Y mod) +{ + return bigmod(n, mod - 2, mod); +} + +template +inline void unify(vector &a) +{ + sort(all(a)); + a.erase(unique(all(a)), a.end()); +} + +/// //CONSTANT// /// +/// power 01234567890123456789 /// +#define MX (200 + 5) +#define MOD 1000000007 +#define inf 1000000000 +#define lim 1000000000 +#define LOGMX 20 +#define eps 1e-9 +///===========================/// + +int in[MX], cost[MX], sum[MX], dp[MX][MX]; + +string s, r; + +map mp; + +vi adj[MX]; + +int dfs(int u) +{ + int &ret = sum[u]; + ret = 1; + repv(i, adj[u]) + { + ret += dfs(adj[u][i]); + } + return ret; +} + +int go(int p, int u, int r) +{ + if (r <= 0) + { + return 0; + } + if (u >= sz(adj[p])) + { + return inf; + } + int &ret = dp[adj[p][u]][r]; + if (ret != -1) + { + return ret; + } + ret = inf; + ret = min(ret, cost[adj[p][u]] + go(p, u + 1, r - sum[adj[p][u]])); + rep(i, 0, r) + { + ret = min(ret, go(adj[p][u], 0, i) + go(p, u + 1, r - i)); + } + return ret; +} + +int main() +{ + int n, m; + while (getline(cin, s) && s[0] != '#') + { + CLR(in); + CLR(cost); + rep(i, 0, MX - 1) adj[i].clear(); + mp.clear(); + istringstream is(s); + is >> n >> m; + rep(i, 1, n) + { + getline(cin, s); + is.clear(); + is.str(s); + string r; + int y; + ll x; + is >> r >> x; + if (!mp.count(r)) + { + mp[r] = sz(mp); + } + y = mp[r]; + cost[y] = x; + while (is >> r) + { + if (!mp.count(r)) + { + mp[r] = sz(mp); + } + adj[y].pb(mp[r]); + in[mp[r]]++; + } + } + rep(i, 1, n) + { + if (!in[i]) + { + dfs(i); + adj[0].pb(i); + } + } + SET(dp); + cout << go(0, 0, m) << endl; + } + return 0; +} diff --git a/12220.cpp b/12220.cpp new file mode 100644 index 0000000..2393db1 --- /dev/null +++ b/12220.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +int c[1000005]; + +int main() +{ + int D, d, N, n, i, j, T, now; + scanf(" %d", &T); + while (T--) + { + scanf(" %d %d", &D, &N); + memset(c, 0, sizeof(c)); + c[0]++; + now = 0; + for (n = 0; n < N; n++) + { + scanf(" %d", &i); + now += i; + now %= D; + c[now]++; + } + long long ans = 0; + for (d = 0; d < D; d++) + { + if (c[d] > 1) + { + ans += (long long)(c[d]) * (long long)(c[d] - 1) / 2; + } + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12222.cpp b/12222.cpp new file mode 100644 index 0000000..d0acaaa --- /dev/null +++ b/12222.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define REP(i, a, b) for (int i = (a); i < (int)(b); i++) +#define clr(a, b) memset(a, b, sizeof(a)) + +const int INF = 0x3f3f3f3f; +int n; +int dp[210][210][2]; +struct Car +{ + int arr, us; +} car[2][210]; +int ta, tb; + +int main() +{ + int cas; + scanf("%d", &cas); + while (cas--) + { + scanf("%d", &n); + ta = tb = 0; + char s[2]; + int a, b; + while (n--) + { + scanf("%s%d%d", s, &a, &b); + if (s[0] == 'A') + { + ta++; + car[0][ta].arr = a; + car[0][ta].us = b; + } + else + { + tb++; + car[1][tb].arr = a; + car[1][tb].us = b; + } + } + int bg, ed; + clr(dp, INF); + dp[0][0][0] = dp[0][0][1] = 0; + for (int i = 0; i <= ta; i++) + { + for (int j = 0; j <= tb; j++) + { + bg = dp[i][j][1], ed = 0; + for (int k = i + 1; k <= ta; k++) + { + bg = max(bg, car[0][k].arr); + ed = max(ed, bg + car[0][k].us); + dp[k][j][0] = min(dp[k][j][0], ed); + bg += 10; + ed += 10; + } + + bg = dp[i][j][0], ed = 0; + for (int k = j + 1; k <= tb; k++) + { + bg = max(bg, car[1][k].arr); + ed = max(ed, bg + car[1][k].us); + dp[i][k][1] = min(dp[i][k][1], ed); + bg += 10; + ed += 10; + } + } + } + printf("%d\n", min(dp[ta][tb][0], dp[ta][tb][1])); + } + return 0; +} diff --git a/12223.cpp b/12223.cpp new file mode 100644 index 0000000..f684874 --- /dev/null +++ b/12223.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define esp 1e-6 +#define pi acos(-1.0) +#define inf 0x0f0f0f0f +#define pb push_back +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 +#define lowbit(x) (x & (-x)) +#define mp(a, b) make_pair((a), (b)) +#define bit(k) (1 << (k)) +#define in freopen("solve_in.txt", "r", stdin); +#define out freopen("solve_out.txt", "w", stdout); + +#define bug puts("********))))))"); +#define inout in out + +#define SET(a, v) memset(a, (v), sizeof(a)) +#define SORT(a) sort((a).begin(), (a).end()) +#define REV(a) reverse((a).begin(), (a).end()) +#define READ(a, n) \ + { \ + REP(i, n) \ + cin >> (a)[i]; \ + } +#define REP(i, n) for (int i = 0; i < (n); i++) +#define Rep(i, base, n) for (int i = base; i < n; i++) +#define REPS(s, i) for (int i = 0; s[i]; i++) +#define pf(x) ((x) * (x)) +#define mod(n) ((n)) +#define Log(a, b) (log((double)b) / log((double)a)) +#define Srand() srand((int)time(0)) +#define random(number) (rand() % number) +#define random_range(a, b) (int)(((double)rand() / RAND_MAX) * (b - a) + a) + +typedef long long LL; +typedef unsigned long long ULL; +typedef vector VI; +typedef pair PII; +typedef vector VII; +typedef vector VIII; +typedef VI::iterator IT; +typedef map Mps; +typedef map Mpi; +typedef map Mpii; +typedef map Mpiii; + +const int maxn = 50000 + 100; +VII g[maxn]; +LL dp[maxn], cnt[maxn], num[maxn], Max; +int n, m; + +void dfs(int u, int fa) +{ + dp[u] = 0; + cnt[u] = num[u]; + REP(i, g[u].size()) + { + int v = g[u][i].first; + int d = g[u][i].second; + if (v == fa) + { + continue; + } + dfs(v, u); + cnt[u] += cnt[v]; + dp[u] += dp[v] + (LL)cnt[v] * d; + } +} + +void dfs1(int u, int fa) +{ + REP(i, g[u].size()) + { + int v = g[u][i].first; + int d = g[u][i].second; + if (v == fa) + { + continue; + } + dp[v] += (dp[u] - dp[v] - (LL)d * cnt[v]) + (LL)d * (cnt[1] - cnt[v]); + Max = min(Max, dp[v]); + dfs1(v, u); + } +} + +int main() +{ + for (int T, t = scanf("%d", &T); t <= T; t++) + { + REP(i, maxn) + g[i].clear(), + cnt[i] = num[i] = 0; + scanf("%d", &n); + REP(i, n - 1) + { + int u, v, d; + scanf("%d%d%d", &u, &v, &d); + g[u].pb(mp(v, d)); + g[v].pb(mp(u, d)); + } + scanf("%d", &m); + REP(i, m) + { + int u; + scanf("%d", &u); + scanf("%d", &num[u]); + } + dfs(1, 1); + Max = dp[1]; + dfs1(1, 1); + int ok = 0; + printf("%lld\n", Max * 2); + Rep(i, 1, n + 1) + { + if (Max == dp[i]) + { + if (ok) + { + putchar(' '); + } + printf("%d", i); + ok = 1; + } + } + puts(""); + } + return 0; +} diff --git a/12225.cpp b/12225.cpp new file mode 100644 index 0000000..fea762c --- /dev/null +++ b/12225.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +struct Catan +{ + struct Pos + { + int q, r; + Pos operator+(const Pos &rhs) const + { + return {q + rhs.q, r + rhs.r}; + } + void operator+=(const Pos &rhs) + { + q += rhs.q; + r += rhs.r; + } + bool operator<(const Pos &rhs) const + { + return make_tuple(q, r) < make_tuple(rhs.q, rhs.r); + } + }; + enum Dir + { + N = 0, + NW, + SW, + S, + SE, + NE, + LENGTH + }; + Catan(int N) + { + idx_kind_.reserve(N); + add({0, 0}, 0); + Pos pos = {0, 0}; + int side = 1; + while (idx_kind_.size() < N) + { + pos += dirs_[NE]; + add(pos, get_kind(pos)); + for (const auto &dir : dirs_) + { + int c = &dir == &dirs_[0] ? side - 1 : side; + for (int s = 0; s < c; ++s) + { + pos += dir; + add(pos, get_kind(pos)); + } + } + side++; + } + } + int get_kind_for_idx(int i) + { + return idx_kind_[i - 1] + 1; + } + void add(const Pos &p, int kind) + { + int i = idx_kind_.size(); + idx_kind_.push_back(kind); + pos_idx_[p] = i; + cnts_[kind]++; + } + int get_kind(const Pos &p) + { + bool kinds[5] = {true, true, true, true, true}; + for (const auto &dir : dirs_) + { + auto iter = pos_idx_.find(p + dir); + if (iter != pos_idx_.end()) + { + kinds[idx_kind_[iter->second]] = false; + } + } + int kind = -1, cnt; + for (int t = 0; t < 5; ++t) + { + if (!kinds[t]) + { + continue; + } + if (kind == -1 || cnts_[t] < cnt) + { + kind = t; + cnt = cnts_[t]; + } + } + return kind; + } + array dirs_ = {{{0, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 0}, {1, -1}}}; + map pos_idx_; + vector idx_kind_; + int cnts_[5] = {0, 0, 0, 0, 0}; +}; + +int main() +{ + Catan cat(10000); + int T; + scanf("%d", &T); + while (T--) + { + int i; + scanf("%d", &i); + printf("%d\n", cat.get_kind_for_idx(i)); + } + return 0; +} diff --git a/12226.cpp b/12226.cpp new file mode 100644 index 0000000..fdd49a1 --- /dev/null +++ b/12226.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +const double eps = 1e-10; + +typedef struct List +{ + double x, y, deg; + int id; +} LIST; +LIST a[2005]; + +int com(const void *aa, const void *bb) +{ + LIST c, d; + c = *(LIST *)(aa); + d = *(LIST *)(bb); + if (c.deg - d.deg > eps) + { + return 1; + } + else if (c.deg - d.deg < -eps) + { + return -1; + } + return 0; +} + +int main() +{ + int T, N, n, i, j; + double cx, cy; + scanf(" %d", &T); + while (T--) + { + scanf(" %d", &N); + cx = cy = 0.0000; + for (n = 0; n < N; n++) + { + scanf(" %lf %lf", &a[n].x, &a[n].y); + a[n].id = n; + cx += a[n].x; + cy += a[n].y; + } + cx /= (double)N; + cy /= (double)N; + for (n = 0; n < N; n++) + { + a[n].deg = atan2((a[n].y - cy), (a[n].x - cx)); + } + qsort(a, N, sizeof(a[0]), com); + for (n = 0; n < N; n++) + { + if (n == 0) + { + printf("%d", a[n].id); + } + else + { + printf(" %d", a[n].id); + } + } + printf("\n"); + } + return 0; +} diff --git a/12227.cpp b/12227.cpp new file mode 100644 index 0000000..3c5f238 --- /dev/null +++ b/12227.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +#define MAXN 128 +#define INF (1LL << 60) + +struct Pt3D +{ + long long x, y, z; + Pt3D(long long a = 0, long long b = 0, long long c = 0) + : x(a), y(b), z(c) + { + } + void read() + { + scanf("%lld%lld%lld", &x, &y, &z); + } + long long dist(const Pt3D &a) const + { + return ceil(sqrt((a.x - x) * (a.x - x) + (a.y - y) * (a.y - y) + (a.z - z) * (a.z - z))); + } +}; + +struct Edge +{ + int to; + long long t, d; + Edge(int a = 0, long long b = 0, long long c = 0) + : to(a), t(b), d(c) + { + } + bool operator<(const Edge &a) const + { + return d < a.d; + } +}; + +int inq[MAXN], cnt[MAXN], pre[MAXN], neg[MAXN], used[MAXN], cases = 0; +long long dist[MAXN], preTime[MAXN]; +vector g[MAXN]; + +void goto_neg_cycle(int st, int n, deque &Q, int inq[]) +{ + for (int i = 0; i < n; i++) + { + cnt[i] = 0; + neg[i] = 0; + } + int u; + Q.push_front(st); + while (!Q.empty()) + { + u = Q.front(); + Q.pop_front(); + inq[u] = 0; + for (int i = 0; i < g[u].size(); i++) + { + long long cost = g[u][i].d; + if (g[u][i].t > dist[u]) + {// !exists(wormholes) + cost = g[u][i].d + g[u][i].t - dist[u]; + } + if (dist[g[u][i].to] > dist[u] + cost) + { + dist[g[u][i].to] = dist[u] + cost; + pre[g[u][i].to] = u; + preTime[g[u][i].to] = g[u][i].t; + if (!inq[g[u][i].to]) + { + if (++cnt[g[u][i].to] > n + 1) + { + cases++; + cnt[g[u][i].to] = 1; + int st = g[u][i].to, ok = 0, disappear; + long long mx; + for (int j = pre[st]; j != -1; j = pre[j]) + { + if (used[j] == cases) + { + ok = 1; + st = j; + break; + } + used[j] = cases; + } + mx = preTime[st]; + disappear = st; + for (int j = pre[st]; j != -1; j = pre[j]) + { + if (j == st) + { + break; + } + if (preTime[j] > mx) + { + mx = preTime[j]; + disappear = j; + } + } + if (ok) + { + if (dist[disappear] > mx) + { + dist[disappear] = mx; + if (Q.size() && dist[Q.front()] > dist[disappear]) + { + Q.push_front(disappear); + } + else + { + Q.push_back(disappear); + } + } + } + } + if (!inq[g[u][i].to]) + { + inq[g[u][i].to] = 1; + if (Q.size() && dist[Q.front()] > dist[g[u][i].to]) + { + Q.push_front(g[u][i].to); + } + else + { + Q.push_back(g[u][i].to); + } + } + } + } + } + } +} + +void spfa(int st, int ed, int n) +{ + for (int i = 0; i < n; i++) + { + pre[i] = -1; + } + int inq[MAXN]; + for (int i = 0; i < n; i++) + { + dist[i] = INF; + inq[i] = 0; + } + int u; + deque Q; + dist[st] = 0; + goto_neg_cycle(st, n, Q, inq); + printf("%lld\n", dist[ed]); +} + +int main() +{ + int T, n; + long long t, d; + Pt3D D[MAXN]; + scanf("%d", &T); + while (T--) + { + for (int i = 0; i < MAXN; i++) + { + g[i].clear(); + } + D[0].read(); + D[1].read(); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + D[(1 + i) * 2].read(); + D[(1 + i) * 2 + 1].read(); + scanf("%lld%lld", &t, &d); + g[(1 + i) * 2].push_back(Edge((1 + i) * 2 + 1, t, d)); + } + n = (n + 1) * 2; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + { + continue; + } + g[i].push_back(Edge(j, -INF, D[i].dist(D[j]))); + } + } + spfa(0, 1, n); + } + return 0; +} diff --git a/1223.cpp b/1223.cpp new file mode 100644 index 0000000..71c1fc7 --- /dev/null +++ b/1223.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +//1223 +//Editor +//Dynamic Programming;Longest Common Substring +#define MAX 5001 + +int T[MAX][MAX]; + +int main() +{ + int t; + cin >> t; + t = 0; + string s; + while (cin >> s) + { + int sz = s.size(); + int maxx = 0; + for (int i = 1; i <= sz; i++) + { + for (int j = 1; j <= sz; j++) + { + if (s[i - 1] == s[j - 1] && i != j) + { + maxx = max(maxx, T[i][j] = T[i - 1][j - 1] + 1); + } + else + { + T[i][j] = 0; + } + } + } + cout << maxx << endl; + } + return 0; +} diff --git a/12230.cpp b/12230.cpp new file mode 100644 index 0000000..6ce3264 --- /dev/null +++ b/12230.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int n; +double d; +double p, l, v; +int main() +{ + int ca = 1; + while (~scanf("%d%lf", &n, &d)) + { + if (!n && d == 0) + { + break; + } + while (n--) + { + scanf("%lf%lf%lf", &p, &l, &v); + d -= l; + d += 2 * l / v; + } + printf("Case %d: %.3f\n\n", ca++, d); + } + return 0; +} diff --git a/12231.cpp b/12231.cpp new file mode 100644 index 0000000..8a0cea1 --- /dev/null +++ b/12231.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int T, n, B; + int cases = 0; + while (scanf("%d %d %d", &T, &n, &B) == 3 && T + n + B) + { + double ret = 0, S, P; + for (int i = 0; i < T; i++) + { + scanf("%lf %lf", &S, &P); + ret += S * (100 - P) / 100.0; + } + printf("Case %d: %.2lf\n\n", ++cases, ret / B); + } + return 0; +} + +/* + 6 3 90 + 100.00 90 + 40.40 70 + 60.30 70 + 40.40 80 + 40.40 85 + 40.40 88 + 1 1 56 + 12.34 100 + 0 0 0 + */ diff --git a/12232.cpp b/12232.cpp new file mode 100644 index 0000000..0a704b8 --- /dev/null +++ b/12232.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +#define MX 100005 +#define REP(i, n) for (int i = 0; i < (n); i++) +#define OREP(i, n) for (int i = 1; i <= (n); i++) + +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int UINT; + +int n, m, k, t; + +char cmd[10]; +inline void Getline(char *input) +{ + scanf("%[^\n]s", input); + getchar(); +} +bool ok; +char input[MX * 10]; +int a, b; +int pa[MX], dis[MX], key[MX]; +int find(int a) +{ + if (a == pa[a]) + { + return a; + } + int par = find(pa[a]); + dis[a] = dis[a] ^ dis[pa[a]]; + pa[a] = par; + return par; +} +bool got[MX]; +void query(stringstream &sin) +{ + memset(got, false, sizeof got); + if (!ok) + { + return; + } + sin >> k; + int a; + int ret = 0; + REP(i, k) + { + sin >> a; + int p_a = find(a); + got[p_a] ^= true; + ret ^= dis[a]; + } + REP(i, n) + { + if (got[i]) + { + if (key[i] == -1) + { + ret = -1; + break; + } + ret ^= key[i]; + } + } + if (ret == -1) + { + printf("I don't know.\n"); + } + else + { + printf("%d\n", ret); + } +} +bool insert(int a, int b, int c) +{ + int p_a = find(a), p_b = find(b); + if (p_a == p_b) + { + return (dis[a] ^ dis[b]) == c; + } + dis[p_a] = (dis[a] ^ c); + pa[p_a] = b; + if (key[p_a] != -1 && key[p_b] != -1) + { + return (key[p_a] ^ key[p_b]) == (dis[p_a] ^ dis[b]); + } + else if (key[p_a] != -1) + { + key[p_b] = (dis[b] ^ dis[p_a] ^ key[p_a]); + } + return true; +} +bool insert(int a, int b) +{ + if (key[a] != -1) + { + return key[a] == b; + } + int p = find(a); + if (p != a) + { + if (key[p] != -1) + { + return (key[p] ^ b) == dis[a]; + } + else + { + key[p] = (dis[a] ^ b); + } + } + key[a] = b; + return true; +} +bool insert(stringstream &sin) +{ + int a, b, c; + if (!ok) + { + return ok; + } + sin >> a; + sin >> b; + if (sin >> c) + { + return insert(a, b, c); + } + else + { + return insert(a, b); + } +} +void init() +{ + REP(i, n) + { + pa[i] = i; + dis[i] = 0; + key[i] = -1; + } +} +int main() +{ + int c = 1; + while (1) + { + int cl = 0; + ok = true; + Getline(input); + stringstream sin(input); + sin >> n >> m; + init(); + if (!n) + { + break; + } + printf("Case %d:\n", c++); + REP(i, m) + { + Getline(input); + stringstream sin2(input); + sin2 >> cmd; + if (cmd[0] == 'Q') + { + if (ok) + { + query(sin2); + } + } + else if (cmd[0] != 'I') + { + continue; + } + else if (ok) + { + cl++; + ok = insert(sin2); + if (!ok) + { + printf("The first %d facts are conflicting.\n", cl); + } + } + } + printf("\n"); + } + return 0; +} diff --git a/12233.cpp b/12233.cpp new file mode 100644 index 0000000..a4d6b0d --- /dev/null +++ b/12233.cpp @@ -0,0 +1,186 @@ +#include + +using namespace std; + +const int INF = 100000008; +const string name[4] = {"Y", "H", "L", "M"}; +int sy_jing, xx_su, sy_su, yurun_jing, yurun_shen, shuerguo_shen; +int maxjing[4], maxshen[4], su[4], d1x[4], d2x[4], d1s[4], d2s[4], wad[4]; +int ssd[4], ssq[4], ssp[4], q1[4], q2[4], jing[4], qi[4], shen[4]; +int xxt, syt; +int maxt, who, pos, sudo; +map MAP[13]; +vector ans; + +int dfs1(int t, int j, int q, int s) +{ + if (j <= 0) + { + return -INF; + } + if (t > maxt) + { + return 0; + } + j = min(j, maxjing[who]), q = min(q, 100), s = min(s, maxshen[who]); + long long h = (long long)j * 1000000 + q * 1000 + s; + if (MAP[t].find(h) != MAP[t].end()) + { + return MAP[t][h]; + } + int dj = 0, dq = 0; + if (t % xxt == 0) + { + int cnt = t / xxt; + if (cnt % 4 == pos + 1) + { + dj -= d1x[who]; + dq += q2[who]; + } + else if (cnt % 4 == 0 && cnt > 0) + { + dj -= d2x[who]; + } + } + if (t % syt == 0) + { + int cnt = t / syt; + if (cnt % 4 == pos + 1) + { + dj -= d1s[who]; + dq += q2[who]; + } + else if (cnt % 4 == 0 && cnt > 0) + { + dj -= d2s[who]; + } + } + int ans = -INF; + if (t == maxt) + { + ans = 0; + if (t % sudo != 0) + { + return 0;//return j+dj>0 ? 0 : -INF; + } + if (j - wad[who] > 0) + { + ans = max(ans, wad[who]); + } + if (q - ssq[who] >= 0) + { + int dj2 = ssp[who] == 1 ? -ssd[who] : 0; + if (j + dj2 > 0) + { + ans = max(ans, ssd[who]); + } + } + return MAP[t][h] = ans; + } + else + { + if (t % sudo != 0) + { + return MAP[t][h] = dfs1(t + 1, j + dj, q + dq, s); + } + ans = max(ans, dfs1(t + 1, j + dj, q + dq + q1[who], s)); + ans = max(ans, dfs1(t + 1, j + dj - wad[who], q + dq + q1[who], s) + wad[who]); + if (s >= yurun_shen && j < maxjing[who]) + { + ans = max(ans, dfs1(t + 1, min(j + yurun_jing, maxjing[who]) + dj, q + dq, s - yurun_shen)); + } + if (s < maxshen[who]) + { + ans = max(ans, dfs1(t + 1, j + dj, q + dq, s + shuerguo_shen)); + } + if (q - ssq[who] >= 0) + { + int dj2 = ssp[who] == 1 ? -ssd[who] : 0; + ans = max(ans, dfs1(t + 1, j + dj + dj2, q + dq - ssq[who], s) + ssd[who]); + } + return MAP[t][h] = ans; + } +} +bool ok(int max_time) +{ + //printf("---------round : %d--------------\n",max_time); + int dmg[4][4]; + ans.clear(); + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 3; j++) + { + maxt = max_time, who = i, pos = j, sudo = 5 - su[i]; + for (int k = 0; k < 13; k++) + { + MAP[k].clear(); + } + dmg[i][j] = dfs1(1, jing[i], qi[i], shen[i]); + } + } + /*for(int i=0;i<4;i++){ + for(int j=0;j<3;j++) + printf("%d ",dmg[i][j]); + printf("\n"); + }*/ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (i == j) + { + continue; + } + for (int k = 0; k < 4; k++) + { + if (i == k || j == k) + { + continue; + } + //printf("(%d,%d,%d) = %d\n",i,j,k,dmg[i][0]+dmg[j][1]+dmg[k][2]); + if (dmg[i][0] + dmg[j][1] + dmg[k][2] >= sy_jing) + { + ans.push_back(name[i] + name[j] + name[k]); + } + } + } + } + sort(ans.begin(), ans.end()); + return ans.size() > 0; +} + +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + for (int t = 0; scanf(" %d", &sy_jing) == 1 && sy_jing; t++) + { + scanf(" %d %d %d %d %d", &xx_su, &sy_su, &yurun_jing, &yurun_shen, &shuerguo_shen); + for (int i = 0; i < 4; i++) + { + scanf(" %d %d %d %d %d %d %d", &maxjing[i], &maxshen[i], &su[i], &d1x[i], &d2x[i], &d1s[i], &d2s[i]); + scanf(" %d %d %d %d %d %d", &wad[i], &ssd[i], &ssq[i], &ssp[i], &q1[i], &q2[i]); + scanf(" %d %d %d", &jing[i], &qi[i], &shen[i]); + } + xxt = 5 - xx_su, syt = 5 - sy_su; + printf("Case %d: ", t + 1); + int re = 0; + for (int round = 1; round <= 12 && re == 0; round++) + if (ok(round)) + { + printf("%d", round); + for (int i = 0; i < ans.size(); i++) + { + cout << " " << ans[i]; + } + printf("\n"); + re = 1; + } + if (re == 0) + { + printf("-1\n"); + } + printf("\n"); + } + return 0; +} diff --git a/12235.cpp b/12235.cpp new file mode 100644 index 0000000..4c75059 --- /dev/null +++ b/12235.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +/* ********************************************** +Author : Nero +Created Time: 2013-8-31 21:55:28 +Problem id : UVA 12235 +Problem Name: Help Bubu +*********************************************** */ + +#define REP(i, a, b) for (int i = (a); i < (int)(b); i++) +#define clr(a, b) memset(a, b, sizeof(a)) + +const int INF = 0x3f3f3f3f; +int dp[2][102][(1 << 8)][8];// å‰Âi本书,å–走j本,留下的书的集åˆ为s,留下的书最åŽ一本为k的最å°Â混乱度 +int n, K; +int all; + +int main() +{ + int ca = 0; + while (~scanf("%d%d", &n, &K), n || K) + { + all = 0; + int a; + clr(dp[0], INF); + for (int i = 0; i < n; i++) + { + scanf("%d", &a); + a -= 25; + int now = i & 1; + int nes = !now; + clr(dp[nes], INF); + dp[nes][i][(1 << a)][a] = 1; + int MM = min(i, K); + for (int j = 0; j <= min(i, K); j++) + { + for (int s = all; s; s = (s - 1) & all) + { + for (int k = 0; (1 << k) <= s; k++) + if (dp[now][j][s][k] != INF) + { + if (k == a) + dp[nes][j][s][a] = min(dp[nes][j][s][a], dp[now][j][s][k]); + else + { + dp[nes][j][s | (1 << a)][a] = min(dp[nes][j][s | (1 << a)][a], dp[now][j][s][k] + 1); + dp[nes][j + 1][s][k] = min(dp[nes][j + 1][s][k], dp[now][j][s][k]); + } + } + } + } + all |= (1 << a); + } + + int minx = INF; + for (int j = 0; j <= K; j++) + { + for (int s = all; s; s = (s - 1) & all) + { + for (int k = 0; (1 << k) <= s; k++) + if (dp[n & 1][j][s][k] != INF) + { + int cnt = 0; + for (int ss = s ^ all; ss; ss >>= 1) + if (ss & 1) + cnt++; + minx = min(minx, cnt + dp[n & 1][j][s][k]); + } + } + } + printf("Case %d: %d\n\n", ++ca, minx); + } + return 0; +} diff --git a/12238.cpp b/12238.cpp new file mode 100644 index 0000000..bd3c26b --- /dev/null +++ b/12238.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define NN 100048 +#define MAX 1048576 + +int P[NN][17], L[NN]; +long long W[NN]; +char buf[MAX], *lim = buf + MAX, *now = lim; + +void adapt() +{ + while (now != lim && !isdigit(*now)) + { + now++; + } + if (now == lim) + { + int r = fread(buf, 1, MAX - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + now = buf; + } + while (!isdigit(*now)) + { + now++; + } +} +void readint(int &n) +{ + adapt(); + n = 0; + while (isdigit(*now)) + { + n = n * 10 + *now++ - '0'; + } +} +void readll(long long int &n) +{ + adapt(); + n = 0; + while (isdigit(*now)) + { + n = n * 10 + *now++ - '0'; + } +} +int query(int p, int q) +{ + int log, i; + if (L[p] < L[q]) + { + p ^= q ^= p ^= q; + } + for (log = 1; 1 << log <= L[p]; log++) + ; + log--; + for (i = log; i >= 0; i--) + if (L[p] - (1 << i) >= L[q]) + { + p = P[p][i]; + } + if (p == q) + { + return p; + } + for (i = log; i >= 0; i--) + if (P[p][i] != -1 && P[p][i] != P[q][i]) + { + p = P[p][i], q = P[q][i]; + } + return P[p][0]; +} +int main() +{ + int a, b, N, q; + for (readint(N); N; readint(N)) + { + long long w; + P[0][0] = -1; + L[0] = W[0] = 0; + for (int i = 1; i < N; ++i) + { + readint(a), readll(w); + for (int j = 0; (1 << j) < N; ++j) + { + P[i][j] = -1; + } + P[i][0] = a; + L[i] = L[a] + 1; + W[i] = W[a] + w; + } + for (int j = 1; (1 << j) < N; ++j) + for (int i = 0; i < N; ++i) + if (P[i][j - 1] != -1) + { + P[i][j] = P[P[i][j - 1]][j - 1]; + } + readint(q); + while (q--) + { + readint(a), readint(b); + printf("%lld", W[a] + W[b] - (W[query(a, b)] << 1)); + if (q) + { + putc(32, stdout); + } + } + putc(10, stdout); + } + return 0; +} diff --git a/12239.cpp b/12239.cpp index 870012e..b7634c6 100644 --- a/12239.cpp +++ b/12239.cpp @@ -1,28 +1,42 @@ -#include -#include -#include -#include +#include + using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int N, B; - scanf("%d%d", &N, &B); - if (!N && !B) break; +bool cando[128]; +int b[128]; - set< int > Set; - int Arr[B]; - for (int i = 0; i < B; i++) scanf("%d", &Arr[i]); +int abs(int x) +{ + return x < 0 ? -x : x; +} - for (int i = 0; i < B; i++) - for (int j = i; j < B; j++) { - int T = (int)abs(Arr[i] - Arr[j]); - if (T <= N) Set.insert(T); - } - if (Set.size() == N + 1) printf("Y\n"); - else printf("N\n"); - } - return 0; +int main() +{ + for (int B, N; scanf("%d %d", &N, &B) == 2 && (N || B);) + { + int s = 1; + for (int i = 0; i <= N; ++i) + { + cando[i] = 0; + } + for (int i = 0; i < B; ++i) + { + scanf("%d", &b[i]); + for (int j = 0; j < i; ++j) + { + int diff = abs(b[i] - b[j]); + s += !cando[diff]; + cando[diff] |= 1; + } + } + if (s == N + 1) + { + puts("Y"); + } + else + { + puts("N"); + } + } + return 0; } diff --git a/1224.cpp b/1224.cpp new file mode 100644 index 0000000..c211940 --- /dev/null +++ b/1224.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +#define MAXN 31 +int main() +{ + int W1[MAXN], W2[MAXN], i, tc; + W1[0] = 1; + W1[1] = 1; + for (i = 2; i < MAXN; i++) + { + W1[i] = 2 * W1[i - 2] + W1[i - 1]; + } + W2[0] = 1; + W2[1] = 1; + W2[2] = 3; + W2[3] = 1; + for (i = 4; i < MAXN; i++) + { + W2[i] = W2[i - 2] + 2 * W2[i - 4]; + } + scanf("%d", &tc); + while (tc-- > 0) + { + scanf("%d", &i); + printf("%d\n", (W1[i] - W2[i]) / 2 + W2[i]); + } + return 0; +} diff --git a/12240.cpp b/12240.cpp new file mode 100644 index 0000000..6a3104e --- /dev/null +++ b/12240.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +struct point +{ + int x, y; +} p[100]; + +double kkk[100]; + +int sqr(int x) +{ + return x * x; +} + +double circle(point p1, point p2, point p3) +{ + int den = (p3.y - p2.y) * (p1.x - p2.x) - (p2.x - p3.x) * (p2.y - p1.y); + int num = (p3.y - p1.y) * (p2.y - p1.y) - (p3.x - p1.x) * (p1.x - p2.x); + return num / (den * 2.); +} + +bool collinear(point p1, point p2, point p3) +{ + return (p2.x - p1.x) * (p3.y - p2.y) == (p2.y - p1.y) * (p3.x - p2.x); +} + +int main() +{ + for (int N; scanf("%d", &N) == 1 && N;) + { + int ans = min(N, 2); + for (int i = 0; i < N; ++i) + { + scanf("%d %d", &p[i].x, &p[i].y); + } + for (int i = 0; i < N; ++i) + for (int j = i + 1; j < N; ++j) + { + int cc = 0; + for (int k = i + 1; k < N; ++k) + if (!collinear(p[i], p[j], p[k])) + { + kkk[cc++] = circle(p[k], p[i], p[j]); + } + sort(kkk, kkk + cc); + for (int r = 0, z = 0; r < cc && z < cc;) + { + while (z < cc && fabs(kkk[z] - kkk[r]) < EPS) + { + ++z; + } + ans = max(ans, z - r + 2); + r = z; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12241.cpp b/12241.cpp new file mode 100644 index 0000000..b99f8ee --- /dev/null +++ b/12241.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int c[2][10], pow10_[10]; + +void cnt_f(int x, bool up) +{ + int *cnt = c[up], d, dcnt = 0, r = 0, rem0 = 0, v; + while (x) + { + d = x % 10; + x /= 10; + if (dcnt) + { + v = d * pow10_[dcnt - 1] * dcnt; + for (int i = 0; i < 10; ++i) + { + cnt[i] += v; + } + if (!d) + { + rem0 += (pow10_[dcnt] - 1) - r; + } + } + v = pow10_[dcnt]; + for (int i = 1; i < d; ++i) + { + cnt[i] += v; + } + if (d) + { + cnt[d] += r + 1; + } + r = pow10_[dcnt++] * d + r; + } + cnt[0] -= rem0; +} + +int main() +{ + pow10_[0] = 1; + for (int i = 1; i < 10; ++i) + { + pow10_[i] = 10 * pow10_[i - 1]; + } + for (int a, b; scanf("%d %d", &a, &b) == 2 && (a || b);) + { + for (int i = 0; i < 10; ++i) + { + c[0][i] = c[1][i] = 0; + } + cnt_f(b, 1); + cnt_f(a - 1, 0); + for (int i = 0; i < 10; ++i) + { + if (i) + { + putc(32, stdout); + } + printf("%d", c[1][i] - c[0][i]); + } + putc(10, stdout); + } + return 0; +} diff --git a/12242.cpp b/12242.cpp new file mode 100644 index 0000000..ae61dfb --- /dev/null +++ b/12242.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define RI(x) readint(x) + +struct vert +{ + int n, x; + bool operator<(const vert v) const + { + return x < v.x; + } +} v[40]; + +struct horz +{ + int n, y; + bool out; + bool operator<(const horz h) const + { + return y < h.y || (y == h.y && out < h.out); + } +} h[40]; + +int M, N, P, gc[20], gr[20]; + +int do_rectangles(int p, int MINR, int MAXR, int MAXC, int &GC) +{ + int b, k = 0, l, r, t; + for (int i = 0; i < P; ++i) + { + b = max(MINR, gr[i] - p); + l = max(0, gc[i] - p - 1); + r = min(MAXC, gc[i] + p); + t = min(MAXR, gr[i] + p); + if (gc[i] <= MAXC && gr[i] >= MINR && gr[i] <= MAXR) + { + ++GC; + } + if (t < MINR || b > MAXR || l > MAXC) + { + continue; + } + v[k].n = i; + v[k].x = l; + h[k].n = i; + h[k].y = b; + h[k].out = 0; + k++; + v[k].n = i; + v[k].x = r; + h[k].n = i; + h[k].y = t; + h[k].out = 1; + k++; + } + sort(v, v + k); + sort(h, h + k); + return k; +} +long long int area(int p, int MINR, int MAXR, int MAXC) +{ + int GC = 0, length, start, xact = 0, yact = 0; + int K = do_rectangles(p, MINR, MAXR, MAXC, GC); + long long ret = 0, diff; + for (int i = 0; i < K; ++i) + { + if (i && v[i].x != v[i - 1].x) + { + length = 0; + for (int j = 0; j < K; ++j) + { + if (!(xact & (1 << h[j].n))) + { + continue; + } + if (!yact) + { + start = h[j].y; + } + yact ^= 1 << h[j].n; + if (!yact) + { + length += h[j].y - start + 1; + } + } + diff = v[i].x - v[i - 1].x; + ret += diff * length; + } + xact ^= 1 << v[i].n; + } + return max(0LL, ret - GC); +} +int bin_search(int LIM, long long int key, int type, int p = 0, int l = 0) +{ + int mid, u, v; + long long A; + for (u = 1, v = LIM; u < v;) + { + mid = (u + v) >> 1; + if (type == 1) + { + A = area(mid, 1, N, M); + } + else if (type == 2) + { + A = area(p, 1, mid, M) - area(p - 1, 1, mid, M); + } + else if (type == 3) + { + A = area(p, l, l, mid) - area(p - 1, l, l, mid); + } + if (A < key) + { + u = mid + 1; + } + else + { + v = mid; + } + } + return u; +} + +int main() +{ + int c, l, p, Q; + long long q; + while (scanf("%d %d %d", &N, &M, &P) == 3 && (M || N || P)) + { + for (int i = 0; i < P; ++i) + { + scanf("%d %d", &gr[i], &gc[i]); + } + scanf("%d", &Q); + for (int i = 0; i < Q; ++i) + { + scanf("%lld", &q); + p = bin_search(max(N, M), q, 1); + q -= area(p - 1, 1, N, M); + l = bin_search(N, q, 2, p); + q -= area(p, 1, l - 1, M) - area(p - 1, 1, l - 1, M); + c = bin_search(M, q, 3, p, l); + printf("%d %d\n", l, c); + } + puts("-"); + } + return 0; +} diff --git a/12243.cpp b/12243.cpp index 6a05a13..5c039af 100644 --- a/12243.cpp +++ b/12243.cpp @@ -1,29 +1,42 @@ -#include -#include -#include -#include -using namespace std; +#include -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - string SS, S; - getline(cin, SS); - if (SS == "*") break; +using namespace std; - bool flag = true; - stringstream ss(SS); - ss >> S; - char C = tolower(S[0]); - while (ss >> S) { - if (tolower(S[0]) != C) { - flag = false; - break; - } - } - if (flag) cout << "Y\n"; - else cout << "N\n"; - } - return 0; +int main() +{ + string input; + while (getline(cin, input)) + { + if (input == "*") + { + break; + } + transform(input.begin(), input.end(), input.begin(), ::tolower); + stringstream ss(input, stringstream::in); + char ch = ' '; + bool ok = true; + while (ss >> input) + { + if (ch == ' ') + { + ch = input[0]; + } + else + { + if (ch != input[0]) + { + ok = false; + } + } + } + if (ok) + { + cout << "Y" << endl; + } + else + { + cout << "N" << endl; + } + } + return 0; } diff --git a/12244.cpp b/12244.cpp new file mode 100644 index 0000000..d559ecf --- /dev/null +++ b/12244.cpp @@ -0,0 +1,292 @@ +#include + +using namespace std; + +inline int readint() +{ + char c = getchar(); + while (!isdigit(c)) + { + c = getchar(); + } + int x = 0; + while (isdigit(c)) + { + x = x * 10 + c - '0'; + c = getchar(); + } + return x; +} + +inline long long readlong() +{ + char c = getchar(); + while (!isdigit(c)) + { + c = getchar(); + } + long long x = 0; + while (isdigit(c)) + { + x = x * 10 + c - '0'; + c = getchar(); + } + return x; +} + +#define FOR(i, n) for (int i = 0; i < (int)(n); i++) +#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++) +#define CIR(i, a, b) for (int i = (int)(b); i >= (int)(a); i--) +#define ADJ(i, u) for (int i = hd[u]; i != -1; i = edge[i].nxt) +#define ECH(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) +#define PII pair +#define FI first +#define SE second +#define MP make_pair +#define PB push_back +#define SZ(v) v.size() +#define ALL(v) v.begin(), v.end() +#define CLR(v, a) memset(v, a, sizeof(v)) +#define IT iterator +#define LL long long +#define DB double +#define PI 3.1415926 + +//#pragma comment(linker,"/STACK:102400000,102400000") +#define INF 1000000000 +#define N 10005 +#define M N * 100 +#define C 26 + +char ss[M]; +int maxv[M << 2]; +int upv[M << 2]; +int Map[256]; +int dp[N], idx[N], len[N], pos[N]; +int tot; + +void init() +{ + FOR(i, 26) + { + Map[i + 'a'] = i; + } +} + +#define lch rt << 1, L, mid +#define rch rt << 1 | 1, mid + 1, R + +void build(int rt, int L, int R) +{ + upv[rt] = -INF; + maxv[rt] = -INF; + if (L == R) + { + return; + } + int mid = L + R >> 1; + build(lch); + build(rch); +} + +void push_down(int rt, int L, int R) +{ + if (upv[rt] != -INF) + { + int v = upv[rt]; + upv[rt] = -INF; + upv[rt << 1] = max(upv[rt << 1], v); + upv[rt << 1 | 1] = max(upv[rt << 1 | 1], v); + maxv[rt << 1] = max(maxv[rt << 1], v); + maxv[rt << 1 | 1] = max(maxv[rt << 1 | 1], v); + } +} + +void update(int rt, int L, int R, int l, int r, int v) +{ + if (l <= L && R <= r) + { + maxv[rt] = max(maxv[rt], v); + upv[rt] = max(upv[rt], v); + return; + } + int mid = L + R >> 1; + push_down(rt, L, R); + if (l <= mid) + { + update(lch, l, r, v); + } + if (r > mid) + { + update(rch, l, r, v); + } +} + +int query(int rt, int L, int R, int p) +{ + if (L == p && R == p) + { + return maxv[rt]; + } + int mid = L + R >> 1; + push_down(rt, L, R); + if (p <= mid) + { + return query(lch, p); + } + else + { + return query(rch, p); + } +} + +struct AC +{ + int ch[M][C]; + int ff[M]; + int it; + queue Q; + vector G[M]; + int tdfn, st[M], ed[M]; + + void init() + { + CLR(ch[0], 0); + FOR(i, it) + G[i].clear(); + it = 1; + tdfn = 1; + } + + void insert(char *ss, int len) + { + int cur = 0; + FOR(i, len) + { + int c = Map[ss[i]]; + if (!ch[cur][c]) + { + ch[cur][c] = it; + CLR(ch[it], 0); + ff[it] = 0; + it++; + } + cur = ch[cur][c]; + } + } + + void set_fail() + { + ff[0] = 0; + FOR(i, C) + { + int u = ch[0][i]; + if (u) + { + ff[u] = 0; + Q.push(u); + G[0].PB(u); + } + } + while (!Q.empty()) + { + int u = Q.front(); + Q.pop(); + FOR(i, C) + { + int v = ch[u][i]; + if (!v) + { + ch[u][i] = ch[ff[u]][i]; + } + else + { + Q.push(v); + int fa = ff[u]; + while (fa && !ch[fa][i]) + { + fa = ff[fa]; + } + ff[v] = ch[fa][i]; + G[ff[v]].PB(v); + } + } + } + } + + void dfs(int u) + { + st[u] = tdfn++; + for (int i = 0; i < G[u].size(); i++) + { + int v = G[u][i]; + dfs(v); + } + ed[u] = tdfn - 1; + } + + void solve(int id) + { + int key = -INF, cur = 0; + for (int i = 0; i < len[id]; i++) + { + int j = i + pos[id]; + int c = Map[ss[j]]; + cur = ch[cur][c]; + key = max(key, query(1, 1, tot, st[cur])); + } + dp[id] = 1; + if (key > 0) + { + dp[id] += key; + } + update(1, 1, tot, st[cur], ed[cur], dp[id]); + } + +} ac; + +bool cmp(const int &a, const int &b) +{ + return len[a] < len[b]; +} + +int main() +{ + init(); + int n; + while (scanf("%d", &n)) + { + if (n == 0) + { + break; + } + int rt = 0; + ac.init(); + REP(i, 1, n) + { + scanf("%s", ss + rt); + pos[i] = rt; + len[i] = strlen(ss + rt); + ac.insert(ss + rt, len[i]); + rt += len[i]; + } + ac.set_fail(); + ac.dfs(0); + REP(i, 1, n) + idx[i] = i; + sort(idx + 1, idx + n + 1, cmp); + tot = ac.tdfn - 1; + build(1, 1, tot); + REP(i, 1, n) + { + ac.solve(idx[i]); + } + int res = 0; + REP(i, 1, n) + { + res = max(res, dp[i]); + } + printf("%d\n", res); + } + return 0; +} diff --git a/12245.cpp b/12245.cpp new file mode 100644 index 0000000..f49333d --- /dev/null +++ b/12245.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int MOD = 100000000; +const int NN = 100; + +struct interval +{ + int b, e; + bool operator<(const interval x) const + { + return b < x.b || (b == x.b && e < x.e); + } +} t[NN]; + +int ans, r[NN][NN]; + +int main() +{ + for (int m, n; scanf("%d %d", &m, &n) == 2 && (m || n);) + { + ans = 0; + for (int i = 0; i < n; ++i) + { + memset(r[i], 0, n << 2); + scanf("%d %d", &t[i].b, &t[i].e); + } + sort(t, t + n); + for (int i = 0; i < n; ++i) + { + r[i][i] = t[i].b == 0; + if (t[i].e != m) + { + for (int j = i + 1; j < n; ++j) + if (t[j].b > t[i].b && t[j].b <= t[i].e && t[j].e > t[i].e) + { + for (int k = 0; k <= i; ++k) + if (r[i][k] && !r[j][k]) + { + r[j][i] = (r[j][i] + r[i][k]) % MOD; + } + } + } + else + for (int j = 0; j <= i; ++j) + if (r[i][j]) + { + ans = (ans + r[i][j]) % MOD; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12246.cpp b/12246.cpp new file mode 100644 index 0000000..c2f70b4 --- /dev/null +++ b/12246.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int w[100048]; + +int main() +{ + for (int Q, T; scanf("%d %d", &T, &Q) == 2 && (Q || T);) + { + for (int i = 0; i < T; ++i) + { + scanf("%d", &w[i]), w[i] <<= 1; + } + int r = 0x1fffffff; + if (T > 1) + { + r = abs(w[1] - w[0]); + } + for (int i = 2; i < T; ++i) + { + r = __gcd(r, abs(w[i] - w[0])); + } + for (int i = 0; i < Q; ++i) + { + int a, b, d, j; + scanf("%d %d", &a, &b); + if (i) + { + printf(" "); + } + d = abs(b - a); + a += b; + if (d & 1) + { + printf("N"); + } + else if (!(d % r)) + { + printf("Y"); + } + else + { + for (j = 0; j < T; ++j) + if (!((w[j] - a) % r)) + { + break; + } + if (j != T) + { + printf("Y"); + } + else + { + printf("N"); + } + } + } + putc(10, stdout); + } + return 0; +} diff --git a/12247.cpp b/12247.cpp index 79edca5..8a83645 100644 --- a/12247.cpp +++ b/12247.cpp @@ -1,55 +1,73 @@ -#include -#include -#include -#include -using namespace std; - -bool check(int A[], int B[]) { - sort(A, A + 3); - bool flag = true; - do { - sort(B, B + 3); - do { - int C = 0; - for (int j = 0; j < 3; j++) { - if (A[j] < B[j]) ++C; - } - if (C < 2) flag = false; - } while (next_permutation(B, B + 3) ); - } while (next_permutation(A, A + 3) ); +#include - return flag; -} +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int X[3], Y[3], i; - cin >> X[0] >> X[1] >> X[2] >> Y[0] >> Y[1]; - if (!X[0] && !X[1] && !X[2] && !Y[0] && !Y[1]) break; +int a[3], b[2]; +bool busy[64], used[3]; - set< int > S; - S.insert(X[0]); S.insert(X[1]); S.insert(X[2]); - S.insert(Y[0]); S.insert(Y[1]); - bool flag = false; - for (i = 1; i < 53; i++) { - if (S.find(i) != S.end()) continue; - //cout << i << " "; - Y[2] = i; - int A[3], B[3]; - for (int i = 0; i < 3; i++) { - A[i] = X[i]; - B[i] = Y[i]; - } - if (check(A, B)) { - flag = true; - break; - } - } - //if (i < 0 || i > 52) flag = false; - if (flag) cout << i << endl; - else cout << "-1" << endl; - } - return 0; +int main() +{ + while (scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &b[0], &b[1]) == 5) + { + if (!(a[0] || a[1] || a[2] || b[0] || b[1])) + { + break; + } + int s = 1, win = 0; + sort(a, a + 3); + if (b[0] > b[1]) + { + swap(b[0], b[1]); + } + for (int i = 0; i < 64; ++i) + { + busy[i] = 0; + } + busy[a[0]] = busy[a[1]] = busy[a[2]] = busy[b[0]] = busy[b[1]] = 1; + used[0] = used[1] = used[2] = 0; + for (int i = 0; i < 2; ++i) + { + int x = b[i], y = -1, idx = -1; + for (int j = 0; j < 3; ++j) + if (!used[j] && a[j] > x) + { + y = a[j], idx = j; + break; + } + if (idx == -1) + for (int j = 0; j < 3; ++j) + if (!used[j]) + { + y = a[j], idx = j; + break; + } + used[idx] = 1; + if (x > y) + { + ++win; + } + } + if (win) + { + if (win == 1) + for (int j = 0; j < 3; ++j) + if (!used[j]) + { + s = a[j] + 1; + } + while (busy[s]) + { + ++s; + } + } + if (!win || s > 52) + { + puts("-1"); + } + else + { + printf("%d\n", s); + } + } + return 0; } diff --git a/12248.cpp b/12248.cpp new file mode 100644 index 0000000..5cc1b23 --- /dev/null +++ b/12248.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +const int MAX_W = (int)1e5; +const int INF = 1 << 29; + +int pars[2 * MAX_W]; +int ranks[2 * MAX_W]; + +void init_uf(int n) +{ + for (int i = 0; i < n; i++) + { + pars[i] = i; + ranks[i] = 0; + } +} + +int get_root(int x) +{ + if (pars[x] == x) + { + return x; + } + else + { + return pars[x] = get_root(pars[x]); + } +} + +void merge(int x, int y) +{ + x = get_root(x); + y = get_root(y); + if (x == y) + { + return; + } + if (ranks[x] < ranks[y]) + { + pars[x] = y; + } + else if (ranks[y] < ranks[x]) + { + pars[y] = x; + } + else + { + pars[x] = y; + ranks[y]++; + } +} + +bool is_same(int x, int y) +{ + return get_root(x) == get_root(y); +} + +int as[MAX_W], bs[MAX_W]; +int nums[2 * MAX_W], degs[2 * MAX_W], tops[2 * MAX_W], min_degs[2 * MAX_W], max_degs[2 * MAX_W]; +int W, K; + +void init() +{ + for (int i = 0; i < W; i++) + { + scanf("%d%d", as + i, bs + i); + } + fill(degs, degs + 2 * MAX_W, 0); +} + +pair get_pair(int a, int b) +{ + return make_pair(min(a, b), max(a, b)); +} + +int compress() +{ + for (int i = 0; i < W; i++) + { + nums[2 * i + 0] = as[i]; + nums[2 * i + 1] = bs[i]; + } + sort(nums, nums + 2 * W); + int N = unique(nums, nums + 2 * W) - nums; + set> asked; + int p = 0; + for (int i = 0; i < W; i++) + { + as[i] = lower_bound(nums, nums + N, as[i]) - nums; + bs[i] = lower_bound(nums, nums + N, bs[i]) - nums; + if (asked.insert(get_pair(as[i], bs[i])).second) + { + as[p] = as[i]; + bs[p++] = bs[i]; + } + } + W = p; + return N; +} + +bool solve() +{ + int N = compress(); + init_uf(N); + for (int i = 0; i < W; i++) + { + merge(as[i], bs[i]); + degs[as[i]]++; + degs[bs[i]]++; + } + for (int i = 0; i < N; i++) + { + tops[i] = get_root(i); + } + sort(tops, tops + N); + int M = unique(tops, tops + N) - tops; + fill(min_degs, min_degs + N, INF); + fill(max_degs, max_degs + N, 0); + for (int i = 0; i < N; i++) + { + min_degs[get_root(i)] = min(min_degs[get_root(i)], degs[i]); + max_degs[get_root(i)] = max(max_degs[get_root(i)], degs[i]); + } + if (N == K && M == 1) + { + return max_degs[tops[0]] <= 2; + } + for (int i = 0; i < M; i++) + { + if (max_degs[tops[i]] > 2 || min_degs[tops[i]] != 1) + { + return false; + } + } + return true; +} + +int main() +{ + while (scanf("%d%d", &K, &W), K) + { + init(); + puts(solve() ? "Y" : "N"); + } + return 0; +} diff --git a/12249.cpp b/12249.cpp new file mode 100644 index 0000000..f927cd2 --- /dev/null +++ b/12249.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +char s[10][15], ss[105]; +int N, ans, len[10]; + +void dfs(int use, int level, int st) +{ + int i, j, k, t; + if (level == N) + { + if (st < ans) + { + ans = st; + } + return; + } + for (i = 0; i < N; i++) + { + if (use & (1 << i)) + { + continue; + } + if (level == 0) + { + for (j = st, k = 0; k < len[i]; j++, k++) + { + ss[j] = s[i][k]; + } + dfs(use | (1 << i), level + 1, j); + } + else + { + int re = 0; + for (j = st - len[i]; j < st; j++) + { + t = j; + k = 0; + if (s[i][0] == ss[j]) + { + for (k = 0, t = j; k < len[i] && t < st; k++, t++) + { + if (s[i][k] != ss[t]) + { + break; + } + } + } + if (t == st) + { + re = 1; + break; + } + } + if (re == 1) + { + for (k = 0; k < len[i]; j++, k++) + { + ss[j] = s[i][k]; + } + dfs(use | (1 << i), level + 1, j); + } + else if (re == 0) + { + for (j = st, k = 0; k < len[i]; j++, k++) + { + ss[j] = s[i][k]; + } + dfs(use | (1 << i), level + 1, j); + } + } + } +} + +int main() +{ + int T, t, n, i, j; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + scanf(" %d", &N); + gets(s[0]); + for (n = 0; n < N; n++) + { + gets(s[n]); + len[n] = strlen(s[n]); + } + ans = 99999999; + dfs(0, 0, 0); + printf("Case %d: %d\n", t + 1, ans); + } + return 0; +} diff --git a/1225.cpp b/1225.cpp index 5e86cdf..3cf919a 100644 --- a/1225.cpp +++ b/1225.cpp @@ -1,38 +1,43 @@ -#include +#include -int Mat[10001][10]; +using namespace std; -void Cal() { - for (int i = 0; i < 10; i++) - Mat[0][i] = 0; - - for (int i = 1; i <= 10000; i++) { - for (int j = 0; j < 10; j++) - Mat[i][j] = Mat[i - 1][j]; - - int K = i; - while (K) { - ++Mat[i][K % 10]; - K /= 10; - } - } +void count_digits(int number, int *result) +{ + while (number > 0) + { + result[number % 10] += 1; + number /= 10; + } } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - Cal(); - - int T; - scanf("%d", &T); - while (T--) { - int N; - scanf("%d", &N); - for (int i = 0; i < 10; i++) { - if (i > 0) printf(" "); - printf("%d", Mat[N][i]); - } - printf("\n"); - } +int main() +{ + int t, n; + cin >> t; + int *result; + result = new int[10]; + while (t--) + { + cin >> n; + for (int i = 0; i < 10; i++) + { + result[i] = 0; + } + for (int i = 1; i <= n; i++) + { + count_digits(i, result); + } + for (int i = 0; i < 10; i++) + { + if (i > 0) + { + cout << " "; + } + cout << result[i]; + } + cout << endl; + } + delete[] result; + return 0; } diff --git a/12250.cpp b/12250.cpp index d216026..4d5290d 100644 --- a/12250.cpp +++ b/12250.cpp @@ -1,26 +1,34 @@ -#include -#include -#include -#include -using namespace std; - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +#include - map Map; - Map["HELLO"] = "ENGLISH"; Map["HOLA"] = "SPANISH"; Map["HALLO"] = "GERMAN"; - Map["BONJOUR"] = "FRENCH"; Map["CIAO"] = "ITALIAN"; Map["ZDRAVSTVUJTE"] = "RUSSIAN"; - - for (int t = 1; ; t++){ - string str; - cin >> str; - if (str == "#") break; +using namespace std; - if (Map.find(str) != Map.end()) - cout << "Case " << t << ": " << Map[str] << endl; - else - cout << "Case " << t << ": UNKNOWN" << endl; - } - return 0; +int main() +{ + int case_num = 0; + string input; + map lang; + lang["HELLO"] = "ENGLISH"; + lang["HOLA"] = "SPANISH"; + lang["HALLO"] = "GERMAN"; + lang["BONJOUR"] = "FRENCH"; + lang["CIAO"] = "ITALIAN"; + lang["ZDRAVSTVUJTE"] = "RUSSIAN"; + while (getline(cin, input)) + { + if (input == "#") + { + break; + } + case_num++; + cout << "Case " << case_num << ": "; + if (lang.find(input) == lang.end()) + { + cout << "UNKNOWN" << endl; + } + else + { + cout << lang[input] << endl; + } + } + return 0; } diff --git a/12251.cpp b/12251.cpp new file mode 100644 index 0000000..9c88d43 --- /dev/null +++ b/12251.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int dp[1010][2], went[1010][2], n, x, y, z; + +int search(int s, int t) +{ + if (went[s][t]) + { + return dp[s][t]; + } + int cost; + if (t) + { + cost = z; + } + else + { + cost = x + y; + } + went[s][t] = 1; + int temp1 = search(1, 0); + int temp2 = search(s - 1, 1); + dp[s][t] = cost + max(temp1, temp2); + for (int i = 2; i <= s - 1; i++) + { + int temp1 = search(i, 0); + int temp2 = search(s - i, 1); + dp[s][t] = min(dp[s][t], cost + max(temp1, temp2)); + } + return dp[s][t]; +} + +int main() +{ + int CASL; + scanf("%d", &CASL); + for (int CAS = 1; CAS <= CASL; CAS++) + { + scanf("%d%d%d%d", &n, &x, &y, &z); + memset(went, 0, sizeof(went)); + dp[1][0] = 0; + dp[1][1] = -(x / 2); + went[1][0] = 1; + went[1][1] = 1; + printf("Case %d: %d\n", CAS, search(n, 0)); + } + return 0; +} diff --git a/12252.cpp b/12252.cpp new file mode 100644 index 0000000..34ce442 --- /dev/null +++ b/12252.cpp @@ -0,0 +1,186 @@ +#include + +using namespace std; + +int n, map_[20][20]; + +inline int check1(int s, int t) +{ + if (map_[s][t]) + { + return 0; + } + if (map_[s + 1][t]) + { + return 0; + } + if (map_[s + 2][t]) + { + return 0; + } + if (map_[s + 1][t - 1]) + { + return 0; + } + if (map_[s + 1][t + 1]) + { + return 0; + } + return 1; +} + +inline int check2(int s, int t, int c) +{ + if (map_[s - 1][t - 1] == c) + { + return 0; + } + if (map_[s - 1][t] == c) + { + return 0; + } + if (map_[s - 1][t + 1] == c) + { + return 0; + } + if (map_[s][t - 1] == c) + { + return 0; + } + if (t > 2 && map_[s][t - 2] == c) + { + return 0; + } + if (map_[s][t + 1] == c) + { + return 0; + } + if (map_[s][t + 2] == c) + { + return 0; + } + if (t > 2 && map_[s + 1][t - 2] == c) + { + return 0; + } + if (map_[s + 1][t + 2] == c) + { + return 0; + } + if (t > 2 && map_[s + 2][t - 2] == c) + { + return 0; + } + if (map_[s + 2][t - 1] == c) + { + return 0; + } + if (map_[s + 2][t + 1] == c) + { + return 0; + } + if (map_[s + 2][t + 2] == c) + { + return 0; + } + if (map_[s + 3][t - 1] == c) + { + return 0; + } + if (map_[s + 3][t] == c) + { + return 0; + } + if (map_[s + 3][t + 1] == c) + { + return 0; + } + return 1; +} + +int bfs(int s, int t) +{ + if (s > n) + { + return 1; + } + if (t > n) + { + s++; + t = 1; + } + if (map_[s][t]) + { + return bfs(s, t + 1); + } + else + { + if (!check1(s, t)) + { + return 0; + } + else + { + for (int i = 2; i <= 4; i++) + if (check2(s, t, i)) + { + map_[s][t] = i; + map_[s + 1][t] = i; + map_[s + 1][t - 1] = i; + map_[s + 1][t + 1] = i; + map_[s + 2][t] = i; + if (bfs(s, t + 1)) + { + return 1; + } + } + map_[s][t] = 0; + map_[s + 1][t] = 0; + map_[s + 1][t - 1] = 0; + map_[s + 1][t + 1] = 0; + map_[s + 2][t] = 0; + return 0; + } + } +} + +int main() +{ + int CASL; + scanf("%d", &CASL); + char str[20]; + for (int CAS = 1; CAS <= CASL; CAS++) + { + scanf("%d", &n); + for (int i = 0; i <= n + 3; i++) + for (int j = 0; j <= n + 3; j++) + { + map_[i][j] = 1; + } + for (int i = 1; i <= n; i++) + { + scanf("%s", str); + for (int j = 1; j <= n; j++) + { + map_[i][j] = (str[j - 1] == 'A'); + } + } + if (!bfs(1, 1)) + { + printf("Case %d: Not Possible!\n", CAS); + } + else + { + printf("Case %d:\n", CAS); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + printf("%c", map_[i][j] + 'A' - 1); + } + printf("\n"); + } + } + } + return 0; +} diff --git a/12253.cpp b/12253.cpp new file mode 100644 index 0000000..31eb6e3 --- /dev/null +++ b/12253.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +typedef long long LL; +LL p[20]; +LL Mul(LL A, LL B, LL C) +{ + LL a1 = A / 1000000; + LL a2 = A % 1000000; + LL b1 = B / 1000000; + LL b2 = B % 1000000; + LL ret = a2 * b1 * 1000000 % C + b2 * a1 * 1000000 % C + a2 * b2; + return ret % C; +} +LL pow(LL A, LL B, LL C) +{ + LL ret = 1; + for (; B; B >>= 1) + { + if (B & 1) + ret = Mul(ret, A, C); + A = Mul(A, A, C); + } + return ret; +} + +bool check(LL A, LL B, LL C) +{ + LL phi = C; + C = C * 10; + return B == pow(A, B % (4 * phi) + 4 * phi, C) || B == pow(A, (B + 2 * phi) % (4 * phi) + 4 * phi, C); +} +bool dfs(int len, LL now, LL A) +{ + //printf(">>> len = %d now = %lld A = %lld\n", len, now, A); + if (len == 12) + { + LL Mod = p[12]; + if (pow(A, now, Mod) == now) + { + printf("%lld\n", now); + return true; + } + return false; + } + for (int i = len == 11 ? 1 : 0; i <= 9; i++) + { + LL tmp = i * p[len] + now; + LL Mod = p[len]; + if (check(A, tmp, Mod)) + { + if (dfs(len + 1, tmp, A)) + return true; + } + } + return false; +} +int main() +{ + p[0] = 1; + for (int i = 1; i <= 13; i++) + p[i] = p[i - 1] * 10; + for (LL n, kase = 1; scanf("%lld", &n) != EOF, n;) + { + printf("Case %d: Public Key = %d Private Key = ", kase++, n); + dfs(0, 0, n); + } +} diff --git a/12255.cpp b/12255.cpp new file mode 100644 index 0000000..17067a5 --- /dev/null +++ b/12255.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +long long K, N, S, D; + +struct Point +{ + long long x, y; +} p[10010]; +long long Where(long long pos, Point A) +{ + long long x = A.x, y = A.y; + if (D * D - (y - pos) * (y - pos) < 0) + { + return -99999999999ll; + } + return (long long)(x + sqrt(D * D - (y - pos) * (y - pos))); +} + +bool Getdist(long long x, long long y, long long h, long long l) +{ + if ((h - x) * (h - x) + (y - l) * (y - l) <= D * D) + { + return true; + } + return false; +} +long long pos; +bool cmp(const Point &A, const Point &B) +{ + return Where(pos, A) < Where(pos, B); +} + +bool Judge(long long DIST) +{ + pos = K - DIST; + for (int i = 1; i <= N; i++) + if (Where(pos, p[i]) == -99999999999ll) + { + return false; + } + sort(p + 1, p + 1 + N, cmp); + Point last; + last.x = Where(pos, p[1]); + last.y = pos; + int need = 1; + for (int i = 2; i <= N; i++) + { + if (Getdist(last.x, last.y, p[i].x, p[i].y)) + { + continue; + } + else + { + last.x = Where(pos, p[i]); + need++; + } + } + return (need <= S); +} + +int main() +{ + int T; + scanf("%d", &T); + int cas = 0; + while (T--) + { + scanf("%lld%lld%lld%lld", &K, &N, &S, &D); + for (int i = 1; i <= N; i++) + { + scanf("%lld%lld", &p[i].x, &p[i].y); + } + long long L = 0, R = D, mid; + if (!Judge(0)) + { + printf("Case %d: IMPOSSIBLE\n", ++cas); + continue; + } + while (L < R) + { + mid = (L + R + 1) / 2; + if (Judge(mid)) + { + L = mid; + } + else + { + R = mid - 1; + } + } + printf("Case %d: %lld\n", ++cas, L); + } + return 0; +} diff --git a/12256.cpp b/12256.cpp new file mode 100644 index 0000000..f302d01 --- /dev/null +++ b/12256.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +long long ans[62]; + +int main() +{ + ans[1] = ans[2] = ans[3] = 1; + for (int i = 4; i < 62; ++i) + { + ans[i] = ans[i - 1] + ans[i - 2] + ans[i - 3]; + } + for (int c = 0, n; scanf("%d", &n) == 1 && n;) + { + printf("Case %d: %lld\n", ++c, ans[n]); + } + return 0; +} diff --git a/12257.cpp b/12257.cpp new file mode 100644 index 0000000..738c1be --- /dev/null +++ b/12257.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define MOD 1000000007 + +int lst[1010][1010], super[1010], sons[1010]; +long long pascal[1010][1010], resu[1010]; + +int cal(int boss) +{ + resu[boss] = 1; + sons[boss] = 0; + for (int i = 1; i <= lst[boss][0]; i++) + { + int cnt = lst[boss][i]; + cal(cnt); + resu[boss] = (resu[boss] * resu[cnt]) % MOD; + sons[boss] += sons[cnt] + 1; + } + int temp = sons[boss]; + for (int i = 1; i <= lst[boss][0]; i++) + { + int cnt = lst[boss][i]; + resu[boss] = (resu[boss] * pascal[temp][sons[cnt] + 1]) % MOD; + temp -= sons[cnt] + 1; + } + return 0; +} + +int main() +{ + int CASL; + pascal[0][0] = 1; + for (int i = 1; i <= 1000; i++) + { + pascal[i][0] = 1; + for (int j = 1; j < i; j++) + { + pascal[i][j] = (pascal[i - 1][j] + pascal[i - 1][j - 1]) % MOD; + } + pascal[i][i] = 1; + } + scanf("%d", &CASL); + for (int CAS = 1; CAS <= CASL; CAS++) + { + int n; + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + lst[i][0] = 0; + super[i] = 0; + } + for (int i = 1; i < n; i++) + { + int s, t; + scanf("%d%d", &s, &t); + super[t] = s; + lst[s][++lst[s][0]] = t; + } + int boss; + for (int i = 1; i <= n; i++) + { + if (super[i] == 0) + { + boss = i; + } + } + cal(boss); + printf("Case %d: %lld\n", CAS, resu[boss]); + } + return 0; +} diff --git a/12259.cpp b/12259.cpp new file mode 100644 index 0000000..b3e2ff6 --- /dev/null +++ b/12259.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +int v[128], p[128], idx[128]; + +bool cmp(int a, int b) +{ + return v[a] > v[b] || (v[a] == v[b] && a < b); +} + +int min(int a, int b) +{ + return a < b ? a : b; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + int n, q, sum = 0; + scanf("%d %d", &q, &n); + for (int i = 0; i < n; ++i) + { + scanf("%d", &v[i]); + sum += v[i]; + p[i] = 0; + idx[i] = i; + } + if (sum < q) + { + puts("IMPOSSIBLE"); + } + else + { + sort(idx, idx + n, cmp); + while (q) + { + int cp; + for (cp = n; cp--;) + if (v[idx[cp]]) + { + break; + } + int pm = 0, qp = v[idx[cp]], tp = qp * (++cp); + if (tp > q) + { + qp = q / cp; + pm = q % cp; + tp = q; + } + for (int i = 0; i < cp; ++i) + { + int x = idx[i]; + if (v[x] > 0) + { + p[x] += qp; + v[x] -= qp; + if (pm && v[x]) + { + p[x]++, v[x]--, pm--; + } + } + } + q -= tp; + } + for (int i = 0; i < n; ++i) + { + if (i) + { + putc(32, stdout); + } + printf("%d", p[i]); + } + putc(10, stdout); + } + } + return 0; +} diff --git a/1226.cpp b/1226.cpp new file mode 100644 index 0000000..a947caf --- /dev/null +++ b/1226.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int t, n, i; + char s[3000]; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + scanf("%s", s); + int tmp = 0; + for (i = 0; s[i]; i++) + { + tmp = tmp * 10 + s[i] - '0'; + tmp %= n; + } + printf("%d\n", tmp); + } + return 0; +} diff --git a/12260.cpp b/12260.cpp new file mode 100644 index 0000000..57d01f1 --- /dev/null +++ b/12260.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define MAXN 1000 + +typedef pair PII; +PII dp[MAXN + 1][MAXN + 1], val[MAXN]; +int t, n; +char name[21]; + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + scanf("%s", name); + int jan_start; + if (!strcmp(name, "Jan")) + { + jan_start = 1; + } + else + { + jan_start = 0; + } + for (int i = 0; i < n; ++i) + { + scanf("%d %d", &val[i].first, &val[i].second); + val[i].first *= -1; + } + sort(val, val + n); + for (int i = 1; i <= n; ++i) + { + dp[i][0].second = dp[i - 1][0].second - val[i - 1].first; + dp[i][(i + jan_start) / 2 + 1] = PII(0, 0); + for (int j = 1; j <= (i + jan_start) / 2; ++j) + dp[i][j] = max(PII(dp[i - 1][j].first, dp[i - 1][j].second - val[i - 1].first), PII(dp[i - 1][j - 1].first + val[i - 1].second, dp[i - 1][j - 1].second)); + } + printf("%d %d\n", dp[n][(n + jan_start) / 2].second, dp[n][(n + jan_start) / 2].first); + } + return 0; +} diff --git a/12261.cpp b/12261.cpp new file mode 100644 index 0000000..7ad7d66 --- /dev/null +++ b/12261.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +const int NN = 1048; + +char s[NN]; +int m[NN], r[2]; + +int min(int a, int b) +{ + return a < b ? a : b; +}; + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%s", s); + int e = 0, L = 0, mov = 0; + for (char *p = s; *p; ++p, mov += m[L++]) + { + m[L] = min(*p - 'A', 'Z' - *p + 1); + } + if (!mov) + { + puts("0"); + continue; + } + r[0] = r[1] = L - 1; + for (int ri = 0; ri < 2; ++ri) + { + for (int i = 0; i < L; ++i) + { + if (m[i]) + { + if (e && e != i) + { + r[ri] = min(r[ri], L + i - 2 - (e << 1)); + } + e = 0; + } + e += !m[i]; + } + if (e) + { + r[ri] = min(r[ri], L - 1 - e); + } + e = 0; + for (int i = 1, t; i < (L >> 1); ++i) + { + t = m[L - i], m[L - i] = m[i], m[i] = t; + } + } + printf("%d\n", mov + min(r[0], r[1])); + } + return 0; +} diff --git a/12262.cpp b/12262.cpp new file mode 100644 index 0000000..26bdeb4 --- /dev/null +++ b/12262.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +const int N = 10001; +const double EPS = 1e-10; + +double sum, a, b, v, f, x[N], y[N]; +int n; + +bool judge(double mid, double &tt) +{ + double v = mid; + double ff = 0; + tt = 0; + for (int i = 1; i <= n; i++) + { + double c; + if (y[i] >= 0) + { + c = a * v + b * y[i] / x[i]; + tt += hypot(x[i], y[i]) / v; + } + else + { + c = max(0.0, a * v + b * y[i] / x[i]); + if (fabs(c) < EPS) + { + double vv = min(-b * y[i] / x[i] / a, ::v); + tt += hypot(x[i], y[i]) / vv; + } + else + tt += hypot(x[i], y[i]) / v; + } + ff += c * hypot(x[i], y[i]); + } + return ff <= f; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%lf%lf%lf%lf", &a, &b, &v, &f); + sum = 0; + scanf("%d", &n); + assert(n > 0); + for (int i = 1; i <= n; i++) + { + scanf("%lf%lf", &x[i], &y[i]); + x[i] /= 1000; + y[i] /= 1000; + sum += hypot(x[i], y[i]); + } + + double low = 0, high = v, mid, tt, ans; + bool ok = false; + while (high - low > EPS) + { + mid = (low + high) / 2; + if (judge(mid, tt)) + { + ans = tt; + low = mid; + ok = true; + } + else + high = mid; + } + + if (!ok) + printf("IMPOSSIBLE\n"); + else + printf("%.10lf\n", ans); + } + return 0; +} diff --git a/12263.cpp b/12263.cpp new file mode 100644 index 0000000..1870dfa --- /dev/null +++ b/12263.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +int s[505][505], in[505], a[505], ans[505], use[505]; + +int main() +{ + int T, N, n, M, m, i, j, x, y; + scanf(" %d", &T); + while (T--) + { + scanf(" %d", &N); + for (n = 0; n < N; n++) + scanf(" %d", &a[n]); + memset(s, 0, sizeof(s)); + memset(in, 0, sizeof(in)); + for (i = 0; i < N; i++) + for (j = i + 1; j < N; j++) + s[a[i]][a[j]] = 1; + scanf(" %d", &M); + for (m = 0; m < M; m++) + { + scanf(" %d %d", &i, &j); + for (n = 0; n < N; n++) + { + if (a[n] == i) + x = n; + else if (a[n] == j) + y = n; + } + if (x < y) + { + s[i][j] = 0; + s[j][i] = 1; + } + else + { + s[j][i] = 0; + s[i][j] = 1; + } + } + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + if (s[i][j] == 1) + in[j]++; + int c = 0; + memset(use, 0, sizeof(use)); + while (c < N) + { + vector v; + v.clear(); + for (i = 1; i <= N; i++) + if (in[i] == 0 && use[i] == 0) + { + v.push_back(i); + use[i] = 1; + } + if (v.size() == 0) + break; + else if (v.size() == 1) + ans[c++] = v[0]; + else + for (i = 0; i < v.size(); i++) + ans[c++] = -1; + for (i = 0; i < v.size(); i++) + { + x = v[i]; + for (j = 1; j <= N; j++) + if (s[x][j] == 1) + { + s[x][j] = 0; + in[j]--; + } + } + } + if (c == N) + { + for (i = 0; i < N; i++) + { + if (i == 0) + { + if (ans[i] == -1) + printf("?"); + else + printf("%d", ans[i]); + } + else + { + if (ans[i] == -1) + printf(" ?"); + else + printf(" %d", ans[i]); + } + } + printf("\n"); + } + else + printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/12264.cpp b/12264.cpp new file mode 100644 index 0000000..c4d46c7 --- /dev/null +++ b/12264.cpp @@ -0,0 +1,261 @@ +#include + +using namespace std; + +#define LEFT(i) ((i) << 1) +#define RIGHT(i) (((i) << 1) | 1) +#define MID(i) ((l[i] + r[i]) >> 1) +#define CC(i, v) memset(i, v, sizeof(i)) +#define REP(i, l, n) for (int i = l; i < int(n); ++i) +#define FOREACH(con, i) for (__typeof(con.begin()) i = con.begin(); i != con.end(); ++i) +#define OP(i) (((i) - (pool)) ^ 1) + +const int INF = 100000000; + +struct sap +{ + const static int V = 2010, E = 1000000, INF = 100000000; + + bool reachS[V], reachT[V]; + int dis[V], numdis[V]; + + struct edge + { + int v, cap; + edge *nxt; + } pool[E], *g[V], *pp; + edge *e[V], *pree[V]; + + int pre[V], maxflow; + + void bfs(int v, int n) + { + int que[V], tail = 0; + bool vst[V] = {0}; + memset(numdis, 0, sizeof(numdis)); + fill(dis, dis + n, n); + dis[v] = 0, vst[v] = 1, que[0] = v; + for (int j = 0; j <= tail; j++) + { + int tmp = que[j % n]; + for (edge *i = g[tmp]; i != NULL; i = i->nxt) + { + if (pool[OP(i)].cap > 0 && !vst[i->v]) + { + tail++; + vst[i->v] = 1; + que[tail % n] = i->v; + dis[i->v] = dis[tmp] + 1; + numdis[dis[i->v]]++; + } + } + } + } + int augment_path(int &v, int s, int t) + { + while (e[v] != NULL) + { + if (e[v]->cap > 0 && dis[v] == dis[e[v]->v] + 1) + { + pre[e[v]->v] = v, pree[e[v]->v] = e[v], v = e[v]->v; + if (v == t) + { + int minf = INF; + for (int i = t; i != s; i = pre[i]) + { + minf = min(minf, pree[i]->cap); + } + for (int i = t; i != s; i = pre[i]) + { + pree[i]->cap -= minf; + pool[OP(pree[i])].cap += minf; + } + v = s; + return minf; + } + } + else + { + e[v] = e[v]->nxt; + } + } + return 0; + } + + int maxflow(int n, int s, int t) + { + bfs(t, n); + int v = s; + copy(g, g + n, e); + while (dis[s] < n) + { + int add = augment_path(v, s, t); + maxflow += add; + if (add == 0) + { + int mindis = n; + numdis[dis[v]]--; + if (!numdis[dis[v]]) + { + break; + } + for (edge *i = g[v]; i != NULL; i = i->nxt) + { + if (i->cap > 0) + { + mindis = min(mindis, dis[i->v] + 1); + } + } + dis[v] = mindis; + numdis[dis[v]]++; + e[v] = g[v]; + if (v != s) + { + v = pre[v]; + } + } + } + return maxflow; + } + void firststart() + { + pp = pool; + maxflow = 0; + memset(g, 0, sizeof(g)); + } + void add_edge(int i, int j, int cap) + { + pp->v = j; + pp->cap = cap; + pp->nxt = g[i]; + g[i] = pp++; + } + void dfss(int x) + { + reachS[x] = true; + for (edge *i = g[x]; i != NULL; i = i->nxt) + if (i->cap && !reachS[i->v]) + { + dfss(i->v); + } + } + void dfst(int x) + { + reachT[x] = true; + for (edge *i = g[x]; i != NULL; i = i->nxt) + if (pool[OP(i)].cap && !reachT[i->v]) + { + dfst(i->v); + } + } +} flow; + +#define FIRST(i) ((i)) +#define SECOND(i) ((i) + n) + +const int N = 110; + +char maps[N][N]; +int a[N], eni[N]; + +void find_adj(int n) +{ + memset(eni, 0, sizeof(eni)); + for (int i = 1; i <= n; i++) + { + if (a[i]) + { + continue; + } + for (int j = 1; j <= n; j++) + { + if (maps[j][i] == 'Y' && a[j]) + { + eni[j] = true; + } + } + } +} +int build(int n, int mid, int s, int t) +{ + int ff = 0; + for (int i = 1; i <= n; i++) + { + if (a[i] == 0) + { + continue; + } + flow.add_edge(s, FIRST(i), a[i]); + flow.add_edge(FIRST(i), s, 0); + flow.add_edge(FIRST(i), SECOND(i), INF); + flow.add_edge(SECOND(i), FIRST(i), 0); + if (eni[i]) + { + flow.add_edge(SECOND(i), t, mid); + flow.add_edge(t, SECOND(i), 0); + ff += mid; + } + else + { + flow.add_edge(SECOND(i), t, 1); + flow.add_edge(t, SECOND(i), 0); + ff++; + } + for (int j = 1; j <= n; j++) + { + if (a[j] == 0 || maps[i][j] == 'N') + { + continue; + } + flow.add_edge(FIRST(i), SECOND(j), INF); + flow.add_edge(SECOND(j), FIRST(i), 0); + } + } + return ff; +} +int ans(int n) +{ + int low = 2, high = INF, ans = 1; + int s = 0, t = n * 2 + 1; + while (low <= high) + { + int mid = (low + high) / 2; + flow.firststart(); + int tmp = build(n, mid, s, t); + int f = flow.maxflow(t + 1, s, t); + if (f == tmp) + { + ans = mid; + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return ans; +} + +int main() +{ + int T, n; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + scanf(" %c", &maps[i][j]); + } + } + find_adj(n); + printf("%d\n", ans(n)); + } + return 0; +} diff --git a/12265.cpp b/12265.cpp new file mode 100644 index 0000000..31cbe3f --- /dev/null +++ b/12265.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define P pair + +int n, m; +char s[1001][1001]; +int mh[1001][1001]; + +int main() +{ + int t; + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> n >> m; + for (int i = 0; i < n; i++) + { + scanf("%s", s[i]); + for (int j = 0; j < m; j++) + { + if (s[i][j] == '#') + { + mh[i][j] = 0; + } + else + { + if (!i) + { + mh[i][j] = 1; + } + else + { + mh[i][j] = mh[i - 1][j] + 1; + } + } + } + } + int ans[5000] = {0}; + for (int i = 0; i < n; i++) + { + P p[1001]; + for (int j = 0; j < m; j++) + { + p[j].first = mh[i][j]; + p[j].second = j; + } + sort(p, p + m); + set st; + int H[1001] = {0}, L[1001] = {0}; + for (int j = 0; j < m; j++) + { + if (!p[j].first) + { + st.insert(p[j].second); + continue; + } + int x = p[j].second; + set::iterator it = st.lower_bound(p[j].second); + if (it != st.begin()) + { + it--; + int h1 = H[*it] + L[*it] + p[j].second - *it; + int h2 = p[j].first + p[j].second - *it; + if (h1 >= h2) + { + H[x] = H[*it]; + L[x] = L[*it] + p[j].second - *it; + } + else + { + H[x] = p[j].first; + L[x] = p[j].second - *it; + } + } + else + { + H[x] = p[j].first; + L[x] = p[j].second + 1; + } + st.insert(x); + int k = (H[x] + L[x]) * 2; + ans[k]++; + } + } + for (int i = 1; i < 5000; i++) + { + if (ans[i]) + { + printf("%d x %d\n", ans[i], i); + } + } + } + return 0; +} diff --git a/12266.cpp b/12266.cpp new file mode 100644 index 0000000..c5a4579 --- /dev/null +++ b/12266.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +const int maxn = 505; +int n; + +struct Buy +{ + int num, p; + bool operator<(const Buy &x) const + { + return p < x.p; + } +} buy; +struct Sell +{ + int num, p; + bool operator<(const Sell &x) const + { + return p > x.p; + } +} sell; +priority_queue b; +priority_queue s; +int ai, bi, si; + +void solve() +{ + int tmp; + ai = bi = -1; + while (b.size() && s.size()) + { + buy = b.top(); + sell = s.top(); + if (buy.p >= sell.p) + { + b.pop(); + s.pop(); + tmp = min(buy.num, sell.num); + si = sell.p; + buy.num -= tmp, sell.num -= tmp; + if (buy.num) + b.push(buy); + if (sell.num) + s.push(sell); + } + else + break; + } + if (b.size()) + bi = b.top().p; + if (s.size()) + ai = s.top().p; +} + +int main() +{ + int ca, x, y; + char op[15], tmp[15]; + scanf("%d", &ca); + while (ca--) + { + si = -1; + scanf("%d", &n); + while (!b.empty()) + b.pop(); + while (!s.empty()) + s.pop(); + while (n--) + { + scanf("%s%d%s%s%d", op, &x, tmp, tmp, &y); + if (op[0] == 'b') + { + buy.num = x, buy.p = y; + b.push(buy); + } + else + { + sell.num = x, sell.p = y; + s.push(sell); + } + solve(); + if (ai > 0) + printf("%d ", ai); + else + printf("- "); + if (bi > 0) + printf("%d ", bi); + else + printf("- "); + if (si > 0) + printf("%d\n", si); + else + printf("-\n"); + } + } + return 0; +} diff --git a/12267.cpp b/12267.cpp new file mode 100644 index 0000000..70fbc20 --- /dev/null +++ b/12267.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +/* *********************************************** +Author :kuangbin +Created Time :2014/6/14 10:49:42 +File Name :E:\2014ACM\区域赛练习\2010\2010NWERC\I.cpp +************************************************ */ + +pair p[100010]; +int Index[100010]; +int tmpIndex[100010]; +int ans[100010]; +vector vec[100010]; +int color[100010]; +bool dfs(int u, int col) +{ + color[u] = col; + int sz = vec[u].size(); + for (int i = 0; i < sz; i++) + { + int v = vec[u][i]; + if (color[v] != -1) + { + if (color[v] == col) + { + return false; + } + continue; + } + if (!dfs(v, !col)) + { + return false; + } + } + return true; +} + +struct Node +{ + int val; + int id; +} node[100010]; +bool cmp(Node a, Node b) +{ + return a.val < b.val; +} +void solve(int n, int l, int r) +{ + if (n == 0) + { + return; + } + if (l >= r) + { + return; + } + for (int i = l; i < r; i++) + { + vec[i].clear(); + } + int cnt = 0; + for (int i = l; i < r; i++) + { + node[cnt].val = (p[Index[i]].first & ((1 << (n - 1)) - 1)); + node[cnt].id = i; + cnt++; + } + sort(node, node + cnt, cmp); + for (int i = 1; i < cnt; i++) + if (node[i - 1].val == node[i].val) + { + vec[node[i - 1].id].push_back(node[i].id); + vec[node[i].id].push_back(node[i - 1].id); + } + cnt = 0; + for (int i = l; i < r; i++) + { + node[cnt].val = (p[Index[i]].second & ((1 << (n - 1)) - 1)); + node[cnt].id = i; + cnt++; + } + sort(node, node + cnt, cmp); + for (int i = 1; i < cnt; i++) + if (node[i - 1].val == node[i].val) + { + vec[node[i - 1].id].push_back(node[i].id); + vec[node[i].id].push_back(node[i - 1].id); + } + for (int i = l; i < r; i++) + { + color[i] = -1; + } + for (int i = l; i < r; i++) + if (color[i] == -1) + { + dfs(i, 0); + } + int cnt1 = l; + for (int i = l; i < r; i++) + if (color[i] == 0) + { + tmpIndex[cnt1++] = Index[i]; + } + int cnt2 = cnt1; + for (int i = l; i < r; i++) + if (color[i] == 1) + { + tmpIndex[cnt2++] = Index[i]; + ans[Index[i]] += (1 << (n - 1)); + } + for (int i = l; i < r; i++) + { + Index[i] = tmpIndex[i]; + } + solve(n - 1, l, cnt1); + solve(n - 1, cnt1, r); +} + +int main() +{ + //freopen("in.txt","r",stdin); + //freopen("out.txt","w",stdout); + int n, m; + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < m; i++) + { + Index[i] = i; + scanf("%d%d", &p[i].first, &p[i].second); + } + memset(ans, 0, sizeof(ans)); + solve(n, 0, m); + for (int i = 0; i < m; i++) + { + printf("%d", ans[i]); + if (i < m - 1) + { + printf(" "); + } + else + { + printf("\n"); + } + } + } + return 0; +} diff --git a/12268.cpp b/12268.cpp new file mode 100644 index 0000000..4916b6e --- /dev/null +++ b/12268.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +#define DBLE 1e-8 +#define PI 3.1415926535898 +#define INF 1000000000 +#define MAXN 2000010 +#define MOD 1000000009 +#define MP(x, y) make_pair((x), (y)) + +char str[MAXN]; +int sum[MAXN]; + +int main() +{ + int ncase, l, b, n, p1, p2, tmp; + long long ans; + scanf("%d", &ncase); + while (ncase--) + { + scanf("%d%d%d", &l, &b, &n); + memset(sum, 0, sizeof(sum)); + scanf("%s", &str[1]); + for (int i = 1; i <= n; ++i) + sum[i] = sum[i - 1] + (str[i] == '0' ? 1 : 0); + p1 = 1, p2 = b; + ans = 0; + while (l != n) + { + if (p1 + l - 1 == n) + break; + while (sum[p2] - sum[p1] + l + p1 <= p2 && p1 < p2) + ++p1; + ans += l; + if (p1 + l - 1 == n) + break; + if (p2 - p1 + 1 == b) + break; + while (p2 < n && p2 - p1 + 1 < b) + ++p2, ++ans; + } + + if (p2 == n && p1 + l - 1 == n) + printf("%lld\n", ans); + else + printf("IMPOSSIBLE\n"); + } + return 0; +} diff --git a/12269.cpp b/12269.cpp index 1df7c25..a4274d9 100644 --- a/12269.cpp +++ b/12269.cpp @@ -1,31 +1,54 @@ -#include -#include -using namespace std; +#include -bool test(double w, int max, int n) { - double v[n]; - for (int i = 0; i < n; i++) - scanf("%lf", &v[i]); - sort(v, v + n); - bool ok = (v[0] <= w/2) && ((max - v[n - 1]) <= w/2); - - for (int i=0; i w) ok = false; - } - - return ok; -} +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - int n1, n2; +const int NN = 1048; +double x[NN], y[NN]; +int main() +{ + int nx, ny; double w; - while (scanf("%d%d%lf", &n1, &n2, &w), w > 0) { - bool x = test(w, 75, n1); - bool y = test(w, 100, n2); - printf("%s\n", x && y ? "YES" : "NO"); + while (scanf("%d %d %lf", &nx, &ny, &w) == 3 && (nx || ny)) + { + for (int i = 0; i < nx; ++i) + { + scanf("%lf", &x[i]); + } + for (int i = 0; i < ny; ++i) + { + scanf("%lf", &y[i]); + } + sort(x, x + nx); + sort(y, y + ny); + w /= 2.0; + if (x[0] - w > 0 || y[0] - w > 0 || x[nx - 1] + w < 75 || y[ny - 1] + w < 100) + { + puts("NO"); + } + else + { + bool ok = 1; + double r = 0; + for (int i = 0; ok && i < nx; ++i) + { + ok &= !(x[i] - w > r); + r = x[i] + w; + } + r = 0; + for (int i = 0; ok && i < ny; ++i) + { + ok &= !(y[i] - w > r); + r = y[i] + w; + } + if (ok) + { + puts("YES"); + } + else + { + puts("NO"); + } + } } return 0; } diff --git a/1227.cpp b/1227.cpp new file mode 100644 index 0000000..abe5d36 --- /dev/null +++ b/1227.cpp @@ -0,0 +1,274 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < n; i++) + +unsigned char mask[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; +#define tget(i) ((t[(i) / 8] & mask[(i) % 8]) ? 1 : 0) +#define tset(i, b) t[(i) / 8] = (b) ? (mask[(i) % 8] | t[(i) / 8]) : ((~mask[(i) % 8]) & t[(i) / 8]) +#define chr(i) (cs == sizeof(int) ? ((int *)s)[i] : ((unsigned char *)s)[i]) +#define isLMS(i) (i > 0 && tget(i) && !tget(i - 1)) +// find the start or end of each bucket +void getBuckets(unsigned char *s, int *bkt, int n, int K, int cs, bool end) +{ + int i, sum = 0; + for (i = 0; i <= K; i++) + { + bkt[i] = 0;// clear all buckets + } + for (i = 0; i < n; i++) + { + bkt[chr(i)]++;// compute the size of each bucket + } + for (i = 0; i <= K; i++) + { + sum += bkt[i]; + bkt[i] = end ? sum : sum - bkt[i]; + } +} +// compute SAl +void induceSAl(unsigned char *t, int *SA, unsigned char *s, int *bkt, + int n, int K, int cs, bool end) +{ + int i, j; + getBuckets(s, bkt, n, K, cs, end);// find starts of buckets + for (i = 0; i < n; i++) + { + j = SA[i] - 1; + if (j >= 0 && !tget(j)) + { + SA[bkt[chr(j)]++] = j; + } + } +} +// compute SAs +void induceSAs(unsigned char *t, int *SA, unsigned char *s, int *bkt, + int n, int K, int cs, bool end) +{ + int i, j; + getBuckets(s, bkt, n, K, cs, end);// find ends of buckets + for (i = n - 1; i >= 0; i--) + { + j = SA[i] - 1; + if (j >= 0 && tget(j)) + { + SA[--bkt[chr(j)]] = j; + } + } +} +// find the suffix array SA of s[0..n-1] in {1..K}^n +// require s[n-1]=0 (the sentinel!), n>=2 +// use a working space (excluding s and SA) of at most 2.25n+O(1) for a constant alphabet +void SA_IS(unsigned char *s, int *SA, int n, int K, int cs) +{ + int i, j; + unsigned char *t = (unsigned char *)malloc(n / 8 + 1);// LS-type array in bits + // Classify the type of each character + tset(n - 2, 0); + tset(n - 1, 1);// the sentinel must be in s1, important!!! + for (i = n - 3; i >= 0; i--) + { + tset(i, (chr(i) < chr(i + 1) || (chr(i) == chr(i + 1) && tget(i + 1) == 1)) ? 1 : 0); + } + // stage 1: reduce the problem by at least 1/2 + // sort all the S-substrings + int *bkt = (int *)malloc(sizeof(int) * (K + 1));// bucket array + getBuckets(s, bkt, n, K, cs, true); // find ends of buckets + for (i = 0; i < n; i++) + { + SA[i] = -1; + } + for (i = 1; i < n; i++) + if (isLMS(i)) + { + SA[--bkt[chr(i)]] = i; + } + induceSAl(t, SA, s, bkt, n, K, cs, false); + induceSAs(t, SA, s, bkt, n, K, cs, true); + free(bkt); + // compact all the sorted substrings into the first n1 items of SA + // 2*n1 must be not larger than n (proveable) + int n1 = 0; + for (i = 0; i < n; i++) + if (isLMS(SA[i])) + { + SA[n1++] = SA[i]; + } + // find the lexicographic names of all substrings + for (i = n1; i < n; i++) + { + SA[i] = -1;// init the name array buffer + } + int name = 0, prev = -1; + for (i = 0; i < n1; i++) + { + int pos = SA[i]; + bool diff = false; + for (int d = 0; d < n; d++) + if (prev == -1 || chr(pos + d) != chr(prev + d) || tget(pos + d) != tget(prev + d)) + { + diff = true; + break; + } + else if (d > 0 && (isLMS(pos + d) || isLMS(prev + d))) + { + break; + } + if (diff) + { + name++; + prev = pos; + } + pos = (pos % 2 == 0) ? pos / 2 : (pos - 1) / 2; + SA[n1 + pos] = name - 1; + } + for (i = n - 1, j = n - 1; i >= n1; i--) + if (SA[i] >= 0) + { + SA[j--] = SA[i]; + } + // stage 2: solve the reduced problem + // recurse if names are not yet unique + int *SA1 = SA, *s1 = SA + n - n1; + if (name < n1) + { + SA_IS((unsigned char *)s1, SA1, n1, name - 1, sizeof(int)); + } + else// generate the suffix array of s1 directly + for (i = 0; i < n1; i++) + { + SA1[s1[i]] = i; + } + // stage 3: induce the result for the original problem + bkt = (int *)malloc(sizeof(int) * (K + 1));// bucket array + // put all left-most S characters into their buckets + getBuckets(s, bkt, n, K, cs, true);// find ends of buckets + for (i = 1, j = 0; i < n; i++) + if (isLMS(i)) + { + s1[j++] = i;// get p1 + } + for (i = 0; i < n1; i++) + { + SA1[i] = s1[SA1[i]];// get index in s + } + for (i = n1; i < n; i++) + { + SA[i] = -1;// init SA[n1..n-1] + } + for (i = n1 - 1; i >= 0; i--) + { + j = SA[i]; + SA[i] = -1; + SA[--bkt[chr(j)]] = j; + } + induceSAl(t, SA, s, bkt, n, K, cs, false); + induceSAs(t, SA, s, bkt, n, K, cs, true); + free(bkt); + free(t); +} + +const int MX = 6000100; + +char in[MX]; +int n, m, len[MX], deq[MX]; +int which[MX], sa[MX], lcp[MX], b[MX]; + +void buildLCP(char *t, int n, int *a, int *lcp) +{ + int h = 0; + rep(i, n + 1) b[a[i]] = i; + rep(i, n + 1) + { + if (b[i]) + { + for (int j = a[b[i] - 1]; j + h < n && i + h < n && t[j + h] == t[i + h]; ++h) + ; + lcp[b[i]] = h; + } + else + { + lcp[b[i]] = -1; + } + if (h > 0) + { + --h; + } + } +} + +int main() +{ + int cs; + scanf("%d", &cs); + while (cs--) + { + scanf("%d", &n); + m = 0; + rep(i, n) + { + scanf("%s", in + m); + len[i] = strlen(in + m); + m += len[i]; + in[m++] = '0' + i; + len[i] = m; + } + len[m] = 0; + SA_IS((unsigned char *)in, sa, m + 1, 256, sizeof(char)); + buildLCP(in, m, sa, lcp); + int cur = 0; + rep(i, m + 1) + { + if (i == len[cur] - 1) + { + which[b[i]] = n, cur++; + } + else + { + which[b[i]] = cur; + } + } + int ans = 0, k = 0, use[6] = {}, h = 0, t = 0; + for (int l = 0, r = 0; l < m; l++) + { + while (l > r) + { + r++; + } + while (r <= m && k < n) + { + int idx = which[r]; + if (idx < n && use[idx]++ == 0) + { + k++; + } + while (h < t && lcp[deq[t - 1]] >= lcp[r]) + { + t--; + } + if (r) + { + deq[t++] = r; + } + r++; + } + if (k < n) + { + break; + } + ans = max(ans, lcp[deq[h]]); + if (h < t && deq[h] <= l + 1) + { + h++; + } + int idx = which[l]; + if (idx < n && --use[idx] == 0) + { + k--; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12270.cpp b/12270.cpp new file mode 100644 index 0000000..3c22538 --- /dev/null +++ b/12270.cpp @@ -0,0 +1,198 @@ +#include + +using namespace std; + +// O(m^3·log(n) + m·n) + +#define MAX 80 +int module; + +vector> matrixproduct(vector> a, vector> b) +{ + int m = a.size(); + vector> ret(m, vector(m)); + for (int i = 0; i < m; i++) + for (int j = 0; j < m; j++) + for (int k = 0; k < m; k++) + { + ret[i][j] += a[i][k] * b[k][j]; + ret[i][j] %= module; + } + return ret; +} + +vector> exp(vector> a, int n) +{ + //computes a^n in O(logn) multiplications + if (n == 1) + { + return a; + } + vector> ret, b; + b = exp(a, n / 2); + ret = matrixproduct(b, b); + if (n % 2 != 0) + { + ret = matrixproduct(ret, a); + } + return ret; +} + +vector compose(vector a, vector b) +{ + int m = a.size(); + vector ret(m); + for (int i = 0; i < m; i++) + { + if (b[i] == -1) + { + ret[i] = -1; + } + else + { + ret[i] = a[b[i]]; + } + } + return ret; +} + +vector iterate(vector a, int n) +{ + if (n == 1) + { + return a; + } + vector ret, b; + b = iterate(a, n / 2); + ret = compose(b, b); + if (n % 2 != 0) + { + ret = compose(ret, a); + } + return ret; +} + +int main() +{ + clock_t start, finish; + start = clock(); + for (;;) + { + //input + int m, f[MAX + 1]; + cin >> m; + if (!m) + { + break; + } + for (int i = 0; i <= m; i++) + { + cin >> f[i]; + } + int n; + cin >> n >> module; + vector> a(m, vector(m)); + for (int i = 0; i < m; i++) + for (int j = 0; j < m; j++) + { + a[i][j] = 0LL; + } + for (int i = 0; i < m; i++) + { + int x = f[i]; + int y = f[i + 1]; + if (x > y) + { + int z = y; + y = x; + x = z; + } + for (int j = x; j < y; j++) + { + a[j][i] = 1LL; + } + } + a = exp(a, n); + //the trace of this matrix will be the output after minor + //changes concerning points of the form i/m and their neighborhoods + int ret = 0; + for (int i = 0; i < m; i++) + { + ret = (ret + a[i][i]) % module; + } + vector b(3 * (m + 1)); + for (int i = 0; i < 3 * (m + 1); i++) + { + int d = (i % 3) - 1; + int k = i / 3; + int dir = 0; + if (k + d >= 0 && k + d <= m) + { + if (f[k + d] > f[k]) + { + dir = 1; + } + else if (f[k + d] < f[k]) + { + dir = -1; + } + } + b[i] = 3 * f[k] + 1 + dir; + } + b = iterate(b, n); + //b[3*i+1] = 3*j+1 iff f^n(i/m) = j/m + for (int i = 0; i < 3 * (m + 1); i++) + if (i > 0 && i <= 3 * m && (i % 3) != 1) + if (b[i] == i) + { + ret = (ret + module - 1) % module; + } + for (int i = 0; i <= m; i++) + if (b[3 * i + 1] == 3 * i + 1) + { + ret = (ret + 1) % module; + } + //decide whether there is an infinite number of solutions + bool infinite = false; + for (int i = 0; i < m && !infinite; i++) + { + bool inf = true; + int j, p = 0; + int interval = i; + int orientation = 1; + for (j = 1; j <= n && inf && !p; j++) + { + if (abs(f[interval] - f[interval + 1]) != 1) + { + inf = false; + } + interval = min(f[interval], f[interval + 1]); + if (f[interval] > f[interval + 1]) + { + orientation *= -1; + } + if (interval == i) + { + p = j; + } + } + if (orientation < 0) + { + p = 2 * p; + } + if (inf && p && (n % p == 0)) + { + infinite = true; + } + } + if (infinite) + { + cout << "Infinity" << endl; + continue; + } + cout << ret << endl; + } + finish = clock(); + cerr << "Tiempo: " << (double)(finish - start) / CLOCKS_PER_SEC << endl; + return 0; +} diff --git a/12271.cpp b/12271.cpp new file mode 100644 index 0000000..b834379 --- /dev/null +++ b/12271.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +// O(n^2) randomized solution to 'Comparing answers' (Freivalds' algorithm). +// David Garcia Soriano. + +const int maxn = 1000; +int mat[2][maxn][maxn]; + +int n; + +vector mult(int mi, vector &x) +{ + vector ret(n); + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + ret[i] += mat[mi][i][j] * x[j]; + } + return ret; +} + +int main() +{ + while (scanf("%i", &n), n) + { + for (int nm = 0; nm < 2; ++nm) + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + scanf("%i", &mat[nm][i][j]); + } + vector v(n); + for (int i = 0; i < n; ++i) + { + v[i] = rand(); + } + vector Av = mult(0, v), A2v = mult(0, Av), + Bv = mult(1, v); + bool equal = true; + for (int i = 0; i < n; ++i) + if (A2v[i] != Bv[i]) + { + equal = false; + break; + } + puts(equal ? "YES" : "NO"); + } + return 0; +} diff --git a/12272.cpp b/12272.cpp new file mode 100644 index 0000000..8daed13 --- /dev/null +++ b/12272.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +// o(n^4) soln using max-flow with 'smart' updates +// after the initial max-flow computation, it determines which edges can be removed at no cost. this +// is accomplished by updating the max-flow network to that of a slightly modified graph, with the +// guarantee that the new flow can be at most 1 unit larger than the prev_ious one, so each such step +// only needs o(e) = o(n^2) time +// since this is repeated o(n^2) times, the total running time is o(n^4) + +const int maxn = 192; + +int cap[maxn][maxn], deg[maxn], edge[maxn][maxn], queue_[maxn], prev_[maxn]; + +// leaves residual capacities in cap[] and returs the max flow value +int maxflow(int n, int src, int sink) +{ + int flow = 0, i, v, w; + for (;;) + { + // find shortest augmenting path with BFS + fill(&prev_[0], &prev_[n], -1); + int startq = 0, endq = 0; + prev_[queue_[endq++] = src] = -2; + while (startq != endq && prev_[sink] < 0) + for (v = queue_[startq++], i = deg[v] - 1; i >= 0; --i) + if (prev_[w = edge[v][i]] < 0 && cap[v][w] > 0) + { + prev_[queue_[endq++] = w] = v; + } + if (prev_[sink] == -1) + { + break; + } + // for each reachable vertex adjacent to the sink, find the path from the source, + // remove these edges, and repeat while possible + for (i = 0; i < n; i++) + if (cap[i][sink] && prev_[i] != -1) + { + int minc = cap[i][sink]; + for (v = i; v != src; v = prev_[v]) + { + minc = min(minc, cap[prev_[v]][v]); + } + if (!minc) + { + continue; + } + cap[i][sink] -= minc; + cap[sink][i] += minc; + for (v = i; v != src; v = prev_[v]) + { + cap[prev_[v]][v] -= minc; + cap[v][prev_[v]] += minc; + } + flow += minc; + } + } + return flow; +} + +// add an edge between 'a' and 'b' with capacity 'c' +void add(int a, int b, int c) +{ + edge[a][deg[a]++] = b; + edge[b][deg[b]++] = a; + cap[a][b] = c; + cap[b][a] = 0; +} + +int main() +{ + int ly, lx; + bool first = true; + while (scanf("%i%i", &ly, &lx), ly) + { + vector sumx(lx), sumy(ly); + for (int i = 0; i < ly; ++i) + { + scanf("%i", &sumy[i]); + } + for (int j = 0; j < lx; ++j) + { + scanf("%i", &sumx[j]); + } + if (!first) + { + puts(""); + } + else + { + first = false; + } + int n = ly + lx + 2, src = n - 1, sink = n - 2, + total = accumulate(sumx.begin(), sumx.end(), 0); + fill(°[0], °[n], 0); + for (int i = 0; i < n; ++i) + { + cap[i][sink] = 0; + } + for (int y = 0; y < ly; ++y) + { + add(src, y, sumy[y]); + } + for (int x = 0; x < lx; ++x) + { + add(ly + x, sink, sumx[x]); + } + for (int i = 0; i < ly * lx; ++i) + { + add(i / lx, ly + (i % lx), 1); + } + if (accumulate(sumy.begin(), sumy.end(), 0) != total || maxflow(n, src, sink) != total) + { + printf("Impossible\n"); + } + else + for (int i = 0; i < ly * lx; ++i) + { + int y = i / lx, x = i % lx; + bool canbezero = (cap[y][ly + x] != 0); + if (!canbezero) + { + // try not using this edge + ++cap[src][y]; + ++cap[ly + x][sink]; + cap[y][ly + x] = cap[ly + x][y] = 0; + if (maxflow(n, src, sink) > 0) + { + canbezero = true; + } + else + { + --cap[src][y]; + --cap[ly + x][sink]; + } + } + cap[y][ly + x] = cap[ly + x][y] = 0; + if (canbezero) + { + putchar('N'); + } + else + { + --total; + putchar('Y'); + } + if (i % lx == lx - 1) + { + printf("\n"); + } + } + } + return 0; +} diff --git a/12273.cpp b/12273.cpp new file mode 100644 index 0000000..e866cfe --- /dev/null +++ b/12273.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +// O(n^2 + n * sets) +// ~ 2SAT soln but using diff graph w/ O(n^2) edges + +const int MAXN = 10000; + +int n, parent[MAXN], rank_[MAXN], fst[MAXN]; +char str[MAXN + 1]; + +int find(int v) +{ + return parent[v] == v ? v : parent[v] = find(parent[v]); +} + +void merge(int a, int b) +{ + a = find(a); + b = find(b); + if (a != b) + { + if (rank_[a] < rank_[b]) + { + parent[a] = b; + } + else + { + parent[b] = a; + if (rank_[a] == rank_[b]) + { + ++rank_[a]; + } + } + } +} + +const char alphabet[] = "AGTC"; + +char sol[MAXN + 1], sol2[MAXN + 1]; +bool possib[MAXN][4]; +int idx[256]; + +// comp[i] is a set of pos that need to have equal chars +vector comp[MAXN]; + +// fix the 'i'th character to 'c' and follow all implications +bool dfs(int i, char c) +{ + int rep = find(i); + if (!possib[rep][idx[(int)c]]) + { + return false; + } + bool visited = (sol[rep] != 0); + if (visited) + { + return sol[rep] == c; + } + else + { + sol[rep] = c; + for (int jj = 0; jj < comp[rep].size(); ++jj) + { + int j = comp[rep][jj]; + if (str[j] != c) + { + if (j > 0 && !dfs(j - 1, str[j - 1])) + { + return false; + } + if (j < n - 1 && !dfs(j + 1, str[j + 1])) + { + return false; + } + } + } + } + return true; +} + +bool solvable() +{ + for (int i = 0; i < MAXN; ++i) + { + comp[i].clear(); + } + for (int i = 0; i < n; ++i) + { + // all pos in comp[i] must end up with the same char + comp[find(i)].push_back(i); + } + // first determine which of the four letters are candidates for the value + // of all positions within a component + for (int i = 0; i < n; ++i) + if (!comp[i].empty()) + { + fill(&possib[i][0], &possib[i][4], true); + for (int jj = 0; jj < comp[i].size(); ++jj) + { + int j = comp[i][jj], l = idx[(int)str[j]]; + possib[i][(l + 2) % 4] = false; + } + } + // now try all possible 4 letters for each component and follow all implications. + // by properties of 2sat, if we can't find a contradiction with some value following + // a chain of implications, we can fix this value and move on to the next component, + // with no need for backtracking. + for (int i = 0; i < n; ++i) + { + if (!comp[i].empty() && !sol2[comp[i][0]]) + { + bool can = false; + for (int l = 0; l < 4; ++l) + if (possib[i][l]) + { + copy(&sol2[0], &sol2[n], &sol[0]); + if (dfs(i, alphabet[l])) + { + can = true; + break; + } + } + if (!can) + { + return false; + } + // store possible soln in sol2[] + for (int j = 0; j < n; ++j) + { + if (sol[find(j)]) + { + sol2[j] = sol[find(j)]; + } + } + } + } + return true; +} + +int main() +{ + int subsets; + for (int i = 0; i < 4; ++i) + { + idx[(int)alphabet[i]] = i; + } + while (scanf("%i%i", &n, &subsets), n) + { + scanf("%s", str); + for (int i = 0; i < n; ++i) + { + parent[i] = i; + rank_[i] = 0; + } + for (int i = 0; i < subsets; ++i) + { + int size; + scanf("%i:", &size); + vector v(size); + for (int i = 0; i < size; ++i) + { + scanf("%i", &v[i]); + } + for (int i = 0, j = size - 1; i < j; ++i, --j) + { + merge(v[i], v[j]); + } + } + fill(&sol2[0], &sol2[n], 0); + puts(solvable() ? "YES" : "NO"); + } + return 0; +} diff --git a/12274.cpp b/12274.cpp new file mode 100644 index 0000000..d22ceac --- /dev/null +++ b/12274.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +const int MAXN = 21; + +int dist[1 << MAXN], shoot[1 << MAXN], prev_[1 << MAXN]; + +int main() +{ + int n, m; + while (scanf("%i%i\n", &n, &m), n) + { + int edge[n]; + fill(&edge[0], &edge[n], 0); + for (int i = 0; i < m; ++i) + { + int a, b; + scanf("%i%i", &a, &b); + edge[a] |= 1 << b; + edge[b] |= 1 << a; + } + deque q; + fill(&dist[0], &dist[1 << n], -1); + q.push_back((1 << n) - 1); + dist[(1 << n) - 1] = 0; + while (dist[0] < 0 && !q.empty()) + { + int s = q.front(), d = dist[s]; + q.pop_front(); + int prefix[n + 1], suffix[n + 1]; // or[0..i), or[i..n) + prefix[0] = suffix[n] = 0; + for (int i = 0; i < n; ++i) + { + if (s & (1 << i)) + { + prefix[i + 1] = prefix[i] | edge[i]; + } + else + { + prefix[i + 1] = prefix[i]; + } + } + for (int i = n - 1; i >= 0; --i) + { + if (s & (1 << i)) + { + suffix[i] = suffix[i + 1] | edge[i]; + } + else + { + suffix[i] = suffix[i + 1]; + } + } + for (int i = 0; i < n; ++i) + { + int t = prefix[i] | suffix[i + 1]; + if (dist[t] < 0) + { + dist[t] = d + 1; + shoot[t] = i; + prev_[t] = s; + q.push_back(t); + } + } + } + if (dist[0] < 0) + { + puts("Impossible"); + } + else + { + printf("%i:", dist[0]); + vector path; + int s = 0; + while (s != ((1 << n) - 1)) + { + path.push_back(shoot[s]); + s = prev_[s]; + } + for (int i = path.size() - 1; i >= 0; --i) + { + printf(" %i", path[i]); + } + puts(""); + } + } + return 0; +} diff --git a/12275.cpp b/12275.cpp new file mode 100644 index 0000000..6fddf9c --- /dev/null +++ b/12275.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +// O(VE) + +#define infinity 1000000000 +#define MAXV 350 + +struct edge +{ + int a, b, w; +}; + +class cmp +{ +public: + bool operator()(const edge &e, const edge &f) + { + return (e.w < f.w); + } +}; + +int n, parent[MAXV], ret, minedge, maxedge, treesize, weight[MAXV][MAXV]; +vector edges; + +int commonancestor(int a, int b) +{ + bool vis[MAXV]; + for (int i = 0; i < n; i++) + { + vis[i] = false; + } + int z = a; + vis[z] = true; + do + { + z = parent[z]; + if (z >= 0) + { + vis[z] = true; + } + } while (z >= 0 && parent[z] != z); + int x = b; + while (x >= 0 && !vis[x] && x != parent[x]) + { + x = parent[x]; + } + if (!vis[x]) + { + return -1; + } + else + { + return x; + } +} + +void findremove(int a, int b) +{ + int x = commonancestor(a, b); + edge light; + light.w = infinity; + int y = a; + while (y != x) + { + if (weight[y][parent[y]] < light.w) + { + light.a = y; + light.b = parent[y]; + light.w = weight[y][parent[y]]; + } + y = parent[y]; + } + bool inb = false; + y = b; + while (y != x) + { + if (weight[y][parent[y]] < light.w) + { + light.a = y; + light.b = parent[y]; + light.w = weight[y][parent[y]]; + inb = true; + } + y = parent[y]; + } + //removing the edge + if (inb) + { + y = b; + } + else + { + y = a; + } + vector v; + v.push_back(y); + while (y != light.a) + { + y = parent[y]; + v.push_back(y); + } + for (int i = v.size() - 1; i > 0; i--) + { + parent[v[i]] = v[i - 1]; + } + if (inb) + { + parent[b] = a; + } + else + { + parent[a] = b; + } + treesize--; + //compute the new lightest edge + minedge = infinity; + for (int i = 0; i < n; i++) + if (parent[i] != -1 && parent[i] != i) + { + minedge = min(minedge, weight[i][parent[i]]); + } + return; +} + +void process(edge e) +{ + int x = commonancestor(e.a, e.b); + if (x != -1) + { + findremove(e.a, e.b); + } + else if (parent[e.a] >= 0 && parent[e.b] >= 0) + { + int y = e.a; + vector v; + v.push_back(y); + while (y != parent[y]) + { + y = parent[y]; + v.push_back(y); + } + for (int i = v.size() - 1; i > 0; i--) + { + parent[v[i]] = v[i - 1]; + } + parent[e.a] = e.b; + } + else if (parent[e.a] == -1) + { + parent[e.a] = e.b; + if (parent[e.b] == -1) + { + parent[e.b] = e.b; + } + } + else + { + parent[e.b] = e.a; + } + treesize++; + maxedge = e.w; + if (treesize == 1) + { + minedge = e.w; + } + if (treesize == n - 1) + { + ret = min(ret, maxedge - minedge); + } + return; +} + +int main() +{ + clock_t start, finish; + start = clock(); + for (;;) + { + int nedges; + cin >> n; + if (!n) + { + break; + } + cin >> nedges; + int x, y, z; + edge e; + edges.clear(); + for (int i = 0; i < nedges; i++) + { + cin >> x >> y >> z; + e.a = x; + e.b = y; + e.w = z; + weight[x][y] = z; + weight[y][x] = z; + edges.push_back(e); + } + sort(edges.begin(), edges.end(), cmp()); + memset(parent, -1, sizeof(parent)); + ret = infinity; + treesize = 0; + minedge = -infinity; + maxedge = infinity; + for (int i = 0; i < nedges; i++) + { + process(edges[i]); + } + cout << ret << endl; + } + finish = clock(); + cerr << "Time: " << (double)(finish - start) / CLOCKS_PER_SEC << endl; + return 0; +} diff --git a/12276.cpp b/12276.cpp new file mode 100644 index 0000000..8007d8d --- /dev/null +++ b/12276.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +// O(length^3 * letters^2) DP + +const int infinity = 1000000000; +const int maxl = 'z' - 'a' + 1, maxn = 200; + +int mult[maxl][maxl], mult_cost[maxl][maxl]; +char letter[maxl], str[maxn + 1]; +int letter_pos[256]; +int best[maxn][maxn + 1][maxl]; + +int main() +{ + int k; + bool first = true; + while (scanf("%i", &k), k) + { + if (!first) + { + puts(""); + } + first = false; + for (int i = 0; i < k; ++i) + { + scanf(" %c", &letter[i]); + letter_pos[letter[i]] = i; + } + for (int i = 0; i < k; ++i) + for (int j = 0; j < k; ++j) + { + char l; + scanf(" %i-%c", &mult_cost[i][j], &l); + mult[i][j] = letter_pos[l]; + } + int lines; + scanf("%i", &lines); + while (lines--) + { + scanf("%s", str); + int n = strlen(str); + for (int i = 0; i < n; ++i) + { + str[i] = letter_pos[str[i]]; + } + fill(&best[0][0][0], &best[n][0][0], infinity); + for (int i = n - 1; i >= 0; --i) + { + best[i][i + 1][str[i]] = 0; + for (int j = i + 2; j <= n; ++j) + for (int m = i + 1; m < j; ++m) + for (int l1 = 0; l1 < k; ++l1) + for (int l2 = 0; l2 < k; ++l2) + { + int prod = mult[l1][l2], + cost = best[i][m][l1] + best[m][j][l2] + mult_cost[l1][l2]; + if (cost < best[i][j][prod]) + { + best[i][j][prod] = cost; + } + } + } + int l = min_element(&best[0][n][0], &best[0][n][k]) - &best[0][n][0]; + printf("%i-%c\n", best[0][n][l], letter[l]); + } + } + return 0; +} diff --git a/12278.cpp b/12278.cpp new file mode 100644 index 0000000..29b403c --- /dev/null +++ b/12278.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +// O(1) + +typedef long long ll; + +struct point +{ + ll x, y; + point(ll a = 0, ll b = 0) : x(a), y(b) {} +}; + +// Returns twice the signed area of triangle abc. +// Its sign is positive if c is to the left of ab. +ll area2(point a, point b, point c) +{ + return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); +} + +int main() +{ + point p[4]; + for (;;) + { + for (int i = 0; i < 4; ++i) + { + ll z; + scanf("%lli%lli%lli", &p[i].x, &p[i].y, &z); + if (p[i].x + p[i].y + z == 0) + { + return 0; + } + } + ll triang = area2(p[0], p[1], p[2]); + if (triang < 0) + { + swap(p[0], p[1]); + } + bool inside; + if (triang == 0)// degenerate triangle + { + ll a = min(p[0].x, min(p[1].x, p[2].x)), + b = max(p[0].x, max(p[1].x, p[2].x)), + c = min(p[0].y, min(p[1].y, p[2].y)), + d = max(p[0].y, max(p[1].y, p[2].y)); + inside = ((a < p[3].x && p[3].x < b) || (c < p[3].y && p[3].y < d)) && (area2(p[0], p[1], p[3]) == 0); + } + else + inside = + (area2(p[0], p[1], p[3]) > 0) && + (area2(p[1], p[2], p[3]) > 0) && + (area2(p[2], p[0], p[3]) > 0); + puts(inside ? "YES" : "NO"); + } + return 0; +} diff --git a/12279.cpp b/12279.cpp index 362c446..17189c6 100644 --- a/12279.cpp +++ b/12279.cpp @@ -1,20 +1,18 @@ -#include +#include -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++) { - int N; - scanf("%d", &N); - if (!N) break; +using namespace std; - int A = 0, B = 0, I; - while (N--) { - scanf("%d", &I); - if (I == 0) ++B; - else ++A; - } - printf("Case %d: %d\n", t, A - B); - } - return 0; +int main() +{ + for (int c = 0, n; scanf("%d", &n) == 1 && n;) + { + int cnt = 0; + for (int i = 0, z; i < n; ++i) + { + scanf("%d", &z); + cnt += !z; + } + printf("Case %d: %d\n", ++c, n - (cnt << 1)); + } + return 0; } diff --git a/1228.cpp b/1228.cpp new file mode 100644 index 0000000..29f4d92 --- /dev/null +++ b/1228.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define inf 0x3f3f3f3f +#define Inf 0x3FFFFFFFFFFFFFFFLL +#define eps 1e-8 +#define pi acos(-1.0) + +typedef unsigned long long ll; +const int maxn = 100 + 10; +int num[maxn], n, d, one, zero; +int ot[maxn], zt[maxn]; +ll dp[maxn][maxn]; +pair v[maxn]; +bool check(int x, int y, int b) +{ + if (b) + { + return zt[x + 1] + d >= ot[y + 1]; + } + return ot[y + 1] + d >= zt[x + 1]; +} +void cal(ll &minv, ll &maxv) +{ + for (int i = 0; i < n; ++i) + { + if (num[i] == 0) + { + int t = i; + v[i] = make_pair(t, 0); + } + else + { + int t = max(i, i + d); + v[i] = make_pair(t, 1); + } + } + sort(v, v + n); + minv = 0; + for (int i = 0; i < n; ++i) + { + if (v[i].second) + { + minv |= 1ULL << (n - i - 1ULL); + } + } + for (int i = 0; i < n; ++i) + { + if (num[i] == 1) + { + int t = i; + v[i] = make_pair(t, -1); + } + else + { + int t = max(i, i + d); + v[i] = make_pair(t, 0); + } + } + sort(v, v + n); + maxv = 0; + for (int i = 0; i < n; ++i) + { + if (v[i].second) + { + maxv |= 1ULL << (n - i - 1ULL); + } + } +} +int main() +{ + // freopen("in.txt","r",stdin); + // freopen("out.txt","w",stdout); + ll m; + int tcase = 0; + while (cin >> n) + { + if (n == 0) + { + break; + } + cin >> d >> m; + // scanf("%d%lld",&d,&m); + // cout<= 0; --i) + { + num[n - i - 1] = (m >> i) & 1; + } + one = 0, zero = 0; + ot[0] = zt[0] = 0; + for (int i = 0; i < n; ++i) + { + if (num[i]) + { + ot[++one] = i; + } + else + { + zt[++zero] = i; + } + } + ot[one + 1] = zt[zero + 1] = inf; + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; + for (int i = 0; i <= n - one; ++i) + for (int j = 0; j <= one; ++j) + { + if (dp[i][j] == 0) + { + continue; + } + if (check(i, j, 0)) + { + dp[i + 1][j] += dp[i][j]; + } + if (check(i, j, 1)) + { + dp[i][j + 1] += dp[i][j]; + } + } + ll minv, maxv; + cal(minv, maxv); + cout << "Case " << ++tcase << ": "; + cout << dp[n - one][one] << " " << minv << " " << maxv << endl; + // printf("Case %d: %lld %lld %lld\n",++tcase,,minv,maxv); + } + return 0; +} diff --git a/12280.cpp b/12280.cpp new file mode 100644 index 0000000..974e066 --- /dev/null +++ b/12280.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define eps 1e-8 + +int n, lnum = 0, rnum = 0; +char in[8][100], ans[8][100], ll[8][100], rr[8][100]; + +int ca(int x) +{ + int cnt0 = 0, cnt1 = 0; + while (x) + { + int temp = x % 2; + if (temp == 1) + cnt1++; + else + cnt0++; + x /= 2; + } + return cnt1 * 500 + cnt0 * 250; +} + +int calc(int type, int l) +{ + int i; + int ans = 0; + if (type == 1) + { + int t = 0; + for (i = 1; i <= 6; i++) + if (in[l][i] != '.') + ans += ca(in[l][i]); + } + else if (type == 2) + { + int t = 0; + for (i = 11; i <= 16; i++) + if (in[l][i] != '.') + ans += ca(in[l][i]); + } + return ans; +} + +void make(int ln, int rn) +{ + int i, j; + for (i = 0; i < 8; i++) + { + if (ln >= 0 && ln <= 4) + for (j = 0; j <= 7; j++) + ans[i][j] = ll[ln][j]; + else + for (j = 0; j <= 7; j++) + ans[i][j] = '.'; + for (j = 8; j <= 9; j++) + ans[i][j] = '|'; + if (rn >= 0 && rn <= 4) + for (j = 10; j <= 17; j++) + ans[i][j] = rr[rn][j - 10]; + else + for (j = 10; j <= 17; j++) + ans[i][j] = '.'; + ln++; + rn++; + } +} +void output() +{ + int i, j; + for (i = 0; i < 7; i++) + { + for (j = 0; j < 18; j++) + printf("%c", ans[i][j]); + printf("\n"); + } +} + +int main() +{ + int i, j, T; + scanf("%d", &T); + gets(in[0]); + int ttt = 0; + while (T--) + { + ttt++; + int lh, rh; + lnum = 0; + rnum = 0; + for (i = 0; i <= 7; i++) + { + gets(in[i]); + for (j = 1; j <= 6; j++) + if (in[i][j] != '_') + break; + if (j == 7) + lh = i; + for (j = 11; j <= 16; j++) + if (in[i][j] != '_') + break; + if (j == 17) + rh = i; + for (j = 0; j < 18; j++) + { + if (in[i][j] >= 'A' && in[i][j] <= 'Z') + { + int temp = ca(in[i][j]); + if (j < 8) + lnum += temp; + else + rnum += temp; + } + } + } + printf("Case %d:\n", ttt); + if ((rnum > lnum) && (rh > lh) || (rnum < lnum) && (rh < lh) || (rnum == lnum) && (rh == lh)) + printf("The figure is correct.\n"); + else + { + int t = 0; + for (i = lh - 4; i <= lh; i++) + { + for (j = 0; j <= 7; j++) + ll[t][j] = in[i][j]; + t++; + } + t = 0; + for (i = rh - 4; i <= rh; i++) + { + int k = 0; + for (j = 10; j <= 17; j++) + { + rr[t][k] = in[i][j]; + k++; + } + t++; + } + if (lnum > rnum) + { + make(-2, 0); + output(); + } + else if (lnum == rnum) + { + make(-1, -1); + output(); + } + else + { + make(0, -2); + output(); + } + } + } + return 0; +} diff --git a/12281.cpp b/12281.cpp new file mode 100644 index 0000000..49a7bf0 --- /dev/null +++ b/12281.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define INT_MAX 2147483647 +#define INT_MIN -2147483647 +#define pi acos(-1.0) +#define N 1000000 +#define LL unsigned long long + +int ar[50], the_index; + +void fibo() +{ + LL a = 0, b = 1, c = 1; + the_index = 0; + while (c <= 2000000000) + { + ar[the_index++] = c; + a = b; + b = c; + c = a + b; + } +} + +int numsOfFibo(int n) +{ + int ret = 0; + for (int i = the_index - 1; i >= 0; i--) + { + if (n == 0) + { + return ret; + } + else if (ar[i] <= n) + { + n -= ar[i]; + ret++; + } + } + return ret; +} + +int main() +{ + fibo(); + int testCase; + scanf("%d", &testCase); + int cases = 0; + while (testCase--) + { + int d; + scanf("%d", &d); + int input; + LL cnt = 1; + for (int i = 0; i < d; i++) + { + scanf("%d", &input); + cnt *= numsOfFibo(input); + } + cout << "Case " << ++cases << ": " << cnt << endl; + } + return 0; +} diff --git a/12282.cpp b/12282.cpp new file mode 100644 index 0000000..67da13e --- /dev/null +++ b/12282.cpp @@ -0,0 +1,559 @@ +#include + +using namespace std; + +#define F(i, a, b) for (int i = a; i < b; i++) +#define NL printf("\n"); +#define cp printf("here\n"); +#define MEM(a, val) memset(a, val, sizeof(a)) +#define CHR getchar() +#define PB(x) push_back(x) +#define sz() size() + +#define MX 100007 +#define MOD 1000000007 +#define INF (1 << 30) - 1 + (1 << 30) +#define eps 1e-9 + +map mp; +int block[11][11], came[9][9][9][9][9][9][9][9]; +char s[12][12]; + +struct z +{ + int axx, ayy, bxx, byy, cxx, cyy, dxx, dyy; +}; + +int check_area(int x, int y, int dir) +{ + if (dir == 0) + { + if (y < 7) + { + if (block[x][y + 2] == 0 && block[x + 1][y + 2] == 0) + { + return 1; + } + } + return 0; + } + else if (dir == 1) + { + if (y > 1) + { + if (block[x][y - 1] == 0 && block[x + 1][y - 1] == 0) + { + return 1; + } + } + return 0; + } + else if (dir == 2) + { + if (x > 1) + { + if (block[x - 1][y] == 0 && block[x - 1][y + 1] == 0) + { + return 1; + } + } + return 0; + } + else + { + if (x < 7) + { + if (block[x + 2][y] == 0 && block[x + 2][y + 1] == 0) + { + return 1; + } + } + return 0; + } +} + +void change(int x, int y) +{ + block[x][y] = 1; + block[x][y + 1] = 1; + block[x + 1][y + 1] = 1; + block[x + 1][y] = 1; + return; +} + +z BFS_DIRECTION(int dir, z a) +{ + int ax, ay, bx, by, cx, cy, dx, dy; + ax = a.axx; + ay = a.ayy; + bx = a.bxx; + by = a.byy; + cx = a.cxx; + cy = a.cyy; + dx = a.dxx; + dy = a.dyy; + if (dir == 1) + { + /// east dir + int vis[9][9][5]; + MEM(vis, 0); + queue q; + q.push(0); + q.push(ax); + q.push(ay); + vis[ax][ay][0] = 1; + q.push(1); + q.push(bx); + q.push(by); + vis[bx][by][1] = 1; + q.push(2); + q.push(cx); + q.push(cy); + vis[cx][cy][2] = 1; + q.push(3); + q.push(dx); + q.push(dy); + vis[dx][dy][3] = 1; + while (q.size()) + { + int x, y, id; + id = q.front(); + q.pop(); + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + vis[x][y][id]++; + if (check_area(x, y, 0)) + { + if (id == 0) + { + q.push(0); + ay = ay + 1; + q.push(ax); + q.push(ay); + } + else if (id == 1) + { + q.push(1); + by = by + 1; + q.push(bx); + q.push(by); + } + else if (id == 2) + { + q.push(2); + cy = cy + 1; + q.push(cx); + q.push(cy); + } + else + { + q.push(3); + dy = dy + 1; + q.push(dx); + q.push(dy); + } + block[x + 1][y] = 0; + block[x][y] = 0; + change(x, y + 1); + } + else if (vis[x][y][id] <= 5) + { + q.push(id); + q.push(x); + q.push(y); + } + } + } + else if (dir == 2) + { + /// west dir + int vis[9][9][5]; + MEM(vis, 0); + queue q; + q.push(0); + q.push(ax); + q.push(ay); + vis[ax][ay][0] = 1; + q.push(1); + q.push(bx); + q.push(by); + vis[bx][by][1] = 1; + q.push(2); + q.push(cx); + q.push(cy); + vis[cx][cy][2] = 1; + q.push(3); + q.push(dx); + q.push(dy); + vis[dx][dy][3] = 1; + while (q.size()) + { + int x, y, id; + id = q.front(); + q.pop(); + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + vis[x][y][id]++; + if (check_area(x, y, 1)) + { + // cout< q; + q.push(0); + q.push(ax); + q.push(ay); + vis[ax][ay][0] = 1; + q.push(1); + q.push(bx); + q.push(by); + vis[bx][by][1] = 1; + q.push(2); + q.push(cx); + q.push(cy); + vis[cx][cy][2] = 1; + q.push(3); + q.push(dx); + q.push(dy); + vis[dx][dy][3] = 1; + while (q.size()) + { + int x, y, id; + id = q.front(); + q.pop(); + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + vis[x][y][id]++; + if (check_area(x, y, 2)) + { + if (id == 0) + { + ax = ax - 1; + q.push(0); + q.push(ax); + q.push(ay); + } + else if (id == 1) + { + bx = bx - 1; + q.push(1); + q.push(bx); + q.push(by); + } + else if (id == 2) + { + cx = cx - 1; + q.push(2); + q.push(cx); + q.push(cy); + } + else + { + dx = dx - 1; + q.push(3); + q.push(dx); + q.push(dy); + } + block[x + 1][y] = 0; + block[x + 1][y + 1] = 0; + change(x - 1, y); + } + else if (vis[x][y][id] <= 5) + { + q.push(id); + q.push(x); + q.push(y); + } + } + } + else + { + /// south dir + int vis[9][9][5]; + MEM(vis, 0); + queue q; + q.push(0); + q.push(ax); + q.push(ay); + vis[ax][ay][0] = 1; + q.push(1); + q.push(bx); + q.push(by); + vis[bx][by][1] = 1; + q.push(2); + q.push(cx); + q.push(cy); + vis[cx][cy][2] = 1; + q.push(3); + q.push(dx); + q.push(dy); + vis[dx][dy][3] = 1; + while (q.size()) + { + int x, y, id; + id = q.front(); + q.pop(); + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + vis[x][y][id]++; + //cout<=2) return; + if (came[ax][ay][bx][by][cx][cy][dx][dy] == tst) + { + return; + } + came[ax][ay][bx][by][cx][cy][dx][dy] = tst; + ans++; + F(i, 1, 5) + { + int nw[10][10]; + F(j, 0, 10) + { + F(k, 0, 10) + { + nw[j][k] = block[j][k]; + } + } + DFS(BFS_DIRECTION(i, a)); + F(j, 0, 10) + { + F(k, 0, 10) + { + block[j][k] = nw[j][k]; + } + } + } + return; +} + +int main() +{ + int ax, ay, bx, by, cx, cy, dx, dy; + int t; + cin >> t; + tst = 1; + F(cs, 1, t + 1) + { + mp.clear(); + F(i, 0, 10) + { + block[i][0] = 1; + } + F(i, 0, 10) + { + block[9][i] = 1; + } + F(i, 0, 10) + { + block[i][9] = 1; + } + F(i, 0, 9) + { + block[0][i] = 1; + } + F(i, 1, 9) + { + cin >> s[i]; + F(j, 0, 8) + { + if (s[i][j] == '#') + { + block[i][1 + j] = 1; + } + else if (s[i][j] == '.') + { + block[i][j + 1] = 0; + } + else + { + char ch = s[i][j]; + if (mp[ch] == 0) + { + int x, y; + x = i, y = j + 1; + mp[ch] = 1; + if (ch == 'A') + { + ax = x; + ay = y; + } + else if (ch == 'B') + { + bx = x; + by = y; + } + else if (ch == 'C') + { + cx = x; + cy = y; + } + else + { + dx = x; + dy = y; + } + } + } + } + } + z tp; + tp.axx = ax; + tp.ayy = ay; + tp.bxx = bx; + tp.byy = by; + tp.cxx = cx; + tp.cyy = cy; + tp.dxx = dx; + tp.dyy = dy; + change(ax, ay); + change(bx, by); + change(cx, cy); + change(dx, dy); + ans = 0; + F(i, 1, 5) + { + int nw[10][10]; + F(j, 0, 10) + { + F(k, 0, 10) + { + nw[j][k] = block[j][k]; + } + } + DFS(BFS_DIRECTION(i, tp)); + F(j, 0, 10) + { + F(k, 0, 10) + { + block[j][k] = nw[j][k]; + } + } + } + printf("Case %d: ", cs); + cout << ans << endl; + tst++; + } + return 0; +} diff --git a/12283.cpp b/12283.cpp new file mode 100644 index 0000000..c2830b0 --- /dev/null +++ b/12283.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int dp[103][103], a[103]; + +void dfs(int i, int j) +{ + if (dp[i][j] != -1) + return; + if (i > j) + { + dp[i][j] = 0; + return; + } + if (i == j) + { + dp[i][j] = 1; + return; + } + if (j == i + 1) + { + dp[i][j] = (a[i] == a[j] ? 1 : 2); + return; + } + int k; + dfs(i, j - 1); + dp[i][j] = dp[i][j - 1] + 1; + for (k = i; k < j; k++) + { + if (a[k] == a[j]) + { + dfs(i, k), dfs(k + 1, j - 1); + if (dp[i][k] + dp[k + 1][j - 1] < dp[i][j]) + dp[i][j] = dp[i][k] + dp[k + 1][j - 1]; + } + } +} + +int main() +{ + int t, cs, n, m, num, i, j; + scanf("%d", &t); + for (cs = 1; cs <= t; cs++) + { + scanf("%d%d", &n, &m); + for (i = 1; i <= n; i++) + scanf("%d", &a[i]); + for (num = 1, i = 2; i <= n; i++) + if (a[i] != a[i - 1]) + a[++num] = a[i]; + n = num; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + dp[i][j] = -1; + dfs(1, n); + printf("Case %d: %d\n", cs, dp[1][n]); + } + return 0; +} diff --git a/12284.cpp b/12284.cpp new file mode 100644 index 0000000..3aecf6a --- /dev/null +++ b/12284.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define INF 0x3fffffff + +int start[110][110], end_[110][110]; + +int main() +{ + int i, sum, j, ca, p = 0, n, k, sum1, a, b; + bool flag; + scanf("%d", &ca); + while (ca--) + { + scanf("%d%d", &n, &k); + flag = false; + memset(start, -1, sizeof(start)); + memset(end_, -1, sizeof(end_)); + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + { + scanf("%d", &start[i][j]); + if (start[i][j] != start[j][i] && start[j][i] != -1) + { + flag = true; + } + } + flag = false; + bool yy = false; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + { + scanf("%d", &end_[i][j]); + if (end_[i][j] != end_[j][i] && end_[j][i] != -1) + { + flag = true; + } + if (start[i][j] != end_[i][j]) + { + yy = true; + } + } + if (!flag && yy) + { + printf("Case %d: -1\n", ++p); + continue; + } + sum = 0; + sum1 = 0; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + { + if (start[i][j] != end_[i][j]) + { + sum++; + if (i != j) + { + sum1++; + a = i; + b = j; + } + } + } + if (sum1 == 0) + { + printf("Case %d: %d\n", ++p, sum); + continue; + } + if (k == 2) + { + if (sum1 != 0 && n == 2) + { + printf("Case %d: -1\n", ++p); + continue; + } + } + flag = false; + for (i = 1; i <= n; i++) + for (j = 1; j <= n; j++) + { + if (start[i][j] != end_[i][j] && i != j) + { + if (start[j][i] != end_[i][j]) + { + flag = true; + } + } + else if (start[i][j] != start[j][i]) + { + flag = true; + } + } + if (!flag && sum1 > 1 && n > 2 && k == 2) + { + printf("Case %d: %d\n", ++p, sum + 2); + } + else if (flag || sum1 == 1 || sum1 > 3) + { + printf("Case %d: %d\n", ++p, sum); + } + else + { + printf("Case %d: %d\n", ++p, sum + 1); + } + } + return 0; +} diff --git a/12285.cpp b/12285.cpp new file mode 100644 index 0000000..c614804 --- /dev/null +++ b/12285.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define MAXN 20000000 + +long double sum[MAXN]; +long double H(long long x) +{ + if (x < MAXN) + { + return sum[x]; + } + return 0.57721566490153286l + logl((long double)x); +} + +int main() +{ + for (int i = 1; i < MAXN; i++) + { + sum[i] = sum[i - 1] + 1.0l / (long double)i; + } + long long W, L; + int cases = 0; + while (scanf("%lld %lld", &W, &L) == 2) + { + if (W == 0 && L == 0) + { + return 0; + } + printf("Case %d:\n", ++cases); + long double ret = 0; + if (L == 0 && W % 8) + { + ret = -1; + } + else if (L == 0 && W % 8 == 0) + { + ret = 8; + } + else + { + long long round = ceil((W - L * 7) / 8.0), n = W / 8; + if (round < 0) + { + round = 0; + } + ret = (H(n + L) - H(L + round - 1)); + ret *= (W + L); + ret /= (n - round + 1); + assert(ret > -0.1); + } + if (ret > -0.1) + { + printf("On Average Bob Reaches Round %.2lf\n", (double)ret); + } + else + { + printf("Situation Impossible.\n"); + } + } + return 0; +} diff --git a/12286.cpp b/12286.cpp new file mode 100644 index 0000000..b827d5f --- /dev/null +++ b/12286.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +typedef long double LD; + +const LD eps = 1e-18; + +struct Point +{ + double x, y; + Point(LD _x = 0, LD _y = 0) : x(_x), y(_y) {} + + Point operator+(const Point &rhs) const + { + return Point(x + rhs.x, y + rhs.y); + } + Point operator-(const Point &rhs) const + { + return Point(x - rhs.x, y - rhs.y); + } + Point operator*(LD d) const + { + return Point(x * d, y * d); + } + Point operator/(LD d) const + { + return Point(x / d, y / d); + } +}; + +Point d[4] = {Point(0, 1), Point(0, -1), Point(1, 0), Point(-1, 0)}; +Point p[3]; + +int dcmp(LD x) +{ + return x < -eps ? -1 : x > eps; +} + +LD sqrt_(LD x) +{ + if (!dcmp(x)) + { + return 0; + } + return sqrt(x); +} + +LD abs(Point p) +{ + return sqrt_(p.x * p.x + p.y * p.y); +} + +LD dis(Point q) +{ + return abs(q - p[0]) + abs(q - p[1]) * 2 + abs(q - p[2]) * 2; +} + +LD calc() +{ + Point now = (p[0] + p[1] + p[2]) / 3.0; + for (LD step = 1e4; dcmp(step) > 0; step /= 2) + { + bool flag = true; + while (flag) + { + flag = false; + for (int i = 0; i < 4; i++) + { + Point cnt = now + d[i] * step; + if (dcmp(dis(cnt) - dis(now)) < 0) + { + now = cnt; + flag = true; + break; + } + } + } + } + return dis(now); +} + +int main() +{ + int t = 0; + while (true) + { + int x1, y1, x2, y2, x3, y3; + scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3); + if (x1 == -1) + { + break; + } + p[0] = Point((LD)x1, (LD)y1); + p[1] = Point((LD)x2, (LD)y2); + p[2] = Point((LD)x3, (LD)y3); + LD ans = 1e20; + for (int i = 0; i < 3; i++) + { + swap(p[0], p[i]); + ans = min(ans, calc()); + } + printf("Case %d: %.8lf\n", ++t, (double)ans); + } + return 0; +} diff --git a/12289.cpp b/12289.cpp index 2b5c46d..85eff40 100644 --- a/12289.cpp +++ b/12289.cpp @@ -1,24 +1,36 @@ -#include -#include +#include -int main() { - char S[3][20] = {"one", "two", "three"}; - int Len[3] = {3, 3, 5}; +using namespace std; + +string words[3] = {"one", "two", "three"}; + +int word_diff(string s1, string s2) +{ + int diff = max(s1.size(), s2.size()) - min(s1.size(), s2.size()); + for (int i = 0; i < min(s1.size(), s2.size()); ++i) + { + if (s1[i] != s2[i]) + { + diff++; + } + } + return diff; +} + +int main() +{ int T; scanf("%d", &T); - while (T--) { - char str[20]; - scanf("%s", str); - int L = strlen(str); - for (int i = 0; i < 3; i++) { - if (L == Len[i]) { - int C = 0; - for (int j = 0; j < Len[i]; j++) - C += (str[j] != S[i][j]); - if (C <= 1) { - printf("%d\n", i + 1); - break; - } + while (T--) + { + string word; + cin >> word; + for (int i = 0; i < 3; ++i) + { + if (word_diff(word, words[i]) <= 1) + { + printf("%d\n", i + 1); + break; } } } diff --git a/1229.cpp b/1229.cpp new file mode 100644 index 0000000..0f71a34 --- /dev/null +++ b/1229.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +//1229 +//Sub-Dictionary +//Graphs;Strongly Connected Components +#define MAX 101 + +map P; +int word(const string &p) +{ + if (P.find(p) != P.end()) + { + return P[p]; + } + else + { + return P[p] = P.size(); + } +} + +int O[MAX], npv, CO[MAX], GR[MAX]; +string W[MAX]; +bool G[MAX][MAX], V[MAX]; +int n; +set words; +set answer; + +void DFS(int v) +{ + V[v] = true; + for (int i = 1; i <= n; i++) + if (G[v][i] && !V[i]) + { + DFS(i); + } + O[npv++] = v; +} + +int DFSt(int v, int comp) +{ + int acum = 1; + V[v] = true; + CO[v] = comp; + for (int i = 1; i <= n; i++) + if (G[i][v] && !V[i]) + { + acum += DFSt(i, comp); + } + return acum; +} + +void DFSf(int v) +{ + V[v] = true; + answer.insert(W[v]); + for (int i = 1; i <= n; i++) + if (G[v][i] && !V[i]) + { + DFSf(i); + } +} + +int main() +{ + string s, p, q; + while (cin >> n, n) + { + memset(G, 0, sizeof(G)); + memset(CO, 0, sizeof(CO)); + memset(GR, 0, sizeof(GR)); + P.clear(); + words.clear(); + answer.clear(); + getline(cin, p); + for (int i = 0; i < n; i++) + { + getline(cin, s); + stringstream sin(s); + sin >> p; + while (sin >> q) + { + G[word(p)][word(q)] = true; + GR[word(p)]++; + } + W[word(p)] = p; + } + npv = 1; + memset(V, 0, sizeof(V)); + memset(O, 0, sizeof(O)); + for (int i = 1; i <= n; i++) + if (!V[i]) + { + DFS(i); + } + memset(V, 0, sizeof(V)); + int comp = 0; + for (int i = n; i > 0; i--) + { + if (!V[O[i]]) + { + comp++; + if (DFSt(O[i], comp) > 1 || GR[O[i]] == 0) + { + for (int j = 1; j <= n; j++) + { + if (CO[j] == comp) + { + words.insert(j); + } + } + } + } + } + memset(V, 0, sizeof(V)); + for (set::iterator it = words.begin(); it != words.end(); it++) + { + DFSf(*it); + } + cout << answer.size() << endl; + for (set::iterator it = answer.begin(); it != answer.end(); it++) + { + cout << (it != answer.begin() ? " " : "") << *it; + } + cout << endl; + } + return 0; +} diff --git a/12290.cpp b/12290.cpp new file mode 100644 index 0000000..e37616b --- /dev/null +++ b/12290.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +#define N 100000 +#define LL long long + +int main() +{ + char a[10]; + set s; + for (int i = 7; i < N; i++) + { + sprintf(a, "%d", i); + if (strchr(a, '7')) + s.insert(i); + if (i % 7 == 0) + s.insert(i); + } + + int n, m, k; + while (scanf("%d %d %d", &n, &m, &k)) + { + if (n == 0 && m == 0 && k == 0) + break; + int person = 1, action = 1; + bool increase = true; + set::iterator it = s.begin(); + while (true) + { + if (*it == action) + { + it++; + if (person == m) + k--; + if (!k) + { + printf("%d\n", action); + break; + } + } + action++; + if (person == n) + increase = false; + else if (person == 1) + increase = true; + + if (increase) + person++; + else + person--; + } + } + return 0; +} diff --git a/12291.cpp b/12291.cpp new file mode 100644 index 0000000..7400f35 --- /dev/null +++ b/12291.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +#define LL unsigned long long + +char largePic[10 + 2][10 + 2], smallPic[10 + 2][10 + 2]; +int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1}; +bool vis[10 + 2][10 + 2], possible; +int n, m; + +void dfs(int rL, int cL, int rS, int cS) +{ + if (rS == m || rS < 0 || cS == m || cS < 0 || vis[rS][cS] || smallPic[rS][cS] != '*') + { + return; + } + vis[rS][cS] = true; + if (rL < n && rL >= 0 && cL < n && cL >= 0) + { + if (largePic[rL][cL] != '*') + { + possible = false; + return; + } + largePic[rL][cL] = '.'; + } + else + { + possible = false; + return; + } + for (int i = 0; i < 4; i++) + { + dfs(rL - dr[i], cL - dc[i], rS - dr[i], cS - dc[i]); + } +} + +void getPosition() +{ + int startRowLargePic = -1; + int startColLargePic = -1; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (largePic[i][j] == '*') + { + startRowLargePic = i; + startColLargePic = j; + i = j = n; + } + } + } + int startRowSmallPic; + int startColSmallPic; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < m; j++) + { + if (smallPic[i][j] == '*') + { + startRowSmallPic = i; + startColSmallPic = j; + i = j = m; + } + } + } + memset(vis, false, sizeof vis); + if (startRowLargePic == -1) + { + possible = false; + } + else + { + dfs(startRowLargePic, startColLargePic, startRowSmallPic, startColSmallPic); + } +} + +void print() +{ + printf("Large pic: \n"); + for (int i = 0; i < n; i++) + printf("%s\n", largePic[i]); + + printf("Small pic: \n"); + for (int i = 0; i < m; i++) + printf("%s\n", smallPic[i]); +} + +int main() +{ + while (scanf("%d %d", &n, &m)) + { + if (n == 0 && m == 0) + break; + + for (int i = 0; i < n; i++) + scanf("%s", largePic[i]); + for (int i = 0; i < m; i++) + scanf("%s", smallPic[i]); + + possible = true; + getPosition(); + if (possible) + getPosition(); + + if (possible) + printf("1\n"); + else + printf("0\n"); + } + return 0; +} diff --git a/12292.cpp b/12292.cpp new file mode 100644 index 0000000..7812304 --- /dev/null +++ b/12292.cpp @@ -0,0 +1,234 @@ +#include + +using namespace std; + +int N, M, op[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, ans, mini, num[12][12]; +char s[12][12], final[12][12], ss[12][12]; +vector> v, z; + +bool check_small() +{ + int i, j, vis[25], all = z.size(), nx, ny, x, y; + memset(vis, 0, sizeof(vis)); + queue q; + vis[num[z[0].first][z[0].second]] = 1; + ss[z[0].first][z[0].second] = '*'; + q.push(z[0].first); + q.push(z[0].second); + while (!q.empty()) + { + x = q.front(); + q.pop(); + y = q.front(); + q.pop(); + for (i = 0; i < 4; i++) + { + nx = x + op[i][0]; + ny = y + op[i][1]; + if (nx >= 0 && nx < N && ny >= 0 && ny < N && num[nx][ny] != -1 && ss[nx][ny] == '!' && vis[num[nx][ny]] == 0) + { + vis[num[nx][ny]] = 1; + ss[nx][ny] = '*'; + q.push(nx); + q.push(ny); + } + } + } + for (i = j = 0; i < all; i++) + if (ss[z[i].first][z[i].second] == '!') + { + j = 1; + } + if (j == 0) + { + return true; + } + return false; +} +void dfs(int lv) +{ + int i, j, nx, ny, k; + for (i = k = 0; i < N; i++) + for (j = 0; j < N; j++) + if (s[i][j] == '*') + { + k = 1; + } + if (k == 0) + { + ans = 1; + if (lv < mini) + { + for (i = 0; i < N; i++) + { + strcpy(final[i], s[i]); + } + mini = lv; + } + else if (lv == mini) + { + int which = 0; + for (i = 0; i < N && which == 0; i++) + for (j = 0; j < N && which == 0; j++) + if (s[i][j] != final[i][j]) + { + which = s[i][j] < final[i][j] ? 1 : 2; + } + if (which == 1) + for (i = 0; i < N; i++) + { + strcpy(final[i], s[i]); + } + } + } + for (i = 0; i < v.size() && ans == 0; i++) + { + if (s[v[i].first][v[i].second] != '*') + { + continue; + } + int re = 0, dx = z[0].first, dy = z[0].second; + for (k = 1; k < z.size() && re == 0; k++) + { + nx = z[k].first - dx + v[i].first; + ny = z[k].second - dy + v[i].second; + if (nx < 0 || ny < 0 || nx >= N || ny >= N || s[nx][ny] != '*') + { + re = 1; + } + } + if (re == 0) + { + s[v[i].first][v[i].second] = lv + 'A'; + for (k = 1; k < z.size() && re == 0; k++) + { + nx = z[k].first - dx + v[i].first; + ny = z[k].second - dy + v[i].second; + s[nx][ny] = lv + 'A'; + } + dfs(lv + 1); + s[v[i].first][v[i].second] = '*'; + for (k = 1; k < z.size() && re == 0; k++) + { + nx = z[k].first - dx + v[i].first; + ny = z[k].second - dy + v[i].second; + s[nx][ny] = '*'; + } + } + return; + } +} + +int main() +{ + int n, m, i, j, k; + while (scanf(" %d", &N) == 1 && N) + { + gets(s[0]); + for (n = 0; n < N; n++) + { + gets(s[n]); + } + for (n = 0; n < N; n++) + { + strcpy(ss[n], s[n]); + } + v.clear(); + memset(num, -1, sizeof(num)); + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + if (s[i][j] == '*') + { + v.push_back(make_pair(i, j)); + num[i][j] = v.size() - 1; + } + M = v.size(); + if (M == 1 || M == 2 || M == 3 || M == 5 || M == 7 || M == 11 || M == 13 || M == 17 || M == 19) + { + k = 0; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + if (s[i][j] == '*') + { + printf("%c", k + 'A'); + k++; + } + else + { + printf("%c", s[i][j]); + } + printf("\n"); + } + printf("\n"); + continue; + } + mini = 1e9; + for (i = M / 2; i > 1; i--) + { + int per[M]; + memset(per, 0, sizeof(per)); + if (M % i != 0) + { + continue; + } + for (j = 0; j < i; j++) + { + per[j] = 1; + } + do + { + z.clear(); + for (j = 0; j < M; j++) + if (per[j]) + { + z.push_back(v[j]); + ss[v[j].first][v[j].second] = '!'; + } + if (check_small()) + { + ans = 0; + dfs(0); + } + for (j = 0; j < M; j++) + if (per[j]) + { + z.push_back(v[j]); + ss[v[j].first][v[j].second] = '*'; + } + } while (prev_permutation(per, per + M)); + if (mini != 1e9) + { + break; + } + } + if (mini != 1e9) + { + for (i = 0; i < N; i++) + { + printf("%s\n", final[i]); + } + printf("\n"); + } + else + { + k = 0; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + if (s[i][j] == '*') + { + printf("%c", k + 'A'); + k++; + } + else + { + printf("%c", s[i][j]); + } + printf("\n"); + } + printf("\n"); + } + } + return 0; +} diff --git a/12293.cpp b/12293.cpp new file mode 100644 index 0000000..7d551a1 --- /dev/null +++ b/12293.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main() +{ + for (int n; scanf("%d", &n) == 1 && n; puts(n & (n + 1) ? "Alice" : "Bob")) + ; + return 0; +} diff --git a/12294.cpp b/12294.cpp new file mode 100644 index 0000000..d4fec26 --- /dev/null +++ b/12294.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +struct +{ + int p1, p2, t1, t2, w1, w2; +} a[10000]; + +int n; +double ans; + +double getTime(int pos, int ablity) +{ + if (ablity < a[pos].p1) + { + return -1; + } + if (ablity >= a[pos].p2) + { + return a[pos].t2; + } + return (0.0 + ablity - a[pos].p1) / (0.0 + a[pos].p2 - a[pos].p1) * (a[pos].t2 - a[pos].t1) + a[pos].t1; +} + +void dfs(int pos, double time, int dbl, int ablity) +{ + if (pos == n) + { + if (time < ans) + { + ans = time; + } + return; + } + double addTime = getTime(pos, ablity); + if (addTime >= 0) + { + dfs(pos + 1, time + addTime, dbl + a[pos].w2, ablity + a[pos].w1); + } + if (ablity > 100) + { + return; + } + if (dbl > 0) + { + dfs(pos, time, dbl - 1, ablity * 2); + } +} + +int main() +{ + int ablity; + while (2 == scanf("%d%d", &n, &ablity) && (n || ablity)) + { + for (int i = 0; i < n; ++i) + { + scanf("%d%d%d%d%d%d", &a[i].p1, &a[i].p2, &a[i].t1, &a[i].t2, &a[i].w1, &a[i].w2); + } + ans = 1e300; + dfs(0, 0, 0, ablity); + if (ans >= 1e300) + { + puts("Impossible"); + } + else + { + printf("%.2f\n", ans); + } + } + return EXIT_SUCCESS; +} diff --git a/12295.cpp b/12295.cpp new file mode 100644 index 0000000..dcae8cc --- /dev/null +++ b/12295.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +#define MAXN 110 +#define MAXD 10010 +#define INF 1000000000 +#define MOD 1000000009 + +int N, M, a[MAXN][MAXN]; +long long int f[MAXD]; +int q[MAXD], inq[MAXD], d[MAXD], vis[MAXD], min_, tree[4 * MAXD]; +int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; + +int init() +{ + int i, j; + scanf("%d", &N); + if (!N) + { + return 0; + } + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + scanf("%d", &a[i][j]); + } + } + for (i = 0; i < N; i++) + { + for (j = 0; j < N - i - 1; j++) + { + a[i][j] += a[N - 1 - j][N - 1 - i]; + } + } + return 1; +} + +void SPFA() +{ + int i, j, k, z, newz, x, y, newx, newy, front, rear; + for (i = 0; i < MAXD; i++) + { + d[i] = INF; + } + d[1] = a[0][0]; + memset(inq, 0, sizeof(inq)); + front = rear = 0; + q[rear++] = 1; + while (front != rear) + { + z = q[front++]; + if (front > N * N) + { + front = 0; + } + inq[z] = 0; + x = (z - 1) / N; + y = (z - 1) % N; + if (x + y == N - 1) + { + continue; + } + for (i = 0; i < 4; i++) + { + newx = x + dx[i]; + newy = y + dy[i]; + if (newx >= 0 && newx < N && newy >= 0 && newy < N) + { + newz = newx * N + newy + 1; + if (d[z] + a[newx][newy] < d[newz]) + { + d[newz] = d[z] + a[newx][newy]; + if (!inq[newz]) + { + q[rear++] = newz; + if (rear > N * N) + { + rear = 0; + } + inq[newz] = 1; + } + } + } + } + } +} + +long long int DP(int cur) +{ + int i, x, y, newx, newy, newz; + if (f[cur] != -1) + { + return f[cur]; + } + x = (cur - 1) / N; + y = (cur - 1) % N; + if (x + y == N - 1) + { + f[cur] = d[cur] == min_ ? 1 : 0; + return f[cur]; + } + f[cur] = 0; + for (i = 0; i < 4; i++) + { + newx = x + dx[i]; + newy = y + dy[i]; + if (newx >= 0 && newx < N && newy >= 0 && newy < N) + { + newz = newx * N + newy + 1; + if (d[cur] + a[newx][newy] == d[newz]) + { + f[cur] += DP(newz); + } + } + } + return f[cur]; +} + +void solve() +{ + int i, j, k; + long long int ans; + SPFA(); + min_ = INF; + for (i = 0; i < N; i++) + { + if (d[i * N + N - i] < min_) + { + min_ = d[i * N + N - i]; + } + } + memset(f, -1, sizeof(f)); + ans = DP(1); + printf("%lld\n", ans); +} + +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/12296.cpp b/12296.cpp new file mode 100644 index 0000000..0f47d13 --- /dev/null +++ b/12296.cpp @@ -0,0 +1,513 @@ +#include + +using namespace std; + +const double EPS = 1e-8; + +template +T sqr(T x) +{ + return x * x; +} +struct Point +{ + double x, y; + Point() + { + } + Point(double x, double y) + : x(x), y(y) + { + } +}; +typedef Point Vec; +Vec operator+(Vec a, Vec b) +{ + return Vec(a.x + b.x, a.y + b.y); +} +Vec operator-(Vec a, Vec b) +{ + return Vec(a.x - b.x, a.y - b.y); +} +Vec operator*(Vec a, double p) +{ + return Vec(a.x * p, a.y * p); +} +Vec operator/(Vec a, double p) +{ + return Vec(a.x / p, a.y / p); +} +inline int sgn(double x) +{ + return (x > EPS) - (x < -EPS); +} +bool operator<(Point a, Point b) +{ + return sgn(a.x - b.x) < 0 || sgn(a.x - b.x) == 0 && a.y < b.y; +} +bool operator==(Point a, Point b) +{ + return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0; +} + +inline double dotDet(Vec a, Vec b) +{ + return a.x * b.x + a.y * b.y; +} +inline double crossDet(Vec a, Vec b) +{ + return a.x * b.y - a.y * b.x; +} +inline double dotDet(Point o, Point a, Point b) +{ + return dotDet(a - o, b - o); +} +inline double crossDet(Point o, Point a, Point b) +{ + return crossDet(a - o, b - o); +} +inline double vecLen(Vec x) +{ + return sqrt(dotDet(x, x)); +} +inline Vec vecUnit(Vec x) +{ + return x / vecLen(x); +} +inline Vec normal(Vec x) +{ + return Vec(-x.y, x.x) / vecLen(x); +} +inline bool onSeg(Point x, Point a, Point b) +{ + return sgn(crossDet(x, a, b)) == 0 && sgn(dotDet(x, a, b)) < 0; +} + +int segIntersect(Point a, Point c, Point b, Point d) +{ + Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d; + int a_bc = sgn(crossDet(v1, v2)); + int b_cd = sgn(crossDet(v2, v3)); + int c_da = sgn(crossDet(v3, v4)); + int d_ab = sgn(crossDet(v4, v1)); + if (a_bc * c_da > 0 && b_cd * d_ab > 0) + { + return 1; + } + if (onSeg(b, a, c) && c_da) + { + return 2; + } + if (onSeg(c, b, d) && d_ab) + { + return 2; + } + if (onSeg(d, c, a) && a_bc) + { + return 2; + } + if (onSeg(a, d, b) && b_cd) + { + return 2; + } + return 0; +} + +Point lineIntersect(Point P, Vec v, Point Q, Vec w) +{ + Vec u = P - Q; + double t = crossDet(w, u) / crossDet(v, w); + return P + v * t; +} +struct Polygon +{ + int n; + Point p[40]; + Polygon() + { + n = 0; + } + Polygon(int nn, Point *pp) + { + n = nn; + for (int q = 0; q < n; q++) + { + p[q] = pp[q]; + } + } + Point operator[](int a) const + { + return p[a]; + } +}; +struct Poly +{ + vector pt; + Poly() + { + pt.clear(); + } + ~Poly() + { + } + Poly(vector &pt) + : pt(pt) + { + } + Point operator[](int x) const + { + return pt[x]; + } + int size() + { + return pt.size(); + } + double area() + { + double ret = 0.0; + for (int i = 0, sz = pt.size(); i < sz; i++) + { + ret += crossDet(pt[i], pt[(i + 1) % sz]); + } + return fabs(ret / 2.0); + } +}; + +Poly cutPoly(Poly &poly, Point a, Point b) +{ + Poly ret = Poly(); + int n = poly.size(); + for (int i = 0; i < n; i++) + { + Point c = poly[i], d = poly[(i + 1) % n]; + if (sgn(crossDet(a, b, c)) >= 0) + { + ret.pt.push_back(c); + } + if (sgn(crossDet(b - a, c - d)) != 0) + { + Point ip = lineIntersect(a, b - a, c, d - c); + if (onSeg(ip, c, d)) + { + ret.pt.push_back(ip); + } + } + } + return ret; +} + +bool isIntersect(Point a, Point b, Poly &poly) +{ + for (int i = 0, sz = poly.size(); i < sz; i++) + { + if (segIntersect(a, b, poly[i], poly[(i + 1) % sz])) + { + return true; + } + } + return false; +} + +struct Circle +{ + Point c; + double r; + Circle() + { + } + Circle(Point c, double r) + : c(c), r(r) + { + } +}; + +inline bool inCircle(Point a, Circle c) +{ + return vecLen(c.c - a) < c.r; +} +bool lineCircleIntersect(Point s, Point t, Circle C, vector &sol) +{ + Vec dir = t - s, nor = normal(dir); + Point mid = lineIntersect(C.c, nor, s, dir); + double len = sqr(C.r) - dotDet(C.c - mid, C.c - mid); + if (sgn(len) < 0) + { + return 0; + } + if (sgn(len) == 0) + { + sol.push_back(mid); + return 1; + } + Vec dis = vecUnit(dir); + len = sqrt(len); + sol.push_back(mid + dis * len); + sol.push_back(mid - dis * len); + return 2; +} +double Cross(Point a, Point b) +{ + return a.x * b.y - a.y * b.x; +} +int dcmp(double x) +{ + if (fabs(x) < EPS) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} +double Length(Point a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} +double Dot(Point a, Point b) +{ + return a.x * b.x + a.y * b.y; +} +double DistanceToLine(Point p, Point a, Point b) +{ + Point v1 = b - a; + Point v2 = p - a; + return fabs(Cross(v1, v2)) / Length(v1); +} +double DistanceToSegment(Point p, Point a, Point b) +{ + if (a == b) + { + return Length(p - a); + } + Point v1 = b - a, v2 = p - a, v3 = p - b; + if (dcmp(Dot(v1, v2)) < 0) + { + return Length(v2); + } + else if (dcmp(Dot(v1, v3)) > 0) + { + return Length(v3); + } + else + { + return fabs(Cross(v1, v2)) / Length(v1); + } +} +bool OnSegment(Point p, Point a1, Point a2) +{ + return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; +} +bool segCircleIntersect(Point s, Point t, Circle C) +{ + vector tmp; + tmp.clear(); + if (lineCircleIntersect(s, t, C, tmp)) + { + if (tmp.size() < 2) + { + return false; + } + for (int i = 0, sz = tmp.size(); i < sz; i++) + { + if (onSeg(tmp[i], s, t)) + { + return true; + } + } + } + return false; +} +vector cutPolies(Point s, Point t, vector polies) +{ + vector ret; + ret.clear(); + for (int i = 0, sz = polies.size(); i < sz; i++) + { + Poly tmp; + tmp = cutPoly(polies[i], s, t); + if (tmp.size() >= 3 && tmp.area() > EPS) + { + ret.push_back(tmp); + } + tmp = cutPoly(polies[i], t, s); + if (tmp.size() >= 3 && tmp.area() > EPS) + { + ret.push_back(tmp); + } + } + return ret; +} + +int ptInPoly(Point p, Poly &poly) +{ + int wn = 0, sz = poly.size(); + for (int i = 0; i < sz; i++) + { + if (onSeg(p, poly[i], poly[(i + 1) % sz])) + { + return -1; + } + int k = sgn(crossDet(poly[(i + 1) % sz] - poly[i], p - poly[i])); + int d1 = sgn(poly[i].y - p.y); + int d2 = sgn(poly[(i + 1) % sz].y - p.y); + if (k > 0 && d1 <= 0 && d2 > 0) + { + wn++; + } + if (k < 0 && d2 <= 0 && d1 > 0) + { + wn--; + } + } + if (wn != 0) + { + return 1; + } + return 0; +} +double ConvexPolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 1; i < n - 1; i++) + { + area += crossDet(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} +bool circlePoly(Circle C, Poly &poly) +{ + int sz = poly.size(); + if (ptInPoly(C.c, poly)) + { + return true; + } + for (int i = 0; i < sz; i++) + { + if (inCircle(poly[i], C)) + { + return true; + } + } + for (int i = 0; i < sz; i++) + { + if (segCircleIntersect(poly[i], poly[(i + 1) % sz], C)) + { + return true; + } + } + return false; +} +int isPointInPolygon(Point p, Polygon po) +{ + int wn = 0; + int n = po.n; + for (int i = 0; i < n; i++) + { + if (OnSegment(p, po[i], po[(i + 1) % n])) + { + return -1; + } + int k = dcmp(Cross(po[(i + 1) % n] - po[i], p - po[i])); + int d1 = dcmp(po[i].y - p.y); + int d2 = dcmp(po[(i + 1) % n].y - p.y); + if (k > 0 && d1 <= 0 && d2 > 0) + { + wn++; + } + if (k < 0 && d2 <= 0 && d1 > 0) + { + wn--; + } + } + if (wn != 0) + { + return 1; + } + return 0; +} +vector circlePolies(Circle C, vector &polies) +{ + vector ret; + ret.clear(); + for (int i = 0, sz = polies.size(); i < sz; i++) + { + if (circlePoly(C, polies[i])) + { + ret.push_back(polies[i].area()); + } + } + return ret; +} + +const double dir[4][2] = {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}}; + +Polygon pl[10000]; +int main() +{ + double L, W; + int n, m; + while (cin >> n >> m >> L >> W && (n + m + L + W > EPS)) + { + vector cur; + cur.push_back(Poly()); + for (int i = 0; i < 4; i++) + { + cur[0].pt.push_back(Point(L * dir[i][0], W * dir[i][1])); + } + Point p[2]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < 2; j++) + { + cin >> p[j].x >> p[j].y; + } + cur = cutPolies(p[0], p[1], cur); + } + double ar[10000]; + double u; + double ans[10000]; + int num = cur.size(); + for (int q = 0; q < num; q++) + { + pl[q].n = cur[q].size(); + for (int e = 0; e < pl[q].n; e++) + { + pl[q].p[e].x = cur[q][e].x; + pl[q].p[e].y = cur[q][e].y; + } + ar[q] = ConvexPolygonArea(pl[q].p, pl[q].n); + } + Point x; + for (int q = 0; q < m; q++) + { + scanf("%lf%lf%lf", &x.x, &x.y, &u); + int s = 0; + for (int e = 0; e < num; e++) + { + bool i = false; + for (int r = 0; r < pl[e].n; r++) + if (DistanceToSegment(x, pl[e][r], pl[e][(r + 1) % pl[e].n]) < u - EPS) + { + i = true; + } + if (isPointInPolygon(x, pl[e])) + { + i = true; + } + if (i) + { + ans[s++] = ar[e]; + } + } + sort(ans, ans + s); + printf("%d", s); + for (int e = 0; e < s; e++) + { + printf(" %.2f", ans[e]); + } + printf("\n"); + } + cout << endl; + } +} diff --git a/12297.cpp b/12297.cpp new file mode 100644 index 0000000..bf2f00c --- /dev/null +++ b/12297.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define MAXD 120 +#define MOD 1000000009 + +typedef long long LL; + +int temp[MAXD][MAXD], f[15][15], N, K, n; +const int d[] = {1, 4, 6, 4, 1}; + +struct matrix +{ + int a[MAXD][MAXD]; + + void init(int x) + { + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + a[i][j] = x; + } + + matrix operator*(const matrix &t) const + { + matrix ans; + ans.init(0); + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (a[i][k]) + { + for (int j = 0; j < n; j++) + if (t.a[k][j]) + ans.a[i][j] = (ans.a[i][j] + (LL)a[i][k] * t.a[k][j]) % MOD; + } + return ans; + } +} mat, unit; + +void prepare() +{ + memset(f[0], 0, sizeof(f[0])); + f[0][0] = 1; + for (int i = 1; i <= 10; i++) + for (int j = 1; j <= 10; j++) + { + f[i][j] = 0; + if (i >= j) + for (int k = 0; k < 5 && j - k >= 0; k++) + f[i][j] = (f[i][j] + (LL)d[k] * f[i - j][j - k]) % MOD; + } +} + +void build() +{ + n = K * (K + 1); + mat.init(0); + for (int i = 0; i < K; i++) + for (int j = 0; j <= K; j++) + mat.a[i * (K + 1) + j][0] = f[K - 1 - i][j]; + unit.init(0); + for (int j = 1; j <= K; j++) + for (int k = 0; k < 5 && j - k >= 0; k++) + unit.a[j][(j - 1) * (K + 1) + j - k] = d[k]; + for (int i = 1; i < K; i++) + for (int j = 0; j <= K; j++) + unit.a[i * (K + 1) + j][(i - 1) * (K + 1) + j] = 1; +} + +void powmod(int n) +{ + while (n) + { + if (n & 1) + mat = unit * mat; + n >>= 1, unit = unit * unit; + } +} + +void solve() +{ + if (N <= 10) + { + int ans = 0; + for (int i = 1; i <= K; i++) + { + ans = (ans + f[N][i]) % MOD; + } + printf("%d\n", ans); + return; + } + build(); + powmod(N - K + 1); + int ans = 0; + for (int i = 1; i <= K; i++) + { + ans = (ans + mat.a[i][0]) % MOD; + } + printf("%d\n", ans); +} + +int main() +{ + prepare(); + while (scanf("%d%d", &N, &K) == 2, N || K) + solve(); + return 0; +} diff --git a/12298.cpp b/12298.cpp new file mode 100644 index 0000000..1409e5b --- /dev/null +++ b/12298.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +bool is[50005], have[5][50005]; +long long int dp[5][50005]; + +int main() +{ + int a, i, j, k, N, n, b; + char ch; + + for (i = 2; i < 50005; i++) + if (!is[i]) + for (j = i * 2; j < 50005; j += i) + is[j] = true; + + while (scanf(" %d %d %d", &a, &b, &N) == 3 && (a + b + N)) + { + for (i = 1; i <= b; i++) + if (is[i]) + have[0][i] = have[1][i] = have[2][i] = have[3][i] = have[4][i] = 1; + + for (n = 0; n < N; n++) + { + scanf(" %d %c", &i, &ch); + if (ch == 'S') + j = 1; + if (ch == 'H') + j = 2; + if (ch == 'C') + j = 3; + if (ch == 'D') + j = 4; + have[j][i] = 0; + } + + memset(dp, 0, sizeof(dp)); + for (i = 1; i <= b; i++) + if (is[i] && have[1][i] == 1) + dp[1][i] = 1; + + for (i = 1; i <= b; i++) + if (is[i] && have[2][i] == 1) + for (j = b; j >= i; j--) + dp[2][j] += dp[1][j - i]; + + for (i = 1; i <= b; i++) + if (is[i] && have[3][i] == 1) + for (j = b; j >= i; j--) + dp[3][j] += dp[2][j - i]; + + for (i = 1; i <= b; i++) + if (is[i] && have[4][i] == 1) + for (j = b; j >= i; j--) + dp[4][j] += dp[3][j - i]; + + for (i = a; i <= b; i++) + printf("%lld\n", dp[4][i]); + printf("\n"); + } + return 0; +} diff --git a/12299.cpp b/12299.cpp new file mode 100644 index 0000000..8aeadd3 --- /dev/null +++ b/12299.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define MAXD 100010 +#define INF 1000000000 + +int N, M, Q, tree[4 * MAXD], a[50], d[50], n; +char b[50]; + +int Min(int &x, int &y) +{ + return x < y ? x : y; +} +void init() +{ + int i, j; + for (M = 1; M < N + 2; M <<= 1) + ; + for (i = M; i < 2 * M; i++) + { + tree[i] = INF; + } + for (i = 0, j = M + 1; i < N; i++, j++) + { + scanf("%d", &tree[j]); + } + for (i = M - 1; i; i--) + { + tree[i] = Min(tree[2 * i], tree[2 * i + 1]); + } +} +void change(int cur, int target) +{ + int i, j; + cur += M; + tree[cur] = target; + for (; cur ^ 1; cur >>= 1) + { + tree[cur >> 1] = Min(tree[cur], tree[cur ^ 1]); + } +} +void solve() +{ + int i, j, k, res, s, t; + scanf("%s", b); + if (b[0] == 's') + { + n = 0; + for (i = 0; b[i]; i++) + { + if (isdigit(b[i])) + { + sscanf(&b[i], "%d", &a[n++]); + } + while (isdigit(b[i])) + { + i++; + } + } + for (i = 0; i < n; i++) + { + if (i == n - 1) + { + d[i] = tree[M + a[0]]; + } + else + { + d[i] = tree[M + a[i + 1]]; + } + } + for (i = 0; i < n; i++) + { + change(a[i], d[i]); + } + } + else + { + sscanf(&b[6], "%d", &s); + sscanf(strchr(b, ',') + 1, "%d", &t); + res = INF; + s = s - 1 + M; + t = t + 1 + M; + for (; s ^ t ^ 1; s >>= 1, t >>= 1) + { + if (~s & 1) + { + res = tree[s ^ 1] < res ? tree[s ^ 1] : res; + } + if (t & 1) + { + res = tree[t ^ 1] < res ? tree[t ^ 1] : res; + } + } + printf("%d\n", res); + } +} + +int main() +{ + while (scanf("%d%d", &N, &Q) == 2) + { + init(); + for (int i = 0; i < Q; i++) + solve(); + } + return 0; +} diff --git a/123.cpp b/123.cpp new file mode 100644 index 0000000..2777db1 --- /dev/null +++ b/123.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +bool theTruthIsOutThere(pair> a, pair> b) +{ + if (a.first < b.first) + { + return true; + } + else if (a.first == b.first) + { + if (a.second.first < b.second.first) + { + return true; + } + else if (a.second.first == b.second.first) + { + if (a.second.second < b.second.second) + { + return true; + } + else + { + return false; + } + } + else + { + return false; + } + } + else + { + return false; + } +} + +int main() +{ + string input; + vector restricted; + vector sentences; + vector>> words; + int counter = 0; + // read restricted words + while (cin >> input) + { + if (input == "::") + { + break; + } + restricted.push_back(input); + } + + while (getline(cin, input)) + { + for (int i = 0; i < input.size(); i++) + { + input[i] = tolower(input[i]); + } + sentences.push_back(input); + stringstream ss; + ss << input; + int c = 0; + while (ss >> input) + { + bool found = false; + for (int i = 0; i < restricted.size(); i++) + { + if (restricted[i] == input) + { + found = true; + break; + } + } + if (!found) + { + words.push_back(pair>(input, pair(counter, c))); + } + c++; + } + counter++; + } + sort(words.begin(), words.end(), theTruthIsOutThere); + for (int i = 0; i < words.size(); i++) + { + stringstream ss; + ss << sentences[words[i].second.first]; + int c = 0; + while (ss >> input) + { + if (c > 0) + { + cout << " "; + } + if (c == words[i].second.second) + { + for (int j = 0; j < input.size(); j++) + { + input[j] = toupper(input[j]); + } + cout << input; + } + else + { + cout << input; + } + c++; + } + cout << endl; + } + return 0; +} diff --git a/1230.cpp b/1230.cpp index 209b4c9..b70e388 100644 --- a/1230.cpp +++ b/1230.cpp @@ -1,26 +1,33 @@ -#include +#include -long long power(long long A, long long B, long long C) { - if (B == 0) return (1 %C); - if (B == 1) return (A %C); +using namespace std; - long long V = power(A, B/2, C); - V = (V %C * V %C) %C; - if (B & 1) - V = (V %C * A %C) %C; - return V; +unsigned int calc(unsigned int x, int y, int n) +{ + if (y == 1) + { + return x; + } + if (y % 2 == 0) + { + return calc((x * x) % n, y / 2, n); + } + else + { + return (x * calc((x * x) % n, y / 2, n)) % n; + } } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - int T; - scanf("%d", &T); - while (T--) { - long long A, B, C; - scanf("%lld%lld%lld", &A, &B, &C); - printf("%lld\n", power(A, B, C)); - } - return 0; +int main() +{ + int cases; + unsigned int x; + int y, n; + cin >> cases; + while (cases--) + { + cin >> x >> y >> n; + cout << calc(x, y, n) << endl; + } + return 0; } diff --git a/12300.cpp b/12300.cpp new file mode 100644 index 0000000..f23ed83 --- /dev/null +++ b/12300.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +const double pi = acos(-1); + +int sqr(int x) +{ + return x * x; +} + +int main() +{ + for (int n, x1, x2, y1, y2; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n) == 5 && n;) + { + double dist = sqrt(sqr(x2 - x1) + sqr(y2 - y1)), R; + if (n & 1) + { + double ang = (n / 2) * 2 * pi / n; + R = dist * sin((pi - ang) / 2) / sin(ang); + } + else + R = 0.5 * dist; + printf("%.6lf\n", R * R * sin(2 * pi / n) * n / 2); + } + return 0; +} diff --git a/12301.cpp b/12301.cpp new file mode 100644 index 0000000..846c320 --- /dev/null +++ b/12301.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define PI 3.141592653589793 + +int a, b, c, d, e; + +bool valid() +{ + if (a + b + c + d + e != 180) + { + return true; + } + return false; +} + +int main() +{ + while (scanf("%d%d%d%d%d", &a, &b, &c, &d, &e) && (a || b || c || d || e)) + { + if (valid()) + { + printf("Impossible\n"); + continue; + } + double pa = (double)a * PI / 180.0; + double pb = (double)b * PI / 180.0; + double pc = (double)c * PI / 180.0; + double pd = (double)d * PI / 180.0; + double pe = (double)e * PI / 180.0; + double la = 10.0; + double lde = la * sin(pd + pe) / sin(pa); + double le = la * sin(pe) / sin(pa + pd); + double ld = lde - le; + double lbc = la * sin(pb + pc) / sin(pa); + double lc = la * sin(pc) / sin(pa + pb); + double lb = lbc - lc; + double lx = sqrt(lb * lb + ld * ld - 2.0 * lb * ld * cos(pa)); + double sx = le * sin(pb) / lx; + printf("%.2lf\n", asin(sx) / PI * 180.0); + } + return 0; +} diff --git a/12302.cpp b/12302.cpp new file mode 100644 index 0000000..23dec5e --- /dev/null +++ b/12302.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +#define sqr(a) ((a) * (a)) +#define trarea(p) fabs((vdet(p[0], p[1], p[2]) / 2.0)) +#define EPS 1e-12 +#define MAXN 20 + +struct Point +{ + double x, y; + Point(){}; + Point(double _x, double _y) : x(_x), y(_y){}; +} p[MAXN]; +struct Seg +{ + Point a, b; + Seg(){}; + Seg(Point _a, Point _b) : a(_a), b(_b){}; +}; +struct Circle +{ + Point c; + double r; +}; +Point must_on[3]; +Circle mc; + +double dot(Point p, Point p1, Point p2) +{ + return (p1.x - p.x) * (p2.x - p.x) + (p1.y - p.y) * (p2.y - p.y); +} +inline double vdet(Point op, Point p1, Point p2) +{ + return (p1.x - op.x) * (p2.y - op.y) - (p2.x - op.x) * (p1.y - op.y); +} +inline double dis(Point a, Point b) +{ + return sqr(a.x - b.x) + sqr(a.y - b.y); +} +Point get_out_Circle(Point p[]) +{ + double c1, c2, xa, xb, xc, ya, yb, yc; + Point o; + xa = p[0].x, xb = p[1].x, xc = p[2].x; + ya = p[0].y, yb = p[1].y, yc = p[2].y; + c1 = (sqr(xa) + sqr(ya) - sqr(xb) - sqr(yb)) / 2.0; + c2 = (sqr(xa) + sqr(ya) - sqr(xc) - sqr(yc)) / 2.0; + o.x = (c1 * (ya - yc) - c2 * (ya - yb)) / ((xa - xb) * (ya - yc) - (xa - xc) * (ya - yb)); + o.y = (c1 * (xa - xc) - c2 * (xa - xb)) / ((ya - yb) * (xa - xc) - (ya - yc) * (xa - xb)); + return o; +} +inline double get_out_r(Point p[]) +{ + double a = dis(p[0], p[1]), b = dis(p[1], p[2]), c = dis(p[2], p[0]), s = trarea(p); + return a * b * c / sqr(4 * s); +} +void get_Circle(int tm) +{ + switch (tm) + { + case 0: + mc.r = -1; + break; + case 1: + mc.r = 0, mc.c = must_on[0]; + break; + case 2: + mc.r = dis(must_on[0], must_on[1]) / 4.0; + mc.c.x = (must_on[0].x + must_on[1].x) / 2.0; + mc.c.y = (must_on[0].y + must_on[1].y) / 2.0; + break; + case 3: + mc.r = get_out_r(must_on); + mc.c = get_out_Circle(must_on); + break; + } +} +void min_Circle(int t, int ton) +{ + get_Circle(ton); + if (ton >= 3) + { + return; + } + for (int i = 0; i < t; i++) + if (dis(mc.c, p[i]) > mc.r + EPS) + { + must_on[ton] = p[i]; + min_Circle(i, ton + 1); + } +} +double relation(Point p, Seg s) +{ + return dot(s.a, p, s.b) / ((s.b.x - s.a.x) * (s.b.x - s.a.x) + (s.b.y - s.a.y) * (s.b.y - s.a.y)); +} +Point perpendicular(Point p, Seg s) +{ + Point t; + double r = relation(p, s); + t.x = s.a.x + r * (s.b.x - s.a.x); + t.y = s.a.y + r * (s.b.y - s.a.y); + return t; +} +Point intersection(Seg u, Seg v) +{ + Point ret = u.a; + double t = ((u.a.x - v.a.x) * (v.a.y - v.b.y) - (u.a.y - v.a.y) * (v.a.x - v.b.x)) / ((u.a.x - u.b.x) * (v.a.y - v.b.y) - (u.a.y - u.b.y) * (v.a.x - v.b.x)); + ret.x += (u.b.x - u.a.x) * t; + ret.y += (u.b.y - u.a.y) * t; + return ret; +} + +int main() +{ + int n; + double x1, x2, x3, y1, y2, y3; + while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3)) + { + n = 0; + if (x1 < 0.0 && x2 < 0.0 && x3 < 0.0 && y1 < 0.0 && y2 < 0.0 && y3 < 0.0) + { + break; + } + p[n++] = Point((x1 + x2) * 0.5, (y1 + y2) * 0.5); + p[n++] = Point((x1 + x3) * 0.5, (y1 + y3) * 0.5); + p[n++] = Point((x2 + x3) * 0.5, (y2 + y3) * 0.5); + Point p1(x1, y1), p2(x2, y2), p3(x3, y3); + Seg s; + s = Seg(p1, p2); + p[n++] = perpendicular(p3, s); + s = Seg(p1, p3); + p[n++] = perpendicular(p2, s); + s = Seg(p2, p3); + p[n++] = perpendicular(p1, s); + Seg s1(p1, p[5]), s2(p2, p[4]); + Point o = intersection(s1, s2); + p[n++] = Point((o.x + x1) * 0.5, (o.y + y1) * 0.5); + p[n++] = Point((o.x + x2) * 0.5, (o.y + y2) * 0.5); + p[n++] = Point((o.x + x3) * 0.5, (o.y + y3) * 0.5); + random_shuffle(p, p + n); + min_Circle(n, 0); + printf("%.6lf %.6lf %.6lf\n", mc.c.x, mc.c.y, sqrt(mc.r)); + } + return 0; +} diff --git a/12303.cpp b/12303.cpp new file mode 100644 index 0000000..aa39198 --- /dev/null +++ b/12303.cpp @@ -0,0 +1,245 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double pi = acos(-1.0); + +int sgn(const double x) +{ + return fabs(x) < eps ? 0 : x < -eps ? -1 : 1; +} + +#define sqr(x) ((x) * (x)) +#define REP(i, x) for (int i = 0; i < x; i++) +#define rep(i, j, k) for (int(i) = (j); (i) < (k); (i)++) +#define maxn 50005 +#define maxm maxn + +struct matrix +{ + int r, c; + double v[10][10]; + matrix() {} + matrix(int a, int b) : r(a), c(b) + { + REP(i, r) + fill(v[i], v[i] + c, 0.0); + if (r == c) + { + REP(i, r)//unit matrix as default matrix + v[i][i] = 1; + } + } + void init() + { + REP(i, r) + fill(v[i], v[i] + c, 0.0); + if (r == c) + { + REP(i, r)//unit matrix as default matrix + v[i][i] = 1; + } + } + matrix operator*(const matrix p) + { + matrix ans(r, p.c); + REP(i, r) + REP(j, ans.c) + { + double tmp = 0.0; + REP(k, c) + tmp += v[i][k] * p.v[k][j]; + ans.v[i][j] = tmp; + } + return ans; + } + void print() + { + rep(i, 0, r) + rep(j, 0, c) + printf("%.2lf%c", v[i][j], j == c - 1 ? '\n' : ' '); + } +}; +struct Tpoint +{ + double x, y, z; + Tpoint() {} + Tpoint(double a, double b, double c) : x(a), y(b), z(c) {} + Tpoint operator-(const Tpoint p) + { + return Tpoint(x - p.x, y - p.y, z - p.z); + } + Tpoint operator+(const Tpoint p) + { + return Tpoint(x + p.x, y + p.y, z + p.z); + } + double operator*(const Tpoint p) + { + return x * p.x + y * p.y + z * p.z; + } + Tpoint operator^(const Tpoint p) + { + return Tpoint(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); + } + bool operator==(const Tpoint p) + { + return sgn(x - p.x) == 0 && sgn(y - p.y) == 0 && sgn(z - p.z) == 0; + } + double norm2() + { + return (*this) * (*this); + } + double norm() + { + return sqrt(norm2()); + } + Tpoint unit() + { + return Tpoint(x / norm(), y / norm(), z / norm()); + } + Tpoint gao(matrix h) + { + matrix o(4, 1); + o.v[0][0] = x; + o.v[1][0] = y; + o.v[2][0] = z; + o.v[3][0] = 1; + o = h * o; + return Tpoint(o.v[0][0], o.v[1][0], o.v[2][0]); + } + void scan() + { + scanf("%lf%lf%lf", &x, &y, &z); + } + void print() + { + printf("%.2lf %.2lf %.2lf\n", x, y, z); + } +} pt[maxn]; +Tpoint tZero(0, 0, 0); +struct Tplane +{ + double a, b, c, d; + //a + b + c = d + Tplane() {} + Tplane(double e, double f, double g, double h) : a(e), b(f), c(g), d(-h) {} + //return a perpendicular vector of the given plane + Tpoint ndir() + { + return Tpoint(a, b, c); + } + //shifting transform + double norm2() + { + return sqr(a) + sqr(b) + sqr(c); + } + double norm() + { + return sqrt(norm2()); + } + Tplane unit() + { + return Tplane(a / norm(), b / norm(), c / norm(), -d / norm()); + } + void print() + { + printf("%.2lf %.2lf %.2lf %.2lf\n", a, b, c, -d); + } +} tp[maxm]; +int n, m, t; +char opt[15]; +int main() +{ + double a, b, c, d; + scanf("%d %d %d", &n, &m, &t); + REP(i, n) + { + scanf("%lf %lf %lf", &a, &b, &c); + pt[i] = Tpoint(a, b, c); + } + REP(i, m) + { + scanf("%lf %lf %lf %lf", &a, &b, &c, &d); + tp[i] = Tplane(a, b, c, d); + } + matrix handle(4, 4), tran(4, 4); + REP(i, t) + { + scanf("%s", opt); + if (opt[0] == 'T') + { + scanf("%lf %lf %lf", &a, &b, &c); + tran.init(); + tran.v[0][3] = a; + tran.v[1][3] = b; + tran.v[2][3] = c; + handle = tran * handle; + } + else if (opt[0] == 'R') + { + scanf("%lf %lf %lf %lf", &a, &b, &c, &d); + tran.init(); + double t = d / 180.0 * pi; + double ct = cos(t), st = sin(t); + double tmp = sqrt(a * a + b * b + c * c); + a /= tmp; + b /= tmp; + c /= tmp; + tran.v[0][0] = (1.0 - ct) * a * a + ct; + tran.v[0][1] = (1.0 - ct) * a * b - st * c; + tran.v[0][2] = (1.0 - ct) * a * c + st * b; + tran.v[1][0] = (1.0 - ct) * b * a + st * c; + tran.v[1][1] = (1.0 - ct) * b * b + ct; + tran.v[1][2] = (1.0 - ct) * b * c - st * a; + tran.v[2][0] = (1.0 - ct) * c * a - st * b; + tran.v[2][1] = (1.0 - ct) * c * b + st * a; + tran.v[2][2] = (1.0 - ct) * c * c + ct; + handle = tran * handle; + } + else if (opt[0] == 'S') + { + scanf("%lf %lf %lf", &a, &b, &c); + tran.init(); + tran.v[0][0] = a; + tran.v[1][1] = b; + tran.v[2][2] = c; + handle = tran * handle; + } + } + REP(i, n) + { + pt[i] = pt[i].gao(handle); + pt[i].print(); + } + REP(i, m) + { + a = tp[i].a, b = tp[i].b, c = tp[i].c, d = tp[i].d; + double limit = max(fabs(a), max(fabs(b), fabs(c))); + Tpoint on[3]; + if (fabs(a) == limit) + { + on[0] = Tpoint(d / a, 0, 0); + on[1] = Tpoint((d - b) / a, 1, 0); + on[2] = Tpoint((d - c) / a, 0, 1); + } + else if (fabs(b) == limit) + { + on[0] = Tpoint(0, d / b, 0); + on[1] = Tpoint(1, (d - a) / b, 0); + on[2] = Tpoint(0, (d - c) / b, 1); + } + else + { + on[0] = Tpoint(0, 0, d / c); + on[1] = Tpoint(1, 0, (d - a) / c); + on[2] = Tpoint(0, 1, (d - b) / c); + } + rep(j, 0, 3) + on[j] = on[j].gao(handle); + Tpoint dir = (on[2] - on[0]) ^ (on[1] - on[0]); + tp[i] = Tplane(dir.x, dir.y, dir.z, -(dir * on[0])); + tp[i].unit().print(); + } + return 0; +} diff --git a/12304.cpp b/12304.cpp new file mode 100644 index 0000000..183c5e1 --- /dev/null +++ b/12304.cpp @@ -0,0 +1,460 @@ +#include + +using namespace std; + +#define maxn 1005 +#define sqr(x) ((x) * (x)) + +const double eps = 1e-8; + +int n; + +int sgn(double x) +{ + return fabs(x) < eps ? 0 : x < -eps ? -1 : 1; +} + +struct point +{ + double x, y; + point() + { + } + point(double a, double b) + : x(a), y(b) + { + } + point operator-(point p) + { + return point(x - p.x, y - p.y); + } + point operator+(point p) + { + return point(x + p.x, y + p.y); + } + double norm() + { + return sqrt(sqr(x) + sqr(y)); + } + double norm2() + { + return sqr(x) + sqr(y); + } + double operator*(point p) + { + return x * p.x + y * p.y; + } + point operator*(double a) + { + return point(x * a, y * a); + } + double operator^(point p) + { + return x * p.y - y * p.x; + } bool + operator==(point p) + { + return sgn(x - p.x) == 0 && sgn(y - p.y) == 0; + } + bool operator<(const point &p) + { + return sgn(x - p.x) < 0 || (sgn(x - p.x) == 0 && sgn(y - p.y) < 0); + } + point rev() + { + return point(-x, -y); + } + point ver() + { + return point(y, -x); + } + point unit() + { + return point(x / norm(), y / norm()); + } + point rotate(double theta) + { + return point(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta)); + } +} p[maxn]; +struct line +{ + point p, k; + line() + { + } + line(point a, point b) + : p(a), k(b) + { + } + line rotate(double theta) + { + line l; + l.p = p; + l.k = k.rotate(theta); + return l; + } + bool on(point q) + { + return sgn((q - p) ^ k) == 0; + } + line move(double offset) + { + point vec = k.ver(); + vec = vec.unit() * offset; + point q = p + vec; + return line(q, k); + } + double dis(point q) + { + point a = p, b = a + k; + return fabs((a - q) ^ (b - q)) / k.norm(); + } + /* + by construct the line through the 'q' parallel to the origin line + calc the lq.p 's vector to 'q' + add it onto the origin line's p + */ + point cast(point q) + { + double d = line(p, k).dis(q); + line lq = line(p, k).move(d); + point vec; + if (!lq.on(q)) + { + lq = line(p, k).move(-d); + } + vec = q - lq.p; + return p + vec; + } + point sym(point q) + { + point c = line(p, k).cast(q); + point vec = c - q; + return c + vec; + } + + int cross(line q, point &ans) + { + point a = p, b = p + k, c = q.p, d = q.p + q.k; + ans = d; + double rate = ((b - a) ^ (a - d)) / ((b - a) ^ (c - d)); + ans = ans + ((c - d) * rate); + } +}; +struct circle +{ + point o; + double r; + circle() + { + } + circle(point _o, double _r) + : o(_o), r(_r) + { + } + circle(point a, point b) + { + o = (a + b) * 0.5; + r = (a - o).norm(); + } + circle(point a, point b, point c) + { + point m1 = (a + b) * 0.5, m2 = (b + c) * 0.5; + line l1 = line(m1, (b - a).ver()), l2 = line(m2, (c - b).ver()); + l1.cross(l2, o); + r = (a - o).norm(); + } + bool in(point x) + { + return sgn((x - o).norm() - r) <= 0; + } + bool inandon(point x) + { + return sgn((x - o).norm() - r) == 0; + } + void tangentline(point p, double &th1, double &th2, int mode) + { + double alpha = atan2(o.y - p.y, o.x - p.x); + if (mode == 1) + { + th1 = alpha + M_PI * 0.5; + if (sgn(th1 - M_PI) >= 0) + { + th1 -= M_PI; + } + else if (sgn(th1) < 0) + { + th1 += M_PI; + } + th1 = th1 / M_PI * 180.0; + } + else + { + double theta = asin(r / (p - o).norm()); + th1 = alpha + theta; + while (sgn(th1 - M_PI) >= 0) + { + th1 -= M_PI; + } + while (sgn(th1) < 0) + { + th1 += M_PI; + } + th2 = alpha - theta; + while (sgn(th2 - M_PI) >= 0) + { + th2 -= M_PI; + } + while (sgn(th2) < 0) + { + th2 += M_PI; + } + th1 = th1 / M_PI * 180.0; + th2 = th2 / M_PI * 180.0; + } + } +}; +circle inscribed(point a, point b, point c) +{ + point a1 = a + (b - a).unit() + (c - a).unit(), b1 = b + (a - b).unit() + (c - b).unit(); + point ans; + line(a, a1 - a).cross(line(b, b1 - b), ans); + double r = line(a, b - a).dis(ans); + return circle(ans, r); +} +int onelineonepoint(point p, line l, double r, circle &a, circle &b) +{ + double d = l.dis(p); + if (sgn(d - 2.0 * r) > 0) + { + return 0; + } + else if (sgn(d - 2.0 * r) == 0) + { + point c = l.cast(p); + a = circle((c + p) * 0.5, r); + return 1; + } + else if (sgn(d - 2.0 * r) < 0 && sgn(d) > 0) + { + double det, theta; + det = d - r; + theta = acos(det / r); + point c = l.cast(p); + point to = c - p; + to = to.unit() * r; + point vec1 = to.rotate(theta), vec2 = to.rotate(-theta); + a = circle(p + vec1, r); + b = circle(p + vec2, r); + return 2; + } + else if (sgn(d) == 0) + { + point per = l.k.ver(); + per = per.unit() * r; + a = circle(p + per, r); + b = circle(p + per.rev(), r); + return 2; + } +} +circle four[10]; +bool cmpcircle(circle a, circle b) +{ + return a.o < b.o; +} +void twolineoneradius(line a, line b, double r) +{ + line A[2], B[2]; + A[0] = a.move(r), A[1] = a.move(-r); + B[0] = b.move(r), B[1] = b.move(-r); + int cnt = 1; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + { + point temp; + A[i].cross(B[j], temp); + four[cnt++] = circle(temp, r); + } +} +int twocircle(circle a, circle b, double r, circle &c, circle &d) +{ + point dir = (b.o - a.o); + double dis = dir.norm(); + double len = dis - a.r - b.r; + double ra = r + a.r, rb = r + b.r; + if (sgn(len - 2.0 * r) > 0) + { + return 0; + } + else if (sgn(len - 2.0 * r) == 0) + { + point to = dir * (ra / (ra + rb)); + c = circle(a.o + to, r); + return 1; + } + else + { + double tempto = (sqr(ra) - sqr(rb) + sqr(dis)) / (2.0 * dis); + point to = dir * (tempto / dis); + point temp = (a.o + to); + point per = to.ver(); + double rate = sqrt(sqr(ra) - to.norm2()) / per.norm(); + per = per * rate; + point per2 = per.rev(); + c = circle(temp + per, r); + d = circle(temp + per2, r); + return 2; + } +} + +bool oneline(point a, point b, point c) +{ + return sgn((a - b) ^ (a - c)) == 0; +} + +char opt[1000]; +char type[][1000] = {"CircumscribedCircle", "InscribedCircle", "TangentLineThroughPoint", "CircleThroughAPointAndTangentToALineWithRadius", "CircleTangentToTwoLinesWithRadius", "CircleTangentToTwoDisjointCirclesWithRadius"}; + +int main() +{ + while (scanf("%s", opt) == 1) + { + int mark = -1; + for (int i = 0; i < 6; i++) + { + if (strcmp(opt, type[i]) == 0) + { + mark = i; + break; + } + } + if (mark == 0) + { + for (int i = 0; i < 3; i++) + { + double x, y; + scanf("%lf %lf", &x, &y); + p[i] = point(x, y); + } + circle ans = circle(p[0], p[1], p[2]); + printf("(%.6lf,%.6lf,%.6lf)\n", ans.o.x + 1e-10, ans.o.y + 1e-10, ans.r + 1e-10); + } + else if (mark == 1) + { + for (int i = 0; i < 3; i++) + { + double x, y; + scanf("%lf %lf", &x, &y); + p[i] = point(x, y); + } + circle ans = inscribed(p[0], p[1], p[2]); + printf("(%.6lf,%.6lf,%.6lf)\n", ans.o.x + 1e-10, ans.o.y + 1e-10, ans.r + 1e-10); + } + else if (mark == 2) + { + double x, y, r; + scanf("%lf %lf %lf", &x, &y, &r); + circle o = circle(point(x, y), r); + scanf("%lf %lf", &x, &y); + double t1, t2; + point p = point(x, y); + if (o.in(p)) + { + if (o.inandon(p)) + { + o.tangentline(p, t1, t2, 1); + printf("[%.6lf]\n", t1 + 1e-10); + } + else + { + puts("[]"); + } + } + else + { + o.tangentline(p, t1, t2, 2); + if (sgn(t1 - t2) > 0) + { + swap(t1, t2); + } + printf("[%.6lf,%.6lf]\n", t1 + 1e-10, t2 + 1e-10); + } + } + else if (mark == 3) + { + double x, y; + for (int i = 0; i < 3; i++) + { + scanf("%lf %lf", &x, &y); + p[i] = point(x, y); + } + double r; + scanf("%lf", &r); + circle a, b; + int mod = onelineonepoint(p[0], line(p[1], p[2] - p[1]), r, a, b); + if (mod == 0) + { + puts("[]"); + } + else if (mod == 1) + { + printf("[(%.6lf,%.6lf)]\n", a.o.x + 1e-10, a.o.y + 1e-10); + } + else + { + if (b.o < a.o) + { + swap(a, b); + } + printf("[(%.6lf,%.6lf),(%.6lf,%.6lf)]\n", a.o.x + 1e-10, a.o.y + 1e-10, b.o.x + 1e-10, b.o.y + 1e-10); + } + } + else if (mark == 4) + { + double x, y, r; + for (int i = 0; i < 4; i++) + { + scanf("%lf %lf", &x, &y); + p[i] = point(x, y); + } + scanf("%lf", &r); + twolineoneradius(line(p[0], p[1] - p[0]), line(p[2], p[3] - p[2]), r); + sort(four + 1, four + 5, cmpcircle); + putchar('['); + for (int i = 1; i <= 4; i++) + { + printf("(%.6lf,%.6lf)%c", four[i].o.x + 1e-10, four[i].o.y + 1e-10, i == 4 ? ']' : ','); + } + puts(""); + } + else if (mark == 5) + { + double x, y, r; + scanf("%lf %lf %lf", &x, &y, &r); + circle a(point(x, y), r); + scanf("%lf %lf %lf", &x, &y, &r); + circle b(point(x, y), r); + scanf("%lf", &r); + circle c, d; + int mod = twocircle(a, b, r, c, d); + if (mod == 0) + { + puts("[]"); + } + else if (mod == 1) + { + printf("[(%.6lf,%.6lf)]\n", c.o.x + 1e-10, c.o.y + 1e-10); + } + else + { + if (d.o < c.o) + { + swap(c, d); + } + printf("[(%.6lf,%.6lf),(%.6lf,%.6lf)]\n", c.o.x + 1e-10, c.o.y + 1e-10, d.o.x + 1e-10, d.o.y + 1e-10); + } + } + } + return 0; +} diff --git a/12305.cpp b/12305.cpp new file mode 100644 index 0000000..5c08ada --- /dev/null +++ b/12305.cpp @@ -0,0 +1,422 @@ +#include + +using namespace std; + +struct halfplane +{ + double a, b, c, d; + int id[1111]; +}; +halfplane half[111]; + +struct point +{ + double x, y, z; + double ax, bx, ay, by, az, bz; +}; +point to, rr; + +struct polygon +{ + vector pt; + int flag, ip; +}; +polygon up, down, lt[111][1111], lst[111111], up_t, down_t[1111], now_t, now, nw, var[1111], temp[1111]; + +int d_cnt[1111], n, m, cnt_up, cnt_down, cnt, num_z[1111]; + +struct zz +{ + double v; + bool del; + bool operator<(const zz &temp) const + { + return v < temp.v; + } +}; +zz z[1111][201]; + +double h, eps = 1e-9, surf[211], volume[211], now_a, now_b, now_c; + +int inter(point l, point r, double a, double b, double c, double d) +{ + double ru, x1 = l.x, y1 = l.y, z1 = l.z, x2 = r.x, y2 = r.y, z2 = r.z, v, ru_a, ru_b; + v = a * (x2 - x1) + b * (y2 - y1) + c * (z2 - z1); + if (fabs(v) < eps) + { + return 0; + } + ru = -(a * x1 + b * y1 + c * z1 + d) / v; + ru_a = -c / v; + ru_b = -(a * x1 + b * y1 + d) / v; + rr.ax = ru_a * (x2 - x1); + rr.bx = x1 + ru_b * (x2 - x1); + rr.x = x1 + ru * (x2 - x1); + rr.ay = ru_a * (y2 - y1); + rr.by = y1 + ru_b * (y2 - y1); + rr.az = 1; + rr.bz = 0; + rr.y = y1 + ru * (y2 - y1); + rr.z = z1 + ru * (z2 - z1); + if (ru < 1 + eps && ru > -eps) + { + return 2; + } + return 0; +} +void deal(polygon &lp, double a, double b, double c, double d) +{ + int i, j, s, p, q, p_ip = lp.ip; + if (lp.pt.size() == 0) + { + return; + } + now.pt.clear(); + now.flag = lp.flag; + for (i = 0; i < lp.pt.size(); i++) + { + if (a * lp.pt[i].x + b * lp.pt[i].y + c * lp.pt[i].z + d < eps) + { + to = lp.pt[i]; + now.pt.push_back(to); + } + if (inter(lp.pt[i], lp.pt[(i + 1) % lp.pt.size()], a, b, c, d) > 0) + { + now.pt.push_back(rr); + } + } + lp = now; + lp.ip = p_ip; +} +void ins(polygon &lp, int ip) +{ + deal(lp, half[ip].a, half[ip].b, half[ip].c, half[ip].d); +} +void insert(int id, int ip) +{ + int i, j, s, p, q; + double a, b, c, d; + if (id == ip) + { + for (i = 1; i < n - 1; i++) + { + a = half[ip].a; + b = half[ip].b; + c = 0; + d = half[ip].d; + if (half[ip].c < 0) + { + a = -a; + b = -b; + d = -d; + } + deal(lt[id][i], a, b, c, d); + a = half[ip].a; + b = half[ip].b; + c = 0; + d = half[ip].c * h + half[ip].d; + if (half[ip].c < 0) + { + a = -a; + b = -b; + d = -d; + } + deal(lt[id][i], -a, -b, -c, -d); + if (half[ip].c == 0) + { + now.pt.clear(); + to = rr = lt[id][i].pt[0]; + for (j = 1; j < lt[id][i].pt.size(); j++) + { + if (fabs(lt[id][i].pt[j].x - to.x) > eps || fabs(lt[id][i].pt[j].y - to.y) > eps) + { + rr = lt[id][i].pt[j]; + break; + } + } + to.z = 0; + now.pt.push_back(to); + to.z = h; + now.pt.push_back(to); + to = rr; + to.z = h; + now.pt.push_back(to); + to.z = 0; + now.pt.push_back(to); + now.ip = 0; + lst[cnt] = now; + lst[cnt++].flag = down_t[i].flag; + half[id].id[i] = cnt - 1; + lt[id][i].pt.clear(); + } + } + } + a = half[id].a; + b = half[id].b; + c = half[id].c; + d = half[id].d; + if (c < 0) + { + a = -a; + b = -b; + c = -c; + d = -d; + } + for (i = 1; i < n - 1; i++) + { + if (half[id].c != 0) + { + deal(lt[id][i], half[ip].a * c - half[ip].c * a, half[ip].b * c - half[ip].c * b, 0, half[ip].d * c - half[ip].c * d); + } + else + { + deal(lst[half[id].id[i]], half[ip].a, half[ip].b, half[ip].c, half[ip].d); + } + } +} +double get_angle(double a, double b, double c) +{ + return atan2(sqrt(a * a + b * b), fabs(c)); +} +double get_area(point a, point b, point c) +{ + double dx1 = (a.x - c.x), dy1 = (a.y - c.y), dz1 = (a.z - c.z), dx2 = (b.x - c.x), dy2 = (b.y - c.y), dz2 = (b.z - c.z); + double dax1, dbx1, day1, dby1, daz1, dbz1, dax2, dbx2, day2, dby2, daz2, dbz2; + double f1 = dx1 * dy2 - dx2 * dy1, f2 = dx1 * dz2 - dz1 * dx2, f3 = dy1 * dz2 - dz1 * dy2; + dax1 = a.ax - c.ax; + dbx1 = a.bx - c.bx; + day1 = a.ay - c.ay; + dby1 = a.by - c.by; + dax2 = b.ax - c.ax; + dbx2 = b.bx - c.bx; + day2 = b.ay - c.ay; + dby2 = b.by - c.by; + now_a = 0.5 * (dax1 * day2 - dax2 * day1); + now_b = 0.5 * (dax1 * dby2 + dbx1 * day2 - dax2 * dby1 - dbx2 * day1); + now_c = 0.5 * (dbx1 * dby2 - dbx2 * dby1); + return 0.5 * sqrt(f1 * f1 + f2 * f2 + f3 * f3); +} +double solve_surf(int id) +{ + int i, j, s, p, q, neg = 0, posi = 0; + double res = 0, now, theta; + for (i = 0; i < cnt; i++) + { + if (lst[i].pt.size() > 0 && lst[i].ip > 0) + { + posi = 1; + } + if (lst[i].pt.size() > 0 && lst[i].ip < 0) + { + neg = 1; + } + } + for (i = 0; i < n - 2; i++) + { + num_z[i] = 0; + if (neg > 0) + { + z[i][num_z[i]++].v = 0; + } + if (posi > 0) + { + z[i][num_z[i]++].v = h; + } + } + for (i = 0; i < cnt; i++) + { + now = 0; + for (j = 1; j < (int)(lst[i].pt.size()) - 1; j++) + { + now += lst[i].flag * get_area(lst[i].pt[0], lst[i].pt[j], lst[i].pt[j + 1]); + } + res += now; + } + for (i = 0; i < id; i++) + for (j = 1; j < n - 1; j++) + { + double test = 0; + for (s = 1; s < (int)(lt[i][j].pt.size()) - 1; s++) + { + now = lt[i][j].flag * get_area(lt[i][j].pt[0], lt[i][j].pt[s], lt[i][j].pt[s + 1]); + theta = get_angle(half[i].a, half[i].b, half[i].c); + now /= cos(theta); + res += now; + test += now; + } + for (s = 0; s < lt[i][j].pt.size(); s++) + { + double xx = lt[i][j].pt[s].x, yy = lt[i][j].pt[s].y; + z[j - 1][num_z[j - 1]++].v = -(half[i].a * xx + half[i].b * yy + half[i].d) / half[i].c; + } + } + return res; +} +double solve_volume(int id) +{ + int i, j, s, p, q, siz; + bool del; + double S1, S2, h1, h2, res = 0, now, rc; + for (j = 1; j < n - 1; j++) + for (i = 0; i < num_z[j - 1] - 1; i++) + { + if (z[j - 1][i].v == z[j - 1][i + 1].v) + { + continue; + } + temp[j] = var[j] = down_t[j]; + siz = var[j].pt.size(); + for (s = 0; s < siz; s++) + { + var[j].pt[s].z = 0.5 * (z[j - 1][i].v + z[j - 1][i + 1].v); + temp[j].pt[s].z = z[j - 1][i].v + 0.25 * (z[j - 1][i + 1].v - z[j - 1][i].v); + var[j].pt[s].ax = 0; + var[j].pt[s].bx = var[j].pt[s].x; + var[j].pt[s].ay = 0; + var[j].pt[s].by = var[j].pt[s].y; + var[j].pt[s].az = 1; + var[j].pt[s].bz = 0; + } + for (s = 0; s <= id; s++) + { + ins(var[j], s); + ins(temp[j], s); + if (var[j].pt.size() < 3) + { + break; + } + } + if (s <= id) + { + continue; + } + siz = var[j].pt.size(); + for (s = 0; s < siz; s++) + { + double ru_a = 1.0 / (var[j].pt[s].z - temp[j].pt[s].z); + double ru_b = -temp[j].pt[s].z / (var[j].pt[s].z - temp[j].pt[s].z); + double x1 = temp[j].pt[s].x, y1 = temp[j].pt[s].y, x2 = var[j].pt[s].x, y2 = var[j].pt[s].y; + var[j].pt[s].ax = ru_a * (x2 - x1); + var[j].pt[s].bx = x1 + ru_b * (x2 - x1); + var[j].pt[s].ay = ru_a * (y2 - y1); + var[j].pt[s].by = y1 + ru_b * (y2 - y1); + } + rc = 0; + for (s = 1; s < siz - 1; s++) + { + get_area(var[j].pt[0], var[j].pt[s], var[j].pt[s + 1]); + now = now_a * (z[j - 1][i + 1].v * z[j - 1][i + 1].v * z[j - 1][i + 1].v - z[j - 1][i].v * z[j - 1][i].v * z[j - 1][i].v) / 3.0; + now += now_b * (z[j - 1][i + 1].v * z[j - 1][i + 1].v - z[j - 1][i].v * z[j - 1][i].v) / 2.0; + now += now_c * (z[j - 1][i + 1].v - z[j - 1][i].v); + res += var[j].flag * fabs(now); + rc += var[j].flag * fabs(now); + } + } + return res; +} + +int main() +{ + int i, j, s, p, q; + while (scanf("%d%lf%d", &n, &h, &m) && (n + h + m)) + { + up.pt.clear(); + down.pt.clear(); + for (i = 0; i < n; i++) + { + scanf("%lf%lf", &to.x, &to.y); + to.z = 0; + down.pt.push_back(to); + to.z = h; + up.pt.push_back(to); + } + cnt = 0; + memset(d_cnt, 0, sizeof(d_cnt)); + for (i = 1; i < n - 1; i++) + { + up_t.pt.clear(); + up_t.pt.push_back(up.pt[0]); + up_t.pt.push_back(up.pt[i]); + up_t.pt.push_back(up.pt[i + 1]); + up_t.ip = 1; + down_t[i].pt.clear(); + down_t[i].pt.push_back(down.pt[0]); + down_t[i].pt.push_back(down.pt[i]); + down_t[i].pt.push_back(down.pt[i + 1]); + down_t[i].ip = -1; + if ((up.pt[i + 1].y - up.pt[0].y) * (up.pt[i].x - up.pt[0].x) > (up.pt[i + 1].x - up.pt[0].x) * (up.pt[i].y - up.pt[0].y)) + { + down_t[i].flag = up_t.flag = 1; + } + else + { + down_t[i].flag = up_t.flag = -1; + } + lst[cnt++] = up_t; + lst[cnt++] = down_t[i]; + now.pt.clear(); + now.pt.push_back(down.pt[0]); + now.pt.push_back(down.pt[i]); + now.pt.push_back(up.pt[i]); + now.pt.push_back(up.pt[0]); + now.pt.clear(); + now.pt.push_back(down.pt[i]); + now.pt.push_back(down.pt[i + 1]); + now.pt.push_back(up.pt[i + 1]); + now.pt.push_back(up.pt[i]); + now.pt.push_back(down.pt[0]); + now.pt.push_back(down.pt[i + 1]); + now.pt.push_back(up.pt[i + 1]); + now.pt.push_back(up.pt[0]); + for (j = 0; j < m; j++) + { + lt[j][i] = down_t[i]; + } + } + for (i = 0; i < n; i++) + { + now_t.pt.clear(); + now_t.pt.push_back(up.pt[i]); + now_t.pt.push_back(up.pt[(i + 1) % n]); + now_t.pt.push_back(down.pt[(i + 1) % n]); + now_t.pt.push_back(down.pt[i]); + now_t.flag = 1; + now_t.ip = 0; + lst[cnt++] = now_t; + } + for (i = 0; i <= m; i++) + { + if (i > 0) + { + scanf("%lf%lf%lf%lf", &half[i - 1].a, &half[i - 1].b, &half[i - 1].c, &half[i - 1].d); + for (j = 0; j < i - 1; j++) + { + insert(j, i - 1); + } + for (j = i - 1; j >= 0; j--) + { + insert(i - 1, j); + } + for (j = 0; j < cnt; j++) + { + if (lst[j].pt.size() == 0) + { + continue; + } + ins(lst[j], i - 1); + } + } + surf[i] = solve_surf(i); + for (j = 0; j < n - 2; j++) + { + sort(z[j], z[j] + num_z[j]); + } + volume[i] = solve_volume(i - 1); + } + for (i = 0; i <= m; i++) + { + printf("%f %f\n", volume[i], surf[i]); + } + } + return 0; +} diff --git a/12306.cpp b/12306.cpp new file mode 100644 index 0000000..24623a3 --- /dev/null +++ b/12306.cpp @@ -0,0 +1,392 @@ +#include + +using namespace std; + +double EPS = 1e-6; + +struct point +{ + double x, y; + int num; + bool operator<(const point &temp) const + { + if (fabs(x - temp.x) < EPS) + { + return y < temp.y - EPS; + } + return x < temp.x; + } + bool operator==(const point &temp) const + { + return !(*this < temp) && !(temp < *this); + } +}; +point poly[222], pt[1000000]; +set ps; + +int cnt_p, num; + +struct segment +{ + point l, r; + bool operator<(const segment &temp) const + { + if (fabs((r.y - l.y) * (temp.r.x - temp.l.x) - (r.x - l.x) * (temp.r.y - temp.l.y)) < EPS) + { + if (fabs((r.x * l.y - l.x * r.y) * (temp.r.x - temp.l.x) - (r.x - l.x) * (temp.r.x * temp.l.y - temp.l.x * temp.r.y)) < EPS) + { + if (l == temp.l) + { + return r < temp.r; + } + return l < temp.l; + } + return (r.x * l.y - l.x * r.y) * (temp.r.x - temp.l.x) - (r.x - l.x) * (temp.r.x * temp.l.y - temp.l.x * temp.r.y) < 0; + } + return (r.y - l.y) * (temp.r.x - temp.l.x) - (r.x - l.x) * (temp.r.y - temp.l.y) < 0; + } +}; +bool cmp(segment a, segment b) +{ + if (a.l == b.l) + { + return a.r < b.r; + } + return a.l < b.l; +} +set hs; +set::iterator it, itt; +set::iterator pit; + +segment lst[1000000], add[1000000]; +int n, len, shu[500], cnt_shu, cnt_lst, cnt_add; +char fs[100], str[1111111]; + +bool inter(segment a, segment b, double &x, double &y) +{ + double ru1, ru2, A, B; + A = (a.r.x - a.l.x) * (b.r.y - b.l.y) - (a.r.y - a.l.y) * (b.r.x - b.l.x); + B = (b.l.x - a.l.x) * (b.r.y - b.l.y) - (b.l.y - a.l.y) * (b.r.x - b.l.x); + if (fabs(A) < EPS) + { + return false; + } + ru1 = B / A; + A = -A; + B = (a.l.x - b.l.x) * (a.r.y - a.l.y) - (a.l.y - b.l.y) * (a.r.x - a.l.x); + ru2 = B / A; + if (ru1 < 1 + EPS && ru1 > -EPS && ru2 < 1 + EPS && ru2 > -EPS) + { + x = a.l.x + ru1 * (a.r.x - a.l.x); + y = a.l.y + ru1 * (a.r.y - a.l.y); + return true; + } + return false; +} +bool same_line(segment a, segment b) +{ + double ju1, ju2; + ju1 = (a.r.y - a.l.y) * (b.r.x - b.l.x) - (a.r.x - a.l.x) * (b.r.y - b.l.y); + ju2 = (a.r.x * a.l.y - a.l.x * a.r.y) * (b.r.x - b.l.x) - (a.r.x - a.l.x) * (b.r.x * b.l.y - b.l.x * b.r.y); + return fabs(ju1) < EPS && fabs(ju2) < EPS; +} +void insert(segment now) +{ + int i, j, s, p, q; + point rp; + segment nw; + double x, y; + cnt_lst = 0; + for (it = hs.begin(); it != hs.end(); it++) + { + if (same_line(now, *it)) + { + if (!(it->r < now.l) && !(now.r < it->l)) + { + lst[cnt_lst++] = *it; + } + } + else if (inter(now, *it, x, y)) + { + lst[cnt_lst++] = *it; + } + } + for (i = 0; i < cnt_lst; i++) + { + hs.erase(lst[i]); + } + num = 0; + for (i = 0; i < cnt_lst; i++) + { + if (same_line(now, lst[i])) + { + if (lst[i].l < now.l) + { + now.l = lst[i].l; + } + if (now.r < lst[i].r) + { + now.r = lst[i].r; + } + } + } + for (i = 0; i < cnt_lst; i++) + { + if (!same_line(now, lst[i])) + { + bool test = inter(now, lst[i], rp.x, rp.y); + if (lst[i].l < rp) + { + nw.l = lst[i].l; + nw.r = rp; + hs.insert(nw); + } + if (rp < lst[i].r) + { + nw.l = rp; + nw.r = lst[i].r; + hs.insert(nw); + } + pt[num++] = rp; + } + } + pt[num++] = now.l; + pt[num++] = now.r; + sort(pt, pt + num); + int nc = 0; + for (i = 0; i < num; i++) + { + if (nc == 0 || pt[nc - 1] < pt[i]) + { + pt[nc++] = pt[i]; + } + } + num = nc; + for (i = 0; i < num - 1; i++) + { + now.l = pt[i]; + now.r = pt[i + 1]; + hs.insert(now); + } +} +void del(double x, double y, double d) +{ + int i, j, s, p, q; + double dis, dt, dis1, dis2; + point rp; + segment now, nw; + cnt_lst = 0; + for (it = hs.begin(); it != hs.end(); it++) + { + now = *it; + dt = sqrt((now.l.x - now.r.x) * (now.l.x - now.r.x) + (now.l.y - now.r.y) * (now.l.y - now.r.y)); + dis = fabs((now.r.y - now.l.y) * x - (now.r.x - now.l.x) * y + now.r.x * now.l.y - now.l.x * now.r.y); + dis /= dt; + dis1 = sqrt((now.l.x - x) * (now.l.x - x) + (now.l.y - y) * (now.l.y - y) - dis * dis); + dis2 = sqrt((now.r.x - x) * (now.r.x - x) + (now.r.y - y) * (now.r.y - y) - dis * dis); + if (dis < d + EPS) + { + if (fabs(dis1 + dis2 - dt) < EPS) + { + lst[cnt_lst++] = *it; + } + else + { + dis1 = sqrt((now.l.x - x) * (now.l.x - x) + (now.l.y - y) * (now.l.y - y)); + dis2 = sqrt((now.r.x - x) * (now.r.x - x) + (now.r.y - y) * (now.r.y - y)); + if (dis1 < d + EPS || dis2 < d + EPS) + { + lst[cnt_lst++] = *it; + } + } + } + } + for (i = 0; i < cnt_lst; i++) + { + hs.erase(lst[i]); + } + ps.clear(); + for (it = hs.begin(); it != hs.end(); it++) + { + now = *it; + pit = ps.find(now.l); + rp = now.l; + if (pit != ps.end()) + { + rp = *pit; + ps.erase(rp); + rp.num++; + } + else + { + rp.num = 1; + } + ps.insert(rp); + pit = ps.find(rp); + rp = now.r; + pit = ps.find(now.r); + if (pit != ps.end()) + { + rp = *pit; + ps.erase(rp); + rp.num++; + } + else + { + rp.num = 1; + } + ps.insert(rp); + } + cnt_lst = cnt_add = 0; + for (it = hs.begin(); it != hs.end(); it++) + { + if (it == hs.begin() || !same_line(add[cnt_add - 1], *it) || !(add[cnt_add - 1].r == it->l)) + { + add[cnt_add++] = *it; + } + else + { + rp = add[cnt_add - 1].r; + pit = ps.find(rp); + if (pit == ps.end() || pit->num > 2) + { + add[cnt_add++] = *it; + } + else + { + add[cnt_add - 1].r = it->r; + } + } + } + hs.clear(); + for (i = 0; i < cnt_add; i++) + { + hs.insert(add[i]); + } +} + +int main() +{ + int i, j, s, p, q; + double x, y, d; + segment now; + while (scanf("%d", &n) && n) + { + getchar(); + hs.clear(); + for (i = 0; i <= n; i++) + { + j = 0; + gets(str); + len = strlen(str); + cnt_shu = 0; + shu[0] = 0; + int flag = 1; + for (j = 0; j < len; j++) + { + if (str[j] == '-') + { + flag = -1; + } + else if (str[j] <= '9' && str[j] >= '0') + { + shu[cnt_shu] = 10 * shu[cnt_shu] + str[j] - '0'; + } + else if (j && str[j - 1] >= '0' && str[j - 1] <= '9' && str[j] == ' ') + { + shu[cnt_shu] *= flag; + cnt_shu++; + shu[cnt_shu] = 0; + flag = 1; + } + } + shu[cnt_shu] *= flag; + cnt_shu++; + if (str[0] == 'D') + { + cnt_p = 0; + for (j = 0; j < cnt_shu; j++) + { + if (j % 2 == 0) + { + poly[cnt_p].x = shu[j]; + } + else + { + poly[cnt_p++].y = shu[j]; + } + } + poly[cnt_p] = poly[0]; + for (j = 0; j < cnt_p - 1; j++) + { + segment now; + now.l = poly[j]; + now.r = poly[j + 1]; + if (now.l == now.r) + { + continue; + } + if (now.r < now.l) + { + swap(now.l, now.r); + } + insert(now); + } + } + else if (str[0] != 'E') + { + x = shu[0]; + y = shu[1]; + d = shu[2]; + del(x, y, d); + } + } + num = 0; + for (it = hs.begin(); it != hs.end(); it++) + { + now = *it; + pt[num++] = now.l; + pt[num++] = now.r; + } + sort(pt, pt + num); + int nc = 0; + for (i = 0; i < num; i++) + { + if (nc == 0 || pt[nc - 1] < pt[i]) + { + pt[nc++] = pt[i]; + } + } + num = nc; + printf("%d\n", num); + for (i = 0; i < num; i++) + { + printf("%.2f %.2f\n", pt[i].x + EPS, pt[i].y + EPS); + } + printf("%d\n", hs.size()); + cnt_lst = 0; + for (it = hs.begin(); it != hs.end(); it++) + { + lst[cnt_lst++] = *it; + } + sort(lst, lst + cnt_lst, cmp); + for (i = 0; i < cnt_lst; i++) + { + now = lst[i]; + int id1 = -1, id2 = -1; + for (j = 0; j < num; j++) + { + if (now.l == pt[j]) + { + id1 = j + 1; + } + if (now.r == pt[j]) + { + id2 = j + 1; + } + } + printf("%d %d\n", id1, id2); + } + } + return 0; +} diff --git a/12307.cpp b/12307.cpp new file mode 100644 index 0000000..d09376f --- /dev/null +++ b/12307.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +#define EPS 1e-12 +#define MAXN 100005 + +struct Point +{ + double x, y; +} pt[MAXN], cpt[MAXN]; +struct Seg +{ + Point a, b; +}; +int n, cn; + +inline bool cmp(const Point &a, const Point &b) +{ + return (a.y < b.y) || (a.y == b.y && a.x < b.x); +} +inline double cross(Point a, Point b, Point c) +{ + return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); +} +inline double dot(Point a, Point b, Point c) +{ + return (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y); +} +inline double sqrdis(Point a, Point b) +{ + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} +double relation(Point p, Seg s) +{ + return dot(s.a, p, s.b) / ((s.b.x - s.a.x) * (s.b.x - s.a.x) + (s.b.y - s.a.y) * (s.b.y - s.a.y)); +} +Point perpendicular(Point p, Seg s) +{ + Point t; + double r = relation(p, s); + t.x = s.a.x + r * (s.b.x - s.a.x); + t.y = s.a.y + r * (s.b.y - s.a.y); + return t; +} +void convexHull() +{ + sort(pt, pt + n, cmp); + int top = 0, i, j, s; + memset(cpt, 0, sizeof(cpt)); + pt[n] = pt[0]; + for (i = 0; i < n; i++) + { + while ((top >= 2) && (cross(cpt[top - 2], cpt[top - 1], pt[i]) <= 0)) + { + top--; + } + cpt[top++] = pt[i]; + } + j = top - 1; + for (i = n - 2; i >= 0; i--) + { + while ((top > j + 1) && (cross(cpt[top - 2], cpt[top - 1], pt[i]) <= 0)) + { + top--; + } + cpt[top++] = pt[i]; + } + cn = top - 1; +} +void areaSBR() +{ + int p, q, r; + double res = 1e99; + p = q = 1; + for (int i = 0; i < cn; i++) + { + while (cross(cpt[i], cpt[i + 1], cpt[p + 1]) - cross(cpt[i], cpt[i + 1], cpt[p]) > 0) + { + p = (p + 1) % cn; + } + while (dot(cpt[i], cpt[i + 1], cpt[q + 1]) - dot(cpt[i], cpt[i + 1], cpt[q]) > 0) + { + q = (q + 1) % cn; + } + if (i == 0) + { + r = q; + } + while (dot(cpt[i], cpt[i + 1], cpt[r + 1]) - dot(cpt[i], cpt[i + 1], cpt[r]) <= 0) + { + r = (r + 1) % cn; + } + double d = sqrdis(cpt[i], cpt[i + 1]); + res = min(res, cross(cpt[i], cpt[i + 1], cpt[p]) * (dot(cpt[i], cpt[i + 1], cpt[q]) - dot(cpt[i], cpt[i + 1], cpt[r])) / d); + } + printf("%.2lf", res); +} +void perimeterSBR() +{ + int p, q, r; + double res = 1e99; + p = q = 1; + for (int i = 0; i < cn; i++) + { + while (cross(cpt[i], cpt[i + 1], cpt[p + 1]) - cross(cpt[i], cpt[i + 1], cpt[p]) > 0) + { + p = (p + 1) % cn; + } + while (dot(cpt[i], cpt[i + 1], cpt[q + 1]) - dot(cpt[i], cpt[i + 1], cpt[q]) > 0) + { + q = (q + 1) % cn; + } + if (i == 0) + { + r = q; + } + while (dot(cpt[i], cpt[i + 1], cpt[r + 1]) - dot(cpt[i], cpt[i + 1], cpt[r]) <= 0) + { + r = (r + 1) % cn; + } + Seg s; + s.a = cpt[i]; + s.b = cpt[i + 1]; + Point p1 = perpendicular(cpt[p], s); + Point p2 = perpendicular(cpt[q], s); + Point p3 = perpendicular(cpt[r], s); + double d = 2.0 * (sqrt(sqrdis(p1, cpt[p])) + sqrt(sqrdis(p2, p3))); + res = min(res, d); + } + printf("%.2lf", res); +} + +int main() +{ + while (scanf("%d", &n) && (n)) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &pt[i].x, &pt[i].y); + } + convexHull(); + if (cn < 3) + { + printf("0.00 0.00\n"); + continue; + } + areaSBR(); + printf(" "); + perimeterSBR(); + printf("\n"); + } + return 0; +} diff --git a/12308.cpp b/12308.cpp new file mode 100644 index 0000000..c4c2bc0 --- /dev/null +++ b/12308.cpp @@ -0,0 +1,384 @@ +#include + +using namespace std; + +#define EPS 1e-10 +#define INF 1e+30 +#define MAXN 128 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) + : x(a), y(b) + { + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + Pt operator/(const double a) const + { + return Pt(x / a, y / a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < EPS && fabs(y - a.y) < EPS; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > EPS) + return x < a.x; + if (fabs(y - a.y) > EPS) + return y < a.y; + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; + +struct Pt3D +{ + double x, y, z; + Pt3D(double a = 0, double b = 0, double c = 0) + : x(a), y(b), z(c) + { + } + Pt3D operator-(const Pt3D &a) const + { + return Pt3D(x - a.x, y - a.y, z - a.z); + } + Pt3D operator+(const Pt3D &a) const + { + return Pt3D(x + a.x, y + a.y, z + a.z); + } + Pt3D operator*(const double a) const + { + return Pt3D(x * a, y * a, z * a); + } + bool operator==(const Pt3D &a) const + { + return fabs(x - a.x) < EPS && fabs(y - a.y) < EPS && fabs(z - a.z) < EPS; + } + bool operator<(const Pt3D &a) const + { + if (fabs(x - a.x) > EPS) + { + return x < a.x; + } + if (fabs(y - a.y) > EPS) + { + return y < a.y; + } + if (fabs(z - a.z) > EPS) + { + return z < a.z; + } + return false; + } + double dot(Pt3D &a) + { + return x * a.x + y * a.y + z * a.z; + } + double length() + { + return sqrt(x * x + y * y + z * z); + } + void read() + { + scanf("%lf%lf%lf", &x, &y, &z); + } +}; + +int cmp_zero(double v) +{ + if (fabs(v) > EPS) + { + return v > 0 ? 1 : -1; + } + return 0; +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int btw(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -EPS && dot(c - b, a - b) >= -EPS; +} +int on_seg(Pt a, Pt b, Pt c) +{ + return btw(a, b, c) && fabs(cross(a, b, c)) < EPS; +} +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} + +struct Seg +{ + Pt s, e; + int label; + Seg(Pt a = Pt(), Pt b = Pt(), int l = 0) + : s(a), e(b), label(l) + { + } + bool operator!=(const Seg &other) const + { + return !((s == other.s && e == other.e) || (e == other.s && s == other.e)); + } +}; + +int intersect(Pt as, Pt at, Pt bs, Pt bt) +{ + return cmp_zero(cross(as, at, bs) * cross(as, at, bt)) <= 0 && + cmp_zero(cross(bs, bt, as) * cross(bs, bt, at)) <= 0; +} +Pt get_intersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +double get_angle(Pt va, Pt vb) +{// seg, !vect + return acos(dot(va, vb) / va.length() / vb.length()); +} +Pt rot_rad(Pt a, double rad) +{ + double x, y; + x = a.x * cos(rad) - a.y * sin(rad); + y = a.x * sin(rad) + a.y * cos(rad); + return Pt(x, y); +} +int in_poly(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (on_seg(p[i], p[j], q)) + { + return 1; + } + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + { + cnt++; + } + } + return cnt & 1; +} + +double rand_dbl() +{ + return (rand() % 32767) / 32767.0f; +} + +Pt3D rot_vect_3d(Pt3D nVec, double dx, double dy, double dz) +{ + Pt3D v = nVec; + // rot x-axis + v = Pt3D(v.x, v.y * cos(dx) - v.z * sin(dx), v.y * sin(dx) + v.z * cos(dx)); + // rot y-axis + v = Pt3D(v.x * cos(dy) + v.z * sin(dy), v.y, -v.x * sin(dy) + v.z * cos(dy)); + // rot z-axis + v = Pt3D(v.x * cos(dz) - v.y * sin(dz), v.x * sin(dz) + v.y * cos(dz), v.z); + return v; +} + +int monotone(int n, Pt p[], Pt ch[]) +{ + sort(p, p + n); + int i, m = 0, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + { + m--; + } + ch[m++] = p[i]; + } + for (i = n - 1, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + { + m--; + } + ch[m++] = p[i]; + } + return m - 1; +} + +double small_rect(int n, Pt p[]) +{ + int l = 1, r = 1, u = 1; + double area = INF, per = INF; + for (int i = 0; i < n; i++) + { + Pt edge = (p[(i + 1) % n] - p[i]) / dist(p[(i + 1) % n], p[i]); + while (dot(edge, p[r % n] - p[i]) < dot(edge, p[(r + 1) % n] - p[i])) + { + r++; + } + while (u < r || cross2(edge, p[u % n] - p[i]) < cross2(edge, p[(u + 1) % n] - p[i])) + { + u++; + } + while (l < u || dot(edge, p[l % n] - p[i]) > dot(edge, p[(l + 1) % n] - p[i])) + { + l++; + } + double w = dot(edge, p[r % n] - p[i]) - dot(edge, p[l % n] - p[i]); + double h = fabs(cross2(p[i] - p[u % n], p[(i + 1) % n] - p[u % n]) / dist(p[i], p[(i + 1) % n])); + area = min(area, w * h); + per = min(per, (w + h) * 2); + } + return area; +} + +double compute_min_vol(Pt3D nVec, Pt3D D[], int N) +{ + double mn, mx, height; + Pt3D pD[MAXN], pVec; + Pt p2D[MAXN]; + mn = mx = nVec.dot(D[0]); + for (int i = 0; i < N; i++) + { + double t = nVec.dot(D[i]); + mn = min(mn, t), mx = max(mx, t); + } + height = (mx - mn) / nVec.length(); + for (int i = 0; i < N; i++) + {// ax + by + cz - mn = 0 + double t = (nVec.dot(D[i]) - mn) / (nVec.x * nVec.x + nVec.y * nVec.y + nVec.z * nVec.z); + pD[i] = Pt3D(D[i].x - t * nVec.x, D[i].y - t * nVec.y, D[i].z - t * nVec.z); + } + double dx, dy, dz, dx2, dy2, dz2; + dx = 0, dy = 0; + dz = -acos(nVec.x / sqrt(nVec.x * nVec.x + nVec.y * nVec.y)); + if (nVec.y < 0) + { + dz = -dz; + } + pVec = rot_vect_3d(nVec, dx, dy, dz); + dx2 = 0, dz2 = 0; + dy2 = -acos(pVec.z / sqrt(pVec.x * pVec.x + pVec.z * pVec.z)); + for (int i = 0; i < N; i++) + { + Pt3D pVec; + pVec = rot_vect_3d(pD[i], dx, dy, dz); + pVec = rot_vect_3d(pVec, dx2, dy2, dz2); + p2D[i] = Pt(pVec.x, pVec.y); + } + Pt ch[MAXN]; + int M = monotone(N, p2D, ch); + double area = small_rect(M, ch); + return height * area; +} + +void test(Pt3D v) +{ + double dx, dy, dz; + Pt3D p; + dx = 0, dy = 0; + dz = -acos(v.x / sqrt(v.x * v.x + v.y * v.y)); + if (v.y < 0) + { + dz = -dz; + } + p = rot_vect_3d(v, dx, dy, dz); + dx = 0, dz = 0; + dy = -acos(p.z / sqrt(p.x * p.x + p.z * p.z)); + p = rot_vect_3d(p, dx, dy, dz); + printf("%lf %lf %lf\n", p.x, p.y, p.z); + printf("%lf %lf\n", v.length(), p.length()); +} + +double anneal(Pt3D D[], int N) +{ +#define S_MUL 0.5f +#define S_LEN acos(-1) +#define T_CNT 20 +#define E_CNT 30 + double step = S_LEN;// deg + Pt3D nVec[E_CNT]; + double val[E_CNT]; + for (int i = 0; i < E_CNT; i++) + { + double x, y, z; + do + { + x = rand_dbl() - 0.5; + y = rand_dbl() - 0.5; + z = rand_dbl() - 0.5; + if (x || y || z) + break; + } while (true); + nVec[i] = Pt3D(x, y, z); + val[i] = compute_min_vol(nVec[i], D, N); + } + for (int it = 0; it < 20; it++) + { + for (int i = 0; i < E_CNT; i++) + { + for (int j = 0; j < T_CNT; j++) + { + double dx, dy, dz, tmp_val; + Pt3D rVec; + dx = rand_dbl() * 2 * step - step; + dy = rand_dbl() * 2 * step - step; + dz = rand_dbl() * 2 * step - step; + rVec = rot_vect_3d(nVec[i], dx, dy, dz); + tmp_val = compute_min_vol(rVec, D, N); + if (tmp_val < val[i]) + { + val[i] = tmp_val, nVec[i] = rVec; + } + } + } + step *= S_MUL; + } + double ret = val[0]; + for (int i = 0; i < E_CNT; i++) + { + ret = min(ret, val[i]); + } + printf("%.2lf\n", ret); +} + +int main() +{ + int N; + Pt3D D[MAXN]; + while (scanf("%d", &N) == 1 && N) + { + for (int i = 0; i < N; i++) + { + D[i].read(); + } + anneal(D, N); + } + return 0; +} diff --git a/1231.cpp b/1231.cpp new file mode 100644 index 0000000..5b70632 --- /dev/null +++ b/1231.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int acorns[2005][2005]; +int dp[2005]; +int max(int a, int b) +{ + return (a > b) ? a : b; +} +int main() +{ + int tc; + int h, n, f; + scanf("%d", &tc); + while (tc--) + { + int i, j; + scanf("%d %d %d", &n, &h, &f); + for (i = 0; i <= n; i++) + for (j = 0; j <= h + 1; j++) + { + dp[j] = acorns[i][j] = 0; + } + for (i = 0; i < n; i++) + { + int k; + scanf("%d", &k); + for (j = 0; j < k; j++) + { + int loc; + scanf("%d", &loc); + acorns[i][loc]++; + } + } + for (i = h; i >= 0; i--) + for (j = 0; j < n; j++) + { + acorns[j][i] += max(acorns[j][i + 1], ((f + i) <= h) ? dp[i + f] : 0); + dp[i] = max(dp[i], acorns[j][i]); + } + printf("%d\n", dp[0]); + } + return 0; +} diff --git a/12311.cpp b/12311.cpp new file mode 100644 index 0000000..dee8c4f --- /dev/null +++ b/12311.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +#define LL long long +const int maxn = 120000 + 10; +struct Point +{ + int x, y; + void input() + { + scanf("%d%d", &x, &y); + } +} pt[maxn]; + +struct Qnode +{ + int l, r, id; + Qnode() {} + Qnode(int l, int r, int id) : l(l), r(r), id(id) {} +} que[maxn]; +int ans[maxn]; +int n; + +LL dis(const Point &A, const Point &B) +{ + return (LL)(A.x - B.x) * (A.x - B.x) + (LL)(A.y - B.y) * (A.y - B.y); +} +void update(int a, int b) +{ + LL d1 = dis(pt[b], pt[a]), d2 = dis(pt[ans[a]], pt[a]); + if (d1 > d2 || d1 == d2 && ans[a] > b) + { + ans[a] = b; + } +} +int find1(int x, int w) +{ + int l = que[x].l, r = que[x].r, ow = que[x].id; + while (l <= r) + { + int m = (l + r) >> 1; + if (dis(pt[m], pt[w]) > dis(pt[m], pt[ow])) + { + r = m - 1; + } + else + { + l = m + 1; + } + } + return r; +} +int find2(int x, int w) +{ + int l = que[x].r, r = que[x].l, ow = que[x].id; + while (l <= r) + { + int m = (l + r) >> 1; + if (dis(pt[m], pt[w]) >= dis(pt[m], pt[ow])) + { + l = m + 1; + } + else + { + r = m - 1; + } + } + return l; +} + +int main() +{ + while (scanf("%d", &n) && n) + { + for (int i = 1; i <= n; i++) + { + pt[i].input(), ans[i] = i; + } + for (int i = 1; i <= n; i++) + { + assert(pt[i].x <= 1000000000 && pt[i].x >= -1000000000), assert(pt[i].y <= 1000000000 && pt[i].y >= -1000000000); + } + int head = 1, tail = 1; + que[head] = Qnode(2, n, 1); + for (int i = 2; i <= n; i++) + { + update(i, que[head].id); + if (i == n) + { + continue; + } + que[head].l++; + if (que[head].l > que[head].r) + { + ++head; + } + if (dis(pt[que[tail].r], pt[i]) <= dis(pt[que[tail].r], pt[que[tail].id])) + { + continue; + } + while (head <= tail && dis(pt[que[tail].l], pt[i]) > dis(pt[que[tail].l], pt[que[tail].id])) + { + --tail; + } + if (head > tail) + { + ++tail; + que[tail] = Qnode(i + 1, n, i); + } + else + { + int t = find1(tail, i); + if (t != que[tail].r) + { + que[tail].r = t; + } + ++tail; + que[tail].l = que[tail - 1].r + 1; + que[tail].r = n; + que[tail].id = i; + } + } + head = 1, tail = 1; + que[head] = Qnode(n - 1, 1, n); + for (int i = n - 1; i >= 1; i--) + { + update(i, que[head].id); + if (i == 1) + { + continue; + } + que[head].l--; + if (que[head].l < que[head].r) + { + ++head; + } + if (dis(pt[que[tail].r], pt[i]) < dis(pt[que[tail].r], pt[que[tail].id])) + { + continue; + } + while (head <= tail && dis(pt[que[tail].l], pt[i]) >= dis(pt[que[tail].l], pt[que[tail].id])) + { + --tail; + } + if (head > tail) + { + ++tail; + que[tail] = Qnode(i - 1, 1, i); + } + else + { + int t = find2(tail, i); + if (t != que[tail].r) + { + que[tail].r = t; + } + ++tail; + que[tail].l = que[tail - 1].r - 1; + que[tail].r = 1; + que[tail].id = i; + } + } + for (int i = 1; i <= n; i++) + { + printf("%d\n", ans[i]); + } + } + return 0; +} diff --git a/12312.cpp b/12312.cpp new file mode 100644 index 0000000..b91e4bc --- /dev/null +++ b/12312.cpp @@ -0,0 +1,564 @@ +#include + +using namespace std; + +struct node +{ + double lx, ly, lz, rx, ry, rz, mx, my, mz; + int flag, mid; + vector iden, idt; +}; +node tree[300000]; + +struct point +{ + double x, y, z; +}; +point pt[44444]; + +double x[44444], y[44444], z[44444], ansx, ansy, ansz, eps = 1e-9, ans_ru; +int tri[3][88888], nx, ny, nz, n, num_tri, num_q, inf = 1000000000, ans_id, vis[44444], ct; +vector adj[44444]; + +double inter(double fx, double fy, double fz, double tx, double ty, double tz, int id) +{ + double A, B, A1, A2, C1, D1, C2, D2, ru, ix, iy, iz, x1, y1, z1, x2, y2, z2, ru1, ru2; + x1 = pt[tri[1][id]].x - pt[tri[0][id]].x; + y1 = pt[tri[1][id]].y - pt[tri[0][id]].y; + z1 = pt[tri[1][id]].z - pt[tri[0][id]].z; + x2 = pt[tri[2][id]].x - pt[tri[0][id]].x; + y2 = pt[tri[2][id]].y - pt[tri[0][id]].y; + z2 = pt[tri[2][id]].z - pt[tri[0][id]].z; + A1 = x1 * y2 - x2 * y1; + C1 = y2 * (tx - fx) - x2 * (ty - fy); + D1 = (fx - pt[tri[0][id]].x) * y2 - (fy - pt[tri[0][id]].y) * x2; + A2 = y1 * z2 - y2 * z1; + C2 = z2 * (ty - fy) - y2 * (tz - fz); + D2 = (fy - pt[tri[0][id]].y) * z2 - (fz - pt[tri[0][id]].z) * y2; + A = A1 * C2 - A2 * C1; + B = -(A1 * D2 - A2 * D1); + bool ff = true; + if (fabs(A) < eps) + { + ff = false; + A1 = x1 * y2 - x2 * y1; + C1 = y2 * (tx - fx) - x2 * (ty - fy); + D1 = (fx - pt[tri[0][id]].x) * y2 - (fy - pt[tri[0][id]].y) * x2; + A2 = x1 * z2 - x2 * z1; + C2 = z2 * (tx - fx) - x2 * (tz - fz); + D2 = (fx - pt[tri[0][id]].x) * z2 - (fz - pt[tri[0][id]].z) * x2; + A = A1 * C2 - A2 * C1; + B = -(A1 * D2 - A2 * D1); + } + ru = B / A; + if (fabs(A1) > eps) + { + ru1 = (C1 * ru + D1) / A1; + } + else + { + ru1 = (C2 * ru + D2) / A2; + } + if (fabs(x2) > eps) + { + ru2 = (fx + ru * (tx - fx) - pt[tri[0][id]].x - ru1 * x1) / x2; + } + else if (ff) + { + ru2 = (fy + ru * (ty - fy) - pt[tri[0][id]].y - ru1 * y1) / y2; + } + else + { + ru2 = (fz + ru * (tz - fz) - pt[tri[0][id]].z - ru1 * z1) / z2; + } + if (ru > 0 && ru1 > 0 && ru2 > 0 && (ru1 + ru2) < 1) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + return ru; + } + return inf; +} +int inner(double xx, double yy, double zz, double lx, double ly, double lz, double rx, double ry, double rz) +{ + int res = 0; + if (xx >= lx) + { + res |= 1; + } + if (xx <= rx) + { + res |= 2; + } + if (yy >= ly) + { + res |= 4; + } + if (yy <= ry) + { + res |= 8; + } + if (zz >= lz) + { + res |= 16; + } + if (zz <= rz) + { + res |= 32; + } + return res; +} +void build(double lx, double ly, double lz, double rx, double ry, double rz, int id) +{ + int nx, ny, nz, siz, flag = -1, ip, cx, cy, cz, l = 2 * id + 1, r = 2 * id + 2, i, siz_t; + double in = -inf, mx, my, mz; + tree[id].lx = lx; + tree[id].ly = ly; + tree[id].lz = lz; + tree[id].rx = rx; + tree[id].ry = ry; + tree[id].rz = rz; + siz = tree[id].iden.size(); + for (i = 0; i < siz; i++) + { + x[i] = pt[tree[id].iden[i]].x; + y[i] = pt[tree[id].iden[i]].y; + z[i] = pt[tree[id].iden[i]].z; + } + sort(x, x + siz); + sort(y, y + siz); + sort(z, z + siz); + nx = 0; + for (i = 0; i < siz; i++) + { + if (nx == 0 || x[nx - 1] < x[i]) + { + x[nx++] = x[i]; + } + } + ny = 0; + for (i = 0; i < siz; i++) + { + if (ny == 0 || y[ny - 1] < y[i]) + { + y[ny++] = y[i]; + } + } + nz = 0; + for (i = 0; i < siz; i++) + { + if (nz == 0 || z[nz - 1] < z[i]) + { + z[nz++] = z[i]; + } + } + if (in < rx - lx && nx > 1) + { + in = rx - lx; + tree[id].mx = mx = 0.5 * (x[(nx - 1) / 2] + x[(nx + 1) / 2]); + flag = 0; + } + if (in < ry - ly && ny > 1) + { + in = ry - ly; + tree[id].my = my = 0.5 * (y[(ny - 1) / 2] + y[(ny + 1) / 2]); + flag = 1; + } + if (in < rz - lz && nz > 1) + { + in = rz - lz; + tree[id].mz = mz = 0.5 * (z[(nz - 1) / 2] + z[(nz + 1) / 2]); + flag = 2; + } + tree[id].flag = flag; + if (tree[id].iden.size() <= 1) + { + return; + } + tree[l].iden.clear(); + tree[r].iden.clear(); + tree[l].idt.clear(); + tree[r].idt.clear(); + siz = tree[id].iden.size(); + siz_t = tree[id].idt.size(); + if (flag == 0) + { + for (i = 0; i < siz; i++) + { + ip = tree[id].iden[i]; + if (pt[ip].x <= mx)//if(pt[ip].x<=x[mx]) + { + tree[l].iden.push_back(ip); + } + else + { + tree[r].iden.push_back(ip); + } + } + for (i = 0; i < siz_t; i++) + { + ip = tree[id].idt[i]; + int id1, id2, id3; + id1 = tri[0][ip]; + id2 = tri[1][ip]; + id3 = tri[2][ip]; + int add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, lx, ly, lz, mx, ry, rz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, lx, ly, lz, mx, ry, rz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, lx, ly, lz, mx, ry, rz); + if (add == 63) + { + tree[l].idt.push_back(ip); + } + add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, mx, ly, lz, rx, ry, rz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, mx, ly, lz, rx, ry, rz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, mx, ly, lz, rx, ry, rz); + if (add == 63) + { + tree[r].idt.push_back(ip); + } + } + build(lx, ly, lz, mx, ry, rz, l); + build(mx, ly, lz, rx, ry, rz, r); + } + else if (flag == 1) + { + for (i = 0; i < siz; i++) + { + ip = tree[id].iden[i]; + if (pt[ip].y <= my) + { + tree[l].iden.push_back(ip); + } + else + { + tree[r].iden.push_back(ip); + } + } + for (i = 0; i < siz_t; i++) + { + ip = tree[id].idt[i]; + int id1, id2, id3; + id1 = tri[0][ip]; + id2 = tri[1][ip]; + id3 = tri[2][ip]; + int add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, lx, ly, lz, rx, my, rz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, lx, ly, lz, rx, my, rz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, lx, ly, lz, rx, my, rz); + if (add == 63) + { + tree[l].idt.push_back(ip); + } + add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, lx, my, lz, rx, ry, rz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, lx, my, lz, rx, ry, rz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, lx, my, lz, rx, ry, rz); + if (add == 63) + { + tree[r].idt.push_back(ip); + } + } + build(lx, ly, lz, rx, my, rz, l); + build(lx, my, lz, rx, ry, rz, r); + } + else + { + for (i = 0; i < siz; i++) + { + ip = tree[id].iden[i]; + if (pt[ip].z <= mz) + { + tree[l].iden.push_back(ip); + } + else + { + tree[r].iden.push_back(ip); + } + } + for (i = 0; i < siz_t; i++) + { + ip = tree[id].idt[i]; + int id1, id2, id3; + id1 = tri[0][ip]; + id2 = tri[1][ip]; + id3 = tri[2][ip]; + int add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, lx, ly, lz, rx, ry, mz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, lx, ly, lz, rx, ry, mz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, lx, ly, lz, rx, ry, mz); + if (add == 63) + { + tree[l].idt.push_back(ip); + } + add = 0; + add |= inner(pt[id1].x, pt[id1].y, pt[id1].z, lx, ly, mz, rx, ry, rz); + add |= inner(pt[id2].x, pt[id2].y, pt[id2].z, lx, ly, mz, rx, ry, rz); + add |= inner(pt[id3].x, pt[id3].y, pt[id3].z, lx, ly, mz, rx, ry, rz); + if (add == 63) + { + tree[r].idt.push_back(ip); + } + } + build(lx, ly, lz, rx, ry, mz, l); + build(lx, ly, mz, rx, ry, rz, r); + } +} +bool query(double fx, double fy, double fz, double tx, double ty, double tz, int id) +{ + int ip, flag = -1, i, j, s, p, q; + int l = 2 * id + 1, r = 2 * id + 2; + double in = inf, ru, ix, iy, iz, lx, ly, lz, rx, ry, rz, mx, my, mz; + lx = tree[id].lx; + ly = tree[id].ly; + lz = tree[id].lz; + rx = tree[id].rx; + ry = tree[id].ry; + rz = tree[id].rz; + mx = tree[id].mx; + my = tree[id].my; + mz = tree[id].mz; + if (fx != tx) + { + ru = (double)(lx - fx) / (double)(tx - fx); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + ru = (double)(rx - fx) / (double)(tx - fx); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + } + if (fy != ty) + { + ru = (double)(ly - fy) / (double)(ty - fy); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + ru = (double)(ry - fy) / (double)(ty - fy); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + } + if (fz != tz) + { + ru = (double)(lz - fz) / (double)(tz - fz); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + ru = (double)(rz - fz) / (double)(tz - fz); + if (ru > 0 && in > ru) + { + ix = fx + ru * (tx - fx); + iy = fy + ru * (ty - fy); + iz = fz + ru * (tz - fz); + if (ix <= rx + eps && ix >= lx - eps && iy <= ry + eps && iy >= ly - eps && iz <= rz + eps && iz >= lz - eps) + { + in = ru; + flag = 0; + } + } + } + if (!(fx <= rx + eps && fx >= lx - eps && fy >= ly - eps && fy <= ry + eps && fz >= lz - eps && fz <= rz + eps) && ans_ru < inf && (ans_ru <= in + eps)) + { + return true; + } + if (flag == -1) + { + return false; + } + ix = fx + in * (tx - fx); + iy = fy + in * (ty - fy); + iz = fz + in * (tz - fz); + if (tree[id].idt.size() <= 1 || tree[id].iden.size() <= 1) + { + for (i = 0; i < tree[id].idt.size(); i++) + { + ip = tree[id].idt[i]; + ru = inter(fx, fy, fz, tx, ty, tz, ip); + if (ans_ru > ru) + { + ans_ru = ru; + ans_id = ip; + ansx = fx + ru * (tx - fx); + ansy = fy + ru * (ty - fy); + ansz = fz + ru * (tz - fz); + } + } + if (ans_ru < inf && ansx <= rx + eps && ansx >= lx - eps && ansy <= ry + eps && ansy >= ly - eps && ansz <= rz + eps && ansz >= lz - eps) + { + return true; + } + return false; + } + if (tree[id].flag == 0) + { + if (ix <= mx + eps && !(fx <= rx + eps && fx >= mx - eps && fy <= ry + eps && fy >= ly - eps && fz <= rz + eps && fz >= lz - eps)) + { + if (query(fx, fy, fz, tx, ty, tz, l)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, r); + } + if (query(fx, fy, fz, tx, ty, tz, r)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, l); + } + else if (tree[id].flag == 1) + { + if (iy <= my + eps && !(fx <= rx + eps && fx >= lx - eps && fy <= ry + eps && fy >= my - eps && fz <= rz + eps && fz >= lz - eps)) + { + if (query(fx, fy, fz, tx, ty, tz, l)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, r); + } + if (query(fx, fy, fz, tx, ty, tz, r)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, l); + } + else + { + if (iz <= mz + eps && !(fx <= rx + eps && fx >= lx - eps && fy <= ry + eps && fy >= ly - eps && fz <= rz + eps && fz >= mz - eps))//||(fx<=rx&&fx>=lx&&fy<=ry&&fy>=ly&&fz<=mz&&fz>=lz)) + { + if (query(fx, fy, fz, tx, ty, tz, l)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, r); + } + if (query(fx, fy, fz, tx, ty, tz, r)) + { + return true; + } + return query(fx, fy, fz, tx, ty, tz, l); + } +} + +int main() +{ + int i, j, s, p, q; + double fx, fy, fz, tx, ty, tz, lx, ly, lz, rx, ry, rz; + while (scanf("%d", &n) && n) + { + for (i = 0; i < n; i++) + { + scanf("%lf%lf%lf", &pt[i].x, &pt[i].y, &pt[i].z); + } + lx = ly = lz = inf; + rx = ry = rz = -inf; + for (i = 0; i < n; i++) + { + x[i] = pt[i].x; + y[i] = pt[i].y; + z[i] = pt[i].z; + if (lx > x[i]) + { + lx = x[i]; + } + if (ly > y[i]) + { + ly = y[i]; + } + if (lz > z[i]) + { + lz = z[i]; + } + if (rx < x[i]) + { + rx = x[i]; + } + if (ry < y[i]) + { + ry = y[i]; + } + if (rz < z[i]) + { + rz = z[i]; + } + adj[i].clear(); + } + scanf("%d", &num_tri); + for (i = 0; i < num_tri; i++) + { + scanf("%d%d%d", &tri[0][i], &tri[1][i], &tri[2][i]); + adj[tri[0][i]].push_back(i); + adj[tri[1][i]].push_back(i); + adj[tri[2][i]].push_back(i); + } + tree[0].iden.clear(); + tree[0].idt.clear(); + for (i = 0; i < n; i++) + { + tree[0].iden.push_back(i); + } + for (i = 0; i < num_tri; i++) + { + tree[0].idt.push_back(i); + } + build(lx, ly, lz, rx, ry, rz, 0); + scanf("%d", &num_q); + for (i = 0; i < num_q; i++) + { + scanf("%lf%lf%lf%lf%lf%lf", &fx, &fy, &fz, &tx, &ty, &tz); + ans_ru = inf; + if (query(fx, fy, fz, tx, ty, tz, 0)) + { + printf("%d %.6f %.6f %.6f\n", ans_id, ansx, ansy, ansz); + } + else + { + printf("-1\n"); + } + } + } + return 0; +} diff --git a/12314.cpp b/12314.cpp new file mode 100644 index 0000000..619f090 --- /dev/null +++ b/12314.cpp @@ -0,0 +1,741 @@ +#include + +using namespace std; + +bool no, there, vis[111][111], mat[111][111]; +double r, inf = 1e9, eps2 = 1e-6, eps = 1e-6, oox = -13573.123479, ooy = -31736.3543217, pr, now, ans, lx, ly, rx, ry; +int n, cnt_po, ps[2][1111], nt, cnt_x[111], tn, pos[111], cnt_cir, num[1111], nxt[1111], tnum[1111], pnum[1111]; + +struct point +{ + double x, y, px, py, qx, qy, theta; + int kind, id1, id2, id, at; + bool operator<(const point &temp) const + { + if (fabs(x - temp.x) < eps) + { + return y < temp.y - eps; + } + return x < temp.x; + } + bool operator==(const point &temp) const + { + return fabs(x - temp.x) < eps && fabs(y - temp.y) < eps; + } +}; + +bool cmp(point a, point b) +{ + return a.theta < b.theta - eps; +} + +point pt[111], cir[10000], adj[1111][1111], tadj[1111][1111], padj[1111][1111], poly[1111], lst[111], xl[111][111]; + +bool inter(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) +{ + int i, j, s, p, q; + double ru1, ru2, A, B; + A = (x2 - x1) * (y4 - y3) - (x4 - x3) * (y2 - y1); + B = (x3 - x1) * (y4 - y3) - (x4 - x3) * (y3 - y1); + if (fabs(A) < eps) + { + pr = inf; + return false; + } + ru1 = B / A; + A = -A; + B = (x1 - x3) * (y2 - y1) - (y1 - y3) * (x2 - x1); + ru2 = B / A; + pr = ru1; + return ru1 < 1 + eps && ru1 > -eps && ru2 < 1 + eps && ru2 > -eps; +} + +bool inner(double cx, double cy) +{ + int res = 0, i, j, s, p, q; + double x1, y1, x2, y2; + for (i = 0; i < n; i++) + { + x1 = pt[i].x; + y1 = pt[i].y; + x2 = pt[(i + 1) % n].x; + y2 = pt[(i + 1) % n].y; + res ^= inter(x1, y1, x2, y2, oox, ooy, cx, cy); + } + return res; +} + +bool check(double cx, double cy) +{ + int i, j, s, p, q; + double nowx, nowy, dist, x1, y1, x2, y2, ru, A, B, C, delta, theta, th, xp, yp; + for (i = 0; i < n; i++) + { + x1 = pt[i].x; + y1 = pt[i].y; + x2 = pt[(i + 1) % n].x; + y2 = pt[(i + 1) % n].y; + dist = sqrt((pt[i].x - cx) * (pt[i].x - cx) + (pt[i].y - cy) * (pt[i].y - cy)); + if (dist < r - 1e-5) + { + return false; + } + nowx = 0.5 * (pt[i].x + pt[(i + 1) % n].x); + nowy = 0.5 * (pt[i].y + pt[(i + 1) % n].y); + dist = sqrt((nowx - cx) * (nowx - cx) + (nowy - cy) * (nowy - cy)); + if (dist < r - eps2) + { + return false; + } + theta = atan2(y2 - y1, x2 - x1); + if ((cy - y1) * (x2 - x1) - (cx - x1) * (y2 - y1) < 0) + { + theta += M_PI / 2; + } + else + { + theta -= M_PI / 2; + } + double ds = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); + ds = fabs(cx * (y2 - y1) - cy * (x2 - x1) + x2 * y1 - x1 * y2) / ds; + if (ds - r > -eps2) + { + continue; + } + th = theta + acos(ds / r); + xp = cx + r * cos(th); + yp = cy + r * sin(th); + if (fabs(x1 - x2) < eps) + { + ru = (yp - y1) / (y2 - y1); + } + else + { + ru = (xp - x1) / (x2 - x1); + } + if (ru < 1 - eps2 && ru > eps2) + { + return false; + } + th = theta - acos(ds / r); + xp = cx + r * cos(th); + yp = cy + r * sin(th); + if (fabs(x1 - x2) < eps) + { + ru = (yp - y1) / (y2 - y1); + } + else + { + ru = (xp - x1) / (x2 - x1); + } + if (ru < 1 - eps2 && ru > eps2) + { + return false; + } + } + return !inner(cx, cy); +} + +bool get_P_to_P(int id1, int id2) +{ + int i, j, s, p, q; + bool res = false; + double cx, cy, th, theta, alpha, dist = sqrt((pt[id1].x - pt[id2].x) * (pt[id1].x - pt[id2].x) + (pt[id1].y - pt[id2].y) * (pt[id1].y - pt[id2].y)); + dist /= 2.0; + if (r < dist - eps) + { + return false; + } + if (r < dist) + { + dist = r; + } + theta = acos(dist / r); + alpha = atan2(pt[id2].y - pt[id1].y, pt[id2].x - pt[id1].x); + th = alpha + theta; + cir[cnt_cir].id1 = id1; + cir[cnt_cir].id2 = id2; + cir[cnt_cir].kind = 0; + cir[cnt_cir].x = cx = pt[id1].x + r * cos(th); + cir[cnt_cir].y = cy = pt[id1].y + r * sin(th); + cir[cnt_cir].px = pt[id1].x; + cir[cnt_cir].py = pt[id1].y; + cir[cnt_cir].qx = pt[id2].x; + cir[cnt_cir].qy = pt[id2].y; + if (check(cx, cy)) + { + cnt_cir++; + i = id1; + j = id2; + padj[i][pnum[i]] = cir[cnt_cir - 1]; + padj[i][pnum[i]].theta = atan2(cir[cnt_cir - 1].y - pt[i].y, cir[cnt_cir - 1].x - pt[i].x); + padj[i][pnum[i]++].id = cnt_cir - 1; + padj[j][pnum[j]] = cir[cnt_cir - 1]; + padj[j][pnum[j]].theta = atan2(cir[cnt_cir - 1].y - pt[j].y, cir[cnt_cir - 1].x - pt[j].x); + padj[j][pnum[j]++].id = cnt_cir - 1; + } + th = alpha - theta; + if (fabs(theta) < eps) + { + return false; + } + cir[cnt_cir].x = cx = pt[id1].x + r * cos(th); + cir[cnt_cir].y = cy = pt[id1].y + r * sin(th); + cir[cnt_cir].px = pt[id1].x; + cir[cnt_cir].py = pt[id1].y; + cir[cnt_cir].qx = pt[id2].x; + cir[cnt_cir].qy = pt[id2].y; + if (check(cx, cy)) + { + cir[cnt_cir].id1 = id1; + cir[cnt_cir].id2 = id2; + cir[cnt_cir].kind = 0; + cnt_cir++; + i = id1; + j = id2; + padj[i][pnum[i]] = cir[cnt_cir - 1]; + padj[i][pnum[i]].theta = atan2(cir[cnt_cir - 1].y - pt[i].y, cir[cnt_cir - 1].x - pt[i].x); + padj[i][pnum[i]++].id = cnt_cir - 1; + padj[j][pnum[j]] = cir[cnt_cir - 1]; + padj[j][pnum[j]].theta = atan2(cir[cnt_cir - 1].y - pt[j].y, cir[cnt_cir - 1].x - pt[j].x); + padj[j][pnum[j]++].id = cnt_cir - 1; + } +} + +bool get_P_to_L(int id1, int id2) +{ + int i, j, s, p, q; + double ds, nx, ny, th, xx, yy, cc, theta, alpha, dist, A, B, C, x1, y1, x2, y2, dd; + x1 = pt[id1].x; + y1 = pt[id1].y; + x2 = pt[(id1 + 1) % n].x; + y2 = pt[(id1 + 1) % n].y; + A = y2 - y1; + B = x1 - x2; + C = y1 * x2 - x1 * y2; + dd = sqrt(A * A + B * B); + double tc = C; + for (q = 0; q < 2; q++) + { + if (q == 1 && A * pt[id2].x + B * pt[id2].y + tc > eps) + { + continue; + } + if (q == 0 && A * pt[id2].x + B * pt[id2].y + tc < -eps) + { + continue; + } + if (q == 0) + { + C = tc - r * dd; + } + else + { + C = tc + r * dd; + } + dist = fabs(A * pt[id2].x + B * pt[id2].y + C) / dd; + if (dist > r + eps) + { + return false; + } + if (dist > r) + { + dist = r; + } + cc = A * pt[id2].y - B * pt[id2].x; + xx = (-C * A - cc * B) / (A * A + B * B); + yy = (cc * A - C * B) / (A * A + B * B); + theta = acos(dist / r); + alpha = atan2(yy - pt[id2].y, xx - pt[id2].x); + xx = pt[id2].x + r * cos(theta + alpha); + yy = pt[id2].y + r * sin(theta + alpha); + cir[cnt_cir].id1 = id1; + cir[cnt_cir].id2 = id2; + cir[cnt_cir].kind = 1; + cir[cnt_cir].x = xx; + cir[cnt_cir].y = yy; + dist = sqrt((xx - pt[id1].x) * (xx - pt[id1].x) + (yy - pt[id1].y) * (yy - pt[id1].y) - r * r); + th = atan2(pt[(id1 + 1) % n].y - pt[id1].y, pt[(id1 + 1) % n].x - pt[id1].x); + cir[cnt_cir].px = nx = xx + r * cos(th + M_PI / 2); + cir[cnt_cir].py = ny = yy + r * sin(th + M_PI / 2); + ds = fabs(A * nx + B * ny - x1 * y2 + x2 * y1); + if (ds > eps) + { + cir[cnt_cir].px = nx = xx + r * cos(th - M_PI / 2); + cir[cnt_cir].py = ny = yy + r * sin(th - M_PI / 2); + } + cir[cnt_cir].qx = pt[id2].x; + cir[cnt_cir].qy = pt[id2].y; + if (nx < max(x1, x2) + eps && nx > min(x1, x2) - eps && ny < max(y1, y2) + eps && ny > min(y1, y2) - eps && check(xx, yy)) + { + cnt_cir++; + i = id1; + j = id2; + adj[i][num[i]] = cir[cnt_cir - 1]; + adj[i][num[i]++].id = cnt_cir - 1; + padj[j][pnum[j]] = cir[cnt_cir - 1]; + padj[j][pnum[j]].theta = atan2(cir[cnt_cir - 1].y - pt[j].y, cir[cnt_cir - 1].x - pt[j].x); + padj[j][pnum[j]++].id = cnt_cir - 1; + } + if (fabs(theta) < eps) + { + continue; + } + xx = pt[id2].x + r * cos(alpha - theta); + yy = pt[id2].y + r * sin(alpha - theta); + cir[cnt_cir].id1 = id1; + cir[cnt_cir].id2 = id2; + cir[cnt_cir].kind = 1; + cir[cnt_cir].x = xx; + cir[cnt_cir].y = yy; + dist = sqrt((xx - pt[id1].x) * (xx - pt[id1].x) + (yy - pt[id1].y) * (yy - pt[id1].y) - r * r); + th = atan2(pt[(id1 + 1) % n].y - pt[id1].y, pt[(id1 + 1) % n].x - pt[id1].x); + cir[cnt_cir].px = nx = xx + r * cos(th + M_PI / 2); + cir[cnt_cir].py = ny = yy + r * sin(th + M_PI / 2); + ds = fabs(A * nx + B * ny - x1 * y2 + x2 * y1); + if (ds > eps) + { + cir[cnt_cir].px = nx = xx + r * cos(th - M_PI / 2); + cir[cnt_cir].py = ny = yy + r * sin(th - M_PI / 2); + } + cir[cnt_cir].qx = pt[id2].x; + cir[cnt_cir].qy = pt[id2].y; + if (nx < max(x1, x2) + eps && nx > min(x1, x2) - eps && ny < max(y1, y2) + eps && ny > min(y1, y2) - eps && check(xx, yy)) + { + cnt_cir++; + i = id1; + j = id2; + adj[i][num[i]] = cir[cnt_cir - 1]; + adj[i][num[i]++].id = cnt_cir - 1; + padj[j][pnum[j]] = cir[cnt_cir - 1]; + padj[j][pnum[j]].theta = atan2(cir[cnt_cir - 1].y - pt[j].y, cir[cnt_cir - 1].x - pt[j].x); + padj[j][pnum[j]++].id = cnt_cir - 1; + } + } +} + +bool get_L_to_L(int id1, int id2) +{ + int i, j, s, p, q; + point pp, qq, gg, ff, tk; + double xx, yy, fx, fy, gx, gy, px, py, qx, qy, dist, dr, alpha, cx, cy, tx, ty, theta, x1, y1, x2, y2, x3, y3, x4, y4; + inter(pt[id1].x, pt[id1].y, pt[(id1 + 1) % n].x, pt[(id1 + 1) % n].y, pt[id2].x, pt[id2].y, pt[(id2 + 1) % n].x, pt[(id2 + 1) % n].y); + if (fabs(pr - inf) < eps) + { + return false; + } + tx = pt[id1].x + pr * (pt[(id1 + 1) % n].x - pt[id1].x); + ty = pt[id1].y + pr * (pt[(id1 + 1) % n].y - pt[id1].y); + x1 = pt[id1].x; + y1 = pt[id1].y; + x2 = pt[(id1 + 1) % n].x; + y2 = pt[(id1 + 1) % n].y; + x3 = pt[id2].x; + y3 = pt[id2].y; + x4 = pt[(id2 + 1) % n].x; + y4 = pt[(id2 + 1) % n].y; + tk.x = tx; + tk.y = ty; + for (q = 0; q < 2; q++) + { + if (fabs(y2 - ty) < eps && fabs(x2 - tx) < eps) + { + ff.x = fx = x1; + ff.y = fy = y1; + gg.x = gx = x2; + gg.y = gy = y2; + } + else + { + ff.x = fx = x2; + ff.y = fy = y2; + gg.x = gx = x1; + gg.y = gy = y1; + } + if (fabs(y4 - ty) < eps && fabs(x4 - tx) < eps) + { + pp.x = px = x3; + pp.y = py = y3; + qq.x = qx = x4; + qq.y = qy = y4; + } + else + { + pp.x = px = x4; + pp.y = py = y4; + qq.x = qx = x3; + qq.y = qy = y3; + } + if (q == 1) + { + if ((pp < tk && tk < qq) || (qq < tk && tk < pp)) + { + swap(pp, qq); + swap(px, qx); + swap(py, qy); + } + else if ((ff < tk && tk < gg) || (gg < tk && tk < ff)) + { + swap(ff, gg); + swap(fx, gx); + swap(fy, gy); + } + else + { + break; + } + } + double f1, f2; + f1 = atan2(fy - ty, fx - tx); + f2 = atan2(py - ty, px - tx); + while (f1 - f2 > M_PI) + { + f1 -= 2 * M_PI; + } + while (f1 - f2 < -M_PI) + { + f1 += 2 * M_PI; + } + theta = 0.5 * (f1 + f2); + alpha = 0.5 * fabs(f1 - f2); + if (fabs(alpha) < eps) + { + return false; + } + dr = r / sin(alpha); + cir[cnt_cir].id1 = id1; + cir[cnt_cir].id2 = id2; + cir[cnt_cir].kind = 2; + cir[cnt_cir].x = cx = tx + dr * cos(theta); + cir[cnt_cir].y = cy = ty + dr * sin(theta); + dist = r * cos(alpha) / sin(alpha); + cir[cnt_cir].px = xx = tx + dist * cos(atan2(fy - ty, fx - tx)); + cir[cnt_cir].py = yy = ty + dist * sin(atan2(fy - ty, fx - tx)); + if (xx < max(x1, x2) + eps && xx > min(x1, x2) - eps && yy < max(y1, y2) + eps && yy > min(y1, y2) - eps) + { + cir[cnt_cir].qx = xx = tx + dist * cos(atan2(py - ty, px - tx)); + cir[cnt_cir].qy = yy = ty + dist * sin(atan2(py - ty, px - tx)); + if (xx < max(x3, x4) + eps && xx > min(x3, x4) - eps && yy < max(y3, y4) + eps && yy > min(y3, y4) - eps) + { + if (check(cx, cy)) + { + cnt_cir++; + i = id1; + j = id2; + adj[i][num[i]] = cir[cnt_cir - 1]; + adj[i][num[i]++].id = cnt_cir - 1; + adj[j][num[j]] = cir[cnt_cir - 1]; + adj[j][num[j]++].id = cnt_cir - 1; + } + } + } + } +} + +double cmp1(double cx, double cy, point a, point b) +{ + int i, j, s, p, q; + double theta; + theta = atan2(a.y - cy, a.x - cx) - atan2(b.y - cy, b.x - cx); + while (theta < -M_PI + eps) + { + theta += 2 * M_PI; + } + while (theta > M_PI - eps) + { + theta -= 2 * M_PI; + } + theta = fabs(theta); + return 0.5 * (theta - sin(theta)) * r * r; +} + +bool at(int kind, double cx, double cy, point a, point b) +{ + double jk; + if (kind == 0) + { + return fabs(a.x - b.x) < eps && fabs(a.y - b.y) < eps; + } + jk = (cx - a.x) * (a.x - b.x) + (cy - a.y) * (a.y - b.y); + if (fabs(jk) < eps) + { + for (int i = 0; i < n; i++) + { + double c1, c2, ru1, ru2, x1, y1, x2, y2; + x1 = pt[i].x; + y1 = pt[i].y; + x2 = pt[(i + 1) % n].x; + y2 = pt[(i + 1) % n].y; + c1 = fabs((a.x - x1) * (y2 - y1) - (a.y - y1) * (x2 - x1)); + c2 = fabs((b.x - x1) * (y2 - y1) - (b.y - y1) * (x2 - x1)); + if (c1 < eps && c2 < eps) + { + if (x1 == x2) + { + ru1 = (a.y - y1) / (y2 - y1); + ru2 = (b.y - y1) / (y2 - y1); + } + else + { + ru1 = (a.x - x1) / (x2 - x1); + ru2 = (b.x - x1) / (x2 - x1); + } + if (ru1 < 1 + eps && ru1 > -eps && ru2 < 1 + eps && ru2 > -eps) + { + return true; + } + } + } + } + return false; +} + +void dfs(int id1, int id2, int pre1, int pre2) +{ + int i, j, s, p, q, id; + double theta, parea = 0; + if (lst[id1].x < lx - eps || lst[id1].x > rx + eps || lst[id1].y < ly - eps || lst[id1].y > ry + eps) + { + no = true; + } + if (vis[id1][id2]) + { + if (!no) + { + for (i = 1; i < cnt_po - 1; i++) + { + double x1, y1, x2, y2; + x1 = poly[i].x - poly[0].x; + y1 = poly[i].y - poly[0].y; + x2 = poly[i + 1].x - poly[0].x; + y2 = poly[i + 1].y - poly[0].y; + parea += 0.5 * (x1 * y2 - x2 * y1); + } + now += fabs(parea); + if (ans < now) + { + ans = now; + } + } + return; + } + vis[id1][id2] = true; + poly[cnt_po++] = xl[id1][id2]; + for (i = 0; i < tnum[id1]; i++) + { + id = tadj[id1][i].id; + for (j = 0; j < cnt_x[id]; j++) + { + if (at(tadj[id1][i].kind, lst[id1].x, lst[id1].y, xl[id1][id2], xl[id][j]) && !(id == pre1 && j == pre2)) + { + break; + } + } + if (j < cnt_x[id]) + { + if (tadj[id1][i].kind == 0) + { + now += cmp1(lst[id1].x, lst[id1].y, xl[id1][id2], xl[id][j]); + } + dfs(id, j, id1, id2); + break; + } + } + if (i >= tnum[id1]) + { + id = id1; + j = (id2 + 1) % cnt_x[id1]; + theta = -(atan2(xl[id1][id2].y - lst[id1].y, xl[id1][id2].x - lst[id1].x) - atan2(xl[id1][j].y - lst[id1].y, xl[id1][j].x - lst[id1].x)); + if (j == 0) + { + theta += 2 * M_PI; + } + if (theta > M_PI - eps || (j == pre2 && id1 == pre1)) + { + j = (id2 - 1 + cnt_x[id1]) % cnt_x[id1]; + } + now += cmp1(lst[id1].x, lst[id1].y, xl[id1][id2], xl[id1][j]); + dfs(id1, j, id1, id2); + } +} + +int main() +{ + double ru, cx, cy, theta; + int i, j, s, p, q, id1, id2, id; + while (scanf("%d%lf", &n, &r) == 2 && (n || r)) + { + ly = lx = 1.0 / eps; + ry = rx = -1.0 / eps; + for (i = 0; i < n; i++) + { + scanf("%lf%lf", &pt[i].x, &pt[i].y); + lx = min(lx, pt[i].x); + ly = min(ly, pt[i].y); + rx = max(rx, pt[i].x); + ry = max(ry, pt[i].y); + } + memset(vis, false, sizeof(vis)); + cnt_cir = 0; + memset(num, 0, sizeof(num)); + memset(tnum, 0, sizeof(tnum)); + memset(pnum, 0, sizeof(pnum)); + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (i < j) + { + cir[cnt_cir].id1 = i; + cir[cnt_cir].id2 = j; + cir[cnt_cir].kind = 0; + get_P_to_P(i, j); + } + cir[cnt_cir].id1 = i; + cir[cnt_cir].id2 = j; + cir[cnt_cir].kind = 1; + get_P_to_L(i, j); + if (i < j) + { + cir[cnt_cir].id1 = i; + cir[cnt_cir].id2 = j; + cir[cnt_cir].kind = 2; + get_L_to_L(i, j); + } + } + } + nt = 0; + for (i = 0; i < cnt_cir; i++) + { + lst[nt++] = cir[i]; + } + sort(lst, lst + nt); + tn = 0; + memset(cnt_x, 0, sizeof(cnt_x)); + for (i = 0; i < nt; i++) + { + if (tn == 0 || lst[tn - 1] < lst[i]) + { + lst[tn++] = lst[i]; + } + xl[tn - 1][cnt_x[tn - 1]].x = lst[i].px; + xl[tn - 1][cnt_x[tn - 1]].theta = atan2(lst[i].py - lst[i].y, lst[i].px - lst[i].x); + xl[tn - 1][cnt_x[tn - 1]++].y = lst[i].py; + xl[tn - 1][cnt_x[tn - 1]].x = lst[i].qx; + xl[tn - 1][cnt_x[tn - 1]].theta = atan2(lst[i].qy - lst[i].y, lst[i].qx - lst[i].x); + xl[tn - 1][cnt_x[tn - 1]++].y = lst[i].qy; + } + nt = tn; + for (i = 0; i < cnt_cir; i++) + { + for (j = 0; j < nt; j++) + { + if (cir[i] == lst[j]) + { + pos[i] = j; + break; + } + } + } + for (i = 0; i < nt; i++) + { + sort(xl[i], xl[i] + cnt_x[i], cmp); + tn = 0; + for (j = 0; j < cnt_x[i]; j++) + { + if (tn == 0 || !(xl[i][tn - 1] == xl[i][j])) + { + xl[i][tn++] = xl[i][j]; + } + } + if (tn != 1 && xl[i][tn - 1] == xl[i][0]) + { + tn--; + } + cnt_x[i] = tn; + } + memset(mat, false, sizeof(mat)); + for (i = 0; i < n; i++) + { + sort(adj[i], adj[i] + num[i]); + for (j = 0; j < num[i] - 1; j++) + { + id1 = adj[i][j].id; + id2 = adj[i][j + 1].id; + for (s = j + 1; s < num[i]; s++) + { + id2 = adj[i][s].id; + if (!inter(cir[id1].x, cir[id1].y, cir[id2].x, cir[id2].y, pt[i].x, pt[i].y, pt[(i + 1) % n].x, pt[(i + 1) % n].y)) + { + break; + } + } + if (s >= num[i]) + { + continue; + } + id2 = adj[i][s].id; + cx = 0.5 * (cir[id1].x + cir[id2].x); + cy = 0.5 * (cir[id1].y + cir[id2].y); + if (pos[id1] != pos[id2] && !mat[pos[id1]][pos[id2]] && check(cx, cy)) + { + id1 = pos[id1]; + id2 = pos[id2]; + mat[id1][id2] = true; + tadj[id1][tnum[id1]].kind = 1; + tadj[id1][tnum[id1]++].id = id2; + tadj[id2][tnum[id2]].kind = 1; + tadj[id2][tnum[id2]++].id = id1; + } + } + sort(padj[i], padj[i] + pnum[i], cmp); + for (j = 0; j < pnum[i]; j++) + { + double theta1 = padj[i][j].theta, theta2 = padj[i][(j + 1) % pnum[i]].theta; + if (j == pnum[i] - 1) + { + theta1 -= 2 * M_PI; + } + id1 = padj[i][j].id; + id2 = padj[i][(j + 1) % pnum[i]].id; + theta = 0.5 * (theta1 + theta2); + cx = pt[i].x + r * cos(theta); + cy = pt[i].y + r * sin(theta); + if (pos[id1] != pos[id2] && !mat[pos[id1]][pos[id2]] && check(cx, cy)) + { + id1 = pos[id1]; + id2 = pos[id2]; + mat[id1][id2] = true; + tadj[id1][tnum[id1]].kind = 0; + tadj[id1][tnum[id1]++].id = id2; + tadj[id2][tnum[id2]].kind = 0; + tadj[id2][tnum[id2]++].id = id1; + } + } + } + memset(vis, false, sizeof(vis)); + ans = -1; + for (i = 0; i < nt; i++) + { + for (j = 0; j < cnt_x[i]; j++) + { + if (vis[i][j]) + { + continue; + } + cnt_po = no = 0; + now = 0.0; + dfs(i, j, -1, -1); + } + } + if (ans < 0) + { + printf("Impossible\n"); + } + else + { + printf("%.2f\n", ans); + } + } + return 0; +} diff --git a/12315.cpp b/12315.cpp new file mode 100644 index 0000000..b6a2902 --- /dev/null +++ b/12315.cpp @@ -0,0 +1,270 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl +#define dassert(x) \ + if (false) \ + { \ + assert(x); \ + } + +struct node +{ + string label; + vector sons; + node() {} + node(const string &lbl) : label(lbl) {} + bool isVariable() + { + dassert(label.size() > 0); + return sons.size() == 0 and isupper(label[0]); + } + bool isConstant() + { + dassert(label.size() > 0); + return sons.size() == 0 and islower(label[0]); + } + bool isFunction() + { + dassert(label.size() > 0); + if (sons.size() == 0) + { + dassert(islower(label[0])); + } + return sons.size() > 0; + } +}; + +node parse(const string &s, int i, int j) +{ + if (s[j] != ')')// constant or variable + { + return node(s.substr(i, j - i + 1)); + } + dassert(s[j] == ')'); + int k = i; + while (s[k] != '(') + { + k++; + } + node ans = node(s.substr(i, k - i)); + // split string from [k+1, j-1] + int balance = 0; + for (i = k + 1; i < j;) + { + k = i; + while (k <= j) + { + if (s[k] == ')') + { + balance--; + } + if (s[k] == '(') + { + balance++; + } + if (k == j or s[k] == ',' and balance == 0) + { + node son = parse(s, i, k - 1); + ans.sons.push_back(son); + break; + } + k++; + } + i = k + 1; + } + dassert(i == j + 1); + return ans; +} + +void print(const node &t, int depth = 0) +{ + for (int i = 0; i < depth; ++i) + { + printf(" "); + } + printf("%s\n", t.label.c_str()); + for (int i = 0; i < t.sons.size(); ++i) + { + print(t.sons[i], depth + 1); + } +} + +map mapping; + +bool contains(node r, const string &variable, int depth = 0) +{ + while (r.isVariable() and mapping.count(r.label) > 0) + { + r = mapping[r.label]; + } + if (r.label == variable) + { + return true; + } + for (int i = 0; i < r.sons.size(); ++i) + { + if (contains(r.sons[i], variable, depth + 1)) + { + return true; + } + } + return false; +} + +bool unify(const node &A, const node &B, int depth = 0) +{ + //if (depth > 4) return false; // Remove this and you'll get a runtime error. Change for depth > 4 and you'll get a runtime error too. + node a = A, b = B; + while (a.isVariable() and mapping.count(a.label) > 0) + { + a = mapping[a.label]; + } + while (b.isVariable() and mapping.count(b.label) > 0) + { + b = mapping[b.label]; + } + if (a.isConstant()) + { + if (b.isFunction()) + { + return false; + } + if (b.isConstant()) + { + if (a.label == b.label) + { + return true; + } + else + { + return false; + } + } + } + if (b.isConstant()) + { + if (a.isFunction()) + { + return false; + } + if (a.isConstant()) + { + if (a.label == b.label) + { + return true; + } + else + { + return false; + } + } + } + if (a.isVariable() and b.isVariable()) + { + if (a.label == b.label) + { + return true; + } + else + { + mapping[a.label] = b; + return true; + } + } + if (a.isVariable()) + { + dassert(!b.isVariable()); + if (contains(b, a.label)) + { + return false; + } + else + { + mapping[a.label] = b; + return true; + } + } + if (b.isVariable()) + { + dassert(!a.isVariable()); + if (contains(a, b.label)) + { + return false; + } + else + { + mapping[b.label] = a; + return true; + } + } + // both are functions + dassert(a.isFunction() and b.isFunction()); + if (a.label != b.label) + { + return false; + } + if (a.sons.size() != b.sons.size()) + { + return false; + } + for (int i = 0; i < a.sons.size(); ++i) + { + bool r = unify(a.sons[i], b.sons[i], depth + 1); + if (!r) + { + return false; + } + } + return true; +} + +int main() +{ + string s = "f(X,g(c))"; + // s = "f(f(Y),Z)"; + // s = "f(c,g(Y,d))"; + // s = "f(f(a),a)"; + // s = "f(f(f(X,a),a),a)"; + // s = "f(f(f(f(f(f(f(f(f(f(f(X)),d)))),e,f,g(c,c))))),a,a,a,a,a)"; + // s = "f(f(f(f(f(f(f(f(f(f(f(X)),d)))),e,f,g(c,c))))),a,a,a,a,a,f(f(f(f(f(a,n,d,y,m,e,j,i,a,f(f(f(f(f(f(X))))))))))))"; + // s = "X"; + // s = "a"; + // s = "x"; + //node t = parse(s, 0, s.size() - 1); + //print(t); + string name; + int n; + while (cin >> name >> n) + { + if (name == "END" and n == 0) + { + break; + } + vector trees(n); + for (int i = 0; i < n; ++i) + { + string expression; + cin >> expression; + trees[i] = parse(expression, 0, expression.size() - 1); + } + mapping.clear(); + bool works = true; + for (int i = 0; i < n - 1 and works; ++i) + { + works &= unify(trees[i], trees[i + 1]); + } + if (works) + { + printf("analysis inconclusive on %s\n", name.c_str()); + } + else + { + printf("%s is a Starflyer agent\n", name.c_str()); + } + } + return 0; +} diff --git a/12317.cpp b/12317.cpp new file mode 100644 index 0000000..5765836 --- /dev/null +++ b/12317.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int NN = 1 << 16; +int d[NN], mask[128], q[NN]; +int head, tail; + +void push(int x, int l) +{ + d[x] = l; + q[tail++] = x; +} + +int main() +{ + for (int m, n; scanf("%d %d", &m, &n) == 2 && m;) + { + for (int i = 0; i < NN; ++i) + { + d[i] = 0; + } + for (int i = 0, t, z; i < m; ++i) + { + mask[i] = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d", &z); + mask[i] |= 1 << (z - 1); + } + } + head = tail = 0; + push(0, 0); + while (head != tail) + { + int at = q[head++], level = d[at]; + int new_mask; + for (int i = 0; i < m; ++i) + { + new_mask = at | mask[i]; + if (!d[new_mask]) + { + push(new_mask, level + 1); + } + } + } + for (int i = 0, t, z; i < n; ++i) + { + mask[m] = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d", &z); + mask[m] |= 1 << (z - 1); + } + printf("%d%c", d[mask[m]], i == n - 1 ? 10 : 32); + } + } + return 0; +} diff --git a/12318.cpp b/12318.cpp new file mode 100644 index 0000000..d76e603 --- /dev/null +++ b/12318.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +typedef long long int LL; +const int MAXN = 10000048; +LL a[16], N; +int v[MAXN], K, M; + +LL p(LL x) +{ + LL r = 0, y = 1; + FOR(i, K) + { + if (a[i]) + { + r = (r + a[i] * y) % N; + } + y = (y * x) % N; + } + return r; +} + +int main() +{ + FOR(i, MAXN) + v[i] = -1; + for (int T = 1; scanf("%lld%d", &N, &M) && (N || M); ++T) + { + int ans = 0; + scanf("%d", &K); + ++K; + ++M; + ++N; + FOR(i, K) + scanf("%lld", a + i); + FOR(i, M) + { + LL r = p(i); + ans += v[r] != T; + v[r] = T; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12319.cpp b/12319.cpp new file mode 100644 index 0000000..da15a3e --- /dev/null +++ b/12319.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +const int INF = 1 << 20, NN = 128; +int m1[NN][NN], m2[NN][NN]; +char line[65536]; + +int min(int a, int b) +{ + return a < b ? a : b; +} + +void read_graph(int m[128][128], int n) +{ + FOR(i, n) + { + gets(line); + for (char *z = strtok(line, " "); z != NULL; z = strtok(NULL, " ")) + { + m[i][atoi(z) - 1] = 1; + } + } + FOR(k, n) + FOR(i, n) FOR(j, n) m[i][j] = min(m[i][j], m[i][k] + m[k][j]); +} + +int main() +{ + for (int A, B, n; scanf("%d\n", &n) == 1 && n;) + { + FOR(i, n) + FOR(j, n) m1[i][j] = m2[i][j] = INF; + read_graph(m1, n); + read_graph(m2, n); + scanf("%d %d", &A, &B); + bool valid = true; + FOR(i, n) + FOR(j, n) valid &= m2[i][j] <= A * m1[i][j] + B; + puts(valid ? "Yes" : "No"); + } + return 0; +} diff --git a/1232.cpp b/1232.cpp new file mode 100644 index 0000000..01255f1 --- /dev/null +++ b/1232.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +struct Node +{ + int a, b, h; + bool leaf; + Node() {} + Node(int a, int b, int h, bool leaf = true) : a(a), b(b), h(h), leaf(leaf) {} +}; + +Node H[5000005]; +inline int left(int i) +{ + return 2 * i; +} +inline int right(int i) +{ + return 2 * i + 1; +} + +inline void cut(int v, int x) +{ + H[left(v)] = Node(H[v].a, x, H[v].h); + H[right(v)] = Node(x, H[v].b, H[v].h); + H[v].leaf = false; +} + +int dfs(int v, int a, int b, int h) +{ + a = max(a, H[v].a); + b = min(b, H[v].b); + if (b <= a) + { + return 0; + } + if (!H[v].leaf) + { + return dfs(left(v), a, b, h) + dfs(right(v), a, b, h); + } + if (H[v].h > h) + { + return 0; + } + if (H[v].a < a) + { + return cut(v, a), dfs(v, a, b, h); + } + if (b < H[v].b) + { + return cut(v, b), dfs(v, a, b, h); + } + H[v].h = h; + return b - a; +} + +int main() +{ + int n, _; + scanf("%d", &_); + while (scanf("%d",&n), n) + { + H[1] = Node(0, 100000, 0); + int sum = 0; + while (n--) + { + int a, b, h; + scanf("%d%d%d", &a,&b,&h); + sum += dfs(1, a, b, h); + } + printf("%d\n", sum); + } + return 0; +} diff --git a/12320.cpp b/12320.cpp new file mode 100644 index 0000000..5495c70 --- /dev/null +++ b/12320.cpp @@ -0,0 +1,243 @@ +#include + +using namespace std; + +template +string toStr(const T &x) +{ + stringstream s; + s << x; + return s.str(); +} +template +int toInt(const T &x) +{ + stringstream s; + s << x; + int r; + s >> r; + return r; +} + +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define D(x) cout << #x " = " << (x) << endl + +namespace IO +{ +#define MAXBUFF (1 << 20) +char buffer[MAXBUFF], *p = buffer + MAXBUFF; +char buffer2[MAXBUFF], *p2 = buffer2; + +inline char read_char() +{ + if (p == buffer + MAXBUFF) + { + fread(buffer, 1, MAXBUFF, stdin); + p = buffer; + } + return *p++; +} + +inline int read_int() +{ + char c = read_char(); + while (!isdigit(c) and c != '-') + { + c = read_char(); + } + int sign = c == '-' ? -1 : +1; + if (c == '-') + { + c = read_char(); + } + int ret = c - '0'; + while (isdigit(c = read_char())) + { + ret = ret * 10 + c - '0'; + } + return ret * sign; +} + +void flush() +{ + fwrite(buffer2, 1, p2 - buffer2, stdout); + p2 = buffer2; +} + +inline void write(char c) +{ + if (p2 == buffer2 + MAXBUFF) + { + fwrite(buffer2, 1, MAXBUFF, stdout); + p2 = buffer2; + } + *p2++ = c; +} +} + +const double EPS = 1e-10; + +int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} + +struct point +{ + double x, y, z; + point() {} + point(double x, double y, double z) : x(x), y(y), z(z) {} +}; + +int R[2], V[2]; +vector P[2]; +double D[2][105]; + +inline double dist(const point &a, const point &b) +{ + double dx = b.x - a.x, dy = b.y - a.y, dz = b.z - a.z; + return sqrt(dx * dx + dy * dy + dz * dz); +} + +point positionAt(int p, double t) +{ + double d = V[p] * t; + int i = upper_bound(D[p], D[p] + P[p].size(), d) - D[p] - 1; + assert(i >= 0); + double s = D[p][i]; + double distance = dist(P[p][i], P[p][i + 1]); + //assert(cmp(s + distance, d) >= 0); + point ans = P[p][i]; + if (cmp(distance, 0.0) > 0) + { + double dx = P[p][i + 1].x - P[p][i].x; + double dy = P[p][i + 1].y - P[p][i].y; + double dz = P[p][i + 1].z - P[p][i].z; + ans.x += (d - s) * dx / distance; + ans.y += (d - s) * dy / distance; + ans.z += (d - s) * dz / distance; + } + //assert(cmp(s + dist(P[p][i], ans), d) == 0); + return ans; +} + +double distanceAtTime(double t) +{ + point p0 = positionAt(0, t); + point p1 = positionAt(1, t); + return dist(p0, p1); +} + +double touchAtTime(double t) +{ + return cmp(distanceAtTime(t), 0.0 + R[0] + R[1]) <= 0; +} + +double findClosestTime(double left, double right) +{ + // double bestTime = -1, bestDistance = 1e200; + // for (double t = left; t <= right; t += 0.5) { + // double d = distanceAtTime(t); + // printf("At time %lf, distance is %lf\n", t, d); + // if (cmp(d, bestDistance) < 0) { + // bestDistance = d; + // bestTime = t; + // } + // } + while (cmp(left, right) < 0) + { + double t1 = (2 * left + right) / 3; + double t2 = (2 * right + left) / 3; + double d1 = distanceAtTime(t1); + double d2 = distanceAtTime(t2); + if (d1 > d2) + { + left = t1; + } + else + { + right = t2; + } + } + double ans = (left + right) / 2; + //printf("Best is at time %lf, where distance is %lf\n", ans, distanceAtTime(ans)); + return ans; +} + +int main() +{ + int casos = IO::read_int(); + while (casos--) + { + for (int p = 0; p < 2; ++p) + { + int k; + R[p] = IO::read_int(), V[p] = IO::read_int(), k = IO::read_int(); + P[p].resize(k); + for (int i = 0; i < k; ++i) + { + P[p][i].x = IO::read_int(), P[p][i].y = IO::read_int(), P[p][i].z = IO::read_int(); + } + P[p].push_back(point(0, 0, 0)); + } + double totalTime = 1e100; + for (int p = 0; p < 2; ++p) + { + D[p][0] = 0.0; + for (int i = 1; i < P[p].size(); ++i) + { + D[p][i] = D[p][i - 1] + dist(P[p][i - 1], P[p][i]); + } + totalTime = min(totalTime, D[p][P[p].size() - 1] / V[p]); + } + // D(totalTime); + // point p1 = positionAt(0, totalTime); + // point p2 = positionAt(1, totalTime); + // printf("<%lf, %lf, %lf>\n", p1.x, p1.y, p1.z); + // printf("<%lf, %lf, %lf>\n", p2.x, p2.y, p2.z); + vector times; + for (int p = 0; p < 2; ++p) + { + double d = 0; + times.push_back(0.0); + for (int i = 0; i < P[p].size() - 1; ++i) + { + d += dist(P[p][i], P[p][i + 1]); + double t = d / V[p]; + if (cmp(t, totalTime) <= 0) + { + times.push_back(d / V[p]); + } + } + } + sort(times.begin(), times.end()); + //For(i, 0, times.size() - 1) assert(cmp(times[i], times[i + 1]) <= 0); + // for (int i = 0; i < times.size(); ++i) { + // printf("%lf ", times[i]); + // } + // puts(""); + int ans = 0; + for (int i = 0; i < times.size() - 1; ++i) + { + //printf("\nInterval from time (%lf, %lf):\n", times[i], times[i+1]); + const double &t = times[i]; + if (touchAtTime(t)) + { + //printf("They touch at time %lf, continuing.\n", t); + continue; + } + double closestTime = findClosestTime(t, times[i + 1]); + if (touchAtTime(closestTime)) + { + ans++; + } + } + if (touchAtTime(0.0)) + { + ans++; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12321.cpp b/12321.cpp new file mode 100644 index 0000000..e9fa5ec --- /dev/null +++ b/12321.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +/* {{{ FAST integer input */ +#define X10(n) ((n << 3) + (n << 1)) +#define RI readint +const int MAXR = 65536; +char buf[MAXR], *lim = buf + MAXR - 1, *now = lim + 1; +bool adapt()// Returns true if there is a number waiting to be read, false otherwise +{ + while (now <= lim && !isdigit(*now)) + { + ++now; + } + if (now > lim) + { + int r = fread(buf, 1, MAXR - 1, stdin); + buf[r] = 0; + lim = buf + r - 1; + if (r == MAXR - 1) + { + while (isdigit(*lim)) + { + ungetc(*lim--, stdin); + } + if (*lim == '-') + { + ungetc(*lim--, stdin); + } + } + now = buf; + } + while (now <= lim && !isdigit(*now)) + { + ++now; + } + return now <= lim; +} +bool readint(int &n)// Returns true on success, false on failure +{ + if (!adapt()) + { + return false; + } + bool ngtv = *(now - 1) == '-'; + for (n = 0; isdigit(*now); n = X10(n) + *now++ - '0') + ; + if (ngtv) + { + n = -n; + } + return true; +} +/* }}} end FAST integer input */ + +struct interval +{ + int b, e; + bool operator<(const interval &i) const + { + return b < i.b || (b == i.b && e < i.e); + } +} d[10048]; +int main() +{ + for (int n, z; (RI(z) + RI(n)) == 2 && (n || z);) + { + for (int a, b, i = 0; i < n; ++i) + { + RI(a) + , RI(b); + d[i].b = a - b; + d[i].e = a + b; + } + sort(d, d + n); + int at = 0, discard = n, idx, newat = 0; + for (int i = 0, j; i < n;) + { + idx = -1; + for (j = i; d[j].b <= at && j < n; ++j) + if (d[j].e >= newat) + { + newat = d[j].e; + idx = j; + } + --discard; + at = newat; + if (idx == -1 || at >= z) + { + break; + } + i = j; + } + printf("%d\n", at >= z ? discard : -1); + } + return 0; +} diff --git a/12322.cpp b/12322.cpp new file mode 100644 index 0000000..c2f8ab1 --- /dev/null +++ b/12322.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +typedef pair interval; + +const double eps = 1e-9; + +const int MAXN = 1001; + +interval intervals[MAXN]; +int B; + +void delete_redundant() +{ + bitset deleted; + for (int i = 0; i < B; ++i) + { + for (int j = 0; j < B; ++j) + { + if (i == j) + { + continue; + } + // delete interval j if interval i is completely inside it + if (intervals[j].first < intervals[i].first - eps and intervals[i].second + eps < intervals[j].second) + { + deleted[j] = true; + } + } + } + int k = 0; + for (int i = 0; i < B; ++i) + { + if (!deleted[i]) + { + intervals[k++] = intervals[i]; + } + } + B = k; +} + +int main() +{ + while (cin >> B) + { + if (B == 0) + { + break; + } + for (int i = 0; i < B; ++i) + { + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + double t1 = atan2(y1, x1), t2 = atan2(y2, x2); + intervals[i] = make_pair(min(t1, t2), max(t1, t2)); + } + delete_redundant(); + sort(intervals, intervals + B); + int ans = 0; + for (int i = 0; i < B;) + { + ans++; + double pick = intervals[i].second; + while (i < B and intervals[i].first - eps < pick) + { + i++; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12323.cpp b/12323.cpp new file mode 100644 index 0000000..08ae08c --- /dev/null +++ b/12323.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << x << endl + +typedef pair point; + +const int MAXN = 10000; +const int MAXR = 101; +const int MAXW = 720; + +int mat[MAXR][MAXW]; +int sum[MAXR][MAXW]; + +void precompute() +{ + for (int i = 0; i < MAXR; ++i) + { + for (int j = 0; j < MAXW; ++j) + { + sum[i][j] = mat[i][j]; + if (i > 0) + { + sum[i][j] += sum[i - 1][j]; + } + if (j > 0) + { + sum[i][j] += sum[i][j - 1]; + } + if (i > 0 and j > 0) + { + sum[i][j] -= sum[i - 1][j - 1]; + } + } + } +} + +int rectangle(int r1, int c1, int r2, int c2) +{ + int ans = sum[r2][c2]; + if (r1 > 0) + { + ans -= sum[r1 - 1][c2]; + } + if (c1 > 0) + { + ans -= sum[r2][c1 - 1]; + } + if (r1 > 0 and c1 > 0) + { + ans += sum[r1 - 1][c1 - 1]; + } + return ans; +} + +void solve(int height, int width) +{ + int ans = 0; + for (int i = 0; i + height < MAXR; ++i) + { + for (int j = 0; j + width < MAXW; ++j) + { + int option = rectangle(i, j, i + height, j + width); + if (option > ans) + { + ans = option; + } + } + } + printf("%d\n", ans); +} + +int main() +{ + int N, R; + while (scanf("%d %d", &N, &R) == 2) + { + if (N == 0 and R == 0) + { + break; + } + memset(mat, 0, sizeof mat); + for (int i = 0; i < N; ++i) + { + int distance, angle; + scanf("%d %d", &distance, &angle); + mat[distance][angle]++; + mat[distance][angle + 360]++; + } + precompute(); + int E; + scanf("%d", &E); + while (E--) + { + int distance, angle; + scanf("%d %d", &distance, &angle); + solve(distance - 1, angle); + } + } + return 0; +} diff --git a/12324.cpp b/12324.cpp new file mode 100644 index 0000000..504aa68 --- /dev/null +++ b/12324.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define FORI(i, s, n) for (int i = s; i < n; ++i) +#define T(i) (i & 0xFFF00000) +#define V(i) (i & 0x000FFFFF) + +const int MAXB = 5051, MAXN = 101; +int b[MAXN], dp[MAXN][MAXB], t[MAXN], T = 1 << 20; + +int min(int a, int b) +{ + return a < b ? a : b; +} + +void set_min(int &x, int nv) +{ + if (T(x) == T) + { + x = min(x, nv); + } + else + { + x = nv; + } +} + +int main() +{ + FOR(j, MAXB) + dp[0][j] = dp[1][j] = -1; + for (int n; scanf("%d", &n) == 1 && n; T += (1 << 20)) + { + int ans = 1 << 20, high = 1, low = 0; + FOR(i, n) + scanf("%d %d", t + i, b + i); + dp[0][0] = T; + FOR(i, n) + { + FORI(j, low, high) + if (T(dp[i][j]) == T) + { + set_min(dp[i + 1][j + b[i]], dp[i][j] + t[i]); + if (j) + { + set_min(dp[i + 1][j + b[i] - 1], dp[i][j] + (t[i] >> 1)); + } + } + low += b[i] - (low > 0); + high += b[i]; + } + FOR(j, high) + if (T(dp[n][j]) == T) + { + ans = min(ans, V(dp[n][j])); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12325.cpp b/12325.cpp new file mode 100644 index 0000000..0a46081 --- /dev/null +++ b/12325.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int c1, v1, c2, v2, ct; +long long int ans; + +int main() +{ + int _, t; + scanf("%d", &_); + for (t = 1; t <= _; t++) + { + scanf("%d%d%d%d%d", &ct, &c1, &v1, &c2, &v2); + if (c1 > c2) + { + swap(c1, c2); + swap(v1, v2); + } + ans = 0; + if (ct / c2 >= 50000) + { + for (long long int i = 0; i <= c1; i++) + { + ans = max(ans, v2 * i + (ct - c2 * i) / c1 * v1); + } + for (long long int i = 0; i <= c2; i++) + { + ans = max(ans, v1 * i + (ct - c1 * i) / c2 * v2); + } + } + else + { + for (long long int i = 0; c2 * i <= ct; i++) + { + ans = max(ans, v2 * i + (ct - c2 * i) / c1 * v1); + } + } + printf("Case #%d: %lld\n", t, ans); + } + return 0; +} diff --git a/12326.cpp b/12326.cpp new file mode 100644 index 0000000..20d13cd --- /dev/null +++ b/12326.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int ans[30]; + +int main() +{ + ans[1] = 1; + ans[2] = 1; + ans[3] = 1; + ans[4] = 4; + ans[5] = 6; + ans[6] = 19; + ans[7] = 43; + ans[8] = 120; + ans[9] = 307; + ans[10] = 866; + ans[11] = 2336; + ans[12] = 6588; + ans[13] = 18373; + ans[14] = 52119; + ans[15] = 147700; + ans[16] = 422016; + int ntest, n; + scanf("%d", &ntest); + for (int test = 1; test <= ntest; test++) + { + scanf("%d", &n); + printf("Case #%d: %d\n", test, ans[n]); + } + return 0; +} diff --git a/12327.cpp b/12327.cpp new file mode 100644 index 0000000..a9965c8 --- /dev/null +++ b/12327.cpp @@ -0,0 +1,252 @@ +#include + +using namespace std; + +typedef long long int LL; + +#define ran 65536 +#define modulo 50000000001507329LL +#define primitive 3 + +int base, bound, m, n, a[40000]; +LL pri, invpri, invn, e[6][ran], f[6][ran], ans[ran]; + +// return (a*b)%p +LL mulmod(LL a, LL b, LL p) +{ + LL y = (LL)((double)a * (double)b / p + 0.5); + LL r = (a * b - y * p) % p; + if (r < 0) + { + r += p; + } + return r; +} + +LL powmod(LL a, LL b, LL p) +{ + LL r = 1; + while (b) + { + if (b & 1) + { + r = mulmod(r, a, p); + } + if (b /= 2) + { + a = mulmod(a, a, p); + } + } + return r; +} + +void fft(int n, LL root, LL a[]) +{ + for (int m = n; m >= 2; m >>= 1) + { + int mh = m >> 1; + LL w = 1; + for (int i = 0; i < mh; i++) + { + for (int j = i; j < n; j += m) + { + int k = j + mh; + LL x = a[j] - a[k]; + if (x < 0) + { + x += modulo; + } + a[j] += a[k]; + if (a[j] >= modulo) + { + a[j] -= modulo; + } + a[k] = mulmod(w, x, modulo); + } + w = mulmod(w, root, modulo); + } + root = mulmod(root, root, modulo); + } + int i = 0; + for (int j = 1; j < n - 1; j++) + { + for (int k = n >> 1; k > (i ^= k); k >>= 1) + ; + if (j < i) + { + swap(a[i], a[j]); + } + } +} + +int main() +{ + LL inv24 = powmod(24, modulo - 2, modulo); + LL inv120 = powmod(120, modulo - 2, modulo); + int t, T, __; + scanf("%d", &__); + for (int _ = 1; _ <= __; _++) + { + scanf("%d%d", &m, &t); + base = 10001; + for (int i = 0; i < m; i++) + { + scanf("%d", &a[i]); + base = min(base, a[i]); + } + printf("Case #%d:\n", _); + if (t == 1) + { + sort(a, a + m); + for (int i = 0; i < m; i++) + { + printf("%d: 1\n", a[i]); + } + puts(""); + continue; + } + for (int i = 0; i < m; i++) + { + a[i] -= base; + } + bound = 1; + for (int i = 0; i < m; i++) + { + bound = max(bound, a[i]); + } + bound *= t; + for (n = 1; n <= bound; n *= 2) + ; + pri = powmod(primitive, modulo / n, modulo); + invpri = powmod(pri, modulo - 2, modulo); + invn = powmod(n, modulo - 2, modulo); + for (int i = 1; i < t; i++) + { + memset(e[i], 0, sizeof(LL) * n); + for (int j = 0; j < m; j++) + { + e[i][a[j] * i] = 1; + } + fft(n, pri, e[i]); + } + if (t == 2) + { + T = 1; + for (int i = 0; i < n; i++) + { + f[0][i] = mulmod(e[1][i], e[1][i], modulo); + } + } + if (t == 3) + { + T = 2; + for (int i = 0; i < n; i++) + { + f[0][i] = mulmod(mulmod(e[1][i], e[1][i], modulo), e[1][i], modulo); + f[1][i] = mulmod(e[2][i], e[1][i], modulo); + } + } + if (t == 4) + { + T = 4; + for (int i = 0; i < n; i++) + { + f[0][i] = mulmod(e[1][i], e[1][i], modulo); + f[0][i] = mulmod(f[0][i], f[0][i], modulo); + f[1][i] = mulmod(mulmod(e[1][i], e[1][i], modulo), e[2][i], modulo); + f[2][i] = mulmod(e[2][i], e[2][i], modulo); + f[3][i] = mulmod(e[3][i], e[1][i], modulo); + } + } + if (t == 5) + { + T = 6; + for (int i = 0; i < n; i++) + { + f[0][i] = mulmod(e[1][i], e[1][i], modulo); + f[0][i] = mulmod(f[0][i], f[0][i], modulo); + f[0][i] = mulmod(f[0][i], e[1][i], modulo); + f[1][i] = mulmod(mulmod(e[1][i], e[1][i], modulo), + mulmod(e[1][i], e[2][i], modulo), modulo); + f[2][i] = mulmod(mulmod(e[1][i], e[1][i], modulo), e[3][i], modulo); + f[3][i] = mulmod(mulmod(e[2][i], e[2][i], modulo), e[1][i], modulo); + f[4][i] = mulmod(e[4][i], e[1][i], modulo); + f[5][i] = mulmod(e[2][i], e[3][i], modulo); + } + } + for (int j = 0; j < T; j++) + { + fft(n, invpri, f[j]); + } + for (int j = 0; j < T; j++) + for (int i = 0; i < n; i++) + { + f[j][i] = mulmod(f[j][i], invn, modulo); + } + if (t == 2) + { + for (int i = 0; i < n; i++) + { + ans[i] = f[0][i]; + } + for (int i = 0; i < m; i++) + { + ans[a[i] * 2]--; + } + for (int i = 0; i < n; i++) + { + ans[i] /= 2; + } + } + if (t == 3) + { + for (int i = 0; i < n; i++) + { + ans[i] = f[0][i] - f[1][i] * 3; + } + for (int i = 0; i < m; i++) + { + ans[a[i] * 3] += 2; + } + for (int i = 0; i < n; i++) + { + ans[i] /= 6; + } + } + if (t == 4) + { + for (int i = 0; i < n; i++) + { + ans[i] = f[0][i] - f[1][i] * 6 + f[2][i] * 3 + f[3][i] * 8; + } + for (int i = 0; i < m; i++) + { + ans[a[i] * 4] -= 6; + } + for (int i = 0; i < n; i++) + { + ans[i] = mulmod(ans[i], inv24, modulo); + } + } + if (t == 5) + { + for (int i = 0; i < n; i++) + ans[i] = f[0][i] - f[1][i] * 10 + f[2][i] * 20 + f[3][i] * 15 - f[4][i] * 30 - f[5][i] * 20; + for (int i = 0; i < m; i++) + { + ans[a[i] * 5] += 24; + } + for (int i = 0; i < n; i++) + { + ans[i] = mulmod(ans[i], inv120, modulo); + } + } + for (int i = 0; i < n; i++) + if (ans[i] > 0) + { + printf("%d: %lld\n", i + base * t, ans[i]); + } + puts(""); + } + return 0; +} diff --git a/12328.cpp b/12328.cpp new file mode 100644 index 0000000..faf68d8 --- /dev/null +++ b/12328.cpp @@ -0,0 +1,567 @@ +#include + +using namespace std; + +#define M 110 +#define MOD 1000003 +#define MAX_BOUND 1100 +#define MAX_MINE 1510 +#define MAX_BOUND_LEN 2800 +#define safe_sight 4 + +class partial_state_type +{ +public: + vector> cnt; +}; +partial_state_type partial_state[MAX_BOUND]; + +const int hh[8] = {0, 1, 0, -1, 1, -1, 1, -1}; +const int ll[8] = {1, 0, -1, 0, 1, 1, -1, -1}; + +int n, m, T, total_state, data[M][M]; +bool visited[M][M]; +int added_unrevealed[M][M]; +vector> around, numbers; + +bool touch_bound_flag; +int dfs_timer; + +int global_tag, top, board[M][M]; +pair stk[M * M]; + +int dp[MAX_BOUND_LEN][MAX_MINE][16]; +int lmt[MAX_BOUND_LEN], cnter[M][M], total_inner = 0; +int cnt_should_check[MAX_BOUND_LEN]; +pair should_check[MAX_BOUND_LEN][10]; + +int find_dep_counter = 0, dep_counter[M][M]; + +long long global_dp[MAX_MINE]; +int CC[10001][1001]; + +inline void read_data() +{ + cin >> n >> m >> T; + int i, j; + char ch; + memset(data, -1, sizeof(data)); + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + { + cin >> ch; + data[i][j] = ch == '.' ? -1 : (ch - '0'); + } +} + +inline bool in_bound(int a, int b) +{ + return !(a < 1 || a > n || b < 1 || b > m); +} + +inline void dfs_extract_field_bound(int a, int b) +{ + dfs_timer++; + visited[a][b] = true; + numbers.push_back(make_pair(a, b)); + if (a == 1 || a == n || b == 1 || b == m) + touch_bound_flag = true; + int i, tempa, tempb; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (in_bound(tempa, tempb) && data[tempa][tempb] == -1 && !added_unrevealed[tempa][tempb]) + { + added_unrevealed[tempa][tempb] = dfs_timer; + around.push_back(make_pair(tempa, tempb)); + } + } + for (i = 0; i < 4; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (in_bound(tempa, tempb) && data[tempa][tempb] > 0 && data[tempa][tempb] < 9 && !visited[tempa][tempb]) + dfs_extract_field_bound(tempa, tempb); + } +} + +inline bool cmp(pair a, pair b) +{ + if (a.first != b.first) + return a.first < b.first; + else + return a.second < b.second; +} + +//int __builtin_popcount (uint x) +inline int pop_count(int s) +{ + int i, res = 0; + for (i = 0; i < safe_sight; i++) + if (s & (1 << i)) + res++; + return res; +} +inline int pop_count(int s, int l) +{ + int i, res = 0; + for (i = 0; i < l; i++) + if (s & (1 << i)) + res++; + return res; +} + +inline void clear_around(pair p) +{ + int i, a = p.first, b = p.second; + int tempa, tempb; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (!in_bound(tempa, tempb)) + continue; + cnter[tempa][tempb] = 0; + } +} + +inline void set_around(pair p) +{ + int i, a = p.first, b = p.second; + int tempa, tempb; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (!in_bound(tempa, tempb)) + continue; + cnter[tempa][tempb]++; + } +} + +inline bool check_around(pair p) +{ + int i, a = p.first, b = p.second; + int tempa, tempb; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (!in_bound(tempa, tempb)) + continue; + if (data[tempa][tempb] > 0 && data[tempa][tempb] < 9) + if (data[tempa][tempb] != cnter[tempa][tempb]) + return false; + } + return true; +} + +inline bool ok(int *who, int *chosen, int cc, pair *dep, int cnt_dep) +{ + int i, j; + int a, b, tempa, tempb; + for (i = 0; i < cnt_dep; i++) + cnter[dep[i].first][dep[i].second] = 0; + for (i = 0; i < cc; i++) + if (chosen[i] == 1) + { + a = stk[who[i]].first; + b = stk[who[i]].second; + for (j = 0; j < 8; j++) + { + tempa = a + hh[j]; + tempb = b + ll[j]; + cnter[tempa][tempb]++; + } + } + for (i = 0; i < cnt_dep; i++) + if (cnter[dep[i].first][dep[i].second] != data[dep[i].first][dep[i].second]) + return false; + return true; +} + +inline bool check_included(int a, int b, int *who, int cc) +{ + int i; + for (i = 0; i < cc; i++) + if (a == stk[who[i]].first && b == stk[who[i]].second) + return true; + return false; +} + +inline bool covered(int a, int b, int *who, int cc) +{ + int i, tempa, tempb; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (in_bound(tempa, tempb) && data[tempa][tempb] == -1) + if (!check_included(tempa, tempb, who, cc)) + return false; + } + return true; +} + +void find_dep(int *who, int cc, pair *dep, int &cnt_dep) +{ + int a, b, tempa, tempb, i, j; + cnt_dep = 0; + for (i = 0; i < cc; i++) + { + a = stk[who[i]].first; + b = stk[who[i]].second; + for (j = 0; j < 8; j++) + { + tempa = a + hh[j]; + tempb = b + ll[j]; + if (data[tempa][tempb] < 1 || dep_counter[tempa][tempb] == find_dep_counter) + continue; + dep_counter[tempa][tempb] = find_dep_counter; + if (covered(tempa, tempb, who, cc)) + dep[cnt_dep++] = make_pair(tempa, tempb); + } + } +} + +bool acceptable(int tm, int hm) +{ + int cc = 8, who[10], chosen[10], i; + for (i = 0; i < 4; i++) + { + who[i] = i; + chosen[i] = (hm & (1 << i)) ? 1 : 0; + } + for (i = 0; i < 4; i++) + { + who[i + 4] = top - 4 + i; + if (tm & (1 << i)) + chosen[i + 4] = 1; + else + chosen[i + 4] = 0; + } + pair dep[100]; + int cnt_dep; + find_dep_counter++; + find_dep(who, cc, dep, cnt_dep); + return ok(who, chosen, cc, dep, cnt_dep); +} + +inline bool check_ok(int place, int mask) +{ + int who[5], chosen[5]; + int i; + for (i = 0; i < 5; i++) + { + who[i] = place - 2 + i; + if (mask & (1 << i)) + chosen[i] = 1; + else + chosen[i] = 0; + } + return ok(who, chosen, 5, should_check[place + 2], cnt_should_check[place + 2]); +} + +inline void work_inner_dp() +{ + int tail_mask, i, j, k; + bool flag; + int mask = (1 << safe_sight) - 1; + map table; + table.clear(); + for (tail_mask = 0; tail_mask < (1 << safe_sight); tail_mask++) + { + flag = false; + for (i = 0; i < (1 << safe_sight); i++) + if (acceptable(tail_mask, i)) + { + flag = true; + dp[3][pop_count(i)][i] = 1; + } + else + { + dp[3][pop_count(i)][i] = 0; + } + if (!flag) + continue; + memset(lmt, 0, sizeof(int) * (top + 1)); + for (i = 0; i < 4; i++) + lmt[i] = 4; + for (i = 3; i < top - 1; i++) + { + for (j = 0; j <= lmt[i] + 1; j++) + for (k = 0; k < (1 << safe_sight); k++) + dp[i + 1][j][k] = 0;// clear previous information + for (j = 0; j <= lmt[i]; j++) + for (k = 0; k < (1 << safe_sight); k++) + if (dp[i][j][k] > 0) + { + if (check_ok(i - 1, k + (1 << safe_sight))) + { + dp[i + 1][j + 1][(k + (1 << safe_sight)) >> 1] += dp[i][j][k]; + dp[i + 1][j + 1][(k + (1 << safe_sight)) >> 1] %= MOD; + lmt[i + 1] = max(lmt[i + 1], j + 1); + lmt[i + 1] = min(lmt[i + 1], T); + } + if (check_ok(i - 1, k)) + { + dp[i + 1][j][k >> 1] += dp[i][j][k]; + dp[i + 1][j][k >> 1] %= MOD; + lmt[i + 1] = max(lmt[i + 1], j); + lmt[i + 1] = min(lmt[i + 1], T); + } + } + } + for (i = 0; i <= lmt[top - 1]; i++) + if (dp[top - 1][i][tail_mask] != 0) + table[i] += dp[top - 1][i][tail_mask]; + } + total_state++; + partial_state[total_state].cnt.clear(); + map::iterator it; + for (it = table.begin(); it != table.end(); it++) + partial_state[total_state].cnt.push_back(*it); +} + +inline void force_enum() +{ + int i, mask; + bool flag; + map table; + table.clear(); + for (mask = 0; mask < (1 << top); mask++) + { + for (i = 0; i < top; i++) + clear_around(stk[i]); + for (i = 0; i < top; i++) + if (mask & (1 << i)) + set_around(stk[i]); + flag = true; + for (i = 0; i < top; i++) + if (!check_around(stk[i])) + flag = false; + if (!flag) + continue; + table[pop_count(mask, top)]++; + } + total_state++; + partial_state[total_state].cnt.clear(); + map::iterator it; + for (it = table.begin(); it != table.end(); it++) + partial_state[total_state].cnt.push_back(*it); +} + +inline void make_dep() +{ + int i, j; + int who[5]; + for (i = 4; i < top; i++) + { + for (j = 0; j < 5; j++) + who[j] = i - 4 + j; + find_dep_counter++; + find_dep(who, 5, should_check[i], cnt_should_check[i]); + } +} + +inline bool adj(pair a, pair b) +{ + return abs(a.first - b.first) <= 1 && abs(a.second - b.second) <= 1; +} + +inline void dfsxxxx(int a, int b) +{ + stk[top++] = make_pair(a, b); + board[a][b] = -1; + int tempa, tempb, i, choosea, chooseb, dist = -1; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (board[tempa][tempb] == global_tag) + { + if (dist == -1 || abs(added_unrevealed[tempa][tempb] - added_unrevealed[a][b]) < dist) + { + dist = abs(added_unrevealed[tempa][tempb] - added_unrevealed[a][b]); + choosea = tempa; + chooseb = tempb; + } + } + } + if (dist == -1) + return; + dfsxxxx(choosea, chooseb); +} + +inline void output_stk() +{ + puts("STK:"); + for (int i = 0; i < top; i++) + printf("%d %d\n", stk[i].first, stk[i].second); +} + +inline void output_dep() +{ + int i, j; + for (i = 4; i < top; i++) + { + printf("%d: ", i); + for (j = 0; j < cnt_should_check[i]; j++) + printf("(%d %d) ", should_check[i][j].first, should_check[i][j].second); + puts(""); + } +} + +inline void process_cycle() +{ + top = 0; + if (around.size() <= 8) + { + int i; + for (i = 0; i < around.size(); i++) + stk[top++] = around[i]; + force_enum(); + return; + } + if (touch_bound_flag) + { + int i, a, b; + for (i = 0; i < around.size(); i++) + { + a = around[i].first; + b = around[i].second; + if (a == 1 || a == n || b == 1 || b == m) + { + dfsxxxx(a, b); + break; + } + } + } + else + { + dfsxxxx(around[0].first, around[0].second); + } + make_dep(); + work_inner_dp(); +} + +inline bool is_inner(int a, int b) +{ + if (data[a][b] != -1) + return false; + int tempa, tempb, i; + for (i = 0; i < 8; i++) + { + tempa = a + hh[i]; + tempb = b + ll[i]; + if (data[tempa][tempb] != -1) + return false; + } + return true; +} + +inline void init() +{ + memset(visited, false, sizeof(visited)); + memset(added_unrevealed, false, sizeof(added_unrevealed)); + memset(board, -1, sizeof(board)); + int i, j, k, cnt = 0; + total_state = 0; + global_tag = 0; + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + if (i == 1 || i == n || j == 1 || j == m) + if (!visited[i][j] && data[i][j] > 0 && data[i][j] < 9) + { + touch_bound_flag = false; + around.clear(); + numbers.clear(); + dfs_timer = 0; + dfs_extract_field_bound(i, j); + global_tag++; + for (k = 0; k < around.size(); k++) + board[around[k].first][around[k].second] = global_tag; + process_cycle(); + } + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + if (!visited[i][j] && data[i][j] > 0 && data[i][j] < 9) + { + touch_bound_flag = false; + around.clear(); + numbers.clear(); + dfs_timer = 0; + dfs_extract_field_bound(i, j); + global_tag++; + for (k = 0; k < around.size(); k++) + board[around[k].first][around[k].second] = global_tag; + process_cycle(); + } + total_inner = 0; + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + if (is_inner(i, j)) + total_inner++; +} + +inline int calc_C(int a, int b) +{ + if (a < b) + return 0; + if (b == 0) + return 1; + if (CC[a][b] != -1) + return CC[a][b]; + if (a == b) + return CC[a][b] = 1; + else + return CC[a][b] = (calc_C(a - 1, b - 1) + calc_C(a - 1, b)) % MOD; +} + +inline int work_ans() +{ + memset(global_dp, 0, sizeof(global_dp)); + global_dp[0] = 1; + int i, j, k; + pair temp; + for (i = 1; i <= total_state; i++) + { + for (j = T; j >= 0; j--) + if (global_dp[j] != 0) + { + for (k = 0; k < partial_state[i].cnt.size(); k++) + { + temp = partial_state[i].cnt[k]; + if (j + temp.first > T) + break; + global_dp[j + temp.first] = (global_dp[j + temp.first] + global_dp[j] * temp.second) % MOD; + } + global_dp[j] = 0; + } + } + int res = 0, tt; + for (i = 0; i <= T; i++) + if (global_dp[i] != 0) + { + res += (global_dp[i] * (tt = calc_C(total_inner, T - i))) % MOD; + res %= MOD; + } + return res; +} + +int main() +{ + int t; + memset(CC, -1, sizeof(CC)); + int ans, i; + cin >> t; + for (i = 1; i <= t; i++) + { + read_data(); + init(); + ans = work_ans(); + printf("Case #%d: %d\n", i, ans); + } + return 0; +} diff --git a/12329.cpp b/12329.cpp new file mode 100644 index 0000000..989d33f --- /dev/null +++ b/12329.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef pair pii; +typedef pair pdd; +typedef pair ppii; +typedef vector vp; +typedef vector vvi; + +const int N = 100010; +const int M = 210; +const int K = 200010; +const int LIT = 2500; +const int INF = 1 << 30; +const double eps = 1e-8; +const int dir[6][2] = {{2, 0}, {-2, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}}; +const int ABS(int x) +{ + while (x < 0) + x = -x; + return x; +} + +#define mp make_pair +#define pb push_back +#define fst first +#define snd second + +struct node +{ + int sl, sr, col; +} r[N]; + +int n, cnt, L, R; +set have, loc; +set::iterator itr; +vp v[N]; + +void insert(int w, int p) +{ + pii nt = mp(w, p); + if (have.count(nt)) + return; + itr = have.lower_bound(nt); + if (itr == have.begin()) + { + r[L].sl = p; + L = p; + } + else if (itr == have.end()) + { + r[R].sr = p; + R = p; + } + else + { + pii tmp = *itr; + int p2 = tmp.snd; + itr--; + tmp = *itr; + int p1 = tmp.snd; + if (r[p1].sr == -1) + r[p1].sr = p; + else + r[p2].sl = p; + } + have.insert(nt); +} + +stack sta; +int beg[N]; + +void dfs() +{ + sta.push(1); + beg[1] = -1; + while (!sta.empty()) + { + int x = sta.top(); + if (beg[x] < 0) + { + if (r[x].sl != -1) + { + sta.push(r[x].sl); + beg[r[x].sl] = beg[x] - 2; + } + beg[x] = -beg[x]; + } + else if (beg[x] > 0) + { + loc.insert(mp(beg[x], ++cnt)); + v[(beg[x] + 1) >> 1].pb(mp(cnt, x)); + r[x].col = cnt; + sta.pop(); + if (r[x].sr != -1) + { + sta.push(r[x].sr); + beg[r[x].sr] = (-beg[x]) - 2; + } + } + } +} + +void init() +{ + have.clear(); + loc.clear(); + cnt = 0; + int w; + scanf("%d", &n); + for (int i = 0; i < N; i++) + { + r[i].sl = r[i].sr = -1; + v[i].clear(); + beg[i] = 0; + } + scanf("%d", &w); + have.insert(mp(w, 1)); + L = R = 1; + for (int i = 2; i <= n; i++) + { + scanf("%d", &w); + insert(w, i); + } + dfs(); +} + +char res[M][M]; + +char cal(int x, int y) +{ + pii pt = mp(x, y); + if (loc.count(pt)) + return 'o'; + pt.fst++; + if (loc.count(pt)) + return '|'; + pt.fst++; + if (loc.count(pt)) + return '+'; + if (x % 2 == 0) + return ' '; + int xx = (x + 1) >> 1; + if (!v[xx].size()) + return ' '; + int p2 = lower_bound(v[xx].begin(), v[xx].end(), mp(y, 0)) - v[xx].begin(); + int p1 = p2 - 1; + if (p1 >= 0) + { + int tmp = v[xx][p1].snd; + int lit = r[r[tmp].sr].col; + if (r[tmp].sr != -1 && y < lit) + return '-'; + } + if (p2 < v[xx].size()) + { + int tmp = v[xx][p2].snd; + int lit = r[r[tmp].sl].col; + if (r[tmp].sl != -1 && y > lit) + return '-'; + } + return ' '; +} + +void solve() +{ + int q; + cin >> q; + while (q--) + { + int X1, X2, Y1, Y2; + scanf("%d %d %d %d", &X1, &Y1, &X2, &Y2); + for (int i = X1, ii = 1; i <= X2 + X1 - 1; i++, ii++) + { + res[ii][0] = 0; + for (int j = Y1, jj = 1; j <= Y2 + Y1 - 1; j++, jj++) + { + res[ii][jj] = cal(i, j); + if (res[ii][jj] != ' ') + res[ii][0] = 1; + } + }; + for (int i = X1, ii = 1; i <= X2 + X1 - 1; i++, ii++) + { + if (!res[ii][0]) + continue; + for (int j = Y1, jj = 1; j <= Y2 + Y1 - 1; j++, jj++) + printf("%c", res[ii][jj]); + printf("\n"); + } + printf("\n"); + } +} + +int main() +{ + int tcase = 1, T; + cin >> T; + while (T--) + { + init(); + printf("Case #%d:\n", tcase++); + solve(); + } +} diff --git a/1233.cpp b/1233.cpp new file mode 100644 index 0000000..76f1fa5 --- /dev/null +++ b/1233.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +//1233 +//USHER +//Graphs;Shortest Path;Floyd-Warshall +#define MAX 501 + +int P[MAX]; +int G[MAX][MAX]; +int n, m; + +int main() +{ + int tt; + cin >> tt; + while (tt--) + { + int b, p, q; + cin >> b >> p >> q; + for (int i = 0; i < q; i++) + { + cin >> P[i]; + } + memset(G, 0x1f, sizeof(G)); + G[0][0] = 0; + for (int i = 1; i <= p; i++) + { + int k; + cin >> k; + G[i][i] = 0; + for (int j = 0; j < k; j++) + { + int x, y; + cin >> x >> y; + G[i][y] = min(G[i][y], x); + } + } + for (int k = 0; k <= p; k++) + for (int i = 0; i <= p; i++) + for (int j = 0; j <= p; j++) + { + G[i][j] = min(G[i][j], G[i][k] + G[k][j]); + } + int minn = 1 << 30; + for (int i = 0; i < q; i++) + { + minn = min(minn, G[P[i]][0]); + } + int current = 0; + int answer = 0; + while (true) + { + if ((current += minn) >= b) + { + break; + } + current--; + answer++; + } + cout << answer << endl; + } + return 0; +} diff --git a/12330.cpp b/12330.cpp new file mode 100644 index 0000000..c2b2921 --- /dev/null +++ b/12330.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +const int maxn = 205; +const int maxm = 80005; + +map T; +int n, cnt; +char input[10000], a[20], b[20], verb[20]; +int g[maxn], gr[maxn], to[maxm], np[maxm], cp; + +void add(int x, int y) +{ + to[cp] = y; + np[cp] = g[x]; + g[x] = cp++; + to[cp] = x; + np[cp] = gr[y]; + gr[y] = cp++; + assert(cp <= maxm); +} + +bool vis[maxn]; +int Q[maxn], BEGIN, END; + +bool bfs(int x, int y) +{ + if (x == y) + { + return true; + } + memset(vis, 0, sizeof(vis)); + BEGIN = END = 0; + Q[END++] = x; + vis[x] = true; + while (BEGIN != END) + { + int x = Q[BEGIN++]; + for (int i = g[x]; i; i = np[i]) + if (!vis[to[i]]) + { + vis[to[i]] = true; + if (to[i] == y) + { + return true; + } + Q[END++] = to[i]; + } + } + return false; +} + +char ask(int x, int y) +{ + return bfs(x, y) ? 'Y' : 'M'; +} + +bool parse(char input[]) +{ + int length = strlen(input); + while (input[length - 1] < 32) + { + length--; + } + char END = input[length - 1]; + if (END == '!') + { + return false; + } + input[length - 1] = 0; + int u = 0, x, y, ws = 0; + for (; input[u] != ' '; u++) + ; + for (int i = 0; input[i]; i++) + if (input[i] == ' ') + { + ws++; + } + if (END == '.') + { + if (input[u + 1] != 'w') + { + sscanf(input, "%s %s %s", a, verb, b); + strcat(a, "n"); + strcat(b, verb[0] == 'a' ? "n" : "v"); + } + else + { + sscanf(input, "%*s %*s %*s %s %s %s", a, verb, b); + strcat(a, "v"); + strcat(b, verb[0] == 'a' ? "n" : "v"); + } + } + else + { + if (ws == 2) + { + sscanf(input, "%s %s %s", verb, a, b); + strcat(a, "n"); + strcat(b, verb[0] == 'a' ? "n" : "v"); + } + else + { + sscanf(input, "%s %*s %*s %*s %s %s", verb, a, b); + strcat(a, "v"); + strcat(b, verb[0] == 'a' ? "n" : "v"); + } + } + if (T.find(a) == T.end()) + { + x = T[a] = cnt++; + } + else + { + x = T[a]; + } + if (T.find(b) == T.end()) + { + y = T[b] = cnt++; + } + else + { + y = T[b]; + } + assert(cnt <= 200); + assert(strlen(a) <= 11); + assert(strlen(b) <= 11); + /* + printf("%s\n", input); + printf("%s %s\n", a, b); + printf("%d %d\n", x, y); + */ + if (END == '.') + { + add(x, y); + } + else + { + printf("%c", ask(x, y)); + } + return true; +} + +int main() +{ + int ntest; + scanf("%d", &ntest); + fgets(input, 10000, stdin); + for (int test = 1; test <= ntest; test++) + { + T.clear(); + cnt = 0; + memset(g, 0, sizeof(g)); + memset(gr, 0, sizeof(gr)); + cp = 2; + printf("Case #%d:\n", test); + while (true) + { + fgets(input, 10000, stdin); + if (!parse(input)) + { + break; + } + } + printf("\n"); + } + return 0; +} diff --git a/12331.cpp b/12331.cpp new file mode 100644 index 0000000..3ee1171 --- /dev/null +++ b/12331.cpp @@ -0,0 +1,371 @@ +#include + +using namespace std; + +const double eps = 1e-7; +const double pi = acos(-1.0); + +struct point +{ + double x, y; +} p[6], q[6], r[8]; + +double lp[6], lq[6], lr[8], ls[8], lt[8]; +double ap[6], aq[6], ar[8]; + +double dist(point a, point b) +{ + a.x -= b.x; + a.y -= b.y; + return sqrt(a.x * a.x + a.y * a.y); +} + +double cross(point a, point b, point c) +{ + a.x -= c.x; + a.y -= c.y; + b.x -= c.x; + b.y -= c.y; + return a.x * b.y - a.y * b.x; +} + +double getAngle(point a, point b, point c) +{ + double angle = atan2(b.y - a.y, b.x - a.x) - atan2(c.y - a.y, c.x - a.x); + while (angle < 0) + { + angle += pi + pi; + } + while (angle > pi + pi - eps) + { + angle -= pi + pi; + } + return angle; +} + +void normalize(point p[], int n) +{ + double area = 0; + for (int i = 1; i + 1 < n; i++) + { + area += cross(p[0], p[i], p[i + 1]); + } + assert(fabs(area) > eps); + if (area > 0) + { + reverse(p, p + n); + } +} + +bool same(double l1[], double l2[]) +{ + double b1[3]; + double b2[3]; + for (int i = 0; i < 3; i++) + { + b1[i] = l1[i]; + } + for (int i = 0; i < 3; i++) + { + b2[i] = l2[i]; + } + sort(b1, b1 + 3); + sort(b2, b2 + 3); + for (int i = 0; i < 3; i++) + if (fabs(b1[i] - b2[i]) > eps) + { + return false; + } + return true; +} + +bool checkTri() +{ + for (int i = 0; i < 4; i++) + { + if (fabs(ar[i] - pi) < eps) + { + for (int j = 0, u = 0; j < 4; j++) + { + if (j == i) + { + lt[u++] = lr[j] + lr[j + 3]; + } + else if (j == (i + 3) % 4) + { + continue; + } + else + { + lt[u++] = lr[j]; + } + } + } + } + for (int a1 = 0; a1 < 3; a1++) + { + for (int b1 = 0; b1 < 3; b1++) + if (a1 != b1) + { + for (int a2 = 0; a2 < 3; a2++) + { + for (int b2 = 0; b2 < 3; b2++) + if (a2 != b2) + { + double l1 = dist(p[a1], p[b1]); + double l2 = dist(q[a2], q[b2]); + int c1 = 3 - a1 - b1; + int c2 = 3 - a2 - b2; + if (fabs(l1 - l2) > eps) + { + continue; + } + if (fabs(ap[a1] + aq[a2] - pi) > eps) + { + continue; + } + ls[0] = dist(p[a1], p[c1]) + dist(q[a2], q[c2]); + ls[1] = dist(p[b1], p[c1]); + ls[2] = dist(q[b2], q[c2]); + if (same(lt, ls)) + { + return true; + } + } + } + } + } + return false; +} + +bool checkSame(double lt[], double lp[], double lq[]) +{ + return same(lt, lp) && same(lt + 3, lq) || same(lt, lq) && same(lt + 3, lp); +} + +bool checkConvex() +{ + for (int i = 0; i < 4; i++) + { + lt[0] = dist(r[i], r[i + 1]); + lt[1] = dist(r[i + 1], r[i + 2]); + lt[2] = dist(r[i], r[i + 2]); + lt[3] = dist(r[i], r[i + 2]); + lt[4] = dist(r[i], r[i + 3]); + lt[5] = dist(r[i + 2], r[i + 3]); + if (checkSame(lt, lp, lq)) + { + return true; + } + } + return false; +} + +void getCut(point a, point b, point c, point d, double &u, double &v) +{ + double p = cross(a, c, d); + double q = cross(b, c, d); + double l = dist(a, b); + u = l / (q - p) * p; + p = cross(a, b, c); + q = cross(a, b, d); + l = dist(c, d); + v = l / (p - q) * p; +} + +bool checkNonConvex() +{ + for (int i = 0; i < 4; i++) + if (ar[i] > pi) + { + lt[0] = dist(r[i], r[i + 1]); + lt[1] = dist(r[i + 1], r[i + 2]); + lt[2] = dist(r[i], r[i + 2]); + lt[3] = dist(r[i], r[i + 2]); + lt[4] = dist(r[i], r[i + 3]); + lt[5] = dist(r[i + 2], r[i + 3]); + if (checkSame(lt, lp, lq)) + { + return true; + } + double u, v; + getCut(r[i], r[i + 1], r[i + 2], r[i + 3], u, v); + // fprintf(stderr, "%lf %lf\n", u, v); + lt[0] = dist(r[i], r[i + 1]) + u; + lt[1] = dist(r[i + 1], r[i + 2]); + lt[2] = v; + lt[3] = dist(r[i + 2], r[i + 3]) - v; + lt[4] = dist(r[i + 3], r[i]); + lt[5] = u; + // fprintf(stderr, "%lf %lf %lf %lf %lf %lf\n", lt[0], lt[1], lt[2], lt[3], lt[4], lt[5]); + if (checkSame(lt, lp, lq)) + { + return true; + } + reverse(r + i + 1, r + i + 4); + getCut(r[i], r[i + 1], r[i + 2], r[i + 3], u, v); + // fprintf(stderr, "%lf %lf\n", u, v); + lt[0] = dist(r[i], r[i + 1]) + u; + lt[1] = dist(r[i + 1], r[i + 2]); + lt[2] = v; + lt[3] = dist(r[i + 2], r[i + 3]) - v; + lt[4] = dist(r[i + 3], r[i]); + lt[5] = u; + // fprintf(stderr, "%lf %lf %lf %lf %lf %lf\n", lt[0], lt[1], lt[2], lt[3], lt[4], lt[5]); + if (checkSame(lt, lp, lq)) + { + return true; + } + } + return false; +} + +bool checkQuad() +{ + bool convex = true; + for (int i = 0; i < 4; i++) + { + if (ar[i] > pi) + { + convex = false; + } + } + if (convex) + { + return checkConvex(); + } + else + { + return checkNonConvex(); + } +} + +int pp[205]; + +void gen_special_1() +{ + memset(pp, 0, sizeof(pp)); + for (int x = 1; x * x <= 200; x++) + { + for (int y = x + 1; x * x + y * y <= 200; y++) + { + pp[x * x + y * y]++; + } + } + int x, y, z = 200; + while (pp[z] < 2) + { + z--; + } + for (x = 1; x * x <= z; x++) + { + for (y = x + 1; x * x + y * y <= z; y++) + { + if (x * x + y * y == z) + { + printf("%d %d %d\n", x, y, z); + } + } + } +} + +/* + * 1) edge length ? + * 2) angle ? + * 3) area ? + * + * matched 1) and 2) yes + * unordered 1) and 2) maybe + * ordered 1) and 3) no + * unordered 1) and 3) no + * 2) and 3) no + */ + +int main() +{ + int ntest; + scanf("%d", &ntest); + for (int test = 1; test <= ntest; test++) + { + for (int i = 0; i < 3; i++) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + } + for (int i = 0; i < 3; i++) + { + scanf("%lf%lf", &q[i].x, &q[i].y); + } + for (int i = 0; i < 4; i++) + { + scanf("%lf%lf", &r[i].x, &r[i].y); + } + for (int i = 0; i < 3; i++) + { + assert(-15000 <= p[i].x && p[i].x <= 15000); + } + for (int i = 0; i < 3; i++) + { + assert(-15000 <= q[i].x && q[i].x <= 15000); + } + for (int i = 0; i < 4; i++) + { + assert(-15000 <= r[i].x && r[i].x <= 15000); + } + normalize(p, 3); + normalize(q, 3); + normalize(r, 4); + for (int i = 0; i < 3; i++) + { + p[i + 3] = p[i]; + } + for (int i = 0; i < 3; i++) + { + q[i + 3] = q[i]; + } + for (int i = 0; i < 4; i++) + { + r[i + 4] = r[i]; + } + for (int i = 0; i < 3; i++) + { + lp[i] = lp[i + 3] = dist(p[i], p[i + 1]); + } + for (int i = 0; i < 3; i++) + { + lq[i] = lq[i + 3] = dist(q[i], q[i + 1]); + } + for (int i = 0; i < 4; i++) + { + lr[i] = lr[i + 4] = dist(r[i], r[i + 1]); + } + for (int i = 0; i < 3; i++) + { + ap[i] = ap[i + 3] = getAngle(p[i], p[i + 1], p[i + 2]); + } + for (int i = 0; i < 3; i++) + { + aq[i] = aq[i + 3] = getAngle(q[i], q[i + 1], q[i + 2]); + } + for (int i = 0; i < 4; i++) + { + ar[i] = ar[i + 4] = getAngle(r[i], r[i + 1], r[i + 3]); + } + bool isTri = false; + for (int i = 0; i < 4; i++) + { + if (fabs(ar[i] - pi) < eps) + { + isTri = true; + } + } + if (isTri) + { + printf("Case #%d: %s\n", test, checkTri() ? "Yes" : "No"); + } + else + { + printf("Case #%d: %s\n", test, checkQuad() ? "Yes" : "No"); + } + } + return 0; +} diff --git a/12332.cpp b/12332.cpp new file mode 100644 index 0000000..a7da8c1 --- /dev/null +++ b/12332.cpp @@ -0,0 +1,412 @@ +#include + +using namespace std; + +#define M 110 +#define eps 1e-8 + +const double pi = acos(-1.0); + +class pnt_type +{ +public: + int x, y; +}; + +class float_pnt_type +{ +public: + double x, y; +}; + +float_pnt_type float_pnt1[M], float_pnt2[M]; +vector> tangent; + +pnt_type pnt1[M], pnt2[M]; +int n1, n2; + +void read_data() +{ + int i; + scanf("%d", &n1); + for (i = 1; i <= n1; i++) + { + scanf("%d%d", &pnt1[i].x, &pnt1[i].y); + } + pnt1[n1 + 1] = pnt1[1]; + scanf("%d", &n2); + for (i = 1; i <= n2; i++) + { + scanf("%d%d", &pnt2[i].x, &pnt2[i].y); + } + pnt2[n2 + 1] = pnt2[1]; + for (i = 1; i <= n1 + 1; i++) + { + float_pnt1[i].x = pnt1[i].x; + float_pnt1[i].y = pnt1[i].y; + } + for (i = 1; i <= n2 + 1; i++) + { + float_pnt2[i].x = pnt2[i].x; + float_pnt2[i].y = pnt2[i].y; + } +} + +inline int area2(pnt_type a, pnt_type b, pnt_type c) +{ + return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); +} + +inline double area2(float_pnt_type a, float_pnt_type b, float_pnt_type c) +{ + return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); +} + +inline int sign(int a) +{ + if (a == 0) + { + return 0; + } + else if (a > 0) + { + return 1; + } + else + { + return -1; + } +} + +inline int sign(double a) +{ + if (a == 0) + { + return 0; + } + else if (a > 0) + { + return 1; + } + else + { + return -1; + } +} +inline bool same_sign(int a, int b) +{ + //int __a = sign(a), __b = sign(b); + return sign(a) * sign(b) >= 0; +} +bool check(pnt_type a, pnt_type b) +{ + //throw new exception("No implementation exception."); + int i; + for (i = 1; i <= n1; i++) + if (!same_sign(area2(a, b, pnt1[i]), area2(a, b, pnt1[i + 1]))) + { + return false; + } + for (i = 1; i <= n2; i++) + if (!same_sign(area2(a, b, pnt2[i]), area2(a, b, pnt2[i + 1]))) + { + return false; + } + int tempa, tempb; + for (i = 1; i <= n1; i++) + if (area2(a, b, pnt1[i]) != 0) + { + tempa = i; + break; + } + for (i = 1; i <= n2; i++) + if (area2(a, b, pnt2[i]) != 0) + { + tempb = i; + break; + } + if (same_sign(area2(a, b, pnt1[tempa]), area2(a, b, pnt2[tempb]))) + { + return false; + } + return true; +} + +bool not_appear(pnt_type a, pnt_type b) +{ + int i; + pnt_type x, y; + for (i = 0; i < tangent.size(); i++) + { + x = tangent[i].first; + y = tangent[i].second; + if (area2(a, b, x) == 0 && area2(a, b, y) == 0) + { + return false; + } + } + return true; +} + +double angle1, angle2; +float_pnt_type cp; + +float_pnt_type cross_pnt(pnt_type a, pnt_type b, pnt_type c, pnt_type d) +{ + float_pnt_type res; + res.x = (area2(a, b, d) * c.x - area2(a, b, c) * d.x + 0.0) / (area2(a, b, d) - area2(a, b, c)); + res.y = (area2(a, b, d) * c.y - area2(a, b, c) * d.y + 0.0) / (area2(a, b, d) - area2(a, b, c)); + return res; +} + +float_pnt_type cross_pnt(float_pnt_type a, float_pnt_type b, float_pnt_type c, float_pnt_type d) +{ + float_pnt_type res; + res.x = (area2(a, b, d) * c.x - area2(a, b, c) * d.x + 0.0) / (area2(a, b, d) - area2(a, b, c)); + res.y = (area2(a, b, d) * c.y - area2(a, b, c) * d.y + 0.0) / (area2(a, b, d) - area2(a, b, c)); + return res; +} + +void init() +{ + int i, j; + //double __slope; + tangent.clear(); + for (i = 1; i <= n1; i++) + for (j = 1; j <= n2; j++) + { + //printf("%d %d\n",i,j); + if (check(pnt1[i], pnt2[j])) + { + if (not_appear(pnt1[i], pnt2[j])) + { + tangent.push_back(make_pair(pnt1[i], pnt2[j])); + } + } + } + i = i; + //if (tangent.size() != 2) throw new exception("Does not exist two common tangent!"); + cp = cross_pnt(tangent[0].first, tangent[0].second, tangent[1].first, tangent[1].second); + angle1 = atan2(tangent[0].second.y - tangent[0].first.y + 0.0, tangent[0].second.x - tangent[0].first.x + 0.0); + angle2 = atan2(tangent[1].second.y - tangent[1].first.y + 0.0, tangent[1].second.x - tangent[1].first.x + 0.0); + if (angle1 < 0) + { + angle1 += 2 * pi; + } + if (angle2 < 0) + { + angle2 += 2 * pi; + } + if (angle2 - angle1 < 0) + { + swap(angle1, angle2); + } + if (angle2 - angle1 > pi) + { + angle2 -= pi * 2; + swap(angle2, angle1); + } +} + +double paraK, paraB; + +float_pnt_type lf[M], rt[M]; +int top_lf, top_rt; + +float_pnt_type base; + +inline bool cmp(float_pnt_type a, float_pnt_type b) +{ + if (a.x != b.x) + { + return a.x < b.x; + } + else + { + return a.y < b.y; + } +} + +inline bool cmp2(float_pnt_type a, float_pnt_type b) +{ + return area2(base, a, b) > 0; +} + +float_pnt_type stk[M]; +double calc_area(float_pnt_type *data, int n) +{ + if (n < 3) + { + return 0; + } + sort(data + 1, data + n + 1, cmp); + base = data[1]; + sort(data + 2, data + n + 1, cmp2); + int top = 2; + stk[1] = base; + stk[2] = data[2]; + int i; + for (i = 3; i <= n; i++) + { + while (area2(stk[top - 1], stk[top], data[i]) < 0 && top > 2) + { + top--; + } + stk[++top] = data[i]; + } + while (area2(stk[top - 1], stk[top], data[1]) < 0 && top > 2) + { + top--; + } + double res = 0; + for (i = 3; i <= n; i++) + { + res += fabs(area2(data[1], data[i - 1], data[i])) / 2; + } + return res; +} +double calc_the_dif(float_pnt_type *float_pnt, int n, double bias, double alpha) +{ + float_pnt_type org, other; + org.x = cp.x + bias * cos(alpha + pi / 2); + org.y = cp.y + bias * sin(alpha + pi / 2); + other.x = org.x + cos(alpha); + other.y = org.y + sin(alpha); + top_lf = top_rt = 0; + int i; + for (i = 1; i <= n; i++) + { + if (area2(org, other, float_pnt[i]) > -eps) + { + top_lf++; + lf[top_lf] = float_pnt[i]; + } + if (area2(org, other, float_pnt[i]) < eps) + { + top_rt++; + rt[top_rt] = float_pnt[i]; + } + } + float_pnt_type temp; + for (i = 1; i <= n; i++) + if (sign(area2(org, other, float_pnt[i])) * sign(area2(org, other, float_pnt[i + 1])) < 0) + { + temp = cross_pnt(org, other, float_pnt[i], float_pnt[i + 1]); + top_lf++; + top_rt++; + lf[top_lf] = temp; + rt[top_rt] = temp; + } + return calc_area(lf, top_lf) - calc_area(rt, top_rt); +} + +double find_the_line(double theta) +{ + double up = 3000, dn = -3000; + double dif1, dif2; + dif1 = calc_the_dif(float_pnt1, n1, up, theta); + dif2 = calc_the_dif(float_pnt1, n1, dn, theta); + double mid, temp; + while (up - dn > eps) + { + mid = (up + dn) / 2; + if (mid > -0.5 && mid < 0) + { + mid = mid; + } + temp = calc_the_dif(float_pnt1, n1, mid, theta); + if (dif1 > dif2) + { + if (temp > 0) + { + up = mid; + } + else + { + dn = mid; + } + } + else + { + if (temp > 0) + { + dn = mid; + } + else + { + up = mid; + } + } + } + return (up + dn) / 2; +} + +double temp_p1, temp_theta; + +double calc_parameter(double theta) +{ + double p1; + p1 = find_the_line(theta); + temp_p1 = p1; + temp_theta = theta; + return calc_the_dif(float_pnt2, n2, p1, theta); +} +void work_ans() +{ + double up = angle2, dn = angle1, mid; + double ref_left = calc_parameter(up); + double ref_right = calc_parameter(dn); + double temp; + while (up - dn > eps) + { + mid = (up + dn) / 2; + temp = calc_parameter(mid); + if (ref_left > ref_right) + { + if (temp > 0) + { + up = mid; + } + else + { + dn = mid; + } + } + else + { + if (temp > 0) + { + dn = mid; + } + else + { + up = mid; + } + } + } + calc_parameter((up + dn) / 2); +} + +void show_ans() +{ + paraK = tan(temp_theta); + double xx, yy; + xx = cp.x + temp_p1 * cos(temp_theta + pi / 2); + yy = cp.y + temp_p1 * sin(temp_theta + pi / 2); + paraB = yy - xx * paraK; + printf("%.9f %.9f\n", paraK, paraB); +} +int main() +{ + int t, i; + scanf("%d", &t); + for (i = 1; i <= t; i++) + { + printf("Case #%d: ", i); + read_data(); + init(); + work_ans(); + show_ans(); + } + return 0; +} diff --git a/12333.cpp b/12333.cpp new file mode 100644 index 0000000..ba42dc7 --- /dev/null +++ b/12333.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +#define mod 1000000000 +int n; +int NEXT[2000000][10]; +int dang[2000000]; +int PREV[50000]; +int ans[100000]; +int fibo[3][3000], l[3]; +int dig[80], ld; +int main() +{ + char s[99]; + int sz = 0; + dang[++sz] = -1; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s", s); + int p = 1; + for (int j = 0; s[j]; j++) + { + s[j] -= '0'; + int &w = NEXT[p][s[j]]; + if (w == 0) + { + dang[++sz] = -1; + w = sz; + } + p = w; + } + if (dang[p] == -1) + { + dang[p] = i; + PREV[i] = -1; + ans[i] = -1; + } + else + { + PREV[i] = dang[p]; + } + } + l[0] = l[1] = 1; + fibo[0][0] = fibo[1][0] = 1; + int p = NEXT[1][1]; + if (p != 0 && dang[p] != -1) + { + ans[dang[p]] = 0; + } + int u = 0, v = 1, w = 2; + for (int t = 2; t < 100000; t++) + { + for (int j = 0; j < l[u] || j < l[v]; j++) + { + fibo[w][j] = (j < l[u] ? fibo[u][j] : 0) + (j < l[v] ? fibo[v][j] : 0); + } + l[w] = l[u] >= l[v] ? l[u] : l[v]; + for (int j = 0; j < l[w]; j++) + if (fibo[w][j] >= mod) + { + ++fibo[w][j + 1], fibo[w][j] -= mod; + if (j == l[w] - 1) + { + l[w]++; + } + } + ld = 0; + for (int x = fibo[w][l[w] - 1]; x > 0; x /= 10) + { + dig[ld++] = x % 10; + } + reverse(dig, dig + ld); + for (int j = l[w] - 2, k = 0; j >= 0 && k < 5; j--, k++) + { + for (int i = 0, x = fibo[w][j]; i < 9; i++, x /= 10) + { + dig[ld + 8 - i] = x % 10; + } + ld += 9; + } + int q = 1; + for (int j = 0; j < 40 && j < ld; j++) + { + q = NEXT[q][dig[j]]; + if (q == 0) + { + break; + } + if (dang[q] != -1 && ans[dang[q]] == -1) + { + ans[dang[q]] = t; + } + } +#ifdef debug + for (int j = l[w] - 1; j >= 0; j--) + { + printf("%09d", fibo[w][j]); + } + puts(""); +#endif + ++u; + if (u == 3) + { + u = 0; + } + ++v; + if (v == 3) + { + v = 0; + } + ++w; + if (w == 3) + { + w = 0; + } + } + for (int i = 0; i < n; i++) + { + printf("Case #%d: %d\n", i + 1, ans[PREV[i] == -1 ? i : PREV[i]]); + } + return 0; +} diff --git a/12334.cpp b/12334.cpp new file mode 100644 index 0000000..6c3259f --- /dev/null +++ b/12334.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define ran 1010 + +int n; +int fat[ran]; +double b[ran], c[ran]; +map e[ran]; +double ans[ran]; +double mon[ran], tim[ran], num[ran]; +void dfs(int x, bool isRoot, double &bes, int &v) +{ + mon[x] = b[x]; + tim[x] = c[x]; + for (map::iterator it = e[x].begin(); it != e[x].end(); it++) + { + int y = it->first; + double z = it->second; + num[y] = num[x] * z; + dfs(y, false, bes, v); + mon[x] += mon[y] * z; + tim[x] += tim[y] * z; + } + if (!isRoot) + { + double R = mon[x] / tim[x]; + if (R > bes) + { + bes = R, v = x; + } + } +} + +void process(int root, double add) +{ + double bes = -1; + int v; + num[root] = 1; + dfs(root, true, bes, v); + if (bes < -.5) + { + ans[root] += add * c[root]; + return; + } + double nm = num[v], w = mon[v], t = tim[v]; + e[fat[v]].erase(v); + process(v, add); + ans[root] += nm * (nm - 1) / 2 * w * t + nm * ans[v]; + process(root, add + nm * w); +} + +int main() +{ + int _, t; + scanf("%d", &_); + for (t = 1; t <= _; t++) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%lf%lf", &b[i], &c[i]); + e[i].clear(); + int x, z; + double y; + for (scanf("%d", &x); x--;) + { + scanf("%d%lf", &z, &y); + e[i][z] = y; + fat[z] = i; + } + ans[i] = 0; + } + process(1, 0); + printf("Case #%d: %.0lf\n", t, ans[1]); + } + return 0; +} diff --git a/12335.cpp b/12335.cpp new file mode 100644 index 0000000..f64ce54 --- /dev/null +++ b/12335.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +typedef long long LL; +LL fact[21]; +char s[32], ans[32]; +int main() +{ + int t; + scanf("%d", &t); + LL n; + fact[0] = 1; + for (int i = 1; i < 21; ++i) + { + fact[i] = i * fact[i - 1]; + } + for (int c = 0; c++ < t && scanf("%s %lld", s, &n) == 2;) + { + int len; + for (len = 0; s[len]; ++len) + ; + for (int i = 0, u = 0; i < len; ++i) + { + int j, rpos = 0, z; + while (n > fact[len - i - 1]) + { + ++rpos; + n -= fact[len - i - 1]; + } + for (j = z = 0; z < rpos || (u & (1 << j)); ++j) + if (!(u & (1 << j))) + { + ++z; + } + ans[j] = s[i]; + u |= (1 << j); + } + ans[len] = 0; + printf("Case %d: ", c); + puts(ans); + } + return 0; +} diff --git a/12336.cpp b/12336.cpp new file mode 100644 index 0000000..18f79ab --- /dev/null +++ b/12336.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define mod 1000007 +#define maxn 100000 + +int f[6][3][2] = {1, 2, 3, 1, 2, -1, + 1, 2, 3, 3, 2, 1, + 1, 2, 2, 0, 1, -2, + 2, 1, 4, 0, 2, -1, + 2, 1, 3, -1, 1, -2, + 2, -1, 3, -3, 1, -2}; +int n, m, c; + +int check(int x, int y) +{ + if (x >= 1 && x <= n && y >= 1 && y <= m) + { + return 1; + } + return 0; +} + +int check1(int id, int x, int y) +{ + int j; + for (j = 0; j < 3; j++) + { + int a = f[id][j][0], b = f[id][j][1]; + if (!check(x + a, y + b)) + { + return 0; + } + } + return 1; +} + +long long mp[maxn * 32]; +int nn; + +int main() +{ + int i, j, k; + int t, cas; + long long res, tag = 1; + scanf("%d", &t); + for (cas = 1; cas <= t; cas++) + { + scanf("%d%d%d", &n, &m, &c); + res = 0; + memset(mp, 0, sizeof(mp)); + for (i = 0; i < 6; i++) + { + int lx, rx, ly, ry; + lx = 1; + rx = n; + ly = 1; + ry = m; + int cnt = 0; + for (j = 0; j < 3; j++) + { + int a = f[i][j][0], b = f[i][j][1]; + lx = max(lx, 1 - a); + rx = min(rx, n - a); + ly = max(ly, 1 - b); + ry = min(ry, m - b); + } + if (lx <= rx && ly <= ry) + { + res += tag * (rx - lx + 1) * (ry - ly + 1), cnt = (rx - lx + 1) * (ry - ly + 1); + } + } + nn = 0; + while (c--) + { + int x, y; + scanf("%d%d", &x, &y); + long long tmp = x * tag * (m + 1) + y; + tmp *= 10; + for (i = 0; i < 6; i++) + { + if (check1(i, x, y)) + { + mp[++nn] = (tmp + i); + } + } + for (i = 0; i < 6; i++) + { + for (j = 0; j < 3; j++) + { + int a = f[i][j][0], b = f[i][j][1]; + if (check(x - a, y - b) && check1(i, x - a, y - b)) + { + long long tmp = (x - a) * tag * (m + 1) + (y - b); + tmp = tmp * 10 + i; + mp[++nn] = tmp; + } + } + } + } + sort(mp + 1, mp + nn + 1); + nn = unique(mp + 1, mp + nn + 1) - mp - 1; + res -= nn; + res *= 8; + printf("Case %d: ", cas); + cout << res << endl; + } + return 0; +} diff --git a/12337.cpp b/12337.cpp new file mode 100644 index 0000000..2b33af1 --- /dev/null +++ b/12337.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +#define FORI(i, s, n) for (int i = s; i < n; ++i) +int dy[4] = {1, 0, -1, 0}; +int dx[4] = {0, 1, 0, -1}; +char b[128]; +char ball[128][128]; + +int min(int a, int b) +{ + return a < b ? a : b; +} + +bool fill(int r, int c) +{ + int dir = 0, nx, ny, t = r * c, x = 0, y = -1; + FOR(i, r) + FOR(j, c) ball[i][j] = '.'; + FOR(i, t) + { + FOR(test, 4)// find direction + { + nx = x + dx[dir]; + ny = y + dy[dir]; + if (nx < 0 || ny < 0 || nx >= r || ny >= c || ball[nx][ny] != '.') + { + dir = (dir + 1) % 4; + } + else + { + break; + } + } + ball[x = nx][y = ny] = b[i]; + } + FOR(j, c) + FORI(i, 1, r) if (i && ball[i][j] != ball[i - 1][j]) + { + return false; + } + return true; +} + +int main() +{ + int t; + scanf("%d", &t); + for (int cnum = 0; t-- && scanf("%s", b) == 1;) + { + int ans = 1000, balls; + for (balls = 0; b[balls]; ++balls) + ; + FORI(i, 2, 51) + FORI(j, i, 51) if (i * j == balls && (fill(i, j) || fill(j, i))) + { + ans = min(ans, i + j); + } + printf("Case %d: %d\n", ++cnum, ans == 1000 ? -1 : ans); + } + return 0; +} diff --git a/12338.cpp b/12338.cpp new file mode 100644 index 0000000..b01a5e8 --- /dev/null +++ b/12338.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +const int N = 100005; + +pair str[N]; +int t, n, sav[N], height[N], rank_[N], best[N * 10][20]; + +int main() +{ + int cas = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + cin >> str[i].first; + sav[i] = str[i].first.length(); + str[i].second = i; + } + sort(str, str + n); + + for (int i = 0; i < n; i++) + { + rank_[str[i].second] = i; + if (i == 0) + { + continue; + } + int len = min(str[i - 1].first.length(), str[i].first.length()); + int j; + for (j = 0; j < len; j++) + { + if (str[i - 1].first[j] != str[i].first[j]) + { + break; + } + } + height[i] = j; + } + + for (int i = 0; i < n; i++) + { + best[i][0] = height[i]; + } + for (int j = 1; (1 << j) <= n; j++) + { + for (int i = 0; i + (1 << j) - 1 < n; i++) + { + best[i][j] = min(best[i][j - 1], best[i + (1 << (j - 1))][j - 1]); + } + } + + int q, l, r; + scanf("%d", &q); + printf("Case %d:\n", ++cas); + while (q--) + { + scanf("%d%d", &l, &r); + l--; + r--; + if (l == r) + { + printf("%d\n", sav[l]); + continue; + } + l = rank_[l]; + r = rank_[r]; + if (l > r) + { + swap(l, r); + } + l++; + int k = 0; + while ((1 << (k + 1)) <= r - l + 1) + { + k++; + } + printf("%d\n", min(best[l][k], best[r - (1 << k) + 1][k])); + } + } + return 0; +} diff --git a/12339.cpp b/12339.cpp new file mode 100644 index 0000000..d635277 --- /dev/null +++ b/12339.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define ll long long +#define inf 1000000009 +#define mod 1000000007 +#define N 2509 + +int s[N][N], a[N]; +void init() +{ + s[0][0] = 1; + for (int i = 1; i < N; i++) + for (int j = 1; j <= i; j++) + { + s[i][j] = (s[i - 1][j - 1] + 1ll * s[i - 1][j] * j) % mod; + } +} +void extend_gcd(ll &x, ll &y, ll a, ll b) +{ + if (b == 0) + { + x = 1, y = 0; + return; + } + extend_gcd(x, y, b, a % b); + x -= a / b * y; + y ^= x, x ^= y, y ^= x; +} +ll rev(ll a) +{ + ll x, y; + extend_gcd(x, y, a, mod); + return x % mod; +} +ll sumk(int n, int m) +{ + ll r = 0; + for (int i = m + 1; i; i--) + { + r = (r + 1ll * s[m][i - 1] * a[i] % mod * rev(i)) % mod; + } + return r; +} +ll mul(ll a, ll b) +{ + ll r = 1; + for (; b; b >>= 1, a = a * a % mod) + if (b & 1) + { + r = r * a % mod; + } + return r; +} +int main() +{ + int T, n, k; + init(); + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &k); + memset(a, 0, sizeof(a)); + a[0] = 1; + for (int i = 1; i <= k + 1 && i <= n + 1; i++) + { + a[i] = 1ll * a[i - 1] * (n + 2 - i) % mod; + } + int ans = ((mul(1ll * (n + 1) * n / 2 % mod, k) - sumk(n, k)) % mod + mod) % mod; + printf("%d\n", ans); + } + return 0; +} diff --git a/1234.cpp b/1234.cpp new file mode 100644 index 0000000..d487fe4 --- /dev/null +++ b/1234.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +#define MAX 10005 + +struct Road +{ + int v, c; + Road(int v, int c) : v(v), c(c) {} + inline bool operator<(const Road &that) const + { + return c < that.c; + } +}; + +bool V[MAX]; +int CStart[MAX], CCount[MAX], nc, n, m; +priority_queue Q; +vector R; +vector G[MAX]; + +int dfs(int v) +{ + V[v] = true; + int accum = 1; + for (int i = 0; i < G[v].size(); i++) + { + if (!V[G[v][i].v]) + { + accum += dfs(G[v][i].v); + } + } + return accum; +} + +int main() +{ + int T; scanf("%d", &T); + for (int t=1; t<=T; t++) + { + scanf("%d%d", &n,&m); + memset(V, 0, sizeof(V)); + memset(G, 0, sizeof(G)); + nc = 0; + R.clear(); + for (int i = 0; i < m; i++) + { + int a, b, c; + cin >> a >> b >> c; + G[a].push_back(Road(b, c)); + G[b].push_back(Road(a, c)); + } + for (int i = 1; i <= n; i++) + { + if (!V[i]) + { + CStart[nc] = i; + CCount[nc] = dfs(i); + nc++; + } + } + int res = 0; + for (int i = 0; i < nc; i++) + { + int totalc = 0; + Q.push(Road(CStart[i], 0)); + memset(V, 0, sizeof(V)); + while (totalc < CCount[i]) + { + Road item = Q.top(); + Q.pop(); + if (V[item.v]) + { + res += item.c; + continue; + } + V[item.v] = true; + totalc++; + for (int j = 0; j < G[item.v].size(); j++) + { + if (!V[G[item.v][j].v]) + { + Q.push(G[item.v][j]); + } + } + } + while (!Q.empty()) + { + res += Q.top().c; + Q.pop(); + } + } + printf("%d\n", res); + } + return 0; +} diff --git a/12342.cpp b/12342.cpp index c111a73..af9edba 100644 --- a/12342.cpp +++ b/12342.cpp @@ -1,35 +1,43 @@ -#include +#include -#define min(A, B) ((A) < (B) ? (A) : (B)) -#define max(A, B) ((A) > (B) ? (A) : (B)) -#define ceil(V) ((V - int(V) == 0) ? int(V) : int(V + 1) ) +using namespace std; -int main() { - int T; +int main() +{ + int T, C = 0, k; scanf("%d", &T); - for (int t = 1; t <= T; t++) { - double Inc, Tax = 0; - scanf("%lf", &Inc); - Inc -= 180000; - if (Inc > 0) { - Tax += (min(300000.0, Inc) * 10) / 100; - Inc -= 300000; + while (T--) + { + scanf("%d", &k); + printf("Case %d: ", ++C); + double tex = 0; + if (k > 180000) + { + k -= 180000; + if (k > 0) + { + tex += (k >= 300000 ? 300000 : k) * 0.1; + k -= 300000; + if (k > 0) + { + tex += (k >= 400000 ? 400000 : k) * 0.15; + k -= 400000; + if (k > 0) + { + tex += (k >= 300000 ? 300000 : k) * 0.2; + k -= 300000; + if (k > 0) + tex += k * 0.25; + } + } + } + if (tex < 2000) + puts("2000"); + else + printf("%d\n", (int)ceil(tex)); } - if (Inc > 0) { - Tax += (min(400000.0, Inc) * 15) / 100; - Inc -= 400000; - } - if (Inc > 0) { - Tax += (min(300000.0, Inc) * 20) / 100; - Inc -= 300000; - } - if (Inc > 0) { - Tax += (Inc * 25.0) / 100; - } - if (Tax > 0) { - Tax = max(2000.0, ceil(Tax)); - } - printf("Case %d: %.0lf\n", t, Tax); + else + puts("0"); } return 0; } diff --git a/12343.cpp b/12343.cpp new file mode 100644 index 0000000..b73a527 --- /dev/null +++ b/12343.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define ll unsigned long long + +int callen(ll x) +{ + if (x <= 1) + { + return 0; + } + int i; + ll p = 1LL; + i = 0; + while ((p << i) <= x) + { + i++; + } + return i - 1; +} + +//统计X到Yå¾—æŸä¸€ä½çš„1的个数,很容易å‘现规律的。 +//就是循环节为Z,一åŠæ˜¯0,一åŠæ˜¯1 +ll cal(ll x, ll y, ll z) +{ + ll tmp = (y - x + 1) / z; + ll mod = (y - x + 1) % z; + if (mod >= z / 2) + { + y = mod - z / 2; + } + else + { + y = 0; + } + return ((y + z / 2 * tmp) & (1LL)); +} +ll doit(ll x, int len) +{ + if (x < 1) + { + return 0; + } + ll p = 1LL; + ll ans = 0LL; + ans = (p << len); + for (int i = 1; i <= len; i++) + { + ll l, r; + l = (p << i); + r = min((p << (i + 1)) - 1, x); + ll sum = 0LL; + for (int j = 0; j < i; j++) + { + ll tmp = cal(l, r, 1LL << (j + 1)); + sum |= (tmp << j); + } + sum |= (((r - l + 1) & (1LL)) << i); + //printf("%I64d\n",sum); + ans ^= (sum << (len - i)); + } + return ans; +} +int main() +{ + int T, t, i; + int len1, len2; + ll l, r; + scanf("%d", &T); + for (t = 1; t <= T; t++) + { + scanf("%llu%llu", &l, &r); + //printf("%I64d %I64d\n",l,r); + len1 = callen(l - 1); + len2 = callen(r); + //printf("%d %d\n",len1,len2); + l = doit(l - 1, len1); + r = doit(r, len2); + ll ans = (l << (len2 - len1)) ^ r; + printf("Case %d: %llu\n", t, ans); + } + return 0; +} diff --git a/12345.cpp b/12345.cpp new file mode 100644 index 0000000..1d8b8a0 --- /dev/null +++ b/12345.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +int pSIZE, pCOUNT, Pval[256][256], Ppre[256][256], Plen[256], A[65536], P[65536]; + +void build(int n) +{ + int pxIdx = -1, pyIdx = pSIZE; + map PREV; + map::iterator it; + + memset(Plen, 0, sizeof(Plen)); + + for (int i = 0; i < n; i++) + { + if (pyIdx == pSIZE) + { + pxIdx++, pyIdx = 0; + } + Pval[pxIdx][pyIdx] = A[i]; + it = PREV.find(A[i]); + if (it == PREV.end()) + { + Ppre[pxIdx][pyIdx] = -1; + PREV[A[i]] = i; + } + else + { + Ppre[pxIdx][pyIdx] = it->second; + it->second = i; + } + P[i] = Ppre[pxIdx][pyIdx]; + pyIdx++; + Plen[pxIdx] = pyIdx; + } + + pCOUNT = pxIdx + 1; + + for (int i = 0; i < pCOUNT; i++) + { + sort(Pval[i], Pval[i] + Plen[i]); + sort(Ppre[i], Ppre[i] + Plen[i]); + } +} +int query(int L, int R) +{ + int B = L; + int ret = 0; + while (L % pSIZE && L <= R) + { + if (P[L] < B) + ret++; + L++; + } + while ((R + 1) % pSIZE && L <= R) + { + if (P[R] < B) + ret++; + R--; + } + if (L > R) + return ret; + L /= pSIZE, R /= pSIZE; + while (L <= R) + { + int cnt = upper_bound(Ppre[L], Ppre[L] + pSIZE, B - 1) - Ppre[L]; + ret += cnt; + L++; + } + return ret; +} +void updatePrev(int x) +{ + for (int i = x * pSIZE, j = 0; j < Plen[x]; i++, j++) + Ppre[x][j] = P[i]; + sort(Ppre[x], Ppre[x] + Plen[x]); +} +void update(int x) +{ + for (int i = x * pSIZE, j = 0; j < Plen[x]; i++, j++) + Pval[x][j] = A[i], Ppre[x][j] = P[i]; + sort(Pval[x], Pval[x] + Plen[x]); + sort(Ppre[x], Ppre[x] + Plen[x]); +} +void getNext(int x, int val, int &NEXT, int &nx) +{ + int y = x / pSIZE * pSIZE + Plen[x / pSIZE] - 1; + NEXT = 0x3f3f3f3f, nx = -1; + while (x % pSIZE && x <= y) + { + if (A[x] == val) + { + NEXT = x, nx = x / pSIZE; + return; + } + x++; + } + int L = x / pSIZE, R = pCOUNT - 1; + if (L * pSIZE < x) + return; + for (int i = L; i <= R; i++) + { + int pos = binary_search(Pval[i], Pval[i] + Plen[i], val); + if (pos == true) + { + NEXT = i * pSIZE; + while (A[NEXT] != val) + NEXT++; + nx = i; + return; + } + } +} +void getPrev(int y, int val, int &PREV, int &px) +{ + int x = 0; + PREV = -1, px = -1; + while ((y + 1) % pSIZE && x <= y) + { + if (A[y] == val) + { + PREV = y, px = y / pSIZE; + return; + } + y--; + } + int L = 0, R = y / pSIZE; + if (R * pSIZE > y) + return; + for (int i = R; i >= L; i--) + { + int pos = binary_search(Pval[i], Pval[i] + Plen[i], val); + if (pos == true) + { + PREV = i * pSIZE + Plen[i] - 1; + while (A[PREV] != val) + PREV--; + px = i; + return; + } + } +} +void modify(int X, int nval) +{ + if (A[X] == nval) + return; + int oval = A[X]; + int NEXT, PREV, nx, px; + PREV = P[X], px = -1; + if (PREV >= 0) + px = PREV / pSIZE; + + getNext(X + 1, oval, NEXT, nx); + if (nx != -1) + { + P[NEXT] = PREV; + updatePrev(nx); + } + + getNext(X + 1, nval, NEXT, nx); + if (nx != -1) + { + P[NEXT] = X; + updatePrev(nx); + } + + getPrev(X - 1, nval, PREV, px); + A[X] = nval, P[X] = PREV; + update(X / pSIZE); +} + +int main() +{ + int n, m, L, R; + char cmd[4]; + while (scanf("%d%d", &n, &m) == 2) + { + for (pSIZE = 1; pSIZE * pSIZE < n; pSIZE++) + ; + + for (int i = 0; i < n; i++) + { + scanf("%d", &A[i]); + } + build(n); + + while (m--) + { + scanf("%s%d%d", cmd, &L, &R); + if (cmd[0] == 'Q') + { + R--; + int r = query(L, R); + printf("%d\n", r); + } + else + { + modify(L, R); + } + } + } + return 0; +} diff --git a/12346.cpp b/12346.cpp new file mode 100644 index 0000000..e39e78b --- /dev/null +++ b/12346.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +long long F[32], C[32]; +struct option +{ + long long cost, flow; + bool operator<(const option &o) const + { + return flow < o.flow || (flow == o.flow && cost < o.cost); + } +} opt[(1 << 20) + 5]; + +int main() +{ + for (int cnum = 0, m, n, q, u, v; scanf("%d", &n) == 1;) + { + for (int i = 0; i < n; ++i) + { + scanf("%lld %lld", &F[i], &C[i]); + } + for (int i = 0; i < 1 << n; ++i) + { + opt[i].cost = opt[i].flow = 0; + for (int j = 0; j < n; ++j) + if (i & (1 << j)) + { + opt[i].cost += C[j]; + opt[i].flow += F[j]; + } + } + opt[1 << n].flow = 0; + sort(opt, opt + (1 << n)); + scanf("%d", &q); + for (long long T, V; cnum++ < q && scanf("%lld %lld", &V, &T) == 2;) + { + V = (V + T - 1) / T; + for (u = 0, v = 1 << n; u < v;) + { + m = (u + v) >> 1; + if (opt[m].flow < V) + { + u = m + 1; + } + else + { + v = m; + } + } + printf("Case %d: ", cnum); + if (opt[u].flow >= V) + { + long long min_cost = opt[u].cost; + while (++u < 1 << n) + if (opt[u].cost < min_cost) + { + min_cost = opt[u].cost; + } + printf("%lld\n", min_cost); + } + else + { + puts("IMPOSSIBLE"); + } + } + } + return 0; +} diff --git a/12347.cpp b/12347.cpp new file mode 100644 index 0000000..8cb7fc5 --- /dev/null +++ b/12347.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int NN = 10048; +struct node +{ + node *l, *r; + node *p; + int smv, v; +} n[NN]; +void post_order(node *n) +{ + if (n->l != NULL) + { + post_order(n->l); + } + if (n->r != NULL) + { + post_order(n->r); + } + printf("%d\n", n->v); +} +int main() +{ + node *c = NULL; + for (int i = 0, z; scanf("%d", &z) == 1; ++i) + { + n[i].v = z; + n[i].l = n[i].r = n[i].p = NULL; + n[i].smv = 1 << 20; + if (c == NULL) + { + c = n + i; + } + else if (z < c->v) + { + n[i].p = c; + n[i].smv = c->v - 1; + c = c->l = n + i; + } + else + { + while (c->smv < z) + { + c = c->p; + } + n[i].p = c; + n[i].smv = c->smv; + c = c->r = n + i; + } + } + post_order(n); + return 0; +} diff --git a/12349.cpp b/12349.cpp new file mode 100644 index 0000000..e101b20 --- /dev/null +++ b/12349.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + string g = "1362562009103927516181790697908402630750268671988097668597117777450735527854639563576129618821798992014472833017048220559487787332344887282230720904047852471891741753346204386294918168151772890219951332270658298362660628186041512451772056484590387955087269949474982621146017318119491514546991869648271932400619948094456004043863022635968291120563590445675620901082407103155632986806704622532360840996825822558112672762642769345471035445915290713846270177699946569990328845263378446890842244919134227178993535032097598933440284513533156850116646864071210887303064572399454044798215356122183768500016975527452811120829055353508873584616106772179431159176829871423795935837208781449105929847742228858623446965112744786500825318410061684214243018248670821856737745340051235323187388447395528370641514796690015328002186543346176418786428113554982377747750459154098816479022495074749948815916254425359841265834127833546047456669442589926588844444922830927760126283633937631418702992007132164919929784148317727124333587746382714421216543612197150137622585279680391455004874966637706966516495200756226943452775660642961859201372096194090880178644373327229364547555094819141164426781615856863542701027014969283148506566268029740493028162373587793880672289813797933830108657007642053845972662246051600529779947565703828023315859592096457917732729591995903040051900646050868107512178982246708590347390585704483207863574772553374585425239528990706699173928105285592228478808933344599927710007262458948996293160335797191503164952754774471916413324017078293689947337092097467528677048657651346838808649621500623361359421922543101040323830989265750551803777089044970554805845167358790289673219052375937503614023926370043950397473459412342796452229268349657428721734248423170315775660440693281771023546954339712976513099919898547513407034283025655932384054685870559490732064701561366809174652666891209304000566270609888954422544643269996778334022627705080772051686234408177703504523886131388215284057807612073921935686708448102325517865347532698085025812149137008551409761660546977028182628904984164522480413763163473444936190410788545002445098716963737426343654343863804518392328900035744402556546554738042170307121476048759772014918277984551382580802818096683708110840203514456294196609403904039968085613684728131383809454886244668692580199423153990355161292678391702923641538928845076539952784517286775221562951457542118453128350253051648156518303972918601076931830811719959537023497356410891590928247194878378993677585975261778405687390400573837814167923994619393103283197573985917144752885227420610966097252900845504997761776165937149565279201367059514630079398226833914594640925654594099332130992890674350009433450190926447371744195946316728602762150446481952761126798526465008387398169540737573005951293228032968596650369249451438534373421771951794003458284225722862311693838382865417712259815218216427261492909937096315326034991252139050863745845552317578401492133736760959903844939517570914321584607259184838588598504723420790120682862002354558119184334335654005093812000916556410872099455799342256430716777000794024994267080595230550559599878287525904667925309341545792784991695240039678222714254310070998733670792584665944663600074162960031415482009747966600343028613989131563952532883237176142662010485835316842973245031403070650871264359968240370714635457273644027101396300083268726582240179003628517080189317969878575736709"; + int tp = 8009, n, t; + while (scanf("%d%d", &n,&t) == 2) + { + if (n == 0 && t == 0) + { + break; + } + int req = n - g.size() - 4; + printf("%s", g.c_str()); + for (int i = 0; i < req; i++) + { + printf("0"); + } + printf("%d\n", tp); + } + return 0; +} diff --git a/1235.cpp b/1235.cpp new file mode 100644 index 0000000..09ef46e --- /dev/null +++ b/1235.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define MAX 501 + +int abs(int a) +{ + return a > 0 ? a : -a; +} + +int d(int a, int b) +{ + int result = 0; + for (int i = 0; i < 4; i++) + { + int aa = a % 10, bb = b % 10; + result += min(abs(aa - bb), 10 - abs(aa - bb)); + a /= 10; + b /= 10; + } + return result; +} + +int K[MAX], G[MAX][MAX], D[MAX], n; +bool V[MAX]; + +int updateD(int i) +{ + D[i] = 0; + for (int j = 0; j < n; j++) + { + if (G[i][j]) + { + D[j] = min(D[j], G[i][j]); + } + } +} + +int main() +{ + int _; scanf("%d", &_); + while (scanf("%d", &n) != EOF) + { + memset(V, 0, sizeof(V)); + memset(D, 0x3f, sizeof(D)); + for (int i = 0; i < n; i++) + { + scanf("%d", &K[i]); + } + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + G[i][j] = G[j][i] = d(K[i], K[j]); + } + } + int total = INT_MAX; + for (int i = 0; i < n; i++) + { + total = min(total, d(0, K[i])); + } + V[0] = true; + updateD(0); + for (int k = 1; k < n; k++) + { + int minn = INT_MAX, minv; + for (int i = 0; i < n; i++) + { + if (!V[i] && D[i] < minn) + { + minn = D[i]; + minv = i; + } + } + V[minv] = true; + updateD(minv); + total += minn; + } + printf("%d\n", total); + } + return 0; +} diff --git a/12350.cpp b/12350.cpp new file mode 100644 index 0000000..2896eb6 --- /dev/null +++ b/12350.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +int sg[26][1850]; +int per[] = {0, 1, 3, 3, 6, 12, 24, 12, 24, 24, 24, 24, 48, 48, 96, 96, 96, 192, 192, 384, 384, 384, 768, 768, 768, 768}; +int g1[] = {1}; +int g2[] = {-2, 2, 3}; +int g3[] = {1, 5, 3}; +int g4[] = {-1, 7, 3, 8, 10, 6}; +int g5[] = {-1, -2, 3, 2, -3, 6, 5, 4, 1, 8, 7, 12}; +int g6[] = {2, -2, 3, -5, 1, 6, 8, 4, 9, 11, 7, 12, 5, 10, 15, 17, 13, 18, 20, 16, 21, 23, 14, 24}; +int g7[] = {-11, -10, 3, 4, 5, 6, 7, -4, -3, 10, 11, 12}; +int g8[] = {1, -6, -10, 4, -3, -7, 7, 8, 9, 10, -4, 3, 12, 6, 15, 2, 11, 5, 16, 19, 13, 22, 23, 24}; +int g9[] = {11, 12, 13, 3, -1, 7, 8, 18, 9, 10, 20, 21, 14, 22, 25, 15, 26, 19, 28, 29, 30, 17, 16, 24}; +int g10[] = {8, -1, 13, 4, 14, 15, 12, 5, 9, 7, 10, 21, 11, 22, 25, 16, 26, 27, 17, 18, 30, 19, 20, 24}; +int g11[] = {-8, 1, -15, -10, -12, 7, 8, 6, -3, 10, 11, -1, 13, 3, 17, 2, 18, 19, 5, 20, 22, 4, 15, 24}; +int g12[] = {-4, -1, -9, 3, -6, 4, 8, -3, -11, 10, 1, 2, 9, -2, 5, 14, 6, 16, 19, 20, 17, 7, 11, 12, 25, 23, 15, 27, 18, 28, 32, 21, 13, 24, 35, 26, 33, 22, 29, 38, 30, 40, 43, 34, 41, 31, 36, 48}; +int g13[] = {-21, 1, -9, -8, -11, -6, 5, 8, 7, -1, 10, 11, 12, 13, 2, 14, 6, -4, -2, 9, 17, 20, 21, 23, 3, 25, 15, 4, 28, 16, 29, 32, 31, 33, 24, 18, 34, 36, 26, 38, 19, 30, 22, 43, 41, 35, 45, 48}; +int g14[] = {-19, -11, -9, -2, -20, 5, -12, -4, 3, -10, 10, 11, 6, 1, 2, 14, 16, 17, 18, 7, 20, 8, -1, 23, 24, 13, 15, 22, 4, 12, 31, 30, 27, 9, 19, 34, 35, 25, 26, 38, 40, 41, 42, 43, 32, 21, 33, 48, 29, 37, 39, 46, 28, 53, 36, 44, 51, 57, 45, 59, 54, 49, 50, 62, 64, 65, 66, 55, 68, 56, 47, 71, 58, 61, 63, 70, 52, 60, 79, 78, 75, 81, 67, 82, 83, 73, 74, 72, 88, 89, 90, 91, 80, 69, 93, 96}; +int g15[] = {24, 26, 9, 27, 20, 13, 14, 19, 32, 34, 36, 37, 38, 23, 15, 28, 25, 29, 41, 30, 31, 35, 46, 49, 43, 50, 33, 51, 44, 54, 40, 45, 56, 58, 39, 47, 55, 48, 64, 52, 66, 42, 65, 69, 70, 68, 60, 74, 72, 75, 57, 59, 76, 61, 53, 67, 80, 83, 63, 85, 86, 62, 71, 88, 73, 77, 89, 78, 79, 82, 84, 97, 91, 98, 81, 99, 92, 102, 90, 93, 104, 107, 87, 94, 103, 95, 112, 100, 114, 101, 113, 117, 118, 106, 108, 96}; +int g16[] = {-11, 17, 18, 1, 16, 6, 7, 21, 19, 25, 12, 28, 10, 14, 8, 30, 32, 20, 22, 15, 36, 24, 37, 40, 41, 42, 26, 43, 29, 23, 27, 47, 48, 33, 35, 50, 51, 52, 39, 55, 31, 57, 46, 44, 34, 45, 60, 65, 38, 64, 66, 67, 53, 54, 70, 69, 71, 74, 59, 76, 49, 61, 56, 79, 80, 68, 82, 63, 84, 58, 73, 72, 88, 62, 86, 90, 77, 93, 75, 95, 91, 81, 78, 98, 99, 100, 87, 101, 105, 89, 94, 92, 109, 107, 83, 96}; +int g17[] = {17, 20, -8, 21, 22, 3, 10, 25, 26, 28, 11, 15, 29, 30, 4, 6, 35, 36, 37, 18, 39, 23, 27, 24, 44, 45, 16, 32, 42, 19, 33, 50, 34, 51, 38, 53, 54, 55, 57, 56, 31, 60, 41, 62, 61, 63, 64, 48, 47, 68, 40, 70, 66, 43, 49, 46, 58, 77, 59, 79, 80, 76, 52, 65, 83, 84, 69, 85, 73, 71, 86, 72, 89, 75, 91, 93, 90, 67, 81, 98, 78, 82, 99, 87, 74, 103, 105, 104, 94, 108, 92, 110, 109, 95, 111, 96, 113, 101, 88, 117, 116, 119, 97, 121, 106, 124, 107, 127, 122, 126, 100, 102, 131, 132, 133, 114, 135, 136, 123, 120, 140, 125, 112, 118, 138, 115, 129, 146, 130, 147, 134, 128, 150, 151, 153, 152, 142, 141, 137, 158, 157, 159, 160, 144, 143, 149, 148, 166, 156, 139, 145, 169, 154, 173, 155, 175, 176, 172, 177, 161, 179, 164, 165, 162, 183, 167, 182, 168, 185, 171, 187, 189, 186, 163, 193, 194, 174, 178, 181, 197, 170, 199, 201, 200, 190, 204, 188, 206, 205, 180, 191, 192}; +int g18[] = {1, -19, 3, -18, -14, -9, 6, 8, -20, -10, -5, -4, 13, -7, 14, 15, 18, 2, 20, 21, 22, 19, 10, 7, 24, 5, 11, -1, 4, 29, 9, 33, 27, 12, 16, 36, 37, 17, 40, 41, 25, 26, 43, 45, 46, 23, 47, 31, 32, 30, 35, 49, 34, 39, 55, 56, 51, 28, 38, 42, 44, 61, 62, 48, 66, 50, 67, 68, 69, 70, 71, 72, 73, 53, 59, 54, 52, 63, 57, 81, 79, 82, 65, 83, 84, 86, 88, 58, 60, 74, 75, 93, 64, 90, 94, 77, 96, 78, 80, 98, 85, 87, 102, 104, 76, 107, 91, 92, 110, 89, 111, 112, 97, 115, 99, 114, 118, 116, 101, 103, 120, 121, 122, 95, 123, 125, 105, 129, 100, 108, 113, 131, 133, 134, 136, 106, 117, 109, 138, 141, 142, 119, 143, 127, 128, 126, 145, 132, 130, 135, 151, 152, 124, 156, 153, 140, 155, 137, 158, 144, 162, 163, 164, 165, 166, 146, 167, 168, 139, 149, 170, 150, 148, 159, 176, 177, 175, 147, 161, 180, 181, 179, 184, 154, 169, 157, 171, 189, 160, 186, 190, 192}; +int g19[] = {6, 7, 23, 10, 11, 27, 29, 31, 5, 33, 14, 35, 30, 32, 17, 18, 19, 39, 40, 25, 24, 42, 44, 47, 49, 50, 28, 34, 51, 38, 22, 37, 36, 26, 55, 59, 60, 43, 41, 46, 64, 65, 66, 68, 48, 54, 67, 72, 52, 57, 73, 58, 45, 75, 78, 61, 79, 63, 80, 84, 81, 53, 85, 86, 71, 56, 76, 91, 92, 62, 94, 95, 96, 97, 98, 82, 69, 83, 70, 87, 77, 74, 90, 106, 88, 105, 89, 109, 112, 111, 113, 116, 117, 115, 93, 103, 100, 120, 119, 122, 104, 102, 125, 107, 127, 130, 108, 99, 129, 101, 114, 118, 136, 135, 138, 137, 141, 110, 128, 143, 145, 121, 124, 126, 146, 131, 149, 133, 132, 152, 151, 123, 156, 139, 140, 142, 159, 160, 144, 164, 165, 150, 134, 147, 148, 168, 153, 171, 169, 155, 174, 157, 175, 179, 154, 163, 181, 178, 161, 182, 167, 183, 172, 184, 173, 158, 188, 191, 176, 177, 162, 194, 193, 196, 166, 200, 180, 170, 186, 187, 204, 205, 185, 190, 208, 207, 209, 212, 213, 195, 189, 192, 198, 199, 201, 202, 203, 219, 221, 223, 197, 225, 206, 227, 222, 224, 210, 214, 211, 215, 232, 217, 216, 234, 236, 239, 241, 242, 220, 226, 243, 230, 245, 229, 228, 218, 247, 231, 252, 235, 233, 238, 256, 257, 258, 260, 240, 246, 259, 264, 244, 249, 265, 250, 237, 251, 270, 253, 271, 255, 272, 276, 273, 274, 277, 278, 263, 248, 268, 283, 284, 254, 286, 287, 288, 289, 290, 291, 261, 275, 262, 279, 269, 266, 282, 267, 280, 297, 281, 301, 304, 303, 305, 308, 309, 307, 285, 295, 292, 312, 311, 298, 296, 294, 317, 299, 319, 322, 300, 324, 321, 293, 306, 310, 328, 327, 330, 329, 333, 302, 320, 335, 337, 313, 316, 318, 338, 323, 341, 325, 344, 345, 314, 315, 348, 331, 332, 334, 351, 352, 336, 356, 357, 342, 326, 339, 340, 343, 361, 360, 362, 347, 366, 349, 367, 371, 346, 355, 373, 370, 353, 374, 359, 375, 364, 376, 365, 350, 380, 383, 368, 369, 354, 386, 385, 388, 358, 392, 372, 393, 378, 363, 396, 397, 377, 382, 400, 399, 379, 404, 405, 387, 381, 384}; +int g20[] = {-16, 2, -3, 1, 4, 5, -27, 9, -9, 11, 12, -21, -11, -7, -2, -17, 18, 19, 20, -4, 6, 21, 22, -8, 8, 24, 7, 27, 29, 14, 30, 16, 32, 15, 13, 3, 10, 17, 40, 39, 42, 26, 41, 46, 28, 43, 23, 25, 33, 50, 45, 31, 51, 36, 37, 54, 55, 38, 56, 35, 60, 59, 63, 64, 47, 66, 34, 65, 49, 48, 52, 73, 57, 72, 76, 77, 44, 78, 61, 81, 79, 82, 83, 84, 58, 53, 62, 67, 68, 90, 69, 94, 95, 91, 71, 96, 97, 80, 74, 100, 98, 70, 85, 104, 102, 86, 106, 75, 108, 89, 92, 110, 114, 115, 113, 116, 119, 120, 118, 87, 88, 123, 103, 124, 107, 101, 93, 112, 122, 111, 109, 99, 134, 135, 136, 137, 138, 121, 105, 125, 140, 126, 143, 127, 128, 146, 141, 145, 131, 117, 133, 152, 151, 157, 153, 158, 156, 159, 139, 160, 162, 129, 130, 132, 166, 144, 148, 150, 169, 168, 170, 172, 154, 142, 176, 178, 179, 177, 180, 147, 182, 149, 163, 183, 164, 186, 155, 173, 191, 174, 167, 192, 193, 161, 189, 195, 196, 197, 165, 201, 198, 203, 204, 171, 181, 185, 190, 175, 187, 208, 194, 188, 214, 213, 215, 184, 200, 216, 199, 219, 221, 206, 223, 225, 222, 207, 205, 209, 202, 226, 211, 231, 232, 218, 212, 238, 220, 234, 239, 217, 224, 242, 210, 241, 233, 244, 229, 246, 247, 230, 248, 227, 252, 237, 255, 235, 258, 256, 236, 228, 262, 240, 261, 265, 249, 264, 268, 269, 251, 270, 253, 273, 271, 274, 275, 243, 250, 245, 254, 259, 260, 282, 281, 286, 287, 283, 263, 288, 257, 272, 266, 291, 290, 276, 277, 296, 294, 278, 298, 267, 300, 297, 284, 302, 306, 289, 305, 308, 311, 312, 310, 279, 280, 315, 295, 292, 299, 293, 285, 304, 314, 301, 320, 307, 303, 326, 328, 327, 329, 313, 330, 317, 316, 318, 332, 319, 321, 337, 322, 340, 323, 309, 325, 344, 338, 349, 345, 350, 343, 333, 331, 352, 335, 353, 354, 324, 358, 336, 356, 342, 361, 360, 362, 364, 346, 334, 348, 370, 351, 369, 339, 372, 371, 341, 355, 374, 378, 379, 347, 365, 383, 366, 359, 384}; +int g21[] = {-3, 19, 1, 20, 18, 4, -10, 26, 24, -8, 29, 8, 30, 31, 13, -2, 16, 36, 17, 34, 38, 6, 39, 23, 21, 44, 9, 41, 25, 28, 27, 49, 46, 50, 51, 32, 33, 53, 37, 22, 35, 40, 60, 42, 43, 62, 63, 48, 64, 67, 47, 65, 57, 69, 45, 52, 73, 55, 74, 54, 71, 78, 59, 81, 56, 80, 61, 66, 86, 72, 84, 90, 91, 89, 93, 58, 92, 95, 75, 98, 96, 82, 79, 77, 101, 68, 83, 70, 85, 107, 87, 88, 76, 94, 111, 113, 97, 114, 109, 99, 115, 100, 103, 121, 119, 106, 123, 104, 126, 125, 127, 102, 131, 112, 108, 133, 136, 116, 135, 137, 117, 120, 105, 139, 122, 141, 124, 110, 144, 130, 145, 128, 129, 146, 132, 118, 155, 156, 138, 158, 140, 154, 160, 161, 159, 163, 143, 165, 164, 166, 134, 169, 170, 151, 153, 150, 149, 173, 157, 142, 179, 177, 178, 162, 147, 148, 183, 185, 152, 186, 167, 171, 187, 172, 174, 195, 191, 168, 175, 176, 199, 197, 180, 200, 181, 203, 204, 206, 188, 208, 207, 192, 189, 194, 193, 209, 210, 196, 182, 218, 216, 184, 221, 222, 223, 224, 205, 190, 202, 226, 228, 227, 211, 198, 230, 212, 213, 236, 201, 233, 217, 215, 219, 220, 240, 241, 242, 244, 246, 245, 225, 214, 229, 232, 231, 234, 235, 238, 256, 258, 254, 259, 239, 260, 261, 262, 237, 264, 265, 247, 249, 251, 263, 270, 272, 273, 248, 275, 253, 277, 243, 257, 276, 255, 282, 266, 285, 250, 284, 252, 267, 290, 288, 274, 271, 269, 292, 294, 296, 279, 280, 299, 300, 303, 268, 286, 283, 305, 281, 306, 289, 308, 309, 310, 278, 313, 311, 295, 315, 318, 319, 293, 297, 298, 287, 304, 324, 323, 291, 326, 325, 329, 331, 312, 328, 317, 314, 333, 316, 302, 301, 322, 338, 320, 321, 307, 345, 346, 327, 347, 330, 350, 348, 351, 352, 336, 353, 354, 335, 357, 358, 356, 340, 361, 362, 343, 363, 342, 341, 332, 367, 334, 349, 371, 337, 373, 339, 360, 375, 377, 344, 378, 359, 365, 379, 364, 366, 387, 383, 370, 386, 368, 355, 389, 369, 391, 395, 372, 394, 398, 380, 396, 399, 384}; +int g22[] = {20, 3, 7, 24, 22, 2, 28, 11, 27, 15, 31, 35, 34, 33, 1, 36, 18, 39, 23, 21, 19, 41, 25, 46, 30, 50, 42, 49, 32, 51, 17, 26, 29, 54, 37, 58, 40, 55, 62, 63, 43, 64, 47, 44, 65, 66, 69, 45, 67, 73, 74, 38, 56, 75, 57, 77, 52, 53, 80, 60, 83, 82, 48, 70, 59, 88, 89, 90, 92, 87, 71, 96, 68, 61, 95, 76, 79, 84, 81, 78, 102, 104, 103, 106, 85, 72, 109, 108, 91, 113, 110, 98, 97, 114, 100, 99, 101, 94, 86, 122, 121, 123, 124, 107, 127, 129, 93, 130, 112, 126, 115, 133, 111, 137, 138, 117, 142, 118, 105, 119, 125, 143, 144, 128, 145, 132, 148, 150, 147, 116, 151, 152, 131, 155, 120, 134, 136, 135, 160, 140, 141, 162, 166, 167, 163, 169, 170, 165, 164, 172, 153, 146, 139, 149, 175, 156, 157, 158, 154, 182, 161, 185, 186, 184, 188, 173, 189, 191, 192, 190, 174, 171, 193, 159, 176, 177, 180, 183, 197, 179, 178, 205, 168, 203, 187, 181, 206, 212, 210, 208, 196, 214, 198, 195, 213, 217, 200, 194, 220, 224, 201, 207, 223, 228, 226, 225, 209, 229, 202, 232, 234, 230, 199, 233, 237, 215, 204, 219, 238, 241, 239, 242, 245, 218, 211, 227, 248, 250, 252, 216, 253, 231, 235, 256, 221, 222, 257, 258, 262, 240, 236, 243, 246, 266, 263, 244, 249, 270, 271, 255, 272, 275, 251, 254, 259, 279, 260, 261, 281, 278, 247, 269, 265, 267, 268, 288, 286, 291, 290, 276, 273, 296, 294, 297, 295, 298, 277, 274, 264, 300, 280, 282, 284, 285, 289, 306, 292, 287, 293, 312, 313, 314, 315, 316, 317, 299, 283, 301, 302, 324, 304, 321, 307, 325, 308, 303, 330, 305, 333, 310, 334, 309, 318, 335, 319, 320, 337, 339, 340, 342, 322, 323, 344, 345, 328, 311, 329, 326, 331, 327, 353, 332, 356, 351, 358, 336, 355, 360, 354, 357, 359, 362, 347, 338, 364, 341, 350, 348, 349, 369, 346, 374, 375, 352, 377, 376, 343, 378, 361, 383, 381, 382, 366, 363, 368, 372, 389, 393, 390, 392, 365, 371, 370, 396, 367, 397, 379, 373, 398, 380, 402, 400, 388, 384, 404, 387, 391, 408, 406, 386, 412, 395, 394, 399, 415, 419, 418, 417, 385, 420, 424, 423, 407, 405, 403, 425, 409, 430, 414, 411, 426, 433, 416, 434, 401, 410, 413, 438, 421, 442, 443, 439, 446, 447, 427, 448, 431, 428, 449, 450, 453, 429, 451, 435, 457, 422, 440, 459, 441, 461, 436, 437, 464, 444, 467, 466, 432, 454, 471, 472, 473, 474, 476, 477, 455, 480, 452, 445, 479, 460, 463, 458, 465, 462, 468, 488, 486, 490, 469, 456, 493, 492, 475, 497, 494, 482, 481, 498, 484, 483, 485, 478, 470, 506, 505, 507, 508, 491, 511, 513, 514, 515, 496, 510, 499, 517, 495, 521, 522, 501, 487, 502, 489, 503, 509, 527, 528, 512, 529, 516, 532, 534, 531, 500, 535, 536, 520, 539, 504, 518, 523, 519, 544, 524, 525, 546, 550, 526, 547, 553, 551, 549, 548, 554, 537, 530, 560, 533, 559, 540, 541, 542, 538, 566, 545, 569, 570, 568, 572, 557, 573, 575, 556, 574, 558, 555, 577, 543, 582, 561, 564, 567, 581, 563, 562, 589, 552, 587, 571, 565, 590, 596, 594, 592, 580, 576, 599, 579, 597, 601, 584, 578, 604, 608, 585, 591, 607, 612, 610, 609, 593, 613, 586, 616, 618, 614, 583, 617, 621, 622, 588, 598, 623, 625, 627, 626, 629, 602, 595, 611, 632, 634, 636, 600, 637, 615, 619, 603, 605, 606, 641, 642, 646, 624, 620, 649, 630, 650, 647, 628, 633, 654, 655, 639, 656, 659, 635, 638, 643, 663, 644, 645, 665, 662, 631, 653, 669, 651, 652, 672, 670, 675, 674, 640, 657, 680, 660, 681, 678, 682, 661, 658, 648, 684, 664, 666, 668, 671, 673, 690, 676, 692, 677, 696, 679, 698, 699, 700, 701, 683, 667, 685, 686, 708, 688, 705, 691, 709, 712, 687, 714, 689, 717, 694, 718, 693, 697, 719, 703, 704, 721, 702, 724, 726, 706, 707, 728, 729, 731, 695, 713, 710, 715, 711, 737, 716, 740, 735, 742, 720, 739, 723, 738, 741, 743, 746, 749, 722, 748, 725, 734, 732, 733, 753, 730, 758, 759, 736, 761, 760, 727, 762, 745, 767, 765, 766, 750, 747, 752, 756, 773, 777, 774, 776, 778, 755, 754, 780, 744, 781, 763, 757, 782, 764, 751, 784, 772, 768}; +int g23[] = {-23, -15, -18, -33, -14, 5, 7, 10, 8, -27, 13, 12, 14, 15, -6, -3, -2, -12, 20, 16, 2, -1, 23, 25, 1, 9, 24, 28, 26, -7, 6, 33, 11, 17, 35, 37, 38, 39, 18, 3, 4, 43, 22, 46, 45, 19, 27, 30, 48, 52, 32, 51, 53, 31, 55, 56, 34, 21, 40, 61, 62, 44, 36, 41, 65, 67, 29, 47, 50, 66, 68, 75, 73, 57, 54, 60, 58, 49, 42, 82, 59, 83, 84, 64, 85, 86, 88, 89, 70, 92, 63, 94, 71, 93, 95, 97, 98, 99, 76, 72, 77, 79, 103, 105, 104, 69, 108, 91, 110, 107, 111, 74, 115, 116, 78, 90, 96, 80, 81, 102, 122, 123, 87, 120, 124, 125, 127, 129, 106, 130, 131, 112, 134, 135, 114, 117, 100, 109, 101, 119, 142, 138, 139, 146, 121, 118, 145, 149, 148, 113, 126, 153, 152, 137, 154, 136, 133, 140, 141, 144, 163, 132, 165, 143, 167, 128, 168, 147, 171, 172, 170, 151, 173, 174, 155, 150, 175, 156, 176, 160, 178, 157, 164, 184, 162, 161, 166, 169, 191, 187, 189, 193, 194, 195, 158, 159, 192, 197, 179, 177, 182, 203, 202, 181, 205, 206, 207, 208, 190, 185, 188, 213, 214, 215, 216, 198, 180, 199, 217, 183, 218, 221, 186, 225, 224, 209, 227, 229, 231, 232, 212, 233, 196, 204, 235, 210, 237, 200, 201, 222, 238, 219, 220, 245, 226, 223, 230, 211, 250, 251, 249, 254, 255, 252, 228, 240, 234, 236, 258, 239, 242, 257, 263, 241, 266, 244, 246, 247, 248, 270, 269, 253, 273, 274, 276, 256, 277, 260, 280, 279, 243, 261, 282, 265, 262, 285, 259, 289, 272, 267, 268, 264, 290, 271, 275, 297, 296, 281, 299, 283, 278, 300, 284, 304, 306, 307, 286, 309, 287, 305, 310, 291, 288, 315, 308, 312, 316, 318, 294, 295, 298, 302, 320, 324, 325, 301, 329, 327, 292, 293, 303, 311, 314, 330, 334, 337, 313, 339, 340, 341, 342, 343, 326, 323, 344, 349, 347, 348, 328, 350, 332, 336, 331, 333, 317, 335, 319, 354, 321, 322, 362, 363, 364, 365, 366, 367, 368, 346, 369, 371, 372, 352, 373, 356, 375, 338, 355, 353, 358, 359, 360, 345, 379, 384, 361, 387, 386, 351, 370, 389, 391, 394, 392, 357, 376, 396, 398, 397, 378, 381, 382, 377, 380, 400, 406, 383, 407, 409, 385, 374, 408, 412, 410, 413, 390, 417, 395, 401, 419, 421, 422, 423, 402, 399, 388, 404, 428, 430, 429, 393, 403, 414, 432, 411, 416, 436, 435, 415, 439, 440, 418, 405, 424, 427, 446, 447, 420, 425, 450, 451, 452, 431, 434, 449, 456, 437, 457, 441, 438, 444, 442, 433, 426, 445, 443, 467, 465, 448, 469, 470, 472, 473, 454, 476, 474, 478, 455, 477, 459, 481, 479, 482, 460, 485, 461, 463, 487, 466, 488, 453, 493, 475, 494, 491, 492, 458, 499, 468, 462, 498, 480, 464, 500, 486, 506, 483, 471, 504, 508, 509, 511, 513, 490, 514, 515, 496, 518, 519, 520, 489, 484, 521, 495, 503, 526, 522, 523, 529, 505, 502, 530, 533, 532, 497, 510, 537, 536, 501, 538, 540, 517, 524, 525, 507, 547, 516, 549, 527, 528, 512, 552, 531, 551, 554, 555, 535, 556, 557, 539, 534, 559, 561, 560, 544, 562, 541, 548, 568, 546, 545, 550, 553, 575, 571, 573, 576, 578, 579, 542, 543, 580, 581, 563, 558, 566, 587, 586, 565, 589, 590, 591, 592, 574, 569, 572, 597, 598, 599, 600, 582, 564, 583, 602, 567, 606, 577, 570, 609, 608, 593, 611, 613, 615, 616, 596, 617, 595, 588, 601, 594, 621, 584, 585, 607, 622, 603, 604, 629, 610, 628, 614, 633, 634, 635, 636, 619, 638, 639, 612, 624, 618, 605, 642, 623, 626, 641, 627, 625, 650, 648, 630, 631, 632, 654, 653, 637, 656, 620, 657, 640, 661, 644, 664, 663, 646, 645, 666, 647, 668, 669, 643, 673, 649, 651, 652, 677, 658, 655, 659, 681, 680, 665, 683, 667, 662, 684, 687, 688, 670, 660, 693, 694, 671, 689, 674, 675, 672, 699, 692, 696, 700, 701, 678, 679, 682, 686, 704, 708, 710, 685, 713, 711, 676, 690, 714, 695, 698, 716, 691, 721, 697, 722, 723, 725, 718, 726, 702, 707, 728, 733, 731, 732, 709, 734, 712, 720, 715, 717, 740, 719, 703, 738, 705, 706, 746, 724, 747, 727, 748, 749, 752, 730, 751, 755, 757, 736, 758, 759, 760, 729, 739, 737, 742, 743, 744, 764, 763, 768}; +int g24[] = {1, 2, 3, -25, 4, -34, -33, -14, -19, -17, -9, -28, -12, 15, 12, 14, 17, 19, -8, 20, -3, 21, 23, 25, 5, 28, 22, 29, -10, 7, -1, 8, 31, 33, 32, 37, 13, 36, 40, 39, 18, 16, 11, 27, 6, 45, 9, 48, 10, 24, 46, 51, 26, 53, 55, 56, 57, 58, 59, 38, 62, 63, 44, 41, 42, 43, 35, 68, 70, 30, 71, 34, 50, 47, 76, 74, 73, 52, 77, 82, 80, 60, 64, 84, 61, 67, 88, 86, 49, 66, 89, 69, 54, 93, 72, 97, 94, 99, 79, 100, 101, 102, 103, 105, 65, 75, 85, 87, 78, 108, 91, 92, 83, 90, 114, 96, 120, 112, 118, 81, 122, 95, 104, 107, 124, 125, 127, 98, 106, 128, 109, 113, 110, 135, 136, 133, 138, 115, 117, 141, 121, 140, 144, 126, 142, 119, 145, 123, 149, 111, 129, 130, 153, 132, 154, 116, 157, 156, 158, 137, 163, 161, 139, 160, 167, 162, 168, 143, 171, 131, 147, 155, 134, 150, 175, 152, 176, 151, 179, 178, 182, 148, 183, 159, 146, 186, 185, 166, 169, 187, 190, 193, 170, 192, 173, 195, 196, 172, 198, 202, 199, 177, 164, 165, 181, 180, 207, 188, 208, 210, 189, 211, 174, 191, 216, 217, 194, 200, 214, 197, 220, 221, 184, 225, 226, 224, 223, 209, 205, 228, 204, 212, 233, 234, 231, 213, 237, 215, 201, 218, 238, 203, 241, 245, 206, 246, 247, 248, 250, 251, 229, 222, 252, 253, 235, 254, 232, 219, 227, 240, 261, 257, 263, 242, 266, 239, 264, 230, 265, 244, 270, 243, 273, 275, 236, 276, 277, 278, 280, 255, 260, 256, 259, 267, 262, 285, 286, 249, 287, 268, 272, 269, 293, 294, 295, 297, 274, 258, 271, 279, 300, 302, 301, 281, 282, 283, 284, 288, 289, 290, 312, 313, 314, 292, 315, 291, 316, 318, 319, 298, 320, 321, 322, 303, 325, 326, 299, 308, 296, 306, 307, 305, 334, 310, 336, 335, 337, 311, 309, 341, 333, 340, 304, 317, 345, 324, 344, 349, 346, 348, 332, 327, 328, 354, 323, 352, 357, 331, 330, 338, 360, 363, 339, 365, 362, 342, 368, 371, 329, 343, 366, 372, 350, 374, 375, 347, 378, 353, 355, 381, 361, 358, 382, 384, 385, 387, 388, 359, 389, 351, 391, 370, 392, 367, 393, 356, 398, 399, 397, 400, 402, 403, 404, 364, 406, 407, 408, 369, 409, 411, 410, 373, 383, 413, 376, 377, 415, 417, 379, 380, 422, 420, 396, 423, 386, 426, 395, 430, 390, 412, 431, 432, 394, 434, 433, 436, 437, 438, 439, 416, 401, 441, 421, 405, 446, 427, 428, 425, 450, 451, 419, 424, 454, 414, 456, 418, 458, 459, 435, 460, 457, 445, 461, 440, 463, 444, 448, 468, 429, 443, 472, 447, 473, 476, 474, 453, 475, 455, 478, 477, 442, 482, 464, 485, 483, 486, 487, 488, 449, 465, 469, 452, 462, 494, 495, 471, 467, 479, 498, 480, 481, 496, 502, 504, 506, 466, 507, 484, 508, 470, 489, 490, 511, 492, 493, 497, 518, 519, 520, 500, 522, 499, 501, 525, 505, 524, 528, 510, 526, 491, 530, 503, 533, 534, 513, 514, 538, 516, 509, 541, 542, 540, 543, 521, 547, 545, 523, 544, 529, 546, 552, 512, 553, 515, 531, 517, 527, 557, 539, 536, 560, 535, 563, 562, 566, 532, 567, 568, 569, 551, 571, 550, 575, 572, 574, 537, 554, 576, 577, 579, 580, 556, 582, 586, 583, 559, 548, 549, 565, 564, 591, 592, 593, 555, 573, 595, 558, 596, 600, 561, 578, 584, 598, 581, 601, 604, 607, 609, 570, 608, 611, 613, 589, 612, 588, 615, 617, 618, 619, 597, 621, 599, 585, 594, 622, 587, 625, 605, 590, 606, 630, 610, 634, 635, 636, 614, 638, 639, 640, 641, 602, 603, 620, 616, 645, 642, 624, 648, 626, 623, 627, 633, 649, 628, 654, 632, 629, 631, 658, 657, 637, 661, 664, 665, 644, 666, 643, 651, 670, 646, 671, 673, 650, 652, 667, 653, 647, 678, 677, 656, 681, 682, 655, 663, 660, 684, 662, 668, 690, 689, 669, 672, 676, 674, 693, 696, 679, 659, 697, 675, 700, 685, 702, 704, 705, 706, 707, 687, 686, 710, 683, 692, 680, 691, 715, 717, 718, 694, 698, 719, 721, 695, 723, 699, 722, 703, 688, 728, 729, 708, 701, 733, 730, 711, 716, 713, 712, 738, 714, 720, 741, 736, 742, 745, 725, 746, 747, 709, 748, 726, 752, 755, 753, 727, 750, 757, 732, 724, 739, 731, 762, 737, 761, 744, 763, 764, 766, 768}; +int g25[] = {5, 25, -15, 26, 27, 28, 1, 6, 30, 32, 31, 8, -5, 34, 36, 37, 40, 20, 42, 39, 15, 4, 22, 48, 23, 51, 46, 11, 21, 52, 35, 55, 54, 56, 29, 18, 58, 59, 63, 64, 41, 65, 24, 68, 44, 69, 47, 71, 67, 50, 33, 74, 49, 45, 53, 38, 80, 62, 82, 83, 43, 61, 84, 86, 87, 66, 89, 91, 93, 90, 92, 97, 75, 95, 57, 76, 73, 60, 77, 79, 78, 105, 101, 107, 104, 106, 81, 109, 70, 114, 72, 116, 113, 117, 118, 115, 96, 119, 102, 99, 103, 121, 85, 128, 129, 108, 88, 130, 132, 131, 110, 133, 94, 138, 139, 135, 98, 100, 137, 142, 112, 122, 144, 120, 147, 125, 150, 126, 111, 124, 151, 152, 156, 158, 134, 159, 140, 136, 160, 164, 165, 123, 143, 127, 149, 146, 168, 171, 141, 172, 148, 175, 166, 178, 155, 177, 180, 176, 179, 182, 161, 162, 145, 154, 163, 183, 167, 191, 188, 170, 174, 153, 169, 194, 198, 157, 197, 199, 202, 173, 181, 203, 204, 206, 207, 186, 185, 184, 213, 210, 190, 192, 189, 215, 216, 218, 219, 220, 193, 225, 222, 195, 226, 201, 228, 187, 230, 231, 205, 233, 208, 209, 211, 212, 196, 237, 240, 200, 239, 241, 223, 224, 221, 214, 246, 247, 249, 251, 229, 252, 253, 255, 254, 232, 257, 217, 236, 258, 262, 244, 245, 238, 264, 268, 227, 265, 250, 271, 248, 274, 272, 234, 235, 276, 277, 279, 280, 256, 259, 242, 243, 282, 286, 260, 263, 266, 269, 273, 261, 289, 267, 270, 296, 298, 293, 299, 300, 275, 281, 301, 302, 304, 305, 278, 308, 309, 306, 284, 288, 312, 310, 314, 290, 313, 291, 285, 294, 320, 295, 324, 283, 292, 321, 326, 328, 287, 330, 297, 333, 303, 307, 315, 311, 337, 334, 316, 339, 317, 323, 318, 340, 341, 345, 343, 348, 325, 351, 349, 329, 332, 331, 322, 357, 353, 359, 319, 356, 338, 360, 336, 364, 363, 365, 366, 342, 327, 347, 372, 373, 374, 346, 375, 335, 377, 355, 352, 380, 378, 358, 383, 384, 344, 386, 385, 361, 387, 367, 394, 350, 370, 371, 354, 395, 397, 362, 398, 401, 381, 399, 376, 404, 402, 382, 407, 389, 368, 369, 409, 410, 412, 388, 390, 415, 396, 391, 392, 379, 422, 420, 403, 425, 400, 427, 393, 429, 426, 406, 431, 408, 434, 428, 435, 430, 436, 413, 405, 414, 418, 437, 416, 423, 419, 446, 447, 444, 424, 450, 453, 451, 411, 454, 432, 455, 457, 417, 456, 433, 461, 439, 421, 440, 464, 465, 466, 469, 445, 468, 470, 449, 448, 474, 442, 452, 471, 476, 480, 438, 479, 441, 460, 443, 481, 458, 463, 486, 459, 488, 491, 492, 467, 473, 475, 494, 472, 497, 500, 501, 502, 478, 462, 503, 505, 485, 483, 487, 477, 510, 511, 512, 513, 515, 493, 516, 490, 518, 498, 495, 496, 499, 522, 482, 484, 525, 527, 504, 506, 489, 530, 531, 509, 533, 535, 507, 508, 537, 536, 514, 542, 543, 539, 519, 520, 544, 521, 523, 547, 524, 526, 549, 528, 553, 555, 529, 552, 517, 532, 550, 540, 560, 541, 565, 566, 563, 567, 545, 546, 571, 538, 548, 570, 551, 575, 574, 534, 558, 561, 557, 580, 556, 564, 581, 559, 582, 586, 589, 587, 588, 591, 592, 593, 569, 554, 598, 572, 597, 576, 573, 562, 600, 579, 603, 604, 577, 609, 606, 610, 568, 585, 612, 584, 590, 594, 596, 616, 618, 614, 578, 619, 595, 623, 624, 583, 625, 626, 602, 608, 605, 607, 630, 631, 634, 615, 613, 635, 637, 639, 638, 599, 601, 617, 620, 642, 646, 647, 629, 622, 641, 651, 611, 621, 653, 628, 657, 655, 654, 627, 658, 659, 663, 664, 665, 640, 643, 633, 668, 666, 669, 644, 672, 632, 670, 648, 636, 645, 675, 680, 649, 679, 681, 682, 683, 684, 687, 685, 661, 689, 662, 650, 660, 652, 690, 674, 677, 656, 694, 699, 696, 697, 701, 703, 702, 705, 673, 708, 667, 676, 686, 710, 713, 671, 688, 714, 691, 712, 716, 678, 700, 698, 718, 721, 693, 706, 725, 727, 704, 729, 707, 730, 711, 709, 692, 733, 735, 734, 695, 740, 741, 738, 739, 743, 724, 744, 746, 715, 717, 748, 731, 751, 726, 732, 750, 756, 757, 754, 737, 758, 719, 761, 720, 762, 722, 723, 742, 736, 766, 728, 770, 747, 745, 771, 775, 778, 752, 777, 755, 749, 759, 781, 780, 782, 785, 765, 784, 760, 764, 787, 767, 768}; +void cal() +{ + bool vis[5000]; + for (int i = 1; i < 26; i++) + for (int j = 1; j <= 1801; j++) + { + memset(vis, 0, sizeof(vis)); + for (int k = 1; k < j; k++) + vis[sg[i][j - k]] = 1; + for (int k = 1; k < i; k++) + vis[sg[i - k][j]] = 1; + for (int k = 1; k < i && k < j; k++) + vis[sg[i - k][j - k]] = 1; + for (int k = 0;; k++) + if (vis[k] == 0) + { + sg[i][j] = k; + break; + } + } +} +long long f(int x, long long y) +{ + if (y < 1800) + return sg[x][y]; + if (x == 1) + return sg[1][1799] + (y - 1800) / per[1] * g1[0] + g1[(y - 1800) % per[1]]; + if (x == 2) + return sg[2][1799] + (y - 1800) / per[2] * g2[per[2] - 1] + g2[(y - 1800) % per[2]]; + if (x == 3) + return sg[3][1799] + (y - 1800) / per[3] * g3[per[3] - 1] + g3[(y - 1800) % per[3]]; + if (x == 4) + return sg[4][1799] + (y - 1800) / per[4] * g4[per[4] - 1] + g4[(y - 1800) % per[4]]; + if (x == 5) + return sg[5][1799] + (y - 1800) / per[5] * g5[per[5] - 1] + g5[(y - 1800) % per[5]]; + if (x == 6) + return sg[6][1799] + (y - 1800) / per[6] * g6[per[6] - 1] + g6[(y - 1800) % per[6]]; + if (x == 7) + return sg[7][1799] + (y - 1800) / per[7] * g7[per[7] - 1] + g7[(y - 1800) % per[7]]; + if (x == 8) + return sg[8][1799] + (y - 1800) / per[8] * g8[per[8] - 1] + g8[(y - 1800) % per[8]]; + if (x == 9) + return sg[9][1799] + (y - 1800) / per[9] * g9[per[9] - 1] + g9[(y - 1800) % per[9]]; + if (x == 10) + return sg[10][1799] + (y - 1800) / per[10] * g10[per[10] - 1] + g10[(y - 1800) % per[10]]; + if (x == 11) + return sg[11][1799] + (y - 1800) / per[11] * g11[per[11] - 1] + g11[(y - 1800) % per[11]]; + if (x == 12) + return sg[12][1799] + (y - 1800) / per[12] * g12[per[12] - 1] + g12[(y - 1800) % per[12]]; + if (x == 13) + return sg[13][1799] + (y - 1800) / per[13] * g13[per[13] - 1] + g13[(y - 1800) % per[13]]; + if (x == 14) + return sg[14][1799] + (y - 1800) / per[14] * g14[per[14] - 1] + g14[(y - 1800) % per[14]]; + if (x == 15) + return sg[15][1799] + (y - 1800) / per[15] * g15[per[15] - 1] + g15[(y - 1800) % per[15]]; + if (x == 16) + return sg[16][1799] + (y - 1800) / per[16] * g16[per[16] - 1] + g16[(y - 1800) % per[16]]; + if (x == 17) + return sg[17][1799] + (y - 1800) / per[17] * g17[per[17] - 1] + g17[(y - 1800) % per[17]]; + if (x == 18) + return sg[18][1799] + (y - 1800) / per[18] * g18[per[18] - 1] + g18[(y - 1800) % per[18]]; + if (x == 19) + return sg[19][1799] + (y - 1800) / per[19] * g19[per[19] - 1] + g19[(y - 1800) % per[19]]; + if (x == 20) + return sg[20][1799] + (y - 1800) / per[20] * g20[per[20] - 1] + g20[(y - 1800) % per[20]]; + if (x == 21) + return sg[21][1799] + (y - 1800) / per[21] * g21[per[21] - 1] + g21[(y - 1800) % per[21]]; + if (x == 22) + return sg[22][1799] + (y - 1800) / per[22] * g22[per[22] - 1] + g22[(y - 1800) % per[22]]; + if (x == 23) + return sg[23][1799] + (y - 1800) / per[23] * g23[per[23] - 1] + g23[(y - 1800) % per[23]]; + if (x == 24) + return sg[24][1799] + (y - 1800) / per[24] * g24[per[24] - 1] + g24[(y - 1800) % per[24]]; + if (x == 25) + return sg[25][1799] + (y - 1800) / per[25] * g25[per[25] - 1] + g25[(y - 1800) % per[25]]; +} +int main() +{ + int t; + scanf("%d", &t); + cal(); + while (t--) + { + int n, x; + long long sum = 0, y; + scanf("%*d%*d%d", &n); + for (int i = 0; i < n; i++) + scanf("%d%lld", &x, &y), sum ^= f(x, y); + if (sum) + printf("YES\n"); + else + printf("NO\n"); + } + return 0; +} diff --git a/12351.cpp b/12351.cpp new file mode 100644 index 0000000..3624c1a --- /dev/null +++ b/12351.cpp @@ -0,0 +1,337 @@ +#include + +using namespace std; + +const int cost_del = 2, cost_ins = 2, cost_trans = 2; + +int min_edit(const string &s1, const string &s2); +int min_3(int a, int b, int c); +void init_subst_tab(); +void set_close_subst(unsigned char c1, unsigned char c2); +int subst_cost(unsigned char c1, unsigned char c2); + +class strie; + +class node +{ + friend class strie; + +public: + node(char c, node *parent, int depth); + inline bool is_term() + { + return w.size() != 0; + } + + char c; + int depth; + string w; + node *parent; + set ssize; + map children; + vector d; +}; + +class nodeinheap +{ + friend class strie; + +public: + nodeinheap(int h, node *p); + inline bool operator<(const nodeinheap &o) const + { + return (h < o.h); + } + inline bool operator==(const nodeinheap &o) const + { + return (h == o.h); + } + inline bool operator!=(const nodeinheap &o) const + { + return (h != o.h); + } + + int h; + node *p; +}; + +class strie +{ +public: + typedef map::iterator child_iterator; + typedef map::const_iterator const_child_iterator; + strie(); + bool insert(const string &word); + int me_search(const string &word);//a* srch + bool exact_search(const string &word) const; + const vector &getresult() const; + + void me_visit(node *c); + int me_est(node *c);// heuristic estimate + node root; + int maxl; + int bestd; + int worstd; + int wsize; + string w; + multiset heap; + vector result; +}; + +const size_t TDIM = 256; + +static int subst_tab[TDIM][TDIM]; + +int min_3(int a, int b, int c) +{ + int m = a; + if (b < m) + { + m = b; + } + if (c < m) + { + m = c; + } + return (m); +} + +void init_subst_tab() +{ + for (size_t i = 0; i < TDIM; i++) + { + for (size_t j = 0; j < TDIM; j++) + { + if (i == j) + { + subst_tab[i][j] = 0; + } + else + { + subst_tab[i][j] = 2; + } + } + } + return; +} + +void set_close_subst(unsigned char c1, unsigned char c2) +{ + subst_tab[c1][c2] = 1; + subst_tab[c2][c1] = 1; + return; +} + +int subst_cost(unsigned char c1, unsigned char c2) +{ + return (subst_tab[c1][c2]); +} + +node::node(char c, node *parent, int depth) + : c(c), parent(parent), depth(depth) +{ +} + +nodeinheap::nodeinheap(int h, node *p) : h(h), p(p) +{ +} + +strie::strie() : root('\0', 0, 0), maxl(0) +{ +} +bool strie::insert(const string &w) +{ + node *p = &root; + bool is_new = false; + if (w.size() > (size_t)maxl) + { + maxl = w.size(); + } + for (size_t i = 0; i < w.size(); i++) + { + char c = w[i]; + pair t = p->children.insert( + make_pair(c, node(c, p, i + 1))); + child_iterator it = t.first; + p = &(it->second); + int sleft = w.size() - i - 1; + if (sleft > 0) + { + p->ssize.insert(sleft); + } + } + if (p->w.size() == 0) + { + is_new = true; + p->w = w; + } + return (is_new); +} +int strie::me_search(const string &word) +{ + w = word; + wsize = w.size(); + worstd = bestd = (wsize + maxl) * 2; + root.d = vector(wsize + 1); + for (int i = 0; i <= wsize; i++) + { + root.d[i] = i * cost_del; + } + heap.clear(); + result.clear(); + me_visit(&root); + while (!heap.empty()) + { + multiset::iterator it = heap.begin(); + node *cur = it->p; + if (it->h > bestd) + { + break; + } + heap.erase(it); + me_visit(cur); + } + sort(result.begin(), result.end()); + return (bestd); +} +void strie::me_visit(node *c) +{ + for (child_iterator it = c->children.begin(); it != c->children.end(); it++) + { + int h = me_est(&(it->second)); + node &child = (it->second); + if (child.w.size() > 0)// terminal node + { + int &curd = child.d[wsize]; + if (curd < bestd) + { + bestd = curd; + result.clear(); + result.push_back(child.w); + } + else if (curd == bestd) + { + result.push_back(child.w); + } + } + if (h <= bestd && child.children.size() > 0) + { + heap.insert(nodeinheap(h, &(it->second))); + } + } +} +int strie::me_est(node *c) +{ + c->d.resize(wsize + 1); + vector &d = c->d; + d[0] = c->depth * cost_ins; + node *p = c->parent; + node *gp = p->parent; + set::const_iterator cs_it, ns_it; + cs_it = ns_it = c->ssize.begin(); + if (ns_it != c->ssize.end()) + { + ns_it++; + } + int besth = worstd;// start with very high heuristic + for (int i = 1; i <= wsize; i++) + { + d[i] = min_3(p->d[i - 1] + subst_cost(w[i - 1], c->c), + d[i - 1] + cost_del, + p->d[i] + cost_ins); + if (i > 1 && c->depth > 1 && w[i - 1] != c->c && w[i - 1] == p->c && w[i - 2] == c->c) + { + d[i] = min(d[i], gp->d[i - 2] + cost_trans); + } + } + for (int i = wsize - 1; i >= 1; i--) + { + int h = d[i]; + if (cs_it != c->ssize.end())// estimate heuristic + { + int sleft = wsize - i; + int diffsize; + if (sleft > *cs_it && ns_it != c->ssize.end())// find best size match + { + if (sleft - *cs_it > *ns_it - sleft) + { + cs_it = ns_it; + ns_it++; + } + } + diffsize = (sleft <= *cs_it) ? (*cs_it - sleft) : (sleft - *cs_it); + h = d[i] + diffsize * 2; + } + if (h < besth) + { + besth = h; + } + } + return (besth); +} +const vector &strie::getresult() const +{ + return result; +} +bool strie::exact_search(const string &word) const +{ + const node *cur = &root; + size_t i; + for (i = 0; i < word.size(); i++) + { + char c = word[i]; + const_child_iterator it = cur->children.find(c); + if (it == cur->children.end()) + { + break; + } + cur = &(it->second); + } + return (i == word.size() && cur->w.size() != 0); +} + +int main() +{ + strie tree; + string l; + init_subst_tab(); + + while (getline(cin, l)) + { + if (l.empty()) + { + break; + } + char c1 = l[0]; + for (size_t i = 2; i < l.size(); i++) + { + set_close_subst(c1, l[i]); + } + } + + while (getline(cin, l)) + { + if (l.empty()) + { + break; + } + tree.insert(l); + } + + while (getline(cin, l)) + { + if (l.empty()) + { + break; + } + int min_op = tree.me_search(l); + const vector &vs = tree.getresult(); + cout << l << ":" << min_op << ":" << vs[0]; + for (size_t i = 1; i < vs.size(); i++) + { + cout << ' ' << vs[i]; + } + printf("\n"); + } + return 0; +} diff --git a/12352.cpp b/12352.cpp new file mode 100644 index 0000000..999c72f --- /dev/null +++ b/12352.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +struct rect +{ + int x, y, ord, col, row; +} r[10]; + +struct node +{ + int x, y, i; +} f[125][10]; +node no1[10]; + +bool visit[105][105]; +char map_[105][105], st[100]; +int s[7][7], p[7]; + +int main() +{ + int T; + scanf("%d", &T); + int t = 0; + while (T--) + { + printf("Case %d: ", ++t); + int cnt = 0; + for (int i = 0; i < 20; i++) + { + scanf("%s", map_[i]); + } + memset(visit, false, sizeof(visit)); + bool fuck = false; + for (int i = 0; i < 20; i++) + { + for (int j = 0; j < 20; j++) + { + if (map_[i][j] == 'x' && !visit[i][j]) + { + int ii = i, jj = j; + while (ii < 20 && map_[ii][j] == 'x') + { + ii++; + } + while (jj < 20 && map_[i][jj] == 'x') + { + jj++; + } + for (int k = i; k < ii; k++) + for (int l = j; l < jj; l++) + { + visit[k][l] = true; + } + r[cnt].x = i; + r[cnt].y = j; + r[cnt].row = ii - i; + r[cnt].col = jj - j; + r[cnt].ord = cnt; + if (r[cnt].row > 5 || r[cnt].col > 5) + { + fuck = true; + } + cnt++; + } + } + } + if (fuck) + { + puts("invalid data"); + continue; + } + for (int i = 0; i < cnt; i++) + { + p[i] = i; + } + int tot = 0; + do + { + memset(s, 0, sizeof(s)); + bool notva = true; + for (int i = 0; i < cnt; i++) + { + rect now = r[p[i]]; + bool flag = true; + for (int j = 0; j < 5; j++) + { + for (int k = 0; k < 5; k++) + if (!s[j][k]) + { + if (j + now.row - 1 >= 5 || k + now.col - 1 >= 5) + { + notva = false; + } + else + { + no1[p[i]].x = j; + no1[p[i]].y = k; + for (int l = j; l < j + now.row; l++) + for (int o = k; o < k + now.col; o++) + { + s[l][o]++; + } + } + flag = false; + break; + } + if (!flag) + { + break; + } + } + } + if (!notva) + { + continue; + } + bool f1 = true; + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + if (s[i][j] != 1) + { + f1 = false; + break; + } + if (!f1) + { + break; + } + } + if (f1) + { + for (int j = 0; j < cnt; j++) + { + f[tot][j] = no1[j]; + } + tot++; + } + } while (next_permutation(p, p + cnt)); + if (tot == 0) + { + puts("invalid data"); + continue; + } + int move = 100000000; + for (int i = 0; i < tot; i++) + { + for (int j = 0; j < 20; j++) + { + for (int k = 0; k < 20; k++) + { + int sum = 0; + for (int l = 0; l < cnt; l++) + { + int nox = j + f[i][l].x; + int noy = k + f[i][l].y; + sum += abs(nox - r[l].x) + abs(noy - r[l].y); + } + if (sum < move) + { + move = sum; + } + } + } + } + printf("%d\n", move); + } + return 0; +} diff --git a/12353.cpp b/12353.cpp new file mode 100644 index 0000000..0e3a1e4 --- /dev/null +++ b/12353.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define SZ(v) ((int)(v).size()) +#define REP(i, n) for (int i = 0; i < (n); ++i) +#define REPF(i, a, b) for (int i = (a); i <= (b); ++i) +#define REPD(i, a, b) for (int i = (a); i >= (b); --i) +const int maxint = -1u >> 1; +const double eps = 1e-9; +const double pi = acos(-1.); +typedef long long lint; + +int r1, r2, d1, d2; +double s; +int main() +{ + while (scanf("%d%d%d%d%lf", &r1, &r2, &d1, &d2, &s) == 5) + { + if (r1 == 0 && r2 == 0 && d1 == 0 && d2 == 0) + { + break; + } + lint ans = 0; + REPF(r, r1, r2) + { + REPF(d, d1, d2) + { + double w = (r + d) * 4; + double s1 = 2 * (r + d), s2 = 2 * pi * r * r / w; + ans += max(0, int((s - s2) / (s1 - s2)) * 2); + } + } + printf("%lld", ans); + cout << endl; + } + return 0; +} diff --git a/12354.cpp b/12354.cpp new file mode 100644 index 0000000..f7b25a5 --- /dev/null +++ b/12354.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +typedef vector BV; +typedef vector IV; + +#define MAXN 20000 + +#define cFor(t, v, c) for (t::const_iterator v = c.begin(); v != c.end(); v++) + +// Graphs +namespace Graph +{ +struct Edge +{ + int v, w; + Edge(int V, int W) : v(V), w(W) {} +}; +typedef list EL; +typedef vector ELV; +void init(ELV &g, int n) +{ + g.clear(); + g.resize(n); +} +void add(ELV &g, int u, int v, int w) +{ + g[u].push_back(Edge(v, w)); +} +} +using namespace Graph; + +ELV g; +BV flg; +IV paths; +int total, N; + +void dfs(int v) +{ + flg[v] = true; + cFor(EL, e, g[v]) + { + if (flg[e->v]) + { + continue; + } + dfs(e->v); + if (e->w < 0) + { + if (paths[e->v] > 0) + { + paths[v] += paths[e->v]; + } + else + { + total += abs(paths[e->v]) + 1, paths[v]++; + } + } + else + { + if (paths[e->v] < 0) + { + paths[v] += paths[e->v]; + } + else + { + paths[v]--; + } + } + } +} + +void solve() +{ + flg.clear(); + flg.resize(N); + paths.clear(); + paths.resize(N); + total = 0; + dfs(0); + if (paths[0] < 0) + { + total += abs(paths[0]); + } +} + +int main() +{ + int T; + scanf("%d", &T); + int ncase = 1; + while (T--) + { + scanf("%d", &N); + init(g, N); + for (int i = 0; i < N - 1; i++) + { + int u, v; + scanf("%d%d", &u, &v); + add(g, u, v, 1); + add(g, v, u, -1); + } + solve(); + printf("Case %d: %d\n", ncase++, total); + } + return 0; +} diff --git a/12355.cpp b/12355.cpp new file mode 100644 index 0000000..4baeb92 --- /dev/null +++ b/12355.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +void pri(); +bool is[10000005]; +long long int prime[664580]; + +int main() +{ + long long int P, p, Q, q, i, j, n, ans, num, tmp, ker, count; + pri(); + while (scanf(" %lld", &Q) == 1 && Q) + { + tmp = 2; + ker = (sqrt(Q) + 0.000001); + while (Q % 2 == 0) + { + Q /= 2; + } + for (i = 1; prime[i] <= ker && i < 664579; i++) + { + if (prime[i] > Q) + { + break; + } + count = 0; + while (Q % prime[i] == 0) + { + Q /= prime[i]; + count++; + } + tmp *= (count * 2 + 1); + } + if (Q != 1) + { + tmp *= 3; + } + printf("%lld\n", tmp); + } + return 0; +} + +void pri() +{ + long long int i, j, k = 0; + for (i = 0; i < 10000005; i++) + { + is[i] = true; + } + for (i = 2; i < 10000005; i++) + if (is[i]) + { + for (j = i * i; j < 10000005; j += i) + { + is[j] = false; + } + prime[k++] = i; + } +} diff --git a/12356.cpp b/12356.cpp new file mode 100644 index 0000000..1f924ae --- /dev/null +++ b/12356.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +const int NN = 100048; + +bool dead[NN]; +int left_[NN], right_[NN]; + +int main() +{ + for (int b, s; scanf("%d%d", &s, &b) == 2 && s;) + { + for (int i = 0; i <= s + 1; ++i) + { + left_[i] = i - 1; + right_[i] = i + 1; + dead[i] = false; + } + int new_left_, new_right_; + for (int i = 0, y, z; i < b; ++i) + { + scanf("%d %d", &y, &z); + for (int j = y; j <= z; ++j) + { + dead[j] = true; + } + new_left_ = y; + new_right_ = z; + while (dead[new_left_]) + { + new_left_ = left_[new_left_]; + } + while (dead[new_right_]) + { + new_right_ = right_[new_right_]; + } + for (int j = y; j <= z; ++j) + { + left_[j] = new_left_; + right_[j] = new_right_; + } + if (new_left_ > 0 && new_right_ <= s) + { + printf("%d %d\n", new_left_, new_right_); + } + else if (new_left_ > 0) + { + printf("%d *\n", new_left_); + } + else if (new_right_ <= s) + { + printf("* %d\n", new_right_); + } + else + { + puts("* *"); + } + } + puts("-"); + } + return 0; +} diff --git a/12357.cpp b/12357.cpp new file mode 100644 index 0000000..53a6afa --- /dev/null +++ b/12357.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +const int NN = 1024; +int dp[NN][NN]; + +int max(int a, int b) +{ + return a > b ? a : b; +} + +int main() +{ + for (int i = 0; i < 1024; ++i) + { + dp[i][0] = 0; + } + for (int n; scanf("%d", &n) == 1 && n;) + { + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= i; ++j) + { + scanf("%d", &dp[i][j]); + dp[i][j] += dp[i - 1][j]; + } + for (int col = 1; col <= n; ++col) + { + for (int line = col; line <= n; ++line) + { + dp[line][col] += dp[line - 1][col - 1]; + } + for (int line = n - 1; line >= col; --line) + { + dp[line][col] = max(dp[line][col], dp[line + 1][col]); + } + } + int ans = 0; + for (int line = 1; line <= n; ++line) + { + ans = max(ans, dp[line][line]); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12358.cpp b/12358.cpp new file mode 100644 index 0000000..b641dda --- /dev/null +++ b/12358.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +const int NN = 100048; +long long C[NN], G, TF; + +template +_T GCD(_T a, _T b) +{ + return b ? GCD(b, a % b) : a; +} + +bool valid(int x, int n) +{ + if (x % n || TF % x || TF / x <= n) + { + return false; + } + if ((C[0] % x) % (x / n)) + { + return false; + } + return G % x == 0; +} +int main() +{ + for (int n; scanf("%d", &n) == 1 && n;) + { + long long ans = TF = 0; + for (int i = 0; i < n; TF += C[i++]) + { + scanf("%lld", C + i); + } + sort(C, C + n); + G = 0; + for (int i = 1; i < n; ++i) + { + C[i] -= C[0]; + if (!G) + { + G = C[i]; + } + else + { + G = GCD(G, C[i]); + } + } + int limit = int(sqrt(TF) + .5); + int c0 = C[0]; + for (int p = 2; p < C[0]; ++p) + { + if (valid(p, n)) + { + ans += (c0 - p) / p + !!(c0 % p); + } + if (p >= limit) + { + break; + } + if (valid(TF / p, n)) + { + ans += (c0 - (TF / p)) / (TF / p) + !!(c0 % (TF / p)); + } + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12359.cpp b/12359.cpp new file mode 100644 index 0000000..4e64866 --- /dev/null +++ b/12359.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +int trie[100000][26], cont; + +void add(char *s) +{ + int L = strlen(s), pos = 0; + for (int i = 0; i < L; ++i) + { + if (trie[pos][s[i] - 'a'] == 0) + { + trie[pos][s[i] - 'a'] = cont++; + } + pos = trie[pos][s[i] - 'a']; + } +} + +int trie2[100000][26], cont2; +int suff[26]; +bool start[26]; + +void add2(char *s) +{ + int L = strlen(s), pos = 0; + start[s[L - 1] - 'a'] = true; + for (int i = L - 1; i >= 0; --i) + { + if (trie2[pos][s[i] - 'a'] == 0) + { + trie2[pos][s[i] - 'a'] = cont2++; + ++suff[s[i] - 'a']; + } + pos = trie2[pos][s[i] - 'a']; + } +} + +long long ans = 0; + +void solve(int pos) +{ + for (int i = 0; i < 26; ++i) + { + if (trie[pos][i]) + { + solve(trie[pos][i]); + if (pos > 0 && start[i]) + { + ++ans; + } + } + else if (pos > 0) + { + ans += suff[i]; + } + } +} + +int main() +{ + int P, S; + char s[1001]; + while (true) + { + scanf("%d %d", &P, &S); + if (P == 0) + { + break; + } + memset(trie, 0, sizeof trie); + cont = 1; + for (int i = 0; i < P; ++i) + { + scanf("%s", s); + add(s); + } + memset(trie2, 0, sizeof trie2); + cont2 = 1; + memset(start, false, sizeof start); + memset(suff, 0, sizeof suff); + for (int i = 0; i < S; ++i) + { + scanf("%s", s); + add2(s); + } + ans = 0; + solve(0); + printf("%lld\n", ans); + } + return 0; +} diff --git a/1236.cpp b/1236.cpp new file mode 100644 index 0000000..7b3fdc2 --- /dev/null +++ b/1236.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +struct Edge +{ + int to, w; + Edge(int a = 0, int b = 0) + : to(a), w(b) + { + } +}; +vector g[65]; + +int N, K, zero[65][65], one[65][65], two[65][65], sz[65]; + +int dfs(int nd, int p) +{ + int i, j, k, x, y, z; + for (i = 0; i <= K; i++) + { + zero[nd][i] = one[nd][i] = two[nd][i] = -0xfffffff; + } + + zero[nd][0] = 0; + for (x = 0; x < g[nd].size(); x++) + { + if (g[nd][x].to == p) + { + continue; + } + sz[nd] += dfs(g[nd][x].to, nd); + } + + // zero + for (x = 0; x < g[nd].size(); x++) + { + int son = g[nd][x].to, sonw = g[nd][x].w; + if (son == p) + { + continue; + } + for (j = K; j >= 0; j--) + { + for (i = 0; i <= K; i++) + { + int mm = max(zero[son][i], max(one[son][i], two[son][i])); + if (j - i >= 0) + { + zero[nd][j] = max(zero[nd][j], zero[nd][j - i] + mm); + } + } + } + } + + // one, make path to son + for (x = 0; x < g[nd].size(); x++) + { + int sonx = g[nd][x].to, sonxw = g[nd][x].w; + if (sonx == p) + { + continue; + } + int dp[65] = {}; + for (i = 1; i <= K; i++) + { + dp[i] = -0xfffffff; + } + for (y = 0; y < g[nd].size(); y++) + { + int sony = g[nd][y].to, sonyw = g[nd][y].w; + if (sony == p) + { + continue; + } + if (sony == sonx) + { + continue;// impt + } + for (j = K; j >= 0; j--) + { + for (i = 0; i <= K; i++) + { + int mm = max(zero[sony][i], max(one[sony][i], two[sony][i])); + if (j - i >= 0) + { + dp[j] = max(dp[j], dp[j - i] + mm); + } + } + } + } + for (i = 0; i <= K; i++) + { + for (j = 0; i + j <= K; j++) + { + one[nd][i + j] = max(one[nd][i + j], dp[i] + one[sonx][j] + sonxw); + one[nd][i + j + 1] = max(one[nd][i + j + 1], dp[i] + zero[sonx][j] + sonxw); + } + } + } + + // two, make a path through this node, between two son + for (x = 0; x < g[nd].size(); x++) + { + int sonx = g[nd][x].to, sonxw = g[nd][x].w; + if (sonx == p) + { + continue; + } + for (y = x + 1; y < g[nd].size(); y++) + { + int sony = g[nd][y].to, sonyw = g[nd][y].w; + if (sony == p || sony == sonx) + { + continue; + } + int dp[65] = {}; + for (i = 1; i <= K; i++) + { + dp[i] = -0xfffffff; + } + for (z = 0; z < g[nd].size(); z++) + { + int sonz = g[nd][z].to, sonzw = g[nd][z].w; + if (sonz == p || sonz == sonx || sonz == sony) + { + continue; + } + for (j = K; j >= 0; j--) + { + for (i = 0; i <= K; i++) + { + int mm = max(zero[sonz][i], max(one[sonz][i], two[sonz][i])); + if (j - i >= 0) + { + dp[j] = max(dp[j], dp[j - i] + mm); + } + } + } + } + + for (i = 0; i <= K; i++) + { + for (j = 0; j <= K; j++) + { + for (k = 0; k <= K && i + j + k - 1 <= K; k++) + { + if (j && k)// j != 0, k != 0 + two[nd][i + j + k - 1] = max(two[nd][i + j + k - 1], dp[i] + one[sonx][j] + one[sony][k] + sonxw + sonyw); + // j == 0 && k + two[nd][i + j + k] = max(two[nd][i + j + k], dp[i] + zero[sonx][j] + one[sony][k] + sonxw + sonyw); + // j && k == 0 + two[nd][i + j + k] = max(two[nd][i + j + k], dp[i] + one[sonx][j] + zero[sony][k] + sonxw + sonyw); + // otherwise + two[nd][i + j + k + 1] = max(two[nd][i + j + k + 1], dp[i] + zero[sonx][j] + zero[sony][k] + sonxw + sonyw); + } + } + } + } + } + return sz[nd]; +} + +int main() +{ + int T, i, j, k, x, y, v; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &K); + for (i = 1; i <= N; i++) + { + g[i].clear(); sz[i] = 1; + } + for (i = 1; i < N; i++) + { + scanf("%d%d%d", &x, &y, &v); + g[x].push_back(Edge(y, v)); + g[y].push_back(Edge(x, v)); + } + + dfs(1, 0); + + int ret = 0; + for (i = 1; i <= K; i++) + { + ret = max(ret, max(zero[1][i], max(one[1][i], two[1][i]))); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/12360.cpp b/12360.cpp new file mode 100644 index 0000000..70f5d4c --- /dev/null +++ b/12360.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +const int INF = 1 << 29, NN = 20048; + +map m; +struct edge +{ + int to, w, next; +} e[NN]; +int val[NN][2], first[NN], tree[NN], rval[NN]; +int cnt, ecnt; + +void add_edge(int from, int to, int cost) +{ + edge &E = e[ecnt]; + E.to = to; + E.w = cost; + E.next = first[from]; + first[from] = ecnt++; +} + +int get_mapping(int x) +{ + int &z = m[x]; + if (!z) + { + z = ++cnt; + } + return z; +} + +void dfs(int u, int value, bool d, int tn) +{ + tree[u] = tn; + val[u][d] = value; + for (int x = first[u]; x != -1; x = e[x].next) + { + int v = e[x].to, z; + if (v == u) + { + continue; + } + if (val[v][!d] == INF) + { + if (d) + { + z = value - e[x].w; + } + else + { + z = value + e[x].w; + } + dfs(v, z, !d, tn); + } + } +} + +void dfsv(int u) +{ + for (int at = first[u]; at != -1; at = e[at].next) + { + int v = e[at].to; + if (rval[v] == INF) + { + rval[v] = e[at].w - rval[u]; + dfsv(v); + } + } +} + +int query(int a, int b) +{ + int r = INF; + if (a == b && rval[a] != INF) + { + return rval[a]; + } + if (val[a][0] != INF && val[b][1] != INF) + { + r = val[b][1] - val[a][0]; + } + if (val[a][1] != INF && val[b][0] != INF) + { + r = val[a][1] - val[b][0]; + } + return r >>= (r != INF && a == b); +} + +int main() +{ + val[0][0] = val[1][0] = INF; + rval[0] = INF; + for (int n, z; scanf("%d %d", &n, &z) == 2 && n;) + { + cnt = ecnt = 0; + m.clear(); + for (int i = 1; i <= n << 1; ++i) + { + val[i][0] = val[i][1] = INF; + first[i] = -1; + tree[i] = i; + rval[i] = INF; + } + for (int a, b, c, i = 0; i < n; ++i) + { + scanf("%d %d %d", &a, &b, &c); + a = get_mapping(a); + b = get_mapping(b); + if (a == b) + { + rval[a] = c; + } + add_edge(a, b, c); + add_edge(b, a, c); + } + for (int i = 1; i <= cnt; ++i) + if (rval[i] != INF) + { + dfsv(i); + } + for (int i = 1; i <= cnt; ++i) + if (rval[i] == INF && val[i][0] == INF && val[i][1] == INF) + { + dfs(i, 0, 0, i); + } + for (int a, b, d, i = 0; i < z; ++i) + { + scanf("%d %d", &a, &b); + a = m[a]; + b = m[b]; + d = INF; + if (tree[a] == tree[b]) + { + d = query(a, b); + } + else if ((a = query(a, a)) != INF && (b = query(b, b)) != INF) + { + d = a + b; + } + if (d != INF) + { + printf("%d\n", d); + } + else + { + puts("*"); + } + } + puts("-"); + } + return 0; +} diff --git a/12361.cpp b/12361.cpp new file mode 100644 index 0000000..2002690 --- /dev/null +++ b/12361.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +//12361 +//File Retrieval +//Misc;String Matching;Suffix Array;Longest Common Prefix +#define MAX 600200 +#define ull unsigned long long + +struct Item +{ + ull v; + int p; + Item(ull v, int p) : v(v), p(p) {} +}; + +int RA[MAX], tempRA[MAX]; +int SA[MAX], tempSA[MAX]; +int C[MAX]; +int Phi[MAX], PLCP[MAX], LCP[MAX]; +int IDX[MAX], SIZ[MAX]; +set R; + +void suffix_sort(int n, int k) +{ + memset(C, 0, sizeof C); + + for (int i = 0; i < n; i++) + C[i + k < n ? RA[i + k] : 0]++; + + int sum = 0; + for (int i = 0; i < max(256, n); i++) + { + int t = C[i]; + C[i] = sum; + sum += t; + } + + for (int i = 0; i < n; i++) + tempSA[C[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i]; + + memcpy(SA, tempSA, n * sizeof(int)); +} + +void suffix_array(string &s) +{ + int n = s.size(); + + for (int i = 0; i < n; i++) + RA[i] = s[i] - 1; + + for (int i = 0; i < n; i++) + SA[i] = i; + + for (int k = 1; k < n; k *= 2) + { + suffix_sort(n, k); + suffix_sort(n, 0); + + int r = tempRA[SA[0]] = 0; + for (int i = 1; i < n; i++) + { + int s1 = SA[i], s2 = SA[i - 1]; + bool equal = true; + equal &= RA[s1] == RA[s2]; + equal &= s1 + k < n && s2 + k < n && RA[s1 + k] == RA[s2 + k]; + + tempRA[SA[i]] = equal ? r : ++r; + } + + memcpy(RA, tempRA, n * sizeof(int)); + } +} + +void lcp(string &s) +{ + int n = s.size(); + + Phi[SA[0]] = -1; + for (int i = 1; i < n; i++) + Phi[SA[i]] = SA[i - 1]; + + int L = 0; + for (int i = 0; i < n; i++) + { + if (Phi[i] == -1) + { + PLCP[i] = 0; + continue; + } + while (s[i + L] != '\1' && s[i + L] == s[Phi[i] + L]) + L++; + + PLCP[i] = L; + L = max(L - 1, 0); + } + + for (int i = 1; i < n; i++) + LCP[i] = PLCP[SA[i]]; +} + +int main() +{ + int n; + while (cin >> n, n) + { + R.clear(); + + stringstream ss; + int kk = 0; + for (int i = 0; i < n; i++) + { + string temp; + cin >> temp; + ss << temp << '\1'; + + for (int j = 0; j <= temp.size(); j++) + { + SIZ[kk] = temp.size() - j; + IDX[kk] = i; + kk++; + } + } + + string s = ss.str(); + + suffix_array(s); + lcp(s); + + stack ST; + + for (int i = n; i < s.size(); i++) + { + if (LCP[i] < SIZ[SA[i]] && (i + 1 == s.size() || LCP[i + 1] < SIZ[SA[i]])) + R.insert(1ull << IDX[SA[i]]); + } + + for (int i = n; i < s.size(); i++) + { + ull lastv = 0; + while (!ST.empty() && (ST.top().p > LCP[i] || LCP[i] == 0)) + { + Item item = ST.top(); + ST.pop(); + + R.insert(item.v); + + if (!ST.empty()) + ST.top().v |= item.v; + + lastv = item.v; + } + if (LCP[i]) + { + if (ST.empty() || ST.top().p < LCP[i]) + { + ST.push(Item(1ull << IDX[SA[i]] | 1ull << IDX[SA[i - 1]] | lastv, LCP[i])); + } + else if (ST.top().p == LCP[i]) + { + ST.top().v |= 1ull << IDX[SA[i]]; + } + } + } + + while (!ST.empty()) + { + Item item = ST.top(); + ST.pop(); + + R.insert(item.v); + if (!ST.empty()) + ST.top().v |= item.v; + } + + cout << R.size() << endl; + } +} diff --git a/12362.cpp b/12362.cpp new file mode 100644 index 0000000..b167fb0 --- /dev/null +++ b/12362.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +#define foreach(x, v) for (typeof(v).begin() x = (v).begin(); x != (v).end(); ++x) +#define For(i, a, b) for (int i = (a); i < (b); ++i) +#define D(x) cout << #x " is " << (x) << endl + +const double EPS = 1e-9; +int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} + +const int MAXN = 2005; + +struct Tree +{ + int x, y, value, color; +}; + +Tree trees[MAXN], v[MAXN]; + +inline bool above(const Tree &t) +{ + assert(t.x != 0 or t.y != 0); + if (t.y == 0) + { + return t.x > 0; + } + return t.y > 0; +} + +inline bool below(const Tree &t) +{ + return !above(t); +} + +bool compare(const Tree &a, const Tree &b) +{ + long long cross = 1LL * a.x * b.y - 1LL * a.y * b.x; + if (!above(a)) + { + cross = -cross; + } + if (!above(b)) + { + cross = -cross; + } + return cross > 0; +} + +bool equal(const Tree &a, const Tree &b) +{ + long long cross = 1LL * a.x * b.y - 1LL * a.y * b.x; + return cross == 0; +} + +int sweep(int n, int colorAbove) +{ + int colorBelow = 1 - colorAbove; + sort(v, v + n, compare); + int score = 0; + for (int i = 0; i < n; ++i) + { + if (above(v[i]) and v[i].color != colorAbove) + { + score += v[i].value; + } + if (below(v[i]) and v[i].color != colorBelow) + { + score += v[i].value; + } + } + int ans = score; + for (int i = 0, j; i < n; i = j) + { + j = i; + while (j < n and equal(v[j], v[i])) + { + // process j + if (above(v[j])) + { + if (v[j].color == colorAbove) + { + score += v[j].value; + } + else + { + score -= v[j].value; + } + } + if (below(v[j])) + { + if (v[j].color == colorBelow) + { + score += v[j].value; + } + else + { + score -= v[j].value; + } + } + j++; + } + if (score < ans) + { + ans = score; + } + } + return ans; +} + +int main() +{ + int P, L; + while (cin >> P >> L) + { + if (P == 0 and L == 0) + { + break; + } + for (int i = 0; i < P; ++i) + { + cin >> trees[i].x >> trees[i].y >> trees[i].value; + trees[i].color = 0; + } + for (int i = 0; i < L; ++i) + { + cin >> trees[P + i].x >> trees[P + i].y >> trees[P + i].value; + trees[P + i].color = 1; + } + int n = P + L; + int ans = 1 << 30; + for (int i = 0; i < n; ++i) + { + const Tree &pivot = trees[i]; + int m = 0; + for (int j = 0; j < n; ++j) + if (i != j) + { + v[m].x = trees[j].x - pivot.x; + v[m].y = trees[j].y - pivot.y; + v[m].color = trees[j].color; + v[m].value = trees[j].value; + m++; + } + int score; + score = sweep(m, 0); + if (score < ans) + { + ans = score; + } + score = sweep(m, 1); + if (score < ans) + { + ans = score; + } + } + cout << ans << endl; + } + return 0; +} diff --git a/12363.cpp b/12363.cpp new file mode 100644 index 0000000..5efafb8 --- /dev/null +++ b/12363.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +const int NN = 10048; +int n; +vector adj[NN]; +stack> s;// BCC +int num[NN], low[NN], p[NN]; +int cnt; + +int root(int x) +{ + return p[x] == x ? x : p[x] = root(p[x]); +} +bool union_find(int a, int b) +{ + p[a = root(a)] = b = root(b); + return a != b; +} +void dfs(int u, int prnt = -1) +{ + low[u] = num[u] = cnt++; + for (int i = 0; i < (int)adj[u].size(); i++) + { + int v = adj[u][i]; + if (num[v] == -1) + { + s.push(make_pair(u, v));// BCC + dfs(v, u); + low[u] = min(low[u], low[v]); + if (low[v] == num[v]) + { + /* u-v eh ponte */ + union_find(u, v); + // printf("found bridge: %d-%d\n", u, v); + } + } + else if (v != prnt) + { + if (num[v] < num[u]) + { + s.push(make_pair(u, v));// BCC + } + low[u] = min(low[u], num[v]); + } + } +} + +void findcuts(void) +{ + memset(num, -1, sizeof(num)); + for (int i = 0; i < n; i++) + if (num[i] == -1) + { + cnt = 0, dfs(i); + } +} + +int main() +{ + for (int c, q; scanf("%d %d %d", &n, &c, &q) == 3 && n;) + { + for (int i = 0; i < n; ++i) + { + adj[i].clear(); + p[i] = i; + } + for (int a, b, i = 0; i < c; ++i) + { + scanf("%d %d", &a, &b); + --a; + --b; + adj[a].push_back(b); + adj[b].push_back(a); + } + findcuts(); + for (int a, b, i = 0; i < q; ++i) + { + scanf("%d %d", &a, &b); + puts(root(--a) == root(--b) ? "Y" : "N"); + } + puts("-"); + } + return 0; +} diff --git a/12364.cpp b/12364.cpp new file mode 100644 index 0000000..8edf0e1 --- /dev/null +++ b/12364.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +const char *nums[10] = + { + ".***..",// 0 + "*.....",// 1 + "*.*...",// 2 + "**....",// 3 + "**.*..",// 4 + "*..*..",// 5 + "***...",// 6 + "****..",// 7 + "*.**..",// 8 + ".**...",// 9 +}; + +char output[3][1024]; +char answer[1024]; +char digits[1024]; +char input[1024][128]; +char type[16]; +int main() +{ + for (int d; scanf("%d", &d) == 1 && d;) + { + scanf("%s", type); + if (type[0] == 'S') + { + scanf("%s", digits); + for (int line = 0; line < 3; ++line) + { + char *p = output[line]; + for (int i = 0; digits[i]; ++i) + { + int z = digits[i] - '0'; + *p++ = nums[z][2 * line]; + *p++ = nums[z][2 * line + 1]; + *p++ = ' '; + } + *--p = 0; + puts(output[line]); + } + } + else + { + for (int line = 0; line < 3; ++line) + for (int i = 0; i < d; ++i) + { + scanf("%s", input[i] + 2 * line); + } + for (int i = 0, j; i < d; ++i) + { + for (j = 0; strcmp(input[i], nums[j]); ++j) + ; + putchar('0' + j); + } + puts(""); + } + } + return 0; +} diff --git a/12365.cpp b/12365.cpp new file mode 100644 index 0000000..fb10118 --- /dev/null +++ b/12365.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +#define LEFT(x) (2 * (x - d) + d + 1) +#define RIGHT(x) (2 * (x - d) + d + 2) +const int NN = 100048; +struct node +{ + int left, right; + long long hash; +} d[5 * NN]; +long long bmodp[NN]; +long long B, P; + +int max(int a, int b) +{ + return a > b ? a : b; +} +int min(int a, int b) +{ + return a < b ? a : b; +} + +void init_node(int left, int right, node *n = d) +{ + n->left = left; + n->right = right; + n->hash = 0; + if (left < right) + { + int mid = (left + right) >> 1; + init_node(left, mid, LEFT(n)); + init_node(mid + 1, right, RIGHT(n)); + } +} + +long long update(int pos, int val, node *n = d) +{ + if (pos < n->left || pos > n->right) + { + return n->hash; + } + if (n->left == pos && n->right == pos) + { + return n->hash = val; + } + long long hright = update(pos, val, RIGHT(n)); + long long hleft = update(pos, val, LEFT(n)); + int mid = (n->left + n->right) >> 1; + int bpow = n->right - mid; + n->hash = (hright + (bmodp[bpow] * hleft) % P) % P; + return n->hash; +} + +long long query(int left, int right, node *n = d) +{ + if (right < n->left || left > n->right) + { + return 0; + } + if (left <= n->left && right >= n->right) + { + return n->hash; + } + long long hright = query(left, right, RIGHT(n)); + long long hleft = query(left, right, LEFT(n)); + int mid = (n->left + n->right) >> 1; + int bpow = max(min(right, n->right) - mid, 0); + long long r = (hright + (bmodp[bpow] * hleft) % P) % P; + return r; +} + +int main() +{ + char cmd[16]; + for (int L, N; scanf("%lld %lld %d %d", &B, &P, &L, &N) == 4 && B;) + { + init_node(1, L); + bmodp[0] = 1; + for (int i = 1; i <= L; ++i) + { + bmodp[i] = (bmodp[i - 1] * B) % P; + } + for (int a, b, i = 0; i < N; ++i) + { + scanf("%s %d %d", cmd, &a, &b); + if (cmd[0] == 'E') + { + update(a, b); + } + else + { + printf("%lld\n", query(a, b)); + } + } + puts("-"); + } + return 0; +} diff --git a/12366.cpp b/12366.cpp new file mode 100644 index 0000000..1649a04 --- /dev/null +++ b/12366.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int main() +{ + for (int x[3]; scanf("%d %d %d", &x[0], &x[1], &x[2]) == 3 && x[0];) + { + bool is_set, is_pair; + int a, b, c, unmatched; + is_set = false; + is_pair = false; + sort(x, x + 3); + a = x[0], b = x[1], c = x[2]; + if (a == b && b == c) + { + is_set = true; + } + else if (a == b || b == c) + { + is_pair = true; + if (a == b) + { + unmatched = c; + } + else + { + unmatched = a; + } + } + if (is_set && a == 13) + { + puts("*"); + } + else if (is_set) + { + printf("%d %d %d\n", a + 1, a + 1, a + 1); + } + else if (is_pair) + { + if (b == 13 && unmatched == 12) + { + puts("1 1 1"); + } + else + { + while (true) + { + unmatched = unmatched + 1; + if (unmatched == 14) + { + unmatched = 1; + ++b; + } + if (unmatched != b) + { + break; + } + } + x[0] = x[1] = b; + x[2] = unmatched; + sort(x, x + 3); + printf("%d %d %d\n", x[0], x[1], x[2]); + } + } + else + { + puts("1 1 2"); + } + } + return 0; +} diff --git a/12367.cpp b/12367.cpp new file mode 100644 index 0000000..cb3f167 --- /dev/null +++ b/12367.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +#define MAXN 1010 +#define INF 1000000000 + +struct edge +{ + int from, to, cap, flow, cost; + edge(int from_, int to_, int cap_, int flow_, int cost_) + { + from = from_, to = to_, cap = cap_, flow = flow_, cost = cost_; + } +}; + +struct MCMF +{ + int n, m, flow, cost, inq[MAXN], d[MAXN], p[MAXN], a[MAXN]; + queue q; + vector edges; + vector g[MAXN]; + + void init(int n_) + { + int i; + n = n_; + for (i = 0; i <= n; i++) + g[i].clear(); + edges.clear(); + } + + void addedge(int from, int to, int cap, int cost) + { + edges.push_back(edge(from, to, cap, 0, cost)); + edges.push_back(edge(to, from, 0, 0, -cost)); + m = edges.size(); + g[from].push_back(m - 2); + g[to].push_back(m - 1); + } + + bool spfa(int s, int t) + { + int i, j; + memset(inq, 0, sizeof(inq)); + for (i = 1; i <= n; i++) + { + d[i] = INF; + } + while (!q.empty()) + { + q.pop(); + } + d[s] = 0; + inq[s] = true; + q.push(s); + a[s] = INF; + while (!q.empty()) + { + int u = q.front(); + q.pop(); + inq[u] = false; + for (i = 0; i < g[u].size(); i++) + { + edge &e = edges[g[u][i]]; + if (e.cap > e.flow && d[e.to] > d[u] + e.cost) + { + d[e.to] = d[u] + e.cost; + p[e.to] = g[u][i]; + a[e.to] = min(a[u], e.cap - e.flow); + if (!inq[e.to]) + { + q.push(e.to); + inq[e.to] = 1; + } + } + } + } + if (d[t] == (INF)) + { + return false; + } + flow += a[t]; + cost += d[t] * a[t]; + int u = t; + while (u != s) + { + edges[p[u]].flow += a[t]; + edges[p[u] ^ 1].flow -= a[t]; + u = edges[p[u]].from; + } + return true; + } + + int solve(int s, int t) + { + flow = 0; + cost = 0; + while (spfa(s, t)) + { + } + return cost; + } +}; + +MCMF solver; +char map_[1010][1010]; +int N, M, ans, r[1010], c[1010]; + +int main() +{ + int T; + scanf("%d", &T); + int i, j, test = 0; + while (T--) + { + scanf("%d %d", &N, &M); + memset(r, 0, sizeof(r)); + memset(c, 0, sizeof(c)); + int sum = 0; + for (i = 1; i <= N; i++) + { + scanf("%s", map_[i] + 1); + for (j = 1; j <= M; j++) + { + if (map_[i][j] == '1') + { + r[i]++, c[j]++, sum++; + } + } + } + int flag1, flag2; + flag1 = flag2 = 0; + ans = 0; + if (sum % N == 0) + { + flag1 = 1; + solver.init(N + 1); + int need = sum / N; + int s, t; + s = 0, t = N + 1; + for (i = 1; i <= N; i++) + { + solver.addedge(s, i, r[i], 0); + solver.addedge(i, t, need, 0); + } + for (i = 1; i <= N; i++) + { + solver.addedge(i, i == N ? 1 : i + 1, INF, 1); + solver.addedge(i, i == 1 ? N : i - 1, INF, 1); + } + ans += solver.solve(s, t); + } + if (sum % M == 0) + { + flag2 = 1; + solver.init(M + 1); + int need = sum / M; + int s, t; + s = 0, t = M + 1; + for (i = 1; i <= M; i++) + { + solver.addedge(s, i, c[i], 0); + solver.addedge(i, t, need, 0); + } + for (i = 1; i <= M; i++) + { + solver.addedge(i, i == M ? 1 : i + 1, INF, 1); + solver.addedge(i, i == 1 ? M : i - 1, INF, 1); + } + ans += solver.solve(s, t); + } + printf("Case %d: ", ++test); + if (flag1 && flag2) + { + printf("both %d\n", ans); + } + else if (flag1 && !flag2) + { + printf("row %d\n", ans); + } + else if (!flag1 && flag2) + { + printf("column %d\n", ans); + } + else if (!flag1 && !flag2) + { + printf("impossible\n"); + } + } + return 0; +} diff --git a/12368.cpp b/12368.cpp new file mode 100644 index 0000000..ee41e2b --- /dev/null +++ b/12368.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define LL long long +#define nMAX (1 << 10 + 10) +#define mMAX 110 + +int ar[25], n; +int f[nMAX]; +bool vis[nMAX][mMAX]; + +struct node +{ + int a, b, num; +}; + +void init() +{ + int a[100]; + node b[100]; + for (int x = 0; x < (1 << 10); x++) + { + int cnta = 0, cntb = 0; + for (int i = 0; i < 10; i++) + if (x & (1 << i)) + { + a[cnta++] = i; + vis[x][i] = true; + } + for (int i = 0; i < cnta; i++) + for (int j = 0; j < cnta; j++) + if (i != j) + { + int num = a[i] * 10 + a[j]; + b[cntb].a = i, b[cntb].b = j, b[cntb++].num = num; + vis[x][num] = true; + } + for (int i = 0; i < cnta; i++) + for (int j = 0; j < cnta; j++) + if (i != j) + { + int num = a[i] + a[j]; + if (num <= 100) + { + vis[x][num] = true; + } + } + for (int i = 0; i < cnta; i++) + for (int j = 0; j < cntb; j++) + if (i != b[j].a && i != b[j].b) + { + int num = a[i] + b[j].num; + if (num <= 100) + { + vis[x][num] = true; + } + } + for (int i = 0; i < cntb; i++) + for (int j = 0; j < cntb; j++) + if (b[i].a != b[j].a && b[i].a != b[j].b && b[i].b != b[j].a && b[i].b != b[j].b) + { + int num = b[i].num + b[j].num; + if (num <= 100) + { + vis[x][num] = true; + } + } + } +} + +bool ok(int x) +{ + for (int i = 0; i < n; i++) + if (!vis[x][ar[i]]) + { + return false; + } + return true; +} + +int number(int x) +{ + int sum = 0, p = 1; + for (int i = 0; i < 10; i++) + if ((1 << i) & x) + { + sum += i * p; + p *= 10; + } + return sum; +} + +int main() +{ + int T = 1; + init(); + for (int i = 0; i < (1 << 10); i++) + for (int j = 0; j < 10; j++) + if (i & (1 << j)) + { + f[i]++; + } + f[nMAX - 1] = 111; + while (scanf("%d", &n) && n) + { + for (int i = 0; i < n; i++) + { + scanf("%d", &ar[i]); + } + int k = 0, ans = nMAX - 1; + for (int i = 0; i < (1 << 10); i++) + { + if (ok(i) && f[ans] >= f[i]) + { + if (f[ans] == f[i] && number(ans) > number(i)) + { + ans = i; + } + else if (f[ans] != f[i]) + { + ans = i; + } + } + } + printf("Case %d: %d\n", T++, number(ans)); + } + return 0; +} diff --git a/12369.cpp b/12369.cpp new file mode 100644 index 0000000..e74b464 --- /dev/null +++ b/12369.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +double dp[16 * 16 * 16 * 16 * 5 * 5]; +char ss[16 * 16 * 16 * 16 * 5 * 5]; +int D[4]; +int cas; + +double dfs(int mask, int step) +{ + if (ss[mask] == cas) + { + return dp[mask]; + } + int stat[6] = {0}, have[6] = {0}; + ss[mask] = cas; + double &sum = dp[mask] = 0.0; + int j1 = mask / 5 % 5, j2 = mask % 5; + int Mask = mask / 25; + for (int i = 0; i < 4; ++i) + { + have[i] = Mask & 15; + stat[i] = 13 - have[i]; + Mask >>= 4; + } + stat[j1]++, stat[j2]++; + bool check = false; + for (int i = 0; i < 4; ++i) + if (stat[i] < D[i]) + { + check = true; + break; + } + if (!check) + { + return sum = 0.0; + } + if (step == 0) + { + return sum = 1e100; + } + for (int i = 0; i < 4; ++i) + if (have[i]) + { + sum += dfs(mask - (1 << (i * 4)) * 25, step - 1) * have[i]; + } + if (j1 == 4) + { + double sun = 1e100; + for (int i = 0; i < 4; ++i) + { + sun = min(sun, dfs(mask - (4 - i) * 5, step - 1)); + } + sum += sun; + } + if (j2 == 4) + { + double sun = 1e100; + for (int i = 0; i < 4; ++i) + { + sun = min(sun, dfs(mask - (4 - i), step - 1)); + } + sum += sun; + } + sum = sum / step + 1.0; + //printf("%d %d %d %d %d %d : %f\n", stat[0], stat[1], stat[2], stat[3], j1, j2, sum); + return sum; +} + +int main() +{ + int T; + scanf("%d", &T); + for (cas = 1; cas <= T; ++cas) + { + int sum15 = 0, sum14 = 0; + int idx15 = 0, idx14[2] = {0}; + for (int i = 0; i < 4; ++i) + { + scanf("%d", D + i); + if (D[i] == 15) + { + sum15++, idx15 = i; + } + if (D[i] == 14) + { + idx14[sum14++] = i; + } + } + printf("Case %d: ", cas); + if (sum15 * 2 + sum14 > 2) + { + printf("-1.000\n"); + continue; + } + printf("%.3f\n", dfs(1419949, 54)); + } +} diff --git a/1237.cpp b/1237.cpp index efb030b..6ab1799 100644 --- a/1237.cpp +++ b/1237.cpp @@ -1,38 +1,64 @@ -#include -#include +#include + using namespace std; -struct Car { +struct manufacturer +{ string name; - int L, H; + int low, high; }; -int main() { - int T; - cin >> T; - while (T--) { - int N; - cin >> N; - Car db[N]; - for (int i = 0; i < N; i++) - cin >> db[i].name >> db[i].L >> db[i].H; - int Q; - cin >> Q; - while (Q--) { - int P, C = 0; - string Ans = ""; - cin >> P; - for (int i = 0; i < N; i++) { - if (db[i].L <= P && db[i].H >= P) { - ++C; - Ans = db[i].name; +int main() +{ + int t, d, low, high, q, p; + string name; + manufacturer tmp; + vector manufacturers; + cin >> t; + while (t--) + { + cin >> d; + manufacturers.clear(); + while (d--) + { + cin >> tmp.name >> tmp.low >> tmp.high; + manufacturers.push_back(tmp); + } + cin >> q; + while (q--) + { + cin >> p; + bool ok = false; + string result = ""; + for (int i = 0, sz = manufacturers.size(); i < sz; i++) + { + if (manufacturers[i].low <= p && manufacturers[i].high >= p) + { + if (result == "") + { + ok = true; + result = manufacturers[i].name; + } + else if (result != "") + { + ok = false; + break; + } } - if (C > 1) break; } - if (C == 1) cout << Ans << endl; - else cout << "UNDETERMINED\n"; + if (ok) + { + cout << result << endl; + } + else + { + cout << "UNDETERMINED" << endl; + } + } + if (t) + { + cout << endl; } - if (T) cout << endl; } return 0; } diff --git a/12370.cpp b/12370.cpp new file mode 100644 index 0000000..9e0d2b9 --- /dev/null +++ b/12370.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +struct pp +{ + int id, ip; +}; +pp adj[111][333]; + +struct qq +{ + int flag, ip; +}; + +bool vis[666], mark[111]; +int n, m, e[2][333], ch[333], last[333], num[111], c, prev_[111], preu[111]; + +bool find_cycle(int flag, int x, int p) +{ + int i, j, s, q, id, ip; + if (x == p) + { + return true; + } + for (i = 0; i < num[x]; i++) + { + id = adj[x][i].id; + ip = adj[x][i].ip; + if (ch[ip] == flag && !mark[id]) + { + mark[id] = true; + prev_[id] = ((ip << 1) | flag); + preu[id] = x; + if (find_cycle(flag, id, p)) + { + return true; + } + } + } + return false; +} + +bool bfs(int ip) +{ + int id, i, j, s, p, q, temp1, temp2, temp, flag, ipen; + qq queue[666]; + bool inq[666] = {0}; + temp = 0; + queue[temp++].ip = (ip << 1); + queue[temp++].ip = (ip << 1) | 1; + inq[ip << 1] = true; + inq[(ip << 1) | 1] = true; + temp1 = 0; + temp2 = temp - 1; + while (temp1 <= temp2) + { + for (i = temp1; i <= temp2; i++) + { + ipen = queue[i].ip; + memset(mark, false, sizeof(mark)); + mark[e[0][ipen >> 1]] = true; + if (!find_cycle(ipen & 1, e[0][ipen >> 1], e[1][ipen >> 1])) + { + while ((ipen >> 1) != ip) + { + ch[ipen >> 1] ^= 1; + ipen = last[ipen]; + } + ch[ip] = (ipen & 1); + return 1; + } + for (j = e[1][ipen >> 1]; j != e[0][ipen >> 1]; j = preu[j]) + { + int ipe = prev_[j] ^ 1; + if (!inq[ipe]) + { + inq[ipe] = true; + queue[temp++].ip = ipe; + last[ipe] = queue[i].ip; + } + } + } + temp1 = temp2 + 1; + temp2 = temp - 1; + } + return 0; +} + +int main() +{ + int i, j, s, p, q, u, v, T, u1, v1, u2, v2, t = 0; + scanf("%d", &T); + while (T--) + { + t++; + scanf("%d%d", &n, &m); + memset(num, 0, sizeof(num)); + memset(ch, -1, sizeof(ch)); + c = 0; + for (i = 0; i < m; i++) + { + scanf("%d%d", &e[0][i], &e[1][i]); + u = e[0][i]; + v = e[1][i]; + adj[u][num[u]].id = v; + adj[u][num[u]++].ip = i; + adj[v][num[v]].id = u; + adj[v][num[v]++].ip = i; + c += bfs(i); + } + printf("Case %d: ", t); + if (c == 2 * (n - 1)) + { + puts("YES"); + } + else + { + puts("NO"); + } + } + return 0; +} diff --git a/12371.cpp b/12371.cpp new file mode 100644 index 0000000..e2d06c7 --- /dev/null +++ b/12371.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int maxn = 100100; +const int mod = 1000000007; + +long long powmod(long long a, long long b) +{ + long long ret = 1; + while (b) + { + if (b & 1) + ret = ret * a % mod; + b >>= 1; + a = a * a % mod; + } + return ret; +} +long long mi[maxn]; + +long long C(int n, int m) +{ + return mi[n] * powmod(mi[m], mod - 2) % mod * powmod(mi[n - m], mod - 2) % mod; +} +int dp[maxn][51]; +int dp1[maxn]; + +int main() +{ + int i, j, k, n, m; + + dp[3][1] = 2; + for (i = 4; i <= 100000; i++) + for (j = 1; j <= 50 && j * 3 <= i; j++) + dp[i][j] = (dp[i - 3][j - 1] * (i - 1ll) % mod * (i - 2ll) % mod + dp[i - 1][j] * (i - 1ll) % mod) % mod; + for (mi[0] = i = 1; i < maxn; i++) + mi[i] = mi[i - 1] * i % mod; + dp1[0] = 1; + for (i = 1; i <= 50; i++) + dp1[i] = dp1[i - 1] * (i * 2 - 1ll) % mod; + for (i = 1; i <= 50; i++) + { + for (j = 1; j <= i; j++) + dp1[i] = dp1[i] * C(i * 2 - j * 2 + 2, 2) % mod; + } + + int t, ii = 0; + + dp[0][0] = 1; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &m); + + long long ans = 0; + if (m * 2 <= n) + { + for (i = 0; i <= m; i++) + ans = (ans + (C(n, i * 2) * dp1[i] % mod * (dp[n - i * 2][m - i] * powmod(powmod(2, m - i), mod - 2) % mod) % mod) * (C(n, i * 2) * mi[n - i * 2] % mod) % mod) % mod; + } + printf("Case %d: %lld\n", ++ii, ans); + } +} diff --git a/12372.cpp b/12372.cpp index d36f502..a7386d8 100644 --- a/12372.cpp +++ b/12372.cpp @@ -1,15 +1,19 @@ -#include +#include -int main() { - int T; - int L, W, H; +using namespace std; + +int main() +{ + int T, L, W, H, C = 0; scanf("%d", &T); - for (int t = 1; t <= T; t++) { + while (T--) + { scanf("%d %d %d", &L, &W, &H); + printf("Case %d: ", ++C); if (L > 20 || W > 20 || H > 20) - printf("Case %d: bad\n", t); + puts("bad"); else - printf("Case %d: good\n", t); + puts("good"); } return 0; } diff --git a/12373.cpp b/12373.cpp new file mode 100644 index 0000000..c1fb63b --- /dev/null +++ b/12373.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +struct IN +{ + int xx, yy, zz; +} mat[10000]; +long long a; +map ync; +int tot = 0; + +void solve() +{ + ync.clear(); + for (int i = 1; i <= 1415; i++) + { + ync[i * i] = i; + } + tot = 0; + for (int i = 1; i <= 1000; i++) + { + for (int j = i; j <= 1000; j++) + { + int cnt = i * i + j * j; + if (ync[cnt]) + { + mat[tot].xx = i; + mat[tot].yy = j; + mat[tot].zz = ync[cnt]; + tot++; + } + } + } +} + +int main() +{ + int cas, w, h; + solve(); + cin >> cas; + for (int ca = 1; ca <= cas; ca++) + { + scanf("%d%d", &w, &h); + a = 0; + for (int i = 1; i <= w / 2; i++) + { + int ww = min(i, (w - i - i) / 2); + for (int j = 1; j <= ww; j++) + { + int x = 2 * (i + j); + int y = 2 * max(i, j); + if (y <= h) + { + if (i != j) + { + a += 2 * (w - x + 1) * (h - y + 1); + } + else + { + a += (w - x + 1) * (h - y + 1); + } + } + } + } + for (int i = 1; i <= h / 2; i++) + { + int hh = min(i, (h - i - i) / 2); + for (int j = 1; j <= hh; j++) + { + int x = 2 * (i + j); + int y = 2 * max(i, j); + if (y <= w) + { + if (i != j) + { + a += 2 * (h - x + 1) * (w - y + 1); + } + else + { + a += (h - x + 1) * (w - y + 1); + } + } + } + } + for (int i = 0; i < tot; i++) + { + int x = mat[i].xx + mat[i].zz; + int y = mat[i].yy + mat[i].zz; + for (int j = 1; j < mat[i].zz; j++) + { + int r = mat[i].zz - j; + int k1 = max(x, max(2 * r, 2 * j)); + int k2 = max(y, max(2 * r, 2 * j)); + if (k1 <= w && k2 <= h) + { + a += 2 * (w + 1 - k1) * (h + 1 - k2); + } + if (k2 <= w && k1 <= h) + { + a += 2 * (h + 1 - k1) * (w + 1 - k2); + } + } + } + printf("Case %d: ", ca); + cout << a << endl; + } + return 0; +} diff --git a/12374.cpp b/12374.cpp new file mode 100644 index 0000000..f68b755 --- /dev/null +++ b/12374.cpp @@ -0,0 +1,182 @@ +#include + +using namespace std; + +#define Fill(a, b) memset(a, b, sizeof(a)) +#define Copy(a, b) memcpy(a, b, sizeof(b)) +#define rep(i, a, b) for (int i = a; i <= b; i++) +#define rrep(i, a, b) for (int i = a; i >= b; i--) +#define erep(j, a) for (Tedge *j = e[a]; j; j = j->next) +#define irep(i, a) for (__typeof(a.begin()) i = a.begin(); i != a.end(); i++) +#define pb push_back +#define mp make_pair +#define vec vector +#define sf scanf +#define pf printf +#define sz(x) int(x.size()) +#define eps 1e-9 +#define bit_cnt(x) __builtin_popcount(x) +#define sqr(a) ((a) * (a)) + +typedef long double ld; +typedef long long ll; +const ld EPS = 1e-9; +const ld DINF = 1e10; +inline int dcmp(const ld &a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); } + +const int MAXN = 20; + +ld d[MAXN]; + +struct Tpoint +{ + ld x, y; + Tpoint() { x = y = 0; } + Tpoint(ld _x, ld _y) : x(_x), y(_y) {} + Tpoint operator+(const Tpoint &a) const + { + return Tpoint(x + a.x, y + a.y); + } + Tpoint operator-(const Tpoint &a) const + { + return Tpoint(x - a.x, y - a.y); + } + Tpoint operator*(const ld &k) const + { + return Tpoint(x * k, y * k); + } + Tpoint operator/(const ld &k) const + { + return Tpoint(x / k, y / k); + } + Tpoint &operator=(const Tpoint &a) + { + x = a.x, y = a.y; + return *this; + } + bool operator<(const Tpoint &a) const + { + return dcmp(x - a.x) < 0 || (!dcmp(x - a.x) && dcmp(y - a.y) < 0); + } + bool operator==(const Tpoint &a) const + { + return !dcmp(x - a.x) && !dcmp(y - a.y); + } + bool operator!=(const Tpoint &a) const + { + return dcmp(x - a.x) || dcmp(y - a.y); + } +} p[MAXN], np[MAXN], t[MAXN]; + +ld dist(const Tpoint &a, const Tpoint &b) +{ + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); +} + +ld length(const Tpoint &a) +{ + return sqrt(sqr(a.x) + sqr(a.y)); +} + +struct Tline +{ + ld a, b, c; + Tline() {} + Tline(ld _a, ld _b, ld _c) : a(_a), b(_b), c(_c) {} + Tline(ld x1, ld y1, ld x2, ld y2) : a(y1 - y2), b(x2 - x1), c(x1 * y2 - x2 * y1) {} +} l[MAXN], e[MAXN]; + +Tpoint intersect(const Tline &la, const Tline &lb) +{ + ld x = (la.b * lb.c - la.c * lb.b) / (la.a * lb.b - la.b * lb.a); + ld y = (la.a * lb.c - la.c * lb.a) / (la.b * lb.a - la.a * lb.b); + return Tpoint(x, y); +} + +bool is_parallel(const Tline &la, const Tline &lb) +{ + return !dcmp(la.a * lb.b - la.b * lb.a); +} + +bool input() +{ + ld sum = 0.0; + rep(i, 1, 8) + { + cin >> p[i].x >> p[i].y; + //sf("%Lf %Lf", &p[i].x, &p[i].y); + //ll a,b; + //sf("%lld.%lld",&a,&b); + //p[i].x = (ld)a + (ld)b / 1e12; + //sf("%lld.%lld",&a,&b); + //p[i].y = (ld)a + (ld)b / 1e12; + sum += p[i].x + p[i].y; + } + return dcmp(sum); +} + +int main() +{ + cout << fixed << setprecision(12); + while (input()) + { + Tpoint p1, p0, dp; + rep(i, 1, 4) p1 = p1 + p[i]; + rep(i, 5, 8) p0 = p0 + p[i]; + p1 = p1 / 4.0; + p0 = p0 / 4.0; + dp = p0 - p1; + ld len = 4.0 * length(dp); + if (!dcmp(len)) + { + rep(i, 1, 4) cout << p[i].x << " " << p[i].y << endl; + } + else + { + rep(i, 1, 4) l[i] = Tline(dp.y, -dp.x, dp.x * p[i].y - p[i].x * dp.y); + p[9] = p[5]; + rep(i, 5, 8) e[i - 4] = Tline(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y); + rep(i, 1, 4) + { + d[i] = DINF; + rep(j, 1, 4) if (!is_parallel(l[i], e[j])) + { + Tpoint q = intersect(l[i], e[j]); + if (dcmp((q.x - p[i].x) * dp.x) > 0) + { + if (dcmp(d[i] - dist(q, p[i])) > 0) + d[i] = dist(q, p[i]), t[i] = q; + } + } + } + //#define debug + //#ifdef debug + //rep (i, 1, 4) cout << t[i].x << " " << t[i].y << endl; + //#endif + + rep(i, 1, 4) np[i] = p[i]; + rep(i, 1, 4) + { + if (dcmp(d[i] - len) >= 0) + { + np[i] = np[i] + dp / length(dp) * len; + break; + } + else + { + np[i] = t[i]; + len -= d[i]; + } + } + rep(i, 1, 4) cout << np[i].x << " " << np[i].y << endl; + //#define debug + //#ifdef debug + //ld sum = 0.0; + //rep (i, 1, 4) sum += dist(np[i], p[i]); + //cout << sum << " " << 4.0 * length(dp) << endl; + //#endif + } + cout << endl; + } + return 0; +} diff --git a/12375.cpp b/12375.cpp new file mode 100644 index 0000000..4abd463 --- /dev/null +++ b/12375.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +const int fl[4][2] = {-1, -1, -1, 1, 1, 1, 1, -1}, f[4][2] = {-1, -1, -1, 0, 0, 0, 0, -1}; + +double ans[10001]; +int i, j, n, m, le, ri, l[10001][2], ma[105][105], map_[105][105], s, q, x, y, x0, y0_, nx, ny, ans0; + +int main() +{ + int k; + scanf("%d", &k); + s = 0; + while (scanf("%d%d", &n, &m) != EOF) + { + s++; + ans0 = -1; + getchar(); + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + k = getchar(); + map_[i][j] = k - '0'; + } + getchar(); + } + for (i = 0; i <= n; i++) + for (j = 0; j <= m; j++) + { + ma[i][j] = -1; + } + printf("Case %d:\n", s); + scanf("%d", &q); + for (k = 1; k <= q; k++) + { + scanf("%d%d", &x, &y); + if ((x + y) % 2 == 1) + { + printf("0.0000\n"); + continue; + } + if (x % 2 == 1) + { + if (map_[x / 2][y / 2]) + { + x--; + y--; + } + else + { + x--; + y++; + } + } + if (ma[x / 2][y / 2] == -1) + { + ans0++; + ans[ans0] = 0.0; + l[1][0] = x / 2; + l[1][1] = y / 2; + le = 1; + ri = 1; + ma[x / 2][y / 2] = ans0; + while (le <= ri) + { + for (i = 0; i < 4; i++) + { + nx = l[le][0] + f[i][0]; + ny = l[le][1] + f[i][1]; + x0 = l[le][0] + fl[i][0]; + y0_ = l[le][1] + fl[i][1]; + if (nx >= n || nx < 0) + { + continue; + } + if (ny >= m || ny < 0) + { + continue; + } + if ((map_[nx][ny] ^ (i % 2)) && (ma[x0][y0_] == -1)) + { + ri++; + l[ri][0] = x0; + l[ri][1] = y0_; + ma[x0][y0_] = ans0; + } + if (map_[nx][ny] ^ (i % 2)) + { + ans[ans0] += 2.0f - M_PI / 4.0f; + } + else + { + ans[ans0] += M_PI / 4.0f; + } + } + le++; + } + } + printf("%.4lf\n", ans[ma[x / 2][y / 2]]); + } + } + return 0; +} diff --git a/12376.cpp b/12376.cpp new file mode 100644 index 0000000..0b84203 --- /dev/null +++ b/12376.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +const int NN = 128; + +struct edge +{ + int nxt, to; +} e[NN * NN]; + +bool v[NN]; +int ecnt, nxt[NN], value[NN]; + +void add_edge(int a, int b) +{ + edge &E = e[ecnt]; + E.to = b; + E.nxt = nxt[a]; + nxt[a] = ecnt++; +} + +void read_edge() +{ + int from, to; + scanf("%d %d", &from, &to); + add_edge(from, to); +} + +int main() +{ + int learned(0); + scanf("%*d"); + for (int cnum = 0, m, n; scanf("%d %d", &n, &m) == 2; learned = 0) + { + FOR(i, n) + { + scanf("%d", value + i); + nxt[i] = -1; + v[i] = false; + } + ecnt = 0; + FOR(i, m) + read_edge(); + int u(0), v(0), w(-1); + while (1) + { + u = v; + learned += value[u]; + for (int at = nxt[u]; at != -1; at = e[at].nxt) + { + if (value[e[at].to] > w) + { + w = value[v = e[at].to]; + } + } + if (u == v) + { + break; + } + w = -1; + } + printf("Case %d: %d %d\n", ++cnum, learned, u); + } + return 0; +} diff --git a/12377.cpp b/12377.cpp new file mode 100644 index 0000000..77a9c2f --- /dev/null +++ b/12377.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +typedef long long LL; +const int N = 30; +const int C[10] = {1, 9, 36, 84, 126, 126, 84, 36, 9, 1}; +const int calu[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880}; +char str[N]; +LL len, num, sum, state[N]; + +void dfs(int ind, int cnt) +{ + if (cnt > num + 1 || str[ind] == '0') + { + return; + } + if (cnt == num + 1) + { + if (ind != len) + { + return; + } + LL res = calu[num], tmp = 1; + for (int i = 1; i < cnt; i++) + { + //printf("%lld ",state[i]); + if (state[i] != state[i - 1]) + { + res /= calu[tmp]; + tmp = 1; + } + else + { + tmp++; + } + } + //puts(""); + if (tmp != 1) + { + res /= tmp; + } + sum += res; + } + LL data = 0; + for (int i = ind; i < len; i++) + { + data = data * 10 + str[i] - '0'; + state[cnt] = data; + if (data >= state[cnt - 1]) + { + dfs(i + 1, cnt + 1); + } + } +} +int main() +{ + int n; + scanf("%d", &n); + while (n--) + { + sum = 0; + scanf("%s", str); + len = (int)strlen(str); + num = str[0] - '0'; + dfs(1, 1); + cout << sum * C[num] << endl; + } + return 0; +} diff --git a/12378.cpp b/12378.cpp new file mode 100644 index 0000000..8cef981 --- /dev/null +++ b/12378.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +#define REP(a, n) for (int a = 0; a < (n); ++a) +#define FOR(a, b, c) for (int a = (b); a <= (c); ++a) +#define FORD(a, b, c) for (int a = (b); a >= (c); --a) +#define swap(t, a, b) t = a, a = b, b = t +#define CL(a, b) memset(a, b, sizeof(a)) +#define WI(a) cout << #a << " : " << a << endl + +typedef long long ll; +typedef unsigned long long ull; + +char box[500010]; +int p[500010]; +int tot; + +void reduce() +{ + tot = 1; + int len = strlen(box); + FOR(i, 1, len - 1) + { + if (box[i] != box[i - 1]) + { + box[tot++] = box[i]; + } + } + box[tot] = '\0'; +} + +void modify() +{ + FORD(i, tot - 1, 0) + { + box[(i + 1) * 2] = box[i]; + } + tot = tot * 2 + 1; + box[0] = '$'; + FOR(i, 1, tot - 1) + { + if (i & 1) + { + box[i] = '#'; + } + } + box[tot++] = '#'; + box[tot] = '\0'; +} +int main() +{ + int n; + int t; + int maxE = -1; + int mx, id; + scanf("%d\n", &t); + while (t--) + { + maxE = -1; + gets(box); + // WI(box); + reduce(); + // WI(box); + modify(); + // WI(box); + mx = 0; + FOR(i, 1, tot - 1) + { + if (mx > i) + { + p[i] = min(p[2 * id - i], mx - i); + } + else + { + p[i] = 1; + } + while (box[i - p[i]] == box[i + p[i]]) + { + p[i]++; + } + if (i + p[i] > mx) + { + mx = i + p[i]; + id = i; + } + } + int maxE = -1; + // WI(box[tot-1]); + FOR(i, 1, tot - 1) + { + if (p[i] - 1 > maxE) + { + maxE = p[i] - 1; + } + } + printf("%d\n", (maxE + 1) / 2); + } + return 0; +} diff --git a/12379.cpp b/12379.cpp new file mode 100644 index 0000000..e2321fe --- /dev/null +++ b/12379.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +const int MAX_N = 1e4; + +int N; +vector graph[MAX_N]; +bool visited[MAX_N]; +int dist[MAX_N]; + +void init() +{ + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + graph[i].clear(); + } + for (int i = 0; i < N; i++) + { + int K; + scanf("%d", &K); + for (int j = 0; j < K; j++) + { + int x; + scanf("%d", &x); + x--; + graph[i].push_back(x); + } + } +} + +void dfs(int v) +{ + visited[v] = true; + for (int i = 0; i < (int)graph[v].size(); i++) + { + int u = graph[v][i]; + if (!visited[u]) + { + dist[u] = dist[v] + 1; + dfs(u); + } + } +} + +int sub() +{ + memset(visited, false, sizeof(visited)); + dist[0] = 0; + dfs(0); + int p = max_element(dist, dist + N) - dist; + memset(visited, false, sizeof(visited)); + dist[p] = 0; + dfs(p); + return *max_element(dist, dist + N); +} + +int solve() +{ + return 2 * N - 2 - sub(); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/1238.cpp b/1238.cpp new file mode 100644 index 0000000..b0a6e45 --- /dev/null +++ b/1238.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +char dp[35][35][6006]; +int op[105], num[105], nidx = 0, oidx = 0; +int diff[6006] = {}; + +void dfs(int idx, int right, int sum, int flip) +{ + if (idx == nidx) + { + diff[sum + 3000] = 1; + return; + } + if (dp[idx][right][sum + 3000]) + return; + dp[idx][right][sum + 3000] = 1; + // no operator + dfs(idx + 1, right, sum + flip * op[idx] * num[idx], flip); + if (op[idx] == -1)// add -(number + dfs(idx + 1, right + 1, sum + (-flip) * num[idx], -flip); + if (right >= 1) + {// (-##### add )[+|-] + //no operator + dfs(idx + 1, right - 1, sum + (-flip) * op[idx] * num[idx], -flip); + // add -(number + if (op[idx] == -1) + dfs(idx + 1, right, sum + flip * num[idx], flip); + } +} + +int main() +{ + char cmd[1005]; + int i, j, k; + while (gets(cmd)) + { + int len = strlen(cmd); + op[0] = 1; + nidx = 0, oidx = 1; + for (i = 0; i < len;) + { + while (cmd[i] == ' ') + i++; + if (cmd[i] >= '0' && cmd[i] <= '9') + { + int tmp = 0; + while (cmd[i] >= '0' && cmd[i] <= '9') + tmp = tmp * 10 + cmd[i] - '0', i++; + num[nidx++] = tmp; + } + else if (cmd[i] == '+' || cmd[i] == '-') + { + if (cmd[i] == '+') + op[oidx++] = 1; + else + op[oidx++] = -1; + i++; + } + } + memset(dp, 0, sizeof(dp)); + memset(diff, 0, sizeof(diff)); + dfs(0, 0, 0, 1); + int ret = 0; + for (i = 0; i <= 6000; i++) + ret += diff[i]; + printf("%d\n", ret); + } + return 0; +} diff --git a/12380.cpp b/12380.cpp new file mode 100644 index 0000000..d1a86c5 --- /dev/null +++ b/12380.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define eps 1e-9 +#define ll long long +char str[5000]; + +void get_data() +{ + scanf("%s", str); +} + +int stk[5000], sn, stt[5000]; + +void disp() +{ + int i; + for (i = 0; i < sn; i++) + { + printf("%d*%d ", stk[i], stt[i]); + } + printf("\n"); +} + +int run() +{ + int i, vmax = 1; + sn = 0; + stk[sn++] = 0; + stt[sn - 1] = 0; + for (i = 0; str[i]; i++) + { + if (str[i] == '2') + { + stk[sn++] = 0; + stt[sn - 1] = 0; + if (sn > vmax) + { + vmax = sn; + } + } + else + { + stk[sn - 1]++; + stt[sn - 1] |= (1 << (str[i] - '0')); + while (stk[sn - 1] == 4) + { + if (stt[sn - 1] != 3) + { + return -1; + } + sn--; + stk[sn - 1]++; + stt[sn - 1] |= 3; + } + } + } + if (sn == 1 && stk[sn - 1] == 1) + { + return vmax; + } + return -1; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + get_data(); + int r = run(); + if (r == -1) + { + printf("Not Possible\n"); + } + else + { + printf("2^%d*2^%d\n", r - 1, r - 1); + } + } + return 0; +} diff --git a/12381.cpp b/12381.cpp new file mode 100644 index 0000000..9811adf --- /dev/null +++ b/12381.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +const int MAXN = 10010; + +struct Rank +{ + int a, b; + int id; + bool operator<(const Rank &r) const + { + if (a == r.a && b == r.b) + { + return id > r.id; + } + else if (a == r.a) + { + return b > r.b; + } + else + { + return a > r.a; + } + } +} rank_[MAXN]; + +inline int EPS(double x, double y) +{ + if (fabs(x - y) < 1e-8) + { + return 0; + } + return x < y ? -1 : 1; +} +struct Pass +{ + int from, to; + double k; + Pass(int from, int to, double k) : from(from), to(to), k(k) {} + bool operator<(const Pass &p) const + { + return EPS(k, p.k) > 0; + } +}; + +int n, m; +vector vec; + +int main() +{ + int cas; + scanf("%d", &cas); + while (cas--) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < n; ++i) + { + scanf("%d%d", &rank_[i].a, &rank_[i].b); + rank_[i].id = i; + } + vec.clear(); + for (int i = 0; i < m; ++i) + { + for (int j = m; j < n; ++j) + { + if (rank_[i].a > rank_[j].a && rank_[i].b < rank_[j].b) + { + vec.push_back(Pass(j, i, (rank_[j].b - rank_[i].b + 0.0) / (rank_[i].a - rank_[j].a))); + } + else if (rank_[i].a < rank_[j].a && rank_[j].b < rank_[i].b) + { + vec.push_back(Pass(i, j, (rank_[i].b - rank_[j].b + 0.0) / (rank_[j].a - rank_[i].a))); + } + } + } + if (n <= 1) + { + printf("0.0000\n"); + continue; + } + sort(rank_, rank_ + n); + long long ans = 0, tmp = 0; + for (int i = 0; i < n - 1; ++i) + { + if (rank_[i].id < m) + { + tmp++; + } + ans += tmp; + } + sort(vec.begin(), vec.end()); + int len = vec.size(); + tmp = ans; + for (int i = 0, j; i < len; i = j) + { + for (j = i; j < len; ++j) + { + if (EPS(vec[i].k, vec[j].k) != 0) + { + break; + } + if (vec[j].from < m) + { + tmp++; + } + else + { + tmp--; + } + } + ans = max(ans, tmp); + } + printf("%.4f\n", ans / (n - 1.0)); + } + return 0; +} diff --git a/12382.cpp b/12382.cpp new file mode 100644 index 0000000..6632178 --- /dev/null +++ b/12382.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define NN 1024 +int r[NN]; + +int main() +{ + int t; + scanf("%d", &t); + for (int rows, cols; t-- && scanf("%d %d", &rows, &cols) == 2;) + { + int lighten = 0; + for (int i = 0; i < rows; ++i) + { + scanf("%d", r + i); + lighten += r[i]; + } + for (int i = 0, z; i < cols; ++i) + { + scanf("%d", &z); + sort(r, r + rows); + for (int row = rows; z && row--;) + { + if (!r[row]) + { + break; + } + z -= r[row] > 0; + r[row] -= r[row] > 0; + } + lighten += z; + } + printf("%d\n", lighten); + } + return 0; +} diff --git a/12383.cpp b/12383.cpp new file mode 100644 index 0000000..a07b883 --- /dev/null +++ b/12383.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +const int MAX_T = (int)1e5; +const double EPS = 1e-8; + +int N, M; +double dp[MAX_T + 1]; +double accum[MAX_T + 2]; + +void init() +{ + scanf("%d%d", &N, &M); +} + +double solve1() +{ + dp[1] = 1.0 / M; + accum[2] = dp[1]; + for (int i = 2; i <= N; i++) + { + double over_prob = (i < M ? (double)(M - i) / M : 0.0); + double win_prob = (i <= M ? 1.0 / M : 0.0); + int low = (M < i ? M : i - 1); + double p = win_prob + 1.0 / M * ((double)low - (accum[i] - accum[i - low])); + dp[i] = p; + accum[i + 1] = accum[i] + p; + } + return dp[N]; +} + +double solve2() +{ + double prev = 1.0, next = 1.0, prev2 = 1.0; + for (int i = 2; i <= N; i++) + { + double win_prob = (i <= M ? (double)(M - i + 1) / M : 0.0); + int low = (M < i ? M : i - 1); + next = win_prob; + if (i == 2) + { + next += (double)low / M * (1.0 - prev); + } + else + { + next += (double)1.0 / M * (1.0 - prev); + next += (double)(low - 1.0) / M * (1.0 - min(prev, prev2)); + } + prev2 = prev; + prev = next; + } + return next; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + init(); + printf("%.4f %.4f\n", solve1() + EPS, solve2() + EPS); + } + return 0; +} diff --git a/12384.cpp b/12384.cpp new file mode 100644 index 0000000..6853185 --- /dev/null +++ b/12384.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define maxp 2000005 +#define maxn 100010 + +int a[maxp]; +int x; +int prime[maxp], k = 0; +int sta[maxn]; +stack s; + +void pri() +{ + int i = 0, j = 0; + int t = 0; + for (i = 2; i <= maxp; i++) + { + if (!a[i]) + { + prime[k++] = i; + if (k > maxn) + { + break; + } + } + if (i == 100016) + { + t = 0; + } + for (j = 0; j < k && i * prime[j] <= maxp; j++) + { + a[i * prime[j]] = 1; + if (!(i % prime[j])) + { + break; + } + } + } +} + +int main() +{ + int n = 0, m = 0, t = 0; + int ans = 0; + int i = 0; + pri(); + scanf("%d", &t); + while ((t--) > 0) + { + scanf("%d%d", &n, &m); + ans = 0; + while (!s.empty()) + { + s.pop(); + } + for (i = 0; i < n; i++) + { + x = prime[i] % m; + while (!s.empty() && s.top() <= x) + { + s.pop(); + } + if (i == 7) + { + k = 0; + } + if (s.empty()) + { + ans = (ans + i + 1) % m; + } + else + { + ans = (ans + i - sta[s.top()]) % m; + } + s.push(x); + sta[x] = i; + } + printf("%d\n", ans % m); + } + return 0; +} diff --git a/12385.cpp b/12385.cpp new file mode 100644 index 0000000..b2c8021 --- /dev/null +++ b/12385.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +const int MAX_N = (int)1e5; + +int N; +int as[MAX_N]; + +void init() +{ + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", as + i); + } +} + +int solve() +{ + int ans = 0; + set lefts; + lefts.insert(as[0]); + for (int i = 1; i < N; i++) + { + if (!lefts.insert(as[i]).second) + { + lefts.clear(); + lefts.insert(as[i]); + ans++; + } + } + return ans; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/12386.cpp b/12386.cpp new file mode 100644 index 0000000..f0b4ac1 --- /dev/null +++ b/12386.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define eps 1e-9 +#define ll long long + +double f_abs(double x) +{ + return x < 0 ? -x : x; +} +struct Point +{ + double x, y; + void disp() + { + printf("%lf %lf\n", x, y); + } +}; +struct Line +{ + Point s, e; + void disp() + { + s.disp(); + e.disp(); + } + double area() + { + return (s.x * e.y - s.y * e.x) / 2; + } + double len() + { + return sqrt((s.x - e.x) * (s.x - e.x) + (s.y - e.y) * (s.y - e.y)); + } +}; +Point pt[15]; +int N; +void get_data() +{ + int i; + scanf("%d", &N); + for (i = 0; i < N; i++) + { + scanf("%lf%lf", &pt[i].x, &pt[i].y); + } +} +Line le[15], tl; +bool mk[15]; +double minarea, L1, L2; +double xmult(Point a, Line b) +{ + return (b.s.x - a.x) * (b.e.y - a.y) - (b.e.x - a.x) * (b.s.y - a.y); +} +double pmult(Point a, Line b) +{ + return (b.s.x - a.x) * (b.e.x - a.x) + (b.e.y - a.y) * (b.s.y - a.y); +} +bool contra(Line a, Line b) +{ + double x = xmult(a.s, b) * xmult(a.e, b), y = xmult(b.s, a) * xmult(b.e, a); + return x < eps && y < eps; +} +bool collison(Line a, Line b) +{ + if (f_abs(xmult(a.s, b)) < eps && f_abs(xmult(a.e, b)) < eps && f_abs(xmult(b.s, a)) < eps && f_abs(xmult(b.e, a)) < eps) + { + return pmult(a.s, b) < -eps || pmult(a.e, b) < -eps || pmult(b.s, a) < -eps || pmult(b.e, a) < -eps; + } + return 0; +} +bool ok(int s, int e) +{ + int i; + for (i = s; i < e; i++) + { + if (contra(tl, le[i])) + { + return 0; + } + } + return 1; +} +void disp(int n) +{ + int i; + for (i = 0; i < n; i++) + { + le[i].disp(); + } + printf("----\n"); +} +void dfs(int loc, int last, double ar, double len) +{ + if (loc == N - 1) + { + tl.s = pt[last]; + tl.e = pt[0]; + if (ok(1, loc - 1) && !collison(tl, le[0]) && !collison(tl, le[loc - 1])) + { + le[loc].s = pt[last]; + le[loc].e = pt[0]; + ar += le[loc].area(); + len += le[loc].len(); + if (ar < 0) + { + ar = -ar; + } + if (minarea < -0.5 || minarea > ar || f_abs(minarea - ar) < eps && len < L1) + { + minarea = ar; + L1 = len; + } + if (L2 < -0.5 || L2 > len) + { + L2 = len; + } + } + return; + } + int i; + for (i = 1; i < N; i++) + { + if (mk[i]) + { + continue; + } + tl.s = pt[last]; + tl.e = pt[i]; + if (!ok(0, loc - 1) || collison(tl, le[loc - 1])) + { + continue; + } + mk[i] = 1; + le[loc] = tl; + dfs(loc + 1, i, ar + tl.area(), len + tl.len()); + mk[i] = 0; + } +} +void run() +{ + memset(mk, 0, sizeof(mk)); + minarea = L2 = -1; + dfs(0, 0, 0, 0); + printf("%.4lf\n", L1 - L2); +} +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + get_data(); + run(); + } + return 0; +} diff --git a/12387.cpp b/12387.cpp new file mode 100644 index 0000000..98330ea --- /dev/null +++ b/12387.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +typedef long long ll; + +const int mod = 100000007, degrees = 360000; + +bool visited[degrees]; +int angles[degrees + 1], n; + +int gcd(int a, int b) +{ + if (a < b) + { + swap(a, b); + } + return (b == 0) ? a : gcd(b, a % b); +} + +// # cycles of a rot by x milli-deg, or -1 if invalid +int cycles(int x) +{ + int order = degrees / gcd(degrees, x); + if (n % order != 0) + { + return -1; + } + for (int i = 0; i < n; ++i) + { + if (!visited[(angles[i] + x) % degrees]) + { + return -1; + } + } + return n / order; +} + +ll raise(ll x, int n) +{ + ll ret = 1; + while (n > 0) + { + if (n & 1) + { + ret = (ret * x) % mod; + } + n >>= 1; + x = (x * x) % mod; + } + return ret; +} + +ll inverse(ll x) +{ + return raise(x, mod - 2); +} + +int main() +{ + ll nsymb; + // Precompute the divisors of 'degrees'; + // there are 2^{O(log n / log log n)} = n^{o(1)} of them. + vector div; + for (int i = 1; i <= degrees; ++i) + if (degrees % i == 0) + { + div.push_back(i); + } + while (scanf("%lli%i", &nsymb, &n), n > 0) + { + fill(&visited[0], &visited[degrees], false); + for (int i = 0; i < n; ++i) + { + scanf("%i", &angles[i]); + visited[angles[i]] = true; + } + sort(&angles[0], &angles[n]); + angles[n] = angles[0] + degrees; + // Look for the smallest valid rotation angle. + ll ret = 0; + int ang = -1; + for (int i = 0; i < div.size(); ++i) + { + int c = cycles(div[i]); + if (c >= 0) + { + ang = div[i]; + break; + } + } + // Loop over iterations of the smallest rotation. + for (int i = 1; ang * i <= degrees; ++i) + { + int s = gcd(degrees, ang * i); + ret += raise(nsymb, n / (degrees / s)); + ret %= mod; + } + ret = (ret * inverse(degrees / ang)) % mod; + printf("%lli\n", ret); + } + return 0; +} diff --git a/12388.cpp b/12388.cpp new file mode 100644 index 0000000..bbfac93 --- /dev/null +++ b/12388.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +const int maxrounds = 300, maxlabels = 10000; +const int maxenv = 2 * maxrounds, maxn = 2 * maxenv; + +int labels[maxn], idx[maxlabels], n; // for country labels +int rounds, coins[maxenv][2], chosen[maxenv], last[2 * maxn], visited[2 * maxn]; + +// graph +vector edges[maxn]; + +// get the other endpoint of an edge e +int endpoint(int e, int a) +{ + return coins[e][0] + coins[e][1] - a; +} + +// is edge e (in envelop e / 2) currently chosen? +bool is_chosen(int e) +{ + return chosen[e / 2] == (e & 1); +} + +int depth[maxn], prev_[maxn]; + +// DFS from vertex (country) v +void dfs(int v, int d = 0, int p = -1) +{ + depth[v] = d; + prev_[v] = p; + for (int i = 0; i < edges[v].size(); ++i) + { + int e = edges[v][i], w = endpoint(e, v); + if (is_chosen(e) && depth[w] < 0) + { + dfs(w, d + 1, e); + } + } +} + +int parent(int a) +{ + return prev_[a] >= 0 ? endpoint(prev_[a], a) : -1; +} + +bool find_cycle(int e, vector &cycle) +{ + int a = coins[e][0], b = coins[e][1]; + fill(&depth[0], &depth[n], -1); + dfs(a); + if (depth[b] < 0) + { + return false; + } + while (b != a) + { + cycle.push_back(prev_[b]); + b = parent(b); + } + return true; +} + +bool process_envelop(int i) +{ + chosen[i] = -1; + deque q; + for (int j = 0; j < 2; ++j) + { + visited[2 * i + j] = i; + q.push_back(2 * i + j); + } + // BFS to find an augmenting path + while (!q.empty()) + { + int add = q.front(); + q.pop_front(); // edge to add + vector cycle; + // if the edge does not create a cycle + if (!find_cycle(add, cycle)) + { + // trace the path back to one of the two edges in this round + while (add / 2 != i) + { + chosen[add / 2] ^= 1; + add = last[add]; + } + chosen[i] = add & 1; + return true; + } + else + { + // try to remove one edge and add its companion + for (int k = 0; k < cycle.size(); ++k) + { + int remove = cycle[k], + add_next = remove ^ 1; + if (visited[add_next] != i) + { + visited[add_next] = i; + last[add_next] = add; + q.push_back(add_next); + } + } + } + } + return false; +} + +int main() +{ + while (scanf("%i", &rounds) == 1 && rounds > 0) + { + int ret = 0; + n = 0; // #country + for (int r = 0; r < rounds; ++r) + { + for (int i = 0; i < 2; ++i) + { + for (int j = 0; j < 2; ++j) + { + int a; + scanf("%i", &a); + // add new country if needed + if (idx[a] >= 0 && idx[a] < n && labels[idx[a]] == a) + ; + else + { + edges[n].clear(); + visited[n] = visited[n + 1] = -1; + labels[idx[a] = n++] = a; + } + coins[2 * r + i][j] = idx[a]; + edges[coins[2 * r + i][j]].push_back(2 * r + i); + } + } + // ret < n - 1 cond not needed + if (ret < n - 1 && process_envelop(r)) + { + ++ret; + } + } + printf("%i\n", 2 * ret); + } + return 0; +} diff --git a/12389.cpp b/12389.cpp new file mode 100644 index 0000000..5111a4f --- /dev/null +++ b/12389.cpp @@ -0,0 +1,318 @@ +#include + +using namespace std; + +#define PB push_back +#define MP make_pair +#define FR(i, n) for (long long i = 0; i < n; i++) +#define FOR(i, a, n) for (long long i = a; i < n; i++) +#define FREACH(it, v) for (typeof(v.end()) it = v.begin(); it != v.end(); it++) +#define EPS 1e-9 + +typedef long long ll; +typedef long double ld; +typedef pair pii; + +const int NLOG = 18, N = 100000; +pair pts[N]; +int sorty[NLOG][N]; +int aux1[NLOG][N], aux2[NLOG][N], root1[N], root2[N], root3[N], root4[N], n, q; + +struct compy +{ + bool operator()(const int &a1, const int &a2) const + { + pair p1 = pts[a1], p2 = pts[a2]; + if (p1.second < p2.second) + { + return true; + } + if (p1.second > p2.second) + { + return false; + } + return (p1.first < p2.first); + } +}; + +void build_tree(int dpt, int lft, int rgt, bool leftBranch = false) +{ + assert(dpt < NLOG); + if (rgt == lft) + { + sorty[dpt][lft] = lft; + } + else + { + build_tree(dpt + 1, lft, (lft + rgt) / 2, true); + build_tree(dpt + 1, (lft + rgt) / 2 + 1, rgt, false); + FOR(j, lft, rgt + 1) + { + sorty[dpt][j] = sorty[dpt + 1][j]; + } + sort(&sorty[dpt][lft], &sorty[dpt][rgt + 1], compy()); + } + if (dpt > 0) + { + int val1, val2; + if (!leftBranch) + { + val1 = pts[sorty[dpt][lft]].first - pts[sorty[dpt][lft]].second; + aux1[dpt][lft] = sorty[dpt][lft]; + FOR(i, lft + 1, rgt + 1) + { + int idx = sorty[dpt][i]; + if (pts[idx].first - pts[idx].second < val1) + { + aux1[dpt][i] = idx; + } + else + { + aux1[dpt][i] = aux1[dpt][i - 1]; + } + val1 = min(val1, pts[idx].first - pts[idx].second); + } + val2 = pts[sorty[dpt][rgt]].first + pts[sorty[dpt][rgt]].second; + aux2[dpt][rgt] = sorty[dpt][rgt]; + for (int i = rgt - 1; i >= lft; i--) + { + int idx = sorty[dpt][i]; + if (pts[idx].first + pts[idx].second < val2) + { + aux2[dpt][i] = idx; + } + else + { + aux2[dpt][i] = aux2[dpt][i + 1]; + } + val2 = min(val2, pts[idx].first + pts[idx].second); + } + } + else + { + val1 = -pts[sorty[dpt][lft]].first - pts[sorty[dpt][lft]].second; + aux1[dpt][lft] = sorty[dpt][lft]; + FOR(i, lft + 1, rgt + 1) + { + int idx = sorty[dpt][i]; + if (-pts[idx].first - pts[idx].second < val1) + { + aux1[dpt][i] = idx; + } + else + { + aux1[dpt][i] = aux1[dpt][i - 1]; + } + val1 = min(val1, -pts[idx].first - pts[idx].second); + } + aux2[dpt][rgt] = sorty[dpt][rgt]; + val2 = -pts[sorty[dpt][rgt]].first + pts[sorty[dpt][rgt]].second; + for (int i = rgt - 1; i >= lft; i--) + { + int idx = sorty[dpt][i]; + if (-pts[idx].first + pts[idx].second < val2) + { + aux2[dpt][i] = idx; + } + else + { + aux2[dpt][i] = aux2[dpt][i + 1]; + } + val2 = min(val2, -pts[idx].first + pts[idx].second); + } + } + } + else + { + int val1, val2, val3, val4; + // val 1 and val2 are for the case when input is at the left of everything + val1 = pts[sorty[0][lft]].first - pts[sorty[0][lft]].second; + root1[lft] = sorty[0][lft]; + FOR(i, lft + 1, rgt + 1) + { + int idx = sorty[dpt][i]; + if (pts[idx].first - pts[idx].second < val1) + { + root1[i] = idx; + } + else + { + root1[i] = root1[i - 1]; + } + val1 = min(val1, pts[idx].first - pts[idx].second); + } + root2[rgt] = sorty[0][rgt]; + val2 = pts[sorty[0][rgt]].first + pts[sorty[0][rgt]].second; + for (int i = rgt - 1; i >= lft; i--) + { + int idx = sorty[dpt][i]; + if (pts[idx].first + pts[idx].second < val2) + { + root2[i] = idx; + } + else + { + root2[i] = root2[i + 1]; + } + val2 = min(val2, pts[idx].first + pts[idx].second); + } + // val 3 and val4 are for the case when input is at the right of everything + val3 = -pts[sorty[0][lft]].first - pts[sorty[0][lft]].second; + root3[lft] = sorty[0][lft]; + FOR(i, lft + 1, rgt + 1) + { + int idx = sorty[dpt][i]; + if (-pts[idx].first - pts[idx].second < val3) + { + root3[i] = idx; + } + else + { + root3[i] = root3[i - 1]; + } + val3 = min(val3, -pts[idx].first - pts[idx].second); + } + val4 = -pts[sorty[0][rgt]].first + pts[sorty[0][rgt]].second; + root4[rgt] = sorty[0][rgt]; + for (int i = rgt - 1; i >= lft; i--) + { + int idx = sorty[dpt][i]; + if (-pts[idx].first + pts[idx].second < val4) + { + root4[i] = idx; + } + else + { + root4[i] = root4[i + 1]; + } + val4 = min(val4, -pts[idx].first + pts[idx].second); + } + } +} + +int binSearch(int *arr, int lft, int rgt, int val) +{ + if (pts[arr[lft]].second >= val) + { + return -1; + } + if (pts[arr[rgt]].second < val) + { + return rgt; + } + while (rgt - lft > 1) + { + int md = (rgt + lft) / 2; + if (pts[arr[md]].second < val) + { + lft = md; + } + else + { + rgt = md; + } + } + return lft; +} + +// first is the distance, second is the point +pair min_dist(int dpt, int lft, int rgt, int xq, int yq, bool leftBranch = false) +{ + assert(dpt < NLOG); + if (rgt - lft <= 3) + { + int dist = 2000000001, idx; + FOR(i, lft, rgt + 1) + { + if (abs(xq - pts[i].first) + abs(yq - pts[i].second) < dist) + { + dist = abs(xq - pts[i].first) + abs(yq - pts[i].second); + idx = i; + } + } + return MP(dist, idx); + } + if (xq >= pts[rgt].first || xq <= pts[lft].first) + { + pair res; + int *ax1, *ax2; + if (dpt == 0) + { + if (xq >= pts[rgt].first) + { + ax1 = root3; + ax2 = root4; + } + else + { + ax1 = root1; + ax2 = root2; + } + } + else + { + ax1 = aux1[dpt]; + ax2 = aux2[dpt]; + } + // id is the last one < + int id = binSearch(sorty[dpt], lft, rgt, yq); + if (id == -1) + { + res.second = ax2[lft]; + res.first = abs(pts[res.second].first - xq) + abs(pts[res.second].second - yq); + } + else if (id == rgt) + { + res.second = ax1[rgt]; + res.first = abs(pts[res.second].first - xq) + abs(pts[res.second].second - yq); + } + else + { + int d1 = abs(pts[ax1[id]].first - xq) + abs(pts[ax1[id]].second - yq); + int d2 = abs(pts[ax2[id + 1]].first - xq) + abs(pts[ax2[id + 1]].second - yq); + if (d1 < d2) + res = MP(d1, ax1[id]); + else + res = MP(d2, ax2[id + 1]); + } + return res; + } + else + { + pii resl = min_dist(dpt + 1, lft, (lft + rgt) / 2, xq, yq, true); + pii resr = min_dist(dpt + 1, (lft + rgt) / 2 + 1, rgt, xq, yq, false); + if (resl.first < resr.first) + return resl; + else + return resr; + } +} + +int main() +{ + bool pr = false; + while (scanf("%d", &n)) + { + if (n == -1) + break; + if (pr) + printf("\n"); + pr = true; + int a, b; + FR(i, n) + { + scanf("%d %d", &a, &b); + pts[i] = MP(a, b); + } + sort(&pts[0], &pts[n]); + build_tree(0, 0, n - 1); + scanf("%d", &q); + FR(i, q) + { + scanf("%d %d", &a, &b); + pii res = min_dist(0, 0, n - 1, a, b); + printf("%d\n", res.first); + } + } + return 0; +} diff --git a/1239.cpp b/1239.cpp new file mode 100644 index 0000000..340a166 --- /dev/null +++ b/1239.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int main() +{ + int t, k, i, result, diff, length; + string input; + cin >> t; + cin.ignore(100, '\n'); + while (t--) + { + cin >> input >> k; + result = 0; + for (int len = 0, sz = input.size(); len < sz; len++) + { + length = 0; + diff = 0; + int d = 0; + // using 1 char as center of palindrome + for (i = len, d = 0; i - d >= 0 && i + d < sz && (input[i - d] == input[i + d] || diff < k); d++) + { + if (input[i - d] != input[i + d]) + { + diff++; + } + } + if (1 + (d - 1) * 2 > length) + { + length = 1 + (d - 1) * 2; + } + d = 0; + diff = 0; + // using 2 chars as center of palindrome + for (i = len; i - d >= 0 && i + 1 + d < sz && (input[i - d] == input[i + 1 + d] || diff < k); d++) + { + if (input[i - d] != input[i + 1 + d]) + { + diff++; + } + } + if (2 + (d - 1) * 2 > length) + { + length = 2 + (d - 1) * 2; + } + if (length > result) + { + result = length; + } + } + cout << result << endl; + } + return 0; +} diff --git a/12390.cpp b/12390.cpp new file mode 100644 index 0000000..4b486a8 --- /dev/null +++ b/12390.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int MAX_N = 500000; + +typedef long long int64; + +int N, B; +int as[MAX_N]; + +void init() +{ + for (int i = 0; i < N; i++) + { + scanf("%d", as + i); + } +} + +bool check(int x) +{ + int64 cnt = 0; + for (int i = 0; i < N; i++) + { + cnt += (as[i] + x - 1) / x; + } + return cnt <= B; +} + +int solve() +{ + int low = 0, high = 1e7; + while (high - low > 1) + { + int mid = (low + high) / 2; + if (check(mid)) + { + high = mid; + } + else + { + low = mid; + } + } + return high; +} + +int main() +{ + while (scanf("%d%d", &N, &B), N + 1) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/12391.cpp b/12391.cpp new file mode 100644 index 0000000..8de841c --- /dev/null +++ b/12391.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +// Probability of winning the game at deuce, if each game is won with probability p +double pr_2dif(double p) +{ + return p * p / (1 - 2 * p * (1 - p)); +} + +// Probability that the first player wins a game if +// a) he wins each point with probability p +// b) he needs to win 'num' >= 2 points with a difference of at least 2 +// c) the winning probability at score num-num is pr_tie +// This can also be used for sets. +double pr_win(double p, int num, double pr_tie) +{ + double f[num + 3][num + 3]; + fill(&f[0][0], &f[num + 3][0], -1); + // Winning states + for (int i = num; i <= num + 2; ++i) + for (int j = 0; j <= i - 2; ++j) + { + f[i][j] = 1; + f[j][i] = 0; + } + // Fill remaining states using dynamic programming + f[num][num] = pr_tie; + f[num][num + 1] = f[num - 1][num] = p * f[num][num]; + f[num + 1][num] = f[num][num - 1] = p + (1 - p) * f[num][num]; + for (int i = num - 1; i >= 0; --i) + for (int j = num - 1; j >= 0; --j) + { + f[i][j] = p * f[i + 1][j] + (1 - p) * f[i][j + 1]; + } + return f[0][0]; +} + +double pr_game(double p) +{ + return pr_win(p, 4, pr_2dif(p)); +} + +double pr_set(double p) +{ + double pr_tiebreak = pr_win(p, 7, pr_2dif(p)); + return pr_win(pr_game(p), 6, pr_tiebreak); +} + +double pr_match(double p) +{ + double q = pr_set(p); + return q * q + q * (1 - q) * q + (1 - q) * q * q; +} + +int main() +{ + double p; + while (scanf("%lf", &p), p >= 0) + { + printf("%.11lf %.11lf %.11lf\n", pr_game(p), pr_set(p), pr_match(p)); + } + return 0; +} diff --git a/12392.cpp b/12392.cpp new file mode 100644 index 0000000..13aebbb --- /dev/null +++ b/12392.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +const int MAX_N = 5; + +int N, M; +int vs[MAX_N]; +bool used_char[0x100]; +int val[0x100]; +int p, L; +char line[100001]; + +void init() +{ + for (int i = 0; i < N; i++) + { + scanf("%d", vs + i); + } + scanf("%d ", &M); + scanf("%[^\n]%*c", line); + memset(used_char, false, sizeof(used_char)); +} + +int expr() +{ + int ret = 0; + if (line[p] == '(') + { + p++; + ret = expr(); + p++; + } + else if (isalpha(line[p])) + { + ret = val[line[p++]]; + } + char op = line[p]; + if (op == '+' || op == '-' || op == '*') + { + p++; + int b = expr(); + if (op == '+') + { + return ret + b; + } + else if (op == '-') + { + return ret - b; + } + else + { + return ret * b; + } + } + return ret; +} + +bool sub() +{ + p = 0; + for (char c = 'a'; c <= 'z'; c++) + { + if (used_char[c]) + { + val[c] = vs[p++]; + } + } + p = 0; + int e = expr(); + return e == M; +} + +bool solve() +{ + L = strlen(line); + sort(vs, vs + N); + for (int i = 0; i < L; i++) + { + if (isalpha(line[i])) + { + used_char[line[i]] = true; + } + } + do + { + if (sub()) + { + return true; + } + } while (next_permutation(vs, vs + N)); + return false; +} + +int main() +{ + while (scanf("%d", &N), N) + { + init(); + puts(solve() ? "YES" : "NO"); + } + return 0; +} diff --git a/12393.cpp b/12393.cpp new file mode 100644 index 0000000..8bf3532 --- /dev/null +++ b/12393.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#define MAXN 1000000 + +static int a[MAXN]; + +int filter(int &n, int sum = 0) +{ + int l = 0; + for (int i = 0; i < n; ++i) + { + sum += a[i]; + if (sum >= 0) + { + a[l++] = sum; + sum = 0; + } + } + n = l; + return sum; +} + +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; ++i) + scanf("%d", &a[i]); + reverse(a, a + n); + filter(n, filter(n)); + printf("%d\n", n); + } + return 0; +} diff --git a/12394.cpp b/12394.cpp new file mode 100644 index 0000000..922fc61 --- /dev/null +++ b/12394.cpp @@ -0,0 +1,166 @@ +#include + +using namespace std; + +const int MAX_N = 1000; +const int MAX_K = 5; +const int MAX_L = 10; + +bool is_ok[MAX_N]; +char names[MAX_N][MAX_L + 1]; +int K, N, cnts[MAX_N], rs[MAX_N][MAX_K]; + +namespace union_find +{ + const int MAX_UF = MAX_N; + + int pars[MAX_UF]; + int ranks[MAX_UF]; + + void init_uf(int n) + { + for (int i = 0; i < n; i++) + { + pars[i] = i; + ranks[i] = 0; + } + } + + int get_root(int x) + { + if (pars[x] == x) + { + return x; + } + else + { + return pars[x] = get_root(pars[x]); + } + } + + void merge(int x, int y) + { + x = get_root(x); + y = get_root(y); + if (x == y) + { + return; + } + if (ranks[x] < ranks[y]) + { + pars[x] = y; + } + else if (ranks[y] < ranks[x]) + { + pars[y] = x; + } + else + { + pars[x] = y; + ranks[y]++; + } + } + + bool is_same_(int x, int y) + { + return get_root(x) == get_root(y); + } +} + +using union_find::init_uf; +using union_find::get_root; +using union_find::merge; +using union_find::is_same_; + +void init() +{ + memset(is_ok, true, sizeof(is_ok)); + memset(cnts, 0, sizeof(cnts)); + for (int i = 0; i < N; i++) + { + scanf(" %s ", names[i]); + for (int j = 0; j < K; j++) + { + scanf("%d ", rs[i] + j); + rs[i][j]--; + } + } +} + +void solve() +{ + init_uf(N); + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (!strcmp(names[i], names[j])) + { + merge(i, j); + } + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < K; j++) + { + for (int k = j + 1; k < K; k++) + { + if (rs[i][j] == rs[i][k]) + { + is_ok[rs[i][j]] = false; + } + } + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < K; j++) + { + if (is_same_(i, rs[i][j])) + { + is_ok[rs[i][j]] = false; + } + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < K; j++) + { + if (!is_same_(i, rs[i][j])) + { + cnts[rs[i][j]]++; + } + } + } + for (int i = 0; i < N; i++) + { + if (cnts[i] != K) + { + is_ok[i] = false; + } + } + int cnt = count(is_ok, is_ok + N, false); + if (cnt == 1) + { + printf("1 PROBLEM FOUND\n"); + } + else if (cnt > 0) + { + printf("%d PROBLEMS FOUND\n", cnt); + } + else + { + printf("NO PROBLEMS FOUND\n"); + } +} + +int main() +{ + while (scanf("%d%d ", &K, &N), N) + { + init(); + solve(); + } + return 0; +} diff --git a/12395.cpp b/12395.cpp new file mode 100644 index 0000000..6d0c0bb --- /dev/null +++ b/12395.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +const double eps = 1e-5; + +struct Point +{ + double x, y; + Point(double a = 0, double b = 0) : x(a), y(b) {} +}; + +double angle(Point a, Point b, Point c) +{ + b.x -= a.x; + b.y -= a.y; + c.x -= a.x; + c.y -= a.y; + return acos((b.x * c.x + b.y * c.y) / (hypot(b.x, b.y) * hypot(c.x, c.y))); +} + +bool fraction(double f, int n) +{ + return fabs(f * n - round(f * n)) < eps; +} + +int main() +{ + const double pi = acos(-1); + for (;;) + { + Point p[3]; + for (int i = 0; i < 3; ++i) + if (scanf("%lf%lf", &p[i].x, &p[i].y) != 2) + { + return 0; + } + double a = angle(p[0], p[1], p[2]) / pi, + b = angle(p[1], p[2], p[0]) / pi; + for (int n = 3; n <= 1000; ++n) + if (fraction(a, n) && fraction(b, n)) + { + printf("%i\n", n); + break; + } + } + return 0; +} diff --git a/12396.cpp b/12396.cpp new file mode 100644 index 0000000..c09631c --- /dev/null +++ b/12396.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +typedef long long int64; + +const int MAX_N = (int)1e7; +const int MAX_SIZE = MAX_N + 10; +const int MAX_PRIME = MAX_N + 10; +const int64 MOD = (int)(1e9) + 7; + +bool isPrime[MAX_SIZE]; +int primes[MAX_PRIME], P = 0, N = 0; + +void sieve() +{ + memset(isPrime, true, sizeof(isPrime)); + isPrime[0] = isPrime[1] = false; + for (int i = 2; i * i < MAX_SIZE; i++) + if (isPrime[i]) + { + for (int j = i << 1; j < MAX_SIZE; j += i) + { + isPrime[j] = false; + } + } + for (int i = 2; i < MAX_SIZE - 1; i++) + { + if (isPrime[i]) + { + primes[P++] = i; + } + } +} +int64 pow_bin(int64 x, int n) +{ + int64 ret = 1; + for (; n; n >>= 1) + { + if (n & 1) + { + ret = ret * x % MOD; + } + x = x * x % MOD; + } + return ret; +} +int solve() +{ + int64 ans = 1; + for (int i = 0; i < P && N >= primes[i] * 2; i++) + { + int cnt = 0; + for (int m = N; m;) + { + cnt += (m /= primes[i]); + } + ans = ans * pow_bin(primes[i], cnt & (~(1))) % MOD; + } + return (int)ans; +} + +int main() +{ + sieve(); + while (scanf("%d", &N), N) + { + printf("%d\n", solve()); + } + return 0; +} diff --git a/12397.cpp b/12397.cpp new file mode 100644 index 0000000..9f621e9 --- /dev/null +++ b/12397.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int c[16] = {4, 6, 3, 5, 2, 4, 2, 4, 2, 3, 2, 3, 1}; +int v[16] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; +int main() +{ + for (int cnt(0), n; scanf("%d", &n) == 1; cnt = 0) + { + for (int i = 0; n; ++i) + while (n >= v[i]) + { + cnt += c[i]; + n -= v[i]; + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/12398.cpp b/12398.cpp new file mode 100644 index 0000000..7f85feb --- /dev/null +++ b/12398.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int m[3][3]; +int dx[5] = {0, -1, 0, 1, 0}; +int dy[5] = {0, 0, -1, 0, 1}; + +void play(int x, int y) +{ + int nx, ny; + for (int dir = 0; dir < 5; ++dir) + { + nx = x + dx[dir]; + ny = y + dy[dir]; + if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) + { + ++m[nx][ny]; + } + } +} + +int main() +{ + char moves[256]; + for (int cnum = 1; fgets(moves, 256, stdin); ++cnum) + { + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + { + m[i][j] = 0; + } + for (int i = 0; moves[i]; ++i) + { + moves[i] -= 'a'; + play(moves[i] / 3, moves[i] % 3); + } + printf("Case #%d:\n", cnum); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + { + printf("%d%c", m[i][j] % 10, j == 2 ? 10 : 32); + } + } + return 0; +} diff --git a/12399.cpp b/12399.cpp new file mode 100644 index 0000000..99cccbf --- /dev/null +++ b/12399.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +const int INF = 1 << 29; + +int dy[] = {0, 0, 0, 1, -1}, dx[] = {0, 1, -1, 0, 0}; +int tmp[3][3], ans[3][3], best_ans[3][3]; +int board[3][3]; +char buf[2000]; + +bool init() +{ + gets(buf); + bool f = true; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + f &= (scanf("%d ", board[i] + j) == 1); + } + } + return f; +} + +void push(int y, int x) +{ + for (int k = 0; k < 5; k++) + { + int ny = y + dy[k], nx = x + dx[k]; + if (0 <= ny && ny < 3 && 0 <= nx && nx < 3) + { + tmp[ny][nx]--; + if (tmp[ny][nx] < 0) + { + tmp[ny][nx] += 10; + } + } + } +} + +void solve() +{ + int len = INF; + for (int top = 0; top < 1000; top++) + { + memcpy(tmp, board, sizeof(board)); + memset(ans, 0, sizeof(ans)); + ans[0][0] = top / 100; + ans[0][1] = (top / 10) % 10; + ans[0][2] = top % 10; + for (int x = 0; x < 3; x++) + { + for (int i = 0; i < ans[0][x]; i++) + { + push(0, x); + } + } + for (int y = 1; y < 3; y++) + { + for (int x = 0; x < 3; x++) + { + ans[y][x] = tmp[y - 1][x]; + for (int i = 0; i < ans[y][x]; i++) + { + push(y, x); + } + } + } + if (tmp[2][0] == 0 && tmp[2][1] == 0 && tmp[2][2] == 0) + { + int cnt = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + cnt += ans[i][j]; + } + } + if (cnt < len) + { + len = cnt; + memcpy(best_ans, ans, sizeof(ans)); + } + } + } + if (len == INF) + { + puts("No solution."); + } + else + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + for (int k = 0; k < best_ans[i][j]; k++) + { + putchar('a' + (i * 3 + j)); + } + } + } + puts(""); + } +} + +int main() +{ + while (init()) + { + solve(); + } + return 0; +} diff --git a/124.cpp b/124.cpp new file mode 100644 index 0000000..a4054a0 --- /dev/null +++ b/124.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int n; +char var[20]; +multimap m; +char current[20]; +bool taken[20]; +bool found; + +bool fit(char c, int depth) +{ + multimap::iterator it; + pair::iterator, multimap::iterator> ret; + ret = m.equal_range(c); + for (it = ret.first; it != ret.second; it++) + { + for (int i = 0; i < depth; i++) + { + if (current[i] == it->second) + { + return false; + } + } + } + return true; +} + +void solve(int depth) +{ + if (depth == n) + { + for (int i = 0; i < n; i++) + { + cout << current[i]; + } + cout << endl; + found = true; + return; + } + for (int i = 0; i < n; i++) + { + if (!fit(var[i], depth) || taken[i]) + { + continue; + } + taken[i] = true; + current[depth] = var[i]; + solve(depth + 1); + taken[i] = false; + } +} + +int main() +{ + char a, b; + bool first = true; + while (cin.peek() != -1) + { + n = 0; + fill(taken, taken + 20, false); + m.clear(); + if (!first) + { + cout << endl; + } + while (cin.peek() != '\n') + { + cin >> a; + var[n] = a; + n++; + } + sort(var, var + n); + cin.ignore(100, '\n'); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> a >> b; + m.insert(make_pair(a, b)); + } + cin.ignore(100, '\n'); + solve(0); + first = false; + } + return 0; +} diff --git a/12400.cpp b/12400.cpp new file mode 100644 index 0000000..0f595d5 --- /dev/null +++ b/12400.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +// 2 * 10^(n-1) = 5^(n-1) * 2^n +// 3 * 10^(n-1) = 3 * 5^(n-1) * 2^(n-1) + +#define MAXN 1024 + +char s[MAXN]; +int A[MAXN][MAXN] = {}, B[MAXN][MAXN] = {}, C[MAXN] = {}; + +int main() +{ + A[0][0] = 1, B[0][0] = 1, B[0][1] = 1; + // A[i] : bin for 5^i, B[i] : bin for 3 * 5^i + for (int i = 1; i < MAXN; i++) + { + for (int j = 0; j < MAXN; j++) + { + A[i][j] = A[i - 1][j] * 5; + B[i][j] = B[i - 1][j] * 5; + } + for (int j = 0; j < MAXN - 1; j++) + { + A[i][j + 1] += A[i][j] / 2; + A[i][j] &= 1; + B[i][j + 1] += B[i][j] / 2; + B[i][j] &= 1; + } + } + for (int i = 0; i < MAXN; i++) + { + if (C[i] % 2 == 0) + {// add 2 + for (int j = i + 1, k = 0; j < MAXN; j++, k++) + { + C[j] += A[i][k]; + } + s[i] = '2'; + } + else + {// add 3 + for (int j = i, k = 0; j < MAXN; j++, k++) + { + C[j] += B[i][k]; + } + s[i] = '3'; + } + C[i + 1] += C[i] / 2; + C[i] &= 1; + } + int n, m, k; + while (scanf("%d%d%d", &n, &m, &k) == 3) + { + int a = 0, b = 0; + for (int i = 0; i < k; i++) + { + a += s[i] == '3'; + b += s[i] == '2'; + } + if (n >= a && m >= b) + { + for (int i = k - 1; i >= 0; i--) + { + printf("%c", s[i]); + } + printf("\n"); + } + else + { + printf("Impossible.\n"); + } + } + return 0; +} diff --git a/12401.cpp b/12401.cpp new file mode 100644 index 0000000..60e1859 --- /dev/null +++ b/12401.cpp @@ -0,0 +1,217 @@ +#include + +using namespace std; + +const int MOD = 1000; + +void mul(int A[2][2], int B[2][2], int C[2][2]) +{ + volatile int a, b, c, d; + a = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % MOD; + b = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % MOD; + c = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % MOD; + d = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % MOD; + C[0][0] = a; + C[0][1] = b; + C[1][0] = c; + C[1][1] = d; +} + +void pow(int A[2][2], int e, int B[2][2]) +{ + int V[2][2] = {{1, 0}, {0, 1}}; + for (; e; e >>= 1, mul(A, A, A)) + if (e & 1) + { + mul(A, V, V); + } + for (int i = 0; i < 2; ++i) + for (int j = 0; j < 2; ++j) + { + B[i][j] = V[i][j]; + } +} + +int main() +{ + int n; + char s[2]; + long long X[50], Y[50], Z[50]; + X[0] = Y[0] = Z[0] = 2; + X[1] = 1; + Y[1] = 2; + Z[1] = 3; + double gold = (1 + sqrt(5)) / 2, silver = 1 + sqrt(2), bronze = (3 + sqrt(13)) / 2; + for (int i = 1; i < 50; ++i) + { + X[i] = (int)(pow(gold, i) + 0.5); + Y[i] = (int)(pow(silver, i) + 0.5); + Z[i] = (int)(pow(bronze, i) + 0.5); + } + while (~scanf("%d%s", &n, s)) + { + if (s[0] == 'G') + { + if (n == 0) + { + puts("The 0th Golden Power is 1."); + } + else + { + printf("The %d", n); + if (n % 10 == 1 && n % 100 != 11) + { + printf("st"); + } + else if (n % 10 == 2 && n % 100 != 12) + { + printf("nd"); + } + else if (n % 10 == 3 && n % 100 != 13) + { + printf("rd"); + } + else + { + printf("th"); + } + printf(" Golden Power is "); + if (n <= 43) + { + printf("%lld.\n", X[n]); + } + else + { + int B[2][2] = {{1, 1}, {1, 0}}; + pow(B, n - 1, B); + int low = (B[0][0] + 2 * B[0][1]) % MOD; + int dig = (int)(ceil(n * log10(gold) - 1e-9) + 1e-9); + double logX = n * log10(gold); + int hlo = 100, hhi = 1000; + while (hhi - hlo > 1) + { + int mid = (hhi + hlo) / 2; + if ((dig - 1) + (log10(mid) - 2) <= logX) + { + hlo = mid; + } + else + { + hhi = mid; + } + } + printf("%d...%03d(%d digits).\n", hlo, low, dig); + } + } + } + else if (s[0] == 'S') + { + if (n == 0) + { + puts("The 0th Silver Power is 1."); + } + else + { + printf("The %d", n); + if (n % 10 == 1 && n % 100 != 11) + { + printf("st"); + } + else if (n % 10 == 2 && n % 100 != 12) + { + printf("nd"); + } + else if (n % 10 == 3 && n % 100 != 13) + { + printf("rd"); + } + else + { + printf("th"); + } + printf(" Silver Power is "); + if (n <= 23) + { + printf("%lld.\n", Y[n]); + } + else + { + int C[2][2] = {{2, 1}, {1, 0}}; + pow(C, n - 1, C); + int low = (2 * C[0][0] + 2 * C[0][1]) % MOD; + int dig = (int)(ceil(n * log10(silver) - 1e-9) + 1e-9); + double logX = n * log10(silver); + int hlo = 100, hhi = 1000; + while (hhi - hlo > 1) + { + int mid = (hhi + hlo) / 2; + if ((dig - 1) + (log10(mid) - 2) <= logX) + { + hlo = mid; + } + else + { + hhi = mid; + } + } + printf("%d...%03d(%d digits).\n", hlo, low, dig); + } + } + } + else + { + if (n == 0) + { + puts("The 0th Bronze Power is 1."); + } + else + { + printf("The %d", n); + if (n % 10 == 1 && n % 100 != 11) + { + printf("st"); + } + else if (n % 10 == 2 && n % 100 != 12) + { + printf("nd"); + } + else if (n % 10 == 3 && n % 100 != 13) + { + printf("rd"); + } + else + { + printf("th"); + } + printf(" Bronze Power is "); + if (n <= 17) + { + printf("%lld.\n", Z[n]); + } + else + { + int D[2][2] = {{3, 1}, {1, 0}}; + pow(D, n - 1, D); + int low = (3 * D[0][0] + 2 * D[0][1]) % MOD; + int dig = (int)(ceil(n * log10(bronze) - 1e-9) + 1e-9); + double logX = n * log10(bronze); + int hlo = 100, hhi = 1000; + while (hhi - hlo > 1) + { + int mid = (hhi + hlo) / 2; + if ((dig - 1) + (log10(mid) - 2) <= logX) + { + hlo = mid; + } + else + { + hhi = mid; + } + } + printf("%d...%03d(%d digits).\n", hlo, low, dig); + } + } + } + } + return 0; +} diff --git a/12402.cpp b/12402.cpp new file mode 100644 index 0000000..1908b06 --- /dev/null +++ b/12402.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +int maze[10][8]; +int cap[10]; +int n; + +int dep; +int ans[22]; + +int ht[10000020]; +int val[10000020]; + +inline int p(int pos, int x) +{ + return (pos & (7 << (x * 3))) >> (x * 3); +} + +bool dfs(int d, int pos) +{ + int h = 0, th = 0; + bool col[15] = {false}; + for (int i = 0; i < n; i++) + { + int tp = p(pos, i); + if (cap[i] - tp > th) + { + th = cap[i] - tp; + } + for (int j = tp; j < cap[i]; j++) + { + if (col[maze[i][j]]) + { + continue; + } + col[maze[i][j]] = true; + h++; + } + } + if (th > h) + { + h = th; + } + if (h + d > dep) + { + return false; + } + if (h == 0) + { + return true; + } + for (int i = 0; i < n; i++) + { + int tp = p(pos, i); + if (cap[i] == tp) + { + continue; + } + int c = maze[i][tp]; + bool flag = true; + for (int j = 0; j < i; j++) + { + int ttp = p(pos, j); + if (ttp >= cap[j]) + { + continue; + } + if (maze[j][ttp] == c) + { + flag = false; + break; + } + } + if (!flag) + { + continue; + } + int tpos = pos; + for (int j = i; j < n; j++) + { + int ttp = p(pos, j); + if (ttp >= cap[j]) + { + continue; + } + if (maze[j][ttp] == c) + { + tpos += (1 << (j * 3)); + } + } + int th = tpos % 10000007; + if (ht[th] == dep && val[th] <= d) + { + continue; + } + ht[th] = dep; + val[th] = d; + ans[d] = c; + if (dfs(d + 1, tpos)) + { + return true; + } + } + return false; +} + +int main() +{ + while (~scanf("%d", &n)) + { + memset(ht, -1, sizeof(ht)); + memset(val, -1, sizeof(val)); + memset(maze, -1, sizeof(maze)); + for (int i = 0; i < n; i++) + { + scanf("%d", &cap[i]); + for (int j = 0; j < cap[i]; j++) + { + scanf("%d", &maze[i][j]); + } + } + int k; + scanf("%d", &k); + int pos = 0; + for (int i = 0; i < n; i++) + { + pos += ((maze[i][0] == k) << (i * 3)); + } + dep = 0; + while (true) + { + if (dfs(0, pos)) + { + break; + } + dep++; + } + printf("%d", k); + for (int i = 0; i < dep; i++) + { + printf(" %d", ans[i]); + } + puts(""); + } + return 0; +} diff --git a/12403.cpp b/12403.cpp index 6f6ad7c..643dc9d 100644 --- a/12403.cpp +++ b/12403.cpp @@ -1,18 +1,24 @@ -#include -#include +#include -int main() { - int T, D = 0, I; - char str[20]; - scanf("%d", &T); - while (T--) { - scanf("%s", str); - if (!strcmp(str, "report")) { - printf("%d\n", D); +using namespace std; + +int main() +{ + int t, sum = 0, tmp; + string input; + cin >> t; + cin.ignore(100, '\n'); + while (t--) + { + cin >> input; + if (input == "donate") + { + cin >> tmp; + sum += tmp; } - else { - scanf("%d", &I); - D += I; + else if (input == "report") + { + cout << sum << endl; } } return 0; diff --git a/12404.cpp b/12404.cpp new file mode 100644 index 0000000..1426d38 --- /dev/null +++ b/12404.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +double const Pi = acos(0.0) * 2.0; +double const Pi2 = acos(0.0) * 4.0; + +double circle_angle(double a) +{ + return a >= 0 ? a : Pi2 + a; +} +double heron(double a, double b, double c) +{ + double s = (a + b + c) / 2.0; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} + +struct Vector +{ + double x, y; + Vector(double X, double Y) : x(X), y(Y) {} + double norm() + { + return sqrt(x * x + y * y); + } + double angle(const Vector &p) const + { + return circle_angle(atan2(p.y, p.x) - atan2(y, x)); + } +}; + +int Ax, Ay, Bx, By, b, c, d; +double Cx, Cy, Dx, Dy; +Vector ux(1.0, 0.0); + +void solve() +{ + Vector va(Bx - Ax, By - Ay); + double bas = va.norm() - c; + double corner = acos((b * b + bas * bas - d * d) / (2 * b * bas)); + double alpha = ux.angle(va) + Pi - corner; + Vector vb(b * cos(alpha), b * sin(alpha)); + Cx = Bx + vb.x; + Cy = By + vb.y; + alpha = ux.angle(va) + Pi; + Vector vc(c * cos(alpha), c * sin(alpha)); + Dx = Cx + vc.x; + Dy = Cy + vc.y; +} + +int main() +{ + int T; + scanf("%d", &T); + int ncase = 0; + while (T--) + { + scanf("%d%d%d%d%d%d%d", &Ax, &Ay, &Bx, &By, &b, &c, &d); + solve(); + printf("Case %d:\n%.8lf %.8lf %.8lf %.8lf\n", ++ncase, Cx, Cy, Dx, Dy); + } + return 0; +} diff --git a/12405.cpp b/12405.cpp index 7d9d9ea..53630f0 100644 --- a/12405.cpp +++ b/12405.cpp @@ -1,30 +1,29 @@ -#include -#include +#include -int main() { - int T, N; - char crop[105]; - scanf("%d", &T); - for (int t = 1; t <= T; t++) { - scanf("%d", &N); - scanf("%s", crop); - bool seen = false; - int previous, scareCrow = 0; - for (int i = 0; i < N; i++) { - if (seen) { - if (i - previous == 2) { - ++scareCrow; - seen = false; - } - continue; +using namespace std; + +int main() +{ + scanf("%*d"); + for (int cnum = 0, n; scanf("%d\n", &n) == 1;) + { + int scarecrows = 0; + char c, p = '#'; + while (n--) + { + c = getc(stdin); + scarecrows += p == '.'; + if (p == '.') + { + c = 'S'; } - if (crop[i] == '.') { - seen = true; - previous = i; + else if (p == 'S') + { + c = '#'; } + p = c; } - if (seen) ++scareCrow; - printf("Case %d: %d\n", t, scareCrow); + printf("Case %d: %d\n", ++cnum, scarecrows + (p == '.')); } return 0; } diff --git a/12406.cpp b/12406.cpp index 1e03d21..92b8c51 100644 --- a/12406.cpp +++ b/12406.cpp @@ -1,45 +1,72 @@ -#include -#include -#include +#include + using namespace std; -typedef long long int64; -const int64 MAXM = 1 * 1LL<<60; -const int64 MINM = -1 * 1LL<<60; +vector precalc[18]; -int main() { - //cout << MAXM << " " << MINM << endl; - - int64 T, t; - scanf("%lld", &T); - for (t = 1; t <= T; t++) { - int64 p, q; - scanf("%lld %lld", &p, &q); - - q = 1LL< result; + for (int i = 1; i < 18; i++) + { + precalculate(i, 0, 0); + sort(precalc[i].begin(), precalc[i].end()); + } + cin >> t; + for (int i = 0; i < t; i++) + { + cin >> p >> q; + result.clear(); + int div = mypow(q); + long long min = -1, max = -1; + for (int j = 0, sz = precalc[p].size(); j < sz; j++) + { + if (precalc[p][j] % div == 0) + { + if (min == -1) + { + min = precalc[p][j]; + } + else + { + max = precalc[p][j]; + } } } - if (Min == MAXM && Max == MINM) { - printf("Case %lld: impossible\n", t); + cout << "Case " << i + 1 << ": "; + if (min == -1) + { + cout << "impossible" << endl; } - else if (Min == Max) { - printf("Case %lld: %lld\n", t, Min); + else if (max == -1) + { + cout << min << endl; } - else { - printf("Case %lld: %lld %lld\n", t, Min, Max); + else + { + cout << min << " " << max << endl; } } return 0; diff --git a/12407.cpp b/12407.cpp new file mode 100644 index 0000000..f71de9d --- /dev/null +++ b/12407.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) + +double s[128], x[128]; +int main() +{ + int t; + scanf("%d", &t); + for (int c = 0; c++ < t;) + { + int n, d; + double l, h = 1e10, m, sum; + scanf("%d %d", &n, &d); + FOR(i, n) + { + scanf("%lf", s + i); + h = min(h, 1.0 / s[i]); + } + l = -h; + FOR(i, 50) + { + sum = 0; + m = (l + h) * .5; + FOR(j, n) + sum += x[j] = (100 * m * s[j]) / sqrt(1 - m * m * s[j] * s[j]); + if (sum < d) + { + l = m; + } + else + { + h = m; + } + } + double ans = 0; + FOR(i, n) + ans += sqrt(10000.0 + x[i] * x[i]) / s[i]; + printf("Case %d: %.8lf\n", c, ans); + } + return 0; +} diff --git a/12408.cpp b/12408.cpp new file mode 100644 index 0000000..9fe03bd --- /dev/null +++ b/12408.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +typedef long long ll; +int main() +{ + ll r, t, n, ans; + while (scanf("%lld%lld%lld", &r, &t, &n), r) + { + if (t < r || t > n * r) + { + puts("0"); + } + else + { + ll x1 = (t - r) / (n - 1); + ll x2 = (n * r - t) / (n - 1); + printf("%lld\n", ((n - 1) * x1 + 2) * (x1 + 1) / 2 + ((n - 1) * (r - x2 - 1) + 2) * (r - x2) / 2 + (x2 - x1) * (t - r + 1) - 2); + } + } + return 0; +} diff --git a/12409.cpp b/12409.cpp new file mode 100644 index 0000000..1e8e7e8 --- /dev/null +++ b/12409.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define MAXI(a, b) (a) > (b) ? (a) : (b) +#define MINI(a, b) (a) < (b) ? (a) : (b) + +int main() +{ + int i, j, r, c, ans, tst, cas = 1, state, pres, buff; + scanf("%d", &tst); + while (tst--) + { + scanf("%d%d", &r, &c); + if ((r & 1) == (c & 1)) + { + state = 0; + } + else + { + state = 1; + } + ans = 0; + for (i = 0; i < r; i++) + { + pres = state + i; + for (j = 0; j < c; j++, pres++) + { + scanf("%d", &buff); + if (pres & 1) + { + ans ^= buff; + } + } + } + printf("Case %d: ", cas++); + if (ans) + { + puts("win"); + } + else + { + puts("lose"); + } + } + return 0; +} diff --git a/1241.cpp b/1241.cpp new file mode 100644 index 0000000..bc0d635 --- /dev/null +++ b/1241.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int N, M; +int maxs[11]; +int data[5000]; +int cnt; + +void init() +{ + cnt = 0; + for (int i = 0; i < maxs[N]; i++) + { + data[i] = true; + } +} + +bool func(int now) +{ + if (now * 2 >= maxs[N]) + { + return data[now]; + } + bool f1, f2; + f1 = func(now * 2); + f2 = func(now * 2 + 1); + if (f1 && f2) + { + return true; + } + else if (f1 || f2) + { + cnt++; + return true; + } + else + { + return false; + } +} + +void solve() +{ + cin >> N >> M; + init(); + for (int i = 0; i < M; i++) + { + int in; + cin >> in; + data[maxs[N] - in] = false; + } + func(1); + cout << cnt << endl; +} + +int main() +{ + int t; + maxs[0] = 2; + for (int i = 1; i <= 11; i++) + { + maxs[i] = maxs[i - 1] * 2; + } + cin >> t; + for (int i = 0; i < t; i++) + { + solve(); + } + return 0; +} diff --git a/12410.cpp b/12410.cpp new file mode 100644 index 0000000..4cceb37 --- /dev/null +++ b/12410.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +/** memo + [ what was done to make the first digit ] + [ with the number of 1 ] + [ difference between the ideal ] + [ mod 3 ] + [ mod 7 ] + [ smaller or not ] +*/ + +#define REP(i, n) for (int i = 0; i < (int)n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() +#define each(i, c) FOR(i, c) +#define VAR(a) cout << #a << " : " << a << endl; +#define LOG() cout << __LINE__ << ", " << __func__ << endl; + +typedef long long int lli; + +const int D = 32; +pair memo[D][D][D][3][7][2]; + +vector ideal, v; +int maxone, K; + +pair rec(int digit, int one, int diff, int mod3, int mod7, bool small) +{ + pair &ret = memo[digit][one][diff][mod3][mod7][small]; + if (ret.first != -1) + { + return ret; + } + if (maxone < one) + { + return ret = make_pair(0, 0); + } + if (K < diff) + { + return ret = make_pair(0, 0); + } + if (digit == v.size()) + { + return (one && mod3 == 0 && mod7 != 0) ? make_pair(0, 1) : make_pair(0, 0); + } + lli sum = 0; + lli cnt = 0; + pair p;// sum, cnt + if (small) + { + // add 1 + p = rec(digit + 1, + one + 1, + diff + (ideal[digit] == 0), + (mod3 * 2 + 1) % 3, + (mod7 * 2 + 1) % 7, + true); + cnt += p.second; + sum += p.second * (1 << (v.size() - digit - 1)); + sum += p.first; + // add 0 + p = rec(digit + 1, + one, + diff + (ideal[digit] == 1), + (mod3 * 2) % 3, + (mod7 * 2) % 7, + true); + cnt += p.second; + sum += p.first; + } + else + { + // add 1 + if (v[digit] == 1) + { + p = rec(digit + 1, + one + 1, + diff + (ideal[digit] == 0), + (mod3 * 2 + 1) % 3, + (mod7 * 2 + 1) % 7, + false); + cnt += p.second; + sum += p.second * (1 << (v.size() - digit - 1)); + sum += p.first; + } + // add 0 + p = rec(digit + 1, + one, + diff + (ideal[digit] == 1), + (mod3 * 2) % 3, + (mod7 * 2) % 7, + v[digit] == 1); + cnt += p.second; + sum += p.first; + } + return ret = make_pair(sum, cnt); +} + +void f(vector &v, int n) +{ + v.clear(); + while (n) + { + v.push_back(n % 2); + n /= 2; + } + while (v.size() < D - 1) + { + v.push_back(0); + } + reverse(v.begin(), v.end()); + return; +} + +lli g(lli n) +{ + fill(&memo[0][0][0][0][0][0], + &memo[D - 1][D - 1][D - 1][3 - 1][7 - 1][2], + make_pair(-1, -1)); + f(v, n); + return rec(0, 0, 0, 0, 0, false).first; +} + +int main(int argc, char *argv[]) +{ + int tc; + cin >> tc; + while (tc--) + { + int start, end, i; + cin >> start >> end >> maxone >> i >> K; + f(ideal, i); + static int tc = 0; + cout << "Case " << ++tc << ": " << g(end) - g(start - 1) << endl; + } + return 0; +} diff --git a/12411.cpp b/12411.cpp new file mode 100644 index 0000000..e047514 --- /dev/null +++ b/12411.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define EPS (1e-9) + +int n, K; +int A, B, p; + +int main() +{ + int T; + scanf("%d", &T); + int ncase = 0; + while (T--) + { + scanf("%d%d", &n, &K); + A = B = p = 0; + for (int i = 0; i < n; ++i) + { + int x; + scanf("%d", &x); + if (x > 0) + { + A += x, ++p; + } + else + { + B += -x; + } + } + printf("Case %d: ", ++ncase); + if (p == 0) + { + puts("-1.000"); + continue; + } + double ans = 1.0 * A / p + 1.0 * B / p; + double f = 1.0 / (n * p); + for (int i = 1; i <= K; ++i) + { + ans -= f * B; + f *= 1.0 * (n - p - i) / (n - i); + } + printf("%.3lf\n", ans + EPS); + } + return 0; +} diff --git a/12412.cpp b/12412.cpp new file mode 100644 index 0000000..867946b --- /dev/null +++ b/12412.cpp @@ -0,0 +1,255 @@ +#include + +using namespace std; + +#define pi acos(-1) +#define INF 0x7fffffff +#define clr(x) memset(x, 0, sizeof(x)); +#define clrto(x, siz, y) \ + for (int xx = 0; xx <= siz; xx++) \ + x[xx] = y; +#define clrset(x, siz) \ + for (int xx = 0; xx <= siz; xx++) \ + x[xx] = xx; +#define clrvec(x, siz) \ + for (int xx = 0; x <= siz; xx++) \ + x[xx].clear(); +#define myprogram By_135678942570 +#define clrcpy(x, siz, y) \ + for (int xx = 0; xx < siz; xx++) \ + x[xx] = y[xx]; + +map mp1; +double ave[5]; +string sub[5] = {"Chinese", "Mathematics", "English", "Programming"}; +int stucnt; + +struct Class +{ + int pass[5], passsub[5], sum[5], cnt; +} CLASS[22]; +struct node +{ + string num; + string name; + int cid, C, M, E, P, sum, flag, cntpass; +} STU[201111]; +void printmenu() +{ + printf("Welcome to Student Performance Management System (SPMS).\n\n1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n"); +} + +int main() +{ + while (1) + { + printmenu(); + int n; + scanf("%d", &n); + if (n == 0) + break; + else if (n == 4) + puts("Showing the ranklist hurts students' self-esteem. Don't do that."); + else if (n == 1) + while (1) + { + puts("Please enter the SID, CID, name and four scores. Enter 0 to finish."); + int b; + string a, c; + int e, f, g, h; + cin >> a; + if (a[0] == '0' && a.size() == 1) + break; + scanf("%d", &b); + cin >> c; + scanf("%d%d%d%d", &e, &f, &g, &h); + node temp; + temp.cntpass = (e >= 60) + (f >= 60) + (g >= 60) + (h >= 60); + if (mp1[a] != 0) + { + puts("Duplicated SID."); + continue; + } + mp1[a] = stucnt + 1; + temp.num = a; + temp.cid = b; + temp.name = c; + temp.C = e; + temp.M = f; + temp.E = g; + temp.P = h; + temp.sum = e + f + g + h; + temp.flag = 1; + STU[++stucnt] = temp; + CLASS[b].cnt++; + CLASS[b].sum[0] += e; + CLASS[b].sum[1] += f; + CLASS[b].sum[2] += g; + CLASS[b].sum[3] += h; + CLASS[b].passsub[0] += e >= 60; + CLASS[b].passsub[1] += f >= 60; + CLASS[b].passsub[2] += g >= 60; + CLASS[b].passsub[3] += h >= 60; + CLASS[b].pass[temp.cntpass]++; + b = 0; + CLASS[b].cnt++; + CLASS[b].sum[0] += e; + CLASS[b].sum[1] += f; + CLASS[b].sum[2] += g; + CLASS[b].sum[3] += h; + CLASS[b].passsub[0] += e >= 60; + CLASS[b].passsub[1] += f >= 60; + CLASS[b].passsub[2] += g >= 60; + CLASS[b].passsub[3] += h >= 60; + CLASS[b].pass[temp.cntpass]++; + } + else if (n == 2) + while (1) + { + puts("Please enter SID or name. Enter 0 to finish."); + string c; + cin >> c; + if (c[0] == '0' && c.size() == 1) + break; + int cnt = 0; + if (isalpha(c[0])) + { + for (int i = 1; i <= stucnt; i++) + { + if (STU[i].flag && STU[i].name == c) + { + cnt++; + STU[i].flag = 0; + int x = STU[i].cid; + mp1[STU[i].num] = 0; + CLASS[x].cnt--; + CLASS[x].pass[STU[i].cntpass]--; + CLASS[x].sum[0] -= STU[i].C; + CLASS[x].sum[1] -= STU[i].M; + CLASS[x].sum[2] -= STU[i].E; + CLASS[x].sum[3] -= STU[i].P; + CLASS[x].passsub[0] -= STU[i].C >= 60; + CLASS[x].passsub[1] -= STU[i].M >= 60; + CLASS[x].passsub[2] -= STU[i].E >= 60; + CLASS[x].passsub[3] -= STU[i].P >= 60; + x = 0; + CLASS[x].cnt--; + CLASS[x].pass[STU[i].cntpass]--; + CLASS[x].sum[0] -= STU[i].C; + CLASS[x].sum[1] -= STU[i].M; + CLASS[x].sum[2] -= STU[i].E; + CLASS[x].sum[3] -= STU[i].P; + CLASS[x].passsub[0] -= STU[i].C >= 60; + CLASS[x].passsub[1] -= STU[i].M >= 60; + CLASS[x].passsub[2] -= STU[i].E >= 60; + CLASS[x].passsub[3] -= STU[i].P >= 60; + } + } + } + else + { + int i = mp1[c]; + if (i && STU[i].flag) + { + cnt++; + STU[mp1[c]].flag = 0; + mp1[c] = 0; + int x = STU[i].cid; + CLASS[x].cnt--; + CLASS[x].pass[STU[i].cntpass]--; + CLASS[x].sum[0] -= STU[i].C; + CLASS[x].sum[1] -= STU[i].M; + CLASS[x].sum[2] -= STU[i].E; + CLASS[x].sum[3] -= STU[i].P; + CLASS[x].passsub[0] -= STU[i].C >= 60; + CLASS[x].passsub[1] -= STU[i].M >= 60; + CLASS[x].passsub[2] -= STU[i].E >= 60; + CLASS[x].passsub[3] -= STU[i].P >= 60; + x = 0; + CLASS[x].cnt--; + CLASS[x].pass[STU[i].cntpass]--; + CLASS[x].sum[0] -= STU[i].C; + CLASS[x].sum[1] -= STU[i].M; + CLASS[x].sum[2] -= STU[i].E; + CLASS[x].sum[3] -= STU[i].P; + CLASS[x].passsub[0] -= STU[i].C >= 60; + CLASS[x].passsub[1] -= STU[i].M >= 60; + CLASS[x].passsub[2] -= STU[i].E >= 60; + CLASS[x].passsub[3] -= STU[i].P >= 60; + } + } + printf("%d student(s) removed.\n", cnt); + } + else if (n == 3) + while (1) + { + puts("Please enter SID or name. Enter 0 to finish."); + string c; + cin >> c; + if (c[0] == '0' && c.size() == 1) + break; + if (isalpha(c[0])) + { + for (int i = 1; i <= stucnt; i++) + { + if (STU[i].flag && STU[i].name == c) + { + int rank = 1; + for (int j = 1; j <= stucnt; j++) + if (STU[j].flag && STU[j].sum > STU[i].sum) + { + rank++; + } + printf("%d ", rank); + cout << STU[i].num << " "; + printf("%d ", STU[i].cid); + cout << STU[i].name << " "; + printf("%d %d %d %d %d %.2f\n", STU[i].C, STU[i].M, STU[i].E, STU[i].P, STU[i].sum, STU[i].sum / 4.0 + 1e-5); + } + } + } + else + { + int i = mp1[c]; + if (i && STU[i].flag) + { + int rank = 1; + for (int j = 1; j <= stucnt; j++) + if (STU[j].flag && STU[j].sum > STU[i].sum) + rank++; + printf("%d ", rank); + cout << STU[i].num << " "; + printf("%d ", STU[i].cid); + cout << STU[i].name << " "; + printf("%d %d %d %d %d %.2f\n", STU[i].C, STU[i].M, STU[i].E, STU[i].P, STU[i].sum, STU[i].sum / 4.0 + 1e-5); + } + } + } + else if (n == 5) + { + puts("Please enter class ID, 0 for the whole statistics."); + int q; + scanf("%d", &q); + for (int i = 0; i < 4; i++) + { + cout << sub[i] << endl; + printf("Average Score: %.2f\n", (double)CLASS[q].sum[i] / CLASS[q].cnt + 1e-5); + printf("Number of passed students: %d\n", CLASS[q].passsub[i]); + printf("Number of failed students: %d\n", CLASS[q].cnt - CLASS[q].passsub[i]); + puts(""); + } + puts("Overall:"); + int cnt = 0; + printf("Number of students who passed all subjects: %d\n", CLASS[q].pass[4]); + cnt += CLASS[q].pass[4]; + for (int i = 3; i >= 1; i--) + { + cnt += CLASS[q].pass[i]; + printf("Number of students who passed %d or more subjects: %d\n", i, cnt); + } + printf("Number of students who failed all subjects: %d\n", CLASS[q].pass[0]); + puts(""); + } + } + return 0; +} diff --git a/12414.cpp b/12414.cpp new file mode 100644 index 0000000..f023114 --- /dev/null +++ b/12414.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int yuanfen(const char *s, int n) +{ + int cnt = 0, digits[64]; + for (int i = 0; s[i]; ++i) + { + int v = s[i] - 'A' + n; + int stack[8], top = 0; + while (v) + { + stack[top++] = v % 10; + v /= 10; + } + while (top--) + { + digits[cnt++] = stack[top]; + } + } + while (cnt > 2) + { + cnt--; + for (int i = 0; i < cnt; ++i) + { + digits[i] = (digits[i] + digits[i + 1]) % 10; + } + if (cnt == 3 && digits[0] == 1 && digits[1] == 0 && digits[2] == 0) + { + return 100; + } + } + return 10 * digits[0] + digits[1]; +} + +int main() +{ + for (char s[16]; scanf("%s", s) == 1;) + { + int i; + for (i = 1; i <= 10000; ++i) + { + if (yuanfen(s, i) == 100) + { + printf("%d\n", i); + break; + } + } + if (i > 10000) + { + puts(":("); + } + } + return 0; +} diff --git a/12415.cpp b/12415.cpp new file mode 100644 index 0000000..c008e95 --- /dev/null +++ b/12415.cpp @@ -0,0 +1,329 @@ +#include + +using namespace std; + +const int MAXSTATE = 32767; +const int epsilon = 0; + +int toIndex(char c) +{ + return c - '0' + 1; +} +struct State +{ + vector trans[11]; + int ac, label; + State() + { + ac = label = 0; + for (int i = 0; i < 11; i++) + trans[i].clear(); + } +} _mem[MAXSTATE]; +int nodesize = 0; +State *newState() +{ + assert(nodesize < MAXSTATE); + State *p = &_mem[nodesize++]; + *p = State(); + return p; +} + +struct NFA +{ + State *st, *ac; + NFA() + { + st = ac = NULL; + } + NFA(char c) + { + st = newState(), ac = newState(); + ac->ac = 1; + st->trans[toIndex(c)].push_back(ac); + } + NFA(NFA L, char c) + {// (A)* + st = ac = NULL; + if (c != '*') + return; + st = newState(), ac = newState(); + ac->ac = 1; + st->trans[epsilon].push_back(L.st); + L.st->trans[epsilon].push_back(L.ac); + L.ac->trans[epsilon].push_back(L.st); + L.ac->trans[epsilon].push_back(ac); + L.ac->ac = 0; + } + NFA(NFA L, NFA R, char c) + { + if (R.st == NULL) + { + st = L.st, ac = L.ac; + return; + } + if (c == '|') + { + st = newState(), ac = newState(); + ac->ac = 1; + st->trans[epsilon].push_back(L.st); + st->trans[epsilon].push_back(R.st); + L.ac->trans[epsilon].push_back(ac); + R.ac->trans[epsilon].push_back(ac); + L.ac->ac = R.ac->ac = 0; + } + else if (c == '&') + { + st = newState(), ac = newState(); + ac->ac = 1; + st->trans[epsilon].push_back(L.st); + L.ac->trans[epsilon].push_back(R.st); + R.ac->trans[epsilon].push_back(ac); + L.ac->ac = R.ac->ac = 0; + } + } +}; + +NFA parser(string exp) +{ + if (exp.length() == 0) + return NFA(); + if (exp.length() == 1) + return NFA(exp[0]); + + int l = 0, pos = -1; + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + l++; + } + else if (exp[i] == ')') + { + l--; + } + else if (exp[i] == '+') + { + if (l == 0) + { + pos = i; + break; + } + } + } + + if (pos != -1) + { + NFA L = parser(exp.substr(0, pos)); + NFA R = parser(exp.substr(pos + 1)); + return NFA(L, R, '|'); + } + + int hasStar = 0; + string ls, rs; + + if (exp[0] == '(') + { + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + l++; + } + else if (exp[i] == ')') + { + l--; + if (l == 0) + {// (...)... + if (i + 1 < exp.length() && exp[i + 1] == '*') + {// (...)*... + hasStar = 1; + ls = exp.substr(1, i - 1), rs = exp.substr(i + 2); + } + else + {// (...)... + ls = exp.substr(1, i - 1), rs = exp.substr(i + 1); + } + break; + } + } + } + } + else + {// ...(...) or ...*... or ...... + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + l++; + } + else if (exp[i] == ')') + { + l--; + } + if (l == 0) + { + if (i + 1 < exp.length() && exp[i + 1] == '*') + { + hasStar = 1; + ls = exp.substr(0, i + 1), rs = exp.substr(i + 2); + } + else + { + ls = exp.substr(0, i + 1), rs = exp.substr(i + 1); + } + break; + } + } + } + for (int i = 0; rs.length() > 0;) + { + while (i < rs.length() && rs[i] == '*') + i++; + rs = rs.substr(i); + break; + } + NFA L = parser(ls); + NFA R = parser(rs); + if (hasStar) + L = NFA(L, '*'); + return NFA(L, R, '&'); +} + +State *gmap[MAXSTATE]; +int relabel(NFA A) +{ + int size = 0; + State *u, *v; + queue Q; + Q.push(A.st), A.st->label = ++size; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + gmap[u->label] = u; + for (int it = 0; it < 11; it++) + { + for (int i = 0; i < u->trans[it].size(); i++) + { + v = u->trans[it][i]; + if (v->label == 0) + { + v->label = ++size; + Q.push(v); + } + } + } + } + return size; +} + +char s[16777216]; +int used[MAXSTATE], ACable[MAXSTATE]; + +int closure(vector &A, int x, int cases) +{ + queue Q; + State *u; + int accept = 0; + if (used[x] != cases) + A.push_back(x), used[x] = cases; + Q.push(x); + while (!Q.empty()) + { + x = Q.front(), Q.pop(); + u = gmap[x]; + accept |= u->ac; + if (u->trans[epsilon].size() == 0) + continue; + for (auto &y : u->trans[epsilon]) + { + if (used[y->label] != cases) + { + Q.push(y->label), used[y->label] = cases; + A.push_back(y->label); + } + } + } + return accept; +} +void rebuild(int n, int ctype) +{ + memset(ACable, 0, sizeof(ACable)); + memset(used, 0, sizeof(used)); + int cases = 0; + for (int i = 1; i <= n; i++) + { + cases++; + vector cc; + closure(cc, i, cases); + for (int j = 1; j <= ctype; j++) + { + set S; + for (int k = 0; k < cc.size(); k++) + { + State *u = gmap[cc[k]]; + for (auto &p : u->trans[j]) + S.insert(p->label); + ACable[i] |= u->ac; + } + State *u = gmap[i]; + u->trans[j].clear(); + for (auto &x : S) + u->trans[j].push_back(gmap[x]); + } + } +} + +int main() +{ + int n, m; + char regex[512]; + while (scanf("%d %s", &n, regex) == 2) + { + nodesize = 0;// global + NFA nfa = parser(regex); + m = relabel(nfa); + rebuild(m, n); + scanf("%s", s); + + memset(used, 0, sizeof(used)); + int cases = 0, flag = 0; + + vector A; + cases++; + A.push_back(1); + for (int i = 0; s[i]; i++) + { + vector next; + cases++; + int accept = 0; + for (int j = 0; j < A.size(); j++) + { + int x = A[j]; + State *u = gmap[x]; + if (u->trans[toIndex(s[i])].size() == 0) + continue; + for (auto &y : u->trans[toIndex(s[i])]) + { + if (used[y->label] != cases) + { + used[y->label] = cases, next.push_back(y->label); + accept |= ACable[y->label]; + } + } + } + if (used[1] != cases) + used[1] = cases, next.push_back(1); + A = next; + if (accept) + { + if (flag) + putchar(' '); + flag = 1; + printf("%d", i + 1); + } + } + puts(""); + } + return 0; +} diff --git a/12416.cpp b/12416.cpp index 4c7777a..f4dc876 100644 --- a/12416.cpp +++ b/12416.cpp @@ -1,22 +1,38 @@ -#include -#include -#include -#include +#include + using namespace std; -int main() { - string str; - while (getline(cin, str)) { - double action = 0.0, space = 0.0; - for (int i = 0; i < str.length(); i++) { - if (str[i] == ' ') space += 1.0; - else { - //cout << space << " "; - action = max(action, log2(space)); - space = 0; +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int longest_spaces(string &input) +{ + int result = 0; + int tmp = 0; + rep(i, input.size()) + { + if (input[i] == ' ') + { + tmp++; + } + else + { + if (tmp > result) + { + result = tmp; } + tmp = 0; } - cout << ceil(action) << endl; + } + return result; +} + +int main() +{ + string input; + while (getline(cin, input)) + { + cout << ceil(log2(longest_spaces(input))) << endl; } return 0; } diff --git a/12418.cpp b/12418.cpp new file mode 100644 index 0000000..42bb982 --- /dev/null +++ b/12418.cpp @@ -0,0 +1,253 @@ +#include + +using namespace std; + +const int MOD = 100007; + +struct pp +{ + int id, dig, ip; +}; +pp adj[11][11], nw; + +struct sta +{ + int pos[11]; +}; +sta now; + +struct re +{ + char str[13]; + int len; + bool operator<(const re &temp) const + { + return strcmp(str, temp.str) < 0; + } +}; +re res[1 << 11]; + +bool have_esc[1 << 11], vi[11], can[11][11]; +int n, m, num[11], dig[11], cnt_dig, sum, ns, tr[11][1 << 10], num_tr[11], vis[11], num_ans, comb[1 << 11], num_com, at[10], num_h[MOD]; +long long hash_[MOD][107]; + +bool dfs(int state, int iden, int deep) +{ + int i, j, s, p, q, id, ip, nt, idd, vv, la, nst; + long long st; + sta nw; + nt = 0; + st = 0; + int mt = 0; + for (i = 0; i < 9; i++) + { + id = now.pos[i]; + st = (long long)n * st + id; + if (id == n - 1) + { + nt++; + } + if (can[id][n - 1]) + { + mt++; + } + } + if (mt < num_ans) + { + return false; + } + st = (1LL << m) * st + state; + if (num_ans < nt) + { + num_ans = nt; + num_com = 1; + have_esc[at[n - 1]] = true; + comb[0] = at[n - 1]; + } + else if (num_ans == nt && nt > 0 && !have_esc[at[n - 1]]) + { + comb[num_com++] = at[n - 1]; + have_esc[at[n - 1]] = true; + } + if (num_ans == 9) + { + return true; + } + la = (int)(st % MOD); + for (i = 0; i < num_h[la]; i++) + { + if (hash_[la][i]) + { + return false; + } + } + hash_[la][num_h[la]++] = st; + for (int ii = iden; ii < iden + n; ii++) + { + i = ii % n; + if (!can[i][n - 1]) + { + continue; + } + for (j = 0; j < num[i]; j++) + { + id = i; + vv = adj[i][j].dig; + ip = adj[i][j].ip; + idd = adj[i][j].id; + if (vv == 0 || !(state & (1 << ip))) + { + if (vv != 0) + { + state |= (1 << ip); + } + for (s = 0; s < num_tr[vv]; s++) + { + nst = (at[id] & tr[vv][s]); + if (nst != tr[vv][s]) + { + continue; + } + at[id] ^= nst; + at[idd] |= nst; + for (p = 0; p < 9; p++) + { + if (nst & (1 << p)) + { + now.pos[p] = idd; + } + } + if (dfs(state, i, deep + 1)) + { + return true; + } + at[id] |= nst; + at[idd] ^= nst; + for (p = 0; p < 9; p++) + { + if (nst & (1 << p)) + { + now.pos[p] = id; + } + } + } + if (vv != 0) + { + state ^= (1 << ip); + } + } + } + } + return false; +} +void get_to(int id, int ip) +{ + if (can[id][ip]) + { + return; + } + can[id][ip] = true; + int i, j, s, p, q; + for (i = 0; i < num[ip]; i++) + { + get_to(id, adj[ip][i].id); + } +} +int main() +{ + int i, j, s, p, q, u, v, d, id; + memset(num_tr, 0, sizeof(num_tr)); + for (i = 1; i < (1 << 9); i++) + { + cnt_dig = 0; + for (j = 0; j < 9; j++) + { + if (i & (1 << j)) + { + dig[cnt_dig++] = j + 1; + } + } + if (cnt_dig > 5 || cnt_dig < 3) + { + continue; + } + sum = 0; + for (j = 0; j < cnt_dig; j++) + { + sum += dig[j]; + } + while (sum >= 10) + { + ns = 0; + while (sum) + { + ns += sum % 10; + sum /= 10; + } + sum = ns; + } + tr[sum][num_tr[sum]++] = i; + } + for (i = 1; i <= 9; i++) + { + tr[0][num_tr[0]++] = (1 << (i - 1)); + } + for (i = 0; i < 10; i++) + { + sort(tr[i], tr[i] + num_tr[i]); + } + while (scanf("%d%d", &n, &m) == 2) + { + memset(num, 0, sizeof(num)); + for (i = 0; i < m; i++) + { + scanf("%d%d%d", &u, &v, &d); + u--; + v--; + nw.id = v; + nw.dig = d; + nw.ip = i; + adj[u][num[u]++] = nw; + dig[i] = d; + } + memset(can, false, sizeof(can)); + for (i = 0; i < n; i++) + { + get_to(i, i); + } + memset(vis, -1, sizeof(vis)); + for (i = 0; i < 9; i++) + { + now.pos[i] = 0; + } + num_ans = 0; + memset(have_esc, false, sizeof(have_esc)); + memset(at, 0, sizeof(at)); + at[0] = (1 << 9) - 1; + num_com = 0; + memset(num_h, 0, sizeof(num_h)); + dfs(0, 0, 0); + for (i = 0; i < num_com; i++) + { + id = comb[i]; + res[i].len = 0; + for (j = 0; j < 9; j++) + { + if (id & (1 << j)) + { + res[i].str[res[i].len++] = j + 1 + '0'; + } + } + res[i].str[res[i].len] = 0; + } + sort(res, res + num_com); + printf("%d", num_ans); + for (i = 0; i < num_com; i++) + { + printf(" "); + printf("%s", res[i].str); + } + printf("\n"); + } + return 0; +} diff --git a/1242.cpp b/1242.cpp new file mode 100644 index 0000000..c24282a --- /dev/null +++ b/1242.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define N 10005 +#define NV i->first +#define p Q.front() + +int karp(map *, map *, int, int); +int bfs(map *, map *, int[], int, int); + +int karp(map adj[], map cap[], int s, int t) +{ + int prev[N] = {0}, f, df; + + for (f = 0; df = bfs(adj, cap, prev, s, t); ++f) + for (int i = prev[t], j = t; i != j; i = prev[j = i]) + adj[i][j] += df, adj[j][i] -= df; + + return f; +} + +int bfs(map adj[], map cap[], int prev[], int s, int t) +{ + queue Q; + bool vis[N] = {true}; + int current[N] = {INT_MAX}; + + for (Q.push(s); !Q.empty(); Q.pop()) + { + for (map::iterator i = adj[p].begin(); i != adj[p].end(); ++i) + if (!vis[NV] && cap[p][NV] - adj[p][NV]) + { + vis[NV] = true; + current[NV] = min(current[p], cap[p][NV] - adj[p][NV]); + prev[NV] = p; + if (NV == t) + return current[t]; + else + Q.push(NV); + } + } + return 0; +} + +int main() +{ + for (int t = 0, V, E, S, T; scanf("%d%d", &V, &E) && V;) + { + map adj[V + 1], cap[V + 1]; + + for (int i = 0, a, b; i < E; ++i) + { + scanf("%d%d", &a, &b); + adj[a][b], ++cap[a][b]; + adj[b][a], ++cap[b][a]; + } + scanf("%d%d", &S, &T); + adj[0][S], cap[0][S] = 2; + + printf("Case %d: %s\n", ++t, karp(adj, cap, 0, T) == 2 ? "YES" : "NO"); + } + return 0; +} diff --git a/12420.cpp b/12420.cpp new file mode 100644 index 0000000..db126b6 --- /dev/null +++ b/12420.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +struct Predict +{ + int movie_; + double score_; + + Predict(int movie, double score) + : movie_(movie), score_(score) + { + } +}; + +bool cmp(const Predict &a, const Predict &b) +{ + return a.score_ > b.score_; +} + +int main() +{ + int n, m, c; + scanf("%d%d%d", &n, &m, &c); + + double mat_rating[52][202] = {}, mat_sim[202][202] = {}; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + mat_rating[i][j] = -1.0f; + } + } + + double rating; + int usr, movie; + for (int i = 0; i < c; i++) + { + scanf("%d%d%lf", &usr, &movie, &rating); + mat_rating[usr][movie] = rating; + } + + for (int i = 1; i <= m; i++) + { + for (int j = i; j <= m; j++) + { + double rating_diff = 0.0f; + for (int k = 1; k <= n; k++) + { + double rating_i = mat_rating[k][i]; + double rating_j = mat_rating[k][j]; + if ((rating_i > 0.0f) && (rating_j > 0.0f)) + { + double diff = rating_i - rating_j; + rating_diff = rating_diff + diff * diff; + } + } + mat_sim[i][j] = 1.0f / (1.0f + rating_diff); + mat_sim[j][i] = mat_sim[i][j]; + } + } + + while (scanf("%d", &usr) == 1) + { + vector predicts; + for (int i = 1; i <= m; i++) + { + if (mat_rating[usr][i] < 0.0f) + { + bool is_similar = false; + double numer = 0.0f, denom = 0.0f; + for (int j = 1; j <= m; j++) + { + if (mat_rating[usr][j] < 0.0f) + { + continue; + } + is_similar = true; + numer += (mat_sim[i][j] * mat_rating[usr][j]); + denom += mat_sim[i][j]; + } + + // if denom == zero + // means movie i is dis-similar to any movie watched + // shd not recommend + if (is_similar) + { + double score = numer / denom; + predicts.push_back(Predict(i, score)); + } + } + } + sort(predicts.begin(), predicts.end(), cmp); + + printf("Recommendations for user %d:\n", usr); + for (unsigned int i = 0; i < predicts.size(); i++) + { + if (i > 9) + { + break; + } + printf("%d %.3lf\n", predicts[i].movie_, predicts[i].score_); + } + printf("\n"); + } + + return 0; +} diff --git a/12424.cpp b/12424.cpp new file mode 100644 index 0000000..a59570a --- /dev/null +++ b/12424.cpp @@ -0,0 +1,266 @@ +#include + +using namespace std; + +#define MAXN 100001 +struct node +{ + int num; + node *next; +}; +struct number +{ + int left, right; + bool mark; + int sum[11]; +}; +int ans[11]; +int father[MAXN]; +int Log[2 * MAXN]; +int n, m, T; +int d[MAXN]; +int euler[2 * MAXN]; +int low[MAXN], high[MAXN]; +int color[MAXN][11]; +int c[MAXN]; +number tree[4 * MAXN]; +node *graph[MAXN]; +node memo[2 * MAXN]; +int home[MAXN]; +int sparse_table[2 * MAXN][20]; +int sparse_table_num[2 * MAXN][20]; +int top, label; +void LOG() +{ + Log[1] = 0; + for (int i = 2; i < 2 * MAXN; i++) + { + Log[i] = Log[i / 2] + 1; + } +} +void add(int x, int y) +{ + node *p = &memo[top++]; + p->num = y; + p->next = graph[x]; + graph[x] = p; + p = &memo[top++]; + p->num = x; + p->next = graph[y]; + graph[y] = p; +} +void dfs(int i) +{ + low[i] = ++label; + for (int j = 1; j <= 10; j++) + { + color[low[i]][j] += color[low[father[i]]][j]; + } + color[low[i]][c[i]]++; + euler[++top] = i; + home[i] = top; + for (node *p = graph[i]; p; p = p->next) + { + if (!low[p->num]) + { + father[p->num] = i; + d[p->num] = d[i] + 1; + dfs(p->num); + euler[++top] = i; + } + } + high[i] = label; +} +void segment(int i) +{ + if (tree[i].left == tree[i].right) + { + for (int j = 1; j <= 10; j++) + { + tree[i].sum[j] = color[tree[i].left][j]; + } + return; + } + int mid = (tree[i].left + tree[i].right) / 2; + tree[2 * i].left = tree[i].left; + tree[2 * i].right = mid; + tree[2 * i + 1].left = mid + 1; + tree[2 * i + 1].right = tree[i].right; + segment(2 * i); + segment(2 * i + 1); +} +void push_down(int i) +{ + for (int j = 1; j <= 10; j++) + { + if (tree[i].sum[j] != 0 && tree[i].left != tree[i].right) + { + tree[2 * i].mark = tree[2 * i + 1].mark = 1; + tree[2 * i].sum[j] += tree[i].sum[j]; + tree[2 * i + 1].sum[j] += tree[i].sum[j]; + } + tree[i].sum[j] = 0; + } + tree[i].mark = 0; +} +void change(int i, int x, int y, int k_add, int k_minus) +{ + if (tree[i].left == x && tree[i].right == y) + { + tree[i].mark = 1; + tree[i].sum[k_add]++; + tree[i].sum[k_minus]--; + return; + } + if (tree[i].mark) + { + push_down(i); + } + int mid = (tree[i].left + tree[i].right) / 2; + if (y <= mid) + { + change(2 * i, x, y, k_add, k_minus); + } + else if (x > mid) + { + change(2 * i + 1, x, y, k_add, k_minus); + } + else + { + change(2 * i, x, mid, k_add, k_minus); + change(2 * i + 1, mid + 1, y, k_add, k_minus); + } +} +void search(int i, int x, int multi) +{ + if (tree[i].left == tree[i].right) + { + for (int j = 1; j <= 10; j++) + { + ans[j] += multi * tree[i].sum[j]; + } + return; + } + if (tree[i].mark) + { + push_down(i); + } + int mid = (tree[i].left + tree[i].right) / 2; + if (x <= mid) + { + search(2 * i, x, multi); + } + else + { + search(2 * i + 1, x, multi); + } +} +void build_sparse_table() +{ + int i, j; + for (i = 1; i <= top; i++) + { + sparse_table[i][0] = d[euler[i]]; + sparse_table_num[i][0] = euler[i]; + } + for (j = 1; (1 << j) <= top; j++) + { + for (i = 1; i <= top - (1 << j) + 1; i++) + { + if (sparse_table[i][j - 1] < sparse_table[i + (1 << (j - 1))][j - 1]) + { + sparse_table[i][j] = sparse_table[i][j - 1]; + sparse_table_num[i][j] = sparse_table_num[i][j - 1]; + } + else + { + sparse_table[i][j] = sparse_table[i + (1 << (j - 1))][j - 1]; + sparse_table_num[i][j] = sparse_table_num[i + (1 << (j - 1))][j - 1]; + } + } + } +} +int LCA(int x, int y) +{ + if (x > y) + { + int t; + t = x; + x = y; + y = t; + } + int z = Log[y - x + 1]; + if (sparse_table[x][z] < sparse_table[y - (1 << z) + 1][z]) + { + return sparse_table_num[x][z]; + } + else + { + return sparse_table_num[y - (1 << z) + 1][z]; + } +} +int main() +{ + scanf("%d", &T); + int i, j, k; + int x, y, z; + LOG(); + for (k = 1; k <= T; k++) + { + top = label = 0; + memset(low, 0, sizeof(low)); + memset(graph, 0, sizeof(graph)); + memset(tree, 0, sizeof(tree)); + memset(color, 0, sizeof(color)); + scanf("%d%d", &n, &m); + for (i = 1; i <= n; i++) + { + scanf("%d", &x); + c[i] = x; + } + for (i = 1; i < n; i++) + { + scanf("%d%d", &x, &y); + add(x, y); + } + father[1] = 0; + d[1] = 0; + top = 0; + dfs(1); + tree[1].left = 1; + tree[1].right = n; + segment(1); + build_sparse_table(); + for (i = 1; i <= m; i++) + { + scanf("%d%d%d", &z, &x, &y); + if (z == 0) + { + change(1, low[x], high[x], y, c[x]); + c[x] = y; + } + else + { + int fa = LCA(home[x], home[y]); + int MAX = 0; + memset(ans, 0, sizeof(ans)); + search(1, low[x], 1); + search(1, low[y], 1); + search(1, low[fa], -2); + for (j = 1; j <= 10; j++) + { + if (c[fa] == j) + { + ans[j]++; + } + if (ans[j] > MAX) + { + MAX = ans[j]; + } + } + printf("%d\n", MAX); + } + } + } + return 0; +} diff --git a/12425.cpp b/12425.cpp new file mode 100644 index 0000000..17dba40 --- /dev/null +++ b/12425.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +const int N = 1000005; +typedef long long LL; +LL eular_cnt[N]; +LL eular_phi(LL n) +{ + LL res = n; + for (LL i = 2; i * i <= n; i++) + if (n % i == 0) + { + res = res / i * (i - 1); + while (n % i == 0) + { + n /= i; + } + } + if (n > 1) + { + res = res / n * (n - 1); + } + return res; +} +int main() +{ + int t, t_cnt = 0; + scanf("%d", &t); + while (t--) + { + LL n, q; + vector data; + scanf("%lld%lld", &n, &q); + for (LL i = 1; i * i <= n; i++) + { + if (n % i) + { + continue; + } + data.push_back(i); + if (n / i != i) + { + data.push_back(n / i); + } + } + sort(data.begin(), data.end()); + for (int i = 0; i < (int)data.size(); i++) + { + eular_cnt[i + 1] = eular_cnt[i] + eular_phi(n / data[i]); + } + printf("Case %d\n", ++t_cnt); + for (LL i = 0; i < q; i++) + { + LL x; + scanf("%lld", &x); + int pos = upper_bound(data.begin(), data.end(), x) - data.begin(); + printf("%lld\n", eular_cnt[pos]); + } + } + return 0; +} diff --git a/12426.cpp b/12426.cpp new file mode 100644 index 0000000..0a8e90d --- /dev/null +++ b/12426.cpp @@ -0,0 +1,166 @@ +#include + +using namespace std; + +#define MAXN 1001 +struct point +{ + long long x, y; + point() {} + point(long long x0, long long(y0)) : x(x0), y(y0) {} +}; +int n, T; +long long max_area; +point a[2 * MAXN]; +int edge[MAXN][MAXN][2]; +point operator-(const point &A, const point &B) +{ + return point(A.x - B.x, A.y - B.y); +} +long long cross(point A, point B) +{ + return A.x * B.y - A.y * B.x; +} +void rotate() +{ + int i, k; + int j1, j2; + for (k = 1; k < n; k++) + { + j1 = 1; + j2 = k + 1; + for (i = 1; i <= n; i++) + { + if (i + k > n) + { + break; + } + while (abs(cross(a[i] - a[j1 + 1], a[i + k] - a[j1 + 1])) > abs(cross(a[i] - a[j1], a[i + k] - a[j1]))) + { + j1 = j1 % n + 1; + } + edge[i][i + k][0] = j1; + while (abs(cross(a[i] - a[j2 + 1], a[i + k] - a[j2 + 1])) > abs(cross(a[i] - a[j2], a[i + k] - a[j2]))) + { + j2 = j2 % n + 1; + } + edge[i][i + k][1] = j2; + } + } +} +int binary(int i, int k, int j) +{ + long long s; + int left = i, right; + int ans = 0; + if (k < i) + { + right = k + n; + } + else + { + right = k; + } + while (left < right) + { + int mid = (left + right) / 2; + s = abs(cross(a[i] - a[mid], a[j] - a[mid])); + if (max_area < s) + { + right = mid; + } + else + { + left = mid; + } + if (right - left == 1) + { + break; + } + } + ans = left - i; + left = k; + if (j < k) + { + right = j + n; + } + else + { + right = j; + } + while (left < right) + { + int mid = (left + right) / 2; + s = abs(cross(a[i] - a[mid], a[j] - a[mid])); + if (max_area < s) + { + left = mid; + } + else + { + right = mid; + } + if (right - left == 1) + { + break; + } + } + ans += (j + n - right) % n; + return ans; +} +int solve() +{ + int i, j; + long long ans = 0; + int k1, k2; + long long s; + for (i = 1; i < n; i++) + for (j = i + 1; j <= n; j++) + { + k1 = edge[i][j][0]; + k2 = edge[i][j][1]; + s = abs(cross(a[i] - a[k1], a[j] - a[k1])); + if (j > i + 1) + { + if (max_area >= s) + { + ans += j - i - 1; + } + else + { + ans += binary(i, k1, j); + } + } + if (i + n > j + 1) + { + s = abs(cross(a[i] - a[k2], a[j] - a[k2])); + if (max_area >= s) + { + ans += i + n - j - 1; + } + else + { + ans += binary(j, k2, i); + } + } + } + return ans / 3; +} +int main() +{ + scanf("%d", &T); + int i, j; + for (j = 1; j <= T; j++) + { + scanf("%d%lld", &n, &max_area); + max_area *= 2; + for (i = 1; i <= n; i++) + { + scanf("%lld%lld", &a[i].x, &a[i].y); + a[n + i] = a[i]; + } + rotate(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/12427.cpp b/12427.cpp new file mode 100644 index 0000000..dbb33cc --- /dev/null +++ b/12427.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +typedef long long int64; + +int main() +{ + int T; + scanf("%d", &T); + for (int i = 0; i < T; i++) + { + int64 A1, A2, B1, B2, C1, C2; + scanf("%lld%lld%lld%lld%lld%lld", &A1, &A2, &B1, &B2, &C1, &C2); + int64 ans = (B2 - B1 + 1) * (A2 / 2 + 1 - (A1 ? (A1 - 1) / 2 + 1 : 0)) * (C2 / 2 + 1 - (C1 ? (C1 - 1) / 2 + 1 : 0)); + printf("Case %d: %lld\n", i + 1, ans); + } + return 0; +} diff --git a/12428.cpp b/12428.cpp new file mode 100644 index 0000000..2728e6e --- /dev/null +++ b/12428.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +typedef long long int64; +int64 N, M; +void init() +{ + scanf("%lld%lld", &N, &M); +} +int solve() +{ + if (M < N) + { + return M; + } + int ans = N - 2; + M -= N - 1; + for (int cur = 1; M > 0; cur++) + { + ans--; + M -= cur; + } + return ans; +} +int main() +{ + int T; + scanf("%d", &T); + for (; T--;) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/12429.cpp b/12429.cpp new file mode 100644 index 0000000..151082b --- /dev/null +++ b/12429.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +#define NMAX 100001 +int n, k; +int tree[NMAX]; +long long ans; +int lowbit(int x) +{ + return x & (x ^ (x - 1)); +} +void update(int i) +{ + while (i <= k) + { + tree[i]++; + i += lowbit(i); + } +} +int sum(int i) +{ + int ret = 0; + while (i > 0) + { + ret += tree[i]; + i -= lowbit(i); + } + return ret; +} +void add(int Min, int Max, int t) +{ + long long ret = 0; + if (Min < Max) + { + ret = sum(Max) - sum(Min); + } + else + { + ret = sum(k) - sum(Min) + sum(Max); + } + ans += t * ret + (t - 1) * (sum(k) - ret); +} +int main() +{ + int T; + int i, j; + int b; + int Min, Max; + int temp; + scanf("%d", &T); + for (j = 1; j <= T; j++) + { + ans = 0; + memset(tree, 0, sizeof(tree)); + scanf("%d%d", &n, &k); + for (b = n; b > 0; b--) + { + temp = ((long long)b * b * b - 1) % k + 1; + update(temp); + temp = ((long long)b * b - 1) % k + 1; + Max = (b % k + temp + k - 1) % k + 1; + Min = temp % k; + add(Min, Max, (b - 1) / k + 1); + } + printf("Case %d: %lld\n", j, ans); + } + return 0; +} diff --git a/12430.cpp b/12430.cpp new file mode 100644 index 0000000..606d937 --- /dev/null +++ b/12430.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define MAXN 50000 +#define MAXM 80000 + +int mi; +vector L[MAXN], W[MAXN]; +int color[MAXN]; + +bool check(int pos, int c = 0) +{ + color[pos] = c; + for (int i = L[pos].size() - 1, to; i >= 0; --i) + { + if (W[pos][i] < mi) + { + to = L[pos][i]; + if (color[to] != -1 && color[to] == c) + { + return false; + } + if (color[to] == -1 && !check(to, c ^ 1)) + { + return false; + } + } + } + return true; +} + +int main() +{ + int T, N, M; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &N, &M); + for (int i = 0; i < N; ++i) + { + L[i].clear(); + W[i].clear(); + } + int mn = INT_MAX; + for (int i = 0, u, v, w; i < M; ++i) + { + scanf("%d %d %d", &u, &v, &w); + --u; + --v; + L[u].push_back(v); + W[u].push_back(w); + L[v].push_back(u); + W[v].push_back(w); + mn = min(mn, w); + } + int lo = mn, hi = INT_MAX; + while (lo < hi) + { + mi = lo + ((hi - lo + 1) >> 1); + memset(color, -1, sizeof color); + bool ok = true; + for (int i = 0; i < N; ++i) + if (color[i] == -1) + { + ok = ok && check(i); + } + if (!ok) + { + hi = mi - 1; + } + else + { + lo = mi; + } + } + if (lo == INT_MAX) + { + puts("0"); + } + else if (lo == mn) + { + puts("-1"); + } + else + { + printf("%d\n", lo); + } + } + return 0; +} diff --git a/12431.cpp b/12431.cpp new file mode 100644 index 0000000..3f30e95 --- /dev/null +++ b/12431.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +typedef unsigned long long LL; + +//typedef long LL; +LL mmod(LL a, LL b, LL n) +{ + a = a % n; + LL res = 0; + while (b) + { + if (b & 1) + { + res = res + a; + if (res >= n) + { + res = res - n; + } + } + a = a << 1; + if (a >= n) + { + a = a - n; + } + b = b >> 1; + } + return res; +} + +LL exmod(LL a, LL b, LL n) +{ + a = a % n; + LL res = 1; + while (b >= 1) + { + if (b & 1) + { + res = mmod(res, a, n); + } + a = mmod(a, a, n); + b = b >> 1; + } + return res; +} + +LL same(LL n, LL b, LL d, LL m) +{ + LL res = exmod(b, n, (b - 1) * m); + //printf("%lld %lld %lld %lld %lld\n", n, b, d, m, res); + if (res == 0) + { + res += (b - 1) * m - 1; + } + else + { + res -= 1; + } + res /= (b - 1); + res = (res * d) % m; + return res; +} + +int main() +{ + int T, cas = 1; + scanf("%d", &T); + while (T--) + { + LL n, b, d, m; + scanf("%lld%ld%ld%ld", &n, &b, &d, &m); + printf("Case %d: %lld\n", cas++, same(n, b, d, m)); + } + return 0; +} diff --git a/12432.cpp b/12432.cpp new file mode 100644 index 0000000..38f3853 --- /dev/null +++ b/12432.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define MaxN 1010 +#define LL long long + +struct Seg +{ + int l, r, c; + LL p; + bool operator<(const Seg &a) const + { + return r < a.r; + } +} seg[MaxN]; + +LL dp[MaxN][4], cost[64]; +; +int cg[MaxN][4]; +bool cheap[64]; + +void update(int i, int j, LL x, int y) +{ + if (dp[i][j] == -1 || dp[i][j] > x || (dp[i][j] == x && cg[i][j] > y)) + { + dp[i][j] = x, cg[i][j] = y; + } +} + +int main() +{ + int T, cas = 0, l, n, m; + LL d; + scanf("%d", &T); + while (T--) + { + LL mv = -1; + cin >> l >> n >> m >> d; + for (int i = 1; i <= m; ++i) + { + cin >> cost[i]; + if (mv == -1 || cost[i] < mv) + { + mv = cost[i]; + } + } + for (int i = 1; i <= m; ++i) + { + cheap[i] = mv == cost[i]; + } + cheap[0] = cheap[m + 1] = 0; + cost[0] = cost[m + 1] = 0; + for (int i = 1; i <= n; ++i) + { + cin >> seg[i].l >> seg[i].r >> seg[i].c >> seg[i].p; + } + sort(seg + 1, seg + n + 1); + seg[0].p = seg[n + 1].p = seg[0].l = seg[0].r = seg[0].c = 0; + seg[n + 1].l = seg[n + 1].r = l + 1; + seg[n + 1].c = m + 1; + memset(dp, -1, sizeof(dp)); + dp[0][0] = 0; + for (int i = 1; i <= n + 1; ++i) + { + for (int k = 0; k < i; ++k) + if (seg[k].r < seg[i].l) + { + for (int j = 0; j < 3; ++j) + if (dp[k][j] != -1) + { + if (cheap[seg[i].c] || cheap[seg[k].c]) + { + update(i, 0, dp[k][j] + seg[i].p + (seg[i].l - seg[k].r - 1) * mv, cg[k][j] + (seg[k].c != seg[i].c)); + } + else + { + update(i, 0, dp[k][j] + seg[i].p + (seg[i].l - seg[k].r - 1) * mv, cg[k][j] + (seg[i].l != seg[k].r + 1) + (seg[i].l != seg[k].r + 1 || seg[i].c != seg[k].c)); + } + } + if (seg[i].l == seg[k].r + 1 && seg[i].c == seg[k].c) + { + for (int j = 0; j < 2; ++j) + if (dp[k][j] != -1) + { + update(i, j + 1, dp[k][j] + seg[i].p, cg[k][j]); + } + if (dp[k][1] != -1) + { + update(i, 2, dp[k][1] + seg[i].p - min(seg[i].p, d), cg[k][1]); + } + if (dp[k][2] != -1) + { + update(i, 2, dp[k][2] + seg[i].p - min(seg[i].p, d), cg[k][2]); + } + } + } + } + // for (int i = 1; i <= n+1; ++i) for (int j = 0; j < 3; ++j) printf("%d %d : %d %d\n", i, j, dp[i][j], cg[i][j]); + cout << "Case " << ++cas << ": " << dp[n + 1][0] << " " << cg[n + 1][0] - 2 << endl; + } + return 0; +} diff --git a/12433.cpp b/12433.cpp new file mode 100644 index 0000000..c0cf18c --- /dev/null +++ b/12433.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +const int oo = 0x3f3f3f3f; +const int Max = 256; +const int MaxE = 200000; +const int MaxC = 10000; + +struct queue +{ + int q[Max], s, t; + bool empty() + { + return s == t; + } + void push(int x) + { + q[s++] = x; + if (s >= Max) + { + s -= Max; + } + } + int pop() + { + int x = q[t++]; + if (t >= Max) + { + t -= Max; + } + return x; + } +} Q; + +struct box +{ + int to, f, c; + box *r, *s; +} edge[MaxE], *hd[Max], *u[Max], *r[Max], *cp; + +int fa[Max], dis[Max], n, s, t; +bool inQ[Max]; + +box *addEdge(int x, int y, int f, int c) +{ + box *p = cp++, *q = cp++; + p->to = y, q->to = x; + p->f = f, q->f = 0; + p->c = c, q->c = -c; + p->s = hd[x], hd[x] = p; + q->s = hd[y], hd[y] = q; + p->r = q, q->r = p; + return p; +} + +bool spfa() +{ + memset(dis, 0x3f, sizeof(dis[0]) * n); + dis[s] = 0, inQ[s] = true; + Q.push(s); + int x, y, d; + while (!Q.empty()) + { + d = dis[x = Q.pop()]; + inQ[x] = false; + if (x == t) + { + continue; + } + for (box *p = hd[x]; p; p = p->s) + if (p->f > 0 && d + p->c < dis[y = p->to]) + { + dis[y] = d + p->c; + fa[y] = x, u[y] = p; + if (!inQ[y]) + { + Q.push(y); + inQ[y] = true; + } + } + } + return dis[t] < oo; +} +void minCost(int &res, int &flow) +{ + while (spfa()) + { + int d = oo, x = t; + while (x != s) + { + if (u[x]->f < d) + { + d = u[x]->f; + } + r[fa[x]] = u[x]; + x = fa[x]; + } + while (x != t) + { + r[x]->f -= d, r[x]->r->f += d; + x = r[x]->to; + } + res += d * dis[t]; + flow += d; + } +} + +const int MaxF = 64; +int V[MaxF], C[MaxF], P[MaxF], D[MaxF], S[MaxF]; + +int main() +{ + int tCase; + scanf("%d", &tCase); + for (int ct = 1; ct <= tCase; ct++) + { + int N, Cp, Sr; + scanf("%d%d%d", &N, &Cp, &Sr); + int Sum = 0; + for (int i = 0; i < N; i++) + { + scanf("%d", &V[i]); + Sum += V[i]; + } + for (int i = 0; i < Cp; i++) + { + scanf("%d%d", &C[i], &P[i]); + } + for (int i = 0; i < Sr; i++) + { + scanf("%d%d", &D[i], &S[i]); + } + n = N * 2 + Cp + 3; + s = n - 1, t = n - 2; + memset(hd, 0, sizeof(hd[0]) * n); + cp = edge; + int s2 = n - 3; + for (int i = 0; i < Cp; i++) + { + addEdge(s, i + N * 2, C[i], P[i]); + addEdge(i + N * 2, s2, C[i], 0); + } + for (int i = 0; i < N; i++) + { + addEdge(s, i * 2, V[i], 0); + addEdge(s2, i * 2 + 1, V[i], 0); + for (int j = 0; j < Sr; j++) + { + int x = i + D[j] + 1; + if (x < N) + { + addEdge(i * 2, x * 2 + 1, oo, S[j]); + } + } + if (i + 1 < N) + { + addEdge(i * 2, (i + 1) * 2, oo, 0); + } + addEdge(i * 2 + 1, t, V[i], 0); + } + int Res = 0, Flow = 0; + minCost(Res, Flow); + printf("Case %d: ", ct); + if (Flow < Sum) + { + puts("impossible"); + } + else + { + printf("%d\n", Res); + } + } + return 0; +} diff --git a/12435.cpp b/12435.cpp new file mode 100644 index 0000000..39a1c9f --- /dev/null +++ b/12435.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) +const int N_MAX = 1000003; +struct hash_table +{ + int a[N_MAX]; + short id[N_MAX], id_now; + + bool insert(int aa) + { + int i = aa % N_MAX; + while (id[i] == id_now && a[i] != aa) + { + i = (i + 1) % N_MAX; + } + if (id[i] != id_now) + { + a[i] = aa; + id[i] = id_now; + return true; + } + else + { + return false; + } + } +}; + +hash_table H; + +#define sq(x) (x) * (x) +#define sqsum(x, y) (sq(x) + sq(y)) +int main() +{ + int T0; + scanf("%d", &T0); + for (int T = 1; T <= T0; T++) + { + int n; + scanf("%d", &n); + int x[1000], y[1000]; + rep(i, n) scanf("%d%d", x + i, y + i); + int ans = 1; + H.id_now = T; + rep(j, n) rep(i, j) if (H.insert(sqsum(x[i] - x[j], y[i] - y[j]))) + { + ans++; + } + printf("Case %d: %d\n", T, ans); + } + return 0; +} diff --git a/12436.cpp b/12436.cpp new file mode 100644 index 0000000..24b0088 --- /dev/null +++ b/12436.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +const int N = 250005; +typedef long long LL; +struct node +{ + bool flag; + int left, right; + LL sum, add1, add2, valu, step; + LL mid() + { + return left + (right - left) / 2; + } + LL len() + { + return right - left + 1; + } + void changeAB(LL a, LL b, LL k) + { + add1 += a; + add2 += b; + step += k; + sum += (a + b) * len() / 2; + } + void changeC(LL a) + { + flag = 1; + valu = a; + add1 = 0; + add2 = 0; + step = 0; + sum = valu * len(); + } +}; +struct Segtree +{ + node tree[N * 4]; + void down(int ind) + { + if (tree[ind].flag) + { + tree[ind * 2].changeC(tree[ind].valu); + tree[ind * 2 + 1].changeC(tree[ind].valu); + tree[ind].flag = 0; + } + if (tree[ind].add1 || tree[ind].add2 || tree[ind].step) + { + LL add1 = tree[ind].add1, add2 = tree[ind].add2; + LL k = tree[ind].step; + LL mid = add1 + k * (tree[ind * 2].len() - 1); + tree[ind * 2].changeAB(add1, mid, k); + tree[ind * 2 + 1].changeAB(mid + k, add2, k); + tree[ind].add1 = 0; + tree[ind].add2 = 0; + tree[ind].step = 0; + } + } + void build(LL left, LL right, LL ind) + { + tree[ind].left = left; + tree[ind].right = right; + tree[ind].add1 = 0; + tree[ind].add2 = 0; + tree[ind].sum = 0; + tree[ind].valu = 0; + tree[ind].step = 0; + if (left != right) + { + LL mid = tree[ind].mid(); + build(left, mid, ind * 2); + build(mid + 1, right, ind * 2 + 1); + } + } + void updataAB(LL be, LL end, LL ind, LL step) + { + LL left = tree[ind].left, right = tree[ind].right; + if (be <= left && right <= end) + { + LL st, ed; + if (step >= 0) + { + st = left - be + 1; + ed = right - be + 1; + } + else + { + st = end - left + 1; + ed = end - right + 1; + } + tree[ind].changeAB(st, ed, step); + } + else + { + down(ind); + LL mid = tree[ind].mid(); + if (be <= mid) + { + updataAB(be, end, ind * 2, step); + } + if (end > mid) + { + updataAB(be, end, ind * 2 + 1, step); + } + tree[ind].sum = tree[ind * 2].sum + tree[ind * 2 + 1].sum; + } + } + void updataC(LL be, LL end, LL ind, LL valu) + { + LL left = tree[ind].left, right = tree[ind].right; + if (be <= left && right <= end) + { + tree[ind].changeC(valu); + } + else + { + down(ind); + LL mid = tree[ind].mid(); + if (be <= mid) + { + updataC(be, end, ind * 2, valu); + } + if (end > mid) + { + updataC(be, end, ind * 2 + 1, valu); + } + tree[ind].sum = tree[ind * 2].sum + tree[ind * 2 + 1].sum; + } + } + LL query(LL be, LL end, LL ind) + { + LL left = tree[ind].left, right = tree[ind].right; + if (be <= left && right <= end) + { + return tree[ind].sum; + } + else + { + down(ind); + LL mid = tree[ind].mid(); + LL sum1 = 0, sum2 = 0; + if (be <= mid) + { + sum1 = query(be, end, ind * 2); + } + if (end > mid) + { + sum2 = query(be, end, ind * 2 + 1); + } + return sum1 + sum2; + } + } +} seg; +int main() +{ + int n; + while (scanf("%d", &n) != EOF) + { + seg.build(1, N - 5, 1); + for (int i = 0; i < n; i++) + { + char str[10]; + LL a, b, c; + scanf("%s", str); + if (str[0] == 'A') + { + scanf("%lld%lld", &a, &b); + seg.updataAB(a, b, 1, 1); + } + else if (str[0] == 'B') + { + scanf("%lld%lld", &a, &b); + seg.updataAB(a, b, 1, -1); + } + else if (str[0] == 'C') + { + scanf("%lld%lld%lld", &a, &b, &c); + seg.updataC(a, b, 1, c); + } + else + { + scanf("%lld%lld", &a, &b); + printf("%lld\n", seg.query(a, b, 1)); + } + } + } + return 0; +} diff --git a/12437.cpp b/12437.cpp new file mode 100644 index 0000000..2f665cc --- /dev/null +++ b/12437.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) +const int INF = 1 << 29; + +int n; +vector G[10000]; + +int ans[10001]; +bool vis[10000]; + +int sz;// 木ã®é ‚点数 +pair dfs(int u, int pre) +{ + pair res(0, u); + vis[u] = true; + sz++; + rep(i, G[u].size()) + { + int v = G[u][i]; + if (v != pre) + { + pair tmp = dfs(v, u); + tmp.first++; + res = max(res, tmp); + } + } + return res; +} + +void f(int u) +{ + pair tmp = dfs(u, -1); + sz = 0; + int diam = dfs(tmp.second, -1).first; + int i; + // 最長パス上ã¯è¾ºã‚’一度ã ã‘通れã°ã„ã„ + for (i = 1; i <= diam; i++) + { + ans[i] = min(ans[i], i - 1); + } + // 最長パスã«ä¹—ã£ã¦ã„ãªã„辺ã¯äºŒåº¦é€šã‚‹ + for (; i <= sz; i++) + { + ans[i] = min(ans[i], diam + 2 * (i - 1 - diam)); + } +} + +int main() +{ + int T0; + scanf("%d", &T0); + for (int T = 1; T <= T0; T++) + { + int m; + scanf("%d%d", &n, &m); + rep(u, n) G[u].clear(); + rep(i, m) + { + int u, v; + scanf("%d%d", &u, &v); + u--; + v--; + G[u].push_back(v); + G[v].push_back(u); + } + rep(i, 10001) ans[i] = INF; + rep(u, n) vis[u] = false; + rep(u, n) if (!vis[u]) + { + f(u); + } + printf("Case %d:\n", T); + int q; + scanf("%d", &q); + while (q--) + { + int k; + scanf("%d", &k); + if (ans[k] < INF) + { + printf("%d\n", ans[k]); + } + else + { + puts("impossible"); + } + } + } + return 0; +} diff --git a/12438.cpp b/12438.cpp new file mode 100644 index 0000000..e6b6c00 --- /dev/null +++ b/12438.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) +typedef long long ll; +const int N_PHI = 15000; +int phi[N_PHI + 1]; +void build_phi() +{ + rep(i, N_PHI + 1) phi[i] = i; + for (int i = 2; i <= N_PHI; i++) + if (phi[i] == i) + { + for (int j = i; j <= N_PHI; j += i) + { + phi[j] = phi[j] / i * (i - 1); + } + } +} + +int main() +{ + build_phi(); + int len[N_PHI + 1];// len[i] := ( i 次㮠Farey 数列ã®é•·ã• ) + len[0] = 2; + rep(i, N_PHI) len[i + 1] = len[i] + phi[i + 1]; + for (ll I; scanf("%lld", &I), I != -1;) + { + if (I == 0) + { + puts("1 1"); + continue; + } + int n = 1, m; + for (m = 15000; m >= 2; m--) + if (2 * (I + m * m - 1) % (m * (m - 1)) == 0) + { + ll a = 2 * (I + m * m - 1) / (m * (m - 1)); + while (n <= 15000 && len[n] < a) + { + n++; + } + if (n > 15000) + { + m = 1; + break; + } + if (len[n] == a) + { + break; + } + } + if (m == 1) + { + puts("NOT FOUND"); + } + else + { + printf("%d %d\n", n, m); + } + } + return 0; +} diff --git a/12439.cpp b/12439.cpp new file mode 100644 index 0000000..6d62084 --- /dev/null +++ b/12439.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +map mp; + +int cnt_leap(int year) +{ + return year / 4 - year / 100 + year / 400; +} + +bool check(int year) +{ + if (year % 400 == 0) + { + return true; + } + if (year % 100 == 0) + { + return false; + } + if (year % 4 == 0) + { + return true; + } + return false; +} + +void solve() +{ + string tmp; + int day1, month1, year1, day2, month2, year2; + char c; + cin >> tmp >> day1 >> c >> year1; + month1 = mp[tmp]; + cin >> tmp >> day2 >> c >> year2; + month2 = mp[tmp]; + int res = cnt_leap(year2 - 1) - cnt_leap(year1); + if (month1 < 2 && check(year1)) + { + res++; + } + if ((month2 > 1 || (month2 == 1 && day2 == 29)) && check(year2)) + { + res++; + } + cout << res << endl; +} + +int main() +{ + mp["January"] = 0; + mp["February"] = 1; + mp["March"] = 2; + mp["April"] = 3; + mp["May"] = 4; + mp["June"] = 5; + mp["July"] = 6; + mp["August"] = 7; + mp["September"] = 8; + mp["October"] = 9; + mp["November"] = 10; + mp["December"] = 11; + int n; + cin >> n; + for (int i = 0; i < n; i++) + { + cout << "Case " << i + 1 << ": "; + solve(); + } + return 0; +} diff --git a/1244.cpp b/1244.cpp new file mode 100644 index 0000000..b39e759 --- /dev/null +++ b/1244.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +/** + require from 0 to n-1's longest palindromic path and + output minimum lexicographical palindrome + + since walk limited to i->j (ip ... q->end when p <= q + + record min value from which later can backtrack + + runtime complexity: O(n^4) +*/ + +char g[105][105]; +int dp[105][105]; + +int main() +{ + int testcase, n; + int i, j, k, p, q; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%s", g[i]); + } + memset(dp, 0, sizeof(dp)); + vector dpArgv[105][105]; + for (i = 0; i < n; i++) + { + for (j = i + 1; j < n; j++) + { + if (g[i][j] != '*') + { + dp[i][j] = 1; + dpArgv[i][j].push_back((i << 10) + j); + } + } + } + for (i = 2; i < n; i++) + { + for (j = 0; i + j < n; j++) + { + //dp[j][i+j] j:start, i+j:end + for (p = j + 1; p < i + j; p++)//j->p ... q->i+j + { + for (q = p; q < i + j; q++) + { + if (g[j][p] == g[q][i + j] && g[j][p] != '*') + { + if (dp[p][q] + 2 >= dp[j][i + j]) + { + if (dp[p][q] + 2 > dp[j][i + j]) + { + dpArgv[j][i + j].clear(); + } + dp[j][i + j] = dp[p][q] + 2; + dpArgv[j][i + j].push_back((p << 10) + q); + } + } + } + } + } + } + if (dp[0][n - 1] == 0) + { + puts("NO PALINDROMIC PATH"); + continue; + } + int p, q, tp, tq; + queue X, Y, rX, rY; + X.push(0), Y.push(n - 1); + char ansBuf[105]; + int ansIdx = 0; + while (!X.empty()) + { + char mn = 127; + while (!X.empty()) + { + p = X.front(), X.pop(); + q = Y.front(), Y.pop(); + rX.push(p), rY.push(q); + for (i = 0; i < dpArgv[p][q].size(); i++) + { + tp = dpArgv[p][q][i] >> 10; + tq = dpArgv[p][q][i] & 1023; + if (p == tp) + { + if (g[p][q] < mn) + { + mn = g[p][q]; + } + } + else if (g[p][tp] < mn) + { + mn = g[p][tp]; + } + } + } + ansBuf[ansIdx++] = mn; + while (!rX.empty()) + { + p = rX.front(), rX.pop(); + q = rY.front(), rY.pop(); + for (i = 0; i < dpArgv[p][q].size(); i++) + { + tp = dpArgv[p][q][i] >> 10; + tq = dpArgv[p][q][i] & 1023; + if (g[p][tp] == mn) + { + if (tp != tq && p + 1 != q) + { + X.push(tp), Y.push(tq); + } + } + } + } + } + if (dp[0][n - 1] & 1)//odd + { + for (i = 0; i < ansIdx; i++) + { + putchar(ansBuf[i]); + } + for (i = ansIdx - 2; i >= 0; i--) + { + putchar(ansBuf[i]); + } + } + else + { + for (i = 0; i < ansIdx; i++) + { + putchar(ansBuf[i]); + } + for (i = ansIdx - 1; i >= 0; i--) + { + putchar(ansBuf[i]); + } + } + puts(""); + } + return 0; +} diff --git a/12440.cpp b/12440.cpp new file mode 100644 index 0000000..9380131 --- /dev/null +++ b/12440.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +#define REP(a, b) for (int a = 0; a < b; a++) +#define FOR(a, b, c) for (int a = b; a <= c; a++) +#define RESET(a, b) memset(a, b, sizeof a) + +#define MP make_pair +#define F first +#define S second +#define PII pair + +#define MAXN 131072 + +int INF = 2123123123; + +struct node +{ + int val, prop; +}; + +int nkasus; +int n, h[MAXN], col[MAXN]; +int dp[MAXN]; +int pre[MAXN]; +int stek[MAXN], p; +node stri[2 * MAXN]; +set him; + +void isiPre() +{ + him.clear(); + int di = 1; + FOR(i, 1, n) + { + while (him.count(col[i])) + { + him.erase(col[di++]); + } + him.insert(col[i]); + pre[i] = di; + } +} + +void build(int nod, int ki, int ka) +{ + stri[nod].val = 0; + stri[nod].prop = 0; + if (ki < ka) + { + int tgh = (ki + ka) >> 1; + build(2 * nod + 1, ki, tgh); + build(2 * nod + 2, tgh + 1, ka); + } +} + +void push(int x) +{ + stri[2 * x + 1].prop += stri[x].prop; + stri[2 * x + 2].prop += stri[x].prop; + stri[x].prop = 0; +} + +int GET(int x) +{ + return stri[x].val + stri[x].prop; +} + +void update(int nod, int ki, int ka, int a, int b, int val) +{ + if ((a <= ki) && (ka <= b)) + { + stri[nod].prop += val; + } + else + { + int tgh = (ki + ka) >> 1; + push(nod); + if (a <= tgh) + { + update(2 * nod + 1, ki, tgh, a, b, val); + } + if (b > tgh) + { + update(2 * nod + 2, tgh + 1, ka, a, b, val); + } + stri[nod].val = min(GET(2 * nod + 1), GET(2 * nod + 2)); + } +} + +int query(int nod, int ki, int ka, int a, int b) +{ + if ((a <= ki) && (ka <= b)) + { + return GET(nod); + } + else + { + int tgh = (ki + ka) >> 1; + int ret = INF; + push(nod); + if (a <= tgh) + { + ret = min(ret, query(2 * nod + 1, ki, tgh, a, b)); + } + if (b > tgh) + { + ret = min(ret, query(2 * nod + 2, tgh + 1, ka, a, b)); + } + stri[nod].val = min(GET(2 * nod + 1), GET(2 * nod + 2)); + return ret; + } +} + +int main() +{ + scanf("%d", &nkasus); + REP(jt, nkasus) + { + scanf("%d", &n); + FOR(i, 1, n) + { + scanf("%d %d", &col[i], &h[i]); + } + h[0] = MAXN; + isiPre(); + stek[0] = 0; + p = 1; + build(0, 1, n); + FOR(i, 1, n) + { + while (h[stek[p - 1]] <= h[i]) + { + update(0, 1, n, stek[p - 2] + 1, stek[p - 1], -h[stek[p - 1]]); + p--; + } + stek[p++] = i; + update(0, 1, n, stek[p - 2] + 1, stek[p - 1], h[i]); + dp[i] = query(0, 1, n, pre[i], i); + update(0, 1, n, i + 1, i + 1, dp[i]); + } + printf("Case %d: %d\n", jt + 1, dp[n]); + } + return 0; +} diff --git a/12441.cpp b/12441.cpp new file mode 100644 index 0000000..cd8a9a0 --- /dev/null +++ b/12441.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +const int MOD = 1000000007; + +struct State +{ + int a, b, c; + State(int _a, int _b, int _c) : a(_a), b(_b), c(_c) + { + } + State() + { + } +}; +State prev_[110][110][310], que[110 * 110 * 310], st; + +int La, Lb, Lc, mark[110][110][310] = {}, cmark, dist[110][110][310], way[110][110][310], qh, qt, nxt[310][256]; +string A, B, C; + +void visit(int a, int b, int c, int d, int w) +{ + if (c > Lc) + { + return; + } + if (mark[a][b][c] != cmark) + { + mark[a][b][c] = cmark; + dist[a][b][c] = d; + way[a][b][c] = w; + prev_[a][b][c] = st; + que[qt++] = State(a, b, c); + } + else if (dist[a][b][c] == d) + { + int &ref = way[a][b][c]; + ref += w; + if (ref >= MOD) + { + ref -= MOD; + } + } +} + +void work() +{ + cin >> A >> B >> C; + La = A.size(), Lb = B.size(), Lc = C.size(); + fill(nxt[Lc], nxt[Lc] + 256, Lc); + for (int i = Lc - 1; i >= 0; --i) + { + memcpy(nxt[i], nxt[i + 1], sizeof nxt[i]); + nxt[i][C[i]] = i; + } + ++cmark; + dist[0][0][0] = 0; + way[0][0][0] = 1; + mark[0][0][0] = cmark; + qh = qt = 0; + que[qt++] = State(0, 0, 0); + int ret = -1; + State last; + while (qh < qt) + { + st = que[qh++]; + int a = st.a, b = st.b, c = st.c; + int d = dist[a][b][c], w = way[a][b][c]; + if (ret != -1 && d > ret) + { + break; + } + if (a == La && b == Lb) + { + if (ret == -1) + { + last = st; + ret = d; + } + continue; + } + if (a == La) + { + visit(a, b + 1, nxt[c][B[b]] + 1, d + 1, w); + continue; + } + if (b == Lb) + { + visit(a + 1, b, nxt[c][A[a]] + 1, d + 1, w); + continue; + } + if (A[a] != B[b]) + { + if (A[a] < B[b]) + { + visit(a + 1, b, nxt[c][A[a]] + 1, d + 1, w); + visit(a, b + 1, nxt[c][B[b]] + 1, d + 1, w); + } + else + { + visit(a, b + 1, nxt[c][B[b]] + 1, d + 1, w); + visit(a + 1, b, nxt[c][A[a]] + 1, d + 1, w); + } + continue; + } + visit(a + 1, b + 1, nxt[c][A[a]] + 1, d + 1, w); + } + if (ret == -1) + { + cout << 0 << endl; + cout << "NOT FOUND" << endl; + return; + } + // calc ans + int ans = 0; + for (int i = 0; i <= Lc; ++i) + { + if (mark[La][Lb][i] == cmark && dist[La][Lb][i] == ret) + { + ans += way[La][Lb][i]; + ans %= MOD; + } + } + cout << ans << endl; + string s = ""; + while (last.c != 0) + { + s += C[last.c - 1]; + last = prev_[last.a][last.b][last.c]; + } + reverse(s.begin(), s.end()); + cout << s << endl; +} + +int main() +{ + int T; + cin >> T; + for (int i = 1; i <= T; ++i) + { + printf("Case %d: ", i); + work(); + } + return 0; +} diff --git a/12442.cpp b/12442.cpp new file mode 100644 index 0000000..4a25470 --- /dev/null +++ b/12442.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +#define NN 50000 +#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) + +bool vis[NN]; +int n, nxt[NN], n_ord, ord[NN], n_scc, scc[NN], i, dp[NN]; +vector rnxt[NN]; + +void dfs1(int u) +{ + vis[u] = true; + int v = nxt[u]; + if (!vis[v]) + { + dfs1(v); + } + ord[n_ord++] = u; +} +void dfs2(int u) +{ + vis[u] = true; + rep(i, rnxt[u].size()) + { + int v = rnxt[u][i]; + if (!vis[v]) + { + dfs2(v); + } + } + scc[u] = n_scc; +} +void SCC() +{ + n_ord = 0; + rep(u, n) vis[u] = false; + rep(u, n) if (!vis[u]) + { + dfs1(u); + } + n_scc = 0; + rep(u, n) vis[u] = false; + for (int i = n - 1; i >= 0; i--) + { + int u = ord[i]; + if (!vis[u]) + { + dfs2(u), n_scc++; + } + } +} +int dfs(int u) +{ + int v = u; + dp[u] = 1; + while (1) + { + v = nxt[v]; + if (v == u || scc[u] != scc[v]) + { + break; + } + dp[u]++; + } + v = u; + while (1) + { + v = nxt[v]; + if (v == u || scc[u] != scc[v]) + { + break; + } + dp[v] = dp[u]; + } + if (u != v) + { + dp[u] += dfs(v); + } + return dp[u]; +} + +int main() +{ + int T0; + scanf("%d", &T0); + for (int T = 1; T <= T0; T++) + { + scanf("%d", &n); + rep(u, n) rnxt[u].clear(); + rep(i, n) + { + int u, v; + scanf("%d%d", &u, &v); + u--; + v--; + nxt[u] = v; + rnxt[v].push_back(u); + } + SCC(); + rep(u, n) dp[u] = -1; + for (int i = n - 1; i >= 0; i--) + { + int u = ord[i]; + if (dp[u] == -1) + { + dfs(u); + } + } + printf("Case %d: %ld\n", T, max_element(dp, dp + n) - dp + 1); + } + return 0; +} diff --git a/12443.cpp b/12443.cpp new file mode 100644 index 0000000..72cf6d7 --- /dev/null +++ b/12443.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int P_MAX=1000000,EMB_MAX=9000; +bool er[P_MAX+1]; +int emb[EMB_MAX]={78498,152,153,138,158,153,138,150,149,141,151,150,142,153,147,148,127,158,148,144,135,155,149,143,145,149,140,148,144,149,141,134,160,144,149,154,145,154,148,147,160,149,143,152,137,159,164,155,147,150,150,150,161,162,144,169,148,145,152,158,133,153,152,159,155,157,154,145,150,148,151,150,148,164,154,146,168,152,162,172,149,157,158,147,154,158,164,167,154,165,157,154,161,153,150,157,158,146,145,155,146,165,165,158,162,155,154,153,157,159,164,138,146,165,157,154,160,169,171,150,152,159,177,158,162,161,165,161,174,153,151,155,159,166,164,173,160,153,151,169,153,165,168,157,158,165,159,158,166,162,171,168,164,154,161,163,157,164,174,166,161,158,150,172,160,178,155,168,169,156,172,169,158,165,179,171,160,163,168,164,161,173,181,180,166,158,157,169,171,177,146,159,161,162,178,179,162,180,180,169,171,166,163,176,169,167,171,182,163,173,173,179,160,160,179,159,180,181,174,163,169,186,171,180,168,164,178,190,167,185,169,172,175,165,166,183,160,178,155,169,179,177,176,154,182,179,170,171,194,178,163,172,187,160,184,173,178,169,170,179,159,191,155,183,174,171,190,192,176,169,176,171,175,184,179,171,198,176,187,185,168,168,171,184,176,183,184,189,168,172,185,175,199,177,173,191,190,179,164,189,170,184,179,187,191,193,168,183,187,168,185,186,175,171,188,184,172,186,184,185,176,180,196,196,195,178,178,169,169,188,189,185,185,185,189,189,187,188,169,195,191,183,201,180,185,188,193,182,189,173,188,203,193,193,186,187,169,184,197,190,192,192,192,193,189,185,204,199,178,186,194,190,197,178,197,198,193,185,194,175,185,188,202,199,192,203,182,186,199,202,196,185,185,200,192,204,199,176,201,191,183,196,188,192,177,188,184,198,196,200,191,192,204,196,207,186,202,203,201,190,209,194,200,187,195,201,207,193,189,192,201,194,191,188,192,209,198,210,189,204,193,193,196,190,195,207,199,201,183,201,206,201,202,202,194,209,191,199,194,210,199,197,207,197,203,210,201,190,207,216,205,206,185,204,201,198,215,196,195,201,210,202,200,191,192,212,208,217,187,202,190,198,206,219,201,206,206,202,200,205,208,187,217,223,205,212,206,217,219,202,203,192,197,208,208,204,211,218,205,191,219,201,203,204,197,196,223,192,217,209,206,209,190,209,208,206,219,209,220,212,197,213,205,209,219,207,227,204,193,231,200,218,198,205,218,222,207,213,210,204,207,217,193,222,223,208,215,217,215,220,218,204,210,206,214,210,208,223,216,204,216,216,223,217,207,220,221,200,211,237,211,233,206,206,219,206,217,216,219,215,213,225,208,222,227,216,230,231,217,214,215,222,217,213,213,210,214,231,228,205,224,209,224,236,211,207,231,226,224,217,233,228,204,235,209,201,233,220,213,229,224,226,227,224,216,229,229,221,224,241,204,216,215,227,209,210,238,224,221,223,209,224,238,224,228,224,222,219,226,224,219,222,228,216,234,239,224,246,230,229,236,228,206,234,248,235,221,221,234,223,233,213,225,236,219,224,220,222,234,228,228,225,224,230,223,232,237,221,224,244,223,226,236,215,246,225,217,221,241,227,216,235,240,237,226,232,224,241,245,223,227,233,238,238,227,234,242,219,230,226,242,226,234,224,226,243,226,226,237,230,240,215,238,244,252,233,237,230,257,242,226,204,252,239,243,228,255,231,231,236,241,237,233,228,232,243,251,245,219,233,253,240,236,238,233,245,249,239,242,252,250,235,246,239,249,234,241,244,238,248,251,223,243,241,233,237,257,233,250,231,246,247,235,241,251,231,246,220,242,246,255,238,239,244,247,241,234,236,257,241,240,237,237,219,231,272,253,234,258,236,248,250,237,253,245,258,240,259,250,240,242,248,250,251,252,250,257,236,255,243,239,236,241,247,248,227,242,231,239,257,245,238,253,240,250,258,232,246,228,244,260,251,235,243,255,242,257,251,244,267,250,234,246,252,257,254,238,264,256,253,274,261,256,249,272,247,261,245,243,252,244,227,250,266,247,255,253,259,275,249,250,252,255,270,261,257,249,252,251,263,257,271,271,253,248,247,242,278,257,250,228,257,252,254,258,260,265,243,242,271,256,266,252,256,282,263,268,251,255,252,265,264,264,244,259,260,261,255,244,259,262,253,266,266,280,249,246,271,265,261,260,263,266,267,253,262,271,263,273,247,256,248,254,267,266,268,254,250,251,271,248,267,267,265,264,257,274,264,265,275,273,273,276,261,256,265,267,256,261,277,276,255,278,259,271,264,256,275,274,258,247,266,268,249,256,268,271,262,272,277,271,289,261,258,258,275,287,275,262,265,277,277,267,264,266,274,268,259,271,250,285,256,284,275,266,274,261,269,266,272,257,262,275,259,281,274,272,282,263,262,265,282,266,277,271,261,272,272,263,276,262,283,282,276,276,263,280,267,261,284,257,294,274,288,279,291,263,293,294,271,279,272,285,271,284,270,283,278,287,287,268,275,274,289,266,263,275,271,277,271,273,279,271,271,295,277,269,294,286,269,283,283,273,282,292,264,278,296,282,264,280,295,274,275,279,278,286,267,262,284,285,289,283,281,297,302,266,283,289,288,273,276,291,287,278,273,307,286,273,282,274,276,290,280,284,265,294,288,277,270,287,287,303,276,284,285,294,263,280,296,309,272,284,297,273,291,288,294,288,274,296,294,300,301,313,266,293,285,278,298,284,293,283,288,301,285,289,284,303,303,280,294,294,298,302,282,284,293,280,283,287,296,291,293,290,282,292,279,289,308,286,287,298,284,294,299,290,301,287,281,280,301,285,286,313,284,307,287,303,286,296,282,302,289,292,298,279,288,292,292,296,305,313,279,305,300,296,292,295,300,305,301,312,277,296,300,301,297,296,277,299,312,294,303,301,282,300,289,302,302,305,298,299,294,295,299,304,298,280,321,307,297,305,285,304,286,296,310,289,315,305,295,306,292,298,320,293,310,303,284,304,291,306,299,303,303,306,310,282,312,288,288,296,332,299,284,301,292,307,298,322,311,274,318,310,295,307,307,292,319,296,297,301,313,314,291,300,296,307,299,311,290,311,304,296,311,320,316,320,304,295,326,315,302,306,301,307,309,314,315,295,302,292,319,294,302,305,315,307,297,328,308,306,310,294,319,301,307,318,320,309,305,301,307,312,309,343,310,321,306,312,317,334,316,323,310,320,314,317,312,299,310,314,314,293,314,304,337,306,290,323,321,328,314,309,339,320,268,322,308,319,323,311,302,313,326,313,289,336,311,328,323,316,317,313,310,322,310,325,307,329,318,315,319,332,311,319,297,319,311,333,322,320,301,299,331,320,321,312,317,319,328,322,333,316,321,317,343,321,321,342,336,312,304,336,324,332,311,317,325,318,313,328,314,315,308,312,315,312,325,322,332,324,307,331,326,314,317,317,325,329,333,320,329,313,320,324,323,326,323,336,321,333,327,317,316,342,319,326,332,321,319,344,324,327,322,354,327,319,326,329,327,346,333,312,329,315,330,333,313,324,321,329,330,326,331,312,339,334,331,337,333,338,338,328,330,327,327,339,334,349,323,322,326,328,328,314,319,342,343,340,327,342,329,334,333,333,329,333,346,356,337,329,330,349,330,342,326,325,337,322,348,322,330,352,337,343,357,314,343,333,343,325,328,340,337,331,330,331,335,345,337,355,349,345,347,334,341,313,325,346,343,338,355,323,354,348,333,340,329,315,330,328,348,340,338,341,335,353,310,348,331,349,332,350,331,345,334,347,327,341,349,342,348,334,343,350,342,345,338,349,343,337,341,334,352,339,342,338,335,326,350,347,342,344,348,358,343,333,334,342,345,327,354,349,343,347,355,338,356,354,330,334,332,341,367,342,341,324,349,344,333,367,329,329,361,346,358,360,347,350,323,352,338,341,356,357,354,365,355,338,322,390,334,341,344,362,348,352,345,358,342,347,356,357,358,338,350,355,361,342,338,343,351,336,359,346,346,349,352,361,324,349,356,364,348,368,347,339,344,373,371,351,353,360,343,366,357,364,355,354,343,348,362,352,358,350,357,350,370,347,342,362,338,363,365,365,346,357,345,363,345,342,372,339,352,361,355,360,372,365,360,341,345,357,344,349,345,363,362,360,393,337,345,354,358,371,361,366,348,358,380,355,353,360,373,365,358,348,363,374,369,359,354,357,343,364,365,367,368,350,357,373,361,361,367,361,349,363,375,383,353,343,369,351,371,371,362,359,358,357,383,351,356,367,356,342,358,372,373,343,374,364,388,363,371,359,370,377,364,383,357,371,378,348,392,356,350,383,393,366,364,362,370,366,383,364,366,357,380,366,352,389,382,353,381,344,372,375,372,376,369,378,375,375,376,360,373,354,363,393,374,380,358,403,355,368,363,365,380,355,391,367,353,364,383,369,363,354,388,370,362,372,387,350,382,381,372,396,368,384,365,378,367,357,369,370,379,394,380,381,340,362,366,390,357,374,375,395,347,373,371,388,371,385,388,373,379,374,362,375,365,372,396,392,368,370,368,382,375,390,381,383,390,358,380,385,395,390,375,384,367,402,382,362,390,370,384,372,381,401,363,389,368,381,375,391,371,404,377,392,379,378,361,389,376,385,381,375,379,384,370,374,388,386,373,372,373,404,375,377,386,381,393,396,373,385,366,405,383,385,378,397,360,395,391,416,393,389,372,383,386,376,397,377,385,401,387,383,389,400,379,390,392,379,362,398,381,381,392,403,392,396,383,380,383,378,388,378,397,387,401,395,380,413,376,390,388,371,375,393,399,380,386,387,388,406,377,385,385,385,380,414,389,382,388,386,392,377,400,381,368,399,384,393,391,409,390,397,392,408,403,393,391,383,405,401,384,396,377,408,396,399,392,389,424,389,385,395,385,397,386,391,369,397,402,384,378,417,405,399,382,397,401,408,409,407,386,398,393,379,405,380,408,405,400,418,370,395,412,390,407,386,413,400,412,397,402,410,392,412,393,408,386,414,396,416,403,395,418,396,404,404,405,388,385,405,400,423,401,402,416,384,400,402,412,391,396,401,397,420,398,402,413,400,405,400,429,413,407,404,405,415,409,424,390,403,396,407,409,395,397,387,408,406,388,404,418,403,414,389,397,421,430,411,398,409,418,395,387,410,400,431,403,394,424,419,394,415,430,399,417,413,387,416,399,416,410,397,397,407,403,409,411,421,417,423,409,415,421,394,425,424,404,427,396,382,402,406,434,409,415,418,420,422,406,422,415,403,403,421,400,408,416,420,428,402,400,416,396,414,435,386,408,425,416,431,400,399,426,412,409,409,419,407,396,438,417,407,412,430,405,396,431,402,443,424,425,422,397,419,418,428,396,410,429,434,416,440,420,409,427,413,415,425,415,407,440,443,442,427,399,383,436,434,422,413,415,401,422,427,444,399,417,423,413,435,407,436,412,415,412,398,463,425,421,437,404,433,418,418,411,398,420,438,438,410,450,413,411,431,419,423,428,457,398,430,433,438,432,404,425,441,416,419,412,426,441,402,419,434,425,423,452,414,429,419,419,405,421,438,437,425,410,439,447,426,424,416,431,412,444,414,422,424,449,425,437,442,416,419,417,437,427,441,415,439,434,439,462,429,419,422,412,421,439,446,486,404,443,409,444,433,455,424,432,423,427,442,431,431,422,434,437,434,412,407,437,448,458,428,397,442,433,426,422,425,444,439,433,436,448,431,435,450,431,426,442,421,423,429,465,425,430,446,421,458,454,425,456,448,428,439,445,441,443,427,438,442,432,437,434,453,437,430,453,434,450,446,420,416,454,416,432,428,447,430,436,425,449,436,436,436,423,423,457,453,442,446,451,419,441,432,454,416,425,444,433,436,422,447,451,420,451,450,450,438,430,439,432,455,444,452,451,449,437,444,446,444,439,449,432,429,452,450,452,417,459,452,452,455,419,462,440,439,453,455,439,460,439,441,456,428,444,443,442,469,413,442,456,431,439,451,438,458,448,434,457,440,447,447,435,447,454,452,446,448,459,440,477,453,442,450,454,449,444,469,452,461,449,462,437,451,445,462,442,448,445,452,482,420,449,435,480,476,430,457,452,456,451,430,480,467,446,443,469,454,464,457,470,457,466,448,446,451,448,433,448,451,440,454,428,460,461,440,465,470,438,458,445,462,432,469,480,451,449,455,480,451,465,449,464,448,461,450,456,446,452,437,449,463,471,447,466,462,462,461,466,443,486,462,452,448,463,480,431,472,463,454,508,459,452,471,478,466,464,449,460,453,463,451,463,462,471,444,463,475,456,441,457,458,459,475,459,466,455,470,456,475,482,457,472,454,446,446,464,461,460,458,453,477,465,494,453,448,471,467,463,476,463,461,469,471,468,499,456,475,450,481,450,464,469,466,502,434,470,459,471,444,471,470,453,466,469,458,461,485,494,427,454,468,464,474,466,472,453,480,484,462,476,476,458,474,465,456,480,474,455,475,471,464,477,457,466,484,502,458,453,463,454,476,474,461,490,471,483,471,486,477,478,473,468,476,461,491,468,456,476,481,481,475,455,452,475,459,463,467,490,457,462,480,459,485,477,482,463,482,480,490,491,480,476,491,467,483,497,476,469,471,504,463,486,482,461,491,471,474,484,492,475,472,477,470,483,486,477,462,481,486,474,478,485,463,487,457,474,483,475,477,489,487,486,512,491,478,496,479,458,485,474,478,502,490,466,468,480,495,483,475,487,490,465,473,491,496,464,481,458,474,508,501,472,471,490,472,484,494,476,503,466,470,479,476,479,487,491,504,496,481,479,503,477,484,494,495,470,491,496,493,485,463,470,481,471,497,467,491,489,490,487,492,477,492,516,489,483,473,475,501,491,491,508,495,488,482,492,464,501,464,481,489,482,489,506,502,497,497,505,459,500,498,502,486,493,490,490,498,469,495,492,495,506,502,477,506,486,500,491,510,504,478,514,490,476,497,483,480,502,505,479,483,499,509,486,488,498,494,511,529,498,494,509,482,509,497,506,499,508,487,489,472,516,505,501,486,508,487,493,505,493,493,490,488,486,481,488,481,501,490,478,510,505,514,493,480,508,492,500,506,514,510,497,533,520,480,513,493,489,489,509,499,501,526,522,491,492,501,513,490,487,516,529,503,513,533,486,488,506,494,515,515,484,525,500,510,502,480,504,503,512,522,496,477,485,508,493,514,515,509,479,513,504,494,485,494,500,504,522,539,497,508,496,503,497,491,501,493,542,509,496,500,515,491,493,521,499,537,523,519,527,512,522,502,513,491,504,496,509,493,503,527,476,509,474,524,526,505,501,511,506,521,492,501,510,510,480,514,507,510,525,523,547,503,510,534,497,527,512,506,533,518,522,503,509,508,477,518,503,516,500,527,523,522,484,524,530,499,523,486,496,509,511,501,513,503,529,516,505,526,504,532,516,529,512,517,518,526,517,515,545,520,524,503,474,524,498,531,499,537,544,510,525,526,525,497,532,494,513,499,536,518,510,542,514,534,508,497,510,507,513,520,540,538,520,521,524,498,514,547,512,505,532,496,546,520,523,509,506,524,537,513,505,508,546,505,529,527,533,510,515,521,525,540,489,520,522,524,522,528,529,512,526,508,529,546,527,535,519,518,507,548,530,513,548,535,504,544,544,508,540,538,524,510,521,543,491,529,524,529,513,522,520,515,506,530,497,521,541,545,512,547,532,522,508,525,544,517,521,543,538,540,515,517,532,546,529,533,536,515,558,536,541,515,540,516,537,541,530,523,524,549,493,537,544,548,530,533,527,554,512,536,539,539,526,530,533,555,536,537,516,537,546,524,542,541,515,520,537,505,549,531,549,553,538,516,543,500,526,526,553,530,530,541,521,549,517,529,550,546,512,520,547,541,555,534,536,534,536,521,519,541,515,534,546,532,549,515,546,536,539,569,528,517,544,541,541,551,526,564,536,521,531,570,529,506,554,525,568,567,550,557,551,536,550,531,529,555,538,529,530,556,537,533,524,529,531,570,522,563,543,553,539,535,526,551,546,537,527,549,533,535,568,550,538,560,558,530,552,538,557,552,531,537,517,557,531,572,528,553,550,509,539,568,528,554,543,533,528,545,540,536,566,559,570,564,549,540,554,534,533,566,535,538,556,553,562,542,534,558,532,545,562,563,576,550,565,541,549,515,540,539,552,555,539,559,537,566,558,541,582,531,560,541,530,559,544,573,539,562,564,551,555,550,562,563,528,565,565,557,551,535,549,576,542,554,560,544,568,548,561,535,560,570,558,588,576,547,540,548,551,546,568,564,557,571,559,585,534,543,529,538,551,533,566,552,559,561,542,554,563,563,543,524,556,543,554,546,552,555,563,565,534,564,570,561,586,533,545,560,546,560,556,548,569,553,561,545,554,553,558,563,545,584,549,562,562,567,565,563,582,560,582,541,579,559,562,555,597,547,528,560,576,555,566,589,572,565,555,570,569,562,568,550,565,554,543,562,558,553,565,597,569,567,562,561,579,559,574,568,562,564,566,552,571,562,554,572,602,570,575,563,566,575,558,584,538,552,564,579,543,568,574,565,590,593,552,537,574,538,585,555,568,567,578,575,598,567,566,589,568,578,552,560,601,566,588,539,549,557,568,572,564,589,559,586,589,578,546,571,565,562,585,586,573,560,574,576,532,602,562,575,587,558,563,580,595,581,561,572,573,553,569,560,573,576,574,577,588,555,575,571,601,543,571,547,572,588,566,593,578,607,568,582,578,569,556,571,574,582,591,600,563,572,587,566,594,577,587,574,543,575,591,534,600,584,598,599,554,590,587,561,582,587,601,574,590,563,581,612,580,587,585,569,580,578,585,588,574,585,587,588,585,580,580,569,570,554,572,598,593,579,592,571,590,584,602,564,578,596,568,582,567,577,612,559,569,592,600,599,617,575,595,583,604,553,593,568,598,608,583,571,580,585,561,578,565,620,578,596,576,584,586,547,598,580,569,602,599,616,562,571,613,598,569,563,577,606,592,592,601,580,607,619,574,576,568,548,604,587,601,576,570,576,581,557,580,596,591,604,578,582,599,608,586,606,573,571,570,608,605,599,578,580,582,573,612,612,610,592,583,573,579,585,614,608,589,580,588,616,592,589,592,582,587,632,607,547,563,618,586,605,609,599,564,608,612,598,598,591,606,596,619,601,591,621,577,595,580,611,602,571,597,610,614,585,576,593,615,614,592,590,588,597,616,591,596,594,584,617,611,568,611,619,591,590,608,584,595,593,583,611,615,597,574,571,597,619,603,592,614,602,610,610,567,594,614,603,604,617,626,631,595,572,581,612,618,589,621,617,621,588,596,623,604,619,595,605,610,580,596,607,615,615,598,608,596,628,581,598,636,564,604,584,582,630,577,622,603,612,601,586,615,619,602,610,627,591,608,621,604,611,594,613,635,600,597,616,620,603,608,584,602,622,606,630,634,617,585,606,623,604,634,603,602,609,582,624,592,623,600,596,615,595,586,621,607,608,602,598,628,602,620,578,627,609,623,611,618,630,628,580,623,607,595,598,620,623,620,609,623,613,615,623,626,660,579,599,636,602,607,619,604,595,599,633,597,636,675,617,585,610,618,591,615,619,595,621,580,602,629,638,612,629,632,628,621,599,628,614,633,606,606,615,635,641,633,600,576,616,623,635,630,591,607,623,629,628,613,625,595,588,641,622,636,621,634,611,636,621,627,637,624,632,620,606,607,606,619,610,627,616,590,649,620,637,613,619,592,645,668,624,621,601,623,634,611,617,644,605,622,633,638,616,631,615,624,643,599,611,618,626,641,616,615,601,630,630,618,593,633,623,627,637,628,621,622,638,619,663,615,590,649,599,628,631,632,630,619,635,637,605,624,614,646,616,625,645,618,614,628,631,601,624,620,655,635,591,619,614,648,639,623,660,649,633,639,628,622,666,638,645,601,632,624,602,601,642,611,637,623,640,643,617,630,651,638,624,622,620,636,636,641,624,653,609,637,640,636,639,641,650,633,666,645,615,608,613,625,622,624,633,628,634,640,663,647,651,661,638,610,650,626,669,662,601,664,642,643,644,656,634,643,619,657,627,673,644,610,625,651,644,659,620,627,644,634,652,651,642,631,636,642,635,656,636,647,639,645,666,631,643,639,623,653,640,659,601,629,607,635,643,638,650,621,642,653,655,656,615,659,644,651,660,645,654,626,659,677,663,629,646,639,633,641,652,636,633,642,637,624,658,661,633,650,636,652,656,665,643,654,648,625,643,650,632,644,630,619,649,647,657,647,620,614,634,661,622,638,650,649,664,663,636,628,626,640,657,643,645,660,651,624,638,665,682,646,666,613,683,638,656,633,645,657,642,642,655,634,654,629,648,634,637,639,656,647,625,655,664,682,654,651,637,662,654,639,655,644,675,658,659,642,634,643,644,651,645,668,651,631,680,660,650,666,672,660,666,644,636,655,653,639,659,658,657,670,638,676,651,628,643,656,643,657,678,633,666,707,635,646,681,669,636,658,638,645,652,621,686,663,650,662,651,679,671,647,656,663,650,677,650,651,679,643,667,649,642,680,648,705,641,659,665,654,664,664,643,664,673,673,660,676,657,655,638,652,686,644,667,681,660,651,656,676,641,647,674,671,650,660,632,670,666,644,662,684,670,648,663,680,671,686,678,654,665,661,654,665,632,673,651,661,660,650,687,655,684,646,686,670,664,676,649,669,656,665,676,691,656,659,692,665,668,671,664,672,662,682,684,670,665,665,676,662,675,666,678,668,656,655,639,659,683,686,677,662,654,659,672,660,655,680,676,674,665,665,680,658,682,658,688,679,690,660,647,677,722,647,673,681,668,694,670,643,680,677,648,648,666,670,670,671,686,650,660,680,647,671,671,676,677,666,670,669,669,658,650,658,672,687,674,655,674,658,687,678,670,677,660,665,674,643,671,652,674,684,691,663,681,654,699,680,682,683,674,689,693,679,654,690,672,687,683,683,675,665,682,642,646,666,693,669,686,682,660,652,657,713,678,702,675,677,664,662,700,706,704,669,687,682,719,650,670,699,686,671,669,681,672,681,665,666,687,667,688,691,679,681,673,702,688,665,678,686,683,701,679,691,690,692,661,669,697,698,692,682,672,675,679,714,699,693,676,676,690,692,670,669,686,693,674,708,693,698,675,675,675,680,689,675,701,708,679,688,684,685,655,688,698,719,675,688,705,712,663,679,674,709,692,662,686,675,664,698,698,685,688,687,679,689,723,698,715,688,675,674,670,707,706,708,674,702,674,695,665,703,666,686,693,700,685,704,727,674,730,695,673,693,737,695,686,711,669,672,700,659,705,687,698,708,705,708,675,710,672,693,708,669,690,728,702,689,690,670,700,716,670,664,710,677,681,697,686,738,706,702,674,705,725,650,689,708,695,692,659,668,665,711,710,701,686,726,711,693,714,687,713,701,702,682,692,725,730,730,713,707,682,698,703,694,704,688,684,703,670,698,713,683,695,705,696,702,716,701,690,731,710,697,683,679,689,707,718,719,710,725,687,728,716,678,684,680,721,703,716,696,716,697,738,660,712,704,711,709,717,678,706,704,701,677,699,718,718,702,686,696,698,723,695,718,689,705,715,691,697,704,725,684,711,734,713,739,686,716,711,706,707,709,701,735,727,702,711,716,695,717,726,710,727,730,696,730,700,694,690,736,697,704,698,732,707,720,705,708,708,711,738,705,694,702,698,695,712,722,693,715,696,720,708,701,723,717,742,718,717,727,710,723,711,737,702,715,704,737,732,708,726,713,707,722,736,721,701,686,713,707,714,713,702,729,702,711,727,721,701,734,713,723,728,700,722,691,716,733,732,731,685,702,773,743,737,731,715,728,715,690,723,710,746,719,701,736,705,711,747,711,738,731,688,742,703,714,728,718,756,745,722,720,705,738,721,729,692,700,713,718,737,727,727,717,716,713,712,724,718,712,722,719,738,708,698,710,708,735,708,737,717,712,717,712,713,740,720,738,733,739,732,707,732,706,735,705,722,706,735,695,775,743,727,744,745,739,701,742,731,726,738,703,720,719,710,725,739,733,745,696,741,733,707,705,719,709,735,712,731,707,743,727,729,708,757,741,748,740,741,729,736,757,719,725,752,735,717,750,720,690,707,714,723,724,736,724,749,735,716,744,728,717,730,735,734,754,726,727,730,757,706,751,752,727,707,753,711,719,726,722,746,711,736,750,740,731,718,726,729,729,710,743,762,744,730,718,745,732,727,723,752,745,698,715,742,690,741,709,746,736,750,744,748,729,731,732,746,746,740,746,733,746,766,741,732,713,740,758,713,727,711,741,741,715,718,730,747,711,744,747,716,754,757,736,723,733,765,762,733,748,728,767,730,725,789,725,759,741,777,755,711,752,727,748,762,720,720,749,727,749,740,762,753,748,739,747,731,732,751,726,727,748,742,742,737,769,748,731,736,767,726,721,762,723,758,748,750,782,722,736,754,742,713,737,717,737,752,758,730,736,723,766,727,732,739,755,718,762,769,764,751,740,750,733,776,737,753,745,752,743,748,732,733,745,744,767,739,740,771,769,734,751,746,747,756,743,778,737,741,776,769,729,760,763,764,777,742,728,733,740,737,756,724,734,753,733,758,756,758,732,768,744,757,731,747,777,753,765,723,761,719,766,728,786,768,791,763,764,746,734,742,781,756,756,741,731,751,731,760,748,753,772,743,774,756,747,734,751,794,789,751,740,740,766,745,758,750,741,764,788,729,773,766,756,754,736,747,776,761,747,772,764,784,766,773,724,771,771,720,785,776,770,755,761,778,785,733,770,742,767,716,761,782,769,742,759,764,754,757,784,764,738,763,765,769,736,785,813,775,743,752,771,773,739,747,742,767,751,757,791,774,783,754,781,783,751,792,785,762,797,755,751,772,739,743,806,789,786,757,752,767,769,772,768,753,756,807,754,793,747,771,767,753,775,761,772,767,748,799,787,766,746,719,786,735,789,767,781,772,797,775,789,759,765,751,729,785,776,779,746,790,773,774,756,759,757,753,766,768,772,784,772,784,759,767,763,766,758,739,777,775,789,769,755,792,766,767,776,789,758,751,766,789,775,812,745,775,761,773,772,755,816,782,776,781,767,777,772,791,774,742,767,791,766,790,788,770,775,780,762,762,785,790,803,746,769,783,765,779,773,802,764,770,772,750,752,758,775,809,775,796,783,766,785,797,796,748,764,770,763,820,764,787,787,779,776,753,801,788,776,765,785,759,763,761,804,813,771,763,807,778,770,779,778,787,759,779,767,755,770,799,791,766,784,761,779,776,784,777,802,778,794,805,794,788,779,771,773,779,764,797,761,787,771,787,785,759,753,771,801,810,778,792,782,773,800,754,806,770,740,794,780,798,794,789,794,778,798,800,754,799,798,777,774,759,792,796,773,815,799,828,754,776,758,790,820,789,785,766,747,834,762,806,772,781,773,783,764,778,813,778,831,821,759,775,806,811,768,785,780,807,785,796,791,773,787,768,797,818,784,773,814,792,780,784,787,802,782,820,827,791,796,776,804,779,792,767,798,800,810,781,803,801,795,827,780,799,784,803,828,790,812,793,790,792,814,811,805,801,800,781,805,772,805,789,790,787,789,832,784,792,798,811,763,788,842,788,796,835,781,788,809,784,803,821,772,775,812,767,823,774,810,793,798,820,812,781,791,787,785,814,806,799,776,796,808,800,825,804,818,799,778,799,798,770,791,824,798,808,808,802,801,810,811,797,832,797,812,778,816,780,818,791,814,802,793,769,831,835,813,784,821,786,820,831,797,810,795,781,807,793,804,771,817,771,825,814,781,805,782,791,826,804,815,830,793,784,803,798,799,816,799,819,807,815,805,799,781,825,786,839,817,834,831,806,804,796,795,775,812,809,827,820,812,792,832,841,783,828,814,770,815,798,795,797,819,800,818,831,795,830,822,834,825,842,791,795,836,789,800,835,801,815,809,812,816,781,818,807,826,849,801,828,778,829,791,813,780,847,802,800,826,800,804,821,780,796,829,828,798,834,825,823,805,838,765,843,813,804,806,825,795,806,817,840,782,827,804,843,840,828,807,801,803,817,833,803,819,810,856,810,808,831,808,810,839,812,810,822,824,810,799,813,829,833,800,816,827,835,790,837,844,790,811,820,867,822,813,777,829,847,788,853,826,827,845,820,805,843,821,772,820,832,813,830,811,797,813,816,820,814,807,831,810,801,831,810,798,835,829,863,812,807,816,851,831,818,819,827,799,851,792,796,837,818,846,858,841,794,822,851,828,827,818,829,799,840,809,830,857,810,833,828,837,799,825,821,807,796,827,817,847,823,833,813,844,839,810,825,793,821,819,849,824,803,838,818,822,854,857,820,798,833,874,803,851,865,829,833,823,805,824,798,837,831,843,835,826,835,814,816,838,803,796,821,836,817,881,837,838,837,830,845,839,843,859,845,847,827,807,833,842,812,841,831,832,854,838,803,824,827,836,851,815,847,834,824,859,821,837,852,835,827,837,825,819,818,838,804,867,816,836,849,858,826,834,863,841,847,834,861,853,861,842,788,843,856,846,837,816,832,833,843,819,839,821,857,859,828,847,817,844,857,855,851,857,852,851,841,868,860,831,824,849,796,860,848,828,858,806,848,849,832,834,833,826,818,834,818,844,860,832,824,871,835,867,824,828,889,823,845,855,857,869,873,869,837,848,834,871,848,857,849,837,820,820,829,874,847,843,876,817,832,824,867,844,840,830,845,865,870,811,883,840,836,853,840,837,842,867,823,877,842,866,888,847,846,887,822,852,866,835,826,866,827,825,836,864,862,843,858,845,859,847,822,843,851,841,863,848,829,845,844,830,858,849,860,847,840,845,832,866,864,851,835,858,857,845,852,824,834,843,875,864,867,861,847,842,841,883,864,821,849,857,859,833,862,865,876,835,823,871,847,860,887,843,869,834,852,834,859,839,857,869,892,830,865,877,840,878,847,869,891,827,825,878,822,881,850,886,845,883,858,885,893,833,846,879,844,872,857,856,835,826,867,852,843,868,900,832,831,872,879,834,843,846,875,872,842,844,858,844,842,858,886,859,853,871,855,864,829,841,853,893,860,866,835,871,863,880,884,857,843,846,870,896,876,838,855,857,872,859,865,838,869,867,889,873,864,866,887,856,848,868,859,865,853,853,868,842,897,869,851,857,860,873,861,857,854,852,881,895,840,849,883,899,886,846,839,865,886,880,874,849,905,846,896,861,896,810,885,873,875,849,854,876,858,859,889,841,908,894,878,863,875,851,876,855,901,885,868,858,884,893,882,899,847,865,876,862,882,837,901,865,878,884,880,858,870,865,870,834,898,859,871,829,854,871,889,870,870,893,885,844,838,858,865,866,873,884,872,847,882,898,881,891,871,841,901,853,875,866,851,868,886,865,867,893,873,874,853,908,865,882,853,857,879,897,866,895,883,886,852,879,893,884,878,854,895,878,908,834,882,889,899,904,873,924,847,863,867,881,868,877,876,866,881,873,914,894,918,879,843,854,895,881,895,867,882,878,884,880,876,867,835,881,886,887,856,880,877,890,867,907,887,885,879,912,899,872,896,883,861,890,874,881,915,879,886,877,876,879,890,880,856,870,934,884,867,877,875,870,885,907,890,907,872,895,884,879,894,881,861,863,906,897,906,877,888,914,886,905,882,879,840,937,898,875,899,875,892,873,895,883,896,866,874,893,912,890,908,907,916,875,916,865,896,929,872,900,910,860,910,875,897,874,879,895,881,911,888,908,884,911,890,848,890,920,891,864,885,904,936,900,862,898,895,876,904,900,919,876,877,904,868,904,899,895,873,901,854,885,883,851,901,898,908,897,896,891,858,897,863,891,899,894,904,886,875,892,933,895,878,911,874,917,886,902,924,933,859,910,893,877,873,895,890,895,874,907,861,940,862,908,918,910,900,895,920,867,877,869,942,927,902,925,944,893,885,896,883,891,907,887,897,873,908,885,888,906,924,880,896,896,899,885,909,920,894,922,910,863,912,930,897,881,933,911,916,913,883,939,937,882,909,887,907,889,892,930,929,904,908,933,889,873,904,962,918,913,889,911,944,895,899,901,901,897,900,929,904,902,896,902,890,941,920,898,894,944,899,922,899,889,904,894,920,899,896,890,890,935,931,921,861,922,907,901,891,923,909,904,907,894,917,899,938,869,915,892,917,920,933,892,906,939,894,951,900,935,924,882,915,910,918,926,914,920,940,900,913,909,903,915,904,944,912,919,862,930,921,915,903,922,930,933,904,872,920,921,893,926,933,891,922,926,933,913,929,894,910,923,904,905,939,904,910,897,931,901,908,919,918,912,920,922,920,909,918,921,893,915,917,893,915,926,926,898,920,933,931,928,907,887,905,937,922,918,891,938,911,929,884,928,938,916,887,946,900,920,881,940,912,937,942,903,900,899,928,917,937,945,937,876,935,926,919,910,921,943,884,922,919,913,931,884,918,889,935,935,919,942,934,927,932,968,914,920,942,944,909,929,924,930,928,954,919,898,918,933,934,944,918,954,906,911,971,914,922,919,909,952,919,921,907,905,927,925,932,901,905,919,936,923,933,906,921,952,910,923,914,954,937,916,912,930,959,930,929,938,931,913,951,915,927,952,931,956,903,944,960,943,889,933,947,938,912,973,897,864,943,935,913,919,951,933,953,866,932,930,920,915,947,945,914,910,937,967,931,935,918,940,937,891,946,931,953,916,958,962,891,906,941,942,949,923,935,950,927,937,905,944,937,938,923,968,966,960,947,943,876,972,945,939,928,943,956,950,908,939,933,947,912,905,940,943,879,920,944,958,929,958,945,927,938,955,910,939,936,912,946,923,977,924,955,971,929,954,933,942,951,965,983,944,927,942,948,980,935,942,927,938,938,940,949,919,938,938,931,930,929,979,942,953,925,913,953,907,946,934,975,935,962,925,927,919,930,981,948,952,950,942,947,957,941,951,940,940,975,916,959,960,928,955,941,930,931,941,915,928,948,948,949,943,956,928,963,946,949,934,964,954,920,915,955,915,962,948,951,952,949,974,966,957,914,954,948,968,957,966,957,921,943,936,950,949,948,942,961,942,966,953,955,930,983,925,949,934,941,951,922,901,932,994,928,948,944,939,958,982,933,937,941,950,943,955,950,958,973,940,963,935,969,960,935,913,961,948,960,916,965,968,998,916,952,952,936,964,958,964,939,976,964,955,952,953,937,921,984,988,956,962,927,955,947,955,911,949,958,989,965,963,941,951,978,949,977,998,942,961,964,948,966,964,959,962,938,982,980,959,971,962,937,959,978,944,940,954,964,930,968,970,969,965,947,962,966,919,993,964,980,979,933,993,940,991,966,964,942,942,1002,982,960,980,932,966,954,955,946,968,974,959,958,953,970,942,948,977,936,944,970,932,969,986,984,935,979,974,939,962,938,920,982,988,981,951,936,950,988,944,950,952,958,962,942,952,961,991,946,1008,956,956,960,980,962,954,973,995,963,986,990,959,966,939,975,974,958,977,961,1002,962,971,964,978,938,976,934,968,966,999,974,950,976,959,973,959,954,981,951,981,970,951,987,972,953,971,954,964,969,949,985,1005,953,965,965,979,962,945,1000,973,978,999,984,947,978,966,958,980,981,983,958,962,990,994,965,958,1004,956,962,989,952,954,942,989,963,942,992,974,987,957,933,979,973,951,955,1000,987,994,975,990,955,981,958,991,987,954,972,985,1014,978,973,1002,944,970,950,959,999,965,977,954,1014,967,966,959,1012,962,988,1007,989,959,984,973,976,979,983,967,994,962,959,952,996,969,959,990,1000,980,1006,979,976,985,968,1014,941,986,973,991,945,1005,1022,995,960,954,991,1006,983,925,991,1009,954,967,983,1005,997,971,970,988,1000,976,945,935,979,989,967,1017,1006,998,968,979,975,959,984,962,1017,1014,992,1012,981,977,969,970,1001,1006,998,964,1012,980,996,998,970,958,974,999,988,965,997,981,986,975,996,964,1016,1024,990,978,982,981,1017,1031,967,991,980,995,992,1011,976,1048,998,981,1001,999,1011,1007,990,1005,979,984,983,974,1014,1002,964,969,985,1007,969,1006,989,1012,995,1003,1018,995,966,981,961,1009,975,1018,973,1013,968,991,1018,973,983,978,1006,977,973,1007,987,1024,979,984,1015,974,1011,994,980,1005,1024,1009,1012,974,1030,974,972,1029,983,972,1010,937,992,978,979,985,1004,994,1000,1003,983,1002,974,1010,1017,973,1022,954,1016,997,1010,997,1008,982,972,1026,1002,999,1038,1001,988,962,973,1054,1001,1026,1005,1024,1007,986,987,1012,998,979,1011,1000,1023,988,970,994,1000,986,984,986,1004,998,970,980,990,1014,1000,1032,967,1017,986,1000,980,1023,993,1005,990,1005,1015,1006,982,1021,997,1014,1001,1003,938,1012,1005,995,1004,1035,1025,1009,963,1027,1010,1024,994,1029,988,1051,1013,1054,1003,1006,994,1022,974,984,977,978,1020,1002,998,992,999,989,994,1017,983,1024,1014,1024,990,1010,1014,982,1001,1005,1046,1006,1048,1009,984,987,1015,984,1029,951,1013,1005,1007,994,1016,1023,1011,1005,1027,993,990,1011,1026,992,987,1006,977,1044,1036,988,1006,971,987,1021,1008,1039,963,1038,1010,1015,981,1018,987,1020,1043,978,1014,1041,1038,988,1007,1024,1039,1019,1042,995,1004,1026,1046,997,997,997,1002,1006,1026,1016,1023,1002,978,1028,1030,1015,1000,1052,980,1037,1004,1019,1022,1021,1004,998,1039,1006,1000,1019,1033,1008,1048,1015,1022,995,985,1016,1025,1018,987,1021,985,1054,1024,1012,984,1033,1006,1038,998,1040,1029,1030,1021,1016,1059,1008,1018,994,1019,1012,1066,976,1028,1020,1003,1017,1021,1022,1040,1012,1013,1003,1023,1048,1028,1008,1016,993,1020,1020,1032,1042,1012,1018,1035,1012,971,1021,1042,1035,1007,1039,1024,1015,1020,1045,1010,1032,1004,1015,1019,980,990,1032,1025,1033,995,1065,1016,1033,1019,1005,1043,1036,1013,1023,1001,1033,1003,1015,1021,1027,1034,1021,1018,1044,1007,1046,1027,1024,1031,1017,1049,1032,1012,1057,1032,1059,1023,1030,1002,999,1075,988,1040,1055,1077,1026,1026,1053,1026,1007,1026,1007,1025,1056,1040,1017,992,1022,1000,1010,1003,1007,1018,1033,1045,1061,1018,1037,988,1040,1041,1017,1033,1026,1053,1045,1025,1046,1047,1041,1024,1067,1019,1009,1049,996,1017,1038,1044,1025,1060,998,1038,1075,1040,1011,1032,1057,1043,1023,1046,1003,1060,1014,989,1029,1056,1034,1011,1021,1020,1033,1047,1012,1028,1021,1033,1046,1022,990,1040,1012,1013,1024,1032,1011,1011,1031,1036,1055,1047,1018,1053,1034,1001,1028,1045,1045,1053,1045,1014,1078,1043,1041,1027,1062,1047,1061,1013,1018,1068,1008,1054,1068,1047,1059,1038,1015,1051,1035,1015,1071,1062,1038,1045,1028,1005,1048,1024,1020,1060,1029,1010,1039,1064,1053,1001,1068,1053,1039,1038,1061,995,1049,1049,1042,1083,1069,1025,1004,1038,1039,1052,1025,1054,1011,1039,1043,1039,1058,1076,1040,1061,1048,1067,1064,1046,1015,1039,1051,1031,1027,1033,1038,1036,1059,1050,1022,1069,1019,1032,1063,1050,1024,1050,1076,1069,1071,1033,1039,1030,1036,1033,1005,1042,1040,1041,1049,1013,1083,1083,1024,1043,1019,1069,1041,1057,1049,1059,1066,1024,1031,1078,1045,1026,1040,1043,1039,1034,1052,1013,1057,1060,1048,1033,1066,1050,1048,1052,1038,1045,1072,1035,1018,1083,1057,1035,1051,1076,1051,1055,1070,1022,1066,1064,1062,1073,1005,1048,1074,1059,1028,1057,1009,1021,1031,1010,1078,1029,1069,1075,1002,1078,1068,1063,1035,1053,1044,1045,1034,1059,1073,1076,1080,1044,1033,1046,1049,1084,1049,1061,1069,1018,1043,1057,1032,1071,1054,1040,1051,1031,1047,1056,1069,1033,1032,1101,1043,1044,1072,1061,1091,1065,1018,1072,1046,1053,1104,1044,1100,1065,1073,1019,1030,1089,1015,1072,1070,1062,1037,1054,1050,1067,1048,1049,1065,1053,1057,1056,1047,1081,1048,1096,1050,1057,1018,1082,1068,1063,1045,1056,1078,1035,1059,1081,1058,1046,1081,1099,1079,1074,1065,1081,1072,1086,1065,1071,1061,1069,1011,1072,1064,1036,1083,1034,1062,1061,1081,1078,1062,1039,1072,1072,1078,1024,1067,1048,1078,1069,1094,1061,1086,1076,1050,1095,1062,1057,1054,1056,1052,1059,1049,1089,1025,1054,1047,1061,1077,1048,1085,1081,1083,1068,1080,1075,1050,1082,1090,1066,1039,1070,1044,1051,1069,1076,1058,1075,1100,1035,1015,1058,1073,1093,1056,1060,1068,1066,1051,1102,1085,1064,1044,1076,1057,1055,1042,1076,1099,1066,1082,1098,1083,1078,1080,1083,1051,1025,1099,1070,1085,1088,1046,1069,1071,1037,1072,1073,1089,1087,1103,1054,1072,1092,1044,1066,1076,1047,1085,1051,1025,1042,1068,1117,1062,1083,1076,1058,1061,1079,1075,1089,1058,1080,1111,1075,1114,1077,1106,1094,1077,1077,1062,1079,1087,1053,1057,1097,1071,1060,1104,1069,1117,1050,1056,1062,1113,1128,1043,1080,1067,1046,1072,1026,1101,1074,1057,1063,1073,1045,1080,1081,1116,1057,1084,1109,1049,1060,1083,1093,1090,1057,1091,1096,1087,1065,1076,1107,1100,1072,1098,1058,1103,1052,1103,1038,1066,1081,1067,1060,1028,1077,1095,1118,1045,1082,1068,1060,1066,1046,1068,1095,1117,1117,1075,1093,1058,1063,1101,1083,1079,1058,1041,1111,1056,1046,1080,1052,1125,1095,1083,1083,1066,1049,1092,1123,1084,1069,1076,1094,1100,1093,1168,1075,1079,1064,1062,1095,1073,1053,1100,1049,1098,1086,1078,1107,1090,1089,1094,1087,1105}; +void sieve() +{ + for (int i=0;i= 2; + } + for (int i=2;i*i<=P_MAX;i++) + { + if (er[i]) + { + for (int j=i*i;j<=P_MAX;j+=i) + { + er[j]=false; + } + } + } +} +int main() +{ + sieve(); + int np[10000]; + int sum=0,sq=1; + for (int i=0;i + +using namespace std; + +int a[40]; +int main() +{ + int t; + cin >> t; + int c, d; + while (t--) + { + cin >> c >> d; + bool re = 1; + ; + int len = 0; + while (c || d) + { + if ((d & 1) == 1 && (c & 1) == 1) + { + a[len] = 1; + } + else if ((d & 1) == 1 && (c & 1) == 0) + { + a[len] = 2; + } + else if ((d & 1) == 0 && (c & 1) == 1) + { + re = 0; + break; + } + else if ((d & 1) == 0 && (c & 1) == 0) + { + a[len] = 0; + } + len++; + c = c >> 1; + d = d >> 1; + } + if (re == 0) + { + cout << "-1\n"; + } + else + { + bool first = 0; + int aa = 0, bb = 0; + for (int i = len - 1; i >= 0; i--) + if (a[i] == 1) + { + aa = aa * 2 + 1; + bb = bb * 2 + 1; + } + else if (a[i] == 0) + { + aa *= 2; + bb *= 2; + } + else + { + if (first == 0) + { + aa = aa * 2 + 1; + bb = bb * 2; + first = 1; + } + else + { + aa = aa * 2; + bb = bb * 2 + 1; + } + } + cout << bb << " " << aa << "\n"; + } + } + return 0; +} diff --git a/12445.cpp b/12445.cpp new file mode 100644 index 0000000..ed8e79a --- /dev/null +++ b/12445.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +typedef long long LL; + +int hd, tl; +LL que[2002000]; +map fwd; + +LL encode(int *v) +{ + LL key = 0; + for (int i = 0; i < 12; ++i) + { + key = (key << 4) + v[i]; + } + return key; +} + +inline LL l_dn(LL key) +{ + return (((key & (0xfffffLL << 24)) << 4) + ((key & 0x0f) << 24) + (key >> 44) + (key & (0xfffff << 4))); +} + +inline LL l_up(LL key) +{ + return (((key & (0xfffffLL << 28)) >> 4) + ((key & (0x0fLL << 24)) >> 24) + ((key & 0x0f) << 44) + (key & (0xfffff << 4))); +} + +inline LL r_dn(LL key) +{ + return ((key & (0xfffffLL << 28)) + ((key & 0x0f) << 24) + ((key & 0xffffff0) >> 4)); +} + +inline LL r_up(LL key) +{ + return ((key & (0xfffffLL << 28)) + ((key & (0x0fLL << 24)) >> 24) + ((key & 0xffffff) << 4)); +} + +inline LL a_dn(LL key) +{ + return ((key >> 4) + ((key & 0x0f) << 44)); +} + +inline LL a_up(LL key) +{ + return (((key & ((1LL << 44) - 1)) << 4) + ((key & (0x0fLL << 44)) >> 44)); +} + +LL decode(LL key, int *v) +{ + for (int i = 12; i-- > 0; key >>= 4) + { + v[i] = key & 0x0f; + } +} + +void dump(LL key) +{ + int v[32]; + decode(key, v); + for (int i = 0; i < 12; ++i) + { + printf("%d ", v[i]); + } + puts(""); +} + +inline void check(LL key, int t) +{ + if (!fwd.count(key)) + { + fwd[key] = t; + if (t < 10) + { + que[tl++] = key; + } + } +} + +void bfs_fore(void) +{ + int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + hd = tl = 0; + que[tl++] = encode(v); + fwd[que[hd]] = 0; + for (int t = 1; hd < tl; ++t) + { + int tl1 = tl; + while (hd < tl1) + { + LL curr = que[hd++]; + check(l_dn(curr), t); + check(l_up(curr), t); + check(r_dn(curr), t); + check(r_up(curr), t); + check(a_dn(curr), t); + check(a_up(curr), t); + } + } +} + +inline int look_up(LL key, int t, map &backward) +{ + if (fwd.count(key)) + { + return fwd[key]; + } + if (!backward.count(key)) + { + backward[key] = t; + if (t < 11) + { + que[tl++] = key; + } + } + return -1; +} + +int bfs_back(LL key) +{ + map backward; + hd = tl = 0; + que[tl++] = key; + backward[que[hd]] = 0; + for (int t = 1; hd < tl; ++t) + { + int tl1 = tl, x; + while (hd < tl1) + { + LL curr = que[hd++]; + if ((x = look_up(l_dn(curr), t, backward)) >= 0) + { + return (x + t); + } + if ((x = look_up(l_up(curr), t, backward)) >= 0) + { + return (x + t); + } + if ((x = look_up(r_dn(curr), t, backward)) >= 0) + { + return (x + t); + } + if ((x = look_up(r_up(curr), t, backward)) >= 0) + { + return (x + t); + } + if ((x = look_up(a_dn(curr), t, backward)) >= 0) + { + return (x + t); + } + if ((x = look_up(a_up(curr), t, backward)) >= 0) + { + return (x + t); + } + } + } + return -1; +} + +int main() +{ + bfs_fore(); + int T; + scanf("%d", &T); + while (T-- > 0) + { + int v[32]; + for (int i = 0; i < 12; ++i) + { + scanf("%d", &v[i]); + } + LL key = encode(v); + printf("%d\n", fwd.count(key) ? fwd[key] : bfs_back(key)); + } + return 0; +} diff --git a/12446.cpp b/12446.cpp new file mode 100644 index 0000000..d63206b --- /dev/null +++ b/12446.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const long long P = 1000000007; +long long dp[1000010]; +long long re[1000010]; +long long re42[1000010]; +int main() +{ + dp[0] = 1; + dp[1] = 2; + dp[2] = 9; + dp[3] = 32; + re[0] = 1; + re[1] = 3; + re[2] = 12; + re[3] = 44; + for (int i = 4; i < 1000004; i++) + { + dp[i] = (dp[i - 1] * 2 + dp[i - 2] * 5 + re[i - 3] * 4) % P; + re[i] = (re[i - 1] + dp[i]) % P; + } + int t; + cin >> t; + while (t--) + { + int n; + scanf("%d", &n); + cout << dp[n] << "\n"; + //printf("%I64d\n",dp[n]); + } + return 0; +} diff --git a/12447.cpp b/12447.cpp new file mode 100644 index 0000000..5e2a9a3 --- /dev/null +++ b/12447.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int code[30]; +int code2[30]; + +void gray2(int len, int n) +{ + memset(code, 0, sizeof(code)); + for (int i = 0; i < len; i++) + if ((n & (1 << i)) != 0) + { + code[i] = 1; + } + code2[len - 1] = code[len - 1]; + for (int i = len - 2; i >= 0; i--) + { + code2[i] = code[i] ^ code[i + 1]; + } +} + +int main() +{ + int n, t; + cin >> t; + while (t--) + { + cin >> n; + for (int i = 0; i < (1 << n); i++) + { + gray2(n, i); + int re = 0; + if (i % 2 == 0) + for (int j = n - 1; j >= 0; j--) + { + code2[j] = 1 - code2[j]; + } + //for(int j = n-1;j >= 0;j--) cout << code2[j]; + for (int j = n - 1; j >= 0; j--) + { + re = re * 2 + code2[j]; + } + printf("%d\n", re); + } + } + return 0; +} diff --git a/12448.cpp b/12448.cpp new file mode 100644 index 0000000..5e1c857 --- /dev/null +++ b/12448.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +struct note +{ + long long a, b; +} dp[200][200]; + +long long gcd(long long a, long long b) +{ + while (b) + { + long long t = a % b; + a = b; + b = t; + } + return a; +} + +note add(note a, note b) +{ + note re; + re.b = a.b * b.b; + re.a = a.a * b.b + b.a * a.b; + long long d = gcd(re.a, re.b); + re.a = re.a / d; + re.b = re.b / d; + return re; +} +note mul(note a, note b) +{ + note re; + re.a = a.a * b.a; + re.b = a.b * b.b; + long long d = gcd(re.a, re.b); + re.a = re.a / d; + re.b = re.b / d; + return re; +} +int main() +{ + //cout << gcd(1,0) << " " << gcd(0,1); + int t; + cin >> t; + while (t--) + { + int n, m, k; + n = 140; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + dp[i][j].a = 0; + dp[i][j].b = 1; + } + } + dp[0][0].a = 1; + dp[0][0].b = 1; + cin >> n >> m >> k; + for (int i = 1; i <= m; i++) + { + for (int j = 0; j <= k; j++) + { + note no; + no.a = j; + no.b = n; + note yes; + yes.a = n - j + 1; + yes.b = n; + if (j != 0) + { + dp[i][j] = add(mul(dp[i - 1][j], no), mul(dp[i - 1][j - 1], yes)); + } + else + { + dp[i][j] = mul(dp[i - 1][j], no); + } + } + } + if (dp[m][k].a == 0) + { + cout << "0\n"; + } + else if (dp[m][k].b == 1) + { + cout << dp[m][k].a << "\n"; + } + else + { + cout << dp[m][k].a << "/" << dp[m][k].b << "\n"; + } + } + return 0; +} diff --git a/12449.cpp b/12449.cpp new file mode 100644 index 0000000..2a173b2 --- /dev/null +++ b/12449.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +const int N = 105; + +char g[N][N]; +bool u[N][N]; + +bool ok(char c) +{ + return c == '#' || c == '.'; +} + +int main() +{ + int T, m, n, r, i, j, cnt; + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &m, &n, &cnt); + for (i = 0; i < m; i++) + scanf("%s", g[i]); + r = 0; + memset(u, true, sizeof(u)); + for (i = 0; i < n; i++) + u[0][i] = u[m - 1][i] = false; + for (i = 0; i < m; i++) + u[i][0] = u[i][n - 1] = false; + for (i = 1; i < m - 1; i++) + { + for (j = 1; j < n - 1; j++) + { + int flag = 0; + if (j < n - 2) + { + if (g[i][j] == '.' && g[i][j + 1] == '.' && u[i][j] && u[i][j + 1]) + { + if (g[i][j - 1] == '+' || g[i][j - 1] == '-' || g[i][j - 1] == '|') + flag = 1; + if (g[i][j + 2] == '+' || g[i][j + 2] == '-' || g[i][j + 2] == '|') + flag = 1; + if (!flag) + { + if (ok(g[i - 1][j]) && ok(g[i - 1][j + 1]) && g[i + 1][j] == '-' && g[i + 1][j + 1] == '-') + { + r++; + u[i][j] = u[i][j + 1] = u[i][j - 1] = u[i][j + 2] = false; + } + else if (ok(g[i + 1][j]) && ok(g[i + 1][j + 1]) && g[i - 1][j] == '-' && g[i - 1][j + 1] == '-') + { + r++; + u[i][j] = u[i][j + 1] = u[i][j - 1] = u[i][j + 2] = false; + } + } + } + } + flag = 0; + if (i < m - 2) + { + if (g[i][j] == '.' && g[i + 1][j] == '.' && u[i + 1][j] && u[i][j]) + { + if (g[i - 1][j] == '+' || g[i - 1][j] == '-' || g[i - 1][j] == '|') + flag = 1; + if (g[i - 1][j] == '+' || g[i + 2][j] == '-' || g[i + 2][j] == '|') + flag = 1; + if (!flag) + { + if (ok(g[i][j - 1]) && ok(g[i + 1][j - 1]) && g[i][j + 1] == '|' && g[i + 1][j + 1] == '|') + { + r++; + u[i][j] = u[i + 1][j] = u[i + 2][j] = u[i - 1][j] = false; + } + if (ok(g[i][j + 1]) && ok(g[i + 1][j + 1]) && g[i][j - 1] == '|' && g[i + 1][j - 1] == '|') + { + r++; + u[i][j] = u[i + 1][j] = u[i + 2][j] = u[i - 1][j] = false; + } + } + } + } + } + } + + if (r >= cnt) + puts("LOCATION OKAY"); + else + puts("CHOOSE ANOTHER LOCATION"); + } + return 0; +} diff --git a/1245.cpp b/1245.cpp new file mode 100644 index 0000000..68e0a92 --- /dev/null +++ b/1245.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define mod 10007 +#define mod1 10006 +#define be 20000 +#define maxl 1000000000 +#define maxn 1100 + +typedef long long ll; + +vector> v[maxn][maxn]; +int f[maxn][maxn], maxr[maxn], maxc[maxn], maxy[maxn], maxx[maxn]; + +void update(int x, int y, int data) +{ + maxr[x] = max(maxr[x], data); + maxc[y] = max(maxc[y], data); + if (x >= y) + maxx[x - y] = max(maxx[x - y], data); + else + maxy[y - x] = max(maxy[y - x], data); +} + +int get(int x, int y) +{ + int re = max(maxr[x], maxc[y]); + if (x >= y) + re = max(re, maxx[x - y]); + else + re = max(re, maxy[y - x]); + return re + 1; +} + +void solve() +{ + int n, m, p; + scanf("%d%d%d", &n, &m, &p); + if (v[n][m].size() > 0) + printf("Sita %d\n", f[n][m] + p * 2); + else + printf("Gita %d\n", f[n][m] + p * 2); +} + +int main() +{ + int n = 1000, m = 1000, i, j, k; + for (i = 0; i <= n; ++i) + for (j = 0; j <= m; ++j) + v[i][j].clear(); + for (i = 0; i <= n; ++i) + for (j = 0; j <= m; ++j) + if (v[i][j].size() == 0) + { + for (k = i + 1; k <= n; ++k) + v[k][j].push_back(make_pair(i, j)); + for (k = j + 1; k <= m; ++k) + v[i][k].push_back(make_pair(i, j)); + for (k = 1; i + k <= n && j + k <= m; ++k) + v[i + k][j + k].push_back(make_pair(i, j)); + } + for (i = 0; i <= n; ++i) + maxr[i] = -1; + for (i = 0; i <= m; ++i) + maxc[i] = -1; + for (i = 0; i <= max(n, m); ++i) + { + maxx[i] = -1; + maxy[i] = -1; + } + for (i = 0; i <= n; ++i) + for (j = 0; j <= m; ++j) + if (v[i][j].size() > 0) + { + f[i][j] = maxl; + for (k = 0; k < v[i][j].size(); ++k) + { + pair p = v[i][j][k]; + f[i][j] = min(f[i][j], f[p.first][p.second] + 1); + update(i, j, f[i][j]); + } + } + else + { + f[i][j] = get(i, j); + } + int t; + scanf("%d", &t); + while (t-- > 0) + solve(); + return 0; +} diff --git a/12450.cpp b/12450.cpp new file mode 100644 index 0000000..1d76a33 --- /dev/null +++ b/12450.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +struct play +{ + char c[22]; + int mat; +} p[1111]; + +bool cmp1(play a, play b) +{ + return a.mat > b.mat; +} + +bool cmp2(play a, play b) +{ + return strcmp(a.c, b.c) < 0; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + int n; + scanf("%d", &n); + n = 1 << n; + for (int i = 0; i < n; i++) + { + memset(p[i].c, 0, sizeof(p[i].c)); + scanf("%s %d", p[i].c, &p[i].mat); + } + sort(p, p + n, cmp1); + printf("%s\n%s\n", p[0].c, p[1].c); + int nn = 2; + int now = 2; + while (now < n) + { + sort(p + now, p + now + nn, cmp2); + now += nn; + nn *= 2; + } + for (int i = 2; i < n; i++) + { + printf("%s\n", p[i].c); + } + } + return 0; +} diff --git a/12451.cpp b/12451.cpp new file mode 100644 index 0000000..4b2a106 --- /dev/null +++ b/12451.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +char str[200], dis[200]; +int dp[200], nxt[200]; + +void get_nxt(int n) +{ + memset(nxt, 0, sizeof(nxt)); + int i, j = -1; + nxt[0] = -1; + for (i = 1; i <= n; i++) + { + while (j > -1 && dis[j + 1] != dis[i]) + j = nxt[j]; + if (dis[j + 1] == dis[i]) + j++; + nxt[i] = j; + } +} + +int get(int start, int end) +{ + int n = end - start; + for (int i = 0; i < n; i++) + dis[i] = str[start + i]; + dis[n] = '\0'; + get_nxt(n); + int l = (n - 1) - nxt[n - 1]; + if (n % l == 0) + return n / l; + else + return 1; +} + +int getNum(int t) +{ + int re = 0; + while (t) + { + re++; + t = t / 10; + } + return re; +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%s", str); + int len = strlen(str); + dp[0] = 0; + dp[1] = 1; + for (int i = 2; i <= len; i++) + { + dp[i] = 1212000; + for (int j = 0; j < i; j++) + { + int t = get(j, i); + int len = i - j; + int ll = len / t; + if (len < ll + 2 + getNum(t)) + { + dp[i] = min(dp[i], dp[j] + len); + } + else + { + dp[i] = min(dp[i], dp[j] + ll + 2 + getNum(t)); + } + } + } + printf("%d\n", dp[len]); + } + return 0; +} diff --git a/12452.cpp b/12452.cpp new file mode 100644 index 0000000..cf92975 --- /dev/null +++ b/12452.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +int dp[10005][2], parent[10005]; +vector e[10005]; + +// dp[current_node][is_edge_covered] +void dfs(int v) +{ + int i, j, all, tmp, tmp2; + for (i = 0; i < e[v].size(); i++) + { + if (e[v][i] != parent[v]) + { + parent[e[v][i]] = v; + dfs(e[v][i]); + } + } + + // compute dp[v][0] and/or dp[v][1] + for (i = all = 0; i < e[v].size(); i++) + if (e[v][i] != parent[v]) + all += dp[e[v][i]][1]; + + dp[v][0] = min(dp[v][0], all); + // know 100, cover parent -> compute dp[v][1] + dp[v][1] = min(dp[v][1], all + 100); + // know 100, don't cover parent -> compute dp[v][0] + for (i = tmp = 0; i < e[v].size(); i++) + if (e[v][i] != parent[v]) + tmp = max(tmp, dp[e[v][i]][1] - dp[e[v][i]][0]); + dp[v][0] = min(dp[v][0], all - tmp + 100); + + // know 175, cover parent -> compute dp[v][1] + dp[v][1] = min(dp[v][1], all - tmp + 175); + // know 175, don't cover parent -> compute dp[v][0] + for (i = tmp = tmp2 = 0; i < e[v].size(); i++) + { + if (e[v][i] != parent[v]) + { + j = dp[e[v][i]][1] - dp[e[v][i]][0]; + if (j >= tmp) + { + tmp2 = tmp; + tmp = j; + } + else if (j > tmp2) + { + tmp2 = j; + } + } + } + dp[v][0] = min(dp[v][0], all - tmp - tmp2 + 175); + + // know 500 + for (i = all = 0; i < e[v].size(); i++) + if (e[v][i] != parent[v]) + { + all += min(dp[e[v][i]][1], dp[e[v][i]][0]); + } + dp[v][1] = min(dp[v][1], all + 500); +} + +int main() +{ + int T, t, N, n, a, b, i, j; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + scanf(" %d", &N); + for (n = 0; n < N; n++) + e[n].clear(); + for (n = 0; n < N; n++) + dp[n][0] = dp[n][1] = MAX; + memset(parent, 0, sizeof(parent)); + for (n = 0; n < N - 1; n++) + { + scanf(" %d %d", &a, &b); + e[a].push_back(b); + e[b].push_back(a); + } + parent[0] = 0; + dfs(0); + printf("$%d\n", min(dp[0][0], dp[0][1])); + } + return 0; +} diff --git a/12453.cpp b/12453.cpp new file mode 100644 index 0000000..84ea2db --- /dev/null +++ b/12453.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +void ext_gcd(long long a, long long b, long long &x, long long &y) +{ + if (b == 0) + { + x = 1, y = 0; + return; + } + ext_gcd(b, a % b, x, y); + long long t = x; + x = y; + y = t - a / b * y; +} +long long inv(long long a, long long b, long long n) +{ + a %= n; + b %= n; + long long x, y, e; + ext_gcd(a, n, x, y); + e = x * b % n; + return (e + n) % n; +} +// ret min (x+y) for a*x-b*y=c (x>=0,y>=0) +long long cal(long long a, long long b, long long c) +{ + long long d = __gcd(a, b); + if (c % d != 0) + { + return 1LL << 60; + } + a /= d; + b /= d; + c /= d; + long long x = inv(a, c, b); + long long y = (a * x - c) / b; + long long k1 = -x / b; + long long k2 = -y / a; + if (y < 0) + { + if ((-y) % a != 0) + { + k2++; + } + } + return x + y + max(k1, k2) * (a + b); +} + +long long s, x1, y1_, x2, y2; + +int main() +{ + int cas = 1; + while (~scanf("%lld%lld%lld%lld%lld", &s, &x1, &y1_, &x2, &y2)) + { + if (s == 0 && x1 == 0 && y1_ == 0 && x2 == 0 && y2 == 0) + { + break; + } + if (x1 == x2 || y1_ == y2) + { + printf("Case %d: 1\n", cas++); + continue; + } + long long ans = (1LL << 60); + long long ca1, ca2; + if (x2 >= x1) + { + ca1 = (s - x1) * 2; + } + else + { + ca1 = x1 * 2; + } + if (y2 >= y1_) + { + ca2 = (s - y1_) * 2; + } + else + { + ca2 = y1_ * 2; + } + long long x3 = abs(x2 - x1); + long long y3 = abs(y2 - y1_); + //tepan + long long cao = __gcd(y3, x3); + cao = y3 * x3 / cao; + ans = (cao / y3 + cao / x3) * 2; + if (x3 * ca2 % (2 * s) == 0) + { + ans = min(ans, cal(y3, x3, x3 * ca2 / (2 * s)) * 2 + 1); + } + if (y3 * ca1 % (2 * s) == 0) + { + ans = min(ans, cal(y3, x3, -y3 * ca1 / (2 * s)) * 2 + 1); + } + if ((x3 * ca2 - y3 * ca1) % (2 * s) == 0) + { + ans = min(ans, cal(y3, x3, (x3 * ca2 - y3 * ca1) / (2 * s)) * 2 + 2); + } + printf("Case %d: %lld\n", cas++, ans); + } + return 0; +} diff --git a/12454.cpp b/12454.cpp new file mode 100644 index 0000000..cf685c6 --- /dev/null +++ b/12454.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES +#define EPS std::numeric_limits::epsilon() +#define epsilon 0.000001 + +int arr[100], i, numCases, num, x, y, diameter, sum; + +void checkIntersect(int x, int y, int diameter, int fig) +{ + double r, m, a, b, c, d; + r = diameter / 2.0; + for (int i = 0; i < 9; i++) + { + m = tan(i * M_PI / 18.0); + a = 1.0 + m * m; + b = (-2.0 * x) - (2.0 * y * m); + c = (x * x) + (y * y) - (r * r); + d = (b * b) - (4.0 * a * c); + if ((d >= 0.0) || (fabs(d) < epsilon)) + { + arr[i]++; + return; + } + } + if (x == 0 || + (x < 0 && x + r < epsilon) || + (x > 0 && x - r < epsilon)) + { + arr[9]++; + } +} + +int main() +{ + scanf("%d", &numCases); + while (numCases--) + { + memset(arr, 0, sizeof(arr)); + scanf("%d", &num); + for (int i = 0; i < num; i++) + { + scanf("%d %d %d", &x, &y, &diameter); + checkIntersect(x, y, diameter, i + 1); + } + sum = 0; + for (int i = 0; i <= 9; i++) + { + if (arr[i] == 0) + { + continue; + } + printf("Fire laser at %d degrees.\n", i * 10); + sum += arr[i]; + } + if (sum == 0) + { + printf("No balloon burst.\n"); + } + else if (sum == 1) + { + printf("1 burst balloon.\n"); + } + else + { + printf("%d burst balloons.\n", sum); + } + } + return 0; +} diff --git a/12455.cpp b/12455.cpp new file mode 100644 index 0000000..b4362f1 --- /dev/null +++ b/12455.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int tar, n; + scanf("%d%d", &tar, &n); + int a[20]; + rep(i, n) scanf("%d", a + i); + bool ok = false; + rep(S, 1 << n) + { + int sum = 0; + rep(i, n) if (S & 1 << i) + { + sum += a[i]; + } + if (sum == tar) + { + ok = true; + break; + } + } + puts(ok ? "YES" : "NO"); + } + return 0; +} diff --git a/12456.cpp b/12456.cpp new file mode 100644 index 0000000..a47ffbe --- /dev/null +++ b/12456.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +typedef long long ll; + +int main() +{ + for (int n; scanf("%d", &n), n;) + { + vector base(n); + rep(i, n) scanf("%d", &base[i]); + ll ans1 = 1, ans2 = 1; + rep(i, n) + { + ans1 *= base[i]; + if (i < (n + 1) / 2) + { + ans2 *= min(base[i], base[n - i - 1]); + } + } + printf("%lld\n", ans1 - ans2); + } + return 0; +} diff --git a/12457.cpp b/12457.cpp new file mode 100644 index 0000000..aade6c4 --- /dev/null +++ b/12457.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +unsigned xor32() +{ + static unsigned x = 123456789, y = 362436069, z = 521288629, w = 88675123; + unsigned t = x ^ (x << 11); + x = y; + y = z; + z = w; + return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + double p; + scanf("%d%lf", &n, &p); + int win = 0; + rep(t, 100000) + { + int a = 0, b = 0; + while (a < n && b < n) + { + if (xor32() < p * UINT_MAX) + { + a++; + } + else + { + b++; + } + } + if (a == n) + { + win++; + } + } + printf("%.2f\n", win / 1e5); + } + return 0; +} diff --git a/12458.cpp b/12458.cpp new file mode 100644 index 0000000..60f65f1 --- /dev/null +++ b/12458.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const int INF = 1 << 30; + +template +struct interval +{ + T a, b; +}; + +bool cover(const interval &I, int data) +{ + return I.a <= data && data <= I.b; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int h; + scanf("%d ", &h); + vector> data(h); + rep(i, h) + { + static char s[100000]; + gets(s); + for (int j = 0, a = 0;; j++) + { + if (s[j] == ' ' || s[j] == '\0') + { + if (j == 0 || s[j - 1] == ' ') + { + continue; + } + data[i].push_back(a); + a = 0; + if (s[j] == '\0') + { + break; + } + } + else + { + a = 10 * a + (s[j] - '0'); + } + } + } + vector> L(h), R(h);// answer + vector>> I(h); + rep(i, h) + { + L[i].assign(data[i].size(), INF); + R[i].assign(data[i].size(), INF); + I[i].assign(data[i].size(), (interval){ + -INF, INF}); + } + for (int i = 1; i < h; i++) + { + rep(j, data[i].size()) + { + int a = data[i][j]; + rep(k, data[i - 1].size()) if (cover(I[i - 1][k], a)) + { + if (a < data[i - 1][k]) + L[i - 1][k] = a, I[i][j] = (interval){ + I[i - 1][k].a, data[i - 1][k] - 1}; + else + R[i - 1][k] = a, I[i][j] = (interval){ + data[i - 1][k] + 1, I[i - 1][k].b}; + } + } + } + rep(i, h) + rep(j, data[i].size()) + { + printf("%d:", data[i][j]); + if (L[i][j] != INF) + printf("%d", L[i][j]); + putchar('-'); + if (R[i][j] != INF) + printf("%d", R[i][j]); + putchar('\n'); + } + } + return 0; +} diff --git a/12459.cpp b/12459.cpp new file mode 100644 index 0000000..f9c8ddd --- /dev/null +++ b/12459.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +typedef long long ll; + +int main() +{ + ll f[81] = {0}, m[81] = {1}; + rep(i, 80) + { + f[i + 1] = f[i] + m[i]; + m[i + 1] = f[i]; + } + for (int n; scanf("%d", &n), n;) + { + printf("%lld\n", f[n] + m[n]); + } + return 0; +} diff --git a/1246.cpp b/1246.cpp new file mode 100644 index 0000000..9e43d93 --- /dev/null +++ b/1246.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +//1246 +//Find Terrorists +//Math;Sieve + +bool P[100]; +int T[10000001]; +vector W; + +long long real_mod(long long a, long long b) +{ + long long c = a % b; + if (c < 0) + { + c += b; + } + return c; +} + +int main() +{ + int n, k; + memset(P, true, sizeof(P)); + P[0] = P[1] = false; + for (int i = 2; i < 100; i++) + { + if (P[i]) + { + W.push_back(i); + for (int j = i * i; j >= 0 && j < 100; j += i) + { + P[j] = false; + } + } + } + int t; + cin >> t; + t = 0; + int a, b; + while (cin >> a >> b) + { + memset(T, 0, sizeof(int) * (b - a + 1)); + if (a == 0) + { + T[0] -= 2; + T[1] -= 1; + } + if (a == 1) + { + T[0] -= 1; + } + for (long long i = 2; i * i <= b; i++) + { + for (long long j = max(real_mod(i * i + i - a, i), i * i + i - a); j <= (b - a); j += i) + { + T[j] += 2; + } + int tmp = i * i - a; + if (tmp >= 0 && tmp <= (b - a)) + { + T[tmp]++; + } + } + int cnt = 0; + for (int i = 0; i <= (b - a); i++) + { + if (P[T[i] + 2]) + { + cout << (cnt++ ? " " : "") << i + a; + } + } + if (!cnt) + { + cout << -1; + } + cout << endl; + } +} diff --git a/12460.cpp b/12460.cpp new file mode 100644 index 0000000..211c556 --- /dev/null +++ b/12460.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +typedef long long ll; + +const ll P1 = 131; +const ll P2 = 1000000007; + +ll P1k[100000];// P1^k mod P2 + +struct hashed +{ + int len; + ll h; + hashed() {} + hashed(const string &s) + { + len = s.length(); + h = 0; + rep(k, len) h += s[k] * P1k[k]; + h %= P2; + } + bool operator<(const hashed &H) const + { + return len < H.len || len == H.len && h < H.h; + } + bool operator==(const hashed &H) const + { + return len == H.len && h == H.h; + } + bool operator!=(const hashed &H) const + { + return !(*this == H); + } +}; + +int main() +{ + P1k[0] = 1; + rep(k, 99999) P1k[k + 1] = P1k[k] * P1 % P2; + vector dic_s; + while (1) + { + string s; + cin >> s; + if (s == "--") + { + break; + } + dic_s.push_back(s); + } + int n = dic_s.size(); + vector dic(n); + rep(i, n) dic[i] = hashed(dic_s[i]); + vector> tmp(n); + rep(i, n) tmp[i] = make_pair(dic[i], dic_s[i]); + sort(tmp.begin(), tmp.end()); + rep(i, n) + { + dic[i] = tmp[i].first; + dic_s[i] = tmp[i].second; + } + while (1) + { + string s0, t0; + if (!(cin >> s0 >> t0)) + { + break; + } + hashed s(s0), t(t0); + int id_s = lower_bound(dic.begin(), dic.end(), s) - dic.begin(); + int id_t = lower_bound(dic.begin(), dic.end(), t) - dic.begin(); + if (id_s == n || dic[id_s] != s || id_t == n || dic[id_t] != t) + { + cout << "No" << endl; + continue; + } + vector vis(n); + vis[id_s] = true; + bool ans = false; + queue Q; + Q.push(id_s); + while (!Q.empty()) + { + int id = Q.front(); + Q.pop(); + if (id == id_t) + { + ans = true; + break; + } + rep(c, 128) if (isprint(c)) rep(k, dic[id].len) + { + hashed H = dic[id]; + H.h += (c - dic_s[id][k]) * P1k[k]; + H.h %= P2; + if (H.h < 0) + { + H.h += P2; + } + int id2 = lower_bound(dic.begin(), dic.end(), H) - dic.begin(); + if (id2 < n && dic[id2] == H && !vis[id2]) + { + vis[id2] = true; + Q.push(id2); + } + } + } + cout << (ans ? "Yes" : "No") << endl; + } + return 0; +} diff --git a/12461.cpp b/12461.cpp new file mode 100644 index 0000000..8f24239 --- /dev/null +++ b/12461.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +#define INF (1 << 29) +#define SET(a) memset(a, -1, sizeof(a)) +#define ALL(a) a.begin(), a.end() +#define CLR(a) memset(a, 0, sizeof(a)) +#define FILL(a, v) memset(a, v, sizeof(a)) +#define PB push_back +#define FOR(i, n) for (int i = 0; i < n; i++) +#define PI acos(-1.0) +#define EPS 1e-9 +#define MP(a, b) make_pair(a, b) +#define min3(a, b, c) min(a, min(b, c)) +#define max3(a, b, c) max(a, max(b, c)) +#define LL long long +#define MX 100000 +#define MOD 1000000007 + +int main() +{ + int n; + while (cin >> n && n) + cout << "1/2" << endl; + return 0; +} diff --git a/12462.cpp b/12462.cpp new file mode 100644 index 0000000..f129baa --- /dev/null +++ b/12462.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define INF (1 << 29) +#define SET(a) memset(a, -1, sizeof(a)) +#define ALL(a) a.begin(), a.end() +#define CLR(a) memset(a, 0, sizeof(a)) +#define FILL(a, v) memset(a, v, sizeof(a)) +#define PB push_back +#define FOR(i, n) for (int i = 0; i < n; i++) +#define PI acos(-1.0) +#define EPS 1e-9 +#define MP(a, b) make_pair(a, b) +#define LL long long +#define MX 100100 + +LL colorNum[MX][40]; +LL n, c, t, kk = 1, histogram[MX], i, lft[MX], rgt[MX], mx, color[MX]; + +int main() +{ + stack stk; + while (cin >> n >> c && n && c) + { + for (i = 1; i <= n; i++) + scanf("%lld", &histogram[i]); + CLR(colorNum); + for (i = 1; i <= n; i++) + { + scanf("%lld", &color[i]); + long long cl = color[i]; + colorNum[i][cl] = (colorNum[i - 1][cl]) + 1; + for (int j = 0; j < cl; j++) + colorNum[i][j] = colorNum[i - 1][j]; + for (int j = cl + 1; j < c; j++) + colorNum[i][j] = colorNum[i - 1][j]; + } + while (!stk.empty()) + stk.pop(); + + stk.push(0); + histogram[0] = 0; + for (i = 1; i <= n; i++) + { + while (histogram[stk.top()] >= histogram[i]) + stk.pop(); + lft[i] = stk.top() + 1; + stk.push(i); + } + while (!stk.empty()) + stk.pop(); + + stk.push(n + 1); + histogram[n + 1] = 0; + for (i = n; i > 0; i--) + { + while (histogram[stk.top()] >= histogram[i]) + stk.pop(); + rgt[i] = stk.top() - 1; + stk.push(i); + } + mx = 0; + for (i = 1; i <= n; i++) + { + bool chk = true; + ; + for (int j = 0; j < c; j++) + if (colorNum[rgt[i]][j] - colorNum[lft[i] - 1][j] == 0) + chk = false; + if (chk) + mx = max(mx, (rgt[i] - lft[i] + 1) * histogram[i]); + } + cout << mx << endl; + } + return 0; +} diff --git a/12463.cpp b/12463.cpp new file mode 100644 index 0000000..9356ab5 --- /dev/null +++ b/12463.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + long int a, b, c, d, e; + long int i, j, k, ans; + while (cin >> a >> b >> c >> d >> e) + { + if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0) + { + break; + } + e = (e) * (e); + d = (d) * (d); + ans = (a * b * c * d * e); + cout << ans << endl; + } + return 0; +} diff --git a/12464.cpp b/12464.cpp new file mode 100644 index 0000000..46b84a2 --- /dev/null +++ b/12464.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() +{ + int a, b; + long long n; + while (true) + { + scanf("%d %d %lld", &a, &b, &n); + if (a == 0) + { + break; + } + n %= 5; + if (n == 0) + { + printf("%d\n", a); + } + if (n == 1) + { + printf("%d\n", b); + } + if (n == 2) + { + printf("%d\n", (1 + b) / a); + } + if (n == 3) + { + printf("%d\n", (1 + a + b) / a / b); + } + if (n == 4) + { + printf("%d\n", (1 + a) / b); + } + } + return 0; +} diff --git a/12465.cpp b/12465.cpp new file mode 100644 index 0000000..504e9ee --- /dev/null +++ b/12465.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int main() +{ + // finding the divisors of |a-b| + int a, b; + while (true) + { + scanf("%d %d", &a, &b); + if (a == 0) + { + break; + } + a -= b; + if (a < 0) + { + a = -a; + } + int ans = 1; + for (int i = 2; i <= a / i; ++i) + { + if (a % i == 0) + { + int cont = 1; + while (a % i == 0) + { + a /= i; + ++cont; + } + ans *= cont; + } + } + if (a > 1) + { + ans <<= 1; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12466.cpp b/12466.cpp new file mode 100644 index 0000000..d02338e --- /dev/null +++ b/12466.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define inf 1e9 +#define maxn 100005 + +int en; +struct EDGE +{ + int v, next; +} edge[maxn]; +int head[maxn], w[maxn], dp[maxn][105]; +int n, m, cn[maxn]; + +void addedge(int u, int v) +{ + edge[en].v = v, edge[en].next = head[u]; + head[u] = en++; +} + +void dfs(int u) +{ + cn[u] = 1; + for (int i = head[u]; i != -1; i = edge[i].next) + { + int v = edge[i].v; + dfs(v); + cn[u] += cn[v]; + } + dp[u][0] = 0; + for (int i = head[u]; i != -1; i = edge[i].next) + { + int v = edge[i].v; + int child = min(cn[u] - 1, m); + for (int j = child; j > 0; j--) + { + for (int k = 1; k <= j && k < cn[v]; k++) + { + dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[v][k]); + } + if (j) + { + dp[u][j] = max(dp[u][j], dp[u][j - 1] + w[v]); + } + } + } + dp[u][1] = max(dp[u][1], w[u]); +} + +int main() +{ + int v; + while (scanf("%d%d", &n, &m) && n) + { + en = 0; + memset(head, -1, sizeof(head)); + memset(cn, 0, sizeof(cn)); + for (int i = 0; i < n; i++) + { + scanf("%d", &w[i]); + } + for (int i = 1; i < n; i++) + { + scanf("%d", &v); + addedge(v, i); + } + for (int i = 0; i <= n; i++) + for (int j = 0; j <= m; j++) + { + dp[i][j] = -inf; + } + dfs(0); + int ans = -inf; + for (int j = 1; j <= m; j++) + { + ans = max(ans, dp[0][j]); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12467.cpp b/12467.cpp new file mode 100644 index 0000000..87f3c55 --- /dev/null +++ b/12467.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +char c1[1000011], c2[1000011]; +int nxt[1000011]; + +int kmp() +{ + int k = strlen(c1), i, j, y = -1; + memset(nxt, 0, sizeof(nxt)); + nxt[0] = -1; + for (i = 1; i < k; i++) + { + while (y > -1 && c1[i] != c1[y + 1]) + { + y = nxt[y]; + } + if (c1[y + 1] == c1[i]) + { + y++; + } + nxt[i] = y; + } + int max = -1; + y = -1; + for (i = 0; i < k; i++) + { + while (y > -1 && c2[i] != c1[y + 1]) + { + y = nxt[y]; + } + if (c1[y + 1] == c2[i]) + { + y++; + if (max < y) + { + max = y; + } + } + } + return max; +} + +int main() +{ + int t, n, m, i, j, k; + scanf("%d%*c", &t); + while (t--) + { + gets(c1); + k = strlen(c1); + for (i = 0; i <= k - 1; i++) + { + c2[i] = c1[k - i - 1]; + } + n = kmp(); + for (i = n; i >= 0; i--) + { + printf("%c", c1[i]); + } + puts(""); + } + return 0; +} diff --git a/12468.cpp b/12468.cpp new file mode 100644 index 0000000..bd66c67 --- /dev/null +++ b/12468.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + int a, b, d; + while (scanf("%d %d", &a, &b) == 2 && a >= 0 && b >= 0) + { + d = abs(b - a); + if (d > 50) + { + printf("%d\n", 100 - d); + } + else + { + printf("%d\n", d); + } + } + return 0; +} diff --git a/12469.cpp b/12469.cpp new file mode 100644 index 0000000..e0ad584 --- /dev/null +++ b/12469.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +#define INF (1 << 29) +#define SET(a) memset(a, -1, sizeof(a)) +#define ALL(a) a.begin(), a.end() +#define CLR(a) memset(a, 0, sizeof(a)) +#define FILL(a, v) memset(a, v, sizeof(a)) +#define PB push_back +#define FOR(i, n) for (int i = 0; i < n; i++) +#define PI acos(-1.0) +#define EPS 1e-9 +#define MP(a, b) make_pair(a, b) +#define min3(a, b, c) min(a, min(b, c)) +#define max3(a, b, c) max(a, max(b, c)) +#define LL long long +#define MX 100000 +#define MOD 1000000007 + +bool isfib[1000 + 10]; +int fib[20], n; + +int main() +{ + fib[0] = 0; + fib[1] = 1; + for (int i = 2; i < 18; i++) + { + int x = fib[i] = fib[i - 1] + fib[i - 2]; + isfib[x] = true; + } + while (cin >> n && n) + { + if (n == 1 || !isfib[n]) + cout << "Alicia" << endl; + else + cout << "Roberto" << endl; + } + return 0; +} diff --git a/1247.cpp b/1247.cpp new file mode 100644 index 0000000..e0c16e2 --- /dev/null +++ b/1247.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +const int MAX = 30; +const int INF = 1e9; +typedef pair P; + +int G[MAX][MAX]; +int d[MAX]; +int path[MAX]; +int N; + +void init() +{ + for (int i = 0; i < MAX; i++) + for (int j = 0; j < MAX; j++) + { + G[i][j] = INF; + } +} + +void dijkstra(int s, int g) +{ + for (int i = 0; i < MAX; i++) + { + d[i] = INF, path[i] = INF; + } + d[s] = 0; + priority_queue, greater

> que; + que.push(P(0, s)); + while (que.size()) + { + P p = que.top(); + que.pop(); + int v = p.second; + if (d[v] < p.first) + { + continue; + } + for (int i = 0; i < MAX; i++) + { + if (G[v][i] == INF) + { + continue; + } + if (d[i] > d[v] + G[v][i]) + { + d[i] = d[v] + G[v][i]; + que.push(P(d[i], i)); + path[i] = v; + } + } + } +} + +int main() +{ + init(); + int N, m; + cin >> N >> m; + for (int i = 0; i < m; i++) + { + int a, b, cost; + char aa, bb; + cin >> aa >> bb >> cost; + a = aa - 'A'; + b = bb - 'A'; + G[a][b] = G[b][a] = cost; + } + cin >> m; + for (int i = 0; i < m; i++) + { + int s, g; + char aa, bb; + cin >> aa >> bb; + s = aa - 'A'; + g = bb - 'A'; + dijkstra(s, g); + vector res; + int now = g; + while (1) + { + res.push_back(now + 'A'); + now = path[now]; + if (now == INF) + { + break; + } + } + reverse(res.begin(), res.end()); + for (int j = 0; j < res.size(); j++) + { + if (j != 0) + { + cout << ' '; + } + cout << res[j]; + } + cout << endl; + } + return 0; +} diff --git a/12470.cpp b/12470.cpp new file mode 100644 index 0000000..4e3a64f --- /dev/null +++ b/12470.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MOD 1000000009 + +struct Matrix +{ + long long M[3][3]; + + void init() + { + memset(M, 0, sizeof M); + M[0][0] = M[1][1] = M[2][2] = 1; + } + + Matrix operator*(Matrix X) + { + Matrix ret; + memset(ret.M, 0, sizeof ret.M); + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j < 3; ++j) + { + for (int k = 0; k < 3; ++k) + { + ret.M[i][j] += M[i][k] * X.M[k][j] % MOD; + } + ret.M[i][j] %= MOD; + } + } + return ret; + } +} P[54]; + +int main() +{ + P[0].M[0][0] = 1; + P[0].M[0][1] = 1; + P[0].M[0][2] = 1; + P[0].M[1][0] = 1; + P[0].M[1][1] = 0; + P[0].M[1][2] = 0; + P[0].M[2][0] = 0; + P[0].M[2][1] = 1; + P[0].M[2][2] = 0; + for (int i = 1; i <= 53; ++i) + { + P[i] = P[i - 1] * P[i - 1]; + } + long long n; + Matrix aux; + while (true) + { + scanf("%lld", &n); + if (n == 0) + { + break; + } + if (n == 1) + { + puts("0"); + } + else if (n == 2) + { + puts("1"); + } + else + { + n -= 3; + aux.init(); + for (int i = 0; i <= 53; ++i, n >>= 1) + if (n & 1) + { + aux = aux * P[i]; + } + printf("%lld\n", (aux.M[0][0] * 2 + aux.M[0][1]) % MOD); + } + } + return 0; +} diff --git a/12471.cpp b/12471.cpp new file mode 100644 index 0000000..b339f37 --- /dev/null +++ b/12471.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define INF (1 << 29) +#define SET(a) memset(a, -1, sizeof(a)) +#define ALL(a) a.begin(), a.end() +#define CLR(a) memset(a, 0, sizeof(a)) +#define FILL(a, v) memset(a, v, sizeof(a)) +#define PB push_back +#define FOR(i, n) for (int i = 0; i < n; i++) +#define PI acos(-1.0) +#define EPS 1e-9 +#define MP(a, b) make_pair(a, b) +#define min3(a, b, c) min(a, min(b, c)) +#define max3(a, b, c) max(a, max(b, c)) +#define LL long long +#define MX 100010 +#define MOD 1000000007 + +int failur[MX], length; +char pattern[MX]; + +void FailureFunction(int i, int j) +{ + while (i < length) + { + if (pattern[i] == pattern[j]) + { + j++; + failur[i] = j; + i++; + } + else if (j > 0) + { + j = failur[j - 1]; + } + else + { + failur[i] = 0; + i++; + } + } +} + +int main() +{ + int t, kk = 1, n, arr[MX]; + bool found, chk; + cin >> t; + while (t--) + { + cin >> n; + for (int i = 0; i < n; i++) + cin >> arr[i]; + if (arr[0] != 0) + { + cout << "Case " << kk++ << ": -1" << endl; + continue; + } + chk = true; + failur[0] = 0; + length = 1; + pattern[0] = 'a'; + for (int i = 1; i < n; i++) + { + length = i + 1; + found = false; + for (int j = 0; j < 16; j++) + { + pattern[i] = j + 'a'; + FailureFunction(i, failur[i - 1]); + if (failur[i] == arr[i]) + { + found = true; + break; + } + } + if (!found) + { + chk = false; + break; + } + } + if (!chk) + cout << "Case " << kk++ << ": -1" << endl; + else + { + cout << "Case " << kk++ << ": "; + for (int i = 0; i < n; i++) + cout << pattern[i]; + cout << endl; + } + } + return 0; +} diff --git a/12472.cpp b/12472.cpp new file mode 100644 index 0000000..b71fa0d --- /dev/null +++ b/12472.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +typedef long long ll; + +const ll INF = 1LL << 61; + +int to_binary(ll a, char *s) +{ + int i; + for (i = 0; a > 0; i++) + { + s[i] = a % 2 + '0'; + a /= 2; + } + return i; +} + +ll dfs(const char *s, char *t, int i = 54, bool b = false) +{ + if (i == -1) + { + ll res = 0; + rep(j, 55) res |= (ll)(t[j] - '0') << j; + return res; + } + if (t[i] == '?') + { + if (b) + { + t[i] = '0'; + ll res = dfs(s, t, i - 1, true); + t[i] = '?'; + return res; + } + else + { + t[i] = '1'; + ll res = dfs(s, t, i - 1, s[i] < t[i]); + if (s[i] == '0') + { + t[i] = '0'; + res = min(res, dfs(s, t, i - 1, false)); + } + t[i] = '?'; + return res; + } + } + else + { + if (b) + { + return dfs(s, t, i - 1, true); + } + else + { + if (s[i] <= t[i]) + { + return dfs(s, t, i - 1, s[i] < t[i]); + } + else + { + return INF; + } + } + } +} + +void solve() +{ + ll a, b, p; + scanf("%lld%lld%lld", &a, &b, &p); + char sa[64], sp[64]; + int na = to_binary(a, sa); + int np = to_binary(p, sp); + for (; na < 55; na++) + { + sa[na] = '0'; + } + ll ans = INF; + rep(i, 56 - np) + { + char s[64] = "???????????????????????????????????????????????????????"; + rep(j, np) s[i + j] = sp[j]; + ans = min(ans, dfs(sa, s)); + } + if (ans <= b) + { + printf("%lld\n", ans); + } + else + { + puts("NONE"); + } +} + +int main() +{ + int T, t; + scanf("%d", &T); + for (t = 1; t <= T; t++) + { + printf("Case %d: ", t), solve(); + } + return 0; +} diff --git a/12473.cpp b/12473.cpp new file mode 100644 index 0000000..1417956 --- /dev/null +++ b/12473.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +char s[77], t[77]; + +int dp[77][77][77][77]; +int dfs(int i, int j, int k, int l) +{ + if (i > j || k > l) + { + return 0; + } + int &res = dp[i][j][k][l]; + if (res != -1) + { + return res; + } + if (s[i] == s[j] && t[k] == t[l] && s[i] == t[k]) + { + return res = (i == j || k == l ? 1 : 2) + dfs(i + 1, j - 1, k + 1, l - 1); + } + return res = max(max(dfs(i + 1, j, k, l), dfs(i, j - 1, k, l)), + max(dfs(i, j, k + 1, l), dfs(i, j, k, l - 1))); +} + +void solve() +{ + scanf("%s%s", s, t); + int m = strlen(s), n = strlen(t); + rep(j, m) rep(i, j + 1) rep(l, n) rep(k, l + 1) dp[i][j][k][l] = -1; + printf("%d\n", dfs(0, m - 1, 0, n - 1)); +} + +int main() +{ + int T, t; + scanf("%d", &T); + for (t = 1; t <= T; t++) + { + printf("Case %d: ", t), solve(); + } + return 0; +} diff --git a/12474.cpp b/12474.cpp new file mode 100644 index 0000000..55bf224 --- /dev/null +++ b/12474.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define lowbit(x) (x & (-x)) +#define LL long long +#define ULL unsigned long long +#define maxn 2000 +#define msize 30000 +#define mod 1000000009ll +#define CC(m, what) memset(m, what, sizeof(m)) +#define inf 0x7f7f7f7f +#define eps 1e-12 +#define lson l, mid, fa << 1 +#define rson mid + 1, r, fa << 1 | 1 +#define pi acos(-1.0) +#define keytree (ch[ch[root][1]][0]) + +template +inline bool checkmax(Z &a, Z b) +{ + if (a == -1 || a < b) + { + a = b; + return true; + } + return false; +} +template +inline bool checkmin(Z &a, Z b) +{ + if (a == -1 || a > b) + { + a = b; + return true; + } + return false; +} + +map p; + +LL dfs(LL x) +{ + if (x == 0) + { + return 0; + } + if (p[x]) + { + return p[x]; + } + if (x & 1) + { + return p[x] = x / 2 + 2 * dfs(x / 2); + } + else + { + return p[x] = (x - 1) / 2 + dfs(x / 2) + dfs(x / 2 - 1); + } +} + +int main() +{ + int cas; + int i, j, k = 1; + LL n; + p.clear(); + scanf("%d", &cas); + while (cas--) + { + cin >> n; + printf("Case %d: %lld\n", k++, dfs(n)); + } + return 0; +} diff --git a/12475.cpp b/12475.cpp new file mode 100644 index 0000000..d776d41 --- /dev/null +++ b/12475.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const double PI = acos(-1); + +double ellipse_circumference(double a, double b) +{ + int p[] = {0, 1, 2, 4, 9, 20, 43, 90, 185, 376, 759, 1526}; + double c[] = {5.9685, 3.7127, 0.28582, 2.098, 1.462, 1.1413, 0.736, 0.459, 0.244, 0.144, 0.051, 0.062}; + double h = pow((a - b) / (a + b), 2), g = 0; + rep(i, 11) g += c[i] * pow(h, p[i]); + return PI * (a + b) * (1 + 3 * h / (10 + sqrt(4 - 3 * h)) + (44 - 14 * PI) / (11 * PI) * pow(h, g)); +} + +void solve() +{ + int a, b; + scanf("%d%d", &a, &b); + printf("%.9f\n", ellipse_circumference(a, b)); +} + +int main() +{ + int T, t; + scanf("%d", &T); + for (t = 1; t <= T; t++) + { + printf("Case %d: ", t), solve(); + } + return 0; +} diff --git a/12476.cpp b/12476.cpp new file mode 100644 index 0000000..54212ae --- /dev/null +++ b/12476.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +struct Edge +{ + int to, next; +}; + +bool hasres; +Edge edge[210000]; +int head[41000], L; +int n, lc[21000], rc[21000]; +char s[41000]; +int l; + +void addedge(int u, int v) +{ + if (u > l || v == -1) + { + return; + } + edge[L].to = v; + edge[L].next = head[u]; + head[u] = L++; +} + +int main() +{ + int totcas; + scanf("%d", &totcas); + for (int cas = 1; cas <= totcas; cas++) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d%d", &lc[i], &rc[i]); + } + scanf("%s", s); + l = strlen(s); + memset(head, -1, sizeof(head)); + L = 0; + addedge(0, 1); + hasres = false; + for (int i = 0; i < l; i++) + { + if (head[i] == -1) + { + continue; + } + int nxt = i; + while (nxt < l && s[nxt] == s[i]) + { + nxt++; + } + int len = (nxt - i); + int halflen = (len + 1) / 2; + if (len == 1)//XXXRL or XXXLR + { + for (int j = head[i]; j != -1; j = edge[j].next) + { + if (s[i] == 'R')//XXXRL + { + addedge(i + 1, rc[edge[j].to]); + addedge(i + 2, lc[edge[j].to]); + } + else//XXXLR + { + addedge(i + 1, lc[edge[j].to]); + addedge(i + 2, rc[edge[j].to]); + } + } + } + else//XXXLLLLLLLLLLLR or XXXRRRRRRRRRRRRL + { + //to i-2 : len/2 .. len-1 + //to i-1 : (len+1)/2 .. len + for (int j = head[i]; j != -1; j = edge[j].next) + { + int now = edge[j].to; + int odd = -1; + for (int k = 1; k <= len; k++) + { + if (s[i] == 'L') + { + now = lc[now]; + } + else + { + now = rc[now]; + } + if (now == -1) + { + break; + } + if (k == halflen - 1) + { + odd = now; + } + if (nxt == l && len % 2 == 1 && k >= halflen - 1) + { + hasres = true; + } + if (k >= halflen)//can go to nxt-1 + { + addedge(nxt, now); + } + } + if (nxt < l && odd != -1 && len % 2 == 1) + { + if (s[nxt] == 'L')//XXXRRRRRL + { + addedge(nxt + 1, lc[odd]); + } + else//XXXLLLLLR + { + addedge(nxt + 1, rc[odd]); + } + } + } + } + } + printf("Case %d: ", cas); + if (hasres || head[l - 1] != -1 || head[l] != -1) + { + puts("Yes"); + } + else + { + puts("No"); + } + } + return 0; +} diff --git a/12477.cpp b/12477.cpp new file mode 100644 index 0000000..b7cb960 --- /dev/null +++ b/12477.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +int const N = 100005; +long long oo = (1LL << 62) - 1LL; +int num[N], Node; + +struct node +{ + int a, b, l, r; + long long sum, ssum, Min, Max, add, setN; + bool flag; + void read(int a, int b) + { + this->a = a; + this->b = b; + flag = false; + add = 0; + } + void Add(long long c) + { + Min += c; + Max += c; + ssum += sum * c * 2LL + c * c * (b - a + 1); + sum += c * (b - a + 1); + if (flag) + { + setN += c; + } + else + { + add += c; + } + } + void Set(long long c) + { + flag = true; + sum = ssum = setN = Min = Max = add = 0; + Add(c); + } +} tr[N * 2], tmp; + +void update(int k, int l, int r) +{ + tmp = tr[k]; + tmp.Max = max(tr[l].Max, tr[r].Max); + tmp.Min = min(tr[l].Min, tr[r].Min); + tmp.sum = tr[l].sum + tr[r].sum; + tmp.ssum = tr[l].ssum + tr[r].ssum; + tr[k] = tmp; +} + +void makeTree(int a, int b) +{ + int k = ++Node; + tr[k].read(a, b); + if (a == b) + { + tr[k].Set(num[a]); + return; + } + tr[k].l = Node + 1; + makeTree(a, (a + b) / 2); + tr[k].r = Node + 1; + makeTree((a + b) / 2 + 1, b); + update(k, tr[k].l, tr[k].r); +} + +void insert(int a, int b, long long c, int cmd, int k) +{ + if (tr[k].b <= b && tr[k].a >= a) + { + switch (cmd) + { + case 0: + tr[k].Set(c); + break; + case 1: + tr[k].Add(c); + break; + case 2: + update(0, 0, k); + break; + } + } + else + { + if (tr[k].flag) + { + tr[tr[k].l].Set(tr[k].setN); + tr[tr[k].r].Set(tr[k].setN); + tr[k].flag = false; + } + if (tr[k].add) + { + tr[tr[k].l].Add(tr[k].add); + tr[tr[k].r].Add(tr[k].add); + tr[k].add = 0; + } + int m = (tr[k].a + tr[k].b) >> 1; + if (a <= m) + { + insert(a, b, c, cmd, tr[k].l); + } + if (b > m) + { + insert(a, b, c, cmd, tr[k].r); + } + update(k, tr[k].l, tr[k].r); + } +} + +long long gcd(long long a, long long b) +{ + if (b == 0) + { + return a; + } + return gcd(b, a % b); +} + +int main() +{ + int T, n, m; + long long t, x, y, d; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + printf("Case %d:\n", cas); + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; i++) + { + scanf("%d", &num[i]); + } + Node = 0; + makeTree(1, n); + int cmd, a, b, c; + for (int i = 1; i <= m; i++) + { + scanf("%d", &cmd); + if (cmd == 0 || cmd == 1) + { + scanf("%d%d%d", &a, &b, &c); + insert(a, b, c, cmd, 1); + } + else + { + scanf("%d%d", &a, &b); + tr[0].Min = oo; + tr[0].Max = -oo; + tr[0].sum = 0; + tr[0].ssum = 0; + insert(a, b, 0, cmd, 1); + t = b - a + 1; + x = tr[0].ssum * t - tr[0].sum * tr[0].sum; + y = t * t; + d = gcd(x, y); + printf("%lld/%lld ", x / d, y / d); + printf("%lld\n", tr[0].Max - tr[0].Min); + } + } + } + return 0; +} diff --git a/12478.cpp b/12478.cpp new file mode 100644 index 0000000..e724c0d --- /dev/null +++ b/12478.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; + +int main() +{ + printf("KABIR\n"); + return 0; +} diff --git a/12479.cpp b/12479.cpp new file mode 100644 index 0000000..37e9c75 --- /dev/null +++ b/12479.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +#define MAXN 30009 +#define MAXE 60009 + +int fo[MAXE], to[MAXE], ne[MAXE], fin[MAXE], used[MAXN], col[MAXN], lo[MAXN], idx[MAXN], N, E, n_edge, dfs_time, road, city; +pair store[MAXE]; +stack > S; +vector G[MAXN]; + +inline void add_edge(int u, int v) +{ + fo[n_edge] = u; + to[n_edge] = v; + ne[n_edge] = fin[u]; + fin[u] = n_edge++; + + fo[n_edge] = v; + to[n_edge] = u; + ne[n_edge] = fin[v]; + fin[v] = n_edge++; + return; +} + +void sol_bcc(int u, int v) +{ + // take a bi-conn component elem + int tot = 0, nu, nv; + pair e; + while (!S.empty()) + { + e = S.top(); + S.pop(); + nu = e.first; + nv = e.second; + G[nu].clear(); + G[nv].clear(); + store[tot] = make_pair(nu, nv); + tot++; + if ((nu == u && nv == v) || (nu == v && nv == u)) + { + break; + } + } + + if (tot <= 1) + { + return;// one edge + } + memset(col, 0, sizeof(col)); + for (int i = 0; i < tot; i++) + { + int nu = store[i].first; + int nv = store[i].second; + used[nu] = used[nv] = 1; + G[nu].push_back(nv); + G[nv].push_back(nu); + } + + // chk odd cycle + queue Q; + Q.push(nu); + col[nu] = 1; + while (!Q.empty()) + { + nu = Q.front(); + Q.pop(); + for (int i = 0; i < (int)G[nu].size(); i++) + { + nv = G[nu][i]; + if (col[nv] == 0) + { + col[nv] = 3 - col[nu]; + Q.push(nv); + } + if (col[nu] == col[nv]) + { + return; + } + } + } + road++; +} + +void dfs(int u, int p) +{ + lo[u] = idx[u] = ++dfs_time; + for (int i = fin[u]; i >= 0; i = ne[i]) + { + int v = to[i]; + if (v == p) + { + continue; + } + if (idx[v] == 0) + { + S.push(make_pair(u, v)); + dfs(v, u); + lo[u] = min(lo[u], lo[v]); + if (lo[v] >= idx[u]) + { + sol_bcc(u, v); + } + } + else if (idx[v] < idx[u]) + { + S.push(make_pair(u, v)); + lo[u] = min(lo[u], idx[v]); + } + } + return; +} + +void bcc() +{ + memset(idx, 0, sizeof(idx)); + memset(used, 0, sizeof(used)); + dfs_time = 0; + road = 0; + city = 0; + for (int i = 0; i < N; i++) + { + if (idx[i]) + { + continue; + } + dfs(i, i); + } + for (int i = 0; i < N; i++) + { + if (used[i] == 0) + { + road += 3; + city += 2; + } + } + return; +} + +int main() +{ + int T, t = 0; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &E); + memset(fin, -1, sizeof(fin)); + n_edge = 0; + int a, b; + for (int i = 0; i < E; i++) + { + scanf("%d%d", &a, &b); + add_edge(a, b); + } + bcc(); + printf("Case %d: %d %d\n", ++t, road, city); + } + return 0; +} diff --git a/1248.cpp b/1248.cpp new file mode 100644 index 0000000..7c4565f --- /dev/null +++ b/1248.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int d[1002], p[1002], n[1002]; +int N; +int DP[1001][201]; + +int main() +{ + int t, i, j, l; + scanf("%d", &t); + while (t--) + { + scanf("%d", &N); + n[0] = 0; + for (i = 1; i <= N; i++) + scanf("%d %d %d", &p[i], &n[i], &d[i]); + for (i = 0; i <= N; i++) + for (j = 0; j < 201; j++) + DP[i][j] = 10000000; + DP[1][0] = d[1] * p[1]; + for (i = 1; i < N; i++) + for (j = 0; j < 201 && j <= n[i]; j++) + for (l = 0; l < 201 && l <= n[i - 1]; l++) + { + int ans = j * p[i] + max(0, d[i + 1] - j) * p[i + 1] + DP[i][l]; + if (j + max(0, d[i] - l) > n[i]) + continue; + if (d[i + 1] - j > n[i + 1]) + continue; + DP[i + 1][j] = min(DP[i + 1][j], ans); + } + int bs = 10000000; + for (j = 0; j < 201 && j <= n[N - 1]; j++) + bs = min(bs, DP[N][j]); + printf("%d\n", bs); + } +} diff --git a/12480.cpp b/12480.cpp new file mode 100644 index 0000000..4eb8718 --- /dev/null +++ b/12480.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +const int Maxn = 22; + +struct Stack +{ + int v[3], top; + int operator[](int x) + { + return v[x]; + } + int size() + { + return top; + } + void push(int x) + { + v[top++] = x; + } + void clear() + { + top = 0; + } + void pop() + { + top--; + } +} stk[Maxn]; + +struct Array +{ + int v[Maxn], top; + int operator[](int x) + { + return v[x]; + } + void add(int x) + { + v[top++] = x; + } + void clear() + { + top = 0; + } + bool operator<(const Array &e) const + { + for (int i = 0; i < top; ++i) + if (v[i] < e.v[i]) + { + return 1; + } + else if (v[i] > e.v[i]) + { + return 0; + } + return 0; + } +}; + +vector ans[Maxn]; +void add(int n, int s) +{ + int l = -1; + Array a; + a.clear(); + while (1) + { + a.add(s); + if (a.top == n) + { + break; + } + bool flag = false; + for (int i = 0; i < stk[s].size(); ++i) + { + int x1 = stk[s][i]; + if (x1 == l) + { + continue; + } + l = s; + s = x1; + flag = true; + break; + } + if (!flag) + { + return; + } + } + ans[n].push_back(a); + reverse(a.v, a.v + n); + ans[n].push_back(a); +} + +void dfs(int n, int e) +{ + if (e == n) + { + for (int i = 1; i <= n; ++i) + { + if (stk[i].size() == 1) + { + add(n, i); + break; + } + } + return; + } + for (int i = 0; i < e; ++i) + { + if (stk[e - i].size() < 2 && stk[n - i].size() < 2) + { + stk[e - i].push(n - i), stk[n - i].push(e - i); + dfs(n, e + 1); + stk[e - i].pop(), stk[n - i].pop(); + } + } +} + +int main() +{ + for (int i = 2; i < 20; ++i) + { + for (int j = 0; j < i; ++j) + { + stk[j].clear(); + } + dfs(i, 1); + sort(ans[i].begin(), ans[i].end()); + } + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + printf("Case %d:", cas); + int n, k; + scanf("%d%d", &n, &k); + if (ans[n].size() < k) + { + printf(" -1"); + } + else + { + for (int i = 0; i < n; ++i) + { + printf(" %d", ans[n][k - 1][i]); + } + } + puts(""); + } + return 0; +} diff --git a/12481.cpp b/12481.cpp new file mode 100644 index 0000000..98deb29 --- /dev/null +++ b/12481.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; +#define MP(a, b) make_pair(a, b) +#define FOREACH(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) + +const int MAX_N = 1000 + 50; +int N, M, K, mat[MAX_N][MAX_N], hU[MAX_N], hR[MAX_N]; + +int que[MAX_N], qT; +int maxRecArea(int hR[], int hU[], int n) +{ + hR[0] = hR[n] = -1;//-INF + que[qT = 1] = 0; + int ret = 0; + for (int i = 1; i <= n; i++) + { + ret = max(ret, hU[i]); + while (qT > 1 && hR[i] <= hR[que[qT]]) + { + //(que[qT-1], i], assert(qT > 1) + ret = max(ret, min(hR[que[qT]], hU[i]) * (i - que[qT - 1])); + --qT; + } + que[++qT] = i; + } + return ret; +} + +int main() +{ + int test; + scanf("%d", &test); + for (int Case = 1; Case <= test; Case++) + { + scanf("%d %d %d", &N, &M, &K); + for (int i = 1; i <= N; i++) + for (int j = 1; j <= M; j++) + { + scanf("%d", &mat[i][j]); + } + memset(hU, 0, sizeof(hU)); + memset(hR, 0, sizeof(hR)); + int res = 1; + for (int r = 1; r <= N; r++) + { + for (int c = 1; c <= M; c++) + if (r == 1 || abs(mat[r][c] - mat[r - 1][c]) <= K) + { + hU[c]++; + } + else + { + hU[c] = 1; + } + for (int c = 1; c < M; c++) + { + if (abs(mat[r][c] - mat[r][c + 1]) <= K) + { + hR[c]++; + } + else + { + hR[c] = 0; + } + hR[c] = min(hU[c], hR[c]); + } + int upd = maxRecArea(hR, hU, M); + res = max(res, upd); + } + printf("Case %d: %d\n", Case, res); + } + return 0; +} diff --git a/12482.cpp b/12482.cpp new file mode 100644 index 0000000..83e7b50 --- /dev/null +++ b/12482.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int N, L, C; + char s[100]; + while (scanf("%d %d %d", &N, &L, &C) == 3) + { + int len, line = 0, sum = 0; + while (N--) + { + scanf("%s", s); + len = strlen(s); + if (sum + len <= C) + { + sum += len + 1; + } + else + { + sum = len + 1; + line++; + } + } + line += (sum != 0); + if (line % L) + printf("%d\n", line / L + 1); + else + printf("%d\n", line / L); + } + return 0; +} diff --git a/12483.cpp b/12483.cpp new file mode 100644 index 0000000..7cccf95 --- /dev/null +++ b/12483.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define eps 1e-6 + +typedef unsigned long long ull; + +struct Point +{ + double x, y; + + Point() {} + Point(double x, double y) : x(x), y(y) {} + + double dist(Point A) + { + return sqrt(pow(A.x - x, 2) + pow(A.y - y, 2)); + } + + double toLine(Point A, Point B) + { + double scale = ((x - A.x) * (B.x - A.x) + (y - A.y) * (B.y - A.y)) / + ((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y)); + return dist(Point(A.x + scale * (B.x - A.x), A.y + scale * (B.y - A.y))); + } + + double toSegment(Point A, Point B) + { + if ((x - A.x) * (B.x - A.x) + (y - A.y) * (B.y - A.y) <= eps) + return dist(A); + if ((x - B.x) * (A.x - B.x) + (y - B.y) * (A.y - B.y) <= eps) + return dist(B); + return toLine(A, B); + } +}; + +int main() +{ + int n, L, H; + while (cin >> n >> L >> H) + { + Point pa, pb; + int ya, yb; + double minn = 100000000.0; + for (int i = 0; i < n; i++) + { + cin >> ya >> pa.x >> pa.y; + int side = i & 1 ? L : 0; + int other_side = i & 1 ? 0 : L; + minn = min(minn, pa.toSegment(Point(other_side, 0), Point(other_side, H))); + if (i > 0) + { + minn = min(minn, pb.toSegment(Point(side, ya), pa)); + } + yb = ya; + pb = pa; + } + cout << fixed << setprecision(2) << minn << endl; + } + return 0; +} diff --git a/12484.cpp b/12484.cpp new file mode 100644 index 0000000..27109de --- /dev/null +++ b/12484.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int n, a[10005]; + long long dp[2][10005]; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); + memset(dp, 0, sizeof(dp)); + for (int i = 0; i < n; i++) + { + for (int j = 0; j + i < n; j++) + { + if ((i & 1) == 1) + dp[0][j] = max(dp[1][j + 1] + a[j], dp[1][j] + a[j + i]); + else + dp[1][j] = min(dp[0][j + 1], dp[0][j]); + } + } + printf("%lld\n", dp[0][0]); + } + return 0; +} diff --git a/12485.cpp b/12485.cpp new file mode 100644 index 0000000..02c44e8 --- /dev/null +++ b/12485.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +//12485 +//Perfect Choir +//Misc;Ad hoc + + +int T[10005]; + +int main() +{ + int n; + while (cin >> n) + { + memset(T, 0, sizeof(T)); + int total = 0; + for (int i = 0; i < n; i++) + { + int a; + cin >> a; + total += a; + T[i] = a; + } + + if (total % n != 0) + { + cout << -1 << endl; + continue; + } + + int media = total / n; + int maior = 0; + for (int i = 0; i < n; i++) + { + if (T[i] > media) + maior += T[i] - media; + } + + cout << maior + 1 << endl; + } +} diff --git a/12486.cpp b/12486.cpp new file mode 100644 index 0000000..5735da4 --- /dev/null +++ b/12486.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int main() +{ + unsigned long long n; + unsigned long long dp[30][2] = {}; + unsigned long long sum[30] = {}; + dp[0][0] = 1;// [1-9^3], dp[i][1] - [3(0-9)*] + for (int i = 1; i < 30; i++) + { + for (int j = 0; j < 2; j++) + { + dp[i][0] = 8 * dp[i - 1][0] + 7 * dp[i - 1][1]; + dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; + } + } + for (int i = 1; i < 30; i++) + sum[i] = sum[i - 1] + (dp[i - 1][0] + dp[i - 1][1]) * 7 + dp[i][1] - dp[i - 1][1]; + while (scanf("%llu", &n) == 1) + { + int len = 0; + for (len = 1; sum[len] <= n; len++) + ; + int prev = 0; + n -= sum[len - 1]; + for (int i = len; i >= 1; i--) + { + int last = -1; + for (int j = (i == len); j <= 9; j++) + { + if (j == 4 || (prev == 1 && j == 3)) + continue; + if (j == 1) + {// [1] - [0-9^3] + if (n > dp[i - 1][0]) + { + n -= dp[i - 1][0], last = j; + // printf("%llu test\n", dp[i-1][0]); + } + else + { + break; + } + } + else + { + if (n > dp[i - 1][0] + dp[i - 1][1]) + { + n -= dp[i - 1][0] + dp[i - 1][1], last = j; + // printf("%llu test\n", dp[i-1][0] + dp[i-1][1]); + } + else + break; + } + } + last++; + if (last == 4) + last++; + if (i == len && last == 0) + last = 1; + if (prev == 1 && last == 3) + last = 5; + printf("%d", last), prev = last; + } + puts(""); + } + return 0; +} +/* +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 + +*/ diff --git a/12487.cpp b/12487.cpp new file mode 100644 index 0000000..0d96166 --- /dev/null +++ b/12487.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +vector g[128]; +vector> Q[128];// query pair, v - input index. +int visited[128], F[128], depth[128]; +int LCA[128];//input query answer buffer. +int findF(int x) +{ + return F[x] == x ? x : (F[x] = findF(F[x])); +} +void tarjan(int u, int p, int d) +{// tree. + F[u] = u, depth[u] = d; + for (int i = 0; i < g[u].size(); i++) + {//son node. + int v = g[u][i]; + if (v == p) + continue; + tarjan(v, u, d + 1); + F[findF(v)] = u; + } + visited[u] = 1; + for (int i = 0; i < Q[u].size(); i++) + { + if (visited[Q[u][i].second]) + { + LCA[Q[u][i].first] = findF(Q[u][i].second); + } + } +} +int main() +{ + int N, A, B, C, x, y; + while (scanf("%d %d %d %d", &N, &A, &B, &C) == 4) + { + for (int i = 1; i <= N; i++) + g[i].clear(); + for (int i = 1; i < N; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(y); + g[y].push_back(x); + } + + for (int i = 1; i <= N; i++) + visited[i] = 0, Q[i].clear(); + Q[B].push_back(make_pair(0, C)); + Q[C].push_back(make_pair(0, B)); + tarjan(A, -1, 0); + + double ret = 0; + int lca = LCA[0]; + int lb = depth[B] - depth[lca], lc = depth[C] - depth[lca]; + if (lb == 0) + ret = 1; + else if (lc == 0) + ret = 0; + else + ret = lc / (double)(lb + lc); + printf("%lf\n", ret); + } + return 0; +} diff --git a/12488.cpp b/12488.cpp new file mode 100644 index 0000000..ccdfe1b --- /dev/null +++ b/12488.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int n, i, j, k, a[30], b[30]; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + scanf("%d", &a[i]); + for (i = 0; i < n; i++) + scanf("%d", &b[i]); + int ans = 0; + for (i = 0; i < n; i++) + { + for (j = i; j < n; j++) + { + if (b[i] == a[j]) + { + break; + } + } + for (k = j; k > i; k--) + { + a[k] = a[k - 1]; + ans++; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/12489.cpp b/12489.cpp new file mode 100644 index 0000000..4a4bfa7 --- /dev/null +++ b/12489.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int dfsMinExp(int u, int p, vector g[]) +{ + vector branch; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (v == p) + continue; + branch.push_back(dfsMinExp(v, u, g)); + } + sort(branch.begin(), branch.end()); + + // string exp = ""; + // for(int i = 0; i < branch.size(); i++) + // exp += branch[i]; + // return "(" + exp + ")"; + int a = 63689, b = 378551; + int ret = 0; + branch.push_back(1); + for (int i = 0; i < branch.size(); i++) + ret = ret * a + branch[i], a *= b; + return ret; +} +vector getTreeMinExp(vector g[], int n) +{ + if (n == 1) + return vector({1}); + int deg[10005] = {}, u, v; + int topo[10005] = {}, idx = 0; + queue Q; + for (int i = 1; i <= n; i++) + { + deg[i] = g[i].size(); + if (deg[i] == 1) + Q.push(i); + } + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + topo[idx++] = u; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (--deg[v] == 1) + Q.push(v); + } + } + vector ret; + ret.push_back(dfsMinExp(topo[idx - 1], -1, g)); + ret.push_back(dfsMinExp(topo[idx - 2], -1, g)); + return ret; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int n, x, y; + vector g1[65536], g2[65536]; + while (scanf("%d", &n) == 1) + { + for (int i = 1; i <= n; i++) + g1[i].clear(), g2[i].clear(); + for (int i = 1; i < n; i++) + { + scanf("%d %d", &x, &y); + g1[x].push_back(y); + g1[y].push_back(x); + } + for (int i = 1; i < n; i++) + { + scanf("%d %d", &x, &y); + g2[x].push_back(y); + g2[y].push_back(x); + } + vector hash1 = getTreeMinExp(g1, n); + vector hash2 = getTreeMinExp(g2, n); + int same = 0; + for (int i = 0; i < hash1.size(); i++) + for (int j = 0; j < hash2.size(); j++) + same |= hash1[i] == hash2[j]; + puts(same ? "S" : "N"); + // printf("%d %d\n", hash1, hash2); + } + return 0; +} +/* +8 +2 1 +3 2 +4 3 +5 2 +6 5 +7 5 +8 1 +2 1 +3 2 +4 2 +5 3 +6 1 +7 4 +8 3 +*/ diff --git a/1249.cpp b/1249.cpp new file mode 100644 index 0000000..03599a9 --- /dev/null +++ b/1249.cpp @@ -0,0 +1,605 @@ +#include + +using namespace std; + +typedef unsigned int uint; +typedef unsigned long long ull; + +#define FORE(k, a, b) for (int k = (a); k <= (int)(b); ++k) +#define FOR(k, a, b) for (int k = (a); k < (int)(b); ++k) + +#define pb push_back +#define mp make_pair + +template +bool isBetween(T x, T a, T b) +{ + return (a <= x && x <= b) || (b <= x && x <= a); +} + +template +ostream &operator<<(ostream &os, const vector &vec) +{ + FOR(i, 0, vec.size()) + { + os << vec[i] << endl; + } + return os; +} + +template +struct Point +{ + T x, y; + + Point(T xx, T yy) : x(xx), y(yy) {} + Point() : x(), y() {} + + template + operator Point() const + { + Point result((U)x, (U)y); + return result; + } +}; + +template +Point operator-(const Point &lhs, const Point &rhs) +{ + Point ret(lhs.x - rhs.x, lhs.y - rhs.y); + return ret; +} + +template +Point operator+(const Point &lhs, const Point &rhs) +{ + Point ret(lhs.x + rhs.x, lhs.y + rhs.y); + return ret; +} + +template +bool operator==(const Point &lhs, const Point &rhs) +{ + return lhs.x == rhs.x && lhs.y == rhs.y; +} + +template +bool operator!=(const Point &lhs, const Point &rhs) +{ + return lhs.x != rhs.x || lhs.y != rhs.y; +} + +template +Point operator/(const Point &lhs, const T &rhs) +{ + Point ret(lhs.x / rhs, lhs.y / rhs); + return ret; +} + +template +Point operator*(const Point &lhs, const T &rhs) +{ + Point ret(lhs.x * rhs, lhs.y * rhs); + return ret; +} + +template +Point operator*(const T &lhs, const Point &rhs) +{ + return rhs * lhs; +} + +template +ostream &operator<<(ostream &os, const Point &rhs) +{ + os << "(" << rhs.x << ", " << rhs.y << ")"; + return os; +} + +typedef Point PointD; +typedef Point PointI; +typedef pair SegmentD; +typedef vector vp; + +template +T dot(const Point &A, const Point &B) +{ + return A.x * B.x + A.y * B.y; +} + +template +T mag(const Point &v) +{ + return sqrt(v.x * v.x + v.y * v.y); +} + +template +T cross(const Point &A, const Point &B) +{ + return A.x * B.y - A.y * B.x; +} + +const double tol = 0.000002; + +// rot about origin ccw +template +Point rotate90(const Point &p) +{ + return Point(-p.y, p.x); +} + +// vect p1p2; p1p3 +// ret +// -1 if p1p3 is clockwise of p1p2 ( "right turn" ) +// +1 if p1p3 is counter-clockwise of p1p2 ( "left turn " ) +// 0 if parallel +template +int ccw(const Point &p1, + const Point &p2, const Point &p3) +{ + Point v1 = p2 - p1; + Point v2 = p3 - p1; + + T cp = cross(v1, v2); + + //v2 ccq to v1, so turn left + if (cp > 0) + return +1; + + // v2 cw of v1 + if (cp < 0) + return -1; + + // vect v1 and v2 colinear + return 0; +} + +template <> +int ccw(const Point &p1, + const Point &p2, const Point &p3) +{ + Point v1 = p2 - p1; + Point v2 = p3 - p1; + + double cp = cross(v1, v2); + + // vect v1 and v2 colinear + if (abs(cp) < tol) + { + return 0; + } + + // v2 ccw to v1, so turn left + if (cp > 0) + { + return +1; + } + + // v2 cw of v1 + if (cp < 0) + { + return -1; + } + + int up = 3; + throw up; +} + +template +T gcd(T a, T b) +{ + if (!numeric_limits::is_exact) + { + return min(abs(a), abs(b)); + } + + if (a == 0) + { + return b; + } + while (b != 0) + { + if (a > b) + { + a = a - b; + } + else + { + b = b - a; + } + } + return a; +} + +template +class Line +{ +public: + // ax + by + c = 0 + T A; + T B; + T C; + + Line(const Point &p1, const Point &p2) + { + assert(p1 != p2); + A = p1.y - p2.y; + B = p2.x - p1.x; + C = p1.x * p2.y - p2.x * p1.y; + + // make A positive + if (A < 0 || (A == 0 && B < 0)) + { + A *= -1; + B *= -1; + C *= -1; + } + + T gcdAB = gcd(abs(A), abs(B)); + T gcdABC = gcd(gcdAB, abs(C)); + + A /= gcdABC; + B /= gcdABC; + C /= gcdABC; + } +}; + +template +class Segment +{ +public: + // ax + by + c = 0 + Point p1; + Point p2; + + Segment(const Point &pp1 = Point(0, 0), const Point &pp2 = Point(0, 0)) + : p1(pp1), p2(pp2) + { + } +}; + +template +ostream &operator<<(ostream &os, const Segment &rhs) +{ + os << " " << rhs.p1 << " -- " << rhs.p2 << " "; + return os; +} + +template +ostream &operator<<(ostream &os, const Line &rhs) +{ + os << " " << rhs.A << "x + " << rhs.B << " y + " << rhs.C << " = 0"; + return os; +} + +template +bool operator<(const Line &lhs, const Line &rhs) +{ + if (lhs.A != rhs.A) + { + return lhs.A < rhs.A; + } + if (lhs.B != rhs.B) + { + return lhs.B < rhs.B; + } + return lhs.C < rhs.C; +} + +template +bool operator==(const Line &lhs, const Line &rhs) +{ + return (lhs.A == rhs.A) && + (lhs.B == rhs.B) && + lhs.C == rhs.C; +} + +template +class Circle +{ +public: + T x; + T y; + T r; + + Circle(T _x, T _y, T _r) : x(_x), y(_y), r(_r) + { + } + + int sgn(double x) + { + if (x < 0) + { + return -1; + } + return 1; + } + + bool getPointsIntersectingLine(const Point &p1, + const Point &p2, Point &i1, Point &i2) + { + // mv circle to origin + T x2 = p2.x - x; + T y2 = p2.y - y; + T x1 = p1.x - x; + T y1 = p2.y - y; + T dx = x2 - x1; + T dy = y2 - y1; + double dr = sqrt(dx * dx + dy * dy); + T D = x1 * y2 - x2 * y1; + double disc = r * r * dr * dr - D * D; + if (disc < 0) + { + return false; + } + double discSqRt = sqrt(disc); + i1.x = x + (D * dy + sgn(dy) * dx * discSqRt) / (dr * dr); + i1.y = y + (-D * dx + abs(dy) * discSqRt) / (dr * dr); + i2.x = x + (D * dy - sgn(dy) * dx * discSqRt) / (dr * dr); + i2.y = y + (-D * dx - abs(dy) * discSqRt) / (dr * dr); + return true; + } +}; + +template +bool isParallel(const Point &a1, const Point &a2, + const Point &b1, const Point &b2) +{ + T z = cross(a2 - a1, b2 - b1); + if (numeric_limits::is_exact && z == 0) + { + return true; + } + if (!numeric_limits::is_exact && abs(z) < tol) + { + return true; + } + return false; +} + +template +int getSide(const Point &A, const Point &B, const Point &P) +{ + T z = cross(B - A, P - A); + if (numeric_limits::is_exact) + { + if (z > 0) + { + return 1; + } + if (z < 0) + { + return -1; + } + } + else + { + if (z - tol > 0) + { + return 1; + } + if (z + tol < 0) + { + return -1; + } + } + return 0; +} + +template +bool collinear(const Point &p1, + const Point &p2, const Point &p3) +{ + if (p1 == p2 || p2 == p3) + { + return true; + } + return 0 == getSide(p1, p2, p3); +} + +template +bool intersects(const Point &a1, const Point &a2, + const Point &b1, const Point &b2) +{ + if (a1 == a2) + { + return isBetween(a1.x, b1.x, b2.x) && + isBetween(a1.y, b1.y, b2.y); + } + + if (getSide(a1, a2, b1) == getSide(a1, a2, b2)) + { + return false; + } + + if (b1 == b2) + { + return isBetween(b1.x, a1.x, a2.x) && + isBetween(b1.y, a1.y, a2.y); + } + + if (getSide(b1, b2, a1) == getSide(b1, b2, a2)) + { + return false; + } + + return true; +} + +// @ http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect +template +bool getIntersection(const Point &a1, const Point &a2, + const Point &b1, const Point &b2, PointD &inter) +{ + Point p = (PointD)a1; + Point r = a2 - a1; + + PointD q = b1; + PointD s = b2 - b1; + + // P + t*r intersects q + u*s + + double rCrossS = cross(r, s); + + if (abs(rCrossS) < tol) + { + return false; + } + + double t = cross(q - p, s / rCrossS); + + inter = p + t * r; + + if (t + tol < 0 || t - tol > 1) + { + return false; + } + + double u = cross(q - p, r / rCrossS); + + if (u + tol < 0 || u - tol > 1) + { + return false; + } + + return true; +} + +double dist(const PointD &p1, const PointD &p2) +{ + return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); +} + +bool cmp(const PointI &i, const PointI &j) +{ + return (i.x != j.x) ? i.x < j.x : i.y < j.y; +} + +typedef vector vecP; + +bool cmpLine(const vecP &v1, const vecP &v2) +{ + uint i = 0; + while (i < v1.size() && i < v2.size()) + { + if (v1[i] != v2[i]) + { + return cmp(v1[i], v2[i]); + } + ++i; + } + + return v1.size() < v2.size(); +} + +template +void minPair(pair &minPair, const pair &pair) +{ + minPair.first = min(minPair.first, pair.first); + minPair.second = min(minPair.second, pair.second); +} + +template +void maxPair(pair &maxPair, const pair &pair) +{ + maxPair.first = max(maxPair.first, pair.first); + maxPair.second = max(maxPair.second, pair.second); +} + +int cmpYX(const PointI &a, const PointI &b) +{ + if (a.y != b.y) + { + return a.y < b.y; + } + return a.x < b.x; +} + +template +struct PolarCmp +{ + Point origin; + + PolarCmp(const Point ori) : origin(ori) + { + } + + int operator()(const Point &a, const Point &b) + { + int isCCW = ccw(origin, a, b); + return isCCW == 1; + } +}; + +template +double area_poly(const vector> &points) +{ + double sum = 0; + for (int i = 0; i < points.size() - 1; ++i) + { + sum += points[i].x * points[i + 1].y; + } + + sum += points[points.size() - 1].x * points[0].y; + + for (int i = 0; i < points.size() - 1; ++i) + { + sum -= points[i + 1].x * points[i].y; + } + + sum -= points[0].x * points[points.size() - 1].y; + + return abs(sum) / 2; +} + +int main() +{ + while (1) + { + vector points, triangle; + FOR(i, 0, 6) + { + PointD pt; + cin >> pt.x >> pt.y; + if (i >= 3) + { + triangle.pb(pt); + } + else + { + points.pb(pt); + } + } + + if (collinear(triangle[0], triangle[1], triangle[2])) + { + break; + } + + double tArea = area_poly(triangle); + + PointD a = points[0]; + PointD b = points[1]; + PointD c = points[2]; + + double AB = dist(a, b); + double height = tArea / AB; + double angABC = acos(dot(c - a, b - a) / (mag(c - a) * mag(b - a))); + + // sin(ang) = height / hyp + // hyp = height / sin(ang) + double AH = height / sin(angABC); + + // unit vect AC + PointD vecAC = c - a; + double AC = dist(points[0], points[2]); + vecAC.x /= AC; + vecAC.y /= AC; + + PointD h = points[0] + vecAC * AH; + PointD g = points[1] + vecAC * AH; + + printf("%.3lf %.3lf %.3lf %.3lf\n", g.x, g.y, h.x, h.y); + } + return 0; +} diff --git a/12490.cpp b/12490.cpp new file mode 100644 index 0000000..648b6f4 --- /dev/null +++ b/12490.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define MAX 1000006 +#define ull long long + +struct Value +{ + int x; + ull v; + + inline bool operator<(const Value &a) const + { + return this->x < a.x; + } +}; + +Value F[MAX]; + +int main() +{ + int n, s, y; + while (cin >> n >> s >> y) + { + for (int i = 0; i < s; i++) + { + cin >> F[i].x >> F[i].v; + } + sort(F, F + s); + double minn = 0, maxx = 0; + for (int i = 0; i < s - 1; i++) + { + Value a = F[i], b = F[i + 1]; + minn += min(a.v, b.v) + (b.x - a.x - 1) * min(a.v, b.v) + abs(a.v - b.v) / 2.0; + maxx += min(a.v, b.v) + (b.x - a.x - 1) * max(a.v, b.v) + abs(a.v - b.v) / 2.0; + } + if (y < minn || y > maxx || ceil(minn) != minn) + { + cout << "N" << endl; + continue; + } + cout << "S"; + ull current = round(maxx); + for (int i = 0; i < s - 1; i++) + { + Value a = F[i], b = F[i + 1]; + ull delta = (b.x - a.x - 1) * (max(a.v, b.v) - min(a.v, b.v)); + if (current == y) + { + for (int x = a.x + 1; x < b.x; x++) + { + cout << " " << max(a.v, b.v); + } + } + else if (current - delta > y) + { + current -= delta; + for (int x = a.x + 1; x < b.x; x++) + { + cout << " " << min(a.v, b.v); + } + } + else if (a.v < b.v) + { + for (int x = a.x + 1; x < b.x; x++) + { + ull value = max(a.v, b.v - (current - y)); + cout << " " << value; + current -= b.v - value; + } + } + else + { + ull plus = (current - y) / (b.x - a.x - 1); + ull rem = (current - y) % (b.x - a.x - 1); + for (int x = a.x + 1; x < b.x; x++) + { + ull value = a.v - plus - (b.x - x <= rem ? 1 : 0); + cout << " " << value; + current -= a.v - value; + } + } + } + cout << endl; + } + return 0; +} diff --git a/12491.cpp b/12491.cpp new file mode 100644 index 0000000..bac87f1 --- /dev/null +++ b/12491.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +int N1, N2; +string A[50], B[50]; +set sA, sB;// state +int calcPrefix(string a, string b) +{ + int la = a.length(), lb = b.length(); + int i; + for (i = 0; i < la && i < lb; i++) + if (a[i] != b[i]) + return -1; + return i; +} +int dfs(string a, string b) +{ + if (sA.find(a) != sA.end() || sB.find(b) != sB.end()) + return 0; + if (a.length() == 0 && b.length() == 0) + return 1; + int found = 0, i, prefix; + if (a.length()) + { + sA.insert(a); + for (i = 0; i < N2 && !found; i++) + { + prefix = calcPrefix(a, B[i]); + if (prefix == -1) + continue; + if (prefix == a.length()) + found |= dfs("", B[i].substr(prefix)); + else + found |= dfs(a.substr(prefix), ""); + } + } + else + { + sB.insert(b); + for (i = 0; i < N1 && !found; i++) + { + prefix = calcPrefix(b, A[i]); + if (prefix == -1) + continue; + if (prefix == b.length()) + found |= dfs(A[i].substr(prefix), ""); + else + found |= dfs("", b.substr(prefix)); + } + } + return found; +} +int main() +{ + int i; + char s[50]; + while (scanf("%d %d", &N1, &N2) == 2) + { + for (i = 0; i < N1; i++) + { + scanf("%s", s); + A[i] = s; + } + for (i = 0; i < N2; i++) + { + scanf("%s", s); + B[i] = s; + } + sA.clear(), sB.clear(); + int found = 0; + for (i = 0; i < N1 && !found; i++) + { + found |= dfs(A[i], ""); + } + puts(found ? "S" : "N"); + } + return 0; +} diff --git a/12492.cpp b/12492.cpp new file mode 100644 index 0000000..a4f51c1 --- /dev/null +++ b/12492.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +//12492 +//Rubik Cycle +//Misc;Ad hoc +#define MAX 200010 + + +int T[54]; + +void rotate(int a, int b, int c, int d, int e, int f, int g, int h) +{ + int x = T[h], y = T[g]; + T[h] = T[f]; + T[g] = T[e]; + + T[f] = T[d]; + T[e] = T[c]; + + T[d] = T[b]; + T[c] = T[a]; + + T[b] = x; + T[a] = y; +} + +void adjust(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l) +{ + int x = T[j], y = T[k], z = T[l]; + + T[j] = T[g]; + T[k] = T[h]; + T[l] = T[i]; + + T[g] = T[d]; + T[h] = T[e]; + T[i] = T[f]; + + T[d] = T[a]; + T[e] = T[b]; + T[f] = T[c]; + + T[a] = x; + T[b] = y; + T[c] = z; +} + +void F() +{ + rotate(0, 1, 2, 5, 8, 7, 6, 3); + adjust(33, 34, 35, 45, 48, 51, 11, 10, 9, 44, 41, 38); +} + +void B() +{ + rotate(26, 25, 24, 21, 18, 19, 20, 23); + adjust(29, 28, 27, 36, 39, 42, 15, 16, 17, 53, 50, 47); +} + +void L() +{ + rotate(36, 37, 38, 41, 44, 43, 42, 39); + adjust(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33); +} + +void R() +{ + rotate(45, 46, 47, 50, 53, 52, 51, 48); + adjust(8, 5, 2, 35, 32, 29, 26, 23, 20, 17, 14, 11); +} + +void U() +{ + rotate(27, 28, 29, 32, 35, 34, 33, 30); + adjust(2, 1, 0, 38, 37, 36, 24, 25, 26, 47, 46, 45); +} + +void D() +{ + rotate(9, 10, 11, 14, 17, 16, 15, 12); + adjust(6, 7, 8, 51, 52, 53, 20, 19, 18, 42, 43, 44); +} + +bool ok() +{ + for (int i = 0; i < 54; i++) + { + if (T[i] != i) + return false; + } + return true; +} + +int main() +{ + string s; + while (cin >> s) + { + for (int i = 0; i < 54; i++) + T[i] = i; + + int result = 0; + do + { + for (int i = 0; i < s.size(); i++) + { + switch (s[i]) + { + case 'F': + F(); + break; + case 'B': + B(); + break; + case 'R': + R(); + break; + case 'L': + L(); + break; + case 'U': + U(); + break; + case 'D': + D(); + break; + case 'f': + F(); + F(); + F(); + break; + case 'b': + B(); + B(); + B(); + break; + case 'r': + R(); + R(); + R(); + break; + case 'l': + L(); + L(); + L(); + break; + case 'u': + U(); + U(); + U(); + break; + case 'd': + D(); + D(); + D(); + break; + } + } + result++; + } while (!ok()); + cout << result << endl; + } +} diff --git a/12493.cpp b/12493.cpp new file mode 100644 index 0000000..78b3f5c --- /dev/null +++ b/12493.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +typedef long long LL; + +const int inf = 0x3f3f3f3f; +const double pi = acos(-1.0); +const double esp = 1e-6; + +LL Euler(LL n) +{ + LL ans = n; + for (LL i = 2; i * i <= n; i++) + { + if (n % i == 0) + { + ans = ans / i * (i - 1); + while (n % i == 0) + n /= i; + } + } + if (n > 1) + ans = ans / n * (n - 1); + return ans; +} + +int main() +{ + LL n; + while (~scanf("%lld", &n)) + { + printf("%lld\n", Euler(n) / 2); + } + return 0; +} diff --git a/12494.cpp b/12494.cpp new file mode 100644 index 0000000..fd16e35 --- /dev/null +++ b/12494.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +typedef long long ll; + +ll hash_(int n, const char *s) +{ + const ll P = 1000000007; + ll Ppow = 1; + rep(i, n) Ppow *= P; + ll h = 0; + rep(i, n) h = h * P + s[i]; + ll res = h; + rep(i, n) + { + res = min(res, h); + h *= P; + h -= Ppow * s[i]; + h += s[i]; + } + return res; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + char s[256]; + scanf("%s", s); + int n = strlen(s); + set S; + rep(i, n) for (int j = i; j < n; j++) + { + int len = j - i + 1; + S.insert(hash_(len, s + i)); + } + printf("%d\n", S.size()); + } + return 0; +} diff --git a/12495.cpp b/12495.cpp new file mode 100644 index 0000000..d8937c1 --- /dev/null +++ b/12495.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const int M = 1000000007; + +int main() +{ + static int dp[10][1001][1001]; + for (int d = 1; d <= 10; d++) + { + rep(n, 1001) dp[d - 1][n][0] = (n == 0 ? d : 1); + rep(n, 1000) rep(r, n + 1) dp[d - 1][n + 1][r + 1] = (dp[d - 1][n][r + 1] + dp[d - 1][n][r]) % M; + } + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + int n, k, d; + scanf("%d%d%d", &n, &k, &d); + printf("Case %d: %d\n", cas, k == 1 ? n : dp[d - 1][max(n - (d - 1) * k, 0)][k]); + } + return 0; +} diff --git a/12496.cpp b/12496.cpp new file mode 100644 index 0000000..9cdb6af --- /dev/null +++ b/12496.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + int h, w, K; + scanf("%d%d%d", &h, &w, &K); + static int a[200][200]; + rep(i, h) rep(j, w) scanf("%d", a[i] + j); + static int sum[201][201]; + rep(i, h) rep(j, w) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + a[i][j]; + int cnt = 0; + rep(i1, h) for (int i2 = i1 + 1; i2 <= h; i2++) + { + for (int j1 = 0, j2 = 1; j1 < w; j1++) + { + while (j2 <= w && sum[i2][j2] - sum[i2][j1] - sum[i1][j2] + sum[i1][j1] < K) + { + j2++; + } + cnt += w + 1 - j2; + } + } + int cnt2 = h * (h + 1) / 2 * w * (w + 1) / 2; + int g = gcd(cnt, cnt2); + printf("Case %d: %d/%d\n", cas, cnt / g, cnt2 / g); + } + return 0; +} diff --git a/12497.cpp b/12497.cpp new file mode 100644 index 0000000..3940b11 --- /dev/null +++ b/12497.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +typedef long long LL; +#define two(i) (1 << (i)) +int g[20][20]; +int cnt[two(13)]; +LL dp[two(13)][13], use[two(13)]; + +int main() +{ + for (int i = 0; i < two(13); i++) + { + cnt[i] = 0; + for (int t = i; t > 0; t -= t & -t) + { + cnt[i]++; + } + } + int T, cas = 0; + scanf("%d", &T); + while (T--) + { + int n, m, x, y; + scanf("%d%d", &n, &m); + memset(g, 0, sizeof(g)); + for (int i = 0; i < m; i++) + { + scanf("%d%d", &x, &y); + g[x][y] = g[y][x] = 1; + } + LL ret = 0; + for (int i = 0; i < n; i++) + { + for (int s = 0; s < two(n); s++) + for (int j = 0; j < n; j++) + { + dp[s][j] = 0; + } + dp[two(i)][i] = 1; + for (int s = 0; s < two(n); s++) + for (int j = 0; j < n; j++) + if (dp[s][j]) + for (int k = 0; k < n; k++) + if (!(two(k) & s) && g[j][k]) + { + dp[two(k) | s][k] += dp[s][j]; + } + for (int s = 0; s < two(n); s++) + { + use[s] = 0; + for (int j = 0; j < n; j++) + if (dp[s][j] && g[j][i]) + { + use[s] += dp[s][j]; + } + use[s] /= 2; + // if(use[s]) + // printf("==%d %d\n",i,s); + } + for (int s = 1; s < two(n); s++) + { + if (s & two(i)) + { + continue; + } + LL t = 0; + for (int ns = s; ns > 0; ns = (ns - 1) & s) + if (cnt[s ^ ns] >= 2 && cnt[ns] >= 2) + { + t += use[s ^ ns ^ two(i)] * use[ns ^ two(i)]; + } + ret += t; + } + } + printf("Case %d: %lld\n", ++cas, ret / 2); + } + return 0; +} diff --git a/12498.cpp b/12498.cpp new file mode 100644 index 0000000..b92cf3c --- /dev/null +++ b/12498.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +char s[64][64]; + +int main() +{ + int t; + scanf("%d", &t); + for (int r, c, cnum(0); cnum++ < t && scanf("%d %d", &r, &c) == 2;) + { + bool possible = true; + for (int i = 0; i < r; ++i) + { + scanf("%s", s[i]); + bool has_zero = false; + for (int j = 0; !has_zero && j < c; ++j) + { + has_zero |= s[i][j] == '0'; + } + possible &= has_zero; + } + printf("Case %d: ", cnum); + if (!possible) + { + puts("-1"); + } + else + { + int min_moves = r * c, moves; + int cL, cR, dL, dR; + for (int col = 0; col < c; ++col) + { + moves = 0; + for (int row = 0; row < r; ++row) + { + dL = dR = 128; + for (cL = col; cL >= 0 && s[row][cL] == '1'; --cL) + ; + if (cL != -1) + { + dL = col - cL; + } + for (cR = col; cR < c && s[row][cR] == '1'; ++cR) + ; + if (cR != c) + { + dR = cR - col; + } + moves += min(dL, dR); + } + min_moves = min(min_moves, moves); + } + printf("%d\n", min_moves); + } + } + return 0; +} diff --git a/12499.cpp b/12499.cpp new file mode 100644 index 0000000..89d41bf --- /dev/null +++ b/12499.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int x, y, i, cases, j; + int arr[51]; + scanf("%d", &cases); + for (i = 1; i < cases + 1; i++) + { + scanf("%d %d", &x, &y); + for (j = 0; j < x; j++) + { + scanf("%d", &arr[j]); + } + arr[x] = y; + int temp = 0; + for (j = 0; j < x; j += 2) + { + temp ^= arr[j + 1] - arr[j]; + } + printf("Case %d: ", i); + if (temp != 0) + { + printf("First Player\n"); + } + else + { + printf("Second Player\n"); + } + } + return 0; +} diff --git a/125.cpp b/125.cpp new file mode 100644 index 0000000..fcd2b09 --- /dev/null +++ b/125.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define MAXN 100 + +int Max; +struct ss +{ + int path; +} M[MAXN][MAXN]; + +void Floyd() +{ + int i, j, k, temp; + for (k = 0; k <= Max; k++) + { + for (i = 0; i <= Max; i++) + { + for (j = 0; j <= Max; j++) + { + temp = M[i][k].path * M[k][j].path; + if (temp) + { + if (M[i][k].path < 0 || M[k][j].path < 0) + { + M[i][j].path = -1; + } + else + { + M[i][j].path += temp; + } + } + if (M[i][j].path && M[j][i].path) + { + M[i][j].path = M[j][i].path = -1; + } + } + } + } +} + +void Cal() +{ + int i, j; + Floyd(); + for (i = 0; i <= Max; i++) + { + printf("%d", M[i][0]); + for (j = 1; j <= Max; j++) + { + printf(" %d", M[i][j]); + } + printf("\n"); + } +} + +int main() +{ + int i, u, v, n, j, m = 0; + while (scanf("%d", &n) == 1) + { + Max = -1; + for (i = 0; i < n; i++) + { + scanf("%d%d", &u, &v); + M[u][v].path = 1; + if (u > Max) + { + Max = u; + } + if (v > Max) + { + Max = v; + } + } + printf("matrix for city %d\n", m++); + Cal(); + for (i = 0; i <= Max; i++) + { + for (j = 0; j <= Max; j++) + { + M[i][j].path = 0; + } + } + } + return 0; +} diff --git a/1250.cpp b/1250.cpp new file mode 100644 index 0000000..0e8dd6c --- /dev/null +++ b/1250.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define MAX 1 << 29 + +typedef struct List { double x, y, p; } LIST; +LIST lst[1005]; +double dp[1005], pre[1005]; + +double dist(double a, double b, double c, double d) { + return hypot(a - c, b - d); +} + +int main() { + int N, n, i, j; + while (~scanf(" %d", &N) && N) { + memset(pre, 0, sizeof(pre)); + for (i = 0; i < 1005; i++) { + dp[i] = 999999999.9; + } + for (n = 1; n <= N; n++) { + scanf(" %lf %lf %lf", &lst[n].x, &lst[n].y, &lst[n].p); + pre[n] = pre[n - 1] + lst[n].p; + } + N++; + lst[N].x = 100.000; + lst[N].y = 100.000; + lst[N].p = 0.000; + lst[0].x = 0.000; + lst[0].y = 0.000; + lst[0].p = 0.000; + dp[1] = dist(0, 0, lst[1].x, lst[1].y) + 1; + dp[0] = 0.000; + for (i = 2; i <= N; i++) { + for (j = i - 1; j >= 0; j--) { + double tmp = dist(lst[i].x, lst[i].y, lst[j].x, lst[j].y); + dp[i] = min(dp[i], dp[j] + tmp + pre[i - 1] - pre[j]); + } + dp[i] += 1.00000; + } + printf("%.3lf\n", dp[N]); + } + return 0; +} diff --git a/12501.cpp b/12501.cpp new file mode 100644 index 0000000..0fb18d7 --- /dev/null +++ b/12501.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 +typedef long long ll; + +const int maxn = 100005; +ll sum1[maxn << 2], sum2[maxn << 2], late[maxn << 2]; + +void up(int rt) +{ + int ls = rt << 1, rs = rt << 1 | 1; + sum1[rt] = sum1[ls] + sum1[rs]; + sum2[rt] = sum2[ls] + sum2[rs]; +} + +void down(int rt, int l, int r) +{ + if (late[rt]) + { + int ls = rt << 1, rs = rt << 1 | 1; + int m = (l + r) >> 1; + late[ls] += late[rt]; + late[rs] += late[rt]; + sum1[ls] += late[rt] * (m - l + 1); + sum1[rs] += late[rt] * (r - m); + sum2[ls] += late[rt] * ((ll)l + m) * (m - l + 1) / 2; + sum2[rs] += late[rt] * ((ll)m + 1 + r) * (r - m) / 2; + late[rt] = 0; + } +} + +void build(int l, int r, int rt) +{ + late[rt] = 0; + if (l == r) + { + sum1[rt] = 100; + sum2[rt] = 100 * l; + return; + } + int m = (l + r) >> 1; + build(lson); + build(rson); + up(rt); +} + +void update(int L, int R, ll key, int l, int r, int rt) +{ + if (L <= l && r <= R) + { + late[rt] += key; + sum1[rt] += key * (r - l + 1); + sum2[rt] += key * ((ll)l + r) * (r - l + 1) / 2; + return; + } + int m = (l + r) >> 1; + down(rt, l, r); + if (L <= m) + update(L, R, key, lson); + if (m < R) + update(L, R, key, rson); + up(rt); +} + +ll query(int L, int R, int l, int r, int rt) +{ + if (L <= l && r <= R) + return sum2[rt] - sum1[rt] * (L - 1); + int m = (l + r) >> 1; + ll ret = 0; + down(rt, l, r); + if (L <= m) + ret += query(L, R, lson); + if (m < R) + ret += query(L, R, rson); + return ret; +} + +int main() +{ + int T; + char op[10]; + scanf("%d", &T); + for (int cc = 1; cc <= T; cc++) + { + int n, m; + scanf("%d%d", &n, &m); + build(1, n, 1); + printf("Case %d:\n", cc); + while (m--) + { + scanf("%s", op); + if (!strcmp(op, "query")) + { + int a, b; + scanf("%d%d", &a, &b); + printf("%lld\n", query(a, b, 1, n, 1)); + } + else + { + int a, b; + ll c; + scanf("%d%d%lld", &a, &b, &c); + update(a, b, c, 1, n, 1); + } + } + } + return 0; +} diff --git a/12502.cpp b/12502.cpp new file mode 100644 index 0000000..ab25d37 --- /dev/null +++ b/12502.cpp @@ -0,0 +1,11 @@ +#include + +using namespace std; + +int main() +{ + scanf("%*d"); + for (int x, y, z; scanf("%d%d%d", &x, &y, &z) == 3; printf("%d\n", z * (2 * x - y) / (x + y))) + ; + return 0; +} diff --git a/12503.cpp b/12503.cpp new file mode 100644 index 0000000..b7c6ce2 --- /dev/null +++ b/12503.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() +{ + char a[30]; + int t, n, s, e, i, b[110]; + while (scanf("%d", &t) == 1) + { + while (t--) + { + scanf("%d", &n); + s = 0; + for (i = 1; i <= n; i++) + { + scanf("%s", a); + if (strcmp(a, "LEFT") == 0) + { + s -= 1; + b[i] = -1; + } + else if (strcmp(a, "RIGHT") == 0) + { + s += 1; + b[i] = 1; + } + else + { + scanf("%s%d", a, &e); + s += b[e]; + b[i] = b[e]; + } + } + printf("%d\n", s); + } + } + return 0; +} diff --git a/12504.cpp b/12504.cpp new file mode 100644 index 0000000..215b070 --- /dev/null +++ b/12504.cpp @@ -0,0 +1,228 @@ +#include + +using namespace std; + +#define SET(a) memset(a, -1, sizeof(a)) +#define ALL(a) a.begin(), a.end() +#define CLR(a) memset(a, 0, sizeof(a)) +#define PB push_back +#define PI acos(-1.0) +#define MP(a, b) make_pair(a, b) +#define PII pair +#define PCC pair +#define PIC pair +#define PCI pair +#define VS vector +#define VI vector +#define VC vector +#define max3(a, b, c) max(a,max(b,c) +#define min3(a, b, c) min(a,min(b,c) +#define LL long long +#define FOR(i, a, b) for (int i = a; i <= b; i++) +#define RFOR(i, a, b) for (int i = a; i >= b; i--) +#define For(i, a) for (int i = 0; i < a; i++) + +#define S(a) scanf("%d", &a) +#define S2(a, b) scanf("%d%d", &a, &b) +#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c) +#define SLL(a) scanf("%lld", &a) +#define SLL2(a, b) scanf("%lld%lld", &a, &b) +#define SLL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c) +#define SS(a) scanf("%s", a) +#define SC(a) scanf("%c", &a) +#define SD(a) scanf("%lf", &a) +#define P(a) printf("%d", a) +#define PLL(a) printf("%lld", a) +#define PD(a) printf("%lf", a) +#define PC(a) printf("%c", a) +#define PS(a) printf("%s", a) +#define KS printf("Case %d: ", kk++) +#define KN printf("Case %d:\n", kk++) +#define KH printf("Case #%d: ", kk++) +#define NL printf("\n") +#define DB cout << "done" << endl; + +#define EPS 1e-9 +#define MOD 1000000007 +#define INF INT_MAX / 3 +#define MX 100010 + +template +inline string tostring(T a) +{ + ostringstream os(""); + os << a; + return os.str(); +} +template +inline LL tolong(T a) +{ + LL res; + istringstream os(a); + os >> res; + return res; +} +template +inline VI parse(T str) +{ + VI res; + int s; + istringstream os(str); + while (os >> s) + { + res.PB(s); + } + return res; +} +template +inline T _sqrt(T x) +{ + return (T)sqrt((double)x); +} +template +inline T _bigmod(T n, T m) +{ + T ans = 1, mult = n % MOD; + while (m) + { + if (m & 1) + { + ans = (ans * mult) % MOD; + } + m >>= 1; + mult = (mult * mult) % MOD; + } + ans %= MOD; + return ans; +} +template +inline T _modinv(T x) +{ + return _bigmod(x, (T)MOD - 2) % MOD; +} +inline int LEN(string a) +{ + return a.length(); +} +inline int LEN(char a[]) +{ + return strlen(a); +} +template +inline T _gcd(T a, T b) +{ + return (b.chk() == 0) ? a : _gcd(b, a % b); +} +template +inline T _lcm(T x, T y) +{ + return x * y / _gcd(x, y); +} + +int main() +{ + int tc, kk = 1, n, m, s1l, s2l, cnt1, cnt2, cnt3, cnt; + cin >> tc; + string s1, s2, tmp1, tmp2, total[210], pos[110], neg[110], chg[110]; + while (tc--) + { + cin >> s1 >> s2; + s1l = LEN(s1); + s2l = LEN(s2); + cnt = 0; + map key1, key2; + map val1, val2; + FOR(i, 1, s1l - 2) + { + tmp1 = ""; + while (s1[i] != ':') + { + tmp1 += s1[i]; + i++; + } + total[cnt++] = tmp1; + key1[tmp1] = 1; + i++; + tmp2 = ""; + while (s1[i] != ',' && s1[i] != '}') + { + tmp2 += s1[i]; + i++; + } + val1[tmp1] = tmp2; + } + FOR(i, 1, s2l - 2) + { + tmp1 = ""; + while (s2[i] != ':') + { + tmp1 += s2[i]; + i++; + } + key2[tmp1] = 1; + if (key1[tmp1] != 1) + { + total[cnt++] = tmp1; + } + i++; + tmp2 = ""; + while (s2[i] != ',' && s2[i] != '}') + { + tmp2 += s2[i]; + i++; + } + val2[tmp1] = tmp2; + } + cnt1 = 0; + cnt2 = 0; + cnt3 = 0; + For(i, cnt) + { + if (key1[total[i]] == 1 && key2[total[i]] != 1) + { + neg[cnt1++] = total[i]; + } + else if (key1[total[i]] != 1 && key2[total[i]] == 1) + { + pos[cnt2++] = total[i]; + } + else if (val1[total[i]] != val2[total[i]]) + { + chg[cnt3++] = total[i]; + } + } + sort(pos, pos + cnt2); + sort(neg, neg + cnt1); + sort(chg, chg + cnt3); + if (!(cnt1 | cnt2 | cnt3)) + { + cout << "No changes" << endl; + } + else + { + if (cnt2) + { + cout << "+" << pos[0]; + FOR(i, 1, cnt2 - 1) + cout << "," << pos[i]; + cout << endl; + } + if (cnt1) + { + cout << "-" << neg[0]; + FOR(i, 1, cnt1 - 1) + cout << "," << neg[i]; + cout << endl; + } + if (cnt3) + { + cout << "*" << chg[0]; + FOR(i, 1, cnt3 - 1) + cout << "," << chg[i]; + cout << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/12505.cpp b/12505.cpp new file mode 100644 index 0000000..4776726 --- /dev/null +++ b/12505.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +short bin[160], mbin[320], obin[320]; +int check(int st) +{ + int i, j; + memset(mbin, 0, sizeof(mbin)); + for (i = st; i < 160; i++) + { + if (bin[i]) + for (j = st; j < 160; j++) + mbin[i + j] += bin[i] & bin[j]; + } + for (i = 0; i < 320; i++) + { + if (mbin[i] >= 2) + { + mbin[i + 1] += mbin[i] >> 1; + mbin[i] &= 1; + } + } + /*for(i = 319; i >= 0; i--) + printf("%d", mbin[i]); + puts(""); + for(i = 319; i >= 0; i--) + printf("%d", obin[i]); + puts("");*/ + for (i = 319; i >= 0; i--) + if (mbin[i] > obin[i]) + return 0; + else if (mbin[i] < obin[i]) + return 1; + return 1; +} +int main() +{ + scanf("%*d"); + int n, i, j; + char s[50]; + while (scanf("%d %s", &n, s) == 2) + { + int sq = sqrt(n); + memset(bin, 0, sizeof(bin)); + memset(obin, 0, sizeof(obin)); + for (i = 10; i >= 0; i--) + bin[150 - (10 - i)] = (sq >> i) & 1; + for (i = 25; i >= 0; i--) + obin[305 - (25 - i)] = (n >> i) & 1; + /*for(i = 150; i >= 140; i--) + printf("%d", bin[i]); + puts("");*/ + for (i = 139; i >= 0; i--) + { + bin[i] = 1; + if (check(i)) + { + } + else + bin[i] = 0; + } + for (i = 139; i >= 0; i--) + { + for (j = 0; s[j]; j++) + if (s[j] - '0' != bin[i - j]) + break; + if (s[j] == '\0') + { + printf("%d\n", 139 - i); + break; + } + } + /*printf("."); + for(i = 139; i >= 0; i--) + printf("%d", bin[i]); + puts("");*/ + } + return 0; +} diff --git a/12506.cpp b/12506.cpp new file mode 100644 index 0000000..76c1aa2 --- /dev/null +++ b/12506.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +struct Trie +{ + bool n; + int link[26], cnt; +} Node[1048576]; + +int TrieSize; + +int insertTrie(const char *str) +{ + static int i, idx; + idx = 0; + for (i = 0; str[i]; i++) + { + if (!Node[idx].link[str[i] - 'a']) + { + TrieSize++; + memset(&Node[TrieSize], 0, sizeof(Node[0])); + Node[idx].link[str[i] - 'a'] = TrieSize; + } + Node[idx].cnt++; + idx = Node[idx].link[str[i] - 'a']; + } + Node[idx].n = true; + return 0; +} +long long ans; +void dfs(int nd, int dep) +{ + int i; + for (i = 0; i < 26; i++) + { + if (Node[nd].link[i]) + { + dfs(Node[nd].link[i], dep + 1); + if (Node[nd].cnt != 1 && Node[Node[nd].link[i]].cnt == 1) + { + ans += dep; + } + } + } +} + +char str[10000]; + +int main() +{ + int t, n; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + TrieSize = 0; + memset(&Node[0], 0, sizeof(Node[0])); + while (n--) + { + scanf("%s", str); + insertTrie(str); + } + ans = 0; + dfs(0, 1); + printf("%lld\n", ans); + } + return 0; +} diff --git a/12507.cpp b/12507.cpp new file mode 100644 index 0000000..9fc18b4 --- /dev/null +++ b/12507.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int u[110], v[110], w[110], g[20], vis[20]; +int r[110], p[20]; +int find(int x) +{ + return p[x] == x ? x : p[x] = find(p[x]); +} +bool cmp(int i, int j) +{ + return w[i] < w[j]; +} +inline int Kruskal(int n, int m, int h) +{ + for (int i = 0; i < m; i++) + { + r[i] = i; + } + for (int i = 0; i <= n; i++) + { + p[i] = i; + } + sort(r, r + m, cmp); + int ans = 0; + int d = 1; + for (int i = 0; i < m; i++) + { + int e = r[i]; + if (vis[u[e]] && vis[v[e]]) + { + int x = find(u[e]); + int y = find(v[e]); + if (x != y) + { + ans += w[e]; + p[x] = y; + d++; + } + } + } + if (d < h) + { + ans = 1000000; + } + return ans; +} +int main() +{ + int t, m, n, k; + scanf("%d", &t); + while (t--) + { + scanf("%d%d%d", &n, &m, &k); + for (int i = 1; i <= n; i++) + { + scanf("%d", &g[i]); + } + for (int i = 0; i < m; i++) + { + scanf("%d%d%d", &u[i], &v[i], &w[i]); + } + int maxx = g[1]; + for (int i = 1; i < (1 << n); i++) + { + if (i % 2 == 1) + { + memset(vis, 0, sizeof(vis)); + int x = 0, h = 0; + ; + for (int j = 0; j < n; j++) + { + if (i & (1 << j)) + { + vis[j + 1] = 1; + x += g[j + 1]; + h++; + } + } + if (x > maxx) + { + //printf("%d*****%d\n",i,x); + int c = Kruskal(n, m, h); + // printf("%d\n",c); + if (c <= k) + { + maxx = x; + } + } + } + } + printf("%d\n", maxx); + } + return 0; +} diff --git a/12508.cpp b/12508.cpp new file mode 100644 index 0000000..9b1192b --- /dev/null +++ b/12508.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +typedef long long LL; +int N, M, A, B; + +LL deal(int limit) +{ + LL ans = 0; + for (int n = 1; n <= N; n++) + for (int m = 1; m <= M; m++) + { + LL cnt = 0; + int y1, y2; + if (m * n <= limit) + { + cnt += 2 * (m - 1 + n - 1); + } + for (int x = 0; x <= n; x++) + { + y2 = (m * x + limit) / n; + if (y2 > m) + { + y2 = m; + } + int t = m * x - limit; + if (t <= 0) + { + y1 = 0; + } + else + { + y1 = (t - 1) / n + 1; + } + if (y1 <= y2) + { + cnt += 2 * (y2 - y1 + 1); + } + } + for (int x = 1; x < n; x++) + { + y2 = (m * n + limit) / x; + if (y2 >= m) + { + y2 = m - 1; + } + int t = m * n - limit; + if (t <= 0) + { + y1 = 1; + } + else + { + y1 = (t - 1) / x + 1; + } + if (y1 <= y2) + { + cnt += 4 * (y2 - y1 + 1); + } + } + ans += cnt * (N - n + 1) * (M - m + 1); + } + return ans; +} + +void solve() +{ + A <<= 1, B <<= 1; + if (A == 0) + { + A = 1; + } + printf("%lld\n", deal(B) - deal(A - 1)); +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + scanf("%d%d%d%d", &N, &M, &A, &B); + if (N > M) + { + std::swap(N, M); + } + solve(); + } + return 0; +} diff --git a/12509.cpp b/12509.cpp new file mode 100644 index 0000000..8483bec --- /dev/null +++ b/12509.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +const int N = 110; +const double EPS = 1e-10; + +int sig(double x) +{ + return (x > EPS) - (x < -EPS); +} + +struct Line +{ + double x1, x2, y1, y2; + void init(double xx1 = 0, double yy1 = 0, double xx2 = 0, double yy2 = 0) + { + x1 = xx1; + x2 = xx2; + y1 = yy1; + y2 = yy2; + } +}; + +struct node +{ + bool f; + double dis; +} map_[N * 2][N * 2][2]; + +int a = 0, b = 0; +Line lin[N]; +bool vis[N * 2][N * 2][2], vas[N * 2][N * 2]; +double X[N * 2], Y[N * 2], ans = 0.0f; + +void read(int n) +{ + a = b = 0; + double x1, x2, y1, y2; + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); + lin[i].init(x1, y1, x2, y2); + X[a++] = x1; + X[a++] = x2; + Y[b++] = y1; + Y[b++] = y2; + } + sort(X, X + a); + sort(Y, Y + b); + int i = 0; + for (int j = 1; j < a; j++) + { + if (sig(X[j] - X[i]) != 0) + { + X[++i] = X[j]; + } + } + a = i + 1; + i = 0; + for (int j = 1; j < b; j++) + { + if (sig(Y[j] - Y[i]) != 0) + { + Y[++i] = Y[j]; + } + } + b = i + 1; +} + +int erfen(int l, int r, double XX[], double xx) +{ + while (l < r) + { + int m = (l + r) / 2; + if (sig(XX[m] - xx) == 0) + { + return m; + } + else if (sig(XX[m] - xx) < 0) + { + l = m + 1; + } + else + { + r = m; + } + } + return 0; +} + +void lisan(int n) +{ + for (int i = 0; i <= a + 1; i++) + { + for (int j = 0; j <= b + 1; j++) + { + for (int k = 0; k < 2; k++) + { + map_[i][j][k].dis = 0; + map_[i][j][k].f = 0; + } + } + } + for (int i = 0; i < n; i++) + { + double x1 = lin[i].x1, x2 = lin[i].x2; + double y1 = lin[i].y1, y2 = lin[i].y2; + int ax1, ax2, ay1, ay2; + ax1 = erfen(0, a, X, x1) + 1; + ax2 = erfen(0, a, X, x2) + 1; + ay1 = erfen(0, b, Y, y1) + 1; + ay2 = erfen(0, b, Y, y2) + 1; + if (ax1 == ax2) + { + if (ay1 > ay2) + { + swap(ay1, ay2); + } + for (int i = ay1; i < ay2; i++) + { + map_[ax1][i][0].dis = fabs(Y[i] - Y[i - 1]); + map_[ax1][i][0].f = 1; + } + } + else + { + if (ax1 > ax2) + { + swap(ax1, ax2); + } + for (int i = ax1; i < ax2; i++) + { + map_[i][ay1][1].dis = fabs(X[i] - X[i - 1]); + map_[i][ay1][1].f = 1; + } + } + } + return; +} + +void dfs(int i, int j) +{ + if (vas[i][j]) + { + return; + } + vas[i][j] = true; + if (i - 1 >= 0) + { + if (vis[i][j][0] == false) + { + vis[i][j][0] = true; + if (map_[i][j][0].f) + { + ans += map_[i][j][0].dis; + } + else + { + dfs(i - 1, j); + } + } + } + if (j - 1 >= 0) + { + if (vis[i][j][1] == false) + { + vis[i][j][1] = true; + if (map_[i][j][1].f) + { + ans += map_[i][j][1].dis; + } + else + { + dfs(i, j - 1); + } + } + } + if (i + 1 <= a + 1) + { + if (vis[i + 1][j][0] == false) + { + vis[i + 1][j][0] = true; + if (map_[i + 1][j][0].f) + { + ans += map_[i + 1][j][0].dis; + } + else + { + dfs(i + 1, j); + } + } + } + if (j + 1 <= b + 1) + { + if (vis[i][j + 1][1] == false) + { + vis[i][j + 1][1] = true; + if (map_[i][j + 1][1].f) + { + ans += map_[i][j + 1][1].dis; + } + else + { + dfs(i, j + 1); + } + } + } + return; +} + +int main() +{ + int n, t; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + memset(map_, false, sizeof(map_)); + memset(vis, false, sizeof(vis)); + memset(vas, false, sizeof(vas)); + read(n); + lisan(n); + ans = 0; + dfs(0, 0); + printf("%.lf\n", ans); + } + return 0; +} diff --git a/1251.cpp b/1251.cpp new file mode 100644 index 0000000..4641516 --- /dev/null +++ b/1251.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +//1251 +//Repeated Substitution with Sed +//Graphs;Shortest Path;BFS +#define MAX 1000 + +struct Item +{ + string s; + int c; + Item(string s, int c) : s(s), c(c) {} +}; + +string replace(string str, string from, string to) +{ + if (from.empty()) + { + return str; + } + int start_pos = 0; + while ((start_pos = str.find(from, start_pos)) != string::npos) + { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); + } + return str; +} + +int n; +string A[MAX], B[MAX]; + +int main() +{ + while (cin >> n, n) + { + for (int i = 0; i < n; i++) + { + cin >> A[i] >> B[i]; + } + string a, b; + cin >> a >> b; + queue Q; + set S; + Q.push(Item(a, 0)); + int answer = -1; + while (!Q.empty()) + { + Item e = Q.front(); + Q.pop(); + if (e.s == b) + { + answer = e.c; + break; + } + if (S.find(e.s) != S.end()) + { + continue; + } + S.insert(e.s); + for (int i = 0; i < n; i++) + { + string s = replace(e.s, A[i], B[i]); + if (S.find(s) != S.end() || s.size() > 10) + { + continue; + } + Q.push(Item(s, e.c + 1)); + } + } + cout << answer << endl; + } +} diff --git a/12510.cpp b/12510.cpp new file mode 100644 index 0000000..41deb93 --- /dev/null +++ b/12510.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +#define sz(v) ((int)(v).size()) +#define rep(i, n) for (int i = 0; i < (n); ++i) +#define repf(i, a, b) for (int i = (a); i <= (b); ++i) +#define repd(i, a, b) for (int i = (a); i >= (b); --i) +#define clr(x) memset(x, 0, sizeof(x)) +#define clrs(x, y) memset(x, y, sizeof(x)) +#define out(x) printf(#x " %d\n", x) + +typedef long long lint; +const int maxint = -1u >> 1; +const double esp = 1e-8; +string g[13]; +int head, last; +int dui[400][2]; +int mov[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}}; +int sum, n, m, t, ans, T, top; +struct hh +{ + int x, y; + bool move; +} h[20]; +void ind(int x, int y, string g[], bool hwalk[13][13]) +{ + if (x <= 0 || x > n || y <= 0 || y > m || hwalk[x][y] || g[x][y] == 'X' || g[x][y] == 'O') + { + return; + } + g[x][y] = '.'; + hwalk[x][y] = true; + last++; + dui[last][0] = x; + dui[last][1] = y; +} +void walk(int x, int y, string g[], bool hwalk[13][13]) +{ + head = 0; + last = 1; + dui[1][0] = x; + dui[1][1] = y; + hwalk[x][y] = true; + while (head < last) + { + head++; + int x = dui[head][0], y = dui[head][1]; + //cout< n || y <= 0 || y > m) + { + return false; + } + if (tag == 0) + { + if (hwalk[x][y] == false) + { + return false; + } + } + else if (tag == 1) + { + if (g[x][y] == 'C' || g[x][y] == 'O' || g[x][y] == 'X') + { + return false; + } + } + return true; +} +bool domove(int way, int x, int y, string g[], bool hwalk[13][13]) +{ + int sx = x + mov[way][0], sy = y + mov[way][1]; + int tx = x - mov[way][0], ty = y - mov[way][1]; + if (ok(0, sx, sy, g, hwalk) && ok(1, tx, ty, g, hwalk)) + { + g[tx][ty] = 'X'; + g[x][y] = '.'; + return true; + } + return false; +} +void dfs(int i, int x, int y, string g[]) +{ + //cout< + +using namespace std; + +int main() +{ + int T, n, m, a[1505], b[1505]; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%d", &b[i]); + } + int LCIS[1505] = {}; + for (int i = 0; i < n; i++) + { + int tmp = 0; + for (int j = 0; j < m; j++) + { + if (a[i] == b[j] && LCIS[j] < tmp + 1) + { + LCIS[j] = tmp + 1; + } + if (a[i] > b[j] && LCIS[j] > tmp) + { + tmp = LCIS[j]; + } + } + } + int res = 0; + for (int i = 0; i < m; i++) + { + if (LCIS[i] > res) + { + res = LCIS[i]; + } + } + printf("%d\n", res); + } + return 0; +} diff --git a/12512.cpp b/12512.cpp new file mode 100644 index 0000000..80e318b --- /dev/null +++ b/12512.cpp @@ -0,0 +1,231 @@ +#include + +using namespace std; + +// Simplex algorithm on augmented matrix a of dimension (m+1)x(n+1) +// returns 1 if feasible, 0 if not feasible, -1 if unbounded +// returns solution in b[] in original var order, max(f) in ret +// form: maximize sum_j(a_mj*x_j)-a_mn s.t. sum_j(a_ij*x_j)<=a_in +// in standard form. +// To convert into standard form: +// 1. if exists equality constraint, then replace by both >= and <= +// 2. if variable x doesn't have nonnegativity constraint, then replace by +// difference of 2 variables like x1-x2, where x1>=0, x2>=0 +// 3. for a>=b constraints, convert to -a<=-b +// note: watch out for -0.0 in the solution, algorithm may cycle +// eps = 1e-7 may give wrong answer, 1e-10 is better + +const double inf = 1e100; +const double eps = 1e-10; + +#define maxm 500 +#define maxn 500 + +void pivot(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn], int r, int c) +{ + int i, j; + swap(N[c], B[r]); + a[r][c] = 1 / a[r][c]; + for (j = 0; j <= n; j++) + if (j != c) + { + a[r][j] *= a[r][c]; + } + for (i = 0; i <= m; i++) + if (i != r) + { + for (j = 0; j <= n; j++) + if (j != c) + { + a[i][j] -= a[i][c] * a[r][j]; + } + a[i][c] = -a[i][c] * a[r][c]; + } +} +int feasible(int m, int n, double a[maxm][maxn], int B[maxm], int N[maxn]) +{ + int r, c, i; + double p, v; + while (1) + { + for (p = inf, i = 0; i < m; i++) + if (a[i][n] < p) + { + p = a[r = i][n]; + } + if (p > -eps) + { + return 1; + } + for (p = 0, i = 0; i < n; i++) + if (a[r][i] < p) + { + p = a[r][c = i]; + } + if (p > -eps) + { + return 0; + } + p = a[r][n] / a[r][c]; + for (i = r + 1; i < m; i++) + if (a[i][c] > eps) + { + v = a[i][n] / a[i][c]; + if (v < p) + { + r = i, p = v; + } + } + pivot(m, n, a, B, N, r, c); + } +} +int simplex(int m, int n, double a[maxm][maxn], double b[maxn], double &ret) +{ + int B[maxm], N[maxn], r, c, i; + double p, v; + for (i = 0; i < n; i++) + { + N[i] = i; + } + for (i = 0; i < m; i++) + { + B[i] = n + i; + } + if (!feasible(m, n, a, B, N)) + { + return 0; + } + while (1) + { + for (p = 0, i = 0; i < n; i++) + if (a[m][i] > p) + { + p = a[m][c = i]; + } + if (p < eps) + { + for (i = 0; i < n; i++) + if (N[i] < n) + { + b[N[i]] = 0; + } + for (i = 0; i < m; i++) + if (B[i] < n) + { + b[B[i]] = a[i][n]; + } + ret = -a[m][n]; + return 1; + } + for (p = inf, i = 0; i < m; i++) + if (a[i][c] > eps) + { + v = a[i][n] / a[i][c]; + if (v < p) + { + p = v, r = i; + } + } + if (p == inf) + { + return -1; + } + pivot(m, n, a, B, N, r, c); + } +} + +int n, c, d, F, e, Q, mat[20][20], qx[300], qy[300]; + +void IN(int a, int l, int h) +{ + assert(l <= a && a <= h); +} + +void input() +{ + int i, j; + scanf("%d%d%d%d%d%d", &n, &c, &d, &F, &e, &Q); + IN(n, 1, 12); + IN(c, 0, 100); + IN(d, 0, 100); + IN(F, -10, 10); + IN(e, 0, 5); + IN(Q, 1, n * n); + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + scanf("%d", &mat[i][j]); + IN(mat[i][j], -10, 10); + } + for (i = 0; i < Q; i++) + { + scanf("%d%d", &qx[i], &qy[i]); + IN(qx[i], 1, n); + IN(qy[i], 1, n); + qx[i]--; + qy[i]--; + } +} + +#define I_ID(r, c) (c) * (n) + (r) +#define D_ID(r, c) n *n + I_ID(r, c) + +double A[maxm][maxn], B[maxn]; + +double process() +{ + memset(A, 0, sizeof(A)); + int neq = 2 * Q, nvar = 2 * n * n; + int cureq = 0; + double ret; + int i, j, status; + for (i = 0; i < Q; i++) + { + A[cureq][nvar] = e + (2 * n - 1) * F; + A[cureq + 1][nvar] = e - (2 * n - 1) * F; + for (j = 0; j < n; j++)//row + { + A[cureq][nvar] -= mat[j][qy[i]]; + A[cureq + 1][nvar] += mat[j][qy[i]]; + A[cureq][I_ID(j, qy[i])] = 1; + A[cureq][D_ID(j, qy[i])] = -1; + A[cureq + 1][I_ID(j, qy[i])] = -1; + A[cureq + 1][D_ID(j, qy[i])] = 1; + } + for (j = 0; j < n; j++) + if (j != qy[i])//col + { + A[cureq][nvar] -= mat[qx[i]][j]; + A[cureq + 1][nvar] += mat[qx[i]][j]; + A[cureq][I_ID(qx[i], j)] = 1; + A[cureq][D_ID(qx[i], j)] = -1; + A[cureq + 1][I_ID(qx[i], j)] = -1; + A[cureq + 1][D_ID(qx[i], j)] = 1; + } + cureq += 2; + } + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + { + A[cureq][I_ID(i, j)] = -c; + A[cureq][D_ID(i, j)] = -d; + } + status = simplex(neq, nvar, A, B, ret); + assert(status == 1); + return -ret; +} + +int main() +{ + int T; + double ans; + scanf("%d", &T); + IN(T, 1, 100); + while (T--) + { + input(); + ans = process(); + printf("%.5lf\n", ans); + } + return 0; +} diff --git a/12514.cpp b/12514.cpp new file mode 100644 index 0000000..ba0388f --- /dev/null +++ b/12514.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +const double eps = 1e-6; +const double pi = acos(-1.); + +inline int dcmp(double x) +{ + if (x < -eps) + return -1; + return x > eps; +} + +struct point +{ + double x, y; + bool operator<(const point rgh) const + { + if (dcmp(y - rgh.y)) + return y < rgh.y; + return x < rgh.x; + } + bool operator==(const point rgh) const + { + return !dcmp(x - rgh.x) && !dcmp(y - rgh.y); + } + point(){}; + point(double _x, double _y) : x(_x), y(_y) {} +} L[100][2], p[3000], q[3000]; + +int hea[3000], nxt[3000000], vv[3000000], c, M; + +double cross(point &k, point &a, point &b) +{ + return (a.x - k.x) * (b.y - k.y) - (a.y - k.y) * (b.x - k.x); +} + +bool chk(point &k) +{ + if (dcmp(k.x) < 0 || dcmp(k.x - M) > 0) + return 0; + if (dcmp(k.y) < 0 || dcmp(k.y - M) > 0) + return 0; + return 1; +} + +int inter(point &a1, point &a2, point &b1, point &b2, point &k) +{ + double u = cross(a1, a2, b1), v = cross(a2, a1, b2); + if (dcmp(u + v)) + { + k = point((b1.x * v + b2.x * u) / (v + u), (b1.y * v + b2.y * u) / (v + u)); + if (chk(k)) + return 1; + } + return 0; +} + +int chip[505][505]; +bool vis[5000]; +double ans; + +double cal(point &k, point &a, point &b) +{ + double x = atan2(b.y - k.y, b.x - k.x) - atan2(a.y - k.y, a.x - k.x); + if (x < 0) + x += 2 * pi; + return x; +} + +int sta[3000], top; +double ya[1000], yb[1000]; + +int tongji() +{ + int i, j, k, xa = 10000, xb = -10000; + int xx, yy, cnt = 0; + double t, xie; + + for (i = 0; i < top; ++i) + { + xa = min(xa, (int)(ceil(p[sta[i]].x) + eps)); + xb = max(xb, (int)(floor(p[sta[i]].x) + eps)); + } + + for (i = xa; i <= xb; ++i) + ya[i] = 1e30, yb[i] = -1e30; + + for (i = 0; i < top; ++i) + if (dcmp(p[sta[i]].x - p[sta[i + 1]].x)) + { + xie = (p[sta[i + 1]].y - p[sta[i]].y) / (p[sta[i + 1]].x - p[sta[i]].x + 0.); + xx = ceil(min(p[sta[i]].x, p[sta[i + 1]].x)) + eps; + yy = floor(max(p[sta[i]].x, p[sta[i + 1]].x)) + eps; + for (j = xx; j <= yy; ++j) + { + t = p[sta[i + 1]].y - xie * (p[sta[i + 1]].x - j); + ya[j] = min(ya[j], t); + yb[j] = max(yb[j], t); + } + } + + for (i = xa; i <= xb; ++i) + { + xx = ceil(ya[i]) + eps; + yy = floor(yb[i]) + eps; + for (j = xx; j <= yy; ++j) + cnt += chip[i][j]; + } + + return cnt; +} + +void solve(int x) +{ + int i, j, k; + int a, b, c; + double t, v; + bool r; + + for (i = hea[x]; i >= 0; i = nxt[i]) + { + r = 0; + top = 0; + a = x, b = vv[i]; + if (vis[b]) + continue; + sta[top++] = a, sta[top++] = b; + while (1) + { + v = -1e30; + for (j = hea[b]; j >= 0; j = nxt[j]) + if (vv[j] != a) + { + c = vv[j]; + t = cal(p[b], p[a], p[c]); + if (t > v) + v = t, k = c; + } + + if (dcmp(v - pi) <= 0) + break; + + if (k == x) + { + r = 1; + break; + } + if (vis[k]) + break; + + a = b, b = k; + sta[top++] = k; + } + + if (r) + { + sta[top] = sta[0]; + k = tongji(); + v = 0; + for (j = 0; j < top; ++j) + v += cross(p[sta[0]], p[sta[j]], p[sta[j + 1]]); + v = fabs(v) * 0.5; + ans = max(ans, k / v); + } + } + + vis[x] = 1; +} + +int main() +{ + int C, N; + int i, j, k, e; + int a, b; + point t; + + while (scanf("%d %d %d", &M, &C, &N), M) + { + c = 0; + ans = 0; + memset(hea, -1, sizeof(hea)); + memset(vis, 0, sizeof(vis)); + memset(chip, 0, sizeof(chip)); + + for (i = 0; i < C; ++i) + { + scanf("%d %d", &a, &b); + chip[a][b]++; + } + + L[0][0] = point(0, 0), L[0][1] = point(M, 0); + L[1][0] = point(M, 0), L[1][1] = point(M, M); + L[2][0] = point(M, M), L[2][1] = point(0, M); + L[3][0] = point(0, M), L[3][1] = point(0, 0); + + N += 4; + for (i = 4; i < N; ++i) + scanf("%lf %lf %lf %lf", &L[i][0].x, &L[i][0].y, &L[i][1].x, &L[i][1].y); + + for (i = 0; i <= M; ++i) + for (j = 0; j <= M; ++j) + if (chip[i][j]) + { + t = point(i, j); + for (k = 0; k < N; ++k) + { + if (dcmp(cross(t, L[k][0], L[k][1]))) + continue; + chip[i][j] = 0; + break; + } + } + + e = 0; + for (i = 0; i < N; ++i) + for (j = i + 1; j < N; ++j) + if (inter(L[i][0], L[i][1], L[j][0], L[j][1], t)) + p[e++] = t; + + if (e >= 2800) + { + while (1) + puts("x"); + } + + sort(p, p + e); + e = unique(p, p + e) - p; + + for (i = 0; i < N; ++i) + { + k = 0; + for (j = 0; j < N; ++j) + if (i != j && inter(L[i][0], L[i][1], L[j][0], L[j][1], t)) + q[k++] = t; + sort(q, q + k); + k = unique(q, q + k) - q; + + for (j = 1; j < k; ++j) + { + a = lower_bound(p, p + e, q[j - 1]) - p; + b = lower_bound(p, p + e, q[j]) - p; + vv[c] = b, nxt[c] = hea[a], hea[a] = c++; + vv[c] = a, nxt[c] = hea[b], hea[b] = c++; + + if (c >= 3000000) + { + while (1) + puts("x"); + } + } + } + + for (i = 0; i < e; ++i) + solve(i); + + printf("%.3lf\n", ans); + } + + return 0; +} diff --git a/12515.cpp b/12515.cpp new file mode 100644 index 0000000..5e4070c --- /dev/null +++ b/12515.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define eps 1e-12 +#define maxn 1100 +#define maxm 1100000 +#define inf 0x3f3f3f3f +#define PB push_back +#define MP make_pair + +bitset<101> a[maxn], b, c, d, e, f; +int l[maxn]; + +int main() +{ + int m, q, i, j, k, ll, t, p, ans; + char s[110]; + while (scanf("%d %d", &m, &q) != EOF) + { + for (i = 0; i < m; i++) + { + scanf("%s", s); + for (k = 0; s[k]; k++) + { + a[i][k] = s[k] - '0'; + } + l[i] = k; + } + while (q--) + { + scanf("%s", s); + for (i = 0; s[i]; i++) + { + b[i] = s[i] - '0'; + c[i] = 1; + } + ll = i; + for (; i < 101; i++) + { + c[i] = 0; + } + d = b; + e = c; + t = -1; + for (i = 0; i < m; i++) + { + b = d; + c = e; + for (j = ll - 1; j < l[i]; j++) + { + f = (a[i] ^ b) & c; + p = ll - f.count(); + if (t < p) + { + t = p; + ans = i; + } + b <<= 1; + c <<= 1; + } + } + printf("%d\n", ans + 1); + } + } +} diff --git a/12516.cpp b/12516.cpp new file mode 100644 index 0000000..7351458 --- /dev/null +++ b/12516.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + + +int main() +{ + int row, col, other, our, c; + char r, use; + + while (scanf("%d %d", &row, &col) && row + col) + { + bool cola[row][col + 1], seat[row][col + 1], opp[row][col + 1], ok = true; + memset(cola, false, sizeof(cola)); + memset(seat, false, sizeof(seat)); + memset(opp, false, sizeof(opp)); + + for (scanf("%d\n", &other); other; --other) + { + scanf("%c %d %c\n", &r, &c, &use); + opp[r - 'A'][c] = seat[r - 'A'][c] = cola[r - 'A'][c + (use == '+')] = true; + } + for (scanf("%d\n", &our); our; --our) + { + scanf("%c %d\n", &r, &c); + seat[r - 'A'][c] = true; + } + for (int i = 0; i < row && ok; ++i) + for (int j = 0; j < col && ok; ++j) + if (seat[i][j] ^ opp[i][j]) + if (!cola[i][j]) + cola[i][j] = true; + else if (!cola[i][j + 1]) + cola[i][j + 1] = true; + else + ok = false; + puts(ok ? "YES" : "NO"); + } + return 0; +} diff --git a/12517.cpp b/12517.cpp new file mode 100644 index 0000000..91496c5 --- /dev/null +++ b/12517.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +long sumofDigit(long n, long ditInd, long poss) +{ + if (n == 0) + { + return 0; + } + long m = n % 10; + return n / 10 * 45 * ditInd + m * poss + m * (m - 1) / 2 * ditInd + sumofDigit(n / 10, ditInd * 10, poss + m * ditInd); +} +int main() +{ + long x, y; + while (1) + { + scanf("%ld %ld", &x, &y); + if (x == 0 && y == 0) + { + break; + } + printf("%ld\n", sumofDigit(y, 1, 1) - sumofDigit(x - 1, 1, 1)); + } + return 0; +} diff --git a/12518.cpp b/12518.cpp new file mode 100644 index 0000000..a34d3e5 --- /dev/null +++ b/12518.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define eps 1e-12 +#define maxn 110000 +#define maxm 1100000 +#define inf 0x3f3f3f3f +#define PB push_back +#define MP make_pair +char s[3][10][5] = {"._.", "...", "._.", "._.", "...", "._.", "._.", "._.", "._.", "._.", + "|.|", "..|", "._|", "._|", "|_|", "|_.", "|_.", "..|", "|_|", "|_|", + "|_|", "..|", "|_.", "._|", "..|", "._|", "|_|", "..|", "|_|", "._|"}; +int d[maxn], f[maxn], l[maxn]; +int pos(int b, int e, int p) +{ + if (b == e) + { + return b; + } + int m = (b + e) >> 1; + if (p >= l[m]) + { + return pos(m + 1, e, p); + } + return pos(b, m, p); +} +int main() +{ + int n, m, i, j, k, x, y; + while (scanf("%d", &n) != EOF) + { + if (n == 0) + { + break; + } + scanf("%d %d", &d[0], &f[0]); + l[0] = f[0] + 2; + for (i = 1; i < n; i++) + { + scanf("%d %d", &d[i], &f[i]); + l[i] = l[i - 1] + f[i] + 3; + } + scanf("%d", &m); + while (m--) + { + scanf("%d %d", &x, &y); + k = pos(0, n - 1, x); + if (k) + { + x -= l[k - 1]; + if (!x) + { + printf(".\n"); + continue; + } + x--; + } + if (x == f[k] + 1) + { + x = 2; + } + else if (x) + { + if (y % f[k]) + { + printf(".\n"); + continue; + } + x = 1; + } + if (y > (f[k] << 1)) + { + printf(".\n"); + continue; + } + if (y < f[k]) + { + y = 2; + } + else if (y < (f[k] << 1)) + { + y = 1; + } + else + { + y = 0; + } + printf("%c\n", s[y][d[k]][x]); + } + } + return 0; +} diff --git a/12519.cpp b/12519.cpp new file mode 100644 index 0000000..cce0997 --- /dev/null +++ b/12519.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define MAX 100000 +#define pi acos(-1.0) +#define inf 2147483647 +#define ll long long int +#define EPS 1e-9 +#define mod 100000007 +#define sf scanf +#define pf printf +#define pb push_back + +struct box +{ + int to; + int time; + box(int c, int nm) + { + to = c; + time = nm; + } +}; + +vector vec[101]; +long vis[101]; + +bool bfs(int node) +{ + bool flag = 0; + queue que; + que.push(node); + vis[node] = 0; + while (!que.empty()) + { + int u = que.front(); + que.pop(); + for (int i = 0; i < vec[u].size(); i++) + { + if (vis[vec[u][i].to] == -inf) + { + vis[vec[u][i].to] = vis[u] + vec[u][i].time; + que.push(vec[u][i].to); + } + else if (vis[vec[u][i].to] != vis[u] + vec[u][i].time) + { + flag = 1; + break; + } + } + if (flag) + { + break; + } + } + return flag; +} + +int main() +{ + int n, b; + while (scanf("%d %d", &n, &b) == 2 && (n || b)) + { + for (int i = 0; i < n; i++) + { + vec[i].clear(); + } + //memset(vis,-1,sizeof(vis)); + for (int i = 0; i < n; i++) + { + vis[i] = -inf; + } + int c, d, t; + for (int i = 0; i < b; i++) + { + scanf("%d %d %d", &c, &d, &t); + vec[c - 1].push_back(box(d - 1, t)); + vec[d - 1].push_back(box(c - 1, -t)); + } + if (bfs(0)) + { + printf("Y\n"); + } + else + { + printf("N\n"); + } + } + return 0; +} diff --git a/1252.cpp b/1252.cpp new file mode 100644 index 0000000..a7bc57d --- /dev/null +++ b/1252.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int m, n; +int g[200]; +char str[200]; +int dp[1 << 12][1 << 12]; + +int solve(int state, int cmp) +{ + int i, j; + if (dp[state][cmp] != -1) + { + return dp[state][cmp]; + } + int num = 0; + for (i = 0; i < n; i++) + { + if ((g[i] & state) == cmp) + { + num++; + } + } + if (num == 1) + { + return dp[state][cmp] = 0; + } + int ans = 0x7fffffff; + for (i = 0; i < m; i++) + { + if (state & (1 << i)) + { + continue; + } + dp[state | (1 << i)][cmp] = solve(state | (1 << i), cmp); + dp[state | (1 << i)][cmp | (1 << i)] = solve(state | (1 << i), cmp | (1 << i)); + ans = min(ans, max(dp[state | (1 << i)][cmp], dp[state | (1 << i)][cmp | (1 << i)]) + 1); + } + return dp[state][cmp] = ans; +} + +int main() +{ + int i, j; + while (scanf("%d %d", &m, &n) != EOF && m && n) + { + for (i = 0; i < n; i++) + { + g[i] = 0; + scanf("%s", str); + for (j = 0; j < m; j++) + { + g[i] = (g[i] << 1) + str[j] - '0'; + } + } + memset(dp, -1, sizeof(dp)); + printf("%d\n", solve(0, 0)); + } + return 0; +} diff --git a/12520.cpp b/12520.cpp new file mode 100644 index 0000000..f0859c7 --- /dev/null +++ b/12520.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +typedef long long LL; + +int main() +{ + LL l, n, ans, ans1; + while (scanf("%lld%lld", &l, &n) && l) + { + if (n <= (l * l + 1) / 2) + { + ans = n * 4; + } + else + { + n = l * l - n; + ans = 4 * l; + LL a = ((l - 2) * (l - 2) + 1) / 2; + LL b = (l - 2) * 2; + if (l & 1) + { + b -= 2; + } + if (n <= a) + { + ans += n * 4; + } + else if (n <= a + b) + { + ans += a * 4 + (n - a) * 2; + } + else + { + ans += a * 4 + b * 2; + } + if (l & 1) + { + ans1 = 4 * l; + LL a = ((l - 2) * (l - 2)) / 2; + LL b = (l - 1) * 2; + if (n <= a) + { + ans1 += n * 4; + } + else if (n <= a + b) + { + ans1 += a * 4 + (n - a) * 2; + } + else + { + ans1 += a * 4 + b * 2; + } + if (ans < ans1) + { + ans = ans1; + } + } + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12521.cpp b/12521.cpp new file mode 100644 index 0000000..d73530a --- /dev/null +++ b/12521.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define eps 1e-12 +#define maxn 110 +#define maxm 5100 +#define inf 0x3f3f3f3f +#define PB push_back +#define MP make_pair + +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +char c[maxn][40]; +int d[maxn], s[maxm], f[maxm], r[maxm][maxm]; +map a; +map::iterator pos; +int main() +{ + int t, n, m, i, j, k, l, x, y, num, cnt = 1; + while (scanf("%d %d", &t, &n) != EOF) + { + if (t == 0 && n == 0) + { + break; + } + for (i = 0; i < t; i++) + { + scanf("%s %d", c[i], &d[i]); + } + for (i = 1; i <= n; i++) + { + scanf("%d", &k); + scanf("%d %d", &s[k], &f[k]); + for (j = 0; j < f[k]; j++) + { + scanf("%d", &r[k][j]); + } + qsort(r[k], f[k], sizeof(int), cmp); + } + m = n / t; + printf("Case %d:\n", cnt++); + for (i = 0; i < t; i++) + { + printf("%s\n", c[i]); + x = d[i]; + a.clear(); + a.insert(make_pair(s[x], 1)); + for (j = 0; j < f[x]; j++) + { + y = r[x][j]; + for (k = l = num = 0; k < f[x] && l < f[y];) + if (r[x][k] < r[y][l]) + { + k++; + } + else if (r[x][k] > r[y][l]) + { + l++; + } + else + { + num++, k++, l++; + } + if (num >= m - 2) + { + pos = a.find(s[y]); + if (pos == a.end()) + { + a.insert(make_pair(s[y], 1)); + } + else + { + pos->second++; + } + } + } + for (pos = a.begin(); pos != a.end(); pos++) + { + printf("%d %d\n", pos->first, pos->second); + } + } + } + return 0; +} diff --git a/12522.cpp b/12522.cpp new file mode 100644 index 0000000..b3496f5 --- /dev/null +++ b/12522.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +const int MAXL = 1001; + +// roman numeral to int +int getNum(char ch) +{ + switch (ch) + { + case 'I': + return 1; + case 'V': + return 5; + case 'X': + return 10; + case 'L': + return 50; + case 'C': + return 100; + case 'D': + return 500; + case 'M': + return 1000; + default: + return -1; + } +} + +char str[MAXL], chr[MAXL]; +void getShortest(int val) +{ + int l = 0; + if (val >= 1000) + { + int t = val / 1000; + for (int i = 1; i <= t; i++) + { + chr[l++] = 'M'; + } + val -= t * 1000; + } + if (val >= 100) + { + int t = val / 100; + if (t != 4 && t != 9) + { + if (t < 5) + for (int i = 1; i <= t; i++) + { + chr[l++] = 'C'; + } + else + { + chr[l++] = 'D'; + for (int i = 6; i <= t; i++) + { + chr[l++] = 'C'; + } + } + } + else + { + if (t == 4) + { + chr[l++] = 'C'; + chr[l++] = 'D'; + } + else + { + chr[l++] = 'C'; + chr[l++] = 'M'; + } + } + val -= t * 100; + } + if (val >= 10) + { + int t = val / 10; + if (t != 4 && t != 9) + { + if (t < 5) + for (int i = 1; i <= t; i++) + { + chr[l++] = 'X'; + } + else + { + chr[l++] = 'L'; + for (int i = 6; i <= t; i++) + { + chr[l++] = 'X'; + } + } + } + else + { + if (t == 4) + { + chr[l++] = 'X'; + chr[l++] = 'L'; + } + else + { + chr[l++] = 'X'; + chr[l++] = 'C'; + } + } + val -= t * 10; + } + if (val > 0) + { + int t = val; + if (t != 4 && t != 9) + { + if (t < 5) + for (int i = 1; i <= t; i++) + { + chr[l++] = 'I'; + } + else + { + chr[l++] = 'V'; + for (int i = 6; i <= t; i++) + { + chr[l++] = 'I'; + } + } + } + else + { + if (t == 4) + { + chr[l++] = 'I'; + chr[l++] = 'V'; + } + else + { + chr[l++] = 'I'; + chr[l++] = 'X'; + } + } + val = 0; + } + chr[l] = '\0'; +} +void solve() +{ + int l1 = strlen(str); + int l2 = strlen(chr); + int ae, ac, res; + res = 100000000; + ae = ac = 0; + for (int i = 0; i < l1; i++) + { + int e = 0, c = 0; + for (int j = 0; j < l2; j++) + if (chr[j] == str[i + j]) + { + e++; + } + c = l2 - e; + e = l1 - e; + if (e + c < res || (e + c == res && e < ae)) + { + res = e + c; + ae = e; + ac = c; + } + } + printf("%d %d\n", ae, ac); +} + +int main() +{ + while (gets(str)) + { + if (strcmp(str, "*") == 0) + { + break; + } + int val = 0; + for (int i = 0; str[i]; i++) + { + val += getNum(str[i]); + } + getShortest(val); + solve(); + } + return 0; +} diff --git a/12523.cpp b/12523.cpp new file mode 100644 index 0000000..eb36941 --- /dev/null +++ b/12523.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +const double EPS = 1e-8; + +bool Cmp(const pair a, const pair b) +{ + return a.first > b.first; +} + +vector sum; +vector> tt; + +int main() +{ + int i, s, n, temp, j, a, b; + while (cin >> s >> n && (s || n)) + { + tt.clear(); + sum.clear(); + sum.resize(n); + for (i = 0; i < s; i++) + for (j = 0; j < n; j++) + { + cin >> temp, sum[j] += temp; + } + int per, ans; + per = ans = 0; + for (i = 0; i < n; i++) + { + cin >> a >> b; + per += a; + ans += (a * sum[i]); + tt.push_back(make_pair(sum[i], b - a)); + } + sort(tt.begin(), tt.end(), Cmp); + i = 0; + while ((per + tt[i].second) < 100) + { + ans += (tt[i].second * tt[i].first), per += tt[i].second, i++; + } + ans += (tt[i].first * (100 - per)); + double anss; + anss = (double)ans / (double)(100 * s) + EPS; + printf("%.2f\n", anss); + } + return 0; +} diff --git a/12524.cpp b/12524.cpp new file mode 100644 index 0000000..eeb94a0 --- /dev/null +++ b/12524.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define MAXN 1024 +long long dp[MAXN][MAXN], X[MAXN], W[MAXN]; +long long sumW[MAXN] = {}, sumXW[MAXN] = {}; +struct SlopeDP +{ + int front, rear; + long long X[MAXN], Y[MAXN]; + void init() { front = 0, rear = -1; } + long long cross(long long x1, long long y1, long long x2, long long y2) + { + return x1 * y2 - y1 * x2; + } + int add(long long x, long long y) + { + while (front < rear && cross(X[rear] - X[rear - 1], Y[rear] - Y[rear - 1], x - X[rear - 1], y - Y[rear - 1]) < 0) + rear--; + rear++; + X[rear] = x, Y[rear] = y; + } + long long get(long long m) + { + while (front < rear && Y[front + 1] - Y[front] < (X[front + 1] - X[front]) * m) + front++; + return Y[front] - m * X[front]; + } +} dpTool; +int main() +{ + int N, M; + while (scanf("%d %d", &N, &M) == 2) + { + for (int i = 1; i <= N; i++) + scanf("%lld %lld", &X[i], &W[i]); + for (int i = 1; i <= N; i++) + { + sumW[i] = sumW[i - 1] + W[i]; + sumXW[i] = sumXW[i - 1] + X[i] * W[i]; + } + for (int i = 0; i <= N; i++) + for (int j = 0; j <= M; j++) + dp[i][j] = 1LL << 60; + dp[0][0] = 0; + // for (int i = 1; i <= N; i++) { + // for (int j = 1; j <= M; j++) { + // for (int k = 0; k < i; k++) + // dp[i][j] = min(dp[i][j], dp[k][j-1] + X[i] * (sumW[i] - sumW[k]) - (sumXW[i] - sumXW[k])); + // } + // } + for (int i = 1; i <= M; i++) + { + dpTool.init(); + for (int j = i; j <= N; j++) + { + dpTool.add(sumW[j - 1], dp[j - 1][i - 1] + sumXW[j - 1]); + long long val = dpTool.get(X[j]); + dp[j][i] = val - (sumXW[j] - sumW[j] * X[j]); + } + } + printf("%lld\n", dp[N][M]); + } + return 0; +} +/* +dp[i][j] = dp[k][j-1] + X[i] * (sumW[i] - sumW[k]) - (sumXW[i] - sumXW[k]) +dp[k][j-1] + sumXW[k] = X[i] * sumW[k] + dp[i][j] + sumXW[i] - sumW[i] * X[i] +^^^^^^^^^^^^^^^^^^^^^ ^^^^ ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +y = a x + b, a : monotone increasing, maintain upper convex +*/ +/* +3 1 +20 1 +30 1 +40 1 +3 1 +11 3 +12 2 +13 1 +6 2 +10 15 +12 17 +16 18 +18 13 +30 10 +32 1 +6 3 +10 15 +12 17 +16 18 +18 13 +30 10 +32 1 +*/ diff --git a/12525.cpp b/12525.cpp new file mode 100644 index 0000000..3a81415 --- /dev/null +++ b/12525.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +#define MOD 1000000007 +#define MAXS 200 +#define MAXB 100 + +int memo[MAXB][MAXS + 1][MAXS + 1]; + +int solve(int pos, int s, int have) +{ + if (pos == 0) + { + if (s) + return 0; + return have == 0 ? 1 : 0; + } + + int &ret = memo[pos][s][have]; + + if (ret == -1) + { + ret = solve(pos - 1, s, have / 2); + if (s) + ret += solve(pos, s - 1, have + 1); + if (ret >= MOD) + ret -= MOD; + } + + return ret; +} + +int main() +{ + int S, B; + memset(memo, -1, sizeof memo); + while (scanf("%d %d", &S, &B) == 2) + { + printf("%d\n", solve(B - 1, S, 0)); + } + return 0; +} diff --git a/12526.cpp b/12526.cpp new file mode 100644 index 0000000..f3c60a0 --- /dev/null +++ b/12526.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int code_node; + +struct node +{ + int next[32]; + int has_next; +} n[1048576]; + +int push(int nn, int x) +{ + node &z = n[nn]; + if (!(z.has_next & (1 << x))) + { + z.has_next |= (1 << x); + z.next[x] = code_node; + n[code_node++].has_next = 0; + } + return z.next[x]; +} + +char word[100048][128]; +int main() +{ + for (int at, z; scanf("%d", &z) == 1;) + { + code_node = 1; + n[0].has_next = 0; + for (int i = 0; i < z; ++i) + { + at = 0; + scanf("%s", word[i]); + for (int j = 0; word[i][j]; ++j) + { + at = push(at, word[i][j] - 'a'); + } + push(at, '{' - 'a'); + } + int ans = z; + for (int i = 0, x; i < z; ++i) + { + at = n[0].next[word[i][0] - 'a']; + for (int j = 1; word[i][j]; ++j) + { + x = n[at].has_next; + ans += !!(x & (x - 1)); + at = n[at].next[word[i][j] - 'a']; + } + } + printf("%.2lf\n", ans / double(z)); + } + return 0; +} diff --git a/12527.cpp b/12527.cpp new file mode 100644 index 0000000..372deac --- /dev/null +++ b/12527.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 50000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + ret *= b; + return ret; +} +const int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +int accuVal[5000 + 10]; + +bool hasRepeated(int p) +{ + bool freq[10 + 5]; + Set(freq, false); + int mod; + while (p) + { + mod = p % 10; + if (freq[mod]) + return false; + freq[mod] = true; + p /= 10; + } + return true; +} + +void preGenerate() +{ + Set(accuVal, 0); + for (int i = 1; i <= 5000; i++) + accuVal[i] = accuVal[i - 1] + (hasRepeated(i) ? 1 : 0); +} + +int main() +{ + preGenerate(); + int n, m; + while (scanf("%d %d", &n, &m) != EOF) + printf("%d\n", accuVal[m] - accuVal[n - 1]); + return 0; +} diff --git a/12528.cpp b/12528.cpp new file mode 100644 index 0000000..6010830 --- /dev/null +++ b/12528.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define MAXK 8 +#define PARTS 4900 + +int W, D, A, K; +double p1[MAXK + 1], q1[MAXK + 1]; +double p2[MAXK + 1], q2[MAXK + 1]; +double P1, Q1, P2, Q2; +double f1[2 * PARTS + 1], f2[2 * PARTS + 1]; + +inline double calc(int pos, double mi) +{ + return max(f1[pos], -mi) - max(f2[pos], -mi); +} + +int main() +{ + while (scanf("%d%d%d%d", &W, &D, &A, &K) != EOF) + { + for (int i = 0; i <= K; ++i) + { + scanf("%lf", &p1[i]); + } + for (int i = 0; i <= K; ++i) + { + scanf("%lf", &q1[i]); + } + for (int i = 0; i <= K; ++i) + { + scanf("%lf", &p2[i]); + } + for (int i = 0; i <= K; ++i) + { + scanf("%lf", &q2[i]); + } + double dx = (double)W / 2 / PARTS, curx = 0, P1; + + for (int i = 2 * PARTS, pos = 0; i >= 0; --i, curx += dx, ++pos) + { + P1 = Q1 = P2 = Q2 = 0; + + for (int i = K; i >= 0; --i) + { + P1 = P1 * curx + p1[i]; + Q1 = Q1 * curx + q1[i]; + P2 = P2 * curx + p2[i]; + Q2 = Q2 * curx + q2[i]; + } + + f1[pos] = P1 / Q1; + f2[pos] = P2 / Q2; + } + + double lo = 0, hi = D, mi; + + for (int it = 0; it < 29; ++it) + { + mi = (lo + hi) / 2.0; + + double sum0 = calc(0, mi) + calc(2 * PARTS, mi), sum1 = 0, sum2 = 0; + + for (int i = 1, pos = 2; i < PARTS; ++i, pos += 2) + { + sum1 += calc(pos, mi); + } + + for (int i = 0, pos = 1; i < PARTS; ++i, pos += 2) + { + sum2 += calc(pos, mi); + } + + if ((sum0 + 2 * sum1 + 4 * sum2) * dx / 3 > A) + { + hi = mi; + } + else + { + lo = mi; + } + } + + printf("%.5lf\n", lo); + } + + return 0; +} diff --git a/12529.cpp b/12529.cpp new file mode 100644 index 0000000..3be63a6 --- /dev/null +++ b/12529.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +const int dx[] = {1, -1, 0, 0}; +const int dy[] = {0, 0, 1, -1}; +char g[700][700][4]; +char visited[700][700]; +void bfs(int x, int y, int n, int m) +{ + queue X, Y; + X.push(x), Y.push(y); + visited[x][y] = 1; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + for (int i = 0, tx, ty; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= n || ty >= m) + continue; + if (visited[tx][ty] || g[x][y][i]) + continue; + visited[tx][ty] = 1; + X.push(tx), Y.push(ty); + } + } +} +int main() +{ + int N; + char line[1024]; + while (scanf("%d", &N) == 1) + { + memset(g, 0, sizeof(g)); + memset(visited, 0, sizeof(visited)); + for (int i = 0; i < 2 * N - 1; i++) + { + scanf("%s", line); + int x = i, y = x & 1; + for (int j = 0; j < N; j++, y += 2) + { + if (line[j] == 'H') + { + g[x][y][0] = g[x + 1][y][1] = 1; + g[x][y + 1][0] = g[x + 1][y + 1][1] = 1; + } + else + { + g[x][y][2] = g[x][y + 1][3] = 1; + g[x + 1][y][2] = g[x + 1][y + 1][3] = 1; + } + } + } + int cc = 0; + for (int i = 0; i < 2 * N; i++) + { + for (int j = 0; j < 2 * N + 1; j++) + { + if (!visited[i][j]) + bfs(i, j, 2 * N, 2 * N + 1), cc++; + } + } + printf("%d\n", cc - 1); + } + return 0; +} +/* +3 +HVH +VVV +HHH +HHH +VHV +1 +H +1 +V +*/ diff --git a/1253.cpp b/1253.cpp new file mode 100644 index 0000000..8a08263 --- /dev/null +++ b/1253.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +#define FOR(i, j, k, in) for (int i = j; i < k; i += in) +#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in) +#define REP(i, j) FOR(i, 0, j, 1) +#define RREP(i, j) FOR(i, j, 0, -1) +#define all(cont) cont.begin(), cont.end() +#define rall(cont) cont.end(), cont.begin() +#define tr(cont, it) for (__typeof(cont.begin()) it = cont.begin(); it != cont.end(); it++) + +#define INF 1000000000 + +typedef pair ii; +typedef vector vi; +typedef vector vii; +typedef vector vvi; +typedef unsigned long long ll; + +typedef pair state; + +queue q; +map dist; +int n; +char line[5]; +ii car; +string s, field, temp; +state ss; + +int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; +int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; + +bool boundaries(int x, int y) +{ + return x >= 0 && x < n && y >= 0 && y < n; +} + +bool done(string field) +{ + tr(field, it) + { + if (*it == '#') + { + return false; + } + } + return true; +} + +int adjacent(string field, int x, int y) +{ + int s = 0, nx, ny; + REP(d, 8) + { + nx = (x + dx[d]); + ny = (y + dy[d]); + if (boundaries(nx, ny) && (field[n * nx + ny] == '#' || field[n * nx + ny] == '@')) + { + s++; + } + } + return s; +} + +string calculate(state u, int d) +{ + int x, y, nx, ny; + x = u.first.first; + y = u.first.second; + nx = x + dx[d]; + ny = y + dy[d]; + string field = u.second, nfield; + nfield.assign(n * n, '.'); + nfield[n * nx + ny] = '@'; + nfield[n * x + y] = '.'; + field[n * nx + ny] = '@'; + field[n * x + y] = '.'; + REP(i, n) + { + REP(j, n) + { + int adj = adjacent(field, i, j); + if (adj == 3) + { + if (field[n * i + j] != '@') + { + nfield[n * i + j] = '#'; + } + } + else if (adj == 2 && field[n * i + j] == '#') + { + nfield[n * i + j] = '#'; + } + } + } + return nfield; +} + +int bfs() +{ + ss = state(car, s); + dist[ss] = 0; + q.push(ss); + while (!q.empty()) + { + state u = q.front(); + q.pop(); + REP(d, 8) + { + int nx = u.first.first + dx[d]; + int ny = u.first.second + dy[d]; + if (boundaries(nx, ny) && u.second[n * nx + ny] != '#') + { + string nfield = calculate(u, d); + int x, y; + x = u.first.first + dx[d]; + y = u.first.second + dy[d]; + state j = make_pair(ii(x, y), nfield); + if (!dist.count(j)) + { + dist[j] = dist[u] + 1; + if (done(j.second)) + { + return dist[j]; + } + q.push(j); + } + } + } + } + return -1; +} + +int main() +{ + while (scanf("%d\n", &n), n) + { + dist.clear(); + s.clear(); + while (!q.empty()) + { + q.pop(); + } + REP(i, n) + { + cin >> temp; + s += temp; + REP(j, n) + if (s[n * i + j] == '@') + { + car = ii(i, j); + } + } + if (done(s)) + { + printf("0\n"); + } + else + { + printf("%d\n", bfs()); + } + } +} diff --git a/12530.cpp b/12530.cpp new file mode 100644 index 0000000..98f0896 --- /dev/null +++ b/12530.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +char s[55][55]; +int id[55][55], head[3000], nxt[20005], v[20005], mat[3000], vis[3000], t, c; +int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; + +void add(int x, int y) +{ + v[c] = y; + nxt[c] = head[x]; + head[x] = c++; +} + +int dfs(int u) +{ + int i; + for (i = head[u]; i != -1; i = nxt[i]) + if (!vis[v[i]]) + { + int x = v[i]; + vis[x] = 1; + if (mat[x] == -1 || dfs(mat[x])) + { + mat[x] = u; + mat[u] = x; + return 1; + } + } + return 0; +} + +int main() +{ + int n, m, i, j; + while (scanf("%d%d", &n, &m) != EOF) + { + t = c = 0; + memset(head, -1, sizeof(head)); + for (i = 0; i < n; i++) + { + scanf("%s", s[i]); + } + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + if (s[i][j] == '.') + { + id[i][j] = ++t; + } + if (t & 1) + { + puts("1"); + continue; + } + for (i = 0; i < n; i++) + for (j = 0; j < m; j++) + if (s[i][j] == '.') + { + int x = id[i][j]; + for (int d = 0; d < 4; d++) + { + int xx = i + dx[d]; + int yy = j + dy[d]; + if (xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] == '.') + { + int y = id[xx][yy]; + add(x, y); + } + } + } + memset(mat, -1, sizeof(mat)); + int f = 1; + for (i = 1; i <= t; i++) + { + if (mat[i] != -1) + { + continue; + } + memset(vis, 0, sizeof(vis)); + if (dfs(i) == 0) + { + f = 0; + break; + } + } + if (f) + { + puts("2"); + } + else + { + puts("1"); + } + } + return 0; +} diff --git a/12531.cpp b/12531.cpp new file mode 100644 index 0000000..8ca7b0b --- /dev/null +++ b/12531.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) +#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) +#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++) +#define repr(i, n) for (int i = (n)-1; i >= 0; i--) +#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) +#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) +#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++) +#define REPD(i, n) for (int i = (n)-1; i >= 0; i--) +#define foreach(it, ar) for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) +#define fill(ar, val) memset(ar, val, sizeof(ar)) +#define debug(x) cout << #x << ": " << x << endl + +#define uint64 unsigned long long +#define int64 long long +#define all(ar) ar.begin(), ar.end() +#define pb push_back +#define mp make_pair +#define ff first +#define ss second + +#define BIT(n) (1 << (n)) +#define AND(a, b) ((a) & (b)) +#define OR(a, b) ((a) | (b)) +#define XOR(a, b) ((a) ^ (b)) +#define sqr(x) ((x) * (x)) + +#define PI 3.1415926535897932385 +#define INF 1000111222 +#define EPS 1e-3 +#define MAXN 20000 +#define MOD 1000000007 + +typedef pair ii; +typedef pair iii; +typedef vector vii; +typedef vector vi; +typedef vector vs; + +template +inline T gcd(T a, T b) +{ + if (b == 0) + return a; + else + return gcd(b, a % b); +} +template +inline T lcm(T a, T b) +{ + return (a * b) / gcd(a, b); +} + +int n, t, m, a, b, c; + +bool solve() +{ + FOR(i, 1, 12) + FOR(j, 0, 59) + { + double a1 = i * 30; + double a2 = j * 6; + double a3 = fabs(a1 - a2); + if (a3 > 180) + a3 = 360 - a3; + if (fabs(a3 - n) <= EPS) + return true; + } + return false; +} + +int main() +{ + while (scanf("%d", &n) != EOF) + printf("%c\n", solve() ? 'Y' : 'N'); + return 0; +} diff --git a/12532.cpp b/12532.cpp new file mode 100644 index 0000000..b1ac65d --- /dev/null +++ b/12532.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +typedef vector VI; + +class SegmentTree +{ +public: + VI A, tree; + SegmentTree(VI &_A) : A(_A) + { + int N = A.size(); + tree = VI(4 * N, 1); + build(1, 0, N - 1); + } + + void build(int node, int lo, int hi) + { + if (lo == hi) + { + tree[node] = A[lo]; + return; + } + int mid = (lo + hi) / 2; + // compute the values in the left and right subtrees + build(2 * node, lo, mid); + build(2 * node + 1, mid + 1, hi); + // each parent node is equal to the product of his childs + tree[node] = tree[2 * node] * tree[2 * node + 1]; + } + + int query(int i, int j) + { + return _query(1, 0, A.size() - 1, i, j); + } + + int _query(int node, int lo, int hi, int i, int j) + { + // if the current interval doesn't intersect the query + // interval return 1, this value is not going to affect + // the sign of the final product + if (hi < i || lo > j) + { + return 1; + } + // if the current interval is included in the query + // interval return tree[node] + if (lo >= i && hi <= j) + { + return tree[node]; + } + int mid = (lo + hi) / 2; + // product of the intervals included in the query + return _query(2 * node, lo, mid, i, j) * _query(2 * node + 1, mid + 1, hi, i, j); + } + + void update(int ind, int val) + { + _update(1, ind, val, 0, A.size() - 1); + } + + void _update(int node, int ind, int val, int lo, int hi) + { + // if the current index is not contained in the interval, + // skip this interval + if (ind < lo || ind > hi) + { + return; + } + if (lo == hi) + { + tree[node] = val; + return; + } + int mid = (lo + hi) / 2; + // update the values in the left and right subtrees + _update(2 * node, ind, val, lo, mid); + _update(2 * node + 1, ind, val, mid + 1, hi); + tree[node] = tree[2 * node] * tree[2 * node + 1]; + } +}; + +vector X; + +int main() +{ + char cmd; + int n, q, x, ind, val, lo, hi, s; + while (cin >> n >> q) + { + X.clear(); + for (int i = 0; i < n; ++i) + { + cin >> x; + // replace the positive values with 1's + // and the negative with -1's + if (x > 0) + { + x = 1; + } + if (x < 0) + { + x = -1; + } + X.push_back(x); + } + SegmentTree st(X); + while (q-- > 0) + { + cin >> cmd; + if (cmd == 'C') + { + cin >> ind >> val; + --ind; + // replace the positive values with 1's + // and the negative with -1's + if (val > 0) + { + val = 1; + } + if (val < 0) + { + val = -1; + } + st.update(ind, val); + X[ind] = val; + } + else + { + cin >> lo >> hi; + --lo, --hi; + s = st.query(lo, hi); + if (s > 0) + { + cout << '+'; + } + else if (s < 0) + { + cout << '-'; + } + else + { + cout << '0'; + } + } + } + cout << endl; + } + return 0; +} diff --git a/12533.cpp b/12533.cpp new file mode 100644 index 0000000..3f66ce0 --- /dev/null +++ b/12533.cpp @@ -0,0 +1,210 @@ +#include + +using namespace std; + +template +T _abs(T n) +{ + return (n < 0 ? -n : n); +} +template +T _max(T a, T b) +{ + return (!(a < b) ? a : b); +} +template +T _min(T a, T b) +{ + return (a < b ? a : b); +} +template +T sq(T x) +{ + return x * x; +} + +#define ALL(p) p.begin(), p.end() +#define MP(x, y) make_pair(x, y) +#define SET(p) memset(p, -1, sizeof(p)) +#define CLR(p) memset(p, 0, sizeof(p)) +#define MEM(p, v) memset(p, v, sizeof(p)) +#define CPY(d, s) memcpy(d, s, sizeof(s)) +#define SZ(c) (int)c.size() +#define PB(x) push_back(x) +#define ff first +#define ss second +#define i64 long long +#define ld long double +#define pii pair +#define psi pair + +const double EPS = 1e-9; +const int INF = 0x7f7f7f7f; +#define mx 100009 +vector tg[mx], g[mx]; +bool tf, vis[mx], rt[mx]; +int cl[mx], Ls[mx], tr[mx]; + +bool chk(int p, int &tp) +{ + if (vis[p]) + { + if (tr[p] != tp) + { + tp = tr[p]; + return true; + } + tr[p] = tp; + rt[p] = 1; + vis[p] = 1; + Ls[tp] = 0; + cl[p] = Ls[tp]++; + tf = true; + return true; + } + tr[p] = tp; + vis[p] = 1; + int i, j = tg[p].size(); + for (i = 0; i < j; i++) + if (chk(tg[p][i], tp)) + { + tr[p] = tp; + if (tf) + if (rt[p]) + { + tf = 0; + return true; + } + else + { + rt[p] = 1; + cl[p] = Ls[tp]++; + return true; + } + else + return true; + } + return false; +} + +int L[mx], T[mx]; + +void dfs(int p, int c, int d) +{ + int i, j = g[p].size(); + L[p] = d; + cl[p] = c; + for (i = 0; i < j; i++) + { + dfs(g[p][i], c, d + 1); + T[g[p][i]] = p; + } +} + +int P[mx][18]; + +void pre(int n) +{ + int i, j; + SET(P); + for (i = 0; i <= n; i++) + if (!rt[i]) + P[i][0] = T[i]; + for (j = 1; (1 << j) <= n; j++) + for (i = 0; i <= n; i++) + if (P[i][j - 1] != -1) + P[i][j] = P[P[i][j - 1]][j - 1]; +} + +int LCA(int p, int q, int n) +{ + int log, i; + if (L[p] < L[q]) + swap(p, q); + for (log = 1; (1 << log) <= L[p]; log++) + ; + log--; + for (i = log; i >= 0; i--) + if (L[p] - (1 << i) >= L[q]) + p = P[p][i]; + if (p == q) + return p; + for (i = log; i >= 0; i--) + if (P[p][i] != -1 && P[p][i] != P[q][i]) + p = P[p][i], q = P[q][i]; + return T[p]; +} + +inline int dist(int p, int q) +{ + if (cl[p] < cl[q]) + swap(p, q); + int r = cl[p] - cl[q]; + r = min(r, cl[q] + Ls[tr[p]] - cl[p]); + return r; +} + +int main() +{ + int n, Q, i, j, k, l, p, q; + while (cin >> n) + { + for (i = 0; i <= n; i++) + { + g[i].clear(); + tg[i].clear(); + vis[i] = 0; + rt[i] = 0; + T[i] = 0; + } + for (i = 1; i <= n; i++) + { + scanf("%d", &p); + tg[i].PB(p); + g[p].PB(i); + } + SET(tr); + k = 0; + for (i = 1; i <= n; i++) + if (!vis[i]) + { + k++; + tf = 0; + l = k; + chk(i, l); + } + T[0] = -1; + L[0] = 0; + CLR(L); + for (i = 1; i <= n; i++) + if (rt[i]) + { + k = g[i].size(); + for (j = 0; j < k; j++) + if (!rt[g[i][j]]) + { + dfs(g[i][j], cl[i], 1); + T[g[i][j]] = 0; + } + } + cin >> Q; + pre(n); + while (Q--) + { + scanf("%d %d", &p, &q); + if (tr[p] != tr[q]) + k = -1; + else if (rt[p] && rt[q]) + k = dist(p, q); + else if (rt[p] || rt[q] || (cl[p] != cl[q])) + k = dist(p, q) + L[p] + L[q]; + else + { + l = LCA(p, q, n); + k = L[p] + L[q] - (L[l] << 1); + } + printf("%d\n", k); + } + } + return 0; +} diff --git a/12534.cpp b/12534.cpp new file mode 100644 index 0000000..7835ac5 --- /dev/null +++ b/12534.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +const int MAXN = 600; +const int MAXE = 80000; +const int INF = 2100000000; + +int dis[MAXN], pre[MAXN], gap[MAXN], arc[MAXN], f[MAXE], cap[MAXE], fst[MAXN], nxt[MAXE], vv[MAXE]; + +int sap(int s, int t, int n) +{ + int q[MAXN], j, mindis, ans = 0, front = 0, rear = 1, u, v, low; + bool found, vis[MAXN]; + memset(dis, 0, sizeof(dis)); + memset(gap, 0, sizeof(gap)); + memset(vis, 0, sizeof(vis)); + memset(arc, 0, sizeof(arc)); + memset(f, 0, sizeof(f)); + u = s; + low = INF; + pre[s] = s; + while (dis[s] < n) + { + found = false; + for (int &e = arc[u]; e; e = nxt[e]) + if (dis[vv[e]] == dis[u] - 1 && cap[e] > f[e]) + { + found = true; + v = vv[e]; + low = low < cap[e] - f[e] ? low : cap[e] - f[e]; + pre[v] = u; + u = v; + if (u == t) + { + while (u - s) + { + u = pre[u]; + f[arc[u]] += low; + f[arc[u] ^ 1] -= low; + } + ans += low; + low = INF; + } + break; + } + if (found) + { + continue; + } + mindis = n; + for (int e = fst[u]; e; e = nxt[e]) + { + if (mindis > dis[vv[e]] && cap[e] > f[e]) + { + mindis = dis[vv[j = e]]; + arc[u] = e; + } + } + gap[dis[u]]--; + if (gap[dis[u]] == 0) + { + return ans; + } + dis[u] = mindis + 1; + gap[dis[u]]++; + u = pre[u]; + } + return ans; +} + +inline void add(int u, int v, int c, int &e) +{ + nxt[e] = fst[u], vv[e] = v, cap[e] = c, fst[u] = e++; + nxt[e] = fst[v], vv[e] = u, cap[e] = 0, fst[v] = e++; +} +char str[100][100]; +int sc[100], sr[100]; + +int solve(int n, int m, int x, int y) +{ + int s = n + m + 1, t = s + 1; + int e = 2, i, j, ans = 0, ss = 0; + memset(fst, 0, sizeof(fst)); + for (i = 1; i <= n; i++) + { + add(s, i, abs(sr[i] - x), e), ans += abs(sr[i] - x), ss += abs(sr[i] - x); + } + for (i = 1; i <= m; i++) + { + add(i + n, t, abs(sc[i] - y), e), ans += abs(sc[i] - y); + } + for (i = 1; i <= n; i++) + if (sr[i] > x) + for (j = 1; j <= m; j++) + if (sc[j] > y && str[i - 1][j - 1] == '1') + { + add(i, j + n, 1, e); + } + for (i = 1; i <= n; i++) + if (sr[i] < x) + for (j = 1; j <= m; j++) + if (sc[j] < y && str[i - 1][j - 1] == '0') + { + add(i, j + n, 1, e); + } + i = sap(s, t, t); + return ans - i + min(ss - i, ans - ss - i); +} + +int main() +{ + int t, n, m, i, j, k; + int ii = 0; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &n, &m); + for (i = 0; i < n; i++) + { + scanf("%s", str[i]); + } + memset(sc, 0, sizeof(sc)); + memset(sr, 0, sizeof(sr)); + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + if (str[i - 1][j - 1] == '1') + { + sr[i]++, sc[j]++; + } + int ans = 210000; + for (i = 0; i <= m; i++) + if (i * n % m == 0) + { + j = solve(n, m, i, i * n / m); + ans = min(ans, j); + } + printf("Case %d: %d\n", ++ii, ans); + } + return 0; +} diff --git a/12535.cpp b/12535.cpp new file mode 100644 index 0000000..6058884 --- /dev/null +++ b/12535.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +#define mx 200015 + +typedef long long i64; + +int arr[mx], kas = 0; +i64 res, First, Second; +int n; + +void call() +{ + sort(arr, arr + n); + queue Q; + for (int i = 0, j = 0; i < n && arr[i] <= 180000; i++) + { + if (arr[i] >= 180000) + return; + while (j < n && arr[j] <= arr[i] + 180000) + Q.push(arr[j]), j++; + while (!Q.empty() && Q.front() <= arr[i]) + Q.pop(); + i64 temp1 = Q.size(), temp2 = n - temp1 - 1; + if (j > 0 && arr[j - 1] == arr[i] + 180000) + temp1--, Second += temp1 + temp2; + First += (temp1 * (temp1 - 1) / 2) + (temp2 * (temp2 - 1) / 2); + } +} + +int main() +{ + int r; + while (scanf("%d %d", &n, &r) == 2) + { + if (!n) + break; + for (int i = 0; i < n; i++) + { + int x, y; + scanf("%d.%d", &x, &y); + arr[i] = x * 1000 + y; + } + First = Second = 0, res = (i64(n) * (n - 1) * (n - 2)) / 6; + call(); + for (int i = 0; i < n; i++) + arr[i] = (arr[i] + 180000) % 360000; + call(); + First >>= 1, Second >>= 1; + res -= (First + Second); + printf("Case %d: %lld\n", ++kas, res); + } + return 0; +} diff --git a/12536.cpp b/12536.cpp new file mode 100644 index 0000000..631fe19 --- /dev/null +++ b/12536.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +typedef long long lld; + +#define pb push_back +#define mp make_pair +#define X first +#define Y second +#define clr(x, y) memset(x, y, sizeof(x)) +#define clrt(x) memset(x, true, sizeof(x)) +#define clrf(x) memset(x, false, sizeof(x)) +#define inf 0xfffffff +#define maxn 10010 +#define eps 1e-8 +#define pi acos(-1.0) +int Sig(double a) +{ + if (a < -eps) + { + return -1; + } + return a > eps; +} +double x[maxn]; +double y[maxn]; +int cnt[maxn]; +double s[maxn]; +int n, Q; +double px[1000010]; +double py[1000010]; +int qq; +double judge(double angle) +{ + int sum = 0; + double mid = 0; + for (int i = 0; i < n; i++) + { + s[i] = x[i] * cos(angle) - y[i] * sin(angle); + mid += s[i] * cnt[i]; + sum += cnt[i]; + } + mid /= sum; + double ans = 0; + for (int i = 0; i < n; i++) + { + ans += (s[i] - mid) * (s[i] - mid) * cnt[i]; + } + return ans / sum; +} +double solve() +{ + int sum = 0; + double sx, sy; + sx = sy = 0; + for (int i = 0; i < n; i++) + { + sum += cnt[i]; + sx += x[i] * cnt[i]; + sy += y[i] * cnt[i]; + } + sx /= sum; + sy /= sum; + qq = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < cnt[i]; j++) + { + px[qq] = x[i]; + py[qq] = y[i]; + qq++; + } + double A = 0; + double B = 0; + double C = 0; + for (int i = 0; i < qq; i++) + { + A += px[i] * px[i] + sx * sx - px[i] * sx * 2; + B += py[i] * py[i] + sy * sy - py[i] * sy * 2; + C += px[i] * sy + sx * py[i] - px[i] * py[i] - sx * sy; + } + C *= 2; + B -= A; + double begin; + if (Sig(B) == 0) + { + begin = 0; + } + else + { + double angle = atan(C / B); + begin = -angle * 0.5; + } + double ans = 1e20; + for (int i = 0; i < 2; i++) + { + ans = min(ans, judge(begin + pi * 0.5 * i)); + } + return ans; +} + +int main() +{ + int cc = 1; + while (scanf("%d %d", &n, &Q) != EOF) + { + if (n + Q == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x[i], &y[i]); + } + for (int i = 0; i < n; i++) + { + cnt[i] = 1; + } + printf("Case %d:\n", cc++); + printf("%.5f\n", solve()); + for (int i = 1; i <= Q; i++) + { + int xx, yy; + scanf("%d %d", &xx, &yy); + cnt[xx] = yy; + printf("%d: %.5f\n", i, solve()); + cnt[xx] = 1; + } + } + return 0; +} diff --git a/12537.cpp b/12537.cpp new file mode 100644 index 0000000..a343154 --- /dev/null +++ b/12537.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int x[200005], y[200005]; +int ReadInt(int *x) +{ + static char c, neg; + while ((c = getchar()) < '-') + { + if (c == EOF) + return EOF; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = getchar()) >= '0') + *x = (*x << 3) + (*x << 1) + c - '0'; + *x *= neg; + return 1; +} +int bsearch(int v, int l, int r, int *A) +{ + static int m; + while (l <= r) + { + m = (l + r) >> 1; + if (A[m] <= v) + l = m + 1; + else + r = m - 1; + } + while (r >= 0 && A[r] > v) + r--; + return r + 1; +} +int main() +{ + int n, q, cases = 0; + int ax, ay, bx, by, i, j, txx, tyy; + while (scanf("%d", &n) == 1 && n) + { + printf("Case %d:\n", ++cases); + for (i = 0; i < n; i++) + { + ReadInt(&x[i]); + ReadInt(&y[i]); + } + scanf("%d %d %d %d", &ax, &ay, &bx, &by); + scanf("%d", &q); + for (i = 0; i < n; i++) + { + txx = (x[i] - ax) * (x[i] - ax) + (y[i] - ay) * (y[i] - ay); + tyy = (x[i] - bx) * (x[i] - bx) + (y[i] - by) * (y[i] - by); + x[i] = txx; + y[i] = tyy; + } + sort(x, x + n); + sort(y, y + n); + int r1, r2, ca, cb; + for (i = 0; i < q; i++) + { + ReadInt(&r1); + ReadInt(&r2); + ca = bsearch(r1 * r1, 0, n - 1, x); + cb = bsearch(r2 * r2, 0, n - 1, y); + r1 = n - ca - cb; + printf("%d\n", r1 < 0 ? 0 : r1); + } + } + return 0; +} diff --git a/12538.cpp b/12538.cpp new file mode 100644 index 0000000..466ffb3 --- /dev/null +++ b/12538.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +const int N = 50000 + 10; +const int SIZE = 20000000; +const int LEN = 100 + 10; + +struct NODE +{ + char ch; + int size; + int weight; + NODE *lch, *rch; +}; + +NODE *root[N]; +NODE mem[SIZE]; +int top; +char s[LEN]; + +inline int get_size(NODE *a) +{ + if (!a) + { + return 0; + } + else + { + return a->size; + } +} + +NODE *new_node(char _ch, int _size, int _weight, NODE *_lch, NODE *_rch) +{ + assert(top < SIZE); + mem[top].ch = _ch; + mem[top].size = _size; + mem[top].weight = _weight; + mem[top].lch = _lch; + mem[top].rch = _rch; + top++; + return &mem[top - 1]; +} + +NODE *merge(NODE *a, NODE *b) +{ + if (!a) + { + return b; + } + if (!b) + { + return a; + } + if (b->weight > a->weight) + { + return new_node(a->ch, a->size + b->size, a->weight, a->lch, merge(a->rch, b)); + } + else + { + return new_node(b->ch, b->size + a->size, b->weight, merge(a, b->lch), b->rch); + } +} + +void split(NODE *a, int size, NODE *&b, NODE *&c) +{ + NODE *tmp; + if (!a) + { + b = c = NULL; + } + else if (get_size(a->lch) >= size) + { + split(a->lch, size, b, tmp); + c = new_node(a->ch, a->size - get_size(a->lch) + get_size(tmp), a->weight, tmp, a->rch); + } + else + { + split(a->rch, size - get_size(a->lch) - 1, tmp, c); + b = new_node(a->ch, size, a->weight, a->lch, tmp); + } +} + +char find(NODE *root, int p) +{ + int lsize; + lsize = get_size(root->lch); + if (lsize > p) + { + return find(root->lch, p); + } + else if (lsize < p) + { + return find(root->rch, p - lsize - 1); + } + else + { + return root->ch; + } +} + +int main() +{ + int n, cmd, ver, v, p, c, d, len, i; + NODE *a, *b, *x, *y; + char ch; + top = 0; + root[0] = NULL; + ver = d = len = 0; + scanf("%d", &n); + while (n--) + { + scanf("%d", &cmd); + if (cmd == 1) + { + scanf("%d %s", &p, s); + p -= d; + split(root[ver], p, a, b); + for (i = 0; s[i]; i++) + { + a = merge(a, new_node(s[i], 1, rand(), NULL, NULL)); + } + root[++ver] = merge(a, b); + len += strlen(s); + } + else if (cmd == 2) + { + scanf("%d %d", &p, &c); + p -= d; + c -= d; + split(root[ver], p - 1, a, b); + split(b, c, x, y); + root[++ver] = merge(a, y); + } + else + { + scanf("%d %d %d", &v, &p, &c); + v -= d; + p -= d; + c -= d; + for (i = 0; i < c; i++) + { + ch = find(root[v], p + i - 1); + printf("%c", ch); + if (ch == 'c') + { + d++; + } + } + printf("\n"); + } + } + return 0; +} diff --git a/12539.cpp b/12539.cpp new file mode 100644 index 0000000..518af9b --- /dev/null +++ b/12539.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +const int N = 100 + 10; +const int MOD = 10007; +const int M = 500; +const int V = 69984; +const int prime[] = {2, 3, 5, 7, 11, 13, 17, 19}; +const int base[] = {1, 9, 54, 216, 864, 2592, 7776, 23328}; + +int A[N]; +int factor[M + 10][9]; +int val[70000]; +int ans[N][70000]; + +inline int max(int x, int y) +{ + return x > y ? x : y; +} + +bool cmp(int a, int b) +{ + return factor[a][8] < factor[b][8]; +} + +void preprocess() +{ + int x, s, i, j; + for (i = 1; i <= 500; i++) + { + x = i; + for (j = 0; j < 8 && x > 1; j++) + { + s = 0; + while (x % prime[j] == 0) + { + x /= prime[j]; + s++; + } + factor[i][j] = s; + } + factor[i][8] = x; + } + val[0] = 1; + for (i = 1; i < V; i++) + { + x = i; + val[i] = 1; + for (j = 7; j >= 0; j--) + { + while (x >= base[j]) + { + val[i] = val[i] * prime[j] % MOD; + x -= base[j]; + } + } + } +} + +int main() +{ + int res, n, t, cn, x, y, z, i, j, k; + int a[8]; + preprocess(); + scanf("%d", &t); + cn = 0; + while (t--) + { + cn++; + printf("Case %d: ", cn); + memset(ans, 0, sizeof(ans)); + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + scanf("%d", &A[i]); + } + sort(A + 1, A + n + 1, cmp); + ans[0][0] = 1; + for (i = 1, j = 0; i <= n; i++) + { + memcpy(ans[i], ans[i - 1], sizeof(ans[i])); + x = A[i]; + y = z = 0; + for (a[0] = 0; a[0] <= 8; a[0]++) + { + y += a[0] * base[0]; + z += max(a[0], factor[x][0]) * base[0]; + for (a[1] = 0; a[1] <= 5; a[1]++) + { + y += a[1] * base[1]; + z += max(a[1], factor[x][1]) * base[1]; + for (a[2] = 0; a[2] <= 3; a[2]++) + { + y += a[2] * base[2]; + z += max(a[2], factor[x][2]) * base[2]; + for (a[3] = 0; a[3] <= 3; a[3]++) + { + y += a[3] * base[3]; + z += max(a[3], factor[x][3]) * base[3]; + for (a[4] = 0; a[4] <= 2; a[4]++) + { + y += a[4] * base[4]; + z += max(a[4], factor[x][4]) * base[4]; + for (a[5] = 0; a[5] <= 2; a[5]++) + { + y += a[5] * base[5]; + z += max(a[5], factor[x][5]) * base[5]; + for (a[6] = 0; a[6] <= 2; a[6]++) + { + y += a[6] * base[6]; + z += max(a[6], factor[x][6]) * base[6]; + for (a[7] = 0; a[7] <= 2; a[7]++) + { + y += a[7] * base[7]; + z += max(a[7], factor[x][7]) * base[7]; + ans[i][z] = (ans[i][z] + ans[i - 1][y]) % MOD; + y -= a[7] * base[7]; + z -= max(a[7], factor[x][7]) * base[7]; + } + y -= a[6] * base[6]; + z -= max(a[6], factor[x][6]) * base[6]; + } + y -= a[5] * base[5]; + z -= max(a[5], factor[x][5]) * base[5]; + } + y -= a[4] * base[4]; + z -= max(a[4], factor[x][4]) * base[4]; + } + y -= a[3] * base[3]; + z -= max(a[3], factor[x][3]) * base[3]; + } + y -= a[2] * base[2]; + z -= max(a[2], factor[x][2]) * base[2]; + } + y -= a[1] * base[1]; + z -= max(a[1], factor[x][1]) * base[1]; + } + y -= a[0] * base[0]; + z -= max(a[0], factor[x][0]) * base[0]; + } + if (i == n || factor[x][8] < factor[A[i + 1]][8]) + { + for (k = 0; k < V; k++) + { + ans[i][k] = (ans[j][k] + (ans[i][k] - ans[j][k]) * factor[x][8]) % MOD; + } + j = i; + } + } + res = MOD - 1; + for (i = 0; i < V; i++) + if (ans[n][i]) + { + res = (res + val[i] * ans[n][i]) % MOD; + } + printf("%d\n", (res + MOD) % MOD); + } + return 0; +} diff --git a/1254.cpp b/1254.cpp new file mode 100644 index 0000000..60ca31a --- /dev/null +++ b/1254.cpp @@ -0,0 +1,263 @@ +#include + +using namespace std; + +typedef pair ii; +typedef vector vi; +struct stnode +{ + vi topTen; +}; +typedef vector vst; + +#define MAXN 200001 +char T[MAXN], P[MAXN]; +int n, m; +int RA[MAXN], tempRA[MAXN]; +int SA[MAXN], tempSA[MAXN]; +int c[MAXN], phi[MAXN]; + +int OWNER[MAXN], WLEN[MAXN], IDX[MAXN]; +int WPOS[MAXN], LEXICORDER[MAXN]; + +void countSort(int k) +{ + int i, sum, maxi = max(300, n); + memset(c, 0, sizeof c); + for (i = 0; i < n; i++) + c[i + k < n ? RA[i + k] : 0]++; + for (i = sum = 0; i < maxi; i++) + { + int t = c[i]; + c[i] = sum; + sum += t; + } + for (i = 0; i < n; i++) + tempSA[c[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i]; + for (i = 0; i < n; i++) + SA[i] = tempSA[i]; +} + +void makeSA() +{ + int i, k, r; + for (i = 0; i < n; i++) + { + RA[i] = T[i]; + SA[i] = i; + } + for (k = 1; k < n; k <<= 1) + { + countSort(k); + countSort(0); + tempRA[SA[0]] = r = 0; + for (i = 1; i < n; i++) + tempRA[SA[i]] = + (RA[SA[i]] == RA[SA[i - 1]] && RA[SA[i] + k] == RA[SA[i - 1] + k]) ? r : ++r; + for (i = 0; i < n; i++) + RA[i] = tempRA[i]; + } +} + +ii strMatch() +{ + int lo = 0, hi = n - 1, mid = lo; + while (lo < hi) + { + mid = (lo + hi) / 2; + int res = strncmp(T + SA[mid], P, m); + if (res >= 0) + hi = mid; + else + lo = mid + 1; + } + if (strncmp(T + SA[lo], P, m) != 0) + return ii(-1, -1); + ii ans; + ans.first = lo; + lo = 0; + hi = n - 1; + mid = lo; + while (lo < hi) + { + mid = (lo + hi) / 2; + int res = strncmp(T + SA[mid], P, m); + if (res > 0) + hi = mid; + else + lo = mid + 1; + } + if (strncmp(T + SA[hi], P, m) != 0) + hi--; + ans.second = hi; + return ans; +} + +bool topTenCmp(int l, int r) +{ + if (l == r) + return false; + else + l = WPOS[OWNER[SA[l]]], r = WPOS[OWNER[SA[r]]]; + if (WLEN[l] != WLEN[r]) + return WLEN[l] < WLEN[r]; + if (LEXICORDER[l] != LEXICORDER[r]) + return LEXICORDER[l] < LEXICORDER[r]; + return l < r; +} + +vi combineTopTen(const vi &l, const vi &r) +{ + vi res = l; + for (int i = 0; i < (int)r.size(); i++) + { + bool dupe = false; + for (int j = 0; j < (int)res.size(); j++) + { + if (OWNER[SA[r[i]]] == OWNER[SA[res[j]]]) + { + dupe = true; + break; + } + } + if (dupe) + continue; + + res.push_back(r[i]); + for (int i = res.size() - 1; i > 0; i--) + if (topTenCmp(res[i], res[i - 1])) + { + swap(res[i], res[i - 1]); + } + if (res.size() > 10) + res.pop_back(); + } + return res; +} + +// Segment Tree Library: The segment tree is stored like a heap array +void st_build(vst &st, int vertex, int L, int R) +{ + if (L == R) // as L == R, either one is fine + st[vertex].topTen.push_back(L);// store the index + else + {// recursively compute the values in the left and right subtrees + int nL = 2 * vertex, nR = 2 * vertex + 1; + st_build(st, nL, L, (L + R) / 2); + st_build(st, nR, (L + R) / 2 + 1, R); + stnode lContent = st[nL], rContent = st[nR]; + st[vertex].topTen = combineTopTen(lContent.topTen, rContent.topTen); + } +} + +void st_create(vst &st) +{// if original array size is N, + // the required segment tree array length is 2*2^(floor(log2(N)) + 1); + int len = (int)(2 * pow(2.0, floor((log((double)n) / log(2.0)) + 1))); + st.assign(len, stnode()); // create vector of size `len' and fill it with zeroes + st_build(st, 1, 0, n - 1);// recursive build +} + +void st_rmq(vst &st, vi &res, int vertex, int L, int R, int i, int j) +{ + if (i > R || j < L) + { + res.push_back(-1); + return;// current segment outside query range + } + if (L >= i && R <= j) + { + res = st[vertex].topTen;// inside query range + return; + } + // compute the min position in the left and right part of the interval + vi res1, res2; + st_rmq(st, res1, 2 * vertex, L, (L + R) / 2, i, j); + st_rmq(st, res2, 2 * vertex + 1, (L + R) / 2 + 1, R, i, j); + + // return the position where the overall minimum is + if (res1[0] == -1) + { + res = res2; + return;// if we try to access segment outside query + } + if (res2[0] == -1) + { + res = res1; + return;// same as above + } + res = combineTopTen(res1, res2); +} + +void st_rmq(vst &st, vi &res, int i, int j) +{// function overloading + return st_rmq(st, res, 1, 0, n - 1, i, j); +} + +int N, Q; +char s[MAXN]; + +int main() +{ + + WPOS[0] = 0; + T[0] = 0; + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%s", s); + strcat(T, s); + strcat(T, "$"); + + OWNER[WPOS[i]] = i; + WLEN[WPOS[i]] = strlen(s) + 1; + IDX[WPOS[i]] = i + 1; + WPOS[i + 1] = WPOS[i] + WLEN[WPOS[i]]; + for (int j = WPOS[i] + 1; j < WPOS[i + 1]; j++) + { + WLEN[j] = WLEN[j - 1]; + IDX[j] = IDX[j - 1]; + OWNER[j] = OWNER[j - 1]; + } + } + n = strlen(T); + + makeSA(); + + for (int i = 0; i < N; i++) + { + strncpy(P, T + WPOS[i], WLEN[WPOS[i]]); + m = WLEN[WPOS[i]]; + ii pos = strMatch(); + LEXICORDER[WPOS[i]] = pos.first; + } + + vst st; + st_create(st); + + vi res; + scanf("%d", &Q); + for (int i = 0; i < Q; i++) + { + scanf("%s", P); + m = strlen(P); + ii pos = strMatch(); + res.clear(); + st_rmq(st, res, pos.first, pos.second); + + if (res[0] >= 0) + { + for (int i = 0; i < (int)res.size(); i++) + { + if (i) + printf(" "); + printf("%d", IDX[SA[res[i]]]); + } + } + else + printf("-1"); + printf("\n"); + } + + return 0; +} diff --git a/12540.cpp b/12540.cpp new file mode 100644 index 0000000..f767781 --- /dev/null +++ b/12540.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int g[110][110], f[110][1100], d[2][1100], pre[1100][110], n, h; +vector r, rr; +const int inf = 11000000; +char s[110000]; + +int main() +{ + while (scanf("%d %d", &n, &h) != EOF) + { + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + scanf("%d", &g[i][j]); + } + for (int i = 0; i < n; ++i) + for (int j = 0; j < h; ++j) + { + scanf("%d", &f[i][j]); + } + getchar(); + gets(s); + while (gets(s), '0' <= s[0] && s[0] <= '9') + { + int len = strlen(s); + rr.clear(); + r.clear(); + for (int i = 0; i < len; ++i) + if ('0' <= s[i] && s[i] <= '9') + { + int num = 0; + while (i < len && '0' <= s[i] && s[i] <= '9') + { + num = num * 10 + s[i++] - '0'; + } + rr.push_back(num); + } + int nn = rr.size(); + for (int i = nn - 1; i >= 0; --i) + { + r.push_back(rr[i]); + } + for (int i = 1; i < n; ++i) + { + pre[0][i] = 0, d[0][i] = g[i][0] + f[i][r[0]]; + } + for (int i = 1; i < nn; ++i) + { + for (int j = 1; j < n; ++j) + { + d[i & 1][j] = inf; + for (int ff = f[j][r[i]], k = 1; k < n; ++k) + if (d[(i & 1) ^ 1][k] + g[j][k] + ff < d[i & 1][j]) + { + d[i & 1][j] = d[(i & 1) ^ 1][k] + g[j][k] + ff; + pre[i][j] = k; + } + } + } + d[nn & 1][0] = inf; + for (int j = 1; j < n; ++j) + if (d[(nn & 1) ^ 1][j] + g[0][j] < d[nn & 1][0]) + { + d[nn & 1][0] = d[(nn & 1) ^ 1][j] + g[0][j]; + pre[nn][0] = j; + } + printf("%d\n", d[nn & 1][0]); + for (int i = nn, j = 0; i > 0; j = pre[i][j], --i) + { + printf("%d", pre[i][j]); + if (i == 1) + { + puts(""); + } + else + { + printf(" "); + } + } + } + } + return 0; +} diff --git a/12541.cpp b/12541.cpp new file mode 100644 index 0000000..a23c551 --- /dev/null +++ b/12541.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; +} + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +struct person +{ + char name[20]; + int day, month, year; + person() {} + person(char *n, int d, int m, int y) + { + strcpy(name, n); + day = d; + month = m; + year = y; + } +}; + +int main() +{ + int n; + while (scanf("%d", &n) != EOF) + { + person youngest("", 0, 0, 0); + person oldest("", 31, 12, 9999); + person input; + for (int i = 0; i < n; i++) + { + scanf("%s %d %d %d", input.name, &input.day, &input.month, &input.year); + + // youngest + if (input.year > youngest.year) + youngest = input; + else if (input.year == youngest.year && input.month > youngest.month) + youngest = input; + else if (input.year == youngest.year && input.month == youngest.month && input.day > youngest.day) + youngest = input; + + // oldest + if (input.year < oldest.year) + oldest = input; + else if (input.year == oldest.year && input.month < oldest.month) + oldest = input; + else if (input.year == oldest.year && input.month == oldest.month && input.day < oldest.day) + oldest = input; + } + printf("%s\n%s\n", youngest.name, oldest.name); + } + return 0; +} diff --git a/12542.cpp b/12542.cpp new file mode 100644 index 0000000..37386a0 --- /dev/null +++ b/12542.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define M 100000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; +} + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +bool mark[M + 100]; +map stringMap; + +void sieve() +{ + Set(mark, true); + mark[0] = mark[1] = false; + for (int i = 4; i < M; i += 2) + { + mark[i] = false; + } + for (int i = 3; i * i <= M; i += 2) + if (mark[i]) + for (int j = i * i; j < M; j += 2 * i) + { + mark[j] = false; + } +} + +void generateStringMap() +{ + stringMap["2"] = true; + char str[10]; + for (int i = 3; i < M; i += 2) + if (mark[i]) + { + sprintf(str, "%d", i); + stringMap[str] = true; + } +} + +int main() +{ + sieve(); + generateStringMap(); + string input; + while (cin >> input && input != "0") + { + int len = input.size(); + int ret = -1; + for (int digit = 5; digit >= 1; digit--) + for (int i = 0; i + digit - 1 < len; i++) + if (stringMap[input.substr(i, digit)]) + ret = max(ret, atoi(input.substr(i, digit).c_str())); + printf("%d\n", ret); + } + return 0; +} diff --git a/12543.cpp b/12543.cpp new file mode 100644 index 0000000..ae51148 --- /dev/null +++ b/12543.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +char ans[100048], text[100048]; +bool valid[256]; + +int main() +{ + for (int i = 0; i < 256; ++i) + { + valid[i] = false; + } + for (int c = 'a'; c <= 'z'; ++c) + { + valid[c] = true; + } + for (int c = 'A'; c <= 'Z'; ++c) + { + valid[c] = true; + } + valid['-'] = true; + int max_len = 0; + for (int i, j, len; fgets(text, 100048, stdin);) + { + for (i = 0; text[i]; i = j + 1) + { + for (j = i; valid[text[j]]; ++j) + ; + text[j] = 0; + if ((len = j - i) > max_len) + { + strcpy(ans, text + i); + max_len = len; + } + if (!strcmp(text + i, "E-N-D")) + { + break; + } + } + if (!strcmp(text + i, "E-N-D")) + { + break; + } + } + for (int i = 0; i < max_len; ++i) + { + ans[i] |= (ans[i] != '-') << 5; + } + puts(ans); + return 0; +} diff --git a/12544.cpp b/12544.cpp new file mode 100644 index 0000000..e5e3fce --- /dev/null +++ b/12544.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +const int INF = 1000000000; +const int NN = 505; + +int cases, caseno; +int n, m, adj[NN][NN], deg[NN], d[NN], pr[NN]; + +int bfs(int s) +{ + queue q; + int res = INF; + for (int i = 0; i < n; i++) + { + d[i] = INF; + } + d[s] = 0; + pr[s] = -1; + q.push(s); + while (!q.empty()) + { + int u = q.front(); + q.pop(); + for (int i = 0; i < deg[u]; i++) + { + int v = adj[u][i]; + // printf("%d %d %d\n",u,v,pr[u]); + if (v != pr[u]) + { + if (d[v] == INF) + { + d[v] = d[u] + 1; + pr[v] = u; + q.push(v); + } + else + { + res = min(res, d[v] + d[u] + 1); + if (res == 3) + { + return 3; + } + } + } + } + } + return res; +} + +int main() +{ + double cl = clock(); + scanf("%d", &cases); + while (cases--) + { + scanf("%d %d", &n, &m); + memset(deg, 0, sizeof(deg)); + for (int i = 0; i < m; i++) + { + int u, v; + scanf("%d %d", &u, &v); + adj[u][deg[u]++] = v; + adj[v][deg[v]++] = u; + } + int res = INF; + for (int u = 0; u < n && res != 3; u++) + { + res = min(res, bfs(u)); + } + printf("Case %d: ", ++caseno); + if (res == INF) + { + puts("impossible"); + } + else + { + printf("%d\n", res); + } + } + fprintf(stderr, "Total Time: %lf\n", (clock() - cl) / CLOCKS_PER_SEC); + return 0; +} diff --git a/12545.cpp b/12545.cpp new file mode 100644 index 0000000..cbcabcb --- /dev/null +++ b/12545.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int NN = 256; +char a[NN], b[NN]; + +int main() +{ + int t; + scanf("%d", &t); + for (int c = 0; t-- && scanf("%s %s", a, b) == 2;) + { + int otz = 0, q = 0, za = 0, zb = 0, zto = 0; + for (int i = 0; a[i]; ++i) + { + za += a[i] == '0'; + zb += b[i] == '0'; + otz += a[i] == '1' && b[i] == '0'; + zto += a[i] == '0' && b[i] == '1'; + q += a[i] == '?'; + } + printf("Case %d: ", ++c); + if (zb > za + q) + { + puts("-1"); + } + else + { + printf("%d\n", otz + zto - min(otz, zto) + q); + } + } + return 0; +} diff --git a/12546.cpp b/12546.cpp new file mode 100644 index 0000000..66bbe38 --- /dev/null +++ b/12546.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define MOD 1000000007 + +typedef long long i64; + +int C, P, a; + +void ext_euclid(int a, int b, int &x, int &y, int &gcd) +{ + x = 0; + y = 1; + gcd = b; + int m, n, q, r; + for (int u = 1, v = 0; a != 0; gcd = a, a = r) + { + q = gcd / a; + r = gcd % a; + m = x - u * q; + n = y - v * q; + x = u; + y = v; + u = m; + v = n; + } +} + +int mod_inv(int n, int m) +{ + int x, y, gcd; + ext_euclid(n, m, x, y, gcd); + if (gcd == 1) + { + return x % m; + } + return 0; +} + +int mod_pow(int _b, i64 e, int m) +{ + i64 res = 1; + for (i64 b = _b; e; e >>= 1, b = b * b % m) + if (e & 1) + { + res = res * b % m; + } + return res; +} + +int main() +{ + int T; + scanf("%d", &T); + int ncase = 0; + while (T--) + { + scanf("%d", &C); + i64 ans = 1; + i64 fac = 1; + while (C--) + { + scanf("%d%d", &P, &a); + ans = (ans * mod_pow(P, a, MOD)) % MOD; + i64 x = mod_pow(P, a, MOD) - 1 + MOD; + x = x * (mod_inv(P - 1, MOD) + MOD) % MOD; + i64 y = a + 1; + y = y * mod_pow(P, a, MOD) % MOD; + x = (x + y) % MOD; + fac = fac * x % MOD; + } + ans = (ans + fac) % MOD; + printf("Case %d: %lld\n", ++ncase, ans); + } + return 0; +} diff --git a/12547.cpp b/12547.cpp new file mode 100644 index 0000000..66dc468 --- /dev/null +++ b/12547.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +const int N = 55 * 3; +const int INF = 0x3f3f3f3f; + +char ss[20000]; +char str[N]; +int dp[N][N][N]; +int limit; + +char match(char ch) +{ + if (ch == 'A') + return 'U'; + else if (ch == 'U') + return 'A'; + else if (ch == 'C') + return 'G'; + else + return 'C'; +} +bool isau(char a, char b) +{ + return (a == 'A' && b == 'U') || (a == 'U' && b == 'A'); +} +bool iscg(char a, char b) +{ + return (a == 'C' && b == 'G') || (a == 'G' && b == 'C'); +} +void work() +{ + scanf("%s%d", ss, &limit); + int ls = strlen(ss); + int len = 0; + int left = 1, right = ls - 1; + while (left < ls && isdigit(ss[left])) + left++; + while (isdigit(ss[right])) + right--; + if (left == ls) + { + puts("0"); + return; + } + + int res = 0; + char lch = ss[0]; + int lcnt = 0; + for (int i = 1; i < left; i++) + lcnt = lcnt * 10 + ss[i] - '0'; + char rch = ss[right]; + int rcnt = 0; + for (int i = right + 1; i < ls; i++) + rcnt = rcnt * 10 + ss[i] - '0'; + + if (isau(lch, rch)) + { + int d = min(lcnt, rcnt); + res += d; + lcnt -= d; + rcnt -= d; + } + if (iscg(lch, rch)) + { + int d = min(lcnt, rcnt); + d = min(d, limit); + res += d; + lcnt -= d; + rcnt -= d; + limit -= d; + } + str[0] = 0; + int p = left; + while (p < right) + { + char ch = ss[p++]; + int k = 0; + while (p < right && isdigit(ss[p])) + k = k * 10 + ss[p++] - '0'; + for (int i = 0; i < k; i++) + str[++len] = ch; + } + int cnt = 0; + for (int i = 1; i <= len; i++) + if (str[i] == match(lch)) + cnt++; + if (lcnt > cnt) + lcnt = cnt; + cnt = 0; + for (int i = 1; i <= len; i++) + if (str[i] == match(rch)) + cnt++; + if (rcnt > len) + rcnt = len; + for (int i = 0; i < rcnt; i++) + str[++len] = rch; + reverse(str + 1, str + len + 1); + for (int i = 0; i < lcnt; i++) + str[++len] = lch; + + //printf("%d\n", res); + //puts(str+1); + memset(dp, 0, sizeof(dp)); + for (int c = 0; c <= limit; c++) + for (int s = 2; s <= len; s++) + for (int i = 1; i + s - 1 <= len; i++) + { + int j = i + s - 1; + for (int k = i; k < j; k++) + { + for (int c1 = 0; c1 <= c; c1++) + dp[i][j][c] = max(dp[i][j][c], dp[i][k][c1] + dp[k + 1][j][c - c1]); + } + if (isau(str[i], str[j])) + dp[i][j][c] = max(dp[i][j][c], dp[i + 1][j - 1][c] + 1); + if (c > 0 && iscg(str[i], str[j])) + dp[i][j][c] = max(dp[i][j][c], dp[i + 1][j - 1][c - 1] + 1); + } + int ans = 0; + for (int c = 0; c <= limit; c++) + ans = max(ans, dp[1][len][c]); + printf("%d\n", ans + res); +} + +int main() +{ + int T, C = 0; + scanf("%d", &T); + while (T--) + { + printf("Case %d: ", ++C); + work(); + } + return 0; +} diff --git a/12548.cpp b/12548.cpp new file mode 100644 index 0000000..4be0c8f --- /dev/null +++ b/12548.cpp @@ -0,0 +1,245 @@ +#include + +using namespace std; + +double EPS = 1e-9; +int tst = 0, n, MOD = 1000003, head, tail, cnt; +long long hx, hy, now_hx, now_hy, tx, ty, cx, cy, tot, ao, tnt[711][711], tct[711][711], now_tnt, now_tct, now_cont, cont[711][711], ahx[711][711], ahy[711][711], anow_hx[711][711], anow_hy[711][711]; + +struct point +{ + int x, y, hx, hy; + double theta; + int id; + bool operator<(const point &temp) const + { + return theta < temp.theta; + } +}; +point pt[1001], adj[711][711], queue_[111111]; + +int solve() +{ + int i, j, s, p, q; + tot = ao = 0; + cnt = 0; + for (i = 0; i < n; i++) + { + cnt = 0; + for (j = 0; j < n; j++) + { + if (i == j) + { + continue; + } + adj[i][cnt] = pt[j]; + adj[i][cnt].id = j; + adj[i][cnt++].theta = atan2((double)(pt[j].y - pt[i].y), (double)(pt[j].x - pt[i].x)); + } + sort(adj[i], adj[i] + cnt); + head = tail = 0; + now_cont = 0; + for (j = 0; j < cnt; j++) + { + int smin; + if (head == tail) + { + smin = j + 1; + } + else + { + smin = queue_[tail - 1].id + 1; + } + for (int ss = smin; ss < j + cnt; ss++) + { + s = ss % cnt; + while (head < tail) + { + if (queue_[head].id <= j) + { + head++; + now_cont--; + } + else + { + break; + } + } + if (ss <= j) + { + continue; + } + if (fabs(2 * M_PI * (ss / cnt) + adj[i][s].theta - adj[i][j].theta) < M_PI) + { + queue_[tail] = adj[i][s]; + queue_[tail++].id = ss; + now_cont++; + } + else + { + break; + } + } + cont[i][j] = now_cont; + } + head = tail = 0; + hx = hy = now_hx = now_hy = now_tnt = 0; + for (j = 0; j < cnt; j++) + { + int smin; + if (head == tail) + { + smin = j + 1; + } + else + { + smin = queue_[tail - 1].id + 1; + } + for (int ss = smin; ss < j + cnt; ss++) + { + s = ss % cnt; + while (head < tail) + { + if (queue_[head].id <= j) + { + int ig = queue_[head].id % cnt; + head++; + now_tnt -= cont[i][ig]; + now_tnt += tail - head; + hx -= adj[i][ig].x; + hy -= adj[i][ig].y; + now_hx -= (tail - head) * adj[i][ig].x % MOD; + now_hy -= (tail - head) * adj[i][ig].y % MOD; + } + else + { + break; + } + } + if (ss <= j) + { + continue; + } + if (fabs(2 * M_PI * (ss / cnt) + adj[i][s].theta - adj[i][j].theta) < M_PI) + { + now_hx += hx; + now_hy += hy; + hx += adj[i][s].x; + hy += adj[i][s].y; + now_tnt += cont[i][s]; + now_tnt -= tail - head; + queue_[tail] = adj[i][s]; + queue_[tail++].id = ss; + } + else + { + break; + } + } + ahx[i][j] = hx; + ahy[i][j] = hy; + tnt[i][j] = now_tnt; + anow_hx[i][j] = now_hx; + anow_hy[i][j] = now_hy; + } + } + tot = ao = 0; + for (i = 0; i < n; i++) + { + head = tail = 0; + cx = cy = 0; + for (j = 0; j < cnt; j++) + { + int smin; + long long temp, tey; + temp = cont[i][j] * adj[i][j].x; + temp = ((long long)(ahx[i][j] - temp) * (long long)(pt[i].y - adj[i][j].y)); + tey = cont[i][j] * adj[i][j].y % MOD; + tey = ((long long)(ahy[i][j] - tey) * (long long)(pt[i].x - adj[i][j].x)); + temp = (temp - tey + MOD); + tot += (long long)(cnt - 1 - cont[i][j]) * (long long)temp % MOD; + if (head == tail) + { + smin = j + 1; + } + else + { + smin = queue_[tail - 1].id + 1; + } + for (int ss = smin; ss < j + cnt; ss++) + { + s = ss % cnt; + while (head < tail) + { + if (queue_[head].id <= j) + { + int ig = queue_[head].id % cnt; + cx -= cont[i][ig] * adj[i][ig].x % MOD; + cy -= cont[i][ig] * adj[i][ig].y % MOD; + head++; + } + else + { + break; + } + } + if (ss <= j) + { + continue; + } + if (fabs(2 * M_PI * (ss / cnt) + adj[i][s].theta - adj[i][j].theta) < M_PI) + { + cx += cont[i][s] * adj[i][s].x % MOD; + cy += cont[i][s] * adj[i][s].y % MOD; + queue_[tail] = adj[i][s]; + queue_[tail++].id = ss; + } + else + { + break; + } + } + temp = (cx - anow_hx[i][j]) - (long long)tnt[i][j] * (long long)adj[i][j].x % MOD; + temp = (long long)temp * (long long)(pt[i].y - adj[i][j].y) % MOD; + tey = (cy - anow_hy[i][j]) - (int)((long long)tnt[i][j] * (long long)adj[i][j].y % MOD); + tey = (tey % MOD + MOD) % MOD; + tey = (int)((long long)tey * (long long)(pt[i].x - adj[i][j].x) % MOD); + temp = (temp - tey + MOD) % MOD; + ao = (ao + temp) % MOD; + } + } + ao = (ao * 2) % MOD; + tot = (tot - ao) % MOD; + if (tot < 0) + { + tot += MOD; + } + if (tot % 2) + { + tot = (tot + MOD) / 2; + } + else + { + tot /= 2; + } + return ((tot + ao) % MOD + MOD) % MOD; +} + +int main() +{ + int t, x, y, i, j, s, p, q; + scanf("%d", &t); + while (t--) + { + tst++; + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", &x, &y); + pt[i].x = x; + pt[i].y = y; + } + printf("Case %d: %d\n", tst, solve()); + } + return 0; +} diff --git a/12549.cpp b/12549.cpp new file mode 100644 index 0000000..37b4ed4 --- /dev/null +++ b/12549.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +#define M 50000 +#define oo 1000000000 + +int head[M], work[M], mark[M], Q[M * 10], dist[M], tot, src, dest; + +struct node +{ + int to, cap, next; +} edge[M * 10]; + +void add(int a, int b, int c) +{ + edge[tot].to = b, edge[tot].cap = c, edge[tot].next = head[a], head[a] = tot++; + edge[tot].to = a, edge[tot].cap = 0, edge[tot].next = head[b], head[b] = tot++; +} + +int min(int a, int b) +{ + return a < b ? a : b; +} + +bool BFS() +{ + int i, j, k, l, h, y; + for (i = 0; i <= dest; i++) + { + dist[i] = -1; + } + dist[src] = 0; + l = h = 0; + Q[l++] = src; + while (h < l) + { + k = Q[h++]; + for (i = head[k]; i != -1; i = edge[i].next) + { + y = edge[i].to; + if (edge[i].cap > 0 && dist[y] == -1) + { + dist[y] = dist[k] + 1; + Q[l++] = y; + } + } + } + return (dist[dest] >= 0); +} +int DFS(int x, int exp) +{ + mark[x] = 1; + if (x == dest) + { + return exp; + } + for (int temp, y, &i = work[x]; i != -1; i = edge[i].next) + { + y = edge[i].to; + if (edge[i].cap > 0 && dist[y] == dist[x] + 1 && !mark[y]) + { + if ((temp = DFS(y, min(edge[i].cap, exp))) > 0) + { + edge[i].cap -= temp; + edge[i ^ 1].cap += temp; + return temp; + } + } + } + return 0; +} +int Dinic_flow() +{ + int ans = 0, flow, i; + while (BFS()) + { + for (i = 0; i <= dest; i++) + work[i] = head[i]; + while (1) + { + for (i = 0; i <= dest; i++) + mark[i] = 0; + flow = DFS(src, oo); + if (flow == 0) + break; + ans += flow; + } + } + return ans; +} + +int row[11000], col[11000], mrow, mcol; +char mp[110][110]; + +int main() +{ + int i, j, n, m, T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + mp[i][j] = '.'; + int c, a, b; + scanf("%d", &c); + for (i = 1; i <= c; i++) + { + scanf("%d%d", &a, &b); + mp[a][b] = '*'; + } + scanf("%d", &c); + for (i = 1; i <= c; i++) + { + scanf("%d%d", &a, &b); + mp[a][b] = '#'; + } + for (i = 1; i <= n; i++) + row[i] = i; + for (i = 1; i <= m; i++) + col[i] = i; + mrow = n, mcol = m; + src = 0, tot = 0, dest = 20020; + for (i = src; i <= dest; i++) + head[i] = -1; + for (i = 1; i <= n; i++) + { + for (j = 1; j <= m; j++) + { + if (mp[i][j] == '#') + { + row[i] = ++mrow; + col[j] = ++mcol; + } + if (mp[i][j] == '*') + add(row[i], col[j] + 10005, 1); + } + } + for (i = 1; i <= mrow; i++) + add(src, i, 1); + for (i = 1; i <= mcol; i++) + add(i + 10005, dest, 1); + printf("%d\n", Dinic_flow()); + } + return 0; +} diff --git a/1255.cpp b/1255.cpp new file mode 100644 index 0000000..7dd8d22 --- /dev/null +++ b/1255.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +typedef struct List +{ + int st, ed; +} LIST; +LIST lst[305]; + +typedef struct Node +{ + int t, id; +} NODE; +NODE node[610]; + +int dp[605][605]; + +int cmp(const void *a, const void *b); +int cmp1(const void *a, const void *b); + +int main() +{ + int T, t, i, j, k, N, n, len, top; + scanf(" %d", &T); + for (t = 0; t < T; t++) + { + scanf(" %d", &N); + for (n = 0; n < N; n++) + { + scanf(" %d %d", &lst[n].st, &lst[n].ed); + } + qsort(lst, N, sizeof(lst[0]), cmp); + for (i = top = 0; i < N; i++) + { + node[top].t = lst[i].st; + node[top].id = (i + 1) * 10; + top++; + node[top].t = lst[i].ed; + node[top].id = (i + 1) * 10 + 1; + top++; + } + qsort(node, top, sizeof(node[0]), cmp1); + memset(dp, 0, sizeof(dp)); + for (len = 2; len <= top; len++) + { + for (i = 0; i <= top - len; i++) + { + int MAX = 0; + k = i + len - 1; + for (j = i; j <= k; j++) + { + if (node[i].id / 10 == node[k].id / 10) + { + MAX = max(MAX, dp[i][j] + dp[j][k] + 1); + } + else + { + MAX = max(MAX, dp[i][j] + dp[j][k]); + } + } + dp[i][k] = MAX; + } + } + printf("%d\n", dp[0][top - 1]); + } + return 0; +} + +int cmp(const void *a, const void *b) +{ + LIST c, d; + c = *(LIST *)(a); + d = *(LIST *)(b); + if (c.st != d.st) + { + return c.st > d.st ? 1 : -1; + } + else if (c.ed != d.ed) + { + return c.ed > d.ed ? -1 : 1; + } + return 0; +} + +int cmp1(const void *a, const void *b) +{ + NODE c, d; + c = *(NODE *)(a); + d = *(NODE *)(b); + if (c.t != d.t) + { + return c.t > d.t ? 1 : -1; + } + else if (c.id % 2 == 1 && d.id % 2 == 1) + { + return c.id > d.id ? -1 : 1; + } + else if (c.id % 2 == 1 && d.id % 2 == 0) + { + return -1; + } + else if (c.id % 2 == 0 && d.id % 2 == 1) + { + return 1; + } + else if (c.id % 2 == 0 && d.id % 2 == 0) + { + return c.id > d.id ? 1 : -1; + } +} diff --git a/12550.cpp b/12550.cpp new file mode 100644 index 0000000..9ccab8f --- /dev/null +++ b/12550.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +const double eps = 1e-8; + +char s[1000000]; +double a[1000000]; + +inline int sig(double x) +{ + return (x > eps) - (x < -eps); +} + +int main() +{ + while (gets(s)) + { + int ans = 0; + int lens = strlen(s); + double tt = 0; + bool yes = 0; + for (int i = 0; i < lens; i++) + { + if (s[i] >= '0' && s[i] <= '9') + { + tt = tt * 10 + s[i] - '0'; + yes = 1; + } + else if (yes) + { + a[++ans] = tt; + yes = 0; + tt = 0; + } + } + if (tt) + { + a[++ans] = tt; + } + double p = a[2]; + int d = a[1]; + int temp = -1; + for (int i = 3; i <= ans; i++) + { + a[++temp] = a[i]; + } + if (sig(a[0] - p) > 0) + { + printf("The spider is going to fall!\n"); + continue; + } + if (temp < d) + { + double ts1, ts2; + ts1 = a[1] * a[3] - a[2] * a[2]; + ts1 /= (a[1] * a[1] - a[0] * a[2]); + ts2 = (a[3] - ts1 * a[1]) / a[2]; + for (int i = temp + 1; i <= d; i++) + { + a[i] = ts2 * a[i - 1] + ts1 * a[i - 2]; + } + } + int res = 0; + for (int i = 0; i <= d; i++) + { + if (sig(a[i] - p) <= 0) + res = i; + else + break; + } + if (res == d) + printf("The spider may fall!\n"); + else + printf("%d\n", d - res); + } + return 0; +} diff --git a/12551.cpp b/12551.cpp new file mode 100644 index 0000000..093d207 --- /dev/null +++ b/12551.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +struct node +{ + int now, tom; +} cas[510]; + +int gcd(int a, int b) +{ + if (a > b) + { + swap(a, b); + } + while (a) + { + int m = a; + a = b % a; + b = m; + } + return b; +} + +int vals1[51000], vals2[51000], n, p; +int dp[5100000], c, num[51000]; + +int main() +{ + bool yes = 0; + while (scanf("%d", &c) != EOF) + { + scanf("%d%d", &n, &p); + for (int i = 1; i <= n; i++) + { + scanf("%d%d", &cas[i].now, &cas[i].tom); + } + int ans; + for (int i = 1; i <= p; i++) + { + scanf("%d", &num[i]); + int res1 = 0, res2 = 0, x, y; + for (int j = 1; j <= num[i]; j++) + { + scanf("%d%d", &x, &y); + res1 += cas[x].now * y; + res2 += cas[x].tom * y; + } + if (res1 > c || res2 < res1) + { + p--; + i--; + continue; + } + if (i == 1) + { + ans = res1; + } + else + { + ans = gcd(ans, res1); + } + vals1[i] = res1; + vals2[i] = res2; + } + memset(dp, 0, sizeof(dp)); + int t = 0; + int px = c / ans; + for (int i = 1; i <= p; i++) + { + t = px; + int x = vals1[i] / ans; + for (int j = px * ans; j >= ans * x; j -= ans) + { + dp[t] = max(dp[t], dp[t - x] + vals2[i] - vals1[i]); + t--; + } + } + if (yes) + { + printf("\n"); + } + yes = 1; + printf("%d\n", dp[px]); + } + return 0; +} diff --git a/12553.cpp b/12553.cpp new file mode 100644 index 0000000..44b42c5 --- /dev/null +++ b/12553.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +#define N 100000 + +struct Item +{ + int num; + int a, b; + char op; + void print() + { + printf("%d %c %d = %d\n", a, op, b, num); + } +} item[10]; +int target; +Item arr[8]; +int t, ans, md; +inline int dist(int x, int y) +{ + if (x > y) + { + return x - y; + } + else + { + return y - x; + } +} +bool f, g; + +void calc(Item a[], int size, int tar) +{ + int i, j, k, l, tmp; + Item b[10], m, n; + if (dist(a[size - 1].num, tar) < md) + { + ans = a[size - 1].num; + md = dist(a[size - 1].num, tar); + } + if (g) + { + return; + } + if (md == 0) + { + if (f) + { + for (int i = 0; i < 6 - size; ++i) + { + item[i].print(); + } + g = true; + } + return; + } + if (size == 1) + { + return; + } + for (int i = 0; i < size - 1; ++i) + { + for (int j = i + 1; j < size; ++j) + { + m = a[i]; + n = a[j]; + for (k = 0, l = 0; k < size; ++k) + if (k != i && k != j) + { + b[l++] = a[k]; + } + b[size - 2].num = m.num + n.num; + b[size - 2].a = m.num; + b[size - 2].b = n.num; + b[size - 2].op = '+'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + if (m.num > n.num) + { + b[size - 2].num = m.num - n.num; + b[size - 2].a = m.num; + b[size - 2].b = n.num; + b[size - 2].op = '-'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + } + else if (m.num < n.num) + { + b[size - 2].num = n.num - m.num; + b[size - 2].a = n.num; + b[size - 2].b = m.num; + b[size - 2].op = '-'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + } + b[size - 2].num = m.num * n.num; + b[size - 2].a = m.num; + b[size - 2].b = n.num; + b[size - 2].op = '*'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + if (m.num % n.num == 0) + { + b[size - 2].num = m.num / n.num; + b[size - 2].a = m.num; + b[size - 2].b = n.num; + b[size - 2].op = '/'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + } + else if (n.num % m.num == 0) + { + b[size - 2].num = n.num / m.num; + b[size - 2].a = n.num; + b[size - 2].b = m.num; + b[size - 2].op = '/'; + if (f) + { + item[6 - size] = b[size - 2]; + } + calc(b, size - 1, tar); + } + } + } +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + for (int i = 0; i < 6; ++i) + { + scanf("%d", &arr[i].num); + } + scanf("%d", &target); + g = false; + for (int i = 0; i < 6; ++i) + if (arr[i].num == target) + { + g = true; + break; + } + if (g) + { + printf("Target: %d\nBest approx: %d\n\n", target, target); + } + else + { + md = N; + g = false; + f = false; + calc(arr, 6, target); + printf("Target: %d\n", target); + md = N; + f = true; + g = false; + calc(arr, 6, ans); + printf("Best approx: %d\n\n", ans); + } + } + return 0; +} diff --git a/12554.cpp b/12554.cpp new file mode 100644 index 0000000..8ebfe0d --- /dev/null +++ b/12554.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; +} + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +char ppl[100 + 5][100 + 5]; + +int main() +{ + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s", ppl[i]); + } + // how many lines would get printed + int loop; + if (n % 16) + loop = n / 16 + 1; + else + loop = n / 16; + loop *= 16; + char str[5][10] = {"Happy", "birthday", "to", "you", "Rujia"}; + int pplIndex = 0; + int strIndex = 0; + // "Rujia" will be printed on every 12th of 16th times + int cnt = 1; + for (int i = 0; i < loop; i++) + { + printf("%s: ", ppl[pplIndex++]); + pplIndex %= n; + if (cnt % 12 == 0) + { + // printing "Rujia" + printf("%s\n", str[4]); + strIndex = 0; + } + else + { + printf("%s\n", str[strIndex++]); + strIndex %= 4; + } + cnt++; + // one full cycle song completed + if (cnt > 16) + { + cnt = 1; + } + } + return 0; +} diff --git a/12555.cpp b/12555.cpp new file mode 100644 index 0000000..148b078 --- /dev/null +++ b/12555.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + ret *= b; + return ret; +} + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +int main() +{ + int testCases; + scanf("%d", &testCases); + int cases = 0; + while (testCases--) + { + char inp[20]; + scanf("%s", inp); + int len = strlen(inp); + int a = 0; + int b = 0; + char firstStr[10]; + int result = 0; + printf("Case %d: ", ++cases); + sscanf(inp, "%d%s", &a, firstStr); + bool digitFound = true; + for (int i = 0; i < len; i++) + { + if (isdigit(inp[i]) && digitFound) + continue; + digitFound = false; + if (isdigit(inp[i])) + { + digitFound = true; + b = inp[i] - '0'; + break; + } + } + result = a * 50; + result += b * 5; + printf("%d", result / 100); + result %= 100; + if (result) + if (result % 10 == 0) + printf(".%d", result / 10); + else + printf(".%d", result); + printf("\n"); + } + return 0; +} diff --git a/12556.cpp b/12556.cpp new file mode 100644 index 0000000..fc3a599 --- /dev/null +++ b/12556.cpp @@ -0,0 +1,252 @@ +#include + +using namespace std; + +double d(double x1, double y1, double x2, double y2) +{ + return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); +} + +double m(double x1, double y1, double x2, double y2) +{ + return (y1 - y2) / (x1 - x2); +} + +double ix(double x1, double y1, double x2, double y2, double m1, double m2) +{ + return (m1 * x1 - m2 * x2 + y2 - y1) / (m1 - m2); +} + +int main() +{ + int T; scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + double xA, yA, xB, yB, xC, yC; + scanf("%lf%lf%lf%lf%lf%lf", &xA, &yA, &xB, &yB, &xC, &yC); + double AB, AC, BC, P; + AB = d(xA, yA, xB, yB); + AC = d(xA, yA, xC, yC); + BC = d(xB, yB, xC, yC); + P = AB + AC + BC; + double pAB, pAC, pBC; + pAB = P / 2 - AC; + pAC = P / 2 - BC; + pBC = P / 2 - AB; + if (pAB < 0 || pAC < 0 || pBC < 0) + { + printf("Case %d: ERROR\n", t); + } + else + { + bool fAB = false, fAC = false, fBC = false; + double mAB, mAC, mBC, iAB, iAC, iBC; + if (xA != xB) + { + mAB = m(xA, yA, xB, yB); + } + else + { + fAB = true; + } + if (xA != xC) + { + mAC = m(xA, yA, xC, yC); + } + else + { + fAC = true; + } + if (xB != xC) + { + mBC = m(xB, yB, xC, yC); + } + else + { + fBC = true; + } + if (!fAB) + { + iAB = yA - mAB * xA; + } + if (!fAC) + { + iAC = yA - mAC * xA; + } + if (!fBC) + { + iBC = yB - mBC * xB; + } + double aAB, aAC, aBC; + if (!fAB) + { + aAB = atan(mAB); + if (aAB > M_PI / 2) + { + aAB = M_PI / 2 - aAB; + } + } + if (!fAC) + { + aAC = atan(mAC); + if (aAC > M_PI / 2) + { + aAC = M_PI / 2 - aAC; + } + } + if (!fBC) + { + aBC = atan(mBC); + if (aBC > M_PI / 2) + { + aBC = M_PI / 2 - aBC; + } + } + double xAB, xAC, xBC; + if (!fAB) + { + if (xA < xB) + { + xAB = xA + pAB * cos(aAB); + } + else + { + xAB = xA - pAB * cos(aAB); + } + } + else + { + xAB = xA; + } + if (!fAC) + { + if (xC < xA) + { + xAC = xC + pAC * cos(aAC); + } + else + { + xAC = xC - pAC * cos(aAC); + } + } + else + { + xAC = xC; + } + if (!fBC) + { + if (xB < xC) + { + xBC = xB + pBC * cos(aBC); + } + else + { + xBC = xB - pBC * cos(aBC); + } + } + else + { + xBC = xB; + } + double yAB, yAC, yBC; + if (!fAB) + { + yAB = xAB * mAB + iAB; + } + else + { + if (yA > yB) + { + yAB = yA - pAB; + } + else + { + yAB = yA + pAB; + } + } + if (!fAC) + { + yAC = xAC * mAC + iAC; + } + else + { + if (yC > yA) + { + yAC = yC - pAC; + } + else + { + yAC = yC + pAC; + } + } + if (!fBC) + { + yBC = xBC * mBC + iBC; + } + else + { + if (yB > yC) + { + yBC = yB - pBC; + } + else + { + yBC = yB + pBC; + } + } + bool f_A = false, f_B = false, f_C = false; + double m_A, m_B, m_C, i_A, i_B, i_C; + if (xA != xBC) + { + m_A = m(xA, yA, xBC, yBC); + i_A = yA - m_A * xA; + } + else + { + f_A = true; + } + if (xB != xAC) + { + m_B = m(xB, yB, xAC, yAC); + i_B = yB - m_B * xB; + } + else + { + f_B = true; + } + if (xC != xAB) + { + m_C = m(xC, yC, xAB, yAB); + i_C = yC - m_C * xC; + } + else + { + f_C = true; + } + double x_AB, x_AC, x_BC, y_AB, y_AC, y_BC; + if (!f_A && !f_B) + { + x_AB = ix(xA, yA, xB, yB, m_A, m_B); + y_AB = m_A * x_AB + i_A; + printf("Case %d: %.6lf %.6lf\n", t,x_AB,y_AB); + } + else if (!f_A && !f_C) + { + x_AC = ix(xA, yA, xC, yC, m_A, m_C); + y_AC = m_C * x_AC + i_C; + printf("Case %d: %.6lf %.6lf\n", t,x_AC,y_AC); + } + else if (!f_B && !f_C) + { + x_BC = ix(xB, yB, xC, yC, m_B, m_C); + y_BC = m_B * x_BC + i_B; + printf("Case %d: %.6lf %.6lf\n", t,x_BC,y_BC); + } + else + { + printf("Case %d: ERROR\n", t); + } + } + } + return 0; +} diff --git a/12557.cpp b/12557.cpp new file mode 100644 index 0000000..28b0558 --- /dev/null +++ b/12557.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +int solve() +{ + static char s[1000001], t[1000001]; + gets(s); + gets(t); + int m = strlen(s), n = strlen(t); + int last1[128], last2[128]; + char f[128]; + rep(c, 128) last1[c] = last2[c] = -1; + int ans = 0; + rep(i, m - n + 1) + { + bool ok = true; + rep(j, n) + { + if ('A' <= s[i + j] && s[i + j] <= 'Z') + { + if (t[j] != s[i + j]) + { + ok = false; + break; + } + } + else + { + if ('A' <= t[j] && t[j] <= 'Z') + { + ok = false; + break; + } + else if (last1[t[j]] != i) + { + if (last2[s[i + j]] == i) + { + ok = false; + break; + } + else + { + f[t[j]] = s[i + j]; + last1[t[j]] = i; + last2[s[i + j]] = i; + } + } + else if (f[t[j]] != s[i + j]) + { + ok = false; + break; + } + } + } + if (ok) + { + ans++; + } + } + return ans; +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + printf("Case %d: %d\n", cas, solve()); + return 0; +} diff --git a/12558.cpp b/12558.cpp new file mode 100644 index 0000000..b000f15 --- /dev/null +++ b/12558.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +typedef long long LL; +LL ans[10002], v[1002]; +set s; +int maxd; +LL gcd(LL a, LL b) +{ + return b ? gcd(b, a % b) : a; +} +typedef long long LL; +LL get_first(LL a, LL b) +{ + return b / a + 1; +} +bool better(int d) +{ + for (int i = d; i >= 0; i--) + if (v[i] != ans[i]) + { + return ans[i] == -1 || v[i] < ans[i]; + } + return false; +} +bool dfs(int d, LL from, LL aa, LL bb) +{ + if (d == maxd) + { + if (bb % aa) + { + return false; + } + v[d] = bb / aa; + if (s.count(bb / aa)) + { + return false; + } + if (better(d)) + { + memcpy(ans, v, sizeof(LL) * (d + 1)); + } + return true; + } + bool ok = false; + for (LL i = max(from, get_first(aa, bb));; i++) + { + if (bb * (maxd + 1 - d) <= i * aa) + { + break; + } + if (s.count(i)) + { + continue; + } + v[d] = i; + LL b2 = bb * i; + LL a2 = aa * i - bb; + LL g = gcd(a2, b2); + if (dfs(d + 1, i + 1, a2 / g, b2 / g)) + { + ok = true; + } + } + return ok; +} +int main() +{ + int t, k; + LL a, b, num; + scanf("%d", &t); + for (int ii = 1; ii <= t; ii++) + { + s.clear(); + scanf("%lld%lld%d", &a, &b, &k); + for (int i = 0; i < k; i++) + { + scanf("%lld", &num); + s.insert(num); + } + int ok = 0; + for (maxd = 1;; maxd++) + { + memset(ans, -1, sizeof(ans)); + if (dfs(0, get_first(a, b), a, b)) + { + ok = 1; + break; + } + } + printf("Case %d: %lld/%lld=", ii, a, b); + for (int i = 0; i <= maxd; ++i) + { + if (i) + { + printf("+"); + } + printf("1/%lld", ans[i]); + } + printf("\n"); + } + return 0; +} diff --git a/12559.cpp b/12559.cpp new file mode 100644 index 0000000..f3604a2 --- /dev/null +++ b/12559.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const double PI = acos(-1); + +struct point +{ + int x, y; +}; + +struct circle +{ + point c; + int r; + bool operator<(const circle &C) const + { + return make_pair(make_pair(r, c.x), c.y) < make_pair(make_pair(C.r, C.c.x), C.c.y); + } + bool operator==(const circle &C) const + { + return r == C.r && c.x == C.c.x && c.y == C.c.y; + } +}; + +int h, w; +char B[100][101]; + +bool check(const circle &C) +{ + int cnt = 0; + rep(t, 1000) + { + double theta = 2 * PI * t / 1000; + int x = (int)(C.c.x + C.r * cos(theta) + 0.5); + int y = (int)(C.c.y + C.r * sin(theta) + 0.5); + if (0 <= y && y < h && 0 <= x && x < w && B[y][x] == '1') + { + cnt++; + } + } + return cnt > 900; +} + +void solve() +{ + scanf("%d%d", &w, &h); + rep(i, h) scanf("%s", B[i]); + vector H, V; + rep(i, h) rep(j, w) if (B[i][j] == '1') + { + int x = j; + while (j < w && B[i][j] == '1' || j + 1 < w && B[i][j + 1] == '1') + { + j++; + } + if (j - x >= 5) + { + H.push_back((point){ + x, i}); + } + } + rep(j, w) rep(i, h) if (B[i][j] == '1') + { + int y = i; + while (i < h && B[i][j] == '1' || i + 1 < h && B[i + 1][j] == '1') + { + i++; + } + if (i - y >= 5) + { + V.push_back((point){ + j, y}); + } + } + vector C; + rep(j, H.size()) rep(i, j) + { + int d1 = abs(H[j].y - H[i].y); + if (d1 % 2 != 0 || d1 < 10) + { + continue; + } + int r1 = d1 / 2; + rep(l, V.size()) rep(k, l) + { + int d2 = abs(V[l].x - V[k].x); + if (d2 % 2 != 0 || d2 < 10) + { + continue; + } + int r2 = d2 / 2; + if (r1 == r2) + { + int y = min(H[i].y, H[j].y) + r1; + int x = min(V[k].x, V[l].x) + r2; + C.push_back((circle){ + (point){x, y}, r1}); + } + } + } + sort(C.begin(), C.end()); + C.erase(unique(C.begin(), C.end()), C.end()); + rep(i, C.size()) if (!check(C[i])) + { + C.erase(C.begin() + i), i--; + } + printf("%d", C.size()); + rep(i, C.size()) printf(" (%d,%d,%d)", C[i].r, C[i].c.x, C[i].c.y); + puts(""); +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + printf("Case %d: ", cas), solve(); + return 0; +} diff --git a/1256.cpp b/1256.cpp new file mode 100644 index 0000000..179cfa1 --- /dev/null +++ b/1256.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +template +T gcd(T a, T b) +{ + T c; + while (b) + { + c = b; + b = a % b; + a = c; + } + return a; +} + +int main() +{ + int t = 1; + char v_s[10]; + while (scanf("%s", v_s) == 1) + { + double v = strtod(v_s, NULL); + if (fabs(v - (-1.0)) < EPS) + { + break; + } + + // int n = strlen(v_s) - (strstr(v_s, ".") - v_s + 1); + // int cnt = 1; + // double vd = abs(v - round(v)); + // double i = round(cnt / vd); + // while (abs((i * v) - round(i * v)) > i * 0.5 * pow(10,-n)) + // { + // cnt++; + // i = round(cnt / vd); + // } + + int n = 0, base = 1; + for (int i = strlen(v_s) - 1; i >= 0; i--) + { + if ('0' <= v_s[i] && v_s[i] <= '9') + { + n += (v_s[i] - '0') * base; + base *= 10; + } + } + base /= 10; + + vector coeffs; + while (1)// n!=1 && base!=1) + { + int gcd_ = gcd(n, base); + n /= gcd_; + base /= gcd_; + + fprintf(stderr, "%d ; %d/%d\n", n / base, n % base, base); + coeffs.push_back(n / base); + n %= base; + + if (n == 1) + { + coeffs.push_back(base); + break; + } + + swap(n, base); + } + + fprintf(stderr, "\ncoeffs[]: "); + for (int i = 0; i < coeffs.size(); i++) + { + fprintf(stderr, "%d ", coeffs[i]); + } + fprintf(stderr, "\n\n"); + + int lo = 0, hi = coeffs.size() - 1, ans = -1; + while (lo < hi) + { + int mid = lo + (hi - lo) / 2; + + int frac[2] = {1, coeffs[mid]}; + for (int i = mid - 1; i >= 0; i--) + { + frac[0] = (coeffs[i]) * frac[1] + frac[0]; + swap(frac[0], frac[1]); + } + swap(frac[0], frac[1]); + + char f_s[n + 2]; + sprintf(f_s, "%.*lf", strlen(v_s) - 2, frac[0] / (double)frac[1]); + + fprintf(stderr, "%d %d/%d = %lf %s %s\n", mid, frac[0], frac[1], frac[0] / (double)frac[1], v_s, f_s); + + if (strcmp(f_s, v_s) == 0) + { + hi = mid; + ans = frac[1]; + } + else + { + lo = mid + 1; + } + } + fprintf(stderr, "lo=%d; hi=%d; coeffs.size()-1=%d\n", lo, hi, coeffs.size() - 1); + + printf("Case %d: %d\n", t++, ans); + } + return 0; +} diff --git a/12560.cpp b/12560.cpp new file mode 100644 index 0000000..e91a362 --- /dev/null +++ b/12560.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +int n, m, group[10000]; +vector ans; + +bool check(int k, int S, int &k2) +{ + k2 = k; + int cnt = 0; + rep(i, k) + { + if ((S & group[i]) == S) + { + cnt++; + } + else + { + swap(group[i], group[k - 1]); + i--; + k--; + k2--; + } + if (5 * cnt >= m) + { + return true; + } + if (5 * (cnt + (k - i - 1)) < m) + { + return false; + } + } + return false; +} + +void dfs(int i, int k, int S) +{ + if (i == n) + { + ans.push_back(S); + return; + } + int k2; + if (check(k, S | 1 << i, k2)) + { + dfs(i + 1, k2, S | 1 << i); + } + dfs(i + 1, k, S); +} + +void solve() +{ + scanf("%d%d", &n, &m); + rep(i, m) + { + group[i] = 0; + int k; + scanf("%d", &k); + rep(j, k) + { + int a; + scanf("%d", &a); + a--; + group[i] |= 1 << a; + } + } + ans.clear(); + dfs(0, m, 0); + rep(i, ans.size()) + { + bool covered = false; + rep(j, ans.size()) if (i != j && (ans[i] & ans[j]) == ans[i]) + { + covered = true; + break; + } + if (covered) + { + ans.erase(ans.begin() + i), i--; + } + } + vector> ans2; + rep(i, ans.size()) + { + vector A; + rep(j, n) if (ans[i] >> j & 1) + A.push_back(j); + ans2.push_back(A); + } + sort(ans2.begin(), ans2.end()); + printf("%d\n", ans2.size()); + rep(i, ans2.size()) rep(j, ans2[i].size()) printf("%d%c", ans2[i][j] + 1, j + 1 < ans2[i].size() ? ' ' : '\n'); + puts(""); +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + printf("Case %d: ", cas), solve(); + return 0; +} diff --git a/12561.cpp b/12561.cpp new file mode 100644 index 0000000..9a0b8d2 --- /dev/null +++ b/12561.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const int N_MAX = 2; + +template +void mul(int n, const T A[N_MAX][N_MAX], const T x[N_MAX], T y[N_MAX]) +{ + static T z[N_MAX]; + rep(i, n) + { + z[i] = 0; + rep(j, n) z[i] += A[i][j] * x[j]; + } + rep(i, n) y[i] = z[i]; +} + +template +void mul(int n, const T A[N_MAX][N_MAX], const T B[N_MAX][N_MAX], T C[N_MAX][N_MAX]) +{ + static T tmp[N_MAX][N_MAX]; + rep(i, n) rep(j, n) + { + tmp[i][j] = 0; + rep(k, n) tmp[i][j] += A[i][k] * B[k][j]; + } + rep(i, n) rep(j, n) C[i][j] = tmp[i][j]; +} + +template +void pow(int n, const T A[N_MAX][N_MAX], int m, T B[N_MAX][N_MAX]) +{ + static T tmp[N_MAX][N_MAX]; + rep(i, n) rep(j, n) + { + tmp[i][j] = A[i][j]; + B[i][j] = (i == j ? 1 : 0); + } + for (int t = 0; m > 0; t++) + { + if (m & (1LL << t)) + { + mul(n, B, tmp, B); + m -= 1LL << t; + } + mul(n, tmp, tmp, tmp); + } +} + +double solve() +{ + double a0, a1, b0, b1; + int n; + scanf("%lf%lf%lf%lf%d", &a0, &a1, &b0, &b1, &n); + complex a(a0, a1), b(b0, b1); + complex A[2][2] = {{1 / sqrt(2), 1 / sqrt(2)}, {1 / sqrt(2), -1 / sqrt(2)}}, v[2] = {a, b}; + pow(2, A, n, A); + mul(2, A, v, v); + return norm(v[0]); +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + { + printf("Case %d: %.6f\n", cas, solve()); + } + return 0; +} diff --git a/12562.cpp b/12562.cpp new file mode 100644 index 0000000..7622520 --- /dev/null +++ b/12562.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +//int __builtin_popcount (unsigned int x) +//# 1-bits in x +inline int popcount(int x) +{ + x = ((x >> 1) & 0x55555555) + (x & 0x55555555); + x = ((x >> 2) & 0x33333333) + (x & 0x33333333); + x = ((x >> 4) + x) & 0x0f0f0f0f; + x += (x >> 8); + x += (x >> 16); + return x & 0x3f; +} + +int expr(const char *, int &, int); + +int number(const char *s, int &idx) +{ + int n = 0; + while ('0' <= s[idx] && s[idx] <= '9') + { + n = n * 10 + (s[idx++] - '0'); + } + return n; +} + +int primary(const char *s, int &idx, int S) +{ + if (s[idx] == '(') + { + idx++; + int a = expr(s, idx, S); + idx++; + return a; + } + else + { + idx++; + int n = number(s, idx); + return S >> (n - 1) & 1; + } +} + +int factor(const char *s, int &idx, int S) +{ + if (s[idx] == '!') + { + idx++; + return !primary(s, idx, S); + } + else + { + return primary(s, idx, S); + } +} + +int term(const char *s, int &idx, int S) +{ + int a = factor(s, idx, S); + while (s[idx] == '&') + { + idx += 2; + a &= factor(s, idx, S); + } + return a; +} + +int expr(const char *s, int &idx, int S) +{ + int a = term(s, idx, S); + while (s[idx] == '|') + { + idx += 2; + a |= term(s, idx, S); + } + return a; +} + +int equation(const char *s, int S) +{ + int idx; + for (idx = 0; s[idx] != '='; idx++) + ; + idx++; + return expr(s, idx, S); +} + +int x, y, z; +char b[15][512], c[15][512]; + +int b_ini[15]; + +int parse(int a_S) +{ + int b_res[15]; + rep(i, y) + b_res[i] = equation(b[i], a_S); + rep(i, y) if (b[i][0] == '*' && b_res[i] != b_ini[i]) + { + return 0; + } + int b_S = 0; + rep(i, y) b_S |= b_res[i] << i; + int c_res[15]; + rep(i, z) c_res[i] = equation(c[i], b_S); + int c_S = 0; + rep(i, z) c_S |= c_res[i] << i; + return c_S; +} + +void solve() +{ + scanf("%d%d%d", &x, &y, &z); + int a[15]; + rep(i, x) scanf("%d", a + i); + rep(i, y) scanf("%s", b[i]); + rep(i, z) scanf("%s", c[i]); + int S_ini = 0; + rep(i, x) S_ini |= a[i] << i; + rep(i, y) b_ini[i] = equation(b[i], S_ini); + int ini = parse(S_ini); + int ans = ini, S_ans; + rep(S, 1 << x) + { + int tmp = parse(S_ini ^ S); + if (popcount(tmp) > popcount(ans) || popcount(tmp) == popcount(ans) && popcount(S) < popcount(S_ans)) + { + ans = tmp, S_ans = S; + } + } + if (ans == ini) + puts("Unable to improve future.\n"); + else + { + printf("Increased from %d to %d.\n", popcount(ini), popcount(ans)); + int sz = popcount(S_ans); + rep(i, x) if (S_ans >> i & 1) + { + printf("a%d%c", i + 1, sz > 1 ? ' ' : '\n'), sz--; + } + puts(""); + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + printf("Case %d: ", cas), solve(); + } + return 0; +} diff --git a/12563.cpp b/12563.cpp new file mode 100644 index 0000000..397e867 --- /dev/null +++ b/12563.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +const int INF = 1 << 29; + +void solve() +{ + int n, T; + scanf("%d%d", &n, &T); + int a[51]; + rep(i, n) scanf("%d", a + i); + a[n++] = 678; + sort(a, a + n); + int last = a[--n]; + static int dp[51][10000]; + rep(i, n + 1) + rep(j, T + 1) + dp[i][j] = (i == 0 && j == 0 ? 0 : -INF); + + rep(i, n) for (int j = T; j >= 0; j--) + { + if (j + a[i] <= T) + dp[i + 1][j + a[i]] = max(dp[i + 1][j + a[i]], dp[i][j] + 1); + dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); + } + + int ans1 = 0; + rep(i, n + 1) + rep(j, T) + ans1 = max(ans1, dp[i][j]); + + int ans2 = 0; + rep(i, n + 1) + rep(j, T) if (dp[i][j] == ans1) + ans2 = max(ans2, j); + + ans1++; + ans2 += last; + printf("%d %d\n", ans1, ans2); +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + printf("Case %d: ", cas), solve(); + return 0; +} diff --git a/12564.cpp b/12564.cpp new file mode 100644 index 0000000..d793e8d --- /dev/null +++ b/12564.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +bool overlap(int t1, int l1, int b1, int r1, int t2, int l2, int b2, int r2) +{ + return t1 < b2 && t2 < b1 && l1 < r2 && l2 < r1; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; cas++) + { + int n[4], t[4][5], l[4][5], b[4][5], r[4][5]; + scanf("%d%d", n + 0, n + 1); + rep(i, n[0]) scanf("%d%d%d%d", l[0] + i, t[0] + i, r[0] + i, b[0] + i); + rep(i, n[1]) scanf("%d%d%d%d", l[1] + i, t[1] + i, r[1] + i, b[1] + i); + scanf("%d%d", n + 2, n + 3); + rep(i, n[2]) scanf("%d%d%d%d", l[2] + i, t[2] + i, r[2] + i, b[2] + i); + rep(i, n[3]) scanf("%d%d%d%d", l[3] + i, t[3] + i, r[3] + i, b[3] + i); + + bool ans1 = false, ans2 = false; + rep(i, n[0]) rep(j, n[3]) if (overlap(t[0][i], l[0][i], b[0][i], r[0][i], t[3][j], l[3][j], b[3][j], r[3][j])) + { + ans1 = true; + } + + rep(i, n[2]) rep(j, n[1]) if (overlap(t[2][i], l[2][i], b[2][i], r[2][i], t[1][j], l[1][j], b[1][j], r[1][j])) + { + ans2 = true; + } + printf("Case %d: %s\n", cas, ans1 && ans2 ? "Both" : ans2 ? "First" : ans1 ? "Second" : "Neither"); + } + return 0; +} diff --git a/12569.cpp b/12569.cpp new file mode 100644 index 0000000..a16e1e8 --- /dev/null +++ b/12569.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define rep(i, n) for (int i = 0; i < (n); i++) + +int n; + +pair encode(int u1, int S1, int u2, int S2) +{ + if (u1 != u2) + return make_pair(u1, u2); + + rep(u, n) if ((S1 >> u & 1) && !(S2 >> u & 1)) + rep(v, n) if (!(S1 >> v & 1) && (S2 >> v & 1)) return make_pair(u, v); +} + +void solve() +{ + int m, s, t; + scanf("%d%d%d%d", &n, &m, &s, &t); + s--; + t--; + int S0 = 0; + rep(i, m) + { + int u; + scanf("%d", &u); + u--; + S0 |= 1 << u; + } + vector T[15]; + rep(i, n - 1) + { + int u, v; + scanf("%d%d", &u, &v); + u--; + v--; + T[u].push_back(v); + T[v].push_back(u); + } + static int dp[15][1 << 15]; + static pair path[15][1 << 15]; + memset(dp, -1, sizeof dp); + dp[s][S0] = 0; + int head = 0, tail = 0; + static pair Q[15 * (1 << 15)]; + Q[tail++] = make_pair(s, S0); + int u_ans = -1, S_ans; + while (head < tail) + { + int u = Q[head].first, S = Q[head].second; + head++; + if (u == t) + { + u_ans = u; + S_ans = S; + break; + } + rep(i, T[u].size()) + { + int v = T[u][i]; + if ((S >> v & 1) == 0 && dp[v][S] == -1) + { + Q[tail++] = make_pair(v, S); + dp[v][S] = dp[u][S] + 1; + path[v][S] = make_pair(u, S); + } + } + rep(v, n) if (S >> v & 1) + { + rep(i, T[v].size()) + { + int w = T[v][i], S2 = S & ~(1 << v) | (1 << w); + if ((S >> w & 1) == 0 && w != u && dp[u][S2] == -1) + { + Q[tail++] = make_pair(u, S2); + dp[u][S2] = dp[u][S] + 1; + path[u][S2] = make_pair(u, S); + } + } + } + } + if (u_ans == -1) + { + puts("-1\n"); + return; + } + printf("%d\n", dp[u_ans][S_ans]); + int u = u_ans, S = S_ans; + vector> p_ans; + while (u != s || S != S0) + { + pair a = path[u][S]; + p_ans.push_back(encode(a.first, a.second, u, S)); + u = a.first; + S = a.second; + } + reverse(p_ans.begin(), p_ans.end()); + rep(i, p_ans.size()) printf("%d %d\n", p_ans[i].first + 1, p_ans[i].second + 1); + puts(""); +} + +int main() +{ + int T; + scanf("%d ", &T); + for (int cas = 1; cas <= T; cas++) + printf("Case %d: ", cas), solve(); + return 0; +} diff --git a/12571.cpp b/12571.cpp new file mode 100644 index 0000000..bc77337 --- /dev/null +++ b/12571.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +static int t, n, q, i, x, a; + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &q); + int m[256] = {0}; + while (n--) + { + scanf("%d", &x); + m[x & 0xff] = 1; + } + while (q--) + { + scanf("%d", &a); + x = 0; + for (i = 255; i > x; i--) + if (m[i] && (a & i) > x) + x = a & i; + printf("%d\n", x); + } + } + return 0; +} diff --git a/12572.cpp b/12572.cpp new file mode 100644 index 0000000..305f2c5 --- /dev/null +++ b/12572.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define N 100005 +#define MOD 1000000007 +#define LL long long + +char s[N]; + +LL cut(LL n) +{ + if (n == 0 || n == 1) + { + return n; + } + else + { + return n * (n + 1) / 2; + } +} + +int main() +{ + LL n, i, j, k, sum, b, e; + while (cin >> n) + { + scanf("%s", s); + sum = 0; + LL l = strlen(s); + for (i = 1; i <= 9; i++) + { + for (j = b = e = 0; j < l; j++) + { + if (s[j] - '0' < i) + { + sum += cut(e - b); + b = e = 0; + } + else + e++; + } + sum += cut(e - b); + } + cout << (sum % MOD) << endl; + } + return 0; +} diff --git a/12573.cpp b/12573.cpp new file mode 100644 index 0000000..90e68bc --- /dev/null +++ b/12573.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int main() +{ + scanf("%*d"); + long long x, y; + int i; + while (scanf("%lld %lld", &x, &y) == 2) + { + long long n = x - (y - 2), sq; + sq = (long long)sqrt(n); + if (n == 0) + { + printf("%lld\n", x + 1); + continue; + } + int flag = 0; + long long mn; + for (i = 1; i <= sq; i++) + { + if (n % i == 0) + { + if (x % i + 2 == y) + { + if (!flag) + { + mn = i, flag = 1; + } + if (i < mn) + mn = i; + } + if (x % (n / i) + 2 == y) + { + if (!flag) + { + mn = n / i, flag = 1; + } + if (n / i < mn) + mn = n / i; + } + } + if (flag && i > mn) + break; + } + if (flag) + printf("%lld\n", mn); + else + puts("Impossible"); + } + return 0; +} diff --git a/12574.cpp b/12574.cpp new file mode 100644 index 0000000..a183738 --- /dev/null +++ b/12574.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const int M = 1e9 + 7; +int a[5000]; + +int main() +{ + int T, n, i; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + long long J = 1, S = 1; + for (i = 0; i < n; i++) + { + scanf("%d", a + i), S += a[i], J *= (a[i] + 1); + if (S >= M) + S -= M; + J %= M; + } + printf("%d\n", (J - S + M) % M); + } + return 0; +} diff --git a/12575.cpp b/12575.cpp new file mode 100644 index 0000000..9251659 --- /dev/null +++ b/12575.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +#define eps 1e-2 + +int main() +{ + int t, i; + scanf("%d", &t); + double A, B, pi = acos(-1); + while (t--) + { + scanf("%lf %lf", &A, &B); + if (A == 0 && B == 0) + { + puts("0.00 0.00"); + continue; + } + double tmp1, tmp2; + double F = sqrt(A * A + B * B); + tmp1 = atan(A / B); + for (; tmp1 <= 2 * pi; tmp1 += pi / 2) + { + if (fabs(A * sin(tmp1) + B * cos(tmp1) - F) < eps && tmp1 >= 0) + break; + } + printf("%.2lf %.2lf\n", tmp1, F); + } + return 0; +} diff --git a/12576.cpp b/12576.cpp new file mode 100644 index 0000000..616e089 --- /dev/null +++ b/12576.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define i64 long long + +const int mod = 7477777; +const int MAX = 100005; +i64 f2[MAX], f3[MAX], p2[MAX], p3[MAX]; + +i64 cal2(i64 x) +{ + return x * (x + 1) * (x + x + 1) / 6 % mod; +} + +i64 cal3(i64 x) +{ + return x * (x + 1) / 2 % mod * (x * (x + 1) / 2) % mod; +} + +void init() +{ + i64 i, temp = 0; + for (i = 2; i < MAX; i++) + { + f2[i] = cal2(i - 1) * 2 % mod; + p2[i] = i - 1; + } + for (i = 3; i < MAX; i++) + { + temp += f2[i - 1]; + f3[i] = i * cal2(i - 2) % mod - cal3(i - 2) - cal2(i - 2) + temp; + f3[i] = (f3[i] % mod + mod) % mod; + p3[i] = (i - 1) * (i - 2) / 2; + } +} + +int C, num = 0, n; + +int main() +{ + init(); + for (scanf("%d", &C); C--;) + { + scanf("%d", &n); + printf("Case %d: ", ++num); + if (n < 5) + { + puts("0"); + continue; + } + i64 ans = 0, i; + for (i = 3; i <= n - 2; i++) + { + ans += p3[i] * f2[n - i] + p2[n - i] * f3[i]; + ans %= mod; + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12577.cpp b/12577.cpp new file mode 100644 index 0000000..e500e67 --- /dev/null +++ b/12577.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; +} +const int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1}; +const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +int main() +{ + char input[50]; + int cases = 0; + while (scanf("%s", input) && strcmp(input, "*")) + { + if (strcmp(input, "Hajj") == 0) + printf("Case %d: Hajj-e-Akbar\n", ++cases); + else + printf("Case %d: Hajj-e-Asghar\n", ++cases); + } + return 0; +} diff --git a/12578.cpp b/12578.cpp new file mode 100644 index 0000000..ced7ee1 --- /dev/null +++ b/12578.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +#define loop(i, n) for (int i = 0; i < n; i++) +#define loopfrom1(i, n) for (int i = 1; i < n; i++) +#define mem(array, value) memset(array, value, sizeof(array)) +#define MIN(a, b) (a < b ? a : b) +#define MAX(a, b) (a > b ? a : b) +#define pb(a) push_back(a) +#define SZ size() +#define getint(n) scanf("%d", &n) +#define pi acos(-1.0) +#define inf 536870912// 1<<29 +#define debug cout << "ok" << endl +#define ll long long int +#define mod(a) (a > 0 ? a : -a) + +int main() +{ + int kases; + getint(kases); + double length, width, r, areaOfRectangle, areaOfCircle; + while (kases--) + { + cin >> length; + width = length * (6.0 / 10.0); + areaOfRectangle = length * width; + r = length / 5.0; + areaOfCircle = pi * r * r; + printf("%.2lf %.2lf\n", areaOfCircle, areaOfRectangle - areaOfCircle); + } + return 0; +} diff --git a/1258.cpp b/1258.cpp new file mode 100644 index 0000000..f014941 --- /dev/null +++ b/1258.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +long long dp[91]; + +int main() +{ + int i, j; + long long N; + dp[1] = 1; + dp[2] = 2; + for (i = 3; i <= 90; i++) + dp[i] = dp[i - 1] + dp[i - 2]; + while (scanf(" %lld", &N) == 1) + { + printf("%lld\n", N); + vector v; + for (i = 90; i > 0; i--) + { + if (N >= dp[i]) + { + N -= dp[i]; + v.push_back(i); + } + } + for (i = 0; i < v.size(); i++) + printf("%lld ", v[i]); + printf("\n"); + for (i = 0; i < v.size(); i++) + printf("%lld ", dp[v[i]]); + printf("\n\n"); + } + + return 0; +} diff --git a/12580.cpp b/12580.cpp new file mode 100644 index 0000000..ad06539 --- /dev/null +++ b/12580.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +const int MM = 111111; + +char ch[MM]; +int N, num[MM], sum[MM], ans[22], cnt, d[11111][10], g = 1; + +void printcase() +{ + printf("Case %d:", g++); +} + +void checkmin(int &x, int y) +{ + if (x == -1 || x > y) + x = y; +} + +void get_data() +{ + int i, j, k; + scanf("%d", &N); + sum[N] = -1; + for (i = 0; i < N; i++) + { + scanf("%d", &num[i]); + sum[N - i - 1] = sum[N - i] + 1; + } +} + +void solve() +{ + int i, j, k; + printcase(); + if (N < 11) + puts(" go home!"); + else + { + int pos = -1, mx = -1, id = 0; + cnt = 0; + while (cnt < 11) + { + mx = -1; + id = pos + 1; + while (id < N && sum[id] >= (10 - cnt)) + { + if (mx == -1 || mx < num[id]) + { + mx = num[id]; + pos = id; + } + id++; + } + ans[cnt++] = mx; + } + for (i = 0; i < cnt; i++) + printf(" %d", ans[i]); + printf("\n"); + } +} + +int main() +{ + int ca; + scanf("%d", &ca); + while (ca--) + { + get_data(), solve(); + } + return 0; +} diff --git a/12581.cpp b/12581.cpp new file mode 100644 index 0000000..5084111 --- /dev/null +++ b/12581.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +const int maxn = 8; +const int maxp = 20; +const int maxd = 70; +const int MOD = 1000000009; + +int n, P; +long long lo[maxn], hi[maxn]; +int bound[maxn][maxd]; +int flag[maxd][maxn][maxp][1 << maxn], flags; +int memo[maxd][maxn][maxp][1 << maxn]; +int digits; + +int go(int col, int row, int sum, int ls) +{ + if (row == n) + { + row = 0; + sum = 0; + if (++col == digits) + { + return 1; + } + } + int &ret = memo[col][row][sum][ls]; + if (flag[col][row][sum][ls] == flags) + { + return ret; + } + flag[col][row][sum][ls] = flags; + ret = 0; + for (int i = 0; sum + i < P; i++) + { + if (ls >> row & 1 || i <= bound[row][col]) + { + ret += go(col, row + 1, sum + i, i < bound[row][col] ? ls | 1 << row : ls); + if (ret >= MOD) + { + ret -= MOD; + } + } + } + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d", &n, &P); + for (int i = 0; i < n; i++) + cin >> lo[i]; + long long maxv = 0; + for (int i = 0; i < n; i++) + { + cin >> hi[i]; + if (lo[i] > hi[i]) + swap(lo[i], hi[i]); + if (maxv < hi[i]) + maxv = hi[i]; + } + digits = 2; + for (long long tmp = maxv; tmp; tmp /= P) + digits++; + + int ans = 0; + for (int mask = 0; mask < 1 << n; ++mask) + { + int sign = 1; + for (int i = 0; i < n; i++) + { + long long b = mask >> i & 1 ? lo[i] - 1 : hi[i]; + if (b < 0) + { + sign = 0; + break; + } + else if (mask >> i & 1) + sign *= -1; + for (int j = digits - 1; j >= 0; j--) + { + bound[i][j] = b % P; + b /= P; + } + } + if (sign != 0) + { + flags++; + ans = (ans + sign * go(0, 0, 0, 0)) % MOD; + } + } + ans = (ans + MOD) % MOD; + printf("Case %d: %d\n", cas, ans); + } + return 0; +} diff --git a/12582.cpp b/12582.cpp new file mode 100644 index 0000000..2e1b712 --- /dev/null +++ b/12582.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +#define ll long long +#define sc scanf +#define pf printf +#define Pi 2 * acos(0.0) + +static map mp; +static stack stk; +static int t, no = 0; +static string s; + +int main() +{ + cin >> t; + while (t--) + { + cin >> s; + stk.push(s[0]); + for (int i = 1; i < s.size(); i++) + { + if (stk.top() == s[i]) + stk.pop(); + else + { + mp[stk.top()]++; + stk.push(s[i]); + mp[stk.top()]++; + } + } + sort(s.begin(), s.end()); + s.erase(unique(s.begin(), s.end()), s.end()); + pf("Case %d\n", ++no); + for (int i = 0; i < s.size(); i++) + pf("%c = %d\n", s[i], mp[s[i]]); + mp.clear(); + } + return 0; +} diff --git a/12583.cpp b/12583.cpp new file mode 100644 index 0000000..f2fb4fe --- /dev/null +++ b/12583.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define Inf 2147483647 +#define Pi acos(-1.0) +#define N 1000000 +#define LL long long + +inline LL Power(int b, int p) +{ + LL ret = 1; + for (int i = 1; i <= p; i++) + { + ret *= b; + } + return ret; +} + +#define F(i, a, b) for (int i = (a); i < (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define Set(a, s) memset(a, s, sizeof(a)) +#define max(a, b) (a < b ? b : a) +#define min(a, b) (a > b ? b : a) + +int main() +{ + int testCases; + scanf("%d", &testCases); + int cases = 0; + while (testCases--) + { + int n, k; + scanf("%d %d", &n, &k); + char str[1000]; + scanf("%s", str); + int vis[26 + 5]; + Set(vis, 0); + int ret = 0; + for (int i = 0; i < n; i++) + { + if (vis[str[i] - 'A']) + { + ret++; + } + vis[str[i] - 'A']++; + if (i - k >= 0) + { + vis[str[i - k] - 'A']--; + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/12584.cpp b/12584.cpp new file mode 100644 index 0000000..68320e7 --- /dev/null +++ b/12584.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define N 110 + +struct node +{ + double C, T, R; + void in() + { + scanf("%lf%lf%lf", &C, &T, &R); + } +} in[N]; +int n; +double rat; +bool judge(double mid, int cnt) +{ + double cost = 0, get = mid * rat * cnt; + for (int i = 0; i < n; i++) + { + if (in[i].C * mid <= in[i].T * in[i].R) + { + continue; + } + get += in[i].R; + cost += in[i].C / in[i].T * mid; + } + return get <= cost; +} +double gao(int m) +{ + double l = 0, r = 100010, best = -1; + while (r - l > 1e-4) + { + double mid = (r + l) * 0.5; + if (judge(mid, m)) + { + r = mid; + } + else + { + best = mid; + l = mid; + } + } + return best; +} +int main() +{ + int q, m; + for (int ca = 1;; ca++) + { + scanf("%d%d", &n, &q); + if (!n && !q) + { + break; + } + scanf("%lf", &rat); + double sum = 0; + double mi = 1e20; + for (int i = 0; i < n; i++) + { + in[i].in(), sum += in[i].C / in[i].T, mi = min(mi, in[i].R * in[i].T / in[i].C); + } + printf("Case %d:\n", ca); + int j = 1; + for (j = 1; rat * j < sum; j++) + ; + printf("%d\n", j); + while (q--) + { + scanf("%d", &m); + if (!m) + { + printf("%.3lf\n", mi); + continue; + } + double ans = gao(m); + if (ans > 100000) + { + puts("-1.000"); + } + else + { + printf("%.3lf\n", ans); + } + } + } + return 0; +} diff --git a/12585.cpp b/12585.cpp new file mode 100644 index 0000000..56e327b --- /dev/null +++ b/12585.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +static double f[333][333], g[333][333]; + +void solve(int n, int m, int step) +{ + if ((n == m) || (step > 100)) + { + f[n][m] = 0.5; + g[n][m] = 1.0; + return; + } + if (n > m) + { + solve(n - m, m * 2, step + 1); + f[n][m] = 0.5 + 0.5 * f[n - m][m * 2]; + g[n][m] = 0.5 + 0.5 * (1 + g[n - m][m * 2]); + } + else + { + solve(n * 2, m - n, step + 1); + f[n][m] = 0.5 * f[n * 2][m - n]; + g[n][m] = 0.5 + 0.5 * (1 + g[n * 2][m - n]); + } +} + +int main() +{ + int n, m, T; + scanf("%d", &T); + for (int caseT = 1; caseT <= T; caseT++) + { + scanf("%d%d", &n, &m); + for (int i = 0; i <= 300; i++) + for (int j = 0; j <= 300; j++) + { + f[i][j] = -1.0; + } + memset(g, 0, sizeof(g)); + solve(n, m, 0); + printf("Case %d: %.6f %.6f\n", caseT, g[n][m], f[n][m]); + } + return 0; +} diff --git a/12586.cpp b/12586.cpp new file mode 100644 index 0000000..43a7131 --- /dev/null +++ b/12586.cpp @@ -0,0 +1,54 @@ +/* + Royal Flush , AUST +*/ +#include +#define pb push_back +#define INF 1 << 30 +#define all(x) x.begin(), x.end() +#define ms(a, v) memset(a, v, sizeof a) +#define II ({int a; scanPlacef("%d", &a); a; }) +#define LL ({ll a; scanPlacef("%lld", &a); a; }) +#define EPS 1e-10 +#define pi acos(0.0) +using namespace std; +typedef long long ll; +#define rep(i, n) for (__typeof(n) i = 0; i < n; i++) +#define For(i, n) for (__typeof(n) i = 1; i <= n; i++) +#define forstl(i, n) for (__typeof(n.begin()) i = n.begin(); i != n.end(); i++) +#define forab(i, a, b) for (__typeof(b) i = a; i <= (b); i++) +#define forba(i, b, a) for (__typeof(b) i = b; i >= (a); i--) + +int Mp[20][50]; +char grid[128][20][50]; +char Str[100]; +bool canPlace[128]; + +void draw(int pos) +{ + rep(i, 16) + rep(j, 43) if (grid[pos][i][j] == '*') + Mp[i][j] = Mp[i][j] == 0 ? pos : -1; +} + +int main() +{ + //freopen("in.txt", "r", stdin); + int n, Q, cs = 1; + scanf("%d %d", &n, &Q); + scanf("%s", Str); + rep(i, n) rep(j, 17) scanf("%s", grid[Str[i]][j]); + while (Q--) + { + scanf("%s", Str); + ms(Mp, 0); + int len = strlen(Str); + rep(i, len) draw(Str[i]); + ms(canPlace, 0); + rep(i, 16) + rep(j, 43) if (Mp[i][j] != -1) + canPlace[Mp[i][j]] = true; + printf("Query %d: ", cs++); + rep(i, len) printf("%c", canPlace[Str[i]] ? 'Y' : 'N'); + puts(""); + } +} diff --git a/12587.cpp b/12587.cpp new file mode 100644 index 0000000..b66fcfa --- /dev/null +++ b/12587.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +#define xx first +#define yy second +#define pb push_back +#define mp make_pair +#define LL long long +#define inf INT_MAX / 2 +#define mod 1000000007 +#define PI 2.0 * acos(0.0) +#define linf (1ll << 63) - 1 +#define all(v) (v.begin(), v.end()) +#define set0(ar) memset(ar, 0, sizeof ar) +#define vsort(v) sort(v.begin(), v.end()) +#define setinf(ar) memset(ar, 126, sizeof ar) + +template +inline T bigmod(T p, T e, T M) +{ + if (e == 0) + return 1; + if (e % 2 == 0) + { + LL t = bigmod(p, e / 2, M); + return (T)((t * t) % M); + } + return ((LL)bigmod(p, e - 1, M) * (LL)p) % M; +} +template +inline T gcd(T a, T b) +{ + if (b == 0) + return a; + return gcd(b, a % b); +} +template +inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } + +vector vc[20001], vn[20001]; +LL vis[20001], pre[20001]; +LL cnt, in[20001], val[20001]; +LL vv[20001], vr[20001]; +LL b, c, ct, grp[20001]; +LL dfs(LL v, LL p) +{ + vis[v] = 1; + in[v] = val[v] = cnt++; + LL a = grp[ct], e, d = 0, x, y, z; + for (x = 0; x < vc[v].size(); x++) + { + if (vc[v][x] == p) + continue; + if (vis[vc[v][x]]) + val[v] = min(val[v], in[vc[v][x]]); + else + { + e = dfs(vc[v][x], v); + d += e; + val[v] = min(val[v], val[vc[v][x]]); + if (val[vc[v][x]] > in[v]) + pre[vn[v][x]] += (a - e) * e; + } + } + return d + 1; +} +LL vis2[20001]; +LL dfs2(LL v) +{ + vis2[v] = 1; + LL ret = 0; + for (LL x = 0; x < vc[v].size(); x++) + { + if (!vis2[vc[v][x]]) + ret += dfs2(vc[v][x]); + } + return ret + 1; +} +pair pp[20001]; +LL tmp[20001]; +LL go(LL mx, LL v, LL p) +{ + vis[v] = 1; + LL a, d = mx - vv[v], e, f, x, y, z; + for (x = 0; x < vc[v].size(); x++) + { + if (vis[vc[v][x]]) + continue; + a = go(mx, vc[v][x], vn[v][x]); + if (a == 0) + return 0; + if (tmp[vn[v][x]]) + continue; + if (vr[vn[v][x]] * pre[vn[v][x]] > d) + return 0; + tmp[vn[v][x]] = 1; + d -= vr[vn[v][x]] * pre[vn[v][x]]; + } + if (p != -1 && vr[p] * pre[p] <= d) + { + tmp[p] = 1; + d -= vr[p] * pre[p]; + } + return 1; +} + +int main() +{ + LL a, d, e, f, x, y, z; + scanf("%lld", &a); + for (z = 1; z <= a; z++) + { + for (x = 0; x < 20000; x++) + { + vc[x].clear(); + vn[x].clear(); + } + scanf("%lld %lld", &b, &c); + LL lo = 0; + for (x = 0; x < b; x++) + { + scanf("%lld", &vv[x]); + lo = max(lo, vv[x]); + } + set0(val); + set0(in); + set0(vis); + set0(pre); + for (x = 0; x < c; x++) + { + scanf("%lld %lld %lld", &pp[x].xx, &pp[x].yy, &vr[x]); + pp[x].xx--; + pp[x].yy--; + vn[pp[x].xx].push_back(x); + vn[pp[x].yy].push_back(x); + vc[pp[x].xx].push_back(pp[x].yy); + vc[pp[x].yy].push_back(pp[x].xx); + } + cnt = ct = 0; + set0(vis2); + set0(grp); + for (x = 0; x < b; x++) + if (!vis2[x]) + grp[cnt++] = dfs2(x); + for (x = 0; x < b; x++) + { + if (!vis[x]) + { + cnt = 0; + dfs(x, -1); + ct++; + } + } + LL hi = 1ll << 61, mid = (lo + hi) / 2; + while (lo < mid) + { + f = 1; + set0(vis); + set0(tmp); + for (x = 0; x < b; x++) + { + LL g = 1; + if (!vis[x]) + g = go(mid, x, -1); + if (g != 1) + f = 0; + } + if (f == 1) + hi = mid; + else + lo = mid + 1; + mid = (lo + hi) / 2; + } + set0(vis); + set0(tmp); + f = 1; + for (x = 0; x < b; x++) + { + LL g = 1; + if (!vis[x]) + g = go(mid, x, -1); + if (g != 1) + f = 0; + } + if (f == 0) + mid++; + printf("Case %lld: %lld\n", z, mid); + } + return 0; +} diff --git a/12588.cpp b/12588.cpp new file mode 100644 index 0000000..fa3fe08 --- /dev/null +++ b/12588.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +int cases, cas, n, m, k, i, j, jz, l, x, r[31], quick[31]; +double ans, p[11][110], dp[30000][2]; + +double work(int i, int j) +{ + int sum, minx, maxx, ii, u; + double value; + int b[11]; + if (dp[i][j] > -100000) + { + return dp[i][j]; + } + sum = 0; + minx = 100; + maxx = 0; + ii = i; + for (u = 1; u <= n; u++) + { + b[u] = ii % jz; + ii = ii / jz; + sum = sum + b[u]; + if (b[u] < minx) + { + minx = b[u]; + } + if (b[u] > maxx) + { + maxx = b[u]; + } + } + if (sum > m) + { + dp[i][j] = -50000; + return dp[i][j]; + } + if (sum == m) + { + if (maxx - minx > 1) + { + dp[i][j] = -50000; + } + else + { + dp[i][j] = 0; + } + return dp[i][j]; + } + dp[i][j] = -50000; + if (r[sum + 1] == 1 && j == 0) + { + for (u = 1; u <= n; u++) + if (b[u] < jz - 1) + { + value = work(i + quick[u], 0); + if (value > dp[i][j]) + { + dp[i][j] = value; + } + } + } + else + { + for (u = 1; u <= n; u++) + if (b[u] < jz - 1) + { + value = p[u][sum + 1] * (work(i + quick[u], 1) + 1) + (1 - p[u][sum + 1]) * work(i + quick[u], 0); + if (value > dp[i][j]) + { + dp[i][j] = value; + } + } + } + return dp[i][j]; +} + +int main() +{ + scanf("%d", &cases); + for (cas = 1; cas <= cases; cas++) + { + scanf("%d%d", &n, &m); + scanf("%d", &k); + for (i = 1; i <= m; i++) + { + r[i] = 0; + } + for (i = 1; i <= k; i++) + { + scanf("%d", &x); + r[x] = 1; + } + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + { + scanf("%lf", &p[i][j]); + } + jz = (m - 1) / n + 2; + quick[1] = 1; + for (i = 2; i <= n; i++) + { + quick[i] = quick[i - 1] * jz; + } + l = 0; + for (i = 1; i <= n; i++) + { + l = l * jz + jz - 1; + } + for (i = 0; i <= l; i++) + { + dp[i][0] = -200000; + dp[i][1] = -200000; + } + ans = work(0, 1); + printf("Case %d: %.4f\n", cas, ans); + } + return 0; +} diff --git a/12589.cpp b/12589.cpp new file mode 100644 index 0000000..3aa828c --- /dev/null +++ b/12589.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +struct vec +{ + int x, y; +}; + +bool cmp(vec a, vec b) +{ + return b.x * a.y > b.y * a.x; +} + +int main() +{ + int t, cases = 0, n, m; + int i, j, k; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &n, &m); + vec V[55]; + int sumh = 0; + for (i = 0; i < n; i++) + { + scanf("%d %d", &V[i].x, &V[i].y); + sumh += V[i].y; + } + sort(V, V + n, cmp); + int dp[m + 1][sumh + 1], dph[m + 1]; + memset(dp, 0, sizeof(dp)); + memset(dph, 0, sizeof(dph)); + dp[0][0] = 1; + int ans = 0; + for (i = 0; i < n; i++) + { + for (j = min(m - 1, i); j >= 0; j--) + { + for (k = min(dph[j], sumh - V[i].y); k >= 0; k--) + { + if (dp[j][k]) + { + dp[j + 1][k + V[i].y] = max( + dp[j + 1][k + V[i].y], + dp[j][k] + (2 * k + V[i].y) * V[i].x); + dph[j + 1] = max(dph[j + 1], k + V[i].y); + } + } + } + } + for (i = 0; i <= sumh; i++) + ans = max(ans, dp[m][i]); + printf("Case %d: %d\n", ++cases, ans - 1); + } + return 0; +} diff --git a/1259.cpp b/1259.cpp new file mode 100644 index 0000000..a17f278 --- /dev/null +++ b/1259.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +const int MAXN = 500, MAXM = 5000, INF = 1000000100; + +struct Edge +{ + Edge(){}; + Edge(int a, int b, int c, int d) + { + v = a; + f = b; + w = c; + nxt = d; + } + int v, f, w, nxt; +}; +Edge e[MAXM + 10]; + +queue que; +bool inQue[MAXN + 10]; +int g[MAXN + 10], a[101][101], dist[MAXN + 10], prev_[MAXN + 10], pree[MAXN + 10], nume, src, sink, maxflow, n, m; + +void addedge(int u, int v, int c, int w) +{ + e[++nume] = Edge(v, c, w, g[u]); + g[u] = nume; + e[++nume] = Edge(u, 0, -w, g[v]); + g[v] = nume; +} + +void init() +{ + nume = 1; + memset(g, 0, sizeof(g)); +} + +bool findPath() +{ + while (!que.empty()) + { + que.pop(); + } + que.push(src); + for (int i = 0; i <= sink; i++) + { + dist[i] = INF; + } + dist[src] = 0; + inQue[src] = 1; + while (!que.empty()) + { + int u = que.front(); + que.pop(); + for (int i = g[u]; i; i = e[i].nxt) + { + if (e[i].f > 0 && dist[u] + e[i].w < dist[e[i].v]) + { + dist[e[i].v] = dist[u] + e[i].w; + prev_[e[i].v] = u; + pree[e[i].v] = i; + if (!inQue[e[i].v]) + { + inQue[e[i].v] = true; + que.push(e[i].v); + } + } + } + inQue[u] = false; + } + return dist[sink] < INF; +} + +int augment() +{ + int u = sink; + int delta = INF; + while (u != src) + { + if (e[pree[u]].f < delta) + { + delta = e[pree[u]].f; + } + u = prev_[u]; + } + u = sink; + while (u != src) + { + e[pree[u]].f -= delta; + e[pree[u] ^ 1].f += delta; + u = prev_[u]; + } + maxflow += delta; + return dist[sink] * delta; +} +int mincostflow() +{ + int cur = 0, ans = 0; + while (findPath()) + { + cur += augment(); + } + return cur; +} + +int main() +{ + int test; + scanf("%d", &test); + for (int ii = 1; ii <= test; ii++) + { + scanf("%d%d", &n, &m); + src = n + 1; + sink = n + 2; + init(); + int ans = 0, tsum = 0, tans = 0; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + a[i][j] = INF; + } + } + for (int i = 1; i <= m; i++) + { + int u, v, p, s, x; + scanf("%d%d%d%d%d", &u, &v, &p, &s, &x); + a[u][v] = p - s; + tans += s; + if (x == 1) + { + ans += p; + tsum++; + addedge(src, v, 1, 0); + addedge(u, sink, 1, 0); + continue; + } + if (p >= s) + { + ans += s; + addedge(u, v, 1, p - s); + continue; + } + ans += p; + tsum++; + addedge(v, u, 1, s - p); + addedge(src, v, 1, 0); + addedge(u, sink, 1, 0); + } + maxflow = 0; + ans += mincostflow(); + printf("Case %d: ", ii); + if (maxflow != tsum) + { + printf("impossible\n"); + continue; + } + if (tans != ans) + { + printf("%d\n", ans); + continue; + } + for (int k = 1; k <= n; k++) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (a[i][j] > a[i][k] + a[k][j]) + { + a[i][j] = a[i][k] + a[k][j]; + } + } + } + } + ans = INF; + for (int i = 1; i <= n; i++) + { + ans = min(ans, a[i][i]); + } + if (ans == INF) + { + printf("impossible\n"); + } + else + { + printf("%d\n", ans + tans); + } + } + return 0; +} diff --git a/12590.cpp b/12590.cpp new file mode 100644 index 0000000..edfe7c6 --- /dev/null +++ b/12590.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +const int Mod = 1e9 + 7; + +int c[10001][401], f[101][101][101]; + +inline int add(int a, int b) +{ + a = a + b; + if (a >= Mod) + { + a -= Mod; + } + return a; +} + +inline int mul(int a, int b) +{ + return (long long)a * b % Mod; +} + +void prepare() +{ + int i, j, k, p; + memset(c, 0, sizeof(c)); + memset(f, 0, sizeof(f)); + for (i = 0; i <= 10000; ++i) + { + c[i][0] = 1; + for (j = 1; j <= i && j <= 400; ++j) + { + c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; + if (c[i][j] >= Mod) + { + c[i][j] -= Mod; + } + } + } + for (i = 0; i <= 100; ++i) + { + f[i][0][0] = 1; + } + for (i = 1; i <= 100; ++i) + for (j = 1; j <= 100; ++j) + for (k = j; k <= 100; ++k) + { + f[i][j][k] = 0; + for (p = j - 1; p < k; ++p) + { + f[i][j][k] = f[i][j][k] + mul(f[i][j - 1][p], c[i][k - p]); + if (f[i][j][k] >= Mod) + { + f[i][j][k] -= Mod; + } + } + } +} + +int n, m, k, ans; +void debug() +{ + cout << ans << endl; +} + +void work() +{ + int p; + ans = 0; + if (n > m) + { + swap(n, m); + } + if (n == 1) + { + printf("%d\n", c[m][k]); + return; + } + else if (n * m < k) + { + puts("0"); + return; + } + else if (n == 2) + { + ans = add(add(c[2 * m][k], Mod - c[m][k]), Mod - c[m][k]); + if (m == k) + { + ans = add(ans, 2); + } + printf("%d\n", ans); + return; + } + // 4 + if (k >= 4) + { + ans = add(ans, c[n * m - 4][k - 4]); + } + if (k >= 3) + { + ans = add(ans, mul(4, c[n * m - 4][k - 3])); + } + if (k >= 2) + { + ans = add(ans, mul(2, c[n * m - 4][k - 2])); + } + if (k >= 2) + { + for (p = 1; p <= m - 2 && k - 2 - p >= 0; ++p) + { + ans = add(ans, mul(mul(2, c[m - 2][p]), c[n * m - 4 - (m - 2)][k - 2 - p])); + } + for (p = 0; p <= 2 * n - 4 && k - 2 - p >= m - 2; ++p) + { + ans = add(ans, mul(mul(2, c[n * 2 - 4][p]), f[n - 1][m - 2][k - 2 - p])); + } + for (p = 1; p <= n - 2 && k - 2 - p >= 0; ++p) + { + ans = add(ans, mul(mul(2, c[n - 2][p]), c[n * m - 4 - (n - 2)][k - 2 - p])); + } + for (p = 0; p <= 2 * m - 4 && k - 2 - p >= n - 2; ++p) + { + ans = add(ans, mul(mul(2, c[m * 2 - 4][p]), f[m - 1][n - 2][k - 2 - p])); + } + } + if (k >= 1) + { + for (p = 1; p <= 2 * m - 4 && k - 1 - p >= n - 2; ++p) + { + ans = add(ans, mul(mul(4, add(c[2 * m - 4][p], Mod - c[m - 2][p])), f[m - 1][n - 2][k - 1 - p])); + } + for (p = 1; p <= 2 * n - 4 && k - 1 - p >= m - 2; ++p) + { + ans = add(ans, mul(mul(4, add(c[2 * n - 4][p], Mod - c[n - 2][p])), f[n - 1][m - 2][k - 1 - p])); + } + for (p = 2; p <= n - 2 + m - 2 && k - p - 1 >= 0; ++p) + { + ans = add(ans, mul(mul(4, add(add(c[n - 2 + m - 2][p], Mod - c[m - 2][p]), Mod - c[n - 2][p])), c[n * m - 4 - (n - 2) - (m - 2)][k - p - 1])); + } + } + if (k >= 4) + { + for (p = 4; p <= (n - 2) * 2 + (m - 2) * 2 && k - p >= 0; ++p) + { + long long tmp = (long long)c[(n - 2) * 2 + (m - 2) * 2][p] - c[n - 2][p] * 2 - c[m - 2][p] * 2 + c[(n - 2) * 2][p] + c[(m - 2) * 2][p] + (long long)c[n - 2 + m - 2][p] * 4 - 2 * (c[(n - 2) * 2 + m - 2][p]) - 2 * (c[n - 2 + (m - 2) * 2][p]); + tmp = tmp % Mod; + if (tmp < 0) + { + tmp += Mod; + } + ans = add(ans, mul((int)tmp, c[n * m - 4 - (n - 2) * 2 - (m - 2) * 2][k - p])); + } + } + for (p = 0; k - p >= 0; ++p) + { + if (p >= 2) + { + ans = add(ans, mul(add(mul(2, f[n - 1][m - 2][k - p]), Mod - f[n - 2][m - 2][k - p]), add(add(c[(n - 2) * 2][p], Mod - c[n - 2][p]), Mod - c[n - 2][p]))); + } + if (p >= 2) + { + ans = add(ans, mul(add(mul(2, f[m - 1][n - 2][k - p]), Mod - f[m - 2][n - 2][k - p]), add(add(c[(m - 2) * 2][p], Mod - c[m - 2][p]), Mod - c[m - 2][p]))); + } + } + printf("%d\n", ans); +} + +int main() +{ + int tt, i; + prepare(); + scanf("%d", &tt); + for (i = 1; i <= tt; ++i) + { + printf("Case %d: ", i); + scanf("%d%d%d", &n, &m, &k); + work(); + } + return 0; +} diff --git a/12592.cpp b/12592.cpp new file mode 100644 index 0000000..b445b9d --- /dev/null +++ b/12592.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2 * acos(0.0) + +int main() +{ + string s[200], p[100], r; + int n, k, q; + while (cin >> n) + { + getchar(); + for (int i = 0; i < n; i++) + { + getline(cin, s[i]); + getline(cin, p[i]); + } + cin >> q; + getchar(); + while (q--) + { + getline(cin, r); + for (int i = 0; i < n; i++) + { + if (s[i] == r) + { + cout << p[i] << endl; + break; + } + } + } + } + return 0; +} diff --git a/12593.cpp b/12593.cpp new file mode 100644 index 0000000..18d7c6c --- /dev/null +++ b/12593.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int i = a; i < b; i++) +#define REP(i, a) FOR(i, 0, a) +#define EPS 1e-9 +#define inf (1LL << 30) +#define LL long long +#define ULL unsigned long long +#define abs(x) (((x) < 0) ? (-(x)) : (x)) +#define all(x) (x).begin(), (x).end() +#define ms(x, a) memset((x), (a), sizeof(x)) +#define VI vector +#define VS vector +#define VC vector +#define pii pair +#define mp make_pair +#define pb push_back +#define CI(x) scanf(" %d", &x) +#define CL(x) scanf(" %lld", &x) +#define READ(x) freopen(x, "r", stdin) +#define WRITE(x) freopen(x, "w", stdout) +#define PI 2 * acos(0.0) +#define MOD 10007 +int K; +template +class Matrix +{ +public: + int dx; + LL v[d][d]; + Matrix() + { + ms(v, 0); + } + Matrix mul(const Matrix &b) + { + Matrix r; + for (int k = 0; k < K; ++k) + { + for (int i = 0; i < K; ++i) + { + for (int j = 0; j < K; ++j) + r.v[i][j] = ((r.v[i][j] % MOD) + ((v[i][k] % MOD) * (b.v[k][j] % MOD))) % MOD; + } + } + return r; + } + Matrix pow(ULL p) + { + // cerr << p << endl; + Matrix r, b = *this; + for (int i = 0; i < d; ++i) + r.v[i][i] = 1; + for (; p; p /= 2) + { + // cerr << p << endl; + if (p % 2) + r = r.mul(b); + b = b.mul(b); + } + return r; + } +}; +int a, b, c; +LL dp[1005][105]; +LL f(int n, int k) +{ + if (n <= 0) + return 0; + if (n == 1) + return 1; + LL &res = dp[n][k]; + if (res != -1) + return res; + res = 0; + for (int i = 2; i <= k; i++) + { + if (i > n) + break; + //cerr << n << " " << i << endl; + res = (res + f(n - i, k)) % MOD; + } + //cerr << n-k-1 << endl; + return res; +} +Matrix<101> A, B; +int main() +{ + ms(dp, -1); + int t, cas = 0, k; + LL n; + scanf(" %d", &t); + while (t--) + { + scanf(" %lld %d", &n, &k); + K = k; + LL res = 0; + if (n < 200) + { + res = 0; + REP(i, k) + res = (res + f(n - i, k)) % MOD; + } + else + { + ms(A.v, 0); + ms(B.v, 0); + for (int i = 0; i < k; i++) + { + B.v[i][0] = f(k - i, k) % MOD; + } + A.v[0][0] = 0; + for (int i = 1; i < k; i++) + { + A.v[0][i] = 1; + } + for (int i = 1; i < k; i++) + { + A.v[i][i - 1] = 1; + } + A = A.pow(n - k); + A = A.mul(B); + res = 0; + REP(i, k) + { + res = (res + A.v[i][0]) % MOD; + } + } + printf("Case %d: %lld\n", ++cas, (res * 2) % MOD); + } + return 0; +} diff --git a/12594.cpp b/12594.cpp new file mode 100644 index 0000000..da062ac --- /dev/null +++ b/12594.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define ll long long + +ll x; +char ch[26], ts[20020]; +int pre[20020]; +int pos[26]; +int n, m; +int f[20020][510]; +int w(int i, int j) +{ + int ret = pre[j]; + if (i != 0) + ret -= pre[i - 1]; + return ret * i; +} +struct QQ +{ + int k, b; +} que[20020]; + +int main() +{ + int T; + scanf("%d", &T); + int cas = 1; + while (T--) + { + scanf("%s", ch); + for (int i = 0; i < 26; i++) + pos[ch[i] - 'a'] = i; + scanf("%d%s", &m, ts); + n = strlen(ts); + int ans = 0; + + for (int i = 0; i < n; i++) + { + ans += (i - pos[ts[i] - 'a']) * pos[ts[i] - 'a']; + if (i == 0) + pre[i] = pos[ts[i] - 'a']; + else + pre[i] = pre[i - 1] + pos[ts[i] - 'a']; + } + for (int j = 0; j <= m; j++) + for (int i = 0; i <= n; i++) + f[i][j] = -2000000000; + for (int i = 0; i < n; i++) + { + f[i][1] = 0; + } + for (int j = 2; j <= m; j++) + { + int st, ed; + st = ed = 1; + for (int i = j - 1; i < n; i++) + { + que[0].k = i; + que[0].b = f[i - 1][j - 1] - i * pre[i - 1]; + while (st < ed && (que[0].b - que[ed - 1].b) * (que[ed - 1].k - que[ed - 2].k) > (que[0].k - que[ed - 1].k) * (que[ed - 1].b - que[ed - 2].b)) + ed--; + que[ed].k = que[0].k; + que[ed].b = que[0].b; + ed++; + while (st < ed - 1 && que[st].k * pre[i] + que[st].b < que[st + 1].k * pre[i] + que[st + 1].b) + st++; + f[i][j] = que[st].k * pre[i] + que[st].b; + } + } + ans -= f[n - 1][m]; + printf("Case %d: %d\n", cas++, ans); + } + return 0; +} diff --git a/12595.cpp b/12595.cpp new file mode 100644 index 0000000..3d32d5f --- /dev/null +++ b/12595.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +struct Point +{ + long long x, y, z; + long long dis(Point a) + { + return (a.x - x) * (a.x - x) + (a.y - y) * (a.y - y) + (a.z - z) * (a.z - z); + } +} po[600050]; +pair dis[600050]; +long long tree[25][600050]; +long long sorted[600050]; +int toleft[25][600050]; +void build(int l, int r, int dep) +{ + if (l == r) + return; + int mid = (l + r) >> 1; + int same = mid - l + 1; + for (int i = l; i <= r; i++) + if (tree[dep][i] < sorted[mid]) + same--; + int lpos = l; + int rpos = mid + 1; + for (int i = l; i <= r; i++) + { + if (tree[dep][i] < sorted[mid]) + tree[dep + 1][lpos++] = tree[dep][i]; + else if (tree[dep][i] == sorted[mid] && same > 0) + tree[dep + 1][lpos++] = tree[dep][i], same--; + else + tree[dep + 1][rpos++] = tree[dep][i]; + toleft[dep][i] = toleft[dep][l - 1] + lpos - l; + } + build(l, mid, dep + 1); + build(mid + 1, r, dep + 1); +} +inline long long query(int L, int R, int l, int r, int dep, int k) +{ + if (l == r) + return tree[dep][l]; + int mid = (L + R) >> 1; + int cnt = toleft[dep][r] - toleft[dep][l - 1]; + if (cnt >= k) + { + int newl = L + toleft[dep][l - 1] - toleft[dep][L - 1]; + int newr = newl + cnt - 1; + return query(L, mid, newl, newr, dep + 1, k); + } + else + { + int newr = r + toleft[dep][R] - toleft[dep][r]; + int newl = newr - (r - l - cnt); + return query(mid + 1, R, newl, newr, dep + 1, k - cnt); + } +} +int p1[600050], p2[600050]; +long long sa1[600050], sa2[600050], temp[600050]; +long long sb[600050]; +int n, k1, k2; +inline int cro(long long x, long long y) +{ + int ed = upper_bound(sb + 1, sb + n + 1, x) - sb - 1; + int l = 0, r = ed - 1, mid; + while (l < r) + { + mid = (l + r + 1) >> 1; + if (query(1, n - 1, 2, ed, 0, mid) <= y) + l = mid; + else + r = mid - 1; + } + return l; +} +int main() +{ + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%lld%lld%lld", &po[i].x, &po[i].y, &po[i].z); + for (int i = 2; i < n; i++) + dis[i] = make_pair(po[0].dis(po[i]), po[1].dis(po[i])); + sort(dis + 2, dis + n); + for (int i = 2; i < n; i++) + sorted[i] = tree[0][i] = sa2[i] = dis[i].second, sa1[i] = sb[i] = dis[i].first; + sb[n] = sa1[n] = sa2[n] = (1ll << 60); + sort(sa2 + 2, sa2 + n); + sb[1] = sa2[1] = sa1[1] = 0.0; + sorted[1] = tree[0][1] = 0.0; + sort(sorted + 1, sorted + n); + build(1, n - 1, 0); + k1 = k2 = 1; + for (int i = 2; i < n; i++) + { + while (sa1[i] == sa1[i + 1]) + i++, p1[k1]++; + p1[k1] += p1[k1 - 1] + 1; + temp[k1++] = sa1[i]; + } + for (int i = 0; i < k1; i++) + sa1[i] = temp[i]; + for (int i = 2; i < n; i++) + { + while (sa2[i] == sa2[i + 1]) + i++, p2[k2]++; + p2[k2] += p2[k2 - 1] + 1; + temp[k2++] = sa2[i]; + } + for (int i = 0; i < k2; i++) + sa2[i] = temp[i]; + sa1[k1++] = sa2[k2++] = (1ll << 60); + int q, ss, bb; + scanf("%d", &q); + long long x, y; + while (q--) + { + scanf("%lld%lld", &x, &y); + x *= x; + y *= y; + ss = p1[upper_bound(sa1, sa1 + k1, x) - sa1 - 1]; + bb = p2[upper_bound(sa2, sa2 + k2, y) - sa2 - 1]; + printf("%d\n", ss + bb - cro(x, y)); + } + return 0; +} diff --git a/12596.cpp b/12596.cpp new file mode 100644 index 0000000..062bcef --- /dev/null +++ b/12596.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +long long int dp[20][30]; +char s1[200], c; +int n, m, pt[30], flag; +char s2[10][10] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"}; +void init(); +void solve(char str[100], int devel); +void deal_dp(int x, int y); + +int main() +{ + int i, j, t, tem = 1; + init(); + scanf("%d", &t); + while (t--) + { + scanf("%s %d %d", s1, &n, &m); + flag = 0; + solve(s1, n); + printf("Case %d: %c\n", tem++, c); + } + return 0; +} + +void deal_dp(int x, int y) +{ + int l, i; + if (dp[x][y] > 0) + { + return; + } + l = strlen(s2[x]); + if (y == 1) + { + dp[x][y] = (long long int)l; + return; + } + for (i = 0; i <= l - 1; i++) + { + deal_dp(pt[s2[x][i] - 'A' + 1], y - 1); + dp[x][y] += dp[pt[s2[x][i] - 'A' + 1]][y - 1]; + } +} + +void init() +{ + char str; + int i, j; + for (i = 'A'; i <= 'Z'; i++) + { + str = (char)i; + if (str >= 'A' && str <= 'C') + { + pt[str - 'A' + 1] = 2; + } + else if (str >= 'D' && str <= 'F') + { + pt[str - 'A' + 1] = 3; + } + else if (str >= 'G' && str <= 'I') + { + pt[str - 'A' + 1] = 4; + } + else if (str >= 'J' && str <= 'L') + { + pt[str - 'A' + 1] = 5; + } + else if (str >= 'M' && str <= 'O') + { + pt[str - 'A' + 1] = 6; + } + else if (str >= 'P' && str <= 'S') + { + pt[str - 'A' + 1] = 7; + } + else if (str >= 'T' && str <= 'V') + { + pt[str - 'A' + 1] = 8; + } + else if (str >= 'W' && str <= 'Z') + { + pt[str - 'A' + 1] = 9; + } + } + memset(dp, 0, sizeof(dp)); + for (i = 2; i <= 9; i++) + { + for (j = 1; j <= 21; j++) + { + deal_dp(i, j); + } + } +} + +void solve(char str[100], int devel) +{ + int i, j, l; + int Str; + if (flag) + { + return; + } + if (devel == 0) + { + c = str[m - 1]; + flag = 1; + return; + } + l = strlen(str); + for (i = 0; i <= l - 1; i++) + { + Str = str[i] - 'A' + 1; + if (dp[pt[Str]][devel] >= m) + { + solve(s2[pt[Str]], devel - 1); + } + else + { + m -= dp[pt[Str]][devel]; + } + } +} diff --git a/12597.cpp b/12597.cpp new file mode 100644 index 0000000..54977b9 --- /dev/null +++ b/12597.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define pb push_back +#define sz(x) (int)x.size() +#define scf scanf +#define ptf printf +#define fst first +#define scd second +#define forp(i, j, k) for (int i = j; i < k; i++) +#define form(i, j, k) for (int i = j; i > k; i--) + +typedef long long LL; + +unsigned long long pw[100]; + +void pr() +{ + pw[0] = 1; + forp(i, 1, 63) pw[i] = pw[i - 1] * 2; +} + +int main() +{ + pr(); + int m, e, t0; + for (scf("%d%d", &m, &e); m || e; scf("%d%d", &m, &e)) + { + unsigned long long ans = 0; + if (pw[e] - 1 < 1LL * m + 1) + t0 = pw[e] - 1; + else + t0 = m + 1; + ans += pw[t0] - 1; + if (pw[e] - 1 < 1LL * m + 1) + { + ptf("%llu\n", ans); + continue; + } + ans += pw[m] * (pw[e] - m - 2); + ptf("%llu\n", ans); + } + return 0; +} diff --git a/12598.cpp b/12598.cpp new file mode 100644 index 0000000..23e0a0d --- /dev/null +++ b/12598.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define all(a) a.begin(), a.end() +#define I1(n) scanf("%d", &n) +#define I2(n1, n2) scanf("%d%d", &n1, &n2) +#define I3(n1, n2, n3) scanf("%d%d%d", &n1, &n2, &n3) +#define F(i, a, b) for (i = (a); i <= (b); i++) +#define FR(i, a, b) for (i = (a); i <= (b); i++) +#define Fs(i, sz) for (size_t i = 0; i < sz.size(); i++) +#define Fe(it, x) for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) +#define Pr(x) \ + for (typeof(x.begin()) it = x.begin(); it != x.end(); it++) \ + cout << *it << " "; \ + cout << endl; +#define pb push_back +#define pi acos(-1.0) +#define MEM(a, val) memset(a, val, sizeof(a)) +#define eps 1e-9 +#define Max(a, b) (a > b ? a : b) +#define Min(a, b) (a < b ? a : b) +#define sz(a) ((int)(a).size()) + +#define lim 50007 + +struct node +{ + int st, ed, cnt, start, en; + node *l, *r; + node(int _, int __) + { + st = _; + ed = __; + cnt = 0; + l = r = NULL; + } +}; + +void Insert(node *ob, int idx, int s, int e) +{ + if (ob->st == ob->ed) + { + ob->cnt = e - s + 1; + ob->start = s; + ob->en = e; + return; + } + int mid = (ob->st + ob->ed) >> 1; + if (ob->l == NULL) + ob->l = new node(ob->st, mid); + if (ob->r == NULL) + ob->r = new node(mid + 1, ob->ed); + if (idx <= mid) + Insert(ob->l, idx, s, e); + else + Insert(ob->r, idx, s, e); + ob->cnt = ob->l->cnt + ob->r->cnt; + return; +} + +int ans; + +void query(node *ob, int tg) +{ + if (ob->st == ob->ed) + { + ans = ob->start + tg - 1; + return; + } + int mid = (ob->st + ob->ed) >> 1; + if (ob->l->cnt >= tg) + query(ob->l, tg); + else + query(ob->r, tg - ob->l->cnt); +} + +int main() +{ + int tc, cas = 1, n, m, i, j, k, a, b, q, cnt; + int call[lim], arr[lim]; + scanf("%d", &tc); + while (tc--) + { + printf("Case %d:\n", cas++); + scanf("%d%d%d", &n, &k, &q); + arr[0] = 0; + arr[k + 1] = n + 1; + for (i = 1; i <= k; i++) + { + scanf("%d", &arr[i]); + call[i] = arr[i]; + } + sort(arr, arr + k + 2); + cnt = 0; + node *root = new node(0, k + 2); + for (i = 1; i <= k + 1; i++) + if (arr[i] > arr[i - 1] + 1) + Insert(root, cnt++, arr[i - 1] + 1, arr[i] - 1); + for (i = 0; i < q; i++) + { + scanf("%d", &a); + if (a <= k) + ans = call[a]; + else + query(root, a - k); + printf("%d\n", ans); + } + } + return 0; +} diff --git a/12599.cpp b/12599.cpp new file mode 100644 index 0000000..327d2af --- /dev/null +++ b/12599.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define ll long long +#define sc scanf +#define pf printf +#define pi 2 * acos(0.0) + +int main() +{ + int t, n; + string s; + cin >> t; + while (t--) + { + cin >> s; + n = s.size(); + vector v; + int f = 0; + if (s[0] == 'W' || s[n - 1] == 'B') + { + cout << "IMPOSSIBLE" << endl; + continue; + } + int c = 0; + for (int i = 0; i < n - 1; i++) + { + if ((s[i] == 'B' && s[i + 1] == 'W') || (s[i] == 'W' && s[i + 1] == 'B')) + { + c++; + } + if (c > 3) + { + break; + } + } + if (c == 1) + { + cout << "1" << endl; + cout << n; + for (int i = 1; i <= n; i++) + { + cout << " " << i; + } + cout << endl; + continue; + } + cout << "2" << endl; + for (int i = 0; i < n - 1; i++) + { + if (s[i] == 'W') + { + v.push_back(i + 1); + } + } + cout << v.size() + 1; + cout << " 1"; + for (int i = 0; i < v.size(); i++) + { + cout << " " << v[i]; + } + cout << endl; + v.clear(); + for (int i = 1; i < n - 1; i++) + { + if (s[i] == 'B') + { + v.push_back(i + 1); + } + } + cout << v.size() + 1; + for (int i = 0; i < v.size(); i++) + { + cout << " " << v[i]; + } + cout << " " << n << endl; + v.clear(); + } + return 0; +} diff --git a/126.cpp b/126.cpp new file mode 100644 index 0000000..5f60df9 --- /dev/null +++ b/126.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +struct TERM +{ + int cof; + int xe; + int ye; + TERM(int c, int x, int y) : cof(c), xe(x), ye(y) {} +}; + +bool GreaterTerm(const TERM &t1, const TERM &t2) +{ + return (t1.xe > t2.xe || (t1.xe == t2.xe && t1.ye < t2.ye)); +} + +void ParsePolynomial(char *pStr, vector &Terms) +{ + for (int nNum; *pStr != 0;) + { + TERM Term(*pStr == '-' ? -1 : 1, 0, 0); + pStr += (*pStr == '-' || *pStr == '+') ? 1 : 0; + if (*pStr == '0') + { + for (++pStr; *pStr != '\0' && *pStr != '+' && *pStr != '-'; ++pStr) + ; + continue; + } + for (nNum = 0; isdigit(*pStr); nNum = nNum * 10 + *pStr++ - '0') + ; + for (Term.cof *= (nNum == 0) ? 1 : nNum; isalpha(*pStr);) + { + int *pe = (*pStr == 'x') ? &Term.xe : &Term.ye; + for (; isdigit(*++pStr); *pe = *pe * 10 + *pStr - '0') + ; + *pe = (*pe == 0) ? 1 : *pe; + } + Terms.push_back(Term); + } +} + +int main() +{ + for (string str1, str2; cin >> str1 && str1 != "#";) + { + cin >> str2; + if (str1.empty() || str2.empty()) + { + continue; + } + const int nMaxLen = 100; + char szBuf1[nMaxLen], szBuf2[nMaxLen]; + vector Poly1, Poly2, Result; + strcpy(szBuf1, str1.c_str()); + strcpy(szBuf2, str2.c_str()); + ParsePolynomial(szBuf1, Poly1); + ParsePolynomial(szBuf2, Poly2); + vector::iterator i, j; + for (i = Poly1.begin(); i != Poly1.end(); ++i) + { + for (j = Poly2.begin(); j != Poly2.end(); ++j) + { + TERM Term(i->cof * j->cof, i->xe + j->xe, i->ye + j->ye); + Result.push_back(Term); + } + } + sort(Result.begin(), Result.end(), GreaterTerm); + fill(&szBuf1[0], &szBuf1[nMaxLen], ' '); + fill(&szBuf2[0], &szBuf2[nMaxLen], ' '); + int nPos = 0; + for (i = Result.begin(); i != Result.end(); ++i) + { + for (j = i + 1; j < Result.end() && + i->xe == j->xe && i->ye == j->ye;) + { + i->cof += j->cof; + j = Result.erase(j); + } + if (i->cof != 0) + { + if (nPos > 0) + { + ++nPos; + szBuf2[nPos++] = i->cof > 0 ? '+' : '-'; + szBuf2[nPos++] = ' '; + } + else + { + szBuf2[0] = '-'; + nPos += (i->cof < 0); + } + i->cof = abs(i->cof); + if (i->cof != 1 || (i->xe == 0 && i->ye == 0)) + { + nPos += sprintf(&szBuf2[nPos], "%d", i->cof); + szBuf2[nPos] = ' '; + } + if (i->xe > 0) + { + szBuf2[nPos++] = 'x'; + if (i->xe > 1) + { + nPos += sprintf(&szBuf1[nPos], "%d", i->xe); + szBuf1[nPos] = ' '; + } + } + if (i->ye > 0) + { + szBuf2[nPos++] = 'y'; + if (i->ye > 1) + { + nPos += sprintf(&szBuf1[nPos], "%d", i->ye); + szBuf1[nPos] = ' '; + } + } + } + } + if (nPos == 0) + { + szBuf2[nPos++] = '0'; + } + szBuf1[nPos] = szBuf2[nPos] = '\0'; + cout << szBuf1 << '\n' + << szBuf2 << endl; + } + return 0; +} diff --git a/1260.cpp b/1260.cpp index 9f7ccb0..bdbd1b7 100644 --- a/1260.cpp +++ b/1260.cpp @@ -1,25 +1,34 @@ -#include +#include -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +using namespace std; - int T; - scanf("%d", &T); - while (T--) { - int N; - scanf("%d", &N); - int Arr[N]; - int Less = 0; - for (int i = 0; i < N; i++) { - scanf("%d", &Arr[i]); - int L = 0; - for (int j = 0; j < i; j++) - L += (Arr[j] <= Arr[i]); +int T, n, A[1001]; - Less += L; - } - printf("%d\n", Less); - } - return 0; +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + int ans = 0, tmp, j; + for (int i = 0; i < n; i++) + { + scanf("%d", &tmp); + for (j = i - 1; j >= 0; j--) + { + if (tmp < A[j]) + { + A[j + 1] = A[j]; + } + else + { + break; + } + } + A[j + 1] = tmp; + ans += j + 1; + } + printf("%d\n", ans); + } + return 0; } diff --git a/12600.cpp b/12600.cpp new file mode 100644 index 0000000..e12137c --- /dev/null +++ b/12600.cpp @@ -0,0 +1,284 @@ +#include + +using namespace std; + +typedef long long LL; + +LL n, K, m, anss; +LL ans[30][30], num[50][30][30], tmp[30][30], g[30][30]; +LL mul(LL a, LL b, LL c) +{ + a %= c; + LL ans = 0, num = a; + while (b) + { + if (b % 2 == 1) + { + ans = (ans + num) % c; + } + num = (num + num) % c; + b = b / 2; + } + return ans; +} +void mul(LL a[30][30], LL b[30][30], LL c[30][30]) +{ + memset(tmp, 0, sizeof(tmp)); + for (int i = 0; i < 13; i++) + for (int j = 0; j < 13; j++) + { + if (b[i][j] == 0) + { + continue; + } + for (int k = 0; k < 13; k++) + { + tmp[i][k] += mul(b[i][j], c[j][k], m); + tmp[i][k] %= m; + } + } + for (int i = 0; i < 13; i++) + for (int j = 0; j < 13; j++) + { + a[i][j] = tmp[i][j]; + } +} +LL b[13], tb[13]; +LL cal(int L) +{ + L += 2; + if (K <= 2 || L < 5) + { + return 0; + } + L -= 3; + int id = 0; + memset(b, 0, sizeof(b)); + b[0] = 1; + while (L) + { + if (L % 2 == 1) + { + for (int i = 0; i < 13; i++) + { + LL tmp = 0; + for (int j = 0; j < 13; j++) + { + tmp += mul(b[j], num[id][j][i], m); + if (tmp >= m) + { + tmp -= m; + } + } + tb[i] = tmp; + } + for (int i = 0; i < 13; i++) + { + b[i] = tb[i]; + } + } + id++; + L = L / 2; + } + LL anss = mul(K - 2, K - 1, m); + anss = mul(anss, K, m); + anss = mul(anss, b[6], m); + return anss; +} +int gs[10010], yz[10010], pr[10010], pcnt; +int flag[10010]; +void dfs(int now, int end, int euler, int tnum) +{ + if (now == end) + { + LL tmp = cal(n / tnum); + anss += mul(tmp, euler, m); + anss %= m; + return; + } + dfs(now + 1, end, euler, tnum); + LL temp = 1; + for (int i = 1; i <= gs[now]; i++) + { + temp *= yz[now]; + dfs(now + 1, end, mul(euler, temp - temp / yz[now], m), tnum * temp); + } + return; +} +void init() +{ + int p[10], q[10], ck[100], Cnt = 0; + int ok[100]; + memset(ok, 0, sizeof(ok)); + for (int i = 0; i < 27; i++) + { + int x = i; + int zeros = 0, ones = 0, twos = 0; + for (int j = 0; j < 3; j++) + { + p[2 - j] = x % 3; + x /= 3; + if (p[2 - j] == 0) + { + zeros++; + } + else if (p[2 - j] == 1) + { + ones++; + } + else + { + twos++; + } + } + if (zeros > 1 || ones > 1) + { + continue; + } + ok[i] = 1; + ck[i] = Cnt++; + } + for (int ii = 0; ii < 27; ii++) + { + if (!ok[ii]) + { + continue; + } + int i = ck[ii]; + int x = ii; + for (int j = 0; j < 3; j++) + { + p[2 - j] = x % 3; + x /= 3; + } + for (int jj = 0; jj < 27; jj++) + { + if (!ok[jj]) + { + continue; + } + int j = ck[jj]; + int x = jj; + for (int k = 0; k < 3; k++) + { + q[2 - k] = x % 3; + x /= 3; + } + if (p[1] == q[0] && p[2] == q[1]) + { + int zeros = 0, ones = 0, twos = 0; + for (int k = 0; k < 3; k++) + { + if (q[k] == 0) + { + zeros++; + } + else if (q[k] == 1) + { + ones++; + } + else + { + twos++; + } + } + if (zeros > 1 || ones > 1) + { + continue; + } + if (q[2] == 0) + { + g[i][j] = 1; + } + else if (q[2] == 1) + { + g[i][j] = 1; + } + else + { + if (q[2] == 0) + { + zeros--; + } + else if (q[2] == 1) + { + ones--; + } + else + { + twos--; + } + g[i][j] = max(0, (int)K - 2 - twos); + } + } + } + } +} +int main() +{ + memset(flag, -1, sizeof(flag)); + pcnt = 0; + for (int i = 2; i <= 10000; i++) + { + if (flag[i]) + { + pr[pcnt++] = i; + } + for (int j = 0; j < pcnt && pr[j] <= 10000 / i; j++) + { + flag[i * pr[j]] = 0; + if (i % pr[j] == 0) + { + break; + } + } + } + int T; + scanf("%d", &T); + for (int ii = 1; ii <= T; ii++) + { + scanf("%lld%lld%lld", &n, &K, &m); + init(); + m *= n; + for (int i = 0; i < 13; i++) + for (int j = 0; j < 13; j++) + { + num[0][i][j] = g[i][j]; + } + for (int i = 1; i <= 30; i++) + { + mul(num[i], num[i - 1], num[i - 1]); + } + int cnt = 0; + LL tn = n; + for (int i = 0; i < pcnt && pr[i] <= n / pr[i]; i++) + { + if (n % pr[i] == 0) + { + yz[cnt] = pr[i]; + gs[cnt] = 0; + while (n % pr[i] == 0) + { + n /= pr[i]; + gs[cnt]++; + } + cnt++; + if (n == 1) + { + break; + } + } + } + if (n != 1) + { + yz[cnt] = n; + gs[cnt] = 1; + cnt++; + } + n = tn; + anss = 0; + dfs(0, cnt, 1, 1); + printf("Case %d: %lld\n", ii, anss / n); + } + return 0; +} diff --git a/12602.cpp b/12602.cpp new file mode 100644 index 0000000..3cd9cee --- /dev/null +++ b/12602.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + char s[10]; + gets(s); + int L, R; + while (gets(s)) + { + L = (s[0] - 'A') * 26 * 26 + (s[1] - 'A') * 26 + (s[2] - 'A'); + R = (s[4] - '0') * 1000 + (s[5] - '0') * 100 + (s[6] - '0') * 10 + (s[7] - '0'); + if (abs(L - R) <= 100) + puts("nice"); + else + puts("not nice"); + } + return 0; +} diff --git a/12603.cpp b/12603.cpp new file mode 100644 index 0000000..29d9708 --- /dev/null +++ b/12603.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) +#define cl(a, b) memset((a), (b), sizeof(a)) +#define EPS 1e-9 + +struct PT +{ + double x, y; + PT() {} + PT(double x, double y) : x(x), y(y) {} + PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); } + PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); } + PT operator*(double c) const { return PT(x * c, y * c); } + PT operator/(double c) const { return PT(x / c, y / c); } +}; + +double dot(PT p, PT q) +{ + return p.x * q.x + p.y * q.y; +} + +PT Project(PT a, PT b, PT c) +{ + return a + (b - a) * dot(c - a, b - a) / dot(b - a, b - a); +} + +double dist2(PT p, PT q) +{ + return dot(p - q, p - q); +} + +double dist(PT a, PT b, PT c) +{ + return sqrt(dist2(c, Project(a, b, c))); +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + double T, x; + scanf("%lf", &T); + T *= 100.0f; + scanf("%lf", &x); + double xd = x; + xd = (xd / 360.0f) * 2 * M_PI; + xd = (M_PI / 2.0f) - xd; + double alfa = tan(xd) * 42.50f; + double L = 85.0f * tan(xd); + int qnts = floor((T - alfa) / L); + double xf = T - qnts * L - alfa; + double yf = (qnts % 2 == 0) ? 10.0f : 95.0f; + PT b = PT(xf - alfa, (qnts % 2 == 0) ? (yf + 42.50f) : (yf - 42.50f)); + double D = dist(PT(xf, yf), b, PT(0.0f, 52.500f)); + char sim = 0; + sim = (D - 16.0f < EPS); + if (sim) + { + printf("yes\n"); + } + else + { + printf("no\n"); + } + } + return 0; +} diff --git a/12604.cpp b/12604.cpp new file mode 100644 index 0000000..74f7729 --- /dev/null +++ b/12604.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +char A[1024], W[500005], S[500005]; +int kmpTable[500005]; +void KMPtable(char *P, int len) +{ + int i, j; + kmpTable[0] = -1, i = 1, j = -1; + while (i < len) + { + while (j >= 0 && P[j + 1] != P[i]) + j = kmpTable[j]; + if (P[j + 1] == P[i]) + j++; + kmpTable[i++] = j; + } +} +int KMPMatching(char T[], char P[], int tlen, int plen) +{ + int i, j; + int matchCnt = 0; + for (i = 0, j = -1; i < tlen; i++) + { + while (j >= 0 && P[j + 1] != T[i]) + j = kmpTable[j]; + if (P[j + 1] == T[i]) + j++; + if (j == plen - 1) + { + matchCnt++; + j = kmpTable[j]; + if (matchCnt >= 2) + return matchCnt; + } + } + return matchCnt; +} +char E[500005]; +int main() +{ + int testcase; + int i, j, k; + scanf("%d", &testcase); + while (getchar() != '\n') + ; + while (testcase--) + { + gets(A); + gets(W); + gets(S); + int Alen = strlen(A); + int Wlen = strlen(W); + int Slen = strlen(S); + int CHAR[1024] = {}; + for (i = 0; A[i]; i++) + CHAR[A[i]] = i; + int ret[1024], ridx = 0; + for (i = 0; i < Alen; i++) + { + for (j = 0; j < Wlen; j++) + E[j] = A[(CHAR[W[j]] + i) % Alen]; + E[Wlen] = '\0'; + KMPtable(E, Wlen); + int cnt = KMPMatching(S, E, Slen, Wlen); + if (cnt == 1) + ret[ridx++] = i; + } + if (ridx == 0) + puts("no solution"); + else if (ridx == 1) + printf("unique: %d\n", ret[0]); + else + { + printf("ambiguous:"); + for (i = 0; i < ridx; i++) + printf(" %d", ret[i]); + puts(""); + } + } + return 0; +} diff --git a/12605.cpp b/12605.cpp new file mode 100644 index 0000000..b55c6df --- /dev/null +++ b/12605.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +#define maxim 100 +#define LLD long long unsigned +#define LLU long long unsigned +#define HD short int +#define HU short unsigned +#define pi acos(-1.0) +#define inf (1 << 29) +#define CLR(a) memset(a, 0, sizeof(a)) +#define SET(a) memset(a, -1, sizeof(a)) +#define pb push_back +#define sz(a) ((int)a.size()) +#define all(a) a.begin(), a.end() +#define eps 1e-9 +#define rep(i, init, n) for (i = init; i < n; i++) +#define rem(i, init, n) for (i = init; i > n; i--) +#define foreach(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define _abs(a) ((a) < 0 ? (-(a)) : (a)) +#define area(x1, y1, x2, y2, x3, y3) (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) +#define sqr(x) ((x) * (x)) +#define distSqr(x1, y1, x2, y2) (sqr(x1 - x2) + sqr(y1 - y2)) +#define spDist(lat1, long1, lat2, long2, r) (r * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long1 - long2))) +#define isEq(a, b) (fabs(a - b) < eps) +#define STR string +#define LF double +#define IT iterator +#define VI vector +#define VLLD vector +#define VS vector +#define VLF vector +#define MII map +#define MIB map +#define MSI map +#define MSB map +#define MSS map +#define M2dII map> +#define QI queue +#define SI stack +#define PII pair +#define PPI pair +#define ff first +#define ss second +#define VPII vector +#define MP make_pair +#define chk(a, k) ((bool)(a & (1 << (k)))) +#define set0(a, k) (a & (~(1 << (k)))) +#define set1(a, k) (a | (1 << (k))) +#define chkA(a, k) (bool)(a[(k) >> 5] & (1 << ((k)&31))) +#define setA0(a, k) (a[(k) >> 5] &= ~(1 << ((k)&31))) +#define setA1(a, k) (a[(k) >> 5] |= (1 << ((k)&31))) +#define SD(a) scanf("%d", &a) +#define SU(a) scanf("%u", &a) +#define SHD(a) scanf("%hd", &a) +#define SHU(a) scanf("%hu", &a) +#define SF(a) scanf("%f", &a) +#define SLF(a) scanf("%lf", &a) +#define SC(a) scanf("%c", &a) +#define SS(a) scanf("%s", a) +int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1}; +int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1}; +using namespace std; +#define DEB(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << " "; + return *this; + } +} dbg; +template +T sq(T n) { return n * n; } +template +T gcd(T a, T b) { return (b != 0 ? gcd(b, a % b) : a); } +template +T lcm(T a, T b) { return (a / gcd(a, b) * b); } +template +bool inside(T a, T b, T c) { return a <= b && b <= c; } +template +void setmax(T &a, T b) +{ + if (a < b) + a = b; +} +template +void setmin(T &a, T b) +{ + if (b < a) + a = b; +} +template +T power(T N, T P) { return (P == 0) ? 1 : N * power(N, P - 1); } +vector G[20][20]; +char clr[20][20]; +char in[20][20]; +int freq[30]; +int cnt; +void dfs(int i, int j) +{ + cnt++; + freq[in[i][j] - '0']++; + clr[i][j] = 1; + int k, a, b; + rep(k, 0, sz(G[i][j])) + { + a = G[i][j][k].ff; + b = G[i][j][k].ss; + if (clr[a][b]) + continue; + dfs(a, b); + } +} + +int main() +{ + int r, c, n, T, t = 1, m, i, j, k, l, val, x, y, a, b; + SD(T); + rep(t, 1, T + 1) + { + SD(r); + SD(c); + rep(i, 0, r) + SS(in[i]); + rep(i, 0, r) + rep(j, 0, c) + G[i][j] + .clear(); + rep(i, 0, r) + rep(j, 0, c) + { + SD(m); + if (m & 1) + G[i][j].pb(PII(i - 1, j)); + if (m & 2) + G[i][j].pb(PII(i, j + 1)); + if (m & 4) + G[i][j].pb(PII(i + 1, j)); + if (m & 8) + G[i][j].pb(PII(i, j - 1)); + } + CLR(clr); + bool ans = 1; + rep(i, 0, r) + rep(j, 0, c) + { + if (clr[i][j]) + continue; + cnt = 0; + CLR(freq); + dfs(i, j); + rep(k, 1, cnt + 1) if (freq[k] != 1) + ans = 0; + } + rep(i, 0, r) + { + SET(freq); + rep(j, 0, c) + { + if (freq[in[i][j] - '0'] != -1 && j - freq[in[i][j] - '0'] - 1 < in[i][j] - '0') + ans = 0; + freq[in[i][j] - '0'] = j; + } + } + rep(j, 0, c) + { + SET(freq); + rep(i, 0, r) + { + if (freq[in[i][j] - '0'] != -1 && i - freq[in[i][j] - '0'] - 1 < in[i][j] - '0') + ans = 0; + freq[in[i][j] - '0'] = i; + } + } + if (ans) + puts("valid"); + else + puts("invalid"); + } + return 0; +} diff --git a/12606.cpp b/12606.cpp new file mode 100644 index 0000000..99d9026 --- /dev/null +++ b/12606.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +//============================================================================ +// Author : LAM PHAN VIET - lamphanviet@gmail.com +// Problem Name : +// Time Limit : .000s +// Description : +//============================================================================ + +#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) +#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) +#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++) +#define repr(i, n) for (int i = (n)-1; i >= 0; i--) +#define foreach(it, ar) for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) +#define fill(ar, val) memset(ar, val, sizeof(ar)) + +#define uint64 unsigned long long +#define int64 long long +#define all(ar) ar.begin(), ar.end() +#define pb push_back +#define mp make_pair +#define ff first +#define ss second + +#define BIT(n) (1 << (n)) +#define AND(a, b) ((a) & (b)) +#define OR(a, b) ((a)(b)) +#define XOR(a, b) ((a) ^ (b)) +#define sqr(x) ((x) * (x)) + +typedef pair ii; +typedef pair iii; +typedef vector vii; +typedef vector vi; + +#define PI 3.1415926535897932385 +#define INF 1000111222 +#define eps 1e-7 +#define maxN 100111 + +int n, m, len; +bool op1[maxN][2], op2[maxN][2][2], kind[maxN]; +map opIdx; +map value; +char s[maxN]; + +int main() +{ +#ifndef ONLINE_JUDGE + freopen("test.inp", "r", stdin); +//freopen("test.out", "w", stdout); +#endif + int caseNo = 0; + string tmp, ope, var; + while (scanf("%d %d", &n, &m) && (n >= 0 && m >= 0)) + { + opIdx.clear(); + rep(i, n) + { + cin >> ope; + opIdx[ope] = i; + kind[i] = 0; + rep(j, 2) + { + cin >> tmp; + op1[i][j] = (tmp[0] == 't'); + } + } + rep(i, m) + { + cin >> ope; + opIdx[ope] = i + n; + kind[i + n] = 1; + rep(j, 2) rep(k, 2) + { + cin >> tmp; + op2[i + n][j][k] = (tmp[0] == 't'); + } + } + scanf(" "); + gets(s); + len = strlen(s); + value.clear(); + while (cin >> var && var != "*") + { + cin >> tmp; + value[var] = (tmp[0] == 't'); + } + vector ex; + stack st; + for (int i = 0; i < len;) + { + if (s[i] != ' ') + { + if (s[i] == '(') + { + st.push(-1); + i++; + } + else if (s[i] == ')') + { + i++; + while (!st.empty() && st.top() != -1) + { + ex.pb(st.top()); + st.pop(); + } + st.pop(); + } + else if (s[i] == toupper(s[i])) + {// operator + tmp = ""; + while (i < len && isalpha(s[i])) + tmp += s[i++]; + st.push(opIdx[tmp]); + } + else + {// variable + tmp = ""; + while (i < len && isalpha(s[i])) + tmp += s[i++]; + if (value.count(tmp)) + { + ex.pb(value[tmp] ? -1 : -2); + } + else + ex.pb(-3); + } + } + else + i++; + } + while (!st.empty()) + { + ex.pb(st.top()); + st.pop(); + } + // -1: true, -2: false, -3: both, >= 0: operator + stack v; + //rep(i, ex.size()) printf("%d ", ex[i]); puts(""); + rep(i, ex.size()) + { + if (ex[i] == -1) + v.push(2); + else if (ex[i] == -2) + v.push(1); + else if (ex[i] == -3) + v.push(3); + else + { + int id = ex[i]; + if (kind[id] == 0) + { + int top = v.top(), res = 0; + v.pop(); + rep(k, 2) if (top & BIT(k)) + res |= BIT(op1[id][k]); + v.push(res); + //printf("res1 %d\n", res); + } + else + { + int b = v.top(); + v.pop(); + int a = v.top(); + v.pop(); + int res = 0; + rep(u, 2) if (a & BIT(u)) rep(v, 2) if (b & BIT(v)) + res |= BIT(op2[id][u][v]); + v.push(res); + //printf("res2 %d\n", res); + } + } + } + int res = v.top(); + v.pop(); + printf("Case %d: ", ++caseNo); + if (res == 3) + puts("unknown"); + else if (res == 2) + puts("true"); + else + puts("false"); + } + return 0; +} diff --git a/12607.cpp b/12607.cpp new file mode 100644 index 0000000..725097f --- /dev/null +++ b/12607.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +const int dx[] = {-1, 0, 1, 0};// N E S W +const int dy[] = {0, 1, 0, -1}; +int used[100][100][4][1 << 8] = {}, testcase = 0; +int dist[100][100][4][1 << 8] = {}; +int main() +{ + int N, M, K, x, y; + char g[128][128]; + while (scanf("%d %d", &N, &M) == 2 && N + M) + { + int treasure[128][128] = {}, dirg[128][128] = {}; + for (int i = 0; i < N; i++) + { + scanf("%s", g[i]); + for (int j = 0; j < M; j++) + { + if (g[i][j] == 'N') + dirg[i][j] = 0; + if (g[i][j] == 'E') + dirg[i][j] = 1; + if (g[i][j] == 'S') + dirg[i][j] = 2; + if (g[i][j] == 'W') + dirg[i][j] = 3; + } + } + scanf("%d", &K); + for (int i = 0; i < K; i++) + { + scanf("%d %d", &x, &y); + x--, y--; + assert(treasure[x][y] == 0); + treasure[x][y] = i + 1; + } + queue X, Y, D, T; + int x = 0, y = 0, d = 0, t = 0; + int tx, ty, td, tt; + if (treasure[x][y]) + t |= 1 << (treasure[x][y] - 1); + X.push(x), Y.push(y), D.push(d), T.push(t); + testcase++; + used[x][y][d][t] = testcase; + dist[x][y][d][t] = 0; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + d = D.front(), D.pop(); + t = T.front(), T.pop(); + + tx = x, ty = y, td = (d + 1) % 4, tt = t; + if (used[tx][ty][td][tt] != testcase) + { + used[tx][ty][td][tt] = testcase; + dist[tx][ty][td][tt] = dist[x][y][d][t] + 1; + X.push(tx), Y.push(ty), D.push(td), T.push(tt); + if (tx == N - 1 && ty == M - 1 && tt == (1 << K) - 1) + break; + } + + int dir = (dirg[x][y] + d) % 4; + tx = x + dx[dir], ty = y + dy[dir], td = (d + 1) % 4, tt = t; + if (tx < 0 || ty < 0 || tx >= N || ty >= M) + continue; + if (treasure[tx][ty]) + tt |= 1 << (treasure[tx][ty] - 1); + if (used[tx][ty][td][tt] != testcase) + { + used[tx][ty][td][tt] = testcase; + dist[tx][ty][td][tt] = dist[x][y][d][t] + 1; + X.push(tx), Y.push(ty), D.push(td), T.push(tt); + if (tx == N - 1 && ty == M - 1 && tt == (1 << K) - 1) + break; + } + } + int ret = 0x3f3f3f3f; + for (int i = 0; i < 4; i++) + { + if (used[N - 1][M - 1][i][(1 << K) - 1] == testcase) + ret = min(ret, dist[N - 1][M - 1][i][(1 << K) - 1]); + } + printf("%d\n", ret); + } + return 0; +} +/* +2 2 +EE +NN +1 +1 2 + +1 1 +E +1 +1 1 + +1 2 +NE +1 +1 2 + +0 0 +*/ diff --git a/12608.cpp b/12608.cpp new file mode 100644 index 0000000..fac8491 --- /dev/null +++ b/12608.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +typedef std::istringstream ISS; +typedef std::ostringstream OSS; +typedef std::vector VS; +typedef long long LL; +typedef int INT; +typedef std::vector VI; +typedef std::vector VVI; +typedef std::pair II; +typedef std::vector VII; + +const int SIZE = 1011; +int L, N, X[SIZE], W[SIZE]; + +inline LL solve() +{ + LL res = 0; + int sum = 0; + for (int i = 0; i < N; ++i) + { + if (sum + W[i] > L) + { + res += X[i] * 2; + sum = 0; + } + res += X[i]; + sum += W[i]; + if (i - 1 >= 0) + { + res -= X[i - 1]; + } + if (i + 1 < N && sum == L) + { + res += X[i] * 2; + sum = 0; + } + } + res += X[N - 1]; + return res; +} + +int main() +{ + int tests; + cin >> tests; + for (int i = 0; i < tests; ++i) + { + cin >> L >> N; + for (int i = 0; i < N; ++i) + cin >> X[i] >> W[i]; + cout << solve() << endl; + } + return 0; +} diff --git a/12609.cpp b/12609.cpp new file mode 100644 index 0000000..8238807 --- /dev/null +++ b/12609.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +//============================================================================ +// Author : LAM PHAN VIET - lamphanviet@gmail.com +// Problem Name : +// Time Limit : .000s +// Description : +//============================================================================ + +#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) +#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--) +#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++) +#define repr(i, n) for (int i = (n)-1; i >= 0; i--) +#define foreach(it, ar) for (typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++) +#define fill(ar, val) memset(ar, val, sizeof(ar)) + +#define uint64 unsigned long long +#define int64 long long +#define all(ar) ar.begin(), ar.end() +#define pb push_back +#define mp make_pair +#define ff first +#define ss second + +#define BIT(n) (1 << (n)) +#define AND(a, b) ((a) & (b)) +#define OR(a, b) ((a)(b)) +#define XOR(a, b) ((a) ^ (b)) +#define sqr(x) ((x) * (x)) + +typedef pair ii; +typedef pair iii; +typedef vector vii; +typedef vector vi; + +#define PI 3.1415926535897932385 +#define INF 1000111000111000LL +#define eps 1e-7 +#define maxN 100111 + +int64 n; + +int64 countDigit(int64 m) +{ + int64 lo = 1, res = 0; + fr(i, 1, 18) + { + if (lo * 10 > m) + { + res += (m - lo + 1) * i; + break; + } + res += lo * 9LL * i; + lo *= 10LL; + } + return res; +} + +int64 getLength(int64 m) +{ + return countDigit(m) + (m - (m / 4)); +} + +int getDigit(int64 m, int pos) +{ + vi d; + while (m) + { + d.pb(m % 10); + m /= 10; + } + reverse(all(d)); + return d[pos - 1]; +} + +int main() +{ +#ifndef ONLINE_JUDGE + freopen("test.inp", "r", stdin); +//freopen("test.out", "w", stdout); +#endif + while (scanf("%lld", &n) && n) + { + int64 lo = 1, hi = INF, pos = 1; + while (lo <= hi) + { + int64 mid = (lo + hi) / 2; + if (getLength(mid) >= n) + { + pos = mid; + hi = mid - 1; + } + else + lo = mid + 1; + } + n -= getLength(pos - 1); + if (pos % 4) + { + for (int i = 0; i < 10; i += 2) + if ((pos * 10 + i) % 4 == 0) + { + pos = pos * 10 + i; + break; + } + } + printf("%d\n", getDigit(pos, n)); + } + return 0; +} diff --git a/1261.cpp b/1261.cpp new file mode 100644 index 0000000..1f8c6c6 --- /dev/null +++ b/1261.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +struct io +{ + template + inline static T next() + { + T n; + std::cin >> n; + return n; + } +}; + +static bool can(string s) +{ + if (s.empty()) + { + return true; + } + bool ok = false; + for (int i = 0, j; i < s.size() && !ok; i++) + { + for (j = i; j < s.size() && s[i] == s[j]; j++) + ; + if (j - i >= 2) + { + ok |= can(s.substr(0, i) + s.substr(j)); + } + i = j - 1; + } + return ok; +} + +int main(int argc, char **args) +{ + for (int T = io::next(); T-- > 0;) + { + printf("%d\n", can(io::next()) ? 1 : 0); + } + return 0; +} diff --git a/12610.cpp b/12610.cpp new file mode 100644 index 0000000..c0291c4 --- /dev/null +++ b/12610.cpp @@ -0,0 +1,256 @@ +#include + +using namespace std; + +const int maxn = 400000; + +int cnttree; + +struct node +{ + node *ch[2], *f; + int key, key2, num; + + void flow() + { + num = ch[0]->num + ch[1]->num + 1; + } + void rot(node *&rt) + { + node *t = f; + int isr = (t->ch[1] == this); + t->ch[isr] = ch[!isr], ch[!isr]->f = t; + f = t->f, t->f = this, ch[!isr] = t; + f->ch[f->ch[1] == t] = this; + t->flow(), flow(); + if (!f->num) + { + rt = this; + } + } + void ins(node *&rt, node *ff, node *t) + { + if (!num) + { + t->f = ff; + if (!ff->num) + { + rt = t; + } + else + { + ff->ch[t->key > ff->key] = t; + } + while (t->f->num && t->key2 > t->f->key2) + { + t->rot(rt); + } + return; + } + num++; + if (t->key <= key) + { + ch[0]->ins(rt, this, t); + } + else + { + ch[1]->ins(rt, this, t); + } + } +} nn[maxn]; + +struct treap +{ + node *rt; + + void ins(int k) + { + ++cnttree; + node *t = nn + cnttree; + t->key = k, t->key2 = rand(); + t->f = t->ch[0] = t->ch[1] = nn; + t->num = 1; + rt->ins(rt, nn, t); + } + void del(int k) + { + if (rt->num == 1) + { + rt = nn; + return; + } + node *t = rt; + while (t->key - k) + { + if (k < t->key) + { + t = t->ch[0]; + } + else + { + t = t->ch[1]; + } + } + while (t->ch[0] - nn || t->ch[1] - nn) + { + if (t->ch[0] && (t->ch[1] == nn || t->ch[0]->key2 > t->ch[1]->key2)) + { + t->ch[0]->rot(rt); + } + else + { + t->ch[1]->rot(rt); + } + } + t->f->ch[t->f->ch[1] == t] = nn; + t = t->f; + while (t - nn) + { + t->num--, t = t->f; + } + } + int get() + { + int ans = 0; + node *t = rt; + while (t->ch[1] - nn) + { + t = t->ch[1]; + } + return t->key; + } + int getk(int k) + { + node *t = rt; + while (1) + { + if (t->ch[0]->num + 1 == k) + { + return t->key; + } + if (t->ch[0]->num >= k) + { + t = t->ch[0]; + } + else + { + k -= t->ch[0]->num + 1, t = t->ch[1]; + } + } + } +} tree[maxn], now; + +int M[maxn], siz[maxn], tot; + +void del(int c, int k) +{ + if (siz[c] == 1) + { + siz[c] = 0; + tree[c].del(k); + now.del(M[c]); + M[c] = -1; + tot--; + } + else + { + siz[c]--; + now.del(M[c]); + tree[c].del(k); + now.ins(M[c] = tree[c].get()); + } +} +void ins(int c, int k) +{ + if (siz[c] == 0) + { + siz[c] = 1; + tot++; + tree[c].ins(k); + now.ins(M[c] = tree[c].get()); + } + else + { + siz[c]++; + now.del(M[c]); + tree[c].ins(k); + now.ins(M[c] = tree[c].get()); + } +} +int query(int L) +{ + if (tot < L) + { + return 0; + } + return now.getk(tot - L + 1); +} +struct NN +{ + int l, r, h, c; +} mm[maxn]; + +int arr[maxn], cnt; +vector> V1[maxn]; +vector> V2[maxn]; + +int main() +{ + int t, ii = 0; + int N, n, L, i, j, k; + scanf("%d", &t); + while (t--) + { + scanf("%d%d%d", &N, &n, &L); + for (i = 1; i <= n; i++) + { + scanf("%d%d%d%d", &mm[i].h, &mm[i].l, &mm[i].r, &mm[i].c); + } + for (i = 1; i <= n; i++) + { + arr[i] = mm[i].l, arr[i + n] = ++mm[i].r; + } + sort(arr + 1, arr + 1 + n + n); + int m = unique(arr + 1, arr + 1 + n + n) - arr - 1; + for (i = 1; i <= m; i++) + { + V1[i].clear(), V2[i].clear(); + } + for (i = 1; i <= n; i++) + { + mm[i].l = lower_bound(arr + 1, arr + 1 + m, mm[i].l) - arr; + mm[i].r = lower_bound(arr + 1, arr + 1 + m, mm[i].r) - arr; + V1[mm[i].l].push_back(make_pair(mm[i].h, mm[i].c)); + V2[mm[i].r].push_back(make_pair(mm[i].h, mm[i].c)); + } + now.rt = nn; + for (i = 1; i <= 100000; i++) + { + siz[i] = 0, tree[i].rt = nn; + } + tot = 0, cnttree = 0; + printf("Case %d: ", ++ii); + long long ans = 0; + if (L == 0) + { + ans = N; + ans *= N; + printf("%lld\n", ans); + continue; + } + for (i = 1; i < m; i++) + { + for (j = 0; j < V2[i].size(); j++) + { + del(V2[i][j].second, V2[i][j].first); + } + for (j = 0; j < V1[i].size(); j++) + { + ins(V1[i][j].second, V1[i][j].first); + } + ans += query(L) * (arr[i + 1] - arr[i] + 0ll); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12611.cpp b/12611.cpp new file mode 100644 index 0000000..07ccb5a --- /dev/null +++ b/12611.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + int t, cases = 0, r; + scanf("%d", &t); + while (t--) + { + scanf("%d", &r); + int length = r * 5, width = length * 60 / 100; + int right = length * 45 / 100, left = length * 55 / 100; + printf("Case %d:\n", ++cases); + printf("%d %d\n", -right, width / 2); + printf("%d %d\n", left, width / 2); + printf("%d %d\n", left, -width / 2); + printf("%d %d\n", -right, -width / 2); + } + return 0; +} diff --git a/12612.cpp b/12612.cpp new file mode 100644 index 0000000..10e0e7c --- /dev/null +++ b/12612.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define MAXN 20005 + +typedef long long LL; +typedef pair PII; +typedef pair PII2; + +int points[MAXN][3]; +int n; +int ax, bx, ay, by, az, bz; + +bool inside(int x, int y, int z) +{ + return bx < x && x < ax && + by < y && y < ay && + bz < z && z < az; +} +bool good() +{ + for (int i = 0; i < n; i++) + { + if (points[i][0] < bx) + return false; + if (points[i][0] > ax) + return false; + if (points[i][1] < by) + return false; + if (points[i][1] > ay) + return false; + if (points[i][2] < bz) + return false; + if (points[i][2] > az) + return false; + if (inside(points[i][0], points[i][1], points[i][2])) + return false; + } + return true; +} + +void solve() +{ + int maxx, maxy, maxz, minx, miny, minz; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d %d %d", &points[i][0], &points[i][1], &points[i][2]); + } + maxx = minx = points[0][0]; + maxy = miny = points[0][1]; + maxz = minz = points[0][2]; + + for (int i = 1; i < n; i++) + { + maxx = max(maxx, points[i][0]); + minx = min(minx, points[i][0]); + maxy = max(maxy, points[i][1]); + miny = min(miny, points[i][1]); + maxz = max(maxz, points[i][2]); + minz = min(minz, points[i][2]); + } + + int maxsize = max(maxx - minx, max(maxy - miny, maxz - minz)); + + for (int i = 0; i < 1 << 3; i++) + { + ax = maxx, bx = minx, ay = maxy, by = miny, az = maxz, bz = minz; + if (i & 1) + ax = bx + maxsize; + else + bx = ax - maxsize; + if (i & 2) + ay = by + maxsize; + else + by = ay - maxsize; + if (i & 4) + az = bz + maxsize; + else + bz = az - maxsize; + if (good()) + { + printf("%d\n", maxsize); + return; + } + } + printf("-1\n"); +} + +int main() +{ + int test; + scanf("%d", &test); + for (int tt = 1; tt <= test; tt++) + { + printf("Case %d: ", tt); + solve(); + } + return 0; +} diff --git a/12613.cpp b/12613.cpp new file mode 100644 index 0000000..095330b --- /dev/null +++ b/12613.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define M 800010 +#define N 20011 +#define ll long long +#define inf 0x3f3f3f3f +#define ull unsigned long long + +int son[M][26], pre[M], len[M]; +int t, tail; +void init() +{ + len[0] = t = tail = 0; + memset(son[0], 0, sizeof(son[0])); + pre[0] = -1; +} + +void add(int x) +{ + int p = tail, np = ++t; + tail = np; + memset(son[t], 0, sizeof(son[t])); + len[np] = len[p] + 1; + while (p != -1 && !son[p][x]) + { + son[p][x] = np, p = pre[p]; + } + if (p == -1) + { + pre[np] = 0; + } + else + { + int q = son[p][x]; + if (len[p] + 1 == len[q]) + { + pre[np] = q; + } + else + { + int nq = ++t; + memcpy(son[nq], son[q], sizeof(son[q])); + len[nq] = len[p] + 1; + pre[nq] = pre[q]; + pre[q] = pre[np] = nq; + while (p != -1 && son[p][x] == q) + { + son[p][x] = nq, p = pre[p]; + } + } + } +} + +ll getCnt() +{ + ll ans = 0; + int p; + for (int i = 1; i <= t; ++i) + { + p = i; + ans += len[p] - len[pre[p]]; + } + return ans; +} +char str[M]; +int k; +ll dp[M]; + +int main() +{ + int T, cas = 0; + scanf("%d", &T); + ll one, two, more, ans; + while (T--) + { + scanf("%s%d", str, &k); + init(); + for (int i = 0; str[i]; ++i) + { + add(str[i] - 'a'); + } + one = getCnt(); + dp[1] = one; + //cout< + +using namespace std; + +int main() +{ + int t, cases = 0, n, x; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + int ret = 0; + while (n--) + { + scanf("%d", &x); + ret = max(ret, x); + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/12616.cpp b/12616.cpp new file mode 100644 index 0000000..b973e6b --- /dev/null +++ b/12616.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define MAXN 20005 + +typedef long long LL; +typedef pair PII; +typedef pair PII2; + +int low_number[MAXN]; +int numComponent; +int dfs_number[MAXN]; +vector adj[MAXN]; +int N; +LL numChild[MAXN]; +LL ans = 0; +bool visited[MAXN]; +int total[MAXN]; + +void dfs(int parent, int *cnt, int u, int N, int root) +{ + dfs_number[u] = low_number[u] = (*cnt)++; + int ss = adj[u].size(); + numChild[u] = 1; + LL temp = 0; + for (int i = 0; i < ss; ++i) + { + int v = adj[u][i]; + + if (dfs_number[v] == 0) + { + dfs(u, cnt, v, N, root); + numChild[u] += numChild[v]; + low_number[u] = min(low_number[u], low_number[v]); + // handle root vertex case (u == 0) + if ((u != root && low_number[v] >= dfs_number[u]) || + (u == root && low_number[v] > dfs_number[u] && ss > 1)) + { + // printf("\n(%d) is an articulation point seperating (%d)\n", u, v); + ans += numChild[v] * (N - numChild[v] - 1); + temp += numChild[v]; + } + } + else if (parent != v) + { + low_number[u] = min(low_number[u], dfs_number[v]); + } + } + // cout << u << "--- " << root << " " << temp << " " << N << endl; + /* if (u != root){*/ + ans += (N - temp - 1) * temp; + // } +} + +void dfs2(int u) +{ + visited[u] = true; + total[u] = 1; + for (int i = 0; i < adj[u].size(); i++) + { + if (!visited[adj[u][i]]) + { + dfs2(adj[u][i]); + total[u] += total[adj[u][i]]; + } + } +} + +void print_biconnected_component(int N) +{ + int cnt = 1; + for (int i = 1; i <= N; i++) + if (!visited[i]) + dfs2(i); + for (int i = 1; i <= N; i++) + if (dfs_number[i] == 0) + dfs(-1, &cnt, i, total[i], i); +} + +void solve() +{ + ans = 0; + int M; + scanf("%d %d", &N, &M); + for (int i = 1; i <= N; i++) + { + adj[i].clear(); + dfs_number[i] = low_number[i] = 0; + numChild[i] = 0; + total[i] = 0; + visited[i] = false; + } + + int u, v; + for (int i = 0; i < M; i++) + { + scanf("%d %d", &u, &v); + adj[u].push_back(v); + adj[v].push_back(u); + } + numComponent = 0; + print_biconnected_component(N); + cout << ans << endl; +} + +int main() +{ + int test; + scanf("%d", &test); + for (int tt = 1; tt <= test; tt++) + { + printf("Case %d: ", tt); + solve(); + } + return 0; +} diff --git a/12617.cpp b/12617.cpp new file mode 100644 index 0000000..b4f3906 --- /dev/null +++ b/12617.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +const double eps = 1e-6; +const int inf = 1000; +#define sqr(x) ((x) * (x)) +inline int sign(double x) +{ + return x < -eps ? -1 : x > eps; +} + +struct Point +{ + double x, y; + Point() {} + Point(double x, double y) : x(x), y(y) {} + void rd() + { + scanf("%lf %lf", &x, &y); + } + void show() + { + cout << x << ' ' << y << endl; + } + Point operator-(const Point &o) const + { + return Point(x - o.x, y - o.y); + } + Point operator+(const Point &o) const + { + return Point(x + o.x, y + o.y); + } + Point operator*(double o) const + { + return Point(x * o, y * o); + } + Point operator/(double o) + { + return Point(x / o, y / o); + } + bool operator==(const Point &o) const + { + return sign(x - o.x) == 0 and sign(y - o.y) == 0; + } + double det(const Point &o) + { + return x * o.y - y * o.x; + } + double dot(const Point &o) + { + return x * o.x + y * o.y; + } + double abs() + { + return sqrt(sqr(x) + sqr(y)); + } + Point unit() + { + return *this / abs(); + } +}; + +struct Segment +{ + Point a, b; + Segment() {} + Segment(Point a, Point b) : a(a), b(b) {} + friend bool intersect(const Segment &u, const Segment &v) + { + return (max(u.a.x, u.b.x) >= min(v.a.x, v.b.x)) && + (max(v.a.x, v.b.x) >= min(u.a.x, u.b.x)) && + (max(u.a.y, u.b.y) >= min(v.a.y, v.b.y)) && + (max(v.a.y, v.b.y) >= min(u.a.y, u.b.y)) && + sign((v.a - u.a).det(u.b - u.a) * ((u.b - u.a).det(v.b - u.a))) >= 0 && + sign((u.a - v.a).det(v.b - v.a) * ((v.b - v.a).det(u.b - v.a))) >= 0; + } + Point commonPoint(const Segment &o) const + { + double a1 = (o.b - a).det(o.a - a), a2 = (o.a - b).det(o.b - b); + return Point((a.x * a2 + b.x * a1) / (a2 + a1), (a.y * a2 + b.y * a1) / (a2 + a1)); + } +}; +int s, T, r; +Point p[7], a, b, dir; + +inline double dis(const Point &a) +{ + return sqrt(a.x * a.x + a.y * a.y); +} +inline double P2S(const Point &p, const Segment &s) +{ + if ((p - s.a).dot(s.b - s.a) > eps && (p - s.b).dot(s.a - s.b) > eps) + return fabs((p - s.a).det(s.b - s.a)) / dis(s.b - s.a); + return min(dis(p - s.a), dis(p - s.b)); +} +Point rotate(const Segment &mirror, const Point &interPt) +{ + Point m_dir = (mirror.b - mirror.a).unit(); + Point cz_dir = Point(m_dir.y, -1 * m_dir.x).unit(); + Point tem = Segment(a - (m_dir * inf), a + (m_dir * inf)).commonPoint(Segment(interPt - (cz_dir * inf), interPt + (cz_dir * inf))); + Point res = (tem * 2) - a; + return (res - interPt).unit(); +} +void solve() +{ + while (T > 0) + { + // cout << T << endl; + Segment cur = Segment(a, a + (dir * inf)); + // cur.a.show(); + // cur.b.show(); + if (P2S(Point(0, 0), cur) < r + eps) + { + printf("%d\n", T); + return; + } + for (int i = 0; i < 6; ++i) + { + if (intersect(cur, Segment(p[i], p[i + 1]))) + { + Point interPt = cur.commonPoint(Segment(p[i], p[i + 1])); + if (interPt == a) + continue; + // interPt.show();cout << "---" << endl; + if (interPt == p[i] or interPt == p[i + 1]) + { + a = interPt, dir = dir * -1; + T -= 2; + } + else + { + dir = rotate(Segment(p[i], p[i + 1]), interPt); + a = interPt; + T -= 1; + } + break; + } + } + } + puts("Stops"); +} + +int main() +{ + int t, cas = 1; + for (scanf("%d", &t); t--;) + { + printf("Case %d: ", cas++); + scanf("%d", &s); + p[0] = Point(0.5 * s, sqrt(3.0) * s / 2); + p[1] = Point(s, 0); + p[2] = Point(0.5 * s, -sqrt(3.0) * s / 2); + p[3] = Point(-0.5 * s, -sqrt(3.0) * s / 2); + p[4] = Point(-s, 0); + p[5] = Point(-0.5 * s, sqrt(3.0) * s / 2); + p[6] = p[0]; + a.rd(); + b.rd(); + scanf("%d %d", &r, &T); + dir = (b - a).unit(); + solve(); + } + return 0; +} diff --git a/12618.cpp b/12618.cpp new file mode 100644 index 0000000..afd744c --- /dev/null +++ b/12618.cpp @@ -0,0 +1,248 @@ +#include + +using namespace std; + +/** + W W W 1 2 3 + W W W 4 5 6 + W W W 7 8 9 +R R R B B B O O O G G G 10 11 12 13 14 15 16 17 18 19 20 21 +R R R B B B O O O G G G 22 23 24 25 26 27 28 29 30 31 32 33 +R R R B B B O O O G G G 34 35 36 37 38 39 40 41 42 43 44 45 + Y Y Y 46 47 48 + Y Y Y 49 50 51 + Y Y Y 52 53 54 + +4 +WWWWWWWWWRRRBBBOOOGGGRRRBBBOOOGGGRRRBBBOOOGGGYYYYYYYYY +WWWWWWRRRRRYBBBWOOGGGRRYBBBWOOGGGRRYBBBWOOGGGOOOYYYYYY +WWBWWBWWBRRRBBYOOOWGGBBYOOOWGGRRRRRRBBYOOOWGGYYGYYGYYG +WWWWWWWWWRRRBBBOOOGGGRRRBBBOOOGGGRRRBBBOOOYYYGGGYYYYYY +**/ + +int rotateInfo[9][12] = { + {1, 4, 7, 13, 25, 37, 46, 49, 52, 45, 33, 21},// margin rotate begin + {3, 6, 9, 15, 27, 39, 48, 51, 54, 43, 31, 19}, + {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, + {34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45}, + {1, 2, 3, 18, 30, 42, 54, 53, 52, 34, 22, 10}, + {7, 8, 9, 16, 28, 40, 48, 47, 46, 36, 24, 12}, + {2, 5, 8, 14, 26, 38, 47, 50, 53, 44, 32, 20},// center rotate begin + {4, 5, 6, 17, 29, 41, 51, 50, 49, 35, 23, 11}, + {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}}; +int rotateFace[6][9] = {// non-center rotate + {10, 11, 12, 22, 23, 24, 34, 35, 36}, + {18, 17, 16, 30, 29, 28, 42, 41, 40}, + {1, 4, 7, 2, 5, 8, 3, 6, 9}, + {52, 49, 46, 53, 50, 47, 54, 51, 48}, + {21, 20, 19, 33, 32, 31, 45, 44, 43}, + {13, 14, 15, 25, 26, 27, 37, 38, 39}}; +int rotateOrder1[9] = {2, 5, 8, 1, 4, 7, 0, 3, 6}; +int rotateOrder2[9] = {6, 3, 0, 7, 4, 1, 8, 5, 2}; + +int face[6][9] = { + {1, 2, 3, 4, 5, 6, 7, 8, 9}, + {10, 11, 12, 22, 23, 24, 34, 35, 36}, + {13, 14, 15, 25, 26, 27, 37, 38, 39}, + {16, 17, 18, 28, 29, 30, 40, 41, 42}, + {19, 20, 21, 31, 32, 33, 43, 44, 45}, + {46, 47, 48, 49, 50, 51, 52, 53, 54}}; + +map Rstep, Bstep; + +void adjustInfo() +{ + for (int i = 0; i < 9; i++) + for (int j = 0; j < 12; j++) + rotateInfo[i][j]--; + for (int i = 0; i < 6; i++) + for (int j = 0; j < 9; j++) + face[i][j]--; + for (int i = 0; i < 6; i++) + for (int j = 0; j < 9; j++) + rotateFace[i][j]--; +} +int isCompleted(const char A[]) +{ + for (int i = 0; i < 6; i++) + { + int ok = 1; + for (int j = 0; j < 9; j++) + ok &= A[face[i][j]] == A[face[i][0]]; + if (!ok) + return ok; + } + return 1; +} +string reduceCube(string u) +{ + static int used[128] = {}, R[128], testcase = 0; + char idx = 'A'; + testcase++; + for (int i = 0; i < u.length(); i++) + { + if (used[u[i]] != testcase) + { + R[u[i]] = idx++, used[u[i]] = testcase; + } + u[i] = R[u[i]]; + } + return u; +} +string rotateCube(string u, int kind, int dir) +{ + static int a, b, c; + static char tmp[9]; + if (dir == 0) + { + char v[3] = {u[rotateInfo[kind][0]], u[rotateInfo[kind][1]], u[rotateInfo[kind][2]]}; + for (int i = 0; i < 3; i++) + { + a = i * 3, b = i * 3 + 1, c = i * 3 + 2; + u[rotateInfo[kind][a]] = u[rotateInfo[kind][a + 3]]; + u[rotateInfo[kind][b]] = u[rotateInfo[kind][b + 3]]; + u[rotateInfo[kind][c]] = u[rotateInfo[kind][c + 3]]; + } + a = 3 * 3, b = 3 * 3 + 1, c = 3 * 3 + 2; + u[rotateInfo[kind][a]] = v[0]; + u[rotateInfo[kind][b]] = v[1]; + u[rotateInfo[kind][c]] = v[2]; + if (kind < 6) + { + for (int i = 0; i < 9; i++) + tmp[i] = u[rotateFace[kind][i]]; + for (int i = 0; i < 9; i++) + u[rotateFace[kind][i]] = tmp[rotateOrder1[i]]; + } + } + else + { + char v[3] = {u[rotateInfo[kind][9]], u[rotateInfo[kind][10]], u[rotateInfo[kind][11]]}; + for (int i = 3; i > 0; i--) + { + a = i * 3, b = i * 3 + 1, c = i * 3 + 2; + u[rotateInfo[kind][a]] = u[rotateInfo[kind][a - 3]]; + u[rotateInfo[kind][b]] = u[rotateInfo[kind][b - 3]]; + u[rotateInfo[kind][c]] = u[rotateInfo[kind][c - 3]]; + } + a = 0, b = 1, c = 2; + u[rotateInfo[kind][a]] = v[0]; + u[rotateInfo[kind][b]] = v[1]; + u[rotateInfo[kind][c]] = v[2]; + if (kind < 6) + { + for (int i = 0; i < 9; i++) + tmp[i] = u[rotateFace[kind][i]]; + for (int i = 0; i < 9; i++) + u[rotateFace[kind][i]] = tmp[rotateOrder2[i]]; + } + } + return u; +} + +int hfunction(string u) +{ + static int used[128] = {}, testcase = 0, cnt = 0; + int ret = 0; + for (int i = 0; i < 6; i++) + { + testcase++, cnt = 0; + for (int j = 0; j < 9; j++) + { + if (used[u[face[i][j]]] != testcase) + { + used[u[face[i][j]]] = testcase; + cnt++; + } + } + ret = max(ret, cnt - 1); + } + return ret; +} +void bfs(string A) +{ + queue Q; + string u, v; + Rstep.clear(); + A = reduceCube(A); + Q.push(A), Rstep[A] = 0; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + int step = Rstep[u]; + if (step >= 4) + continue; + for (int i = 0; i < 9; i++) + { + v = reduceCube(rotateCube(u, i, 0)); + if (Rstep.find(v) == Rstep.end()) + { + Rstep[v] = step + 1; + Q.push(v); + } + v = reduceCube(rotateCube(u, i, 1)); + if (Rstep.find(v) == Rstep.end()) + { + Rstep[v] = step + 1; + Q.push(v); + } + } + } + // printf("4 steps state %d\n", Rstep.size()); +} + +int bfs2(string A) +{ + if (isCompleted(A.c_str())) + return 0; + queue Q; + string u, v; + Bstep.clear(); + A = reduceCube(A); + Q.push(A), Bstep[A] = 0; + int ret = 0x3f3f3f3f; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + int step = Bstep[u]; + if (Rstep.find(u) != Rstep.end()) + ret = min(ret, step + Rstep[u]); + if (step >= 3 || step + hfunction(u) >= min(7, ret)) + continue; + for (int i = 0; i < 9; i++) + { + v = reduceCube(rotateCube(u, i, 0)); + if (Bstep.find(v) == Bstep.end()) + { + Bstep[v] = step + 1; + Q.push(v); + } + v = reduceCube(rotateCube(u, i, 1)); + if (Bstep.find(v) == Bstep.end()) + { + Bstep[v] = step + 1; + Q.push(v); + } + } + } + return ret <= 7 ? ret : -1; +} +int main() +{ + adjustInfo(); + bfs("WWWWWWWWWRRRBBBOOOGGGRRRBBBOOOGGGRRRBBBOOOGGGYYYYYYYYY"); + int testcase, cases = 0; + char A[64]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", A); + int ret = bfs2(A); + printf("Case %d: ", ++cases); + if (ret == -1) + puts("Impossible"); + else + printf("%d\n", ret); + } + return 0; +} diff --git a/12619.cpp b/12619.cpp new file mode 100644 index 0000000..591a1ff --- /dev/null +++ b/12619.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +#define MOD 1000000007 +#define MAXL (1000000 >> 5) + 1 +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) + +int mark[MAXL], pfactor[1000005], pfactorn[1000005], nxt[1000005], mapped[1000005], cnt[80000]; + +void sieve() +{ + register int i, j, k; + SET(1); + int n = 1000000; + int pt = 0; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + mapped[i] = pt; + pt++; + for (k = n / i, j = i * k; k >= i; k--, j -= i) + { + pfactor[j] = i; + SET(j); + } + pfactor[i] = i; + } + } + for (i = 2; i <= n; i++) + { + j = i, k = pfactor[i]; + while (j % k == 0) + { + pfactorn[i]++, j /= k; + } + nxt[i] = j; + } +} +long long mpow(long long x, long long y, long long mod) +{ + if (y == 0) + { + return 1; + } + if (y & 1) + { + return (x * mpow((x * x) % mod, y >> 1, mod)) % mod; + } + return mpow((x * x) % mod, y >> 1, mod); +} +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + { + return EOF; + } + p = buf; + } + return *p++; +} +inline int ReadInt(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + return 0; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + { + *x = (*x << 3) + (*x << 1) + c - '0'; + } + *x *= neg; + return 1; +} + +int main() +{ + sieve(); + int testcase, cases = 0, n, D; + ReadInt(&testcase); + while (testcase--) + { + ReadInt(&n); + long long ret = 1, sum = 0; + int neg; + memset(cnt, 0, sizeof(cnt)); + while (n--) + { + ReadInt(&D); + if (D < 0) + { + neg = 1, D = -D; + } + else + { + neg = 0; + } + while (D > 1) + { + int &v = cnt[mapped[pfactor[D]]]; + ret = (ret * mpow(v + 1, MOD - 2, MOD)) % MOD; + if (neg) + { + v -= pfactorn[D]; + } + else + { + v += pfactorn[D]; + } + ret = (ret * (v + 1)) % MOD; + D = nxt[D]; + } + sum = sum + ret; + if (sum >= MOD) + { + sum -= MOD; + } + } + printf("Case %d: %lld\n", ++cases, sum); + } + return 0; +} diff --git a/1262.cpp b/1262.cpp new file mode 100644 index 0000000..bcd5119 --- /dev/null +++ b/1262.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +char a[5][6], b[5][6]; + +int main() +{ + int tc, q; + scanf("%d", &tc); + + while (tc--) + { + scanf("%d", &q); + for (int i = 0; i < 6; i++) + { + for (int j = 0; j < 5; j++) + { + scanf("%c", &a[j][i]); + if (a[j][i] == '\n') + { + j--; + } + } + } + for (int i = 0; i < 6; i++) + { + for (int j = 0; j < 5; j++) + { + scanf("%c", &b[j][i]); + if (b[j][i] == '\n') + { + j--; + } + } + } + + for (int i = 0; i < 5; i++) + { + sort(a[i], a[i] + 6); + sort(b[i], b[i] + 6); + } + + char p1, p2, p3, p4, p5; + int cnt = 0; + for (int i = 0; i < 6; i++) + { + if (i > 0 && a[0][i] == a[0][i - 1]) + { + continue; + } + for (int j = 0; j < 6; j++) + { + if (j > 0 && a[1][j] == a[1][j - 1]) + { + continue; + } + for (int k = 0; k < 6; k++) + { + if (k > 0 && a[2][k] == a[2][k - 1]) + { + continue; + } + for (int l = 0; l < 6; l++) + { + if (l > 0 && a[3][l] == a[3][l - 1]) + { + continue; + } + for (int m = 0; m < 6; m++) + { + if (m > 0 && a[4][m] == a[4][m - 1]) + { + continue; + } + + p1 = a[0][i]; + p2 = a[1][j]; + p3 = a[2][k]; + p4 = a[3][l]; + p5 = a[4][m]; + if (binary_search(b[0], b[0] + 6, p1) && binary_search(b[1], b[1] + 6, p2) && binary_search(b[2], b[2] + 6, p3) && binary_search(b[3], b[3] + 6, p4) && binary_search(b[4], b[4] + 6, p5)) + { + cnt++; + } + + if (cnt == q) + { + break; + } + } + if (cnt == q) + { + break; + } + } + if (cnt == q) + { + break; + } + } + if (cnt == q) + { + break; + } + } + if (cnt == q) + { + break; + } + } + + if (cnt == q) + { + printf("%c%c%c%c%c\n", p1, p2, p3, p4, p5); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/12620.cpp b/12620.cpp new file mode 100644 index 0000000..ada51ae --- /dev/null +++ b/12620.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int f1 = 1, f2 = 1, f3; +int i, j; +int A[305] = {0, 1}, cycle; +int C[305]; +void build() +{ + for (i = 2;; i++) + { + if (f1 == 1 && f2 == 1 && i != 2) + break; + A[i] = f2; + f3 = f1 + f2; + f1 = f2, f2 = f3 % 100; + } + cycle = i - 2; + for (i = 1; i <= cycle; i++) + C[i] = C[i - 1] + A[i]; +} +long long get(long long n) +{ + return n / cycle * C[cycle] + C[n % cycle]; +} +int main() +{ + build(); + int testcase; + long long L, R; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld %lld", &L, &R); + printf("%lld\n", get(R) - get(L - 1)); + } + return 0; +} diff --git a/12621.cpp b/12621.cpp new file mode 100644 index 0000000..50755b7 --- /dev/null +++ b/12621.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + int n, p, x; + int i, j; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &p); + n = n / 10; + int dp[1000] = {}; + dp[0] = 1; + for (i = 0; i < p; i++) + { + scanf("%d", &x), x /= 10; + for (j = 999 - x; j >= 0; j--) + { + if (dp[j]) + dp[j + x] = 1; + } + } + int ret = n; + while (ret < 1000 && dp[ret] == 0) + ret++; + if (ret == 1000) + puts("NO SOLUTION"); + else + printf("%d\n", ret * 10); + } + return 0; +} diff --git a/12622.cpp b/12622.cpp new file mode 100644 index 0000000..7d36095 --- /dev/null +++ b/12622.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) + +typedef long long ll; + +int go(int n, int b) +{ + int ret = 0; + while (n) + { + if (n % b == 4) + ret++; + n /= b; + } + return ret; +} + +int main() +{ + int T, P; + scanf("%d", &T); + while (T--) + { + scanf("%d", &P); + int C = P; + if (P < 0) + P = -P; + int fou = 0, temp, res; + int q = max(1000, (int)sqrt(P)) + 10; + fr(i, 5, q) + { + temp = go(P, i); + if (temp > fou) + { + fou = temp; + res = i; + } + } + if (P <= 1000 || fou > 1) + { + if (!fou) + printf("%d is infuriable.\n", C); + else + printf("%d %d %d\n", C, fou, res); + } + else + { + if (P % 4 == 0) + printf("%d 2 %d\n", C, (P - 4) / 4); + else if (fou == 1) + printf("%d %d %d\n", C, fou, res); + } + } + return 0; +} diff --git a/12623.cpp b/12623.cpp new file mode 100644 index 0000000..e15151e --- /dev/null +++ b/12623.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) +#define mp make_pair +#define F first +#define S second +#define inf 0x3f3f3f3f + +typedef pair pii; + +int N, P; +int n, c, a; +int size[30]; +pii adj[30][30]; +int mark[30], TOTAL, resp[30], temp[30]; + +void bt(int v, int cost) +{ + if (v == N) + { + TOTAL = cost; + rp(i, N) resp[i] = temp[i]; + return; + } + + rp(i, size[v]) + { + if (!mark[adj[v][i].F] && cost + adj[v][i].S < TOTAL) + { + mark[adj[v][i].F] = 1; + temp[v] = adj[v][i].F; + bt(v + 1, cost + adj[v][i].S); + mark[adj[v][i].F] = 0; + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &P); + memset(size, 0, sizeof size); + rp(i, P) + { + scanf("%d%d", &c, &n); + rp(j, n) + { + scanf("%d", &a); + adj[a][size[a]++] = mp(i, c); + } + } + memset(mark, 0, sizeof mark); + TOTAL = inf; + bt(0, 0); + if (TOTAL == inf) + printf("No solution.\n"); + else + { + printf("%d", TOTAL); + rp(i, N) printf(" %d", resp[i]); + printf("\n"); + } + } + return 0; +} diff --git a/12625.cpp b/12625.cpp new file mode 100644 index 0000000..dd577eb --- /dev/null +++ b/12625.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) +#define add(a, b, c) to[z] = b, cost[z] = c, ant[z] = adj[a], adj[a] = z++ +#define maxn 300 +#define maxm 300000 + +int to[maxm], cost[maxm], ant[maxm], adj[maxn], gas[maxn], totalcost[maxn][105]; +int z, N, R, Q, a, b, w; +char gb[10]; + +struct st +{ + int no, driven, cost; + st() {} + st(int no, int driven, int cost) : no(no), driven(driven), cost(cost) {} + + bool operator<(const st &lhs) const + { + return cost > lhs.cost; + } +} aux; + +priority_queue pq; + +int main() +{ + int cas = 1; + int T; + scanf("%d", &T); + while (T--) + { + memset(adj, -1, sizeof adj); + z = 0; + scanf("%d%d%d", &N, &R, &Q); + rp(i, N) + { + scanf("%s", gb); + gas[i] = (gb[0] == 'G'); + } + rp(i, R) + { + scanf("%d%d%d", &a, &b, &w); + if (w <= 100) + add(a - 1, b - 1, w), add(b - 1, a - 1, w); + } + printf("CASE %d\n", cas++); + rp(i, Q) + { + scanf("%d%d", &a, &b); + a--, b--; + while (!pq.empty()) + pq.pop(); + pq.push(st(a, 0, 0)); + memset(totalcost, 0x3f, sizeof totalcost); + totalcost[a][0] = 0; + while (!pq.empty()) + { + aux = pq.top(); + pq.pop(); + if (aux.no == b) + { + printf("%d\n", aux.cost); + goto fim; + } + int newdriven = (gas[aux.no] ? 0 : aux.driven); + for (int i = adj[aux.no]; ~i; i = ant[i]) + { + if (newdriven + cost[i] <= 100 && aux.cost + cost[i] < totalcost[to[i]][newdriven + cost[i]]) + { + totalcost[to[i]][newdriven + cost[i]] = aux.cost + cost[i]; + pq.push(st(to[i], newdriven + cost[i], aux.cost + cost[i])); + } + } + } + printf("NO GAS PATH\n"); + fim:; + } + } + return 0; +} diff --git a/12626.cpp b/12626.cpp new file mode 100644 index 0000000..62a514c --- /dev/null +++ b/12626.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int t; + char s[605]; + scanf("%d", &t); + while (t--) + { + scanf("%s", s); + int ascii[128] = {}; + for (int i = 0; s[i]; i++) + ascii[s[i]]++; + int ret = 0xffff; + ret = min(ret, ascii['M'] / 1); + ret = min(ret, ascii['A'] / 3); + ret = min(ret, ascii['R'] / 2); + ret = min(ret, ascii['G'] / 1); + ret = min(ret, ascii['T'] / 1); + ret = min(ret, ascii['I'] / 1); + printf("%d\n", ret); + } + return 0; +} diff --git a/12627.cpp b/12627.cpp new file mode 100644 index 0000000..4d81afa --- /dev/null +++ b/12627.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +long long a[60]; + +long long get(long long n, int K) +{ + long long ans = 0, i; + for (i = 30; i >= 0; i--) + if (n & (1 << i)) + { + n -= (1 << i); + ans += a[i] * (1 << (K - i)); + K--; + } + return ans; +} + +int main() +{ + int A, B, K, i; + a[0] = 1; + for (i = 1; i <= 30; i++) + a[i] = a[i - 1] * 3; + int T, tc; + scanf("%d", &T); + for (tc = 1; tc <= T; tc++) + { + scanf("%d%d%d", &K, &A, &B); + printf("Case %d: %lld\n", tc, get(B, K) - get(A - 1, K)); + } + return 0; +} diff --git a/12629.cpp b/12629.cpp new file mode 100644 index 0000000..a201e1d --- /dev/null +++ b/12629.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int table[2][2] = {0, 0, 0, 1}; +int sg[105][105], a[105][105], b[105][105], num[1 << 15], tmp[105], on[105]; + +int Nim_Mult(int x, int y) +{ + if (x < y) + return Nim_Mult(y, x); + if (x < 2) + return table[x][y]; + int a = 0; + while (x >= (1 << (1 << (a + 1)))) + a++; + int m = 1 << (1 << a); + int p = x / m, q = x % m, s = y / m, t = y % m; + int c1 = Nim_Mult(p, s), c2 = Nim_Mult(p, t) ^ Nim_Mult(q, s), c3 = Nim_Mult(q, t); + return m * (c1 ^ c2) ^ c3 ^ Nim_Mult(m / 2, c1); +} + +void init() +{ + int i, j; + for (i = 1; i <= 100; i++) + for (j = 1; j <= 100; j++) + sg[i][j] = Nim_Mult(i & -i, j & -j); +} + +int main() +{ + int n, m, c, i, j, k, s; + init(); + while (scanf("%d%d", &n, &m) != EOF) + { + s = c = 0; + memset(on, 0, sizeof(on)); + for (i = 1; i <= n; i++) + for (j = 1; j <= m; j++) + { + scanf("%d", &a[i][j]); + if (a[i][j]) + s ^= sg[n - i + 1][m - j + 1]; + on[i] = 1; + } + if (!s) + { + puts("No"); + continue; + } + for (j = 1; j <= m; j++) + for (i = 1; i <= n; i++) + b[i][j] = b[i - 1][j] ^ sg[n - i + 1][m - j + 1]; + int x1 = n, y1 = m, x2, y2; + for (i = 1; i <= n; i++) + if (on[i]) + for (j = i; j <= n; j++) + { + for (k = 1; k <= m; k++) + tmp[k] = b[i - 1][k] ^ b[j][k]; + tmp[m + 1] = 0; + for (k = m; k > 0; k--) + tmp[k] ^= tmp[k + 1]; + num[s] = 1; + for (k = m; k > 0; k--) + { + if (a[i][k] && num[tmp[k]]) + { + c += num[tmp[k]]; + if (i < x1 || (i == x1 && k < y1)) + x1 = i, y1 = k; + } + num[tmp[k] ^ s]++; + } + num[s]--; + for (k = 1; k <= m; k++) + num[tmp[k] ^ s]--; + } + memset(b, 0, sizeof(b)); + for (i = n; i > 0; i--) + for (j = m; j > 0; j--) + b[i][j] = sg[n - i + 1][m - j + 1] ^ b[i + 1][j] ^ b[i][j + 1] ^ b[i + 1][j + 1]; + for (i = x1; i <= n; i++) + { + for (j = y1; j <= m; j++) + { + if ((b[x1][y1] ^ b[x1][j + 1] ^ b[i + 1][y1] ^ b[i + 1][j + 1]) == s) + { + x2 = i, y2 = j; + break; + } + } + if (j <= m) + break; + } + printf("%d %d %d %d %d\n", c, x1, y1, x2, y2); + } + return 0; +} diff --git a/1263.cpp b/1263.cpp new file mode 100644 index 0000000..43a1fa9 --- /dev/null +++ b/1263.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +//1263 +//Mines +//Graphs;Topological Sorting +#define MAX 2001 + +int n; +bool V[MAX], G[MAX][MAX]; +int X[MAX], Y[MAX], D[MAX], O[MAX], npv; + +inline int abs(int a) +{ + return a > 0 ? a : -a; +} + +void dfs(int v, bool sort) +{ + V[v] = true; + for (int i = 1; i <= n; i++) + if (G[v][i] && !V[i]) + { + dfs(i, sort); + } + if (sort) + { + O[++npv] = v; + } +} + +int main() +{ + int t; + cin >> t; + t = 0; + while (cin >> n) + { + memset(G, 0, sizeof(G)); + for (int i = 1; i <= n; i++) + { + cin >> X[i] >> Y[i] >> D[i]; + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + int r = D[i] / 2; + if (abs(X[j] - X[i]) <= r && abs(Y[j] - Y[i]) <= r && i != j) + { + G[i][j] = true; + } + } + } + npv = 0; + memset(V, 0, sizeof(V)); + memset(O, 0, sizeof(O)); + for (int i = 1; i <= n; i++) + if (!V[i]) + { + dfs(i, true); + } + memset(V, 0, sizeof(V)); + int comp = 0; + for (int i = n; i > 0; i--) + if (!V[O[i]]) + { + comp++; + dfs(O[i], false); + } + cout << comp << endl; + } + return 0; +} diff --git a/12630.cpp b/12630.cpp new file mode 100644 index 0000000..da69350 --- /dev/null +++ b/12630.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + int n, c; + while (scanf("%d", &n) == 1, n) + { + printf("%d\n", ((n+2)*(n+1)*n*(n-1))/24); + } + return 0; +} diff --git a/12631.cpp b/12631.cpp new file mode 100644 index 0000000..52624f7 --- /dev/null +++ b/12631.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < (n); ++i) +#define TR(i, x) for (typeof(x.begin()) i = x.begin(); i != x.end(); i++) +#define PB push_back +#define MP make_pair +typedef long long ll; + +const int MOD = 1000000007; +int counter[100005]; +int p[100005], mutiple[100005]; +int inex_flag[100005], firstFac[100005]; +int primes[100005]; +bool isprime[100005]; +ll cnk[100005], pow2[100005]; +int inv[100005]; + +void GetPrime(int n) +{ + memset(isprime, 1, sizeof(isprime)); + int num = 0, i, j; + for (i = 2; i < n; ++i) + { + if (isprime[i]) + primes[num++] = i; + for (j = 0; (j < num && i * primes[j] < n); ++j) + { + isprime[i * primes[j]] = 0; + firstFac[i * primes[j]] = primes[j]; + if (i % primes[j] == 0) + break; + } + } +} +vector> vec; +void prepareVec() +{ + for (int i = 1; i < 100005; ++i) + { + int x = i; + int last = -1; + bool repeatFac = false; + int cnt = 0; + while (x > 1) + { + int fac = isprime[x] ? x : firstFac[x]; + if (fac == last) + { + repeatFac = true; + break; + } + x /= fac; + last = fac; + cnt++; + } + if (!repeatFac) + { + vec.push_back(make_pair(i, (cnt & 1) ? -1 : 1)); + } + } + //REP(i, 10) + //cout << vec[i].first << " " << vec[i].second << endl; +} +void preparePow() +{ + pow2[0] = 1; + for (int i = 1; i < 100005; ++i) + pow2[i] = pow2[i - 1] * 2 % MOD; +} +void prepareInv() +{ + inv[1] = 1; + for (int i = 2; i < 100005; i++) + inv[i] = (ll)(MOD - MOD / i) * inv[(int)(MOD % i)] % MOD; +} +int n, k; +ll C; + +int main() +{ + GetPrime(100005); + prepareVec(); + preparePow(); + prepareInv(); + + int test; + cin >> test; + for (int cas = 1; cas <= test; ++cas) + { + int a, b, c; + cin >> n >> C >> k; + cin >> a >> b >> c; + + memset(cnk, 0, sizeof(cnk)); + for (int i = k; i < 100005; ++i) + { + if (i == k) + cnk[i] = 1; + else + cnk[i] = (ll)cnk[i - 1] * i % MOD * inv[i - k] % MOD; + } + + p[0] = a; + p[1] = b; + for (int i = 2; i < n; ++i) + { + p[i] = 1 + (a * (long long)p[i - 2] + b * (long long)p[i - 1] + c) % 100000; + } + memset(counter, 0, sizeof(counter)); + REP(i, n) + counter[p[i]]++; + + int maxn = 0; + + REP(i, n) + maxn = max(maxn, p[i]); + + memset(mutiple, 0, sizeof(mutiple)); + for (int i = 1; i <= maxn; ++i) + { + for (int j = i; j <= maxn; j += i) + { + mutiple[i] += counter[j]; + } + } + + int ans2 = 0, ansk = 0; + for (int d = 1; d <= maxn; ++d) + { + int cnt2 = 0; + int cntk = 0; + for (int i = 0; i < vec.size(); ++i) + { + int x = d * vec[i].first; + if (x > maxn) + break; + cnt2 = (cnt2 + (pow2[mutiple[x]] - 1ll) * vec[i].second) % MOD; + cntk = (cntk + cnk[mutiple[x]] * (ll)vec[i].second) % MOD; + } + ans2 = (ans2 + cnt2 * (ll)(C / d % MOD) % MOD) % MOD; + ansk = (ansk + cntk * (ll)(C / d % MOD) % MOD) % MOD; + } + ans2 = (ans2 + MOD) % MOD; + ansk = (ansk + MOD) % MOD; + printf("Case %d: %d %d\n", cas, ans2, ansk); + } + return 0; +} diff --git a/12632.cpp b/12632.cpp new file mode 100644 index 0000000..89d9bf1 --- /dev/null +++ b/12632.cpp @@ -0,0 +1,190 @@ +#include + +using namespace std; + +#define rep(x, n) for (int x = 0; x < n; ++x) +#define print(x) cout << x << endl +#define dbg(x) cerr << #x << " == " << x << endl +#define _ << " , " << +#define mp make_pair +#define x first +#define y second + +template +void pv(T a, T b) +{ + for (T i = a; i != b; ++i) + cout << *i << " "; + cout << endl; +} + +typedef long long ll; +typedef pair pii; + +const long long lim = 1e16; + +void mult(long long &x, long long y) +{ + if (x > lim / y + 1) + throw "bad"; + else + x *= y; + if (x > lim) + throw "bad"; +} + +long long gcd(long long A, long long B) +{ + while (A) + { + B %= A; + swap(A, B); + } + return B; +} + +int greatestFactor(int a) +{ + if (a <= 3) + return a; + while ((a & 1) == 0) + { + a >>= 1; + if (a < 2 * 2) + return a; + } + while (a % 3 == 0) + { + a /= 3; + if (a < 3 * 3) + return a; + } + int x = 5; + int s = 5 * 5; + while (a >= s && x <= 46337) + { + while (a % x == 0) + { + a /= x; + if (a < s) + return a; + } + x += 2; + s = x * x; + while (a % x == 0) + { + a /= x; + if (a < s) + return a; + } + x += 4; + s = x * x; + } + return a; +} + +void solve(long long A, long long B, long long &I, long long &J) +{ + if (A == 1) + throw "bad"; + int p, q = 0; + set s; + for (;;) + { + if (A == 1 && B == 1) + break; + if (q > 0) + { + p = 0; + if (A % q == 0 || B % q == 0) + p = greatestFactor(q); + if (A % q == 0) + p = max(p, greatestFactor(A / q)); + else + p = max(p, greatestFactor(A)); + if (B % q == 0) + p = max(p, greatestFactor(B / q)); + else + p = max(p, greatestFactor(B)); + } + else + { + p = max(greatestFactor(A), greatestFactor(B)); + } + s.insert(p), q = 0; + if (A % p == 0) + { + assert(B % p); + int t = 0; + do + t++, A /= p; + while (A % p == 0); + if (t % 2 == 0) + { + t++; + mult(J, p); + } + else + { + long long g = gcd(A, p - 1); + A /= g; + q = g = (p - 1) / g; + B *= q; + } + t = (t + 1) / 2; + rep(i, t) mult(I, p); + } + if (B % p == 0) + { + assert(A % p); + int t = 0; + do + t++, B /= p; + while (B % p == 0); + if (t % 2 == 0) + { + t++; + mult(I, p); + } + else + { + long long g = gcd(B, p - 1); + B /= g; + q = g = (p - 1) / g; + A *= q; + } + t = (t + 1) / 2; + rep(i, t) mult(J, p); + } + } + if (I == 1 || J == 1) + { + for (int q = 2;; q++) + if (greatestFactor(q) == q && s.insert(q).second) + { + mult(I, q), mult(J, q); + return; + } + } +} + +int main() +{ + for (long long M;;) + { + cin >> M; + if (M == 0) + break; + try + { + long long I = 1, J = 1; + solve(M, 1, I, J); + printf("%lld %lld %lld\n", M, I, J); + } + catch (char const *exception) + { + printf("%lld No solution\n", M); + } + } + return 0; +} diff --git a/12633.cpp b/12633.cpp new file mode 100644 index 0000000..d2286ee --- /dev/null +++ b/12633.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double pi = acos(-1.); + +#define cp complex + +int dcmp(double a) +{ + if (a < -eps) + { + return -1; + } + return a > eps; +} + +void fft(cp *a, int n, int rev) +{ + int i, j, k, m; + for (i = 1, j = 0; i < n; i++) + { + for (k = n >> 1; k > (j ^= k); k >>= 1) + ; + if (i < j) + { + swap(a[i], a[j]); + } + } + for (m = 2; m <= n; m <<= 1) + { + cp wm(cos(2 * pi * rev / m), sin(2 * pi * rev / m)); + for (i = 0; i < n; i += m) + { + cp w(1, 0); + for (j = i; j < i + m / 2; j++) + { + cp t = w * a[j + m / 2]; + a[j + m / 2] = a[j] - t; + a[j] = a[j] + t; + w = w * wm; + } + } + } +} + +cp ya[510000], yb[510000]; + +void PolyMulti(int *a, int na, int *b, int nb, int *c) +{ + int i, j, n = max(na, nb); + for (n = 1; n < na + nb; n <<= 1) + ; + for (i = 0; i < n; ++i) + { + ya[i] = (i < na) ? a[i] : 0; + } + fft(ya, n, 1); + for (i = 0; i < n; ++i) + { + yb[i] = (i < nb) ? b[i] : 0; + } + fft(yb, n, 1); + for (i = 0; i < n; ++i) + { + ya[i] = ya[i] * yb[i]; + } + fft(ya, n, -1); + for (i = 0; i < n; ++i) + { + c[i] = (ya[i].real() / n + 0.01); + } +} + +int a[110000], b[110000], c[210000]; +bool h[51000]; + +int main() +{ + int T, cases = 1; + int i, j, k, R, C, N, s1, s2; + long long ans; + int x, y, LF; + scanf("%d", &T); + while (T--) + { + scanf("%d %d %d", &R, &C, &N); + ans = (long long)R * C; + memset(a, 0, sizeof(a)); + memset(b, 0, sizeof(b)); + memset(h, 0, sizeof(h)); + LF = R - 1; + for (i = 0; i < N; ++i) + { + scanf("%d %d", &y, &x); + a[x - y + LF] = 1; + b[C - x] = 1; + h[y] = 1; + } + for (i = s1 = 0; i < C; ++i) + { + s1 += b[i]; + } + for (i = s2 = 0; i < C; ++i) + { + s2 += a[i + LF]; + } + PolyMulti(a, LF + C, b, C, c); + for (i = 1; i <= R; ++i) + { + if (h[i]) + { + ans -= C; + } + else + { + ans -= s1 + s2 - c[-i + LF + C]; + } + if (a[C - i + LF]) + { + s2--; + } + if (a[0 - i + LF]) + { + s2++; + } + } + printf("Case %d: %lld\n", cases++, ans); + } + return 0; +} diff --git a/12634.cpp b/12634.cpp new file mode 100644 index 0000000..c561983 --- /dev/null +++ b/12634.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +struct Node +{ + int v[512], v_size; + int v_sum; // sigma(abs(v[i])) + int v_pair;// this problem matched. + int v_girl;// unmatch girl. + Node *next; + Node() + { + v_size = v_sum = v_pair = v_girl = 0; + next = NULL; + } +}; +int P_SIZE = 250; +void freeList(Node *head) +{ + Node *idx = head, *pre = NULL; + while (idx != NULL) + { + pre = idx, idx = idx->next; + delete pre; + } +} +int calcList(Node *head) +{ + int mpair = 0; + int boy = 0; + Node *idx = head, *pre = NULL; + while (idx != NULL) + { + mpair += idx->v_pair; + mpair += boy < idx->v_girl ? boy : idx->v_girl; + boy -= idx->v_girl; + if (boy < 0) + boy = 0; + boy += idx->v_sum - idx->v_pair * 2 - idx->v_girl; + pre = idx, idx = idx->next; + } + return mpair; +} +void reCalculate(Node *p) +{ + int sum = 0, mpair = 0, boy = 0, girl = 0; + for (int i = 0; i < p->v_size; i++) + { + if (p->v[i] > 0) + { + boy += p->v[i]; + } + else + { + mpair += boy < -(p->v[i]) ? boy : -(p->v[i]); + girl += boy + p->v[i] < 0 ? -(boy + p->v[i]) : 0; + boy = boy + p->v[i] > 0 ? boy + p->v[i] : 0; + } + sum += abs(p->v[i]); + } + p->v_sum = sum, p->v_pair = mpair, p->v_girl = girl; +} +Node *insertNode(Node *head, int where, int val) +{ + if (where == 0) + { + Node *p = new Node(); + p->v[0] = val, p->v_size = 1; + p->next = head; + reCalculate(p); + return p; + } + int sum = 0; + Node *idx = head, *pre = NULL; + while (idx != NULL) + { + sum += idx->v_sum; + pre = idx, idx = idx->next; + if (sum >= where) + break; + } + if (sum == where) + { + Node *p = new Node(); + p->next = pre->next, pre->next = p; + p->v[0] = val, p->v_size = 1; + reCalculate(p); + return head; + } + sum -= pre->v_sum; + for (int i = 0; i < pre->v_size; i++) + { + if (sum + abs(pre->v[i]) >= where) + { + Node *p = new Node(); + int div = i + 1; + int diff = sum + abs(pre->v[i]) - where; + p->next = pre->next, pre->next = p; + p->v[p->v_size++] = val; + if (diff > 0) + { + if (pre->v[i] > 0) + { + p->v[p->v_size++] = diff; + pre->v[i] -= diff; + } + else + { + p->v[p->v_size++] = -diff; + pre->v[i] += diff; + } + } + for (int j = div; j < pre->v_size; j++) + p->v[p->v_size++] = pre->v[j]; + pre->v_size = div; + reCalculate(p); + reCalculate(pre); + break; + } + sum += abs(pre->v[i]); + } + return head; +} +void maintainList(Node *head) +{ + Node *idx = head, *p; + while (idx != NULL && idx->next != NULL) + { + if (idx->v_size + idx->next->v_size <= 2 * P_SIZE) + {// merge + p = idx->next; + for (int i = idx->v_size, j = 0; j < p->v_size; i++, j++) + idx->v[i] = p->v[j]; + idx->v_size += p->v_size; + idx->next = p->next; + delete p; + reCalculate(idx); + } + idx = idx->next; + } +} +void printList(Node *head) +{ + Node *idx = head; + int i, j = 0; + while (idx != NULL) + { + printf("%d [%d, %d]: ", ++j, idx->v_pair, idx->v_girl); + for (i = 0; i < idx->v_size; i++) + printf("%d ", idx->v[i]); + puts(""); + idx = idx->next; + } + puts("===="); +} +int main() +{ + int testcase, cases = 0, N; + scanf("%d", &testcase); + while (testcase--) + { + printf("Case %d:\n", ++cases); + scanf("%d", &N); + Node *head = NULL; + head = new Node(); + head->v[0] = 1, head->v_size = 1; + head->v_sum = 1, head->v_pair = 0; + for (int i = 0; i < N; i++) + { + int girl, where, cnt; + scanf("%d %d %d", &girl, &where, &cnt); + if (girl) + cnt = -cnt; + head = insertNode(head, where, cnt); + maintainList(head); + //printList(head); + printf("%d\n", calcList(head)); + } + freeList(head); + } + return 0; +} +/* +2 +3 +1 1 4 +0 3 3 +0 0 2 +1 +1 0 1 +*/ diff --git a/12635.cpp b/12635.cpp new file mode 100644 index 0000000..d6fc542 --- /dev/null +++ b/12635.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +struct NN +{ + int s, t, n; + NN(int ss, int tt, int nn) + { + s = ss, t = tt, n = nn; + } +}; + +queue q; +int s[11000]; + +int is(int i, int j) +{ + if (i == 0) + { + return s[j] % 2 && s[j] == s[j - 1]; + } + return (s[j] - s[i - 1]) % 2 && s[j] == s[j - 1] && s[i] == s[i - 1]; +} + +void upd(int s, int t, int k) +{ + q.push(NN(s, t, k)); +} +int bfs(int k) +{ + int ans = 0; + int i, j, u; + for (i = 0; i < k; i++) + q.push(NN(i, k, 0)); + while (!q.empty()) + { + NN n = q.front(); + q.pop(); + + if (n.n == 0) + { + int l = n.t - n.s; + if (l && n.t + l <= 10000) + q.push(NN(n.s, n.t + l, 1)); + continue; + } + if (is(n.s, n.t) && n.n <= 7) + ans++; + else + continue; + int l = n.t - n.s; + if (n.t + l <= 10000) + upd(n.s, n.t + l, n.n + 1); + if (n.s - l >= 0) + upd(n.s - l, n.t, n.n + 1); + } + return ans; +} +inline void scan(int &u) +{ + char c; + while (c = getchar(), c < '0' || c > '9') + ; + u = c - '0'; + while (c = getchar(), c <= '9' && c >= '0') + u = u * 10 + c - '0'; +} + +int main() +{ + int n, i, j, k; + int t, ii = 0; + + scanf("%d", &t); + while (t--) + { + memset(s, 0, sizeof(s)); + while (scan(i), i) + s[i]++; + for (i = 1; i <= 10000; i++) + s[i] += s[i - 1]; + scan(i); + printf("%d\n", bfs(i)); + } + return 0; +} diff --git a/12636.cpp b/12636.cpp new file mode 100644 index 0000000..56637e3 --- /dev/null +++ b/12636.cpp @@ -0,0 +1,193 @@ +#include + +using namespace std; + +#define rep(x, n) for (int x = 0; x < n; ++x) +#define print(x) cout << x << endl +#define dbg(x) cerr << #x << " == " << x << endl +#define _ << " , " << +#define mp make_pair +#define x first +#define y second + +template +void pv(T a, T b) +{ + for (T i = a; i != b; ++i) + { + cout << *i << " "; + } + printf("\n"); +} + +typedef unsigned long long Int; +typedef pair pt; + +Int gcd(Int A, Int B) +{ + while (A) + { + B %= A; + swap(A, B); + } + return B; +} + +Int root(Int A) +{ + Int r = sqrt(double(A)); + if (r * r != A) + { + r = 0; + } + return r; +} + +Int solve(Int AB, Int BC, Int AC) +{ + Int g = gcd(AB, AC); + AB /= g, AC /= g; + if (BC % AC) + { + return 0; + } + else + { + BC /= AC; + } + Int x = gcd(AB, BC); + return x * root(AB / x) * root(BC / x); +} + +bool valid(vector &a) +{ + sort(a.begin(), a.end()); + if (a[0] <= 1) + { + return false; + } + if (a.back() > 1e9) + { + return false; + } + rep(i, a.size() - 1) + { + if (a[i] == a[i + 1]) + { + return false; + } + } + return true; +} + +bool pop(map &M, Int x) +{ + if (M.count(x) == 0) + { + return false; + } + else + { + M[x]--; + } + if (M[x] <= 0) + { + M.erase(x); + } + return true; +} + +Int top(map &M) +{ + return M.begin()->first; +} + +int main() +{ + cin.sync_with_stdio(false); + int T; + cin >> T; + for (int testcase = 1; testcase <= T; testcase++) + { + int n; + cin >> n; + int m = n * (n - 1) / 2; + vector b(m), a(n, 2e9); + rep(i, m) cin >> b[i]; + sort(b.begin(), b.end()); + if (n == 3) + { + a[0] = solve(b[0], b[1], b[2]); + a[1] = solve(b[1], b[2], b[0]); + a[2] = solve(b[2], b[0], b[1]); + valid(a); + } + else + { + Int AB = b[0]; + Int AC = b[1]; + int lim = min(m, n + 2); + for (int candidate = 2; candidate < lim; candidate++) + { + Int BC = b[candidate]; + Int A = solve(AB, AC, BC); + Int B = solve(AB, BC, AC); + Int C = solve(AC, BC, AB); + if (A > B) + { + swap(A, B); + } + if (A > C) + { + swap(A, C); + } + if (B > C) + { + swap(B, C); + } + if (A <= 1) + { + continue; + } + int ok = true; + map M; + rep(i, m) M[b[i]]++; + pop(M, AB), pop(M, AC), pop(M, BC); + vector aa(n); + aa[0] = A; + aa[1] = B; + aa[2] = C; + for (int p = 3; p < n; p++) + { + Int X = top(M); + if (X % A) + { + ok = false; + goto fail; + } + aa[p] = X / A; + for (int i = 0; i < p; i++) + { + if (!pop(M, aa[i] * aa[p])) + { + ok = false; + goto fail; + } + } + } + fail: + if (ok && valid(aa) && aa < a) + { + a = aa; + } + } + } + printf("Case %d:", testcase); + rep(i, n) + { + printf(" %d", int(a[i])); + } + printf("\n"); + } + return 0; +} diff --git a/12637.cpp b/12637.cpp new file mode 100644 index 0000000..9de51a8 --- /dev/null +++ b/12637.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +struct KM +{ + int W[105][105], N; + int mx[105], my[105];// match arr + int lx[105], ly[105];// label arr + int x[105], y[105]; // used arr + int hungary(int nd) + { + int i; + x[nd] = 1; + for (i = 0; i < N; i++) + { + if (y[i] == 0 && W[nd][i] == lx[nd] + ly[i]) + { + y[i] = 1; + if (my[i] == -1 || hungary(my[i])) + { + my[i] = nd; + return 1; + } + } + } + return 0; + } + int run() + { + int i, j, k, d; + memset(mx, -1, sizeof(mx)); + memset(my, -1, sizeof(my)); + for (int i = 0; i < N; i++) + lx[i] = -0x3f3f3f3f, ly[i] = 0; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + lx[i] = lx[i] > W[i][j] ? lx[i] : W[i][j]; + for (i = 0; i < N; i++) + { + while (1) + { + memset(x, 0, sizeof(x)); + memset(y, 0, sizeof(y)); + if (hungary(i)) + break; + d = 0x3f3f3f3f; + for (j = 0; j < N; j++) + { + if (x[j]) + { + for (k = 0; k < N; k++) + if (!y[k]) + d = d < lx[j] + ly[k] - W[j][k] ? d : lx[j] + ly[k] - W[j][k]; + } + } + if (d == 0x3f3f3f3f) + break; + for (j = 0; j < N; j++) + { + if (x[j]) + lx[j] -= d; + if (y[j]) + ly[j] += d; + } + } + } + int res = 0; + for (i = 0; i < N; i++) + { + if (my[i] != -1) + res += W[my[i]][i]; + } + return res; + } +} km; + +int dist(int x1, int y1, int x2, int y2) +{ + return abs(x1 - x2) + abs(y1 - y2); +} +int main() +{ + int N, R; + int x[256], y[256], cost[128][128]; + while (scanf("%d %d", &R, &N) == 2) + { + for (int i = 0; i < R; i++) + scanf("%d %d", &x[i], &y[i]); + for (int i = 0; i < N; i++) + scanf("%d %d", &x[i + R], &y[i + R]); + + km.N = R; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < N; j++) + { + cost[i][j] = dist(x[i], y[i], x[j + R], y[j + R]); + km.W[i][j] = -cost[i][j]; + } + for (int j = N; j < R; j++) + km.W[i][j] = 0; + } + int ret = km.run(); + printf("%d\n", -ret); + } + return 0; +} +/* +2 2 +1 5 +2 1 +4 2 +4 3 +3 2 +2 1 +4 3 +7 4 +4 5 +5 -1 +*/ diff --git a/12638.cpp b/12638.cpp new file mode 100644 index 0000000..7e7983a --- /dev/null +++ b/12638.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +const int N = 10050; +int len; +char s[N]; + +void solve() +{ + int sum = 1, ans = 0; + for (int i = 0; i < len; ++i) + { + if (s[i] == '0' && sum < 1) + { + ++sum; + } + if (s[i] == '1') + { + if (sum >= 1) + { + sum = -1; + } + else + { + --sum; + } + } + if (sum >= 1) + { + ++ans; + } + } + if (sum < 1) + { + ans = 0; + } + printf("%d\n", ans); +} + +int main() +{ + while (scanf("%s", s) != -1) + { + len = strlen(s); + for (int i = 0; i < len; ++i) + if (s[i] == '?') + { + s[i] = '0'; + } + solve(); + } + return 0; +} diff --git a/12639.cpp b/12639.cpp new file mode 100644 index 0000000..bc147d4 --- /dev/null +++ b/12639.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +struct Hex +{ + int v[6], m[7], rotate; + int read() + { + for (int i = 0; i < 6; i++) + { + if (scanf("%d", &v[i]) == 1) + ; + else + return 0; + } + return 1; + } + void normal() + { + int t[6], mnexp = 0; + for (int i = 0; i < 6; i++) + t[i] = v[i]; + for (int i = 1; i < 6; i++) + if (t[i] == 1) + mnexp = i; + for (int i = 0; i < 6; i++) + v[i] = t[(mnexp + i) % 6]; + for (int i = 0; i < 6; i++) + m[v[i]] = i; + } + int getPos(int x) + { + return m[x]; + } + int next() + { + return v[(rotate + 1) % 6]; + } + int prev() + { + return v[(rotate + 5) % 6]; + } + void println() + { + for (int i = 0; i < 6; i++) + printf("%d ", v[(rotate + i) % 6]); + puts(""); + } +}; +int used[7], path[7]; +Hex h[7]; +int dfs(int idx) +{ + if (idx == 7) + return 1; + // printf("%d\n", idx); + // for (int i = 0; i < idx; i++) { + // printf("%d : ", h[path[i]].rotate); + // h[path[i]].println(); + // } + // puts("--"); + // getchar(); + if (idx == 0) + { + for (int i = 0; i < 7; i++) + { + if (used[i] == 0) + { + used[i] = 1; + path[idx] = i, h[i].rotate = 0; + if (dfs(idx + 1)) + return 1; + used[i] = 0; + } + } + } + else + { + if (idx == 1) + { + for (int i = 0; i < 7; i++) + { + if (used[i] == 0) + { + used[i] = 1; + path[idx] = i, h[i].rotate = h[i].getPos(h[path[0]].v[idx - 1]); + if (dfs(idx + 1)) + return 1; + used[i] = 0; + } + } + } + else if (idx < 6) + { + for (int i = 0; i < 7; i++) + { + if (used[i] == 0) + { + used[i] = 1; + path[idx] = i, h[i].rotate = h[i].getPos(h[path[0]].v[idx - 1]); + if (h[path[idx - 1]].prev() == h[i].next()) + if (dfs(idx + 1)) + return 1; + used[i] = 0; + } + } + } + else + { + for (int i = 0; i < 7; i++) + { + if (used[i] == 0) + { + used[i] = 1; + path[idx] = i, h[i].rotate = h[i].getPos(h[path[0]].v[idx - 1]); + if (h[path[idx - 1]].prev() == h[i].next() && h[path[1]].next() == h[i].prev()) + if (dfs(idx + 1)) + return 1; + used[i] = 0; + } + } + } + } + return 0; +} +int main() +{ + while (true) + { + for (int i = 0; i < 7; i++) + { + if (!h[i].read()) + return 0; + h[i].normal(); + } + memset(used, 0, sizeof(used)); + int ret = dfs(0); + puts(ret ? "YES" : "NO"); + } + return 0; +} +/* +1 2 3 4 5 6 +1 3 6 5 2 4 +1 4 2 5 6 3 +1 5 2 3 6 4 +1 6 2 4 3 5 +1 6 2 4 5 3 +1 6 5 3 2 4 + +1 2 3 4 5 6 +1 2 3 4 5 6 +1 2 3 4 5 6 +1 2 3 4 5 6 +1 2 3 4 5 6 +1 2 3 4 5 6 +1 2 3 4 5 6 +*/ diff --git a/1264.cpp b/1264.cpp new file mode 100644 index 0000000..7d7df73 --- /dev/null +++ b/1264.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define ll long long +const int M = 9999991; +int c[20][20]; +class node +{ +public: + int key, num; + ll value; + node *left, *right; + node() + { + left = right = 0; + value = 1; + num = 0; + key = 0; + } + node(int el) + { + left = right = 0; + value = 1; + num = 0; + key = el; + } +}; +node *root; +void insert(int el) +{ + node *p = root, *pre = 0; + while (p) + { + pre = p; + if (p->key < el) + { + p = p->right; + } + else + { + p = p->left; + } + } + if (root == 0) + { + root = new node(el); + } + else if (pre->key < el) + { + p = pre->right = new node(el); + } + else + { + p = pre->left = new node(el); + } +} +void cal(node *p) +{ + if (!p->key) + { + return; + } + if (p->left == 0) + { + p->left = new node(); + } + if (p->right == 0) + { + p->right = new node(); + } + cal(p->left); + cal(p->right); + p->num = p->left->num + p->right->num + 1; + p->value = (p->left->value * p->right->value) % M * c[p->left->num + p->right->num][p->left->num] % M; +} +int main() +{ + int t, n, a, i, j; + c[0][0] = 1; + for (i = 1; i < 20; i++) + { + c[i][0] = c[i][i] = 1; + for (j = 1; j < i; j++) + { + c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; + } + } + scanf("%d", &t); + while (t--) + { + root = 0; + scanf("%d", &n); + while (n--) + { + scanf("%d", &a); + insert(a); + } + cal(root); + printf("%lld\n", root->value); + } + return 0; +} diff --git a/12640.cpp b/12640.cpp new file mode 100644 index 0000000..14e086e --- /dev/null +++ b/12640.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +bool debug = false; + +const int N = 100111; + +int dp[N], a[N]; + +char str[N * 10]; + +int main() +{ + int data, n = 0; + while (gets(str) != NULL) + { + int len = strlen(str); + str[len++] = ' '; + str[len] = 0; + if (debug) + { + cout << str << endl; + } + int l = 0, r = 0, n = 1; + for (int i = 0; str[i]; i++) + { + if (str[i] == ' ') + { + r = i - 1; + a[n] = 0; + bool tag = true; + for (int j = l; j <= r; j++) + { + if (str[j] == '-') + { + tag = false; + } + else + { + a[n] = a[n] * 10 + str[j] - '0'; + } + } + l = i + 1; + if (!tag) + { + a[n] = -a[n]; + } + n++; + } + } + if (debug) + { + cout << "debug" << endl; + } + if (debug) + { + for (int i = 1; i < n; i++) + { + cout << a[i] << " "; + } + cout << endl; + } + int res = 0; + dp[0] = 0; + for (int i = 1; i < n; i++) + { + dp[i] = max(a[i], dp[i - 1] + a[i]); + res = max(res, dp[i]); + } + if (debug) + { + for (int i = 1; i < n; i++) + { + cout << dp[i] << " "; + } + cout << endl; + } + cout << res << endl; + } +} diff --git a/12641.cpp b/12641.cpp new file mode 100644 index 0000000..7cd4daa --- /dev/null +++ b/12641.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +vector getLineToken(char s[]) +{ + stringstream sin(s); + string token; + vector ret; + while (sin >> token) + ret.push_back(token); + return ret; +} +int main() +{ + char s[32767]; + gets(s); + while (gets(s)) + { + map R[26 * 26]; + vector D, C; + D = getLineToken(s); + gets(s); + C = getLineToken(s); + for (int i = D.size() - 1; i >= 0; i--) + { + int len = D[i].length(); + int h1 = D[i][0] - 'a', h2 = D[i][len - 1] - 'a'; + string t = D[i]; + sort(t.begin(), t.end()); + R[h1 * 26 + h2][t] = i; + } + for (int i = 0; i < C.size(); i++) + { + int len = C[i].length(); + int h1 = C[i][0] - 'a', h2 = C[i][len - 1] - 'a'; + string t = C[i]; + sort(t.begin(), t.end()); + if (R[h1 * 26 + h2].find(t) != R[h1 * 26 + h2].end()) + printf("%s%c", D[R[h1 * 26 + h2][t]].c_str(), i == C.size() - 1 ? '\n' : ' '); + else + printf("%s%c", C[i].c_str(), i == C.size() - 1 ? '\n' : ' '); + } + } + return 0; +} +/* +2 +make me programming rich will +pagmrnmorig will mkae me rcih +dreaming drinaemg yeah +yaeh right keep drinaemg +*/ diff --git a/12642.cpp b/12642.cpp new file mode 100644 index 0000000..2e718ff --- /dev/null +++ b/12642.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + int n, x; + long long m; + while (scanf("%d %lld", &n, &m) == 2) + { + int f[2048], a[2048]; + for (int i = 1; i <= n; i++) + { + scanf("%d", &x); + f[i] = x; + } + + int visited[2048] = {}; + for (int i = 1; i <= n; i++) + { + if (visited[i]) + continue; + vector cycle; + for (int j = i; !visited[j]; j = f[j]) + { + visited[j] = 1; + cycle.push_back(j); + } + for (int j = 0; j < cycle.size(); j++) + { + int v = cycle[j]; + a[v] = cycle[(j + m % cycle.size()) % cycle.size()]; + } + } + for (int i = 1; i <= n; i++) + { + printf("%d%c", a[i], i == n ? '\n' : ' '); + } + } + return 0; +} +/* + 5 0 + 4 2 1 5 3 + 5 1 + 4 2 1 5 3 + 5 2 + 4 2 1 5 3 + */ \ No newline at end of file diff --git a/12643.cpp b/12643.cpp new file mode 100644 index 0000000..d93a162 --- /dev/null +++ b/12643.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int n, a, b; + while (scanf("%d%d%d", &n, &a, &b) != EOF) + { + int k = 1; + while (n--) + { + a = (a + 1) / 2; + b = (b + 1) / 2; + k++; + if (a == b) + { + break; + } + } + printf("%d\n", k - 1); + } + return 0; +} diff --git a/12644.cpp b/12644.cpp new file mode 100644 index 0000000..4c772e6 --- /dev/null +++ b/12644.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +// Maximum Cardinality Bipartite Matching (MCBM) Unweighted +// Augmenting Path + +struct Node +{ + int y; + int next; +} edge[100005]; +int e, head[512]; + +void addEdge(int x, int y) +{ + edge[e].y = y; + edge[e].next = head[x], head[x] = e++; +} + +int mx[512], my[512], used[512]; +int dfs(int now) +{ + int i, x; + for (i = head[now]; i != -1; i = edge[i].next) + { + x = edge[i].y; + if (!used[x]) + { + used[x] = 1; + if (my[x] == -1 || dfs(my[x])) + { + mx[now] = x, my[x] = now; + return 1; + } + } + } + return 0; +} + +int main() +{ + int V, C; + char s[512]; + while (scanf("%d%d", &V, &C) == 2) + { + e = 0; + memset(head, -1, sizeof(head)); + int Vcnt[512][26] = {}, Ccnt[512][26] = {}; + for (int i = 0; i < V; i++) + { + scanf("%s", s); + for (int j = 0; s[j]; j++) + Vcnt[i][s[j] - 'a']++; + } + for (int i = 0; i < C; i++) + { + scanf("%s", s); + for (int j = 0; s[j]; j++) + Ccnt[i][s[j] - 'a']++; + } + for (int i = 0; i < V; i++) + { + for (int j = 0; j < C; j++) + { + int f = 1; + for (int k = 0; k < 26; k++) + { + f &= Vcnt[i][k] >= Ccnt[j][k]; + } + if (f) + { + addEdge(i, j); + } + } + } + + memset(mx, -1, sizeof(mx)); + memset(my, -1, sizeof(my)); + int match = 0; + for (int i = 0; i < V; i++) + { + if (mx[i] == -1) + { + memset(used, 0, sizeof(used)); + if (dfs(i)) + { + match++; + } + } + } + printf("%d\n", match); + } + + return 0; +} diff --git a/12645.cpp b/12645.cpp new file mode 100644 index 0000000..b8af602 --- /dev/null +++ b/12645.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +// http://en.wikipedia.org/wiki/Topological_sort + +vector g[1024], topo; +int visited[1024]; + +void dfs(int u) +{ + visited[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (visited[v]) + continue; + dfs(v); + } + topo.push_back(u); +} +void dfs2(int u) +{ + visited[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (visited[v]) + continue; + dfs(v); + } +} + +int main() +{ + int n, m, x, y; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i <= n; i++) + g[i].clear(); + + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + if (y == 0) + continue; + g[x].push_back(y); + } + memset(visited, 0, sizeof(visited)); + topo.clear(); + for (int i = 0; i <= n; i++) + { + if (visited[i] == 0) + dfs(i); + } + reverse(topo.begin(), topo.end()); + memset(visited, 0, sizeof(visited)); + + int ret = 0; + for (int i = 0; i < topo.size(); i++) + { + int u = topo[i]; + if (visited[u]) + continue; + ret++; + dfs2(u); + } + printf("%d\n", ret - 1); + } + return 0; +} +/* + 4 5 + 0 1 + 1 2 + 2 1 + 0 4 + 3 4 + 4 2 + 3 1 + 2 1 + */ \ No newline at end of file diff --git a/12646.cpp b/12646.cpp new file mode 100644 index 0000000..1d09610 --- /dev/null +++ b/12646.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int A, B, C; + while (cin >> A >> B >> C) + { + if (A == B && B == C) + { + cout << "*" << endl; + } + else if (A == B) + { + cout << "C" << endl; + } + else if (A == C) + { + cout << "B" << endl; + } + else + { + cout << "A" << endl; + } + } + return 0; +} diff --git a/12647.cpp b/12647.cpp new file mode 100644 index 0000000..fbd85cd --- /dev/null +++ b/12647.cpp @@ -0,0 +1,295 @@ +#include + +using namespace std; + +#define For(i, n) for (int i = 0, _m = (n); i < _m; i++) + +typedef long long LL; + +struct Point +{ + int x, y; + Point(int x = 0, int y = 0) : x(x), y(y) {} + Point operator-(const Point &p) const + { + return Point(x - p.x, y - p.y); + } +}; + +LL cross(const Point &a, const Point &b) +{ + return LL(a.x) * b.y - LL(a.y) * b.x; +} + +inline bool between(int x, int a, int b) +{ + return a <= x and x <= b; +} +struct Seg +{ + Point p1, p2; + enum + { + UP, + HOR, + DOWN + } type; + void normalize() + { + if (p1.x > p2.x) + { + swap(p1, p2); + } + if (p1.y < p2.y) + { + type = UP; + } + else if (p1.y > p2.y) + { + type = DOWN; + } + else + { + type = HOR; + } + } +}; + +struct P2 +{ + int id; + enum + { + OPEN, + CLOSE + } side; + int x; + bool operator<(const P2 &p) const + { + return x < p.x; + } +}; + +const int N = 100102; +const int M = 1000102; + +P2 ps[N * 2]; +int fa[N + M], fx[N + M]; +int n, m; +int xmin, xmax; +int qx[N]; +Seg segs[N]; + +void getFa(int u, int &v, int &x) +{ + if (fa[u] == u) + { + v = u; + if (fx[u] != -1) + { + x = fx[u]; + } + return; + } + x = fx[u]; + getFa(fa[u], v, x); + fa[u] = v; + fx[u] = x; + // printf("getFa(%d) -> (%d, %d)\n", u, v, x); +} + +void setFa(int u, int v, int x) +{ + // printf("setFa(%d, %d, %d)\n", u, v, x); + fa[u] = v; + fx[u] = x; +} + +struct SegCmp +{ + bool lt(const Seg &a, const Seg &b) + { + if (between(a.p1.x, b.p1.x, b.p2.x)) + { + if (cross(a.p1 - b.p1, b.p2 - b.p1) > 0) + { + return true; + } + } + if (between(a.p2.x, b.p1.x, b.p2.x)) + { + if (cross(a.p2 - b.p1, b.p2 - b.p1) > 0) + { + return true; + } + } + if (between(b.p1.x, a.p1.x, a.p2.x)) + { + if (cross(a.p2 - a.p1, b.p1 - a.p1) > 0) + { + return true; + } + } + if (between(b.p2.x, a.p1.x, a.p2.x)) + { + if (cross(a.p2 - a.p1, b.p2 - a.p1) > 0) + { + return true; + } + } + return false; + } + bool operator()(int i, int j) + { + const Seg &a = segs[i]; + const Seg &b = segs[j]; + return lt(a, b); + // if(lt(a, b)) return true; + // if(lt(b, a)) return false; + // else return i < j; + } +}; + +void flush(int i) +{ + int v, x; + getFa(i, v, x); +} + +void scan() +{ + int k = 0; + For(i, n) + { + ps[k].id = i; + ps[k].side = P2::OPEN; + ps[k].x = segs[i].p1.x; + k++; + ps[k].id = i; + ps[k].side = P2::CLOSE; + ps[k].x = segs[i].p2.x; + k++; + } + sort(ps, ps + k); + set h; + int lastX, curX; + lastX = 0; + For(r, k) + { + curX = ps[r].x; + // cout << "x:" << curX << endl; + if (not h.empty()) + { + int i = *h.begin(); + while (lastX < curX) + { + setFa(n + lastX, i, lastX); + lastX++; + } + } + else + { + lastX = curX; + } + int i = ps[r].id; + if (ps[r].side == P2::OPEN) + { + h.insert(i); + // printf("seg#%d(%d, %d) enter\n", i, segs[i].p1.x, segs[i].p2.x); + if (segs[i].type == Seg::DOWN) + { + if (h.upper_bound(i) != h.end()) + { + setFa(i, *h.upper_bound(i), curX); + } + else + { + setFa(i, i, curX); + } + // flush(i); + } + } + else + { + // printf("seg#%d(%d, %d) leave\n", i, segs[i].p1.x, segs[i].p2.x); + if (segs[i].type == Seg::UP) + { + if (h.upper_bound(i) != h.end()) + { + setFa(i, *h.upper_bound(i), curX); + } + else + { + setFa(i, i, curX); + } + // flush(i); + } + setFa(n + lastX, *h.begin(), lastX); + lastX++; + h.erase(i); + } + // cout << "hsize: " << h.size() << endl; + } + // For(i, n + xmax + 1) { + // printf("(%d, %d) ", fa[i], fx[i]); + // } cout << endl; +} + +void init() +{ + xmin = xmax = qx[0]; + For(i, m) + { + xmin = min(xmin, qx[i]); + xmax = max(xmax, qx[i]); + } + For(i, n) + { + xmin = min(xmin, segs[i].p1.x); + xmax = max(xmax, segs[i].p2.x); + } + For(i, n) + { + setFa(i, i, -1); + } + For(i, xmax + 1) + { + setFa(n + i, n + i, i); + } +} + +int main() +{ + while (cin >> n >> m) + { + if (n == -1) + { + break; + } + For(i, n) + { + scanf("%d%d%d%d", &segs[i].p1.x, &segs[i].p1.y, &segs[i].p2.x, &segs[i].p2.y); + segs[i].normalize(); + } + For(i, m) + { + scanf("%d", &qx[i]); + } + init(); + scan(); + For(i, m) + { + int u, x; + getFa(n + qx[i], u, x); + if (u >= n || segs[u].type != Seg::HOR) + { + printf("%d\n", x); + } + else + { + printf("%d %d\n", x, segs[u].p1.y); + } + } + } + return 0; +} diff --git a/12648.cpp b/12648.cpp new file mode 100644 index 0000000..5d3b107 --- /dev/null +++ b/12648.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +int visited[512]; +int age[512]; +vector g[512], boss[512]; +void dfs(int u, int em) +{ + visited[u] = 1, boss[em].push_back(u); + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (visited[v]) + continue; + dfs(v, em); + } +} +int main() +{ + int N, M, I, x, y, em; + char cmd[10]; + while (scanf("%d %d %d", &N, &M, &I) == 3) + { + for (int i = 1; i <= N; i++) + { + scanf("%d", &age[i]); + g[i].clear(), boss[i].clear(); + } + for (int i = 0; i < M; i++) + { + scanf("%d %d", &x, &y); + g[y].push_back(x); + } + + int mp[512]; + for (int i = 1; i <= N; i++) + { + mp[i] = i; + memset(visited, 0, sizeof(visited)); + visited[i] = 1; + for (int j = 0; j < g[i].size(); j++) + if (!visited[g[i][j]]) + dfs(g[i][j], i); + } + + for (int i = 0; i < I; i++) + { + scanf("%s", cmd); + if (cmd[0] == 'P') + { + scanf("%d", &em); + int ret = 0x3f3f3f3f; + em = mp[em]; + for (int i = 0; i < boss[em].size(); i++) + ret = min(ret, age[boss[em][i]]); + if (ret == 0x3f3f3f3f) + puts("*"); + else + printf("%d\n", ret); + } + else + { + scanf("%d %d", &x, &y); + swap(age[mp[x]], age[mp[y]]); + swap(mp[x], mp[y]); + } + } + } + return 0; +} +/* +7 8 9 +21 33 33 18 42 22 26 +1 2 +1 3 +2 5 +3 5 +3 6 +4 6 +4 7 +6 7 +P 7 +T 4 2 +P 7 +P 5 +T 1 4 +P 7 +T 4 7 +P 2 +P 6 +6 5 6 +10 20 30 40 50 60 +1 5 +1 4 +3 6 +2 5 +4 5 +P 1 +P 5 +P 6 +T 1 6 +P 1 +P 4 +*/ diff --git a/12649.cpp b/12649.cpp new file mode 100644 index 0000000..5ff59c1 --- /dev/null +++ b/12649.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int n, m, A[16], B[16]; + +int dfs(int A[], int n) +{ + if (n < m) + return 0; + if (n == m) + { + int same = 1; + for (int i = 0; i < n; i++) + same &= A[i] == B[i]; + if (same) + return 1; + same = 1; + for (int i = 0; i < n; i++) + same &= A[i] == B[n - i - 1]; + if (same) + return 1; + return 0; + } + int C[16], cn; + for (int i = 1; i < n; i++) + { + cn = n - min(i, n - i); + int l, r; + for (l = i - 1, r = i; l >= 0 && r < n; l--, r++) + C[--cn] = A[l] + A[r]; + while (l >= 0) + C[--cn] = A[l--]; + while (r < n) + C[--cn] = A[r++]; + cn = n - min(i, n - i); + if (dfs(C, cn)) + return 1; + } + return 0; +} +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + scanf("%d", &m); + for (int i = 0; i < m; i++) + scanf("%d", &B[i]); + int f = dfs(A, n); + puts(f ? "S" : "N"); + } + return 0; +} + +/* + 7 + 5 6 23 8 19 7 10 + 4 + 5 16 30 27 + 7 + 1 2 3 4 5 6 7 + 5 + 7 6 5 5 5 + 4 + 1 2 3 4 + 1 + 10 + 6 + 19 23 3 51 2 0 + 2 + 34 64 + 6 + 1 2 3 4 5 6 + 6 + 1 2 3 4 5 6 + 6 + 1 2 3 4 5 6 + 6 + 6 5 4 3 2 1 + */ \ No newline at end of file diff --git a/1265.cpp b/1265.cpp new file mode 100644 index 0000000..dccdf0a --- /dev/null +++ b/1265.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +//Graphs;Minimum Spanning Tree;Kruskal + +struct Edge +{ + int x, y, v; + Edge() {} + Edge(int x, int y, int v) : x(x), y(y), v(v) {} + + inline bool operator<(const Edge &that) const + { + return this->v > that.v; + } +}; + +Edge E[5006 * 2506]; +int A[5006][5006], B[5006][5006]; +int P[5002], C[5002]; + +inline int findset(int v) +{ + if (P[v] != v) + { + return P[v] = findset(P[v]); + } + return v; +} + +inline int unionset(int x, int y) +{ + int a = findset(x), b = findset(y); + if (a == b) + { + return 0; + } + if (a > b) + { + swap(a, b); + } + P[b] = a; + C[a] += C[b]; + C[b] = 0; + return a; +} + +int main() +{ + int tt; + cin >> tt; + while (tt--) + { + int n, m; + cin >> n >> m; + for (int i = 1; i <= n; i++) + { + P[i] = i; + C[i] = 1; + for (int j = 1; j <= n; j++) + { + A[i][j] = 1 << 29; + B[i][j] = 0; + } + } + for (int i = 0; i < m; i++) + { + int x, y, v; + cin >> x >> y >> v; + E[i] = Edge(x, y, v); + A[x][y] = A[y][x] = B[x][y] = B[y][x] = v; + } + sort(E, E + m); + int total = 0; + for (int i = 0; i < m; i++) + { + int x = findset(E[i].x), y = findset(E[i].y); + if (x == y) + { + continue; + } + int a = unionset(x, y); + int outside = 0, inside = 1 << 29; + for (int j = 1; j <= n; j++) + { + A[a][j] = A[j][a] = min(A[x][j], A[y][j]); + B[a][j] = B[j][a] = max(B[x][j], B[y][j]); + if (findset(a) == findset(j)) + { + inside = min(inside, A[a][j]); + } + else + { + outside = max(outside, B[a][j]); + } + } + if (inside > outside) + { + total += C[a]; + } + } + cout << total << endl; + } + return 0; +} diff --git a/12650.cpp b/12650.cpp new file mode 100644 index 0000000..ddfbe1c --- /dev/null +++ b/12650.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +long int num[10001]; + +int main() +{ + long int i, n, r, t, sum; + while (scanf("%ld %ld", &n, &r) == 2) + { + for (i = 0; i < n; i++) + { + num[i + 1] = 1; + } + sum = 0; + while (r--) + { + scanf("%ld", &t); + num[t] = 0; + sum += t; + } + if (sum == (n * (n + 1)) / 2) + { + printf("*"); + } + else + { + for (i = 0; i < n; i++) + { + if (num[i + 1]) + { + printf("%ld ", i + 1); + } + } + } + printf("\n"); + } + return 0; +} diff --git a/12651.cpp b/12651.cpp new file mode 100644 index 0000000..ddb6a56 --- /dev/null +++ b/12651.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() +{ + int n, x[100005]; + while (scanf("%d", &n) == 1) + { + int sum = 0; + for (int i = 0; i < n; i++) + { + scanf("%d", &x[i]); + sum += x[i]; + } + if (sum % 3) + { + puts("0"); + continue; + } + int e = sum / 3; + set S; + for (int i = 0, s = 0; i < n; i++) + { + s += x[i], s %= sum; + S.insert(s); + } + int ret = 0; + for (int i = 0, s = 0; i < n; i++) + { + s += x[i]; + if (S.find((s + e) % sum) != S.end() && + S.find((s + 2 * e) % sum) != S.end()) + ret++; + } + printf("%d\n", ret / 3); + } + return 0; +} diff --git a/12652.cpp b/12652.cpp new file mode 100644 index 0000000..f3326a7 --- /dev/null +++ b/12652.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} +inline int ReadInt(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + return 0; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + *x = (*x << 3) + (*x << 1) + c - '0'; + *x *= neg; + return 1; +} + +int R[262144], C[262144], A[512], B[512]; +int swapSort(int A[], int n) +{ + int visited[512] = {}, ret = 0; + for (int i = 0; i < n; i++) + { + if (A[i] != i && visited[i] == 0) + { + int cnt = 0; + for (int j = i; visited[j] == 0;) + { + visited[j] = 1, cnt++; + j = A[j]; + } + ret += cnt - 1; + } + } + return ret; +} +int main() +{ + int n, m, x; + while (/* scanf("%d %d", &n, &m) == 2 && n */ ReadInt(&n)) + { + ReadInt(&m); + vector row[512], col[512]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + ReadInt(&x); + row[i].push_back(i * m + j + 1); + col[j].push_back(i * m + j + 1); + R[x] = i, C[x] = j; + } + } + + int err = 0; + for (int i = 0; i < n && !err; i++) + { + int r = R[row[i][0]]; + for (int j = 0; j < row[i].size() && !err; j++) + { + if (R[row[i][j]] != r) + err = 1; + } + A[i] = r; + } + for (int i = 0; i < m && !err; i++) + { + int c = C[col[i][0]]; + for (int j = 0; j < col[i].size() && !err; j++) + { + if (C[col[i][j]] != c) + err = 1; + } + B[i] = c; + } + if (err) + puts("*"); + else + { + printf("%d\n", swapSort(A, n) + swapSort(B, m)); + } + } + return 0; +} +/* + 2 2 + 3 4 + 1 2 + 3 3 + 9 2 4 + 5 8 7 + 6 1 3 + 5 4 + 13 15 14 16 + 5 7 6 8 + 9 11 10 12 + 1 3 2 4 + 17 19 18 20 + 1 4 + 2 1 4 3 + */ \ No newline at end of file diff --git a/12653.cpp b/12653.cpp new file mode 100644 index 0000000..9069a67 --- /dev/null +++ b/12653.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +const long long mod = 1000000LL; +struct Matrix +{ + long long v[2][2]; + int row, col;// row x col + Matrix(int n, int m, long long a = 0) + { + memset(v, 0, sizeof(v)); + row = n, col = m; + for (int i = 0; i < row && i < col; i++) + v[i][i] = a; + } + Matrix operator*(const Matrix &x) const + { + Matrix ret(row, x.col); + for (int i = 0; i < row; i++) + for (int j = 0; j < x.col; j++) + for (int k = 0; k < col; k++) + ret.v[i][j] = (ret.v[i][j] + v[i][k] * x.v[k][j] % mod) % mod; + return ret; + } + Matrix operator^(const long long &n) const + { + Matrix ret(row, col, 1), x = *this; + long long y = n; + while (y) + { + if (y & 1) + ret = ret * x; + y = y >> 1, x = x * x; + } + return ret; + } +}; + +int main() +{ + long long N, K, L; + while (scanf("%lld %lld %lld", &N, &K, &L) == 3) + { + N /= 5; + // long long dp[120]= {}; + // dp[0] = 1; + // for (int i = 1; i <= N; i++) { + // dp[i] = dp[i-1] * K; + // if (i - 2 >= 0) { + // dp[i] += dp[i-2] * L; + // } + // } + Matrix A(2, 2, 0); + A.v[0][0] = K % mod, A.v[0][1] = 1; + A.v[1][0] = L % mod, A.v[1][1] = 0; + Matrix M = A ^ N; + printf("%06lld\n", M.v[0][0]); + } + return 0; +} +/* + $ a_{n} = K a_{n-1} + L a_{n-2} $ + + 25 5 5 + 5 1000 1000 + 20 17 31 + 15 9 2 + */ \ No newline at end of file diff --git a/12654.cpp b/12654.cpp new file mode 100644 index 0000000..29fcb07 --- /dev/null +++ b/12654.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int F[1024]; +int main() +{ + int N, C, T[2]; + while (scanf("%d %d %d %d", &N, &C, &T[0], &T[1]) == 4) + { + for (int i = 0; i < N; i++) + scanf("%d", &F[i]); + sort(F, F + N); + int ret = 0x3f3f3f3f; + for (int i = 0; i < N; i++) + { + int dp[1024] = {}; + for (int j = 0; j <= N; j++) + dp[j] = 0x3f3f3f3f; + dp[0] = 0; + int cover1 = 0, cover2 = 0; + for (int j = 0; j < N; j++) + { + while (cover1 + 1 < N && F[cover1 + 1] - F[j] <= T[0]) + cover1++; + while (cover2 + 1 < N && F[cover2 + 1] - F[j] <= T[1]) + cover2++; + dp[cover1 + 1] = min(dp[cover1 + 1], dp[j] + T[0]); + dp[cover2 + 1] = min(dp[cover2 + 1], dp[j] + T[1]); + } + ret = min(ret, dp[N]); + swap(F[0], F[N]); + F[N] += C; + for (int j = 0; j < N; j++) + F[j] = F[j + 1]; + } + printf("%d\n", ret); + } + return 0; +} +/* +5 20 2 3 +2 5 8 11 15 +4 20 12 9 +1 2 3 13 +2 10 3 2 +0 9 +2 10 3 2 +0 2 +2 10 3 2 +0 3 +*/ diff --git a/12655.cpp b/12655.cpp new file mode 100644 index 0000000..fe1e936 --- /dev/null +++ b/12655.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +struct E +{ + int x, y, v; + E(int a = 0, int b = 0, int c = 0) : x(a), y(b), v(c) {} + bool operator<(const E &a) const + { + return v > a.v; + } +}; +E D[100005]; +vector tree[32767]; +int visited[32767], parent[32767], weight[32767]; + +int findp(int x) +{ + return parent[x] == x ? x : (parent[x] = findp(parent[x])); +} +int joint(int x, int y) +{ + x = findp(x), y = findp(y); + if (x == y) + { + return 0; + } + if (weight[x] > weight[y]) + { + weight[x] += weight[y], parent[y] = x; + } + else + { + weight[y] += weight[x], parent[x] = y; + } + return 1; +} +int kruskal(int n, int m) +{ + int sum = 0; + + sort(D, D + m); + for (int i = 0; i <= n; i++) + { + parent[i] = i, weight[i] = 1; + tree[i].clear(); + } + + for (int i = 0; i < m; i++) + { + if (joint(D[i].x, D[i].y)) + { + sum += D[i].v; + tree[D[i].x].push_back(E(D[i].x, D[i].y, D[i].v)); + tree[D[i].y].push_back(E(D[i].y, D[i].x, D[i].v)); + } + } + return sum; +} +int dp[32767][20], dpw[32767][20], treeLevel[32767], treePath[32767]; +void dfs(int u, int p, int level) +{ + treeLevel[u] = level, treePath[level] = u; + for (int i = 1; (1 << i) < level; i++) + { + dp[u][i] = min(dp[u][i - 1], dp[dpw[u][i - 1]][i - 1]); + dpw[u][i] = treePath[level - (1 << i)]; + } + for (int i = 0; i < tree[u].size(); i++) + { + int v = tree[u][i].y; + if (v == p) + continue; + dp[v][0] = tree[u][i].v; + dpw[v][0] = u; + dfs(v, u, level + 1); + } +} + +// LCA +vector> Q[32767]; // query pair, input index - node +int LCA[131072]; //input query answer buffer. +void tarjan(int u, int p) +{ // rooted-tree + parent[u] = u; + for (int i = 0; i < tree[u].size(); i++) + { // child node + int v = tree[u][i].y; + if (v == p) + { + continue; + } + tarjan(v, u); + parent[findp(v)] = u; + } + visited[u] = 1; + for (int i = 0; i < Q[u].size(); i++) + { + if (visited[Q[u][i].second]) + { + LCA[Q[u][i].first] = findp(Q[u][i].second); + } + } +} + +int query(int x, int y, int lca) +{ + int hx = treeLevel[x], hy = treeLevel[y], hlca = treeLevel[lca]; + int ret = 0x3f3f3f3f; + for (int i = 16; i >= 0; i--) + { + if (hx - hlca >= (1 << i)) + { + ret = min(ret, dp[x][i]); + x = dpw[x][i]; + hx -= (1 << i); + } + if (hy - hlca >= (1 << i)) + { + ret = min(ret, dp[y][i]); + y = dpw[y][i]; + hy -= (1 << i); + } + } + return ret; +} + +int main() +{ + int n, m, q, x, y; + while (scanf("%d %d %d", &n, &m, &q) == 3) + { + + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &D[i].x, &D[i].y, &D[i].v); + } + + kruskal(n, m); + memset(dp, 0, sizeof(dp)); + memset(dpw, 0, sizeof(dpw)); + dfs(1, -1, 1); + + for (int i = 1; i <= n; i++) + { + visited[i] = 0; + Q[i].clear(); + } + + vector> ask; + for (int i = 0; i < q; i++) + { + scanf("%d %d", &x, &y); + Q[x].push_back(make_pair(i, y)); + Q[y].push_back(make_pair(i, x)); + ask.push_back(make_pair(x, y)); + } + tarjan(1, -1); + + for (int i = 0; i < q; i++) + { + printf("%d\n", query(ask[i].first, ask[i].second, LCA[i])); + } + } + return 0; +} diff --git a/12656.cpp b/12656.cpp new file mode 100644 index 0000000..51b8d8d --- /dev/null +++ b/12656.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +#define MAXN 1024 +char s[MAXN]; +int dp[MAXN][MAXN], A[MAXN]; +int main() +{ + int K, cases = 0; + while (scanf("%d", &K) == 1) + { + while (getchar() != '\n') + ; + gets(s); + int n = 0; + for (int i = 0; s[i]; i++) + { + if (isalpha(s[i])) + { + s[n] = tolower(s[i]); + A[n] = i + 1; + n++; + } + } + + int ret = -1, pos = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; i + j < n; j++) + { + int l = j, r = i + j; + dp[l][r] = dp[l + 1][r - 1] + (s[l] != s[r]); + if (dp[l][r] <= K) + { + int len = A[r] - A[l] + 1; + if (ret < len || (ret == len && pos > A[l])) + ret = len, pos = A[l]; + } + } + } + printf("Case %d: %d %d\n", ++cases, ret, pos); + } + return 0; +} +/* +1 +Wow, it is a Race cat! +0 +abcdefg +0 +Kitty: Madam, I'm adam. +*/ diff --git a/12657.cpp b/12657.cpp new file mode 100644 index 0000000..999de86 --- /dev/null +++ b/12657.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +struct Node +{ + int l, r; +}; +Node node[131072]; +void linkNode(int l, int r) +{ + node[r].l = l, node[l].r = r; +} +int main() +{ + int n, m, cases = 0; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + node[i].l = i - 1, node[i].r = i + 1; + node[0].r = 1, node[0].l = n, node[n].r = 0; + int cmd, X, Y; + int rflag = 0; + while (m--) + { + scanf("%d", &cmd); + if (cmd == 4) + { + rflag = !rflag; + continue; + } + scanf("%d %d", &X, &Y); + if (cmd == 3) + {// swap box X, Y + if (node[Y].r == X) + swap(X, Y); + if (Y == node[X].r) + {// adjacency + linkNode(node[X].l, Y), linkNode(X, node[Y].r); + linkNode(Y, X); + } + else + { + int Yl = node[Y].l, Yr = node[Y].r; + linkNode(node[X].l, Y), linkNode(Y, node[X].r); + linkNode(Yl, X), linkNode(X, Yr); + } + } + else + { + if (rflag) + cmd = 3 - cmd; + if ((cmd == 1 && node[Y].l == X) || (cmd == 2 && node[Y].r == X)) + continue; + if (cmd == 1) + {//move box X to the left to Y + linkNode(node[X].l, node[X].r); + linkNode(node[Y].l, X), linkNode(X, Y); + } + else + {//move box X to the right to Y + linkNode(node[X].l, node[X].r); + linkNode(X, node[Y].r), linkNode(Y, X); + } + } + // for (int i = 1, p = 0; i <= n; i++) { + // p = node[p].r; + // printf("[%d] - ", p); + // } + // puts(""); + } + long long ret = 0; + for (int i = 1, p = 0; i <= n; i++) + { + p = node[p].r; + // printf("[%d] - ", p); + if (i & 1) + ret += p; + } + // puts(""); + if (rflag && n % 2 == 0) + { + ret = (long long)(n) * (n + 1) / 2 - ret; + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} +/* + 6 4 + 1 1 4 + 2 3 5 + 3 1 6 + 4 + 6 3 + 1 1 4 + 2 3 5 + 3 1 6 + 100000 1 + 4 + */ \ No newline at end of file diff --git a/12658.cpp b/12658.cpp new file mode 100644 index 0000000..a13955c --- /dev/null +++ b/12658.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int n; +string s[5]; + +int main() +{ + cin >> n; + for (int i = 0; i < 5; i++) + { + cin >> s[i]; + } + for (int i = 0; i < n; i++) + { + string sub = s[3].substr(4 * i, 3); + if (sub == ".*.") + { + cout << 1; + } + else if (sub == "*..") + { + cout << 2; + } + else + { + cout << 3; + } + } + cout << endl; + return 0; +} diff --git a/12659.cpp b/12659.cpp new file mode 100644 index 0000000..2facd38 --- /dev/null +++ b/12659.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define mp make_pair +#define A first +#define B second + +char zz[10]; +int tmp[10], ca = 0, n, m; +pair ans[5050][5050]; +vector cc[2]; +map, pair> lst; + +void deal() +{ + map, pair> use; + for (map, pair>::iterator it = lst.begin(); it != lst.end(); it++) + { + use[it->B] = it->A; + } + memset(ans, -1, sizeof(ans)); + int tcc = 0; + long long dis = 0; + for (int i = 0; i < cc[0].size(); i++) + for (int j = 0; j < cc[1].size(); j++) + { + int a = cc[0][i], b = cc[1][j]; + pair tmp = mp(a, b); + if (use.find(tmp) != use.end()) + { + a = use[tmp].A; + b = use[tmp].B; + } + if (a < n && b < m) + { + ans[a][b] = mp(i, j); + tcc++; + dis += abs(i - a) + abs(j - b); + } + } + printf("Spreadsheet #%d\n", ++ca); + printf("There are %d cell(s) kept, total distance = %lld\n", tcc, dis); +} + +int solve() +{ + if (scanf("%d%d", &n, &m) == EOF) + { + return 0; + } + if (n == 0 && m == 0) + { + return 0; + } + if (ca != 0) + { + puts(""); + } + lst.clear(); + cc[0].resize(n); + cc[1].resize(m); + for (int i = 0; i < n; i++) + { + cc[0][i] = i; + } + for (int i = 0; i < m; i++) + { + cc[1][i] = i; + } + int k; + scanf("%d", &k); + int cnt = n + m; + for (int i = 0; i < k; i++) + { + scanf("%s", zz); + if (zz[0] == 'D') + { + int t; + scanf("%d", &t); + for (int i = 0; i < t; i++) + { + scanf("%d", tmp + i); + } + sort(tmp, tmp + t); + int w = zz[1] == 'C'; + for (int i = t - 1; i >= 0; i--) + { + tmp[i]--; + cc[w].erase(cc[w].begin() + tmp[i]); + } + } + else if (zz[0] == 'I') + { + int t; + scanf("%d", &t); + for (int i = 0; i < t; i++) + { + scanf("%d", tmp + i); + } + sort(tmp, tmp + t); + int w = zz[1] == 'C'; + for (int i = t - 1; i >= 0; i--) + { + tmp[i]--; + cc[w].insert(cc[w].begin() + tmp[i], cnt++); + } + } + else + { + int a, b, c, d; + scanf("%d%d%d%d", &a, &b, &c, &d); + a--; + b--; + c--; + d--; + a = cc[0][a], b = cc[1][b], c = cc[0][c], d = cc[1][d]; + if (lst.find(mp(a, b)) == lst.end()) + { + lst[mp(a, b)] = mp(a, b); + } + if (lst.find(mp(c, d)) == lst.end()) + { + lst[mp(c, d)] = mp(c, d); + } + pair tmp = lst[mp(a, b)]; + lst[mp(a, b)] = lst[mp(c, d)]; + lst[mp(c, d)] = tmp; + } + } + deal(); + scanf("%d", &k); + for (int i = 0; i < k; i++) + { + int a, b; + scanf("%d%d", &a, &b); + a--; + b--; + if (ans[a][b].A == -1) + { + printf("Cell data in (%d,%d) GONE\n", a + 1, b + 1); + } + else + { + printf("Cell data in (%d,%d) moved to (%d,%d)\n", a + 1, b + 1, ans[a][b].A + 1, ans[a][b].B + 1); + } + } + return 1; +} + +int main() +{ + while (solve()) + ; + return 0; +} diff --git a/1266.cpp b/1266.cpp new file mode 100644 index 0000000..b7f1bf5 --- /dev/null +++ b/1266.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +template +int ndigit(T N) +{ + int digits = 0; + // remove this if '-' counts as a digit + if (N < 0) + { + digits = 1; + } + while (N) + { + N /= 10; + digits++; + } + return digits; +} + +int main() +{ + int n, i, j, t = 0; + while (scanf("%d", &n) == 1) + { + if (t > 0) + { + printf("\n"); + } + printf("n=%d, sum=%d\n", n, n * (n * n + 1) / 2); + int map_[15][15], x = 0, y = n / 2; + memset(map_, 0, sizeof(map_)); + for (i = 1; i <= n * n; i++) + { + if (map_[x][y]) + { + x += 2, y--; + if (x >= n) + { + x -= n; + } + if (y < 0) + { + y += n; + } + map_[x][y] = i; + } + else + { + map_[x][y] = i; + } + x--, y++; + if (x < 0) + { + x += n; + } + if (y >= n) + { + y -= n; + } + } + + int l = ndigit(n * n); + // fprintf(stderr, "width = %d\n", l); + + for (i = 0; i < n; i++, puts("")) + { + for (j = 0; j < n; j++) + { + cout << setw(l + 1) << map_[i][j]; + } + } + t++; + } + return 0; +} diff --git a/12660.cpp b/12660.cpp new file mode 100644 index 0000000..3a22379 --- /dev/null +++ b/12660.cpp @@ -0,0 +1,224 @@ +#include + +using namespace std; + +typedef long long lld; + +#define mp make_pair +#define pb push_back +#define X first +#define Y second +#define eps 1e-8 +#define inf 1e10 + +int Sig(double a) +{ + if (a < -eps) + { + return -1; + } + return a > eps; +} +struct Point +{ + double x, y; + Point() {} + Point(double x0, double y0) : x(x0), y(y0) {} + void in() + { + scanf("%lf %lf", &x, &y); + } + Point operator*(double t) + { + return Point(x * t, y * t); + } + double operator*(Point pt) + { + return x * pt.y - y * pt.x; + } + double operator^(Point pt) + { + return x * pt.x + y * pt.y; + } + Point operator+(Point pt) + { + return Point(x + pt.x, y + pt.y); + } + Point operator-(Point pt) + { + return Point(x - pt.x, y - pt.y); + } + double len() + { + return sqrt(x * x + y * y); + } +}; +double dis(Point x, Point y) +{ + return (x - y).len(); +} +struct Line +{ + Point p, q; + Line() {} + Line(Point p0, Point q0) : p(p0), q(q0) {} +}; +bool isinline(Point o, Line l) +{ + if (Sig((o - l.p) * (o - l.q)) != 0) + { + return false; + } + double f1 = (o - l.p) ^ (l.p - l.q); + double f2 = (o - l.q) ^ (l.p - l.q); + if (Sig(f1) == 0 || Sig(f2) == 0) + { + return true; + } + return Sig(f1) != Sig(f2); +} +bool iscross(Line l1, Line l2) +{ + if (isinline(l1.p, l2) || isinline(l1.q, l2)) + { + return true; + } + swap(l1, l2); + if (isinline(l1.p, l2) || isinline(l1.q, l2)) + { + return true; + } + Point o1 = l1.p - l1.q; + Point o2 = l2.p - l2.q; + if (Sig(o1 * o2) == 0) + { + return false; + } + if (Sig((l1.p - l2.q) * (l2.q - l2.p)) == Sig((l1.q - l2.q) * (l2.q - l2.p))) + { + return false; + } + swap(l1, l2); + if (Sig((l1.p - l2.q) * (l2.q - l2.p)) == Sig((l1.q - l2.q) * (l2.q - l2.p))) + { + return false; + } + return true; +} +bool cross(Line dec, Line l) +{ + Point o = dec.q - dec.p; + Point p = l.q - dec.p; + Point q = l.p - dec.p; + return Sig((p * q) * (p * o)) > 0 && Sig((p * o) * (q * o)) < 0; +} +bool inside(Point o, Point p[], int n) +{ + p[n] = p[0]; + for (int i = 0; i < n; i++) + if (isinline(o, Line(p[i], p[i + 1]))) + { + return true; + } + Point next = Point(o.x + rand() % 10000, o.y + rand() % 10000); + Line l1 = Line(o, next); + int tmp = 0; + for (int i = 0; i < n; i++) + { + if (Sig((next - o) * (p[i + 1] - p[i])) == 0) + { + continue; + } + Line l2 = Line(p[i], p[i + 1]); + if (cross(l1, l2)) + { + tmp++; + } + } + return tmp & 1; +} +Point p[110]; +bool can[110][110]; +double dp[110][110]; +int main() +{ + int cc = 1; + int n; + while (scanf("%d", &n) != EOF) + { + for (int i = 0; i < n; i++) + { + p[i].in(); + } + p[n] = p[0]; + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + { + if (i == j) + { + continue; + } + bool flag = true; + for (int k = 0; k < n && flag; k++) + { + int l = (k + 1) % n; + if (k == i || k == j || l == i || l == j) + { + continue; + } + if (iscross(Line(p[i], p[j]), Line(p[k], p[l]))) + { + flag = false; + } + } + if (flag) + { + Point o = (p[i] + p[j]) * 0.5; + if (!inside(o, p, n)) + { + flag = false; + } + } + can[i][j] = can[j][i] = flag; + } + // for (int i=0;i + +using namespace std; + +struct edge +{ + int to; + long long a, b, t; + edge(int x = 0, int y = 0, int z = 0, int w = 0) : to(x), a(y), b(z), t(w) {} +}; +vector g[512]; +long long dist[512], inq[512]; +long long spfa(int st, int ed, int n) +{ + memset(dist, 0x3f, sizeof(dist)); + memset(inq, 0, sizeof(inq)); + queue Q; + int u, v; + long long a, b, t; + Q.push(st), dist[st] = 0; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + inq[u] = 0; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i].to; + a = g[u][i].a, b = g[u][i].b, t = g[u][i].t; + if (a < t) + continue;// important + long long cost = 0; + if (dist[u] % (a + b) + t <= a) + cost = dist[u] + t; + else + cost = dist[u] + a + b - dist[u] % (a + b) + t; + if (cost < dist[v]) + { + dist[v] = cost; + if (!inq[v]) + inq[v] = 1, Q.push(v); + } + } + } + return dist[ed]; +} +int main() +{ + int n, m, st, ed, cases = 0; + int x, y, a, b, t; + while (scanf("%d %d %d %d", &n, &m, &st, &ed) == 4) + { + for (int i = 1; i <= n; i++) + g[i].clear(); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d %d %d", &x, &y, &a, &b, &t); + g[x].push_back(edge(y, a, b, t)); + } + + long long ret = spfa(st, ed, n); + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} +/* +3 2 1 3 +1 2 5 6 3 +2 3 7 7 6 +3 2 1 3 +1 2 5 6 3 +2 3 9 5 6 +*/ diff --git a/12662.cpp b/12662.cpp new file mode 100644 index 0000000..48c7b3d --- /dev/null +++ b/12662.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int main() +{ + int n, q, x; + char s[105][10]; + while (scanf("%d", &n) == 1) + { + for (int i = 1; i <= n; i++) + scanf("%s", s[i]); + scanf("%d", &q); + while (q--) + { + scanf("%d", &x); + if (strcmp(s[x], "?")) + puts(s[x]); + else + { + int l, r, p; + l = r = 0; + p = x - 1; + while (p > 0 && !strcmp(s[p], "?")) + p--, l++; + if (p <= 0) + l = 0x3f3f3f; + p = x + 1; + while (p <= n && !strcmp(s[p], "?")) + p++, r++; + if (p > n) + r = 0x3f3f3f; + l++, r++; + if (l == r) + printf("middle of %s and %s\n", s[x - l], s[x + r]); + else if (l < r) + { + for (int j = 0; j < l; j++) + printf("right of "); + printf("%s\n", s[x - l]); + } + else + { + for (int j = 0; j < r; j++) + printf("left of "); + printf("%s\n", s[x + r]); + } + } + } + } + return 0; +} +/* +10 +A ? ? D ? ? ? H ? ? +4 +3 +8 +6 +10 +*/ diff --git a/12663.cpp b/12663.cpp new file mode 100644 index 0000000..172da81 --- /dev/null +++ b/12663.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int A[131072], tree[131072]; + +void modify(int x, int N, int val) +{ + while (x <= N) + { + tree[x] += val; + x += x & (-x); + } +} + +int query(int x) +{ + int ret = 0; + while (x) + { + ret += tree[x]; + x -= x & (-x); + } + return ret; +} +int main() +{ + int cases = 0; + int N, M, K, l = 0, r; + while (scanf("%d %d %d", &N, &M, &K) == 3) + { + for (int i = 1; i <= N; i++) + scanf("%d", &A[i]); + sort(A + 1, A + 1 + N); + memset(tree, 0, sizeof(tree)); + int p = 1; + for (int i = 0; i < M; i++) + { + scanf("%d %d", &r, &l); + p = upper_bound(A + 1, A + 1 + N, p) - A; + r = upper_bound(A + 1, A + 1 + N, r) - A; + if (p <= r) + { + modify(p, N, 1); + modify(r, N, -1); + } + p = l; + } + + int ret = 0; + for (int i = 1; i <= N; i++) + { + int cnt = query(i); + ret += cnt >= K; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} +/* + 2 2 2 + 2 5 + 6 2 + 8 3 + 5 3 2 + 2 3 4 5 6 + 5 3 + 4 2 + 5 2 + */ \ No newline at end of file diff --git a/12664.cpp b/12664.cpp new file mode 100644 index 0000000..fd966ae --- /dev/null +++ b/12664.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int x, y; +int c[3][10], dp[105000], step[105000]; + +int main() +{ + int T = 0; + while (scanf("%d%d", &x, &y) != EOF) + { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 10; j++) + { + scanf("%d", &c[i][j]); + } + memset(dp, 17, sizeof(dp)); + dp[x] = 0; + step[x] = 0; + if (x != 0) + { + dp[0] = c[2][0]; + step[0] = 1; + } + for (int i = 0; i < y; i++) + { + int k = 0; + for (int j = 0; j < 10; j++) + { + k = i * 10 + j; + if (k > y) + { + break; + } + if (dp[k] > dp[i] + c[0][j] || (dp[k] == dp[i] + c[0][j] && step[k] > step[i] + 1)) + { + dp[k] = dp[i] + c[0][j]; + step[k] = step[i] + 1; + } + } + for (int j = 0; j < 10; j++) + { + k = i + j; + if (k > y) + { + break; + } + if (dp[k] > dp[i] + c[1][j] || (dp[k] == dp[i] + c[1][j] && step[k] > step[i] + 1)) + { + dp[k] = dp[i] + c[1][j]; + step[k] = step[i] + 1; + } + } + for (int j = 0; j < 10; j++) + { + k = i * j; + if (k > y) + { + break; + } + if (dp[k] > dp[i] + c[2][j] || (dp[k] == dp[i] + c[2][j] && step[k] > step[i] + 1)) + { + dp[k] = dp[i] + c[2][j]; + step[k] = step[i] + 1; + } + } + } + printf("Case %d: %d %d\n", ++T, dp[y], step[y]); + } + return 0; +} diff --git a/12665.cpp b/12665.cpp new file mode 100644 index 0000000..e6fd727 --- /dev/null +++ b/12665.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int main() +{ + int L, R, cases = 0; + while (scanf("%d %d", &L, &R) == 2) + { + int limit = min(1005, R);// (10^3)^3 = 10^9 + int ret = 0; + for (long long a = L; a <= limit; a++) + { + for (long long b = L; b <= limit; b++) + { + long long t = (a * a * a + b * b * b); + long long c = t / 10; + if (t % 10 != 3) + continue; + if (L <= c && c <= R) + ret++; + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} +/* +1 10 +1 20 +123 456789 +*/ diff --git a/12667.cpp b/12667.cpp new file mode 100644 index 0000000..0e74ab5 --- /dev/null +++ b/12667.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int main() +{ + int N, T, M; + int time, team; + char prob[50], judge[50]; + while (scanf("%d %d %d", &N, &T, &M) == 3) + { + int scoreboard[105][25] = {}; + int prob_blood[25], prob_solve[25]; + memset(prob_blood, -1, sizeof(prob_blood)); + while (M--) + { + scanf("%d %d %s %s", &time, &team, &prob, &judge); + if (judge[0] == 'Y') + { + if (scoreboard[team][prob[0] - 'A'] == 0) + { + scoreboard[team][prob[0] - 'A'] = 1; + prob_blood[prob[0] - 'A'] = time; + prob_solve[prob[0] - 'A'] = team; + } + } + } + for (int i = 0; i < N; i++) + { + printf("%c ", i + 'A'); + if (prob_blood[i] == -1) + puts("- -"); + else + printf("%d %d\n", prob_blood[i], prob_solve[i]); + } + } + return 0; +} diff --git a/12668.cpp b/12668.cpp new file mode 100644 index 0000000..e27e771 --- /dev/null +++ b/12668.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +const int SIZE = 100 + 10, MAXN = 10000 + 10; + +vector G[MAXN]; +int col[SIZE][SIZE], row[SIZE][SIZE]; +char A[SIZE][SIZE]; +int mth1[MAXN], mth2[MAXN]; +bool vis[MAXN]; +int R, N, M; + +int path(int u) +{ + for (int i = 0; i < (int)G[u].size(); ++i) + { + int v = G[u][i]; + if (vis[v]) + continue; + vis[v] = true; + if (mth2[v] < 0 || path(mth2[v])) + { + mth1[u] = v; + mth2[v] = u; + return 1; + } + } + return 0; +} + +int main() +{ + while (scanf("%d", &R) == 1) + { + for (int i = 0; i < R; ++i) + scanf("%s", A[i]); + for (int i = 0; i < R; ++i) + { + for (int j = 0; j < R; ++j) + { + col[i][j] = row[i][j] = -1; + } + } + // gao row + N = 0; + for (int i = 0; i < R; ++i) + { + for (int j = 0; j < R; ++j) + { + if (A[i][j] == 'X' || row[i][j] != -1) + continue; + for (int k = j; k < R && A[i][k] == '.'; ++k) + row[i][k] = N; + ++N; + } + } + // gao col + for (int i = 0; i < N; ++i) + G[i].clear(); + M = 0; + for (int i = 0; i < R; ++i) + { + for (int j = 0; j < R; ++j) + { + if (A[i][j] == 'X' || col[i][j] != -1) + continue; + for (int k = i; k < R && A[k][j] == '.'; ++k) + { + col[k][j] = M; + if (row[k][j] != -1) + G[row[k][j]].push_back(M); + } + ++M; + } + } + /* + for (int i = 0; i < R; ++ i) { + for (int j = 0; j < R; ++ j) { + cout << row[i][j] << " "; + } + cout << endl; + } + for (int i = 0; i < R; ++ i) { + for (int j = 0; j < R; ++ j) { + cout << col[i][j] << " "; + } + cout << endl; + }*/ + for (int i = 0; i < N; ++i) + mth1[i] = -1; + for (int i = 0; i < M; ++i) + mth2[i] = -1; + int ret = 0; + for (int i = 0; i < N; ++i) + { + if (mth1[i] < 0) + { + for (int j = 0; j < M; ++j) + vis[j] = false; + ret += path(i); + } + } + printf("%d\n", ret); + } +} diff --git a/12669.cpp b/12669.cpp new file mode 100644 index 0000000..d832db1 --- /dev/null +++ b/12669.cpp @@ -0,0 +1,195 @@ +#include + +using namespace std; + +#define lson (rt << 1) +#define rson (rt << 1 | 1) +#define mid ((l + r) >> 1) + +const int MAXN = 100000 + 10; + +struct Node +{ + int mi, ma, rev; +} SegTree[MAXN << 2]; + +char S[MAXN], P[MAXN]; +int val[MAXN], N, L; + +inline char change(char c) +{ + if (c >= 'a' && c <= 'z') + return c - 'a' + 'A'; + else + return c - 'A' + 'a'; +} + +int mismatch(int st) +{ + int ret = 0; + if (st + L > N) + return -1; + for (int i = 0; i < L; ++i) + { + if (tolower(S[st + i]) != tolower(P[i])) + return -1; + else + ret += (S[st + i] != P[i]); + } + return ret; +} + +inline int tmax(int a, int b) { return max(a, b); } +inline int tmin(int a, int b) +{ + if (a == -1) + return b; + else if (b == -1) + return a; + else + return min(a, b); +} + +inline void update(int rt) +{ + SegTree[rt].mi = tmin(SegTree[lson].mi, SegTree[rson].mi); + SegTree[rt].ma = tmax(SegTree[lson].ma, SegTree[rson].ma); +} + +inline void Set(int rt) +{ + SegTree[rt].rev ^= 1; + if (SegTree[rt].mi != -1) + SegTree[rt].mi = L - SegTree[rt].mi; + if (SegTree[rt].ma != -1) + SegTree[rt].ma = L - SegTree[rt].ma; + swap(SegTree[rt].mi, SegTree[rt].ma); +} + +inline void pushdown(int rt) +{ + if (SegTree[rt].rev) + { + SegTree[rt].rev = 0; + Set(lson); + Set(rson); + } +} + +void build(int rt, int l, int r) +{ + SegTree[rt].rev = 0; + if (l + 1 == r) + { + SegTree[rt].mi = SegTree[rt].ma = val[l]; + return; + } + build(lson, l, mid); + build(rson, mid, r); + update(rt); +} + +void reverse(int rt, int l, int r, int L, int R) +{ + if (L <= l && R >= r) + { + Set(rt); + return; + } + pushdown(rt); + if (L < mid) + reverse(lson, l, mid, L, R); + if (R > mid) + reverse(rson, mid, r, L, R); + update(rt); +} + +int query(int rt, int l, int r, int L, int R) +{ + if (R <= L) + return -1; + if (L <= l && R >= r) + return SegTree[rt].ma; + pushdown(rt); + int ret = -1; + if (L < mid) + ret = tmax(ret, query(lson, l, mid, L, R)); + if (R > mid) + ret = tmax(ret, query(rson, mid, r, L, R)); + return ret; +} + +void push(int rt, int l, int r, int p) +{ + if (p < l || p >= r) + return; + if (l + 1 == r) + { + if (SegTree[rt].rev) + { + S[l] = change(S[l]); + SegTree[rt].rev = 0; + } + return; + } + pushdown(rt); + if (p < mid) + push(lson, l, mid, p); + else + push(rson, mid, r, p); +} + +void modify(int rt, int l, int r, int p, int v) +{ + if (l + 1 == r) + { + SegTree[rt].mi = SegTree[rt].ma = v; + return; + } + pushdown(rt); + if (p < mid) + modify(lson, l, mid, p, v); + else + modify(rson, mid, r, p, v); + update(rt); +} + +int main() +{ + for (int Q; scanf("%d%s", &Q, P) == 2;) + { + L = strlen(P); + scanf("%s", S); + N = strlen(S); + for (int i = 0; i < N; ++i) + val[i] = mismatch(i); + build(1, 0, N); + while (Q--) + { + int l, r; + scanf("%d%d", &l, &r); + --l, --r; + printf("%d\n", query(1, 0, N, l, r - L + 2)); + reverse(1, 0, N, l, r + 1); + for (int i = l - L; i <= l + L; ++i) + push(1, 0, N, i); + for (int i = r - L; i <= r + L; ++i) + push(1, 0, N, i); + for (int i = l - L; i < l; ++i) + { + if (i < 0 || i >= N || val[i] == -1) + continue; + val[i] = mismatch(i); + modify(1, 0, N, i, val[i]); + } + for (int i = r - L; i <= r; ++i) + { + if (i < 0 || i >= N || val[i] == -1) + continue; + val[i] = mismatch(i); + modify(1, 0, N, i, val[i]); + } + } + } + return 0; +} diff --git a/1267.cpp b/1267.cpp new file mode 100644 index 0000000..5a97c97 --- /dev/null +++ b/1267.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +const int maxn = 1000 + 5; +int s, k, n, fa[maxn]; +bool covered[maxn]; +vector G[maxn], nodes[maxn]; + +void dfs(int u, int f, int d) +{ + fa[u] = f; + int l = G[u].size(); + if (l == 1 && d > k) + nodes[d].push_back(u); + for (int i(0); i < l; ++i) + { + if (G[u][i] != f) + dfs(G[u][i], u, d + 1); + } +} + +void dfs2(int u, int f, int d) +{ + covered[u] = true; + int l = G[u].size(); + for (int i(0); i < l; ++i) + { + int v = G[u][i]; + if (v != f && d < k) + dfs2(v, u, d + 1); + } +} + +int solve() +{ + int ans = 0; + memset(covered, 0, sizeof(covered)); + for (int d(n - 1); d > k; --d) + for (int i(0); i < nodes[d].size(); ++i) + { + int u = nodes[d][i]; + if (covered[u]) + continue; + int v = u; + for (int j(0); j < k; ++j) + v = fa[v]; + dfs2(v, -1, 0); + ++ans; + } + return ans; +} + +int main() +{ + // freopen("in.txt","r",stdin); + int casenum; + scanf("%d", &casenum); + while (casenum--) + { + for (int i(1); i <= n; ++i) + { + G[i].clear(); + nodes[i].clear(); + } + scanf("%d%d%d", &n, &s, &k); + int u, v; + for (int i(1); i < n; ++i) + { + scanf("%d%d", &u, &v); + G[u].push_back(v); + G[v].push_back(u); + } + dfs(s, -1, 0); + printf("%d\n", solve()); + } + return 0; +} diff --git a/12670.cpp b/12670.cpp new file mode 100644 index 0000000..15b108c --- /dev/null +++ b/12670.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +long long mpow(int n, int m) +{ + long long ret = 1; + for (int i = 1; i <= m; i++) + ret *= n; + return ret; +} +void ito2(long long n, char buf[]) +{ + if (n == 0) + { + buf[0] = '0'; + buf[1] = '\0'; + return; + } + int i, j, f; + for (i = 63, j = f = 0; i >= 0; i--) + { + if ((n >> i) & 1) + f = 1; + if (f) + { + buf[j++] = ((n >> i) & 1) + '0'; + } + } + buf[j] = '\0'; +} +void a2toi(long long &r, char buf[]) +{ + r = 0; + for (int i = 0; buf[i]; i++) + r = r << 1 | (buf[i] - '0'); +} +void calc(long long n, long long cnt[]) +{ + if (n <= 0) + return; + //printf("%d\n", n); + char buf[105]; + ito2(n, buf); + int len = strlen(buf); + long long prev = 0; + long long suffix; + calc(mpow(2, len - 1) - 1, cnt); + //for(int i = 0; i < 10; i++) + // cnt[i] = 0; + long long prev10 = 1; + for (int i = 0; i < len; i++) + { + int d = buf[i] - '0'; + a2toi(suffix, buf + i + 1); + + if (i != len - 1) + cnt[d] += suffix + 1; + else + cnt[d]++; + if (i != 0) + cnt[d] += (prev - prev10 / 2) * mpow(2, len - i - 1); + for (int j = i == 0; j < 2; j++) + { + if (j == d) + continue; + if (j < d) + { + if (prev > 0) + { + cnt[j] += (prev - prev10 / 2 + 1) * mpow(2, len - i - 1); + } + else + { + cnt[j] += mpow(2, len - i - 1); + } + } + else + { + if (prev > 0 && prev - prev10 / 2 > 0) + { + cnt[j] += (prev - prev10 / 2) * mpow(2, len - i - 1); + } + } + } + prev10 *= 2; + prev = prev * 2 + d; + } +} +int main() +{ + long long A, B; + while (scanf("%lld %lld", &A, &B) == 2) + { + long long cntA[2] = {}, cntB[2] = {}; + calc(A - 1, cntA); + calc(B, cntB); + printf("%lld\n", cntB[1] - cntA[1]); + } + return 0; +} diff --git a/12671.cpp b/12671.cpp new file mode 100644 index 0000000..31d6c43 --- /dev/null +++ b/12671.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +int parent[32767], weight[32767]; + +int findp(int x) +{ + return parent[x] == x ? x : parent[x] = findp(parent[x]); +} + +int main() +{ + int n, m, x, y; + while (scanf("%d %d", &n, &m) == 2 && n) + { + for (int i = 0; i <= n; i++) + { + parent[i] = 0, weight[i] = 0; + } + + vector g[1024]; + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + if (x == 1) + { + parent[y] = y; + } + else + g[y].push_back(x); + } + for (int i = 2; i <= n; i++) + { + for (int j = 0; j < g[i].size(); j++) + { + int u = g[i][j]; + if (parent[i] == 0) + parent[i] = u; + else if (findp(i) != findp(u)) + parent[i] = i; + } + } + + for (int i = 2; i <= n; i++) + { + weight[findp(i)]++; + } + int ret = 0; + for (int i = 2; i <= n; i++) + { + if (i == parent[i]) + { + ret += weight[i] * (n - 1 - weight[i]); + } + } + printf("%d\n", ret / 2 + n - 1); + } + return 0; +} +/* + 6 6 + 1 2 + 1 3 + 1 4 + 2 5 + 2 6 + 3 6 + 8 11 + 1 2 + 1 3 + 1 4 + 2 5 + 3 4 + 6 7 + 3 6 + 3 7 + 4 8 + 2 6 + 5 6 + */ \ No newline at end of file diff --git a/12672.cpp b/12672.cpp new file mode 100644 index 0000000..023724e --- /dev/null +++ b/12672.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +#define MAXN 128 +char s[MAXN]; +long long dp[MAXN][MAXN][11]; +long long C[MAXN][MAXN] = {}; +const long long mod = 1000000007LL; +int main() +{ + C[0][0] = 1; + for (int i = 1; i < MAXN; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; + } + + while (scanf("%s", s) == 1) + { + int n = strlen(s); + int digit[11] = {}, sum[11] = {}; + for (int i = 0; i < n; i++) + digit[s[i] - '0']++; + digit[10] = digit[0]; + for (int i = 1; i <= 10; i++) + sum[i] = sum[i - 1] + digit[i]; + memset(dp, 0, sizeof(dp)); + int m = 0; + int half = n / 2 + n % 2; + dp[0][0][0] = 1; + for (int i = 1; i <= 10; i++) + { + for (int j = 0; j <= digit[i]; j++) + { + for (int k = 0; k <= half; k++) + { + for (int l = 0; l < 11; l++) + { + long long way = dp[i - 1][k][l]; + if (way == 0) + continue; + int p = sum[i - 1] - k; + if (p < 0) + continue; + int l1 = j, l2 = digit[i] - j; + // printf("%d %d %d %d %d\n", i, k, l, k + l1, p + l2); + if (k + l1 > n - half) + continue; + if (p + l2 > half) + continue; + if (n % 2 == 0 && i == 10) + {// append 0 + way = way * C[k + l1 - 1][l1] % mod; + } + else + { + way = way * C[k + l1][l1] % mod; + } + if (n % 2 != 0 && i == 10) + {// append 0 + way = way * C[p + l2 - 1][l2] % mod; + } + else + { + way = way * C[p + l2][l2] % mod; + } + int t = ((l + l1 * (i == 10 ? 0 : i) - l2 * (i == 10 ? 0 : i)) % 11 + 11) % 11; + dp[i][k + l1][t] += way; + dp[i][k + l1][t] %= mod; + // printf("%d %d %d %lld\n", i, k+l1, t, dp[i][k+l1][t]); + } + } + } + } + printf("%lld\n", dp[10][n - half][0]); + } + return 0; +} +/* +2090 +16510 +201400000000000000000000000000 +*/ diff --git a/12673.cpp b/12673.cpp new file mode 100644 index 0000000..c9f3873 --- /dev/null +++ b/12673.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +bool cmp(pair x, pair y) +{ + return x.first - x.second > y.first - y.second; +} +int main() +{ + int N, G, S, R; + while (scanf("%d %d", &N, &G) == 2) + { + vector> D; + for (int i = 0; i < N; i++) + { + scanf("%d %d", &S, &R); + D.push_back(make_pair(S, R)); + } + sort(D.begin(), D.end(), cmp); + int ret = 0; + for (int i = 0; i < N; i++) + { + S = D[i].first, R = D[i].second; + if (S > R) + ret += 3; + else if (S == R) + { + if (G) + ret += 3, G--; + else + ret += 1; + } + else + { + if (G > R - S) + ret += 3, G -= R - S + 1; + else if (G == R - S) + ret += 1, G = 0; + } + } + printf("%d\n", ret); + } + return 0; +} +/* +2 1 +1 1 +1 1 +3 2 +1 3 +3 1 +2 2 +4 10 +1 1 +2 2 +1 3 +0 4 +*/ diff --git a/12674.cpp b/12674.cpp new file mode 100644 index 0000000..9b67ade --- /dev/null +++ b/12674.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int n; +int h[1048576], a[1048576]; +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &h[i]), a[i] = h[i]; + + stack> stk; + for (int i = 0; i < n; i++) + { + if (!stk.empty()) + { + stk.top().second = min(stk.top().second, h[i]); + } + while (!stk.empty() && stk.top().first <= h[i]) + { + pair p = stk.top(); + stk.pop(); + if (!stk.empty()) + stk.top().second = min(stk.top().second, p.second); + } + if (!stk.empty()) + { + a[i] = min(a[i], h[i] - stk.top().second); + } + stk.push(make_pair(h[i], h[i])); + } + + while (!stk.empty()) + stk.pop(); + + for (int i = n - 1; i >= 0; i--) + { + if (!stk.empty()) + { + stk.top().second = min(stk.top().second, h[i]); + } + while (!stk.empty() && stk.top().first <= h[i]) + { + pair p = stk.top(); + stk.pop(); + if (!stk.empty()) + stk.top().second = min(stk.top().second, p.second); + } + if (!stk.empty()) + { + a[i] = min(a[i], h[i] - stk.top().second); + } + stk.push(make_pair(h[i], h[i])); + } + + int f = 0; + for (int i = 0; i < n; i++) + { + if (a[i] >= 150000) + { + if (f++) + printf(" "); + printf("%d", i + 1); + } + } + puts(""); + } + return 0; +} +/* + 5 + 0 10000 100000 884813 0 + 7 + 0 100000 0 200000 180000 200000 0 + */ \ No newline at end of file diff --git a/12675.cpp b/12675.cpp new file mode 100644 index 0000000..3863ce8 --- /dev/null +++ b/12675.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +const double eps = 1e-10, PI = acos(-1.0); + +inline int sgn(double x) +{ + if (fabs(x) <= eps) + return 0; + else if (x > eps) + return 1; + else + return -1; +} + +struct Point +{ + double x, y, ang; + Point() : x(0), y(0) {} + Point(double a, double b) : x(a), y(b) + { + ang = atan2(y, x); + } + Point operator+(const Point &rhs) const + { + return Point(x + rhs.x, y + rhs.y); + } + Point operator-(const Point &rhs) const + { + return Point(x - rhs.x, y - rhs.y); + } + Point operator*(double k) const + { + return Point(x * k, y * k); + } + Point operator/(double k) const + { + return Point(x / k, y / k); + } + double dot(const Point &rhs) const + { + return x * rhs.x + y * rhs.y; + } + double det(const Point &rhs) const + { + return x * rhs.y - y * rhs.x; + } + double abs() const + { + return hypot(x, y); + } + void read() + { + scanf("%lf%lf", &x, &y); + } +} O; + +double nowAng; + +Point inter(Point A, Point B, Point C, Point D) +{ + return A + (B - A) * ((D - C).det(C - A) / (D - C).det(B - A)); +} + +struct Line +{ + Point A, B; + Line() {} + Line(Point a, Point b) : A(a), B(b) {} + double dis() const + { + if (sgn((O - A).det(O - B)) == 0) + return min((O - A).abs(), (O - B).abs()); + return (O - inter(A, B, O, Point(cos(nowAng), sin(nowAng)))).abs(); + } + bool operator<(const Line &rhs) const + { + return sgn(dis() - rhs.dis()) < 0; + } +}; + +struct Event +{ + double ang; + int id, type; + Event() : ang(0), id(0), type(0) {} + Event(double a, int b, int c) : ang(a), id(b), type(c) {} + bool operator<(const Event &rhs) const + { + if (sgn(ang - rhs.ang) != 0) + return sgn(ang - rhs.ang) < 0; + return type < rhs.type; + } +}; + +const int MAXN = 30000 + 10; + +Point P[MAXN]; +Line L[MAXN]; +int S, N, M; + +vector E; +set Seg; +set::iterator its[MAXN]; + +double fix(double x) +{ + if (x < 0) + x += PI * 2; + if (x >= PI * 2) + x -= PI * 2; + return x; +} + +int gao(int id) +{ + int ret = 0; + E.clear(); + for (int i = 0; i < N; ++i) + { + if (i == id) + continue; + Point tmp = P[i] - P[id]; + E.push_back(Event(tmp.ang, i, 1)); + } + for (int i = 0; i < M; ++i) + { + Point A = L[i].A - P[id]; + Point B = L[i].B - P[id]; + double delta = fix(B.ang - A.ang); + if (sgn(delta - PI) > 0) + swap(A, B); + if (sgn(A.ang - B.ang) > 0) + { + E.push_back(Event(A.ang, i, 0)); + E.push_back(Event(PI, i, 2)); + E.push_back(Event(-PI, i, 0)); + E.push_back(Event(B.ang, i, 2)); + } + else + { + E.push_back(Event(A.ang, i, 0)); + E.push_back(Event(B.ang, i, 2)); + } + } + sort(E.begin(), E.end()); + Seg.clear(); + for (int i = 0; i < (int)E.size(); ++i) + { + int nowID = E[i].id; + nowAng = E[i].ang; + if (E[i].type == 0) + { + its[nowID] = Seg.insert(Line(L[nowID].A - P[id], L[nowID].B - P[id])).first; + } + else if (E[i].type == 1) + { + ret += (Seg.empty() || sgn(Seg.begin()->dis() - (P[id] - P[nowID]).abs()) > 0); + } + else if (E[i].type == 2) + { + Seg.erase(its[nowID]); + } + } + return ret; +} + +int main() +{ + O = Point(0, 0); + while (scanf("%d%d%d", &S, &N, &M) == 3) + { + for (int i = 0; i < N; ++i) + P[i].read(); + for (int i = 0; i < M; ++i) + { + L[i].A.read(); + L[i].B.read(); + } + for (int i = 0; i < S; ++i) + { + printf("%d\n", gao(i)); + } + } + return 0; +} diff --git a/12676.cpp b/12676.cpp new file mode 100644 index 0000000..4a734af --- /dev/null +++ b/12676.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int n; +int a[100]; +vector s[100]; +long long h[100]; + +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%d", a + i); + s[i].clear(); + h[i] = 1; + } + s[n].clear(); + h[n] = 1; + for (int i = 0; i < n; i++) + { + s[a[i]].push_back(0); + } + for (int i = n - 1; i > 0; i--) + if (s[i].size()) + { + sort(s[i].begin(), s[i].end()); + int tot = s[i].size(); + for (int j = 0; j < tot; j++) + { + if (s[i][j] < h[i + 1]) + s[i][j] = h[i + 1]; + } + h[i] = s[i][tot - 1]; + for (int j = 0; j < tot; j += 2) + { + s[i - 1].push_back(s[i][j] + s[i][j + 1]); + } + } + printf("%lld\n", s[0][0]); + } + return 0; +} diff --git a/12677.cpp b/12677.cpp new file mode 100644 index 0000000..4cc613e --- /dev/null +++ b/12677.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define oo 0xfffffff + +vector g[65536]; +int dp_down[65536][2], dp_up[65536]; +int used[65536]; +void dfs(int u) +{ + used[u] = 1; + dp_down[u][0] = dp_down[u][1] = 0; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (used[v] == 0) + { + dfs(v); + if (dp_down[v][0] + 1 > dp_down[u][1]) + dp_down[u][1] = dp_down[v][0] + 1; + if (dp_down[u][1] > dp_down[u][0]) + swap(dp_down[u][0], dp_down[u][1]); + } + } +} +void dfs2(int u, int dep) +{ + dp_up[u] = dep, used[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (used[v] == 0) + { + int hv; + if (dp_down[v][0] + 1 != dp_down[u][0]) + hv = dp_down[u][0]; + else + hv = dp_down[u][1]; + hv = max(hv, dp_up[u]); + dfs2(v, hv + 1); + } + } +} +int main() +{ + int n, m; + int x, y; + while (scanf("%d %d", &n, &m) == 2) + { + int mxpath[2][65636]; + int a_diameter = 0, b_diameter = 0; + for (int i = 0; i <= n; i++) + g[i].clear(); + for (int i = 1; i < n; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(y); + g[y].push_back(x); + } + memset(used, 0, sizeof(used)); + dfs(1); + memset(used, 0, sizeof(used)); + dfs2(1, 0); + for (int i = 1; i <= n; i++) + { + mxpath[0][i] = max(max(dp_down[i][0], dp_down[i][1]), dp_up[i]); + a_diameter = max(a_diameter, dp_down[i][0] + dp_down[i][1]); + a_diameter = max(a_diameter, dp_down[i][0] + dp_up[i]); + } + + for (int i = 0; i <= m; i++) + g[i].clear(); + for (int i = 1; i < m; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(y); + g[y].push_back(x); + } + memset(used, 0, sizeof(used)); + dfs(1); + memset(used, 0, sizeof(used)); + dfs2(1, 0); + for (int i = 1; i <= m; i++) + { + mxpath[1][i] = max(max(dp_down[i][0], dp_down[i][1]), dp_up[i]); + b_diameter = max(b_diameter, dp_down[i][0] + dp_down[i][1]); + b_diameter = max(b_diameter, dp_down[i][0] + dp_up[i]); + } + double ret = 0, suffix[65536] = {}; + sort(mxpath[0] + 1, mxpath[0] + 1 + n); + sort(mxpath[1] + 1, mxpath[1] + 1 + m); + for (int i = m; i >= 0; i--) + suffix[i] = suffix[i + 1] + mxpath[1][i]; + int mx_diameter = max(a_diameter, b_diameter); + for (int i = 1, j = m; i <= n; i++) + { + while (j > 0 && mxpath[0][i] + mxpath[1][j] >= mx_diameter) + j--; + ret += j * mx_diameter + (suffix[j + 1] + mxpath[0][i] * (m - j) + m - j); + } + printf("%.3lf\n", ret / n / m); + } + return 0; +} +/* +4 5 +1 2 +2 3 +4 2 +2 3 +3 4 +4 1 +4 5 + +1 5 +1 2 +2 3 +3 4 +4 5 +*/ diff --git a/12679.cpp b/12679.cpp new file mode 100644 index 0000000..21f9729 --- /dev/null +++ b/12679.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +typedef vector VI; +typedef vector VVI; + +const int INF = 1000000000; + +struct MaxFlow +{ + int N; + VVI cap, flow; + VI dad, Q; + + MaxFlow(int N) : N(N), cap(N, VI(N)), flow(N, VI(N)), dad(N), Q(N) {} + + void AddEdge(int from, int to, int cap) + { + this->cap[from][to] += cap; + } + + void SetEdge(int from, int to, int cap) + { + this->cap[from][to] = cap; + } + + int BlockingFlow(int s, int t) + { + fill(dad.begin(), dad.end(), -1); + dad[s] = -2; + + int head = 0, tail = 0; + Q[tail++] = s; + while (head < tail) + { + int x = Q[head++]; + for (int i = 0; i < N; i++) + { + if (dad[i] == -1 && cap[x][i] - flow[x][i] > 0) + { + dad[i] = x; + Q[tail++] = i; + } + } + } + + if (dad[t] == -1) + return 0; + + int totflow = 0; + for (int i = 0; i < N; i++) + { + if (dad[i] == -1) + continue; + int amt = cap[i][t] - flow[i][t]; + for (int j = i; amt && j != s; j = dad[j]) + amt = min(amt, cap[dad[j]][j] - flow[dad[j]][j]); + if (amt == 0) + continue; + flow[i][t] += amt; + flow[t][i] -= amt; + for (int j = i; j != s; j = dad[j]) + { + flow[dad[j]][j] += amt; + flow[j][dad[j]] -= amt; + } + totflow += amt; + } + + return totflow; + } + + int GetMaxFlow(int source, int sink) + { + fill(Q.begin(), Q.end(), 0); + + for (int i = 0; i < N; ++i) + { + fill(flow[i].begin(), flow[i].end(), 0); + } + + int totflow = 0; + while (int flow = BlockingFlow(source, sink)) + totflow += flow; + return totflow; + } +}; + +struct Course +{ + // init, end, classes required + int a, b, s; +}; + +Course courses[101]; +int clean[101][101]; + +int main() +{ + int tests = 0; + cin >> tests; + + for (int t = 1; t <= tests; ++t) + { + int n, m; + cin >> n >> m; + + int totalClasses = 0; + + for (int i = 0; i < n; ++i) + { + int s = 0; + cin >> courses[i].a >> courses[i].b >> s; + courses[i].s = s / m + (s % m != 0); + totalClasses += courses[i].s; + } + + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + cin >> clean[i][j]; + } + } + + MaxFlow maxFlow(2 * n + 4); + + //source to classes + int sourceId = 2 * n; + int sinkId = 2 * n + 1; + int subSourceId = 2 * n + 2; + int subSinkId = 2 * n + 3; + + for (int i = 0; i < n; ++i) + { + maxFlow.AddEdge(sourceId, i, courses[i].s); + maxFlow.AddEdge(i + n, sinkId, courses[i].s); + maxFlow.AddEdge(i, subSourceId, INF); + maxFlow.AddEdge(subSinkId, i + n, INF); + } + + for (int i = 0; i < n - 1; ++i) + { + const Course &c1 = courses[i]; + + for (int j = i + 1; j < n; ++j) + { + const Course &c2 = courses[j]; + + if (c1.b + clean[i][j] < c2.a) + { + maxFlow.AddEdge(i, j + n, INF); + } + + if (c2.b + clean[j][i] < c1.a) + { + maxFlow.AddEdge(j, i + n, INF); + } + } + } + + int low = 0; + int high = totalClasses; + int ans = -1; + + while (low <= high) + { + int mid = (low + high) / 2; + + maxFlow.SetEdge(subSourceId, subSinkId, mid); + int totFlow = maxFlow.GetMaxFlow(sourceId, sinkId); + + if (totFlow == totalClasses) + { + ans = mid; + high = mid - 1; + } + else + { + low = mid + 1; + } + } + + cout << "Case " << t << ": " << ans << endl; + } + + return 0; +} diff --git a/12680.cpp b/12680.cpp new file mode 100644 index 0000000..a479072 --- /dev/null +++ b/12680.cpp @@ -0,0 +1,171 @@ +#include + +using namespace std; + +#define X first +#define Y second +#define MP make_pair +#define PB push_back +#define SZ size() +#define SQ(a) ((a) * (a)) + +int N;// numero de nodos + +int M;// number of connections + +int X1[201];// x-position of nodes +int Y1[201];// y-position of nodes +int F[201]; // floor of nodes + +typedef double V; // tipo de costes +typedef pair P;// par de (coste,nodo) +typedef set

S; // conjunto de pares + +vector

A[201];// listas adyacencia (coste,nodo) + +int prec[201];// predecesores + +V dijkstra(int s, int t) +{ + S m; // cola de prioridad + vector z(N, 1000000000);// distancias iniciales + z[s] = 0; // distancia a s es 0 + m.insert(MP(0, s)); // insertar (0,s) en m + while (m.SZ > 0) + { + P p = *m.begin(); // p=(coste,nodo) con menor coste + m.erase(m.begin());// elimina este par de m + if (p.Y == t) + return p.X;// cuando nodo es t, acaba + // para cada nodo adjacente al nodo p.Y + for (int i = 0; i < (int)A[p.Y].SZ; i++) + { + // q = (coste hasta nodo adjacente, nodo adjacente) + P q(p.X + A[p.Y][i].X, A[p.Y][i].Y); + // si q.X es la menor distancia hasta q.Y + if (q.X < z[q.Y]) + { + m.erase(MP(z[q.Y], q.Y));// borrar anterior + m.insert(q); // insertar q + z[q.Y] = q.X; // actualizar distancia + prec[q.Y] = p.Y; // actualizar predecesores + } + } + } + return -1; +} + +double dist(int id1, int id2) +{ + return sqrt(SQ(X1[id1] - X1[id2]) + SQ(Y1[id1] - Y1[id2]) + 25.0 * SQ(F[id1] - F[id2])); +} + +void setCost(int id1, int id2, double cost) +{ + /*for (int i = 0; i < A[id1].size(); ++i) + { + if (A[id1][i].Y == id2) + { + if (A[id1][i].X > cost) + { + A[id1][i].X = cost; + return; + } + } + }*/ + + A[id1].PB(MP(cost, id2)); +} + +void addEdgeToGraph(int id1, int id2, const string &act) +{ + if (act[0] == 'w') + { + double cost = dist(id1, id2); + + setCost(id1, id2, cost); + setCost(id2, id1, cost); + } + else if (act[0] == 'l') + { + setCost(id1, id2, 1.0); + setCost(id2, id1, 1.0); + } + else if (act[0] == 's') + { + double cost = dist(id1, id2); + + setCost(id1, id2, cost); + setCost(id2, id1, cost); + } + else if (act[0] == 'e') + { + setCost(id1, id2, 1.0); + + double cost = 3.0 * dist(id1, id2); + setCost(id2, id1, cost); + } +} + +int main() +{ + bool first = true; + while (cin >> N >> M) + { + if (first) + first = false; + else + cout << endl; + + for (int i = 0; i < N; ++i) + { + A[i].clear(); + cin >> F[i] >> X1[i] >> Y1[i]; + } + + int id1, id2;// ids of the nodes to connect + string act; + + for (int i = 0; i < M; ++i) + { + cin >> id1 >> id2 >> act; + addEdgeToGraph(id1, id2, act); + } + + int Q;// number of queries + cin >> Q; + + int a, b; + + for (int i = 0; i < Q; ++i) + { + cin >> a >> b; + + dijkstra(a, b); + + int c = b; + stack path; + + while (c != a) + { + path.push(c); + c = prec[c]; + } + + path.push(a); + + while (!path.empty()) + { + cout << path.top(); + path.pop(); + + if (!path.empty()) + cout << " "; + } + + cout << endl; + } + } + + return 0; +} diff --git a/12681.cpp b/12681.cpp new file mode 100644 index 0000000..002418a --- /dev/null +++ b/12681.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int n, m, p[111]; +char a[1111], b[111]; + +void makestr() +{ + a[0] = 'L', a[1] = 'L', a[2] = 'R'; + for (int i = 3; i <= 10; ++i) + { + int t = (int)pow(2, i - 1), u = (int)pow(2, i - 2); + a[t - 1] = 'L'; + for (int j = 0; j < t - 1; ++j) + { + a[j + t] = a[j]; + } + a[t + u - 1] = 'R'; + } +} + +void init() +{ + p[0] = -1; + int i = 0, j = -1; + while (i < m) + { + if (j == -1 || b[i] == b[j]) + { + ++i, ++j, p[i] = j; + } + else + { + j = p[j]; + } + } +} + +int kmp() +{ + int i = 0, j = 0, ans = 0; + while (i < n) + { + if (j == -1 || b[j] == a[i]) + { + ++i, ++j; + } + else + { + j = p[j]; + } + if (j == m) + { + return true; + } + } + return false; +} + +int main() +{ + makestr(); + int T, k; + scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + scanf("%d", &k); + scanf("%s", b); + n = (k > 10 ? 1023 : (int)pow(2, k)); + m = strlen(b); + init(); + printf("Case %d: %s\n", t, kmp() ? "Yes" : "No"); + } + return 0; +} diff --git a/12682.cpp b/12682.cpp new file mode 100644 index 0000000..c42eff6 --- /dev/null +++ b/12682.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +set mem[6]; +int N; +int isSeparate(char c) +{ + static string separate = ".,:;?!"; + return separate.find(c) != string::npos; +} +void memorize(int n, string stmt[]) +{ + for (int j = 1; j <= N; j++) + { + for (int k = 0; k + j - 1 < n; k++) + { + string mm = ""; + for (int p = 0; p < j; p++) + { + if (p) + mm += "*"; + mm += stmt[k + p]; + } + mem[j].insert(mm); + } + } +} +int check(int n, string stmt[]) +{ + for (int j = 1; j <= N; j++) + { + for (int k = 0; k + j - 1 < n; k++) + { + string mm = ""; + for (int p = 0; p < j; p++) + { + if (p) + mm += "*"; + mm += stmt[k + p]; + } + if (mem[j].find(mm) == mem[j].end()) + return 1; + } + } + return 0; +} +void build() +{ + for (int i = 1; i <= N; i++) + mem[i].clear(); + mem[1].insert("joe"); + string stmt[128]; + char s[32]; + for (int i = 0;; i++) + { + int end = 0, n = 0; + while (scanf("%s", s) == 1) + { + if (isSeparate(s[0])) + break; + if (s[0] == '*') + { + end = 1; + break; + } + stmt[n++] = s; + } + if (end) + break; + for (int j = 0; j < n; j++) + { + for (int k = 0; k < stmt[j].length(); k++) + stmt[j][k] = tolower(stmt[j][k]); + } + memorize(n, stmt); + } +} +void query() +{ + string stmt[128], stmt2[128]; + int end = 0; + char s[32]; + for (int i = 0; !end; i++) + { + int n = 0; + while (scanf("%s", s) == 1) + { + if (isSeparate(s[0])) + break; + if (s[0] == '#') + { + end = 1; + break; + } + stmt[n++] = s; + } + for (int j = 0; j < n; j++) + { + stmt2[j] = stmt[j]; + for (int k = 0; k < stmt[j].length(); k++) + stmt2[j][k] = tolower(stmt2[j][k]); + } + for (int j = 0; j < n; j++) + { + if (mem[1].find(stmt2[j]) == mem[1].end()) + { + printf("What does the word \"%s\" mean?\n", stmt[j].c_str()); + mem[1].insert(stmt2[j]); + } + } + if (check(n, stmt2)) + { + printf("What does the sentence \""); + for (int j = 0; j < n; j++) + { + printf("%s%c", stmt[j].c_str(), j == n - 1 ? '"' : ' '); + } + puts(" mean?"); + memorize(n, stmt2); + } + } +} +int main() +{ + int cases = 0; + while (scanf("%d", &N) == 1) + { + if (cases++) + puts(""); + build(); + printf("Learning case %d\n", cases); + query(); + } + return 0; +} +/* +2 +hello sam . say hello to everybody . good bye . +* +say hi to everybody . +hi to everybody . +hello joe . +swerc +# +4 +Joe is my name . I have a dog . My dog is +a +cinnamon coloured golden retriever +. The name of my father is Ben and the name +of my mother is Linda . +* +Hello Joe , how are you ? Are you my friend ? +Last day you told me : the name of my mother is Linda . +You also told me : the name of my father is Ben . +How are you my friend ? +# +*/ diff --git a/12683.cpp b/12683.cpp new file mode 100644 index 0000000..3af9e43 --- /dev/null +++ b/12683.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define N 100100 +#define M 26 + +typedef long long ll; + +ll n, p; +int a[M]; + +int main() +{ + for (int i = 0; i < M; ++i) + { + if (i == 25) + { + a[i] = 16; + } + else if (i >= 20) + { + a[i] = 10 + i - 20 + 1; + } + else if (i >= 15) + { + a[i] = 10; + } + else if (i >= 10) + { + a[i] = 6 + i - 10; + } + else if (i >= 5) + { + a[i] = 5; + } + else + { + a[i] = i + 1; + } + } + while (scanf("%lld", &n) && n != -1) + { + bool state = true; + ll ans = 0; + if (n <= 25) + { + printf("%d\n", a[n]); + } + else + { + while (n > 25) + { + ll p = 1; + ll cnt = 0; + int t = 1; + ll a = 1; + ll b = 0; + while (p * 5 <= n) + { + if (t % 2 == 1) + { + a *= 5; + } + else + { + a = 3 * a + 2 * (p - a); + } + cnt += p; + t++; + p *= 5; + } + if (state) + { + ans += a; + } + else + { + ans += p - a; + } + if (cnt % 2 == 1) + { + state = !state; + } + n -= p; + } + if (state) + { + ans += a[n]; + } + else + { + ans += n + 1 - a[n]; + } + printf("%lld\n", ans); + } + } + return 0; +} diff --git a/12684.cpp b/12684.cpp new file mode 100644 index 0000000..fb1791c --- /dev/null +++ b/12684.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +vector g[128]; +int visited[128]; +int A[128], An, color[128]; +int used[128]; +int dfs(int idx) +{ + if (idx == An) + return 1; + for (int c = (idx == 0 ? 4 : 1); c <= 4; c++) + { + int u = A[idx], v, ok = 1; + for (int i = 0; i < g[u].size() && ok; i++) + { + v = g[u][i]; + if (used[v] && color[v] == c) + ok = 0; + } + if (ok) + { + used[u] = 1; + color[u] = c; + if (dfs(idx + 1)) + return 1; + used[u] = 0; + } + } + return 0; +} +void bfs(int st) +{ + queue Q; + int u; + An = 0; + Q.push(st), visited[st] = 1; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + A[An++] = u; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (visited[v] == 0) + { + visited[v] = 1; + Q.push(v); + } + } + } + memset(used, 0, sizeof(used)); + dfs(0); +} +int main() +{ + char line[128]; + int x, y, n, cases = 0; + while (scanf("%d", &n) == 1) + { + while (getchar() != '\n') + ; + for (int i = 0; i <= n; i++) + g[i].clear(); + while (gets(line)) + { + if (line[0] == '\0') + break; + sscanf(line, "%d-%d", &x, &y); + g[x].push_back(y); + g[y].push_back(x); + } + for (int i = 1; i <= n; i++) + { + sort(g[i].begin(), g[i].end()); + g[i].resize(distance(g[i].begin(), unique(g[i].begin(), g[i].end()))); + } + memset(visited, 0, sizeof(visited)); + for (int i = 1; i <= n; i++) + { + if (visited[i] == 0) + { + bfs(i); + } + } + if (cases++) + puts(""); + for (int i = 1; i <= n; i++) + printf("%d %d\n", i, color[i]); + } + return 0; +} +/* +8 +1-2 +3-1 +4-5 +4-8 +1-7 +1-4 +7-1 +2-4 +1-8 +6-7 +2-3 +1-5 +1-6 +7-6 +7-8 +2-5 +7-1 +3-4 +5-6 +7-8 +*/ diff --git a/12685.cpp b/12685.cpp new file mode 100644 index 0000000..f60d1b0 --- /dev/null +++ b/12685.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +char S[131072], T[131072]; +const int mod = 21092013; +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s %s", S, T); + stack stk; + for (int i = 0; S[i]; i++) + { + if (S[i] == 'L' || S[i] == 'R') + stk.push(S[i]); + else if (!stk.empty()) + stk.pop(); + } + int ret = 1, lson = 1, rson = 1; + for (int i = 0; T[i]; i++) + { + if (T[i] == 'L') + ret = (ret + lson) % mod, rson = (rson + lson) % mod; + else if (T[i] == 'R') + ret = (ret + rson) % mod, lson = (lson + rson) % mod; + else + { + if (!stk.empty()) + { + ret = (ret + 1) % mod; + if (stk.top() == 'L') + rson++; + else + lson++; + stk.pop(); + } + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/12686.cpp b/12686.cpp new file mode 100644 index 0000000..bf2e55c --- /dev/null +++ b/12686.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +bool cmp(pair a, pair b) +{ + if (a.first != b.first) + return a.first > b.first; + return a.second < b.second; +} +int main() +{ + char s[1024]; + int day = 0, N; + map R[7], Top; + while (scanf("%s", s) == 1) + { + if (!strcmp(s, "")) + { + day = (day + 1) % 7; + for (map::iterator it = R[day].begin(); + it != R[day].end(); it++) + Top[it->first] -= it->second; + R[day].clear(); + while (scanf("%s", s) == 1) + { + if (!strcmp(s, "")) + break; + if (strlen(s) < 4) + continue; + R[day][s]++, Top[s]++; + } + } + else if (!strcmp(s, "> A; + for (map::iterator it = Top.begin(); + it != Top.end(); it++) + if (it->second > 0) + A.push_back(make_pair(it->second, it->first)); + sort(A.begin(), A.end(), cmp); + int M = 0; + printf("\n", N); + for (int i = 0; i < A.size(); i++) + { + printf("%s %d\n", A[i].second.c_str(), A[i].first); + M++; + if (i + 1 < A.size() && A[i].first > A[i + 1].first) + { + if (M >= N) + break; + } + } + printf("\n"); + } + } + return 0; +} +/* + +imagine you are in the hiring process of a company whose +main business is analyzing the information that appears +in the web + + +a simple test consists in writing a program for +maintaining up to date a set of trending topics + + +you will be hired depending on the efficiency of your solution + + + +they provide you with a file containing the text +corresponding to a highly active blog + + +the text is organized daily and you have to provide the +sorted list of the n most frequent words during last week +when asked + + +each input file contains one test case the text corresponding +to a day is delimited by tag text + + +the query of top n words can appear between texts corresponding +to two different days + + + +blah blah blah blah blah blah blah blah blah +please please please + + +*/ diff --git a/12688.cpp b/12688.cpp new file mode 100644 index 0000000..6ecdaac --- /dev/null +++ b/12688.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + if (n == 2) + puts("16"); + else if (n == 3) + puts("17745"); + else if (n == 4) + puts("1064918960"); + else if (n == 5) + puts("3271331573452800"); + else if (n == 6) + puts("504061943351318476800000"); + } + return 0; +} diff --git a/12689.cpp b/12689.cpp new file mode 100644 index 0000000..972bb6d --- /dev/null +++ b/12689.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +const int MAXN = 100000 + 10; + +int p[MAXN], cnt[MAXN]; +bool vis[MAXN]; +int N, M, X; + +void sieve() +{ + for (int i = 0; i < MAXN; ++i) + vis[i] = 0; + M = 0; + for (int i = 2; i < MAXN; ++i) + { + if (vis[i]) + continue; + p[M++] = i; + for (int j = i + i; j < MAXN; j += i) + vis[j] = true; + } +} + +struct Node +{ + int p, t, c; + Node() {} + Node(int _p, int _t, int _c) : p(_p), t(_t), c(_c) {} + bool operator<(const Node &rhs) const + { + return c > rhs.c; + } +}; + +vector V; + +int main() +{ + sieve(); + while (scanf("%d%d", &N, &X) == 2 && N) + { + V.clear(); + for (int i = 0; i < M; ++i) + { + if (p[i] > N) + break; + int c = 0; + for (long long j = p[i]; j <= N; j *= p[i]) + c += N / j; + for (int k = 1; c / k >= X; ++k) + V.push_back(Node(p[i], k == 1, c / k)); + } + sort(V.begin(), V.end()); + for (int i = 0; i <= N; ++i) + cnt[i] = 1; + long long ret = 0, tot = 0, mul = 1; + for (int i = 0; i < (int)V.size(); ++i) + { + if (i == 0 || V[i].c != V[i - 1].c) + { + ret += tot * (tot - 1) / 2; + tot = 0; + } + if (V[i].c < X) + break; + if (V[i].t == 1) + tot += mul; + else + tot += mul / cnt[V[i].p]; + mul /= cnt[V[i].p]; + ++cnt[V[i].p]; + mul *= cnt[V[i].p]; + } + ret += tot * (tot - 1) / 2; + printf("%lld\n", ret); + } + return 0; +} diff --git a/12690.cpp b/12690.cpp new file mode 100644 index 0000000..483fa55 --- /dev/null +++ b/12690.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +typedef long long ULL; + +int main() +{ + int n, m; + while (scanf("%d%d", &n, &m) == 2 && n) + { + if (n > m) + { + swap(n, m); + } + ULL N = n, M = m, K = (n+1)/2; + ULL S1 = K*(K+1)/2; + ULL S2 = K*(K+1)*(2*K+1)/6; + ULL S3 = K*K*(K+1)*(K+1)/4; + cout << 8*S3-(4*M+4*N+20)*S2+((2*N+6)*(M+2)+2*N+4)*S1-(N+2)*(M+2)*K << endl; + } + return 0; +} diff --git a/12691.cpp b/12691.cpp new file mode 100644 index 0000000..4d98f12 --- /dev/null +++ b/12691.cpp @@ -0,0 +1,271 @@ +#include + +using namespace std; + +vector E[600]; +int du[4], T, n, m; +bool flag; +vector> ans; +bool vis[600]; + +void dfs2(int k) +{ + if (vis[k]) + return; + vis[k] = true; + for (int i = E[k].size() - 1; i >= 0; --i) + dfs2(E[k][i]); +} + +bool check() +{ + for (int i = 1; i <= n; ++i) + vis[i] = false; + dfs2(1); + for (int i = 1; i <= n; ++i) + if (!vis[i]) + return false; + return true; +} + +int dfs(int k, int pre) +{ + if (E[k].size() == 2) + { + for (int i = 0; i < 2; ++i) + if (E[k][i] != pre) + return 1 + dfs(E[k][i], k); + } + else + return 1; +} + +void gao0() +{ + if (du[0] == 0 && du[1] == 0 && du[3] == 0) + { + if (du[2] >= 6 && ((du[2] % 6) == 0)) + { + ans.push_back(make_pair(0, (du[2] - 6) / 6)); + } + } +} + +void gao1() +{ + if (du[0] == 0 && du[1] == 2 && du[3] == 0) + { + if ((du[2] % 2) == 1) + { + ans.push_back(make_pair(1, du[2] / 2)); + } + } +} + +void gao2() +{ + if (du[0] == 0 && du[1] == 2 && du[3] == 0) + { + if ((du[2] >= 4) && (du[2] % 5 == 4)) + { + ans.push_back(make_pair(2, du[2] / 5)); + } + } +} + +void gao3() +{ + if (du[0] == 0 && du[1] == 3 && du[3] == 1) + { + if ((du[2] >= 2) && (du[2] % 5 == 2)) + { + int x = 1; + while (E[x].size() != 3) + ++x; + int l[3]; + for (int i = 0; i < 3; ++i) + l[i] = dfs(E[x][i], x); + sort(l, l + 3); + if (l[1] != l[2]) + return; + if (l[0] * 2 != l[1]) + return; + if (l[0] + l[1] + l[2] + 1 != n) + return; + + ans.push_back(make_pair(3, du[2] / 5)); + } + } +} + +void gao4() +{ + if (du[0] == 0 && du[1] == 3 && du[3] == 1) + { + if ((du[2] >= 1) && (du[2] % 4 == 1)) + { + int x = 1; + while (E[x].size() != 3) + ++x; + int l[3]; + for (int i = 0; i < 3; ++i) + l[i] = dfs(E[x][i], x); + sort(l, l + 3); + if (l[0] != l[1]) + return; + if (l[0] * 2 != l[2]) + return; + if (l[0] + l[1] + l[2] + 1 != n) + return; + + ans.push_back(make_pair(4, du[2] / 4)); + } + } +} + +void gao5() +{ + if (du[0] == 0 && du[1] == 2 && du[3] == 0) + { + if ((du[2] >= 4) && (du[2] % 5 == 4)) + { + ans.push_back(make_pair(5, du[2] / 5)); + } + } +} + +void gao6() +{ + if (du[0] == 0 && du[1] == 1 && du[3] == 1) + { + if ((du[2] >= 4) && (du[2] % 6 == 4)) + { + int x = 1; + while (E[x].size() != 3) + ++x; + int l[3]; + for (int i = 0; i < 3; ++i) + l[i] = dfs(E[x][i], x); + sort(l, l + 3); + if (l[1] != l[2]) + return; + if (l[0] * 2 != l[2]) + return; + if (l[0] + l[1] != n) + return; + + ans.push_back(make_pair(6, du[2] / 6)); + } + } +} + +void gao7() +{ + if (du[0] == 0 && du[1] == 2 && du[3] == 0) + { + if ((du[2] >= 2) && (du[2] % 3 == 2)) + { + ans.push_back(make_pair(7, du[2] / 3)); + } + } +} + +void gao8() +{ + if (du[0] == 0 && du[1] == 0 && du[3] == 2) + { + if ((du[2] >= 4) && (du[2] % 7 == 4)) + { + int x = 1; + while (E[x].size() != 3) + ++x; + int l[3]; + for (int i = 0; i < 3; ++i) + l[i] = dfs(E[x][i], x); + sort(l, l + 3); + if (l[1] != l[2]) + return; + if (l[0] * 3 != l[1]) + return; + if (l[0] + l[1] + l[2] - 1 != n) + return; + + ans.push_back(make_pair(8, du[2] / 7)); + } + } +} + +void gao9() +{ + if (du[0] == 0 && du[1] == 1 && du[3] == 1) + { + if ((du[2] >= 4) && (du[2] % 6 == 4)) + { + int x = 1; + while (E[x].size() != 3) + ++x; + int l[3]; + for (int i = 0; i < 3; ++i) + l[i] = dfs(E[x][i], x); + sort(l, l + 3); + if (l[1] != l[2]) + return; + if (l[0] * 2 != l[2]) + return; + if (l[0] + l[1] != n) + return; + + ans.push_back(make_pair(9, du[2] / 6)); + } + } +} + +int main() +{ + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d", &n, &m); + for (int i = 1; i <= n; ++i) + E[i].clear(); + for (int i = 0; i < m; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + E[x].push_back(y); + E[y].push_back(x); + } + memset(du, 0, sizeof(du)); + flag = check(); + for (int i = 1; i <= n; ++i) + { + int t = E[i].size(); + if (t >= 4) + flag = false; + du[t]++; + } + ans.clear(); + if (flag) + { + gao0(); + gao1(); + gao2(); + gao3(); + gao4(); + gao5(); + gao6(); + gao7(); + gao8(); + gao9(); + } + printf("Case %d: %d\n", cas, ans.size()); + sort(ans.begin(), ans.end()); + for (int i = 0; i < ans.size(); ++i) + { + printf("%d %d\n", ans[i].first, ans[i].second); + } + if (cas < T) + puts(""); + } + return 0; +} diff --git a/12692.cpp b/12692.cpp new file mode 100644 index 0000000..7c0d308 --- /dev/null +++ b/12692.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +int n, k, g; +int a[30000], zone[30000], f[30000]; +int idx[30000]; +int T; +int X, Y; + +void add(int x) +{ + for (int i = x; i <= n; i += (i & -i)) + f[i]++; +} + +int sum(int x) +{ + int ret = 0; + for (int i = x; i > 0; i -= (i & -i)) + ret += f[i]; + return ret; +} + +int main() +{ + scanf("%d", &T); + int cas = 0; + while (T--) + { + scanf("%d%d", &n, &k); + for (int i = 1; i <= n; i++) + scanf("%d", a + i); + g = (n + k - 1) / k; + for (int i = 0; i < g; i++) + zone[i] = i * k + 1; + + for (int i = 1; i <= n; i++) + { + int p = (a[i] - 1) / k; + a[i] = zone[p]++; + } + + X = 0; + for (int i = 1; i <= n; i++) + X = max(X, abs(a[i] - i)); + + Y = 0; + for (int i = 1; i <= n; i++) + f[i] = 0; + for (int i = n; i > 0; i--) + { + int now = i + sum(a[i]); + Y += now - a[i]; + add(a[i]); + } + cas++; + printf("Case %d: %d\n", cas, Y - X); + } + return 0; +} diff --git a/12693.cpp b/12693.cpp new file mode 100644 index 0000000..894e205 --- /dev/null +++ b/12693.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +const int MAXN = 100 + 10, MAXW = 1000 + 10, MOD = 10007; + +int dp[MAXN][MAXN][MAXW], C[MAXN][MAXN]; +int cnt[MAXW], A[MAXN], B[MAXN]; +int N, M, W; + +int main() +{ + for (int i = 0; i < MAXN; ++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; + } + } + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d", &N, &W); + M = 0; + for (int i = 0; i <= W; ++i) + cnt[i] = 0; + for (int i = 0; i < N; ++i) + { + int x; + scanf("%d", &x); + ++cnt[x]; + } + for (int i = W; i >= 0; --i) + { + if (!cnt[i]) + continue; + A[++M] = i; + B[M] = cnt[i]; + } + for (int i = 0; i <= M; ++i) + { + for (int j = 0; j <= N; ++j) + { + for (int k = 0; k <= W; ++k) + { + dp[i][j][k] = 0; + } + } + } + dp[0][0][0] = 1; + for (int i = 1; i <= M; ++i) + { + for (int j = 0; j <= N; ++j) + { + for (int k = 0; k <= W; ++k) + { + if (dp[i - 1][j][k] == 0) + continue; + for (int x = 0; x <= B[i]; ++x) + { + if (k + x * A[i] > W) + break; + dp[i][j + x][k + x * A[i]] += dp[i - 1][j][k] * C[j + x][x] % MOD; + dp[i][j + x][k + x * A[i]] %= MOD; + } + } + } + } + int ret = 0; + for (int now = 1; now <= W; ++now) + { + int gap = W - now, used = 0, st = 0; + for (int i = M; i > 0; --i) + { + if (A[i] <= gap) + { + used += A[i] * B[i]; + } + else + { + st = i; + break; + } + } + if (used > now) + continue; + for (int k = 0; k <= N; ++k) + { + if (dp[st][k][now - used] == 0) + continue; + int tmp = dp[st][k][now - used], tot = k; + for (int i = M; i > 0; --i) + { + if (A[i] <= gap) + { + tmp = tmp * C[tot + B[i]][B[i]] % MOD; + tot += B[i]; + } + } + ret = (ret + tmp) % MOD; + } + } + printf("Case %d: %d\n", cas, ret); + } + return 0; +} diff --git a/12694.cpp b/12694.cpp new file mode 100644 index 0000000..7006158 --- /dev/null +++ b/12694.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int main() +{ + int testcase, L, R; + scanf("%d", &testcase); + while (testcase--) + { + vector> M; + while (scanf("%d %d", &L, &R) == 2 && L + R) + { + M.push_back(make_pair(R, L)); + } + sort(M.begin(), M.end()); + int ret = 0; + L = 0; + for (int i = 0; i < M.size(); i++) + { + if (L <= M[i].second) + { + L = M[i].first; + ret++; + } + } + printf("%d\n", ret); + } + return 0; +} +/* +3 +0 6 +5 7 +8 9 +5 9 +1 2 +3 4 +0 5 +0 0 +6 10 +5 6 +0 3 +0 5 +3 5 +4 5 +0 0 +1 5 +3 9 +0 0 +*/ diff --git a/12696.cpp b/12696.cpp new file mode 100644 index 0000000..cb00a35 --- /dev/null +++ b/12696.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int n, m = 0; + scanf("%d", &n); + while (n--) + { + int a, b, length, width, depth, weight; + scanf("%d.%d", &a, &b); + length = a * 100 + b; + scanf("%d.%d", &a, &b); + width = a * 100 + b; + scanf("%d.%d", &a, &b); + depth = a * 100 + b; + scanf("%d.%d", &a, &b); + weight = a * 100 + b; + int f = 1; + f &= (length <= 5600 && width <= 4500 && depth <= 2500) || length + width + depth <= 12500; + f &= weight <= 700; + printf("%d\n", f); + m += f; + } + printf("%d\n", m); + return 0; +} diff --git a/12697.cpp b/12697.cpp new file mode 100644 index 0000000..0f93d48 --- /dev/null +++ b/12697.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define MAXN 5024288 +#define INF 0x3f3f3f3f +long long A[MAXN], SM[MAXN], SB[MAXN]; +// binary index tree +int BIT[MAXN << 1]; +void modify(int x, int v, int L) +{ + while (x <= L) + { + BIT[x] = max(BIT[x], v); + x += x & (-x); + } +} +int query(int x) +{ + int ret = -INF; + while (x) + { + ret = max(ret, BIT[x]); + x -= x & (-x); + } + return ret; +} +int main() +{ + int testcase, N, X; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &X); + for (int i = 1; i <= N; i++) + scanf("%lld", A + i); + + for (int i = 1; i <= N; i++) + SM[i] = SM[i - 1] + A[i]; + for (int i = 0; i <= N; i++) + SB[i] = SM[i]; + for (int i = 1; i <= N + 1; i++) + BIT[i] = -INF; + sort(SB, SB + N + 1); + + int M = unique(SB, SB + N + 1) - SB; + int ret = INF; + for (int i = 0; i <= N; i++) + { + long long v = SM[i] - X; + int pos = upper_bound(SB, SB + M, v) - SB; + if (pos - 1 >= 0 && i) + { + ret = min(ret, i - query(pos)); + // printf("query %d\n", i - query(pos)); + } + pos = upper_bound(SB, SB + M, SM[i]) - SB; + modify(pos, i, M); + // printf("%lld %d\n", v, pos); + } + + printf("%d\n", ret == INF ? -1 : ret); + } + return 0; +} +/* +3 +5 4 +1 2 1 2 1 +6 -2 +-5 -6 -7 -8 -9 -10 +5 3 +-1 1 1 1 -1 +*/ diff --git a/12698.cpp b/12698.cpp new file mode 100644 index 0000000..6ff6666 --- /dev/null +++ b/12698.cpp @@ -0,0 +1,224 @@ +#include + +using namespace std; + +#define REP(i, n) for (int i = 0; i < (n); ++i) +#define SZ(x) x.size() + +const int SIZE = 300010; + +int tn, N; + +struct Point +{ + int x, y; + Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} + bool operator==(const Point &b) { return x == b.x && y == b.y; } + bool operator!=(const Point &b) const { return x != b.x || y != b.y; } + Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } + long long operator*(const Point &b) { return (long long)x * b.y - (long long)y * b.x; } +} tri[50002][4]; + +int ccheck(const Point &X, int id) +{ + bool on_edge = false; + REP(i, 3) + { + long long tmp = (tri[id][i + 1] - tri[id][i]) * (X - tri[id][i]); + if (tmp > 0) + return 0; + if (!tmp) + on_edge = true; + } + if (on_edge) + return -1; + return id; +} + +bool point_to_edge(const Point &X, const Point &L, const Point &R) +{ + return (R - L) * (X - L) < 0; +} + +bool edge_to_edge(const Point &L, const Point &R, const Point &L2, const Point &R2) +{ + if (L.x >= L2.x && L.x <= R2.x) + { + if (L != L2) + return point_to_edge(L, L2, R2); + } + if (L2.x >= L.x && L2.x <= R.x) + { + if (L2 != L) + return !point_to_edge(L2, L, R); + } + if (R.x >= L2.x && R.x <= R2.x) + { + if (R != R2) + return point_to_edge(R, L2, R2); + } + if (R2.x >= L.x && R2.x <= R.x) + { + if (R != R2) + return !point_to_edge(R2, L, R); + } + return 0; +} + +struct Compare +{ + bool operator()(int x, int y) + { + int x_id = x >> 2; + int x_pid = x & 3; + int x_pid2 = x_pid + 1; + if (tri[x_id][x_pid].x > tri[x_id][x_pid2].x) + swap(x_pid, x_pid2); + int y_id = y >> 2; + int y_pid = y & 3; + int y_pid2 = y_pid + 1; + if (tri[y_id][y_pid].x > tri[y_id][y_pid2].x) + swap(y_pid, y_pid2); + + if (tri[x_id][x_pid] == tri[x_id][x_pid2]) + return point_to_edge(tri[x_id][x_pid], tri[y_id][y_pid], tri[y_id][y_pid2]); + if (tri[y_id][y_pid] == tri[y_id][y_pid2]) + return !point_to_edge(tri[y_id][y_pid], tri[x_id][x_pid], tri[x_id][x_pid2]); + return edge_to_edge(tri[x_id][x_pid], tri[x_id][x_pid2], tri[y_id][y_pid], tri[y_id][y_pid2]); + } +}; + +multiset H[400000]; +int now_l, now_r, now_id, now_pid; + +int query(int L, int R, int id) +{ + int res = 0; + if (SZ(H[id])) + { + multiset::iterator it = H[id].lower_bound(tn << 2); + if (it != H[id].end()) + { + res = ccheck(tri[tn][0], (*it) >> 2); + if (res) + return res; + multiset::iterator it2 = it; + it2++; + if (it2 != H[id].end()) + { + res = ccheck(tri[tn][0], (*it2) >> 2); + if (res) + return res; + } + } + if (it != H[id].begin()) + { + it--; + res = ccheck(tri[tn][0], (*it) >> 2); + if (res) + return res; + if (it != H[id].begin()) + { + it--; + res = ccheck(tri[tn][0], (*it) >> 2); + if (res) + return res; + } + } + } + if (L + 1 >= R) + return res; + int mm = (L + R) >> 1; + if (tri[tn][0].x <= mm) + { + res = query(L, mm, id << 1); + if (res) + return res; + } + if (tri[tn][0].x >= mm) + { + res = query(mm, R, (id << 1) | 1); + if (res) + return res; + } + return res; +} + +void add(int L, int R, int id) +{ + if (now_l <= L && now_r >= R) + { + //fprintf(stderr,"[%d,%d]\n",L,R); + H[id].insert((now_id << 2) | now_pid); + return; + } + int mm = (L + R) >> 1; + if (now_l < mm) + add(L, mm, id << 1); + if (now_r > mm) + add(mm, R, (id << 1) | 1); +} + +int main() +{ + int case_n = 1, T; + scanf("%d", &T); + while (T--) + { + int d = 0; + REP(i, 400000) + H[i].clear(); + scanf("%d", &N); + printf("Case %d:\n", case_n++); + tn = 1; + while (N--) + { + char s[4]; + scanf("%s", s); + if (s[0] == 'Q') + { + int x, y; + scanf("%d%d", &x, &y); + if (d > 0) + { + x += tri[d][0].x; + y += tri[d][0].y; + } + tri[tn][0] = tri[tn][1] = Point(x, y); + now_l = now_r = x; + now_id = tn; + d = query(0, 100000, 1); + printf("%d\n", d); + } + else if (s[0] == 'R') + { + REP(i, 3) + scanf("%d%d", &tri[tn][i].x, &tri[tn][i].y); + if (d > 0) + REP(i, 3) + { + tri[tn][i].x += tri[d][0].x; + tri[tn][i].y += tri[d][0].y; + } + tri[tn][3] = tri[tn][0]; + now_id = tn; + REP(i, 3) + { + now_pid = i; + now_l = tri[tn][i].x; + now_r = tri[tn][i + 1].x; + if (now_l != now_r) + { + if (now_l > now_r) + { + swap(now_l, now_r); + } + add(0, 100000, 1); + } + } + tn++; + } + } + } + return 0; +} diff --git a/12699.cpp b/12699.cpp new file mode 100644 index 0000000..025eddc --- /dev/null +++ b/12699.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int MAXN = 1000010; + +double dp[MAXN]; +int vis[MAXN]; +int N, d, s, a, f; + +double solve(int n) +{ + if (vis[n] == f) + return dp[n]; + vis[n] = f; + if (n == 1) + return dp[n] = 0; + if (n & 1) + return dp[n] = (solve(n + 1) + solve(n - 1) + a + s) / 2.0; + else + return dp[n] = solve(n / 2) + d; +} + +int main() +{ + int T; + scanf("%d", &T); + for (f = 1; f <= T; ++f) + { + scanf("%d%d%d%d", &N, &d, &s, &a); + printf("%.3f\n", solve(N) + a + d + s); + } + return 0; +} diff --git a/127.cpp b/127.cpp new file mode 100644 index 0000000..29f6e54 --- /dev/null +++ b/127.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +int main() +{ + map stats; + stats['2'] = 2; + stats['3'] = 3; + stats['4'] = 4; + stats['5'] = 5; + stats['6'] = 6; + stats['7'] = 7; + stats['8'] = 8; + stats['9'] = 9; + stats['T'] = 10; + stats['J'] = 11; + stats['Q'] = 12; + stats['K'] = 13; + stats['A'] = 14; + stats['S'] = 20; + stats['H'] = 30; + stats['D'] = 40; + stats['C'] = 50; + string input; + vector> cards; + stack> piles[52]; + while (cin >> input) + { + if (input == "#") + { + break; + } + cards.clear(); + for (int i = 0; i < 52; i++) + { + while (!piles[i].empty()) + { + piles[i].pop(); + } + } + cards.push_back(pair(stats[input[0]], stats[input[1]])); + for (int i = 0; i < 51; i++) + { + cin >> input; + cards.push_back(pair(stats[input[0]], stats[input[1]])); + } + int pos = 0; + bool ended = false; + while (!cards.empty()) + { + piles[pos].push(cards[0]); + cards.erase(cards.begin()); + ++pos; + bool mademove = true; + while (mademove) + { + mademove = false; + for (int i = 0; i < pos; i++) + { + if (i - 3 >= 0) + { + if (piles[i].top().first == piles[i - 3].top().first || piles[i].top().second == piles[i - 3].top().second) + { + piles[i - 3].push(piles[i].top()); + piles[i].pop(); + mademove = true; + break; + } + } + if (i - 1 >= 0) + { + if (piles[i].top().first == piles[i - 1].top().first || piles[i].top().second == piles[i - 1].top().second) + { + piles[i - 1].push(piles[i].top()); + piles[i].pop(); + mademove = true; + break; + } + } + } + if (mademove == true) + { + for (int i = 0; i < pos; i++) + { + if (piles[i].empty()) + { + if (i == pos - 1) + { + --pos; + } + else + { + for (int j = i + 1; j < pos; j++) + { + swap(piles[j], piles[j - 1]); + } + --pos; + } + break; + } + } + } + } + } + cout << pos; + if (pos == 1) + { + cout << " pile remaining:"; + } + else + { + cout << " piles remaining:"; + } + for (int i = 0; i < pos; i++) + { + cout << " " << piles[i].size(); + } + cout << endl; + } + return 0; +} diff --git a/12700.cpp b/12700.cpp new file mode 100644 index 0000000..688f1c6 --- /dev/null +++ b/12700.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int main() +{ + char s[1024]; + int n, cases = 0; + scanf("%*d"); + while (scanf("%d", &n) == 1) + { + scanf("%s", s); + int c[128] = {}; + for (int i = 0; i < n; i++) + c[s[i]]++; + printf("Case %d: ", ++cases); + if (c['B'] > 0 && c['W'] == 0 && c['T'] == 0) + puts("BANGLAWASH"); + else if (c['W'] > 0 && c['B'] == 0 && c['T'] == 0) + puts("WHITEWASH"); + else if (c['A'] == n) + puts("ABANDONED"); + else if (c['B'] > c['W']) + printf("BANGLADESH %d - %d\n", c['B'], c['W']); + else if (c['B'] < c['W']) + printf("WWW %d - %d\n", c['W'], c['B']); + else + printf("DRAW %d %d\n", c['W'], c['T']); + } + return 0; +} +/* +6 +3 +BBB +3 +WWW +3 +BWB +4 +BWWT +3 +BTW +2 +AA +*/ diff --git a/12701.cpp b/12701.cpp new file mode 100644 index 0000000..3f5c92a --- /dev/null +++ b/12701.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +struct Edge +{ + int to, w, i; + Edge(int a = 0, int b = 0, int c = 0) : to(a), w(b), i(c) {} +}; +vector g[16]; +int n, used[1 << 16]; +double dp[1 << 16]; +double dfs(int); + +void dfs2(int u, int mask, int w, int e, int o) +{ + if (e) + { + dp[o] = min(dp[o], dfs(mask) + (double)w / e); + } + for (int i = 0; i < g[u].size(); i++) + { + if ((mask >> g[u][i].i) & 1) + { + int v = g[u][i].to; + dfs2(v, mask ^ (1 << g[u][i].i), w + g[u][i].w, e + 1, o); + } + } +} +double dfs(int mask) +{ + if (mask == 0) + return 0; + if (used[mask]) + return dp[mask]; + used[mask] = 1; + double &ret = dp[mask]; + ret = 1e+30; + for (int i = 0; i < n; i++) + { + dfs2(i, mask, 0, 0, mask); + } + return ret; +} +int main() +{ + int x, y, w; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; i++) + g[i].clear(); + for (int i = 0; i < n - 1; i++) + { + scanf("%d %d %d", &x, &y, &w); + g[x].push_back(Edge(y, w, i)); + g[y].push_back(Edge(x, w, i)); + } + memset(used, 0, sizeof(used)); + printf("%.4lf\n", dfs((1 << (n - 1)) - 1)); + } + return 0; +} + +/* + 4 + 0 2 1 + 1 2 2 + 2 3 3 + 6 + 0 1 10000 + 0 2 10000 + 0 3 1 + 0 4 1 + 0 5 10000 + 0 */ \ No newline at end of file diff --git a/12702.cpp b/12702.cpp new file mode 100644 index 0000000..e6dd3ab --- /dev/null +++ b/12702.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int n, m, q, r; + int g[105][105], mg[15][15]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%d", &g[i][j]); + + scanf("%d %d", &q, &r); + for (int i = 0; i < q; i++) + for (int j = 0; j < r; j++) + scanf("%d", &mg[i][j]); + + int cx = q / 2, cy = r / 2; + int ret[105][105] = {}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + ret[i][j] |= g[i][j]; + if (g[i][j] == 0) + continue; + int ox = i - cx, oy = j - cy; + for (int a = 0; a < q; a++) + { + for (int b = 0; b < r; b++) + { + if (ox + a < 0 || ox + a >= n) + continue; + if (oy + b < 0 || oy + b >= m) + continue; + ret[ox + a][oy + b] |= mg[a][b]; + } + } + } + } + printf("Case %d:\n", ++cases); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + printf("%d%c", ret[i][j], j == m - 1 ? '\n' : ' '); + } + } + } + return 0; +} +/* +2 +3 4 +0 0 0 0 +0 1 1 0 +0 0 0 0 +3 3 +0 1 0 +1 1 1 +0 1 0 +4 4 +0 0 0 0 +0 1 0 0 +0 0 1 0 +0 0 0 0 +3 3 +1 1 1 +1 1 1 +1 1 1 +*/ diff --git a/12703.cpp b/12703.cpp new file mode 100644 index 0000000..e71228e --- /dev/null +++ b/12703.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +vector> factor(int n) +{ + vector> R; + for (int i = 2, j; i * i <= n; i++) + { + if (n % i == 0) + { + for (j = 0; n % i == 0; n /= i, j++) + ; + R.push_back(make_pair(i, j)); + } + } + if (n != 1) + R.push_back(make_pair(n, 1)); + return R; +} +int main() +{ + int testcase; + int n, a, b; + long long f[50] = {0, 1}; + for (int i = 2; i < 50; i++) + f[i] = f[i - 1] + f[i - 2]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &n, &a, &b); + map R; + vector> F; + + F = factor(a); + for (vector>::iterator it = F.begin(); + it != F.end(); it++) + R[it->first] += f[n - 1] * it->second; + F = factor(b); + for (vector>::iterator it = F.begin(); + it != F.end(); it++) + R[it->first] += f[n] * it->second; + + for (map::iterator it = R.begin(); + it != R.end(); it++) + printf("%d %lld\n", it->first, it->second); + puts(""); + } + return 0; +} +/* +2 +2 2 3 +13 2 3 +*/ diff --git a/12704.cpp b/12704.cpp new file mode 100644 index 0000000..455662e --- /dev/null +++ b/12704.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + scanf("%*d"); + double x, y, r; + while (scanf("%lf%lf%lf", &x, &y, &r) == 3) + { + printf("%.2lf %.2lf\n", r - hypot(x, y), r + hypot(x, y)); + } + return 0; +} diff --git a/12705.cpp b/12705.cpp new file mode 100644 index 0000000..9379edf --- /dev/null +++ b/12705.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + char s[1024]; + vector C; + for (int i = 1; i <= 6; i++) + for (int j = 1; j <= 6; j++) + C.push_back(i + j); + sort(C.begin(), C.end()); + gets(s); + while (gets(s)) + { + int cnt[128] = {}; + vector M; + for (int i = 0; s[i]; i++) + cnt[s[i]]++; + for (int i = 'A'; i <= 'Z'; i++) + { + if (cnt[i]) + M.push_back(cnt[i]); + } + for (int i = '0'; i <= '9'; i++) + { + if (cnt[i]) + M.push_back(cnt[i]); + } + sort(M.begin(), M.end(), greater()); + int ret = 0; + for (int i = 0; i < M.size(); i++) + { + ret += M[i] * C[i]; + } + printf("%d\n", ret); + } + return 0; +} +/* +3 +CALL DEA +WALTER WHITE +09AZ +*/ diff --git a/12706.cpp b/12706.cpp new file mode 100644 index 0000000..fd241ae --- /dev/null +++ b/12706.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int A[32767], B[32767]; +int main() +{ + int testcase, n, m, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + map R; + 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]); + R[B[i]]++; + } + int diff = (int)R.size(); + long long ret = 0; + for (int i = 0; i < n; i++) + { + int &x = R[A[i]]; + if (x == 1) + diff--; + else if (x == 0) + diff++; + x--; + if (i - m >= 0) + { + int &y = R[A[i - m]]; + if (y == 0) + diff++; + else if (y == -1) + diff--; + y++; + } + if (diff == 0) + { + long long pos = i - m + 2; + ret += pos * pos; + // printf("pos %d\n", pos); + } + } + printf("%lld\n", ret); + } + return 0; +} + +/* + 2 + 3 2 + 10 11 10 + 10 11 + 4 3 + 10 11 11 10 + 10 11 11 + */ \ No newline at end of file diff --git a/12707.cpp b/12707.cpp new file mode 100644 index 0000000..c8acf37 --- /dev/null +++ b/12707.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +bool cmp(pair a, pair b) +{ + if (a.second != b.second) + return a.second < b.second; + return a.first < b.first; +} +#define INF 0x3f3f3f3f +int main() +{ + int testcase, cases = 0; + int n, s, e; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + vector> D; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &s, &e); + D.push_back(make_pair(s, e)); + } + sort(D.begin(), D.end(), cmp); + vector leftside; + for (int i = 0; i < D.size(); i++) + { + int pos = (int)(lower_bound(leftside.begin(), leftside.end(), D[i].first + 1) - leftside.begin()); + if (pos == leftside.size()) + { + leftside.push_back(D[i].first); + } + else + { + leftside[pos] = D[i].first; + } + } + printf("Case %d: %d\n", ++cases, (int)leftside.size()); + } + return 0; +} + +/* + 2 + 4 + 1 5 + 2 5 + 3 5 + 3 4 + 4 + 1 5 + 2 4 + 3 5 + 3 3 + */ \ No newline at end of file diff --git a/12708.cpp b/12708.cpp new file mode 100644 index 0000000..4207204 --- /dev/null +++ b/12708.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + scanf("%*d"); + for (long long n; scanf("%lld", &n) == 1;) + { + printf("%lld\n", n / 2); + } + return 0; +} diff --git a/12709.cpp b/12709.cpp new file mode 100644 index 0000000..80f1ca8 --- /dev/null +++ b/12709.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + for (int n; scanf("%d", &n) == 1 && n;) + { + int L, W, H, V, maxV, maxH = 0; + while (n--) + { + scanf("%d %d %d", &L, &W, &H); + V = L * W * H; + if (H > maxH || (H == maxH && V > maxV)) + maxH = H, maxV = V; + } + printf("%d\n", maxV); + } + return 0; +} diff --git a/12710.cpp b/12710.cpp new file mode 100644 index 0000000..e152868 --- /dev/null +++ b/12710.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n, L, B, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + long long m0, m1, m2; + m0 = m1 = m2 = 0; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &L, &B); + L--; + if (B % 3 == 2) + {// 2, 1, 2, 1 ... + m1 += (L / 2) * (B - 1); + m2 += ((L + 1) / 2) * (B - 1); + } + else if (B % 3 == 1) + {// 1, 1, 1, 1 ... + m1 += L * (B - 1); + } + else + { + m0 += L * (B - 1); + } + } + printf("Case %d: ", ++cases); + if (m0 + m1 + m2 == 0) + { + puts("Draw"); + } + else if (m1 + m2 == 0) + { + puts("J"); + } + else + { + if (m0 % 2 == 0) + { + if (m1 == 0 || m2 == 0) + { + if (m1 <= 2 && m2 <= 2) + puts("Draw"); + else + puts("J"); + } + else + { + puts("M"); + } + } + else + { + if (m1 == m2) + { + puts("J"); + } + else if (abs(m1 - m2) >= 3) + { + puts("M"); + } + else + { + puts("Draw"); + } + } + } + } + return 0; +} + +/* + 3 + 1 + 5 2 + 2 + 3 2 + 2 6 + 1 + 2 2 + */ \ No newline at end of file diff --git a/12711.cpp b/12711.cpp new file mode 100644 index 0000000..306f5b7 --- /dev/null +++ b/12711.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +const int MAXN = 100 + 10, inf = 0x3f3f3f3f; + +int G[MAXN][MAXN], ID[MAXN]; +int match[MAXN], stk[MAXN]; +int vis[MAXN], dis[MAXN]; +int N, M, K, top; + +bool spfa(int u) +{ + stk[top++] = u; + if (vis[u]) + return true; + vis[u] = true; + for (int i = 1; i <= K; ++i) + { + if (i != u && i != match[u] && !vis[i]) + { + int v = match[i]; + if (dis[v] < dis[u] + G[u][i] - G[i][v]) + { + dis[v] = dis[u] + G[u][i] - G[i][v]; + if (spfa(v)) + return true; + } + } + } + top--; + vis[u] = false; + return false; +} + +int MaxWeightMatch() +{ + for (int i = 1; i <= K; ++i) + ID[i] = i; + for (int i = 1; i <= K; i += 2) + match[i] = i + 1, match[i + 1] = i; + for (int times = 0, flag; times < 3;) + { + memset(dis, 0, sizeof(dis)); + memset(vis, 0, sizeof(vis)); + top = 0; + flag = 0; + for (int i = 1; i <= K; ++i) + { + if (spfa(ID[i])) + { + flag = 1; + int t = match[stk[top - 1]], j = top - 2; + while (stk[j] != stk[top - 1]) + { + match[t] = stk[j]; + swap(t, match[stk[j]]); + --j; + } + match[t] = stk[j]; + match[stk[j]] = t; + break; + } + } + if (!flag) + times++; + if (!flag) + random_shuffle(ID + 1, ID + K + 1); + } + int ret = 0; + for (int i = 1; i <= K; ++i) + if (i < match[i]) + ret += G[i][match[i]]; + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d%d", &N, &M, &K); + memset(G, 0x3f, sizeof(G)); + for (int i = 1; i <= N; ++i) + G[i][i] = 0; + for (int i = 0; i < M; ++i) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + G[u][v] = G[v][u] = w; + } + printf("Case %d: ", cas); + if (K & 1) + { + puts("Impossible"); + continue; + } + for (int k = 1; k <= N; ++k) + { + for (int i = 1; i <= N; ++i) + { + for (int j = 1; j <= N; ++j) + { + G[i][j] = min(G[i][j], G[i][k] + G[k][j]); + } + } + } + for (int i = 1; i <= K; ++i) + { + for (int j = 1; j <= K; ++j) + { + G[i][j] = -G[i][j]; + } + G[i][i] = -inf; + } + printf("%d\n", -MaxWeightMatch()); + } + return 0; +} diff --git a/12712.cpp b/12712.cpp new file mode 100644 index 0000000..3961c6c --- /dev/null +++ b/12712.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int L, M, N; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &L, &M, &N); + int LL = L * L; + long long ret = 0, ways = 1; + for (int i = 1; i < M; i++) + { + ways *= (LL - i + 1); + ways %= 10000000000007LL; + } + for (int i = M; i <= N; i++) + { + ways *= (LL - i + 1); + ways %= 10000000000007LL; + ret += ways; + ret %= 10000000000007LL; + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/12713.cpp b/12713.cpp new file mode 100644 index 0000000..49ab2e8 --- /dev/null +++ b/12713.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +typedef long long LL; + +const LL MOD = 1000003; + +LL fac[MOD + 10], inv[MOD + 10]; +LL PX[MOD + 10], PY[MOD + 10], PZ[MOD + 10]; + +LL pow_mod(LL a, LL n) +{ + LL ret = 1; + for (a %= MOD; n; a = a * a % MOD, n >>= 1) + { + if (n & 1) + ret = ret * a % MOD; + } + return ret; +} + +LL comb(int N, int M) +{ + return fac[N] * inv[M] % MOD * inv[N - M] % MOD; +} + +int main() +{ + int T; + scanf("%d", &T); + PX[0] = PY[0] = PZ[0] = fac[0] = inv[0] = 1; + for (int i = 1; i <= MOD; ++i) + { + fac[i] = fac[i - 1] * i % MOD; + inv[i] = pow_mod(fac[i], MOD - 2); + } + for (int cas = 1; cas <= T; ++cas) + { + LL A, B, X, Y, Z; + scanf("%lld%lld%lld%lld%lld", &A, &B, &X, &Y, &Z); + X %= MOD, Y %= MOD, Z %= MOD; + for (int i = 1; i < MOD; ++i) + { + PX[i] = PX[i - 1] * X % MOD; + PY[i] = PY[i - 1] * Y % MOD; + PZ[i] = PZ[i - 1] * Z % MOD; + } + LL ret = 1; + while (A || B) + { + LL a = A % MOD, b = B % MOD, tmp = 0; + for (int i = 0; i <= a && i <= b; ++i) + { + tmp += comb(a + b - i, a - i) * comb(b, i) % MOD * PX[a - i] % MOD * PY[b - i] % MOD * PZ[i] % MOD; + tmp %= MOD; + } + ret = ret * tmp % MOD; + A /= MOD, B /= MOD; + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/12714.cpp b/12714.cpp new file mode 100644 index 0000000..39040a3 --- /dev/null +++ b/12714.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int x1, y1, x2, y2; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d %d", &x1, &y1, &x2, &y2); + printf("Case %d: ", ++cases); + int vx = y1 - y2, vy = x2 - x1; + int S = max(max(x1, x2), max(y1, y2)); + if (vx < 0 || (vx == 0 && vy < 0)) + vx = -vx, vy = -vy; + if (vx >= 0 && vy >= 0) + printf("0 0 %d %d\n", vx, vy); + else + printf("%d %d %d %d\n", 0, S, vx, S + vy); + } + return 0; +} +/* +2 +4 4 5 5 +9 0 10 0 +*/ diff --git a/12715.cpp b/12715.cpp new file mode 100644 index 0000000..493eddd --- /dev/null +++ b/12715.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +const int MAXN = 100000 + 10; +const int inf = 1e9; + +typedef pair PII; + +struct POS +{ + int p, id; + bool operator<(const POS &rhs) const + { + return p < rhs.p; + } +}; + +struct Node +{ + int l, r; +}; + +bool cmpl(const Node &a, const Node &b) +{ + return (a.l < b.l || (a.l == b.l && a.r < b.r)); +} + +bool cmpr(const Node &a, const Node &b) +{ + return (a.r > b.r || (a.r == b.r && a.l > b.l)); +} + +priority_queue Q; +multiset::iterator it; +multiset S; +Node CL[MAXN], CR[MAXN]; +POS pos[MAXN]; +int ret[MAXN]; +int N, M; + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + scanf("%d%d", &N, &M); + for (int i = 0; i < N; ++i) + { + int l, r, m; + scanf("%d%d", &l, &r); + m = (l + r) >> 1; + if ((r - l) & 1) + { + CL[i] = (Node){l, m}; + CR[i] = (Node){m + 1, r}; + } + else + { + CL[i] = (Node){l, m}; + CR[i] = (Node){m, r}; + } + } + for (int i = 0; i < M; ++i) + { + scanf("%d", &pos[i].p); + pos[i].id = i; + } + sort(pos, pos + M); + sort(CL, CL + N, cmpl); + S.clear(); + while (!Q.empty()) + Q.pop(); + for (int i = 0, j = 0; i < M; ++i) + { + int x = pos[i].p, id = pos[i].id, y; + while (j < N && CL[j].l <= x) + { + Q.push(make_pair(-CL[j].r, j)); + S.insert(CL[j].l); + j++; + } + while (!Q.empty()) + { + PII now = Q.top(); + if (-now.first < x) + { + Q.pop(); + it = S.find(CL[now.second].l); + S.erase(it); + } + else + break; + } + if (S.empty()) + y = x; + else + y = *S.begin(); + ret[id] = max(x - y, 0); + } + sort(CR, CR + N, cmpr); + S.clear(); + while (!Q.empty()) + Q.pop(); + for (int i = M - 1, j = 0; i >= 0; --i) + { + int x = pos[i].p, id = pos[i].id, y; + while (j < N && CR[j].r >= x) + { + Q.push(make_pair(CR[j].l, j)); + S.insert(CR[j].r); + j++; + } + while (!Q.empty()) + { + PII now = Q.top(); + if (now.first > x) + { + Q.pop(); + it = S.find(CR[now.second].r); + S.erase(it); + } + else + break; + } + if (S.empty()) + y = x; + else + y = *S.rbegin(); + ret[id] = max(ret[id], y - x); + } + printf("Case %d:\n", cas); + for (int i = 0; i < M; ++i) + printf("%d\n", ret[i]); + } + return 0; +} diff --git a/12716.cpp b/12716.cpp new file mode 100644 index 0000000..f7df8de --- /dev/null +++ b/12716.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int F[30000005] = {}; +int main() +{ + for (int i = 1; i <= 30000000; i++) + { + for (int j = i << 1; i + j <= 30000000; j += i) + { + if (((i + j) & (j)) == j) + F[i + j]++; + } + } + for (int i = 1; i <= 30000000; i++) + F[i] += F[i - 1]; + int testcase, cases = 0, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + printf("Case %d: %d\n", ++cases, F[n]); + } + return 0; +} diff --git a/12717.cpp b/12717.cpp new file mode 100644 index 0000000..eb4e39c --- /dev/null +++ b/12717.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +vector g[2505]; +int used[25005]; +int in_x[25005], in_y[25005], in_w[25005]; +map, int> shortest_tree; + +void bfs(int source) +{ + queue Q; + int u, v, e = 0; + Q.push(source), used[source] = 1; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (used[v]) + continue; + used[v] = 1; + shortest_tree[make_pair(u, v)] = in_w[e], e++; + Q.push(v); + } + } +} +int main() +{ + int testcase, cases = 0; + int n, m, source; + int x, y, w; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &n, &m, &source); + + for (int i = 1; i <= n; i++) + { + g[i].clear(); + used[i] = 0; + } + shortest_tree.clear(); + + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &x, &y, &w); + g[x].push_back(y); + g[y].push_back(x); + in_x[i] = x, in_y[i] = y, in_w[i] = w; + } + + for (int i = 1; i <= n; i++) + sort(g[i].begin(), g[i].end()); + sort(in_w, in_w + m); + bfs(source); + + printf("Case %d:\n", ++cases); + map, int>::iterator it, jt, kt; + int e = m - 1; + kt = shortest_tree.end(); + for (int i = 0; i < m; i++) + { + it = shortest_tree.find(make_pair(in_x[i], in_y[i])); + jt = shortest_tree.find(make_pair(in_y[i], in_x[i])); + if (it != kt) + w = it->second; + else if (jt != kt) + w = jt->second; + else + w = in_w[e], e--; + printf("%d %d %d\n", in_x[i], in_y[i], w); + } + } + return 0; +} +/* +1 +7 9 5 +2 4 2 +1 4 8 +7 2 6 +3 4 7 +5 7 5 +7 3 9 +6 1 1 +6 3 4 +5 6 3 +*/ diff --git a/12718.cpp b/12718.cpp new file mode 100644 index 0000000..95a5b03 --- /dev/null +++ b/12718.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + char s[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int ret = 0, n = strlen(s); + for (int i = 0; i < n; i++) + { + int cnt[26] = {}, odd = 0; + for (int j = i, m = 1; j < n; j++, m++) + { + cnt[s[j] - 'a']++; + odd += cnt[s[j] - 'a'] % 2 ? 1 : -1; + if (m % 2 == 0 && odd == 0) + ret++; + else if (m % 2 == 1 && odd == 1) + ret++; + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} +/* +4 +acmicpc +aaaaa +isyoursolutionfastenough +abbabababbaba +*/ diff --git a/12719.cpp b/12719.cpp new file mode 100644 index 0000000..22d5699 --- /dev/null +++ b/12719.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +const int MAXC = 150, SIZE = 5000, MAXR = 250; +int CubeSize[SIZE], blockID[30][30][30]; +bool ret[MAXC], can[30][30][30]; + +class DLX +{ +public: + int D[SIZE], U[SIZE], L[SIZE], R[SIZE]; + int CID[SIZE], RID[SIZE], S[MAXC], H[MAXR], sz; + void init(int n) + { + memset(H, -1, sizeof(H)); + for (int i = 1; i <= n; ++i) + { + L[i + 1] = R[i - 1] = i; + U[i] = D[i] = i; + S[i] = 0; + } + L[1] = R[n] = 0; + L[0] = n; + sz = n + 1; + } + void link(int r, int c) + { + D[sz] = D[c], U[sz] = c, U[D[c]] = sz, D[c] = sz; + S[c]++; + CID[sz] = c; + RID[sz] = r; + if (H[r] == -1) + H[r] = L[sz] = R[sz] = sz; + R[L[H[r]]] = sz; + L[sz] = L[H[r]]; + R[sz] = H[r]; + L[H[r]] = sz; + sz++; + } + void remove(int c) + { + L[R[c]] = L[c]; + R[L[c]] = R[c]; + for (int i = D[c]; i != c; i = D[i]) + { + for (int j = R[i]; j != i; j = R[j]) + { + D[U[j]] = D[j], U[D[j]] = U[j]; + S[CID[j]]--; + } + } + } + void resume(int c) + { + L[R[c]] = c; + R[L[c]] = c; + for (int i = U[c]; i != c; i = U[i]) + { + for (int j = L[i]; j != i; j = L[j]) + { + U[D[j]] = j, D[U[j]] = j; + S[CID[j]]++; + } + } + } + void dance(int n, int sum) + { + ret[n + sum] = true; + if (R[0] == 0) + return; + int idx, now = 0; + for (int i = R[0]; i; i = R[i]) + { + if (S[i] > now) + now = S[i], idx = i; + } + remove(idx); + for (int i = D[idx]; i != idx; i = D[i]) + { + if (CubeSize[RID[i]] == 1) + continue; + for (int j = R[i]; j != i; j = R[j]) + remove(CID[j]); + dance(n + 1, sum - CubeSize[RID[i]]); + for (int j = L[i]; j != i; j = L[j]) + resume(CID[j]); + } + resume(idx); + } +} dlx; + +bool check(int x, int y, int z, int p) +{ + for (int i = 0; i < p; ++i) + { + for (int j = 0; j < p; ++j) + { + for (int k = 0; k < p; ++k) + { + if (!can[x + i][y + j][z + k]) + return false; + } + } + } + return true; +} + +void add(int line, int x, int y, int z, int p) +{ + for (int i = 0; i < p; ++i) + { + for (int j = 0; j < p; ++j) + { + for (int k = 0; k < p; ++k) + { + dlx.link(line, blockID[x + i][y + j][z + k]); + } + } + } + CubeSize[line] = p * p * p; +} + +int main() +{ + int a, b, c, m, cas = 0; + while (scanf("%d%d%d%d", &a, &b, &c, &m) == 4 && a) + { + memset(blockID, 0, sizeof(blockID)); + memset(ret, 0, sizeof(ret)); + memset(can, 1, sizeof(can)); + int sum = a * b * c; + while (m--) + { + int x, y, z; + scanf("%d%d%d", &x, &y, &z); + can[x][y][z] = 0; + sum--; + } + m = 0; + for (int x = 1; x <= a; ++x) + { + for (int y = 1; y <= b; ++y) + { + for (int z = 1; z <= c; ++z) + { + if (can[x][y][z]) + blockID[x][y][z] = ++m; + } + } + } + dlx.init(m); + memset(CubeSize, 0, sizeof(CubeSize)); + for (int p = min(a, min(b, c)), line = 0; p >= 1; --p) + { + for (int x = 1; x + p - 1 <= a; ++x) + { + for (int y = 1; y + p - 1 <= b; ++y) + { + for (int z = 1; z + p - 1 <= c; ++z) + { + if (!check(x, y, z, p)) + continue; + add(++line, x, y, z, p); + } + } + } + } + dlx.dance(0, sum); + printf("Case %d:", ++cas); + for (int i = 0; i < MAXC; ++i) + if (ret[i]) + printf(" %d", i); + puts(""); + } + return 0; +} diff --git a/12720.cpp b/12720.cpp new file mode 100644 index 0000000..9262fc0 --- /dev/null +++ b/12720.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + char s[100005]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int n = strlen(s); + int ret = 0; + int p, q, r; +#define mod 1000000007 + q = n / 2, p = q - 1; + for (int i = 0, j = n & 1; i < n; i++, j = !j) + { + if (j) + { + if (n - q > p) + r = (s[q] - '0'), q++; + else + r = (s[p] - '0'), p--; + } + else + { + if (s[p] > s[q]) + r = (s[p] - '0'), p--; + else + r = (s[q] - '0'), q++; + } + ret = (ret << 1) + r; + if (ret >= mod) + ret %= mod; + } + printf("Case #%d: %d\n", ++cases, ret); + } + return 0; +} +/* +3 +00000 +0101101 +100 +*/ diff --git a/12721.cpp b/12721.cpp new file mode 100644 index 0000000..2304e23 --- /dev/null +++ b/12721.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +int dp[128][128], cost[26][128]; +int main() +{ + int testcase, cases = 0; + char S[128]; + int B, P, C, N, M; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s %d", S + 1, &B); + + N = strlen(S + 1); + memset(dp, 0x3f, sizeof(dp)); + memset(cost, 0, sizeof(cost)); + + for (int i = 0; i < 26; i++) + { + scanf("%d", &M); + for (int j = 0; j < M; j++) + { + scanf("%d %d", &P, &C); + int oP = P; + while (oP <= N) + cost[i][oP] += oP / P * C, oP += P; + } + } + + dp[0][0] = 0; + for (int i = 0; i < B; i++) + { + for (int j = 0; j <= N; j++) + { + for (int k = j + 1; k <= N; k++) + { + int c = cost[S[k] - 'a'][i + 1]; + dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + c); + } + } + } + int ret = 0x3f3f3f3f; + for (int i = 1; i <= N; i++) + ret = min(ret, dp[B][i]); + printf("Case #%d: %d\n", ++cases, ret); + } + return 0; +} +/* +2 +abcd 1 +1 1 20 +1 1 15 +1 1 8 +1 1 30 +1 1 2 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + +abaabcbc 4 +2 2 3 4 10 +2 1 4 7 50 +2 1 2 4 20 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 + +*/ diff --git a/12722.cpp b/12722.cpp new file mode 100644 index 0000000..1789aa8 --- /dev/null +++ b/12722.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +char s[2048]; +int g[512][512]; +void show(int n, int m) +{ + for (int i = 0; i < n; i++, puts("")) + for (int j = 0; j < m; j++) + printf("%c", g[i][j]); +} +int main() +{ + int testcase, cases = 0, n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s %d %d", s, &n, &m); + int len = strlen(s); + while (n * m < len) + { + if (n == m) + m <<= 1; + else if (n < m) + n += m; + else + m += n; + } + n = len / m + 1; + int blank = n * m - len; + for (int i = m - 1, k = 0; i >= 0; i--) + { + for (int j = 0; j < n; j++) + { + if (k < len) + { + if (j == n - 1 && blank) + g[j][i] = ' ', blank--; + else + g[j][i] = (int)s[k++]; + } + else + g[j][i] = ' '; + } + } + for (int i = 0, color = 0, x = 0, y = 0; i < n - 1; i++) + { + if (g[i + 1][0] == ' ') + break; + int st = i % 2 ? m - 1 : 0; + int dy = i % 2 ? -1 : 1; + for (int j = st; j >= 0 && j < m; j += dy) + { + if (color % 2 == 0) + { + x = i, y = j; + } + else + { + swap(g[x][y], g[i][j]); + } + color++; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] >= 'a' && g[i][j] <= 'z') + { + g[i][j] = (g[i][j] - 'a' - (i + j + 2) + 2600) % 26 + 'a'; + } + } + } + + printf("Case #%d: ", ++cases); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] == ' ') + continue; + printf("%c", g[i][j]); + } + } + puts(""); + } + return 0; +} +/* +3 +zsvmvjfnroqfdek 3 3 +vnvxjbxylndjhzq 1 1 +uhyeobnqcmayftjvtttrplnraykamqw 50 100 + +10 +gyytetorcanweobhsxhykgumsicihuospggbdicrmmhxdhzfzpmh 15 13 +*/ diff --git a/12723.cpp b/12723.cpp new file mode 100644 index 0000000..3035396 --- /dev/null +++ b/12723.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + double p[128], e[512], C, X; + for (int i = 1; i <= m; i++) + { + scanf("%lf", &p[i]); + } + + for (int i = 1; i <= n; i++) + { + int q; + scanf("%d", &q); + e[i] = 0; + for (int j = 0; j < q; j++) + { + scanf("%lf %lf", &C, &X); + e[i] += C * X; + } + } + + double dp[512] = {}, ret = 0; + dp[1] = 1; + for (int i = 1; i <= n; i++) + { + for (int j = 1; i + j <= n && j <= m; j++) + { + dp[i + j] += dp[i] * p[j]; + } + } + for (int i = 1; i <= n; i++) + { + ret += dp[i] * e[i]; + } + printf("Case #%d: %.6lf\n", ++cases, ret); + } + return 0; +} + +/* + 2 + 2 1 + 1.0 + 2 50 0.5 100 0.5 + 2 10 0.5 20 0.5 + 5 2 + 0.3 0.7 + 5 10 0.2 20 0.3 5 0.1 25 0.35 2 0.05 + 2 20 0.4 40 0.6 + 1 4 1.0 + 3 30 0.8 3 0.1 4 0.1 + 10 1 0.1 2 0.1 3 0.1 4 0.1 5 0.1 6 0.1 7 0.1 8 0.1 9 0.1 10 0.1 + */ \ No newline at end of file diff --git a/12724.cpp b/12724.cpp new file mode 100644 index 0000000..9242717 --- /dev/null +++ b/12724.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int n, m; + char token[128]; + char lines[4096]; + + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + + map R; + + for (int i = 0; i < n; i++) + { + scanf("%s", token); + string acc(token); + sort(token, token + strlen(token)); + R[token] = acc; + } + while (getchar() != '\n') + ; + printf("Case #%d:\n", ++cases); + for (int i = 0; i < m; i++) + { + gets(lines); + + stringstream sin(lines); + string str, p; + map::iterator it; + int f = 0; + + while (sin >> str) + { + p = str; + sort(p.begin(), p.end()); + it = R.find(p); + + if (f) + putchar(' '); + else + f = 1; + + if (it == R.end()) + printf("%s", str.c_str()); + else + printf("%s", it->second.c_str()); + } + puts(""); + } + } + return 0; +} +/* +2 +9 2 +andre copy exam free gimme lemme plox stuff yo +ardne lmeme cpoy yo eaxm polx +adnre gmime fere sfutf polx +4 1 +abacabadabacaba abacaba aba a +a aba acbbaaa adccbbbbaaaaaaa +*/ diff --git a/12725.cpp b/12725.cpp new file mode 100644 index 0000000..b1bb376 --- /dev/null +++ b/12725.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + double N, M, A, B, C; + scanf("%lf %lf %lf %lf", &N, &M, &A, &B); + + C = (M * (A + B) - N * A) / B; + + printf("Case #%d: ", ++cases); + if (C >= 0 && C <= 10) + printf("%.2lf\n", C); + else + puts("Impossible"); + } + return 0; +} +/* +3 +7 8 1 1 +7 9 5 2 +9.05 9.20 28 20 +*/ diff --git a/12726.cpp b/12726.cpp new file mode 100644 index 0000000..35757e6 --- /dev/null +++ b/12726.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int dp[1 << 20], mask[20], N, M, K; +int used[1 << 20] = {}, ucases = 0; +int dfs(int mm) +{ + if ((mm >> (N - 1)) & 1) + return 0; + if (used[mm] == ucases) + return dp[mm]; + int &ret = dp[mm]; + ret = 0x3f3f3f3f, used[mm] = ucases; + for (int i = 0; i < N; i++) + { + if ((mm >> i) & 1) + continue; + int common = __builtin_popcount(mm & mask[i]); + if (common >= K) + ret = min(ret, dfs(mm | (1 << i)) + 1); + } + return ret; +} +int main() +{ + int testcase, cases = 0, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &N, &M, &K); + for (int i = 0; i < N; i++) + mask[i] = 1 << i; + for (int i = 0; i < M; i++) + { + scanf("%d %d", &x, &y), x--, y--; + mask[x] |= 1 << y; + mask[y] |= 1 << x; + } + if ((mask[0] >> (N - 1)) & 1) + { + printf("Case #%d: %d\n", ++cases, 0); + continue; + } + ucases++; + int ret = dfs(mask[0]); + printf("Case #%d: %d\n", ++cases, ret == 0x3f3f3f3f ? -1 : (ret - 1)); + } + return 0; +} +/* +3 +3 2 1 +1 2 +2 3 +7 6 1 +1 2 +2 3 +3 4 +4 5 +5 6 +6 7 +4 3 2 +1 2 +2 4 +3 4 +*/ diff --git a/12727.cpp b/12727.cpp new file mode 100644 index 0000000..58aaed4 --- /dev/null +++ b/12727.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +const int MAXN = 65, MAXC = 310; + +struct Node +{ + int l, r, w; + bool operator<(const Node &rhs) const + { + if (l != rhs.l) + return l < rhs.l; + return r < rhs.r; + } +}; + +bool dp[MAXN][MAXC], Can[MAXC]; +int pre[MAXN][MAXC]; +Node S[MAXN]; + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + int N, M, K, tot = 0; + scanf("%d%d%d", &N, &M, &K); + for (int i = 1; i <= M; ++i) + { + scanf("%d%d%d", &S[i].l, &S[i].r, &S[i].w); + tot += S[i].w; + } + S[0] = (Node){0, 0, 0}; + sort(S, S + M); + memset(dp, 0, sizeof(dp)); + memset(pre, 0x3f, sizeof(pre)); + dp[0][0] = true; + pre[0][0] = -1; + for (int i = 1; i <= M; ++i) + { + for (int j = 0; j < i; ++j) + { + if (S[j].l < S[i].l && S[j].r + 1 >= S[i].l && S[j].r < S[i].r) + { + for (int c = 0; c <= tot; ++c) + { + if (dp[j][c] && pre[j][c] + 1 < S[i].l) + { + int tmp = c + S[i].w; + pre[i][tmp] = min(pre[i][tmp], S[j].r); + dp[i][tmp] = true; + } + } + } + } + } + memset(Can, 0, sizeof(Can)); + for (int i = 0; i <= M; ++i) + { + if (S[i].r == N) + { + for (int c = 0; c <= tot; ++c) + Can[c] |= dp[i][c]; + } + } + long long ret = 0; + while (K--) + { + long long X, Y; + scanf("%lld%lld", &X, &Y); + if (X > tot) + { + long long c; + for (c = tot; c > 0; --c) + if (Can[c]) + break; + ret += c * (Y - X + 1); + } + else + { + long long c; + for (int i = X; i <= tot && i <= Y; ++i) + { + for (c = i; c > 0; --c) + if (Can[c]) + break; + ret += c; + } + if (Y > tot) + { + for (c = tot; c > 0; --c) + if (Can[c]) + break; + ret += c * (Y - tot); + } + } + } + printf("Case #%d: %lld\n", cas, ret); + } + return 0; +} diff --git a/12728.cpp b/12728.cpp new file mode 100644 index 0000000..3be35ab --- /dev/null +++ b/12728.cpp @@ -0,0 +1,176 @@ +#include + +using namespace std; + +const double PI = acos(-1.0); +const double eps = 1e-8; +const int MAXN = 10000 + 10; + +struct Interval +{ + double l, r; + Interval() {} + Interval(double _l, double _r) : l(_l), r(_r) {} + bool operator<(const Interval &rhs) const + { + if (l != rhs.l) + { + return l < rhs.l; + } + return r < rhs.r; + } +}; + +vector Walls[MAXN], Doors[MAXN]; +int N; + +void init() +{ + scanf("%d", &N); + for (int i = 0, K; i < N; ++i) + { + Walls[i].clear(); + scanf("%d", &K); + while (K--) + { + double l, r; + scanf("%lf%lf", &l, &r); + if (l > r) + { + r += PI * 2; + } + if (r > PI * 2) + { + Walls[i].push_back(Interval(0, r - PI * 2)); + r = PI * 2; + } + Walls[i].push_back(Interval(l, r)); + } + } +} + +void merge(vector &V) +{ + sort(V.begin(), V.end()); + vector tmp(V); + V.clear(); + double l = -1, r = -1; + for (int i = 0; i < (int)tmp.size(); ++i) + { + double x = tmp[i].l, y = tmp[i].r; + if (r + eps > x) + { + r = max(r, y); + } + else + { + if (r > -eps) + { + V.push_back(Interval(l, r)); + } + l = x, r = y; + } + } + if (r > -eps) + { + V.push_back(Interval(l, r)); + } +} + +void get_door() +{ + for (int i = 0; i < N; ++i) + { + merge(Walls[i]); + Doors[i].clear(); + double st = 0; + for (int j = 0; j < (int)Walls[i].size(); ++j) + { + double x = Walls[i][j].l, y = Walls[i][j].r; + if (st + eps < x) + { + Doors[i].push_back(Interval(st, x)); + st = y; + } + else + { + st = max(st, y); + } + } + if (st + eps < PI * 2) + { + Doors[i].push_back(Interval(st, PI * 2)); + } + merge(Doors[i]); + } +} + +bool check(double p) +{ + double v = p * PI * 2; + vector now; + now.push_back(Interval(0, PI * 2)); + for (int i = 0; i < N; ++i) + { + vector nxt; + for (int j = 0; j < (int)now.size(); ++j) + { + double x = now[j].l - v, y = now[j].r + v; + if (x < 0) + nxt.push_back(Interval(x + PI * 2, PI * 2)); + x = max(x, 0.0); + if (y > PI * 2) + nxt.push_back(Interval(0, y - PI * 2)); + y = min(PI * 2, y); + nxt.push_back(Interval(x, y)); + } + merge(nxt); + now.clear(); + for (int a = 0, b = 0; a < (int)nxt.size() && b < (int)Doors[i].size();) + { + double l1 = nxt[a].l, r1 = nxt[a].r; + double l2 = Doors[i][b].l, r2 = Doors[i][b].r; + double l = max(l1, l2), r = min(r1, r2); + if (l - eps < r) + now.push_back(Interval(l, r)); + if (r1 < r2) + a++; + else + b++; + } + merge(now); + } + return (int)now.size(); +} + +void solve(int t) +{ + double left = 0, right = 0.5; + for (int times = 0; times < 100; ++times) + { + double mid = (left + right) / 2; + if (check(mid)) + { + right = mid; + } + else + { + left = mid; + } + } + printf("Case #%d: %.4lf\n", t, left); +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + init(); + get_door(); + check(0.44); + solve(t); + } + return 0; +} diff --git a/12729.cpp b/12729.cpp new file mode 100644 index 0000000..c9d7834 --- /dev/null +++ b/12729.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + int N, M; + scanf("%d%d", &N, &M); + int c = N / 2, ret = 0; + for (int i = 0; i < M / 2; ++i) + { + if (i & 1) + ret += c / 2; + else + ret += c / 2 + (c & 1); + } + int Ana = ret * 4, Bob = N * M - Ana; + printf("Case #%d: ", cas); + if (Ana > Bob) + puts("Ana"); + else if (Ana == Bob) + puts("Draw"); + else + puts("Bob"); + } + return 0; +} diff --git a/12730.cpp b/12730.cpp new file mode 100644 index 0000000..8354931 --- /dev/null +++ b/12730.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +double dp[1048576], sum[1048576]; +int main() +{ + int testcase, cases = 0; + int n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 1; i <= n; i++) + { + if (i <= m) + { + dp[i] = 1; + } + else + { + dp[i] = 1 + sum[i - m - 1] * 2.0 / i; + } + sum[i] = sum[i - 1] + dp[i]; + } + printf("Case #%d: %.4lf\n", ++cases, dp[n]); + } + return 0; +} + +/* + 3 + 4 2 + 7 2 + 10 3 + */ \ No newline at end of file diff --git a/12743.cpp b/12743.cpp new file mode 100644 index 0000000..bc8e0df --- /dev/null +++ b/12743.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define fr(a, b, c) for (int a = b; a < c; a++) +#define rp(a, b) fr(a, 0, b) +#define cl(a, b) memset(a, b, sizeof a) +#define MOD 100007 + +int N, M, cas = 1; +int dp[511][30], mark[511][30], passo; +int cnt[300], F[30]; + +int go(int i, int j) +{ + int &pd = dp[i][j]; + if (mark[i][j] == passo) + return pd; + mark[i][j] = passo; + + if (i == 0 && j >= M) + return pd = 1; + + int ret = 0; + if (i > 0) + ret = (ret + i * go(i - 1, j)) % MOD; + if (j < M) + ret = (ret + F[j] * go(i + F[j] - 1, j + 1)) % MOD; + return pd = ret; +} + +char S[511], P[30]; +int main() +{ + cl(mark, 0); + passo = 0; + int T; + scanf("%d", &T); + while (T--) + { + scanf("%s%s", S, P); + N = strlen(S); + M = strlen(P); + + cl(cnt, 0); + int total = 0; + rp(i, N) + { + total++; + cnt[S[i]]++; + } + rp(i, M) + { + total -= cnt[P[i]]; + F[i] = cnt[P[i]]; + } + passo++; + printf("Case %d: %d\n", cas++, go(total, 0)); + } + return 0; +} \ No newline at end of file diff --git a/12745.cpp b/12745.cpp new file mode 100644 index 0000000..1106970 --- /dev/null +++ b/12745.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define MAXN 262144 +vector g[MAXN]; +int vfind[MAXN], findIdx; +int stk[MAXN], stkIdx; +int in_stk[MAXN], visited[MAXN]; +int contract[MAXN]; +int scc_cnt; +int scc(int nd) +{ + in_stk[nd] = visited[nd] = 1; + stk[++stkIdx] = nd; + vfind[nd] = ++findIdx; + int mn = vfind[nd]; + for (int i = 0; i < g[nd].size(); i++) + { + if (!visited[g[nd][i]]) + mn = min(mn, scc(g[nd][i])); + if (in_stk[g[nd][i]]) + mn = min(mn, vfind[g[nd][i]]); + } + if (mn == vfind[nd]) + { + do + { + in_stk[stk[stkIdx]] = 0; + contract[stk[stkIdx]] = nd; + } while (stk[stkIdx--] != nd); + scc_cnt++; + } + return mn; +} +int main() +{ + int testcase, n, m, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + n = (n + 1) * 2; + for (int i = 0; i < n; i++) + g[i].clear(); + // 2*node: false, 2*node+1: true + while (m--) + { + int a, b, x, y; + scanf("%d %d", &a, &b); + x = abs(a), y = abs(b); + x <<= 1, y <<= 1; + if (a < 0) + x ^= 1; + if (b < 0) + y ^= 1; + g[x ^ 1].push_back(y); + g[y ^ 1].push_back(x); + } + //SCC + memset(visited, 0, sizeof(visited)); + memset(in_stk, 0, sizeof(in_stk)); + scc_cnt = 1; + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + stkIdx = 0; + findIdx = 0; + scc(i); + } + } + //2-SAT check + int hasSol = 1; + for (int i = 0; i < n && hasSol; i += 2) + if (contract[i] == contract[i ^ 1]) + hasSol = 0; + + printf("Case %d: %s\n", ++cases, hasSol ? "Yes" : "No"); + } + return 0; +} +/* +2 +3 5 +-1 2 +1 3 +3 -2 +1 -3 +-2 -3 +4 4 +-1 2 +1 3 +-2 4 +-3 -4 +*/ diff --git a/12747.cpp b/12747.cpp new file mode 100644 index 0000000..bdebc4e --- /dev/null +++ b/12747.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +// LCS to LIS +int A[262144], B[262144]; +int LIS(int A[], int n) +{ + vector r; + r.push_back(A[0]); + for (int i = 1; i < n; i++) + { + int pos = (int)(upper_bound(r.begin(), r.end(), A[i]) - r.begin()); + if (pos == r.size()) + r.push_back(A[i]); + else + r[pos] = A[i]; + } + return r.size(); +} +int main() +{ + int testcase, cases = 0; + int n, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", &x); + A[x] = i; + } + for (int i = 0; i < n; i++) + { + scanf("%d", &x); + B[i] = A[x]; + } + printf("Case %d: %d\n", ++cases, (n - LIS(B, n)) * 2); + } + return 0; +} +/* +2 +5 +1 3 5 4 2 +1 5 4 3 2 +4 +1 2 4 3 +3 4 2 1 +*/ diff --git a/12748.cpp b/12748.cpp new file mode 100644 index 0000000..0c74882 --- /dev/null +++ b/12748.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n, m, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + int x[128], y[128], r[128]; + for (int i = 0; i < n; i++) + { + scanf("%d %d %d", &x[i], &y[i], &r[i]); + } + + printf("Case %d:\n", ++cases); + for (int i = 0; i < m; i++) + { + int px, py, dist, ok = 0; + scanf("%d %d", &px, &py); + for (int j = 0; j < n && !ok; j++) + { + dist = (x[j] - px) * (x[j] - px) + (y[j] - py) * (y[j] - py); + if (dist <= r[j] * r[j]) + ok = 1; + } + puts(ok ? "Yes" : "No"); + } + } + return 0; +} + +/* + 1 + 1 3 + 0 0 2 + 0 0 + 0 2 + 0 3 + */ \ No newline at end of file diff --git a/12749.cpp b/12749.cpp new file mode 100644 index 0000000..38518fe --- /dev/null +++ b/12749.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const long long mod = 1000000007LL; +long long mul(long long a, long long b) +{ + long long ret = 0; + for (; b != 0; b >>= 1, (a <<= 1) %= mod) + if (b & 1) + (ret += a) %= mod; + return ret; +} +long long mpow(long long x, long long y, long long mod) +{ + long long ret = 1; + while (y) + { + if (y & 1) + ret = (ret * x) % mod; + y >>= 1LL, x = (x * x) % mod; + } + return ret; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int testcase, cases = 0; + long long D, V; + scanf("%d", &testcase); + while (scanf("%lld %lld", &D, &V) == 2) + { + assert(D >= 0 && V > 0 && D <= 2e+9 && V <= 2e+9); + long long ret = 0; + if (D == 0) + ret = 1; + else if (D == 1) + ret = (V + 1) % mod; + else if (V == 1) + ret = -1; + else if (V == 2) + ret = (1 + D * 2) % mod; + else + { + ret = mpow(V - 1, D, mod) - 1 + mod; + ret = ret * mpow(V - 2, mod - 2, mod) % mod; + ret = ret * V % mod; + ret = (ret + 1 + mod) % mod; + assert(ret >= 0); + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} +/* +1000 +0 1 +1 2 +1 5 +500 1 +0 500 +2000000000 2000000000 +*/ diff --git a/12750.cpp b/12750.cpp new file mode 100644 index 0000000..cca8b20 --- /dev/null +++ b/12750.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int N; + char token[10]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &N); + int win = 0, cnt = 0, Y = 0; + while (N--) + { + scanf("%s", token); + if (win) + continue; + if (token[0] == 'W') + cnt = 0; + else + cnt++, win |= cnt >= 3; + Y++; + } + printf("Case %d: ", ++cases); + if (win) + printf("%d\n", Y); + else + puts("Yay! Mighty Rafa persists!"); + } + return 0; +} +/* +3 +5 +L +D +L +D +W +6 +L +D +W +L +L +L +3 +D +D +W +*/ diff --git a/12751.cpp b/12751.cpp new file mode 100644 index 0000000..7e17a5c --- /dev/null +++ b/12751.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int f(int n) +{ + return (n) * (n + 1) / 2; +} +int main() +{ + scanf("%*d"); + int N, K, X; + int cases = 0; + while (scanf("%d %d %d", &N, &K, &X) == 3) + printf("Case %d: %d\n", ++cases, f(N) - f(X + K - 1) + f(X - 1)); + return 0; +} diff --git a/12753.cpp b/12753.cpp new file mode 100644 index 0000000..af4121e --- /dev/null +++ b/12753.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +const int MOD = 1000000009; +typedef long long Long; +const int MX = 600; + +int N, k, D[MX]; +Long dp[MX][MX], nCr[MX][MX]; +int Vis[MX][MX], cs; + +Long solve(int nth, int rem) +{ + if (nth == 0) + return rem == 0; + if (Vis[nth][rem] == cs) + return dp[nth][rem]; + Vis[nth][rem] = cs; + Long ret = 0; + for (int i = 0; i <= k and i <= rem; i++) + { + ret += (nCr[rem][i] * solve(nth - 1, rem - i)) % MOD; + ret %= MOD; + } + return dp[nth][rem] = ret; +} + +int main() +{ + + nCr[0][0] = 1; + for (int i = 1; i < MX; i++) + { + nCr[i][0] = 1; + for (int j = 1; j <= i; j++) + nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % MOD; + } + + int T; + scanf("%d", &T); + for (cs = 1; cs <= T; cs++) + { + scanf("%d %d", &N, &k); + for (int i = 0; i <= N; i++) + scanf("%d", &D[i]); + Long Ans = 1; + for (int i = 1; i <= N; i++) + Ans = (Ans * solve(D[i - 1], D[i])) % MOD; + printf("Case %d: %lld\n", cs, Ans); + } +} diff --git a/12754.cpp b/12754.cpp new file mode 100644 index 0000000..9c50d6a --- /dev/null +++ b/12754.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + char s[1024], type1[1024], type2[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int row, col, D = col - 2; + int ok = 1; + col = (int)strlen(s); + D = col - 2; + row = D * 2 + 3; + for (int i = 0; i < col; i++) + type1[i] = '-', type2[i] = '.'; + type1[0] = type1[col - 1] = '.'; + type2[0] = type2[col - 1] = '|'; + type1[col] = type2[col] = '\0'; + for (int i = 0; i <= row; i++) + { + if (i) + scanf("%s", s); + if (!strcmp(s, "*")) + { + if (i == row) + break; + ok = 0; + break; + } + if (i == 0 || i == row - 1 || i == D + 1) + { + ok &= !strcmp(s, type1); + // printf("%s === %s\n", type1, s); + } + else + { + ok &= !strcmp(s, type2); + // printf("%s === %s\n", type2, s); + } + } + while (strcmp(s, "*")) + { + ok = 0; + scanf("%s", s); + } + printf("Case %d: ", ++cases); + if (ok && D >= 0) + printf("%d\n", D); + else + puts("Bad"); + } + return 0; +} + +/* + 2 + .---. + |...| + |...| + |...| + .---. + |...| + |...| + |...| + .---. + * + .---. + |...| + |...| + |...| + .---. + |...| + |...| + .---. + * + */ \ No newline at end of file diff --git a/12755.cpp b/12755.cpp new file mode 100644 index 0000000..3f18087 --- /dev/null +++ b/12755.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int g[262144], visited[262144]; +int main() +{ + int testcase, cases = 0; + int n, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + scanf("%d", &x); + g[i * n + j] = x; + visited[i * n + j] = 0; + } + } + + int ret = 0; + for (int i = 0; i < n * n; i++) + { + if (visited[i] == 0 && g[i] != i) + { + for (int j = i; !visited[j];) + { + visited[j] = 1; + j = g[j]; + ret++; + } + if (i) + ret++; + } + } + printf("Case %d: %d\n", ++cases, ret - (g[0] != 0)); + } + return 0; +} + +/* + 2 + 3 + 7 2 4 + 5 0 6 + 8 3 1 + 2 + 3 0 + 1 2 + */ \ No newline at end of file diff --git a/12761.cpp b/12761.cpp new file mode 100644 index 0000000..0ac9238 --- /dev/null +++ b/12761.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +const int MAXN = 100; +typedef int matrix[60][60]; + +int X[MAXN]; +int N, K, D; + +void mul(matrix A, matrix B) +{ + matrix C; + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < N; ++j) + { + C[i][j] = 0; + for (int k = 0; k < N; ++k) + { + C[i][j] += A[i][k] * B[k][j]; + C[i][j] %= N; + } + } + } + memcpy(A, C, sizeof(C)); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &N, &K, &D); + matrix A; + memset(A, 0, sizeof(A)); + for (int i = 0; i < N; ++i) + { + scanf("%d", &X[i]); + for (int j = 1; j <= D; ++j) + { + A[i][(i + j) % N] = 1; + A[i][(i - j + N) % N] = 1; + } + A[i][i] = 0; + } + for (; K; K >>= 1) + { + if (K & 1) + { + int tmp[MAXN]; + for (int i = 0; i < N; ++i) + { + tmp[i] = 0; + for (int j = 0; j < N; ++j) + { + tmp[i] += A[i][j] * X[j]; + tmp[i] %= N; + } + } + for (int i = 0; i < N; ++i) + X[i] = tmp[i]; + } + mul(A, A); + } + int mx = N; + for (int i = 0; i < N; ++i) + mx = min(mx, X[i]); + printf("%d\n", mx); + for (int i = 0, f = 0; i < N; ++i) + { + if (X[i] == mx) + { + if (f) + putchar(' '); + else + f = 1; + printf("%d", i + 1); + } + } + puts(""); + } +} diff --git a/12763.cpp b/12763.cpp new file mode 100644 index 0000000..0af70ef --- /dev/null +++ b/12763.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + char s[10]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int player = s[0] - 'A'; + int dice[3][6]; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 6; j++) + { + scanf("%d", &dice[i][j]); + } + } + double rmx = 0, rmx2 = 1; + for (int i = 0; i < 3; i++) + {// A player pick + double mx = 1, mx2 = 0; + for (int j = 0; j < 3; j++) + {// B player pick + if (i == j) + continue; + int pa = 0, pb = 0; + for (int p = 0; p < 6; p++) + { + for (int q = 0; q < 6; q++) + { + pa += dice[i][p] > dice[j][q]; + pb += dice[i][p] < dice[j][q]; + } + } + + if (mx > pa / 36.0 || (mx == pa / 36.0 && mx2 < pb / 36.0)) + { + mx = pa / 36.0; + mx2 = pb / 36.0; + } + } + if (rmx < mx || (rmx == mx && rmx2 > mx2)) + { + rmx = mx; + rmx2 = mx2; + } + } + if (rmx > rmx2) + printf("%c\n", player + 'A'); + else if (rmx < rmx2) + printf("%c\n", 1 - player + 'A'); + else + printf("fair\n"); + // printf("%lf %lf\n", rmx, rmx2); + } + return 0; +} +/* +3 +A +1 1 1 1 1 1 +2 3 2 4 5 3 +6 6 6 6 6 6 +A +4 3 7 9 2 5 +8 1 4 6 9 2 +6 5 1 8 3 7 +B +1 2 3 4 4 4 +1 2 3 4 4 4 +1 2 3 4 4 4 +*/ diff --git a/12764.cpp b/12764.cpp new file mode 100644 index 0000000..3c179d8 --- /dev/null +++ b/12764.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +char frame[10][5][5] = { + {"\\./.", + ".|..", + "/o\\."}, + {".o..", + "/|\\.", + "/.\\."}, + {"....", + "....", + "__\\o"}, + {"....", + "....", + "\\_\\o"}, + {"_o_.", + ".|..", + "/.\\."}, + {"\\o/.", + ".|..", + "/.\\."}, + {"\\o_.", + "\\|..", + ".|.."}, + {"\\o/.", + "\\|/.", + "...."}, + {"....", + ".__o", + "/..|"}, + {".o..", + "(.).", + "/.\\."}}; +int main() +{ + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 3; j++) + { + for (int k = 0; frame[i][j][k]; k++) + { + if (frame[i][j][k] == '.') + { + frame[i][j][k] = ' '; + } + } + } + } + int testcase; + char s[128]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + for (int i = 0; i < 3; i++) + { + for (int j = 0; s[j]; j++) + { + if (j) + putchar(' '); + printf("%s", frame[s[j] - '0'][i]); + } + puts(""); + } + puts(""); + } + return 0; +} + +/* + 2 + 171 + 151415 + */ \ No newline at end of file diff --git a/12765.cpp b/12765.cpp new file mode 100644 index 0000000..94e826e --- /dev/null +++ b/12765.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int f[10][10] = { + {0}, + {0}, + {2, 1, 0}, + {2, 1, 3, 1, 0}, + {2, 3, 3, 1, 0}, + {2, 3, 3, 1, 5, 1, 0}, + {2, 4, 3, 2, 5, 1, 0}, + {2, 4, 3, 2, 5, 1, 7, 1, 0}, + {2, 7, 3, 2, 5, 1, 7, 1, 0}, + {2, 7, 3, 4, 5, 1, 7, 1, 0}}; +int main() +{ + int n, m, x; + while (scanf("%d %d", &n, &m) == 2 && n + m) + { + int A[10] = {}; + for (int i = 0; i < n; i++) + { + scanf("%d", &x); + for (int j = 0; f[x][j]; j += 2) + { + A[f[x][j]] += f[x][j + 1]; + } + } + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + for (int j = 0; f[x][j]; j += 2) + { + A[f[x][j]] -= f[x][j + 1]; + } + } + int ret = 1; + for (int i = 0; i < 10; i++) + ret &= A[i] == 0; + puts(ret ? "YES" : "NO"); + } + return 0; +} +/* +1 1 +9 +9 +1 2 +5 +3 6 +0 0 +*/ diff --git a/12766.cpp b/12766.cpp new file mode 100644 index 0000000..c21c82c --- /dev/null +++ b/12766.cpp @@ -0,0 +1,259 @@ +#include + +using namespace std; + +#define pb push_back +#define mp make_pair +#define eps 1e-9 +#define pi acos(-1.0) +#define ff first +#define ss second +#define re return +#define QI queue +#define SI stack +#define SZ(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define sq(a) ((a) * (a)) +#define distance(a, b) (sq(a.x - b.x) + sq(a.y - b.y)) +#define iseq(a, b) (fabs(a - b) < eps) +#define eq(a, b) iseq(a, b) +#define ms(a, b) memset((a), (b), sizeof(a)) +#define G() getchar() +#define MAX3(a, b, c) max(a, max(b, c)) +#define II ({ int a ; read(a) ; a; }) +#define LL ({ Long a ; read(a) ; a; }) +#define DD ({double a; scanf("%lf", &a); a; }) + +double const EPS = 3e-8; + +typedef long long Long; +typedef long long int64; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +// For loop + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +template +T gcd(T a, T b) +{ + return (b != 0 ? gcd(b, a % b) : a); +} +template +T lcm(T a, T b) +{ + return (a / gcd(a, b) * b); +} +#define __(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << "\t"; + return *this; + } +} dbg; +#define __1D(a, n) \ + rep(i, n) \ + { \ + if (i) \ + printf(" "); \ + cout << a[i]; \ + } +#define __2D(a, r, c, f) \ + forab(i, f, r - !f) \ + { \ + forab(j, f, c - !f) \ + { \ + if (j != f) \ + printf(" "); \ + cout << a[i][j]; \ + } \ + cout << endl; \ + } + +template +ostream &operator<<(ostream &o, const pair &p) +{ + return o << "(" << p.ff << ", " << p.ss << ")";//Pair print +} +template +ostream &operator<<(ostream &o, const vector &v) +{ + o << "[";//Vector print + forstl(it, v) o << *it << ", "; + return o << "]"; +} +template +ostream &operator<<(ostream &o, const set &v) +{ + o << "[";//Set print + forstl(it, v) o << *it << ", "; + return o << "]"; +} + +//Fast Reader +template +inline bool read(T &x) +{ + int c = getchar(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = getchar(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction + +/* ************************************** My code start here ****************************************** */ + +const int NX = 1e5 + 10; + +int inp[2][2][NX]; + +int main() +{ + + int cs, t = II; + for (cs = 1; cs <= t; cs++) + { + + int idx = II; + int sv = idx; + int idx1 = 0; + bool update = 1; + set s; + rep(i, idx) + { + inp[0][0][i] = II; + s.insert(inp[0][0][i]); + inp[0][1][i] = inp[0][0][i];// 0 for min 1 for max + } + if (s.size() != idx) + { + puts("no"); + continue; + } + idx--; + int prv = 0, cur = 1; + int total = 0; + while (update && idx != 0 && total++ <= 1000) + { + update = 0; + idx1 = 0; + inp[cur][0][idx1] = inp[prv][0][0]; + inp[cur][1][idx1] = inp[prv][1][0]; + for (int i = 1; i <= idx; i++) + { + // same + if (inp[cur][0][idx1] == inp[prv][0][i] && inp[cur][1][idx1] == inp[prv][1][i]) + continue; + if (inp[prv][1][i] + 1 == inp[cur][0][idx1]) + { + inp[cur][0][idx1] = min(inp[cur][0][idx1], inp[prv][0][i]); + inp[cur][1][idx1] = max(inp[cur][1][idx1], inp[prv][1][i]); + update = 1; + } + else if (inp[prv][0][i] - 1 == inp[cur][1][idx1]) + { + inp[cur][0][idx1] = min(inp[cur][0][idx1], inp[prv][0][i]); + inp[cur][1][idx1] = max(inp[cur][1][idx1], inp[prv][1][i]); + update = 1; + } + else if (inp[cur][0][idx1] <= inp[prv][0][i] && inp[cur][1][idx1] >= inp[prv][1][i]) + { + update = 1; + } + else if (inp[cur][0][idx1] <= inp[prv][0][i] && inp[prv][0][i] <= inp[cur][1][idx1] && inp[prv][1][i] > inp[cur][1][idx1]) + { + inp[cur][1][idx1] = inp[prv][1][i]; + update = 1; + } + else if (inp[cur][0][idx1] > inp[prv][0][i] && inp[prv][1][i] >= inp[cur][0][idx1] && inp[prv][1][i] <= inp[cur][1][idx1]) + { + inp[cur][0][idx1] = inp[prv][0][1]; + update = 1; + } + // here + else if (inp[prv][0][i] <= inp[cur][0][idx1] && inp[cur][1][idx1] >= inp[prv][0][1] && inp[cur][1][idx1] <= inp[prv][1][i]) + { + inp[cur][0][idx1] = inp[prv][0][i]; + inp[cur][1][idx1] = inp[prv][1][i]; + update = 1; + } + // now + else if (inp[cur][0][idx1] <= inp[prv][0][i] && inp[prv][0][i] <= inp[cur][1][idx1] && inp[prv][1][i] > inp[cur][1][idx1]) + { + inp[cur][1][idx1] = inp[prv][1][i]; + update = 1; + } + // update + else if (inp[cur][0][idx1] > inp[prv][0][i] && inp[cur][0][idx1] <= inp[prv][1][i] && inp[cur][1][idx1] >= inp[prv][1][i]) + { + inp[cur][0][idx1] = inp[prv][0][i]; + update = 1; + } + + else + { + ++idx1; + inp[cur][0][idx1] = inp[prv][0][i]; + inp[cur][1][idx1] = inp[prv][1][i]; + } + } + swap(idx1, idx); + swap(cur, prv); + } + /*cout << idx << endl ; + rep( i , idx + 1 ) + { + printf("min :: %d mx :: %d\n " , inp[ prv ][ 0 ][ i ] , inp[prv][1][i] ); + } */ + if (total >= 1000 || (idx == 0 && inp[prv][0][0] == 1 && inp[prv][1][0] == sv)) + puts("yes"); + else + puts("no"); + } +} +/* + +4 +4 +1 2 4 3 +4 +1 2 4 4 +4 +1 1 1 1 +2 +1 1 + +*/ diff --git a/12768.cpp b/12768.cpp new file mode 100644 index 0000000..9999c02 --- /dev/null +++ b/12768.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int MAXN = 100, inf = 1e9; + +vector> G[MAXN]; +int dis[MAXN], vis[MAXN], times[MAXN]; +int N, M; + +bool spfa() +{ + queue Q; + for (int i = 1; i <= N; ++i) + dis[i] = -inf, vis[i] = false, times[i] = 0; + Q.push(1); + vis[1] = true; + dis[1] = 0; + while (!Q.empty()) + { + int u = Q.front(); + Q.pop(); + vis[u] = false; + times[u]++; + if (times[u] > N) + return false; + for (int i = 0; i < (int)G[u].size(); ++i) + { + int v = G[u][i].first, w = G[u][i].second; + if (dis[v] < dis[u] + w) + { + dis[v] = dis[u] + w; + if (!vis[v]) + vis[v] = true, Q.push(v); + } + } + } + return true; +} + +int main() +{ + while (scanf("%d%d", &N, &M) == 2 && N) + { + for (int i = 1; i <= N; ++i) + G[i].clear(); + while (M--) + { + int a, b, c; + scanf("%d%d%d", &a, &b, &c); + G[a].push_back(make_pair(b, c)); + } + if (spfa()) + { + int ret = 0; + for (int i = 1; i <= N; ++i) + ret = max(ret, dis[i]); + printf("%d\n", ret); + } + else + puts("Unlimited!"); + } + return 0; +} diff --git a/12769.cpp b/12769.cpp new file mode 100644 index 0000000..e0c99fa --- /dev/null +++ b/12769.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define MAXN 100000 +int BIT[MAXN + 5]; +void modify(int x, int val, int L) +{ + while (x <= L) + { + BIT[x] += val; + x += x & (-x); + } +} +int query(int x) +{ + int ret = 0; + while (x) + { + ret += BIT[x]; + x -= x & (-x); + } + return ret; +} +int main() +{ + int n, a, b, y; + char cmd[8]; + while (scanf("%d", &n) == 1 && n) + { + memset(BIT, 0, sizeof(BIT)); + for (int i = 0; i < n; i++) + { + scanf("%s", cmd); + if (cmd[0] == 'B') + { + scanf("%d %d %d", &a, &b, &y); + modify(a, y, MAXN); + modify(b + 1, -y, MAXN); + } + else + { + scanf("%d", &a); + int ret = query(a); + printf("%d\n", ret); + } + } + } + return 0; +} +/* +9 +B 5 5 2 +B 8 8 2 +B 10 13 1 +Q 8 +B 8 13 1 +Q 8 +B 15 16 1 +B 2 10 1 +Q 8 +9 +B 5 5 2 +B 8 8 2 +B 10 13 1 +Q 8 +B 8 13 1 +Q 8 +B 15 16 1 +B 2 10 1 +Q 8 +0 +*/ diff --git a/12770.cpp b/12770.cpp new file mode 100644 index 0000000..61420ab --- /dev/null +++ b/12770.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + char s[1024], buf[1024]; + while (scanf("%s", s) == 1 && s[0] != '#') + { + int cnt[128] = {}; + for (int i = 0; s[i]; i++) + cnt[s[i]]++; + int f = 0, n = 0; + for (int i = 127; i >= 0; i--) + { + if (cnt[i] & 1) + { + if (f == 0) + f = 1; + else + { + buf[n++] = i; + } + } + } + sort(buf, buf + n); + buf[n] = '\0'; + puts(buf); + } + return 0; +} +/* +ddc +aaab +# +*/ diff --git a/12775.cpp b/12775.cpp new file mode 100644 index 0000000..0d1f6fa --- /dev/null +++ b/12775.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +long long exgcd(long long x, long long y, long long &a, long long &b) +{ + // ax + by = gcd(x,y) + int flag = 0; + long long t, la = 1, lb = 0, ra = 0, rb = 1; + while (x % y) + { + if (flag == 0) + la -= x / y * ra, lb -= x / y * rb; + else + ra -= x / y * la, rb -= x / y * lb; + t = x, x = y, y = t % y; + flag = 1 - flag; + } + if (flag == 0) + a = ra, b = rb; + else + a = la, b = lb; + return y; +} +long long countSolution(long long n1, long long n2, long long n) +{ + long long a, b, g; + g = exgcd(n1, n2, a, b);// a*n1 + b*n2 = gcd(n1,2) + if (n % g) + return 0; + long long k = n / g, k1, k2; + a *= k, b *= k;// a*n1 + b*n2 = n + // (a+F)*n1 + (b+G)*n2 = n => Fn1 + Gn2 = 0, + //F = lcm(n1, n2)/n1 * i, G = lcm(n1, n2)/n2 * i + k1 = n1 * n2 / g / n1, k2 = n1 * n2 / g / n2; + if (a < 0) + {// adjust a >= 0 + k = -(a / k1) + (a % k1 != 0); + a += k * k1, b -= k * k2; + } + if (b < 0) + {// adjust b >= 0 + k = -(b / k2) + (b % k2 != 0); + a -= k * k1, b += k * k2; + } + if (a < 0 || b < 0) + return 0; + long long x1, x2, y1, y2; + // minimize a, maximize b + k = a / k1; + a -= k * k1; + b += k * k2; + x1 = a, y1 = b; + // maximize a, minimize b + k = b / k2; + a += k * k1; + b -= k * k2; + x2 = a, y2 = b; + return (x2 - x1) / k1 + 1; +} +int main() +{ + int testcase, cases = 0; + long long A, B, C, P, ret, ta, tb; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld %lld %lld %lld", &A, &B, &C, &P); + long long g = exgcd(exgcd(A, B, ta, tb), C, ta, tb); + P /= g, A /= g, B /= g, C /= g; + ret = 0; + for (long long i = 0; P - C * i >= 0; i++) + ret += countSolution(A, B, P - C * i); + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} +/* +1 +202 203 200 606 +*/ diff --git a/12776.cpp b/12776.cpp new file mode 100644 index 0000000..66ac1f3 --- /dev/null +++ b/12776.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +#define maxL (50000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[maxL]; +int P[32767], Pt = 0; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 50000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} +vector> factor(int n) +{ + int on = n; + vector> R; + + for (int i = 0, j; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + for (j = 0; n % P[i] == 0; n /= P[i], j++) + ; + R.push_back(make_pair(P[i], j)); + } + } + if (n != 1) + R.push_back(make_pair(n, 1)); + return R; +} +void make(int idx, int n, int m, vector> &v, vector &ret) +{ + if (idx == v.size()) + { + ret.push_back(m); + return; + } + int a = m, b = v[idx].first; + for (int i = v[idx].second; i >= 0; i--) + make(idx + 1, n, a, v, ret), a *= b; +} +int A[131072], L[131072], R[131072]; +vector Af[131072], RM[131072]; +vector> Q[131072], QL[131072]; +int OUT[131072]; +int BIT[131072]; +void modify(int x, int val, int L) +{ + while (x <= L) + { + BIT[x] += val; + x += (x) & (-x); + } +} +int query(int x) +{ + int ret = 0; + while (x) + { + ret += BIT[x]; + x -= (x) & (-x); + } + return ret; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out2.txt", "w+t", stdout); + sieve(); + int testcase, cases = 0; + int n, m, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 1; i <= n; i++) + { + scanf("%d", &A[i]); + vector> f = factor(A[i]); + Af[i].clear(); + make(0, A[i], 1, f, Af[i]); + } + for (int i = 0; i < n + 20; i++) + Q[i].clear(), QL[i].clear(), RM[i].clear(); + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + Q[x].push_back(make_pair(y, i)); + } + map mp; + map::iterator mpit; + for (int i = 1; i <= n; i++) + { + x = 0; + for (int j = 0; j < Af[i].size(); j++) + x = max(x, mp[Af[i][j]]); + mp[A[i]] = i; + L[i] = x + 1; + } + mp.clear(); + for (int i = n; i >= 1; i--) + { + x = n + 1; + for (int j = 0; j < Af[i].size(); j++) + { + mpit = mp.find(Af[i][j]); + if (mpit != mp.end()) + x = min(x, mpit->second); + } + mp[A[i]] = i; + R[i] = x - 1; + } + for (int i = 1; i <= n; i++) + { + QL[L[i]].push_back(make_pair(R[i], i)); + RM[i].push_back(R[i]); + } + for (int i = 1; i <= n + 20; i++) + BIT[i] = 0; + for (int i = 0; i <= n; i++) + { + for (int j = 0; j < QL[i].size(); j++) + { + modify(QL[i][j].second, 1, n + 20); + modify(QL[i][j].first + 1, -1, n + 20); + // printf("add [%d %d]\n", QL[i][j].second, QL[i][j].first); + } + if (i) + for (int j = 0; j < RM[i - 1].size(); j++) + { + modify(i - 1, -1, n + 20); + modify(RM[i - 1][j] + 1, 1, n + 20); + // printf("rm [%d %d]\n", i-1, RM[i][j]); + } + for (int j = 0; j < Q[i].size(); j++) + { + int v = query(Q[i][j].first); + OUT[Q[i][j].second] = v; + // printf("%d %d - %d\n", Q[i][j].first, Q[i][j].second, v); + } + // puts("--"); + } + printf("Case %d:\n", ++cases); + for (int i = 0; i < m; i++) + printf("%d\n", OUT[i]); + } + return 0; +} +/* +2 +10 5 +4 6 2 7 5 11 14 21 13 2 +2 6 +4 8 +2 8 +3 7 +4 9 +5 3 +4 6 8 1 5 +1 5 +2 3 +3 3 +*/ diff --git a/12778.cpp b/12778.cpp new file mode 100644 index 0000000..52eb7bc --- /dev/null +++ b/12778.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +#define MAXN 65536 +#define INF 0x3f3f3f3f +long long sum[MAXN], ssum[MAXN], isum[MAXN], issum[MAXN]; +// binary indexed tree +int BIT[MAXN << 1]; +int query(int x) +{ + int ret = 0; + while (x) + { + ret = max(ret, BIT[x]); + x -= x & (-x); + } + return ret; +} +void modify(int x, int v, int L) +{ + while (x <= L) + { + BIT[x] = max(BIT[x], v); + x += x & (-x); + } +} +int query2(int x) +{ + int ret = INF; + while (x) + { + ret = min(ret, BIT[x]); + x -= x & (-x); + } + return ret; +} +void modify2(int x, int v, int L) +{ + while (x <= L) + { + BIT[x] = min(BIT[x], v); + x += x & (-x); + } +} +int cases = 0, preA[MAXN], sufA[MAXN], start[MAXN], cover[MAXN << 1]; +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out2.txt", "w+t", stdout); + int testcase, n, A[MAXN]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + scanf("%d", &A[i]), start[i] = 0, cover[A[i] + MAXN] = -1; + + int L = 50000 + MAXN; + for (int i = 0; i <= L; i++) + BIT[i] = 0; + for (int i = 1; i <= n; i++) + { + preA[i] = query(A[i] + MAXN); + modify(A[i] + MAXN, i, L); + if (preA[i] <= cover[A[i] + MAXN]) + start[i] = cover[A[i] + MAXN] + 1; + cover[A[i] + MAXN] = i; + } + for (int i = 0; i <= L; i++) + BIT[i] = n + 1; + for (int i = n; i >= 1; i--) + { + sufA[i] = query2(A[i] + MAXN); + modify2(A[i] + MAXN + 1, i, L); + } + + for (int i = 1; i <= n; i++) + sum[i] = sum[i - 1] + A[i]; + for (int i = 1; i <= n; i++) + ssum[i] = ssum[i - 1] + sum[i]; + + isum[n + 1] = issum[n + 1] = 0; + for (int i = n; i >= 1; i--) + isum[i] = isum[i + 1] + A[i]; + for (int i = n; i >= 1; i--) + issum[i] = issum[i + 1] + isum[i]; + + long long ret = 0; + for (int i = 1; i <= n; i++) + { + long long suma, a, sumb, b; + if (start[i]) + { + suma = issum[start[i]] - issum[i], a = i - start[i]; + sumb = ssum[sufA[i] - 1] - ssum[i], b = sufA[i] - i - 1; + suma -= isum[i] * a; + sumb -= sum[i] * b; + } + else + { + suma = issum[preA[i] + 1] - issum[i], a = i - preA[i] - 1; + sumb = ssum[sufA[i] - 1] - ssum[i], b = sufA[i] - i - 1; + suma -= isum[i] * a; + sumb -= sum[i] * b; + } + // printf("index %d\n", i); + // printf("%lld %lld\n", ssum[sufA[i]-1], ssum[i]); + // printf("%lld %lld %lld %lld -- %lld\n", suma, a, sumb, b, (suma - A[i]*a*(a+1)/2) * (b + 1) + (sumb - A[i]*b*(b+1)/2) * (a + 1)); + ret += (suma - A[i] * a * (a + 1) / 2) * (b + 1) + (sumb - A[i] * b * (b + 1) / 2) * (a + 1); + // long long test = 0; + // for(int j = i; j >= 1; j--) { + // if(A[j] < A[i]) + // break; + // for(int k = i; k <= n; k++) { + // if(A[k] < A[i]) break; + // for(int l = j; l <= k; l++) + // test += A[l] - A[i]; + // } + // } + // printf("DEBUG %lld\n", test); + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} +/* +1 +5 +1 2 3 4 5 + +10 +10 +-7 2 -6 7 -9 5 -5 -3 9 3 +10 +1 -7 9 2 4 7 -10 8 0 0 +10 +-8 9 4 -8 1 2 -3 2 8 8 +10 +-3 -4 0 -2 5 9 -3 -4 2 1 +10 +3 -8 -9 -4 8 -9 -7 6 -10 -9 +10 +-7 0 -2 8 -6 6 9 -5 -1 -3 +10 +1 -10 7 -1 7 -10 5 -1 -10 9 +10 +-2 9 -8 8 -7 0 -1 -8 7 -9 +10 +-9 2 3 -8 -10 9 7 9 1 3 +10 +3 5 1 -4 -6 -3 0 2 -5 -5 +*/ diff --git a/12779.cpp b/12779.cpp new file mode 100644 index 0000000..0073f71 --- /dev/null +++ b/12779.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +// thanks for flere help debug. + +#define eps 1e-6 +struct Pt +{ + int x, y; + Pt(int a = 0, int b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + return y < a.y; + } + Pt operator-(const Pt &a) const + { + Pt ret; + ret.x = x - a.x; + ret.y = y - a.y; + return ret; + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double length(Pt a) +{ + return hypot(a.x, a.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} + +double distProjection(Pt as, Pt at, Pt s) +{ + long long a, b, c; + a = at.y - as.y; + b = as.x - at.x; + c = -(a * as.x + b * as.y); + return fabs(a * s.x + b * s.y + c) / hypot(a, b); +} +void distProjection2(Pt as, Pt at, Pt s, long long &p, long long &q) +{ + long long a, b, c; + a = at.y - as.y; + b = as.x - at.x; + c = -(a * as.x + b * as.y); + p = (a * s.x + b * s.y + c) * (a * s.x + b * s.y + c); + q = a * a + b * b; +} +int main() +{ + Pt p[4]; + while (true) + { + int end = 1; + for (int i = 0; i < 4; i++) + { + scanf("%d %d", &p[i].x, &p[i].y); + end &= p[i].x == 0 && p[i].y == 0; + } + if (end) + break; + double h1 = distProjection(p[0], p[1], p[2]); + double h2 = distProjection(p[1], p[2], p[3]); + long long n, m; + if (h1 <= h2) + distProjection2(p[0], p[1], p[2], n, m); + else + distProjection2(p[1], p[2], p[3], n, m); + long long g = __gcd(n, m); + n /= g, m /= g; + m *= 4; + g = __gcd(n, m); + n /= g, m /= g; + if (min(h1, h2) < eps) + while (1) + ; + printf("(%lld/%lld)*pi\n", n, m); + } + return 0; +} +/* +0 0 2 3 5 5 3 2 +0 0 2 3 6 3 4 0 +0 0 10 0 10 10 0 10 +0 0 0 0 0 0 0 0 +*/ diff --git a/12783.cpp b/12783.cpp new file mode 100644 index 0000000..9e0deaa --- /dev/null +++ b/12783.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +vector g[32767]; + +int visited[32767], depth[32767]; +vector> bridge; +int findBridge(int u, int p, int dep) +{ + visited[u] = 1, depth[u] = dep; + int back = 0x3f3f3f3f; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (v == p) + continue; + if (!visited[v]) + { + int b = findBridge(v, u, dep + 1); + if (b > dep) + bridge.push_back(make_pair(u, v)); + back = min(back, b); + } + else + { + back = min(back, depth[v]); + } + } + return back; +} +int main() +{ + int n, m, q, x, y; + while (scanf("%d %d", &n, &m) == 2 && n + m) + { + for (int i = 0; i < n; i++) + g[i].clear(); + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(y); + g[y].push_back(x); + } + + bridge.clear(); + memset(visited, 0, sizeof(visited)); + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + findBridge(i, -1, 0); + } + } + for (int i = 0; i < bridge.size(); i++) + if (bridge[i].first > bridge[i].second) + swap(bridge[i].first, bridge[i].second); + sort(bridge.begin(), bridge.end()); + printf("%d", bridge.size()); + for (int i = 0; i < bridge.size(); i++) + printf(" %d %d", bridge[i].first, bridge[i].second); + puts(""); + } + return 0; +} diff --git a/12784.cpp b/12784.cpp new file mode 100644 index 0000000..6fcfb7d --- /dev/null +++ b/12784.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +vector g[1024]; +set A[1024]; +int used[1024]; +int instk[1024]; +int dfs(int u) +{ + used[u] = 1, instk[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (instk[v]) + return 1; + if (!used[v]) + { + if (dfs(v)) + return 1; + A[u].insert(A[v].begin(), A[v].end()); + if (A[u].size() > 1) + return 1; + } + } + if (g[u].size() == 0) + A[u].insert(u); + instk[u] = 0; + return A[u].size() > 1; +} +int main() +{ + int n, m, x, y; + while (scanf("%d %d", &n, &m) == 2 && n) + { + for (int i = 0; i < n; i++) + g[i].clear(), used[i] = 0, A[i].clear(), instk[i] = 0; + int indeg[1024] = {}; + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + g[x].push_back(y); + indeg[y]++; + } + + int err = 0; + for (int i = 0; i < n && !err; i++) + { + if (used[i] == 0 && indeg[i] == 0) + err |= dfs(i); + } + for (int i = 0; i < n; i++)// cycle + if (used[i] == 0) + err = 1; + printf("%d\n", !err); + } + return 0; +} +/* +3 2 +0 1 +1 2 +2 2 +0 1 +0 1 +2 2 +0 1 +1 0 +3 2 +0 1 +0 2 +0 0 +*/ diff --git a/12785.cpp b/12785.cpp new file mode 100644 index 0000000..4e28dc1 --- /dev/null +++ b/12785.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define MAXN 100005 +char P[MAXN], T[MAXN]; +int kmpTable[MAXN]; +void KMPtable(const char *P, int len) +{ + int i, j; + kmpTable[0] = -1, i = 1, j = -1; + while (i < len) + { + while (j >= 0 && P[j + 1] != P[i]) + j = kmpTable[j]; + if (P[j + 1] == P[i]) + j++; + kmpTable[i++] = j; + } +} +int KMPmatching(const char *T, int tlen, const char *P, int plen) +{ + for (int i = 0, j = -1; i < tlen; i++) + { + while (j >= 0 && P[j + 1] != T[i]) + j = kmpTable[j]; + if (P[j + 1] == T[i]) + j++; + if (j == plen - 1) + return i; + } + return -1; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + int n, plen, tlen; + string token; + while (scanf("%d", &n) == 1) + { + while (getchar() != '\n') + ; + gets(P); + plen = strlen(P); + for (int i = 0; i < n; i++) + { + gets(T); + for (int j = 0; T[j]; j++) + { + if (T[j] == '*') + T[j] = ' '; + } + stringstream sin(T); + int start = 0; + while (sin >> token) + { + KMPtable(token.c_str(), token.length()); + int test = KMPmatching(P + start, plen - start, token.c_str(), token.length()); + // printf("%s %d %s\n", token.c_str(), test, P + start); + if (test == -1) + { + start = -1; + break; + } + start += test + 1; + } + puts(start == -1 ? "no" : "yes"); + } + } + return 0; +} +/* +4 +heyhelloyou +hel* +*o*e +e*o +hello +1 +hello +x + +10 +rwoeyhtdvtswftfguuujqxdxdqylkyqaahianzbejckxbgeybq +oexktdvtswftf*w*n*q*rvdloll*qr +kdd*ts*ftv**ur**cdx*qi +*dtlk +**q**g****a**n +*/ diff --git a/12786.cpp b/12786.cpp new file mode 100644 index 0000000..9167caa --- /dev/null +++ b/12786.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int n, i, k; +long long sum[1005], d[1005]; +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + sum[0] = 0; + for (i = 0; i < n; i++) + scanf("%lld", &d[i]); + sort(d, d + n, greater()); + for (i = 0; i < n; i++) + sum[i + 1] = sum[i] + d[i]; + long long left = 0, right; + int ret = 1; + if (sum[n] & 1) + ret = 0; + for (k = 0; k < n; k++) + { + left += d[k]; + right = (long long)k * (k + 1); + int l = lower_bound(d, d + n, k + 1, greater()) - d; + if (l < k + 1) + right += sum[n] - sum[k + 1]; + else + right += sum[n] - sum[l] + (long long)(k + 1) * (l - k - 1); + if (left > right) + ret = 0; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/12787.cpp b/12787.cpp new file mode 100644 index 0000000..ef75360 --- /dev/null +++ b/12787.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +#define sf scanf +#define pf printf +#define fo(i, n) for (i = 0; i < n; i++) +#define of(i, n) for (i = n - 1; i >= 0; i--) +#define CLR(n, v) memset(n, v, sizeof(n)) +#define INF 1 << 30 +#define pb push_back +#define lim(v) v.begin(), v.end() +#define sz(v) ((int)v, size()) +#define equals(a, b) (fabs(a - b) < eps) + +#define white 0 +#define black 1 + +const double PI = 2 * acos(0.0); +const double eps = 1e-9; + +typedef long long lld; +typedef unsigned long long llu; +typedef pair pi; +typedef vector vi; +typedef vector vpi; + +template +T jog(T a, T b) +{ + return a + b; +} +template +T bog(T a, T b) +{ + return a - b; +} +template +T gon(T a, T b) +{ + return a * b; +} +template +T sq(T x) +{ + return x * x; +} +template +T gcd(T a, T b) +{ + return b == 0 ? a : gcd(b, a % b); +} +template +T lcm(T a, T b) +{ + return (a / gcd(a, b)) * b; +} +template +T power(T a, T p) +{ + int res = 1, x = a; + while (p) + { + if (p & 1) + { + res = res * x; + } + x = x * x; + p >>= 1; + } + return res; +} +template +T cordinatlenth(T a, T b, T c, T d) +{ + return sqrt(sq(a - c) + sq(b - d)); +} +template +string toString(T n) +{ + ostringstream oss; + oss << n; + oss.flush(); + return oss.str(); +} +int toInt(string s) +{ + int r = 0; + istringstream sin(s); + sin >> r; + return r; +} +lld bigmod(lld a, lld p, lld mod) +{ + lld res = 1, x = a; + while (p) + { + if (p & 1) + { + res = (res * x) % mod; + } + x = (x * x) % mod; + p >>= 1; + } + return res; +} + +int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1}; +int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1}; +int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1}; +int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2}; + +void input(); + +bool isok(int n, int base); + +int main() +{ + input(); + return 0; +} +void input() +{ + int n, res; + while (~sf("%d", &n)) + { + if (n == 2) + { + puts("3"); + continue; + } + res = 1 << 30; + int range = sqrt(n) + 2; + for (int i = 2; i < range; i++) + { + if (isok(n, i)) + { + res = i; + break; + } + if (n % i == 0) + { + int b = n / i; + b--; + if (b > i) + { + res = res < b ? res : b; + } + } + } + res = res == 1 << 30 ? n - 1 : res; + printf("%d\n", res); + } +} +bool isok(int n, int base) +{ + int k = n; + vector v; + while (k) + { + v.push_back(k % base); + k /= base; + } + int l = 0, h = v.size() - 1; + while (l < h) + { + if (v[l] != v[h]) + { + return false; + } + l++; + h--; + } + return true; +} diff --git a/12788.cpp b/12788.cpp new file mode 100644 index 0000000..343873f --- /dev/null +++ b/12788.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + int n, A[100005], B[100005]; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + int cnt = 1, m = 0; + A[n] = -0x3f3f3f3f; + for (int i = 1; i <= n; i++) + { + if (A[i] >= A[i - 1]) + cnt++; + else + { + B[m++] = cnt; + cnt = 1; + } + } + int ret = B[0]; + for (int i = 1; i < m; i++) + ret = max(ret, B[i] + B[i - 1]); + printf("%d\n", ret); + } + return 0; +} +/* +3 +1 2 3 +1 +0 +8 +1 2 1 2 1 2 3 1 +4 +1 -10 -100 -100 +*/ diff --git a/12789.cpp b/12789.cpp new file mode 100644 index 0000000..1bcaab0 --- /dev/null +++ b/12789.cpp @@ -0,0 +1,252 @@ +#include + +using namespace std; + +#define maxm 1050 +#define inf (1 << 29) +#define ii int + +#define pi acos(-1.0) +#define eps 1e-9 +#define iseq(a, b) (fabs(a - b) < eps) + +#define pii pair +#define mp make_pair +#define uu first +#define vv second + +ii on(ii n, ii k) +{ + return (n | (1 << k)); +} +ii off(ii n, ii k) +{ + return (n - (n & (1 << k))); +} +bool chck(ii n, ii k) +{ + return (n & (1 << k)); +} + +ii mini(ii a, ii b) +{ + if (a < b) + { + return a; + } + return b; +} +ii maxi(ii a, ii b) +{ + if (a > b) + { + return a; + } + return b; +} + +int r, c, s; +char grid[maxm][maxm]; +int coln[maxm], cols[maxm]; +int flagn[maxm][maxm], flags[maxm][maxm], kas = 1; + +int dfss(int x, int y) +{ + if (x > r || x < 1 || y > c || y < 1) + { + return 0; + } + if (grid[x][y] == '#') + { + return 0; + } + if (x == r) + { + return 1; + } + if (flags[x][y] == kas) + { + return 0; + } + flags[x][y] = kas; + if (dfss(x + 1, y)) + { + return 1; + } + if (dfss(x, y + 1)) + { + return 1; + } + if (dfss(x, y - 1)) + { + return 1; + } + if (dfss(x - 1, y)) + { + return 1; + } + return 0; +} + +int dfsn(int x, int y) +{ + if (x > r || x < 1 || y > c || y < 1) + { + return 0; + } + if (grid[x][y] == '#') + { + return 0; + } + if (x == 1) + { + return 1; + } + if (flagn[x][y] == kas) + { + return 0; + } + flagn[x][y] = kas; + if (dfsn(x - 1, y)) + { + return 1; + } + if (dfsn(x, y + 1)) + { + return 1; + } + if (dfsn(x, y - 1)) + { + return 1; + } + if (dfsn(x + 1, y)) + { + return 1; + } + return 0; +} + +int main() +{ + int i, j, k, l, test, t = 1; + //freopen("i.txt","r",stdin); + //freopen("out.txt","w",stdout); + while (scanf("%d", &r) == 1) + { + //if (r==-1) break; + scanf("%d %d", &c, &s); + //memset(flagn,0,sizeof(flagn)); + //memset(flags,0,sizeof(flags)); + kas++; + for (i = 1; i <= r; i++) + { + scanf("%s", grid[i] + 1); + } + for (i = 1; i <= c; i++) + { + coln[i] = r + 1; + for (j = 1; j <= r; j++) + { + if (grid[j][i] == '#') + { + coln[i] = j; + break; + } + } + cols[i] = 0; + for (j = r; j >= 1; j--) + { + if (grid[j][i] == '#') + { + cols[i] = j; + break; + } + } + } + int shot; + int ans, ans1 = 1; + ans = -1; + for (j = 1; j <= c; j++) + { + if (dfss(1, j)) + { + ans = 0; + break; + } + if (dfsn(r, j)) + { + ans = 0; + break; + } + } + for (i = 1; i <= s; i++) + { + scanf("%d", &shot); + if (ans != -1) + { + continue; + } + if (shot < 0) + { + shot = -shot; + if (cols[shot] == 0) + { + continue; + } + grid[cols[shot]][shot] = '.'; + if (dfsn(cols[shot], shot)) + { + ans = i; + ans1 = -1; + continue; + } + for (j = cols[shot]; j >= 1; j--) + { + if (grid[j][shot] == '#') + { + cols[shot] = j; + break; + } + } + if (j < 1) + { + cols[shot] = 0; + } + } + else + { + if (coln[shot] > r) + { + continue; + } + grid[coln[shot]][shot] = '.'; + if (dfss(coln[shot], shot)) + { + ans = i; + ans1 = 1; + continue; + } + for (j = coln[shot]; j <= r; j++) + { + if (grid[j][shot] == '#') + { + coln[shot] = j; + break; + } + } + if (j > r) + { + coln[shot] = j; + } + } + } + if (ans == -1) + { + puts("X"); + continue; + } + ans *= ans1; + printf("%d\n", ans); + } + return 0; +} diff --git a/1279.cpp b/1279.cpp new file mode 100644 index 0000000..24ff12d --- /dev/null +++ b/1279.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +const int maxn = 55; +const int maxd = 3000; +const double zero = 1e-9; +int n, timer, lenl, ans, tc; +int pos[maxn * maxn], now[maxn], p[maxn]; +struct things +{ + double t; + int num1, num2; + things() {} + things(double t, int num1, int num2) : t(t), num1(num1), num2(num2) {} +}; +std::vector q; +bool operator<(things a, things b) +{ + return a.t - b.t < -zero; +} +struct points +{ + double x, y, z, dx, dy, dz; + void scan1() + { + scanf("%lf%lf%lf%lf%lf%lf", &x, &y, &z, &dx, &dy, &dz); + } +} dat[maxn]; +struct lines +{ + double a, b, c; + int x, y; +} l[maxd]; +bool operator<(lines x, lines y) +{ + return x.c - y.c < -zero; +} +inline double cfsqr(double x) +{ + return x * x; +} +inline double cfabs(double x) +{ + if (x > zero) + { + return x; + } + if (x < -zero) + { + return -x; + } + return 0; +} +inline int getp(int x) +{ + int t = x, t1; + while (p[t] != t) + { + t = p[t]; + } + while (x != t) + { + t1 = p[x]; + p[x] = t; + x = t1; + } + return t; +} +int main() +{ + // freopen("12a.in", "r", stdin); + // freopen("12a.out", "w", stdout); + tc = 0; + while (scanf("%d", &n) != EOF) + { + for (int i = 1; i <= n; i++) + { + dat[i].scan1(); + } + q.clear(); + lenl = 0; + for (int i = 1; i < n; i++) + for (int j = i + 1; j <= n; j++) + { + //sigma ((dat[i].dx - dat[j].dx)*t+(dat[i].x-dat[j].x))^2 + lenl++; + l[lenl].a = cfsqr((dat[i].dx - dat[j].dx)) + cfsqr(dat[i].dy - dat[j].dy) + cfsqr(dat[i].dz - dat[j].dz); + l[lenl].b = 2 * ((dat[i].dx - dat[j].dx) * (dat[i].x - dat[j].x) + (dat[i].dy - dat[j].dy) * (dat[i].y - dat[j].y) + (dat[i].dz - dat[j].dz) * (dat[i].z - dat[j].z)); + l[lenl].c = cfsqr((dat[i].x - dat[j].x)) + cfsqr(dat[i].y - dat[j].y) + cfsqr(dat[i].z - dat[j].z); + l[lenl].x = i; + l[lenl].y = j; + } + std::sort(l + 1, l + lenl + 1); + for (int i = 1; i < lenl; i++) + for (int j = i + 1; j <= lenl; j++) + { + int p1 = i, q1 = j; + if (l[p1].a - l[q1].a < -zero) + { + p1 = j, q1 = i; + } + double ta, tb, tc, delta, t1, t2; + ta = l[p1].a - l[q1].a; + tb = l[p1].b - l[q1].b; + tc = l[p1].c - l[q1].c; + if (cfabs(ta) < zero) + { + if (cfabs(tb) < zero) + { + continue; + } + if (tb > zero) + { + p1 ^= q1 ^= p1 ^= q1; + tb = -tb; + tc = -tc; + } + if (tc > zero) + { + q.push_back(things(-tc / tb, p1, q1)); + } + continue; + } + delta = tb * tb - 4 * ta * tc; + if (delta < zero) + { + continue; + } + delta = sqrt(delta); + t1 = -(tb + delta) / (2 * ta); + t2 = (delta - tb) / (2 * ta); + if (t1 > zero) + { + q.push_back(things(t1, p1, q1)); + } + if (t2 > zero) + { + q.push_back(things(t2, q1, p1)); + } + } + for (int i = 1; i <= n; i++) + { + p[i] = i; + } + for (int i = 1; i <= lenl; i++) + { + pos[i] = 0; + } + for (int i = 1, j = 0; i <= lenl && j < n - 1; i++) + { + int x = getp(l[i].x), y = getp(l[i].y); + if (x != y) + { + now[pos[i] = ++j] = i; + p[x] = y; + } + } + std::sort(&q[0], &q[q.size()]); + ans = 1; + for (int i = 0; i < q.size(); i++) + { + // printf("%.6lf %d %d\n", q[i].t, q[i].num1, q[i].num2); + if (pos[q[i].num2] && (!pos[q[i].num1])) + { + for (int j = 1; j <= n; j++) + { + p[j] = j; + } + for (int j = 1; j < n; j++) + if (j != pos[q[i].num2]) + { + int x = getp(l[now[j]].x), y = getp(l[now[j]].y); + if (x != y) + { + p[x] = y; + } + } + int x = getp(l[q[i].num1].x), y = getp(l[q[i].num1].y); + if (x != y) + { + ans++; + now[pos[q[i].num1] = pos[q[i].num2]] = q[i].num1; + pos[q[i].num2] = 0; + } + continue; + } + } + printf("Case %d: %d\n", ++tc, ans); + } + return 0; +} diff --git a/12790.cpp b/12790.cpp new file mode 100644 index 0000000..ab40028 --- /dev/null +++ b/12790.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define MAXN 1024 + +char A[32767], B[32767]; +const char hand[] = "RPS"; +double dp[131072][3], edp[131072][3]; +int resultRPS(char a, char b) +{ + if (a == b) + return 0; + if (a == 'R' && b == 'P') + return -1; + if (a == 'P' && b == 'S') + return -1; + if (a == 'S' && b == 'R') + return -1; + return 1; +} +int rps2num(char a) +{ + if (a == 'R') + return 0; + if (a == 'P') + return 1; + if (a == 'S') + return 2; + return 0; +} +// rock-paper-scissors + +int main() +{ + int testcase; + int n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s %s", A, B); + n = (int)strlen(A); + double R, P, S; + scanf("%lf %lf %lf", &R, &P, &S); + int oscore = 0; + for (int i = 0; i < n; i++) + oscore += resultRPS(A[i], B[i]); + memset(dp, 0, sizeof(dp)); + memset(edp, 0, sizeof(edp)); + R /= 100, P /= 100, S /= 100; + dp[0][0] = R; + dp[0][1] = P; + dp[0][2] = S; + for (int i = 0; i < n; i++) + { + for (int p = 0; p < 3; p++) + {// now + if (resultRPS(hand[p], B[i]) == 0) + { + dp[i + 1][0] += dp[i][p] * R; + dp[i + 1][1] += dp[i][p] * P; + dp[i + 1][2] += dp[i][p] * S; + edp[i + 1][0] += edp[i][p] * R; + edp[i + 1][1] += edp[i][p] * P; + edp[i + 1][2] += edp[i][p] * S; + } + else if (resultRPS(hand[p], B[i]) == 1) + { + dp[i + 1][(p + 1) % 3] += dp[i][p]; + edp[i + 1][(p + 1) % 3] += edp[i][p] + 1 * dp[i][p]; + } + else + { + int q = rps2num(B[i]); + dp[i + 1][(q + 1) % 3] += dp[i][p]; + edp[i + 1][(q + 1) % 3] += edp[i][p] - 1 * dp[i][p]; + } + } + } + double e = edp[n][0] + edp[n][1] + edp[n][2]; + printf("%d %.4lf %c\n", oscore, e, e > oscore ? 'Y' : 'N'); + } + return 0; +} +/* + 4 + SPR + PPR + 5 80 15 + RRR + PPR + 5 80 15 + S + S + 33 34 33 + S + S + 34 33 33 +*/ diff --git a/12791.cpp b/12791.cpp new file mode 100644 index 0000000..c36bf9b --- /dev/null +++ b/12791.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int X, Y; + while (scanf("%d %d", &X, &Y) == 2) + { + double l = 0, r = 1e+30, mid; + int ret = 0; +#define eps 1e-8 + while (fabs(l - r) > eps) + { + mid = (l + r) / 2; + if (mid * (1.0 / X - 1.0 / Y) >= 1) + r = mid; + else + l = mid; + } + printf("%.0lf\n", ceil(mid / X)); + } + return 0; +} +/* +1 10 +4 8 +5 7 +6875 7109 +*/ diff --git a/12792.cpp b/12792.cpp new file mode 100644 index 0000000..42d6d77 --- /dev/null +++ b/12792.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int A[1 << 21], used[1 << 21] = {}; +int testcase = 0; +long long gcd(long long x, long long y) +{ + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; +} +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + if (i & 1) + A[i] = i / 2; + else + A[i] = i / 2 + n / 2; + } + + testcase++; + long long lcm = 1; + for (int i = 0; i < n; i++) + { + if (A[i] != i && used[i] != testcase) + { + int ss = 0; + for (int j = i; used[j] != testcase; j = A[j]) + used[j] = testcase, ss++; + lcm = lcm / gcd(lcm, ss) * ss; + } + } + printf("%d\n", lcm); + } + return 0; +} +/* +4 +6 +2 +100002 +*/ diff --git a/12793.cpp b/12793.cpp new file mode 100644 index 0000000..0fe1e29 --- /dev/null +++ b/12793.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int main() +{ + int n, m, x, y, z; + int A[512], B[512], C[512], D[512]; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + scanf("%d %d %d %d", A + i, B + i, C + i, D + i); + map R; + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &x, &y, &z); + unsigned int a = 63689, b = 378551; + unsigned int value = 0; + for (int j = 0; j < n; j++) + { + if (A[j] * x + B[j] * y + C[j] * z > D[j]) + value = value * a + 1; + else + value = value * a + 0; + a *= b; + } + R[value]++; + } + int ret = 0; + for (map::iterator it = R.begin(); + it != R.end(); it++) + ret = max(ret, it->second); + printf("%d\n", ret); + } + return 0; +} +/* +2 5 +1 0 0 1 +2 0 0 8 +0 1 0 +2 2 2 +3 3 3 +5 5 5 +2 18 4 +4 8 +0 0 1 1 +1 0 1 2 +-1 1 1 3 +-1 -1 1 3 +0 0 5 +0 0 4 +0 0 -2 +1 0 5 +40 19 104 +13 26 84 +89 -45 18 +3 1 0 +*/ diff --git a/12794.cpp b/12794.cpp new file mode 100644 index 0000000..41dfe1c --- /dev/null +++ b/12794.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +// each chamber is part of at most one cycle. +vector> g[10005]; +int visited[10005], dist[10005], cycle[10005]; +int dfs(int u, int p, int st, int cost) +{ + visited[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i].first, w = g[u][i].second; + if (v == st && v != p) + return cost + w; + if (visited[v] == 0) + { + int ret = dfs(v, u, st, cost + w); + if (ret) + { + cycle[u] = cycle[v] = ret; + return ret; + } + } + } + return 0; +} +void dijkstra(int st, int n) +{ + for (int i = 1; i <= n; i++) + dist[i] = 1e+9; + int u, v, d; + priority_queue, vector>, greater>> Q; + Q.push(make_pair(0, st)), dist[st] = 0; + + while (!Q.empty()) + { + u = Q.top().second, d = Q.top().first; + Q.pop(); + if (dist[u] < d) + continue; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i].first, w = g[u][i].second; + if (dist[v] > dist[u] + w) + { + dist[v] = dist[u] + w; + Q.push(make_pair(dist[v], v)); + } + } + } +} +int main() +{ + int n, m, q, st; + int x, y, w; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + g[i].clear(), cycle[i] = 0; + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &x, &y, &w); + g[x].push_back(make_pair(y, w)); + g[y].push_back(make_pair(x, w)); + } + for (int i = 1; i <= n; i++) + { + if (cycle[i] == 0 && g[i].size() > 1) + { + memset(visited, 0, sizeof(visited)); + cycle[i] = dfs(i, i, i, 0); + } + } + scanf("%d", &q); + while (q--) + { + scanf("%d %d", &st, &w); + dijkstra(st, n); + int ret = 1e+9; + for (int i = 1; i <= n; i++) + { + if (cycle[i] >= w) + ret = min(ret, cycle[i] + dist[i] * 2); + } + printf("%d\n", ret == 1e+9 ? -1 : ret); + } + } + return 0; +} +/* +4 4 +1 2 12 +2 3 10 +3 4 8 +2 4 5 +3 +1 23 +4 10 +1 24 +8 9 +1 2 1 +2 3 1 +3 4 1 +2 5 10 +5 6 25 +2 6 20 +3 7 9 +7 8 3 +3 8 4 +4 +1 10 +4 60 +8 5 +7 55 +*/ diff --git a/12795.cpp b/12795.cpp new file mode 100644 index 0000000..98b7937 --- /dev/null +++ b/12795.cpp @@ -0,0 +1,194 @@ +#include + +using namespace std; + +int g[64][64], n, m; +int used[64][64]; +pair path[64], pre[64][64]; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +struct Area +{ + vector> V; + bool operator<(const Area &a) const + { + for (int i = 0; i < V.size(); i++) + if (V[i] != a.V[i]) + return V[i] < a.V[i]; + return false; + } + Area() + { + V.clear(); + } +}; +// Number of fixed polyominoes with n cells. https://oeis.org/A001168 +set shape[16][16][16]; + +void storeArea(Area a) +{ + sort(a.V.begin(), a.V.end()); + int x, y, mx, my; + x = a.V[0].first, y = a.V[0].second; + mx = x, my = y; + for (int i = 0; i < a.V.size(); i++) + { + x = min(x, a.V[i].first), y = min(y, a.V[i].second); + mx = max(mx, a.V[i].first), my = max(my, a.V[i].second); + } + for (int i = 0; i < a.V.size(); i++) + a.V[i].first -= x, a.V[i].second -= y; + sort(a.V.begin(), a.V.end()); + shape[a.V.size()][mx - x][my - y].insert(a); + assert(mx - x >= 0 && mx - x < 16 && my - y >= 0 && my - y < 16); +} +void dfs(int idx, int x, int y, int pick, int m) +{ + if (pick == m) + { + Area t; + for (int i = 0; i < pick; i++) + t.V.push_back(path[i]); + storeArea(t); + return; + } + if (idx >= 2 * m) + return; + vector> test; + for (int i = 0; i < 4; i++) + { + int tx = x + dx[i], + ty = y + dy[i]; + if (used[tx][ty]) + continue; + pre[tx][ty] = make_pair(x, y); + path[pick] = make_pair(tx, ty); + used[tx][ty] = 1; + dfs(idx + 1, tx, ty, pick + 1, m); + test.push_back(make_pair(tx, ty)); + } + if (pre[x][y].first != -1)// stop on leaf + dfs(idx + 1, pre[x][y].first, pre[x][y].second, pick, m); + for (int i = 0; i < test.size(); i++) + { + int tx = test[i].first, + ty = test[i].second; + used[tx][ty] = 0; + } +} + +int place(int x, int y, int n, const Area &a) +{ + int ox = x, oy = y; + int sum = 0; + for (int i = 0; i < a.V.size(); i++) + { + x = ox + a.V[i].first; + y = oy + a.V[i].second; + if (x < 0 || y < 0 || x >= n || y >= n) + return 0; + sum += g[x][y]; + } + return sum; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + for (int i = 1; i <= 10; i++) + { + for (int j = 0; j < 32; j++) + for (int k = 0; k < 32; k++) + pre[j][k] = make_pair(-1, -1); + pre[11][11] = make_pair(-1, -1); + path[0] = make_pair(11, 11); + used[11][11] = 1; + dfs(1, 11, 11, 1, i); + used[11][11] = 0; + int sum = 0; + for (int j = 0; j < i; j++) + { + for (int k = 0; k < i; k++) + sum += shape[i][j][k].size(); + } + // printf("complete %d %d\n", i, sum); + } + while (scanf("%d %d", &n, &m) == 2) + { + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + scanf("%d", &g[i][j]); + } + } + + int ret = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int mx[16] = {}, mxx = 0, tmp; + for (int p = 0; p < m && i + p <= n; p++) + { + mxx = 0; + for (int q = 0; q < m && j + q <= n; q++) + { + mx[q] = max(mx[q], g[i + p][j + q]); + mxx = max(mxx, mx[q]); + if (mxx * m <= ret) + continue; + vector D; + for (int a = i; a <= i + p; a++) + for (int b = j; b <= j + q; b++) + D.push_back(g[a][b]); + sort(D.begin(), D.end(), greater()); + tmp = 0; + for (int a = 0; a < m; a++) + tmp += D[a]; + if (tmp <= ret) + continue; + for (set::iterator it = shape[m][p][q].begin(); + it != shape[m][p][q].end(); it++) + { + ret = max(ret, place(i, j, n, *it)); + } + } + } + } + } + printf("%d\n", ret); + } + return 0; +} +/* +5 6 +31 12 7 1 14 +23 98 3 87 1 +5 31 8 2 99 +12 3 42 17 88 +120 2 7 5 7 +4 8 +1 1 1 1 +9 9 9 1 +9 1 9 1 +9 9 9 1 +3 5 +0 5 0 +5 5 5 +0 5 0 + +10 5 +733 950 26 696 512 570 327 531 829 600 +499 459 728 877 673 464 368 438 566 599 +512 631 242 499 919 931 688 602 490 172 +587 745 704 453 475 370 47 439 705 844 +133 449 264 732 361 612 196 635 739 853 +944 872 938 228 74 296 604 677 801 27 +763 628 650 40 558 159 7 500 405 423 +450 455 26 543 881 87 292 431 74 546 +349 115 568 589 390 40 606 802 434 479 +732 890 361 334 208 439 118 18 494 894 +*/ diff --git a/12796.cpp b/12796.cpp new file mode 100644 index 0000000..1350da7 --- /dev/null +++ b/12796.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +const int mod = 10000; +struct Matrix +{ + int v[100][100]; + int row, col;// row x col + Matrix(int n, int m, int a = 0) + { + memset(v, 0, sizeof(v)); + row = n, col = m; + for (int i = 0; i < row && i < col; i++) + v[i][i] = a; + } + Matrix operator*(const Matrix &x) const + { + Matrix ret(row, x.col); + for (int i = 0; i < row; i++) + { + for (int k = 0; k < col; k++) + { + if (v[i][k]) + for (int j = 0; j < x.col; j++) + { + ret.v[i][j] += v[i][k] * x.v[k][j], + ret.v[i][j] %= mod; + } + } + } + return ret; + } + Matrix operator^(const int &n) const + { + Matrix ret(row, col, 1), x = *this; + int y = n; + while (y) + { + if (y & 1) + ret = ret * x; + y = y >> 1, x = x * x; + } + return ret; + } +}; +int main() +{ + int N, L, S, T; + int x, y; + while (scanf("%d %d", &N, &L) == 2) + { + scanf("%d %d", &S, &T); + Matrix m(N, N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < 4; j++) + { + scanf("%d", &x), x--; + m.v[i][x]++; + } + } + Matrix ret = m ^ L; + S--, T--; + printf("%d\n", ret.v[S][T]); + } + return 0; +} +/* +2 20 +1 1 +2 2 2 2 +1 1 1 1 +2 29 +1 1 +2 2 2 2 +1 1 1 1 +2 0 +1 1 +2 2 2 2 +1 1 1 1 +2 0 +1 2 +2 2 2 2 +1 1 1 1 +3 2 +3 1 +1 2 2 2 +2 1 3 2 +2 2 3 1 +*/ diff --git a/12797.cpp b/12797.cpp new file mode 100644 index 0000000..01dfa4a --- /dev/null +++ b/12797.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int N, letter_cnt[128], letter_used[128]; +int used[128][128] = {}, dist[128][128], testcase = 0, ret; +char g[128][128]; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; +void dfs(int idx) +{ + if (idx == 10) + { + queue X, Y; + int tx, ty, x, y; + if (letter_used[g[0][0]] == 0) + return; + testcase++; + X.push(0), Y.push(0); + used[0][0] = testcase; + dist[0][0] = 1; + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + if (x == N - 1 && y == N - 1) + { + ret = min(ret, dist[x][y]); + return; + } + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= N || ty >= N) + continue; + if (used[tx][ty] == testcase || letter_used[g[tx][ty]] == 0) + continue; + used[tx][ty] = testcase; + dist[tx][ty] = dist[x][y] + 1; + X.push(tx), Y.push(ty); + } + } + return; + } + int c = 0; + if (letter_cnt[idx + 'a']) + { + letter_used[idx + 'a'] = 1; + dfs(idx + 1); + letter_used[idx + 'a'] = 0; + c++; + } + if (letter_cnt[idx + 'A']) + { + letter_used[idx + 'A'] = 1; + dfs(idx + 1); + letter_used[idx + 'A'] = 0; + c++; + } + if (c == 0) + dfs(idx + 1); +} +int main() +{ + while (scanf("%d", &N) == 1) + { + for (int i = 0; i < N; i++) + scanf("%s", g[i]); + memset(letter_cnt, 0, sizeof(letter_cnt)); + memset(letter_used, 0, sizeof(letter_used)); + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + letter_cnt[g[i][j]]++; + ret = 0x3f3f3f3f; + dfs(0); + printf("%d\n", ret == 0x3f3f3f3f ? -1 : ret); + } + return 0; +} +/* +6 +DdaAaA +CBAcca +eEaeeE +bBbabB +DbDdDc +fFaAaC +7 +aAaaaaa +aAaaaAa +aAaaaAA +aaAaAaa +AaAaaAa +aaAAaAa +aaaaaAa +*/ diff --git a/12798.cpp b/12798.cpp new file mode 100644 index 0000000..4faeecb --- /dev/null +++ b/12798.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + int ret = 0, x; + for (int i = 0; i < n; i++) + { + int ok = 1; + for (int j = 0; j < m; j++) + { + scanf("%d", &x); + ok &= x > 0; + } + ret += ok; + } + printf("%d\n", ret); + } + return 0; +} +/* +5 3 +0 0 0 +1 0 5 +0 0 0 +0 1 2 +1 1 0 +12 5 +4 4 2 3 7 +0 0 0 1 0 +7 4 7 0 6 +1 2 3 3 2 +0 0 0 0 0 +4 0 9 10 10 +0 1 0 0 0 +1 2 0 2 3 +10 10 10 1 0 +0 3 3 3 4 +10 10 0 10 10 +1 1 2 0 9 +*/ diff --git a/12799.cpp b/12799.cpp new file mode 100644 index 0000000..3336068 --- /dev/null +++ b/12799.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define MOD 100000000 +long long inv(long long n, long long m) +{// get n*? = 1 (mod m) + long long la = 1, lb = 0, ra = 0, rb = 1; + long long i = 0, t, mod = m; + while (n % m) + { + if (!i) + { + la -= n / m * ra; + lb -= n / m * rb; + } + else + { + ra -= n / m * la; + rb -= n / m * lb; + } + i = !i; + t = n, n = m, m = t % m; + } + if (i) + return (la % mod + mod) % mod; + return (ra % mod + mod) % mod; +} +long long mpow(long long x, long long y, long long mod) +{ + long long ret = 1; + while (y) + { + if (y & 1) + ret = (ret * x) % mod; + y >>= 1, x = (x * x) % mod; + } + return ret; +} +int main() +{ + int N, E, C; + while (scanf("%d %d %d", &N, &E, &C) == 3) + { + int sqr = (int)sqrt(N); + int phi = N; + for (int i = 3; i <= sqr; i++) + { + if (N % i == 0) + phi = (i - 1) * (N / i - 1); + } + int d = inv(E, phi); + printf("%lld\n", mpow(C, d, N)); + } + return 0; +} +/* +1073 71 436 +91 43 19 +*/ diff --git a/128.cpp b/128.cpp new file mode 100644 index 0000000..03e296a --- /dev/null +++ b/128.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const char ss[] = "0123456789ABCDEFGH"; + +char str[1200]; +int C; + +int Rem() +{ + int i, d = 1, g = 34943, v, rem = 0, p, l = strlen(str); + if (!l) + { + return l; + } + for (i = l - 1; i >= 0; i--) + { + v = str[i]; + p = d * v; + p %= g; + rem += p; + rem %= g; + d *= 256; + d %= g; + } + p = (256 * 256) % g; + p *= rem; + return p % g; +} + +void Cal() +{ + int d, ind = 0, i; + char xx[12]; + d = Rem(); + if (!d) + { + printf("00 00\n"); + return; + } + d = 34943 - d; + while (d) + { + xx[ind++] = ss[d % 16]; + d /= 16; + } + for (i = ind; i < 4; i++) + { + xx[ind++] = '0'; + } + printf("%c%c", xx[3], xx[2]); + printf(" %c%c\n", xx[1], xx[0]); +} + +int main() +{ + while (1) + { + gets(str); + if (!strcmp(str, "#")) + { + break; + } + Cal(); + } + return 0; +} diff --git a/1280.cpp b/1280.cpp new file mode 100644 index 0000000..22baf3e --- /dev/null +++ b/1280.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define MAXN 12000 +#define pi pair +#define y first +#define x second + +int n, cs; +double a[32], b[32]; +double _lo, lo, hi, inc, V; +vector v; + +double f(double x) +{ + double r = 0.0; + for (int i = n; i >= 0; i -= 1) + { + r = r * x + a[i]; + } + return r * x; +} + +double R(double x) +{ + return int(x * 100.0 + 0.5 + 1e-6) / 100.0; +} + +int main() +{ + while (cin >> n) + { + for (int i = 0; i <= 2 * n; i += 1) + { + a[i] = 0, b[i] = 0; + } + for (int i = 0; i <= n; i += 1) + { + cin >> b[i]; + } + for (int i = 0; i <= 2 * n; i += 1) + for (int j = 0; j <= i; j += 1) + { + a[i] += b[j] * b[i - j]; + } + n *= 2; + for (int i = 0; i <= n; i += 1) + { + a[i] *= acos(-1) / double(i + 1); + } + cin >> lo >> hi >> inc; + V = f(hi) - f(lo); + cout << "Case " << ++cs << ": "; + printf("%.2lf\n", R(V)); + if (inc > V) + { + cout << "insufficient volume\n"; + } + else + { + v.clear(); + double p; + _lo = lo; + double dis = f(lo) + inc; + while (dis <= f(hi) && int(v.size()) < 8) + { + double p; + for (double x = lo + 0.0001;; x += 0.0001) + { + if (fabs(f(x) - dis) <= fabs(f(x - 0.0001) - dis)) + { + p = x; + } + else + { + break; + } + } + lo = p; + v.push_back(lo); + dis += inc; + } + printf("%.2lf", R(v[0] - _lo)); + for (int i = 1; i < v.size(); i += 1) + { + printf(" %.2lf", R(v[i] - _lo)); + } + cout << '\n'; + } + } + return 0; +} diff --git a/12800.cpp b/12800.cpp new file mode 100644 index 0000000..45e0492 --- /dev/null +++ b/12800.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define MAXN 256 +double dp[MAXN][MAXN], dist[MAXN][MAXN]; +int used[MAXN][MAXN]; +double dfs(int l, int r) +{ + if (used[l][r]) + return dp[l][r]; + if (r - l <= 3) + return 0; + used[l][r] = 1; + double &ret = dp[l][r]; + ret = 1e+30; + for (int i = l + 1; i <= r - 1; i += 2) + { + for (int j = i + 1; j <= r - 1; j += 2) + { + ret = min(ret, dfs(l, i) + dfs(i, j) + dfs(j, r) + + dist[l][i] + dist[i][j] + dist[j][r]); + } + } + return ret; +} +int main() +{ + int n; + double x[MAXN], y[MAXN]; + while (scanf("%d", &n) == 1) + { + n = n * 2; + for (int i = 0; i < n; i++) + scanf("%lf %lf", &x[i], &y[i]); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + dist[i][j] = hypot(x[i] - x[j], y[i] - y[j]); + used[i][j] = 0; + } + dist[i][i + 1] = 0; + } + double ret = dfs(0, n - 1); + printf("%.4lf\n", ret); + } + return 0; +} +/* +4 +5715.7584 3278.6962 +3870.5535 4086.7950 +3823.2104 4080.7543 +3574.4323 170.2905 +4521.4796 144.9156 +4984.6486 306.2896 +5063.1061 347.1661 +6099.9959 2095.9358 +2 +6044.4737 2567.9978 +5752.5635 3226.5140 +5148.8242 3802.9292 +4598.8042 4036.8000 +*/ diff --git a/12801.cpp b/12801.cpp new file mode 100644 index 0000000..05ae03b --- /dev/null +++ b/12801.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int main() +{ + int n, m, A[65536]; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < m; i++) + scanf("%d", &A[i]); + A[m] = A[0] + n; + int ret = 0, div = n / m; + for (int i = A[0]; i < A[1]; i++) + { + int d = i, ok = 1; + for (int j = 1; j <= m; j++) + { + // printf("(%d %d] %d\n", d, d+div, A[j]); + if (d < A[j] && A[j] <= d + div) + d += div; + else + { + ok = 0; + break; + } + } + // puts(""); + if (ok) + { + ret = 1; + break; + } + } + puts(ret ? "S" : "N"); + } + return 0; +} +/* +12 3 +2 8 11 +12 4 +4 5 7 11 +*/ diff --git a/12802.cpp b/12802.cpp new file mode 100644 index 0000000..3c43367 --- /dev/null +++ b/12802.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define maxL (1000000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[maxL]; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 1000000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + } + } +} +int isPalindromePrime(int n) +{ + if (n == 1) + return 1; + if (GET(n)) + return 0; + char buf[20]; + sprintf(buf, "%d", n); + int m = strlen(buf); + for (int i = 0, j = m - 1; i < j; i++, j--) + if (buf[i] != buf[j]) + return 0; + return 1; +} +int main() +{ + sieve(); + int n; + while (scanf("%d", &n) == 1) + { + printf("%d\n", n << 1); + if (isPalindromePrime(n)) + break; + } + return 0; +} diff --git a/12803.cpp b/12803.cpp new file mode 100644 index 0000000..b159e66 --- /dev/null +++ b/12803.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +int priority_op(char c) +{ + switch (c) + { + case '(': + return 0; + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + } + return -1; +} +void trans(char infix[], char buffer[]) +{ + int len = strlen(infix); + char *ptr = buffer; + stack op; + *ptr = '\0'; + for (int i = 0; i < len; i++) + { + if (infix[i] == ' ') + continue; + if (infix[i] >= '0' && infix[i] <= '9' || + (infix[i] == '-' && infix[i + 1] >= '0' && infix[i + 1] <= '9')) + { + while (infix[i] >= '0' && infix[i] <= '9' || + (infix[i] == '-' && infix[i + 1] >= '0' && infix[i + 1] <= '9') || infix[i] == '.') + { + sprintf(ptr, "%c", infix[i]), i++; + while (*ptr) + ptr++; + } + sprintf(ptr, " "); + while (*ptr) + ptr++; + i--; + } + else + { + if (infix[i] == ')') + { + while (!op.empty() && op.top() != '(') + { + sprintf(ptr, "%c ", op.top()), op.pop(); + while (*ptr) + ptr++; + } + op.pop(); + } + else + { + if (infix[i] != '(') + while (!op.empty() && priority_op(op.top()) >= priority_op(infix[i])) + { + sprintf(ptr, "%c ", op.top()), op.pop(); + while (*ptr) + ptr++; + } + op.push(infix[i]); + } + } + } + while (!op.empty()) + { + sprintf(ptr, "%c ", op.top()), op.pop(); + while (*ptr) + ptr++; + } +} +int isOper(char c) +{ + switch (c) + { + case '(': + return 1; + case '+': + case '-': + return 1; + case '*': + case '/': + return 1; + } + return 0; +} +double calcPostfix(char postfix[]) +{ + stringstream sin(postfix); + string token; + stack stk; + double a, b, c; + while (sin >> token) + { + if (token.length() == 1 && isOper(token[0])) + { + b = stk.top(), stk.pop(); + a = stk.top(), stk.pop(); + switch (token[0]) + { + case '+': + a = a + b; + break; + case '-': + a = a - b; + break; + case '*': + a = a * b; + break; + case '/': + a = a / b; + break; + } + stk.push(a); + } + else + { + stringstream cc(token); + cc >> c; + stk.push(c); + } + } + return stk.top(); +} +char infix[262144], postfix[262144]; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (getchar() != '\n') + ; + while (testcase--) + { + gets(infix); + trans(infix, postfix); + printf("%.2lf\n", calcPostfix(postfix)); + } + return 0; +} +/* +3 +( 3.00 + 4.50 ) +( 5.00 - ( 2.50 * 3.00 ) ) +( ( 7.00 / 3.00 ) + ( 4.00 - ( 3.00 * 7.00 ) ) ) +*/ diff --git a/12804.cpp b/12804.cpp new file mode 100644 index 0000000..58cde1b --- /dev/null +++ b/12804.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 1024 +vector g[MAXN]; +char cmd[MAXN][16]; +int N[MAXN], used[MAXN], instk[MAXN]; +int loopflag, endflag; +void dfs(int u) +{ + instk[u] = 1, used[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + if (used[g[u][i]] == 0) + dfs(g[u][i]); + if (instk[g[u][i]] == 1) + loopflag = 1; + } + instk[u] = 0; +} +int main() +{ + int testcase, L; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &L); + for (int i = 0; i <= L + 1; i++) + g[i].clear(); + for (int i = 1; i <= L; i++) + { + scanf("%s", cmd[i]); + if (cmd[i][0] == 'A') + { + g[i].push_back(i + 1); + } + else if (cmd[i][0] == 'J') + { + scanf("%d", &N[i]); + g[i].push_back(N[i]); + } + else if (cmd[i][0] == 'C') + { + scanf("%d", &N[i]); + g[i].push_back(N[i]); + g[i].push_back(i + 1); + } + } + memset(used, 0, sizeof(used)); + memset(instk, 0, sizeof(instk)); + loopflag = 0, endflag = 0; + dfs(1); + endflag = used[L + 1]; + if (loopflag == 0 && endflag == 1) + puts("ALWAYS"); + else if (loopflag == 1 && endflag == 0) + puts("NEVER"); + else + puts("SOMETIMES"); + } + return 0; +} +/* +3 +3 +A +A +J 1 +5 +A +J 4 +J 5 +C 3 +A +4 +A +A +C 2 +A +*/ diff --git a/12805.cpp b/12805.cpp new file mode 100644 index 0000000..eb04dcd --- /dev/null +++ b/12805.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +#define maxL (50000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[maxL]; +int P[32767], Pt; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 50000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} +vector> factor(int n) +{ + vector> R; + + for (int i = 0, j; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + for (j = 0; n % P[i] == 0; n /= P[i], j++) + ; + R.push_back(make_pair(P[i], j)); + } + } + if (n != 1) + R.push_back(make_pair(n, 1)); + return R; +} +int main() +{ + sieve(); + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + vector> f = factor(n); + int sign = 0; + for (int i = 0; i < f.size(); i++) + { + if (f[i].first == 2) + { + } + else + { + if ((f[i].first - 1) % 4 == 0) + sign += f[i].second; + } + } + puts(sign & 1 ? "-" : "+"); + } + return 0; +} diff --git a/12806.cpp b/12806.cpp new file mode 100644 index 0000000..1bcb26e --- /dev/null +++ b/12806.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +long long C[50][50]; +const char kind[20] = "ADPMd23456789TJQK"; +int ihand[20], ohand[20], mhand[20]; +int suffix[20]; +long long va[10], vb; +void dfs(int idx, int card, long long ways) +{ + if (idx > 4) + { + ways = ways * C[suffix[idx]][14 - card]; + int hh = 0; + if (ohand[1] > 0 && ohand[2] > 0 && ohand[0] > 0) + hh = 1, va[hh] += ways;// D P A + if (ohand[1] > 0 && ohand[0] >= 2) + hh = 2, va[hh] += ways;// D A A + if (ohand[2] > 0 && ohand[0] >= 3) + hh = 3, va[hh] += ways;// P A A A + if (ohand[1] > 0 && ohand[2] > 0 && ohand[3] > 0 && ohand[4] > 0) + hh = 4, va[hh] += ways;// D P M d + vb += ways; + return; + } + if (card == 14) + { + int hh = 0; + if (ohand[1] > 0 && ohand[2] > 0 && ohand[0] > 0) + hh = 1, va[hh] += ways;// D P A + if (ohand[1] > 0 && ohand[0] >= 2) + hh = 2, va[hh] += ways;// D A A + if (ohand[2] > 0 && ohand[0] >= 3) + hh = 3, va[hh] += ways;// P A A A + if (ohand[1] > 0 && ohand[2] > 0 && ohand[3] > 0 && ohand[4] > 0) + hh = 4, va[hh] += ways;// D P M d + vb += ways; + return; + } + if (idx == 17 || card + suffix[idx] < 14) + return; + for (int i = 0; i <= ihand[idx] && card + i <= 14; i++) + { + ohand[idx] += i; + dfs(idx + 1, card + i, ways * C[ihand[idx]][i]); + ohand[idx] -= i; + } +} +int main() +{ + for (int i = 0; i < 50; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; + } + char hand[64]; + while (scanf("%s", &hand) == 1 && hand[0] != '0') + { + memset(ihand, 0, sizeof(ihand)); + memset(ohand, 0, sizeof(ohand)); + for (int i = 4; i < 17; i++) + ihand[i] = 4, mhand[i] = 4; + ihand[0] = 4, mhand[0] = 4; + for (int i = 1; i <= 4; i++) + ihand[i] = 1, mhand[i] = 1; + for (int i = 0; i < 8; i++) + { + int p = find(kind, kind + 17, hand[i]) - kind; + ihand[p]--, ohand[p]++; + } + memset(suffix, 0, sizeof(suffix)); + for (int i = 17; i >= 0; i--) + suffix[i] = suffix[i + 1] + ihand[i]; + memset(va, 0, sizeof(va)); + vb = 0; + dfs(0, 8, 1); + int ok = 0; + for (int i = 0; i < 4; i++) + { + double p = (double)va[i] / vb; + if (p > 0.25) + ok = 1; + } + if (ok) + puts("Grand Tichu!"); + else + puts("..."); + } + return 0; +} +/* +D23468AA +D2P4688K +D23468KA +D2233889 +PdM2345T +PdM234AA +0 + +D23468AA +0.000000 +0.125000 +1.000000 +0.025439 +Grand Tichu +D2P4688K +0.000000 +0.424761 +0.070768 +0.004394 +Grand Tichu +D23468KA +0.000000 +0.125000 +0.336263 +0.003315 +Grand Tichu +D2233889 +0.000000 +0.046558 +0.070768 +0.000298 +... +PdM2345T +0.000000 +0.046558 +0.006332 +0.004394 +... +PdM234AA +0.000000 +0.125000 +0.125000 +0.236702 +... +*/ diff --git a/12808.cpp b/12808.cpp new file mode 100644 index 0000000..e4fde43 --- /dev/null +++ b/12808.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + double L, D, H, V; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lf %lf %lf %lf", &L, &D, &H, &V); + L /= 1000; + D /= 1000; + H /= 1000; + V /= 1000; + double t = sqrt(H * 2 / 9.8), d = V * t; + if (fabs(d - D) < 0.5 || fabs(d - (D + L)) < 0.5) + puts("EDGE"); + else if (d < D || d > D + L) + puts("FLOOR"); + else + puts("POOL"); + } + return 0; +} diff --git a/12809.cpp b/12809.cpp new file mode 100644 index 0000000..7c9aa00 --- /dev/null +++ b/12809.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +double f[128], sum[128], w[128][128], dp[128][128]; +int arg_[128][128]; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + for (int i = 1; i <= n; i++) + { + scanf("%lf", &f[i]); + } + sum[0] = 0; + for (int i = 1; i <= n; i++) + { + sum[i] = sum[i - 1] + f[i]; + } + for (int i = 1; i <= n; i++) + { + for (int j = i; j <= n; j++) + { + w[i][j] = sum[j] - sum[i - 1]; + } + } + for (int i = 0; i <= n; i++) + { + dp[i][i] = 0, arg_[i][i] = i; + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; i + j <= n; j++) + { + double mn = 1e+30; + int idx = -1; + for (int k = arg_[j][i + j - 1]; k <= arg_[j + 1][i + j]; k++) + { + double t = dp[j][k - 1] + dp[k + 1][i + j] + w[j][k - 1] + w[k + 1][i + j]; + if (t < mn) + { + mn = t, idx = k; + } + } + dp[j][i + j] = mn, arg_[j][i + j] = idx; + } + } + printf("%lf\n", dp[1][n] + 1); + } + return 0; +} diff --git a/1281.cpp b/1281.cpp new file mode 100644 index 0000000..14c6c60 --- /dev/null +++ b/1281.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAXN 100500 +#define pi pair +#define y first +#define x second + +int N, E, A, cs, a, b; +int m[1 << 20], src[32][1 << 20], dst[32][1 << 20], d[32][32], t[32][32]; +vector v[32]; + +int main() +{ + for (int i = 0; i < 20; i += 1) + for (int n = 0; n < (1 << i); n += 1) + { + m[n | (1 << i)] = m[n] + 1; + } + while (cin >> N >> E) + { + for (int i = 0; i < N; i += 1) + for (int j = 0; j < N; j += 1) + { + t[i][j] = 1000000; + } + for (int i = 0; i < E; i += 1) + { + cin >> a >> b; + cin >> t[a][b]; + t[b][a] = t[a][b]; + } + for (int i = 0; i < N; i += 1) + { + t[i][i] = 0; + } + for (int k = 0; k < N; k += 1) + for (int i = 0; i < N; i += 1) + for (int j = 0; j < N; j += 1) + { + t[i][j] = min(t[i][j], t[i][k] + t[k][j]); + } + N -= 2; + for (int i = 0; i <= N; i += 1) + { + v[i].clear(); + } + for (int i = 0; i < (1 << N); i += 1) + { + v[m[i]].push_back(i); + for (int j = 0; j < N; j += 1) + { + src[j][i] = 1000000; + dst[j][i] = 1000000; + } + } + for (int i = 0; i < N; i += 1) + { + src[i][(1 << i)] = t[0][i + 1]; + dst[i][(1 << i)] = t[N + 1][i + 1]; + for (int j = 0; j < N; j += 1) + { + d[i][j] = t[i + 1][j + 1]; + } + } + if (N == 1) + { + A = 2 * (t[0][1] + t[1][2]); + goto hell; + } + for (int n = 2; n <= (N + 1) / 2; n += 1) + for (int i = 0; i < N; i += 1) + for (int j = 0; j < N; j += 1) + for (int k = 0; k < v[n].size(); k += 1) + if ((v[n][k] & (1 << i)) && (v[n][k] & (1 << j))) + { + src[i][v[n][k]] = min(src[i][v[n][k]], src[j][v[n][k] ^ (1 << i)] + d[j][i]); + } + for (int n = 2; n <= (N + 1) / 2; n += 1) + for (int i = 0; i < N; i += 1) + for (int j = 0; j < N; j += 1) + for (int k = 0; k < v[n].size(); k += 1) + if ((v[n][k] & (1 << i)) && (v[n][k] & (1 << j))) + { + dst[i][v[n][k]] = min(dst[i][v[n][k]], dst[j][v[n][k] ^ (1 << i)] + d[j][i]); + } + A = 1000000; + for (int i = 0; i < v[N / 2].size(); i += 1) + { + a = v[N / 2][i]; + b = ((1 << N) - 1) ^ a; + int fr = 1000000, bk = 1000000; + for (int i = 0; i < N; i += 1) + if (a & (1 << i)) + for (int j = 0; j < N; j += 1) + if (b & (1 << j)) + { + fr = min(fr, src[i][a] + d[i][j] + dst[j][b]), bk = min(bk, src[j][b] + d[i][j] + dst[i][a]); + } + A = min(A, fr + bk); + } + hell: + cout << "Case " << ++cs << ": " << A << "\n"; + } + return 0; +} diff --git a/12810.cpp b/12810.cpp new file mode 100644 index 0000000..055956f --- /dev/null +++ b/12810.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const long long mod = 1000000009LL; +long long mpow(long long x, long long y, long long mod) +{ + long long ret = 1; + while (y) + { + if (y & 1) + ret = (ret * x) % mod; + y >>= 1, x = (x * x) % mod; + } + return ret; +} +int main() +{ + int testcase, n; + long long div2 = mpow(2, mod - 2, mod); + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + long long p = 1, x; + for (int i = 0; i < n; i++) + { + scanf("%lld", &x); + p *= (1 + x * 2); + p %= mod; + } + p = ((p - 1) * div2) % mod; + printf("%lld\n", p); + } + return 0; +} diff --git a/12811.cpp b/12811.cpp new file mode 100644 index 0000000..8498488 --- /dev/null +++ b/12811.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const long long mod = 1000003; +char s[128]; +void dfs(long long cmd[]) +{ + scanf("%s", s); + if (s[0] == 'e' || s[0] == ']') + return; + long long x, ncmd[3] = {}; + scanf("%lld", &x); + if (s[0] == 'l') + cmd[0] += x, dfs(ncmd); + else if (s[0] == 'r' && s[1] == 'i') + cmd[1] += x, dfs(ncmd); + else if (s[0] == 'f') + cmd[2] += x, dfs(ncmd); + else if (s[0] == 'r' && s[1] == 'e') + { + scanf("%*s"); + dfs(cmd); + for (int i = 0; i < 3; i++) + cmd[i] = (cmd[i] * x) % mod; + dfs(ncmd); + } + for (int i = 0; i < 3; i++) + cmd[i] = (cmd[i] + ncmd[i]) % mod; +} +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%*s"); + long long cmd[3] = {}; + dfs(cmd); + printf("%lld %lld %lld\n", cmd[0], cmd[1], cmd[2]); + } + return 0; +} +/* + 2 + begin + forward 10 + left 90 + forward 10 + left 90 + forward 10 + left 90 + forward 10 + left 90 + end + begin + forward 10 + left 90 + forward 10 + left 90 + forward 10 + left 90 + forward 10 + left 90 + end */ \ No newline at end of file diff --git a/12812.cpp b/12812.cpp new file mode 100644 index 0000000..6b66e7e --- /dev/null +++ b/12812.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +char g[512][512]; +int dp[512][512]; +int main() +{ + int testcase; + int n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + { + scanf("%s", g[i]); + } + int ret = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] == '#') + { + int v1, v2, v3; + v1 = i - 1 >= 0 && j - 1 >= 0 ? dp[i - 1][j - 1] : 0; + v2 = i - 2 >= 0 ? dp[i - 2][j] : 0; + v3 = i - 1 >= 0 && j + 1 < m ? dp[i - 1][j + 1] : 0; + if (i - 1 >= 0 && g[i - 1][j] == '#') + dp[i][j] = min(v1, min(v2, v3)) + 1; + else + dp[i][j] = 1; + } + else + { + dp[i][j] = 0; + } + ret = max(ret, dp[i][j]); + } + } + printf("%d\n", ret * 2 - 1 < 0 ? 0 : ret * 2 - 1); + } + return 0; +} +/* + 2 + 3 3 + .#. + ### + .#. + 8 10 + ..##...#.. + .###..###. + ..#....... + ....##.... + ....###### + ...#####.. + ..######## + .....#.... + */ \ No newline at end of file diff --git a/12813.cpp b/12813.cpp new file mode 100644 index 0000000..91e225e --- /dev/null +++ b/12813.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +unsigned long long gcd(unsigned long long x, unsigned long long y) +{ + unsigned long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; +} +unsigned long long mpow(unsigned long long x, unsigned long long y) +{ + unsigned long long r = 1; + while (y) + { + if (y & 1) + r = r * x; + x = x * x, y >>= 1; + } + return r; +} +int main() +{ + char s[1024]; + while (gets(s)) + { + int sign = 1, flag = 0, tail = 0; + int cycle = 0; + unsigned long long num = 0, cnum = 0, head = 0; + int test = 0; + for (int i = 0; s[i]; i++) + { + if (s[i] == '-') + { + sign = -1; + } + else if (s[i] == '.') + { + flag = 1; + } + else if (s[i] == '(') + { + for (i++; s[i] != ')'; i++) + cnum = cnum * 10 + s[i] - '0', cycle++; + } + else + { + if (flag == 1) + tail++; + if (flag == 1) + num = num * 10 + s[i] - '0'; + else + head = head * 10 + s[i] - '0'; + test++; + } + } + assert(tail <= 8 && cycle <= 8 && cycle + tail <= 18); + unsigned long long a, b, g; + if (cycle) + a = num * mpow(10, cycle) + cnum - num, b = mpow(10, cycle) - 1; + else + a = num, b = 1; + g = gcd(a, b), a /= g, b /= g; + b = b * mpow(10, tail); + g = gcd(a, b), a /= g, b /= g; + int f = 0; + head += a / b; + if (sign == -1) + printf("-"); + if (head) + printf("%llu", head), f = 1; + if (a % b) + printf("%s%llu/%llu", f ? " " : "", a % b, b), f = 1; + if (!f) + printf("0"); + puts(""); + } + return 0; +} +/* +1.5 +10 +0.(3) +0.(142857) +0.1(23) +-0.(428571) +0.0(142857) +0.(9) +1.(9) +*/ diff --git a/12814.cpp b/12814.cpp new file mode 100644 index 0000000..d249fbb --- /dev/null +++ b/12814.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define p1 0.6 +#define p2 0.4 + +double day[7]; + +int main() +{ + int i, r = 1, c; + double sum; + while (r) + { + for (i = 0; i < 7; i++) + { + day[i] = 0; + } + for (i = r = 0; i < 7; i++) + { + r += scanf("%d", &c); + day[(i + 1) % 7] += c ? p1 : 0; + day[(i + 2) % 7] += c ? p2 : 0; + } + if (r != 7) + { + break; + } + for (i = sum = 0; i < 7; i++) + { + sum += day[i]; + } + for (i = 0; i < 7; i++) + { + if (i) + { + printf(" "); + } + if (sum) + { + printf("%.5lf", day[i] / sum); + } + else + { + printf("%.5lf", 0.0); + } + } + printf("\n"); + } + return 0; +} diff --git a/12815.cpp b/12815.cpp new file mode 100644 index 0000000..fe2d800 --- /dev/null +++ b/12815.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + int H, W, N, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &H, &W, &N); + vector> D; + for (int i = 0; i < N; i++) + { + scanf("%d %d", &x, &y); + D.push_back(make_pair(x, y)); + } + sort(D.begin(), D.end()); + int A[26]; + for (int i = 1; i <= H; i++) + A[i] = i - 1; + for (int i = 0; i < N; i++) + swap(A[D[i].second], A[D[i].second + 1]); + for (int i = 1; i <= H; i++) + { + if (i != 1) + putchar(' '); + printf("%c", A[i] + 'A'); + } + puts(""); + } + return 0; +} +/* + 3 + 5 30 2 + 10 3 + 20 1 + 5 30 7 + 4 4 + 8 3 + 12 2 + 16 1 + 20 2 + 24 3 + 28 4 + 5 30 2 + 10 2 + 20 1 + */ \ No newline at end of file diff --git a/12816.cpp b/12816.cpp new file mode 100644 index 0000000..4ec86a0 --- /dev/null +++ b/12816.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +struct Pt +{ + long long x, y; + Pt(long long a = 0, long long b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (x != a.x) + return x < a.x; + return y < a.y; + } + Pt operator-(const Pt &a) const + { + Pt ret; + ret.x = x - a.x; + ret.y = y - a.y; + return ret; + } +}; +long long dist(Pt a, Pt b) +{ + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} +long long cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +int main() +{ + int n; + Pt D[128]; + long long x, y; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%lld %lld", &x, &y), D[i] = Pt(x, y); + long long ret = 0; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + for (int k = j + 1; k < n; k++) + { + long long d[3] = {dist(D[i], D[j]), dist(D[j], D[k]), dist(D[k], D[i])}; + sort(d, d + 3); + if (cross(D[i], D[j], D[k]) != 0) + if ((d[0] == d[1] && d[1] != d[2]) || (d[1] == d[2] && d[0] != d[1])) + ret++; + } + } + } + printf("%lld\n", ret); + } + return 0; +} +/* + 6 + -4 1 + -3 3 + -2 1 + -2 0 + -1 1 + -1 -1 + 3 + -4 1 + -2 1 + -1 1 + */ \ No newline at end of file diff --git a/12817.cpp b/12817.cpp new file mode 100644 index 0000000..8b0c218 --- /dev/null +++ b/12817.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +int main() +{ + double R; + int N; + char cmd[6]; + while (scanf("%lf", &R) == 1) + { + scanf("%d", &N); + double sumr = 1, sump = 1, p; + for (int i = 0; i < N; i++) + { + scanf("%s %lf", cmd, &p); + if (cmd[0] == 'R') + sumr *= p, sump *= 1 - p; + else + sumr *= 1 - p, sump *= p; + } + sumr *= R, sump *= 1 - R; + printf("%lf %lf\n", sumr / (sumr + sump), sump / (sumr + sump)); + } + return 0; +} \ No newline at end of file diff --git a/12818.cpp b/12818.cpp new file mode 100644 index 0000000..23380a5 --- /dev/null +++ b/12818.cpp @@ -0,0 +1,206 @@ +#include + +using namespace std; + +#define eps 1e-6 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + return y < a.y; + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator/(const double a) const + { + return Pt(x / a, y / a); + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dist2(Pt a, Pt b) +{ + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} +double length(Pt a) +{ + return hypot(a.x, a.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double angle(Pt a, Pt b) +{ + return acos(dot(a, b) / length(a) / length(b)); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} +Pt getIntersection(Pt p, Pt l1, Pt q, Pt l2) +{ + double a1, a2, b1, b2, c1, c2; + double dx, dy, d; + a1 = l1.y, b1 = -l1.x, c1 = a1 * p.x + b1 * p.y; + a2 = l2.y, b2 = -l2.x, c2 = a2 * q.x + b2 * q.y; + d = a1 * b2 - a2 * b1; + dx = b2 * c1 - b1 * c2; + dy = a1 * c2 - a2 * c1; + return Pt(dx / d, dy / d); +} +Pt circle(Pt a, Pt b, Pt c) +{ + Pt mab = (a + b) / 2; + Pt mbc = (b + c) / 2; + Pt lab = b - a, lbc = c - b; + swap(lab.x, lab.y); + swap(lbc.x, lbc.y); + lab.x = -lab.x; + lbc.x = -lbc.x; + return getIntersection(mab, lab, mbc, lbc); +} + +int main() +{ + Pt a, b, c, p; + int cases = 0; + while (scanf("%lf %lf", &a.x, &a.y) == 2) + { + scanf("%lf %lf", &b.x, &b.y); + scanf("%lf %lf", &c.x, &c.y); + scanf("%lf %lf", &p.x, &p.y); + double ret = min(dist(p, a), dist(p, c)); + double a1, b1, c1; + Pt o = circle(a, b, c); + a1 = a.y - c.y, b1 = c.x - a.x, c1 = a1 * a.x + b1 * a.y;// line ac + // printf("%lf %lf %lf\n", a1, b1, c1); + // printf("%lf %lf\n", a1 * b.x + b1 * b.y - c1, a1 * p.x + b1 * p.y - c1); + // printf("%lf %lf\n", o.x, o.y); + if ((a1 * b.x + b1 * b.y - c1 > 0) == (a1 * o.x + b1 * o.y - c1 > 0)) + { + double d = dist(o, p), r = dist(o, a); + if ((a1 * b.x + b1 * b.y - c1 > 0) == (a1 * p.x + b1 * p.y - c1 > 0)) + { + if (cross(a, c, p) * cross(a, o, p) < eps && + cross(c, o, p) * cross(c, a, p) < eps && + cross(o, a, p) * cross(o, c, p) < eps) + { + } + else + { + if (d > r) + d -= r; + else + d = r - d; + ret = min(ret, d); + } + } + else + { + if (cross(o, a, p) * cross(o, c, p) > -eps) + { + d = d - r; + ret = min(ret, d); + } + } + } + else + { + double d = dist(o, p), r = dist(o, a); + if ((a1 * b.x + b1 * b.y - c1 > 0) == (a1 * p.x + b1 * p.y - c1 > 0)) + { + if (cross(o, a, p) * cross(o, c, p) < eps) + { + if (d > r) + d -= r; + else + d = r - d; + ret = min(ret, d); + } + } + else + { + if (cross(a, c, p) * cross(a, o, p) < eps && + cross(c, o, p) * cross(c, a, p) < eps && + cross(o, a, p) * cross(o, c, p) < eps) + { + d = r - d; + ret = min(ret, d); + } + } + } + assert(ret >= 0); + printf("Case %d: %.3lf\n", ++cases, ret + eps); + } + return 0; +} +/* + 4 -2 -2 4 -4 -2 -1 3 + 4 -2 -2 4 -4 -2 1 3 + 4 -2 -2 4 -4 -2 -3 5 + 4 -2 -2 4 -4 -2 3 5 + 4 -2 -2 4 -4 -2 -3 1 + 4 -2 -2 4 -4 -2 -5 1 + 4 -2 -2 4 -4 -2 -1 -1 + 4 -2 -2 4 -4 -2 -1 -3 + 4 -2 -2 4 -4 -2 0 0 + + -4 3 0 5 4 3 9 4 + -4 3 0 5 4 3 -9 4 + -4 3 0 5 4 3 10 3 + -4 3 0 5 4 3 -4 5 + -4 3 0 5 4 3 8 2 + -4 3 0 5 4 3 1 2 + -4 3 0 5 4 3 1 -2 + -4 3 0 5 4 3 2 1 + + 0 0 1 1 2 0 1 -1 + + 3 4 0 5 -3 4 0 1 + 3 4 0 5 -3 4 0 4.5 + 3 4 0 5 -3 4 1 3 + 3 4 0 5 -3 4 -3 3 + 3 4 0 5 -3 4 0 -1 + + -1 -1 0 3 1 -1 1 1 + -1 -1 0 3 1 -1 -2 -1 + -1 -1 0 3 1 -1 0 0 + -1 -1 0 3 1 -1 0 -1 + -1 -1 0 3 1 -1 0 -2 + + 2 0 0 2 -2 0 0 0 + + Case 1: 1.414 + Case 2: 4.000 + Case 3: 0.500 + Case 4: 1.838 + Case 5: 1.000 + Case 6: 5.831 + Case 7: 1.117 + Case 8: 0.616 + Case 9: 1.414 +*/ diff --git a/12819.cpp b/12819.cpp new file mode 100644 index 0000000..7457f19 --- /dev/null +++ b/12819.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +template +inline T sqr(T x) +{ + return x * x; +} +typedef long long LL; +typedef unsigned long long ULL; +typedef long double LD; +typedef pair PII; +typedef pair PIII; +typedef pair PLL; +typedef pair PLI; +typedef pair PDD; +#define MP make_pair +#define PB push_back +#define sz(x) ((int)(x).size()) +#define clr(ar, val) memset(ar, val, sizeof(ar)) +#define istr stringstream +#define FOR (i, n) for (int i = 0; i < (n); ++i) +#define forIt(mp, it) for (__typeof(mp.begin()) it = mp.begin(); it != mp.end(); it++) +const double PI = acos(-1.0); + +#define lson l, mid, rt << 1 +#define rson mid + 1, r, rt << 1 | 1 +#define lowbit(u) (u & (-u)) + +using namespace std; + +int cover[25]; +int dp[22][17][1 << 16][2]; +int g[17][17]; +int r, c; + +#define MOD 1000000007 + +inline int get(int x, int y) +{ + return x * c + y; +} + +void Add(int &ans, int v) +{ + ans += v; + if (ans >= MOD) + { + ans -= MOD; + } +} + +int main(void) +{ +#ifndef ONLINE_JUDGE + freopen("/Users/mac/Desktop/data.in", "r", stdin); +#endif + while (scanf("%d %d", &r, &c) != EOF) + { + for (int i = 0; i < r; i++) + for (int j = 0; j < c; j++) + { + scanf("%d", &g[i][j]); + } + for (int i = 0; i <= 20; i++) + { + cover[i] = 0; + for (int j = 0; j < r; j++) + { + for (int k = 0; k < c; k++) + { + if (g[j][k] >= i) + { + cover[i] |= (1 << get(j, k)); + } + } + } + } + memset(dp, 0, sizeof(dp)); + dp[1][0][(1 << (r * c)) - 1][0] = 1; + for (int f = 1; f <= 20; f++) + { + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + for (int st = 0; st < (1 << (r * c)); st++) + { + for (int l = 0; l < 2; l++) + { + if (dp[f][get(i, j)][st][l] == 0) + { + continue; + } + int nx = i, ny = j + 1, nf = f; + if (ny == c) + { + ny = 0, nx++; + } + if (nx == r) + { + nx = ny = 0, nf++; + } + if (((st & (1 << (get(i, j)))) == 0) && g[i][j] >= f - 1) + { + if (g[i][j] >= f) + { + Add(dp[nf][get(nx, ny)][st | (1 << get(i, j))][l], + dp[f][get(i, j)][st][l]);//必须放下去1*2 + } + continue; + } + if (st & (1 << get(i, j))) + Add(dp[nf][get(nx, ny)][st ^ (1 << get(i, j))][l], + dp[f][get(i, j)][st][l]);//ä¸Â放 + else + { + Add(dp[nf][get(nx, ny)][st][l], dp[f][get(i, j)][st][l]); + } + if (g[i][j] >= f) + Add(dp[nf][get(nx, ny)][st | (1 << get(i, j))][1], + dp[f][get(i, j)][st][l]);//1*1 + if (j) + { + if ((st & (1 << get(i, j - 1))) == 0) + { + if (g[i][j] >= f && g[i][j - 1] >= f) + Add(dp[nf][get(nx, ny)][st | (1 << get(i, j)) | (1 << get(i, j - 1))][l], + dp[f][get(i, j)][st][l]); + } + } + if (i) + { + if ((st & (1 << get(i - 1, j))) == 0) + { + if (g[i][j] >= f && g[i - 1][j] >= f) + Add(dp[nf][get(nx, ny)][st | (1 << get(i, j)) | (1 << get(i - 1, j))][l], + dp[f][get(i, j)][st][l]); + } + } + } + } + } + } + } + static int ca = 1; + printf("Case %d: %d %d\n", ca++, dp[21][get(0, 0)][cover[20]][1], dp[21][get(0, 0)][cover[20]][0]); + } + return 0; +} diff --git a/1282.cpp b/1282.cpp new file mode 100644 index 0000000..efe16f5 --- /dev/null +++ b/1282.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +#define MAXN 100500 +#define pi pair +#define y first +#define x second + +int N, _n, cs, tr, u; +int chk[MAXN][128], pre[MAXN], suf[MAXN]; +long long A[128], B[128], F[128]; +string s; + +int bS(long long n, int lo = 1, int hi = 100)// first i s.t. F[i] >= n +{ + if (lo == hi) + { + if (F[lo] >= n) + { + return lo; + } + else + { + return lo + 1; + } + } + if (lo + 1 == hi) + { + if (F[lo] >= n) + { + return lo; + } + if (F[hi] >= n) + { + return hi; + } + return hi + 1; + } + int mid = (hi + lo) / 2; + if (F[mid] >= n) + { + return bS(n, lo, mid); + } + return bS(n, mid + 1, hi); +} + +int check(int i, int n) +{ + if (chk[i][n] != -1) + { + return chk[i][n]; + } + if (F[n] > N - i) + { + return chk[i][n] = 0; + } + if (n == 0) + { + return chk[i][0] = (s[i] == '0'); + } + if (n == 1) + { + return chk[i][1] = (s[i] == '1'); + } + chk[i][n] = 0; + if (check(i, n - 1)) + { + if (check(i + F[n - 1], n - 2)) + { + chk[i][n] = 1; + } + } + return chk[i][n]; +} + +int Pre(int i, int n) +{ + if (n == 0) + { + return (i == N - 1 && s[i] == '0'); + } + return pre[i] <= n; +} + +int Suf(int i, int n) +{ + if (n == 0) + { + return (i == 0 && s[i] == '0'); + } + return suf[i] <= n && (suf[i] % 2 == n % 2); +} + +long long find(int n) +{ + if (A[n] != -1) + { + return A[n]; + } + if (n == 0) + { + A[0] = (s == "0"); + return A[0]; + } + if (n == 1) + { + A[1] = (s == "1"); + return A[1]; + } + A[n] = find(n - 1) + find(n - 2); + B[n] = 0; + if (n >= 4 && N <= F[n - 4]) + { + B[n] = B[n - 2]; + } + else + for (int i = 0; i < N - 1; i += 1) + { + B[n] += (long long)(Suf(i, n - 1) & Pre(i + 1, n - 2)); + } + A[n] += B[n]; + return A[n]; +} + +int main() +{ + F[0] = 1; + F[1] = 1; + for (int n = 2; n < 128; n += 1) + { + F[n] = min(F[n - 1] + F[n - 2], 1000000000LL); + } + while (cin >> _n >> s) + { + N = s.size(); + for (tr = 0; F[tr] < (long long)N; tr += 1) + ; + tr += tr & 1; + tr = 10; + memset(chk, -1, sizeof(chk)); + suf[0] = s[0] - '0'; + for (int i = 1; i < N; i += 1) + { + u = bS(i + 1); + if (u < 2) + { + suf[i] = (s[0] == '1') ? u : 1000; + } + else + { + suf[i] = (Suf(i - F[u - 2], u - 1) && check(i - F[u - 2] + 1, u - 2)) ? u : 1000; + } + u++; + if (u < 2) + { + suf[i] = min(suf[i], (s[0] == char(u + '0')) ? u : 1000); + } + else + { + suf[i] = min(suf[i], (Suf(i - F[u - 2], u - 1) && check(i - F[u - 2] + 1, u - 2)) ? u : 1000); + } + } + for (int i = N - 1; i >= 0; i -= 1) + { + u = bS(N - i); + if (u < 2) + { + pre[i] = (s[N - 1] == char(u + '0')) ? u : 1000; + } + else + { + pre[i] = (Pre(i + F[u - 1], u - 2) && check(i, u - 1)) ? u : 1000; + } + } + for (int i = 0; i <= _n; i += 1) + { + A[i] = -1; + } + cout << "Case " << ++cs << ": " << find(_n) << '\n'; + } + return 0; +} diff --git a/12820.cpp b/12820.cpp new file mode 100644 index 0000000..a9a1eb7 --- /dev/null +++ b/12820.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int n, cases = 0; + char s[32]; + while (scanf("%d", &n) == 1) + { + int ret = 0; + for (int i = 0; i < n; i++) + { + scanf("%s", s); + int cnt[128] = {}, mcnt[32] = {}; + int diff = 0, same = 0; + for (int j = 0; s[j]; j++) + cnt[s[j]]++; + for (int j = 'a'; j <= 'z'; j++) + { + if (cnt[j]) + { + if (mcnt[cnt[j]] == 0) + diff++; + if (mcnt[cnt[j]] > 0) + same = 1; + mcnt[cnt[j]]++; + } + } + if (diff > 1 && same == 0) + ret++; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} \ No newline at end of file diff --git a/12821.cpp b/12821.cpp new file mode 100644 index 0000000..c165fc2 --- /dev/null +++ b/12821.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +struct Node +{ + // x->y, v + int x, y, cap, cost, nxt; +} edge[100005]; +int e, head[505], dis[505], prev_[505], record[505], inq[505]; + +void add_edge(int x, int y, int cap, int cost) +{ + edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost; + edge[e].nxt = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost; + edge[e].nxt = head[y], head[y] = e++; +} + +int min_cost(int s, int t) +{ + int mncost = 0, flow, totflow = 0; + int i, x, y; + while (1) + { + memset(dis, 63, sizeof(dis)); + int oo = dis[0]; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(), Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].nxt) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + prev_[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + { + Q.push_front(y); + } + else + { + Q.push_back(y); + } + } + } + } + } + if (dis[t] == oo) + { + break; + } + flow = oo; + for (x = t; x != s; x = prev_[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = prev_[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return mncost; +} + +int main() +{ + int n, m, t = 0; + int x, y, d, a; + while (scanf("%d %d", &n, &m) == 2) + { + e = 0; + memset(head, -1, sizeof(head)); + int source = n + 1, sink = n + 2; + for (int i = 0; i < m; i++) + { + scanf("%d %d %d %d", &x, &y, &d, &a); + add_edge(x, y, 1, d); + add_edge(x, y, 1, d + a); + } + add_edge(source, 1, 2, 0); + add_edge(n, sink, 2, 0); + printf("Case %d: %d\n", ++t, min_cost(source, sink)); + } + return 0; +} diff --git a/12822.cpp b/12822.cpp new file mode 100644 index 0000000..126c51a --- /dev/null +++ b/12822.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int w[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; +int calcLED(int n) +{ + char buf[32]; + sprintf(buf, "%d", n); + int ret = 0; + for (int i = 0; buf[i]; i++) + ret += w[buf[i] - '0']; + return ret; +} +int main() +{ + int cases = 0; + int hh, mm, ss, x; + char cmd[32]; + while (scanf("%*s") == 0) + { + scanf("%d:%d:%d", &hh, &mm, &ss); + int pre = hh * 3600 + mm * 60 + ss, now; + int home = 0, guest = 0, ret = 0; + while (scanf("%s", cmd) == 1) + { + scanf("%d:%d:%d", &hh, &mm, &ss); + now = hh * 3600 + mm * 60 + ss; + ret += (now - pre) * (calcLED(home) + calcLED(guest)); + if (cmd[0] == 'E') + break; + scanf("%s %d", cmd, &x); + if (cmd[0] == 'h') + home += x; + else + guest += x; + pre = now; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} +/* + START 09:00:00 + SCORE 09:01:05 home 2 + SCORE 09:10:07 guest 3 + END 09:15:00 + START 09:00:00 + SCORE 10:00:00 home 1 + SCORE 11:00:00 home 1 + SCORE 12:00:00 home 1 + SCORE 13:00:00 home 1 + SCORE 14:00:00 home 1 + SCORE 15:00:00 home 1 + SCORE 16:00:00 home 1 + SCORE 17:00:00 home 1 + SCORE 18:00:00 home 1 + SCORE 19:00:00 home 1 + SCORE 20:00:00 home 1 + END 21:00:00 +*/ \ No newline at end of file diff --git a/12825.cpp b/12825.cpp new file mode 100644 index 0000000..ecdaebf --- /dev/null +++ b/12825.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +int main() +{ + char s[1024]; + int cases = 0; + while (scanf("%s", s) == 1) + { + int dpx[1024][4][2], dpy[1024][4][2]; + // [N E S W][min/max] + const int dx[4] = {0, 1, 0, -1}; + const int dy[4] = {1, 0, -1, 0}; + int n = strlen(s); + for (int i = 0; i <= n; i++) + for (int j = 0; j < 4; j++) + { + dpx[i][j][0] = INF, dpx[i][j][1] = -INF; + dpy[i][j][0] = INF, dpy[i][j][1] = -INF; + } + dpx[0][1][0] = 0, dpx[0][1][1] = 0; + dpy[0][1][0] = 0, dpy[0][1][1] = 0; + for (int i = 0; i < n; i++) + { + if (s[i] == 'F' || s[i] == '?') + { + for (int j = 0; j < 4; j++) + { + dpx[i + 1][j][0] = min(dpx[i + 1][j][0], dpx[i][j][0] + dx[j]); + dpx[i + 1][j][1] = max(dpx[i + 1][j][1], dpx[i][j][1] + dx[j]); + dpy[i + 1][j][0] = min(dpy[i + 1][j][0], dpy[i][j][0] + dy[j]); + dpy[i + 1][j][1] = max(dpy[i + 1][j][1], dpy[i][j][1] + dy[j]); + } + } + if (s[i] == 'R' || s[i] == '?') + { + for (int j = 0; j < 4; j++) + { + dpx[i + 1][(j + 1) % 4][0] = min(dpx[i + 1][(j + 1) % 4][0], dpx[i][j][0]); + dpx[i + 1][(j + 1) % 4][1] = max(dpx[i + 1][(j + 1) % 4][1], dpx[i][j][1]); + dpy[i + 1][(j + 1) % 4][0] = min(dpy[i + 1][(j + 1) % 4][0], dpy[i][j][0]); + dpy[i + 1][(j + 1) % 4][1] = max(dpy[i + 1][(j + 1) % 4][1], dpy[i][j][1]); + } + } + if (s[i] == 'L' || s[i] == '?') + { + for (int j = 0; j < 4; j++) + { + dpx[i + 1][(j + 3) % 4][0] = min(dpx[i + 1][(j + 3) % 4][0], dpx[i][j][0]); + dpx[i + 1][(j + 3) % 4][1] = max(dpx[i + 1][(j + 3) % 4][1], dpx[i][j][1]); + dpy[i + 1][(j + 3) % 4][0] = min(dpy[i + 1][(j + 3) % 4][0], dpy[i][j][0]); + dpy[i + 1][(j + 3) % 4][1] = max(dpy[i + 1][(j + 3) % 4][1], dpy[i][j][1]); + } + } + // for (int j = 0; j < 4; j++) { + // printf("%d %d %d %d\n", dpx[i][j][0], dpx[i][j][1], dpy[i][j][0], dpy[i][j][1]); + // } + // puts("--"); + } + int mnx = INF, mxx = -INF, mny = INF, mxy = -INF; + for (int i = 0; i < 4; i++) + { + mnx = min(mnx, dpx[n][i][0]); + mxx = max(mxx, dpx[n][i][1]); + mny = min(mny, dpy[n][i][0]); + mxy = max(mxy, dpy[n][i][1]); + } + printf("Case %d: %d %d %d %d\n", ++cases, mnx, mxx, mny, mxy); + } + return 0; +} diff --git a/12826.cpp b/12826.cpp new file mode 100644 index 0000000..93c9d99 --- /dev/null +++ b/12826.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}; +const int dy[] = {1, -1, 0, 1, -1, 0, 1, -1}; +int main() +{ + int sx, sy, ex, ey, px, py, tx, ty; + int cases = 0; + while (scanf("%d %d", &sx, &sy) == 2) + { + scanf("%d %d", &ex, &ey); + scanf("%d %d", &px, &py); + int used[16][16] = {}, dist[16][16] = {}; + used[px][py] = 1; + queue X, Y; + X.push(sx), Y.push(sy), used[sx][sy] = 1; + while (!X.empty()) + { + sx = X.front(), X.pop(); + sy = Y.front(), Y.pop(); + if (sx == ex && sy == ey) + break; + for (int i = 0; i < 8; i++) + { + tx = sx + dx[i], ty = sy + dy[i]; + if (tx <= 0 || tx > 8 || ty <= 0 || ty > 8) + continue; + if (used[tx][ty] == 0) + { + used[tx][ty] = 1; + dist[tx][ty] = dist[sx][sy] + 1; + X.push(tx), Y.push(ty); + } + } + } + printf("Case %d: %d\n", ++cases, dist[ex][ey]); + } + return 0; +} diff --git a/12828.cpp b/12828.cpp new file mode 100644 index 0000000..75ffdad --- /dev/null +++ b/12828.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int main() +{ + double A[5], B[5]; + char s[5]; + int cases = 0, ga, gb; + while (scanf("%lf", &A[0]) == 1) + { + for (int i = 1; i < 5; i++) + scanf("%lf", &A[i]); + for (int i = 0; i < 5; i++) + scanf("%lf", &B[i]); + scanf("%s", s); + ga = s[0] - '0', gb = s[2] - '0'; + double dp[16][16][16] = {}; + double result[16][16] = {}; + dp[0][0][0] = 1; + int a = 0, b = 0; + for (int i = 0; i <= 10; i++) + { + for (int p = 0; p <= 5; p++) + { + for (int q = 0; q <= 5; q++) + { + if ((p > q && q + (5 - b) < p) || i == 10) + { + result[p][q] += dp[i][p][q]; + dp[i][p][q] = 0; + } + if ((q > p && p + (5 - a) < q) || i == 10) + { + result[p][q] += dp[i][p][q]; + dp[i][p][q] = 0; + } + } + } + if (i & 1) + { + if (b < 5) + for (int p = 0; p <= 5; p++) + { + for (int q = 0; q <= 5; q++) + { + dp[i + 1][p][q] += dp[i][p][q] * (1 - B[b]); + dp[i + 1][p][q + 1] += dp[i][p][q] * B[b]; + } + } + b++; + } + else + { + if (a < 5) + for (int p = 0; p <= 5; p++) + { + for (int q = 0; q <= 5; q++) + { + dp[i + 1][p][q] += dp[i][p][q] * (1 - A[a]); + dp[i + 1][p + 1][q] += dp[i][p][q] * A[a]; + } + } + a++; + } + } + printf("Case %d: %.2lf%%\n", ++cases, result[ga][gb] * 100.0); + } + return 0; +} +/* +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +1-3 +1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 +2-0 +1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 +2-0 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +2-3 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +4-2 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +1-3 +1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 +2-0 +1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 +0-0 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +2-3 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +4-1 +0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 +0-0 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +0-0 +0.4 0.7 0.7 0.6 0.5 0.8 0.9 0.7 0.2 0.8 +5-5 +1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 +5-5 +*/ diff --git a/1283.cpp b/1283.cpp new file mode 100644 index 0000000..2a61cbb --- /dev/null +++ b/1283.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +#define MAXN 100500 +#define pi pair +#define y first +#define x second + +typedef vector VI; +typedef vector VVI; + +int N, cs, cnt; +char ch; +VVI x; +int sid = 200000; +VI a, e; +int l, st; + +void find(int n = 1) +{ + a = VI(n); + for (int s = 0; s < sid; s += 1) + { + e = VI(N + 1, 0); + st = 0; + for (int i = 0; i < n; i += 1) + { + a[i] = st + 1 + rand() % (N - st); + st = a[i]; + e[st] = 1; + if (st == N) + { + l = 1; + for (int j = i + 1; j < n; j += 1) + { + for (; l <= N; l += 1) + { + if (!e[l]) + { + a[j] = l; + l++; + break; + } + } + } + break; + } + } + cnt = 0; + for (int k = 1; k <= N; k += 1) + { + l = 0; + for (int j = 0; j < n; j += 1) + { + l |= x[a[j]][k]; + } + cnt += l; + } + if (cnt == N) + { + cout << n; + for (int i = 0; i < n; i += 1) + { + cout << ' ' << a[i]; + } + cout << '\n'; + return; + } + } + find(n + 1); +} + +int main() +{ + srand(time(NULL)); + while (cin >> N) + { + x = VVI(N + 1, VI(N + 1)); + for (int i = 1; i <= N; i += 1) + for (int j = 1; j <= N; j += 1) + { + cin >> ch; + x[i][j] = ch - '0'; + } + for (int i = 1; i <= N; i += 1) + { + x[i][i] = 1; + } + cout << "Case " << ++cs << ": "; + find(); + } + return 0; +} diff --git a/12830.cpp b/12830.cpp new file mode 100644 index 0000000..753a7c0 --- /dev/null +++ b/12830.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +// same 10043 - Chainsaw Massacre. +struct Pt +{ + int x, y; + Pt(int a = 0, int b = 0) : x(a), y(b) {} + bool operator<(const Pt &p) const + { + if (p.x != x) + return x < p.x; + return y < p.y; + } +}; +bool cmp(Pt a, Pt b) +{ + if (a.y != b.y) + return a.y < b.y; + return a.x < b.x; +} +Pt tree[3000]; +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int h, w; + int x, y; + scanf("%d %d", &h, &w); + int op, i, j; + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d %d", &tree[i].x, &tree[i].y); + tree[n++] = Pt(0, 0); + tree[n++] = Pt(h, w); + tree[n++] = Pt(h, 0); + tree[n++] = Pt(0, w); + sort(tree, tree + n); + int area = 0; + for (i = 0; i < n; i++) + { + int mny = 0, mxy = w; + for (j = i + 1; j < n; j++) + { + area = max(area, (tree[j].x - tree[i].x) * (mxy - mny)); + if (tree[j].x == tree[i].x) + continue; + if (tree[j].y > tree[i].y) + mxy = min(mxy, tree[j].y); + else + mny = max(mny, tree[j].y); + } + } + sort(tree, tree + n, cmp); + for (i = 0; i < n; i++) + { + int mnx = 0, mxx = h; + for (j = i + 1; j < n; j++) + { + area = max(area, (tree[j].y - tree[i].y) * (mxx - mnx)); + if (tree[j].y == tree[i].y) + continue; + if (tree[j].x > tree[i].x) + mxx = min(mxx, tree[j].x); + else + mnx = max(mnx, tree[j].x); + } + } + printf("Case %d: %d\n", ++cases, area); + } + return 0; +} diff --git a/12831.cpp b/12831.cpp new file mode 100644 index 0000000..2a8cc62 --- /dev/null +++ b/12831.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +const int MAXV = 20010; +const int MAXE = MAXV * 300 * 2; +const int INF = 1 << 29; +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + assert(x < MAXV && y < MAXV); + assert(e < MAXE); + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; + +int visited[32767], N, L; +vector D; +void dfs(int u) +{ + if (visited[u]) + return; + visited[u] = 1, D.push_back(u); + for (int i = 0; (1 << i) <= u; i++) + { + if ((u >> i) & 1) + { + int v = u + (1 << i); + if (v <= L) + { + dfs(v); + g.Addedge(u, v + L, 1); + } + } + } +} +int main() +{ + int testcase, cases = 0; + int x, y, u, v; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &L); + assert(N > 0 && N <= 36); + memset(visited, 0, sizeof(visited)); + D.clear(); + int A[10000 + 5]; + int source = 0, sink = 2 * L + 1; + g.Init(2 * L + 5); + for (int i = 0; i < N; i++) + { + scanf("%d", &A[i]); + dfs(A[i]); + } + for (int i = 0; i < D.size(); i++) + { + g.Addedge(source, D[i], 1); + g.Addedge(D[i] + L, sink, 1); + } + int ret = D.size() - g.Maxflow(source, sink); + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} +/* +99999 +1 36 +20 +2 40 +8 20 +1 2 +2 +1 10 +6 +*/ diff --git a/12832.cpp b/12832.cpp new file mode 100644 index 0000000..df75765 --- /dev/null +++ b/12832.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +long long gcd(long long x, long long y) +{ + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; +} +long long lcm(long long x, long long y) +{ + return x / gcd(x, y) * y; +} +void simplify(long long &x, long long &y) +{ + long long g = gcd(x, y); + x /= g, y /= g; +} +int main() +{ + int testcase, cases = 0, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + long long a = 0, b = 1, x, y; + for (int i = 0; i < n; i++) + { + scanf("%lld %lld", &y, &x); + simplify(x, y); + long long ta, tb = lcm(b, y); + ta = a * (tb / b) + x * (tb / y); + a = ta, b = tb; + simplify(a, b); + } + printf("Case %d: %lld/%lld\n", ++cases, a, b); + } + return 0; +} diff --git a/12833.cpp b/12833.cpp new file mode 100644 index 0000000..7361ba4 --- /dev/null +++ b/12833.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define M 200100 + +char s1[M], s2[M]; +int p[M], sum[M][26], all[26][2][M]; + +void add(int *sum, int i) +{ + for (; i > 0; i -= i & -i) + { + sum[i]++; + } +} + +int query(int *sum, int i, int len) +{ + int ans = 0; + if (i > len || i <= 0) + { + return 0; + } + return sum[i]; +} + +int f(int len) +{ + p[1] = 2, p[0] = 1; + int id = 1, mx = 2; + for (int i = 2; i < len; ++i) + { + int k = min(mx - i, p[id - (i - id)]); + while (i - k >= 0 && i + k < len && s2[i + k] == s2[i - k]) + { + ++k; + } + p[i] = k; + if (i + k >= mx) + { + mx = i + k, id = i; + } + } + for (int i = 1; i < len; ++i) + { + memcpy(sum[i], sum[i - 1], sizeof(sum[i - 1])); + if (s2[i] >= 0 && s2[i] < 26) + { + sum[i][s2[i]]++; + } + } + for (int i = 0; i < 26; ++i) + { + for (int j = 0; j <= len; ++j) + { + all[i][0][j] = all[i][1][j] = 0; + } + } + for (int i = 1; i < len; ++i) + { + for (int j = 0; j < 26; ++j) + { + int s = sum[i + p[i] - 1][j] - sum[i - 1][j]; + int o = 0; + if (s2[i] == j) + { + o = 1; + } + all[j][o][s]++; + } + } + return 0; +} + +int solve(int c, int x, int len) +{ + int ans; + if (x > len || x <= 0) + { + return 0; + } + if (x & 1) + { + ans = query(all[c][1], x / 2 + 1, len); + } + else + { + ans = query(all[c][0], x / 2, len); + } + return ans; +} + +int main() +{ + int T; + scanf("%d", &T); + int j, i, len, cas = 1, x; + while (T--) + { + scanf("%d", &len); + scanf("%s", s1); + for (j = i = 0; i < len; ++i) + { + s2[j++] = 30, s2[j++] = s1[i] - 'a'; + } + s2[j++] = 30; + f(j); + for (int i = len; i >= 0; --i) + { + for (int j = 0; j < 26; ++j) + { + for (int k = 0; k < 2; ++k) + { + all[j][k][i] += all[j][k][i + 1]; + } + } + } + int m; + scanf("%d", &m); + scanf("%s", s1); + printf("Case %d:\n", cas++); + for (int j = 0; j < m; ++j) + { + scanf("%d", &x); + x = solve(s1[j] - 'a', x, len); + printf("%d\n", x); + } + } +} diff --git a/12834.cpp b/12834.cpp new file mode 100644 index 0000000..eeaa9da --- /dev/null +++ b/12834.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +long long A[131072]; +int main() +{ + int testcase, cases = 0; + int n, m; + long long x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + scanf("%lld", &A[i]), A[i] = -A[i]; + for (int i = 0; i < n; i++) + scanf("%lld", &x), A[i] += x; + sort(A, A + n, greater()); + long long p = 0; + for (int i = 0, j = n - 1; i < m; i++, j--) + if (A[j] < 0) + n--; + for (int i = 0; i < n; i++) + p += A[i]; + printf("Case %d: ", ++cases); + if (p <= 0) + puts("No Profit"); + else + printf("%lld\n", p); + } + return 0; +} diff --git a/12836.cpp b/12836.cpp new file mode 100644 index 0000000..3e77f4e --- /dev/null +++ b/12836.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define SZ(x) ((int)x.size()) +#define all(a) a.begin(), a.end() +#define allr(a) a.rbegin(), a.rend() +#define clrall(name, val) memset(name, (val), sizeof(name)); +#define EPS 10e-9 +#define ll long long +#define ull long long unsigned +#define SF scanf +#define PF printf +#define psb(b) push_back((b)) +#define ppb() pop_back() +#define mp make_pair +#define fs first +#define sc second +#define rep(var, s, n, it) for (var = (s); var < (n); (var) += it) +#define rev(var, s, n, it) for (var = (n - 1); var > (s - 1); (var) -= it) +#define Read freopen("in.txt", "r", stdin) +#define Write freopen("out12836.txt", "w", stdout) +#define __ std::ios_base::sync_with_stdio(false) + +#define casePrint cout << "Case " << (++cas) << ": "; + +const int inf = (1 << 30) - 1; + +const int MAX = 1005; +int dp[MAX][MAX]; +int mid[MAX][MAX]; + +int val[MAX]; +int csum[MAX]; +int LS[MAX]; +int RS[MAX]; + +int knuthTrick(int n) +{ + int s, L, R, midLeft, midRight, cost, cur, M; + for (s = 0; s < n; s++) + { + for (L = 0; L + s < n; L++) + { + R = L + s; + if (s < 1) + { + dp[L][R] = 0; + mid[L][R] = L; + continue; + } + midLeft = mid[L][R - 1]; + midRight = mid[L + 1][R]; + cost = csum[R] - (L == 0 ? 0 : csum[L - 1]); + cur; + dp[L][R] = inf; + for (M = midLeft; M <= midRight && M + 1 <= R; M++) + { + if (dp[L][M] == inf || dp[M + 1][R] == inf) + { + continue; + } + cur = dp[L][M] + dp[M + 1][R] + cost; + if (dp[L][R] > cur) + { + dp[L][R] = cur; + mid[L][R] = M; + } + } + } + } + return dp[0][n - 1]; +} + +void process(int n) +{ + int i, j; + for (i = 0; i < n; i++) + { + LS[i] = 0; + for (j = 0; j < i; j++) + { + if (val[i] > val[j]) + { + if (LS[i] < LS[j]) + { + LS[i] = LS[j]; + } + } + } + LS[i]++; + } + for (i = n - 1; i > -1; i--) + { + RS[i] = 0; + for (j = n - 1; j > i; j--) + { + if (val[i] > val[j]) + { + if (RS[i] < RS[j]) + { + RS[i] = RS[j]; + } + } + } + RS[i]++; + } + for (i = 0; i < n; i++) + { + csum[i] = LS[i] + RS[i] - 1 + (i > 0 ? csum[i - 1] : 0); + } + return; +} + +int main() +{ + __; + int test, cas = 0, n, i; + cin >> test; + while (test--) + { + cin >> n; + for (i = 0; i < n; i++) + { + cin >> val[i]; + } + process(n); + cout << "Case " << (++cas) << ": " << knuthTrick(n) << "\n"; + } + return 0; +} diff --git a/12837.cpp b/12837.cpp new file mode 100644 index 0000000..4f46398 --- /dev/null +++ b/12837.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +struct TrieNode +{ + int n; + int link[27]; +} Node[1048576 << 2]; +int TrieSize; +int rename(char c) +{ + if ('a' <= c && c <= 'z') + return c - 'a'; + return 26; +} +void insertTrie(const char *str, int root) +{ + static int i, idx, c; + for (i = 0, idx = root; str[i]; i++) + { + c = rename(str[i]); + if (Node[idx].link[c] == 0) + { + TrieSize++; + memset(&Node[TrieSize], 0, sizeof(Node[0])); + Node[idx].link[c] = TrieSize; + } + idx = Node[idx].link[c]; + } +} +TrieNode *getTrieNode(const char *str, int root) +{ + static int i, idx, c; + for (i = 0, idx = root; str[i]; i++) + { + c = rename(str[i]); + if (Node[idx].link[c] == 0) + { + TrieSize++; + memset(&Node[TrieSize], 0, sizeof(Node[0])); + Node[idx].link[c] = TrieSize; + } + idx = Node[idx].link[c]; + } + return &Node[idx]; +} +const int MAXQL = 10 - 1; +const int MAXQ = 50000; +char ms[MAXQ][32]; +int main() +{ + int testcase, q, cases = 0; + char s1[32], s2[32], s[1024]; + int A[1024], B[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + scanf("%d", &q); + int sn = strlen(s), s1n, s2n, an, bn; + TrieSize = 2; + int root1 = 0, root2 = 1; + memset(&Node[root1], 0, sizeof(Node[root1])); + memset(&Node[root2], 0, sizeof(Node[root2])); + for (int i = 0; i < q; i++) + { + scanf("%s %s", s1, s2); + s1n = strlen(s1), s2n = strlen(s2); + int m = 0; + for (int j = 0; j < s1n; j++) + ms[i][m++] = s1[j]; + ms[i][m++] = '#'; + for (int j = s2n - 1; j >= 0; j--) + ms[i][m++] = s2[j]; + ms[i][m] = '\0'; + insertTrie(ms[i], root2); + } + + for (int i = 0; i < sn; i++) + { + int idx = root1, idx2, c; + for (int j = i; j < sn; j++) + {// add s[l, r] + c = rename(s[j]); + if (Node[idx].link[c] == 0) + { + TrieSize++; + memset(&Node[TrieSize], 0, sizeof(Node[0])); + Node[idx].link[c] = TrieSize; + // create new node == create distinct substring + int idx2 = root2; + int L = min(j, i + MAXQL); + for (int k = i; k <= L; k++) + {// brute head + if (Node[idx2].link[rename(s[k])] == 0) + break; + idx2 = Node[idx2].link[rename(s[k])]; + if (Node[idx2].link[rename('#')]) + { + int idx3 = Node[idx2].link[rename('#')]; + int R = max(i, j - MAXQL); + for (int l = j; l >= R; l--) + {// brute tail + if (Node[idx3].link[rename(s[l])] == 0) + break; + idx3 = Node[idx3].link[rename(s[l])]; + Node[idx3].n++;// [l, r] = strA + ... + strB + } + } + } + } + idx = Node[idx].link[c]; + } + } + printf("Case %d:\n", ++cases); + for (int i = 0; i < q; i++) + { + TrieNode *p = getTrieNode(ms[i], root2); + printf("%d\n", p->n); + } + } + return 0; +} +/* +1 +abab +3 +a a +a b +ba ab +*/ diff --git a/12839.cpp b/12839.cpp new file mode 100644 index 0000000..523ed15 --- /dev/null +++ b/12839.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int A[131072]; +int main() +{ + int testcase, cases = 0, n, m, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + sort(A, A + n, greater()); + priority_queue, vector>, + greater>> + pQ; + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + pQ.push(make_pair(0, x)); + } + long long ret = 0; + for (int i = 0; i < n; i++) + { + ret = max(ret, A[i] + pQ.top().first); + pair v = pQ.top(); + pQ.pop(), pQ.push(make_pair(v.first + v.second, v.second)); + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/12840.cpp b/12840.cpp new file mode 100644 index 0000000..150f151 --- /dev/null +++ b/12840.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0, n, m, A[64]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 1; i <= n; i++) + scanf("%d", &A[i]); + int dp[55][305]; + pair from[55][305]; + memset(dp, 0x3f, sizeof(dp)); + dp[0][0] = 0, from[0][0] = make_pair(-1, -1); + for (int i = 1; i <= n; i++) + { + for (int j = 0; j <= m; j++) + dp[i][j] = dp[i - 1][j], from[i][j] = from[i - 1][j]; + for (int j = A[i]; j <= m; j++) + { + dp[i][j] = min(dp[i][j], min(dp[i][j - A[i]] + 1, dp[i - 1][j - A[i]] + 1)); + if (dp[i][j] == dp[i - 1][j - A[i]] + 1) + from[i][j] = make_pair(i - 1, j - A[i]); + if (dp[i][j] == dp[i][j - A[i]] + 1) + from[i][j] = make_pair(i, j - A[i]); + } + } + printf("Case %d: ", ++cases); + if (dp[n][m] == 0x3f3f3f3f) + puts("impossible"); + else + { + printf("[%d]", dp[n][m]); + int s = m; + pair p = from[n][m], q; + while (p.first != -1) + { + printf(" %d", s - p.second); + s = p.second; + p = from[p.first][p.second]; + } + puts(""); + } + } + return 0; +} +/* +3 +6 100 +16 17 23 24 39 40 +3 50 +10 15 20 +2 25 +7 13 +*/ diff --git a/12841.cpp b/12841.cpp new file mode 100644 index 0000000..04f9e61 --- /dev/null +++ b/12841.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int a[20], vis[20], nxt[20], n, jie; +map mp; +vector G[20]; + +bool dfs(int x, int dep) +{ + if (dep == n && jie == x) + { + return true; + } + int sz = G[x].size(); + for (int i = 0; i < sz; i++) + { + if (!vis[G[x][i]]) + { + if (dep != n - 1 && G[x][i] == jie) + { + continue; + } + if (dep == n - 1 && G[x][i] != jie) + { + continue; + } + vis[G[x][i]] = 1; + nxt[x] = G[x][i]; + if (dfs(G[x][i], dep + 1)) + { + return true; + } + vis[G[x][i]] = 0; + } + } + return false; +} + +int main() +{ + int t, m, cas = 1; + scanf("%d", &t); + while (t--) + { + printf("Case %d: ", cas++); + mp.clear(); + scanf("%d%d", &n, &m); + char st, en; + for (int i = 0; i < n; i++) + { + char temp; + getchar(); + scanf("%c", &temp); + if (i == 0) + { + st = temp; + } + if (i == n - 1) + { + en = temp; + } + a[i] = temp - 'A'; + } + sort(a, a + n); + for (int i = 0; i < n; i++) + { + mp[a[i] + 'A'] = i; + G[i].clear(); + } + for (int i = 0; i < m; i++) + { + getchar(); + char t1, t2; + scanf("%c %c", &t1, &t2); + G[mp[t1]].push_back(mp[t2]); + G[mp[t2]].push_back(mp[t1]); + } + for (int i = 0; i < n; i++) + { + sort(G[i].begin(), G[i].end()); + } + int flag = 0, i, kai; + kai = mp[st]; + jie = mp[en]; + memset(vis, 0, sizeof(vis)); + memset(nxt, -1, sizeof(nxt)); + vis[kai] = 1; + if (dfs(kai, 1)) + { + int j = 0, i = kai; + while (j < n) + { + printf("%c", a[i] + 'A'); + i = nxt[i]; + j++; + } + printf("\n"); + } + else + { + printf("impossible\n"); + } + } + return 0; +} diff --git a/12842.cpp b/12842.cpp new file mode 100644 index 0000000..15d59a9 --- /dev/null +++ b/12842.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + double L; + double u = 1, v = (-2 + sqrt(8)) / 2.0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lf", &L); + double time = L / v; + printf("Case %d: %.2lf\n", ++cases, time * u); + } + return 0; +} +/* +army v, courier u +time: +L / v = L / (u - v) + L / (u + v) => u^2 - v^2 = 2v +result L / v * u, let u = 1 => v = (-2 + sqrt(8))/ 2.0. +*/ diff --git a/12843.cpp b/12843.cpp new file mode 100644 index 0000000..71f8da2 --- /dev/null +++ b/12843.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +struct E +{ + long long a, b, c; + E(long long x = 0, long long y = 0, long long z = 0) : a(x), b(y), c(z) {} +}; +int main() +{ + int testcase, cases = 0; + long long n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld", &n); + n *= 2; + long long a, b, c, c2; + vector ret; + for (long long i = 1; i * i <= n; i++) + { + if (n % i == 0) + { + a = i, b = n / i; + c2 = a * a + b * b, c = sqrt(c2); + if (c * c == c2) + ret.push_back(E(a, b, c)); + } + } + printf("Case %d: %d\n", ++cases, (int)ret.size()); + for (int i = 0; i < ret.size(); i++) + printf("(%lld,%lld,%lld)\n", ret[i].a, ret[i].b, ret[i].c); + } + return 0; +} +/* +4 +210 +1000 +2400 +3360 +*/ diff --git a/12844.cpp b/12844.cpp new file mode 100644 index 0000000..e5f0be8 --- /dev/null +++ b/12844.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +// same 10202 - Pairsumonious Numbers +int main() +{ + int n, m, A[50], sum[50]; + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + n = 5; + m = n * (n - 1) / 2; + + for (int i = 0; i < m; i++) + scanf("%d", &sum[i]); + sort(sum, sum + m); + + printf("Case %d:", ++cases); + + multiset oRB; + for (int i = 2; i < m; i++) + oRB.insert(sum[i]); + int idx, flag = 0; + for (int i = 2; i < m; i++) + {// A[1]+A[2] = sum[i] + if ((sum[0] + sum[1] + sum[i]) % 2) + continue; + int tmp = (sum[0] + sum[1] + sum[i]) / 2; + A[0] = tmp - sum[i]; + A[1] = tmp - sum[1]; + A[2] = tmp - sum[0]; + multiset RB;// copy + multiset::iterator it; + RB = oRB; + it = RB.find(sum[i]); + RB.erase(it); + int pass = 1; + for (int j = 3; j < n; j++) + { + it = RB.begin();// get min + A[j] = (*it) - A[0]; + RB.erase(it); + int ok = 1; + for (int k = 1; k < j; k++) + {// delete A[j]+A[0-(j-1)] + int tmp = A[j] + A[k]; + it = RB.find(tmp); + if (it == RB.end()) + { + ok = 0; + break; + } + RB.erase(it); + } + if (!ok) + { + pass = 0; + break; + } + } + if (pass) + {// output ans + flag = 1; + for (int j = 0; j < n; j++) + printf(" %d", A[j]); + puts(""); + break; + } + } + if (!flag) + puts("Impossible"); + } + return 0; +} +/* +3 +114 116 118 120 121 122 123 124 125 129 +110 111 114 115 118 118 119 122 123 126 +180 190 190 196 196 206 216 216 226 232 +*/ diff --git a/12846.cpp b/12846.cpp new file mode 100644 index 0000000..c39e83e --- /dev/null +++ b/12846.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +int SG[1024]; +void buildSG() +{ + int mex[1024] = {}; + SG[0] = 0; + for (int i = 1; i < 50; i++) + { + memset(mex, 0, sizeof(mex)); + for (int j = 0; j < i; j++) + { + mex[SG[j] ^ SG[i - j - 1]] = 1; + if (i - j - 2 >= 0) + mex[SG[j] ^ SG[i - j - 2]] = 1; + } + int sg = 0; + for (sg; mex[sg]; sg++) + ; + SG[i] = sg; + } +} +int main() +{ + buildSG(); + int testcase, cases = 0, n, m, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + int used[64] = {}, last = 0; + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + x--; + used[last = x] = 1; + } + + vector nim; + for (int i = 0, tmp = 0, pos; i < n; i++) + { + pos = (last + i) % n; + if (used[pos] == 0) + tmp++; + if (used[pos] || i == n - 1) + { + if (tmp) + nim.push_back(tmp); + tmp = 0; + } + } + + int ret = 0; + for (int i = 0; i < nim.size(); i++) + ret ^= SG[nim[i]]; + printf("Case %d: %s\n", ++cases, ret ? "yes" : "no"); + } + return 0; +} +/* +9999 +13 1 +7 + +5 3 +1 3 4 + +6 2 +1 5 + +1 1 +1 + +1 0 + +*/ diff --git a/12848.cpp b/12848.cpp new file mode 100644 index 0000000..8d8b03e --- /dev/null +++ b/12848.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int testcase, H1, S1, H2, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &H1, &S1, &H2); + int a = S1 * (H2 - 1), b = H1 - 1, g; + g = __gcd(a, b); + a /= g, b /= g; + printf("Case %d:", ++cases); + if (a / b > 0) + printf(" %d", a / b); + if (a % b) + printf(" %d/%d", a % b, b); + puts(""); + } + return 0; +} diff --git a/12849.cpp b/12849.cpp new file mode 100644 index 0000000..1874ae3 --- /dev/null +++ b/12849.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +void gaussianElimination(double mtx[][16], int n, double sol[], int nosol[]) +{ +#define eps 1e-6 + int i, j; + for (i = 0; i < n; i++) + { + int k = i; + for (j = i; j < n; j++) + if (fabs(mtx[k][i]) < fabs(mtx[j][i])) + k = j; + if (fabs(mtx[k][i]) < eps) + continue; + if (k != i) + { + for (j = 0; j <= n; j++) + swap(mtx[k][j], mtx[i][j]); + } + for (j = 0; j < n; j++) + { + if (i == j) + continue; + for (k = n; k >= i; k--) + { + mtx[j][k] -= mtx[j][i] / mtx[i][i] * mtx[i][k]; + } + } + } + for (int i = 0; i < n; i++) + nosol[i] = 0; + for (i = n - 1; i >= 0; i--) + { + if (fabs(mtx[i][i]) < eps) + nosol[i] = 1; + else + { + if (fabs(mtx[i][n]) < eps) + sol[i] = 0; + else + sol[i] = mtx[i][n] / mtx[i][i]; + } + for (j = i + 1; j < n; j++) + if (fabs(mtx[i][j]) > eps && nosol[j]) + nosol[i] = 1; + } +} +int main() +{ + int testcase, cases = 0; + double f[16][16], ret[16]; + int sol[16]; + scanf("%d", &testcase); + while (testcase--) + { + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 4; j++) + scanf("%lf", &f[i][j]); + } + gaussianElimination(f, 3, ret, sol); + printf("Case %d: %.2lf %.2lf %.2lf\n", ++cases, ret[0], ret[1], ret[2]); + } + return 0; +} +/* +2 +3 3 1 20.00 +6 0 2 20.00 +6 4 0 20.00 +3 0 1 6.00 +0 2 2 10.00 +1 3 1 10.00 +*/ diff --git a/1285.cpp b/1285.cpp new file mode 100644 index 0000000..82b9388 --- /dev/null +++ b/1285.cpp @@ -0,0 +1,210 @@ +#include + +using namespace std; + +const int MAXN = 440; +const int MAXM = 55000; +const double EPS = 1e-8; + +int p[MAXN], size[MAXN], pos[MAXN], a[MAXN], wei[MAXN], lst[MAXN], n, m, timer, x, y, l, tc, s, e; +double dist[MAXN][MAXN], dfn[MAXN], had[MAXN], len[MAXN][MAXN], ans; + +inline int csqr(int x) +{ + return x * x; +} +struct linerec +{ + int y, next; +} map_[MAXM * 2]; +inline void line(int x, int y) +{ + map_[++timer].y = y; + map_[timer].next = a[x]; + a[x] = timer; + map_[++timer].y = x; + map_[timer].next = a[y]; + a[y] = timer; +} +struct rec +{ + int x, y, z, c, num; +} dat[MAXN]; +bool operator<(rec a, rec b) +{ + return a.z < b.z; +} +inline double dis(rec a, rec b) +{ + return sqrt(csqr(a.x - b.x) + csqr(a.y - b.y) + csqr(a.z - b.z)); +} +inline void denew(double &x, double y) +{ + if (x - y > EPS) + { + x = y; + } +} +int getp(int x) +{ + int t = x, t1; + while (p[t] != t) + { + t = p[t]; + } + while (x != t) + { + t1 = p[x]; + p[x] = t; + x = t1; + } + return x; +} +inline bool gengraph(int siz) +{ + l = 0; + for (int t = 1; t <= n; t++) + { + wei[t] = 0; + } + for (int t = 1; t <= siz; t++) + { + int i = dat[t].num; + int j = getp(i); + if ((!wei[j]) && (size[j] > 1 || (size[j] == 1 && (j == getp(1) || j == getp(n))))) + { + lst[wei[j] = ++l] = j; + } + wei[i] = wei[j]; + } + s = wei[getp(1)]; + e = wei[getp(n)]; + if ((!s) || (!e)) + { + return 0; + } + for (int i = 1; i <= l; i++) + { + for (int j = 1; j <= l; j++) + { + dist[i][j] = 0x1fffffff; + } + } + for (int i = 1; i <= siz; i++) + { + if (wei[dat[i].num]) + { + for (int j = 1; j <= siz; j++) + { + if (wei[dat[j].num] && wei[dat[i].num] != wei[dat[j].num] && dat[i].c && dat[j].c) + { + double l = len[i][j] - 1 + size[getp(dat[j].num)] * 0.5; + denew(dist[wei[dat[i].num]][wei[dat[j].num]], l); + } + } + } + } + return 1; +} + +inline void dij() +{ + for (int i = 1; i <= l; i++) + { + dfn[i] = 0x3fffffff, had[i] = 0; + } + dfn[s] = size[getp(1)] * 0.5; + for (int i = 1; i < l; i++) + { + int low = 0x3fffffff, lowk; + for (int j = 1; j <= l; j++) + if ((!had[j]) && low > dfn[j]) + { + low = dfn[j], lowk = j; + } + if (lowk == e) + { + break; + } + had[lowk] = 1; + for (int j = 1; j <= l; j++) + if (!had[j]) + { + denew(dfn[j], dfn[lowk] + dist[lowk][j]); + } + } + denew(ans, dfn[e]); +} + +int main() +{ + tc = 0; + while (scanf("%d%d", &n, &m) != EOF) + { + for (int i = 1; i <= n; i++) + { + scanf("%d%d%d%d", &dat[i].x, &dat[i].y, &dat[i].z, &dat[i].c), size[i] = dat[i].c; + } + for (int i = 1; i <= n; i++) + { + dat[i].num = i; + } + dat[n + 1].z = 11000; + std::sort(dat + 1, dat + n + 1); + for (int i = 1; i <= n; i++) + { + pos[dat[i].num] = i; + } + memset(a, 0, sizeof(a)); + timer = 0; + for (int i = 1; i <= m; i++) + { + scanf("%d%d", &x, &y); + line(x, y); + } + ans = 0x7fffffff; + for (int i = 1; i <= n; i++) + { + p[i] = i; + } + for (int i = 1; i <= n; i++) + for (int j = 1; j <= n; j++) + { + len[i][j] = dis(dat[i], dat[j]); + } + for (int t = 1; t <= n; t++) + { + int i = dat[t].num, ti = getp(i); + for (int j = a[i]; j; j = map_[j].next) + if (pos[map_[j].y] < t) + { + int tj = getp(map_[j].y); + p[tj] = ti; + if (tj != ti) + { + size[ti] += size[tj]; + } + } + if (dat[t].z != dat[t + 1].z && t >= pos[1] && t >= pos[n]) + { + if (gengraph(t)) + { + dij(); + } + if (getp(1) == getp(n)) + { + break; + } + } + } + if (ans - 0x1fffffff < EPS) + { + printf("Case %d: %.4lf\n", ++tc, ans); + } + else + { + printf("Case %d: impossible\n", ++tc); + } + } + return 0; +} diff --git a/12850.cpp b/12850.cpp new file mode 100644 index 0000000..665caa4 --- /dev/null +++ b/12850.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +template +inline char read(T &n) +{ + T x = 0, tmp = 1; + char c = getchar(); + while ((c < '0' || c > '9') && c != '-' && c != EOF) + { + c = getchar(); + } + if (c == '-') + { + c = getchar(), tmp = -1; + } + while (c >= '0' && c <= '9') + { + x *= 10, x += (c - '0'), c = getchar(); + } + n = x * tmp; + return c; +} + +template +inline void write(T n) +{ + if (n < 0) + { + putchar('-'); + n = -n; + } + int len = 0, data[20]; + while (n) + { + data[len++] = n % 10; + n /= 10; + } + if (!len) + { + data[len++] = 0; + } + while (len--) + { + putchar(data[len] + 48); + } +} + +const double eps = 1e-8; + +int main() +{ + int T, ic = 1; + read(T); + while (T--) + { + double r, t, s, v; + scanf("%lf %lf", &r, &t); + s = (t * r) / (r - 1) + eps; + v = t / (r - 1) + eps; + printf("Case %d: %.3f %.3f\n", ic++, v, s); + } + return 0; +} diff --git a/12851.cpp b/12851.cpp new file mode 100644 index 0000000..e351549 --- /dev/null +++ b/12851.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + double D, V; + const double pi = acos(-1); + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lf %lf", &D, &V); + V *= 282.0; + double r = sqrt(V * 3 / (pi * D * 7) * 4); + printf("Case %d: %.3lf\n", ++cases, r * 2); + } + return 0; +} +/* +2 +12 25 +10 33 +*/ diff --git a/12852.cpp b/12852.cpp new file mode 100644 index 0000000..4e489c8 --- /dev/null +++ b/12852.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +long long gcd(long long x, long long y) +{ + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; +} +long long lcm(long long x, long long y) +{ + return x / gcd(x, y) * y; +} +int main() +{ + int testcase, cases = 0, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + long long ret = 1, x; + for (int i = 0; i < n; i++) + scanf("%lld", &x), ret = lcm(ret, x); + printf("Case %d: %lld\n", ++cases, ret * (5 + 10 + 20)); + } + return 0; +} +/* +2 +3 +4 5 6 +4 +10 14 15 35 +*/ diff --git a/12853.cpp b/12853.cpp new file mode 100644 index 0000000..4884d2e --- /dev/null +++ b/12853.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + const double pi = acos(-1); + double D, N, A; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lf %lf", &D, &N); + A = D / (N - 1); + double ret = 2 * pi * (A + D); + printf("Case %d: %.3lf\n", ++cases, ret); + } + return 0; +} diff --git a/12854.cpp b/12854.cpp new file mode 100644 index 0000000..5c08b15 --- /dev/null +++ b/12854.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + while (true) + { + int a[10], b[10], y = 1; + for (int i = 0; i < 5; i++) + if (scanf("%d", &a[i]) != 1) + return 0; + for (int i = 0; i < 5; i++) + if (scanf("%d", &b[i]) != 1) + return 0; + for (int i = 0; i < 5; i++) + y &= a[i] != b[i]; + puts(y ? "Y" : "N"); + } + return 0; +} +/* +1 1 0 1 0 +0 0 1 0 1 +1 0 0 1 0 +1 0 1 1 0 +*/ diff --git a/12855.cpp b/12855.cpp new file mode 100644 index 0000000..37f3671 --- /dev/null +++ b/12855.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int main() +{ + long long A, B; + char s[8192]; + while (scanf("%lld %lld", &A, &B) == 2) + { + scanf("%s", s); + B = A - B; + int n = strlen(s); + long long ret = 0; + long long inv = 0, w = 0, b = 0; + for (int i = 0; i < n; i++) + { + if (s[i] == 'W') + w++; + else + inv += w; + } + int fw = 0, fb = n; + for (int i = 0; i < n; i++) + if (s[i] == 'W') + fw = i, i = n; + for (int i = n - 1; i >= 0; i--) + if (s[i] == 'B') + fb = i, i = -1; + w = b = 0; + for (int i = fw; i <= fb; i++) + if (s[i] == 'W') + w++; + else + b++; + while (inv) + { + if ((w + b - 1) * B <= A) + { + ret += inv * B; + inv = 0; + } + else + { + ret += A; + // swap(s[fw], s[fb]); + inv -= w + b - 1, w--, b--; + fw++, fb--; + while (fw <= fb && s[fw] != 'W') + fw++, b--; + while (fb >= fw && s[fb] != 'B') + fb--, w--; + } + } + printf("%lld\n", ret); + } + return 0; +} +/* +2 1 +BWWB +5 3 +WBWWBWBWBWBBBWWBBB +1000000 0 +W +5 5 +BWBWBWBWBBWBWBWB +24 22 +BWWWWBWWWWWBWWBBBBWBWBWWBWBWW +*/ diff --git a/12856.cpp b/12856.cpp new file mode 100644 index 0000000..65c6520 --- /dev/null +++ b/12856.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +char s[1048576]; +int main() +{ + int mod3[32]; + for (int i = 0; i < 32; i++) + mod3[i] = i % 3; + while (gets(s)) + { + long long ret = 0; + for (int i = 0; s[i]; i++) + { + if (s[i] >= '0' && s[i] <= '9') + { + int c[3] = {1, 0, 0}, sum = 0; + while (s[i] >= '0' && s[i] <= '9') + { + sum += s[i] - '0'; + if (sum >= 3) + sum = mod3[sum]; + ret += c[sum], c[sum]++; + i++; + } + i--; + } + } + printf("%lld\n", ret); + } + return 0; +} +/* +130a303 +0000000000 +icpc2014regional +*/ diff --git a/12857.cpp b/12857.cpp new file mode 100644 index 0000000..9d3e2e1 --- /dev/null +++ b/12857.cpp @@ -0,0 +1,166 @@ +#include + +using namespace std; + +const int MAX = (int)(1e9); + +int n; +string s[200]; + +template +struct Trie +{ + int at, nxt[N][M], cnt[N], f[N][201]; + void init() + { + memset(f, 0xff, sizeof(f)); + at = -1; + add(); + } + int add() + { + at++; + cnt[at] = 0; + memset(nxt[at], 0xff, sizeof(nxt[at])); + return at; + } + void insert(string &word) + { + int u = 0; + for (int i = 0; i < word.size(); ++i) + { + cnt[u]++; + int &v = nxt[u][word[i] - 'A']; + if (v == -1) + { + v = add(); + } + u = v; + } + cnt[u]++; + } + void dfs(int u, int d) + { + if (cnt[u] == 1) + { + f[u][0] = f[u][1] = 0; + return; + } + for (int i = 0; i < M; ++i) + { + int v = nxt[u][i]; + if (v == -1) + { + continue; + } + dfs(v, d + 1); + } + /* + * 0: u = 0; + * 1: l = 1; + * 2: r = 1; + * 3: other; + * */ + int g[2][201][4], add[4]; + int x = 0, y = 1; + for (int i = 0; i <= cnt[u]; ++i) + { + for (int j = 0; j < 4; ++j) + { + g[x][i][j] = (i == 0 ? 0 : MAX); + } + } + for (int i = 0; i < M; ++i) + { + int v = nxt[u][i]; + if (v == -1) + { + continue; + } + for (int j = 0; j <= cnt[u]; ++j) + { + for (int k = 0; k < 4; ++k) + { + g[y][j][k] = MAX; + } + } + for (int j = 0; j <= cnt[u]; ++j) + { + for (int k = 0; k <= cnt[v] && j + k <= cnt[u]; ++k) + { + memset(add, 0x00, sizeof(add)); + if (k == 1) + { + if (u == 0) + { + add[0] += d; + } + add[3] += d; + if (cnt[u] != 2) + { + add[2] += d; + } + } + if (cnt[v] - k == 1) + { + if (u == 0) + { + add[0] += d; + } + add[3] += d; + if (cnt[u] != 2) + { + add[1] += d; + } + } + for (int p = 0; p < 4; ++p) + { + g[y][j + k][p] = min(g[y][j + k][p], g[x][j][p] + f[v][k] + add[p]); + } + } + } + swap(x, y); + } + for (int i = 0; i <= cnt[u]; ++i) + { + if (u == 0) + { + f[u][i] = g[x][i][0]; + } + else + { + if (i == 1) + { + f[u][i] = g[x][i][1]; + } + else if (i == cnt[u] - 1) + { + f[u][i] = g[x][i][2]; + } + else + { + f[u][i] = g[x][i][3]; + } + } + } + } +}; + +Trie<200 * 18 + 1, 26> trie; + +int main() +{ + ios::sync_with_stdio(false); + while (cin >> n) + { + trie.init(); + for (int i = 0; i < 2 * n; ++i) + { + cin >> s[i]; + trie.insert(s[i]); + } + trie.dfs(0, 1); + cout << trie.f[0][n] * n << endl; + } + return 0; +} diff --git a/12858.cpp b/12858.cpp new file mode 100644 index 0000000..9437d1d --- /dev/null +++ b/12858.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int C[65536]; +vector g[65536]; +int search(int n) +{ + set S[65536], UNION; + queue Q, P; + int u, v, p, gg; + for (int i = 0; i < n; i++) + Q.push(i), P.push(C[i]), S[i].insert(C[i]), UNION.insert(C[i]); + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + p = P.front(), P.pop(); + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + gg = __gcd(p, C[v]); + if (S[v].find(gg) == S[v].end()) + { + S[v].insert(gg), UNION.insert(gg); + Q.push(v), P.push(gg); + } + } + } + int ret = UNION.size(); + return ret; +} +int main() +{ + int n, m, x, y; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + { + scanf("%d", &C[i]); + g[i].clear(); + } + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + x--, y--; + g[x].push_back(y); + g[y].push_back(x); + } + printf("%d\n", search(n)); + } + return 0; +} +/* +2 1 +1 9 +1 2 +4 2 +1 2 3 4 +1 3 +1 2 +4 3 +30 42 105 70 +2 4 +1 2 +2 3 +*/ diff --git a/12859.cpp b/12859.cpp new file mode 100644 index 0000000..4a8f1d1 --- /dev/null +++ b/12859.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXN = 400000 + 10; + +struct Point +{ + int x, y; + Point() {} + Point(int a, int b) : x(a), y(b) {} +}; + +Point P[MAXN]; +int n; + +bool cmp_top(const Point &a, const Point &b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} + +bool cmp_bottom(const Point &a, const Point &b) +{ + return a.x < b.x || (a.x == b.x && a.y > b.y); +} + +bool rv_top(Point &a, Point &b, Point &c) +{ + return a.y >= b.y && c.y >= b.y; +} + +bool rv_bottom(Point &a, Point &b, Point &c) +{ + return a.y <= b.y && c.y <= b.y; +} + +LL solve_top() +{ + vector st, border; + for (int i = 0, j = 0; i < n; ++i) + { + while (i + 1 < n && P[i + 1].x == P[i].x) + ++i; + while (j >= 2 && rv_top(st[j - 2], st[j - 1], P[i])) + --j, st.pop_back(); + st.push_back(P[i]); + ++j; + } + border.push_back(Point(st[0].x - 1, st[0].y + 1)); + int stop = -1; + for (size_t i = 1; i < st.size(); ++i) + { + if (st[i].y < st[i - 1].y) + { + stop = i; + break; + } + border.push_back(Point(st[i].x - 1, st[i - 1].y + 1)); + border.push_back(Point(st[i].x - 1, st[i].y + 1)); + } + if (stop != -1) + { + for (size_t i = stop; i < st.size(); ++i) + { + border.push_back(Point(st[i - 1].x + 1, st[i - 1].y + 1)); + border.push_back(Point(st[i - 1].x + 1, st[i].y + 1)); + } + } + border.push_back(Point(st.back().x + 1, st.back().y + 1)); + LL ret = 0; + for (size_t i = 0; i < border.size(); i += 2) + { + ret += (LL)border[i].y * (border[i + 1].x - border[i].x); + } + border[0].x = st[0].x; + border[border.size() - 1].x = st.back().x; + for (size_t i = 0; i < border.size(); ++i) + P[n++] = border[i]; + return ret; +} + +LL solve_bottom() +{ + vector st, border; + for (int i = 0, j = 0; i < n; ++i) + { + while (i + 1 < n && P[i + 1].x == P[i].x) + ++i; + while (j >= 2 && rv_bottom(st[j - 2], st[j - 1], P[i])) + --j, st.pop_back(); + st.push_back(P[i]); + ++j; + } + border.push_back(Point(st[0].x - 1, st[0].y - 1)); + int stop = -1; + for (size_t i = 1; i < st.size(); ++i) + { + if (st[i].y > st[i - 1].y) + { + stop = i; + break; + }; + border.push_back(Point(st[i].x - 1, st[i - 1].y - 1)); + border.push_back(Point(st[i].x - 1, st[i].y - 1)); + } + if (stop != -1) + { + for (size_t i = stop; i < st.size(); ++i) + { + border.push_back(Point(st[i - 1].x + 1, st[i - 1].y - 1)); + border.push_back(Point(st[i - 1].x + 1, st[i].y - 1)); + } + } + border.push_back(Point(st.back().x + 1, st.back().y - 1)); + LL ret = 0; + for (size_t i = 0; i < border.size(); i += 2) + { + ret += (LL)border[i].y * (border[i + 1].x - border[i].x); + } + return ret; +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + int x1, y1, x2, y2; + for (int i = 0; i < n; ++i) + scanf("%d%d", &P[i].x, &P[i].y); + if (n == 1) + { + puts("8 4"); + continue; + } + x1 = x2 = P[0].x, y1 = y2 = P[0].y; + for (int i = 0; i < n; ++i) + { + x1 = min(x1, P[i].x); + x2 = max(x2, P[i].x); + y1 = min(y1, P[i].y); + y2 = max(y2, P[i].y); + } + LL peri = (x2 - x1) * 2 + (y2 - y1) * 2 + 8; + sort(P, P + n, cmp_top); + LL area = solve_top(); + sort(P, P + n, cmp_bottom); + area -= solve_bottom(); + printf("%lld %lld\n", peri, area); + } + return 0; +} diff --git a/12860.cpp b/12860.cpp new file mode 100644 index 0000000..f68551c --- /dev/null +++ b/12860.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +vector g[65536]; +vector> X[524288]; +void buildGraph(int x) +{ + int d; + for (int i = X[x].size() - 1; i >= 0; i--) + { + int y = X[x][i].first, u = X[x][i].second; + for (int j = x - 5; j <= x + 5; j++) + { + if (j < 0) + continue; + int st = (int)(lower_bound(X[j].begin(), X[j].end(), make_pair(y - 5, -1)) - X[j].begin()); + for (int k = st; k < X[j].size(); k++) + { + if (X[j][k].first > y + 5) + break; + d = (x - j) * (x - j) + (X[j][k].first - y) * (X[j][k].first - y); + // d = abs(x-j) + abs(X[j][k].first - y); + if (d <= 25) + { + if (u != X[j][k].second) + { + g[u].push_back(X[j][k].second); + // printf("%d -> %d\n", u + 1, X[j][k].second + 1); + } + } + } + } + } +} +int visited[65536], dist[65536]; +int bfs(int st) +{ + queue Q; + int o[2] = {}, u, v; + Q.push(st), dist[st] = 1, visited[st] = 1; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + o[dist[u] & 1]++; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (visited[v] == 0) + { + dist[v] = dist[u] + 1; + visited[v] = 1; + Q.push(v); + } + } + } + return min(o[0], o[1]); +} +int main() +{ + int n, x, y; + while (scanf("%d", &n) == 1) + { + set S; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &x, &y); + X[x].push_back(make_pair(y, i)); + S.insert(x); + visited[i] = 0; + g[i].clear(); + } + for (set::iterator it = S.begin(); + it != S.end(); it++) + sort(X[*it].begin(), X[*it].end()); + for (set::iterator it = S.begin(); + it != S.end(); it++) + buildGraph(*it); + int ret = 0; + for (int i = 0; i < n; i++) + if (visited[i] == 0) + ret += bfs(i); + printf("%d\n", ret); + for (set::iterator it = S.begin(); + it != S.end(); it++) + { + X[*it].clear(); + } + } + return 0; +} +/* +3 +1 1 +2 2 +9 9 +2 +1 1 +2 2 +6 +1 3 +9 1 +11 7 +5 7 +13 5 +4 4 +2 +10 10 +50 30 +*/ diff --git a/12861.cpp b/12861.cpp new file mode 100644 index 0000000..e47e4db --- /dev/null +++ b/12861.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + int time[24] = {}, x; + for (int i = 0; i < n; i++) + scanf("%d", &x), time[x + 11]++; + for (int i = 0; i < 24; i++) + time[i] = time[i] & 1; + int A[64], m = 0, ret = 0x3f3f3f3f; + for (int i = 0; i < 24; i++) + if (time[i]) + A[m++] = i; + for (int i = 0; i < m; i++) + A[i + m] = A[i] + 24; + if (m == 0) + ret = 0; + for (int i = 0; i < m; i++) + { + int c = 0; + for (int j = 0; j < m; j += 2) + c += A[i + j + 1] - A[i + j]; + ret = min(ret, c); + } + printf("%d\n", ret); + } + return 0; +} +/* +6 +-3 -10 -5 11 4 4 +2 +-6 6 +8 +0 0 0 0 0 0 0 0 +*/ diff --git a/12862.cpp b/12862.cpp new file mode 100644 index 0000000..73b4a4a --- /dev/null +++ b/12862.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +vector> g[131072]; +int f[131072], w = 0, ret = 0; +void dfs(int u, int &friends, int dep) +{ + int v, ff = 0, cost; + friends = f[u]; + if (f[u]) + ret = max(ret, dep); + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i].first; + dfs(v, ff, dep + g[u][i].second); + if (ff > 0) + { + w += g[u][i].second; + friends += ff; + } + } +} +int main() +{ + int n, m, x, y, v; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + g[i].clear(), f[i] = 0; + for (int i = 1; i < n; i++) + { + scanf("%d %d %d", &x, &y, &v); + g[x].push_back(make_pair(y, v)); + } + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + f[x] = 1; + } + w = 0, ret = 0; + dfs(1, x, 0); + printf("%d\n", w - ret); + } + return 0; +} +/* +6 2 +1 2 2 +2 4 2 +1 3 3 +3 6 3 +3 5 1 +5 2 +4 2 +1 2 2 +1 3 1 +3 4 2 +2 4 +4 2 +1 4 1 +1 3 1 +4 2 2 +2 4 +*/ diff --git a/12863.cpp b/12863.cpp new file mode 100644 index 0000000..ea07da2 --- /dev/null +++ b/12863.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +#define MAXN 512 + +int V[MAXN][MAXN], R[MAXN][MAXN], C[MAXN][MAXN], TX, TY; + +struct Node +{ + int r, c, v, h; + Node(int a = 0, int b = 0, int d = 0, int e = 0) : r(a), c(b), v(d), h(e) {} + bool operator<(const Node &x) const + { + if (v != x.v) + { + return v > x.v; + } + return h > x.h; + } +}; + +priority_queue pQ; // dijkstra + +struct RangeTree +{ // 2D binary indexed tree + int A[MAXN][MAXN]; + int R, C; + void init(int R, int C) + { + this->R = R, this->C = C; + memset(A, 0, sizeof(A)); + for (int i = 1; i <= R; i++) + { + for (int j = 1; j <= C; j++) + { + modify(i, j, 1); + } + } + } + + void modify(int x, int y, int val) + { + for (; x <= R; x += x & (-x)) + { + for (int i = y; i <= C; i += i & (-i)) + { + A[x][i] += val; + } + } + } + + int query(int x, int y) + { + int ret = 0; + for (; x > 0; x -= x & -x) + { + for (int i = y; i > 0; i -= i & (-i)) + { + ret += A[x][i]; + } + } + return ret; + } + + int rectSum(int lx, int rx, int ly, int ry) + { + return query(rx, ry) - query(lx - 1, ry) - query(rx, ly - 1) + query(lx - 1, ly - 1); + } + + void update(int lx, int rx, int ly, int ry, int val, int tot) + { // {val: update cost, tot: #unvisited point in area.} + if (tot == -1) + { + tot = rectSum(lx, rx, ly, ry); + } + if (tot == 0) + { + return; + } + if (lx == rx) + { + if (ly == ry) + { + pQ.push(Node(lx, ly, val + V[lx][ly], abs(lx - TX) + abs(ly - TY))); + modify(lx, ly, -1); + return; + } + + int cnt = rectSum(lx, rx, ly, (ly + ry) / 2); + if (cnt) + { + update(lx, rx, ly, (ly + ry) / 2, val, cnt); + } + if (cnt < tot) + { + update(lx, rx, (ly + ry) / 2 + 1, ry, val, tot - cnt); + } + } + else + { + int cnt = rectSum(lx, (lx + rx) / 2, ly, ry); + if (cnt) + { + update(lx, (lx + rx) / 2, ly, ry, val, cnt); + } + if (cnt < tot) + { + update((lx + rx) / 2 + 1, rx, ly, ry, val, tot - cnt); + } + } + } +} rangeTree; + +int findPath(int n, int m, int sx, int sy, int ex, int ey) +{ + if (sx == ex && sy == ey) + { + return 0; + } + TX = ex, TY = ey; + rangeTree.init(n, m); + rangeTree.modify(sx, sy, -1); + + while (!pQ.empty()) + { + pQ.pop(); + } + pQ.push(Node(sx, sy, V[sx][sy], abs(sx - ex) + abs(sy - ey))); + + Node u; + int lr, rr, lc, rc; + while (!pQ.empty()) + { + u = pQ.top(), pQ.pop(); + if (abs(u.r - ex) <= R[u.r][u.c] && abs(u.c - ey) <= C[u.r][u.c]) + { + return u.v; + } + lr = max(1, u.r - R[u.r][u.c]), rr = min(n, u.r + R[u.r][u.c]); + lc = max(1, u.c - C[u.r][u.c]), rc = min(m, u.c + C[u.r][u.c]); + rangeTree.update(lr, rr, lc, rc, u.v, -1); + } + return -1; +} + +int main() +{ + int n, m, q, X[16], Y[16]; + while (scanf("%d%d%d", &n, &m, &q) == 3) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + scanf("%d", &V[i][j]); + } + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + scanf("%d", &R[i][j]); + } + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + scanf("%d", &C[i][j]); + } + } + for (int i = 0; i < q; i++) + { + scanf("%d%d", &X[i], &Y[i]); + } + + for (int i = 1; i < q; i++) + { + int r = findPath(n, m, X[i - 1], Y[i - 1], X[i], Y[i]); + printf("%d%c", r, i == q - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/12864.cpp b/12864.cpp new file mode 100644 index 0000000..7b93c31 --- /dev/null +++ b/12864.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +const int MAXN = 2000000 + 10, MOD = 1e9 + 7; + +int seated[MAXN], knight[MAXN], sum[MAXN]; +int Q[MAXN], N, M; + +int main() +{ + while (scanf("%d%d", &N, &M) == 2) + { + for (int i = 1; i <= N; ++i) + { + seated[i] = knight[i] = 0; + } + for (int i = 0; i < M; ++i) + { + int a, b; + scanf("%d%d", &a, &b); + knight[a] = seated[b] = 1; + } + sum[0] = 0; + for (int i = 1; i <= N * 2; ++i) + { + int x = i > N ? i - N : i; + sum[i] = sum[i - 1]; + if (knight[x]) + --sum[i]; + if (seated[x]) + ++sum[i]; + } + int h = 0, t = -1, st(-1); + for (int i = 1; i <= N * 2; ++i) + { + while (h <= t && Q[h] <= i - N) + ++h; + if (i >= N) + { + int mx = sum[Q[h]]; + if (mx >= sum[i - N]) + { + st = i - N + 1; + break; + } + } + while (h <= t && sum[Q[t]] >= sum[i]) + --t; + Q[++t] = i; + } + LL ret(1); + for (int i = 0, cnt(0); i < N; ++i) + { + int x = st + i > N ? st + i - N : st + i; + if (!knight[x]) + ++cnt; + if (!seated[x]) + ret = ret * cnt % MOD, --cnt; + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/12865.cpp b/12865.cpp new file mode 100644 index 0000000..2f3f44a --- /dev/null +++ b/12865.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + int ret[65536] = {1}, sum = 1; + int nop[32767] = {}, cases = 0; + for (int i = 1; i <= 30000; i++) + { + vector f; + int first_prime; + for (int j = 2; j * j <= i; j++) + { + if (i % j == 0) + { + f.push_back(j); + } + } + // i * j == p * q (p, q < i) + // i = a * b, p = a * ?1, q = b * ?2 + // j = ?1 * ?2 + cases++; + first_prime = f.size() ? f[0] : i; + for (int j = 0, q1, q2 = 2; j < f.size(); j++) + {// f[j] x (i / f[j]) + for (; q2 < f[j]; q2++) + {// ?2 < a, ?1 < b + for (q1 = i / f[j] - 1; q1 >= 1; q1--) + nop[q1 * q2] = cases; + } + } + for (int j = i / first_prime; j <= i; j++) + sum += nop[j] != cases; + ret[i] = sum; + } + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + printf("%d\n", ret[n]); + } + return 0; +} diff --git a/12866.cpp b/12866.cpp new file mode 100644 index 0000000..dcc81d6 --- /dev/null +++ b/12866.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +// https://oeis.org/A006046 +// a(0) = 0, a(1) = 1, a(2k) = 3*a(k), a(2k+1) = 2*a(k) + a(k+1). +long long f(long long n) +{ + if (n < 2) + return n; + if (n & 1) + return f(n / 2) * 2 + f(n / 2 + 1); + else + return f(n / 2) * 3; +} + +unsigned long long dp[50] = {}; +unsigned long long g(long long n) +{ + if (n == 0) + return 0; + if (n == 1) + return 1; + if (n == 2) + return 3; + if (n == 3) + return 5; + if (n == 4) + return 9; + if (n == 5) + return 11; + if (n == 6) + return 15; + if (n == 7) + return 19; + long long i, j; + for (i = 1, j = 1; 8 * i < n; i <<= 1, j++) + ; + // printf("%lld %lld %lld %lld\n", j, n, i, n - 4 * i); + return dp[j] + 2 * g(n - 4 * i); +} +int main() +{ + dp[1] = 9, dp[2] = 27; + for (int i = 3; i < 50; i++) + dp[i] = dp[i - 1] * 3; + + // int C[105][105] = {}, totsum = 0; + // C[0][0] = 1; + // for (int i = 0; i < 100; i++) { + // C[i][0] = 1; + // int sum = 1; + // for (int j = 1; j <= i; j++) { + // C[i][j] = (C[i-1][j] + C[i-1][j-1])&1; + // sum += C[i][j]; + // } + // totsum += sum; + // printf("%2d: %5d %5d\n", i, sum, totsum); + // } + // for (int i = 0; i < 50; i++) { + // for (int j = 0; j <= i; j++) + // printf("%d", C[i][j]); + // puts(""); + // } + long long L, R; + while (scanf("%lld %lld", &L, &R) == 2) + { + if (L == 0 && R == 0) + break; + // printf("%lld %lld\n", f(R + 1), g(R + 1)); + // long long ret = f(R + 1) - f(L); + // printf("%lld\n", ret); + unsigned long long ret2 = g(R + 1) - g(L); + printf("%llu\n", ret2); + } + return 0; +} diff --git a/12869.cpp b/12869.cpp new file mode 100644 index 0000000..865e0bb --- /dev/null +++ b/12869.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + long long L, R; + while (scanf("%lld%lld", &L, &R) == 2 && L) + { + printf("%lld\n", R / 5 - L / 5 + 1); + } + return 0; +} diff --git a/1287.cpp b/1287.cpp new file mode 100644 index 0000000..4407442 --- /dev/null +++ b/1287.cpp @@ -0,0 +1,263 @@ +#include + +using namespace std; + +struct event_t +{ + int type; + int a, b, iter; + bool operator<(const event_t &other) const + { + if (type == other.type) + { + if (a == other.a) + { + return b < other.b; + } + return a < other.a; + } + return type < other.type; + } +}; +int R = 0, D = 1, L = 2, U = 3; +typedef long long ll; +const int MAXN = 1 << 20; +// valid positions are [1..MAXN], commented lines are hints for a 2D BIT +// MODES OF USE: point-insert range-query OR range-insert point-query +struct BITree +{ + ll freq[MAXN + 1]; + BITree() + { + memset(freq, 0, sizeof freq); + } + void insert(int x, ll cnt = 1) + { + for (; x <= MAXN; x += x & -x) + { + freq[x] += cnt; + } + } + ll query(int x) + { + ll sum = 0; + for (; x; x -= x & -x) + { + sum += freq[x]; + } + return sum; + } + ll query_range(int xl, int xr) + { + return query(xr) - query(xl - 1); + } +} T, TT[2]; +vector> rs[1000001], cs[1000001]; +int main() +{ + ios::sync_with_stdio(0); + int r, c, m, n; + int zz = 1; + while (cin >> r >> c >> m >> n) + { + for (int i = 1; i <= r; i++) + { + rs[i].clear(); + } + for (int i = 1; i <= c; i++) + { + cs[i].clear(); + } + cout << "Case " << zz++ << ": "; + for (int i = 0; i < m; i++) + { + int a, b; + cin >> a >> b; + rs[a].push_back(make_pair(b, 1)); + cs[b].push_back(make_pair(a, 1)); + } + for (int i = 0; i < n; i++) + { + int a, b; + cin >> a >> b; + rs[a].push_back(make_pair(b, 2)); + cs[b].push_back(make_pair(a, 2)); + } + for (int i = 1; i <= r; i++) + { + sort(rs[i].begin(), rs[i].end()); + } + for (int i = 1; i <= c; i++) + { + sort(cs[i].begin(), cs[i].end()); + } + bool solved = false; + vector> events; + for (int iter = 0; iter < 2; iter++) + { + int cr = 1, cc = 0; + int d = R; + if (iter == 0) + { + cr = r; + cc = c + 1; + d = L; + } + bool done = false; + int asdf = 0; + while (!done) + { + event_t evt, evt2; + evt.iter = evt2.iter = iter; + if (d == R) + { + evt.type = 0; + evt.a = cc + 1; + int i = upper_bound(rs[cr].begin(), rs[cr].end(), make_pair(cc + 1, 0)) - rs[cr].begin(); + if (i >= rs[cr].size()) + { + done = true; + evt.b = c + 1; + } + else + { + cc = rs[cr][i].first; + d = (rs[cr][i].second == 1 ? U : D); + evt.b = cc - 1; + } + if (evt.a <= evt.b) + { + events.push_back(make_pair(cr, evt)); + } + } + else if (d == D) + { + evt.type = -1; + evt.a = cc; + evt.b = cr + 1; + int i = upper_bound(cs[cc].begin(), cs[cc].end(), make_pair(cr + 1, 0)) - cs[cc].begin(); + if (i >= cs[cc].size()) + { + done = true; + events.push_back(make_pair(cr + 1, evt)); + } + else + { + cr = cs[cc][i].first; + d = (cs[cc][i].second == 1 ? L : R); + evt2.type = 1; + evt2.a = cc; + if (evt.b <= cr - 1) + { + events.push_back(make_pair(evt.b, evt)); + events.push_back(make_pair(cr - 1, evt2)); + } + } + } + else if (d == L) + { + evt.type = 0; + evt.b = cc - 1; + int i = lower_bound(rs[cr].begin(), rs[cr].end(), make_pair(cc, 0)) - 1 - rs[cr].begin(); + if (i < 0) + { + cc = 0; + done = true; + evt.a = 1; + } + else + { + cc = rs[cr][i].first; + d = (rs[cr][i].second == 1 ? D : U); + evt.a = cc + 1; + } + if (evt.a <= evt.b) + { + events.push_back(make_pair(cr, evt)); + } + } + else if (d == U) + { + evt.type = 1; + evt.a = cc; + evt.b = cr - 1; + int i = lower_bound(cs[cc].begin(), cs[cc].end(), make_pair(cr, 0)) - 1 - cs[cc].begin(); + if (i < 0) + { + done = true; + evt2.type = -1; + evt2.a = cc; + events.push_back(make_pair(1, evt2)); + events.push_back(make_pair(cr - 1, evt)); + } + else + { + cr = cs[cc][i].first; + d = (cs[cc][i].second == 1 ? R : L); + evt2.type = -1; + evt2.a = cc; + if (cr + 1 <= evt.b) + { + events.push_back(make_pair(evt.b, evt)); + events.push_back(make_pair(cr + 1, evt2)); + } + } + } + } + if (iter == 0 && cr == 1 && cc == 0) + { + cout << 0 << endl; + solved = true; + break; + } + } + if (solved) + { + continue; + } + sort(events.begin(), events.end()); + ll cnt = 0; + int fr, fc; + set open[2]; + memset(T.freq, 0, sizeof T.freq); + memset(TT[0].freq, 0, sizeof TT[0].freq); + memset(TT[1].freq, 0, sizeof TT[1].freq); + for (int i = 0; i < events.size(); i++) + { + if (events[i].second.type == -1) + { + open[events[i].second.iter].insert(events[i].second.a); + T.insert(events[i].second.a, 1); + TT[events[i].second.iter].insert(events[i].second.a, 1); + } + else if (events[i].second.type == 1) + { + open[events[i].second.iter].erase(events[i].second.a); + T.insert(events[i].second.a, -1); + TT[events[i].second.iter].insert(events[i].second.a, -1); + } + else + { + if (cnt == 0) + { + set::iterator cand = open[1 - events[i].second.iter].lower_bound(events[i].second.a); + if (cand != open[1 - events[i].second.iter].end() && *cand <= events[i].second.b) + { + fr = events[i].first; + fc = *cand; + } + } + cnt += T.query_range(events[i].second.a, events[i].second.b) - TT[events[i].second.iter].query_range(events[i].second.a, events[i].second.b); + } + } + if (cnt == 0) + { + cout << "impossible" << endl; + } + else + { + cout << cnt << " " << fr << " " << fc << endl; + } + } + return 0; +} diff --git a/12870.cpp b/12870.cpp new file mode 100644 index 0000000..497545b --- /dev/null +++ b/12870.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +#define MAXN 128 +int dp[MAXN][MAXN][MAXN]; +int dp2[MAXN][MAXN][MAXN]; +int main() +{ + int testcase; + int n, m, A[128][128]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%d", &A[i][j]); + int nm = min(n, m); + int lenMAX[128] = {}, lenMIN[128] = {}; + for (int i = 0; i <= nm; i++) + { + lenMAX[i] = 0; + lenMIN[i] = -0x3f3f3f3f; + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + for (int k = 0; k <= nm; k++) + { + dp[i][j][k] = 0, dp2[i][j][k] = -0x3f3f3f3f; + } + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + dp[i][j][1] = max(dp[i][j][1], A[i][j]); + dp2[i][j][1] = max(dp2[i][j][1], -A[i][j]); + for (int k = 1; k <= nm; k++) + { + if (i > 0) + { + dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]); + dp2[i][j][k] = max(dp2[i][j][k], dp2[i - 1][j][k]); + } + if (j > 0) + { + dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]); + dp2[i][j][k] = max(dp2[i][j][k], dp2[i][j - 1][k]); + } + if (i > 0 && j > 0) + { + dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - 1][k - 1] + A[i][j]); + dp2[i][j][k] = max(dp2[i][j][k], dp2[i - 1][j - 1][k - 1] - A[i][j]); + } + // printf("%d %d %d %d\n", i, j, k, dp2[i][j][k]); + } + for (int k = 1; k <= nm; k++) + lenMAX[k] = max(lenMAX[k], dp[i][j][k]), lenMIN[k] = max(lenMIN[k], dp2[i][j][k]); + } + } + int ret = 0; + for (int i = 2; i <= nm; i += 2) + { + ret = max(ret, lenMAX[i / 2] + lenMIN[i]); + // printf("%d %d %d\n", i, lenMAX[i/2], lenMIN[i]); + } + printf("%d\n", ret); + } + return 0; +} +/* + 9999 + 4 4 + 1 1 1 4 + 1 3 1 1 + 1 1 2 1 + 1 1 1 1 + 3 5 + 1 1 1 1 1 + 1 1 1 1 1 + 1 1 1 1 1 + 3 4 + 0 0 1 0 + 0 0 2 0 + 0 1 0 0 + 2 2 + 0 1 + 2 0 + 2 3 + 10 20 30 + 30 4 50 + 3 2 + 10 20 + 30 30 + 4 50 + */ \ No newline at end of file diff --git a/12871.cpp b/12871.cpp new file mode 100644 index 0000000..f7d166f --- /dev/null +++ b/12871.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define MAXN 1024 + +int A[MAXN][MAXN], solv[MAXN][MAXN]; +int g[MAXN][MAXN]; +const int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}; +const int dy[] = {1, -1, 0, 1, -1, 0, 1, -1}; +int n, m; +int isplace(int x, int y) +{ + int v = A[x][y], tx, ty; + int unknown = 0, score = 0; + for (int i = 0; i < 8; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= n || ty >= m) + continue; + if (solv[tx][ty]) + score += g[tx][ty]; + else + unknown++; + } + if (v < 3) + return -1; + if (v > 8) + return 1; + if (score + unknown < v) + return 1; + if (score + 4 > v) + return -1; + return 0;// not sure. +} +int main() +{ + int testcase; + int t; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + scanf("%d", &A[i][j]); + } + } + memset(solv, 0, sizeof(solv)); + int test = n * m; + while (test) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (solv[i][j]) + continue; + int t = isplace(i, j); + if (t) + { + test--; + g[i][j] = t > 0; + solv[i][j] = 1; + } + } + } + } + for (int i = 0; i < n; i++, puts("")) + { + for (int j = 0; j < m; j++) + { + if (solv[i][j]) + putchar(g[i][j] ? 'L' : '-'); + else + { + assert(false); + } + } + } + } + return 0; +} +/* +9999 +3 4 +2 6 3 2 +7 5 7 5 +6 7 3 2 + +3 3 +7 9 7 +9 12 9 +7 9 7 + +3 1 +0 +0 +0 + +*/ diff --git a/12872.cpp b/12872.cpp new file mode 100644 index 0000000..beefbcc --- /dev/null +++ b/12872.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +int g[32][32], n, m; +int A[900][2], N, SUM = 0; +int isEmpty() +{ + return SUM == 0; +} +void remove(int x, int y, int w) +{ + for (int i = x - w; i <= x + w; i++) + g[i][y]--, SUM--; + for (int i = y - w; i <= y + w; i++) + g[x][i]--, SUM--; + g[x][y]++, SUM++; +} +void resume(int x, int y, int w) +{ + for (int i = x - w; i <= x + w; i++) + g[i][y]++, SUM++; + for (int i = y - w; i <= y + w; i++) + g[x][i]++, SUM++; + g[x][y]--, SUM--; +} +int place(int x, int y, int w) +{ + for (int i = x - w; i <= x + w; i++) + if (!g[i][y]) + return 0; + for (int i = y - w; i <= y + w; i++) + if (!g[x][i]) + return 0; + return 1; +} +int dfs(int idx, int used, int LX, int LY) +{ + if (isEmpty()) + { + printf("%d\n%d %d\n", used, LX + 1, LY + 1); + return 1; + } + if (idx == N) + return 0; + int t = min(min(A[idx][0], A[idx][1]), min(n - 1 - A[idx][0], m - 1 - A[idx][1])); + for (int i = t; i >= 1; i--) + { + if (place(A[idx][0], A[idx][1], i)) + { + remove(A[idx][0], A[idx][1], i); + if (dfs(idx + 1, used + 1, A[idx][0], A[idx][1])) + return 1; + resume(A[idx][0], A[idx][1], i); + } + } + if (dfs(idx + 1, used, LX, LY)) + return 1; + return 0; +} +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%d", &g[i][j]), SUM += g[i][j]; + N = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (g[i][j] == 1) + A[N][0] = i, A[N][1] = j, N++; + dfs(0, 0, -1, -1); + } + return 0; +} +/* +2 +5 5 +0 1 1 0 0 +1 1 2 0 0 +1 2 1 1 1 +0 0 1 0 0 +0 0 1 0 0 +10 11 +0 0 0 0 1 1 0 0 0 0 0 +0 0 0 0 1 1 0 1 0 0 0 +0 0 1 1 1 2 2 1 1 0 0 +0 0 1 2 2 1 1 2 2 0 0 +0 0 1 1 3 1 0 0 1 0 0 +0 0 0 2 1 2 2 1 1 1 1 +0 1 0 0 1 1 1 0 1 1 0 +1 1 1 0 1 1 1 1 3 1 1 +0 1 0 0 0 0 1 0 0 1 0 +0 0 0 0 0 0 1 0 0 0 0 +*/ diff --git a/12873.cpp b/12873.cpp new file mode 100644 index 0000000..10bf7de --- /dev/null +++ b/12873.cpp @@ -0,0 +1,144 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int P, S, C, m, x, y; + scanf("%d %d %d %d", &P, &S, &C, &m); + g.Init(P + S + 2); + int source = 0, sink = P + S + 1; + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + g.Addedge(x, P + y, 1); + } + for (int i = 1; i <= P; i++) + g.Addedge(source, i, 1); + for (int i = 1; i <= S; i++) + g.Addedge(P + i, sink, C); + int ret = g.Maxflow(source, sink); + printf("%d\n", ret); + } + return 0; +} +/* + 2 + 2 2 1 4 + 1 1 + 1 2 + 2 1 + 2 2 + 4 3 1 12 + 1 1 + 1 2 + 1 3 + 2 1 + 2 2 + 2 3 + 3 1 + 3 2 + 3 3 + 4 1 + 4 2 + 4 3 +*/ \ No newline at end of file diff --git a/12874.cpp b/12874.cpp new file mode 100644 index 0000000..9bd41ce --- /dev/null +++ b/12874.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} +inline int ReadInt(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + return 0; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + *x = (*x << 3) + (*x << 1) + c - '0'; + *x *= neg; + return 1; +} +int ret[131072]; +int main() +{ + int testcase, n, m; + // scanf("%d", &testcase); + ReadInt(&testcase); + while (testcase--) + { + // scanf("%d %d", &n, &m); + ReadInt(&n); + ReadInt(&m); + int A[20][20] = {}, a, b; + for (int i = 0; i < n; i++) + { + // scanf("%d %d", &a, &b); + ReadInt(&a); + ReadInt(&b); + A[b][a]++; + } + for (int i = 1; i <= 16; i++) + { + for (int j = 1; j <= 16; j++) + A[i][j] += A[i][j - 1]; + } + for (int i = 0; i <= n; i++) + ret[i] = 0; + for (int i = 0; i < m; i++) + { + int cover = 0; + for (int j = 1, t; j <= 16; j++) + { + t = i % j; + cover += A[j][16] - A[j][t]; + } + ret[cover]++; + } + for (int i = 0; i <= n; i++) + printf("%d\n", ret[i]); + } + return 0; +} +/* +9999 +3 30 +2 5 +3 5 +3 6 + +2 15 +1 2 +3 4 +*/ diff --git a/12875.cpp b/12875.cpp new file mode 100644 index 0000000..4a55c34 --- /dev/null +++ b/12875.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define MAXN 128 +#define MAXM 64 +long long dp[MAXN][MAXM]; +int profit[MAXN][MAXM], cost[MAXN][MAXN]; +int main() +{ + int testcase, n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%d", &profit[i][j]); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + scanf("%d", &cost[i][j]); + + memset(dp, 0, sizeof(dp)); + + long long ret = 0; + for (int i = 0; i < n; i++) + dp[i][0] = profit[i][0]; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + for (int k = 0; k < n; k++) + { + dp[k][i + 1] = max(dp[k][i + 1], dp[j][i] + profit[k][i + 1] - cost[j][k]); + } + } + } + for (int i = 0; i < n; i++) + ret = max(ret, dp[i][m - 1]); + printf("%lld\n", ret); + } + return 0; +} +/* +3 +3 4 +1 3 20 40 +50 20 1 2 +20 50 50 1 +0 10 10 +10 0 10 +10 10 0 +3 3 +20 20 20 +20 20 20 +20 20 20 +0 20 40 +20 0 40 +40 10 0 +2 4 +10 20 10 20 +20 10 20 10 +0 5 +5 0 +*/ diff --git a/12876.cpp b/12876.cpp new file mode 100644 index 0000000..0071038 --- /dev/null +++ b/12876.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} +inline int ReadInt(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + return 0; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + *x = (*x << 3) + (*x << 1) + c - '0'; + *x *= neg; + return 1; +} +int main() +{ + int testcase, n, m, x; + // scanf("%d", &testcase); + ReadInt(&testcase); + while (testcase--) + { + // scanf("%d %d", &n, &m); + ReadInt(&n); + ReadInt(&m); + int odd = 0, even = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + // scanf("%d", &x); + ReadInt(&x); + if (x == -1) + continue; + if ((i + j) & 1) + odd += x; + else + even += x; + } + } + printf("%d\n", abs(odd - even)); + } + return 0; +} +/* +1 +3 3 +2 2 3 +1 -1 3 +1 1 0 +*/ diff --git a/12877.cpp b/12877.cpp new file mode 100644 index 0000000..c6672cb --- /dev/null +++ b/12877.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +int mp[200], L[50], n, ret; +bool inv[200], used[10]; +char str[50][50]; + +void work(int num, int pos, int sum) +{ + int i, j, k, v; + k = L[num] - pos; + if (num == n) + { + j = mp[str[n][k]]; + if (j != -1) + { + if (j != sum % 10) + { + return; + } + if (k == 0) + { + if (sum / 10 == 0) + { + ret++; + } + return; + } + work(1, pos + 1, sum / 10); + } + else + { + v = sum % 10; + if (used[v]) + { + return; + } + if (v == 0 && inv[str[n][k]]) + { + return; + } + //used[v]=1; + //mp[str[n][k]]=v; + if (k == 0) + { + if (sum / 10 == 0) + { + ret++; + } + return; + } + used[v] = 1; + mp[str[n][k]] = v; + work(1, pos + 1, sum / 10); + used[v] = 0; + mp[str[n][k]] = -1; + } + } + else + { + if (k < 0) + { + work(num + 1, pos, sum); + return; + } + if (mp[str[num][k]] != -1) + { + sum += mp[str[num][k]]; + work(num + 1, pos, sum); + } + else + { + if (inv[str[num][k]] == 0 && used[0] == 0) + { + mp[str[num][k]] = 0; + used[0] = 1; + work(num + 1, pos, sum); + used[0] = 0; + mp[str[num][k]] = -1; + } + for (i = 1; i <= 9; i++) + { + if (used[i] == 0) + { + mp[str[num][k]] = i; + used[i] = 1; + work(num + 1, pos, sum + i); + used[i] = 0; + mp[str[num][k]] = -1; + } + } + } + } +} + +int main() +{ + int i, j, k; + while (scanf("%d", &n) == 1) + { + ret = 0; + memset(mp, -1, sizeof(mp)); + memset(inv, 0, sizeof(inv)); + memset(used, 0, sizeof(used)); + for (i = 1; i <= n; i++) + { + scanf("%s", str[i]); + L[i] = strlen(str[i]); + inv[str[i][0]] = 1; + } + work(1, 1, 0); + printf("%d\n", ret); + } + return 0; +} diff --git a/12878.cpp b/12878.cpp new file mode 100644 index 0000000..4772bef --- /dev/null +++ b/12878.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +#define MAXN 32767 +vector> g[MAXN]; +int dist1[MAXN], dist2[MAXN]; +void spfa(int st, int ed, int dist[]) +{ + static int inq[MAXN]; + queue Q; + int u, v, w; + dist[st] = 0, Q.push(st); + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + inq[u] = 0; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i].first, w = g[u][i].second; + if (dist[v] > dist[u] + w) + { + dist[v] = dist[u] + w; + if (!inq[v]) + inq[v] = 1, Q.push(v); + } + } + } +} +int main() +{ + int n, m; + int x, y, w; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + g[i].clear(); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &x, &y, &w); + g[x].push_back(make_pair(y, w)); + g[y].push_back(make_pair(x, w)); + } + + for (int i = 0; i < n; i++) + dist1[i] = dist2[i] = 0x3f3f3f3f; + spfa(0, n - 1, dist1); + spfa(n - 1, 0, dist2); + + int sp = dist1[n - 1], ret = 0; + for (int i = 0; i < n; i++) + { + x = i; + for (int j = 0; j < g[i].size(); j++) + { + y = g[i][j].first, w = g[i][j].second; + if (dist1[x] + w + dist2[y] == sp) + ret += w; + } + } + printf("%d\n", ret * 2); + } + return 0; +} +/* +10 15 +0 1 580 +1 4 90 +1 4 90 +4 9 250 +4 2 510 +2 7 600 +7 3 200 +3 3 380 +3 0 150 +0 3 100 +7 8 500 +7 9 620 +9 6 510 +6 5 145 +5 9 160 +4 7 +0 1 1 +0 2 2 +0 3 10 +0 3 3 +1 3 2 +2 3 1 +1 1 1 +*/ diff --git a/12879.cpp b/12879.cpp new file mode 100644 index 0000000..f90020a --- /dev/null +++ b/12879.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +typedef complex base; + +#define PI (2.0 * acos(0)) + +inline void fft(vector &a, bool invert) +{ + int n = (int)a.size(); + for (int i = 1, j = 0; i < n; ++i) + { + int bit = n >> 1; + for (; j >= bit; bit >>= 1) + { + j -= bit; + } + j += bit; + if (i < j) + { + swap(a[i], a[j]); + } + } + for (int len = 2; len <= n; len <<= 1) + { + double ang = 2 * PI / len * (invert ? -1 : 1); + base wlen(cos(ang), sin(ang)); + for (int i = 0; i < n; i += len) + { + base w(1); + for (int j = 0; j < len / 2; ++j) + { + base u = a[i + j], v = a[i + j + len / 2] * w; + a[i + j] = u + v; + a[i + j + len / 2] = u - v; + w *= wlen; + } + } + } + if (invert) + for (int i = 0; i < n; ++i) + { + a[i] /= n; + } +} + +inline void multiply(const vector &a, vector &res) +{ + vector fa(a.begin(), a.end()); + size_t n = 1; + while (n < a.size()) + { + n <<= 1; + } + n <<= 1; + fa.resize(n); + fft(fa, false); + for (size_t i = 0; i < n; ++i) + { + fa[i] *= fa[i]; + } + fft(fa, true); + res.resize(n); + for (size_t i = 0; i < n; ++i) + { + res[i] = int(fa[i].real() + 0.5); + } +} + +vector va, vr; +bool a[300000]; + +int main() +{ + int i, j, k, n, m, mx = 0, ret; + while (scanf("%d", &n) == 1) + { + memset(a, 0, sizeof(a)); + for (i = 0; i < n; i++) + { + scanf("%d", &k); + mx = max(mx, k); + a[k] = 1; + } + va.clear(); + vr.clear(); + va.push_back(1); + for (i = 1; i <= mx; i++) + { + va.push_back(a[i] ? 1 : 0); + } + multiply(va, vr); + scanf("%d", &m); + ret = 0; + for (i = 0; i < m; i++) + { + scanf("%d", &k); + if (k > 2 * mx) + { + continue; + } + ret += (vr[k] > 0); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/12880.cpp b/12880.cpp new file mode 100644 index 0000000..ccea790 --- /dev/null +++ b/12880.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; + +int main() +{ + int n, m, x, y; + while (scanf("%d %d", &n, &m) == 2) + { + g.Init(2 * n + 2); + int source = 2 * n, sink = 2 * n + 1; + for (int i = 0; i < n; i++) + g.Addedge(source, i, 1), g.Addedge(i + n, sink, 1); + for (int i = 0; i < m; i++) + { + scanf("%d %d", &x, &y); + g.Addedge(x, y + n, 1); + } + + int matching = g.Maxflow(source, sink); + puts(matching == n ? "YES" : "NO"); + } + return 0; +} +/* +9 9 +0 1 +1 2 +2 0 +3 4 +4 3 +5 6 +6 7 +7 8 +8 5 +*/ diff --git a/12881.cpp b/12881.cpp new file mode 100644 index 0000000..329b87d --- /dev/null +++ b/12881.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +int N, W, H, L; +struct state +{ + pair xy[4]; + bool operator<(const state &a) const + { + for (int i = 0; i < N; i++) + { + if (xy[i] != a.xy[i]) + { + return xy[i] < a.xy[i]; + } + } + return false; + } + void normal() + { + sort(xy + 1, xy + N); + } +}; +char g[16][16]; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +int isValid(int x, int y) +{ + return x >= 0 && y >= 0 && x < H && y < W; +} +int bfs(state init, int L) +{ + state u, v; + queue Q; + map R; + int x, y, tx, ty; + int used[11][11] = {}, testcase = 0; + init.normal(); + Q.push(init), R[init] = 0; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + int step = R[u]; + if (g[u.xy[0].first][u.xy[0].second] == 'X') + { + return step; + } + if (step > L) + { + continue; + } + testcase++; + for (int i = 0; i < N; i++) + { + x = u.xy[i].first, y = u.xy[i].second; + used[x][y] = testcase; + } + for (int i = 0; i < N; i++) + { + x = u.xy[i].first, y = u.xy[i].second; + for (int j = 0; j < 4; j++) + { + tx = x, ty = y; + while (isValid(tx + dx[j], ty + dy[j])) + { + if (g[tx + dx[j]][ty + dy[j]] == 'W') + { + break; + } + if (used[tx + dx[j]][ty + dy[j]] == testcase) + { + break; + } + tx += dx[j], ty += dy[j]; + } + v = u, v.xy[i] = make_pair(tx, ty); + v.normal(); + if (R.find(v) == R.end()) + { + R[v] = step + 1; + Q.push(v); + } + } + } + } + return 0x3f3f3f3f; +} + +int main() +{ + while (scanf("%d %d %d %d", &N, &W, &H, &L) == 4) + { + for (int i = 0; i < H; i++) + scanf("%s", g[i]); + + state init; + for (int i = 0; i < H; i++) + { + for (int j = 0; j < W; j++) + { + if (g[i][j] >= '1' && g[i][j] <= '9') + init.xy[g[i][j] - '1'] = make_pair(i, j); + } + } + + int ret = bfs(init, L); + if (ret > L) + puts("NO SOLUTION"); + else + printf("%d\n", ret); + } + return 0; +} diff --git a/12882.cpp b/12882.cpp new file mode 100644 index 0000000..9d34cac --- /dev/null +++ b/12882.cpp @@ -0,0 +1,316 @@ +#include + +using namespace std; + +#define print1(a) cout << a << endl +#define print2(a, b) cout << a << " " << b << endl +#define print3(a, b, c) cout << a << " " << b << " " << c << endl +#define oo (1 << 30) +#define PI 3.141592653589793 +#define pi (2.0 * acos(0.0)) +#define ERR 1e-5 +#define PRE 1e-8 +#define SZ(s) ((int)s.size()) +#define LL long long +#define ISS istringstream +#define OSS ostringstream +#define VS vector +#define VI vector +#define VD vector +#define VLL vector +#define SII set::iterator +#define SI set +#define mem(a, b) memset(a, b, sizeof(a)) +#define fr(i, a, b) for (i = a; i <= b; i++) +#define frn(i, a, b) for (i = a; i >= b; i--) +#define fri(a, b) for (i = a; i <= b; i++) +#define frin(a, b) for (i = a; i >= b; i--) +#define frj(a, b) for (j = a; j <= b; j++) +#define frjn(a, b) for (j = a; j >= b; j--) +#define frk(a, b) for (k = a; k <= b; k++) +#define frkn(a, b) for (k = a; k >= b; k--) +#define frl(a, b) for (l = a; l <= b; l++) +#define frln(a, b) for (l = a; l >= b; l--) +#define REP(i, n) for (i = 0; i < n; i++) +#define EQ(a, b) (fabs(a - b) < ERR) +#define all(a, b, c) \ + for (int I = 0; I < b; I++) \ + a[I] = c +#define CROSS(a, b, c, d) ((b.x - a.x) * (d.y - c.y) - (d.x - c.x) * (b.y - a.y)) +#define sqr(a) ((a) * (a)) +#define FORE(i, a) for (typeof((a).begin()) i = (a).begin(); i != (a).end(); i++) +#define typing(j, b) typeof((b).begin()) j = (b).begin(); +#define BE(a) a.begin(), a.end() +#define rev(a) reverse(BE(a)); +#define sorta(a) sort(BE(a)) +#define pb push_back +#define popb pop_back +#define mp make_pair +#define round(i, a) i = (a < 0) ? a - 0.5 : a + 0.5; +#define makeint(n, s) istringstream(s) >> n +#define inpow(a, x, y) \ + int i; \ + a = x; \ + fri(2, y) a *= x +#define cntbit(mask) __builtin_popcountll(mask) +//0 based print +#define debug_array(a, n) \ + for (int i = 0; i < n; i++) \ + cerr << a[i] << " "; \ + cerr << endl; +#define debug_matrix(mat, row, col) \ + for (int i = 0; i < row; i++) \ + { \ + for (int j = 0; j < col; j++) \ + cerr << mat[i][j] << " "; \ + cerr << endl; \ + } + +#define csprnt printf("Case %d: ", ++cas); +#define mod 1000000007 +#define eulerconstant 0.5772156649 + +template +void debug(T1 e) +{ + cout << e << endl; +} +template +void debug(T1 e1, T2 e2) +{ + cout << e1 << "\t" << e2 << endl; +} +template +void debug(T1 e1, T2 e2, T3 e3) +{ + cout << e1 << "\t" << e2 << "\t" << e3 << endl; +} +template +void debug(T1 e1, T2 e2, T3 e3, T4 e4) +{ + cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << endl; +} +template +void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) +{ + cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << endl; +} +template +void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) +{ + cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << "\t" << e6 << endl; +} +template +void debug(vector> e, int row, int col) +{ + int i, j; + REP(i, row) + { + REP(j, col) + cout << e[i][j] << " "; + cout << endl; + } + cout << endl; +} +template +void debug(vector> e, int row, int col) +{ + int i, j; + REP(i, row) + { + REP(j, col) + cout << e[i][j]; + cout << endl; + } + cout << endl; +} +template +void debug(T e[110][110], int row, int col) +{ + int i, j; + REP(i, row) + { + REP(j, col) + cout << e[i][j] << " "; + cout << endl; + } +} +template +string toString(T n) +{ + ostringstream oss; + oss << n; + oss.flush(); + return oss.str(); +} +int toInt(string s) +{ + int r = 0; + istringstream sin(s); + sin >> r; + return r; +} +bool isVowel(char ch) +{ + ch = tolower(ch); + if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') + { + return true; + } + return false; +} +bool isUpper(char c) +{ + return c >= 'A' && c <= 'Z'; +} +bool isLower(char c) +{ + return c >= 'a' && c <= 'z'; +} +//*************************************************My Code Starts Here********************************************************************************* +#define lim 151000 +int xlow[lim], xhigh[lim], ylow[lim], yhigh[lim]; +struct node +{ + int val, st; + int op; + int in; +}; +VI adj[lim]; +LL area[lim]; +bool col[lim]; +map mapofst; + +LL dfs(int node) +{ + col[node] = true; + LL ret = area[node]; + for (int i = 0; i < SZ(adj[node]); i++) + { + int tem = adj[node][i]; + if (col[tem]) + { + continue; + } + ret += dfs(tem); + } + return ret; +} +int n; +stack stck[2 * lim];//index stack +node arr[2 * lim]; + +bool comp(node p, node q) +{ + if (p.val != q.val) + { + return p.val < q.val; + } + return p.op < q.op; +} + +void makeEdge() +{ + mem(col, false); + sort(arr + 1, arr + 4 * n + 1, comp); + int i; + for (i = 1; i <= 4 * n; i++) + while (!stck[i].empty()) + { + stck[i].pop(); + } + mapofst.clear(); + int in = 0; + for (i = 1; i <= 4 * n; i++) + { + if (arr[i].op == 1) + { + col[arr[i].in] = true; + continue; + } + if (mapofst.find(arr[i].st) == mapofst.end())//notun + { + mapofst[arr[i].st] = (++in); + } + int now = mapofst[arr[i].st]; + while (!stck[now].empty() && col[stck[now].top()]) + { + stck[now].pop(); + } + if (!stck[now].empty()) + { + adj[arr[i].in].pb(stck[now].top()); + adj[stck[now].top()].pb(arr[i].in); + // print2(stck[now].top(),arr[i].in); + } + stck[now].push(arr[i].in); + } +} + +int main() +{ + //srand(time(NULL)); + //freopen("A.txt","r",stdin); + //freopen("B.txt","w",stdout); + while (cin >> n) + { + int i; + for (i = 1; i <= n; i++) + { + scanf("%d %d %d %d", &xlow[i], &ylow[i], &xhigh[i], &yhigh[i]); + area[i] = xhigh[i] * yhigh[i]; + xhigh[i] += xlow[i]; + yhigh[i] += ylow[i]; + adj[i].clear(); + } + for (i = 1; i <= n; i++) + { + arr[4 * i - 1].in = i; + arr[4 * i - 1].op = 0; + arr[4 * i - 1].val = xlow[i]; + arr[4 * i - 1].st = ylow[i]; + arr[4 * i - 2].in = i; + arr[4 * i - 2].op = 0; + arr[4 * i - 2].val = xlow[i]; + arr[4 * i - 2].st = yhigh[i]; + arr[4 * i - 3].in = i; + arr[4 * i - 3].op = 1; + arr[4 * i - 3].val = xhigh[i]; + arr[4 * i - 3].st = ylow[i]; + arr[4 * i].in = i; + arr[4 * i].op = 1; + arr[4 * i].val = xhigh[i]; + arr[4 * i].st = yhigh[i]; + } + makeEdge(); + for (i = 1; i <= n; i++) + { + arr[4 * i - 1].in = i; + arr[4 * i - 1].op = 0; + arr[4 * i - 1].val = ylow[i]; + arr[4 * i - 1].st = xlow[i]; + arr[4 * i - 2].in = i; + arr[4 * i - 2].op = 0; + arr[4 * i - 2].val = ylow[i]; + arr[4 * i - 2].st = xhigh[i]; + arr[4 * i - 3].in = i; + arr[4 * i - 3].op = 1; + arr[4 * i - 3].val = yhigh[i]; + arr[4 * i - 3].st = xlow[i]; + arr[4 * i].in = i; + arr[4 * i].op = 1; + arr[4 * i].val = yhigh[i]; + arr[4 * i].st = xhigh[i]; + } + makeEdge(); + mem(col, false); + LL ans = 0; + for (i = 1; i <= n; i++) + if (!col[i]) + { + ans = max(ans, dfs(i)); + } + print1(ans); + } + return 0; +} diff --git a/12883.cpp b/12883.cpp new file mode 100644 index 0000000..1d7de92 --- /dev/null +++ b/12883.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +struct Polygon +{ + vector pt; + vector Xs, Ys; + int n, m; + void init() + { + pt.clear(); + Xs.clear(); + Ys.clear(); + } + void add(int x, int y) + { + pt.push_back(PII(x, y)); + Xs.push_back(x); + Ys.push_back(y); + } + void build() + { + sort(Xs.begin(), Xs.end()); + sort(Ys.begin(), Ys.end()); + Xs.erase(unique(Xs.begin(), Xs.end()), Xs.end()); + Ys.erase(unique(Ys.begin(), Ys.end()), Ys.end()); + n = m = 0; + for (auto &p : pt) + { + p.first = lower_bound(Xs.begin(), Xs.end(), p.first) - Xs.begin(); + p.second = lower_bound(Ys.begin(), Ys.end(), p.second) - Ys.begin(); + n = max(n, p.first); + m = max(m, p.second); + } + } + void rotate() + { + vector tp; + for (auto &p : pt) + { + tp.push_back(PII(m - p.second, p.first)); + } + pt = tp; + swap(n, m); + } + bool same(const Polygon &r) + { + if (pt.size() != r.pt.size()) + { + return false; + } + if (n != r.n || m != r.m) + { + return false; + } + for (size_t s = 0; s < pt.size(); ++s) + { + size_t cnt(0); + for (size_t i = 0; i < pt.size(); ++i) + { + if (pt[(s + i) % pt.size()] == r.pt[i]) + { + ++cnt; + } + } + if (cnt == pt.size()) + { + return true; + } + } + return false; + } +} A, B; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + A.init(); + B.init(); + for (int i = 0; i < n; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + A.add(x, y); + } + scanf("%d", &n); + for (int i = 0; i < n; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + B.add(x, y); + } + A.build(); + B.build(); + if (A.same(B)) + { + puts("yes"); + } + else + { + A.rotate(); + if (A.same(B)) + { + puts("yes"); + } + else + { + A.rotate(); + if (A.same(B)) + { + puts("yes"); + } + else + { + A.rotate(); + if (A.same(B)) + { + puts("yes"); + } + else + { + puts("no"); + } + } + } + } + } + return 0; +} diff --git a/12884.cpp b/12884.cpp new file mode 100644 index 0000000..423c26d --- /dev/null +++ b/12884.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +const int N = 1010; +const int M = 10010; +const long long oo = 4557430888798830399; + +bool lab[N]; +int u[M], v[M], w[M], n, p, s, t; +long long dp[N][N][2]; + +struct Re +{ + int x, y, z; + Re() {} + Re(int x, int y, int z) : x(x), y(y), z(z) {} +}; + +void spfa() +{ + memset(dp, 63, sizeof(dp)); + if (lab[s]) + { + dp[s][0][0] = 0; + } + else + { + dp[s][0][1] = 0; + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < p; j++) + { + for (int k = 0; k < 2; k++) + { + if (lab[v[j]] && 0 == k) + { + dp[v[j]][i + 1][0] = min(dp[v[j]][i + 1][0], dp[u[j]][i][0] + w[j]); + } + else + { + dp[v[j]][i + 1][1] = min(dp[v[j]][i + 1][1], dp[u[j]][i][k] + w[j]); + } + if (lab[u[j]] && 0 == k) + { + dp[u[j]][i + 1][0] = min(dp[u[j]][i + 1][0], dp[v[j]][i][0] + w[j]); + } + else + { + dp[u[j]][i + 1][1] = min(dp[u[j]][i + 1][1], dp[v[j]][i][k] + w[j]); + } + } + } + } +} + +void solve() +{ + for (int i = 1; i <= n; i++) + { + if (dp[t][i][0] < dp[t][i][1]) + { + puts("Infinity"); + return; + } + if (dp[t][i][1] != oo) + { + break; + } + } + long long ret = -1; + for (int i = 1; i <= n; i++) + { + if (dp[t][i][0] != oo && dp[t][i][0] < dp[t][i][1]) + { + long long mx = oo; + for (int j = 1; j < i; j++) + { + if (dp[t][j][1] <= dp[t][i][0]) + { + mx = -1; + break; + } + long long tmp = (dp[t][j][1] - dp[t][i][0]) / (i - j); + if (0 == (dp[t][j][1] - dp[t][i][0]) % (i - j)) + { + tmp--; + } + mx = min(mx, tmp); + } + if (mx < 0) + { + continue; + } + for (int j = i + 1; j <= n; j++) + { + if (dp[t][i][0] < dp[t][j][1]) + { + continue; + } + long long tmp = (dp[t][i][0] - dp[t][j][1]) / (j - i); + if (0 == (dp[t][i][0] - dp[t][j][1]) % (j - i)) + { + tmp++; + } + if (mx < tmp) + { + mx = -1; + break; + } + } + ret = max(ret, mx); + } + } + if (ret < 0) + { + puts("Impossible"); + return; + } + else + { + cout << ret << endl; + } +} + +int main() +{ + while (scanf("%d%d%d%d", &n, &p, &s, &t) != EOF) + { + for (int i = 0; i < p; i++) + { + scanf("%d%d%d", &u[i], &v[i], &w[i]); + } + for (int i = 1; i <= n; i++) + { + lab[i] = false; + } + int m; + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + int x; + scanf("%d", &x); + lab[x] = true; + } + spfa(); + solve(); + } + return 0; +} diff --git a/12885.cpp b/12885.cpp new file mode 100644 index 0000000..2d05f67 --- /dev/null +++ b/12885.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +#define x first +#define y second + +const int MAXN = 400 + 10; + +PII dp[MAXN][MAXN]; +bool vs[MAXN][MAXN]; + +int a[MAXN], n; +char op[MAXN]; + +PII sub(PII a, PII b) +{ + return PII(a.x - b.y, a.y - b.x); +} + +PII mul(PII &a, PII &b) +{ + LL x = min(a.x * b.x, a.x * b.y); + x = min(x, a.y * b.x); + x = min(x, a.y * b.y); + LL y = max(a.x * b.x, a.x * b.y); + y = max(y, a.y * b.x); + y = max(y, a.y * b.y); + return PII(x, y); +} + +PII add(PII &a, PII &b) +{ + return PII(a.x + b.x, a.y + b.y); +} + +PII solve(int s, int d) +{ + if (vs[s][d]) + { + return dp[s][d]; + } + vs[s][d] = 1; + if (d == 1) + { + return dp[s][d] = PII(a[s], a[s]); + } + PII ret(9223372036854775807LL, -9223372036854775807LL - 1); + for (int i = 1; i < d; ++i) + { + PII l = solve(s, i); + PII r = solve(s + i, d - i); + PII rt, tp; + if (op[s + i - 1] == '?') + { + rt = sub(l, r); + tp = add(l, r); + rt.x = min(rt.x, tp.x); + rt.y = max(rt.y, tp.y); + tp = mul(l, r); + rt.x = min(rt.x, tp.x); + rt.y = max(rt.y, tp.y); + } + else if (op[s + i - 1] == '-') + { + rt = sub(l, r); + } + else if (op[s + i - 1] == '+') + { + rt = add(l, r); + } + else + { + rt = mul(l, r); + } + ret.x = min(ret.x, rt.x); + ret.y = max(ret.y, rt.y); + } + return dp[s][d] = ret; +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; ++i) + { + scanf("%d", a + i); + char s[10]; + scanf("%s", s); + op[i] = s[0]; + a[i + n] = a[i]; + op[i + n] = op[i]; + } + memset(vs, 0, sizeof(vs)); + stringstream sin; + for (int i = 0; i < n; ++i) + { + PII rt = solve(i, n); + sin << abs(rt.x) << abs(rt.y); + } + string ret; + sin >> ret; + printf("%s\n", ret.c_str()); + } + return 0; +} diff --git a/12886.cpp b/12886.cpp new file mode 100644 index 0000000..90cf87b --- /dev/null +++ b/12886.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +// ~ 11019 - Matrix Matcher +#define MAXN 2048 + +char T[MAXN][MAXN], P[MAXN][MAXN]; +int M[MAXN][MAXN]; + +// AC automation +struct Node +{ + Node *next[26], *fail; + int matched, label; + Node() + { + fail = NULL; + matched = -1; + label = -1; + memset(next, 0, sizeof(next)); + } +}; + +int insertTrie(const char str[], Node *root, int label) +{ + static Node *p; + static int i, idx; + p = root; + for (i = 0; str[i]; i++) + { + idx = str[i] - 'a'; + if (p->next[idx] == NULL) + p->next[idx] = new Node(); + p = p->next[idx]; + } + if (p->label == -1) + p->label = label; + return p->label; +} + +void buildACautomation(Node *root) +{ + queue Q; + Node *nd, *p; + root->fail = NULL; + Q.push(root); + while (!Q.empty()) + { + nd = Q.front(), Q.pop(); + for (int i = 0; i < 26; i++) + { + if (nd->next[i] == NULL) + continue; + Q.push(nd->next[i]); + p = nd->fail; + while (p != NULL && p->next[i] == NULL) + p = p->fail; + if (p == NULL) + nd->next[i]->fail = root; + else + nd->next[i]->fail = p->next[i]; + } + } +} + +void quertACautomaiton(const char *str, Node *root, int row) +{ + static Node *p, *q; + static int i, idx; + p = root; + for (i = 0; str[i]; i++) + { + idx = str[i] - 'a'; + while (p != root && p->next[idx] == NULL) + { + p = p->fail; + } + p = p->next[idx]; + p = (p == NULL) ? root : p; + q = p; + M[row][i] = -1; + if (q != root && q->label >= 0) + { + M[row][i] = q->label; + } + } +} + +void freeTrie(Node *root) +{ + queue Q; + Node *nd; + while (!Q.empty()) + { + nd = Q.front(), Q.pop(); + for (int i = 0; i < 26; i++) + { + if (nd->next[i] != NULL) + { + Q.push(nd->next[i]); + } + } + delete nd; + } +} +// // + +int kmpTable[MAXN]; +void KMPtable(int P[], int len) +{ + int i, j; + kmpTable[0] = -1, i = 1, j = -1; + while (i < len) + { + while (j >= 0 && P[j + 1] != P[i]) + { + j = kmpTable[j]; + } + if (P[j + 1] == P[i]) + { + j++; + } + kmpTable[i++] = j; + } +} + +int KMPMatching(int T[], int P[], int tlen, int plen) +{ + int i, j, matchCnt = 0; + for (i = 0, j = -1; i < tlen; i++) + { + while (j >= 0 && P[j + 1] != T[i]) + { + j = kmpTable[j]; + } + if (P[j + 1] == T[i]) + { + j++; + } + if (j == plen - 1) + { + matchCnt++; + j = kmpTable[j]; + } + } + return matchCnt; +} + +int main() +{ + int n, m, x, y, i, j, k; + int pattern[MAXN], str[MAXN]; + while (scanf("%d%d", &x, &y) == 2) + { + scanf("%d%d", &n, &m); + Node *root = new Node(); + for (i = 0; i < x; i++) + { + scanf("%s", P[i]); + pattern[i] = insertTrie(P[i], root, i); + } + for (i = 0; i < n; i++) + { + scanf("%s", T[i]); + } + + buildACautomation(root); + for (i = 0; i < n; i++) + { + quertACautomaiton(T[i], root, i); + } + + KMPtable(pattern, x); + int ret = 0; + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + str[j] = M[j][i]; + } + ret += KMPMatching(str, pattern, n, x); + } + printf("%d\n", ret); + freeTrie(root); + } + return 0; +} diff --git a/12887.cpp b/12887.cpp new file mode 100644 index 0000000..e61f9be --- /dev/null +++ b/12887.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +/** + add tallest people in position i + ways[n] = \sum ways[i] * ways[n-i-1]. + ^^^^^^[1] ^^^^^^^^^^[2] + [1] left tree pos[0, i-1] + [2] right tree pos[i+1, n-1] + then label of right tree must small than label of left tree + => Catalan number, an = (4n-2)/(n+1) * an-1 +**/ +long long inv(long long n, long long m) +{ // get n*? = 1 (mod m) + long long la = 1, lb = 0, ra = 0, rb = 1; + long long i = 0, t, mod = m; + while (n % m) + { + if (!i) + { + la -= n / m * ra, lb -= n / m * rb; + } + else + { + ra -= n / m * la, rb -= n / m * lb; + } + i = !i; + t = n, n = m, m = t % m; + } + return i ? (la % mod + mod) % mod : (ra % mod + mod) % mod; +} + +#define MAXN 5005 +long long Catalan[MAXN]; +const long long mod = 1000000007LL; + +int main() +{ + Catalan[0] = Catalan[1] = 1; + for (int i = 2; i < MAXN; i++) + { + Catalan[i] = Catalan[i - 1] * (4 * i - 2) % mod; + Catalan[i] = Catalan[i] * inv(i + 1, mod) % mod; + } + int n, T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + printf("%lld\n", Catalan[n]); + } + return 0; +} diff --git a/12888.cpp b/12888.cpp new file mode 100644 index 0000000..658821a --- /dev/null +++ b/12888.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define pb push_back +#define ins insert +#define ff first +#define ss second +#define ll long long int +#define llu long long unsigned +#define sss stringstream +#define oss ostringstream +#define iss istringstream +#define pii pair +#define _sq(x) ((x) * (x)) +#define lcm(a, b) ((a) * ((b) / gcd(a, b))) +#define mem(a) memset(a, 0, sizeof(a)) +#define _set(a) memset(a, -1, sizeof(a)) +#define popcount(n) __builtin_popcount(n) +#define popcountl(n) __builtin_popcountll(n) +#define ctz(x) __builtin_ctz(x)// # trailing 0s +#define ctzl(x) __builtin_ctzll(x) +#define clz(x) __builtin_clz(x) +#define clzl(x) __builtin_clzll(x)// # leading 0s +#define check(mask, k) (mask & (1 << k)) +#define set1(mask, k) (mask | (1 << k)) +#define set0(mask, k) (mask & (~(1 << k))) +#define READ(f) freopen(f, "r", stdin) +#define WRITE(f) freopen(f, "w", stdout) +#define pi acos(-1.0) +#define inf (1 << 30) +#define eps 1e-9 +#define MAX 1000005 +#define MAXL 15 + +int fr[MAX], mul[MAX]; + +void gen() +{ + int i, j; + for (i = 2; i < MAX; i++) + { + mul[i] = 1; + } + for (i = 2; i < MAX; i++) + { + if (fr[i] == 0) + { + for (j = i; j < MAX; j += i) + { + fr[j]++; + mul[j] *= i; + } + } + } +} + +int main() +{ + int tcase; + ll n, m, i; + gen(); + scanf("%d", &tcase); + while (tcase--) + { + scanf("%lld %lld", &n, &m); + if (n > m) + { + swap(n, m); + } + ll ans = n * m; + for (i = 2; i <= n; i++) + { + if (mul[i] == i) + { + ll tmp = (m / i); + ll tmp1 = (n / i); + if (fr[i] & 1) + { + ans -= (tmp * tmp1); + } + else + { + ans += (tmp * tmp1); + } + } + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/12889.cpp b/12889.cpp new file mode 100644 index 0000000..795cdcb --- /dev/null +++ b/12889.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +#define sc scanf +#define pf printf +#define pb push_back +#define pob pop_back +#define PI 2 * acos(0.0) +#define NL printf("\n") +#define gcd(a, b) __gcd(a, b) +#define lcm(a, b) (a * b) / gcd(a, b) +#define fr0(i, n) for (i = 0; i < n; i++) +#define frp(i, a, b) for (i = a; i <= b; i++) +#define frn(i, a, b) for (i = a; i >= b; i--) +#define CLR(a) memset(a, 0, sizeof(a)) +#define mem(a, b) memset(a, b, sizeof(a)) + +template +T bigmod(T b, T p, T m) +{ + if (!p) + { + return 1; + } + T res = (bigmod(b, p / 2, m)) % m; + res = (res * res) % m; + if (p & 1) + { + res = (res * b) % m; + } + return res % m; +} + +template +T modinv(T b, T m) +{ + return bigmod(b, m - 2, m); +} + +typedef long long LL; +typedef unsigned long long ULL; + +const LL INF = 1000000007LL; +const LL MOD = 1000000007LL; +const int MAX = 1000005; +const int sz = 100005; + +int main() +{ + int n, k, i, f, ff, fff, t, on; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + f = 0, ff = 0, fff = 0, on = 0; + for (i = 0; i < n; i++) + { + scanf("%d", &k); + if (!k) + { + f = 1; + } + if (k) + { + ff = 1; + } + if (k > 1) + { + fff = 1; + } + if (k == 1) + { + on = 1; + } + } + if (n == 1 || (f && !ff) || (!f && !on && fff) || (!f && !fff)) + { + puts("Better luck next time!"); + } + else + { + puts("Happy Birthday Tutu!"); + } + } + return 0; +} diff --git a/1289.cpp b/1289.cpp new file mode 100644 index 0000000..d74ad23 --- /dev/null +++ b/1289.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define MAXN 100500 +#define pi pair +#define y first +#define x second + +int N, M, cs, k, x[64 * 64][64], t[64 * 64], a[64], b[64], h, cnt; +vector s[64], S; +map f; + +int main() +{ + while (cin >> N) + { + S.clear(); + for (int i = 0; i < N; i += 1) + { + cin >> M; + s[i].clear(); + for (int j = 0; j < M; j += 1) + { + cin >> h; + s[i].push_back(h); + S.push_back(h); + } + } + sort(S.begin(), S.end()); + f.clear(); + cnt = 0; + for (int i = 0; i < S.size(); i += 1) + if (f.find(S[i]) == f.end()) + { + f[S[i]] = cnt++; + } + for (int i = 0; i < cnt; i += 1) + for (int j = 0; j < N; j += 1) + { + x[i][j] = 0, t[i] = 0; + } + for (int i = 0; i < N; i += 1) + for (int j = 0; j < s[i].size(); j += 1) + { + x[f[s[i][j]]][i] = 1; + } + for (int i = 0; i < cnt; i += 1) + for (int j = 0; j < N; j += 1) + { + t[i] += x[i][j]; + } + for (int i = 0; i < N; i += 1) + { + a[i] = 0; + } + k = 0; + for (int i = 0; i < cnt; i += 1) + { + k += t[i]; + int flag = 0; + for (int j = 0; j < N; j += 1) + { + b[j] = 0; + } + for (int j = 0; j < N; j += 1) + { + if (x[i][j] && a[j]) + { + flag = 1; + for (int k = 0; k < N; k += 1) + if (k != j) + { + b[k] |= (1 & x[i][k]); + } + b[j] = (t[i] == 1); + } + } + k -= flag; + for (int j = 0; j < N; j += 1) + { + a[j] = (flag) ? b[j] : x[i][j]; + } + } + cout << "Case " << ++cs << ": " << 2 * k - N - 1 << '\n'; + } + return 0; +} diff --git a/12890.cpp b/12890.cpp new file mode 100644 index 0000000..0a0d6da --- /dev/null +++ b/12890.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + map R; + int low = 0; + long long ret = 0; + for (int i = 1; i <= n; i++) + { + scanf("%d", &x); + int &y = R[x]; + if (y > low) + low = y; + y = i; + ret += i - low; + // printf("[%d %d]\n", low + 1, i); + } + printf("%lld\n", ret); + } + return 0; +} +/* +9 +3 +1 2 1 +5 +1 2 3 1 2 +4 +1 2 2 1 +*/ diff --git a/12891.cpp b/12891.cpp new file mode 100644 index 0000000..90d3878 --- /dev/null +++ b/12891.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +#define N 55 +#define EPS 1e-9 +#define INF 100000000 + +typedef long long LL; + +bool S[N], T[N]; +double cost[N][N], lx[N], ly[N], slack[N], slackx[N], prev_[N]; +int n, max_match, xy[N], yx[N]; +LL lob[N][N], hor[N][N]; + +void init_lbls() +{ + fill(lx, lx + N, 0); + fill(ly, ly + N, 0); + for (int x = 0; x < n; x++) + { + for (int y = 0; y < n; y++) + { + lx[x] = max(lx[x], cost[x][y]); + } + } +} + +void update_lbls() +{ + int x, y; + double delta = INF; + for (y = 0; y < n; y++) + { + if (!T[y]) + { + delta = min(delta, slack[y]); + } + } + for (x = 0; x < n; x++) + { + if (S[x]) + { + lx[x] -= delta; + } + } + for (y = 0; y < n; y++) + { + if (T[y]) + { + ly[y] += delta; + } + } + for (y = 0; y < n; y++) + { + if (!T[y]) + { + slack[y] -= delta; + } + } +} + +void add_to_tree(int x, int prev_x) +{ + S[x] = true; + prev_[x] = prev_x; + for (int y = 0; y < n; y++) + { + if (lx[x] + ly[y] - cost[x][y] + EPS < slack[y]) + { + slack[y] = lx[x] + ly[y] - cost[x][y]; + slackx[y] = x; + } + } +} + +void augment() +{ + if (max_match == n) + { + return; + } + int x, y, root, q[N], wr = 0, rd = 0; + memset(S, false, sizeof(S)); + memset(T, false, sizeof(T)); + memset(prev_, -1, sizeof(prev_)); + for (x = 0; x < n; x++) + if (xy[x] == -1) + { + q[wr++] = root = x; + prev_[x] = -2; + S[x] = true; + break; + } + for (y = 0; y < n; y++) + { + slack[y] = lx[root] + ly[y] - cost[root][y]; + slackx[y] = root; + } + while (true) + { + while (rd < wr) + { + x = q[rd++]; + for (y = 0; y < n; y++) + if (cost[x][y] == lx[x] + ly[y] && !T[y]) + { + if (yx[y] == -1) + { + break; + } + T[y] = true; + q[wr++] = yx[y]; + add_to_tree(yx[y], x); + } + if (y < n) + { + break; + } + } + if (y < n) + { + break; + } + update_lbls(); + wr = rd = 0; + for (y = 0; y < n; y++) + if (!T[y] && slack[y] == 0) + { + if (yx[y] == -1) + { + x = slackx[y]; + break; + } + else + { + T[y] = true; + if (!S[yx[y]]) + { + q[wr++] = yx[y]; + add_to_tree(yx[y], slackx[y]); + } + } + } + if (y < n) + { + break; + } + } + if (y < n) + { + max_match++; + for (int cx = x, cy = y, ty; cx != -2; cx = prev_[cx], cy = ty) + { + ty = xy[cx]; + yx[cy] = cx; + xy[cx] = cy; + } + augment(); + } +} + +int hungarian() +{ + max_match = 0; + memset(xy, -1, sizeof(xy)); + memset(yx, -1, sizeof(yx)); + init_lbls(); + augment(); + LL u = 1; + LL v = 1, g; + for (int x = 0; x < n; x++) + { + u = u * lob[x][xy[x]]; + v = v * hor[x][xy[x]]; + g = __gcd(u, v); + u /= g; + v /= g; + } + u = v - u; + g = __gcd(u, v); + u /= g; + v /= g; + printf("%lld/%lld\n", u, v); +} + +int main() +{ + int T, i, j; + scanf("%d", &T); + double z; + while (T--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + scanf("%d/%d", &lob[i][j], &hor[i][j]); + lob[i][j] = hor[i][j] - lob[i][j]; + z = (double)(lob[i][j]) / (double)hor[i][j]; + z = z * 10; + if (z != 0) + { + cost[i][j] = (log(z)); + } + else + { + cost[i][j] = 0; + } + } + } + hungarian(); + } + return 0; +} diff --git a/12892.cpp b/12892.cpp new file mode 100644 index 0000000..269f368 --- /dev/null +++ b/12892.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +typedef long long ll; + +#define pb push_back +#define mem(a, x) memset(a, x, sizeof a) +#define PI acos(-1) +#define all(a) a.begin(), a.end() +#define MAX 100010 +#define read(in) freopen("in.txt", "r", stdin) +#define write(out) freopen("out.txt", "w", stdout) +#define INF 10000000 +#define eps 1e - 06 +#define arraysize(a) sizeof(a) / sizeof(a[0]) +#define fori(i, n) for (int i = 0; i < n; i++) +#define ford(i, n) for (int i = n - 1; i >= 0; i--) +#define scan(n) scanf("%d", &n) +#define print(n) printf("%d\n", n) +#define tor vector + +int mod(int n, int m) +{ + if (n < 0) + { + n += (ceil(-n * 1.00 / m) * m); + } + return n % m; +} + +int tc, par[10100], p, spar[10100]; +vector adj[10100]; +int rec(int n) +{ + if (!spar[n]) + { + return par[n] = 1; + } + int sum = 0; + for (int i = 0; i < adj[n].size(); i++) + { + sum += rec(adj[n][i]); + } + par[n] += sum; + return par[n]; +} + +int main() +{ + scan(tc); + int t = 1; + while (tc--) + { + int n; + scan(n); + for (int i = 0; i <= n; i++) + { + par[i] = 1; + } + mem(spar, false); + vector root; + for (int i = 1; i <= n; i++) + { + scan(p); + spar[p] = true; + adj[p].pb(i); + if (p == 0) + { + root.pb(i); + } + } + for (int i = 0; i < root.size(); i++) + { + rec(root[i]); + } + printf("Forest#%d:", t++); + for (int i = 1; i <= n; i++) + { + int sum = par[i] - 1; + for (int j = 0; j < adj[i].size(); j++) + { + for (int k = j + 1; k < adj[i].size(); k++) + { + sum += (par[adj[i][j]] * par[adj[i][k]]); + } + } + cout << " " << sum; + } + cout << endl; + for (int i = 0; i <= n; i++) + { + adj[i].clear(); + } + } + return 0; +} diff --git a/12893.cpp b/12893.cpp new file mode 100644 index 0000000..d779883 --- /dev/null +++ b/12893.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + scanf("%*d"); + long long n; + while (scanf("%lld", &n) == 1) + { + printf("%d\n", (int)__builtin_popcountll(n)); + } + return 0; +} diff --git a/12894.cpp b/12894.cpp new file mode 100644 index 0000000..8020845 --- /dev/null +++ b/12894.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +#define eps 1e-6 +#define MAXN 131072 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + Pt operator/(const double a) const + { + return Pt(x / a, y / a); + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} + +double solve(double a, double b, double c, double d) +{ + // d^2 - x^2 = b^2 - (a - x - c)^2 + // d^2 - x^2 = b^2 - (a-c - x)^2 + // d^2 - x^2 = b^2 - ((a-c)^2 - 2(a-c)x + x^2) + // d^2 - x^2 = b^2 - (a-c)^2 + 2(a-c)x - x^2 + // d^2 - b^2 + (a-c)^2 = 2(a-c)x + double x; + x = (pow(d, 2) - pow(b, 2) + pow(a - c, 2)) / (2 * (a - c)); + return x; +} +int main() +{ + int testcase, cases = 0; + double x, y, r; + const double WIDTH = 10, HEIGHT = 6, DIAGONAL = hypot(10, 6), RD = hypot(4.5, 3); + const double RADIUS = 2, THETA = atan2(6, 10), THETA2 = atan2(3, 4.5); + scanf("%d", &testcase); + while (testcase--) + { + Pt L, R, C, mC; + scanf("%lf %lf", &x, &y); + L = Pt(x, y); + scanf("%lf %lf", &x, &y); + R = Pt(x, y); + scanf("%lf %lf", &x, &y); + C = Pt(x, y); + scanf("%lf", &r); + double d = dist(L, R) / DIAGONAL, w, h, cr; + w = d * WIDTH, h = d * HEIGHT; + cr = d * RADIUS; + Pt vAB = rotateRadian((R - L) / dist(L, R), -THETA); + mC = rotateRadian(vAB, THETA2) * d * RD + L; + if (fabs(cr - r) < eps && C == mC) + puts("YES"); + else + puts("NO"); + } + return 0; +} +/* +4 +0 0 20 12 9 6 4 +0 0 10 6 4 3 2 +1 1 21 13 10 7 4 +0 0 20 20 9 10 4 +*/ diff --git a/12895.cpp b/12895.cpp new file mode 100644 index 0000000..88afe20 --- /dev/null +++ b/12895.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int mpow(int x, int y) +{ + int ret = 1; + while (y) + { + if (y & 1) + ret *= x; + y >>= 1, x = x * x; + } + return ret; +} +int main() +{ + scanf("%*d"); + char buf[32]; + int n; + while (scanf("%d", &n) == 1) + { + sprintf(buf, "%d", n); + int m = strlen(buf), sum = 0; + for (int i = 0; i < m; i++) + sum += mpow(buf[i] - '0', m); + puts("Not Armstrong" + (sum == n ? 4 : 0)); + } + return 0; +} diff --git a/12896.cpp b/12896.cpp new file mode 100644 index 0000000..8777630 --- /dev/null +++ b/12896.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +char SMS[10][10] = {" ", + ".,?\"", "abc", "def", + "ghi", "jkl", "mno", + "pqrs", "tuv", "wxyz"}; +int main() +{ + int testcase, n, A[128], B[128]; + scanf("%d", &testcase); + while (testcase--) + { + 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]); + for (int i = 0; i < n; i++) + putchar(SMS[A[i]][B[i] - 1]); + puts(""); + } + return 0; +} diff --git a/12897.cpp b/12897.cpp new file mode 100644 index 0000000..e101203 --- /dev/null +++ b/12897.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +char s[1048576]; +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + scanf("%d", &n); + + char R[128], s1[10], s2[10]; + for (int i = 0; i < 128; i++) + R[i] = i; + for (int i = 0; i < n; i++) + { + scanf("%s %s", s1, s2); + for (int j = 'A'; j <= 'Z'; j++) + if (R[j] == s2[0]) + R[j] = s1[0]; + } + + for (int i = 0; s[i]; i++) + putchar(R[s[i]]); + puts(""); + } + return 0; +} +/* +2 +AVVU_TUMI_COLING_PARO_NAY +3 +B V +D L +H Y +AABBCCY +3 +A B +B C +C A +*/ diff --git a/12898.cpp b/12898.cpp new file mode 100644 index 0000000..8b771dd --- /dev/null +++ b/12898.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +long long f(long long u, int i) +{ + if ((u >> i) & 1) + { + return ((u >> (i + 1)) << i) + (u & ((1LL << i) - 1)) + 1; + } + else + { + return ((u >> (i + 1)) << i); + } +} +int main() +{ + int testcase, cases = 0; + long long retAND, retOR; + long long sa, sb, a, b; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld %lld", &a, &b); + retAND = retOR = 0; + for (int i = 0; i < 63; i++) + { + sb = f(b, i), sa = f(a - 1, i); + if (sb - sa > 0) + retOR |= 1LL << i; + if (sb - sa == b - a + 1) + retAND |= 1LL << i; + } + printf("Case %d: %lld %lld\n", ++cases, retOR, retAND); + } + return 0; +} diff --git a/12899.cpp b/12899.cpp new file mode 100644 index 0000000..f3147cd --- /dev/null +++ b/12899.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define LL long long + +//为自己加油O(∩_∩)O~ + +const long long mod = 21092013;//这个必须是质数 +int n; + +vector v[14111]; +int L[11111], R[11111]; +LL dp[11111][2]; +LL ans[52]; +void dfs(int pos, int fa, int g) +{ + for (int j = 0; j < v[pos].size(); j++) + { + int k = v[pos][j]; + if (k == fa) + { + continue; + } + dfs(k, pos, g); + } + int ha = 0; + for (int j = L[pos]; j <= R[pos]; j++) + if (j % g == 0) + { + ha++; + } + vector c; + c.clear(); + dp[pos][0] = dp[pos][1] = 0; + for (int j = 0; j < v[pos].size(); j++) + { + int k = v[pos][j]; + if (k == fa) + { + continue; + } + c.push_back(k); + dp[pos][0] += dp[k][1] + dp[k][0]; + dp[pos][0] %= mod; + } + if (ha > 0) + { + dp[pos][1] = 1; + for (int j = 0; j < c.size(); j++) + { + dp[pos][1] += dp[c[j]][1]; + dp[pos][1] %= mod; + } + for (int j = 0; j < c.size(); j++) + { + for (int k = j + 1; k < c.size(); k++) + { + dp[pos][0] += dp[c[j]][1] * dp[c[k]][1] * ha; + dp[pos][0] %= mod; + } + } + dp[pos][1] *= ha; + dp[pos][1] %= mod; + } +} +int main() +{ + int T; + scanf("%d", &T); + int t = T; + while (T--) + { + scanf("%d", &n); + for (int j = 1; j <= n; j++) + { + v[j].clear(); + } + for (int j = 0; j < n - 1; j++) + { + int g, h; + scanf("%d%d", &g, &h); + v[g].push_back(h); + v[h].push_back(g); + } + for (int j = 1; j <= n; j++) + { + scanf("%d", &L[j]); + } + for (int j = 1; j <= n; j++) + { + scanf("%d", &R[j]); + } + memset(ans, 0, sizeof(ans)); + for (int j = 50; j >= 1; j--) + { + dfs(1, 0, j); + ans[j] = dp[1][0] + dp[1][1]; + ans[j] %= mod; + for (int k = j + j; k <= 50; k += j) + { + ans[j] = (mod + ans[j] - ans[k]) % mod; + } + } + printf("Case %d:\n", t - T); + for (int j = 1; j <= 50; j++) + { + printf("%d: %lld\n", j, (ans[j] + mod) % mod); + } + } + return 0; +} diff --git a/129.cpp b/129.cpp new file mode 100644 index 0000000..bbf61e6 --- /dev/null +++ b/129.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define MAX 40000 +char str[MAX]; +int n, l; +int last; + +bool check() +{ + int length; + int i; + int half = (last + 1) / 2; + for (length = 1; length <= half; length++) + { + for (i = 0; i < length; i++) + { + if (str[last - i] != str[last - length - i]) + { + break; + } + } + if (i == length) + { + return false; + } + } + return true; +} + +void gen() +{ + int ct = 1; + str[0] = 0;// 'A' + str[1] = 1;// 'B' + last = 0; + while (ct < n) + { + // gen next + if (check()) + { + // extend + ct++; + str[++last] = 0; + } + else + { + // do baktrk + while (1) + { + for (; str[last] == l; last--) + ; + str[last]++; + if (str[last] != l) + { + break; + } + } + } + } + + while (!check()) + { + while (1) + { + for (; str[last] == l; last--) + ; + str[last]++; + if (str[last] != l) + { + break; + } + } + } +} + +int main() +{ + int i; + while (1) + { + scanf("%d %d", &n, &l); + if (n == 0) + { + break; + } + gen(); + int ct = 0; + printf("%c", str[0] + 'A'); + for (i = 1; i <= last; i++) + { + if (i % 4 == 0) + { + ct++; + if (ct % 16 == 0) + { + printf("\n"); + } + else + { + printf(" "); + } + } + printf("%c", str[i] + 'A'); + } + printf("\n%d\n", last + 1); + } + return 0; +} diff --git a/1290.cpp b/1290.cpp new file mode 100644 index 0000000..fdb352b --- /dev/null +++ b/1290.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +#define x first +#define y second +#define mp make_pair +#define pb push_back +#define REP(i, l, r) for ((i) = (l); (i) <= (r); ++(i)) +#define REP2(i, l, r) for ((i) = (l); (i) != (r); ++(i)) + +typedef long long LL; +typedef double ld; + +const int MAX = 100000 + 10; + +int n, m; +LL a[MAX], b[MAX], ta[MAX], tb[MAX]; + +int check(int now) +{ + while (n && m) + { + if (now == 1) + { + if (a[n] > b[m]) + { + --m; + } + else + { + --n; + a[n] += a[n + 1]; + } + } + else + { + if (b[m] > a[n]) + { + --n; + } + else + { + --m; + b[m] += b[m + 1]; + } + } + now ^= 1; + } + if (n) + { + return 1; + } + else + { + return 0; + } +} + +int main() +{ +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); + freopen("output.txt", "w", stdout); +#endif + int Test = 0; + while (cin >> n >> m) + { + int i; + REP(i, 1, n) + cin >> a[i]; + sort(a + 1, a + n + 1); + ta[0] = n; + REP(i, 1, n) + ta[i] = a[i]; + REP(i, 1, m) + cin >> b[i]; + sort(b + 1, b + m + 1); + tb[0] = m; + REP(i, 1, m) + tb[i] = b[i]; + int flag = 0; + if (a[n] > b[m]) + { + --m; + flag |= check(0); + } + n = ta[0]; + REP(i, 1, n) + a[i] = ta[i]; + m = tb[0]; + REP(i, 1, m) + b[i] = tb[i]; + --n; + a[n] += a[n + 1]; + flag |= check(0); + printf("Case %d: ", ++Test); + if (flag) + { + cout << "Takeover Incorporated" << endl; + } + else + { + cout << "Buyout Limited" << endl; + } + } + return 0; +} diff --git a/12900.cpp b/12900.cpp new file mode 100644 index 0000000..a44f967 --- /dev/null +++ b/12900.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define NN 220 + +int dist[NN][NN]; +int val[NN], valvst[NN]; +int cost[NN][NN], cap[NN][NN]; +int flow[NN][NN], mind[NN], vst[NN]; +int prev_[NN], minf[NN]; + +int minCostFlow(int N, int src, int tar) +{ + int i, j, u, v; + memset(flow, 0, sizeof(flow)); + queue q; + int res = 0; + while (true) + { + memset(mind, 0x3f, sizeof(mind)); + memset(prev_, -1, sizeof(prev_)); + memset(vst, 0, sizeof(vst)); + mind[src] = 0; + q = queue(); + q.push(src); + vst[src] = 1; + minf[src] = INF; + while (q.size() > 0) + { + u = q.front(); + q.pop(); + vst[u] = 0; + for (v = 0; v < N; v++) + { + if (cap[u][v] == 0) + continue; + if (mind[v] > mind[u] + cost[u][v]) + { + mind[v] = mind[u] + cost[u][v]; + minf[v] = min(minf[u], cap[u][v]); + prev_[v] = u; + if (vst[v] == 1) + continue; + q.push(v); + vst[v] = 1; + } + } + } + if (prev_[tar] == -1) + return res; + res += mind[tar] * minf[tar]; + for (i = tar; i != src; i = prev_[i]) + { + j = prev_[i]; + cap[j][i] -= minf[tar]; + flow[j][i] += minf[tar]; + cap[i][j] += minf[tar]; + flow[i][j] -= minf[tar]; + } + } +} +int main() +{ + int T, N, M, x, y, i, j, k, Lw, Sw, Tw, u; + int n; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + printf("Case %d: ", cas); + scanf("%d%d%d%d", &N, &M, &Sw, &Tw); + n = N + M; + memset(dist, 0x3f, sizeof(dist)); + memset(cost, 0, sizeof(cost)); + memset(cap, 0, sizeof(cap)); + int add = 0; + for (int x = 0; x < N; ++x) + { + for (int y = 0; y < M; ++y) + { + char s[100]; + scanf("%s", s); + if (s[0] == 'X') + continue; + int Lw; + sscanf(s, "%d", &Lw); + dist[y + N][x] = Lw - Sw;// L + P - Q >= S + dist[x][y + N] = Tw - Lw;// L + P - Q <= T + cap[n][x]++; + cap[y + N][n + 1]++; + add += Lw; + } + } + for (k = 0; k < n; k++) + { + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (dist[i][j] > dist[i][k] + dist[k][j]) + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + for (k = 0; k < n; k++) + if (dist[k][k] < 0) + break; + if (k < n) + { + puts("Impossible"); + continue; + } + + for (i = 0; i < N; i++) + for (j = 0; j < M; j++) + { + cost[i][j + N] = dist[i][j + N]; + cost[j + N][i] = -dist[i][j + N]; + cap[i][j + N] = INF; + } + + int res = minCostFlow(n + 2, n, n + 1); + + for (i = 0; i < N; i++) + for (j = 0; j < M; j++) + { + if (flow[i][j + N]) + dist[j + N][i] = -dist[i][j + N]; + } + for (k = 0; k < n; k++) + { + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (dist[i][j] > dist[i][k] + dist[k][j]) + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + + memset(valvst, 0, sizeof(valvst)); + for (i = 0; i < n; i++) + if (!valvst[i]) + { + val[i] = 0; + valvst[i] = 1; + for (j = i + 1; j < n; j++) + if (!valvst[j]) + { + if (dist[j][i] < -INF / 2 || dist[j][i] > INF / 2) + continue; + val[j] = dist[j][i]; + valvst[j] = 1; + } + } + int minval = 0; + for (i = 0; i < n; i++) + if (val[i] < minval) + minval = val[i]; + for (i = 0; i < n; i++) + val[i] -= minval; + printf("%d\n", res + add); + for (i = 0; i < N; i++) + { + if (i) + printf(" "); + printf("%d", val[i]); + } + puts(""); + for (i = N; i < n; i++) + { + if (i > N) + printf(" "); + printf("%d", val[i]); + } + puts(""); + } +} diff --git a/12901.cpp b/12901.cpp new file mode 100644 index 0000000..d0b69e4 --- /dev/null +++ b/12901.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +typedef long long ll; + +#define INT (0x3f3f3f3f) * 2 +#define LL (0x3f3f3f3f3f3f3f3f) * 2 + +inline int read() +{ + int x = 0, f = 1; + char ch = getchar(); + while (ch > '9' || ch < '0') + { + if (ch == '-') + { + f = -1; + } + ch = getchar(); + } + while (ch >= '0' && ch <= '9') + { + x = x * 10 + ch - '0'; + ch = getchar(); + } + return x * f; +} +const double eps = 1e-9; +inline int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else if (x > 0) + { + return 1; + } + else + { + return -1; + } +} +struct Point +{ + double x, y; + Point(){}; + Point(double xx, double yy) + { + x = xx, y = yy; + } +}; +typedef Point Vector; +Vector operator+(const Vector a, const Vector b) +{ + return Vector(a.x + b.x, a.y + b.y); +} +Vector operator-(const Vector a, const Vector b) +{ + return Vector(a.x - b.x, a.y - b.y); +} +double operator*(const Vector a, const Vector b) +{ + return a.x * b.x + a.y * b.y; +} +double Length(Vector v) +{ + return sqrt(v.x * v.x + v.y * v.y); +} +double angle(Vector a, Vector b) +{ + return acos((a * b) / (Length(a) * Length(b))); +} +double W, H, x, xe, ye, u; +int main() +{ + //freopen("/Users/peteryuanpan/data.txt","r",stdin); + int T = read(); + for (int kase = 1; kase <= T; kase++) + { + W = read(), H = read(), x = read(), xe = read(), ye = read(); + scanf("%lf", &u); + Vector AE = Point(xe, ye) - Point(W, H); + Vector V = Vector(0, 1); + double a = angle(AE, V); + double tana = (W - xe) / (H - ye); + double b = asin(sin(a) / u); + double h = (W - x - H * tana) / (tan(b) - tana); + if ((h - H) > (3e-6)) + { + printf("Impossible\n"); + } + else + { + printf("%.4f\n", h); + } + } + return 0; +} diff --git a/12902.cpp b/12902.cpp new file mode 100644 index 0000000..c812063 --- /dev/null +++ b/12902.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +int sum[3000100]; +int cnt[3000100]; + +int solve(string s) +{ + int ans = 0; + int a = 0, b = 0; + string A = "", B = ""; + for (int i = 0; i < s.length(); i++) + { + if (s[i] == 'a') + { + a++; + } + else + { + b++; + } + } + if (a - 1 > b) + { + for (int i = 0; i < a - 1 - b; i++) + { + B += '+'; + } + ans += a - 1 - b; + } + else if (a - 1 < b) + { + for (int i = 0; i < b - a + 1; i++) + { + A += 'a'; + } + ans += b - a + 1; + } + s = A + s + B; + if (s[0] == '+') + { + s = 'a' + s + '+'; + ans += 2; + } + sum[0] = 0; + int l = s.length(); + for (int i = 1; i < l; i++) + { + if (s[i] == 'a') + { + sum[i] = sum[i - 1] + 1; + } + else + { + sum[i] = sum[i - 1] - 1; + } + if (sum[i] < 0) + { + cnt[-sum[i]]++; + } + } + int d = 0; + int pv = 1; + for (int i = 1; i < l; i++) + { + if (s[i] == '+' && sum[i] + d < 0) + { + if (i + 1 < l && sum[i] + d == -1 && sum[i + 1] + d == 0 && cnt[pv] == 1) + { + ans++; + cnt[pv]--; + } + else + { + ans += 2; + d++; + pv++; + } + } + } + for (int i = 0; i < l; i++) + if (sum[i] < 0) + { + cnt[-sum[i]] = 0; + } + return ans; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int kase = 1; kase <= T; kase++) + { + string s; + cin >> s; + int ans = solve(s); + if (s[0] == '+') + { + int d = 0; + for (int i = 1; i < s.length(); i++) + { + if (s[i] == 'a') + { + d = i; + break; + } + } + if (d) + { + swap(s[0], s[d]); + ans = min(ans, solve(s) + d); + } + } + cout << "Case " << kase << ": " << ans << endl; + } + return 0; +} diff --git a/12903.cpp b/12903.cpp new file mode 100644 index 0000000..39a4e34 --- /dev/null +++ b/12903.cpp @@ -0,0 +1,219 @@ +#include +using namespace std; +typedef long long LL; +typedef pair PII; +const int MAXN = 2001, MOD = 1e9 + 7; + +template +struct Mint +{ + int x; + Mint() : x(0) {} + Mint(int y) + { + x = y % M; + if (x < 0) + x += M; + } + Mint(long long y) + { + x = y % M; + if (x < 0) + x += M; + } + int get() const { return x; } + bool operator==(const Mint &r) const { return x == r.x; } + Mint &operator+=(const Mint &r) + { + if ((x += r.x) >= M) + x -= M; + return *this; + } + Mint &operator-=(const Mint &r) + { + if ((x += M - r.x) >= M) + x -= M; + return *this; + } + Mint &operator*=(const Mint &r) + { + x = (long long)x * r.x % M; + return *this; + } + Mint &operator/=(const Mint &r) + { + x = (long long)x * r.inv().x % M; + return *this; + } + Mint operator+(const Mint &r) const { return Mint(*this) += r; } + Mint operator-(const Mint &r) const { return Mint(*this) -= r; } + Mint operator*(const Mint &r) const { return Mint(*this) *= r; } + Mint operator/(const Mint &r) const { return Mint(*this) /= r; } + Mint inv() const + { + int a = x, b = M, u = 1, v = 0; + while (b) + { + int t = a / b; + a -= t * b; + std::swap(a, b); + u -= t * v; + std::swap(u, v); + } + if (u < 0) + u += M; + Mint res; + res.x = (unsigned)u; + return res; + } +}; + +typedef Mint mint; + +mint f[MAXN][MAXN][16], g[MAXN][MAXN][16]; +mint C[MAXN][MAXN], fac[MAXN], ret[MAXN]; + +void init(int n) +{ + fac[0] = 1; + for (int i = 1; i < n; ++i) + fac[i] = fac[i - 1] * i; + for (int i = 0; i < n; ++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]; + } + } + f[0][0][0] = 1; + for (int i = 0; i < n - 1; ++i) + { + for (int j = 0; j <= i; ++j) + { + for (int msk = 0; msk < 16; ++msk) + if (f[i][j][msk].get()) + { + int sp = msk >> 3, sc = msk >> 2 & 1; + int sf = msk >> 1 & 1, sl = msk & 1; + mint &add = f[i][j][msk]; + if (sp == 0) + {// put i in prev + if (i == 0) + f[i + 1][j + 1][sc << 3 | 1] += add; + else if (i == 1) + f[i + 1][j + 1][(sc << 3) | 2 | sl] += add; + else + f[i + 1][j + 1][(sc << 3) | (sf << 1) | sl] += add; + } + if (sc == 0) + {// put i in curr + if (i == 0) + f[i + 1][j + 1][8 | 2] += add; + else + f[i + 1][j + 1][8 | (sf << 1) | sl] += add; + } + // put i in next + f[i + 1][j + 1][(sc << 3) | 4 | (sf << 1) | sl] += add; + // don't put + f[i + 1][j][(sc << 3) | (sf << 1) | sl] += add; + } + } + } +} + +void solve(int n) +{ + for (int i = n - 2; i <= n; ++i) + { + for (int j = 0; j <= i; ++j) + { + for (int msk = 0; msk < 16; ++msk) + { + if (i == n - 2) + g[i][j][msk] = f[i][j][msk]; + else + g[i][j][msk] = 0; + } + } + } + for (int i = n - 2; i < n; ++i) + { + for (int j = 0; j <= i; ++j) + { + for (int msk = 0; msk < 16; ++msk) + if (g[i][j][msk].get()) + { + int sp = msk >> 3, sc = msk >> 2 & 1; + int sf = msk >> 1 & 1, sl = msk & 1; + mint &add = g[i][j][msk]; + if (sp == 0) + {// put i in prev + if (i == n - 2) + g[i + 1][j + 1][(sc << 3) | (sl << 2) | (sf << 1) | sl] += add; + else + g[i + 1][j + 1][(sc << 3) | (sf << 1) | sl] += add; + } + if (sc == 0) + {// put i in curr + if (i == n - 2) + g[i + 1][j + 1][8 | (sl << 2) | (sf << 1) | sl] += add; + else + g[i + 1][j + 1][8 | (sf << 1) | sl] += add; + } + // put i in next + if (i == n - 1 && !sf) + g[i + 1][j + 1][15] += add; + else if (i == n - 2 && !sl) + g[i + 1][j + 1][(sc << 3) | 4 | (sf << 1) | 1] += add; + // dont put + if (i == n - 2) + g[i + 1][j][(sc << 3) | (sl << 2) | (sf << 1) | sl] += add; + else + g[i + 1][j][(sc << 3) | (sf << 1) | sl] += add; + } + } + } +} + +int main() +{ + init(MAXN); + int T; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + int n, k; + scanf("%d%d", &n, &k); + printf("Case %d: ", cas); + if (n == 3) + { + puts("6"); + continue; + }; + solve(n); + for (int i = 0; i <= n; ++i) + { + ret[i] = 0; + for (int msk = 0; msk < 16; ++msk) + { + ret[i] += g[n][i][msk]; + } + ret[i] *= fac[n - i]; + } + mint ans(0); + for (int i = n; i >= k; --i) + { + ans += ret[i]; + for (int j = i + 1; j <= n; ++j) + { + if ((j - i) & 1) + ans -= C[j][j - i] * ret[j]; + else + ans += C[j][j - i] * ret[j]; + } + } + printf("%d\n", ans.get()); + } + return 0; +} \ No newline at end of file diff --git a/12904.cpp b/12904.cpp new file mode 100644 index 0000000..9bd0589 --- /dev/null +++ b/12904.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define FRE(i, a, b) for (i = a; i <= b; i++) +#define FRL(i, a, b) for (i = a; i < b; i++) +#define mem(t, v) memset((t), v, sizeof(t)) +#define sqr(x) (x) * (x) +#define all(x) x.begin(), x.end() +#define un(x) x.erase(unique(all(x)), x.end()) +#define sf(n) scanf("%d", &n) +#define sff(a, b) scanf("%d %d", &a, &b) +#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c) +#define D(x) cout << #x " = " << (x) << endl +#define pf printf +#define VI vector +#define pii pair +#define pll pair +#define pb push_back +#define mp make_pair +#define pi acos(-1.00) +#define DBG pf("Hi\n") +#define sz size() +#define ins insert +#define fi first +#define se second +#define xx first +#define yy second +#define inf (1 << 29) +#define MOD 100007 +#define eps 1e-9 +#define MAX 100000 + +typedef long long int LL; +typedef double db; + +int c[200], A[MAX + 10]; + +int main() +{ + int i, j, k, cs, t; + int a, b, d, q, w, e, r, y; + db nw, mn, n; + sf(t); + FRE(cs, 1, t) + { + mn = inf; + sf(y); + n = y; + mem(c, 0); + FRE(i, 1, n) + { + sf(A[i]); + c[A[i]]++; + } + FRE(i, 1, 160) + c[i] += c[i - 1]; + FRE(i, 0, 160) + { + FRE(j, i + 1, 160) + { + FRE(k, j + 1, 160) + { + a = c[i]; + b = c[j] - c[i]; + r = c[k] - c[j]; + d = c[160] - c[k]; + nw = fabs((n / 4.00) - a) + fabs((n / 4.00) - b) + fabs((n / 4.00) - r) + fabs((n / 4.00) - d); + if (nw < mn) + { + mn = nw; + q = i; + w = j; + e = k; + } + } + } + } + pf("Case %d: %d %d %d\n", cs, q, w, e); + } + return 0; +} diff --git a/12905.cpp b/12905.cpp new file mode 100644 index 0000000..9b2d7a0 --- /dev/null +++ b/12905.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +const double pi = acos(-1); +double P[32], Q[32]; + +#define eps 1e-6 + +void integral(double Q[]) +{ + for (int i = 31; i >= 1; i--) + Q[i] = Q[i - 1] / (i); + Q[0] = 0; +} + +double calcVal(double Q[], double x) +{ + double ret = 0; + for (int i = 31; i >= 0; i--) + ret = ret * x + Q[i]; + return ret; +} + +int main() +{ + int testcase, cases = 0; + int n, slices, stacks; + double a, b; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + + memset(P, 0, sizeof(P)); + memset(Q, 0, sizeof(Q)); + for (int i = n; i >= 0; i--) + scanf("%lf", &P[i]); + + scanf("%lf %lf", &a, &b); + scanf("%d %d", &slices, &stacks); + + for (int i = 0; i <= n; i++) + for (int j = 0; j <= n; j++) + Q[i + j] += P[i] * P[j]; + integral(Q); + double trueVal = (calcVal(Q, b) - calcVal(Q, a)) * pi; + double apprVal = 0; + double dx = (b - a) / stacks, dtheta = 2 * pi / slices; + for (double x = a; x + dx - eps <= b; x += dx) + { + double x1 = x, x2 = x + dx, x12; + double fx1, fx2, S1, S2, fx12, S12; + fx1 = calcVal(P, x1); + fx2 = calcVal(P, x2); + S1 = fx1 * fx1 * sin(dtheta) / 2; + S2 = fx2 * fx2 * sin(dtheta) / 2; + apprVal += dx * (S1 + S2 + sqrt(S1 * S2)) / 3 * slices; + } + printf("Case %d: %.4lf\n", ++cases, fabs(trueVal - apprVal) * 100 / trueVal); + } + return 0; +} diff --git a/12906.cpp b/12906.cpp new file mode 100644 index 0000000..e4e8214 --- /dev/null +++ b/12906.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define FRE(i, a, b) for (i = a; i <= b; i++) +#define FRL(i, a, b) for (i = a; i < b; i++) +#define mem(t, v) memset((t), v, sizeof(t)) +#define sqr(x) (x) * (x) +#define all(x) x.begin(), x.end() +#define un(x) x.erase(unique(all(x)), x.end()) +#define sf(n) scanf("%d", &n) +#define sff(a, b) scanf("%d%d", &a, &b) +#define sfff(a, b, c) scanf("%d%d%d", &a, &b, &c) +#define D(x) cout << #x " = " << (x) << endl +#define pf printf +#define VI vector +#define pii pair +#define pll pair +#define pb push_back +#define mp make_pair +#define pi acos(-1.00) +#define DBG pf("Hi\n") +#define sz size() +#define ins insert +#define fi first +#define se second +#define xx first +#define yy second +#define inf (1 << 29) +#define MOD 1000000007 +#define eps 1e-9 +#define MAX 100000 + +typedef long long int LL; +typedef double db; + +unsigned long long int cnt[MAX + 10]; + +int main() +{ + int i, j, k, t, cs, v, f, p; + unsigned long long res, F; + LL w, mx; + sf(t); + FRE(cs, 1, t) + { + mem(cnt, 0); + res = F = mx = 0; + w = 1; + sf(p); + FRE(i, 1, p) + { + sff(v, f); + cnt[v] = f; + mx = max(mx, (LL)v); + } + FRE(i, 1, MAX) + { + F += cnt[i]; + res += F * cnt[i]; + if (i == mx) + { + break; + } + w = w * (cnt[i] + 1); + if (w >= MOD) + { + w %= MOD; + } + } + pf("Case %d: %llu %d\n", cs, res, w); + } + return 0; +} diff --git a/12907.cpp b/12907.cpp new file mode 100644 index 0000000..f23adf9 --- /dev/null +++ b/12907.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +typedef pair PII; + +const int MAXN = 10000 + 10; + +vector G[MAXN]; +map mp; +int vis[MAXN], N, M, R, sum; +int fa[MAXN], cost[MAXN]; + +void dfs(int u, int f = -1) +{ + fa[u] = f; + vis[u] = true; + for (auto &x : G[u]) + if (!vis[x.first]) + { + cost[x.first] = x.second; + sum += x.second; + mp[PII(u, x.first)] = x.second; + dfs(x.first, u); + } +} + +bool is_connect() +{ + for (int i = 0; i < N; ++i) + { + if (!vis[i]) + return false; + } + return true; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int _ = 0; _ < T; ++_) + { + scanf("%d%d%d", &N, &M, &R); + for (int i = 0; i < N; ++i) + { + G[i].clear(); + fa[i] = -1; + vis[i] = 0; + cost[i] = 0; + } + static int u[MAXN], v[MAXN], w[MAXN]; + for (int i = 0; i < M; ++i) + { + scanf("%d%d%d", u + i, v + i, w + i); + G[u[i]].push_back(PII(v[i], w[i])); + } + sum = 0; + mp.clear(); + dfs(R); + if (M < N - 1 || !is_connect()) + puts("impossible"); + else + { + if (M == N) + { + for (int i = 0; i < M; ++i) + { + if (mp.count(PII(u[i], v[i]))) + { + mp[PII(u[i], v[i])] = max(mp[PII(u[i], v[i])], w[i]); + } + else + { + if (w[i] < cost[v[i]]) + { + sum += w[i] - cost[v[i]]; + mp.erase(PII(fa[v[i]], v[i])); + mp[PII(u[i], v[i])] = w[i]; + } + break; + } + } + } + printf("%d\n", sum); + for (auto &x : mp) + { + printf("%d %d %d\n", x.first.first, x.first.second, x.second); + } + } + } + return 0; +} diff --git a/12908.cpp b/12908.cpp new file mode 100644 index 0000000..8207514 --- /dev/null +++ b/12908.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int i, j, c, n = 2, sum; + double p; + while (n != 0) + { + scanf("%d", &n); + if (n == 0) + { + return 0; + } + sum = 0; + c = 1; + p = (sqrt(1 + 8 * (n + 1)) - 1) / 2.0; + sum = ceil(p); + printf("%d %d\n", sum * (sum + 1) / 2 - n, sum); + } + return 0; +} diff --git a/12909.cpp b/12909.cpp new file mode 100644 index 0000000..f3f0407 --- /dev/null +++ b/12909.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +typedef long long LL; + +LL a[100]; + +int main() +{ + a[0]=0, a[1]=1; + for (int i = 2; i < 23; ++i) + { + a[i] = a[i - 1] * 6 - a[i - 2] + 2; + } + for (LL n; scanf("%lld", &n), n;) + { + int ret = 0; + for (int i = 2; i < 23; ++i) + { + ret += a[i] <= n; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1291.cpp b/1291.cpp new file mode 100644 index 0000000..07894cf --- /dev/null +++ b/1291.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +#define MX 100005 +#define REP(i, n) for (int i = 0; i < (n); i++) +#define OREP(i, n) for (int i = 1; i <= (n); i++) + +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int UINT; + +int n, m, k, t; + +int dp[2][5][5]; + +int d[5][5] = {{1, 2, 2, 2, 2}, + {2, 1, 3, 4, 3}, + {2, 3, 1, 3, 4}, + {2, 4, 3, 1, 3}, + {2, 3, 4, 3, 1}}; +void go(int i, int j, int a, int now) +{ + int rev = (now ^ 1); + int c = dp[rev][i][j]; + if (c == -1) + { + return; + } + int K = c + d[a][i]; + if (dp[now][a][j] == -1 || dp[now][a][j] > K) + { + dp[now][a][j] = K; + } + K = c + d[a][j]; + if (dp[now][i][a] == -1 || dp[now][i][a] > K) + { + dp[now][i][a] = K; + } +} +void go(int &now, int a) +{ + now ^= 1; + memset(dp[now], -1, sizeof dp[now]); + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + go(i, j, a, now); + } + } +} +int main() +{ + int a; + while (scanf("%d", &a) && a) + { + memset(dp[0], -1, sizeof dp[0]); + dp[0][0][0] = 0; + int now = 0; + go(now, a); + while (scanf("%d", &a) && a) + { + go(now, a); + } + int mn = 9999999; + for (int i = 0; i < 5; i++) + for (int j = 0; j < 5; j++) + if (dp[now][i][j] != -1) + { + mn = min(mn, dp[now][i][j]); + } + printf("%d\n", mn); + } + return 0; +} diff --git a/12910.cpp b/12910.cpp new file mode 100644 index 0000000..41c2a4b --- /dev/null +++ b/12910.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +typedef double flt; + +const int MAXN = 200; +const flt eps = 1e-8; + +flt a[MAXN][MAXN], b[MAXN]; +int w, h, s; + +bool gauss(flt a[][MAXN], flt b[], int n) +{ + for (int k = 0, i, j, s; k < n; ++k) + { + for (s = k, i = k + 1; i < n; ++i) + if (fabs(a[i][k]) > fabs(a[s][k])) + s = i; + if (fabs(a[s][k]) < eps) + return false; + if (s != k) + { + swap(b[s], b[k]); + for (i = k; i < n; ++i) + swap(a[s][i], a[k][i]); + } + for (j = k + 1; j < n; ++j) + { + flt t = -a[j][k] / a[k][k]; + b[j] += b[k] * t; + for (i = k + 1; i < n; ++i) + a[j][i] += t * a[k][i]; + } + } + for (int i = n - 1; i >= 0; --i) + { + b[i] /= a[i][i]; + for (int j = 0; j < i; ++j) + b[j] -= a[j][i] * b[i]; + } + return 1; +} + +int main() +{ + while (scanf("%d%d%d", &w, &h, &s) == 3) + { + int n = w * h; + memset(a, 0, sizeof(a)); + map mp; + for (int i = 0; i < n; ++i) + b[i] = a[i][i] = 1; + for (int i = 0; i < s; ++i) + { + int u, v; + scanf("%d%d", &u, &v); + --u, --v; + mp[u] = v; + } + for (int i = 0; i < n - 1; ++i) + { + for (int d = 1; d <= 6; ++d) + { + int j = i + d; + if (j > n - 1) + j = n * 2 - 2 - j; + if (mp.count(j)) + j = mp[j]; + //cout << i << "->" << j << endl; + a[i][j] -= 1.0 / 6; + } + } + gauss(a, b, n); + printf("%.10f\n", b[0] - 1); + } + return 0; +} diff --git a/12911.cpp b/12911.cpp new file mode 100644 index 0000000..1c5cfd2 --- /dev/null +++ b/12911.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +const unsigned long long MOD = 1000007; +unsigned long hash_(unsigned long long x) +{ + return (x * x) % MOD; +} +map dp[MOD]; +int n; +long long m, A[64], sum[1 << 20]; + +void meetInMiddle() +{ + sort(A, A + n); + // D&C, dp, bitmask + long long ret = 0; + int div1 = n / 2, div2 = n - n / 2; + + for (int i = 0; i < MOD; i++) + dp[i].clear(); + + for (int i = 0; i < div1; i++) + sum[1 << i] = A[i]; + for (int i = 0; i < (1 << div1); i++) + { + sum[i] = sum[i - ((i - 1) & i)] + sum[(i - 1) & i]; + long long x = sum[i]; + dp[hash_(x)][x]++; + } + for (int i = 0; i < div2; i++) + sum[1 << i] = A[i + div1]; + for (int i = 0; i < (1 << div2); i++) + { + sum[i] = sum[i - ((i - 1) & i)] + sum[(i - 1) & i]; + long long x = sum[i]; + if (dp[hash_(m - x)].count(m - x)) + ret += dp[hash_(m - x)][m - x]; + } + if (m == 0) + ret--; + printf("%lld\n", ret); +} + +int main() +{ + while (scanf("%d %lld", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + { + scanf("%lld", &A[i]); + } + meetInMiddle(); + } + return 0; +} diff --git a/12912.cpp b/12912.cpp new file mode 100644 index 0000000..aedc029 --- /dev/null +++ b/12912.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define mid ((l + r) >> 1) +#define lson (rt << 1) +#define rson (rt << 1 | 1) + +const int MAXN = 1000000 + 10; + +int T[MAXN << 2]; + +void build(int rt, int l, int r) +{ + T[rt] = r - l + 1; + if (l == r) + return; + build(lson, l, mid); + build(rson, mid + 1, r); +} + +int query(int rt, int l, int r, int k) +{ + T[rt]--; + if (l == r) + return l; + if (k <= T[lson]) + return query(lson, l, mid, k); + else + return query(rson, mid + 1, r, k - T[lson]); +} + +int main() +{ + int N, D, K; + while (scanf("%d%d", &N, &D) == 2 && N) + { + build(1, 1, N); + K = D; + int pos, sum; + for (int i = 1; i <= N; i++) + { + pos = query(1, 1, N, K); + if (i == N) + break; + sum = T[1]; + D = -D; + if (D > 0) + K = (K - 1 + D - 1) % sum; + else + K = (K + sum + D + 1) % sum; + K = (K + sum) % sum; + if (K == 0) + K = sum; + } + printf("%d\n", pos); + } + return 0; +} diff --git a/12913.cpp b/12913.cpp new file mode 100644 index 0000000..b7074eb --- /dev/null +++ b/12913.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +typedef long long LL; +const int P = 1e9 + 7, MAXN = 1e6 + 10; +int rv[MAXN], f[MAXN], g[MAXN]; +LL pm(LL a, LL n, LL m) +{ + LL r(1); + for (; n; n >>= 1) + { + if (n & 1) + r = r * a % m; + a = a * a % m; + } + return r; +} + +int main() +{ + f[0] = f[1] = 1; + g[0] = g[1] = 1; + rv[1] = 1; + rv[0] = 0; + for (int i = 2; i < MAXN; ++i) + { + rv[i] = rv[P % i] * LL(P - P / i) % P; + f[i] = (LL)i * f[i - 1] % P; + g[i] = (LL)rv[i] * g[i - 1] % P; + } + int n, k; + while (scanf("%d%d", &n, &k) == 2) + { + int f0 = pm(2, (pm(2, n, P - 1) - n + P - 1) % (P - 1), P); + int c = (LL)f[n] * g[n - k] % P * g[k] % P; + printf("%lld\n", (LL)c * f0 % P); + } + return 0; +} diff --git a/12914.cpp b/12914.cpp new file mode 100644 index 0000000..17c9843 --- /dev/null +++ b/12914.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +typedef long long LL; + +const int P = 1711276033, MAXN = 300000 + 10, G = 29; + +LL dp[MAXN], f[MAXN], g[MAXN], h[MAXN]; + +LL pm(LL a, LL n, LL P) +{ + LL r = 1; + for (; n; n >>= 1) + { + if (n & 1) + r = r * a % P; + a = a * a % P; + } + return r; +} + +void NTT(LL a[], int n, bool inv = false) +{ + LL w = 1, d = pm(G, (P - 1) / n, P), t; + int i, j, c, s; + if (inv) + { + for (i = 1, j = n - 1; i < j; swap(a[i++], a[j--])) + ; + for (t = pm(n, P - 2, P), i = 0; i < n; ++i) + a[i] = a[i] * t % P; + } + for (s = n >> 1; s; s >>= w = 1, d = d * d % P) + { + for (c = 0; c < s; ++c, w = w * d % P) + { + for (i = c; i < n; i += s << 1) + { + a[i | s] = (a[i] + P - (t = a[i | s])) * w % P; + a[i] = (a[i] + t) % P; + } + } + } + for (i = 1; i < n; ++i) + { + for (j = 0, s = i, c = n >> 1; c; c >>= 1, s >>= 1) + j = j << 1 | s & 1; + if (i < j) + swap(a[i], a[j]); + } +} + +int main() +{ + for (int n; scanf("%d", &n) == 1;) + { + int s = 1; + while (s < n * 2) + s <<= 1; + f[0] = 1; + g[0] = 1; + for (int i = 1; i <= n; ++i) + { + f[i] = f[i - 1] * i % P; + g[i] = pm(f[i], P - 2, P); + } + for (int i = 0; i < n; ++i) + { + h[i] = i * f[n - i - 1] % P; + } + for (int i = n; i < s; ++i) + h[i] = g[i] = 0; + NTT(g, s); + NTT(h, s); + for (int i = 0; i < s; ++i) + h[i] = h[i] * g[i] % P; + NTT(h, s, 1); + for (int i = 1; i < n; ++i) + { + dp[i] = (LL)i * (i + 1) / 2 % P * f[i - 1] % P; + dp[i] = dp[i] * (n - i) % P * h[i] % P; + } + dp[n] = (LL)n * (n + 1) / 2 % P * f[n] % P; + for (int i = 1; i <= n; ++i) + { + printf("%lld%c", dp[i], " \n"[i == n]); + } + } + return 0; +} diff --git a/12915.cpp b/12915.cpp new file mode 100644 index 0000000..36d7453 --- /dev/null +++ b/12915.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +const int MAXN = 10000 + 10, inf = 1e9; + +int f[2][MAXN], a[MAXN], s[MAXN], p[2][MAXN], N, M; + +int W(int x, int y) +{ + int t = (x + y) >> 1; + return s[y] - s[t] - (y - t) * a[t] + (t - x) * a[t] - (s[t - 1] - s[x - 1]); +} + +void solve() +{ + for (int i = 1; i <= N; ++i) + { + f[0][i] = W(1, i); + p[0][i] = 0; + } + int t = 0; + for (int c(2); c <= M; ++c) + { + p[t ^ 1][N + 1] = N; + for (int i = N; i; --i) + { + int tmp = inf, k; + for (int j = p[t][i]; j <= p[t ^ 1][i + 1]; ++j) + { + if (f[t][j] + W(j + 1, i) < tmp) + tmp = f[t][k = j] + W(j + 1, i); + } + f[t ^ 1][i] = tmp; + p[t ^ 1][i] = k; + } + t ^= 1; + } + printf("%d\n", f[t][N]); +} + +int main() +{ + while (scanf("%d%d", &N, &M) == 2) + { + for (int i = 1; i <= N; ++i) + { + scanf("%d", a + i); + } + sort(a + 1, a + 1 + N); + for (int i = 1; i <= N; ++i) + { + s[i] = s[i - 1] + a[i]; + } + solve(); + } + return 0; +} diff --git a/12916.cpp b/12916.cpp new file mode 100644 index 0000000..3fb436e --- /dev/null +++ b/12916.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int MAXN = 1000 + 10; + +char s[MAXN]; +int nx[MAXN]; + +int main() +{ + int T; + scanf("%d", &T); + for (int _ = 0; _ < T; ++_) + { + scanf("%s", s); + int n = strlen(s); + nx[0] = -1; + for (int i = 1, j = -1; i < n; ++i) + { + while (j >= 0 && s[j + 1] != s[i]) + j = nx[j]; + nx[i] = s[j + 1] == s[i] ? ++j : j; + } + int T = n - 1 - nx[n - 1]; + if (nx[n - 1] == -1 || n % T) + printf("%d\n", n); + else + printf("%d\n", T); + } + return 0; +} diff --git a/12917.cpp b/12917.cpp new file mode 100644 index 0000000..4449989 --- /dev/null +++ b/12917.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + int P, H, O; + while (scanf("%d%d%d", &P, &H, &O) == 3) + { + if (H > O - P) + { + printf("Hunters win!\n"); + } + else + { + printf("Props win!\n"); + } + } + return 0; +} diff --git a/12918.cpp b/12918.cpp new file mode 100644 index 0000000..0e83de9 --- /dev/null +++ b/12918.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int n, m; + scanf("%d %d", &n, &m); + long long ret = 0; + ret += (long long)(m - n) * n; + ret += (long long)(n) * (n - 1) / 2; + printf("%lld\n", ret); + } + return 0; +} diff --git a/12919.cpp b/12919.cpp new file mode 100644 index 0000000..5f93ba2 --- /dev/null +++ b/12919.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +typedef long long LL; + +int main() +{ + int T; + scanf("%d", &T); + for (int _ = 0; _ < T; ++_) + { + int n, d; + scanf("%d%d", &n, &d); + LL ret(1); + for (int i = 0; i < n; ++i) + { + int x; + scanf("%d", &x); + x -= d; + LL g = __gcd((LL)x, ret); + ret /= g; + ret *= x; + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/1292.cpp b/1292.cpp new file mode 100644 index 0000000..cdcf24c --- /dev/null +++ b/1292.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +const int maxn = 1501; + +vector e[maxn]; +int n, dp[maxn][2]; + +void DFS(int u, int f) +{ + dp[u][0] = 0; + dp[u][1] = 1; + for (int i = 0; i < e[u].size(); i++) + { + int v = e[u][i]; + if (v == f) + { + continue; + } + DFS(v, u); + dp[u][0] += dp[v][1]; + dp[u][1] += min(dp[v][1], dp[v][0]); + } +} + +int main() +{ + while (scanf("%d", &n) != EOF) + { + for (int i = 0; i <= n; i++) + { + e[i].clear(); + } + for (int i = 0; i < n; i++) + { + int ita, itb, m; + scanf("%d:(%d)", &ita, &m); + for (int j = 0; j < m; j++) + { + scanf("%d", &itb); + e[ita].push_back(itb); + e[itb].push_back(ita); + } + } + DFS(0, -1); + printf("%d\n", min(dp[0][0], dp[0][1])); + } + return 0; +} diff --git a/12920.cpp b/12920.cpp new file mode 100644 index 0000000..c76a678 --- /dev/null +++ b/12920.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef double flt; + +const flt eps = 0, INF = 1e18, PI = acos(-1.0); + +flt sqr(flt x) { return x * x; } +int sgn(flt x) { return x < -eps ? -1 : (x > eps); } + +struct Point +{ + flt x, y; + Point(flt a = 0, flt b = 0) : x(a), y(b) {} + bool operator<(const Point &r) const + { + return sgn(x - r.x) < 0 || (sgn(x - r.x) == 0 && sgn(y - r.y) < 0); + } + bool operator==(const Point &r) const + { + return sgn(x - r.x) == 0 && sgn(y - r.y) == 0; + } + Point operator*(const flt &k) const { return Point(x * k, y * k); } + Point operator/(const flt &k) const { return Point(x / k, y / k); } + Point operator-(const Point &r) const { return Point(x - r.x, y - r.y); } + Point operator+(const Point &r) const { return Point(x + r.x, y + r.y); } + flt dot(const Point &r) { return x * r.x + y * r.y; } + flt det(const Point &r) { return x * r.y - y * r.x; } + flt sqr() { return x * x + y * y; } + flt abs() { return hypot(x, y); } + Point rot() { return Point(-y, x); } + Point rot(flt A) { return Point(x * cos(A) - y * sin(A), x * sin(A) + y * cos(A)); } + Point trunc(flt a = 1.0) { return (*this) * (a / this->abs()); } +} A, B; + +Point inter(Point A, Point B, Point C, Point D) +{ + return A + (B - A) * ((D - C).det(C - A) / (D - C).det(B - A)); +} + +flt fix(flt x) +{ + if (x < 0) + x += PI * 2; + if (x > PI * 2) + x -= PI * 2; + return x; +} + +flt L; +Point get(flt x) +{ + flt s = (A - B).abs(); + flt y = L - x, k = y / x; + flt u = s / (1 + k), v = s - u; + flt l = sqrt(x * y - u * v); + Point O = A + (B - A) / (k + 1); + O.y += l; + return O; +} + +int main() +{ + while (scanf("%lf%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y, &L) == 5) + { + if (sgn(A.x - B.x) > 0) + swap(A, B); + Point O(0, 0); + if (A.x == B.x) + { + O.x = A.x; + O.y = min(A.y, B.y) - (L - (A - B).abs()) / 2; + } + else if (sgn((A - B).abs() - L) >= 0) + { + if (sgn(A.y - B.y) < 0) + O = A; + else + O = B; + } + else + { + flt left = 0, right = L; + for (int _ = 0; _ < 100; ++_) + { + flt mid = (left + right) / 2; + O = get(mid); + if (sgn((O - A).abs() - mid) < 0) + right = mid; + else + left = mid; + } + O = get(right); + O.x = A.x + B.x - O.x; + O.y = A.y + B.y - O.y; + } + printf("%.10f %.10f\n", O.x, O.y); + } + return 0; +} diff --git a/12921.cpp b/12921.cpp new file mode 100644 index 0000000..4889dc2 --- /dev/null +++ b/12921.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +typedef double flt; + +const flt eps = 1e-12, INF = 1e18, PI = acos(-1.0); + +flt sqr(flt x) { return x * x; } +int sgn(flt x) { return x < -eps ? -1 : (x > eps); } +flt fix(flt x) { return sgn(x) == 0 ? 0 : x; } + +struct Point +{ + flt x, y; + Point(flt a = 0, flt b = 0) : x(a), y(b) {} + bool operator<(const Point &r) const + { + return sgn(x - r.x) < 0 || (sgn(x - r.x) == 0 && sgn(y - r.y) < 0); + } + bool operator==(const Point &r) const + { + return sgn(x - r.x) == 0 && sgn(y - r.y) == 0; + } + Point operator*(const flt &k) const { return Point(x * k, y * k); } + Point operator/(const flt &k) const { return Point(x / k, y / k); } + Point operator-(const Point &r) const { return Point(x - r.x, y - r.y); } + Point operator+(const Point &r) const { return Point(x + r.x, y + r.y); } + flt dot(const Point &r) { return x * r.x + y * r.y; } + flt det(const Point &r) { return x * r.y - y * r.x; } + flt sqr() { return x * x + y * y; } + flt abs() { return hypot(x, y); } + Point rot() { return Point(-y, x); } + Point rot(flt A) { return Point(x * cos(A) - y * sin(A), x * sin(A) + y * cos(A)); } + Point trunc(flt a = 1.0) { return (*this) * (a / this->abs()); } +} a, b, c; + +int main() +{ + while (scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y) == 6) + { + flt x = (c - b).dot(a - c), y = (c - b).det(a - b); + if (y == 0) + { + puts("Impossible"); + } + else + { + Point m = (a + b) / 2 + (a - b).rot() * x / y / 2; + printf("%.10f %.10f\n", m.x, m.y); + } + } + return 0; +} diff --git a/12922.cpp b/12922.cpp new file mode 100644 index 0000000..8b58363 --- /dev/null +++ b/12922.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +int dp[1<<16], popcount[1<<16], sw[1<<16], w[16], n, k, W; + +int solve(int msk) +{ + if (dp[msk] != -1) + { + return dp[msk]; + } + int &ret = dp[msk]; + ret = 1e9; + if (popcount[msk] <= k && sw[msk] <= W) + { + return dp[msk] = 1; + } + for (int sub = (msk - 1) & msk; sub; sub = (sub - 1) & msk) + { + if (popcount[sub] > k || sw[sub] > W) + { + continue; + } + int r(-1), rst = ((1 << n) - 1) ^ (msk ^ sub); + for (int i = 0; i < n; ++i) + { + if (rst >> i & 1) + { + if (r == -1 || w[i] < w[r]) + { + r = i; + } + } + } + int nxt = (msk ^ sub) | (1 << r); + ret = min(ret, solve(nxt) + 2); + } + return dp[msk] = ret; +} + +int main() +{ + while (scanf("%d%d%d", &n, &W, &k) == 3) + { + for (int i = 0; i < n; ++i) + scanf("%d", w + i); + for (int msk = 0; msk < (1 << n); ++msk) + { + popcount[msk] = sw[msk] = 0; + dp[msk] = -1; + for (int i = 0; i < n; ++i) + { + if (msk >> i & 1) + { + sw[msk] += w[i]; + popcount[msk]++; + } + } + } + int ret = solve((1 << n) - 1); + if (ret == 1e9) + { + ret = -1; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/12923.cpp b/12923.cpp new file mode 100644 index 0000000..0acffe6 --- /dev/null +++ b/12923.cpp @@ -0,0 +1,119 @@ +#include +using namespace std; + +namespace ZKW +{ +const int MAXN = 50000 + 10, MAXM = 300000, inf = 1e9; +struct Edge +{ + int v, f, c, nx; + Edge() {} + Edge(int v, int f, int c, int nx) : v(v), f(f), c(c), nx(nx) {} +} E[MAXM]; +int G[MAXN], dis[MAXN], Q[MAXN]; +bool vis[MAXN], mark[MAXN]; +int N, S, T, sz, mincost; +void init(int _n, int _s, int _t) +{ + N = _n; + S = _s; + T = _t; + sz = 0; + memset(G, -1, sizeof(G[0]) * N); +} +void link(int u, int v, int f, int c) +{ + E[sz] = Edge(v, f, +c, G[u]); + G[u] = sz++; + E[sz] = Edge(u, 0, -c, G[v]); + G[v] = sz++; +} +bool spfa() +{ + for (int i = 0; i < N; ++i) + dis[i] = inf, vis[i] = 0; + dis[T] = 0; + Q[0] = T; + vis[T] = 1; + for (int h = 0, t = 1; h != t;) + { + int u = Q[h++], v; + vis[u] = false; + if (h == MAXN) + h = 0; + for (int it = G[u]; ~it; it = E[it].nx) + { + if (dis[u] - E[it].c < dis[v = E[it].v] && E[it ^ 1].f) + { + dis[v] = dis[u] - E[it].c; + if (!vis[v]) + { + vis[v] = true; + Q[t++] = v; + if (t == MAXN) + t = 0; + } + } + } + } + return dis[S] != inf; +} +int dfs(int u, int low) +{ + mark[u] = true; + if (u == T) + return low; + int ret = 0, tmp = 0; + for (int it = G[u], v; ~it && ret < low; it = E[it].nx) + { + if (E[it].f && !mark[v = E[it].v] && dis[u] - E[it].c == dis[v]) + { + tmp = dfs(v, min(E[it].f, low - ret)); + E[it].f -= tmp; + E[it ^ 1].f += tmp; + mincost += E[it].c * tmp; + ret += tmp; + } + } + return ret; +} +int solve() +{ + int maxflow = 0; + mincost = 0; + while (spfa()) + { + mark[T] = true; + while (mark[T]) + { + memset(mark, 0, sizeof(mark[0]) * N); + maxflow += dfs(S, inf); + } + } + return mincost; +} +} + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + int S(0), T = n * 2 + 9; + ZKW::init(n * 2 + 10, S, T); + for (int i = 1; i <= n; ++i) + { + ZKW::link(S, i, 1, 0); + ZKW::link(i + n, T, 1, 0); + for (int j = 1; j <= n; ++j) + { + int x; + scanf("%d", &x); + ZKW::link(i, j + n, 1, -x); + } + } + int ret = ZKW::solve(); + printf("%d\n", -ret); + } + return 0; +} \ No newline at end of file diff --git a/12926.cpp b/12926.cpp new file mode 100644 index 0000000..ff81d3b --- /dev/null +++ b/12926.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +static const int MAXN = 5000 + 10; +bool G[MAXN][MAXN]; +int n, m; + +struct Tarjan +{// index from 0 to n-1 + int low[MAXN], dfn[MAXN], stk[MAXN], col[MAXN]; + int scc_cnt, sz, top; + void dfs(int x, const bool G[][MAXN]) + { + low[x] = dfn[x] = ++sz; + stk[++top] = x; + for (int y = 0; y < n; y++) + if (G[x][y]) + { + if (!dfn[y]) + { + dfs(y, G); + low[x] = min(low[x], low[y]); + } + else if (col[y] == -1) + low[x] = min(low[x], dfn[y]); + } + if (dfn[x] == low[x]) + { + scc_cnt++; + do + { + col[stk[top]] = scc_cnt - 1; + } while (stk[top--] != x); + } + } + int solve(int n, const bool G[][MAXN]) + { + sz = top = scc_cnt = 0; + memset(dfn, 0, sizeof(dfn)); + memset(col, -1, sizeof(col)); + for (int i = 0; i < n; ++i) + if (!dfn[i]) + dfs(i, G); + return scc_cnt; + } +} AC; + +int main() +{ + while (scanf("%d%d", &n, &m) == 2) + { + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < n; ++j) + { + G[i][j] = 1; + } + } + for (int i = 0; i < m; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + G[x - 1][y - 1] = 0; + } + int d; + scanf("%d", &d); + printf("%d\n", d * AC.solve(n, G)); + } + return 0; +} diff --git a/12927.cpp b/12927.cpp new file mode 100644 index 0000000..a22af52 --- /dev/null +++ b/12927.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +namespace NF +{ +const int MAXN = 200000, MAXM = 1000000, inf = 1e9; +struct Edge +{ + int v, c, f, nx;//c:capcity, f:flow + Edge() {} + Edge(int v, int c, int f, int nx) : v(v), c(c), f(f), nx(nx) {} +} E[MAXM]; +int G[MAXN], cur[MAXN], pre[MAXN], dis[MAXN], gap[MAXN], N, sz; +void init(int _n) +{ + N = _n, sz = 0; + memset(G, -1, sizeof(G[0]) * N); +} +void link(int u, int v, int c) +{ + E[sz] = Edge(v, c, 0, G[u]); + G[u] = sz++; + E[sz] = Edge(u, 0, 0, G[v]); + G[v] = sz++; +} +int ISAP(int S, int T) +{//S -> T + int maxflow = 0, aug = inf, flag = false, u, v; + for (int i = 0; i < N; ++i) + cur[i] = G[i], gap[i] = dis[i] = 0; + for (gap[S] = N, u = pre[S] = S; dis[S] < N; flag = false) + { + for (int &it = cur[u]; ~it; it = E[it].nx) + { + if (E[it].c > E[it].f && dis[u] == dis[v = E[it].v] + 1) + { + if (aug > E[it].c - E[it].f) + aug = E[it].c - E[it].f; + pre[v] = u, u = v; + flag = true; + if (u == T) + { + for (maxflow += aug; u != S;) + { + E[cur[u = pre[u]]].f += aug; + E[cur[u] ^ 1].f -= aug; + } + aug = inf; + } + break; + } + } + if (flag) + continue; + int mx = N; + for (int it = G[u]; ~it; it = E[it].nx) + { + if (E[it].c > E[it].f && dis[E[it].v] < mx) + { + mx = dis[E[it].v]; + cur[u] = it; + } + } + if ((--gap[dis[u]]) == 0) + break; + ++gap[dis[u] = mx + 1]; + u = pre[u]; + } + return maxflow; +} +bool bfs(int S, int T) +{ + static int Q[MAXN]; + memset(dis, -1, sizeof(dis[0]) * N); + dis[S] = 0; + Q[0] = S; + for (int h = 0, t = 1, u, v, it; h < t; ++h) + { + for (u = Q[h], it = G[u]; ~it; it = E[it].nx) + { + if (dis[v = E[it].v] == -1 && E[it].c > E[it].f) + { + dis[v] = dis[u] + 1; + Q[t++] = v; + } + } + } + return dis[T] != -1; +} +int dfs(int u, int T, int low) +{ + if (u == T) + return low; + int ret = 0, tmp, v; + for (int &it = cur[u]; ~it && ret < low; it = E[it].nx) + { + if (dis[v = E[it].v] == dis[u] + 1 && E[it].c > E[it].f) + { + if (tmp = dfs(v, T, min(low - ret, E[it].c - E[it].f))) + { + ret += tmp; + E[it].f += tmp; + E[it ^ 1].f -= tmp; + } + } + } + if (!ret) + dis[u] = -1; + return ret; +} +int dinic(int S, int T) +{ + int maxflow = 0, tmp; + while (bfs(S, T)) + { + memcpy(cur, G, sizeof(G[0]) * N); + while (tmp = dfs(S, T, inf)) + maxflow += tmp; + } + return maxflow; +} +} + +int main() +{ + int T; + scanf("%d", &T); + for (int _ = 0; _ < T; ++_) + { + int n, S(0), T(1e5 + 1); + scanf("%d", &n); + NF::init(1e5 + 10); + for (int i = 1; i <= 50000; ++i) + { + NF::link(S, i, 1); + NF::link(i + 50000, T, 1); + } + for (int i = 0; i < n; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + NF::link(x, y + 50000, 1); + } + printf("%d\n", NF::ISAP(S, T)); + } + return 0; +} diff --git a/12928.cpp b/12928.cpp new file mode 100644 index 0000000..d226277 --- /dev/null +++ b/12928.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +struct Point +{ + int x, y; + Point() {} + Point(int x, int y) : x(x), y(y) {} + Point operator+(const Point &rhs) const + { + return Point(x + rhs.x, y + rhs.y); + } + Point operator-(const Point &rhs) const + { + return Point(x - rhs.x, y - rhs.y); + } + int det(const Point &rhs) const + { + return x * rhs.y - y * rhs.x; + } +}; + +struct Line +{ + Point a, b; + Line() {} + Line(const Point &a, const Point &b) : a(a), b(b) {} + int side(const Point &p) const + { + return (b - a).det(p - a); + } +}; + +bool inside(const Line &a, const Line &b, const Line &c, const Point &p) +{ + return a.side(p) <= 0 && b.side(p) >= 0 && c.side(p) >= 0; +} + +bool ccw(const Point &a, const Point &b, const Point &c) +{ + return (b - a).det(c - a) > 0; +} + +int main() +{ + Point a, b, c, d, e; + while (scanf("%d%d%d%d%d%d%d%d%d%d", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y, &d.x, &d.y, &e.x, &e.y) == 10) + { + int n; + scanf("%d", &n); + if (!ccw(a, e, d) || !ccw(e, d, c) || !ccw(d, c, b) || !ccw(c, b, a) || !ccw(b, a, e)) + { + puts("Impossible"); + for (int i = 0; i < n; ++i) + scanf("%*d%*d"); + } + else + { + for (int i = 0; i < n; ++i) + { + Point p; + scanf("%d%d", &p.x, &p.y); + if (inside(Line(a, c), Line(a, d), Line(e, b), p) || + inside(Line(e, b), Line(e, c), Line(d, a), p) || + inside(Line(d, a), Line(d, b), Line(c, e), p) || + inside(Line(c, e), Line(c, a), Line(b, d), p) || + inside(Line(b, d), Line(b, e), Line(a, c), p)) + puts("Yes"); + else + puts("No"); + } + } + } + return 0; +} diff --git a/12929.cpp b/12929.cpp new file mode 100644 index 0000000..d888e61 --- /dev/null +++ b/12929.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define rep(i, l, n) for (int i = l; i <= n; i++) +#define dep(i, n, l) for (int i = n; i >= l; i--) + +const int N = 210, inf = ~0U >> 2; +int T, n, m, i, j, k, flag, x[N], y[N], cnt, one, b[N], g[N], nxt[N], ans; +int size[N], f[N][N][10], t[N][10]; +struct P +{ + int l, r, w; + P() {} + P(int _l, int _r, int _w) { l = _l, r = _r, w = _w; } +} a[N]; +inline bool cmp(int x, int y) { return x > y; } +inline void up(int &a, int b) +{ + if (a < b) + a = b; +} +void dfs(int x) +{ + rep(a, 0, m) rep(b, 0, k) f[x][a][b] = -inf; + f[x][0][0] = 0; + for (int i = g[x]; i; i = nxt[i]) + { + dfs(i); + dep(a, min(size[x] + size[i], m), 0) rep(b, 0, k) t[a][b] = f[x][a][b]; + dep(a, min(size[x], m), 0) dep(c, min(size[i], m - a), 0) + { + int t1 = -inf, t2 = -inf; + rep(b, 0, k) + { + up(t1, f[i][c][b]); + up(t2, f[x][a][b]); + up(t[a + c][b], max(f[x][a][b] + t1, f[i][c][b] + t2)); + } + } + size[x] += size[i]; + dep(a, min(size[x], m), 0) rep(b, 0, k) f[x][a][b] = t[a][b]; + } + if (!x) + return; + size[x]++; + dep(a, min(size[x], m - 1), 0) dep(b, k - 1, 0) up(f[x][a + 1][b + 1], f[x][a][b] + ::a[x].w); +} +int main() +{ + while (~scanf("%d%d%d", &n, &m, &k)) + { + k--; + rep(i, 1, n) scanf("%d%d", &x[i], &y[i]); + rep(i, 1, n) rep(j, i + 1, n) if (y[i] == y[j]) + { + flag = 1; + rep(k, i + 1, j - 1) if (y[k] >= y[i]) + { + flag = 0; + break; + } + if (flag) + { + if (i + 1 == j) + { + b[++one] = x[j] - x[i]; + } + else + { + a[++cnt] = P(i + 1, j - 1, x[j] - x[i]); + } + } + } + if (one > 1) + sort(b + 1, b + one + 1, cmp); + rep(i, 2, one) b[i] += b[i - 1]; + rep(i, 1, cnt) + { + j = 0; + rep(k, 1, cnt) if (a[k].l < a[i].l && a[k].r > a[i].r) if (!j || a[k].w < a[j].w) j = k; + nxt[i] = g[j], g[j] = i; + } + dfs(0); + ans = -1; + rep(i, m - one, m) rep(j, 0, k) up(ans, f[0][i][j] + b[m - i]); + printf("Case %d: %d\n", ++T, ans); + rep(i, 0, cnt) g[i] = size[i] = 0; + cnt = one = 0; + } + return 0; +} diff --git a/12930.cpp b/12930.cpp new file mode 100644 index 0000000..3908474 --- /dev/null +++ b/12930.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +struct BigDecimal +{ + string a, b; + int sign; + int read() + { + char s[1024]; + int f = scanf("%s", s) == 1; + sign = 1; + for (int i = 0; s[i]; i++) + { + if (s[i] == '.') + { + s[i] = '\0'; + if (s[0] == '-') + a = s + 1, sign = -1; + else if (s[0] == '+') + a = s + 1, sign = 1; + else + a = s, sign = 1; + b = s + i + 1; + } + } + return f; + } + int cmp(const BigDecimal &x) const + { + if (sign != x.sign) + return sign < x.sign ? -1 : 1; + if (sign == 1) + { + if (a.length() != x.a.length()) + return a.length() < x.a.length() ? -1 : 1; + for (int i = 0; i < a.length(); i++) + { + if (a[i] != x.a[i]) + return a[i] < x.a[i] ? -1 : 1; + } + for (int i = 0; i < max(b.length(), x.b.length()); i++) + { + int p = '0', q = '0'; + if (i < b.length()) + p = b[i]; + if (i < x.b.length()) + q = x.b[i]; + if (p != q) + return p < q ? -1 : 1; + } + } + else + { + if (a.length() != x.a.length()) + return a.length() < x.a.length() ? 1 : -1; + for (int i = 0; i < a.length(); i++) + { + if (a[i] != x.a[i]) + return a[i] < x.a[i] ? 1 : -1; + } + for (int i = 0; i < max(b.length(), x.b.length()); i++) + { + int p = '0', q = '0'; + if (i < b.length()) + p = b[i]; + if (i < x.b.length()) + q = x.b[i]; + if (p != q) + return p < q ? 1 : -1; + } + } + return 0; + } +}; + +int main() +{ + int cases = 0; + BigDecimal a, b; + while (a.read()) + { + b.read(); + printf("Case %d: ", ++cases); + int c = a.cmp(b); + if (c == 0) + puts("Same"); + else if (c == 1) + puts("Bigger"); + else + puts("Smaller"); + } + return 0; +} diff --git a/12931.cpp b/12931.cpp new file mode 100644 index 0000000..2f6ea52 --- /dev/null +++ b/12931.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; +typedef double flt; + +const flt eps = 1e-12, INF = 1e18, PI = acos(-1.0); + +flt sqr(flt x) { return x * x; } +int sgn(flt x) { return x < -eps ? -1 : (x > eps); } +flt fix(flt x) { return sgn(x) == 0 ? 0 : x; } + +struct Point +{ + flt x, y; + Point(flt a = 0, flt b = 0) : x(a), y(b) {} + bool operator<(const Point &r) const + { + return sgn(x - r.x) < 0 || (sgn(x - r.x) == 0 && sgn(y - r.y) < 0); + } + bool operator==(const Point &r) const + { + return sgn(x - r.x) == 0 && sgn(y - r.y) == 0; + } + Point operator*(const flt &k) const { return Point(x * k, y * k); } + Point operator/(const flt &k) const { return Point(x / k, y / k); } + Point operator-(const Point &r) const { return Point(x - r.x, y - r.y); } + Point operator+(const Point &r) const { return Point(x + r.x, y + r.y); } + flt dot(const Point &r) { return x * r.x + y * r.y; } + flt det(const Point &r) { return x * r.y - y * r.x; } + flt sqr() { return x * x + y * y; } + flt abs() { return hypot(x, y); } + Point rot() { return Point(-y, x); } + Point rot(flt A) { return Point(x * cos(A) - y * sin(A), x * sin(A) + y * cos(A)); } + Point trunc(flt a = 1.0) { return (*this) * (a / this->abs()); } +}; + +struct Line +{ + Point a, b;// a->b + flt ang; + Line() {} + Line(const Point &a, const Point &b) : a(a), b(b) + { + ang = atan2(b.y - a.y, b.x - a.x); + } + bool operator<(const Line &l) const + { + int res = sgn(ang - l.ang); + return res == 0 ? l.side(a) >= 0 : res < 0; + } + int side(const Point &p) const + {// 1: left, 0: on, -1:right + return sgn((b - a).det(p - a)); + } + Point inter(const Line &l) const + { + flt k = (l.a - l.b).det(a - l.b); + k = k / (k - (l.a - l.b).det(b - l.b)); + return a + (b - a) * k; + } +}; + +bool onSeg(const Point &A, const Point &B, const Point &O) +{ + return sgn((A - O).det(B - O) == 0) && sgn((A - O).dot(B - O)) <= 0; +} +bool intersect(const Point &A, const Point &B, const Point &C, const Point &D, Point &res) +{ + Point AB(B - A), CD(D - C); + if (sgn(AB.det(CD)) == 0) + return false;// 平行 + int d1 = sgn(AB.det(C - A)) * sgn(AB.det(D - A)); + int d2 = sgn(CD.det(A - C)) * sgn(CD.det(B - C)); + res = A + (B - A) * ((D - C).det(C - A) / (D - C).det(B - A)); + return d1 < 0 && d2 < 0; +} +int inPolygon(vector &P, Point O) +{ + int cnt = 0, n = P.size(); + for (int i = 0; i < n; ++i) + { + if (onSeg(P[i], P[(i + 1) % n], O)) + return 2; + int k = sgn((P[(i + 1) % n] - P[i]).det(O - P[i])); + int d1 = sgn(P[i].y - O.y), d2 = sgn(P[(i + 1) % n].y - O.y); + cnt += (k > 0 && d1 <= 0 && d2 > 0) - (k < 0 && d2 <= 0 && d1 > 0); + } + return cnt != 0; +} +bool inPolygon(vector &P, Point A, Point B) +{ + if (inPolygon(P, A) == 0 || inPolygon(P, B) == 0) + return 0; + int n = P.size(); + vector v; + for (int i = 0; i < n; ++i) + { + Point tmp; + if (intersect(A, B, P[i], P[(i + 1) % n], tmp)) + return 0; + if (onSeg(A, B, P[i])) + v.push_back(P[i]); + } + sort(v.begin(), v.end()); + for (size_t i = 1; i < v.size(); ++i) + { + Point O = (v[i] + v[i - 1]) / 2; + if (inPolygon(P, O) == 0) + return 0; + } + return 1; +} + +bool halfplane(vector v) +{ + sort(v.begin(), v.end()); + deque q; + q.push_back(v[0]); + deque ans; + for (size_t i = 1; i < v.size(); ++i) + { + if (sgn(v[i].ang - v[i - 1].ang) == 0) + continue; + while (ans.size() && v[i].side(ans.back()) < 0) + ans.pop_back(), q.pop_back(); + while (ans.size() && v[i].side(ans.front()) < 0) + ans.pop_front(), q.pop_front(); + ans.push_back(q.back().inter(v[i])); + q.push_back(v[i]); + } + while (ans.size() && q.front().side(ans.back()) < 0) + ans.pop_back(), q.pop_back(); + while (ans.size() && q.back().side(ans.front()) < 0) + ans.pop_front(), q.pop_front(); + if (q.size() <= 2) + return false; + vector pt(ans.begin(), ans.end()); + pt.push_back(q.front().inter(q.back())); + sort(pt.begin(), pt.end()); + pt.erase(unique(pt.begin(), pt.end()), pt.end()); + return pt.size() > 2; +} + +struct Triangle +{ + Point a, b, c; + Triangle() {} + Triangle(const Point &_a, const Point &_b, const Point &_c) : a(_a), b(_b), c(_c) + { + if (sgn((c - a).det(b - a)) > 0) + swap(b, c); + } + vector toHalfplane() const + { + vector r; + r.push_back(Line(a, b)); + r.push_back(Line(b, c)); + r.push_back(Line(c, a)); + return r; + } +}; + +vector A, B; +int n, m; + +vector getTriangle(vector pt) +{ + vector ret; + while (pt.size() > 2) + { + int n = pt.size(); + for (int i = 0; i < n; ++i) + { + Point A = pt[(i - 1 + n) % n], B = pt[(i + 1) % n]; + if (inPolygon(pt, A, B)) + { + ret.push_back(Triangle(A, B, pt[i])); + /*cerr << "(" << A.x << "," << A.y << ") "; + cerr << "(" << B.x << "," << B.y << ") "; + cerr << "(" << pt[i].x << "," << pt[i].y << ") " << endl;*/ + pt.erase(pt.begin() + i); + break; + } + } + } + return ret; +} + +int main() +{ + for (int cas(1); scanf("%d", &n) == 1; ++cas) + { + A.clear(); + B.clear(); + for (int i = 0; i < n; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + A.push_back(Point(x, y)); + } + scanf("%d", &m); + for (int i = 0; i < m; ++i) + { + int x, y; + scanf("%d%d", &x, &y); + B.push_back(Point(x, y)); + } + vector TA = getTriangle(A); + vector TB = getTriangle(B); + bool flag = true; + for (size_t i = 0; i < TA.size() && flag; ++i) + { + for (size_t j = 0; j < TB.size() && flag; ++j) + { + vector la = TA[i].toHalfplane(); + vector lb = TB[j].toHalfplane(); + for (auto &x : lb) + la.push_back(x); + if (halfplane(la)) + flag = false; + } + } + printf("Case %d: %s\n", cas, flag ? "No" : "Yes"); + } + return 0; +} diff --git a/12932.cpp b/12932.cpp new file mode 100644 index 0000000..af63e6a --- /dev/null +++ b/12932.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int g[512][512]; + +int main() +{ + int n, m, cases = 0; + while (scanf("%d %d", &n, &m) == 2) + { + int row[512] = {}, col[512] = {}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + scanf("%d", &g[i][j]); + row[i] += g[i][j]; + col[j] += g[i][j]; + } + } + + int mxr = 0, mxc = 0; + for (int i = 0; i < n; i++) + if (row[i] > row[mxr]) + mxr = i; + for (int i = 0; i < m; i++) + if (col[i] > col[mxc]) + mxc = i; + int test = row[mxr] + col[mxc] - g[mxr][mxc]; + int acc = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + acc = max(acc, row[i] + col[j] - g[i][j]); + } + } + printf("Case %d: %s\n", ++cases, acc == test ? "Weak" : "Strong"); + } + return 0; +} diff --git a/12933.cpp b/12933.cpp new file mode 100644 index 0000000..503f7aa --- /dev/null +++ b/12933.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +const int MAXN = 512; +const int INF = 0x3f3f3f3f; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +int g[MAXN][MAXN], dist[MAXN][MAXN], dist2[MAXN][MAXN][4]; +int n, m, sx, sy, ex, ey; + +void dijkstra(int sx, int sy) +{ + typedef pair> PLL; + for (int i = 0; i <= n; i++) + { + for (int j = 1; j <= m; j++) + dist[i][j] = INF; + } + + set pQ; + PLL u; + pQ.insert(PLL(g[sx][sy], {sx, sy})), dist[sx][sy] = g[sx][sy]; + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + int x, y, tx, ty; + x = u.second.first, y = u.second.second; + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 1 || ty < 1 || tx > n || ty > m) + continue; + if (g[tx][ty] == 0) + continue; + if (dist[tx][ty] > dist[x][y] + g[tx][ty]) + { + if (dist[tx][ty] != INF) + pQ.erase(pQ.find(PLL(dist[tx][ty], {tx, ty}))); + dist[tx][ty] = dist[x][y] + g[tx][ty]; + pQ.insert(PLL(dist[tx][ty], {tx, ty})); + } + } + } +} +void dijkstra_turn(int sx, int sy) +{ + struct E + { + int x, y, d; + bool operator<(const E &a) const + { + if (x != a.x) + return x < a.x; + if (y != a.y) + return y < a.y; + return d < a.d; + } + }; + typedef pair PLL; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + for (int k = 0; k < 4; k++) + dist2[i][j][k] = INF; + } + + set pQ; + PLL u; + int x, y, tx, ty, d; + for (int i = 0; i < 4; i++) + { + x = sx, y = sy; + tx = x + dx[i], ty = y + dy[i]; + if (tx < 1 || ty < 1 || tx > n || ty > m) + continue; + if (g[tx][ty] == 0) + continue; + dist2[tx][ty][i] = g[sx][sy] + g[tx][ty]; + pQ.insert(PLL(g[tx][ty], {tx, ty, i})); + } + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + x = u.second.x, y = u.second.y, d = u.second.d; + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (i == d) + continue; + if (tx < 1 || ty < 1 || tx > n || ty > m) + continue; + if (g[tx][ty] == 0) + continue; + if (dist2[tx][ty][i] > dist2[x][y][d] + g[tx][ty]) + { + if (dist2[tx][ty][i] != INF) + pQ.erase(pQ.find(PLL(dist2[tx][ty][i], {tx, ty, i}))); + dist2[tx][ty][i] = dist2[x][y][d] + g[tx][ty]; + pQ.insert(PLL(dist2[tx][ty][i], {tx, ty, i})); + } + } + } +} + +int main() +{ + int cases = 0; + char s[128]; + while (scanf("%d %d", &n, &m) == 2) + { + scanf("%d %d %d %d", &sx, &sy, &ex, &ey); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + scanf("%s", s); + if (s[0] == '*') + g[i][j] = 0; + else + sscanf(s, "%d", &g[i][j]); + } + } + printf("Case %d: ", ++cases); + dijkstra(sx, sy); + if (dist[ex][ey] == INF) + printf("-1 "); + else + printf("%d ", dist[ex][ey]); + dijkstra_turn(sx, sy); + int ret = INF; + for (int i = 0; i < 4; i++) + ret = min(ret, dist2[ex][ey][i]); + if (ret == INF) + printf("-1"); + else + printf("%d", ret); + puts(""); + } + return 0; +} diff --git a/12934.cpp b/12934.cpp new file mode 100644 index 0000000..71fa995 --- /dev/null +++ b/12934.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +void solve(long long x) +{ + for (long long n = 2; n * (n - 1) <= x; n++) + { + long long pp = n; + for (long long m = n - 1; m >= 1 && pp <= x; m--) + { + if (pp == x) + { + printf("%lld %lld\n", n, m); + return; + } + pp *= m; + } + } + printf("%lld %lld\n", x, x - 1); +} + +int main() +{ + int cases = 0; + long long x; + while (scanf("%lld", &x) == 1) + { + printf("Case %d: ", ++cases); + if (x < 2) + puts("Impossible"); + else + solve(x); + } + return 0; +} diff --git a/12936.cpp b/12936.cpp new file mode 100644 index 0000000..ed94d29 --- /dev/null +++ b/12936.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +map R; + +int toIndex(string s) +{ + if (R.count(s)) + return R[s]; + int v = (int)R.size(); + return R[s] = v; +} + +int main() +{ + string s1, s2; + map, int> hehe; + while (cin >> s1) + { + getline(cin, s2); + string name1, name2; + int pos = (int)s1.find("->"); + name1 = s1.substr(0, pos); + name2 = s1.substr(pos + 2, s1.length() - pos - 3); + int x = toIndex(name1), y = toIndex(name2); + if (x > y) + swap(x, y); + for (int i = 0; i < s2.length(); i++) + s2[i] = tolower(s2[i]); + int has = 0; + stringstream sin(s2); + while (sin >> s2) + { + if (s2.length() % 2 || s2.length() < 4) + continue; + int ok = 1; + for (int i = 0; i < s2.length(); i += 2) + { + ok &= s2[i] == 'h' && s2[i + 1] == 'e'; + } + has |= ok; + } + hehe[make_pair(x, y)] = has; + } + int a = 0, b = 0; + for (auto &x : hehe) + { + a += x.second, b++; + } + printf("%d%%\n", (int)round((double)a * 100 / b)); + return 0; +} diff --git a/12937.cpp b/12937.cpp new file mode 100644 index 0000000..1a247f8 --- /dev/null +++ b/12937.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; +typedef long long LL; +typedef pair PII; + +const int MAXN = 300000 + 10, SIZE = MAXN * 50; + +struct Node +{ + Node *ch[2]; + int sz; +} pool[SIZE], *rt[MAXN], *cnt, *null; + +LL light[MAXN]; +int n, m, a, b; + +Node *newNode() +{ + cnt->ch[0] = cnt->ch[1] = null; + cnt->sz = 0; + return cnt++; +} + +Node *ins(Node *o, LL x, int i = 0) +{ + Node *p = newNode(); + *p = *o; + if (i == n) + { + p->sz++; + return p; + } + int c = x >> i & 1; + p->ch[c] = ins(o->ch[c], x, i + 1); + return p; +} + +int ask(Node *o, LL x) +{ + Node *p = o; + for (int i = 0; i < n; ++i) + { + int c = x >> i & 1; + p = p->ch[!c]; + } + return p->sz; +} + +void init() +{ + cnt = pool; + null = newNode(); + null->ch[0] = null->ch[1] = null; +} + +int main() +{ + for (int cas(1); scanf("%d%d%d%d", &n, &m, &a, &b) == 4; ++cas) + { + LL ret(0); + init(); + light[0] = 0; + rt[0] = ins(null, 0); + for (int i = 1; i <= m; ++i) + { + char s[100]; + scanf("%s", s); + light[i] = 0; + for (int j = 0; j < n; ++j) + { + light[i] |= LL(s[j] - '0') << j; + } + Node *l = (i - b - 1 >= 0) ? rt[i - b - 1] : null; + Node *r = (i - a >= 0) ? rt[i - a] : null; + light[i] ^= light[i - 1]; + ret += ask(r, light[i]) - ask(l, light[i]); + rt[i] = ins(rt[i - 1], light[i]); + } + printf("Case %d: %lld\n", cas, ret); + } + return 0; +} diff --git a/12938.cpp b/12938.cpp new file mode 100644 index 0000000..61c1630 --- /dev/null +++ b/12938.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + char n[16], m[16]; + scanf("%s", n); + + int ret = 0; + for (int i = 32; i < 100; i++) + { + sprintf(m, "%d", i * i); + int diff = 0; + for (int j = 0; j < 4; j++) + diff += n[j] != m[j]; + if (diff == 1) + ret++; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/12939.cpp b/12939.cpp new file mode 100644 index 0000000..26a8589 --- /dev/null +++ b/12939.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +const int N = 200010, M = 10010; + +int T, n, m, d, i, x, y, id[N], root, cmp_d, X1, X2, Y1, Y2, lim, l, r, k; +struct P +{ + int x, y; +} a[N]; +struct Q +{ + int l, r, id; +} q[M]; +long long now, ans[M]; +inline bool cmpq(const Q &a, const Q &b) { return a.l / lim < b.l / lim || a.l / lim == b.l / lim && a.r < b.r; } +struct node +{ + int d[2], l, r, Max[2], Min[2], val, sum, f; +} t[N]; +inline bool cmp(const node &a, const node &b) { return a.d[cmp_d] < b.d[cmp_d]; } +inline void umax(int &a, int b) +{ + if (a < b) + a = b; +} +inline void umin(int &a, int b) +{ + if (a > b) + a = b; +} +inline void up(int x) +{ + if (t[x].l) + { + umax(t[x].Max[0], t[t[x].l].Max[0]); + umin(t[x].Min[0], t[t[x].l].Min[0]); + umax(t[x].Max[1], t[t[x].l].Max[1]); + umin(t[x].Min[1], t[t[x].l].Min[1]); + } + if (t[x].r) + { + umax(t[x].Max[0], t[t[x].r].Max[0]); + umin(t[x].Min[0], t[t[x].r].Min[0]); + umax(t[x].Max[1], t[t[x].r].Max[1]); + umin(t[x].Min[1], t[t[x].r].Min[1]); + } +} +int build(int l, int r, int D, int f) +{ + int mid = (l + r) >> 1; + cmp_d = D, std::nth_element(t + l + 1, t + mid + 1, t + r + 1, cmp); + id[t[mid].f] = mid; + t[mid].f = f; + t[mid].Max[0] = t[mid].Min[0] = t[mid].d[0]; + t[mid].Max[1] = t[mid].Min[1] = t[mid].d[1]; + t[mid].val = t[mid].sum = 0; + if (l != mid) + t[mid].l = build(l, mid - 1, !D, mid); + else + t[mid].l = 0; + if (r != mid) + t[mid].r = build(mid + 1, r, !D, mid); + else + t[mid].r = 0; + return up(mid), mid; +} +inline void change(int x, int p) +{ + for (t[x].val += p; x; x = t[x].f) + t[x].sum += p; +} +void ask(int x) +{ + if (t[x].Min[0] > X2 || t[x].Max[0] < X1 || t[x].Min[1] > Y2 || t[x].Max[1] < Y1 || !t[x].sum) + return; + if (t[x].Min[0] >= X1 && t[x].Max[0] <= X2 && t[x].Min[1] >= Y1 && t[x].Max[1] <= Y2) + { + k += t[x].sum; + return; + } + if (t[x].d[0] >= X1 && t[x].d[0] <= X2 && t[x].d[1] >= Y1 && t[x].d[1] <= Y2) + k += t[x].val; + if (t[x].l) + ask(t[x].l); + if (t[x].r) + ask(t[x].r); +} +inline void add(int x) +{ + X1 = a[x].x - d, X2 = a[x].x + d, Y1 = a[x].y - d, Y2 = a[x].y + d; + k = 0, ask(root), now += k; + change(id[x], 1); +} +inline void del(int x) +{ + change(id[x], -1); + X1 = a[x].x - d, X2 = a[x].x + d, Y1 = a[x].y - d, Y2 = a[x].y + d; + k = 0, ask(root), now -= k; +} + +int main() +{ + while (~scanf("%d%d%d", &n, &d, &m)) + { + printf("Case %d:\n", ++T); + lim = (int)std::sqrt(n + 0.5); + for (i = 1; i <= n; i++) + { + scanf("%d%d", &x, &y); + t[i].d[0] = a[i].x = x + y, t[i].d[1] = a[i].y = x - y, t[i].f = i; + } + root = build(1, n, 0, 0); + for (i = 1; i <= m; i++) + scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i; + std::sort(q + 1, q + m + 1, cmpq); + for (i = l = 1, r = now = 0; i <= m; i++) + { + int L = q[i].l, R = q[i].r; + if (r < R) + { + for (r++; r <= R; r++) + add(r); + r--; + } + if (r > R) + for (; r > R; r--) + del(r); + if (l < L) + for (; l < L; l++) + del(l); + else if (l > L) + { + for (l--; l >= L; l--) + add(l); + l++; + } + ans[q[i].id] = now; + } + for (i = 1; i <= m; i++) + printf("%lld\n", ans[i]); + } + return 0; +} diff --git a/12940.cpp b/12940.cpp new file mode 100644 index 0000000..b8f2408 --- /dev/null +++ b/12940.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +int main() +{ + int m, n; + char d[128]; + while (scanf("%d %s", &m, d) == 2) + { + n = (int)strlen(d); + int A[128] = {}, B[128] = {}; + reverse(d, d + n); + for (int i = 0; i < n; i++) + A[i] = d[i] - '0', B[i] = A[i]; + for (int i = n - 1, j = 0; i >= n / 2; i--, j++) + B[j] = B[i]; + int flag = 0; + for (int i = n - 1; i >= 0; i--) + { + if (B[i] != A[i]) + { + flag = B[i] > A[i]; + break; + } + } + if (flag) + { + for (int i = n - 1; i >= 0; i--) + printf("%d", B[i]); + puts(""); + m--; + } + for (int i = 0; i < m; i++) + { + A[n / 2]++; + for (int j = n / 2; j <= n; j++) + { + if (A[j] >= 10) + { + A[j + 1] += A[j] / 10; + A[j] %= 10; + } + } + if (A[n]) + n++; + if (n % 2) + { + for (int j = n - 1; j > n / 2; j--) + printf("%d", A[j]); + printf("%d", A[n / 2]); + for (int j = n / 2 + 1; j < n; j++) + printf("%d", A[j]); + } + else + { + for (int j = n - 1; j >= n / 2; j--) + printf("%d", A[j]); + for (int j = n / 2; j < n; j++) + printf("%d", A[j]); + } + puts(""); + } + } + return 0; +} diff --git a/12942.cpp b/12942.cpp new file mode 100644 index 0000000..c5ee9ae --- /dev/null +++ b/12942.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +int priority_op(char c) +{ + switch (c) + { + case '(': + return 0; + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + } + return -1; +} +void toPostfix(char infix[], char buffer[]) +{ + int len = (int)strlen(infix); + char *ptr = buffer; + stack op; + *ptr = '\0'; + for (int i = 0; i < len; i++) + { + if (infix[i] == ' ') + continue; + if (isdigit(infix[i]) || isalpha(infix[i])) + { + while (isdigit(infix[i]) || isalpha(infix[i])) + { + // sprintf(ptr, "%c", infix[i]), i++; + i++; + while (*ptr) + ptr++; + } + sprintf(ptr, "x"); + while (*ptr) + ptr++; + i--; + } + else + { + if (infix[i] == ')') + { + while (!op.empty() && op.top() != '(') + { + sprintf(ptr, "%c", op.top()), op.pop(); + while (*ptr) + ptr++; + } + op.pop(); + } + else + { + if (infix[i] != '(') + { + while (!op.empty() && priority_op(op.top()) >= priority_op(infix[i])) + { + sprintf(ptr, "%c", op.top()), op.pop(); + while (*ptr) + ptr++; + } + } + op.push(infix[i]); + } + } + } + while (!op.empty()) + { + sprintf(ptr, "%c", op.top()), op.pop(); + while (*ptr) + ptr++; + } +} +vector buildBST(char buf[]) +{ + stack stk; + vector hash; + int a, b, c; + for (int i = 0; buf[i]; i++) + { + int ha = 63689, hb = 378551; + switch (buf[i]) + { + case '+': + case '-': + case '*': + case '/': + b = stk.top(), stk.pop();// right + a = stk.top(), stk.pop();// left + c = 0; + c = c * ha + a, ha *= hb; + c = c * ha + b, ha *= hb; + c = c * ha + 1; + stk.push(c); + hash.push_back(c); + break; + case 'x': + stk.push(1); + hash.push_back(1); + break; + default: + break; + } + } + return hash; +} + +char s[1048576], buf[1048576]; + +int main() +{ + while (scanf("%s", s) == 1) + { + toPostfix(s, buf); + vector a = buildBST(buf); + scanf("%s", s); + toPostfix(s, buf); + vector b = buildBST(buf); + + int ret = 0, final = a.back(); + for (int i = 0; i < b.size(); i++) + ret += b[i] == final; + printf("%d\n", ret); + } + return 0; +} diff --git a/12943.cpp b/12943.cpp new file mode 100644 index 0000000..e3bff04 --- /dev/null +++ b/12943.cpp @@ -0,0 +1,139 @@ +#include + +using namespace std; + +struct Frac +{ + long long x, y; + Frac(long long a = 0, long long b = 1) + { + x = a, y = b; + normal(); + } + void normal() + { + if (y < 0) + x = -x, y = -y; + long long g = llgcd(x, y); + x /= g, y /= g; + if (y < 0) + x = -x, y = -y; + } + long long llgcd(long long x, long long y) const + { + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; + } + Frac operator-(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x - vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator+(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x + vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator*(const Frac a) const + { + long long g1 = llgcd(x, a.y), g2 = llgcd(a.x, y); + long long va = 0, vb = 0; + va = (x / g1) * (a.x / g2); + vb = (y / g2) * (a.y / g1); + return Frac(va, vb); + } + Frac operator/(const Frac a) const + { + long long g1 = llgcd(y, a.y), g2 = llgcd(x, a.x); + long long va = 0, vb = 0; + va = (a.y / g1) * (x / g2); + vb = (y / g1) * (a.x / g2); + return Frac(va, vb); + } + bool operator==(const Frac &a) const + { + return x - a.x == 0 && y - a.y == 0; + } + bool operator<(const Frac &a) const + { + // WTF about this. + double f1, f2; + f1 = 1.0 * x / y, f2 = 1.0 * a.x / a.y; + if (fabs(f1 - f2) < 1e-9) + return false; + return f1 < f2; + // return x * a.y < a.x * y; + } + void print() + { + printf("%lld/%lld", x, y); + } +}; + +int N, K, M[32], cost[32][32]; +Frac nofail[32][32], ret; +Frac dp[32][2048]; +void dfs(int idx, int K, Frac succ) +{ + if (K < 0 || succ < ret) + return; + if (idx == N) + { + ret = succ; + return; + } + for (int i = 0; i < M[idx]; i++) + { + dfs(idx + 1, K - cost[idx][i], succ * nofail[idx][i]); + } +} +int main() +{ + int x, y, z; + while (scanf("%d %d", &N, &K) == 2) + { + for (int i = 0; i < N; i++) + scanf("%d", &M[i]); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M[i]; j++) + { + scanf("%d %d %d", &x, &y, &z); + nofail[i][j] = Frac(y - x, y), cost[i][j] = z; + } + } + + for (int i = 0; i <= N; i++) + for (int j = 0; j <= K; j++) + dp[i][j] = Frac(0, 1); + + dp[0][0] = Frac(1, 1); + for (int i = 0; i <= N; i++) + { + for (int j = 0; j < M[i]; j++) + { + for (int k = K - cost[i][j]; k >= 0; k--) + { + Frac p = dp[i][k] * nofail[i][j]; + if (dp[i + 1][k + cost[i][j]] < p) + dp[i + 1][k + cost[i][j]] = p; + } + } + } + + ret = Frac(0, 1); + for (int i = 0; i <= K; i++) + { + if (ret < dp[N][i]) + ret = dp[N][i]; + } + ret = Frac(1, 1) - ret; + ret.print(); + puts(""); + } + return 0; +} diff --git a/12944.cpp b/12944.cpp new file mode 100644 index 0000000..b0b225a --- /dev/null +++ b/12944.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +const int MAXV = 65536; +const int MAXE = 65536; +class MinCost +{ +public: + struct Node + { + int x, y, cap, cost;// x->y, v + int next; + } edge[MAXE]; + int e, head[MAXV], dis[MAXV], pre[MAXV], record[MAXV], inq[MAXV]; + void Addedge(int x, int y, int cap, int cost) + { + edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost; + edge[e].next = head[y], head[y] = e++; + } + pair mincost(int s, int t) + { + int mncost = 0, flow, totflow = 0; + int i, x, y; + while (1) + { + memset(dis, 63, sizeof(dis)); + int oo = dis[0]; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(), Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + pre[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + Q.push_front(y); + else + Q.push_back(y); + } + } + } + } + if (dis[t] == oo) + break; + flow = oo; + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return make_pair(mncost, totflow); + } + void init(int n) + { + e = 0; + for (int i = 0; i <= n; i++) + head[i] = -1; + } +} g; + +int main() +{ + int n, m; + int x, y, w, c; + while (scanf("%d %d", &n, &m) == 2) + { + g.init(n + 10); + int source = n + 2, sink = n; + for (int i = 1; i < n; i++) + { + scanf("%d", &w); + g.Addedge(source, i, w, 0); + } + for (int i = 0; i < m; i++) + { + scanf("%d %d %d %d", &x, &y, &w, &c); + g.Addedge(x, y, w, c); + g.Addedge(y, x, w, c); + } + pair ret = g.mincost(source, sink); + printf("%d %d\n", ret.second, ret.first); + } + return 0; +} diff --git a/12945.cpp b/12945.cpp new file mode 100644 index 0000000..472aca1 --- /dev/null +++ b/12945.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +struct Point +{ + double x, y, w; +}; +double _random() +{ + return (double)rand() / RAND_MAX; +} +double h_func(double x, double y, Point p[], int n) +{ + double sum = 0; + for (int i = 0; i < n; i++) + sum += p[i].w * ((p[i].x - x) * (p[i].x - x) + (p[i].y - y) * (p[i].y - y)); + return sum; +} +double annealing(Point p[], int n) +{ + const double S_MUL = 0.5f; + const double S_LEN = 1000; + const int T_CNT = 10; + const int E_CNT = 100; + double step = S_LEN; + double x[E_CNT], y[E_CNT], val[E_CNT]; + double Lx, Ly, Rx, Ry, tx, ty, tcost; + Lx = Rx = p[0].x; + Ly = Ry = p[0].y; + for (int i = 0; i < n; i++) + { + Lx = min(Lx, p[i].x), Rx = max(Rx, p[i].x); + Ly = min(Ly, p[i].y), Ry = max(Ry, p[i].y); + } + for (int i = 0; i < E_CNT; i++) + { + x[i] = _random() * (Rx - Lx) + Lx; + y[i] = _random() * (Ry - Ly) + Ly; + val[i] = h_func(x[i], y[i], p, n); + } + while (step > 0.01) + { + for (int i = 0; i < E_CNT; i++) + { + for (int j = 0; j < T_CNT; j++) + { + tx = x[i] + _random() * 2 * step - step; + ty = y[i] + _random() * 2 * step - step; + tcost = h_func(tx, ty, p, n); + if (tcost < val[i]) + val[i] = tcost, x[i] = tx, y[i] = ty; + } + } + step *= S_MUL; + } + double ret = val[0]; + for (int i = 0; i < n; i++) + ret = min(ret, val[i]); + return ret; +} +double solve(Point p[], int n) +{ + double cx = 0, cy = 0, cw = 0; + for (int i = 0; i < n; i++) + { + cx += p[i].x * p[i].w; + cy += p[i].y * p[i].w; + cw += p[i].w; + } + cx /= cw, cy /= cw; + return h_func(cx, cy, p, n); +} + +int main() +{ + int n; + Point p[128]; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].w); + printf("%.3lf\n", solve(p, n)); + } + return 0; +} diff --git a/12946.cpp b/12946.cpp new file mode 100644 index 0000000..ff4180b --- /dev/null +++ b/12946.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + complex base(-1, 1); + int p; + while (scanf("%d", &p) == 1) + { + complex q(0, 0); + for (complex t(1, 0); p; p >>= 1, t = t * base) + { + if (p & 1) + q = q + t; + } + printf("%lld %lld\n", q.real(), q.imag()); + } + return 0; +} diff --git a/12947.cpp b/12947.cpp new file mode 100644 index 0000000..b750e9e --- /dev/null +++ b/12947.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +const int MAXN = 262144; +int tree[MAXN], A[MAXN]; +vector T, g[MAXN]; + +int validInput(int n) +{ + for (int i = 0; i < T.size(); i++) + { + if (T[i] >= n || T[i] < 0) + return 0; + } + return 1; +} +void build(int k, int l, int r) +{ + if (l == r) + { + tree[k] = A[l] != 0; + return; + } + int mid = (l + r) / 2; + build(k << 1, l, mid); + build(k << 1 | 1, mid + 1, r); + tree[k] = tree[k << 1] + tree[k << 1 | 1]; +} +int fetch(int k, int l, int r) +{ + if (l == r) + return l; + int mid = (l + r) / 2; + if (tree[k << 1] <= mid - l) + return fetch(k << 1, l, mid); + else + return fetch(k << 1 | 1, mid + 1, r); +} +void remark(int k, int l, int r, int x) +{ + if (l == r) + { + tree[k] = A[l] != 0; + return; + } + int mid = (l + r) / 2; + if (x <= mid) + remark(k << 1, l, mid, x); + else + remark(k << 1 | 1, mid + 1, r, x); + tree[k] = tree[k << 1] + tree[k << 1 | 1]; +} +void solve(int n) +{ + int u, v; + for (int i = 0; i < n; i++) + A[i] = 0, g[i].clear(); + for (int i = 0; i < T.size(); i++) + A[T[i]]++; + build(1, 0, n - 1); + for (int i = 0; i < T.size(); i++) + { + v = fetch(1, 0, n - 1); + if (A[v]) + { + puts("impossible"); + return; + } + g[T[i]].push_back(v), g[v].push_back(T[i]); + A[T[i]]--, A[v]++; + remark(1, 0, n - 1, v); + remark(1, 0, n - 1, T[i]); + } + v = fetch(1, 0, n - 1); + if (A[v]) + { + puts("impossible"); + return; + } + A[v]++, remark(1, 0, n - 1, v); + u = fetch(1, 0, n - 1); + if (A[u]) + { + puts("impossible"); + return; + } + A[u]++, remark(1, 0, n - 1, u); + g[u].push_back(v), g[v].push_back(u); + printf("%d\n", n); + for (int i = 0; i < n; i++) + { + sort(g[i].begin(), g[i].end()); + for (int j = 0; j < g[i].size(); j++) + { + printf("%d%c", g[i][j], j == g[i].size() - 1 ? '\n' : ' '); + } + } +} + +int main() +{ + int cases = 0; + string line; + int x, n; + while (getline(cin, line)) + { + if (cases++) + puts("*"); + stringstream sin(line); + T.clear(); + while (sin >> x) + T.push_back(x); + n = (int)T.size() + 2; + if (!validInput(n)) + { + puts("impossible"); + continue; + } + solve(n); + } + return 0; +} diff --git a/12948.cpp b/12948.cpp new file mode 100644 index 0000000..c4a92bb --- /dev/null +++ b/12948.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +struct Edge +{ + int to, cc, tt; + Edge(int a = 0, int b = 0, int c = 0) : to(a), cc(b), tt(c) {} +}; +const int MAXN = 305; +vector g[MAXN]; +pair dist[MAXN][MAXN]; +int inq[MAXN][MAXN]; +void spfa(int st, int n) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j <= 301; j++) + dist[i][j] = make_pair(INT_MAX, INT_MAX), inq[i][j] = 0; + } + dist[st][0] = make_pair(0, 0); + queue Q, P; + Q.push(st), P.push(0); + while (!Q.empty()) + { + int u, v, p; + u = Q.front(), Q.pop(); + p = P.front(), P.pop(); + for (auto &e : g[u]) + { + pair c; + c.first = dist[u][p].first + e.cc; + c.second = dist[u][p].second + e.tt; + v = e.to; + if (c < dist[v][p + 1]) + { + dist[v][p + 1] = c; + if (inq[v][p + 1] == 0) + { + inq[v][p + 1] = 1; + Q.push(v), P.push(p + 1); + } + } + } + } + for (int i = 0; i < n; i++) + { + for (int j = 1; j <= 301; j++) + { + if (dist[i][j - 1] < dist[i][j]) + dist[i][j] = dist[i][j - 1]; + } + } +} + +int main() +{ + char s[1024]; + int n, m, q; + int x, y, c, t; + int cases = 0; + while (scanf("%d %d %d", &n, &m, &q) == 3) + { + if (cases++) + puts("."); + map R; + + for (int i = 0; i < n; i++) + { + scanf("%s", s); + R[s] = i, g[i].clear(); + } + for (int i = 0; i < m; i++) + { + scanf("%s", s); + x = R[s]; + scanf("%s", s); + y = R[s]; + scanf("%d %d", &c, &t); + g[x].push_back(Edge(y, c, t)); + } + scanf("%s", s); + int st = R[s]; + spfa(st, n); + for (int i = 0; i < q; i++) + { + scanf("%s %d", s, &x), x++; + y = R[s]; + if (dist[y][x].first != INT_MAX) + printf("%d %d\n", dist[y][x].first, dist[y][x].second); + else + puts("* *"); + } + } + return 0; +} diff --git a/12949.cpp b/12949.cpp new file mode 100644 index 0000000..8282281 --- /dev/null +++ b/12949.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +char s[1024]; +int prefer[128][128], votes[128]; +int n, m, st, visited[128]; +int same[128][128]; +vector g[128], g2[128]; + +void dfs(int u) +{ + visited[u] = 1; + int v; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (visited[v] == 0) + dfs(v); + } +} +int member[128]; +int dfs_clique(int idx, int mm) +{ + if (idx == n) + { + memset(visited, 0, sizeof(visited)); + for (int i = 0; i < mm; i++) + dfs(member[i]); + + int ret = 0; + for (int i = 0; i < n; i++) + ret += visited[i]; + return ret == n; + } + int ok = 1; + for (int i = 0; i < mm; i++) + ok &= same[member[i]][idx]; + if (ok) + { + member[mm] = idx; + if (dfs_clique(idx + 1, mm + 1)) + return 1; + } + return dfs_clique(idx + 1, mm); +} +int solve() +{ + for (int i = 0; i < n; i++) + g[i].clear(), g2[i].clear(); + memset(same, 0, sizeof(same)); + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + int a = 0, b = 0; + for (int k = 0; k < m; k++) + { + if (prefer[k][i] < prefer[k][j]) + a += votes[k]; + else + b += votes[k]; + } + if (a > b) + g[i].push_back(j); + else if (b > a) + g[j].push_back(i); + else + { + g2[i].push_back(j), g2[j].push_back(i); + same[i][j] = same[j][i] = 1; + } + } + same[i][i] = 1; + } + member[0] = st; + return dfs_clique(0, 1); +} + +int main() +{ + while (scanf("%d %d %s", &n, &m, s) == 3) + { + st = s[0] - 'a'; + for (int i = 0; i < m; i++) + { + scanf("%d %s", &votes[i], s); + for (int j = 0; s[j]; j++) + prefer[i][s[j] - 'a'] = j; + } + puts(solve() ? "Y" : "N"); + } + return 0; +} diff --git a/12950.cpp b/12950.cpp new file mode 100644 index 0000000..2283a47 --- /dev/null +++ b/12950.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const int MAXV = 65536; +const int MAXE = 131072; +const long long INF = 1LL << 62; + +struct Edge +{ + int to, eid; + long long w; + Edge *next; +}; +Edge edge[MAXE], *adj[MAXV]; +int e = 0; +long long dist[MAXV][2]; +void addEdge(int x, int y, long long v) +{ + edge[e].to = y, edge[e].w = v, edge[e].eid = e; + edge[e].next = adj[x], adj[x] = &edge[e++]; +} +void dijkstra(int st, long long dist[][2], int n) +{ + struct PLL + { + long long d; + int v, k; + PLL(long long a = 0, int b = 0, int c = 0) : d(a), v(b), k(c) {} + bool operator<(const PLL &e) const + { + if (d != e.d) + return d < e.d; + if (v != e.v) + return v < e.v; + return k < e.k; + } + }; + for (int i = 0; i <= n; i++) + dist[i][0] = dist[i][1] = INF; + set pQ; + PLL u; + pQ.insert(PLL(0, st, 0)), dist[st][0] = 0; + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + for (Edge *p = adj[u.v]; p; p = p->next) + { + int tk = (u.k + 1) % 2; + if (dist[p->to][tk] > dist[u.v][u.k] + p->w) + { + if (dist[p->to][tk] != INF) + pQ.erase(pQ.find(PLL(dist[p->to][tk], p->to, tk))); + dist[p->to][tk] = dist[u.v][u.k] + p->w; + pQ.insert(PLL(dist[p->to][tk], p->to, tk)); + } + } + } +} +int main() +{ + int n, m, x, y, w; + while (scanf("%d %d", &n, &m) == 2) + { + e = 0; + for (int i = 0; i <= n; i++) + adj[i] = NULL; + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &x, &y, &w); + addEdge(x, y, w); + addEdge(y, x, w); + } + dijkstra(1, dist, n); + if (dist[n][0] != INF) + printf("%lld\n", dist[n][0]); + else + puts("-1"); + } + return 0; +} diff --git a/12951.cpp b/12951.cpp new file mode 100644 index 0000000..e31aa55 --- /dev/null +++ b/12951.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define MAXN 1024 + +int BIT[MAXN]; + +int query(int x) +{ + int ret = -0x3f3f3f3f; + while (x > 0) + { + ret = max(ret, BIT[x]); + x -= x & (-x); + } + return ret; +} +void update(int x, int val) +{ + while (x < MAXN) + { + BIT[x] = max(BIT[x], val); + x += x & (-x); + } +} +int main() +{ + int N, C, x; + while (scanf("%d %d", &N, &C) == 2) + { + for (int i = 0; i < MAXN; i++) + BIT[i] = -0x3f3f3f3f; + int ret = 0; + for (int i = 0; i < N; i++) + { + scanf("%d", &x); + int v = query(x - C); + ret = max(ret, v + (x - C)); // sell + update(x, ret - x); // buy + } + printf("%d\n", ret); + } + return 0; +} diff --git a/12952.cpp b/12952.cpp new file mode 100644 index 0000000..fab04eb --- /dev/null +++ b/12952.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + int A, B; + while (scanf("%d%d", &A, &B) == 2) + { + printf("%d\n", max(A, B)); + } + return 0; +} diff --git a/12953.cpp b/12953.cpp new file mode 100644 index 0000000..bc6466c --- /dev/null +++ b/12953.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int main() +{ + int n, m; + int rs[128][128]; + char s[128][128][8]; + while (scanf("%d %d", &n, &m) == 2) + { + int rsum[128], csum[128]; + map R; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + scanf("%s", &s[i][j]), R[s[i][j]] = 0; + scanf("%d", &rsum[i]); + } + for (int j = 0; j < m; j++) + scanf("%d", &csum[j]); + + int size = 0, equ = 0; + for (map::iterator it = R.begin(); + it != R.end(); it++) + it->second = size++; + + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + rs[i][j] = R[s[i][j]]; + } + + int has_solve[1024] = {}, varx[1024] = {}; + for (int it = 0; it < size; it++) + { + for (int i = 0; i < n; i++) + { + int un = 0, xid = -1, xval = 0; + long long sum = 0; + for (int j = 0; j < m; j++) + { + if (has_solve[rs[i][j]]) + sum += varx[rs[i][j]]; + else + { + if (rs[i][j] != xid) + xid = rs[i][j], xval = 1, un++; + else if (rs[i][j] == xid) + xval++; + } + } + if (un == 1) + { + has_solve[xid] = 1; + varx[xid] = (rsum[i] - sum) / xval; + } + } + for (int i = 0; i < m; i++) + { + int un = 0, xid = -1, xval = 0; + long long sum = 0; + for (int j = 0; j < n; j++) + { + if (has_solve[rs[j][i]]) + sum += varx[rs[j][i]]; + else + { + if (rs[j][i] != xid) + xid = rs[j][i], xval = 1, un++; + else if (rs[j][i] == xid) + xval++; + } + } + if (un == 1) + { + has_solve[xid] = 1; + varx[xid] = (csum[i] - sum) / xval; + } + } + } + + for (map::iterator it = R.begin(); + it != R.end(); it++) + printf("%s %d\n", it->first.c_str(), varx[it->second]); + } + return 0; +} diff --git a/12954.cpp b/12954.cpp new file mode 100644 index 0000000..86e8149 --- /dev/null +++ b/12954.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int main() +{ + long long N, B; + while (scanf("%lld %lld", &N, &B) == 2) + { + long long l = 1, r = N / 2 + N % 2, mid; + if (N % 2 && B == N * N) + { + printf("%lld %lld\n", N / 2 + 1, N / 2 + 1); + continue; + } + while (l <= r) + { + mid = (l + r) / 2; + long long st = 4 * N - 4; + long long ed = 4 * (N - (mid - 1) * 2) - 4; + long long ss = (st + ed) * mid / 2; + if (ss - ed + 1 <= B && B <= ss) + { + long long x = mid, y = mid; + long long b = B - (ss - ed + 1), e = N - (mid - 1) * 2; + // printf("st %lld %lld %lld %lld\n", x, y, e, b); + if (b < e) + { + printf("%lld %lld\n", x, y + b); + break; + } + y += e - 1, b -= e, x++; + if (b < e - 1) + { + printf("%lld %lld\n", x + b, y); + break; + } + x += e - 2, b -= e - 1, y--; + if (b < e - 1) + { + printf("%lld %lld\n", x, y - b); + break; + } + y -= e - 2, b -= e - 1, x--; + printf("%lld %lld\n", x - b, y); + break; + } + if (ss < B) + l = mid + 1; + else + r = mid - 1; + } + } + return 0; +} diff --git a/12955.cpp b/12955.cpp new file mode 100644 index 0000000..72b7492 --- /dev/null +++ b/12955.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int dp[100005], N; + memset(dp, 0x3f, sizeof(dp)); + dp[0] = 0; + for (int i = 1, f = 1; f <= 1e+5; i++, f *= i) + { + for (int j = f; j <= 1e+5; j++) + dp[j] = min(dp[j], dp[j - f] + 1); + } + while (scanf("%d", &N) == 1) + printf("%d\n", dp[N]); + return 0; +} diff --git a/12956.cpp b/12956.cpp new file mode 100644 index 0000000..0d0465c --- /dev/null +++ b/12956.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +// @ http://codeforces.com/blog/entry/20335#comment-251217 + +const int MAXN = 128; +const long long mod = 1e9 + 7; +long long C[MAXN][MAXN] = {}, dp[MAXN][MAXN]; +int N, K; + +long long dfs(int n, int k) +{ + // node label between [1, n+1], and label 1 as root which limied degree k + if (n <= 1) + return 1; + if (k == 0) + return 0; + if (k == 1) + return (n * dfs(n - 1, K - 1)) % mod; + if (dp[n][k] != -1) + return dp[n][k]; + long long &ret = dp[n][k]; + ret = 0; + // root 1 has at most $k$ subtrees + // pick one subtree has $i$ node (ways = C(n, i), and choose root of subtree way = i) + // dp(n, k) = \sum C(n, i) * i * dp(i-1, K-1) * dp(n-i, k-1) + // in order to eliminate overcounting, make picked subtree has smallest label. + // modify C(n, i) to C(n-1, i-1) + for (int i = 1; i <= n; i++) + { + long long sum = (C[n - 1][i - 1] * i) % mod; + sum *= dfs(i - 1, K - 1); + sum %= mod; + sum *= dfs(n - i, k - 1); + sum %= mod; + ret += sum; + ret %= mod; + } + return ret; +} + +int main() +{ + C[0][0] = 1; + for (int i = 0; i < MAXN; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; + } + + while (scanf("%d %d", &N, &K) == 2) + { + memset(dp, -1, sizeof(dp)); + long long ret = dfs(N - 1, K); + printf("%lld\n", ret); + } + return 0; +} diff --git a/12957.cpp b/12957.cpp new file mode 100644 index 0000000..c7ce3fd --- /dev/null +++ b/12957.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +struct Pt +{ + int x, y; + Pt(int a = 0, int b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + bool operator<(const Pt &a) const + { + if (x != a.x) + return x < a.x; + return y < a.y; + } +}; +int main() +{ + int n, x, y; + Pt P[4096], L[4096], R[4096]; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d %d", &x, &y), P[i] = Pt(x, y); + sort(P, P + n); + + int ret = 0; + for (int i = 0; i < n; i++) + { + int l = 0, r = 0; + for (int j = 0; j < n; j++) + { + if (P[i].x == P[j].x) + continue; + if (P[i].y == P[j].y) + continue; + if (P[j].x < P[i].x) + L[l++] = P[j] - P[i]; + else + R[r++] = P[j] - P[i]; + } + int Ay, By; + Ay = 0x3f3f3f3f, By = -0x3f3f3f3f; + for (int i = 0; i < r; i++) + { + if (R[i].y > 0) + { + if (R[i].y < Ay) + Ay = R[i].y, ret++; + } + else + { + if (R[i].y > By) + By = R[i].y, ret++; + } + } + Ay = 0x3f3f3f3f, By = -0x3f3f3f3f; + for (int i = l - 1; i >= 0; i--) + { + if (L[i].y > 0) + { + if (L[i].y < Ay) + Ay = L[i].y, ret++; + } + else + { + if (L[i].y > By) + By = L[i].y, ret++; + } + } + } + printf("%d\n", ret / 2); + } + return 0; +} diff --git a/12959.cpp b/12959.cpp new file mode 100644 index 0000000..8f15451 --- /dev/null +++ b/12959.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +int main() +{ + int J, R, x; + while (scanf("%d %d", &J, &R) == 2) + { + int S[512] = {}, L[512] = {}; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < J; j++) + scanf("%d", &x), S[j] += x, L[j] = x; + } + int ret = 0; + for (int i = 1; i < J; i++) + { + if (S[i] > S[ret] || (S[i] == S[ret] && L[i] >= L[ret])) + ret = i; + } + printf("%d\n", ret + 1); + } + return 0; +} diff --git a/12960.cpp b/12960.cpp new file mode 100644 index 0000000..66fb289 --- /dev/null +++ b/12960.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +const int MAXN = 2048; +pair dp[MAXN][MAXN]; + +int main() +{ + char s[MAXN]; + int sp[MAXN]; + while (scanf("%s", s) == 1) + { + int n = (int)strlen(s), m, x; + memset(sp, 0, sizeof(sp)); + scanf("%d", &m); + for (int i = 0; i < m; i++) + scanf("%d", &x), sp[x - 1] = 1; + + pair ret(0, 0); + for (int k = 0; k < n; k++) + { + for (int i = 0; i + k < n; i++) + { + int l = i, r = i + k; + if (l == r) + { + dp[l][r] = make_pair(sp[l], 1); + } + else + { + dp[l][r] = max(dp[l + 1][r], dp[l][r - 1]); + if (s[l] == s[r]) + { + pair tmp(dp[l + 1][r - 1].first + sp[l] + sp[r], dp[l + 1][r - 1].second + 2); + dp[l][r] = max(dp[l][r], tmp); + } + } + ret = max(ret, dp[l][r]); + } + } + printf("%d\n", ret.second); + } + return 0; +} diff --git a/12961.cpp b/12961.cpp new file mode 100644 index 0000000..887b17b --- /dev/null +++ b/12961.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +class XOR_GAUSSIAN +{// XOR Gaussian Elimination +public: + static const int MAXN = 32767, MAXM = 64; + char mtx[MAXN][MAXM + 1], varX[MAXN]; + int compute(int n, int m) + { + int row = 0, col = 0, arb = 0; + int equ = n, var = m; + while (row < equ && col < var) + { + int c = row; + for (int i = row; i < equ; i++) + if (mtx[i][col]) + c = i; + for (int i = 0; i <= var; i++) + swap(mtx[c][i], mtx[row][i]); + if (mtx[row][col] == 0) + { + col++, arb++; + continue; + } + for (int i = 0; i < equ; i++) + { + if (i == row || mtx[i][col] == 0) + continue; + for (int j = var; j >= 0; j--) + mtx[i][j] ^= mtx[row][j]; + } + row++, col++; + } + return row; + memset(varX, 0, sizeof(varX)); + for (int i = 0, j; i < equ; i++) + { + if (mtx[i][var] == 0) + continue; + for (j = 0; j < var && mtx[i][j] == 0; j++) + ; + varX[j] = mtx[i][var]; + } + } +} gauss; + +int main() +{ + int n, m, x; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + scanf("%d", &x); + gauss.mtx[i][j] = x & 1; + } + } + + int rank = gauss.compute(n, m); + if (rank < m) + puts("S"); + else + { + if (n > rank) + puts("N"); // for zeroes + else + puts("S"); + } + } + return 0; +} diff --git a/12962.cpp b/12962.cpp new file mode 100644 index 0000000..e96dbf5 --- /dev/null +++ b/12962.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + static char s[16777216]; + while (scanf("%s", s) == 1 && s[0] != '0') + { + int past[128]; + long long sum = 0, cnt = 0; + memset(past, -1, sizeof(past)); + for (int i = 0; s[i]; i++) + { + int l = past[s[i]], r = i; + int dist = 0; + for (int j = 'a'; j <= 'z'; j++) + dist += past[j] > l; + if (l != -1) + sum += dist, cnt++; + past[s[i]] = i; + } + if (cnt == 0) + puts("NaN"); + else + printf("%.4lf\n", (double)sum / cnt); + } + return 0; +} diff --git a/12963.cpp b/12963.cpp new file mode 100644 index 0000000..2247df0 --- /dev/null +++ b/12963.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +#define MAX 160 + +typedef unsigned long long ULL; +typedef vector VI; +typedef vector VVI; + +char mat[MAX][MAX]; + +bool find_match(int i, const VVI &w, VI &mr, VI &mc, VI &seen) +{ + for (int j = 0; j < w[i].size(); j++) + { + if (w[i][j] && !seen[j]) + { + seen[j] = true; + if (mc[j] < 0 || find_match(mc[j], w, mr, mc, seen)) + { + mr[i] = j; + mc[j] = i; + return true; + } + } + } + return false; +} + +int bipartite_match(const VVI &w, VI &mr, VI &mc) +{ + mr = VI(w.size(), -1); + mc = VI(w[0].size(), -1); + int ct = 0; + for (int i = 0; i < w.size(); i++) + { + VI seen(w[0].size()); + if (find_match(i, w, mr, mc, seen)) + { + ct++; + } + } + return ct; +} + +int main() +{ + ULL astragalus, garbancillos; + const ULL gar_keep = 0xfffffffffffffffLL, ast_keep = 0x3ffffffffffffLL; + const char *ast = "ASTRAGALUS", *gar = "GARBANCILLOS"; + const int ast_len = strlen(ast), gar_len = strlen(gar); + astragalus = 0; + for (int i = 0; ast[i]; i++) + { + astragalus <<= 5; + astragalus |= ast[i] - 'A'; + } + garbancillos = 0; + for (int i = 0; gar[i]; i++) + { + garbancillos <<= 5; + garbancillos |= gar[i] - 'A'; + } + + int T; + scanf("%d", &T); + while (T--) + { + int R, C; + scanf("%d%d", &R, &C); + gets(&mat[0][0]); + for (int i = 0; i < R; i++) + { + gets(&mat[i][0]); + } + if (R < ast_len || C < gar_len) + { + printf("0\n"); + continue; + } + vector gar_starts[MAX]; + int garban_n = 0; + map garban_indices; + int j, i; + for (j = 0; j < R; j++) + { + ULL h = 0; + for (i = 0; i < gar_len - 1; i++) + { + h <<= 5; + h |= (mat[j][i] < 'A' || mat[j][i] > 'Z' ? 0x1f : mat[j][i] - 'A'); + } + for (; i < C; i++) + { + h = ((h << 5) & gar_keep) | (mat[j][i] < 'A' || mat[j][i] > 'Z' ? 0x1f : mat[j][i] - 'A'); + if (h == garbancillos) + { + gar_starts[j].push_back(i); + garban_indices[j * MAX + i] = garban_n++; + } + } + gar_starts[j].push_back(MAX + 200);// avoid edge case later + } + VVI used; + for (i = 0; i < C; i++) + { + ULL h = 0; + for (j = 0; j < ast_len - 1; j++) + { + h <<= 5; + h |= (mat[j][i] < 'A' || mat[j][i] > 'Z' ? 0x1f : mat[j][i] - 'A'); + } + for (; j < R; j++) + { + h = ((h << 5) & ast_keep) | (mat[j][i] < 'A' || mat[j][i] > 'Z' ? 0x1f : mat[j][i] - 'A'); + if (h == astragalus) + { + VI this_astragalus(garban_n, 0); + for (int k = j - ast_len + 1; k <= j; k++) + { + int gst = *lower_bound(gar_starts[k].begin(), gar_starts[k].end(), i); + if (gst < i + gar_len) + { + this_astragalus[garban_indices[k * MAX + gst]] = 1; + } + } + used.push_back(this_astragalus); + } + } + } + VI a, b; + if (used.size() == 0 || used[0].size() == 0) + { + printf("0\n"); + } + else + { + printf("%d\n", bipartite_match(used, a, b)); + } + } + return 0; +} diff --git a/12965.cpp b/12965.cpp new file mode 100644 index 0000000..1315c07 --- /dev/null +++ b/12965.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int A[131072], B[131072], C[131072]; + +int main() +{ + int testcase, n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + scanf("%d", &A[i]), C[i] = A[i]; + for (int i = 0; i < m; i++) + scanf("%d", &B[i]), C[i + n] = B[i]; + C[n + m] = 0; + sort(A, A + n); + sort(B, B + m); + sort(C, C + n + m + 1); + + int price = 0, angry = INT_MAX; + int idx1 = 0, idx2 = 0; + for (int i = 0; i <= n + m; i++) + { + int test = C[i]; + while (idx1 < n && A[idx1] <= test) + idx1++; + while (idx2 < m && B[idx2] < test) + idx2++; + int v = n - idx1 + idx2; + if (v < angry) + angry = v, price = test; + } + printf("%d %d\n", price, angry); + } + return 0; +} diff --git a/12966.cpp b/12966.cpp new file mode 100644 index 0000000..731dd3c --- /dev/null +++ b/12966.cpp @@ -0,0 +1,285 @@ +#include + +using namespace std; + +#define pb push_back +#define mp make_pair +#define eps 1e-9 +#define pi acos(-1.0) +#define ff first +#define ss second +#define re return +#define QI queue +#define SI stack +#define SZ(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define sq(a) ((a) * (a)) +#define distance(a, b) (sq(a.x - b.x) + sq(a.y - b.y)) +#define iseq(a, b) (fabs(a - b) < eps) +#define eq(a, b) iseq(a, b) +#define ms(a, b) memset((a), (b), sizeof(a)) +#define G() getchar() +#define MAX3(a, b, c) max(a, max(b, c)) +#define II ({ int a ; read(a) ; a; }) +#define LL ({ Long a ; read(a) ; a; }) +#define DD ({double a; scanf("%lf", &a); a; }) + +double const EPS = 3e-8; + +typedef long long Long; +typedef long long int64; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +// For loop + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +template +T gcd(T a, T b) +{ + return (b != 0 ? gcd(b, a % b) : a); +} +template +T lcm(T a, T b) +{ + return (a / gcd(a, b) * b); +} + +//Fast Reader +template +inline bool read(T &x) +{ + int c = getchar(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = getchar(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction + +/* ************************************** My code start here ****************************************** */ + +const int NX = 55; + +int inp[NX][NX], n, m, r, c, mv; + +int MX[NX][NX]; +vector adj[(NX * NX) + 10]; +int dp[(NX * NX)][NX], vis[(NX * NX)][NX], cs; +int Ase[(NX * NX) + 10]; + +int mxi(int x, int y) +{ + int svx = x, svy = y; + int mx = -1; + int tx, ty; + int idx = (x * n) + y; + Ase[idx] = inp[x][y]; + adj[idx].clear(); + while (x + 1 < n && y + 1 < n) + { + x += 1; + y += 1; + if (mx < inp[x][y]) + { + mx = inp[x][y]; + adj[idx].clear(); + adj[idx].pb((x * n) + y); + } + else if (mx == inp[x][y]) + { + adj[idx].pb((x * n) + y); + } + } + x = svx, y = svy; + while (x + 1 < n && y - 1 >= 0) + { + x += 1; + y -= 1; + if (mx < inp[x][y]) + { + mx = inp[x][y]; + adj[idx].clear(); + adj[idx].pb((x * n) + y); + } + else if (mx == inp[x][y]) + { + adj[idx].pb((x * n) + y); + } + } + x = svx, y = svy; + while (x - 1 >= 0 && y + 1 < n) + { + x -= 1; + y += 1; + if (mx < inp[x][y]) + { + mx = inp[x][y]; + adj[idx].clear(); + adj[idx].pb((x * n) + y); + } + else if (mx == inp[x][y]) + { + adj[idx].pb((x * n) + y); + } + } + x = svx, y = svy; + while (x - 1 >= 0 && y - 1 >= 0) + { + x -= 1; + y -= 1; + if (mx < inp[x][y]) + { + mx = inp[x][y]; + adj[idx].clear(); + adj[idx].pb((x * n) + y); + } + else if (mx == inp[x][y]) + { + adj[idx].pb((x * n) + y); + } + } + Ase[idx] = mx; +} + +int DP(int idx, int mv) +{ + if (mv == 0) + return 0; + int &v = vis[idx][mv]; + int &ret = dp[idx][mv]; + if (v == cs) + return ret; + v = cs; + ret = 0; + int sz = adj[idx].size(); + rep(i, sz) + { + int u = adj[idx][i]; + ret = max(ret, DP(u, mv - 1) + Ase[idx]); + } + return ret; +} + +int main() +{ + // I will always use scanf and printf + // May be i won't be a good programmer but i will be a good human being + + int t = II; + for (cs = 1; cs <= t; cs++) + { + n = II, mv = II; + c = II, r = II; + //go.clear(); + int idx = (r * n) + c; + rep(i, n) + { + rep(j, n) inp[i][j] = II; + } + rep(i, n) + { + rep(j, n) mxi(i, j); + } + /* + int mx = 0 ; + int x = r , y = c ; + int svx = x , svy = y ; + while( x + 1 < n && y + 1< n ) + { + x += 1 ; + y += 1 ; + int tmp = ( inp[ x ][ y ] * ( mv / 2 ) ) + ( mv % 2 ? inp[ x ][ y ] : 0 ); + tmp += ( MX[ x ][ y ] * ( mv/2 ) ); + mx = max( mx , tmp ); + } + x = svx , y = svy ; + while( x + 1 < n && y - 1 >= 0 ) + { + x += 1 ; + y -= 1 ; + int tmp = ( inp[ x ][ y ] * ( mv / 2 ) ) + ( mv % 2 ? inp[ x ][ y ] : 0 ); + tmp += ( MX[ x ][ y ] * ( mv/2 ) ); + mx = max( mx , tmp ); + } + x = svx , y = svy ; + while( x - 1 >= 0 && y + 1 < n ) + { + x -= 1 ; + y += 1 ; + int tmp = ( inp[ x ][ y ] * ( mv / 2 ) ) + ( mv % 2 ? inp[ x ][ y ] : 0 ); + tmp += ( MX[ x ][ y ] * ( mv/2 ) ); + mx = max( mx , tmp ); + } + x = svx , y = svy ; + while( x - 1 >= 0 && y - 1 >= 0 ) + { + x -= 1 ; + y -= 1 ; + int tmp = ( inp[ x ][ y ] * ( mv / 2 ) ) + ( mv % 2 ? inp[ x ][ y ] : 0 ); + tmp += ( MX[ x ][ y ] * ( mv/2 ) ); + mx = max( mx , tmp ); + } */ + + printf("%d\n", DP(idx, mv)); + } + return 0; +} +/* +9 +1 2 3 2 3 4 +2 1 0 +3 0 2 5 6 +4 1 3 7 8 9 +5 3 0 +6 0 0 +7 0 0 +8 2 0 +9 0 0 +9 +1 0 3 2 3 4 +2 0 0 +3 0 2 5 6 +4 9 3 7 8 9 +5 0 0 +6 0 0 +7 0 0 +8 0 0 +9 0 0 +9 +1 0 3 2 3 4 +2 9 0 +3 0 2 5 6 +4 0 3 7 8 9 +5 0 0 +6 0 0 +7 0 0 +8 0 0 +9 0 0 +0 +*/ diff --git a/12967.cpp b/12967.cpp new file mode 100644 index 0000000..92e025a --- /dev/null +++ b/12967.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + long long dp[32] = {0, 1, 4, 12, 28}; + for (int n = 5; n <= 30; n++) + dp[n] = dp[n - 1] * 2 + 4; + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + printf("%lld\n", dp[n]); + } + return 0; +} diff --git a/12968.cpp b/12968.cpp new file mode 100644 index 0000000..556e875 --- /dev/null +++ b/12968.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define NUM_PLAYERS 10 +#define NUM_FEATURES 12 + +int P[NUM_PLAYERS + 1][NUM_FEATURES + 1]; + +long double getScore(const vector &team) +{ + long double geoMean = 1.0; + for (int i = 0; i < NUM_FEATURES; ++i) + { + int maxFeature = -1; + for (int j = 0; j < team.size(); ++j) + { + if (P[team[j]][i] > maxFeature) + { + maxFeature = P[team[j]][i]; + } + } + geoMean *= double(maxFeature); + } + geoMean = pow(geoMean, 1.0 / double(NUM_FEATURES)); + return geoMean; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + for (int i = 0; i < NUM_PLAYERS; ++i) + { + for (int j = 0; j < NUM_FEATURES; ++j) + { + scanf("%d", &P[i][j]); + } + } + int base_team[NUM_PLAYERS] = {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}; + vector> teams; + do + { + vector team; + for (int i = 0; i < NUM_PLAYERS; ++i) + { + if (base_team[i]) + { + team.push_back(i); + } + } + teams.push_back(team); + } while (next_permutation(base_team, base_team + NUM_PLAYERS)); + int bestTeam = -1; + long double maxScore = -1.0; + for (int i = teams.size() - 1; i >= 0; --i) + { + const vector &team = teams[i]; + long double currentScore = getScore(team); + if (currentScore > maxScore) + { + bestTeam = i; + maxScore = currentScore; + } + } + bool first = true; + for (int i = 0; i < teams[bestTeam].size(); ++i) + { + if (!first) + { + printf(" "); + } + else + { + first = false; + } + printf("%d", 4 + teams[bestTeam][i]); + } + printf("\n"); + } + return 0; +} diff --git a/12970.cpp b/12970.cpp new file mode 100644 index 0000000..5e0b989 --- /dev/null +++ b/12970.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +struct Frac +{ + long long x, y; + Frac(long long a = 0, long long b = 1) + { + x = a, y = b; + normal(); + } + void normal() + { + if (y < 0) + x = -x, y = -y; + long long g = llgcd(x, y); + x /= g, y /= g; + if (y < 0) + x = -x, y = -y; + } + long long llgcd(long long x, long long y) const + { + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; + } + Frac operator-(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x - vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator+(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x + vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator*(const Frac a) const + { + long long g1 = llgcd(x, a.y), g2 = llgcd(a.x, y); + long long va = 0, vb = 0; + va = (x / g1) * (a.x / g2); + vb = (y / g2) * (a.y / g1); + return Frac(va, vb); + } + Frac operator/(const Frac a) const + { + long long g1 = llgcd(y, a.y), g2 = llgcd(x, a.x); + long long va = 0, vb = 0; + va = (a.y / g1) * (x / g2); + vb = (y / g1) * (a.x / g2); + return Frac(va, vb); + } + bool operator==(const Frac &a) const + { + return x - a.x == 0 && y - a.y == 0; + } + bool operator<(const Frac &a) const + { + return x * a.y < a.x * y; + } + void print() + { + if (y == 1) + printf("%lld", x); + else + printf("%lld/%lld", x, y); + } +}; + +int main() +{ + int cases = 0; + long long x, y, a, b; + while (scanf("%lld %lld %lld %lld", &x, &y, &a, &b) == 4) + { + if (!x && !y && !a && !b) + return 0; + printf("Case #%d: ", ++cases); + Frac p(y, x), q(b, a), r = (p + q) * Frac(1, 2); + if (p < q) + puts("You owe me a beer!"); + else + puts("No beer for the captain."); + printf("Avg. arrival time: "); + r.print(); + puts(""); + } + return 0; +} diff --git a/12972.cpp b/12972.cpp new file mode 100644 index 0000000..056b43f --- /dev/null +++ b/12972.cpp @@ -0,0 +1,244 @@ +#include + +using namespace std; + +#define pb push_back +#define mp make_pair +#define eps 1e-9 +#define pi acos(-1.0) +#define ff first +#define ss second +#define re return +#define QI queue +#define SI stack +#define SZ(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define sq(a) ((a) * (a)) +#define distance(a, b) (sq(a.x - b.x) + sq(a.y - b.y)) +#define iseq(a, b) (fabs(a - b) < eps) +#define eq(a, b) iseq(a, b) +#define ms(a, b) memset((a), (b), sizeof(a)) +#define G() getchar() +#define MAX3(a, b, c) max(a, max(b, c)) +#define II ({ int a ; read(a) ; a; }) +#define LL ({ Long a ; read(a) ; a; }) +#define DD ({double a; scanf("%lf", &a); a; }) + +double const EPS = 3e-8; + +typedef long long Long; +typedef long long int64; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +// For loop + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +template +T gcd(T a, T b) +{ + return (b != 0 ? gcd(b, a % b) : a); +} +template +T lcm(T a, T b) { return (a / gcd(a, b) * b); } + +//Fast Reader +template +inline bool read(T &x) +{ + int c = getchar(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = getchar(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +int dx[] = {1, 0, -1, 0}; +int dy[] = {0, 1, 0, -1};//4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction + +/* ************************************** My code start here ****************************************** */ + +const int maxn = 1e5 + 10; +const int MX = 1e3 + 10; +const int NIL = 0; +const int INF = (1 << 29); + +vector g[maxn]; +int match[maxn], dist[maxn], inf_size, sz, n, m, k, idx; + +int Idx, Val[MX][MX]; +char inp[MX][MX]; +bool ok(int x, int y) +{ + if (x < 0 || y < 0 || x >= n || y >= m) + return 0; + return 1; +} + +bool bfs() +{ + int x, y, i; + queue q; + for (i = 1; i <= Idx; i++) + { + if (match[i] == NIL) + { + dist[i] = 0; + q.push(i); + } + else + dist[i] = INF; + } + dist[NIL] = INF; + while (!q.empty()) + { + x = q.front(); + q.pop(); + if (x != NIL) + { + int sz = g[x].size(); + rep(i, sz) + { + y = g[x][i]; + if (dist[match[y]] == INF) + { + dist[match[y]] = dist[x] + 1; + q.push(match[y]); + } + } + } + } + return dist[NIL] != INF; +} +bool dfs(int x) +{ + + if (x != NIL) + { + int i; + int sz = g[x].size(); + rep(i, sz) + { + int y = g[x][i]; + + if (dist[match[y]] == dist[x] + 1 && dfs(match[y])) + { + match[x] = y; + match[y] = x; + return 1; + } + } + dist[x] = INF; + return 0; + } + return 1; +} +int Hopcropt() +{ + int matching = 0, i; + for (int i = 1; i <= Idx; i++) + { + match[i] = 0; + } + + while (bfs()) + { + for (i = 1; i <= Idx; i++) + { + if (match[i] == 0 && dfs(i)) + { + matching++; + } + } + } + //printf("matching:: %d\n",matching); + return matching; +} + +void clearkori() +{ + for (int i = 0; i <= Idx + 2; i++) + g[i].clear(); +} +int main() +{ + // I will always use scanf and printf + // May be i won't be a good programmer but i will be a good human being + + int cs = 1; + while (scanf("%d %d", &n, &m) == 2) + { + if (n == 0 && m == 0) + break; + + Idx = 0; + rep(i, n) scanf("%s", inp[i]); + rep(i, n) + { + rep(j, m) if (inp[i][j] == '.') Val[i][j] = ++Idx; + } + rep(i, n) + { + rep(j, m) + { + if (inp[i][j] != '.') + continue; + int x = Val[i][j]; + for (int k = 0; k < 4; k++) + { + int nx = i + dx[k]; + int ny = j + dy[k]; + if (ok(nx, ny) && inp[nx][ny] == '.') + { + int y = Val[nx][ny]; + g[x].pb(y); + // printf(" x :: %d y :: %d\n" , x , y ); + } + } + } + } + if (Idx == 0) + { + printf("Case #%d: 0\n", cs++); + continue; + } + if (Idx == 1) + { + printf("Case #%d: 1\n", cs++); + continue; + } + int hc = Hopcropt(); + int ans = Idx - hc - hc; + // printf(" Idx :: %d hc :: %d\n" , Idx , hc ); + ans = (ans + 1) / 2; + printf("Case #%d: %d\n", cs++, ans); + + clearkori(); + } + + return 0; +} diff --git a/12974.cpp b/12974.cpp new file mode 100644 index 0000000..660106a --- /dev/null +++ b/12974.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +const int MAXS = 131072; + +struct SuffixArray +{ + int sa[MAXS], h[MAXS], n; + int w[MAXS], ta[MAXS], tb[MAXS]; + char str[MAXS]; + void sort(int *x, int *y, int m) + { + static int i; + for (i = 0; i < m; i++) + w[i] = 0; + for (i = 0; i < n; i++) + w[x[y[i]]]++; + for (i = 1; i < m; i++) + w[i] += w[i - 1]; + for (i = n - 1; i >= 0; i--) + sa[--w[x[y[i]]]] = y[i]; + } + bool cmp(int *r, int a, int b, int l) + { + if (r[a] == r[b]) + { + if (a + l >= n || b + l >= n) + return false; + return r[a + l] == r[b + l]; + } + return false; + } + void build_h() + { + int i, j, k; + for (i = 0; i < n; i++) + ta[sa[i]] = i; + for (i = 0; i < n; i++) + { + if (ta[i] == 0) + { + h[ta[i]] = 0; + continue; + } + if (i == 0 || h[ta[i - 1]] <= 1) + k = 0; + else + k = h[ta[i - 1]] - 1; + while (str[sa[ta[i] - 1] + k] == str[sa[ta[i]] + k]) + k++; + h[ta[i]] = k; + } + } + void build() + {// x: rank, y: second key + int i, k, m = 128, p; + int *x = ta, *y = tb, *z; + n = strlen(str); + x[n] = 0; + for (i = 0; i < n; i++) + x[i] = str[i], y[i] = i; + sort(x, y, m); + for (k = 1, p = 1; p < n; k *= 2, m = p) + { + for (p = 0, i = n - k; i < n; i++) + y[p++] = i; + for (i = 0; i < n; i++) + { + if (sa[i] >= k) + { + y[p++] = sa[i] - k; + } + } + sort(x, y, m); + z = x, x = y, y = z; + for (i = 1, p = 1, x[sa[0]] = 0; i < n; i++) + x[sa[i]] = cmp(y, sa[i - 1], sa[i], k) ? p - 1 : p++; + } + } +}; + +SuffixArray in; +const int MOD = 1e9 + 7; + +int main() +{ + int testcase, K, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s %d", in.str, &K); + + in.build(); + in.build_h(); + // for(int i = 0; i < in.n; i++) + // printf("%s %d\n", in.str + in.sa[i], in.h[i]); + long long ret = 0; + for (int i = 0; i < in.n;) + { + long long cnt = 0; + long long sum = in.n - in.sa[i] - K + 1; + if (sum > 0) + { + cnt += sum * (sum - 1) / 2, i++; + while (i < in.n && in.h[i] >= K) + { + long long t = in.n - in.sa[i] + 1 - K; + cnt += (sum * t) % MOD + t * (t - 1) / 2; + cnt %= MOD; + sum += t; + sum %= MOD; + i++; + } + ret += cnt; + ret %= MOD; + } + else + { + i++; + } + } + printf("Case #%d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/12976.cpp b/12976.cpp new file mode 100644 index 0000000..7b5d324 --- /dev/null +++ b/12976.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +typedef pair Doll; + +int main() +{ + int N, K, cases = 0; + while (scanf("%d %d", &N, &K) == 2) + { + if (N == 0 && K == 0) + return 0; + vector A; + for (int i = 0; i < N; i++) + { + int s, p; + scanf("%d %d", &s, &p); + A.push_back(make_pair(s, p)); + } + sort(A.begin(), A.end()); + int Ssize = 0; + double ret = 1e+30; + long long mx = 0, sum = 0; + for (int i = 0; i < N; i++) + { + if (Ssize >= K - 1) + { + // [WARN] don't use (\sum{size} / size_max) * price_max + ret = min(ret, (double)sum * A[i].second / A[i].first + A[i].second); + } + if (Ssize < K - 1) + { + sum += A[i].first; + Ssize++; + } + } + printf("Case #%d: %.6lf\n", ++cases, ret); + } + return 0; +} diff --git a/12978.cpp b/12978.cpp new file mode 100644 index 0000000..ff3a62d --- /dev/null +++ b/12978.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int N; // rows +int M; // columns +bool V[3][3];// visited +int S; // num solutions + +// a bit hardcoded here (assumes that the maximum is 3x3) +bool isSelectable(int ci, int cj, int li, int lj) +{ + if (li == ci) + { + if (abs(cj - lj) > 1 && !V[li][1]) + { + return false; + } + } + else if (lj == cj) + { + if (abs(ci - li) > 1 && !V[1][lj]) + { + return false; + } + } + else + { + if (abs(ci - li) == 2 && abs(cj - lj) == 2 && !V[1][1]) + { + return false; + } + } + + return true; +} + +void findSol(int lasti, int lastj) +{ + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < M; ++j) + { + if (!V[i][j]) + { + if ((lasti == -1 && lastj == -1) || + isSelectable(i, j, lasti, lastj)) + { + V[i][j] = true; + findSol(i, j); + V[i][j] = false; + ++S; + } + } + } + } +} + +int main() +{ + int T = 0; + + while (cin >> N >> M) + { + if (N == 0 && M == 0) + { + break; + } + + S = 0; + findSol(-1, -1); + cout << "Case #" << ++T << ": " << S << "\n"; + } + + return 0; +} diff --git a/1298.cpp b/1298.cpp new file mode 100644 index 0000000..1cfb0de --- /dev/null +++ b/1298.cpp @@ -0,0 +1,260 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double pi = acos(-1); + +typedef complex point; +typedef point vec; + +#define X real() +#define Y imag() + +struct line +{ + point s, t; + double ang; + line() {} + line(point a, point b) : s(a), t(b), ang(arg(b - a)) {} +}; + +// basic +int dcmp(double x) +{ + return x < -eps ? -1 : x > eps; +} +bool zero(vec v) +{ + return !dcmp(v.X) && !dcmp(v.Y); +} + +double sqr(double x) +{ + return x * x; +} +double dis(point a, point b) +{ + return abs(a - b); +} + +double cross(vec a, vec b) +{ + return a.X * b.Y - a.Y * b.X; +} +double cross(point a, point b, point c) +{ + return cross(b - a, c - a); +} +double dot(vec a, vec b) +{ + return a.X * b.X + a.Y * b.Y; +} +double dot(point a, point b, point c) +{ + return dot(b - a, c - a); +} + +vec dir(line ln) +{ + return ln.t - ln.s; +} +vec normal(vec v) +{ + return vec(-v.Y, v.X); +} +vec unit(vec v) +{ + return v / abs(v); +} + +vec proj(vec v, vec n) +{ + return n * dot(v, n) / norm(n); +} +point proj(point p, line ln) +{ + return ln.s + proj(p - ln.s, dir(ln)); +} +vec reflect(vec v, vec n) +{ + return proj(v, n) * 2.0 - v; +} +point reflect(point p, line ln) +{ + return ln.s + reflect(p - ln.s, dir(ln)); +} + +vec rotate(vec v, double a) +{ + return v * polar(1.0, a); +} +double angle(vec a, vec b) +{ + return arg(b / a); +} + +// line +double len(line ln) +{ + return abs(ln.s - ln.t); +} + +bool onseg(point p, line ln) +{ + return dcmp(cross(p, ln.s, ln.t)) == 0 && dcmp(dot(p, ln.s, ln.t)) <= 0; +} + +double distoline(point p, line ln) +{ + return fabs(cross(p, ln.s, ln.t)) / len(ln); +} +double distoseg(point p, line ln) +{ + if (dcmp(dot(ln.s, ln.t, p)) <= 0) + { + return dis(p, ln.s); + } + if (dcmp(dot(ln.t, ln.s, p)) <= 0) + { + return dis(p, ln.t); + } + return distoline(p, ln); +} + +bool inter(line a, line b, point &p) +{ + double s1 = cross(a.s, a.t, b.s); + double s2 = cross(a.s, a.t, b.t); + if (!dcmp(s1 - s2)) + { + return false; + } + p = (s1 * b.t - s2 * b.s) / (s1 - s2); + return true; +} + +bool seginter(line a, line b, point &p) // strict +{ + double s1 = cross(a.s, a.t, b.s), s2 = cross(a.s, a.t, b.t); + if ((dcmp(s1) ^ dcmp(s2)) != -2) + { + return false; + } + double s3 = cross(b.s, b.t, a.s), s4 = cross(b.s, b.t, a.t); + if ((dcmp(s3) ^ dcmp(s4)) != -2) + { + return false; + } + p = (s1 * b.t - s2 * b.s) / (s1 - s2); + return true; +} + +const int maxn = 1000; + +bool inhp(point p, line hp) +{ + return dcmp(cross(hp.s, hp.t, p)) > 0; +} + +bool cmpang(line a, line b) +{ + return dcmp(a.ang - b.ang) ? a.ang < b.ang : cross(a.s, a.t, b.s) < 0; +} + +void hpinter(line *q, int n, point *h, int &m) +{ + int head = 0, tail = 0; + m = 0; + sort(q, q + n, cmpang); + for (int i = 1; i < n; ++i) + { + if (!dcmp(q[i].ang - q[i - 1].ang)) + { + continue; + } + while (head < tail && !inhp(h[tail - 1], q[i])) + { + --tail; + } + while (head < tail && !inhp(h[head], q[i])) + { + ++head; + } + q[++tail] = q[i]; + if (head < tail) + { + inter(q[tail - 1], q[tail], h[tail - 1]); + } + } + while (head < tail && !inhp(h[tail - 1], q[head])) + { + --tail; + } + if (head < tail) + { + inter(q[tail], q[head], h[tail]); + } + for (int i = head; i <= tail; ++i) + { + h[m++] = h[i]; + } +} + +line makehp(double a, double b, double c) // ax + by + c > 0 +{ + point p1 = fabs(a) > fabs(b) ? point(-c / a, 0) : point(0, -c / b); + point p2 = p1 + vec(b, -a); + return line(p1, p2); +} + +int u[maxn], v[maxn], w[maxn]; +point tmp[maxn]; +line q[maxn]; +int n; + +bool can(int t) +{ + bool ok = true; + for (int i = 0; i < n; ++i) + if (i != t && u[i] >= u[t] && v[i] >= v[t] && w[i] >= w[t]) + { + ok = false; + break; + } + if (!ok) + { + return false; + } + int cnt = 0, m; + double k = 10000; + for (int i = 0; i < n; ++i) + if (i != t) + { + double a = (k / v[i] - k / w[i]) - (k / v[t] - k / w[t]); + double b = (k / u[i] - k / w[i]) - (k / u[t] - k / w[t]); + double c = k / w[i] - k / w[t]; + q[cnt++] = makehp(a, b, c); + } + q[cnt++] = line(point(0, 0), point(0, -1)); + q[cnt++] = line(point(0, 0), point(1, 0)); + q[cnt++] = line(point(0, 1), point(-1, 2)); + hpinter(q, cnt, tmp, m); + return m >= 3; +} + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + for (int i=0; i + +using namespace std; + +int match(int A[][2], int B[][2]) +{ + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + if (A[i][j] != B[i][j]) + return 0; + return 1; +} +void rotate(int A[][2]) +{ + int B[2][2]; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + B[1 - j][i] = A[i][j]; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + A[i][j] = B[i][j]; +} + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int A[2][2], B[2][2]; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + scanf("%d", &A[i][j]); + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + scanf("%d", &B[i][j]); + + int same = 0; + for (int i = 0; i < 4; i++) + { + if (match(A, B)) + same = 1, i = 4; + rotate(B); + } + printf("Case #%d: %s\n", ++cases, same ? "POSSIBLE" : "IMPOSSIBLE"); + } + return 0; +} diff --git a/12983.cpp b/12983.cpp new file mode 100644 index 0000000..efafaf0 --- /dev/null +++ b/12983.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +// i should use the bit to write +// because the recursion is very slow +// line segment tree if it is done n * m inquiries +// instead of checking n times each check m will be tle +// maintain num [k] for interval [i, j] with line segment tree +// the representative says that the ending value is in [i, j] +// the number of incremented sequences of length k + +#define MOD (int)(1e9 + 7) + +// do not re-arrange order +int N, M, clk = 0, A[1000], num[1001]; + +struct node +{ + int num[1001]; + node *l, *r; + node() + { + l = r = NULL; + for (int Mi = 1; Mi <= M; Mi++) + { + num[Mi] = 0; + } + } +}; +node *root, mem[2000]; + +node *get() +{ + mem[clk] = node(); + return mem + (clk++); +} + +node *build(int L, int R) +{ + node *r = get(); + if (L + 1 < R) + { + r->l = build(L, (L + R) / 2); + r->r = build((L + R) / 2, R); + } + return r; +} + +void add(node *O, int L, int R, int x) +{ + if (x + 1 <= L || R <= x) + { + return; + } + else if (x <= L && R <= x + 1) + { + for (int Mi = 1; Mi <= M; Mi++) + { + O->num[Mi] += num[Mi], O->num[Mi] %= MOD; + } + } + else + { + add(O->l, L, (L + R) / 2, x); + add(O->r, (L + R) / 2, R, x); + for (int Mi = 1; Mi <= M; Mi++) + { + O->num[Mi] = O->l->num[Mi] + O->r->num[Mi]; + O->num[Mi] %= MOD; + } + } +} + +void ask(node *O, int L, int R, int A, int B) +{ + if (B <= L || R <= A) + ; + else if (A <= L && R <= B) + { + for (int Mi = 1; Mi <= M; Mi++) + { + num[Mi] += O->num[Mi]; + num[Mi] %= MOD; + } + } + else + { + ask(O->l, L, (L + R) / 2, A, B); + ask(O->r, (L + R) / 2, R, A, B); + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + clk = 0; + scanf("%d%d", &N, &M); + vector v; + for (int Ni = 0; Ni < N; Ni++) + { + scanf("%d", &A[Ni]), v.push_back(A[Ni]); + } + sort(v.begin(), v.end()); + v.resize(unique(v.begin(), v.end()) - v.begin()); + map m; + for (int vi = 0; vi < v.size(); vi++) + { + m[v[vi]] = vi; + } + for (int Ni = 0; Ni < N; Ni++) + { + A[Ni] = m[A[Ni]]; + } + root = build(0, N); + for (int Ni = 0; Ni < N; Ni++) + { + for (int Mi = 1; Mi <= M; Mi++) + { + num[Mi] = 0; + } + ask(root, 0, N, 0, A[Ni]); + for (int Mi = M; Mi >= 2; Mi--) + { + num[Mi] = num[Mi - 1]; + } + num[1] = 1; + add(root, 0, N, A[Ni]); + } + for (int Mi = 1; Mi <= M; Mi++) + { + num[Mi] = 0; + } + ask(root, 0, N, 0, N); + printf("Case #%d: %d\n", t, num[M]); + } + return 0; +} diff --git a/12984.cpp b/12984.cpp new file mode 100644 index 0000000..09b165f --- /dev/null +++ b/12984.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +// place sticks on both sides; can be as long as half the length +// thus, it becomes possible to select two dp[][]s that become half +// do not want the same number of decimal places +// dp[i][j] records the sum of the lengths becomes i and j becomes half; can get value +// note special case that you can take any stick + +typedef long long LL; + +int A[1000], V[1000]; +LL DP[4001][3]; + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + int N, L; + scanf("%d%d", &N, &L); + for (int Ni = 0; Ni < N; Ni++) + { + scanf("%d%d", &A[Ni], &V[Ni]); + } + fill(DP[0], DP[L * 2] + 3, 0); + for (int Ni = 0; Ni < N; Ni++) + { + DP[L * 2][2] = max(DP[L * 2][2], (LL)V[Ni]); + } + for (int Ni = 0; Ni < N; Ni++) + { + for (int i = L * 2 - A[Ni] * 2; i >= 0; i--) + { + DP[i + A[Ni] * 2][2] = max(DP[i + A[Ni] * 2][2], DP[i][2] + V[Ni]); + } + for (int i = L * 2 - A[Ni]; i >= 0; i--) + { + DP[i + A[Ni]][2] = max(DP[i + A[Ni]][2], DP[i][1] + V[Ni]); + } + for (int i = L * 2 - A[Ni] * 2; i >= 0; i--) + { + DP[i + A[Ni] * 2][1] = max(DP[i + A[Ni] * 2][1], DP[i][1] + V[Ni]); + } + for (int i = L * 2 - A[Ni]; i >= 0; i--) + { + DP[i + A[Ni]][1] = max(DP[i + A[Ni]][1], DP[i][0] + V[Ni]); + } + for (int i = L * 2 - A[Ni] * 2; i >= 0; i--) + { + DP[i + A[Ni] * 2][0] = max(DP[i + A[Ni] * 2][0], DP[i][0] + V[Ni]); + } + } + printf("Case #%d: %lld\n", t, DP[L * 2][2]); + } + return 0; +} diff --git a/12987.cpp b/12987.cpp new file mode 100644 index 0000000..249f3b8 --- /dev/null +++ b/12987.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +typedef pair xy; + +const int vx[] = {-1, 0, 1, 0}; +const int vy[] = {0, 1, 0, -1}; + +char c[9][10]; +bool visit[9][9]; +vector v; + +void dfs(int x, int y) +{ + if (c[x][y] == '.') + { + v.push_back(xy(x, y)); + } + if (c[x][y] != 'o') + { + return; + } + visit[x][y] = true; + for (int vi = 0; vi < 4; vi++) + { + int nx = x + vx[vi], ny = y + vy[vi]; + if (nx < 0 || nx >= 9 || ny < 0 || ny >= 9) + { + continue; + } + if (visit[nx][ny]) + { + continue; + } + dfs(nx, ny); + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + fill(visit[0], visit[8] + 9, false); + for (int i = 0; i < 9; i++) + { + scanf("%s", c[i]); + } + bool flag = false; + for (int i = 0; i < 9 && !flag; i++) + { + for (int j = 0; j < 9 && !flag; j++) + { + if (c[i][j] == 'o' && !visit[i][j]) + { + v.clear(); + dfs(i, j); + sort(v.begin(), v.end()); + v.resize(unique(v.begin(), v.end()) - v.begin()); + if (v.size() == 1) + { + flag = true; + } + } + } + } + printf("Case #%d: Can", t); + if (!flag) + { + printf(" not"); + } + printf(" kill in one move!!!\n"); + } +} diff --git a/12988.cpp b/12988.cpp new file mode 100644 index 0000000..217a8cc --- /dev/null +++ b/12988.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +char c[4][5]; +bool row[4][5], col[4][5], blk[4][5]; + +bool DFS(int O) +{ + if (O == 16) + { + for (int i = 0; i < 4; i++) + puts(c[i]); + + return true; + } + else + { + int x = O / 4, y = O % 4, z = x / 2 * 2 + y / 2; + + if (c[x][y] != '*') + return DFS(O + 1); + else + { + for (int i = 1; i <= 4; i++) + { + if (row[x][i] || col[y][i] || blk[z][i]) + continue; + + row[x][i] = col[y][i] = blk[z][i] = true; + c[x][y] = '0' + i; + + if (DFS(O + 1)) + return true; + + row[x][i] = col[y][i] = blk[z][i] = false; + } + + c[x][y] = '*'; + return false; + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + + for (int Ti = 1; Ti <= T; Ti++) + { + for (int i = 0; i < 4; i++) + scanf("%s", c[i]); + + printf("Case #%d:\n", Ti); + + fill(row[0], row[3] + 5, false); + fill(col[0], col[3] + 5, false); + fill(blk[0], blk[3] + 5, false); + + for (int i = 0; i < 16; i++) + { + int x = i / 4, y = i % 4, z = x / 2 * 2 + y / 2, n = c[x][y] - '0'; + + if (c[x][y] == '*') + continue; + + row[x][n] = col[y][n] = blk[z][n] = true; + } + + DFS(0); + } +} diff --git a/12992.cpp b/12992.cpp new file mode 100644 index 0000000..b50a1bd --- /dev/null +++ b/12992.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + printf("Case #%d: %d\n", ++cases, n * 2 - 1); + } + return 0; +} diff --git a/12994.cpp b/12994.cpp new file mode 100644 index 0000000..8d1896c --- /dev/null +++ b/12994.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +const int N = 1e7 + 1; +bool np[N] = {false, false, true}; +long long primes[664600] = {2}, size = 1, t, n; + +int factor(long long n) +{ + int ans = 1; + + for (int i = 0; i < size && primes[i] * primes[i] <= n; ++i) + if (n % primes[i] == 0) + { + int t = 0; + while (n % primes[i] == 0) + n /= primes[i], ++t; + ans *= ++t; + } + + return ans << (n > 1); +} + +int *rep(long long n, int base) +{ + static int ans[50]; + int len = 0; + + memset(ans, 0, sizeof(ans)); + while (n) + ans[len++] = n % base, n /= base; + ans[len] = -1; + + return ans; +} + +bool pal(int *s) +{ + int len = std::find(s, s + 50, -1) - s; + if (len & 1) + return false; + for (int i = 0; i <= len / 2; ++i) + if (s[i] != s[len - i - 1]) + return false; + return true; +} + +bool sq(long long n) +{ + long long root = sqrt(n) + 1e-9; + return root * root == n; +} + +int offset(long long n) +{ + return n && sq(4 * n + 1) && int(sqrt(4 * n + 1) + 1e-9) % 2 == 1; +} + +int main() +{ + for (int i = 3; i * i < N; i += 2) + if (!np[i]) + for (int j = i * i; j < N; j += i + i) + np[j] = true; + for (int i = 3; i < N; i += 2) + if (!np[i]) + primes[size++] = i; + + scanf("%lld", &t); + for (int T = 1; T <= t && scanf("%lld", &n); ++T) + { + int ans = factor(n) / 2 - offset(n);// when len=2 + for (int base = cbrt(n); base > 1; --base) + if (pal(rep(n, base))) + ++ans;//, printf( "palindrome at %d\n", base ); + printf("Case %d: %d\n", T, ans); + } +} diff --git a/12995.cpp b/12995.cpp new file mode 100644 index 0000000..906c451 --- /dev/null +++ b/12995.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +#define MAXL (1000000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) + +int mark[MAXL], phi[1048576]; + +void sieve() +{ + register int i, j, k; + SET(1); + int n = 1000000; + phi[1] = 0; + for (i = 2; i <= n; i++) + phi[i] = i; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + for (j = i; j <= n; j += i) + phi[j] = phi[j] / i * (i - 1); + } + } +} + +long long ret[1048576]; + +int main() +{ + sieve(); + for (int i = 1; i <= 1000000; i++) + ret[i] = ret[i - 1] + phi[i]; + int n; + while (scanf("%d", &n) == 1 && n) + printf("%lld\n", ret[n]); + return 0; +} diff --git a/12996.cpp b/12996.cpp new file mode 100644 index 0000000..67effbf --- /dev/null +++ b/12996.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int N, L; + int A[20], B[20], S = 0, ret = 1; + scanf("%d %d", &N, &L); + for (int i = 0; i < N; i++) + scanf("%d", &A[i]), S += A[i]; + for (int i = 0; i < N; i++) + scanf("%d", &B[i]), ret &= A[i] <= B[i]; + ret &= S <= L; + printf("Case %d: %s\n", ++cases, ret ? "Yes" : "No"); + } + return 0; +} diff --git a/12997.cpp b/12997.cpp new file mode 100644 index 0000000..dcc34a1 --- /dev/null +++ b/12997.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int y1, y2, t, exit, d; + + scanf("%d", &t); + for (int n = 1; n <= t; ++n) + { + scanf("%d %d %d %d", &exit, &y2, &y1, &d); + double exp = 0, scale = 5e2; + int add = 0; + + if (y1 + d == y2) + { + double a = hypot(y1, exit), b = hypot(y1 + d, exit), s = (a + b + d) / 2; + exp += (2 * s * s - a * a - b * b) / 2 / d; + add = 1; + } + else + for (double step = (y2 - y1 - d) / scale, i = y1 + step / 2; i + d <= y2; i += step) + { + double a = hypot(i, exit), b = hypot(i + d, exit), s = (a + b + d) / 2; + exp += (2 * s * s - a * a - b * b) / 2 / d; + ++add; + } + printf("Case %d: %f\n", n, exp / add); + } +} diff --git a/12999.cpp b/12999.cpp new file mode 100644 index 0000000..7edd309 --- /dev/null +++ b/12999.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +struct Pt +{ + long long x, y; +}; +Pt arr[110], ans[110]; + +bool visit[111][111][111]; +double dp[111][111][111]; +int P, K, cnt; +stack S; + +double area_poly(vector lst) +{ + double area = 0; + for (int i = 1; i + 1 < lst.size(); i++) + { + area += (lst[i].y - lst[0].y) * (lst[i + 1].x - lst[i].x) - (lst[i].x - lst[0].x) * (lst[i + 1].y - lst[i].y); + } + return fabs(area / 2.0f); +} + +long long dist(Pt P, Pt Q) +{ + return hypot(P.x - Q.x, P.y - Q.y); +} + +int orient(Pt P, Pt Q, Pt R) +{ + long long ret = (Q.y - P.y) * (R.x - Q.x) - (Q.x - P.x) * (R.y - Q.y); + if (ret < 0) + { + return 2; + } + if (ret > 0) + { + return 1; + } + return ret; +} + +bool cmp(Pt X, Pt Y) +{ + int ret = orient(arr[0], X, Y); + if (ret == 0) + { + return dist(arr[0], X) < dist(arr[0], Y); + } + return ret == 2; +} + +Pt nxt_to_top() +{ + Pt P = S.top(); + S.pop(); + Pt res = S.top(); + S.push(P); + return res; +} + +double solve(int idx, int first, int prev, int how) +{ + if (idx < 0 || how == 0) + { + return 0.0f; + } + if (visit[idx][prev][how]) + { + return dp[idx][prev][how]; + } + visit[idx][prev][how] = true; + double &ret = dp[idx][prev][how]; + ret = 0.0f; + vector V{ans[first], ans[prev], ans[idx]}; + ret = max(ret, area_poly(V) + solve(idx - 1, first, idx, how - 1)); + ret = max(ret, solve(idx - 1, first, prev, how)); + return ret; +} + +double convex_hull(int N) +{ + int ymin = arr[0].y, idx = 0; + for (int i = 1; i < N; i++) + { + if (arr[i].y < ymin || (arr[i].y == ymin && arr[i].x < arr[idx].x)) + { + ymin = arr[i].y; + idx = i; + } + } + swap(arr[0], arr[idx]); + sort(&arr[1], &arr[N], cmp); + S.push(arr[0]); + for (int i = 1; i < N; i++) + { + while (S.size() > 1 && orient(nxt_to_top(), S.top(), arr[i]) != 2) + { + S.pop(); + } + S.push(arr[i]); + } + cnt = 0; + while (!S.empty()) + { + ans[cnt++] = S.top(); + S.pop(); + } + if (cnt <= K) + { + vector V; + for (int i = cnt - 1; i >= 0; i--) + { + V.push_back(ans[i]); + } + return area_poly(V); + } + double ans = 0.0f; + for (int i = cnt - 1; i >= 0; i--) + { + memset(visit, false, sizeof(visit)); + ans = max(ans, solve(i - 1, i, i, K - 1)); + } + return ans; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + scanf("%d%d", &P,&K); + for (int i = 0; i < P; i++) + { + scanf("%lld%lld", &arr[i].x,&arr[i].y); + } + printf("Case %d: %.3lf\n", t,convex_hull(P)); + } + return 0; +} diff --git a/130.cpp b/130.cpp index 6e69a52..243fcf6 100644 --- a/130.cpp +++ b/130.cpp @@ -1,48 +1,71 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int64 josephus(int64 n, int64 k){ - if(n==1) - return 0; - return (josephus(n-1, k) + k)%n; -} -int main(){ - while(true){ - int64 n, k; - scanf("%lld%lld", &n, &k); - if(n==0 && k==0) - break; - int64 val = josephus(n ,k) + 1; - printf("%lld\n", val); - } - return 0; -} +#include + +using namespace std; + +int queue_[101]; +int n, k; + +void roman() +{ + int cnt, i, nxt = -1, killed; + for (i = 0; i < n; i++) + { + queue_[i] = i + 1; + } + cnt = 0; + i = n; + while (i > 1) + { + cnt = 0; + while (cnt < k) + { + nxt = (nxt + 1) % n; + if (queue_[nxt]) + { + cnt++; + } + } + killed = nxt; + queue_[nxt] = 0; + cnt = 0; + nxt--; + while (cnt < k) + { + nxt = (nxt + 1) % n; + if (queue_[nxt]) + { + cnt++; + } + } + queue_[killed] = queue_[nxt]; + queue_[nxt] = 0; + nxt = killed; + i--; + } + for (i = 0; i < n; i++) + if (queue_[i]) + { + if (queue_[i] == 1) + { + printf("1\n"); + } + else + { + printf("%d\n", n - queue_[i] + 2); + } + break; + } +} + +int main() +{ + while (scanf("%d%d", &n, &k)) + { + if (n == 0 && k == 0) + { + break; + } + roman(); + } + return 0; +} diff --git a/13000.cpp b/13000.cpp new file mode 100644 index 0000000..04f18f5 --- /dev/null +++ b/13000.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const long long LLINF = 1LL << 62; + +typedef struct Edge +{ + int v; + long long cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, long long flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + long long Maxflow(int s, int t) + { + long long ret = 0; + int i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + long long update = LLINF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int M, N, K; + int W[64], V[64], R[64]; + vector WR[64]; + int sumVIP = 0, maxW = 0; + scanf("%d %d %d", &M, &N, &K); + for (int i = 0; i < N; i++) + scanf("%d", &W[i]), maxW = max(maxW, W[i]); + for (int i = 0; i < M; i++) + { + int n, x; + scanf("%d %d %d", &V[i], &R[i], &n); + WR[i].clear(); + for (int j = 0; j < n; j++) + { + scanf("%d", &x), x--; + WR[i].push_back(x); + } + sumVIP += V[i]; + } + + long long l = 0, r = (long long)maxW * (sumVIP + K), mid, ret = 0; + while (l <= r) + { + mid = (l + r) / 2; + + long long time = mid; + int source = N + 2 * M; + int sink1 = N + 2 * M + 1; // VIP + int sink2 = N + 2 * M + 2; // Regular + int sink = N + 2 * M + 3; + g.Init(N + 2 * M + 5); + for (int i = 0; i < N; i++) + { + g.Addedge(source, i, time / W[i]); + } + g.Addedge(sink1, sink, sumVIP); + g.Addedge(sink2, sink, K); + for (int i = 0; i < M; i++) + { + int u1 = N + 2 * i, u2 = N + 2 * i + 1; + g.Addedge(u1, sink1, V[i]); + g.Addedge(u2, sink2, R[i]); + for (int j = 0; j < WR[i].size(); j++) + { + int v = WR[i][j]; + g.Addedge(v, u1, LLINF); + g.Addedge(v, u2, LLINF); + } + } + + long long flow = g.Maxflow(source, sink); + if (flow == sumVIP + K) + r = mid - 1, ret = time; + else + l = mid + 1; + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/13001.cpp b/13001.cpp new file mode 100644 index 0000000..9ab4962 --- /dev/null +++ b/13001.cpp @@ -0,0 +1,271 @@ +#include + +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; + +#define INF 1000000 + +struct node +{ + LL st, end; +} seg[1000005]; + +LL Rp[100005], Gr[100005], Rv[100005], Gv[100005], pos[100005]; +vector res; + +int main() +{ + bool f; + int T, r, g, n, id; + LL st, end, s1, s2, t1, t2, ans; + + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + scanf("%d%d%d", &r, &g, &n); + res.clear(); + for (int i = 0; i < r; i++) + { + scanf("%lld", &Rp[i]); + } + for (int i = 0; i < r; i++) + { + scanf("%lld", &Rv[i]); + } + for (int i = 0; i < g; i++) + { + scanf("%lld", &Gr[i]); + } + for (int i = 0; i < g; i++) + { + scanf("%lld", &Gv[i]); + } + for (int i = 0; i < n; i++) + { + scanf("%lld", &pos[i]); + } + sort(pos, pos + n); + + seg[0].st = 0; + seg[0].end = pos[0]; + for (int i = 1; i < n; i++) + { + seg[i].st = seg[i - 1].end; + seg[i].end = pos[i]; + } + seg[n].st = seg[n - 1].end; + seg[n].end = 1000000000LL; + + f = false; + id = 0; + ans = 10000000000LL; + for (int i = 0; i <= n; i++) + { + st = 0; + end = 1000000007LL; + if (Rv[0] == 0) + { + if (Rp[0] >= seg[i].st && Rp[0] <= seg[i].end) + { + t1 = 0LL; + t2 = 10000000000LL; + } + else + { + continue; + } + } + else + { + s1 = seg[i].st - Rp[0]; + if (s1 <= 0) + { + t1 = 0; + } + else + { + t1 = s1 / Rv[0]; + if (s1 % Rv[0] != 0) + { + t1++; + } + } + s2 = seg[i].end - Rp[i]; + if (s2 < 0) + { + continue; + } + t2 = s2 / Rv[0]; + } + + st = max(st, t1); + end = min(end, t2); + + if (st > end) + { + continue; + } + + if (Gv[0] == 0) + { + if (Gr[0] >= seg[i].st && Gr[0] <= seg[i].end) + { + t1 = 0LL; + t2 = 10000000000LL; + } + else + { + continue; + } + } + else + { + s1 = Gr[0] - seg[i].end; + if (s1 <= 0) + { + t1 = 0LL; + } + else + { + t1 = s1 / Gv[0]; + if (s1 % Gv[0] != 0) + { + t1++; + } + } + s2 = Gr[0] - seg[i].st; + if (s2 < 0) + { + continue; + } + t2 = s2 / Gv[0]; + } + + st = max(st, t1); + end = min(end, t2); + + if (end >= st) + { + id = i; + res.push_back(i); + } + } + + int len = res.size(); + for (int j = 0; j < len; j++) + { + id = res[j]; + f = true; + st = 0LL; + end = 1000000007LL; + for (int i = 0; i < r; i++) + { + if (Rv[i] == 0) + { + if (Rp[i] >= seg[id].st && Rp[i] <= seg[id].end) + { + t1 = 0LL; + t2 = 10000000000LL; + } + else + { + f = false; + break; + } + } + else + { + s1 = seg[id].st - Rp[i]; + t1 = s1 / Rv[i]; + if (s1 % Rv[i] != 0) + { + t1++; + } + if (s1 < 0) + { + t1 = 0; + } + s2 = seg[id].end - Rp[i]; + if (s2 < 0) + { + f = false; + break; + } + t2 = s2 / Rv[i]; + } + + st = max(st, t1); + end = min(end, t2); + } + + if (f) + { + for (int i = 0; i < g; i++) + { + if (Gv[i] == 0) + { + if (Gr[i] >= seg[id].st && Gr[i] <= seg[id].end) + { + t1 = 0LL; + t2 = 10000000000LL; + } + else + { + f = false; + break; + } + } + else + { + s1 = Gr[i] - seg[id].end; + t1 = s1 / Gv[i]; + if (s1 % Gv[i] != 0) + { + t1++; + } + if (s1 < 0) + { + t1 = 0; + } + s2 = Gr[i] - seg[id].st; + if (s2 < 0) + { + f = false; + break; + } + t2 = s2 / Gv[i]; + } + + if (f) + { + st = max(st, t1); + end = min(end, t2); + } + } + + if (f && st <= end) + { + ans = min(ans, st); + break; + } + else + { + ans = 10000000000LL; + } + } + } + printf("Case %d: ", t); + if (ans == 10000000000LL) + { + printf("-1\n"); + } + else + { + printf("%lld\n", ans); + } + } + + return 0; +} diff --git a/13003.cpp b/13003.cpp new file mode 100644 index 0000000..3dfb4a3 --- /dev/null +++ b/13003.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +typedef long long LL; + +int dis[10001], pre[2][10001]; +vector vc[10001], vp[10001]; +pair, int> edges[100001]; +priority_queue, vector >, greater > > pq; + +int main() +{ + int T; + scanf("%d", &T); + for (int t = 1; t <= T; t++) + { + int n, m; + scanf("%d%d", &n, &m); + memset(dis, 126, sizeof(dis)); + for (int i = 0; i < n + 1; i++) + { + vc[i].clear(); + vp[i].clear(); + } + for (int i = 0; i < m; i++) + { + int x, y, w; + scanf("%d%d%d", &x, &y, &w); + vc[x].push_back(y); + vp[x].push_back(w); + edges[i] = make_pair(make_pair(x, y), w); + } + + pre[0][1] = 1; + pre[1][1] = -1; + dis[1] = 0; + pq.push(make_pair(0, 1)); + while (!pq.empty()) + { + pair p = pq.top(); + pq.pop(); + for (int i = 0; i < vc[p.second].size(); i++) + { + if (dis[vc[p.second][i]] > p.first + vp[p.second][i]) + { + dis[vc[p.second][i]] = p.first + vp[p.second][i]; + pre[0][vc[p.second][i]] = p.second; + pre[1][vc[p.second][i]] = -1; + pq.push(make_pair(dis[vc[p.second][i]], vc[p.second][i])); + } + else if (dis[vc[p.second][i]] == p.first + vp[p.second][i]) + { + if (p.second != pre[0][vc[p.second][i]]) + { + pre[1][vc[p.second][i]] = p.second; + } + } + } + } + + LL res = 0LL; + for (int i = 0; i < m; i++) + { + int x = edges[i].first.first, y = edges[i].first.second, w = edges[i].second; + if (dis[x] + w == dis[y]) + { + if (pre[1][y] != -1) + { + res -= (i + 1); + } + } + else + { + res -= (i + 1); + res += (LL)(i + 1) * (LL)(i + 1) * (LL)(dis[x] + w - dis[y]); + } + } + printf("Case %d: %lld\n", t, res); + } + + return 0; +} diff --git a/13004.cpp b/13004.cpp new file mode 100644 index 0000000..797af51 --- /dev/null +++ b/13004.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + long long n; + while (scanf("%lld", &n) == 1) + { + char buf[1024]; + sprintf(buf, "%lld", n); + int cnt[16] = {}; + int len = strlen(buf); + long long pre = 0; + for (int i = 0; i < len; i++) + { + int ch = -1; + for (int j = 9; j >= 0; j--) + { + if (cnt[j] == 2) + continue; + cnt[j]++; + int tlen = 0; + long long mn = j, ten = 10; + for (int k = 0; k < 10; k++) + { + for (int l = cnt[k]; l < 2; l++) + { + if (tlen < len - i - 1) + { + mn = mn * 10 + k; + tlen++, ten *= 10; + } + } + } + cnt[j]--; + if (pre * ten + mn <= n) + { + ch = j; + break; + } + } + if (ch != -1) + pre = pre * 10 + ch, cnt[ch]++; + } + printf("%lld\n", pre); + } + return 0; +} diff --git a/13005.cpp b/13005.cpp new file mode 100644 index 0000000..0e36c0e --- /dev/null +++ b/13005.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; + +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; + +int main() +{ + int N, Q, B, x; + while (scanf("%d %d", &N, &Q) == 2) + { + set> S; + set A[128]; + for (int i = 0; i < N; i++) + { + scanf("%d", &B); + for (int j = 0; j < B; j++) + { + scanf("%d", &x); + A[i].insert(x); + } + if (B != N) + A[i].insert(0); + } + + for (int i = 0; i < Q; i++) + { + int source = 2 * N + 2, sink = 2 * N + 3; + g.Init(2 * N + 5); + + int used[128] = {}; + for (int j = 0; j < N; j++)// parent + g.Addedge(source, j, 1); + + scanf("%d", &B); + for (int j = 0; j < B; j++) + { + scanf("%d", &x); + g.Addedge(N + x, sink, 1); + for (int k = 0; k < N; k++) + { + if (A[k].count(x)) + { + g.Addedge(k, N + x, 1); + used[k] = 1; + } + } + } + + int allused = 1; + for (int j = 0; j < N; j++) + { + if (A[j].count(0) && B != N) + used[j] = 1; + allused &= used[j]; + } + + if (!allused) + { + puts("N"); + continue; + } + + int flow = g.Maxflow(source, sink); + puts(flow == B ? "Y" : "N"); + } + } + return 0; +} diff --git a/13006.cpp b/13006.cpp new file mode 100644 index 0000000..f7a62fe --- /dev/null +++ b/13006.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +struct Pt +{ + long long x, y; + Pt(long long a = 0, long long b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } +}; +long long cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +long long area2(Pt o, Pt a, Pt b) +{ + return llabs(cross(o, a, b)); +} + +const int MAXN = 262144; +Pt P[MAXN]; + +int main() +{ + int N; + while (scanf("%d", &N) == 1) + { + for (int i = 0; i < N; i++) + { + scanf("%lld %lld", &P[i].x, &P[i].y); + P[i + N] = P[i]; + } + + long long area = 0; + for (int i = 2; i < N; i++) + area += area2(P[0], P[i - 1], P[i]); + + long long tmp = 0; + pair ret(0, 0); + tmp += P[0].x * P[1].y - P[0].y * P[1].x; + tmp += P[1].x * P[2].y - P[1].y * P[2].x; + for (int i = 0, j = 2; i < N; i++) + { + while (1) + { + long long test = tmp + (P[j].x * P[i].y - P[j].y * P[i].x); + if (llabs(test) * 2 >= area) + break; + tmp += P[j].x * P[j + 1].y - P[j].y * P[j + 1].x; + j++; + } + long long t1, t2, p1, p2; + t1 = llabs(tmp + (P[j].x * P[i].y - P[j].y * P[i].x)); + t2 = area - t1; + p1 = llabs(tmp - (P[j - 1].x * P[j].y - P[j - 1].y * P[j].x) + (P[j - 1].x * P[i].y - P[j - 1].y * P[i].x)); + p2 = area - p1; + if (t1 < t2) + swap(t1, t2); + if (p1 < p2) + swap(p1, p2); + if (t1 > p1) + t1 = p1, t2 = p2; + ret = max(ret, make_pair(t1, t2)); + tmp -= P[i].x * P[i + 1].y - P[i].y * P[i + 1].x; + } + printf("%lld %lld\n", ret.first, ret.second); + } + return 0; +} diff --git a/13007.cpp b/13007.cpp new file mode 100644 index 0000000..8aab695 --- /dev/null +++ b/13007.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int main() +{ + int n, m; + int A[5] = {1, 10, 100, 1000, 10000}; + while (scanf("%d %d", &n, &m) == 2) + { + int B, x, ret = 0; + for (int i = 0; i < m; i++) + { + int sum = 0, f; + scanf("%d %d", &B, &f), sum = f; + for (int j = 1; j < n; j++) + scanf("%d", &x), sum += x; + if (sum <= B) + { + for (int j = 4; j >= 0; j--) + { + if (sum - f + A[j] <= B) + ret += A[j] - f, j = -1; + } + } + else + { + for (int j = 4; j >= 0; j--) + { + if (sum - f + A[j] <= B) + ret += A[j], j = -1; + } + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13008.cpp b/13008.cpp new file mode 100644 index 0000000..55cd872 --- /dev/null +++ b/13008.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +int D, P, R, B; +int cD[128], cP[128]; +vector gD[128], gP[128]; +int used[2][128]; + +void dfs(int u, int flag, int isDSP, int *cnt, int *cost) +{ + if (used[isDSP][u]) + return; + used[isDSP][u] = 1; + if (isDSP) + *cost += cD[u]; + else + *cost += cP[u]; + if (flag) + (*cnt)++; + else + (*cnt)--; + if (isDSP) + { + for (int i = gD[u].size() - 1; i >= 0; i--) + dfs(gD[u][i], !flag, !isDSP, cnt, cost); + } + else + { + for (int i = gP[u].size() - 1; i >= 0; i--) + dfs(gP[u][i], !flag, !isDSP, cnt, cost); + } +} +int solve(int flag) +{ + vector> E; + memset(used, 0, sizeof(used)); + if (flag == 0) + { + for (int i = 0; i < P; i++) + { + if (used[0][i] == 0) + { + int cnt = 0, cost = 0; + dfs(i, 1, 0, &cnt, &cost); + E.push_back(make_pair(cnt, cost)); + } + } + } + else + { + for (int i = 0; i < D; i++) + { + if (used[1][i] == 0) + { + int cnt = 0, cost = 0; + dfs(i, 1, 1, &cnt, &cost); + E.push_back(make_pair(cnt, cost)); + } + } + } + + int dp[32767] = {}, ret = 0; + dp[0] = 0; + for (int i = 0; i < E.size(); i++) + { + int a = E[i].first, b = E[i].second; + for (int j = B; j >= b; j--) + dp[j] = max(dp[j], dp[j - b] + a); + } + for (int i = 0; i <= B; i++) + ret = max(ret, dp[i]); + return ret; +} + +int main() +{ + while (scanf("%d %d %d %d", &D, &P, &R, &B) == 4) + { + for (int i = 0; i < D; i++) + scanf("%d", &cD[i]), gD[i].clear(); + for (int i = 0; i < P; i++) + scanf("%d", &cP[i]), gP[i].clear(); + for (int i = 0; i < R; i++) + { + int x, y; + scanf("%d %d", &x, &y); + x--, y--; + gD[x].push_back(y), gP[y].push_back(x); + } + + int ret1 = solve(0) + D; + int ret2 = solve(1) + P; + printf("%d %d\n", ret1, ret2); + } + return 0; +} diff --git a/13009.cpp b/13009.cpp new file mode 100644 index 0000000..6a2d42c --- /dev/null +++ b/13009.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +struct Pt +{ + int x, y, v; + Pt(int a = 0, int b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator<(const Pt &a) const + { + if (x != a.x) + return x < a.x; + if (y != a.y) + return y < a.y; + return false; + } +}; +struct Seg +{ + int xl, xr, y; + Seg(int a = 0, int b = 0, int c = 0) : xl(a), xr(b), y(c) {} + bool operator<(const Seg &u) const + { + return xl < u.xl; + } +}; +Pt P[131072], D[131072]; +int BIT[262144]; +void modify(int x, int val, int L) +{ + while (x <= L) + BIT[x] += val, x += x & (-x); +} +int query(int x) +{ + int sum = 0; + while (x) + sum += BIT[x], x -= x & (-x); + return sum; +} +void solve(int N, int M) +{ + map RX, RY; + for (int i = 0; i < N; i++) + RX[P[i].x] = RY[P[i].y] = 0; + for (int i = 0; i < M; i++) + RX[D[i].x] = RY[D[i].y] = 0; + + int label_x = 0, label_y = 0; + + for (auto &x : RX) + x.second = ++label_x; + for (auto &y : RY) + y.second = ++label_y; + + memset(BIT, 0, sizeof(BIT)); + + for (int i = 0; i < N; i++) + P[i].x = RX[P[i].x], P[i].y = RY[P[i].y]; + for (int i = 0; i < M; i++) + D[i].x = RX[D[i].x], D[i].y = RY[D[i].y]; + + vector segs; + for (int i = 0; i < M; i++) + { + if (D[i].y == D[(i + 1) % M].y) + { + int xl = min(D[i].x, D[(i + 1) % M].x); + int xr = max(D[i].x, D[(i + 1) % M].x); + segs.push_back(Seg(xl, xr, D[i].y)); + } + } + + long long outer_val = 0; + sort(P, P + N); + sort(segs.begin(), segs.end()); + int Pidx = 0; + set> PQ; + for (int i = 0, line_x = 1; line_x <= label_x; line_x++) + { + while (Pidx < N && P[Pidx].x <= line_x) + { + int intersect = query(P[Pidx].y); + if (intersect % 2 == 0) + outer_val += P[Pidx].v; + Pidx++; + } + while (!PQ.empty() && PQ.begin()->first <= line_x) + modify(PQ.begin()->second, -1, label_y), PQ.erase(PQ.begin()); + while (i < segs.size() && segs[i].xl == line_x) + { + modify(segs[i].y, 1, label_y); + PQ.insert(make_pair(segs[i].xr, segs[i].y)); + i++; + } + } + printf("%lld\n", outer_val); +} + +int main() +{ + int N, M; + while (scanf("%d %d", &N, &M) == 2) + { + for (int i = 0; i < N; i++) + { + scanf("%d %d", &P[i].x, &P[i].y); + P[i].v = i + 1; + } + + for (int i = 0; i < M; i++) + scanf("%d %d", &D[i].x, &D[i].y); + + solve(N, M); + } + return 0; +} diff --git a/13010.cpp b/13010.cpp new file mode 100644 index 0000000..bbd5be3 --- /dev/null +++ b/13010.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +const int MAXV = 2048; +const int MAXE = 131072; +const long long INF = 1e+60; + +struct Edge +{ + int to, eid; + double w; + Edge *next; +}; +Edge edge[MAXE], *adj[MAXV]; + +double dist[MAXV]; +int e = 0, I[MAXE], J[MAXE], A[MAXE], B[MAXE]; + +void addEdge(int x, int y, double v) +{ + edge[e].to = y, edge[e].w = v, edge[e].eid = e; + edge[e].next = adj[x], adj[x] = &edge[e++]; +} + +void dijkstra(int st, double dist[], int n) +{ + typedef pair PLL; + for (int i = 0; i <= n; i++) + { + dist[i] = INF; + } + set pQ; + PLL u; + pQ.insert(PLL(0, st)), dist[st] = 0; + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + for (Edge *p = adj[u.second]; p; p = p->next) + { + if (dist[p->to] > dist[u.second] + p->w) + { + if (dist[p->to] != INF) + { + pQ.erase(pQ.find(PLL(dist[p->to], p->to))); + } + dist[p->to] = dist[u.second] + p->w; + pQ.insert(PLL(dist[p->to], p->to)); + } + } + } +} + +double f(int N, int M, double t) +{ + e = 0; + for (int i = 1; i <= N; i++) + { + adj[i] = NULL; + } + for (int i = 0; i < M; i++) + { + double cost = t * A[i] + B[i]; + addEdge(I[i], J[i], cost); + addEdge(J[i], I[i], cost); + } + dijkstra(1, dist, N); + return dist[N]; +} + +int main() +{ + int N, M; + while (scanf("%d%d", &N, &M) == 2) + { + for (int i = 0; i < M; i++) + { + scanf("%d%d%d%d", &I[i], &J[i], &A[i], &B[i]); + } + double l = 0.0f, r = 24 * 60, mid, midmid, md, mmd, ret = 0.0f; + for (int it = 0; it < 100; it++) + { + mid = (l + r) / 2; + midmid = (mid + r) / 2; + md = f(N, M, mid); + mmd = f(N, M, midmid); + ret = max(ret, md); + if (fabs(md - mmd) < EPS) + { + break; + } + if (md < mmd) + { + l = mid; + } + else + { + r = midmid; + } + } + printf("%.5lf\n", ret); + } + return 0; +} diff --git a/13011.cpp b/13011.cpp new file mode 100644 index 0000000..238328d --- /dev/null +++ b/13011.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +int used[128][128][5]; +int g[128][128] = {}, n, m; + +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +int valid(int x, int y) +{ + return x >= 1 && y >= 1 && x <= n && y <= m; +} + +int hasface(int x, int y, int k) +{ + if (k == 0) + return 1; + if (k == 1) + return !valid(x - 1, y) || g[x][y] > g[x - 1][y]; + if (k == 2) + return !valid(x + 1, y) || g[x][y] > g[x + 1][y]; + if (k == 3) + return !valid(x, y - 1) || g[x][y] > g[x][y - 1]; + if (k == 4) + return !valid(x, y + 1) || g[x][y] > g[x][y + 1]; +} +int hasconnect(int ax, int ay, int bx, int by, int f) +{ + int lx, ly, rx, ry; + if (f == 1) + { + lx = g[ax - 1][ay], rx = g[ax][ay]; + ly = g[bx - 1][by], ry = g[bx][by]; + } + else if (f == 2) + { + lx = g[ax + 1][ay], rx = g[ax][ay]; + ly = g[bx + 1][by], ry = g[bx][by]; + } + else if (f == 3) + { + lx = g[ax][ay - 1], rx = g[ax][ay]; + ly = g[bx][by - 1], ry = g[bx][by]; + } + else if (f == 4) + { + lx = g[ax][ay + 1], rx = g[ax][ay]; + ly = g[bx][by + 1], ry = g[bx][by]; + } + return max(lx, ly) < min(rx, ry); +} +void dfs(int x, int y, int f) +{ + if (!valid(x, y) || used[x][y][f]) + return; + used[x][y][f] = 1; + if (f == 0) + { + for (int i = 0; i < 4; i++) + { + int tx = x + dx[i], ty = y + dy[i]; + if (!valid(tx, ty)) + continue; + if (g[x][y] == g[tx][ty]) + dfs(tx, ty, f); + } + } + else + { + if (f == 1 || f == 2) + { + for (int i = 0; i < 2; i++) + { + int tx = x + dx[i], ty = y + dy[i]; + if (!valid(tx, ty)) + continue; + if (hasface(tx, ty, f) && hasconnect(x, y, tx, ty, f)) + dfs(tx, ty, f); + } + } + else + { + for (int i = 2; i < 4; i++) + { + int tx = x + dx[i], ty = y + dy[i]; + if (!valid(tx, ty)) + continue; + if (hasface(tx, ty, f) && hasconnect(x, y, tx, ty, f)) + dfs(tx, ty, f); + } + } + } +} +int countFace(int n, int m) +{ + int faces = 0; + for (int k = 0; k < 5; k++) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (used[i][j][k]) + continue; + if (hasface(i, j, k)) + { + dfs(i, j, k); + faces++; + } + } + } + } + return faces + 1; +} + +int main() +{ + while (scanf("%d %d", &n, &m) == 2) + { + memset(g, 0, sizeof(g)); + memset(used, 0, sizeof(used)); + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + scanf("%d", &g[i][j]); + printf("%d\n", countFace(n, m)); + } + return 0; +} diff --git a/13012.cpp b/13012.cpp new file mode 100644 index 0000000..e799e8b --- /dev/null +++ b/13012.cpp @@ -0,0 +1,19 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + int x, res = 0; + for (int i = 0; i < 5; i++) + { + scanf("%d", &x); + res += n == x; + } + printf("%d\n", res); + } + return 0; +} diff --git a/13013.cpp b/13013.cpp new file mode 100644 index 0000000..c1d0575 --- /dev/null +++ b/13013.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +const int MAXN = 5005; +const long long MOD = 1e9 + 7; +long long dp[MAXN][MAXN], sum[MAXN][MAXN]; + +// [2, 3, 1, 2] is a valid seq +// L(i): set of number i appear pos +// L(1) = {3}, L(2) = {1, 4}, L(3) = {2} +// concat L(K)L(K-1)...L(1) = [2, 1, 4, 3] +// map to Euler's Triangle +// find n elements sequence, k places where A(i) > A(i+1) +// E(n, k) = (k+1) E(n-1, k) + (n-k) E(n-1, k-1) +// ref. Concrete Mathematics pg#268 +int main() +{ + dp[0][0] = 1; + for (int i = 1; i < MAXN; i++) + { + dp[i][0] = 1; + for (int j = 1; j <= i; j++) + { + dp[i][j] = (j + 1) * dp[i - 1][j] + (i - j) * dp[i - 1][j - 1]; + dp[i][j] %= MOD; + } + } + + for (int i = 0; i < MAXN; i++) + { + long long t = 0; + for (int j = 0; j <= i; j++) + { + t += dp[i][j]; + t %= MOD; + sum[i][j] = t; + } + } + int N, Q, K; + while (scanf("%d %d", &N, &Q) == 2) + { + vector ret; + for (int i = 0; i < Q; i++) + { + scanf("%d", &K); + K = min(K, N); + ret.push_back(sum[N][K - 1]); + } + for (int i = 0; i < ret.size(); i++) + { + if (i) + putchar(' '); + printf("%lld", ret[i]); + } + puts(""); + } + return 0; +} diff --git a/13014.cpp b/13014.cpp new file mode 100644 index 0000000..1a8e484 --- /dev/null +++ b/13014.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const int MAXN = 131072; + +int E[MAXN]; +long long sumE[MAXN]; +vector> shop[MAXN]; +struct ELE +{ + long long cost; + int x, e; + ELE(long long cost = 0, int x = 0, int e = 0) : cost(cost), x(x), e(e) {} + bool operator<(const ELE &v) const + { + if (cost != v.cost) + return cost < v.cost; + if (x != v.x) + return x < v.x; + return e < v.e; + } +}; + +int main() +{ + int N, M; + while (scanf("%d %d", &N, &M) == 2) + { + for (int i = 1; i <= N; i++) + scanf("%d", &E[i]), shop[i].clear(); + for (int i = 0; i < M; i++) + { + int L, S, C; + scanf("%d %d %d", &L, &S, &C); + shop[L].push_back(make_pair(S, C)); + } + + for (int i = 1; i <= N; i++) + sumE[i] = sumE[i - 1] + E[i]; + + set S; + for (int i = 1; i <= N; i++) + { + while (!S.empty()) + { + ELE u = *S.begin(); + if (u.e < sumE[i - 1] - sumE[u.x - 1]) + S.erase(S.begin()); + else + break; + } + if (S.empty() && i != 1) + { + } + else + { + long long mm = i == 1 ? 0 : S.begin()->cost; + for (int j = 0; j < shop[i].size(); j++) + { + pair p = shop[i][j]; + if (p.first >= E[i]) + S.insert(ELE(mm + p.second, i, p.first)); + } + } + } + while (!S.empty()) + { + ELE u = *S.begin(); + if (u.e < sumE[N] - sumE[u.x - 1]) + S.erase(S.begin()); + else + break; + } + if (S.empty()) + puts("-1"); + else + printf("%lld\n", S.begin()->cost); + } + return 0; +} diff --git a/13015.cpp b/13015.cpp new file mode 100644 index 0000000..4dae66c --- /dev/null +++ b/13015.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +const int MAXN = 5005; + +int A, B, E, P; +vector G[MAXN], invG[MAXN]; +int used[MAXN] = {}, cases = 0; + +int dfs(int u, vector g[]) +{ + if (used[u] == cases) + return 0; + used[u] = cases; + int ret = 1; + for (auto v : g[u]) + ret += dfs(v, g); + return ret; +} +int main() +{ + int x, y; + while (scanf("%d %d %d %d", &A, &B, &E, &P) == 4) + { + for (int i = 0; i < E; i++) + G[i].clear(), invG[i].clear(); + // Must DAG + for (int i = 0; i < P; i++) + { + scanf("%d %d", &x, &y); + G[x].push_back(y), invG[y].push_back(x); + } + + int retA = 0, retB = 0, retN = 0; + for (int i = 0; i < E; i++) + { + int worst, best; + cases++; + worst = E - dfs(i, G) + 1; + cases++; + best = dfs(i, invG); + if (worst <= A) + retA++; + if (worst <= B) + retB++; + if (best > B) + retN++; + } + printf("%d\n%d\n%d\n", retA, retB, retN); + } + return 0; +} diff --git a/13017.cpp b/13017.cpp new file mode 100644 index 0000000..72e945f --- /dev/null +++ b/13017.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +// Huffman Coding, Greedy +// input A[1...n] +// minimum \sum A[i]*code[i] +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int n, x; + long long t; + multiset S; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &x), S.insert(x); + + long long ret = 0; + for (int i = n - 2; i >= 0; i--) + { + long long a, b; + a = *S.begin(); + S.erase(S.begin()); + b = *S.begin(); + S.erase(S.begin()); + ret += a + b; + S.insert(a + b); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/13018.cpp b/13018.cpp new file mode 100644 index 0000000..2f59384 --- /dev/null +++ b/13018.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int cases = 0; + int N, M; + while (scanf("%d %d", &N, &M) == 2) + { + if (cases++) + puts(""); + + int cnt[128] = {}, mx = 0; + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= M; j++) + cnt[i + j]++; + } + for (int i = 1; i <= N + M; i++) + mx = max(mx, cnt[i]); + for (int i = 1; i <= N + M; i++) + { + if (cnt[i] == mx) + printf("%d\n", i); + } + } + return 0; +} diff --git a/13021.cpp b/13021.cpp new file mode 100644 index 0000000..6284e30 --- /dev/null +++ b/13021.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +const int M = 1002; + +int p, k, n, A[M], sg[M]; +bool mark[M]; + +int solve() +{ + 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, t, _ = min(i - 1, k); j <= _; j++) + { + t = i - j - A[i - j]; + if (t >= 0) + { + mark[sg[t]] = 1; + } + } + + for (sg[i] = 0; mark[sg[i]]; sg[i]++) + ; + + for (int j = 0, t, _ = min(i - 1, k); j <= _; j++) + { + t = i - j - A[i - j]; + if (t >= 0) + { + mark[sg[t]] = 0; + } + } + } + return sg[n]; +} + +int main() +{ + while (scanf("%d%d", &p, &k) != EOF) + { + int res = 0; + for (; p--;) + { + res ^= solve(); + } + puts(res ? "Alice can win." : "Bob will win."); + } + return 0; +} diff --git a/13022.cpp b/13022.cpp new file mode 100644 index 0000000..3cd5f44 --- /dev/null +++ b/13022.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int main() +{ + vector ret; + unsigned long long a = 0; + for (int N = 1; N <= 64; N++) + { + a = a << 1 | 1; + for (int M = 1; M <= 64; M++) + { + unsigned long long v = a; + for (int i = N; i <= 64; i += N + M) + { + ret.push_back(v); + v = v << M; + v = v << N | a; + } + v = a << M; + for (int i = N + M; i <= 64; i += N + M) + { + ret.push_back(v); + v = v << N | a; + v = v << M; + } + } + } + sort(ret.begin(), ret.end()); + ret.resize(unique(ret.begin(), ret.end()) - ret.begin()); + unsigned long long L, R; + while (scanf("%llu %llu", &L, &R) == 2) + { + int sum = 0; + for (auto x : ret) + { + sum += x >= L && x <= R; + } + printf("%d\n", sum); + } + return 0; +} diff --git a/13024.cpp b/13024.cpp new file mode 100644 index 0000000..48d0c87 --- /dev/null +++ b/13024.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define eps 1e-10 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + return y < a.y; + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator/(const double val) const + { + return Pt(x / val, y / val); + } + Pt operator*(const double val) const + { + return Pt(x * val, y * val); + } +}; +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +int monotone(int n, Pt p[], Pt ch[]) +{ + sort(p, p + n); + int i, m = 0, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + m--; + ch[m++] = p[i]; + } + for (i = n - 1, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(ch[m - 2], ch[m - 1], p[i]) <= 0) + m--; + ch[m++] = p[i]; + } + return m - 1; +} + +double g(Pt a, Pt b, double x) +{ + Pt vab = b - a; + return a.y + vab.y * (x - a.x) / vab.x; +} +int inside_convex(const Pt &p, Pt ch[], int n) +{ + if (n < 3) + return false; + if (cross(ch[0], p, ch[1]) > eps) + return false; + if (cross(ch[0], p, ch[n - 1]) < -eps) + return false; + + int l = 2, r = n - 1; + int line = -1; + while (l <= r) + { + int mid = (l + r) >> 1; + if (cross(ch[0], p, ch[mid]) > -eps) + { + line = mid; + r = mid - 1; + } + else + l = mid + 1; + } + return cross(ch[line - 1], p, ch[line]) < eps; +} + +Pt D[131072], ch[262144]; + +int main() +{ + int testcase, n, m; + double x, y; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x, &y); + D[i] = Pt(x, y); + } + n = monotone(n, D, ch); + scanf("%d", &m); + int ret = 0; + for (int i = 0; i < m; i++) + { + scanf("%lf %lf", &x, &y); + int f = inside_convex(Pt(x, y), ch, n); + ret += f; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13025.cpp b/13025.cpp new file mode 100644 index 0000000..ce7e03e --- /dev/null +++ b/13025.cpp @@ -0,0 +1,9 @@ +#include + +using namespace std; + +int main() +{ + printf("May 29, 2013 Wednesday\n"); + return 0; +} diff --git a/13026.cpp b/13026.cpp new file mode 100644 index 0000000..860fed3 --- /dev/null +++ b/13026.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0, n; + char s[1024][16], sq[16]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%s", s[i]); + printf("Case %d:\n", ++cases); + scanf("%s", sq); + for (int i = 0; i < n; i++) + { + int diff = 0; + for (int j = 0; sq[j] || s[i][j]; j++) + diff += sq[j] != s[i][j]; + if (diff <= 1) + printf("%s\n", s[i]); + } + } + return 0; +} diff --git a/13028.cpp b/13028.cpp new file mode 100644 index 0000000..619f97c --- /dev/null +++ b/13028.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +typedef long long LL; + +const LL MOD = 1000000007LL; + +LL modpow(int base, int power) +{ + if (power == 0) + { + return 1LL; + } + LL tmp = modpow(base, power >> 1); + tmp = (tmp * tmp) % MOD; + if (power & 1) + { + tmp = (tmp * base) % MOD; + } + return tmp; +} + +int main() +{ + char pp[10005]; + int T, t = 0; + scanf("%d", &T); + getchar(); + while (T--) + { + gets(pp); + int l = strlen(pp); + LL mod = 0LL; + for (int i = 0; i < l; i++) + { + mod = (mod * 10 + (pp[i] - '0')) % (MOD - 1); + } + LL ans = modpow(3, mod); + ans = (ans + 1) % MOD; + ans = (ans * modpow(2, MOD - 2)) % MOD; + ans = (ans + MOD) % MOD; + printf("Case %d: %lld\n", ++t, ans); + } + return 0; +} diff --git a/13029.cpp b/13029.cpp new file mode 100644 index 0000000..32ae9c7 --- /dev/null +++ b/13029.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +char s[131072]; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int n = strlen(s); + int ret = 0; + int h1 = 0, h2 = 0, h3 = 0; + for (int i = 0; i < n; i++) + { + if (s[i] == '^' && h2) + { + h2--, ret++; + } + else if (s[i] == '^') + { + if (h3 && ret) + h2++, h3--; + else + h1++; + } + else if (s[i] == '_' && h1) + { + h1--, h2++; + } + else if (s[i] == '_') + { + if (ret > h3) + h3++; + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/1303.cpp b/1303.cpp new file mode 100644 index 0000000..c182650 --- /dev/null +++ b/1303.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +const double eps = 1e-8; +const double pi = acos(-1.0); +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; + +typedef Point Vector; + +Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } + +Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } + +Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); } + +Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); } + +bool operator<(Point a, Point b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +int dcmp(double x) +{ + if (fabs(x) < eps) + return 0; + else + return x < 0 ? -1 : 1; +} +bool operator==(Point a, Point b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} + +double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } +double PolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 0; i < n - 1; ++i) + { + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} +//逆时针旋转 rad弧度 +Vector Rotate(Vector A, double rad) +{ + return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); +} + +//求凸包点集 +int ConvexHull(Point *p, int n, Point *ch) +{ + sort(p, p + n); + n = unique(p, p + n) - p;//去釠+ int m = 0; + for (int i = 0; i < n; ++i) + { + while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + + int k = m; + for (int i = n - 2; i >= 0; --i) + { + while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + + if (n > 1) + m--; + return m; +} +double sqr(double x) +{ + return x * x; +} +//点积 +double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } + +double Length(Vector A) { return sqrt(Dot(A, A)); } +double torad(double deg) +{ + return deg / 180 * acos(-1); +} +Point P[1009], ch[1009]; +int main() +{ + // freopen("in.txt","r",stdin); + int t; + cin >> t; + while (t--) + { + int n, l; + cin >> n >> l; + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &P[i].x, &P[i].y); + } + int m = ConvexHull(P, n, ch); + double ans = 0; + for (int i = 1; i < m; i++) + { + Vector o(ch[i].x - ch[i - 1].x, ch[i].y - ch[i - 1].y); + ans += Length(o); + } + Vector o(ch[m - 1].x - ch[0].x, ch[m - 1].y - ch[0].y); + ans += Length(o); + ans += 2 * pi * l; + printf("%.0lf\n", ans); + if (t != 0) + printf("\n"); + } + return 0; +} diff --git a/13031.cpp b/13031.cpp new file mode 100644 index 0000000..9d3704d --- /dev/null +++ b/13031.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int n, a, b; + vector> A; + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d %d", &a, &b), A.push_back(make_pair(b, a)); + sort(A.begin(), A.end()); + + long long ret = 0; + long long sum = 0; + for (int i = n - 1; i >= 0; i--) + { + sum += A[i].second; + ret = max(ret, sum * A[i].first); + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/13032.cpp b/13032.cpp new file mode 100644 index 0000000..4765fa9 --- /dev/null +++ b/13032.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +const long long MOD = 1000000007; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int N, M[128]; + scanf("%d", &N); + for (int i = 1; i <= N; i++) + scanf("%d", &M[i]); + sort(M + 1, M + 1 + N); + + long long dp[128][128] = {}; + dp[0][0] = 1; + for (int i = 1; i <= N; i++) + { + for (int j = 0; j <= M[i]; j++) + { + if (j - (i - 1) >= 0) + { + dp[i][j] = dp[i][j] + dp[i - 1][j] * (j - (i - 1)); + dp[i][j] %= MOD; + } + for (int k = j + 1; k <= M[i]; k++) + { + dp[i][k] = dp[i][k] + dp[i - 1][j]; + dp[i][k] %= MOD; + } + } + } + + long long ret = 0; + for (int i = 0; i <= M[N]; i++) + ret = (ret + dp[N][i]) % MOD; + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/13034.cpp b/13034.cpp new file mode 100644 index 0000000..095173c --- /dev/null +++ b/13034.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int x, ret = 1; + for (int i = 0; i < 13; i++) + scanf("%d", &x), ret &= x > 0; + printf("Set #%d: %s\n", ++cases, ret ? "Yes" : "No"); + } + return 0; +} diff --git a/13035.cpp b/13035.cpp new file mode 100644 index 0000000..79fa91e --- /dev/null +++ b/13035.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const long long MOD = 1000000007; +void exgcd(long long x, long long y, long long &g, + long long &a, long long &b) +{ + if (y == 0) + g = x, a = 1, b = 0; + else + exgcd(y, x % y, g, b, a), b -= (x / y) * a; +} +long long inverse(long long x, long long p) +{ + long long g, b, r; + exgcd(x, p, g, r, b); + if (g < 0) + r = -r; + return (r % p + p) % p; +} + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + long long n; + scanf("%lld", &n); + n++; + long long ret = 1; + ret = (n + 1) * (n) % MOD * (n - 1) % MOD; + ret = ret * inverse(2, MOD) % MOD * inverse(3, MOD) % MOD; + ret = ret * 2 % MOD; + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/13036.cpp b/13036.cpp new file mode 100644 index 0000000..c680ccb --- /dev/null +++ b/13036.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +const int MAXN = 85; +const long long MAXV = 1e+18; +long long F[100] = {2, 3}; + +vector S1, S2; + +void bfs(int begin, int end, vector &S) +{ + S.push_back(1); + for (int i = begin; i < end; i++) + { + long long x = F[i]; + vector next(S); + for (auto e : S) + { + while (MAXV / e / x) + { + e *= x; + next.push_back(e); + } + } + S = next; + } + sort(S.begin(), S.end()); + S.resize(unique(S.begin(), S.end()) - S.begin()); +} + +int main() +{ + for (int i = 2; i < MAXN; i++) + { + F[i] = F[i - 1] + F[i - 2]; + assert(F[i] / MAXV == 0); + } + int split = 6; + bfs(0, split, S1); + bfs(split, MAXN, S2); + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + long long a, b; + long long ret = -1; + scanf("%lld %lld", &a, &b); + int idx1 = 0, idx2 = S2.size() - 1; + for (; idx1 < S1.size(); idx1++) + { + if (S1[idx1] > b) + break; + long long e = S1[idx1]; + while (idx2 > 0 && b / S2[idx2] / e == 0) + idx2--; + long long t = S2[idx2] * e; + if (t >= a) + ret = max(ret, t); + } + vector::iterator it = lower_bound(S1.begin(), S1.end(), b); + if (it == S1.end() || it != S1.begin() && *it > b) + it--; + if (it != S1.end() && b / *it) + { + long long t = *it; + if (t >= a) + ret = max(ret, t); + } + it = lower_bound(S2.begin(), S2.end(), b); + if (it == S2.end() || it != S2.begin() && *it > b) + it--; + if (it != S2.end() && b / *it) + { + long long t = *it; + if (t >= a) + ret = max(ret, t); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/13037.cpp b/13037.cpp new file mode 100644 index 0000000..2714c1a --- /dev/null +++ b/13037.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int n[3], x; + set A[3], S; + for (int i = 0; i < 3; i++) + scanf("%d", &n[i]); + for (int i = 0; i < 3; i++) + { + for (int j = n[i] - 1; j >= 0; j--) + { + scanf("%d", &x); + A[i].insert(x), S.insert(x); + } + } + printf("Case #%d:\n", ++cases); + for (int i = 0; i < 3; i++) + { + int p = 0, q = 0; + for (auto &x : A[i]) + { + int sp = 1; + for (int j = 0; j < 3; j++) + { + if (i == j) + continue; + if (A[j].count(x)) + sp = 0, j = 3; + } + if (sp) + p++; + } + for (auto &x : S) + { + if (A[i].count(x)) + continue; + int sp = 1; + for (int j = 0; j < 3; j++) + { + if (i == j) + continue; + if (!A[j].count(x)) + sp = 0, j = 3; + } + if (sp) + q++; + } + printf("%d %d\n", p, q); + } + } + return 0; +} diff --git a/13038.cpp b/13038.cpp new file mode 100644 index 0000000..7653c74 --- /dev/null +++ b/13038.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +const int MAXN = 131072; + +vector g[MAXN]; +int indeg[MAXN]; + +int dfs(int u) +{ + int ret = 1; + for (auto &v : g[u]) + ret = max(ret, dfs(v) + 1); + return ret; +} +int solve(int root, int N) +{ + return dfs(root); +} + +int main() +{ + int testcase, cases = 0; + int N, M, u, v; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &M); + for (int i = 0; i <= N; i++) + g[i].clear(), indeg[i] = 0; + for (int i = 0; i < M; i++) + { + scanf("%d %d", &u, &v); + g[u].push_back(v), indeg[v]++; + } + + int ret = 1; + for (int i = 1; i <= N; i++) + { + if (indeg[i] == 0) + ret = max(solve(i, N), ret); + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13039.cpp b/13039.cpp new file mode 100644 index 0000000..fd66733 --- /dev/null +++ b/13039.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + long long A[1024]; + for (int i = 0; i < n; i++) + scanf("%lld", &A[i]); + + long long ret = 0, prev = 0; + for (int i = 0; i < n; i++) + { + while (prev > 0 && A[i] >= 2) + ret++, prev--, A[i] -= 2; + ret += A[i] / 3; + A[i] %= 3; + if (i + 1 < n && A[i] == 2 && A[i + 1]) + ret++, A[i + 1]--, A[i] = 0; + if (i + 1 < n && A[i] == 1 && A[i + 1] > 1) + ret++, A[i + 1] -= 2, A[i] = 0; + prev += A[i]; + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/1304.cpp b/1304.cpp new file mode 100644 index 0000000..896221b --- /dev/null +++ b/1304.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +const int MAXN = 3000; +const double pi = acos(-1.0); + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; +typedef Point Vector; +struct Line +{ + Point P; + Vector v; + double ang; + Line(){}; + Line(Point P, Vector v) : P(P), v(v) { ang = atan2(v.y, v.x); } + bool operator<(const Line &L) const + { + return ang < L.ang; + } +}; + +Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } +Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } +Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); } +Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); } +bool operator<(Point a, Point b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +const double eps = 1e-8; +int dcmp(double x) +{ + if (fabs(x) < eps) + return 0; + else + return x < 0 ? -1 : 1; +} +bool operator==(Point a, Point b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} + +//平方 +double sqr(double x) +{ + return x * x; +} +double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } +double Area2(Point A, Point B, Point C) +{ + return Cross(B - A, C - A); +} + +//点积 +double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } +//长度 +double Length(Vector A) { return sqrt(Dot(A, A)); } +//夹角 +double Angle(Vector A, Vector B) +{ + return acos(Dot(A, B) / Length(A) / Length(B)); +} +Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) +{ + Vector u = P - Q; + double t = Cross(w, u) / Cross(v, w); + return P + v * t; +} +Vector Normal(const Vector &A) +{//计算å•ä½Â法å‘釠+ double L = Length(A); + return Vector(-A.y / L, A.x / L); +} +double DistanceToline(Point P, Point A, Point B) +{ + Vector v1 = B - A, v2 = P - A; + return fabs(Cross(v1, v2)) / Length(v1); +} + +double DistanceToSegment(Point P, Point A, Point B) +{ + if (A == B) + return Length(P - A); + Vector v1 = B - A, v2 = P - A, v3 = P - B; + if (dcmp(Dot(v1, v2)) > 0) + return Length(v2); + else if (dcmp(Dot(v1, v3)) > 0) + return Length(v3); + else + return fabs(Cross(v1, v2)) / Length(v1); +} + +//线段相交判定 +bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) +{ + double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), + c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); + return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; +} +//线段相交是å¦在端点上 +bool OnSegment(Point p, Point a1, Point a2) +{ + return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; +} + +double PolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 0; i < n - 1; ++i) + { + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} + +int ConvexHull(Point *p, int n, Point *ch)//求凸包 +{ + sort(p, p + n); + n = unique(p, p + n) - p; + int m = 0; + for (int i = 0; i < n; ++i) + { + while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + int k = m; + for (int i = n - 2; i >= 0; i--) + { + while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + if (n > 1) + m--; + return m; +} +double RotatingCalipers(Point *ch, int n)//旋转å¡壳 +{ + int q = 1; + double ans = 0; + for (int i = 0; i < n; i++) + { + while (Cross(ch[i + 1] - ch[i], ch[q + 1] - ch[i]) > Cross(ch[i + 1] - ch[i], ch[q] - ch[i])) + q = (q + 1) % n; + ans = max(ans, max(Length(ch[i] - ch[q]), Length(ch[i + 1] - ch[q + 1]))); + } + return ans; +} + +bool OnLeft(Line L, Point p) +{ + return Cross(L.v, p - L.P) > 0; +} +Point GetIntersection(Line a, Line b) +{ + Vector u = a.P - b.P; + double t = Cross(b.v, u) / Cross(a.v, b.v); + return a.P + a.v * t; +} + +//具有n个顶点(0-n-1)的多边形的é¢积 +double ConvexPolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 1; i < n - 1; i++) + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + return area / 2; +} +int HalfplaneIntersection(Line *L, int n, Point *poly) +{ + sort(L, L + n); + int first, last; + Point *p = new Point[n]; + Line *q = new Line[n]; + q[first = last = 0] = L[0]; + for (int i = 1; i < n; ++i) + { + while (first < last && !OnLeft(L[i], p[last - 1])) + last--; + while (first < last && !OnLeft(L[i], p[first])) + first++; + q[++last] = L[i]; + if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) + { + last--; + if (OnLeft(q[last], L[i].P)) + q[last] = L[i]; + } + if (first < last) + p[last - 1] = GetIntersection(q[last - 1], q[last]); + } + while (first < last && !OnLeft(q[first], p[last - 1])) + last--; + if (last - first <= 1) + return 0; + p[last] = GetIntersection(q[last], q[first]); + + int m = 0; + for (int i = first; i <= last; i++) + poly[m++] = p[i]; + return m; +} +Line L[MAXN]; +Point poly[MAXN]; +Point p[MAXN], ch[MAXN]; +Vector d[MAXN], v[MAXN], v2[MAXN]; +int n, m, num; +int tt; +double x, y, z, px, py; +int main() +{ + //freopen("a.txt","r",stdin); + cin >> tt; + while (tt--) + { + cin >> n; + for (int i = 0; i < n; i++) + cin >> p[i].x >> p[i].y; + for (int i = n - 1; i >= 1; i--) + { + L[i] = Line(p[i], p[i - 1] - p[i]); + } + L[0] = Line(p[0], p[n - 1] - p[0]); + int m = HalfplaneIntersection(L, n, poly); + double ans = ConvexPolygonArea(poly, m); + printf("%.2lf\n", ans); + } + + return 0; +} diff --git a/13045.cpp b/13045.cpp new file mode 100644 index 0000000..f8e05b1 --- /dev/null +++ b/13045.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + int N, L; + const double pi = acos(-1); + scanf("%d", &testcase); + while (testcase--) + { + double x, y; + scanf("%d %d %lf %lf", &N, &L, &x, &y); + double theta = 0; + if (cases) + puts(""); + printf("Case #%d:\n", ++cases); + for (int i = 0; i < N; i++) + { + printf("%.6lf %.6lf\n", x, y); + x += L * cos(theta), y += L * sin(theta); + theta += 2.f * pi / N; + } + } + return 0; +} diff --git a/13046.cpp b/13046.cpp new file mode 100644 index 0000000..d16bb7f --- /dev/null +++ b/13046.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +struct Pt +{ + int x, y; + Pt(int x = 0, int y = 0) : x(x), y(y) + { + } + int dist(Pt u) + { + return abs(x - u.x) + abs(y - u.y); + } + bool operator==(const Pt &u) const + { + return x == u.x && y == u.y; + } + Pt operator-(const Pt &u) const + { + return Pt(x - u.x, y - u.y); + } + Pt operator+(const Pt &u) const + { + return Pt(x + u.x, y + u.y); + } +}; +int main() +{ + int testcase, cases = 0; + int N[2], x, y; + Pt R[2][64]; + scanf("%d", &testcase); + while (testcase--) + { + int Len[2] = {}; + for (int i = 0; i < 2; i++) + { + scanf("%d", &N[i]); + for (int j = 0; j < N[i]; j++) + { + scanf("%d %d", &x, &y); + R[i][j] = Pt(x, y); + } + for (int j = 0; j < N[i]; j++) + Len[i] += R[i][j].dist(R[i][(j + 1) % N[i]]); + } + + printf("Case %d: ", ++cases); + + int simT = Len[0] / __gcd(Len[0], Len[1]) * Len[1]; + int posIdx[2], has = 0; + Pt posXY[2]; + for (int i = 0; i < 2; i++) + posIdx[i] = 0, posXY[i] = R[i][0]; + + for (int it = 0; it < simT; it++) + { + for (int i = 0; i < 2; i++) + { + // move + if (posXY[i] == R[i][posIdx[i]]) + posIdx[i] = (posIdx[i] + 1) % N[i]; + Pt dv = R[i][posIdx[i]] - posXY[i]; + int lenDv = abs(dv.x) + abs(dv.y); + dv.x /= lenDv, dv.y /= lenDv; + posXY[i] = posXY[i] + dv; + } + if (posXY[0] == posXY[1]) + { + printf("%d %d\n", posXY[0].x, posXY[0].y); + has = 1; + break; + } + } + if (!has) + puts("No Collision"); + } + return 0; +} diff --git a/13047.cpp b/13047.cpp new file mode 100644 index 0000000..ddc4830 --- /dev/null +++ b/13047.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + char s[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int n = strlen(s); + int ret = -1; + for (int i = 0; i < n; i++) + { + if (s[i] == '>') + { + int type = i ? s[i - 1] : 0, cnt = 1; + if (type == '=' || type == '-') + for (int j = i - 1; j >= 0 && s[j] == type; j--) + cnt++; + ret = max(ret, cnt); + } + else if (s[i] == '<') + { + int type = s[i + 1], cnt = 1; + if (type == '=' || type == '-') + for (int j = i + 1; s[j] && s[j] == type; j++) + cnt++; + ret = max(ret, cnt); + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13048.cpp b/13048.cpp new file mode 100644 index 0000000..af0b689 --- /dev/null +++ b/13048.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + char s[1024]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int n = strlen(s); + int ret = 0; + for (int i = 0; i < n; i++) + { + if (s[i] != '-') + continue; + int valid = 1; + if (i + 1 < n && s[i + 1] == 'B') + valid = 0; + if (i + 2 < n && s[i + 2] == 'B') + valid = 0; + if (i + 1 < n && s[i + 1] == 'S') + valid = 0; + if (i - 1 >= 0 && s[i - 1] == 'S') + valid = 0; + if (valid) + ret++; + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13049.cpp b/13049.cpp new file mode 100644 index 0000000..14dfe70 --- /dev/null +++ b/13049.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +const int MAXS = 128; +char sa[MAXS], sb[MAXS]; + +int main() +{ + int testcase, n, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %s %s", &n, sa, sb); + int ret = 0; + for (int i = 0; i < n; i++) + { + int a = sa[i] - '0'; + int b = sb[i] - '0'; + ret += min(abs(a - b), 10 - abs(a - b)); + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13052.cpp b/13052.cpp new file mode 100644 index 0000000..7f25a05 --- /dev/null +++ b/13052.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int A, B; + double p, q; + scanf("%d %d", &A, &B); + if (A == 0 && B == 0) + p = q = 0; + else if (B % 2 == 0) + p = 1, q = 0; + else + p = 0, q = 1; + printf("Case %d: %.3lf %.3lf\n", ++cases, p, q); + } + return 0; +} diff --git a/13053.cpp b/13053.cpp new file mode 100644 index 0000000..233ae29 --- /dev/null +++ b/13053.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + int T, t; + scanf("%d", &T); + while (T--) + { + int a, b, c; + double h, l, w, ans; + scanf("%d%d%d", &a, &b, &c); + h = c * 1.0f; + l = (a + h) * 2; + w = (b + h) * 2; + ans = hypot(l, w); + printf("Case %d: %.4lf\n", ++t, ans); + } + return 0; +} diff --git a/13054.cpp b/13054.cpp new file mode 100644 index 0000000..7691956 --- /dev/null +++ b/13054.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int A[131072], used[131072]; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int N, H, Ta, Tb; + scanf("%d %d %d %d", &N, &H, &Ta, &Tb); + for (int i = 0; i < N; i++) + scanf("%d", &A[i]); + sort(A, A + N); + + long long ret = 0; + memset(used, 0, sizeof(used)); + int r = N - 1; + for (int i = 0; i < N; i++) + { + if (used[i] == 1) + continue; + while (r > i && (A[i] + A[r] >= H || used[r] == 1)) + r--; + if (r > i && A[i] + A[r] < H && used[r] == 0 && Ta + Ta > Tb) + { + ret += Tb; + used[r] = used[i] = 1; + } + else + { + ret += Ta; + used[i] = 1; + } + } + printf("Case %d: %lld\n", ++cases, ret); + } + return 0; +} diff --git a/13055.cpp b/13055.cpp new file mode 100644 index 0000000..40b27e3 --- /dev/null +++ b/13055.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int main() +{ + int n; + char cmd[16], name[16]; + stack path; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s", cmd); + if (cmd[0] == 'S') + {// Sleep + scanf("%s", name); + path.push(name); + } + else if (cmd[0] == 'K') + {// Kick + if (!path.empty()) + path.pop(); + } + else + {// Test + if (path.empty()) + { + puts("Not in a dream"); + } + else + { + printf("%s\n", path.top().c_str()); + } + } + } + return 0; +} diff --git a/13056.cpp b/13056.cpp new file mode 100644 index 0000000..fee8ac9 --- /dev/null +++ b/13056.cpp @@ -0,0 +1,17 @@ +#include + +using namespace std; + +int main() +{ + const double pi = acos(-1); + double R, V; + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lf %lf", &R, &V); + printf("Case %d: %.8lf\n", ++cases, pi / 2 * R / V); + } + return 0; +} diff --git a/13057.cpp b/13057.cpp new file mode 100644 index 0000000..05cdf29 --- /dev/null +++ b/13057.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +// ~ UVa11504: SCC + +const int MAXN = 32767; + +class SCC +{ +public: + int n; + vector g[MAXN], dag[MAXN]; + // + int vfind[MAXN], findIdx; + int stk[MAXN], stkIdx; + int in_stk[MAXN], visited[MAXN]; + int contract[MAXN]; + int scc_cnt; + // + int scc(int u) + { + in_stk[u] = visited[u] = 1; + stk[++stkIdx] = u, vfind[u] = ++findIdx; + int mn = vfind[u], v; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i]; + if (!visited[v]) + mn = min(mn, scc(v)); + if (in_stk[v]) + mn = min(mn, vfind[v]); + } + if (mn == vfind[u]) + { + do + { + in_stk[stk[stkIdx]] = 0; + contract[stk[stkIdx]] = scc_cnt; + } while (stk[stkIdx--] != u); + scc_cnt++; + } + return mn; + } + void addEdge(int u, int v) + {// u -> v + g[u].push_back(v); + } + void solve() + { + for (int i = 0; i < n; i++) + visited[i] = in_stk[i] = 0; + scc_cnt = 0; + for (int i = 0; i < n; i++) + { + if (visited[i]) + continue; + stkIdx = findIdx = 0; + scc(i); + } + } + void make_DAG() + { + int x, y; + for (int i = 0; i < n; i++) + { + x = contract[i]; + for (int j = 0; j < g[i].size(); j++) + { + y = contract[g[i][j]]; + if (x != y) + dag[x].push_back(y); + } + } + for (int i = 0; i < scc_cnt; i++) + { + sort(dag[i].begin(), dag[i].end()); + dag[i].resize(unique(dag[i].begin(), dag[i].end()) - dag[i].begin()); + } + } + void init(int n) + { + this->n = n; + for (int i = 0; i < n; i++) + g[i].clear(), dag[i].clear(); + } +} g; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + int n, m, u, v; + scanf("%d %d", &n, &m); + g.init(n); + for (int i = 0; i < m; i++) + { + scanf("%d %d", &u, &v); + u--, v--; + g.addEdge(u, v); + } + g.solve(); + g.make_DAG(); + + int indeg[MAXN] = {}, ret = 0; + for (int i = 0; i < g.scc_cnt; i++) + { + for (auto &e : g.dag[i]) + { + indeg[e]++; + } + } + for (int i = 0; i < g.scc_cnt; i++) + ret += indeg[i] == 0; + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13058.cpp b/13058.cpp new file mode 100644 index 0000000..044dd52 --- /dev/null +++ b/13058.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + for (int n, pos[1000]; scanf("%d %d", &n, pos) == 2;) + { + int ans = -1, arc, len; + for (int i = 1; i < n; ++i) + scanf("%d", &arc), pos[i] = pos[i - 1] + arc; + len = pos[n - 1]; + for (int side = 3; side <= n; ++side) + if (len % side == 0) + { + bool ok = false; + for (int i = 0, size = len / side; i < n && pos[i] <= size && !ok; ++i) + { + bool now = true; + for (int j = 2; j <= side && now; ++j) + now &= *std::lower_bound(pos + i, pos + n, size * j) == size * j; + ok |= now; + } + if (ok) + ans = side; + } + printf("%d\n", ans); + } +} diff --git a/13059.cpp b/13059.cpp new file mode 100644 index 0000000..32a4239 --- /dev/null +++ b/13059.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + long long n; + while (scanf("%lld", &n) == 1) + { + printf("%lld\n", n - 1); + } + return 0; +} diff --git a/1306.cpp b/1306.cpp new file mode 100644 index 0000000..6ff9953 --- /dev/null +++ b/1306.cpp @@ -0,0 +1,205 @@ +#include + +using namespace std; + +#define PB push_back +#define MP make_pair +#define SZ(v) ((int)(v).size()) +#define abs(x) ((x) > 0 ? (x) : -(x)) + +typedef long long LL; +typedef int F; + +#define inf (1 << 29) +#define maxV 1000 +#define maxE 400000 + +struct Dinic +{ + int V, E; + F cap[maxE], flow[maxE]; + int to[maxE], prev[maxE]; + int level[maxV], used[maxV], last[maxV]; + + void init(int n) + { + V = n; + E = 0; + for (int i = 0; i < V; i++) + { + last[i] = -1; + } + } + + void addCap(int x, int y, F f) + { + cap[E] = f; + flow[E] = 0; + to[E] = y; + prev[E] = last[x]; + last[x] = E; + E++; + cap[E] = 0; + flow[E] = 0; + to[E] = x; + prev[E] = last[y]; + last[y] = E; + E++; + } + + bool bfs(int s, int t) + { + for (int i = 0; i < V; i++) + { + level[i] = -1; + } + queue q; + q.push(s); + level[s] = 0; + while (!q.empty()) + { + int x = q.front(); + q.pop(); + for (int i = last[x]; i != -1; i = prev[i]) + if (level[to[i]] == -1 && cap[i] > flow[i]) + { + q.push(to[i]); + level[to[i]] = level[x] + 1; + } + } + return (level[t] != -1); + } + + F dfs(int v, int t, F f) + { + if (v == t) + { + return f; + } + for (int i = used[v]; i != -1; used[v] = i = prev[i]) + if (level[to[i]] > level[v] && cap[i] > flow[i]) + { + F tmp = dfs(to[i], t, min(f, cap[i] - flow[i])); + if (tmp > 0) + { + flow[i] += tmp; + flow[i ^ 1] -= tmp; + return tmp; + } + } + return 0; + } + + bool solve(int s, int t) + { + while (bfs(s, t)) + { + for (int i = 0; i < V; i++) + { + used[i] = last[i]; + } + while (dfs(s, t, inf) != 0) + ; + } + bool ans = true; + for (int i = last[s]; i != -1; i = prev[i]) + { + ans &= (cap[i] - flow[i] == 0); + } + return ans; + } +} mf; + +int match[33][33], tmp[33][33]; +int w[33], d[33]; + +int main() +{ + int Test; + scanf("%d", &Test); + while (Test--) + { + int n, m = 0; + map, int> mp; + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d%d", &w[i], &d[i]); + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + scanf("%d", &match[i][j]); + if (match[i][j]) + { + mp[MP(i, j)] = ++m; + } + } + } + vector ans; + for (int win = 1; win <= n; win++) + { + memcpy(tmp, match, sizeof(match)); + int sum = w[win]; + for (int i = 1; i <= n; i++) + { + if (tmp[win][i]) + { + sum += tmp[win][i]; + tmp[win][i] = tmp[i][win] = 0; + } + } + bool flag = true; + for (int i = 1; i <= n; i++) + { + if (i == win) + { + continue; + } + if (sum < w[i]) + { + flag = false; + break; + } + } + if (!flag) + { + continue; + } + mf.init(m + n + 2); + int source = 0, sink = n + m + 1; + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + { + if (tmp[i][j] == 0) + { + continue; + } + int idx = mp[MP(i, j)]; + mf.addCap(source, idx, tmp[i][j]); + mf.addCap(idx, m + i, tmp[i][j]); + mf.addCap(idx, m + j, tmp[i][j]); + } + for (int i = 1; i <= n; i++) + { + if (i == win) + { + continue; + } + mf.addCap(m + i, sink, max(sum - w[i], 0)); + } + flag = mf.solve(source, sink); + if (flag) + { + ans.PB(win); + } + } + sort(ans.begin(), ans.end()); + for (int i = 0; i < ans.size(); i++) + { + printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/13061.cpp b/13061.cpp new file mode 100644 index 0000000..858aa4f --- /dev/null +++ b/13061.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const long long mod = 9999959999; +long long dp[2048][2048]; +int used[2048][2048] = {}; + +long long dfs(int u, int v) +{ + if (u == 0 && v == 0) + return 1; + if (used[u][v]) + return dp[u][v]; + used[u][v] = 1; + long long &ret = dp[u][v]; + ret = 1; + if (u > 0) + ret = ret + dfs(u - 1, v + 1); + if (v > 0) + ret = ret + dfs(u, v - 1); + ret %= mod; + return ret; +} + +int main() +{ + int A, B; + while (scanf("%d %d", &A, &B) == 2) + { + if (A == 0 && B == 0) + break; + printf("%lld\n", dfs(A, B)); + } + return 0; +} diff --git a/13062.cpp b/13062.cpp new file mode 100644 index 0000000..36e9d7c --- /dev/null +++ b/13062.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const int N = 205; + +int main() +{ + // [ big ][ small ] + double pill[N][N] = {{0}}, day[N][N] = {{0}}; + + for (int i = 0; i < N; ++i) + pill[0][i] = i; + for (int i = 1; i < N; ++i) + for (int j = 0; j < N; ++j) + { + pill[i][j] = i / double(i + j) * pill[i - 1][j + 1]; + if (j) + pill[i][j] += j / double(i + j) * pill[i][j - 1]; + } + + for (int i = 1; i < N; ++i) + day[i][0] = i; + for (int i = 1; i < N; ++i) + for (int j = 0; j < N; ++j) + { + day[i][j] = 1 + i / double(i + j) * day[i - 1][j + 1]; + if (j) + day[i][j] += j / double(i + j) * day[i][j - 1]; + } + + for (int l, s; scanf("%d %d", &l, &s) && l + s;) + printf("%.12f %.12f\n", pill[l][s], day[l][s]); +} diff --git a/13067.cpp b/13067.cpp new file mode 100644 index 0000000..58a12d6 --- /dev/null +++ b/13067.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define MAXL (10000000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) + +int mark[MAXL]; +int P[1048576], Pt = 0; + +void sieve() +{ + register int i, j, k; + SET(1); + int n = 10000000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} + +int main() +{ + sieve(); + long long N; + while (scanf("%lld", &N) == 1 && N != 1) + { + int ret = 0; + for (int i = 0; i < Pt && (long long)P[i] * P[i] <= N; i++) + { + while (N % P[i] == 0) + { + N /= P[i], ret++; + } + } + if (N != 1) + ret++; + printf("%d\n", ret); + } + return 0; +} diff --git a/13068.cpp b/13068.cpp new file mode 100644 index 0000000..41f1ab4 --- /dev/null +++ b/13068.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int MinExp(const char *s, const int slen) +{ + int i = 0, j = 1, k = 0, x, y, tmp; + while (i < slen && j < slen && k < slen) + { + x = i + k; + y = j + k; + if (x >= slen) + x -= slen; + if (y >= slen) + y -= slen; + if (s[x] == s[y]) + { + k++; + } + else if (s[x] > s[y]) + { + i = j + 1 > i + k + 1 ? j + 1 : i + k + 1; + k = 0; + tmp = i, i = j, j = tmp; + } + else + { + j = i + 1 > j + k + 1 ? i + 1 : j + k + 1; + k = 0; + } + } + return i; +} + +int main() +{ + int testcase; + char s[65536]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int n = strlen(s); + printf("%d\n", MinExp(s, n) + 1); + } + return 0; +} diff --git a/13069.cpp b/13069.cpp new file mode 100644 index 0000000..c376d7b --- /dev/null +++ b/13069.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + int st, ed; + while (scanf("%d %d", &st, &ed) == 2) + { + int used[32767] = {}, u, x; + queue Q; + Q.push(st), used[st] = 1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + if (used[ed]) + break; + x = (u + 1) % 10000; + if (used[x] == 0) + { + used[x] = used[u] + 1; + Q.push(x); + } + x = (u * 2 + 10000) % 10000; + if (used[x] == 0) + { + used[x] = used[u] + 1; + Q.push(x); + } + x = (u / 3); + if (used[x] == 0) + { + used[x] = used[u] + 1; + Q.push(x); + } + } + printf("%d\n", used[ed] - 1); + } + return 0; +} diff --git a/13070.cpp b/13070.cpp new file mode 100644 index 0000000..32bf367 --- /dev/null +++ b/13070.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int W, N, x; + scanf("%d %d", &W, &N); + vector A; + A.push_back(-1); + for (int i = 0; i < N; i++) + { + scanf("%d", &x); + if (x >= W) + A.push_back(i); + } + A.push_back(N); + int ret = 0; + for (int i = 1; i < A.size() - 1; i++) + { + int x = A[min(i + 5, (int)A.size() - 1)]; + ret = max(ret, x - A[i - 1] - 1); + } + if (A.size() == 2) + ret = N; + printf("%d\n", ret); + } + return 0; +} diff --git a/13071.cpp b/13071.cpp new file mode 100644 index 0000000..21d7928 --- /dev/null +++ b/13071.cpp @@ -0,0 +1,18 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + long long N, M; + scanf("%lld %lld", &N, &M); + long long S = N + M, ret; + ret = (S * (S + 1) / 2) + N + 1; + printf("%lld\n", ret); + } + return 0; +} diff --git a/13072.cpp b/13072.cpp new file mode 100644 index 0000000..eeb30db --- /dev/null +++ b/13072.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +const int MAXN = 1024; + +char s[MAXN]; +int dp[MAXN][MAXN], argdp[MAXN][MAXN]; +int bidx = 0; +char buf[MAXN]; + +void dfs(int l, int r) +{ + if (l > r) + return; + if (l == r) + { + buf[bidx++] = s[l]; + return; + } + if (argdp[l][r] == 0) + { + buf[bidx++] = s[l]; + dfs(l + 1, r - 1); + buf[bidx++] = s[l]; + } + else if (argdp[l][r] == 1) + { + dfs(l + 1, r); + } + else + { + dfs(l, r - 1); + } +} +int main() +{ + while (scanf("%s", s) == 1) + { + int n = strlen(s); + for (int i = 0; i < n; i++) + dp[i][i] = 1; + for (int i = 1; i < n; i++) + { + for (int j = 0; j + i < n; j++) + { + int mx = 0, dir; + if (s[j] == s[j + i]) + { + if (dp[j + 1][j + i - 1] + 2 > mx) + mx = dp[j + 1][j + i - 1] + 2, dir = 0; + } + if (dp[j + 1][j + i] > mx) + mx = dp[j + 1][j + i], dir = 1; + if (dp[j][j + i - 1] > mx) + mx = dp[j][j + i - 1], dir = 2; + dp[j][j + i] = mx, argdp[j][j + i] = dir; + } + } + bidx = 0; + dfs(0, n - 1); + buf[bidx] = '\0'; + puts(buf); + } + return 0; +} diff --git a/13073.cpp b/13073.cpp new file mode 100644 index 0000000..82a693e --- /dev/null +++ b/13073.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int f(int n) +{ + int ret = 0, t = 10; + for (int i = 1;; i++) + { + if (n >= t) + { + ret += 9 * (t / 10) * i; + } + else + { + ret += (n - t / 10 + 1) * i; + return ret; + } + t = t * 10; + } + return 0; +} + +int main() +{ + int a, b; + while (scanf("%d %d", &a, &b) == 2 && a) + { + int l = a, r = b, mid, ret = -1, retv = -1; + int tot = f(b) - f(a - 1); + while (l <= r) + { + mid = (l + r) / 2; + int t = f(mid) - f(a - 1); + if (t <= tot / 2) + l = mid + 1, ret = mid, retv = t; + else + r = mid - 1; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13074.cpp b/13074.cpp new file mode 100644 index 0000000..24f231b --- /dev/null +++ b/13074.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +long long gcd(long long x, long long y) +{ + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; +} + +long long lcm(long long x, long long y) +{ + return x / gcd(x, y) * y; +} + +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + int A[128] = {}, used[128] = {}; + for (int i = 1; i <= n; i++) + scanf("%d", &A[i]); + long long ret = 1; + for (int i = 1; i <= n; i++) + { + if (used[i]) + continue; + int u = i, cc = 0; + while (!used[u]) + used[u] = 1, cc++, u = A[u]; + ret = lcm(ret, cc); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/13075.cpp b/13075.cpp new file mode 100644 index 0000000..c5ba644 --- /dev/null +++ b/13075.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int C, V, A[1024] = {}, N; + char s[1024], mm[3] = "CV"; + scanf("%d %d", &C, &V); + N = C + V; + for (int i = C; i < C + V; i++) + A[i] = 1; + + int f = 0; + do + { + for (int i = 0; i < N; i++) + s[i] = mm[A[i]]; + s[N] = '\0'; + if (f) + putchar(' '); + printf("%s", s), f = 1; + } while (next_permutation(A, A + N)); + puts(""); + } + return 0; +} diff --git a/13078.cpp b/13078.cpp new file mode 100644 index 0000000..d2e74af --- /dev/null +++ b/13078.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int G[128][128]; +int N, M, Q; + +const char dir[] = "NESW"; +const int dx[] = {-1, 0, 1, 0}; +const int dy[] = {0, 1, 0, -1}; + +void draw(int sx, int sy, char sd[], int sv) +{ + int d = 0; + for (int i = 0; i < 4; i++) + { + if (sd[0] == dir[i]) + d = i; + } + G[sx][sy]++; + for (int i = 1;; i++) + { + for (int j = 0; j < 2; j++) + { + for (int k = 0; k < i; k++) + { + sx += dx[d], sy += dy[d]; + if (sx > N || sy > M || sx <= 0 || sy <= 0) + return; + G[sx][sy]++; + sv--; + if (sv == 0) + return; + } + d = (d + 1) % 4; + } + } +} +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &N, &M, &Q); + memset(G, 0, sizeof(G)); + for (int i = 0; i < Q; i++) + { + char s[128]; + int x, y, v; + scanf("%d %d %s %d", &x, &y, s, &v); + draw(x, y, s, v); + } + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= M; j++) + printf("%d%c", G[i][j], j == M ? '\n' : ' '); + } + puts("---"); + } + return 0; +} diff --git a/13079.cpp b/13079.cpp new file mode 100644 index 0000000..400931f --- /dev/null +++ b/13079.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int N; + while (scanf("%d", &N) == 1 && N) + { + vector> A; + int L, R; + for (int i = 0; i < N; i++) + { + scanf("%d %d", &L, &R); + A.push_back(make_pair(L, R)); + } + sort(A.begin(), A.end()); + + int ret = 0; + int PQ = INT_MAX; + for (int i = 0; i < N; i++) + { + int line_x = A[i].first; + if (PQ != INT_MAX && PQ <= line_x) + { + ret++; + PQ = INT_MAX; + } + PQ = min(PQ, A[i].second); + } + if (PQ != INT_MAX) + ret++; + printf("%d\n", ret); + } + return 0; +} diff --git a/1308.cpp b/1308.cpp new file mode 100644 index 0000000..311723d --- /dev/null +++ b/1308.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +const double eps = 5 * (1e-13); +const double pi = acos(-1.0); + +int n; + +struct Point +{ + double x, y; + Point() {} + Point(double _x, double _y) : x(_x), y(_y) {} +}; + +struct Circle +{ + Point c; + double r; + Circle() {} + Circle(Point _c, double _r) : c(_c), r(_r) {} + Point point(double x) + { + return Point(c.x + cos(x) * r, c.y + sin(x) * r); + } +}; + +double normal(double x) +{ + return x - floor(x / (2 * pi)) * 2 * pi; +} + +double dcmp(double x) +{ + if (fabs(x) <= eps) + { + return 0; + } + return (x < 0) ? -1 : 1; +} + +double DIST(Point a, Point b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +Circle C[200]; +double a[1000]; +int tot = 0; +bool flag[110]; + +void check(Point x) +{ + for (int i = n - 1; i >= 0; i--) + { + double d = DIST(x, C[i].c); + if (dcmp(d - C[i].r) < 0) + { + flag[i] = true; + break; + } + } +} + +int main() +{ + while (scanf("%d", &n) != EOF && n) + { + memset(flag, 0, sizeof(flag)); + for (int i = 0; i < n; i++) + { + double x, y, z; + scanf("%lf%lf%lf", &x, &y, &z); + C[i] = Circle(Point(x, y), z); + } + for (int i = 0; i < n; i++) + { + tot = 0; + for (int j = 0; j < n; j++) + { + if (i == j) + { + continue; + } + double dist = DIST(C[i].c, C[j].c); + double ri = C[i].r, rj = C[j].r; + if (dcmp(dist - ri - rj) >= 0 || dcmp(dist - fabs(ri - rj)) <= 0) + { + continue; + } + double t = atan2(C[j].c.y - C[i].c.y, C[j].c.x - C[i].c.x); + double dt = acos((ri * ri + dist * dist - rj * rj) / (2. * ri * dist)); + a[tot++] = normal(t + dt); + a[tot++] = normal(t - dt); + } + a[tot++] = 0; + a[tot++] = 2 * pi; + sort(a, a + tot); + tot = unique(a, a + tot) - a; + for (int j = 0; j < tot - 1; j++) + { + double u = (a[j] + a[j + 1]) / 2; + double r1 = C[i].r + eps, r2 = C[i].r - eps; + Point p1 = Point(C[i].c.x + r1 * cos(u), C[i].c.y + r1 * sin(u)); + Point p2 = Point(C[i].c.x + r2 * cos(u), C[i].c.y + r2 * sin(u)); + check(p1); + check(p2); + } + } + int ans = 0; + for (int i = 0; i < n; i++) + if (flag[i]) + { + ans++; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/13080.cpp b/13080.cpp new file mode 100644 index 0000000..34ab054 --- /dev/null +++ b/13080.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const char s[][16] = { + "Dob", "Do", "Do#", "Reb", "Re", "Re#", "Mib", "Mi", "Fab", + "Mi#", "Fa", "Fa#", "Solb", "Sol", "Sol#", "Lab", "La", + "La#", "Sib", "Si", "Si#"}; +const int w[] = { + -1, 0, 1, 1, 2, 3, 3, 4, 4, + 5, 5, 6, 6, 7, 8, 8, 9, + 10, 10, 11, 12}; +int main() +{ + int m = sizeof(s) / sizeof(s[0]); + map R; + for (int i = 1; i <= 7; i++) + { + int base = i * 12; + for (int j = 0; j < m; j++) + { + char buf[16]; + sprintf(buf, "%s%d", s[j], i); + // printf("%s %d\n", buf, base + w[j]); + R[buf] = base + w[j]; + } + } + int n; + while (scanf("%d", &n) == 1 && n) + { + map ret; + for (int i = 0; i < n; i++) + { + char buf[16]; + scanf("%s", buf); + ret[R[buf]]++; + // printf("%s %d\n", buf, R[buf]); + } + int prev = -1; + for (auto &e : ret) + { + if (prev == -1) + { + printf("%d", e.second), prev = e.first + 1; + } + else + { + for (; prev < e.first; prev++) + printf(" 0"); + printf(" %d", e.second), prev = e.first + 1; + } + } + puts(""); + } + return 0; +} diff --git a/13081.cpp b/13081.cpp new file mode 100644 index 0000000..fa00b4a --- /dev/null +++ b/13081.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + unsigned long long L, R; + while (testcase--) + { + scanf("%llu %llu", &L, &R); + if (L == R) + { + unsigned long long ret = 0; + for (int i = 1; i < 63; i++) + { + unsigned long long a; + unsigned long long v = 1LL << (i + 1); + a = L / v * v + (1LL << i); + int odd = a <= L && L < a + (1LL << i) && (L - a) % 2 == 0; + if (i == 0) + odd++; + if (odd % 2) + ret |= 1LL << i; + } + if ((L + 1) / 2 % 2 == 0) + ret |= 1; + printf("%llu\n", ret); + } + else if (R - L >= 2) + { + puts("0"); + } + else + { + printf("%d\n", R % 4 == 0); + } + } + return 0; +} diff --git a/13082.cpp b/13082.cpp new file mode 100644 index 0000000..6981ccf --- /dev/null +++ b/13082.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int R[32767]; + +int main() +{ + int testcase, cases = 0; + int n, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + scanf("%d", &x), R[x] = i; + + int ret = 0, pos = -1; + for (int i = 1; i <= n; i++) + { + if (R[i] < pos) + { + ret++; + pos = n + 1; + } + else + { + pos = R[i]; + } + } + printf("Case %d: %d\n", ++cases, ret); + } + return 0; +} diff --git a/13085.cpp b/13085.cpp new file mode 100644 index 0000000..8cb90c5 --- /dev/null +++ b/13085.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +const int MOD = 1000000007, N = 1000001; +int T, n, fact_mod[N] = {1}; + +long long pow_mod(int base, int exp) +{ + long long pm = base % MOD, ans = 1; + for (int i = 1; i <= exp; i <<= 1, pm = pm * pm % MOD) + if (exp & i) + ans = ans * pm % MOD; + return ans; +} + +void mul_inv(int a, int b, long long int &x, long long int &y) +{ + if (!b) + x = 1, y = 0; + else + mul_inv(b, a % b, y, x), y -= a / b * x; +} + +int calc(int n, int g) +{ + long long D = pow_mod(fact_mod[g], n / g) * fact_mod[n / g] % MOD, x, y; + mul_inv(D, MOD, x, y); + if (x < 0) + x += MOD; + return x * fact_mod[n] % MOD; +} + +int main() +{ + for (long long i = 1; i < N; ++i) + fact_mod[i] = fact_mod[i - 1] * i % MOD; + + scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + unsigned ans = 0; + scanf("%d", &n); + for (int i = 1; i * i <= n; ++i) + if (n % i == 0) + if (i * i < n) + ans = (ans + calc(n, i) + calc(n, n / i)) % MOD; + else + ans = (ans + calc(n, i)) % MOD; + printf("Case %d: %u\n", t, ans); + } +} diff --git a/13086.cpp b/13086.cpp new file mode 100644 index 0000000..3937ae8 --- /dev/null +++ b/13086.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main() +{ + int T, all, small, p, q, r, x, y, z; + long long n[100000]; + + scanf("%d", &T); + for (int t = 1; t <= T; ++t) + { + bool exclude[100000] = {false}; + scanf("%d %d", &all, &small); + scanf("%d %d %d %d %d %d", &p, &q, &r, &x, &y, &z); + for (long long i = 1; i <= all; ++i) + n[i - 1] = (p * i * i + q * i + r) % 1000007; + for (long long i = 1; i <= small; ++i) + ++exclude[(x * i * i + y * i + z) % all]; + long long root = INT_MAX, s = accumulate(n, n + all, 0ll); + for (int i = 0; i < all; ++i) + if (!exclude[i] && n[i] < root) + root = n[i]; + printf("Case %d: %lld\n", t, (s - root) * root); + } +} diff --git a/13088.cpp b/13088.cpp new file mode 100644 index 0000000..da1c98c --- /dev/null +++ b/13088.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + char s[1024]; + scanf("%s", s); + n = strlen(s); + int mark[128] = {}, m = 0; + for (int i = 0; i < n; i++) + mark[s[i]]++; + for (int i = 'a'; i <= 'z'; i++) + m += mark[i] != 0; + for (int i = 'a', t = 0; i <= 'z'; i++) + { + if (mark[i]) + { + for (; t < n - m + 1; t++) + putchar(i); + m--; + } + } + puts(""); + } + return 0; +} diff --git a/1309.cpp b/1309.cpp new file mode 100644 index 0000000..80e0c2d --- /dev/null +++ b/1309.cpp @@ -0,0 +1,186 @@ +#include + +using namespace std; + +#define MAXV 100000 + +struct DancingLinks {int left, right, up, down, data, ch, rh;} DL[1000001 + 3001]; +int s[2001], o[2001], head, size, time_, n, m, a, b, c, tn; + +void Remove(int c) +{ + DL[DL[c].right].left = DL[c].left; + DL[DL[c].left].right = DL[c].right; + int i, j; + for (i = DL[c].down; i != c; i = DL[i].down) + { + for (j = DL[i].right; j != i; j = DL[j].right) + { + DL[DL[j].down].up = DL[j].up; + DL[DL[j].up].down = DL[j].down; + s[DL[j].ch]--; + } + } +} +void Resume(int c) +{ + int i, j; + for (i = DL[c].down; i != c; i = DL[i].down) + { + for (j = DL[i].left; j != i; j = DL[j].left) + { + DL[DL[j].down].up = j; + DL[DL[j].up].down = j; + s[DL[j].ch]++; + } + } + DL[DL[c].right].left = c; + DL[DL[c].left].right = c; +} +void Print_board(int k) +{ + static int set[512], a, b; + for (a = 0; a < k; a++) + { + set[DL[o[a]].data] = DL[o[a]].rh + 1; + } + for (a = 0; a < n; a++) + { + for (b = 0; b < n; b++) + { + printf("%c", set[a * 16 + b] + 'A' - 1); + } + puts(""); + } +} +void DFS(int k) +{ + if (time_) + { + return; + } + if (DL[head].right == head) + { + Print_board(k); + time_++; + return; + } + int t = MAXV, c, i, j; + for (i = DL[head].right; i != head; i = DL[i].right) + { + if (s[i] < t) + { + t = s[i], c = i; + } + } + Remove(c); + for (i = DL[c].down; i != c; i = DL[i].down) + { + o[k] = i; + for (j = DL[i].right; j != i; j = DL[j].right) + { + Remove(DL[j].ch); + } + DFS(k + 1); + for (j = DL[i].left; j != i; j = DL[j].left) + { + Resume(DL[j].ch); + } + } + Resume(c); +} +int new_node(int up, int down, int left, int right) +{ + DL[size].up = up, DL[size].down = down; + DL[size].left = left, DL[size].right = right; + DL[up].down = DL[down].up = DL[left].right = DL[right].left = size; + return size++; +} +void new_row(int n, int Row[], int rh, int set) +{ + int r, row = -1, k; + for (int a = 0; a < n; a++) + { + r = Row[a]; + DL[size].ch = r, DL[size].data = set, s[r]++; + if (row == -1) + { + row = new_node(DL[DL[r].ch].up, DL[r].ch, size, size); + DL[row].rh = rh; + } + else + { + k = new_node(DL[DL[r].ch].up, DL[r].ch, DL[row].left, row); + DL[k].rh = rh; + } + } +} +void init(int m) +{ + size = 0; + head = new_node(0, 0, 0, 0); + for (int i = 1; i <= m; i++) + { + new_node(i, i, DL[head].left, head); + DL[i].ch = i, s[i] = 0; + } +} + +int main() +{ + int map[20][20], Row[256], t, i; + char g[20][20], first = 0; + while (scanf("%s", g[0]) == 1) + { + for (i = 1; i < 16; i++) + { + scanf("%s", g[i]); + } + n = 4; + tn = n, n *= n, m = n * n * 4; + init(m); + for (a = 0; a < n; a++) + { + for (b = 0; b < n; b++) + { + if (g[a][b] == '-') + { + map[a][b] = 0; + } + else + { + map[a][b] = g[a][b] - 'A' + 1; + } + if (map[a][b] == 0) + { + for (c = 0; c < n; c++) + { + t = 0; + Row[t++] = a * n + b + 1; + Row[t++] = n * n + a * n + c + 1; + Row[t++] = 2 * n * n + b * n + c + 1; + Row[t++] = 3 * n * n + (a / tn * tn + b / tn) * n + c + 1; + new_row(t, Row, c, a * n + b); + } + } + else + { + c = map[a][b] - 1, t = 0; + Row[t++] = a * n + b + 1; + Row[t++] = n * n + a * n + c + 1; + Row[t++] = 2 * n * n + b * n + c + 1; + Row[t++] = 3 * n * n + (a / tn * tn + b / tn) * n + c + 1; + new_row(t, Row, c, a * n + b); + } + } + } + if (first) + { + puts(""); + } + first = 1; + time_ = 0; + DFS(0); + } + return 0; +} diff --git a/13090.cpp b/13090.cpp new file mode 100644 index 0000000..3e9fcd1 --- /dev/null +++ b/13090.cpp @@ -0,0 +1,16 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + long long B, D; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld %lld", &B, &D); + printf("Case %d: %lld\n", ++cases, (B - 1) / D); + } + return 0; +} diff --git a/13091.cpp b/13091.cpp new file mode 100644 index 0000000..90b223f --- /dev/null +++ b/13091.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (testcase--) + { + char g[5][8]; + for (int i = 0; i < 5; i++) + scanf("%s", g[i]); + + int bpos = 0, vpos = 0, vdir = 0; + for (int i = 0; i < 5; i++) + { + if (g[0][i] == '|') + bpos = i; + for (int j = 0; j < 5; j++) + { + if (g[i][j] == '.' || g[i][j] == '|') + continue; + vpos = j, vdir = g[i][j] == '>'; + } + } + + printf("Case %d: ", ++cases); + if ((vdir && vpos > bpos) || + (!vdir && vpos < bpos)) + puts("No Ball"); + else + puts("Thik Ball"); + } + return 0; +} diff --git a/13092.cpp b/13092.cpp new file mode 100644 index 0000000..8221580 --- /dev/null +++ b/13092.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +// according to problem, we have given a string,starting from last position of string,we have to fold the string into even palindrome mirror with cost of "y" or we may erase last char with cost of "x". first, observation to solve this problem is that , from each point we should try to fold a string with maximum length possible. so, using manacer, we find from each position , minimum possible index to which it can be folded. for this, initially, put a minimum index to each end index of palindrome centered at some position. then, update this minimum values to each valid index coming from backside of string. now, finally a simple dp with two options whether to erase last char or to go to min index + +const int MX = 2000115; +const int INF = 2000000010; + +char s[MX + 2], ss[MX + 2]; +int n, m, x, y, p[MX + 2], mn[MX + 2], dp[MX + 2]; + +void manacher() +{ + m = n * 2 + 2; + ss[0] = '*'; + for (int i = 1, j = 0; i < m; i += 2, j++) + { + ss[i] = s[j]; + ss[i + 1] = '*'; + } + ss[m - 1] = '\0'; + int c = 0, r = 0, len; + for (int i = 1; i < m - 1; i++) + { + int mi = 2 * c - i; + len = (r > i) ? min(r - i, p[mi]) : 0; + while (i - 1 - len >= 0 && i + 1 + len < m && ss[i - 1 - len] == ss[i + 1 + len]) + { + ++len; + } + p[i] = len; + if (i + len > r) + { + c = i, r = i + len; + } + } +} + +int go(int p) +{ + if (p <= 0) + { + return 0; + } + int &ret = dp[p]; + if (ret != -1) + { + return ret; + } + ret = x + go(p - 1); + if (mn[p] < p) + { + ret = min(ret, y + go(mn[p])); + } + return ret; +} + +int main() +{ + int T, t = 1; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &x, &y); + scanf("%s", s); + n = strlen(s); + manacher(); + for (int i = 0; i < m; i++) + { + mn[i] = INF; + } + for (int i = 0; i < m - 1; i++) + { + if (i % 2 == 0 && p[i]) + { + // 1-based endpos in main str of pal having mirror @i + int nx = (i + p[i]) / 2; + mn[nx] = min(mn[nx], i / 2); + } + } + int pv = INF; + for (int i = n; i >= 1; i--) + { + pv = min(pv, mn[i]); + if (pv < i) + { + mn[i] = pv; + } + } + memset(dp, -1, sizeof(dp)); + int ans = go(n); + printf("Case %d: %d\n", t++, ans); + } + return 0; +} diff --git a/13093.cpp b/13093.cpp new file mode 100644 index 0000000..566e8d3 --- /dev/null +++ b/13093.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int main() +{ + char line[1024]; + while (fgets(line, 1024, stdin)) + { + stringstream sin1(line); + string b1, b2, x; + while (sin1 >> x) + b1 += x[0]; + fgets(line, 1024, stdin); + stringstream sin2(line); + while (sin2 >> x) + b2 += x[0]; + puts(b1 == b2 ? "yes" : "no"); + } + return 0; +} diff --git a/13094.cpp b/13094.cpp new file mode 100644 index 0000000..618bc05 --- /dev/null +++ b/13094.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +const int INF = 700000; +const int SZ = 780; + +int arr[600100]; +vector vect[SZ]; + +void upd(int idx, int tmp, int li) +{ + int pos = idx / SZ; + for (int i = 0; i < vect[pos].size(); i++) + { + if (vect[pos][i] == tmp) + { + vect[pos].erase(vect[pos].begin() + i); + break; + } + } + vect[pos].push_back(li); + sort(vect[pos].begin(), vect[pos].end()); +} + +int query(int s, int e, int li) +{ + int pos1 = s / SZ, pos2 = e / SZ, tmp, cnt = 0; + if (pos1 == pos2) + { + int cnt = 0; + for (int i = s; i <= e; i++) + { + if (arr[i] <= li) + { + cnt++; + } + } + return cnt; + } + pos1++; + tmp = pos1 * SZ; + for (int i = s; i < tmp; i++) + { + if (arr[i] <= li) + { + cnt++; + } + } + tmp = pos2 * SZ; + for (int i = tmp; i <= e; i++) + { + if (arr[i] <= li) + { + cnt++; + } + } + for (int i = pos1; i < pos2; i++) + { + cnt += upper_bound(vect[i].begin(), vect[i].end(), li) - vect[i].begin(); + } + return cnt; +} + +int main() +{ + int n, m, k, s, e, tmp; + while (scanf("%d%d", &n, &m) == 2) + { + for (int i = 0; i <= n + 2; i++) + { + arr[i] = INF; + } + while (m--) + { + scanf("%d", &k); + if (k == 0) + { + scanf("%d%d", &s, &tmp); + upd(s, arr[s], tmp); + arr[s] = tmp; + } + else + { + scanf("%d%d%d", &s, &e, &tmp); + printf("%d\n", query(s, e, tmp)); + } + } + for (int i = 0; i < SZ; i++) + { + vect[i].clear(); + } + } + return 0; +} diff --git a/13095.cpp b/13095.cpp new file mode 100644 index 0000000..3d22ce2 --- /dev/null +++ b/13095.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int A[131072][10]; + +int main() +{ + int n, m, x; + while (scanf("%d", &n) == 1) + { + for (int i = 1; i <= n; i++) + { + scanf("%d", &x); + for (int j = 0; j < 10; j++) + A[i][j] = A[i - 1][j]; + A[i][x]++; + } + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + int l, r, ret = 0; + scanf("%d %d", &l, &r); + for (int j = 0; j < 10; j++) + ret += A[r][j] - A[l - 1][j] > 0; + printf("%d\n", ret); + } + } + return 0; +} diff --git a/13096.cpp b/13096.cpp new file mode 100644 index 0000000..46e218b --- /dev/null +++ b/13096.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + double n; + while (scanf("%lf", &n) == 1 && n) + { + double n2 = n * 2; + double x_sq = 0, x; + double avg; + x_sq = n2 * (n2 + 1) * (2 * n2 + 1) / 6 - 4 * (n) * (n + 1) * (2 * n + 1) / 6; + x = n2 * (n2 + 1) / 2 - 2 * (n) * (n + 1) / 2; + avg = x * 1.f / n; + double S = sqrt((x_sq - 2 * avg * x + avg * avg * n) * 1.f / (n - 1)); + printf("%.6lf\n", S); + } + return 0; +} diff --git a/13097.cpp b/13097.cpp new file mode 100644 index 0000000..831d0f4 --- /dev/null +++ b/13097.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +int main() +{ + int N; + long long T; + static char buf[3][262144], dirS[1024]; + while (scanf("%d %lld", &N, &T) == 2) + { + T %= N; + scanf("%s", dirS); + while (getchar() != '\n') + ; + for (int i = 0; i < 3; i++) + fgets(buf[i], 262144, stdin); + fputs(buf[0], stdout); + int pos = dirS[0] == 'R' ? N - T : N + T; + pos = (pos + N) % N; + pos = pos * 2; + for (int i = pos; i < 2 * N; i++) + putchar(buf[1][i]); + for (int i = 0; i <= pos; i++) + putchar(buf[1][i]); + puts(""); + fputs(buf[2], stdout); + } + return 0; +} diff --git a/13098.cpp b/13098.cpp new file mode 100644 index 0000000..9a25480 --- /dev/null +++ b/13098.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +const int MAXN = 1e+7 + 5; +unsigned char dp[MAXN]; +int A[MAXN], An = 0; + +int main() +{ + memset(dp, 0x3f, sizeof(dp)); + dp[0] = 0; + for (int i = 1, x = 1; x <= MAXN; i++, x += i) + A[An++] = x; + for (int i = 0; i < An; i++) + dp[A[i]] = 1; + for (int i = 0; i < An; i++) + { + for (int j = i; j < An && A[i] + A[j] <= MAXN; j++) + dp[A[i] + A[j]] = min(dp[A[i] + A[j]], (unsigned char)2); + } + int n; + while (scanf("%d", &n) == 1) + { + if (dp[n] <= 2) + printf("%d\n", dp[n]); + else + puts("3"); + } + return 0; +} diff --git a/13099.cpp b/13099.cpp new file mode 100644 index 0000000..f499e7a --- /dev/null +++ b/13099.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + double xl, yl, xr, yr; + while (scanf("%lf%lf%lf%lf", &xl, &yl, &xr, &yr) == 4) + { + printf("%.6lf\n", ((xr - xl) * (xr - xl) + (yr - yl) * (yr - yl)) / 6); + } + return 0; +} diff --git a/131.cpp b/131.cpp new file mode 100644 index 0000000..be867fb --- /dev/null +++ b/131.cpp @@ -0,0 +1,354 @@ +#include + +using namespace std; + +struct poke +{ + char f; + char s; + int v; +}; + +poke hand[5], deck[5], temp[5]; + +char Ans[][20] = + { + "straight-flush", + "four-of-a-kind", + "full-house", + "flush", + "straight", + "three-of-a-kind", + "two-pairs", + "one-pair", + "highest-card"}; + +int data[1100][5]; +int c = 0; + +void print_subset(int s, int n) +{ + for (int i = 0; i < n; ++i) + if (s & 1 << i) + { + data[c][i] = 1; + } + else + { + data[c][i] = 0; + } + c++; + return; +} + +void subset(int n) +{ + for (int i = 0; i < 1 << n; ++i) + { + print_subset(i, n); + } + return; +} + +void output(int ans) +{ + cout << "Hand: "; + for (int i = 0; i < 5; ++i) + { + cout << hand[i].f << hand[i].s << " "; + } + cout << "Deck: "; + for (int i = 0; i < 5; ++i) + { + cout << deck[i].f << deck[i].s << " "; + } + cout << "Best hand: "; + cout << Ans[ans] << endl; + return; +} + +int cmp(const void *_a, const void *_b) +{ + poke *a = (poke *)_a; + poke *b = (poke *)_b; + return a->v - b->v; +} + +bool is_straight_flush(poke p[]) +{ + int v = p[0].v; + char s = p[0].s; + for (int i = 1; i < 5; ++i) + if (p[i].s != s) + { + return false; + } + for (int i = 1; i < 4; ++i) + if (p[i - 1].v + 1 != p[i].v) + { + return false; + } + if (v == 2 && p[4].v == 14) + { + return true; + } + if (p[4].v == p[3].v + 1) + { + return true; + } +} + +bool is_four_of_a_kind(poke p[]) +{ + int cc1 = 0, cc2 = 0, v1 = p[0].v, v2 = p[4].v; + for (int i = 0; i < 5; ++i) + if (p[i].v == v1) + { + cc1++; + } + else if (p[i].v == v2) + { + cc2++; + } + return cc1 == 4 || cc2 == 4; +} + +bool is_full_house(poke p[]) +{ + int cc1 = 0, cc2 = 0, v1 = p[0].v, v2 = p[4].v; + for (int i = 0; i < 5; ++i) + if (p[i].v == v1) + { + cc1++; + } + else if (p[i].v == v2) + { + cc2++; + } + return (cc1 == 2 && cc2 == 3) || (cc1 == 3 && cc2 == 2); +} + +bool is_flush(poke p[]) +{ + char s = p[0].s; + for (int i = 1; i < 5; ++i) + if (p[i].s != s) + { + return false; + } + return true; +} + +bool is_straight(poke p[]) +{ + int v = p[0].v; + for (int i = 1; i < 4; ++i) + if (p[i - 1].v + 1 != p[i].v) + { + return false; + } + if (v == 2 && p[4].v == 14) + { + return true; + } + if (p[4].v == p[3].v + 1) + { + return true; + } + return false; +} + +bool is_three_of_a_kind(poke p[]) +{ + int cc1 = 0, cc2 = 0, v1 = p[0].v, v2 = p[4].v; + for (int i = 0; i < 5; ++i) + if (p[i].v == v1) + { + cc1++; + } + else if (p[i].v == v2) + { + cc2++; + } + return cc1 == 3 || cc2 == 3; +} + +bool is_two_pairs(poke p[]) +{ + int cc1 = 1, v = p[0].v; + for (int i = 1; i < 5; ++i) + { + if (p[i - 1].v != p[i].v) + { + cc1++; + } + } + return cc1 == 3; +} + +bool is_one_pair(poke p[]) +{ + int cc1 = 1, v = p[0].v; + for (int i = 1; i < 5; ++i) + { + if (p[i - 1].v != p[i].v) + { + cc1++; + } + } + return cc1 == 4; +} + +int match(poke p[]) +{ + if (is_straight_flush(p)) + { + return 0; + } + if (is_four_of_a_kind(p)) + { + return 1; + } + if (is_full_house(p)) + { + return 2; + } + if (is_flush(p)) + { + return 3; + } + if (is_straight(p)) + { + return 4; + } + if (is_three_of_a_kind(p)) + { + return 5; + } + if (is_two_pairs(p)) + { + return 6; + } + if (is_one_pair(p)) + { + return 7; + } + return 8; +} + +int main() +{ + subset(5); + char str[100]; + while (gets(str)) + { + int c1 = 0, c2 = 0; + int len = strlen(str); + for (int i = 0; i < len; i += 3) + { + if (i < len / 2) + { + if (isalpha(str[i])) + { + switch (str[i]) + { + case 'T': + hand[c1].f = str[i]; + hand[c1].s = str[i + 1]; + hand[c1].v = 10; + break; + case 'J': + hand[c1].f = str[i]; + hand[c1].s = str[i + 1]; + hand[c1].v = 11; + break; + case 'Q': + hand[c1].f = str[i]; + hand[c1].s = str[i + 1]; + hand[c1].v = 12; + break; + case 'K': + hand[c1].f = str[i]; + hand[c1].s = str[i + 1]; + hand[c1].v = 13; + break; + case 'A': + hand[c1].f = str[i]; + hand[c1].s = str[i + 1]; + hand[c1].v = 14; + break; + } + } + else + { + hand[c1].v = str[i] - '0', hand[c1].s = str[i + 1], hand[c1].f = str[i]; + } + c1++; + } + else + { + if (isalpha(str[i])) + { + switch (str[i]) + { + case 'T': + deck[c2].f = str[i]; + deck[c2].s = str[i + 1]; + deck[c2].v = 10; + break; + case 'J': + deck[c2].f = str[i]; + deck[c2].s = str[i + 1]; + deck[c2].v = 11; + break; + case 'Q': + deck[c2].f = str[i]; + deck[c2].s = str[i + 1]; + deck[c2].v = 12; + break; + case 'K': + deck[c2].f = str[i]; + deck[c2].s = str[i + 1]; + deck[c2].v = 13; + break; + case 'A': + deck[c2].f = str[i]; + deck[c2].s = str[i + 1]; + deck[c2].v = 14; + break; + } + } + else + { + deck[c2].v = str[i] - '0', deck[c2].s = str[i + 1], deck[c2].f = str[i]; + } + c2++; + } + } + int ans = 8; + for (int i = 0; i < c; ++i) + { + int cc = 0; + for (int j = 0; j < 5; ++j) + { + if (data[i][j]) + { + temp[j] = deck[cc++]; + } + else + { + temp[j] = hand[j]; + } + } + qsort(temp, 5, sizeof(poke), cmp); + int t = match(temp); + if (ans > t) + { + ans = t; + } + memset(temp, 0, sizeof(temp)); + } + output(ans); + } + return 0; +} diff --git a/1310.cpp b/1310.cpp new file mode 100644 index 0000000..33c69d0 --- /dev/null +++ b/1310.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define maxn 2010 + +int n, m; + +int g[maxn][maxn]; +int lowu[maxn]; +int pre[maxn]; +int dfs_clock; + +void dfs(int u, int fa) +{ + int i, j; + pre[u] = lowu[u] = ++dfs_clock; + for (i = 1; i <= n; i++) + { + if ((g[u][i] || g[i][u]) && i != fa) + { + if (pre[i] == -1) + { + dfs(i, u); + lowu[u] = min(lowu[u], lowu[i]); + if (lowu[i] > pre[u]) + { + printf("%d %d %d\n", u, i, 2); + g[u][i] = g[i][u] = 0; + } + } + else + { + lowu[u] = min(lowu[u], lowu[i]); + } + } + } +} + +void dfs2(int u, int fa) +{ + int i, j; + pre[u] = lowu[u] = ++dfs_clock; + for (i = 1; i <= n; i++) + { + if (g[u][i] && i != fa) + { + if (pre[i] == -1) + { + dfs2(i, u); + lowu[u] = min(lowu[u], lowu[i]); + if (g[u][i] && g[i][u]) + { + if (lowu[i] > pre[u]) + { + printf("%d %d %d\n", i, u, 1); + g[u][i] = 0; + } + else + { + printf("%d %d %d\n", u, i, 1); + g[i][u] = 0; + } + } + } + else + { + lowu[u] = min(lowu[u], pre[i]); + if (g[u][i] && g[i][u]) + { + printf("%d %d %d\n", u, i, 1); + g[i][u] = 0; + } + } + } + } +} + +int main() +{ + // freopen("input.txt","r",stdin); + int i, j; + while (scanf("%d %d", &n, &m) == 2) + { + memset(g, 0, sizeof(g)); + for (i = 0; i < m; i++) + { + int a, b, c; + scanf("%d %d %d", &a, &b, &c); + if (c == 1) + { + g[a][b] = 1; + } + else + { + g[a][b] = g[b][a] = 1; + } + } + memset(pre, -1, sizeof(pre)); + dfs_clock = 0; + dfs(1, -1); + memset(pre, -1, sizeof(pre)); + dfs_clock = 0; + for (i = 1; i <= n; i++) + { + if (pre[i] == -1) + { + dfs2(i, -1); + } + } + } + return 0; +} diff --git a/13100.cpp b/13100.cpp new file mode 100644 index 0000000..4e3b982 --- /dev/null +++ b/13100.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; + +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + assert(n < MAXV); + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + assert(e < MAXE); + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} g; +const int MAXN = 1024 * 1024; +int mx[MAXN], my[MAXN]; +int X[MAXN], Y[MAXN]; +int n, m, VX, VY; +int C[MAXN][3], R[MAXN][3]; +void dfs(int u) +{ + if (X[u]) + return; + X[u] = 1; + for (Edge *p = g.adj[u]; p != NULL; p = p->next) + { + if (p->v - VX >= 1 && p->v - VX <= VY) + { + if (my[p->v - VX] != -1 && Y[p->v - VX] == 0) + { + Y[p->v - VX] = 1; + dfs(my[p->v - VX]); + } + } + } +} + +int main() +{ + static char s[1024][1024]; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + scanf("%s", s[i] + 1); + + static int xg[1024][1024] = {}; + static int yg[1024][1024] = {}; + memset(xg, 0, sizeof(xg)); + memset(yg, 0, sizeof(yg)); + VX = 0, VY = 0; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (s[i][j] == '*') + { + if (xg[i][j] == 0) + { + xg[i][j] = ++VX; + int k = j; + while (k <= m && s[i][k] == '*') + xg[i][k] = VX, k++; + C[VX][0] = i, C[VX][1] = j, C[VX][2] = k - 1; + } + if (yg[i][j] == 0) + { + yg[i][j] = ++VY; + int k = i; + while (k <= n && s[k][j] == '*') + yg[k][j] = VY, k++; + R[VY][0] = j, R[VY][1] = i, R[VY][2] = k - 1; + } + } + } + } + + int source = 0, sink = VX + VY + 1; + g.Init(VX + VY + 2); + for (int i = 1; i <= VX; i++) + g.Addedge(source, i, 1); + for (int i = 1; i <= VY; i++) + g.Addedge(VX + i, sink, 1); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (s[i][j] == '*') + { + g.Addedge(xg[i][j], yg[i][j] + VX, 1); + } + } + } + int mxflow = g.Maxflow(source, sink); + memset(mx, -1, sizeof(mx)); + memset(my, -1, sizeof(my)); + memset(X, 0, sizeof(X)); + memset(Y, 0, sizeof(Y)); + int match = 0; + for (int i = 1; i <= VX; i++) + { + for (Edge *p = g.adj[i]; p != NULL; p = p->next) + { + int x = i, y = p->v, flow = p->flow; + if (flow == 0 && y - VX >= 1 && y - VX <= VY) + { + // match x - (y - VX) in bipartite graph + int r = x, c = y - VX; + mx[r] = c; + my[c] = r; + match++; + } + } + } + assert(match == mxflow); + + for (int i = 1; i <= VX; i++) + { + if (mx[i] == -1) + dfs(i); + } + + printf("%d\n", mxflow); + for (int i = 1; i <= VX; i++) + { + if (!X[i] && mx[i] != -1) + { + printf("hline %d %d %d\n", C[i][0], C[i][1], C[i][2]); + mxflow--; + } + } + for (int i = 1; i <= VY; i++) + { + if (Y[i]) + { + printf("vline %d %d %d\n", R[i][0], R[i][1], R[i][2]); + mxflow--; + } + } + assert(mxflow == 0); + } + return 0; +} diff --git a/13101.cpp b/13101.cpp new file mode 100644 index 0000000..42e73b1 --- /dev/null +++ b/13101.cpp @@ -0,0 +1,207 @@ +#include + +using namespace std; + +class LCT +{// Link-Cut Tree +public: + static const int MAXN = 262144; + struct Node + { + static Node *EMPTY; + Node *ch[2], *fa; + int rev; + int val, size; + int gcd; + Node() + { + ch[0] = ch[1] = fa = NULL; + rev = 0; + val = 0; + gcd = 1, size = 1; + } + bool is_root() + { + return fa->ch[0] != this && fa->ch[1] != this; + } + void pushdown() + { + if (rev) + { + ch[0]->rev ^= 1, ch[1]->rev ^= 1; + swap(ch[0], ch[1]); + rev ^= 1; + } + } + void pushup() + { + if (this == EMPTY) + return; + gcd = this->val, size = 1; + if (ch[0] != EMPTY) + { + gcd = __gcd(gcd, ch[0]->gcd); + size += ch[0]->size; + } + if (ch[1] != EMPTY) + { + gcd = __gcd(gcd, ch[1]->gcd); + size += ch[1]->size; + } + } + } _mem[MAXN]; + + int bufIdx; + LCT() + { + Node::EMPTY = &_mem[0]; + Node::EMPTY->fa = Node::EMPTY->ch[0] = Node::EMPTY->ch[1] = Node::EMPTY; + Node::EMPTY->size = 0; + bufIdx = 1; + } + void init() + { + bufIdx = 1; + } + Node *newNode() + { + Node *u = &_mem[bufIdx++]; + *u = Node(); + u->fa = u->ch[0] = u->ch[1] = Node::EMPTY; + return u; + } + void rotate(Node *x) + { + Node *y; + int d; + y = x->fa, d = y->ch[1] == x ? 1 : 0; + x->ch[d ^ 1]->fa = y, y->ch[d] = x->ch[d ^ 1]; + x->ch[d ^ 1] = y; + if (!y->is_root()) + y->fa->ch[y->fa->ch[1] == y] = x; + x->fa = y->fa, y->fa = x; + y->pushup(), x->pushup(); + } + void deal(Node *x) + { + if (!x->is_root()) + deal(x->fa); + x->pushdown(); + } + void splay(Node *x) + { + Node *y, *z; + deal(x); + while (!x->is_root()) + { + y = x->fa, z = y->fa; + if (!y->is_root()) + { + if (y->ch[0] == x ^ z->ch[0] == y) + rotate(x); + else + rotate(y); + } + rotate(x); + } + x->pushup(); + } + Node *access(Node *u) + { + Node *v = Node::EMPTY; + for (; u != Node::EMPTY; u = u->fa) + { + splay(u); + u->ch[1] = v; + v = u; + v->pushup(); + } + return v; + } + void mk_root(Node *u) + { + access(u)->rev ^= 1, splay(u); + } + void cut(Node *x, Node *y) + { + mk_root(x); + access(y), splay(y); + y->ch[0] = x->fa = Node::EMPTY; + } + Node *_cut(Node *rt, Node *x) + { + Node *u, *v; + mk_root(rt); + access(x), splay(x); + for (v = x->ch[0]; v->ch[1] != Node::EMPTY; v = v->ch[1]) + ; + x->ch[0]->fa = x->fa; + x->fa = x->ch[0] = Node::EMPTY; + return v; + } + void link(Node *x, Node *y) + { + mk_root(x); + x->fa = y; + } + Node *find(Node *x) + { + for (x = access(x); x->pushdown(), x->ch[0] != Node::EMPTY; x = x->ch[0]) + ; + return x; + } + // + int gcdPath(Node *x, Node *y) + { + mk_root(x); + access(y), splay(y); + return y->gcd; + } + // + void changeNode(Node *x, int c) + { + mk_root(x); + x->val = c; + } + void debug(int n) + { + } +} tree; +LCT::Node *LCT::Node::EMPTY; +LCT::Node *A[262144]; + +int W[131072]; +int main() +{ + int n, m, cmd, x, y; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &W[i]); + tree.init(); + for (int i = 0; i < n; i++) + { + A[i] = tree.newNode(); + A[i]->val = W[i]; + } + for (int i = 1; i < n; i++) + { + scanf("%d %d", &x, &y); + tree.link(A[x], A[y]); + } + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &cmd, &x, &y); + if (cmd == 1) + { + printf("%d\n", tree.gcdPath(A[x], A[y])); + } + else + { + tree.changeNode(A[x], y); + } + } + } + return 0; +} diff --git a/13103.cpp b/13103.cpp new file mode 100644 index 0000000..539c6d6 --- /dev/null +++ b/13103.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + long long N; + int K, A[128]; + while (scanf("%lld %d", &N, &K) == 2) + { + for (int i = 0; i < K; i++) + scanf("%d", &A[i]); + sort(A, A + K); + + int one = 0; + for (int i = 0; i < K; i++) + { + if ((N >> A[i]) & 1) + { + one++; + N ^= 1LL << A[i]; + } + } + long long ret = 0; + for (int i = (1 << K) - 1; i >= 0; i--) + { + if (__builtin_popcount(i) != one) + continue; + long long tN = N; + for (int j = 0; j < K; j++) + { + if ((i >> j) & 1) + tN |= 1LL << A[j]; + } + if (tN % 7 == 0) + ret = max(ret, tN); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/13104.cpp b/13104.cpp new file mode 100644 index 0000000..65cbb63 --- /dev/null +++ b/13104.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define MAXL (50000 >> 5) + 1 +#define GET1(x) (mark1[(x) >> 5] >> ((x)&31) & 1) +#define SET1(x) (mark1[(x) >> 5] |= 1 << ((x)&31)) + +int mark1[MAXL]; +int P[5500], Pt = 0; +const int MOD = 1e9 + 7; + +void sieve() +{ + register int i, j, k; + SET1(1) + , SET1(0); + int n = 50000; + for (i = 2; i <= n; i++) + { + if (!GET1(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET1(j); + P[Pt++] = i; + } + } +} +int dp[512][10]; +int dp_sum[512][10][5015]; +void build() +{ + int f[2][5015] = {}; + int p = 0, q = 1; + f[0][0] = 1, dp_sum[0][0][0] = 1; + for (int i = 0; i < 500; i++) + { + memset(f[q], 0, sizeof(f[0])); + for (int k = 0; k <= i * 10; k++) + { + if (!f[p][k]) + continue; + for (int r = 0; r < 10; r++) + { + f[q][k + r] += f[p][k]; + if (f[q][k + r] >= MOD) + f[q][k + r] -= MOD; + dp_sum[i + 1][r][k + r] += f[p][k]; + if (dp_sum[i + 1][r][k + r] >= MOD) + dp_sum[i + 1][r][k + r] -= MOD; + if (!GET1(k + r)) + { + dp[i + 1][r] += f[p][k]; + if (dp[i + 1][r] >= MOD) + dp[i + 1][r] -= MOD; + } + } + } + p ^= 1, q ^= 1; + } + // for (int i = 1; i <= 2; i++) { + // for (int j = 0; j < 10; j++) + // printf("%d ", dp[i][j]); + // puts(""); + // } +} + +long long f(char s[]) +{ + int n = strlen(s); + long long ret = 0; + for (int i = 1; i < n; i++) + { + for (int j = 1; j < 10; j++) + { + ret += dp[i][j]; + if (ret >= MOD) + ret -= MOD; + } + } + // printf("%d\n", GET1(8)); + // printf("--- %lld\n", ret); + long long base = 0, pre = 0, sum = 0; + for (int i = 0; i < n; i++) + { + int d = s[i] - '0'; + for (int j = (i == 0); j < d; j++) + { + for (int k = 0; k <= (n - i) * 10; k++) + { + if (GET1((k + sum)) == 0) + { + if (dp_sum[n - i][j][k] == 0) + continue; + // printf("digits %d, head %d, sum %d, %lld %d\n",n-i, j, k, pre - base + 1, dp_sum[n-i][j][k]); + ret = (ret + (pre - base + 1) * dp_sum[n - i][j][k] % MOD) % MOD; + } + } + } + pre = pre * 10 + d; + pre %= MOD; + base = base * 10 + d; + base %= MOD; + sum += d; + } + if (!GET1(sum)) + ret++; + ret = ret % MOD; + return ret; +} + +int main() +{ + sieve(); + build(); + char s1[1024], s2[1024]; + while (scanf("%s %s", s1, s2) == 2) + { + int sum = 0; + for (int i = 0; s1[i]; i++) + sum += s1[i] - '0'; + long long ret = f(s2) - f(s1) + (GET1(sum) == 0); + ret = (ret % MOD + MOD) % MOD; + printf("%lld\n", ret); + } + return 0; +} diff --git a/13105.cpp b/13105.cpp new file mode 100644 index 0000000..77397f7 --- /dev/null +++ b/13105.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +const int MAXN = 20002; + +class DisjointSet +{ +public: + short parent[MAXN], weight[MAXN]; + int findp(int x) + { + if (parent[x] == -1) + return -1; + return parent[x] == x ? x : parent[x] = findp(parent[x]); + } + int joint(int tx, int ty) + { + int x = findp(tx), y = findp(ty); + if (x == y && x != -1) + return 0; + if (x == -1) + x = tx, parent[tx] = tx; + if (y == -1) + y = ty, parent[ty] = ty; + if (weight[x] > weight[y]) + weight[x] += weight[y], parent[y] = x; + else + weight[y] += weight[x], parent[x] = y; + return 1; + } + void init(int n) + { + for (int i = 0; i <= n; i++) + parent[i] = -1, weight[i] = 1; + } +} D; +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + return EOF; + p = buf; + } + return *p++; +} +inline int ReadInt(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + return 0; + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + *x = (*x << 3) + (*x << 1) + c - '0'; + *x *= neg; + return 1; +} +int mkFriend(int x, int y) +{ + D.joint(x * 2, y * 2); + D.joint(x * 2 + 1, y * 2 + 1); + ; +} +int mkEmeny(int x, int y) +{ + D.joint(x * 2, y * 2 + 1); + D.joint(x * 2 + 1, y * 2); +} +int isFriend(int x, int y) +{ + int xp, xn, yp, yn; + xp = D.findp(x * 2); + xn = D.findp(x * 2 + 1); + yp = D.findp(y * 2); + yn = D.findp(y * 2 + 1); + if (x == y) + return 1; + if (xp == -1 || yp == -1) + return -1; + if (xp == yp) + return 1; + return 0; +} +int isEmeny(int x, int y) +{ + int xp, xn, yp, yn; + xp = D.findp(x * 2); + xn = D.findp(x * 2 + 1); + yp = D.findp(y * 2); + yn = D.findp(y * 2 + 1); + if (xp == -1 || yp == -1) + return -1; + if (xp == yn) + return 1; + return 0; +} + +int main() +{ + int n, m; + static char buffer[16777216]; + static char *bptr = buffer; + while (ReadInt(&n)) + { + ReadInt(&m); + D.init(2 * n); + int cmd, x, y; + for (int i = 0; i < m; i++) + { + ReadInt(&cmd), ReadInt(&x), ReadInt(&y); + if (cmd == 1) + { + if (isEmeny(x, y) == 1) // emeny + (*bptr) = '-', bptr++, (*bptr) = '1', bptr++, (*bptr) = '\n', bptr++; + else + mkFriend(x, y); + } + else if (cmd == 2) + { + if (isFriend(x, y) == 1) // friend + (*bptr) = '-', bptr++, (*bptr) = '1', bptr++, (*bptr) = '\n', bptr++; + else + mkEmeny(x, y); + } + else if (cmd == 3) + { + int t = isFriend(x, y) > 0; + (*bptr) = '0' + t, bptr++, (*bptr) = '\n', bptr++; + } + else + { + int t = isEmeny(x, y) > 0; + (*bptr) = '0' + t, bptr++, (*bptr) = '\n', bptr++; + } + } + } + *bptr = '\0'; + fputs(buffer, stdout); + return 0; +} diff --git a/13106.cpp b/13106.cpp new file mode 100644 index 0000000..dd35f5b --- /dev/null +++ b/13106.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +const int MAXN = 512; +int g[MAXN][MAXN]; + +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + scanf("%d", &g[i][j]); + } + + int gc[MAXN] = {}; + for (int i = 1; i <= m; i++) + { + int dp[MAXN] = {}; + for (int j = 1; j <= n; j++) + dp[j] = max(dp[j - 1], (j - 2 >= 0 ? dp[j - 2] : 0) + g[j][i]); + for (int j = 1; j <= n; j++) + gc[i] = max(gc[i], dp[j]); + } + + int dp[MAXN] = {}, ret = 0; + for (int i = 1; i <= m; i++) + { + dp[i] = max(dp[i - 1], (i - 2 >= 0 ? dp[i - 2] : 0) + gc[i]); + ret = max(ret, dp[i]); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13107.cpp b/13107.cpp new file mode 100644 index 0000000..8b46634 --- /dev/null +++ b/13107.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +static char buf[262144], *p; +void printInt(int x) +{ + static char stk[16]; + int idx = 0; + static char m[10] = {0, 1, 5, 3, 4, 2, 9, 7, 8, 6}; + if (!x) + stk[idx++] = '0'; + while (x) + stk[idx++] = m[x % 10] + '0', x /= 10; + while (idx) + *p = stk[--idx], p++; +} + +int main() +{ + char s[131072]; + while (scanf("%s", s) == 1) + { + int used[128] = {}, t = 0; + p = buf; + for (int i = 0; s[i]; i++) + { + int u; + if (used[s[i]] == 0) + { + used[s[i]] = ++t; + } + u = used[s[i]]; + printInt(u); + } + *p = '\0'; + puts(buf); + } + return 0; +} diff --git a/13108.cpp b/13108.cpp new file mode 100644 index 0000000..cc2087b --- /dev/null +++ b/13108.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +/* +A000127 + Maximal number of regions obtained by joining n points around a circle by straight lines. + Also number of regions in 4-space formed by n-1 hyperplanes. +*/ +// (n^4-6*n^3+23*n^2-18*n+24)/24 +int main() +{ + int testcase; + long long n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld", &n); + long long ret; + ret = n * n * n * n - 6 * n * n * n + 23 * n * n - 18 * n + 24; + ret /= 24; + printf("%lld\n", ret); + } + return 0; +} diff --git a/13109.cpp b/13109.cpp new file mode 100644 index 0000000..d0ed669 --- /dev/null +++ b/13109.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n, m; + static int A[131072]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + sort(A, A + n); + int ret = 0; + for (int i = 0; i < n && A[i] <= m; i++) + ret++, m -= A[i]; + printf("%d\n", ret); + } + return 0; +} diff --git a/13110.cpp b/13110.cpp new file mode 100644 index 0000000..b1267c0 --- /dev/null +++ b/13110.cpp @@ -0,0 +1,173 @@ +#include + +using namespace std; + +const int MAXN = 32767; + +class DisjointSet +{ +public: + int parent[MAXN], weight[MAXN]; + int findp(int x) + { + return parent[x] == x ? x : parent[x] = findp(parent[x]); + } + int joint(int tx, int ty) + { + int x = findp(tx), y = findp(ty); + if (x == y) + return 0; + if (weight[x] > weight[y]) + weight[x] += weight[y], parent[y] = x; + else + weight[y] += weight[x], parent[x] = y; + return 1; + } + void init(int n) + { + for (int i = 0; i <= n; i++) + parent[i] = i, weight[i] = 1; + } +} D; + +int N, C; +vector AG[MAXN]; +int deg[MAXN] = {}; +int isValid(int ban) +{ + int valid = 1, one_comp = 1; + D.init(C); + for (int i = 0; i < N; i++) + { + if (i == ban) + continue; + if (AG[i].size() == 2) + D.joint(AG[i][0], AG[i][1]); + } + for (int i = 0; i < N; i++) + { + int x, y; + if (AG[i].size() == 2) + { + x = AG[i][0], y = AG[i][1]; + } + else + { + x = AG[i][0], y = AG[i][0]; + } + if (D.findp(x) != D.findp(AG[0][0]) || D.findp(y) != D.findp(AG[0][0])) + one_comp = 0; + } + if (!one_comp) + { + int u = AG[ban][0]; + int v = AG[ban][1]; + if (!(deg[u] == 1 || deg[v] == 1)) + valid = 0; + } + return valid; +} +int main() +{ + int line = 0, cases = 0; + while (scanf("%d %d", &N, &C) == 2) + { + line++; + D.init(C); + for (int i = 0; i < N; i++) + AG[i].clear(); + for (int i = 0; i < C; i++) + deg[i] = 0; + + int x, n; + for (int i = 0; i < C; i++) + { + scanf("%d", &n); + for (int j = 0; j < n; j++) + { + scanf("%d", &x); + assert(x < N && x >= 0); + AG[x].push_back(i); + } + line++; + } + + vector G[MAXN]; + for (int i = 0; i < N; i++) + { + if (AG[i].size() == 2) + { + D.joint(AG[i][0], AG[i][1]); + deg[AG[i][0]]++; + deg[AG[i][1]]++; + G[AG[i][0]].push_back(i); + G[AG[i][1]].push_back(i); + // printf("%d %d --\n", AG[i][0], AG[i][1]); + } + else + { + assert(AG[i].size() == 1); + // printf("%d %d --\n", AG[i][0], AG[i][0]); + deg[AG[i][0]]++; + deg[AG[i][0]]++; + G[AG[i][0]].push_back(i); + } + } + + int oddcnt = 0; + int cand[3] = {INT_MAX, INT_MAX, INT_MAX}; + for (int i = 0; i < C; i++) + { + if (deg[i] & 1) + { + oddcnt++; + for (int j = 0; j < G[i].size(); j++) + { + x = G[i][j]; + if (x == cand[0] || x == cand[1] || x == cand[2]) + continue; + if (x < cand[2]) + cand[2] = x; + if (cand[2] < cand[1]) + swap(cand[2], cand[1]); + if (cand[1] < cand[0]) + swap(cand[0], cand[1]); + } + } + } + + int valid = 1; + for (int i = 0; i < N; i++) + { + int x, y; + if (AG[i].size() == 2) + { + x = AG[i][0], y = AG[i][1]; + } + else + { + x = AG[i][0], y = AG[i][0]; + } + if (D.findp(x) != D.findp(AG[0][0]) || D.findp(y) != D.findp(AG[0][0])) + valid = 0; + } + if (valid == 0 || oddcnt > 2) + { + puts("-1"); + } + else if (oddcnt == 0) + { + puts("0"); + } + else + { + if (isValid(cand[0])) + printf("%d\n", cand[0]); + else if (isValid(cand[1])) + printf("%d\n", cand[1]); + else + printf("%d\n", cand[2]); + } + } + return 0; +} diff --git a/13111.cpp b/13111.cpp new file mode 100644 index 0000000..7d9b78a --- /dev/null +++ b/13111.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int MAXN = 22; +int G[1024][MAXN][MAXN]; +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int N, M, P, SX, SY; + scanf("%d %d %d", &N, &M, &P); + scanf("%d %d", &SX, &SY); + + memset(G, 0, sizeof(G)); + int T = 0; + for (int i = 0; i < P; i++) + { + int x, y, t, v; + scanf("%d %d %d %d", &x, &y, &t, &v); + G[t][x][y] = v; + T = max(T, t); + } + + int dp[MAXN][MAXN]; + const int dx[] = {0, 0, 1, -1, 0}; + const int dy[] = {1, -1, 0, 0, 0}; + memset(dp, -1, sizeof(dp)); + dp[SX][SY] = 0; + for (int t = 1; t <= T; t++) + { + int next[MAXN][MAXN]; + memcpy(next, dp, sizeof(dp)); + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= M; j++) + { + if (dp[i][j] == -1) + continue; + for (int k = 0; k < 5; k++) + { + int tx = i + dx[k]; + int ty = j + dy[k]; + if (tx < 1 || ty < 1 || tx > N || ty > M) + continue; + next[tx][ty] = max(next[tx][ty], dp[i][j] + G[t][tx][ty]); + } + } + } + memcpy(dp, next, sizeof(dp)); + } + + int ret = 0; + for (int i = 1; i <= N; i++) + { + for (int j = 1; j <= M; j++) + ret = max(ret, dp[i][j]); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13112.cpp b/13112.cpp new file mode 100644 index 0000000..1ec67c8 --- /dev/null +++ b/13112.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define eps 1e-6 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; +const double pi = acos(-1); +int cmpZero(double v) +{ + if (fabs(v) > eps) + return v > 0 ? 1 : -1; + return 0; +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +int inPolygon(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (p[i] == p[j]) + continue; + if (onSeg(p[i], p[j], q)) + return 0; + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + cnt++; + } + return cnt & 1; +} + +int main() +{ + int testcase, n, m; + scanf("%d", &testcase); + const int MAXN = 1024; + Pt A[MAXN]; + while (testcase--) + { + scanf("%d %d", &n, &m); + set S; + for (int i = 0; i < n; i++) + A[i].read(); + int ret = 0; + for (int i = 0; i < m; i++) + { + Pt q; + q.read(); + if (inPolygon(A, n, q)) + ret++; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13113.cpp b/13113.cpp new file mode 100644 index 0000000..ec70b9a --- /dev/null +++ b/13113.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +double P[1024][512], V[1024]; +bool cmp(pair a, pair b) +{ + if (a.first != b.first) + return a.first > b.first; + return a.second < b.second; +} + +int main() +{ + int testcase, A, B; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &A, &B); + double T[1024] = {}, tot = 0; + int out[1024] = {}; + for (int i = 0; i < B; i++) + { + for (int j = 0; j < A; j++) + scanf("%lf", &P[i][j]); + scanf("%lf", &V[i]); + tot += V[i]; + for (int j = 0; j < A; j++) + T[j] += P[i][j] * V[i]; + } + vector> O; + for (int i = 0; i < A; i++) + O.push_back(make_pair(T[i], i + 1)); + sort(O.begin(), O.end(), cmp); + int ret = 2; + if (O[0].first >= tot * 50.1) + ret = 1; + for (int i = 0; i < O.size() && i < ret; i++) + printf("%d %.lf\n", O[i].second, O[i].first / 100); + if (testcase) + puts(""); + } + return 0; +} diff --git a/13114.cpp b/13114.cpp new file mode 100644 index 0000000..80a12bd --- /dev/null +++ b/13114.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int josephus(int n, int k) +{ + static int st[1 << 20]; + int M; + for (M = 1; M < n + 1; M <<= 1) + ; + for (int i = 2 * M - 1; i > 0; i--) + { + if (i >= M) + st[i] = 1; + else + st[i] = st[i << 1] + st[i << 1 | 1]; + } + int last, prev = 0; + for (int i = 1; i <= n; i++) + { + int m, s, sz = n - i + 1; + if (i % 2) + m = ((prev + k) % sz + sz) % sz; + else + m = ((prev - k + 2) % sz + sz) % sz; + if (m == 0) + m = sz; + if (i % 2) + prev = m - 1; + else + prev = m - 2; + for (s = 1; s < M;) + { + if (st[s << 1] < m) + m -= st[s << 1], s = s << 1 | 1; + else + s = s << 1; + } + last = s - M + 1; + while (s) + { + st[s]--, s >>= 1; + } + } + return last; +} + +int main() +{ + int n = 0; + int ret[101] = {}; + for (int i = 1; i <= 10000 && n < 100; i++) + { + int live = josephus(100, i); + if (ret[live] == 0) + ret[live] = i, n++; + } + while (scanf("%d", &n) == 1 && n) + { + printf("%d\n", ret[n]); + } + return 0; +} diff --git a/13115.cpp b/13115.cpp new file mode 100644 index 0000000..f771ba5 --- /dev/null +++ b/13115.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int n, m; + scanf("%d", &n); + for (m = 1; m * m < n; m++) + ; + + int ret = 1, x; + int mark[3][32][32] = {}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + scanf("%d", &x); + mark[0][i][x]++; + ret &= mark[0][i][x] == 1; + mark[1][j][x]++; + ret &= mark[1][j][x] == 1; + mark[2][i / m * m + j / m][x]++; + ret &= mark[2][i / m * m + j / m][x] == 1; + } + } + puts(ret ? "yes" : "no"); + } + return 0; +} diff --git a/13116.cpp b/13116.cpp new file mode 100644 index 0000000..388c259 --- /dev/null +++ b/13116.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +char g[128][128][128]; +int used[128][128][128]; +int main() +{ + int W, L, H; + while (scanf("%d %d %d", &W, &L, &H) == 3 && W) + { + int sx, sy, sz; + int ex, ey, ez; + for (int i = 0; i < H; i++) + { + for (int j = 0; j < W; j++) + { + scanf("%s", g[i][j]); + for (int k = 0; k < L; k++) + { + if (g[i][j][k] == 'S') + { + sx = j, sy = k, sz = i; + } + if (g[i][j][k] == 'E') + { + ex = j, ey = k, ez = i; + } + } + } + } + + memset(used, 0, sizeof(used)); + queue X, Y, Z; + X.push(sx); + Y.push(sy); + Z.push(sz); + used[sz][sx][sy] = 1; + const int dx[4] = {0, 0, 1, -1}; + const int dy[4] = {1, -1, 0, 0}; + const int dz[2] = {1, -1}; + while (!X.empty()) + { + sx = X.front(), X.pop(); + sy = Y.front(), Y.pop(); + sz = Z.front(), Z.pop(); + int cc = used[sz][sx][sy] + 1; + for (int i = 0; i < 4; i++) + { + int tx, ty; + tx = sx + dx[i]; + ty = sy + dy[i]; + if (tx < 0 || ty < 0 || tx >= W || ty >= L) + continue; + if (g[sz][tx][ty] == '#' || used[sz][tx][ty]) + continue; + used[sz][tx][ty] = cc; + X.push(tx); + Y.push(ty); + Z.push(sz); + } + if (g[sz][sx][sy] == '-') + { + for (int i = 0; i < 2; i++) + { + int tz = sz + dz[i]; + if (tz < 0 || tz >= H) + continue; + if (g[tz][sx][sy] != '-' || used[tz][sx][sy]) + continue; + used[tz][sx][sy] = cc; + X.push(sx); + Y.push(sy); + Z.push(tz); + } + } + if (used[ez][ex][ey]) + break; + } + printf("%d\n", used[ez][ex][ey] - 1); + } + return 0; +} diff --git a/13117.cpp b/13117.cpp new file mode 100644 index 0000000..3709a0b --- /dev/null +++ b/13117.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define eps 1e-6 +#define MAXN 131072 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +double dist2Seg(Pt a, Pt b, Pt o) +{ + return fabs(cross(a, b, o)) / dist(a, b); +} +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + Pt o, A[32]; + int x, y; + scanf("%d %d", &x, &y); + o = Pt(x, y); + for (int i = 0; i < n; i++) + { + scanf("%d %d", &x, &y); + A[i] = Pt(x, y); + } + double ret = dist(o, A[0]); + for (int i = 1; i < n; i++) + ret = min(ret, dist(o, A[i])); + for (int i = 0, j = n - 1; i < n; j = i++) + { + if (!between(A[i], A[j], o)) + continue; + double t = dist2Seg(A[i], A[j], o); + ret = min(ret, t); + } + printf("%.3lf\n", ret); + } + return 0; +} diff --git a/13118.cpp b/13118.cpp new file mode 100644 index 0000000..a5661f0 --- /dev/null +++ b/13118.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +struct State +{ + int lx, ly, gx, gy; + State(int lx = 0, int ly = 0, int gx = 0, int gy = 0) : lx(lx), ly(ly), gx(gx), gy(gy) + { + } +}; +int R, C; +char g[42][50]; +int lx, ly, gx, gy, mx, my; +char visited[42][42][42][42]; +int bfs() +{ + memset(visited, 0, sizeof(visited)); + queue Q; + queue DQ; + visited[lx][ly][gx][gy] = 1; + Q.push(State(lx, ly, gx, gy)), DQ.push(0); + const int dx[] = {0, 0, 1, -1}; + const int dy[] = {1, -1, 0, 0}; + const int ox[] = {0, 0, 1, -1}; + const int oy[] = {-1, 1, 0, 0}; + if (lx == gx && ly == gy && lx == mx && ly == my) + return 0; + while (!Q.empty()) + { + State u = Q.front(); + int d = DQ.front(); + Q.pop(), DQ.pop(); + for (int i = 0; i < 4; i++) + { + State v = u; + int tx, ty; + tx = v.lx + dx[i], ty = v.ly + dy[i]; + if (tx > 0 && ty > 0 && tx <= R && ty <= C && g[tx][ty] == '.') + v.lx = tx, v.ly = ty; + tx = v.gx + ox[i], ty = v.gy + oy[i]; + if (tx > 0 && ty > 0 && tx <= R && ty <= C && g[tx][ty] == '.') + v.gx = tx, v.gy = ty; + if (v.lx == v.gx && v.ly == v.gy && v.lx == mx && v.ly == my) + return d + 1; + if (!visited[v.lx][v.ly][v.gx][v.gy]) + { + visited[v.lx][v.ly][v.gx][v.gy] = 1; + visited[v.gx][v.gy][v.lx][v.ly] = 1; + Q.push(v), DQ.push(d + 1); + } + } + } + return -1; +} + +int main() +{ + while (scanf("%d %d", &R, &C) == 2) + { + scanf("%d %d %d %d %d %d", &mx, &my, &gx, &gy, &lx, &ly); + for (int i = 1; i <= R; i++) + scanf("%s", g[i] + 1); + + int ret = bfs(); + if (ret == -1) + puts("NO LOVE"); + else + printf("%d\n", ret); + } + return 0; +} diff --git a/13119.cpp b/13119.cpp new file mode 100644 index 0000000..af1f1dc --- /dev/null +++ b/13119.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +int areaTri(double a, double b, double c) +{ + double s = (a + b + c) / 2.f; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} + +int validTri(int a, int b, int c) +{ + return a < b + c && b < a + c && c < b + a; +} +int n, A[512], C[512][2], ret, cn; +int D[3]; +int pickRect() +{ + int p = -1; + for (int i = cn - 1; i >= 0; i--) + { + if (C[i][1] >= 4 && p == -1) + return C[i][0] * C[i][0]; + if (C[i][1] >= 2) + { + if (p != -1) + return p * C[i][0]; + p = C[i][0]; + } + } + return 0; +} +void dfs(int idx, int i) +{ + if (idx == 3) + { + if (!validTri(D[0], D[1], D[2])) + return; + int tmp = areaTri(D[0], D[1], D[2]) + pickRect(); + ret = max(ret, tmp); + return; + } + if (i == cn) + return; + for (int j = 1; j <= C[i][1] && idx + j <= 3; j++) + { + if (j == 1) + D[idx] = C[i][0], C[i][1]--; + else if (j == 2) + D[idx] = D[idx + 1] = C[i][0], C[i][1] -= 2; + else + D[idx] = D[idx + 1] = D[idx + 2] = C[i][0], C[i][1] -= 3; + dfs(idx + j, i + 1); + if (j == 1) + C[i][1]++; + else if (j == 2) + C[i][1] += 2; + else + C[i][1] += 3; + } + dfs(idx, i + 1); +} +int main() +{ + int cases = 0; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + + int B[512] = {}, M[512]; + for (int i = 0; i < n; i++) + B[A[i]]++; + cn = 0; + for (int i = 0; i <= 256; i++) + { + if (B[i]) + { + M[i] = cn; + C[cn][0] = i, C[cn][1] = B[i], cn++; + } + } + ret = pickRect(); + dfs(0, 0); + printf("%d\n", ret); + } + return 0; +} diff --git a/1312.cpp b/1312.cpp new file mode 100644 index 0000000..dfac657 --- /dev/null +++ b/1312.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define X first +#define Y second + +typedef pair Point; + +void max_area(const vector &v, int xMin, int yMin, int xMax, int yMax, int &P, int &Q, int &L) +{ + if (v.empty()) + { + int side = xMax - xMin; + if (yMax - yMin < side) + { + side = yMax - yMin; + } + if (L >= side) + { + return; + } + L = side; + P = xMin; + Q = yMin; + return; + } + Point pivot = v[0]; + vector v_top, v_btm, v_left, v_right; + for (unsigned int i = 1; i < v.size(); ++i) + { + Point p = v[i]; + if (p.X > pivot.X) + { + v_right.push_back(p); + } + else + { + v_left.push_back(p); + } + if (p.Y > pivot.Y) + { + v_top.push_back(p); + } + else + { + v_btm.push_back(p); + } + } + max_area(v_top, xMin, pivot.Y, xMax, yMax, P, Q, L); + max_area(v_btm, xMin, yMin, xMax, pivot.Y, P, Q, L); + max_area(v_left, xMin, yMin, pivot.X, yMax, P, Q, L); + max_area(v_right, pivot.X, yMin, xMax, yMax, P, Q, L); +} + +int main() +{ + int T, N, W, H, x, y, P, Q, L; + scanf("%d", &T); + for (int t = 0; t < T; t++) + { + if (t != 0) + { + printf("\n"); + } + scanf("%d%d%d", &N, &W, &H); + vector v; + for (int i = 0; i < N; ++i) + { + scanf("%d%d", &x, &y); + v.push_back(Point(x, y)); + } + random_shuffle(v.begin(), v.end()); + L = 0; + max_area(v, 0, 0, W, H, P, Q, L); + printf("%d %d %d\n", P, Q, L); + } + return 0; +} diff --git a/13121.cpp b/13121.cpp new file mode 100644 index 0000000..e269c93 --- /dev/null +++ b/13121.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + int a[512], b[512], cnt[360] = {}; + for (int i = 0; i < n; i++) + { + scanf("%*d %d %d", &a[i], &b[i]); + a[i] = (a[i] % 360 + 360) % 360; + cnt[a[i]]++; + } + int ret = -1; + for (int time = 0; time <= 360 && ret == -1; time++) + { + for (int i = 0; i < 360; i++) + { + if (cnt[i] > 1) + ret = time; + cnt[i] = 0; + } + for (int i = 0; i < n; i++) + { + a[i] = a[i] + b[i]; + a[i] = (a[i] % 360 + 360) % 360; + cnt[a[i]]++; + } + } + if (ret == -1) + printf("GIANIK IS NEVER ECLIPSED\n"); + else + printf("%d\n", ret); + } + return 0; +} diff --git a/13122.cpp b/13122.cpp new file mode 100644 index 0000000..150c49d --- /dev/null +++ b/13122.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +const double INF = 1e+30; + +double dp[260][260] = {}; + +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + int x[260], y[260]; + for (int i = 0; i < n; i++) + scanf("%d %d", &x[i], &y[i]); + + for (int i = 0; i < n; i++) + { + dp[i][1] = i == 0 ? 0 : INF; + for (int j = 2; j <= n - m; j++) + dp[i][j] = INF; + for (int k = 0; k < i; k++) + { + double dist = hypot(x[i] - x[k], y[i] - y[k]); + for (int j = 2; j <= n - m; j++) + dp[i][j] = min(dp[i][j], dp[k][j - 1] + dist); + } + } + double ret = dp[n - 1][n - m]; + printf("%.3lf\n", ret); + } + return 0; +} diff --git a/13123.cpp b/13123.cpp new file mode 100644 index 0000000..6cdf982 --- /dev/null +++ b/13123.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +const int MX = 2000000; + +bool lights[MX]; +int nq, sq, n; + +int hex_to_dec(char x) +{ + return (x <= '9') ? (x - '0') : (x - 'A' + 10); +} + +char dec_to_hex(int x) +{ + return (x <= 9) ? ('0' + x) : ('A' + (x - 10)); +} + +struct bucket +{ + bool st, ho, hz; + bucket() + { + st = false; + ho = false; + hz = false; + } + bool has_one() + { + if (st) + { + return hz; + } + return ho; + } +}; + +void flip(int l, int r) +{ + r = min(r, n); + for (int i = l; i <= r; i++) + { + lights[i] ^= 1; + } +} + +void find_one(vector &bck, int tb, int l, int r) +{ + r = min(r, n); + for (int i = l; i <= r; i++) + { + bck[tb].ho |= lights[i]; + bck[tb].hz |= !lights[i]; + } +} + +void proc_bucket(vector &bck, int tb, int lo, int hi) +{ + int l = tb * sq; + int r = l + sq - 1; + if (bck[tb].st) + { + flip(l, r); + } + bck[tb].st = false; + bck[tb].ho = false; + flip(lo, hi); + find_one(bck, tb, l, r); +} + +bool check_l(int l, int r, int &x, bool st) +{ + for (int i = l; i >= r; i--) + { + if (lights[i] == !st && i <= x) + { + x = i; + return true; + } + } + return false; +} + +void get_l(vector &bck, int &tb, int &l) +{ + int bck_t = tb; + while (bck_t >= 0) + { + int lo = bck_t * sq + sq - 1; + int hi = bck_t * sq; + if (bck[bck_t].has_one() && check_l(lo, hi, l, bck[bck_t].st)) + { + tb = bck_t; + return; + } + else + { + bck_t--; + } + } +} + +bool check_r(int l, int r, int &x, bool st) +{ + for (int i = l; i <= r; i++) + { + if (lights[i] == !st && i >= x && i < n) + { + x = i; + return true; + } + } + return false; +} + +void get_r(vector &bck, int &tb, int &r) +{ + int bck_t = tb; + while (bck_t < nq) + { + int lo = bck_t * sq; + int hi = lo + sq - 1; + if (bck[bck_t].has_one() && check_r(lo, hi, r, bck[bck_t].st)) + { + tb = bck_t; + return; + } + else + { + bck_t++; + } + } +} + +int main() +{ + int T, q, ss; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n,&q); + string s; + cin >> s; + sq = sqrt(n) + 1; + nq = n / sq + 1; + vector buckets(nq); + ss = s.size(); + int idx = n - 1; + for (int i = ss - 1; i >= 0; i--) + { + int dec = hex_to_dec(s[i]); + for (int j = 0; j < 4 && idx >= 0; j++) + { + lights[idx--] = ((dec & (1 << j)) != 0); + } + } + for (int i = idx; i >= 0; i--) + { + lights[i] = 0; + } + for (int i = 0; i < nq; i++) + { + find_one(buckets, i, i * sq, i * sq + sq - 1); + } + for (int i = 0; i < q; i++) + { + int l, r; + scanf("%d%d", &l,&r); + l--; + r--; + int bl = l / sq, br = r / sq; + get_l(buckets, bl, l); + get_r(buckets, br, r); + if (bl == br) + { + proc_bucket(buckets, bl, l, r); + } + else + { + for (int i = bl + 1; i < br; i++) + { + buckets[i].st ^= 1; + } + proc_bucket(buckets, bl, l, bl * sq + sq - 1); + proc_bucket(buckets, br, br * sq, r); + } + } + for (int i = 0; i < nq; i++) + { + if (buckets[i].st) + { + flip(i * sq, i * sq + sq - 1); + } + } + vector ans; + for (int i = n - 1; i >= 0; i--) + { + int c = 0, t = 0, b = 1; + while (c < 4 && i >= 0) + { + if (lights[i]) + { + t += b; + } + b <<= 1; + c++; + i--; + } + i++; + ans.push_back(dec_to_hex(t)); + } + bool fst = false; + for (int i = ans.size() - 1; i >= 0; i--) + { + if (ans[i] != '0' || fst) + { + printf("%c", ans[i]); + fst = true; + } + } + if (!fst) + { + printf("0"); + } + printf("\n"); + } + return 0; +} diff --git a/13124.cpp b/13124.cpp new file mode 100644 index 0000000..8956d5d --- /dev/null +++ b/13124.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +int R, C, W; +char g[105][105], w[105]; +int test(char w[]) +{ + int n = strlen(w); + const int dx[] = {0, 1, 1, -1}; + const int dy[] = {1, 0, 1, 1}; + + int stk[105][2]; + int used[128] = {}; + for (int i = 0; i < n; i++) + used[w[i]]++; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + for (int k = 0; k < 4; k++) + { + if (k == 0 && C - j < n) + continue; + if (k == 1 && R - i < n) + continue; + if (k == 2 && min(C - j, R - i) < n) + continue; + if (k == 3 && min(C - j, i + 1) < n) + continue; + int x = i, y = j, t = 0; + int err = 0; + while (x >= 0 && y >= 0 && x < R && y < C && t < n) + { + used[g[x][y]]--; + err |= used[g[x][y]] < 0; + stk[t][0] = x, stk[t][1] = y, t++; + if (err) + break; + x += dx[k], y += dy[k]; + } + for (int idx = 0; idx < t; idx++) + used[g[stk[idx][0]][stk[idx][1]]]++; + if (t == n && !err) + { + return 1; + } + } + } + } + return 0; +} + +int main() +{ + while (scanf("%d %d %d", &R, &C, &W) == 3) + { + for (int i = 0; i < R; i++) + scanf("%s", g[i]); + + int ret = 0; + for (int i = 0; i < W; i++) + { + scanf("%s", w); + ret += test(w); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13125.cpp b/13125.cpp new file mode 100644 index 0000000..21c88db --- /dev/null +++ b/13125.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXN = 7; +const LL INF = 1LL << 60; + +int n; +LL arr[MAXN], tgt; + +// prob can be reduced to replacing arithmetic ops: +, -, *, / btw a set of # and check if the res >= tgt. note that we can use >=1 # in any order for the solution use recursive and bitmask approach, on each iteration, we check if we haven't included the ith number (hose) in the solution, if not, include it using the possible operators and recursively call the method again +// - if a subtraction gives a negative number, prune the branch (not able to subtract more water than +// available) +// - if a division gives a non-integer number, prune the branch (not able to divide into non-integer +// numbers) + +LL solve(int mask, LL acc) +{ + // if current solution is greater or equal than tgt, use it as current minimum, or use inf otherwise + LL mini = acc >= tgt ? acc : INF; + // iterate through each number (hose) + for (int i = 0; i < n; ++i) + { + // if i-th # (hose) is alrdy incl in soln, skip + if (mask & (1 << i)) + { + continue; + } + // try add i-th hose to soln + mini = min(mini, solve(mask | (1 << i), acc + arr[i])); + // if subtraction won't give neg #, try subtracting i-th hose from soln + if (acc >= arr[i]) + { + mini = min(mini, solve(mask | (1 << i), acc - arr[i])); + } + // try multiplying i-th hose to the solution + mini = min(mini, solve(mask | (1 << i), acc * arr[i])); + // if division won't give non-integer #, try dividing i-th hose from soln + if (acc % arr[i] == 0) + { + mini = min(mini, solve(mask | (1 << i), acc / arr[i])); + } + } + return mini; +} + +int main() +{ + string line; + while (getline(cin, line)) + { + n = 0; + stringstream ss(line); + ss >> tgt; + if (tgt == 0LL) + { + break; + } + while (ss >> arr[n++]) + ; // store hose val in arr[] + n--;// actual # hose + // initially call recursive() with mask 0 (haven't incl any hose yet) and 0ll as accum (no op performed yet) + LL ans = solve(0, 0LL); + printf("%lld\n", ans == INF ? 0LL : ans); + } + return 0; +} diff --git a/13126.cpp b/13126.cpp new file mode 100644 index 0000000..093f74e --- /dev/null +++ b/13126.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +const int MAXCHAR = 26; +const int MAXNODE = 1048576; + +class ACmachine +{ +public: + struct Node + { + Node *next[MAXCHAR], *fail; + Node *has; + int cnt, id; + void init() + { + fail = has = NULL; + cnt = 0; + id = -1; + memset(next, 0, sizeof(next)); + } + } nodes[MAXNODE]; + Node *root; + int size; + Node *getNode() + { + Node *p = &nodes[size++]; + p->init(); + return p; + } + void init() + { + size = 0; + root = getNode(); + } + int toIndex(char c) + { + assert(c - 'a' >= 0 && c - 'a' < MAXCHAR); + return c - 'a'; + } + void insert(const char str[], int sid) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + p->cnt = 1, p->id = sid; + } + int find(const char str[]) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + return -1; + p = p->next[idx]; + } + return p->id; + } + void build() + {// AC automation + queue Q; + Node *u, *p; + Q.push(root), root->fail = NULL; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < MAXCHAR; i++) + { + if (u->next[i] == NULL) + continue; + Q.push(u->next[i]); + p = u->fail; + while (p != NULL && p->next[i] == NULL) + p = p->fail; + if (p == NULL || p->next[i] == NULL) + { + u->next[i]->fail = root; + u->next[i]->has = root; + } + else + { + u->next[i]->fail = p->next[i]; + u->next[i]->has = p->next[i]; + if (p->next[i]->id == -1) + u->next[i]->has = p->next[i]->has; + } + } + } + } + int query(const char str[], vector> &fpos) + { + Node *u = root, *p; + int matched = 0; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + while (u->next[idx] == NULL && u != root) + u = u->fail; + u = u->next[idx]; + u = (u == NULL) ? root : u; + p = u; + while (p != root && p->cnt != -1) + { + if (p->id != -1) + fpos[p->id].push_back(i); + p = p->has; + } + } + return matched; + } + void free() + { + return; + } +} AC; + +int countMatch(char S[], char T[]) +{ + int n = strlen(S), m = strlen(T); + if (m > n) + return 0; + AC.init(); + static int gap[100005], wid[100005], wlen[100005]; + int gsize = 0, cnt = 0; + int wsize = 0, lsize = 0; + vector> fpos; + for (int i = 0; i < m; i++) + { + if (T[i] == '?') + { + cnt++; + } + else + { + gap[gsize++] = cnt; + char *t_str = T + i; + while (T[i] != '?' && i < m) + i++; + T[i] = '\0'; + int id = AC.find(t_str); + wlen[wsize] = strlen(t_str); + if (id != -1) + { + wid[wsize++] = id; + } + else + { + wid[wsize++] = lsize; + fpos.push_back(vector()); + AC.insert(t_str, lsize), lsize++; + } + cnt = 0; + if (i != m) + cnt = 1; + else + gap[gsize++] = 0; + // printf("%s %d\n", t_str, wid[wsize-1]); + } + } + if (cnt) + gap[gsize++] = cnt; + if (wsize == 0) + return strlen(S) - strlen(T) + 1; + + AC.build(); + AC.query(S, fpos); + + vector fidx(wsize, 0); + int ret = 0; + while (true) + { + int end = 0; + for (int i = 0; i < wsize; i++) + { + int id = wid[i]; + if (fidx[i] == fpos[id].size()) + { + end = 1; + break; + } + } + if (end) + break; + + int ok = 1; + if (fpos[wid[0]][fidx[0]] - wlen[0] + 1 < gap[0]) + ok = 0; + for (int i = 1; i < wsize && ok; i++) + { + int id = wid[i], pos, last; + last = fpos[wid[i - 1]][fidx[i - 1]]; + while (fidx[i] < fpos[id].size()) + { + pos = fpos[id][fidx[i]] - wlen[i]; + if (pos - gap[i] < last) + { + fidx[i]++; + } + else if (pos - gap[i] == last) + { + break; + } + else + { + ok = 0; + break; + } + } + if (fidx[i] == fpos[id].size()) + ok = 0; + } + if (ok && fpos[wid[wsize - 1]][fidx[wsize - 1]] + gap[gsize - 1] >= n) + ok = 0; + ret += ok; + fidx[0]++; + } + return ret; +} + +int main() +{ + static char S[100005], T[100005]; + while (scanf("%s", S) == 1) + { + scanf("%s", T); + printf("%d\n", countMatch(S, T)); + } + return 0; +} diff --git a/13127.cpp b/13127.cpp new file mode 100644 index 0000000..89311ea --- /dev/null +++ b/13127.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +const int MAXV = 1024; +const int MAXE = 131072; +const long long INF = 1LL << 60; +struct Edge +{ + int to, eid; + long long w; + Edge *next; +}; +Edge edge[MAXE], *adj[MAXV]; +int e = 0; +double dist[MAXV]; +void addEdge(int x, int y, long long v) +{ + edge[e].to = y, edge[e].w = v, edge[e].eid = e; + edge[e].next = adj[x], adj[x] = &edge[e++]; +} +void dijkstra(int st[], int m, long long dist[], int n) +{ + typedef pair PLL; + for (int i = 0; i <= n; i++) + dist[i] = INF; + set pQ; + PLL u; + for (int i = 0; i < m; i++) + pQ.insert(PLL(0, st[i])), dist[st[i]] = 0; + while (!pQ.empty()) + { + u = *pQ.begin(), pQ.erase(pQ.begin()); + for (Edge *p = adj[u.second]; p; p = p->next) + { + if (dist[p->to] > dist[u.second] + p->w) + { + if (dist[p->to] != INF) + pQ.erase(pQ.find(PLL(dist[p->to], p->to))); + dist[p->to] = dist[u.second] + p->w; + pQ.insert(PLL(dist[p->to], p->to)); + } + } + } +} + +int main() +{ + int N, M, B, P; + while (scanf("%d %d %d %d", &N, &M, &B, &P) == 4) + { + e = 0; + for (int i = 0; i <= N; i++) + adj[i] = NULL; + for (int i = 0; i < M; i++) + { + int u, v, t; + scanf("%d %d %d", &u, &v, &t); + addEdge(u, v, t); + addEdge(v, u, t); + } + int b[1024] = {}, p[1024]; + long long dist[1024]; + for (int i = 0; i < B; i++) + { + int x; + scanf("%d", &x); + b[x] = 1; + } + for (int i = 0; i < P; i++) + scanf("%d", &p[i]); + dijkstra(p, P, dist, N); + + long long mxv = 0; + int mxc = 0; + for (int i = 0; i < N; i++) + { + if (b[i]) + mxv = max(mxv, dist[i]); + } + for (int i = 0; i < N; i++) + { + if (b[i]) + mxc += mxv == dist[i]; + } + if (mxv == INF) + printf("%d *\n", mxc); + else + printf("%d %lld\n", mxc, mxv); + for (int i = 0; i < N; i++) + { + if (mxv == dist[i] && b[i]) + { + mxc--; + printf("%d%c", i, mxc ? ' ' : '\n'); + } + } + } + return 0; +} diff --git a/13128.cpp b/13128.cpp new file mode 100644 index 0000000..3e6df87 --- /dev/null +++ b/13128.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +typedef long long LL; + +const LL MOD = 1000000009LL; + +int kk = 1; +char s[1005]; +int n, fr[26], ncr[1005][1005], qs[26]; +LL p[2][26][1005][1005]; +int vs[2][26][1005][1005]; +LL q[1005][1005]; + +LL dp2(int i, int r) +{ + if (r == 0) + { + return 1; + } + if (i == -1) + { + return 0; + } + LL &pr = q[i][r]; + if (pr != -1) + { + return pr; + } + pr = dp2(i - 1, r) + dp2(i, r - 1); + if (pr >= MOD) + { + pr = MOD; + } + return pr; +} + +LL dp(bool g, int i, int ln, int r) +{ + if (i == 26) + { + if (g && r == 1) + { + return 1; + } + return 0; + } + LL &pr = p[g][i][ln][r]; + if (vs[g][i][ln][r] == kk) + { + return pr; + } + vs[g][i][ln][r] = kk; + pr = 0; + for (int j = 0; j <= fr[i]; j++) + { + LL qq = dp2(ln, j); + if (qq > r) + { + return pr; + } + if (r % qq) + { + continue; + } + qs[i] = j; + pr += dp(g || (j > 0), i + 1, ln + j, r / qq) * ncr[fr[i]][j]; + if (pr >= MOD) + { + pr %= MOD; + } + } + return pr; +} + +int main() +{ + int t, i, j, k; + ncr[0][0] = 1; + for (int i = 1; i < 1005; i++) + { + ncr[i][0] = ncr[i][i] = 1; + for (int j = 1; j < i; j++) + { + ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j]; + if (ncr[i][j] >= MOD) + { + ncr[i][j] -= MOD; + } + } + } + memset(q, -1, sizeof(q)); + while (scanf("%s", s) != EOF) + { + kk++; + scanf("%d", &n); + memset(fr, 0, sizeof(fr)); + int ln = strlen(s); + for (int i = 0; i < ln; i++) + { + fr[s[i] - 'a']++; + } + printf("%d\n", dp(0, 0, 0, n)); + } + return 0; +} diff --git a/13129.cpp b/13129.cpp new file mode 100644 index 0000000..7863b2d --- /dev/null +++ b/13129.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +const int MAXN = 1048576 << 1; +const long long MOD = 1e+9 + 7; +class SegmentTree +{ +public: + struct Node + { + long long mul; + void init() {} + } nodes[MAXN]; + void pushDown(int k, int l, int r) + { + } + void pushUp(int k) + { + nodes[k].mul = (nodes[k << 1].mul * nodes[k << 1 | 1].mul) % MOD; + } + void build(int k, int l, int r, long long A[]) + { + nodes[k].init(); + if (l == r) + { + nodes[k].mul = A[l]; + return; + } + int mid = (l + r) / 2; + build(k << 1, l, mid, A); + build(k << 1 | 1, mid + 1, r, A); + pushUp(k); + } + // query + long long r_mul; + void qinit() + { + r_mul = 1; + } + void query(int k, int l, int r, int x, int y) + { + if (x <= l && r <= y) + { + r_mul *= nodes[k].mul; + r_mul %= MOD; + return; + } + pushDown(k, l, r); + int mid = (l + r) / 2; + if (x <= mid) + query(k << 1, l, mid, x, y); + if (y > mid) + query(k << 1 | 1, mid + 1, r, x, y); + } +} tree; + +long long mpow(long long x, long long y, long long mod) +{ + long long ret = 1; + while (y) + { + if (y & 1) + ret = (ret * x) % mod; + y >>= 1, x = (x * x) % mod; + } + return ret; +} +static long long A[MAXN]; + +int main() +{ + int N, P; + int x, l, r, Q; + while (scanf("%d %d", &N, &P) == 2) + { + for (int i = 1; i <= N; i++) + { + scanf("%d", &x); + A[i] = (mpow(P, x, MOD) + 1) % MOD; + } + tree.build(1, 1, N, A); + + scanf("%d", &Q); + for (int i = 0; i < Q; i++) + { + scanf("%d %d", &l, &r); + tree.qinit(); + tree.query(1, 1, N, l, r); + long long ret = (tree.r_mul + MOD - 1) % MOD; + printf("%lld\n", ret); + } + } + return 0; +} diff --git a/13130.cpp b/13130.cpp new file mode 100644 index 0000000..4915d25 --- /dev/null +++ b/13130.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + int ret = 1, x, p; + for (int i = 0; i < 5; i++) + { + scanf("%d", &x); + if (i) + ret &= x == p + 1; + p = x; + } + puts(ret ? "Y" : "N"); + } + return 0; +} diff --git a/13131.cpp b/13131.cpp new file mode 100644 index 0000000..4c8d895 --- /dev/null +++ b/13131.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +#define MAXL (50000 >> 5) + 1 +#define GET(x) (mark[x >> 5] >> (x & 31) & 1) +#define SET(x) (mark[x >> 5] |= 1 << (x & 31)) +int mark[MAXL], P[50000], Pt = 0; +void sieve() +{ + register int i, j, k; + SET(1); + int n = 46340; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (k = n / i, j = i * k; k >= i; k--, j -= i) + SET(j); + P[Pt++] = i; + } + } +} + +void factorize(int n, vector &f) +{ + for (int i = 0; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + while (n % P[i] == 0) + f.push_back(P[i]), n /= P[i]; + } + } + if (n != 1) + f.push_back(n); +} +void factor_gen(int idx, long long x, vector> &f, vector &ret) +{ + if (idx == f.size()) + { + ret.push_back(x); + return; + } + for (long long i = 0, a = 1; i <= f[idx].second; i++, a *= f[idx].first) + factor_gen(idx + 1, x * a, f, ret); +} +int main() +{ + sieve(); + + int testcase, N, K; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &K); + if (K == 1) + { + printf("0\n"); + continue; + } + vector Nf; + map NR; + factorize(N, Nf); + for (auto e : Nf) + NR[e]++; + + vector> F; + vector ret; + for (auto e : NR) + F.push_back(make_pair(e.first, e.second)); + factor_gen(0, 1, F, ret); + long long sum = 0; + for (auto e : ret) + { + if (e % K) + sum += e; + } + printf("%lld\n", sum); + } + return 0; +} diff --git a/13132.cpp b/13132.cpp new file mode 100644 index 0000000..ccb8cc5 --- /dev/null +++ b/13132.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) + +const int MAXN = 100005; +const int MAXL = (MAXN >> 5) + 1; +int mark[MAXL]; +int P[10000], Pt = 0; +int phi[MAXN]; + +void sieve_phi() +{ + register int i, j, k; + SET(1); + int n = 100000; + for (i = 1; i <= n; i++) + phi[i] = i; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (j = i + i; j <= n; j += i) + SET(j) + , phi[j] = phi[j] / i * (i - 1); + phi[i] = i - 1; + P[Pt++] = i; + } + } +} + +int main() +{ + sieve_phi(); + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + printf("%d\n", phi[n]); + } + return 0; +} diff --git a/13133.cpp b/13133.cpp new file mode 100644 index 0000000..6a77c0b --- /dev/null +++ b/13133.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +string int2str(unsigned long long x) +{ + stringstream sin; + sin << x; + return sin.str(); +} + +int main() +{ + unsigned long long dp[100] = {0, 1, 1, 2, 3}; + const int n = 92; + string strf[100]; + int g[100][100] = {}; + for (int i = 2; i <= 92; i++) + dp[i] = dp[i - 1] + dp[i - 2]; + + for (int i = 1; i <= 92; i++) + strf[i] = int2str(dp[i]); + + set S; + for (int i = 1; i <= 92; i++) + { + for (int j = 1; j <= 92; j++) + { + if (i == j) + continue; + string tmp = strf[i] + strf[j]; + if (S.count(tmp)) + g[i][j] = 1; + else + S.insert(tmp); + } + } + static char s[1048576]; + while (scanf("%s", s) == 1) + { + int m = 0; + for (int i = 0; s[i]; i++) + { + if (isdigit(s[i])) + s[m++] = s[i]; + } + s[m] = '\0'; + + int ret = 0; + + int first[100], last[100]; + memset(first, -1, sizeof(first)); + memset(last, -1, sizeof(last)); + for (int i = 1; i <= 92; i++) + { + int idx = 0, len = strf[i].length(); + const char *sptr = strf[i].c_str(); + for (int j = 0; j < m && idx < len; j++) + { + if (sptr[idx] == s[j]) + { + idx++; + if (idx == len) + { + first[i] = j; + break; + } + } + } + + idx = len - 1; + for (int j = m - 1; j >= 0; j--) + { + if (sptr[idx] == s[j]) + { + idx--; + if (idx == -1) + { + last[i] = j; + break; + } + } + } + } + + for (int i = 1; i <= 92; i++) + { + if (first[i] == -1) + continue; + for (int j = 1; j <= 92; j++) + { + if (i == j || last[j] == -1) + continue; + if (first[i] <= last[j] && g[i][j] == 0) + { + ret += first[i] < last[j] && g[i][j] == 0; + } + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/13134.cpp b/13134.cpp new file mode 100644 index 0000000..3e5d0f5 --- /dev/null +++ b/13134.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +int main() +{ + int a, c, m, x, q, n; + while (scanf("%d %d %d %d", &a, &c, &m, &x) == 4) + { + scanf("%d %d", &q, &n); + x %= m; + int cnt[1024] = {}, path[1024] = {}, f[1024]; + for (int i = 1; i <= n; i++) + { + f[i] = x; + if (path[x]) + { + for (int j = path[x] - 1; j >= 1; j--) + cnt[f[j]]++; + int clen = i - path[x]; + assert(clen != 0); + for (int j = path[x]; j < i; j++) + cnt[f[j]] += (n - path[x]) / clen + (j - path[x] <= (n - path[x]) % clen); + break; + } + path[x] = i; + if (i == n) + { + for (int i = 1; i <= n; i++) + cnt[f[i]]++; + break; + } + x = (a * x + c) % m; + } + int sum = 0; + vector> T; + for (int i = 0; i < m; i++) + { + if (cnt[i]) + { + sum += cnt[i]; + T.push_back(make_pair(sum, i)); + } + } + for (int i = 0; i < q; i++) + { + int t; + scanf("%d", &t); + int pos = int(lower_bound(T.begin(), T.end(), make_pair(t, -1)) - T.begin()); + printf("%d\n", T[pos].second); + } + } + return 0; +} diff --git a/13135.cpp b/13135.cpp new file mode 100644 index 0000000..1c728f4 --- /dev/null +++ b/13135.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + map R; + for (int i = 0;; i++) + { + int ways = (i + 2) * (i + 1) / 2; + if (ways > 50000000) + break; + R[ways] = i; + } + + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + if (R.count(n)) + printf("%d\n", R[n]); + else + printf("No solution\n"); + } + return 0; +} diff --git a/13136.cpp b/13136.cpp new file mode 100644 index 0000000..91ad5cd --- /dev/null +++ b/13136.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +const int MOD = 10; + +struct Matrix +{ + vector > A, C; + Matrix() + { + } + Matrix(int n, int m) + { + A = vector >(n, vector(m, 0)); + C = vector >(n, vector(m, 0)); + } + + void ident() + { + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < A.size(); j++) + { + A[i][j] = 0; + } + } + for (int i = 0; i < A.size(); i++) + { + A[i][i] = 1; + } + } + + void operator*(const Matrix &B) + { + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < B.A[0].size(); j++) + { + C[i][j] = 0; + for (int k = 0; k < A[0].size(); k++) + { + C[i][j] = (C[i][j] + (A[i][k] * B.A[k][j])); + } + } + } + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < A.size(); j++) + { + A[i][j] = C[i][j] % MOD; + } + } + } + + void print() + { + for (int i = 0; i < A.size(); i++) + { + for (int j = 0; j < A[i].size(); j++) + { + printf("%d ", A[i][j]); + } + printf("\n"); + } + } +}; + +// B = I and will store the the matrix B^e +void mat_pow(long long e, Matrix &A, Matrix &B) +{ + while (e) + { + if (e & 1) + { + B *A; + } + A *A; + e >>= 1; + } +} + +int main() +{ + int T, a, b, c; + scanf("%d", &T); + long long e; + Matrix A(4, 4), B(4, 4), C(4, 1); + while (T--) + { + scanf("%lld%d%d%d", &e,&a,&b,&c); + if (e == 0) + { + printf("%d\n", a+b+c); + continue; + } + A.A[0][0] = 1; + A.A[0][1] = 8; + A.A[0][2] = -6; + A.A[0][3] = -7; + A.A[1][0] = 0; + A.A[1][1] = 4; + A.A[1][2] = -3; + A.A[1][3] = -3; + A.A[2][0] = 0; + A.A[2][1] = 5; + A.A[2][2] = -4; + A.A[2][3] = -4; + A.A[3][0] = 0; + A.A[3][1] = -1; + A.A[3][2] = 1; + A.A[3][3] = 0; + C.A[0][0] = 0; + C.A[1][0] = a; + C.A[2][0] = b; + C.A[3][0] = c; + B.ident(); + // e % 15 because this == len(cycle) + mat_pow(e % 15, A, B); + B *C; + cout << (MOD + B.A[0][0]) % 10 << endl; + } + return 0; +} diff --git a/13137.cpp b/13137.cpp new file mode 100644 index 0000000..10811ee --- /dev/null +++ b/13137.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +const int MAXN = 1024; +int A[MAXN][MAXN]; +int th[MAXN][MAXN]; +int solve(int id, int n, int h[]) +{ + int ret = 0; + int i, height; + stack> stk;// + pair e; + stk.push(make_pair(-1, 1)); + h[n + 1] = 0;// visual height. + for (i = 1; i <= n + 1; i++) + { + height = h[i]; + // printf("- %d\n", h[i]); + e = make_pair(height, i); + if (i <= n && i > 1 && A[i][id] != A[i - 1][id] + 1) + { + while (stk.size() > 1) + { + e = stk.top(), stk.pop(); + // printf("? %d %d\n", i-e.second, e.first); + ret = max(ret, (i - e.second) * e.first); + } + e.second = i; + } + while (height < stk.top().first) + { + e = stk.top(), stk.pop(); + ret = max(ret, (i - e.second) * e.first); + } + if (height > stk.top().first) + stk.push(make_pair(height, e.second)); + } + return ret; +} +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + scanf("%d", &A[i][j]); + th[i][j] = A[i][j] == A[i][j - 1] + 1; + } + } + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= m; j++) + { + if (th[i][j] && j != 1) + th[i][j] = th[i][j - 1] + 1; + else + th[i][j] = 1; + } + } + + int ret = 0; + for (int j = 1; j <= m; j++) + { + int h[MAXN]; + for (int i = 1; i <= n; i++) + h[i] = th[i][j]; + int t = solve(j, n, h); + ret = max(ret, t); + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1314.cpp b/1314.cpp new file mode 100644 index 0000000..4954fbc --- /dev/null +++ b/1314.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +typedef unsigned long long hash_t; +const int maxn = 2e5 + 11; +const int seed = 131; +int T, N; +char s[maxn]; +hash_t val[maxn], base[maxn]; +void pre() +{ + base[0] = 1; + for (int i = 1; i < maxn; ++i) + { + base[i] = base[i - 1] * seed; + } +} +void build(char s[], int N) +{ + val[N] = 0; + for (int i = N - 1; i >= 0; --i) + { + val[i] = val[i + 1] + s[i] * base[N - i - 1]; + } +} +inline hash_t getInteval(int beg, int len) +{ + return (val[beg] - val[beg + len]) * base[beg + len]; +} +inline bool cmp(int beg1, int len1, int beg2, int len2) +{ + int l = 1, r = min(len1, len2) + 1; + while (l < r) + { + int m = (l + r) >> 1; + if (getInteval(beg1, m) == getInteval(beg2, m)) + { + l = m + 1; + } + else + { + r = m; + } + } + return s[beg1 + l - 1] < s[beg2 + l - 1]; +} +int main() +{ + pre(); + scanf("%d", &T); + while (T--) + { + scanf("%d%s", &N, s); + for (int i = N; i < 2 * N; ++i) + { + s[i] = s[i - N]; + } + s[2 * N] = '\0'; + build(s, 2 * N); + int ans = 0; + for (int i = 1; i < N; ++i) + { + if (cmp(i, N, ans, N)) + { + ans = i; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/13140.cpp b/13140.cpp new file mode 100644 index 0000000..ebf3d58 --- /dev/null +++ b/13140.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int n = 10000000; +int m2[1024] = {}; +void run() +{ + for (int i = 0; i < 30; i++) + m2[i * i] = 1; + int cnt = 0; + for (long long i = 16; i < n; i++) + { + long long t = i * i; + int sum = 0; + while (t) + sum += t % 10, t /= 10; + if (m2[sum] == 1) + { + cnt++; + if (cnt == 7) + { + printf("%d\n", i); + exit(0); + } + } + else + { + cnt = 0; + } + } +} + +int main() +{ + for (int i = 10005 - 6; i <= 10005; i++) + { + printf("%d %lld\n", i, 1LL * i * i); + } + return 0; +} diff --git a/13141.cpp b/13141.cpp new file mode 100644 index 0000000..6bd48bb --- /dev/null +++ b/13141.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + unsigned long long dp[100] = {0, 1, 1, 2, 3}; + for (int i = 2; i < 100; i++) + dp[i] = dp[i - 1] + dp[i - 2]; + int n; + while (scanf("%d", &n) == 1 && n) + printf("%llu\n", dp[n]); + return 0; +} diff --git a/13142.cpp b/13142.cpp new file mode 100644 index 0000000..e375bb6 --- /dev/null +++ b/13142.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +typedef long long LL; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + LL t, u, s; + scanf("%lld%lld%lld", &t, &u, &s); + LL t_t = 24 * 60 * 60; + LL s_s = 1000000; + t *= t_t; + s *= s_s; + LL v = abs((s / t) - u); + printf(v - u < 0 ? "Remove " : "Add "); + LL mass = trunc(abs(abs(v) - u)); + printf("%ld tons\n", mass); + } + return 0; +} diff --git a/13144.cpp b/13144.cpp new file mode 100644 index 0000000..d6ef8f6 --- /dev/null +++ b/13144.cpp @@ -0,0 +1,239 @@ +#include + +using namespace std; + +#define BASE 1000000000 +#define X first +#define Y second +#define LI long long +#define MP make_pair +#define PB push_back +#define SZ size() +#define SQ(a) ((a) * (a)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define FOR(i, x, y) for (int i = (int)x; i < (int)y; i++) +#define RFOR(i, x, y) for (int i = (int)x; i > (int)y; i--) +#define SORT(a) sort(a.begin(), a.end()) +#define RSORT(a) sort(a.rbegin(), a.rend()) +#define IN(a, pos, c) insert(a.begin() + pos, 1, c) +#define DEL(a, pos, cant) erase(a.begin() + pos, cant) + +struct big +{ + vector V; + big() : V(1, 0) {} + big(int n) : V(1, n) {}// supone n < 1000000000 !!! + big(const big &b) : V(b.V) {} + + bool operator==(const big &b) const { return V == b.V; } + int &operator[](int i) { return V[i]; } + int operator[](int i) const { return V[i]; } + int size() const { return V.SZ; } + void resize(int i) { V.resize(i); } + + bool operator<(const big &b) const + { + for (int i = b.SZ - 1; SZ == b.SZ && i >= 0; i--) + if (V[i] == b[i]) + continue; + else + return (V[i] < b[i]); + return (SZ < b.SZ); + } + + void add_digit(int l) + { + if (l > 0) + V.PB(l); + } +}; + +inline big suma(const big &a, const big &b, int k) +{ + LI l = 0; + int size = MAX(a.SZ, b.SZ + k); + big c; + c.resize(size); + for (int i = 0; i < size; ++i) + { + l += i < a.SZ ? a[i] : 0; + l += (k <= i && i < k + b.SZ) ? b[i - k] : 0; + c[i] = l % BASE; + l /= BASE; + } + c.add_digit(int(l)); + return c; +} + +inline big operator+(const big &a, const big &b) +{ + return suma(a, b, 0); +} +inline big operator+(const big &a, int b) { return a + big(b); } +inline big operator+(int b, const big &a) { return a + big(b); } + +inline big operator-(const big &a, const big &b) +{ + assert(b < a || a == b); + LI l = 0, m = 0; + big c; + c.resize(a.SZ); + for (int i = 0; i < a.SZ; ++i) + { + l += a[i]; + l -= i < b.SZ ? b[i] + m : m; + if (l < 0) + { + l += BASE; + m = 1; + } + else + m = 0; + c[i] = l % BASE; + l /= BASE; + } + if (c[c.SZ - 1] == 0 && c.SZ > 1) + c.resize(c.SZ - 1); + return c; +} +inline big operator-(const big &a, int b) { return a - big(b); } + +inline big operator*(const big &a, int b) +{ + if (b == 0) + return big(0); + big c; + c.resize(a.SZ); + LI l = 0; + for (int i = 0; i < a.SZ; ++i) + { + l += (LI)b * a[i]; + c[i] = l % BASE; + l /= BASE; + } + c.add_digit(int(l)); + return c; +} +inline big operator*(int b, const big &a) { return a * b; } +inline big operator*(const big &a, const big &b) +{ + big res; + for (int i = 0; i < b.SZ; ++i) + res = suma(res, a * b[i], i); + return res; +} + +inline void divmod(const big &a, int b, big &div, int &mod) +{ + div.resize(a.SZ); + LI l = 0; + for (int i = a.SZ - 1; i >= 0; --i) + { + l *= BASE; + l += a[i]; + div[i] = l / b; + l %= b; + } + if (div[div.SZ - 1] == 0 && div.SZ > 1) + div.resize(div.SZ - 1); + mod = int(l); +} + +inline big operator/(const big &a, int b) +{ + big div; + int mod; + divmod(a, b, div, mod); + return div; +} + +inline int operator%(const big &a, int b) +{ + big div; + int mod; + divmod(a, b, div, mod); + return mod; +} + +inline istream &operator>>(istream &is, big &b) +{ + string s; + if (is >> s) + { + b.resize((s.SZ - 1) / 9 + 1); + for (int n = s.SZ, k = 0; n > 0; n -= 9, k++) + { + b[k] = 0; + for (int i = MAX(n - 9, 0); i < n; i++) + b[k] = 10 * b[k] + s[i] - '0'; + } + } + return is; +} + +inline ostream &operator<<(ostream &os, const big &b) +{ + os << b[b.SZ - 1]; + for (int k = b.SZ - 2; k >= 0; k--) + os << setw(9) << setfill('0') << b[k]; + return os; +} + +char table[19 * 19]; + +int main() +{ + int cases = 0; + cin >> cases; + + big n = 0; + + while (cases--) + { + cin >> n; + + n = n - 1; + + for (int i = 0; i < 19 * 19; ++i) + { + table[i] = '.'; + } + + int j = 0; + + while (!(n < 1)) + { + big m; + int res = n % 3; + divmod(n, 3, m, res); + n = m; + + if (res == 1) + { + table[j] = 'W'; + } + else if (res == 2) + { + table[j] = 'B'; + } + + ++j; + } + + for (int i = 0; i < 19 * 19; ++i) + { + if (i > 0 && i % 19 == 0) + { + cout << endl; + } + + cout << table[i]; + } + + cout << endl + << endl; + } + + return 0; +} diff --git a/13145.cpp b/13145.cpp new file mode 100644 index 0000000..7cbcd49 --- /dev/null +++ b/13145.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int d; + char s[65536]; + while (scanf("%d", &d) == 1 && d) + { + while (getchar() != '\n') + ; + fgets(s, 65536, stdin); + d = (d % 26 + 26) % 26; + for (int i = 0; s[i]; i++) + { + if (s[i] >= 'A' && s[i] <= 'Z') + { + s[i] = ((int)s[i] - 'A' + d) % 26 + 'A'; + } + else if (s[i] >= 'a' && s[i] <= 'z') + { + s[i] = ((int)s[i] - 'a' + d) % 26 + 'a'; + } + } + fputs(s, stdout); + } + return 0; +} diff --git a/13146.cpp b/13146.cpp new file mode 100644 index 0000000..5432d63 --- /dev/null +++ b/13146.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int cache[101][101]; + +int getDistance(const string &s1, const string &s2, int i, int j) +{ + if (cache[i][j] != -1) + { + return cache[i][j]; + } + else if (i == 0) + { + cache[i][j] = j; + } + else if (j == 0) + { + cache[i][j] = i; + } + else if (s1[i - 1] == s2[j - 1]) + { + cache[i][j] = getDistance(s1, s2, i - 1, j - 1); + } + else + { + cache[i][j] = min(min(1 + getDistance(s1, s2, i - 1, j - 1), + 1 + getDistance(s1, s2, i - 1, j)), + 1 + getDistance(s1, s2, i, j - 1)); + } + + return cache[i][j]; +} + +int main() +{ + int tests = 0; + cin >> tests; + + cin.ignore(); + + string s1, s2; + + while (tests--) + { + for (int i = 0; i < 101; ++i) + { + for (int j = 0; j < 101; ++j) + { + cache[i][j] = -1; + } + } + + getline(cin, s1); + getline(cin, s2); + + int distance = getDistance(s1, s2, s1.length(), s2.length()); + cout << distance << endl; + } + + return 0; +} diff --git a/13147.cpp b/13147.cpp new file mode 100644 index 0000000..700a2ec --- /dev/null +++ b/13147.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +typedef set> Cities; +typedef map Routes; + +struct Node +{ + int _remPlanes; + int _city; + int _accCost; + + Node(int city, int planes, int cost) : _city(city), _remPlanes(planes), _accCost(cost) + { + } +}; + +struct NodeCmp +{ + bool operator()(const Node &lhs, const Node &rhs) const + { + return lhs._accCost < rhs._accCost; + } +}; + +int findCost(int oCity, int dCity, Routes &trainRoutes, Routes &planeRoutes, int maxPlanes) +{ + Node startNode(oCity, maxPlanes, 0); + + multiset openedNodes; + openedNodes.insert(startNode); + + set> visitedStates; + + while (!openedNodes.empty()) + { + const Node n = *openedNodes.begin(); + openedNodes.erase(openedNodes.begin()); + + if (n._city == dCity) + { + return n._accCost; + } + + pair s(n._city, n._remPlanes); + + if (visitedStates.find(s) == visitedStates.end()) + { + visitedStates.insert(s); + + if (n._remPlanes > 0) + { + const Cities &cities = planeRoutes[n._city]; + + for (auto it = cities.begin(); it != cities.end(); ++it) + { + Node n2(it->first, n._remPlanes - 1, it->second + n._accCost); + openedNodes.insert(n2); + } + } + + const Cities &cities = trainRoutes[n._city]; + + for (auto it = cities.begin(); it != cities.end(); ++it) + { + Node n2(it->first, n._remPlanes, it->second + n._accCost); + openedNodes.insert(n2); + } + } + } + + return -1; +} + +int main() +{ + int cases = 0; + cin >> cases; + + int numCities = 0; + int oCity = 0; + int dCity = 0; + int maxPlanes = 0; + int numTrainRoutes = 0; + int numPlaneRoutes = 0; + + Routes trainRoutes; + Routes planeRoutes; + + while (cases--) + { + trainRoutes.clear(); + planeRoutes.clear(); + + cin >> numCities; + cin >> oCity >> dCity; + cin >> maxPlanes; + cin >> numTrainRoutes; + + for (int i = 0; i < numTrainRoutes; ++i) + { + int c1, c2, time; + cin >> c1 >> c2 >> time; + trainRoutes[c1].insert(make_pair(c2, time)); + trainRoutes[c2].insert(make_pair(c1, time)); + } + + cin >> numPlaneRoutes; + + for (int i = 0; i < numPlaneRoutes; ++i) + { + int c1, c2, time; + cin >> c1 >> c2 >> time; + planeRoutes[c1].insert(make_pair(c2, time)); + planeRoutes[c2].insert(make_pair(c1, time)); + } + + cout << findCost(oCity, dCity, trainRoutes, planeRoutes, maxPlanes) << endl; + } + + return 0; +} diff --git a/13148.cpp b/13148.cpp new file mode 100644 index 0000000..507c27d --- /dev/null +++ b/13148.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + long long int n; + while (scanf("%lld", &n) && n != 0) + { + if (n == 1 || n == 64 || n == 729 || n == 4096 || n == 15625 || n == 46656 || + n == 117649 || n == 262144 || n == 531441 || n == 1000000 || + n == 1771561 || n == 2985984 || n == 4826809 || n == 7529536 || + n == 11390625 || n == 16777216 || n == 24137569 || n == 34012224 || + n == 47045881 || n == 64000000 || n == 85766121) + { + printf("Special\n"); + } + else + { + printf("Ordinary\n"); + } + } + return 0; +} diff --git a/13149.cpp b/13149.cpp new file mode 100644 index 0000000..cc51ebe --- /dev/null +++ b/13149.cpp @@ -0,0 +1,172 @@ +#include + +using namespace std; + +// give you a binary string of length <10 ^ 5 +// define the grow operation to insert its xor value between two compartments +// queries the number of modals 0,1 in the interval after g <10 ^ 4 growth + +// ideas +// the first query interval [a, b] can be [0, b] - [0, a-1] to calculate so the problem can be streamlined into a one-way growth +// then in a growth process 0 increase the number of the same neighbor 1 on the contrary +// so i can only calculate the same after each growth and the number of different can know the number of 0,1 +// so list the possible relationships +// 00 -> 000 (with 0 will produce 2x with 0) +// 11 -> 101 (with 1 will produce 2x different) +// 10 -> 110 (different will produce 1x different 1x same 1) +// next, because his query range is not directly to the index but the growth path and start index + +// growth path for the d, r form +// 1 1 +//   | d +// 101 +// 1 1 +//  \ r +// 101 + +// so the rdd starting at 1 in 10010 would be +// 10010 +// 1 0 0 1 0 +//           \ r +// 1 1 0 0 0 1 1 1 0 +//             | d +// 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1 0 +//             | d +// 110110110000000001101101110110110 +//             ^ + +// d is the way in front of the maintenance can be +// however, the current need to end r and the next one can know what information +// so the need to maintain the end and the next value (end_val, nxt_val) +// in the d when nxt_val = end_val ^ nxt_val +// in the r end_val = end_val ^ nxt_val +// then remember to maintain the number of similarities and differences in the r when the relationship + +const long long prime = 342307123; + +bool dat[100010]; +char str[100010], gf[10010], gt[10010]; +int g, gpf, gpt; + +tuple get_cnt(const char *path, int idx) +{ + long long diff = 0, zsame = 0, osame = 0, one = 0, zero = 0; + bool end_val = dat[idx], nxt_val = dat[idx + 1]; + for (int i = 0; i <= idx; i++) + { + if (dat[i]) + { + one++; + } + else + { + zero++; + } + } + for (int i = 1; i <= idx; i++) + { + if (dat[i - 1] == dat[i] && !dat[i]) + { + zsame++; + } + else if (dat[i - 1] == dat[i] && dat[i]) + { + osame++; + } + else + { + diff++; + } + } + for (int i = 0; i < g - 1; i++) + { + long long ndiff = diff + 2 * osame, nzsame = 2 * zsame, nosame = diff; + zero += osame + zsame; + one += diff; + diff = ndiff % prime; + zsame = nzsame % prime; + osame = nosame % prime; + if (path[i] == 'R') + { + end_val = end_val ^ nxt_val; + if (end_val) + { + one++; + } + else + { + zero++; + } + if ((end_val ^ nxt_val) == end_val && end_val) + { + osame++; + } + else if ((end_val ^ nxt_val) == end_val && !end_val) + { + zsame++; + } + else + { + diff++; + } + } + else + { + nxt_val = end_val ^ nxt_val; + } + one %= prime; + zero %= prime; + } + zero %= prime; + one %= prime; + return make_tuple(zero, one, end_val); +} + +int main() +{ + int T; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + scanf("%s%d", str, &g); + scanf("%d", &gpf); + if (g > 1) + { + scanf("%s", gf); + } + scanf("%d", &gpt); + if (g > 1) + { + scanf("%s", gt); + } + int len = strlen(str); + for (int i = 0; i < len; i++) + { + dat[i] = str[i] == '1'; + } + long long tone, tzero, tend_val, fone, fzero, fend_val; + tie(fzero, fone, fend_val) = get_cnt(gf, gpf); + tie(tzero, tone, tend_val) = get_cnt(gt, gpt); + long long one = tone - fone, zero = tzero - fzero; + if (fend_val) + { + one++; + } + else + { + zero++; + } + zero %= prime; + one %= prime; + if (zero < 0) + { + zero += prime; + } + if (one < 0) + { + one += prime; + } + printf("Case %d: %lld %lld\n", i, zero, one); + } + return 0; +} diff --git a/1315.cpp b/1315.cpp new file mode 100644 index 0000000..a022c01 --- /dev/null +++ b/1315.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +typedef long long LL; + +int main() +{ + LL n, t; + scanf("%lld", &t); + while (t--) + { + scanf("%lld", &n); + if (n & 1) + { + printf("%lld\n", (n * n - 2 * n + 1) / 4); + } + else + { + printf("%lld\n", (n * n - 2 * n) / 4); + } + } + return 0; +} diff --git a/13154.cpp b/13154.cpp new file mode 100644 index 0000000..146d1a7 --- /dev/null +++ b/13154.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +// given a str len 10 ^ 4 elements <10 ^ 9 +// define the xor sum within the interval to be xor for the left and right neighbors +// and then do the same until there is only one element for the xor sum +// there will be 30,000 interval query + +// first observe the behavior of the interval xor +// 1 2 3 +// 1 ^ 2 2 ^ 3 +// 1 ^ 2 ^ 2 ^ 3 + +// 1 of which was xor 1, 2 2, 3 1 times +// in 1 to 4 was 1 1 times, 2 3 times, 3 3 times, 4 times +// in fact, pascal triangle corresponds to the first few lines up +// then the xor is evenly zero +// so set the pascal odd to an even number to zero +// we're going to a sierpinski triangle +// @ https://en.wikipedia.org/wiki/file:sierpinski_triangle.svg +// then because he is broken, for example, the first of the fifth line can correspond to the first line of the first +// so we can know in log n that this is 1 or 0 within the triangle +// apply o (nqlgn) => tle +// but this is not enough +// so do observe in n <10000 triangles within the interval 01 not more than 1024 +// so the first section of the triangle pretreatment 01 +// then the prefix can be optimized + +// DO NOT RE-ORDER VAR DECL; somehow, will RE // +int a[10010], psum[10010], n, q; +bool p[10010][10010]; +vector > v[10000]; +// // + +void gen() +{ + for (int i = 1; (1 << (i - 1)) < 10000; i++) + { + for (int j = (1 << (i - 1)) + 1; j <= 10000 && j <= (1 << i); j++) + { + for (int k = 0; k < j; k++) + { + p[j][k] = p[j - (1 << (i - 1))][k >= j / 2 ? j - k - 1 : k]; + } + } + } + for (int i = 1; i <= 10000; i++) + { + int head = 0; + bool finding = false; + for (int j = 0; j < i; j++) + { + if (!finding && p[i][j] == 0) + { + v[i].push_back({head, j - 1}); + finding = true; + } + else if (finding && p[i][j] == 1) + { + head = j; + finding = false; + } + } + v[i].push_back({head, i - 1}); + } +} + +int main() +{ + p[1][0] = 1; + gen(); + int T; + scanf("%d", &T); + for (int I = 1; I <= T; I++) + { + fill(psum, psum + 10010, 0); + printf("Case %d:\n", I); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d", a + i); + psum[i + 1] = a[i] ^ psum[i]; + } + scanf("%d", &q); + for (int J = 0; J < q; J++) + { + int b, c; + scanf("%d%d", &b, &c); + int sum = 0; + for (const auto &x : v[c - b + 1]) + { + sum ^= psum[b + x.first] ^ psum[b + x.second + 1]; + } + printf("%d\n", sum); + } + } + return 0; +} diff --git a/1316.cpp b/1316.cpp new file mode 100644 index 0000000..1912cff --- /dev/null +++ b/1316.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + vector vec[10010]; + for (int i = 0; i < n; i++) + { + int a, b; + scanf("%d%d", &a, &b); + vec[b].push_back(a); + } + priority_queue que; + int ans = 0; + for (int i = 10000; i >= 1; i--) + { + for (int j = 0; j < vec[i].size(); j++) + { + que.push(vec[i][j]); + } + if (que.size() == 0) + { + continue; + } + ans += que.top(); + que.pop(); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/13161.cpp b/13161.cpp new file mode 100644 index 0000000..d3a9793 --- /dev/null +++ b/13161.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int quad_eqn_pos_root(int nA, int nB, int nC) +{ + int n_delta = nB * nB - 4 * nA * nC; + return (-nB + static_cast(sqrt(static_cast(n_delta)))) / 2 / nA; +} + +int main() +{ + int nD, nR, nT; + while (scanf("%d%d%d", &nD,&nR,&nT) == 3) + { + int age = quad_eqn_pos_root(2, 2 - 2 * nD, nD * nD - nD - 18 - 2 * (nT + nR)); + int candles = (age + 4) * (age - 3) / 2; + printf("%d\n", nR - candles); + } + return 0; +} diff --git a/1317.cpp b/1317.cpp new file mode 100644 index 0000000..d2d426e --- /dev/null +++ b/1317.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +#define maxn 2100 +#define inf 1000000000 + +struct edge +{ + int from, to, cap, flow; + int cost; + edge(int from_, int to_, int cap_, int flow_, int cost_) + { + from = from_, to = to_, cap = cap_, flow = flow_, cost = cost_; + } +}; + +struct MCMF +{ + int n, m; + int flow; + int cost; + vector edges; + vector g[maxn]; + int inq[maxn]; + int d[maxn]; + int p[maxn]; + int a[maxn]; + queue q; + + void init(int n_) + { + int i; + n = n_; + for (i = 0; i <= n; i++) + { + g[i].clear(); + } + edges.clear(); + } + + void addedge(int from, int to, int cap, int cost)//有å‘图 + { + edges.push_back(edge(from, to, cap, 0, cost)); + edges.push_back(edge(to, from, 0, 0, -cost)); + m = edges.size(); + g[from].push_back(m - 2); + g[to].push_back(m - 1); + } + + bool spfa(int s, int t) + { + int i, j; + memset(inq, 0, sizeof(inq)); + for (i = 0; i <= n; i++) + { + d[i] = -(inf); + } + while (!q.empty()) + { + q.pop(); + } + d[s] = 0; + inq[s] = true; + q.push(s); + a[s] = inf; + while (!q.empty()) + { + int u = q.front(); + q.pop(); + inq[u] = false; + for (i = 0; i < g[u].size(); i++) + { + edge &e = edges[g[u][i]]; + if (e.cap > e.flow && d[e.to] < d[u] + e.cost) + { + d[e.to] = d[u] + e.cost; + p[e.to] = g[u][i]; + a[e.to] = min(a[u], e.cap - e.flow); + if (!inq[e.to]) + { + q.push(e.to); + inq[e.to] = 1; + } + } + } + } + if (d[t] == -(inf)) + { + return false; + } + flow += a[t]; + cost += d[t] * a[t]; + int u = t; + while (u != s) + { + edges[p[u]].flow += a[t]; + edges[p[u] ^ 1].flow -= a[t]; + u = edges[p[u]].from; + } + return true; + } + + void solve(int s, int t) + { + flow = 0;//æœ€å¤§æµ + cost = 0;//最å°è´¹ç”¨ + int i, j; + for (i = 0; i < 2; i++) + { + spfa(s, t); + } + printf("%d\n", cost); + }; +}; + +int n; +MCMF solver; +struct node +{ + int x, y; + int w; + bool operator<(const node &r) const + { + if (x == r.x) + { + return y < r.y; + } + return x < r.x; + } +} p[2100]; + +int main() +{ + int i, j; + while (scanf("%d", &n) == 1 && n) + { + for (i = 1; i <= n; i++) + { + scanf("%d %d %d", &p[i].x, &p[i].y, &p[i].w); + } + sort(p + 1, p + n + 1); + int s, t; + s = 0, t = 2 * n + 1; + solver.init(2 * n + 1); + for (i = 1; i <= n; i++) + { + solver.addedge(s, i, 1, 0); + solver.addedge(i, i + n, 1, p[i].w); + solver.addedge(i + n, t, 1, 0); + } + for (i = 1; i <= n; i++) + { + for (j = i + 1; j <= n; j++) + { + if (p[j].x > p[i].y) + { + break; + } + } + for (; j <= n; j++) + { + solver.addedge(i + n, j, 1, 0); + } + } + solver.solve(s, t); + } + return 0; +} diff --git a/1319.cpp b/1319.cpp new file mode 100644 index 0000000..b0246dd --- /dev/null +++ b/1319.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vl; +typedef vector vs; +typedef pair pii; +typedef vector vpi; +const double eps = 1e-7; +const int inf = 0x3f3f3f3f; +const int hinf = 0x3f3f3f3f; +const ll mod = 1000000007; + +#define out(x) cout << x << '\n'; +#define fio \ + freopen("input.txt", "r", stdin); \ + freopen("output.txt", "w", stdout); +#define fino freopen("input.txt", "r", stdin); +#define sz(a) sizeof(a) +#define mp make_pair +#define pb push_back +#define fi first +#define se second +#define ms(a, i) memset((a), (i), sz(a)) +#define clr(x) memset(x, 0, sz(x)) +#define cdp(x) memset((x), -1, sizeof(x)) +#define infi(x) memset(x, 0x3f, sz(x)) +#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) + +int m, p, a, b; +double ans; + +int main() +{ + while (scanf("%d%d%d%d", &m, &p, &a, &b) != EOF) + { + ans = 0; + if (b >= 0) + { + ans += 1.0 * b * pow(a, p / 2.0); + m -= b; + } + else + { + ans += 1.0 * a * (-b) * pow(1.0 / a, p / 2.0); + m -= a * (-b); + } + while (m >= (1 + a)) + { + ans += 1.0 * pow(a, p / 2.0); + ans += 1.0 * a * pow(1.0 / a, p / 2.0); + m -= (1 + a); + } + if (m > 1) + { + ans += 1.0 * pow((1.0 * m - 1.0) * (1.0 * m - 1.0) / a, p / 2.0); + ans += 1.0 * (m - 1.0) * pow(1.0 / a, p / 2.0); + } + if (ans - floor(ans) > 0.5) + { + cout << floor(ans) + 1 << endl; + } + else + { + cout << floor(ans) << endl; + } + } + return 0; +} diff --git a/132.cpp b/132.cpp new file mode 100644 index 0000000..88c466b --- /dev/null +++ b/132.cpp @@ -0,0 +1,203 @@ +#include + +using namespace std; + +struct POINT +{ + int x, y; +}; + +// determine whether two points are equal +bool operator==(const POINT &p1, const POINT &p2) +{ + return (p1.x == p2.x && p1.y == p2.y); +} + +// compare the two-point coordinate size, first compare the x coordinate, if the same comparison y coordinates +bool operator>(const POINT &p1, const POINT &p2) +{ + return (p1.x > p2.x || (p1.x == p2.x && p1.y > p2.y)); +} + +// calculate the two-vector outer product +int operator^(const POINT &p1, const POINT &p2) +{ + return (p1.x * p2.y - p1.y * p2.x); +} + +// calculate the two-dimensional inner product +int operator*(const POINT &p1, const POINT &p2) +{ + return (p1.x * p2.x + p1.y * p2.y); +} + +// which of the comparison vectors has a greater angle to the x-axis vector (1, 0) +bool CompareVector(const POINT &pt1, const POINT &pt2) +{ + // modulus of the vector + float m1 = sqrt((float)(pt1.x * pt1.x + pt1.y * pt1.y)); + float m2 = sqrt((float)(pt2.x * pt2.x + pt2.y * pt2.y)); + // two vectors are (1, 0) and the inner product, respectively + float v1 = pt1.x / m1, v2 = pt2.x / m2; + // if the vector angles are equal, then return to the nearest one from the base point, to ensure order + return (v1 > v2 || v1 == v2 && m1 < m2); +} + +bool CalcConvexHull(vector &Src) +{ + // a point set should have ≥3 points to form a polygon + if (Src.size() < 3) + { + return false; + } + // find base pt + vector::iterator i; + POINT ptBase = Src.front();// fst pt is preset to min pt + for (i = Src.begin() + 1; i != Src.end(); ++i) + { + // if y val of curr pt < min pt, or y val ==, x val < + if (i->y < ptBase.y || (i->y == ptBase.y && i->x > ptBase.x)) + { + // curr pt is taken as min pt + ptBase = *i; + } + } + // vect of each pt + base pt is calc + for (i = Src.begin(); i != Src.end();) + { + // exclude same pts as base pt to avoid div err in subsequent sort calc + if (*i == ptBase) + { + i = Src.erase(i); + } + else + { + // dir from base pt to tgt pt + i->x -= ptBase.x, i->y -= ptBase.y; + ++i; + } + } + // sort by angle btw each vect + abscissa + sort(Src.begin(), Src.end(), &CompareVector); + // del repeated vect + Src.erase(unique(Src.begin(), Src.end()), Src.end()); + // ≥2 pts left in pt set, base pt is used to form polygon + if (Src.size() < 2) + { + return false; + } + // fst + last vect are connected + for (vector::reverse_iterator ri = Src.rbegin(); + ri != Src.rend() - 1; ++ri) + { + vector::reverse_iterator riNext = ri + 1; + // vector triangle calc + ri->x -= riNext->x, ri->y -= riNext->y; + } + // del vect not on convex hull + for (i = Src.begin() + 1; i != Src.end(); ++i) + { + // trace back vect in opp dir of rotation + use outer product to det dir of rotation + for (vector::iterator iLast = i - 1; iLast != Src.begin();) + { + int nCross = *i ^ *iLast; + // if cross prod < 0, no rev rot + // if cross prod == 0, use inner product to det dir of inverse + if (nCross < 0 || (nCross == 0 && i->x * iLast->x > 0 && + i->y * iLast->y > 0)) + { + break; + } + // after del prev vect, curr vect needs to be upd, leading + trailing preceding vect + // vect triangle calc + i->x += iLast->x, i->y += iLast->y; + iLast = (i = Src.erase(iLast)) - 1; + } + } + // end-to-end connected vect accum + convert to coord + Src.front().x += ptBase.x, Src.front().y += ptBase.y; + for (i = Src.begin() + 1; i != Src.end(); ++i) + { + i->x += (i - 1)->x, i->y += (i - 1)->y; + } + // base pt is added; all convex hulls calc + Src.push_back(ptBase); + return (Src.size() >= 3); +} + +int main() +{ + while (1) + { + string Name; + while (Name.empty()) + { + getline(cin, Name); + } + if (Name[0] == '#') + { + break; + } + POINT Mass; + cin >> Mass.x >> Mass.y; + vector Poly, Convex; + for (POINT pt; cin >> pt.x >> pt.y && pt.x != 0 && pt.y != 0;) + { + Poly.push_back(pt); + } + Convex = Poly; + if (!CalcConvexHull(Convex)) + { + *(int *)0 = 0; + } + // del extra pt on convex hull edge + for (int i = 0; i < (int)Convex.size(); ++i) + { + int j = (i + 1) % Convex.size(); + int k = (j + 1) % Convex.size(); + POINT v1 = {Convex[j].x - Convex[i].x, Convex[j].y - Convex[i].y}; + POINT v2 = {Convex[k].x - Convex[j].x, Convex[k].y - Convex[j].y}; + // outer product == 0 -> collinear -> can del + if ((v1 ^ v2) == 0) + { + // del midpt + Convex.erase(Convex.begin() + j); + --i; + } + } + int nMin = Poly.size(); + // every edge of a convex hull + for (int i = 0; i < (int)Convex.size(); ++i) + { + int j = (i + 1) % (int)Convex.size(); + // construct a vector with the center of gravity to the two vertices of the edge + POINT v1 = {Mass.x - Convex[i].x, Mass.y - Convex[i].y}; + POINT v2 = {Mass.x - Convex[j].x, Mass.y - Convex[j].y}; + POINT s1 = {Convex[j].x - Convex[i].x, Convex[j].y - Convex[i].y}; + POINT s2 = {Convex[i].x - Convex[j].x, Convex[i].y - Convex[j].y}; + // for outer plot, to det whether center of gravity in inside + // for inner plot to det whether center of gravity btw two ends + if ((s1 ^ v1) >= 0 && v1 * s1 >= 0 && v2 * s2 >= 0) + { + int nMax = 0; + // find pt on edge of orig polygon + for (int k = 0; k < (int)Poly.size(); ++k) + { + // use outer plot to det whether pt on line + s2.x = Poly[k].x - Convex[i].x; + s2.y = Poly[k].y - Convex[i].y; + if ((s2 ^ s1) == 0) + { + // find # of edge (max # of all pts on edge) + nMax = max(k, nMax); + } + } + // find smallest numbered edge + nMin = min(nMin, nMax); + } + } + // output numbered fr 1 + cout << Name << ' ' << nMin + 1 << endl; + } + return 0; +} diff --git a/1322.cpp b/1322.cpp new file mode 100644 index 0000000..df38e41 --- /dev/null +++ b/1322.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define MAXN 65536 +int BIT[MAXN]; +void modify(int x, int val, int L) +{ + while (x <= L) + { + BIT[x] = min(BIT[x], val); + x += x & (-x); + } +} +int query(int x) +{ + int ret = INF; + while (x) + { + ret = min(ret, BIT[x]); + x -= x & (-x); + } + return ret; +} +int main() +{ + int testcase, N, M, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &N, &M); + vector> D; + for (int i = 0; i <= N; i++) + BIT[i] = INF; + modify(N, 0, N);// [1, 1] => 0 + for (int i = 0; i < M; i++) + { + scanf("%d %d", &x, &y); + int val = query(N - x + 1) + 1; + modify(N - y + 1, val, N); + } + int ret = query(1); + printf("%d\n", ret); + if (testcase) + puts(""); + } + return 0; +} +/* + +*/ \ No newline at end of file diff --git a/1324.cpp b/1324.cpp new file mode 100644 index 0000000..976e3fa --- /dev/null +++ b/1324.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +/* ********************************************** +Author : JayYe +Created Time: 2013/8/26 15:17:45 +File Name : JayYe.cpp +*********************************************** */ + +const int maxn = 50 + 10; + +struct Edge +{ + int u, to, c, next; +} edge[888888]; + +int E, head[100010], gap[100010], dis[100010], cur[100010], pre[100010]; + +void init(int n) +{ + for (int i = 0; i <= n; i++) + head[i] = -1; + E = 0; +} + +void addedge(int u, int to, int c, int cc) +{ + edge[E].u = u; + edge[E].to = to; + edge[E].c = c; + edge[E].next = head[u]; + head[u] = E++; + edge[E].u = to; + edge[E].to = u; + edge[E].c = cc; + edge[E].next = head[to]; + head[to] = E++; +} + +int SAP(int s, int t, int n) +{ + for (int i = 0; i <= n; i++) + { + dis[i] = gap[i] = 0; + cur[i] = head[i]; + } + + gap[0] = n; + int u = pre[s] = s, maxflow = 0, aug = -1, to; + while (dis[s] < n) + { + loop: + for (int i = cur[u]; i != -1; i = edge[i].next) + { + to = edge[i].to; + if (edge[i].c > 0 && dis[u] == dis[to] + 1) + { + aug = (aug == -1 || aug > edge[i].c) ? edge[i].c : aug; + pre[to] = u; + cur[u] = i; + u = to; + if (u == t) + { + for (u = pre[u]; to != s; to = u, u = pre[u]) + { + edge[cur[u]].c -= aug; + edge[cur[u] ^ 1].c += aug; + } + maxflow += aug; + aug = -1; + } + goto loop; + } + } + + int mindis = n; + for (int i = head[u]; i != -1; i = edge[i].next) + { + to = edge[i].to; + if (edge[i].c > 0 && dis[to] < mindis) + { + cur[u] = i; + mindis = dis[to]; + } + } + if ((--gap[dis[u]]) == 0) + break; + gap[dis[u] = mindis + 1]++; + u = pre[u]; + } + return maxflow; +} + +struct PP +{ + int id, to; + PP() {} + PP(int id, int to) : id(id), to(to) {} + bool operator<(const PP &a) const + { + return id < a.id; + } +} print[maxn]; + +int n, m, k, s, t; +int mp[maxn][maxn], pos[maxn], ti[maxn]; + +int solve() +{ + init(n); + addedge(0, s, 1111111, 0); + addedge(t, 100000, 1111111, 0); + head[100000] = -1; + int ans = 0, maxflow = 0; + while (maxflow < k) + { + ans++; + for (int i = 1; i <= n; i++) + head[i + ans * n] = -1; + addedge(0, s + ans * n, 1111111, 0); + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + if (mp[i][j]) + { + addedge((ans - 1) * n + i, ans * n + j, 1, 0); + addedge((ans - 1) * n + j, ans * n + i, 1, 0); + } + } + addedge(ans * n + t, 100000, 1111111, 0); + maxflow += SAP(0, 100000, (ans + 1) * n + 2); + } + return ans; +} + +int main() +{ + while (scanf("%d%d%d%d%d", &n, &m, &k, &s, &t) != -1) + { + memset(mp, 0, sizeof(mp)); + for (int i = 0; i < m; i++) + { + int u, to; + scanf("%d%d", &u, &to); + mp[u][to] = 1; + } + int ans = solve(); + printf("%d\n", ans); + for (int i = 1; i <= k; i++) + pos[i] = s, ti[i] = 0; + for (int i = 0; i < ans; i++) + { + int tot = 0; + for (int j = i * n + 1; j <= (i + 1) * n; j++) + for (int l = head[j]; l != -1; l = edge[l].next) + { + int to = edge[l].to; + if (to == 100000 || to == j + n || to == 0) + continue; + int tt = to / n + 1; + if (to % n == 0) + tt--; + if (edge[l].c == 0 && tt == i + 2) + { + int u = j % n; + if (u == 0) + u = n; + to = to % n; + if (to == 0) + to = n; + for (int ii = 1; ii <= k; ii++) + if (pos[ii] == u && ti[ii] == i) + { + print[tot++] = PP(ii, to); + pos[ii] = to; + ti[ii] = i + 1; + break; + } + } + } + for (int j = 1; j <= k; j++) + ti[j] = i + 1; + sort(print, print + tot); + printf("%d", tot); + for (int j = 0; j < tot; j++) + printf(" %d %d", print[j].id, print[j].to); + puts(""); + } + } + return 0; +} diff --git a/1325.cpp b/1325.cpp new file mode 100644 index 0000000..d977aa3 --- /dev/null +++ b/1325.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +const int MAXN = 1000 + 10; + +struct Node +{ + double dis; + int p1, p2; + Node() {} + Node(double a, int b, int c) : dis(a), p1(b), p2(c) {} + bool operator<(const Node &rhs) const + { + return dis < rhs.dis; + } +}; + +int x[MAXN], y[MAXN], z[MAXN], p[MAXN]; +int N0[MAXN], N1[MAXN]; +Node R[MAXN * MAXN]; +int N, M; + +double dist(int i, int j) +{ + return sqrt((double)(x[i] - x[j]) * (x[i] - x[j]) + (double)(y[i] - y[j]) * (y[i] - y[j]) + (double)(z[i] - z[j]) * (z[i] - z[j])); +} + +int main() +{ + while (scanf("%d", &N) == 1) + { + for (int i = 0; i < N; ++i) + { + scanf("%d%d%d%d", x + i, y + i, z + i, p + i); + N0[i] = 1, N1[i] = 0; + } + M = 0; + for (int i = 0; i < N; ++i) + { + for (int j = i + 1; j < N; ++j) + { + R[M++] = Node(dist(i, j), i, j); + } + } + sort(R, R + M); + int cnt = 0, tmp = 0; + double ret = 0; + for (int i = 0; i < M; ++i) + { + int a = R[i].p1, b = R[i].p2; + if (p[a] == p[b]) + { + if (N1[a] - 1 == N0[a]) + tmp--; + if (N1[b] - 1 == N0[b]) + tmp--; + N0[a]++, N0[b]++; + } + else + { + if (N1[a] == N0[a]) + tmp++; + if (N1[b] == N0[b]) + tmp++; + N1[a]++, N1[b]++; + } + if (i + 1 < M && R[i].dis == R[i + 1].dis) + continue; + if (tmp > cnt) + ret = R[i].dis, cnt = tmp; + } + printf("%d\n%.5f\n", cnt, ret); + } + return 0; +} diff --git a/1326.cpp b/1326.cpp new file mode 100644 index 0000000..44edf7a --- /dev/null +++ b/1326.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +const int maxn = 24; +int bitcount(int x) +{ + return x == 0 ? 0 : bitcount(x / 2) + (x & 1); +} +map table; +int a[maxn], n; +int main() +{ + while (scanf("%d", &n) != EOF) + { + char ita[300]; + for (int i = 0; i < n; i++) + { + scanf("%s", ita); + a[i] = 0; + for (int j = 0; j < strlen(ita); j++) + { + a[i] ^= (1 << (ita[j] - 'A')); + } + } + table.clear(); + int n1 = n / 2, n2 = n - n1; + for (int i = 0; i < (1 << n1); i++) + { + int x = 0; + for (int j = 0; j < n1; j++) + if (i & (1 << j)) + { + x ^= a[j]; + } + if (!table.count(x) || bitcount(table[x]) < bitcount(i)) + { + table[x] = i; + } + } + int ans = 0; + for (int i = 0; i < (1 << n2); i++) + { + int x = 0; + for (int j = 0; j < n2; j++) + if (i & (1 << j)) + { + x ^= a[n1 + j]; + } + if (table.count(x) && bitcount(ans) < bitcount(table[x]) + bitcount(i)) + { + ans = (i << n1) ^ table[x]; + } + } + printf("%d\n", bitcount(ans)); + int flag = 0; + for (int i = 0; i < n; i++) + if (ans & (1 << i)) + { + if (!flag) + { + printf("%d", i + 1); + flag = 1; + } + else + { + printf(" %d", i + 1); + } + } + printf("\n"); + } + return 0; +} diff --git a/1327.cpp b/1327.cpp new file mode 100644 index 0000000..10db3f3 --- /dev/null +++ b/1327.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define inf 1 << 20 +#define ll long long +#define maxn 2100 * 2 + +vector g[maxn], scc[maxn], ans[maxn], woman[maxn]; +int n, dfs_clock, scc_cnt, pre[maxn], sccno[maxn], lowlink[maxn]; +stack s; + +void dfs(int u) +{ + int i, j; + pre[u] = lowlink[u] = ++dfs_clock; + s.push(u); + for (i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (!pre[v]) + { + dfs(v); + lowlink[u] = min(lowlink[u], lowlink[v]); + } + else if (!sccno[v]) + lowlink[u] = min(lowlink[u], pre[v]); + } + if (lowlink[u] == pre[u]) + { + scc_cnt++; + scc[scc_cnt].clear(); + while (true) + { + int x = s.top(); + s.pop(); + sccno[x] = scc_cnt; + scc[scc_cnt].push_back(x); + if (x == u) + break; + } + } +} + +void find_scc() +{ + int i, j; + scc_cnt = dfs_clock = 0; + memset(sccno, 0, sizeof(sccno)); + memset(pre, 0, sizeof(pre)); + for (i = 0; i < n; i++) + if (!pre[i]) + dfs(i); +} + +int main() +{ + int i, j; + while (scanf("%d", &n) == 1 && n) + { + for (i = 0; i < 2 * n; i++) + g[i].clear(); + for (i = 0; i < n; i++) + { + int tt; + scanf("%d", &tt); + woman[i].clear(); + while (tt--) + { + int x; + scanf("%d", &x); + x = x - 1 + n; + woman[i].push_back(x); + g[i].push_back(x); + } + } + for (i = 0; i < n; i++) + { + int x; + scanf("%d", &x); + x = x - 1 + n; + g[x].push_back(i); + } + n = n * 2; + find_scc(); + n = n / 2; + for (i = 0; i < n; i++) + { + ans[i].clear(); + for (j = 0; j < woman[i].size(); j++) + if (sccno[i] == sccno[woman[i][j]]) + ans[i].push_back(woman[i][j] - n + 1); + } + for (i = 0; i < n; i++) + { + printf("%d", ans[i].size()); + for (j = 0; j < ans[i].size(); j++) + printf(" %d", ans[i][j]); + printf("\n"); + } + } + return 0; +} diff --git a/1328.cpp b/1328.cpp new file mode 100644 index 0000000..f593274 --- /dev/null +++ b/1328.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +const int maxn = 1e6 + 10; +char str[maxn]; +int f[maxn]; +void getFail(char *str, int len) +{ + f[0] = f[1] = 0; + for (int i = 1; i < len; i++) + { + int j = f[i]; + while (j && str[i] != str[j]) + { + j = f[j]; + } + f[i + 1] = (str[i] == str[j] ? j + 1 : 0); + } +} +int main() +{ + int n, cas = 1; + while (scanf("%d", &n) && n) + { + scanf("%s", str); + getFail(str, n); + printf("Test case #%d\n", cas++); + for (int i = 1; i <= n; i++) + { + if (f[i] > 0 && i % (i - f[i]) == 0) + { + printf("%d %d\n", i, i / (i - f[i])); + } + } + printf("\n"); + } + return 0; +} diff --git a/1329.cpp b/1329.cpp new file mode 100644 index 0000000..0004ded --- /dev/null +++ b/1329.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +#define MAX 20001 + +int p[MAX], dis[MAX], n; +int find(int x) +{ + if (p[x] == x) + { + return x; + } + int ita = p[x]; + p[x] = find(p[x]); + dis[x] += dis[ita]; + return p[x]; +} +int main() +{ + int cas; + scanf("%d", &cas); + while (cas--) + { + memset(dis, 0, sizeof(dis)); + scanf("%d", &n); + for (int i = 0; i <= n; i++) + { + p[i] = i; + } + char op[5]; + while (scanf("%s", op) && op[0] != 'O') + { + if (op[0] == 'E') + { + int x; + scanf("%d", &x); + find(x); + printf("%d\n", dis[x]); + } + else + { + int u, v; + scanf("%d%d", &u, &v); + dis[u] = abs(u - v) % 1000; + p[u] = v; + } + } + } + return 0; +} diff --git a/133.cpp b/133.cpp new file mode 100644 index 0000000..713391b --- /dev/null +++ b/133.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +vector people; + +int main() +{ + int N, k, m, lc, lw; + while (1) + { + scanf("%d%d%d", &N, &k, &m); + if (!N && !k && !m) + { + break; + } + people.clear(); + for (int i = 1; i <= N; i++) + { + people.push_back(i); + } + lc = 0; + lw = N - 1; + while (int(people.size()) > 0) + { + lc += k - 1; + lw -= m - 1; + while (lc >= people.size()) + { + lc -= people.size(); + } + while (lw < 0) + { + lw += people.size(); + } + if (lc == lw) + { + printf("%3d", people[lc]); + people.erase(people.begin() + lc); + lw--; + } + else + { + printf("%3d%3d", people[lc], people[lw]); + if (lc > lw) + { + people.erase(people.begin() + lc); + lc--; + people.erase(people.begin() + lw); + lw--; + } + else + { + people.erase(people.begin() + lw); + people.erase(people.begin() + lc); + lw -= 2; + } + } + if (int(people.size()) > 0) + { + printf(","); + } + } + printf("10"); + } + return 0; +} diff --git a/1330.cpp b/1330.cpp new file mode 100644 index 0000000..2997d70 --- /dev/null +++ b/1330.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const int MAXN = 1001; +int m, n, up[MAXN][MAXN], left_[MAXN][MAXN], right_[MAXN][MAXN]; +char map_[MAXN][MAXN]; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + int ans = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + char ch; + while (ch = getchar()) + { + if (ch == 'R' || ch == 'F') + { + break; + } + } + map_[i][j] = ch; + } + } + for (int i = 0; i < n; i++) + { + int posleft_ = -1, posright_ = m; + for (int j = 0; j < m; j++) + { + if (map_[i][j] == 'R') + { + up[i][j] = left_[i][j] = 0; + posleft_ = j; + } + else + { + up[i][j] = i == 0 ? 1 : up[i - 1][j] + 1; + left_[i][j] = i == 0 ? posleft_ + 1 : max(left_[i - 1][j], posleft_ + 1); + } + } + for (int j = m - 1; j >= 0; j--) + { + if (map_[i][j] == 'R') + { + right_[i][j] = n; + posright_ = j; + } + else + { + right_[i][j] = i == 0 ? posright_ - 1 : min(right_[i - 1][j], posright_ - 1); + } + ans = max(ans, up[i][j] * (right_[i][j] - left_[i][j] + 1)); + } + } + printf("%d\n", ans * 3); + } + return 0; +} diff --git a/1331.cpp b/1331.cpp new file mode 100644 index 0000000..2b3b850 --- /dev/null +++ b/1331.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) + +#define INF 0x3f3f3f3f +const int N = 55; +const double eps = 1e-6; +int t, n; +double dp[N][N]; +struct Point +{ + double x, y; + void read() + { + scanf("%lf%lf", &x, &y); + } +} p[N]; + +double area(Point a, Point b, Point c) +{ + return fabs((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y)) / 2.0; +} + +void init() +{ + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + p[i].read(); + } +} + +bool judge(int i, int j, int k) +{ + double s = area(p[i], p[j], p[k]); + for (int x = 0; x < n; x++) + { + if (x == i || x == j || x == k) + { + continue; + } + double sum = area(p[i], p[j], p[x]) + area(p[i], p[k], p[x]) + area(p[k], p[j], p[x]); + if (fabs(sum - s) < eps) + { + return false; + } + } + return true; +} + +double solve() +{ + double ans = INF; + for (int len = 2; len < n; len++) + { + for (int l = 0; l < n; l++) + { + int r = (l + len) % n; + dp[l][r] = INF; + for (int k = (l + 1) % n; k != r; k = (k + 1) % n) + { + if (!judge(l, k, r)) + { + continue; + } + dp[l][r] = min(dp[l][r], max(max(dp[l][k], dp[k][r]), area(p[l], p[k], p[r]))); + } + if (len == n - 1) + { + ans = min(ans, dp[l][r]); + } + } + } + return ans; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + init(); + printf("%.1lf\n", solve()); + } + return 0; +} diff --git a/1333.cpp b/1333.cpp new file mode 100644 index 0000000..666c82f --- /dev/null +++ b/1333.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +// UVa 1333 - Model Rocket Height +// Accepted 0.015s C++ + + + + +double D, H; +double a, b, c; +double x, y, z; + +double cot2(double theta) +{ + theta *= M_PI / 180.; + return 1 / tan(theta) / tan(theta); +} + +int main() +{ + while (scanf("%lf%lf", &D, &H) == 2) + { + while (scanf("%lf%lf%lf", &a, &b, &c) && a > 0 && b > 0 && c > 0) + { + a = cot2(a); + b = cot2(b); + c = cot2(c); + x = D * (c - a) / (2 * a - 4 * b + 2 * c); + z = sqrt(((x - D) * (x - D) - x * x) / (a - b)); + y = sqrt(c * z * z - (x + D) * (x + D)); + printf("%.0lf\n", z + H); + } + } +} diff --git a/1335.cpp b/1335.cpp new file mode 100644 index 0000000..1fd5f47 --- /dev/null +++ b/1335.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +const int maxn = 1e5 + 10; +int a[maxn], l[maxn], r[maxn]; +bool judge(int p, int n) +{ + int x = a[0], y = p - a[0], i; + l[0] = x; + r[0] = 0; + for (i = 1; i < n; i++) + { + if (i % 2 == 0)//第奇数个,尽é‡Âå‘å³å– + { + r[i] = min(a[i], y - r[i - 1]); + l[i] = a[i] - r[i]; + } + else//第å¶数个,尽é‡Âå‘左å– + { + l[i] = min(a[i], x - l[i - 1]); + r[i] = a[i] - l[i]; + } + } + return l[n - 1] == 0; +} +int main() +{ + int n; + while (scanf("%d", &n) != EOF) + { + if (n == 0) + { + break; + } + int i, j, k, ans = -1; + for (i = 0; i < n; i++) + { + scanf("%d", &a[i]); + if (i != 0) + { + ans = max(ans, a[i] + a[i - 1]); + } + } + ans = max(ans, a[0] + a[n - 1]); + if (n % 2 == 0) + { + printf("%d\n", ans); + } + else if (n == 1) + { + printf("%d\n", a[0]); + } + else + { + int l = ans, r = ans * 3; + while (l < r) + { + int mid = (l + r) / 2; + if (judge(mid, n)) + { + r = mid; + } + else + { + l = mid + 1; + } + } + printf("%d\n", l); + } + } + return 0; +} diff --git a/1336.cpp b/1336.cpp new file mode 100644 index 0000000..20a2fe8 --- /dev/null +++ b/1336.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +#define MAXN 1024 +#define INF 1000000005 + +struct Pos +{ + int x, c, delta;// cost = c + t * delta + bool operator<(const Pos &a) const + { + return x < a.x; + } +} D[MAXN]; + +double dp[MAXN][MAXN][2];// [interval len][l][stop interval L/R] +int sumD[MAXN]; + +int main() +{ + double V; + int N, X; + while (scanf("%d%lf%d", &N, &V, &X) == 3 && N) + { + for (int i = 1; i <= N; i++) + { + scanf("%d%d%d", &D[i].x, &D[i].c, &D[i].delta); + } + sort(D + 1, D + 1 + N); + + sumD[0] = 0; + for (int i = 1; i <= N; i++) + { + sumD[i] = sumD[i - 1] + D[i].delta; + } + + for (int i = 0; i <= N; i++) + { + for (int j = 0; j <= N + 1; j++) + { + dp[i][j][0] = dp[i][j][1] = INF; + } + } + + for (int i = 1; i <= N; i++) + { + double cost = sumD[N] * (fabs(X - D[i].x) / V) + D[i].c; + dp[1][i][0] = dp[1][i][1] = cost; + } + + for (int i = 2; i <= N; i++) + { + for (int j = 1; j + i - 1 <= N; j++) + { + double fromL, fromR; + fromL = fromR = INF; + int l = j, r = j + i - 1; + + fromL = dp[i - 1][l + 1][0] + (sumD[l] + sumD[N] - sumD[r]) * ((D[l + 1].x - D[l].x) / V) + D[l].c; + fromR = dp[i - 1][l + 1][1] + (sumD[l] + sumD[N] - sumD[r]) * ((D[r].x - D[l].x) / V) + D[l].c; + dp[i][l][0] = min(fromL, fromR); + + fromL = dp[i - 1][l][0] + (sumD[l - 1] + sumD[N] - sumD[r - 1]) * ((D[r].x - D[l].x) / V) + D[r].c; + fromR = dp[i - 1][l][1] + (sumD[l - 1] + sumD[N] - sumD[r - 1]) * ((D[r].x - D[r - 1].x) / V) + D[r].c; + dp[i][l][1] = min(fromL, fromR); + } + } + printf("%d\n", (int)min(dp[N][1][0], dp[N][1][1])); + } + return 0; +} diff --git a/1337.cpp b/1337.cpp new file mode 100644 index 0000000..0c7c16a --- /dev/null +++ b/1337.cpp @@ -0,0 +1,616 @@ +#include + +using namespace std; + +#define abs(x) ((x) >= 0 ? (x) : -(x)) +#define i64 long long +#define u32 unsigned int +#define u64 unsigned long long +#define clr(x, y) memset(x, y, sizeof(x)) +#define CLR(x) x.clear() +#define ph(x) push(x) +#define pb(x) push_back(x) +#define Len(x) x.length() +#define SZ(x) x.size() +#define PI acos(-1.0) +#define sqr(x) ((x) * (x)) +#define MP(x, y) make_pair(x, y) +#define EPS 1e-8 + +#define FOR0(i, x) for (i = 0; i < x; i++) +#define FOR1(i, x) for (i = 1; i <= x; i++) +#define FOR(i, a, b) for (i = a; i <= b; i++) +#define FORL0(i, a) for (i = a; i >= 0; i--) +#define FORL1(i, a) for (i = a; i >= 1; i--) +#define FORL(i, a, b) for (i = a; i >= b; i--) + +#define rush() \ + int CC; \ + for (scanf("%d", &CC); CC--;) +#define Rush(n) while (scanf("%d", &n) != -1) + +void RD(int &x) { scanf("%d", &x); } +void RD(i64 &x) { scanf("%lld", &x); } +void RD(u64 &x) { scanf("%I64u", &x); } +void RD(u32 &x) { scanf("%u", &x); } +void RD(double &x) { scanf("%lf", &x); } +void RD(int &x, int &y) { scanf("%d%d", &x, &y); } +void RD(i64 &x, i64 &y) { scanf("%lld%lld", &x, &y); } +void RD(u32 &x, u32 &y) { scanf("%u%u", &x, &y); } +void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); } +void RD(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); } +void RD(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); } +void RD(i64 &x, i64 &y, i64 &z) { scanf("%lld%lld%lld", &x, &y, &z); } +void RD(u32 &x, u32 &y, u32 &z) { scanf("%u%u%u", &x, &y, &z); } +void RD(char &x) { x = getchar(); } +void RD(char *s) { scanf("%s", s); } +void RD(string &s) { cin >> s; } + +void PR(int x) { printf("%d\n", x); } +void PR(int x, int y) { printf("%d %d\n", x, y); } +void PR(i64 x) { printf("%lld\n", x); } +void PR(i64 x, i64 y) { printf("%lld %lld\n", x, y); } +void PR(u32 x) { printf("%u\n", x); } +void PR(u64 x) { printf("%llu\n", x); } +void PR(double x) { printf("%.2lf\n", x); } +void PR(double x, double y) { printf("%.5lf %.5lf\n", x, y); } +void PR(char x) { printf("%c\n", x); } +void PR(char *x) { printf("%s\n", x); } +void PR(string x) { cout << x << endl; } + +const int mod = 10007; +const i64 inf = ((i64)1) << 40; +const double dinf = 1000000000000000000.0; +const int INF = 100000000; +const int N = 1005; + +int sgn(double x) +{ + if (x > EPS) + return 1; + if (x < -EPS) + return -1; + return 0; +} + +struct point +{ + double x, y; + + point() {} + point(double _x, double _y) + { + x = _x; + y = _y; + } + + void get() + { + RD(x); + RD(y); + } + + point operator+(point a) + { + return point(x + a.x, y + a.y); + } + + point operator-(point a) + { + return point(x - a.x, y - a.y); + } + + double operator*(point a) + { + return x * a.y - y * a.x; + } + + point operator*(double t) + { + return point(x * t, y * t); + } + + double operator^(point a) + { + return x * a.x + y * a.y; + } + + double len() + { + return sqrt(x * x + y * y); + } + + point zhuanShun(double t) + { + return point(x * cos(t) + y * sin(t), y * cos(t) - x * sin(t)); + } + + point zhuanNi(double t) + { + return point(x * cos(t) - y * sin(t), x * sin(t) + y * cos(t)); + } + + point adjust(double L) + { + double d = len(); + L /= d; + return point(x * L, y * L); + } + + void print() + { + printf("%.3lf %.3lf\n", x + EPS, y + EPS); + } +}; + +double len(point a) +{ + return a.len(); +} + +struct point3 +{ + double x, y, z; + + point3() {} + point3(double _x, double _y, double _z) + { + x = _x; + y = _y; + z = _z; + } + + void get() + { + cin >> x >> y >> z; + } + + point3 operator+(point3 a) + { + return point3(x + a.x, y + a.y, z + a.z); + } + + point3 operator-(point3 a) + { + return point3(x - a.x, y - a.y, z - a.z); + } + + point3 operator*(point3 a) + { + return point3(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x); + } + + point3 operator*(double t) + { + return point3(x * t, y * t, z * t); + } + + double operator^(point3 a) + { + return x * a.x + y * a.y + z * a.z; + } + + point3 operator/(double t) + { + return point3(x / t, y / t, z / t); + } + + double len() + { + return sqrt(x * x + y * y + z * z); + } + + point3 adjust(double L) + { + double t = len(); + L /= t; + return point3(x * L, y * L, z * L); + } + + void print() + { + printf("%.10lf %.10lf %.10lf\n", x + EPS, y + EPS, z + EPS); + } +}; + +double len(point3 a) +{ + return a.len(); +} + +double getArea(point3 a, point3 b, point3 c) +{ + double x = len((b - a) * (c - a)); + return x / 2; +} + +double getVolume(point3 a, point3 b, point3 c, point3 d) +{ + double x = (b - a) * (c - a) ^ (d - a); + return x / 6; +} + +point3 pShadowOnPlane(point3 p, point3 a, point3 b, point3 c) +{ + point3 v = (b - a) * (c - a); + if (sgn(v ^ (a - p)) < 0) + v = v * -1; + v = v.adjust(1); + double d = fabs(v ^ (a - p)); + return p + v * d; +} + +double lineToLine(point3 a, point3 b, point3 p, point3 q) +{ + point3 v = (b - a) * (q - p); + return fabs((a - p) ^ v) / len(v); +} + +int pInPlane(point3 p, point3 a, point3 b, point3 c) +{ + double S = getArea(a, b, c); + double S1 = getArea(a, b, p); + double S2 = getArea(a, c, p); + double S3 = getArea(b, c, p); + return sgn(S - S1 - S2 - S3) == 0; +} + +int opposite(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + point3 v = (b - a) * (c - a); + double x = v ^ (p - a); + double y = v ^ (q - a); + return sgn(x * y) < 0; +} + +int segCrossTri(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + return opposite(p, q, a, b, c) && + opposite(a, b, p, q, c) && + opposite(a, c, p, q, b) && + opposite(b, c, p, q, a); +} + +double pToPlane(point3 p, point3 a, point3 b, point3 c) +{ + double v = ((b - a) * (c - a) ^ (p - a)) / 6; + double s = len((b - a) * (c - a)) / 2; + return fabs(3 * v / s); +} + +double pToLine(point3 p, point3 a, point3 b) +{ + double S = len((a - p) * (b - p)); + return S / len(a - b); +} + +double pToSeg(point3 p, point3 a, point3 b) +{ + if (sgn((p - a) ^ (b - a)) <= 0) + return len(a - p); + if (sgn((p - b) ^ (a - b)) <= 0) + return len(b - p); + return pToLine(p, a, b); +} + +double pToPlane1(point3 p, point3 a, point3 b, point3 c) +{ + point3 k = pShadowOnPlane(p, a, b, c); + if (pInPlane(k, a, b, c)) + return pToPlane(p, a, b, c); + double x = pToSeg(p, a, b); + double y = pToSeg(p, a, c); + double z = pToSeg(p, b, c); + return min(x, min(y, z)); +} + +double getAng(point3 a, point3 b) +{ + double x = (a ^ b) / len(a) / len(b); + return acos(x); +} + +double segToSeg(point3 a, point3 b, point3 p, point3 q) +{ + point3 v = (b - a) * (q - p); + + double A, B, A1, B1; + A = ((b - a) * v) ^ (p - a); + B = ((b - a) * v) ^ (q - a); + + A1 = ((p - q) * v) ^ (a - q); + B1 = ((p - q) * v) ^ (b - q); + if (sgn(A * B) <= 0 && sgn(A1 * B1) <= 0) + { + return lineToLine(a, b, p, q); + } + + double x = min(pToSeg(a, p, q), pToSeg(b, p, q)); + double y = min(pToSeg(p, a, b), pToSeg(q, a, b)); + return min(x, y); +} + +struct face +{ + int a, b, c, ok; + + face() {} + face(int _a, int _b, int _c, int _ok) + { + a = _a; + b = _b; + c = _c; + ok = _ok; + } +}; + +struct _3DCH +{ + face F[N << 2]; + int b[N][N], cnt, n; + point3 p[N]; + + int getDir(point3 t, face F) + { + double x = (p[F.b] - p[F.a]) * (p[F.c] - p[F.a]) ^ (t - p[F.a]); + return sgn(x); + } + + void deal(int i, int x, int y) + { + int f = b[x][y]; + if (!F[f].ok) + return; + if (getDir(p[i], F[f]) == 1) + DFS(i, f); + else + { + b[y][x] = b[x][i] = b[i][y] = cnt; + F[cnt++] = face(y, x, i, 1); + } + } + + void DFS(int i, int j) + { + F[j].ok = 0; + deal(i, F[j].b, F[j].a); + deal(i, F[j].c, F[j].b); + deal(i, F[j].a, F[j].c); + } + + void construct() + { + int i, j, k = 0; + for (i = 1; i < n; i++) + if (sgn(len(p[i] - p[0]))) + { + swap(p[i], p[1]); + k++; + break; + } + if (k != 1) + return; + for (i = 2; i < n; i++) + if (sgn(getArea(p[0], p[1], p[i]))) + { + swap(p[i], p[2]); + k++; + break; + } + if (k != 2) + return; + for (i = 3; i < n; i++) + if (sgn(getVolume(p[0], p[1], p[2], p[i]))) + { + swap(p[i], p[3]); + k++; + break; + } + if (k != 3) + return; + + cnt = 0; + FOR0(i, 4) + { + face k = face((i + 1) % 4, (i + 2) % 4, (i + 3) % 4, 1); + if (getDir(p[i], k) == 1) + swap(k.b, k.c); + b[k.a][k.b] = b[k.b][k.c] = b[k.c][k.a] = cnt; + F[cnt++] = k; + } + + for (i = 4; i < n; i++) + FOR0(j, cnt) + { + if (F[j].ok && getDir(p[i], F[j]) == 1) + { + DFS(i, j); + break; + } + } + j = 0; + FOR0(i, cnt) + if (F[i].ok) F[j++] = F[i]; + cnt = j; + } + + point3 getCenter() + { + point3 ans = point3(0, 0, 0), o = point3(0, 0, 0); + double s = 0, temp; + int i; + FOR0(i, cnt) + { + face k = F[i]; + temp = getVolume(o, p[k.a], p[k.b], p[k.c]); + ans = ans + (o + p[k.a] + p[k.b] + p[k.c]) / 4 * temp; + s += temp; + } + ans = ans / s; + return ans; + } + + double getMinDis(point3 a) + { + double ans = dinf; + int i; + FOR0(i, cnt) + { + face k = F[i]; + ans = min(ans, pToPlane(a, p[k.a], p[k.b], p[k.c])); + } + return ans; + } +}; + +/***********************************************************/ + +point3 a[N]; +vector V, V1; +double R[N]; +int n; + +void deal(point3 a, double ra, point3 b, double rb, int i, int j) +{ + b = b - a; + point p = point(0, 0), q = point(sqrt(b.y * b.y + b.x * b.x), b.z); + double d = len(b); + if (sgn(d - ra - rb) == 1) + return; + if (sgn(fabs(ra - rb) - d) == 1) + return; + + double ang = atan2(q.y, q.x); + q = q.zhuanShun(ang); + double ang1 = acos((ra * ra + d * d - rb * rb) / (2 * ra * d)); + point v; + + v = point(cos(ang1), sin(ang1)); + v = v.adjust(ra); + v = v.zhuanNi(ang); + V.pb(v.y + a.z); + + v = point(cos(ang1), sin(-ang1)); + v = v.adjust(ra); + v = v.zhuanNi(ang); + V.pb(v.y + a.z); +} + +int visit[N], num; +vector G[N]; +point C[N]; +double CR[N]; + +void DFS(int u) +{ + visit[u] = 1; + int i, v; + FOR0(i, SZ(G[u])) + { + v = G[u][i]; + if (!visit[v]) + DFS(v); + } +} + +int cal(double z) +{ + vector p; + int i, j; + num = 0; + FOR1(i, n) + if (sgn(a[i].z - R[i] - z) <= 0 && sgn(a[i].z + R[i] - z) >= 0) + { + p.pb(i); + G[i].clear(); + C[num] = point(a[i].x, a[i].y); + CR[num] = sqr(R[i]) - sqr(a[i].z - z); + if (CR[num] < 0) + CR[num] = 0; + CR[num] = sqrt(CR[num]); + num++; + } + clr(visit, 0); + double low, high; + FOR0(i, SZ(p)) + for (j = i + 1; j < SZ(p); j++) + { + if (sgn(len(C[i] - C[j]) - CR[i] - CR[j]) <= 0) + { + G[p[i]].pb(p[j]); + G[p[j]].pb(p[i]); + } + } + int ans = 0; + FOR0(i, SZ(p)) + if (!visit[p[i]]) + { + DFS(p[i]); + ans++; + } + + return ans; +} + +int main() +{ + Rush(n) + { + if (!n) + { + break; + } + int i, j; + FOR1(i, n) + { + a[i].get(); + RD(R[i]); + } + V.clear(); + FOR1(i, n) + for (j = i + 1; j <= n; j++) + { + deal(a[i], R[i], a[j], R[j], i, j); + } + FOR1(i, n) + V.pb(a[i].z - R[i]), + V.pb(a[i].z + R[i]); + V.pb(-1); + V.pb(36001); + sort(V.begin(), V.end()); + V1.clear(); + FOR0(i, SZ(V)) + { + if (i == 0 || sgn(V[i] - V[i - 1]) > 0) + { + V1.pb(V[i]); + } + } + V = V1; + vector ans; + int pre = -1; + FOR1(i, SZ(V) - 1) + { + j = cal((V[i] + V[i - 1]) / 2); + if (j != pre) + { + ans.pb(j); + pre = j; + } + } + PR((int)SZ(ans) - 1); + FOR1(i, SZ(ans) - 1) + { + if (ans[i] > ans[i - 1]) + { + printf("1"); + } + else + { + printf("0"); + } + } + printf("\n"); + } + return 0; +} diff --git a/1339.cpp b/1339.cpp new file mode 100644 index 0000000..5304ba0 --- /dev/null +++ b/1339.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +char x[101], y[101]; +int arr1[26], arr2[26]; + +int main() +{ + while(scanf("%s%s", x, y) == 2) + { + int len = strlen(x); + for (int i = 0; i < 26; i++) + { + arr1[i] = arr2[i] = 0; + } + + for (int i = 0; i < len; i++) + { + arr1[x[i] - 65]++; + arr2[y[i] - 65]++; + } + + sort(arr1, arr1 + 26); + sort(arr2, arr2 + 26); + + bool flag = true; + for (int i = 0; i < 26; i++) + { + if (arr1[i] != arr2[i]) + { + flag = false; + break; + } + } + + printf(flag ? "YES\n" : "NO\n"); + } + return 0; +} diff --git a/134.cpp b/134.cpp new file mode 100644 index 0000000..0befd90 --- /dev/null +++ b/134.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +//å„ç§ç¬¦å·çš„枚举,åŽé¢çš„æ³¨é‡Šä¸ºé¢˜ç›®ä¸­å¯¹åº”çš„ç¬¦å· +enum SYMBOL +{ + A, + MOD, + LA, + BA, + DA, + PREDA, + NAM, + SE, + PC, + P, + PN, + PS, + ST, + VP, + PV, + UN +}; +bool aVowel[] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0};//元音表 +static SYMBOL aConvTbl[14][4] = //状æ€è½¬æ¢è¡¨ï¼Œæ¯å››ä¸ªçŠ¶æ€ä¸ºä¸€ç»„,顺åºæŽ’列 + { + {PREDA, UN, PREDA, PREDA}, {PREDA, UN, UN, PS}, {NAM, UN, UN, PN}, {LA, UN, PS, PN}, {MOD, UN, PS, VP}, {A, PS, PS, PS}, {PS, UN, UN, P}, {DA, UN, P, PC}, {BA, PN, P, PC}, {VP, PN, UN, PV}, {PV, UN, PN, ST}, {PV, UN, UN, ST}, {PC, UN, UN, SE}, {ST, UN, UN, SE},//æ¯ç»„å››ä¸ªç¬¦å· +}; //1:åˆå§‹ç¬¦å·ï¼Œ2:å‰ç¼€ï¼Œ3:åŽç¼€ï¼Œ4ï¼šæŽ¨å¯¼å‡ºçš„ç¬¦å· + +//将输入的字符串转æ¢ä¸ºç¬¦å·çš„函数 +SYMBOL Token2Status(const string &str) +{ + int nNum = str.length(), cLast = str[nNum - 1]; + if (!islower(cLast) || !aVowel[cLast - 'a']) + { + return NAM;//末尾ä¸æ˜¯å…ƒéŸ³çš„为NAM + } + switch (nNum) + { + case 1: + return A;//åªæœ‰ä¸€ä½å…ƒéŸ³çš„åªèƒ½æ˜¯A + case 5: //用ä½è¿ç®—快速判断谓è¯æ˜¯å¦ç¬¦åˆè§„则CCVCV或CVCCV + nNum = aVowel[str[4] - 'a']; + nNum |= ((aVowel[str[0] - 'a'] << 4) | (aVowel[str[1] - 'a'] << 3)); + nNum |= ((aVowel[str[2] - 'a'] << 2) | (aVowel[str[3] - 'a'] << 1)); + return (nNum == 5 || nNum == 9) ? PREDA : UN; + case 2: //两ä½çš„å•è¯ + switch (str[0])//æ ¹æ®ç¬¬ä¸€ä½åˆ¤æ–­æ˜¯å“ªä¸€ç»„ + { + case 'g': + return MOD; + case 'b': + return BA; + case 'd': + return DA; + case 'l': + return LA; + } + } + return UN;//æœªèƒ½è¯†åˆ«çš„é”™è¯¯ç¬¦å· +} + +int main() +{ + vector Set; + for (string str; cin >> str && str != "#";)//循环读入æ¯ä¸ªå•è¯ + { + int nDot = str.find('.');//如果å•è¯ä¸­å‘现å¥ç‚¹ï¼Œåˆ™è®¤ä¸ºå¥å­ç»“æŸ + if (nDot == str.npos) //没有å‘现å¥ç‚¹ + { + Set.push_back(Token2Status(str));//å°†å•è¯è½¬ä¸ºç¬¦å·åŽå­˜å…¥è¯­å¥ + continue; + } //以下为å‘现å¥ç‚¹ï¼Œå³é‡åˆ°å¥å­ç»“æŸ + str.erase(str.length() - 1);//删除å¥ç‚¹ + if (!str.empty()) //å•è¯ä¸ä¸ºç©ºåˆ™åŠ å…¥è¯­å¥ + { + Set.push_back(Token2Status(str)); + } //以下进行è¯æ³•åˆ†æžå¹¶è¾“出结果 + for (int i = 0; i < 14; ++i)//ä¾æ¬¡å¤„ç†æ¯ä¸€ç§çŠ¶æ€ + { + SYMBOL *pTbl = aConvTbl[i];//为加快è¿ç®—,节çœä»£ç ï¼Œè®¾ä¸´æ—¶å˜é‡ + for (vector::iterator j = Set.begin(); j != Set.end();) + { + vector::iterator iBeg = Set.begin(), iEnd = Set.end(); + if (*j != pTbl[0]) + { + ++j;//ä¸æ˜¯æŒ‡å®šç¬¦å·ï¼Œé例下一个 + continue; + }//如果指定了å‰é¢æˆ–åŽé¢ç›¸é‚»çš„符å·åˆ™éªŒè¯å…¶æ˜¯å¦å­˜åœ¨ + if (pTbl[1] != UN && (j == iBeg || *(j - 1) != pTbl[1])) + { + ++j;//存在的符å·ä¸ŽæŒ‡å®šçš„ä¸ç¬¦ï¼Œç»“果错误 + continue; + } + if (pTbl[2] != UN && (j == iEnd - 1 || *(j + 1) != pTbl[2])) + { + ++j;//存在的符å·ä¸ŽæŒ‡å®šçš„ä¸ç¬¦ï¼Œç»“果错误 + continue; + }//删除å‰åŽçš„符å·(如果指定) + j = pTbl[1] != UN ? Set.erase(j - 1) : j; + j = pTbl[2] != UN ? Set.erase(j + 1) - 1 : j; + *j = pTbl[3];//当å‰ç¬¦å·å˜æ›´ä¸ºæŒ‡å®šçš„ç›®æ ‡ç¬¦å· + } + } + cout << (Set.size() == 1 && Set[0] == SE ? "Good" : "Bad!") << endl; + Set.clear();//清空语å¥ï¼Œå‡†å¤‡å¤„ç†ä¸‹ä¸€æ¡è¯­å¥ + } + return 0; +} diff --git a/1340.cpp b/1340.cpp new file mode 100644 index 0000000..0b607ae --- /dev/null +++ b/1340.cpp @@ -0,0 +1,299 @@ +#include + +using namespace std; + +#define dcmp(x) (((x) > -eps) - ((x) < eps)) + +const double eps = 1e-7; + +struct Poi +{ + double x, y; + Poi(double x = 0, double y = 0) : x(x), y(y) {} + bool operator<(const Poi &rhs) const + { + return dcmp(x - rhs.x) < 0 || (dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) < 0); + } + bool operator==(const Poi &rhs) const + { + return dcmp(x - rhs.x) == 0 && dcmp(y - rhs.y) == 0; + } + Poi operator+(const Poi &rhs) const + { + return Poi(x + rhs.x, y + rhs.y); + } + Poi operator-(const Poi &rhs) const + { + return Poi(x - rhs.x, y - rhs.y); + } + Poi operator*(double p) const + { + return Poi(x * p, y * p); + } + Poi operator/(double p) + { + return Poi(x / p, y / p); + } + void out() + { + printf("%.4lf %.4lf\n", x, y); + } +}; + +typedef Poi Vec; +typedef vector Poly; + +double cross(const Vec &a, const Vec &b) +{ + return a.x * b.y - a.y * b.x; +} + +double dot(const Vec &a, const Vec &b) +{ + return a.x * a.x + a.y * a.y; +} + +double length(const Vec &v) +{ + return sqrt(dot(v, v)); +} + +Poi gPoi() +{ + int x, y; + scanf("%d%d", &x, &y); + return Poi(x, y); +} + +bool segprointer(const Poi &a1, const Poi &a2, const Poi &b1, const Poi &b2) +{ + Vec vb = b2 - b1, va = a2 - a1; + int c1 = dcmp(cross(vb, a2 - b1)), c2 = dcmp(cross(vb, a1 - b1)); + int c3 = dcmp(cross(va, b2 - a1)), c4 = dcmp(cross(va, b1 - a1)); + return (c1 ^ c2) == -2 && (c3 ^ c4) == -2; +} + +Poi lineinter(const Poi &p, const Vec &v, const Poi &q, const Vec &w) +{ + Vec u = p - q; + double t = cross(w, u) / cross(v, w); + return p + v * t; +} + +struct Edge +{ + int from, to; + double ang; +}; + +double PolyArea(const Poly &poly) +{ + int n = poly.size(); + double area = 0; + for (int i = 1; i < n - 1; i++) + { + area += cross(poly[i] - poly[0], poly[i + 1] - poly[0]); + } + return area / 2; +} + +bool cmp(int, int); + +template +struct PSLG +{ + vector edges; + vector faces; + vector G[N]; + double x[N], y[N], area[N]; + bool vis[N]; + int left[N], prev[N]; + int n, m, face_cnt; + void init(int n) + { + this->n = n; + for (int i = 0; i < n; i++) + { + G[i].clear(); + } + edges.clear(); + faces.clear(); + } + double getAngle(int from, int to) + { + return atan2(y[to] - y[from], x[to] - x[from]); + } + void AddEdge(int from, int to) + { + edges.push_back((Edge){ + from, to, getAngle(from, to)}); + edges.push_back((Edge){ + to, from, getAngle(to, from)}); + m = edges.size(); + G[from].push_back(m - 2); + G[to].push_back(m - 1); + } + void Build() + { + for (int i = 0; i < n; i++) + { + int sz = G[i].size(); + for (int j = 0; j < sz; j++) + { + for (int k = j + 1; k < sz; k++) + { + if (edges[G[i][j]].ang > edges[G[i][k]].ang) + { + swap(G[i][j], G[i][k]); + } + } + } + for (int j = 0; j < sz; j++) + { + prev[G[i][(j + 1) % sz]] = G[i][j]; + } + } + m = edges.size(); + face_cnt = 0; + memset(vis, 0, sizeof(vis)); + for (int k = 0; k < m; k++) + { + if (!vis[k]) + { + Poly poly; + int e = k; + for (;;) + { + vis[e] = 1; + left[e] = face_cnt; + int from = edges[e].from; + poly.push_back(Poi(x[from], y[from])); + e = prev[e ^ 1]; + if (e == k) + { + break; + } + assert(!vis[e]); + } + face_cnt++; + faces.push_back(poly); + } + } + for (int i = 0; i < face_cnt; i++) + { + area[i] = PolyArea(faces[i]); + } + } +}; + +PSLG<20010> g; +const int N = 110; + +Poi p[N]; +vector dist[N]; +vector V; + +inline int ID(const Poi &a) +{ + return lower_bound(V.begin(), V.end(), a) - V.begin(); +} + +int n; + +Poly simplify(const Poly &poly) +{ + Poly newpoly; + int n = poly.size(); + for (int i = 0; i < n; i++) + { + Poi a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; + if (dcmp(cross(c - b, c - a))) + { + newpoly.push_back(b); + } + } + return newpoly; +} + +int main() +{ + while (~scanf("%d", &n)) + { + for (int i = 0; i < n; i++) + { + p[i] = gPoi(); + dist[i].clear(); + } + V.assign(p, p + n); + p[n] = p[0]; + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + if (segprointer(p[i], p[i + 1], p[j], p[j + 1])) + { + Poi a = lineinter(p[i], p[i + 1] - p[i], p[j], p[j + 1] - p[j]); + dist[i].push_back(length(a - p[i])); + dist[j].push_back(length(a - p[j])); + V.push_back(a); + } + } + } + sort(V.begin(), V.end()); + V.erase(unique(V.begin(), V.end()), V.end()); + g.init(V.size()); + for (int i = 0; i < V.size(); i++)//!! + { + g.x[i] = V[i].x; + g.y[i] = V[i].y; + } + for (int i = 0; i < n; i++) + { + Vec v = p[i + 1] - p[i]; + double len = length(v); + v = v / length(v); + dist[i].push_back(0); + dist[i].push_back(len); + sort(dist[i].begin(), dist[i].end());//!! + for (int j = 1; j < dist[i].size(); j++) + { + Poi a = p[i] + v * dist[i][j - 1]; + Poi b = p[i] + v * dist[i][j]; + if (a == b) + { + continue; + } + g.AddEdge(ID(a), ID(b)); + } + } + g.Build(); + for (int i = 0; i < g.face_cnt; i++) + { + if (g.area[i] < 0) + { + Poly poly = g.faces[i]; + reverse(poly.begin(), poly.end()); + poly = simplify(poly); + int start = 0; + for (int i = 1; i < poly.size(); i++) + { + if (poly[i] < poly[start]) + { + start = i; + } + } + printf("%d\n", poly.size()); + for (int i = start; i < poly.size(); i++) + { + poly[i].out(); + } + for (int i = 0; i < start; i++) + { + poly[i].out(); + } + break; + } + } + } + return 0; +} diff --git a/1341.cpp b/1341.cpp new file mode 100644 index 0000000..19cd639 --- /dev/null +++ b/1341.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define pb push_back +const int N = 65537; + +queue qu; +bool vis[N]; +int pre[N], cnt[N]; +char val[N], s[N], str[N]; + +int n; +int anslen; + +bool bfs(int len) +{ + memset(vis, false, sizeof(vis)); + while (!qu.empty()) + { + qu.pop(); + } + for (int i = 0; i < len; i++) + if (s[i] != '0') + { + int cur = (s[i] - '0') % n; + if (!vis[cur]) + { + qu.push(cur); + val[cur] = s[i]; + cnt[cur] = 1; + pre[cur] = -1; + vis[cur] = true; + } + } + if (vis[0]) + { + return true; + } + while (!qu.empty()) + { + int cur = qu.front(); + qu.pop(); + if (anslen && cnt[cur] >= anslen) + { + return false; + } + for (int i = 0; i < len; i++) + { + int to = (cur * 10 + s[i] - '0') % n; + if (!vis[to]) + { + pre[to] = cur; + vis[to] = true; + val[to] = s[i]; + cnt[to] = cnt[cur] + 1; + if (to == 0) + { + return true; + } + qu.push(to); + } + } + } + return false; +} + +bool compare() +{ + for (int i = 0; i < anslen; i++) + { + if (s[i] < str[i]) + { + return true; + } + if (s[i] > str[i]) + { + return false; + } + } + return false; +} + +void Get() +{ + int cur = 0; + for (int i = cnt[0] - 1; i >= 0; i--) + { + s[i] = val[cur]; + cur = pre[cur]; + } +} + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + anslen = 0; + for (int i = 1; i < 10; i++) + { + s[0] = '0' + i; + if (bfs(1)) + { + Get(); + if (!anslen || anslen > cnt[0] || (anslen == cnt[0] && compare())) + { + anslen = cnt[0]; + strcpy(str, s); + } + } + } + if (anslen) + { + for (int i = 0; i < anslen; i++) + { + printf("%c", str[i]); + } + puts(""); + continue; + } + for (int i = 0; i < 10; i++) + { + for (int j = i + 1; j < 10; j++) + { + s[0] = i + '0'; + s[1] = j + '0'; + if (bfs(2)) + { + Get(); + if (!anslen || anslen > cnt[0] || (anslen == cnt[0] && compare())) + { + anslen = cnt[0]; + strcpy(str, s); + } + } + } + } + for (int i = 0; i < anslen; i++) + { + printf("%c", str[i]); + } + puts(""); + } + return 0; +} diff --git a/1342.cpp b/1342.cpp new file mode 100644 index 0000000..a30fc3f --- /dev/null +++ b/1342.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +const double eps = 1e-9; +const double pi = acos(-1.0); +const int maxn = 300 + 10; +int dblcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + return x > 0 ? 1 : -1; +} +inline double sqr(double x) +{ + return x * x; +} +struct Point2D +{ + double x, y; + Point2D() {} + Point2D(double a, double b) : x(a), y(b) {} + void input() + { + scanf("%lf%lf", &x, &y); + } + void output() + { + printf("%.3lf %.3lf\n", x, y); + } + Point2D operator+(const Point2D &a) const + { + return Point2D(x + a.x, y + a.y); + } + Point2D operator-(const Point2D &a) const + { + return Point2D(x - a.x, y - a.y); + } + Point2D operator*(const double &a) const + { + return Point2D(x * a, y * a); + } + Point2D operator/(const double &a) const + { + return Point2D(x / a, y / a); + } + bool operator==(const Point2D &a) const + { + return dblcmp(x - a.x) == 0 && dblcmp(y - a.y) == 0; + } + bool operator<(const Point2D &a) const + { + return dblcmp(x - a.x) < 0 || (dblcmp(x - a.x) == 0 && dblcmp(y - a.y) < 0); + } + double len() + { + return hypot(x, y); + } +}; +double det(const Point2D &a, const Point2D &b) +{ + return a.x * b.y - a.y * b.x; +} +double dot(const Point2D &a, const Point2D &b) +{ + return a.x * b.x + a.y * b.y; +} +double dist(const Point2D &a, const Point2D &b) +{ + return (a - b).len(); +} +Point2D rotate(const Point2D &p, double a) +{ + return Point2D(p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a)); +} +bool judgeinter(Point2D a, Point2D b, Point2D c, Point2D d) +{ + double d1, d2, d3, d4; + d1 = det(c - a, b - a); + d2 = det(d - a, b - a); + d3 = det(a - c, d - c); + d4 = det(b - a, d - c); + return dblcmp(d1 * d2) < 0 && dblcmp(d3 * d4) < 0; +} +Point2D getinter(Point2D p, Point2D v, Point2D q, Point2D w) +{ + Point2D u = p - q; + double t = det(w, u) / det(v, w); + return p + v * t; +} +bool onSeg(const Point2D &p, const Point2D &a1, const Point2D &a2) +{ + return dblcmp(det(a1 - p, a2 - p)) == 0 && dblcmp(dot(a1 - p, a2 - p)) < 0; +} +int n, kase = 0; +Point2D p[maxn], v[maxn * maxn]; +void solve() +{ + for (int i = 0; i < n; i++) + { + p[i].input(); + v[i] = p[i]; + } + n -= 1; + int c = n, e = n; + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (judgeinter(p[i], p[i + 1], p[j], p[j + 1])) + { + v[c++] = getinter(p[i], p[i + 1] - p[i], p[j], p[j + 1] - p[j]); + } + sort(v, v + c); + c = unique(v, v + c) - v; + for (int i = 0; i < c; i++) + for (int j = 0; j < n; j++) + if (onSeg(v[i], p[j], p[j + 1])) + { + e++; + } + printf("Case %d: There are %d pieces.\n", ++kase, e + 2 - c); +} +int main() +{ +#ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); +#endif + while (scanf("%d", &n) == 1 && n) + { + solve(); + } + return 0; +} diff --git a/1343.cpp b/1343.cpp new file mode 100644 index 0000000..36c4f6c --- /dev/null +++ b/1343.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define MAXN 50 +int a[MAXN + 10], maxd; +char ans[1000]; +int line[8][7] = + { + {0, 2, 6, 11, 15, 20, 22}, // A + {1, 3, 8, 12, 17, 21, 23}, // B + {10, 9, 8, 7, 6, 5, 4}, // C + {19, 18, 17, 16, 15, 14, 13},// D +}; +const int rev[8] = {5, 4, 7, 6, 1, 0, 3, 2}; //通过A,B,C,D,åÂÂ推其他方å‘。指å‘åÂÂå‘,用æ¥还原现场。 +const int final[8] = {6, 7, 8, 11, 12, 15, 16, 17};//最åÂŽç­”案è¦Â求的点 +bool ok() +{ + for (int i = 0; i < 8; i++) + if (a[final[i]] != a[final[0]]) + { + return false; + } + return true; +} +int dis(int k) +{ + int cnt = 0; + for (int i = 0; i < 8; i++) + if (a[final[i]] != k) + { + cnt++; + } + return cnt; +} +void move(int i) +{ + int tmp = a[line[i][0]]; + for (int j = 0; j < 6; j++) + { + a[line[i][j]] = a[line[i][j + 1]]; + } + a[line[i][6]] = tmp; +} +int h() +{ + return min(dis(1), min(dis(2), dis(3))); +} +void init()//Ã¥ÂÂ推方å‘ +{ + for (int i = 4; i < 8; i++) + for (int j = 0; j < 7; j++) + { + line[i][j] = line[rev[i]][6 - j]; + } +} +bool dfs(int cur, int maxd) +{ + if (ok()) + { + ans[cur] = 0; + printf("%s\n", ans); + return true; + } + if (cur + h() > maxd) + { + return false; + } + for (int i = 0; i < 8; i++) + { + ans[cur] = 'A' + i; + move(i); + if (dfs(cur + 1, maxd)) + { + return true; + } + move(rev[i]); + } + return false; +} +int main() +{ + for (int i = 4; i < 8; i++) + for (int j = 0; j < 7; j++) + { + line[i][j] = line[rev[i]][6 - j]; + } + while (scanf("%d", &a[0]) == 1 && a[0]) + { + for (int i = 1; i < 24; i++) + { + scanf("%d", &a[i]); + } + for (int i = 0; i < 24; i++) + if (!a[i]) + { + return 0; + } + if (ok()) + { + printf("No moves needed\n%d\n", a[6]); + continue; + } + for (maxd = 1;; maxd++) + if (dfs(0, maxd)) + { + break; + } + printf("%d\n", a[6]); + } +} diff --git a/1344.cpp b/1344.cpp new file mode 100644 index 0000000..d9102df --- /dev/null +++ b/1344.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +int n; +int a[1005], b[1005]; +int main() +{ + int i; + while (scanf("%d", &n) != EOF && n) + { + for (i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + for (i = 0; i < n; i++) + { + scanf("%d", &b[i]); + } + sort(a, a + n); + sort(b, b + n); + int star1, star2; + star1 = star2 = 0; + int end1, end2; + end1 = end2 = n - 1; + int sum = 0; + int num = n; + while (num) + { + if (a[end1] > b[end2]) + { + sum += 200; + end1--; + end2--; + } + else if (a[end1] < b[end2]) + { + sum -= 200; + star1++; + end2--; + } + else + { + if (a[star1] > b[star2]) + { + sum += 200; + star1++; + star2++; + } + else if (a[star1] < b[star2]) + { + sum -= 200; + star1++; + end2--; + } + else + { + if (a[star1] == b[end2]) + { + break; + } + else + { + sum -= 200; + star1++; + end2--; + } + } + } + num--; + } + printf("%d\n", sum); + } + return 0; +} diff --git a/1346.cpp b/1346.cpp new file mode 100644 index 0000000..9e52c83 --- /dev/null +++ b/1346.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +using std::sort; +const int max_song = (1 << 16) + 10; +int number_songs, quire; +double freq_count; + +struct song +{ + int id, len; + double freq; + bool operator<(const song &T) const + { + return len * (freq + T.freq) + T.len * T.freq < T.len * (freq + T.freq) + len * freq; + } +} songs[max_song]; +int main() +{ + // freopen("in.txt","r",stdin); + while (scanf("%d", &number_songs) != EOF) + { + freq_count = 0.0; + for (int i(0); i < number_songs; ++i) + { + scanf("%d%d%lf", &songs[i].id, &songs[i].len, &songs[i].freq); + freq_count += songs[i].freq; + } + scanf("%d", &quire); + sort(songs, songs + number_songs); + printf("%d\n", songs[quire - 1].id); + } + return 0; +} diff --git a/1347.cpp b/1347.cpp new file mode 100644 index 0000000..f7905c5 --- /dev/null +++ b/1347.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +/* ********************************************** +Author : Nero +Created Time: 2013/9/7 13:56:38 +Problem id : UVA 1347 +Problem Name: Tour +*********************************************** */ + +#define REP(i, a, b) for (int i = (a); i < (int)(b); i++) +#define clr(a, b) memset(a, b, sizeof(a)) + +const double Inf = 1e15; +double dp[55][55]; +int n; +double x[55], y[55]; + +double sqr(double x) { return x * x; } +double dis(int a, int b) +{ + return sqrt(sqr(x[a] - x[b]) + sqr(y[a] - y[b])); +} + +int main() +{ + while (~scanf("%d", &n)) + { + for (int i = 1; i <= n; i++) + scanf("%lf%lf", &x[i], &y[i]); + for (int i = 0; i <= n; i++) + for (int j = 0; j <= n; j++) + dp[i][j] = Inf; + dp[1][1] = 0.0; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= i; j++) + { + dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + dis(i, i + 1)); + dp[i + 1][i] = min(dp[i + 1][i], dp[i][j] + dis(j, i + 1)); + } + } + double ans = Inf; + for (int j = 1; j <= n; j++) + { + ans = min(ans, dp[n][j] + dis(n, j)); + } + printf("%.2lf\n", ans); + } + return 0; +} diff --git a/1349.cpp b/1349.cpp new file mode 100644 index 0000000..759e963 --- /dev/null +++ b/1349.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:16777216") + +typedef long long LL; +const int N = 105; + +int S, g[N][N], lx[N], ly[N], match[N], ans, slack[N]; +int res[N]; +bool fx[N], fy[N]; + +bool find(int x) +{ + fx[x] = 1; + for (int y = 1; y <= S; ++y) + { + if (fy[y]) + { + continue; + } + if (lx[x] + ly[y] == g[x][y]) + { + fy[y] = 1; + if (!match[y] || find(match[y])) + { + match[y] = x; + res[x] = y; + return 1; + } + } + else + { + slack[y] = min(slack[y], lx[x] + ly[y] - g[x][y]); + } + } + return 0; +} + +void update() +{ + int delta = 1 << 30, i; + for (i = 1; i <= S; ++i) + if (!fy[i]) + { + delta = min(delta, slack[i]); + } + for (i = 1; i <= S; ++i) + { + if (fx[i]) + { + lx[i] -= delta; + } + if (fy[i]) + { + ly[i] += delta; + } + slack[i] -= delta; + } +} + +void Kuhn_Munkras() +{ + int i, j; + for (i = 1; i <= S; ++i) + { + match[i] = lx[i] = ly[i] = 0; + for (j = 1; j <= S; ++j) + { + lx[i] = max(lx[i], g[i][j]); + } + } + for (i = 1; i <= S; ++i) + { + while (1) + { + for (j = 1; j <= S; ++j) + { + fx[j] = fy[j] = 0, slack[j] = 1 << 30; + } + if (find(i)) + { + break; + } + else + { + update(); + } + } + } +} +void work() +{ + int i, j, x; + for (i = 1; i <= S; ++i) + for (j = 1; j <= S; ++j) + { + g[i][j] = -1 << 29; + } + for (i = 1; i <= S; ++i) + { + while (scanf("%d", &x), x) + { + scanf("%d", &j); + g[i][x] = max(g[i][x], -j); + } + } + Kuhn_Munkras(); + int res = 0; + for (i = 1; i <= S; ++i) + { + if (g[match[i]][i] == -1 << 29) + { + break; + } + res += g[match[i]][i]; + } + if (i <= S) + { + puts("N"); + } + else + { + printf("%d\n", -res); + } +} + +int main() +{ + while (scanf("%d", &S), S) + { + work(); + } + return 0; +} diff --git a/135.cpp b/135.cpp new file mode 100644 index 0000000..35a7e6b --- /dev/null +++ b/135.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int k, rows[33][33]; + +inline int mv_row(int r, int idx) +{ + return rows[r][idx % (k - 1)]; +} + +int main() +{ + bool fst = true; + while (cin >> k) + { + if (fst) + { + fst = false; + } + else + { + printf("\n"); + } + int c = 0; + for (int i = 0; i < k - 1; ++i) + { + cout << ++c << ' '; + } + cout << ++c << endl; + for (int i = 0; i < k - 1; ++i) + for (int j = 0; j < k - 1; ++j) + { + rows[i + 1][j] = ++c; + } + for (int i = 0; i < k - 1; ++i) + { + cout << 1 << ' '; + for (int j = 0; j < k - 2; ++j) + { + cout << rows[i + 1][j] << ' '; + } + cout << rows[i + 1][k - 2] << endl; + } + for (int i = 0; i < k - 1; ++i) + { + for (int r = 0; r < k - 1; ++r) + { + cout << i + 2 << ' '; + for (int v = 1; v < k - 1; ++v) + { + cout << mv_row(v, r + i * (v - 1)) << ' '; + } + cout << mv_row(k - 1, r + i * (k - 2)) << endl; + } + } + } + return 0; +} diff --git a/1350.cpp b/1350.cpp new file mode 100644 index 0000000..94e70eb --- /dev/null +++ b/1350.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +typedef long long ll; +typedef vector vi; +typedef vector vl; +typedef vector vs; +typedef pair pii; +typedef vector vpi; +const double eps = 1e-7; +const int inf = 0x3f3f3f3f; +const int hinf = 0x3f3f3f3f; +const ll mod = 1000000007; + +#define out(x) cout << x << '\n'; +#define fio \ + freopen("input.txt", "r", stdin); \ + freopen("output.txt", "w", stdout); +#define fino freopen("input.txt", "r", stdin); +#define sz(a) sizeof(a) +#define mp make_pair +#define pb push_back +#define fi first +#define se second +#define ms(a, i) memset((a), (i), sz(a)) +#define clr(x) memset(x, 0, sz(x)) +#define cdp(x) memset((x), -1, sizeof(x)) +#define infi(x) memset(x, 0x3f, sz(x)) +#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) + +int cal0[60], cal1[60]; +int sum0[60], sum1[60]; +int ans[60]; + +int t; +int n, wei; + +int main() +{ + clr(sum0); + clr(sum1); + cal0[1] = cal1[1] = 1; + sum0[1] = sum1[1] = 1; + for (int i = 2; i <= 55; i++) + { + cal0[i] = cal0[i - 1] + cal1[i - 1]; + cal1[i] = cal0[i - 1]; + sum0[i] = cal0[i] + sum0[i - 1]; + sum1[i] = cal1[i] + sum1[i - 1]; + } + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + clr(ans); + for (int i = 0; i <= 55; i++) + { + if (sum1[i] < n) + { + wei = i + 1; + } + else + { + break; + } + } + ans[wei] = 1; + n -= sum1[wei - 1]; + int now = wei - 1; + while (n != 0) + { + int pos; + for (int i = 0; i <= 55; i++) + { + if (cal0[i] < n) + { + pos = i; + } + else + { + break; + } + } + if (pos > 0) + { + ans[pos] = 1; + } + else + { + break; + } + now = pos - 1; + n -= cal0[pos]; + } + for (int i = wei; i > 0; i--) + { + cout << ans[i]; + } + cout << endl; + } + return 0; +} diff --git a/1351.cpp b/1351.cpp new file mode 100644 index 0000000..61302c3 --- /dev/null +++ b/1351.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define MX 100005 +#define REP(i, n) for (int i = 0; i < (n); i++) +#define OREP(i, n) for (int i = 1; i <= (n); i++) + +typedef long long LL; +typedef unsigned long long ULL; +typedef unsigned int UINT; + +int n, m, k, t; + +char input[MX]; + +int dp[205][205]; +int nums[205]; + +bool check(int i, int j, int L) +{ + for (int k = 0; k < L; k++) + { + for (int l = i + k + L; l <= j; l += L) + { + if (input[l] != input[l - L]) + { + return false; + } + } + } + return true; +} +void go(int i, int j) +{ + int len = j - i + 1; + int ret = len; + for (int L = i; L < j; L++) + { + ret = min(ret, dp[i][L] + dp[L + 1][j]); + } + for (int L = 1; L <= len / 2; L++) + { + if (len % L == 0) + { + if (check(i, j, L)) + { + ret = min(ret, nums[len / L] + 2 + dp[i][i + L - 1]); + } + } + } + dp[i][j] = ret; +} +int main() +{ + for (int i = 0; i < 10; i++) + { + nums[i] = 1; + } + for (int i = 10; i < 100; i++) + { + nums[i] = 2; + } + for (int i = 100; i < 201; i++) + { + nums[i] = 3; + } + scanf("%d", &t); + REP(i, t) + { + scanf("%s", input); + int len = strlen(input); + memset(dp, 0x7f, sizeof dp); + REP(i, n) + dp[i][i] = 1; + for (int L = 1; L <= len; L++) + { + for (int i = 0; i < len; i++) + { + int j = i + L - 1; + if (j >= len) + { + break; + } + go(i, j); + } + } + printf("%d\n", dp[0][len - 1]); + } + return 0; +} diff --git a/1352.cpp b/1352.cpp new file mode 100644 index 0000000..cbf3bc1 --- /dev/null +++ b/1352.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +#define esp 1e-6 +#define LL long long +#define inf 0x0f0f0f0f +#define maxn 4 + +const int dice24[24][6] = + { + {2, 1, 5, 0, 4, 3}, {2, 0, 1, 4, 5, 3}, {2, 4, 0, 5, 1, 3}, {2, 5, 4, 1, 0, 3}, {4, 2, 5, 0, 3, 1}, {5, 2, 1, 4, 3, 0}, {1, 2, 0, 5, 3, 4}, {0, 2, 4, 1, 3, 5}, {0, 1, 2, 3, 4, 5}, {4, 0, 2, 3, 5, 1}, {5, 4, 2, 3, 1, 0}, {1, 5, 2, 3, 0, 4}, {5, 1, 3, 2, 4, 0}, {1, 0, 3, 2, 5, 4}, {0, 4, 3, 2, 1, 5}, {4, 5, 3, 2, 0, 1}, {1, 3, 5, 0, 2, 4}, {0, 3, 1, 4, 2, 5}, {4, 3, 0, 5, 2, 1}, {5, 3, 4, 1, 2, 0}, {3, 4, 5, 0, 1, 2}, {3, 5, 1, 4, 0, 2}, {3, 1, 0, 5, 4, 2}, {3, 0, 4, 1, 5, 2}, +}; +int n, dice[maxn][6], ans; +vector names; +int id(char *name) +{ + string s(name); + int n = names.size(); + for (int i = 0; i < n; i++) + if (s == names[i]) + { + return i; + } + names.push_back(s); + return n; +} +int r[maxn], color[maxn][6]; +void check() +{ + for (int i = 0; i < n; i++) + for (int j = 0; j < 6; j++) + { + color[i][dice24[r[i]][j]] = dice[i][j]; + } + int tot = 0; + for (int j = 0; j < 6; j++) + { + int cnt[maxn * 6]; + memset(cnt, 0, sizeof(cnt)); + int max1 = 0; + for (int i = 0; i < n; i++) + { + max1 = max(max1, ++cnt[color[i][j]]); + } + tot += n - max1; + } + ans = min(ans, tot); +} +void dfs(int d) +{ + if (d == n) + { + check(); + } + else + { + for (int i = 0; i < 24; i++) + { + r[d] = i; + dfs(d + 1); + } + } +} +int main() +{ + while (scanf("%d", &n) != EOF) + { + if (n == 0) + { + break; + } + names.clear(); + for (int i = 0; i < n; i++) + for (int j = 0; j < 6; j++) + { + char ss[30]; + scanf("%s", ss); + dice[i][j] = id(ss); + } + ans = n * 6; + r[0] = 0; + dfs(1); + printf("%d\n", ans); + } + return 0; +} diff --git a/1354.cpp b/1354.cpp new file mode 100644 index 0000000..030ebed --- /dev/null +++ b/1354.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +struct node +{ + double L, R; + node() : L(0), R(0) {} +}; +const int N = 6; +int vis[1 << N], n; +double w[N], sum[1 << N], r; +vector tree[1 << N]; +void dfs(int subset) +{ + if (vis[subset]) + { + return; + } + vis[subset] = true; + bool have_child = false; + for (int left = (subset - 1) & subset; left; left = (left - 1) & subset) + { + have_child = true; + int right = subset ^ left; + double d1 = sum[right] / sum[subset]; + double d2 = sum[left] / sum[subset]; + dfs(left); + dfs(right); + for (int i = 0; i < tree[left].size(); i++) + { + for (int j = 0; j < tree[right].size(); j++) + { + node t; + t.L = max(tree[left][i].L + d1, tree[right][j].L - d2); + t.R = max(tree[right][j].R + d2, tree[left][i].R - d1); + if (t.L + t.R < r) + { + tree[subset].push_back(t); + } + } + } + } + if (!have_child) + { + tree[subset].push_back(node()); + } +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%lf%d", &r, &n); + for (int i = 0; i < n; i++) + { + scanf("%lf", w + i); + } + int tot = 1 << n; + for (int i = 0; i < tot; i++) + { + sum[i] = 0; + tree[i].clear(); + for (int j = 0; j < n; j++) + if (i & (1 << j)) + { + sum[i] += w[j]; + } + } + tot--; + memset(vis, 0, sizeof(vis)); + dfs(tot); + double ans = -1; + for (int i = 0; i < tree[tot].size(); i++) + { + ans = max(ans, tree[tot][i].L + tree[tot][i].R); + } + printf("%.10lf\n", ans); + } + return 0; +} diff --git a/1356.cpp b/1356.cpp new file mode 100644 index 0000000..2cdf8f6 --- /dev/null +++ b/1356.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +/* 自适应辛普森 UVA 1356 + * 二分求解高度 求解抛物线的长度是ä¸æ˜¯åˆé€‚ + * + * */ + +const int INF = ~0u >> 1; +typedef pair P; +#define MID(x, y) ((x + y) >> 1) +#define iabs(x) ((x) > 0 ? (x) : -(x)) +#define REP(i, a, b) for (int i = (a); i < (b); i++) +#define FOR(i, a, b) for (int i = (a); i <= (b); i++) +#define pb push_back +#define mp make_pair +#define print() cout << "--------" << endl + +// 这里为了方便,把a声明æˆå…¨å±€çš„。 +// è¿™ä¸æ˜¯ä¸€ä¸ªå¥½çš„编程习惯,但在本题中å´å¯ä»¥æ高代ç çš„å¯è¯»æ€§ +double a; + +//simpson å…¬å¼ç”¨çš„函数 (这个需è¦æ ¹æ®é¢˜ç›®è€Œæ”¹å˜å‡½æ•°) +double F(double x) +{ + return sqrt(1 + 4 * a * a * x * x); +} + +//三点simpson法,这里è¦æ±‚F是一个全局函数 +double simpson(double a, double b) +{ + double c = a + (b - a) / 2; + return (F(a) + 4 * F(c) + F(b)) * (b - a) / 6; +} + +//自适应simpsonå…¬å¼(递归)。已知整个区间[a,b]上三点的simpson值A +double asr(double a, double b, double eps, double A) +{ + double c = a + (b - a) / 2; + double L = simpson(a, c), R = simpson(c, b); + if (fabs(L + R - A) <= 15 * eps) + return L + R + (L + R - A) / 15.0; + return asr(a, c, eps / 2, L) + asr(c, b, eps / 2, R); +} + +//自适应simpsonå…¬å¼(主过程) +double asr(double a, double b, double eps) +{ + return asr(a, b, eps, simpson(a, b)); +} + +//下é¢è¿™ä¸ªå‡½æ•°ä¾é¢˜é¢è€Œå¼‚,本题求解simpson求解宽度为w,高度为h的抛物线长度 +double solve(double w, double h) +{ + a = 4.0 * h / (w * w);//修改全局å˜é‡a,从而改å˜F的行为 + return asr(0, w / 2, 1e-5) * 2; +} + +int main() +{ + int t, cas = 0; + scanf("%d", &t); + while (cas++ < t) + { + int D, H, B, L; + scanf("%d%d%d%d", &D, &H, &B, &L); + int n = (B + D - 1) / D; + double D1 = (double)B / n; + double L1 = (double)L / n; + double x = 0, y = H; + while (y - x > 1e-5) + {//二分求解高度 + double m = x + (y - x) / 2; + if (solve(D1, m) < L1) + x = m; + else + y = m; + } + if (cas > 1) + puts(""); + printf("Case %d:\n%.2f\n", cas, H - x); + } + + return 0; +} diff --git a/1357.cpp b/1357.cpp new file mode 100644 index 0000000..2760f37 --- /dev/null +++ b/1357.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +const int maxn = 300000 + 10; + +struct Edge +{ + int to, next; +} edge[maxn]; + +int head[maxn], dfn[maxn], B[maxn << 2], F[maxn << 2], d[maxn << 2][20], pos[maxn << 2]; +int E, Time, tot; + +void newedge(int u, int to) +{ + edge[E].to = to; + edge[E].next = head[u]; + head[u] = E++; +} + +void init(int n) +{ + for (int i = 0; i <= n; i++) + { + head[i] = -1; + dfn[i] = 0; + } + E = Time = tot = 0; +} + +void rmq_init(int n) +{ + for (int i = 1; i <= n; i++) + { + d[i][0] = B[i]; + } + for (int j = 1; (1 << j) <= n; j++) + for (int i = 1; i + j - 1 <= n; i++) + { + d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); + } +} + +int rmq(int L, int R) +{ + int k = 0; + while ((1 << (k + 1)) <= R - L + 1) + { + k++; + } + return min(d[L][k], d[R - (1 << k) + 1][k]); +} + +int lca(int a, int b) +{ + if (pos[a] > pos[b]) + { + swap(a, b); + } + int ans = rmq(pos[a], pos[b]); + return F[ans]; +} + +int st[maxn]; +void dfs() +{ + int top = 0; + st[++top] = 0; + while (top) + { + int u = st[top]; + if (!dfn[u]) + { + dfn[u] = ++Time; + F[Time] = u; + pos[u] = tot + 1; + } + B[++tot] = dfn[u]; + bool flag = 0; + for (int i = head[u]; i != -1; i = edge[i].next) + { + int to = edge[i].to; + if (!dfn[to]) + { + flag = 1; + st[++top] = to; + break; + } + } + if (!flag) + { + top--; + } + } +} + +int c[maxn], n; + +int bin(int l, int r, int val) +{ + while (l < r) + { + int mid = (l + r) / 2; + if (c[mid] >= val) + { + r = mid; + } + else + { + l = mid + 1; + } + } + return r; +} + +void solve() +{ + dfs(); + rmq_init(tot); + int m, u, to; + scanf("%d", &m); + while (m--) + { + scanf("%d%d", &u, &to); + if (u >= n) + { + puts("No"); + continue; + } + if (u == to) + { + puts("No"); + continue; + } + int k = bin(0, n - 1, to); + if (lca(u, k) == u) + { + puts("Yes"); + } + else + { + puts("No"); + } + } +} + +int main() +{ + int t, m, cas = 1; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + init(n); + for (int i = 0; i < n; i++) + { + scanf("%d", &c[i]); + } + for (int i = 1; i < n; i++) + { + c[i] += c[i - 1]; + } + for (int i = 1; i < n; i++) + { + int k = bin(0, n - 1, i); + newedge(k, i); + } + printf("Case %d:\n", cas++); + solve(); + if (t) + { + puts(""); + } + } + return 0; +} diff --git a/1358.cpp b/1358.cpp new file mode 100644 index 0000000..0f77b31 --- /dev/null +++ b/1358.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXL = 100; +const int C = 26; + +LL mat[MAXL][MAXL]; +int N; + +struct automata +{ + int go[MAXL][C], fail[MAXL], val[MAXL], id[MAXL]; + int sz; + int New_Node() + { + memset(go[sz], 0, sizeof(go[sz])); + val[sz] = 0; + fail[sz] = 0; + return sz++; + } + void init() + { + sz = 0; + New_Node(); + } + void insert(char s[]) + { + int now = 0; + for (int i = 0, o; s[i]; ++i, now = go[now][o]) + { + o = s[i] - 'A'; + if (go[now][o] == 0) + go[now][o] = New_Node(); + } + val[now] = 1; + } + void get_fail() + { + queue q; + while (!q.empty()) + q.pop(); + q.push(0); + while (!q.empty()) + { + int x = q.front(); + q.pop(); + for (int i = 0; i < N; ++i) + { + int y = go[x][i]; + if (!y) + go[x][i] = go[fail[x]][i]; + else + q.push(y); + if (x && y) + { + fail[y] = go[fail[x]][i]; + val[y] |= val[fail[y]]; + } + } + } + } + void get_matrix(int &n) + { + memset(id, 0, sizeof(id)); + n = 0; + for (int i = 0; i < sz; ++i) + { + if (!val[i]) + id[i] = n++; + } + memset(mat, 0, sizeof(mat)); + for (int i = 0; i < sz; ++i) + { + if (val[i]) + continue; + mat[id[i]][n] = -N; + mat[id[i]][id[i]] = -N; + for (int j = 0; j < N; ++j) + { + int k = go[i][j]; + if (val[k]) + continue; + mat[id[i]][id[k]] += 1; + } + } + } +}; + +LL gcd(LL a, LL b) +{ + return b ? gcd(b, a % b) : a; +} + +void gauss(int n) +{ + for (int r = 0; r < n; ++r) + { + int id = r; + for (int i = r; i < n; ++i) + { + if (abs(mat[id][r]) > abs(mat[i][r]) && mat[i][r]) + id = i; + } + for (int i = 0; i <= n; ++i) + swap(mat[r][i], mat[id][i]); + for (int i = r + 1; i < n; ++i) + { + if (mat[i][r] != 0) + { + LL g = gcd(abs(mat[i][r]), abs(mat[r][r])); + LL lcm = mat[i][r] * mat[r][r] / g; + LL l1 = lcm / mat[i][r], l2 = lcm / mat[r][r]; + for (int j = r; j <= n; ++j) + mat[i][j] = mat[i][j] * l1 - mat[r][j] * l2; + } + } + } + for (int i = n - 1; i >= 0; --i) + { + for (int j = i + 1; j < n; ++j) + { + mat[i][n] -= mat[i][j] * mat[j][j]; + } + mat[i][i] = mat[i][n] / mat[i][i]; + } +} + +int main() +{ + int T, n; + scanf("%d", &T); + for (int cas = 1; cas <= T; ++cas) + { + char st[100]; + scanf("%d%s", &N, st); + automata AC; + AC.init(); + AC.insert(st); + AC.get_fail(); + AC.get_matrix(n); + gauss(n); + printf("Case %d:\n%lld\n", cas, mat[0][0]); + if (cas < T) + puts(""); + } + return 0; +} diff --git a/136.cpp b/136.cpp index 78565f8..4978b0b 100644 --- a/136.cpp +++ b/136.cpp @@ -1,35 +1,28 @@ -#include - -using namespace std; - -int primes(int j); - -int main() -{ - int ugly[1500],primes[1500],k=0,j=0; - for(k=0;k<1500;k++) - ugly[k]=0; - while(ugly[1500]==0) - { - ugly[0]=1; - k=1; - j=2; - primes(j) - if(j%2==0 || j%3==0 || j%5==0) - { - ugly[k]=j; - k++; - j++; - } - else - j++; - } - - cout<<"The 1500'th ugly number is "<>k; return 0; -} - -primes(int j) -{ - for(int p=2;p + +using namespace std; + +int main() +{ + priority_queue, greater> ugly; + ugly.push(2); + ugly.push(3); + ugly.push(5); + int cnt = 1, curr = 1; + while (cnt < 1500) + { + int tmp = 0; + while (tmp <= curr) + { + tmp = ugly.top(); + ugly.pop(); + } + ugly.push(2 * tmp); + ugly.push(3 * tmp); + ugly.push(5 * tmp); + curr = tmp; + cnt++; + } + printf("The 1500'th ugly number is %d.\n", curr); + return 0; +} diff --git a/1361.cpp b/1361.cpp new file mode 100644 index 0000000..1d5efca --- /dev/null +++ b/1361.cpp @@ -0,0 +1,227 @@ +#include + +using namespace std; + +#define inf 2139062143 +#define Inf 0x3FFFFFFFFFFFFFFFLL +#define eps 1e-9 +#define pi acos(-1.0) + +typedef long long ll; +const int maxn = 20000 + 10; + +struct Edge +{ + int u, v; +}; +vector G[maxn], bcc[maxn]; +int pre[maxn], bccno[maxn], dfs_clock, bcc_cnt; +int parents[maxn], sum; +bool flag; +stack S; +struct BigInt +{ + int num[7000]; + int len; + void clear() + { + for (int i = 0; i < 7000; ++i) + { + num[i] = 0; + } + len = 0; + } + void getBigInt(ll n) + { + clear(); + if (n == 0) + { + num[len++] = 0; + } + while (n != 0) + { + num[len++] = n % 10; + n /= 10; + } + } + void getBigInt(int n) + { + ll m = (ll)n; + getBigInt(m); + } + BigInt operator*(const BigInt a) const + { + int m = a.len; + int n = len; + BigInt c; + c.clear(); + int bs = 0, w = 0, jinwei = 0, tp; + for (int i = 0; i < m; ++i) + { + jinwei = 0; + w = 0; + for (int j = 0; j < n; ++j) + { + tp = a.num[i] * num[j] + jinwei + c.num[bs + w]; + c.num[bs + w] = tp % 10; + jinwei = tp / 10; + w++; + } + while (jinwei != 0) + { + c.num[bs + w] = jinwei % 10; + jinwei /= 10; + w++; + } + c.len = max(c.len, bs + w); + bs++; + } + return c; + } +}; +int Find(int x) +{ + return x == parents[x] ? parents[x] : parents[x] = Find(parents[x]); +} +void Uion(int x, int y) +{ + int a = Find(x); + int b = Find(y); + if (a != b) + { + sum++; + parents[b] = a; + } +} +int dfs(int u, int fa) +{ + int cnt = 0; + int lowu = pre[u] = ++dfs_clock; + for (int i = 0; i < G[u].size(); ++i) + { + int v = G[u][i]; + Edge e = (Edge){ + u, v}; + if (!pre[v]) + { + S.push(e); + int lowv = dfs(v, u); + lowu = min(lowu, lowv); + if (lowv < pre[u]) + { + cnt++; + } + if (lowv >= pre[u]) + { + bcc_cnt++; + bcc[bcc_cnt].clear(); + while (true) + { + Edge x = S.top(); + S.pop(); + if (bccno[x.u] != bcc_cnt) + { + bcc[bcc_cnt].push_back(x.u); + bccno[x.u] = bcc_cnt; + } + if (bccno[x.v] != bcc_cnt) + { + bcc[bcc_cnt].push_back(x.v); + bccno[x.v] = bcc_cnt; + } + if (x.u == u && x.v == v) + { + break; + } + } + } + } + else if (pre[v] < pre[u] && v != fa) + { + S.push(e); + lowu = min(lowu, pre[v]); + } + } + if (cnt >= 2) + { + flag = false; + } + return lowu; +} +void find_bcc(int n) +{ + memset(pre, 0, sizeof(pre)); + memset(bccno, 0, sizeof(bccno)); + dfs_clock = bcc_cnt = 0; + dfs(1, -1); +} +BigInt slove(int n) +{ + BigInt ans; + ans.getBigInt(0); + flag = true; + find_bcc(n); + if (!flag) + { + return ans; + } + ans.getBigInt(1); + BigInt tmp; + for (int i = 1; i <= bcc_cnt; ++i) + { + int z = bcc[i].size(); + tmp.getBigInt(z + 1); + if (z >= 3) + { + ans = ans * tmp; + } + } + return ans; +} + +int main() +{ + int n, m, k, tcase = 0; + while (~scanf("%d%d", &n, &m)) + { + if (tcase++) + { + printf("\n"); + } + int a, b; + for (int i = 0; i <= n; ++i) + { + parents[i] = i; + G[i].clear(); + } + sum = 0; + while (m--) + { + scanf("%d", &k); + scanf("%d", &a); + for (int i = 1; i < k; ++i) + { + scanf("%d", &b); + G[a].push_back(b); + G[b].push_back(a); + Uion(a, b); + a = b; + } + } + BigInt ans; + if (sum + 1 != n) + { + ans.getBigInt(0); + } + else + { + ans = slove(n); + } + for (int i = ans.len - 1; i >= 0; --i) + { + printf("%d", ans.num[i]); + } + printf("\n"); + } + return 0; +} diff --git a/1362.cpp b/1362.cpp new file mode 100644 index 0000000..c6f63bb --- /dev/null +++ b/1362.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +const long long MOD = 1000000000; + +long long dp[330][330]; +char s[330]; + +long long solve(int pos1, int pos2) +{ + if (pos1 > pos2) + { + return 0; + } + if (pos1 == pos2) + { + return 1; + } + if (dp[pos1][pos2] != -1) + { + return dp[pos1][pos2]; + } + long long ans = 0; + for (int i = pos1 + 2; i <= pos2; i++) + { + if (s[pos1] == s[i] && s[i] == s[pos2]) + { + ans = (ans + solve(pos1 + 1, i - 1) * solve(i, pos2)) % MOD; + } + } + return dp[pos1][pos2] = ans; +} + +int main() +{ + while (scanf("%s", s) != EOF) + { + memset(dp, -1, sizeof(dp)); + cout << solve(0, strlen(s) - 1) << endl; + } + return 0; +} diff --git a/1363.cpp b/1363.cpp new file mode 100644 index 0000000..f15ac26 --- /dev/null +++ b/1363.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +/* ********************************************** +Author : JayYe +Created Time: 2013-9-13 15:24:23 +File Name : JayYe.cpp +*********************************************** */ + +#define LL long long + +LL cal(int l, int r) +{ + return (LL)(r - l + 1) * (l + r) / 2; +} + +int main() +{ + int n, k; + while (scanf("%d%d", &n, &k) != -1) + { + LL ans = 0; + for (int i = 1; i <= n && i <= k; i++) + { + int tmp = k / i; + int then = k / tmp; + if (then >= n) + { + int num = n - i + 1; + ans += (LL)num * k - tmp * cal(i, n); + break; + } + int num = then - i + 1; + ans += (LL)num * k - tmp * cal(i, then); + i = then; + } + if (n > k) + ans += (LL)k * (n - k); + printf("%lld\n", ans); + } + return 0; +} diff --git a/1364.cpp b/1364.cpp new file mode 100644 index 0000000..93c5c32 --- /dev/null +++ b/1364.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +#define esp 1e-6 +#define pb push_back +#define mp(a, b) make_pair((a), (b)) +#define in freopen("in.txt", "r", stdin); +#define out freopen("out.txt", "w", stdout); +#define print(a) printf("%d\n", (a)); +#define bug puts("********))))))"); +#define stop system("pause"); +#define Rep(i, c) for (__typeof(c.end()) i = c.begin(); i != c.end(); i++) +#define inf 0x0f0f0f0f +#pragma comment(linker, "/STACK:102400000,102400000") + +typedef long long LL; +typedef vector VI; +typedef pair pii; +typedef vector VII; +typedef vector::iterator IT; + +const int maxn = 1111; +const int maxm = 1000100; +int pre[maxn], iscut[maxn], bccno[maxn], bcc_cnt, dfs_clock; +int odd[maxn], color[maxn], low[maxn]; +VI g[maxn], bcc[maxn]; +struct Edge +{ + int u, v; + Edge(int u, int v) : u(u), v(v) {} +}; +stack S; +bool bipartite(int u, int b) +{ + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (bccno[v] != b) + continue; + if (color[v] == color[u]) + return false; + if (!color[v]) + { + color[v] = 3 - color[u]; + if (!bipartite(v, b)) + return false; + } + } + return true; +} +int dfs(int u, int fa) +{ + int lowu = pre[u] = ++dfs_clock; + int child = 0; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + Edge e = Edge(u, v); + if (!pre[v]) + { + S.push(e); + child++; + int lowv = dfs(v, u); + lowu = min(lowu, lowv); + if (lowv >= pre[u]) + { + iscut[u] = 1; + bcc_cnt++; + bcc[bcc_cnt].clear(); + for (;;) + { + Edge x = S.top(); + S.pop(); + if (bccno[x.u] != bcc_cnt) + { + bcc[bcc_cnt].pb(x.u); + bccno[x.u] = bcc_cnt; + } + if (bccno[x.v] != bcc_cnt) + { + bcc[bcc_cnt].pb(x.v); + bccno[x.v] = bcc_cnt; + } + if (x.u == u && x.v == v) + break; + } + } + } + else if (pre[v] < pre[u] && v != fa) + { + S.push(e); + lowu = min(lowu, pre[v]); + } + } + if (child == 1 && fa < 0) + iscut[u] = 0; + return low[u] = lowu; +} +void find_bcc(int n) +{ + memset(pre, 0, sizeof(pre)); + memset(iscut, 0, sizeof(iscut)); + memset(bccno, 0, sizeof(bccno)); + dfs_clock = bcc_cnt = 0; + for (int i = 0; i < n; i++) + if (!pre[i]) + dfs(i, -1); +} +int A[maxn][maxn]; +int main(void) +{ + int n, m; + while (scanf("%d%d", &n, &m) == 2 && n) + { + memset(A, 0, sizeof(A)); + for (int i = 0; i < maxn; i++) + g[i].clear(); + for (int i = 0; i < m; i++) + { + int u, v; + scanf("%d%d", &u, &v); + u--, v--; + A[u][v] = A[v][u] = 1; + } + for (int u = 0; u < n; u++) + { + for (int v = u + 1; v < n; v++) + if (!A[u][v]) + g[u].pb(v), g[v].pb(u); + } + memset(odd, 0, sizeof(odd)); + + find_bcc(n); + for (int i = 1; i <= bcc_cnt; i++) + { + memset(color, 0, sizeof(color)); + memset(bccno, 0, sizeof(bccno)); + for (int j = 0; j < bcc[i].size(); j++) + bccno[bcc[i][j]] = i; + int v = bcc[i][0]; + color[v] = 1; + if (!bipartite(v, i)) + for (int j = 0; j < bcc[i].size(); j++) + odd[bcc[i][j]] = 1; + } + int ans = n; + for (int i = 0; i < n; i++) + if (odd[i]) + ans--; + printf("%d\n", ans); + } + return 0; +} diff --git a/1365.cpp b/1365.cpp new file mode 100644 index 0000000..303dc8d --- /dev/null +++ b/1365.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +#define lson (rt << 1) +#define rson (rt << 1 | 1) +#define mid ((l + r) >> 1) + +typedef long long LL; +const int MAXN = 100000 + 10; + +struct Node +{ + int l, r, rm, mx, cov; + LL sum; + void MarkCover(int v) + { + rm = mx = v; + cov = 1; + sum = (LL)v * (r - l); + } +} T[MAXN << 2]; + +struct Cube +{ + int x, y, z; + bool operator<(const Cube &rhs) const + { + return z > rhs.z; + } + void read() + { + scanf("%d%d%d", &x, &y, &z); + } +} cube[MAXN]; + +void build(int rt, int l, int r) +{ + T[rt].l = l; + T[rt].r = r; + T[rt].sum = T[rt].rm = T[rt].mx = T[rt].cov = 0; + if (l + 1 == r) + return; + build(lson, l, mid); + build(rson, mid, r); +} + +inline void Update(int rt) +{ + T[rt].rm = T[rson].rm; + T[rt].sum = T[lson].sum + T[rson].sum; + T[rt].mx = max(T[lson].mx, T[rson].mx); +} + +inline void PushDown(int rt) +{ + if (T[rt].cov) + { + T[lson].MarkCover(T[rt].mx); + T[rson].MarkCover(T[rt].mx); + T[rt].cov = 0; + } +} + +void Modify(int rt, int l, int r, int L, int R, int v) +{ + if (T[rt].rm >= v) + return; + if (L <= l && R >= r && T[rt].mx <= v) + { + T[rt].MarkCover(v); + return; + } + PushDown(rt); + if (L < mid) + Modify(lson, l, mid, L, R, v); + if (R > mid) + Modify(rson, mid, r, L, R, v); + Update(rt); +} + +int main() +{ + int N, M; + while (scanf("%d%d", &N, &M) == 2 && N) + { + build(1, 0, M); + for (int i = 0; i < N; ++i) + cube[i].read(); + sort(cube, cube + N); + cube[N].z = 0; + LL ret = 0; + for (int i = 0; i < N; ++i) + { + if (cube[i].x) + Modify(1, 0, M, 0, cube[i].x, cube[i].y); + ret += T[1].sum * (cube[i].z - cube[i + 1].z); + } + printf("%lld\n", (LL)M * M * M - ret); + } + return 0; +} diff --git a/1366.cpp b/1366.cpp new file mode 100644 index 0000000..5772dbd --- /dev/null +++ b/1366.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:1024000000,1024000000") + +typedef long long ll; +typedef pair pii; +#define pb(a) push_back(a) +#define INF 0x1f1f1f1f +#define lson idx << 1, l, mid +#define rson idx << 1 | 1, mid + 1, r +#define PI 3.1415926535898 +template +T min(const T &a, const T &b, const T &c) +{ + return min(min(a, b), min(a, c)); +} +template +T max(const T &a, const T &b, const T &c) +{ + return max(max(a, b), max(a, c)); +} +void debug() +{ +#ifdef ONLINE_JUDGE +#else + freopen("d:\\in.txt", "r", stdin); + freopen("d:\\out1.txt", "w", stdout); +#endif +} +int getch() +{ + int ch; + while ((ch = getchar()) != EOF) + { + if (ch != ' ' && ch != ' ') + { + return ch; + } + } + return EOF; +} +int n, m; +int a[550][550], b[550][550]; +int dp[550][550]; +int f(int k, int maxl) +{ + if (dp[k][maxl] >= 0) + { + return dp[k][maxl]; + } + if (k > n) + { + return 0; + } + int num = 0; + int maxx = -1; + for (int i = 0; i <= m; i++) + { + num += a[k][i]; + if (i >= maxl) + { + maxx = max(maxx, f(k + 1, max(i, maxl)) + num + b[k][i + 1]); + } + } + return dp[k][maxl] = maxx; +} +int main() +{ + while (scanf("%d%d", &n, &m) != EOF && (n || m)) + { + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + { + scanf("%d", &a[i][j]); + } + for (int i = 1; i <= n; i++) + for (int j = 1; j <= m; j++) + { + scanf("%d", &b[i][j]); + } + for (int i = 1; i <= n; i++) + { + b[i][m + 1] = 0; + } + for (int i = 1; i <= n; i++) + for (int j = m - 1; j >= 1; j--) + { + b[i][j] += b[i][j + 1]; + } + memset(dp, -1, sizeof(dp)); + printf("%d\n", f(1, 0)); + } + return 0; +} diff --git a/1367.cpp b/1367.cpp new file mode 100644 index 0000000..f3b441c --- /dev/null +++ b/1367.cpp @@ -0,0 +1,283 @@ +#include + +using namespace std; + +// LA3532/UVa1367 Nuclear Plants +// Rujia Liu + +const double eps = 5 * 1e-13; +int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} + +const double PI = acos(-1); +const double TWO_PI = PI * 2; + +double NormalizeAngle(double rad, double center = PI) +{ + return rad - TWO_PI * floor((rad + PI - center) / TWO_PI); +} + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; + +typedef Point Vector; + +Vector operator+(Vector A, Vector B) +{ + return Vector(A.x + B.x, A.y + B.y); +} +Vector operator-(Point A, Point B) +{ + return Vector(A.x - B.x, A.y - B.y); +} +Vector operator*(Vector A, double p) +{ + return Vector(A.x * p, A.y * p); +} +Vector operator/(Vector A, double p) +{ + return Vector(A.x / p, A.y / p); +} + +// ç†论上这个“å°Â于â€Âè¿Â算符是错的,因为å¯能有三个点a, b, c, a和b很接近(å³a &rad) +{ + double d = Length(c1 - c2); + if (dcmp(d) == 0) + { + return;// ä¸Â管是内å«还是é‡Âåˆ,都ä¸Â相交 + } + if (dcmp(r1 + r2 - d) < 0) + { + return; + } + if (dcmp(fabs(r1 - r2) - d) > 0) + { + return; + } + double a = angle(c2 - c1); + double da = acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d)); + rad.push_back(NormalizeAngle(a - da)); + rad.push_back(NormalizeAngle(a + da)); +} + +Point GetLineProjection(Point P, Point A, Point B) +{ + Vector v = B - A; + return A + v * (Dot(v, P - A) / Dot(v, v)); +} + +// 直线AB和圆心为C,åŠ径为r的圆的交点。相对于圆的æžÂ角ä¿Â存在rad中 +void getLineCircleIntersection(Point A, Point B, Point C, double r, vector &rad) +{ + Point p = GetLineProjection(C, A, B); + double a = angle(p - C); + double d = Length(p - C); + if (dcmp(d - r) > 0) + { + return; + } + if (dcmp(d) == 0)// 过圆心 + { + rad.push_back(NormalizeAngle(angle(A - B))); + rad.push_back(NormalizeAngle(angle(B - A))); + } + double da = acos(d / r); +} + +/////////// 题目相关 +const int maxn = 200 + 5; +int n, N, M;// n是圆的总数,N和M是场地长宽 +Point P[maxn]; +double R[maxn]; + +// Ã¥ÂЌϠno弧度为rad的点 +Point getPoint(int no, double rad) +{ + return Point(P[no].x + cos(rad) * R[no], P[no].y + sin(rad) * R[no]); +} + +// 第no个圆弧度为rad的点是å¦å¯è§Â。相åŒ的圆åª有编å·最å°Â的å¯è§Â(虽然对于本题æ¥说ä¸Â必è¦Â) +bool visible(int no, double rad) +{ + Point p = getPoint(no, rad); + if (p.x < 0 || p.y < 0 || p.x > N || p.y > M) + { + return false; + } + for (int i = 0; i < n; i++) + { + if (P[no] == P[i] && dcmp(R[no] - R[i]) == 0 && i < no) + { + return false; + } + if (dcmp(Length(p - P[i]) - R[i]) < 0) + { + return false; + } + } + return true; +} + +// 场地边界上的点p是å¦å¯觠+bool visible(Point p) +{ + for (int i = 0; i < n; i++) + { + if (dcmp(Length(p - P[i]) - R[i]) <= 0) + { + return false; + } + } + return true; +} + +// 求圆的并在(0,0)-(N,M)内的é¢积 +// 使用一般曲边图形的é¢积算法。下文中,“所求图形â€Â指的是ä¸Â能ç§Âèœ的区域,它的边界由圆弧和直线段构æˆÂ。 +// 算法:对于所求图形边界上的æ¯Â一段(å¯以是曲线)a~>b,累加Cross(a, b)和它在直线段a->bå³边部分的é¢积(左边部分算负) +// 边界计算: +// 1. æ¯Â个圆被其他圆和场地边界分æˆÂ了若干æ¡圆弧,中点ä¸Â被其他圆覆盖且在场地内的圆弧在所求图形边界上 +// 2. 场地的四æ¡边界被圆分æˆÂ了若干æ¡线段。中点在æŸÂ个圆内部的线段在所求图形边界上 +double getArea() +{ + Point b[4]; + b[0] = Point(0, 0); + b[1] = Point(N, 0); + b[2] = Point(N, M); + b[3] = Point(0, M); + double area = 0; + // 圆弧部分 + for (int i = 0; i < n; i++) + { + vector rad; + rad.push_back(0); + rad.push_back(PI * 2); + // 圆和边界的交点 + for (int j = 0; j < 4; j++) + { + getLineCircleIntersection(b[j], b[(j + 1) % 4], P[i], R[i], rad); + } + // 圆和圆的交点 + for (int j = 0; j < n; j++) + { + getCircleCircleIntersection(P[i], R[i], P[j], R[j], rad); + } + sort(rad.begin(), rad.end()); + for (int j = 0; j < rad.size() - 1; j++) + if (rad[j + 1] - rad[j] > eps) + { + double mid = (rad[j] + rad[j + 1]) / 2.0;// 圆弧中点相对于圆i圆心的æžÂ角 + if (visible(i, mid)) // 弧中点å¯è§Â,因此弧在图形边界上 + { + area += Cross(getPoint(i, rad[j]), getPoint(i, rad[j + 1])) / 2.0; + double a = rad[j + 1] - rad[j]; + area += R[i] * R[i] * (a - sin(a)) / 2.0; + } + } + } + // 直线段部分 + for (int i = 0; i < 4; i++) + { + Vector v = b[(i + 1) % 4] - b[i]; + double len = Length(v); + vector dist; + dist.push_back(0); + dist.push_back(len); + for (int j = 0; j < n; j++) + { + vector rad; + getLineCircleIntersection(b[i], b[(i + 1) % 4], P[j], R[j], rad); + for (int k = 0; k < rad.size(); k++) + { + Point p = getPoint(j, rad[k]); + dist.push_back(Length(p - b[i])); + } + } + sort(dist.begin(), dist.end());// 必须按照到起点的è·Â离排åºÂ而ä¸Â是按照点的字典åºÂ排åºÂ,å¦则å‘é‡Â方å‘å¯能会å + vector points; + for (int j = 0; j < dist.size(); j++) + { + points.push_back(b[i] + v * (dist[j] / len)); + } + for (int j = 0; j < dist.size() - 1; j++) + { + Point midp = (points[j] + points[j + 1]) / 2.0; + if (!visible(midp)) + { + area += Cross(points[j], points[j + 1]) / 2.0;// 线段中点ä¸Âå¯è§Â,因此线段在图形边界上 + } + } + } + return N * M - area; +} + +int main() +{ + int ks, kl; + while (scanf("%d%d%d%d", &N, &M, &ks, &kl) == 4 && N && M) + { + for (int i = 0; i < ks; i++) + { + scanf("%lf%lf", &P[i].x, &P[i].y); + R[i] = 0.58; + } + sort(P, P + ks); + ks = unique(P, P + ks) - P; + for (int i = 0; i < kl; i++) + { + scanf("%lf%lf", &P[ks + i].x, &P[ks + i].y); + R[ks + i] = 1.31; + } + sort(P + ks, P + ks + kl); + n = unique(P + ks, P + ks + kl) - P; + printf("%.2lf\n", getArea()); + } + return 0; +} diff --git a/1368.cpp b/1368.cpp new file mode 100644 index 0000000..760da4d --- /dev/null +++ b/1368.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int main() +{ + char dna[64][1024]; + int T, n, m; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (int i = 0; i < n; i++) + { + scanf("%s", dna[i]); + } + + char ret[1024] = {}, cc[] = "ACGT"; + int hh = 0; + for (int i = 0; i < m; i++) + { + int cnt[4] = {}, mx = 0; + for (int j = 0; j < n; j++) + { + cnt[find(cc, cc + 4, dna[j][i]) - cc]++; + } + + for (int j = 0; j < 4; j++) + { + if (cnt[j] > cnt[mx]) + { + mx = j; + } + } + ret[i] = cc[mx]; hh += n - cnt[mx]; + } + + printf("%s\n%d\n", ret, hh); + } + return 0; +} diff --git a/137.cpp b/137.cpp new file mode 100644 index 0000000..93c6c26 --- /dev/null +++ b/137.cpp @@ -0,0 +1,291 @@ +#include + +using namespace std; + +struct POINTF +{ + float x, y; +}; + +bool Equal(float f1, float f2) +{ + return (abs(f1 - f2) < 1e-4f); +} + +bool operator==(const POINTF &p1, const POINTF &p2) +{ + return (Equal(p1.x, p2.x) && Equal(p1.y, p2.y)); +} + +bool operator>(const POINTF &p1, const POINTF &p2) +{ + return (p1.x > p2.x || (Equal(p1.x, p2.x) && p1.y > p2.y)); +} + +float operator^(const POINTF &p1, const POINTF &p2) +{ + return (p1.x * p2.y - p1.y * p2.x); +} + +int Intersection(POINTF p1, POINTF p2, POINTF p3, POINTF p4, POINTF &Int) +{ + if (p1 == p2 || p3 == p4) + { + return -1; } + if (p1 > p2) + { + swap(p1, p2); + } + if (p3 > p4) + { + swap(p3, p4); + } + POINTF v1 = {p2.x - p1.x, p2.y - p1.y}, v2 = {p4.x - p3.x, p4.y - p3.y}; + float Corss = v1 ^ v2; + if (p1 == p3 && p2 == p4) + { + return 6; + } + if (p1 == p3) + { + Int = p1; + return (Equal(Corss, 0) ? 5 : 3); + } + if (p2 == p4) + { + Int = p2; + return (Equal(Corss, 0) ? 5 : 3); + } + if (p1 == p4) + { + Int = p1; + return 3; + } + if (p2 == p3) + { + Int = p2; + return 3; + } if (p1 > p3) + { + swap(p1, p3); + swap(p2, p4); + swap(v1, v2); + Corss = v1 ^ v2; + } + if (Equal(Corss, 0)) + { + POINTF vs = {p3.x - p1.x, p3.y - p1.y}; + if (Equal(v1 ^ vs, 0)) + { + if (p2 > p3) + { + Int = p3; + return 4; } + } return 0; + } float ymax1 = p1.y, ymin1 = p2.y, ymax2 = p3.y, ymin2 = p4.y; + if (ymax1 < ymin1) + { + swap(ymax1, ymin1); + } + if (ymax2 < ymin2) + { + swap(ymax2, ymin2); + } + if (p1.x > p4.x || p2.x < p3.x || ymax1 < ymin2 || ymin1 > ymax2) + { + return 0; + } POINTF vs1 = {p1.x - p3.x, p1.y - p3.y}, vs2 = {p2.x - p3.x, p2.y - p3.y}; + POINTF vt1 = {p3.x - p1.x, p3.y - p1.y}, vt2 = {p4.x - p1.x, p4.y - p1.y}; + float s1v2, s2v2, t1v1, t2v1; + if (Equal(s1v2 = vs1 ^ v2, 0) && p4 > p1 && p1 > p3) + { + Int = p1; + return 2; + } + if (Equal(s2v2 = vs2 ^ v2, 0) && p4 > p2 && p2 > p3) + { + Int = p2; + return 2; + } + if (Equal(t1v1 = vt1 ^ v1, 0) && p2 > p3 && p3 > p1) + { + Int = p3; + return 2; + } + if (Equal(t2v1 = vt2 ^ v1, 0) && p2 > p4 && p4 > p1) + { + Int = p4; + return 2; + } if (s1v2 * s2v2 > 0 || t1v1 * t2v1 > 0) + { + return 0; + } float ConA = p1.x * v1.y - p1.y * v1.x; + float ConB = p3.x * v2.y - p3.y * v2.x; + Int.x = (ConB * v1.x - ConA * v2.x) / Corss; + Int.y = (ConB * v1.y - ConA * v2.y) / Corss; + return 1; +} + +bool CompareVector(const POINTF &pt1, const POINTF &pt2) +{ + float m1 = sqrt(pt1.x * pt1.x + pt1.y * pt1.y); + float m2 = sqrt(pt2.x * pt2.x + pt2.y * pt2.y); + float v1 = pt1.x / m1, v2 = pt2.x / m2; + return (v1 < v2 || v1 == v2 && m1 < m2); +} + +bool CalcConvexHull(vector &Src) +{ + if (Src.size() < 3) + { + return false; + } + vector::iterator i; + POINTF ptBase = Src.front(); for (i = Src.begin() + 1; i != Src.end(); ++i) + { + if (i->y < ptBase.y || (i->y == ptBase.y && i->x > ptBase.x)) + { + ptBase = *i; + } + } + for (i = Src.begin(); i != Src.end();) + { + if (*i == ptBase) + { + i = Src.erase(i); + } + else + { + i->x -= ptBase.x, i->y -= ptBase.y; + ++i; + } + } + sort(Src.begin(), Src.end(), &CompareVector); + Src.erase(unique(Src.begin(), Src.end()), Src.end()); + if (Src.size() < 2) + { + return false; + } + for (vector::reverse_iterator ri = Src.rbegin(); + ri != Src.rend() - 1; ++ri) + { + vector::reverse_iterator riNext = ri + 1; + ri->x -= riNext->x, ri->y -= riNext->y; + } + for (i = Src.begin() + 1; i != Src.end(); ++i) + { + for (vector::iterator iLast = i - 1; iLast != Src.begin();) + { + float v1 = i->x * iLast->y, v2 = i->y * iLast->x; + if (v1 > v2 || (v1 == v2 && i->x * iLast->x > 0 && + i->y * iLast->y > 0)) + { + break; + } + i->x += iLast->x, i->y += iLast->y; + iLast = (i = Src.erase(iLast)) - 1; + } + } + Src.front().x += ptBase.x, Src.front().y += ptBase.y; + for (i = Src.begin() + 1; i != Src.end(); ++i) + { + i->x += (i - 1)->x, i->y += (i - 1)->y; + } + Src.push_back(ptBase); + return (Src.size() >= 3); +} + +float CalcArea(vector &Covex) +{ + float fArea = 0; + vector::iterator i, j; + for (i = Covex.begin(); i != Covex.end(); ++i) + { + if ((j = i + 1) == Covex.end()) + { + j = Covex.begin(); + } + fArea += j->x * i->y - i->x * j->y; + } + return fArea / 2.0f; +} + +bool PointInPolygon(POINTF pt, vector &Poly) +{ + for (int i = 0; i < (int)Poly.size(); ++i) + { + int j = (i + 1) % (int)Poly.size(); + POINTF p1 = {pt.x - Poly[i].x, pt.y - Poly[i].y}; + POINTF p2 = {Poly[j].x - Poly[i].x, Poly[j].y - Poly[i].y}; + float fCross = p1 ^ p2; + if (fCross < 0) + { + return false; + } + } + return true; +} + +int main() +{ + vector Result; + for (int nNum; cin >> nNum && nNum != 0; ++nNum) + { + vector Poly1, Poly2; + for (POINTF pt; nNum-- > 0 && cin >> pt.x >> pt.y; Poly1.push_back(pt)) + ; + cin >> nNum; + for (POINTF pt; nNum-- > 0 && cin >> pt.x >> pt.y; Poly2.push_back(pt)) + ; + unique(Poly1.begin(), Poly1.end()); + unique(Poly2.begin(), Poly2.end()); + if (Poly1.size() < 3 || Poly2.size() < 3) + { + printf("%8.2f", 0.0f); + } + float fAreaUnion = CalcArea(Poly1); + fAreaUnion += CalcArea(Poly2); + vector IntPoly; + for (int i = 0; i < (int)Poly1.size(); ++i) + { + if (PointInPolygon(Poly1[i], Poly2)) + { + IntPoly.push_back(Poly1[i]); + } + } + for (int i = 0; i < (int)Poly2.size(); ++i) + { + if (PointInPolygon(Poly2[i], Poly1)) + { + IntPoly.push_back(Poly2[i]); + } + } + for (int i = 0; i < (int)Poly1.size(); ++i) + { + for (int j = 0; j < (int)Poly2.size(); ++j) + { + POINTF Int; + int nr = Intersection( + Poly1[i], Poly1[(i + 1) % (int)Poly1.size()], + Poly2[j], Poly2[(j + 1) % (int)Poly2.size()], Int); + if (nr == 6) + { + IntPoly.push_back(Poly1[i]); + IntPoly.push_back(Poly1[(i + 1) % (int)Poly1.size()]); + } + else if (nr > 0) + { + IntPoly.push_back(Int); + } + } + } + float fIntArea = CalcConvexHull(IntPoly) ? CalcArea(IntPoly) * 2 : 0; + Result.push_back(fAreaUnion - fIntArea); + } + for (vector::iterator i = Result.begin(); i != Result.end(); ++i) + { + printf("%8.2f", *i); + } + cout << endl; + return 0; +} diff --git a/1371.cpp b/1371.cpp new file mode 100644 index 0000000..2c22691 --- /dev/null +++ b/1371.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) +const int INF = 0x3f3f3f3f; +const int N = 5005; +const int M = 55; + +int t, n, m, dp[N][M]; +char a[N], b[M]; + +bool judge(int mid) +{ + memset(dp, INF, sizeof(dp)); + dp[0][0] = 0; + for (int i = 0; i <= n; i++) + { + if (dp[i][m] <= mid) + { + dp[i][0] = 0; + } + for (int j = 0; j <= m; j++) + { + dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + (a[i + 1] != b[j + 1])); + dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1); + dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1); + } + } + return dp[n][m] <= mid; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%s", b + 1); + scanf("%s", a + 1); + n = strlen(a + 1); + m = strlen(b + 1); + int l = 0, r = min(n, m); + while (l < r) + { + int mid = (l + r) / 2; + if (judge(mid)) + { + r = mid; + } + else + { + l = mid + 1; + } + } + printf("%d\n", l); + } + return 0; +} diff --git a/1372.cpp b/1372.cpp new file mode 100644 index 0000000..ccfc14f --- /dev/null +++ b/1372.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define INF 0x3f3f3f3f +const int N = 5005; +int t, n, k, i, start[N]; + +bool check(int s, int e) +{ + return e - s <= k; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + int ans = 0; + scanf("%d%d", &n, &k); + for (i = 0; i < n; i++) + { + scanf("%d", &start[i]); + } + sort(start, start + n); + i = 0; + while (i < n) + { + int sum = 1; + while (i < n - 1 && ((sum == 1 && check(start[i], start[i + 1])) || + (check(start[i], start[i + 1]) && check(start[i - 1], start[i + 1])))) + { + sum++, i++; + } + if (!check(start[i], start[i + 1]) || i == n - 1) + { + i++; + } + ans = max(ans, sum); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1374.cpp b/1374.cpp new file mode 100644 index 0000000..a1caf87 --- /dev/null +++ b/1374.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define ll long long +#define oo 10000007 + +int way[1005], num; +bool DFSID(int x, int step) +{ + int i; + if (num > step) + return false; + if (way[num] == x) + return true; + if (way[num] << (step - num) < x) + return false;// 强力剪枠+ for (i = 0; i <= num; i++) + { + num++; + way[num] = way[num - 1] + way[i]; + if (way[num] <= 1024 && DFSID(x, step)) + return true; + way[num] = way[num - 1] - way[i]; + if (way[num] > 0 && DFSID(x, step)) + return true; + num--; + } + return false; +} +int main() +{ + int i, x; + while (~scanf("%d", &x) && x) + { + for (i = 0;; i++) + { + way[num = 0] = 1; + if (DFSID(x, i)) + break; + } + printf("%d\n", i); + } + return 0; +} diff --git a/1376.cpp b/1376.cpp new file mode 100644 index 0000000..aa4ec71 --- /dev/null +++ b/1376.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +// LA3661 Animal Run:å¦一ç§Â建模方法(书上使用的方法),图的结点较多,速度较慢 +// Rujia Liu + +const int INF = 1000000000; +const int maxn = 3000000 + 10; + +struct Edge +{ + int from, to, dist; +}; + +struct HeapNode +{ + int d, u; + bool operator<(const HeapNode &rhs) const + { + return d > rhs.d; + } +}; + +struct Dijkstra +{ + int n, m; + vector edges; + vector G[maxn]; + bool done[maxn];// 是å¦已永久标å· + int d[maxn]; // s到å„个点的è·Â离 + int p[maxn]; // 最短路中的上一æ¡弧 + + void init(int n) + { + this->n = n; + for (int i = 0; i < n; i++) + G[i].clear(); + edges.clear(); + } + + void AddEdge(int from, int to, int dist) + { + edges.push_back((Edge){from, to, dist}); + m = edges.size(); + G[from].push_back(m - 1); + } + + void dijkstra(int s) + { + priority_queue Q; + for (int i = 0; i < n; i++) + d[i] = INF; + d[s] = 0; + memset(done, 0, sizeof(done)); + Q.push((HeapNode){0, s}); + while (!Q.empty()) + { + HeapNode x = Q.top(); + Q.pop(); + int u = x.u; + if (done[u]) + continue; + done[u] = true; + for (int i = 0; i < G[u].size(); i++) + { + Edge &e = edges[G[u][i]]; + if (d[e.to] > d[u] + e.dist) + { + d[e.to] = d[u] + e.dist; + p[e.to] = G[u][i]; + Q.push((HeapNode){d[e.to], e.to}); + } + } + } + } +}; + +//////// 题目相关 +#define REP(i, n) for (int i = 0; i < (n); ++i) + +int n, m; +int ID(int r, int c, int type) { return type * n * m + r * m + c + 1; } + +const int maxsize = 1000; +int cost[maxsize][maxsize][3]; + +Dijkstra solver; + +void AddTriangleEdges(int *s1, int *s2, int *s3) +{ + int *edges[3] = {s1, s2, s3}; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if (i != j) + { + int id1 = ID(edges[i][0], edges[i][1], edges[i][2]); + int id2 = ID(edges[j][0], edges[j][1], edges[j][2]); + int c = cost[edges[j][0]][edges[j][1]][edges[j][2]]; + solver.AddEdge(id1, id2, c); + } +} + +int main() +{ + int kase = 0; + while (scanf("%d%d", &n, &m) == 2 && n && m) + { + REP(i, n) + REP(j, m - 1) scanf("%d", &cost[i][j][0]);// 横线 + REP(i, n - 1) + REP(j, m) scanf("%d", &cost[i][j][1]);// 竖线 + REP(i, n - 1) + REP(j, m - 1) scanf("%d", &cost[i][j][2]);// 斜线 + solver.init(3 * n * m + 1); + REP(i, n - 1) + REP(j, m - 1) + { + int top[] = {i, j, 0}; + int bottom[] = {i + 1, j, 0}; + int left[] = {i, j, 1}; + int right[] = {i, j + 1, 1}; + int slash[] = {i, j, 2}; + AddTriangleEdges(top, slash, right); + AddTriangleEdges(left, slash, bottom); + } + // 从起点到左/下边界的弧 + REP(i, n - 1) + solver.AddEdge(0, ID(i, 0, 1), cost[i][0][1]);// å·¦ + REP(i, m - 1) + solver.AddEdge(0, ID(n - 1, i, 0), cost[n - 1][i][0]);// 下 + solver.dijkstra(0); + + // 找出å³/上边界的最少d值 + int ans = 2000000000; + REP(i, n - 1) + ans = min(ans, solver.d[ID(i, m - 1, 1)]);// å³ + REP(i, m - 1) + ans = min(ans, solver.d[ID(0, i, 0)]);// 上 + printf("Case %d: Minimum = %d\n", ++kase, ans); + } + return 0; +} diff --git a/1377.cpp b/1377.cpp new file mode 100644 index 0000000..6f66e06 --- /dev/null +++ b/1377.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +const int MAXN = 1000005; + +int N, n, id[MAXN], d[25], cas = 0, Max, vis[2222222]; + +set ans; +struct S +{ + int state; + set ans; +} p; + +void init() +{ + int D; + n = 0; + ans.clear(); + memset(id, 0, sizeof(id)); + memset(vis, 0, sizeof(vis)); + for (int i = 0; i < N; i++) + { + scanf("%d", &D); + if (!id[D]) + { + d[n++] = D; + id[D] = 1; + } + } + sort(d, d + n); + Max = d[n - 1]; + memset(id, -1, sizeof(id)); + for (int j = 0; j < n; j++) + id[d[j]] = j; +} + +void BFS() +{ + queue Q; + p.ans.clear(); + p.ans.insert(0); + p.state = 0; + Q.push(p); + while (!Q.empty()) + { + p = Q.front(); + Q.pop(); + if (p.state == (1 << n) - 1) + { + if (ans.size() == 0) + { + ans = p.ans; + } + else + { + if (ans.size() < p.ans.size()) + return; + else if (ans.size() > p.ans.size()) + ans = p.ans; + else + { + if (*ans.rbegin() > *p.ans.rbegin()) + ans = p.ans; + } + } + } + if (p.ans.size() == 7) + continue; + for (int i = 0; i < n; i++) + { + if ((p.state & (1 << i))) + continue; + for (set::iterator it = p.ans.begin(); it != p.ans.end(); it++) + { + if (*it > d[i]) + { + S q = p; + int sum = *it - d[i]; + for (set::iterator it2 = q.ans.begin(); it2 != q.ans.end(); it2++) + { + int nu = abs(*it2 - sum); + if (id[nu] == -1) + continue; + q.state = (q.state | (1 << id[nu])); + } + q.ans.insert(sum); + if (!vis[q.state]) + { + Q.push(q); + vis[q.state] = 1; + } + } + if (*it + d[i] <= Max) + { + S q = p; + int sum = *it + d[i]; + for (set::iterator it2 = q.ans.begin(); it2 != q.ans.end(); it2++) + { + int nu = abs(*it2 - sum); + if (id[nu] == -1) + continue; + q.state = (q.state | (1 << id[nu])); + } + q.ans.insert(sum); + if (!vis[q.state]) + { + Q.push(q); + vis[q.state] = 1; + } + } + } + } + } +} + +void solve() +{ + BFS(); + int bo = 0; + printf("Case %d:\n%d\n", ++cas, ans.size()); + for (set::iterator it = ans.begin(); it != ans.end(); it++) + { + if (bo++) + printf(" "); + printf("%d", *it); + } + printf("\n"); +} + +int main() +{ + while (~scanf("%d", &N) && N) + { + init(); + solve(); + } + return 0; +} diff --git a/1378.cpp b/1378.cpp new file mode 100644 index 0000000..2edec30 --- /dev/null +++ b/1378.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define INF 100 +#define NIM_SIZE 100 + +struct NIM +{ + int vis[NIM_SIZE], sg[NIM_SIZE]; + int _SG(int n) + { + memset(vis, 0, sizeof(vis)); + for (int i = 0; i < n; i++) + for (int j = 0; j <= i; j++) + { + vis[sg[i] ^ sg[j]] = 1; + } + for (int i = 0; i < NIM_SIZE; i++) + if (vis[i] == 0) + { + return i; + } + } + void pre_sg(int n) + { + sg[0] = 0; + for (int i = 1; i <= n; i++) + { + sg[i] = _SG(i); + } + } + void show(int n) + { + for (int i = 0; i <= n; i++) + { + printf("sg[%d] = %d\n", i, sg[i]); + } + } + int find_period() + { + for (int res = 1, j; res * 2 < NIM_SIZE; res++) + { + for (j = NIM_SIZE / 2; j < NIM_SIZE; j++) + if (sg[j] != sg[j - res]) + { + break; + } + if (j == NIM_SIZE) + { + return res; + } + } + return -1; + } +} nim; +int N[INF]; +void work(int &x, int &y, int &z, int n, int sg) +{ + for (int i = n; i; i--) + for (int j = i - 1; j; j--) + for (int k = j; k; k--) + if (N[i] && (nim.sg[i - 1] ^ nim.sg[j - 1] ^ nim.sg[k - 1]) == sg) + { + x = n - i, y = n - j, z = n - k; + return; + } +} +int main() +{ + int cas = 0, n, i; + nim.pre_sg(23); + while (scanf("%d", &n), n) + { + int sg = 0, x, y, z; + for (i = n; i; i--) + { + scanf("%d", &N[i]); + } + for (i = 1; i <= n; i++) + if (N[i] & 1) + { + sg ^= nim.sg[i - 1]; + } + if (sg) + { + work(x, y, z, n, sg); + } + else + { + x = -1, y = -1, z = -1; + } + printf("Game %d: %d %d %d\n", ++cas, x, y, z); + } + return 0; +} diff --git a/1379.cpp b/1379.cpp new file mode 100644 index 0000000..4323466 --- /dev/null +++ b/1379.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +struct Beat +{ + int p, id; + bool operator<(const Beat &tt) const + { + return p > tt.p; + } +} team[555][555]; + +int n, m, k, tm_, day[555], dp[2][5][5][5][5], vis[555]; + +void tomax(int &a, int b) +{ + if (a < b) + { + a = b; + } +} +int get(int i, int j) +{ + if (i <= 0 || day[i] == 0) + { + return 111111; + } + return team[day[i]][j].id; +} + +bool check(int i, int j, int r, int e, int w, int f) +{ + tm_++; + int tt; + tt = get(i, f); + if (tt < 110) + { + vis[tt] = tm_; + } + tt = get(i - 1, w); + if (tt < 110 && vis[tt] == tm_) + { + return 0; + } + if (tt < 110) + { + vis[tt] = tm_; + } + tt = get(i - 2, e); + if (tt < 110 && vis[tt] == tm_) + { + return 0; + } + if (tt < 110) + { + vis[tt] = tm_; + } + tt = get(i - 3, r); + if (tt < 110 && vis[tt] == tm_) + { + return 0; + } + if (tt < 110) + { + vis[tt] = tm_; + } + tt = get(i - 4, j); + if (tt < 110 && vis[tt] == tm_) + { + return 0; + } + if (tt < 110) + { + vis[tt] = tm_; + } + return 1; +} + +int work() +{ + int ret = 0; + memset(dp, 0, sizeof(dp)); + memset(vis, 0, sizeof(vis)); + tm_ = 0; + int now = 0, nex = 1; + for (int i = 1; i <= k; i++) + { + for (int j = 0; j < 5; j++) + { + for (int r = 0; r < 5; r++) + { + for (int e = 0; e < 5; e++) + { + for (int w = 0; w < 5; w++) + { + for (int f = 0; f < 5; f++) + { + if (check(i, j, r, e, w, f)) + { + tomax(dp[nex][r][e][w][f], dp[now][j][r][e][w] + team[day[i]][f].p); + if (i == k) + { + tomax(ret, dp[nex][r][e][w][f]); + } + } + } + } + } + } + } + swap(now, nex); + memset(dp[nex], 0, sizeof(dp[nex])); + } + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &n, &m, &k); + k += 10; + for (int i = 1; i <= m; i++) + { + for (int j = 0; j < n; j++) + { + scanf("%d", &team[i][j].p); + team[i][j].id = j; + } + sort(team[i], team[i] + n); + } + for (int i = 1; i <= k; i++) + { + scanf("%d", &day[i]); + } + printf("%.2f\n", work() / 100.0); + } + return 0; +} diff --git a/138.cpp b/138.cpp index e16c4bd..048278a 100644 --- a/138.cpp +++ b/138.cpp @@ -1,7 +1,23 @@ -#include +#include + using namespace std; -int main(){ - printf(" 6 8\n 35 49\n 204 288\n 1189 1681\n 6930 9800\n 40391 57121\n 235416 332928\n 1372105 1940449\n 7997214 11309768\n 46611179 65918161\n"); +int R[] = {6, 8, + 35, 49, + 204, 288, + 1189, 1681, + 6930, 9800, + 40391, 57121, + 235416, 332928, + 1372105, 1940449, + 7997214, 11309768, + 46611179, 65918161}; + +int main() +{ + for (int i = 0; i < 20; i += 2) + { + printf("%10d%10d\n", R[i], R[i + 1]); + } return 0; } diff --git a/1380.cpp b/1380.cpp new file mode 100644 index 0000000..536b78d --- /dev/null +++ b/1380.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define MAXN 256 + +int dg[MAXN][MAXN]; +vector g[MAXN]; + +pair dfs(int u, int p, int day) +{ + int up = 0, down = 0; + vector > D; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (v == p) + { + continue; + } + pair t = dfs(v, u, day); + if (dg[u][v] == 2) + { + D.push_back(t); + } + else if (dg[u][v] == 1) + { + down = max(down, t.second + 1); + } + else + { + up = max(up, t.first + 1); + } + } + if (D.size() > 0) + { + sort(D.begin(), D.end()); + int tup = INF, tdown = INF; + for (int i = 0; i <= D.size(); i++) + { + int u = i - 1 >= 0 ? D[i - 1].first + 1 : 0, d = 0; + for (int j = i; j < D.size(); j++) + { + d = max(d, D[j].second + 1); + } + u = max(u, up); + d = max(d, down); + if (u + d <= day) + { + tup = min(tup, u); + tdown = min(tdown, d); + } + } + up = tup, down = tdown; + } + if (up + down <= day) + { + return make_pair(up, down); + } + else + { + return make_pair(INF, INF); + } +} + +int tst(int day) +{ + pair t = dfs(1, -1, day); + return t.first + t.second <= day; +} + +int main() +{ + int x, y; + char s[MAXN]; + while (1) + { + for (int i = 0; i < MAXN; i++) + { + g[i].clear(); + } + int f = 0; + while (scanf("%d", &x) == 1 && x) + { + f++; + while (scanf("%s", s) == 1 && s[0] != '0') + { + int n = (int)strlen(s); + if (s[n - 1] == 'd') + { + s[n - 1] = '\0'; + sscanf(s, "%d", &y); + g[x].push_back(y), g[y].push_back(x); + dg[x][y] = 1, dg[y][x] = 0; + } + else if (s[n - 1] == 'u') + { + s[n - 1] = '\0'; + sscanf(s, "%d", &y); + g[x].push_back(y), g[y].push_back(x); + dg[x][y] = 0, dg[y][x] = 1; + } + else + { + sscanf(s, "%d", &y); + g[x].push_back(y), g[y].push_back(x); + dg[x][y] = 2, dg[y][x] = 2; + } + } + } + if (!f) + { + break; + } + int ret = 0, l = 0, r = MAXN, mid; + while (l <= r) + { + mid = (l + r) / 2; + if (tst(mid)) + { + r = mid - 1; + ret = mid; + } + else + { + l = mid + 1; + } + } + printf("%d\n", ret + 1); + } + return 0; +} diff --git a/1381.cpp b/1381.cpp new file mode 100644 index 0000000..1473f0f --- /dev/null +++ b/1381.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int main() +{ + int cases = 0; + int A[16], B[4]; + while (scanf("%d", &A[0]) == 1 && A[0]) + { + for (int i = 1; i < 16; i++) + scanf("%d", &A[i]); + sort(A, A + 16); + int C[1 << 16] = {}; + vector D[32767]; + for (int i = (1 << 16) - 1; i >= 0; i--) + { + if (__builtin_popcount(i) == 4) + { + int n = 0; + for (int j = 0; j < 16; j++) + if ((i >> j) & 1) + B[n++] = A[j]; + do + { + int sum = B[0] + B[1] * 2 + B[2] * 3 + B[3] * 4; + for (int j = 0; j < D[sum].size(); j++) + { + if (D[sum][j] & i) + continue; + C[D[sum][j] | i]++; + } + D[sum].push_back(i); + } while (next_permutation(B, B + 4)); + } + } + int ret = 0; + for (int i = (1 << 16) - 1; i >= 0; i--) + ret += C[i] * C[((1 << 16) - 1) ^ i]; + printf("Case %d: %d\n", ++cases, ret / 2); + } + return 0; +} diff --git a/1382.cpp b/1382.cpp new file mode 100644 index 0000000..5a3b056 --- /dev/null +++ b/1382.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +#define mem(name, value) memset(name, value, sizeof(name)) +#define FOR(i, n) for (int i = 1; i <= n; i++) + +const int MAXN = 100 + 10; + +struct Point +{ + int x, y; + bool operator<(const Point &rhs) const + { + return x < rhs.x; + } +} p[MAXN]; +int n, m, y[MAXN], left_[MAXN], on[MAXN], on2[MAXN]; + +int solve() +{ + sort(p, p + n); + sort(y, y + n); + m = unique(y, y + n) - y; + if (m <= 2) + { + return n; + } + int ans = 0; + for (int a = 0; a < m; a++) + for (int b = a + 1; b < m; b++) + { + int ymin = y[a], ymax = y[b]; + int k = 0; + for (int i = 0; i < n; i++) + { + if (i == 0 || p[i].x != p[i - 1].x) + { + k++; + on[k] = on2[k] = 0; + left_[k] = k == 0 ? 0 : left_[k - 1] + on2[k - 1] - on[k - 1]; + } + if (p[i].y > ymin && p[i].y < ymax) + { + on[k]++; + } + if (p[i].y >= ymin && p[i].y <= ymax) + { + on2[k]++; + } + } + if (k <= 2) + { + return n; + } + int M = 0; + for (int j = 1; j <= k; j++) + { + ans = max(ans, left_[j] + on2[j] + M); + M = max(on[j] - left_[j], M); + } + } + return ans; +} + +int main() +{ + int t = 0; + while (~scanf("%d", &n) && n) + { + for (int i = 0; i < n; i++) + { + scanf("%d%d", &p[i].x, &p[i].y); + y[i] = p[i].y; + } + printf("Case %d: %d\n", ++t, solve()); + } + return 0; +} diff --git a/1383.cpp b/1383.cpp new file mode 100644 index 0000000..a248285 --- /dev/null +++ b/1383.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define MAXN 524288 +struct E +{ + int value, index; + E(int a = 0, int b = 0) : value(a), index(b) {} + bool operator<(const E &a) const + { + return value < a.value; + } +}; +E tree[(MAXN << 1) + 10]; +int M; +void setTree(int s, int t) +{ + int i; + for (i = 2 * M - 1; i >= 0; i--) + { + tree[i] = E(0x3f3f3f3f, 0); + } +} +E query(int s, int t) +{ + E ret(0x3f3f3f3f, 0); + for (s = s + M - 1, t = t + M + 1; (s ^ t) != 1;) + { + if (~s & 1) + ret = min(ret, tree[s ^ 1]); + if (t & 1) + ret = min(ret, tree[t ^ 1]); + s >>= 1, t >>= 1; + } + return ret; +} +void update(int s, E v) +{ + tree[s + M] = v; + for (s = s + M; s > 0; s >>= 1) + tree[s] = min(tree[s], v); +} +int main() +{ + int n, x, cases = 0; + char cmd[5]; + while (scanf("%d", &n) == 1 && n) + { + for (M = 1; M < 500000 + 2; M <<= 1) + ; + setTree(0, M); + int B = 0; + vector BX; + if (cases++) + puts(""); + printf("Case %d:\n", cases); + for (int i = 0; i < n; i++) + { + scanf("%s %d", cmd, &x); + if (cmd[0] == 'B') + { + update(x + 1, E(x + 1, ++B)); + BX.push_back(x); + } + else + { + if (BX.size() == 0) + puts("-1"); + else if (x < 5000) + { + int mn1 = 0x3f3f3f3f, mn2 = -1, u; + for (int i = BX.size() - 1; i >= 0; i--) + { + u = BX[i] % x; + if (u == 0) + { + mn1 = 0, mn2 = i + 1; + break; + } + if (u < mn1) + mn1 = u, mn2 = i + 1; + } + printf("%d\n", mn2); + } + else + { + int mn1 = 0x3f3f3f3f, mn2 = -1; + for (int y = 0; y <= 500000; y += x) + { + E t = query(y + 1, min(y + 1 + x - 1, 500000)); + if (t.value - y < mn1 || (t.value - y == mn1 && t.index > mn2)) + mn1 = t.value - y, mn2 = t.index; + } + printf("%d\n", mn2); + } + } + } + } + return 0; +} diff --git a/1385.cpp b/1385.cpp new file mode 100644 index 0000000..8a3b1e7 --- /dev/null +++ b/1385.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +const int MAXN = 110; +const string ZERO = (string) "00000000000"; +const string NINE = (string) "99999999999"; + +struct Node +{ + Node *go[10]; + string name; + bool push; + Node() {} + Node(string p) + { + for (int i = 0; i < 10; ++i) + this->go[i] = NULL; + this->name = p; + this->push = true; + } +}; + +vector ans; +Node *root; +string A[MAXN], B[MAXN], plan[MAXN]; +int cnt; + +inline void make(string &s1, string &s2) +{ + int n1 = s1.size(), n2 = s2.size(); + for (int i = n1 - n2 - 1; i >= 0; --i) + { + s2 = s1[i] + s2; + } +} + +inline bool same(string &s, int st, int ed, char c) +{ + for (int i = st; i < ed; ++i) + { + if (s[i] != c) + return false; + } + return true; +} + +inline void PushDown(Node *p) +{ + for (int i = 0; i < 10; ++i) + { + if (p->go[i] == NULL) + { + p->go[i] = new Node(p->name); + } + else + { + p->go[i]->name = p->name; + p->go[i]->push = true; + } + } + p->push = false; +} + +void Insert(Node *p, string s1, string s2, string v, int dep, int n) +{ + if (same(s1, dep, n, '0') && same(s2, dep, n, '9')) + { + p->name = v; + p->push = true; + return; + } + if (p->push == true) + { + PushDown(p); + } + for (char i = s1[dep]; i <= s2[dep]; ++i) + { + int o = i - '0'; + Insert(p->go[o], i == s1[dep] ? s1 : ZERO, i == s2[dep] ? s2 : NINE, v, dep + 1, n); + } + string ret = p->go[0]->name; + bool flag = true; + for (int i = 0; i < 10; ++i) + { + if (ret != p->go[i]->name) + { + flag = false; + } + } + p->name = flag ? ret : ""; +} + +void query(Node *p, string s) +{ + if (p->name != "") + { + if (p->name != "invalid") + { + if (p == root) + { + for (char i = '0'; i <= '9'; ++i) + { + ans.push_back((string) "" + i + " " + p->name); + } + } + else + { + ans.push_back(s + " " + p->name); + } + } + return; + } + for (int i = 0; i < 10; ++i) + { + query(p->go[i], s + (char)(i + '0')); + } +} + +void Recycle(Node *p) +{ + if (p == NULL) + return; + for (int i = 0; i < 10; ++i) + { + Recycle(p->go[i]); + } + free(p); +} + +int main() +{ + int n, cas = 0; + while (cin >> n) + { + if (cas++) + cout << endl; + root = new Node("invalid"); + for (int i = 0; i < n; ++i) + { + cin >> A[i] >> B[i]; + cin >> B[i] >> plan[i]; + make(A[i], B[i]); + } + for (int i = n - 1; i >= 0; --i) + { + Insert(root, A[i], B[i], plan[i], 0, A[i].size()); + } + ans.clear(); + query(root, ""); + cout << ans.size() << endl; + for (int i = 0; i < (int)ans.size(); ++i) + { + cout << ans[i] << endl; + } + Recycle(root); + } + return 0; +} diff --git a/1386.cpp b/1386.cpp new file mode 100644 index 0000000..bc85542 --- /dev/null +++ b/1386.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define CLR(a, b) memset(a, b, sizeof(a)) +#define MAXN 500 +typedef long long LL; +int n, d, k, MOD; +LL A[MAXN], B[MAXN]; + +void mul(LL *a, LL *b) +{ + LL c[MAXN] = {0}; + for (int i = 0; i < n; ++i) + for (int j = 0; j < n; ++j) + { + c[i] += a[j] * b[i <= j ? (j - i) : (n - i + j)]; + } + for (int i = 0; i < n; ++i) + { + a[i] = c[i] % MOD; + } +} + +void fast_mod(int b) +{ + while (b) + { + if (b & 1) + { + mul(A, B); + } + mul(B, B); + b >>= 1; + } +} + +void scan(LL &x) +{ + char c; + while (c = getchar(), c < '0' || c > '9') + ; + x = c - '0'; + while (c = getchar(), c >= '0' && c <= '9') + { + x = x * 10 + c - '0'; + } +} + +int main() +{ + while (~scanf("%d %d %d %d", &n, &MOD, &d, &k)) + { + CLR(A, 0); + CLR(B, 0); + for (int i = 0; i < n; ++i) + { + scan(A[i]); + } + for (int i = -d; i <= d; ++i) + { + B[(i + n) % n] = 1; + } + fast_mod(k); + for (int i = 0; i < n; ++i) + { + if (i != 0) + { + putchar(' '); + } + printf("%lld", A[i]); + } + putchar('\n'); + } +} diff --git a/1388.cpp b/1388.cpp new file mode 100644 index 0000000..a357caa --- /dev/null +++ b/1388.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + double pos, ans; + int n, m; + while (scanf("%d%d", &n, &m) != EOF) + { + ans = 0.0f; + for (int i = 1; i < n; i++) + { + pos = (i * 1.0 / n) * (n + m); + ans += fabs(pos - floor(pos + 0.5f)) / (n + m); + } + printf("%.4lf\n", ans * 10000.0f); + } + return 0; +} diff --git a/139.cpp b/139.cpp new file mode 100644 index 0000000..3c258e8 --- /dev/null +++ b/139.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int str2int(string s) +{ + int result = 0; + for (int i = 0, sz = s.size(); i < sz; i++) + { + result = result * 10 + (s[i] - '0'); + } + return result; +} + +int main() +{ + size_t found; + int duration, pos; + string input, code, locality, number, subscriber; + vector codes; + map> iddstd; + while (cin >> code) + { + if (code == "000000") + { + break; + } + codes.push_back(code); + cin.get(); + getline(cin, input); + found = input.find('$'); + locality = string(input.begin(), input.begin() + found); + iddstd[code] = make_pair(locality, str2int(string(input.begin() + found + 1, input.end()))); + } + while (cin >> number) + { + if (number == "#") + { + break; + } + cin >> duration; + pos = -1; + for (int i = 0, sz = codes.size(); i < sz; i++) + { + if (int(number.find(codes[i])) == 0) + { + if (codes[i][1] == '0' && codes[i].size() > 2 && number.size() - codes[i].size() > 3 && number.size() - codes[i].size() < 11) + { + pos = i; + break; + } + else if (codes[i][1] != '0' && number.size() - codes[i].size() > 3 && number.size() - codes[i].size() < 8) + { + pos = i; + break; + } + } + } + if (pos != -1) + { + subscriber = number.substr(codes[pos].size()); + } + if (pos == -1 && number.size() > 0 && number[0] == '0') + { + cout << left << setw(16) << number << setw(35) << "Unknown" << setw(5) << right << duration << setw(13) << "-1.00" << endl; + } + else if (pos == -1 && number.size() > 0 && number[0] != '0') + { + cout << left << setw(16) << number << "Local" << right << setw(30) << number << setw(5) << duration << setw(6) << "0.00" << setw(7) << "0.00" << endl; + } + else + { + cout << left << setw(16) << number << iddstd[codes[pos]].first << right << setw(35 - iddstd[codes[pos]].first.size()) << subscriber << setw(5) << duration << setw(6) << fixed << setprecision(2) << iddstd[codes[pos]].second / 100.0 << setw(7) << iddstd[codes[pos]].second / 100.0 * duration << endl; + } + } + return 0; +} diff --git a/1390.cpp b/1390.cpp new file mode 100644 index 0000000..9efdad9 --- /dev/null +++ b/1390.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +typedef long long LL; + +#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) +#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++) + +const int maxn = 31; + +struct node +{ + int a[maxn]; + double res; + bool operator==(const node &b) const + { + for (int i = 1; i <= 30; i++) + { + if (a[i] != b.a[i]) + { + return 0; + } + } + return 1; + } +}; +vector G[maxn]; +int n, m; + +inline int read() +{ + if (scanf("%d%d", &n, &m) != 2) + { + return 0; + } + rep1(i, n) G[i].clear(); + rep(i, m) + { + int x, y; + scanf("%d %d", &x, &y); + G[x].push_back(y); + G[y].push_back(x); + } + return 1; +} + +int vis[maxn], b[maxn], cnt; +inline int dfs(int u) +{ + vis[u] = 1; + int res = 1; + for (int i = 0; i < G[u].size(); i++) + { + if (!vis[G[u][i]]) + { + res += dfs(G[u][i]); + } + } + return res; +} + +const int maxhash = 1000007; +int head[maxhash], nxt[maxhash * 3]; +vector st; + +inline int hashh(node &A) +{ + int res = 0; + rep1(i, 30) + { + res = (res * 30 + A.a[i]) % maxhash; + } + return res; +} + +int ok; + +inline int Vis(node &A) +{ + int id = hashh(A); + for (int p = head[id]; p != -1; p = nxt[p]) + { + if (st[p] == A) + { + ok = 1; + return p; + } + } + st.push_back(A); + int u = st.size() - 1; + nxt[u] = head[id]; + head[id] = u; + return u; +} + +inline double dp(node &A) +{ + ok = 0; + int id = Vis(A); + if (ok) + { + return st[id].res; + } + if (A.a[n] == 1) + { + return st[id].res = 0; + } + int sum = n * (n - 1); + double ans = 0; + rep1(i, 30) if (A.a[i]) + { + sum -= A.a[i] * i * (i - 1); + } + ans = (n * (n - 1) * 1.0) / (sum); + rep1(i, 30) if (A.a[i]) + { + for (int j = i; j <= 30; j++) + if (A.a[j]) + { + if (i == j && A.a[j] < 2) + { + continue; + } + int ji = (A.a[i]) * (A.a[j]) * 2.0; + if (i == j) + { + ji = (A.a[i]) * (A.a[j] - 1); + } + A.a[i]--; + A.a[j]--; + A.a[i + j]++; + ans += dp(A) * (i * j * ji) / sum; + A.a[i]++; + A.a[j]++; + A.a[i + j]--; + } + } + return st[id].res = ans; +} + +inline double solve() +{ + st.clear(); + memset(head, -1, sizeof(head)); + node A; + rep(i, 31) A.a[i] = 0; + rep(i, cnt) + { + A.a[b[i]]++; + } + return dp(A); +} + +inline void show(node &A) +{ + for (int i = 1; i <= 4; i++) + { + cout << A.a[i] << " "; + } + cout << endl; +} + +int main() +{ + while (read()) + { + memset(vis, 0, sizeof(vis)); + cnt = 0; + rep1(i, n) + { + if (!vis[i]) + { + b[cnt++] = dfs(i); + } + } + printf("%.10lf\n", solve()); + } + return 0; +} diff --git a/1391.cpp b/1391.cpp new file mode 100644 index 0000000..8920d8a --- /dev/null +++ b/1391.cpp @@ -0,0 +1,190 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:1024000000,1024000000") + +typedef long long ll; +typedef unsigned long long ull; +typedef pair pii; +#define pb(a) push(a) +#define INF 0x1f1f1f1f +#define lson idx << 1, l, mid +#define rson idx << 1 | 1, mid + 1, r +#define PI 3.1415926535898 +template +T min(const T &a, const T &b, const T &c) +{ + return min(min(a, b), min(a, c)); +} +template +T max(const T &a, const T &b, const T &c) +{ + return max(max(a, b), max(a, c)); +} +void debug() +{ +#ifdef ONLINE_JUDGE +#else + freopen("d:\\in1.txt", "r", stdin); + freopen("d:\\out1.txt", "w", stdout); +#endif +} +int getch() +{ + int ch; + while ((ch = getchar()) != EOF) + { + if (ch != ' ' && ch != '\n') + { + return ch; + } + } + return EOF; +} + +struct Edge +{ + int u, v; +}; +const double eps = 10e-6; +const int maxn = 100005; +double x; + +int age[maxn]; + +struct TwoSat +{ + int n; + int mark[maxn * 2]; + vector g[maxn * 2]; + int s[maxn], c; + + void init(int n) + { + this->n = n; + for (int i = 0; i < n * 2; i++) + { + g[i].clear(); + } + memset(mark, 0, sizeof(mark)); + } + void add_clause(int u, int v) + { + g[u].push_back(v); + } + + bool dfs(int u) + { + if (mark[u ^ 1]) + { + return false; + } + if (mark[u]) + { + return true; + } + mark[u] = 1; + s[c++] = u; + for (int i = 0; i < g[u].size(); i++) + { + if (!dfs(g[u][i])) + { + return false; + } + } + return true; + } + + bool solve() + { + for (int i = 0; i < n * 2; i += 2) + { + if (!mark[i] && !mark[i + 1]) + { + c = 0; + if (!dfs(i)) + { + while (c) + { + mark[s[--c]] = 0; + } + if (!dfs(i + 1)) + { + return false; + } + } + } + } + return true; + } +}; + +int n, m; +TwoSat solver; + +bool same(int a, int b) +{ + if (age[a] >= x && age[b] >= x) + { + return true; + } + if (age[a] < x && age[b] < x) + { + return true; + } + return false; +} +int main() +{ + while (scanf("%d%d", &n, &m) != EOF && (n || m)) + { + x = 0; + solver.init(n); + for (int i = 0; i < n; i++) + { + scanf("%d", &age[i]); + x += age[i]; + } + x /= n; + for (int i = 0; i < m; i++) + { + int u, v; + scanf("%d%d", &u, &v); + u--; + v--; + if (same(u, v)) + { + solver.add_clause(u * 2, v * 2 + 1); + solver.add_clause(u * 2 + 1, v * 2); + solver.add_clause(v * 2, u * 2 + 1); + solver.add_clause(v * 2 + 1, u * 2); + } + else + { + solver.add_clause(u * 2, v * 2 + 1); + solver.add_clause(v * 2, u * 2 + 1); + } + } + bool res = solver.solve(); + if (!res) + { + printf("No solution.\n"); + } + else + { + for (int i = 0; i < n; i++) + { + if (solver.mark[i * 2]) + { + printf("C\n"); + } + else + { + printf("%c\n", age[i] >= x ? 'A' : 'B'); + } + } + } + } + return 0; +} diff --git a/1392.cpp b/1392.cpp new file mode 100644 index 0000000..a4d7732 --- /dev/null +++ b/1392.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +const int maxn = 200200; + +struct Data +{ + int x, y; + bool operator<(const Data &rhs) const + { + return (x > rhs.x) || (x == rhs.x && y < rhs.y); + } +} F[maxn]; + +int n, P, ans; +char s1[maxn], s2[maxn]; + +int main() +{ + while (scanf("%d %d", &n, &P) != EOF && n + P > 0) + { + scanf("%s %s", s1 + 1, s2 + 1); + int sum = 0; + ans = 0; + for (int i = 1; i <= n; i++) + { + if (s1[i] != s2[i]) + sum++; + F[i].x = 100 * sum - i * P; + F[i].y = i; + } + F[0].x = F[0].y = 0; + sort(F, F + n + 1); + for (int i = 1, j = F[0].y; i <= n; i++) + if (j < F[i].y) + ans = max(ans, F[i].y - j); + else + j = F[i].y; + if (ans > 0) + printf("%d\n", ans); + else + printf("No solution.\n"); + } + return 0; +} diff --git a/1393.cpp b/1393.cpp new file mode 100644 index 0000000..429e82c --- /dev/null +++ b/1393.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +int gcd[310][310]; + +int Gcd(int a, int b) +{ + return b == 0 ? a : Gcd(b, a % b); +} + +int main() +{ + for (int i = 1; i <= 300; i++) + for (int j = i; j <= 300; j++) + gcd[i][j] = Gcd(i, j); + int n, m; + long long int ans; + while (scanf("%d%d", &n, &m) == 2) + { + if (!n && !m) + break; + n--; + m--; + if (n > m) + n ^= m, m ^= n, n ^= m; + ans = 0; + for (int x = 1; x <= n; x++) + { + for (int y = x; y <= m; y++) + if (gcd[x][y] <= 2) + { + if (x == y) + { + int tmp = (n - x + 1) * (m - y + 1); + if (gcd[x][y] == 1) + ans += tmp; + else + ans -= tmp; + } + else + { + int tmp = (n - x + 1) * (m - y + 1); + if (x <= m && y <= n) + tmp += (m - x + 1) * (n - y + 1); + if (gcd[x][y] == 1) + ans += tmp; + else + ans -= tmp; + } + } + } + ans *= 2; + printf("%lld\n", ans); + } + return 0; +} diff --git a/1394.cpp b/1394.cpp new file mode 100644 index 0000000..ee2dd2c --- /dev/null +++ b/1394.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +int st[1 << 20]; + +int main() +{ + int n, tm, k, i, j; + while (scanf("%d %d %d", &n, &k, &tm) == 3) + { + if (n == 0 && k == 0 && tm == 0) + { + break; + } + int M; + for (M = 1; M < n + 1; M <<= 1) + ; + for (i = 2 * M - 1; i > 0; i--) + { + if (i >= M) + { + st[i] = 1; + } + else + { + st[i] = st[i << 1] + st[i << 1 | 1]; + } + } + int m, last, prev = 0, s; + for (i = 1; i <= n; i++) + { + if (i == 1) + { + m = (tm + prev) % (n - i + 1); + } + else + { + m = (k + prev) % (n - i + 1); + } + if (m == 0) + { + m = n - i + 1; + } + prev = m - 1; + for (s = 1; s < M;) + { + if (st[s << 1] < m) + { + m -= st[s << 1], s = s << 1 | 1; + } + else + { + s = s << 1; + } + } + last = s - M + 1; + while (s) + { + st[s]--; + s >>= 1; + } + } + printf("%d\n", last); + } + return 0; +} diff --git a/1395.cpp b/1395.cpp new file mode 100644 index 0000000..ae093b4 --- /dev/null +++ b/1395.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +const int maxn = 100000 + 10; +struct Edge +{ + int u; + int v; + int w; +} edge[maxn]; +int f[maxn]; +int n, m; +bool cmp(Edge a, Edge b) +{ + return a.w < b.w; +} +int find(int x) +{ + return f[x] == x ? x : f[x] = find(f[x]); +} +int main() +{ + while (cin >> n >> m) + { + if (n == 0 && m == 0) + { + break; + } + int u, v, w; + for (int i = 1; i <= m; i++) + { + cin >> u >> v >> w; + edge[i].u = u; + edge[i].v = v; + edge[i].w = w; + } + int flag = 0; + int ans = 10000000; + int Max = 0, Min = 10000000; + sort(edge + 1, edge + 1 + m, cmp); + for (int i = 1; i <= m; i++) + { + Max = 0; + Min = 10000000; + int k = 0; + for (int j = 0; j <= n; j++) + { + f[j] = j; + } + for (int j = i; j <= m; j++) + { + int x = find(edge[j].u); + int y = find(edge[j].v); + if (x != y) + { + k++; + f[x] = y; + if (edge[j].w > Max) + { + Max = edge[j].w; + } + if (edge[j].w < Min) + { + Min = edge[j].w; + } + } + } + if (k == n - 1 && Max - Min < ans) + { + ans = Max - Min; + flag = 1; + } + } + if (flag) + { + cout << ans << endl; + } + else + { + cout << -1 << endl; + } + } + return 0; +} diff --git a/1396.cpp b/1396.cpp new file mode 100644 index 0000000..7fd202c --- /dev/null +++ b/1396.cpp @@ -0,0 +1,238 @@ +#include + +using namespace std; + +int n, m; +int tt; +const double pi = acos(-1.0); + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; +typedef Point Vector; +struct Line +{ + Point P; + Vector v; + double ang; + Line(){}; + Line(Point P, Vector v) : P(P), v(v) { ang = atan2(v.y, v.x); } + bool operator<(const Line &L) const + { + return ang < L.ang; + } +}; + +Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } +Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } +Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); } +Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); } +bool operator<(Point a, Point b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +const double eps = 1e-8; +int dcmp(double x) +{ + if (fabs(x) < eps) + return 0; + else + return x < 0 ? -1 : 1; +} +bool operator==(Point a, Point b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} + +//平方 +double sqr(double x) +{ + return x * x; +} +double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } +double Area2(Point A, Point B, Point C) +{ + return Cross(B - A, C - A); +} + +//点积 +double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } +//长度 +double Length(Vector A) { return sqrt(Dot(A, A)); } +//夹角 +double Angle(Vector A, Vector B) +{ + return acos(Dot(A, B) / Length(A) / Length(B)); +} +Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) +{ + Vector u = P - Q; + double t = Cross(w, u) / Cross(v, w); + return P + v * t; +} +Vector Normal(const Vector &A) +{//计算å•ä½Â法å‘釠+ double L = Length(A); + return Vector(-A.y / L, A.x / L); +} +double DistanceToline(Point P, Point A, Point B) +{ + Vector v1 = B - A, v2 = P - A; + return fabs(Cross(v1, v2)) / Length(v1); +} + +double DistanceToSegment(Point P, Point A, Point B) +{ + if (A == B) + return Length(P - A); + Vector v1 = B - A, v2 = P - A, v3 = P - B; + if (dcmp(Dot(v1, v2)) > 0) + return Length(v2); + else if (dcmp(Dot(v1, v3)) > 0) + return Length(v3); + else + return fabs(Cross(v1, v2)) / Length(v1); +} + +//线段相交判定 +bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) +{ + double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), + c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); + return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; +} +//线段相交是å¦在端点上 +bool OnSegment(Point p, Point a1, Point a2) +{ + return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; +} + +double PolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 0; i < n - 1; ++i) + { + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} + +int ConvexHull(Point *p, int n, Point *ch)//求凸包 +{ + sort(p, p + n); + n = unique(p, p + n) - p; + int m = 0; + for (int i = 0; i < n; ++i) + { + while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + int k = m; + for (int i = n - 2; i >= 0; i--) + { + while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + if (n > 1) + m--; + return m; +} +double RotatingCalipers(Point *ch, int n)//旋转å¡壳 +{ + int q = 1; + double ans = 0; + for (int i = 0; i < n; i++) + { + while (Cross(ch[i + 1] - ch[i], ch[q + 1] - ch[i]) > Cross(ch[i + 1] - ch[i], ch[q] - ch[i])) + q = (q + 1) % n; + ans = max(ans, max(Length(ch[i] - ch[q]), Length(ch[i + 1] - ch[q + 1]))); + } + return ans; +} + +bool OnLeft(Line L, Point p) +{ + return Cross(L.v, p - L.P) > 0; +} +Point GetIntersection(Line a, Line b) +{ + Vector u = a.P - b.P; + double t = Cross(b.v, u) / Cross(a.v, b.v); + return a.P + a.v * t; +} +int HalfplaneIntersection(Line *L, int n, Point *poly) +{ + sort(L, L + n); + int first, last; + Point *p = new Point[n]; + Line *q = new Line[n]; + q[first = last = 0] = L[0]; + for (int i = 1; i < n; ++i) + { + while (first < last && !OnLeft(L[i], p[last - 1])) + last--; + while (first < last && !OnLeft(L[i], p[first])) + first++; + q[++last] = L[i]; + if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) + { + last--; + if (OnLeft(q[last], L[i].P)) + q[last] = L[i]; + } + if (first < last) + p[last - 1] = GetIntersection(q[last - 1], q[last]); + } + while (first < last && !OnLeft(q[first], p[last - 1])) + last--; + if (last - first <= 1) + return 0; + p[last] = GetIntersection(q[last], q[first]); + + int m = 0; + for (int i = first; i <= last; i++) + poly[m++] = p[i]; + return m; +} +Line L[200]; +Point poly[200]; +Point p[200], ch[200]; +Vector d[200], v[200], v2[200]; + +int main() +{ + //freopen("a.txt","r",stdin); + int n; + while (scanf("%d", &n) == 1 && n) + { + int m, x, y; + for (int i = 0; i < n; i++) + { + scanf("%d%d", &x, &y); + p[i] = Point(x, y); + } + for (int i = 0; i < n; i++) + { + v[i] = p[(i + 1) % n] - p[i]; + v2[i] = Normal(v[i]); + } + double left = 0, right = 20000; + while (right - left > 1e-6) + { + double mid = left + (right - left) / 2; + for (int i = 0; i < n; i++) + L[i] = Line(p[i] + v2[i] * mid, v[i]); + m = HalfplaneIntersection(L, n, poly); + if (!m) + right = mid; + else + left = mid; + } + printf("%.6lf\n", left); + } + return 0; +} diff --git a/1397.cpp b/1397.cpp new file mode 100644 index 0000000..43cc7d1 --- /dev/null +++ b/1397.cpp @@ -0,0 +1,281 @@ +#include + +using namespace std; + +typedef long long LL; + +const int N = 25; +const double eps = 1e-9; + +LL a, m, b, n, C[N][N]; +int hash_[N][N], tot; +long double A[N][N]; + +struct Frac +{ + + LL a, b; + + Frac() + { + a = 0; + b = 1; + } + Frac(LL a, LL b) + { + this->a = a; + this->b = b; + deal(); + } + + void init() + { + a = 0; + b = 1; + } + + LL gcd(LL a, LL b) + { + while (b) + { + LL tmp = a % b; + a = b; + b = tmp; + } + return a; + } + + void deal() + { + LL d = gcd(a, b); + a /= d; + b /= d; + if (b < 0) + { + a = -a; + b = -b; + } + } + + Frac operator+(Frac c) + { + Frac ans; + ans.a = a * c.b + b * c.a; + ans.b = b * c.b; + ans.deal(); + return ans; + } + + Frac operator-(Frac c) + { + Frac ans; + ans.a = a * c.b - b * c.a; + ans.b = b * c.b; + ans.deal(); + return ans; + } + + Frac operator*(Frac c) + { + Frac ans; + ans.a = a * c.a; + ans.b = b * c.b; + ans.deal(); + return ans; + } + + Frac operator/(Frac c) + { + Frac ans; + ans.a = a * c.b; + ans.b = b * c.a; + ans.deal(); + return ans; + } + + void operator+=(Frac c) + { + *this = *this + c; + } + void operator+=(LL c) + { + *this = *this + Frac(c, 1); + } + void operator-=(Frac c) + { + *this = *this - c; + } + void operator*=(Frac c) + { + *this = *this * c; + } + void operator/=(Frac c) + { + *this = *this / c; + } + + bool operator>(Frac c) + { + return a * c.b > b * c.a; + } + bool operator==(Frac c) + { + return a * c.b == b * c.a; + } + bool operator<(Frac c) + { + return !(*this < c && *this == c); + } + bool operator>=(Frac c) + { + return !(*this < c); + } + bool operator<=(Frac c) + { + return !(*this > c); + } + bool operator!=(Frac c) + { + return !(*this == c); + } + bool operator!=(LL c) + { + return *this != Frac(c, 1); + } + + void operator=(LL c) + { + this->a = c; + this->b = 1; + } +}; + +void build() +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + A[i][j] = 0.0f; + } + } + A[0][0] = 1; + for (int i = 1; i <= tot; i++) + { + for (int j = 0; j <= i; j++) + { + int l = j, r = i - j; + long double tmp = C[i][l]; + while (l >= m) + { + tmp *= a; + l -= m; + } + while (r >= n) + { + tmp *= b; + r -= n; + } + A[hash_[l][r]][i] += tmp; + } + } + A[tot][tot] = 1; + A[tot][tot + 1] = 1; + tot++; +} + +void getC() +{ + for (int i = 0; i <= 20; i++) + { + 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]; + } + } +} + +void gethash_() +{ + tot = 0; + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + hash_[i][j] = tot++; + } + } +} + +void print(double x) +{ + char s[100]; + sprintf(s, "%.0lf", x); + if (strcmp(s, "-0") == 0) + { + printf(" %s", s + 1); + } + else + { + printf(" %s", s); + } +} + +void gauss() +{ + for (int i = 0; i < tot; i++) + { + int r; + for (r = i; r < tot; r++) + if (fabs(A[r][i]) > eps) + { + break; + } + if (r == tot) + { + continue; + } + for (int j = i; j <= tot; j++) + { + swap(A[r][j], A[i][j]); + } + for (int j = 0; j < tot; j++) + { + if (i == j) + { + continue; + } + if (fabs(A[j][i]) >= eps) + { + double tmp = A[j][i] / A[i][i]; + for (int k = i; k <= tot; k++) + { + A[j][k] -= tmp * A[i][k]; + } + } + } + } + printf("1"); + for (int i = tot - 2; i >= 0; i--) + { + print(A[i][tot] / A[i][i]); + } + printf("\n"); +} + +int main() +{ + getC(); + while (~scanf("%lld%lld%lld%lld", &a, &m, &b, &n)) + { + if (!a && !m && !b && !n) + { + break; + } + gethash_(); + build(); + gauss(); + } + return 0; +} diff --git a/1398.cpp b/1398.cpp new file mode 100644 index 0000000..23531e7 --- /dev/null +++ b/1398.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define min(a, b) ((a) < (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define INF 0x3f3f3f3f + +bool eq(double a, double b) +{ + return fabs(a - b) < 1e-9; +} +const int N = 100005; + +int t, n, en; +double w, h, l, r; + +struct E +{ + double t; + int flag; +} e[N * 2]; + +bool cmp(E a, E b) +{ + if (!eq(a.t, b.t)) + return a.t < b.t; + return a.flag < b.flag; +} + +void g(double x, double xd, double w) +{ + if (eq(xd, 0)) + { + if (!(x > 0 && x < w)) + r = 0; + } + else if (xd > 0) + { + l = max(l, -x / xd); + r = min(r, (w - x) / xd); + } + else + { + l = max(l, (w - x) / xd); + r = min(r, -x / xd); + } +} + +void init() +{ + en = 0; + scanf("%lf%lf%d", &w, &h, &n); + for (int i = 0; i < n; i++) + { + double x, y, xd, yd; + scanf("%lf%lf%lf%lf", &x, &y, &xd, &yd); + l = 0, r = INF; + g(x, xd, w); + g(y, yd, h); + if (l < r) + { + e[en].t = l; + e[en++].flag = 1; + e[en].t = r; + e[en++].flag = -1; + } + } +} + +int solve() +{ + int ans = 0, num = 0; + sort(e, e + en, cmp); + for (int i = 0; i < en; i++) + { + num += e[i].flag; + ans = max(ans, num); + } + return ans; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/1399.cpp b/1399.cpp new file mode 100644 index 0000000..257c3d8 --- /dev/null +++ b/1399.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +const int MAXL = 500010; +const int MAXN = 26; + +bool exist[MAXL]; +int vis[MAXL]; +int go[MAXL][MAXN], fail[MAXL]; +int dp[MAXL], pre[MAXL]; +int n, cnt, s; + +int new_node() +{ + memset(go[cnt], 0, sizeof(go[cnt])); + exist[cnt] = false; + return cnt++; +} + +void init() +{ + memset(go[0], 0, sizeof(go[0])); + exist[0] = false; + cnt = 1; +} + +void Insert(const char *s) +{ + int now = 0; + for (int i = 0, o; s[i]; ++i, now = go[now][o]) + { + o = s[i] - 'A'; + if (go[now][o] == 0) + go[now][o] = new_node(); + } + exist[now] = true; +} + +void build() +{ + queue q; + while (!q.empty()) + q.pop(); + for (int i = 0; i < n; ++i) + { + if (go[0][i]) + { + fail[go[0][i]] = 0; + q.push(go[0][i]); + } + } + while (!q.empty()) + { + int x = q.front(); + q.pop(); + for (int i = 0; i < n; ++i) + { + int &y = go[x][i]; + if (!y) + y = go[fail[x]][i]; + else + { + fail[y] = go[fail[x]][i]; + exist[y] |= exist[fail[y]]; + q.push(y); + } + } + } +} + +int solve(int now) +{ + if (vis[now] == 1) + return -1; + if (vis[now] == 2) + return dp[now]; + vis[now] = 1; + dp[now] = 0; + for (int i = n - 1; i >= 0; --i) + { + if (!exist[go[now][i]]) + { + int res = solve(go[now][i]); + if (res == -1) + return -1; + if (res + 1 > dp[now]) + { + dp[now] = res + 1; + pre[now] = i; + } + } + } + vis[now] = 2; + return dp[now]; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &s); + init(); + char st[100]; + for (int i = 0; i < s; ++i) + { + scanf("%s", st); + Insert(st); + } + build(); + memset(vis, 0, sizeof(vis)); + memset(pre, -1, sizeof(pre)); + memset(dp, 0, sizeof(dp)); + int ret = solve(0); + if (ret == -1 || ret == 0) + { + puts("No"); + } + else + { + for (int i = 0, now = 0; i < ret; ++i) + { + putchar(pre[now] + 'A'); + now = go[now][pre[now]]; + } + puts(""); + } + } + return 0; +} diff --git a/140.cpp b/140.cpp new file mode 100644 index 0000000..c859ee7 --- /dev/null +++ b/140.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define MAXN 30 + +char Have[MAXN], Rec[MAXN], input[10000], link[MAXN][MAXN], temp[10000], F[MAXN]; +int tc, min_, H[MAXN], A[MAXN]; + +void Sep() +{ + char *p; + int i, j, k = 0, u, v; + for (i = 0; input[i]; i++) + { + if (input[i] != ' ') + { + temp[k++] = input[i]; + } + } + temp[k] = '\0'; + p = strtok(temp, ";"); + while (p) + { + u = p[0] - 'A'; + Have[u] = 1; + for (j = 2; p[j]; j++) + { + v = p[j] - 'A'; + link[u][v] = link[v][u] = 1; + Have[v] = 1; + } + p = strtok(NULL, ";"); + } +} +void Record() +{ + int i, t = 0, u, j, v, m = 0; + for (i = 1; i < tc; i++) + { + u = A[i]; + for (j = tc; j > i; j--) + { + v = A[j]; + if (link[u][v] || link[v][u]) + { + break; + } + } + v = j - i; + if (v > m) + m = v; + } + if (m >= min_) + { + return; + } + min_ = m; + for (i = 1; i <= tc; i++) + { + Rec[i] = A[i]; + } +} +void recur(int n, int level) +{ + int i; + A[level] = H[n]; + if (level == tc) + { + Record(); + return; + } + F[n] = 1; + for (i = 0; i < tc; i++) + { + if (F[i] == 0) + { + recur(i, level + 1); + } + } + F[n] = 0; +} +void Cal() +{ + int i, j; + min_ = 1000000000; + tc = 0; + for (i = 0; i < 26; i++) + { + if (Have[i]) + { + H[tc++] = i; + } + } + for (i = 0; i < tc; i++) + { + recur(i, 1); + } + for (i = 1; i <= tc; i++) + { + printf("%c ", Rec[i] + 'A'); + } + printf("-> %d\n", min_); + for (i = 0; i < MAXN; i++) + { + Have[i] = 0; + for (j = 0; j < MAXN; j++) + { + link[i][j] = 0; + } + } +} + +int main() +{ + while (scanf("%s", input) != EOF) + { + if (!strcmp(input, "#")) + { + break; + } + Sep(); + Cal(); + } + return 0; +} diff --git a/1400.cpp b/1400.cpp new file mode 100644 index 0000000..709fae8 --- /dev/null +++ b/1400.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +const double eps = 1e-6; +const double offset = 2000; +const double PI = acos(-1.0); +const int MAXN = 500005; +const int MAXM = 1 << 20; +const LL INF = 0x3f3f3f3f3f3f3f3fLL; +const int MOD = 1000000009; + +int a[MAXN]; +LL sum[MAXN]; +int n, m; + +struct NODE +{ + int left, right; + LL val; + NODE() + { + left = right = val = -1; + } + NODE(int _left, int _right) : left(_left), right(_right) + { + val = sum[_right] - sum[_left - 1]; + } + bool operator<(const NODE &o) const + { + if (val == o.val) + { + if (left == o.left) + { + return right > o.right; + } + return left > o.left; + } + return val < o.val; + } +}; + +struct TREE +{ + int left, right; + NODE l, r, m; +} tree[MAXN << 2]; + +void build(int p, int left, int right) +{ + tree[p].left = left; + tree[p].right = right; + if (left == right) + { + tree[p].m = tree[p].l = tree[p].r = NODE(left, right); + return; + } + int mid = (left + right) >> 1; + build(p << 1, left, mid); + build(p << 1 | 1, mid + 1, right); + tree[p].m = max(tree[p << 1].m, tree[p << 1 | 1].m); + tree[p].m = max(tree[p].m, NODE(tree[p << 1].r.left, tree[p << 1 | 1].l.right)); + tree[p].l = max(tree[p << 1].l, NODE(tree[p << 1].left, tree[p << 1 | 1].l.right)); + tree[p].r = max(tree[p << 1 | 1].r, NODE(tree[p << 1].r.left, tree[p << 1 | 1].right)); +} + +NODE cal(int p, int left, int right, NODE &l, NODE &r) +{ + if (left == tree[p].left && right == tree[p].right) + { + l = tree[p].l; + r = tree[p].r; + return tree[p].m; + } + int mid = (tree[p].left + tree[p].right) >> 1; + NODE ret; + ret.val = -INF; + NODE ll, lr, rl, rr; + if (right <= mid) + { + ret = max(ret, cal(p << 1, left, right, ll, lr)); + if (left <= tree[p].left) + { + l = ll; + } + } + else if (left > mid) + { + ret = max(ret, cal(p << 1 | 1, left, right, rl, rr)); + if (right >= tree[p].right) + { + r = rr; + } + } + else + { + ret = max(ret, cal(p << 1, left, mid, ll, lr)); + ret = max(ret, cal(p << 1 | 1, mid + 1, right, rl, rr)); + if (rl.right != -1 && lr.left != -1) + { + ret = max(ret, NODE(lr.left, rl.right)); + } + if (left <= tree[p].left) + { + l = max(ll, NODE(tree[p].left, rl.right)); + } + if (right >= tree[p].right) + { + r = max(rr, NODE(lr.left, tree[p].right)); + } + } + return ret; +} + +void solved(int cas) +{ + //scanf ("%d %d", &n, &m); + for (int i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + sum[i] = sum[i - 1] + a[i]; + } + build(1, 1, n); + printf("Case %d:\n", cas); + int x, y; + while (m--) + { + scanf("%d %d", &x, &y); + NODE l, r; + NODE ans = cal(1, x, y, l, r); + printf("%d %d\n", ans.left, ans.right); + } +} + +int main() +{ + int cas = 1; + while (scanf("%d %d", &n, &m) != EOF) + { + solved(cas++); + } + return 0; + int T = 1; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + solved(i); + } + return 0; +} diff --git a/1401.cpp b/1401.cpp new file mode 100644 index 0000000..8319f79 --- /dev/null +++ b/1401.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const int MOD = 20071027; +const int MAXN = 500010; + +struct Trie {int ch[MAXN][30], val[MAXN], sz;} trie; + +char str[MAXN]; +int dp[MAXN], start, num; + +int GetPos(char c) +{ + return c - 'a'; +} +void insert(char *s) +{ + int u = 0, len = strlen(s); + for (int i = 0; i < len; i++) + { + int c = GetPos(s[i]); + if (!trie.ch[u][c]) + { + memset(trie.ch[trie.sz], 0, sizeof(trie.ch[trie.sz])); + trie.val[trie.sz] = 0; + trie.ch[u][c] = trie.sz++; + } + u = trie.ch[u][c]; + } + trie.val[u] = 1; +} +void find(char *str) +{ + int u = 0; + for (int i = 0; str[i]; i++) + { + int c = GetPos(str[i]); + if (trie.ch[u][c]) + { + u = trie.ch[u][c]; + if (trie.val[u]) + { + num = (num + dp[start + i + 1]) % MOD; + } + } + else + { + return; + } + } +} + +int main() +{ + int t = 1; + while (scanf("%s", str) != EOF) + { + memset(trie.ch[0], 0, sizeof(trie.ch[0])); + trie.sz = 1; + int s, ans = 0; + scanf("%d", &s); + char ita[3001]; + while (s--) + { + scanf("%s", ita); + insert(ita); + } + int len = strlen(str); + dp[len] = 1; + for (int i = len - 1; i >= 0; i--) + { + num = 0; + start = i; + find(&str[i]); + dp[i] = num; + } + printf("Case %d: %d\n", t++, dp[0]); + } + return 0; +} diff --git a/1402.cpp b/1402.cpp new file mode 100644 index 0000000..662257f --- /dev/null +++ b/1402.cpp @@ -0,0 +1,267 @@ +#include + +using namespace std; + +// Unrolled Linked List +const int MAXPILE = 512; +const int MAXN = 131072; +class UnrolledLinkedList +{ +public: + struct Node + { + int v[MAXPILE], vn; + int rev_label, pid; + Node *next; + Node() + { + vn = rev_label = 0; + next = NULL; + } + void relax() + { + if (rev_label) + { + for (int i = 0, j = vn - 1; i < j; i++, j--) + swap(v[i], v[j]); + rev_label = 0; + } + } + }; + Node *head; + int PSIZE, pid; + int e_pos[MAXN], e_id[MAXN]; + void init() + { + free(); + head = NULL, PSIZE = MAXPILE / 2; + pid = 0; + } + Node *getNode() + { + Node *p = new Node(); + p->pid = pid++; + return p; + } + void remap(Node *u) + { + for (int i = 0; i < u->vn; i++) + e_pos[u->v[i]] = i, e_id[u->v[i]] = u->pid; + } + int find(int e_val) + { + int pid = e_id[e_val]; + int sum_element = 0; + Node *u, *v; + + for (u = head; u != NULL && u->pid != pid; u = u->next) + sum_element += u->vn; + + // printf("find %d - %d\n", e_val, pid); + assert(u != NULL); + if (u->rev_label) + return sum_element + u->vn - 1 - e_pos[e_val]; + else + return sum_element + e_pos[e_val]; + } + void set(int A[], int n) + { + init(); + Node *u, *v; + + head = getNode(); + u = head, v = NULL; + PSIZE = min(PSIZE, (int)sqrt(n)); + for (int i = 0; i < n; i++) + { + if (u->vn == PSIZE) + { + u->next = getNode(); + v = u, u = u->next; + } + u->v[u->vn++] = A[i]; + } + for (u = head; u != NULL; u = u->next) + remap(u); + } + void shrinkList() + { + Node *u, *v; + u = head; + for (u = head; u != NULL && u->next != NULL; u = u->next) + { + if (u->vn + u->next->vn <= 2 * PSIZE) + {// merge + v = u->next; + u->relax(), v->relax(); + for (int i = u->vn, j = 0; j < v->vn; i++, j++) + u->v[i] = v->v[j]; + u->vn += v->vn; + remap(u); + u->next = v->next; + delete v; + } + } + } + void splitNode(Node *u, int left_size) + {// length(left) = v + Node *v = getNode(); + u->relax(); + v->next = u->next; + u->next = v; + v->vn = u->vn - left_size; + for (int i = left_size, j = 0; i < u->vn; i++, j++) + v->v[j] = u->v[i]; + u->vn = left_size; + remap(u), remap(v); + } + void reverse(int l, int r) + {// [l, r] = [0, n-1] + Node *lptr, *rptr, *u, *v; + Node *lpre, *rpre, *rnext; + int sum_element = 0; + + u = head, v = NULL; + for (u = head, v = NULL; u != NULL; v = u, u = u->next) + { + if (sum_element < l && l < sum_element + u->vn) + splitNode(u, l - sum_element);// left[...l-1], right[l...] + if (sum_element <= r && r < sum_element + u->vn) + splitNode(u, r - sum_element + 1); + + if (sum_element == l) + lptr = u, lpre = v; + if (sum_element + u->vn - 1 == r) + rptr = u, rpre = v; + + sum_element += u->vn; + } + + // debug(); + + rnext = rptr->next; + stack stk; + for (u = lptr; u != rnext; u = u->next) + { + u->rev_label = !u->rev_label; + stk.push(u); + } + + if (lpre == NULL) + { + head = stk.top(); + u = head, stk.pop(); + while (!stk.empty()) + { + u->next = stk.top(), stk.pop(); + u = u->next; + } + u->next = rnext; + } + else + { + u = lpre; + while (!stk.empty()) + { + u->next = stk.top(), stk.pop(); + u = u->next; + } + u->next = rnext; + } + + shrinkList(); + } + void debug() + { + Node *u = head; + while (u != NULL) + { + printf("%d : %d, ", u->pid, u->rev_label); + for (int i = 0; i < u->vn; i++) + printf("%d ", u->v[i]); + puts(""); + u = u->next; + } + puts("===="); + } + void free() + { + Node *u = head, *v = NULL; + while (u != NULL) + { + v = u, u = u->next; + delete v; + } + } +} g; + +int A[MAXN]; +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + vector> B; + for (int i = 0; i < n; i++) + { + scanf("%d", &A[i]); + B.push_back(make_pair(A[i], i)); + } + + sort(B.begin(), B.end()); + + map, int> C; + for (int i = 0; i < B.size(); i++) + C[B[i]] = i; + + for (int i = 0; i < n; i++) + A[i] = C[make_pair(A[i], i)]; + + g.set(A, n); + // g.debug(); + + vector ret; + for (int i = 0; i < n; i++) + { + int pos = g.find(i); + g.reverse(i, pos); + ret.push_back(pos); + // g.debug(); + } + + for (int i = 0; i < n; i++) + printf("%d%c", ret[i] + 1, i == n - 1 ? '\n' : ' '); + } + return 0; +} +/* +6 +3 4 5 1 6 2 +4 +3 3 2 1 +0 + +10 +5 18 19 12 7 12 0 2 11 9 +1 +4 +19 +5 17 8 10 13 18 10 5 3 15 2 19 12 10 2 14 18 0 6 +12 +15 13 7 14 15 7 12 15 4 10 6 3 +15 +18 7 5 6 5 5 10 9 2 4 9 10 7 13 19 +5 +3 4 1 1 3 +6 +8 0 6 2 6 16 +7 +17 5 12 1 3 9 13 +1 +8 +10 +15 19 17 19 17 18 2 12 0 10 +10 +5 1 14 6 7 12 15 17 5 11 +0 +*/ diff --git a/1404.cpp b/1404.cpp new file mode 100644 index 0000000..d99abec --- /dev/null +++ b/1404.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +typedef long long LL; +#define N 10000005 +bool f[N]; +int n; +int prime[N], tot; +vector p; +void init() +{ + n = 32000; + int i, j, x; + for (i = 2; i <= n; ++i) + { + if (!f[i]) + { + prime[tot++] = i; + } + for (j = 0; j < tot; ++j) + { + x = i * prime[j]; + if (x > n) + { + break; + } + f[x] = 1; + if (i % prime[j] == 0) + { + break; + } + } + } +} +int a, b, k, s; +void work() +{ + int i, j, x; + memset(f, 0, sizeof(f)); + p.clear(); + scanf("%d%d%d%d", &a, &b, &k, &s); + for (i = 0; i < tot && prime[i] * prime[i] <= b; ++i) + { + x = prime[i], j = (x - a % x) % x; + if (x == a + j) + { + j += x; + } + for (; j <= b - a; j += x) + { + f[j] = 1; + } + } + for (i = 0; i <= b - a; ++i) + if (a + i > 1 && !f[i]) + { + p.push_back(a + i); + } + int ans = 0; + for (i = 0; i + k - 1 < p.size(); ++i) + if (p[i + k - 1] - p[i] == s) + { + ++ans; + } + printf("%d\n", ans); +} + +int main() +{ + init(); + int _; + scanf("%d", &_); + while (_--) + { + work(); + } + return 0; +} diff --git a/1406.cpp b/1406.cpp new file mode 100644 index 0000000..c555781 --- /dev/null +++ b/1406.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define low(x) ((x) & (-x)) + +int c[16][(1 << 16) + 10]; + +inline void add(int w, int x, int d) +{ + c[w][x]++; +} + +inline void init() +{ + for (int i = 0; i < 16; i++) + { + for (int j = 1; j < (1 << i + 1); j++) + { + c[i][j] += c[i][j - 1]; + } + } +} + +inline int query(int w, int x) +{ + return c[w][x]; +} + +int n, kase; + +int main() +{ + while (~scanf("%d", &n) && ~n) + { + memset(c, 0, sizeof(c)); + for (int i = 0, x; i < n; i++) + { + scanf("%d", &x); + for (int j = 15; j >= 0; j--) + { + add(j, x & ((1 << j + 1) - 1), 1); + /* + 将æ¯Â个数低j+1ä½Â的值加1 + 因为如果对æ¯Âä½Â分别求解,高ä½Â是无用的 + */ + } + } + init(); + int mk = 0, x; + long long ans = 0; + char op; + while (~scanf(" %c", &op) && op != 'E') + { + scanf("%d", &x); + if (op == 'C') + { + mk = (mk + x) & ((1 << 16) - 1); + } + else + { + /* + 1< + +using namespace std; + +#define maxn 510 +#define inf 1 << 26 + +struct Edge +{ + int from, to, len; + int next; + void assign(int a, int b, int c, int d) + { + from = a, to = b, len = c, next = d; + } +} edges[maxn * 2]; +int n; +int dp[maxn][maxn][2]; +int cnt; +int head[maxn]; +int son[maxn]; + +void init() +{ + int i, j; + cnt = 0; + memset(head, -1, sizeof(head)); + for (i = 0; i <= n; i++) + { + for (j = 0; j <= n; j++) + { + dp[i][j][0] = dp[i][j][1] = inf; + } + } +} + +void addedge(int x, int y, int z) +{ + edges[cnt].assign(x, y, z, head[x]); + head[x] = cnt++; +} + +void dfs(int root, int fa) +{ + // cout< 1; j--) + { + for (k = 1; k <= j && k <= son[e.to]; k++) + { + dp[root][j][0] = min(dp[root][j][0], dp[root][j - k][0] + dp[e.to][k][0] + e.len * 2); + dp[root][j][1] = min(dp[root][j][1], dp[root][j - k][0] + dp[e.to][k][1] + e.len * 1); + dp[root][j][1] = min(dp[root][j][1], dp[root][j - k][1] + dp[e.to][k][0] + e.len * 2); + } + } + } +} + +int main() +{ + int i, j; + int test = 0; + while (scanf("%d", &n) == 1 && n) + { + init(); + for (i = 0; i < n - 1; i++) + { + int a, b, c; + scanf("%d %d %d", &a, &b, &c); + addedge(a, b, c); + addedge(b, a, c); + } + dfs(0, -1); + int m; + scanf("%d", &m); + // cout< + +using namespace std; + +int n, m; +int f[2][1024 * 1025]; + +vector vis[2]; + +const int INF = 0x3f3f3f3f; + +int mar[52][20]; + +#define pb push_back + +/*int get(int v, int state) { + return (state >> ((m - v) * 2)) & 3; +} + +int change(int v, int d, int state) { + return (((state >> ((m - v) * 2) >> 2 << 2) | d) << ((m - v) * 2)) | (state & ((1 << ((m - v) * 2)) - 1)); +}*/ +int get(int v, int state) +{ + return (state >> ((m - v + 1) * 2)) & 3; +} + +int change(int v, int d, int state) +{ + return (((state >> ((m - v + 1) * 2) >> 2 << 2) | d) << ((m - v + 1) * 2)) | (state & ((1 << ((m - v + 1) * 2)) - 1)); +} + +void update(int state, int now, int d) +{ + if (f[now][state] == INF) + { + vis[now].pb(state); + } + f[now][state] = min(f[now][state], d); +} + +int main() +{ + // freopen("o", "w", stdout); + int cas = 0; + while (scanf("%d%d", &n, &m) == 2 && n && m) + { + cas++; + printf("Case %d: ", cas); + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= m; ++j) + { + scanf("%d", &mar[i][j]); + } + int now = 0; + int pre = 1; + memset(f, 0x3f, sizeof(f)); + f[0][0] = 0; + vis[0].clear(); + vis[1].clear(); + vis[now].pb(0); + for (int i = 1; i <= n; ++i) + { + for (int j = 1; j <= m; ++j) + { + now = 1 - now; + pre = 1 - pre; + for (int k = 0; k < (int)vis[now].size(); ++k) + { + f[now][vis[now][k]] = INF; + } + vis[now].clear(); + for (int k = 0; k < (int)vis[pre].size(); ++k) + { + int kk = vis[pre][k]; + int tot = f[pre][kk]; + int k1 = get(j + 1, kk); + int k2 = get(j, kk); + if (mar[i][j] == 0) + { + update(change(j, 0, change(j + 1, 0, kk)), now, tot); + continue; + } + update(change(j + 1, 1, change(j, 0, kk)), now, tot + (mar[i][j - 1] >= mar[i][j] || k2 != 1)); + update(change(j + 1, 2, change(j, 0, kk)), now, tot + (mar[i][j - 1] <= mar[i][j] || k2 != 2)); + update(change(j + 1, 0, change(j, 1, kk)), now, tot + (mar[i - 1][j] >= mar[i][j] || k1 != 1)); + update(change(j + 1, 0, change(j, 2, kk)), now, tot + (mar[i - 1][j] <= mar[i][j] || k1 != 2)); + } + } + now = 1 - now; + pre = 1 - pre; + for (int k = 0; k < (int)vis[now].size(); ++k) + { + f[now][vis[now][k]] = INF; + } + vis[now].clear(); + for (int k = 0; k < (int)vis[pre].size(); ++k) + { + int kk = vis[pre][k]; + int tot = f[pre][kk]; + update(kk >> 2, now, tot); + } + } + int ans = INF; + for (int k = 0; k < (int)vis[now].size(); ++k) + { + ans = min(ans, f[now][vis[now][k]]); + } + printf("%d\n", ans); + } +} diff --git a/141.cpp b/141.cpp new file mode 100644 index 0000000..32884fd --- /dev/null +++ b/141.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define maxn 52 + +char B[maxn][maxn]; +int N; +map M; + +void Process(int ply) +{ + int i, j, k, l; + string a, b, c; + a = b = c = ""; + for (i = 1, k = N; i <= N; i++, k--) + { + for (j = 1, l = N; j <= N; j++, l--) + { + if (B[j][k] == 1) + { + a += i; + a += j; + } + if (B[k][l] == 1) + { + b += i; + b += j; + } + if (B[i][l] == 1) + { + c += i; + c += j; + } + } + } + M[a] = ply; + M[b] = ply; + M[c] = ply; +} +string GetString() +{ + string ss = ""; + int i; + for (i = 1; i <= N; i++) + for (int j = 1; j <= N; j++) + { + if (B[i][j] == 1) + { + ss += i; + ss += j; + } + } + return ss; +} +void Cal() +{ + int res = 0, win, i, x, y, mv; + char move; + string ss; + for (i = 0; i < 2 * N; i++) + { + cin >> x >> y >> move; + if (res) + continue; + if (move == '+') + B[x][y] = 1; + else + B[x][y] = 0; + ss = GetString(); + if (M[ss] > 0) + { + res = 1; + win = M[ss]; + mv = i + 1; + } + else + { + M[ss] = i % 2 + 1; + Process(i % 2 + 1); + } + } + if (res) + { + if (win == 1) + win = 2; + else + win = 1; + cout << "Player " << win << " wins on move " << mv << endl; + } + else + cout << "Draw\n"; + M.clear(); +} + +int main() +{ + int i, j; + while (cin >> N && N) + { + Cal(); + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + B[i][j] = 0; + } + } + } + return 0; +} diff --git a/1411.cpp b/1411.cpp new file mode 100644 index 0000000..29779d7 --- /dev/null +++ b/1411.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +struct Point +{ + int x, y; +}; +double get_dis(Point a, Point b) +{ + return sqrt((1.0 * a.x - b.x) * (1.0 * a.x - b.x) + (1.0 * a.y - b.y) * (1.0 * a.y - b.y)); +} +int N, Left[110]; +bool S[110], T[110]; +double W[110][110], LX[110], LY[110]; +Point PX[110], PY[110]; +bool match(int i) +{ + S[i] = true; + for (int j = 1; j <= N; j++) + if (fabs(LX[i] + LY[j] - W[i][j]) < 1e-7 && !T[j]) + { + T[j] = true; + if (Left[j] == 0 || match(Left[j])) + { + Left[j] = i; + return true; + } + } + return false; +} +void update() +{ + double a = 1 << 30; + for (int i = 1; i <= N; i++) + if (S[i]) + { + for (int j = 1; j <= N; j++) + if (!T[j]) + { + a = min(a, LX[i] + LY[j] - W[i][j]); + } + } + for (int i = 1; i <= N; i++) + { + if (S[i]) + { + LX[i] -= a; + } + if (T[i]) + { + LY[i] += a; + } + } +} +void KM() +{ + for (int i = 1; i <= N; i++) + { + Left[i] = LX[i] = LY[i] = 0; + for (int j = 1; j <= N; j++) + { + if (j == 1) + { + LX[i] = W[i][j]; + } + LX[i] = max(LX[i], W[i][j]); + } + } + for (int i = 1; i <= N; i++) + { + for (;;) + { + for (int j = 1; j <= N; j++) + { + S[j] = T[j] = 0; + } + if (match(i) == true) + { + break; + } + else + { + update(); + } + } + } +} +int main() +{ + while (scanf("%d", &N) != EOF) + { + for (int i = 1; i <= N; i++) + { + scanf("%d%d", &PX[i].x, &PX[i].y); + } + for (int i = 1; i <= N; i++) + { + scanf("%d%d", &PY[i].x, &PY[i].y); + } + for (int i = 1; i <= N; i++) + for (int j = 1; j <= N; j++) + { + W[i][j] = -1.0 * get_dis(PX[i], PY[j]); + } + KM(); + int Right[110]; + for (int i = 1; i <= N; i++) + { + Right[Left[i]] = i; + } + for (int i = 1; i <= N; i++) + { + printf("%d\n", Right[i]); + } + } + return 0; +} diff --git a/1412.cpp b/1412.cpp new file mode 100644 index 0000000..8f1dd05 --- /dev/null +++ b/1412.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +//Template updates date: 20140316 +#define esp 1e-6 +#define pi acos(-1.0) +#define inf 0x0f0f0f0f +#define pb push_back +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 +#define lowbit(x) (x & (-x)) +#define mp(a, b) make_pair((a), (b)) +#define bit(k) (1 << (k)) +#define in freopen("in.txt", "r", stdin); +#define out freopen("solve_out.txt", "w", stdout); + +#define bug puts("********))))))"); +#define inout in out + +#define SET(a, v) memset(a, (v), sizeof(a)) +#define SORT(a) sort((a).begin(), (a).end()) +#define REV(a) reverse((a).begin(), (a).end()) +#define READ(a, n) \ + { \ + REP(i, n) \ + cin >> (a)[i]; \ + } +#define REP(i, n) for (int i = 0; i < (n); i++) +#define Rep(i, base, n) for (int i = base; i < n; i++) +#define REPS(s, i) for (int i = 0; s[i]; i++) +#define pf(x) ((x) * (x)) +#define mod(n) ((n)) +#define Log(a, b) (log((double)b) / log((double)a)) +#define Srand() srand((int)time(0)) +#define random(number) (rand() % number) +#define random_range(a, b) (int)(((double)rand() / RAND_MAX) * (b - a) + a) + +typedef long long LL; +typedef unsigned long long ULL; +typedef vector VI; +typedef pair PII; +typedef vector VII; +typedef vector VIII; +typedef VI::iterator IT; +typedef map Mps; +typedef map Mpi; +typedef map Mpii; +typedef map Mpiii; + +const int maxn = 110; +const int maxm = 50000 + 100; +long dp[maxn][maxm]; +Mpi M; + +struct NODE +{ + char s[10]; + int cap; + long pri[maxn]; +} name[10]; + +struct Transition +{ + int from; + int to; + int buy; + int sell; + Transition() {} + Transition(int from, int to, int buy, int sell) : from(from), to(to), buy(buy), sell(sell) {} + long cost(int days) + { + return (buy >= 0 ? name[buy].pri[days] : 0) + + (sell >= 0 ? -name[sell].pri[days] : 0); + } + void toString() + { + if (buy >= 0) + { + printf("BUY %s\n", name[buy].s); + } + else if (sell >= 0) + { + printf("SELL %s\n", name[sell].s); + } + else + { + puts("HOLD"); + } + } +} by[maxn][maxm]; + +vector transition; +long c; +int n, m, kk, lastcode; + +int encode(int Count[]) +{ + int code = 0; + REP(i, n) + code = code * (kk + 1) + Count[i]; + if (!M.count(code)) + { + M[code] = lastcode++; + } + return M[code]; +} + +int Generate(int Count[], int c) +{ + int code = encode(Count); + if (code == lastcode - 1) + { + transition.pb(Transition(code, code, -1, -1)); + REP(i, n) + { + if (Count[i] < name[i].cap && c < kk) + { + Count[i]++; + int tcode = Generate(Count, c + 1); + Count[i]--; + transition.pb(Transition(code, tcode, i, -1)); + } + if (Count[i] > 0) + { + Count[i]--; + int tcode = Generate(Count, c - 1); + transition.pb(Transition(code, tcode, -1, i)); + Count[i]++; + } + } + } + return code; +} +void print_ans(int s, int st) +{ + if (s > 1) + { + print_ans(s - 1, by[s][st].from); + } + by[s][st].toString(); +} +void solve() +{ + int Count[10]; + SET(Count, 0); + int root = Generate(Count, 0); + SET(dp, -1); + dp[0][root] = c; + for (int i = 0; i < m; i++) + { + REP(j, (int)transition.size()) + { + int from = transition[j].from; + int to = transition[j].to; + if (dp[i][from] >= 0) + { + long Cost = dp[i][from] - transition[j].cost(i); + if (Cost > dp[i + 1][to]) + { + dp[i + 1][to] = Cost; + by[i + 1][to] = transition[j]; + } + } + } + } + printf("%.2f\n", (double)dp[m][root] / 10000); + print_ans(m, root); +} + +int main() +{ + int kase = 1; + double tmp; + while (scanf("%lf%d%d%d", &tmp, &m, &n, &kk) != EOF) + { + if (kase != 1) + { + puts(""); + } + kase++; + transition.clear(); + M.clear(); + lastcode = 0; + c = (long)(tmp * 10000); + REP(i, n) + { + int size; + scanf("%s%d%d", name[i].s, &size, &name[i].cap); + REP(j, m) + { + scanf("%lf", &tmp); + name[i].pri[j] = (long)(tmp * 10000 * size); + } + } + solve(); + } + return 0; +} diff --git a/1413.cpp b/1413.cpp new file mode 100644 index 0000000..b756477 --- /dev/null +++ b/1413.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +const int L = 0, R = 1; +int n, k; +double p[55], pn[55][55][2][2], px[55][55][2]; + +int main() +{ + while (scanf("%d%d", &n, &k) != EOF) + { + for (int i = 0; i < n - 1; i++) + { + scanf("%lf", &p[i]); + } + + for (int i = 0; i < 55; i++) + { + for (int j = 0; j < 55; j++) + { + pn[i][j][L][L] = pn[i][j][R][L] = pn[i][j][R][R] = pn[i][j][L][R] = px[i][j][L] = px[i][j][R] = 0; + } + } + + for (int i = 0; i < n - 1; i++) + { + pn[i][i][L][L] = 1.0f - p[i]; + pn[i][i][R][L] = 1.0f - p[i]; + pn[i][i][R][R] = p[i]; + pn[i][i][L][R] = p[i]; + } + + for (int d = 2; d < n; d++) + { + for (int i = 0; i + d < n; i++) + { + int j = i + d - 1; + pn[i][j][L][L] = (1.0f - p[i]) / ((1.0f - p[i]) + p[i] * pn[i + 1][j][L][R]); + pn[i][j][R][L] = (1.0f - p[j]) * pn[i][j - 1][R][L] / ((1.0f - p[j]) * pn[i][j - 1][R][L] + p[j]); + pn[i][j][R][R] = p[j] / (p[j] + (1.0f - p[j]) * pn[i][j - 1][R][L]); + pn[i][j][L][R] = p[i] * pn[i + 1][j][L][R] / (1.0f - p[i] + p[i] * pn[i + 1][j][L][R]); + } + } + + px[k - 1][k - 1][L] = 1.0f; + + for (int d = 2; d < n; d++) + { + for (int i = 0; i + d < n; i++) + { + int j = i + d - 1; + px[i][j][L] = px[i + 1][j][L] * pn[i + 1][j][L][L] + px[i + 1][j][R] * pn[i + 1][j][R][L]; + px[i][j][R] = px[i][j - 1][L] * pn[i][j - 1][L][R] + px[i][j - 1][R] * pn[i][j - 1][R][R]; + } + } + + printf("%.10lf\n", px[0][n - 2][L] + px[0][n - 2][R]); + } + return 0; +} diff --git a/1414.cpp b/1414.cpp new file mode 100644 index 0000000..230bc94 --- /dev/null +++ b/1414.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define mem(name, value) memset(name, value, sizeof(name)) +#define FOR(i, n) for (int i = 1; i <= n; i++) + +const int maxn = 40; + +char s[maxn]; + +int v[maxn], x, y; + +void init() +{ + mem(v, 0); + for (int i = 0; i < 6; i++) + { + scanf("%s", s); + int u = s[0] - 'A' + 1; + int k = s[1] - 'A' + 1; + if (v[u]) + { + continue; + } + v[u] = k; + } + if (v[2] != 1 && v[3] != 1) + { + x = 3; + y = 0; + } + else if (v[v[1]] == 1) + { + x = 3; + y = 2; + } + else + { + x = 2; + y = 1; + } +} + +long long d[maxn]; + +int main() +{ + int n; + while (~scanf("%d", &n)) + { + init(); + d[1] = 1; + for (int i = 2; i <= n; i++) + { + d[i] = d[i - 1] * x + y; + } + printf("%lld\n", d[n]); + } + return 0; +} diff --git a/1415.cpp b/1415.cpp new file mode 100644 index 0000000..a4772ac --- /dev/null +++ b/1415.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long long LL; +#define N 10005 +bool f[N], fac[N]; +int n, m; +vector prime; +void init() +{ + f[1] = f[0] = 1; + for (int i = 2; i * i <= 10000; ++i) + if (!f[i]) + for (int j = i * i; j <= 10000; j += i) + { + f[j] = 1; + } + for (int i = 2; i <= 10000; ++i) + if (!f[i]) + { + prime.push_back(i); + } + for (int i = 0; i * i <= 10000; ++i) + for (int j = 0; i * i + j * j * 2 <= 10000; ++j) + { + fac[i * i + j * j * 2] = 1; + } +} +int main() +{ + init(); + int _; + scanf("%d", &_); + while (_--) + { + scanf("%d%d", &n, &m); + if (n == 0) + { + if (!f[m] && !fac[m]) + { + puts("Yes"); + } + else + { + puts("No"); + } + } + else + { + int i, x = n * n + m * m * 2; + for (i = 2; i * i <= x; ++i) + if (x % i == 0) + { + break; + } + printf("%s\n", i * i <= x ? "No" : "Yes"); + } + } + return 0; +} diff --git a/1416.cpp b/1416.cpp new file mode 100644 index 0000000..c0cd24b --- /dev/null +++ b/1416.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +typedef long long LL; + +int L; +const int maxn = 100 + 10; +const LL INF = (LL)1 << 62; + +struct HeapNode +{ + int u; + LL d; + HeapNode(int u, LL d) : u(u), d(d) {} + bool operator<(const HeapNode &rhs) const + { + return d > rhs.d; + } +}; + +struct Edge +{ + int from, to, dist; + Edge(int a, int b, int c) : from(a), to(b), dist(c) {} +}; + +struct Dijkstra +{ + int n; + vector edges; + vector G[maxn]; + bool done[maxn]; + LL d[maxn]; + int p[maxn]; + + void init(int n) + { + this->n = n; + for (int i = 0; i < n; i++) + { + G[i].clear(); + } + edges.clear(); + } + + void AddEdge(int from, int to, int dist) + { + edges.push_back(Edge(from, to, dist)); + edges.push_back(Edge(to, from, dist)); + int m = edges.size(); + G[from].push_back(m - 2); + G[to].push_back(m - 1); + } + + void dijkstra(int s, int td) + { + priority_queue Q; + for (int i = 0; i < n; i++) + { + d[i] = INF; + } + d[s] = 0; + memset(done, 0, sizeof(done)); + memset(p, -1, sizeof(p)); + Q.push(HeapNode(s, 0)); + while (!Q.empty()) + { + HeapNode x = Q.top(); + Q.pop(); + int u = x.u; + if (done[u]) + { + continue; + } + done[u] = true; + for (int i = 0; i < G[u].size(); i++) + { + Edge &e = edges[G[u][i]]; + if (G[u][i] == td / 2 * 2 || G[u][i] == td / 2 * 2 + 1) + { + continue; + } + if (d[e.to] > d[u] + e.dist) + { + d[e.to] = d[u] + e.dist; + p[e.to] = G[u][i]; + Q.push(HeapNode(e.to, d[e.to])); + } + } + } + } +} p; + +int td[maxn]; +LL A[maxn]; +LL S[maxn * 10][maxn]; + +int main() +{ + //freopen("in.txt", "r", stdin); + int n, m; + while (scanf("%d%d%d", &n, &m, &L) != EOF) + { + p.init(n); + for (int i = 0; i < m; i++) + { + int u, v, w; + scanf("%d%d%d", &u, &v, &w); + u--, v--; + p.AddEdge(u, v, w); + } + LL res = 0; + for (int i = 0; i < m; i++) + for (int j = 0; j < n; j++) + { + S[i][j] = -1; + } + for (int i = 0; i < n; i++) + { + p.dijkstra(i, -10); + LL sum = 0; + for (int j = 0; j < n; j++) + { + if (p.d[j] == INF) + { + p.d[j] = L; + } + sum += p.d[j], td[j] = p.p[j]; + } + //printf("i = %d %lld\n", i+1, sum); + A[i] = sum; + //printf("\n"); + res += sum; + for (int j = 0; j < n; j++) + { + if (j == i || td[j] == -1) + { + continue; + } + LL s2 = 0; + p.dijkstra(i, td[j]); + for (int k = 0; k < n; k++) + { + if (p.d[k] == INF) + { + p.d[k] = L; + } + //printf("k = %d %lld\n", k, p.d[k]); + s2 += p.d[k]; + } + //printf("j = %d s2 - sum = %d\n", j+1, s2-sum); + S[td[j] / 2][i] = s2; + //printf("-->%d %d\n", i+1, td[j]/2+1); + } + } + LL n_max = 0; + for (int i = 0; i < m; i++) + { + LL sum = 0; + for (int j = 0; j < n; j++) + { + if (S[i][j] == -1) + { + sum += A[j]; + } + else + { + sum += S[i][j]; + } + } + n_max = max(n_max, sum); + } + printf("%lld %lld\n", res, n_max); + //printf("----------\n"); + } + return 0; +} diff --git a/1417.cpp b/1417.cpp new file mode 100644 index 0000000..6ab5dd1 --- /dev/null +++ b/1417.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +struct Node +{ + int c[6]; +};// - \ / _ | | +Node ST[262144 + 10]; + +void build(int k, int l, int r) +{ + if (l > r) + return; + if (l == r) + { + memset(&ST[k], 0, sizeof(ST[k])); + return; + } + int mid = (l + r) / 2; + build(k << 1, l, mid); + build(k << 1 | 1, mid + 1, r); +} +Node combine(Node s1, Node s2) +{ + Node s; + s.c[0] = (s1.c[0] && s2.c[0]) || (s1.c[1] && s2.c[2]); + s.c[1] = (s1.c[0] && s2.c[1]) || (s1.c[1] && s2.c[3]); + s.c[2] = (s1.c[2] && s2.c[0]) || (s1.c[3] && s2.c[2]); + s.c[3] = (s1.c[2] && s2.c[1]) || (s1.c[3] && s2.c[3]); + s.c[4] = (s1.c[4]) || (s1.c[0] && s2.c[4] && s1.c[3]); + s.c[5] = (s2.c[5]) || (s2.c[0] && s1.c[5] && s2.c[3]); + return s; +} +void modify(int k, int l, int r, int qidx, int qfield, int qval) +{ + if (l > r) + return; + if (l == r) + { + ST[k].c[qfield] = qval; + ST[k].c[1] = (ST[k].c[0] && ST[k].c[5]) || (ST[k].c[3] && ST[k].c[4]); + ST[k].c[2] = (ST[k].c[0] && ST[k].c[4]) || (ST[k].c[3] && ST[k].c[5]); + return; + } + int mid = (l + r) / 2; + if (qidx > mid) + modify(k << 1 | 1, mid + 1, r, qidx, qfield, qval); + else + modify(k << 1, l, mid, qidx, qfield, qval); + ST[k] = combine(ST[k << 1], ST[k << 1 | 1]); +} +Node query(int k, int l, int r, int ql, int qr) +{ + if (ql <= l && r <= qr) + return ST[k]; + int mid = (l + r) / 2; + if (ql > mid) + return query(k << 1 | 1, mid + 1, r, ql, qr); + else if (qr <= mid) + return query(k << 1, l, mid, ql, qr); + else + return combine(query(k << 1, l, mid, ql, qr), + query(k << 1 | 1, mid + 1, r, ql, qr)); +} +int main() +{ + //freopen("in.txt", "r+t", stdin); + //freopen("out2.txt", "w+t", stdout); + int testcase, C; + int r1, r2, c1, c2; + int lines = 0; + char cmd[10]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &C); + memset(ST, 0, sizeof(ST)); + build(1, 0, C); + while (scanf("%s", cmd) == 1 && cmd[0] != 'E') + { + scanf("%d %d %d %d", &r1, &c1, &r2, &c2); + if (cmd[0] == 'O' || cmd[0] == 'C') + {// open + int qval = cmd[0] == 'O' ? 1 : 0; + if (c1 != c2) + { + modify(1, 0, C, min(c1, c2), r1 == 1 ? 0 : 3, qval); + } + else + { + modify(1, 0, C, min(c1, c2) - 1, 5, qval); + modify(1, 0, C, min(c1, c2), 4, qval); + } + } + else + {// ask + if (c1 > c2) + swap(c1, c2), swap(r1, r2); + + if (r1 == r2 && c1 == c2) + { + puts("Y"); + } + else if (r1 != r2 && c1 == c2) + { + Node l = query(1, 0, C, 0, c1 - 1); + Node r = query(1, 0, C, c1, C); + if (l.c[5] || r.c[4]) + puts("Y"); + else + puts("N"); + } + else + { + Node l = query(1, 0, C, 0, c1 - 1); + Node r = query(1, 0, C, c2, C); + Node m = query(1, 0, C, c1, c2 - 1); + int kc[2][2] = {{0, 1}, {2, 3}}; + if (m.c[kc[r1 - 1][r2 - 1]] || + (l.c[5] && m.c[kc[(3 - r1) - 1][r2 - 1]]) || + (r.c[4] && m.c[kc[r1 - 1][(3 - r2) - 1]]) || + (l.c[5] && r.c[4] && m.c[kc[(3 - r1) - 1][(3 - r2) - 1]])) + puts("Y"); + else + puts("N"); + } + } + } + } + return 0; +} +/* +3 +2 +Open 1 1 1 2 +Open 1 2 2 2 +Ask 1 1 2 2 +Ask 2 1 2 2 +Exit +3 +Open 1 1 1 2 +Ask 1 1 1 2 +Close 1 1 1 2 +Ask 1 1 1 2 +Exit +2 +Open 1 1 1 2 +Open 1 2 2 2 +Open 2 1 2 2 +Ask 1 1 2 1 +Exit +*/ diff --git a/1418.cpp b/1418.cpp new file mode 100644 index 0000000..253987c --- /dev/null +++ b/1418.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +typedef long long ll; +const int INF = 0x3f3f3f3f; + +int main() +{ + int n; + while (scanf("%d", &n) != EOF && n) + { + if (n <= 3) + { + puts("1"); + } + else if (n == 4) + { + puts("2"); + } + else if (n > 4) + { + printf("%d\n", n); + } + } + return 0; +} diff --git a/1419.cpp b/1419.cpp new file mode 100644 index 0000000..9281221 --- /dev/null +++ b/1419.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +const int COL = 100; +const int ROW = 100; +const int INF = 1 << 30; +char win[COL][ROW]; +int c, r, X, Y; +int a[30]; + +void out() +{ + int i, j; + for (i = 0; i < c; i++) + { + for (j = 0; j < r; j++) + { + cout << win[i][j]; + } + cout << endl; + } + for (i = 0; i < 26; i++) + { + cout << a[i] << " "; + } + cout << endl; +} +int IsWin(int x) +{ + int lx, ly, rx, ry; + lx = ly = INF; + rx = ry = -1; + for (int i = 0; i < c; i++) + for (int j = 0; j < r; j++) + { + if (win[i][j] == x + 'A') + { + lx = min(lx, i); + ly = min(ly, j); + rx = max(rx, i); + ry = max(ry, j); + } + } + if (lx == INF || ly == INF || rx == -1 || ry == -1 || rx - lx < 2 || ry - ly < 2) + { + return false; + } + for (int i = lx; i <= rx; i++) + if (win[i][ly] != x + 'A' || win[i][ry] != x + 'A') + { + return false; + } + for (int i = ly; i <= ry; i++) + if (win[lx][i] != x + 'A' || win[rx][i] != x + 'A') + { + return false; + } + for (int i = lx + 1; i < rx; i++) + for (int j = ly + 1; j < ry; j++) + if (win[i][j] != '.') + { + return false; + } + return true; +} +void solve() +{ + char ch; + int i, j; + for (i = 0; i < c; i++) + for (j = 0; j < r; j++) + if (win[i][j] != '.') + { + a[win[i][j] - 'A']++; + } + for (i = 0; i < 26; i++) + { + if (!a[i]) + { + continue; + } + if (IsWin(i)) + { + ch = i + 'A'; + cout << ch; + } + } +} + +int main() +{ + int i, j; + while (cin >> c >> r) + { + if (!c && !r) + { + break; + } + for (i = 0; i < c; i++) + for (j = 0; j < r; j++) + { + cin >> win[i][j]; + } + memset(a, 0, sizeof(a)); + solve(); + cout << endl; + } + return 0; +} diff --git a/142.cpp b/142.cpp new file mode 100644 index 0000000..47468ae --- /dev/null +++ b/142.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +int main() +{ + int nregions = 0, nicons = 0; + int regions[25][4]; + int icons[50][3]; + string command; + int x, y; + priority_queue, greater> clicked; + bool checked = false; + while (cin >> command) + { + if (command == "#") + { + break; + } + else if (command == "I") + { + cin >> icons[nicons][0] >> icons[nicons][1]; + icons[nicons][2] = 1; + nicons++; + } + else if (command == "R") + { + cin >> regions[nregions][0] >> regions[nregions][1] >> regions[nregions][2] >> regions[nregions][3]; + nregions++; + } + else if (command == "M") + { + cin >> x >> y; + // checking all icons for their visibility + if (!checked) + { + for (int i = 0; i < nicons; i++) + { + for (int j = 0; j < nregions; j++) + { + if (icons[i][0] >= regions[j][0] && icons[i][1] >= regions[j][1] && icons[i][0] <= regions[j][2] && icons[i][1] <= regions[j][3]) + { + icons[i][2] = 0;// icon is invisible + break; + } + } + } + checked = true; + } + bool selected = false; + for (int i = nregions - 1; i >= 0; i--) + { + if (x >= regions[i][0] && y >= regions[i][1] && x <= regions[i][2] && y <= regions[i][3]) + { + selected = true; + cout << char(65 + i) << endl; + break; + } + } + if (selected) + { + continue; + } + while (!clicked.empty()) + { + clicked.pop(); + } + double min = 10000000.0; + for (int i = 0; i < nicons; i++) + { + if (icons[i][2] == 1) + { + double temp = pow(x - icons[i][0], 2) + pow(y - icons[i][1], 2); + if (temp == min) + { + clicked.push(i); + } + else if (temp < min) + { + while (!clicked.empty()) + { + clicked.pop(); + } + clicked.push(i); + min = temp; + } + } + } + while (!clicked.empty()) + { + printf("%3.d", clicked.top() + 1); + clicked.pop(); + } + cout << endl; + } + } + return 0; +} diff --git a/1420.cpp b/1420.cpp new file mode 100644 index 0000000..c0eee08 --- /dev/null +++ b/1420.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +//BISMILLAHIRRAHMANIRRAHIM +/* + manus tar shopner soman boro + Author :: Shakil Ahmed +.............AUST_CSE27......... + prob :: + Type :: + verdict:: + */ + +#define pf(a) printf("%d\n", a) +#define pf2(a, b) printf("%d %d\n", a, b) +#define pfcs(cs, a) printf("Case %d: %d\n", cs, a) +#define sc(a) scanf("%d", &a) +#define sc2(a, b) scanf("%d %d", &a, &b) +#define pb push_back +#define mp make_pair +#define pi acos(-1.0) +#define ff first +#define LL long long +#define ss second +#define rep(i, n) for (i = 0; i < n; i++) +#define REP(i, n) for (i = n; i >= 0; i--) +#define FOR(i, a, b) for (int i = a; i <= b; i++) +#define ROF(i, a, b) for (int i = a; i >= b; i--) +#define re return +#define QI queue +#define SI stack +#define pii pair +#define MAX +#define MOD +#define INF 1 << 30 +#define SZ(x) ((int)(x).size()) +#define ALL(x) (x).begin(), (x).end() +#define sqr(x) ((x) * (x)) +#define memo(a, b) memset((a), (b), sizeof(a)) +#define G() getchar() +#define MAX3(a, b, c) max(a, max(b, c)) + +double const EPS = 3e-8; + +template +T gcd(T a, T b) +{ + return (b != 0 ? gcd(b, a % b) : a); +} +template +T lcm(T a, T b) +{ + return (a / gcd(a, b) * b); +} + +const int N = 100005; +struct abc +{ + LL s, t, time; +} inp[2 * N]; +bool cmp(abc a, abc b) +{ + return a.time < b.time; +} +int main() +{ + int n, i; + while (scanf("%d", &n) && n) + { + int idx = 0; + LL s, t; + rep(idx, n) + { + scanf("%lld %lld", &inp[idx].s, &inp[idx].t); + inp[idx].time = inp[idx].s + (inp[idx].t - inp[idx].s) / 2 + 1; + } + int cnt = 0, mx = -1; + sort(inp, inp + n, cmp); + LL tmp = -1; + rep(i, idx) + { + tmp = max(tmp, inp[i].s) + (inp[i].t - inp[i].s) / 2 + 1; + if (tmp > inp[i].t) + { + break; + } + } + if (i < idx) + { + puts("NO"); + } + else + { + puts("YES"); + } + } + return 0; +} diff --git a/1421.cpp b/1421.cpp new file mode 100644 index 0000000..efea2a4 --- /dev/null +++ b/1421.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +const int MAXN = 5000 + 10; +const long double eps = 1e-8; + +struct Archery +{ + int D, L, R; + void scan() + { + scanf("%d%d%d", &D, &L, &R); + } +}; + +Archery S[MAXN]; +int N, W; + +bool check(int x, int y, int id) +{ + long double a = 0, b = acos(-1); + for (int i = 0; i < N; ++i) + { + if (i == id) + continue; + long double ta, tb; + if (S[i].D > y) + { + ta = atan2(S[i].D - y, S[i].R - x); + tb = atan2(S[i].D - y, S[i].L - x); + } + else + { + ta = atan2(y - S[i].D, x - S[i].L); + tb = atan2(y - S[i].D, x - S[i].R); + } + a = max(a, ta); + b = min(b, tb); + if (a > b + eps) + return false; + } + return true; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &W, &N); + for (int i = 0; i < N; ++i) + S[i].scan(); + S[N] = (Archery){0, 0, W}; + N++; + bool flag = false; + for (int i = 0; i < N - 1; ++i) + { + flag = check(S[i].L, S[i].D, i) || check(S[i].R, S[i].D, i); + if (flag) + break; + } + if (flag) + puts("YES"); + else + puts("NO"); + } + return 0; +} diff --git a/1422.cpp b/1422.cpp new file mode 100644 index 0000000..066e083 --- /dev/null +++ b/1422.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +struct node +{ + int st, ed, wt; + bool operator<(const node &a) const + { + return ed > a.ed; + } +} t[100100]; +int n; +bool cmp(node a, node b) +{ + return a.st < b.st; +} +priority_queue q; +bool check(int k) +{ + while (!q.empty()) + { + q.pop(); + } + int i, j, cnt = 0, cur; + for (i = 1; i <= 20000; i++) + { + while (t[cnt].st < i && cnt != n) + { + q.push(t[cnt++]); + } + cur = k; + while (!q.empty() && cur != 0) + { + node d = q.top(); + q.pop(); + if (d.ed < i) + { + return false; + } + if (d.wt > cur) + { + d.wt -= cur; + cur = 0; + q.push(d); + } + else + { + cur -= d.wt; + } + } + } + if (q.empty() && cnt == n) + { + return true; + } + return false; +} +int main() +{ + int i, i1, j, T, l = 1e9, r = 0, mid; + scanf("%d", &T); + for (i1 = 1; i1 <= T; i1++) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d%d", &t[i].st, &t[i].ed, &t[i].wt); + l = min(l, t[i].wt / (t[i].ed - t[i].st)); + r += t[i].wt; + } + sort(t, t + n, cmp); + while (l < r) + { + mid = (l + r) / 2; + if (check(mid)) + { + r = mid; + } + else + { + l = mid + 1; + } + } + printf("%d\n", l); + } +} diff --git a/1423.cpp b/1423.cpp new file mode 100644 index 0000000..eefdc69 --- /dev/null +++ b/1423.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +char map_[11][11]; +int n, ans[11], sum[11]; + +bool DFS(int idx) +{ + if (idx == n + 1) + { + return true; + } + for (int i = -10; i <= 10; i++) + { + int m, flag = 0; + if (i <= 0) + { + m = -(i + 10); + } + else + { + m = i; + } + for (int j = 1; j <= idx; j++) + { + if (map_[idx][j] == '+') + { + if (m + sum[idx - 1] - sum[j - 1] <= 0) + { + flag = 1; + break; + } + } + else if (map_[idx][j] == '-') + { + if (m + sum[idx - 1] - sum[j - 1] >= 0) + { + flag = 1; + break; + } + } + else + { + if (m + sum[idx - 1] - sum[j - 1] != 0) + { + flag = 1; + break; + } + } + } + if (!flag) + { + ans[idx] = m; + sum[idx] = sum[idx - 1] + m; + if (DFS(idx + 1)) + { + return true; + } + } + } + return false; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + char str[1001]; + scanf("%d", &n); + scanf("%s", str); + int cou = 0; + for (int i = 1; i <= n; i++) + { + for (int j = i; j <= n; j++) + { + map_[j][i] = str[cou++]; + } + } + memset(ans, 0, sizeof(ans)); + memset(sum, 0, sizeof(sum)); + DFS(1); + for (int i = 1; i <= n; i++) + { + printf("%d%c", ans[i], i == n ? '\n' : ' '); + } + } + return 0; +} diff --git a/1424.cpp b/1424.cpp new file mode 100644 index 0000000..ed0c3cf --- /dev/null +++ b/1424.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define maxn 110 +#define inf 2139062143 + +int n, m, len, g[110][110], dp[210][110], path[210]; + +int main() +{ + int i, j, k, T; + scanf("%d", &T); + while (T--) + { + scanf("%d %d", &n, &m); + memset(g, 0, sizeof(g)); + for (i = 0; i < m; i++) + { + int a, b; + scanf("%d %d", &a, &b); + g[a][b] = g[b][a] = 1; + } + for (i = 1; i <= n; i++) + g[i][i] = 1; + memset(dp, 0x7f, sizeof(dp)); + scanf("%d", &len); + for (i = 1; i <= len; i++) + scanf("%d", &path[i]); + for (i = 1; i <= n; i++) + dp[1][i] = 1; + dp[1][path[1]] = 0; + for (i = 2; i <= len; i++) + for (j = 1; j <= n; j++) + { + if (dp[i - 1][j] == inf) + continue; + for (k = 1; k <= n; k++) + if (g[j][k]) + dp[i][k] = min(dp[i][k], dp[i - 1][j] + (path[i] != k)); + } + int ans = inf; + for (i = 1; i <= n; i++) + ans = min(ans, dp[len][i]); + printf("%d\n", ans); + } + return 0; +} diff --git a/1425.cpp b/1425.cpp new file mode 100644 index 0000000..656ea22 --- /dev/null +++ b/1425.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +const int N = 55; +int t, n; +struct Point +{ + double x, y; + void read() + { + scanf("%lf%lf", &x, &y); + } +} p[N]; + +long long dp[N][N]; + +bool cmp(Point a, Point b) +{ + return a.x < b.x; +} + +double xmul(Point a1, Point a2, Point b1, Point b2) +{ + double ax = a2.x - a1.x, ay = a2.y - a1.y; + double bx = b2.x - b1.x, by = b2.y - b1.y; + return ax * by - bx * ay; +} + +bool judge(int s, int e, double flag) +{ + for (int i = s + 1; i < e; i++) + { + double ans = xmul(p[s], p[i], p[s], p[e]) * flag; + if (ans <= 0) + { + return false; + } + } + return true; +} + +long long solve() +{ + memset(dp, 0, sizeof(dp)); + dp[1][0] = dp[0][1] = 1; + for (int i = 2; i < n; i++) + { + for (int j = 0; j < i - 1; j++) + { + int k = i - 1; + dp[i][j] += dp[k][j]; + dp[j][i] += dp[j][k]; + } + for (int j = 0; j < i - 1; j++) + { + if (judge(j, i, 1)) + { + dp[i][i - 1] += dp[j][i - 1]; + } + if (judge(j, i, -1)) + { + dp[i - 1][i] += dp[i - 1][j]; + } + } + } + long long ans = 0; + for (int i = 0; i < n - 1; i++) + { + if (judge(i, n - 1, 1)) + { + ans += dp[i][n - 1]; + } + if (judge(i, n - 1, -1)) + { + ans += dp[n - 1][i]; + } + } + return ans; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + p[i].read(); + } + sort(p, p + n, cmp); + printf("%lld\n", solve() / 2); + } + return 0; +} diff --git a/1426.cpp b/1426.cpp new file mode 100644 index 0000000..0fa0735 --- /dev/null +++ b/1426.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +#define maxn 40000 + 10 +#define LL long long + +bool vis[maxn]; +LL pri[20000], yinzi[20000], cou[20000], ans[2000000], all, ansnum; +LL n, r; +LL extEculid(LL a, LL b, LL &x, LL &y) +{ + if (b == 0) + { + x = 1; + y = 0; + return a; + } + LL g = extEculid(b, a % b, x, y); + LL tmp = x; + x = y; + y = tmp - a / b * y; + return g; +} +void bfs(int num, LL cur) +{ + int i, j, k; + if (num == all) + { + LL p, q; + //printf("cur=%lld,",cur); + LL g = extEculid(cur, n / cur, p, q); + if (2 * r % g) + { + return; + } + p *= -2 * r / g; + q *= 2 * r / g; + for (int h = 0; h < g; h++) + { + ans[ansnum++] = ((r + p * cur) % n + n) % n; + p += n / cur / g; + } + return; + } + bfs(num + 1, cur); + for (i = 0; i < cou[num]; i++) + { + cur *= yinzi[num]; + bfs(num + 1, cur); + } +} +int main() +{ + ios::sync_with_stdio(false); + int i, j, k, v = 1; + LL x; + for (i = 2; i * i < maxn; i++) + if (!vis[i]) + for (j = i * i; j < maxn; j += i) + { + vis[j] = 1; + } + for (i = 2, k = 0; i < maxn; i++) + if (!vis[i]) + { + pri[k++] = i; + } + while (cin >> x >> n >> r && (x + n + r)) + { + printf("Case %d:", v++); + LL num = n; + for (i = 0, k = 0; pri[i] * pri[i] <= n; i++) + if (num % pri[i] == 0) + { + yinzi[k] = pri[i]; + cou[k] = 0; + while (num % pri[i] == 0) + { + cou[k]++; + num /= pri[i]; + } + k++; + if (num == 1) + { + break; + } + } + if (num > 1) + { + yinzi[k] = num; + cou[k++] = 1; + } + //for(i=0;i + +using namespace std; + +const int maxn = 110; +const int maxm = 11000; + +int n, m, K, ans; +int V[maxn][maxm], C[maxn][maxm], f[maxn][maxm], sc[maxm], sv[maxm], q[maxm]; + +int get() +{ + int f = 0, v = 0; + char ch; + while (!isdigit(ch = getchar())) + if (ch == '-') + break; + if (ch == '-') + f = 1; + else + v = ch - 48; + while (isdigit(ch = getchar())) + v = v * 10 + ch - 48; + if (f) + return -v; + else + return v; +} + +void init() +{ + n++; + m++; + for (int i = 1; i <= n; i++) + for (int j = 2; j <= m; j++) + V[i][j] = get(); + for (int i = 1; i <= n; i++) + for (int j = 2; j <= m; j++) + C[i][j] = get(); +} + +void work() +{ + for (int i = 1; i <= n; i++) + { + int head = 1, tail = 0; + sc[0] = sv[0] = 0; + for (int j = 1; j <= m; j++) + { + sc[j] = sc[j - 1] + C[i][j]; + sv[j] = sv[j - 1] + V[i][j]; + while (head <= tail && f[i - 1][j] - f[i - 1][q[tail]] >= sv[j] - sv[q[tail]]) + tail--; + q[++tail] = j; + while (head <= tail && sc[j] - sc[q[head]] > K) + head++; + f[i][j] = f[i - 1][q[head]] + sv[j] - sv[q[head]]; + } + head = 1, tail = 1; + q[head] = m; + for (int j = m - 1; j >= 1; j--) + { + while (head <= tail && sc[q[head]] - sc[j] > K) + head++; + f[i][j] = max(f[i][j], f[i - 1][q[head]] + sv[q[head]] - sv[j]); + while (head <= tail && f[i - 1][j] - f[i - 1][q[tail]] >= sv[q[tail]] - sv[j]) + tail--; + q[++tail] = j; + } + } + ans = 0; + for (int i = 1; i <= m; i++) + ans = max(ans, f[n][i]); + printf("%d\n", ans); +} + +int main() +{ + while ((n = get()) + (m = get()) + (K = get()) > 0) + { + init(); + work(); + } + return 0; +} diff --git a/1428.cpp b/1428.cpp new file mode 100644 index 0000000..b78cbf0 --- /dev/null +++ b/1428.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define MAX 40001 + +struct node +{ + int id, val; + bool operator<(const node &a) const + { + return val < a.val; + } +} a[MAX]; + +int n, c[2][MAX], posa[MAX], posb[MAX]; + +int lowbit(int x) +{ + return x & (-x); +} + +void add(int t, int x, int val) +{ + while (x <= n) + { + c[t][x] += val; + x += lowbit(x); + } +} + +int sum(int t, int x) +{ + int sum = 0; + while (x > 0) + { + sum += c[t][x]; + x -= lowbit(x); + } + return sum; +} + +int main() +{ + int cas; + scanf("%d", &cas); + while (cas--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d", &a[i].val); + a[i].id = i; + } + sort(a + 1, a + n + 1); + memset(c, 0, sizeof(c)); + memset(posa, 0, sizeof(posa)); + memset(posb, 0, sizeof(posb)); + for (int i = 1; i <= n; i++) + { + posa[a[i].id] = i; + } + for (int i = 1; i <= n; i++) + { + posb[a[i].id] = n - i + 1; + } + long long ans = 0; + for (int i = 1; i <= n; i++) + { + int ita = sum(0, posa[i]); + add(0, posa[i], 1); + ans += ita * (n - posa[i] - (i - ita - 1)); + ita = sum(1, posb[i]); + add(1, posb[i], 1); + ans += ita * (n - posb[i] - (i - ita - 1)); + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/143.cpp b/143.cpp new file mode 100644 index 0000000..a2b78fe --- /dev/null +++ b/143.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +struct POINT +{ + int x, y; +}; + +int main() +{ + for (POINT Coord[3], Tree;;) + { + POINT Min = {10000, 10000}, Max = {0, 0}; + for (int i = 0; i < 3; ++i) + { + float fX, fY; + if (scanf("%f%f", &fX,&fY) == 0) + { + return 0; + } + Coord[i].x = (int)(fX * 100 + 0.5); + Coord[i].y = (int)(fY * 100 + 0.5); + + if (Coord[i].x < Min.x) + { + Min.x = Coord[i].x; + } + if (Coord[i].y < Min.y) + { + Min.y = Coord[i].y; + } + if (Coord[i].x > Max.x) + { + Max.x = Coord[i].x; + } + if (Coord[i].y > Max.y) + { + Max.y = Coord[i].y; + } + } + + if (Min.x > Max.x || Min.y > Max.y) + { + printf(" 0\n"); + } + + if (Coord[0].x == 0 && Coord[0].y == 0 && + Coord[1].x == 0 && Coord[1].y == 0 && + Coord[2].x == 0 && Coord[2].y == 0) + { + break; + } + POINT Vec[3] = + { + {Coord[1].x - Coord[0].x, Coord[1].y - Coord[0].y}, + {Coord[2].x - Coord[1].x, Coord[2].y - Coord[1].y}, + {Coord[0].x - Coord[2].x, Coord[0].y - Coord[2].y}, + }; + + if (Vec[0].x * Vec[1].y - Vec[1].x * Vec[0].y > 0) + { + swap(Coord[0], Coord[1]); + Vec[0].x = Coord[1].x - Coord[0].x; + Vec[0].y = Coord[1].y - Coord[0].y; + Vec[1].x = Coord[2].x - Coord[1].x; + Vec[1].y = Coord[2].y - Coord[1].y; + Vec[2].x = Coord[0].x - Coord[2].x; + Vec[2].y = Coord[0].y - Coord[2].y; + } + + if (Min.x % 100 != 0) + { + Min.x = (Min.x / 100 + 1) * 100; + } + if (Min.y % 100 != 0) + { + Min.y = (Min.y / 100 + 1) * 100; + } + if (Max.x % 100 != 0) + { + Max.x = Max.x / 100 * 100; + } + if (Max.y % 100 != 0) + { + Max.y = Max.y / 100 * 100; + } + + if (Min.x < 100) + { + Min.x = 100; + } + if (Min.y < 100) + { + Min.y = 100; + } + if (Max.x > 9900) + { + Max.x = 9900; + } + if (Max.y > 9900) + { + Max.y = 9900; + } + int nCnt = 0, i; + + for (Tree.y = Min.y; Tree.y <= Max.y; Tree.y += 100) + { + for (Tree.x = Min.x; Tree.x <= Max.x; Tree.x += 100) + { + for (i = 0; i < 3; ++i) + { + POINT VecT = {Coord[i].x - Tree.x, Coord[i].y - Tree.y}; + if (VecT.x * Vec[i].y - VecT.y * Vec[i].x > 0) + { + break; + } + } + nCnt += (i == 3); + } + } + + printf("%4d\n", nCnt); + } + return 0; +} diff --git a/1431.cpp b/1431.cpp new file mode 100644 index 0000000..f00ccbd --- /dev/null +++ b/1431.cpp @@ -0,0 +1,286 @@ +#include + +using namespace std; + +/* + 大数模版 å«大数开平方 +*/ + +const int numlen = 1005;// ä½Â数 + +int max(int a, int b) { return a > b ? a : b; } +struct bign +{ + int len, s[numlen]; + bign() + { + memset(s, 0, sizeof(s)); + len = 1; + } + bign(int num) { *this = num; } + bign(const char *num) { *this = num; } + bign operator=(const int num) + { + char s[numlen]; + sprintf(s, "%d", num); + *this = s; + return *this; + } + bign operator=(const char *num) + { + len = strlen(num); + while (len > 1 && num[0] == '0') + num++, len--; + for (int i = 0; i < len; i++) + s[i] = num[len - i - 1] - '0'; + return *this; + } + + void deal() + { + while (len > 1 && !s[len - 1]) + len--; + } + + bign operator+(const bign &a) const + { + bign ret; + ret.len = 0; + int top = max(len, a.len), add = 0; + for (int i = 0; add || i < top; i++) + { + int now = add; + if (i < len) + now += s[i]; + if (i < a.len) + now += a.s[i]; + ret.s[ret.len++] = now % 10; + add = now / 10; + } + return ret; + } + bign operator-(const bign &a) const + { + bign ret; + ret.len = 0; + int cal = 0; + for (int i = 0; i < len; i++) + { + int now = s[i] - cal; + if (i < a.len) + now -= a.s[i]; + if (now >= 0) + cal = 0; + else + { + cal = 1; + now += 10; + } + ret.s[ret.len++] = now; + } + ret.deal(); + return ret; + } + bign operator*(const bign &a) const + { + bign ret; + ret.len = len + a.len; + for (int i = 0; i < len; i++) + { + for (int j = 0; j < a.len; j++) + ret.s[i + j] += s[i] * a.s[j]; + } + for (int i = 0; i < ret.len; i++) + { + ret.s[i + 1] += ret.s[i] / 10; + ret.s[i] %= 10; + } + ret.deal(); + return ret; + } + + //乘以å°Â数,直接乘快点 + bign operator*(const int num) + { + bign ret; + ret.len = 0; + int bb = 0; + for (int i = 0; i < len; i++) + { + int now = bb + s[i] * num; + ret.s[ret.len++] = now % 10; + bb = now / 10; + } + while (bb) + { + ret.s[ret.len++] = bb % 10; + bb /= 10; + } + ret.deal(); + return ret; + } + + bign operator/(const bign &a) const + { + bign ret, cur = 0; + ret.len = len; + for (int i = len - 1; i >= 0; i--) + { + cur = cur * 10; + cur.s[0] = s[i]; + while (cur >= a) + { + cur -= a; + ret.s[i]++; + } + } + ret.deal(); + return ret; + } + + bign operator%(const bign &a) const + { + bign b = *this / a; + return *this - b * a; + } + + bign operator+=(const bign &a) + { + *this = *this + a; + return *this; + } + bign operator-=(const bign &a) + { + *this = *this - a; + return *this; + } + bign operator*=(const bign &a) + { + *this = *this * a; + return *this; + } + bign operator/=(const bign &a) + { + *this = *this / a; + return *this; + } + bign operator%=(const bign &a) + { + *this = *this % a; + return *this; + } + + bool operator<(const bign &a) const + { + if (len != a.len) + return len < a.len; + for (int i = len - 1; i >= 0; i--) + if (s[i] != a.s[i]) + return s[i] < a.s[i]; + return false; + } + bool operator>(const bign &a) const { return a < *this; } + bool operator<=(const bign &a) const { return !(*this > a); } + bool operator>=(const bign &a) const { return !(*this < a); } + bool operator==(const bign &a) const { return !(*this > a || *this < a); } + bool operator!=(const bign &a) const { return *this > a || *this < a; } + + string str() const + { + string ret = ""; + for (int i = 0; i < len; i++) + ret = char(s[i] + '0') + ret; + return ret; + } +}; +istream &operator>>(istream &in, bign &x) +{ + string s; + in >> s; + x = s.c_str(); + return in; +} +ostream &operator<<(ostream &out, const bign &x) +{ + out << x.str(); + return out; +} +// 大数开平方 +bign Sqrt(bign x) +{ + int a[numlen / 2]; + int top = 0; + for (int i = 0; i < x.len; i += 2) + { + if (i == x.len - 1) + { + a[top++] = x.s[i]; + } + else + a[top++] = x.s[i] + x.s[i + 1] * 10; + } + bign ret = (int)sqrt(a[top - 1]); + int xx = sqrt(a[top - 1]); + bign pre = a[top - 1] - xx * xx; + bign cc; + for (int i = top - 2; i >= 0; i--) + { + pre = pre * 100 + a[i]; + cc = ret * 20; + for (int j = 9; j >= 0; j--) + { + bign now = (cc + j) * j; + if (now <= pre) + { + ret = ret * 10 + j; + pre -= now; + break; + } + } + } + return ret; +} + +int main() +{ + bign n, d; + int e, cas = 1; + while (cin >> n >> d >> e) + { + if (!e) + break; + printf("Case #%d: ", cas++); + int chu = 0; + d = d * e; + while (true) + { + chu++; + if ((d - 1) % chu == 0 && n + 1 > (d - 1) / chu) + { + bign p_q = n + 1 - (d - 1) / chu; + bign now = p_q * p_q - n * 4; + // cout< 0) + { + bign p = p_q - Sqrt(now); + if (p % 2 == 0) + { + p /= 2; + } + else + continue; + bign q = p_q + Sqrt(now); + if (q % 2 == 0) + { + q /= 2; + } + else + continue; + cout << p << " " << q << endl; + break; + } + } + } + } + return 0; +} diff --git a/1432.cpp b/1432.cpp new file mode 100644 index 0000000..ee5278b --- /dev/null +++ b/1432.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +#define pi 3.141592653 + +struct node +{ + int x, y, r; + double angle; + bool operator<(const node &r) const + { + return angle < r.angle; + } +} p[5050]; +int N, K, vis[3000000]; +vector v; +double ans; + +int main() +{ + int i, j, test = 0; + while (scanf("%d %d", &N, &K) == 2 && N + K) + { + for (i = 0; i < N; i++) + { + scanf("%d %d", &p[i].x, &p[i].y); + p[i].r = p[i].x * p[i].x + p[i].y * p[i].y; + p[i].angle = atan2(p[i].y * 1.0, p[i].x * 1.0); + } + sort(p, p + N); + printf("Case #%d: ", ++test); + if (K == 0) + { + puts("0.00"); + continue; + } + memset(vis, 0, sizeof(vis)); + ans = 1e12; + for (i = 0; i < N; i++) + { + if (vis[p[i].r]) + continue; + vis[p[i].r] = 1; + int R = p[i].r; + v.clear(); + for (j = 0; j < N; j++) + if (p[j].r <= R) + v.push_back(p[j].angle); + if (v.size() < K) + continue; + for (j = 0; j < v.size(); j++) + { + int t = j + K - 1; + if (t < v.size()) + ans = min(ans, 0.5 * R * (v[t] - v[j])); + else + { + t = t - v.size(); + ans = min(ans, 0.5 * R * (v[t] + 2 * pi - v[j])); + } + } + } + printf("%.2lf\n", ans); + } + return 0; +} diff --git a/1433.cpp b/1433.cpp new file mode 100644 index 0000000..df61455 --- /dev/null +++ b/1433.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; + +const int maxn = 10005; +const int offset = 1000001; +const int inf = 0x3f3f3f3f; +const double eps = 1e-8; +const double pi = acos(-1.0); + +struct point +{ + double x, y; + point(double _x = 0, double _y = 0) : x(_x), y(_y) {} +}; + +struct point3 +{ + double x, y, z; + point3(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {} +}; + +bool mult(point sp, point ep, point op) +{ + return (sp.x - op.x) * (ep.y - op.y) > (ep.x - op.x) * (sp.y - op.y) - eps; +} + +bool operator<(const point &l, const point &r) +{ + return l.y < r.y || (l.y == r.y && l.x < r.x); +} + +int graham(point pnt[], int n, point res[]) +{ + int i, len, k = 0, top = 1; + sort(pnt, pnt + n); + if (n == 0) + { + return 0; + } + res[0] = pnt[0]; + if (n == 1) + { + return 1; + } + res[1] = pnt[1]; + if (n == 2) + { + return 2; + } + res[2] = pnt[2]; + for (i = 2; i < n; i++) + { + while (top && mult(pnt[i], res[top], res[top - 1])) + { + top--; + } + res[++top] = pnt[i]; + } + len = top; + res[++top] = pnt[n - 2]; + for (i = n - 3; i >= 0; i--) + { + while (top != len && mult(pnt[i], res[top], res[top - 1])) + { + top--; + } + res[++top] = pnt[i]; + } + return top; +} + +int n, m, zmin, zmax; +point3 p[maxn]; +point pnt[maxn], res[maxn]; + +int main() +{ + //freopen("input.txt","r",stdin); + bool flag = false; + while (scanf("%d %d %d", &n, &zmin, &zmax) != EOF) + { + if (flag) + { + printf("\n"); + } + flag = true; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].z); + } + for (int z = zmin; z <= zmax; z++) + { + m = 0; + for (int i = 0; i < n; i++) + if (fabs(p[i].z - z) < eps) + { + pnt[m++] = point(p[i].x, p[i].y); + } + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + { + if ((p[i].z - z) * (p[j].z - z) >= 0) + { + continue; + } + double u = abs(p[i].z - z); + double v = abs(p[j].z - z); + pnt[m++] = point(1.0 * (p[i].x * v + p[j].x * u) / (u + v), + 1.0 * (p[i].y * v + p[j].y * u) / (u + v)); + /* + point3 a = p[i], b = p[j]; + if (fabs(a.z - b.z) < eps) + continue; + if (a.z > b.z) + swap(a, b); + if (a.z < z - eps && b.z > z + eps) + { + double temp = b.z - a.z; + point pp; + pp.x = a.x + (b.x - a.x) * (z - a.z) / (temp); + pp.y = a.y + (b.y - a.y) * (z - a.z) / (temp); + pnt[m++] = pp; + }*/ + } + /* + point3 s1 = point3(-10000, -10000, z), s2 = point3(10000, -10000, z), s3 = point3(0, 20000, z); + m = 0; + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (!parallel(p[i], p[j], s1, s2, s3) && intersect_in(p[i], p[j], s1, s2, s3)) + { + point3 jiao = intersection(p[i], p[j], s1, s2, s3); + pnt[m++] = point(jiao.x, jiao.y); + } + */ + m = graham(pnt, m, res); + if (m < 3) + { + printf("0.000000000\n"); + continue; + } + double s = res[0].y * (res[m - 1].x - res[1].x); + res[m] = res[0]; + for (int i = 1; i < m; i++) + { + s += res[i].y * (res[i - 1].x - res[i + 1].x); + } + printf("%.10lf\n", fabs(s / 2)); + } + } + return 0; +} diff --git a/1434.cpp b/1434.cpp new file mode 100644 index 0000000..9559c8b --- /dev/null +++ b/1434.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +typedef long long LL; + +const int N = 5000005; +const int inf = 1 << 28; +const double eps = 1e-8; + +bool prime[N]; +int ans[N]; + +void getprime() +{ + memset(prime, 0, sizeof(prime)); + for (int i = 3; i < N; i++, i++) + { + if (prime[i]) + { + continue; + } + if ((LL)i * i >= N) + { + continue; + } + for (int j = i * i; j < N; j += i) + { + prime[j] = 1; + } + } +} + +void init() +{ + memset(ans, 0, sizeof(ans)); + for (int i = 1, n = N / 5; i <= n; i++) + { + if (((3 * i + 7) & 1) && !prime[3 * i + 7]) + { + ans[i] = 1; + } + ans[i] += ans[i - 1]; + } +} + +int main() +{ + getprime(); + init(); + int n, t; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + printf("%d\n", ans[n]); + } + return 0; +} diff --git a/1435.cpp b/1435.cpp new file mode 100644 index 0000000..d634322 --- /dev/null +++ b/1435.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define maxn 3000009 +#define ll long long + +void gcd(ll a, ll b, ll &d, ll &x, ll &y) +{ + if (!b) + { + d = a; + x = 1; + y = 0; + } + else + { + gcd(b, a % b, d, y, x); + y -= x * (a / b); + } +} + +bool check(ll a, ll b, ll n) +{ + ll d, x, y; + gcd(a, b, d, x, y); + if (n % d) + { + return 0; + } + a /= d; + b /= d; + n /= d; + x *= n; + y *= n; + if (x < 0) + { + swap(x, y); + swap(a, b); + } + return y + a * (x / b) >= 0; +} + +bool yes(ll a, ll b, ll x, ll y) +{ + if ((x % a == 0 && y % b == 0) || (x % b == 0 && y % a == 0)) + { + return 1; + } + return 0; +} + +int main() +{ + int t; + ll a, b, x, y; + scanf("%d", &t); + while (t--) + { + scanf("%lld%lld%lld%lld", &a, &b, &x, &y); + if ((x % a == 0 && x % b == 0 && check(a, b, y) || (y % a == 0 && y % b == 0 && check(a, b, x)) || yes(a, b, x, y))) + { + puts("YES"); + } + else + { + puts("NO"); + } + } + return 0; +} diff --git a/1436.cpp b/1436.cpp new file mode 100644 index 0000000..82f3dea --- /dev/null +++ b/1436.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +const int N = 500005; +int t, n, cnt[N], prime[N], vis[N], pn = 0, f[N], ispri[N]; +long long m; + +int bfs() +{ + queue Q; + for (int i = 1; i <= n; i++) + if (!vis[i]) + { + Q.push(i); + } + while (!Q.empty()) + { + int now = Q.front(); + Q.pop(); + if (now == 0) + { + break; + } + cnt[f[now]] += cnt[now]; + vis[f[now]]--; + if (vis[f[now]] == 0) + { + Q.push(f[now]); + } + } +} + +void solve(int num, int v) +{ + for (int i = 0; i < pn && prime[i] <= num; i++) + { + while (num % prime[i] == 0) + { + cnt[prime[i]] += v; + num /= prime[i]; + } + if (ispri[num]) + { + cnt[num] += v; + break; + } + } +} + +long long pow_mod(long long x, int k) +{ + long long ans = 1; + while (k) + { + if (k & 1) + { + ans = ans * x % m; + } + x = x * x % m; + k >>= 1; + } + return ans; +} + +int main() +{ + for (int i = 2; i < N; i++) + { + if (vis[i]) + { + continue; + } + ispri[i] = 1; + prime[pn++] = i; + for (int j = i; j < N; j += i) + { + vis[j] = 1; + } + } + scanf("%d", &t); + while (t--) + { + scanf("%d%lld", &n, &m); + for (int i = 1; i <= n; i++) + { + cnt[i] = 1; + } + f[1] = 0; + memset(vis, 0, sizeof(vis)); + for (int i = 2; i <= n; i++) + { + scanf("%d", &f[i]); + vis[f[i]]++; + } + bfs(); + memset(vis, 0, sizeof(vis)); + for (int i = 1; i <= n; i++) + { + vis[cnt[i]]++; + } + memset(cnt, 0, sizeof(cnt)); + for (int i = 2; i <= n; i++) + { + solve(i, 1); + if (vis[i]) + { + solve(i, -vis[i]); + } + } + long long ans = 1; + for (int i = 0; i < pn; i++) + { + if (cnt[prime[i]] == 0) + { + continue; + } + ans = (ans * pow_mod((long long)prime[i], cnt[prime[i]])) % m; + } + printf("%lld\n", ans); + } + return 0; +} diff --git a/1437.cpp b/1437.cpp new file mode 100644 index 0000000..9743b38 --- /dev/null +++ b/1437.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define MAXN 128 + +char A[MAXN], B[MAXN]; +int f[MAXN][MAXN];// cost from 'x...xx' to B[l, r] +int dp[MAXN]; + +int solve() +{ + int n = strlen(A + 1); + memset(f, 63, sizeof(f)); + for (int i = 1; i <= n; i++) + { + f[i][i] = 1; + if (i + 1 <= n) + { + f[i][i + 1] = B[i] == B[i + 1] ? 1 : 2; + } + } + for (int i = 2; i <= n; i++) + { + for (int j = 1; j + i <= n; j++) + { + int l = j, r = j + i; + for (int k = l; k < r; k++) + { + f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r]); + } + if (B[l] == B[r]) + { + f[l][r] = min(f[l][r], f[l + 1][r]); + f[l][r] = min(f[l][r], f[l][r - 1]); + } + } + } + memset(dp, 63, sizeof(dp)); + dp[0] = 0; + for (int i = 0; i <= n; i++) + { + if (i && A[i] == B[i]) + { + dp[i] = min(dp[i], dp[i - 1]); + } + for (int j = i + 1; j <= n; j++) + { + dp[j] = min(dp[j], dp[i] + f[i + 1][j]); + } + } + return dp[n]; +} + +int main() +{ + while (scanf("%s%s", A + 1, B + 1) == 2) + { + printf("%d\n", solve()); + } + return 0; +} diff --git a/1438.cpp b/1438.cpp new file mode 100644 index 0000000..f324eab --- /dev/null +++ b/1438.cpp @@ -0,0 +1,300 @@ +#include + +using namespace std; + +const int MAXN = 505; +const double eps = 1e-8; + +struct Point +{ + double x, y, z; + Point() {} + Point(double xx, double yy, double zz) : x(xx), y(yy), z(zz) {} + Point operator-(const Point p1) + { + return Point(x - p1.x, y - p1.y, z - p1.z); + } + Point operator+(const Point p1) + { + return Point(x + p1.x, y + p1.y, z + p1.z); + } + Point operator*(Point p) + { + return Point(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); + } + Point operator*(double d) + { + return Point(x * d, y * d, z * d); + } + Point operator/(double d) + { + return Point(x / d, y / d, z / d); + } + double operator^(Point p) + { + return (x * p.x + y * p.y + z * p.z); + } +}; + +struct CH3D +{ + struct face + { + int a, b, c; + bool ok; + }; + int n; + Point P[MAXN]; + int num; + face F[8 * MAXN]; + int g[MAXN][MAXN]; + + double vlen(Point a) + { + return sqrt(a.x * a.x + a.y * a.y + a.z * a.z); + } + + Point cross(const Point &a, const Point &b, const Point &c) + { + return Point((b.y - a.y) * (c.z - a.z) - (b.z - a.z) * (c.y - a.y), + -((b.x - a.x) * (c.z - a.z) - (b.z - a.z) * (c.x - a.x)), (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)); + } + + double area(Point a, Point b, Point c) + { + return vlen((b - a) * (c - a)); + } + + double volume(Point a, Point b, Point c, Point d) + { + return (b - a) * (c - a) ^ (d - a); + } + + double dblcmp(Point &p, face &f) + { + Point m = P[f.b] - P[f.a]; + Point n = P[f.c] - P[f.a]; + Point t = p - P[f.a]; + return (m * n) ^ t; + } + + void deal(int p, int a, int b) + { + int f = g[a][b]; + face add; + if (F[f].ok) + { + if (dblcmp(P[p], F[f]) > eps) + { + dfs(p, f); + } + else + { + add.a = b; + add.b = a; + add.c = p; + add.ok = 1; + g[p][b] = g[a][p] = g[b][a] = num; + F[num++] = add; + } + } + } + + void dfs(int p, int now) + { + F[now].ok = 0; + deal(p, F[now].b, F[now].a); + deal(p, F[now].c, F[now].b); + deal(p, F[now].a, F[now].c); + } + + bool same(int s, int t) + { + Point &a = P[F[s].a]; + Point &b = P[F[s].b]; + Point &c = P[F[s].c]; + return fabs(volume(a, b, c, P[F[t].a])) < eps && fabs(volume(a, b, c, P[F[t].b])) < eps && fabs(volume(a, b, c, P[F[t].c])) < eps; + } + + void create() + { + int i, j, tmp; + face add; + bool flag = true; + num = 0; + if (n < 4) + { + return; + } + for (i = 1; i < n; i++) + { + if (vlen(P[0] - P[i]) > eps) + { + swap(P[1], P[i]); + flag = false; + break; + } + } + if (flag) + { + return; + } + flag = true; + for (i = 2; i < n; i++) + { + if (vlen((P[0] - P[1]) * (P[1] - P[i])) > eps) + { + swap(P[2], P[i]); + flag = false; + break; + } + } + if (flag) + { + return; + } + flag = true; + for (i = 3; i < n; i++) + { + if (fabs((P[0] - P[1]) * (P[1] - P[2]) ^ (P[0] - P[i])) > eps) + { + swap(P[3], P[i]); + flag = false; + break; + } + } + if (flag) + { + return; + } + for (i = 0; i < 4; i++) + { + add.a = (i + 1) % 4; + add.b = (i + 2) % 4; + add.c = (i + 3) % 4; + add.ok = true; + if (dblcmp(P[i], add) > 0) + { + swap(add.b, add.c); + } + g[add.a][add.b] = g[add.b][add.c] = g[add.c][add.a] = num; + F[num++] = add; + } + for (i = 4; i < n; i++) + { + for (j = 0; j < num; j++) + { + if (F[j].ok && dblcmp(P[i], F[j]) > eps) + { + dfs(i, j); + break; + } + } + } + tmp = num; + for (i = num = 0; i < tmp; i++) + if (F[i].ok) + { + F[num++] = F[i]; + } + } + + double area() + { + double res = 0.0; + if (n == 3) + { + Point p = cross(P[0], P[1], P[2]); + res = vlen(p) / 2.0; + return res; + } + for (int i = 0; i < num; i++) + { + res += area(P[F[i].a], P[F[i].b], P[F[i].c]); + } + return res / 2.0; + } + + double volume() + { + double res = 0.0; + Point tmp(0, 0, 0); + for (int i = 0; i < num; i++) + { + res += volume(tmp, P[F[i].a], P[F[i].b], P[F[i].c]); + } + return fabs(res / 6.0); + } + + int triangle() + { + return num; + } + + int polygon() + { + int i, j, res, flag; + for (i = res = 0; i < num; i++) + { + flag = 1; + for (j = 0; j < i; j++) + if (same(i, j)) + { + flag = 0; + break; + } + res += flag; + } + return res; + } + Point barycenter() + { + Point ans(0, 0, 0), o(0, 0, 0); + double all = 0; + for (int i = 0; i < num; ++i) + { + double vol = volume(o, P[F[i].a], P[F[i].b], P[F[i].c]); + ans = ans + (o + P[F[i].a] + P[F[i].b] + P[F[i].c]) / 4.0 * vol; + all += vol; + } + ans = ans / all; + return ans; + } + double ptoface(Point p, int i) + { + return fabs(volume(P[F[i].a], P[F[i].b], P[F[i].c], p) / vlen((P[F[i].b] - P[F[i].a]) * (P[F[i].c] - P[F[i].a]))); + } +}; + +CH3D hull; + +int main() +{ + while (~scanf("%d", &hull.n)) + { + for (int i = 0; i < hull.n; i++) + { + scanf("%lf %lf %lf", &hull.P[i].x, &hull.P[i].y, &hull.P[i].z); + } + hull.create(); + Point p = hull.barycenter(); + double minn = 2147483647; + for (int i = 0; i < hull.num; i++) + { + minn = min(minn, hull.ptoface(p, i)); + } + scanf("%d", &hull.n); + for (int i = 0; i < hull.n; i++) + { + scanf("%lf %lf %lf", &hull.P[i].x, &hull.P[i].y, &hull.P[i].z); + } + hull.create(); + p = hull.barycenter(); + double minm = 2147483647; + for (int i = 0; i < hull.num; i++) + { + minm = min(minm, hull.ptoface(p, i)); + } + printf("%.6lf\n", minn + minm); + } +} diff --git a/1439.cpp b/1439.cpp new file mode 100644 index 0000000..6c52d57 --- /dev/null +++ b/1439.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define MAXN 128 +#define MAXM 15 +#define INF 0x3f3f3f3f + +// min vertex color O(3^n) +int main() +{ + char R1[MAXN][8], R2[MAXN][8]; + int N, M; + while (scanf("%d", &N) == 1 && N) + { + map R; + for (int i = 0; i < N; i++) + { + scanf("%s %s", R1[i], R2[i]); + R[R1[i][0]] = R[R2[i][0]] = 1; + } + M = 0; + for (map::iterator it = R.begin(); it != R.end(); it++) + { + it->second = M++; + } + int maskg[MAXM] = {}, self_edge[1 << MAXM]; + for (int i = 0; i < N; i++) + { + int x = R[R1[i][0]], y = R[R2[i][0]]; + maskg[x] |= 1 << y, maskg[y] |= 1 << x; + } + for (int i = 0; i < (1 << M); i++) + { + int ok = 1; + for (int j = 0; j < M; j++) + { + if ((i >> j) & 1) + { + ok &= (i & maskg[j]) == 0; + } + } + self_edge[i] = ok; + } + int dp[1 << MAXM] = {}, from[1 << MAXM]; + dp[0] = 0; + for (int i = 1; i < (1 << M); i++) + { + dp[i] = INF; + for (int j = i; j; j = (j - 1) & i) + { + if (self_edge[j]) + { + if (dp[i - j] + 1 < dp[i]) + { + dp[i] = dp[i - j] + 1; + from[i] = i - j; + } + } + } + } + int color[MAXM] = {}; + for (int c = 1, i = (1 << M) - 1; i; c++, i = from[i]) + { + int lvl = i ^ (from[i]); + for (int j = 0; j < M; j++) + { + if ((lvl >> j) & 1) + { + color[j] = c; + } + } + } + printf("%d\n", dp[(1 << M) - 1] - 2); + for (int i = 0; i < N; i++) + { + int x = color[R[R1[i][0]]], y = color[R[R2[i][0]]]; + if (x > y) + { + printf("%s %s\n", R1[i], R2[i]); + } + else + { + printf("%s %s\n", R2[i], R1[i]); + } + } + } + return 0; +} diff --git a/144.cpp b/144.cpp new file mode 100644 index 0000000..1b960f7 --- /dev/null +++ b/144.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int main() +{ + int n, k; + while (cin >> n >> k) + { + if (n == 0 && k == 0) + { + break; + } + queue> q; + for (int i = 1; i <= n; i++) + { + q.push(make_pair(i, 40)); + } + int tmp; + int atm_store = 1; + int next_limit = min(2, k); + pair student; + while (!q.empty()) + { + student = q.front(); + q.pop(); + tmp = min(student.second, atm_store); + student.second -= tmp; + atm_store -= tmp; + if (atm_store == 0) + { + atm_store = next_limit; + next_limit++; + if (next_limit > k) + { + next_limit = 1; + } + } + if (student.second > 0) + { + q.push(student); + } + else + { + cout << setw(3) << student.first; + } + } + cout << endl; + } + return 0; +} diff --git a/1440.cpp b/1440.cpp new file mode 100644 index 0000000..9f4c51d --- /dev/null +++ b/1440.cpp @@ -0,0 +1,206 @@ +#include + +using namespace std; + +#define MAXN 111 +#define MAXM 55555 +#define INF 1000000007 + +struct EDGE +{ + int v, next; + int w; +} edge[MAXM]; +int head[MAXN], e; +void init() +{ + memset(head, -1, sizeof(head)); + e = 0; +} +void add(int u, int v, int w) +{ + edge[e].v = v; + edge[e].w = w; + edge[e].next = head[u]; + head[u] = e++; + edge[e].v = u; + edge[e].w = 0; + edge[e].next = head[v]; + head[v] = e++; +} +int n, nt; +int h[MAXN]; +int gap[MAXN]; +int src, des; +int dfs(int pos, int cost) +{ + if (pos == des) + { + return cost; + } + int j, minh = n - 1; + int lv = cost, d; + for (j = head[pos]; j != -1; j = edge[j].next) + { + int v = edge[j].v; + int w = edge[j].w; + if (w > 0) + { + if (h[v] + 1 == h[pos]) + { + if (lv < edge[j].w) + { + d = lv; + } + else + { + d = edge[j].w; + } + d = dfs(v, d); + edge[j].w -= d; + edge[j ^ 1].w += d; + lv -= d; + if (h[src] >= n) + { + return cost - lv; + } + if (lv == 0) + { + break; + } + } + if (h[v] < minh) + { + minh = h[v]; + } + } + } + if (lv == cost) + { + --gap[h[pos]]; + if (gap[h[pos]] == 0) + { + h[src] = n; + } + h[pos] = minh + 1; + ++gap[h[pos]]; + } + return cost - lv; +} +int sap() +{ + int ret = 0; + memset(gap, 0, sizeof(gap)); + memset(h, 0, sizeof(h)); + gap[0] = n; + while (h[src] < n) + { + ret += dfs(src, INF); + } + return ret; +} +int d[MAXN]; +typedef pair PII; +vector g[MAXN]; +int ans[MAXN]; +int cnt, flag; +void dfs(int u) +{ + int f = 0; + ans[cnt++] = u; + for (int i = 0; i < g[u].size(); i++) + { + if (flag) + { + return; + } + int v = g[u][i].first; + if (!g[u][i].second) + { + continue; + } + f = 1; + --g[u][i].second; + dfs(v); + } + if (!f) + { + d[u]--, flag = 1; + } +} +int main() +{ + int u, v, t; + while (scanf("%d", &nt) != EOF) + { + init(); + memset(d, 0, sizeof(d)); + for (int i = 1; i <= nt; i++) + { + scanf("%d", &t); + while (t--) + { + scanf("%d", &v); + d[i]--; + d[v]++; + add(i, v, INF); + } + } + src = nt + 1; + des = nt + 2; + n = nt + 2; + int res = 0; + for (int i = 1; i <= nt; i++) + { + if (d[i] > 0) + { + add(src, i, d[i]); + } + else if (d[i] < 0) + { + add(i, des, -d[i]), res -= d[i]; + } + } + printf("%d\n", res - sap()); + for (int i = 1; i <= nt; i++) + { + g[i].clear(); + } + for (int i = 1; i <= nt; i++) + { + for (int j = head[i]; j != -1; j = edge[j].next) + { + if ((j & 1) || edge[j].v > nt) + { + continue; + } + g[i].push_back(make_pair(edge[j].v, edge[j ^ 1].w + 1)); + d[i] -= edge[j ^ 1].w; + d[edge[j].v] += edge[j ^ 1].w; + } + } + for (int i = 1; i <= nt; i++) + { + while (d[i] < 0) + { + cnt = 0; + flag = 0; + d[i]++; + dfs(i); + for (int j = 0; j < cnt; j++) + { + printf("%d", ans[j]); + if (j == cnt - 1) + { + printf("\n"); + } + else + { + printf(" "); + } + } + } + } + } + return 0; +} diff --git a/1442.cpp b/1442.cpp new file mode 100644 index 0000000..421eab9 --- /dev/null +++ b/1442.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +const int maxn = 10e6 + 10; +const int inf = 0x3f3f3f3f; +int n, ans; + +struct _node +{ + int f, c; +} node[maxn]; + +int main(void) +{ + int ncase; + scanf("%d", &ncase); + while (ncase--) + { + scanf("%d", &n); + for (int i = 1; i <= n; ++i) + { + scanf("%d", &node[i].f); + } + for (int i = 1; i <= n; ++i) + { + scanf("%d", &node[i].c); + } + int t = inf; + for (int i = n; i >= 1; --i) + { + t = max(node[i].f, t); + t = min(node[i].c, t); + node[i].c = t; + } + ans = 0; + t = inf; + for (int i = 1; i <= n; ++i) + { + t = max(node[i].f, t); + t = min(node[i].c, t); + ans += (t - node[i].f); + } + cout << ans << endl; + } + return 0; +} diff --git a/1443.cpp b/1443.cpp new file mode 100644 index 0000000..709c9bd --- /dev/null +++ b/1443.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +#define EPS 1e-8 +#define MAXN 65536 +#define INF 0x3f3f3f3f + +int W[MAXN], sum[MAXN] = {}, dp[MAXN][2], N, M, D; + +// bin srch `heaviest half-seg` +// dp[previous i-th][odd/even seg] = minimum #seg +// #odd seg = x => #odd seg = y (> x), the heaviest half-seg won't be larger, +// and split other seg to make #seg = M-1, #even seg and so on. +// ex. (x1 x2 x3, y3 y2 y1) // discuss odd half-seg, even half-seg is trivial +// x1 + x2 + x3 <= limit, y3 + y2 + y1 <= limit +// => (x1, x2) (x3, y3) (y2, y1) 3 segs // additional 2 seg +// x1, x2, x3, y1, y2, y3 <= limit +// but split to 2 seg is impossible. (must have even) + +int test(int limit) +{ + dp[0][0] = 0; + dp[0][1] = INF; + for (int i = 2; i <= N; i += 2) + { + dp[i][0] = INF; + dp[i][1] = INF; + for (int j = 1; j <= D && i - j * 2 >= 0; j++) + { + int front = sum[i - j] - sum[i - j * 2], + back = sum[i] - sum[i - j]; + if (back > limit) + { + break; + } + if (front <= limit) + { + dp[i][0] = min(dp[i][0], dp[i - j * 2][1] + 1); + dp[i][1] = min(dp[i][1], dp[i - j * 2][0] + 1); + } + } + } + return dp[N][(M - 1) % 2] <= M - 1; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &N, &M, &D); + int mx = 0; + for (int i = 1; i <= N; i++) + { + scanf("%d", &W[i]); + sum[i] = sum[i - 1] + W[i]; + mx = max(mx, W[i]); + } + if (N % 2 == 1 || N < 2 * (M - 1) || N > 2 * D * (M - 1)) + { + printf("BAD\n"); + continue; + } + int l = 1, r = mx * D, mid, ret = 1; + while (l <= r) + { + mid = (l + r) / 2; + if (test(mid)) + { + r = mid - 1; + ret = mid; + } + else + { + l = mid + 1; + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1445.cpp b/1445.cpp new file mode 100644 index 0000000..98a312c --- /dev/null +++ b/1445.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int a[11], b[11], vis[21]; + +int main() +{ + int w, d; + while (scanf("%d%d", &w, &d) && (w + d)) + { + int ans = 0; + memset(vis, 0, sizeof(vis)); + for (int i = 0; i < w; i++) + { + scanf("%d", &a[i]); + ans += a[i]; + vis[a[i]]++; + } + for (int i = 0; i < d; i++) + { + scanf("%d", &b[i]); + if (!vis[b[i]]) + ans += b[i]; + else + vis[b[i]]--; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1449.cpp b/1449.cpp new file mode 100644 index 0000000..0d1cec7 --- /dev/null +++ b/1449.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +struct node +{ + int count, id; + struct node *next[26]; + struct node *fail; + void init() + { + int i; + for (i = 0; i < 26; i++) + { + next[i] = NULL; + } + count = 0; + fail = NULL; + id = -1; + } +} * root; +void insert(char *str, int id) +{ + int len, k; + node *p = root; + len = strlen(str); + for (k = 0; k < len; k++) + { + int pos = str[k] - 'a'; + if (p->next[pos] == NULL) + { + p->next[pos] = new node; + p->next[pos]->init(); + p = p->next[pos]; + } + else + { + p = p->next[pos]; + } + } + p->count++; + p->id = id; +} +void getfail() +{ + int i; + node *p = root, *son, *temp; + queue que; + que.push(p); + while (!que.empty()) + { + temp = que.front(); + que.pop(); + for (i = 0; i < 26; i++) + { + son = temp->next[i]; + if (son != NULL) + { + if (temp == root) + { + son->fail = root; + } + else + { + p = temp->fail; + while (p) + { + if (p->next[i]) + { + son->fail = p->next[i]; + break; + } + p = p->fail; + } + if (!p) + { + son->fail = root; + } + } + que.push(son); + } + } + } +} +int num[200]; +char str[1000000 + 100]; +void query() +{ + int len, i, cnt = 0; + len = strlen(str); + node *p, *temp; + p = root; + for (i = 0; i < len; i++) + { + int pos = str[i] - 'a'; + while (!p->next[pos] && p != root) + { + p = p->fail; + } + p = p->next[pos];// + if (!p) + { + p = root;// + } + temp = p; + /*ä¸è¦ç”¨*temp=*p 因为*p表示一个node,而*temp也表示一个node 但是由于*temp没有分é…空间 所以是ä¸èƒ½è¿›è¡Œèµ‹å€¼çš„ 但是å¯ä»¥ç”¨temp指针去指å‘p*/ + while (temp != root) + { + if (temp->count >= 1) + { + if (temp->id != -1) + { + num[temp->id]++; + } + // temp->count=-1; + } + temp = temp->fail; + } + } + //printf("%d\n",cnt); +} +char rem[160][100]; +int main() +{ + int cas, n; + while (scanf("%d", &n) != EOF) + { + if (!n) + { + break; + } + root = new node; + root->init(); + root->fail = NULL; + int i; + getchar(); + for (i = 0; i < n; i++) + { + gets(rem[i]); + // gets(str); + insert(rem[i], i); + } + getfail(); + memset(num, 0, sizeof(num)); + gets(str); + query(); + int maxnum = -1; + for (i = 0; i < n; i++) + { + // printf("num[%d]=%d\n",i,num[i]); + if (num[i] > maxnum) + { + maxnum = num[i]; + } + } + printf("%d\n", maxnum); + for (i = 0; i < n; i++) + if (maxnum == num[i]) + { + printf("%s\n", rem[i]); + } + } + return 0; +} diff --git a/145.cpp b/145.cpp new file mode 100644 index 0000000..40886f0 --- /dev/null +++ b/145.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +double call_rate(char step, int time_of_day) +{ + double rates[3][5] = {{0.10, 0.25, 0.53, 0.87, 1.44}, + {0.06, 0.15, 0.33, 0.47, 0.80}, + {0.02, 0.05, 0.13, 0.17, 0.30}}; + return rates[time_of_day][step - 'A']; +} + +void print_result(string &number, int day, int evening, int night, char step) +{ + printf("%10s%6d%6d%6d%3c%8.2f\n", number.c_str(), day, evening, night, step, call_rate(step, 0) * day + call_rate(step, 1) * evening + call_rate(step, 2) * night); +} + +void inc_time(int &h1, int &m1) +{ + m1++; + if (m1 == 60) + { + h1++; + m1 = 0; + } + if (h1 == 24) + { + h1 = 0; + } +} + +int main() +{ + char step; + string number; + int h1, m1, h2, m2; + while (cin >> step) + { + if (step == '#') + { + break; + } + cin >> number >> h1 >> m1 >> h2 >> m2; + // boundary case start == end --> 24h call + if (h1 == h2 && m1 == m2) + { + print_result(number, 600, 240, 600, step); + continue; + } + int day, evening, night; + day = evening = night = 0; + while (h1 != h2 || m1 != m2) + { + if (h1 >= 8 && h1 < 18) + { + day++; + } + else if (h1 >= 18 && h1 < 22) + { + evening++; + } + else + { + night++; + } + inc_time(h1, m1); + } + print_result(number, day, evening, night, step); + } + return 0; +} diff --git a/1450.cpp b/1450.cpp new file mode 100644 index 0000000..25f937e --- /dev/null +++ b/1450.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +const int maxn = 5001; +int n, a[maxn], b[maxn]; +bool check(int k) +{ + int suma = 0, sumb = 0, count = 0, coua = 0, coub = 0; + for (int i = 0; i < n; i++) + { + suma += a[i]; + sumb += b[i]; + if (a[i] > k + 1 || b[i] > k + 1) + { + return false; + } + while (suma > k) + { + if (count == 0 || coua == 0) + { + return false; + } + count--; + coua--; + suma--; + } + while (sumb > k) + { + if (count == 0 || coub == 0) + { + return false; + } + count--; + coub--; + sumb--; + } + if (coua + 1 <= suma) + { + coua++; + } + if (coub + 1 <= sumb) + { + coub++; + } + if (count + 1 <= suma + sumb) + { + count++; + } + } + return true; +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + int suma = 0, sumb = 0; + for (int i = 0; i < n; i++) + { + scanf("%d%d", &a[i], &b[i]); + suma += a[i]; + sumb += b[i]; + } + int l = 0, r = max(suma, sumb) * 2, ans = 0; + while (l + 1 < r) + { + int mid = l + ((r - l) >> 1); + // printf("lr %d %d %d\n",l,r,mid); + if (check(mid)) + { + ans = mid; + r = mid; + } + else + { + l = mid; + } + } + printf("%d\n", l); + } + return 0; +} diff --git a/1451.cpp b/1451.cpp new file mode 100644 index 0000000..9370803 --- /dev/null +++ b/1451.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int maxn = 10e5 + 10; + +char str[maxn]; +int q[maxn], sum[maxn]; + +double dis(int a, int b) +{ + return (sum[b] - sum[a]) * 1.0 / (b - a); +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n, m; + scanf("%d%d", &n, &m); + scanf("%s", str + 1); + sum[0] = 0; + for (int i = 1; i <= n; i++) + sum[i] = sum[i - 1] + str[i] - '0'; + int ansl = 0, ansr = m; + double ans = dis(0, m); + int front = 0, rear = -1, length = m; + for (int i = m; i <= n; i++) + { + int ita = i - m; + while (front < rear && dis(q[rear], ita) <= dis(q[rear - 1], ita)) + rear--; + q[++rear] = ita; + while (front < rear && dis(q[front], i) <= dis(q[front + 1], i)) + front++; + double itb = dis(q[front], i); + if (itb > ans) + { + ans = itb; + ansl = q[front]; + ansr = i; + length = i - q[front]; + } + else if (itb == ans && length > i - q[front]) + { + ansl = q[front]; + ansr = i; + length = i - q[front]; + } + } + printf("%d %d\n", ansl + 1, ansr); + } + return 0; +} diff --git a/1452.cpp b/1452.cpp new file mode 100644 index 0000000..39383d6 --- /dev/null +++ b/1452.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +typedef long long ll; +typedef vector VI; +typedef pair PII; +typedef vector VPII; +const int NN = 105; +const int mod = 1000000007; +const int inf = 0x7f7f7f7f; +const ll INF = ll(inf) * inf; +const double eps = 1e-9; +#define rd cin >> +#define bit(n) (1 << (n)) +#define bit64(n) (ll(1) << (n)) +#define pb push_back +#define mp make_pair +#define oo -inf; +#define cl clear() +#define rep(i, i0, in) for (int i = i0; i < in; i++) +#define all(a) a.begin(), a.end() +#define lla(a) a.rbegin(), r.rend() +#define __fill(ar, val) memset(ar, val, sizeof ar) +#define sqr(x) ((x) * (x)) +#define cub(x) ((x) * (x) * (x)) +#define ff first +#define ss second +#define timestamp(x) printf("Time : %.3lf s.\n", clock() * 1.0 / CLOCKS_PER_SEC) +//#define int long long +template +inline bool odd(Int x) { return x & 1; } +template +inline bool even(Int x) { return !odd(x); } +template +inline int size(const Int &a) { return (int)a.size(); } + +int main() +{ + ios::sync_with_stdio(false); + + int n, k, t; + cin >> t; + while (t--) + { + cin >> n >> k; + int a1 = (k % 2 + k) % 3; + int a2 = ((k - 1) % 2 + (k)) % 3; + int a3 = ((k - 2) % 2 + (k - 1)) % 3; + int a4 = ((k - 1) % 2 + (k - 1)) % 3; + for (int i = 4; i <= n; i++) + { + a1 = (a1 + k) % i; + a2 = (a2 + k) % i; + a3 = (a3 + k) % i; + a4 = (a4 + k) % i; + } + if (a3 == a2) + cout << a4 + 1 << ' '; + else + cout << a3 + 1 << ' '; + cout << a2 + 1 << ' ' << a1 + 1 << endl; + } + return 0; +} diff --git a/1453.cpp b/1453.cpp new file mode 100644 index 0000000..52f8497 --- /dev/null +++ b/1453.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +int n, m; +int tt; +const double pi = acos(-1.0); + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) : x(x), y(y) {} +}; +typedef Point Vector; +Point P[440000], ch[440000]; +Vector d[440000]; +Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } +Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } +Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); } +Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); } +bool operator<(Point a, Point b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +const double eps = 1e-8; +int dcmp(double x) +{ + if (fabs(x) < eps) + return 0; + else + return x < 0 ? -1 : 1; +} +bool operator==(Point a, Point b) +{ + return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; +} +//平方 +double sqr(double x) +{ + return x * x; +} +double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; } +double Area2(Point A, Point B, Point C) +{ + return Cross(B - A, C - A); +} + +//点积 +double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; } +//长度 +double Length(Vector A) { return sqrt(Dot(A, A)); } +//夹角 +double Angle(Vector A, Vector B) +{ + return acos(Dot(A, B) / Length(A) / Length(B)); +} +Point GetLineIntersection(Point P, Vector v, Point Q, Vector w) +{ + Vector u = P - Q; + double t = Cross(w, u) / Cross(v, w); + return P + v * t; +} + +double DistanceToline(Point P, Point A, Point B) +{ + Vector v1 = B - A, v2 = P - A; + return fabs(Cross(v1, v2)) / Length(v1); +} + +double DistanceToSegment(Point P, Point A, Point B) +{ + if (A == B) + return Length(P - A); + Vector v1 = B - A, v2 = P - A, v3 = P - B; + if (dcmp(Dot(v1, v2)) > 0) + return Length(v2); + else if (dcmp(Dot(v1, v3)) > 0) + return Length(v3); + else + return fabs(Cross(v1, v2)) / Length(v1); +} + +//线段相交判定 +bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2) +{ + double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1), + c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1); + return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; +} +//线段相交是å¦在端点上 +bool OnSegment(Point p, Point a1, Point a2) +{ + return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; +} + +double PolygonArea(Point *p, int n) +{ + double area = 0; + for (int i = 0; i < n - 1; ++i) + { + area += Cross(p[i] - p[0], p[i + 1] - p[0]); + } + return area / 2; +} + +int ConvexHull(Point *p, int n, Point *ch) +{ + sort(p, p + n); + n = unique(p, p + n) - p; + int m = 0; + for (int i = 0; i < n; ++i) + { + while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + int k = m; + for (int i = n - 2; i >= 0; i--) + { + while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) + m--; + ch[m++] = p[i]; + } + if (n > 1) + m--; + return m; +} +double RotatingCalipers(Point *ch, int n) +{ + int q = 1; + double ans = 0; + for (int i = 0; i < n; i++) + { + while (Cross(ch[i + 1] - ch[i], ch[q + 1] - ch[i]) > Cross(ch[i + 1] - ch[i], ch[q] - ch[i])) + q = (q + 1) % n; + ans = max(ans, max(Length(ch[i] - ch[q]), Length(ch[i + 1] - ch[q + 1]))); + } + return ans; +} +int L; +Vector tb[440000]; + +int main() +{ + //freopen("a.txt","r",stdin); + cin >> tt; + while (tt--) + { + cin >> n; + int sp = 0; + double x, y, w; + for (int i = 0; i < n; i++) + { + cin >> x >> y >> w; + d[sp].x = x; + d[sp].y = y; + sp++; + d[sp].x = x + w; + d[sp].y = y; + sp++; + d[sp].x = x; + d[sp].y = y + w; + sp++; + d[sp].x = x + w; + d[sp].y = y + w; + sp++; + } + n = sp; + int num = ConvexHull(d, n, tb); + double ret = RotatingCalipers(tb, num); + cout << (int)(ret * ret + 0.5) << endl; + } + return 0; +} diff --git a/1455.cpp b/1455.cpp new file mode 100644 index 0000000..eaf3078 --- /dev/null +++ b/1455.cpp @@ -0,0 +1,172 @@ +#include + +using namespace std; + +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 + +const int maxn = 1000002; + +int sum[2][maxn << 2], add[2][maxn << 2]; + +struct FA +{ + int fa, u, d, size; +} f[maxn]; + +int find(int a) +{ + return f[a].fa = (a == f[a].fa ? a : find(f[a].fa)); +} +int p[maxn]; + +void push_down(int rt, int flag) +{ + if (add[flag][rt]) + { + add[flag][rt << 1] += add[flag][rt], add[flag][rt << 1 | 1] += add[flag][rt]; + sum[flag][rt << 1] += add[flag][rt], sum[flag][rt << 1 | 1] += add[flag][rt]; + add[flag][rt] = 0; + } +} + +void update(int a, int b, int flag, int c, int l, int r, int rt) +{ + if (a > b) + { + return; + } + if (a <= l && r <= b) + { + // printf ( "sum[%d][%d]" ); + sum[flag][rt] += c; + // printf ( "a = %d, b = %d, l = %d, r = %d, c = %d\n", a, b, l, r, c ); + add[flag][rt] += c; + return; + } + push_down(rt, flag); + int m = (l + r) >> 1; + if (a <= m) + { + update(a, b, flag, c, lson); + } + if (m < b) + { + update(a, b, flag, c, rson); + } + sum[flag][rt] = sum[flag][rt << 1 | 1] + sum[flag][rt << 1]; +} + +int query(int a, int flag, int l, int r, int rt) +{ + if (l == r) + { + return sum[flag][rt]; + } + push_down(rt, flag); + int m = (l + r) >> 1, ret = 0; + if (a <= m) + { + ret = query(a, flag, lson); + } + else + { + ret = query(a, flag, rson); + } + return ret; +} + +char s[111]; + +int main() +{ + int n, i, j, k, cas, a, b, m, l, r, x, y; + double c; + scanf("%d", &cas); + while (cas--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", &a, &b); + f[i].u = f[i].d = p[i] = b * 2; + f[i].size = 1, f[i].fa = i; + update(b, b, 0, 1, 0, maxn - 111, 1); + update(b, b, 1, 1, 0, maxn - 111, 1); + } + memset(sum, 0, sizeof(sum)); + memset(add, 0, sizeof(add)); + scanf("%d", &m); + while (m--) + { + scanf("%s", s); + if (s[0] == 'l') + { + scanf("%lf", &c); + c *= 2; + printf("%d %d\n", query(c, 1, 0, maxn - 111, 1), query(c, 0, 0, maxn - 111, 1)); + } + else + { + scanf("%d%d", &l, &r); + x = find(l), y = find(r); + if (x == y) + { + continue; + } + int u1 = f[x].u, u2 = f[y].u, d1 = f[x].d, d2 = f[y].d; + // printf ( "%d %d %d %d %d %d\n", x, y, u1, u2, d1, d2 ); + if (d1 > u2) + { + update(u2 + 1, u1, 0, f[y].size, 0, maxn - 111, 1); + update(d2, d1 - 1, 0, f[x].size, 0, maxn - 111, 1); + if (d1 > u2 + 1) + { + update(u2 + 1, d1 - 1, 1, 1, 0, maxn - 111, 1); + } + } + else if (d2 > u1) + { + update(u1 + 1, u2, 0, f[x].size, 0, maxn - 111, 1); + update(d1, d2 - 1, 0, f[y].size, 0, maxn - 111, 1); + if (d2 > u1 + 1) + { + update(u1 + 1, d2 - 1, 1, 1, 0, maxn - 111, 1); + } + } + else if (u2 >= d1 && u2 <= u1) + { + update(u2 + 1, u1, 0, f[y].size, 0, maxn - 111, 1); + if (d2 < d1) + { + update(d2, d1 - 1, 0, f[x].size, 0, maxn - 111, 1); + } + else + { + update(d1, d2 - 1, 0, f[y].size, 0, maxn - 111, 1); + } + update(max(d1, d2), u2, 1, -1, 0, maxn - 111, 1); + } + else + { + update(u1 + 1, u2, 0, f[x].size, 0, maxn - 111, 1); + if (d1 < d2) + { + update(d1, d2 - 1, 0, f[y].size, 0, maxn - 111, 1); + } + else + { + update(d2, d1 - 1, 0, f[x].size, 0, maxn - 111, 1); + } + update(max(d1, d2), u1, 1, -1, 0, maxn - 111, 1); + } + f[y].u = max(f[y].u, f[x].u); + f[y].d = min(f[y].d, f[x].d); + f[x].fa = y; + f[y].size += f[x].size; + // printf ( "%d %d\n", query ( 9, 1, 0, maxn - 111, 1 ), query ( 9, 0, 0, maxn - 111, 1 ) ); + } + } + } + return 0; +} diff --git a/1456.cpp b/1456.cpp new file mode 100644 index 0000000..68cf11f --- /dev/null +++ b/1456.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +//#pragma comment(linker, "/STACK:1024000000,1024000000") + +typedef long long ll; +typedef pair pii; +#define pb(a) push_back(a) +#define INF 0x1f1f1f1f +#define lson idx << 1, l, mid +#define rson idx << 1 | 1, mid + 1, r +#define PI 3.1415926535898 +template +T min(const T &a, const T &b, const T &c) +{ + return min(min(a, b), min(a, c)); +} +template +T max(const T &a, const T &b, const T &c) +{ + return max(max(a, b), max(a, c)); +} +void debug() +{ +#ifdef ONLINE_JUDGE +#else + freopen("d:\\in.txt", "r", stdin); + freopen("d:\\out1.txt", "w", stdout); +#endif +} +int getch() +{ + int ch; + while ((ch = getchar()) != EOF) + { + if (ch != ' ' && ch != ' ') + { + return ch; + } + } + return EOF; +} +int n, w; +int u[110]; +int dp[110][110]; +int f(int k, int g) +{ + if (dp[k][g] >= 0) + { + return dp[k][g]; + } + if (k > n) + { + if (g == 0) + { + return 0; + } + else + { + return INF; + } + } + int minn = INT_MAX; + int num = 0; + for (int i = k; i <= n && n - i >= g - 1; i++) + { + num += u[i]; + minn = min(minn, f(i + 1, g - 1) + i * num); + } + return dp[k][g] = minn; +} +int main() +{ + int t; + scanf("%d", &t); + for (int ca = 1; ca <= t; ca++) + { + scanf("%d%d", &n, &w); + for (int i = 1; i <= n; i++) + { + scanf("%d", &u[i]); + } + sort(u + 1, u + 1 + n, greater()); + memset(dp, -1, sizeof(dp)); + int num = f(1, w); + int sum = 0; + for (int i = 1; i <= n; i++) + { + sum += u[i]; + } + printf("%.4lf\n", (double)num / sum); + } + return 0; +} diff --git a/146.cpp b/146.cpp index 5ec77ac..878a76e 100644 --- a/146.cpp +++ b/146.cpp @@ -1,21 +1,26 @@ -#include -#include -#include +#include + using namespace std; -int main() { - while (true) { - char ch[51]; - scanf("%s", ch); - int L = strlen(ch); - - if (strcmp(ch, "#") == 0) - return 0; - - if (next_permutation(ch, ch + L)) - printf("%s\n", ch); +int main() +{ + char p[60]; + while (scanf("%s", p) == 1) + { + if (strcmp(p, "#") == 0) + { + break; + } + int n; + n = (int)strlen(p); + if (next_permutation(p, p + n)) + { + printf("%s\n", p); + } else + { printf("No Successor\n"); - } - return 0; + } + } + return 0; } diff --git a/1462.cpp b/1462.cpp new file mode 100644 index 0000000..7ef8cb0 --- /dev/null +++ b/1462.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +const int maxn = 3000005; +const int sigma_size = 26; + +struct Tire +{ + int sz; + int g[maxn][sigma_size]; + int val[maxn], vis[maxn]; + + int deep, ans; + char s[15]; + + void init(); + int idx(char ch); + void insert(char *str); + int solve(char *str, int x); + void dfs(int u, int d, int x); + void clear(int u, int flag); +} AC; + +int main() +{ + int n, x; + char str[15]; + while (scanf("%d", &n) == 1) + { + AC.init(); + + for (int i = 0; i < n; i++) + { + scanf("%s", str); + AC.insert(str); + } + + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s%d", str, &x); + printf("%d\n", AC.solve(str, x)); + } + } + return 0; +} + +void Tire::init() +{ + sz = 1; + val[0] = 0; + //memset(vis, 0, sizeof(vis)); + memset(g[0], 0, sizeof(g[0])); +} + +int Tire::idx(char ch) +{ + return ch - 'a'; +} + +void Tire::dfs(int u, int d, int x) +{ + if (x < 0) + return; + + if (vis[u] == 0) + vis[u] = 1; + + if (d == deep) + { + vis[u] = 2; + return; + } + + int v = idx(s[d]); + if (g[u][v]) + dfs(g[u][v], d + 1, x); + + dfs(u, d + 1, x - 1); + + for (int i = 0; i < sigma_size; i++) + { + if (g[u][i]) + { + dfs(g[u][i], d, x - 1); + dfs(g[u][i], d + 1, x - 1); + } + } +} + +void Tire::clear(int u, int flag) +{ + if (vis[u] == 0) + return; + + if (flag && vis[u] == 2) + { + ans += val[u]; + flag = 0; + } + + for (int i = 0; i < sigma_size; i++) + { + if (g[u][i]) + clear(g[u][i], flag); + } + vis[u] = 0; +} + +int Tire::solve(char *str, int x) +{ + ans = 0; + deep = strlen(str); + strcpy(s, str); + + dfs(0, 0, x); + clear(0, 1); + return ans; +} + +void Tire::insert(char *str) +{ + int u = 0, n = strlen(str); + + for (int i = 0; i < n; i++) + { + int v = idx(str[i]); + + if (g[u][v] == 0) + { + val[sz] = 0; + memset(g[sz], 0, sizeof(g[sz])); + g[u][v] = sz++; + } + + u = g[u][v]; + val[u]++; + } +} diff --git a/1464.cpp b/1464.cpp new file mode 100644 index 0000000..376075a --- /dev/null +++ b/1464.cpp @@ -0,0 +1,208 @@ +#include + +using namespace std; + +#define esp 1e-6 +#define pi acos(-1.0) +#define pb push_back +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 +#define mp(a, b) make_pair((a), (b)) +#define in freopen("in.txt", "r", stdin); +#define out freopen("out.txt", "w", stdout); +#define print(a) printf("%d\n", (a)); +#define bug puts("********))))))"); +#define stop system("pause"); +#define Rep(i, c) for (__typeof(c.end()) i = c.begin(); i != c.end(); i++) +#define inf 0x0f0f0f0f + +typedef long long LL; +typedef vector VI; +typedef pair pii; +typedef vector VII; +typedef vector VIII; +typedef VI::iterator IT; +#define eid first +#define vtx second + +const int maxn = 10000 + 10000; +const int maxm = 100000 + 10000; +int pre[maxn], low[maxn], bccno[maxn], ebccno[maxm], iscut[maxn], vis[maxm], ans[maxn], pa[maxn], bcc_cnt, dfs_clock; +VI g[maxn], bcc[maxn]; +VII adj[maxn], query[maxn]; +struct edge +{ + int u, v; +} ee[maxm]; +stack S; + +int dfs(int u, int fa) +{ + int lowu = pre[u] = ++dfs_clock; + int child = 0; + for (int i = 0; i < adj[u].size(); i++) + { + int v = adj[u][i].vtx; + if (!pre[v]) + { + S.push(adj[u][i].eid); + child++; + vis[adj[u][i].eid] = 1; + int lowv = dfs(v, u); + lowu = min(lowu, lowv); + if (lowv >= pre[u]) + { + iscut[u] = 1; + bcc_cnt++; + bcc[bcc_cnt].clear(); + for (;;) + { + int e = S.top(); + S.pop(); + bcc[bcc_cnt].pb(e); + ebccno[e] = bcc_cnt; + bccno[ee[e].u] = bccno[ee[e].v] = bcc_cnt; + if (e == adj[u][i].eid) + { + break; + } + } + } + } + else if (pre[v] < pre[u]) + { + if (v != fa) + { + lowu = min(lowu, pre[v]); + S.push(adj[u][i].eid); + vis[adj[u][i].eid] = 1; + } + else + { + if (!vis[adj[u][i].eid]) + { + lowu = min(lowu, pre[v]); + S.push(adj[u][i].eid); + vis[adj[u][i].eid] = 1; + } + } + } + } + if (fa < 0 && child == 1) + { + iscut[u] = 0; + } + return low[u] = lowu; +} +void find_bcc(int n) +{ + memset(pre, 0, sizeof(pre)); + memset(bccno, 0, sizeof(bccno)); + memset(iscut, 0, sizeof(iscut)); + memset(ebccno, 0, sizeof(ebccno)); + memset(vis, 0, sizeof(vis)); + while (!S.empty()) + { + S.pop(); + } + dfs_clock = bcc_cnt = 0; + for (int i = 0; i < n; i++) + if (!pre[i]) + { + dfs(i, -1); + } +} +int flag[maxn]; +int dis[maxn]; +int vv[maxn]; +int findset(int x) +{ + return pa[x] == x ? x : pa[x] = findset(pa[x]); +} +void tarjan(int u, int d) +{ + vv[u] = 1; + dis[u] = d; + pa[u] = u; + if (u > bcc_cnt) + { + dis[u]++; + } + for (int i = 0; i < query[u].size(); i++) + { + int v = query[u][i].first; + int id = query[u][i].second; + if (vis[v]) + { + ans[id] = dis[u] + dis[v] - 2 * dis[findset(v)] + (findset(v) > bcc_cnt); + } + } + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (!vv[v]) + { + tarjan(v, dis[u]); + pa[v] = u; + } + } +} +int main(void) +{ + int n, m; + while (scanf("%d%d", &n, &m), n || m) + { + for (int i = 0; i < maxn; i++) + { + adj[i].clear(), g[i].clear(), query[i].clear(); + } + memset(flag, -1, sizeof(flag)); + for (int i = 1; i <= m; i++) + { + int u, v; + scanf("%d%d", &u, &v); + u--, v--; + adj[u].pb(mp(i, v)); + adj[v].pb(mp(i, u)); + ee[i].u = u, ee[i].v = v; + } + find_bcc(n); + int cut_cnt = 0; + for (int i = 0; i < n; i++) + if (iscut[i]) + { + cut_cnt++; + int u = cut_cnt + bcc_cnt; + for (int k = 0; k < adj[i].size(); k++) + { + int v = ebccno[adj[i][k].first]; + if (flag[v] != u) + { + flag[v] = u; + g[v].pb(u); + g[u].pb(v); + } + } + } + int q; + for (int i = scanf("%d", &q); i <= q; i++) + { + int s, t; + scanf("%d%d", &s, &t); + s = ebccno[s], t = ebccno[t]; + query[s].pb(mp(t, i)); + query[t].pb(mp(s, i)); + } + memset(vv, 0, sizeof(vv)); + for (int i = 1; i <= bcc_cnt + cut_cnt; i++) + if (!vv[i]) + { + tarjan(i, 0); + } + for (int i = 1; i <= q; i++) + { + printf("%d\n", ans[i]); + } + } + return 0; +} diff --git a/1466.cpp b/1466.cpp new file mode 100644 index 0000000..78017aa --- /dev/null +++ b/1466.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +const int maxn = 3005; +const int maxm = 300005; + +struct TwoSAT +{ + int n, s[maxn * 2], c; + bool mark[maxn * 2]; + vector g[maxn * 2]; + + void init(int n) + { + this->n = n; + memset(mark, false, sizeof(mark)); + for (int i = 0; i < 2 * n; i++) + g[i].clear(); + } + + void addClause(int x, int xflag, int y, int yflag) + { + x = x * 2 + xflag; + y = y * 2 + yflag; + g[x ^ 1].push_back(y); + g[y ^ 1].push_back(x); + } + + bool dfs(int u) + { + if (mark[u ^ 1]) + return false; + if (mark[u]) + return true; + mark[u] = true; + s[c++] = u; + + for (int i = 0; i < g[u].size(); i++) + if (!dfs(g[u][i])) + return false; + return true; + } + + bool solve() + { + for (int i = 0; i < 2 * n; i += 2) + { + if (!mark[i] && !mark[i + 1]) + { + c = 0; + if (!dfs(i)) + { + while (c) + mark[s[--c]] = false; + if (!dfs(i + 1)) + return false; + } + } + } + return true; + } +} solver; + +const int dx[][2] = {{0, 1}, {1, 0}}; +const int dy[][2] = {{0, 1}, {0, 1}}; + +int N, idx[maxn], X[maxn], Y[maxn], F[maxn], C[maxn]; +int M, L[maxm], R[maxm], W[maxm]; +int E, first[maxn], jump[maxm * 2], link[maxm * 2], val[maxm * 2]; + +inline int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); } +inline int distance(int a, int b) { return abs(X[a] - X[b]) + abs(Y[a] - Y[b]); } +inline void addEdge(int u, int v, int w) +{ + jump[E] = first[u]; + link[E] = v; + val[E] = w; + first[u] = E++; +} + +void init() +{ + E = 0; + memset(first, -1, sizeof(first)); + + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d%d", &X[i], &Y[i]); + F[i] = i, C[i] = 1; + } + + int u, v; + scanf("%d", &M); + for (int i = 0; i < M; i++) + { + scanf("%d%d%d", &u, &v, &W[i]); + L[i] = --u; + R[i] = --v; + if (find(u) != find(v)) + { + C[find(v)] += C[find(u)]; + F[find(u)] = find(v); + } + addEdge(u, v, W[i]); + addEdge(v, u, W[i]); + } +} + +bool draw(int u, int c) +{ + if (idx[u] != -1) + return idx[u] == c; + idx[u] = c; + for (int i = first[u]; i != -1; i = jump[i]) + { + int v = link[i], w = val[i]; + if (!draw(v, c ^ ((distance(u, v) & 1) ^ (w & 1)))) + return false; + } + return true; +} + +bool judge(int u, int k) +{ + memset(idx, -1, sizeof(idx)); + if (!draw(u, k)) + return false; + solver.init(N); + + for (int i = 0; i < M; i++) + { + if (find(L[i]) != u || find(R[i]) != u) + continue; + for (int p = 0; p < 2; p++) + { + for (int q = 0; q < 2; q++) + { + int x0 = X[L[i]] + dx[idx[L[i]]][p]; + int y0 = Y[L[i]] + dy[idx[L[i]]][p]; + int x1 = X[R[i]] + dx[idx[R[i]]][q]; + int y1 = Y[R[i]] + dy[idx[R[i]]][q]; + if (abs(x0 - x1) + abs(y0 - y1) != W[i]) + solver.addClause(L[i], p, R[i], q); + } + } + } + return solver.solve(); +} + +int main() +{ + int cas; + scanf("%d", &cas); + while (cas--) + { + init(); + bool flag = true; + for (int i = 0; i < N; i++) + { + if (i != F[i]) + { + continue; + } + if (!judge(i, 0) && !judge(i, 1)) + { + flag = false; + break; + } + } + printf("%s\n", flag ? "possible" : "impossible"); + } + return 0; +} diff --git a/1467.cpp b/1467.cpp new file mode 100644 index 0000000..bc7137a --- /dev/null +++ b/1467.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const int maxn = 501; +struct node +{ + int s; + int d; + bool operator<(const node &a) const + { + if (d == a.d) + { + return s < a.s; + } + return d < a.d; + } +} a[maxn]; +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d%d", &a[i].s, &a[i].d); + } + sort(a, a + n); + int now = 0, ta = 0, tb = 0, pos = 0; + for (int i = 0; i < n; i++) + { + now += a[i].s; + int t = max(now - a[i].d, 0); + if (t > ta) + { + tb = ta; + ta = t; + pos = i; + } + else if (t > tb) + { + tb = t; + pos = i; + } + } + int ans = ta + tb; + for (int i = 0; i <= pos; i++) + { + now = 0, ta = 0, tb = 0; + for (int j = 0; j <= pos; j++) + { + if (i == j) + { + continue; + } + now += a[j].s; + int t = max(now - a[j].d, 0); + if (t > ta) + { + tb = ta; + ta = t; + } + else if (t > tb) + { + tb = t; + } + } + now += a[i].s; + int t = max(now - a[i].d, 0); + if (t > ta) + { + tb = ta; + ta = t; + } + else if (t > tb) + { + tb = t; + } + ans = min(ans, ta + tb); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1468.cpp b/1468.cpp new file mode 100644 index 0000000..98968ee --- /dev/null +++ b/1468.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +const int maxn = 60001; +const int inf = 1 << 28; + +int miny[maxn], maxy[maxn], h[maxn]; + +int main() +{ + int T; + scanf("%d", &T); + int n, m; + int x1, y1, x2, y2; + while (T--) + { + scanf("%d%d", &n, &m); + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + if (x1 > x2) + { + swap(x1, x2); + swap(y1, y2); + } + for (int i = x1 + 1; i < x2; i++) + { + miny[i] = inf; + maxy[i] = -inf; + } + for (int i = 2; i < m; i++) + { + int ita, itb; + scanf("%d%d", &ita, &itb); + if (itb >= y1) + miny[ita] = min(miny[ita], itb); + if (itb <= y1) + maxy[ita] = max(maxy[ita], itb); + } + for (int i = x1; i <= x2; i++) + h[i] = min(y1 - maxy[i], miny[i] - y1); + h[x1] = 0; + for (int i = x1 + 1; i < x2; i++) + h[i] = min(h[i], h[i - 1] + 1); + h[x2] = 0; + for (int i = x2 - 1; i > x1; i--) + h[i] = min(h[i], h[i + 1] + 1); + long long ans = 0; + for (int i = x1; i <= x2; i++) + if (h[i]) + { + ans++; + ans += min(h[i] - 1, y1); + ans += min(h[i] - 1, n - y1 - 1); + } + cout << ans << endl; + } + return 0; +} diff --git a/1469.cpp b/1469.cpp new file mode 100644 index 0000000..5327564 --- /dev/null +++ b/1469.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +typedef long long ll; + +struct Point3 { + int x, y, z; + + Point3(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} + bool operator<(const Point3 &u) const { + return x - u.x < 0 || (x - u.x == 0 && y - u.y < 0) || + (x - u.x == 0 && y - u.y == 0 && z - u.z < 0); + } + bool operator>(const Point3 &u) const { return u < (*this); } + bool operator==(const Point3 &u) const { + return !(u < (*this) || (*this) < u); + } + bool operator!=(const Point3 &u) const { return !((*this) == u); } + bool operator<=(const Point3 &u) const { return *this < u || *this == u; } + bool operator>=(const Point3 &u) const { return *this > u || *this == u; } + Point3 operator+(const Point3 &u) const { + return Point3(x + u.x, y + u.y, z + u.z); + } + Point3 operator-(const Point3 &u) const { + return Point3(x - u.x, y - u.y, z - u.z); + } + Point3 operator*(const int u) const { return Point3(x * u, y * u, z * u); } + Point3 operator/(const int u) const { return Point3(x / u, y / u, z / u); } + + void read() { scanf("%d%d%d", &x, &y, &z); } +}; + +typedef Point3 Vector3; +ll getDot(Vector3 a, Vector3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } +Vector3 getCross(Vector3 a, Vector3 b) { + return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x); +} +ll getPowerLength(Vector3 u) { return getDot(u, u); } + +ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } + +struct Rat { + ll s, m; + Rat(ll s = 0, ll m = 1) { + ll d = gcd(s, m); + s /= d, m /= d; + if (m < 0) + m = -m, s = -s; + this->s = s; + this->m = m; + } + + Rat operator+(const Rat &u) const { + ll d = gcd(m, u.m); + return Rat(s * (u.m / d) + u.s * (m / d), m * (u.m / d)); + } + Rat operator-(const Rat &u) const { + ll d = gcd(m, u.m); + return Rat(s * (u.m / d) - u.s * (m / d), m * (u.m / d)); + } + Rat operator*(const Rat &u) const { return Rat(s * u.s, m * u.m); } + // Rat operator * (const int& u) const { return Rat(s*u, m); } + // Rat operator / (const Rat& u) const { return Rat(s*u.m, m*u.s); } + // Rat operator / (const int& u) const { return Rat(s, m*u); } + bool operator<(const Rat &u) const { return s * u.m < u.s * m; } + bool operator>(const Rat &u) const { return u < (*this); } + bool operator==(const Rat &u) const { return !(u < (*this) || (*this) < u); } + bool operator!=(const Rat &u) const { return !((*this) == u); } + bool operator<=(const Rat &u) const { return *this < u || *this == u; } + bool operator>=(const Rat &u) const { return *this > u || *this == u; } +}; + +inline int dcmp(Rat u) { + if (u.s == 0) + return 0; + else + return u.s < 0 ? -1 : 1; +} + +Rat getDistancePointToSegment(Point3 p, Point3 a, Point3 b) { + if (a == b) + return getPowerLength(p - a); + Vector3 v1 = b - a, v2 = p - a, v3 = p - b; + if (getDot(v1, v2) < 0) + return getPowerLength(v2); + else if (getDot(v1, v3) > 0) + return getPowerLength(v3); + else + return Rat(getPowerLength(getCross(v1, v2)), getPowerLength(v1)); +} + +bool getDistanceLineToLine(Point3 p1, Vector3 u, Point3 p2, Vector3 v, Rat &s) { + ll b = getDot(u, u) * getDot(v, v) - getDot(u, v) * getDot(u, v); + if (b == 0) + return false; + ll a = getDot(u, v) * getDot(v, p1 - p2) - getDot(v, v) * getDot(u, p1 - p2); + s = Rat(a, b); + return true; +} + +const ll inf = 0x3f3f3f3f; + +int main() { + int cas; + scanf("%d", &cas); + while (cas--) { + Point3 a, b, c, d; + a.read(), b.read(), c.read(), d.read(); + + Rat s, t, ans(inf); + bool flag1 = getDistanceLineToLine(a, b - a, c, d - c, s); + bool flag2 = getDistanceLineToLine(c, d - c, a, b - a, t); + if (flag1 && flag2 && s.s > 0 && s.s < s.m && t.s > 0 && t.s < t.m) { + Vector3 u = b - a, v = d - c; + Rat x1 = Rat(a.x) + s * u.x, y1 = Rat(a.y) + s * u.y, + z1 = Rat(a.z) + s * u.z; + Rat x2 = Rat(c.x) + t * v.x, y2 = Rat(c.y) + t * v.y, + z2 = Rat(c.z) + t * v.z; + ans = + (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1); + } else { + ans = min(ans, getDistancePointToSegment(a, c, d)); + ans = min(ans, getDistancePointToSegment(b, c, d)); + ans = min(ans, getDistancePointToSegment(c, a, b)); + ans = min(ans, getDistancePointToSegment(d, a, b)); + } + printf("%lld %lld\n", ans.s, ans.m); + } + return 0; +} diff --git a/147.cpp b/147.cpp index 967e705..50b2e68 100644 --- a/147.cpp +++ b/147.cpp @@ -1,44 +1,40 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#define N 1000000 -using namespace std; - -int main (){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int coins [] = {1, 2, 4, 10, 20, 40, 100, 200, 400, 1000, 2000}; - double dp [6000 + 10]; - - memset (dp, 0, sizeof (dp)); - - dp [0] = 1; - - for ( int i = 0; i < 11; i++ ) { - for ( int j = 1; j <= 6000; j++ ) { - if ( j - coins [i] >= 0 ) - dp [j] += dp [j - coins [i]]; - } - } - - double c; - - while ( scanf ("%lf", &c) ) { - if ( c <= 0 ) break; - int index = c * 20; - - printf ("%6.2lf%17.lf\n", c, dp [index]); - } - - return 0; -} +#include + +using namespace std; + +const int MAXSIZE = 30000; +const int coin[] = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000}; + +long long dp[MAXSIZE + 10]; + +void init() +{ + int i, j; + dp[0] = 1; + for (i = 0; i < 11; i++) + { + for (j = 1; j <= MAXSIZE; j++) + { + if (j >= coin[i]) + { + dp[j] += dp[j - coin[i]]; + } + } + } +} + +int main() +{ + init(); + double f; + while (EOF != scanf("%lf", &f)) + { + int val = (int)(f * 100 + 1e-10); + if (!val) + { + break; + } + printf("%6.2lf%17lld\n", f, dp[val]); + } + return 0; +} diff --git a/1470.cpp b/1470.cpp new file mode 100644 index 0000000..0cd185b --- /dev/null +++ b/1470.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +const int MAXN = 300010; +const int MAXSTR = MAXN * 2 + 2; + +struct Manacher +{ + int n; + char s[MAXN]; + int p[MAXSTR]; + Manacher() + { + n = 0; + memset(s, 0, sizeof(s)); + memset(p, 0, sizeof(p)); + } + Manacher(char *_s) + { + n = 0; + memset(s, 0, sizeof(s)); + if (_s) + { + memcpy(s, _s, sizeof(char) * (strlen(_s) + 1)); + n = strlen(s); + } + memset(p, 0, sizeof(p)); + } + void mem(char *_s) + { + n = 0; + memset(s, 0, sizeof(s)); + if (_s) + { + memcpy(s, _s, sizeof(char) * (strlen(_s) + 1)), n = strlen(s); + } + memset(p, 0, sizeof(p)); + } + char str[MAXSTR]; + void work() + { + str[0] = '*'; + for (int i = 0; i < n; i++) + { + str[2 * i + 1] = '_'; + str[2 * i + 2] = s[i]; + } + str[2 * n + 1] = '_'; + str[2 * n + 2] = '\0'; + n = 2 * n + 2; + memset(p, 0, sizeof(p)); + int mx = 0, id = 0; + for (int i = 1; i < n; i++) + { + if (i < mx) + { + p[i] = min(p[2 * id - i], mx - i); + } + while (str[i - p[i]] == str[i + p[i]]) + { + ++p[i]; + } + if (mx < i + p[i]) + { + mx = i + p[i]; + id = i; + } + } + } +} S; + +char s[MAXN]; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%s", s); + S.mem(s); + S.work(); + int ans = 0; + for (int i = 1; i <= S.n - 1; i += 2) + { + if (S.p[i] == 1) + { + continue; + } + for (int l = (S.p[i] - 1) / 4 * 4; l > ans; l -= 4) + { + if (S.p[i - l / 2] >= l / 2 && S.p[i + l / 2] >= l / 2) + { + ans = max(ans, l); + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1471.cpp b/1471.cpp new file mode 100644 index 0000000..a519f3b --- /dev/null +++ b/1471.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +const int maxn = 200010; + +int num[maxn], ab[maxn]; +int un[maxn], e; + +struct T +{ + int ll, rr, v; +} p[maxn * 3]; + +void build(int s, int e, int num) +{ + p[num].ll = s, p[num].rr = e; + p[num].v = 0; + if (s != e) + { + int mid = (s + e) / 2; + build(s, mid, num * 2 + 1); + build(mid + 1, e, num * 2 + 2); + } +} + +void update(int pos, int k, int num) +{ + if (p[num].ll == p[num].rr) + { + p[num].v = max(p[num].v, k); + return; + } + int mid = (p[num].ll + p[num].rr) / 2; + if (pos <= mid) + { + update(pos, k, num * 2 + 1); + } + else + { + update(pos, k, num * 2 + 2); + } + p[num].v = max(p[num * 2 + 1].v, p[num * 2 + 2].v); +} + +int query(int s, int e, int num) +{ + if (p[num].ll == s && p[num].rr == e) + { + return p[num].v; + } + int mid = (p[num].ll + p[num].rr) / 2; + if (e <= mid) + { + return query(s, e, num * 2 + 1); + } + else if (s > mid) + { + return query(s, e, num * 2 + 2); + } + else + { + return max(query(s, mid, num * 2 + 1), query(mid + 1, e, num * 2 + 2)); + } +} + +int main() +{ + int T, n, i, j, k; + int ans; + scanf("%d", &T); + while (T--) + { + ans = 1; + scanf("%d", &n); + for (i = 1; i <= n; ++i) + { + scanf("%d", &num[i]); + un[i - 1] = num[i]; + } + sort(un, un + n); + e = unique(un, un + n) - un; + for (i = 1; i <= n; ++i) + { + num[i] = lower_bound(un, un + e, num[i]) - un; + } + build(0, e, 0); + ab[0] = 0; + num[0] = num[n + 1] = -1; + for (i = 1; i <= n; ++i) + { + if (num[i] > num[i - 1]) + { + ab[i] = ab[i - 1] + 1; + } + else + { + ab[i] = 1; + } + } + k = 0; + for (i = n; i >= 1; --i) + { + ans = max(ans, ab[i] + query(num[i] + 1, e, 0)); + if (num[i] < num[i + 1]) + { + k++; + } + else + { + k = 1; + } + update(num[i], k, 0); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1473.cpp b/1473.cpp new file mode 100644 index 0000000..df87e9a --- /dev/null +++ b/1473.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int MAXN = 10005; + +double x[MAXN], y[MAXN], z[MAXN], distO[MAXN]; +int n; + +double geth(double r) +{ + double mxh = 0.0f; + for (int i = 0; i < n; i++) + { + mxh = max(mxh, z[i] * r / (r - distO[i])); + } + return mxh; +} + +double f(double r) +{ + return r * r * M_PI * geth(r) / 3; +} + +int main() +{ + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf%lf", &x[i], &y[i], &z[i]); + } + for (int i = 0; i < n; i++) + { + distO[i] = hypot(x[i], y[i]); + } + double l = 0.0f, r = 10000.0f, mid, midmid, md, mmd; + for (int it = 0; it < 100; it++) + { + mid = (l + r) / 2; + midmid = (mid + r) / 2; + md = f(mid); + mmd = f(midmid); + if (md < mmd) + { + r = midmid; + } + else + { + l = mid; + } + } + printf("%.3lf %.3lf\n", geth(mid), mid); + } + return 0; +} diff --git a/1474.cpp b/1474.cpp new file mode 100644 index 0000000..fe92fae --- /dev/null +++ b/1474.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +/* ********************************************** +Author : Nero +Created Time: 2013-9-1 7:38:36 +Problem id : LA 4987 +Problem Name: Evacuation Plan +*********************************************** */ + +#define REP(i, a, b) for (int i = (a); i < (int)(b); i++) +#define clr(a, b) memset(a, b, sizeof(a)) +typedef long long lld; + +const lld INF = (lld)1e15; +lld dp[2][4001];// å‰Âi个é¿难所包å«å‰Âj个队的最å°Âè·Â离 +struct She +{ + int pos, id; + bool operator<(const She &tt) const + { + return pos < tt.pos; + } +} b[4001], a[4001]; +int n, m; +int ans[4001]; +bool path[4001][4001]; + +void dfs(int x, int y) +{ + if (x == 0 || y == 0) + return; + ans[a[y].id] = b[x].id; + dfs(x - path[x][y], y - 1); +} + +int main() +{ + while (~scanf("%d", &n)) + { + for (int i = 1; i <= n; i++) + scanf("%d", &a[i].pos), a[i].id = i; + scanf("%d", &m); + for (int i = 1; i <= m; i++) + scanf("%d", &b[i].pos), b[i].id = i; + sort(a + 1, a + n + 1); + sort(b + 1, b + m + 1); + for (int i = 0; i <= n; i++) + dp[0][i] = INF; + dp[0][0] = 0; + for (int i = 1; i <= m; i++) + { + int now = i & 1; + int pre = !now; + dp[now][i - 1] = INF; + for (int j = i; j <= n; j++) + { + dp[now][j] = abs(a[j].pos - b[i].pos); + if (dp[now][j - 1] <= dp[pre][j - 1]) + { + dp[now][j] += dp[now][j - 1]; + path[i][j] = 0; + } + else + { + dp[now][j] += dp[pre][j - 1]; + path[i][j] = 1; + } + } + } + printf("%lld\n", dp[m & 1][n]); + // for(int i = 1; i <= m; i ++) { + // for(int j = 1; j <= n; j ++) printf("%20lld", dp[i][j]); + // printf("\n"); + // } + dfs(m, n); + for (int i = 1; i <= n; i++) + printf("%d%c", ans[i], i == n ? '\n' : ' '); + } + return 0; +} diff --git a/1475.cpp b/1475.cpp new file mode 100644 index 0000000..8a7d255 --- /dev/null +++ b/1475.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +//精度控制 +const double eps = 1e-10; +int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + return x < 0 ? -1 : 1; +} + +//点 +struct Point +{ + double x, y; + Point() {} + Point(double x, double y) : x(x), y(y) {} +}; + +//å‘釠+typedef Point Vector; + +//点-点==å‘釠+Vector operator-(Point A, Point B) +{ + return Vector(A.x - B.x, A.y - B.y); +} + +//å‘é‡Â+å‘é‡Â==å‘釠+Vector operator+(Vector A, Vector B) +{ + return Vector(A.x + B.x, A.y + B.y); +} + +//å‘é‡Â*实数==å‘釠+Vector operator*(Vector A, double p) +{ + return Vector(A.x * p, A.y * p); +} + +//å‰积 +double Cross(Vector A, Vector B) +{ + return A.x * B.y - A.y * B.x; +} + +//有方å‘的直线 +struct Line +{ + Point p; + Vector v; + double ang; + Line() {} + Line(Point p, Vector v) : p(p), v(v) + { + ang = atan2(v.y, v.x); + } + bool operator<(const Line &L) const + { + return ang < L.ang; + } +}; + +//判断点p是å¦在直线L左边 +bool OnLeft(Line L, Point p) +{ + return Cross(L.v, p - L.p) > 0; +} + +//得到a与b两直线的交点 +Point GetIntersection(Line a, Line b) +{ + Vector u = a.p - b.p; + double t = Cross(b.v, u) / Cross(a.v, b.v); + return a.p + a.v * t; +} + +//返回åŠ平é¢交的凸多边形poly节点集åˆ +int HalfplaneIntersection(Line *L, int n, Point *poly) +{ + sort(L, L + n); + int first = 0, last = 0; + Point *p = new Point[n]; + Line *q = new Line[n]; + q[0] = L[0]; + for (int i = 1; i < n; i++) + { + while (first < last && !OnLeft(L[i], p[last - 1])) + { + last--; + } + while (first < last && !OnLeft(L[i], p[first])) + { + first++; + } + q[++last] = L[i]; + if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) + { + last--; + if (OnLeft(q[last], L[i].p)) + { + q[last] = L[i]; + } + } + if (first < last) + { + p[last - 1] = GetIntersection(q[last - 1], q[last]); + } + } + while (first < last && !OnLeft(q[first], p[last - 1])) + { + last--; + } + if (last - first <= 1) + { + return 0; + } + p[last] = GetIntersection(q[last], q[first]); + int m = 0; + for (int i = first; i <= last; i++) + { + poly[m++] = p[i]; + } + return m; +} +/***以上为刘æ±Â佳模æ¿***/ + +const int maxn = 50000 + 5; +Point p[maxn], poly[maxn]; +Line L[maxn]; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + } + if (n == 3)//三角形的è¯Â,直接删除一个点å³å¯ + { + printf("1\n"); + continue; + } + reverse(p, p + n);//将所有顶点逆时针存放 + //当炸掉n-2个连续的点时,åŠ平é¢交肯定是空 + int left = 1, right = n - 2; + while (right > left) + { + int mid = left + (right - left) / 2; + for (int i = 0; i < n; i++) + { + L[i] = Line(p[i], p[(i + 1 + mid) % n] - p[i]); + } + int m = HalfplaneIntersection(L, n, poly); + //m>0表示åŠ平é¢交éž空,那么需è¦Â加炸弹 + if (m > 0) + { + left = mid + 1; + } + else + { + right = mid; + } + } + printf("%d\n", left); + } + return 0; +} diff --git a/1476.cpp b/1476.cpp new file mode 100644 index 0000000..9e8f10b --- /dev/null +++ b/1476.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +#define Mod (1000000007LL) +#define eps (1e-6) +#define inf (-10000000000.0) + +double a[10010], b[10010], c[10010]; +double x1, x2, m1, m2; +double y, y2; +int n; +inline double Abs(double x) +{ + return max(x, -x); +} +inline double f(double x) +{ + double tmp = inf; + for (int i = 1; i <= n; i++) + tmp = max(tmp, x * x * a[i] + b[i] * x + c[i]); + return tmp; +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + x1 = 0; + x2 = 1000; + for (int i = 1; i <= n; i++) + scanf("%lf%lf%lf", &a[i], &b[i], &c[i]); + y = 0; + y2 = f(1000.0); + while (Abs(y - y2) > eps) + { + //printf("%.2lf %.2lf\n",x1,x2); + m1 = x1 + (x2 - x1) / 3.0; + m2 = x2 - (x2 - x1) / 3.0; + y = f(m1); + y2 = f(m2); + if (y < y2) + x2 = m2; + else + x1 = m1; + } + printf("%.4lf\n", y); + } + return 0; +} diff --git a/1478.cpp b/1478.cpp new file mode 100644 index 0000000..650f712 --- /dev/null +++ b/1478.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define N 100001 +#define M 25 +#define Mod 100000000 +#define INF 2000000007 + +int n, m; + +struct _int +{ + long long d[10001]; + int len; + _int() + { + len = 1; + memset(d, 0, sizeof(d)); + } + void multiply(long long x) + { + for (int i = 1; i <= len; ++i) + { + d[i] *= x; + } + for (int i = 1; i <= len; ++i) + { + d[i + 1] += d[i] / Mod, d[i] %= Mod; + } + while (d[len + 1]) + { + ++len; + } + } + void divid(long long x) + { + for (int i = len; i >= 1; --i) + { + d[i - 1] += (d[i] % x) * Mod; + d[i] /= x; + } + while (len > 1 && d[len] == 0) + { + --len; + } + } + void add(_int x) + { + if (x.len > len) + { + len = x.len; + } + if (len > 14) + { + len = 14; + } + for (int i = 1; i <= len; ++i) + { + d[i] += x.d[i]; + } + for (int i = 1; i <= len; ++i) + { + d[i + 1] += d[i] / Mod, d[i] %= Mod; + } + if (d[len + 1]) + { + ++len; + } + } + void print() + { + len = 13; + d[len] %= 10000; + while (len > 1 && d[len] == 0) + { + --len; + } + printf("%lld", d[len]); + for (int i = len - 1; i >= 1; --i) + { + if (d[i] < 10000000) + { + printf("0"); + } + if (d[i] < 1000000) + { + printf("0"); + } + if (d[i] < 100000) + { + printf("0"); + } + if (d[i] < 10000) + { + printf("0"); + } + if (d[i] < 1000) + { + printf("0"); + } + if (d[i] < 100) + { + printf("0"); + } + if (d[i] < 10) + { + printf("0"); + } + printf("%lld", d[i]); + } + printf("\n"); + } +}; + +void Solve() +{ + int i, j, k; + _int ans, x; + x.d[1] = 1; + ans.add(x); + for (i = 1; i <= n / 2; ++i) + { + x.multiply(1ll * (n - 2 * i + 1) * (n - 2 * i + 2)); + x.divid(1ll * (i + 1) * i); + ans.add(x); + } + ans.print(); +} + +int main() +{ + while (scanf("%d", &n) != EOF) + { + Solve(); + } + return 0; +} diff --git a/1479.cpp b/1479.cpp new file mode 100644 index 0000000..208fd65 --- /dev/null +++ b/1479.cpp @@ -0,0 +1,287 @@ +#include + +using namespace std; + +struct Node +{ + Node *ch[2]; + int r, v, s;//s表示节点数 + Node(int v) : v(v) + { + ch[0] = ch[1] = NULL; + r = rand(); + s = 1; + } + bool operator<(const Node &b) const + { + return r < b.r; + } + int cmp(int x) + { + if (x == v) + { + return -1; + } + return x < v ? 0 : 1; + } + void maintain() + { + s = 1; + if (ch[0] != NULL) + { + s += ch[0]->s; + } + if (ch[1] != NULL) + { + s += ch[1]->s; + } + } +}; +void rotate(Node *&o, int d) +{ + Node *k = o->ch[d ^ 1]; + o->ch[d ^ 1] = k->ch[d]; + k->ch[d] = o; + o->maintain(); + k->maintain(); + o = k; +} +void insert(Node *&o, int x)//oå­Â树中事先ä¸Â存在x +{ + if (o == NULL) + { + o = new Node(x); + } + else + { + int d = (x < o->v) ? 0 : 1;//å…Â许相åŒ的v值在Treap中,所以注æ„Â这里的写法,ä¸Â能用cmp + insert(o->ch[d], x); + if (o->ch[d] > o) + { + rotate(o, d ^ 1); + } + } + o->maintain(); +} +void remove(Node *&o, int x)//oå­Â树中实现必须存在x +{ + int d = o->cmp(x); + if (d == -1) + { + Node *u = o; + if (o->ch[0] && o->ch[1]) + { + int d2 = (o->ch[0] > o->ch[1]) ? 1 : 0; + rotate(o, d2); + remove(o->ch[d2], x); + } + else + { + if (o->ch[0] == NULL) + { + o = o->ch[1]; + } + else + { + o = o->ch[0]; + } + delete u; + } + } + else + { + remove(o->ch[d], x); + } + if (o) + { + o->maintain();//之å‰Âo存在,但是删除节点åÂÂŽoå¯能就是空NULL了,所以需è¦Â先判断o是å¦为空 + } +} +const int maxn = 20000 + 100; +const int maxm = 60000 + 100; +int weight[maxn]; +struct edge +{ + int u, v; +} edges[maxm]; +struct command +{ + int type;//0,1,2,对于D,Q,C + int x, p; +} coms[600000 + 1000]; +int cnt;//命令æ¡数 +bool removed[maxm]; +int F[maxn]; +int findset(int i) +{ + if (F[i] == -1) + { + return i; + } + return F[i] = findset(F[i]); +} +Node *nodes[maxn]; +int kth(Node *o, int k) +{ + if (o == NULL || k <= 0 || k > o->s) + { + return 0; + } + int s = (o->ch[1] == NULL) ? 0 : o->ch[1]->s; + if (k == s + 1) + { + return o->v; + } + else if (k <= s) + { + return kth(o->ch[1], k); + } + else + { + return kth(o->ch[0], k - s - 1); + } +} +void merge(Node *&a, Node *&b) +{ + if (a->ch[0]) + { + merge(a->ch[0], b); + } + if (a->ch[1]) + { + merge(a->ch[1], b); + } + insert(b, a->v); + delete a; + a = NULL; +} +void removetree(Node *&a) +{ + if (a->ch[0]) + { + removetree(a->ch[0]); + } + if (a->ch[1]) + { + removetree(a->ch[1]); + } + delete a; + a = NULL; +} +void add_edge(int e) +{ + int x = findset(edges[e].u), y = findset(edges[e].v); + if (x != y) + { + if (nodes[x]->s < nodes[y]->s) + { + F[x] = y; + merge(nodes[x], nodes[y]); + } + else + { + F[y] = x; + merge(nodes[y], nodes[x]); + } + } +} +int query_cnt; +long long query_tot; +void query(int x, int k) +{ + int fx = findset(x); + query_tot += kth(nodes[fx], k); + query_cnt++; +} +void change_weight(int x, int v) +{ + int u = findset(x); + remove(nodes[u], weight[x]); + insert(nodes[u], v); + weight[x] = v; +} +int main() +{ + int n, m, kase = 0; + while (scanf("%d%d", &n, &m) == 2 && n) + { + if (n == 0 && m == 0) + { + break; + } + memset(removed, 0, sizeof(removed)); + for (int i = 1; i <= n; i++) + { + scanf("%d", &weight[i]); + } + for (int i = 1; i <= m; i++) + { + scanf("%d%d", &edges[i].u, &edges[i].v); + } + char str[100]; + int x, p; + cnt = 0; + while (scanf("%s", str) == 1) + { + if (str[0] == 'E') + { + break; + } + scanf("%d", &x); + if (str[0] == 'D') + { + coms[cnt++] = (command){ + 0, x, 0}; + removed[x] = true; + } + else if (str[0] == 'Q') + { + scanf("%d", &p); + coms[cnt++] = (command){ + 1, x, p}; + } + else if (str[0] == 'C') + { + int v; + scanf("%d", &v); + p = weight[x]; + weight[x] = v; + coms[cnt++] = (command){ + 2, x, p}; + } + } + for (int i = 1; i <= n; i++) + { + F[i] = -1; + if (nodes[i]) + { + removetree(nodes[i]); + } + nodes[i] = new Node(weight[i]); + } + for (int i = 1; i <= m; i++) + if (!removed[i]) + { + add_edge(i); + } + query_tot = query_cnt = 0; + for (int i = cnt - 1; i >= 0; i--) + { + if (coms[i].type == 0) + { + add_edge(coms[i].x); + } + else if (coms[i].type == 1) + { + query(coms[i].x, coms[i].p); + } + else if (coms[i].type == 2) + { + change_weight(coms[i].x, coms[i].p); + } + } + //printf("tot=%I64d, cnt=%d\n",query_tot,query_cnt); + printf("Case %d: %.6lf\n", ++kase, query_tot / (double)query_cnt); + } + return 0; +} diff --git a/148.cpp b/148.cpp new file mode 100644 index 0000000..52a4dfd --- /dev/null +++ b/148.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +void check(int loc, int lleft, string now); +vector totalwords, wint, words; +string temp, repeat; +int letter[26], total, ltemp[26]; + +int main() +{ + while (getline(cin, temp, '\n') && temp.find("#") == string::npos) + { + totalwords.push_back(temp); + } + while (getline(cin, temp, '\n') && temp.find("#") == string::npos) + { + wint.clear(); + words.clear(); + total = 0; + memset(letter, 0, sizeof(letter)); + for (int i = 0; i < temp.size(); i++) + if (temp[i] >= 'A' && temp[i] <= 'Z') + { + letter[temp[i] - 'A']++; + } + for (int i = 0; i < 26; i++) + { + total += letter[i]; + } + for (int i = 0; i < totalwords.size(); i++) + { + memset(ltemp, 0, sizeof(ltemp)); + for (int j = 0; j < totalwords[i].size(); j++) + { + ltemp[totalwords[i][j] - 'A']++; + } + bool ok = true; + for (int j = 0; ok && j < 26; j++) + if (ltemp[j] > letter[j]) + { + ok = false; + } + if (ok) + { + words.push_back(totalwords[i]); + } + } + stringstream sin(temp); + while (sin >> repeat) + { + wint.push_back(repeat); + } + sort(wint.begin(), wint.end()); + repeat = " ="; + for (int i = 0; i < wint.size(); i++) + { + repeat += " " + wint[i]; + } + check(0, total, " ="); + } + return 0; +} + +void check(int loc, int lleft, string now) +{ + if (lleft == 0) + { + if (now != repeat) + { + cout << temp << now << endl; + } + return; + } + else if (loc == words.size()) + { + return; + } + bool ok = true; + for (int i = 0; i < words[loc].size(); i++) + { + letter[words[loc][i] - 'A']--; + if (letter[words[loc][i] - 'A'] < 0) + { + ok = false; + } + } + if (ok) + { + check(loc + 1, lleft - words[loc].size(), now + " " + words[loc]); + } + for (int i = 0; i < words[loc].size(); i++) + { + letter[words[loc][i] - 'A']++; + } + check(loc + 1, lleft, now); +} diff --git a/1480.cpp b/1480.cpp new file mode 100644 index 0000000..bc2582f --- /dev/null +++ b/1480.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +const int MAXN = 100000 + 10; +const int SIZE = 22; +int A[MAXN], S[MAXN]; +int num[SIZE][MAXN], cnt[SIZE][MAXN]; + +bool cmp(int i, int j) { return (A[i] < A[j]); } + +void build(int l, int r, int d) +{ + if (l == r) + return; + int m = (l + r) >> 1, p = 0; + for (int i = l; i <= r; i++) + if (num[d][i] <= m) + { + num[d + 1][l + p] = num[d][i]; + cnt[d][i] = ++p; + } + else + { + num[d + 1][m + 1 + i - l - p] = num[d][i]; + cnt[d][i] = p; + } + build(l, m, d + 1); + build(m + 1, r, d + 1); +} + +int query(int l, int r, int d, int ll, int rr, int k) +{ + if (l == r) + return l; + int ls = (ll == l) ? 0 : cnt[d][ll - 1]; + int rs = cnt[d][rr]; + int m = (l + r) >> 1; + if (k <= rs - ls) + return query(l, m, d + 1, l + ls, l + rs - 1, k); + return query(m + 1, r, d + 1, m + 1 + (ll - l - ls), m + 1 + (rr - l - rs), k - (rs - ls)); +} + +struct node +{ + int type, x, y, k; +} CMD[MAXN]; + +int main() +{ + int N, M, Q, cas = 0; + while (scanf("%d", &M) == 1) + { + Q = N = 0; + while (M--) + { + char st[30]; + scanf("%s", st); + if (st[0] == 'I') + { + N++; + S[N] = N; + scanf("%d", &A[N]); + } + else + { + CMD[Q].type = st[6] - '1'; + if (st[6] == '1') + scanf("%d%d%d", &CMD[Q].x, &CMD[Q].y, &CMD[Q].k); + else if (st[6] == '2') + scanf("%d", &CMD[Q].x), CMD[Q].y = N; + else if (st[6] == '3') + { + CMD[Q].x = 1; + CMD[Q].y = N; + scanf("%d", &CMD[Q].k); + } + Q++; + } + } + sort(S + 1, S + 1 + N, cmp); + for (int i = 1; i <= N; i++) + num[0][S[i]] = i; + build(1, N, 0); + long long ret[3] = {0, 0, 0}; + for (int i = 0; i < Q; i++) + { + int x = CMD[i].x, y = CMD[i].y, k = CMD[i].k; + if (CMD[i].type == 0 || CMD[i].type == 2) + { + long long tmp = A[S[query(1, N, 0, x, y, k)]]; + ret[CMD[i].type] += tmp; + } + else + { + int l = 1, r = y + 1; + while (l < r) + { + int m = (l + r) >> 1; + int ret = A[S[query(1, N, 0, 1, y, m)]]; + if (ret >= x) + r = m; + else + l = m + 1; + } + ret[1] += r; + //printf("%d\n", r); + } + } + printf("Case %d:\n", ++cas); + for (int i = 0; i < 3; i++) + printf("%lld\n", ret[i]); + } + return 0; +} diff --git a/1481.cpp b/1481.cpp new file mode 100644 index 0000000..ecb220b --- /dev/null +++ b/1481.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const int maxn = 3001; + +int a[maxn], b[maxn], p[1000001]; + +int main() +{ + int n; + while (scanf("%d", &n) && n) + { + memset(p, 0, sizeof(p)); + for (int i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + p[a[i]] = i; + } + for (int i = 1; i <= n; i++) + scanf("%d", &b[i]); + int ans = 0; + for (int i = 1; i <= n; i++) + { + if (!p[b[i]]) + continue; + int maxpos = p[b[i]], minpos = p[b[i]]; + for (int j = i + 1; j <= n; j++) + { + if (!p[b[j]]) + break; + maxpos = max(maxpos, p[b[j]]); + minpos = min(minpos, p[b[j]]); + if (j - i > 0 && maxpos - minpos == j - i) + ans++; + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1482.cpp b/1482.cpp new file mode 100644 index 0000000..8b846c7 --- /dev/null +++ b/1482.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +#define LL long long +LL f[65]; +LL solve(LL x) +{ + int i, j; + for (i = 61; i >= 0; i--) + if (x % f[i] == f[i] - 1) + return x / f[i + 1]; +} +int main() +{ + int i, j, k; + f[0] = 1; + for (i = 1; i <= 62; i++) + f[i] = f[i - 1] << 1; + int t, n; + LL x, ans; + scanf("%d", &t); + while (t--) + { + ans = 0; + scanf("%d", &n); + while (n--) + { + scanf("%lld", &x); + ans ^= solve(x); + } + if (ans) + puts("YES"); + else + puts("NO"); + } + return 0; +} diff --git a/1484.cpp b/1484.cpp new file mode 100644 index 0000000..830bc38 --- /dev/null +++ b/1484.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f + +const int N = 500005; + +int n, l, r, dp[N], E, fst[N], nxt[N]; + +struct Edge +{ + int u, v, w; +} edge[N]; + +inline void scanf_(int &num) +{ + char in; + while ((in = getchar()) > '9' || in < '0') + ; + num = in - '0'; + while (in = getchar(), in >= '0' && in <= '9') + { + num *= 10, num += in - '0'; + } +} + +void dfs(int u, int fa, int sum, int who) +{ + if (who && fst[u] != -1) + { + dp[u] = INF; + } + else + { + dp[u] = 0; + } + for (int i = fst[u]; i != -1; i = nxt[i]) + { + int v = edge[i].v, w = edge[i].w; + if (v == fa) + { + continue; + } + dfs(v, u, sum + w, 1 - who); + if (who == 0 && dp[v] + w + sum >= l && dp[v] + w + sum <= r) + { + dp[u] = max(dp[u], dp[v] + w); + } + if (who == 1 && dp[v] + w + sum >= l && dp[v] + w + sum <= r) + { + dp[u] = min(dp[u], dp[v] + w); + } + } +} + +void add(int u, int v, int w) +{ + edge[E].u = u; + edge[E].v = v; + edge[E].w = w; + nxt[E] = fst[u]; + fst[u] = E++; +} + +int main() +{ + while (~scanf("%d%d%d", &n, &l, &r)) + { + E = 0; + memset(fst, -1, sizeof(fst)); + int u, v, w; + for (int i = 0; i < n - 1; i++) + { + scanf_(u); + scanf_(v); + scanf_(w); + add(u, v, w); + } + dfs(0, -1, 0, 0); + if (dp[0] < l || dp[0] > r) + { + printf("Oh, my god!\n"); + } + else + { + printf("%d\n", dp[0]); + } + } + return 0; +} diff --git a/1485.cpp b/1485.cpp new file mode 100644 index 0000000..f68fba0 --- /dev/null +++ b/1485.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +//*************************OUTPUT************************* +#ifdef WIN32 +#define INT64 "%I64d" +#define UINT64 "%I64u" +#else +#define INT64 "%lld" +#define UINT64 "%llu" +#endif + +//**************************CONSTANT*********************** +#define INF 0x7f7f7f7f +#define eps 1e-8 +#define PI acos(-1.) +#define PI2 asin(1.); +typedef long long LL; +//typedef __int64 LL; //codeforces +typedef unsigned int ui; +typedef unsigned long long ui64; +#define MP make_pair +typedef vector VI; +typedef pair PII; +#define pb push_back +#define mp make_pair + +//***************************SENTENCE************************ +#define CL(a, b) memset(a, b, sizeof(a)) +#define sqr(a, b) sqrt((double)(a) * (a) + (double)(b) * (b)) +#define sqr3(a, b, c) sqrt((double)(a) * (a) + (double)(b) * (b) + (double)(c) * (c)) + +//****************************FUNCTION************************ +template +double DIS(T va, T vb) +{ + return sqr(va.x - vb.x, va.y - vb.y); +} +template +inline T INTEGER_LEN(T v) +{ + int len = 1; + while (v /= 10) + { + ++len; + } + return len; +} +template +inline T square(T va, T vb) +{ + return va * va + vb * vb; +} + +// aply for the memory of the stack +//#pragma comment (linker, "/STACK:1024000000,1024000000") +//end + +#define CY 1005 +const int mod = 1000000007; + +int dp[CY][CY]; +int N, K; + +int dfs(int n, int k) +{ + if (n == 0 || n == k) + { + return dp[n][k] = 0; + } + if (k == 0) + { + return dp[n][k] = 1; + } + if (~dp[n][k]) + { + return dp[n][k]; + } + dp[n][k] = 0; + dp[n][k] = (int)((LL) - ~k * dfs(n - 1, k) % mod + (LL)(n - k) * dfs(n - 1, k - 1) % mod) % mod; + return dp[n][k]; +} + +int main() +{ + memset(dp, -1, sizeof(dp)); + while (2 == scanf("%d%d", &N, &K)) + { + printf("%d\n", dfs(N, K)); + } + return 0; +} diff --git a/1486.cpp b/1486.cpp new file mode 100644 index 0000000..b5b7489 --- /dev/null +++ b/1486.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define MAXN 2048 +#define MAXM 1048576 + +struct Node +{ + int x, y, cap; + double cost;// x->y, v + int next; +} edge[MAXM]; + +struct MinCost +{ + const int INF = 0x3f3f3f3f; + + int e, n, head[MAXN], dis[MAXN], pre[MAXN], record[MAXN], inq[MAXN]; + + void init(int n) + { + this->n = n; + e = 0; + for (int i = 0; i <= n; i++) + { + head[i] = -1; + } + } + + void add_edge(int x, int y, int cap, int cost) + { + edge[e].x = x; + edge[e].y = y; + edge[e].cap = cap; + edge[e].cost = cost; + edge[e].next = head[x]; + head[x] = e++; + edge[e].x = y; + edge[e].y = x; + edge[e].cap = 0; + edge[e].cost = -cost; + edge[e].next = head[y]; + head[y] = e++; + } + + pair mincost(int s, int t) + { + int mncost = 0, flow, totflow = 0, i, x, y; + while (1) + { + for (int i = 0; i < n; i++) + { + dis[i] = INF; + } + int oo = dis[0]; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(); + Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + pre[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + { + Q.push_front(y); + } + else + { + Q.push_back(y); + } + } + } + } + } + if (dis[t] == oo) + { + break; + } + flow = INF; + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return make_pair(mncost, totflow); + } +} g; + +int main() +{ + int N, M, K, u, v, a, c; + while (scanf("%d%d%d", &N, &M, &K) == 3) + { + g.init(N + 1); + int src = 0, sink = N; + for (int i = 0; i < M; i++) + { + scanf("%d%d%d%d", &u, &v, &a, &c); + for (int j = 1; j <= c; j++) + { + g.add_edge(u, v, 1, a * (j * j - (j - 1) * (j - 1))); + } + } + g.add_edge(src, 1, K, 0); + pair ret = g.mincost(src, sink); + printf("%d\n", ret.second == K ? ret.first : -1); + } + return 0; +} diff --git a/1487.cpp b/1487.cpp new file mode 100644 index 0000000..60a7163 --- /dev/null +++ b/1487.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +#define Mod (1000000009LL) +#define Pi (acos(-1.0)) + +double R, H, h; +double ans, tmp; +double f(double x) +{ + return 16.0 / 3.0 * x * sqrt(x); +} +int main() +{ + while (~scanf("%lf%lf", &R, &H)) + { + ans = 2.0 * R * R * Pi * H; + h = H /= 2.0; + h = min(h, R); + tmp = f(R * R) - f(R * R - h * h); + //printf("%.2lf %.2lf\n",ans,tmp); + printf("%.4f\n", ans - tmp); + } + return 0; +} diff --git a/1488.cpp b/1488.cpp new file mode 100644 index 0000000..116c891 --- /dev/null +++ b/1488.cpp @@ -0,0 +1,634 @@ +#include + +using namespace std; + +#define abs(x) ((x) >= 0 ? (x) : -(x)) +#define i64 long long +#define u32 unsigned int +#define u64 unsigned long long +#define clr(x, y) memset(x, y, sizeof(x)) +#define CLR(x) x.clear() +#define ph(x) push(x) +#define pb(x) push_back(x) +#define Len(x) x.length() +#define SZ(x) x.size() +#define PI acos(-1.0) +#define sqr(x) ((x) * (x)) +#define MP(x, y) make_pair(x, y) +#define EPS 1e-6 + +#define FOR0(i, x) for (i = 0; i < x; i++) +#define FOR1(i, x) for (i = 1; i <= x; i++) +#define FOR(i, a, b) for (i = a; i <= b; i++) +#define FORL0(i, a) for (i = a; i >= 0; i--) +#define FORL1(i, a) for (i = a; i >= 1; i--) +#define FORL(i, a, b) for (i = a; i >= b; i--) + +#define rush() \ + int CC; \ + for (scanf("%d", &CC); CC--;) +#define Rush(n) while (scanf("%d", &n) != -1) + +void RD(int &x) { scanf("%d", &x); } +void RD(i64 &x) { scanf("%lld", &x); } +void RD(u64 &x) { scanf("%I64u", &x); } +void RD(u32 &x) { scanf("%u", &x); } +void RD(double &x) { scanf("%lf", &x); } +void RD(int &x, int &y) { scanf("%d%d", &x, &y); } +void RD(i64 &x, i64 &y) { scanf("%lld%lld", &x, &y); } +void RD(u32 &x, u32 &y) { scanf("%u%u", &x, &y); } +void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); } +void RD(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); } +void RD(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); } +void RD(i64 &x, i64 &y, i64 &z) { scanf("%lld%lld%lld", &x, &y, &z); } +void RD(u32 &x, u32 &y, u32 &z) { scanf("%u%u%u", &x, &y, &z); } +void RD(char &x) { x = getchar(); } +void RD(char *s) { scanf("%s", s); } +void RD(string &s) { cin >> s; } + +void PR(int x) { printf("%d\n", x); } +void PR(int x, int y) { printf("%d %d\n", x, y); } +void PR(i64 x) { printf("%lld\n", x); } +void PR(i64 x, i64 y) { printf("%lld %lld\n", x, y); } +void PR(u32 x) { printf("%u\n", x); } +void PR(u64 x) { printf("%llu\n", x); } +void PR(double x) { printf("%.2lf\n", x); } +void PR(double x, double y) { printf("%.5lf %.5lf\n", x, y); } +void PR(char x) { printf("%c\n", x); } +void PR(char *x) { printf("%s\n", x); } +void PR(string x) { cout << x << endl; } + +const int mod = 10007; +const i64 inf = ((i64)1) << 40; +const double dinf = 1000000000000000000.0; +const int INF = 100000000; +const int N = 1005; + +int sgn(double x) +{ + if (x > EPS) + return 1; + if (x < -EPS) + return -1; + return 0; +} + +struct POINT +{ + int x, y; + + POINT() {} + POINT(int _x, int _y) + { + x = _x; + y = _y; + } + + void get() + { + RD(x, y); + } +}; + +struct point +{ + double x, y; + + point() {} + point(double _x, double _y) + { + x = _x; + y = _y; + } + + void get() + { + RD(x); + RD(y); + } + + point operator+(point a) + { + return point(x + a.x, y + a.y); + } + + point operator-(point a) + { + return point(x - a.x, y - a.y); + } + + double operator*(point a) + { + return x * a.y - y * a.x; + } + + point operator*(double t) + { + return point(x * t, y * t); + } + + double operator^(point a) + { + return x * a.x + y * a.y; + } + + double len() + { + return sqrt(x * x + y * y); + } + + point zhuanShun(double t) + { + return point(x * cos(t) + y * sin(t), y * cos(t) - x * sin(t)); + } + + point zhuanNi(double t) + { + return point(x * cos(t) - y * sin(t), x * sin(t) + y * cos(t)); + } + + point adjust(double L) + { + double d = len(); + L /= d; + return point(x * L, y * L); + } + + void print() + { + printf("%.3lf %.3lf\n", x + EPS, y + EPS); + } +}; + +double len(point a) +{ + return a.len(); +} + +struct point3 +{ + double x, y, z; + + point3() {} + point3(double _x, double _y, double _z) + { + x = _x; + y = _y; + z = _z; + } + + void get() + { + cin >> x >> y >> z; + } + + point3 operator+(point3 a) + { + return point3(x + a.x, y + a.y, z + a.z); + } + + point3 operator-(point3 a) + { + return point3(x - a.x, y - a.y, z - a.z); + } + + point3 operator*(point3 a) + { + return point3(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x); + } + + point3 operator*(double t) + { + return point3(x * t, y * t, z * t); + } + + double operator^(point3 a) + { + return x * a.x + y * a.y + z * a.z; + } + + point3 operator/(double t) + { + return point3(x / t, y / t, z / t); + } + + double len() + { + return sqrt(x * x + y * y + z * z); + } + + point3 adjust(double L) + { + double t = len(); + L /= t; + return point3(x * L, y * L, z * L); + } + + void print() + { + printf("%.10lf %.10lf %.10lf\n", x + EPS, y + EPS, z + EPS); + } +}; + +double len(point3 a) +{ + return a.len(); +} + +double getArea(point3 a, point3 b, point3 c) +{ + double x = len((b - a) * (c - a)); + return x / 2; +} + +double getVolume(point3 a, point3 b, point3 c, point3 d) +{ + double x = (b - a) * (c - a) ^ (d - a); + return x / 6; +} + +point3 pShadowOnPlane(point3 p, point3 a, point3 b, point3 c) +{ + point3 v = (b - a) * (c - a); + if (sgn(v ^ (a - p)) < 0) + v = v * -1; + v = v.adjust(1); + double d = fabs(v ^ (a - p)); + return p + v * d; +} + +double lineToLine(point3 a, point3 b, point3 p, point3 q) +{ + point3 v = (b - a) * (q - p); + return fabs((a - p) ^ v) / len(v); +} + +int pInPlane(point3 p, point3 a, point3 b, point3 c) +{ + double S = getArea(a, b, c); + double S1 = getArea(a, b, p); + double S2 = getArea(a, c, p); + double S3 = getArea(b, c, p); + return sgn(S - S1 - S2 - S3) == 0; +} + +int opposite(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + point3 v = (b - a) * (c - a); + double x = v ^ (p - a); + double y = v ^ (q - a); + return sgn(x * y) < 0; +} + +int segCrossTri(point3 p, point3 q, point3 a, point3 b, point3 c) +{ + return opposite(p, q, a, b, c) && + opposite(a, b, p, q, c) && + opposite(a, c, p, q, b) && + opposite(b, c, p, q, a); +} + +double pToPlane(point3 p, point3 a, point3 b, point3 c) +{ + double v = ((b - a) * (c - a) ^ (p - a)) / 6; + double s = len((b - a) * (c - a)) / 2; + return fabs(3 * v / s); +} + +double pToLine(point3 p, point3 a, point3 b) +{ + double S = len((a - p) * (b - p)); + return S / len(a - b); +} + +double pToSeg(point3 p, point3 a, point3 b) +{ + if (sgn((p - a) ^ (b - a)) <= 0) + return len(a - p); + if (sgn((p - b) ^ (a - b)) <= 0) + return len(b - p); + return pToLine(p, a, b); +} + +double pToPlane1(point3 p, point3 a, point3 b, point3 c) +{ + point3 k = pShadowOnPlane(p, a, b, c); + if (pInPlane(k, a, b, c)) + return pToPlane(p, a, b, c); + double x = pToSeg(p, a, b); + double y = pToSeg(p, a, c); + double z = pToSeg(p, b, c); + return min(x, min(y, z)); +} + +double getAng(point3 a, point3 b) +{ + double x = (a ^ b) / len(a) / len(b); + return acos(x); +} + +double segToSeg(point3 a, point3 b, point3 p, point3 q) +{ + point3 v = (b - a) * (q - p); + + double A, B, A1, B1; + A = ((b - a) * v) ^ (p - a); + B = ((b - a) * v) ^ (q - a); + + A1 = ((p - q) * v) ^ (a - q); + B1 = ((p - q) * v) ^ (b - q); + if (sgn(A * B) <= 0 && sgn(A1 * B1) <= 0) + { + return lineToLine(a, b, p, q); + } + + double x = min(pToSeg(a, p, q), pToSeg(b, p, q)); + double y = min(pToSeg(p, a, b), pToSeg(q, a, b)); + return min(x, y); +} + +struct face +{ + int a, b, c, ok; + + face() {} + face(int _a, int _b, int _c, int _ok) + { + a = _a; + b = _b; + c = _c; + ok = _ok; + } +}; + +struct _3DCH +{ + face F[N << 2]; + int b[N][N], cnt, n; + point3 p[N]; + + int getDir(point3 t, face F) + { + double x = (p[F.b] - p[F.a]) * (p[F.c] - p[F.a]) ^ (t - p[F.a]); + return sgn(x); + } + + void deal(int i, int x, int y) + { + int f = b[x][y]; + if (!F[f].ok) + return; + if (getDir(p[i], F[f]) == 1) + DFS(i, f); + else + { + b[y][x] = b[x][i] = b[i][y] = cnt; + F[cnt++] = face(y, x, i, 1); + } + } + + void DFS(int i, int j) + { + F[j].ok = 0; + deal(i, F[j].b, F[j].a); + deal(i, F[j].c, F[j].b); + deal(i, F[j].a, F[j].c); + } + + void construct() + { + int i, j, k = 0; + for (i = 1; i < n; i++) + if (sgn(len(p[i] - p[0]))) + { + swap(p[i], p[1]); + k++; + break; + } + if (k != 1) + return; + for (i = 2; i < n; i++) + if (sgn(getArea(p[0], p[1], p[i]))) + { + swap(p[i], p[2]); + k++; + break; + } + if (k != 2) + return; + for (i = 3; i < n; i++) + if (sgn(getVolume(p[0], p[1], p[2], p[i]))) + { + swap(p[i], p[3]); + k++; + break; + } + if (k != 3) + return; + + cnt = 0; + FOR0(i, 4) + { + face k = face((i + 1) % 4, (i + 2) % 4, (i + 3) % 4, 1); + if (getDir(p[i], k) == 1) + swap(k.b, k.c); + b[k.a][k.b] = b[k.b][k.c] = b[k.c][k.a] = cnt; + F[cnt++] = k; + } + + for (i = 4; i < n; i++) + FOR0(j, cnt) + { + if (F[j].ok && getDir(p[i], F[j]) == 1) + { + DFS(i, j); + break; + } + } + j = 0; + FOR0(i, cnt) + if (F[i].ok) F[j++] = F[i]; + cnt = j; + } + + point3 getCenter() + { + point3 ans = point3(0, 0, 0), o = point3(0, 0, 0); + double s = 0, temp; + int i; + FOR0(i, cnt) + { + face k = F[i]; + temp = getVolume(o, p[k.a], p[k.b], p[k.c]); + ans = ans + (o + p[k.a] + p[k.b] + p[k.c]) / 4 * temp; + s += temp; + } + ans = ans / s; + return ans; + } + + double getMinDis(point3 a) + { + double ans = dinf; + int i; + FOR0(i, cnt) + { + face k = F[i]; + ans = min(ans, pToPlane(a, p[k.a], p[k.b], p[k.c])); + } + return ans; + } +}; + +double a, b, c, d; +int n; +point3 p[N], S; + +void init() +{ + if (sgn(a) == 0 && sgn(b) == 0) + return; + p[n + 1] = S; + double sinC, cosC; + int i; + point3 temp; + + sinC = a / sqrt(a * a + b * b); + cosC = b / sqrt(a * a + b * b); + FOR1(i, n + 1) + { + temp = p[i]; + p[i].x = temp.x * cosC - temp.y * sinC; + p[i].y = temp.x * sinC + temp.y * cosC; + } + sinC = sqrt(a * a + b * b) / sqrt(a * a + b * b + c * c); + cosC = c / sqrt(a * a + b * b + c * c); + FOR1(i, n + 1) + { + temp = p[i]; + p[i].y = temp.y * cosC - temp.z * sinC; + p[i].z = temp.y * sinC + temp.z * cosC; + } + S = p[n + 1]; +} + +int deal() +{ + double D = a * S.x + b * S.y + c * S.z; + if (sgn(D - d) < 0) + { + a *= -1; + b *= -1; + c *= -1; + d *= -1; + D *= -1; + } + int cnt = 0, i; + FOR1(i, n) + { + if (sgn(a * p[i].x + b * p[i].y + c * p[i].z - D) < 0) + { + cnt++; + } + } + return cnt; +} + +point A[N], B[N]; +int Bnum; + +point get(point3 p) +{ + p = p - S; + double t = (d / sqrt(a * a + b * b + c * c) - S.z) / p.z; + p = S + p * t; + return point(p.x, p.y); +} + +point H; + +int cmp(point a, point b) +{ + int x = sgn((a - H) * (b - H)); + if (x) + return x == 1; + return sgn(len(a - H) - len(b - H)) <= 0; + ; +} + +int cross(point a, point b, point p) +{ + return sgn((b - a) * (p - a)); +} + +void Graham(point p[], int n, point q[], int &m) +{ + m = 0; + if (n < 3) + return; + int i, k = 0; + int a, b; + FOR0(i, n) + { + a = sgn(p[i].y - p[k].y); + b = sgn(p[i].x - p[k].x); + if (a == -1 || a == 0 && b == -1) + k = i; + } + swap(p[0], p[k]); + H = p[0]; + sort(p, p + n, cmp); + q[0] = p[0]; + q[1] = p[1]; + p[n] = p[0]; + m = 2; + for (i = 2; i <= n; i++) + { + while (m > 1 && cross(q[m - 2], q[m - 1], p[i]) <= 0) + m--; + q[m++] = p[i]; + } + m--; +} + +double cal() +{ + int i; + FOR1(i, n) + A[i - 1] = get(p[i]); + Graham(A, n, B, Bnum); + double ans = 0; + B[Bnum] = B[0]; + FOR1(i, Bnum) + ans += B[i] * B[i - 1]; + return fabs(ans / 2); +} + +int main() +{ + while (scanf("%lf", &a) == 1) + { + RD(b, c, d); + if (!a && !b && !c && !d) + { + break; + } + RD(n); + int i; + FOR1(i, n) + p[i].get(); + S.get(); + + int flag = deal(); + if (flag == 0) + puts("0.00"); + else if (flag != n) + puts("Infi"); + else + { + init(); + PR(cal()); + } + } + return 0; +} diff --git a/1489.cpp b/1489.cpp new file mode 100644 index 0000000..99d0f43 --- /dev/null +++ b/1489.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +#define CLR(a) memset(a, 0, sizeof(a)) +#define REP(i, a, b) for (int i = a; i < b; i++) +#define REP_D(i, a, b) for (int i = a; i <= b; i++) + +typedef long long ll; + +const ll maxn = 55; +const ll key = 30; +const ll MOD = 1000000003L; +ll n, a[maxn], k; +ll bit[40]; +ll dp[maxn][maxn]; + +void getBit() +{ + bit[0] = 1; + REP_D(i, 1, 31) + { + bit[i] = bit[i - 1] * 2; + } +} + +ll getLeft(int i, int have) +{ + if (have < 0) + { + return 0; + } + return (a[i] & (bit[have + 1] - 1)); +} + +ll getDp(int pos, int key, int need, int num) +{ + CLR(dp); + dp[0][0] = 1; + REP_D(i, 1, n) + { + REP_D(j, 0, num) + { + if (a[i] & bit[pos]) + { + if (i < key || i > key) + { + if (j >= 1) + { + dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] * (getLeft(i, pos - 1) + 1)) % MOD; + } + } + } + if (a[i] & bit[pos]) + { + if (i > key) + { + dp[i][j] = (dp[i][j] + dp[i - 1][j] * (bit[pos])) % MOD; + } + else if (i == key) + { + dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD; + } + } + else + { + dp[i][j] = (dp[i][j] + dp[i - 1][j] * (getLeft(i, pos - 1) + 1)) % MOD; + } + } + } + ll res = 0; + REP_D(i, 0, num) + { + if ((i & 1) == need) + { + res += dp[n][i]; + } + } + return res; +} + +ll dfs(int pos) +{ + if (pos < 0) + { + return 1; + } + ll ans = 0; + ll num = 0; + REP_D(i, 1, n) + { + if (bit[pos] & a[i]) + { + num++; + } + } + ll need = ((k & bit[pos]) >> pos); + if ((num & 1) == need) + { + ans = (ans + dfs(pos - 1)) % MOD; + } + CLR(dp); + //dp[0][0] = 1; + REP_D(i, 1, n) + { + if (a[i] & bit[pos]) + { + ans = (ans + getDp(pos, i, need, num)) % MOD; + } + } + return ans; +} + +int main() +{ + //freopen("1Ain.txt", "r", stdin); + //freopen("1Aout.txt", "w", stdout); + getBit(); + while (scanf("%lld%lld", &n, &k) != EOF) + { + if (!n && !k) + { + break; + } + REP_D(i, 1, n) + { + scanf("%lld", &a[i]); + } + printf("%lld\n", dfs(key)); + } + return 0; +} diff --git a/149.cpp b/149.cpp new file mode 100644 index 0000000..136b5c4 --- /dev/null +++ b/149.cpp @@ -0,0 +1,226 @@ +#include + +using namespace std; + +#define EPS 1.0e-10 +#define MAX 16 +#define PI (acos(-1.0)) +#define RAD(deg) (((deg) / 180) * PI) +#define DISCERNIBLE_GAP RAD(0.01) +#define SQ(a) ((a) * (a)) + +typedef double elem; +typedef complex point, vec; + +const double ok_gap = DISCERNIBLE_GAP; + +struct circle +{ + point c; + elem r; +}; + +struct tree +{ + circle c; + bool visible; + pair tangential; +}; + +inline bool eq(elem a, elem b) +{ + return abs(a - b) < EPS; +} +inline bool eqless(elem a, elem b) +{ + return eq(a, b) || a < b; +} +inline elem dist(point a, point b) +{ + return abs(b - a); +} +inline point rotp(point p, elem theta) +{ + return p * exp(point(0, theta)); +} +inline point rotp_base(point base, point p, elem theta) +{ + point ret = rotp(p - base, theta); + return ret + base; +} +inline vec uvec(vec a) +{ + return a / abs(a); +} +inline vec normr(vec a) +{ + return a * vec(0, -1); +} +inline vec norml(vec a) +{ + return a * vec(0, 1); +} +inline vec unormr(vec a) +{ + return normr(a) / abs(a); +} +inline vec unorml(vec a) +{ + return norml(a) / abs(a); +} +inline elem dot(vec a, vec b) +{ + return (a.real() * b.real() + a.imag() * b.imag()); +} +inline elem len(vec a) +{ + return abs(a); +} + +// unit vector ret.first which passes through a and contacts c is for the left side a->c.c +int UnitTangentialVecs(circle c, point a, pair &ret) +{ + if (dist(c.c, a) < c.r) + { + return 0; + } + if (dist(c.c, a) == c.r) + { + vec v = c.c - a; + ret.first = unorml(v); + ret.second = unormr(v); + return 2; + } + else + { + vec v = c.r * uvec(a - c.c); + elem d = dist(a, c.c); + elem xx = SQ(d) - SQ(c.r); + //cout << "d:"<< d << "xx:" << xx << endl; + elem theta = acos((SQ(c.r) + SQ(d) - xx) / (2 * c.r * d)); + //cout << "theta:" << theta << endl; + point p = c.c + rotp(v, theta); + ret.second = uvec(p - a); + p = c.c + rotp(v, -theta); + ret.first = uvec(p - a); + return 2; + } +} + +// ret radian val rad <=pi +inline elem Radians(vec a, vec b) +{ + return acos(dot(a, b) / len(a) * len(b)); +} + +// whether pt p lies btw two dir vect a, b, starting from pt base +bool IsBetween(point p, point base, vec a, vec b) +{ + point t = p - base; + elem rad_ab = Radians(a, b); + elem rad_ta = Radians(t, a); + elem rad_tb = Radians(t, b); + return eqless(rad_ta, rad_ab) && eqless(rad_tb, rad_ab); +} + +int SubForest(int quad, double diam, double x, double y) +{ + int ret = 0; + tree forest[MAX][MAX]; + int ix = 1, iy = 1; + const int ox[] = {1, 0, 0, 1}; + const int oy[] = {1, 1, 0, 0}; + const int dx[] = {1, -1, -1, 1}; + const int dy[] = {1, 1, -1, -1}; + ix = ox[quad]; + iy = oy[quad]; + for (int i = 0; i < MAX; ++i) + { + for (int j = 0; j < MAX; ++j) + { + forest[i][j].c.c = point(ix, iy); + forest[i][j].c.r = diam / 2.0; + forest[i][j].visible = true; + UnitTangentialVecs(forest[i][j].c, point(x, y), forest[i][j].tangential); + ix += dx[quad]; + } + ix = ox[quad]; + iy += dy[quad]; + } + for (int i = 0; i < MAX; ++i) + { + for (int j = 0; j < MAX; ++j) + { + //if( !forest[i][j].visible ) + //continue; + //cout << "Forest "< tans = forest[i][j].tangential; + if (eqless(Radians(tans.first, tans.second), ok_gap)) + { + forest[i][j].visible = false; + } + for (int k = i; k < MAX; ++k) + { + for (int l = j; l < MAX; ++l) + { + if (k == i && l == j) + { + continue; + } + if (!forest[k][l].visible) + { + continue; + } + pair this_tans = forest[k][l].tangential; + if (IsBetween(this_tans.second, point(0, 0), + tans.first, + tans.second) || + IsBetween(this_tans.first, point(0, 0), + tans.first, + tans.second) || + eqless(Radians(tans.first, this_tans.second), ok_gap) || + eqless(Radians(tans.second, this_tans.first), ok_gap)) + { + forest[k][l].visible = false; + } + } + } + } + } + for (int i = 0; i < MAX; ++i) + { + for (int j = 0; j < MAX; ++j) + { + if (forest[i][j].visible) + { + ++ret; + } + } + } + return ret; +} + +int Forests(double diam, double x, double y) +{ + int ret = 0; + for (int q = 0; q < 4; ++q) + { + ret += SubForest(q, diam, x, y); + } + return ret; +} + +int main() +{ + while (1) + { + double d, x, y; + cin >> d >> x >> y; + if (d == 0 && x == 0 && y == 0) + { + break; + } + cout << Forests(d, x, y) << endl; + } + return 0; +} diff --git a/1492.cpp b/1492.cpp new file mode 100644 index 0000000..02049d1 --- /dev/null +++ b/1492.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define lson (rt << 1) +#define rson (rt << 1 | 1) +#define mid ((l + r) >> 1) + +const int MAXN = 100000 + 10; +typedef long long LL; + +struct Node +{ + int cnt, width; +} Tr[MAXN << 2]; + +struct SEG +{ + int l, r, x, tag; + bool operator<(const SEG &oth) const + { + if (x == oth.x) + return tag < oth.tag; + else + return x < oth.x; + } +} seg[MAXN]; + +struct REC +{ + int x1, x2, y1, y2; +} rec[MAXN]; + +int C[MAXN], xs[MAXN][2], ys[MAXN][2]; +int N, M, W, H; + +void build(int rt, int l, int r) +{ + Tr[rt].cnt = Tr[rt].width = 0; + if (l + 1 == r) + return; + build(lson, l, mid); + build(rson, mid, r); +} + +inline void Update(int rt, int l, int r) +{ + if (Tr[rt].cnt > 0) + Tr[rt].width = C[r] - C[l]; + else if (l + 1 == r) + Tr[rt].width = 0; + else + Tr[rt].width = Tr[lson].width + Tr[rson].width; +} + +void Modify(int rt, int l, int r, int k) +{ + if (seg[k].l <= C[l] && seg[k].r >= C[r]) + { + Tr[rt].cnt += seg[k].tag; + Update(rt, l, r); + return; + } + if (seg[k].l < C[mid]) + Modify(lson, l, mid, k); + if (seg[k].r > C[mid]) + Modify(rson, mid, r, k); + Update(rt, l, r); +} + +LL calc_area(int N, REC rec[]) +{ + if (N == 0) + return 0; + for (int i = 1; i <= N; ++i) + { + C[2 * i - 1] = rec[i].y1; + C[2 * i] = rec[i].y2; + seg[2 * i - 1].l = rec[i].y1; + seg[2 * i].l = rec[i].y1; + seg[2 * i - 1].r = rec[i].y2; + seg[2 * i].r = rec[i].y2; + seg[2 * i - 1].x = rec[i].x1; + seg[2 * i].x = rec[i].x2; + seg[2 * i - 1].tag = 1; + seg[2 * i].tag = -1; + } + int cnt = 1; + N <<= 1; + sort(C + 1, C + N + 1); + sort(seg + 1, seg + N + 1); + for (int i = 2; i <= N; ++i) + { + if (C[i] != C[i - 1]) + C[++cnt] = C[i]; + } + build(1, 1, cnt); + LL ret = 0; + for (int i = 1; i <= N; ++i) + { + ret += (LL)Tr[1].width * (seg[i].x - seg[i - 1].x); + Modify(1, 1, cnt, i); + } + return ret; +} + +LL solve(int W, int H, int xs[][2], int ys[][2]) +{ + if (W - M + 1 <= 0) + return 0; + LL ret = (LL)H * (W - M + 1); + int cnt = 1; + for (int i = 1; i <= N; ++i) + { + rec[cnt].x1 = max(0, xs[i][0] - M); + rec[cnt].x2 = min(W - M + 1, xs[i][1]); + rec[cnt].y1 = ys[i][0] - 1; + rec[cnt].y2 = ys[i][1]; + if (rec[cnt].y1 > rec[cnt].y2 || rec[cnt].x1 > rec[cnt].x2) + continue; + cnt++; + } + return ret - calc_area(cnt - 1, rec); +} + +int main() +{ + while (scanf("%d%d%d%d", &W, &H, &N, &M) == 4) + { + for (int i = 1; i <= N; ++i) + scanf("%d%d%d%d", &xs[i][0], &ys[i][0], &xs[i][1], &ys[i][1]); + LL ans = solve(W, H, xs, ys); + if (M > 1) + ans += solve(H, W, ys, xs); + printf("%lld\n", ans); + } + return 0; +} diff --git a/1493.cpp b/1493.cpp new file mode 100644 index 0000000..0cb0dba --- /dev/null +++ b/1493.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define lson l, m, rt << 1 +#define rson m + 1, r, rt << 1 | 1 + +int n, t, cnt[11]; +struct Seg_tree +{ + + short col[55555 << 2]; + + inline void update(int a, int b, int c, int l, int r, int rt) + { + if (a <= l && r <= b) + { + col[rt] = c; + return; + } + int m = (l + r) >> 1; + if (col[rt]) + { + col[rt << 1] = col[rt << 1 | 1] = col[rt], col[rt] = 0; + } + if (a <= m) + { + update(a, b, c, lson); + } + if (m < b) + { + update(a, b, c, rson); + } + } + + inline void query(int l, int r, int rt) + { + if (col[rt] != 0) + { + cnt[col[rt]] += r - l + 1; + return; + } + if (l == r) + { + return; + } + int m = (l + r) >> 1; + query(lson); + query(rson); + } + +} tree[202]; +char s[11]; +inline int Abs(int x) +{ + return x >= 0 ? x : -x; +} + +int main() +{ + int i, j, k, q, x, y, h, xc, yc, r, c, l, w; + while (scanf("%d%d%d", &n, &t, &q) != EOF) + { + for (i = 0; i < n; i++) + for (j = 0; j <= (t << 2); j++) + { + tree[i].col[j] = 0; + } + memset(cnt, 0, sizeof(cnt)); + while (q--) + { + scanf("%s", s); + if (s[0] == 'R') + { + scanf("%d%d", &x, &y); + scanf("%d%d%d", &w, &h, &c); + if (w == 0 || h == 0) + { + continue; + } + for (i = x; i <= x + w - 1 && i < n; i++) + { + int r = min(y + h - 1, t - 1); + tree[i].update(y, r, c, 0, t - 1, 1); + } + } + else if (s[0] == 'D') + { + scanf("%d%d", &x, &y); + scanf("%d%d", &w, &c); + if (w == 0) + { + tree[x].update(y, y, c, 0, t - 1, 1); + continue; + } + for (i = -w; i <= w; i++) + { + if (x + i < 0 || x + i >= n) + { + continue; + } + int l = max(0, y - w + Abs(i)), r = min(t - 1, y + w - Abs(i)); + tree[i + x].update(l, r, c, 0, t - 1, 1); + } + } + else if (s[0] == 'T') + { + scanf("%d%d", &x, &y); + scanf("%d%d", &w, &c); + h = (w + 1) / 2; + for (i = 0; i < h; i++) + { + if (x + i < 0 || x + i >= n) + { + continue; + } + int l = max(0, y - h + i + 1), r = min(t - 1, y + h - i - 1); + r = min(r, t - 1); + tree[i + x].update(l, r, c, 0, t - 1, 1); + } + } + else + { + scanf("%d%d", &x, &y); + scanf("%d%d", &w, &c); + if (w == 0) + { + tree[x].update(y, y, c, 0, t - 1, 1); + continue; + } + for (i = -w; i <= w; i++) + { + if (x + i < 0 || x + i >= n) + { + continue; + } + double h = sqrt(1.0 * w * w - 1.0 * i * i); + int l = max(0, (int)ceil(y - h)); + int r = min(t - 1, (int)floor(y + h)); + tree[i + x].update(l, r, c, 0, t - 1, 1); + } + } + } + for (i = 0; i < n; i++) + { + tree[i].query(0, t - 1, 1); + } + for (i = 1; i <= 9; i++) + { + if (i != 1) + { + printf(" "); + } + printf("%d", cnt[i]); + } + puts(""); + } +} diff --git a/1494.cpp b/1494.cpp new file mode 100644 index 0000000..b1c8dfa --- /dev/null +++ b/1494.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +const int maxn = 10e5 + 1000; +struct node +{ + int u; + int v; + double cost; + bool operator<(const node &a) const + { + return cost < a.cost; + } +} a[maxn]; +struct Tree +{ + int u; + double val; + int next; +} t[maxn * 3]; +int n, m, x[1001], y[1001], val[1001], head[1001], p[1001]; +double maxcost[1001][1001]; +bool vis[1001]; +int find(int x) +{ + if (x == p[x]) + { + return x; + } + return p[x] = find(p[x]); +} +double Dis(int x1, int y1, int x2, int y2) +{ + return sqrt((x1 - x2) * (x1 - x2) * 1.0 + (y1 - y2) * (y1 - y2) * 1.0); +} +void DFS(int x, int root) +{ + vis[x] = 1; + for (int i = head[x]; i != -1; i = t[i].next) + { + if (!vis[t[i].u]) + { + maxcost[root][t[i].u] = max(t[i].val, maxcost[root][x]); + DFS(t[i].u, root); + } + } +} +void Deal() +{ + memset(head, -1, sizeof(head)); + for (int i = 0; i <= n; i++) + { + p[i] = i; + } + sort(a, a + m); + double sum = 0; + int num; + for (int i = 0; i < m; i++) + { + int ita = find(a[i].u); + int itb = find(a[i].v); + if (ita != itb) + { + p[ita] = itb; + sum += a[i].cost; + t[num].u = a[i].u; + t[num].val = a[i].cost; + t[num].next = head[a[i].v]; + head[a[i].v] = num++; + t[num].u = a[i].v; + t[num].val = a[i].cost; + t[num].next = head[a[i].u]; + head[a[i].u] = num++; + } + } + memset(maxcost, 0, sizeof(maxcost)); + for (int i = 1; i <= n; i++) + { + memset(vis, 0, sizeof(vis)); + DFS(i, i); + } + double ans = 0; + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + { + ans = max(ans, (val[i] + val[j]) / (sum - maxcost[i][j])); + } + printf("%.2lf\n", ans); +} +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + m = 0; + for (int i = 1; i <= n; i++) + { + scanf("%d%d%d", &x[i], &y[i], &val[i]); + for (int j = i - 1; j >= 1; j--) + { + a[m].u = i; + a[m].v = j; + a[m++].cost = Dis(x[i], y[i], x[j], y[j]); + } + } + Deal(); + } + return 0; +} diff --git a/1498.cpp b/1498.cpp new file mode 100644 index 0000000..0449afd --- /dev/null +++ b/1498.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +// @ http://blog.csdn.net/u011345136/article/details/39082379 + +const int maxn = 2020; +const double eps = 1e-5; + +double c[maxn], f[maxn], dp[maxn][maxn]; + +int main() +{ + int n, m, k; + double p1, p2, p3, p4; + while (scanf("%d%d%d%lf%lf%lf%lf", &n, &m, &k, &p1, &p2, &p3, &p4) != EOF) + { + if (p4 < eps) + { + printf("0.00000\n"); + continue; + } + double p = p2 / (1 - p1); + double p41 = p4 / (1 - p1); + double p31 = p3 / (1 - p1); + f[0] = 1.0; + for (int i = 1; i <= n; i++) + f[i] = p * f[i - 1]; + + dp[1][1] = p41 / (1 - p); + c[1] = p41; + for (int i = 2; i <= n; i++) + { + for (int j = 2; j <= k; j++) + c[j] = p31 * dp[i - 1][j - 1] + p41; + for (int j = k + 1; j <= i; j++) + c[j] = p31 * dp[i - 1][j - 1]; + + double tmp = c[1] * f[i - 1]; + for (int j = 2; j <= i; j++) + tmp += c[j] * f[i - j]; + + dp[i][i] = tmp / (1 - f[i]); + dp[i][1] = p * dp[i][i] + c[1]; + + for (int j = 2; j < i; j++) + dp[i][j] = p * dp[i][j - 1] + c[j]; + } + + printf("%.5lf\n", dp[n][m]); + } + return 0; +} diff --git a/150.cpp b/150.cpp new file mode 100644 index 0000000..f289da2 --- /dev/null +++ b/150.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +//平年和闰年的å„月累计天数表 +static int aMDays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; +static int aMDaysL[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; + +//æ ¹æ®æŒ‡å®šçš„历法判定闰年。bNew为true代表新历法,fase代表旧历法 +bool LeapYear(int Year, bool bNew) +{ + if (bNew) + { + //标准的闰年判断 + return ((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0); + } + return (Year % 4 == 0); +} + +//将年月日æ¢ç®—为从元年1月1æ—¥(第0天)到这一天的天数 +int Date2Days(int Year, int Month, int Date, bool bNew) +{ + --Year;//年份定为以0年起始 + //年份乘以365天,加上闰年数(总置闰天数) + //如果为新历法,è¦å‡åŽ»ç™¾å¹´ä¸­ä¸èƒ½è¢«400整除的置闰天数 + int Days = Year * 365 + Year / 4 + (bNew ? (Year / 400 - Year / 100) : 0); + //如果为闰年且月分大于2,è¦é—°1天 + Days += ((Month > 2) ? LeapYear(Year + 1, bNew) : 0); + //返回:年累计天数+月累计天数+日期-æ–°æ—§åŽ†æ³•èµ·ç‚¹å¯¹é½ + return (Days + aMDays[Month - 1] + Date - 1 - (!bNew * 2)); +} + +int main() +{ + //月分和星期的字符串表,用于处ç†è¾“入和输出 + const static string aDays[] = {"Sunday", "Monday", "Tuesday", + "Wednesday", "Thursday", "Friday", "Saturday"}; + const static string aMonths[] = {"January", "February", "March", + "April", "May", "June", "July", "August", "September", + "October", "November", "December"}; + //400年的天数,100年的天数,4年的天数和1å¹´çš„å¤©æ•°å¸¸é‡ + const int nDays400Y = 400 * 365 + 100 - 3; + const int nDays100Y = 100 * 365 + 25 - 1; + const int nDays4Y = 4 * 365 + 1, nDays1Y = 365; + //循环处ç†æ¯ä¸€ä¸ªè¾“出的日期 + for (string str; cin >> str && str != "#";) + { + int Day, Date, Month, Year; + //在字符串表中检索是周几 + Day = find(&aDays[0], &aDays[7], str) - &aDays[0]; + //读入年月日 + cin >> Date >> str >> Year; + //在字符串表中检索是几月 + Month = find(&aMonths[0], &aMonths[12], str) - &aMonths[0] + 1; + //将当å‰æ—¥æœŸæŒ‰æ—§åŽ†æ¢ç®—为天数 + int nDays = Date2Days(Year, Month, Date, false); + bool bOld2New = true;//为真表示由旧历转新历 + //判定算得的星期是å¦ä¸Žè¾“入的相符,若相符则为旧历纪年 + if (Day == (nDays + 1) % 7) + { + //转新历时è¦å…ˆå¤„ç†400å¹´å’Œ100年的置闰 + Year = nDays / nDays400Y * 400;//算出第几个400å¹´ + nDays %= nDays400Y; + //算出400年中的第几个百年 + if (nDays == nDays100Y * 4)//该400年的最åŽä¸€å¤© + { + Year += 300; + nDays -= nDays100Y * 3; + } + else + { + Year += nDays / nDays100Y * 100; + nDays %= nDays100Y;//å–零头 + } + }//å¦åˆ™ä¸ºäº²åŽ†çºªå¹´ï¼Œé‡æ–°è®¡ç®—天数,并加2ä¸Žæ—§åŽ†å¯¹é½ + else + { + nDays = Date2Days(Year, Month, Date, true) + 2; + Year = bOld2New = 0; + } + //算出百年中的第几个4å¹´ + Year += nDays / nDays4Y * 4; + nDays %= nDays4Y;//å–零头 + //算出4年中的第几年 + if (nDays == nDays1Y * 4)//å¹´4年的最åŽä¸€å¤© + { + Year += 3; + nDays -= nDays1Y * 3; + } + else + { + Year += nDays / nDays1Y; + nDays %= nDays1Y;//å–零头 + } + //判定闰年,选å–相应的å„月累计天数表 + int *pMDays = (LeapYear(++Year, bOld2New) ? aMDaysL : aMDays); + //按当年累计天数查找月份 + for (Month = 0; Month < 12 && pMDays[Month] <= nDays; ++Month) + ; + //计算当月日期 + Date = nDays - pMDays[Month - 1] + 1; + //计算星期几 + Day = (Date2Days(Year, Month, Date, bOld2New) + 1) % 7; + //按格å¼è¦æ±‚输出结果 + cout << aDays[Day] << ' ' << Date << (bOld2New ? " " : "* "); + cout << aMonths[Month - 1] << ' ' << Year << endl; + } + return 0; +} diff --git a/1501.cpp b/1501.cpp new file mode 100644 index 0000000..0bc4cb4 --- /dev/null +++ b/1501.cpp @@ -0,0 +1,285 @@ +#include + +using namespace std; + +int dp[10][1 << 8][1 << 8]; +vector getPlace(int O, int X, int W) +{ + vector ret; + for (int i = (1 << W) - 1; i >= 0; i--) + { + if ((i & O) != O || (i & X) != 0) + continue; + ret.push_back(i); + } + return ret; +} +int checkIntersect(int up, int down, int W) +{ + // xo ox + // ox and xo are Intersect. + for (int i = 1; i < W; i++) + { + int LL, LR, RL, RR; + LL = (up >> (i - 1)) & 1; + LR = (up >> (i)) & 1; + RL = (down >> (i - 1)) & 1; + RR = (down >> (i)) & 1; + if (LL + LR + RL + RR != 2) + continue; + if (LL == RR || LR == RL) + return 1; + } + return 0; +} +struct DisjointSet +{ + int parent[20]; + void init(int n) + { + for (int i = 0; i < n; i++) + parent[i] = i; + } + int findp(int x) + { + return parent[x] == x ? x : parent[x] = findp(parent[x]); + } + int joint(int x, int y) + { + x = findp(x), y = findp(y); + if (x == y) + return 0; + parent[x] = y; + return 1; + } +}; +void solve(int H, int W, char g[][10]) +{ + int last_row = 0; + int rowO[10] = {}, rowX[10] = {}; + int i, j, k, p, q, r; + int prev; + for (i = 0; i < H; i++) + { + for (j = 0; j < W; j++) + { + if (g[i][j] == 'o') + last_row = i; + if (g[i][j] == 'o') + rowO[i] |= 1 << j; + if (g[i][j] == 'x') + rowX[i] |= 1 << j; + } + } + memset(dp, 0x7f, sizeof(dp)); +#define oo 0x7f7f7f7f + dp[0][0][0] = 0; + for (i = 0; i < H; i++) + { + vector place = getPlace(rowO[i], rowX[i], W); + for (j = 1 << W; j >= 0; j--) + { + for (k = 0; k < 256; k++) + { + if (dp[i][j][k] == oo) + continue; + int up = j, down; + int upC[4] = {k & 3, (k >> 2) & 3, (k >> 4) & 3, (k >> 6) & 3}, upCnt = 0; + int upA[8] = {}, downA[8] = {}; + for (p = 0, prev = 0; p < 4; p++) + { + int cover = upC[p]; + while (prev < W && ((up >> prev) & 1) == 0) + prev++; + if (prev < W) + upCnt++; + while (prev < W && ((up >> prev) & 1) == 1) + upA[prev] = cover, prev++; + } + for (vector::iterator it = place.begin(); + it != place.end(); it++) + { + down = *it; + if (checkIntersect(up, down, W)) + continue; + // build new component assign + int mapped_upC[4] = {-1, -1, -1, -1}; + DisjointSet dset;// component[0~3], posY[4~3+W] + dset.init(4 + W); + int cnt_upC[4] = {}; + for (p = 0, prev = -2; p < W; p++) + { + if ((down >> p) & 1) + { + if (prev == p - 1) + dset.joint(4 + prev, 4 + p); + prev = p; + } + } + for (p = 0; p < W; p++) + { + if (((down >> p) & 1) && ((up >> p) & 1)) + { + int C = upA[p]; + cnt_upC[C]++; + dset.joint(C, 4 + p); + } + } + int err = 0; + for (p = 0; p < W; p++) + { + if ((up >> p) & 1) + { + int C = upA[p]; + if (cnt_upC[C] == 0) + err = 1; + } + } + if (err) + continue;// lost one kind of component. + int downKind = 0, downCnt = 0; + int mapped_cc[12]; + int downC = 0; + for (p = 0; p < W + 4; p++) + mapped_cc[p] = -1; + for (p = 0, prev = -2; p < W; p++) + { + if ((down >> p) & 1) + { + if (prev != p - 1) + { + int parent = dset.findp(p + 4); + if (mapped_cc[parent] == -1) + mapped_cc[parent] = downKind++; + downC |= mapped_cc[parent] << (2 * downCnt); + downCnt++; + } + prev = p; + } + } + for (p = 0; p < W;) + {// check don't have a hole. + while (p < W && ((down >> p) & 1) == 0) + p++; + if (p >= W) + break; + int kind[4] = {}; + while (p < W && ((down >> p) & 1)) + { + if ((up >> p) & 1) + { + kind[upA[p]]++; + if (kind[upA[p]] > 1) + { + err = 1; + } + while (p < W && ((down >> p) & 1) && ((up >> p) & 1)) + p++; + } + else + p++; + } + } + if (err) + { + /*for(p = 0; p < W; p++) + printf("%d", (up>>p)&1); + puts("--- up"); + for(p = 0; p < W; p++) { + if(((up>>p)&1) == 0) { + printf("n"); + continue; + } + printf("%d", upA[p]); + } + puts("--- upkind"); + for(p = 0; p < W; p++) + printf("%d", (down>>p)&1); + puts("--- down"); + for(p = 0; p < W; p++) { + if(((down>>p)&1) == 0) { + printf("n"); + continue; + } + int parent = dset.findp(p+4); + printf("%d", mapped_cc[parent]); + } + puts("--- downkind"); + puts("");*/ + continue;// have a hole. + } + if (downKind > 4 || downCnt > 4) + { + puts("error !!!"); + } + int diff = 0; + for (p = 0; p < W; p++) + { + if (((down >> p) & 1) == 0) + continue; + int cost = 4; + if (p - 1 >= 0 && ((down >> (p - 1)) & 1)) + cost -= 2; + if ((up >> p) & 1) + cost -= 2; + diff += cost; + } + dp[i + 1][down][downC] = min(dp[i + 1][down][downC], dp[i][up][k] + diff); + } + } + } + } + int ret = oo; + for (i = last_row + 1; i <= H; i++) + { + for (j = (1 << W) - 1; j >= 0; j--) + ret = min(ret, dp[i][j][0]); + } + if (ret == oo) + ret = -1; + printf("%d\n", ret); +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + int testcase, cases = 0; + int H, W; + int i, j, k; + char g[10][10]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &H, &W); + for (i = 0; i < H; i++) + scanf("%s", g[i]); + printf("Case #%d: ", ++cases); + solve(H, W, g); + } + return 0; +} +/* +10 +5 5 +..... +..o.. +..x.. +..o.. +..... + +3 +3 3 +.o. +.x. +o.o +4 4 +.... +.ox. +.xo. +.... +5 5 +.ooo. +.x... +..xoo +x.xoo +.ox.x +*/ diff --git a/1502.cpp b/1502.cpp new file mode 100644 index 0000000..d1ad31f --- /dev/null +++ b/1502.cpp @@ -0,0 +1,344 @@ +#include + +using namespace std; + +#define For(i, a) for ((i) = 0; i < (a); ++(i)) +#define Rep(i, v) for ((i) = (v).begin(); (v).end() != (i); ++i) +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#define sz(a) (sizeof(a)) +#define MEM(a) (memset((a), 0, sizeof(a))) +#define MEME(a) (memset((a), -1, sizeof(a))) +#define MEMX(a) (memset((a), 0x7f, sizeof(a))) +#define pb(a) push_back(a) + +typedef long long ll; +typedef unsigned long long ull; +typedef unsigned int uint; +typedef unsigned char uchar; + +template +inline void checkmin(T &a, T b) +{ + if (a > b) + { + a = b; + } +} +template +inline void checkmax(T &a, T b) +{ + if (a < b) + { + a = b; + } +} + +const int oo = 1 << 30; +const double eps = 1e-7; +const int N = 330000; +const int M = 20010; +const ll P = 10000000097ll; + +//char s[N]; /// 长度+1,对于éžå­—符串,加一个å°äºŽæœ€å°å€¼çš„元素, +int sa[N];/// å€å¢žç®—法,结果 下标 1-n,第 i 大的是 sa[i] +int rk[N];/// 第 i ä½å¼€å§‹çš„åŽç¼€ï¼Œçš„排å为 rk[i] +int wa[N], wb[N], wv[N], rmq[20][N]; +int to[N]; + +bool cmp(int *y, int a, int b, int L) +{ + return y[a] == y[b] && y[a + L] == y[b + L]; +} + +void da_(char *s, int *sa, int len, int dn) +{ + int i, j, p; + int *x, *y, *t; + x = wa; + y = wb; + For(i, dn) rk[i] = 0; + For(i, len) rk[x[i] = s[i]]++; + For(i, dn) rk[i + 1] += rk[i]; + for (i = len - 1; i >= 0; i--) + { + sa[--rk[x[i]]] = i; + } + for (j = 1, p = 1; p < len; j *= 2, dn = p) + { + For(p, j) y[p] = len - j + p; + For(i, len) if (sa[i] >= j) + { + y[p++] = sa[i] - j; + } + For(i, len) wv[i] = x[y[i]]; + For(i, dn) rk[i] = 0; + For(i, len) rk[wv[i]]++; + For(i, dn) rk[i + 1] += rk[i]; + for (i = len - 1; i >= 0; i--) + { + sa[--rk[wv[i]]] = y[i]; + } + swap(x, y); + x[sa[0]] = 0; + for (p = i = 1; i < len; i++) + { + p += !cmp(y, sa[i], sa[i - 1], j); + x[sa[i]] = p - 1; + } + } +} + +void da(char *s, int *sa, int len, int dn) +{ + int i, j, p; + int *x, *y, *t; + x = wa; + y = wb; + ///For(i, len) x[i]=s[i],sa[i]=i; + ///sort(sa, sa+len, sacmp); + /// 注æ„此时也应传入正确dn。 + For(i, dn) rk[i] = 0; + For(i, len) rk[x[i] = s[i]]++; + For(i, dn) rk[i + 1] += rk[i]; + for (i = len - 1; i >= 0; i--) + { + sa[--rk[x[i]]] = i; + } + for (j = 1, p = 1; p < len; j *= 2, dn = p) + { + For(p, j) y[p] = len - j + p; + For(i, len) if (sa[i] >= j) + { + y[p++] = sa[i] - j; + } + For(i, len) wv[i] = x[y[i]]; + For(i, dn) rk[i] = 0; + For(i, len) rk[wv[i]]++; + For(i, dn) rk[i + 1] += rk[i]; + for (i = len - 1; i >= 0; i--) + { + sa[--rk[wv[i]]] = y[i]; + } + swap(x, y); + x[sa[0]] = 0; + for (p = 1, i = 1; i < len; i++)/// æ³¨æ„ (p-1):(p++) + { + x[sa[i]] = (cmp(y, sa[i], sa[i - 1], j) ? (p - 1) : (p++)); + } + dn = p; + } +} + +void find_height(char *s, int *sa, int len) +{ + int *h = rmq[0];/// sa中为 i 的点,与 i-1 的最长公共å‰ç¼€ + int i, j, k = 0; + for (i = 1; i <= len; i++) + { + rk[sa[i]] = i; + } + For(i, len) + { + if (k > 0) + { + k--; + } + j = sa[rk[i] - 1]; + while (s[i + k] == s[j + k]) + { + k++; + } + h[rk[i]] = k; + } +} + +void RMQ(int n) +{ + int i, j, k; + int rn = floor(log(n * 2.0) / log(2.0)); + for (i = 1; i < rn; i++) + For(j, n + 2 - (1 << (i - 1))) + rmq[i][j] = MIN(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]); +} + +int askRMQ(int a, int b)/// [a,b]闭区间 +{ + ///int rq = floor( log(b-a+0.0) / log(2.0) ); + if (a == b) + { + return 1 << 20; + } + a++; + int rq = to[b - a]; + return MIN(rmq[rq][a], rmq[rq][b + 1 - (1 << rq)]); +} + +void pre_log() +{ + int i; + to[0] = to[1] = 0; + for (i = 1; i * 2 < N; i++) + { + to[i * 2] = to[i * 2 + 1] = to[i] + 1; + } +} + +char str[N]; +int sn; +int stn[M]; +int val[M]; +int tree[N * 4]; +int n; + +void suffix() +{ + da(str, sa, sn + 1, 128); + find_height(str, sa, sn); + RMQ(sn); +} + +void find_lr(int id, int len, int &l, int &r) +{ + int low, high, mid; + low = 0; + high = id; + while (low < high) + { + mid = (low + high) / 2; + if (askRMQ(mid, id) < len) + { + low = mid + 1; + } + else + { + high = mid; + } + } + l = high; + low = id; + high = sn; + while (low < high) + { + mid = (low + high + 1) / 2; + if (askRMQ(id, mid) < len) + { + high = mid - 1; + } + else + { + low = mid; + } + } + r = low; +} + +int find_mx(int tk, int tl, int tr, int l, int r) +{ + if (l <= tl && tr <= r) + { + return tree[tk]; + } + int mid = (tl + tr) / 2; + int mx = 0; + if (l <= mid) + { + checkmax(mx, find_mx(tk * 2, tl, mid, l, r)); + } + if (r > mid) + { + checkmax(mx, find_mx(tk * 2 + 1, mid + 1, tr, l, r)); + } + return mx; +} + +void add_mx(int tk, int tl, int tr, int id, int val) +{ + checkmax(tree[tk], val); + if (tl == tr) + { + return; + } + int mid = (tl + tr) / 2; + if (id <= mid) + { + add_mx(tk * 2, tl, mid, id, val); + } + else + { + add_mx(tk * 2 + 1, mid + 1, tr, id, val); + } +} + +int work() +{ + int ret = 0; + int start; + start = sn; + memset(tree, 0, sizeof(int) * 4 * N); + for (int i = n - 1; i >= 0; i--) + { + start -= stn[i] + 1; + int l, r; + find_lr(rk[start], stn[i], l, r); + int get = find_mx(1, 1, sn, l, r) + val[i]; + for (int j = 0; j < stn[i]; j++) + { + add_mx(1, 1, sn, rk[start + j], get); + } + checkmax(ret, get); + } + return ret; +} + +int doit() +{ + sn = 0; + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%s %d", str + sn, &val[i]); + checkmax(val[i], 0); + stn[i] = strlen(str + sn); + sn += stn[i]; + str[sn++] = '$'; + } + str[sn] = 0; + suffix(); + return work(); +} + +int main() +{ + pre_log(); + int T; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + int ans = doit(); + printf("Case #%d: %d\n", i, ans); + } + return 0; +} + +/** + +Sample Input +2 +5 +a 1 +ab 2 +abb 3 +baba 5 +abbab 8 +5 +abbab 8 +baba 5 +abb 3 +ab 2 +a 1 + +Sample Output +Case #1: 14 + +**/ diff --git a/1508.cpp b/1508.cpp new file mode 100644 index 0000000..475e800 --- /dev/null +++ b/1508.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define xx first +#define yy second +#define pb push_back +#define mp make_pair +#define LL long long +#define inf INT_MAX / 2 +#define mod 1000000007 +#define PI 2.0 * acos(0.0) +#define vsort(v) sort(v.begin(), v.end()) + +int ar[10001][5]; +int pp[51], ad[5], sum[51]; +int get(int bit, int nd) +{ + //cout< 0) + { + e += ar[x][z]; + } + pp[y] = max(pp[y], e); + } + } + if (c > 1 && c < 5) + { + cout << get((1 << 5) - 1, c) << endl; + } + else if (c == 1) + { + d = 0; + for (x = 0; x < b; x++) + { + e = 0; + for (y = 0; y < 5; y++) + { + e += ar[x][y]; + } + d = max(d, e); + } + printf("%d\n", d); + } + else + { + d = 0; + for (x = 0; x < 5; x++) + { + d += ad[x]; + } + printf("%d\n", d); + } + } + return 0; +} +/* +2 +4 2 +30 30 30 30 0 +50 0 0 0 0 +0 50 0 50 10 +0 0 50 0 20 +5 1 +10 20 60 0 0 +0 0 20 50 30 +30 50 20 20 0 +10 10 10 20 30 +30 0 20 10 20 +*/ diff --git a/1509.cpp b/1509.cpp new file mode 100644 index 0000000..755f03d --- /dev/null +++ b/1509.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +const int maxn = 100; +char str[maxn], mapStr[maxn]; +map mp; +int k, len1, len2; + +int dfs(int pos1, int pos2) +{ + if (pos1 == len1 && pos2 == len2) + { + return 1; + } + if (pos1 == len1 && pos2 != len2) + { + return 0; + } + if (mp[str[pos1]])//如果已ç»æœ‰å€¼ + { + char *tmp = mp[str[pos1]]; + int len = strlen(tmp); + int tmpPos = pos2; + for (int i = 0; i < len; i++) + { + if (tmp[i] != mapStr[tmpPos]) + { + return 0; + } + tmpPos++; + } + if (dfs(pos1 + 1, tmpPos)) + { + return 1; + } + } + else//还没有值 + { + char tmp[maxn]; + for (int i = 1; i <= k; i++) + { + memset(tmp, '\0', sizeof(tmp)); + int pos = 0; + for (int j = pos2; j < pos2 + i; j++) + { + tmp[pos++] = mapStr[j]; + } + mp[str[pos1]] = tmp; + if (dfs(pos1 + 1, pos2 + i)) + { + return 1; + } + mp[str[pos1]] = 0; + } + } + return 0; +} + +int main() +{ + int Case; + scanf("%d", &Case); + while (Case--) + { + scanf("%d%*c", &k); + gets(str); + gets(mapStr); + len1 = strlen(str); + len2 = strlen(mapStr); + mp.clear(); + printf("%d\n", dfs(0, 0)); + } + return 0; +} diff --git a/151.cpp b/151.cpp index 46f131e..c36d428 100644 --- a/151.cpp +++ b/151.cpp @@ -1,39 +1,65 @@ -#include -#include -using namespace std; - -int N; - -int simulation (int m){ - list l; - int i; - for (i = 1; i <= N; i++) - l.push_back (i); +#include - while ( l.size () > 1 ) { - l.pop_front (); - for (i = 1; i < m; i++) { - l.push_back ( l.front () ); - l.pop_front (); - } - } - return l.front (); -} +using namespace std; -int main (){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (scanf ("%d", &N)) { - if (N == 0) break; - int m = 1; - int region = 0; - while (true) { - region = simulation (m); - if (region == 13) break; - m++; - } - printf("%d\n", m); - } - return 0; +int main() +{ + int n, current; + deque regions; + while (cin >> n) + { + if (n == 0) + { + break; + } + int m = 0; + while (++m) + { + regions.clear(); + for (int i = 1; i <= n; i++) + { + regions.push_back(i); + } + regions.pop_front(); + regions.push_back(0); + int count = 1; + int taken = 0; + int c = 0; + while (count < n) + { + if (regions.front() > 0) + { + c++; + taken = regions.front(); + regions.pop_front(); + if (c == m) + { + c = 0; + count++; + if (count < n && taken == 13) + { + taken = 0; + break; + } + regions.push_back(0); + } + else + { + regions.push_back(taken); + } + } + else + { + regions.pop_front(); + regions.push_back(0); + } + } + if (taken == 13) + { + break; + } + } + cout << m << endl; + } + return 0; } - diff --git a/1510.cpp b/1510.cpp new file mode 100644 index 0000000..aec6c08 --- /dev/null +++ b/1510.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int n, ans; + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + int p[1001] = {0}; + ans = n * (n - 1) * (n - 2) / 6; + for (int i = n; i > 1; i--) + for (int j = 1; j < i; j++) + { + int t; + scanf("%d", &t); + if (t) + { + p[n - i + 1]++; + p[n - i + j + 1]++; + } + } + for (int i = 1; i <= n; i++) + { + ans -= (n - p[i] - 1) * p[i] / 2; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1511.cpp b/1511.cpp new file mode 100644 index 0000000..c8d3c2e --- /dev/null +++ b/1511.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +const int INF = 0x3f3f3f3f; +const int N = 200005; + +int t, n, m, a1, a2, a3, a4; + +struct Point +{ + int x, y, flag; +} p[N]; + +bool cmp(Point a, Point b) +{ + return a.y < b.y; +} + +int solve() +{ + int ans = INF; + sort(p, p + n + m, cmp); + int tmp = INF; + for (int i = 0; i < n + m; i++) + { + if (p[i].flag) + { + tmp = min(tmp, p[i].x - p[i].y); + } + else + { + ans = min(ans, p[i].y - p[i].x + tmp); + } + } + tmp = INF; + reverse(p, p + n + m); + for (int i = 0; i < n + m; i++) + { + if (p[i].flag) + { + tmp = min(tmp, p[i].x + p[i].y); + } + else + { + ans = min(ans, -p[i].x - p[i].y + tmp); + } + } + return ans; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d%d", &p[i].x, &p[i].y); + p[i].flag = 0; + } + scanf("%d", &m); + for (int i = n; i < n + m; i++) + { + scanf("%d%d", &p[i].x, &p[i].y); + p[i].flag = 1; + } + printf("%d\n", solve()); + } + return 0; +} diff --git a/1513.cpp b/1513.cpp new file mode 100644 index 0000000..3e33681 --- /dev/null +++ b/1513.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int c[300005]; +int a[100005]; +int ans[100005]; +int n, m; + +int lowbit(int x) +{ + return x & (-x); +} + +int sum(int m) +{ + int sum = 0; + while (m > 0) + { + sum += c[m]; + m -= lowbit(m); + } + return sum; +} + +int insert(int i, int num) +{ + while (i <= n + 100000) + { + c[i] += num; + i += lowbit(i); + } +} + +int main() +{ + int T, num; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + memset(a, 0, sizeof(a)); + memset(c, 0, sizeof(c)); + for (int i = 1; i <= n; i++) + { + a[i] = i + 100000; + } + for (int i = 2; i <= n; i++) + { + insert(i + 100000, 1); + } + for (int i = 1; i <= m; i++) + { + scanf("%d", &num); + ans[i] = sum(a[num]); + insert(a[num], -1); + insert(100000 - i + 2, 1); + a[num] = 100000 - i + 1; + } + for (int i = 1; i <= m; i++) + if (i != m) + { + printf("%d ", ans[i]); + } + else + { + printf("%d\n", ans[i]); + } + } + return 0; +} diff --git a/1514.cpp b/1514.cpp new file mode 100644 index 0000000..af595eb --- /dev/null +++ b/1514.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +struct node +{ + int v, pre; +} edge[1000000]; +int pos[300000], nEdge; +int W[600][600], B[600][600]; +void connect(int u, int v) +{ + edge[nEdge].pre = pos[u]; + edge[nEdge].v = v; + pos[u] = nEdge++; +} +int vis[300000]; +int link1[300000]; +char str[600][600]; +int m, n; +int shuang; +inline bool find(int k) +{ + int i; + for (int p = pos[k]; p != -1; p = edge[p].pre) + { + int v = edge[p].v; + if (vis[v] == shuang) + continue; + vis[v] = shuang; + if (link1[v] == -1 || find(link1[v])) + { + link1[v] = k; + return 1; + } + } + return 0; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + nEdge = 0; + scanf("%d%d", &n, &m); + int num = 0, flag = 0; + for (int i = 0; i < n; i++) + scanf("%s", str[i]); + + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (str[i][j] == 'B') + B[i][j] = ++num; + else if (str[i][j] == 'W') + W[i][j] = ++flag; + + if (num * 2 != flag) + { + puts("NO"); + continue; + } + + memset(link1, -1, sizeof(link1)); + memset(pos, -1, sizeof(pos)); + memset(vis, -1, sizeof(vis)); + + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + if (str[i][j] == 'B') + { + if (str[i + 1][j] == 'W') + connect(B[i][j], W[i + 1][j]); + + if (str[i - 1][j] == 'W') + connect(B[i][j], W[i - 1][j]); + + if (str[i][j + 1] == 'W') + connect(B[i][j] + num, W[i][j + 1]); + + if (str[i][j - 1] == 'W') + connect(B[i][j] + num, W[i][j - 1]); + } + int ans = 0; + shuang = 0; + for (int i = 1; i <= num * 2; i++) + { + shuang++; + if (find(i)) + ans++; + else + break; + } + if (ans == 2 * num) + puts("YES"); + else + puts("NO"); + } + return 0; +} diff --git a/1515.cpp b/1515.cpp new file mode 100644 index 0000000..5c0d55d --- /dev/null +++ b/1515.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define LL_INF 0x3fffffffffffffffll +#define EPS 1e-12 +#define N 10010 +#define E 100010 + +typedef long long LL; +typedef unsigned long long ULL; +typedef double DB; +struct Edge +{ + int en, next; + double cap, flow; +} edge[E]; +int head[N], tot, now[N]; +int source, sink, tot_num; +int pre[N], gap[N], dis[N]; +void add_edge(int st, int en, double cap) +{ + edge[tot].en = en; + edge[tot].cap = cap; + edge[tot].flow = 0; + edge[tot].next = head[st]; + head[st] = tot++; + + edge[tot].en = st; + edge[tot].cap = 0; + edge[tot].flow = 0; + edge[tot].next = head[en]; + head[en] = tot++; +} + +void augment(double flow) +{ + for (int i = source; i != sink; i = edge[now[i]].en) + { + edge[now[i]].flow += flow; + edge[now[i] ^ 1].flow -= flow; + } +} +double sap() +{ + memset(dis, 0, sizeof(dis)); + memset(gap, 0, sizeof(gap)); + memset(pre, -1, sizeof(pre)); + for (int i = 0; i < tot_num; i++) + now[i] = head[i]; + gap[0] = tot_num; + int point = source; + double flow = 0; + double min_flow = LL_INF; + while (dis[source] < tot_num) + { + bool fg = false; + for (int i = now[point]; i != -1; i = edge[i].next) + if (edge[i].cap - edge[i].flow > 0 && dis[point] == dis[edge[i].en] + 1) + { + min_flow = min(min_flow, edge[i].cap - edge[i].flow); + now[point] = i; + pre[edge[i].en] = point; + point = edge[i].en; + if (point == sink) + { + flow += min_flow; + augment(min_flow); + point = source; + min_flow = LL_INF; + } + fg = true; + break; + } + if (fg) + continue; + if (--gap[dis[point]] == 0) + break; + int Min = tot_num; + for (int i = head[point]; i != -1; i = edge[i].next) + if (edge[i].cap - edge[i].flow > 0 && Min > dis[edge[i].en]) + { + Min = dis[edge[i].en]; + now[point] = i; + } + gap[dis[point] = Min + 1]++; + if (point != source) + point = pre[point]; + } + return flow; +} +int n, m; +int val[N]; +int a[E], b[E]; +LL c[E]; +bool pd; +int map[200][200]; +int u, ans[10010]; +bool vis[200]; +void dfs(int now) +{ + vis[now] = true; + for (int p = head[now]; p != -1; p = edge[p].next) + if (!vis[edge[p].en] && edge[p].flow < edge[p].cap) + dfs(edge[p].en); +} +char str[10000]; +double A, B, C; +void build() +{ + memset(head, -1, sizeof(head)); + tot = 0; + source = n * m, sink = n * m + 1, tot_num = 2 + n * m; + swap(n, m); + for (int i = 0; i < n; i++) + { + scanf("%s", str); + for (int j = 0; j < m; j++) + { + if (i != 0 && j != 0 && i != n - 1 && j != m - 1) + { + if (str[j] == '#') + add_edge(source, i * m + j, A); + else + add_edge(i * m + j, sink, B); + } + else + { + add_edge(source, i * m + j, LL_INF); + if (str[j] == '.') + add_edge(i * m + j, sink, B); + } + if (i) + { + add_edge((i - 1) * m + j, i * m + j, C); + add_edge(i * m + j, (i - 1) * m + j, C); + } + if (j) + { + add_edge(i * m + j, i * m + j - 1, C); + add_edge(i * m + j - 1, i * m + j, C); + } + } + } +} +int T; +int main() +{ + //freopen("input.txt","r",stdin); + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + scanf("%lf%lf%lf", &A, &B, &C); + build(); + printf("%.0f\n", sap()); + } +} diff --git a/1516.cpp b/1516.cpp new file mode 100644 index 0000000..4d8b461 --- /dev/null +++ b/1516.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define PB push_back +#define MP make_pair +#define INF 0x5fffffff +#define rep(i, x, y) for (i = x; i <= y; i++) +#define reps(i, x, y) for (i = x; i >= y; i--) +#define sqr(x) ((x) * (x)) +#define eps 1e-9 +#define N 110 + +typedef long long LL; +typedef double DB; +typedef vector VI; +vector> G[N]; +VI g[N]; +map M; +pair p[N]; +int In[N]; +bool mark[N]; +int cou[N]; +DB d[N]; +bool v[N]; +int n, m; +void ins(int x, int y, DB val) +{ + G[x].PB(MP(y, val)); + // In[y]++; + mark[x] = 1; + mark[y] = 1; +} +DB get_dis(int i, int j) +{ + return sqrt(sqr(p[i].first - p[j].first) + sqr(p[i].second - p[j].second)); +} +bool spfa(int src) +{ + int i, j, k; + queue Q; + rep(i, 1, n) d[i] = 100000000000000.00; + d[src] = 0; + memset(v, 0, sizeof(v)); + v[src] = 1; + memset(cou, 0, sizeof(cou)); + // rep(i,1,n)Q.push(i); + Q.push(src); + while (!Q.empty()) + { + int x = Q.front(); + Q.pop(); + for (k = 0; k < G[x].size(); k++) + { + int y = G[x][k].first; + if (d[y] > d[x] + G[x][k].second) + { + d[y] = d[x] + G[x][k].second; + if (!v[y]) + { + cou[y]++; + if (cou[y] > n) + { + return 0; + } + v[y] = 1; + Q.push(y); + } + } + } + v[x] = 0; + } + rep(i, 1, n) + { + if (d[i] < -eps) + { + g[i].PB(src); + In[src]++; + } + } + return 1; +} +char st[1000]; +string s[N]; +void solve() +{ + int i, j, k; + map M; + scanf("%d%d", &n, &m); + rep(i, 1, n) + { + scanf("%s", st); + M[st] = i; + s[i] = st; + G[i].clear(); + g[i].clear(); + In[i] = 0; + mark[i] = 0; + scanf("%lf%lf", &p[i].first, &p[i].second); + } + while (m--) + { + scanf("%s", st); + i = M[st]; + scanf("%*s%s", st); + j = M[st]; + scanf("%*s%*s%s", st); + k = M[st]; + double d1 = get_dis(i, j); + double d2 = get_dis(i, k); + ins(k, j, d2 - d1 - eps); + } + rep(i, 1, n) if (!spfa(i)) + { + puts("IMPOSSIBLE"); + return; + } + VI ans; + queue Q; + rep(i, 1, n) if (!In[i] && mark[i]) + { + Q.push(i); + } + while (!Q.empty()) + { + if (Q.size() > 1) + { + puts("UNKNOWN"); + return; + } + int x = Q.front(); + Q.pop(); + ans.PB(x); + for (k = 0; k < g[x].size(); k++) + { + int y = g[x][k]; + In[y]--; + if (!In[y]) + { + Q.push(y); + } + } + } + printf("%s", s[ans[0]].c_str()); + for (i = 1; i < ans.size(); i++) + { + printf(" %s", s[ans[i]].c_str()); + } + puts(""); +} +int main() +{ +#ifndef ONLINE_JUDGE + freopen("in.txt", "r", stdin); +//freopen("out.txt","w",stdout); +#endif + int Case; + scanf("%d", &Case); + // init(); + while (Case--) + { + solve(); + } + return 0; +} diff --git a/1517.cpp b/1517.cpp new file mode 100644 index 0000000..d371ee0 --- /dev/null +++ b/1517.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +typedef long long LL; +typedef pair PII; + +const double eps = 1e-6; +const double offset = 2000; +const double PI = acos(-1.0); +const int MAXN = 100005; +const int MAXM = 1 << 20; +const int INF = 0x3f3f3f3f; +const int MOD = 1000000009; + +bool zero(double a) +{ + return fabs(a) < eps; +} +int sgn(double x) +{ + return x < -eps ? -1 : x > eps; +} + +struct point +{ + int x, y; + point() {} + point(int _x, int _y) : x(_x), y(_y) {} + point operator-(point o) const + { + return point(x - o.x, y - o.y); + } + point operator+(point o) const + { + return point(x + o.x, y + o.y); + } + int operator^(point r) + { + return x * r.y - y * r.x; + } + bool operator<(const point &r) const + { + return x == r.x ? y < r.y : x < r.x; + } + void input() + { + scanf("%d %d", &x, &y); + } +}; + +bool intersectIn(point a, point b, point c, point d) +{ + if (min(a.x, b.x) > max(c.x, d.x) || + min(a.y, b.y) > max(c.y, d.y) || + min(c.x, d.x) > max(a.x, b.x) || + min(c.y, d.y) > max(a.y, b.y)) + { + return false; + } + LL h, i, j, k; + h = (b - a) ^ (c - a); + i = (b - a) ^ (d - a); + j = (d - c) ^ (a - c); + k = (d - c) ^ (b - c); + return h * i <= 0 && j * k <= 0; +}//判线段相交,包括端点和部分é‡åˆ + +int s, r, w, p; +set se; +vector> line; + +bool check(const point &a, int x, int y) +{ + if (x * x + y * y > r * r) + { + return false; + } + point temp = a + point(x, y); + if (se.find(temp) == se.end()) + { + return false; + } + int cnt = 0; + for (int i = 0; i < line.size(); i++) + if (intersectIn(a, temp, line[i].first, line[i].second)) + { + cnt++; + } + if (cnt > r) + { + return false; + } + if (x * x + y * y <= (r - cnt) * (r - cnt)) + { + return true; + } + return false; +} + +void solved(int cas) +{ + se.clear(); + line.clear(); + scanf("%d %d %d %d", &s, &r, &w, &p); + point p1, p2; + while (s--) + { + p1.input(); + se.insert(p1); + } + while (w--) + { + p1.input(), p2.input(); + line.push_back(make_pair(p1, p2)); + } + while (p--) + { + p1.input(); + vector vec; + for (int i = -r; i <= r; i++) + for (int j = -r; j <= r; j++) + if (check(p1, i, j)) + { + vec.push_back(p1 + point(i, j)); + } + sort(vec.begin(), vec.end()); + printf("%d", vec.size()); + for (int i = 0; i < vec.size(); i++) + { + printf(" (%d,%d)", vec[i].x, vec[i].y); + } + printf("\n"); + } +} + +int main() +{ + int T = 1; + scanf("%d", &T); + for (int i = 1; i <= T; i++) + { + solved(i); + } + return 0; +} diff --git a/1518.cpp b/1518.cpp new file mode 100644 index 0000000..13daa14 --- /dev/null +++ b/1518.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +const double infty = 1e100; +const double eps = 1e-10; + +int N; +map city_ids; + +int city_id(string s) +{ + if (!city_ids.count(s)) + { + city_ids[s] = N++; + } + return city_ids[s]; +} + +int main() +{ + int runs; + cin >> runs; + while (runs--) + { + N = 0; + city_ids.clear(); + string s; + cin >> s; + int start = city_id(s); + cin >> s; + int finish = city_id(s); + int M; + cin >> M; + vector fr(M), to(M), t(M), dt(M), p(M), d(M); + for (int i = 0; i < M; i++) + { + cin >> s; + fr[i] = city_id(s); + cin >> s; + to[i] = city_id(s); + cin >> t[i] >> dt[i] >> p[i] >> d[i]; + } + vector> best(N, vector(60, infty)); + for (int i = 0; i < 60; i++) + { + best[finish][i] = 0; + } + for (bool changed = true; changed;) + { + changed = false; + for (int i = 0; i < M; i++) + { + double time = (1 - p[i] / 100.0) * (dt[i] + best[to[i]][(t[i] + dt[i]) % 60]); + for (int j = 1; j <= d[i]; j++) + { + time += p[i] / 100.0 / d[i] * (dt[i] + j + best[to[i]][(t[i] + dt[i] + j) % 60]); + } + if (time < eps * infty && time < best[fr[i]][t[i]] - eps) + { + best[fr[i]][t[i]] = time; + changed = true; + for (int j = 0; j < 60; j++) + if (best[fr[i]][t[i]] + (60 + t[i] - j) % 60 < best[fr[i]][j] - eps) + { + best[fr[i]][j] = best[fr[i]][t[i]] + (60 + t[i] - j) % 60; + } + } + } + } + double res = infty; + for (int i = 0; i < 60; i++) + { + res = min(res, best[start][i]); + } + if (res == infty) + { + cout << "IMPOSSIBLE" << endl; + } + else + { + cout << setprecision(10) << res << endl; + } + } + return 0; +} diff --git a/1519.cpp b/1519.cpp new file mode 100644 index 0000000..84ae17d --- /dev/null +++ b/1519.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +#define MAXCHAR 26 +#define MAXS (1024) +#define MAXNODE (1048576) +#pragma comment(linker, "/STACK:1024000000,1024000000") + +class Trie +{ +public: + struct Node + { + Node *next[MAXCHAR]; + int cnt; + void init() + { + cnt = 0; + memset(next, 0, sizeof(next)); + } + } nodes[MAXNODE]; + Node *root; + int size, cases; + int chCnt[MAXCHAR];// custom + Node *getNode() + { + Node *p = &nodes[size++]; + p->init(); + return p; + } + void init() + { + size = cases = 0; + root = getNode(); + memset(chCnt, 0, sizeof(chCnt)); + } + inline int toIndex(char c) + { + return c - 'a'; + } + // basis operation + void insert(const char str[], int w) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + { + p->next[idx] = getNode(); + if (i) + chCnt[idx]++; + } + p = p->next[idx]; + } + p->cnt += w; + } + int find(const char str[]) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + return p->cnt; + } + + void free() + { + return; + // owner memory pool version + queue Q; + Q.push(root); + Node *u; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < MAXCHAR; i++) + { + if (u->next[i] != NULL) + { + Q.push(u->next[i]); + } + } + delete u; + } + } +} trie1, trie2; +/* +#different_prefix * #different_suffix - duplicate + +duplicate example + +prefix : Aaa, suffix : aaaB +then (Aaa)x(aaaB) AB, AaB, AaaB, ..., AaaaaaB +if use '*' => 3 * 4 = 12, there are only 6 str. overlap !! +(x+1) * (y+1) => x + y + 1, #duplicate = x * y +*/ +int main() +{ + int N; + char s[MAXS]; + while (scanf("%d", &N) == 1 && N) + { + trie1.init(), trie2.init(); + + int sp[MAXCHAR] = {}; + + for (int i = 0; i < N; i++) + { + scanf("%s", s); + + int len = strlen(s); + + trie1.insert(s, 1); + reverse(s, s + len); + trie2.insert(s, 1); + + if (len == 1) + sp[s[0] - 'a'] = 1; + } + long long ret = (long long)(trie1.size - 1) * (trie2.size - 1); + for (int i = 0; i < 26; i++) + ret -= (long long)trie1.chCnt[i] * trie2.chCnt[i]; + for (int i = 0; i < 26; i++)// lost count string with single character + ret += sp[i]; + printf("%lld\n", ret); + + trie1.free(), trie2.free(); + } + return 0; +} +/* +3 +abc +def +abef + +1 +a + +2 +a +b + +2 +ab +ba + +3 +a +a +a + + */ diff --git a/152.cpp b/152.cpp index f3def6d..b6805f7 100644 --- a/152.cpp +++ b/152.cpp @@ -1,34 +1,75 @@ -#include -#include -#include -#include +#include + using namespace std; -int main() { - int tree[5000 + 5][3], N = 0; - int dist[10]; - memset(dist, 0, sizeof dist); - - while (true) { - scanf("%d%d%d", &tree[N][0], &tree[N][1], &tree[N][2]); - if (!(tree[N][0] || tree[N][1] || tree[N][2])) break; - ++N; +struct point +{ + int x, y, z; +}; + +bool theTruthIsOutThere(const point &a, const point &b) +{ + return a.x < b.x; +} + +int main() +{ + point tmp; + int best; + double dist; + int distance; + bool **counted; + int result[10] = {0}; + vector points; + while (cin >> tmp.x >> tmp.y >> tmp.z) + { + if (tmp.x == 0 && tmp.y == 0 && tmp.z == 0) + { + break; + } + points.push_back(tmp); } - - for (int i = 0; i < N; i++) { - double minD = DBL_MAX; - for (int j = 0; j < N; j++) { - if (i == j) continue; - double xD = tree[i][0] - tree[j][0]; - double yD = tree[i][1] - tree[j][1]; - double zD = tree[i][2] - tree[j][2]; - double DD = sqrt(xD * xD + yD * yD + zD * zD); - if (DD < minD) minD = DD; + sort(points.begin(), points.end(), theTruthIsOutThere); + for (int i = 0, sz = points.size(); i < sz; i++) + { + best = 10; + // left side + for (int j = i - 1; j >= 0; j--) + { + if (abs(points[i].x - points[j].x) >= best) + { + break; + } + dist = sqrt(pow(points[i].x - points[j].x, 2) + pow(points[i].y - points[j].y, 2) + pow(points[i].z - points[j].z, 2)); + distance = dist; + if (distance < best) + { + best = distance; + } + } + // right side + for (int j = i + 1; j < sz; j++) + { + if (abs(points[i].x - points[j].x) >= best) + { + break; + } + dist = sqrt(pow(points[i].x - points[j].x, 2) + pow(points[i].y - points[j].y, 2) + pow(points[i].z - points[j].z, 2)); + distance = dist; + if (distance < best) + { + best = distance; + } } - if (minD < 10) ++dist[(int) minD]; + if (best < 10) + { + result[best]++; + } + } + for (int i = 0; i < 10; i++) + { + printf("%4i", result[i]); } - - for (int i = 0; i < 10; i++) printf("%4d", dist[i]); - printf("\n"); + cout << endl; return 0; } diff --git a/1521.cpp b/1521.cpp new file mode 100644 index 0000000..37c8061 --- /dev/null +++ b/1521.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +/* ********************************************** +Author : JayYe +Created Time: 2013-9-10 16:12:01 +File Name : JayYe.cpp +*********************************************** */ + +const int maxn = 10000 + 10; + +bool vis[maxn]; +int a[maxn]; + +void get_prime() +{ + vis[1] = 1; + for (int i = 2; i * i <= 10000; i++) + if (!vis[i]) + for (int j = i * i; j <= 10000; j += i) + vis[j] = 1; +} + +int main() +{ + get_prime(); + int n; + while (scanf("%d", &n) != -1) + { + int sum = 0; + for (int i = 2; i <= n; i++) + if (!vis[i]) + a[++sum] = i; + int l = 1, r = sum; + int ans = 0; + while (l <= r) + { + ans++; + int cur = a[r]; + while (l < r && cur * a[l] <= n) + cur *= a[l++]; + r--; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/1523.cpp b/1523.cpp new file mode 100644 index 0000000..628d7ff --- /dev/null +++ b/1523.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +#define INF 999999999 +typedef long long ll; +int seat[8]; +double ans; +double get_M() +{ + double sum1(0), sum2(0), sum; + sum1 += seat[0] - seat[6] + seat[1] - seat[5] + seat[2] - seat[4]; + sum2 += seat[0] - seat[2] + seat[7] - seat[3] + seat[6] - seat[4]; + sum = sqrt(sum1 * sum1 + sum2 * sum2); + return sum; +} +void solve() +{ + do + { + ans = min(ans, get_M()); + } while (next_permutation(seat, seat + 8)); + return; +} +int main() +{ + while (true) + { + int sum(0); + for (int i = 0; i < 8; i++) + scanf("%d", &seat[i]), sum += seat[i]; + if (sum == 0) + break; + ans = 999999999; + sort(seat, seat + 8); + solve(); + printf("%.3lf\n", ans); + } + return 0; +} diff --git a/1524.cpp b/1524.cpp new file mode 100644 index 0000000..44493f2 --- /dev/null +++ b/1524.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +double f[128] = {}; +int n; +double g(double val) +{ + double ret = 0; + for (int i = 0; i <= n; i++) + { + ret += f[i] * pow(val, i + 1); + } + return ret; +} +int main() +{ + double l, r; + while (scanf("%d", &n) == 1 && n) + { + for (int i = n; i >= 0; i--) + { + scanf("%lf", &f[i]); + f[i] = f[i] / (i + 1); + } + scanf("%lf %lf", &l, &r); + printf("%.3lf\n", (g(r) - g(l)) / (r - l)); + } + return 0; +} diff --git a/1525.cpp b/1525.cpp new file mode 100644 index 0000000..e4a0a3d --- /dev/null +++ b/1525.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +struct Node +{ + Node *l, *r; + char c; + void init(char a) + { + c = a, l = r = NULL; + } +} nodes[128]; +int nsize; +void insert(Node *&u, char c) +{ + if (u == NULL) + { + u = &nodes[nsize++]; + u->init(c); + return; + } + if (u->c > c) + insert(u->l, c); + else + insert(u->r, c); +} +void dfs(Node *u) +{ + if (u == NULL) + return; + putchar(u->c); + dfs(u->l); + dfs(u->r); +} +int main() +{ + while (true) + { + string line; + vector g; + while (cin >> line) + { + if (line == "$" || line == "*") + break; + g.push_back(line); + } + memset(nodes, 0, sizeof(nodes)); + + Node *root = NULL; + nsize = 0; + for (int i = g.size() - 1; i >= 0; i--) + { + for (int j = 0; j < g[i].size(); j++) + insert(root, g[i][j]); + } + dfs(root); + puts(""); + if (line == "$") + return 0; + } + return 0; +} diff --git a/1526.cpp b/1526.cpp new file mode 100644 index 0000000..7fdef4f --- /dev/null +++ b/1526.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +const int N = 9005; +int w, n, sum, an, pos; +struct Seg +{ + int value, len, pos; +} s[N], ans[N]; + +bool cmp(Seg a, Seg b) +{ + return a.len < b.len; +} + +int getnum(int pos) +{ + int l = 1, r = n; + while (l < r) + { + int mid = (l + r) / 2; + if (s[mid].pos > pos) + { + r = mid; + } + else + { + l = mid + 1; + } + } + return s[l - 1].value; +} + +int cal(int pos) +{ + int num = getnum(pos), ans = 0; + int r = (pos - 1) / w; + int c = (pos - 1) % w; + for (int i = r - 1; i <= r + 1; i++) + for (int j = c - 1; j <= c + 1; j++) + { + int v = i * w + j; + if (i < 0 || j < 0 || j >= w || v >= sum || v == pos - 1) + { + continue; + } + int num2 = getnum(v + 1); + int t = abs(num2 - num); + ans = max(ans, t); + } + return ans; +} + +int main() +{ + while (~scanf("%d", &w) && w) + { + n = sum = an = 0; + while (~scanf("%d%d", &s[n].value, &s[n].len) && s[n].value || s[n].len) + { + if (n != 0) + { + s[n].pos = s[n - 1].pos + s[n - 1].len; + } + else + { + s[n].pos = 1; + } + sum += s[n].len; + n++; + } + s[n].len = s[n].value = 0; + s[n].pos = s[n - 1].pos + s[n - 1].len; + for (int k = 0; k <= n; k++) + { + int r = (s[k].pos - 1) / w; + int c = (s[k].pos - 1) % w; + for (int i = r - 1; i <= r + 1; i++) + for (int j = c - 1; j <= c + 1; j++) + { + int v = i * w + j; + if (i < 0 || j < 0 || j >= w || v >= sum) + { + continue; + } + ans[an].len = v + 1; + ans[an++].value = cal(v + 1); + } + } + sort(ans, ans + an, cmp); + Seg save = ans[0]; + printf("%d\n", w); + for (int i = 0; i < an; i++) + { + if (ans[i].value == save.value) + { + continue; + } + printf("%d %d\n", save.value, ans[i].len - save.len); + save = ans[i]; + } + printf("%d %d\n", save.value, sum - save.len + 1); + printf("0 0\n"); + } + printf("0\n"); + return 0; +} diff --git a/1529.cpp b/1529.cpp new file mode 100644 index 0000000..1bf98ef --- /dev/null +++ b/1529.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +const double esp = 1e-6; +int h1, m1, h2, m2; +double t1, t2, s[25]; + +int main() +{ + s[0] = 0; + for (int i = 1; i < 25; i++) + { + s[i] = s[i - 1] + 720.0 / 11; + } + printf("Program 3 by team X\n"); + printf("Initial time Final time Passes\n"); + while (~scanf("%d%d%d%d", &h1, &m1, &h2, &m2)) + { + t1 = h1 * 60 + m1; + t2 = h2 * 60 + m2; + if (t2 < t1) + { + t2 += 720; + } + int ans1, ans2; + for (int i = 0; i < 24; i++) + { + if (s[i] < t1) + { + ans1 = i; + } + if (s[i] < t2) + { + ans2 = i; + } + } + printf(" %02d:%02d %02d:%02d%8d\n", h1, m1, h2, m2, ans2 - ans1); + } + printf("End of program 3 by team X\n"); + return 0; +} diff --git a/153.cpp b/153.cpp index 8e05250..f4d51ec 100644 --- a/153.cpp +++ b/153.cpp @@ -1,34 +1,110 @@ -#include -#include -using namespace std; - -int main(){ - for(;;){ - char str[30]; - cin>>str; - if(str=="#") - break; - string init=str; - int len=sizeof(str); - for(int i=0;istr[j]){ - char temp=str[i]; - str[i]=str[j]; - str[j]=temp; - } - } - } - int count=0; - do{ - count++; - string s; - for(int k=0;k + +using namespace std; + +set chars; +int char_count[128]; + +template +inline T gcd(T a, T b) +{ + T c; + while (b) + { + c = b, b = a % b, a = c; + } + return a; +} + +int perm_cnt(string &s) +{ + vector top, btm; + int size = 0; + for (set::iterator it = chars.begin(); it != chars.end(); it++) + { + size += char_count[*it]; + if (char_count[*it] > 1) + { + for (int i = 2; i <= char_count[*it]; i++) + { + btm.push_back(i); + } + } + } + for (int i = 2; i <= size; i++) + { + top.push_back(i); + } + for (int i = 0, sz1 = top.size(); i < sz1; i++) + { + for (int j = 0, sz2 = btm.size(); j < sz2; j++) + { + if (top[i] == 1 || btm[j] == 1) + { + continue; + } + int tmp = gcd(top[i], btm[j]); + if (tmp == 1) + { + continue; + } + top[i] = top[i] / tmp; + btm[j] = btm[j] / tmp; + } + } + int result = 1; + for (int i = 0, sz = top.size(); i < sz; i++) + { + result *= top[i]; + } + return result; +} + +int solve(string &s, int start) +{ + int result = 0, counter = 0; + if (start == s.size()) + { + return 0; + } + for (set::iterator it = chars.begin(); it != chars.end(); it++) + { + if (*it == s[start]) + { + break; + } + if (char_count[*it] == 0) + { + continue; + } + char_count[*it]--; + result += perm_cnt(s); + char_count[*it]++; + } + char_count[s[start]]--; + return result + solve(s, start + 1); +} + +int main() +{ + string input; + while (getline(cin, input)) + { + if (input == "#") + { + break; + } + chars.clear(); + for (int i = 0; i < 128; i++) + { + char_count[i] = 0; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + chars.insert(input[i]); + char_count[input[i]]++; + } + cout << setw(10) << solve(input, 0) + 1 << endl; + } + return 0; +} diff --git a/1530.cpp b/1530.cpp new file mode 100644 index 0000000..81e85a6 --- /dev/null +++ b/1530.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +const double esp = 1e-6; +char str[20]; +int i; + +int main() +{ + printf("Program 6 by team X\n"); + while (~scanf("%s", str)) + { + int num1 = 0, flag = 1; + for (i = 7; i >= 1; i--) + { + if (str[i] == '1') + { + flag = 0; + } + num1 += (str[i] - '0') * (1 << (7 - i)); + } + double s = pow(2, num1 - 63); + double num2 = 1; + for (i = 8; i < 16; i++) + { + if (str[i] == '1') + { + flag = 0; + } + num2 += (str[i] - '0') * (1.0 / (1 << (i - 7))); + } + if (flag) + { + printf("0.000000e+000\n"); + continue; + } + double ans = s * num2; + int w = 0; + while (ans - 10 > -esp) + { + ans /= 10; + w++; + } + while (ans - 1 < -esp) + { + ans *= 10; + w--; + } + if (str[0] == '1') + { + printf("-"); + } + else + { + printf(" "); + } + printf("%.6lfe", ans); + if (w >= 0) + { + printf("+%03d\n", w); + } + else + { + printf("-%03d\n", -w); + } + } + printf("End of program 6 by team X\n"); + return 0; +} diff --git a/1531.cpp b/1531.cpp new file mode 100644 index 0000000..570b9d8 --- /dev/null +++ b/1531.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +const double EPS = 1e-6; +const double SQRT3 = sqrt((double)3); +const int dir[7][2] = {{0, 0}, {-1, 1}, {0, 2}, {1, 1}, {1, -1}, {0, -2}, {-1, -1}}; + +struct Point +{ + double x, y; +}; + +double r, xa, ya, xb, yb; +double stepx, stepy; + +bool input(); +Point findFirstSection(double x, double y); +Point find(double x, double y); +void solve(); + +int main() +{ +#ifndef ONLINE_JUDGE + freopen("d:\\OJ\\uva_in.txt", "r", stdin); +#endif + + while (input()) + { + solve(); + } + return 0; +} + +bool input() +{ + if (scanf("%lf%lf%lf%lf%lf", &r, &xa, &ya, &xb, &yb) != 5 || + !(r || xa || ya || xb || yb)) + return false; + + stepx = 1.5 * r; + stepy = SQRT3 / 2 * r; + + return true; +} + +Point findFirstSection(double x, double y) +{ + int dx = (int)(x / stepx); + int dy = (int)(y / stepy); + if (dx & 1) + { + if (!(dy & 1)) + dy++; + } + else + { + if (dy & 1) + dx++; + } + + double Min = 0x3f3f3f3f; + Point ans; + + for (int i = 0; i < 7; i++) + { + double tmpx = stepx * (dx + dir[i][0]); + double tmpy = stepy * (dy + dir[i][1]); + double tmp = sqrt(pow(tmpx - x, 2) + pow(tmpy - y, 2)); + + if (tmp < Min) + { + ans.x = tmpx; + ans.y = tmpy; + Min = tmp; + } + } + + return ans; +} + +Point find(double x, double y) +{ + Point ans; + + if (x > 0 && y > 0) + { + ans = findFirstSection(x, y); + } + else if (x < 0 && y > 0) + { + ans = findFirstSection(-x, y); + ans.x = -ans.x; + } + else if (x < 0 && y < 0) + { + ans = findFirstSection(-x, -y); + ans.x = -ans.x; + ans.y = -ans.y; + } + else + { + ans = findFirstSection(x, -y); + ans.y = -ans.y; + } + + return ans; +} + +void solve() +{ + Point a = find(xa, ya); + Point b = find(xb, yb); + double dis; + + if (fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS) + { + dis = sqrt(pow(xa - xb, 2) + pow(ya - yb, 2)); + printf("%.3lf\n", dis); + return; + } + + int x1 = (int)(fabs(a.x - b.x) / stepx + 0.5); + int x2 = (int)(fabs(a.y - b.y) / stepy + 0.5); + int d; + + if (x1 >= x2) + { + d = x1; + } + else + { + d = x1 + (x2 - x1) / 2; + } + dis = sqrt(pow(a.x - xa, 2) + pow(a.y - ya, 2)) + sqrt(pow(b.x - xb, 2) + pow(b.y - yb, 2)) + d * SQRT3 * r; + + printf("%.3lf\n", dis); +} diff --git a/1533.cpp b/1533.cpp new file mode 100644 index 0000000..6bba74b --- /dev/null +++ b/1533.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define PB push_back +#define MP make_pair +#define INF 0x5fffffff +#define rep(i, x, y) for (i = x; i <= y; i++) +#define reps(i, x, y) for (i = x; i >= y; i--) +#define sqr(x) ((x) * (x)) + +typedef long long LL; +typedef double DB; +typedef vector VI; + +struct node +{ + int val, pre, from, to; +}; +vector L; +const int dir[6][16] = + { + 0, 0, 0, 1, 0, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 10, //NW 1 + 0, 0, 1, 0, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 10, 0, //NE 2 + 0, 0, 0, 2, 0, 4, 5, 0, 7, 8, 9, 0, 11, 12, 13, 14, //W 0 + 0, 0, 3, 0, 5, 6, 0, 8, 9, 10, 0, 12, 13, 14, 15, 0,//E 3 + 0, 2, 4, 5, 7, 8, 9, 11, 12, 13, 14, 0, 0, 0, 0, 0, //SW 5 + 0, 3, 5, 6, 8, 9, 10, 12, 13, 14, 15, 0, 0, 0, 0, 0 //SE 4 + // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +}; +int cnt = 0; +int hash_(bool *a) +{ + int val = 0, i; + rep(i, 1, 15) val = val * 2 + a[i]; + assert(0 <= val && val <= 33000); + return val; +} +void get(int val, bool *a) +{ + int i; + reps(i, 15, 1) a[i] = val & 1, val >>= 1; +} +int mark[40000]; +bool bo[20]; +void Print(node t) +{ + VI ans; + while (t.pre != -1) + { + ans.PB(t.to); + ans.PB(t.from); + t = L[t.pre]; + } + printf("%d\n", ans.size() >> 1); + for (int i = ans.size() - 1; i >= 1; i--) + { + printf("%d ", ans[i]); + } + printf("%d\n", ans[0]); +} +void solve() +{ + int i, j, k, l, q; + cnt++; + scanf("%d", &k); + node t; + t.pre = -1; + memset(bo, 1, sizeof(bo)); + bo[k] = bo[0] = 0; + t.val = hash_(bo); + rep(i, 1, 15) bo[i] ^= 1; + int mval = hash_(bo); + L.clear(); + L.PB(t); + for (l = 0; l < L.size(); l++) + { + t = L[l]; + get(t.val, bo); + rep(i, 1, 15) + { + if (bo[i]) + { + rep(k, 0, 5) + { + j = 0; + int x = i; + while (bo[x]) + { + bo[x] = 0; + j++; + x = dir[k][x]; + } + if (j >= 2 && x) + { + bo[x] = 1; + t.val = hash_(bo); + if (mark[t.val] != cnt) + { + t.from = i; + t.to = x; + t.pre = l; + if (t.val == mval) + { + Print(t); + return; + } + mark[t.val] = cnt; + L.PB(t); + } + bo[x] = 0; + } + x = i; + while (j--) + { + bo[x] = 1; + x = dir[k][x]; + } + } + } + } + } + puts("IMPOSSIBLE"); +} + +int main() +{ + int Case; + scanf("%d", &Case); + while (Case--) + { + solve(); + } + return 0; +} diff --git a/1534.cpp b/1534.cpp new file mode 100644 index 0000000..5a905ec --- /dev/null +++ b/1534.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +typedef long long LL; +typedef unsigned long long ULL; +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MEM(a, b) memset((a), (b), sizeof(a)) +const LL INF = 1000000007; +const int N = 510; + +double dp[N][N]; +double a[N]; +double b[N]; +int main() +{ + //freopen("input.txt", "r", stdin); + int ncase; + cin >> ncase; + while (ncase--) + { + double *x = a; + double *y = b; + int n, m; + cin >> n >> m; + MEM(dp, 0x6f); + if (n < m) + { + swap(x, y); + } + for (int i = 0; i < n; i++) + { + cin >> x[i]; + } + for (int j = 0; j < m; j++) + { + cin >> y[j]; + } + if (n < m) + { + swap(n, m); + } + sort(a, a + n); + sort(b, b + m); + for (int i = 0; i < m; i++) + { + for (int j = i; j < n; j++) + { + if (i == j) + { + if (i > 0) + { + dp[i][j] = dp[i - 1][j - 1] + abs(b[i] - a[j]); + } + else + { + dp[i][j] = abs(b[i] - a[j]); + } + } + else + { + if (i > 0) + { + dp[i][j] = min(dp[i][j - 1], dp[i - 1][j - 1] + abs(b[i] - a[j])); + } + else + { + dp[i][j] = min(dp[i][j - 1], abs(b[i] - a[j])); + } + } + } + } + printf("%.1lf\n", dp[m - 1][n - 1]); + } + return 0; +} diff --git a/1537.cpp b/1537.cpp new file mode 100644 index 0000000..ee1ab31 --- /dev/null +++ b/1537.cpp @@ -0,0 +1,217 @@ +#include + +using namespace std; + +#define MAXN 105 +#define MAXM 100005 +#define INF 1000000000 + +struct node +{ + int v, w, next; +} edge[MAXM]; +struct Edge +{ + int u, v, w; + Edge() {} + Edge(int a, int b, int c) + { + u = a; + v = b; + w = c; + } + void init() + { + w = 0; + } + bool operator>(const Edge &a) const + { + return w > a.w; + } +} mx[MAXN]; //用于存储æ¯Â个点到park点的最大边 +int n, m, k, sum; //sum为结果 +int e, head[MAXN], vis[MAXN], dis[MAXN], use[MAXN][MAXN];//head用于邻接表 vis是标记数组 dis用于求最å°Â生æˆÂ树 +//use用æ¥标记两点之间是å¦有边 +int blocks, size[MAXN], belong[MAXN], nearvex[MAXN];//blocks表示去除parkåŽ有几个连通å— size是æ¯Â个连通å—的个数 +//belong表示该点属于哪个连通å— nearvex用于在生æˆÂ树中记录边 +int point[MAXN], link[MAXN];//point表示æ¯Â个连通å—中与park点最近的点 link则是该点与park点的è·Â离 +map mp; //用于映射åÂÂ字 +void init() +{ + e = 0, n = 1; + blocks = 0, sum = 0; + memset(head, -1, sizeof(head)); + memset(vis, 0, sizeof(vis)); + memset(size, 0, sizeof(size)); + memset(use, 0, sizeof(use)); + for (int i = 1; i < MAXN; i++) + { + mx[i].init(); + } + memset(nearvex, 0, sizeof(nearvex)); + mp.clear(); +} +void insert(int x, int y, int w) +{ + edge[e].v = y; + edge[e].w = w; + edge[e].next = head[x]; + head[x] = e++; +} +int getId(char s[]) +{ + if (mp.find(s) == mp.end()) + { + mp[s] = ++n; + } + else + { + return mp[s]; + } + return n; +} +void dfs(int v)//该dfs将图分æˆÂ了一些连通å— +{ + vis[v] = 1; + size[blocks]++; + belong[v] = blocks; + for (int i = head[v]; i != -1; i = edge[i].next) + if (!vis[edge[i].v]) + { + dfs(edge[i].v); + } +} +void prim(int cur)//对æŸÂ个连通å—求最å°Â生æˆÂ树 +{ + for (int i = 1; i <= n; i++) + { + dis[i] = INF; + } + for (int i = 1; i <= n; i++)//设置å—内æŸÂ点为起点æ¥求生æˆÂ树 + if (belong[i] == cur) + { + dis[i] = 0; + break; + } + for (int i = 1; i <= size[cur]; i++)//循环次数为该å—的顶点数,因为这与一般的求MST略微ä¸ÂÃ¥ÂÂŒ + { + int mi = INF, pos = -1; + for (int j = 1; j <= n; j++) + if (nearvex[j] != -1 && mi > dis[j]) + { + mi = dis[j], pos = j; + } + if (pos != -1) + { + sum += mi; + use[pos][nearvex[pos]] = use[nearvex[pos]][pos] = 1;//标记生æˆÂ树中所用的边 + nearvex[pos] = -1; + for (int j = head[pos]; j != -1; j = edge[j].next) + if (nearvex[edge[j].v] != -1 && dis[edge[j].v] > edge[j].w) + { + dis[edge[j].v] = edge[j].w; + nearvex[edge[j].v] = pos; + } + } + } +} +void getMax(int v, int fa, int w)//该函数用于更新新的生æˆÂ树中点到park点的最大边 +{ + nearvex[v] = fa; + Edge t(v, fa, w); + if (mx[fa] > t) + { + mx[v] = mx[fa]; + } + else + { + mx[v] = t; + } + for (int i = head[v]; i != -1; i = edge[i].next) + if (use[v][edge[i].v] && edge[i].v != fa) + { + getMax(edge[i].v, v, edge[i].w);//必须是生æˆÂ树中的边并且ä¸Â是回边æ‰Â往下æÂÂœ + } +} +void GetMdegreeMST() +{ + vis[1] = 1; + for (int i = 2; i <= n; i++)//求连通å— + if (!vis[i]) + { + blocks++; + dfs(i); + } + nearvex[1] = -1; + for (int i = 1; i <= blocks; i++) + { + prim(i); + } + for (int i = 1; i <= n; i++) + { + link[i] = INF; + } + for (int i = head[1]; i != -1; i = edge[i].next)//生æˆÂ一棵m度的生æˆÂ树 + if (link[belong[edge[i].v]] > edge[i].w) + { + link[belong[edge[i].v]] = edge[i].w; + point[belong[edge[i].v]] = edge[i].v; + } + for (int i = 1; i <= blocks; i++)//将park点与æ¯Â个连通å—中与其最近的点相连,并且标记边 + { + sum += link[i]; + use[1][point[i]] = use[point[i]][1] = 1; + } +} +void slove() +{ + int degree = blocks; + getMax(1, 0, 0); //首先从park点出å‘求一éÂÂ最大边 + while (degree < k)//å°Â试迭代 k - degree次 + { + int maxval = 0, pos = 0, w; + for (int i = head[1]; i != -1; i = edge[i].next)//用于找到差值最大的点 + if (!use[1][edge[i].v] && mx[edge[i].v].w - edge[i].w > maxval) + { + maxval = mx[edge[i].v].w - edge[i].w, pos = edge[i].v; + w = edge[i].w; + } + if (!pos) + { + break; + } + sum -= maxval;//更新答案 + degree++; + use[mx[pos].u][mx[pos].v] = use[mx[pos].v][mx[pos].u] = 0;//将最大边删除 + use[1][pos] = use[pos][1] = 1; + getMax(pos, 1, w);//更新最大边 + } +} +int main() +{ + int t; + cin >> t; + while (t--) + { + char s1[55], s2[55]; + int w; + scanf("%d", &m); + init(); + mp["Park"] = 1; + for (int i = 0; i < m; i++) + { + scanf("%s%s%d", s1, s2, &w); + insert(getId(s1), getId(s2), w); + insert(getId(s2), getId(s1), w); + } + scanf("%d", &k); + GetMdegreeMST(); + slove(); + printf("Total miles driven: %d\n", sum); + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/154.cpp b/154.cpp new file mode 100644 index 0000000..92bf272 --- /dev/null +++ b/154.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +char str[1000]; +char Bin[] = "roygb"; +int A[101][130], N; +int C[101]; + +void Set(int n) +{ + int i, j, d; + for (i = 2; str[i]; i += 4) + { + j = str[i - 2]; + d = str[i]; + A[n][j] = d; + } +} + +void Cal() +{ + int i, j, c, k, d; + int min = 21474836; + for (i = 0; i < N - 1; i++) + { + for (j = i + 1; j < N; j++) + { + c = 0; + for (k = 0; Bin[k]; k++) + { + d = Bin[k]; + if (A[i][d] != A[j][d]) + { + c++; + } + } + C[i] += c; + C[j] += c; + } + } + for (i = 0; i < N; i++) + { + if (C[i] < min) + { + min = C[i]; + k = i; + } + } + printf("%d\n", k + 1); +} + +int main() +{ + while (gets(str) && strcmp(str, "#")) + { + N = 0; + Set(N++); + while (1) + { + gets(str); + if (str[0] == 'e') + { + break; + } + Set(N++); + } + Cal(); + for (int i = 0; i < N; i++) + { + C[i] = 0; + } + } + return 0; +} diff --git a/1541.cpp b/1541.cpp new file mode 100644 index 0000000..053f049 --- /dev/null +++ b/1541.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define inf 0x3f3f3f3f +#define Max 110 +int max(int a, int b) +{ + return a > b ? a : b; +} +int min(int a, int b) +{ + return a < b ? a : b; +} +double dp[60][60]; +char s[10]; +int t, m, T; +int to[60]; +int main() +{ + int i, j; + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &m, &T); + to[0] = 0; + to[m + 1] = 0; + to[m + 2] = -1; + for (i = 1; i <= m; i++) + { + if (scanf("%d", &to[i]) == 0) + { + scanf("%s", s); + to[i] = inf; + } + } + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1.0; + for (j = 0; j <= T; j++) + for (i = 0; i <= m; i++) + { + // printf("i %d j %d dp %lf\n",i,j,dp[i][j]); + if (to[i + 1] == inf) + { + dp[i + 1][j + 2] += dp[i][j] * 0.5; + } + else + { + dp[i + 1 + to[i + 1]][j + 1] += dp[i][j] * 0.5; + } + if (to[i + 2] == inf) + { + dp[i + 2][j + 2] += dp[i][j] * 0.5; + } + else + { + dp[i + 2 + to[i + 2]][j + 1] += dp[i][j] * 0.5; + } + } + for (i = 0; i < T; i++) + { + dp[m + 1][T] += dp[m + 1][i]; + } + if (dp[m + 1][T] == 0.5) + { + printf("Push. 0.5000\n"); + } + else if (dp[m + 1][T] < 0.5) + { + printf("Bet against. %.4lf\n", dp[m + 1][T]); + } + else + { + printf("Bet for. %.4lf\n", dp[m + 1][T]); + } + } +} diff --git a/1543.cpp b/1543.cpp new file mode 100644 index 0000000..40ae6a0 --- /dev/null +++ b/1543.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) < (b) ? (a) : (b)) +const int N = 45; +int n, m, i, j, k, x; +double p[N], dp[N][N][N]; +const double pi = acos(-1.0); + +inline double cal(double p1, double p2) +{ + double d = pi * (p1 - p2); + return 2 * sin(d); +} + +inline double area(double p1, double p2, double p3) +{ + double a = cal(p1, p2); + double b = cal(p2, p3); + double c = cal(p3, p1); + double p = (a + b + c) / 2; + return sqrt(p * (p - a) * (p - b) * (p - c)); +} + +int main() +{ + while (~scanf("%d%d", &n, &m) && n || m) + { + double ans = 0; + memset(dp, 0, sizeof(dp)); + for (i = 1; i <= n; i++) + { + scanf("%lf", &p[i]); + } + for (x = 1; x <= n; x++) + { + int t = min(x, m); + for (i = 3; i <= t; i++) + { + for (j = 1; j < x; j++) + { + for (k = j + 1; k < x; k++) + { + double s = area(p[x], p[k], p[j]); + dp[i][j][x] = max(dp[i][j][x], dp[i - 1][j][k] + s); + if (i == m) + { + ans = max(ans, dp[i][j][x]); + } + } + } + } + } + printf("%.6lf\n", ans); + } + return 0; +} diff --git a/1544.cpp b/1544.cpp new file mode 100644 index 0000000..830493f --- /dev/null +++ b/1544.cpp @@ -0,0 +1,211 @@ +#include + +using namespace std; + +int main() +{ + int testcase; + char s[2048]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%s", s); + int x[505] = {}, y[505] = {}, op; + int z[1005] = {}; + int slen = strlen(s), xlen = 0, ylen = 0, zlen = 0; + int i, j; + for (i = slen - 1; i >= 0; i--) + { + if (s[i] == '+' || s[i] == '-' || s[i] == '*') + { + op = s[i]; + break; + } + y[ylen++] = s[i] - '0'; + } + for (i--; i >= 0; i--) + x[xlen++] = s[i] - '0'; + int plen = max(xlen, ylen + 1); + if (op == '+') + { + for (i = 0; i < xlen; i++) + z[i] = x[i]; + for (i = 0; i < ylen; i++) + z[i] += y[i]; + zlen = max(xlen, ylen) + 1; + for (i = 0; i <= zlen; i++) + { + if (z[i] >= 10) + z[i + 1] += z[i] / 10, z[i] %= 10; + } + zlen++; + while (zlen > 0 && z[zlen] == 0) + zlen--; + plen = max(plen, zlen + 1); + for (i = xlen; i < plen; i++) + putchar(' '); + for (i = xlen - 1; i >= 0; i--) + putchar(x[i] + '0'); + puts(""); + for (i = ylen + 1; i < plen; i++) + putchar(' '); + putchar('+'); + for (i = ylen - 1; i >= 0; i--) + putchar(y[i] + '0'); + puts(""); + for (i = 0; i < plen; i++) + putchar('-'); + puts(""); + for (i = zlen + 1; i < plen; i++) + putchar(' '); + for (i = zlen; i >= 0; i--) + putchar(z[i] + '0'); + puts(""); + puts(""); + continue; + } + if (op == '-') + { + for (i = 0; i < xlen; i++) + z[i] = x[i]; + for (i = 0; i < ylen; i++) + z[i] -= y[i]; + zlen = max(xlen, ylen) + 1; + for (i = 0; i <= zlen; i++) + { + while (z[i] < 0) + { + z[i + 1]--; + z[i] += 10; + } + } + zlen++; + while (zlen > 0 && z[zlen] == 0) + zlen--; + plen = max(plen, zlen + 1); + for (i = xlen; i < plen; i++) + putchar(' '); + for (i = xlen - 1; i >= 0; i--) + putchar(x[i] + '0'); + puts(""); + for (i = ylen + 1; i < plen; i++) + putchar(' '); + putchar('-'); + for (i = ylen - 1; i >= 0; i--) + putchar(y[i] + '0'); + puts(""); + int pp = max(ylen + 1, zlen + 1); + for (i = pp; i < plen; i++) + putchar(' '); + for (i = pp - 1; i >= 0; i--) + putchar('-'); + puts(""); + for (i = zlen + 1; i < plen; i++) + putchar(' '); + for (i = zlen; i >= 0; i--) + putchar(z[i] + '0'); + puts(""); + puts(""); + continue; + } + if (op == '*') + { + short tmp[505][1035] = {}, tlen; + zlen = xlen + ylen + 5; + for (i = 0; i < ylen; i++) + { + if (y[i] == 0) + continue; + for (j = 0; j < xlen; j++) + { + tmp[i][i + j] = y[i] * x[j]; + z[i + j] += y[i] * x[j]; + } + tlen = i + xlen + 5; + for (j = 0; j < tlen; j++) + { + if (tmp[i][i + j] >= 10) + tmp[i][i + j + 1] += tmp[i][i + j] / 10, tmp[i][i + j] %= 10; + } + } + for (i = 0; i <= zlen; i++) + { + if (z[i] >= 10) + { + z[i + 1] += z[i] / 10; + z[i] %= 10; + } + } + zlen++; + while (zlen > 0 && z[zlen] == 0) + zlen--; + plen = max(plen, zlen + 1); + for (i = xlen; i < plen; i++) + putchar(' '); + for (i = xlen - 1; i >= 0; i--) + putchar(x[i] + '0'); + puts(""); + for (i = ylen + 1; i < plen; i++) + putchar(' '); + putchar('*'); + for (i = ylen - 1; i >= 0; i--) + putchar(y[i] + '0'); + puts(""); + if (ylen > 1) + { + + for (i = 0; i < ylen; i++) + { + if (y[i]) + for (j = i; j <= i + xlen; j++) + { + if (tmp[i][j] >= 10) + tmp[i][j + 1] += tmp[i][j] / 10, tmp[i][j] %= 10; + } + tlen = i + xlen + 5; + while (tlen > i && tmp[i][tlen] == 0) + tlen--; + if (i == 0) + { + int pp = max(ylen + 1, tlen + 1); + for (j = pp; j < plen; j++) + putchar(' '); + for (j = pp - 1; j >= 0; j--) + putchar('-'); + puts(""); + } + for (j = tlen + 1; j < plen; j++) + putchar(' '); + for (j = tlen; j >= i; j--) + putchar(tmp[i][j] + '0'); + puts(""); + } + } + for (i = zlen + 1; i < plen; i++) + putchar(' '); + for (i = 0; i <= zlen; i++) + putchar('-'); + puts(""); + for (i = zlen + 1; i < plen; i++) + putchar(' '); + for (i = zlen; i >= 0; i--) + putchar(z[i] + '0'); + puts(""); + puts(""); + continue; + } + } + return 0; +} +/* +50 +123+123 +123-123 +1+99999 +123456*4 +4*123456 +10500*50 +12354*56 +10000-1 +123-123 +*/ diff --git a/1546.cpp b/1546.cpp new file mode 100644 index 0000000..765a109 --- /dev/null +++ b/1546.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +const int N = 205; +int t, s, c, a[N][N]; + +int main() +{ + scanf("%d", &t); + while (t--) + { + scanf("%d%d", &s, &c); + memset(a, 0, sizeof(a)); + for (int i = 0; i < s; i++) + { + scanf("%d", &a[0][i]); + } + for (int i = 1; i < s; i++) + { + for (int j = 0; j < s - i; j++) + { + a[i][j] = a[i - 1][j + 1] - a[i - 1][j]; + } + } + for (int i = 0; i < c; i++) + { + for (int j = s - 1; j >= 0; j--) + { + a[j][s - j + i] = a[j + 1][s - j - 1 + i] + a[j][s - j - 1 + i]; + } + printf("%d%c", a[0][s + i], (i == c - 1 ? '\n' : ' ')); + } + } + return 0; +} diff --git a/1548.cpp b/1548.cpp new file mode 100644 index 0000000..f55d86d --- /dev/null +++ b/1548.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +const int N = 15; +const int M = 105; +int t, p, c, m, vis[M], have[M], hn, b[M], s[M], ans[N], num; +struct Mi +{ + int num[N]; + int vis[M]; + int b, w; +} mi[M]; + +void init() +{ + int i, j; + memset(vis, 0, sizeof(vis)); + memset(b, 0, sizeof(b)); + memset(s, 0, sizeof(s)); + hn = 0; + scanf("%d%d%d", &p, &c, &m); + for (i = 0; i < m; i++) + { + memset(mi[i].vis, 0, sizeof(mi[i].vis)); + for (j = 0; j < p; j++) + { + scanf("%d", &mi[i].num[j]); + mi[i].vis[mi[i].num[j]]++; + if (!vis[mi[i].num[j]]) + { + vis[mi[i].num[j]] = 1; + have[hn++] = mi[i].num[j]; + } + } + scanf("%d%d", &mi[i].b, &mi[i].w); + } + for (i = 1; i <= c; i++) + if (!vis[i]) + { + have[hn++] = i; + break; + } + sort(have, have + hn); +} + +bool dfs(int d) +{ + if (d == p) + { + for (int i = 0; i < m; i++) + { + if (b[i] != mi[i].b || s[i] - b[i] != mi[i].w) + { + return false; + } + } + for (int j = 0; j < p - 1; j++) + { + printf("%d ", ans[j]); + } + printf("%d\n", ans[p - 1]); + return true; + } + for (int i = 0; i < hn; i++) + { + int num = have[i], flag = 0, j, flag2[M]; + memset(flag2, 0, sizeof(flag2)); + for (j = 0; j < m; j++) + { + if (num == mi[j].num[d]) + { + b[j]++; + } + if (mi[j].vis[num]) + { + s[j]++; + mi[j].vis[num]--; + flag2[j] = 1; + } + if (b[j] > mi[j].b || s[j] > mi[j].b + mi[j].w) + { + flag = 1; + } + } + if (!flag) + { + ans[d] = num; + if (dfs(d + 1)) + { + return true; + } + } + for (j = 0; j < m; j++) + { + if (num == mi[j].num[d]) + { + b[j]--; + } + if (flag2[j]) + { + s[j]--; + mi[j].vis[num]++; + } + } + } + return false; +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + init(); + if (!dfs(0)) + { + printf("You are cheating!\n"); + } + } + return 0; +} diff --git a/1549.cpp b/1549.cpp new file mode 100644 index 0000000..69f882e --- /dev/null +++ b/1549.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +#define SZ(x) ((int)x.size()) +#define all(a) a.begin(), a.end() +#define allr(a) a.rbegin(), a.rend() +#define clrall(name, val) memset(name, (val), sizeof(name)); +#define EPS 10e-9 +#define ll long long +#define ull long long unsigned +#define SF scanf +#define PF printf +#define psb(b) push_back((b)) +#define ppb() pop_back() +#define oo (1 << 28) +#define mp make_pair +#define mt make_tuple +#define get(a, b) get(a) +#define fs first +#define sc second +#define rep(var, s, n, it) for (var = (s); var < (n); (var) += it) +#define rev(var, s, n, it) for (var = (n - 1); var > (s - 1); (var) -= it) +#define Read freopen("in.txt", "r", stdin) +#define Write freopen("out.txt", "w", stdout) +#define __ std::ios_base::sync_with_stdio(false) + +ll BigMod(ll B, ll P, ll M) +{ + ll R = 1;/// (B^P)%M + while (P > 0) + { + if (P % 2 == 1) + { + R = (R * B) % M; + } + P /= 2; + B = (B * B) % M; + } + return R; +} + +template +void deb(T1 e1) +{ + cout << e1 << endl; +} +template +void deb(T1 e1, T2 e2) +{ + cout << e1 << " " << e2 << endl; +} +template +void deb(T1 e1, T2 e2, T3 e3) +{ + cout << e1 << " " << e2 << " " << e3 << endl; +} +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4) +{ + cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; +} +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) +{ + cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; +} +template +void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) +{ + cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; +} + +//int dx[]= {-1,-1,0,0,1,1}; +//int dy[]= {-1,0,-1,1,0,1}; +//int dx[]= {0,0,1,-1};/*4 side move*/ +//int dy[]= {-1,1,0,0};/*4 side move*/ +//int dx[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/ +//int dy[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/ +//int dx[]={1,1,2,2,-1,-1,-2,-2};/*night move*/ +//int dy[]={2,-2,1,-1,2,-2,1,-1};/*night move*/ + +long D(long r) +{ + long x, y, count = 0; + for (x = -r; x <= r; x++) + for (y = -r; y <= r; y++) + if (x * x + y * y <= r * r) + { + count++; + } + return count; +} + +/** +* How many latice point in a circle of radius R +*/ + +int main() +{ +#ifdef MAHDI +// Read; +// Write; +#endif// MAHDI + ll n, res, n2, r; + int i, k = 0; + while (SF("%lld", &n) == 1) + { + k++; + assert(k <= 100); + res = 0; + n2 = n * n; + r = n; + for (ll i = 0; i <= n; i++) + { + while (r * r + i * i > n2) + { + r--; + } + res += r; + } + res *= 4; + res++; + PF("%lld\n", n); + PF("%lld\n", res); + } + return 0; +} diff --git a/155.cpp b/155.cpp new file mode 100644 index 0000000..24d1d8f --- /dev/null +++ b/155.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int inside, px, py; +struct point +{ + int x; + int y; +}; +struct square +{ + point p1; + point p2; + point p3; + point p4; +}; + +void func(int k, int x, int y) +{ + square mySqr; + mySqr.p1.x = x - k; + mySqr.p1.y = y - k; + mySqr.p2.x = x + k; + mySqr.p2.y = y - k; + mySqr.p3.x = x + k; + mySqr.p3.y = y + k; + mySqr.p4.x = x - k; + mySqr.p4.y = y + k; + if ((px >= mySqr.p1.x && px <= mySqr.p2.x) && (py >= mySqr.p2.y && py <= mySqr.p3.y)) + { + inside++; + } + if (k > 1) + { + func(k / 2, mySqr.p1.x, mySqr.p1.y); + func(k / 2, mySqr.p2.x, mySqr.p2.y); + func(k / 2, mySqr.p3.x, mySqr.p3.y); + func(k / 2, mySqr.p4.x, mySqr.p4.y); + } +} + +int main() +{ + int k; + while (1) + { + scanf("%d%d%d", &k, &px, &py); + if (k == 0 && px == 0 && py == 0) + { + break; + } + inside = 0; + func(k, 1024, 1024); + printf("%3d\n", inside); + } + return 0; +} diff --git a/1554.cpp b/1554.cpp new file mode 100644 index 0000000..df7f8e4 --- /dev/null +++ b/1554.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +const int MAXN = 10000 + 10; + +bool C[MAXN]; +int ret[MAXN][2]; + +void BinarySearch(int N, int L, int x) +{ + int p = 0, q = N - 1, times = 0; + while (p <= q) + { + int mid = (p + q) / 2; + times++; + if (mid == x) + { + if (L == times) + C[N] = 1; + return; + } + if (x < mid) + q = mid - 1; + else + p = mid + 1; + } +} + +int main() +{ + int N, L; + while (scanf("%d%d", &N, &L) == 2) + { + memset(C, 0, sizeof(C)); + for (int i = N; i < MAXN; ++i) + BinarySearch(i, L, N); + int tot = 0; + for (int i = 0, j; i <= 10000; i = j) + { + if (C[i]) + { + for (j = i; j <= 10000 && C[j]; ++j) + ; + ret[tot][0] = i; + ret[tot][1] = j - 1; + tot++; + } + else + j = i + 1; + } + printf("%d\n", tot); + for (int i = 0; i < tot; ++i) + printf("%d %d\n", ret[i][0], ret[i][1]); + } + return 0; +} diff --git a/1555.cpp b/1555.cpp new file mode 100644 index 0000000..fc5ed42 --- /dev/null +++ b/1555.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +typedef long long ll; +const int N = 1e3 + 10; +double eps = 1e-8; +int n; +double A, B, H[N]; +bool judge(double h2) +{ + H[2] = h2; + for (int i = 3; i <= n; i++) + { + H[i] = H[i - 1] * 2 + 2 - H[i - 2]; + if (H[i] < 0) + { + return false; + } + } + B = H[n]; + return true; +} +int main() +{ + while (scanf("%d%lf", &n, &A) != EOF) + { + H[1] = A; + double L = 0, R = A; + while (R - L > eps) + { + double M = (L + R) / 2; + if (judge(M)) + { + R = M; + } + else + { + L = M; + } + } + printf("%.2lf\n", B); + } + return 0; +} diff --git a/1556.cpp b/1556.cpp new file mode 100644 index 0000000..29de4f7 --- /dev/null +++ b/1556.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +const int maxn = 50005; +typedef map::iterator iter; + +struct Tire +{ + int sz; + map g[maxn]; + + void init(); + void insert(string s); + void put(int u, int d); +} tree; + +int main() +{ + int n; + string s; + while (cin >> n && n) + { + tree.init(); + for (int i = 0; i < n; i++) + { + cin >> s; + s += '\\'; + tree.insert(s); + } + tree.put(0, 0); + cout << endl; + } + return 0; +} + +void Tire::init() +{ + sz = 1; + g[0].clear(); +} + +void Tire::insert(string s) +{ + + int u = 0; + string word = ""; + + for (int i = 0; i < s.length(); i++) + { + if (s[i] == '\\') + { + + if (!g[u].count(word)) + { + g[sz].clear(); + g[u][word] = sz++; + } + + u = g[u][word]; + word = ""; + } + else + word += s[i]; + } +} + +void Tire::put(int u, int d) +{ + + for (iter i = g[u].begin(); i != g[u].end(); i++) + { + for (int j = 0; j < d; j++) + cout << " "; + cout << i->first << endl; + put(i->second, d + 1); + } +} diff --git a/1557.cpp b/1557.cpp new file mode 100644 index 0000000..0d53542 --- /dev/null +++ b/1557.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +#define LL long long +int main() +{ + int n, x, y, z; + scanf("%d", &n); + while (n--) + { + scanf("%d%d%d", &x, &y, &z); + if ((y == 9 || y == 11) && z == 30) + { + puts("YES"); + continue; + } + y &= 1; + z &= 1; + if (y == z) + puts("YES"); + else + puts("NO"); + } + return 0; +} diff --git a/1558.cpp b/1558.cpp new file mode 100644 index 0000000..435a661 --- /dev/null +++ b/1558.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int sg[1 << 20], a[22]; +int get_solve(int state, int x) +{ + for (int i = x; i <= 20; i += x) + state &= ~(1 << (i - 2)); + for (int i = 2; i <= 20; i++) + if (state & (1 << (i - 2))) + { + for (int j = x; i - j >= 2; j += x) + if (!(state & (1 << (i - j - 2)))) + { + state ^= 1 << (i - 2); + break; + } + } + return state; +} +int dfs(int state) +{ + if (sg[state] != -1) + return sg[state]; + for (int i = 2; i <= 20; i++) + if (state & (1 << (i - 2))) + if (dfs(get_solve(state, i)) == 0) + return sg[state] = 1; + return sg[state] = 0; +} +int main() +{ + int t, n, i, j, k; + memset(sg, -1, sizeof(sg)); + scanf("%d", &t); + for (k = 1; k <= t; k++) + { + scanf("%d", &n); + + int state = 0; + for (i = 1; i <= n; i++) + scanf("%d", &a[i]), state |= 1 << (a[i] - 2); + printf("Scenario #%d:\n", k); + if (dfs(state) == 0) + puts("There is no winning move."); + else + { + printf("The winning moves are:"); + for (i = 2; i <= 20; i++) + if (state & (1 << (i - 2))) + if (dfs(get_solve(state, i)) == 0) + printf(" %d", i); + puts("."); + } + puts(""); + } + return 0; +} diff --git a/1559.cpp b/1559.cpp new file mode 100644 index 0000000..100ddb9 --- /dev/null +++ b/1559.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +#define LL long long +int m[33], n, S, win[21][11111]; +int dfs(int cur, int t) +{ + if (cur == 1) + return -1; + if (t > 2 * n) + t = 1; + + if (win[t][cur]) + return win[t][cur]; + if (cur <= m[t] + 1) + return 1; + + for (int i = 1; i <= m[t]; i++) + if (dfs(cur - i, t + 1) < 0) + return win[t][cur] = 1; + return win[t][cur] = -1; +} +void init() +{ + for (int i = 1; i <= 2 * n; i++) + for (int j = 1; j <= S; j++) + win[i][j] = 0; +} +int main() +{ + int i, j; + while (scanf("%d", &n) != -1 && n) + { + scanf("%d", &S); + for (i = 1; i <= 2 * n; i++) + scanf("%d", &m[i]); + init(); + if (dfs(S, 1) > 0) + puts("1"); + else + puts("0"); + } + return 0; +} diff --git a/156.cpp b/156.cpp index 9e82b2e..9af38e6 100644 --- a/156.cpp +++ b/156.cpp @@ -1,68 +1,72 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -string mod(string A){ - int L = A.length(), i; - string B = ""; - FOI(i, 0, L-1) - B += tolower(A[i]); - - sort(B.begin(), B.end()); - return B; +bool theTruthIsOutThere(pair a, pair b) +{ + if (a.second < b.second) + { + return true; + } + else + { + return false; + } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - map Map; - map::iterator mit; - map Cnt; - map::iterator cit; - vector V; - int i, j; - while (true){ - string A, B; - cin >> A; - if (A == "#") +int main() +{ + string input; + vector> words; + vector result; + while (getline(cin, input)) + { + if (input == "#") + { break; - B = mod(A); - Map[A] = B; - ++Cnt[B]; + } + stringstream ss; + ss << input; + while (ss >> input) + { + string temp(input); + for (int i = 0; i < temp.size(); i++) + if (temp[i] >= 'A' && temp[i] <= 'Z') + { + temp[i] = tolower(temp[i]); + } + sort(temp.begin(), temp.end()); + words.push_back(pair(input, temp)); + } } - for (mit = Map.begin(); mit != Map.end(); mit++){ - if (Cnt[(*mit).second] <= 1) - V.push_back((*mit).first); + sort(words.begin(), words.end(), theTruthIsOutThere); + bool bad = false; + for (int i = 0; i < words.size() - 1; i++) + { + if (words[i].second != words[i + 1].second) + { + if (bad) + { + bad = false; + } + else + { + result.push_back(words[i].first); + } + } + else + { + bad = true; + } + } + if (!bad) + { + result.push_back(words[words.size() - 1].first); + } + sort(result.begin(), result.end()); + for (int i = 0; i < result.size(); i++) + { + cout << result[i] << endl; } - sort(V.begin(), V.end()); - FOI(i, 0, V.size() - 1) - cout << V[i] << endl; return 0; } - diff --git a/1560.cpp b/1560.cpp new file mode 100644 index 0000000..155dfee --- /dev/null +++ b/1560.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +int g[10][10]; +int now[10][10], times[10][10]; + +bool dfs(int i, int j) +{ + if (j == 7) + return dfs(i + 1, 1); + if (i == 6 && j == 1) + { + bool ok = true; + for (int k = 1; k <= 6; k++) + if ((g[5][k] + times[5][k]) % 2) + { + ok = false; + break; + } + return ok; + } + for (int k = 0; k < 2; k++) + { + now[i][j] = k; + if (k) + { + times[i][j]++; + times[i - 1][j]++; + times[i + 1][j]++; + times[i][j - 1]++; + times[i][j + 1]++; + } + if (!(i != 1 && (times[i - 1][j] + g[i - 1][j]) % 2 != 0)) + if (dfs(i, j + 1)) + return true;// decide g[i-1][j] is legal? + if (k) + { + times[i][j]--; + times[i - 1][j]--; + times[i + 1][j]--; + times[i][j - 1]--; + times[i][j + 1]--; + } + } + return false; +} + +int main() +{ + int T; + cin >> T; + for (int t = 1; t <= T; t++) + { + for (int i = 1; i <= 5; i++) + { + for (int j = 1; j <= 6; j++) + { + cin >> g[i][j]; + } + } + memset(times, 0, sizeof(times)); + dfs(1, 1); + cout << "PUZZLE #" << t << endl; + for (int i = 1; i <= 5; i++) + { + for (int j = 1; j <= 6; j++) + { + cout << now[i][j] << (j == 6 ? "\n" : " "); + } + } + } + return 0; +} diff --git a/1564.cpp b/1564.cpp new file mode 100644 index 0000000..51039d1 --- /dev/null +++ b/1564.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +const int N = 305; +char week[7][5] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}; + +int n, m, k, A[N][N], cnt[N]; +char day1[5], day2[5]; + +int find(char *day) +{ + for (int i = 0; i < 7; i++) + if (strcmp(week[i], day) == 0) + { + return i; + } +} + +int inv(int x) +{ + int ans = 1; + for (int i = 0; i < 5; i++) + { + ans = ans * x % 7; + } + return ans; +} + +void build() +{ + for (int i = 0; i < m; i++) + { + scanf("%d%s%s", &k, day1, day2); + A[i][n] = (find(day2) - find(day1) + 8) % 7; + int tmp; + memset(cnt, 0, sizeof(cnt)); + while (k--) + { + scanf("%d", &tmp); + cnt[tmp]++; + } + for (int j = 1; j <= n; j++) + { + A[i][j - 1] = cnt[j] % 7; + } + } +} + +int gauss() +{ + int i = 0, j = 0; + while (i < m && j < n) + { + int r; + for (r = i; r < m; r++) + if (A[r][j]) + { + break; + } + if (r == m) + { + j++; + continue; + } + for (int k = j; k <= n; k++) + { + swap(A[r][k], A[i][k]); + } + for (int k = 0; k < m; k++) + { + if (i == k) + { + continue; + } + if (A[k][j]) + { + int tmp = A[k][j] * inv(A[i][j]) % 7; + for (int x = j; x <= n; x++) + { + A[k][x] = ((A[k][x] - tmp * A[i][x]) % 7 + 7) % 7; + } + } + } + i++; + } + for (int k = i; k < m; k++) + if (A[k][n]) + { + return 2; + } + if (i < n) + { + return 1; + } + for (int i = 0; i < n; i++) + { + int ans = A[i][n] * inv(A[i][i]) % 7; + if (ans < 3) + { + ans += 7; + } + printf("%d%c", ans, i == n - 1 ? '\n' : ' '); + } + return 0; +} + +void solve() +{ + int tmp = gauss(); + if (tmp == 1) + { + printf("Multiple solutions.\n"); + } + else if (tmp == 2) + { + printf("Inconsistent data.\n"); + } +} + +int main() +{ + while (~scanf("%d%d", &n, &m) && n) + { + build(); + solve(); + } + return 0; +} diff --git a/1566.cpp b/1566.cpp new file mode 100644 index 0000000..5364d6e --- /dev/null +++ b/1566.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int t, n; + +bool solve() +{ + scanf("%d", &n); + int x, sum = 0, flag = 1; + for (int i = 0; i < n; i++) + { + scanf("%d", &x); + if (x > 1) + { + flag = 0; + } + sum ^= x; + } + if (flag) + { + return n % 2 == 0; + } + else + { + return sum != 0; + } +} + +int main() +{ + scanf("%d", &t); + while (t--) + { + if (solve()) + { + printf("John\n"); + } + else + { + printf("Brother\n"); + } + } + return 0; +} diff --git a/1569.cpp b/1569.cpp new file mode 100644 index 0000000..ec70384 --- /dev/null +++ b/1569.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +#define pb push_back +#define mp make_pair +#define eps 1e-9 +#define pi acos(-1.0) +#define ff first +#define ss second +#define re return +#define QI queue +#define SI stack +#define SZ(x) ((int)(x).size()) +#define all(x) (x).begin(), (x).end() +#define sq(a) ((a) * (a)) +#define distance(a, b) (sq(a.x - b.x) + sq(a.y - b.y)) +#define iseq(a, b) (fabs(a - b) < eps) +#define eq(a, b) iseq(a, b) +#define ms(a, b) memset((a), (b), sizeof(a)) +#define G() getchar() +#define MAX3(a, b, c) max(a, max(b, c)) +#define II ({ int a ; read(a) ; a; }) +#define LL ({ Long a ; read(a) ; a; }) +#define DD ({double a; scanf("%lf", &a); a; }) + +double const EPS = 3e-8; + +typedef long long Long; +typedef long long int64; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +// For loop + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +template +T gcd(T a, T b) +{ + return (b != 0 ? gcd(b, a % b) : a); +} +template +T lcm(T a, T b) { return (a / gcd(a, b) * b); } + +//Fast Reader +template +inline bool read(T &x) +{ + int c = getchar(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = getchar(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +//int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction +//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction +//int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction +//int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; //Hexagonal Direction + +/* ************************************** My code start here ****************************************** */ + +const int NX = 5005; + +int ans[NX], from[NX], Q[NX], vis[NX], n, m, inp[NX]; + +void print(int u) +{ + if (u == -1) + return; + print(from[u]); + printf("%d", ans[u]); +} +int main() +{ + // I will always use scanf and printf + // May be i won't be a good programmer but i will be a good human being + while (scanf("%d", &n) == 1) + { + m = II; + rep(i, m) inp[i] = II; + if (!n) + { + printf("0\n"); + continue; + } + // printf("here\n"); + sort(inp, inp + m); + ms(vis, 0); + int head = 0, rear = 0; + rep(i, m) + { + if (inp[i] == 0) + continue; + int tmp = inp[i] % n; + ans[rear] = inp[i]; + from[rear] = -1; + Q[rear++] = tmp; + vis[tmp] = 1; + } + bool flag = 1; + while (head < rear) + { + int u = Q[head]; + // printf(" u :: %d\n" , u ); + if (u == 0) + { + print(head); + flag = 0; + printf("\n"); + break; + } + rep(i, m) + { + int tmp = (u * 10 + inp[i]) % n; + if (vis[tmp] == 0) + { + vis[tmp] = 1; + Q[rear] = tmp; + from[rear] = head; + ans[rear++] = inp[i]; + } + } + head++; + } + if (flag) + printf("0\n"); + } + return 0; +} diff --git a/157.cpp b/157.cpp new file mode 100644 index 0000000..38c061c --- /dev/null +++ b/157.cpp @@ -0,0 +1,236 @@ +#include + +using namespace std; + +const int MAX_ALPHA = 'z' - 'a' + 1; + +struct Station +{ + int _line; // 'A' - 'Z' + int _index;// 'a' - 'z' + +public: + Station() + : _line(0) + { + } + Station(int _Line, int _Index) + : _line(_Line), _index(_Index) + { + // ASSERT('A' <= _line && _line <= 'Z'); + // ASSERT('a' <= _index && _index <= 'z'); + } + bool valid() const + { + return _line > 0; + } + int line() const + { + return _line; + } + int index() const + { + return _index; + } + int code() const + { + return (_line - 'A') * MAX_ALPHA + (_index - 'a'); + } + bool operator==(const Station &_Rhs) const + { + return code() == _Rhs.code(); + } + bool operator<(const Station &_Rhs) const + { + return code() < _Rhs.code(); + } +}; + +int main() +{ + int i, lines; + typedef set Connection; + map con; + Connection::iterator it_con1, it_con2; + scanf("%d", &lines); + for (i = 0; i < lines; ++i) + { + int line; + do + { + line = getchar(); + } while (line < 'A' || 'Z' < line); + const int colon = getchar(); + // ASSERT(colon == ':'); + Station st0; + while (true) + { + const int tmp = getchar(); + if ('a' <= tmp && tmp <= 'z') + { + const Station st(line, tmp); + if (st0.valid()) + { + con[st].insert(st0); + con[st0].insert(st); + } + st0 = st; + } + else if (tmp == '=') + { + // ASSERT(st0.valid()); + const int c_line = getchar(); + // ASSERT('A' <= c_line && c_line <= 'Z'); + const int c_index = getchar(); + // ASSERT('a' <= c_index && c_index <= 'z'); + const Station st(c_line, c_index); + Connection &con_st = con[st], con_st0 = con[st0]; + con_st.insert(st0); + for (it_con2 = con_st0.begin(); + it_con2 != con_st0.end(); ++it_con2) + { + if (it_con2->line() != st0.line()) + { + con_st.insert(*it_con2); + } + } + for (it_con1 = con_st.begin(); + it_con1 != con_st.end(); ++it_con1) + { + if (it_con1->line() != st.line()) + { + con[*it_con1].insert(st0); + for (it_con2 = con_st0.begin(); + it_con2 != con_st0.end(); ++it_con2) + { + if (it_con2->line() != st0.line()) + { + con[*it_con1].insert(*it_con2); + } + } + } + } + con_st0.insert(st); + for (it_con2 = con_st.begin(); + it_con2 != con_st.end(); ++it_con2) + { + if (it_con2->line() != st.line()) + { + con_st0.insert(*it_con2); + } + } + for (it_con1 = con_st0.begin(); + it_con1 != con_st0.end(); ++it_con1) + { + if (it_con1->line() != st0.line()) + { + con[*it_con1].insert(st); + for (it_con2 = con_st.begin(); + it_con2 != con_st.end(); ++it_con2) + { + if (it_con2->line() != st.line()) + { + con[*it_con1].insert(*it_con2); + } + } + } + } + } + else + { + break; + } + } + } + typedef vector Route; + typedef pair RouteAndDist; + map> routes; + map>::iterator it_st0, it_st1, it_st2; + for (it_st1 = con.begin(); it_st1 != con.end(); ++it_st1) + { + const Station st1 = it_st1->first; + for (it_st2 = con.begin(); it_st2 != con.end(); ++it_st2) + { + const Station st2 = it_st2->first; + if (st1 == st2) + { + continue; + } + if (con[st1].count(st2)) + { + Route route; + route.push_back(st1); + route.push_back(st2); + const int dist = st1.line() == st2.line() ? 1 : 3; + routes[st1][st2] = RouteAndDist(dist, route); + reverse(route.begin(), route.end()); + routes[st2][st1] = RouteAndDist(dist, route); + } + } + } + for (it_st0 = con.begin(); it_st0 != con.end(); ++it_st0) + { + const Station st0 = it_st0->first; + for (it_st1 = con.begin(); it_st1 != con.end(); ++it_st1) + { + const Station st1 = it_st1->first; + if (st0 == st1) + { + continue; + } + for (it_st2 = con.begin(); it_st2 != con.end(); ++it_st2) + { + const Station st2 = it_st2->first; + if (st0 == st2 || st1 == st2) + { + continue; + } + const RouteAndDist &rad1 = routes[st1][st0]; + const RouteAndDist &rad2 = routes[st0][st2]; + if (rad1.second.size() > 0 && rad2.second.size() > 0) + { + const int dist = rad1.first + rad2.first; + RouteAndDist &rad0 = routes[st1][st2]; + if (rad0.second.size() == 0 || rad0.second.size() > dist) + { + rad0.first = dist; + rad0.second.clear(); + rad0.second.insert(rad0.second.end(), + rad1.second.begin(), rad1.second.end()); + rad0.second.pop_back(); + rad0.second.insert(rad0.second.end(), + rad2.second.begin(), rad2.second.end()); + } + } + } + } + } + char query[5]; + while (scanf("%s", query) == 1) + { + if (query[0] == '#') + { + break; + } + const Station st1(query[0], query[1]); + const Station st2(query[2], query[3]); + // ASSERT(!(st1 == st2)); + const RouteAndDist &ans = routes[st1][st2]; + // ASSERT(ans.second.size() > 0); + printf("%3d: ", ans.first); + for (i = 0; i < ans.second.size(); ++i) + { + if (i == 0 || ans.second[i].line() != ans.second[i - 1].line()) + { + if (i != 0) + { + printf("="); + } + printf("%c", ans.second[i].line()); + } + printf("%c", ans.second[i].index()); + } + printf("\n"); + } + return 0; +} diff --git a/1571.cpp b/1571.cpp new file mode 100644 index 0000000..28030e5 --- /dev/null +++ b/1571.cpp @@ -0,0 +1,243 @@ +#include + +using namespace std; + +#define EPS 1e-9 +#define PI acos(-1.0) + +double DEG_to_RAD(double d) +{ + return d * PI / 180.0; +} + +double RAD_to_DEG(double r) { return r * 180.0 / PI; } + +struct point +{ + double x, y; // only used if more precision is needed + point() { x = y = 0.0; } // default constructor + point(double _x, double _y) : x(_x), y(_y) {} // user-defined + bool operator==(point other) const + { + return (fabs(x - other.x) < EPS && (fabs(y - other.y) < EPS)); + } +}; + +struct vec +{ + double x, y; // name: `vec' is different from STL vector + vec(double _x, double _y) : x(_x), y(_y) {} +}; + +vec toVec(point a, point b) +{ // convert 2 points to vector a->b + return vec(b.x - a.x, b.y - a.y); +} + +double dist(point p1, point p2) +{ // Euclidean distance + return hypot(p1.x - p2.x, p1.y - p2.y); +} // return double + +// returns the perimeter, which is the sum of Euclidian distances +// of consecutive line segments (polygon edges) +double perimeter(const vector &P) +{ + double result = 0.0; + for (int i = 0; i < (int)P.size() - 1; i++) // remember that P[0] = P[n-1] + { + result += dist(P[i], P[i + 1]); + } + return result; +} + +// returns the area, which is half the determinant +double area(const vector &P) +{ + double result = 0.0, x1, y1, x2, y2; + for (int i = 0; i < (int)P.size() - 1; i++) + { + x1 = P[i].x; + x2 = P[i + 1].x; + y1 = P[i].y; + y2 = P[i + 1].y; + result += (x1 * y2 - x2 * y1); + } + return fabs(result) / 2.0; +} + +double dot(vec a, vec b) { return (a.x * b.x + a.y * b.y); } + +double norm_sq(vec v) { return v.x * v.x + v.y * v.y; } + +double angle(point a, point o, point b) +{ // returns angle aob in rad + vec oa = toVec(o, a), ob = toVec(o, b); + return acos(dot(oa, ob) / sqrt(norm_sq(oa) * norm_sq(ob))); +} + +double cross(vec a, vec b) { return a.x * b.y - a.y * b.x; } + +// note: to accept collinear points, we have to change the `> 0' +// returns true if point r is on the left side of line pq +bool ccw(point p, point q, point r) +{ + return cross(toVec(p, q), toVec(p, r)) > 0; +} + +// returns true if point r is on the same line as the line pq +bool collinear(point p, point q, point r) +{ + return fabs(cross(toVec(p, q), toVec(p, r))) < EPS; +} + +// returns true if we always make the same turn while examining +// all the edges of the polygon one by one +bool isConvex(const vector &P) +{ + int sz = (int)P.size(); + if (sz <= 3) + return false; // a point/sz=2 or a line/sz=3 is not convex + bool isLeft = ccw(P[0], P[1], P[2]); // remember one result + for (int i = 1; i < sz - 1; i++) // then compare with the others + if (ccw(P[i], P[i + 1], P[(i + 2) == sz ? 1 : i + 2]) != isLeft) + return false; // different sign -> this polygon is concave + return true; +} // this polygon is convex + +// returns true if point p is in either convex/concave polygon P +bool inPolygon(point pt, const vector &P) +{ + if ((int)P.size() == 0) + return false; + double sum = 0; // assume the first vertex is equal to the last vertex + for (int i = 0; i < (int)P.size() - 1; i++) + { + if (ccw(pt, P[i], P[i + 1])) + sum += angle(P[i], pt, P[i + 1]); // left turn/ccw + else + sum -= angle(P[i], pt, P[i + 1]); + } // right turn/cw + return fabs(fabs(sum) - 2 * PI) < EPS; +} + +// line segment p-q intersect with line A-B. +point lineIntersectSeg(point p, point q, point A, point B) +{ + double a = B.y - A.y; + double b = A.x - B.x; + double c = B.x * A.y - A.x * B.y; + double u = fabs(a * p.x + b * p.y + c); + double v = fabs(a * q.x + b * q.y + c); + return point((p.x * v + q.x * u) / (u + v), (p.y * v + q.y * u) / (u + v)); +} + +// cuts polygon Q along the line formed by point a -> point b +// note: the last point must be the same as the first point +vector cutPolygon(point a, point b, const vector &Q) +{ + vector P; + for (int i = 0; i < (int)Q.size(); i++) + { + double left1 = cross(toVec(a, b), toVec(a, Q[i])), left2 = 0; + if (i != (int)Q.size() - 1) + left2 = cross(toVec(a, b), toVec(a, Q[i + 1])); + if (left1 > -EPS) + P.push_back(Q[i]); // Q[i] is on the left of ab + if (left1 * left2 < -EPS) // edge (Q[i], Q[i+1]) crosses line ab + P.push_back(lineIntersectSeg(Q[i], Q[i + 1], a, b)); + } + if (!P.empty() && !(P.back() == P.front())) + P.push_back(P.front()); // make P's first point = P's last point + return P; +} + +point pivot; +bool angleCmp(point a, point b) +{ // angle-sorting function + if (collinear(pivot, a, b)) // special case + return dist(pivot, a) < dist(pivot, b); // check which one is closer + double d1x = a.x - pivot.x, d1y = a.y - pivot.y; + double d2x = b.x - pivot.x, d2y = b.y - pivot.y; + return (atan2(d1y, d1x) - atan2(d2y, d2x)) < 0; +} // compare two angles + +vector CH(vector P) +{ // the content of P may be reshuffled + int i, j, n = (int)P.size(); + if (n <= 3) + { + if (!(P[0] == P[n - 1])) + P.push_back(P[0]); // safeguard from corner case + return P; // special case, the CH is P itself + } + + // first, find P0 = point with lowest Y and if tie: rightmost X + int P0 = 0; + for (i = 1; i < n; i++) + if (P[i].y < P[P0].y || (P[i].y == P[P0].y && P[i].x > P[P0].x)) + P0 = i; + + point temp = P[0]; + P[0] = P[P0]; + P[P0] = temp;// swap P[P0] with P[0] + + // second, sort points by angle w.r.t. pivot P0 + pivot = P[0]; // use this global variable as reference + sort(++P.begin(), P.end(), angleCmp); // we do not sort P[0] + + // third, the ccw tests + vector S; + S.push_back(P[n - 1]); + S.push_back(P[0]); + S.push_back(P[1]);// initial S + i = 2; // then, we check the rest + while (i < n) + {// note: N must be >= 3 for this method to work + j = (int)S.size() - 1; + if (ccw(S[j - 1], S[j], P[i])) + S.push_back(P[i++]);// left turn, accept + else + S.pop_back(); + } // or pop the top of S until we have a left turn + return S; +} // return the result + +int main() +{ + int n; + scanf("%d", &n); + while (n != 0) + { + vector p; + for (int i = 0; i < n; i++) + { + int x, y; + scanf("%d %d", &x, &y); + p.push_back(point(x, y)); + } + p.push_back(p[0]); + bool valid = true; + int sz = p.size(); + vector pcopy; + pcopy = p; + for (int i = 0; i < sz - 1; i++) + { + pcopy = cutPolygon(p[i], p[i + 1], pcopy); + } + if (area(pcopy) < EPS) + { + valid = false; + } + if (valid) + { + printf("1\n"); + } + else + { + printf("0\n"); + } + scanf("%d", &n); + } + return 0; +} diff --git a/1572.cpp b/1572.cpp new file mode 100644 index 0000000..ee69c7b --- /dev/null +++ b/1572.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int g[64][64]; +int node(char a, char b) +{ + return (a - 'A') + (b == '+' ? 0 : 26); +} +int rnode(char a, char b) +{ + return (a - 'A') + (b == '+' ? 26 : 0); +} +int exist_cycle; +int main() +{ + int n; + char s[50]; + while (scanf("%d", &n) == 1) + { + memset(g, 0, sizeof(g)); + for (int i = 0; i < n; i++) + { + scanf("%s", s); + for (int j = 0; j < 4; j++) + { + for (int k = 0; k < 4; k++) + { + if (j == k || s[2 * j] == '0' || s[2 * k] == '0') + continue; + int x = node(s[2 * j], s[2 * j + 1]); + int y = rnode(s[2 * k], s[2 * k + 1]); + g[x][y] = 1; + } + } + } + exist_cycle = 0; + n = 52; + for (int k = 0; k < n; k++) + { + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + g[i][j] |= g[i][k] & g[k][j]; + } + for (int i = 0; i < n; i++) + exist_cycle |= g[i][i]; + puts(exist_cycle ? "unbounded" : "bounded"); + } + return 0; +} +/* +3 +A+00A+A+ 00B+D+A- B-C+00C+ +1 +K+K-Q+Q- +*/ diff --git a/1574.cpp b/1574.cpp new file mode 100644 index 0000000..8b431c0 --- /dev/null +++ b/1574.cpp @@ -0,0 +1,207 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; +class MaxFlow +{ +public: + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + void Init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + adj[i] = NULL; + } + void Addedge(int x, int y, int flow) + { + edge[e].v = y, edge[e].cap = flow, edge[e].next = adj[x]; + edge[e].re = &edge[e + 1], adj[x] = &edge[e++]; + edge[e].v = x, edge[e].cap = 0, edge[e].next = adj[y]; + edge[e].re = &edge[e - 1], adj[y] = &edge[e++]; + } + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + level[i] = n, lvCnt[i] = 0; + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + ++dis, r = rear; + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + level[t] = dis, Q[rear++] = t, ++lvCnt[dis]; + } + } + } + int Maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + pre[i] = NULL, arc[i] = adj[i]; + for (i = 0; i < e; ++i) + edge[i].flow = edge[i].cap; + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + arc[i] = arc[i]->next; + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + if (update > p->flow) + update = p->flow; + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + p->flow -= update, p->re->flow += update; + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + if (p->flow && depth > level[p->v]) + depth = level[p->v]; + if (--lvCnt[level[i]] == 0) + return ret; + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + i = pre[i]->re->v; + } + } + return ret; + } +} maxflow; + +vector> g[MAXV]; +int dist[MAXV], inq[MAXV]; +void spfa(int st, int n) +{ + queue Q; + int u, v; + + for (int i = 0; i <= n; i++) + dist[i] = 0x3f3f3f3f, inq[i] = 0; + Q.push(st), dist[st] = 0; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + inq[u] = 0; + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i].first; + if (dist[v] > dist[u] + g[u][i].second) + { + dist[v] = dist[u] + g[u][i].second; + if (!inq[v]) + inq[v] = 0, Q.push(v); + } + } + } +} + +const int MAXM = 131072; +int X[MAXM], Y[MAXM], T[MAXM], C[MAXM]; +int main() +{ + int n, m, c; + while (scanf("%d %d %d", &n, &m, &c) == 3) + { + for (int i = 1; i <= n; i++) + g[i].clear(); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &X[i], &Y[i], &T[i]); + g[X[i]].push_back(make_pair(Y[i], T[i])); + g[Y[i]].push_back(make_pair(X[i], T[i])); + } + + for (int i = 0; i < c; i++) + scanf("%d", &C[i]); + + spfa(1, n); + + // shortest path as DAG + int m_cnt = 0; + for (int i = 0; i < m; i++) + { + int u = X[i], v = Y[i]; + if (dist[u] == dist[v] + T[i] || dist[v] == dist[u] + T[i]) + { + X[m_cnt] = X[i], Y[m_cnt] = Y[i], T[m_cnt] = T[i]; + m_cnt++; + } + } + m = m_cnt;// #remain edge + + vector> D; + for (int i = 0; i < c; i++) + D.push_back(make_pair(dist[C[i]], C[i])); + sort(D.begin(), D.end()); + + // only commuters which are the same distance from position to downtown occur collision. + int ret = 0; + for (int i = 0; i < c; i++) + { + maxflow.Init(n + 2); + + int source = n + 1, sink = 1; + int j = i; + while (j < c && D[j].first == D[i].first) + maxflow.Addedge(source, D[j].second, 1), j++; + i = j - 1; + + for (int j = 0; j < m; j++) + { + int u = X[j], v = Y[j]; + if (dist[u] == dist[v] + T[j]) + maxflow.Addedge(u, v, 1); + if (dist[v] == dist[u] + T[j]) + maxflow.Addedge(v, u, 1); + } + + int t = maxflow.Maxflow(source, sink); + ret += t; + } + printf("%d\n", ret); + } + return 0; +} +/* +3 3 2 +1 2 42 +2 3 1 +2 3 1 +2 3 + +4 4 5 +1 2 5 +1 3 4 +4 2 5 +4 3 6 +4 4 4 4 1 +*/ diff --git a/1575.cpp b/1575.cpp new file mode 100644 index 0000000..d61db50 --- /dev/null +++ b/1575.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +const long long MAX_INT64 = ~0ULL >> 1; +int path[64]; +long long P[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 51, 53}; +long long C[64][64] = {}; +map F; +void build(int idx, long long n, long long limit) +{ + long long ways = 1, items = 0; + for (int i = 0; i < idx; i++) + items += path[i]; + for (int i = 0; i < idx; i++) + { + long long t = C[items][path[i]]; + if (MAX_INT64 / ways < t) + {// ways * t > MAX_INT64 + ways = -1; + break; + } + ways *= t, items -= path[i]; + } + if (ways != -1) + { + long long &ret = F[ways]; + if (ret == 0) + ret = n; + else + ret = min(ret, n); + } + + for (int i = 1;; i++) + { + limit /= P[idx]; + if (limit == 0) + break; + path[idx] = i, n *= P[idx]; + build(idx + 1, n, limit); + } +} +int main() +{ + C[0][0] = 1; + for (int i = 1; i < 64; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; + } + build(0, 1, MAX_INT64); + F[1] = 2; + long long n; + while (scanf("%lld", &n) == 1) + { + printf("%lld %lld\n", n, F[n]); + } + return 0; +} +/* + 1 + 2 + 3 + 105 + */ \ No newline at end of file diff --git a/1577.cpp b/1577.cpp new file mode 100644 index 0000000..dcd8f5a --- /dev/null +++ b/1577.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int A[1048576]; +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + int N = 2 * n * m; + for (int i = 0; i < N; i++) + scanf("%d", &A[i]); + sort(A, A + N); + int l = A[1] - A[0], r = A[N - 1] - A[0], mid; + int ret = l; + while (l <= r) + { + mid = (l + r) / 2; + int cnt = 0, used = 0; + for (int i = 0; i < N - 1; i++) + { + if (A[i + 1] - A[i] <= mid) + { + cnt++, i++, used += 2; + } + else + { + used++; + } + if (used > cnt * 2 * m || cnt == n) + break; + } + if (cnt == n) + ret = mid, r = mid - 1; + else + l = mid + 1; + } + printf("%d\n", ret); + } + return 0; +} +/* +2 3 +1 2 3 4 5 6 7 8 9 10 11 12 +2 2 +3 1 3 3 3 3 3 3 +*/ diff --git a/1579.cpp b/1579.cpp new file mode 100644 index 0000000..7848229 --- /dev/null +++ b/1579.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define MAXN 512 +int A[MAXN]; +int dp[MAXN][MAXN] = {}, complete[MAXN][MAXN]; +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + scanf("%d", &A[i]); + + for (int i = 1; i < n; i++) + {// build cost table. + for (int j = 0; i + j < n; j++) + { + int l = j, r = i + j; + int &val = dp[l][r]; + val = 0x3f3f3f3f; + for (int k = l; k < r; k++) + { + int open = 0;// [l, r] = L[l, k] + R[k+1, r] + int minL = 0x3f3f3f3f, minR = 0x3f3f3f3f; + for (int p = l; p <= k; p++) + minL = min(minL, A[p]); + for (int p = k + 1; p <= r; p++) + minR = min(minR, A[p]); + for (int p = l; p <= k; p++) + open += A[p] > minR; + for (int p = k + 1; p <= r; p++) + open += A[p] > minL; + // printf("[%d %d %d %d] %d %d\n", l, k, k+1, r, dp[l][k] + dp[k+1][r]+ open, open); + val = min(val, dp[l][k] + dp[k + 1][r] + open); + } + } + } + + for (int i = 0; i < n; i++) + { + for (int j = 0; i + j < n; j++) + { + int l = j, r = i + j, m = i + 1;// [l, r] need 1, 2, 3, ..., m + int used[MAXN] = {}, ok = 1; + for (int k = l; k <= r && ok; k++) + { + if (A[k] > m) + { + ok = 0; + break; + } + used[A[k]]++; + if (used[A[k]] > 1) + ok = 0; + } + complete[l][r] = ok; + } + } + + int dp2[MAXN]; + for (int i = 0; i <= n; i++) + dp2[i] = 0x3f3f3f3f; + for (int i = 0; i < n; i++) + { + for (int j = i; j < n; j++) + { + if (complete[i][j]) + { + int comb = dp[i][j]; + if (i) + comb += dp2[i - 1]; + dp2[j] = min(dp2[j], comb); + } + } + } + if (dp2[n - 1] == 0x3f3f3f3f) + puts("impossible"); + else + printf("%d\n", dp2[n - 1]); + } + return 0; +} +/* +7 +1 2 1 2 4 3 3 +7 +1 2 3 2 4 1 3 +2 +1 1 +2 +2 1 +5 +1 3 2 3 1 +5 +1 2 2 3 1 +*/ diff --git a/158.cpp b/158.cpp new file mode 100644 index 0000000..367a375 --- /dev/null +++ b/158.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +struct Date +{ + int _day, _month, _days; // days from Jan 1 + + void calcDays(int _Year) + { + _days = _day; + if (_month > 1) + { + _days += 31; + } + if (_month > 2) + { + _days += _Year % 4 ? 28 : 29; + } + if (_month > 3) + { + _days += 31; + } + if (_month > 4) + { + _days += 30; + } + if (_month > 5) + { + _days += 31; + } + if (_month > 6) + { + _days += 30; + } + if (_month > 7) + { + _days += 31; + } + if (_month > 8) + { + _days += 31; + } + if (_month > 9) + { + _days += 30; + } + if (_month > 10) + { + _days += 31; + } + if (_month > 11) + { + _days += 30; + } + } +}; +struct Event : public Date +{ + int _priority; + string _desc; +}; +struct EventPointer +{ + const Event *_event; + int _rest; + + int priority() const + { + return _event->_priority; + } + bool operator<(const EventPointer &_Rhs) const + { + if (_rest < _Rhs._rest) + { + return true; + } + if (_rest > _Rhs._rest) + { + return false; + } + if (_rest > 0) + { + if (priority() > _Rhs.priority()) + { + return true; + } + if (priority() < _Rhs.priority()) + { + return false; + } + } + return _event < _Rhs._event; + } +}; + +int main() +{ + int year; + scanf("%d", &year); + vector events; + vector dates; + while (true) + { + char type[2]; + scanf("%s", type); + if (type[0] == 'A') + { + Event event; + scanf("%d %d %d", &event._day, &event._month, &event._priority); + char ch; + do + { + ch = getchar(); + } while (ch == ' ' || ch == '\t'); + do + { + event._desc.insert(event._desc.end(), ch); + ch = getchar(); + } while (ch != '\n' && ch != '\r'); + event.calcDays(year); + events.push_back(event); + if (event._days <= 7) + { + event._days += year % 4 ? 365 : 366; + events.push_back(event); + } + } + else if (type[0] == 'D') + { + Date date; + scanf("%d %d", &date._day, &date._month); + date.calcDays(year); + dates.push_back(date); + } + else + { + break; + } + } + int i, j; + for (i = 0; i < dates.size(); ++i) + { + const Date &date = dates[i]; + set pointers; + for (j = 0; j < events.size(); ++j) + { + const Event &event = events[j]; + EventPointer pointer; + pointer._event = &event; + pointer._rest = event._days - date._days; + if (date._days <= event._days && pointer._rest <= event._priority) + { + pointers.insert(pointer); + } + } + if (i > 0) + { + printf("\n"); + } + printf("Today is:%3d%3d\n", date._day, date._month); + set::iterator itr; + for (itr = pointers.begin(); itr != pointers.end(); ++itr) + { + const Event &event = *itr->_event; + printf("%3d%3d ", event._day, event._month); + if (itr->_rest == 0) + { + printf("*TODAY* "); + } + else + { + int importance = event._priority - itr->_rest + 1; + // ASSERT(1 <= importance && importance <= 7); + for (j = 0; j < importance; ++j) + { + printf("*"); + } + for (; j < 8; ++j) + { + printf(" "); + } + } + printf("%s\n", event._desc.c_str()); + } + } + return 0; +} diff --git a/1580.cpp b/1580.cpp new file mode 100644 index 0000000..f4640d9 --- /dev/null +++ b/1580.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define EPS 1e-6 +#define INF 0x3f3f3f3f +#define MAXN 512 + +int g[MAXN][MAXN], A, B, N, M; + +long long get_height(int w, int h, int v) +{ + if (v == 0) + { + return 0; + } + v = -v; + int base = v * w * h; + int ret = base / (N * M - w * h) - (base % (N * M - w * h) == 0); + return ret + v; +} + +int main() +{ + while (scanf("%d%d%d%d", &A, &B, &N, &M) == 4) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + scanf("%d", &g[i][j]); + g[i][j] = -g[i][j]; + } + } + if (A > B) + { + swap(A, B); + } + int mx[MAXN]; + long long ret = 0; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < M; j++) + { + mx[j] = -INF; + } + for (int j = i; j < N; j++) + { + if (j - i + 1 > B) + { + break; + } + for (int k = 0; k < M; k++) + { + int prev = -INF; + for (int l = k; l < M; l++) + { + if (l - k + 1 > B) + { + break; + } + mx[l] = max(mx[l], g[j][l]); + prev = max(prev, mx[l]); + int w = j - i + 1, h = l - k + 1; + if (w > h) + { + swap(w, h); + } + if (w <= A && h <= B) + { + long long V = get_height(w, h, prev) * w * h; + ret = max(ret, V); + } + } + } + } + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/1581.cpp b/1581.cpp new file mode 100644 index 0000000..a6f7d2a --- /dev/null +++ b/1581.cpp @@ -0,0 +1,345 @@ +#include + +using namespace std; + +#define eps 1e-9 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + bool operator!=(const Pt &a) const + { + return !(a == *this); + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; +const double pi = acos(-1); +int cmpZero(double v) +{ + if (fabs(v) > eps) + return v > 0 ? 1 : -1; + return 0; +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +struct Seg +{ + Pt s, e; + int label; + Seg(Pt a = Pt(), Pt b = Pt(), int l = 0) : s(a), e(b), label(l) + { + } + bool operator!=(const Seg &other) const + { + return !((s == other.s && e == other.e) || (e == other.s && s == other.e)); + } +}; +int intersection(Pt as, Pt at, Pt bs, Pt bt) +{ + if (cmpZero(cross(as, at, bs) * cross(as, at, bt)) < 0 && + cmpZero(cross(bs, bt, as) * cross(bs, bt, at)) < 0) + return 1; + return 0; +} +Pt getIntersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +double getAngle(Pt va, Pt vb) +{// segment, not vector + return acos(dot(va, vb) / va.length() / vb.length()); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} +int inPolygon(vector &p, Pt q) +{ + int i, j, cnt = 0; + int n = p.size(); + for (i = 0, j = n - 1; i < n; j = i++) + { + if (onSeg(p[i], p[j], q)) + return 1; + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + cnt++; + } + return cnt & 1; +} +double polygonArea(vector &p) +{ + double area = 0; + int n = p.size(); + for (int i = 0; i < n; i++) + area += p[i].x * p[(i + 1) % n].y - p[i].y * p[(i + 1) % n].x; + return fabs(area) / 2; +} + +Pt projectLine(Pt as, Pt ae, Pt p) +{ + double a, b, c, v; + a = as.y - ae.y, b = ae.x - as.x; + c = -(a * as.x + b * as.y); + v = a * p.x + b * p.y + c; + return Pt(p.x - v * a / (a * a + b * b), p.y - v * b / (a * a + b * b)); +} + +// +vector circleInterectSeg(Pt a, Pt b, double r) +{ + vector ret; + Pt c, vab, p; + double v, lab; + c = projectLine(a, b, Pt(0, 0)); + vab = a - b, lab = (a - b).length(); + if (cmpZero(c.x * c.x + c.y * c.y - r * r) > 0) + return ret; + v = sqrt(r * r - (c.x * c.x + c.y * c.y)); + vab = vab * (v / lab); + p = c + vab; + if (onSeg(a, b, p)) + ret.push_back(p); + p = c - vab; + if (onSeg(a, b, p)) + ret.push_back(p); + if (ret.size() == 2 && ret[0] == ret[1]) + ret.pop_back(); + return ret; +} + +bool cmp(pair a, pair b) +{ + return a.first < b.first; +} +bool cmp2(pair a, pair b) +{ + return a.first < b.first; +} +double scan(vector poly, double r) +{ + int n = poly.size(); + vector all; + + for (int i = 0, j = n - 1; i < n; j = i++) + { + all.push_back(poly[i]); + all.push_back(poly[j]); + vector inter = circleInterectSeg(poly[i], poly[j], r); + for (int k = 0; k < inter.size(); k++) + all.push_back(inter[k]); + } + sort(all.begin(), all.end()); + all.resize(unique(all.begin(), all.end()) - all.begin()); + + vector> polar; + for (int i = 0; i < all.size(); i++) + { + Pt p = all[i]; + polar.push_back(make_pair(atan2(p.y, p.x), p)); + } + sort(polar.begin(), polar.end(), cmp); + + double ret = 0; + + for (int i = 0; i < polar.size();) + { + vector A, B; + int idx1, idx2; + double ltheta, rtheta; + idx1 = i, ltheta = polar[i].first; + while (idx1 < polar.size() && cmpZero(polar[i].first - polar[idx1].first) == 0) + A.push_back(polar[idx1].second), idx1++; + if (idx1 == polar.size())// end + break; + idx2 = idx1, rtheta = polar[idx1].first; + while (idx2 < polar.size() && cmpZero(polar[idx1].first - polar[idx2].first) == 0) + B.push_back(polar[idx2].second), idx2++; + i = idx1; + + if (A.size() == 0 || B.size() == 0) + assert(false); + + for (int j = 0, k = n - 1; j < n; k = j++) + { + if (cmpZero(cross(Pt(0, 0), A[0], poly[j])) * cmpZero(cross(Pt(0, 0), A[0], poly[k])) < 0) + A.push_back(getIntersect(Seg(Pt(0, 0), A[0]), Seg(poly[j], poly[k]))); + if (cmpZero(cross(Pt(0, 0), B[0], poly[j])) * cmpZero(cross(Pt(0, 0), B[0], poly[k])) < 0) + B.push_back(getIntersect(Seg(Pt(0, 0), B[0]), Seg(poly[j], poly[k]))); + } + + A.push_back(Pt(0, 0)); + B.push_back(Pt(0, 0)); + sort(A.begin(), A.end()); + sort(B.begin(), B.end()); + A.resize(unique(A.begin(), A.end()) - A.begin()); + B.resize(unique(B.begin(), B.end()) - B.begin()); + + vector> crossEdge; + for (int p = 0; p < A.size(); p++) + { + for (int q = 0; q < B.size(); q++) + { + if (A[p] == B[q] || A[p] == Pt(0, 0) || B[q] == Pt(0, 0)) + continue; + for (int j = 0, k = n - 1; j < n; k = j++) + { + if (onSeg(poly[j], poly[k], A[p]) && onSeg(poly[j], poly[k], B[q])) + { + Pt mid = (A[p] + B[q]) * 0.5; + crossEdge.push_back(make_pair((mid - Pt(0, 0)).length(), Seg(A[p], B[q]))); + } + } + } + } + crossEdge.push_back(make_pair(0.0, Seg(Pt(0, 0), Pt(0, 0)))); + sort(crossEdge.begin(), crossEdge.end(), cmp2); + for (int j = 0; j < crossEdge.size() - 1; j++) + { + Seg a = crossEdge[j].second; + Seg b = crossEdge[j + 1].second; + Pt ma = (a.s + a.e) * 0.5; + Pt mb = (b.s + b.e) * 0.5; + Pt mab = (ma + mb) * 0.5; + if (!inPolygon(poly, mab)) + continue; + double area = (fabs(cross(b.s, b.e, a.e)) + fabs(cross(a.s, a.e, b.s))) / 2; + + int inout[4] = {}, all_in, all_out; + inout[0] = cmpZero((a.s - Pt(0, 0)).length() - r) <= 0; + inout[1] = cmpZero((a.e - Pt(0, 0)).length() - r) <= 0; + inout[2] = cmpZero((b.s - Pt(0, 0)).length() - r) <= 0; + inout[3] = cmpZero((b.e - Pt(0, 0)).length() - r) <= 0; + all_in = inout[0] & inout[1] & inout[2] & inout[3]; + all_out = (!inout[0]) & (!inout[1]) & (!inout[2]) & (!inout[3]); + // printf("area %lf\n", area); + // printf("%lf %lf, %lf %lf\n", a.s.x, a.s.y, a.e.x, a.e.y); + // printf("%lf %lf, %lf %lf\n", b.s.x, b.s.y, b.e.x, b.e.y); + if (all_out) + { + // printf("no %lf\n", 0); + continue; + } + if (all_in) + { + // printf("all %lf\n", area); + ret += area; + continue; + } + if (inout[0] == 1 && inout[1] == 1) + { + // printf("part %lf\n", r * r * (rtheta - ltheta)/2 - fabs(cross(Pt(0, 0), a.s, a.e)) /2); + ret += r * r * (rtheta - ltheta) / 2 - fabs(cross(Pt(0, 0), a.s, a.e)) / 2; + } + else + { + // printf("no %lf\n", 0); + } + } + // puts("---"); + } + + return ret; +} +int main() +{ + int n; + double r, x, y; + while (scanf("%d %lf", &n, &r) == 2) + { + vector poly; + for (int i = 0; i < n; i++) + { + scanf("%lf %lf", &x, &y); + poly.push_back(Pt(x, y)); + } + + double ret = scan(poly, r); + printf("%.9lf\n", ret); + } + return 0; +} +/* +6 10 +-8 2 +8 2 +8 14 +0 14 +0 6 +-8 14 + +4 10 +10 0 +10 10 +-10 10 +-10 0 + +6 10 +2 2 +12 2 +6 4 +12 4 +8 8 +-2 8 + +5 10 +-4 6 +-2 2 +0 4 +4 2 +8 4 +*/ diff --git a/1583.cpp b/1583.cpp new file mode 100644 index 0000000..589adc1 --- /dev/null +++ b/1583.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int proc(int n) +{ + int sum = n; + while (n != 0) + { + sum = sum + (n % 10); + n = n / 10; + } + return sum; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + bool flag = 0; + int s, n; + scanf("%d", &n); + if (n > 100) + { + for (int i = n - 100; i < n; i++) + { + if (proc(i) == n) + { + flag = true; + s = i; + break; + } + } + } + else + { + for (int i = n / 2; i < n; i++) + { + if (proc(i) == n) + { + flag = true; + s = i; + break; + } + } + } + printf("%d\n", flag ? s : 0); + } + return 0; +} diff --git a/1584.cpp b/1584.cpp new file mode 100644 index 0000000..7a6bd07 --- /dev/null +++ b/1584.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +int T; +string s, min_; + +int main() { + scanf("%d", &T); + while (T--) { + min_ = "Z"; + cin >> s; + int sz = s.size(); + s += s; + for (int i = 0; i < sz; ++i) { + min_ = min(min_, s.substr(i, sz)); + } + printf("%s\n", min_.c_str()); + } + return 0; +} diff --git a/1585.cpp b/1585.cpp new file mode 100644 index 0000000..53236b7 --- /dev/null +++ b/1585.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +#define MAXN 131072 + +char s[MAXN]; + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%s", s); + int ret = 0, O = 0; + for (int i = 0; s[i]; i++) + { + O = s[i] == 'O' ? O + 1 : 0; + ret += O; + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1586.cpp b/1586.cpp new file mode 100644 index 0000000..e8f215f --- /dev/null +++ b/1586.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +char s[1024]; + +int main() +{ + double gmol[128]; + gmol['C'] = 12.01f; + gmol['H'] = 1.008f; + gmol['O'] = 16.00f; + gmol['N'] = 14.01f; + + int T; + scanf("%d", &T); + while (T--) + { + scanf("%s", s); + double res = 0.0f; + int n = strlen(s); + for (int i = 0; i < n;) + { + int ele = s[i], num = 0; + i++; + while (s[i] >= '0' && s[i] <= '9') + { + num = num * 10 + s[i] - '0', i++; + } + if (num == 0) + { + num = 1; + } + res += gmol[ele] * num; + } + printf("%.3lf\n", res); + } + return 0; +} diff --git a/1587.cpp b/1587.cpp new file mode 100644 index 0000000..bc75ee7 --- /dev/null +++ b/1587.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +struct Pallet {int w, h;} pallet[6]; + +int main() +{ + int w, h; + while (scanf("%d%d", &w, &h) != EOF) + { + pallet[0].w = min(w,h); + pallet[0].h = max(w,h); + for (int i = 1; i < 6; i++) + { + scanf("%d%d", &w, &h); + pallet[i].w = min(w,h); + pallet[i].h = max(w,h); + } + + sort(pallet, pallet + 6, [](Pallet &a, Pallet &b) -> bool + { + return a.w < b.w || ((a.w == b.w) && (a.h < b.h)); + }); + + bool ok = true; + for (int i = 0; i < 6; i += 2) + { + if (pallet[i].w != pallet[i + 1].w + || pallet[i].h != pallet[i + 1].h) + { + ok = false; + } + } + + if (ok) + { + ok = false; + if (pallet[0].w == pallet[2].w + && pallet[0].h == pallet[4].w + && pallet[2].h == pallet[4].h) + { + ok = true; + } + } + + printf(ok ? "POSSIBLE\n" : "IMPOSSIBLE\n"); + } + + return 0; +} diff --git a/1588.cpp b/1588.cpp new file mode 100644 index 0000000..3f5c5fe --- /dev/null +++ b/1588.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +char s1[128], s2[128]; + +int main() +{ + while (scanf("%s%s", s1, s2) == 2) + { + int n1 = strlen(s1), n2 = strlen(s2); + int res = n1 + n2; + for (int i = -n2; i <= n1; i++) + { + int ok = 1; + for (int j = 0; j < n2 && ok; j++) + { + if (i + j >= 0 && i + j < n1) + { + ok &= s1[i + j] - '0' + s2[j] - '0' <= 3; + } + } + if (ok) + { + res = min(res, max(n1, i + n2) - min(i, 0)); + } + } + printf("%d\n", res); + } + return 0; +} diff --git a/1589.cpp b/1589.cpp new file mode 100644 index 0000000..ddd2c25 --- /dev/null +++ b/1589.cpp @@ -0,0 +1,187 @@ +#include + +using namespace std; + +const int N = 12; + +const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; +const int hx[] = {-2, -1, -2, -1, 1, 2, 1, 2}; +const int hy[] = {-1, -2, 1, 2, -2, -1, 2, 1}; +const int tx[] = {-1, -1, -1, -1, 1, 1, 1, 1}; +const int ty[] = {-1, -1, 1, 1, -1, -1, 1, 1}; + +char brd[N][N]; +int cr[2], cc[2]; + +int chk(int r, int c) +{ + int i, j, k, tr, tc, cnt; + if (r < 1 || r > 3 || c < 4 || c > 6) + { + return 1; + } + + for (j = c - 1; j > 0; --j) + { + if (brd[r][j]) + { + if (brd[r][j] == 'R') + { + return 1; + } + else + { + break; + } + } + } + for (j = c + 1; j <= 9; ++j) + { + if (brd[r][j]) + { + if (brd[r][j] == 'R') + { + return 1; + } + else + { + break; + } + } + } + + for (i = r - 1; i > 0; --i) + { + if (brd[i][c]) + { + if (brd[i][c] == 'R') + { + return 1; + } + else + { + break; + } + } + } + for (i = r + 1; i <= 10; ++i) + { + if (brd[i][c]) + { + if (brd[i][c] == 'R' || brd[i][c] == 'G') + { + return 1; + } + else + { + break; + } + } + } + + for (int k = 0; k < 2; ++k) + { + if (cr[k] == r) + { + for (j = c - 1, cnt = 0; j > cc[k]; --j) + { + if (brd[r][j]) + { + ++cnt; + } + } + if (cnt == 1) + { + return 1; + } + for (j = c + 1, cnt = 0; j < cc[k]; ++j) + { + if (brd[r][j]) + { + ++cnt; + } + } + if (cnt == 1) + { + return 1; + } + } + if (cc[k] == c) + { + for (i = r - 1, cnt = 0; i > cr[k]; --i) + { + if (brd[i][c]) + { + ++cnt; + } + } + if (cnt == 1) + { + return 1; + } + for (i = r + 1, cnt = 0; i < cr[k]; ++i) + { + if (brd[i][c]) + { + ++cnt; + } + } + if (cnt == 1) + { + return 1; + } + } + } + + for (int k = 0; k < 8; ++k) + { + tr = r + hx[k], tc = c + hy[k]; + if (tr < 1 || tr > 10 || tc < 1 || tc > 9) + { + continue; + } + if (brd[tr][tc] == 'H' && (!brd[r + tx[k]][c + ty[k]])) + { + return 1; + } + } + + return 0; +} + +int main() +{ + char s[5]; + int n, r, c, x, y; + + while (scanf("%d%d%d", &n, &r, &c) == 3, n || r || c) + { + memset(brd, 0, sizeof(brd)); + cr[0] = cc[0] = cr[1] = cc[1] = 0; + + while (n--) + { + scanf("%s%d%d", s, &x, &y); + if (s[0] == 'C') + { + if (cr[0]) + { + cr[1] = x, cc[1] = y; + } + else + { + cr[0] = x, cc[0] = y; + } + } + brd[x][y] = s[0]; + } + + int cnt = 0; + for (int i = 0; i < 4; ++i) + { + cnt += chk(r + dx[i], c + dy[i]); + } + printf(cnt < 4 ? "NO\n" : "YES\n"); + } + return 0; +} diff --git a/159.cpp b/159.cpp new file mode 100644 index 0000000..24a4bbd --- /dev/null +++ b/159.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +bool cross_possible(const string &a, const string &b) +{ + for (int i = 0, sz = a.size(); i < sz; i++) + { + for (int j = 0, sz2 = b.size(); j < sz2; j++) + { + if (a[i] == b[j]) + { + return true; + } + } + } + return false; +} + +int main() +{ + int h1, h2; + int v1, v2; + string words[4]; + bool first = true; + while (cin >> words[0]) + { + if (words[0] == "#") + { + break; + } + if (!first) + { + cout << endl; + } + cin >> words[1] >> words[2] >> words[3]; + if (cross_possible(words[0], words[1]) && cross_possible(words[2], words[3])) + { + for (int i = 0, sz = words[0].size(); i < sz; i++) + { + for (int j = 0, sz2 = words[1].size(); j < sz2; j++) + { + if (words[0][i] == words[1][j]) + { + h1 = i; + v1 = j; + i = 10000000; + break; + } + } + } + for (int i = 0, sz = words[2].size(); i < sz; i++) + { + for (int j = 0, sz2 = words[3].size(); j < sz2; j++) + { + if (words[2][i] == words[3][j]) + { + h2 = i; + v2 = j; + i = 10000000; + break; + } + } + } + // top part + for (int i = 0, sz = max(v1, v2); i < sz; i++) + { + for (int j = 0; j < h1; j++) + { + cout << " "; + } + // first virtical word + if (v1 < sz - i) + { + cout << " "; + } + else + { + if (v1 >= v2) + { + cout << words[1][i]; + } + else + { + cout << words[1][i - abs(v1 - v2)]; + } + } + // second virtical word + if (v2 < sz - i) + { + // do nothing + } + else + { + for (int j = 0; j < words[0].size() - h1 - 1 + 3 + h2; j++) + { + cout << " "; + } + if (v2 >= v1) + { + cout << words[3][i]; + } + else + { + cout << words[3][i - abs(v1 - v2)]; + } + } + cout << endl; + } + // horizontal part + cout << words[0] << " " << words[2] << endl; + // bottom part + for (int i = 0, sz = max(words[1].size() - v1 - 1, words[3].size() - v2 - 1); i < sz; i++) + { + for (int j = 0; j < h1; j++) + { + cout << " "; + } + // first vertical word + if (words[1].size() - v1 - 1 <= i) + { + cout << " "; + } + else + { + cout << words[1][v1 + 1 + i]; + } + // second vertical word + if (words[3].size() - v2 - 1 <= i) + { + // do nothing + } + else + { + for (int j = 0; j < words[0].size() - h1 - 1 + 3 + h2; j++) + { + cout << " "; + } + cout << words[3][v2 + 1 + i]; + } + cout << endl; + } + } + else + { + cout << "Unable to make two crosses" << endl; + } + first = false; + } + return 0; +} diff --git a/1590.cpp b/1590.cpp new file mode 100644 index 0000000..b3ff1bb --- /dev/null +++ b/1590.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +struct Trie +{ + int n, label, dist; + int link[2]; +} Node[1048576]; +int TrieSize; +int insertTrie(const char *str) +{ + static int i, idx; + for (i = idx = 0; str[i]; i++) + { + if (Node[idx].link[str[i] - '0'] == 0) + { + TrieSize++; + memset(&Node[TrieSize], 0, sizeof(Node[0])); + Node[TrieSize].label = TrieSize; + Node[TrieSize].dist = i + 1; + Node[idx].link[str[i] - '0'] = TrieSize; + } + idx = Node[idx].link[str[i] - '0']; + } + Node[idx].n++; + return Node[idx].label; +} +void binaryIP(const int ip[], char buf[]) +{ + int idx = 0; + for (int i = 0; i < 4; i++) + { + for (int j = 8 - 1; j >= 0; j--) + { + buf[idx++] = '0' + ((ip[i] >> j) & 1); + } + } + buf[idx] = '\0'; +} +void getAllLCP(char buf[]) +{ + int idx = 0, bidx = 0; + do + { + int branch = 0, next = 0, c = 0; + for (int i = 0; i < 2; i++) + { + if (Node[idx].link[i]) + { + branch++, next = Node[idx].link[i], c = i; + } + } + if (branch != 1) + break; + buf[bidx++] = c + '0', idx = next; + } while (true); + buf[bidx] = '\0'; +} +int main() +{ + int n, ip[4]; + char bip[128]; + while (scanf("%d", &n) == 1) + { + TrieSize = 0; + memset(&Node[0], 0, sizeof(Node[0])); + for (int i = 0; i < n; i++) + { + scanf("%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); + binaryIP(ip, bip); + insertTrie(bip); + } + getAllLCP(bip); + int m = (int)strlen(bip); + for (int i = 0; i < 4; i++) + { + int bb = 0; + for (int j = 8 - 1; j >= 0; j--) + { + if (i * 8 + 7 - j < m) + { + if (bip[i * 8 + 7 - j] == '1') + { + bb |= 1 << j; + } + } + } + printf("%d%c", bb, i == 3 ? '\n' : '.'); + } + for (int i = 0; i < 4; i++) + { + int bb = 0; + for (int j = 8 - 1; j >= 0; j--) + { + if (i * 8 + 7 - j < m) + { + bb |= 1 << j; + } + } + printf("%d%c", bb, i == 3 ? '\n' : '.'); + } + } + return 0; +} diff --git a/1591.cpp b/1591.cpp new file mode 100644 index 0000000..0f54874 --- /dev/null +++ b/1591.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int main() +{ + long long N, S, Q; + while (scanf("%lld%lld%lld", &N, &S, &Q) == 3) + { + long long retK = 1LL << 60, retA = 0, retB = 0; + for (int A = 0; A <= 33; A++) + { + for (int B = 0; B <= 100; B++) + { + if (((S + (S << A)) >> B) >= Q) + { + long long pos = (N - 1) * S; + long long K = (pos + (pos << A)) >> B; + if (K < retK) + { + retK = K; + retA = A; + retB = B; + } + } + } + } + retK += Q; + printf("%lld %lld %lld\n", retK, retA, retB); + } + return 0; +} diff --git a/1592.cpp b/1592.cpp new file mode 100644 index 0000000..9bc1c93 --- /dev/null +++ b/1592.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +char data[10010][128]; +int colPos[10010][16]; +int hashCode[10010][16]; +struct cmp +{ + static int C1, C2; + bool operator()(const pair, int> &a, + const pair, int> &b) const + { + if (a.first != b.first) + return a.first < b.first; + int t; + t = strcmp(data[a.second] + colPos[a.second][C1], + data[b.second] + colPos[b.second][C1]); + if (t) + return t < 0; + t = strcmp(data[a.second] + colPos[a.second][C2], + data[b.second] + colPos[b.second][C2]); + if (t) + return t < 0; + return false; + } +}; +int cmp::C1 = 0; +int cmp::C2 = 0; +int main() +{ + int n, m; + // freopen("in.txt", "r+t", stdin); + // freopen("out.txt", "w+t", stdout); + while (scanf("%d %d", &n, &m) == 2) + { + while (getchar() != '\n') + ; + for (int i = 0; i < n; i++) + { + gets(data[i]); + for (int j = 0, pos = 0; j < m; j++) + { + colPos[i][j] = pos; + int hash = 0; + while (data[i][pos] != ',' && data[i][pos] != '\0') + hash = ((hash << 15) + data[i][pos]) & 32767, pos++; + hashCode[i][j] = hash; + data[i][pos] = '\0', pos++; + } + } + + pair, int> D[10010]; + int flag = 0, r1, r2, c1, c2; + for (int i = 0; i < m; i++) + { + for (int j = i + 1; j < m; j++) + { + for (int k = 0; k < n; k++) + {// (hashCode[k][i], hashCode[k][j]) + D[k] = make_pair(make_pair(hashCode[k][i], hashCode[k][j]), k); + } + cmp::C1 = i, cmp::C2 = j; + sort(D, D + n, cmp()); + for (int k = 1; k < n; k++) + { + if (!cmp()(D[k], D[k - 1]) && !cmp()(D[k - 1], D[k])) + { + flag = 1; + r1 = D[k - 1].second, r2 = D[k].second; + c1 = i, c2 = j; + i = j = m; + break; + } + } + } + } + puts(flag ? "NO" : "YES"); + if (flag) + printf("%d %d\n%d %d\n", r1 + 1, r2 + 1, c1 + 1, c2 + 1); + } + return 0; +} diff --git a/1593.cpp b/1593.cpp new file mode 100644 index 0000000..0cd8a15 --- /dev/null +++ b/1593.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int main() +{ + string line; + vector > ret; + int mx[256] = {}; + while (getline(cin, line)) + { + stringstream sin(line); + vector v; + string x; + while (sin >> x) + { + v.push_back(x); + mx[v.size()] = max(mx[v.size()], (int)x.length()); + } + ret.push_back(v); + } + for (int i = 1; i < 256; i++) + { + mx[i] += mx[i - 1] + 1; + } + for (int i = 0; i < ret.size(); i++) + { + vector &v = ret[i]; + int pos = 0; + for (int j = 0; j < v.size(); j++) + { + while (pos < mx[j]) + { + printf(" "); + pos++; + } + printf("%s", v[j].c_str()); + pos += v[j].length(); + } + printf("\n"); + } + return 0; +} diff --git a/1594.cpp b/1594.cpp new file mode 100644 index 0000000..462a097 --- /dev/null +++ b/1594.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int main() +{ + int grp, n, num; + scanf("%d", &grp); + for (int i = 1; i <= grp; i++) + { + vector nums, nums_tmp; + scanf("%d", &n); + for (int j = 1; j <= n; j++) + { + scanf("%d", &num); + nums.push_back(num); + nums_tmp.push_back(num); + } + for (int k = 0; k < 1000; k++) + { + int total = 0; + for (int j = 0; j < n; j++) + { + total += nums[j]; + } + if (total == 0) + { + printf("ZERO\n"); + break; + } + for (int j = 0; j < n; j++) + { + if (j == n - 1) + { + nums_tmp[j] = abs(nums[j] - nums[0]); + } + else + { + nums_tmp[j] = abs(nums[j] - nums[j + 1]); + } + } + for (int j = 0; j < n; j++) + { + nums[j] = nums_tmp[j]; + } + } + int total = 0; + for (int j = 0; j < n; j++) + { + total += nums[j]; + } + if (total != 0) + { + printf("LOOP\n"); + } + } + return 0; +} diff --git a/1595.cpp b/1595.cpp new file mode 100644 index 0000000..1385803 --- /dev/null +++ b/1595.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +int T, n, x[1024], y[1024]; + +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%d %d", &x[i], &y[i]); + } + int mx, mn; + mx = mn = x[0]; + for (int i = 0; i < n; i++) + { + mx = max(mx, x[i]); + mn = min(mn, x[i]); + } + double mid = (mx + mn) / 2.0; + set > S; + for (int i = 0; i < n; i++) + { + S.insert(make_pair((int)((x[i] - mid) * 2), y[i])); + } + int ret = 1; + for (int i = 0; i < n && ret; i++) + { + ret &= S.find(make_pair((int)(-(x[i] - mid) * 2), y[i])) != S.end(); + } + printf(ret ? "YES\n" : "NO\n"); + } + return 0; +} diff --git a/1596.cpp b/1596.cpp new file mode 100644 index 0000000..29af701 --- /dev/null +++ b/1596.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +int testBug; +map dsz; +map > dr; + +void declara(char exp[]) +{ + string name = ""; + int sz = 0, i; + for (i = 0; exp[i] != '['; i++) + { + name += exp[i]; + } + for (i++; exp[i] != ']'; i++) + { + sz = sz * 10 + exp[i] - '0'; + } + dsz[name] = sz; + dr[name].clear(); +} + +int get_val(char exp[]) +{ + if (isdigit(exp[0])) + { + int val = 0; + for (int i = 0; isdigit(exp[i]); i++) + { + val = val * 10 + exp[i] - '0'; + } + return val; + } + string name = ""; + int i; + for (i = 0; exp[i] != '['; i++) + { + name += exp[i]; + } + int idx = get_val(exp + i + 1); + if (dsz.count(name) == 0 || idx >= dsz[name] || dr[name].count(idx) == 0) + { + testBug = 1; + return 0; + } + return dr[name][idx]; +} + +void assign(char exp[]) +{ + string name = ""; + int i; + for (i = 0; exp[i] != '['; i++) + { + name += exp[i]; + } + int idx = get_val(exp + i + 1); + for (i = 0; exp[i] != '='; i++) + ; + int val = get_val(exp + i + 1); + if (testBug) + { + return; + } + if (dsz.count(name) == 0 || idx >= dsz[name]) + { + testBug = 1; + return; + } + dr[name][idx] = val; +} + +int main() +{ + char c; + while ((c = getchar()) != '.') + { + ungetc(c, stdin); + dsz.clear(); + dr.clear(); + char exp[32767]; + int bug_line = 0, lines = 0; + testBug = 0; + while (gets(exp) && exp[0] != '.') + { + lines++; + int f = 0; + for (int i = 0; exp[i]; i++) + { + f |= exp[i] == '='; + } + if (!f) + { + declara(exp); + } + else + { + assign(exp); + } + if (testBug && bug_line == 0) + { + bug_line = lines; + } + } + printf("%d\n", bug_line); + } + return 0; +} diff --git a/1597.cpp b/1597.cpp new file mode 100644 index 0000000..5a572cf --- /dev/null +++ b/1597.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +struct article +{ + article() + { + for (char s[99]; gets(s) && strcmp(s, "**********");) + { + lines.emplace_back(s); + transform(s, s + strlen(s), s, ::tolower); + string S; + stringstream ss(S = regex_replace(s, regex("[^a-z]"), " ")); + lower.push_back(S); + while (ss >> s) + words.push_back(s); + } + } + bool exist(const string &s) { return ::find(words.begin(), words.end(), s) != words.end(); } + void find(char *term1, char *term2 = "") + { + begin = true; + for (int i = 0; i < lower.size(); ++i) + if (match(lower[i], term1)) + print(lines[i]); + else if (*term2 && match(lower[i], term2)) + print(lines[i]); + } + bool match(string &s1, char *s2) + { + stringstream ss(s1); + for (char s[99]; ss >> s;) + if (!strcmp(s, s2)) + return true; + return false; + } + void print(string s) + { + if (begin) + { + begin = false; + if (first) + first = false; + else + puts("----------"); + } + puts(s.c_str()); + } + vector lines, words, lower; + static bool first; + bool begin; +}; + +bool article::first; + +int main() +{ + int n; + char key[3][99], search[99]; + scanf("%d\n", &n); + article collection[n]; + for (scanf("%d\n", &n); n--;) + { + fgets(search, 99, stdin); + article::first = true; + int get = sscanf(search, "%s %s %s", key[0], key[1], key[2]); + if (get == 1) + { + for (auto &c : collection) + { + if (c.exist(key[0])) + { + c.find(key[0]); + } + } + } + else if (get == 2) + { + for (auto &c : collection) + if (!c.exist(key[1])) + { + if (article::first) + { + article::first = false; + } + else + { + puts("----------"); + } + for (auto &s : c.lines) + { + cout << s << endl; + } + } + } + else if (strcmp(key[1], "AND") == 0) + { + for (auto &c : collection) + { + if (c.exist(key[0]) && c.exist(key[2])) + { + c.find(key[0], key[2]); + } + } + } + else + { + for (auto &c : collection) + { + if (c.exist(key[0]) || c.exist(key[2])) + { + c.find(key[0], key[2]); + } + } + } + if (article::first) + { + puts("Sorry, I found nothing."); + } + puts("=========="); + } + return 0; +} diff --git a/1598.cpp b/1598.cpp new file mode 100644 index 0000000..3da5d85 --- /dev/null +++ b/1598.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +struct CMD +{ + string cmd; + int size, price; + CMD(string kind, int x = 0, int y = 0) + : cmd(kind), size(x), price(y) + { + } +}; +vector D; + +map > BUY, SELL; +map BUY_VAL, SELL_VAL; + +void trade(int kind) +{ + while (!BUY.empty() && !SELL.empty()) + { + if (BUY.rbegin()->first >= SELL.begin()->first) + { + set &v1 = BUY.rbegin()->second; + set &v2 = SELL.begin()->second; + int aid = *v1.begin(), bid = *v2.begin(); + int z = min(D[aid].size, D[bid].size); + printf("TRADE %d %d\n", z, kind ? D[aid].price : D[bid].price); + D[aid].size -= z; + D[bid].size -= z; + BUY_VAL[D[aid].price] -= z; + SELL_VAL[D[bid].price] -= z; + if (D[aid].size == 0) + { + v1.erase(aid); + } + if (D[bid].size == 0) + { + v2.erase(bid); + } + if (v1.size() == 0) + { + BUY.erase(D[aid].price); + } + if (v2.size() == 0) + { + SELL.erase(D[bid].price); + } + } + else + { + return; + } + } +} + +void print() +{ + while (BUY_VAL.size() && BUY_VAL.rbegin()->second == 0) + { + BUY_VAL.erase(BUY_VAL.rbegin()->first); + } + while (SELL_VAL.size() && SELL_VAL.begin()->second == 0) + { + SELL_VAL.erase(SELL_VAL.begin()->first); + } + printf("QUOTE "); + if (BUY_VAL.size()) + { + printf("%d %d", BUY_VAL.rbegin()->second, BUY_VAL.rbegin()->first); + } + else + { + printf("0 0"); + } + printf(" - "); + if (SELL_VAL.size()) + { + printf("%d %d", SELL_VAL.begin()->second, SELL_VAL.begin()->first); + } + else + { + printf("0 99999"); + } + printf("\n"); +} + +int main() +{ + char cmd[16]; + int Q, cases = 0; + while (scanf("%d", &Q) == 1) + { + if (cases++) + { + printf("\n"); + } + BUY.clear(); + SELL.clear(); + BUY_VAL.clear(); + SELL_VAL.clear(); + D.clear(); + int size, price, id; + for (int i = 0; i < Q; i++) + { + scanf("%s", cmd); + if (!strcmp(cmd, "BUY")) + { + scanf("%d %d", &size, &price); + BUY[price].insert(i); + BUY_VAL[price] += size; + D.push_back(CMD("BUY", size, price)); + trade(0); + } + else if (!strcmp(cmd, "SELL")) + { + scanf("%d %d", &size, &price); + SELL[price].insert(i); + SELL_VAL[price] += size; + D.push_back(CMD("SELL", size, price)); + trade(1); + } + else if (!strcmp(cmd, "CANCEL")) + { + scanf("%d", &id), id--; + D.push_back(CMD("CANCEL", id)); + if (D[id].cmd == "BUY") + { + BUY[D[id].price].erase(id); + if (BUY[D[id].price].size() == 0) + { + BUY.erase(D[id].price); + } + BUY_VAL[D[id].price] -= D[id].size; + D[id].size = 0; + } + if (D[id].cmd == "SELL") + { + SELL[D[id].price].erase(id); + if (SELL[D[id].price].size() == 0) + { + SELL.erase(D[id].price); + } + SELL_VAL[D[id].price] -= D[id].size; + D[id].size = 0; + } + } + print(); + } + } + return 0; +} diff --git a/1599.cpp b/1599.cpp new file mode 100644 index 0000000..1e85acd --- /dev/null +++ b/1599.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +const int MAXN = 100010; +const int INF = 1000000010; + +struct Edge +{ + int u, v, c; + Edge(int u = 0, int v = 0, int c = 0) + : u(u), v(v), c(c) + { + } +}; +vector edges; + +bool vis[MAXN]; +int n, d[MAXN]; +vector G[MAXN], ans; + +void add_edge(int u, int v, int c) +{ + edges.push_back(Edge(u, v, c)); + int idx = edges.size() - 1; + G[u].push_back(idx); +} + +void rev_bfs() +{ + memset(vis, 0, sizeof(vis)); + queue q; + q.push(n - 1); + vis[n - 1] = true; + d[n - 1] = 0; + + while (!q.empty()) + { + int u = q.front(); + q.pop(); + for (int i = 0; i < G[u].size(); ++i) + { + int e = G[u][i]; + int v = edges[e].v; + if (!vis[v]) + { + vis[v] = true; + d[v] = d[u] + 1; + q.push(v); + } + } + } +} + +void bfs() +{ + memset(vis, 0, sizeof(vis)); + vis[0] = true; + ans.clear(); + + vector nxt; + nxt.push_back(0); + for (int i = 0; i < d[0]; ++i) + { + int min_color = INF; + for (int j = 0; j < nxt.size(); ++j) + { + int u = nxt[j]; + for (int k = 0; k < G[u].size(); ++k) + { + int e = G[u][k]; + int v = edges[e].v; + if (d[u] == d[v] + 1) + { + min_color = min(min_color, edges[e].c); + } + } + } + ans.push_back(min_color); + + vector nxt2; + for (int j = 0; j < nxt.size(); ++j) + { + int u = nxt[j]; + for (int k = 0; k < G[u].size(); ++k) + { + int e = G[u][k]; + int v = edges[e].v; + if (!vis[v] && d[u] == d[v] + 1 && edges[e].c == min_color) + { + vis[v] = true; + nxt2.push_back(v); + } + } + } + nxt = nxt2; + } + + printf("%d\n%d", d[0], ans[0]); + for (int i = 1; i < ans.size(); ++i) + { + printf(" %d", ans[i]); + } + printf("\n"); +} + +int main() +{ + int m, u, v, c; + while (scanf("%d%d", &n, &m) == 2) + { + edges.clear(); + for (int i = 0; i < n; ++i) + { + G[i].clear(); + } + while (m--) + { + scanf("%d%d%d", &u, &v, &c); + if (u == v) + { + continue; + } + add_edge(u - 1, v - 1, c); + add_edge(v - 1, u - 1, c); + } + rev_bfs(); + bfs(); + } + return 0; +} diff --git a/160.cpp b/160.cpp index 2f50fd8..2a3a7bf 100644 --- a/160.cpp +++ b/160.cpp @@ -1,54 +1,87 @@ -#include -#include -using namespace std; - -int isPrime(int a){ - for(int k=2;k<=sqrt(a);k++){ - if(a%k==0) - return 0; - } - return 1; -} - -int main(){ - int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; - for(;;){ - int n; - cin>>n; - if(n==0) - break; - int factor[25]={0}; - int ind=0; - for(int i=n;i>=2;i--){ - int val=i; - for(int j=0;j<25;j++){ - while((val%prime[j])==0){ - factor[j]++; - val/=prime[j]; - } - if(isPrime(val)==1) - break; - } - for(int j=0;j<25;j++){ - if(prime[j]==val){ - factor[j]++; - break; - } - } - } - for(int j=24;j>=0;j--){ - if(factor[j]!=0){ - ind=j; - break; - } - } - printf("%3d! =",n); - for(int i=0;i<=ind;i++){ - if(i!=0 && i%15==0) - printf("\n "); - printf("%3d",factor[i]); - } - cout< + +using namespace std; + +enum +{ + PRIME_COUNT = 25 +}; + +int primes[PRIME_COUNT] = + { + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, + 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; + +int count_factorial(int num, int fac) +{ + int counter = 0; + while ((num % fac) == 0) + { + num = num / fac; + counter++; + } + return counter; +} + +int init(int fac_term[101][PRIME_COUNT]) +{ + for (int i = 0; i < 101; ++i) + for (int j = 0; j < PRIME_COUNT; ++j) + { + fac_term[i][j] = -1; + } +} + +void print_result(int n, int fac_term[PRIME_COUNT]) +{ + printf("%3d! =", n); + for (int i = 0; i < PRIME_COUNT; ++i) + { + if (fac_term[i] == -1) + { + break; + } + if (i == 15) + { + printf("\n "); + } + printf("%3d", fac_term[i]); + } + putchar('\n'); +} + +int main() +{ + int fac_term[101][PRIME_COUNT]; + init(fac_term); + // build factorial terms table + for (int i = 2; i <= 100; ++i) + { + for (int j = 0; j < PRIME_COUNT; ++j) + { + if (primes[j] > i) + { + break; + } + fac_term[i][j] = count_factorial(i, primes[j]); + } + for (int j = 0; j < PRIME_COUNT; ++j) + { + if (fac_term[i - 1][j] >= 0) + { + fac_term[i][j] += fac_term[i - 1][j]; + } + } + } + + int n; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + print_result(n, fac_term[n]); + } + return 0; +} diff --git a/1600.cpp b/1600.cpp new file mode 100644 index 0000000..755e402 --- /dev/null +++ b/1600.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int g[32][32]; +int dist[32][32][32]; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; +int main() +{ + int testcase; + int N, M, K; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d %d", &N, &M, &K); + for (int i = 0; i < N; i++) + for (int j = 0; j < M; j++) + scanf("%d", &g[i][j]); + memset(dist, 63, sizeof(dist)); + dist[0][0][0] = 0; + queue X, Y, S; + int x, y, s, tx, ty, ts; + X.push(0), Y.push(0), S.push(0); + while (!X.empty()) + { + x = X.front(), X.pop(); + y = Y.front(), Y.pop(); + s = S.front(), S.pop(); + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= N || ty >= M) + continue; + if (g[tx][ty]) + ts = s + 1; + else + ts = 0; + if (ts > K) + continue; + if (dist[tx][ty][ts] > dist[x][y][s] + 1) + { + dist[tx][ty][ts] = dist[x][y][s] + 1; + X.push(tx), Y.push(ty), S.push(ts); + } + } + } + int ret = 0x3f3f3f3f; + for (int i = 0; i <= K; i++) + ret = min(ret, dist[N - 1][M - 1][i]); + printf("%d\n", ret == 0x3f3f3f3f ? -1 : ret); + } + return 0; +} +/* +3 +2 5 +0 +0 1 0 0 0 +0 0 0 1 0 +4 6 +1 +0 1 1 0 0 0 +0 0 1 0 1 1 +0 1 1 1 1 0 +0 1 1 1 0 0 +2 2 +0 +0 1 +1 0 +*/ diff --git a/1601.cpp b/1601.cpp new file mode 100644 index 0000000..b4fb96f --- /dev/null +++ b/1601.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +#define pos_now link[i][j] +#define pos_nxt link[i + nxt[0][k]][j + nxt[1][k]] +#define pos_a conn[queue_fro[fro_fro].a][i] +#define pos_b conn[queue_fro[fro_fro].b][j] +#define pos_c conn[queue_fro[fro_fro].c][k] +#define pos_A conn[queue_back[back_fro].a][i] +#define pos_B conn[queue_back[back_fro].b][j] +#define pos_C conn[queue_back[back_fro].c][k] + +struct Node {int a,b,c;}; +Node queue_fro[4000000], queue_back[4000000]; + +const int nxt[2][4] = {{0, 1, 0, -1},{1, 0, -1, 0}}; + +char map_[20][20]; +int link[20][20], conn[305][7], vis[305][305][305], beg[3], end_[3], w, h, n, num, step_fro, step_back, back_fro, back_rear, fro_fro, fro_rear; + +void bfs() +{ + int fro_cnt = 1, back_cnt = 1; + vis[beg[0]][beg[1]][beg[2]] = ++step_fro; + vis[end_[0]][end_[1]][end_[2]] = ++step_back; + fro_fro = back_fro = 0; + fro_rear = back_rear = 1; + queue_fro[0].a = beg[0], queue_fro[0].b = beg[1], queue_fro[0].c = beg[2]; + queue_back[0].a = end_[0], queue_back[0].b = end_[1], queue_back[0].c = end_[2]; + + while (fro_fro < fro_rear) + { + ++step_fro; + while (fro_cnt--) + { + for (int i = 0; pos_a != 0; ++i) + for (int j = 0; pos_b != 0; ++j) + for (int k = 0; pos_c != 0; ++k) + { + if (beg[2] != 303) + { + if (pos_a == queue_fro[fro_fro].b && pos_b == queue_fro[fro_fro].a) + continue; + if (pos_a == queue_fro[fro_fro].c && pos_c == queue_fro[fro_fro].a) + continue; + if (pos_b == queue_fro[fro_fro].c && pos_c == queue_fro[fro_fro].b) + continue; + if (pos_a == pos_b || pos_a == pos_c || pos_b == pos_c) + continue; + } + else if (beg[1] != 303) + { + if (pos_a == queue_fro[fro_fro].b && pos_b == queue_fro[fro_fro].a) + continue; + if (pos_a == pos_b) + continue; + } + + if (vis[pos_a][pos_b][pos_c] > 1000) + return; + if (vis[pos_a][pos_b][pos_c] > 0) + continue; + + vis[pos_a][pos_b][pos_c] = step_fro; + queue_fro[fro_rear].a = pos_a; + queue_fro[fro_rear].b = pos_b; + queue_fro[fro_rear].c = pos_c; + ++fro_rear; + } + ++fro_fro; + } + fro_cnt = fro_rear - fro_fro; + + ++step_back; + while (back_cnt--) + { + for (int i = 0; pos_A != 0; ++i) + for (int j = 0; pos_B != 0; ++j) + for (int k = 0; pos_C != 0; ++k) + { + if (beg[2] != 303) + { + if (pos_A == queue_back[back_fro].b && pos_B == queue_back[back_fro].a) + continue; + if (pos_A == queue_back[back_fro].c && pos_C == queue_back[back_fro].a) + continue; + if (pos_B == queue_back[back_fro].c && pos_C == queue_back[back_fro].b) + continue; + if (pos_A == pos_B || pos_A == pos_C || pos_B == pos_C) + continue; + } + else if (beg[1] != 303) + { + if (pos_A == queue_back[back_fro].b && pos_B == queue_back[back_fro].a) + continue; + if (pos_A == pos_B) + continue; + } + + if (vis[pos_A][pos_B][pos_C] > 1000) + continue; + if (vis[pos_A][pos_B][pos_C] > 0) + return; + + vis[pos_A][pos_B][pos_C] = step_back; + queue_back[back_rear].a = pos_A; + queue_back[back_rear].b = pos_B; + queue_back[back_rear].c = pos_C; + ++back_rear; + } + ++back_fro; + } + back_cnt = back_rear - back_fro; + } +} + +int main() +{ + while (scanf("%d%d%d", &w, &h, &n) == 3 && n) + { + getchar(); + //åˆå§‹åŒ– + memset(map_, 0, sizeof(map_)); + memset(vis, 0, sizeof(vis)); + memset(link, 0, sizeof(link)); + memset(conn, 0, sizeof(conn)); + num = step_fro = 0; + step_back = 1000; + + //对图进行输入并处ç†ï¼Œå»ºç«‹å¯ç§»åŠ¨åŒºåŸŸçš„图 + for (int i = 0; i < h; ++i) + gets(map_[i]); + + for (int i = 0; i < h; ++i) + for (int j = 0; j < w; ++j) + if (map_[i][j] != '#') + link[i][j] = ++num; + for (int i = 0; i < h; ++i) + for (int j = 0; j < w; ++j) + { + if (link[i][j]) + { + for (int k = 0; k < 4; ++k) + { + if (pos_nxt) + conn[pos_now][++conn[pos_now][0]] = pos_nxt; + } + conn[pos_now][0] = pos_now; + } + } + + beg[0] = beg[1] = beg[2] = end_[0] = end_[1] = end_[2] = 303; + conn[303][0] = 303; + for (int i = 0; i < h; ++i) + for (int j = 0; j < w; ++j) + { + if (islower(map_[i][j])) + beg[map_[i][j] - 'a'] = pos_now; + else if (isupper(map_[i][j])) + end_[map_[i][j] - 'A'] = pos_now; + } + bfs(); + printf("%d\n", step_fro + step_back - 1002); + } + return 0; +} diff --git a/1602.cpp b/1602.cpp new file mode 100644 index 0000000..976918e --- /dev/null +++ b/1602.cpp @@ -0,0 +1,214 @@ +#include + +using namespace std; + +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +int g[64][64], used[64][64], n, m; +pair path[64], pre[64][64]; + +struct Area +{ + vector > V; + bool operator<(const Area &a) const + { + for (int i = 0; i < V.size(); i++) + { + if (V[i] != a.V[i]) + { + return V[i] < a.V[i]; + } + } + return false; + } + + Area() + { + V.clear(); + } + + Area rot() + { + Area ret = (*this); + for (int i = 0; i < V.size(); i++) + { + int x = ret.V[i].first, y = ret.V[i].second; + ret.V[i].first = -y; + ret.V[i].second = x; + } + return ret.norm(); + } + + Area flip() + { + Area ret = (*this); + for (int i = 0; i < V.size(); i++) + { + int x = ret.V[i].first, y = ret.V[i].second; + ret.V[i].first = -x; + ret.V[i].second = y; + } + return ret.norm(); + } + + Area norm() + { + sort(V.begin(), V.end()); + int x = V[0].first, y = V[0].second, mx = x, my = y; + for (int i = 0; i < V.size(); i++) + { + x = min(x, V[i].first); + y = min(y, V[i].second); + mx = max(mx, V[i].first); + my = max(my, V[i].second); + } + for (int i = 0; i < V.size(); i++) + { + V[i].first -= x; + V[i].second -= y; + } + sort(V.begin(), V.end()); + return (*this); + } + + void print() const + { + for (int i = 0; i < V.size(); i++) + { + printf("(%d %d)", V[i].first, V[i].second); + } + printf("\n"); + } +}; + +// # fixed polyominoes with n cells @ https://oeis.org/A001168 +set shape[16][16][16]; +Area min_exp(Area a) +{ + Area ret = a; + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 4; j++) + { + ret = min(ret, a); + a = a.rot(); + } + a = a.flip(); + } + return ret; +} + +void store_area(Area a) +{ + sort(a.V.begin(), a.V.end()); + int x = a.V[0].first, y = a.V[0].second, mx = x, my = y; + for (int i = 0; i < a.V.size(); i++) + { + x = min(x, a.V[i].first); + y = min(y, a.V[i].second); + mx = max(mx, a.V[i].first); + my = max(my, a.V[i].second); + } + for (int i = 0; i < a.V.size(); i++) + { + a.V[i].first -= x; + a.V[i].second -= y; + } + sort(a.V.begin(), a.V.end()); + if (mx - x + 1 <= my - y + 1) + { + shape[a.V.size()][mx - x + 1][my - y + 1].insert(a); + } + // assert(mx - x >= 0 && mx - x < 16 && my - y >= 0 && my - y < 16); +} + +void dfs(int idx, int x, int y, int pick, int m) +{ + if (pick == m) + { + Area t; + for (int i = 0; i < pick; i++) + { + t.V.push_back(path[i]); + } + store_area(t); + return; + } + if (idx >= 2 * m) + { + return; + } + vector > test; + for (int i = 0; i < 4; i++) + { + int tx = x + dx[i], ty = y + dy[i]; + if (used[tx][ty]) + { + continue; + } + pre[tx][ty] = make_pair(x, y); + path[pick] = make_pair(tx, ty); + used[tx][ty] = 1; + dfs(idx + 1, tx, ty, pick + 1, m); + test.push_back(make_pair(tx, ty)); + } + if (pre[x][y].first != -1)// stop on leaf + { + dfs(idx + 1, pre[x][y].first, pre[x][y].second, pick, m); + } + for (int i = 0; i < test.size(); i++) + { + int tx = test[i].first, ty = test[i].second; + used[tx][ty] = 0; + } +} + +int main() +{ + for (int i = 1; i <= 10; i++) + { + for (int j = 0; j < 32; j++) + { + for (int k = 0; k < 32; k++) + { + pre[j][k] = make_pair(-1, -1); + } + } + pre[11][11] = make_pair(-1, -1); + path[0] = make_pair(11, 11); + used[11][11] = 1; + dfs(1, 11, 11, 1, i); + used[11][11] = 0; + int sum = 0; + for (int j = 0; j < i; j++) + { + for (int k = 0; k < i; k++) + { + sum += shape[i][j][k].size(); + } + } + } + int N, H, W; + while (scanf("%d%d%d", &N, &H, &W) == 3) + { + set ret; + if (H > W) + { + swap(H, W); + } + for (int i = 1; i <= H; i++) + { + for (int j = i; j <= W; j++) + { + for (set::iterator it = shape[N][i][j].begin(); + it != shape[N][i][j].end(); it++) + { + ret.insert(min_exp(*it)); + } + } + } + printf("%d\n", (int)ret.size()); + } + return 0; +} diff --git a/1603.cpp b/1603.cpp new file mode 100644 index 0000000..d8297e9 --- /dev/null +++ b/1603.cpp @@ -0,0 +1,229 @@ +#include + +using namespace std; + +#define MAXV 0x3f3f3f3f +#define MAXE 1048576 +#define MAXC 1048576 +#define MAXR 1024 + +class DLX +{ +public: + struct DacingLinks + { + int left, right, up, down, ch, rh, data;// extra info + } DL[MAXE]; + + int s[MAXC], o[MAXR], used[MAXC] = {}, head, size, ans, find_flag; + + void init(int m) + { + size = 0; + head = new_node(0, 0, 0, 0); + int i; + for (i = 1; i <= m; i++) + { + new_node(i, i, DL[head].left, head); + DL[i].ch = i, s[i] = 0; + } + } + + void rm(int c) + { + static int i; + for (i = DL[c].down; i != c; i = DL[i].down) + { + DL[DL[i].right].left = DL[i].left; + DL[DL[i].left].right = DL[i].right; + s[DL[i].ch]--; + } + } + + void resume(int c) + { + static int i; + for (i = DL[c].down; i != c; i = DL[i].down) + { + DL[DL[i].right].left = i; + DL[DL[i].left].right = i; + s[DL[i].ch]++; + } + } + + int H() + { + static int c, ret, i, j, time = 0; + for (c = DL[head].right, ++time, ret = 0; c != head; c = DL[c].right) + { + if (used[c] != time) + { + ret++, used[c] = time; + for (i = DL[c].down; i != c; i = DL[i].down) + { + for (j = DL[i].right; j != i; j = DL[j].right) + { + used[DL[j].ch] = time; + } + } + } + } + return ret; + } + + void dfs(int k) + { + if (k + H() >= ans) + { + return; + } + if (DL[head].right == head) + { + ans = min(ans, k); + return; + } + int t = MAXV, c = 0, i, j; + for (i = DL[head].right; i != head; i = DL[i].right) + { + if (s[i] < t) + { + t = s[i]; + c = i; + } + } + for (i = DL[c].down; i != c; i = DL[i].down) + { + o[k] = i; + rm(i); + for (j = DL[i].right; j != i; j = DL[j].right) + { + rm(j); + } + dfs(k + 1); + for (j = DL[i].left; j != i; j = DL[j].left) + { + resume(j); + } + resume(i); + if (find_flag) + { + break; + } + } + } + + int new_node(int up, int down, int left, int right) + { + // assert(size < MAXE); + DL[size].up = up, DL[size].down = down; + DL[size].left = left, DL[size].right = right; + DL[up].down = DL[down].up = DL[left].right = DL[right].left = size; + return size++; + } + + void add_row(int n, int row[], int data) + { + int a, r, _row = -1, k; + for (a = 0; a < n; a++) + { + r = row[a]; + DL[size].ch = r, s[r]++; + DL[size].data = data; + if (_row == -1) + { + _row = new_node(DL[DL[r].ch].up, DL[r].ch, size, size); + DL[_row].rh = a; + } + else + { + k = new_node(DL[DL[r].ch].up, DL[r].ch, DL[_row].left, _row); + DL[k].rh = a; + } + } + } +} dlx; + +int main() +{ + int T, n, m, x; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + // assert(n * n < MAXC); + int lbl = 0, cover_col = 0, lbl_h[16][16], lbl_v[16][16], enable[2048]; + vector g[2048]; + for (int i = 1; i <= n + 1; i++) + { + for (int j = 1; j <= n; j++) + { + lbl_h[i][j] = ++lbl; + enable[lbl] = 1; + } + for (int j = 1; j <= n + 1; j++) + { + lbl_v[i][j] = ++lbl; + enable[lbl] = 1; + } + } + for (int k = 0; k <= n; k++) + { + for (int i = 1; i + k <= n; i++) + { + for (int j = 1; j + k <= n; j++) + { + cover_col++; + for (int l = 0; l <= k; l++) + { + g[lbl_h[i][j + l]].push_back(cover_col); + g[lbl_h[i + 1 + k][j + l]].push_back(cover_col); + g[lbl_v[i + l][j]].push_back(cover_col); + g[lbl_v[i + l][j + 1 + k]].push_back(cover_col); + } + } + } + } + dlx.init(cover_col); + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + enable[x] = 0; + } + int cover[2048] = {}, row[2028], pre_cover = 0, row_sz = 0; + for (int i = 1; i <= lbl; i++) + { + if (!enable[i] || g[i].size() == 0) + { + for (int j = 0; j < g[i].size(); j++) + { + cover[g[i][j]] = 1; + } + continue; + } + row_sz = 0; + for (int j = 0; j < g[i].size(); j++) + { + row[row_sz++] = g[i][j]; + } + dlx.add_row(row_sz, row, i); + } + row_sz = 0; + for (int i = 1; i <= cover_col; i++) + { + if (cover[i]) + { + row[row_sz++] = i; + pre_cover = 1; + } + } + if (pre_cover) + { + dlx.add_row(row_sz, row, lbl + 1); + } + dlx.ans = 0x3f3f3f3f; + dlx.dfs(0); + printf("%d\n", dlx.ans - pre_cover); + } + return 0; +} diff --git a/1604.cpp b/1604.cpp new file mode 100644 index 0000000..f7309fe --- /dev/null +++ b/1604.cpp @@ -0,0 +1,247 @@ +#include + +using namespace std; + +#define MAXSTATE (1048576 << 2) +#define EMPTY 0 +#define RED 1 +#define BLUE 2 +#define WHITE 3 +#define HASH_MOD 1000007 +#define MAXL ((1 << 27) >> 5) + 1 +#define GET(x) (used[x >> 5] >> (x & 31) & 1) +#define SET(x) (used[x >> 5] |= 1 << (x & 31)) + +const int relabel[4][4] = {{0}, {-1, -1, 1, 2}, {-1, 3, -1, 4}, {-1, 5, 6, -1}}; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +int used[MAXL], finalA[4][4], state_sz = 0; + +struct State +{ + unsigned long long mask; + int x, y, dist; + State(unsigned long long a = 0, int b = 0, int c = 0) + : mask(a), x(b), y(c) + { + } + unsigned int hash() + { + unsigned int v = mask % HASH_MOD; + return v; + } + int encode() + { + int ret = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + long long dice = (mask >> ((i * 3 + j) * 4)) & 15; + int top = (dice >> 2) & 3, front = (dice)&3; + ret |= relabel[top][front] << ((i * 3 + j) * 3); + } + } + return ret; + } + bool operator<(const State &a) const + { + if (mask != a.mask) + { + return mask < a.mask; + } + return false; + } +} states[MAXSTATE]; + +char to_lbl(int c) +{ + switch (c) + { + case EMPTY: + return 'E'; + case RED: + return 'R'; + case WHITE: + return 'W'; + case BLUE: + return 'B'; + } + return '#'; +} + +void print_state(State u) +{ + for (int i = 0; i < 3; i++, printf("\n")) + { + for (int j = 0; j < 3; j++) + { + long long dice = (u.mask >> ((i * 3 + j) * 4)) & 15; + int top = (dice >> 2) & 3, front = (dice)&3; + printf("(%c, %c) ", to_lbl(front), to_lbl(top)); + } + } + printf("--\n"); +} + +void rot(unsigned long long top, unsigned long long front, int dir, unsigned long long &a, unsigned long long &b) +{ + // a: nxt top, b: nxt front + if (dir == 0 || dir == 1) + { + b = front, a = 6 - top - front; + } + else + { + a = front, b = top; + } +} + +int is_end(State u) +{ + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + long long dice = (u.mask >> ((i * 3 + j) * 4)) & 15; + long long top = (dice >> 2) & 3; + if (top != finalA[i][j]) + { + return 0; + } + } + } + return 1; +} + +int H(State u) +{ + int ret = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + long long dice = (u.mask >> ((i * 3 + j) * 4)) & 15; + long long top = (dice >> 2) & 3; + if (top != finalA[i][j]) + { + ret++; + } + } + } + return ret - 1; +} + +void bfs(int emx, int emy) +{ + memset(used, 0, sizeof(used)); + unsigned long long mask = 0; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + if (i == emx && j == emy) + { + continue; + } + long long dice = RED | (WHITE << 2);// front/top + mask |= dice << ((i * 3 + j) * 4); + } + } + State *u, *v, t; + queue Q; + int tx, ty, tval; + state_sz = 0; + u = &states[state_sz++]; + *u = State(mask, emx, emy); + u->dist = 0; + tval = u->encode(); + Q.push(u), SET(tval); + if (is_end(*u)) + { + printf("0\n"); + return; + } + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + // assert (u->dist <= 30 && state_sz <= MAXSTATE); + for (int i = 0; i < 4; i++) + { + tx = u->x + dx[i], ty = u->y + dy[i]; + if (tx < 0 || ty < 0 || tx >= 3 || ty >= 3) + { + continue; + } + unsigned long long dice = (u->mask >> ((tx * 3 + ty) * 4)) & 15; + unsigned long long top = (dice >> 2) & 3, front = (dice)&3, a, b; + rot(top, front, i, a, b); + mask = (u->mask) ^ (dice << ((tx * 3 + ty) * 4)); + dice = b | (a << 2); + mask |= (dice) << (((u->x) * 3 + (u->y)) * 4); + t = State(mask, u->x, u->y), t.dist = u->dist + 1; + t.x = tx, t.y = ty, tval = t.encode(); + // assert(mask >= 0); + if (t.dist + H(t) > 30 || GET(tval)) + { + continue; + } + SET(tval); + v = &states[state_sz++]; + *v = t; + if (is_end(*v)) + { + printf("%d\n", v->dist); + return; + } + Q.push(v); + } + } + printf("-1\n"); +} + +int main() +{ + char grid[4][4][4]; + int sx, sy; + while (scanf("%d%d", &sy, &sx) == 2 && sx) + { + sx--; + sy--; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + scanf("%s", grid[i][j]); + } + } + + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + if (grid[i][j][0] == 'E') + { + finalA[i][j] = EMPTY; + } + else if (grid[i][j][0] == 'R') + { + finalA[i][j] = RED; + } + else if (grid[i][j][0] == 'B') + { + finalA[i][j] = BLUE; + } + else if (grid[i][j][0] == 'W') + { + finalA[i][j] = WHITE; + } + } + } + + bfs(sx, sy); + } + return 0; +} diff --git a/1605.cpp b/1605.cpp new file mode 100644 index 0000000..123cbf4 --- /dev/null +++ b/1605.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +// ignore Print a blank line between test cases. +int main() +{ + int n, cases = 0; + while (scanf("%d", &n) == 1) + { + printf("%d %d %d\n", 2, n, n); + for (int i = 0; i < n; i++, puts("")) + for (int j = 0; j < n; j++) + putchar(i < 26 ? i + 'a' : i - 26 + 'A'); + puts(""); + for (int i = 0; i < n; i++, puts("")) + for (int j = 0; j < n; j++) + putchar(j < 26 ? j + 'a' : j - 26 + 'A'); + puts(""); + } + return 0; +} diff --git a/1606.cpp b/1606.cpp new file mode 100644 index 0000000..b4a0ac6 --- /dev/null +++ b/1606.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +const int MAXN = 1010; + +int n, color[MAXN]; + +struct Point +{ + int x, y; + Point(int x = 0, int y = 0) + : x(x), y(y) + { + } + double rad; + bool operator<(const Point &rhs) const + { + return rad < rhs.rad; + } +} op[MAXN], p[MAXN]; + +Point operator-(const Point &A, const Point &B) +{ + return Point(A.x - B.x, A.y - B.y); +} + +int cross(const Point &A, const Point &B) +{ + return A.x * B.y - A.y * B.x; +} + +int solve() +{ + int ans = 0; + for (int i = 0; i < n; ++i) + { + int k = 0; + for (int j = 0; j < n; ++j) + { + if (i != j) + { + p[k] = op[j] - op[i]; + if (color[j]) + { + p[k].x = -p[k].x; + p[k].y = -p[k].y; + } + p[k].rad = atan2(p[k].y, p[k].x); + k++; + } + } + sort(p, p + k); + int L = 0, R = 0, cnt = 2; + for (; L < k; ++L) + { + if (L == R) + { + R = (R + 1) % k; + cnt++; + } + while (L != R && cross(p[L], p[R]) >= 0) + { + R = (R + 1) % k; + cnt++; + } + cnt--; + ans = max(ans, cnt); + } + } + return ans; +} + +int main() +{ + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; ++i) + { + scanf("%d%d%d", &op[i].x, &op[i].y, &color[i]); + } + printf("%d\n", solve()); + } + return 0; +} diff --git a/1607.cpp b/1607.cpp new file mode 100644 index 0000000..30607ee --- /dev/null +++ b/1607.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +#define MAXM 262144 +#define MAXN 131072 + +int IN1[MAXM], IN2[MAXM], VAL[MAXM], n, m; + +int calc(int div) +{ + for (int i = 1; i <= m; i++) + { + int a = IN1[i], b = IN2[i], va, vb; + va = a < 0 ? -a > div : VAL[a]; + vb = b < 0 ? -b > div : VAL[b]; + VAL[i] = !(va & vb); + } + return VAL[m]; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &n, &m); + for (int i = 1; i <= m; i++) + { + scanf("%d%d", &IN1[i], &IN2[i]); + } + int v0 = calc(0), v1 = calc(n); + if (v0 == v1) + { + for (int i = 0; i < n; i++) + { + printf("0"); + } + printf("\n"); + } + else + { + int l = 1, r = n + 1, mid, ret = 1; + while (l <= r) + { + mid = (l + r) / 2; + if (calc(mid) == v1) + { + r = mid - 1; + ret = mid; + } + else + { + l = mid + 1; + } + } + for (int i = 1; i < ret; i++) + { + printf("0"); + } + printf("x"); + for (int i = ret + 1; i <= n; i++) + { + printf("1"); + } + printf("\n"); + } + } + return 0; +} diff --git a/1608.cpp b/1608.cpp new file mode 100644 index 0000000..2711844 --- /dev/null +++ b/1608.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int A[262144]; +int NEXT[262144], PREV[262144]; +map R; +int dfs(int l, int r) +{ + if (l >= r) + return 1; + for (int i = 0; i <= (r - l + 1) / 2; i++) + { + if (NEXT[l + i] > r && PREV[l + i] < l) + return dfs(l, l + i - 1) && dfs(l + i + 1, r); + if (NEXT[r - i] > r && PREV[r - i] < l) + return dfs(l, r - i - 1) && dfs(r - i + 1, r); + } + return 0; +} +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d", &A[i]); + NEXT[i] = n + 1; + PREV[i] = 0; + } + R.clear(); + for (int i = 1; i <= n; i++) + { + PREV[i] = R[A[i]], NEXT[PREV[i]] = i; + R[A[i]] = i; + } + puts(dfs(1, n) ? "non-boring" : "boring"); + } + return 0; +} +/* +20 +6 +1 2 3 1 2 3 +5 +1 2 3 4 5 +5 +1 1 1 1 1 +5 +1 2 3 2 1 +5 +1 1 2 1 1 +*/ diff --git a/1609.cpp b/1609.cpp new file mode 100644 index 0000000..8e2d4c5 --- /dev/null +++ b/1609.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define MAXN (1024 + 5) + +char sg[MAXN][MAXN]; +int N; +vector g[MAXN]; + +void ad_hoc() +{ + for (int i = 0; i < N; i++) + { + g[i].clear(); + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (sg[i][j] == '1') + { + g[i].push_back(j); + } + } + } + set A, B; + for (int i = 1; i < N; i++) + { + if (sg[0][i] == '1') + { + A.insert(i); + } + else + { + B.insert(i); + } + } + for (int i = 1, M = N; i < N; i *= 2, M /= 2) + { + set nA, nB; + vector lA, lB; + for (set::iterator it = A.begin(); it != A.end();) + { + if (A.size() == 1) + { + break; + } + int x = *it, match = 0; + for (int j = 0; j < g[x].size(); j++) + { + if (B.count(g[x][j])) + { + printf("%d %d\n", x + 1, g[x][j] + 1); + B.erase(g[x][j]), nA.insert(x); + match = 1; + break; + } + } + if (match) + { + A.erase(it++); + } + else + { + it++; + } + } + for (int j = 0; j < g[0].size(); j++) + { + if (A.count(g[0][j])) + { + printf("%d %d\n", 0 + 1, g[0][j] + 1); + A.erase(g[0][j]); + break; + } + } + for (set::iterator it = A.begin(); it != A.end(); it++) + { + lA.push_back(*it); + } + for (set::iterator it = B.begin(); it != B.end(); it++) + { + lB.push_back(*it); + } + for (int i = 0; i + 1 < lB.size(); i += 2) + { + int x = lB[i], y = lB[i + 1]; + if (sg[x][y] == '1') + { + nB.insert(x); + printf("%d %d\n", x + 1, y + 1); + } + else + { + nB.insert(y); + printf("%d %d\n", y + 1, x + 1); + } + } + for (int i = 0; i + 1 < lA.size(); i += 2) + { + int x = lA[i], y = lA[i + 1]; + if (sg[x][y] == '1') + { + nA.insert(x); + printf("%d %d\n", x + 1, y + 1); + } + else + { + nA.insert(y); + printf("%d %d\n", y + 1, x + 1); + } + } + if (lB.size() % 2) + { + // assert(lB.size() % 2 && lA.size() % 2); + int x = lA[lA.size() - 1], y = lB[lB.size() - 1]; + if (sg[x][y] == '1') + { + nA.insert(x); + printf("%d %d\n", x + 1, y + 1); + } + else + { + nB.insert(y); + printf("%d %d\n", y + 1, x + 1); + } + } + A = nA; + B = nB; + } +} + +int main() +{ + while (scanf("%d", &N) == 1) + { + for (int i = 0; i < N; i++) + { + scanf("%s", sg[i]); + } + ad_hoc(); + } + return 0; +} diff --git a/161.cpp b/161.cpp index cf0f724..961a06e 100644 --- a/161.cpp +++ b/161.cpp @@ -1,65 +1,58 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define INF 1<<30 +using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int I; - vector< int > V(2); - - cin >> V[0] >> V[1]; - while (cin >> I && I != 0) V.push_back(I); - if (V.size() == 2 && !V[0] && !V[1]) break; - - int N = V.size(); - int Min = *min_element(V.begin(), V.end()) - 5; - bool flag = false; - - for (int i = Min; i <= 18000; i++) { - int Cnt = 0; - for (int j = 0; j < N; j++) { - if (i % (2 * V[j]) < V[j] - 5) ++Cnt; - else break; +int main() +{ + int M[105], L[105], n, i, j, k, sec, ok; + while (scanf("%d%d", M, M + 1) == 2) + { + L[0] = M[0] - 5, L[1] = M[1] - 5; + M[0] <<= 1, M[1] <<= 1; + for (n = 2; scanf("%d", M + n); n++) + { + if (!M[n]) + { + break; + } + L[n] = M[n] - 5; + M[n] <<= 1; + } + if (n == 2 && (M[0] | M[1] | M[2]) == 0) + { + break; + } + for (i = 0, sec = 10000; i < n; i++) + { + if (M[i] < sec) + { + sec = M[i]; + } + } + while (sec <= 18000) + { + for (i = 0, ok = 1; i < n; i++) + { + if (sec % M[i] >= L[i]) + { + ok = 0; + sec = sec - sec % M[i] + M[i]; + break; + } } - if (Cnt == N) { - int S = i % 60; i /= 60; - int M = i % 60; i /= 60; - int H = i % 24; - printf("%02d:%02d:%02d\n", H, M, S); - flag = true; + if (ok) + { break; } } - if (!flag) + if (sec > 18000) + { printf("Signals fail to synchronise in 5 hours\n"); - //cout << "Iteration Complete !" << endl; + } + else + { + printf("%02d:%02d:%02d\n", sec / 3600, sec / 60 % 60, sec % 60); + } } return 0; } diff --git a/1610.cpp b/1610.cpp new file mode 100644 index 0000000..11b6dbe --- /dev/null +++ b/1610.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +int main() +{ + int n; + char s[1024]; + while (scanf("%d", &n) == 1 && n) + { + vector A; + for (int i = 0; i < n; i++) + scanf("%s", s), A.push_back(s); + sort(A.begin(), A.end()); + string p = A[n / 2 - 1], q = A[n / 2]; + if (p.length() < q.length()) + { + for (int i = 0; i < p.length(); i++) + { + if (p[i] == q[i] || i == p.length() - 1)// equal. + putchar(p[i]); + else + {// less + putchar(p[i] + 1); + break; + } + } + } + else + { + for (int i = 0; i < q.length(); i++) + { + if (p[i] == q[i]) + putchar(p[i]); + else + { + if (i == q.length() - 1) + { + if (i == p.length() - 1) + {// equal + putchar(p[i]); + break; + } + if (p[i] + 1 != q[i]) + { + putchar(p[i] + 1); + break; + } + else + { + putchar(p[i]); + for (int j = i + 1; j < p.length(); j++) + { + if (j == p.length() - 1)// equal + putchar(p[j]); + else if (p[j] != 'Z') + { + putchar(p[j] + 1); + break; + } + else + putchar(p[j]); + } + break; + } + } + else + { + putchar(p[i] + 1); + break; + } + } + } + } + puts(""); + } + return 0; +} +/* +4 +FRED +SAM +JOE +MARGARET +2 +FRED +FREDDIE +2 +JOSEPHINE +JERRY +2 +LARHONDA +LARSEN +2 +ABA +AC +2 +ABCA +AC +2 +ABZA +AC +2 +ABZCA +AC +0 +*/ diff --git a/1611.cpp b/1611.cpp new file mode 100644 index 0000000..1db011e --- /dev/null +++ b/1611.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +#define MAXN 131072 + +int A[MAXN], R[MAXN], N; +vector > ret; + +void swap_interval(int l, int r) +{ + // assert(0 <= l && l < N && 0 <= r && r < N); + // assert((r - l + 1) % 2 == 0); + ret.push_back(make_pair(l, r)); + int mid = (l + r) / 2; + for (int i = l, j = mid + 1; i <= mid; i++, j++) + { + swap(R[A[i]], R[A[j]]); + swap(A[i], A[j]); + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", &A[i]); + A[i]--; + R[A[i]] = i; + } + ret.clear(); + for (int i = 0; i < N; i++) + { + if (R[i] == i) + { + continue; + } + int div = (N + i) / 2; + if (R[i] > div) + {// need swap into [i, (N+i)/2] + swap_interval(div - (R[i] - div - 1), R[i]); + } + swap_interval(i, R[i] + (R[i] - i - 1)); + } + printf("%d\n", (int)ret.size()); + for (int i = 0; i < ret.size(); i++) + { + printf("%d %d\n", ret[i].first + 1, ret[i].second + 1); + } + } + return 0; +} diff --git a/1612.cpp b/1612.cpp new file mode 100644 index 0000000..d222dd4 --- /dev/null +++ b/1612.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +const int MAXN = 16384 + 5; +const int INF = 0x3f3f3f3f; + +struct node +{ + int score[3], id, ran; + int sum() + { + return score[0] + score[1] + score[2]; + } + bool operator<(const node &p) const + { + return ran < p.ran; + } +}; +node a[MAXN]; + +bool solve(int n, int val, bool ok) +{ + int num[10]; + vector v[10]; + for (int i = 0; i < 8; ++i) + { + num[i] = 0; + for (int j = 0; j < 3; ++j) + { + if (i & (1 << j)) + { + num[i] += a[n].score[j]; + v[i].push_back(j); + } + } + } + int m = INF, t = -1; + for (int i = 0; i < 8; ++i) + { + if ((ok && num[i] >= val) || (num[i] > val)) + { + if (num[i] < m) + { + m = num[i]; + t = i; + } + } + } + if (t == -1) + { + return false; + } + for (int i = 0; i < v[t].size(); ++i) + { + a[n].score[v[t][i]] = 0; + } + return true; +} + +int main() +{ + + int n, t = 0; + while (scanf("%d", &n) == 1 && n) + { + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < 3; ++j) + { + double x; + scanf("%lf", &x); + a[i].score[j] = (int)round(x * 100.0f); + } + } + int x; + for (int i = 0; i < n; ++i) + { + scanf("%d", &x); + a[x - 1].ran = i; + a[x - 1].id = x; + } + sort(a, a + n); + bool ok = true; + for (int i = 1; i < n; ++i) + { + int s = a[i].sum(), t = a[i - 1].sum(); + if (s < t || (s == t && a[i].id > a[i - 1].id)) + { + continue; + } + if (!solve(i, s - t, a[i].id > a[i - 1].id)) + { + ok = false; + break; + } + } + printf("Case %d: ", ++t); + ok ? printf("%.2lf\n", (double)a[n - 1].sum() / 100.0f) : printf("No solution\n"); + } + return 0; +} diff --git a/1613.cpp b/1613.cpp new file mode 100644 index 0000000..83e80da --- /dev/null +++ b/1613.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAXN 32767 + +int color[MAXN], used[MAXN], visited[MAXN], N, M, K; +pair A[MAXN]; +vector g[MAXN]; +vector topo; + +void dfs(int u) +{ + visited[u] = 1; + for (int i = 0; i < g[u].size(); i++) + { + if (visited[g[u][i]] == 0) + { + dfs(g[u][i]); + } + } + topo.push_back(u); +} + +int rand_tst() +{ + for (int i = 0; i < N; i++) + { + color[i] = -1; + used[i] = 0; + visited[i] = 0; + } + topo.clear(); + for (int i = 0; i < N; i++) + { + if (visited[i] == 0) + { + dfs(i); + } + } + int testcase = 0; + for (int i = N - 1; i >= 0; i--) + { + int x = topo[i]; + testcase++; + for (int j = 0; j < g[x].size(); j++) + { + if (color[g[x][j]] >= 0) + { + used[color[g[x][j]]] = testcase; + } + } + for (int j = 0; j < K; j++) + { + if (used[j] != testcase) + { + color[x] = j; + } + } + assert(color[x] >= 0); + if (color[x] < 0) + { + return 0; + } + } + return 1; +} + +int main() +{ + int cases = 0, x, y; + while (scanf("%d%d", &N, &M) == 2 && N) + { + for (int i = 0; i < N; i++) + { + g[i].clear(); + } + for (int i = 0; i < M; i++) + { + scanf("%d%d", &x, &y); + x--; + y--; + g[x].push_back(y); + g[y].push_back(x); + } + K = 0; + for (int i = 0; i < N; i++) + { + K = max(K, (int)g[i].size()); + } + if (K % 2 == 0) + { + K++; + } + rand_tst(); + if (cases++) + { + printf("\n"); + } + printf("%d\n", K); + for (int i = 0; i < N; i++) + { + printf("%d\n", color[i] + 1); + } + } + return 0; +} diff --git a/1614.cpp b/1614.cpp new file mode 100644 index 0000000..8ee309e --- /dev/null +++ b/1614.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +pair A[100000]; +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + long long sum = 0; + int ret[100000] = {}; + for (int i = 0; i < n; i++) + { + scanf("%d", &A[i].first); + A[i].second = i; + sum += A[i].first; + } + if (sum % 2) + { + puts("No"); + } + else + { + puts("Yes"); + sum /= 2; + sort(A, A + n); + for (int i = n - 1; i >= 0; i--) + { + if (sum >= A[i].first) + sum -= A[i].first, ret[A[i].second] = 1; + else + ret[A[i].second] = -1; + } + for (int i = 0; i < n; i++) + printf("%d%c", ret[i], i == n - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/1615.cpp b/1615.cpp new file mode 100644 index 0000000..83eaae8 --- /dev/null +++ b/1615.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +const int MAXN = 1e5 + 5; + +struct node +{ + double l, r; + bool operator<(const node &p) const + { + return r < p.r || (r == p.r && l > p.l); + } +}; +node a[MAXN]; + +int main() +{ + double x, y, l, d; + int n; + while (scanf("%lf", &l) == 1) + { + scanf("%lf%d", &d, &n); + for (int i = 0; i < n; ++i) + { + scanf("%lf%lf", &x, &y); + a[i].l = x - sqrt(d * d - y * y); + a[i].r = x + sqrt(d * d - y * y); + } + sort(a, a + n); + int cnt = 1; + double ans = a[0].r; + for (int i = 0; i < n; ++i) + { + if (a[i].l > ans) + { + ++cnt; + ans = a[i].r; + } + } + printf("%d\n", cnt); + } + return 0; +} diff --git a/1616.cpp b/1616.cpp new file mode 100644 index 0000000..847675c --- /dev/null +++ b/1616.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define EPS 1e-10 +#define MAXN 131072 + +pair D[MAXN]; + +int main() +{ + int N, x, y; + while (scanf("%d", &N) == 1) + { + for (int i = 0; i < N; i++) + { + scanf("%d%d", &x, &y); + D[i] = make_pair(x, y); + } + sort(D, D + N); + double l = 0.0f, r = 1000000.0f, mid, ret = 0.0f; + for (int it = 0; it < 100; it++) + { + mid = (l + r) / 2; + double left = 0.0f; + int ok = 1; + for (int i = 0; i < N && ok; i++) + { + if (D[i].first > left) + { + left = D[i].first; + } + if (left + mid > D[i].second) + { + ok = 0; + } + left += mid; + } + if (ok) + { + l = mid; + ret = mid; + } + else + { + r = mid; + } + } + int rp = 0, rq = 1; + for (int p, q = 1; q <= N; q++) + { + p = round(ret * q); + if (fabs((double)p / q - ret) < fabs((double)rp / rq - ret)) + { + rp = p; + rq = q; + } + } + printf("%d/%d\n", rp, rq); + } + return 0; +} diff --git a/1617.cpp b/1617.cpp new file mode 100644 index 0000000..d8d330f --- /dev/null +++ b/1617.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define EPS 1e-6 + +bool cmp(pair a, pair b) +{ + if (a.second != b.second) + { + return a.second < b.second; + } + return a.first < b.first; +} + +int main() +{ + int T, N, x, y; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + vector > A; + for (int i = 0; i < N; i++) + { + scanf("%d%d", &x, &y); + A.push_back(make_pair(x, y)); + } + sort(A.begin(), A.end(), cmp); + int ret = 1, right = A[0].second; + for (int i = 1; i < N; i++) + { + if (right != A[i].second) + { + if (right < A[i].first) + { + ret++, right = A[i].second; + } + else + { + right++;// greedy + } + } + } + printf("%d\n", ret - 1); + } + return 0; +} diff --git a/1618.cpp b/1618.cpp new file mode 100644 index 0000000..d083e3e --- /dev/null +++ b/1618.cpp @@ -0,0 +1,240 @@ +#include +#include + +using namespace std; + +#define pb push_back +#define all(x) x.begin(), x.end() +#define ms(a, v) memset(a, v, sizeof a) +#define II ({int a; scanf("%d", &a); a; }) +#define LL ({Long a; scanf("%lld", &a); a; }) +#define DD ({double a; scanf("%lf", &a); a; }) +#define ff first +#define ss second +#define mp make_pair +#define gc getchar +#define EPS 1e-10 +#define pi 3.1415926535897932384626433832795 + +#define FI freopen("in.txt", "r", stdin) +#define FO freopen("out.txt", "w", stdout) + +typedef long long Long; +typedef unsigned long long ull; +typedef vector vi; +typedef set si; +typedef vector vl; +typedef pair pii; +typedef pair psi; +typedef pair pll; +typedef pair pdd; +typedef vector vpii; + +#define forab(i, a, b) for (__typeof(b) i = (a); i <= b; ++i) +#define rep(i, n) forab(i, 0, (n)-1) +#define For(i, n) forab(i, 1, n) +#define rofba(i, a, b) for (__typeof(b) i = (b); i >= a; --i) +#define per(i, n) rofba(i, 0, (n)-1) +#define rof(i, n) rofba(i, 1, n) +#define forstl(i, s) for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i) + +#define __(args...) \ + { \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << "\t"; + return *this; + } +} dbg; +#define __1D(a, n) \ + rep(i, n) \ + { \ + if (i) \ + printf(" "); \ + cout << a[i]; \ + } +#define __2D(a, r, c, f) \ + forab(i, f, r - !f) \ + { \ + forab(j, f, c - !f) \ + { \ + if (j != f) \ + printf(" "); \ + cout << a[i][j]; \ + } \ + cout << endl; \ + } + +template +ostream &operator<<(ostream &o, const pair &p) +{ + return o << "(" << p.ff << ", " << p.ss << ")"; +}//Pair print +template +ostream &operator<<(ostream &o, const vector &v) +{ + o << "["; + forstl(it, v) o << *it << ", "; + return o << "]"; +}//Vector print +template +ostream &operator<<(ostream &o, const set &v) +{ + o << "["; + forstl(it, v) o << *it << ", "; + return o << "]"; +}//Set print +template +inline void MAX(T &a, T b) +{ + if (a < b) + a = b; +} +template +inline void MIN(T &a, T b) +{ + if (a > b) + a = b; +} + +//Fast Reader +template +inline bool read(T &x) +{ + int c = gc(); + int sgn = 1; + while (~c && c < '0' || c > '9') + { + if (c == '-') + sgn = -1; + c = gc(); + } + for (x = 0; ~c && '0' <= c && c <= '9'; c = gc()) + x = x * 10 + c - '0'; + x *= sgn; + return ~c; +} + +// int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; // 4 direction +// int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1}; // 8 direction +// int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1}; // knight direction +// int dx[]={2,1,-1,-2,-1,1};int dy[]={0,1,1,0,-1,-1}; // hexagonal direction +int K, A[5007]; +vi Vlo[5007]; +vi Vhi[5007]; + +bool solve1() +{ + for (int q = 0; q < K; q++) + { + for (int i1 = Vlo[q].size() - 1; i1 >= 0; i1--) + { + int s = Vlo[q][i1]; + if (q < s) + { + for (int i2 = 0; i2 < (int)Vlo[s].size(); i2++) + { + int p = Vlo[s][i2]; + if (p < q and p < s) + { + for (int i3 = Vlo[p].size() - 1; i3 >= 0; i3--) + { + int r = Vlo[p][i3]; + if (q > r) + { + break; + } + if (q < r and r < s) + { + return true; + } + } + } + else + break; + } + } + else + break; + } + } + return false; +} + +bool solve2() +{ + for (int q = 0; q < K; q++) + { + for (int i1 = Vhi[q].size() - 1; i1 >= 0; i1--) + { + int s = Vhi[q][i1]; + if (q < s) + { + for (int i2 = 0; i2 < (int)Vhi[s].size(); i2++) + { + int p = Vhi[s][i2]; + if (p < q and p < s) + { + for (int i3 = Vhi[p].size() - 1; i3 >= 0; i3--) + { + int r = Vhi[p][i3]; + if (q > r) + break; + if (q < r and r < s) + return true; + // else break ; + } + } + else + break; + } + } + else + break; + } + } + return false; +} + +int main() +{ + int T; + read(T); + For(cs, T) + { + read(K); + rep(i, K) read(A[i]); + rep(i, 5001) Vlo[i].clear(), Vhi[i].clear(); + for (int i = 0; i < K; i++) + { + for (int j = 0; j < K; j++) + { + if (i == j) + continue; + if (A[j] > A[i]) + Vhi[i].pb(j); + else if (A[j] < A[i]) + Vlo[i].pb(j); + } + } + bool Ok = solve1(); + if (!Ok) + { + Ok |= solve2(); + } + if (Ok) + { + puts("YES"); + } + else + { + puts("NO"); + } + } +} diff --git a/1619.cpp b/1619.cpp new file mode 100644 index 0000000..9dafea8 --- /dev/null +++ b/1619.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXN = 100005; + +int n, a[MAXN], L[MAXN], R[MAXN]; +LL sum[MAXN]; +stack q; + +void init() +{ + sum[0] = 0; + for (int i = 1; i <= n; i++) + { + scanf("%d", &a[i]); + sum[i] = sum[i - 1] + a[i]; + } + a[0] = a[n + 1] = -0x3f3f3f3f; + while (!q.empty()) + { + q.pop(); + } + q.push(0); + for (int i = 1; i <= n; i++) + { + while (a[q.top()] >= a[i]) + { + q.pop(); + } + L[i] = q.top(); + q.push(i); + } + while (!q.empty()) + { + q.pop(); + } + q.push(n + 1); + for (int i = n; i; i--) + { + while (a[q.top()] >= a[i]) + { + q.pop(); + } + R[i] = q.top(); + q.push(i); + } +} + +int main() +{ + int T = 0; + while (scanf("%d", &n) == 1) + { + if (T++) + { + printf("\n"); + } + + init(); + + int l, r; + LL ans = 0; + for (int i = 1; i <= n; i++) + { + LL t = a[i] * (sum[R[i] - 1] - sum[L[i]]); + if (t > ans) + { + ans = t; + l = L[i] + 1; + r = R[i] - 1; + } + else if (t == ans && R[i] - L[i] - 1 < r - l + 1) + { + l = L[i] + 1; + r = R[i] - 1; + } + } + if (ans == 0) + { + printf("0\n1 1\n"); + } + else + { + printf("%lld\n%d %d\n", ans,l,r); + } + } + return 0; +} diff --git a/162.cpp b/162.cpp new file mode 100644 index 0000000..81f3257 --- /dev/null +++ b/162.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +int card2int(const string &card) +{ + switch (card[1]) + { + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return int(card[1] - '0'); + break; + case 'T': + return 10; + break; + case 'J': + return 11; + break; + case 'Q': + return 12; + break; + case 'K': + return 13; + break; + case 'A': + return 14; + break; + } +} + +int main() +{ + int tmp; + int cover; + string card; + while (cin >> card) + { + if (card == "#") + { + break; + } + deque player, dealer, middle; + bool p = true;// player starts the game, not the dealer + cover = 0; + player.push_front(card2int(card)); + for (int i = 0; i < 51; i++) + { + cin >> card; + if (i % 2 == 0) + { + dealer.push_front(card2int(card)); + } + else + { + player.push_front(card2int(card)); + } + } + while (true) + { + if (player.empty() && p || dealer.empty() && !p) + { + break; + } + //cout << "player = " << player.size() << " dealer = " << dealer.size() << " cover = " << cover << endl; + //cout << "middle = " << middle.size() << " front = " << middle.front() << endl; + if (p) + { + tmp = player.front(); + player.pop_front(); + middle.push_front(tmp); + if (tmp > 10) + { + p = false; + cover = tmp - 10; + continue; + } + if (cover > 0) + { + if (cover == 1) + { + cover = 0; + while (!middle.empty()) + { + dealer.push_back(middle.back()); + middle.pop_back(); + } + p = false; + } + else + { + cover--; + } + } + else + { + p = false; + } + } + else + { + tmp = dealer.front(); + dealer.pop_front(); + middle.push_front(tmp); + if (tmp > 10) + { + p = true; + cover = tmp - 10; + continue; + } + if (cover > 0) + { + if (cover == 1) + { + cover = 0; + while (!middle.empty()) + { + player.push_back(middle.back()); + middle.pop_back(); + } + p = true; + } + else + { + cover--; + } + } + else + { + p = true; + } + } + } + if (player.size() == 0) + { + printf("%d%3d\n", 1, dealer.size()); + } + else + { + printf("%d%3d\n", 2, player.size()); + } + } + return 0; +} diff --git a/1620.cpp b/1620.cpp new file mode 100644 index 0000000..896902c --- /dev/null +++ b/1620.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +// (A, B, C, D) => (D, C, B, A) +// #inverse pair = x => 6-x +// final state #inverse pair = 0, in a time delta = 6-2x, is even. +// then, init #inverse pair must even. + +int BIT[1024]; + +void upd(int x, int val, int L) +{ + while (x <= L) + { + BIT[x] += val; + x += x & (-x); + } +} + +int query(int x) +{ + int ret = 0; + while (x) + { + ret += BIT[x]; + x -= x & (-x); + } + return ret; +} + +int main() +{ + int T, N, A[1024]; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", &A[i]); + A[i + N] = A[i]; + } + int ret = 0; + for (int i = 0; i < N; i++) + { + int inv = 0; + memset(BIT, 0, sizeof(BIT)); + for (int j = i; j < i + N; j++) + { + inv += query(N - A[j]); + upd(N - A[j] + 1, 1, N); + } + if (inv % 2 == 0) + { + ret = 1; + break; + } + } + printf(ret ? "possible\n" : "impossible\n"); + } + return 0; +} diff --git a/1623.cpp b/1623.cpp new file mode 100644 index 0000000..8e2f6fd --- /dev/null +++ b/1623.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define MAXN 1048576 + +int A[MAXN], B[MAXN]; + +int main() +{ + int T, N, M; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &N, &M); + for (int i = 1; i <= M; i++) + { + scanf("%d", &A[i]); + B[i] = 0; + } + map prev; + set E; + int err = 0; + for (int i = 1; i <= M && !err; i++) + { + if (A[i] > 0) + { + int &v = prev[A[i]]; + set::iterator it = E.lower_bound(v); + if (it != E.end() && *it > v) + { + B[*it] = A[i]; + E.erase(it), v = i; + } + else + { + err = 1; + } + } + else + { + E.insert(i); + } + } + printf(err ? "NO\n" : "YES\n"); + if (!err) + { + int f = 0; + for (int i = 1; i <= M; i++) + { + if (A[i] == 0) + { + if (f) + { + printf(" "); + } + f = 1; + printf("%d", B[i]); + } + } + printf("\n"); + } + } + return 0; +} diff --git a/1624.cpp b/1624.cpp new file mode 100644 index 0000000..cdb9934 --- /dev/null +++ b/1624.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +#define EPS 1e-6 +#define INF 0x3f3f3f3f +#define MAXN 1048576 + +struct Node +{ + int l, r; + int face; +} nodes[MAXN]; + +int F[MAXN], used[MAXN]; + +void rm_node(int idx) +{ + used[idx] = 1; + nodes[nodes[idx].r].l = nodes[idx].l; + nodes[nodes[idx].l].r = nodes[idx].r; +} + +int main() +{ + int T, cases = 0, L, P, x, y; + scanf("%d", &T); + while (T--) + { + scanf("%d%d", &L, &P); + for (int i = 0; i < L; i++) + { + nodes[i].l = i - 1, nodes[i].r = i + 1, F[i] = 0, used[i] = 0; + } + nodes[0].l = L - 1, nodes[L - 1].r = 0; + for (int i = 0; i < P; i++) + { + scanf("%d%d", &x, &y); + nodes[x].face = y; + nodes[y].face = x; + F[x] = 1, F[y] = -1; + } + int st = 0, lst_sz = 2 * P; + for (int i = 0; i < L; i++) + { + if (!F[i]) + { + rm_node(i); + } + } + while (lst_sz) + { + int update = 0; + while (used[st]) + { + st++; + } + for (int i = nodes[st].r; !update && i != st; i = nodes[i].r) + { + x = i, y = nodes[i].r; + if (F[x] == F[y] && (nodes[nodes[y].face].r == nodes[x].face || + nodes[nodes[x].face].r == nodes[y].face)) + { + rm_node(x), rm_node(nodes[x].face); + rm_node(y), rm_node(nodes[y].face); + lst_sz -= 4; + update = 1; + }// pass + else if (y == nodes[x].face) + { + rm_node(x), rm_node(y); + lst_sz -= 2; + update = 1; + }// self-loop + } + if (!update) + { + break; + } + } + printf("Case #%d: %s\n", ++cases, lst_sz ? "NO" : "YES"); + } + return 0; +} diff --git a/1625.cpp b/1625.cpp new file mode 100644 index 0000000..a38dd8b --- /dev/null +++ b/1625.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +#define L 5005 + +char a[L], b[L]; +int la, lb; +int dp[L][L], ta[L][26], tb[L][26]; +int isEnd(int pa, int pb, char ch) +{ + ch -= 'A'; + if (ta[pa][ch] + tb[pb][ch] == ta[la - 1][ch] + tb[lb - 1][ch]) + return pa + pb; + return 0; +} +int isBegin(int pa, int pb, char ch) +{ + ch -= 'A'; + if (ta[pa][ch] + tb[pb][ch] == 1) + return pa + pb; + return 0; +} +int main() +{ + int t, i, j; + scanf("%d", &t); + getchar(); + while (t--) + { + a[0] = b[0] = ' '; + gets(a + 1); + gets(b + 1); + la = strlen(a); + lb = strlen(b); + for (i = 0; i < 26; i++) + ta[0][i] = tb[0][i] = 0; + for (i = 1; i < la; i++) + { + for (j = 0; j < 26; j++) + ta[i][j] = ta[i - 1][j]; + ta[i][a[i] - 'A']++; + } + for (i = 1; i < lb; i++) + { + for (j = 0; j < 26; j++) + tb[i][j] = tb[i - 1][j]; + tb[i][b[i] - 'A']++; + } + for (i = 0; i < la; i++) + for (j = 0; j < lb; j++) + dp[i][j] = 1 << 30; + dp[0][0] = 0; + for (i = 0; i < la; i++) + { + for (j = 0; j < lb; j++) + { + dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + isEnd(i + 1, j, a[i + 1]) - isBegin(i + 1, j, a[i + 1])); + dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + isEnd(i, j + 1, b[j + 1]) - isBegin(i, j + 1, b[j + 1])); + } + } + printf("%d\n", dp[la - 1][lb - 1]); + } + return 0; +} diff --git a/1626.cpp b/1626.cpp new file mode 100644 index 0000000..b14da50 --- /dev/null +++ b/1626.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +int dp[128][128], arg_dp[128][128]; +char s[128]; + +void dfs(int l, int r) +{ + if (l > r) + return; + if (l == r) + { + if (s[l] == '(' || s[r] == ')') + printf("()"); + else + printf("[]"); + return; + } + int k = arg_dp[l][r]; + if (k == -1) + { + putchar(s[l]); + dfs(l + 1, r - 1); + putchar(s[r]); + } + else + { + dfs(l, k); + dfs(k + 1, r); + } +} +int main() +{ + int testcase, n; + char foo[128]; + scanf("%d", &testcase); + while (getchar() != '\n') + ; + while (getchar() != '\n') + ; + while (testcase--) + { + gets(s); + gets(foo); + + n = (int)strlen(s); + memset(dp, 0, sizeof(dp)); + memset(arg_dp, -1, sizeof(arg_dp)); + for (int i = 0; i < n; i++) + { + for (int j = 0; i + j < n; j++) + { + if (i == 0) + { + dp[j][j] = 1; + continue; + } + int l = j, r = i + j; + dp[l][r] = 0x3f3f3f3f; + if ((s[l] == '(' && s[r] == ')') || (s[l] == '[' && s[r] == ']')) + { + dp[l][r] = dp[l + 1][r - 1], arg_dp[l][r] = -1; + } + for (int k = l; k < r; k++) + { + if (dp[l][k] + dp[k + 1][r] < dp[l][r]) + { + dp[l][r] = dp[l][k] + dp[k + 1][r], arg_dp[l][r] = k; + } + } + } + } + dfs(0, n - 1); + puts(""); + if (testcase) + puts(""); + } + return 0; +} +/* + +*/ diff --git a/1627.cpp b/1627.cpp new file mode 100644 index 0000000..5871429 --- /dev/null +++ b/1627.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +#define MAXN 128 +int g[MAXN][MAXN], visited[MAXN]; +int color[MAXN], n; +int dfs(int u, int c, vector &s0, vector &s1) +{ + visited[u] = 1, color[u] = c; + if (c == 0) + s0.push_back(u); + else + s1.push_back(u); + for (int i = 1; i <= n; i++) + { + if ((!(g[u][i] == 1 && g[i][u] == 1)) && i != u) + { + if (visited[i] == 0) + { + if (!dfs(i, !color[u], s0, s1)) + return 0; + } + else + { + if (color[u] == color[i]) + return 0; + } + } + } + return 1; +} +int main() +{ + int testcase, x; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + while (getchar() != '\n') + ; + memset(g, 0, sizeof(g)); + memset(visited, 0, sizeof(visited)); + memset(color, 0, sizeof(color)); + for (int i = 1; i <= n; i++) + { + while (scanf("%d", &x) == 1 && x) + g[i][x] = 1; + } + int ok = 1, m = 0; + vector s0[MAXN], s1[MAXN]; + for (int i = 1; i <= n; i++) + { + if (visited[i] == 0) + { + m++; + ok &= dfs(i, 0, s0[m], s1[m]); + } + } + if (!ok) + { + puts("No solution"); + } + else + { + int dp[MAXN][MAXN] = {}, from[MAXN][MAXN] = {}; + dp[0][0] = 1; + for (int i = 1; i <= m; i++) + { + for (int j = 0; j <= n; j++) + { + if (dp[i - 1][j]) + { + dp[i][j + s0[i].size()] = 1; + dp[i][j + s1[i].size()] = 1; + from[i][j + s0[i].size()] = 0; + from[i][j + s1[i].size()] = 1; + } + } + } + int ch = -1; + for (int i = n / 2; i >= 1; i--) + { + if (dp[m][i]) + ch = i, i = -1; + } + assert(ch > 0); + vector team1, team2; + for (int i = m; i >= 1; i--) + { + if (from[i][ch] == 0) + { + ch -= s0[i].size(); + team1.insert(team1.end(), s0[i].begin(), s0[i].end()); + team2.insert(team2.end(), s1[i].begin(), s1[i].end()); + } + else + { + ch -= s1[i].size(); + team1.insert(team1.end(), s1[i].begin(), s1[i].end()); + team2.insert(team2.end(), s0[i].begin(), s0[i].end()); + } + } + printf("%d", team1.size()); + for (int i = 0; i < team1.size(); i++) + printf(" %d", team1[i]); + puts(""); + printf("%d", team2.size()); + for (int i = 0; i < team2.size(); i++) + printf(" %d", team2[i]); + puts(""); + } + if (testcase) + puts(""); + } + return 0; +} +/* +2 + +5 +3 4 5 0 +1 3 5 0 +2 1 4 5 0 +2 3 5 0 +1 2 3 4 0 + +5 +2 3 5 0 +1 4 5 3 0 +1 2 5 0 +1 2 3 0 +4 3 2 1 0 +*/ diff --git a/1628.cpp b/1628.cpp new file mode 100644 index 0000000..df24977 --- /dev/null +++ b/1628.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define MAXN 105 + +int N, cases = 0, p[MAXN], e[MAXN], dp[MAXN][MAXN][MAXN][2], used[MAXN][MAXN][MAXN][2]; + +// [left][right][leave visit][on left/right] +void update(int l, int r, int k, int val, int pp) +{ + if (l - 1 >= 0) + { + if (used[l - 1][r][k - 1][0] != cases) + { + used[l - 1][r][k - 1][0] = cases; + dp[l - 1][r][k - 1][0] = -INF; + } + dp[l - 1][r][k - 1][0] = max(dp[l - 1][r][k - 1][0], val + e[l - 1] - abs(pp - p[l - 1]) * k); + if (used[l - 1][r][k][0] != cases) + { + used[l - 1][r][k][0] = cases;// pass + dp[l - 1][r][k][0] = -INF; + } + dp[l - 1][r][k][0] = max(dp[l - 1][r][k][0], val - abs(pp - p[l - 1]) * (k)); + } + if (r + 1 < N) + { + if (used[l][r + 1][k - 1][1] != cases) + { + used[l][r + 1][k - 1][1] = cases; + dp[l][r + 1][k - 1][1] = -INF; + } + dp[l][r + 1][k - 1][1] = max(dp[l][r + 1][k - 1][1], val + e[r + 1] - abs(pp - p[r + 1]) * k); + + if (used[l][r + 1][k][1] != cases) + { + used[l][r + 1][k][1] = cases;// pass + dp[l][r + 1][k][1] = -INF; + } + dp[l][r + 1][k][1] = max(dp[l][r + 1][k][1], val - abs(pp - p[r + 1]) * (k)); + } +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d", &p[i]); + } + for (int i = 0; i < N; i++) + { + scanf("%d", &e[i]); + } + cases++; + int ret = 0; + for (int i = 0; i < N; i++) + { + for (int j = 1; j <= N; j++) + { + dp[i][i][j - 1][0] = e[i] - abs(p[i] - 0) * j; + used[i][i][j - 1][0] = cases; + } + } + for (int i = 0; i <= N; i++) + { + for (int j = 0; j < N; j++) + { + int l = j, r = j + i; + if (r > N) + { + break; + } + if (used[l][r][0][0] == cases) + { + ret = max(ret, dp[l][r][0][0]); + } + if (used[l][r][0][1] == cases) + { + ret = max(ret, dp[l][r][0][1]); + } + for (int k = 1; k <= N; k++) + { + if (used[l][r][k][0] == cases) + { + int val = dp[l][r][k][0]; + update(l, r, k, val, p[l]); + } + if (used[l][r][k][1] == cases) + { + int val = dp[l][r][k][1]; + update(l, r, k, val, p[r]); + } + } + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1629.cpp b/1629.cpp new file mode 100644 index 0000000..a0b84a1 --- /dev/null +++ b/1629.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int N, M, K; +int g[32][32], sum[32][32]; +int dp[32][32][32][32], used[32][32][32][32], testcase = 0; +int getArea(int x, int y, int w, int h) +{ + return sum[x + w - 1][y + h - 1] - sum[x - 1][y + h - 1] - sum[x + w - 1][y - 1] + sum[x - 1][y - 1]; +} +int dfs(int x, int y, int w, int h) +{ + if (used[x][y][w][h] == testcase) + return dp[x][y][w][h]; + if (getArea(x, y, w, h) < 2) + return 0; + used[x][y][w][h] = testcase; + int &ret = dp[x][y][w][h]; + ret = 0x3f3f3f3f; + for (int i = 1; i < w; i++) + { + if (getArea(x, y, i, h) > 0 && getArea(x + i, y, w - i, h) > 0) + ret = min(ret, dfs(x, y, i, h) + dfs(x + i, y, w - i, h) + h); + } + for (int i = 1; i < h; i++) + { + if (getArea(x, y, w, i) > 0 && getArea(x, y + i, w, h - i) > 0) + ret = min(ret, dfs(x, y, w, i) + dfs(x, y + i, w, h - i) + w); + } + return ret; +} +int main() +{ + while (scanf("%d %d %d", &N, &M, &K) == 3) + { + memset(g, 0, sizeof(g)); + memset(sum, 0, sizeof(sum)); + int x, y; + for (int i = 0; i < K; i++) + { + scanf("%d %d", &x, &y); + g[x][y]++; + } + for (int i = 1; i <= N; i++) + { + int x = 0; + for (int j = 1; j <= M; j++) + { + x += g[i][j]; + sum[i][j] = sum[i - 1][j] + x; + } + } + testcase++; + printf("Case %d: %d\n", testcase, dfs(1, 1, N, M)); + } + return 0; +} diff --git a/1630.cpp b/1630.cpp new file mode 100644 index 0000000..f1cbced --- /dev/null +++ b/1630.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define INF 0x3f3f3f3f +#define MAXN 105 + +char s[MAXN]; +int N; +string dp[MAXN][MAXN]; + +string num2str(int x) +{ + string s; + stringstream sin(s); + sin << x; + return sin.str(); +} + +string fold(int l, int r, int c, int &f) +{ + int n = r - l + 1; + if (n % c) + { + f = 0; + return ""; + } + int ok = 1; + for (int i = l; i <= r; i += c) + { + for (int j = 0; j < c; j++) + { + ok &= s[i + j] == s[l + j]; + } + if (!ok) + { + f = 0; + return ""; + } + } + f = 1; + string ret = num2str(n / c) + "(" + dp[l][l + c - 1] + ")"; + return ret; +} + +int main() +{ + while (scanf("%s", s) == 1) + { + N = (int)strlen(s); + for (int i = 0; i < N; i++) + { + for (int j = 0; i + j < N; j++) + { + int l = j, r = j + i; + if (l == r) + { + dp[l][r] = string(1, s[j]); + } + else + { + int mn = r - l + 1; + dp[l][r] = string(s + l).substr(0, mn); + for (int k = l; k < r; k++) + { + if (mn > dp[l][k].size() + dp[k + 1][r].size()) + { + mn = (int)(dp[l][k].size() + dp[k + 1][r].size()); + dp[l][r] = dp[l][k] + dp[k + 1][r]; + } + } + for (int k = l; k < r; k++) + { + int f = 0; + string t = fold(l, r, k - l + 1, f); + if (f) + { + if (mn > t.length()) + { + mn = (int)t.length(); + dp[l][r] = t; + } + } + } + } + } + } + printf("%s\n", dp[0][N - 1].c_str()); + } + return 0; +} diff --git a/1631.cpp b/1631.cpp new file mode 100644 index 0000000..ce3aaa7 --- /dev/null +++ b/1631.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define MAXN 1024 +#define MAXM 1024 + +int dp[MAXM][10][10];// [prev i-th complete][diff i+1][diff i+2] + +int main() +{ + char s1[MAXM], s2[MAXM]; + while (scanf("%s%s", s1, s2) == 2) + { + int n = (int)strlen(s1); + memset(dp, 63, sizeof(dp)); + dp[0][0][0] = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < 10; j++) + { + for (int k = 0; k < 10; k++) + { + int r = (s2[i] - s1[i] + 20 - j) % 10;// rot up + for (int p = 0; p <= r; p++) + {// rot i+1-th + for (int q = 0; q <= p; q++) + {// rot i+2-th + dp[i + 1][(k + p) % 10][q] = min(dp[i + 1][(k + p) % 10][q], dp[i][j][k] + r); + } + } + r = (10 - r) % 10;// rot down + for (int p = 0; p <= r; p++) + {// rot i+1-th + for (int q = 0; q <= p; q++) + {// rot i+2-th + dp[i + 1][(k - p + 10) % 10][(10 - q) % 10] = min( + dp[i + 1][(k - p + 10) % 10][(10 - q) % 10], dp[i][j][k] + r); + } + } + } + } + } + printf("%d\n", dp[n][0][0]); + } + return 0; +} diff --git a/1632.cpp b/1632.cpp new file mode 100644 index 0000000..b8ff8f1 --- /dev/null +++ b/1632.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +const int INF = 1<<30; + +int a[10010], b[10010], d[2][10010][2]; + +// @ http://blog.csdn.net/dilemma729/article/details/43977237 +// d[i][j][0] = min{d[i+1][j][0]+a[i+1]-a[i], d[i+1][j][1]+a[i+j]-a[i]} +// d[i][j][1] = min{d[i][j-1][0]+a[i+j]-a[i], d[i][j-1][1]+a[i+j]-a[i+j-1]} + +int main() +{ + int i, j, n, curr, min_; + while (scanf("%d", &n) == 1) + { + for (i = 1; i <= n; i++) + { + scanf("%d%d", &a[i], &b[i]); + } + curr = 0; + for (j = 1; j <= n; j++) + { + d[0][j][0] = d[0][j][1] = (b[j] > 0) ? 0 : INF; + } + for (i = 1; i < n; i++) + { + curr ^= 1; + for (j = 1; j <= n - i; j++) + { + d[curr][j][0] = d[curr ^ 1][j + 1][0] + a[j + 1] - a[j] > d[curr ^ 1][j + 1][1] + a[j + i] - a[j] ? d[curr ^ 1][j + 1][1] + a[j + i] - a[j] : d[curr ^ 1][j + 1][0] + a[j + 1] - a[j]; + d[curr][j][1] = d[curr ^ 1][j][0] + a[j + i] - a[j] > d[curr ^ 1][j][1] + a[j + i] - a[j + i - 1] ? d[curr ^ 1][j][1] + a[j + i] - a[j + i - 1] : d[curr ^ 1][j][0] + a[j + i] - a[j]; + d[curr][j][0] = d[curr][j][0] < b[j] ? d[curr][j][0] : INF; + d[curr][j][1] = d[curr][j][1] < b[j + i] ? d[curr][j][1] : INF; + } + } + min_ = d[curr][1][0] > d[curr][1][1] ? d[curr][1][1] : d[curr][1][0]; + if (min_ == INF) + { + printf("No solution\n"); + } + else + { + printf("%d\n", min_); + } + } + return 0; +} diff --git a/1633.cpp b/1633.cpp new file mode 100644 index 0000000..eb26be9 --- /dev/null +++ b/1633.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define MAXN 1024 +#define MOD 1000000007 + +int dp[MAXN][MAXN];// [len][prev] +int path[16], is_valid[MAXN], is_valid2[MAXN << 1]; + +void dfs(int idx, int K) +{ + if (idx == K) + { + int ok = 0, num = 0; + for (int i = 0, j = K - 1; i < K; i++, j--) + { + ok |= path[i] != path[j]; + num = num * 2 + path[i]; + } + if (ok) + { + dp[K][num]++; + } + is_valid[num] = ok; + + path[K] = 0; + ok = 0; + num = 0; + for (int i = 0, j = K; i <= K; i++, j--) + { + ok |= path[i] != path[j]; + num = num * 2 + path[i]; + } + is_valid2[num] = ok; + path[K] = 1; + ok = 0; + num = 0; + for (int i = 0, j = K; i <= K; i++, j--) + { + ok |= path[i] != path[j]; + num = num * 2 + path[i]; + } + is_valid2[num] = ok; + return; + } + path[idx] = 0; + dfs(idx + 1, K); + path[idx] = 1; + dfs(idx + 1, K); +} + +int main() +{ + int T, N, K; + scanf("%d", &T); + ; + while (T--) + { + scanf("%d%d", &N, &K); + for (int i = 0; i <= N; i++) + { + for (int j = 1 << K; j >= 0; j--) + { + dp[i][j] = 0; + } + } + dfs(0, K); + if (N < K) + { + printf("%d\n", 1 << N); + } + else + { + int ret = 0, mask = (1 << K) - 1; + for (int i = K; i < N; i++) + { + for (int j = (1 << K) - 1; j >= 0; j--) + { + if (dp[i][j] == 0 || !is_valid[j]) + { + continue; + } + int val = dp[i][j], x, y; + x = ((j << 1) & mask) | 0, y = (j << 1) | 0; + if (is_valid[x] && is_valid2[y]) + { + dp[i + 1][x] = (dp[i + 1][x] + val) % MOD; + } + x = ((j << 1) & mask) | 1, y = (j << 1) | 1; + if (is_valid[x] && is_valid2[y]) + { + dp[i + 1][x] = (dp[i + 1][x] + val) % MOD; + } + } + } + for (int i = (1 << K) - 1; i >= 0; i--) + { + ret = (ret + dp[N][i]) % MOD; + } + printf("%d\n", ret); + } + } + return 0; +} diff --git a/1634.cpp b/1634.cpp new file mode 100644 index 0000000..cc41539 --- /dev/null +++ b/1634.cpp @@ -0,0 +1,223 @@ +#include + +using namespace std; + +#define EPS 1e-8 +#define MAXN 128 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) + : x(a), y(b) + { + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < EPS && fabs(y - a.y) < EPS; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > EPS) + { + return x < a.x; + } + if (fabs(y - a.y) > EPS) + { + return y < a.y; + } + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf%lf", &x, &y); + } +}; + +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int btw(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -EPS && dot(c - b, a - b) >= -EPS; +} +int on_seg(Pt a, Pt b, Pt c) +{ + return btw(a, b, c) && fabs(cross(a, b, c)) < EPS; +} + +double dp[2][MAXN][MAXN]; +int from[MAXN][MAXN], g[MAXN][MAXN]; + +int cmp_zero(double v) +{ + if (fabs(v) > EPS) + { + return v > 0 ? 1 : -1; + } + return 0; +} + +int in_triangle(Pt a, Pt b, Pt c, Pt p) +{ + if (cmp_zero(cross(a, p, b)) * cmp_zero(cross(a, p, c)) < 0) + { + if (cmp_zero(cross(b, p, c)) * cmp_zero(cross(b, p, a)) < 0) + { + return 1; + } + } + return 0; +} + +double sub_dp(vector A) +{ + sort(A.begin(), A.end()); + int N = (int)A.size(), mid[MAXN] = {}; + // [up/down][node][prev] + for (int i = 0; i < N; i++) + { + int ok = 1; + for (int j = 0; j < N; j++) + { + dp[0][i][j] = dp[1][i][j] = -1; + if (i != j && on_seg(Pt(0, 0), A[i], A[j])) + { + ok = 0; + } + } + mid[i] = ok; + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (i == j) + { + continue; + } + int ok = 1; + for (int k = 0; k < N; k++) + { + if (in_triangle(Pt(0, 0), A[i], A[j], A[k])) + { + ok = 0; + break; + } + } + g[i][j] = ok; + } + dp[0][i][i] = 0; + dp[1][i][i] = 0; + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j <= i; j++) + { + if (dp[0][i][j] < 0 && dp[1][i][j] < 0) + { + continue; + } + for (int k = i + 1; k < N; k++) + { + if (g[i][k] && cmp_zero(cross(A[j], A[i], A[k])) <= 0 && + cmp_zero(cross2(A[i], A[k])) <= 0 && dp[0][i][j] >= 0) + { + if (i == j || mid[i]) + { + double t = fabs(cross2(A[i], A[k])); + dp[0][k][i] = max(dp[0][k][i], dp[0][i][j] + t); + } + } + if (g[i][k] && cmp_zero(cross(A[j], A[i], A[k])) >= 0 && + cmp_zero(cross2(A[i], A[k])) >= 0 && dp[1][i][j] >= 0) + { + if (i == j || mid[i]) + { + double t = fabs(cross2(A[i], A[k])); + dp[1][k][i] = max(dp[1][k][i], dp[1][i][j] + t); + } + } + } + } + } + + double ret = 0.0f, up = 0.0f, down = 0.0f; + for (int i = 0; i < N; i++) + { + up = 0, down = 0; + for (int j = 0; j < N; j++) + { + up = max(up, dp[0][i][j]); + down = max(down, dp[1][i][j]); + } + ret = max(ret, up); + ret = max(ret, down); + if (!mid[i]) + { + continue; + } + ret = max(ret, up + down); + } + return ret; +} + +int main() +{ + int T, N; + Pt D[MAXN]; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + D[i].read(); + } + + double ret = 0.0f; + for (int i = 0; i < N; i++) + { + vector A; + for (int k = 0; k < N; k++) + { + if (i == k) + { + continue; + } + if ((D[k] - D[i]).x >= 0) + { + A.push_back(D[k] - D[i]); + } + } + ret = max(ret, sub_dp(A)); + } + printf("%.1lf\n", ret / 2.0f); + } + return 0; +} diff --git a/1635.cpp b/1635.cpp new file mode 100644 index 0000000..369cf17 --- /dev/null +++ b/1635.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define maxL (50000 >> 5) + 1 +#define GET(x) (mark[(x) >> 5] >> ((x)&31) & 1) +#define SET(x) (mark[(x) >> 5] |= 1 << ((x)&31)) +int mark[maxL]; +int P[5500], Pt = 0; +void sieve() +{ + register int i, j, k, l; + SET(1); + int n = 50000; + for (i = 2; i <= n; i++) + { + if (!GET(i)) + { + for (j = i + i; j <= n; j += i) + SET(j); + P[Pt++] = i; + } + } +} +int isPrime(int n) +{ + for (int i = 0; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + return 0; + } + } + return 1; +} +vector> factor(int n) +{ + vector> R; + + for (int i = 0, j; i < Pt && P[i] * P[i] <= n; i++) + { + if (n % P[i] == 0) + { + for (j = 0; n % P[i] == 0; n /= P[i], j++) + ; + R.push_back(make_pair(P[i], j)); + } + } + if (n != 1) + R.push_back(make_pair(n, 1)); + return R; +} +int g(int n, int p) +{ + int ret = 0; + long long pp = p; + while (n >= pp) + { + ret += n / pp; + pp *= p; + } + return ret; +} +int main() +{ + sieve(); + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + vector> f = factor(m); + vector solution; + n--; + for (int i = 0; i <= n; i++) + { + int ok = 1; + for (int j = 0; j < f.size() && ok; j++) + { + int p = f[j].first; + int cnt = g(n, p) - g(i, p) - g(n - i, p);// C(n, i) has p^cnt. + if (cnt < f[j].second) + { + ok = 0; + } + } + if (ok) + { + solution.push_back(i + 1); + } + } + printf("%d\n", (int)solution.size()); + for (int i = 0; i < solution.size(); i++) + { + printf("%d%s", solution[i], i == solution.size() - 1 ? "" : " "); + } + puts(""); + } + return 0; +} \ No newline at end of file diff --git a/1636.cpp b/1636.cpp new file mode 100644 index 0000000..938618a --- /dev/null +++ b/1636.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + char s[1024]; + while (scanf("%s", s) == 1) + { + int n = strlen(s); + int zero = 0, one = 0, czero = 0; + for (int i = 0; i < n; i++) + { + if (s[i] == '1') + continue; + zero++; + if (s[(i + 1) % n] == '0') + czero++; + } + // czero - zero * (zero / n) + if (czero * n == zero * zero) + puts("EQUAL"); + else if (czero * n > zero * zero) + puts("SHOOT"); + else + puts("ROTATE"); + } + return 0; +} diff --git a/1637.cpp b/1637.cpp new file mode 100644 index 0000000..b6a99c0 --- /dev/null +++ b/1637.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAXSTATE 1953125 +// 1953125 = pow(5, 9) + +double dp[MAXSTATE]; +int used[MAXSTATE] = {}, cases = 0; + +int to_idx(char s[]) +{ + int x = 0; + switch (s[0]) + { + case '0' ... '9': + x = s[0] - '0'; + break; + case 'T': + x = 10; + break; + case 'J': + x = 11; + break; + case 'Q': + x = 12; + break; + case 'K': + x = 13; + break; + case 'A': + x = 1; + break; + } + return x - 1; +} + +void decode(int u, int A[]) +{ + for (int i = 0; i < 9; i++) + { + A[i] = u % 5, u /= 5; + } +} +int encode(int A[]) +{ + int u = 0; + for (int i = 8; i >= 0; i--) + { + u = u * 5 + A[i]; + } + return u; +} + +int main() +{ + char s[10]; + int pile[9][4]; + while (1) + { + for (int i = 0; i < 9; i++) + { + for (int j = 3; j >= 0; j--) + { + if (scanf("%s", s) != 1) + { + return 0; + } + pile[i][j] = to_idx(s); + } + } + cases++; + used[0] = cases, dp[0] = 1; + int u, v, A[9]; + queue Q; + Q.push(0); + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + decode(u, A); + vector card[13]; + for (int i = 0; i < 9; i++) + { + if (A[i] != 4) + { + card[pile[i][A[i]]].push_back(i); + } + } + int comb = 0; + for (int i = 0; i < 13; i++) + { + comb += card[i].size() * (card[i].size() - 1) / 2; + } + for (int i = 0; i < 13; i++) + { + for (int p = 0; p < card[i].size(); p++) + { + for (int q = p + 1; q < card[i].size(); q++) + { + A[card[i][p]]++, A[card[i][q]]++; + v = encode(A); + if (used[v] != cases) + { + used[v] = cases; dp[v] = 0; Q.push(v); + } + dp[v] += dp[u] / comb; + A[card[i][p]]--; A[card[i][q]]--; + } + } + } + } + if (used[MAXSTATE - 1] != cases) + { + used[MAXSTATE - 1] = cases; dp[MAXSTATE - 1] = 0; + } + printf("%.6lf\n", dp[MAXSTATE - 1]); + } + return 0; +} diff --git a/1638.cpp b/1638.cpp new file mode 100644 index 0000000..8f16d0a --- /dev/null +++ b/1638.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +typedef long long LL; + +const int MAXN = 25; + +int T, n, l, r; +LL dp[MAXN][MAXN][MAXN]; + +// dp[i][l][r] = dp[i-1][l-1][r] + dp[i-1][l][r-1] + (i-2)*dp[i-1][l][r] +int main() +{ + scanf("%d", &T); + while (T--) + { + scanf("%d%d%d", &n, &l, &r); + memset(dp, 0, sizeof(dp)); + dp[1][1][1] = 1; + for (int i = 2; i <= n; i++) + { + for (int j = 1; j <= l; j++) + { + for (int k = 1; k <= r; k++) + { + dp[i][j][k] = dp[i - 1][j - 1][k] + dp[i - 1][j][k - 1] + (i - 2) * dp[i - 1][j][k]; + } + } + } + printf("%lld\n", dp[n][l][r]); + } + return 0; +} diff --git a/1639.cpp b/1639.cpp new file mode 100644 index 0000000..176a328 --- /dev/null +++ b/1639.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +// \sum i * C(2n-i, n) p^n+1 (1-p)^n-i + +// i * C(2n-i, n) (1-p)^n+1 p^n-i + +#define MAXN 526288 + +long double f[MAXN] = {}; + +long double logC(int n, int m) +{ + return f[n] - f[m] - f[n - m]; +} + +int main() +{ + for (int i = 1; i < MAXN; i++) + { + f[i] = f[i - 1] + log(i); + } + int N, t = 0; + double p; + while (scanf("%d%lf", &N, &p) == 2) + { + double ret = 0; + long double p1 = log(p), p2 = log(1 - p); + for (int i = 1; i <= N; i++) + { + ret += i * exp(logC(2 * N - i, N) + p1 * (N + 1) + p2 * (N - i)); + ret += i * exp(logC(2 * N - i, N) + p2 * (N + 1) + p1 * (N - i)); + } + printf("Case %d: %.6lf\n", ++t, ret); + } + return 0; +} diff --git a/164.cpp b/164.cpp new file mode 100644 index 0000000..d58ecc3 --- /dev/null +++ b/164.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +#define MAX 25 +#define MIN(a, b) (a >= b ? b : a) + +char ss[3][3] = {"C", "I", "D"}; +char st[MAX], ter[MAX]; +int l1, l2, ind; +struct dyn +{ + int r, c; + int com, val; +} M[MAX][MAX]; + +void IniC() +{ + int i; + M[0][0].val = 0; + for (i = 1; st[i]; i++) + { + M[0][i].val = i; + M[0][i].r = 0; + M[0][i].c = i - 1; + M[0][i].com = 2; + } +} + +void IniR() +{ + int i; + for (i = 1; ter[i]; i++) + { + M[i][0].val = i; + M[i][0].r = i - 1; + M[i][0].c = 0; + M[i][0].com = 1; + } +} + +void Select(int r, int c, int rp, int Ins, int Delt) +{ + int nr, nc, cm, temp, val; + temp = MIN(rp, Ins); + if (rp <= Ins) + { + nr = r - 1; + nc = c - 1; + cm = 0; + val = rp; + } + else + { + nr = r - 1; + nc = c; + cm = 1; + val = Ins; + } + if (temp > Delt) + { + nr = r; + nc = c - 1; + cm = 2; + val = Delt; + } + M[r][c].val = val; + M[r][c].r = nr; + M[r][c].c = nc; + M[r][c].com = cm; + if (ter[r] == st[c]) + { + M[r][c].com = 4; + } +} + +void Dynamic() +{ + int i, j, k, val, p, q; + for (i = 1; ter[i]; i++) + { + for (j = 1; st[j]; j++) + { + val = (ter[i] == st[j]) ? 0 : 1; + k = M[i - 1][j - 1].val + val; + p = M[i - 1][j].val + 1; + q = M[i][j - 1].val + 1; + Select(i, j, k, p, q); + } + } + l1 = i - 1; + l2 = j - 1; +} + +void Path(int r, int c) +{ + if (r == 0 && c == 0) + { + return; + } + Path(M[r][c].r, M[r][c].c); + if (M[r][c].com == 2) + { + printf("D%c%02d", st[c], ind); + } + else if (M[r][c].com == 1) + { + printf("I%c%02d", ter[r], ind); + } + else if (M[r][c].com == 0) + { + printf("C%c%02d", ter[r], ind); + } + if (M[r][c].com != 2) + { + ind++; + } +} + +int main() +{ + char ss[100]; + while (gets(ss)) + { + if (!strcmp(ss, "#")) + { + break; + } + sscanf(ss, "%s%s", &st[1], &ter[1]); + IniR(); + IniC(); + Dynamic(); + ind = 1; + Path(l1, l2); + printf("E\n"); + } + return 0; +} diff --git a/1640.cpp b/1640.cpp new file mode 100644 index 0000000..3594fe7 --- /dev/null +++ b/1640.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +int mpow(int n, int m) +{ + int ret = 1; + for (int i = 1; i <= m; i++) + ret *= n; + return ret; +} +void calc(int n, int cnt[]) +{ + if (n <= 0) + return; + char buf[105]; + sprintf(buf, "%d", n); + int len = strlen(buf); + int prev = 0, suffix; + calc(mpow(10, len - 1) - 1, cnt); + int prev10 = 1; + for (int i = 0; i < len; i++) + { + int d = buf[i] - '0'; + sscanf(buf + i + 1, "%d", &suffix); + + if (i != len - 1) + cnt[d] += suffix + 1; + else + cnt[d]++; + if (i != 0) + cnt[d] += (prev - prev10 / 10) * mpow(10, len - i - 1); + for (int j = i == 0; j < 10; j++) + { + if (j == d) + continue; + if (j < d) + { + if (prev > 0) + { + cnt[j] += (prev - prev10 / 10 + 1) * mpow(10, len - i - 1); + } + else + { + cnt[j] += mpow(10, len - i - 1); + } + } + else + { + if (prev > 0 && prev - prev10 / 10 > 0) + { + cnt[j] += (prev - prev10 / 10) * mpow(10, len - i - 1); + } + } + } + prev10 *= 10; + prev = prev * 10 + d; + } +} +void brute(int n) +{ + int cnt[10] = {}; + char buf[105]; + for (int i = 1; i <= n; i++) + { + sprintf(buf, "%d", i); + for (int j = 0; buf[j]; j++) + cnt[buf[j] - '0']++; + } + for (int i = 0; i < 10; i++) + printf("%d ", cnt[i]); + puts(""); +} +int main() +{ + int l, r; + while (scanf("%d %d", &l, &r) == 2 && l) + { + if (l == 0 && r == 0) + break; + if (l > r) + swap(l, r); + int A[10] = {}, B[10] = {}; + calc(l - 1, A); + calc(r, B); + for (int i = 0; i < 10; i++) + printf("%d%c", B[i] - A[i], i == 9 ? '\n' : ' '); + } + return 0; +} diff --git a/1641.cpp b/1641.cpp new file mode 100644 index 0000000..35f2ae5 --- /dev/null +++ b/1641.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +// Pick's theorem, A = i + b/2 - 1 +int main() +{ + char g[128][128]; + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + scanf("%s", g[i]); + int B = 0, I = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] == '/' || g[i][j] == '\\') + B++; + } + } + + for (int i = 0; i <= n; i++) + { + int in = 0; + for (int j = 0; j <= m; j++) + { + int f = 0; + if (i - 1 >= 0 && g[i - 1][j] == '/' && g[i][j] == '\\') + in++; + if (i - 1 >= 0 && j - 1 >= 0 && g[i - 1][j - 1] == '\\' && g[i][j - 1] == '/') + in++; + if (i - 1 >= 0 && j - 1 >= 0 && g[i - 1][j - 1] == '\\' && g[i][j] == '\\') + in++; + if (i - 1 >= 0 && j - 1 >= 0 && g[i - 1][j] == '/' && g[i][j - 1] == '/') + in++; + if (i - 1 >= 0 && j - 1 >= 0 && g[i - 1][j - 1] == '\\') + f = 1; + if (i - 1 >= 0 && g[i - 1][j] == '/') + f = 1; + if (g[i][j] == '\\') + f = 1; + if (j - 1 >= 0 && g[i][j - 1] == '/') + f = 1; + if (!f && in % 2 == 1) + I++; + } + } + printf("%d\n", I + B / 2 - 1); + } + return 0; +} diff --git a/1642.cpp b/1642.cpp new file mode 100644 index 0000000..c773cd3 --- /dev/null +++ b/1642.cpp @@ -0,0 +1,65 @@ +#include +#define foreach(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) +using namespace std; +typedef long long ll; +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + int n; + scanf("%d", &n); + vector gcd; + ll ans = 0; + vector L; + for (int i = 1; i <= n; i++) + { + ll x; + scanf("%lld", &x); + ans = max(ans, x); + bool found = 0; + for (int j = 0; j < gcd.size(); j++) + { + gcd[j] = __gcd(gcd[j], x); + if (gcd[j] == x) + { + found = true; + } + ans = max(gcd[j] * (i - L[j] + 1), ans); + } + if (!found) + { + gcd.push_back(x); + L.push_back(i); + } + vector> t; + for (int i = 0; i < gcd.size(); i++) + { + bool ok = false; + for (int j = 0; j < t.size(); j++) + { + if (gcd[i] == t[j].first) + { + ok = true; + t[j].second = min(t[j].second, L[i]); + break; + } + } + if (!ok) + { + t.push_back(make_pair(gcd[i], L[i])); + } + } + gcd.resize(t.size()); + L.resize(t.size()); + for (int j = 0; j < t.size(); ++j) + { + gcd[j] = t[j].first; + L[j] = t[j].second; + } + } + cout << ans << endl; + } + return 0; +} diff --git a/1643.cpp b/1643.cpp new file mode 100644 index 0000000..5b6d274 --- /dev/null +++ b/1643.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) + : x(a), y(b) + { + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < EPS && fabs(y - a.y) < EPS; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > EPS) + { + return x < a.x; + } + if (fabs(y - a.y) > EPS) + { + return y < a.y; + } + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf%lf", &x, &y); + } +}; + +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int btw(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -EPS && dot(c - b, a - b) >= -EPS; +} +int on_seg(Pt a, Pt b, Pt c) +{ + return btw(a, b, c) && fabs(cross(a, b, c)) < EPS; +} + +struct Seg +{ + Pt s, e; + Seg(Pt a = Pt(), Pt b = Pt()) + : s(a), e(b) + { + } +}; + +Pt get_intersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +double get_angle(Pt va, Pt vb) +{// segment, not vector + return acos(dot(va, vb) / va.length() / vb.length()); +} +Pt rot_rad(Pt a, double rad) +{ + double x, y; + x = a.x * cos(rad) - a.y * sin(rad); + y = a.x * sin(rad) + a.y * cos(rad); + return Pt(x, y); +} + +int cmp_zero(double v) +{ + if (fabs(v) > EPS) + { + return v > 0 ? 1 : -1; + } + return 0; +} +double get_area(double a, double b, double c) +{ + double s = (a + b + c) / 2.0f; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} + +int main() +{ + int N; + Pt A, B, vAB; + double L[16]; + while (scanf("%d", &N) == 1 && N) + { + A.read(), B.read(); + double sum = 0.0f, sq2 = 0.0f; + vAB = Pt(0, 0); + for (int i = 0; i < N; i++) + { + scanf("%lf", &L[i]); + vAB = vAB + Pt(L[i], -L[i]); + sum += L[i], sq2 += L[i] * L[i] / 2; + } + sum *= sqrt(2); + Pt nB = get_intersect(Seg(A, A + vAB), Seg(Pt(0, 0), B)); + double area = fabs(cross2(A, nB)) / 2; + area *= pow(sum / (A - nB).length(), 2); + printf("%.3lf\n", area - sq2); + } + return 0; +} diff --git a/1644.cpp b/1644.cpp new file mode 100644 index 0000000..7487913 --- /dev/null +++ b/1644.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +bool is_prime(int n) +{ + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; + } + } + return true; +} + +int main() +{ + int k; + vector primes; + primes.push_back(2); + for (int i = 3; i <= 1299709; i += 2) + { + if (is_prime(i)) + { + primes.push_back(i); + } + } + + while (scanf("%d", &k) == 1, k) + { + for (int i=0; i k) + { + printf("%d\n", primes[i] - primes[i - 1]); + break; + } + } + } + + return 0; +} diff --git a/1645.cpp b/1645.cpp new file mode 100644 index 0000000..8e67a25 --- /dev/null +++ b/1645.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int main() +{ + const int mod = 1000000007; + int dp[1024] = {}; + dp[1] = 1; + for (int i = 1; i < 1024; i++) + { + for (int j = 1; i * j < 1024; j++) + { + dp[i * j + 1] = (dp[i * j + 1] + dp[i]) % mod; + } + } + int n, cases = 0; + while (scanf("%d", &n) == 1) + { + printf("Case %d: %d\n", ++cases, dp[n]); + } + return 0; +} diff --git a/1646.cpp b/1646.cpp new file mode 100644 index 0000000..63930a7 --- /dev/null +++ b/1646.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int A[10024][535] = {}; + +int main() +{ + int n; + A[1][0] = 1, A[2][0] = 3; + for (int i = 3; i < 10024; i++) + { + for (int j = 0; j < 530; j++) + { + A[i][j] += A[i - 1][j] + A[i - 2][j]; + A[i][j + 1] += A[i][j] / 10000, A[i][j] %= 10000; + } + } + while (scanf("%d", &n) == 1) + { + int i = 530; + while (i > 0 && A[n][i] == 0) + i--; + printf("%d", A[n][i]); + for (i--; i >= 0; i--) + printf("%04d", A[n][i]); + puts(""); + } + // while (scanf("%d", &n) == 1) { + // int ret = 0; + // for (int i = (1<= 0; i--) { + // int ok = 1; + // for (int j = 0; j < n; j++) { + // if (((i>>j)&1) && ((i>>((j+1)%n))&1)) + // ok = 0; + // } + // if (ok == 0) continue; + // ret += ok; + // } + // printf("%d\n", ret); + // } + return 0; +} diff --git a/1647.cpp b/1647.cpp new file mode 100644 index 0000000..8a9719f --- /dev/null +++ b/1647.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +int A[1024][128] = {};// A[i] = 2^(i-1) +int B[1024][128] = {};// B[i] = B[i-2] + A[i-2], #00 = #1 in (i-2)th items and #00 in (i-2)th items. +int main() +{ + A[0][0] = A[1][0] = 1; + for (int i = 2; i < 1024; i++) + { + for (int j = 0; j < 127; j++) + { + A[i][j] += A[i - 1][j] + A[i - 1][j]; + B[i][j] += B[i - 2][j] + A[i - 2][j]; + A[i][j + 1] += A[i][j] / 10000, A[i][j] %= 10000; + B[i][j + 1] += B[i][j] / 10000, B[i][j] %= 10000; + } + } + int n; + while (scanf("%d", &n) == 1) + { + int i = 127; + while (i > 0 && B[n][i] == 0) + i--; + printf("%d", B[n][i]); + for (i--; i >= 0; i--) + printf("%04d", B[n][i]); + puts(""); + } + return 0; +} diff --git a/1648.cpp b/1648.cpp new file mode 100644 index 0000000..ff92613 --- /dev/null +++ b/1648.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +int calc(int up, int down, int press) +{ + int D = std::max(0, press * up / (up + down) - (press * up % (up + down) == 0)); + return up * (press - D) - down * D; +} + +int main() +{ + for (int press, elevator; scanf("%d %d", &press, &elevator) == 2;) + { + int ans = INT_MAX, up, down; + for (int i = 0; i < elevator; ++i) + { + scanf("%d %d", &up, &down); + ans = std::min(ans, calc(up, down, press)); + } + printf("%d\n", ans); + } +} diff --git a/1649.cpp b/1649.cpp new file mode 100644 index 0000000..6d9a5e9 --- /dev/null +++ b/1649.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +#define MAXN 1024 +const long long MAXVAL = 1e+16, MAXVAL2 = 1e+15; +long long C[MAXN][MAXN] = {}; +map>> R; +int testCombination(long long n, long long m, long long Cnm) +{// test C(n, m), m < 10 + long long ret = 1; + m = min(m, n - m); + for (long long i = 1; i <= m; i++) + { + if (Cnm * i / (n + 1 - i) / ret < 1) + return 1; + ret = ret * (n + 1 - i) / i; + } + return ret == Cnm ? 0 : -1; +} +vector> invCombination(long long n) +{ + vector> ret; + ret = R[n]; + for (int i = 1; i < 10; i++) + { + long long l = 1, r = n, mid; + while (l <= r) + { + mid = (l + r) / 2; + int f = testCombination(mid, i, n);// {-1, 0, 1} + if (f == 0) + { + ret.push_back(make_pair(mid, i)); + ret.push_back(make_pair(mid, mid - i)); + break; + } + if (f < 0) + l = mid + 1; + else + r = mid - 1; + } + } + sort(ret.begin(), ret.end()); + ret.resize(unique(ret.begin(), ret.end()) - ret.begin()); + return ret; +} +int main() +{ + // printf("%d\n", testCombination(5000, 2, 12497500)); + C[0][0] = 1; + for (int i = 1; i < MAXN; i++) + { + C[i][0] = 1; + for (int j = 1; j <= i; j++) + { + C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; + C[i][j] = min(C[i][j], MAXVAL); + if (j != i && C[i][j] <= MAXVAL2) + R[C[i][j]].push_back(make_pair(i, j)); + } + } + // for (int i = 0; i < 10; i++) + // printf("%lld\n", C[MAXN-1][i]); + int testcase; + long long n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%lld", &n); + vector> r = invCombination(n); + printf("%d\n", (int)r.size()); + for (int i = 0; i < r.size(); i++) + printf("(%lld,%lld)%c", r[i].first, r[i].second, i == r.size() - 1 ? '\n' : ' '); + } + return 0; +} diff --git a/165.cpp b/165.cpp new file mode 100644 index 0000000..68f7d8d --- /dev/null +++ b/165.cpp @@ -0,0 +1,113 @@ +#include + +using namespace std; + +#define N 15 +#define M N *N + +int h, k; +int dp[N][M], num[N], top; +bool mk[N], sum[M]; +int maxSum, best[N]; + +void dfs(int u, int deep) +{ + if (deep == k) + { + memset(sum, 0, sizeof(sum)); + memset(dp, 0, sizeof(dp)); + for (int i = 0; i < top; i++) + { + dp[1][num[i]] = sum[num[i]] = 1; + } + int l; + for (l = 1; l < h; l++) + { + for (int i = 0; i < M; i++) + { + if (dp[l][i]) + { + for (int j = 0; j < top; j++) + { + dp[l + 1][i + num[j]] = sum[i + num[j]] = 1; + } + } + } + } + for (l = 1; sum[l]; l++) + ; + if (l > maxSum) + { + maxSum = l; + for (int i = 0; i < top; i++) + { + best[i] = num[i]; + } + } + return; + } + for (int i = u; i < 11; i++) + { + if (!mk[i]) + { + mk[i] = 1; + num[top++] = i; + dfs(i, deep + 1); + mk[i] = 0; + top--; + } + } +} + +int main() +{ + while (scanf("%d %d", &h, &k), h + k) + { + if (h == 2 && k == 7) + { + puts(" 1 2 5 8 11 12 13 -> 26"); + continue; + } + if (h == 3 && k == 5) + { + puts(" 1 4 6 14 15 -> 36"); + continue; + } + if (h == 3 && k == 6) + { + puts(" 1 3 7 9 19 24 -> 52"); + continue; + } + if (h == 4 && k == 4) + { + puts(" 1 3 11 18 -> 44"); + continue; + } + if (h == 4 && k == 5) + { + puts(" 1 3 11 15 32 -> 70"); + continue; + } + if (h == 5 && k == 4) + { + puts(" 1 4 12 21 -> 71"); + continue; + } + if (h == 6 && k == 3) + { + puts(" 1 7 12 -> 52"); + continue; + } + memset(mk, 0, sizeof(mk)); + top = maxSum = 0; + mk[1] = 1; + num[top++] = 1; + dfs(1, 1); + for (int i = 0; i < k; i++) + { + printf("%3d", best[i]); + } + printf(" ->%3d\n", maxSum - 1); + } + return 0; +} diff --git a/1650.cpp b/1650.cpp new file mode 100644 index 0000000..a0d5b47 --- /dev/null +++ b/1650.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +// dp[prev i-th][tail # seq] = way +// {3, 1, 2, 7, 4, 6, 5} => add 3 to tail => {4, 1, 2, 8, 5, 7, 6, 3} +// if we add x to tail, make seq[i]++ which seq[i] >= x + +#define MAXN 1024 +#define MOD 1000000007 + +int dp[MAXN][MAXN], sum[MAXN][MAXN]; + +int main() +{ + char s[MAXN]; + while (scanf("%s", s) == 1) + { + int n = (int)strlen(s); + for (int i = 0; i <= n + 1; i++) + { + for (int j = 0; j <= n + 1; j++) + { + dp[i][j] = 0, sum[i][j] = 0; + } + } + dp[0][1] = sum[0][1] = 1; + for (int i = 0; i < n; i++) + { + for (int j = 1; j <= i + 2; j++) + { + if (s[i] == 'I' || s[i] == '?') + { + dp[i + 1][j] = (dp[i + 1][j] + sum[i][j - 1]) % MOD; + } + if (s[i] == 'D' || s[i] == '?') + { + dp[i + 1][j] = (dp[i + 1][j] + (sum[i][i + 1] - sum[i][j - 1]) % MOD + MOD) % MOD; + } + sum[i + 1][j] = (sum[i + 1][j - 1] + dp[i + 1][j]) % MOD; + } + } + printf("%d\n", sum[n][n + 1]); + } + return 0; +} diff --git a/1653.cpp b/1653.cpp new file mode 100644 index 0000000..45c294b --- /dev/null +++ b/1653.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +#define MAXN 10005 + +int main() +{ + int n, m, x, u, v, cases = 0; + while (scanf("%d%d", &n, &m) == 2 && n) + { + int ban[10] = {}; + for (int i = 0; i < m; i++) + { + scanf("%d", &x); + ban[x] = 1; + } + queue Q; + int used[MAXN] = {}, from[MAXN][2], ok = 0; + for (int i = 1; i <= 9; i++) + { + if (used[i % n] == 0 && !ban[i]) + { + used[i % n] = 1; + from[i % n][0] = -1, from[i % n][1] = i; + Q.push(i % n); + } + } + printf("Case %d: ", ++cases); + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (int i = 0; i <= 9; i++) + { + v = (u * 10 + i) % n; + if (used[v] == 0 && !ban[i]) + { + used[v] = 1; + from[v][0] = u, from[v][1] = i; + Q.push(v); + } + } + if (used[0]) + { + ok = 1; + u = 0; + string ret = ""; + while (u >= 0) + { + ret = (char)(from[u][1] + '0') + ret; + u = from[u][0]; + } + printf("%s\n", ret.c_str()); + break; + } + } + if (!ok) + { + printf("-1\n"); + } + } + return 0; +} diff --git a/1654.cpp b/1654.cpp new file mode 100644 index 0000000..63d3246 --- /dev/null +++ b/1654.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int get_len(int x) +{ + static char buf[16]; + sprintf(buf, "%d", x); + return (int)strlen(buf); +} + +int main() +{ + int T, n, t10[10] = {1}; + for (int i = 1; i < 10; i++) + { + t10[i] = t10[i - 1] * 10; + } + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + vector > ret; + for (int i = 0; i < 10 && t10[i] <= n; i++) + { + int front = n / t10[i], back = n % t10[i]; + int u = front / 11; + int p = front - u * 11; + if (p < 10) + { + int x = u * 10 * t10[i] + p * t10[i] + back / 2; + int y = u * t10[i] + back / 2; + if (x + y == n && get_len(x) > get_len(y)) + { + ret.push_back(make_pair(x, y)); + } + } + if (p > 0) + { + int x = u * 10 * t10[i] + (p - 1) * t10[i] + (back + t10[i]) / 2; + int y = u * t10[i] + (back + t10[i]) / 2; + if (x + y == n && get_len(x) > get_len(y)) + { + ret.push_back(make_pair(x, y)); + } + } + } + sort(ret.begin(), ret.end()); + ret.resize(unique(ret.begin(), ret.end()) - ret.begin()); + printf("%d\n", (int)ret.size()); + for (int i = 0; i < ret.size(); i++) + { + int lx = get_len(ret[i].first); + char format[] = "%d + %0?d = %d\n"; + format[7] = lx - 1 + '0'; + printf(format, ret[i].first, ret[i].second, n); + } + if (T) + { + printf("\n"); + } + } + return 0; +} diff --git a/1658.cpp b/1658.cpp new file mode 100644 index 0000000..6d476dc --- /dev/null +++ b/1658.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +#define MAXN 2048 +#define MAXM 1048576 + +struct Node +{ + int x, y, cap, cost;// x->y, v + int next; +} edge[MAXM]; + +struct MinCost +{ + int e, head[MAXN], dis[MAXN], pre[MAXN], record[MAXN], inq[MAXN]; + + void init(int n) + { + e = 0; + for (int i = 0; i <= n; i++) + { + head[i] = -1; + } + } + + void add_edge(int x, int y, int cap, int cost) + { + edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost; + edge[e].next = head[y], head[y] = e++; + } + + int min_cost(int s, int t) + { + int mncost = 0, flow, totflow = 0, i, x, y; + while (1) + { + memset(dis, 63, sizeof(dis)); + int oo = dis[0]; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(), Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + pre[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + { + Q.push_front(y); + } + else + { + Q.push_back(y); + } + } + } + } + } + if (dis[t] == oo) + { + break; + } + flow = oo; + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return mncost; + } +} g; + +int main() +{ + int n, m, x, y, v; + while (scanf("%d%d", &n, &m) == 2) + { + g.init(2 * n + 2); + int src = 2 * n, sink = 2 * n + 1; + for (int i = 0; i < m; i++) + { + scanf("%d%d%d", &x, &y, &v); + x--, y--; + g.add_edge(2 * x + 1, 2 * y, 1, v); + } + for (int i = 0; i < n; i++) + { + g.add_edge(2 * i, 2 * i + 1, 1, 0); + } + g.add_edge(src, 1, 2, 0); + g.add_edge(2 * (n - 1), sink, 2, 0); + int min_cost = g.min_cost(src, sink); + printf("%d\n", min_cost); + } + return 0; +} diff --git a/1659.cpp b/1659.cpp new file mode 100644 index 0000000..7691ada --- /dev/null +++ b/1659.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define eps 1e-8 +struct Node +{ + int x, y, cap; + double cost;// x->y, v + int next; +} edge[1000005]; +int e, head[600], prev_[600], record[600], inq[600]; +double dis[600]; +void addEdge(int x, int y, int cap, double cost) +{ + edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost; + edge[e].next = head[y], head[y] = e++; +} +double mincost(int s, int t, int n) +{ + double mncost = 0; + int flow, totflow = 0; + int i, x, y; + double oo = 1e+30; + while (1) + { + for (int i = 0; i <= n; i++) + dis[i] = oo; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(), Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + prev_[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + Q.push_front(y); + else + Q.push_back(y); + } + } + } + } + if (dis[t] == oo) + break; + flow = 0x3f3f3f3f; + for (x = t; x != s; x = prev_[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = prev_[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return mncost; +} +int main() +{ + int cases = 0; + int N, X, Y; + int x[128], y[128], u; + while (scanf("%d %d %d", &N, &X, &Y) == 3 && N) + { + vector g[128]; + for (int i = 1; i <= N; i++) + { + scanf("%d %d", &x[i], &y[i]); + while (scanf("%d", &u) == 1 && u) + g[i].push_back(u); + } + + e = 0; + memset(head, -1, sizeof(head)); + int deg[128] = {}; + double sumCost = 0; + for (int i = 1; i <= N; i++) + { + for (int j = 0; j < g[i].size(); j++) + { + u = g[i][j]; + double d = hypot(x[i] - x[u], y[i] - y[u]); + double cost = d * X - Y; + if (cost < 0) + { + addEdge(u, i, 1, -cost); + } + else + { + sumCost += cost; + addEdge(i, u, 1, cost); + deg[u]--, deg[i]++; + } + } + } + int source = 0, sink = N + 1; + for (int i = 1; i <= N; i++) + { + if (deg[i] > 0) + addEdge(source, i, deg[i], 0); + if (deg[i] < 0) + addEdge(i, sink, -deg[i], 0); + } + double mncost = mincost(source, sink, N + 1); + printf("Case %d: %.2lf\n", ++cases, sumCost - mncost + eps); + } + return 0; +} diff --git a/166.cpp b/166.cpp new file mode 100644 index 0000000..f94fbaf --- /dev/null +++ b/166.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +struct MakingChange +{ + int coin_face[10], price, coin_used, min_coin; + + int readData(); + void solve(int index, int sum, int cnt); + int min_coinChange(int change); + void init(); + void print(); +} mc; + +int MakingChange::readData() +{ + bool all_zero = true; + for (int loop = 0; loop < 6; loop++) + { + cin >> coin_face[loop]; + if (coin_face[loop]) + { + all_zero = false; + } + } + if (all_zero) + { + return 0; + } + else + { + double tmp; + cin >> tmp; + price = (int)(tmp * 100.0 + 0.5); + return 1; + } +} + +void MakingChange::init() +{ + coin_used = 0; + min_coin = 0x7fffffff; +} + +int MakingChange::min_coinChange(int change) +{ + int value, cnt = 0; + value = change; + while (value) + { + if (value < 200) + { + break; + } + cnt++; + value -= 200; + } + while (value) + { + if (value < 100) + { + break; + } + cnt++; + value -= 100; + } + while (value) + { + if (value < 50) + { + break; + } + cnt++; + value -= 50; + } + while (value) + { + if (value < 20) + { + break; + } + cnt++; + value -= 20; + } + while (value) + { + if (value < 10) + { + break; + } + cnt++; + value -= 10; + } + while (value) + { + cnt++; + value -= 5; + } + return cnt; +} + +void MakingChange::solve(int index, int sum, int cnt) +{ + if (sum >= price) + { + int temp; + temp = cnt + min_coinChange(sum - price); + if (temp < min_coin) + { + min_coin = temp; + } + if (index == 6) + { + return; + } + } + else + { + int loop; + for (loop = 0; loop <= coin_face[index]; loop++) + { + switch (index) + { + case 0: + solve(index + 1, sum + loop * 5, cnt + loop); + break; + case 1: + solve(index + 1, sum + loop * 10, cnt + loop); + break; + case 2: + solve(index + 1, sum + loop * 20, cnt + loop); + break; + case 3: + solve(index + 1, sum + loop * 50, cnt + loop); + break; + case 4: + solve(index + 1, sum + loop * 100, cnt + loop); + break; + case 5: + solve(index + 1, sum + loop * 200, cnt + loop); + break; + } + } + } +} + +void MakingChange::print() +{ + printf("%3d\n", min_coin); +} + +int main() +{ + while (mc.readData()) + { + mc.init(); + mc.solve(0, 0, 0); + mc.print(); + } + return 0; +} diff --git a/1660.cpp b/1660.cpp new file mode 100644 index 0000000..41921af --- /dev/null +++ b/1660.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; + +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; + +struct MaxFlow +{ + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + + void init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + { + adj[i] = NULL; + } + } + + void add_edge(int x, int y, int flow) + { + edge[e].v = y; + edge[e].cap = flow; + edge[e].next = adj[x]; + edge[e].re = &edge[e + 1]; + adj[x] = &edge[e++]; + edge[e].v = x; + edge[e].cap = 0; + edge[e].next = adj[y]; + edge[e].re = &edge[e - 1]; + adj[y] = &edge[e++]; + } + + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + { + level[i] = n; + lvCnt[i] = 0; + } + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + { + ++dis; + r = rear; + } + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + { + level[t] = dis; + Q[rear++] = t; + ++lvCnt[dis]; + } + } + } + } + int maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + { + pre[i] = NULL; + arc[i] = adj[i]; + } + for (i = 0; i < e; ++i) + { + edge[i].flow = edge[i].cap; + } + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + { + arc[i] = arc[i]->next; + } + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + { + if (update > p->flow) + { + update = p->flow; + } + } + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + { + p->flow -= update, p->re->flow += update; + } + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + { + if (p->flow && depth > level[p->v]) + { + depth = level[p->v]; + } + } + if (--lvCnt[level[i]] == 0) + { + return ret; + } + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + { + i = pre[i]->re->v; + } + } + } + return ret; + } +} g; + +int main() +{ + int N, M, x, y; + while (scanf("%d%d", &N, &M) == 2) + { + g.init(2 * N); + for (int i = 0; i < M; i++) + { + scanf(" (%d,%d)", &x, &y); + g.add_edge(x * 2 + 1, y * 2, INF); + g.add_edge(y * 2 + 1, x * 2, INF); + } + for (int i = 0; i < N; i++) + { + g.add_edge(i * 2, i * 2 + 1, 1); + } + int ret = INF; + for (int i = 1; i < N; i++) + { + int flow = g.maxflow(0 * 2 + 1, i * 2); + ret = min(ret, flow); + } + printf("%d\n", ret == INF ? N : ret); + } + return 0; +} diff --git a/1661.cpp b/1661.cpp new file mode 100644 index 0000000..f23d9a5 --- /dev/null +++ b/1661.cpp @@ -0,0 +1,295 @@ +#include + +using namespace std; + +#define MAXN 65536 + +struct Frac +{ + long long x, y; + + Frac(long long a = 0, long long b = 1) + { + x = a, y = b; + norm(); + } + + void norm() + { + if (y < 0) + { + x = -x; + y = -y; + } + long long g = llgcd(x, y); + x /= g; + y /= g; + if (y < 0) + { + x = -x; + y = -y; + } + } + long long llgcd(long long x, long long y) const + { + long long t; + while (x % y) + { + t = x; + x = y; + y = t % y; + } + return y; + } + Frac operator-(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x - vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator+(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x + vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator*(const Frac a) const + { + long long g1 = llgcd(x, a.y), g2 = llgcd(a.x, y); + long long va = 0, vb = 0; + va = (x / g1) * (a.x / g2); + vb = (y / g2) * (a.y / g1); + return Frac(va, vb); + } + Frac operator/(const Frac a) const + { + assert(a.x); + long long g1 = llgcd(y, a.y), g2 = llgcd(x, a.x); + long long va = 0, vb = 0; + va = (a.y / g1) * (x / g2); + vb = (y / g1) * (a.x / g2); + return Frac(va, vb); + } + bool operator==(const Frac &a) const + { + return x - a.x == 0 && y - a.y == 0; + } + bool operator<(const Frac &a) const + { + return x * a.y < a.x * y; + } + void print() + { + printf("%lld/%lld\n", x, y); + } +}; +vector ret; + +struct Node +{ + Node *l, *r; + Frac val; + int state; + string token; + Node(Frac c = Frac(), Node *a = NULL, Node *b = NULL) + : l(a), r(b), val(c) + { + state = 0; + } +} nodes[MAXN]; + +int dfs(Node *u) +{ + int hasX = 0; + if (u->l != NULL) + { + hasX |= dfs(u->l); + } + if (u->r != NULL) + { + hasX |= dfs(u->r); + } + if (u->state == 1) + { + hasX = 1; + } + else if (u->state == 0) + { + // assert(u->l && u->r); + if (u->l->state == 2 && u->r->state == 2) + { + u->state = 2; + if (u->token == "+") + { + u->val = u->l->val + u->r->val; + } + else if (u->token == "-") + { + u->val = u->l->val - u->r->val; + } + else if (u->token == "*") + { + u->val = u->l->val * u->r->val; + } + else if (u->token == "/") + { + u->val = u->l->val / u->r->val; + } + } + if (u->l->state == 2 && u->l->val == Frac(0, 1) && u->token == "*") + { + u->state = 2; + u->val = Frac(0, 1); + } + if (u->r->state == 2 && u->r->val == Frac(0, 1) && u->token == "*") + { + u->state = 2; + u->val = Frac(0, 1); + } + } + return hasX; +} + +void dfs2(Node *u, Frac tgt) +{ + if (u->r != NULL && u->r->state == 2) + { + Frac t; + if (u->token == "+") + { + t = tgt - u->r->val; + } + else if (u->token == "-") + { + t = tgt + u->r->val; + } + else if (u->token == "*") + { + if (u->r->val.x == 0) + { + ret.push_back(Frac()); + ret.push_back(Frac()); + return; + } + t = tgt / u->r->val; + } + else if (u->token == "/") + { + t = tgt * u->r->val; + } + dfs2(u->l, t); + } + if (u->l != NULL && u->l->state == 2) + { + Frac t; + if (u->token == "+") + { + t = tgt - u->l->val; + } + else if (u->token == "-") + { + t = u->l->val - tgt; + } + else if (u->token == "*") + { + if (u->l->val.x == 0) + { + ret.push_back(Frac()); + ret.push_back(Frac()); + return; + } + t = tgt / u->l->val; + } + else if (u->token == "/") + { + if (u->l->val.x == 0 && tgt.x == 0) + { + ret.push_back(Frac()); + ret.push_back(Frac()); + return; + } + if (u->l->val.x == 0 || tgt.x == 0) + { + return; + } + t = u->l->val / tgt; + } + dfs2(u->r, t); + } + if (u->state == 1) + { + ret.push_back(tgt); + } +} + +int main() +{ + string line, token; + int x; + while (getline(cin, line)) + { + int nodesize = 0; + Node *p; + stringstream sin(line); + stack stk; + while (sin >> token) + { + if (token == "X") + { + p = &nodes[nodesize++]; + *p = Node(), p->state = 1, p->token = token; + stk.push(p); + } + else if (token == "*" || token == "/" || token == "+" || token == "-") + { + p = &nodes[nodesize++]; + *p = Node(), p->state = 0, p->token = token; + p->r = stk.top(), stk.pop(); + p->l = stk.top(), stk.pop(); + stk.push(p); + } + else + { + stringstream vin(token); + vin >> x; + p = &nodes[nodesize++]; + *p = Node(), p->state = 2, p->token = token; + p->val = Frac(x, 1); + stk.push(p); + } + } + if (stk.size() == 0) + { + continue; + } + Node *root = stk.top(); + ret.clear(); + dfs(root); + if (root->state == 2) + { + if (root->val == Frac(0, 1)) + { + printf("MULTIPLE\n"); + } + else + { + printf("NONE\n"); + } + continue; + } + dfs2(root, Frac(0, 1)); + if (ret.size() == 1) + { + printf("X = %lld/%lld\n", ret[0].x, ret[0].y); + } + else if (ret.size() == 0) + { + printf("NONE\n"); + } + else + { + printf("MULTIPLE\n"); + } + } + return 0; +} diff --git a/1662.cpp b/1662.cpp new file mode 100644 index 0000000..49f635a --- /dev/null +++ b/1662.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +#define MAXN 1024 + +int op(char c) +{ + switch (c) + { + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + } + return 0; +} + +int one_item_plus(string exp) +{// x+x-x/x*x*x + int p = 0; + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + p++; + } + else if (exp[i] == ')') + { + p--; + } + else if (op(exp[i]) && p == 0) + { + if (exp[i] == '-' || exp[i] == '+') + { + return 1; + } + } + } + return 0; +} + +string flip_op(string exp, int flip) +{ + int p = 0; + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + p++; + } + else if (exp[i] == ')') + { + p--; + } + else if (op(exp[i]) && p == 0) + { + if (flip == 1) + { + if (exp[i] == '+') + { + exp[i] = '-'; + } + else if (exp[i] == '-') + { + exp[i] = '+'; + } + } + else if (flip == 2) + { + if (exp[i] == '*') + { + exp[i] = '/'; + } + else if (exp[i] == '/') + { + exp[i] = '*'; + } + } + } + } + return exp; +} + +string parse(string exp) +{ + if (exp.length() <= 1) + { + return exp; + } + string ret, left, right; + int p = 0; + int pos = -1, wp = 0x3f3f3f3f; + for (int i = 0; i < exp.length(); i++) + { + if (exp[i] == '(') + { + p++; + } + else if (exp[i] == ')') + { + p--; + } + else if (op(exp[i])) + { + if (p == 0) + { + if (op(exp[i]) <= wp) + { + wp = op(exp[i]); + pos = i; + } + } + } + } + if (pos == -1) + { + return parse(exp.substr(1, exp.length() - 2)); + } + left = exp.substr(0, pos); + right = exp.substr(pos + 1); + + left = parse(left); + right = parse(right); + if (exp[pos] == '+') + { + ret = left + exp[pos] + right; + } + else if (exp[pos] == '-') + { + right = flip_op(right, 1); + ret = left + exp[pos] + right; + } + else if (exp[pos] == '*') + { + if (one_item_plus(left)) + { + left = '(' + left + ')'; + } + if (one_item_plus(right)) + { + right = '(' + right + ')'; + } + ret = left + exp[pos] + right; + } + else if (exp[pos] == '/') + { + if (one_item_plus(left)) + { + left = '(' + left + ')'; + } + if (one_item_plus(right)) + { + right = '(' + right + ')'; + } + else + { + right = flip_op(right, 2); + } + ret = left + exp[pos] + right; + } + return ret; +} + +int main() +{ + string exp; + while (cin >> exp) + { + string ret = parse(exp); + printf("%s\n", ret.c_str()); + } + return 0; +} diff --git a/1663.cpp b/1663.cpp new file mode 100644 index 0000000..1380370 --- /dev/null +++ b/1663.cpp @@ -0,0 +1,226 @@ +#include + +using namespace std; + +// at most one switch can be turned to state '*' +// bipartite graph, maximum matching +const int MAXV = 40010; +const int MAXE = MAXV * 200 * 2; +const int INF = 1 << 29; + +typedef struct Edge +{ + int v, cap, flow; + Edge *next, *re; +} Edge; + +struct MaxFlow +{ + int e, n, level[MAXV], lvCnt[MAXV], Q[MAXV]; + Edge edge[MAXE], *adj[MAXV], *pre[MAXV], *arc[MAXV]; + + void init(int x) + { + n = x, e = 0; + for (int i = 0; i < n; ++i) + { + adj[i] = NULL; + } + } + + void add_edge(int x, int y, int flow) + { + edge[e].v = y; + edge[e].cap = flow; + edge[e].next = adj[x]; + edge[e].re = &edge[e + 1]; + adj[x] = &edge[e++]; + edge[e].v = x; + edge[e].cap = 0; + edge[e].next = adj[y]; + edge[e].re = &edge[e - 1]; + adj[y] = &edge[e++]; + } + + void Bfs(int v) + { + int front = 0, rear = 0, r = 0, dis = 0; + for (int i = 0; i < n; ++i) + { + level[i] = n; + lvCnt[i] = 0; + } + level[v] = 0, ++lvCnt[0]; + Q[rear++] = v; + while (front != rear) + { + if (front == r) + { + ++dis; + r = rear; + } + v = Q[front++]; + for (Edge *i = adj[v]; i != NULL; i = i->next) + { + int t = i->v; + if (level[t] == n) + { + level[t] = dis; + Q[rear++] = t; + ++lvCnt[dis]; + } + } + } + } + int maxflow(int s, int t) + { + int ret = 0, i, j; + Bfs(t); + for (i = 0; i < n; ++i) + { + pre[i] = NULL; + arc[i] = adj[i]; + } + for (i = 0; i < e; ++i) + { + edge[i].flow = edge[i].cap; + } + i = s; + while (level[s] < n) + { + while (arc[i] && (level[i] != level[arc[i]->v] + 1 || !arc[i]->flow)) + { + arc[i] = arc[i]->next; + } + if (arc[i]) + { + j = arc[i]->v; + pre[j] = arc[i]; + i = j; + if (i == t) + { + int update = INF; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + { + if (update > p->flow) + { + update = p->flow; + } + } + ret += update; + for (Edge *p = pre[t]; p != NULL; p = pre[p->re->v]) + { + p->flow -= update, p->re->flow += update; + } + i = s; + } + } + else + { + int depth = n - 1; + for (Edge *p = adj[i]; p != NULL; p = p->next) + { + if (p->flow && depth > level[p->v]) + { + depth = level[p->v]; + } + } + if (--lvCnt[level[i]] == 0) + { + return ret; + } + level[i] = depth + 1; + ++lvCnt[level[i]]; + arc[i] = adj[i]; + if (i != s) + { + i = pre[i]->re->v; + } + } + } + return ret; + } +} g; + +int color[1024] = {}; +vector D;// diff + +int main() +{ + for (int i = 0; i < 1024; i++) + {// graph + for (int j = 0; j < i; j++) + { + if (__builtin_popcount(i ^ j) == 1) + { + color[i] = !color[j]; + break; + } + } + } + char s[16]; + int n, m; + while (scanf("%d%d", &m, &n) == 2 && m) + { + D.clear(); + + int used[1024] = {}; + for (int i = 0; i < n; i++) + { + scanf("%s", s); + int val = 0, star = -1; + for (int j = 0; j < m; j++) + { + if (s[j] == '*') + { + star = 1 << j; + } + else + { + val |= (s[j] - '0') << j; + } + } + if (star >= 0) + { + D.push_back(val); + D.push_back(val + star); + } + else + { + D.push_back(val); + } + } + sort(D.begin(), D.end()); + D.resize(unique(D.begin(), D.end()) - D.begin()); + + for (int i = 0; i < D.size(); i++) + { + used[D[i]] = 1; + } + + g.init(1024 + 5); + int src = 1024, sink = 1024 + 1; + for (int i = 0; i < D.size(); i++) + { + if (color[D[i]] == 0) + { + for (int j = 0; j < m; j++) + { + if (used[D[i] ^ (1 << j)]) + { + g.add_edge(D[i], D[i] ^ (1 << j), 1); + } + } + g.add_edge(src, D[i], 1); + } + else + { + g.add_edge(D[i], sink, 1); + } + } + + int ret = (int)D.size() - g.maxflow(src, sink); + printf("%d\n", ret); + } + return 0; +} diff --git a/1664.cpp b/1664.cpp new file mode 100644 index 0000000..1c7e515 --- /dev/null +++ b/1664.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 262144 + +struct edge +{ + int x, y; + long long v; + edge(int a = 0, int b = 0, long long c = 0) + : x(a), y(b), v(c) + { + } + bool operator<(const edge &a) const + { + return v > a.v; + } +}; + +int parent[MAXN], weight[MAXN]; +// dp[u]: u as root, each node will get dp[u] for C(i, j) +long long dp[MAXN]; + +int findp(int x) +{// disjoint set, forest + int p = parent[x]; + if (p == x) + { + return x; + } + p = findp(p); + if (p != parent[x])// extra process + { + dp[x] += dp[parent[x]]; + } + return parent[x] = p; +} + +int main() +{ + int N, x, y, px, py; + long long v; + while (scanf("%d", &N) == 1) + { + vector D; + for (int i = 0; i <= N; i++) + { + parent[i] = i; + weight[i] = 1; + dp[i] = 0; + } + for (int i = 1; i < N; i++) + { + scanf("%d%d%lld", &x, &y, &v); + D.push_back(edge(x, y, v)); + } + sort(D.begin(), D.end()); + for (int i = 0; i < D.size(); i++) + { + x = D[i].x, y = D[i].y, v = D[i].v; + px = findp(x), py = findp(y); + dp[px] += weight[py] * v; + dp[py] += weight[px] * v - dp[px]; + parent[py] = px, weight[px] += weight[py]; + } + long long ret = 0; + for (int i = 1; i <= N; i++) + { + findp(i); + long long Cij = dp[i]; + if (parent[i] != i) + { + Cij += dp[parent[i]]; + } + ret = max(ret, Cij); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/1665.cpp b/1665.cpp new file mode 100644 index 0000000..d1e1350 --- /dev/null +++ b/1665.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +#define MAXN 1024 +int g[MAXN][MAXN], h[131072], out[131072]; +int parent[MAXN * MAXN], weight[MAXN * MAXN]; + +struct Pos +{ + int x, y, h; + Pos(int a = 0, int b = 0, int c = 0) : x(a), y(b), h(c) {} + bool operator<(const Pos &a) const + { + return h > a.h; + } +}; + +int findp(int x) +{ + return parent[x] == x ? x : parent[x] = findp(parent[x]); +} +int joint(int x, int y) +{ + x = findp(x), y = findp(y); + if (x == y) + return 0; + if (weight[x] > weight[y]) + weight[x] += weight[y], parent[y] = x; + else + weight[y] += weight[x], parent[x] = y; + return 1; +} +int main() +{ + const int dx[] = {0, 0, 1, -1}; + const int dy[] = {1, -1, 0, 0}; + int testcase, n, m, q, tx, ty; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%d", &g[i][j]); + scanf("%d", &q); + for (int i = 0; i < q; i++) + scanf("%d", &h[i]); + + vector D; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + D.push_back(Pos(i, j, g[i][j])); + } + } + sort(D.begin(), D.end()); + int idx = 0, nm = n * m, sum = 0; + Pos u; + for (int i = nm; i >= 0; i--) + parent[i] = i, weight[i] = 1; + for (int i = q - 1; i >= 0; i--) + { + while (idx < nm && D[idx].h > h[i]) + { + sum++; + u = D[idx++]; + // printf("add %d %d - %d\n", u.x, u.y, u.h); + for (int j = 0; j < 4; j++) + { + tx = u.x + dx[j], ty = u.y + dy[j]; + if (tx < 0 || ty < 0 || tx >= n || ty >= m) + continue; + if (g[tx][ty] > h[i]) + { + sum -= joint(u.x * m + u.y, tx * m + ty); + } + } + } + out[i] = sum; + // printf("%d\n", sum); + } + for (int i = 0; i < q; i++) + printf("%d ", out[i]); + puts(""); + } + return 0; +} diff --git a/1666.cpp b/1666.cpp new file mode 100644 index 0000000..d19e4f6 --- /dev/null +++ b/1666.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +// can walk on edge of rect +// split pt(x, y) to 3x3 sq.; 2 rect shared common edge will proc gap + +#define MAXN 64 +#define MAXW 512 + +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; +const int ddx[] = {1, 1, -1, -1}; +const int ddy[] = {1, -1, 1, -1}; + +int g[MAXW][MAXW], dist[MAXW][MAXW][4]; +map RX, RY; + +int relbl_map(map &R) +{ + int sz = 1; + for (map::iterator it = R.begin(); it != R.end(); it++) + { + it->second = sz++; + } + return sz; +} + +void fill_map(int lx, int ly, int rx, int ry, int val) +{ + int tx, ty, vx, vy; + int a = RX[lx] * 3 + 2; + int b = RX[rx] * 3; + int p = RY[ly] * 3 + 2; + int q = RY[ry] * 3; + for (int i = a; i <= b; i++) + { + for (int j = p; j <= q; j++) + { + g[i][j] = val; + // fill gap + for (int k = 0; k < 4; k++) + { + tx = i + dx[k] * 2, ty = j + dy[k] * 2; + vx = i + dx[k] * 1, vy = j + dy[k] * 1; + if (g[tx][ty] == val) + { + g[vx][vy] = val; + } + tx = i + ddx[k] * 2, ty = j + ddy[k] * 2; + vx = i + ddx[k] * 1, vy = j + ddy[k] * 1; + if (g[tx][ty] == val) + { + g[vx][vy] = val; + } + } + } + } +} + +pair fill_pt_map(int lx, int ly, int val) +{ + int a = RX[lx] * 3 + 1; + int b = RY[ly] * 3 + 1; + g[a][b] = val; + return make_pair(a, b); +} + +void bfs(int sx, int sy, int ex, int ey) +{ + pair st, ed; + int x, y, d, tx, ty; + queue X, Y, D; + + st = fill_pt_map(sx, sy, 2); + ed = fill_pt_map(ex, ey, 3); + memset(dist, 63, sizeof(dist)); + int INF = dist[0][0][0]; + for (int i = 0; i < 4; i++) + { + X.push(st.first), Y.push(st.second), D.push(i); + dist[st.first][st.second][i] = 0; + } + + int ret = INF; + while (!X.empty()) + { + x = X.front(); + X.pop(); + y = Y.front(); + Y.pop(); + d = D.front(); + D.pop(); + if (dist[x][y][d] >= ret) + { + continue; + } + for (int i = 0; i < 4; i++) + { + int w = (d != i); + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= MAXW || ty >= MAXW || g[tx][ty] == 1) + { + continue; + } + if (dist[tx][ty][i] > dist[x][y][d] + w) + { + dist[tx][ty][i] = dist[x][y][d] + w; + if (tx == ed.first && ty == ed.second) + { + ret = min(ret, dist[tx][ty][i]); + continue; + } + X.push(tx); + Y.push(ty); + D.push(i); + } + } + } + printf("%d\n", ret == INF ? -1 : ret); +} + +int main() +{ + int sx, sy, ex, ey, N, lx[MAXN], ly[MAXN], rx[MAXN], ry[MAXN]; + while (scanf("%d%d%d%d", &sx, &sy, &ex, &ey) == 4) + { + if (sx == 0 && sy == 0 && ex == 0 && ey == 0) + { + return 0; + } + RX.clear(); + RY.clear(); + scanf("%d", &N); + for (int i = 0; i < N; i++) + { + scanf("%d%d%d%d", &lx[i], &ly[i], &rx[i], &ry[i]); + if (lx[i] > rx[i]) + { + swap(lx[i], rx[i]); + } + if (ly[i] > ry[i]) + { + swap(ly[i], ry[i]); + } + RX[lx[i]] = RX[rx[i]] = 1; + RY[ly[i]] = RY[ry[i]] = 1; + } + RX[sx] = RX[ex] = 1; + RY[sy] = RY[ey] = 1; + relbl_map(RX); + relbl_map(RY); + memset(g, 0, sizeof(g)); + for (int i = 0; i < N; i++) + { + fill_map(lx[i], ly[i], rx[i], ry[i], 1); + } + bfs(sx, sy, ex, ey); + } + return 0; +} diff --git a/1667.cpp b/1667.cpp new file mode 100644 index 0000000..29b1689 --- /dev/null +++ b/1667.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define MAXN 64 + +int g[MAXN][MAXN]; + +// during every iteration each leaf is contracted with its parent, until +// * 1 leaf left +// * 2 leaves left, and they connected by a single edge +// a vertex i is a leaf only when d[j][i]+d[i][k]>d[j][k] for all vertices j, k. +// if a leaf i is still a leaf(instead of becoming an internal node) after contraction, then there is a router with degree n+2, where n is the number of vertices having zero distance with i. +// special case: router's degree is n+1 if it is the final iteration, ending with condition 1 above +int is_leaf(int N, int x, int used[]) +{ + int ret = 1; + for (int j = 0; j < N && ret; j++) + { + for (int k = 0; k < N && ret; k++) + { + if (used[j] || used[k])// 0: leaf, 1: inner, 2: del + { + continue; + } + if (j == k || x == j || x == k) + { + continue; + } + ret &= g[j][x] + g[x][k] > g[j][k]; + } + } + return ret; +} + +int main() +{ + int N; + while (scanf("%d", &N) == 1 && N) + { + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + scanf("%d", &g[i][j]); + } + } + int used[MAXN] = {}; + vector ret; + while (true) + { + vector leaf; + for (int i = 0; i < N; i++) + { + if (used[i] == 2)// del + { + continue; + } + int b = is_leaf(N, i, used); + if (b) + { + leaf.push_back(i); + } + else + { + used[i] = 1; + } + } + int x = leaf[0]; + for (int i = 0; i < N; i++) + { + if (i == x) + { + continue; + } + if (g[x][i]) + { + g[x][i]--; + g[i][x]--; + } + } + if (is_leaf(N, x, used)) + { + int router = 0; + for (int i = 0; i < N; i++) + { + if (used[i] == 2 || i == x) + { + continue; + } + if (g[x][i] == 0) + { + router++; + used[i] = 2; + } + } + ret.push_back(router + 2); + } + if (leaf.size() == 1 || (leaf.size() == 2 && g[leaf[0]][leaf[1]] == 1)) + { + break; + } + } + sort(ret.begin(), ret.end()); + for (int i = 0; i < ret.size(); i++) + { + printf("%d%c", ret[i], i == ret.size() - 1 ? '\n' : ' '); + } + } + return 0; +} diff --git a/1668.cpp b/1668.cpp new file mode 100644 index 0000000..a9576c7 --- /dev/null +++ b/1668.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +#define MAXN 100005 + +int ret = 0; +vector > g[MAXN]; + +void dfs(int u, int p, int pw) +{ + int mx = pw, sum = pw; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i].first; + if (v == p) + { + continue; + } + dfs(v, u, g[u][i].second); + mx = max(mx, g[u][i].second); + sum += g[u][i].second; + } + if (mx * 2 >= sum) + { + // exactly gen #mx-pw path, connected or not + ret += mx - pw; + } + else + { + // pair branch connect(completed path), reserve #pw to parent + ret += (sum + 1) / 2 - pw; + } +} + +int main() +{ + int T, t = 0, N, x, y, v; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i <= N; i++) + { + g[i].clear(); + } + for (int i = 1; i < N; i++) + { + scanf("%d%d%d", &x, &y, &v); + g[x].push_back(make_pair(y, v)); + g[y].push_back(make_pair(x, v)); + } + ret = 0; + dfs(1, -1, 0); + printf("Case #%d: %d\n", ++t, ret); + } + return 0; +} diff --git a/1669.cpp b/1669.cpp new file mode 100644 index 0000000..04af388 --- /dev/null +++ b/1669.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +#define MAXN 262144 + +int visited[MAXN], parent[MAXN], tree[MAXN]; +vector > g[MAXN]; + +void solve(int N) +{ + for (int i = 0; i <= N; i++) + { + visited[i] = 0; + } + queue Q; + vector A; + int u, v; + Q.push(1); + visited[1] = 1; + parent[1] = -1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + A.push_back(u); + for (int i = 0; i < g[u].size(); i++) + { + v = g[u][i].first; + if (visited[v] == 0) + { + visited[v] = 1; + Q.push(v); + parent[v] = u; + } + } + } + for (int i = (int)A.size() - 1; i >= 0; i--) + { + u = A[i]; + int sz = 1; + for (int j = 0; j < g[u].size(); j++) + { + v = g[u][j].first; + if (v == parent[u]) + { + continue; + } + sz += tree[v]; + } + tree[u] = sz; + } + long long ret = 0; + for (int i = 1; i <= N; i++) + { + u = i; + for (int j = 0; j < g[u].size(); j++) + { + v = g[u][j].first; + if (v == parent[u]) + { + continue; + } + int pass = min(tree[v], N - tree[v]) * 2;// in&out + ret += (long long)g[u][j].second * pass; + } + } + printf("%lld\n", ret); +} + +int main() +{ + int N, x, y, v, T, cases = 0; + scanf("%d", &T); + while (T--) + { + scanf("%d", &N); + for (int i = 0; i <= N; i++) + { + g[i].clear(); + } + for (int i = 1; i < N; i++) + { + scanf("%d%d%d", &x, &y, &v); + g[x].push_back(make_pair(y, v)); + g[y].push_back(make_pair(x, v)); + } + printf("Case #%d: ", ++cases); + solve(N); + } + return 0; +} diff --git a/167.cpp b/167.cpp new file mode 100644 index 0000000..f649a0e --- /dev/null +++ b/167.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +#define MAX 10 +int A[MAX]; +int BORD[MAX][MAX]; +int MAXVALUE; +char F[MAX]; + +int ABS(int n) +{ + if (n < 0) + { + return -n; + } + else + { + return n; + } +} + +void MAXIMUM(int n) +{ + int i, c = 0; + for (i = 0; i < n; i++) + { + int x = A[i]; + c += BORD[i][x - 1]; + } + if (c > MAXVALUE) + { + MAXVALUE = c; + } +} + +void PERM(int level) +{ + int x = level - 1; + for (int j = level - 2; j >= 0; j--) + { + int p = j; + if (x >= 0 && p >= 0) + { + int y = A[x]; + int q = A[p]; + if (ABS(x - p) == ABS(y - q)) + { + return; + } + } + } + if (level == 8) + { + MAXIMUM(8); + return; + } + for (int i = 1; i <= 8; i++) + { + if (F[i] == 0) + { + F[i] = 1; + A[level] = i; + PERM(level + 1); + F[i] = 0; + } + } + return; +} + +int main() +{ + int N; + scanf("%d", &N); + while (N--) + { + MAXVALUE = 0; + for (int i = 0; i < 8; i++) + for (int j = 0; j < 8; j++) + { + scanf("%d", &BORD[i][j]); + } + PERM(0); + printf("%5d\n", MAXVALUE); + } + return 0; +} diff --git a/1671.cpp b/1671.cpp new file mode 100644 index 0000000..99deea0 --- /dev/null +++ b/1671.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +#define MAXN 2048 +#define MAXT 32 + +struct DFA +{ +public: + int N, T, st, trans[MAXN][MAXT], ac[MAXN]; + void init(int N, int T) + { + this->N = N; + this->T = T; + st = 0; + } + void read() + { + for (int i = 0; i < N; i++) + { + scanf("%d", &ac[i]); + for (int j = 0; j < T; j++) + { + scanf("%d", &trans[i][j]); + } + } + } +} A, B; + +int visited[MAXN][MAXN]; + +int no_intersect(DFA &u, DFA &v) +{// u' and v != empty + static int cases = 0; + ++cases; + queue P, Q; + int p, q, x, y; + P.push(u.st); + Q.push(v.st); + visited[u.st][v.st] = cases; + while (!Q.empty()) + { + p = P.front(); + P.pop(); + q = Q.front(); + Q.pop(); + if (!u.ac[p] && v.ac[q]) + { + return 0; + } + for (int i = 0; i < u.T; i++) + { + x = u.trans[p][i]; + y = v.trans[q][i]; + if (x == -1 && y != -1 && v.ac[y]) + { + return 0; + } + if (x == -1 || y == -1 || visited[x][y] == cases) + { + continue; + } + visited[x][y] = cases; + P.push(x), Q.push(y); + } + } + cases++; + return 1; +} + +int main() +{ + int T, N, cases = 0; + while (scanf("%d", &T) == 1 && T) + { + scanf("%d", &N); + A.init(N, T); + A.read(); + scanf("%d", &N); + B.init(N, T); + B.read(); + int f = no_intersect(A, B) && no_intersect(B, A); + printf("Case #%d: %s\n", ++cases, f ? "Yes" : "No"); + } + return 0; +} diff --git a/1673.cpp b/1673.cpp new file mode 100644 index 0000000..519541b --- /dev/null +++ b/1673.cpp @@ -0,0 +1,139 @@ +#include +using namespace std; + +struct SuffixAutomaton +{ + static const int MAXN = 262144; + static const int MAXC = 11; + + struct Node + { + Node *next[MAXC], *pre; + int step; + int way, sum; + Node() + { + pre = NULL, step = 0; + memset(next, 0, sizeof(next)); + way = 0; + sum = 0; + } + } _mem[MAXN], *order[MAXN]; + + int size, cnt[MAXN]; + Node *root, *tail; + + inline int to_idx(char c) + { + if (isdigit(c)) + { + return c - '0'; + } + return 10; + } + void init() + { + size = 0; + root = tail = node_new(); + } + Node *node_new() + { + Node *p = &_mem[size++]; + *p = Node(); + return p; + } + void add(char c, int len) + { + Node *p, *q, *np, *nq; + c = to_idx(c); + p = tail, np = node_new(); + np->step = len; + for (; p && p->next[c] == NULL; p = p->pre) + p->next[c] = np; + tail = np; + if (p == NULL) + { + np->pre = root; + } + else + { + if (p->next[c]->step == p->step + 1) + { + np->pre = p->next[c]; + } + else + { + q = p->next[c], nq = node_new(); + *nq = *q; + nq->step = p->step + 1; + q->pre = np->pre = nq; + for (; p && p->next[c] == q; p = p->pre) + p->next[c] = nq; + } + } + } + void topo() + {// radix sort + for (int i = 0; i < size; i++) + cnt[i] = 0; + for (int i = 0; i < size; i++) + cnt[_mem[i].step]++; + for (int i = 1; i < size; i++) + cnt[i] += cnt[i - 1]; + for (int i = 0; i < size; i++) + order[--cnt[_mem[i].step]] = &_mem[i]; + } + +public: + void build(const char *s) + { + init(); + for (int i = 0; s[i]; i++) + add(s[i], i + 1); + topo(); + } + int unique_sum(const char *s) + { + int ret = 0; + root->way = 1; + for (int i = 0; i < size; i++) + { + Node *u = order[i]; + ret = (ret + u->sum) % 2012; + for (int j = 0; j < 10; j++) + { + if (i == 0 && j == 0) + continue; + if (u->next[j]) + { + u->next[j]->way += u->way; + u->next[j]->sum += (u->sum * 10 + u->way * j) % 2012; + u->next[j]->way %= 2012; + u->next[j]->sum %= 2012; + } + } + } + return ret; + } +} SAM; + +char s[262144]; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + char *ptr = s; + for (int i = 0; i < n; i++) + { + scanf("%s", ptr); + for (; *ptr; ptr++) + ; + *ptr = '$', ptr++, *ptr = '\0'; + } + SAM.build(s); + printf("%d\n", SAM.unique_sum(s)); + } + return 0; +} diff --git a/1674.cpp b/1674.cpp new file mode 100644 index 0000000..94619d6 --- /dev/null +++ b/1674.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define MAXV 65536 +int visited[MAXV]; +vector tree[MAXV]; +int parent[MAXV], weight[MAXV]; +int findp(int x) +{ + return parent[x] == x ? x : (parent[x] = findp(parent[x])); +} +int joint(int x, int y) +{ + x = findp(x), y = findp(y); + if (x == y) + return 0; + if (weight[x] > weight[y]) + weight[x] += weight[y], parent[y] = x; + else + weight[y] += weight[x], parent[x] = y; + return 1; +} + +// LCA +vector> Q[MAXV];// query pair, input index - node +int LCA[131072]; // [query time] input query answer buffer. +void tarjan(int u, int p) +{// rooted-tree. + parent[u] = u; + for (int i = 0; i < tree[u].size(); i++) + {//son node. + int v = tree[u][i]; + if (v == p) + continue; + tarjan(v, u); + parent[findp(v)] = u; + } + visited[u] = 1; + for (int i = 0; i < Q[u].size(); i++) + { + if (visited[Q[u][i].second]) + { + LCA[Q[u][i].first] = findp(Q[u][i].second); + } + } +} +int dfs(int u, int p, int weight[]) +{ + int sum = weight[u]; + for (int i = 0; i < tree[u].size(); i++) + {//son node. + int v = tree[u][i]; + if (v == p) + continue; + sum += dfs(v, u, weight); + } + return weight[u] = sum; +} +int main() +{ + // freopen("in.txt", "r+t", stdin); + // freopen("out2.txt", "w+t", stdout); + int n, m, x, y; + int testcase, cases = 0; + int X[MAXV], Y[MAXV], K[MAXV]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + tree[i].clear(); + for (int i = 1; i < n; i++) + { + scanf("%d %d", &x, &y); + tree[x].push_back(y); + tree[y].push_back(x); + } + + int weight[MAXV] = {}, extra[MAXV] = {}; + for (int i = 0; i < n; i++) + visited[i] = 0, Q[i].clear(); + scanf("%d", &m); + for (int i = 0; i < m; i++) + { + scanf("%d %d %d", &X[i], &Y[i], &K[i]); + Q[X[i]].push_back(make_pair(i, Y[i])); + Q[Y[i]].push_back(make_pair(i, X[i])); + } + tarjan(0, -1); + for (int i = 0; i < m; i++) + { + extra[LCA[i]] += K[i]; + weight[X[i]] += K[i]; + weight[Y[i]] += K[i]; + weight[LCA[i]] -= 2 * K[i]; + } + dfs(0, -1, weight); + + printf("Case #%d:\n", ++cases); + for (int i = 0; i < n; i++) + printf("%d\n", weight[i] + extra[i]); + } + return 0; +} diff --git a/1676.cpp b/1676.cpp new file mode 100644 index 0000000..0a77bc4 --- /dev/null +++ b/1676.cpp @@ -0,0 +1,275 @@ +#include + +using namespace std; + +#define MAXCHAR 2 +#define MAXS (5000005) +#define MAXNODE 526288 +#pragma comment(linker, "/STACK:1024000000,1024000000") + +class ACmachine +{ +public: + struct Node + { + Node *next[MAXCHAR], *fail; + int cnt, val; + void init() + { + fail = NULL; + cnt = val = 0; + memset(next, 0, sizeof(next)); + } + } nodes[MAXNODE]; + Node *root; + int size; + Node *getNode() + { + Node *p = &nodes[size++]; + p->init(); + return p; + } + void init() + { + size = 0; + root = getNode(); + } + int toIndex(char c) + { + return c - '0'; + } + void dfs(Node *p, Node *q) + { + for (int i = 0; i < MAXCHAR; i++) + { + if (q->next[i]) + { + Node *u = p->next[i], *v = q->next[i]; + if (u == NULL) + p->next[i] = getNode(), u = p->next[i]; + u->cnt |= v->cnt; + dfs(u, v); + } + } + } + void merge(const ACmachine &other) + { + dfs(root, other.root); + } + void insert(const char str[]) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + p->cnt = 1; + } + int find(const char str[]) + { + Node *p = root; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + if (p->next[idx] == NULL) + p->next[idx] = getNode(); + p = p->next[idx]; + } + return p->cnt; + } + void build() + {// AC automation + queue Q; + Node *u, *p; + Q.push(root), root->fail = NULL; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < MAXCHAR; i++) + { + if (u->next[i] == NULL) + continue; + Q.push(u->next[i]); + p = u->fail; + while (p != NULL && p->next[i] == NULL) + p = p->fail; + if (p == NULL) + u->next[i]->fail = root; + else + u->next[i]->fail = p->next[i]; + u->next[i]->val = u->next[i]->fail->val + u->next[i]->cnt; + } + } + } + int query(const char str[]) + { + Node *u = root, *p; + int matched = 0; + for (int i = 0, idx; str[i]; i++) + { + idx = toIndex(str[i]); + while (u->next[idx] == NULL && u != root) + u = u->fail; + u = u->next[idx]; + u = (u == NULL) ? root : u; + p = u; + matched += p->val; + } + return matched; + } + void free() + { + return; + // owner memory pool version + queue Q; + Q.push(root); + Node *u; + while (!Q.empty()) + { + u = Q.front(), Q.pop(); + for (int i = 0; i < MAXCHAR; i++) + { + if (u->next[i] != NULL) + { + Q.push(u->next[i]); + } + } + delete u; + } + } +}; +char s[MAXS]; +void decodeString(char s[], int L) +{ + static char buf[MAXS]; + int n = (int)strlen(s); + L %= n; + for (int i = L, j = 0; i < n; i++, j++) + buf[j] = s[i]; + for (int i = 0, j = n - L; i < L; i++, j++) + buf[j] = s[i]; + for (int i = 0; i < n; i++) + s[i] = buf[i]; +} +ACmachine cache, disk; +int main() +{ + int testcase, cases = 0; + int N; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &N); + printf("Case #%d:\n", ++cases); + int L = 0;// encrypted key + int cFlag = 0, dFlag = 0; + cache.init(), disk.init(); + for (int i = 0; i < N; i++) + { + scanf("%s", s); + decodeString(s + 1, L); +// printf("%s\n", s); +#define THRESHOLD 4096 + if (s[0] == '+') + { + if (disk.find(s + 1) || cache.find(s + 1)) + continue; + cache.insert(s + 1), cFlag = 0; + if (cache.size > THRESHOLD) + { + disk.merge(cache), dFlag = 0; + cache.free(); + cache.init(), cFlag = 0; + } + } + else + { + if (!cFlag) + cache.build(); + if (!dFlag) + disk.build(); + int ret = disk.query(s + 1) + cache.query(s + 1); + printf("%d\n", ret); + L = ret; + } + } + disk.free(), cache.free(); + } + return 0; +} +/* + 99999 + 10 + +01 + +110 + ?010 + +110 + +00 + +0 + ?001001 + ?001001 + +110110 + ?1101001101 + + 6 + +01 + +110 + +110 + +00 + +0 + ?001001 + + 20 + +101001011 + ?110100 + +11010100 + ?0011001101 + +111011 + ?00010011 + +0111010110 + +0000101 + +0 + +11000 + ?1 + +1010101 + +0001 + +0110 + +0111101111 + ?1100 + +0111 + +1001 + ?0110111011 + ?1010010100 + + 10 + +00 + ?010110100 + +0100000100 + +111 + +000000 + ?0000110 + +110 + +00 + +0011 + ?101001 + + 5 + +0 + +1000100 + +01 + +0 + ?1110010011 + + 2 + 3 + +01 + +01 + ?01001 + 3 + +01 + ?010 + ?011 + */ \ No newline at end of file diff --git a/168.cpp b/168.cpp new file mode 100644 index 0000000..0f33d15 --- /dev/null +++ b/168.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +int main() +{ + char c, v; + bool newvertex = true; + vector moves[26];// for each cavern there is a list of reachable caverns + // and it is listed in order that the Minotaur will attempt them + while (c = getchar()) + { + if (c == '#') + { + break; + } + if (c >= 'A' && c <= 'Z' && newvertex == true) + { + v = c; + newvertex = false; + c = getchar();// reading ':' + } + else if (c >= 'A' && c <= 'Z' && newvertex == false) + { + moves[v - 65].push_back(c); + } + else if (c == ';') + { + newvertex = true; + } + else if (c == '.') + { + newvertex = true; + char mstart; + bool mfound = false; + char tstart; + int k = 0; + while (c = getchar()) + { + if (c == '\n') + { + break; + } + if (c >= 'A' && c <= 'Z' && mfound == false) + { + mfound = true; + mstart = c; + } + else if (c >= 'A' && c <= 'Z' && mfound == true) + { + tstart = c; + } + else if (c >= '0' && c <= '9') + { + k = k * 10 + c - 48; + } + } + // start the movement of Theseus and the Minotaur + char prev = tstart; + int temp = 0;// how many moves where made - we need it to "set up candles" + while (true) + { + if (moves[mstart - 65].size() == 0) + { + cout << "/" << mstart << endl; + break; + } + else if (moves[mstart - 65].size() == 1 && moves[mstart - 65][0] == prev) + { + cout << "/" << mstart << endl; + break; + } + else + { + if (moves[mstart - 65][0] != prev) + { + prev = mstart; + mstart = moves[mstart - 65][0]; + } + else + { + prev = mstart; + mstart = moves[mstart - 65][1]; + } + //cout << "MINOTAUR MOVES TO: " << mstart << endl; + //cout << "AVAILABLE MOVES: "; + //for (int i = 0; i < moves[mstart-65].size(); i++) + // cout << moves[mstart-65][i] << " "; + //cout << endl; + ++temp; + if (temp == k) + { + cout << prev << " "; + temp = 0; + moves[prev - 65].clear(); + for (int i = 0; i < 26; i++) + { + for (vector::iterator it = moves[i].begin(); it != moves[i].end(); it++) + { + if ((*it) == prev) + { + moves[i].erase(it); + break; + } + } + } + } + } + } + for (int i = 0; i < 26; i++) + { + moves[i].clear(); + } + newvertex = true; + } + } + return 0; +} diff --git a/1683.cpp b/1683.cpp new file mode 100644 index 0000000..1aec9d8 --- /dev/null +++ b/1683.cpp @@ -0,0 +1,330 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define MAXN (1048576) +#define MAXV 262144 +struct Point +{ + double x, y; + int id; + Point(double a = 0, double b = 0, int c = -1) : x(a), y(b), id(c) {} + Point operator-(const Point &a) const + { + return Point(x - a.x, y - a.y); + } + Point operator+(const Point &a) const + { + return Point(x + a.x, y + a.y); + } + Point operator*(const double a) const + { + return Point(x * a, y * a); + } + Point operator/(const double a) const + { + return Point(x / a, y / a); + } + bool operator<(const Point &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + bool operator==(const Point &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + bool operator!=(const Point &a) const + { + return !(fabs(x - a.x) < eps && fabs(y - a.y) < eps); + } + void read(int id = -1) + { + this->id = id; + } + double dist(Point b) + { + return hypot(x - b.x, y - b.y); + } + double dist2(Point b) + { + return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y); + } + void print() + { + printf("point (%lf, %lf)\n", x, y); + } +}; +struct Point3D +{ + double x, y, z; + Point3D(double a = 0, double b = 0, double c = 0) : x(a), y(b), z(c) {} + Point3D(Point p) + { + x = p.x, y = p.y, z = p.x * p.x + p.y * p.y; + } + Point3D operator-(const Point3D &a) const + { + return Point3D(x - a.x, y - a.y, z - a.z); + } + double dot(Point3D a) + { + return x * a.x + y * a.y + z * a.z; + } +}; +struct Edge +{ + int id; + list::iterator twin; + Edge(int id = 0) + { + this->id = id; + } +}; + +int cmpZero(double v) +{ + if (fabs(v) > eps) + return v > 0 ? 1 : -1; + return 0; +} + +double cross(Point o, Point a, Point b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +Point3D cross(Point3D a, Point3D b) +{// normal vector + return Point3D(a.y * b.z - a.z * b.y, -a.x * b.z + a.z * b.x, a.x * b.y - a.y * b.x); +} +int inCircle(Point a, Point b, Point c, Point p) +{ + if (cross(a, b, c) < 0) + swap(b, c); + Point3D a3(a), b3(b), c3(c), p3(p); + // printf("%lf %lf %lf\n", a3.x, a3.y, a3.z); + // printf("%lf %lf %lf\n", b3.x, b3.y, b3.z); + // printf("%lf %lf %lf\n", c3.x, c3.y, c3.z); + // printf("%lf %lf %lf\n", p3.x, p3.y, p3.z); + b3 = b3 - a3, c3 = c3 - a3, p3 = p3 - a3; + Point3D f = cross(b3, c3);// normal vector + return cmpZero(p3.dot(f));// check same direction, in: < 0, on: = 0, out: > 0 +} +int intersection(Point a, Point b, Point c, Point d) +{// seg(a, b) and seg(c, d) + return cmpZero(cross(a, c, b)) * cmpZero(cross(a, b, d)) > 0 && cmpZero(cross(c, a, d)) * cmpZero(cross(c, d, b)) > 0; +} +class Delaunay +{ +public: + list head[MAXV];// graph + Point p[MAXV]; + int n, rename[MAXV]; + void init(int n, Point p[]) + { + for (int i = 0; i < n; i++) + head[i].clear(); + for (int i = 0; i < n; i++) + this->p[i] = p[i]; + sort(this->p, this->p + n); + for (int i = 0; i < n; i++) + rename[p[i].id] = i; + this->n = n; + divide(0, n - 1); + } + void addEdge(int u, int v) + { + head[u].push_front(Edge(v)); + head[v].push_front(Edge(u)); + head[u].begin()->twin = head[v].begin(); + head[v].begin()->twin = head[u].begin(); + } + void divide(int l, int r) + { + if (r - l <= 2) + {// #point <= 3 + for (int i = l; i <= r; i++) + for (int j = i + 1; j <= r; j++) + addEdge(i, j); + return; + } + int mid = (l + r) / 2; + divide(l, mid); + divide(mid + 1, r); + + list::iterator it; + int nowl = l, nowr = r; + + // printf("divide %d %d\n", l, r); + for (int update = 1; update;) + {// find left and right convex, lower common tangent + update = 0; + Point ptL = p[nowl], ptR = p[nowr]; + for (it = head[nowl].begin(); it != head[nowl].end(); it++) + { + Point t = p[it->id]; + double v = cross(ptR, ptL, t); + if (cmpZero(v) > 0 || (cmpZero(v) == 0 && ptR.dist2(t) < ptR.dist2(ptL))) + { + nowl = it->id, update = 1; + break; + } + } + if (update) + continue; + for (it = head[nowr].begin(); it != head[nowr].end(); it++) + { + Point t = p[it->id]; + double v = cross(ptL, ptR, t); + if (cmpZero(v) < 0 || (cmpZero(v) == 0 && ptL.dist2(t) < ptL.dist2(ptR))) + { + nowr = it->id, update = 1; + break; + } + } + } + + addEdge(nowl, nowr);// add tangent + // printf("add base %d %d\n", nowl, nowr); + for (int update = 1; true;) + { + update = 0; + Point ptL = p[nowl], ptR = p[nowr]; + int ch = -1, side = 0; + for (it = head[nowl].begin(); it != head[nowl].end(); it++) + { + // ptL.print(), ptR.print(), p[it->id].print(); + if (cmpZero(cross(ptL, ptR, p[it->id])) > 0 && (ch == -1 || inCircle(ptL, ptR, p[ch], p[it->id]) < 0)) + ch = it->id, side = -1; + // printf("test L %d %d %d\n", nowl, it->id, inCircle(ptL, ptR, p[ch], p[it->id])); + } + for (it = head[nowr].begin(); it != head[nowr].end(); it++) + { + if (cmpZero(cross(ptR, p[it->id], ptL)) > 0 && (ch == -1 || inCircle(ptL, ptR, p[ch], p[it->id]) < 0)) + ch = it->id, side = 1; + // printf("test R %d %d %d\n", nowr, it->id, inCircle(ptL, ptR, p[ch], p[it->id])); + } + if (ch == -1) + break;// upper common tangent + // printf("choose %d %d\n", ch, side); + if (side == -1) + { + for (it = head[nowl].begin(); it != head[nowl].end();) + { + if (intersection(ptL, p[it->id], ptR, p[ch])) + { + head[it->id].erase(it->twin); + head[nowl].erase(it++); + } + else + it++; + } + nowl = ch; + addEdge(nowl, nowr); + } + else + { + for (it = head[nowr].begin(); it != head[nowr].end();) + { + if (intersection(ptR, p[it->id], ptL, p[ch])) + { + head[it->id].erase(it->twin); + head[nowr].erase(it++); + } + else + it++; + } + nowr = ch; + addEdge(nowl, nowr); + } + } + } + + vector> getEdge() + { + vector> ret; + list::iterator it; + for (int i = 0; i < n; i++) + { + for (it = head[i].begin(); it != head[i].end(); it++) + { + if (it->id < i) + continue; + // printf("DG %d %d\n", i, it->id); + ret.push_back(make_pair(p[i].id, p[it->id].id)); + } + } + return ret; + } +} tool; +#define INF (1LL << 60) +Point p[MAXV]; +long long ret[MAXV]; +int main() +{ + int testcase, n; + long long x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lld %lld", &x, &y); + p[i] = Point(x, y, i); + } + tool.init(n, p); + vector> DG = tool.getEdge(); + for (int i = 0; i < n; i++) + { + ret[i] = INF; + } + for (int i = 0; i < DG.size(); i++) + { + x = DG[i].first, y = DG[i].second; + long long v = (long long)(p[x].x - p[y].x) * (long long)(p[x].x - p[y].x) + + (long long)(p[x].y - p[y].y) * (long long)(p[x].y - p[y].y); + ret[x] = min(ret[x], v); + ret[y] = min(ret[y], v); + } + for (int i = 0; i < n; i++) + { + printf("%lld\n", ret[i]); + } + } + return 0; +} +/* + 2 + 10 + 17 41 + 0 34 + 24 19 + 8 28 + 14 12 + 45 5 + 27 31 + 41 11 + 42 45 + 36 27 + 15 + 0 0 + 1 2 + 2 3 + 3 2 + 4 0 + 8 4 + 7 4 + 6 3 + 6 1 + 8 0 + 11 0 + 12 2 + 13 1 + 14 2 + 15 0 + */ diff --git a/1684.cpp b/1684.cpp new file mode 100644 index 0000000..98c5caa --- /dev/null +++ b/1684.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAXN 128 +#define MAXCYCLE 2520 +#define INF (1LL << 60) + +struct Edge +{ + int to, c, w; + Edge(int x = 0, int y = 0, int z = 0) + : to(x), c(y), w(z) + { + } +}; +vector g[MAXN], invg[MAXN]; + +long long distFromSt[MAXN][MAXCYCLE], kpath[MAXN][MAXCYCLE]; + +struct Info +{ + int x, t; + long long d; + Info(int a = 0, int b = 0, long long c = 0) + : x(a), t(b), d(c) + { + } + bool operator<(const Info &a) const + { + return d > a.d; + } +}; + +void kthSSSP(int st, int ed, int K, int N, int T) +{ + int kpathCnt[MAXN]; + priority_queue > pQ; + Info u; + int cycle = 1; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < g[i].size(); j++) + { + int lcm = cycle * g[i][j].c / __gcd(cycle, g[i][j].c); + cycle = lcm; + } + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < cycle; j++) + { + distFromSt[i][j] = INF, kpath[i][j] = 0; + } + kpathCnt[i] = 0; + } + pQ.push(Info(st, 0, 0)); + while (!pQ.empty()) + { + u = pQ.top(), pQ.pop(); + if (kpath[u.x][u.t] > K) + { + continue; + } + kpath[u.x][u.t]++, distFromSt[u.x][u.t] = u.d; + kpathCnt[u.x]++; + if (kpathCnt[u.x] == K && u.x == ed) + { + printf("%lld\n", distFromSt[u.x][u.t]); + return; + } + for (int i = 0; i < g[u.x].size(); i++) + { + long long av = (u.d / g[u.x][i].c + (u.d % g[u.x][i].c != 0)), w; + int to = g[u.x][i].to; + w = g[u.x][i].w; + av = av * g[u.x][i].c; + for (; av - u.d <= T; av += g[u.x][i].c) + { + pQ.push(Info(to, (av + w) % cycle, av + w)); + } + } + } + printf("-1\n"); +} + +int main() +{ + int N, M, K, T, U, V, C, W, t = 0; + while (scanf("%d%d%d%d", &N, &M, &K, &T) == 4 && N) + { + for (int i = 0; i < N; i++) + { + g[i].clear(); + invg[i].clear(); + } + for (int i = 0; i < M; i++) + { + scanf("%d%d%d%d", &U, &V, &C, &W); + g[U].push_back(Edge(V, C, W)); + invg[V].push_back(Edge(U, C, W)); + } + printf("Case %d: ", ++t); + kthSSSP(0, N - 1, K + 1, N, T); + } + return 0; +} diff --git a/1687.cpp b/1687.cpp new file mode 100644 index 0000000..d277105 --- /dev/null +++ b/1687.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define MAXN 1024 +#define MAXM 32767 +#define INF 1LL << 60 + +struct Node +{ + Node *l, *r; + vector > v; + int kind; + Node(Node *a = NULL, Node *b = NULL, int k = 0) + { + l = a; + r = b; + kind = k; + v.clear(); + } +} nodes[MAXM]; + +bool cmp(pair a, pair b) +{ + if (a.second != b.second) + { + return a.second < b.second; + } + return a.first < b.first; +} + +void dfs(Node *u) +{ + if (u->l == NULL && u->r == NULL) + { + return; + } + dfs(u->l); + dfs(u->r); + Node *lson = u->l, *rson = u->r; + long long w, h, rmin = INF, lmin = INF; + if (u->kind) + { + sort(lson->v.begin(), lson->v.end()); + sort(rson->v.begin(), rson->v.end()); + for (int i = 0, j = 0; i < lson->v.size(); i++) + { + while (j < rson->v.size() && rson->v[j].first <= lson->v[i].first) + { + rmin = min(rmin, rson->v[j].second); + j++; + } + if (rmin == INF) + { + continue; + } + w = lson->v[i].first; + h = lson->v[i].second + rmin; + u->v.push_back(make_pair(w, h)); + } + for (int i = 0, j = 0; i < rson->v.size(); i++) + { + while (j < lson->v.size() && lson->v[j].first <= rson->v[i].first) + { + lmin = min(lmin, lson->v[j].second); + j++; + } + if (lmin == INF) + { + continue; + } + w = rson->v[i].first; + h = rson->v[i].second + lmin; + u->v.push_back(make_pair(w, h)); + } + } + else + { + sort(lson->v.begin(), lson->v.end(), cmp); + sort(rson->v.begin(), rson->v.end(), cmp); + for (int i = 0, j = 0; i < lson->v.size(); i++) + { + while (j < rson->v.size() && rson->v[j].second <= lson->v[i].second) + { + rmin = min(rmin, rson->v[j].first); + j++; + } + if (rmin == INF) + { + continue; + } + w = lson->v[i].first + rmin; + h = lson->v[i].second; + u->v.push_back(make_pair(w, h)); + } + for (int i = 0, j = 0; i < rson->v.size(); i++) + { + while (j < lson->v.size() && lson->v[j].second <= rson->v[i].second) + { + lmin = min(lmin, lson->v[j].first); + j++; + } + if (lmin == INF) + { + continue; + } + w = rson->v[i].first + lmin; + h = rson->v[i].second; + u->v.push_back(make_pair(w, h)); + } + } + sort(u->v.begin(), u->v.end()); + u->v.resize(unique(u->v.begin(), u->v.end()) - u->v.begin()); +} + +int main() +{ + char s[MAXN]; + int T, n, H[MAXN], W[MAXN], x; + scanf("%d", &T); + while (T--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%d%d", &H[i], &W[i]); + } + stack stk; + Node *p, *a, *b, *root; + int size = 0; + for (int i = 1; i < 2 * n; i++) + { + scanf("%s", s); + if (s[0] == 'V' || s[0] == 'H') + { + p = &nodes[size++]; + b = stk.top(), stk.pop(); + a = stk.top(), stk.pop(); + *p = Node(a, b, s[0] == 'V'); + stk.push(p); + } + else + { + sscanf(s, "%d", &x); + p = &nodes[size++]; + *p = Node(NULL, NULL); + p->v.push_back(make_pair(H[x], W[x])); + p->v.push_back(make_pair(W[x], H[x])); + stk.push(p); + } + } + root = stk.top(); + dfs(root); + long long ret = INF; + for (int i = 0; i < root->v.size(); i++) + { + ret = min(ret, (long long)root->v[i].first * root->v[i].second); + } + printf("%lld\n", ret); + } + return 0; +} diff --git a/169.cpp b/169.cpp new file mode 100644 index 0000000..51c0419 --- /dev/null +++ b/169.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +struct Word +{ + int a, b, cnt, ok; + bool operator<(const Word &v) const + { + if (ok != v.ok) + { + return ok == 1; + } + if (b < v.a) + { + return true; + } + if (v.b < a) + { + return false; + } + return b < v.b; + } +}; + +vector words; +map mwords; + +char text[1024]; +int N, here[1024], pass; + +void doit(int a, int b) +{ + int m = b - a + 1; + if (m < 2 || m > 250) + { + return; + } + static char buf[256]; + memcpy(buf, text + a, m); + buf[m] = 0; + sort(buf, buf + m); + string s = buf; + if (pass == 0) + { + // pass 1: discover words and count their occurences + if (mwords.count(s) == 0) + { + Word w = {a, b, 1, 0}; + mwords[s] = words.size(); + words.push_back(w); + } + else + { + words[mwords[s]].cnt++; + } + } + else + { + // pass 2: filter true words + int w = mwords[s]; + if (words[w].cnt < 2) + { + return; + } + for (int i = a; i <= b; i++) + { + if (here[i] >= 0) + { + words[w].ok = 1; + words[here[i]].ok = 1; + } + here[i] = w; + } + } +} + +int readText() +{ + char line[1024]; + for (N = 0;;) + { + if (gets(line) == NULL) + { + return 0; + } + int k = strlen(line); + while (k > 0 && line[k - 1] == '\r') + { + line[--k] = 0; + } + if (strcmp(line, "#") == 0) + { + return 0; + } + for (int i = 0; i < k; i++) + { + if (islower(line[i])) + { + assert(N <= 999); + text[N++] = line[i]; + } + } + if (k == 0 || line[k - 1] != '-') + { + break; + } + } + text[N] = 0; + return 1; +} + +int main() +{ + while (readText()) + { + words.clear(); + mwords.clear(); + memset(here, 0xff, sizeof(here)); + for (pass = 0; pass < 2; pass++) + { + int cn[128]; + // left spacer is a joker + memset(cn, 0, sizeof(cn)); + for (int n = 1; n <= 250 && n < N; n++) + { + cn[text[n - 1]] = 1; + if (cn[text[n]] == 0) + { + doit(0, n - 1); + } + } + // left and right spacers are letters + for (int a = 0; a < N; a++) + { + int b; + for (b = a + 1; b < N && b - a < 260 && text[b] != text[a]; b++) + ; + if (b >= N || text[b] != text[a]) + { + continue; + } + doit(a + 1, b - 1); + } + // right spacer is a joker + memset(cn, 0, sizeof(cn)); + for (int n = 1; n <= 250 && n < N; n++) + { + cn[text[N - n]] = 1; + if (cn[text[N - n - 1]] == 0) + { + doit(N - n, N - 1); + } + } + } + sort(words.begin(), words.end()); + for (int i = 0; i < (int)words.size(); i++) + if (words[i].ok) + { + printf("%.*s\n", words[i].b - words[i].a + 1, text + words[i].a); + } + printf("*\n"); + } +} diff --git a/170.cpp b/170.cpp new file mode 100644 index 0000000..77597f9 --- /dev/null +++ b/170.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +struct Card +{ + char suit; + int rank; +}; + +struct ClockPatience +{ + char buf[100]; + int cntExposed, cntPile[14]; + Card card[52], pile[14][5]; + + int readData(); + void dealtOut(); + void play(); + void outPut(); + void printCard(); +} cp; + +int ClockPatience::readData() +{ + int cntCard, loop; + cntCard = 0; + for (loop = 0; loop < 4; loop++) + { + cin.getline(buf, 100); + if (!strcmp(buf, "#")) + { + return 0; + } + int index; + for (index = 0; buf[index]; index++) + { + if (isalpha(buf[index])) + { + switch (buf[index]) + { + case 'T': + card[cntCard].rank = 10; + break; + case 'J': + card[cntCard].rank = 11; + break; + case 'Q': + card[cntCard].rank = 12; + break; + case 'K': + card[cntCard].rank = 13; + break; + case 'A': + card[cntCard].rank = 1; + break; + } + } + if (isdigit(buf[index])) + { + card[cntCard].rank = buf[index] - '0'; + } + index++; + card[cntCard].suit = buf[index]; + index++; + cntCard++; + } + } + return 1; +} + +void ClockPatience::dealtOut() +{ + int level, loopCard, loop; + loopCard = 51; + for (level = 0; level < 4; level++) + { + int loopPile; + for (loopPile = 1; loopPile <= 13; loopPile++) + { + pile[loopPile][level] = card[loopCard--]; + } + } + for (loop = 1; loop <= 13; loop++) + { + cntPile[loop] = 3; + } + cntExposed = 0; +} + +void ClockPatience::play() +{ + Card now; + now = pile[13][3]; + cntPile[13]--; + cntExposed = 1; + while (true) + { + if (cntPile[now.rank] < 0) + { + break; + } + // cout< 9) + { + cout << cntExposed; + } + else + { + cout << "0" << cntExposed; + } + cout << ","; + if (now.rank > 9 || now.rank == 1) + { + switch (now.rank) + { + case 1: + cout << 'A'; + break; + case 10: + cout << 'T'; + break; + case 11: + cout << 'J'; + break; + case 12: + cout << 'Q'; + break; + case 13: + cout << 'K'; + break; + } + } + else + { + cout << now.rank; + } + cout << now.suit << endl; +} + +void ClockPatience::printCard() +{ + int loop; + for (loop = 1; loop <= 13; loop++) + { + cout << "Pile " << loop << " : "; + for (int j = 0; j < 4; j++) + { + cout << pile[loop][j].rank << pile[loop][j].suit << " "; + } + cout << endl; + } +} + +int main() +{ + while (cp.readData()) + { + cp.dealtOut(); + cp.play(); + } + return 0; +} diff --git a/1709.cpp b/1709.cpp new file mode 100644 index 0000000..0225777 --- /dev/null +++ b/1709.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int p,a,b,c,d,n; + while (scanf("%d%d%d%d%d",&p,&a,&b,&c,&d) == 5) + { + scanf("%d", &n); + double mx = -1e+30, ret = 0; + double tsa = sin(a), tca = cos(a); + double tsb = sin(c), tcb = cos(c); + double sin1 = sin(a + b), cos1 = cos(a + b); + double cos2 = cos(c + d), sin2 = sin(c + d); + for (int i = 1; i <= n; i++) + { + double f = p * (sin1 + cos2 + 2); + ret = max(ret, mx - f); + mx = max(mx, f); + + double t1, t2; + t1 = sin1 * tca + cos1 * tsa; + t2 = cos1 * tca - sin1 * tsa; + sin1 = t1, cos1 = t2; + + t1 = sin2 * tcb + cos2 * tsb; + t2 = cos2 * tcb - sin2 * tsb; + sin2 = t1, cos2 = t2; + } + printf("%.8lf\n", ret); + } + return 0; +} diff --git a/171.cpp b/171.cpp new file mode 100644 index 0000000..7fa6008 --- /dev/null +++ b/171.cpp @@ -0,0 +1,233 @@ +#include + +using namespace std; + +typedef vector VS; +typedef vector VVS; + +VS tokenize(const string &s) +{ + VS ret; + for (int i = 0, j;;) + { + while (i < (int)s.size() && isspace(s[i])) + { + i++; + } + if (i >= (int)s.size()) + { + return ret; + } + if (s[i] == '\"') + { + for (j = i++; i < (int)s.size() && s[i] != '\"'; i++) + ; + if (i < (int)s.size()) + { + i++; + } + } + else + { + for (j = i; i < (int)s.size() && !isspace(s[i]); i++) + ; + } + ret.push_back(s.substr(j, i - j)); + } +} + +struct Grammar +{ + VS tok; + int pos; + + Grammar(VS v) : tok(v), pos(0) {} + bool end() + { + return pos >= (int)tok.size(); + } + bool is(const string &s) + { + return !end() && tok[pos] == s; + } + bool match(const string &s) + { + if (is(s)) + { + pos++; + return true; + } + return false; + } + + int ins() + { + if (is("GO") || is("KEEP")) + { + for (;;) + { + if (!dir()) + { + return 0; + } + if (match("AND")) + { + if (match("THEN")) + { + continue; + } + return time() && end(); + } + return end(); + } + } + return time() && end(); + } + + int dir() + { + if (match("GO")) + { + match("FIRST") || match("SECOND") || match("THIRD"); + } + else if (!match("KEEP")) + { + return 0; + } + if (!match("LEFT") && !match("RIGHT")) + { + return 0; + } + if (match("AT") && !match("quoted")) + { + return 0; + } + return 1; + } + + int time() + { + if (match("RECORD")) + { + return match("TIME"); + } + if (match("CHANGE")) + { + if (!match("AVERAGE") || !match("SPEED")) + { + return 0; + } + } + else if (!match("CAS")) + { + return 0; + } + return match("TO") && match("number") && match("KMH"); + } + /* + instruction = navigational | time-keeping | navigational AND time-keeping + navigational = directional | navigational AND THEN directional + directional = how direction | how direction AT sign + how = GO | GO when | KEEP + direction = RIGHT | LEFT + when = FIRST | SECOND | THIRD + time = RECORD TIME | cas TO nnn KMH + cas = CHANGE AVERAGE SPEED | CAS + */ +}; + +int is_number(const string &s) +{ + for (int i = 0; i < (int)s.size(); i++) + if (!isdigit(s[i])) + { + return 0; + } + return 1; +} + +int check(const VS &v, char *line) +{ + VS t; + if (strstr(line, "\"\"") != NULL) + { + return 1; + } + if (strstr(line, " .") != NULL) + { + return 1; + } + for (int i = 0; i < (int)v.size(); i++) + { + const string &s = v[i]; + for (int j = 0; j < (int)s.size(); j++) + { + int c = s[j]; + if (c == '\"' || c == '.' || c == ' ') + { + continue; + } + if (isupper(c) || isdigit(c)) + { + continue; + } + return 1; + } + if (s[0] == '\"') + { + if (s.size() < 3 || s[s.size() - 1] != '\"') + { + return 1; + } + if (s[1] == ' ') + { + return 1; + } + if (s[s.size() - 2] == ' ') + { + return 1; + } + for (int i = 1; i + 1 < (int)s.size(); i++) + if (!isupper(s[i]) && s[i] != ' ' && s[i] != '.') + { + return 1; + } + t.push_back("quoted"); + } + else if (is_number(s)) + { + t.push_back("number"); + } + else if (s == "quoted" || s == "number") + { + return 1; + } + else + { + t.push_back(s); + } + } + return Grammar(t).ins() == 0; +} + +int main() +{ + char line[65536]; + for (int cs = 1; gets(line) && strcmp(line, "#") != 0; cs++) + { + VS v = tokenize(line); + if (check(v, line)) + { + printf("%3d. Trap!\n", cs); + } + else + { + printf("%3d.", cs); + for (int i = 0; i < v.size(); i++) + { + printf(" %s", v[i].c_str()); + } + printf("\n"); + } + } +} diff --git a/1711.cpp b/1711.cpp new file mode 100644 index 0000000..886a20f --- /dev/null +++ b/1711.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +const int MAXN = 400; +const int MAXM = 1048576; + +struct Node +{ + int x, y, cap; + long long cost;// x->y, v + int next; +} edge[MAXM]; + +const long long INF = 1LL << 30; + +class MinCost +{ +public: + int e, head[MAXN], pre[MAXN], record[MAXN], inq[MAXN]; + long long dis[MAXN]; + int n; + void addedge(int x, int y, int cap, long long cost) + { + assert(e < MAXM); + // printf("Edge %d %d cost %lld cap %d\n", x, y, cost, cap); + edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost; + edge[e].next = head[x], head[x] = e++; + edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost; + edge[e].next = head[y], head[y] = e++; + } + pair mincost(int s, int t) + { + long long mncost = 0, oo = INF; + int flow, totflow = 0; + int i, x, y; + while (1) + { + for (int i = 0; i < n; i++) + dis[i] = INF; + dis[s] = 0; + deque Q; + Q.push_front(s); + while (!Q.empty()) + { + x = Q.front(), Q.pop_front(); + inq[x] = 0; + for (i = head[x]; i != -1; i = edge[i].next) + { + y = edge[i].y; + if (edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) + { + dis[y] = dis[x] + edge[i].cost; + pre[y] = x, record[y] = i; + if (inq[y] == 0) + { + inq[y] = 1; + if (Q.size() && dis[Q.front()] > dis[y]) + Q.push_front(y); + else + Q.push_back(y); + } + } + } + } + if (dis[t] == oo) + break; + // this problem doesn't need full flow + if (dis[t] >= 0) + break; + flow = 0x3f3f3f3f; + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + flow = min(flow, edge[ri].cap); + } + for (x = t; x != s; x = pre[x]) + { + int ri = record[x]; + edge[ri].cap -= flow; + edge[ri ^ 1].cap += flow; + edge[ri ^ 1].cost = -edge[ri].cost; + } + totflow += flow; + mncost += dis[t] * flow; + } + return make_pair(mncost, totflow); + } + void init(int n) + { + this->n = n; + e = 0; + for (int i = 0; i <= n; i++) + head[i] = -1; + } +} g; + +int main() +{ + int N, K, c[105][105]; + while (scanf("%d%d", &N,&K) == 2) + { + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N + 1; j++) + { + scanf("%d", &c[i][j]); + } + } + + int source = 2 * (N + 1), sink = 2 * (N + 1) + 1; + g.init(2 * (N + 1) + 2); + g.addedge(source, 0, K, 0); + for (int i = 0; i < N + 1; i++) + { + g.addedge(2 * i + 1, sink, 1, 0); + if (i) + g.addedge(2 * i, 2 * i + 1, 1, -INF); + } + for (int i = 0; i < N + 1; i++) + { + for (int j = i + 1; j < N + 1; j++) + { + if (i == 0) + g.addedge(2 * i, 2 * j, 1, c[i][j]); + else + g.addedge(2 * i + 1, 2 * j, 1, c[i][j]); + } + } + + pair ret = g.mincost(source, sink); + printf("%lld\n", (ret.first % INF + INF) % INF); + } + return 0; +} diff --git a/1712.cpp b/1712.cpp new file mode 100644 index 0000000..6fb88f7 --- /dev/null +++ b/1712.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +const int MAXN = 10005; +const double pi = acos(-1); + +struct Hole +{ + double x, y, z, r; + void unit() + { + x /= 1000.0, y /= 1000.0, z /= 1000.0, r /= 1000.0; + } + double V() + { + return r * r * r * pi * 4.0 / 3; + } + bool operator<(const Hole &a) const + { + return z < a.z; + } + bool intersect(double tl, double tr) + { + return max(tl, z - r) < min(tr, z + r); + } + double f(double x) + { + return (r * r * x - x * x * x / 3) * pi; + } + double partial(double tl, double tr) + { + tl = max(tl, z - r), tr = min(tr, z + r); + double a = tl - z, b = tr - z; + if (a >= 0) + return f(b) - f(a); + if (b <= 0) + return f(-a) - f(-b); + return f(b) + f(-a); + } +} H[MAXN]; + +int main() +{ + int N, S; + while (scanf("%d%d",&N,&S) == 2) + { + double V = 100.0 * 100.0 * 100.0; + for (int i = 0; i < N; i++) + { + scanf("%lf%lf%lf%lf",&H[i].r,&H[i].x,&H[i].y,&H[i].z); + H[i].unit(); + V -= H[i].V(); + } + V /= S; + double lz = 0; + for (int i = 0; i < S; i++) + { + double l = lz, r = 100, mid; + for (int it = 0; it < 100 && fabs(l - r) > 1e-9; it++) + { + mid = (l + r) / 2; + double vs = (mid - lz) * 100.0 * 100.0; + for (int j = 0; j < N && vs > V; j++) + { + if (H[j].intersect(lz, mid)) + { + vs -= H[j].partial(lz, mid); + } + } + if (vs < V) + { + l = mid; + } + else + { + r = mid; + } + } + printf("%.9lf\n", mid - lz); + lz = mid; + } + } + return 0; +} diff --git a/1714.cpp b/1714.cpp new file mode 100644 index 0000000..53c7bb7 --- /dev/null +++ b/1714.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +char g[64][64], s[32767]; +int to_where[64][64][4][2], R, C; +const int dx[4] = {0, 0, 1, -1}; +const int dy[4] = {1, -1, 0, 0}; + +inline int isValid(int x, int y) +{ + return x >= 0 && y >= 0 && x < R && y < C; +} + +struct Node +{ + int x, y, d; + Node(int x, int y, int d) : x(x), y(y), d(d) + { + } + bool operator<(const Node &a) const + { + if (d != a.d) + return d < a.d; + if (x != a.x) + return x < a.x; + return y < a.y; + } +}; + +int main() +{ + while (scanf("%d%d",&R,&C) == 2) + { + for (int i=0; i pQ; + int dist[64][64] = {}, inq[64][64] = {}; + memset(dist, 0x3f, sizeof(dist)); + dist[0][0] = 0; + pQ.push(Node(0, 0, 0)); + for (int i = 0; i < n; i++) + { + Node u(0, 0, 0); + while (!pQ.empty()) + { + u = pQ.front(), pQ.pop(); + inq[u.x][u.y] = 0; + int d = dist[u.x][u.y]; + for (int k = 0; k < 4; k++) + { + if (to_where[u.x][u.y][k][0] == -1) + continue; + int x = to_where[u.x][u.y][k][0]; + int y = to_where[u.x][u.y][k][1]; + if (dist[x][y] > d + 1) + { + dist[x][y] = d + 1; + if (inq[x][y] == 0) + { + pQ.push(Node(x, y, d + 1)); + inq[x][y] = 1; + } + } + } + } + + for (int j = 0; j < R; j++) + { + for (int k = 0; k < C; k++) + { + if (g[j][k] == s[i]) + { + dist[j][k] = dist[j][k] + 1; + pQ.push(Node(j, k, dist[j][k])), inq[j][k] = 1; + } + else + { + dist[j][k] = 0x3f3f3f3f; + } + } + } + } + + int ret = 0x3f3f3f3f; + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + if (g[i][j] == '*') + ret = min(ret, dist[i][j]); + } + } + printf("%d\n", ret); + } + return 0; +} diff --git a/1718.cpp b/1718.cpp new file mode 100644 index 0000000..eea62ee --- /dev/null +++ b/1718.cpp @@ -0,0 +1,189 @@ +#include + +using namespace std; + +struct Complex +{ + double x, y; + Complex(double x = 0, double y = 0) : x(x), y(y) {} + Complex operator-(const Complex &v) const + { + return Complex(x - v.x, y - v.y); + } + Complex operator+(const Complex &v) const + { + return Complex(x + v.x, y + v.y); + } + Complex operator*(const Complex &v) const + { + return Complex(x * v.x - y * v.y, x * v.y + y * v.x); + } + Complex operator/(const int &v) const + { + return Complex(x / v, y / v); + } + double real() + { + return x; + } +}; + +template +class TOOL_FFT +{ +public: + typedef unsigned int UINT32; +#define MAXN (1048576 << 1) + Complex p[2][MAXN]; + int pre_n; + T PI; + TOOL_FFT() + { + pre_n = 0; + PI = acos(-1); + } + int NumberOfBitsNeeded(int PowerOfTwo) + { + for (int i = 0;; ++i) + { + if (PowerOfTwo & (1 << i)) + { + return i; + } + } + } + inline UINT32 FastReverseBits(UINT32 a, int NumBits) + { + a = ((a & 0x55555555U) << 1) | ((a & 0xAAAAAAAAU) >> 1); + a = ((a & 0x33333333U) << 2) | ((a & 0xCCCCCCCCU) >> 2); + a = ((a & 0x0F0F0F0FU) << 4) | ((a & 0xF0F0F0F0U) >> 4); + a = ((a & 0x00FF00FFU) << 8) | ((a & 0xFF00FF00U) >> 8); + a = ((a & 0x0000FFFFU) << 16) | ((a & 0xFFFF0000U) >> 16); + return a >> (32 - NumBits); + } + + void FFT(bool InverseTransform, vector &In, vector &Out) + { + // simultaneous data copy and bit-reversal ordering into outputs + int NumSamples = In.size(); + int NumBits = NumberOfBitsNeeded(NumSamples); + for (int i = 0; i < NumSamples; ++i) + { + Out[FastReverseBits(i, NumBits)] = In[i]; + } + // the FFT process + for (register int i = 1; i <= NumBits; i++) + { + int BlockSize = 1 << i, BlockEnd = BlockSize >> 1, BlockCnt = NumSamples / BlockSize; + for (register int j = 0; j < NumSamples; j += BlockSize) + { + Complex *t = p[InverseTransform]; + for (register int k = 0; k < BlockEnd; k++, t += BlockCnt) + { + Complex a = (*t) * Out[k + j + BlockEnd]; + Out[k + j + BlockEnd] = Out[k + j] - a; + Out[k + j] = Out[k + j] + a; + } + } + } + // normalize if inverse transform + if (InverseTransform) + { + for (int i = 0; i < NumSamples; ++i) + { + Out[i] = Out[i] / NumSamples; + } + } + } + void prework(int n) + { + if (pre_n == n) + return; + pre_n = n; + p[0][0] = Complex(1, 0); + p[1][0] = Complex(1, 0); + for (register int i = 1; i < n; i++) + { + p[0][i] = Complex(cos(2 * i * PI / n), sin(2 * i * PI / n)); + p[1][i] = Complex(cos(2 * i * PI / n), -sin(2 * i * PI / n)); + } + } + vector convolution(Complex *a, Complex *b, int n) + { + prework(n); + vector s(a, a + n), d1(n), d2(n), y(n); + vector ret(n); + FFT(false, s, d1); + s[0] = b[0]; + for (int i = 1, j = n - 1; i < n; ++i, --j) + s[i] = b[j]; + FFT(false, s, d2); + for (int i = 0; i < n; ++i) + { + y[i] = d1[i] * d2[i]; + } + FFT(true, y, s); + for (int i = 0; i < n; ++i) + { + ret[i] = s[i].real(); + } + return ret; + } +}; +TOOL_FFT tool; + +Complex a[MAXN], b[MAXN]; +vector c; +long long ret[1048576]; +long long pr[1048576] = {}; +int main() +{ + int m; + const int N = 500000; + for (m = 1; m < (N << 1); m <<= 1) + ; + /* + (a + c)(b + d) - 2 * 0.5 * (a b) - 2 * 0.5 * (c * d) + = ad + bc = area + a, b, c, d > 0, + ways of area = #(a, b, c, d) sat. ad + bc = area + */ + for (int i = 1; i <= N; i++) + { + for (int j = i; j <= N; j += i) + pr[j]++; + } + + memset(a, 0, sizeof(a[0]) * m); + memset(b, 0, sizeof(b[0]) * m); + + for (int i = 1; i <= N; i++) + { + a[i] = Complex(pr[i], 0); + b[m - i] = Complex(pr[i], 0); + } + + c = tool.convolution(a, b, m); + + for (int i = 1; i <= N; i++) + ret[i] = (long long)(c[i] + 0.5); + + int testcase, L, R; + while (scanf("%d", &testcase) == 1) + { + while (testcase--) + { + scanf("%d %d", &L, &R); + assert(L <= R); + assert(L >= 1 && R <= 500000); + int mxA = L; + for (int i = L; i <= R; i++) + { + if (ret[i] > ret[mxA]) + mxA = i; + } + printf("%d %lld\n", mxA, ret[mxA]); + } + } + return 0; +} diff --git a/172.cpp b/172.cpp new file mode 100644 index 0000000..4b0c848 --- /dev/null +++ b/172.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +char line[1024], *tok; +int var[32], prev_[32], size; +struct Node +{ + int left, right, type, val, lval; +} tree[1024]; + +void ws() +{ + while (*tok && isspace(*tok)) + { + tok++; + } +} + +int node(int t, int l = 0, int r = 0) +{ + size++; + tree[size].type = t; + tree[size].left = l; + tree[size].right = r; + return size; +} + +int expr(); + +int fact() +{ + ws(); + if (isalpha(*tok)) + { + return node(toupper(*tok++)); + } + if (*tok == '_') + { + tok++; + return node('-', node('n', 0), fact()); + } + if (*tok == '(') + { + tok++; + int x = expr(); + ws(); + if (*tok == ')') + { + tok++; + } + return x; + } + int p = 0, x; + sscanf(tok, "%d %n", &x, &p); + tok += p; + return node('n', x); +} + +int expr() +{ + int l = fact(); + ws(); + int op = *tok; + if (op == 0 || strchr("+-*/=", op) == NULL) + { + return l; + } + else + { + tok++; + return node(op, l, expr()); + } +} + +int eval(int r) +{ + Node &n = tree[r]; + n.lval = -1; + if (isupper(n.type)) + { + n.lval = n.type - 'A'; + n.val = var[n.lval]; + } + else if (n.type == 'n') + { + n.val = n.left; + } + else if (n.type == '+') + { + n.val = eval(n.right); + n.val += eval(n.left); + } + else if (n.type == '-') + { + n.val = -eval(n.right); + n.val += eval(n.left); + } + else if (n.type == '*') + { + n.val = eval(n.right); + n.val *= eval(n.left); + } + else if (n.type == '/') + { + n.val = eval(n.right); + assert(n.val != 0); + n.val = eval(n.left) / n.val; + } + else if (n.type == '=') + { + n.val = eval(n.right); + eval(n.left); + n.lval = tree[n.left].lval; + assert(n.lval >= 0); + var[n.lval] = n.val; + } + return n.val; +} + +int main() +{ + memset(var, 0, sizeof(var)); + while (gets(line) && strchr(line, '#') == NULL) + { + memcpy(prev_, var, sizeof(var)); + tok = line; + size = 0; + eval(expr()); + int k = 0; + for (int i = 0; i < 26; i++) + { + if (prev_[i] != var[i]) + { + printf("%s%c = %d", k++ ? ", " : "", i + 'A', var[i]); + } + } + printf(k ? "\n" : "No Change\n"); + } +} diff --git a/1726.cpp b/1726.cpp new file mode 100644 index 0000000..610f629 --- /dev/null +++ b/1726.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +inline int readchar() +{ + const int N = 1048576; + static char buf[N]; + static char *p = buf, *end = buf; + if (p == end) + { + if ((end = buf + fread(buf, 1, N, stdin)) == buf) + { + return EOF; + } + p = buf; + } + return *p++; +} + +inline int readint(int *x) +{ + static char c, neg; + while ((c = readchar()) < '-') + { + if (c == EOF) + { + return 0; + } + } + neg = (c == '-') ? -1 : 1; + *x = (neg == 1) ? c - '0' : 0; + while ((c = readchar()) >= '0') + { + *x = (*x << 3) + (*x << 1) + c - '0'; + } + *x *= neg; + return 1; +} + +int main() +{ + int t,n; + readint(&t); + while (t--) + { + readint(&n); + int Ql[16]={}, Qu[16]={}; + for (int i=0; i + +using namespace std; + +string day_str[7] = { + "SUN","MON","TUE","WED","THU","FRI","SAT"}; +string mon_str[12] = { + "JAN","FEB","MAR","APR","MAY","JUN","JUL", + "AUG","SEP","OCT","NOV","DEC"}; +int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + char s1[16], s2[16]; + scanf("%s%s", s1,s2); + int a = find(day_str, day_str+7, s2) - day_str; + int b = find(mon_str, mon_str+12, s1) - mon_str; + int res = 0; + for (int i=0; i + +using namespace std; + +int main() +{ + int testcase, cases = 0; + scanf("%d", &testcase); + while (getchar() != '\n') + ; + while (testcase--) + { + static char s[131072]; + fgets(s, 131072, stdin); + int cnt[128] = {}; + for (int i = 0; s[i] > ' '; i++) + cnt[s[i]]++; + int mn = INT_MAX; + for (int i = 'a'; i <= 'z'; i++) + mn = min(mn, cnt[i]); + printf("Case %d: %d\n", ++cases, mn); + } + return 0; +} diff --git a/173.cpp b/173.cpp new file mode 100644 index 0000000..c392de9 --- /dev/null +++ b/173.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +bool table[32][32]; +char line[512]; + +int main() +{ + while (gets(line) != NULL) + { + bool isFinish = false; + char *str = strtok(line, ". \t"); + if (*str == '#') + { + break; + } + memset(table, 0, 32 * 32); + int startp = *strtok(NULL, ". \t") - 'A'; + int startl = *strtok(NULL, ". \t") - 'A'; + char *p = strtok(str, ":;"); + while (p != NULL) + { + int from = *p - 'A'; + p = strtok(NULL, ":;"); + while (*p != 0) + table[from][*p - 'A'] = + table[*p - 'A'][from] = true, + p++; + p = strtok(NULL, ":;"); + } + if (startp == startl) + { + printf("Both annihilated in node %c\n", 'A' + startp); + continue; + } + bool visitedp[26], visitedl[26]; + memset(visitedp, 0, 26); + memset(visitedl, 0, 26); + while (1) + { + visitedp[startp] = true; + visitedl[startl] = true; + int t; + //pascal + int i; + for (t = 1; t <= 25; t++) + { + i = (startp + t) % 26; + if (table[startp][i] && !visitedp[i] && + !visitedl[i]) + { + break; + } + } + if (t == 26) + { + printf("Paskill trapped in node %c", 'A' + startp); + isFinish = true; + i = startp; + } + //lisp + int j; + for (t = 1; t <= 25; t++) + { + j = (startl - t + 26) % 26; + if (table[startl][j] && !visitedl[j]) + { + break; + } + } + if (t == 26) + { + if (isFinish) + { + printf(" "); + } + printf("Lisper trapped in node %c", 'A' + startl); + isFinish = true; + j = startl; + } + if (i == j) + { + if (isFinish) + { + printf(" "); + } + printf("Both annihilated in node %c\n", 'A' + i); + isFinish = true; + break; + } + if (visitedp[j]) + { + if (isFinish) + { + printf(" "); + } + printf("Lisper destroyed in node %c\n", 'A' + j); + isFinish = true; + break; + } + if (isFinish) + { + printf("\n"); + break; + } + startp = i; + startl = j; + } + } +} diff --git a/1730.cpp b/1730.cpp new file mode 100644 index 0000000..136080e --- /dev/null +++ b/1730.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +/* + for (int i = 1; i <= n; i++) + ret += i * (n/i); +*/ +long long MSLCM(long long n) +{ + long long l = 1, r, val; + long long ret = 0; + while (l <= n) + { + val = n / l; + r = n / val; + ret += val * ((l + r) * (r - l + 1) / 2); + l = r + 1; + } + return ret - 1; +} +int main() +{ + int n; + while (scanf("%d", &n) == 1 && n) + { + printf("%lld\n", MSLCM(n)); + } + return 0; +} diff --git a/174.cpp b/174.cpp new file mode 100644 index 0000000..12dd1a2 --- /dev/null +++ b/174.cpp @@ -0,0 +1,219 @@ +#include + +using namespace std; + +struct Node +{ + char *what, *c_rhs; + Node *a, *b, *c; + int c_my, c_last, c_eq; + Node(char *S, Node *A = NULL, Node *B = NULL, Node *C = NULL) : what(S), a(A), b(B), c(C) {} +}; + +struct Parser +{ + char *p; + Parser(char *src) : p(src) {} + + int eq(char *word) + { + if (memcmp(p, word, strlen(word)) != 0) + { + return 0; + } + p += strlen(word); + return 1; + } + + Node *cond() + { + Node *z = new Node("COND"); + if (eq("MYLAST")) + { + z->c_my = 1; + } + else if (eq("YOURLAST")) + { + z->c_my = 0; + } + else + { + assert(0 == 1); + } + assert(isdigit(*p)); + z->c_last = *p++ - '0'; + assert(*p == '=' || *p == '#'); + z->c_eq = (*p++ == '='); + if (eq("TRADE")) + { + z->c_rhs = "TRADE"; + } + else if (eq("CHEAT")) + { + z->c_rhs = "CHEAT"; + } + else if (eq("NULL")) + { + z->c_rhs = "NULL"; + } + else + { + assert(0 == 1); + } + if (eq("AND")) + { + return new Node("AND", z, cond()); + } + if (eq("OR")) + { + return new Node("OR", z, cond()); + } + return z; + } + + Node *stmt() + { + if (eq("TRADE")) + { + return new Node("TRADE"); + } + if (eq("CHEAT")) + { + return new Node("CHEAT"); + } + if (!eq("IF")) + { + assert(0 == 1); + } + Node *c = cond(); + if (!eq("THEN")) + { + assert(0 == 1); + } + Node *a = stmt(); + if (!eq("ELSE")) + { + assert(0 == 1); + } + return new Node("IF", c, a, stmt()); + } +}; + +struct Strategy +{ + int score; + char source[512], *last[3]; + Node *node; +}; + +int evalBool(Strategy &A, Strategy &B, Node *x) +{ + if (strcmp(x->what, "AND") == 0) + { + return evalBool(A, B, x->a) && evalBool(A, B, x->b); + } + if (strcmp(x->what, "OR") == 0) + { + return evalBool(A, B, x->a) || evalBool(A, B, x->b); + } + char *lhs = x->c_my ? A.last[x->c_last] : B.last[x->c_last]; + return strcmp(lhs, x->c_rhs) == 0 ? x->c_eq : !x->c_eq; +} + +char *eval(Strategy &A, Strategy &B, Node *x) +{ + for (;;) + { + if (strcmp(x->what, "TRADE") == 0 || strcmp(x->what, "CHEAT") == 0) + { + return x->what; + } + x = evalBool(A, B, x->a) ? x->b : x->c; + } +} + +void play(Strategy &A, Strategy &B) +{ + for (int k = 1; k <= 2; k++) + { + A.last[k] = B.last[k] = "NULL"; + } + for (int k = 0; k < 10; k++) + { + char *a = eval(A, B, A.node); + char *b = eval(B, A, B.node); + if (strcmp(a, "TRADE") == 0) + { + if (strcmp(b, "TRADE") == 0) + { + A.score++, B.score++; + } + else + { + A.score -= 2; + B.score += 2; + } + } + else + { + if (strcmp(b, "TRADE") == 0) + { + A.score += 2; + B.score -= 2; + } + else + { + A.score--, B.score--; + } + } + A.last[2] = A.last[1]; + A.last[1] = a; + B.last[2] = B.last[1]; + B.last[1] = b; + } +} + +int main() +{ + Strategy S[16]; + int n; + memset(S, 0, sizeof(S)); + for (n = 0;; n++) + { + int k = 0, c; + while ((c = getchar()) != EOF) + { + if (isspace(c)) + { + continue; + } + if (k == 0 && c == '#') + { + break; + } + S[n].source[k++] = toupper(c); + if (c == '.') + { + break; + } + } + S[n].source[k] = 0; + if (k == 0) + { + break; + } + S[n].node = Parser(S[n].source).stmt(); + S[n].score = 0; + } + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + play(S[i], S[j]); + } + } + for (int i = 0; i < n; i++) + { + printf("%3d\n", S[i].score); + } +} diff --git a/175.cpp b/175.cpp new file mode 100644 index 0000000..1a89865 --- /dev/null +++ b/175.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +struct Profile +{ + int d; + vector w; + vector out; +}; + +vector prof; + +int main() +{ + char s[4096]; + int ntit = 0; + while (scanf(" %[A-Z] :", s) == 1 && (s[0] == 'P' || s[0] == 'T')) + { + int c; + if (s[0] == 'P') + { + Profile p; + scanf("%d", &p.d); + while ((c = getchar()) != EOF && c != '\n') + { + if (isspace(c)) + { + continue; + } + ungetc(c, stdin); + scanf(" %s", s); + p.w.push_back(string(s)); + } + p.out.clear(); + prof.push_back(p); + } + else + { + vector words; + string w = ""; + ntit++; + do + { + c = getchar(); + if (c != EOF && c != '|' && !isspace(c) && !isalpha(c)) + { + continue; + } + if (c != EOF && isalpha(c)) + { + w += tolower(c); + } + else if (w.size() > 0) + { + words.push_back(w); + w = ""; + } + } while (c != EOF && c != '|'); + for (int p = 0; p < (int)prof.size(); p++) + { + for (int k = 0; k < (int)prof[p].w.size(); k++) + for (int m = 0; m < (int)prof[p].w.size(); m++) + if (k != m) + for (int i = 0; i < (int)words.size(); i++) + for (int j = i + 1; j < (int)words.size(); j++) + { + if (j - i - 1 > prof[p].d) + { + break; + } + if (words[i] == prof[p].w[k] && + words[j] == prof[p].w[m]) + { + prof[p].out.push_back(ntit); + goto done; + } + } + done:; + } + } + } + for (int i = 0; i < (int)prof.size(); i++) + { + printf("%d: ", i + 1); + for (int j = 0; j < (int)prof[i].out.size(); j++) + { + printf(j ? ",%d" : "%d", prof[i].out[j]); + } + printf("\n"); + } +} diff --git a/176.cpp b/176.cpp new file mode 100644 index 0000000..a864eda --- /dev/null +++ b/176.cpp @@ -0,0 +1,383 @@ +#include + +using namespace std; + +long vzdal[51][51][4]; +struct _s {int neex1, neex2;}; +struct _s smerx[49][50]; +struct _s smery[50][49]; +int fronta[16000], vefronte; +char vstup[100]; +int i, j, k, l, m, n, start, konec, navic; +long vysl; + +void zarad(int p) +{ + int x, y, z; + long v; + v = vzdal[(fronta[n] >> 8) & 63][(fronta[n] >> 2) & 63][fronta[n] & 3]; + + if (p == konec) + { + vysl = v; + return; + } + + x = (p >> 8) & 63; + y = (p >> 2) & 63; + z = p & 3; + + switch (z) + { + case 2: + if (x == 0) + { + return; + } + if (smerx[x - 1][y].neex1 == -1) + { + v += 50; + x--; + } + else + { + if (smerx[x - 1][y].neex2 == 49) + { + return; + } + v += 98 - 2 * smerx[x - 1][y].neex2; + z = 0; + } + break; + + case 0: + if (x == 49) + { + return; + } + if (smerx[x][y].neex1 == -1) + { + v += 50; + x++; + } + else + { + if (smerx[x][y].neex1 == 0) + { + return; + } + v += 2 * smerx[x][y].neex1; + z = 2; + } + break; + + case 3: + if (y == 0) + { + return; + } + if (smery[x][y - 1].neex1 == -1) + { + v += 50; + y--; + } + else + { + if (smery[x][y - 1].neex2 == 49) + { + return; + } + v += 98 - 2 * smery[x][y - 1].neex2; + z = 1; + } + break; + + case 1: + if (y == 49) + { + return; + } + if (smery[x][y].neex1 == -1) + { + v += 50; + y++; + } + else + { + if (smery[x][y].neex1 == 0) + { + return; + } + v += 2 * smery[x][y].neex1; + z = 3; + } + break; + } + + if (vzdal[x][y][z] != -1) + { + return; + } + + vzdal[x][y][z] = v; + + if ((fronta[vefronte++] = (x << 8) + (y << 2) + z) == konec) + { + vysl = v; + } +} + +int main() +{ + for (i = 0; i < 50; i++) + { + for (j = 0; j < 49; j++) + { + smery[i][j].neex1 = smerx[j][i].neex1 = -1; + } + } + + gets(vstup); + + while (vstup[0] != '#') + { + sscanf(vstup + 1, "%d", &i); + sscanf(vstup + 4, "%d", &j); + sscanf(vstup + 9, "%d", &k); + + if (vstup[0] == 'A') + { + for (l = j / 100; l <= k / 100; l++) + { + smery[i][l].neex1 = (l == j / 100) ? j % 100 / 2 : 0; + smery[i][l].neex2 = (l == k / 100) ? (k % 100) / 2 : 49; + } + } + else + { + for (l = j / 100; l <= k / 100; l++) + { + smerx[l][i].neex1 = (l == j / 100) ? j % 100 / 2 : 0; + smerx[l][i].neex2 = (l == k / 100) ? (k % 100) / 2 : 49; + } + } + gets(vstup); + } + + gets(vstup); + + while (vstup[0] != '#') + { + for (i = 0; i < 51; i++) + { + for (j = 0; j < 51; j++) + { + for (k = 0; k < 4; k++) + { + vzdal[i][j][k] = -1; + } + } + } + + navic = 0; + vysl = -1; + sscanf(vstup + 1, "%d", &i); + sscanf(vstup + 4, "%d", &j); + + if (vstup[0] == 'A') + { + if (j & 1) + { + if ((smery[i][j / 100].neex1 == -1) || ((j >> 1) % 50 > smery[i][j / 100].neex2)) + { + start = (i << 8) + ((j / 100 + 1) << 2) + 1; + navic += 49 - (j >> 1) % 50; + } + else + { + start = (i << 8) + ((j / 100) << 2) + 3; + navic += 2 * smery[i][j / 100].neex1 - (j >> 1) % 50 - 1; + } + } + else if ((smery[i][j / 100].neex1 == -1) || ((j >> 1) % 50 < smery[i][j / 100].neex1)) + { + start = (i << 8) + ((j / 100) << 2) + 3; + navic += (j >> 1) % 50; + } + else + { + start = (i << 8) + ((j / 100 + 1) << 2) + 1; + navic += 48 + (j >> 1) % 50 - 2 * smery[i][j / 100].neex2; + } + } + else if (j & 1) + { + if ((smerx[j / 100][i].neex1 == -1) || ((j >> 1) % 50 > smerx[j / 100][i].neex2)) + { + start = (i << 2) + ((j / 100 + 1) << 8) + 0; + navic += 49 - (j >> 1) % 50; + } + else + { + start = (i << 2) + ((j / 100) << 8) + 2; + navic += 2 * smerx[j / 100][i].neex1 - (j >> 1) % 50 - 1; + } + } + else if ((smerx[j / 100][i].neex1 == -1) || ((j >> 1) % 50 < smerx[j / 100][i].neex1)) + { + start = (i << 2) + ((j / 100) << 8) + 2; + navic += (j >> 1) % 50; + } + else + { + start = (i << 2) + ((j / 100 + 1) << 8) + 0; + navic += 48 + (j >> 1) % 50 - 2 * smerx[j / 100][i].neex2; + } + + m = i; + n = j; + sscanf(vstup + 10, "%d", &i); + sscanf(vstup + 13, "%d", &j); + + if (vstup[9] == 'A') + { + if (j & 1) + { + if ((smery[i][j / 100].neex1 == -1) || ((j >> 1) % 50 < smery[i][j / 100].neex1)) + { + konec = (i << 8) + ((j / 100) << 2) + 1; + navic += (j >> 1) % 50; + if ((vstup[0] == 'A') && (n / 100 == j / 100) && (m == i) && (n & 1) && ((n >> 1) % 50 <= (j >> 1) % 50)) + { + vysl = (j - n) >> 1; + } + } + else + { + konec = (i << 8) + ((j / 100 + 1) << 2) + 3; + navic += 48 + (j >> 1) % 50 - 2 * smery[i][j / 100].neex2; + if ((vstup[0] == 'A') && (start + 4 - 2 == konec) && ((n >> 1) % 50 > smery[i][j / 100].neex2)) + { + if (!(n & 1)) + { + vysl = -1 + (n >> 1) % 50 + (j >> 1) % 50 - 2 * smery[i][j / 100].neex2; + } + else if (n <= j) + { + vysl = (j - n) >> 1; + } + } + } + } + else if ((smery[i][j / 100].neex1 == -1) || ((j >> 1) % 50 > smery[i][j / 100].neex2)) + { + konec = (i << 8) + ((j / 100 + 1) << 2) + 3; + navic += 49 - (j >> 1) % 50; + if ((vstup[0] == 'A') && (n / 100 == j / 100) && (m == i) && (!(n & 1)) && ((j >> 1) % 50 <= (n >> 1) % 50)) + { + vysl = (n - j) >> 1; + } + } + else + { + konec = (i << 8) + ((j / 100) << 2) + 1; + navic += 2 * smery[i][j / 100].neex1 - (j >> 1) % 50 - 1; + if ((vstup[0] == 'A') && (start - 4 + 2 == konec) && ((n >> 1) % 50 < smery[i][j / 100].neex1)) + { + if (n & 1) + { + vysl = 2 * smery[i][j / 100].neex1 - (n >> 1) % 50 - (j >> 1) % 50 - 1; + } + else if (j <= n) + { + vysl = (n - j) >> 1; + } + } + } + } + else if (j & 1) + { + if ((smerx[j / 100][i].neex1 == -1) || ((j >> 1) % 50 < smerx[j / 100][i].neex1)) + { + konec = (i << 2) + ((j / 100) << 8) + 0; + navic += (j >> 1) % 50; + + if ((vstup[0] == 'S') && (n / 100 == j / 100) && (m == i) && (n & 1) && ((n >> 1) % 50 <= (j >> 1) % 50)) + vysl = (j - n) >> 1; + } + else + { + konec = (i << 2) + ((j / 100 + 1) << 8) + 2; + navic += 48 + (j >> 1) % 50 - 2 * smerx[j / 100][i].neex2; + + if ((vstup[0] == 'S') && (start == konec - 2) && ((n >> 1) % 50 > smerx[j / 100][i].neex2)) + { + if (!(n & 1)) + vysl = -1 + (n >> 1) % 50 + (j >> 1) % 50 - 2 * smerx[j / 100][i].neex2; + else if (n <= j) + vysl = (j - n) >> 1; + } + } + } + else if ((smerx[j / 100][i].neex1 == -1) || ((j >> 1) % 50 > smerx[j / 100][i].neex2)) + { + konec = (i << 2) + ((j / 100 + 1) << 8) + 2; + navic += 49 - (j >> 1) % 50; + if ((vstup[0] == 'S') && (n / 100 == j / 100) && (m == i) && (!(n & 1)) && ((j >> 1) % 50 <= (n >> 1) % 50)) + { + vysl = (n - j) >> 1; + } + } + else + { + konec = (i << 2) + ((j / 100) << 8) + 0; + navic += 2 * smerx[j / 100][i].neex1 - (j >> 1) % 50 - 1; + if ((vstup[0] == 'S') && (start == konec + 2) && ((n >> 1) % 50 < smerx[j / 100][i].neex1)) + { + if (n & 1) + { + vysl = 2 * smerx[j / 100][i].neex1 - (n >> 1) % 50 - (j >> 1) % 50 - 1; + } + else if (j <= n) + { + vysl = (n - j) >> 1; + } + } + } + + if (vysl >= 0) + { + printf("%ld\n", --vysl); + } + else + { + fronta[0] = start; + vzdal[(start >> 8) & 63][(start >> 2) & 63][start & 3] = 0; + n = 0; + vefronte = 1; + while (vysl < 0) + { + if ((vysl < 0) && ((fronta[n] & 3) != 0)) + { + zarad((fronta[n] & 0xfffc) | 2); + } + if ((vysl < 0) && ((fronta[n] & 3) != 1)) + { + zarad((fronta[n] & 0xfffc) | 3); + } + if ((vysl < 0) && ((fronta[n] & 3) != 2)) + { + zarad((fronta[n] & 0xfffc) | 0); + } + if ((vysl < 0) && ((fronta[n] & 3) != 3)) + { + zarad((fronta[n] & 0xfffc) | 1); + } + n++; + } + printf("%ld\n", vysl + navic); + } + gets(vstup); + } + return 0; +} diff --git a/177.cpp b/177.cpp new file mode 100644 index 0000000..ac30b10 --- /dev/null +++ b/177.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +int main() +{ + int a, ll = 0, rl = 0, ul = 0, dl = 0; + string b = "", temp1, temp2; + while (true) + { + scanf("%d", &a); + ll = 0; + rl = 0; + ul = 0; + dl = 0; + if (a == 0) + { + return 0; + } + b = "RU"; + for (int i = 1; i < a; ++i) + { + temp1 = b; + for (int j = 0; j < temp1.size(); ++j) + { + if (temp1[j] == 'R') + { + temp1[j] = 'U'; + } + else if (temp1[j] == 'L') + { + temp1[j] = 'D'; + } + else if (temp1[j] == 'U') + { + temp1[j] = 'L'; + } + else + { + temp1[j] = 'R'; + } + } + temp2 = temp1; + for (int j = 0; j < temp1.size(); j++) + { + temp2[j] = temp1[temp1.size() - j - 1]; + } + b += temp2; + } + int x = 0, y = 0; + for (int i = 0; i < b.size(); ++i) + { + if (b[i] == 'R') + { + x += 1; + if (x > rl) + { + rl = x; + } + } + if (b[i] == 'L') + { + x -= 1; + if (x < ll) + { + ll = x; + } + } + if (b[i] == 'U') + { + y -= 1; + if (y < ul) + { + ul = y; + } + } + if (b[i] == 'D') + { + y += 1; + if (y > dl) + { + dl = y; + } + } + } + const int num = dl - ul; + string c[num + 1]; + y = (-1) * ul; + x = (-1) * ll; + for (int i = 0; i < b.size(); i++) + { + if (b[i] == 'R') + { + x += 1; + if (c[y].size() < 2 * x) + { + c[y].resize(2 * x); + } + (c[y])[2 * x - 1] = '_'; + } + if (b[i] == 'L') + { + x -= 1; + if (c[y].size() < 2 * x + 2) + { + c[y].resize(2 * x + 2); + } + (c[y])[2 * x + 1] = '_'; + } + if (b[i] == 'U') + { + y -= 1; + if (c[y + 1].size() < 2 * x + 1) + { + c[y + 1].resize(2 * x + 1); + } + (c[y + 1])[2 * x] = '|'; + } + if (b[i] == 'D') + { + y += 1; + if (c[y].size() < 2 * x + 1) + { + c[y].resize(2 * x + 1); + } + (c[y])[2 * x] = '|'; + } + } + bool first = false; + for (int i = 0; i < num + 1; i++) + { + for (int j = 0; j < c[i].size(); j++) + { + if ((c[i])[j] != '|' && (c[i])[j] != '_') + { + (c[i])[j] = ' '; + } + } + } + for (int i = 0; i < c[0].size(); i++) + { + if ((c[0])[i] != ' ') + { + first = true; + break; + } + } + for (int i = 0; i < num + 1; i++) + { + if (a == 1) + { + printf("_|\n"); + break; + } + if (i == 0 && first == false) + { + continue; + } + cout << c[i]; + putchar(10); + } + putchar('^'); + putchar(10); + } +} diff --git a/178.cpp b/178.cpp new file mode 100644 index 0000000..4366a71 --- /dev/null +++ b/178.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +int top[64], cnt[64], n; + +int get() +{ + static char s[] = "A23456789TJQK"; + int c, t; + while ((c = getchar()) != EOF && c != '#' && !isalnum(c)) + ; + if (c == '#') + { + ungetc(c, stdin); + } + if (c == EOF || c == '#') + { + return -1; + } + while ((t = getchar()) != EOF && !isalnum(t)) + ; + return (strchr(s, toupper(c)) - s) + 1; +} + +void find2(int x[]) +{ + int i, j; + for (i = 0; i < n; i++) + { + if (top[i] > 10) + { + continue; + } + for (j = i + 1; j < n; j++) + { + if (top[j] > 10 || (top[i] + top[j]) != 11) + { + continue; + } + x[0] = i; + x[1] = j; + return; + } + } +} + +void find3(int x[]) +{ + int i, j, k; + for (i = 0; i < n; i++) + { + if (top[i] <= 10) + { + continue; + } + for (j = i + 1; j < n; j++) + { + if (top[j] <= 10 || top[j] == top[i]) + { + continue; + } + for (k = j + 1; k < n; k++) + { + if (top[k] <= 10 || top[k] == top[i] || + top[k] == top[j]) + { + continue; + } + x[0] = i; + x[1] = j; + x[2] = k; + return; + } + } + } +} + +int main() +{ + int a[64], x[4], y[4], i, t; + for (t = 1;; t++) + { + for (i = 0; i < 52 && (a[i] = get()) > 0; i++) + ; + if (i < 52) + { + break; + } + n = 0; + for (i = 0; i < 52;) + { + x[0] = y[0] = 100; + find2(x); + find3(y); + if (x[0] == 100 && y[0] == 100) + { + if (n == 16) + { + break; + } + top[n] = a[i++]; + cnt[n++] = 1; + } + else if (x[0] < y[0]) + { + if (i < 52) + { + top[x[0]] = a[i++], cnt[x[0]]++; + } + if (i < 52) + { + top[x[1]] = a[i++], cnt[x[1]]++; + } + } + else + { + if (i < 52) + { + top[y[0]] = a[i++], cnt[y[0]]++; + } + if (i < 52) + { + top[y[1]] = a[i++], cnt[y[1]]++; + } + if (i < 52) + { + top[y[2]] = a[i++], cnt[y[2]]++; + } + } + } + if (i < 52) + { + printf("%3d: Overflowed on card no %d\n", t, i + 1); + continue; + } + printf("%3d:", t); + for (i = 0; i < n; i++) + { + printf("%3d", cnt[i]); + } + printf("\n"); + } + return 0; +} diff --git a/179.cpp b/179.cpp new file mode 100644 index 0000000..1788860 --- /dev/null +++ b/179.cpp @@ -0,0 +1,303 @@ +#include + +using namespace std; + +// algo for each (plain-text, cipher-text) pair is +// as follows. Values for k (the period length) are considered in +// turn from 1 up to the text length. For each value of k: +// 1. exists_permute is called to check that k is possible. For every +// full block, it is checked that the frequencies of plain text characters +// matches that of the cipher text characters (if not one can't be a +// permutation of the other). If the final block is not a full block +// then it isn't checked. +// 2. The overall set of bit maps is initialised---one for each position +// in the line (0..linelen). For overall[i], bit j is set if character +// j in the cipher text is the same as character i in the plain text, and is +// 0 otherwise. +// 3. The k bitmaps (maps) are calculated based on the overall bitmaps. +// maps[i] shows for position i in the block, the set of bit positions +// in the cipher text that position i could be mapped to. If any maps[i] +// is 0 then the current value of k must be wrong. +// 4. Try to find a permutation based on maps. Start with the plaintext +// characters with the fewest possible cipher text positions. Try all +// possible combinations. +// 5. If a permutation is found print the details. + +// maxlen of ciphertxt + plaintxt str +#define MAXLEN 256 +// len(char[]) needed to hold a bitmap of maxlen bits +#define BIT_LEN (MAXLEN / 8) +#define NUM_CHARS 256 + +typedef unsigned char *bitmap; + +struct lst +{ + int posn; + bitmap b; + int num_bits; + struct lst *next; +} lst_arr[MAXLEN], *head; + +string plain, cipher, cipher2; +int mapping[MAXLEN], linelen; +bitmap *maps, *overall; + +static void clear_bits(bitmap b) +{ + for (int i = 0; i < BIT_LEN; i++) + { + b[i] = 0; + } +} + +static void set_bits(bitmap b) +{ + for (int i = 0; i < BIT_LEN; i++) + { + b[i] = 0xff; + } +} + +static int get_bit(bitmap b, int bit) +{ + int word = bit / 8; + int offset = bit % 8; + return (b[word] >> offset) & 1; +} + +static void set_bit(bitmap b, int bit, int value) +{ + int word = bit / 8; + int offset = bit % 8; + if (value) + { + b[word] |= 1 << offset; + } + else + { + b[word] &= ~(1 << offset); + } +} + +static void and_bits(bitmap op1, bitmap op2) +{ + int i; + for (i = 0; i < BIT_LEN; i++) + { + op1[i] &= op2[i]; + } +} + +static int zero_bits(bitmap b) +{ + int i; + for (i = 0; i < BIT_LEN; i++) + { + if (b[i]) + { + return 0; + } + } + return 1; +} + +static int count_bits(bitmap b) +{ + int i; + int cnt = 0; + for (i = 0; i < MAXLEN; i++) + { + if (get_bit(b, i)) + { + cnt++; + } + } + return cnt; +} + +static int exists_permute(int k, int linelen) +{ + int pcnt[NUM_CHARS], ccnt[NUM_CHARS]; + for (int block = 0; block + k < linelen; block += k) + { + for (int i = 0; i < NUM_CHARS; i++) + { + pcnt[i] = ccnt[i] = 0; + } + for (int i = 0; i < k && block + i < linelen; i++) + { + pcnt[plain[block + i]]++; + ccnt[cipher[block + i]]++; + } + for (int i = 0; i < NUM_CHARS; i++) + { + if (pcnt[i] != ccnt[i]) + { + return 0; + } + } + } + return 1; +} + +static int compute_map(int i, int k, int linelen) +{ + int posn, j, block; + static bitmap tmp; + if (!tmp) + { + tmp = (bitmap) malloc(BIT_LEN); + } + // calc pos i for cycle length == k + set_bits(maps[i]); + for (posn = i; posn < linelen; posn += k) + { + clear_bits(tmp); + block = (posn / k) * k; + for (j = block; j < block + k; j++) + { + if (j >= linelen || get_bit(overall[posn], j)) + { + set_bit(tmp, j - block, 1); + } + } + and_bits(maps[i], tmp); + } +} + +static int search(struct lst *lst, bitmap used, int k) +{ + if (!lst) + { + return 1; + } + for (int i = 0; i < k; i++) + { + if (get_bit(lst->b, i) && !get_bit(used, i)) + { + mapping[lst->posn] = i; + set_bit(used, i, 1); + if (search(lst->next, used, k)) + { + return 1; + } + set_bit(used, i, 0); + } + } + return 0; +} + +static int check_permute(int k) +{ + int i; + struct lst **lp; + static bitmap used = 0; + head = 0; + for (i = 0; i < k; i++) + { + lst_arr[i].posn = i; + lst_arr[i].num_bits = count_bits(maps[i]); + lst_arr[i].b = maps[i]; + for (lp = &head; *lp && (*lp)->num_bits < lst_arr[i].num_bits; + lp = &(*lp)->next) + { + ; + } + lst_arr[i].next = *lp; + *lp = &lst_arr[i]; + } + if (!used) + { + used = (bitmap)malloc(BIT_LEN); + } + clear_bits(used); + return search(head, used, k); +} + +static void print_permute(int k) +{ + int i; + printf("%1d", k); + for (i = 0; i < k; i++) + { + printf(" %1d->%1d", i + 1, mapping[i] + 1); + } + printf("\n"); +} + +static void decipher(string ctext, int k) +{ + char *cpos; + int reverse_map[MAXLEN]; + int clen = ctext.length(); + while (clen % k != 0) + { + ctext += '?'; + clen++; + } + for (int i = 0; i < k; i++) + { + reverse_map[i] = mapping[i]; + } + for (int cpos = 0; cpos < clen; cpos += k) + { + for (int i = 0; i < k; i++) + { + putchar(ctext[cpos+reverse_map[i]]); + } + } + putchar('\n'); +} + +int main() +{ + int found, i, j, k, abort; + maps = (bitmap*)malloc(MAXLEN * sizeof(*maps)); + overall = (bitmap*)malloc(MAXLEN * sizeof(*maps)); + for (i = 0; i < MAXLEN; i++) + { + maps[i] = (bitmap)malloc(BIT_LEN); + overall[i] = (bitmap)malloc(BIT_LEN); + } + while (getline(cin, plain) && getline(cin, cipher) && getline(cin, cipher2)) + { + found = 0; + linelen = plain.length(); + for (i = 0; i < linelen; i++) + { + for (j = 0; j < linelen; j++) + { + set_bit(overall[i], j, plain[i] == cipher[j]); + } + } + for (k = 1; k <= linelen && !found; k++) + { + if (exists_permute(k, linelen)) + { + abort = 0; + for (i = 0; !abort && i < k; i++) + { + compute_map(i, k, linelen); + if (zero_bits(maps[i])) + { + abort = 1; + break; + } + } + if (!abort) + { + if (found = check_permute(k)) + { + decipher(cipher2, k); + } + } + } + } + if (!found) + { + printf("%s\n", cipher2.c_str()); + } + } + return 0; +} diff --git a/180.cpp b/180.cpp new file mode 100644 index 0000000..1684ea3 --- /dev/null +++ b/180.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +const int maxn = 1000001; +int f[maxn], g[maxn]; +bool safe[maxn]; + +void init() +{ + for (int i = 1; i <= maxn; i++) + { + f[i] = (f[i - 1] + 15) % i, g[i] = (i - f[i]) % i; + } +} + +int main() +{ + init(); + int min, max; + while (scanf("%d %d", &min, &max) == 2, min + max > 0) + { + if (min > max) + { + int t = min; + min = max; + max = t; + } + memset(safe, true, sizeof(safe)); + for (int i = min; i <= max; i++) + { + safe[f[i]] = safe[g[i]] = false; + } + int p; + for (p = 1; p <= min / 2; p++) + if (safe[p]) + { + printf("%d\n", p); + break; + } + if (p > min / 2) + { + puts("Better estimate needed"); + } + } +} diff --git a/181.cpp b/181.cpp new file mode 100644 index 0000000..bfeaab3 --- /dev/null +++ b/181.cpp @@ -0,0 +1,221 @@ +#include + +using namespace std; + +static map denominations; + +bool theTruthIsOutThere(string a, string b) +{ + if (denominations[a[0]] > denominations[b[0]]) + { + return true; + } + else if (denominations[a[0]] == denominations[b[0]]) + { + if (a[1] == 'S') + { + return true; + } + else if (b[1] == 'S') + { + return false; + } + else if (a[1] == 'H') + { + return true; + } + else if (b[1] == 'H') + { + return false; + } + else if (a[1] == 'D') + { + return true; + } + else if (b[1] == 'D') + { + return false; + } + } + else + { + return false; + } +} + +int main() +{ + char trump; + string input; + vector cards, players[5], result[5], step; + denominations['2'] = 2; + denominations['3'] = 3; + denominations['4'] = 4; + denominations['5'] = 5; + denominations['6'] = 6; + denominations['7'] = 7; + denominations['8'] = 8; + denominations['9'] = 9; + denominations['T'] = 10; + denominations['J'] = 11; + denominations['Q'] = 12; + denominations['K'] = 13; + denominations['A'] = 14; + while (cin >> input) + { + if (input == "#") + { + break; + } + cards.clear(); + for (int i = 0; i < 5; i++) + { + result[i].clear(); + } + cards.push_back(input); + for (int i = 0; i < 51; i++) + { + cin >> input; + cards.push_back(input); + } + for (int i = 0; i < 50; i++) + { + players[i % 5].push_back(cards[i]); + } + // find the trump from 2 remaining cards + if (denominations[cards[50][0]] < denominations[cards[51][0]]) + { + trump = cards[51][1]; + } + else if (denominations[cards[50][0]] > denominations[cards[51][0]]) + { + trump = cards[50][1]; + } + else + { + // S H D C + if (cards[50][1] == 'S' || cards[51][1] == 'S') + { + trump = 'S'; + } + else if (cards[50][1] == 'H' || cards[51][1] == 'H') + { + trump = 'H'; + } + else if (cards[50][1] == 'D' || cards[51][1] == 'D') + { + trump = 'D'; + } + else + { + trump = 'C'; + } + } + // sort all players' cards + for (int i = 0; i < 5; i++) + { + sort(players[i].begin(), players[i].end(), theTruthIsOutThere); + } + // who's turn it is to start + int player = 0;// 0 means the one to the left of the dealer + pair best; + string first = ""; + string taken; + // the game begins + for (int i = 0; i < 10; i++) + { + step.clear(); + int current; + first = players[player][0]; + best.first = players[player][0]; + best.second = player; + // removing this card from player's stack + players[player].erase(players[player].begin()); + step.push_back(first); + // for each player + for (int j = 1; j < 5; j++) + { + current = player + j; + if (current > 4) + { + current = current % 5; + } + bool found = false; + // searching if this player has a card of the same suit + for (int k = 0; k < players[current].size(); k++) + { + if (players[current][k][1] == first[1]) + { + found = true; + taken = players[current][k]; + step.push_back(players[current][k]); + players[current].erase(players[current].begin() + k); + break; + } + } + // so there is no card of the same suit + // trying to find the biggest trump + if (!found) + { + for (int k = 0; k < players[current].size(); k++) + { + if (players[current][k][1] == trump) + { + found = true; + taken = players[current][k]; + step.push_back(players[current][k]); + players[current].erase(players[current].begin() + k); + break; + } + } + } + // this player has no card of the needed suit and no trumps + // which leaves the only solution - give up the biggest hand + if (!found) + { + found = true; + taken = players[current][0]; + step.push_back(players[current][0]); + players[current].erase(players[current].begin()); + } + // now after this step we need to determine the player + // who is currently winning this stack... + if (taken[1] == best.first[1])// if the suit is the same + { + if (denominations[taken[0]] > denominations[best.first[0]]) + { + best.first = taken; + best.second = current; + } + } + else if (taken[1] == trump)// if it is the first trump during this step + { + best.first = taken; + best.second = current; + } + } + // now we have the winner and save the results of the step + for (int p = 0; p < step.size(); p++) + { + result[best.second].push_back(step[p]); + } + player = best.second; + } + // now we have played the game and can count the results + int temp = 4; + for (int i = 0; i < 5; i++) + { + int sum = 0; + for (int j = 0; j < result[(temp + i) % 5].size(); j++) + { + if (result[(temp + i) % 5][j][1] == 'H') + { + sum += denominations[result[(temp + i) % 5][j][0]]; + } + } + cout << setw(3) << sum; + } + cout << endl; + } + return 0; +} diff --git a/182.cpp b/182.cpp new file mode 100644 index 0000000..2badbfe --- /dev/null +++ b/182.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define IN_FILE "PROBLEMD.DAT" +char buff[100]; + +long raise(long x, int exponent) +{ + int i; + long res = 1; + for (i = 0; i < exponent; i++) + { + res *= x; + } + return res; +} + +int main() +{ + long num, div, dist, range; + long freq[10]; + int position; + /* FILE *in_file = fopen(IN_FILE, "r");*/ + FILE *in_file; + char district; + char *cp; + int cnt = 0; + int left_posn, i; + /* if (in_file == 0) { + fprintf(stderr, "Failed to open `%s'\n", IN_FILE); + exit(1); + }*/ + in_file = stdin; + while (1) + { + fgets(buff, sizeof(buff), in_file); + num = 0; + for (i = 0, cp = buff; i < 10; i++, cp++) + { + int digit; + if (i == 2) + { + district = *cp; + } + else + { + digit = *cp - '0'; + num = num == 0 && digit != 0 ? digit : num * 10 + digit; + } + } + if (num == 0 && district == '0') + { + break; + } + sscanf(cp, "%d", &position); + if (position == 0) + { + break; + } + if (cnt++ != 0) + { + printf("\n"); + } + if (position == 3) + { + for (i = 0; i < 10; i++) + { + if (i == (district - '0')) + { + printf("%11ld\n", district == '0' ? num - 1 : num); + } + else + { + printf("%11ld\n", 0L); + } + } + continue; + } + for (i = 0; i < 10; i++) + { + freq[i] = 0; + } + if (position == 1) + { + left_posn = 9; + } + else if (position == 2) + { + left_posn = 8; + } + else + { + left_posn = 11 - position; + } + /* + * Distribute everything to the digits left + */ + if (left_posn != 9) + { + dist = num - num % raise(10, left_posn); + for (i = 0; i < 10; i++) + { + freq[i] += dist / 10; + } + } + /* + * Distribute the digit. + */ + dist = (num % raise(10, left_posn)) - num % raise(10, left_posn - 1); + range = dist / raise(10, left_posn - 1); + for (i = 0; i < range; i++) + { + freq[i] += dist / range; + } + /* + * Distribute everything to the digit's right. + */ + if (left_posn != 1) + { + freq[range] += num % raise(10, left_posn - 1); + } + for (i = 0; i < 10; i++) + { + printf("%11ld\n", district == '0' && i == 0 ? freq[i] - 1 : freq[i]); + } + } + return 0; +} diff --git a/183.cpp b/183.cpp new file mode 100644 index 0000000..b74619c --- /dev/null +++ b/183.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back + +static string output2; +static char output[10000], in[1000][1000]; + +inline int calc(int basey, int basex, int r, int c) +{ + bool all1 = true, all0 = true; + rep(i, r) + { + rep(j, c) + { + if (in[basey + i][basex + j] == '0') + { + all1 = false; + } + else if (in[basey + i][basex + j] == '1') + { + all0 = false; + } + } + } + if (all1) + { + return 1; + } + else if (all0) + { + return 0; + } + else + { + return -1; + } +} + +inline void enc(int &p, int basey, int basex, int r, int c) +{ + int val = calc(basey, basex, r, c); + if (val == 0) + { + output[p++] = '0'; + } + else if (val == 1) + { + output[p++] = '1'; + } + else + { + output[p++] = 'D'; + enc(p, basey, basex, r / 2 + r % 2, c / 2 + c % 2); + if (c != 1) + { + enc(p, basey, basex + c / 2 + c % 2, r / 2 + r % 2, c / 2); + } + if (r != 1) + { + enc(p, basey + r / 2 + r % 2, basex, r / 2, c / 2 + c % 2); + } + if (c != 1 && r != 1) + { + enc(p, basey + r / 2 + r % 2, basex + c / 2 + c % 2, r / 2, c / 2); + } + } +} + +inline void fillt(int basey, int basex, int r, int c, char t) +{ + rep(i, r) + { + rep(j, c) + { + in[basey + i][basex + j] = t; + } + } +} + +inline void dec(int &p, int basey, int basex, int r, int c) +{ + if (output2[p] == 'D') + { + p++; + dec(p, basey, basex, r / 2 + r % 2, c / 2 + c % 2); + if (c != 1) + { + dec(p, basey, basex + c / 2 + c % 2, r / 2 + r % 2, c / 2); + } + if (r != 1) + { + dec(p, basey + r / 2 + r % 2, basex, r / 2, c / 2 + c % 2); + } + if (c != 1 && r != 1) + { + dec(p, basey + r / 2 + r % 2, basex + c / 2 + c % 2, r / 2, c / 2); + } + } + else if (output2[p] == '0') + { + fillt(basey, basex, r, c, '0'); + p++; + } + else if (output2[p] == '1') + { + fillt(basey, basex, r, c, '1'); + p++; + } +} + +inline void getdata(int &p, int basey, int basex, int r, int c, int &cnt) +{ + if (p >= (int)output2.size()) + { + return; + } + if (output2[p] == 'D') + { + p++; + getdata(p, basey, basex, r / 2 + r % 2, c / 2 + c % 2, cnt); + if (c != 1) + { + getdata(p, basey, basex + c / 2 + c % 2, r / 2 + r % 2, c / 2, cnt); + } + if (r != 1) + { + getdata(p, basey + r / 2 + r % 2, basex, r / 2, c / 2 + c % 2, cnt); + } + if (c != 1 && r != 1) + { + getdata(p, basey + r / 2 + r % 2, basex + c / 2 + c % 2, r / 2, c / 2, cnt); + } + } + else if (output2[p] == '0') + { + cnt += r * c; + p++; + } + else if (output2[p] == '1') + { + cnt += r * c; + p++; + } +} + +int main() +{ + char type; + int r, c; + while (cin >> type >> r >> c && type != '#') + { + int p = 0; + if (type == 'B') + { + rep(i, r) rep(j, c) cin >> in[i][j]; + enc(p, 0, 0, r, c); + output[p] = '\0'; + printf("D%4d%4d\n", r, c); + int cnt = 0; + for (int i = 0; output[i] != '\0'; i++) + { + if (cnt == 50) + { + cout << endl, cnt = 0; + } + cout << output[i]; + cnt++; + } + cout << endl; + } + else if (type == 'D') + { + int p = 0; + output2 = ""; + while (true) + { + string tmp; + cin >> tmp; + output2 += tmp; + int cnt = 0; + getdata(p, 0, 0, r, c, cnt); + if (cnt == r * c) + { + break; + } + } + p = 0; + dec(p, 0, 0, r, c); + printf("B%4d%4d\n", r, c); + int cnt = 0; + rep(i, r) rep(j, c) + { + if (cnt == 50) + { + cout << endl, cnt = 0; + } + putchar(in[i][j]); + cnt++; + } + puts(""); + } + } + return 0; +} diff --git a/184.cpp b/184.cpp new file mode 100644 index 0000000..52876c3 --- /dev/null +++ b/184.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +struct Point +{ + int x, y; +}; + +int cmpY(const Point &a, const Point &b) +{ + return (a.y < b.y); +} + +int cmpX(const Point &a, const Point &b) +{ + return (a.x < b.x); +} + +inline bool is_collinear(const Point &a, const Point &b, const Point &c) +{ + int pos = a.x * b.y + b.x * c.y + c.x * a.y; + int neg = a.x * c.y + b.x * a.y + c.x * b.y; + return pos == neg; +} + +void output(vector &line, int is_first_line) +{ + if (is_first_line) + { + puts("The following lines were found:"); + } + for (int i = 0; i < line.size(); ++i) + { + printf("(%4d,%4d)", line[i].x, line[i].y); + } + puts(""); +} + +bool is_prev_exist(Point a, Point b, vector prev_line) +{ + for (int i = 0; i < prev_line.size(); i = i + 2) + { + int r1 = is_collinear(a, b, prev_line[i]); + int r2 = is_collinear(a, b, prev_line[i + 1]); + if (r1 && r2) + { + return true; + } + } + return false; +} + +void find_all_collinear(vector &points) +{ + int no_line_found = 1; + int is_first_line = 1; + std::stable_sort(points.begin(), points.end(), cmpY); + std::stable_sort(points.begin(), points.end(), cmpX); + vector prev_line; + for (int i = 0; i < points.size(); ++i) + { + for (int j = i + 1; j < points.size(); ++j) + { + if (is_prev_exist(points[i], points[j], prev_line)) + { + continue; + } + vector line; + line.push_back(points[i]); + line.push_back(points[j]); + // check line pi-pj + for (int k = j + 1; k < points.size(); ++k) + { + if (is_collinear(points[i], points[j], points[k])) + { + no_line_found = 0; + line.push_back(points[k]); + } + } + if (line.size() > 2) + { + output(line, is_first_line); + is_first_line = 0; + prev_line.push_back(line[0]); + prev_line.push_back(line[1]); + } + } + } + if (no_line_found) + { + puts("No lines were found"); + } +} + +int main() +{ + while (1) + { + vector point_list; + Point p; + while (scanf("%d %d", &p.x, &p.y) == 2) + { + if (p.x == 0 && p.y == 0) + { + break; + } + point_list.push_back(p); + } + if (point_list.size() == 0) + { + break; + } + find_all_collinear(point_list); + } + return 0; +} diff --git a/185.cpp b/185.cpp new file mode 100644 index 0000000..b366aba --- /dev/null +++ b/185.cpp @@ -0,0 +1,268 @@ +#include + +using namespace std; + +#define BUF_LEN 40 + +void split_input(char *buf, char *a, char *b, char *c); +int is_correct(char *a, char *b, char *c); +int cal_value(char *a); +int map_(char c); +int cal_char_num(char *a, char *b, char *c); +void dfs(int cur, char *str); + +static int dep, vis[10]; +char combuf[BUF_LEN], temp[10], abuf[BUF_LEN], bbuf[BUF_LEN], cbuf[BUF_LEN]; +int combuflen, lena, lenb, lenc, solnum; + +int main() +{ + char buf[BUF_LEN]; + setvbuf(stdout, NULL, _IONBF, 0); + while (fgets(buf, BUF_LEN, stdin) && buf[0] != '#') + { + split_input(buf, abuf, bbuf, cbuf); + if (is_correct(abuf, bbuf, cbuf)) + { + printf("Correct "); + } + else + { + printf("Incorrect "); + } + dep = cal_char_num(abuf, bbuf, cbuf); + lena = strlen(abuf); + lenb = strlen(bbuf); + lenc = strlen(cbuf); + combuflen = lena + lenb + lenc; + memset(vis, 0, sizeof(vis)); + solnum = 0; + strcpy(combuf, abuf); + strcat(combuf, bbuf); + strcat(combuf, cbuf); + dfs(0, combuf); + if (solnum == 0) + { + printf("impossible\n"); + } + else if (solnum == 1) + { + printf("valid\n"); + } + else + { + printf("ambiguous\n"); + } + } + return 0; +} + +void split_input(char *buf, char *a, char *b, char *c) +{ + int aflag, bflag, cflag; + int alen, blen, clen; + int len; + int i; + len = strlen(buf); + aflag = 1, bflag = 0, cflag = 0; + alen = blen = clen = 0; + for (i = 0; i < len; i++) + { + if (isalpha(buf[i])) + { + if (aflag) + { + a[alen++] = buf[i]; + } + else if (bflag) + { + b[blen++] = buf[i]; + } + else if (cflag) + { + c[clen++] = buf[i]; + } + } + else if (buf[i] == '+') + { + a[alen] = '\0'; + aflag = 0; + bflag = 1; + cflag = 0; + } + else if (buf[i] == '=') + { + b[blen] = '\0'; + aflag = 0; + bflag = 0; + cflag = 1; + } + } + c[clen] = '\0'; +} + +int is_correct(char *a, char *b, char *c) +{ + int v1, v2, v3; + v1 = cal_value(a); + v2 = cal_value(b); + v3 = cal_value(c); + return v1 + v2 == v3; +} + +int cal_value(char *a) +{ + int len, i; + int value; + int v1, v2; + len = strlen(a); + value = 0; + for (i = 0; i < len - 1; i++) + { + v1 = map_(a[i]); + v2 = map_(a[i + 1]); + if (v1 < v2) + { + value -= v1; + } + else + { + value += v1; + } + } + value += map_(a[len - 1]); + return value; +} + +int map_(char c) +{ + int value; + switch (c) + { + case 'I': + value = 1; + break; + case 'V': + value = 5; + break; + case 'X': + value = 10; + break; + case 'L': + value = 50; + break; + case 'C': + value = 100; + break; + case 'D': + value = 500; + break; + case 'M': + value = 1000; + break; + } + return value; +} + +int cal_char_num(char *a, char *b, char *c) +{ + int alen, blen, clen; + int len = 0; + int i, j; + char ch; + alen = strlen(a); + for (i = 0; i < alen; i++) + { + ch = a[i]; + for (j = 0; j < len; j++) + { + if (temp[j] == ch) + { + break; + } + } + if (j == len) + { + temp[len++] = ch; + } + } + blen = strlen(b); + for (i = 0; i < blen; i++) + { + ch = b[i]; + for (j = 0; j < len; j++) + { + if (ch == temp[j]) + { + break; + } + } + if (j == len) + { + temp[len++] = ch; + } + } + clen = strlen(c); + for (i = 0; i < clen; i++) + { + ch = c[i]; + for (j = 0; j < len; j++) + { + if (ch == temp[j]) + { + break; + } + } + if (j == len) + { + temp[len++] = ch; + } + } + temp[len] = '\0'; + return len; +} + +void dfs(int cur, char *str) +{ + int i, j; + int a, b, c; + char tempa[BUF_LEN], tempb[BUF_LEN], tempc[BUF_LEN], s[BUF_LEN]; + if (cur >= dep) + { + memcpy(tempa, str, lena); + memcpy(tempb, str + lena, lenb); + memcpy(tempc, str + lena + lenb, lenc); + tempa[lena] = '\0'; + tempb[lenb] = '\0'; + tempc[lenc] = '\0'; + sscanf(tempa, "%d", &a); + sscanf(tempb, "%d", &b); + sscanf(tempc, "%d", &c); + if (a + b == c) + { + solnum++; + } + return; + } + for (i = 0; i < 10; i++) + { + if (!vis[i]) + { + strcpy(s, str); + for (j = 0; j < combuflen; j++) + { + if (s[j] == temp[cur]) + { + s[j] = i + '0'; + } + } + if ((lena > 1 && s[0] == '0') || (lenb > 1 && s[lena] == '0') || (lenc > 1 && s[lena + lenb] == '0')) + { + continue; + } + vis[i] = 1; + dfs(cur + 1, s); + vis[i] = 0; + } + } +} diff --git a/186.cpp b/186.cpp new file mode 100644 index 0000000..dc3d422 --- /dev/null +++ b/186.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define INF 500000000 +#define MAXN 501 + +char s[80]; +int n, c[MAXN][MAXN], Trace[MAXN][MAXN], namelen[MAXN]; +string route[MAXN][MAXN], name1, name2; +map idx; +vector name; + +void init() +{ + for (int i = 1; i < MAXN; i++) + { + for (int j = i + 1; j < MAXN; j++) + { + c[i][j] = c[j][i] = INF; + Trace[i][j] = j; + Trace[j][i] = i; + route[i][j] = route[j][i] = ""; + } + Trace[i][i] = i; + c[i][i] = 0; + } + name.clear(); + name.push_back(""); + idx.clear(); + n = 0; +} + +bool read_ln() +{ + int len, k = 0, w, u, v; + gets(s); + len = strlen(s); + if (len == 0) + { + return false; + } + string r = ""; + name1 = name2 = ""; + while (s[k] != ',') + { + name1 += s[k++]; + } + k++; + while (s[k] != ',') + { + name2 += s[k++]; + } + k++; + while (s[k] != ',') + { + r += s[k++]; + } + k++; + sscanf(s + k, "%d", &w); + u = idx[name1]; + if (u == 0) + { + u = idx[name1] = ++n; + name.push_back(name1); + namelen[n] = name[n].length(); + } + v = idx[name2]; + if (v == 0) + { + v = idx[name2] = ++n; + name.push_back(name2); + namelen[n] = name[n].length(); + } + if (w < c[u][v]) + { + c[u][v] = c[v][u] = w; + route[u][v] = route[v][u] = r; + } + return true; +} + +void floyd() +{ + for (int k = 1; k < n; k++) + { + for (int u = 1; u < n; u++) + { + for (int v = 1; v < n; v++) + { + if (c[u][v] > c[u][k] + c[k][v]) + { + c[u][v] = c[u][k] + c[k][v]; + Trace[u][v] = Trace[u][k]; + } + } + } + } +} + +void disp(int s, int f) +{ + printf("\n\n"); + printf("From To Route Miles\n"); + printf("-------------------- -------------------- ---------- -----\n"); + int u, v = s; + do + { + cout << name[v]; + for (int i = namelen[v]; i < 21; i++) + { + printf(" "); + } + u = Trace[v][f]; + cout << name[u]; + for (int i = namelen[u]; i < 21; i++) + { + printf(" "); + } + cout << route[u][v]; + for (int i = route[u][v].length(); i < 11; i++) + { + printf(" "); + } + printf("%5d\n", c[u][v]); + v = u; + } while (v != f); + printf(" -----\n"); + printf(" Total "); + printf("%5d\n", c[s][f]); +} + +int main() +{ + init(); + while (read_ln()) + ; + floyd(); + while (gets(s)) + { + name1 = name2 = ""; + int len = strlen(s), k = 0, u, v; + while (s[k] != ',') + { + name1 += s[k++]; + } + k++; + while (k < len) + { + name2 += s[k++]; + } + u = idx[name1]; + v = idx[name2]; + disp(u, v); + } + return 0; +} diff --git a/187.cpp b/187.cpp new file mode 100644 index 0000000..2f42e78 --- /dev/null +++ b/187.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int read_account_number() +{ + char c; + int result = 0; + for (int i = 0; i < 3; i++) + { + cin >> c; + result = result * 10 + (c - '0'); + } + return result; +} + +int main() +{ + int account_number, old_transaction_number = -1, transaction_number, amount_in_dollars; + string account_name; + map accounts; + while (1) + { + account_number = read_account_number(); + getline(cin, account_name); + if (account_number == 0) + { + break; + } + accounts[account_number] = account_name; + } + // keep the input order of transactions + vector> payments; + // verifying balance + int balance = 0; + while (true) + { + transaction_number = read_account_number(); + account_number = read_account_number(); + cin >> amount_in_dollars; + // account_name is used as a filler to finish off the line + getline(cin, account_name); + if (old_transaction_number == -1 || transaction_number == old_transaction_number) + { + payments.push_back(make_pair(account_number, amount_in_dollars)); + balance += amount_in_dollars; + } + else + { + // transaction sequence number has changed, time to print a report + // if the balance is anything other than zero + if (balance != 0) + { + printf("*** Transaction %03d is out of balance ***\n", old_transaction_number); + for (int i = 0, sz = payments.size(); i < sz; i++) + { + printf("%03d ", payments[i].first); + printf("%-30s ", accounts[payments[i].first].c_str()); + printf("%10.2f\n", payments[i].second / 100.0); + } + balance *= -1; + printf("999 Out of Balance %10.2f\n", balance / 100.0); + cout << endl; + } + payments.clear(); + balance = 0; + payments.push_back(make_pair(account_number, amount_in_dollars)); + balance += amount_in_dollars; + } + old_transaction_number = transaction_number; + if (transaction_number == 0) + { + break; + } + } + return 0; +} diff --git a/188.cpp b/188.cpp new file mode 100644 index 0000000..4a0e01c --- /dev/null +++ b/188.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define MIN(a, b) (a > b ? b : a) + +int W[15], tw; +char str[200]; + +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +int Val(char ss[]) +{ + int i, j, k = 1, d = 0, l; + l = strlen(ss); + for (i = l - 1; i >= 0; i--) + { + j = ss[i] - 'a' + 1; + d += j * k; + k *= 32; + } + return d; +} + +void Set() +{ + char *p; + tw = 0; + p = strtok(str, " "); + while (p) + { + W[tw++] = Val(p); + p = strtok(NULL, " "); + } +} + +int Match(int n) +{ + int i, j, x, y; + for (i = 0; i + 1 < tw; i++) + { + for (j = i + 1; j < tw; j++) + { + x = n / W[i]; + y = n / W[j]; + x = x % tw; + y = y % tw; + if (x == y) + { + return 1; + } + } + } + return 0; +} + +int Next(int n) +{ + int i, j, max = -1000000, x, p, q; + for (i = 0; i + 1 < tw; i++) + for (j = i + 1; j < tw; j++) + { + p = (n / W[i]) % tw; + q = (n / W[j]) % tw; + if (q != p) + continue; + x = MIN(((n / W[i]) + 1) * W[i], ((n / W[j]) + 1) * W[j]); + if (x > max) + max = x; + } + return max; +} +void Cal() +{ + int min, ok; + qsort(W, tw, sizeof(int), com); + min = W[0]; + ok = Match(min); + while (ok) + { + min = Next(min); + ok = Match(min); + } + printf("%d\n\n", min); +} + +int main() +{ + while (gets(str)) + { + puts(str); + Set(); + Cal(); + } + return 0; +} diff --git a/189.cpp b/189.cpp new file mode 100644 index 0000000..6e99c52 --- /dev/null +++ b/189.cpp @@ -0,0 +1,200 @@ +#include + +using namespace std; + +int main() +{ + int result = 0, ch = getchar(); + while (1) + { + if (ch == EOF) + { + break; + } + if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || + ch == ';' || ch == '.' || ch == ')' || ch == ']' || ch == ',') + { + ch = getchar(); + continue; + } + if (ch == '{') + { + do + { + ch = getchar(); + } while (ch != '}'); + ch = getchar(); + continue; + } + if (ch == '(') + { + ch = getchar(); + if (ch != '*') + { + result++;//operator ( + continue; + } + char prev = getchar(); + ch = getchar(); + while (!(prev == '*' && ch == ')')) + { + prev = ch; + ch = getchar(); + } + ch = getchar(); + continue; + } + if (ch == '\'')//string.. + { + result++; + int state = 0; + while (1) + { + ch = getchar(); + if (state == 0) + { + if (ch == '\'') + { + state = 1; + } + } + else + { + if (ch == '\'') + { + state = 0; + } + else + { + break; + } + } + } + continue; + /* + int prev=getchar(); + ch=getchar(); + if (prev=='\'') continue; //empty string + while (1) + { + if (prev=='\'' && ch=='\'') + ch=0; + else if (prev=='\'' && ch!='\'') + break; + prev=ch; + ch=getchar(); + } + continue; + */ + // printf("<>\n"); + } + if (ch == '_' || isalpha(ch)) + { + //var,ident + result++; + // printf("<>\n"); + while (ch == '_' || isalpha(ch) || isdigit(ch)) + { + ch = getchar(); + } + } + else if (isdigit(ch)) + { + //dec const.. + result++; + // printf("<>\n"); + while (isdigit(ch)) + { + ch = getchar(); + } + /* + if (ch=='.') ch=getchar(); + else continue; + while (isdigit(ch)) + ch=getchar(); + */ + if (ch == '.') + { + ch = getchar(); + while (isdigit(ch)) + { + ch = getchar(); + } + } + if (ch == 'E' || ch == 'e') + { + ch = getchar(); + } + else + { + continue; + } + if (ch == '+' || ch == '-') + { + ch = getchar(); + } + // else while (1); //debug..yes continue; + while (isdigit(ch)) + { + ch = getchar(); + } + } + else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || + ch == '=' || ch == '<' || ch == '>' || ch == '@' || ch == '^' || + ch == '[') + { + char t = ch; + result++; + // printf("<>\n"); + ch = getchar(); + switch (t) + { + case '<': + if (ch == '=' || ch == '>') + { + ch = getchar(); + } + break; + case '>': + if (ch == '=') + { + ch = getchar(); + } + break; + } + } + else if (ch == ':') + { + ch = getchar(); + if (ch == '=') + { + result++; + // printf("<<:=>>\n"); + ch = getchar(); + } + } + else if (ch == '$') + { + //hex const + result++; + // printf("<>\n"); + do + { + ch = getchar(); + } while (isxdigit(ch)); + } + else if (ch == '~') + { + ch = getchar(); + if (ch == '~') + { + char name[80]; + gets(name); + printf("Program by %s contains %d units.\n", name, result); + result = 0; + ch = getchar(); + } + } + } + return 0; +} diff --git a/190.cpp b/190.cpp new file mode 100644 index 0000000..5b7dff8 --- /dev/null +++ b/190.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +double g, f, c, A, B, C, R, E, F, G, K, M; +double x1, y1_, x2, y2, x3, y3; + +void CENTER() +{ + A = 2 * (x1 - x2); + B = 2 * (y1_ - y2); + C = (x2 * x2) + (y2 * y2) - (x1 * x1) - (y1_ * y1_); + E = 2 * (x2 - x3); + F = 2 * (y2 - y3); + G = (x3 * x3) + (y3 * y3) - (x2 * x2) - (y2 * y2); + g = (C * F - G * B) / (A * F - E * B); + f = (C * E - G * A) / (B * E - F * A); + c = -(x2 * x2 + y2 * y2 + 2 * g * x2 + 2 * f * y2); + K = 2 * g; + M = 2 * f; +} + +void RADIUS() +{ + char p, q, r, s; + R = sqrt(((x1 - (-g)) * (x1 - (-g))) + ((y1_ - (-f)) * (y1_ - (-f)))); + if (g < 0) + { + p = '-'; + } + else + { + p = '+'; + } + if (f < 0) + { + q = '-'; + } + else + { + q = '+'; + } + g = fabs(g); + f = fabs(f); + printf("(x %c %.3lf)^2 + (y %c %.3lf)^2 = %.3lf^2\n", p, g, q, f, R); + if (c < 0) + { + p = '-'; + } + else + { + p = '+'; + } + c = fabs(c); + if (K < 0) + { + r = '-'; + } + else + { + r = '+'; + } + K = fabs(K); + if (M < 0) + { + s = '-'; + } + else + { + s = '+'; + } + M = fabs(M); + printf("x^2 + y^2 %c %.3lfx %c %.3lfy %c %.3lf = 0\n", r, K, s, M, p, c); +} + +int main() +{ + while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1_, &x2, &y2, &x3, &y3) == 6) + { + CENTER(); + RADIUS(); + printf("\n"); + } + return 0; +} diff --git a/191.cpp b/191.cpp new file mode 100644 index 0000000..48b3b92 --- /dev/null +++ b/191.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +int main() +{ + int n, t; + double xstart, ystart, xend, yend, xleft, ytop, xright, ybottom, k; + cin >> n; + for (t = 0; t < n; t++) + { + cin >> xstart >> ystart >> xend >> yend >> xleft >> ytop >> xright >> ybottom; + double ybigger, ysmaller, xbigger, xsmaller, tmp; + if (ytop < ybottom) + { + tmp = ytop; + ytop = ybottom; + ybottom = tmp; + } + if (xright < xleft) + { + tmp = xright; + xright = xleft; + xleft = tmp; + } + ybigger = ystart > yend ? ystart : yend; + ysmaller = ystart < yend ? ystart : yend; + xbigger = xstart > xend ? xstart : xend; + xsmaller = xstart < xend ? xstart : xend; + if (xstart == xend && ystart == yend)//when it is an dot; + { + if (xstart == xleft && ystart <= ytop && ystart >= ybottom) + { + cout << 'T' << endl; + continue; + } + if (xstart == xright && ystart <= ytop && ystart >= ybottom) + { + cout << 'T' << endl; + continue; + } + if (ystart == ytop && xstart >= xleft && xstart <= xright) + { + cout << 'T' << endl; + continue; + } + if (ystart == ybottom && xstart >= xleft && xstart <= xright) + { + cout << 'T' << endl; + continue; + } + cout << 'F' << endl; + continue; + } + if (xstart == xend && ystart != yend)//k==infinite + { + if (xstart >= xleft && xstart <= xright) + { + if (ybigger > ytop && ysmaller > ytop) + { + cout << 'F' << endl; + continue; + } + if (ybigger < ybottom && ysmaller < ybottom) + { + cout << 'F' << endl; + continue; + } + } + cout << 'T' << endl; + continue; + } + if (ystart == yend && xstart != xend)//k==0 + { + if (ystart >= ybottom && ystart <= ytop) + { + if (xbigger > xright && xsmaller > xright) + { + cout << 'F' << endl; + continue; + } + if (xbigger < xleft && xsmaller < xleft) + { + cout << 'F' << endl; + continue; + } + } + cout << 'T' << endl; + continue; + } + if (xstart <= xright && xstart >= xleft && ystart <= ytop && ystart >= ybottom) + { + cout << 'T' << endl; + continue; + } + if (xend <= xright && xend >= xleft && yend <= ytop && yend >= ybottom) + { + cout << 'T' << endl; + continue; + } + //k>0&&k= ybottom && tmp <= ytop && xsmaller <= xleft && xbigger >= xleft) + { + cout << 'T' << endl; + continue; + } + tmp = k * (xright - xstart) + ystart; + if (tmp >= ybottom && tmp <= ytop && xsmaller <= xright && xbigger >= xright) + { + cout << 'T' << endl; + continue; + } + tmp = (ytop - ystart) / k + xstart; + if (tmp >= xleft && tmp <= xright && ysmaller <= ytop && ybigger >= ytop) + { + cout << 'T' << endl; + continue; + } + tmp = (ybottom - ystart) / k + xstart; + if (tmp >= xleft && tmp <= xright && ysmaller <= ybottom && ybigger >= ybottom) + { + cout << 'T' << endl; + continue; + } + cout << 'F' << endl; + } + return 0; +} diff --git a/192.cpp b/192.cpp new file mode 100644 index 0000000..185ae9c --- /dev/null +++ b/192.cpp @@ -0,0 +1,175 @@ +#include + +using namespace std; + +typedef struct +{ + bool isSynch; + int delay; + int incomingConnections; + bool removed; + int totalDelay; +} NODE; +typedef struct +{ + int from; + int to; +} CONNECTION; +void read_circuit(); +void delete_circuit(); +void analyze_circuit(); +void propagate_delay(int node); +NODE *sNodes; +int sNumNodes; +CONNECTION *sConnections; +int sNumConnections; +int sClockPeriod; +int sMaxDelay; + +int main() +{ + int numCircuits; + int i; + scanf("%d", &numCircuits); + for (i = 0; i < numCircuits; i++) + { + read_circuit(); + analyze_circuit(); + delete_circuit(); + } + return 0; +} + +void read_circuit() +{ + char type[20]; + int i; + /* read the clock period */ + scanf("%d", &sClockPeriod); + /* read the number of nodes */ + scanf("%d", &sNumNodes); + /* allocate the nodes */ + sNodes = (NODE *)malloc(sNumNodes * sizeof(NODE)); + for (i = 0; i < sNumNodes; i++) + { + /* read the type and delay of the node */ + scanf("%s %d", type, &sNodes[i].delay); + sNodes[i].isSynch = (type[0] != 'a'); + sNodes[i].totalDelay = 0; + sNodes[i].incomingConnections = 0; + sNodes[i].removed = false; + } + /* read the number of connections */ + scanf("%d", &sNumConnections); + /* allocate the connections */ + sConnections = (CONNECTION *)malloc(sNumConnections * sizeof(CONNECTION)); + for (i = 0; i < sNumConnections; i++) + { + scanf("%d %d", &sConnections[i].from, &sConnections[i].to); + } +} + +void delete_circuit() +{ + free(sNodes); + free(sConnections); +} + +void analyze_circuit() +{ + /* search for cycles in the circuits */ + /* Strategy: We count the number of incomingConnections connections for every node. + Then we remove all nodes without incomingConnections connections together with their + outgoing connections until no nodes are left (no cycles in the circuit) or + no further nodes can be removed (cycle in the circuit). + Synchronously working nodes always break a cycle and therefore don't count. */ + bool nodeRemoved; + int numRemovedNodes; + int i, j; + /* count number of incomingConnections connections */ + for (i = 0; i < sNumConnections; i++) + { + int to = sConnections[i].to; + if (!sNodes[to].isSynch) + { + sNodes[to].incomingConnections++; + } + } + nodeRemoved = true; + numRemovedNodes = 0; + while (nodeRemoved) /* while a further node has been removed do ... */ + { + nodeRemoved = false; + for (i = 0; i < sNumNodes; i++) /* loop over all nodes */ + { + if (!sNodes[i].removed && sNodes[i].incomingConnections == 0) + { + /* node has no incomingConnections connections => remove it */ + nodeRemoved = true; + numRemovedNodes++; + /* remove nodes */ + sNodes[i].removed = true; + /* remove outgoing connections */ + for (j = 0; j < sNumConnections; j++) + { + int to = sConnections[j].to; + if (sConnections[j].from == i && !sNodes[to].isSynch) + { + sNodes[to].incomingConnections--; + } + } + } + } + } + /* if all nodes have been removed, there are no cycles */ + if (numRemovedNodes != sNumNodes) + { + printf("Circuit contains cycle.\n"); + return; + } + /* calculate delays */ + /* Strategy: We start at all inputs and propagate recursively the delays until + we reach an output or a synchronous node. The outputs of synchronous nodes + are also considered as inputs. This algorithm will terminate because we + know there are no cycles. */ + sMaxDelay = 0; + for (i = 0; i < sNumNodes; i++) + if (sNodes[i].isSynch) /* start propagating the delays */ + { + propagate_delay(i); + } + if (sMaxDelay > sClockPeriod) + { + printf("Clock period exceeded.\n"); + } + else + { + printf("Synchronous design. Maximum delay: %d.\n", sMaxDelay); + } +} + +void propagate_delay(int i) +{ + int j; + /* for all connections that lead from the current node to another one do ... */ + for (j = 0; j < sNumConnections; j++) + if (sConnections[j].from == i) + { + /* calculate new delay and take the maximum of the new and old value */ + int to = sConnections[j].to; + int newDelay = sNodes[i].totalDelay + sNodes[to].delay; + if (newDelay > sNodes[to].totalDelay) + { + sNodes[to].totalDelay = newDelay; + if (newDelay > sMaxDelay) + { + sMaxDelay = newDelay; + } + /* recursively propagte the new delay */ + if (!sNodes[to].isSynch) + { + propagate_delay(to); + } + } + } +} diff --git a/193.cpp b/193.cpp new file mode 100644 index 0000000..0135f35 --- /dev/null +++ b/193.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +int maximum = 0, colored = 0, vertices; +vector best, taken; + +struct Vertex +{ + int id, color; + vector adj; + Vertex(int id) : id(id) + { + color = -1; + } +}; +typedef map vmap; +typedef pair vpair; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(int v); + void addEdge(int begin, int end, bool directed); + Vertex *firstVertex(); + void applyColors(int n); + + Vertex *getVertex(int v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(int v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(int begin, int end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(int v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::applyColors(int n) +{ + if (n == allVertexes.size()) + { + if (colored > maximum) + { + maximum = colored; + best.clear(); + for (int i = 0; i < taken.size(); i++) + { + best.push_back(taken[i]); + } + } + } + else + { + allVertexes[n]->color = 0; + colored += 1; + taken.push_back(allVertexes[n]->id); + bool good = true; + for (int i = 0; i < allVertexes[n]->adj.size(); i++) + if (allVertexes[n]->adj[i]->color == 0) + { + good = false; + break; + } + if (good) + { + applyColors(n + 1); + } + colored -= 1; + taken.pop_back(); + allVertexes[n]->color = 1; + applyColors(n + 1); + } +} + +int main() +{ + int n, v, e, a, b; + cin >> n; + while (n--) + { + best.clear(); + taken.clear(); + maximum = 0; + colored = 0; + Graph *g = new Graph(); + cin >> v >> e; + vertices = v; + for (int i = 1; i <= v; i++) + { + g->addVertex(i); + } + while (e--) + { + cin >> a >> b; + g->addEdge(a, b); + } + g->applyColors(0); + cout << maximum << endl; + for (int i = 0; i < best.size(); i++) + { + if (i > 0) + { + cout << " "; + } + cout << best[i]; + } + cout << endl; + delete g; + } + return 0; +} diff --git a/194.cpp b/194.cpp new file mode 100644 index 0000000..dba07be --- /dev/null +++ b/194.cpp @@ -0,0 +1,406 @@ +#include + +using namespace std; + +const double cEps = 0.000001f, cUndefined = -1.0f; + +typedef enum { + errOk, + errInvalidInput, + errManySolutions +} ErrorState; + +typedef double *Params; + +bool IsDef(double value); +bool CheckParams(Params p); +bool ValidTriangle(Params p); +bool IsState(char *s, Params p); +void Rule_SumOfAngles(Params p, int alpha, int beta, int gamma, ErrorState *err); +void Rule_Sine_Side(Params p, int a, int b, int alpha, int beta, ErrorState *err); +void Rule_Sine_Angle(Params p, int alpha, int a, int beta, int b, ErrorState *err); +void Rule_Cosine_Side(Params p, int a, int b, int alpha, int c, ErrorState *err); +void Rule_Cosine_Angle(Params p, int alpha, int a, int b, int c, ErrorState *err); +bool DetectRule(Params p, ErrorState *err); +void CalculateTriangle(Params p, ErrorState *err); + +bool IsDef(double value) +{ + return value != cUndefined; +} + +bool CheckParams(Params p) +{ + int i; + for (i = 0; i < 3; i++) + { + // check side + if (IsDef(p[i * 2])) + if (p[i * 2] <= 0.0) + { + return false; + } + // check angle + if (IsDef(p[i * 2 + 1])) + if (p[i * 2 + 1] <= 0.0 || p[i * 2 + 1] >= M_PI) + { + return false; + } + } + return true; +} + +bool ValidTriangle(Params p) +{ + double q; + if (!CheckParams(p)) + { + return false; + } + if (fabs(p[1] + p[3] + p[5] - M_PI) >= cEps) + { + return false; + } + if (p[0] + p[2] <= p[4] || p[2] + p[4] <= p[0] || p[4] + p[0] <= p[2]) + { + return false; + } + q = sin(p[3]) / p[0]; + if (fabs(sin(p[5]) / p[2] - q) >= cEps) + { + return false; + } + if (fabs(sin(p[1]) / p[4] - q) >= cEps) + { + return false; + } + return true; +} + +bool IsState(char *s, Params p) +{ + // s: "x" = already defined + // "0" = to be calculated + // "-" = don't care + int i; + for (i = 0; i < 6; i++) + { + if (s[i] == 'x' && !IsDef(p[i])) + { + return false; // rule cannot be applied + } + if (s[i] == '0' && IsDef(p[i])) + { + return false; // parameter already defined + } + } + return true; +} + +void Rule_SumOfAngles(Params p, int alpha, int beta, int gamma, ErrorState *err) +{ + // Rule: alpha + beta + gamma = Pi + // To compute: alpha ( = Pi - beta - gamma ) + // Defined: beta, gamma + assert((alpha & 1) == 1); + assert((beta & 1) == 1); + assert((gamma & 1) == 1); + assert(alpha != beta); + assert(beta != gamma); + p[alpha] = M_PI - p[beta] - p[gamma]; +} + +void Rule_Sine_Angle(Params p, int alpha, int beta, int a, int b, ErrorState *err) +{ + // Rule: a / sin(alpha) = b / sin(beta) = c / sin(gamma) + // To compute: alpha ( = arcsin( a * sin(beta) / b ) ) + // Situation: Angle-Side-Side or Side-Side-Angle + // Defined: beta, a, b + double q; + assert((a & 1) == 0); + assert((b & 1) == 0); + assert((alpha & 1) == 1); + assert((beta & 1) == 1); + assert(a != b); + assert(alpha != beta); + if (p[b] > 0.0) + { + q = p[a] * sin(p[beta]) / p[b]; + if (fabs(q - 1.0) < cEps) // special case: single solution! + { + p[alpha] = M_PI / 2.0; + return; + } + if (q < 1.0) + { + if (p[b] < p[a]) // two solutions + { + *err = errManySolutions; + return; + } + // alpha must be smaller than 90 deg here, + // so we don't have to check for a second solution + p[alpha] = asin(q); + return; + } + } + *err = errInvalidInput; // no solution +} + +void Rule_Sine_Side(Params p, int a, int b, int alpha, int beta, ErrorState *err) +{ + // Rule: a / sin(alpha) = b / sin(beta) = c / sin(gamma) + // To compute: a ( = sin(alpha) * b / sin(beta) ) + // Situation: Angle-Angle-Side or Side-Angle-Angle + // Defined: alpha, b, beta + double q, m; + assert((a & 1) == 0); + assert((b & 1) == 0); + assert((alpha & 1) == 1); + assert((beta & 1) == 1); + assert(a != b); + assert(alpha != beta); + if (sin(p[beta]) != 0.0) + { + q = sin(p[alpha]) / sin(p[beta]); + m = p[b] * sin(p[alpha]); + if (q != 0.0) + { + p[a] = p[b] * q; + return; + } + else if (m != 0.0) + { + p[a] = m / sin(p[beta]); + return; + } + } + *err = errInvalidInput; +} + +void Rule_Cosine_Side(Params p, int a, int b, int alpha, int c, ErrorState *err) +{ + // Rule: a^2 = b^2 + c^2 - 2 * b * c * cos(alpha) + // To compute: a + // Defined: b, c, alpha + // Situation: Side-Angle-Side + double sqra; + assert((a & 1) == 0); + assert((b & 1) == 0); + assert((alpha & 1) == 1); + assert((c & 1) == 0); + assert(a != b); + assert(b != c); + // Summing up the squares of number can be tricky, + // but we have only three numbers and use doubles + // That should be enough precision + sqra = -2 * p[b] * p[c] * cos(p[alpha]) + p[b] * p[b] + p[c] * p[c]; + if (sqra > 0.0) + { + p[a] = sqrt(sqra); + return; + } + *err = errInvalidInput; +} + +void Rule_Cosine_Angle(Params p, int alpha, int a, int b, int c, ErrorState *err) +{ + // rule: a^2 = b^2 + c^2 - 2 * b * c * cos(alpha) + // to compute: alpha + // defined: a, b, c + // situation: side-side-side + double d, cosalpha; + assert((alpha & 1) == 1); + assert((a & 1) == 0); + assert((b & 1) == 0); + assert((c & 1) == 0); + assert(a != b); + assert(b != c); + d = 2.0 * p[b] * p[c]; + if (d != 0.0) + { + cosalpha = (-p[a] * p[a] + p[b] * p[b] + p[c] * p[c]) / d; + if (fabs(cosalpha) <= 1.0) + { + p[alpha] = acos(cosalpha); + return; + } + } + *err = errInvalidInput; +} + +bool DetectRule(Params p, ErrorState *err) +{ + // format of pattern: a, gamma, b, alpha, c, beta + // angle - side - side and side - side - angle + // this rule may only be applied if exactly three parameters are defined + // otherwise it could turn valid cases into "more than one solution" cases + if (IsState("x00xx0", p)) + { + Rule_Sine_Angle(p, 1, 3, 4, 0, err); + return true; + } + if (IsState("x0x00x", p)) + { + Rule_Sine_Angle(p, 3, 5, 0, 2, err); + return true; + } + if (IsState("0xx0x0", p)) + { + Rule_Sine_Angle(p, 5, 1, 2, 4, err); + return true; + } + if (IsState("00x0xx", p)) + { + Rule_Sine_Angle(p, 1, 5, 4, 2, err); + return true; + } + if (IsState("xx00x0", p)) + { + Rule_Sine_Angle(p, 3, 1, 0, 4, err); + return true; + } + if (IsState("x0xx00", p)) + { + Rule_Sine_Angle(p, 5, 3, 2, 0, err); + return true; + } + // side - side - side + if (IsState("x-x0x-", p)) + { + Rule_Cosine_Angle(p, 3, 0, 2, 4, err); + return true; + } + if (IsState("x-x-x0", p)) + { + Rule_Cosine_Angle(p, 5, 2, 4, 0, err); + return true; + } + if (IsState("x0x-x-", p)) + { + Rule_Cosine_Angle(p, 1, 4, 0, 2, err); + return true; + } + // side - angle - side + if (IsState("0-xxx-", p)) + { + Rule_Cosine_Side(p, 0, 2, 3, 4, err); + return true; + } + if (IsState("x-0-xx", p)) + { + Rule_Cosine_Side(p, 2, 4, 5, 0, err); + return true; + } + if (IsState("xxx-0-", p)) + { + Rule_Cosine_Side(p, 4, 0, 1, 2, err); + return true; + } + // side - angle - angle + angle - angle - side + if (IsState("0-xx-x", p)) + { + Rule_Sine_Side(p, 0, 2, 3, 5, err); + return true; + } + if (IsState("-x0-xx", p)) + { + Rule_Sine_Side(p, 2, 4, 5, 1, err); + return true; + } + if (IsState("xx-x0-", p)) + { + Rule_Sine_Side(p, 4, 0, 1, 3, err); + return true; + } + if (IsState("0x-xx-", p)) + { + Rule_Sine_Side(p, 0, 4, 3, 1, err); + return true; + } + if (IsState("x-0x-x", p)) + { + Rule_Sine_Side(p, 2, 0, 5, 3, err); + return true; + } + if (IsState("-xx-0x", p)) + { + Rule_Sine_Side(p, 4, 2, 1, 5, err); + return true; + } + // replace angle - side - angle rule + if (IsState("-0-x-x", p)) + { + Rule_SumOfAngles(p, 1, 3, 5, err); + return true; + } + if (IsState("-x-0-x", p)) + { + Rule_SumOfAngles(p, 3, 5, 1, err); + return true; + } + if (IsState("-x-x-0", p)) + { + Rule_SumOfAngles(p, 5, 1, 3, err); + return true; + } + return false; +} + +void CalculateTriangle(Params p, ErrorState *err) +{ + bool changed = true; + *err = errOk; + // valid parameters on first sight? + if (!CheckParams(p)) + { + *err = errInvalidInput; + } + while (changed && *err == errOk) + { + changed = DetectRule(p, err); + } + // have all parameters been calculated? + if (*err == errOk && !IsState("xxxxxx", p)) + { + *err = errInvalidInput; + } + // valid triangle? + if (*err == errOk && !ValidTriangle(p)) + { + *err = errInvalidInput; + } +} + +int main() +{ + int nTriangles, i, dummy; + ErrorState err; + double _p[6]; + Params p = _p; + scanf("%d", &nTriangles); + for (i = 0; i < nTriangles; i++) + { + // input: a, alpha, b, beta, c, gamma + // internal rep: a, gamma, b, alpha, c, beta + scanf("%lf %lf %lf %lf %lf %lf", + &p[0], &p[3], &p[2], &p[5], &p[4], &p[1]); + CalculateTriangle(p, &err); + if (err == errOk) + { + // internal rep: a, gamma, b, alpha, c, beta + // output: a, alpha, b, beta, c, gamma + printf("%0.6lf %0.6lf %0.6lf %0.6lf %0.6lf %0.6lf\n", + p[0], p[3], p[2], p[5], p[4], p[1]); + } + else if (err == errInvalidInput) + { + printf("Invalid input.\n"); + } + else if (err == errManySolutions) + { + printf("More than one solution.\n"); + } + } + return 0; +} diff --git a/195.cpp b/195.cpp index 73176b5..95e08d1 100644 --- a/195.cpp +++ b/195.cpp @@ -1,51 +1,97 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -bool comp(char A, char B){ - char sa = tolower(A); - char sb = tolower(B); - if( sa == sb ) - return A < B; - return sa < sb; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int test; - scanf("%d", &test); - while( test-- ){ - char str[10000]; - scanf("%s", str); - int N = strlen(str); - sort(str, str+N, comp); - do{ - printf("%s\n", str); - }while( next_permutation(str, str+N, comp) ); - } - return 0; -} +#include + +using namespace std; + +char ALPHA[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; +int POS[200]; +int VALUE[200]; +char STR[500]; + +int sort_function(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +void INTERCHANGE(int m, int n) +{ + int c; + c = VALUE[m]; + VALUE[m] = VALUE[n]; + VALUE[n] = c; +} + +void GETVALUE() +{ + int i, j; + for (i = 0; STR[i]; i++) + { + j = STR[i]; + VALUE[i] = POS[j]; + } +} + +void FIXED() +{ + int i, j; + for (i = 0; ALPHA[i]; i++) + { + j = ALPHA[i]; + POS[j] = i; + } +} + +int main() +{ + int i, n, c, m, p, N, M, kase, j, Q; + FIXED(); + scanf("%d", &kase); + while (kase--) + { + scanf("%s", STR); + GETVALUE(); + n = strlen(STR) - 1; + qsort((void *)VALUE, n + 1, sizeof(int), sort_function); + for (i = 0; STR[i]; i++) + { + Q = VALUE[i]; + printf("%c", ALPHA[Q]); + } + printf("\n"); + while (1) + { + c = 0; + for (i = n; i >= 1; i--) + { + if (VALUE[i] > VALUE[i - 1]) + { + c = 1; + break; + } + } + if (c == 0) + { + break; + } + m = i - 1; + for (i = n; VALUE[i] <= VALUE[m]; i--) + ; + p = i; + INTERCHANGE(m, p); + N = n; + M = m + 1; + while (N > M) + { + INTERCHANGE(N, M); + N--; + M++; + } + for (j = 0; STR[j]; j++) + { + Q = VALUE[j]; + printf("%c", ALPHA[Q]); + } + printf("\n"); + } + } + return 0; +} diff --git a/196.cpp b/196.cpp new file mode 100644 index 0000000..b80a94b --- /dev/null +++ b/196.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +struct xx +{ + char *col; +}; +struct ss +{ + xx *cc; +}; +ss *S; +int R, C; + +int Srow(char dummy[]) +{ + int i, j, k = 0, m = 1; + int l = strlen(dummy); + for (i = l - 1; i >= 0; i--) + { + if (dummy[i] == 'a') + break; + j = dummy[i] - 'A' + 1; + k += j * m; + m *= 26; + } + return k; +} +int Recur(int r, int c) +{ + int val = 0, i, j, k, tf = 0, nr, nc; + char Formula[100][20], *p; + char dum[10]; + if (S[r].cc[c].col[0] != '=') + return atoi(S[r].cc[c].col); + p = strtok(S[r].cc[c].col, "+"); + while (p) + { + strcpy(Formula[tf++], p); + p = strtok(NULL, "+"); + } + for (i = 0; i < tf; i++) + { + Formula[0][0] = 'a'; + for (j = 0; Formula[i][j] && isalpha(Formula[i][j]); j++) + { + dum[j] = Formula[i][j]; + } + dum[j] = '\0'; + nc = Srow(dum); + k = 0; + for (j; Formula[i][j] && isdigit(Formula[i][j]); j++) + { + dum[k++] = Formula[i][j]; + } + dum[k] = '\0'; + nr = atoi(dum); + val += Recur(nr, nc); + } + sprintf(S[r].cc[c].col, "%d", val); + return val; +} +void Cal() +{ + int i, j; + for (i = 1; i <= R; i++) + { + for (j = 1; j <= C; j++) + { + if (S[i].cc[j].col[0] != '=') + printf("%s", S[i].cc[j].col); + else + printf("%d", Recur(i, j)); + if (j < C) + printf(" "); + } + printf("\n"); + } + delete[] S; +} +char ttm[1000005]; + +int main() +{ + int ks, i, j, l; + scanf("%d", &ks); + while (ks--) + { + scanf("%d%d", &C, &R); + S = new ss[R + 1]; + for (i = 1; i <= R; i++) + { + S[i].cc = new xx[C + 1]; + for (j = 1; j <= C; j++) + { + scanf("%s", ttm); + l = strlen(ttm); + S[i].cc[j].col = new char[l + 4]; + strcpy(S[i].cc[j].col, ttm); + } + } + Cal(); + } + return 0; +} diff --git a/197.cpp b/197.cpp new file mode 100644 index 0000000..7f07508 --- /dev/null +++ b/197.cpp @@ -0,0 +1,276 @@ +#include + +using namespace std; + +struct Partial +{ + char a[28]; + int used; + bool operator<(const Partial &c) const + { + return memcmp(a, c.a, 27) < 0; + } + bool operator==(const Partial &c) const + { + return memcmp(a, c.a, 27) == 0; + } + void print() + { + a[27] = 0; + printf("%s\n", a); + } + void fix_used() + { + used = 0; + for (int i = 0; i < 27; i++) + { + if (a[i] != 0) + { + used |= 1 << i; + } + } + } +}; + +struct Figure +{ + Partial *c; + int n; +} figure[7]; + +vector solutions; + +void doit(Partial *cur, int fig) +{ + if (fig == 7) + { + solutions.push_back(*cur); + return; + } + Partial *p = figure[fig].c, next; + for (int i = figure[fig].n; i-- > 0; p++) + { + if ((cur->used & p->used) == 0) + { + for (int j = 0; j < 27; j++) + { + next.a[j] = cur->a[j] | p->a[j]; + } + next.used = cur->used | p->used; + doit(&next, fig + 1); + } + } +} + +char RX[] = {7, 8, 9, 16, 17, 18, 25, 26, 27, 4, 5, 6, 13, 14, 15, 22, 23, 24, 1, 2, 3, 10, 11, 12, 19, 20, 21}; +char RY[] = {7, 4, 1, 8, 5, 2, 9, 6, 3, 16, 13, 10, 17, 14, 11, 18, 15, 12, 25, 22, 19, 26, 23, 20, 27, 24, 21}; +char RZ[] = {3, 12, 21, 6, 15, 24, 9, 18, 27, 2, 11, 20, 5, 14, 23, 8, 17, 26, 1, 10, 19, 4, 13, 22, 7, 16, 25}; +char RA[64][32]; + +void rot(char *s, char *tab) +{ + char t[32]; + for (int i = 0; i < 27; i++) + { + t[tab[i] - 1] = s[i]; + } + for (int i = 0; i < 27; i++) + { + s[i] = t[i]; + } +} + +void make_rots() +{ + char c[28]; + for (int i = 0; i < 27; i++) + { + c[i] = i + 1; + } + int k = 0; + for (int x = 0; x < 4; x++, rot(c, RX)) + { + for (int y = 0; y < 4; y++, rot(c, RY)) + { + for (int z = 0; z < 4; z++, rot(c, RZ)) + { + memcpy(RA[k++], c, 27); + for (int i = 0; i < k - 1; i++) + if (memcmp(RA[i], RA[k - 1], 27) == 0) + { + k--; + break; + } + } + } + } + assert(k == 24); +} + +bool trans(char s[], int dx, int dy, int dz) +{ + char t[28]; + for (int i = 0; i < 27; i++) + { + t[i] = 0; + } + for (int x = 0; x < 3; x++) + { + for (int y = 0; y < 3; y++) + { + for (int z = 0; z < 3; z++) + { + int c = s[9 * y + 3 * z + x]; + if (c != 0) + { + if (x + dx < 0 || x + dx > 2) + { + return false; + } + if (y + dy < 0 || y + dy > 2) + { + return false; + } + if (z + dz < 0 || z + dz > 2) + { + return false; + } + t[9 * (y + dy) + 3 * (z + dz) + (x + dx)] = c; + } + } + } + } + for (int i = 0; i < 27; i++) + { + s[i] = t[i]; + } + return true; +} + +void tr_canonize(char s[]) +{ + while (trans(s, -1, 0, 0)) + ; + while (trans(s, 0, -1, 0)) + ; + while (trans(s, 0, 0, -1)) + ; +} + +int main() +{ + make_rots(); + for (int fig = 'b'; fig <= 'g'; fig++) + { + vector res; + set S; + for (int r = 0; r < 24; r++) + { + for (int dx = -3; dx <= 3; dx++) + { + for (int dy = -3; dy <= 3; dy++) + { + for (int dz = -3; dz <= 3; dz++) + { + Partial c; + const char *pieces = "adcaccaacddgbfgffedggbfebee"; + for (int i = 0; i < 27; i++) + { + c.a[i] = (pieces[i] == fig) ? fig : 0; + } + rot(c.a, RA[r]); + if (!trans(c.a, dx, dy, dz)) + { + continue; + } + if (S.count(c) == 0) + { + c.fix_used(); + res.push_back(c); + S.insert(c); + } + } + } + } + } + Figure &F = figure[fig - 'a']; + F.n = res.size(); + F.c = new Partial[F.n]; + for (int i = 0; i < F.n; i++) + { + F.c[i] = res[i]; + } + } + Partial Z; + for (int i = 0; i < 27; i++) + { + Z.a[i] = ("aa.a..a...................."[i] == 'a') ? 'a' : 0; + } + tr_canonize(Z.a); + for (int dx = -3; dx <= 3; dx++) + { + for (int dy = -3; dy <= 3; dy++) + { + for (int dz = -3; dz <= 3; dz++) + { + Partial Y = Z; + if (!trans(Y.a, dx, dy, dz)) + { + continue; + } + Y.fix_used(); + doit(&Y, 1); + } + } + } + for (;;) + { + Partial Q; + for (int i = 0; i < 27; i++) + { + int c; + while ((c = getchar()) != EOF && c != '.' && c != 'a') + ; + if (c == EOF) + { + return 0; + } + Q.a[i] = (c == 'a') ? 'a' : 0; + } + int r; + for (r = 0; r < 24; r++) + { + Partial C = Q; + rot(C.a, RA[r]); + tr_canonize(C.a); + if (C == Z) + { + break; + } + } + if (r >= 25) + { + printf("\n"); + continue; + } + int rev; + for (rev = 0; rev < 24; rev++) + { + Partial C = Q; + rot(C.a, RA[r]); + rot(C.a, RA[rev]); + if (C == Q) + { + break; + } + } + for (int i = 0; i < (int)solutions.size(); i++) + { + Partial C = solutions[i]; + rot(C.a, RA[rev]); + C.print(); + } + printf("\n"); + } + return 0; +} diff --git a/198.cpp b/198.cpp new file mode 100644 index 0000000..4de5327 --- /dev/null +++ b/198.cpp @@ -0,0 +1,398 @@ +#include + +using namespace std; + +string order[100]; +string var[100]; +bool used[100]; +bool undef; +int loc1; + +void printvar(const int &num); +int getvalue(string varname); +int getexp(string exp); + +void printvar(const int &num) +{ + for (int i = 0; i < 100; i++) + { + used[i] = false; + } + undef = false; + int result; + result = getvalue(var[num]); + if (undef) + { + cout << "UNDEF" << endl; + return; + } + cout << result << endl; + return; +} +int getvalue(string varname) +{ + while (true) + { + if (varname[0] == ' ') + { + varname = varname.substr(1); + } + else + { + break; + } + } + while (true) + { + if (varname[varname.size() - 1] == ' ') + { + varname = varname.substr(0, varname.size() - 1); + } + else + { + break; + } + } + bool num = true; + for (int i = 0; i < varname.size(); i++) + { + if (varname[i] >= '0' && varname[i] <= '9') + { + continue; + } + else + { + num = false; + break; + } + } + if (num) + { + int number = 0; + for (int i = 0; i < varname.size(); i++) + { + number *= 10; + number += varname[i] - '0'; + } + return number; + } + int locvar = 101; + for (int i = 0; i < loc1; ++i) + if (varname == var[i]) + { + locvar = i; + break; + } + if (locvar == 101) + { + undef = true; + return 0; + } + else + { + if (used[locvar] == true) + { + undef = true; + return 0; + } + else + { + used[locvar] = true; + int result = getexp(order[locvar]); + used[locvar] = false; + return result; + } + } +} +int getexp(string exp) +{ + while (true) + { + if (exp.size() > 0 && exp[0] == ' ') + { + exp = exp.substr(1); + } + else + { + break; + } + } + while (true) + { + if (exp.size() > 0 && exp[exp.size() - 1] == ' ') + { + exp = exp.substr(0, exp.size() - 1); + } + else + { + break; + } + } + if (exp == "") + { + return 0; + } + int sit; + int zkh[100], ykh[100]; + int khzs = 0, khjs = 0; + bool khcz; + khcz = false; + for (int i = 0; i < exp.size(); i++) + { + if (exp[i] == '(') + { + khcz = true; + khjs++; + if (khjs == 1) + { + zkh[khzs] = i; + } + } + else if (exp[i] == ')') + { + if (khjs == 1) + { + ykh[khzs] = i; + khzs++; + } + khjs--; + } + } + sit = exp.size(); + while ((sit = exp.rfind('+', sit - 1)) > 0) + { + int check = sit - 1; + bool valid = true; + for (; check >= 0; check--) + { + if (exp[check] == '+' || exp[check] == '-' || exp[check] == '*') + { + valid = false; + break; + } + else if (exp[check] == ' ') + { + continue; + } + else + { + break; + } + } + if (valid) + { + if (!khcz) + { + int result = getexp(exp.substr(0, sit)) + getexp(exp.substr(sit + 1)); + return result; + } + else + { + bool ok = true; + for (int i = 0; i < khzs; i++) + if (sit > zkh[i] && sit < ykh[i]) + { + ok = false; + break; + } + if (ok) + { + int result = getexp(exp.substr(0, sit)) + getexp(exp.substr(sit + 1)); + return result; + } + } + } + } + sit = exp.size(); + while ((sit = exp.rfind('-', sit - 1)) > 0) + { + int check = sit - 1; + bool valid = true; + for (; check >= 0; check--) + { + if (exp[check] == '+' || exp[check] == '-' || exp[check] == '*') + { + valid = false; + break; + } + else if (exp[check] == ' ') + { + continue; + } + else + { + break; + } + } + if (valid) + { + if (!khcz) + { + int result = getexp(exp.substr(0, sit)) - getexp(exp.substr(sit + 1)); + return result; + } + else + { + bool ok = true; + for (int i = 0; i < khzs; i++) + if (sit > zkh[i] && sit < ykh[i]) + { + ok = false; + break; + } + if (ok) + { + int result = getexp(exp.substr(0, sit)) - getexp(exp.substr(sit + 1)); + return result; + } + } + } + } + sit = exp.size(); + while ((sit = exp.rfind('*', sit - 1)) > 0) + { + if (!khcz) + { + int result = getexp(exp.substr(0, sit)) * getexp(exp.substr(sit + 1)); + return result; + } + else + { + bool ok = true; + for (int i = 0; i < khzs; i++) + if (sit > zkh[i] && sit < ykh[i]) + { + ok = false; + break; + } + if (ok) + { + int result = getexp(exp.substr(0, sit)) * getexp(exp.substr(sit + 1)); + return result; + } + } + } + sit = exp.find('+'); + if (sit == 0) + { + int result = getexp(exp.substr(1)); + return result; + } + sit = exp.find('-'); + if (sit == 0) + { + int result = -1 * getexp(exp.substr(1)); + return result; + } + if (khcz) + { + int result = getexp(exp.substr(1, exp.size() - 2)); + return result; + } + int result = getvalue(exp); + return result; +} +int main() +{ + string now; + now = " "; + loc1 = 0; + while (getline(cin, now, '\n')) + { + while (true) + { + if (now.size() > 0 && now[0] == ' ') + { + now = now.substr(1); + } + else + { + break; + } + } + while (true) + { + if (now.size() > 0 && now[now.size() - 1] == ' ') + { + now = now.substr(0, now.size() - 1); + } + else + { + break; + } + } + if (int(now.find(':')) >= 0) + { + string temp = now.substr(0, int(now.find(':'))); + while (true) + { + if (temp.size() > 0 && temp[temp.size() - 1] == ' ') + { + temp = temp.substr(0, temp.size() - 1); + } + else + { + break; + } + } + int i = 101; + for (int j = 0; j < loc1; j++) + { + if (var[j] == temp) + { + i = j; + break; + } + } + if (i == 101) + { + var[loc1] = temp; + order[loc1] = now.substr(int(now.find('=')) + 1); + loc1++; + } + else + { + var[i] = temp; + order[i] = now.substr(int(now.find('=')) + 1); + } + } + else + { + if (int(now.find("PRINT")) >= 0) + { + string temp = now.substr(6); + while (true) + { + if (temp[0] == ' ') + { + temp = temp.substr(1); + } + else + { + break; + } + } + int i = 101; + for (int j = 0; j < loc1; j++) + if (var[j] == temp) + { + i = j; + break; + } + if (i == 101) + { + cout << "UNDEF" << endl; + } + else + { + printvar(i); + } + continue; + } + else if (int(now.find("RESET")) >= 0) + { + loc1 = 0; + } + } + } +} diff --git a/199.cpp b/199.cpp new file mode 100644 index 0000000..3587665 --- /dev/null +++ b/199.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int main() +{ + int m, n, v[3][3], g[3][3], b[4][31], f[31][31], *a = new int[841 * 841]; + for (cin >> m; --m >= 0;) + { + cin >> n; + int r[841] = {0}, nSize = (n - 1) * (n - 1), h2 = n * n; + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j<3; cin>> v[i][j++]) + ; + } + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j<3; cin>> g[i][j++]) + ; + } + for (int i = 0; i < 4; ++i) + { + for (int j = 0; j> b[i][j++]) + ; + } + for (int i = 0; i < n + 1; ++i) + { + for (int j = 0; j> f[i][j++]) + ; + } + for (int i = 1; i < n; ++i) + { + for (int j = 1; j < n; ++j) + { + int id = (i - 1) * (n - 1) + j - 1; + for (int s = 0; s < 3; ++s) + { + for (int t = 0; t < 3; ++t) + { + int as = i + s - 1, at = j + t - 1; + r[id] += f[as][at] * g[s][t]; + if (as == 0) + { + r[id] -= b[1][at] * v[s][t] * h2; + } + else if (at == 0) + { + r[id] -= b[2][n - as] * v[s][t] * h2; + } + else if (as == n) + { + r[id] -= b[0][at] * v[s][t] * h2; + } + else if (at == n) + { + r[id] -= b[3][n - as] * v[s][t] * h2; + } + else + { + int aid = (as - 1) * (n - 1) + at - 1; + a[id * nSize + aid] = v[s][t] * h2; + } + } + } + } + } + for (int i = 0; i < nSize; ++i) + { + for (int j = 0; j < nSize - 1; ++j) + { + cout << a[i * nSize + j] << ' '; + } + cout << a[i * nSize + nSize - 1] << endl; + } + for (int i = 0; i < nSize - 1; ++i) + { + cout << r[i] << ' '; + } + cout << r[nSize - 1] << endl; + } + delete[] a; + return 0; +} diff --git a/200.cpp b/200.cpp new file mode 100644 index 0000000..e85cbb4 --- /dev/null +++ b/200.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define MAXN 30 +#define MAX 100000 + +char STR[MAX][MAXN]; +char LIST[MAX][MAXN]; +int TIME; +struct vertex +{ + char color; + int Ftime; +} VERTEX[MAXN]; +struct node +{ + int ft; + node *next; +}; +node *lst; + +void ADD(int m, int n) +{ + int i, j, k, en; + j = STR[m][0] - 65; + k = STR[n][0] - 65; + LIST[j][k] = 1; + VERTEX[j].color = 'w'; + VERTEX[k].color = 'w'; + k = strlen(STR[m]); + j = strlen(STR[n]); + if (k > j) + { + en = j; + } + else + { + en = k; + } + for (i = 1; i < en; i++) + { + if (STR[m][i - 1] == STR[n][i - 1]) + { + j = STR[m][i] - 65; + k = STR[n][i] - 65; + LIST[j][k] = 1; + VERTEX[j].color = 'w'; + VERTEX[k].color = 'w'; + k = strlen(STR[m]); + j = strlen(STR[n]); + } + else + { + break; + } + } +} + +void DoDFS(int n) +{ + VERTEX[n].color = 'g'; + TIME++; + for (int j = 0; j < 27; j++) + { + if (LIST[n][j]) + { + if (VERTEX[j].color == 'w') + { + DoDFS(j); + } + } + } + node *p = (node *)malloc(sizeof(node)); + p->ft = n; + p->next = lst; + lst = p; + VERTEX[n].color = 'b'; +} + +void PRINT() +{ + int j; + node *x = lst; + while (x != NULL) + { + j = x->ft + 65; + printf("%c", j); + x = x->next; + } +} + +void DFS() +{ + int i; + for (i = 0; i < 28; i++) + { + VERTEX[i].Ftime = 0; + } + TIME = 0; + for (i = 0; i < 28; i++) + { + if (VERTEX[i].color == 'w') + { + DoDFS(i); + } + } +} + +int main() +{ + int k = 0, i; + while (1) + { + scanf("%s", STR[k]); + if (!strcmp(STR[k], "#")) + { + break; + } + k++; + } + for (i = 0; i < k - 1; i++) + { + ADD(i, i + 1); + } + DFS(); + PRINT(); + putchar('\n'); + return 0; +} diff --git a/201.cpp b/201.cpp new file mode 100644 index 0000000..50a521b --- /dev/null +++ b/201.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ + +void check(const int &i, const int &j, const int &num, int square[]); +bool h[100][100]; +bool v[100][100]; +int main() +{ + int num; + int count = 1; + while (scanf("%d", &num) == 1) + { + int square[num - 1]; + memset(square, 0, sizeof(square)); + memset(v, 0, sizeof(v)); + memset(h, 0, sizeof(h)); + int line; + scanf("%d", &line); + char temp; + int d1, d2; + for (int i = 0; i < line; i++) + { + cin >> temp; + scanf("%d%d", &d1, &d2); + if (temp == 'V') + { + v[d2 - 1][d1 - 1] = true; + } + else if (temp == 'H') + { + h[d1 - 1][d2 - 1] = true; + } + } + for (int i = 0; i < num - 1; i++) + for (int j = 0; j < num - 1; j++) + { + check(i, j, num, square); + } + if (count > 1) + { + printf("\n**********************************\n\n"); + } + printf("Problem #%d\n\n", count); + count++; + bool have = false; + for (int i = 0; i < num - 1; i++) + if (square[i] != 0) + { + have = true; + } + if (!have) + { + printf("No completed squares can be found.\n"); + } + else + { + for (int i = 0; i < num - 1; i++) + { + if (square[i] != 0) + { + printf("%d square (s) of size %d\n", square[i], i + 1); + } + } + } + } +} +void check(const int &i, const int &j, const int &num, int square[]) +{ + int maxp = max(i, j); + int maxs = num - 1 - maxp; + for (int size = 1; size <= maxs; size++) + { + bool ok = true; + for (int a = 0; ok && a < size; a++) + if (!v[i + a][j] || !v[i + a][j + size] || !h[i][j + a] || !h[i + size][j + a]) + { + ok = false; + } + if (ok) + { + square[size - 1]++; + } + } + return; +} diff --git a/202.cpp b/202.cpp new file mode 100644 index 0000000..f5818ce --- /dev/null +++ b/202.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ + +vector di; +int main() +{ + unsigned int digits = 0; + unsigned int fenzi = 1, fenmu = 1; + while (cin >> fenzi >> fenmu) + { + di.clear(); + int *p = new int[fenmu]; + cout << fenzi << "/" << fenmu << " = " << fenzi / fenmu; + cout << "."; + fenzi = fenzi % fenmu; + for (int i = 0; i < fenmu; i++) + { + fenzi *= 10; + *(p + i) = fenzi; + if (i > 0 && fenzi != 0) + { + for (int j = 0; j < i; j++) + { + if (*(p + j) == *(p + i)) + { + digits = i - j; + break; + } + } + } + if (fenzi == 0) + { + for (int i = 0; i < di.size(); i++) + { + cout << di[i]; + } + cout << "(0)" << endl; + cout << " 1 = number of digits in repeating cycle" << endl + << endl; + break; + } + if (digits != 0) + { + int loc = di.size() - digits; + for (int i = 0; i < loc; i++) + { + cout << di[i]; + } + cout << "("; + for (int i = loc; i < di.size() && i < loc + 50; i++) + { + cout << di[i]; + } + if (digits > 50) + { + cout << "..."; + } + cout << ")" << endl; + cout << " " << digits << " = number of digits in repeating cycle" << endl + << endl; + break; + } + di.push_back(fenzi / fenmu); + fenzi = fenzi % fenmu; + } + digits = 0; + delete[] p; + } + return 0; +} diff --git a/205.cpp b/205.cpp new file mode 100644 index 0000000..6bc58f5 --- /dev/null +++ b/205.cpp @@ -0,0 +1,275 @@ +#include + +using namespace std; + +/* + * 205 getting there.cpp + * + * Created on: Apr 22, 2010 + * Author: 122513 + */ + +int timebetween(int starttime, int endtime) +{ + return (starttime <= endtime) ? endtime - starttime : 1440 - starttime + endtime; +} + +int timeminutes(int hour, char ap, int minutes) +{ + return hour * 60 + ((ap == 'P') ? 720 : 0) + minutes; +} + +char *initcap(char *p) +{ + p[0] = toupper(p[0]); + for (int i = 1; p[i]; ++i) + { + p[i] = tolower(p[i]); + } + return p; +} + +struct info +{ + int starttime, endtime, totaltime, to; + double cost; + + info(int starttime, int endtime, double cost, int to) : starttime(starttime), endtime(endtime), to(to), cost(cost) + + { + totaltime = timebetween(starttime, endtime); + } +}; + +//Variables globales +int mintime, maxto; +double mincost; + +vector minroute; +map> cities; +int ntravel, ncity, citya, cityb, cases = 0; +char buffer[200]; +string stringtmp, stringtmp2; + +map citiestoint; +map inttocities; + +//mode : 0 = cost, 1 = time +void minimize(int starttime, int to, vector used, int totaltime, + double totalcost, vector routes, bool mode) +{ + int tmptotaltime; + double tmptotalcost; + used[to] = 1; + vector from = cities[to]; + vector tmproutes; + if (to == maxto) + { + if (routes.size() < minroute.size() && (fabs(totalcost - mincost) < 1e-9 && totaltime == mintime)) + { + mintime = totaltime; + mincost = totalcost; + minroute = routes; + } + else if (mode) + { + if (totaltime < mintime || (totaltime == mintime && totalcost < mincost)) + { + mintime = totaltime; + mincost = totalcost; + minroute = routes; + } + } + else if (totalcost < mincost || (fabs(totalcost - mincost) < 1e-9 && totaltime < mintime)) + { + mintime = totaltime; + mincost = totalcost; + minroute = routes; + } + return; + } + for (unsigned int i = 0; i < from.size(); i++) + { + if (!used[from[i].to]) + { + tmptotaltime = totaltime + timebetween(starttime, from[i].starttime) + from[i].totaltime; + tmptotalcost = totalcost + from[i].cost; + tmproutes = routes; + tmproutes.push_back(from[i]); + if (mode) + { + if (tmptotaltime <= mintime) + { + minimize(from[i].endtime, from[i].to, used, tmptotaltime, + tmptotalcost, tmproutes, mode); + } + } + else + { + if (tmptotalcost < mincost || fabs(tmptotalcost - mincost) < 1e-9) + { + minimize(from[i].endtime, from[i].to, used, tmptotaltime, + tmptotalcost, tmproutes, mode); + } + } + } + } +} + +bool readandstore() +{ + int r; + //lectura + r = scanf("%17s", buffer); + if (buffer[0] == '#') + { + return 0; + } + initcap(buffer); + stringtmp = string(buffer); + if (citiestoint.count(stringtmp) == 0) + { + inttocities[ncity] = stringtmp; + citiestoint[stringtmp] = ncity++; + } + citya = citiestoint[stringtmp]; + r = scanf("%16s", buffer); + initcap(buffer); + stringtmp = string(buffer); + if (citiestoint.count(stringtmp) == 0) + { + inttocities[ncity] = stringtmp; + citiestoint[stringtmp] = ncity++; + } + cityb = citiestoint[stringtmp]; + //Horarios + int h0, h1, m0, m1; + char ctmp, ap0, ap1; + double cost; + r = scanf("%d%c%d%c%d%c%d%c%lf", &h0, &ctmp, &m0, &ap0, &h1, &ctmp, &m1, &ap1, + &cost); + cities[citya].push_back(info(timeminutes(h0, ap0, m0), timeminutes(h1, ap1, + m1), + cost, cityb)); + return 1; +} + +int main(int argc, char **argv) +{ + int r; + //cout << "leido" << endl; + while (scanf("%s %d", buffer, &ntravel) != EOF) + { + //cout << "leido" << endl; + citiestoint.clear(); + cities.clear(); + inttocities.clear(); + ncity = 1; + while (readandstore()) + ; + if (cases++) + { + printf("\n\n"); + } + printf("Requests and optimal routes for travel %d\n", ntravel); + printf("------------------------------------------\n"); + //Leer todos los request + while (1) + { + r = scanf("%s", buffer); + if (buffer[0] == '#') + { + break; + } + initcap(buffer); + stringtmp = string(buffer); + citya = citiestoint[stringtmp]; + r = scanf("%s", buffer); + initcap(buffer); + stringtmp2 = string(buffer); + cityb = citiestoint[stringtmp2]; + //modo + r = scanf("%s", buffer); + if (stringtmp == stringtmp2) + { + printf("\nYou are already in %s.\n", stringtmp.c_str()); + } + else if (citya == 0 || cityb == 0) + { + printf("\nThere is no route from %s to %s.\n", + stringtmp.c_str(), stringtmp2.c_str()); + } + else + { + mintime = 1000000; + mincost = 1000000; + minroute.clear(); + maxto = cityb; + vector used(ncity, 0); + used[citya] = 1; + vector tmproutes = cities[citya]; + for (unsigned int i = 0; i < tmproutes.size(); ++i) + { + minimize(tmproutes[i].endtime, tmproutes[i].to, used, + tmproutes[i].totaltime, tmproutes[i].cost, + vector(1, tmproutes[i]), buffer[0] == 'T'); + } + //Imprimir resultado + if (!minroute.size()) + { + printf("\nThere is no route from %s to %s.\n", + stringtmp.c_str(), stringtmp2.c_str()); + } + else + { + printf("\nFrom: %-20s To: %-20s", + inttocities[citya].c_str(), + inttocities[cityb].c_str()); + printf(" Optimize: %s\n", (buffer[0] == 'T') ? "Time" + : "Cost"); + printf( + "==================================================================\n"); + printf( + "From To Leave Arrive Cost\n"); + printf( + "==================================================================\n"); + for (unsigned int i = 0; i < minroute.size(); i++) + { + //Imprimir from to + printf("%-20s%-23s", inttocities[citya].c_str(), + inttocities[minroute[i].to].c_str()); + sprintf(buffer, "%d:%02d%c", (minroute[i].starttime / 60) % 12, minroute[i].starttime % 60, + ((minroute[i].starttime / 60) % 24 >= 12) ? 'P' : 'A'); + printf("%-8s", buffer); + sprintf(buffer, "%d:%02d%c", (minroute[i].endtime / 60) % 12, minroute[i].endtime % 60, + ((minroute[i].endtime / 60) % 24 >= 12) ? 'P' : 'A'); + printf("%-6s", buffer); + sprintf(buffer, "$%.2f", minroute[i].cost); + printf("%9s\n", buffer); + citya = minroute[i].to; + } + printf( + " -----------------------\n"); + string duracion; + if (mintime >= 2880) + { + sprintf(buffer, "%d days ", mintime / 1440); + duracion += buffer; + } + else if (mintime >= 1440) + { + sprintf(buffer, "%d day ", mintime / 1440); + duracion += buffer; + } + sprintf(buffer, "%d:%02d", (mintime % 1440) / 60, mintime % 60); + duracion += buffer; + /*}*/ + printf("%57s", duracion.c_str()); + sprintf(buffer, "$%.2f", mincost); + printf("%9s\n", buffer); + } + } + } + } + return 0; +} diff --git a/206.cpp b/206.cpp new file mode 100644 index 0000000..0ce87d7 --- /dev/null +++ b/206.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +//2003/8/8 a_q206.cpp +//2003/8/11 simple sort?! +//#include + +struct PointStruct +{ + char name[32]; + int x, y; + double theta; + bool operator<(const PointStruct &p) const + { + if (fabs(theta - p.theta) > 1e-7) + { + return theta < p.theta; + } + return x * x + y * y < p.x * p.x + p.y * p.y; + } + void computeTheta() + { + //assert(x!=0 || y!=0); //ok + theta = double(y) / double(abs(x) + abs(y)); + if (x < 0) + { + theta = 2 - theta; + } + else if (y < 0) + { + theta = theta + 4; + } + //printf("[%.0f]\n",theta*90.0); + } +}; + +PointStruct p[1024]; +int n, m; + +int main() +{ + int c = 0; + char line[128]; + while (gets(line)) + { + if (c++) + { + puts("***********************************"); + } + puts(line); + gets(line); + n = atoi(strtok(line, " \t")); + m = atoi(strtok(NULL, " \t")); + printf("Number of Customers: %-10d Number of Routes: %d\n\n", m, n); + //assert(n<1024 && m<1024); //ok + //assert(m>=n); //ok + for (int i = 0; i < m; i++) + { + gets(p[i].name); + gets(line); + p[i].x = atoi(strtok(line, " \t")); + p[i].y = atoi(strtok(NULL, " \t")); + p[i].computeTheta(); + } + sort(p, p + m); + /* + for (int i=0; i %d\n", i + 1); + int nowx = 0, nowy = 0, len = 0; + int j = (m - now) / (n - i); + if ((m - now) % (n - i) != 0) + { + j++; + } + while (--j >= 0) + { + len += abs(p[now].x - nowx); + len += abs(p[now].y - nowy); + nowx = p[now].x; + nowy = p[now].y; + printf("Customer: %s\n", p[now].name); + now++; + } + len += abs(nowx); + len += abs(nowy); + printf("Route Length ==> %d\n\n", len); + totalmin += len; + } + printf("Total Route Length ==> %d\n", totalmin); + } +} diff --git a/207.cpp b/207.cpp new file mode 100644 index 0000000..bebc2cb --- /dev/null +++ b/207.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +const int maxn = 145; + +int n, pos; +double sum, pri[maxn]; + +struct player +{ + char name[25]; + double prize; + bool amateur, t; + int sc[4], pre, tot, dq, rk; +} p[maxn]; + +bool cmp1(const player &a, const player &b) +{ + if (a.dq > -3 && b.dq > -3) + { + return a.pre < b.pre; + } + return a.dq > b.dq; +} + +bool cmp2(const player &a, const player &b) +{ + if (a.dq != b.dq) + { + return a.dq > b.dq; + } + if (a.tot != b.tot) + { + return a.tot < b.tot; + } + return strcmp(a.name, b.name) < 0; +} + +void init() +{ + memset(p, 0, sizeof(p)); + scanf("%lf", &sum); + for (int i = 0; i < 70; ++i) + { + scanf("%lf", &pri[i]); + pri[i] = pri[i] / 100.0 * sum; + } + scanf("%d\n", &n); + char s[40]; + for (int i = 0; i < n; ++i) + { + fgets(p[i].name, 20, stdin); + if (strchr(p[i].name, '*')) + { + p[i].amateur = true; + } + for (int j = 0; j < 4; ++j) + { + if (!scanf("%d", &p[i].sc[j])) + { + p[i].dq = j - 4; + break; + } + if (j < 2) + { + p[i].pre += p[i].sc[j]; + } + p[i].tot += p[i].sc[j]; + } + fgets(s, 40, stdin); + } + return; +} + +void solve() +{ + sort(p, p + n, cmp1); + pos = 0; + while (pos < min(n, 70) && p[pos].dq > -3) + { + ++pos; + } + while (p[pos].dq > -3 && p[pos].pre == p[pos - 1].pre) + { + ++pos; + } + sort(p, p + pos, cmp2); + int k = 0, rk = 0, prk = 0; + while (k < pos) + { + if (p[k].dq) + { + break; + } + int sta = k, prerk = rk + 1, cnt = 0; + bool x = false; + double sum = 0; + while (!p[sta].dq && p[k].tot == p[sta].tot) + { + if (!p[sta].amateur) + { + sum += pri[prk + cnt++], x = true; + } + ++rk, ++sta; + } + sum /= cnt; + for (int i = k; i <= sta; ++i) + { + p[i].rk = prerk; + if (prk > 69) + { + p[i].amateur = true, p[i].t = false; + } + if (!p[i].amateur) + { + p[i].prize = sum, p[i].t = cnt > 1; + } + } + k = sta - 1, k++; + prk += cnt; + } +} + +void disp() +{ + printf("Player Name Place RD1 RD2 RD3 RD4 TOTAL Money Won\n"); + printf("-----------------------------------------------------------------------\n"); + for (int i = 0; i < pos; ++i) + { + printf("%-21s", p[i].name); + if (p[i].dq) + printf(" "); + else + { + char t[5]; + sprintf(t, "%d%c", p[i].rk, p[i].t ? 'T' : ' '); + printf("%-10s", t); + } + for (int j = -4; j < p[i].dq; ++j) + printf("%-5d", p[i].sc[4 + j]); + for (int j = p[i].dq; j < 0; ++j) + printf(" "); + if (p[i].dq) + printf("%s", "DQ"); + else if (!p[i].amateur) + printf("%-10d", p[i].tot); + else + printf("%d", p[i].tot); + if (p[i].dq || p[i].amateur) + { + printf("\n"); + continue; + } + printf("$%9.2lf\n", p[i].prize); + } +} + +int main() +{ + int t; + scanf("%d", &t); + while (t--) + { + init(); + solve(); + disp(); + if (t) + { + printf("\n"); + } + } + return 0; +} diff --git a/208.cpp b/208.cpp new file mode 100644 index 0000000..892d1d2 --- /dev/null +++ b/208.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +/**************** +TITLE : FIRETRUCK + NO : 208 +****************/ +#define MAXN 22 +char F[MAXN]; +int P[MAXN]; +int CONECT[MAXN][MAXN]; +int MAXC, N, C, kase; +struct node +{ + char cl; +} VER[MAXN], VV[MAXN]; +void PRINT_PATH(int st, int en) +{ + if (st == en) + { + printf("%d", st); + return; + } + else + { + PRINT_PATH(st, P[en]); + printf(" %d", en); + } +} +int REACH(int n) +{ + if (n == N) + { + return 1; + } + VV[n].cl = 'r'; + for (int i = 1; i < MAXN; i++) + if (CONECT[n][i] && VV[i].cl == 'w' && !F[i]) + if (REACH(i) == 1) + { + return 1; + } +} +void DFS(int st, int en) +{ + int i; + if (st == N) + { + PRINT_PATH(1, N); + printf("\n"); + C++; + return; + } + F[st] = 1; + for (i = 1; i <= MAXC; i++) + { + if (F[i] == 0) + { + for (int j = 1; j < MAXN; j++) + { + VV[j].cl = 'w'; + } + if (CONECT[st][i] && (VER[i].cl == 'r' || REACH(i) == 1)) + { + P[i] = st; + VER[i].cl = 'r'; + DFS(i, en); + F[i] = 0; + } + } + } +} +void FRESH() +{ + int i, j; + for (i = 1; i < MAXN; i++) + { + F[i] = 0; + P[i] = 0; + VER[i].cl = 'w'; + VV[i].cl = 'w'; + for (j = 1; j < MAXN; j++) + { + CONECT[i][j] = 0; + } + } +} +void COMPUTE() +{ + printf("CASE %d:\n", kase++); + C = 0; + DFS(1, N); + printf("There are %d routes from the firestation to streetcorner %d.\n", C, N); +} +int main() +{ + int u, v; + kase = 1; + while (scanf("%d", &N) == 1) + { + FRESH(); + MAXC = 0; + while (1) + { + scanf("%d%d", &u, &v); + if (!u && !v) + { + break; + } + if (u > MAXC) + { + MAXC = u; + } + if (v > MAXC) + { + MAXC = v; + } + CONECT[u][v] = 1; + CONECT[v][u] = 1; + } + COMPUTE(); + } + return 0; +} diff --git a/209.cpp b/209.cpp new file mode 100644 index 0000000..1e89107 --- /dev/null +++ b/209.cpp @@ -0,0 +1,227 @@ +#include + +using namespace std; + +/* +209 +Triangular Vertices +*/ +char str[100]; +int P[10], N, Or[10]; +int S[260]; +struct ss +{ + int line; + int left; + int right; +} pp[10]; +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +void Ini() +{ + int i, sum = 0; + for (i = 1; sum < 32767; i++) + { + sum += i; + S[i] = sum; + } +} +int Line(int n) +{ + int i; + for (i = 1; i < 260; i++) + { + if (S[i] >= n) + { + break; + } + } + return i; +} +void Sep() +{ + char *p; + N = 0; + p = strtok(str, " "); + P[N] = Or[N] = atoi(p); + N++; + while (p) + { + p = strtok(NULL, " "); + if (p) + { + Or[N] = P[N] = atoi(p); + N++; + } + } +} +void Set() +{ + int i, ll, rl; + qsort(P, N, sizeof(int), com); + for (i = 0; i < N; i++) + { + rl = Line(P[i]); + ll = S[rl] - rl + 1; + pp[i].line = rl; + pp[i].right = P[i] - ll + 1; + pp[i].left = S[rl] - P[i] + 1; + } +} +int isTri() +{ + int dis; + if (pp[0].line == pp[1].line) + { + dis = P[1] - P[0]; + if ((pp[2].line - pp[1].line) != dis) + { + return 0; + } + if (pp[0].left != pp[2].left || pp[2].right != pp[1].right) + { + return 0; + } + } + else + { + if (pp[1].line != pp[2].line) + { + return 0; + } + dis = P[2] - P[1]; + if ((pp[1].line - pp[0].line) != dis) + { + return 0; + } + if (pp[0].right != pp[1].right || pp[0].left != pp[2].left) + { + return 0; + } + } + return 1; +} +int isPara() +{ + int dis; + if (pp[0].line != pp[1].line) + { + return 0; + } + if (pp[2].line != pp[3].line) + { + return 0; + } + dis = P[1] - P[0]; + if ((P[3] - P[2]) != dis || (pp[2].line - pp[0].line) != dis) + { + return 0; + } + if ((pp[3].line - pp[1].line) != dis) + { + return 0; + } + if (pp[0].left == pp[2].left && pp[1].left == pp[3].left) + { + return 1; + } + if (pp[0].right == pp[2].right && pp[1].right == pp[3].right) + { + return 1; + } + return 0; +} +int isPara2() +{ + int dis; + if (pp[1].line != pp[2].line) + { + return 0; + } + dis = P[2] - P[1]; + if ((pp[3].line - pp[1].line) != dis) + { + return 0; + } + if ((pp[1].line - pp[0].line) != dis) + { + return 0; + } + if (pp[0].right != pp[1].right || pp[0].left != pp[2].left) + { + return 0; + } + if (pp[1].left != pp[3].left || pp[2].right != pp[3].right) + { + return 0; + } + return 1; +} +int isHexa() +{ + int dis; + if (pp[0].line != pp[1].line || pp[2].line != pp[3].line) + { + return 0; + } + if (pp[4].line != pp[5].line) + { + return 0; + } + dis = P[1] - P[0]; + if ((pp[2].line - pp[0].line) != dis || (pp[4].line - pp[2].line) != dis) + { + return 0; + } + if ((pp[3].line - pp[1].line) != dis || (pp[5].line - pp[3].line) != dis) + { + return 0; + } + if (P[5] - P[4] != dis) + { + return 0; + } + if (pp[0].right == pp[2].right && pp[2].left == pp[4].left) + if (pp[5].right == pp[3].right && pp[3].left == pp[1].left) + { + return 1; + } + return 0; +} +void Cal() +{ + int i; + for (i = 0; i < N; i++) + { + printf("%d ", Or[i]); + } + if (N == 3 && isTri()) + { + printf("are the vertices of a triangle\n"); + } + else if (N == 4 && (isPara() || isPara2())) + { + printf("are the vertices of a parallelogram\n"); + } + else if (N == 6 && isHexa()) + { + printf("are the vertices of a hexagon\n"); + } + else + { + printf("are not the vertices of an acceptable figure\n"); + } +} +int main() +{ + Ini(); + while (gets(str)) + { + Sep(); + Set(); + Cal(); + } + return 0; +} diff --git a/210.cpp b/210.cpp new file mode 100644 index 0000000..6b1df3b --- /dev/null +++ b/210.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +struct Statement +{ + int op; + int var, num; + Statement() {} + Statement(int op_, int var_ = -1, int num_ = -1) + { + op = op_; + var = var_; + num = num_; + } +}; + +struct Program +{ + int id, cur; + vector code; +}; + +int N, quantum, cost[256], variable[26]; +Program *progs[100000]; +deque readyQ, blockQ; +Program *holder; + +void simulate() +{ + memset(variable, 0, sizeof(variable)); + holder = NULL; + readyQ.clear(); + blockQ.clear(); + for (int i = 1; i <= N; i++) + { + readyQ.push_back(progs[i]); + } + while (!readyQ.empty()) + { + Program *p = readyQ.front(); + readyQ.pop_front(); + int tm = quantum; + while (true) + { + if (tm <= 0) + { + readyQ.push_back(p); + break; + } + if (p->cur >= (int)p->code.size()) + { + break; + } + const Statement &s = p->code[p->cur++]; + tm -= cost[s.op]; + if (s.op == 'e') + { + break; + } + else if (s.op == 'p') + { + printf("%d: %d\n", p->id, variable[s.var]); + } + else if (s.op == '=') + { + variable[s.var] = s.num; + } + else if (s.op == 'l') + { + if (holder == NULL) + { + holder = p; + } + else + { + p->cur--; + blockQ.push_back(p); + tm = 0; + break; + } + } + else + { + assert(holder == p); + holder = NULL; + if (!blockQ.empty()) + { + readyQ.push_front(blockQ.front()); + blockQ.pop_front(); + } + } + } + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d", &N); + for (int i = 0; i < 5; i++) + { + scanf("%d", &cost["=plue"[i]]); + } + scanf("%d", &quantum); + for (int i = 1; i <= N; i++) + { + progs[i] = new Program(); + progs[i]->id = i; + progs[i]->cur = 0; + vector &code = progs[i]->code; + code.clear(); + while (true) + { + char s[100]; + int k = scanf(" %s", s); + assert(k == 1); + s[0] = tolower(s[0]); + if (strlen(s) > 1) + { + code.push_back(Statement(s[0])); + if (s[0] == 'p') + { + scanf(" %s", s); + code.back().var = tolower(s[0]) - 'a'; + } + } + else + { + code.push_back(Statement('=', s[0] - 'a')); + scanf(" = %d", &code.back().num); + } + if (code.back().op == 'e') + { + break; + } + } + } + simulate(); + for (int i = 1; i <= N; i++) + { + delete progs[i]; + } + if (cs < T) + { + printf("\n"); + } + } + return 0; +} diff --git a/211.cpp b/211.cpp new file mode 100644 index 0000000..2e5a6fd --- /dev/null +++ b/211.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +int bones[7][7]; +int pips[7][8], placed[7][8], used[29]; +int layout, sol; +void Print(int board[7][8], int d) +{ + int r, c; + for (r = 0; r < 7; r++) + { + if (d) + { + putchar(' '); + } + for (c = 0; c < 8; c++) + { + printf("%4d", board[r][c]); + } + putchar('\n'); + } + putchar('\n'); +} + +void Place(int depth) +{ + if (depth == 56) + { + int c = 0; + for (int i = 1; i < 29; i++) + if (used[i]) + { + c++; + } + if (c != 28) + { + return; + } + sol++; + Print(placed, 1); + return; + } + int b, r = depth / 8, c = depth % 8; + if (placed[r][c]) + { + return Place(depth + 1); + } + if (c < 7 && !placed[r][c + 1] && !used[bones[pips[r][c]][pips[r][c + 1]]]) + { + int d = bones[pips[r][c]][pips[r][c + 1]]; + used[d] = true; + placed[r][c] = placed[r][c + 1] = d; + Place(depth + 1); + used[d] = false; + placed[r][c] = placed[r][c + 1] = 0; + } + if (r < 6 && !placed[r + 1][c] && !used[bones[pips[r][c]][pips[r + 1][c]]]) + { + int d = bones[pips[r][c]][pips[r + 1][c]]; + used[d] = true; + placed[r][c] = placed[r + 1][c] = d; + Place(depth + 1); + used[d] = false; + placed[r][c] = placed[r + 1][c] = 0; + } + return; +} + +int main() +{ + int r, c, i, j, b; + for (b = 1, i = 0, j = 0; b < 29; b++) + { + bones[i][j] = bones[j][i] = b; + if (++i == 7) + { + i = ++j; + } + } + while (scanf("%d", &pips[0][0]) == 1) + { + if (layout++) + { + printf("\n\n\n"); + } + sol = 0; + for (r = 0; r < 7; r++) + { + for (c = 0; c < 8; c++) + { + if (r || c) + { + scanf("%d", &pips[r][c]); + } + placed[r][c] = 0; + } + } + for (i = 1; i <= 28; i++) + { + used[i] = 0; + } + printf("Layout #%d:\n\n", layout); + Print(pips, 0); + printf("Maps resulting from layout #%d are:\n\n", layout); + Place(0); + printf("There are %d solution(s) for layout #%d.\n", sol, layout); + } + return 0; +} diff --git a/212.cpp b/212.cpp new file mode 100644 index 0000000..cf6c58e --- /dev/null +++ b/212.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +int nrooms, nbeds, npatients, startTime; +int transportTime, roomCleanTime, bedCleanTime; + +struct Patient +{ + int id, time1, time2; + char name[20]; + int room, bed, roomStart; +} patients[1000]; + +struct Facility +{ + int avail; + int used; + int cur; +} rooms[50], beds[50]; + +enum +{ + BED_READY = 1, + ROOM_READY = 2, + LEAVE_BED = 3, + LEAVE_ROOM = 4 +}; + +struct Event +{ + int t, kind, x; + + Event(int t_, int kind_, int x_) : t(t_), kind(kind_), x(x_) {} + + bool operator<(const Event &e) const + { + if (t != e.t) + { + return t < e.t; + } + if (kind != e.kind) + { + return kind < e.kind; + } + return x < e.x; + } +}; + +int nextPatient, curTime, endTime, freeRooms; +set events; + +void simulate() +{ + for (int i = 0; i < nrooms; i++) + { + rooms[i].avail = 1; + rooms[i].used = 0; + } + freeRooms = nrooms; + for (int i = 0; i < nbeds; i++) + { + beds[i].avail = 1; + beds[i].used = 0; + } + nextPatient = 0; + events.clear(); + curTime = startTime; + endTime = startTime; + while (events.size() != 0 || nextPatient < npatients) + { + if (events.size() != 0 && events.begin()->t == curTime) + { + Event e = *events.begin(); + events.erase(events.begin()); + if (e.kind == LEAVE_ROOM) + { + int room = e.x; + Patient &p = patients[rooms[room].cur]; + for (p.bed = 0; p.bed < nbeds && beds[p.bed].avail == 0; p.bed++) + ; + assert(p.bed < nbeds); + beds[p.bed].avail = 0; + rooms[p.room].used += p.time1; + beds[p.bed].used += p.time2; + events.insert(Event(curTime + roomCleanTime, ROOM_READY, p.room)); + events.insert(Event(curTime + transportTime + p.time2, LEAVE_BED, p.id)); + } + else if (e.kind == ROOM_READY) + { + rooms[e.x].avail = 1; + freeRooms++; + } + else if (e.kind == LEAVE_BED) + { + Patient &p = patients[e.x]; + endTime = max(endTime, curTime); + events.insert(Event(curTime + bedCleanTime, BED_READY, p.bed)); + } + else if (e.kind == BED_READY) + { + beds[e.x].avail = 1; + } + continue; + } + if (freeRooms != 0 && nextPatient < npatients) + { + Patient &p = patients[nextPatient++]; + for (p.room = 0; p.room < nrooms && rooms[p.room].avail == 0; p.room++) + ; + assert(p.room < nrooms); + p.roomStart = curTime; + rooms[p.room].avail = 0; + rooms[p.room].cur = p.id; + freeRooms--; + events.insert(Event(curTime + p.time1, LEAVE_ROOM, p.room)); + continue; + } + assert(events.size() != 0); + curTime = events.begin()->t; + } +} + +char *fmt(int minutes) +{ + static char buf[100][100]; + static int z = 0; + z = (z + 1) % 100; + sprintf(buf[z], "%2d:%.2d", minutes / 60, minutes % 60); + return buf[z]; +} + +int main() +{ + while (cin >> nrooms >> nbeds >> startTime >> transportTime >> + roomCleanTime >> bedCleanTime >> npatients) + { + startTime *= 60; + for (int i = 0; i < npatients; i++) + { + Patient &p = patients[i]; + p.id = i; + cin >> p.name >> p.time1 >> p.time2; + } + simulate(); + printf(" Patient Operating Room Recovery Room\n"); + printf(" # Name Room# Begin End Bed# Begin End\n"); + printf(" ------------------------------------------------------\n"); + for (int i = 0; i < npatients; i++) + { + Patient &p = patients[i]; + printf("%2d %-8s %3d %s %s %3d %s %s\n", + i + 1, p.name, p.room + 1, + fmt(p.roomStart), fmt(p.roomStart + p.time1), + p.bed + 1, + fmt(p.roomStart + p.time1 + transportTime), + fmt(p.roomStart + p.time1 + transportTime + p.time2)); + } + printf("\n"); + printf("Facility Utilization\n"); + printf("Type # Minutes %% Used\n"); + printf("-------------------------\n"); + for (int i = 0; i < nrooms + nbeds; i++) + { + Facility &z = (i < nrooms ? rooms[i] : beds[i - nrooms]); + long long num = z.used; + long long den = endTime - startTime; + if (den == 0 || num > den) + { + num = 1; + den = 1; + } + int m = (num * 20000 + den) / (2 * den); + printf("%s %2d %7d %4d.%.2d\n", + (i < nrooms ? "Room" : "Bed "), + (i < nrooms ? (i + 1) : (i - nrooms + 1)), + z.used, m / 100, m % 100); + } + printf("\n"); + } +} diff --git a/213.cpp b/213.cpp new file mode 100644 index 0000000..b30af1b --- /dev/null +++ b/213.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +static vector all; + +int count(string::iterator start, string::iterator end, char c) +{ + int the_count = 0; + string::iterator it; + for (it = start; it < end; ++it) + if (*it == c) + { + the_count++; + } + return the_count; +} + +void makeall(int n, int now, string a) +{ + if (n == now) + { + if (!(count(a.begin(), a.end(), '1') == (int)a.size())) + { + all.push_back(a); + } + return; + } + makeall(n, now + 1, a + "0"); + makeall(n, now + 1, a + "1"); +} + +bool solve(map &m) +{ + char c; + int num = 0, mul = 4; + for (int i = 0; i < 3; i++) + { + cin >> c; + num += (c - '0') * mul; + mul /= 2; + } + if (num == 0) + { + return false; + } + while (true) + { + string tmp = ""; + for (int i = 0; i < num; i++) + { + cin >> c; + tmp += string(1, c); + } + if (m.count(tmp)) + { + cout << m[tmp]; + } + if (count(tmp.begin(), tmp.end(), '1') == num) + { + break; + } + } + return true; +} + +int main() +{ + for (int i = 1; i < 8; i++) + { + makeall(i, 0, ""); + } + string in; + while (getline(cin, in)) + { + map m; + for (int i = 0; i < (int)in.size(); i++) + { + m[all[i]] = in[i]; + } + while (solve(m)) + ; + cout << endl; + getline(cin, in); + } + return 0; +} diff --git a/214.cpp b/214.cpp new file mode 100644 index 0000000..2f96451 --- /dev/null +++ b/214.cpp @@ -0,0 +1,196 @@ +#include + +using namespace std; + +int stk[65536], top; +int use[256]; +char fmt[256][16]; + +int store() +{ + int i; + for (i = '0'; i <= '9'; i++) + if (use[i] == 0) + { + break; + } + use[i] = 1; + printf("ST $%c\n", i); + return i; +} + +void take(int k) +{ + int i; + top -= k; + for (i = 0; i < top; i++) + { + if (stk[i] == 0) + { + stk[i] = store(); + break; + } + } + for (i = 0; i < top; i++) + if (isalpha(stk[i])) + { + break; + } + if (i >= top) + { + return; + } + for (i = 0; i < k; i++) + { + if (stk[top + i] == 0) + { + stk[top + i] = store(); + break; + } + } + for (i = 0; i < top; i++) + { + if (isalpha(stk[i])) + { + printf("L %s\n", fmt[stk[i]]); + stk[i] = store(); + } + } +} + +void commut(char op) +{ + int x, y; + take(2); + x = stk[top]; + y = stk[top + 1]; + if (x == 0) + { + printf("%c %s\n", op, fmt[y]); + } + else if (y == 0) + { + printf("%c %s\n", op, fmt[x]); + } + else + { + printf("L %s\n%c %s\n", fmt[x], op, fmt[y]); + } + use[x] = 0; + use[y] = 0; + stk[top++] = 0; +} + +void sub() +{ + int x, y; + take(2); + x = stk[top]; + y = stk[top + 1]; + if (x == 0) + { + printf("S %s\n", fmt[y]); + } + else if (y == 0) + { + printf("N\nA %s\n", fmt[x]); + } + else + { + printf("L %s\nS %s\n", fmt[x], fmt[y]); + } + use[x] = 0; + use[y] = 0; + stk[top++] = 0; +} + +void div() +{ + int x, y; + take(2); + x = stk[top]; + y = stk[top + 1]; + if (x == 0) + { + printf("D %s\n", fmt[y]); + } + else if (y == 0) + { + /* reg <- x / reg */ + y = store(); + printf("L %s\nD %s\n", fmt[x], fmt[y]); + } + else + { + printf("L %s\nD %s\n", fmt[x], fmt[y]); + } + use[x] = 0; + use[y] = 0; + stk[top++] = 0; +} + +void neg() +{ + take(1); + if (stk[top] == 0) + { + printf("N\n"); + } + else + { + printf("L %s\nN\n", fmt[stk[top]]); + } + use[stk[top]] = 0; + stk[top++] = 0; +} + +int main() +{ + static char line[65536]; + char *p; + int i, t; + for (i = 0; i <= 255; i++) + { + sprintf(fmt[i], "%c", i); + } + for (i = '0'; i <= '9'; i++) + { + sprintf(fmt[i], "$%c", i); + } + for (t = 0; gets(line);) + { + if (t++ != 0) + { + printf("\n"); + } + memset(use, 0, sizeof(use)); + for (top = 0, p = line; *p; p++) + { + if (isalpha(*p)) + { + stk[top++] = *p; + } + else if (*p == '+' || *p == '*') + { + commut((*p == '+') ? 'A' : 'M'); + } + else if (*p == '-') + { + sub(); + } + else if (*p == '/') + { + div(); + } + else if (*p == '@') + { + neg(); + } + } + if (top > 0 && stk[top - 1] != 0) + { + printf("L %s\n", fmt[stk[top - 1]]); + } + } + return 0; +} diff --git a/215.cpp b/215.cpp new file mode 100644 index 0000000..402c912 --- /dev/null +++ b/215.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +const int R = 20; +const int C = 10; + +string ori[R][C]; + +int data[R][C]; + +bool isdecided[R][C]; + +int getdata(string &now, int &p, bool &canget) +{ + int tmp = 0; + if (isdigit(now[p]) || now[p] == '-') + { + int sign = 1; + if (now[p] == '-') + { + sign = -1; + } + while (p < now.size() && isdigit(now[p])) + { + tmp = tmp * 10 + now[p] - '0'; + p++; + } + tmp *= sign; + } + else + { + int rdata = now[p] - 'A', cdata = now[p + 1] - '0'; + p += 2; + if (isdecided[rdata][cdata]) + { + tmp = data[rdata][cdata]; + } + else + { + canget = false; + } + } + return tmp; +} + +bool parse(int r, int c) +{ + int p = 0; + string now = ori[r][c]; + bool canget = true; + int tmp = getdata(now, p, canget); + if (!canget) + { + return false; + } + while (p < now.size()) + { + canget = true; + if (now[p] == '+') + { + p++; + tmp += getdata(now, p, canget); + } + else + { + p++; + tmp -= getdata(now, p, canget); + } + if (!canget) + { + return false; + } + } + data[r][c] = tmp; + isdecided[r][c] = true; + return true; +} + +void solve(int r, int c) +{ + int total = r * c; + int k; + for (k = 0; k < total; k++) + { + bool isok = false; + rep(i, r && !isok) + { + rep(j, c && !isok) + { + if (!isdecided[i][j] && parse(i, j)) + { + isok = true; + break; + } + } + } + if (!isok) + { + break; + } + } + if (k == total) + { + putchar(' '); + rep(i, c) + { + printf("%6d", i); + } + printf("\n"); + rep(i, r) + { + cout << (char)('A' + i); + rep(j, c) + { + printf("%6d", data[i][j]); + } + cout << endl; + } + } + else + { + rep(i, r) + { + rep(j, c) + { + if (!isdecided[i][j]) + { + cout << (char)('A' + i) << j << ": " << ori[i][j] << endl; + } + } + } + } +} + +main() +{ + int r, c; + while (cin >> r >> c && r) + { + rep(i, r) + { + rep(j, c) + { + isdecided[i][j] = false; + cin >> ori[i][j]; + } + } + solve(r, c); + cout << endl; + } +} diff --git a/216.cpp b/216.cpp new file mode 100644 index 0000000..02dc96d --- /dev/null +++ b/216.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +/** +216 +**/ +#define MAXN 10 +double Dis[MAXN][MAXN]; +double Point[MAXN][2]; +double MAX, Temp; +int N, Kase; +int A[MAXN]; +char F[MAXN]; +char Record[MAXN]; +char as[] = "**********************************************************"; +double DD(int p, int q) +{ + double x, y, z; + x = pow((Point[p][0] - Point[q][0]), 2); + y = pow((Point[p][1] - Point[q][1]), 2); + z = sqrt((x + y)); + return z; +} +void Set_dis() +{ + int i, j; + double k; + for (i = 0; i < N - 1; i++) + for (j = i + 1; j < N; j++) + { + k = DD(i, j) + 16.0; + Dis[i][j] = Dis[j][i] = k; + } +} +void Recur(int level, int m) +{ + int x; + if (level >= 2) + { + x = A[level - 2]; + Temp += Dis[x][m]; + } + if (Temp >= MAX) + { + return; + } + if (level == N) + { + if (Temp < MAX) + { + MAX = Temp; + for (x = 0; x < N; x++) + { + Record[x] = A[x]; + } + } + return; + } + for (int i = 0; i < N; i++) + { + if (F[i] == 0) + { + F[i] = 1; + A[level] = i; + Recur(level + 1, i); + F[i] = 0; + if (level) + { + x = A[level - 1]; + Temp -= Dis[x][i]; + } + } + } +} +void Cal() +{ + int i, j, k; + Temp = 0.0; + Recur(0, 0); + printf("%s\n", as); + printf("Network #%d\n", Kase++); + for (i = 0; i < N - 1; i++) + { + j = Record[i]; + k = Record[i + 1]; + printf("Cable requirement to connect (%.lf,%.lf) to (%.lf,%.lf) is %.2lf feet.\n", Point[j][0], Point[j][1], Point[k][0], Point[k][1], Dis[j][k]); + } + printf("Number of feet of cable required is %.2lf.\n", MAX); +} +int main() +{ + int i; + Kase = 1; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + MAX = 2147483647; + for (i = 0; i < N; i++) + { + scanf("%lf%lf", &Point[i][0], &Point[i][1]); + } + Set_dis(); + Cal(); + } + return 0; +} diff --git a/217.cpp b/217.cpp new file mode 100644 index 0000000..9f3892e --- /dev/null +++ b/217.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +typedef struct +{ + double x, y; +} pair_t; + +struct +{ + char name[256]; + pair_t loc; +} beacon[32]; + +int nbeacons; +double pi; + +int sol1(pair_t *r, double d, double a[2], pair_t p[2]) +{ + double t, t0, t1, x0, x1; + while (a[0] < 0) + { + a[0] += pi; + } + while (a[0] > pi) + { + a[0] -= pi; + } + while (a[1] < 0) + { + a[1] += pi; + } + while (a[1] > pi) + { + a[1] -= pi; + } + if (fabs(a[0] - pi) < EPS) + { + a[0] = 0; + } + if (fabs(a[1] - pi) < EPS) + { + a[1] = 0; + } + t = fabs(a[0] - a[1]); + if (fabs(t) < EPS || fabs(t - pi) < EPS) + { + return 0; + } + if (fabs(a[0]) < EPS) + { + r->x = p[1].x - (p[1].y - p[0].y) / tan(a[1]); + r->y = p[0].y; + return 1; + } + if (fabs(a[1]) < EPS) + { + r->x = p[0].x - (p[0].y - p[1].y) / tan(a[0]) + d; + r->y = p[1].y; + return 1; + } + t0 = cos(a[0]) / sin(a[0]); + t1 = cos(a[1]) / sin(a[1]); + x0 = p[0].x - p[0].y * t0; + x1 = p[1].x - p[1].y * t1; + r->y = (d - x1 + x0) / (t1 - t0); + r->x = x1 + r->y * t1; + return 1; +} + +pair_t rot(pair_t p, double a) +{ + pair_t q; + q.x = p.x * cos(a) - p.y * sin(a); + q.y = p.x * sin(a) + p.y * cos(a); + return q; +} + +int sol(pair_t *r, double c, double d, double a[2], pair_t p[2]) +{ + double t = -c; + pair_t q[2], z; + a[0] += t; + a[1] += t; + q[0] = rot(p[0], t); + q[1] = rot(p[1], t); + if (sol1(&z, d, a, q) == 0) + { + return 0; + } + *r = rot(z, -t); + return 1; +} + +pair_t getloc() +{ + static char s[256]; + int i; + scanf(" %s", s); + for (i = 0; i < nbeacons; i++) + if (strcmp(beacon[i].name, s) == 0) + { + break; + } + assert(i < nbeacons); + return beacon[i].loc; +} + +int main() +{ + pair_t p[2], r; + double course, speed, tm[2], a[2], d; + int i, m, t; + pi = 2. * acos(0.); + for (t = 1; scanf("%d", &nbeacons) == 1;) + { + for (i = 0; i < nbeacons; i++) + scanf(" %s %lf %lf", + beacon[i].name, + &beacon[i].loc.x, &beacon[i].loc.y); + for (scanf("%d", &m); m-- > 0; t++) + { + scanf("%lf %lf %lf", &course, &speed, &tm[0]); + p[0] = getloc(); + scanf("%lf %lf", &a[0], &tm[1]); + p[1] = getloc(); + scanf("%lf", &a[1]); + a[0] = (90. - course - a[0]) * pi / 180.; + a[1] = (90. - course - a[1]) * pi / 180.; + course = (90. - course) * pi / 180.; + d = fabs(tm[1] - tm[0]) * speed; + printf("Scenario %d: ", t); + if (sol(&r, course, d, a, p)) + { + printf("Position is (%.2f, %.2f)\n", r.x, r.y); + } + else + { + printf("Position cannot be determined\n"); + } + } + } + return 0; +} diff --git a/218.cpp b/218.cpp new file mode 100644 index 0000000..2ce81fe --- /dev/null +++ b/218.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +const int maxn = 1000 + 10; +const double eps = 1e-8; +struct point +{ + double x, y; +}; + +point points[maxn]; +struct convex +{ + point c[maxn]; + int len; +}; +convex cn[maxn]; +int n1, n2, len1, len2; +inline double multiply(point a, point b, point o) +{ + return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); +} +inline double dot(point a, point b, point o) +{ + return (a.x - o.x) * (b.x - o.x) + (a.y - o.y) * (b.y - o.y); +} +inline double getdis(point a, point b)//两个点之间的è·ç¦» +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} +double dis2(point a, point b) +{ + return ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} +inline bool cmpp(point a, point b)//按æžè§’æŽ’åº +{ + //从å°åˆ°å¤§æŽ’åº + double m = multiply(a, b, points[0]); + if (m > 0) + { + return true; + } + else if (m == 0) + { + return dis2(a, points[0]) > dis2(b, points[0]); + } + return false; +} +int grahamConvex(point points[], int n, point done[]) +{ + int u = 0; + int top = 0; + for (int i = 1; i < n; i++) + { + if ((points[i].x < points[u].x) || (points[i].x == points[u].x && points[i].y < points[u].y)) + { + u = i; + } + } + std::swap(points[0], points[u]); + std::sort(points + 1, points + n, cmpp); + for (int i = 0; i <= 2; i++) + { + done[i] = points[i]; + } + top = 2; + for (int i = 3; i < n; i++) + { + while (multiply(done[top], done[top - 1], points[i]) >= 0) + { + top--; + } + done[++top] = points[i]; + } + return top + 1; +} + +int main() +{ + int n, s = 0; + while (scanf("%d", &n), n) + { + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &points[i].x, &points[i].y); + } + cn[s].len = grahamConvex(points, n, cn[s].c); + s++; + } + for (int i = 0; i < s; i++) + { + printf("Region #%d:\n", i + 1); + printf("(%.1lf,%.1lf)", cn[i].c[0].x, cn[i].c[0].y); + double p = 0; + cn[i].c[cn[i].len] = cn[i].c[0]; + for (int j = cn[i].len - 1; j >= 0; j--) + { + printf("-(%.1lf,%.1lf)", cn[i].c[j].x, cn[i].c[j].y); + p += getdis(cn[i].c[j], cn[i].c[j + 1]); + } + printf("\nPerimeter length = %.2lf\n\n", p); + } + return 0; +} diff --git a/219.cpp b/219.cpp new file mode 100644 index 0000000..fa963be --- /dev/null +++ b/219.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +struct FD +{ + int A, B; +}; + +int N, path[200], used[200], curFD; +FD fds[200]; + +int rec(int curLen, int known) +{ + int i, s; + path[curLen] = -1; + if ((known & fds[curFD].B) == fds[curFD].B) + { + return 1; + } + s = known; + while (true) + { + bool changed = false; + for (i = 0; i < N; i++) + { + if (used[i]) + { + continue; + } + if ((s & fds[i].A) != fds[i].A) + { + continue; + } + if ((s | fds[i].B) == s) + { + continue; + } + s |= fds[i].B; + changed = true; + } + if (!changed || (s & fds[curFD].B) == fds[curFD].B) + { + break; + } + } + if ((s & fds[curFD].B) != fds[curFD].B) + { + return 0; + } + s = known; + for (i = 0; i < N; i++) + { + if (used[i]) + { + continue; + } + if ((s & fds[i].A) != fds[i].A) + { + continue; + } + if ((s | fds[i].B) == s) + { + continue; + } + used[i] = 1; + if (rec(curLen, s)) + { + return 1; + } + path[curLen] = i; + s |= fds[i].B; + if (rec(curLen + 1, s)) + { + return 1; + } + break; + } + assert(0 == 1); + return 0; +} + +void parseFD(char *line, FD &fd) +{ + fd.A = fd.B = 0; + for (int i = 0, j = 0; line[i] != 0; i++) + { + if (line[i] == '>') + { + j = 1; + } + if (isupper(line[i])) + { + if (j == 0) + { + fd.A |= 1 << (line[i] - 'A'); + } + fd.B |= 1 << (line[i] - 'A'); + } + } +} + +int main() +{ + char line[1000]; + for (int cs = 1; scanf("%d", &N) == 1 && N > 0; cs++) + { + for (int i = 0; i < N; i++) + { + scanf(" %s", line); + parseFD(line, fds[i]); + } + printf("Set number %d\n", cs); + int nr = 0; + for (curFD = 0; curFD < N; curFD++) + { + memset(used, 0, sizeof(used)); + used[curFD] = 1; + if (rec(0, fds[curFD].A)) + { + printf(" FD %d is redundant using FDs:", curFD + 1); + for (int j = 0; path[j] != -1; j++) + { + printf(" %d", path[j] + 1); + } + printf("\n"); + nr++; + } + } + if (nr == 0) + { + printf(" No redundant FDs.\n"); + } + printf("\n"); + } +} diff --git a/220.cpp b/220.cpp new file mode 100644 index 0000000..726a595 --- /dev/null +++ b/220.cpp @@ -0,0 +1,262 @@ +#include + +using namespace std; + +char board[10][10]; + +void clear_board() +{ + for (int row = 0; row < 10; row++) + for (int column = 0; column < 10; column++) + { + board[row][column] = '-'; + } +} + +void read_board() +{ + for (int row = 1; row < 9; row++) + for (int column = 1; column < 9; column++) + { + cin >> board[row][column]; + } +} + +void print_board() +{ + for (int row = 1; row < 9; row++) + { + for (int column = 1; column < 9; column++) + { + cout << board[row][column]; + } + cout << endl; + } +} + +vector> get_legal_moves(char current_player) +{ + int i, j; + vector> legal_moves; + char opposite_player = (current_player == 'B' ? 'W' : 'B'); + for (int row = 1; row < 9; row++) + { + for (int column = 1; column < 9; column++) + { + if (board[row][column] != '-') + { + continue; + } + if (board[row - 1][column] == opposite_player) + { + for (i = row - 1; i >= 1 && board[i][column] == opposite_player; i--) + ; + if (board[i][column] == current_player && i >= 1) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row + 1][column] == opposite_player) + { + for (i = row + 1; i < 9 && board[i][column] == opposite_player; i++) + ; + if (board[i][column] == current_player && i < 9) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row][column - 1] == opposite_player) + { + for (i = column - 1; i >= 1 && board[row][i] == opposite_player; i--) + ; + if (board[row][i] == current_player && i >= 1) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row][column + 1] == opposite_player) + { + for (i = column + 1; i < 9 && board[row][i] == opposite_player; i++) + ; + if (board[row][i] == current_player && i < 9) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row - 1][column - 1] == opposite_player) + { + for (i = row - 1, j = column - 1; i >= 1 && j >= 1 && board[i][j] == opposite_player; i--, j--) + ; + if (board[i][j] == current_player && i >= 1 && j >= 1) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row - 1][column + 1] == opposite_player) + { + for (i = row - 1, j = column + 1; i >= 1 && j < 9 && board[i][j] == opposite_player; i--, j++) + ; + if (board[i][j] == current_player && i >= 1 && j < 9) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row + 1][column - 1] == opposite_player) + { + for (i = row + 1, j = column - 1; i < 9 && j >= 1 && board[i][j] == opposite_player; i++, j--) + ; + if (board[i][j] == current_player && i < 9 && j >= 1) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + if (board[row + 1][column + 1] == opposite_player) + { + for (i = row + 1, j = column + 1; i < 9 && j < 9 && board[i][j] == opposite_player; i++, j++) + ; + if (board[i][j] == current_player && i < 9 && j < 9) + { + legal_moves.push_back(make_pair(row, column)); + continue; + } + } + } + } + return legal_moves; +} + +char make_move(char current_player, int row, int column) +{ + int i, j; + vector> legal_moves = get_legal_moves(current_player); + if (legal_moves.empty()) + { + current_player = (current_player == 'B' ? 'W' : 'B'); + } + board[row][column] = current_player; + char opposite_player = (current_player == 'B' ? 'W' : 'B'); + for (i = row - 1; i >= 1 && board[i][column] == opposite_player; i--) + ; + if (board[i][column] == current_player && i >= 1) + for (i = row - 1; i >= 1 && board[i][column] == opposite_player; board[i][column] = current_player, i--) + ; + for (i = row + 1; i < 9 && board[i][column] == opposite_player; i++) + ; + if (board[i][column] == current_player && i < 9) + for (i = row + 1; i < 9 && board[i][column] == opposite_player; board[i][column] = current_player, i++) + ; + for (i = column - 1; i >= 1 && board[row][i] == opposite_player; i--) + ; + if (board[row][i] == current_player && i >= 1) + for (i = column - 1; i >= 1 && board[row][i] == opposite_player; board[row][i] = current_player, i--) + ; + for (i = column + 1; i < 9 && board[row][i] == opposite_player; i++) + ; + if (board[row][i] == current_player && i < 9) + for (i = column + 1; i < 9 && board[row][i] == opposite_player; board[row][i] = current_player, i++) + ; + for (i = row - 1, j = column - 1; i >= 1 && j >= 1 && board[i][j] == opposite_player; i--, j--) + ; + if (board[i][j] == current_player && i >= 1 && j >= 1) + for (i = row - 1, j = column - 1; i >= 1 && j >= 1 && board[i][j] == opposite_player; board[i][j] = current_player, i--, j--) + ; + for (i = row - 1, j = column + 1; i >= 1 && j < 9 && board[i][j] == opposite_player; i--, j++) + ; + if (board[i][j] == current_player && i >= 1 && j < 9) + for (i = row - 1, j = column + 1; i >= 1 && j < 9 && board[i][j] == opposite_player; board[i][j] = current_player, i--, j++) + ; + for (i = row + 1, j = column - 1; i < 9 && j >= 1 && board[i][j] == opposite_player; i++, j--) + ; + if (board[i][j] == current_player && i < 9 && j >= 1) + for (i = row + 1, j = column - 1; i < 9 && j >= 1 && board[i][j] == opposite_player; board[i][j] = current_player, i++, j--) + ; + for (i = row + 1, j = column + 1; i < 9 && j < 9 && board[i][j] == opposite_player; i++, j++) + ; + if (board[i][j] == current_player && i < 9 && j < 9) + for (i = row + 1, j = column + 1; i < 9 && j < 9 && board[i][j] == opposite_player; board[i][j] = current_player, i++, j++) + ; + int black_count = 0; + int white_count = 0; + for (int row = 1; row < 9; row++) + { + for (int column = 1; column < 9; column++) + { + if (board[row][column] == 'B') + { + black_count++; + } + else if (board[row][column] == 'W') + { + white_count++; + } + } + } + cout << "Black - " << (black_count < 10 ? " " : "") << black_count << " White - " << (white_count < 10 ? " " : "") << white_count << endl; + current_player = (current_player == 'B' ? 'W' : 'B'); + return current_player; +} + +void print_legal_moves(char current_player) +{ + vector> legal_moves = get_legal_moves(current_player); + if (legal_moves.empty()) + { + cout << "No legal move." << endl; + return; + } + for (int i = 0, sz = legal_moves.size(); i < sz; i++) + { + if (i > 0) + { + cout << " "; + } + cout << "(" << legal_moves[i].first << "," << legal_moves[i].second << ")"; + } + cout << endl; +} + +int main() +{ + int number_of_games; + char row, column; + char command; + char current_player; + cin >> number_of_games; + while (number_of_games--) + { + clear_board(); + read_board(); + cin >> current_player; + bool end_of_game = false; + while (!end_of_game) + { + cin >> command; + switch (command) + { + case 'L': + print_legal_moves(current_player); + break; + case 'M': + cin >> row >> column; + current_player = make_move(current_player, row - '0', column - '0'); + break; + case 'Q': + end_of_game = true; + print_board(); + break; + } + } + if (number_of_games) + { + cout << endl; + } + } + return 0; +} diff --git a/221.cpp b/221.cpp new file mode 100644 index 0000000..a5a2fb4 --- /dev/null +++ b/221.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +int N; + +struct Rect +{ + double x, y, w, d, h; + + friend istream &operator>>(istream &in, Rect &r) + { + return in >> r.x >> r.y >> r.w >> r.d >> r.h; + } +} R[100]; + +int seq[100]; + +bool comp1(const int &i, const int &j) +{ + return R[i].y < R[j].y || R[i].y == R[j].y && R[i].x < R[j].x; +} + +vector ans; + +bool comp2(const int &i, const int &j) +{ + return R[i].x < R[j].x || R[i].x == R[j].x && R[i].y < R[j].y; +} + +void discrete() +{ + int i, j, k; + bool taken[100] = {0}; + ans.clear(); + vector X, Y; + for (i = 0; i < N; ++i) + { + X.push_back(R[i].x); + X.push_back(R[i].x + R[i].w); + } + for (i = 0; i < N; ++i) + { + Y.push_back(R[i].h); + } + sort(X.begin(), X.end()); + sort(Y.begin(), Y.end()); + X.erase(unique(X.begin(), X.end()), X.end()); + Y.erase(unique(Y.begin(), Y.end()), Y.end()); + for (j = 0; j < Y.size(); ++j) + for (i = 0; i < X.size() - 1; ++i) + for (k = 0; k < N; ++k) + { + Rect &r = R[seq[k]]; + if (r.x <= X[i] && r.x + r.w >= X[i + 1] && r.h >= Y[j]) + { + if (!taken[seq[k]]) + { + ans.push_back(seq[k]); + taken[seq[k]] = true; + } + break; + } + } +} + +int main() +{ + int cn = 0; + while (cin >> N && N) + { + if (cn++) + { + cout << endl; + } + int i; + for (i = 0; i < N; ++i) + { + cin >> R[i]; + seq[i] = i; + } + sort(seq, seq + N, comp1); + discrete(); + sort(ans.begin(), ans.end(), comp2); + cout << "For map #" << cn << ", the visible buildings are numbered as follows:" << endl; + cout << ans[0] + 1; + for (i = 1; i < ans.size(); ++i) + { + cout << " " << ans[i] + 1; + } + cout << endl; + } + return 0; +} diff --git a/222.cpp b/222.cpp new file mode 100644 index 0000000..512641d --- /dev/null +++ b/222.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +double DIST, CAP, MPG, D[52], P[52], COST, LOWEST; +int N; +void recurse(int n, double fuel, double cost) +{ + double need, spend; + if (n == N + 1) + { + if (LOWEST < COST || cost < LOWEST) + { + LOWEST = cost; + } + return; + } + fuel -= (D[n] - D[n - 1]) / MPG; + need = (D[n + 1] - D[n]) / MPG; + if (!(fuel > CAP / 2 && fuel > need)) + { + spend = (((CAP - fuel) * P[n] + 200.0)) / 100.0; + recurse(n + 1, CAP, cost + spend); + } + if (fuel > need) + { + recurse(n + 1, fuel, cost); + } +} +int main() +{ + int i, j, n = 0; + while (1) + { + scanf("%lf", &DIST); + if (DIST < 0) + { + break; + } + scanf("%lf %lf %lf %i", &CAP, &MPG, &COST, &N); + D[0] = P[0] = 0; + for (i = 1; i <= N; i++) + { + scanf("%lf %lf", &D[i], &P[i]); + } + D[N + 1] = DIST; + P[N + 1] = 0; + LOWEST = 0; + recurse(1, CAP, COST); + printf("Data Set #%d\n", ++n); + printf("minimum cost = $%.2f\n", LOWEST); + } +} diff --git a/223.cpp b/223.cpp new file mode 100644 index 0000000..5bb8bd0 --- /dev/null +++ b/223.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +#define FOREACH(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) + +struct Point +{ + int x, y; + Point() {} + Point(int x_, int y_) : x(x_), y(y_) {} + bool operator<(const Point &p) const + { + return x != p.x ? (x < p.x) : (y < p.y); + } +}; + +Point operator-(const Point &a, const Point &b) +{ + return Point(a.x - b.x, a.y - b.y); +} + +Point V[500]; +int N; +map Vmap; +map answer; + +int read() +{ + scanf("%d %d", &V[N].x, &V[N].y); + if (Vmap.count(V[N]) == 0) + { + Vmap[V[N]] = N; + return N++; + } + return Vmap[V[N]]; +} + +struct Segment +{ + int a, b, index, face; + Segment *rev; + + void print() + { + printf("(%d,%d)-(%d,%d)\n", V[a].x, V[a].y, V[b].x, V[b].y); + } +} seg[500]; +int M; +vector adj[500]; + +struct Cmp +{ + bool operator()(Segment *S1, Segment *S2) const + { + Point a = V[S1->b] - V[S1->a]; + Point b = V[S2->b] - V[S1->a]; + return atan2(a.y, a.x) < atan2(b.y, b.x); + } +}; + +int main() +{ + for (int cs = 1; scanf("%d", &M) == 1 && M > 0; cs++) + { + N = 0; + Vmap.clear(); + for (int i = 0; i < 2 * M; i += 2) + { + seg[i].a = read(); + seg[i].b = read(); + seg[i + 1] = seg[i]; + swap(seg[i + 1].a, seg[i + 1].b); + seg[i].rev = &seg[i + 1]; + seg[i + 1].rev = &seg[i]; + } + for (int i = 0; i < N; i++) + { + adj[i].clear(); + } + for (int i = 0; i < 2 * M; i++) + { + adj[seg[i].a].push_back(&seg[i]); + } + for (int a = 0; a < N; a++) + { + sort(adj[a].begin(), adj[a].end(), Cmp()); + for (int i = 0; i < (int)adj[a].size(); i++) + { + Segment *s = adj[a][i]; + s->index = i; + } + } + answer.clear(); + for (int i = 0; i < 2 * M; i++) + { + seg[i].face = -1; + } + int numfaces = 0; + for (int s1 = 0; s1 < 2 * M; s1++) + { + if (seg[s1].face != -1) + { + continue; + } + numfaces++; + seg[s1].face = numfaces; + vector face; + face.push_back(&seg[s1]); + while (true) + { + Segment *s = face.back(); + int m = adj[s->b].size(); + Segment *t = adj[s->b][(s->rev->index + m - 1) % m]; + if (t == face[0]) + { + break; + } + assert(t->face == -1); + t->face = numfaces; + face.push_back(t); + } + long long area = 0; + FOREACH(it, face) + { + Segment *s = *it; + area += V[s->a].x * V[s->b].y - V[s->a].y * V[s->b].x; + } + if (area >= 0) + { + ++answer[face.size()]; + } + } + if (cs != 1) + { + printf("\n"); + } + printf("Case %d\n", cs); + int total = 0; + FOREACH(it, answer) + { + printf("Number of lots with perimeter consisting of %d surveyor's lines = %d\n", + it->first, it->second); + total += it->second; + } + printf("Total number of lots = %d\n", total); + } +} diff --git a/225.cpp b/225.cpp new file mode 100644 index 0000000..3427962 --- /dev/null +++ b/225.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define FOR(i, n) for (int i = 0; i < n; ++i) +const char *md = "ensw"; +char path[32]; +int dx[4] = {1, 0, 0, -1}; +int dy[4] = {0, 1, -1, 0}; +int nd[4][2] = {{1, 2}, {0, 3}, {0, 3}, {1, 2}}; +int bx[64], by[64], px[32], py[32]; +int blocked, found, steps; + +int min(int a, int b) +{ + return a < b ? a : b; +} +int max(int a, int b) +{ + return a > b ? a : b; +} +bool between(int v, int a, int b) +{ + return v >= min(a, b) && v <= max(a, b); +} +bool free(int x, int y, int nx, int ny) +{ + if (x == nx) + { + FOR(i, blocked) + if (bx[i] == x && between(by[i], y, ny)) + { + return false; + } + } + if (y == ny) + { + FOR(i, blocked) + if (by[i] == y && between(bx[i], x, nx)) + { + return false; + } + } + return true; +} +bool notbeenin(int x, int y, int s) +{ + FOR(i, s) + if (px[i] == x && py[i] == y) + { + return false; + } + return true; +} + +void bt(int step_n, int x, int y, int dir) +{ + if (step_n == steps) + { + if (x == y && !x) + { + ++found; + puts(path); + } + return; + } + int mul = step_n + 1, next, nx, ny; + FOR(i, 2) + { + next = nd[dir][i]; + nx = x + mul * dx[next]; + ny = y + mul * dy[next]; + if (free(x, y, nx, ny) && notbeenin(nx, ny, step_n)) + { + path[step_n] = md[next]; + px[step_n] = nx; + py[step_n] = ny; + bt(mul, nx, ny, next); + } + } +} +int main() +{ + int t; + scanf("%d", &t); + while (t-- && scanf("%d%d", &steps, &blocked) == 2) + { + FOR(i, blocked) + scanf("%d%d", bx + i, by + i); + found = path[steps] = 0; + FOR(i, 4) + { + path[0] = md[i]; + px[0] = dx[i]; + py[0] = dy[i]; + bt(1, dx[i], dy[i], i); + } + printf("Found %d golygon(s).\n\n", found); + } + return 0; +} diff --git a/227.cpp b/227.cpp index 28d6f8b..a3789bd 100644 --- a/227.cpp +++ b/227.cpp @@ -1,94 +1,168 @@ -#include -#include +#include + using namespace std; -int main() { - for (int p = 1; ; p++) { - string str; - string mat[5]; - bool valid = true; - while (true) { - getline(cin, mat[0]); - if (mat[0] == "Z") { - return 0; +char B[7][7], com[1000]; +int er, ec, F; + +void Above() +{ + char tc; + if (er == 0) + { + F = 1; + return; + } + tc = B[er - 1][ec]; + B[er][ec] = tc; + B[er - 1][ec] = ' '; + er--; +} +void Right() +{ + char tc; + if (ec == 4) + { + F = 1; + return; + } + tc = B[er][ec + 1]; + B[er][ec + 1] = ' '; + B[er][ec] = tc; + ec++; +} +void Below() +{ + char tc; + if (er == 4) + { + F = 1; + return; + } + tc = B[er + 1][ec]; + B[er + 1][ec] = ' '; + B[er][ec] = tc; + er++; +} +void Left() +{ + char tc; + if (ec == 0) + { + F = 1; + return; + } + tc = B[er][ec - 1]; + B[er][ec - 1] = ' '; + B[er][ec] = tc; + ec--; +} +void Print() +{ + int i, j; + if (F) + { + printf("This puzzle has no final configuration.\n"); + return; + } + for (i = 0; i < 5; i++) + { + printf("%c", B[i][0]); + for (j = 1; j < 5; j++) + printf(" %c", B[i][j]); + printf("\n"); + } +} +void Cal() +{ + int i; + F = 0; + while (1) + { + gets(com); + for (i = 0; com[i]; i++) + { + if (com[i] == '\n') + { + com[i] = '\0'; } - getline(cin, mat[1]); - getline(cin, mat[2]); - getline(cin, mat[3]); - getline(cin, mat[4]); - - int I, J; - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { - if (mat[i][j] == ' ') { - I = i; J = j; - i = j = 5; - } - } + } + for (i = 0; com[i]; i++) + { + if (com[i] == '0') + { + return; } - - bool endPuzzle = false; - while (!endPuzzle && getline(cin, str)) { - - for (int i = 0; i < str.length(); i++) { - - if (str[i] == '0' || !valid) { - endPuzzle = true; - break; - } - - if (str[i] == 'A') { - if (I <= 0) valid = false; - else { - mat[I][J] = mat[I - 1][J]; - --I; - mat[I][J] = ' '; - } - } - if (str[i] == 'B') { - if (I >= 4) valid = false; - else { - mat[I][J] = mat[I + 1][J]; - ++I; - mat[I][J] = ' '; - } - } - if (str[i] == 'L') { - if (J <= 0) valid = false; - else { - mat[I][J] = mat[I][J - 1]; - --J; - mat[I][J] = ' '; - } - } - if (str[i] == 'R') { - if (J >= 4) valid = false; - else { - mat[I][J] = mat[I][J + 1]; - ++J; - mat[I][J] = ' '; - } - } - } + if (F) + { + continue; + } + switch (com[i]) + { + case 'A': + Above(); + break; + case 'R': + Right(); + break; + case 'B': + Below(); + break; + case 'L': + Left(); + break; + default: + F = 1; + } + } + } +} + +int main() +{ + int i, j, f, kase = 0; + while (1) + { + gets(B[0]); + if (!strcmp(B[0], "Z")) + { + break; + } + f = 1; + for (i = 0; B[0][i]; i++) + { + if (B[0][i] == ' ') + { + f = 0; + er = 0; + ec = i; + break; } } - - if (p > 1) - cout << endl; - - cout << "Puzzle #" << p << ":\n"; - if (!valid) { - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { - if (j) cout << " "; - cout << mat[i][j]; + for (i = 1; i < 5; i++) + { + gets(B[i]); + if (!f) + { + continue; + } + for (j = 0; B[i][j]; j++) + { + if (B[i][j] == ' ') + { + f = 0; + er = i; + ec = j; } - cout << endl; } } - else { - cout << "This puzzle has no final configuration.\n"; + if (kase++) + { + printf("\n"); } + Cal(); + printf("Puzzle #%d:\n", kase); + Print(); } return 0; } diff --git a/229.cpp b/229.cpp new file mode 100644 index 0000000..0a00291 --- /dev/null +++ b/229.cpp @@ -0,0 +1,181 @@ +#include + +using namespace std; + +int nextn[10][15] = +{ + {14, 29, 44, 43, 42, 41, 40, 126, 143, 144, 145, 146, 147, 148, 149}, + {1, 2, 3, 4, 5, 6, 22, 23, 114, 8, 9, 10, 11, 12, 13}, + {16, 17, 18, 19, 20, 55, 112, 96, 50, 85, 24, 25, 26, 27, 28}, + {104, 103, 102, 101, 35, 36, 97, 81, 65, 38, 39, 141, 140, 139, 138}, + {45, 46, 47, 48, 111, 51, 67, 82, 66, 53, 79, 56, 57, 58, 59}, + {60, 61, 62, 63, 64, 98, 83, -1, 68, 69, 70, 71, 72, 73, 74}, + {75, 76, 77, 78, 49, 113, 80, 52, 84, 54, 110, 86, 87, 88, 89}, + {30, 31, 32, 33, 34, 94, 95, 37, 99, 100, 142, 93, 92, 91, 90}, + {15, 105, 106, 107, 108, 109, 127, 128, 21, 115, 116, 117, 118, 119, 137}, + {0, 120, 121, 122, 123, 124, 125, 7, 129, 130, 131, 132, 133, 134, 136} +}; +int diag[10][15] = +{ + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, + {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, + {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, + {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, + {8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, + {9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23} +}; +int udiag[10][15] = +{ + {9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, + {8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}, + {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}, + {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, + {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, + {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, + {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, + {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, + {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} +}; + +char ans[10][15], find_, tmp[10][15]; +int hori[10], ver[15], topright[24], topleft[24]; +int numhori[10], numver[15], numtopright[24], numtopleft[24]; + +int print() +{ + int i, j; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 15; j++) + { + putchar((tmp[i][j] ? '#' : '.')); + } + putchar('\n'); + } + return 0; +} +int DFS(int x, int y) +{ + int f, i, j; + if (find_ == 2) + { + return 1; + } + if (x == -1 && y == -1) + { + find_++; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 15; j++) + { + tmp[i][j] = ans[i][j]; + } + } + return 1; + } + if (hori[x] > 0 && ver[y] > 0 && topright[diag[x][y]] > 0 && topleft[udiag[x][y]] > 0) + { + hori[x]--, ver[y]--, topright[diag[x][y]]--, topleft[udiag[x][y]]--; + ans[x][y] = 1; + if (nextn[x][y] == -1) + { + f = DFS(-1, -1); + } + else + { + f = DFS(nextn[x][y] / 15, nextn[x][y] % 15); + } + if (find_ == 2) + { + return 1; + } + hori[x]++, ver[y]++, topright[diag[x][y]]++, topleft[udiag[x][y]]++; + } + if (numhori[x] > 0 && numver[y] > 0 && numtopright[diag[x][y]] > 0 && numtopleft[udiag[x][y]] > 0) + { + numhori[x]--, numver[y]--, numtopright[diag[x][y]]--, numtopleft[udiag[x][y]]--; + ans[x][y] = 0; + if (nextn[x][y] == -1) + { + f = DFS(-1, -1); + } + else + { + f = DFS(nextn[x][y] / 15, nextn[x][y] % 15); + } + if (find_ == 2) + { + return 1; + } + numhori[x]++, numver[y]++, numtopright[diag[x][y]]++, numtopleft[udiag[x][y]]++; + } + return 0; +} + +int main() +{ + int c, i, j; + scanf("%d", &c); + for (; c; c--) + { + find_ = 0; + for (i = 0, j = 1; i < 10; i++, j++) + { + numtopright[i] = numtopleft[i] = j; + } + for (; i < 15; i++) + { + numtopright[i] = numtopleft[i] = 10; + } + for (j = 9; i < 24; j--, i++) + { + numtopright[i] = numtopleft[i] = j; + } + for (i = 0; i < 10; i++) + { + scanf("%d", &hori[i]); + numhori[i] = 15 - hori[i]; + } + for (i = 0; i < 24; i++) + { + scanf("%d", &topright[i]); + numtopright[i] -= topright[i]; + } + for (i = 0; i < 15; i++) + { + scanf("%d", &ver[i]); + numver[i] = 10 - ver[i]; + } + for (i = 0; i < 24; i++) + { + scanf("%d", &topleft[i]); + numtopleft[i] -= topleft[i]; + } + DFS(9, 0); + if (find_ == 1) + { + print(); + } + else + { + for (i = 0; i < 10; i++) + { + for (j = 0; j < 15; j++) + { + tmp[i][j] = 0; + } + } + print(); + } + if (c > 1) + { + putchar('\n'); + } + } + return 0; +} diff --git a/230.cpp b/230.cpp new file mode 100644 index 0000000..8a6e484 --- /dev/null +++ b/230.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +class Book +{ +public: + string t, a; + bool operator<(const Book &b) const + { + if (a != b.a) + { + return a < b.a; + } + else + { + return t < b.t; + } + } +}; + +void shelve(set &S, map &M, vector &ret) +{ + sort(ALL(ret)); + rep(i, ret.size()) + { + string tar = ret[i].t; + Book ins = {tar, M[tar]}; + S.insert(ins); + set::iterator itr = S.find(ins); + if (itr == S.begin()) + { + cout << "Put \"" << tar << "\" first" << endl; + } + else + { + itr--; + cout << "Put \"" << tar << "\" after \"" << (*itr).t << "\"" << endl; + } + } + puts("END"); + ret.clear(); + return; +} + +main() +{ + set S; + map M; + vector ret; + string in; + while (getline(cin, in) && in != "END") + { + string title = "", author; + int p = 0; + while (p < in.size()) + { + if (in[p++] == '"') + { + break; + } + } + while (p < in.size()) + { + if (in[p] == '"') + { + p++; + break; + } + title += in[p++]; + } + p += 4; + author = in.substr(p); + S.insert((Book){ + title, author}); + M[title] = author; + } + while (getline(cin, in) && in != "END") + { + if (in[0] == 'B' || in[0] == 'R') + { + string title; + int p = 0; + while (p < in.size()) + { + if (in[p++] == '"') + { + break; + } + } + while (p < in.size()) + { + if (in[p] == '"') + { + p++; + break; + } + title += in[p++]; + } + if (in[0] == 'B') + S.erase((Book){ + title, M[title]}); + else if (in[0] == 'R') + ret.push_back((Book){ + title, M[title]}); + } + else if (in == "SHELVE") + { + shelve(S, M, ret); + } + } +} diff --git a/231.cpp b/231.cpp index 6adec2a..45030fe 100644 --- a/231.cpp +++ b/231.cpp @@ -1,43 +1,151 @@ -#include -using namespace std; - -int main(){ - for(int test=1; ; test++){ - bool flag = false; - int N=0, missile[40000]; - for(N=0; ;N++){ - int no; - cin>>no; - if(no == -1){ - if(N == 0) - flag = true; - break; - } - missile[N] = no; - } - if(flag == true) - break; - if(test > 1) - cout<= missile[j]){ - if(len[i] + 1 > len[j]){ - len[j] = len[i] + 1; - } - } - } - } - cout<<"Test #"< + +using namespace std; + +const int MAXSIZE = 300001; + +struct Element +{ + int key, pre; + + bool operator>(Element const &another) + { + if (this->key < another.key) + { + return true; + } + else + { + return false; + } + } + + bool operator<(Element const &another) + { + if (this->key > another.key) + { + return true; + } + else + { + return false; + } + } + + bool operator==(Element const &another) + { + if (this->key == another.key) + { + return true; + } + else + { + return false; + } + } +}; + +Element arr[MAXSIZE]; // arr of elements +int lis[MAXSIZE]; // arr of indxs in a +int len; // arr len +int last; // lst len + +int binSearch(int start, int end, Element e) +{ + int low = start, mid, high = end; + while (low <= high) + { + mid = (low + high) >> 1; + if (arr[lis[mid]] == e) + { + return mid; + } + else if (arr[lis[mid]] < e) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return mid; +} + +void solve() +{ + arr[0].pre = -1; + lis[last++] = 0; + int i; + for (i = 1; i < len; i++) + { + if (arr[i] > arr[lis[last - 1]]) + { + arr[i].pre = lis[last - 1]; + lis[last++] = i; + } + else + { + int x = binSearch(0, last - 1, arr[i]); + if (arr[lis[x]] == arr[i]) + { + arr[i].pre = arr[lis[x]].pre; + } + else if (arr[lis[x]] < arr[i]) + { + arr[i].pre = lis[x]; + lis[x + 1] = i; + } + else + { + if (x == 0) + { + arr[i].pre = -1; + } + else + { + arr[i].pre = lis[x - 1]; + } + lis[x] = i; + } + } + } +} + +void output() +{ + cout << " maximum possible interceptions: " << last << endl; +} + +int input() +{ + len = last = 0; + cin >> arr[len++].key; + if (arr[0].key < 0) + { + return 0; + } + while (cin >> arr[len].key && arr[len].key >= 0) + { + len++; + } + return 1; +} + +int main() +{ + int t = 1, f = 0; + while (input()) + { + if (f) + { + printf("\n"); + } + f = 1; + solve(); + printf("Test #%d:\n", t); + t++; + output(); + } + return 0; +} diff --git a/232.cpp b/232.cpp new file mode 100644 index 0000000..53f1b6f --- /dev/null +++ b/232.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int main() +{ + int n, m, t = 0; + char map_[20][20]; + while (scanf("%d%d", &n, &m) == 2) + { + for (int i = 0; i < n; i++) + { + scanf("%s", map_[i]); + } + if (t) + { + printf("\n"); + } + printf("puzzle #%d:\n", ++t); + int num[20][20] = {}, cnt = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (i - 1 < 0 || map_[i - 1][j] == '*' || j - 1 < 0 || map_[i][j - 1] == '*') + { + if (map_[i][j] != '*') + { + num[i][j] = ++cnt; + } + } + } + } + puts("Across"); + for (int i = 0; i < n; i++) + { + int g = 0; + for (int j = 0; j < m; j++) + { + if (map_[i][j] != '*') + { + if (g == 0) + { + printf("%3d.", num[i][j]); + } + printf("%c", map_[i][j]); + g = 1; + } + else + { + if (g) + { + printf("\n"); + g = 0; + } + } + } + if (g) + { + printf("\n"); + } + } + printf("Down\n"); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if ((i - 1 < 0 || map_[i - 1][j] == '*') && map_[i][j] != '*') + { + int ti = i; + printf("%3d.", num[i][j]); + while (ti < n && map_[ti][j] != '*') + { + printf("%c", map_[ti++][j]); + } + printf("\n"); + } + } + } + } + return 0; +} diff --git a/233.cpp b/233.cpp new file mode 100644 index 0000000..d9332d7 --- /dev/null +++ b/233.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +struct COMB +{ + int label, supply[4]; + double price; + void init() + { + memset(supply, 0, sizeof(supply)); + } + void print() + { + printf("%d %lf %d %d %d %d\n", label, price, supply[0], supply[1], supply[2], supply[3]); + } + bool operator<(const COMB &p) const + { + return label < p.label; + } +} products[64]; +int n, q, need[16]; +int mnCnt[64], path[64]; +double mnCost; +double dp[1048576]; +int dpChoose[1048576][2]; +int row[4]; +int getIndex(int A[]) +{ + int v = 0; + for (int j = 0; j < 4; j++) + v += A[j] * row[j]; + return v; +} +void bfs() +{ + row[0] = (need[3] + 1) * (need[2] + 1) * (need[1] + 1); + row[1] = (need[3] + 1) * (need[2] + 1); + row[2] = need[3] + 1; + row[3] = 1; + int A[4], B[4], u, v, mxState = getIndex(need); + for (int i = 0; i <= mxState; i++) + dp[i] = 1e+50, dpChoose[i][0] = dpChoose[i][1] = -1; + dpChoose[0][1] = -1, dp[0] = 0; + for (int p = 0; p <= mxState; p++) + { + u = p; + for (int i = 0; i < 4; i++) + A[i] = u / row[i], u %= row[i]; + u = p; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < 4; j++) + B[j] = min(A[j] + products[i].supply[j], need[j]); + v = getIndex(B); + if (dp[v] > dp[u] + products[i].price) + dp[v] = dp[u] + products[i].price, dpChoose[v][0] = i, dpChoose[v][1] = u; + } + } + memset(mnCnt, 0, sizeof(mnCnt)); + for (int p = mxState; p != -1; p = dpChoose[p][1]) + mnCnt[dpChoose[p][0]]++; + mnCost = dp[mxState]; +} +int main() +{ + char line[1024]; + string token; + int cnt = 0; + while (scanf("%d", &n) == 1) + { + while (getchar() != '\n') + ; + + for (int i = 0; i < n; i++) + { + products[i].init(); + gets(line); + stringstream sin(line); + sin >> products[i].label >> products[i].price; + while (sin >> token >> cnt) + products[i].supply[token[0] - 'a'] += cnt; + } + sort(products, products + n); + gets(line); + sscanf(line, "%d", &q); + for (int i = 0; i < q; i++) + { + memset(need, 0, sizeof(need)); + gets(line); + stringstream sin(line); + while (sin >> token >> cnt) + need[token[0] - 'a'] += cnt; + bfs(); + printf("%d:%8.2lf", i + 1, mnCost); + for (int j = 0; j < n; j++) + { + if (mnCnt[j]) + { + printf(" %d", products[j].label); + if (mnCnt[j] > 1) + printf("(%d)", mnCnt[j]); + } + } + puts(""); + } + puts(""); + } + return 0; +} diff --git a/234.cpp b/234.cpp new file mode 100644 index 0000000..d3daf01 --- /dev/null +++ b/234.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +const int INF = INT_MAX; + +struct Align +{ + int priority, time; +}; + +bool compare(Align a1, Align a2) +{ + return a1.time < a2.time; +} + +int main() +{ + int best_miss[10], curr_miss[10], best_arrange[10], prog[10], seg[10]; + Align align[10]; + int t = 0, p; + while (scanf("%d", &p) == 1, p) + { + ++t; + for (int i = 0; i < p; ++i) + { + scanf("%d", &prog[i]); + } + int a; + scanf("%d", &a); + int max_p = 0; + for (int i = 0; i < a; ++i) + { + scanf("%d%d", &align[i].priority, &align[i].time); + max_p = max(max_p, align[i].priority); + } + sort(prog, prog + p); + sort(align, align + a, compare); + for (int i = 1; i <= max_p; ++i) + { + best_miss[i] = INF; + } + seg[p] = INF; + + do + { + for (int i = 1; i <= max_p; ++i) + { + curr_miss[i] = 0; + } + seg[0] = prog[0]; + for (int i = 1; i < p; ++i) + { + seg[i] = seg[i - 1] + prog[i]; + } + int bef = 0, aft = seg[0], curr = 0; + for (int i = 0; i < a; ++i) + { + int t = align[i].time; + while (1) + { + if (t < aft) + { + break; + } + bef = aft; + aft = seg[++curr]; + } + curr_miss[align[i].priority] += min(t - bef, aft - t); + } + bool best = false; + for (int i = 1; i <= max_p; ++i) + { + if (curr_miss[i] < best_miss[i]) + { + best = true; + break; + } + if (curr_miss[i] > best_miss[i]) + { + break; + } + } + if (best) + { + for (int i = 1; i <= max_p; ++i) + { + best_miss[i] = curr_miss[i]; + } + for (int i = 0; i < p; ++i) + { + best_arrange[i] = prog[i]; + } + } + } while (next_permutation(prog, prog + p)); + + printf("Data set %d\n Order:", t); + for (int i = 0; i < p; ++i) + { + printf(" %d", best_arrange[i]); + } + int sum_miss = 0; + for (int i = 1; i <= max_p; ++i) + { + sum_miss += best_miss[i]; + } + printf("\n Error: %d\n", sum_miss); + } + return 0; +} diff --git a/238.cpp b/238.cpp new file mode 100644 index 0000000..1019860 --- /dev/null +++ b/238.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +int height[32][32], nrows, ncols; +bool canMove[32 * 32][4]; +int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; + +bool isvalid(int y, int x) +{ + return 0 <= y && y < nrows && 0 <= x && x < ncols; +} + +int sqr(int x) +{ + return x * x; +} + +bool travelOkay(int y1, int x1, int y2, int x2) +{ + return abs(height[y1][x1] - height[y2][x2]) <= 10; + // return sqr(height[y1][x1] - height[y2][x2]) + 1 <= 100; +} + +bool solve(int s, int t) +{ + double dist[32 * 32]; + bool seen[32 * 32]; + int prev[32 * 32]; + int N = nrows * ncols; + for (int i = 0; i < N; i++) + { + seen[i] = false; + prev[i] = -1; + } + dist[s] = 0; + prev[s] = s; + for (;;) + { + int u = -1; + for (int i = 0; i < N; i++) + if (prev[i] >= 0 && !seen[i] && (u < 0 || dist[i] < dist[u])) + { + u = i; + } + if (u < 0) + { + break; + } + int y1 = u / ncols; + int x1 = u % ncols; + seen[u] = true; + if (u == t) + { + break; + } + for (int d = 0; d < 4; d++) + { + if (!canMove[u][d]) + { + continue; + } + int y2 = y1 + dy[d]; + int x2 = x1 + dx[d]; + int v = y2 * ncols + x2; + if (seen[v]) + { + continue; + } + //double cost = sqrt(sqr(height[y1][x1] - height[y2][x2]) + 1.0); + double cost = 1.0; + if (prev[v] < 0 || dist[u] + cost < dist[v]) + { + dist[v] = dist[u] + cost; + prev[v] = u; + } + } + } + if (prev[t] < 0) + { + return false; + } + vector path; + for (int i = t;; i = prev[i]) + { + path.push_back(i); + if (i == s) + { + break; + } + } + for (int i = path.size() - 1; i >= 0; i--) + { + int u = path[i]; + printf("%d-%d%s", u / ncols + 1, u % ncols + 1, i == 0 ? "\n" : " to "); + } + return true; +} + +int main() +{ + int query = 0; + while (scanf("%d %d", &nrows, &ncols) == 2) + { + for (int i = 0; i < nrows; i++) + for (int j = 0; j < ncols; j++) + { + assert(scanf("%d", &height[i][j]) == 1); + } + for (int y = 0; y < nrows; y++) + for (int x = 0; x < ncols; x++) + for (int d = 0; d < 4; d++) + { + canMove[y * ncols + x][d] = false; + } + for (;;) + { + int y1, x1, y2, x2; + assert(scanf("%d %d %d %d", &y1, &x1, &y2, &x2) == 4); + if (y1-- == 0 || x1-- == 0 || y2-- == 0 || x2-- == 0) + { + break; + } + assert(isvalid(y1, x1) && isvalid(y2, x2)); + int d; + for (d = 0; d < 4; d++) + { + int t = abs(y1 - y2) + abs(x2 - x1); + if (y1 + t * dy[d] == y2 && x1 + t * dx[d] == x2) + { + break; + } + } + assert(0 <= d && d < 4); + while (y1 != y2 || x1 != x2) + { + if (travelOkay(y1, x1, y1 + dy[d], x1 + dx[d])) + { + canMove[y1 * ncols + x1][d] = true; + } + y1 += dy[d]; + x1 += dx[d]; + } + } + for (;;) + { + int y1, x1, y2, x2; + assert(scanf("%d %d %d %d", &y1, &x1, &y2, &x2) == 4); + if (y1-- == 0 || x1-- == 0 || y2-- == 0 || x2-- == 0) + { + break; + } + assert(isvalid(y1, x1) && isvalid(y2, x2)); + if (query++ > 0) + { + printf("\n"); + } + if (y1 == y2 && x1 == x2) + { + printf("To get from %d-%d to %d-%d, stay put!\n", y1 + 1, x1 + 1, y2 + 1, x2 + 1); + } + else if (!solve(y1 * ncols + x1, y2 * ncols + x2)) + { + printf("There is no acceptable route from %d-%d to %d-%d.\n", y1 + 1, x1 + 1, y2 + 1, x2 + 1); + } + } + } +} diff --git a/239.cpp b/239.cpp new file mode 100644 index 0000000..7eb2cea --- /dev/null +++ b/239.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +int primes[900], no; +int factor[900], temp[900]; +void init() +{ + primes[0] = 2; + no = 1; + for (int i = 3; i < 7000; i += 2) + { + bool flag = true; + for (int j = 0; primes[j] * primes[j] <= i && flag; j++) + if (i % primes[j] == 0) + { + flag = false; + } + if (flag) + { + primes[no++] = i; + } + } +} +void solve(int n) +{ + int queue[7000], front = -1, rear = n - 1; + int alter[7000]; + int stack1[5], stack5[12], stackh[12]; + int top1 = 0, top5 = 0, toph = 0; + bool v[7000]; + int ans[10]; + memset(v, false, sizeof(v)); + memset(factor, 0, sizeof(factor)); + for (int i = 0; i < n; i++) + { + queue[i] = i; + } + for (int t = 1; t <= 1440; t++) + { + stack1[top1++] = queue[front = (front + 1) % 7000]; + if (top1 == 5) + { + for (int i = 3; i >= 0; i--) + { + queue[rear = (rear + 1) % 7000] = stack1[i]; + } + stack5[top5++] = stack1[4]; + top1 = 0; + } + if (top5 == 12) + { + for (int i = 10; i >= 0; i--) + { + queue[rear = (rear + 1) % 7000] = stack5[i]; + } + stackh[toph++] = stack5[11]; + top5 = 0; + } + if (toph == 12) + { + for (int i = 10; i >= 0; i--) + { + queue[rear = (rear + 1) % 7000] = stackh[i]; + } + queue[rear = (rear + 1) % 7000] = stackh[11]; + toph = 0; + } + } + for (int i = 0, j = (front + 1) % 7000; i < n; i++, j = (j + 1) % 7000) + { + alter[queue[j]] = i; + } + for (int i = 0; i < n; i++) + if (!v[i]) + { + int len = 0, s = i; + do + { + v[s] = true; + s = alter[s]; + len++; + } while (!v[s]); + memset(temp, 0, sizeof(temp)); + for (int j = 0; j < no && len != 1; j++) + { + while (len % primes[j] == 0) + { + len /= primes[j]; + temp[j]++; + } + if (temp[j] > factor[j]) + { + factor[j] = temp[j]; + } + } + } + memset(ans, 0, sizeof(ans)); + ans[0] = 1; + for (int i = 0; i < no; i++) + { + for (int j = 0; j < factor[i]; j++) + { + int carry = 0; + for (int k = 0; k < 10; k++) + { + ans[k] = ans[k] * primes[i] + carry; + carry = ans[k] / 10000; + ans[k] %= 10000; + } + } + } + int p; + for (p = 9; ans[p] == 0; p--) + ; + printf("%d", ans[p]); + for (p--; p >= 0; p--) + { + printf("%04d", ans[p]); + } +} +int main() +{ + int n; + init(); + while (scanf("%d", &n) == 1, n) + { + printf("%d balls cycle after ", n); + solve(n); + puts(" days."); + } +} diff --git a/240.cpp b/240.cpp new file mode 100644 index 0000000..2d92b7a --- /dev/null +++ b/240.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int r, n, nn, p; +int pass[30][30]; +bool alive[30][30]; +int parent[30][30], weight[30][30]; +char small[30][30]; +int seq[30][30], l[30]; +double total, s; +int main() +{ + int caseno = 1; + while (scanf("%d", &r), r) + { + scanf("%d", &n); + s = total = 0; + memset(pass, 0, sizeof(pass)); + memset(alive, true, sizeof(alive)); + memset(parent, -1, sizeof(parent)); + memset(weight, -1, sizeof(weight)); + memset(small, 26 + 'A', sizeof(small)); + for (int i = 0; i < n; i++) + { + scanf("%d", &pass[0][i]); + s += pass[0][i]; + small[0][i] = i + 'A'; + } + int k = (int)ceil((double)(n - r) / (double)(r - 1)); + nn = k * (r - 1) + r; + for (p = 1; nn != 1; p++) + { + int sum = 0; + for (int i = 0; i < r; i++) + { + int min = 100000000, ptr; + for (int j = 0; j < nn; j++) + if (alive[p - 1][j]) + { + if ((pass[p - 1][j] < min) || (pass[p - 1][j] == min && small[p - 1][j] < small[p - 1][ptr])) + { + min = pass[p - 1][j]; + ptr = j; + } + } + alive[p - 1][ptr] = false; + weight[p - 1][ptr] = i; + parent[p - 1][ptr] = nn - r; + if (small[p - 1][ptr] < small[p][nn - r]) + { + small[p][nn - r] = small[p - 1][ptr]; + } + sum += pass[p - 1][ptr]; + } + int s = 0; + for (int i = 0; i < nn; i++) + if (alive[p - 1][i]) + { + pass[p][s] = pass[p - 1][i]; + small[p][s] = small[p - 1][i]; + weight[p - 1][i] = -1; + parent[p - 1][i] = s++; + } + pass[p][nn - r] = sum; + nn -= r - 1; + } + parent[p - 1][0] = -1; + for (int i = 0; i < n; i++) + { + l[i] = 0; + int x = i, k = 0; + while (parent[k][x] != -1) + { + if (weight[k][x] != -1) + { + seq[i][l[i]++] = weight[k][x]; + } + x = parent[k++][x]; + } + total += l[i] * pass[0][i]; + } + printf("Set %d; average length %.2lf\n", caseno++, + total / s); + for (int i = 0; i < n; i++) + { + printf(" %c: ", i + 'A'); + for (int j = l[i] - 1; j >= 0; j--) + { + printf("%d", seq[i][j]); + } + printf("\n"); + } + printf("\n"); + } +} diff --git a/242.cpp b/242.cpp new file mode 100644 index 0000000..bfc3b1e --- /dev/null +++ b/242.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +void find(const int &S, const int &N, const int stamp[], const int st[][10], int &choice, int &coverage, int &stnum); +int main() +{ + while (true) + { + int S, N; + scanf("%d", &S); + if (S == 0) + { + return 0; + } + scanf("%d", &N); + int stamp[N]; + int st[N][10]; + for (int i = 0; i < N; ++i) + { + scanf("%d", &stamp[i]); + for (int j = 0; j < stamp[i]; ++j) + { + scanf("%d", &st[i][j]); + } + } + int choice = 0; + int coverage = 0; + int stnum = 100; + //////////////////////////////////// + find(S, N, stamp, st, choice, coverage, stnum);// + //////////////////////////////////// + //cout<<"choice:"<= st[i][k]) + { + a[j] = min(a[j], a[j - st[i][k]] + 1); + } + } + } while (a[j++] <= S); + tcover = j - 2; + //cout< coverage) + { + coverage = tcover; + choice = i; + stnum = stamp[i]; + } + else if (tcover == coverage) + { + if (stamp[i] == stnum) + { + for (int j = 1; j < stamp[i] && j < stamp[choice]; j++) + { + if (st[i][stamp[i] - j] == st[choice][stamp[choice] - j]) + { + continue; + } + else if (st[i][stamp[i] - j] > st[choice][stamp[choice] - j]) + { + break; + } + else + { + choice = i; + break; + } + } + } + else if (stamp[i] < stnum) + { + choice = i; + stnum = stamp[i]; + } + } + } +} diff --git a/243.cpp b/243.cpp new file mode 100644 index 0000000..8869653 --- /dev/null +++ b/243.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +typedef struct dnode +{ + int point; + int flag1; + int flag2; + dnode *left; + dnode *right; +} dlink; +dlink Node[2000]; +dlink *Head[30]; + +int d_size; + +void dlink_inital(void) +{ + d_size = 0; + for (int i = 0; i < 26; ++i) + { + Head[i] = NULL; + } +} + +void dlink_add(int a, int b) +{ + if (!Head[a]) + { + Node[d_size].left = &Node[d_size]; + Node[d_size].right = &Node[d_size]; + Head[a] = &Node[d_size]; + } + else + { + Node[d_size].right = Head[a]; + Node[d_size].left = Head[a]->left; + Head[a]->left->right = &Node[d_size]; + Head[a]->left = &Node[d_size]; + } + Node[d_size].flag1 = 0; + Node[d_size].flag2 = 0; + Node[d_size].point = b; + d_size++; +} + +dlink *dlink_next(dlink *p, int id, int forword) +{ + dlink *t = Head[p->point]; + while (t->point != id) + { + t = t->right; + } + return (forword ? t->right : t->left); +} + +dlink *dlink_last(dlink *p) +{ + dlink *t = p, *m = p, *q; + for (q = t->right; t != q; q = q->right) + if (m->flag2 < q->flag2) + { + m = q; + } + return m; +} + +int visit[30]; + +int main() +{ + char a, b, c, d; + while ((a = getchar()) != '#') + { + dlink_inital(); + while (a != '@') + { + getchar(); + while ((b = getchar()) != '\n') + { + dlink_add(a - 'A', b - 'A'); + } + a = getchar(); + } + a = getchar(); + b = getchar(); + c = getchar(); + d = getchar(); + getchar(); + if ((a == c && b == d) || (a == d && b == c)) + { + printf("Theseus is killed between %c and %c\n", a, b); + continue; + } + for (int i = 0; i < 26; ++i) + { + visit[i] = 0; + } + int T = a - 'A', M = c - 'A', S; + dlink *p = Head[a - 'A']; + while (p->point != b - 'A') + { + p = p->right; + } + dlink *q = Head[c - 'A']; + while (q->point != d - 'A') + { + q = q->right; + } + int flagp = 1, flagq = 1, Mmark = 1; + while (1) + { + //Minotaur into cavern + S = M; + M = q->point; + q = dlink_next(q, S, 0); + if (visit[M] == 1) + { + q = dlink_next(q->right, M, 0); + M = S; + } + else + { + visit[M] = 2; + } + //Thseus into cavern + S = T; + T = p->point; + p = dlink_next(p, S, 1); + if (T == M)//meet in cavern + { + printf("The Minotaur is slain in %c\n", M + 'A'); + break; + } + //Thseus into passage + if (visit[T] == 2) + { + visit[T] = 1; + p = dlink_last(p); + } + else + while (p->flag1) + { + p = p->right; + } + p->flag1 = 1; + //Minotaur into passage + while (q->flag2) + { + q = q->left; + } + if (M == p->point && T == q->point)//meet in passage + { + printf("Theseus is killed between %c and %c\n", 'A' + T, 'A' + M); + break; + } + q->flag2 = Mmark++; + } + } + return 0; +} diff --git a/244.cpp b/244.cpp new file mode 100644 index 0000000..a0808d0 --- /dev/null +++ b/244.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +double dis[50], from[50], to[50]; +double v, s, m, acctime, accdis; + +double givetime(double d) +{ + return acctime * 2 + (d - accdis * 2) / v; +} + +int main() +{ + int t = 0; + while (true) + { + int end = 0, c = 0; + double a; + while (scanf("%lf", &a) == 1) + { + if (a == -1) + { + end = 1; + break; + } + if (a <= 1e-13) + { + break; + } + dis[++c] = a * 5280; + } + for (int i = c; i >= 1; i--) + { + dis[i] -= dis[i - 1]; + } + if (end) + { + break; + } + if (t) + { + printf("\n"); + } + scanf("%lf%lf%lf", &v, &s, &m); + acctime = v / s; + accdis = s * acctime * acctime / 2.0; + for (int i = 1; i <= c; i++) + { + from[i] = from[i - 1] + givetime(dis[i]) + m; + } + to[c] = 0; + for (int i = c - 1; i >= 0; i--) + { + to[i] = to[i + 1] + givetime(dis[i + 1]) + m; + } + printf("Scenario #%d:\n", ++t); + double disacu = 0; + for (int i = 0; i <= c; i++) + { + disacu += dis[i]; + if (fabs(from[i] - to[i]) <= m) + { + printf(" Meeting time: %.1lf minutes\n", max(to[i] - m, from[i] - m)); + printf(" Meeting distance: %.3lf miles from metro center hub, in station %d\n", disacu / 5280, i); + break; + } + else if (from[i] > to[i]) + { + disacu -= dis[i]; + double duration = (dis[i] - accdis * 2) / v; + double t1 = from[i - 1] + acctime; + double t2 = from[i - 1] + acctime + duration; + double t3 = to[i] + acctime; + double t4 = to[i] + acctime + duration; + double tt = 0.0f; + if (t1 >= t4) + { + tt = (acctime + t4 + from[i - 1]) / 2; + disacu += (tt - from[i - 1]) * (tt - from[i - 1]) * s / 2; + } + else if (t3 >= t2) + { + tt = (acctime + t2 + to[i]) / 2; + disacu += dis[i] - (from[i] - tt - m) * (from[i] - tt - m) * s / 2; + } + else + { + tt += (duration + t1 + t3) / 2; + disacu += accdis + (tt - t1) * v; + } + printf(" Meeting time: %.1lf minutes\n", tt); + printf(" Meeting distance: %.3lf miles from metro center hub\n", disacu / 5280); + break; + } + } + } + return 0; +} diff --git a/245.cpp b/245.cpp new file mode 100644 index 0000000..e4774ba --- /dev/null +++ b/245.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +// the original uncompressed file has no digit characters +// so we can safely assume that the word we read from the +// compressed file is a number by its first character +bool is_number(const string &s) +{ + return s[0] >= '0' && s[0] <= '9'; +} + +int string2int(const string &s) +{ + int result = 0; + for (int i = 0, sz = s.size(); i < sz; i++) + { + result = result * 10 + (s[i] - '0'); + } + return result; +} + +void process_current_word(string ¤t_word, list &words, char c) +{ + list::iterator it; + if (current_word.empty()) + { + cout << c; + } + else + { + if (is_number(current_word)) + { + int pos = string2int(current_word); + it = words.begin(); + advance(it, pos - 1); + cout << *it; + current_word = *it; + words.erase(it); + words.push_front(current_word); + } + else + { + cout << current_word; + words.push_front(current_word); + } + cout << c; + current_word = ""; + } +} + +int main() +{ + string input, current_word; + list words; + list::iterator it; + while (getline(cin, input)) + { + if (input == "0") + { + break; + } + current_word = ""; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i] <= 'Z' || input[i] >= '0' && input[i] <= '9') + { + current_word += input[i]; + } + else + { + process_current_word(current_word, words, input[i]); + } + } + process_current_word(current_word, words, '\n'); + } + return 0; +} diff --git a/246.cpp b/246.cpp new file mode 100644 index 0000000..4c438bf --- /dev/null +++ b/246.cpp @@ -0,0 +1,361 @@ +#include + +using namespace std; + +struct CARD +{ + int card[52]; + int num; + int top; + int buttom; +}; + +struct Trace +{ + int round; + struct CARD *Pile[7]; + struct CARD *Deck; + struct Trace *next; + struct Trace *end; +}; + +struct CARD pile[7]; +struct CARD deck; +struct Trace *trace[999999]; +int duplicate; + +void dealt_card_to_top(struct CARD *dst, struct CARD *src) +{ + if (dst->card[dst->top] != 0) + { + dst->top = (52 + dst->top - 1) % 52; + } + dst->card[dst->top] = src->card[src->top]; + src->top = (src->top + 1) % 52; + dst->num++; + src->num--; +} + +void dealt_card_to_buttom(struct CARD *dst, struct CARD *src) +{ + dst->buttom = (dst->buttom + 1) % 52; + dst->card[dst->buttom] = src->card[src->top]; + src->top = (src->top + 1) % 52; + dst->num++; + src->num--; +} + +void insert_card_to_top(struct CARD *dst, struct CARD *src) +{ + dst->top = (52 + dst->top - 1) % 52; + dst->card[dst->top] = src->card[src->buttom]; + src->buttom = (52 + src->buttom - 1) % 52; + dst->num++; + src->num--; +} + +void insert_card_to_buttom(struct CARD *dst, struct CARD *src) +{ + dst->buttom = (dst->buttom + 1) % 52; + dst->card[dst->buttom] = src->card[src->buttom]; + src->buttom = (52 + src->buttom - 1) % 52; + dst->num++; + src->num--; +} + +int hash_of_trace() +{ + int Hash = 0; + int i, j; + for (i = 0; i < 7; i++) + { + for (j = 0; j < pile[i].num; j++) + { + Hash += pile[i].card[(pile[i].top + j) % 52]; + } + Hash *= 10; + } + for (j = 0; j < deck.num; j++) + { + Hash += deck.card[(deck.top + j) % 52]; + } + if (Hash < 0) + { + Hash = Hash * -1; + } + return Hash % 999999; +} + +bool check_duplicate() +{ + int i, j; + int Hash; + int check; + struct Trace *T_ptr; + Hash = hash_of_trace(); + T_ptr = trace[Hash]; + for (; T_ptr != NULL; T_ptr = T_ptr->next) + { + check = 0; + for (i = 0; i < 7; i++) + { + for (j = 0; j < pile[i].num && pile[i].num == T_ptr->Pile[i]->num; j++) + { + if (pile[i].card[(pile[i].top + j) % 52] != T_ptr->Pile[i]->card[(T_ptr->Pile[i]->top + j) % 52]) + { + check = 1; + break; + } + } + if (pile[i].num != T_ptr->Pile[i]->num) + { + check = 1; + } + if (check == 1) + { + break; + } + } + for (j = 0; j < deck.num && deck.num == T_ptr->Deck->num; j++) + { + if (deck.card[(deck.top + j) % 52] != T_ptr->Deck->card[(T_ptr->Deck->top + j) % 52]) + { + check = 1; + break; + } + if (check == 1) + { + break; + } + } + if (deck.num != T_ptr->Deck->num) + { + check = 1; + } + if (check == 0) + { + duplicate = T_ptr->round; + return true; + } + } + return false; +} + +void add_trace_data(int round) +{ + int i; + int hash; + struct CARD *P[7], *D; + struct Trace *T_ptr, *create; + for (i = 0; i < 7; i++) + { + P[i] = (struct CARD *)malloc(sizeof(struct CARD)); + memcpy(P[i], &pile[i], sizeof(struct CARD)); + } + D = (struct CARD *)malloc(sizeof(struct CARD)); + memcpy(D, &deck, sizeof(struct CARD)); + hash = hash_of_trace(); + create = (struct Trace *)malloc(sizeof(struct Trace)); + create->round = round; + create->Deck = D; + for (i = 0; i < 7; i++) + { + create->Pile[i] = P[i]; + } + create->next = NULL; + create->end = NULL; + T_ptr = trace[hash]; + if (trace[hash] == NULL) + { + trace[hash] = create; + trace[hash]->end = create; + } + else + { + trace[hash]->end->next = create; + trace[hash]->end = trace[hash]->end->next; + } +} + +void free_trace_data() +{ + struct Trace *tmp, *T_ptr; + int k; + int i, j; + for (i = 0; i < 999999; i++) + { + if (trace[i] == NULL) + { + continue; + } + T_ptr = trace[i]; + while (trace[i] != NULL) + { + T_ptr = trace[i]; + trace[i] = trace[i]->next; + free(T_ptr->Deck); + for (j = 0; j < 7; j++) + { + free(T_ptr->Pile[j]); + } + free(T_ptr); + } + free(trace[i]); + trace[i] = NULL; + } +} + +int check_over() +{ + if (deck.num == 0) + { + return 1; + } + else if (deck.num == 52) + { + return 2; + } + else if (duplicate != 0) + { + return 3; + } + return 0; +} + +int main() +{ + int i, j, k; + int first, second, last, last_2, last_3; + int tmp; + int counter; + int round; + int check_num; + FILE *in; + in = stdin;//fopen("246.in","r"); + while (fscanf(in, "%d", &tmp) != EOF) + { + duplicate = 0; + //memset( pile , 0 , sizeof(struct CARD)*7 ); + //memset( &deck , 0 , sizeof(struct CARD) ); + deck.num = 0; + deck.top = 0; + deck.buttom = 0; + for (i = 0; i < 52; i++) + { + deck.card[i] = 0; + } + for (i = 0; i < 7; i++) + { + pile[i].num = 0; + pile[i].top = 0; + pile[i].buttom = 0; + for (j = 0; j < 52; j++) + { + pile[i].card[j] = 0; + } + } + for (i = 0; i < 999999; i++) + { + trace[i] = NULL; + } + if (tmp == 0) + { + break; + } + i = 0; + deck.card[i] = tmp; + for (i = 1; i < 52; i++) + { + fscanf(in, "%d", &tmp); + deck.card[i] = tmp; + } + // init + deck.top = 0; + deck.buttom = 51; + deck.num = 52; + // first two card in each pile + for (i = 0; i < 14; i++) + { + dealt_card_to_top(&(pile[i % 7]), &deck); + } + round = 14; + check_num = 16; + while (!check_over()) + { + for (i = 0; i < 7 && !check_over(); i++) + { + if (pile[i].num != 0) + { + dealt_card_to_top(&pile[i], &deck); + round++; + } + while (pile[i].num >= 3) + { + first = pile[i].buttom; + second = (pile[i].buttom - 1 + 52) % 52; + last = pile[i].top; + last_2 = (pile[i].top + 1) % 52; + last_3 = (pile[i].top + 2) % 52; + if ((pile[i].card[first] + pile[i].card[second] + pile[i].card[last]) % 10 == 0) + { + insert_card_to_buttom(&deck, &(pile[i]));//first + insert_card_to_buttom(&deck, &(pile[i]));//second + dealt_card_to_buttom(&deck, &(pile[i])); //last + } + else if ((pile[i].card[first] + pile[i].card[last] + pile[i].card[last_2]) % 10 == 0) + { + insert_card_to_buttom(&deck, &(pile[i]));//first + dealt_card_to_buttom(&deck, &(pile[i])); //last + dealt_card_to_buttom(&deck, &(pile[i])); //last2 + tmp = deck.card[(52 + deck.buttom - 1) % 52]; + deck.card[(52 + deck.buttom - 1) % 52] = deck.card[deck.buttom]; + deck.card[deck.buttom] = tmp; + } + else if ((pile[i].card[last] + pile[i].card[last_2] + pile[i].card[last_3]) % 10 == 0) + { + dealt_card_to_buttom(&deck, &(pile[i]));//last + dealt_card_to_buttom(&deck, &(pile[i]));//last2 + dealt_card_to_buttom(&deck, &(pile[i]));//last3 + tmp = deck.card[(52 + deck.buttom - 2) % 52]; + deck.card[(52 + deck.buttom - 2) % 52] = deck.card[deck.buttom]; + deck.card[deck.buttom] = tmp; + } + else + { + break; + } + } + if (pile[i].num != 0) + { + if (check_duplicate() == true) + { + // printf("duplicate!\n"); + break; + } + //if ( check_num == round || round < 16 ){ + add_trace_data(round); + // if ( check_num == round ) + // check_num = check_num*2; + //} + } + } + if (duplicate != 0) + { + break; + } + } + tmp = check_over(); + switch (tmp) + { + case 1: + printf("Loss: %d\n", round); + break; + case 2: + printf("Win : %d\n", round); + break; + case 3: + printf("Draw: %d\n", round); + } + free_trace_data(); + } +} diff --git a/247.cpp b/247.cpp new file mode 100644 index 0000000..92bfd5e --- /dev/null +++ b/247.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +/* +247 +Calling Circles +*/ +#define MAXN 30 + +char S[MAXN][30]; +char flg[MAXN]; +char links[MAXN][MAXN]; +int N, E; +map M; +void Set() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + links[i][j] = links[j][i] = 0; + } + links[i][i] = 0; + } +} +void Dfs(int n, int k) +{ + int i; + flg[n] = 1; + links[k][n] = 1; + for (i = 1; i <= N; i++) + { + if (flg[i] == 0 && links[n][i]) + { + Dfs(i, k); + } + } +} +void Circle(int n) +{ + int i; + for (i = 1; i <= N; i++) + { + if (flg[i] || i == n) + { + continue; + } + if (links[i][n] && links[n][i]) + { + printf(", %s", S[i]); + flg[i] = 1; + } + } +} +void Cal() +{ + int i, j, k; + for (i = 1; i <= N; i++) + { + Dfs(i, i); + memset(flg, 0, MAXN); + } + for (i = 1; i <= N; i++) + { + if (flg[i] == 1) + { + continue; + } + printf("%s", S[i]); + flg[i] = 1; + Circle(i); + printf("\n"); + } + memset(flg, 0, MAXN); +} +int main() +{ + char ss[30], xx[30]; + int ind, u, v, st = 1; + while (scanf("%d%d", &N, &E) == 2) + { + ind = 1; + if (!N && !E) + { + break; + } + Set(); + for (int i = 0; i < E; i++) + { + scanf("%s%s", ss, xx); + u = M[ss]; + if (!u) + { + M[ss] = ind++; + u = ind - 1; + strcpy(S[ind - 1], ss); + } + v = M[xx]; + if (!v) + { + M[xx] = ind++; + v = ind - 1; + strcpy(S[ind - 1], xx); + } + links[u][v] = 1; + } + if (st > 1) + { + printf("\n"); + } + printf("Calling circles for data set %d:\n", st++); + Cal(); + M.clear(); + } + return 0; +} diff --git a/248.cpp b/248.cpp new file mode 100644 index 0000000..29502a7 --- /dev/null +++ b/248.cpp @@ -0,0 +1,191 @@ +#include + +using namespace std; + +#define FOREACH(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) + +double EPS = 1e-9; + +struct Point +{ + double x, y; + Point() {} + Point(double xx, double yy) : x(xx), y(yy) {} + bool operator<(const Point &p) const + { + return x == p.x ? (y < p.y) : (x < p.x); + } +}; + +Point operator-(const Point &a, const Point &b) +{ + return Point(a.x - b.x, a.y - b.y); +} +Point operator+(const Point &a, const Point &b) +{ + return Point(a.x + b.x, a.y + b.y); +} +double dot(const Point &a, const Point &b) +{ + return a.x * b.x + a.y * b.y; +} +double dist(const Point &a, const Point &b) +{ + return sqrt(dot(a - b, a - b)); +} + +struct Rect +{ + Point p[5]; + + void solve() + { + sort(p, p + 3); + while (true) + { + if (fabs(dot(p[0] - p[1], p[2] - p[1])) < EPS) + { + break; + } + assert(next_permutation(p, p + 3)); + } + p[3] = p[0] + p[2] - p[1]; + p[4] = p[0]; + double area = 0; + for (int i = 0; i < 4; i++) + { + area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y; + } + if (area < 0) + { + reverse(p, p + 4); + p[4] = p[0]; + } + } + + bool inside(Point q) + { + for (int i = 0; i < 4; i++) + { + double cross = (p[i].x - q.x) * (p[i + 1].y - q.y) - (p[i + 1].x - q.x) * (p[i].y - q.y); + if (cross < EPS) + { + return false; + } + } + return true; + } +}; + +Rect rect[100]; +int RN; + +bool same(Point &A, Point &B) +{ + return fabs(A.x - B.x) < EPS && fabs(A.y - B.y) < EPS; +} + +bool visible(Point A, Point B) +{ + vector w; + w.push_back(0); + w.push_back(1); + for (int i = 0; i < RN; i++) + { + for (int j = 0; j < 4; j++) + { + Point C = rect[i].p[j]; + Point D = rect[i].p[j + 1]; + if (same(A, C) || same(A, D) || same(B, C) || same(B, D)) + { + continue; + } + // (B-A)t + (C-D)s = C-A + double a1 = B.x - A.x, b1 = C.x - D.x, c1 = C.x - A.x; + double a2 = B.y - A.y, b2 = C.y - D.y, c2 = C.y - A.y; + double det = a1 * b2 - a2 * b1; + if (fabs(det) < EPS) + { + continue; + } + double t = (c1 * b2 - c2 * b1) / det; + double s = (a1 * c2 - a2 * c1) / det; + if (t < EPS || t > 1 - EPS) + { + continue; + } + if (fabs(s) < EPS || fabs(s - 1) < EPS) + { + w.push_back(t); + } + if (s < EPS || s > 1 - EPS) + { + continue; + } + return false; + } + } + sort(w.begin(), w.end()); + for (int j = 1; j < (int)w.size(); j++) + { + if (fabs(w[j] - w[j - 1]) < EPS) + { + continue; + } + double t = (w[j] + w[j - 1]) / 2; + Point M(A.x + (B.x - A.x) * t, A.y + (B.y - A.y) * t); + for (int i = 0; i < RN; i++) + { + if (rect[i].inside(M)) + { + return false; + } + } + } + //printf("A=%.2f,%.2f B=%.2f,%.2f\n", A.x, A.y, B.x,B.y); + return true; +} + +int main() +{ + double G[128][128]; + int GN; + Point V[128]; + for (int cs = 1; scanf("%d", &RN) == 1 && RN >= 0; cs++) + { + scanf("%lf %lf %lf %lf", &V[0].x, &V[0].y, &V[1].x, &V[1].y); + for (int i = 0; i < RN; i++) + { + for (int j = 0; j < 3; j++) + { + scanf("%lf %lf", &rect[i].p[j].x, &rect[i].p[j].y); + } + rect[i].solve(); + for (int j = 0; j < 4; j++) + { + V[2 + 4 * i + j] = rect[i].p[j]; + } + } + GN = 2 + 4 * RN; + for (int i = 0; i < GN; i++) + { + G[i][i] = 0; + for (int j = i + 1; j < GN; j++) + { + G[i][j] = G[j][i] = visible(V[i], V[j]) ? dist(V[i], V[j]) : 1e99; + } + } + for (int k = 0; k < GN; k++) + for (int i = 0; i < GN; i++) + for (int j = 0; j < GN; j++) + { + G[i][j] = min(G[i][j], G[i][k] + G[k][j]); + } + if (cs != 1) + { + printf("\n"); + } + printf("Scenario #%d\n", cs); + printf(" route distance: %.2f\n", G[0][1]); + } +} diff --git a/250.cpp b/250.cpp new file mode 100644 index 0000000..b23a46d --- /dev/null +++ b/250.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +double ary[30][30]; + +double ab(double now) +{ + if (now < 0) + { + return -now; + } + return now; +} + +int main() +{ + int cas, n, m, i, j, x, y; + double d, diff; + cas = 0; + while (scanf("%d%d", &n, &m) == 2) + { + if (!n && !m) + { + break; + } + for (i = 1; i <= n; i++) + { + for (j = 1; j <= m; j++) + { + scanf("%lf", &ary[i][j]); + ary[i][j] += ary[i - 1][j] + ary[i][j - 1] - ary[i - 1][j - 1]; + } + } + for (i = n, diff = 1e30; i >= 1; i--) + { + d = ab(ary[i - 1][m] - ary[n][m] + ary[i][m]); + if (d + 1e-8 < diff) + { + diff = d; + x = i; + } + } + for (i = m, diff = 1e30; i >= 1; i--) + { + d = ab(ary[n][i - 1] - ary[n][m] + ary[n][i]); + if (d + 1e-8 < diff) + { + diff = d; + y = i; + } + } + printf("Case %d: center at (%d, %d)\n", ++cas, x, y); + } + return 0; +} diff --git a/251.cpp b/251.cpp new file mode 100644 index 0000000..31ba7d5 --- /dev/null +++ b/251.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +int tab[16][16][16], n, g; + +int rec(int *s) +{ + int t[16], a[16], i; + if (s[1] < 0) + { + return (s[0] >= (n - g)); + } + for (i = 0; s[i] >= 0; i++) + { + a[i] = 0; + } + for (;;) + { + for (i = 0; s[i + 1] >= 0; i++) + { + t[i] = tab[s[i]][s[i + 1]][a[i]]; + } + t[i] = -1; + if (rec(t)) + { + return 1; + } + for (i = 0;; i++) + { + if (s[i + 1] < 0) + { + return 0; + } + else if (tab[s[i]][s[i + 1]][++a[i]] < 0) + { + a[i] = 0; + } + else + { + break; + } + } + } +} + +int solve(int *s) +{ + static int r[16][16], f[16][16]; + int i, j, k, m; + memset(r, 0, sizeof(r)); + memset(f, 0, sizeof(f)); + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + for (k = 0; tab[i][j][k] >= 0; k++) + { + r[i][j] |= 1 << tab[i][j][k]; + } + for (m = 0; s[m] >= 0; m++) + ; + for (i = 0; i < m; i++) + { + f[m][i] |= 1 << s[i]; + } + while (m-- > 1) + { + for (k = 0; k < m; k++) + { + for (i = 0; i < n; i++) + { + if (((f[m + 1][k] >> i) & 1) == 0) + { + continue; + } + for (j = 0; j < n; j++) + if (((f[m + 1][k + 1] >> j) & 1) == 1) + { + f[m][k] |= r[i][j]; + } + } + } + } + if ((f[1][0] >> (n - g)) == 0) + { + return 0; + } + return rec(s); +} + +int main() +{ + char s[256]; + int a[256], i, j, k, t; + for (t = 1; scanf("%d %d", &n, &g) == 2 && n > 0; t++) + { + printf("%sNTA %d\n", (t == 1) ? "" : "\n", t); + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + scanf(" %s", s); + for (k = 0; s[k]; k++) + { + tab[i][j][k] = s[k] - 'a'; + } + tab[i][j][k] = -1; + } + } + while (scanf(" %s", s) == 1 && s[0] != '#') + { + for (i = 0; s[i]; i++) + { + a[i] = s[i] - 'a'; + } + a[i] = -1; + printf("%s %s\n", solve(a) ? "accept" : "reject", s); + } + } + return 0; +} diff --git a/253.cpp b/253.cpp new file mode 100644 index 0000000..75751d5 --- /dev/null +++ b/253.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ + +void check(char fc[], char sc[], bool &over); +void exam(char fc[], char sc[]); +int main() +{ + while (true) + { + char fc[6], sc[6]; + for (int i = 0; i < 6; i++) + { + cin >> fc[i]; + if (cin.eof()) + { + return 0; + } + } + for (int i = 0; i < 6; i++) + { + cin >> sc[i]; + } + exam(fc, sc); + } +} +void check(char fc[], char sc[], bool &over) +{ + if (sc[0] == fc[0]) + { + if (sc[5] == fc[5]) + { + if (sc[1] == fc[1] && sc[2] == fc[2] && sc[3] == fc[3] && sc[4] == fc[4]) + { + cout << "TRUE" << endl; + over = !over; + return; + } + if (sc[1] == fc[2] && sc[2] == fc[4] && sc[3] == fc[1] && sc[4] == fc[3]) + { + cout << "TRUE" << endl; + over = !over; + return; + } + if (sc[1] == fc[3] && sc[2] == fc[1] && sc[3] == fc[4] && sc[4] == fc[2]) + { + cout << "TRUE" << endl; + over = !over; + return; + } + if (sc[1] == fc[4] && sc[2] == fc[3] && sc[3] == fc[2] && sc[4] == fc[1]) + { + cout << "TRUE" << endl; + over = !over; + return; + } + } + } +} +void exam(char fc[], char sc[]) +{ + char temp; + bool over = false; + check(fc, sc, over); + for (int i = 0; i < 4; i++) + { + if (!over) + { + temp = sc[0]; + sc[0] = sc[1]; + sc[1] = sc[5]; + sc[5] = sc[4]; + sc[4] = temp; + check(fc, sc, over); + } + } + for (int i = 0; i < 3; i++) + { + if (!over) + { + temp = sc[0]; + sc[0] = sc[3]; + sc[3] = sc[5]; + sc[5] = sc[2]; + sc[2] = temp; + check(fc, sc, over); + } + } + if (!over) + { + cout << "FALSE" << endl; + } + return; +} diff --git a/254.cpp b/254.cpp new file mode 100644 index 0000000..ba6244e --- /dev/null +++ b/254.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +char s[128] = {'\0'}; +int n, b[128] = {0}, lb = 0; +void Transform(int len) +{ + int a[128] = {0}; + lb = 0; + for (int i = 0; i < len; i++) + { + a[i] = s[len - 1 - i] - '0'; + } + while (len) + { + int mod = 0, mul; + for (int i = len - 1; i >= 0; i--) + { + mul = mod * 10 + a[i]; + a[i] = mul >> 1; + mod = mul & 1; + } + b[lb++] = mod; + if (!a[len - 1]) + { + len--; + } + } +} +int main() +{ + int tower1 = 0, tower2 = 0, tower3 = 0, tmp; + while (scanf("%d %s", &n, s) == 2) + { + if (!n && s[0] == '0') + { + break; + } + Transform(strlen(s)); + tower1 = 0; + tower2 = 0; + tower3 = 0; + for (int i = 0; i < lb; i++) + { + if (b[i])//b[i]==1 To tower 3 ,swap(tower1,tower2) + { + tower3++; + tmp = tower1; + tower1 = tower2; + tower2 = tmp; + } + else//b[i]==0 To towe 1 ,swap(tower2,tower3) + { + tower1++; + tmp = tower2; + tower2 = tower3; + tower3 = tmp; + } + } + int left = n - lb; + if (left & 1) + { + tmp = tower2; + tower2 = tower3; + tower3 = tmp; + } + tower1 += left; + if (n & 1) + { + printf("%d %d %d\n", tower1, tower3, tower2); + } + else + { + printf("%d %d %d\n", tower1, tower2, tower3); + } + } + return 0; +} diff --git a/255.cpp b/255.cpp new file mode 100644 index 0000000..96ade15 --- /dev/null +++ b/255.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +int K, Q, M, kr, kc, qr, qc, mr, mc; +int X[] = {-1, 1, 0, 0}; +int Y[] = {0, 0, -1, 1}; + +int illegal() +{ + return K == Q; +} +int illegal_mv() +{ + int i, max_, min_; + if (M == Q) + { + return 1; + } + if (qr != mr && qc != mc) + { + return 1; + } + if (qr != kr && qc != kc) + { + return 0; + } + max_ = max(Q, M); + min_ = Q + M - max_; + if (qr == mr) + { + for (i = min_; i <= max_; i++) + { + if (i == K) + { + return 1; + } + } + } + else if (qc == mc) + { + for (i = min_; i <= max_; i += 8) + { + if (i == K) + { + return 1; + } + } + } + return 0; +} +int Not_allowed() +{ + int i, j, r, c; + char F[8][8]; + for (i = 0; i < 8; i++) + { + for (j = 0; j < 8; j++) + { + F[i][j] = 0; + } + } + for (i = qr - 1; i >= 0 && (kc != qc || i != kr); i--) + { + F[i][qc]++; + } + for (i = qr + 1; i < 8 && (kc != qc || i != kr); i++) + { + F[i][qc]++; + } + for (i = qc - 1; i >= 0 && (kr != qr || i != kc); i--) + { + F[qr][i]++; + } + for (i = qc + 1; i < 8 && (kr != qr || i != kc); i++) + { + F[qr][i]++; + } + for (i = 0; i < 4; i++) + { + r = X[i] + kr; + c = Y[i] + kc; + if (r >= 8 || r < 0 || c >= 8 || c < 0) + { + continue; + } + F[r][c]++; + } + return F[mr][mc] > 1; +} +int Continue() +{ + int i, j, r, c; + char F[8][8]; + for (i = 0; i < 8; i++) + { + for (j = 0; j < 8; j++) + { + F[i][j] = 0; + } + } + for (i = mr - 1; i >= 0 && (kc != mc || i != kr); i--) + { + F[i][mc]++; + } + for (i = mr + 1; i < 8 && (kc != mc || i != kr); i++) + { + F[i][mc]++; + } + for (i = mc - 1; i >= 0 && (kr != mr || i != kc); i--) + { + F[mr][i]++; + } + for (i = mc + 1; i < 8 && (kr != mr || i != kc); i++) + { + F[mr][i]++; + } + for (i = 0; i < 4; i++) + { + r = X[i] + kr; + c = Y[i] + kc; + if (r >= 8 || r < 0 || c >= 8 || c < 0) + { + continue; + } + F[r][c]++; + if (F[r][c] < 2) + { + return 1; + } + } + return 0; +} +void Cal() +{ + if (illegal()) + { + printf("Illegal state\n"); + return; + } + else if (illegal_mv()) + { + printf("Illegal move\n"); + return; + } + else if (Not_allowed()) + { + printf("Move not allowed\n"); + return; + } + else if (Continue()) + { + printf("Continue\n"); + } + else + { + printf("Stop\n"); + } +} +void Ini() +{ + kr = K / 8; + kc = K % 8; + qr = Q / 8; + qc = Q % 8; + mr = M / 8; + mc = M % 8; +} + +int main() +{ + while (scanf("%d%d%d", &K, &Q, &M) == 3) + { + Ini(); + Cal(); + } + return 0; +} diff --git a/256.cpp b/256.cpp index b9374da..933e849 100644 --- a/256.cpp +++ b/256.cpp @@ -1,19 +1,28 @@ -#include -#include -#include -#include -using namespace std; -int main(){ - int n; - while(scanf("%d",&n)!=EOF){ - if(n==2) - printf("00\n01\n81\n"); - if(n==4) - printf("0000\n0001\n2025\n3025\n9801\n"); - if(n==6) - printf("000000\n000001\n088209\n494209\n998001\n"); - if(n==8) - printf("00000000\n00000001\n04941729\n07441984\n24502500\n25502500\n52881984\n60481729\n99980001\n"); - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int n; + while (scanf("%d", &n) == 1) + { + if (n == 2) + { + printf("00\n01\n81\n"); + } + else if (n == 4) + { + printf("0000\n0001\n2025\n3025\n9801\n"); + } + else if (n == 6) + { + printf("000000\n000001\n088209\n494209\n998001\n"); + } + else if (n == 8) + { + printf("00000000\n00000001\n04941729\n07441984\n24502500\n25502500\n52881984\n60481729\n99980001\n"); + } + } + return 0; +} diff --git a/257.cpp b/257.cpp new file mode 100644 index 0000000..0cfb0ae --- /dev/null +++ b/257.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +char line[1024]; +set palin; + +int isPalin(int l, int r) +{ + while (l < r && line[l] == line[r]) + { + l++, r--; + } + if (l >= r) + { + return 1; + } + return 0; +} + +int main() +{ + while (scanf("%s", line) == 1) + { + int str_len = strlen(line); + string str(line); + palin.clear(); + // int counter=0; + for (int len = 3; len <= 4; len++)//len<=4 !! + { + int s = 0, e = len - 1; + if (e >= str_len /*|| counter==2*/) + { + break; + } + // int found=0; + for (; e < str_len; s++, e++) + { + if (!isPalin(s, e)) + { + continue; + } + string now(str.substr(s, len)); + if (palin.find(now) != palin.end()) + { + continue; + } + // found++; + // for (int i=palin.size()-1; i>=0; i--) + set::iterator i; + for (i = palin.begin(); i != palin.end(); ++i) + { + // printf("[%s,%s]",now.c_str(),palin[i].c_str()); + // if (now.find(palin[i])>=len) + if (now.find(*i) >= len) + { + printf("%s\n", line); + goto End; + } + } + // printf("[%s]",now.c_str()); + // palin.push_back(now); + palin.insert(now); + } + // if (found==0) counter++; + } + // printf("\n"); + End: + str_len--; + } + return 0; +} diff --git a/258.cpp b/258.cpp new file mode 100644 index 0000000..482fc43 --- /dev/null +++ b/258.cpp @@ -0,0 +1,222 @@ +#include + +using namespace std; + +#define fr(a,b,c) for(int a = b; a < c; a++) +#define rp(a,b) fr(a,0,b) +#define cl(a,b) memset(a,b,sizeof a) +#define mp make_pair +#define F first +#define S second + +typedef pair pii; + +const int di[] = {1, 0, -1, 0}; +const int dj[] = {0, 1, 0, -1}; +const char dc[] = {'\\', '/'}; + +bool vis[100][100][5]; +char g[100][100]; +int n, m, sz, pr[100][100]; +pii to[100][100][5]; +pair ns[3]; + +bool valid(int i, int j) +{ + return i >= 0 && i < n && j >= 0 && j < m; +} + +bool dfs(int i, int j, int d, bool ini = 0) +{ + vis[i][j][d] = 1; + if ((i == 0 || i == n - 1 || j == 0 || j == m - 1) && !ini) + { + if (g[i][j] == '.') + { + return 1; + } + if ((i == 0 && j == 0) || (i == 0 && j == m - 1) || (i == n - 1 && j == 0) || (i == n - 1 && j == m - 1)) + { + return 1; + } + if (i == 0) + { + if (d == 1) + { + g[i][j] = '/'; + } + else + { + g[i][j] = '\\'; + } + } + else if (i == n - 1) + { + if (d == 1) + { + g[i][j] = '\\'; + } + else + { + g[i][j] = '/'; + } + } + else if (j == 0) + { + if (d == 0) + { + g[i][j] = '/'; + } + else + { + g[i][j] = '\\'; + } + } + else + { + if (d == 0) + { + g[i][j] = '\\'; + } + else + { + g[i][j] = '/'; + } + } + return 1; + } + + int k = (d + 1) & 3; + int ni = to[i][j][k].F; + int nj = to[i][j][k].S; + if (ni != -1 && !vis[ni][nj][k] && (pr[i][j] == -1 || pr[i][j] == (d & 1))) + { + if (g[i][j] != '.') + { + g[i][j] = dc[d & 1]; + } + pr[i][j] = d & 1; + if (dfs(ni, nj, k)) + { + return 1; + } + pr[i][j] = -1; + } + k = (d + 3) & 3; + ni = to[i][j][k].F; + nj = to[i][j][k].S; + if (ni != -1 && !vis[ni][nj][k] && (pr[i][j] == -1 || pr[i][j] == (!(d & 1)))) + { + if (g[i][j] != '.') + { + g[i][j] = dc[!(d & 1)]; + } + pr[i][j] = !(d & 1); + if (dfs(ni, nj, k)) + { + return 1; + } + pr[i][j] = -1; + } + vis[i][j][d] = 0; + return 0; +} + +int main() +{ + bool ok = 0; + while (scanf("%d%d", &m, &n) == 2 && m != -1) + { + rp(i, n) scanf("%s", g[i]); + if (g[0][0] != '*' && g[0][0] != '.') + { + g[0][0] = '\\'; + } + if (g[n - 1][0] != '*' && g[n - 1][0] != '.') + { + g[n - 1][0] = '/'; + } + if (g[0][m - 1] != '*' && g[0][m - 1] != '.') + { + g[0][m - 1] = '/'; + } + if (g[n - 1][m - 1] != '*' && g[n - 1][m - 1] != '.') + { + g[n - 1][m - 1] = '\\'; + } + sz = 0; + rp(i, n) + { + if (g[i][0] != '*') + { + ns[sz++] = mp(mp(i, 0), 1); + } + if (g[i][m - 1] != '*') + { + ns[sz++] = mp(mp(i, m - 1), 3); + } + } + rp(j, m) + { + if (g[0][j] != '*') + { + ns[sz++] = mp(mp(0, j), 0); + } + if (g[n - 1][j] != '*') + { + ns[sz++] = mp(mp(n - 1, j), 2); + } + } + rp(i, n) rp(j, m) + { + if (g[i][j] == '/' || g[i][j] == '\\') + { + rp(k, 4) + { + int ni = i + di[k], nj = j + dj[k]; + while (valid(ni, nj) && g[ni][nj] == '.') + { + ni += di[k], nj += dj[k]; + } + if (!valid(ni, nj)) + { + ni -= di[k], nj -= dj[k]; + } + to[i][j][k] = mp(-1, -1); + if (valid(ni, nj) && g[ni][nj] != '*' && (ni != i || nj != j)) + { + to[i][j][k] = mp(ni, nj); + } + } + } + } + cl(vis, 0); + cl(pr, -1); + int ni = ns[0].F.F, nj = ns[0].F.S, k = ns[0].S; + while (valid(ni, nj) && g[ni][nj] == '.') + { + ni += di[k], nj += dj[k]; + } + if (valid(ni, nj)) + { + dfs(ni, nj, k, 1); + } + if (ok) + { + printf("\n"); + } + else + { + ok = 1; + } + rp(i, n) + { + rp(j, m) + { + printf("%c", g[i][j]); + } + printf("\n"); + } + } + return 0; +} diff --git a/259.cpp b/259.cpp new file mode 100644 index 0000000..1167d1a --- /dev/null +++ b/259.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +char str[100]; +struct ss {int Job[30], ind;} com[12]; +int F[30], T, Count[30], A[12]; + +void Reset() +{ + int i; + for (i = 0; i < 10; i++) + { + F[i] = com[i].ind = Count[i] = 0; + } + for (i; i < 30; i++) + { + F[i] = Count[i] = 0; + } +} +void Set() +{ + int i, j, k, m; + k = str[0] - 'A'; + m = str[1] - '0'; + F[k] += m; + T += m; + for (i = 3; str[i] != ';'; i++) + { + j = str[i] - '0'; + com[j].Job[com[j].ind++] = k; + } +} +void Print() +{ + int i, j; + for (i = 1; i < 11; i++) + { + j = A[i]; + if (j == 28) + printf("_"); + else + printf("%c", j + 'A'); + } + printf("\n"); +} +int BackTrak(int n, int level, int total, int cm) +{ + int i, j, m, k; + A[level] = cm; + if (level == 10) + { + if (T == total) + { + Print(); + return 1; + } + return 0; + } + Count[cm]++; + for (i = n + 1; i < 10; i++) + { + for (j = 0; j < com[i].ind; j++) + { + m = com[i].Job[j]; + k = total; + if (m != 28) + { + k = total + 1; + } + if (Count[m] < F[m] || m == 28) + { + if (BackTrak(i, level + 1, k, m)) + { + return 1; + } + } + } + } + Count[cm]--; + return 0; +} +void Cal() +{ + int i, j; + for (i = 0; i < 10; i++) + { + com[i].Job[com[i].ind++] = 28; + } + if (T > 10) + { + printf("!\n"); + return; + } + j = BackTrak(-1, 0, 0, 29); + if (!j) + { + printf("!\n"); + } +} + +int main() +{ + int i; + while (gets(str)) + { + T = 0; + Set(); + while (gets(str)) + { + for (i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = '\0'; + break; + } + } + if (strlen(str) == 0) + { + break; + } + Set(); + } + Cal(); + Reset(); + } + return 0; +} diff --git a/260.cpp b/260.cpp index 087b47e..017e696 100644 --- a/260.cpp +++ b/260.cpp @@ -1,86 +1,111 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -int N; -char adjMat[205][205]; -bool visit[205][205]; -char output; +using namespace std; -void dfs(int X, int Y, char cur){ - if ( X < 0 || Y < 0 || X >= N || Y >= N ) - return; - if ( adjMat[X][Y] != cur || visit[X][Y] ) - return; - - if( X == N-1 && adjMat[X][Y] == 'b' ){ - output = 'B'; - return; +/************** +IL GIOCO DELL'X + 260 +***************/ +#define MAXN 205 +#define MAX 1000 +/*10000*/ +int R[] = {-1, -1, 0, 0, 1, 1}; +int C[] = {-1, 0, -1, 1, 0, 1}; +char Game[MAXN][MAXN]; +int QR[MAX], QC[MAX]; +int QHR, QTR, QHC, QTC, N, Kase; +void PushR(int r) +{ + QR[QHR++] = r; + QHR %= MAX; +} +int PopR() +{ + int r = QR[QTR++]; + QTR %= MAX; + return r; +} +void PushC(int r) +{ + QC[QHC++] = r; + QHC %= MAX; +} +int PopC() +{ + int r = QC[QTC++]; + QTC %= MAX; + return r; +} +int IsEmpty() +{ + return QHR == QTR; +} +int BFS(int r, int c) +{ + int i, p, q, nr, nc; + QHR = QTR = QHC = QTC = 0; + PushR(r); + PushC(c); + Game[r][c] = 'A'; + while (!IsEmpty()) + { + p = PopR(); + q = PopC(); + for (i = 0; i < 6; i++) + { + nr = p + R[i]; + nc = q + C[i]; + if (nr >= N || nc >= N || nr < 0 || nc < 0) + { + continue; + } + if (Game[nr][nc] != 'w') + { + continue; + } + if (nc == N - 1 && Game[nr][nc] == 'w') + { + return 1; + } + Game[nr][nc] = 'A'; + PushR(nr); + PushC(nc); + } } - if( Y == N-1 && adjMat[X][Y] == 'w' ){ - output = 'W'; - return; + return 0; +} +void Com() +{ + int i, j; + for (i = 0; i < N; i++) + { + if (Game[i][0] == 'w') + if (BFS(i, 0)) + { + printf("%d W\n", Kase++); + return; + } } - visit[X][Y] = true; - - int dx[] = {-1, -1, 0, 0, 1, 1}; - int dy[] = {-1, 0, -1, 1, 0, 1}; - int j; - FOI(j, 0, 5) - dfs(X + dx[j], Y + dy[j], cur); + printf("%d B\n", Kase++); } - -int main(int argc, char **argv){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int T = 1; ; T++){ - scanf("%d", &N); - if( N == 0 ) +int main() +{ + int i; + char input[50]; + Kase = 1; + while (1) + { + gets(input); + sscanf(input, "%d", &N); + if (!N) + { break; - int i; - FOI(i, 0, N-1) - scanf("%s", adjMat[i]); - - memset( visit, false, sizeof (visit) ); - - FOI(i, 0, N-1) - if( !visit[0][i] && adjMat[0][i] == 'b' ) - dfs(0, i, 'b'); - FOI(i, 0, N-1) - if( !visit[i][0] && adjMat[i][0] == 'w' ) - dfs(i, 0, 'w'); - - printf("%d %c\n", T, output); + } + for (i = 0; i < N; i++) + { + gets(Game[i]); + } + Com(); } return 0; } diff --git a/261.cpp b/261.cpp new file mode 100644 index 0000000..decac39 --- /dev/null +++ b/261.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +char str[1005], *p; +struct Word +{ + char str[1005]; + Word *next; +} * h[1005]; +int l[1005]; +void add(char *ss, int len) +{ + for (Word *s = h[len]; s; s = s->next) + if (strcmp(s->str, ss) == 0) + { + return; + } + Word *x = new Word; + strcpy(x->str, ss); + x->next = h[len]; + h[len] = x; + l[len]++; +} +void freemem(Word *&p) +{ + if (p) + { + freemem(p->next); + delete p; + } + p = NULL; +} +int main() +{ + bool flag; + while (gets(str) != NULL) + { + if (strcmp(str, "") == 0) + { + break; + } + for (int i = 0; i < 1005; i++) + { + h[i] = NULL, l[i] = 0; + } + int len = strlen(str), s; + flag = true; + for (int i = 1; i <= len; i++) + { + char c = str[i]; + str[i] = '\0'; + for (p = str, s = 0; s < i; p++, s++) + { + add(p, i - s); + if (l[i - s] > i - s + 1) + { + flag = false; + } + } + str[i] = c; + if (!flag) + { + printf("NO:%d\n", i); + break; + } + } + if (flag) + { + puts("YES"); + } + for (int i = 0; i < 1005; i++) + { + freemem(h[i]); + } + } +} diff --git a/263.cpp b/263.cpp index 492bdf2..7b747e8 100644 --- a/263.cpp +++ b/263.cpp @@ -1,91 +1,129 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +typedef long long ss; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +struct node +{ + ss info; + node *parent, *left, *right; +}; +node *root; -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - bool stat = false; - while (true){ - int N, i, j; - cin >> N; - if (N == 0) - break; - if (stat) - cout << endl; - stat = true; - cout << "Original number was " << N << endl; - vector < int > V; - while (true){ - vector < int > vec; - while (N){ - vec.push_back(N % 10); - N /= 10; +void insert(int info) +{ + node *p = (node *)malloc(sizeof(node)); + p->left = p->right = NULL; + p->info = info; + if (root == NULL) + { + root = p; + p->parent = NULL; + return; + } + node *x = root; + while (1) + { + if (x->info < info) + { + if (x->right == NULL) + { + x->right = p; + p->parent = x; + break; + } + else + { + x = x->right; + } + } + else + { + if (x->left == NULL) + { + x->left = p; + p->parent = x; + break; } - sort(vec.begin(), vec.end()); - int A = 0, B = 0; - FOD(i, vec.size() - 1, 0) - A = A * 10 + vec[i]; - FOI(i, 0, vec.size() - 1) - B = B * 10 + vec[i]; - N = A - B; - cout << A << " - " << B << " = " << N << endl; - V.push_back(N); - int S = V.size(); - if (N == 0 || S > 1){ - if (N == 0){ - cout << "0 - 0 = 0\n"; - cout << "Chain length " << S + 1 << endl; - break; - } - else{ - bool flag = false; - FOI(i, 0, S - 2) - if (V[i] == V[S - 1]){ - cout << "Chain length " << S << endl; - flag = true; - break; - } - if (flag) - break; - } + else + { + x = x->left; } } } - return 0; +} +node *search(node *r, int info) +{ + node *x = r; + while (x != NULL) + { + if (x->info == info) + { + return x; + } + if (x->info < info) + { + x = x->right; + } + else + { + x = x->left; + } + } + return x; +} +int com(const void *a, const void *b) +{ + return *(char *)a - *(char *)b; +} +void Rev(char *str) +{ + char temp[100]; + int i, n = 0; + for (i = strlen(str) - 1; i >= 0; i--) + { + temp[n++] = str[i]; + } + temp[n] = '\0'; + strcpy(str, temp); +} +void Cal(ss n) +{ + char dummy[100]; + root = NULL; + ss b, m, pre, temp, length = 0; + while (1) + { + sprintf(dummy, "%lld", n); + qsort(dummy, strlen(dummy), sizeof(char), com); + sscanf(dummy, "%lld", &b); + Rev(dummy); + sscanf(dummy, "%lld", &m); + temp = m - b; + printf("%lld - %lld = %lld\n", m, b, temp); + if (search(root, temp)) + { + break; + } + insert(temp); + n = temp; + length++; + } + printf("Chain length %lld\n\n", length + 1); } +int main() +{ + ss n; + while (scanf("%lld", &n) && n) + { + if (!n) + { + break; + } + printf("Original number was %lld\n", n); + Cal(n); + } + return 0; +} diff --git a/264.cpp b/264.cpp index 2eae266..85f4df2 100644 --- a/264.cpp +++ b/264.cpp @@ -1,109 +1,76 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -#define LIM 100000000 -struct Cantor{ - int64 num; - int64 den; - /* - Cantor(int n, int d){ - num = n; - den = d; - } - */ - -}; - -Cantor can[LIM+1]; - -int main(){ - int i, SZ=0; - bool flag = true; - for(i=1; ; i++){ - if(i%2==0){ - int64 nu = 1; - int64 de = i; - while(true){ - Cantor c; - c.num = nu; - c.den = de; - can[SZ] = c; - SZ++; - if(SZ == LIM){ - flag = false; - break; - } - if(de == 1) - break; - nu++; - de--; - - } - } - else{ - int64 nu = i; - int64 de = 1; - while(true){ - Cantor c; - c.num = nu; - c.den = de; - can[SZ] = c; - SZ++; - if(SZ == LIM){ - flag = false; - break; - } - if(nu == 1) - break; - nu--; - de++; - - } - } - if(!flag) - break; - } - //cout<>ind){ - if(nl == true) - cout< + +using namespace std; + +/*********************** +TITLE : COUNT ON COUNTOR + NO : 264 +***********************/ +int N; +int STEP(int n) +{ + int j = 0, i; + int sum = 0; + for (i = 1; sum < n; i++) + { + sum += i; + j++; + } + return j; +} +void UPDOWN(int m) +{ + int k; + int d = m, n = 1; + k = m * (m + 1) / 2; + while (1) + { + if (k == N) + { + break; + } + d--; + n++; + k--; + } + printf("TERM %d IS %d/%d\n", N, d, n); +} +void DOWNUP(int m) +{ + int k; + int d = 1, n = m; + k = m * (m + 1) / 2; + while (1) + { + if (k == N) + { + break; + } + d++; + n--; + k--; + } + printf("TERM %d IS %d/%d\n", N, d, n); +} +int main() +{ + int j; + while (scanf("%d", &N) == 1) + { + if (N == 1) + { + printf("TERM 1 IS 1/1\n"); + continue; + } + j = STEP(N); + if (j % 2 == 0) + { + UPDOWN(j); + } + else + { + DOWNUP(j); + } + } + return 0; +} diff --git a/265.cpp b/265.cpp new file mode 100644 index 0000000..09ef4fc --- /dev/null +++ b/265.cpp @@ -0,0 +1,159 @@ +#include + +using namespace std; + +struct State +{ + string name, lang; + set g; + int lang_mask[128]; + int read() + { + string line; + if (getline(cin, line)) + { + assert(line.length() > 0); + g.clear(); + stringstream sin(line); + sin >> name >> lang; + while (sin >> line) + g.insert(line); + + g.insert(name);// what the fuck, same country in test data. + memset(lang_mask, 0, sizeof(lang_mask)); + for (int i = 0; i < lang.length(); i++) + lang_mask[lang[i]] = 1; + return 1; + } + else + { + return 0; + } + } + int hasCommon(const State &a) + { + for (int i = 0; i < 128; i++) + if (lang_mask[i] && a.lang_mask[i]) + return 1; + return 0; + } + char common(const State &a) + { + for (int i = 0; i < 128; i++) + if (lang_mask[i] && a.lang_mask[i]) + return i; + return '-'; + } +} C[10]; +int g[10][10]; +int dp[1 << 10][10], from[1 << 10][10]; + +int tsp(int u, int last) +{ + if (dp[u][last] != -1) + return dp[u][last]; + int &ret = dp[u][last]; + ret = 0; + for (int i = 0; i < 10; i++) + { + if (((u >> i) & 1) && g[last][i]) + { + int f = tsp(u ^ (1 << i), i); + if (f) + { + from[u][last] = i; + return ret = 1; + } + } + } + return ret; +} +int main() +{ + while (true) + { + for (int i = 0; i < 10; i++) + { + if (!C[i].read()) + return 0; + } + + memset(g, 0, sizeof(g)); + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 10; j++) + { + if (C[i].g.count(C[j].name) && C[j].g.count(C[i].name) && C[i].hasCommon(C[j])) + { + g[i][j] = 1; + } + } + } + + // for (int i = 0; i < 10; i++, puts("")) + // for (int j = 0; j < 10; j++) + // printf("%d ", g[i][j] > 0); + + memset(dp, -1, sizeof(dp)); + dp[0][0] = 1; + int flag = tsp((1 << 10) - 1, 0); + if (flag) + { + int p = (1 << 10) - 1, q = 0; + int seat[10]; + for (int i = 0; i < 10; i++) + { + seat[i] = q; + q = from[p][q]; + p = p ^ (1 << q); + } + for (int i = 0; i < 10; i++) + printf("%d %c %s %c\n", i + 1, C[seat[i]].common(C[seat[(i - 1 + 10) % 10]]), + C[seat[i]].name.c_str(), C[seat[i]].common(C[seat[(i + 1 + 10) % 10]])); + } + else + { + puts("NO SOLUTION EXISTS"); + } + puts(""); + + while (getchar() != '\n') + ; + } + return 0; +} +/* +USA EF CHN GBR USR FRA FRG JPN ISR POR KOR +CHN CFE USA GBR FRA FRG +GBR ER USA CHN USR FRA FRG JPN ISR POR KOR +USR RF USA GBR FRA FRG +FRA F USA CHN GBR USR FRG JPN ISR POR +FRG ERG USA CHN GBR USR FRA JPN ISR POR +JPN JHG USA GBR FRA FRG JPN ISR POR KOR +ISR HER USA GBR FRA FRG JPN KOR +POR PGE USA GBR FRA FRG JPN +KOR KEC USA GBR USR JPN ISR + +USA EF CHN GBR USR FRA FRG JPN ISR POR KOR +CHN CFE USA GBR FRA FRG +GBR ER USA CHN USR FRA FRG JPN ISR POR KOR +USR RF +FRA F USA CHN GBR USR FRG JPN ISR POR +FRG ERG USA CHN GBR USR FRA JPN ISR POR +JPN JHG USA GBR FRA FRG JPN ISR POR KOR +ISR HER USA GBR FRA FRG JPN KOR +POR PGE USA GBR FRA FRG JPN +KOR KEC USA GBR USR JPN ISR + +TLC ADBE TQA DAO FHH NUW FAB PSR FEQ QPA KCW +TQA EB TLC DAO PSR FEQ KCW +DAO B TLC TQA FHH FAB PSR FEQ QPA KCW +FHH B TLC DAO PSR KCW +NUW DBE TLC +FAB D TLC DAO PSR FEQ QPA +PSR AC TLC TQA DAO FHH FAB QPA +FEQ CB TLC TQA DAO FAB QPA +QPA D TLC DAO FAB PSR FEQ KCW +KCW AE TLC TQA DAO FHH QPA + +*/ diff --git a/267.cpp b/267.cpp new file mode 100644 index 0000000..0450089 --- /dev/null +++ b/267.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + double ws, wd, dc, ta; + double wdd, dcd; + double ah, gs; + double angle; + double b, c; + double x, y; + double pi = acos(-1); + while (scanf("%lf %lf %lf %lf", &ws, &wd, &dc, &ta) == 4) + { + wdd = wd * pi / 180; + dcd = dc * pi / 180; + angle = pi - (wdd - dcd); + b = -2 * ws * cos(angle); + c = ws * ws - ta * ta; + gs = (-b + sqrt(b * b - 4 * c)) / 2; + x = ws * cos(wdd) + gs * cos(dcd); + y = ws * sin(wdd) + gs * sin(dcd); + ah = atan2(y, x); + if (ah < 0) + { + ah = ah + pi * 2; + } + ah = ah * 180 / pi; + printf("WIND SPEED %.2lf\nWIND DIRECTION %.2lf\n", ws, wd); + printf("DESIRED COURSE %.2lf\nTRUE AIRSPEED %.2lf\n", dc, ta); + printf("AIRCRAFT HEADING %.2lf\nGROUND SPEED %.2lf\n\n", ah, gs); + } + return 0; +} diff --git a/268.cpp b/268.cpp new file mode 100644 index 0000000..e2b7fa2 --- /dev/null +++ b/268.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int main() +{ + int a; + vector num; + vector jin; + vector temp; + while (scanf("%d", &a) == 1) + { + num.clear(); + jin.clear(); + temp.clear(); + for (int i = 1; i < a; i++) + { + temp.clear(); + jin.clear(); + temp.push_back(i); + jin.push_back(0); + while (true) + { + int j = temp.size() - 1; + int k = (temp[j] * 2 + jin[j]) % a; + int l = (temp[j] * 2 + jin[j]) / a; + if (k == i && l == 0) + { + break; + } + temp.push_back(k); + jin.push_back(l); + } + if (i == 1) + { + num = temp; + } + else + { + if (num.size() > temp.size()) + { + num = temp; + } + else if (num.size() == temp.size()) + { + for (int j = num.size() - 1; j >= 0; j--) + { + if (num[j] > temp[j]) + { + num = temp; + break; + } + else if (num[j] < temp[j]) + { + break; + } + } + } + } + //cout<<"Begins with "<=0;i--) cout<= 0; i--) + { + printf("%d ", num[i]); + } + putchar(10); + } + return 0; +} diff --git a/269.cpp b/269.cpp new file mode 100644 index 0000000..af46632 --- /dev/null +++ b/269.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +int a[16], sum, n; +int tup[131072][16], total; + +static int cmp(int b[]) +{ + int i; + /* a[n] = -0x1FFFFFFF; */ + for (i = 0; a[i] == b[i]; i++) + ; + return (b[i] > a[i] && i < n) ? 1 : 0; +} + +static void check() +{ + static int b[32]; + int i, j; + for (i = 0, j = 0; i < n; i++) + { + b[j++] = a[i]; + } + for (i = 0; i < n; i++) + { + b[j++] = a[i]; + } + for (i = 1; i < n; i++) + if (b[i] == a[0] && cmp(b + i)) + { + return; + } + for (i = 1, j = n << 1; i < j; i++) + { + b[i] = -b[i]; + } + for (i = 1; i < n; i++) + if (b[i] == a[0] && cmp(b + i)) + { + return; + } + for (i = n, j = 0; i--;) + { + b[j++] = a[i]; + } + for (i = n; i--;) + { + b[j++] = a[i]; + } + for (i = 0; i < n; i++) + if (b[i] == a[0] && cmp(b + i)) + { + return; + } + for (i = 0, j = n << 1; i < j; i++) + { + b[i] = -b[i]; + } + for (i = 0; i < n; i++) + if (b[i] == a[0] && cmp(b + i)) + { + return; + } + for (i = 0; i < n; i++) + { + tup[total][i] = a[i]; + } + total++; +} + +static void go(int p) +{ + if (p == (n - 1)) + { + if (-a[0] <= sum && sum <= a[0]) + { + a[p] = -sum; + check(); + } + return; + } + if (abs(sum) > ((n - p) * a[0])) + { + return; + } + for (a[p] = -a[0]; a[p] < a[0]; a[p]++) + { + if (a[p - 1] == a[0] && a[p] > a[1]) + { + return; + } + sum += a[p]; + go(p + 1); + sum -= a[p]; + } + if (a[p] == a[0] && a[p - 1] <= a[1]) + { + sum += a[0]; + go(p + 1); + sum -= a[0]; + } +} + +int main() +{ + int i, k, t = 0; + while (scanf("%d %d", &n, &k) == 2) + { + if (t++) + { + printf("\n"); + } + if (n == 1) + { + /*damn case*/ + printf("1\n(0)\n"); + continue; + } + a[n] = -0x1FFFFFFF; + total = 0; + for (a[0] = 0; a[0] <= k; a[0]++) + { + sum = a[0]; + go(1); + } + printf("%d\n", total); + for (i = 0; i < total; i++) + { + for (k = 0; k < n; k++) + { + printf(k ? ",%d" : "(%d", tup[i][k]); + } + printf(")\n"); + } + } + return 0; +} diff --git a/270.cpp b/270.cpp new file mode 100644 index 0000000..a6481f7 --- /dev/null +++ b/270.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +using std::vector; +using std::cin; + +struct Point +{ + int x, y; +}; + +bool is_collinear(Point a, Point b, Point c) +{ + int pos = a.x * b.y + b.x * c.y + c.x * a.y; + int neg = b.x * a.y + c.x * b.y + a.x * c.y; + return (pos == neg); +} + +bool is_line_exist(Point a, Point b, vector &prev_line) +{ + for (int i = 0; i < prev_line.size(); i = i + 2) + { + bool r1 = is_collinear(a, b, prev_line[i]); + bool r2 = is_collinear(a, b, prev_line[i + 1]); + if (r1 && r2) + return true; + } + return false; +} + +int search_lines(vector &points) +{ + int max_points = 0; + vector prev_line; + for (int i = 0; i < points.size(); ++i) + { + for (int j = i + 1; j < points.size(); ++j) + { + // line pi-pj + if (is_line_exist(points[i], points[j], prev_line)) + continue; + vector line; + line.push_back(points[i]); + line.push_back(points[j]); + for (int k = j + 1; k < points.size(); ++k) + if (is_collinear(points[i], points[j], points[k])) + line.push_back(points[k]); + if (line.size() > max_points) + max_points = line.size(); + } + } + return max_points; +} + +int main() +{ + int num_case; + scanf("%d\n", &num_case); + while (num_case--) + { + vector points; + char line[128]; + while (cin.getline(line, sizeof(line))) + { + if (0 == strlen(line)) + break;//empty line + Point p; + sscanf(line, "%d%d", &p.x, &p.y); + points.push_back(p); + } + int max_points = search_lines(points); + printf("%d\n", max_points); + if (num_case != 0) + putchar('\n'); + } + return 0; +} diff --git a/271.cpp b/271.cpp index a815933..2dd7e9e 100644 --- a/271.cpp +++ b/271.cpp @@ -1,102 +1,65 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +#include -string Ap = "pqrstuvwxyz"; -string Pe = "CDEI"; -set< char > Alp, Pre, Set; +using namespace std; -bool check(string S) { - int L = S.length(); - string A = ""; - string B = S.substr(1); - - if (S == "") - return false; - - if (L == 1) { - if (Set.find(S[0]) != Set.end()) - return true; - return false; +bool FitSyntax(char &a) +{ + if ((a >= 'p' && a <= 'z') || a == 'N' || a == 'C' || a == 'D' || a == 'E' || a == 'I') + { + return true; } - - if (S[0] == 'N') - return check(B); - else if (S[0] == 'C' || S[0] == 'D' || S[0] == 'E' || S[0] == 'I') { - int i; - FOI(i, 1, L-2) { - A += S[i]; - B.erase(0, 1); - if (check(A) && check(B)) return true; - } + else + { return false; } - else return false; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int i = 0; - FOI(i, 0, Ap.length()-1) - Alp.insert(Ap[i]); - FOI(i, 0, Pe.length()-1) - Pre.insert(Pe[i]); - string str; - while (getline(cin, str)) { - int L = str.length()-1; - int S = 0; - FOD(i, L, 0) { - if (Alp.find(str[i]) != Alp.end()) - ++S; - else if (Pre.find(str[i]) != Pre.end()) { - if (S >= 2) --S; - else { - S = 0; - break; - } +int main() +{ + char buf[1000]; + while (cin.getline(buf, 1000)) + { + int cnt = 0, i; + for (i = strlen(buf) - 1; i >= 0; i--) + { + if (!FitSyntax(buf[i])) + { + cnt = 0; + break; } - else if (str[i] == 'N') { - if (S < 1) { - S = 0; - break; + if (FitSyntax(buf[i])) + { + if (buf[i] >= 'p' && buf[i] <= 'z') + { + cnt++; } - else - S = S; - } - else { - S = 0; - break; + if (buf[i] == 'C' || buf[i] == 'D' || buf[i] == 'E' || buf[i] == 'I') + { + if (cnt >= 2) + { + cnt--; + } + else + { + cnt = 0; + break; + } + } + if (buf[i] == 'N') + if (cnt == 0) + { + break; + } } } - if (S == 1) cout << "YES\n"; - else cout << "NO\n"; + if (cnt == 1) + { + cout << "YES" << endl; + } + else + { + cout << "NO" << endl; + } } return 0; } diff --git a/272.cpp b/272.cpp index 827bb44..24571c3 100644 --- a/272.cpp +++ b/272.cpp @@ -1,26 +1,35 @@ -#include -#include - -char text[1000000]; - -int main() -{ - int i,j = 0; - - while(gets(text)) { - for( i = 0; text[i]; i ++) { - if(text[i] =='"') { - j ++; - if(j % 2 != 0) - printf("``"); - else - printf("''"); - } - else - printf("%c",text[i]); - } - printf("\n"); - } - return 0; -} - +#include + +using namespace std; + +int main() +{ + char buf[1000]; + int flag = 0; + while (gets(buf)) + { + int i; + for (i = 0; buf[i]; i++) + { + if (buf[i] == '"') + { + if (flag == 0) + { + cout << "``"; + flag = 1; + } + else if (flag == 1) + { + cout << "''"; + flag = 0; + } + } + else + { + cout << buf[i]; + } + } + cout << endl; + } + return 0; +} diff --git a/273.cpp b/273.cpp new file mode 100644 index 0000000..b1f7154 --- /dev/null +++ b/273.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +/* by Michal Koucky */ +/* + Reads input and compute graph components +*/ +int n; +struct coords +{ + int x1, y1, x2, y2; +} st[16]; /* straws endoipnts */ +int comps[16]; /* list of components */ +int connected(int a, int b) +/* 1 - if a,b geometricaly cconnected + 0 - otherwise */ +{ + int d = (st[a].x2 - st[a].x1) * (st[b].y1 - st[b].y2) - + (st[b].x1 - st[b].x2) * (st[a].y2 - st[a].y1); + if (d) /* not parallel */ + { + int dt = (st[a].x1 - st[b].x1) * (st[b].y1 - st[b].y2) - + (st[b].x1 - st[b].x2) * (st[a].y1 - st[b].y1); + int ds = (st[a].x2 - st[a].x1) * (st[a].y1 - st[b].y1) - + (st[a].x1 - st[b].x1) * (st[a].y2 - st[a].y1); + if (d < 0) + { + dt = -dt; + ds = -ds; + d = -d; + } + if (dt < -d || dt > 0 || ds < -d || ds > 0) + { + return 0; + } + else + { + return 1; + } + } + else /* parallel */ + { + int dx = (st[a].x2 - st[a].x1) * (st[b].y1 - st[a].y2) - + (st[b].x1 - st[a].x2) * (st[a].y2 - st[a].y1); + if (dx) + { + return 0; + } + if (((st[a].x1 <= st[b].x1 && st[b].x1 <= st[a].x2) || (st[b].x1 <= st[a].x1 && st[a].x1 <= st[b].x2)) && st[a].x1 != st[a].x2) + { + return 1; + } + if (((st[a].y1 <= st[b].y1 && st[b].y1 <= st[a].y2) || (st[b].y1 <= st[a].y1 && st[a].y1 <= st[b].y2)) && st[a].y1 != st[a].y2) + { + return 1; + } + return 0; + } + return 0; +} + +void change(int a) +{ + int p; + p = st[a].x1; + st[a].x1 = st[a].x2; + st[a].x2 = p; + p = st[a].y1; + st[a].y1 = st[a].y2; + st[a].y2 = p; +} +int main() +{ + int i, j, k, a, b, d1, d2, cc; + bool first = true; + scanf("%d", &cc); + while (cc--) + { + if (!first) + { + printf("\n"); + } + scanf("%d", &n); + for (i = 1; i <= n; i++) + { + scanf("%d%d%d%d", &(st[i].x1), &(st[i].y1), &(st[i].x2), &(st[i].y2)); + if (st[i].x1 > st[i].x2 || (st[i].x1 == st[i].x2 && st[i].y1 > st[i].y2)) + { + change(i); + } + comps[i] = i; + } + for (i = 1; i <= n; i++) + for (j = i + 1; j <= n; j++) + if ((comps[i] != comps[j]) && connected(i, j)) + { + d1 = comps[i]; + d2 = comps[j]; + for (k = 1; k <= n; k++) + if (comps[k] == d1) + { + comps[k] = d2; + } + } + scanf("%d%d", &a, &b); + while (a && b) + { + if (comps[a] == comps[b]) + { + printf("CONNECTED\n"); + } + else + { + printf("NOT CONNECTED\n"); + } + scanf("%d%d", &a, &b); + } + first = false; + } + return 0; +} diff --git a/274.cpp b/274.cpp new file mode 100644 index 0000000..1854e4e --- /dev/null +++ b/274.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +const int MAXSIZE = 105; + +int nRooms, initCat, initMouse; +bool CatMove[MAXSIZE][MAXSIZE], MouseMove[MAXSIZE][MAXSIZE]; +bool cond1, cond2; +int catVisted[MAXSIZE], mouseVisted[MAXSIZE]; + +void input() +{ + memset(CatMove, 0, sizeof(CatMove)); + memset(MouseMove, 0, sizeof(MouseMove)); + cond1 = cond2 = false; + cin >> nRooms >> initCat >> initMouse; + int a, b; + // input cat road + while (cin >> a >> b && a >= 0) + { + CatMove[a][b] = true; + } + cin.get(); + // input mouse road + char str[100]; + while (cin.getline(str, 100) && strcmp(str, "\0")) + { + sscanf(str, "%d%d", &a, &b); + MouseMove[a][b] = true; + } +} + +void dfs(int *vis, int room, int cm, bool end) +{ + if (vis[room] || end) + { + return; + } + if (cm == 2 && catVisted[room]) + { + end = true; + cond1 = true; + return; + } + vis[room] = cm; + for (int i = 1; i <= nRooms; i++) + { + if (cm == 1 && CatMove[room][i])// cat + { + dfs(vis, i, cm, end); + } + else if (cm == 2 && MouseMove[room][i])//mouse + { + dfs(vis, i, cm, end); + } + } +} + +void solve() +{ + memset(catVisted, 0, sizeof(catVisted)); + memset(mouseVisted, 0, sizeof(mouseVisted)); + dfs(catVisted, initCat, 1, false); + dfs(mouseVisted, initMouse, 2, false); + for (int i = 1; i <= nRooms; i++) + if (MouseMove[initMouse][i] && !catVisted[i]) + { + cond2 = true; + break; + } + if (cond1) + { + cout << "Y "; + } + else + { + cout << "N "; + } + if (cond2) + { + cout << "Y" << endl; + } + else + { + cout << "N" << endl; + } +} + +int main() +{ + int numOfCase; + cin >> numOfCase; + for (int kase = 1; kase <= numOfCase; kase++) + { + input(); + solve(); + if (kase != numOfCase) + { + cout << endl; + } + } + // system( "pause" ); + return 0; +} diff --git a/275.cpp b/275.cpp new file mode 100644 index 0000000..f2a9823 --- /dev/null +++ b/275.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ + +int main() +{ + unsigned int digits = 0; + unsigned int fenzi = 1, fenmu = 1; + int count = 0; + while (scanf("%d%d", &fenzi, &fenmu) == 2) + { + if (fenzi == 0 || fenmu == 0) + { + break; + } + int *p = new int[fenmu]; + putchar('.'); + count++; + for (int i = 0; i < fenmu; i++) + { + fenzi *= 10; + *(p + i) = fenzi; + if (i > 0 && fenzi != 0) + { + for (int j = 0; j < i; j++) + { + if (*(p + j) == *(p + i)) + { + digits = i - j; + break; + } + } + } + if (fenzi == 0) + { + printf("\nThis expansion terminates.\n\n"); + count = 0; + break; + } + if (digits != 0) + { + printf("\nThe last %d digits repeat forever.\n\n", digits); + count = 0; + break; + } + count++; + if (count == 51) + { + putchar(10); + count = 1; + } + printf("%d", fenzi / fenmu); + fenzi = fenzi % fenmu; + } + digits = 0; + delete[] p; + } + return 0; +} diff --git a/276.cpp b/276.cpp new file mode 100644 index 0000000..d39299b --- /dev/null +++ b/276.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +const char trans[] = {"|n98r"}; +int acnt[6], bcnt[6], ccnt[6], dcnt[6], A, B; +char a[50], b[50]; +int calcul(char s[], int cnt[])//将字符串记录æˆæ•°ç»„,返回å进制的值 +{ + int i = 0, num = 0, len = strlen(s); + while (i < len) + { + while (i < len && s[i] == '|') + { + cnt[0]++, i++; + } + while (i < len && s[i] == 'n') + { + cnt[1]++, i++; + } + while (i < len && s[i] == '9') + { + cnt[2]++, i++; + } + while (i < len && s[i] == '8') + { + cnt[3]++, i++; + } + while (i < len && s[i] == 'r') + { + cnt[4]++, i++; + } + i++; + } + for (i = 4; i >= 0; i--) + { + num = num * 10 + cnt[i]; + } + return num; +} +bool lessthan(const int c[], const int b[])//ccnt是å¦å°äºŽbcnt,结æŸä¹˜æ³• +{ + for (int i = 4; i >= 0; i--) + if (b[i] > c[i]) + { + return 1; + } + else if (b[i] < c[i]) + { + return 0; + } + return 1; +} +int Print(const int x[])//打å°x[],æ¯ç»„结尾å‡æœ‰ç©ºæ ¼ï¼ŒåŒ…括末尾 +{ + int i, j, tot = 0; + for (i = 0; i <= 4; i++) + { + if (x[i] == 0) + { + continue; + } + for (j = 0; j < x[i]; j++) + { + printf("%c", trans[i]); + tot++; + } + printf(" "); + tot++; + } + return tot; +} +void Carry(int x[])//处ç†è¿›ä½ï¼Œé«˜äºŽä¸‡ä½çœç•¥ +{ + for (int i = 0; i <= 4; i++) + if (x[i] > 9) + { + x[i + 1] += (x[i] / 10); + x[i] %= 10; + } +} +void solve() +{ + int i, len; + bool f; + memset(acnt, 0, sizeof(acnt));//åˆå§‹åŒ–,乘数 + memset(bcnt, 0, sizeof(bcnt));//被乘数 + memset(ccnt, 0, sizeof(ccnt));//açš„double + memset(dcnt, 0, sizeof(dcnt));//最åŽç»“æžœ + A = calcul(a, acnt); + B = calcul(b, bcnt); + ccnt[0] = 1; + while (lessthan(ccnt, bcnt)) + { + f = (B % 2) ? 1 : 0; + B /= 2; + len = Print(ccnt); + if (f) + { + printf("*"); + len++; + } + for (; len < 34; len++) + { + printf(" "); + } + len = Print(acnt); + printf("\n"); + if (f) + for (i = 0; i <= 4; i++) + { + dcnt[i] += acnt[i]; + } + for (i = 0; i <= 4; i++)//Double + { + ccnt[i] *= 2, acnt[i] *= 2; + } + Carry(ccnt); + Carry(acnt); + } + Carry(dcnt); + printf("The solution is: "); + Print(dcnt); + printf("\n"); +} +int main() +{ + fgets(a, 50, stdin); + while (fgets(b, 50, stdin) != NULL) + { + solve(); + fgets(a, 50, stdin); + } + return 0; +} diff --git a/277.cpp b/277.cpp new file mode 100644 index 0000000..fef1dcc --- /dev/null +++ b/277.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +#define MAXN 1000 + +char Rec[70], code[5], stname[17], Ex[27], Pri[10], des[17], F[MAXN], Stn[MAXN][17]; +int fn, fe, fu; + +int ReadCase() +{ + int i; + while (gets(Rec)) + { + for (i = 0; Rec[i]; i++) + { + if (Rec[i] == '\n') + { + Rec[i] = '\0'; + } + } + if (strlen(Rec) == 0) + { + return 0; + } + return 1; + } + return 0; +} +void Sep() +{ + int i, j, k = 0, co, l; + char temp[30]; + fn = fu = fe = 0; + l = strlen(Rec); + for (i = 0; Rec[i] != ',' && i < l; i++) + { + temp[k++] = Rec[i]; + } + temp[k] = '\0'; + co = atoi(temp); + sprintf(code, "%03d", co); + k = 0; + for (j = i + 1; Rec[j] != ',' && j < l; j++) + { + stname[k++] = Rec[j]; + } + if (k > 0) + { + fn = 1; + } + stname[k] = '\0'; + if (F[co] == 0) + { + F[co] = 1; + if (fn) + { + strcpy(Stn[co], stname); + } + else + { + strcpy(Stn[co], ",,,"); + } + } + k = 0; + for (i = j + 1; Rec[i] != ',' && i < l; i++) + { + des[k++] = Rec[i]; + } + des[k] = '\0'; + k = 0; + for (j = i + 1; Rec[j] != ',' && j < l; j++) + { + Ex[k++] = Rec[j]; + } + Ex[k] = '\0'; + if (k > 0) + { + fe = 1; + } + k = 0; + for (i = j + 1; Rec[i] != ',' && i < l; i++) + { + Pri[k++] = Rec[i]; + } + Pri[k] = '\0'; + if (k > 0) + { + fu = 1; + } +} +void SolvedCase() +{ + char iid[100], ides[100]; + double price = 0; + int co = atoi(code); + strcpy(iid, code); + strcat(iid, des); + if (strlen(iid) > 13) + { + return; + } + if (fn == 0) + { + if (F[co] == 1) + { + if (!strcmp(Stn[co], ",,,")) + { + return; + } + else + { + strcpy(ides, Stn[co]); + } + } + else + { + return; + } + } + else + { + strcpy(ides, stname); + } + if (fe) + { + strcat(ides, "-"); + strcat(ides, Ex); + } + ides[30] = '\0'; + if (fu) + { + price = atof(Pri); + } + printf("%s,%s,%.2lf\n", iid, ides, price / 100); +} + +int main() +{ + char input[100]; + int kase; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + printf("Item Id,Item Desc,Item Price\n"); + while (ReadCase()) + { + Sep(); + SolvedCase(); + } + if (kase) + { + putchar('\n'); + for (int i = 0; i < MAXN; i++) + { + F[i] = 0; + } + } + } + return 0; +} diff --git a/278.cpp b/278.cpp new file mode 100644 index 0000000..542f073 --- /dev/null +++ b/278.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +int main() +{ + int t; + char c; + int rows, columns; + int result; + cin >> t; + while (t--) + { + cin >> c >> rows >> columns; + switch (c) + { + case 'r': + cout << min(rows, columns) << endl; + break; + case 'k': + result = 0; + for (int i = 0; i < rows; i++) + { + for (int j = (i % 2 == 0 ? 0 : 1); j < columns; j += 2) + { + result++; + } + } + cout << result << endl; + break; + case 'K': + result = 0; + for (int i = 0; i < rows; i += 2) + { + for (int j = 0; j < columns; j += 2) + { + result++; + } + } + cout << result << endl; + break; + case 'Q': + cout << min(rows, columns) << endl; + break; + } + } + return 0; +} diff --git a/279.cpp b/279.cpp new file mode 100644 index 0000000..3dfdd14 --- /dev/null +++ b/279.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +typedef long long ll; +const int maxn = 30; + +int main() +{ + int cas, n; + ll pos, dp[maxn + 5]; + char str[maxn + 5]; + dp[0] = 0; + for (int i = 1; i <= maxn; i++) + { + dp[i] = (dp[i - 1] + i) * 2 - 1; + } + scanf("%d", &cas); + while (cas--) + { + scanf("%d%s%lld", &n, str, &pos); + ll ans = 0, sign = 1; + for (int i = 0; i < n; i++) + { + if (str[i] == 'v') + { + ans += (sign * dp[n - i]); + sign *= -1; + } + } + printf("%lld\n", ans + pos + 1); + } + return 0; +} diff --git a/280.cpp b/280.cpp new file mode 100644 index 0000000..9f52459 --- /dev/null +++ b/280.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + void solve(int point, int depth); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::solve(int n, int a) +{ + int tmp; + Vertex *v; + bool visited[n + 1]; + fill(visited, visited + n + 1, false); + queue q; + int count = n; + q.push(a); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + v = getVertex(tmp); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + if (visited[v->adj[i]->id]) + { + continue; + } + visited[v->adj[i]->id] = true; + count--; + q.push(v->adj[i]->id); + } + } + cout << count; + for (int i = 1; i <= n; i++) + { + if (!visited[i]) + { + cout << " " << i; + } + } + cout << endl; +} + +int main() +{ + int n; + int a, b; + while (cin >> n) + { + if (!n) + { + break; + } + Graph g; + for (int i = 1; i <= n; i++) + { + g.addVertex(i); + } + while (cin >> a) + { + if (!a) + { + break; + } + while (cin >> b) + { + if (!b) + { + break; + } + g.addEdge(a, b, true); + } + } + cin >> a; + while (a--) + { + cin >> b; + g.solve(n, b); + } + } + return 0; +} diff --git a/282.cpp b/282.cpp new file mode 100644 index 0000000..594ab7d --- /dev/null +++ b/282.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int MAXN = 10000; +const int SIZE = 200; +; + +char file[MAXN][SIZE]; + +void check(char src[], char s1[], char s2[]) +{ + int n = strlen(src), m = strlen(s1), l, r; + for (l = 0; l < n; ++l) + { + if (s1[l] == '*') + break; + if (src[l] != s1[l]) + return; + } + for (r = n - 1; r >= 0; --r) + { + if (s1[m - (n - r)] == '*') + break; + if (src[r] != s1[m - (n - r)]) + return; + } + if (l > r + 1) + return; + printf("mv %s ", src); + for (int i = 0; s2[i]; ++i) + { + if (s2[i] == '*') + { + for (int j = l; j <= r; ++j) + putchar(src[j]); + } + else + putchar(s2[i]); + } + puts(""); +} + +int main() +{ + int n = 0; + while (scanf("%s", file[n]) == 1) + { + while (strcmp(file[n], "end") != 0) + { + n++; + scanf("%s", file[n]); + } + char st[SIZE]; + while (scanf("%s", st) == 1 && strcmp(st, "end") != 0) + { + char s1[SIZE], s2[SIZE]; + scanf("%s%s", s1, s2); + printf("rename %s %s\n", s1, s2); + for (int i = 0; i < n; ++i) + { + check(file[i], s1, s2); + } + } + n = 0; + puts(""); + } +} diff --git a/283.cpp b/283.cpp new file mode 100644 index 0000000..e9e95dc --- /dev/null +++ b/283.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +struct Char +{ + char c; + int count; +} c[256]; +int m[260]; +char str[500000]; +int ptr; +int find(char ch) +{ + //if(!isalpha(ch)&&ch!=','&&ch!='.'&&ch!='-'&&ch!='$'&&ch!=' ') return -1; + for (int i = 0; i < ptr; i++) + if (c[i].c == ch) + { + c[i].count++; + return i; + } + c[ptr].c = ch; + c[ptr++].count = 1; + return ptr - 1; +} +int compare(const void *a, const void *b) +{ + return ((Char *)b)->count - ((Char *)a)->count; +} + +int main() +{ + int t; + gets(str); + sscanf(str, "%d", &t); + while (t--) + { + for (int i = 0; i < 256; i++) + { + c[i].c = '\0'; + c[i].count = 0; + } + int n; + ptr = 0; + gets(str); + sscanf(str, "%d", &n); + while (n--) + { + gets(str); + int i; + for (i = 0; str[i]; i++) + { + find(str[i]); + } + } + qsort(c, ptr, sizeof(Char), compare); + for (int j = ptr; j < 70; j++) + { + m[j] = 0; + } + for (int i = ptr - 1; i >= 0; i--) + { + m[i] = 9999999; + for (int j = 1, k = 2; j <= 16; j++, k *= 2) + { + int p = k - 1; + int sum = 0, sum1 = 0; + for (int x = i; x < i + p; x++) + { + sum += c[x].count; + } + for (int x = i + p; x < ptr; x++) + { + sum1 += c[x].count; + } + if (i + k == ptr) + { + int q = 0; + for (int x = i; x < ptr; x++) + { + q += c[x].count; + } + int temp = q * j; + if (temp < m[i]) + { + m[i] = temp; + } + } + if (i + p > ptr) + { + break; + } + if (m[i + p] + sum1 * j + sum * j < m[i]) + { + m[i] = m[i + p] + sum1 * j + sum * j; + } + } + } + printf("%d\n", m[0]); + } + return 0; +} diff --git a/284.cpp b/284.cpp new file mode 100644 index 0000000..14e6bfb --- /dev/null +++ b/284.cpp @@ -0,0 +1,320 @@ +#include + +using namespace std; + +// @ http://algoshare.blogspot.sg/2012/08/284-logic.html +// the code uses several global variables for the reason that i created a debug function, print_array(), to aid me in checking the code prior to submission, to see whether the code actually does what it is intended to do. instead of passing the arrays as arguments to the print_arrays() function, i thought it would be easier to use global variables. using global variables is not good programming practice. however, since we are after performance and not beauty or maintainability, i thought it would be all write to deviate from good programming habits. + +// first, i explain the data structures used. several array variables are utilized. in the real world, one would not do this to implement the algorithm to this problem. in the real world, one would probably use object-oriented programming and classes to implement the algorithm. however, once again, as the emphasis of uva problems is speed rather than beauty, i resorted to this rather ugly but probably faster code implementation. + +// the problem specifies a 10x10 grid array where each element or node in the grid corresponds to an input node, output node, not gate, and gate, or or gate. we could have used a two-dimensional array to implement the code; however, it would probably easier to reduce and think of the problem in terms of only one dimension to simplify things. after all, every node in the 10x10 grid two-dimensional array would correspond to a unique element in a 100-element one-dimensional array. for instance the node [9][8] in the 2d array would correspond to node [98] in the 1d array, and node [0][0] in the 2d array would correspond to node [0] in the 1d array. + +// in object-oriented programming, a node would correspond to a class with several variables in it. the class would have a variable corresponding to the symbol of the node, which can be i, o, &, | or !, depending whether the node is an input, output, and, or or not gate. in addition, the class would have a variable corresponding to the x-coordinate of the node, and another variable corresponding to the y-coordinate of the node. moreover, the class would have one or two inputs depending on what type of gate it is. if the node is an input, output or not gate, then the node would only have one input. if the node, however, were an and gate or an or gate, then it would require two input variables. finally, the class would need an output variable to store the output of a gate. + +// the gates referred to in this problem are digital electronic gates which are very common in electronics. they are often called logic gates and are implemented using transistors. these gates can be very tiny and are almost always present inside most electronic devices such as laptops, televisions and cellphones. you may read more about logic gates in the wikipedia. i guess the most important aspect of a gate is the truth table which is a table which lists down all possible input combinations and the corresponding output values. + +// in the code below, no classes are used and we did not use a linked-list of class nodes or a similar data structure to store the nodes. instead, the maximum number of nodes are pre-allocated. the maximum number of nodes is 100 since a 10x10 grid would have 100 nodes. one would think that this is a waste of memory resources since we are pre-allocating 100 nodes immediately when in fact maybe just a few nodes are needed. this argument is true; however, remember that we are after speed and not beauty or maintainability. since 100 is not that large a number, pre-allocating them immediately may be faster than using some other complex data structure that allocates the exact number of nodes needed. using a complex data structure may be more efficient in using memory resources; however, the added complexity will inevitably slow things down. + +// though no classes are actually used, the concept of classes is somewhat indirectly embedded within the code. each node is represented by an index alignment of several array variables. for instance, the node with an x-coordinate of 9 and a y-coordinate of 8 is represented by the elements with index 98 of the arrays arr_v, arr_s, arr_a and arr_c. array arr_s is for storing the symbol and elements in it can have values of i, o, &, | or !. array arr_v is for storing the calculated output of the nodes. if for example, node 98 is an and gate, then arr_v[98] would correspond to the output of an and gate. + +// array arr_a stores the input arguments of the gate in reference form. arr_a is a 2d array and each node can have up to a maximum of two input arguments. if node 98 is an and gate that takes as its inputs the output of nodes 72 and 25, then arr_a[98][0] and arr_a[98][1] would have the values 72 and 25. it should be noted that the code is written in such a way that arr[98][0] could have a value of 72 and arr[98][1] a value of 25, or vice-versa. the order in which 72 and 25 are stored does not matter. similarly, a real and gate will not mind the order in which its inputs are applied. a two-input and gate can take a 1 in its first input and a 0 in its second input, and output a 0 based on these inputs. if a 0 were input in its first input and a 1 in its second input, the output will still be 0. so, the ordering does not really matter, and we actually use this fact to optimize the code, as will be explained in a while. + +// the array arr_c is used in relation to arr_a. each node in arr_a has two elements corresponding to the input arguments of a given gate. for instance, the input arguments to node 98 are placed in the slots, arr_a[98][0] and arr_a[98][1]. arr_c is used to take note of the next slot to store an input argument. ideally, arr_c should only store 0 or 1 values which would tell us whether to store an input argument into arr_a[98][0] or arr_a[98][1] for the case of gate 98. however, in order to save time, and avoid having to initialize all the elements in arr_c to 0 before testing each test case, we simply use the least significant bit of every element in arr_c. + +// the least significant bit of any number would be 0 or 1 at any given time. for instance, the number 7 would be presented in binary as 0b0111 and its least significant bit (lsb) would be 1. in this case, if we were to store an input argument for node 98, then we would store it in arr_a[98][1]. we then increment the number 7 by 1 to obtain 8, which in binary is 0b1000. now, the least significant bit is 0, so storing another input argument to node 98 would be done in arr_a[98][0]. the protocol used in this code is to increment arr_c[98] whenever a new argument is stored in arr_a[98][i], where is either 0 or 1. to obtain the last input argument stored into arr_a[98], we then simply subtract 1 from arr_c[98]. if, for example, arr_c[98] has a value of 8, then to obtain the last input argument stored into arr_a[98], we simply subtract 1 from 8 to obtain 7, and then obtain the least significant bit of 7 which is 1. as 1 is obtained, then the last input argument stored for node 98 is found in arr_a[98][1]. + +// obtaining the lsb of a number can be done efficiently by bit-wise anding the number with 1. for instance to get the lsb of the number 7, we can simply bit-wise and 7 with 1 to obtain 1. in binary form, this would be represented as: + +// 0d7: 0b0111 +// 0d1: 0b0001 +// =========== +// 0d1: 0b0001 + +// in bit-wise anding, the result will only be 1 if the inputs are both 1. in this case, as the lsb's of both 7 and 1 are both 1, then the result is 1, which is the lsb of 7. + +// we have explained the arrays, arr_v, arr_a, arr_c. now, we explain array, arr_r. arr_r lists all the nodes present in a given circuit. in the example given in the problem specification, arr_r would have the values: 0, 2, 11, 21, 13, and 23. these values correspond to the first column of the program inputs: + +// 00i 11 13 .. +// 02i 11 13 .. +// 11& 21 .. +// 21o .. +// 13| 23 .. +// 23o .. + +// arr_i is used to store the nodes that correspond to input gates. in the example of the problem specification, arr_i would contain 0 and 2. lastly, arr_o is used to store the nodes corresponding to output gates. in the problem example, arr_o would contain gates 21 and 23. + +// once all program inputs are encoded into the arrays, the program starts working by first doing some initialization for each test case for a given circuit. initialization is done by setting all nodes in arr_v that are listed in arr_r to -1 to indicate unknown gate output values (lines 123-125). the following screen output shows the values of the arrays after this initialization of arr_v: + +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : -1 -1 -1 -1 -1 -1 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 + +// notice that arr_i contains the input gates of the circuit, which are 00 and 02. arr_o contains the output gates, 21 and 23. arr_r lists all the gates present in the circuit. for the rest of the arrays, print_arrays() prints the elements with indices sourced from arr_r. for instance, arr_v lists the values of its elements with indices 0, 2, 11, 21, 13, 23. these indices are found in arr_r. arr_s lists the symbols stored in its elements with indices 0, 2, 11, 21, 13, and 23. using the output of print_arrays(), we can see that node 11 is an and gate that takes as its inputs the outputs of nodes 00 and 02. similarly, node 21 is an output gate that takes as its input the output of node 11. for node 21, notice that arr_c[21] has a value of 01. this means that the last input argument for node 21 was stored in arr_a[21][1-1] which is arr_a[21][0]. remember that arr_c[x] takes note of the next position in arr_a[x][i] to store an input argument for node x in, whether in arr_a[x][0] or in arr_a[x][1]. + +// after initializing arr_v to -1 values to indicate unknown output values, we initialize arr_v with the inputs as specified by the test case (lines 130-132). for the first test case, 00, in the problem specification, the output of print_arrays() will show the following upon input initialization: + +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : 0 0 -1 -1 -1 -1 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 + +// notice that gates 0 and 2 now have output values of 0 and 0. + +// next, we iterate through the nodes, attempting to calculate the output values for each of the nodes. if a node in arr_v has a value of -1, then it means that the output value of that node is unknown, and so we try to calculate for it. if can be calculated, then we convert the output value from unknown to the calculated value. if the unknown output value of a given gate cannot be calculated because the output values of another gate to which it is connected to is also unknown, then we simply move on to the next gate. we keep iterating through the nodes until all output values of all gates are no longer unknown; that is arr_v does not contain any -1 values anymore (lines 134-178). + +// let us view the output of print_arrays() every time there is an update in one of the values in arr_v: + +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : 0 0 0 -1 -1 -1 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 +// notice above that the output value of gate 11 changes since now, the output values of gates 0 and 2, which are inputs to gate 11, are now known. +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : 0 0 0 0 -1 -1 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 +// since the output of gate 11 is connected to gate 21 which is an output gate, and since gate 11 has value of 0, then gate 21 gains an output value of 0. +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : 0 0 0 0 0 -1 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 +// since gate 13 is an or gate and since its two inputs have values of both 0, then gate 13 obtains an output value of 0. an or gate will output a 1 if at least one of its inputs is 1. in this case, however, both inputs into the or gate are 0, so the output is 0. +// arr_i : 00 02 +// arr_o : 21 23 +// arr_r : 00 02 11 21 13 23 +// arr_v : 0 0 0 0 0 0 +// arr_s : i i & o | o +// arr_c : 00 00 00 01 00 01 +// arr_a0: 00 00 00 11 00 13 +// arr_a1: 00 00 02 00 02 00 +// since gate 13 is connected to gate 23, which is an output gate, then gate 23 obtains a value of 0 as well. once all output values are obtained, we print the output values of the output gates (lines 180-182). + +char arr_s[100]; +int num_nodes, num_i, num_o, arr_i[100], arr_o[100], arr_r[100], arr_v[100], arr_a[100][2], arr_c[100]; + +void print_arrays() +{ + int j, k; + + printf("arr_i : "); + for (j = 0; j < num_i; j += 1) + { + printf("%02d ", arr_i[j]); + } + printf("\n"); + printf("arr_o : "); + for (j = 0; j < num_o; j += 1) + { + printf("%02d ", arr_o[j]); + } + printf("\n"); + printf("arr_r : "); + for (j = 0; j < num_nodes; j += 1) + { + printf("%02d ", arr_r[j]); + } + printf("\n"); + printf("arr_v : "); + for (j = 0; j < num_nodes; j += 1) + { + k = arr_v[arr_r[j]]; + printf("%2d ", k); + } + printf("\n"); + printf("arr_s : "); + for (j = 0; j < num_nodes; j += 1) + { + printf(" %c ", arr_s[arr_r[j]]); + } + printf("\n"); + printf("arr_c : "); + for (j = 0; j < num_nodes; j += 1) + { + printf("%02d ", arr_c[arr_r[j]] & 1); + } + printf("\n"); + printf("arr_a0: "); + for (j = 0; j < num_nodes; j += 1) + { + printf("%02d ", arr_a[arr_r[j]][0]); + } + printf("\n"); + printf("arr_a1: "); + for (j = 0; j < num_nodes; j += 1) + { + printf("%02d ", arr_a[arr_r[j]][1]); + } + printf("\n\n"); +} + +int main() +{ + char line[100], ctmp[100]; + char *pch; + int n, t, node, curr_node, curr_arg, num_unk; + int arg_a, arg_b; + int i, j, k; + char symbol; + + fgets(line, 100, stdin); + sscanf(line, "%d", &n); + + for (i = 0; i < n; i += 1) + { + num_nodes = 0; + num_i = 0; + num_o = 0; + + while (1) + { + fgets(line, 100, stdin); + pch = strtok(line, " \t"); + + if (pch[0] == 'e') + { + break; + } + + ctmp[0] = pch[0]; + ctmp[1] = pch[1]; + ctmp[2] = 0; + curr_node = atoi(ctmp); + arr_r[num_nodes] = curr_node; + num_nodes += 1; + + symbol = pch[2]; + arr_s[curr_node] = symbol; + if (symbol == 'i') + { + arr_i[num_i] = curr_node; + num_i += 1; + } + else if (symbol == 'o') + { + arr_o[num_o] = curr_node; + num_o += 1; + } + + pch = strtok(NULL, " \t"); + while (pch != NULL) + { + if (pch[0] == '.') + { + break; + } + + node = atoi(pch); + curr_arg = arr_c[node] & 1; + arr_a[node][curr_arg] = curr_node; + arr_c[node] += 1; + + pch = strtok(NULL, " \t"); + } + } + + fgets(line, 100, stdin); + sscanf(line, "%d", &t); + + for (j = 0; j < t; j += 1) + { + for (k = 0; k < num_nodes; k += 1) + { + arr_v[arr_r[k]] = -1; + } + + fgets(line, 100, stdin); + pch = strtok(line, " \t"); + + for (k = 0; k < num_i; k += 1) + { + arr_v[arr_i[k]] = (pch[k] == 48 ? 0 : 1); + } + + while (1) + { + num_unk = 0; + + for (k = 0; k < num_nodes; k += 1) + { + node = arr_r[k]; + + if (arr_v[node] == -1) + { + symbol = arr_s[node]; + + arg_a = arr_v[arr_a[node][(arr_c[node] - 1) & 1]]; + + if (arg_a == -1) + { + num_unk += 1; + continue; + } + + if (symbol == '!') + { + arr_v[node] = (arg_a == 0 ? 1 : 0); + continue; + } + else if (symbol == 'o') + { + arr_v[node] = arg_a; + continue; + } + + arg_b = arr_v[arr_a[node][arr_c[node] & 1]]; + + if (arg_b == -1) + { + num_unk += 1; + continue; + } + + if (symbol == '&') + { + arr_v[node] = arg_a & arg_b; + } + else + { + arr_v[node] = arg_a | arg_b; + } + } + } + + if (num_unk <= 0) + { + break; + } + } + + for (k = 0; k < num_o; k += 1) + { + printf("%d", arr_v[arr_o[k]]); + } + printf("\n"); + } + printf("\n"); + } + + return 0; +} diff --git a/286.cpp b/286.cpp new file mode 100644 index 0000000..01749a2 --- /dev/null +++ b/286.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +char B[16][16]; +int A[16][16]; + +bool valid(int r, int c) +{ + return 1 <= r && r <= 8 && 1 <= c && c <= 8; +} +void atk(int r, int c) +{ + if (valid(r, c)) + { + A[r][c] = 1; + } +} + +void ray(int r, int c, int dr, int dc) +{ + if (dr == 0 && dc == 0) + { + return; + } + for (;;) + { + r += dr; + c += dc; + if (!valid(r, c)) + { + break; + } + atk(r, c); + if (B[r][c] != ' ') + { + break; + } + } +} + +void mark() +{ + memset(A, 0, sizeof(A)); + for (int r = 1; r <= 8; r++) + { + for (int c = 1; c <= 8; c++) + { + switch (B[r][c]) + { + case 'p': + atk(r + 1, c - 1); + atk(r + 1, c + 1); + break; + case 'r': + ray(r, c, 1, 0); + ray(r, c, -1, 0); + ray(r, c, 0, 1); + ray(r, c, 0, -1); + break; + case 'n': + for (int i = 0; i < 8; i++) + { + atk(r + "00113344"[i] - '2', + c + "13040413"[i] - '2'); + } + break; + case 'b': + ray(r, c, 1, 1); + ray(r, c, 1, -1); + ray(r, c, -1, 1); + ray(r, c, -1, -1); + break; + case 'q': + for (int R = -1; R <= 1; R++) + { + for (int C = -1; C <= 1; C++) + { + ray(r, c, R, C); + } + } + break; + case 'k': + for (int R = -1; R <= 1; R++) + { + for (int C = -1; C <= 1; C++) + { + atk(r + R, c + C); + } + } + break; + } + } + } +} + +int main() +{ + char buf[256]; + while (gets(buf)) + { + memset(B, ' ', sizeof(B)); + for (int i = 0; i < 64; i++) + { + B[1 + i / 8][1 + (i % 8)] = buf[i]; + } + int r, c; + for (r = 1; r <= 8; r++) + { + for (c = 1; c <= 8; c++) + { + if (B[r][c] == 'K') + { + goto ok; + } + } + } + ok: + mark(); + int attacked = A[r][c]; + int can_move = 0; + for (int dr = -1; dr <= 1; dr++) + { + for (int dc = -1; dc <= 1; dc++) + { + if (dr == 0 && dc == 0) + { + continue; + } + if (!valid(r + dr, c + dc)) + { + continue; + } + mark(); + if (A[r + dr][c + dc]) + { + continue; + } + int t = B[r + dr][c + dc]; + B[r][c] = ' '; + B[r + dr][c + dc] = 'K'; + mark(); + if (!A[r + dr][c + dc]) + { + can_move = 1; + } + B[r][c] = 'K'; + B[r + dr][c + dc] = t; + } + } + if (can_move) + { + printf("game is not over\n"); + } + else if (attacked) + { + printf("checkmate\n"); + } + else + { + printf("draw\n"); + } + } +} diff --git a/288.cpp b/288.cpp new file mode 100644 index 0000000..2c99310 --- /dev/null +++ b/288.cpp @@ -0,0 +1,354 @@ +#include + +using namespace std; + +char str[250000]; + +struct BigNumber +{ + int man; + vector d; + BigNumber() + { + man = 1; + d.clear(); + } + BigNumber(char *str) : man(1) + { + d.clear(); + int temp[4000], i = 0, j, s = 0, len = strlen(str), t; + if (len % 4 != 0) + { + for (t = 0; i < len % 4; i++) + { + t = t * 10 + str[i] - '0'; + } + temp[s++] = t; + } + while (i < len) + { + for (j = 0, t = 0; j < 4; j++) + { + t = t * 10 + str[i++] - '0'; + } + temp[s++] = t; + } + for (i = s - 1; i >= 0; i--) + { + d.push_back(temp[i]); + } + } + BigNumber operator+(BigNumber ref) + { + BigNumber ans; + if (man * ref.man == 1) + { + add(*this, ref, ans); + ans.man = this->man; + } + else + { + if (this->compare(ref)) + { + sub(*this, ref, ans); + ans.man = this->man; + } + else + { + sub(ref, *this, ans); + ans.man = ref.man; + } + } + return ans; + } + BigNumber operator-(BigNumber ref) + { + BigNumber ans; + if (man * ref.man == -1) + { + add(*this, ref, ans); + ans.man = this->man; + } + else + { + if (this->compare(ref)) + { + sub(*this, ref, ans); + if (this->man == 1) + { + ans.man = 1; + } + else + { + ans.man = -1; + } + } + else + { + sub(ref, *this, ans); + if (ans.man == 1) + { + ans.man = -1; + } + else + { + ans.man = 1; + } + } + } + return ans; + } + void operator=(BigNumber ref) + { + this->man = ref.man; + this->d.clear(); + for (int i = 0; i < ref.d.size(); i++) + { + d.push_back(ref.d[i]); + } + } + BigNumber operator*(BigNumber ref) + { + BigNumber ans; + ans.man = this->man * ref.man; + int temp[4000], i, j, p; + memset(temp, 0, sizeof(temp)); + for (i = 0; i < this->d.size(); i++) + for (j = 0; j < ref.d.size(); j++) + { + int t = temp[i + j] + this->d[i] * ref.d[j]; + temp[i + j + 1] += t / 10000; + temp[i + j] = t % 10000; + } + for (p = 3999; temp[p] == 0; p--) + ; + for (i = 0; i <= p; i++) + { + ans.d.push_back(temp[i]); + } + return ans; + } + BigNumber exp(BigNumber e) + { + if (e.d.size() == 0) + { + return BigNumber("1"); + } + char ref[30000], temp[10]; + ref[0] = '\0'; + int i; + BigNumber table[10], ans("1"); + table[0] = BigNumber("1"); + for (i = 1; i < 10; i++) + { + table[i] = table[i - 1] * (*this); + } + sprintf(temp, "%d", e.d[e.d.size() - 1]); + strcat(ref, temp); + for (i = e.d.size() - 2; i >= 0; i--) + { + sprintf(temp, "%04d", e.d[i]); + strcat(ref, temp); + } + for (i = 0; ref[i]; i++) + { + BigNumber p2 = ans * ans; + BigNumber p4 = p2 * p2; + BigNumber p8 = p4 * p4; + ans = p8 * p2 * table[ref[i] - '0']; + } + return ans; + } + bool compare(BigNumber &ref) + { + if (this->d.size() != ref.d.size()) + { + return this->d.size() > ref.d.size(); + } + int k = this->d.size(); + for (int i = k - 1; i >= 0; i--) + { + if (this->d[i] > ref.d[i]) + { + return true; + } + if (this->d[i] < ref.d[i]) + { + return false; + } + } + return true; + } + void add(BigNumber a, BigNumber b, BigNumber &c) + { + int temp[4000], carry = 0, i, p; + memset(temp, 0, sizeof(temp)); + for (i = 0; i < a.d.size(); i++) + { + temp[i] = a.d[i]; + } + for (i = 0; i < b.d.size(); i++) + { + temp[i] += b.d[i] + carry; + carry = temp[i] / 10000; + temp[i] %= 10000; + } + temp[i] += carry; + for (p = 3999; temp[p] == 0; p--) + ; + for (i = 0; i <= p; i++) + { + c.d.push_back(temp[i]); + } + } + void sub(BigNumber a, BigNumber b, BigNumber &c) + { + int temp[4000], carry = 0; + memset(temp, 0, sizeof(temp)); + int i, borrow = 0, p; + for (i = 0; i < a.d.size(); i++) + { + temp[i] = a.d[i]; + } + for (i = 0; i < 4000; i++) + { + temp[i] += borrow; + if (i < b.d.size()) + { + temp[i] -= b.d[i]; + } + if (temp[i] < 0) + { + temp[i] += 10000; + borrow = -1; + } + else + { + borrow = 0; + } + } + for (p = 3999; temp[p] == 0; p--) + ; + for (i = 0; i <= p; i++) + { + c.d.push_back(temp[i]); + } + } + void print() + { + if (d.size() == 0) + { + puts("0"); + return; + } + if (man == -1) + { + putchar('-'); + } + printf("%d", d[d.size() - 1]); + for (int i = d.size() - 2; i >= 0; i--) + { + printf("%04d", d[i]); + } + printf("\n"); + } +}; +void preprocess() +{ + int len = strlen(str), l = 0; + for (int i = 0; i < len; i++) + if (str[i] == '*' && str[i + 1] == '*') + { + str[l++] = '^', i++; + } + else + { + str[l++] = str[i]; + } + str[l] = '\0'; +} +BigNumber fact(char *s) +{ + char *p; + for (p = s; *p != '\0' && *p != '^'; p++) + ; + if (*p == '\0') + { + return BigNumber(s); + } + *p = '\0'; + BigNumber base(s), e = fact(p + 1); + return base.exp(e); +} +BigNumber term(char *s) +{ + char *p, *t, temp; + for (p = s; *p != '\0' && *p != '*'; p++) + ; + if (*p == '\0') + { + return fact(s); + } + temp = *p; + *p = '\0'; + BigNumber ans = fact(s); + *p = temp; + while (*p) + { + for (t = p + 1; *t != '\0' && *t != '*'; t++) + ; + char temp = *t; + *t = '\0'; + BigNumber tt = fact(p + 1); + *t = temp; + p = t; + ans = ans * tt; + } + return ans; +} +BigNumber expr(char *s) +{ + char *p, *t, tt; + for (p = s; *p != '\0' && *p != '+' && *p != '-'; p++) + ; + if (*p == '\0') + { + return term(s); + } + tt = *p; + *p = '\0'; + BigNumber ans = term(s); + *p = tt; + while (*p) + { + for (t = p + 1; *t != '\0' && *t != '+' && *t != '-'; t++) + ; + char tt = *t; + *t = '\0'; + BigNumber temp = term(p + 1); + *t = tt; + if (*p == '+') + { + ans = ans + temp; + } + else + { + ans = ans - temp; + } + p = t; + } + return ans; +} +int main() +{ + while (gets(str) != NULL) + { + if (strlen(str) == 0) + { + continue; + } + preprocess(); + BigNumber ans = expr(str); + ans.print(); + } +} diff --git a/290.cpp b/290.cpp new file mode 100644 index 0000000..9f6ed1e --- /dev/null +++ b/290.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +char Digit[] = "0123456789ABCDE"; +int Val[200], MXB; +char Num[1000000], Rev[1000000], Temp[1000000], ss[1000000], tt[1000000], temp[1000000]; + +void Ini() +{ + int i, j; + for (i = 0; Digit[i]; i++) + { + j = Digit[i]; + Val[j] = i; + } +} +int Mbase() +{ + int max = -1, i, j; + for (i = 0; Num[i]; i++) + { + j = Num[i]; + if (max < Val[j]) + { + max = Val[j]; + } + } + return max; +} +void REV() +{ + int i, m = 0; + for (i = strlen(Temp) - 1; i >= 0; i--) + { + Rev[m++] = Temp[i]; + } + Rev[m] = '\0'; +} +void ADD(int base) +{ + int i, j, sum, k, m = 0; + int carry = 0, d; + for (i = strlen(Num) - 1; i >= 0; i--) + { + j = Num[i]; + k = Rev[i]; + sum = Val[j] + Val[k] + carry; + d = sum % base; + Temp[m++] = Digit[d]; + carry = sum / base; + } + if (carry) + { + Temp[m++] = Digit[carry]; + } + Temp[m] = '\0'; +} +int STEP(int base) +{ + int step = 0, i, m = 0; + for (i = strlen(Num) - 1; i >= 0; i--) + { + ss[m++] = Num[i]; + } + ss[m] = '\0'; + if (!strcmp(ss, Num)) + { + return 0; + } + strcpy(Temp, Num); + REV(); + while (1) + { + ADD(base); + step++; + REV(); + if (!strcmp(Temp, Rev)) + { + break; + } + strcpy(Num, Rev); + strcpy(Rev, Temp); + } + return step; +} +void Cal() +{ + int i, step; + strcpy(temp, Num); + for (i = 15; i >= MXB + 1; i--) + { + step = STEP(i); + printf("%d", step); + if (i > MXB + 1) + { + printf(" "); + } + strcpy(Num, temp); + } +} + +int main() +{ + int i; + Ini(); + while (scanf("%s", Num) != EOF) + { + MXB = Mbase(); + if (MXB == 0) + { + MXB = 1; + } + Cal(); + for (i = MXB; i >= 2; i--) + { + printf(" ?"); + } + printf("\n"); + } + return 0; +} diff --git a/291.cpp b/291.cpp index 4e98ae3..e4ab960 100644 --- a/291.cpp +++ b/291.cpp @@ -1,71 +1,131 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +bool visited[100]; +vector steps; +set results; +vector r; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + void solve(int point, int depth); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} -bool adj[5][5]; -int vec[10]; +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} -void backTrack(int ind, int cnt){ - vec[cnt] = ind; - int i; - if( cnt == 8 ){ - FOI(i, 0, 8) - printf("%d", vec[i] + 1); - printf("\n"); +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::solve(int point, int depth) +{ + if (depth == 8) + { + int result = 0; + for (int i = 0, sz = steps.size(); i < sz; i++) + { + result = result * 10 + steps[i]; + } + results.insert(result); return; } - FOI(i, 0, 4){ - if( adj[ind][i] ){ - adj[ind][i] = adj[i][ind] = false; - backTrack(i, cnt + 1); - adj[ind][i] = adj[i][ind] = true; + Vertex *v = getVertex(point); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + if (visited[point * 10 + v->adj[i]->id] == false) + { + visited[point * 10 + v->adj[i]->id] = true; + visited[v->adj[i]->id * 10 + point] = true; + steps.push_back(v->adj[i]->id); + solve(v->adj[i]->id, depth + 1); + steps.pop_back(); + visited[v->adj[i]->id * 10 + point] = false; + visited[point * 10 + v->adj[i]->id] = false; } } } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - memset(adj, false, sizeof adj); - adj[0][1] = true; adj[1][0] = true; - adj[0][2] = true; adj[2][0] = true; - adj[0][4] = true; adj[4][0] = true; - adj[1][2] = true; adj[2][1] = true; - adj[1][4] = true; adj[4][1] = true; - adj[2][3] = true; adj[3][2] = true; - adj[2][4] = true; adj[4][2] = true; - adj[3][4] = true; adj[4][3] = true; - - backTrack(0, 0); +int main() +{ + Graph g; + g.addEdge(1, 2); + g.addEdge(1, 3); + g.addEdge(1, 5); + g.addEdge(2, 3); + g.addEdge(2, 5); + g.addEdge(3, 4); + g.addEdge(3, 5); + g.addEdge(4, 5); + for (int i = 0; i < 100; i++) + { + visited[i] = false; + } + steps.push_back(1); + g.solve(1, 0); + for (set::iterator it = results.begin(); it != results.end(); it++) + { + r.push_back(*it); + } + sort(r.begin(), r.end()); + for (int i = 0, sz = r.size(); i < sz; i++) + { + cout << r[i] << endl; + } return 0; } diff --git a/293.cpp b/293.cpp new file mode 100644 index 0000000..1b8d132 --- /dev/null +++ b/293.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int lg(int n) +{ + for (int k = 0;; k++) + if ((1 << k) >= n) + { + return k; + } +} + +int type() +{ + int x, y; + char c; + static char s[1024]; + assert(scanf(" %c", &c) == 1); + if (c == '{') + { + int res = 0; + while (scanf(" %[^:}] : ", s) == 1) + { + res += type(); + } + scanf(" } "); + return res; + } + else if (c == '(') + { + for (int n = 0;;) + { + assert(scanf(" %[^,)] %c", s, &c) == 2); + n++; + assert(c == ',' || c == ')'); + if (c == ')') + { + return lg(n); + } + } + } + else if (c == '[') + { + assert(scanf(" %d . . %d ] ", &x, &y) == 2); + return lg(abs(x - y) + 1); + } + else if (c == 'a') + { + assert(scanf(" r r a y [ %d . . %d ] o f ", &x, &y) == 2); + return (abs(x - y) + 1) * type(); + } + else if (c == 's') + { + assert(scanf(" t r i n g ( %d )", &x) == 1); + return x * 7; + } + assert(0 == 1); +} + +int main() +{ + int T; + scanf(" %d", &T); + while (T-- > 0) + { + char name[1024]; + assert(scanf(" %[A-Za-z0-9_] : ", name) == 1); + printf("A \"%s\" message requires %d bits.\n", name, type()); + } +} diff --git a/294.cpp b/294.cpp index 2bd7551..5423598 100644 --- a/294.cpp +++ b/294.cpp @@ -1,50 +1,76 @@ -#include -#include -#include +#include + using namespace std; -#define MAX 66000 -bool P[MAX + 1]; -vector< int > Prime; -void Sieve() { - memset(P, true, sizeof P); - P[0] = P[1] = false; - for (int i = 2; i * i <= MAX; i++) { - if (P[i]) { - Prime.push_back(i); - for (int j = 2 * i; j <= MAX; j += i) - P[j] = false; - } - } +enum +{ + MAX = 1000000000, + SQRT_MAX = 31622 +}; +char isPrime[SQRT_MAX] = {0}; +vector primeNum; +int sieve() +{ + isPrime[0] = 0; + isPrime[1] = 0; + isPrime[2] = 1; + for (int i = 3; i < SQRT_MAX; ++i) + isPrime[i] = i % 2; + int sqrt_n = sqrt((int)SQRT_MAX); + for (int i = 3; i < sqrt_n; i += 2) + { + if (isPrime[i] == 0) + continue; + for (int j = i * i; j < SQRT_MAX; j += i) + isPrime[j] = 0; + } + for (int i = 2; i < SQRT_MAX; ++i) + if (isPrime[i]) + primeNum.push_back(i); } -int main() { - Sieve(); +int getDivisorNumber(int n) +{ + if (n == 1) + return 1; + if (n < SQRT_MAX && isPrime[n]) + return 2; + int total = 1, tmp = n; + for (int i = 0; i < primeNum.size() && primeNum[i] * primeNum[i] <= n; ++i) + { + int div = primeNum[i], exp = 1; + for (; tmp % div == 0; exp++) + tmp /= div; + total *= exp; + if (tmp == 1) + return total; + } + if (total != 1) + return total * 2; + return 2; +} - int T; - scanf("%d", &T); - while (T--) { - int L, U; - scanf("%d%d", &L, &U); - int Div = 1, Num = L; - for (int i = L; i <= U; i++) { - int N = i, D = 1; - for (int j = 0; j < Prime.size(); j++) { - if (N % Prime[j] == 0) { - int C = 0; - while (N % Prime[j] == 0) { - ++C; - N /= Prime[j]; - } - D *= (C + 1); - } - } - if (D > Div) { - Div = D; - Num = i; - } - } - printf("Between %d and %d, %d has a maximum of %d divisors.\n", L, U, Num, Div); - } - return 0; +int main() +{ + sieve(); + int T; + scanf("%d", &T); + while (T--) + { + int a, b; + scanf("%d%d", &a, &b); + int maxi = 0, maxDiv = 0; + for (int i = a; i <= b; ++i) + { + int curDiv = getDivisorNumber(i); + if (curDiv > maxDiv) + { + maxi = i; + maxDiv = curDiv; + } + } + printf("Between %d and %d, %d has a maximum of %d divisors.\n", + a, b, maxi, maxDiv); + } + return 0; } diff --git a/295.cpp b/295.cpp new file mode 100644 index 0000000..414f8e9 --- /dev/null +++ b/295.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +int X[100], Y[100], N; +int width, height; +bool seen[100]; +double diameter; + +bool dfs(int i) +{ + if (height - Y[i] < diameter) + { + return true; + } + if (seen[i]) + { + return false; + } + seen[i] = true; + for (int j = 0; j < N; j++) + { + double dx = X[i] - X[j]; + double dy = Y[i] - Y[j]; + if (dx * dx + dy * dy < diameter * diameter && dfs(j)) + { + return true; + } + } + return false; +} + +bool possible() +{ + memset(seen, 0, sizeof(seen)); + for (int i = 0; i < N; i++) + { + if (Y[i] < diameter && dfs(i)) + { + return false; + } + } + return true; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%d %d %d", &width, &height, &N); + for (int i = 0; i < N; i++) + { + scanf("%d %d", &X[i], &Y[i]); + } + double left = 0, right = height; + while (right - left > 1e-6) + { + double middle = (left + right) / 2; + diameter = middle; + if (possible()) + { + left = middle; + } + else + { + right = middle; + } + } + printf("Maximum size in test case %d is %.4f.\n", cs, left); + } +} diff --git a/296.cpp b/296.cpp new file mode 100644 index 0000000..a5178cf --- /dev/null +++ b/296.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +struct guess +{ + int code; + int perfect; + int correct; +}; + +pair compare_code_and_guess(int code, int guess) +{ + int c[4] = {0, 0, 0, 0}; + int g[4] = {0, 0, 0, 0}; + int counter = 0; + while (code) + { + c[counter] = code % 10; + code /= 10; + counter++; + } + counter = 0; + while (guess) + { + g[counter] = guess % 10; + guess /= 10; + counter++; + } + int perfect = 0; + for (int i = 0; i < 4; i++) + { + if (c[i] == g[i]) + { + c[i] = g[i] = -1; + perfect++; + } + } + int correct = 0; + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + if (c[i] == g[j] && c[i] != -1) + { + c[i] = g[j] = -1; + correct++; + } + } + } + return make_pair(perfect, correct); +} + +bool code_matches_guesses(int code, vector &guesses) +{ + for (int i = 0, sz = guesses.size(); i < sz; i++) + { + pair matches = compare_code_and_guess(code, guesses[i].code); + if (matches.first != guesses[i].perfect || matches.second != guesses[i].correct) + { + return false; + } + } + return true; +} + +int main() +{ + int n, guesses_num; + vector guesses; + char delimiter; + cin >> n; + while (n--) + { + guesses.clear(); + cin >> guesses_num; + while (guesses_num--) + { + guess tmp; + cin >> tmp.code >> tmp.perfect >> delimiter >> tmp.correct; + guesses.push_back(tmp); + } + vector result; + for (int i = 0; i < 10000; i++) + { + if (code_matches_guesses(i, guesses)) + { + result.push_back(i); + } + } + if (result.empty()) + { + cout << "impossible" << endl; + } + else if (result.size() > 1) + { + cout << "indeterminate" << endl; + } + else + { + cout << setw(4) << setfill('0') << result[0] << endl; + } + } + return 0; +} diff --git a/297.cpp b/297.cpp new file mode 100644 index 0000000..7eb2c69 --- /dev/null +++ b/297.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +/* +Quadtrees +297 +*/ +#define MAXN 100000 +char str[MAXN + 5], F[6][MAXN + 5]; +int sum, m; +void Recur(int depth, int p, int k) +{ + int i, j; + j = p * 4 + k; + if (F[depth][j] == 2) + { + return; + } + if (str[m] == 'e') + { + return; + } + if (str[m] == 'f') + { + F[depth][j] = 2; + return; + } + F[depth][j] = 1; + for (i = 0; i < 4; i++) + { + m++; + Recur(depth + 1, j, i); + } +} +void Recur1(int depth, int p, int unit, int k) +{ + int i, j; + j = p * 4 + k; + if (F[depth][j] == 0) + { + return; + } + if (F[depth][j] == 2) + { + sum += (unit * unit); + return; + } + for (i = 0; i < 4; i++) + { + m++; + Recur1(depth + 1, j, unit / 2, i); + } +} +void Cal() +{ + int i, j; + sum = m = 0; + scanf("%s", str); + Recur(0, 0, 0); + m = 0; + scanf("%s", str); + Recur(0, 0, 0); + Recur1(0, 0, 32, 0); + printf("There are %d black pixels.\n", sum); + for (i = 0; i < 6; i++) + for (j = 0; j < MAXN; j++) + { + F[i][j] = 0; + } +} +int main() +{ + int kase; + scanf("%d", &kase); + while (kase--) + { + Cal(); + } + return 0; +} diff --git a/298.cpp b/298.cpp new file mode 100644 index 0000000..884c3f7 --- /dev/null +++ b/298.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +vector nowposx, nowposy, nowvx, nowvy, reposx, reposy, revx, revy; +int main() +{ + int times; + scanf("%d", ×); + for (int i = 0; i < times; i++) + { + nowposx.clear(); + nowposy.clear(); + nowvx.clear(); + nowvy.clear(); + reposx.clear(); + reposy.clear(); + revx.clear(); + revy.clear(); + int x, y; + scanf("%d%d", &x, &y); + bool pos[x][y][7][7]; + memset(pos, 0, sizeof(pos)); + int stx, sty, edx, edy; + scanf("%d%d%d%d", &stx, &sty, &edx, &edy); + bool finish = false; + int nofob; + scanf("%d", &nofob); + bool oc[x][y]; + memset(oc, 0, sizeof(oc)); + for (int j = 0; j < nofob; j++) + { + int obx1, obx2, oby1, oby2; + scanf("%d%d%d%d", &obx1, &obx2, &oby1, &oby2); + for (int k = obx1; k <= obx2; k++) + for (int l = oby1; l <= oby2; l++) + { + oc[k][l] = true; + } + } + if (stx == edx && sty == edy) + { + printf("Optimal solution takes 0 hops.\n"); + finish = true; + } + pos[stx][sty][3][3] = true; + nowposx.push_back(stx); + nowposy.push_back(sty); + nowvx.push_back(0); + nowvy.push_back(0); + int count = 0; + while (!finish) + { + reposx.clear(); + reposy.clear(); + revx.clear(); + revy.clear(); + count++; + for (int j = 0; !finish && j < nowposx.size(); j++) + { + int tempvx, tempvy; + for (int k = -1; !finish && k <= 1; k++) + for (int l = -1; !finish && l <= 1; l++) + { + tempvx = nowvx[j] + k; + tempvy = nowvy[j] + l; + if (tempvx < -3 || tempvx > 3 || tempvy < -3 || tempvy > 3) + { + continue; + } + if (tempvx + nowposx[j] < 0 || tempvx + nowposx[j] >= x || tempvy + nowposy[j] < 0 || tempvy + nowposy[j] >= y) + { + continue; + } + if (oc[tempvx + nowposx[j]][tempvy + nowposy[j]]) + { + continue; + } + if (tempvx + nowposx[j] == edx && tempvy + nowposy[j] == edy) + { + finish = true; + printf("Optimal solution takes %d hops.\n", count); + } + if (pos[tempvx + nowposx[j]][tempvy + nowposy[j]][tempvx + 3][tempvy + 3]) + { + continue; + } + else + { + pos[tempvx + nowposx[j]][tempvy + nowposy[j]][tempvx + 3][tempvy + 3] = true; + reposx.push_back(tempvx + nowposx[j]); + reposy.push_back(tempvy + nowposy[j]); + revx.push_back(tempvx); + revy.push_back(tempvy); + } + } + } + if (int(reposx.size()) == 0) + { + printf("No solution.\n"); + finish = true; + } + else + { + nowposx = reposx; + nowposy = reposy; + nowvx = revx; + nowvy = revy; + } + } + } +} diff --git a/299.cpp b/299.cpp index 33dcd94..fe3ebef 100644 --- a/299.cpp +++ b/299.cpp @@ -1,29 +1,30 @@ -#include -#include - -using namespace std; - -int main() -{ - int no,n,l[50],s; - cin>>no; - for(int z=0;z>n; - for(int i=0;i>l[i]; - for(int j=0;jl[k]) - { - int temp=l[j]; - l[j]=l[k]; - l[k]=temp; - - s++; - } - cout<<"Optimal train swapping takes "< + +using namespace std; + +int main() +{ + int n, t, l, i, j; + int array[100]; + cin >> n; + for (t = 0; t < n; t++) + { + cin >> l; + int cnt = 0, tmp; + for (i = 0; i < l; i++) + { + cin >> array[i]; + } + for (i = 1; i < l; i++) + for (j = 1; j < l; j++) + if (array[j] < array[j - 1]) + { + tmp = array[j]; + array[j] = array[j - 1]; + array[j - 1] = tmp; + cnt++; + } + cout << "Optimal train swapping takes " << cnt << " swaps." << endl; + } + return 0; +} diff --git a/300.cpp b/300.cpp new file mode 100644 index 0000000..b566118 --- /dev/null +++ b/300.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +/** +300 +**/ +char X[20][10] = {"imix", "ik", "akbal", "kan", "chicchan", + "cimi", "manik", "lamat", "muluk", "ok", + "chuen", "eb", "ben", "ix", "mem", "cib", + "caban", "eznab", "canac", "ahau"}; +int ResolveMonth(char P[100]) +{ + char X[19][10] = {"pop", "no", "zip", "zotz", "tzec", "xul", + "yoxkin", "mol", "chen", "yax", "zac", "ceh", + "mac", "kankin", "muan", "pax", "koyab", "cumhu", + "uayet"}; + int i; + for (i = 0; i < 19; i++) + { + if (strcmp(P, X[i]) == 0) + { + return i; + } + } + return -1; +} +int main() +{ + int N, n, d, y; + char M[100]; + scanf("%d", &N); + printf("%d\n", N); + for (n = 0; n < N; n++) + { + scanf("%d.%s%d", &d, &M, &y); + d += ResolveMonth(M) * 20 + y * 365; + y = d / 260; + d %= 260; + strcpy(M, X[d % 20]); + d %= 13; + d++; + printf("%d %s %d\n", d, M, y); + } + return 0; +} diff --git a/301.cpp b/301.cpp new file mode 100644 index 0000000..6171685 --- /dev/null +++ b/301.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +/* +¿ÎÌâÃû³Æ£º¿ÃÔË(TRANSPORTATION) + +×÷Õߣº¼ÆËã»ú032 ºúÓ½¡ 03095218 +*/ + +int tranCapicity, numStations, numOrders; +/* +tranCapicity »ð³µÈÃÿ£» +numStations ³µÕ¾×ÜÊý£» +numOrders ¶©µ¥×ÜÊý£» +*/ + +struct Order +{ + int from, to, passengers;//Æðʼվ£¬Ã¿ÎÕ¾£¬³Ë¿ÃÊý£» + int price, remainings; + /* + price = (to-from)*passengers ÿÕŶ©µ¥µÄ¼ÛÖµ£» + remainings ´ú±í´Ë¶©µ¥ºóËùÓö©µ¥µÄ¼ÛÖµ×ܺã» + */ +} order[50]; + +int maxProfit;//È«¾Ö±äÿ£¬¼Ç¼ËùÓÿÉÄÜ·½°¸µÄ×î´óçÒ棻 +int tran[100];//¼Ç¼»ð³µµÄ³Ë¿ÃÈÃÿÇé¿ö£» + +void solve(int start, int earnings) +/* +start ´ú±íÆðʼµÄ¶©µ¥ºÅ£» +earnings ´ú±íÔÚ´Ë֮ǰËù³¢ÊԵĶ©µ¥µÄ×ܼÛÖµ +*/ +{ + if (earnings > maxProfit)//µ±²úÉúµÄ¼ÛÖµ±ÈmaxProfit´óʱ£¬½«Æ丳¸ømaxProfit; + { + maxProfit = earnings; + } + for (; start < numOrders; start++)//°ÑÿÕŶ©µ¥½øÃÃÒ»´Î±éÀú + { + int i, flag = false; + if (earnings + order[start].remainings < maxProfit) + //ÕâÖÖÇé¿öã¬×î´óçÒæ×ÜÊÇáÓÚmaxProfit,ûÓñØÒªÔÙ½øÃñéÀú + { + return; + } + for (i = order[start].from; i < order[start].to; i++) + //°Ñ´Ë¶©µ¥´ÓÆðʼվµ½Ã¿ÎÕ¾½øÃÃÄ£Äâ + { + if ((tran[i] += order[start].passengers) > tranCapicity) + //µ±Óó˿Ã×ÜÊý³¬¹ý»ð³µÈÃÿʱ£¬²»½ÓÊܴ˶©µ¥ + { + flag = true; + for (; i >= order[start].from; i--) + { + tran[i] -= order[start].passengers; + } + break; + } + } + if (!flag) + { + solve(start + 1, earnings + order[start].price);//ÓõùéÄ£ÄâÃÂÒ»ÕŶ©µ¥µÄÇé¿ö + i--; + for (; i >= order[start].from; i--) + { + tran[i] -= order[start].passengers; + } + } + } +} + +int qsort_cmp(const void *a, const void *b) +{ + return (*(struct Order *)b).price - (*(struct Order *)a).price; +} + +int main() +{ + while (cin >> tranCapicity >> numStations >> numOrders && tranCapicity) + { + int i, s = 0; + for (i = 0; i < numOrders; i++) + { + cin >> order[i].from >> order[i].to >> order[i].passengers; + order[i].price = (order[i].to - order[i].from) * order[i].passengers; + } + memset(tran, 0, sizeof(tran)); + maxProfit = 0; + qsort(order, numOrders, sizeof(order[0]), qsort_cmp); + for (i = numOrders - 1; i >= 0; i--) + { + order[i].remainings = (s += order[i].price); + } + solve(0, 0); + cout << maxProfit << endl; + } + return 0; +} diff --git a/302.cpp b/302.cpp new file mode 100644 index 0000000..dee6f26 --- /dev/null +++ b/302.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +const int INTMAX = 1000000000; +const double PI = 3.141592653589793238462643383279502884197169399375105820974944; + +int stk[1996], conmap[45][1996], constreet[45], top, maxstreet; +bool visit[1996]; + +void Euler(int s) +{ + int i; + for (i = 1; i <= maxstreet; i++) + { + if (conmap[s][i] && !visit[i]) + { + visit[i] = true; + Euler(conmap[s][i]); + stk[top++] = i; + } + } +} + +int main() +{ + int x, y, z, home; + while (scanf("%d %d", &x, &y) == 2 && x && y) + { + int i; + memset(visit, 0, sizeof(visit)); + memset(conmap, 0, sizeof(conmap)); + memset(constreet, 0, sizeof(constreet)); + top = 0; + maxstreet = 0; + scanf("%d", &z); + home = min(x, y); + conmap[x][z] = y; + constreet[x]++; + conmap[y][z] = x; + constreet[y]++; + maxstreet = max(maxstreet, z); + while (1) + { + scanf("%d %d", &x, &y); + if (x == 0 && y == 0) + { + break; + } + scanf("%d", &z); + conmap[x][z] = y; + constreet[x]++; + conmap[y][z] = x; + constreet[y]++; + maxstreet = max(maxstreet, z); + } + for (i = 1; i < 45; i++) + { + if (constreet[i] % 2) + { + break; + } + } + if (i < 45) + { + printf("Round trip does not exist.\n\n"); + } + else + { + Euler(home); + for (i = top - 1; i >= 0; i--) + { + printf("%d", stk[i]); + if (i > 0) + { + printf(" "); + } + } + printf("\n\n"); + } + } + return 0; +} diff --git a/303.cpp b/303.cpp new file mode 100644 index 0000000..b2fee85 --- /dev/null +++ b/303.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define INF 0x7f7f7f7f +const double eps = 1e-11; + +struct point +{ + double x, y; +}; + +double X; +int n; + +int dep(double num) +{ + if (fabs(num) < eps) + { + return 0; + } + else + { + return num > 0 ? 1 : -1; + } +} + +double xmul(point p1, point p2, point p3) +{ + return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y); +} + +bool judge(point A, point B, point C, point D) +{ + double s1, s2; + int d1, d2; + s1 = xmul(A, B, C); + s2 = xmul(A, B, D); + d1 = dep(s1); + d2 = dep(s2); + if (d1 * d2 < 0) + { + X = (C.x * s2 - D.x * s1) / (s2 - s1); + return true; + } + if (d1 * d2 == 0) + { + return true; + } + return false; +} + +int main() +{ + while (~scanf("%d", &n) && n) + { + vector up(n); + vector down(n); + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &up[i].x, &up[i].y); + down[i].x = up[i].x; + down[i].y = up[i].y - 1; + } + double Y = -INF; + X = -INF; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + int k = 0; + while (k < n) + { + if (!judge(up[i], down[j], up[k], down[k])) + { + break; + } + k++; + } + if (k == n) + { + Y = up[n - 1].x; + } + else if (k > max(i, j)) + { + judge(up[i], down[j], up[k - 1], up[k]); + if (X > Y) + { + Y = X; + } + judge(up[i], down[j], down[k - 1], down[k]); + if (X > Y) + { + Y = X; + } + } + } + } + if (Y == up[n - 1].x) + { + printf("Through all the pipe.\n"); + } + else + { + printf("%.2lf\n", Y); + } + } + return 0; +} diff --git a/305.cpp b/305.cpp new file mode 100644 index 0000000..b6f22ca --- /dev/null +++ b/305.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int t, r[] = {0, 2, 7, 5, 30, 169, 441, 1872, 7632, 1740, 93313, 459901, 1358657, 2504881}; + +int main() +{ + while (scanf("%d", &t) == 1 && t != 0) + { + printf("%d\n", r[t]); + } + return 0; +} diff --git a/306.cpp b/306.cpp new file mode 100644 index 0000000..9320878 --- /dev/null +++ b/306.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +/** +306 +**/ +#define MAXN 215 +char MS[MAXN], OUTPUT[MAXN]; +int A[MAXN], TEMP[MAXN]; +int K, N, L, C; +struct ss +{ + char color; +} V[MAXN]; +void ReadMS() +{ + int m = 1, f = 1; + char c; + while (1) + { + c = getchar(); + if (f-- && c == ' ') + { + continue; + } + if (c == '\n') + { + break; + } + MS[m++] = c; + f = 0; + } + L = m - 1; +} +void SET(int k) +{ + int m = 0, n, i, j; + TEMP[m++] = k; + V[k].color = 'r'; + C++; + k = A[k]; + while (1) + { + if (V[k].color == 'r') + { + break; + } + C++; + V[k].color = 'r'; + TEMP[m++] = k; + k = A[k]; + } + for (i = 0; i < m; i++) + { + n = (i + K) % m; + k = TEMP[i]; + j = TEMP[n]; + if (k <= L) + { + OUTPUT[j] = MS[k]; + } + else + { + OUTPUT[j] = ' '; + } + } +} +void Cal() +{ + int i; + C = 0; + for (i = 1; i <= N; i++) + { + if (C >= N) + { + break; + } + if (V[i].color == 'w') + { + SET(i); + } + } + for (i = 1; i <= N; i++) + { + printf("%c", OUTPUT[i]); + } + printf("\n"); +} +void INI() +{ + int i; + for (i = 0; i <= N; i++) + { + V[i].color = 'w'; + } +} +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + for (i = 1; i <= N; i++) + { + scanf("%d", &A[i]); + } + while (1) + { + scanf("%d", &K); + if (!K) + { + break; + } + ReadMS(); + INI(); + Cal(); + } + printf("\n"); + } + return 0; +} diff --git a/307.cpp b/307.cpp new file mode 100644 index 0000000..160a80e --- /dev/null +++ b/307.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +int S[300][300] = {0}, I[300][300], X[300] = {100}, U[300], p, l, n; +int cmp(const void *a, const void *b) +{ + return *(int *)b - *(int *)a; +} +int stick(int lv, int len, int sum, int st, int less) +{ + int i, j, pr, nless; + if (lv == p) + { + return 1; + } + if (len == 0) + { + for (i = lv; i <= n; i++) + { + if (!U[i]) + { + break; + } + } + S[lv][1] = i; + U[i] = 1; + if (X[i] == l) + { + if (stick(lv + 1, 0, 0, 1, 0)) + { + return 1; + } + } + else + { + if (X[i] < X[S[lv - 1][1]]) + { + nless = 1; + } + else + { + nless = 0; + } + if (stick(lv, len + 1, X[i], i + 1, nless)) + { + return 1; + } + } + U[i] = 0; + S[lv][1] = 0; + return 0; + } + for (i = st; i <= n; i++) + { + if (U[i] || (X[i - 1] == X[i] && !U[i - 1]) || sum + X[i] > l) + { + continue; + } + pr = S[lv - 1][len + 1]; + if (!less && X[i] > X[pr]) + { + continue; + } + S[lv][len + 1] = i; + U[i] = 1; + if (sum + X[i] == l) + { + if (stick(lv + 1, 0, 0, 1, 0)) + { + return 1; + } + } + else + { + if (less || X[i] < X[pr]) + { + nless = 1; + } + else + { + nless = 0; + } + if (stick(lv, len + 1, sum + X[i], i + 1, nless)) + { + return 1; + } + } + U[i] = 0; + S[lv][len + 1] = 0; + } + return 0; +} +int main() +{ + int i, j, max, sum; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (i = 1, max = sum = 0; i <= n; i++) + { + scanf("%d", X + i); + if (X[i] > max) + { + max = X[i]; + } + sum += X[i]; + U[i] = 0; + } + qsort(X + 1, n, sizeof(int), cmp); + for (i = 0; i <= n; i++) + { + for (j = 0; j <= n; j++) + { + S[i][j] = 0; + } + } + for (l = max; l < sum; l++) + { + if (sum % l) + { + continue; + } + p = sum / l; + if (stick(1, 0, 0, 1, 0)) + { + break; + } + } + printf("%d\n", l); + } + return 0; +} diff --git a/308.cpp b/308.cpp new file mode 100644 index 0000000..c99af3d --- /dev/null +++ b/308.cpp @@ -0,0 +1,447 @@ +#include + +using namespace std; + +#define UP 0 +#define RIGHT 1 +#define DOWN 2 +#define LEFT 3 +#define MAX_DIR 4 +#define SEARCH_RECT 1 +#define SEARCH_WRAP 2 +#define EPS 1e-8 + +const double double_inf = (double)5.0 * pow(10, 9); + +#define FORIT(type, obj, var) for (type::iterator var = (obj).begin(); var != (obj).end(); ++var) + +typedef double elem; +typedef complex point; +typedef pair line; + +inline bool eq(elem a, elem b) +{ + return abs(a - b) < EPS; +} + +inline bool eqless(elem a, elem b) +{ + return eq(a, b) || a < b; +} + +inline elem cross(const point &a, const point &b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +inline elem dist(const point &a, const point &b, const point &x) +{ + return abs(cross(b - a, x - a)) / abs(b - a); +} + +inline bool intersected_seg(const line &a, const line &b) +{ + double d1 = dist(b.first, b.second, a.first); + double d2 = dist(b.first, b.second, a.second); + if (eq(d1 + d2, 0.0)) + { + return false; + } + return (cross(a.second - a.first, b.first - a.first) * + cross(a.second - a.first, b.second - a.first) <= + EPS && + cross(b.second - b.first, a.first - b.first) * + cross(b.second - b.first, a.second - b.first) <= + EPS); +} + +inline point intersection_seg(const line &a, const line &b) +{ + double d1 = dist(b.first, b.second, a.first); + double d2 = dist(b.first, b.second, a.second); + double t = d1 / (d1 + d2); + return a.first + t * (a.second - a.first); +} + +inline bool LineCrossPoint(line a, line b, point &ret) +{ + if (intersected_seg(a, b)) + { + ret = intersection_seg(a, b); + return true; + } + return false; +} + +class Point +{ +public: + Point() + { + fill(bcon, bcon + MAX_DIR, false); + } + Point(point p) : p(p) + { + fill(bcon, bcon + MAX_DIR, false); + } + + point p; + bool bcon[MAX_DIR]; + point con[MAX_DIR]; + + bool operator<(const Point &t) const + { + if (eq(p.real(), t.p.real())) + { + return !eq(p.imag(), t.p.imag()) && p.imag() > t.p.imag(); + } + else if (p.real() < t.p.real()) + { + return true; + } + return false; + } + bool operator==(const Point &t) const + { + return eq(p.real(), t.p.real()) && eq(p.imag(), t.p.imag()); + } + + bool SetEdge(const line &l, const set &cross_points) + { + set::iterator itcp = cross_points.find(Point(p)); + set::iterator beg = cross_points.begin(); + set::iterator last = cross_points.end(); + if (beg == last) + { + return false; + } + --last; + if (l.first.real() == l.second.real()) + { + // åž‚ç›´ãªç·šåˆ† + if (distance(beg, itcp)) + { + Point t = *(--itcp); + ++itcp; + con[UP] = bcon[UP] ? (t < Point(con[UP]) ? con[UP] : t.p) : t.p; + bcon[UP] = true; + } + if (distance(itcp, last)) + { + Point t = *(++itcp); + --itcp; + con[DOWN] = bcon[DOWN] ? (Point(con[DOWN]) < t ? con[DOWN] : t.p) : t.p; + bcon[DOWN] = true; + } + } + else + { + // æ°´å¹³ãªç·šåˆ† + if (distance(itcp, last)) + { + Point t = *(++itcp); + --itcp; + con[RIGHT] = bcon[RIGHT] ? (Point(con[RIGHT]) < t ? con[RIGHT] : t.p) : t.p; + bcon[RIGHT] = true; + } + if (distance(beg, itcp)) + { + Point t = *(--itcp); + ++itcp; + con[LEFT] = bcon[LEFT] ? (t < Point(con[LEFT]) ? con[LEFT] : t.p) : t.p; + bcon[LEFT] = true; + } + } + return true; + } +}; + +class Line +{ +public: + Line() {} + Line(point a, point b) : l(a, b) {} + + int id; + line l; + set cross_points; +}; + +class State +{ +public: + int dir; + Point p; +}; + +bool dfs(State now, const set &cpset, vector &ret, set &visited) +{ + if (visited.find(now.p) != visited.end()) + { + vector::iterator itc = find(ret.begin(), ret.end(), now.p); + ret.erase(ret.begin(), itc); + return true; + } + else + { + visited.insert(Point(now.p)); + ret.push_back(Point(now.p)); + for (int i = -1; i <= 2; ++i) + { + State new_state; + new_state.dir = (now.dir + i + MAX_DIR) % MAX_DIR; + if (now.p.bcon[new_state.dir]) + { + set::iterator itcp = cpset.find(Point(now.p.con[new_state.dir])); + if (itcp == cpset.end()) + { + continue; + } + new_state.p = *itcp; + if (dfs(new_state, cpset, ret, visited)) + { + return true; + } + else + { + return false; + } + } + } + return false; + } +} + +void CreateGraph(set &cross_set, + map &vis, + map> &T, + const vector &lines) +{ + vector v; + FORIT(set, cross_set, its) + { + v.push_back(*its); + } + for (int i = 0; i < v.size(); ++i) + { + if (T.find(v[i]) != T.end()) + { + FORIT(set, T[v[i]], itv) + { + int l = *itv; + v[i].SetEdge(lines[l].l, lines[l].cross_points); + } + } + } + cross_set.clear(); + vis.clear(); + for (unsigned int i = 0; i < v.size(); ++i) + { + cross_set.insert(v[i]); + vis.insert(make_pair(v[i], false)); + } +} + +void CrossPoints(vector &lines, + set &cross_point, + map> &T) +{ + for (int i = 0; i < (int)lines.size(); ++i) + { + for (int j = 0; j < (int)lines.size(); ++j) + { + if (i != j) + { + point cross; + Point cp; + if (LineCrossPoint(lines[i].l, lines[j].l, cross)) + { + cp = Point(cross); + cross_point.insert(cp); + lines[i].cross_points.insert(cp); + lines[j].cross_points.insert(cp); + T[cp].insert(i); + T[cp].insert(j); + } + } + } + } +} + +bool LineConnected(const line &a, const line &b, line &conn) +{ + bool ret = false; + elem aminx = min(a.first.real(), a.second.real()); + elem amaxx = max(a.first.real(), a.second.real()); + elem aminy = min(a.first.imag(), a.second.imag()); + elem amaxy = max(a.first.imag(), a.second.imag()); + elem bminx = min(b.first.real(), b.second.real()); + elem bmaxx = max(b.first.real(), b.second.real()); + elem bminy = min(b.first.imag(), b.second.imag()); + elem bmaxy = max(b.first.imag(), b.second.imag()); + if (eq(aminx, amaxx) && eq(aminx, bminx) && eq(bminx, bmaxx)) + { + if (eqless(aminy, bminy) && eqless(bminy, amaxy)) + { + conn = line(point(aminx, aminy), point(aminx, max(amaxy, bmaxy))); + ret = true; + } + else if (eqless(bminy, aminy) && eqless(aminy, bmaxy)) + { + conn = line(point(aminx, bminy), point(aminx, max(amaxy, bmaxy))); + ret = true; + } + } + else if (eq(aminy, amaxy) && eq(aminy, bminy) && eq(bminy, bmaxy)) + { + if (eqless(aminx, bminx) && eqless(bminx, amaxx)) + { + conn = line(point(aminx, aminy), point(max(amaxx, bmaxx), aminy)); + ret = true; + } + else if (eqless(bminx, aminx) && eqless(aminx, bmaxx)) + { + conn = line(point(bminx, aminy), point(max(amaxx, bmaxx), aminy)); + ret = true; + } + } + return ret; +} + +void ConnectLines(vector &lines) +{ + int sz = 1, aftersz = 0; + while (sz != aftersz) + { + sz = lines.size(); + for (unsigned int i = 0; i < lines.size(); ++i) + { + for (unsigned int j = i + 1; j < lines.size(); ++j) + { + line conn; + if (LineConnected(lines[i].l, lines[j].l, conn)) + { + lines[i].l = conn; + lines.erase(lines.begin() + j); + } + } + } + aftersz = lines.size(); + } +} + +int main() +{ + while (true) + { + int n; + int ans = 0; + set cross_point; + map> T; + map vis_map; + vector lines; + scanf("%d", &n); + if (n == 0) + { + break; + } + for (int i = 0; i < n; ++i) + { + int x1, y1, x2, y2; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + Line l = Line(point(x1, y1), point(x2, y2)); + l.id = i; + lines.push_back(l); + } + ConnectLines(lines); + CrossPoints(lines, cross_point, T); + CreateGraph(cross_point, vis_map, T, lines); + while (true) + { + vector circuit; + set visited; + State ini_state; + bool bEnd = true; + ini_state.dir = RIGHT; + for (set::iterator itcp = cross_point.begin(); itcp != cross_point.end(); ++itcp) + { + if (!vis_map[*itcp]) + { + ini_state.p = *itcp; + bEnd = false; + break; + } + } + if (bEnd) + { + break; + } + visited.clear(); + if (dfs(ini_state, cross_point, circuit, visited)) + { + vector vperim; + bool bErased = false; + if (circuit.size() >= 4) + { + ++ans; + vis_map[ini_state.p] = true; + visited.clear(); + for (unsigned int i = 0; i < circuit.size() - 1; ++i) + { + vperim.push_back(make_pair(circuit[i].p, circuit[i + 1].p)); + visited.insert(circuit[i]); + } + visited.insert(circuit.back()); + vperim.push_back(make_pair(circuit.back().p, circuit.front().p)); + FORIT(set, cross_point, itc) + { + set cross_set; + if (bErased) + { + itc = cross_point.begin(); + bErased = false; + } + point from = point(itc->p.real(), itc->p.imag() + 0.5); + point to = point(itc->p.real() + double_inf, itc->p.imag() + 0.5); + line l(from, to); + if (visited.find(*itc) != visited.end()) + { + continue; + } + for (unsigned int i = 0; i < vperim.size(); ++i) + { + point cross; + if (LineCrossPoint(l, vperim[i], cross)) + { + cross_set.insert(Point(cross)); + } + } + if (cross_set.size() & 1) + { + cross_point.erase(itc); + itc = cross_point.begin(); + bErased = true; + } + } + } + for (unsigned int i = 1; i < circuit.size(); ++i) + { + set::iterator itc = cross_point.find(circuit[i]); + if (itc != cross_point.end()) + { + cross_point.erase(itc); + } + } + } + else + { + for (unsigned int i = 0; i < circuit.size(); ++i) + { + vis_map[circuit[i]] = true; + } + } + } + printf("%d\n", ans); + } + return 0; +} diff --git a/309.cpp b/309.cpp new file mode 100644 index 0000000..ee5a52e --- /dev/null +++ b/309.cpp @@ -0,0 +1,168 @@ +#include + +using namespace std; + +/* + Problem: FORCAL + Author: Jan Kotas, Michal Koucky + Algorithm: simple parser + Complexity: linear +*/ + +char m[256]; +char tk[64]; + +int main() +{ + int i, c, te, tl, tt, nt, pt; + memset(m, 0, sizeof(m)); + for (i = '0'; i <= '9'; i++) + { + m[i] = 1; + } + for (i = 'A'; i <= 'Z'; i++) + { + m[i] = 2; + } + for (i = 'a'; i <= 'z'; i++) + { + m[i] = 2; + } + m['_'] = 2; + m['+'] = m[';'] = m[','] = m['('] = m[')'] = 3; + m['-'] = 4; + m[':'] = 5; + m['='] = 6; + m[' '] = m['\t'] = 7; + /* 8 - new line */ + /* 9 - comment */ + te = 0; + tl = 0; + tt = 8; + for (;;) + { + if ((c = getchar()) == EOF) + { + break; + } + nt = 0; + if (c == '\n') + { + if (tt == 8) + { + if (te) + { + puts("TOKEN ERROR"); + } + puts(""); + te = 0; + } + nt = 8; + } + else if (te || tt == 9) + { + continue; + } + else + switch (m[c]) + { + case 1: /* 0..9 */ + if (tt != 1 && tt != 2) + { + nt = 1; + } + break; + case 2: /* a..z, A..Z */ + if (tt == 1) + { + tt = 2; + } + else if (tt != 2) + { + nt = 2; + } + break; + case 3: /* +();, */ + nt = 3; + break; + case 4: /* - */ + if (tt == 4) + { + tt = 9; + } + else + { + nt = 4; + } + break; + case 5: /* : */ + nt = 5; + break; + case 6: /* = */ + if (tt == 5) + { + tt = 6; + } + else + { + nt = -1; + te = 1; + } + break; + case 7: /* space, tab */ + nt = 7; + break; + default: + nt = -1; + te = 1; + } + if (nt) + { + pt = 0; + switch (tt) + { + case 1: + if (tl <= 8) + { + pt = 1; + } + else + { + te = 1; + } + break; + case 2: + if (tl <= 32) + { + pt = 1; + } + else + { + te = 1; + } + break; + case 3: + case 4: + pt = 1; + break; + case 5: + te = 1; + break; + case 6: + pt = 1; + break; + } + if (pt) + { + tk[tl] = 0; + puts(tk); + } + tt = nt; + tl = 0; + } + if (tl < 64) + { + tk[tl++] = c; + } + } +} diff --git a/310.cpp b/310.cpp new file mode 100644 index 0000000..135d740 --- /dev/null +++ b/310.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +#define MAXD 600000 +#define HASH 70000 + +char a[110], b[110], w[20], z[20], st[MAXD][20], t[210]; +int next[MAXD], hash_[HASH], Z; + +void solve() +{ + int i, j, k, front, rear, v; + front = rear = 0; + memset(hash_, 0, sizeof(hash_)); + Z = 0; + for (i = 0; z[i]; i++) + { + Z = Z * 2 + z[i] - 'a' + 1; + } + for (i = 0; w[i]; i++) + { + v = 0; + for (j = i, k = 0; w[j] && z[k]; j++, k++) + { + v = v * 2 + w[j] - 'a' + 1; + st[rear][k] = w[j]; + } + st[rear][k] = '\0'; + if (!hash_[v]) + { + hash_[v] = 1; + ++rear; + } + } + while (front < rear) + { + if (hash_[Z]) + { + break; + } + for (i = j = 0; st[front][i]; i++) + { + if (st[front][i] == 'a') + { + for (k = 0; a[k]; k++) + { + t[j++] = a[k]; + } + } + else + { + for (k = 0; b[k]; k++) + { + t[j++] = b[k]; + } + } + } + t[j] = '\0'; + for (i = 0; t[i]; i++) + { + v = 0; + for (j = i, k = 0; t[j] && z[k]; j++, k++) + { + st[rear][k] = t[j]; + v = v * 2 + t[j] - 'a' + 1; + } + st[rear][k] = '\0'; + if (!hash_[v]) + { + hash_[v] = 1; + ++rear; + } + } + front++; + } + if (front != rear) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } +} + +int main() +{ + while (gets(a) != NULL) + { + gets(b); + gets(w); + gets(z); + if (z[0] == '\0') + { + printf("YES\n"); + } + else + { + solve(); + } + } + return 0; +} diff --git a/311.cpp b/311.cpp new file mode 100644 index 0000000..eb259cd --- /dev/null +++ b/311.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +/* + +int main() +{ + int n1,n2,n3,n4,n5,n6,sum; + while (cin>>n1>>n2>>n3>>n4>>n5>>n6) + { + if (n1==0&&n2==0&&n3==0&&n4==0&&n5==0&&n6==0) break; + sum=n6; + sum+=n5; + n1-=11*n5; + sum+=n4; + if (n2>=5*n4) n2-=5*n4; + else if (n2<5*n4) + { + n1-=20*n4-4*n2; + n2=0; + } + sum+=n3/4; + if (n3%4==1) + { + sum++; + if (n2>=5) + { + n2-=5; + n1-=7; + } + else if (n2<5&&n2>0) + { + n1-=27-4*n2; + n2=0; + } + else if (n2<=0) + { + n1-=27; + n2=0; + } + } + else if (n3%4==2) + { + sum++; + if (n2>=3) + { + n2-=3; + n1-=6; + } + else if (n2<3&&n2>0) + { + n1-=18-4*n2; + n2=0; + } + else if (n2<=0) + { + n1-=18; + n2=0; + } + } + else if (n3%4==3) + { + sum++; + if (n2>=1) + { + n2-=1; + n1-=5; + } + else if (n2<1) + { + n1-=9; + n2=0; + } + } + if (n2>0) + { + if (n2>9) + { + sum+=(n2+8)/9; + n1-=36-4*(n2%9); + } + else if (n2==9) sum++; + else if (n2<9) + { + sum++; + n1-=36-4*(n2%9); + } + } + if (n1>0) + { + if (n1>36) sum+=(n1+35)/36; + else if (n1<=36) sum++; + } + cout< + +using namespace std; + +// map_[][]: 0-white, -1-black, -2-wall, >0-number +int m, n, map_[32][32]; + +void Fill(int x, int y) +{ + if (map_[y][x] == -1) + { + map_[y][x] = -2; + Fill(x + 1, y); + Fill(x - 1, y); + Fill(x, y + 1); + Fill(x, y - 1); + } +} + +char output[32 * 3 + 1][32 * 5 + 1]; + +int main() +{ + while (scanf("%d %d", &m, &n) == 2) + { + if (m == 0 && n == 0) + { + break; + } + int i, j, counter = 1; + for (i = 0; i < m + 2; i++) + { + for (j = 0; j < n + 2; j++) + { + map_[i][j] = -1; + } + } + for (i = 1; i <= m; i++) + { + for (j = 1; j <= n; j++) + { + int t; + scanf("%d", &t); + map_[i][j] = -t; + } + } + Fill(0, 0); + for (i = 1; i <= m; i++) + { + for (j = 1; j <= n; j++) + { + if (map_[i][j] != 0) + { + continue; + } + if ((map_[i][j - 1] < 0 && map_[i][j + 1] >= 0) || + (map_[i - 1][j] < 0 && map_[i + 1][j] >= 0)) + { + map_[i][j] = counter++; + } + } + } + memset(output, ' ', (32 * 5 + 1) * (32 * 3 + 1)); + for (i = 1; i <= m; i++) + { + for (j = 1; j <= n; j++) + { + int a, b; + int ox = (j - 1) * 5, oy = (i - 1) * 3; + if (map_[i][j] == -2) + { + continue; + } + for (a = 0; a < 6; a++) + { + output[oy][ox + a] = output[oy + 3][ox + a] = '+'; + } + for (a = 0; a < 4; a++) + { + output[oy + a][ox] = output[oy + a][ox + 5] = '+'; + } + if (map_[i][j] == -1) + for (a = 0; a < 4; a++) + for (b = 0; b < 6; b++) + { + output[oy + a][ox + b] = '+'; + } + if (map_[i][j] > 0) + { + output[oy + 1][ox + 1 + 0] = '0' + map_[i][j] / 100; + output[oy + 1][ox + 1 + 1] = '0' + (map_[i][j] / 10) % 10; + output[oy + 1][ox + 1 + 2] = '0' + map_[i][j] % 10; + } + } + } + // 1.no skip 2.skip empty 3.skip upper/lower empty + int o_width = n * 5 + 1, o_height = m * 3 + 1; + int start = 0, end = o_height - 1; + for (i = start; i <= end; i++) + { + int r; + for (r = o_width - 1; r >= 0 && output[i][r] == ' '; r--) + ; + for (j = 0; j <= r; j++) + { + putchar(output[i][j]); + } + putchar('\n'); + } + putchar('\n'); + } +} diff --git a/313.cpp b/313.cpp new file mode 100644 index 0000000..38cbca5 --- /dev/null +++ b/313.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +struct Seg +{ + double a, b; +} s[500]; +double cx, cy, r, bx, by; +int n; +void solve(double &x, double &y) +{ + double delta = (bx - cx) * (bx - cx) + (by - cy) * (by - cy); + double k2 = r / sqrt(delta - r * r); + double k1 = -(bx - cx) / (by - cy); + y = (k1 + k2) / (1 - k1 * k2) * by + bx; + x = (k1 - k2) / (1 + k1 * k2) * by + bx; +} +int compare(const void *a, const void *b) +{ + double t = ((Seg *)a)->a - ((Seg *)b)->a; + if (t < 0) + { + return -1; + } + else if (t >= 0) + { + return 1; + } +} +int main() +{ + while (scanf("%d", &n), n) + { + scanf("%lf %lf", &bx, &by); + for (int i = 0; i < n; i++) + { + scanf("%lf %lf %lf", &cx, &cy, &r); + solve(s[i].a, s[i].b); + } + qsort(s, n, sizeof(Seg), compare); + double x1 = s[0].a, x2 = s[0].b; + for (int i = 1; i < n; i++) + { + if (s[i].a <= x2) + { + if (s[i].b > x2) + { + x2 = s[i].b; + } + } + else + { + printf("%.2lf %.2lf\n", x1, x2); + x1 = s[i].a; + x2 = s[i].b; + } + } + printf("%.2lf %.2lf\n\n", x1, x2); + } + return 0; +} diff --git a/314.cpp b/314.cpp new file mode 100644 index 0000000..edb5f03 --- /dev/null +++ b/314.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +/* +Robot +314 +*/ + +#define MAXN 55 +#define MAX MAXN *MAXN * 4 +int B[MAXN][MAXN]; +int D[] = {0, 1, 2, 3}; +int X[] = {1, 0, -1, 0}; +int Y[] = {0, -1, 0, 1}; +char G[MAXN][MAXN]; +char ff[MAXN][MAXN][4]; +char Side[4][7] = {"south", "west", "north", "east"}; +int R, C, sr, sc, er, ec; +struct Queue +{ + int r, c, s; + int count; +} Q[MAX]; +int QH, QT; +void Set() +{ + int i, j; + for (i = 0; i <= R; i++) + { + memset(G[i], 0, C); + } + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + if (B[i][j]) + { + G[i][j] = 1; + G[i][j + 1] = 1; + G[i + 1][j] = 1; + G[i + 1][j + 1] = 1; + } + for (int k = 0; k < 4; k++) + { + ff[i][j][k] = 0; + } + } + } +} +void Push(int r, int c, int s, int count) +{ + if (ff[r][c][s]) + { + return; + } + Q[QH].r = r; + Q[QH].c = c; + Q[QH].count = count; + Q[QH++].s = s; + QH %= MAX; + ff[r][c][s] = 1; +} +Queue Pop() +{ + Queue temp; + temp.r = Q[QT].r; + temp.c = Q[QT].c; + temp.s = Q[QT].s; + temp.count = Q[QT].count; + QT++; + QT %= MAX; + return temp; +} +int IsEmpty() +{ + return QH == QT; +} +int Bfs(int side) +{ + int i, sd, r, c; + int nr, nc, count, nd1, nd2; + Queue temp; + QH = QT = 0; + if (G[er][ec]) + { + return -1; + } + if (G[sr][sc]) + { + return -1; + } + if (sr == er && sc == ec) + { + return 0; + } + Push(sr, sc, side, 0); + ff[sr][sc][side] = 1; + while (!IsEmpty()) + { + temp = Pop(); + sd = temp.s; + r = temp.r; + c = temp.c; + count = temp.count + 1; + nd1 = sd + 1; + nd2 = sd - 1 + 4; + Push(r, c, nd1 % 4, count); + Push(r, c, nd2 % 4, count); + nd1++; + Push(r, c, nd1 % 4, count + 1); + nr = r; + nc = c; + for (i = 0; i < 3; i++) + { + nr += X[sd]; + nc += Y[sd]; + if (nr >= R || nc >= C || nr <= 0 || nc <= 0) + { + continue; + } + if (G[nr][nc]) + { + break; + } + if (nr == er && nc == ec) + { + return count; + } + Push(nr, nc, sd, count); + } + } + return -1; +} +int Search(char ss[]) +{ + int i; + for (i = 0; i < 4; i++) + { + if (!strcmp(Side[i], ss)) + { + break; + } + } + return i; +} +void Cal(char ss[]) +{ + int p, d; + p = Search(ss); + d = Bfs(p); + cout << d << endl; +} +int main() +{ + int i, j; + char ss[10]; + while (1) + { + cin >> R >> C; + if (!R && !C) + { + break; + } + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + cin >> B[i][j]; + } + } + cin >> sr >> sc >> er >> ec >> ss; + Set(); + Cal(ss); + } + return 0; +} diff --git a/315.cpp b/315.cpp new file mode 100644 index 0000000..36fa16c --- /dev/null +++ b/315.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +#define MAXN 102 + +int p[MAXN], rank_[MAXN], N, cp; +char L[MAXN][MAXN]; + +void Ini() +{ + int i; + for (i = 1; i <= N; i++) + { + p[i] = i; + rank_[i] = 0; + } +} +int Find(int x) +{ + if (x != p[x]) + { + p[x] = Find(p[x]); + } + return p[x]; +} +void Link(int x, int y) +{ + if (rank_[x] > rank_[y]) + { + p[y] = x; + } + else + { + p[x] = y; + if (rank_[x] == rank_[y]) + { + rank_[y]++; + } + } +} +void ReadCase() +{ + char input[1000], *p; + int n, m; + while (gets(input)) + { + p = strtok(input, " "); + n = atoi(p); + if (!n) + { + break; + } + while (p) + { + p = strtok(NULL, " "); + if (p) + { + m = atoi(p); + L[n][m] = L[m][n] = 1; + } + } + } +} +void SolvedCase() +{ + int i, j, k, c = 0; + int x, y, t; + for (i = 1; i <= N; i++) + { + Ini(); + t = N - 1; + for (j = 1; j < N; j++) + { + if (j == i) + { + continue; + } + for (k = j + 1; k <= N; k++) + { + if (k == i) + { + continue; + } + if (L[j][k] == 1) + { + x = Find(j); + y = Find(k); + if (x != y) + { + Link(x, y); + t--; + } + } + } + } + if (t - 1 > 0) + { + c++; + } + } + printf("%d\n", c); +} + +int main() +{ + char input[1000]; + int i, j; + while (gets(input)) + { + sscanf(input, "%d", &N); + if (!N) + { + break; + } + Ini(); + cp = N - 1; + ReadCase(); + SolvedCase(); + for (i = 1; i <= N; i++) + { + for (j = i + 1; j <= N; j++) + { + L[i][j] = L[j][i] = 0; + } + } + } + return 0; +} diff --git a/317.cpp b/317.cpp new file mode 100644 index 0000000..262f5eb --- /dev/null +++ b/317.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +int A[10], B[10], C[10], a[5], b[5], c[5]; +int p[] = {3, 4, 5, 4, 3}; +int d[][5] = {{0, 0, 2, 1, 1}, {0, 1, 2, 0, 1}, {0, 1, 2, 1, 0}, {1, 0, 2, 0, 1}, {1, 0, 2, 1, 0}, {1, 1, 2, 0, 0}}; +int cmp(const void *_p, const void *_q) +{ + int *p = (int *)_p; + int *q = (int *)_q; + return *q - *p; +} +void init() +{ + int i; + for (i = 0; i < 3; i++) + { + scanf("%d", &a[i]); + } + for (i = 0; i < 3; i++) + { + scanf("%d", &b[i]); + } + for (i = 0; i < 3; i++) + { + scanf("%d", &c[i]); + } + qsort(a, 3, sizeof(a[0]), cmp); + qsort(b, 3, sizeof(b[0]), cmp); + qsort(c, 3, sizeof(c[0]), cmp); +} +void solve() +{ + int i, j, k, r, res, max = 0; + for (i = 0; i < 6; i++) + for (j = 0; j < 6; j++) + { + if (j == i) + { + continue; + } + for (k = 0; k < 6; k++) + { + if (k == i || k == j) + { + continue; + } + res = 0; + for (r = 0; r < 5; r++) + { + A[r] = d[i][r], B[r] = d[j][r], C[r] = d[k][r]; + res += p[r] * (a[A[r]] + b[B[r]] + c[C[r]]); + } + if (res > max) + { + max = res; + } + } + } + printf("%d\n", max); +} +int main() +{ + int t, tt; + scanf("%d", &t); + for (tt = 0; tt < t; tt++) + { + init(); + printf("Test #%d\n", tt + 1); + solve(); + printf("\n"); + } + return 0; +} diff --git a/318.cpp b/318.cpp new file mode 100644 index 0000000..47ff0f9 --- /dev/null +++ b/318.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +int main() +{ + int n, m, a, b, x, i, j, count = 1, flag, aa, bb, temp; + int map[502][502], hash[502], now; + double dis[502]; + double length, mmax, mmin, max; + while (scanf("%d %d", &n, &m) && n)//如果写æˆ&&n&&m 那么1 0 这组数æ®è¿‡ä¸åŽ»å’§ã€‚。 + { + memset(hash, 0, sizeof(hash)); + for (i = 1; i <= n; i++)//åˆå§‹åŒ–邻接矩阵,对角线上è·ç¦»éƒ½ä¸º0 + { + for (j = 1; j <= n; j++) + { + map[i][j] = INT_MAX; + } + map[i][i] = 0; + dis[i] = INT_MAX; + } + for (i = 1; i <= m; i++)//输入邻接矩阵,map[a][b] == map[b][a] + { + scanf("%d %d %d", &a, &b, &x); + map[a][b] = x; + map[b][a] = x; + } + now = 1; + dis[now] = 0; + hash[1] = 1; + for (i = 1; i <= n; i++)//Dijkstra 算法 + { + max = INT_MAX; + for (j = 1; j <= n; j++) + if (dis[j] - dis[now] - map[now][j] > 0.000001 && map[now][j] != INT_MAX) + { + dis[j] = dis[now] + map[now][j]; + } + for (j = 1; j <= n; j++) + if (!hash[j] && max - dis[j] > 0.000001) + { + max = dis[j]; + now = j; + } + hash[now] = 1; + } + mmax = 0; + temp = 1; + for (i = 1; i <= n; i++)//找最短路径的最大值 + if (dis[i] > mmax) + { + mmax = dis[i]; + temp = i; + } + mmin = 0; + flag = 1; + for (i = 1; i <= n; i++)//寻找从1出å‘的两æ¡æœ€çŸ­è·¯å¾„到达的点以åŠè¿™ä¸¤ç‚¹ä¹‹é—´çš„è·ç¦»çš„和的最大值 + for (j = 1; j <= n; j++) + { + if (!fabs(dis[i] - map[i][j] - dis[j]) < 0.0000001) + if (dis[i] + map[i][j] - mmax > 0.0000001 && map[i][j] != INT_MAX) + { + flag = 0;//标记是å¦åœ¨key点åœæ­¢è¿˜æ˜¯åœ¨è¾¹ä¸Šåœæ­¢ + if (dis[i] + map[i][j] + dis[j] > mmin) + { + mmin = dis[i] + map[i][j] + dis[j]; + aa = i; + bb = j; + } + } + } + if (flag == 0 && (dis[aa] + dis[bb] + map[aa][bb]) / 2 < mmax) + //如果找到最长è·ç¦»çš„时间比最短路径的时间å°ï¼Œåˆ™æœ€ç»ˆåœ¨key点åœæ­¢ + { + flag = 1; + } + printf("System #%d\n", count++); + if (flag) + { + printf("The last domino falls after %.1lf seconds, at key domino %d.\n", mmax, temp); + } + else + { + length = (dis[aa] + dis[bb] + map[aa][bb]) / 2; + printf("The last domino falls after %.1lf seconds, between key dominoes %d and %d.\n", length, aa < bb ? aa : bb, aa > bb ? aa : bb); + }//这个输出我是按从å°ç‚¹åˆ°å¤§ç‚¹è¾“出的,ä¸æŒ‰è¿™ä¸ªé¡ºåºä¹Ÿå¯ä»¥ã€‚ + printf("\n"); + } + return 0; +} diff --git a/319.cpp b/319.cpp new file mode 100644 index 0000000..80306cc --- /dev/null +++ b/319.cpp @@ -0,0 +1,660 @@ +#include + +using namespace std; + +const double eps = 1e-8; + +inline int dcmp(double x) +{ + if (fabs(x) < eps) + { + return 0; + } + else + { + return x < 0 ? -1 : 1; + } +} +inline double get_dist(double x, double y) +{ + return hypot(x, y); +} + +struct Point +{ + double x, y; + Point(double x = 0, double y = 0) + : x(x), y(y) + { + } + void read() + { + scanf("%lf%lf", &x, &y); + } + void write() + { + printf("%lf %lf", x, y); + } + + bool operator==(const Point &u) const + { + return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0; + } + bool operator!=(const Point &u) const + { + return !(*this == u); + } + bool operator<(const Point &u) const + { + return x < u.x || (x == u.x && y < u.y); + } + bool operator>(const Point &u) const + { + return u < *this; + } + bool operator<=(const Point &u) const + { + return *this < u || *this == u; + } + bool operator>=(const Point &u) const + { + return *this > u || *this == u; + } + Point operator+(const Point &u) + { + return Point(x + u.x, y + u.y); + } + Point operator-(const Point &u) + { + return Point(x - u.x, y - u.y); + } + Point operator*(const double u) + { + return Point(x * u, y * u); + } + Point operator/(const double u) + { + return Point(x / u, y / u); + } + double operator*(const Point &u) + { + return x * u.y - y * u.x; + } +}; +typedef Point Vector; + +struct Line +{ + double a, b, c; + Line(double a = 0, double b = 0, double c = 0) + : a(a), b(b), c(c) + { + } +}; + +struct Circle +{ + Point o; + double r; + Circle() + { + } + Circle(Point o, double r = 0) + : o(o), r(r) + { + } + void read() + { + o.read(), scanf("%lf", &r); + } + Point point(double rad) + { + return Point(o.x + cos(rad) * r, o.y + sin(rad) * r); + } + double get_area(double rad) + { + return rad * r * r / 2; + } +}; + +namespace Punctual +{ + double get_dist(Point a, Point b) + { + double x = a.x - b.x, y = a.y - b.y; + return sqrt(x * x + y * y); + } +}; + +namespace Vectorial +{ + +double get_dot(Vector a, Vector b) +{ + return a.x * b.x + a.y * b.y; +} + +double get_cross(Vector a, Vector b) +{ + return a.x * b.y - a.y * b.x; +} + +double get_len(Vector a) +{ + return sqrt(get_dot(a, a)); +} +double get_plength(Vector a) +{ + return get_dot(a, a); +} +double get_angle(Vector u) +{ + return atan2(u.y, u.x); +} +double get_angle(Vector a, Vector b) +{ + return acos(get_dot(a, b) / get_len(a) / get_len(b)); +} +Vector rot(Vector a, double rad) +{ + return Vector(a.x * cos(rad) - a.y * sin(rad), a.x * sin(rad) + a.y * cos(rad)); +} + +Vector get_norm(Vector a) +{ + double l = get_len(a); + return Vector(-a.y / l, a.x / l); +} +}; + +namespace Linear +{ + using namespace Vectorial; + + Line get_line(double x1, double y1, double x2, double y2) + { + return Line(y2 - y1, x1 - x2, y1 * (x2 - x1) - x1 * (y2 - y1)); + } + Line get_line(double a, double b, Point u) + { + return Line(a, -b, u.y * b - u.x * a); + } + + bool get_intersect(Line p, Line q, Point &o) + { + if (fabs(p.a * q.b - q.a * p.b) < eps) + { + return false; + } + o.x = (q.c * p.b - p.c * q.b) / (p.a * q.b - q.a * p.b); + o.y = (q.c * p.a - p.c * q.a) / (p.b * q.a - q.b * p.a); + return true; + } + + bool get_intersect(Point p, Vector v, Point q, Vector w, Point &o) + { + if (dcmp(get_cross(v, w)) == 0) + { + return false; + } + Vector u = p - q; + double k = get_cross(w, u) / get_cross(v, w); + o = p + v * k; + return true; + } + + double get_dist_to_line(Point p, Point a, Point b) + { + return fabs(get_cross(b - a, p - a) / get_len(b - a)); + } + double get_dist_to_seg(Point p, Point a, Point b) + { + if (a == b) + { + return get_len(p - a); + } + Vector v1 = b - a, v2 = p - a, v3 = p - b; + if (dcmp(get_dot(v1, v2)) < 0) + { + return get_len(v2); + } + else if (dcmp(get_dot(v1, v3)) > 0) + { + return get_len(v3); + } + else + { + return fabs(get_cross(v1, v2) / get_len(v1)); + } + } + + Point get_pt_to_line(Point p, Point a, Point b) + { + Vector v = b - a; + return a + v * (get_dot(v, p - a) / get_dot(v, v)); + } + + bool have_intersect(Point a1, Point a2, Point b1, Point b2) + { + double c1 = get_cross(a2 - a1, b1 - a1), c2 = get_cross(a2 - a1, b2 - a1), c3 = get_cross(b2 - b1, a1 - b1), c4 = get_cross(b2 - b1, a2 - b1); + return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; + } + + bool on_seg(Point p, Point a, Point b) + { + return dcmp(get_cross(a - p, b - p)) == 0 && dcmp(get_dot(a - p, b - p)) < 0; + } +} + +namespace Triangular +{ + using namespace Vectorial; + + double get_angle(double a, double b, double c) + { + return acos((a * a + b * b - c * c) / (2 * a * b)); + } + double get_area(double a, double b, double c) + { + double s = (a + b + c) / 2; + return sqrt(s * (s - a) * (s - b) * (s - c)); + } + double get_area(double a, double h) + { + return a * h / 2; + } + double get_area(Point a, Point b, Point c) + { + return fabs(get_cross(b - a, c - a)) / 2; + } + double get_dir_area(Point a, Point b, Point c) + { + return get_cross(b - a, c - a) / 2; + } +}; + +namespace Polygonal +{ + using namespace Vectorial; + using namespace Linear; + + double get_area(Point *p, int n) + { + double ret = 0; + for (int i = 1; i < n - 1; i++) + { + ret += get_cross(p[i] - p[0], p[i + 1] - p[0]); + } + return fabs(ret) / 2; + } + + int get_convex_hull(Point *p, int n, Point *ch) + { + sort(p, p + n); + int m = 0; + for (int i = 0; i < n; i++) + { + while (m > 1 && dcmp(get_cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 1])) <= 0) + { + m--; + } + ch[m++] = p[i]; + } + int k = m; + for (int i = n - 2; i >= 0; i--) + { + while (m > k && dcmp(get_cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) + { + m--; + } + ch[m++] = p[i]; + } + if (n > 1) + { + m--; + } + return m; + } + + int isPointInPolygon(Point o, Point *p, int n) + { + int wn = 0; + for (int i = 0; i < n; i++) + { + int j = (i + 1) % n; + if (on_seg(o, p[i], p[j])) + { + return 0; + } + int k = dcmp(get_cross(p[j] - p[i], o - p[i])); + int d1 = dcmp(p[i].y - o.y); + int d2 = dcmp(p[j].y - o.y); + if (k > 0 && d1 <= 0 && d2 > 0) + { + wn++; + } + if (k < 0 && d2 <= 0 && d1 > 0) + { + wn--; + } + } + return wn ? -1 : 1; + } +}; + +namespace Circular +{ + using namespace Linear; + using namespace Vectorial; + using namespace Triangular; + + int get_line_circle_intersect(Point p, Point q, Circle O, double &t1, double &t2, vector &sol) + { + Vector v = q - p; + + double a = v.x, b = p.x - O.o.x, c = v.y, d = p.y - O.o.y; + double e = a * a + c * c, f = 2 * (a * b + c * d), g = b * b + d * d - O.r * O.r; + double delta = f * f - 4 * e * g; + if (dcmp(delta) < 0) + { + return 0; + } + if (dcmp(delta) == 0) + { + t1 = t2 = -f / (2 * e); + sol.push_back(p + v * t1); + return 1; + } + + t1 = (-f - sqrt(delta)) / (2 * e); + sol.push_back(p + v * t1); + t2 = (-f + sqrt(delta)) / (2 * e); + sol.push_back(p + v * t2); + return 2; + } + + int get_circle_circle_intersect(Circle o1, Circle o2, vector &sol) + { + double d = get_len(o1.o - o2.o); + + if (dcmp(d) == 0) + { + if (dcmp(o1.r - o2.r) == 0) + { + return -1; + } + return 0; + } + + if (dcmp(o1.r + o2.r - d) < 0) + { + return 0; + } + if (dcmp(fabs(o1.r - o2.r) - d) > 0) + { + return 0; + } + + double a = get_angle(o2.o - o1.o); + double da = acos((o1.r * o1.r + d * d - o2.r * o2.r) / (2 * o1.r * d)); + Point p1 = o1.point(a - da), p2 = o1.point(a + da); + sol.push_back(p1); + if (p1 == p2) + { + return 1; + } + sol.push_back(p2); + return 2; + } + + int get_tan(Point p, Circle o, Vector *v) + { + Vector u = o.o - p; + double d = get_len(u); + if (d < o.r) + { + return 0; + } + else if (dcmp(d - o.r) == 0) + { + v[0] = rot(u, M_PI / 2); + return 1; + } + else + { + double ang = asin(o.r / d); + v[0] = rot(u, -ang); + v[1] = rot(u, ang); + return 2; + } + } + + int get_tan(Circle o1, Circle o2, Point *a, Point *b) + { + int cnt = 0; + if (o1.r < o2.r) + { + swap(o1, o2); + swap(a, b); + } + double d2 = get_len(o1.o - o2.o); + d2 = d2 * d2; + double rdif = o1.r - o2.r, rsum = o1.r + o2.r; + if (d2 < rdif * rdif) + { + return 0; + } + if (dcmp(d2) == 0 && dcmp(o1.r - o2.r) == 0) + { + return -1; + } + + double base = get_angle(o2.o - o1.o); + if (dcmp(d2 - rdif * rdif) == 0) + { + a[cnt] = o1.point(base); + b[cnt] = o2.point(base); + cnt++; + return cnt; + } + + double ang = acos((o1.r - o2.r) / sqrt(d2)); + a[cnt] = o1.point(base + ang); + b[cnt] = o2.point(base + ang); + cnt++; + a[cnt] = o1.point(base - ang); + b[cnt] = o2.point(base - ang); + cnt++; + + if (dcmp(d2 - rsum * rsum) == 0) + { + a[cnt] = o1.point(base); + b[cnt] = o2.point(base); + cnt++; + } + else if (d2 > rsum * rsum) + { + double ang = acos((o1.r + o2.r) / sqrt(d2)); + a[cnt] = o1.point(base + ang); + b[cnt] = o2.point(base + ang); + cnt++; + a[cnt] = o1.point(base - ang); + b[cnt] = o2.point(base - ang); + cnt++; + } + return cnt; + } + + Circle circumscribed_circle(Point p1, Point p2, Point p3) + { + double Bx = p2.x - p1.x, By = p2.y - p1.y; + double Cx = p3.x - p1.x, Cy = p3.y - p1.y; + double D = 2 * (Bx * Cy - By * Cx); + double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x; + double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y; + Point p = Point(cx, cy); + return Circle(p, get_len(p1 - p)); + } + + Circle inscribed_circle(Point p1, Point p2, Point p3) + { + double a = get_len(p2 - p3); + double b = get_len(p3 - p1); + double c = get_len(p1 - p2); + Point p = (p1 * a + p2 * b + p3 * c) / (a + b + c); + return Circle(p, get_dist_to_line(p, p1, p2)); + } + + double get_pub_area_to_triangle(Circle O, Point a, Point b) + { + if (dcmp((a - O.o) * (b - O.o)) == 0) + return 0; + int sig = 1; + double da = get_plength(O.o - a), db = get_plength(O.o - b); + if (dcmp(da - db) > 0) + { + swap(da, db); + swap(a, b); + sig = -1; + } + + double t1, t2; + vector sol; + int n = get_line_circle_intersect(a, b, O, t1, t2, sol); + + if (dcmp(da - O.r * O.r) <= 0) + { + if (dcmp(db - O.r * O.r) <= 0) + return get_dir_area(O.o, a, b) * sig; + + int k = 0; + if (get_plength(sol[0] - b) > get_plength(sol[1] - b)) + k = 1; + + double ret = get_area(O.o, a, sol[k]) + O.get_area(get_angle(sol[k] - O.o, b - O.o)); + double tmp = (a - O.o) * (b - O.o); + return ret * sig * dcmp(tmp); + } + + double d = get_dist_to_seg(O.o, a, b); + if (dcmp(d - O.r) >= 0) + { + double ret = O.get_area(get_angle(a - O.o, b - O.o)); + double tmp = (a - O.o) * (b - O.o); + return ret * sig * dcmp(tmp); + } + + double k1 = O.r / get_len(a - O.o), k2 = O.r / get_len(b - O.o); + Point p = O.o + (a - O.o) * k1, q = O.o + (b - O.o) * k2; + double ret1 = O.get_area(get_angle(p - O.o, q - O.o)); + double ret2 = O.get_area(get_angle(sol[0] - O.o, sol[1] - O.o)) - get_area(O.o, sol[0], sol[1]); + double ret = (ret1 - ret2), tmp = (a - O.o) * (b - O.o); + return ret * sig * dcmp(tmp); + } + + double get_pub_area_to_polygon(Circle O, Point *p, int n) + { + if (dcmp(O.r) == 0) + return 0; + double area = 0; + for (int i = 0; i < n; i++) + { + int u = (i + 1) % n; + area += get_pub_area_to_triangle(O, p[i], p[u]); + } + return fabs(area); + } +}; + +using namespace Linear; +using namespace Polygonal; + +const int MAXN = 505; + +double ans; +int N, E; +Point P[MAXN]; + +void solve(Point o, Point s, double r) +{ + double rad = 2 * M_PI; + int idx = -1; + + for (int i = 0; i < N; i++) + { + double d = get_len(P[i] - o); + if (dcmp(d - r) > 0) + { + continue; + } + double k = get_angle(s - o, P[i] - o); + if (dcmp((s - o) * (P[i] - o)) < 0) + { + k = 2 * M_PI - k; + } + if (dcmp(k - rad) == 0 && (idx == -1 || dcmp(get_len(P[idx] - o) - get_len(P[i] - o)) < 0)) + { + idx = i; + } + if (dcmp(k - rad) < 0) + { + rad = k; + idx = i; + } + } + + if (dcmp(o.y + r) <= 0) + { + if (idx == -1) + { + E = 1, ans = rad * r; + return; + } + } + else + { + Point t = Point(o.x + sqrt(r * r - o.y * o.y), 0); + double k = get_angle(s - o, t - o); + if (dcmp((s - o) * (t - o)) < 0) + { + k = 2 * M_PI - k; + } + + if (idx == -1 || dcmp(k - rad) <= 0) + { + rad = k; + idx = -1; + } + } + + if (idx != -1) + { + rad = get_angle(s - o, P[idx] - o); + solve(P[idx], o + rot(s - o, rad), r - get_len(P[idx] - o)); + if (E == 0) + { + ans += 2 * rad * r; + } + } + else + { + ans = 2 * rad * r; + } +} + +int main() +{ + int t = 1; + double r; + while (scanf("%d%lf", &N, &r) == 2 && r != 0) + { + for (int i = 0; i < N; i++) + { + P[i].read(); + } + ans = E = 0; + solve(Point(0, 0), Point(-r, 0), r); + printf("Pendulum #%d\nLength of periodic orbit = %.2lf\n\n", t++, ans); + } + return 0; +} diff --git a/320.cpp b/320.cpp new file mode 100644 index 0000000..ee22581 --- /dev/null +++ b/320.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int main() +{ + int cases, col, row; + string movement; + cin >> cases; + for (int case_num = 0; case_num < cases; case_num++) + { + cin >> col >> row; + char bitmap[32][32]; + for (int i = 0; i < 32; i++) + { + for (int j = 0; j < 32; j++) + { + bitmap[i][j] = '.'; + } + } + cin >> movement; + for (int i = 0, sz = movement.size(); i < sz; i++) + { + if (movement[i] == '.') + { + break; + } + switch (movement[i]) + { + case 'W': + bitmap[row][col - 1] = 'X'; + col--; + break; + case 'E': + bitmap[row - 1][col] = 'X'; + col++; + break; + case 'N': + bitmap[row][col] = 'X'; + row++; + break; + case 'S': + bitmap[row - 1][col - 1] = 'X'; + row--; + break; + } + } + cout << "Bitmap #" << case_num + 1 << endl; + for (int i = 31; i >= 0; i--) + { + for (int j = 0; j < 32; j++) + { + cout << bitmap[i][j]; + } + cout << endl; + } + cout << endl; + } + return 0; +} diff --git a/321.cpp b/321.cpp new file mode 100644 index 0000000..58a9ddb --- /dev/null +++ b/321.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define Maxn 1200 + +struct node +{ + int p, step, state;//p记录ä½ç½®,state记录状æ€ï¼ˆäºŒè¿›åˆ¶ï¼‰ +}; +bool G[11][11], S[11][11], used[11][Maxn];//G记录房与房之间的关系,S记录开关的信æ¯,usedåˆ¤é‡ +int dis[11][Maxn], num[Maxn]; //dis记录点与状æ€ä¹‹é—´çš„è·ç¦» num记录结果 ans记录结果 +string ans[Maxn]; +node path[11][Maxn];//记录å‰é©±çŠ¶æ€ä»Žè€ŒæŽ¨å‡ºè·¯å¾„ +int main() +{ + int r, d, s, i, j, u, v, x, y, t, c = 1; + while (cin >> r >> d >> s) + { + if (r == 0 && d == 0 && s == 0) + { + break; + } + cout << "Villa #" << c << endl; + c++; + memset(G, false, sizeof(G)); + memset(S, false, sizeof(S)); + memset(used, false, sizeof(used)); + node ts, tp; + for (i = 0; i < d; i++) + { + cin >> x >> y; + G[x][y] = G[y][x] = true; + } + for (i = 0; i < s; i++) + { + cin >> x >> y; + S[x][y] = true; + } + ts.p = 1, ts.step = 0, ts.state = 1; + queue q; + q.push(ts); + used[1][1] = true; + while (!q.empty()) + { + ts = q.front(); + q.pop(); + dis[ts.p][ts.state] = ts.step; + for (i = 1; i <= r; i++)//首先关开关 + { + if (i == ts.p || !S[ts.p][i]) + { + continue; + } + tp = ts; + tp.state = tp.state ^ (1 << (i - 1)); + tp.step++; + if (!used[tp.p][tp.state]) + { + q.push(tp); + used[tp.p][tp.state] = true; + path[tp.p][tp.state] = ts; + } + } + for (i = 1; i <= r; i++) + { + t = ts.state & (1 << (i - 1)); + if (t == 0 || !G[ts.p][i]) + { + continue; + } + tp = ts; + tp.step++; + tp.p = i; + if (used[tp.p][tp.state]) + { + continue; + } + q.push(tp); + path[tp.p][tp.state] = ts; + used[tp.p][tp.state] = true; + } + } + u = r, v = (int)pow(2.0, r - 1); + if (!used[u][v]) + { + cout << "The problem cannot be solved." << endl + << endl; + continue; + } + t = 0; + cout << "The problem can be solved in " << dis[u][v] << " steps:" << endl; + while (1) + { + if (u == 1 && v == 1) + { + break; + } + if (path[u][v].p != u) + { + ans[t] = "move"; + num[t] = u; + t++; + } + else + { + for (i = 0; i < 11; i++) + { + x = v & (1 << i); + y = path[u][v].state & (1 << i); + if (x != y) + { + break; + } + } + if (x) + { + ans[t] = "on"; + num[t] = i + 1; + t++; + } + else + { + ans[t] = "off"; + num[t] = i + 1; + t++; + } + } + x = path[u][v].p, y = path[u][v].state; + u = x, v = y; + } + for (i = t - 1; i >= 0; i--) + { + if (ans[i] == "move") + { + cout << "- Move to room " << num[i] << "." << endl; + } + if (ans[i] == "off") + { + cout << "- Switch off light in room " << num[i] << "." << endl; + } + if (ans[i] == "on") + { + cout << "- Switch on light in room " << num[i] << "." << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/322.cpp b/322.cpp new file mode 100644 index 0000000..c80da55 --- /dev/null +++ b/322.cpp @@ -0,0 +1,346 @@ +#include + +using namespace std; + +char img[19][4][5] =// 19 kinds by 4x4 + { + {{"xx.."}, + {"xx.."}, + {"...."}, + {"...."}}, + {{"xx.."}, + {".xx."}, + {"...."}, + {"...."}}, + {{".x.."}, + {"xx.."}, + {"x..."}, + {"...."}}, + {{".xx."}, + {"xx.."}, + {"...."}, + {"...."}}, + {{"x..."}, + {"xx.."}, + {".x.."}, + {"...."}}, + {{"x..."}, + {"xxx."}, + {"...."}, + {"...."}}, + {{".x.."}, + {".x.."}, + {"xx.."}, + {"...."}}, + {{"xx.."}, + {"x..."}, + {"x..."}, + {"...."}}, + {{"xxx."}, + {"..x."}, + {"...."}, + {"...."}}, + {{"..x."}, + {"xxx."}, + {"...."}, + {"...."}}, + {{"x..."}, + {"x..."}, + {"xx.."}, + {"...."}}, + {{"xx.."}, + {".x.."}, + {".x.."}, + {"...."}}, + {{"xxx."}, + {"x..."}, + {"...."}, + {"...."}}, + {{"xxxx"}, + {"...."}, + {"...."}, + {"...."}}, + {{"x..."}, + {"x..."}, + {"x..."}, + {"x..."}}, + {{".x.."}, + {"xxx."}, + {"...."}, + {"...."}}, + {{"x..."}, + {"xx.."}, + {"x..."}, + {"...."}}, + {{"xxx."}, + {".x.."}, + {"...."}, + {"...."}}, + {{".x.."}, + {"xx.."}, + {".x.."}, + {"...."}}}; +int kinds[19] = {0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6}; +int hk[19] = {2, 2, 3, 2, 3, 2, 3, 3, 2, 2, 3, 3, 2, 1, 4, 2, 3, 2, 3}; +int wk[19] = {2, 3, 2, 3, 2, 3, 2, 2, 3, 3, 2, 2, 3, 4, 1, 3, 2, 3, 2}; +int lk[7] = {0, 1, 3, 5, 9, 13, 15}; +int rk[7] = {0, 2, 4, 8, 12, 14, 18}; +int h, w; +char g[30][30]; +struct BITMAP//16x16 = 256, 256/64 = 4 +{ + unsigned long long f[4]; +}; + +BITMAP bitImg[19][16][16];// transform img->bitImg, [kinds][left_upX][left_upY] + +#define DEBUG (0) +void buildBitImg() +{ + int i, j, k; + int p, q; + memset(&bitImg, 0, sizeof(bitImg)); + for (i = 0; i < 19; i++) + { + for (p = 0; p + hk[i] <= 16; p++) + { + for (q = 0; q + wk[i] <= 16; q++) + { + for (j = 0; j < 4; j++) + { + for (k = 0; k < 4; k++) + { + if (img[i][j][k] == 'x') + { + int pos = (p + j) * 16 + (q + k); + bitImg[i][p][q].f[pos / 64] |= 1LLU << (pos % 64); + } + } + } +#if DEBUG == 2 + for (j = 0; j < 16; j++, puts("")) + { + for (k = 0; k < 16; k++) + { + int pos = j * 16 + k; + int v = (bitImg[i][p][q].f[pos / 64] >> (pos % 64)) & 1; + printf("%d", v); + } + } + puts(""); + getchar(); +#endif + } + } + } +} +void printBITMAP(BITMAP &S) +{ + int i, j; + for (i = 0; i < h; i++, puts("")) + { + for (j = 0; j < w; j++) + { + int pos = i * 16 + j; + int v = (S.f[pos / 64] >> (pos % 64)) & 1; + printf("%d", v); + } + } + puts(""); +} +BITMAP G, X; +BITMAP buf[1000]; +int solution; +int check(BITMAP &Filter, BITMAP &IMG) +{ + int i; + unsigned long long r1, r2; + r1 = 1, r2 = 0; + for (i = 0; i < 4; i++) + { + r1 &= (G.f[i] & IMG.f[i]) == IMG.f[i]; + r2 |= Filter.f[i] & IMG.f[i]; + }//r2 == 0 and r1 == 1 + return r1 == 1 && r2 == 0; +} +void dfs(int idx, BITMAP &S, BITMAP &Filter)//find i-th ship cover map +{ + // G: (x, y) still can place set 1, i.e. can_used[x][y] + // Filter: (x, y) is obstacle set 1, i.e. 'o' + int i, j, k, p, f; + BITMAP NS; + if (solution > h * w) + { + return; + } + if (idx == 7)//find one of solutions + { + for (i = 0; i < 4; i++)//all 'x' must be cover. + { + if ((S.f[i] & X.f[i]) != X.f[i]) + { + return; + } + } + for (i = 0; i < solution; i++) + { + for (j = 0; j < 4; j++) + if (buf[i].f[j] != S.f[j]) + { + break; + } + if (j == 4)//equal + { + return; + } + } + if (solution > h * w) + { + return; + } + for (i = 0; i < 4; i++) + { + buf[solution].f[i] = S.f[i]; + } + solution++; +#if DEBUG == 1 + for (i = 0; i < h; i++, puts("")) + { + for (j = 0; j < w; j++) + { + int pos = i * 16 + j; + int v = (S.f[pos / 64] >> (pos % 64)) & 1; + printf("%d", v); + } + } + puts(""); + getchar(); +#endif + return; + } + for (i = lk[idx]; i <= rk[idx]; i++)//this ship rotation + { + for (j = 0; j + hk[i] <= h; j++) + { + for (k = 0; k + wk[i] <= w; k++) + { + f = check(Filter, bitImg[i][j][k]); + if (f == 1) + { + NS = S; + for (p = 0; p < 4; p++) + { + G.f[p] ^= bitImg[i][j][k].f[p]; + NS.f[p] |= bitImg[i][j][k].f[p]; + } + dfs(idx + 1, NS, Filter); + for (p = 0; p < 4; p++) + { + G.f[p] ^= bitImg[i][j][k].f[p]; + NS.f[p] |= bitImg[i][j][k].f[p]; + } + } + } + } + } +} +int main() +{ + buildBitImg(); + /*freopen("in.txt", "r+t", stdin); + freopen("out2.txt", "w+t", stdout);*/ + int i, j, k, cases = 0; + while (scanf("%d %d", &w, &h) == 2 && h) + { + for (i = 0; i < h; i++) + { + scanf("%s", g[i]); + } + BITMAP Filter, S; + memset(&G, 0, sizeof(G)); + memset(&S, 0, sizeof(S)); + memset(&X, 0, sizeof(X)); + memset(&Filter, 0, sizeof(Filter)); + for (i = 0; i < h; i++) + { + for (j = 0; j < w; j++) + { + int pos = i * 16 + j; + if (g[i][j] == 'o') + { + Filter.f[pos / 64] |= 1LLU << (pos % 64); + } + else + { + G.f[pos / 64] |= 1LLU << (pos % 64);//'.' or 'x' + } + if (g[i][j] == 'x') + { + X.f[pos / 64] |= 1LLU << (pos % 64); + } + } + } + solution = 0; + dfs(0, S, Filter); + printf("Game #%d\n", ++cases); + if (solution == 0) + { + puts("no."); + } + else + { + int update; + int judge[10000] = {}; + do + { + update = 0; + for (i = 0; i < h; i++) + { + for (j = 0; j < w; j++) + { + if (g[i][j] != '.') + { + continue; + } + // if cover '.' -> 'o', must be one solution. + int sol = 0, solidx; + int pos = i * 16 + j, v; + for (k = 0; k < solution; k++) + { + if (judge[k]) + { + continue; + } + v = (buf[k].f[pos / 64] >> (pos % 64)) & 1; + if (v == 0)// one solution don't place (i, j) + { + sol++, solidx = k; + if (sol >= 2) + { + break; + } + } + } + if (sol == 1) + { + judge[solidx] = 1; + update = 1; + } + } + } + } while (update); + int ret = 1; + for (i = 0; i < solution; i++) + { + ret &= judge[i]; + } + if (solution == 1) + { + ret = 1; + } + puts(ret ? "yes." : "no."); + } + puts(""); + } + return 0; +} diff --git a/323.cpp b/323.cpp new file mode 100644 index 0000000..6d20197 --- /dev/null +++ b/323.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define SHIFT 4096 + +typedef struct +{ + int m, diff, sum; + unsigned a[8]; +} jury_t; + +jury_t list_[163840], list_new[163840]; +int tab[32][8192], cnt, m; + +void add(int p, int d, int s) +{ + int i, j, k, r; + for (i = 1, r = 0; i <= cnt; i++) + { + if (list_[i].m >= m) + { + continue; + } + j = tab[list_[i].m + 1][SHIFT + list_[i].diff + d]; + if (j > 0 && list_[j].sum >= (list_[i].sum + s)) + { + continue; + } + list_new[r].m = list_[i].m + 1; + list_new[r].diff = list_[i].diff + d; + list_new[r].sum = list_[i].sum + s; + for (k = 0; k < 8; k++) + { + list_new[r].a[k] = list_[i].a[k]; + } + list_new[r].a[p >> 5] |= 1U << (p & 31); + r++; + } + while (r-- > 0) + { + j = tab[list_new[r].m][SHIFT + list_new[r].diff]; + if (j > 0 && list_[j].sum >= list_new[r].sum) + { + continue; + } + if (j == 0) + { + j = ++cnt; + } + memcpy(&list_[j], &list_new[r], sizeof(jury_t)); + tab[list_[j].m][SHIFT + list_[j].diff] = j; + } +} + +int main() +{ + int i, j, n, t, x, y; + for (t = 1; scanf("%d %d", &n, &m) == 2 && n > 0; t++) + { + printf("Jury #%d\n", t); + memset(tab, 0, sizeof(tab)); + memset(list_, 0, 2 * sizeof(jury_t)); + cnt = 1; + for (i = 0; i < n && scanf("%d %d", &x, &y) == 2; i++) + { + add(i, x - y, x + y); + } + for (i = 0;; i++) + if ((tab[m][SHIFT + i] | tab[m][SHIFT - i]) != 0) + { + break; + } + if (tab[m][SHIFT + i] != 0) + { + j = tab[m][SHIFT + i]; + } + else + { + j = tab[m][SHIFT - i]; + } + if (tab[m][SHIFT - i] != 0) + if (list_[tab[m][SHIFT - i]].sum > list_[j].sum) + { + j = tab[m][SHIFT - i]; + } + printf("Best jury has value %d for prosecution and value %d for defence:\n", + (list_[j].diff + list_[j].sum) / 2, + (list_[j].sum - list_[j].diff) / 2); + for (i = 0; i < n; i++) + if ((list_[j].a[i >> 5] >> (i & 31)) & 1) + { + printf(" %d", i + 1); + } + printf("\n\n"); + } + return 0; +} diff --git a/324.cpp b/324.cpp new file mode 100644 index 0000000..7ea26aa --- /dev/null +++ b/324.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +char mid[10000], in[10000]; +main() +{ + char a[10000]; + int i, j, k, c = 0, d = 0, pp, cary, end; + int m, sum, cary_1, l, x, y, h; + int fact, t, pos; + int digit[20]; + while (scanf("%d", &fact) && fact) + { + for (i = 0; i < 10; i++) + { + digit[i] = 0; + } + for (i = 0; i < 10000; i++) + { + mid[i] = in[i] = a[i] = 0; + } + printf("%d! --\n", fact); + a[0] = 1; + x = 1; + for (i = 2; i <= fact; i++) + { + h = i; + d = 0; + c = 0; + while (h) + { + k = c; + cary = 0; + y = h % 10; + for (j = 0; j < x; j++) + { + pp = y * a[j] + cary; + cary = pp / 10; + in[k++] = pp % 10; + } + if (cary) + { + in[k++] = cary; + } + if (k > d) + { + end = k; + } + else + { + end = d; + } + d = 0; + cary_1 = 0; + for (m = 0; m < end; m++) + { + sum = in[m] + mid[m] + cary_1; + mid[d++] = sum % 10; + cary_1 = sum / 10; + } + if (cary_1) + { + mid[d++] = cary_1; + } + for (l = 0; l < k; l++) + { + in[l] = 0; + } + c++; + h = h / 10; + } + for (j = d - 1; j >= 0; j--) + { + a[j] = mid[j]; + } + for (j = 0; j < d; j++) + { + mid[j] = 0; + } + x = d; + } + for (i = x - 1; i >= 0; i--) + { + pos = a[i]; + digit[pos]++; + } + for (i = 0; i < 5; i++) + { + printf(" (%d)%4d", i, digit[i]); + } + printf("\n"); + for (i = 5; i < 10; i++) + { + printf(" (%d)%4d", i, digit[i]); + } + printf("\n"); + } + return 0; +} diff --git a/325.cpp b/325.cpp new file mode 100644 index 0000000..d29ab85 --- /dev/null +++ b/325.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +void init(char *p) +{ + int i, j, tmp; + for (j = 0; p[j] == ' '; j++) + ; + for (i = j;; i++) + { + if (p[i] == ' ' || p[i] == '\0') + { + tmp = i; + break; + } + } + for (i = j; i < tmp; i++) + { + p[i - j] = p[i]; + } + p[i - j] = '\0'; +} + +bool judge(char *p) +{ + int i, dot = -1, pe = -1, flag[2] = {-1, -1}; + int numdot = 0, nume = 0, numflag = 0; + for (i = 0; p[i] != '\0'; i++) + if (p[i] == '.') + { + dot = i; + numdot++; + } + for (i = 0; p[i] != '\0'; i++) + if (p[i] == 'e' || p[i] == 'E') + { + pe = i; + nume++; + } + for (i = 0; p[i] != '\0'; i++) + { + if (p[i] == '+' || p[i] == '-') + { + flag[numflag] = i; + numflag++; + } + } + if (numflag == 1) + { + if (flag[0] > 0 && flag[0] < pe) + { + return false; + } + if (flag[0] > pe + 1) + { + return false; + } + } + if (numflag == 2) + { + if (flag[0] != 0 && flag[1] != pe + 1) + { + return false; + } + } + if (numdot == 2) + { + return false; + } + if (dot == 0) + { + return false; + } + if (numdot == 0 && nume == 0) + { + return false; + } + if (nume == 1) + { + if (p[pe + 1] == '\0') + { + return false; + } + for (i = pe; p[i] != '\0'; i++) + if (p[i] == '.') + { + return false; + } + if (numdot == 0) + { + return true; + } + } + if (p[dot - 1] < '0' || p[dot - 1] > '9') + { + return false; + } + if (p[dot + 1] < '0' || p[dot + 1] > '9') + { + return false; + } + return true; +} + +int main() +{ + char buf[1000]; + while (cin.getline(buf, 1000)) + { + if (strcmp(buf, "*") == 0) + { + break; + } + init(buf); + cout << buf << " is "; + if (judge(buf) == false) + { + cout << "illegal."; + } + else + { + cout << "legal."; + } + cout << endl; + } + return 0; +} diff --git a/326.cpp b/326.cpp new file mode 100644 index 0000000..8bf3f4c --- /dev/null +++ b/326.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +/* +Extrapolation Using a Difference Table +326 +*/ +int a[50]; +int main() +{ + int n, i, j, k; + scanf("%d", &n); + while (n) + { + for (i = 0; i < n; i++) + { + scanf("%d", &a[i]); + } + scanf("%d", &k); + for (j = n - 1; j > 0; j--) + for (i = 0; i < j; i++) + { + a[i] = a[i + 1] - a[i]; + } + for (i = 0; i < k; i++) + for (j = 1; j < n; j++) + { + a[j] += a[j - 1]; + } + printf("Term %d of the sequence is %d\n", n + k, a[n - 1]); + scanf("%d", &n); + } + return 0; +} diff --git a/327.cpp b/327.cpp new file mode 100644 index 0000000..989a262 --- /dev/null +++ b/327.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +char alp[] = "abcdefghijklmnopqrstuvwxyz"; +char str[200], ch[50], value[50], total[50]; + +int space() +{ + int i, k = 0; + for (i = 0; str[i]; i++) + { + if (str[i] != ' ') + { + str[k++] = str[i]; + } + } + str[k] = '\0'; + return 0; +} +int main() +{ + int i, c, j, k, sum, tc; + char dup[150]; + while (gets(str)) + { + sum = c = tc = 0; + strcpy(dup, str); + space(); + for (i = 0; str[i]; i++) + { + if (str[i] != '+') + { + if (str[i] != '-') + { + tc++; + } + } + } + for (i = 0; alp[i]; i++) + { + for (j = 0; str[j]; j++) + { + if (alp[i] == str[j]) + { + c++; + ch[c] = alp[i]; + if (str[j - 1] == '+' && str[j - 2] == '+') + { + value[c] = i + 2; + total[i] = i + 2; + str[j - 1] = str[j - 2] = '0'; + } + else if (str[j + 1] == '+' && str[j + 2] == '+') + { + value[c] = i + 2; + total[i] = i + 1; + str[j + 1] = str[j + 2] = '0'; + } + else if (str[j - 1] == '+' && str[j - 2] == '+') + { + value[c] = i + 2; + total[i] = i + 2; + str[j + 1] = str[j + 2] = '0'; + } + else if (str[j - 1] == '-' && str[j - 2] == '-') + { + value[c] = i; + total[i] = i; + str[j - 1] = str[j - 2] = '0'; + } + else if (str[j + 1] == '-' && str[j + 2] == '-') + { + value[c] = i; + total[i] = i + 1; + str[j + 1] = str[j + 2] = '0'; + } + else + { + value[c] = i + 1; + total[i] = i + 1; + } + } + } + if (c == tc) + { + break; + } + } + k = 0; + for (i = 0; str[i]; i++) + { + if (str[i] != '0') + { + str[k++] = str[i]; + } + } + str[k] = '\0'; + k = 0; + for (i = 0; str[i]; i++) + { + for (j = 0; alp[j]; j++) + { + if (str[i] == alp[j]) + { + k++; + if (str[i - 1] == '-') + { + sum -= total[j]; + } + else + { + sum += total[j]; + } + } + } + if (k == tc) + { + break; + } + } + printf("Expression: %s\n", dup); + printf(" value = %d\n", sum); + for (i = 1; i <= c; i++) + { + printf(" %c = %d\n", ch[i], value[i]); + } + } + return 0; +} diff --git a/328.cpp b/328.cpp new file mode 100644 index 0000000..fba5ffa --- /dev/null +++ b/328.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +/* + ACM North Central Region, 1993-94 + Problem E, The Finite State Text-Processing Machine + + Ed Karrels, April 1996 +*/ + +typedef struct +{ + char *in_set, *out_str; + char st_nm[9]; +} Trans; + +typedef struct +{ + char name[9]; + int n_tr; + Trans *tr; +} State; + +char *CvtStr(char *str) +{ + char *outs, *r, *w; + outs = (char *)malloc(strlen(str) + 1); + r = str; + w = outs; + while (*r) + { + if (*r == '\\') + { + r++; + switch (*r) + { + case 'b': + *w++ = ' '; + break; + case 'n': + *w++ = '\n'; + break; + case '\\': + *w++ = '\\'; + break; + case '0': + *w++ = 0; + break; + case 'c': + *w++ = -1; + } + r++; + } + else + { + *w++ = *r++; + } + } + *w = 0; + return outs; +} + +int FindSt(State *st, int n_st, const char *st_nm) +{ + int i; + for (i = 0; i < n_st; i++) + if (!strcmp(st_nm, st[i].name)) + { + return i; + } + fprintf(stderr, "no state %s\n", st_nm); + return 0; +} + +int main() +{ + int n_st, i, j; + State *st; + char buf[1000], c, *w; + int cur_st, catch_all, tr_no, mch_no = 1; + scanf("%d", &n_st); + while (n_st) + { + printf("Finite State Machine %d:\n", mch_no++); + n_st++; + st = (State *)malloc(sizeof(State) * n_st); + strcpy(st[0].name, "END"); + for (i = 1; i < n_st; i++) + { + scanf("%s %d", st[i].name, &st[i].n_tr); + st[i].tr = (Trans *)malloc(sizeof(Trans) * st[i].n_tr); + for (j = 0; j < st[i].n_tr; j++) + { + scanf("%s", buf); + st[i].tr[j].in_set = CvtStr(buf); + scanf("%s", st[i].tr[j].st_nm); + scanf("%s", buf); + st[i].tr[j].out_str = CvtStr(buf); + } + } + scanf("%*[^\n]"); + getchar(); + cur_st = FindSt(st, n_st, "START"); + while (cur_st != 0) + { + c = getchar(); + catch_all = tr_no = -1; + for (i = 0; i < st[cur_st].n_tr; i++) + { + if (strchr(st[cur_st].tr[i].in_set, c)) + { + tr_no = i; + break; + } + else if (strchr(st[cur_st].tr[i].in_set, -1)) + { + catch_all = i; + } + } + if (tr_no == -1) + { + tr_no = catch_all; + } + if (tr_no != -1) + { + for (w = st[cur_st].tr[tr_no].out_str; *w; w++) + { + if (*w == -1) + { + putchar(c); + } + else + { + putchar(*w); + } + } + cur_st = FindSt(st, n_st, st[cur_st].tr[tr_no].st_nm); + } + } + for (i = 1; i < n_st; i++) + { + for (j = 0; j < st[i].n_tr; j++) + { + free(st[i].tr[j].in_set); + free(st[i].tr[j].out_str); + } + free(st[i].tr); + } + scanf("%d", &n_st); + } + return 0; +} diff --git a/330.cpp b/330.cpp new file mode 100644 index 0000000..7cf12be --- /dev/null +++ b/330.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +const int nr_chars_max = 15, nr_items_max = 200; + +struct item_name +{ + char name_[nr_chars_max + 1]; + bool operator<(const item_name &itn) const + { + return strcmp(name_, itn.name_) < 0; + } + bool operator==(const item_name &itn) const + { + return !strcmp(name_, itn.name_); + } +}; + +struct item +{ + bool deleted_; + double cost_, price_; + int in_stock_; +}; + +int main() +{ + map items; + char activity[nr_chars_max + 1]; + item_name itn; + int quantity; + double profit = 0.0; + while (scanf("%s", activity) != EOF && activity[0] != '*') + { + switch (activity[0]) + { + case 'n': + { + item it; + scanf("%s %lf %lf", itn.name_, &it.cost_, &it.price_); + it.deleted_ = false, it.in_stock_ = 0; + items[itn] = it; + } + break; + case 'b': + scanf("%s %d", itn.name_, &quantity); + items[itn].in_stock_ += quantity; + break; + case 's': + { + scanf("%s %d", itn.name_, &quantity); + item &it = items[itn]; + it.in_stock_ -= quantity; + profit += (it.price_ - it.cost_) * quantity; + } + break; + case 'd': + { + scanf("%s", itn.name_); + item &it = items[itn]; + it.deleted_ = true; + profit -= it.cost_ * it.in_stock_; + it.in_stock_ = 0; + } + break; + default:// report + double total = 0.0; + puts(" INVENTORY REPORT"); + puts("Item Name Buy At Sell At On Hand Value"); + puts("--------- ------ ------- ------- -----"); + for (map::const_iterator ii = items.begin(), ie = items.end(); + ii != ie; ++ii) + { + const item &it = ii->second; + if (!it.deleted_) + { + double value = it.cost_ * it.in_stock_; + printf("%-10s %9.2lf %12.2lf %12d %12.2lf\n", + ii->first.name_, it.cost_, it.price_, it.in_stock_, value); + total += value; + } + } + puts("------------------------"); + printf("Total value of inventory %12.2lf\n", total); + printf("Profit since last report %12.2lf\n\n", profit); + profit = 0.0; + break; + } + } + return 0; +} diff --git a/331.cpp b/331.cpp new file mode 100644 index 0000000..b03cf30 --- /dev/null +++ b/331.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +int A[6], N, B[15], total, max_; + +int Sorted(int ss[]) +{ + int i; + for (i = 1; i < N; i++) + { + if (ss[i] > ss[i + 1]) + { + return 0; + } + } + return 1; +} +int Cheack(int level) +{ + int i, t, ss[10], x, y; + for (i = 1; i <= N; i++) + { + ss[i] = A[i]; + } + for (i = 1; i <= level; i++) + { + x = B[i]; + y = x + 1; + t = ss[x]; + ss[x] = ss[y]; + ss[y] = t; + } + return Sorted(ss); +} +void Swap(int x, int y) +{ + int t; + t = A[x]; + A[x] = A[y]; + A[y] = t; +} +void Recur(int n, int level) +{ + int i, x; + x = n + 1; + if (A[x] >= A[n]) + { + return; + } + Swap(n, x); + if (Sorted(A)) + { + if (level < max_) + { + max_ = level; + total = 1; + } + else + { + total++; + } + } + if (level >= max_) + { + Swap(n, x); + return; + } + for (i = 1; i < N; i++) + { + Recur(i, level + 1); + } + Swap(n, x); +} +int Cal() +{ + int i; + max_ = 10; + if (N == 1) + { + return 0; + } + if (Sorted(A)) + { + return 0; + } + total = 0; + for (i = 1; i < N; i++) + { + Recur(i, 1); + } + return total; +} + +int main() +{ + int i, ks = 1; + while (scanf("%d", &N) && N) + { + if (!N) + { + break; + } + for (i = 1; i <= N; i++) + { + scanf("%d", &A[i]); + } + printf("There are %d swap maps for input data set %d.\n", Cal(), ks++); + } + return 0; +} diff --git a/332.cpp b/332.cpp new file mode 100644 index 0000000..e898e4c --- /dev/null +++ b/332.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int kase = 1; +int j, k; +char buffer[50]; + +long long gcd(long long a, long long b) +{ + long long temp; + while (b) + { + temp = a % b; + a = b; + b = temp; + } + return a; +} + +int main() +{ + while (cin >> j && j >= 0) + { + cin >> buffer; + k = strlen(buffer) - 2 - j; + double X; + long long p, q; + if (j == 0) + { + sscanf(buffer, "%lf", &X); + p = 0; + for (int i = 2; buffer[i]; i++) + { + p = p * 10 + (long long)(buffer[i] - '0'); + } + // p = (long long)ceil( (X-0.000000005) * pow( 10.0,strlen(buffer)*1.0-2.0 )); + q = (long long)ceil(pow(10.0, strlen(buffer) * 1.0 - 2.0)); + } + else + { + char temp[50] = {'\0'}; + int t = 0; + for (int i = strlen(buffer) - j; buffer[i]; i++) + { + temp[t++] = buffer[i]; + } + temp[t] = '\0'; + strcat(buffer, temp); + sscanf(buffer, "%lf", &X); + p = (long long)ceil((pow(10.0, 1.0 * (k + j)) - pow(10.0, 1.0 * k)) * X); + q = (long long)ceil((pow(10.0, j * 1.0) * pow(10.0, k * 1.0) - pow(10.0, k * 1.0))); + } + long long g = gcd(p, q); + p /= g; + q /= g; + cout << "Case " << kase++ << ": " << p << "/" << q << endl; + } + return 0; +} diff --git a/333.cpp b/333.cpp new file mode 100644 index 0000000..a341428 --- /dev/null +++ b/333.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +char cmd[100], buf[100]; + +void delSpace() +{ + int start, end, i, j; + for (start = 0; isspace(cmd[start]); start++) + ; + for (end = strlen(cmd) - 1; isspace(cmd[end]); end--) + ; + for (j = 0, i = start; i <= end; i++) + { + buf[j++] = cmd[i]; + } + buf[j] = '\0'; +} + +bool solve() +{ + int i, j = 0, numOfEle = 0, numOfHyphen = 0; + int sum[20]; + for (i = 0; buf[i]; i++) + { + if (buf[i] == '-') + { + numOfHyphen++; + } + else if (buf[i] == 'X' && numOfEle == 9) + { + sum[j++] = 10; + numOfEle++; + } + else if (isdigit(buf[i])) + { + sum[j++] = buf[i] - '0'; + numOfEle++; + } + else + { + return false; + } + } + if (numOfEle != 10) + { + return false; + } + for (i = 1; i < 10; i++) + { + sum[i] += sum[i - 1]; + } + for (i = 1; i < 10; i++) + { + sum[i] += sum[i - 1]; + } + if (sum[9] % 11 != 0) + { + return false; + } + return true; +} + +int main() +{ + while (cin.getline(cmd, 100)) + { + delSpace(); + if (solve()) + { + cout << buf << " is correct." << endl; + } + else + { + cout << buf << " is incorrect." << endl; + } + } + return 0; +} diff --git a/334.cpp b/334.cpp new file mode 100644 index 0000000..abeb22a --- /dev/null +++ b/334.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back +#define mp make_pair +#define ALL(C) (C).begin(), (C).end() +int getnum(string &a, map &M, vector &in) +{ + int index = M.size(); + if (M.find(a) == M.end()) + { + M[a] = index, in.pb(a); + }; + return M[a]; +} +const int N = 2000; +vector edge[N]; +vector cango[N]; +bool vis[N]; +bool ispair(int a, int b, vector &va, vector &vb) +{ + return !binary_search(ALL(va), b) && !binary_search(ALL(vb), a); +} +void dfs(int now, vector &a) +{ + if (vis[now]) + { + return; + } + vis[now] = true; + a.pb(now); + rep(i, edge[now].size()) dfs(edge[now][i], a); +} +void op(string a, string b) +{ + cout << "(" << a << "," << b << ")"; +} +void dfsall(int n) +{ + rep(i, n) + { + rep(j, n) vis[j] = false; + dfs(i, cango[i]); + } +} +void solve(int n, vector &in) +{ + dfsall(n); + rep(i, n) sort(ALL(cango[i])); + /* + rep(i,n){ + cout << i <<" " << in[i]<<":"; + rep(j,cango[i].size())cout << in[cango[i][j]]<<" "; + cout << endl; + } + */ + pair p1(-1, -1), p2(-1, -1); + int cnt = 0; + rep(i, n) + { + REP(j, i + 1, n) + { + if (ispair(i, j, cango[i], cango[j])) + { + //cout <> n && n) + { + map M; + vector in; + rep(i, N) edge[i].clear(), cango[i].clear(); + rep(i, n) + { + int tmp; + cin >> tmp; + string a; + int f, t; + cin >> a; + f = getnum(a, M, in); + rep(j, tmp - 1) + { + cin >> a; + t = getnum(a, M, in); + edge[f].pb(t); + f = t; + } + } + cin >> m; + rep(i, m) + { + string a; + int b[2]; + rep(j, 2) cin >> a, b[j] = getnum(a, M, in); + edge[b[0]].pb(b[1]); + } + cout << "Case " << tc++ << ", "; + solve(M.size(), in); + } + return 0; +} diff --git a/335.cpp b/335.cpp new file mode 100644 index 0000000..5c2e146 --- /dev/null +++ b/335.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +typedef struct _mxentry +{ + string wildcard; + int priority; + string machine; +} mx; + +bool theTruthIsOutThere(const mx &a, const mx &b) +{ + return a.priority < b.priority; +} + +int str2int(string &s) +{ + int result = 0; + for (int i = 0, sz = s.size(); i < sz; i++) + { + result = result * 10 + (s[i] - '0'); + } + return result; +} + +bool wildcard_matches(string &wildcard, string machine) +{ + bool result = true; + int w = wildcard.size() - 1, m = machine.size() - 1, i = 0; + while (wildcard[w - i] != '*') + { + if (wildcard[w - i] != machine[m - i]) + { + result = false; + break; + } + i++; + } + return result; +} + +void print_resolution(string machine, map &enabled, vector &mxentries) +{ + bool found = false, good = false; + cout << machine << " =>"; + for (int i = 0, sz = mxentries.size(); i < sz; i++) + { + if (mxentries[i].wildcard[0] == '*') + { + if (!enabled[mxentries[i].machine]) + { + continue; + } + if (wildcard_matches(mxentries[i].wildcard, machine)) + { + cout << " " << mxentries[i].machine; + break; + } + } + else if (mxentries[i].wildcard == machine) + { + if (!enabled[mxentries[i].machine]) + { + continue; + } + cout << " " << mxentries[i].machine; + break; + } + } + cout << endl; +} + +int main() +{ + int n; + char c; + mx tmpentry; + string input, prev, machine; + vector mxentries; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, input); + vector tmp; + istringstream iss(input); + copy(istream_iterator(iss), istream_iterator(), back_inserter(tmp)); + if (tmp.size() == 2) + { + tmpentry.wildcard = prev; + tmpentry.priority = str2int(tmp[0]); + tmpentry.machine = tmp[1]; + } + else + { + prev = tmp[0]; + tmpentry.wildcard = tmp[0]; + tmpentry.priority = str2int(tmp[1]); + tmpentry.machine = tmp[2]; + } + mxentries.push_back(tmpentry); + } + sort(mxentries.begin(), mxentries.end(), theTruthIsOutThere); + /* + for (int i = 0, sz = mxentries.size(); i < sz; i++) { + cout << mxentries[i].wildcard << " " << mxentries[i].priority << " " << mxentries[i].machine << endl; + } + */ + // save state of the box + // it can be either up or down + // initially all boxes are up + // (or enabled in our sitation) + map enabled; + for (int i = 0, sz = mxentries.size(); i < sz; i++) + { + enabled[mxentries[i].machine] = true; + } + while (cin >> c) + { + if (c == 'X') + { + break; + } + cin >> machine; + switch (c) + { + case 'A': + print_resolution(machine, enabled, mxentries); + break; + case 'D': + enabled[machine] = false; + break; + case 'U': + enabled[machine] = true; + break; + } + } + return 0; +} diff --git a/336.cpp b/336.cpp index c8b1a37..351e0f1 100644 --- a/336.cpp +++ b/336.cpp @@ -1,82 +1,101 @@ -#include -#include -using namespace std; -#define FOR(i,N) for(i=0;i<(N);i++) -bool visited[30]; -int NNode,i,j; -int node[30]; -bool mat[30][30]; -int TTL[30]; - -void gen(){ - FOR(i,30){ - node[i]==-1; - visited[i]=false; - TTL[i]=-1; - } - FOR(i,30) - FOR(j,30) - mat[i][j]=0; - NNode=-1; -} -void init(){ - FOR(i,NNode+1){ - TTL[i]=-1; - visited[i]=false; - } -} - - -int find(int i){ - FOR(j,NNode+1) - {if(node[j]==i) return j;} - NNode++; - node[NNode]=i; - return NNode; -} - -int calc(int p,int ttl){ - init(); - int m,ans=0; - queue q; - m=find(p); - q.push(m); - visited[m]=true; - TTL[m]=ttl; - while (!q.empty()){ - m=q.front(); - q.pop(); - if(!TTL[m]) continue; - if(TTL[m]>=1) - FOR(i,NNode+1){ - if (mat[m][i] && !visited[i]){ - q.push(i); - TTL[i]=TTL[m]-1; - visited[i]=true;} - } - } - FOR(i,NNode+1) - if(!visited[i]) ans++; - return ans; -} - -int main(){ - int ans,NC,p,q,m,n,ttl,cases=0; - while (cin>>NC){ - if(!NC) break; - gen(); - FOR (i,NC){ - cin>>p>>q; - m=find(p); - n=find(q); - mat[m][n]=true; - mat[n][m]=true; - } - while (cin>>p>>ttl){ - if(!p && !ttl) break; - ans=calc(p,ttl); - cout<<"Case "<<++cases<<": "< + +using namespace std; + +const int MAXNODE = 35; + +struct Node +{ + int id, ttl; +}; + +bool adj[MAXNODE][MAXNODE]; +int NC, kase = 1; +int nodeID[MAXNODE], cntNode; + +int getIndex(int id) +{ + for (int i = 0; i < cntNode; i++) + if (nodeID[i] == id) + { + return i; + } + return -1; +} + +void input() +{ + cntNode = 0; + memset(adj, 0, sizeof(adj)); + int x, y; + for (int i = 0; i < NC; i++) + { + cin >> x >> y; + int xID = getIndex(x); + if (xID < 0) + { + xID = cntNode; + nodeID[cntNode++] = x; + } + int yID = getIndex(y); + if (yID < 0) + { + yID = cntNode; + nodeID[cntNode++] = y; + } + adj[xID][yID] = adj[yID][xID] = true; + } +} + +int bfs(int start, int ttl) +{ + bool vis[MAXNODE] = {false}; + Node node; + queue q; + node.id = start; + node.ttl = 0; + q.push(node); + while (!q.empty()) + { + node = q.front(); + q.pop(); + if (node.ttl > ttl || vis[node.id]) + { + continue; + } + vis[node.id] = true; + for (int i = 0; i < cntNode; i++) + { + if (adj[node.id][i] && node.ttl < ttl && !vis[i]) + { + Node p; + p.id = i; + p.ttl = node.ttl + 1; + q.push(p); + } + } + } + int res = 0; + for (int i = 0; i < cntNode; i++) + if (!vis[i]) + { + res++; + } + return res; +} + +int main() +{ + while (cin >> NC && NC) + { + input(); + int start, ttl; + while (cin >> start >> ttl && start) + { + int s = getIndex(start); + cout << "Case " << kase++ << ": " << bfs(s, ttl) << " nodes not reachable from node "; + cout << start << " with TTL = " << ttl << "." << endl; + } + } + return 0; +} diff --git a/337.cpp b/337.cpp new file mode 100644 index 0000000..067bbfe --- /dev/null +++ b/337.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +enum mode +{ + OVERWRITE, + INSERT +}; + +void clear_terminal(char terminal[10][10]) +{ + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) + { + terminal[i][j] = ' '; + } +} + +void write_character(char terminal[10][10], char c, int row, int col, mode m) +{ + if (m == INSERT) + { + for (int i = 8; i >= col; i--) + { + terminal[row][i + 1] = terminal[row][i]; + } + terminal[row][col] = c; + } + else// OVERWRITE + { + terminal[row][col] = c; + } +} + +int main() +{ + int n, row, col, case_num = 0; + mode m; + string input; + char terminal[10][10]; + while (cin >> n) + { + if (n == 0) + { + break; + } + case_num++; + row = 0; + col = 0; + m = OVERWRITE; + clear_terminal(terminal); + cin.ignore(100, '\n'); + while (n--) + { + getline(cin, input); + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] == '^') + { + i++; + switch (input[i]) + { + case 'b': + col = 0; + break; + case 'c': + clear_terminal(terminal); + break; + case 'd': + row = min(row + 1, 9); + break; + case 'e': + for (int j = col; j < 10; j++) + { + terminal[row][j] = ' '; + } + break; + case 'h': + row = col = 0; + break; + case 'i': + m = INSERT; + break; + case 'l': + col = max(0, col - 1); + break; + case 'o': + m = OVERWRITE; + break; + case 'r': + col = min(col + 1, 9); + break; + case 'u': + row = max(0, row - 1); + break; + case '^': + write_character(terminal, '^', row, col, m); + col = min(col + 1, 9); + break; + default: + row = input[i] - '0'; + i++; + col = input[i] - '0'; + break; + } + } + else + { + write_character(terminal, input[i], row, col, m); + col = min(col + 1, 9); + } + } + } + cout << "Case " << case_num << endl; + cout << "+----------+" << endl; + for (int i = 0; i < 10; i++) + { + cout << "|"; + for (int j = 0; j < 10; j++) + { + cout << terminal[i][j]; + } + cout << "|" << endl; + } + cout << "+----------+" << endl; + } + return 0; +} diff --git a/338.cpp b/338.cpp new file mode 100644 index 0000000..a282b04 --- /dev/null +++ b/338.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +void mult(char *s, char *a, int b) +{ + int i; + int len = strlen(a); + int cal[100]; + for (i = 0; i < len; i++) + { + cal[i] = (a[i] - '0') * b; + } + for (i = len - 1; i > 0; i--) + { + if (cal[i] >= 10) + { + cal[i - 1] += cal[i] / 10; + cal[i] %= 10; + } + } + if (cal[0] >= 10) + { + s[0] = (cal[0] / 10) + '0'; + s[1] = (cal[0] % 10) + '0'; + for (i = 1; i < len; i++) + { + s[i + 1] = cal[i] + '0'; + } + s[len + 1] = '\0'; + } + else + { + for (i = 0; i < len; i++) + { + s[i] = cal[i] + '0'; + } + s[len] = '\0'; + } +} + +int main() +{ + int i, j; + int alen, blen, len, anslen, max, maxAB; + int cal[100]; + int cnt; + char a[100], b[100]; + char s[100][100]; + char ans[100]; + while (scanf("%s%s", a, b) == 2) + { + alen = strlen(a); + blen = strlen(b); + cnt = 0; + memset(cal, 0, sizeof(cal)); + for (i = 0; i < blen; i++) + { + mult(s[i], a, b[blen - i - 1] - '0'); + if (s[i][0] == '0') + { + continue; + } + cnt++; + len = strlen(s[i]); + for (j = 0; j < len; j++) + { + cal[j + i] += s[i][len - j - 1] - '0'; + } + } + for (i = 0; i < 30; i++) + { + if (cal[i] >= 10) + { + cal[i + 1] += cal[i] / 10; + cal[i] %= 10; + } + } + while (i > 0 && cal[i] == 0) + { + i--; + } + for (j = 0; i >= 0; i--, j++) + { + ans[j] = cal[i] + '0'; + } + ans[j] = '\0'; + anslen = j; + maxAB = (alen > blen ? alen : blen); + max = (maxAB > anslen ? maxAB : anslen); + if (cnt >= 2) + { + for (i = 0; i < max - alen; i++) + { + putchar(' '); + } + puts(a); + for (i = 0; i < max - blen; i++) + { + putchar(' '); + } + puts(b); + for (i = 0; i < max - maxAB; i++) + { + putchar(' '); + } + for (i = 0; i < maxAB; i++) + { + putchar('-'); + } + putchar('\n'); + for (i = 0; i < blen; i++) + { + if (s[i][0] == '0') + { + continue; + } + len = strlen(s[i]); + for (j = 0; j < max - len - i; j++) + { + putchar(' '); + } + puts(s[i]); + } + for (i = 0; i < max; i++) + { + putchar('-'); + } + putchar('\n'); + puts(ans); + } + else + { + for (i = 0; i < max - alen; i++) + { + putchar(' '); + } + puts(a); + for (i = 0; i < max - blen; i++) + { + putchar(' '); + } + puts(b); + for (i = 0; i < max - maxAB; i++) + { + putchar(' '); + } + for (i = 0; i < maxAB; i++) + { + putchar('-'); + } + putchar('\n'); + for (i = 0; i < max - anslen; i++) + { + putchar(' '); + } + puts(ans); + } + putchar('\n'); + } + return 0; +} diff --git a/339.cpp b/339.cpp new file mode 100644 index 0000000..fe0e34f --- /dev/null +++ b/339.cpp @@ -0,0 +1,281 @@ +#include + +using namespace std; + +int R, C; +// 0 - 9: field has a value of specified int +// -1: emptied value +int board[10][40]; + +void clear_board() +{ + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 40; j++) + { + board[i][j] = -1; + } + } +} + +bool removable(int row, int col) +{ + if (row > R || row < 1 || col > C || col < 1) + { + return false; + } + row = R - row; + col = col - 1; + if (board[row][col] == -1) + { + return false; + } + int v = board[row][col]; + if (row > 0 && board[row - 1][col] == v || col > 0 && board[row][col - 1] == v || row + 1 < R && board[row + 1][col] == v || col + 1 < C && board[row][col + 1] == v) + { + return true; + } + return false; +} + +void remove_at(int row, int col) +{ + if (!removable(row, col)) + { + return; + } + row = R - row; + col = col - 1; + int v = board[row][col]; + board[row][col] = -1; + queue> q; + pair tmp = make_pair(row, col); + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && board[tmp.first - 1][tmp.second] == v) + { + board[tmp.first - 1][tmp.second] = -1; + q.push(make_pair(tmp.first - 1, tmp.second)); + } + if (tmp.second > 0 && board[tmp.first][tmp.second - 1] == v) + { + board[tmp.first][tmp.second - 1] = -1; + q.push(make_pair(tmp.first, tmp.second - 1)); + } + if (tmp.first + 1 < R && board[tmp.first + 1][tmp.second] == v) + { + board[tmp.first + 1][tmp.second] = -1; + q.push(make_pair(tmp.first + 1, tmp.second)); + } + if (tmp.second + 1 < C && board[tmp.first][tmp.second + 1] == v) + { + board[tmp.first][tmp.second + 1] = -1; + q.push(make_pair(tmp.first, tmp.second + 1)); + } + } +} + +void remove_column(int col) +{ + if (col + 1 == C) + { + return; + } + for (int i = 0; i < R; i++) + { + board[i][col] = board[i][col + 1]; + board[i][col + 1] = -1; + } + remove_column(col + 1); +} + +bool empty_column(int col) +{ + for (int i = 0; i < R; i++) + { + if (board[i][col] != -1) + { + return false; + } + } + return true; +} + +void remove_empty_columns() +{ + for (int i = 0; i < C; i++) + { + if (empty_column(i)) + { + remove_column(i); + C--; + i--; + } + } +} + +bool board_empty() +{ + for (int i = 0; i < R; i++) + { + for (int j = 0; j < C; j++) + { + if (board[i][j] != -1) + { + return false; + } + } + } + return true; +} + +void show_board() +{ + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 5; j++) + { + cout << board[i][j] << " "; + } + cout << endl; + } + cout << endl; +} + +bool empty_row(int row) +{ + for (int i = 0; i < C; i++) + { + if (board[row][i] != -1) + { + return false; + } + } + return true; +} + +bool should_be_dropped_down(int col) +{ + bool flag = false; + for (int i = 0; i < R; i++) + { + if (board[i][col] != -1) + { + flag = true; + } + else + { + if (flag) + { + return true; + } + } + } + return false; +} + +void drop(int row, int col) +{ + if (row == 0) + { + return; + } + board[row][col] = board[row - 1][col]; + board[row - 1][col] = -1; + drop(row - 1, col); +} + +void drop_down() +{ + for (int i = 0; i < C; i++) + { + if (!should_be_dropped_down(i)) + { + continue; + } + for (int j = R - 1; j >= 0; j--) + { + if (board[j][i] == -1) + { + drop(j, i); + } + } + i--; + } +} + +int main() +{ + int row, col, counter = 0; + int save_R, save_C; + while (cin >> R >> C) + { + if (R == 0 || C == 0) + { + break; + } + save_R = R; + save_C = C; + clear_board(); + counter++; + for (int i = R - 1; i >= 0; i--) + { + for (int j = 0; j < C; j++) + { + cin >> board[i][j]; + } + } + bool game_won = false; + while (cin >> row >> col) + { + if (row == 0 && col == 0) + { + break; + } + if (game_won) + { + continue; + } + remove_at(row, col); + drop_down(); + remove_empty_columns(); + if (board_empty()) + { + game_won = true; + } + } + cout << "Grid " << counter << "." << endl; + if (game_won) + { + cout << " Game Won" << endl; + } + else + { + for (int i = 0; i < save_R; i++) + { + cout << " "; + for (int j = 0; j < save_C; j++) + { + if (j > 0) + { + cout << " "; + } + if (board[i][j] == -1) + { + cout << " "; + } + else + { + cout << board[i][j]; + } + } + cout << endl; + } + } + cout << endl; + } + return 0; +} diff --git a/340.cpp b/340.cpp index 221b7a9..60607ec 100644 --- a/340.cpp +++ b/340.cpp @@ -1,46 +1,69 @@ -#include -#include +#include + using namespace std; -int main() { - int t = 1; - while (true) { - int N; - scanf("%d", &N); - if (!N) break; - printf("Game %d:\n", t++); - int A[N], B[N]; - for (int i = 0; i < N; i++) - scanf("%d", &A[i]); - while (true) { - int C = 0; - for (int i = 0; i < N; i++) { - scanf("%d", &B[i]); - if (!B[i]) ++C; +int main() +{ + int n; + int correct[1000], guess[1000]; + int correct_count[10], guess_count[10], correct_count_copy[10]; + int counter = 0; + while (cin >> n) + { + if (!n) + { + break; + } + counter++; + cout << "Game " << counter << ":" << endl; + for (int i = 0; i < 10; i++) + { + correct_count[i] = 0; + } + for (int i = 0; i < n; i++) + { + cin >> correct[i]; + correct_count[correct[i]]++; + } + while (true) + { + int strong = 0; + int weak = 0; + bool empty = true; + for (int i = 0; i < 10; i++) + { + guess_count[i] = 0; } - if (C == N) break; - bool flag[N]; - int X = 0, Y = 0; - memset(flag, true, sizeof flag); - for (int i = 0; i < N; i++) { - if (A[i] == B[i]) { - ++X; - flag[i] = false; - B[i] = -1; - } + for (int i = 0; i < 10; i++) + { + correct_count_copy[i] = correct_count[i]; } - for (int i = 0; i < N; i++) { - if (flag[i]) { - for (int j = 0; j < N; j++) { - if (A[i] == B[j]) { - ++Y; - B[j] = -1; - break; - } - } + for (int i = 0; i < n; i++) + { + cin >> guess[i]; + if (guess[i] > 0) + { + empty = false; } + if (guess[i] == correct[i]) + { + strong++; + correct_count_copy[correct[i]]--; + } + else + { + guess_count[guess[i]]++; + } + } + if (empty) + { + break; + } + for (int i = 1; i < 10; i++) + { + weak += min(correct_count_copy[i], guess_count[i]); } - printf(" (%d,%d)\n", X, Y); + cout << " (" << strong << "," << weak << ")" << endl; } } return 0; diff --git a/341.cpp b/341.cpp new file mode 100644 index 0000000..1bacfbc --- /dev/null +++ b/341.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int INF = 1000000000; +int dist[150][150]; +int p[150][150]; +vector path; + +void calculate_path(int a, int b) +{ + if (a != b) + { + calculate_path(a, p[a][b]); + } + path.push_back(b); +} + +int main() +{ + int n, s, a, b; + int casenum = 0; + while (cin >> n) + { + if (n == 0) + { + break; + } + casenum++; + for (int i = 0; i < 150; i++) + { + for (int j = 0; j < 150; j++) + { + dist[i][j] = INF; + p[i][j] = i; + } + } + for (int i = 0; i < n; i++) + { + cin >> s; + while (s--) + { + cin >> a >> b; + dist[i + 1][a] = b; + } + } + for (int i = 0; i < 150; i++) + { + dist[i][i] = 0; + } + for (int k = 1; k <= n; k++) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + if (dist[i][k] + dist[k][j] < dist[i][j]) + { + dist[i][j] = dist[i][k] + dist[k][j]; + p[i][j] = p[k][j]; + } + } + } + } + cin >> a >> b; + path.clear(); + calculate_path(a, b); + cout << "Case " << casenum << ": Path ="; + for (int i = 0, sz = path.size(); i < sz; i++) + { + cout << " " << path[i]; + } + cout << "; " << dist[a][b] << " second delay" << endl; + } + return 0; +} diff --git a/342.cpp b/342.cpp new file mode 100644 index 0000000..27a1625 --- /dev/null +++ b/342.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +bool eq(string s, string t) +{ + if (s.size() != t.size()) + { + return false; + } + for (int i = 0; i < (int)s.size(); i++) + if (tolower(s[i]) != tolower(t[i])) + { + return false; + } + return true; +} + +int main() +{ + char buf[1024]; + int N; + for (int cs = 1; gets(buf) && sscanf(buf, "%d", &N) == 1 && N > 0; cs++) + { + vector stk; + printf("Test Case %d\n", cs); + int L = 1, err = 0; + while (L <= N) + { + int c = getchar(); + if (c == '\n') + { + L++; + continue; + } + if (err || c != '<') + { + continue; + } + c = getchar(); + int open = 1; + if (c == '/') + { + open = 0; + c = getchar(); + } + string tag = ""; + while (c != '>') + { + if (!isupper(c)) + { + printf("line %d: bad character in tag name.\n", L); + err = 1; + ungetc(c, stdin); + break; + } + if (tag.size() >= 10) + { + printf("line %d: too many/few characters in tag name.\n", L); + err = 1; + ungetc(c, stdin); + break; + } + tag += (char)c; + c = getchar(); + } + if (err) + { + continue; + } + if (tag.size() < 1) + { + printf("line %d: too many/few characters in tag name.\n", L); + err = 1; + continue; + } + if (open) + { + stk.push_back(tag); + continue; + } + if (stk.size() == 0) + { + printf("line %d: no matching begin tag.\n", L); + err = 1; + continue; + } + if (!eq(stk.back(), tag)) + { + printf("line %d: expected \n", L, stk.back().c_str()); + err = 1; + continue; + } + stk.pop_back(); + } + if (!err && stk.size() != 0) + { + printf("line %d: expected \n", N, stk.back().c_str()); + err = 1; + } + if (!err) + { + printf("OK\n"); + } + } +} diff --git a/343.cpp b/343.cpp index 28e630d..597a6ef 100644 --- a/343.cpp +++ b/343.cpp @@ -1,57 +1,68 @@ -#include -#include -#include -#include -#include +#include + using namespace std; -string Alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -map Map; +typedef long long big_int; -int calc(string S, int B) { - int mul = 1; - int val = 0; - for (int i = S.length() - 1; i >= 0; i--) { - val += (Map[S[i]] * mul); - mul *= B; - } - return val; +inline int CharToDigit(char c) +{ + if (isdigit(c)) + return c - '0'; + return c - 'A' + 10; } -int maxm(string S) { - int B = 0; - for (int i = 0; i < S.length(); i++) { - B = max(B, Map[S[i]]); - } - return max(2, B + 1); +inline char DigitToChar(int n) +{ + return "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n]; +} + +bool checkIllegal(int base1, string &in) +{ + for (int i = 0; i < in.length(); ++i) + if (CharToDigit(in[i]) >= base1) + return true; + return false; } -int main() { - for (int i = 0; i < Alpha.length(); i++) { - Map[(char)Alpha[i]] = i; - //cout << Alpha[i] << " " << Map[Alpha[i]] << endl; +// convert string 'in' to int 'value' +big_int parseValue(int base, string &in) +{ + if (checkIllegal(base, in)) + return -1; + big_int value = 0, curBase = 1; + for (int i = in.length() - 1; i >= 0; --i) + { + value += CharToDigit(in[i]) * curBase; + curBase *= base; } - - string A, B; - while (cin >> A >> B) { - int baseA, baseB; - int aSta = maxm(A), bSta = maxm(B); - //cout << aSta << " " << bSta << endl; - bool flag = false; - for (baseA = aSta; baseA <= 36; baseA++) { - int valA = calc(A, baseA); - for (baseB = bSta; baseB <= 36; baseB++) { - int valB = calc(B, baseB); - if (valA == valB) { - cout << A << " (base " << baseA << ") = " << B << " (base " << baseB << ")\n"; - flag = true; - break; - } + return value; +} + +void match(string &num1, string &num2) +{ + for (int i = 2; i <= 36; ++i) + { + for (int j = 2; j <= 36; ++j) + { + big_int value1 = parseValue(i, num1); + big_int value2 = parseValue(j, num2); + if (value1 < 0 || value2 < 0) + continue; + if (value1 == value2) + { + printf("%s (base %d) = %s (base %d)\n", num1.c_str(), i, num2.c_str(), j); + return; } - if (flag) break; } - if (!flag) - cout << A << " is not equal to " << B << " in any base 2..36\n"; } + // fail + printf("%s is not equal to %s in any base 2..36\n", num1.c_str(), num2.c_str()); +} + +int main() +{ + string num1, num2; + while (cin >> num1 >> num2) + match(num1, num2); return 0; } diff --git a/344.cpp b/344.cpp index fa2ebec..0547182 100644 --- a/344.cpp +++ b/344.cpp @@ -1,53 +1,108 @@ -#include -#include -using namespace std; +#include -string decimalToRoman(int value) { - string roman[9] = {"c", "xc", "l", "xl", "x", "ix", "v", "iv", "i"}; - int decimal[9] = {100, 90, 50, 40, 10, 9, 5, 4, 1}; - - string romanNumeral = ""; +using namespace std; - for (int i = 0; i < 9; i++) { - while (value >= decimal[i]) { - value -= decimal[i]; - romanNumeral += roman[i]; - } - } - return romanNumeral; +int i, v, x, l, c; +void single(int n) +{ + switch (n) + { + case 0: + break; + case 1: + i++; + break; + case 2: + i += 2; + break; + case 3: + i += 3; + break; + case 4: + i++; + v++; + break; + case 5: + v++; + break; + case 6: + v++; + i++; + break; + case 7: + i += 2; + v++; + break; + case 8: + i += 3; + v++; + break; + case 9: + i++; + x++; + break; + } } - -struct Alpha { - int ii, vv, xx, ll, cc; -}; - -int main() { - string S; - Alpha R[101]; - R[0].ii = R[0].vv = R[0].xx = R[0].ll = R[0].cc = 0; - for (int i = 1; i <= 100; i++) { - S = decimalToRoman(i); - R[i] = R[i - 1]; - for (int j = 0; j < S.length(); j++) { - switch (S[j]) { - case 'i': ++R[i].ii; - break; - case 'v': ++R[i].vv; - break; - case 'x': ++R[i].xx; - break; - case 'l': ++R[i].ll; - break; - case 'c': ++R[i].cc; - break; - } - } +void dou(int n) +{ + switch (n) + { + case 0: + break; + case 1: + x++; + break; + case 2: + x += 2; + break; + case 3: + x += 3; + break; + case 4: + x++; + l++; + break; + case 5: + l++; + break; + case 6: + x++; + l++; + break; + case 7: + x += 2; + l++; + break; + case 8: + x += 3; + l++; + break; + case 9: + x += 1; + c++; + break; + case 10: + c++; + break; } - - int N; - while (scanf("%d", &N)) { - if (!N) break; - printf("%d: %d i, %d v, %d x, %d l, %d c\n", N, R[N].ii, R[N].vv, R[N].xx, R[N].ll, R[N].cc); +} +main() +{ + int n, j; + while (1) + { + i = v = x = l = c = 0; + scanf("%d", &n); + if (!n) + { + break; + } + for (j = 1; j <= n; j++) + { + single(j % 10); + dou(j / 10); + } + printf("%d: %d i, %d v, %d x, %d l, %d c\n", n, i, v, x, l, c); } return 0; } diff --git a/345.cpp b/345.cpp new file mode 100644 index 0000000..6830c72 --- /dev/null +++ b/345.cpp @@ -0,0 +1,198 @@ +#include + +using namespace std; + +#define MAX_N 1000 + +int N, A, B, cnt[MAX_N], map_[MAX_N]; +double a[MAX_N][MAX_N]; + +void error() +{ + int um = 1, zero = 0; + um = um / zero; +} + +int search(int k) +{ + int i; + for (i = 0; i < N; i++) + if (map_[i] == k) + { + return i; + } + map_[N] = k; + N++; + if (N >= MAX_N) + { + error(); + } + return N - 1; +} + +void init() +{ + int i, j; + for (i = 0; i < MAX_N; i++) + { + cnt[i] = 0; + for (j = 0; j < MAX_N; j++) + { + a[i][j] = 0.0; + } + } +} + +void visit(int start, int deleted, int vis[]) +{ + int i, j, x, y; + int b[MAX_N][MAX_N]; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + b[i][j] = 0; + } + for (i = 0; i < N; i++) + if (i != deleted) + for (j = 0; j < N; j++) + if (j != deleted && a[i][j] > 0.0) + { + b[i][j] = 1; + } + for (y = 0; y < N; y++) + for (x = 0; x < N; x++) + if (b[x][y]) + for (j = 0; j < N; j++) + if (b[y][j]) + { + b[x][j] = 1; + } + for (i = 0; i < N; i++) + { + vis[i] = b[start][i]; + } + vis[start] = 1; + vis[deleted] = 1; +} + +int eliminate() +{ + int i, j, c = 0; + for (i = 0; i < N; i++) + if (i != A && i != B && cnt[i] == 1) + { + // eliminate this + for (j = 0; j < N; j++) + { + if (a[i][j] > 0.0) + { + cnt[i]--; + cnt[j]--; + a[i][j] = 0.0; + a[j][i] = 0.0; + c++; + if (cnt[i] == 0) + { + break; + } + } + } + } + return c; +} + +void putResistor(int x, int y, double r) +{ + if (r > 0.0) + { + if (a[x][y] > 0.0) + { + // parallel + //1/rt = 1/r1 + 1/r2 + a[x][y] = (a[x][y] * r) / (a[x][y] + r); + a[y][x] = a[x][y]; + } + else + { + cnt[x]++; + cnt[y]++; + a[x][y] = r; + a[y][x] = r; + } + } +} + +void findRes() +{ + int i, j, x, y; + int c = 1; + double r; + while (c > 0) + { + c = 0; + while (eliminate()) + ; + for (j = 0; j < N; j++) + if (j != A && j != B && cnt[j] == 2) + { + x = -1; + y = -1; + for (i = 0; i < N; i++) + if (i != j && a[i][j] > 0.0) + { + if (x < 0) + { + x = i; + } + else + { + y = i; + break; + } + } + if (y == -1 || x == -1) + { + error(); + } + cnt[x]--; + cnt[y]--; + cnt[j] -= 2; + r = a[x][j] + a[j][y]; + a[x][j] = 0.0; + a[j][x] = 0.0; + a[j][y] = 0.0; + a[y][j] = 0.0; + putResistor(x, y, r); + c++; + } + } +} + +int main() +{ + int i, x, y, k; + int pn = 0; + double r; + while (3 == scanf("%d %d %d", &k, &A, &B)) + { + N = 0; + if (k == 0 && A == 0 && B == 0) + { + break; + } + A = search(A); + B = search(B); + init(); + for (i = 0; i < k; i++) + { + scanf("%d %d %lf", &x, &y, &r); + x = search(x); + y = search(y); + putResistor(x, y, r); + } + findRes(); + printf("Case %d: %.2lf Ohms\n", ++pn, + ((int)(a[A][B] * 100 + 0.5)) / 100.0); + } + return 0; +} diff --git a/346.cpp b/346.cpp new file mode 100644 index 0000000..99fa069 --- /dev/null +++ b/346.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int str2int(const string &input) +{ + switch (tolower(input[0])) + { + case 'a': + if (input.size() == 1) + { + return 1; + } + return (input[1] == '#' ? 2 : 12); + case 'b': + return (input.size() == 1 ? 3 : 2); + case 'c': + return (input.size() == 2 ? 5 : 4); + case 'd': + if (input.size() == 1) + { + return 6; + } + return (input[1] == '#' ? 7 : 5); + case 'e': + return (input.size() == 1 ? 8 : 7); + case 'f': + return (input.size() == 2 ? 10 : 9); + case 'g': + if (input.size() == 1) + { + return 11; + } + return (input[1] == '#' ? 12 : 10); + } +} + +string int2chord(int c) +{ + string result[12] = {"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"}; + return result[c - 1]; +} + +int main() +{ + int chords[3]; + string input; + while (cin >> input) + { + cout << input; + chords[0] = str2int(input); + for (int i = 0; i < 2; i++) + { + cin >> input; + cout << " " << input; + chords[i + 1] = str2int(input); + } + sort(chords, chords + 3); + bool found = false; + do + { + int a = chords[1] - chords[0]; + int b = chords[2] - chords[1]; + a = (a < 0 ? a + 12 : a); + b = (b < 0 ? b + 12 : b); + if (a == 4 && b == 3) + { + found = true; + cout << " is a " << int2chord(chords[0]) << " Major chord." << endl; + break; + } + else if (a == 3 && b == 4) + { + found = true; + cout << " is a " << int2chord(chords[0]) << " Minor chord." << endl; + break; + } + } while (next_permutation(chords, chords + 3)); + if (!found) + { + cout << " is unrecognized." << endl; + } + } + return 0; +} diff --git a/347.cpp b/347.cpp new file mode 100644 index 0000000..8baa121 --- /dev/null +++ b/347.cpp @@ -0,0 +1,78 @@ +#include + +using namespace std; + +/** +347 +**/ +#define MAXN 10000000 +struct ss +{ + char p; +} V[2 * MAXN]; +int DigitsAreUnique(char *str) +{ + int used[10] = {0}, dig; + for (; *str; str++) + { + dig = *str - '0'; + if (used[dig]) + { + return 0; + } + used[dig] = 1; + } + return 1; +} +int main() +{ + char num[10]; + int len, i, k, pos, is_run, c = 1; + long x, r; + while (scanf("%ld", &x), x) + { + is_run = 0; + for (r = x; !is_run; r++) + { + if (V[r].p == 1) + { + continue; + } + if (V[r].p == 2) + { + r++; + break; + } + sprintf(num, "%ld", r); + if (!DigitsAreUnique(num)) + { + V[r].p = 1; + goto not_run; + } + len = strlen(num); + pos = 0; + k = num[0] - '0'; + num[pos] = 0; + for (i = 1; i < len; i++) + { + pos = (pos + k) % len; + if (!num[pos]) + { + V[r].p = 1; + goto not_run; + } + k = num[pos] - '0'; + num[pos] = 0; + } + pos = (pos + k) % len; + if (pos == 0) + { + is_run = 1; + } + not_run:; + } + V[r - 1].p = 2; + printf("Case %d: %ld\n", c++, --r); + } + return 0; +} diff --git a/348.cpp b/348.cpp new file mode 100644 index 0000000..4523e41 --- /dev/null +++ b/348.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +/** +348 +Optimal Array multiplication sequence +**/ +#define MAXN 15 +#define INF 2147483647 +typedef long long ss; +ss M[MAXN][MAXN]; +int P[MAXN], S[MAXN][MAXN]; +int N, K; +char St[30][3]; +void Matrix_Chain_Order() +{ + int i, j, k, l; + ss q; + for (i = 1; i <= N; i++) + { + M[i][i] = 0; + } + for (l = 2; l <= N; l++) + { + for (i = 1; i <= N - l + 1; i++) + { + j = i + l - 1; + M[i][j] = INF; + for (k = i; k <= j - 1; k++) + { + q = M[i][k] + M[k + 1][j] + P[i - 1] * P[j] * P[k]; + if (q < M[i][j]) + { + M[i][j] = q; + S[i][j] = k; + } + } + } + } +} +/* +void Print_Op_P(int i, int j) { + if(i == j){ + sprintf(St[K++],"%d",i); + } + else { + strcpy(St[K++],"(");//printf("("); + Print_Op_P(i,S[i][j]); + Print_Op_P(S[i][j]+1,j); + strcpy(St[K++],")");//printf(")"); + } +} +void Print() { + int i; + for(i = 0; i + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef struct _voter +{ + queue votes; + bool valid; +} voter; + +vector start_election(voter voters[100], int number_of_candidates, int number_of_voters, int bad_ballots) +{ + vector result; + bool eliminated[number_of_candidates + 1]; + rep(i, number_of_candidates + 1) + { + eliminated[i] = false; + } + int limit = (number_of_voters - bad_ballots) / 2 + 1; + while (true) + { + int votes[number_of_candidates + 1]; + rep(i, number_of_candidates + 1) + votes[i] = 0; + rep(i, number_of_voters) + { + if (!voters[i].valid || voters[i].votes.empty()) + { + continue; + } + votes[voters[i].votes.front()]++; + } + int highest = -1; + int lowest = 10000; + bool multiple_highest = false; + REP(i, 1, number_of_candidates + 1) + { + if (eliminated[i]) + { + continue; + } + if (votes[i] < lowest) + { + lowest = votes[i]; + } + if (votes[i] > highest) + { + highest = votes[i]; + multiple_highest = false; + } + else if (votes[i] == highest) + { + multiple_highest = true; + } + } + if (highest >= limit) + { + // find highest + REP(i, 1, number_of_candidates + 1) + { + if (votes[i] == highest) + { + highest = i; + break; + } + } + result.push_back(highest); + break; + } + else if (lowest < highest) + { + // find first lowest and eliminate + REP(i, 1, number_of_candidates + 1) + { + if (!eliminated[i] && votes[i] == lowest) + { + lowest = i; + break; + } + } + eliminated[lowest] = true; + rep(i, number_of_voters) + { + while (true) + { + bool e = false; + if (!voters[i].votes.empty() && eliminated[voters[i].votes.front()]) + { + voters[i].votes.pop(); + e = true; + } + if (!e) + { + break; + } + } + } + } + else// lowest == highest + { + REP(i, 1, number_of_candidates + 1) + { + if (votes[i] == highest) + { + result.push_back(i); + } + } + break; + } + } + return result; +} + +int main() +{ + int c, n, vote, election_counter = 0; + while (cin >> c >> n) + { + if (c == 0 && n == 0) + { + break; + } + election_counter++; + voter voters[100]; + int bad_ballots = 0; + rep(i, n) + { + set votes; + rep(j, c) + { + cin >> vote; + votes.insert(vote); + voters[i].votes.push(vote); + } + voters[i].valid = (votes.size() == c && *(votes.begin()) == 1 && *(votes.rbegin()) == c ? true : false); + if (!voters[i].valid) + { + bad_ballots++; + } + } + cout << "Election #" << election_counter << endl; + if (bad_ballots > 0) + { + cout << " " << bad_ballots << " bad ballot(s)" << endl; + } + vector result = start_election(voters, c, n, bad_ballots); + if (result.size() == 1) + { + cout << " Candidate " << result[0] << " is elected." << endl; + } + else + { + cout << " The following candidates are tied:"; + rep(i, result.size()) + { + cout << " " << result[i]; + } + cout << endl; + } + } + return 0; +} diff --git a/350.cpp b/350.cpp new file mode 100644 index 0000000..37b8bad --- /dev/null +++ b/350.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int solve(int Z, int I, int M, int L) +{ + int i, cnt = 1, seed; + bool flag[10000]; + for (i = 0; i < 10000; i++) + { + flag[i] = false; + } + seed = L; + flag[L] = true; + while (flag[(Z * L + I) % M] == false) + { + flag[(Z * L + I) % M] = true; + L = (Z * L + I) % M; + cnt++; + } + if (seed != (Z * L + I) % M) + { + cnt--; + } + return cnt; +} + +int main() +{ + int Z, I, M, L, case_num = 1; + while (cin >> Z >> I >> M >> L) + { + if (Z == 0 && I == 0 && M == 0 && L == 0) + { + break; + } + cout << "Case " << case_num++ << ": " << solve(Z, I, M, L) << endl; + } + return 0; +} diff --git a/352.cpp b/352.cpp index 4380b6f..5a46eb4 100644 --- a/352.cpp +++ b/352.cpp @@ -1,76 +1,66 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +enum +{ + MAX = 25 + 2 +}; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +int img_size; +char img[MAX][MAX]; -int N; -char adjMat[30][30]; -bool visit[30][30]; +bool is_valid(int x, int y) +{ + return (x < img_size && x >= 0 && y < img_size && y >= 0); +} -void floodFill(int X, int Y){ - if ( X < 0 || Y < 0 || X >= N || Y >= N ) - return; - if ( adjMat[X][Y] == '0' ) - return; - - adjMat[X][Y] = '0'; - - int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; - int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; - int j; - FOI(j, 0, 7) - floodFill(X + dx[j], Y + dy[j]); +void dfs(int x, int y) +{ + img[x][y] = '0'; + for (int dx = -1; dx <= 1; ++dx) + { + for (int dy = -1; dy <= 1; ++dy) + { + int next_x = x + dx; + int next_y = y + dy; + if (is_valid(next_x, next_y) && img[next_x][next_y] == '1') + { + dfs(next_x, next_y); + } + } + } } -int main(int argc, char **argv){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T = 1; - while( scanf("%d", &N) != EOF ){ - if( N == 0 ) - break; - int i, j; - int comp = 0; - memset(adjMat, '0', sizeof (adjMat) ); - FOI(i, 0, N-1) - scanf("%s", adjMat[i]); - FOI(i, 0, N-1) - FOI(j, 0, N-1) - if ( adjMat[i][j] == '1' ){ - comp++; - floodFill(i, j); +int solve(char img[MAX][MAX], int img_size) +{ + int war_eagles_counter = 0; + for (int i = 0; i < img_size; ++i) + { + for (int j = 0; j < img_size; ++j) + { + if (img[i][j] == '1') + { + dfs(i, j); + war_eagles_counter++; } - - printf("Image number %d contains %d war eagles.\n", T++, comp); + } + } + return war_eagles_counter; +} + +int main() +{ + int caseNo = 1; + while (scanf("%d ", &img_size) == 1) + { + for (int i = 0; i < img_size; ++i) + { + scanf("%s", img[i]); + } + int war_eagles = solve(img, img_size); + printf("Image number %d ", caseNo++); + printf("contains %d war eagles.\n", war_eagles); } return 0; } diff --git a/353.cpp b/353.cpp index 37a1b25..9c64a85 100644 --- a/353.cpp +++ b/353.cpp @@ -1,62 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -bool isPalin(string str){ - int len = str.length(); - for(int k=0; k>str){ - int len = str.length(), i, j, tot=0, K=0; - string palin[10000]; - for(i=0; i + +using namespace std; + +set palindromes; + +bool is_palindrome(const string &input, int start, int end) +{ + string a(input.begin() + start, input.begin() + end); + string b(a.rbegin(), a.rend()); + return a == b; +} + +int count_palindromes(const string &input) +{ + int counter = 0; + for (int i = 0, sz = input.size(); i < sz; i++) + { + for (int j = i + 1; j <= sz; j++) + { + if (is_palindrome(input, i, j)) + { + palindromes.insert(string(input.begin() + i, input.begin() + j)); + } + } + } + return palindromes.size(); +} + +int main() +{ + string input; + while (cin >> input) + { + palindromes.clear(); + cout << "The string '" << input << "' contains " << count_palindromes(input) << " palindromes." << endl; + } + return 0; +} diff --git a/354.cpp b/354.cpp new file mode 100644 index 0000000..064e956 --- /dev/null +++ b/354.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +typedef long long i64; + +void assertD(int x) +{ + if (!x) + { + for (;;) + { + malloc(1000000); + } + } +} +void assertS(int x) +{ + if (!x) + { + for (int x = 42;;) + { + x %= x; + } + } +} + +struct Operator +{ + int real, local, prec, assoc; + i64 apply(i64 x, i64 y) + { + switch (real) + { + case '+': + return x + y; + case '-': + return x - y; + case '*': + return x * y; + case '/': + assertD(y != 0); + return x / y; + default: + assert(0 == 1); + return 42; + } + } +} oper[4]; + +int tok; +i64 tokn; +char *tokp; + +int next() +{ + while (*tokp && isspace(*tokp)) + { + tokp++; + } + if (*tokp == 0) + { + return tok = 0; + } + else if (isdigit(*tokp)) + { + for (tokn = 0; *tokp && isdigit(*tokp); tokp++) + { + tokn = tokn * 10LL + (tokp[0] - '0'); + } + return tok = 'n'; + } + else + { + return tok = *tokp++; + } +} + +i64 eval(int prec) +{ + if (prec > '9') + { + assertS(tok == 'n'); + i64 r = tokn; + next(); + return r; + } + vector z; + z.push_back(eval(prec + 1)); + for (int zo = -1;;) + { + int o; + for (o = 0; o < 4; o++) + if (tok == oper[o].local && oper[o].prec == prec) + { + break; + } + if (o >= 4 || oper[o].assoc == 'L' || o != zo) + { + while (z.size() > 1) + { + i64 y = z.back(); + z.pop_back(); + i64 x = z.back(); + z.pop_back(); + z.push_back(oper[zo].apply(x, y)); + } + } + if (o >= 4) + { + return z[0]; + } + next(); + i64 y = eval(prec + 1); + if (oper[o].assoc == 'L') + { + z[0] = oper[o].apply(z[0], y); + } + else + { + zo = o; + z.push_back(y); + } + } +} + +int main() +{ + static char s[4096]; + int T; + for (scanf("%d", &T); T-- > 0;) + { + scanf(" "); + for (int i = 0; i < 4; i++) + { + gets(s); + oper[i].real = s[0]; + oper[i].local = s[1]; + oper[i].prec = s[2]; + oper[i].assoc = toupper(s[3]); + } + while (gets(s)) + { + int k = strlen(s); + while (k > 0 & s[k - 1] == '\r') + { + s[--k] = 0; + } + if (k <= 0) + { + break; + } + tokp = s; + next(); + for (int i = 0; s[i]; i++) + { + int j; + for (j = 0; j < 4; j++) + { + if (oper[j].local == s[i]) + { + putchar(oper[j].real); + break; + } + } + if (j == 4) + { + putchar(s[i]); + } + } + printf(" = %lld\n", eval('0')); + } + if (T) + { + printf("\n"); + } + } +} diff --git a/355.cpp b/355.cpp new file mode 100644 index 0000000..7d34c6a --- /dev/null +++ b/355.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +string convert(long long n, int base) +{ + if (n == 0) + { + return "0"; + } + char NUMS[] = "0123456789ABCDEF"; + string result = ""; + do + { + result.push_back(NUMS[n % base]); + n /= base; + } while (n); + return string(result.rbegin(), result.rend()); +} + +long long any2dec(const string &s, int base) +{ + char c; + size_t found; + unsigned long long result = 0; + string NUMS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + for (int i = 0, sz = s.size(); i < sz; i++) + { + found = NUMS.find(s[i]); + if (found == string::npos || int(found) >= base) + { + return -1; + } + result = result * base + int(found); + } + return result; +} + +int main() +{ + int a, b; + string num; + while (cin >> a >> b >> num) + { + long long tmp = any2dec(num, a); + if (tmp == -1) + { + cout << num << " is an illegal base " << a << " number" << endl; + continue; + } + cout << num << " base " << a << " = " << convert(tmp, b) << " base " << b << endl; + } + return 0; +} diff --git a/356.cpp b/356.cpp new file mode 100644 index 0000000..b8b9e2f --- /dev/null +++ b/356.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int n; + int f = 0; + while (cin >> n) + { + if (f) + { + cout << endl; + } + f = 1; + int cmp = 0, k; + double r = (2 * (double)n - 1) / 2; + for (k = 1; k < n; k++) + { + cmp += (int)sqrt(r * r - (double)(k * k)); + } + printf("In the case n = %d, %d cells contain segments of the circle.\n", n, 8 * n - 4); + printf("There are %d cells completely contained in the circle.\n", 4 * cmp); + } + return 0; +} + +//Reference to Concrete Mathematics [Knuth] Exercise 3.33 diff --git a/357.cpp b/357.cpp new file mode 100644 index 0000000..f532786 --- /dev/null +++ b/357.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +/*************************** +TITLE : LET ME COUNT THE WAY + NO : 357 +****************************/ +#define MAXTOTAL 30002 +long long nway[MAXTOTAL]; +int coin[5] = {50, 25, 10, 5, 1}; +void Cal() +{ + int i, j, c, v; + v = 5; + nway[0] = 1; + for (i = 0; i < v; i++) + { + c = coin[i]; + for (j = c; j <= 30000; j++) + { + nway[j] += nway[j - c]; + } + } +} +int main() +{ + int n; + Cal(); + while (scanf("%d", &n) == 1) + { + if (nway[n] > 1) + { + printf("There are %lld ways to produce %d cents change.\n", nway[n], n); + } + else + { + printf("There is only 1 way to produce %d cents change.\n", n); + } + } + return 0; +} diff --git a/358.cpp b/358.cpp new file mode 100644 index 0000000..9465722 --- /dev/null +++ b/358.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +double r1, P, r2, area, pi = 2 * acos(0); +double f(double r2) +{ + double theta1 = acos((r2 * r2) / (2 * r1 * r2)), + theta2 = acos(((2 * r1 * r1) - (r2 * r2)) / (2 * r1 * r1)); + double t = r2 * r2 * theta1; + t += r1 * r1 * theta2; + t -= r1 * r1 * sin(theta2); + return t - area; +} +int main() +{ + int t; + scanf("%d", &t); + for (int i = 0; i < t; i++) + { + if (i) + { + puts(""); + } + scanf("%lf %lf", &r1, &P); + double left = 0, right = 2 * r1, mid = (left + right) / 2; + area = pi * r1 * r1 * P; + while (true) + { + double t = f(mid); + if (fabs(t) < 1e-6) + { + r2 = mid; + break; + } + else if (t > 0) + { + right = mid; + } + else + { + left = mid; + } + mid = (left + right) / 2; + } + printf("R = %.lf, P = %.2lf, Rope = %.2lf\n", r1, P, r2); + } + return 0; +} diff --git a/359.cpp b/359.cpp new file mode 100644 index 0000000..8e57c71 --- /dev/null +++ b/359.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +typedef vector VI; +typedef vector VVI; + +#define MAX 65536 + +int p[MAX][2], s[MAX], n, ok; +VVI H; + +void col(int x) +{ + if (s[x] == 0) + { + s[x] = 1; + } + for (int i = 0; ok && i < (int)H[x].size(); i++) + { + int y = H[x][i]; + if (s[y] == 0) + { + s[y] = 3 - s[x]; + col(y); + } + else if (s[y] != 3 - s[x]) + { + ok = 0; + } + } +} + +void acyc(int x) +{ + if (s[x]) + { + ok &= (s[x] == 1); + } + else if (ok) + { + s[x] = 2; + for (int i = 0; i < 2 && p[x][i] > 0; i++) + { + acyc(p[x][i]); + } + s[x] = 1; + } +} + +int main() +{ + for (int cs = 1, m, x, y; scanf("%d %d", &n, &m) == 2; cs++) + { + assert(0 <= n && n <= MAX - 10); + ok = 1; + for (int i = 1; i <= n; i++) + { + p[i][0] = p[i][1] = 0; + } + while (m-- > 0 && scanf("%d %d", &x, &y) == 2) + { + assert(1 <= x && x <= n && 1 <= y && y <= n); + if (x == y) + { + ok = 0; + continue; + } + if (p[y][0] == x || p[y][1] == x) + { + ok = 0; /*????*/ + continue; + } + if (p[y][0] == 0) + { + p[y][0] = x; + } + else if (p[y][1] == 0) + { + p[y][1] = x; + } + else + { + ok = 0; + } + } + memset(s, 0, sizeof(int) * (n + 1)); + for (int i = 1; ok && i <= n; i++) + { + acyc(i); + } + if (ok) + { + H = VVI(n + 1, VI()); + for (int i = 1; i <= n; i++) + if (p[i][0] > 0 && p[i][1] > 0) + { + H[p[i][0]].push_back(p[i][1]); + H[p[i][1]].push_back(p[i][0]); + } + memset(s, 0, sizeof(int) * (n + 1)); + for (int i = 1; ok && i <= n; i++) + { + col(i); + } + } + printf("Graph number %d is %ssexy\n", cs, ok ? "" : "not "); + } +} diff --git a/361.cpp b/361.cpp new file mode 100644 index 0000000..9ee099b --- /dev/null +++ b/361.cpp @@ -0,0 +1,215 @@ +#include + +using namespace std; + +const int MAX = 220; +struct point +{ + double x, y; +}; +struct segment +{ + point a, b; +}; +point c[MAX], r[MAX]; +point cc[MAX], rr[MAX]; +int t1, t2; +const double eps = 1e-6; +bool dy(double x, double y) +{ + return x > y + eps;// x > y +} +bool xy(double x, double y) +{ + return x < y - eps;// x < y +} +bool dyd(double x, double y) +{ + return x > y - eps;// x >= y +} +bool xyd(double x, double y) +{ + return x < y + eps;// x <= y +} +bool dd(double x, double y) +{ + return fabs(x - y) < eps;// x == y +} +double disp2p(point a, point b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} +double crossProduct(point a, point b, point c)//å‘é‡ ac 在 ab çš„æ–¹å‘ +{ + return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y); +} +bool cmp(point a, point b)// æŽ’åº +{ + double len = crossProduct(c[0], a, b); + if (dd(len, 0.0)) + { + return xy(disp2p(c[0], a), disp2p(c[0], b)); + } + return xy(len, 0.0); +} +void Graham(point c[], int n, point stk[], int &top) +{ + int tmp = 0; + for (int i = 1; i < n; i++) + if (xy(c[i].x, c[tmp].x) || dd(c[i].x, c[tmp].x) && xy(c[i].y, c[tmp].y)) + { + tmp = i; + } + swap(c[0], c[tmp]); + sort(c + 1, c + n, cmp); + stk[0] = c[0]; + stk[1] = c[1]; + top = 1; + for (int i = 2; i < n; i++) + { + while (xyd(crossProduct(stk[top], stk[top - 1], c[i]), 0.0) && top >= 1) + { + top--; + } + stk[++top] = c[i]; + } +} +bool onSegment(point a, point b, point c) +{ + double maxx = max(a.x, b.x); + double maxy = max(a.y, b.y); + double minx = min(a.x, b.x); + double miny = min(a.y, b.y); + if (dd(crossProduct(a, b, c), 0.0) && dyd(c.x, minx) && xyd(c.x, maxx) && dyd(c.y, miny) && xyd(c.y, maxy)) + { + return true; + } + return false; +} +bool segIntersect(point p1, point p2, point p3, point p4) +{ + double d1 = crossProduct(p3, p4, p1); + double d2 = crossProduct(p3, p4, p2); + double d3 = crossProduct(p1, p2, p3); + double d4 = crossProduct(p1, p2, p4); + if (xy(d1 * d2, 0.0) && xy(d3 * d4, 0.0)) + { + return true; + } + if (dd(d1, 0.0) && onSegment(p3, p4, p1)) + { + return true;//如果ä¸åˆ¤ç«¯ç‚¹ç›¸äº¤ï¼Œåˆ™ä¸‹é¢è¿™å››å¥è¯ä¸éœ€è¦ + } + if (dd(d2, 0.0) && onSegment(p3, p4, p2)) + { + return true; + } + if (dd(d3, 0.0) && onSegment(p1, p2, p3)) + { + return true; + } + if (dd(d4, 0.0) && onSegment(p1, p2, p4)) + { + return true; + } + return false; +} +bool point_inPolygon(point pot, point p[], int n) +{ + int count = 0; + segment l; + l.a = pot; + l.b.x = 1e10; + l.b.y = pot.y; + p[n] = p[0]; + for (int i = 0; i < n; i++) + { + if (onSegment(p[i], p[i + 1], pot)) + { + return true; + } + if (!dd(p[i].y, p[i + 1].y)) + { + int tmp = -1; + if (onSegment(l.a, l.b, p[i])) + { + tmp = i; + } + else if (onSegment(l.a, l.b, p[i + 1])) + { + tmp = i + 1; + } + if (tmp != -1 && dd(p[tmp].y, max(p[i].y, p[i + 1].y))) + { + count++; + } + else if (tmp == -1 && segIntersect(p[i], p[i + 1], l.a, l.b)) + { + count++; + } + } + } + if (count % 2 == 1) + { + return true; + } + return false; +} +int main() +{ + int n, m, o; + int ind = 1; + point pot; + while (~scanf("%d%d%d", &n, &m, &o)) + { + t1 = t2 = 0; + if (m == n && m == o && o == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &c[i].x, &c[i].y); + } + for (int i = 0; i < m; i++) + { + scanf("%lf%lf", &r[i].x, &r[i].y); + } + if (n) + { + Graham(c, n, cc, t1); + } + memcpy(c, r, sizeof(r)); + if (m) + { + Graham(c, m, rr, t2); + } + printf("Data set %d:\n", ind++); + bool flag1 = false, flag2 = false; + if (t1 + 1 <= 2 && n <= 2) + { + flag1 = true; + } + if (t2 + 1 <= 2 && m <= 2) + { + flag2 = true; + } + while (o--) + { + scanf("%lf%lf", &pot.x, &pot.y); + if (!flag1 && point_inPolygon(pot, cc, t1 + 1)) + { + printf(" Citizen at (%.0lf,%.0lf) is safe.\n", pot.x, pot.y); + continue; + } + if (!flag2 && point_inPolygon(pot, rr, t2 + 1)) + { + printf(" Citizen at (%.0lf,%.0lf) is robbed.\n", pot.x, pot.y); + continue; + } + printf(" Citizen at (%.0lf,%.0lf) is neither.\n", pot.x, pot.y); + } + printf("\n"); + } + return 0; +} diff --git a/362.cpp b/362.cpp new file mode 100644 index 0000000..53b72ca --- /dev/null +++ b/362.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +int main() +{ + int file_size, transfer_size; + int case_num = 0; + vector file_transfer; + while (cin >> file_size) + { + if (file_size == 0) + { + break; + } + case_num++; + file_transfer.clear(); + cout << "Output for data set " << case_num << ", " << file_size << " bytes:" << endl; + while (file_size > 0) + { + cin >> transfer_size; + file_transfer.push_back(transfer_size); + file_size -= transfer_size; + if (file_transfer.size() % 5 == 0) + { + int bytes_transferred = 0; + for (int i = 0; i < 5; i++) + { + bytes_transferred += file_transfer[file_transfer.size() - 1 - i]; + } + if (bytes_transferred == 0) + { + cout << " Time remaining: stalled" << endl; + } + else + { + double tmp = file_size * 5.0 / bytes_transferred; + int time_remaining = (tmp > int(tmp) ? int(tmp) + 1 : int(tmp)); + cout << " Time remaining: " << time_remaining << " seconds" << endl; + } + } + } + cout << "Total time: " << file_transfer.size() << " seconds" << endl; + cout << endl; + } + return 0; +} diff --git a/366.cpp b/366.cpp new file mode 100644 index 0000000..041deaf --- /dev/null +++ b/366.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +int pow2(int y) +{ + int res = 1; + for (int i = 0; i < y; i++) + { + res *= 2; + } + return res; +} + +int main() +{ + int m, xin, yin, x, y; + while (scanf("%d%d%d", &m,&xin,&yin) != EOF) + { + x = xin; + y = yin; + if (m == 0 && x == 0 && y == 0) + { + break; + } + if (y > x) + { + swap(x,y); + } + int yd = y - 1; + int i = 0, sumy = 0, sumx = 0; + int cut; + while ((cut = pow2(i)) <= yd && yd >= cut) + { + if (cut > m) + { + break; + } + i++; + sumy++; + yd -= cut; + } + int ky; + if (yd > 0) + { + ky = (yd / m); + if ((ky * m) != yd) + { + ky++; + } + } + else + { + ky = 0; + } + sumy += ky; + int xd = y * (x - 1); + int kx; + if (x > 1) + { + i = 0; + if (m <= y * 1) + { + kx = xd / m; + if ((kx * m) != xd) + { + kx++; + } + sumx += kx; + } + else + { + while ((cut = y * pow2(i)) < m && xd >= cut) + { + i++; + sumx++; + xd -= cut; + } + if (xd > 0) + { + kx = (xd / m); + if ((kx * m) != xd) + { + kx++; + } + } + else + { + kx = 0; + } + sumx += kx; + } + int sum = sumx + sumy; + printf("%d by %d takes %d cuts\n\n", xin,yin,sum); + } + else + { + int sum = 0; + printf("%d by %d takes %d cuts\n\n", xin,yin,sum); + } + } + return 0; +} diff --git a/369.cpp b/369.cpp index 02be262..1acfccb 100644 --- a/369.cpp +++ b/369.cpp @@ -1,40 +1,33 @@ -#include -using namespace std; - -long gcd(long A, long B) { - if (A % B == 0) return B; - return gcd(B, A % B); -} +#include -void Divbygcd(long& A, long& B) { - long g = gcd(A, B); - A /= g; - B /= g; -} - -long C(long N, long K) { - long num = 1, den = 1, toMul, toDiv; - if (K > N/2) K = N - K; +using namespace std; - for (int i = K; i; i--) { - toMul = N - K + i; - toDiv = i; - Divbygcd(toMul, toDiv); - Divbygcd(toMul, den); - Divbygcd(toDiv, num); +//C(n,r)= n! / (n-r)!r! - num *= toMul; - den *= toDiv; - } - return num / den; +long double C(int n, int r) +{ + if (n - r < r) + { + r = n - r;// C(5,3)==C(5,2) + } + long double product = 1; + for (int i = 1; i <= r; i++) + { + product = product * (n - r + i) / i; + } + return product; } -int main() { - while (true) { - long N, M; - scanf("%ld%ld", &N, &M); - if (!N && !M) break; - - printf("%ld things taken %ld at a time is %ld exactly.\n", N, M, C(N, M)); - } +int main() +{ + int n, r; + while (scanf("%d %d", &n, &r) == 2) + { + if (n == 0 && r == 0) + { + break; + } + printf("%d things taken %d at a time is %.0Lf exactly.\n", n, r, C(n, r)); + } + return 0; } diff --git a/370.cpp b/370.cpp new file mode 100644 index 0000000..05f35fb --- /dev/null +++ b/370.cpp @@ -0,0 +1,258 @@ +#include + +using namespace std; + +bool match[5][5]; +int map_[5][5]; + +bool chk_1() +{ + bool temp = false; + for (int i = 0; i < 5; i++) + { + bool chk = true; + for (int j = 0; j < 5; j++) + { + if (!match[i][j]) + { + chk = false; + } + } + if (chk) + { + temp = true; + printf("BINGO #1\n"); + for (int j = 0; j < 5; j++) + { + printf("%d,%d,", i + 1, j + 1); + if (map_[i][j] == 0) + { + printf("FREE\n"); + } + else + { + printf("%d\n", map_[i][j]); + } + } + } + } + return temp; +} + +bool chk_2() +{ + bool temp = false; + for (int i = 0; i < 5; i++) + { + bool chk = true; + for (int j = 0; j < 5; j++) + { + if (!match[j][i]) + { + chk = false; + } + } + if (chk) + { + temp = true; + printf("BINGO #2\n"); + for (int j = 0; j < 5; j++) + { + printf("%d,%d,", j + 1, i + 1); + if (map_[j][i] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[j][i]); + } + } + } + } + return temp; +} + +bool chk_3() +{ + if (!match[0][0] || !match[0][4] || !match[4][0] || !match[4][4]) + { + return false; + } + printf("BINGO #3\n"); + printf("1,1,"); + if (map_[0][0] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[0][0]); + } + printf("1,5,"); + if (map_[0][4] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[0][4]); + } + printf("5,1,"); + if (map_[4][0] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[4][0]); + } + printf("5,5,"); + if (map_[4][4] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[4][4]); + } + return true; +} + +bool chk_4() +{ + int cnt = 0; + bool chk = true; + for (int i = 0; i < 5; i++) + { + if (!match[i][i]) + { + chk = false; + } + } + if (chk) + { + printf("BINGO #4\n"); + for (int i = 0; i < 5; i++) + { + printf("%d,%d,", i + 1, i + 1); + if (map_[i][i] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[i][i]); + } + } + cnt++; + } + chk = true; + for (int i = 0; i < 5; i++) + { + if (!match[i][4 - i]) + { + chk = false; + } + } + if (chk) + { + printf("BINGO #4\n"); + for (int i = 0; i < 5; i++) + { + printf("%d,%d,", i + 1, 5 - i); + if (map_[i][4 - i] == 0) + { + puts("FREE"); + } + else + { + printf("%d\n", map_[i][4 - i]); + } + } + cnt++; + } + return cnt != 0; +} + +int main() +{ + int t; + while (scanf("%d", &t) == 1) + { + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + if (i == 0 && j == 0) + { + map_[i][j] = t; + } + else + { + scanf("%d", &map_[i][j]); + } + match[i][j] = map_[i][j] == 0; + } + } + + bool flag = false; + if (chk_1()) + { + flag = true; + } + if (chk_2()) + { + flag = true; + } + if (chk_3()) + { + flag = true; + } + if (chk_4()) + { + flag = true; + } + + while (scanf("%d", &t), t) + { + if (flag) + { + continue; + } + for (int i = 0; i < 5; i++) + { + for (int j = 0; j < 5; j++) + { + if (map_[i][j] == t) + { + match[i][j] = true; + } + } + } + if (chk_1()) + { + flag = true; + } + if (chk_2()) + { + flag = true; + } + if (chk_3()) + { + flag = true; + } + if (chk_4()) + { + flag = true; + } + } + + if (!flag) + { + printf("No BINGO on this card.\n"); + } + printf("\n"); + } + return 0; +} diff --git a/371.cpp b/371.cpp index d1b06fa..d270e16 100644 --- a/371.cpp +++ b/371.cpp @@ -1,33 +1,55 @@ -#include -using namespace std; - -int main(){ - long long i,j; - for(;;){ - cin>>i>>j; - if(i==0 && j==0) - break; - long long x=0,y=0,k=0,max=0,maxn=0; - x=(i>j)?i:j; - y=(imax){ - maxn=k; - max=nc; - } - } - cout<<"Between "< + +using namespace std; + +/** + * 371 Ackermann function (AC) + * Author: chchwy + * Last Modified: 2010.03.18 + */ + +int getLength(long long n) +{ + int length = 0; + do + { + length++; + if (n % 2 == 0) + { + n = n >> 1;// div 2 + } + else + { + n = n * 3 + 1; + } + } while (n != 1); + return length; +} + +int main() +{ + int L, H; + while (scanf("%d %d", &L, &H)) + { + if (L == 0 && H == 0) + { + break; + } + if (L > H) + { + std::swap(L, H);//L may be greater than H + } + int maxLen = 1, max = L; + for (int i = L; i <= H; ++i) + { + int curLen = getLength(i); + if (curLen > maxLen) + { + max = i; + maxLen = curLen; + } + } + printf("Between %d and %d, %d generates the longest sequence of %d values.\n", L, H, max, maxLen); + } + return 0; +} diff --git a/372.cpp b/372.cpp new file mode 100644 index 0000000..36c2ec6 --- /dev/null +++ b/372.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +char infix[105], prefix[105]; +int idx; +void dfs(int l, int r) +{ + if (l > r) + return; + char p = prefix[idx], i; + idx++; + for (i = l; i <= r; i++) + if (infix[i] == p) + break; + dfs(l, i - 1); + dfs(i + 1, r); + printf(" %c", p); +} +int main() +{ + while (gets(infix)) + { + gets(prefix); + int i, j; + for (i = 0, j = 0; infix[i]; i++) + { + if (infix[i] != ' ') + infix[j++] = infix[i]; + } + infix[j] = '\0'; + for (i = 0, j = 0; prefix[i]; i++) + { + if (prefix[i] != ' ') + prefix[j++] = prefix[i]; + } + prefix[j] = '\0'; + printf("INFIX =>"); + for (i = 0; infix[i]; i++) + printf(" %c", infix[i]); + puts(""); + printf("PREFIX =>"); + for (i = 0; prefix[i]; i++) + printf(" %c", prefix[i]); + puts(""); + printf("POSTFIX =>"); + idx = 0; + dfs(0, j - 1); + puts(""); + } + return 0; +} diff --git a/373.cpp b/373.cpp new file mode 100644 index 0000000..deeefeb --- /dev/null +++ b/373.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int temp; +char buf[2008]; + +int equal(char now, char tar) +{ + return now == tar || now + 32 == tar; +} + +void adjust(int loc) +{ + if (!loc) + { + return; + } + /*printf("%d\n", loc);*/ + if (equal(buf[loc - 1], 'p')) + { + if ((loc == 1 || !equal(buf[loc - 2], 'e')) && equal(buf[loc - 1], 'p')) + { + if (!equal(buf[loc + 1], 'u') || !equal(buf[loc + 2], 'k')) + { + temp = buf[loc]; + buf[loc] = buf[loc - 1]; + buf[loc - 1] = temp; + adjust(loc - 1); + return; + } + } + } + if (equal(buf[loc + 1], 'p')) + { + if (equal(buf[loc - 1], 'e') || (equal(buf[loc + 2], 'u') && equal(buf[loc + 3], 'k'))) + { + temp = buf[loc]; + buf[loc] = buf[loc + 1]; + buf[loc + 1] = temp; + adjust(loc + 1); + if (equal(buf[loc - 1], 'g')) + { + adjust(loc - 1); + } + } + } +} + +int main() +{ + int i; + while (gets(buf)) + { + for (i = 0; buf[i]; i++) + { + if (equal(buf[i], 'g')) + { + adjust(i); + } + } + puts(buf); + } + return 0; +} diff --git a/374.cpp b/374.cpp index 42fb607..f1dce84 100644 --- a/374.cpp +++ b/374.cpp @@ -1,20 +1,31 @@ -#include - -long long BigMod(long long a, long long b, long long c) { - long long x = 1, y = a; - while (b > 0) { - if (b % 2 == 1) - x = (x * y) % c; - y = (y * y) % c; - b = b / 2; - } - return x % c; -} - -int main(){ - long long B, E, N; - while (scanf("%lld%lld%lld", &B, &E, &N) != EOF) - printf("%lld\n", BigMod(B, E, N)); - - return 0; -} +#include + +using namespace std; + +long long modulo(long long b, long long p, long long m) +{ + if (p == 0) + { + return 1 % m; + } + long long tmp = modulo(b, p / 2, m); + if (p % 2 == 0) + { + return (tmp * tmp) % m; + } + if (p % 2 == 1) + { + return (tmp * tmp * (b % m)) % m; + } +} + +int main() +{ + long long b, p, m; + while (cin >> b >> p >> m) + { + b %= m; + cout << modulo(b, p, m) << endl; + } + return 0; +} diff --git a/375.cpp b/375.cpp new file mode 100644 index 0000000..d10c8e2 --- /dev/null +++ b/375.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define EPS 1.0e-10 +#define PI (acos(-1.0)) +#define ALPHA 0.000001 + +typedef long double elem; +typedef complex point, vec; +typedef pair line; + +bool eq(elem a, elem b) +{ + return abs(a - b) < EPS; +} + +bool lessthan(elem a, elem b) +{ + return !eq(a, b) && a < b; +} + +elem cross(point a, point b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +elem len(vec a) +{ + return abs(a); +} + +vec uvec(vec x) +{ + return x / abs(x); +} + +vec norml(vec x) +{ + return x * vec(0, 1); +} + +vec unorml(vec x) +{ + return norml(x) / len(x); +} + +bool prll(point a1, point a2, point b1, point b2) +{ + return eq(cross(a2 - a1, b2 - b1), 0); +} + +bool intersected(line a, line b) +{ + return !prll(a.first, a.second, b.first, b.second); +} + +point intersection(line a, line b) +{ + vec va = a.second - a.first; + vec vb = b.second - b.first; + return a.first + + (cross(vb, b.first - a.first) / cross(vb, va)) * + (a.second - a.first); +} + +bool LineCrossPoint(line a, line b, point &ret) +{ + if (intersected(a, b)) + { + ret = intersection(a, b); + } + else + { + return false; + } + return true; +} + +elem Circumference(elem r) +{ + return 2.0 * r * PI; +} + +point InscribedCircleCenter(point a, point b, point c) +{ + elem A = len(vec(c - b)); + elem B = len(vec(c - a)); + elem C = len(vec(b - a)); + elem S = A + B + C; + return (A / S) * a + (B / S) * b + (C / S) * c; +} + +elem AreaOfTriangle(elem a, elem b, elem c) +{ + elem s = (a + b + c) / 2.0; + return sqrt(s * (s - a) * (s - b) * (s - c)); +} + +elem InradiusOfTriangle(point a, point b, point c) +{ + elem A = len(vec(c - b)); + elem B = len(vec(c - a)); + elem C = len(vec(b - a)); + return 2 * AreaOfTriangle(A, B, C) / (A + B + C); +} + +elem Solve(elem b, elem h) +{ + elem ret = 0; + point center = InscribedCircleCenter(point(0, 0), + point(-b / 2, -h), + point(b / 2, -h)); + line upper_edge = line(point(0, 0), point(-b / 2, -h)); + line downer_edge = line(point(0, 0), point(b / 2, -h)); + vector rs; + rs.push_back(InradiusOfTriangle(point(0, 0), + point(-b / 2, -h), + point(b / 2, -h))); + while (lessthan(ALPHA, rs.back())) + { + elem next = len(center) - rs.back(); + point p = next * uvec(center); + point upper_cross, downer_cross; + LineCrossPoint(line(p, p + unorml(p)), upper_edge, upper_cross); + LineCrossPoint(line(p, p + unorml(p)), downer_edge, downer_cross); + rs.push_back(InradiusOfTriangle(point(0, 0), + upper_cross, + downer_cross)); + center = InscribedCircleCenter(point(0, 0), + downer_cross, + upper_cross); + } + rs.pop_back(); + for (unsigned int i = 0; i < rs.size(); ++i) + { + ret += Circumference(rs[i]); + } + return ret; +} + +int main() +{ + int T; + scanf("%d", &T); + for (int i = 0; i < T; ++i) + { + elem B, H; + scanf("%Lf%Lf", &B, &H); + printf("%13.6Lf\n", Solve(B, H)); + if (i != T - 1) + { + puts(""); + } + } + return 0; +} diff --git a/376.cpp b/376.cpp new file mode 100644 index 0000000..7466bf8 --- /dev/null +++ b/376.cpp @@ -0,0 +1,117 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +double a, b, c[2], alpha, beta, pi; + +int solve() +{ + double s, t, u, v; + while (beta < -EPS) + { + beta += 2 * pi; + } + while (beta > (2 * pi + EPS)) + { + beta -= 2 * pi; + } + if (fabs(beta) < EPS || fabs(beta - 2 * pi) < EPS) + { + beta = 0.; + } + if (a < EPS || b < EPS) + { + return 0; + } + if (fabs(beta) < EPS) + { + c[0] = a - b; + c[1] = a + b; + } + else if (fabs(beta - pi) < EPS) + { + c[0] = b - a; + c[1] = b - a; + } + else + { + if (beta > (pi - EPS)) + { + return 0; + } + s = sin(beta); + t = a * s / b; + if (t < (-1. - EPS)) + { + return 0; + } + if (t < -1.) + { + t = -1.; + } + if (t > (1. + EPS)) + { + return 0; + } + if (t > 1.) + { + t = 1.; + } + u = sqrt(1 - t * t) * b; + v = cos(beta) * t * b / s; + c[0] = v + u; + c[1] = v - u; + } + if (c[0] < EPS && c[1] < EPS) + { + return 0; + } + if (c[0] < EPS) + { + c[0] = c[1]; + return 1; + } + if (c[1] < EPS) + { + c[1] = c[0]; + return 1; + } + return 1; +} + +int main() +{ + int t; + printf("Case A B THETA # of Side Side\n" + " # side side (deg) Triags 1 2\n"); + pi = 2. * acos(0.); + for (t = 0; scanf("%lf %lf %lf", &a, &b, &beta) == 3;) + { + if (fabs(a) < EPS && fabs(b) < EPS && fabs(beta) < EPS) + { + break; + } + printf("%4d%7.2f%7.2f%7.2f ", ++t, a, b, beta); + beta *= pi / 180.; + if (solve() == 0) + { + printf("0\n"); + } + else if (fabs(c[0] - c[1]) < EPS) + { + printf("1%9.2f\n", c[0]); + } + else if (c[0] > c[1]) + { + printf("2%9.2f%7.2f\n", c[0], c[1]); + } + else + { + printf("2%9.2f%7.2f\n", c[1], c[0]); + } + } + printf("\nEND OF REPORT for %d cases\n", t); + return 0; +} diff --git a/377.cpp b/377.cpp new file mode 100644 index 0000000..75869c2 --- /dev/null +++ b/377.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +#define MAXN 15 + +char Num1[MAXN], Num2[MAXN], Comm[3][5], Res[MAXN], Val_1[MAXN], Val_2[MAXN]; +const char SS[] = "VUCD"; +int VV[150]; + +struct ss +{ + int len; +} Q; +void Set() +{ + int i; + for (i = 0; SS[i]; i++) + { + int k = SS[i]; + VV[k] = i; + } +} +void GetV() +{ + int i, j; + int l = strlen(Num1); + Q.len = 5; + for (i = l - 1; i >= 0; i--) + { + j = Num1[i]; + Val_1[i] = VV[j]; + } + l = strlen(Num2); + for (i = l - 1; i >= 0; i--) + { + j = Num2[i]; + Val_2[i] = VV[j]; + } +} +void ADD() +{ + char Temp[MAXN]; + char TVal[MAXN]; + int i, j, c = 0; + int k = 0, s, d; + s = Q.len; + d = 4; + for (i = 0; i < MAXN; i++) + { + TVal[i] = 0; + } + i = s - 1; + while (1) + { + TVal[i] = Val_1[d--]; + if (d == -1) + { + break; + } + i--; + } + for (i = s - 1; i >= 0; i--) + { + j = TVal[i] + Val_2[i] + c; + Temp[k++] = j % 4; + c = j / 4; + } + if (c) + { + Temp[k++] = c; + } + c = 0; + Q.len = k; + for (i = k - 1; i >= 0; i--) + { + Val_2[i] = Temp[c++]; + } +} +void Right() +{ + int i; + for (i = Q.len - 1; i >= 1; i--) + { + Val_2[i] = Val_2[i - 1]; + } + Val_2[0] = 0; +} +void Left() +{ + int i; + i = Q.len; + Val_2[i++] = 0; + Q.len = i; +} +int GetR() +{ + int i, l = Q.len, d = 8 - l, x = d; + char RES[MAXN]; + for (i = 0; i < l; i++) + { + int j = Val_2[i]; + RES[d++] = SS[j]; + } + for (i = 0; i < x; i++) + { + RES[i] = 'V'; + } + RES[d] = '\0'; + return !strcmp(RES, Res); +} +void Cal() +{ + int i, j; + GetV(); + for (i = 0; i < 3; i++) + { + if (Comm[i][0] == 'A') + { + ADD(); + } + else if (Comm[i][0] == 'R') + { + Right(); + } + else if (Comm[i][0] == 'L') + { + Left(); + } + } + j = GetR(); + if (j) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } +} + +int main() +{ + int n, i; + Set(); + printf("COWCULATIONS OUTPUT\n"); + scanf("%d", &n); + while (n--) + { + scanf("%s", Num1); + scanf("%s", Num2); + for (i = 0; i < 3; i++) + { + scanf("%s", Comm[i]); + } + scanf("%s", Res); + Cal(); + } + printf("END OF OUTPUT\n"); + return 0; +} diff --git a/378.cpp b/378.cpp index 70d8a58..67a425c 100644 --- a/378.cpp +++ b/378.cpp @@ -1,61 +1,64 @@ -#include -using namespace std; -int main(){ - int test; - cin>>test; - cout<<"INTERSECTING LINES OUTPUT\n"; - while(test--){ - double x1,y1,x2,y2; - double x3,y3,x4,y4; - double m1,m2,m3,m4; - double ptx,pty; - cin>>x1>>y1>>x2>>y2; - cin>>x3>>y3>>x4>>y4; - m1=(y2-y1)/(x2-x1); - m2=(y4-y3)/(x4-x3); - if(x1==x2 || x3==x4){ - if(x1==x2){ - ptx=x1; - pty=m2*(x1-x3)+y3; - } - else{ - ptx=x3; - pty=m1*(x3-x1)+y1; - } - printf("POINT %.2lf %.2lf\n",ptx,pty); - continue; - } - else if(y1==y2 || y3==y4){ - if(y1==y2){ - pty=y1; - ptx=(y1-y3)/m2 + x3; - } - else{ - pty=y3; - ptx=(y3-y1)/m1 + x1; - } - printf("POINT %.2lf %.2lf\n",ptx,pty); - continue; - } - else if(m1==m2){ - m3=(y3-y1)/(x3-x1); - m4=(y4-y2)/(x4-x2); - if(m3==m4){ - cout<<"LINE"< + +using namespace std; + +int main() +{ + cout << "INTERSECTING LINES OUTPUT" << endl; + int t, N; + double k1, k2, k3, x1, y1, x2, y2, x3, y3, x4, y4; + double inter_x, inter_y; + cin >> N; + for (t = 0; t < N; t++) + { + cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; + if (((x1 == x2) && (x2 == x3) && (x3 == x4)) || ((y1 == y2) && (y2 == y3) && (y3 == y4))) + { + cout << "LINE" << endl; + continue; + } + k1 = (y2 - y1) / (x2 - x1);//dot 1,2 + k2 = (y4 - y3) / (x4 - x3);//dot 3,4 + k3 = (y3 - y2) / (x3 - x2);//dot 2,3 + if (x1 == x2 && x3 == x4 && x2 != x3) + { + cout << "NONE" << endl; + continue; + } + else if (y1 == y2 && y3 == y4 && y2 != y3) + { + cout << "NONE" << endl; + continue; + } + else if (x1 == x2 && k1 != k2) + { + inter_x = x1; + inter_y = k2 * (x1 - x3) + y3; + printf("POINT %.2f %.2f\n", inter_x, inter_y); + } + else if (x3 == x4 && k1 != k2) + { + inter_x = x3; + inter_y = k1 * (x3 - x1) + y1; + printf("POINT %.2f %.2f\n", inter_x, inter_y); + } + else if (k1 == k2 && k2 == k3) + { + cout << "LINE" << endl; + continue; + } + else if (k1 == k2 && k2 != k3) + { + cout << "NONE" << endl; + continue; + } + else if (k1 != k2) + { + inter_x = (y3 - k2 * x3 - y1 + k1 * x1) / (k1 - k2); + inter_y = k1 * (inter_x - x1) + y1; + printf("POINT %.2f %.2f\n", inter_x, inter_y); + } + } + cout << "END OF OUTPUT" << endl; + return 0; +} diff --git a/379.cpp b/379.cpp new file mode 100644 index 0000000..88eaca5 --- /dev/null +++ b/379.cpp @@ -0,0 +1,185 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef struct _mv +{ + pair from, to, remv; + bool found; +} mv; + +bool pegs[9][9]; +int field[9][9] = +{ + {0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 2, 3, 0, 0, 0}, + {0, 0, 0, 4, 5, 6, 0, 0, 0}, + {0, 7, 8, 9, 10, 11, 12, 13, 0}, + {0, 14, 15, 16, 17, 18, 19, 20, 0}, + {0, 21, 22, 23, 24, 25, 26, 27, 0}, + {0, 0, 0, 28, 29, 30, 0, 0, 0}, + {0, 0, 0, 31, 32, 33, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0} +}; + +void clear_pegs() +{ + rep(i, 9) + { + rep(j, 9) + { + pegs[i][j] = false; + } + } +} + +void set_peg(int peg) +{ + rep(i, 9) + { + rep(j, 9) + { + if (field[i][j] == peg) + { + pegs[i][j] = true; + } + } + } +} + +mv find_next_mv() +{ + mv result; + result.found = false; + pair to_field, from_field, remv_field; + int from = 0, to = 0; + rep(i, 9) + { + rep(j, 9) + { + if (pegs[i][j]) + { + // left + if (pegs[i][j - 1] && !pegs[i][j + 1]) + { + if (field[i][j + 1] > to || field[i][j + 1] == to && to > 0 && field[i][j - 1] > from) + { + to = field[i][j + 1]; + to_field = make_pair(i, j + 1); + from = field[i][j - 1]; + from_field = make_pair(i, j - 1); + result.found = true; + remv_field = make_pair(i, j); + } + } + // top + if (pegs[i - 1][j] && !pegs[i + 1][j]) + { + if (field[i + 1][j] > to || field[i + 1][j] == to && to > 0 && field[i - 1][j] > from) + { + to = field[i + 1][j]; + to_field = make_pair(i + 1, j); + from = field[i - 1][j]; + from_field = make_pair(i - 1, j); + result.found = true; + remv_field = make_pair(i, j); + } + } + // right + if (pegs[i][j + 1] && !pegs[i][j - 1]) + { + if (field[i][j - 1] > to || field[i][j - 1] == to && to > 0 && field[i][j + 1] > from) + { + to = field[i][j - 1]; + to_field = make_pair(i, j - 1); + from = field[i][j + 1]; + from_field = make_pair(i, j + 1); + result.found = true; + remv_field = make_pair(i, j); + } + } + // btm + if (pegs[i + 1][j] && !pegs[i - 1][j]) + { + if (field[i - 1][j] > to || field[i - 1][j] == to && to > 0 && field[i + 1][j] > from) + { + to = field[i - 1][j]; + to_field = make_pair(i - 1, j); + from = field[i + 1][j]; + from_field = make_pair(i + 1, j); + result.found = true; + remv_field = make_pair(i, j); + } + } + } + } + } + if (result.found) + { + result.to = to_field; + result.from = from_field; + result.remv = remv_field; + } + return result; +} + +void play() +{ + mv next_mv; + while (1) + { + next_mv = find_next_mv(); + if (!next_mv.found) + { + break; + } + // rm peg + pegs[next_mv.remv.first][next_mv.remv.second] = false; + // mv peg to new pos + pegs[next_mv.from.first][next_mv.from.second] = false; + pegs[next_mv.to.first][next_mv.to.second] = true; + } +} + +int cnt_res() +{ + int res = 0; + rep(i, 9) + { + rep(j, 9) + { + if (pegs[i][j]) + { + res += field[i][j]; + } + } + } + return res; +} + +int main() +{ + int n, peg; + scanf("%d", &n); + printf("HI Q OUTPUT\n"); + while (n--) + { + clear_pegs(); + while (scanf("%d", &peg) == 1) + { + if (peg != 0) + { + set_peg(peg); + continue; + } + play(); + printf("%d\n", cnt_res()); + break; + } + } + printf("END OF OUTPUT\n"); + return 0; +} diff --git a/380.cpp b/380.cpp new file mode 100644 index 0000000..c959b3e --- /dev/null +++ b/380.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAXN 10002 + +struct xx +{ + int st, end, des; +}; +struct ss +{ + xx list[111]; + int ind; +} Call[MAXN]; +int M[10003], Num[MAXN], tn; +char Fg[MAXN]; + +void ReadCase() +{ + int a, b, c, d, u, v; + tn = 1; + while (1) + { + scanf("%d", &a); + if (!a) + break; + u = M[a]; + if (!u) + { + u = tn++; + Num[tn - 1] = a; + M[a] = tn - 1; + } + scanf("%d%d%d", &b, &c, &d); + v = M[d]; + if (!v) + { + v = tn++; + Num[tn - 1] = d; + M[d] = tn - 1; + } + Call[u].list[Call[u].ind].des = v; + Call[u].list[Call[u].ind].st = b; + Call[u].list[Call[u].ind++].end = c + b; + } +} +int Recur(int u, int time) +{ + int i; + if (Fg[u]) + return 10000; + Fg[u] = 1; + for (i = 0; i < Call[u].ind; i++) + if (time >= Call[u].list[i].st && time <= Call[u].list[i].end) + return Recur(Call[u].list[i].des, time); + return u; +} +void Cal() +{ + int n, u, t, d, v; + while (1) + { + scanf("%d", &t); + if (t == 9000) + break; + scanf("%d", &n); + u = M[n]; + if (u) + { + d = Recur(u, t); + v = Num[d]; + } + else + v = n; + + if (d == 10000) + printf("AT %.4d CALL TO %.4d RINGS 9999\n", t, n); + else + printf("AT %.4d CALL TO %.4d RINGS %.4d\n", t, n, v); + + for (t = 1; t <= tn; t++) + Fg[t] = 0; + } +} +void Free() +{ + int i; + for (i = 0; i < 10000; i++) + M[i] = 0; + for (i = 0; i <= tn; i++) + Call[i].ind = 0; +} + +int main() +{ + int ks, sys = 1; + printf("CALL FORWARDING OUTPUT\n"); + scanf("%d", &ks); + while (ks--) + { + ReadCase(); + printf("SYSTEM %d\n", sys++); + Cal(); + Free(); + } + printf("END OF OUTPUT\n"); + return 0; +} diff --git a/381.cpp b/381.cpp new file mode 100644 index 0000000..746260c --- /dev/null +++ b/381.cpp @@ -0,0 +1,207 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +enum grade +{ + A = 4, + B = 3, + C = 2, + D = 1, + F = 0 +}; + +typedef struct _student +{ + int number_of_tests; + int grades[10]; + int bonus; + int absences; + double average; + grade final_grade; +} student; + +student students[30]; + +void drop_students_lowest_grade(int number_of_students, int number_of_tests) +{ + if (number_of_tests < 3) + { + return; + } + rep(i, number_of_students) + { + sort(students[i].grades, students[i].grades + number_of_tests, greater()); + students[i].number_of_tests--; + } +} + +void compute_students_averages(int number_of_students) +{ + rep(i, number_of_students) + { + double sum = 0; + rep(j, students[i].number_of_tests) + { + sum += students[i].grades[j]; + } + students[i].average = sum / students[i].number_of_tests; + } +} + +double compute_class_mean(int number_of_students) +{ + double sum = 0; + rep(i, number_of_students) + { + sum += students[i].average; + } + return sum / number_of_students; +} + +double computer_class_standard_deviation(int number_of_students, double class_mean) +{ + double sum = 0; + rep(i, number_of_students) + { + sum += (students[i].average - class_mean) * (students[i].average - class_mean); + } + return sqrt(sum / number_of_students); +} + +void adjust_students_averages(int number_of_students) +{ + rep(i, number_of_students) + { + int increase = (students[i].bonus / 2) * 3; + students[i].average += increase; + } +} + +void determine_students_unadjusted_grades(int number_of_students, double class_mean, double class_sd) +{ + rep(i, number_of_students) + { + if (students[i].average >= class_mean + class_sd) + { + students[i].final_grade = A; + } + else if (students[i].average >= class_mean) + { + students[i].final_grade = B; + } + else if (students[i].average >= class_mean - class_sd) + { + students[i].final_grade = C; + } + else + { + students[i].final_grade = D; + } + } +} + +void increase_final_grade(student &s) +{ + switch (s.final_grade) + { + case B: + s.final_grade = A; + break; + case C: + s.final_grade = B; + break; + case D: + s.final_grade = C; + break; + case F: + s.final_grade = D; + break; + default: + break; + } +} + +void decrease_final_grade(student &s) +{ + switch (s.final_grade) + { + case A: + s.final_grade = B; + break; + case B: + s.final_grade = C; + break; + case C: + s.final_grade = D; + break; + case D: + s.final_grade = F; + break; + default: + break; + } +} + +void adjust_grades_based_on_attendance(int number_of_students) +{ + rep(i, number_of_students) + { + if (students[i].absences == 0) + { + increase_final_grade(students[i]); + } + else + { + int decreases = students[i].absences / 4; + rep(j, decreases) + { + decrease_final_grade(students[i]); + } + } + } +} + +double count_average_grade_point_of_class(int number_of_students) +{ + double sum = 0; + rep(i, number_of_students) + { + sum += int(students[i].final_grade); + } + return sum / number_of_students; +} + +int main() +{ + int n, s, t; + cin >> n; + cout << "MAKING THE GRADE OUTPUT" << endl; + while (n--) + { + cin >> s >> t; + rep(i, s) + { + students[i].number_of_tests = t; + rep(j, t) + { + cin >> students[i].grades[j]; + } + cin >> students[i].bonus; + cin >> students[i].absences; + } + drop_students_lowest_grade(s, t); + compute_students_averages(s); + double class_mean = compute_class_mean(s); + double class_sd = computer_class_standard_deviation(s, class_mean); + adjust_students_averages(s); + determine_students_unadjusted_grades(s, class_mean, class_sd); + adjust_grades_based_on_attendance(s); + cout << fixed << setprecision(1) << count_average_grade_point_of_class(s) << endl; + } + cout << "END OF OUTPUT" << endl; + return 0; +} diff --git a/382.cpp b/382.cpp index 16ff4e2..c9f30b2 100644 --- a/382.cpp +++ b/382.cpp @@ -1,27 +1,40 @@ -#include - -using namespace std; - -int main() -{ - int N,div_sum=1; - cout<<"PERFECTION OUTPUT"<>N)) - { - div_sum=1; - for(int i=2;iN) - cout<<"ABUNDANT"< + +using namespace std; + +int main() +{ + int n; + cout << "PERFECTION OUTPUT" << endl; + while (cin >> n) + { + if (n == 0) + { + break; + } + int sum = 0; + for (int i = 1; i <= n / 2; i++) + { + if (n % i == 0) + { + sum += i; + } + } + cout << setw(5) << n << " "; + if (sum == n) + { + cout << "PERFECT"; + } + else if (sum < n) + { + cout << "DEFICIENT"; + } + else + { + cout << "ABUNDANT"; + } + cout << endl; + } + cout << "END OF OUTPUT" << endl; + return 0; +} diff --git a/383.cpp b/383.cpp index d69b4e3..9705567 100644 --- a/383.cpp +++ b/383.cpp @@ -1,101 +1,86 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -#define INF 999999999 - -using namespace std; - -struct node{ - string str; - int dis; - node (string s, int d){ - str = s; - dis = d; - } -}; - -int bfs( string fr, string to, map > adj ){ - map m; - map ::iterator it; - queue q; - node source(fr, 0); - q.push(source); - while(!q.empty()){ - node n = q.front(); - if(n.str == to) - return n.dis; - q.pop(); - m[n.str] = n.dis; - for(int i=0; i> test; - cout<< "SHIPPING ROUTES OUTPUT\n\n"; - for(t=1; t<=test; t++){ - cout << "DATA SET "<> M >> N >> P; - map < string, vector > adj; - for(i=0; i nul; - cin >> str; - adj[str] = nul; - } - for(i=0; i> strA >> strB; - adj[strA].push_back(strB); - adj[strB].push_back(strA); - } - for(i=0; i> val >> fr >> to; - int cost = bfs(fr, to, adj); - if(cost == INF) - cout << "NO SHIPMENT POSSIBLE" << endl; - else - cout << "$" << val*cost*100 << endl; - } - cout << endl; - } - cout << "END OF OUTPUT" << endl; - return 0; -} +#include + +using namespace std; + +typedef unsigned int uint; +typedef long long int64; +typedef unsigned long long uint64; + +#define INF 999999999 + +struct node +{ + string str; + int dis; + node(string s, int d) + { + str = s; + dis = d; + } +}; + +int bfs(string fr, string to, map> adj) +{ + map m; + map::iterator it; + queue q; + node source(fr, 0); + q.push(source); + while (!q.empty()) + { + node n = q.front(); + if (n.str == to) + return n.dis; + q.pop(); + m[n.str] = n.dis; + for (int i = 0; i < adj[n.str].size(); i++) + { + it = m.find(adj[n.str][i]); + if (it == m.end()) + q.push(node(adj[n.str][i], n.dis + 1)); + } + } + return INF; +} + +int main() +{ + int test, t, i, j; + cin >> test; + cout << "SHIPPING ROUTES OUTPUT\n\n"; + for (t = 1; t <= test; t++) + { + cout << "DATA SET " << t << "\n\n"; + int M, N, P; + cin >> M >> N >> P; + map> adj; + for (i = 0; i < M; i++) + { + string str; + vector nul; + cin >> str; + adj[str] = nul; + } + for (i = 0; i < N; i++) + { + string strA, strB; + cin >> strA >> strB; + adj[strA].push_back(strB); + adj[strB].push_back(strA); + } + for (i = 0; i < P; i++) + { + int val; + string fr, to; + cin >> val >> fr >> to; + int cost = bfs(fr, to, adj); + if (cost == INF) + cout << "NO SHIPMENT POSSIBLE" << endl; + else + cout << "$" << val * cost * 100 << endl; + } + cout << endl; + } + cout << "END OF OUTPUT" << endl; + return 0; +} diff --git a/384.cpp b/384.cpp new file mode 100644 index 0000000..d397d64 --- /dev/null +++ b/384.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +int read_slimp(int start, string &s); +int read_slump(int start, string &s); + +int read_slimp(int start, string &s) +{ + int i = start, sz = s.size(); + if (i == sz) + { + return -1; + } + if (s[i] == 'A') + { + i++; + } + else + { + return -1; + } + if (i == sz) + { + return -1; + } + if (s[i] == 'H') + { + return i + 1; + } + if (s[i] == 'B') + { + i = read_slimp(i + 1, s); + } + else + { + i = read_slump(i, s); + } + if (i == -1 || i == sz) + { + return -1; + } + if (s[i] == 'C') + { + return i + 1; + } + return -1; +} + +int read_slump(int start, string &s) +{ + int i = start, sz = s.size(); + if (i == sz) + { + return -1; + } + if (s[i] == 'D' || s[i] == 'E') + { + i++; + } + else + { + return -1; + } + if (i == sz) + { + return -1; + } + if (s[i] != 'F') + { + return -1; + } + for (; s[i] == 'F' && i < sz; i++) + ; + if (i == sz) + { + return -1; + } + if (s[i] == 'G') + { + return i + 1; + } + i = read_slump(i, s); + if (i == -1) + { + return -1; + } + return i; +} + +bool is_slurpy(string &s) +{ + int end = read_slimp(0, s); + if (end == -1) + { + return false; + } + end = read_slump(end, s); + return end == s.size(); +} + +int main() +{ + int n; + string input; + cin >> n; + getline(cin, input);// getting \n after n + cout << "SLURPYS OUTPUT" << endl; + while (n--) + { + getline(cin, input); + cout << (is_slurpy(input) ? "YES" : "NO") << endl; + } + cout << "END OF OUTPUT" << endl; + return 0; +} diff --git a/385.cpp b/385.cpp new file mode 100644 index 0000000..2f1a94d --- /dev/null +++ b/385.cpp @@ -0,0 +1,281 @@ +#include + +using namespace std; + +char *strrev(char *str) +{ + char *p1, *p2; + if (!str || !*str) + { + return str; + } + for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) + { + *p1 ^= *p2; + *p2 ^= *p1; + *p1 ^= *p2; + } + return str; +} +void codon2protein(char *codon) +{ + switch (codon[0]) + { + case 'T': + switch (codon[1]) + { + case 'T': + switch (codon[2]) + { + case 'T': + cout << "Phe"; + break; + case 'C': + cout << "Phe"; + break; + case 'A': + cout << "Leu"; + break; + case 'G': + cout << "Leu"; + } + break; + case 'C': + cout << "Ser"; + break; + case 'A': + switch (codon[2]) + { + case 'T': + cout << "Tyr"; + break; + case 'C': + cout << "Tyr"; + } + break; + case 'G': + switch (codon[2]) + { + case 'T': + cout << "Cys"; + break; + case 'C': + cout << "Cys"; + break; + case 'G': + cout << "Trp"; + } + } + break; + case 'C': + switch (codon[1]) + { + case 'T': + cout << "Leu"; + break; + case 'C': + cout << "Pro"; + break; + case 'A': + switch (codon[2]) + { + case 'T': + cout << "His"; + break; + case 'C': + cout << "His"; + break; + case 'A': + cout << "Gln"; + break; + case 'G': + cout << "Gln"; + } + break; + case 'G': + cout << "Arg"; + break; + } + break; + case 'A': + switch (codon[1]) + { + case 'T': + switch (codon[2]) + { + case 'G': + cout << "Met"; + break; + default: + cout << "Ile"; + } + break; + case 'C': + cout << "Thr"; + break; + case 'A': + switch (codon[2]) + { + case 'T': + cout << "Asn"; + break; + case 'C': + cout << "Asn"; + break; + case 'A': + cout << "Lys"; + break; + case 'G': + cout << "Lys"; + } + break; + case 'G': + switch (codon[2]) + { + case 'T': + cout << "Ser"; + break; + case 'C': + cout << "Ser"; + break; + case 'A': + cout << "Arg"; + break; + case 'G': + cout << "Arg"; + } + } + break; + case 'G': + switch (codon[1]) + { + case 'T': + cout << "Val"; + break; + case 'C': + cout << "Ala"; + break; + case 'A': + switch (codon[2]) + { + case 'T': + cout << "Asp"; + break; + case 'C': + cout << "Asp"; + break; + case 'A': + cout << "Glu"; + break; + case 'G': + cout << "Glu"; + } + break; + case 'G': + cout << "Gly"; + break; + } + } +} +void transcodes(char *dna, int codes) +{ + for (int i = 0; i < codes; i++) + { + char codon[4]; + strncpy(codon, dna + 3 * i, 3); + codon[3] = '\0'; + codon2protein(codon); + if (i < codes - 1) + { + cout << '-'; + } + }; + cout << endl; +} +char *terminator(char *start) +{ + for (int i = 6; i < (int)strlen(start) - 2; i += 3) + if (start[i] == 'T') + switch (start[i + 1]) + { + case 'A': + if (start[i + 2] == 'A' || start[i + 2] == 'G') + { + return start + i; + } + break; + case 'G': + if (start[i + 2] == 'A') + { + return start + i; + } + } + return NULL; +} +int translate(char *dna) +{ + char *start = strstr(dna, "ATG"); + while (start != NULL) + { + char *end = terminator(start); + if (end != NULL) + { + transcodes(start + 3, (end - start - 3) / 3); + return 1; + } + else + { + start = strstr(start + 1, "ATG"); + } + } + return 0; +} +void complement(char *dna) +{ + for (int i = 0; i < (int)strlen(dna); i++) + switch (dna[i]) + { + case 'A': + dna[i] = 'T'; + break; + case 'C': + dna[i] = 'G'; + break; + case 'G': + dna[i] = 'C'; + break; + case 'T': + dna[i] = 'A'; + } +} +int main() +{ + char dna[256]; + while (true) + { + scanf("%s", dna); + if (strcmp(dna, "*") == 0) + { + break; + } + int r1 = translate(dna); + if (!r1) + { + int r2 = translate(strrev(dna)); + if (!r2) + { + complement(dna); + int r3 = translate(dna); + if (!r3) + { + int r4 = translate(strrev(dna)); + if (!r4) + { + printf("*** No translatable DNA found ***\n"); + } + } + } + } + } + return 0; +} diff --git a/386.cpp b/386.cpp index a631aa1..3e84ee1 100644 --- a/386.cpp +++ b/386.cpp @@ -1,57 +1,42 @@ -#include -#include -#include -#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; -typedef long double ldouble; -using namespace std; -void swapp(int a, int b){ - int temp=a; - a=b; - b=temp; -} -int main(){ - int a=0,b=0,c=0,d=0,n,i,j; - int cubes[300][4]; - for(a=2;a<=200;a++){ - int lhs=a*a*a,ctr=0; - for(b=2;b<=a;b++){ - int rhs1=b*b*b; - for(c=2;c<=b;c++){ - int rhs2=c*c*c; - for(d=2;d<=c;d++){ - int rhs3=d*d*d; - if(rhs1+rhs2+rhs3==lhs){ - cubes[n][0]=a; cubes[n][1]=d; cubes[n][2]=c; cubes[n][3]=b; - n++; - } - } - } - } - } - for(i=0;i=cubes[j][1]){ - if(cubes[i][1]>cubes[j][1]){ - swapp(cubes[i][1],cubes[j][1]); - swapp(cubes[i][2],cubes[j][2]); - swapp(cubes[i][3],cubes[j][3]); - } - else{ - if(cubes[i][2]>cubes[j][2]){ - swapp(cubes[i][1],cubes[j][1]); - swapp(cubes[i][2],cubes[j][2]); - swapp(cubes[i][3],cubes[j][3]); - } - } - } - } - } - for(i=0;i + +using namespace std; + +void Cal(int n) +{ + int i, j, k, p, q, r; + int N = n * n * n; + for (i = 2; i <= n; i++) + { + p = i * i * i; + for (j = i + 1; j < n; j++) + { + q = j * j * j; + if (p + q >= N) + { + break; + } + for (k = j + 1; k < n; k++) + { + r = k * k * k; + if (N == r + p + q) + { + printf("Cube = %d, Triple = (%d,%d,%d)\n", n, i, j, k); + } + if (r + p + q > N) + { + break; + } + } + } + } +} +int main() +{ + int i; + for (i = 1; i <= 200; i++) + { + Cal(i); + } + return 0; +} diff --git a/387.cpp b/387.cpp new file mode 100644 index 0000000..0b19534 --- /dev/null +++ b/387.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +/* +387 +A Puzzling Problem +*/ +struct ss +{ + char sp[6][6]; + int r, c, total; +} P[6]; +char B[5][5]; +int tp, to; +void ReadCase() +{ + int i, j, k, c; + to = 0; + for (i = 0; i < tp; i++) + { + scanf("%d%d", &P[i].r, &P[i].c); + c = 0; + for (j = 0; j < P[i].r; j++) + { + scanf("%s", P[i].sp[j]); + for (k = 0; P[i].sp[j][k]; k++) + { + if (P[i].sp[j][k] == '1') + { + to++; + c++; + } + } + } + P[i].total = c; + } +} +void Print() +{ + int i, j; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + printf("%d", B[i][j]); + } + printf("\n"); + } +} +int Recur(int n, int r, int c, int total, int level) +{ + int i, j, k = n + 1, l, m, f; + if (level == tp) + { + if (total == 16) + { + for (i = r; i < r + P[n].r; i++) + { + for (j = c; j < c + P[n].c; j++) + { + if (P[n].sp[i - r][j - c] == '1') + { + B[i][j] = n + 1; + } + } + } + Print(); + return 1; + } + return 0; + } + for (i = r; i < r + P[n].r; i++) + { + for (j = c; j < c + P[n].c; j++) + { + if (P[n].sp[i - r][j - c] == '1') + { + B[i][j] = n + 1; + } + } + } + for (i = 0; i < 4; i++) + { + if ((i + P[k].r - 1) > 3) + { + break; + } + for (j = 0; j < 4; j++) + { + if ((j + P[k].c - 1) > 3) + { + break; + } + f = 1; + for (l = i; l < i + P[k].r; l++) + { + for (m = j; m < j + P[k].c; m++) + { + if (B[l][m] && P[k].sp[l - i][m - j] == '1') + { + f = 0; + break; + } + } + if (!f) + { + break; + } + } + if (f) + { + if (Recur(k, i, j, total + P[k].total, level + 1)) + { + return 1; + } + } + } + } + for (i = r; i < r + P[n].r; i++)// Free + { + for (j = c; j < c + P[n].c; j++) + { + if (B[i][j] == n + 1) + { + B[i][j] = 0; + } + } + } + return 0; +} +void Cal() +{ + int i, j, d; + if (to > 16) + { + printf("No solution possible\n"); + return; + } + for (i = 0; i < 4; i++) + { + if ((i + P[0].r - 1) > 3) + { + break; + } + for (j = 0; j < 4; j++) + { + if ((j + P[0].c - 1) > 3) + { + break; + } + d = Recur(0, i, j, P[0].total, 1); + if (d) + { + return; + } + } + } + printf("No solution possible\n"); +} +void Free() +{ + memset(B, 0, 5 * 5); +} +int main() +{ + int k = 0; + while (scanf("%d", &tp) && tp) + { + ReadCase(); + if (k++) + { + putchar('\n'); + } + Cal(); + Free(); + } + return 0; +} diff --git a/388.cpp b/388.cpp new file mode 100644 index 0000000..1571394 --- /dev/null +++ b/388.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +#define N 27 +#define INF 100 +#define EARTH 26 + +int dist[N][N]; +double val[N]; + +void floyd_warshall(void) +{ + register int i, j, k; + for (k = 0; k < N; ++k) + { + for (i = 0; i < N; ++i) + { + for (j = 0; j < N; ++j) + { + int dist_ikj = dist[i][k] + dist[k][j]; + if (dist_ikj < dist[i][j]) + { + dist[i][j] = dist_ikj; + } + } + } + } +} + +void add_edge(char A, char B) +{ + int i = (A == '*') ? EARTH : A - 'A'; + int j = (B == '*') ? EARTH : B - 'A'; + dist[i][j] = 1; + dist[j][i] = 1; +} + +void init() +{ + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < N; ++j) + { + dist[i][j] = INF; + } + } + for (int i = 0; i < N; ++i) + { + val[i] = 0.0f; + } +} + +double import_val(int planet) +{ + if (dist[EARTH][planet] < INF) + { + return pow(0.95, dist[EARTH][planet] - 1) * val[planet]; + } + else + { + return 0.0; + } +} + +void find_best() +{ + double best = 0.0; + int i, planet = 0; + for (i = 0; i < EARTH; ++i) + { + double v = import_val(i); + if (v > best) + { + best = v; + planet = i; + } + } + printf("Import from %c\n", planet + 'A'); +} + +int main() +{ + int i, j, planets; + while (scanf("%d\n", &planets) == 1) + { + init(); + for (i = 0; i < planets; ++i) + { + double v; + char p, neighbor[N]; + scanf("%c %lf %s\n", &p, &v, neighbor); + val[p - 'A'] = v; + for (j = 0; j < (int)strlen(neighbor); ++j) + { + add_edge(p, neighbor[j]); + } + } + floyd_warshall(); + find_best(); + } + return 0; +} diff --git a/389.cpp b/389.cpp index f7b45c9..f60a51a 100644 --- a/389.cpp +++ b/389.cpp @@ -1,70 +1,59 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -typedef long long int64; +inline int CharToDigit(char c) +{ + if (isdigit(c)) + return c - '0'; + return c - 'A' + 10; +} + +inline char DigitToChar(int n) +{ + return "0123456789ABCDEF"[n]; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string Dic = "0123456789ABCDEF"; - string S; - int64 A, B; - while (cin >> S >> A >> B) { - int64 i, j; - int64 V = 0, mul = 1; - vector< char > C; - int L = S.length() - 1; - FOD(i, L, 0) { - if (isalpha(S[i])) - V += (S[i] - 'A' + 10) * mul; - else - V += (S[i] - '0') * mul; - - mul *= A; - } - while (V > 0) { - C.push_back(Dic[V % B]); - V /= B; - } - if (C.size() == 0) - C.push_back('0'); - string O = ""; - if (C.size() > 7) - O = "ERROR"; - else { - O = ""; - reverse(C.begin(), C.end()); - L = C.size() - 1; - FOI(i, 0, L) - O += C[i]; - } - printf("%7s\n", O.c_str()); +// convert string 'in' to int 'value' +long long parseValue(int base1, string &in) +{ + long long value = 0, curBase = 1; + for (int i = in.length() - 1; i >= 0; --i) + { + value += CharToDigit(in[i]) * curBase; + curBase *= base1; } + return value; +} + +// convert int 'value' to string 'out' +string toBase(int base2, long long value) +{ + if (value == 0) + return "0"; + string out; + while (value > 0) + { + out += DigitToChar(value % base2); + value /= base2; + } + reverse(out.begin(), out.end()); + return out; +} + +string convert(string &in, int base1, int base2) +{ + string out = toBase(base2, parseValue(base1, in)); + if (out.length() > 7) + return "ERROR"; + return out; +} + +int main() +{ + int base1, base2; + string in; + while (cin >> in >> base1 >> base2) + printf("%7s\n", convert(in, base1, base2).c_str()); return 0; } diff --git a/390.cpp b/390.cpp new file mode 100644 index 0000000..f052260 --- /dev/null +++ b/390.cpp @@ -0,0 +1,192 @@ +#include + +using namespace std; + +struct node +{ + char seq[6]; + int freq; + node *nxt; +}; +node *lst = NULL; + +char seq[6]; +int seqlen; + +int isaseq() +{ + if ((int)strlen(seq) < seqlen) + { + return 0; + } + for (int i = 0; i < seqlen; i++) + { + if (!isalpha(seq[i])) + { + return 0; + } + } + return 1; +} +void addchar(char c) +{ + int n = strlen(seq); + if (n < seqlen) + { + seq[n] = c; + seq[n + 1] = '\0'; + return; + } + for (int i = 0; i < n - 1; i++) + { + seq[i] = seq[i + 1]; + } + seq[n - 1] = c; +} +void addseq(char *seq) +{ + if (lst == NULL || strcmp(seq, lst->seq) < 0) + { + node *n = new node; + strcpy(n->seq, seq); + n->freq = 1; + n->nxt = lst; + lst = n; + return; + } + if (strcmp(seq, lst->seq) == 0) + { + lst->freq += 1; + return; + } + node *p = lst; + while (p->nxt != NULL && strcmp(seq, p->nxt->seq) >= 0) + { + p = p->nxt; + } + if (strcmp(seq, p->seq) == 0) + { + p->freq += 1; + return; + } + node *n = new node; + strcpy(n->seq, seq); + n->freq = 1; + n->nxt = p->nxt; + p->nxt = n; +} +void freelst() +{ + while (lst != NULL) + { + node *d = lst; + lst = lst->nxt; + delete d; + } +} +void printdelmax_() +{ + int max_ = 0; + node *p = lst; + while (p != NULL) + { + if (p->freq > max_) + { + max_ = p->freq; + } + p = p->nxt; + } + if (max_ == 0) + { + return; + } + printf("Frequency = %d, Sequence(s) = (", max_); + int gotone = 0; + node *q = NULL; + p = lst; + while (p != NULL) + { + if (p->freq == max_) + { + if (gotone) + { + cout << ','; + } + cout << p->seq; + gotone = 1; + node *d = p; + if (q == NULL) + { + lst = p->nxt; + } + else + { + q->nxt = p->nxt; + } + p = p->nxt; + delete d; + } + else + { + q = p; + p = p->nxt; + } + } + printf(")\n"); +} +void report() +{ + printf("Analysis for Letter Sequences of Length %d\n", seqlen); + printf("-----------------------------------------\n"); + for (int i = 0; i < 5; i++) + { + printdelmax_(); + } + if (seqlen < 5) + { + printf("\n"); + } +} + +char c, text[10240]; +int text_index = 0; + +void cnt(int n) +{ + seq[0] = '\0'; + seqlen = n; + if (n == 1) + { + while (scanf("%c", &c) == 1) + { + addchar(toupper(char(c))); + if (isaseq()) + { + addseq(seq); + } + text[text_index++] = c; + } + } + else + { + for (int i = 0; i < text_index; ++i) + { + addchar(toupper(char(text[i]))); + if (isaseq()) + { + addseq(seq); + } + } + } + report(); + freelst(); +} + +int main() +{ + for (int i = 1; i <= 5; i++) + { + cnt(i); + } + return 0; +} diff --git a/391.cpp b/391.cpp new file mode 100644 index 0000000..56d6e80 --- /dev/null +++ b/391.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int cin_, lahead; +int fpeek(FILE *stream) +{ + int c; + c = fgetc(stream); + ungetc(c, stream); + return c; +} + +int main() +{ + while ((cin_ = fgetc(stdin)) != EOF) + { + lahead = fpeek(stdin); + if (cin_ == '\\') + { + if (lahead == 'b' || lahead == 'i') + { + cin_ = fgetc(stdin); + } + else if (lahead == '*') + { + cin_ = fgetc(stdin); + while ((cin_ = fgetc(stdin)) != EOF && + (cin_ != '\\' || (cin_ == '\\' && fpeek(stdin) != '*'))) + { + fputc(cin_, stdout); + } + if (fpeek(stdin) == '*') + { + cin_ = fgetc(stdin); + } + } + else if (lahead == 's') + { + cin_ = fgetc(stdin); + while ((cin_ = fgetc(stdin)) != EOF && isdigit(cin_)); + if (cin_ != EOF && cin_ != '.') + { + ungetc(cin_, stdin); + } + while ((cin_ = fgetc(stdin)) != EOF && isdigit(cin_)); + if (cin_ != EOF) + { + ungetc(cin_, stdin); + } + } + else if (lahead != EOF) + { + cin_ = fgetc(stdin); + fputc(cin_, stdout); + } + } + else + { + fputc(cin_, stdout); + } + } + return 0; +} diff --git a/392.cpp b/392.cpp new file mode 100644 index 0000000..d825fce --- /dev/null +++ b/392.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ +int main() +{ + int temp; + while (scanf("%d", &temp) == 1) + { + bool first = true; + if (temp > 0) + { + first = false; + if (temp != 1) + { + printf("%dx^8", temp); + } + else + { + printf("x^8"); + } + } + else if (temp < 0) + { + first = false; + if (temp != -1) + { + printf("%dx^8", temp); + } + else + { + printf("-x^8"); + } + } + for (int i = 7; i > 1; i--) + { + scanf("%d", &temp); + if (temp > 0) + { + if (first) + { + first = false; + if (temp != 1) + { + printf("%dx^%d", temp, i); + } + else + { + printf("x^%d", i); + } + } + else + { + if (temp != 1) + { + printf(" + %dx^%d", temp, i); + } + else + { + printf(" + x^%d", i); + } + } + } + else if (temp < 0) + { + if (first) + { + first = false; + if (temp != -1) + { + printf("%dx^%d", temp, i); + } + else + { + printf("-x^%d", i); + } + } + else + { + if (temp != -1) + { + printf(" - %dx^%d", -1 * temp, i); + } + else + { + printf(" - x^%d", i); + } + } + } + } + scanf("%d", &temp); + if (temp > 0) + { + if (first) + { + first = false; + if (temp != 1) + { + printf("%dx", temp); + } + else + { + printf("x"); + } + } + else + { + if (temp != 1) + { + printf(" + %dx", temp); + } + else + { + printf(" + x"); + } + } + } + else if (temp < 0) + { + if (first) + { + first = false; + if (temp != -1) + { + printf("%dx", temp); + } + else + { + printf("-x"); + } + } + else + { + if (temp != -1) + { + printf(" - %dx", -1 * temp); + } + else + { + printf(" - x"); + } + } + } + scanf("%d", &temp); + if (temp > 0) + { + if (first) + { + first = false; + printf("%d", temp); + } + else + { + printf(" + %d", temp); + } + } + else if (temp < 0) + { + if (first) + { + first = false; + printf("%d", temp); + } + else + { + printf(" - %d", -1 * temp); + } + } + else if (first) + { + printf("0"); + } + putchar(10); + } +} diff --git a/393.cpp b/393.cpp new file mode 100644 index 0000000..a0174e7 --- /dev/null +++ b/393.cpp @@ -0,0 +1,178 @@ +#include + +using namespace std; + +const double maxdist = 0x7FFFFFFF; +int wall_num; +int edge_num; +int point_num; +double precision = 0.00000001;//用æ¥æŽ§åˆ¶åˆ¤æ–­ç²¾åº¦ +double dist[100]; +double point_dist[100][100]; +struct edge_node +{ + double x1, x2, y1, y2; +} edge[80]; +struct point_node +{ + double x, y; +} point[100]; + +/* + *在边的集åˆé‡Œæ·»åŠ è¾¹ + *在点的集åˆé‡Œæ·»åŠ ç‚¹ + */ +void add_edge(double x1, double y1, double x2, double y2) +{ + edge[edge_num].x1 = x1; + edge[edge_num].x2 = x2; + edge[edge_num].y1 = y1; + edge[edge_num].y2 = y2; + ++edge_num; +} +void add_point(double x, double y) +{ + point[point_num].x = x; + point[point_num].y = y; + point_num++; +} + +/* + *使用dijkstra求两点之间 + */ +void dijkstra() +{ + for (int i = 1; i < point_num; i++) + { + dist[i] = maxdist; + } + bool reach[100]; + memset(reach, true, sizeof(reach)); + for (int i = 1; i <= point_num; i++) + { + int pos; + double value = maxdist; + for (int j = 0; j < point_num; j++) + if (dist[j] < value && reach[j]) + { + value = dist[j]; + pos = j; + } + reach[pos] = false; + for (int j = 0; j < point_num; j++) + if (reach[j] && + dist[pos] + point_dist[pos][j] < dist[j]) + { + dist[j] = dist[pos] + point_dist[pos][j]; + } + } +} + +/* + *以下一连串å­ç¨‹åºç”¨å‰ç§¯æ¥åˆ¤æ–­ä¸¤æ¡çº¿æ®µæ˜¯å¦ç›¸äº¤ + */ +double det(double x1, double y1, double x2, double y2) +{ + return x1 * y2 - x2 * y1; +} +double cross(point_node a, point_node b, point_node c) +{ + return det(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y); +} +int cmp(double d) +{ + if (fabs(d) < precision) + { + return 0; + } + return (d > 0) ? 1 : -1; +} +bool segment_cross_simple(point_node a, point_node b, point_node c, point_node d) +{ + if (((cmp(cross(a, c, d)) ^ cmp(cross(b, c, d))) == -2) && + ((cmp(cross(c, a, b)) ^ cmp(cross(d, a, b))) == -2)) + { + return true; + } + else + { + return false; + } +} +/* + *解决过程 + */ +void solve() +{ + /* + *åˆå§‹åŒ–过程 + */ + memset(edge, 0, sizeof(edge)); + memset(point, 0, sizeof(point)); + memset(dist, 0, sizeof(dist)); + memset(point_dist, 0, sizeof(point_dist)); + //点集和边集清零 + edge_num = 0; + point_num = 0; + add_point(0, 5); + add_point(10, 5); + //增加起点和终点 + for (int i = 1; i <= wall_num; i++) + { + double x, y1, y2, y3, y4; + cin >> x >> y1 >> y2 >> y3 >> y4; + //输入æ¯ä¸ªå¢™ï¼Œå¹¶æ·»åŠ å¢™æ‰€å¯¹åº”çš„è¾¹ + add_edge(x, 0, x, y1); + add_edge(x, y2, x, y3); + add_edge(x, y4, x, 10); + //添加墙所对应的新增顶点 + add_point(x, y1); + add_point(x, y2); + add_point(x, y3); + add_point(x, y4); + } + for (int i = 0; i < point_num; i++) + for (int j = 0; j < point_num; j++)//枚举任æ„两个点 + if (i != j) //如果他们ä¸æ˜¯åŒä¸€ä¸ªç‚¹ + { + bool link = true; + for (int k = 0; k < edge_num; k++) + { + point_node lv, lv2; + lv.x = edge[k].x1; + lv.y = edge[k].y1; + lv2.x = edge[k].x2; + lv2.y = edge[k].y2; + if (segment_cross_simple(point[i], point[j], lv, lv2)) + { + link = false; + } + } + if (link) + point_dist[i][j] = sqrt(pow(point[i].x - point[j].x, 2) + + pow(point[i].y - point[j].y, 2)); + else + { + point_dist[i][j] = maxdist; + } + } + //对于任æ„两个顶点求他们之间的路径 + dijkstra(); + //求出æºç‚¹å¼€å§‹çš„dijkstra + cout << setiosflags(ios::fixed) + << setprecision(2) + << dist[1] << endl; +} + +/* + *主过程 + */ +int main() +{ + cin >> wall_num; + while (wall_num != -1) + { + solve(); + cin >> wall_num; + } +} diff --git a/394.cpp b/394.cpp new file mode 100644 index 0000000..4f81d32 --- /dev/null +++ b/394.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +struct arr +{ + int base_address, arr_elem_sz, d, *c, c0; + vector> bounds; + friend istream &operator>>(istream &istr, arr &arr); +}; + +istream &operator>>(istream &istr, arr &arr) +{ + int a, b; + istr >> arr.base_address >> arr.arr_elem_sz >> arr.d; + for (int i = 0; i < arr.d; i++) + { + istr >> a >> b; + arr.bounds.push_back(make_pair(a, b)); + } + arr.c = new int[arr.d]; + arr.c[arr.d - 1] = arr.arr_elem_sz; + for (int i = arr.d - 2; i >= 0; i--) + { + arr.c[i] = arr.c[i + 1] * (arr.bounds[i + 1].second - arr.bounds[i + 1].first + 1); + } + arr.c0 = arr.base_address; + for (int i = 0; i < arr.d; i++) + { + arr.c0 -= arr.c[i] * arr.bounds[i].first; + } +} + +int main() +{ + int n, r, index, counter; + string name; + map m; + cin >> n >> r; + while (n--) + { + arr tmp; + cin >> name; + cin >> tmp; + m[name] = tmp; + } + while (r--) + { + counter = 0; + cin >> name; + cout << name << "["; + int result = m[name].c0; + for (int i = 0; i < m[name].d; i++) + { + cin >> index; + if (counter > 0) + { + printf(", "); + } + cout << index; + result += m[name].c[i] * index; + counter = 1; + } + cout << "] = " << result << endl; + } + return 0; +} diff --git a/395.cpp b/395.cpp new file mode 100644 index 0000000..3b825f6 --- /dev/null +++ b/395.cpp @@ -0,0 +1,392 @@ +#include + +using namespace std; + +/* +395 +Board Silly +*/ +char R[] = "ABCDEFGH"; +char C[] = "12345678"; +char B[10][10], pie, opo; +char M[100][10]; +int rp, cp, dgp1, dgp2, in; +int com(const void *a, const void *b) +{ + return strcmp((char *)a, (char *)b); +} +void ReadCase() +{ + int i; + char temp[2]; + for (i = 1; i < 8; i++) + { + scanf("%s", B[i]); + } + scanf("%s", temp); + pie = temp[0]; + opo = 'O'; + if (pie == 'O') + { + opo = 'X'; + } +} +void Count(int r, int c) +{ + int i, j; + rp = cp = dgp1 = dgp2 = 1; + for (i = r - 1; i >= 0; i--) + if (B[i][c] != '.') + { + rp++; + } + for (i = r + 1; i < 8; i++) + if (B[i][c] != '.') + { + rp++; + } + for (i = c + 1; i < 8; i++) + if (B[r][i] != '.') + { + cp++; + } + for (i = c - 1; i >= 0; i--) + if (B[r][i] != '.') + { + cp++; + } + i = r - 1; + j = c - 1; + while (i >= 0 && j >= 0) + { + if (B[i][j] != '.') + { + dgp2++; + } + i--; + j--; + } + i = r + 1; + j = c + 1; + while (i < 8 && j < 8) + { + if (B[i][j] != '.') + { + dgp2++; + } + i++; + j++; + } + i = r - 1; + j = c + 1; + while (i >= 0 && j < 8) + { + if (B[i][j] != '.') + { + dgp1++; + } + i--; + j++; + } + i = r + 1; + j = c - 1; + while (i < 8 && j >= 0) + { + if (B[i][j] != '.') + { + dgp1++; + } + i++; + j--; + } +} +void u_l_dg(int r, int c) +{ + int tr, tc, i, p, q; + tr = r - dgp2; + tc = c - dgp2; + if (tr < 0 || tc < 0) + { + return; + } + if (B[tr][tc] == pie) + { + return; + } + p = r - 1; + q = c - 1; + for (i = 0; i < dgp2 - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p--; + q--; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void l_r_dg(int r, int c) +{ + int tr, tc, i, p, q; + tr = r + dgp2; + tc = c + dgp2; + if (tr >= 8 || tc >= 8) + { + return; + } + if (B[tr][tc] == pie) + { + return; + } + p = r + 1; + q = c + 1; + for (i = 0; i < dgp2 - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p++; + q++; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void u_r(int r, int c) +{ + int tr, i, p, q; + tr = r - rp; + if (tr < 0) + { + return; + } + if (B[tr][c] == pie) + { + return; + } + p = r - 1; + q = c; + for (i = 0; i < rp - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p--; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[c]; + M[in++][5] = NULL; +} +void l_r(int r, int c) +{ + int tr, i, p, q; + tr = r + rp; + if (tr >= 8) + { + return; + } + if (B[tr][c] == pie) + { + return; + } + p = r + 1; + q = c; + for (i = 0; i < rp - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p++; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[c]; + M[in++][5] = NULL; +} +void r_c(int r, int c) +{ + int tc, i, p; + tc = c + cp; + if (tc >= 8) + { + return; + } + if (B[r][tc] == pie) + { + return; + } + p = c + 1; + for (i = 0; i < cp - 1; i++) + { + if (B[r][p] == opo) + { + return; + } + p++; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[r]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void l_c(int r, int c) +{ + int tc, i, p; + tc = c - cp; + if (tc < 0) + { + return; + } + if (B[r][tc] == pie) + { + return; + } + p = c - 1; + for (i = 0; i < cp - 1; i++) + { + if (B[r][p] == opo) + { + return; + } + p--; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[r]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void u_r_dg(int r, int c) +{ + int tr, tc, i, p, q; + tr = r - dgp1; + tc = c + dgp1; + if (tr < 0 || tc >= 8) + { + return; + } + if (B[tr][tc] == pie) + { + return; + } + p = r - 1; + q = c + 1; + for (i = 0; i < dgp1 - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p--; + q++; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void l_l_dg(int r, int c) +{ + int tr, tc, i, p, q; + tr = r + dgp1; + tc = c - dgp1; + if (tr >= 8 || tc < 0) + { + return; + } + if (B[tr][tc] == pie) + { + return; + } + p = r + 1; + q = c - 1; + for (i = 0; i < dgp1 - 1; i++) + { + if (B[p][q] == opo) + { + return; + } + p++; + q--; + } + M[in][0] = R[r]; + M[in][1] = C[c]; + M[in][2] = '-'; + M[in][3] = R[tr]; + M[in][4] = C[tc]; + M[in++][5] = NULL; +} +void Print() +{ + int i; + if (!in) + { + printf("No moves are possible\n"); + return; + } + qsort(M, in, sizeof(M[0]), com); + for (i = 0; i < in; i++) + { + printf("%s\n", M[i]); + } +} +void Cal() +{ + int i, j; + in = 0; + for (i = 0; i < 8; i++) + { + for (j = 0; j < 8; j++) + { + if (B[i][j] == pie) + { + Count(i, j); + u_l_dg(i, j); + u_r(i, j); + u_r_dg(i, j); + r_c(i, j); + l_r_dg(i, j); + l_r(i, j); + l_l_dg(i, j); + l_c(i, j); + } + } + } + Print(); +} +int main() +{ + int f = 0; + while (scanf("%s", B[0]) != EOF) + { + if (f++) + { + putchar('\n'); + } + ReadCase(); + Cal(); + } + return 0; +} diff --git a/397.cpp b/397.cpp new file mode 100644 index 0000000..3878d8a --- /dev/null +++ b/397.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +char input[400], Vary[10], op; +struct ss +{ + int opd; + char opt; +} Eq[25]; + +void GetOperand(char dummy[], int len) +{ + int i = 0, j = 0, k = 0; + char xx[15]; + op = 0; + for (j = 0; !isdigit(dummy[j]); j++) + ; + while (j < len) + { + for (i = j; i < len && isdigit(dummy[i]); i++) + xx[k++] = dummy[i]; + xx[k] = NULL; + Eq[op++].opd = atoi(xx); + k = 0; + for (j = i; j < len && !isdigit(dummy[j]); j++) + ; + } +} +void GetOperator(char dummy[], int len) +{ + int i, j = 1, k = 0; + if (!isdigit(dummy[0])) + if (dummy[0] == '-') + Eq[0].opd *= -1; + for (i = 1; i < len; i++) + if (!isdigit(dummy[i])) + if (isdigit(dummy[i - 1])) + Eq[k++].opt = dummy[i]; + else if (dummy[i] == '-') + Eq[k].opd *= -1; +} +void Sep() +{ + char temp[400]; + int i, len = 0, k = 0; + for (i = 0; input[i]; i++) + if (input[i] != ' ') + temp[len++] = input[i]; + temp[len] = NULL; + for (i = len - 1; temp[i] != '='; i--) + ; + for (i = i + 1; temp[i]; i++) + Vary[k++] = temp[i]; + Vary[k] = NULL; + GetOperand(temp, len); + GetOperator(temp, len); +} +void Print() +{ + int i; + if (op == 1) + { + printf("%d = %s\n", Eq[0].opd, Vary); + return; + } + printf("%d ", Eq[0].opd); + for (i = 0; i + 1 < op; i++) + printf("%c %d ", Eq[i].opt, Eq[i + 1].opd); + printf("= %s\n", Vary); +} +int DivMult() +{ + int i, j; + for (i = 0; i + 1 < op; i++) + { + if (Eq[i].opt == '*' || Eq[i].opt == '/') + { + if (Eq[i].opt == '*') + Eq[i].opd = Eq[i].opd * Eq[i + 1].opd; + else + Eq[i].opd = Eq[i].opd / Eq[i + 1].opd; + for (j = i + 1; j + 1 < op; j++) + Eq[j].opd = Eq[j + 1].opd; + for (j = i; j + 2 < op; j++) + Eq[j].opt = Eq[j + 1].opt; + op--; + Print(); + return 1; + } + } + return 0; +} +int AddSub() +{ + int i, j; + for (i = 0; i + 1 < op; i++) + { + if (Eq[i].opt == '+' || Eq[i].opt == '-') + { + if (Eq[i].opt == '+') + Eq[i].opd = Eq[i].opd + Eq[i + 1].opd; + else + Eq[i].opd = Eq[i].opd - Eq[i + 1].opd; + for (j = i + 1; j + 1 < op; j++) + Eq[j].opd = Eq[j + 1].opd; + for (j = i; j + 2 < op; j++) + Eq[j].opt = Eq[j + 1].opt; + op--; + Print(); + return 1; + } + } + return 0; +} +void Cal() +{ + int d; + Print(); + while (op > 1) + { + d = DivMult(); + if (!d) + AddSub(); + } +} + +int main() +{ + int k = 0; + while (gets(input)) + { + if (k++) + { + putchar('\n'); + } + Sep(); + Cal(); + } + return 0; +} diff --git a/398.cpp b/398.cpp new file mode 100644 index 0000000..9ca1cb2 --- /dev/null +++ b/398.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +static int a[32][32], id[256], n; +static char name[32]; + +inline void solve() +{ + int i, j, k; + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (id[a[i][j]] >= 0) + { + a[i][j] = id[a[i][j]]; + continue; + } + printf("NOT A SEMIGROUP: %c#%c = %c WHICH IS NOT AN ELEMENT OF THE SET\n", + name[i], name[j], a[i][j]); + return; + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + for (k = 0; k < n; k++) + { + if (a[a[i][j]][k] == a[i][a[j][k]]) + { + continue; + } + printf("NOT A SEMIGROUP: (%c#%c)#%c IS NOT EQUAL TO %c#(%c#%c)\n", + name[i], name[j], name[k], + name[i], name[j], name[k]); + return; + } + } + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + if (a[i][j] == a[j][i]) + { + continue; + } + printf("SEMIGROUP BUT NOT COMMUTATIVE (%c#%c IS NOT EQUAL TO %c#%c)\n", + name[i], name[j], name[j], name[i]); + return; + } + } + printf("COMMUTATIVE SEMIGROUP\n"); +} + +int main() +{ + bool first = true; + scanf("%d", &n); + int k; + while (n > 0) + { + if (!first) + { + scanf("%d", &n); + if (n != 0) + { + printf("\n\n"); + } + else + { + printf("\n"); + break; + } + } + else + { + first = false; + } + for (int i = 0; i < n && (k = getchar()) != EOF;) + if (isalpha(k)) + { + name[i++] = k; + } + name[n] = '\0'; + for (int i = 0; i < 256; i++) + { + id[i] = -1; + } + for (int i = 0; i < n; i++) + { + id[(int)name[i]] = i; + } + for (int i = 0; i < n; i++) + { + a[i][n] = 0; + for (int j = 0; j < n && (k = getchar()) != EOF;) + if (isalpha(k)) + { + a[i][j++] = k; + } + } + printf("S = {%c", name[0]); + for (int i = 1; i < n; i++) + { + printf(",%c", name[i]); + } + printf("}\n #|%s\n -+", name); + for (int i = 0; i < n; i++) + { + putchar('-'); + } + printf("\n"); + for (int i = 0; i < n; i++) + { + printf(" %c|", name[i]); + for (int j = 0; j < n; j++) + { + putchar(a[i][j]); + } + putchar('\n'); + } + printf("\n"); + solve(); + printf("------------------------------"); + } + return 0; +} diff --git a/399.cpp b/399.cpp new file mode 100644 index 0000000..4f564a0 --- /dev/null +++ b/399.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +typedef struct +{ + char used; + short key[4]; + char data[25][25]; +} PIECE; + +PIECE piece[100]; +int n_piece, dim; +char table[10][10]; + +int Recur(int n) +{ + int i, y, x; + short mask[4] = {-10, -10, -10, -10}; + if (n == -1) + { + return 0; + } + y = n / dim, x = n % dim; + if (x == 0) + { + mask[1] = 0;//left + } + else if (x == dim - 1) + { + mask[3] = 0;//right + } + else + { + mask[3] = -(piece[(int)table[y][x + 1]].key[1]);//left of right + } + if (y == 0) + { + mask[0] = 0;//up + } + else if (y == dim - 1) + { + mask[2] = 0;//down + } + else + { + mask[2] = -(piece[(int)table[y + 1][x]].key[0]);//up of down + } + for (i = 0; i < n_piece; i++) + { + if (piece[i].used) + { + continue; + } + if ((mask[0] != -10 && mask[0] != piece[i].key[0]) || + (mask[1] != -10 && mask[1] != piece[i].key[1]) || + (mask[2] != -10 && mask[2] != piece[i].key[2]) || + (mask[3] != -10 && mask[3] != piece[i].key[3])) + { + continue; + } + piece[i].used = 1; + table[y][x] = i; + if (Recur(n - 1) == 0) + { + return 0; + } + piece[i].used = 0; + } + return -1; +} + +void MyGets(char *s) +{ + do + if (gets(s) == 0) + { + exit(0); + } + while (*s == 0); +} + +int main() +{ + int n; + char line[128]; + gets(line); + n = atoi(line); + while (--n >= 0) + { + int i, j, wid, hei; + memset(table, 0x77, sizeof(char)); + MyGets(line); + sscanf(line, "%d %d %d", &dim, &hei, &wid); + n_piece = dim * dim; + for (i = n_piece; --i >= 0;) + { + for (j = 0; j < hei; j++) + { + MyGets(line); + memset(piece[i].data[j], ' ', wid); + memcpy(piece[i].data[j], line, strlen(line)); + } + MyGets(line); + sscanf(line, "%hd %hd %hd %hd", + &piece[i].key[0], &piece[i].key[1], + &piece[i].key[2], &piece[i].key[3]); + piece[i].used = 0; + } + Recur(n_piece - 1); + for (i = 0; i < dim; i++) + { + int k, a; + for (j = 0; j < hei; j++, printf("\n")) + for (k = 0; k < dim; k++) + for (a = 0; a < wid; a++) + { + printf("%c", piece[(int)table[i][k]].data[j][a]); + } + } + if (n) + { + printf("\n"); + } + } + return 0; +} diff --git a/400.cpp b/400.cpp new file mode 100644 index 0000000..c1fbce7 --- /dev/null +++ b/400.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +int main() +{ + int n; + int rows; + int columns; + int longest = 0; + string file; + vector files; + while (cin >> n) + { + longest = 0; + files.clear(); + for (int i = 0; i < n; i++) + { + cin >> file; + files.push_back(file); + if (file.size() > longest) + { + longest = file.size(); + } + } + sort(files.begin(), files.end()); + columns = 60 / longest; + while (longest * columns + 2 * (columns - 1) > 60) + { + columns--; + } + rows = files.size() / columns; + if (rows * columns < files.size()) + { + rows++; + } + for (int i = 0; i < 60; i++) + { + cout << "-"; + } + cout << endl; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < columns && j * rows + i < files.size(); j++) + { + cout << files[j * rows + i]; + for (int k = 0; k < longest - files[j * rows + i].size(); k++) + { + cout << " "; + } + if (j < columns - 1) + { + cout << " "; + } + } + cout << endl; + } + } + return 0; +} diff --git a/401.cpp b/401.cpp index 424e7f4..012d0e2 100644 --- a/401.cpp +++ b/401.cpp @@ -1,86 +1,68 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - string orig; - while(cin>>orig){ - bool pal=false, mir=true; - string dup=orig ,mirr=""; - reverse(dup.begin(), dup.end()); - if(dup == orig) - pal=true; - int len=orig.length(), i; - map palin; - map ::iterator it; - palin['A']='A'; - palin['E']='3'; - palin['H']='H'; - palin['I']='I'; - palin['J']='L'; - palin['L']='J'; - palin['M']='M'; - palin['O']='O'; - palin['S']='2'; - palin['T']='T'; - palin['U']='U'; - palin['V']='V'; - palin['W']='W'; - palin['X']='X'; - palin['Y']='Y'; - palin['Z']='5'; - palin['1']='1'; - palin['2']='S'; - palin['3']='E'; - palin['5']='Z'; - palin['8']='8'; - for(i=0; isecond; - } - if(mirr != orig) - mir = false; - - if(pal && mir) - cout< + +using namespace std; + +char mirror1[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; +char mirror2[50] = "A***3**HIL*JM*O***2TUVWXY51SE*Z**8*"; + +bool ispalindrome(char *p) +{ + int i, j, length = (int)strlen(p); + for (i = 0, j = length - 1; p[i] != '\0'; i++, j--) + if (p[i] != p[j]) + { + return false; + } + return true; +} + +bool ismirrored(char *p) +{ + int i, j, length = (int)strlen(p); + for (i = 0, j = length - 1; p[i] != '\0'; i++, j--) + { + if (isupper(p[i])) + if (mirror2[p[i] - 'A'] != p[j]) + { + return false; + } + else if (isdigit(p[i])) + if (mirror2[p[i] - '1' + 26] != p[j]) + { + return false; + } + } + return true; +} + +int main() +{ + char p[100]; + while (cin >> p) + { + bool ispal, ismirr; + ispal = ispalindrome(p); + ismirr = ismirrored(p); + if (ispal && ismirr) + { + cout << p << " -- is a mirrored palindrome.\n" + << endl; + } + else if (ispal && !ismirr) + { + cout << p << " -- is a regular palindrome.\n" + << endl; + } + else if (!ispal && ismirr) + { + cout << p << " -- is a mirrored string.\n" + << endl; + } + else + { + cout << p << " -- is not a palindrome.\n" + << endl; + } + } + return 0; +} diff --git a/402.cpp b/402.cpp new file mode 100644 index 0000000..5cacafa --- /dev/null +++ b/402.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +//HKUST - ZHANG Xiongqi +//Using C++ + +vector people; +int main() +{ + int count = 1; + int num; + int luck; + int N; + while (scanf("%d", &num) == 1) + { + people.clear(); + scanf("%d", &luck); + for (int i = 1; i <= num; i++) + { + people.push_back(i); + } + bool fin = false; + if (num == luck) + { + fin = true; + } + for (int i = 0; i < 20; i++) + { + scanf("%d", &N); + if (fin) + { + continue; + } + int loc = 0; + while (loc < people.size() && !fin) + { + loc += N - 1; + if (loc < people.size()) + { + people.erase(people.begin() + loc); + } + if (int(people.size()) == luck) + { + fin = true; + } + } + } + printf("Selection #%d\n", count); + count++; + printf("%d", people[0]); + for (int i = 1; i < people.size(); i++) + { + printf(" %d", people[i]); + } + putchar(10); + putchar(10); + } + return 0; +} diff --git a/403.cpp b/403.cpp new file mode 100644 index 0000000..d4af56c --- /dev/null +++ b/403.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +char c5[26][31] = + { + ".***..*...*.*****.*...*.*...*.", + "****..*...*.****..*...*.****..", + ".****.*...*.*.....*......****.", + "****..*...*.*...*.*...*.****..", + "*****.*.....***...*.....*****.", + "*****.*.....***...*.....*.....", + ".****.*.....*..**.*...*..***..", + "*...*.*...*.*****.*...*.*...*.", + "*****...*.....*.....*...*****.", + "..***....*.....*..*..*...**...", + "*...*.*..*..***...*..*..*...*.", + "*.....*.....*.....*.....*****.", + "*...*.**.**.*.*.*.*...*.*...*.", + "*...*.**..*.*.*.*.*..**.*...*.", + ".***..*...*.*...*.*...*..***..", + "****..*...*.****..*.....*.....", + ".***..*...*.*...*.*..**..****.", + "****..*...*.****..*..*..*...*.", + ".****.*......***......*.****..", + "*****.*.*.*...*.....*....***..", + "*...*.*...*.*...*.*...*..***..", + "*...*.*...*..*.*...*.*....*...", + "*...*.*...*.*.*.*.**.**.*...*.", + "*...*..*.*....*....*.*..*...*.", + "*...*..*.*....*.....*.....*...", + "*****....*....*....*....*****."}; + +char page[60][60]; + +void blank() +{ + int i, j; + for (i = 0; i < 60; i++) + for (j = 0; j < 60; j++) + { + page[i][j] = '.'; + } +} + +void dump_page() +{ + int i, j; + /* printf(" "); + for (i = 1; i <= 60; i++) + printf("%d", i % 10); + printf("\n");*/ + for (i = 0; i < 60; i++) + { + // printf("%2d: ", i + 1); /* DEBUG: print the line number */ + for (j = 0; j < 60; j++) + { + fprintf(stdout, "%c", page[i][j]); + } + fprintf(stdout, "\n"); + } + fprintf(stdout, "\n"); + for (i = 0; i < 60; i++) + { + fprintf(stdout, "-"); + } + fprintf(stdout, "\n\n"); +} + +void print(int row, int column, int size, int align, char *text) +{ + int left, right, letter, i, k; + char c; + /* calculate the positions */ + if (!align || align == 1) + { + left = column - 1; + if (size == 1) + { + right = left + strlen(text) - 1; + } + else + { + right = left + strlen(text) * 6 - 1; + } + } + else if (align == 2) + { + right = column - 1; + if (size == 1) + { + left = right - strlen(text) + 1; + } + else + { + left = right - strlen(text) * 6 + 1; + } + } + else if (align == 3) + { + if (size == 1) + { + left = 29 - strlen(text) / 2 + strlen(text) % 2; + right = left + strlen(text) - 1; + } + else + { + left = 30 - strlen(text) * 3; + right = left + strlen(text) * 6 - 1; + } + } + /* the actual printing */ + if (size == 1) + { + for (i = left; i <= right; i++) + { + if (i < 0 || i > 59) + { + continue; + } + if (text[abs(i - left)] != ' ') + { + page[row - 1][i] = text[abs(i - left)]; + } + } + } + else + { + for (k = 0; k < 5; k++) + { + for (i = left; i <= right; i++) + { + if (i < 0 || i > 59) + { + continue; + } + letter = abs(i - left) / 6; + if (toupper(text[letter]) - 'A' < 26 && + toupper(text[letter]) - 'A' >= 0) + { + c = c5[toupper(text[letter]) - 'A'][k * 6 + abs(i - left) % 6]; + } + else + { + c = '.'; + } + if (row + k - 1 < 60) + if (c != '.') + { + page[row + k - 1][i] = c; + } + } + } + } +} + +int main() +{ + blank(); + char cmd[5]; + while (scanf("%s", cmd) == 1) + { + char font[3], text[1024], tmp[1024]; + int row, column, fsize, i, align; + if (strcmp(cmd, ".EOP")) + { + row = column = 1; + scanf("%s", font); + scanf("%d", &row); + align = 0; + if (!strcmp(cmd, ".P")) + { + scanf("%d", &column); + } + else if (!strcmp(cmd, ".L")) + { + column = 1; + align = 1; /* left alignment */ + } + else if (!strcmp(cmd, ".R")) + { + column = 60; + align = 2; /* right alignment */ + } + else if (!strcmp(cmd, ".C")) + { + align = 3; + } + fgets(tmp, 1024, stdin); + tmp[strlen(tmp) - 1] = '\0'; + /* get rid of the delimiters */ + strcpy(text, tmp + 2); + for (i = strlen(text) - 1; i >= 0; i--) + if (text[i] == '|') + { + text[i] = '\0'; + break; + } + if (!strcmp(font, "C5")) + { + fsize = 5; + } + else + { + fsize = 1; + } + print(row, column, fsize, align, text); + } + else + { + dump_page(); + blank(); + } + } + return 0; +} diff --git a/405.cpp b/405.cpp index 8fd2299..00f1e8f 100644 --- a/405.cpp +++ b/405.cpp @@ -1,28 +1,99 @@ -#include -using namespace std; -int main(){ - int test; - cin>>test; - while(test--){ - string str; - cin>>str; - int len=str.length(),period=0,allStr[150]={0},i=0; - for(i=0;i0) - min=(min0 && min!=0) - period+=(allStr[i]/min); - } - cout<0) - cout< + +using namespace std; + +typedef struct _routing_table +{ + string mta; + string country; + string admd; + string prmd; + string organization; +} routing_table; + +map visited; + +pair send_message(routing_table &msg, map> &mta) +{ + visited[msg.mta] = true; + for (int i = 0, sz = mta[msg.mta].size(); i < sz; i++) + { + bool ok = true; + if (mta[msg.mta][i].country != "*" && mta[msg.mta][i].country != msg.country) + { + ok = false; + } + if (mta[msg.mta][i].admd != "*" && mta[msg.mta][i].admd != msg.admd) + { + ok = false; + } + if (mta[msg.mta][i].prmd != "*" && mta[msg.mta][i].prmd != msg.prmd) + { + ok = false; + } + if (mta[msg.mta][i].organization != "*" && mta[msg.mta][i].organization != msg.organization) + { + ok = false; + } + if (ok) + { + if (mta[msg.mta][i].mta == msg.mta) + { + return make_pair(0, msg.mta); + } + else if (visited[mta[msg.mta][i].mta]) + { + return make_pair(-2, mta[msg.mta][i].mta); + } + else + { + msg.mta = mta[msg.mta][i].mta; + return send_message(msg, mta); + } + } + } + return make_pair(-1, msg.mta); +} + +int main() +{ + int m, n, case_num = 0; + string name; + routing_table tmp; + while (cin >> m) + { + case_num++; + map> mta; + while (m--) + { + cin >> name >> n; + while (n--) + { + cin >> tmp.mta >> tmp.country >> tmp.admd >> tmp.prmd >> tmp.organization; + mta[name].push_back(tmp); + } + } + cin >> n; + cout << "Scenario # " << case_num << endl; + for (int i = 0; i < n; i++) + { + cin >> tmp.mta >> tmp.country >> tmp.admd >> tmp.prmd >> tmp.organization; + visited.clear(); + pair result = send_message(tmp, mta); + switch (result.first) + { + case 0: + cout << i + 1 << " -- delivered to " << result.second << endl; + break; + case -1: + cout << i + 1 << " -- unable to route at " << result.second << endl; + break; + case -2: + cout << i + 1 << " -- circular routing detected by " << result.second << endl; + break; + } + } + cout << endl; + } + return 0; +} diff --git a/406.cpp b/406.cpp index bd7111f..319b677 100644 --- a/406.cpp +++ b/406.cpp @@ -1,69 +1,70 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define MAX 1010 -bool prime[MAX]; -vector< int > P; +using namespace std; -void sieve() { - memset(prime, true, sizeof prime); - prime[1] = prime[0] = false; - P.push_back(1); - for (int i = 2; i < MAX; i++) { - if (prime[i]) { - P.push_back(i); - for (int j = i * i; j < MAX; j += i) - prime[j] = false; +bool isPrime(int n) +{ + for (int i = 3; i <= sqrt((double)n); i += 2) + { + if (n % i == 0) + { + return false; } } - //printf("%d\n", P.size()); + return true; } - -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - sieve(); - - int N, C; - while (scanf("%d%d", &N, &C) != EOF) { - printf("%d %d:", N, C); - vector< int > V; - for (int i = 0; i < P.size() && P[i] <= N; i++) - V.push_back(P[i]); - - C *= 2; - if (V.size() & 1) C -= 1; - int A = floor(C / 2.0); - int B = ceil(C / 2.0); - - for (int i = max(0, (int) V.size() / 2 - A); i < min((int)V.size(), (int)V.size() / 2 + B); i++) - printf(" %d", V[i]); - printf("\n\n"); +int main() +{ + vector primes; + int n, c; + primes.push_back(1); + primes.push_back(2); + for (int i = 3; i < 1000; i += 2) + { + if (isPrime(i)) + { + primes.push_back(i); + } + } + while (cin >> n >> c) + { + deque p; + for (int i = 0; i < primes.size(); i++) + { + if (primes[i] > n) + { + break; + } + else + { + p.push_back(primes[i]); + } + } + if (p.size() % 2 == 0) + { + while (p.size() > c * 2) + { + p.pop_front(); + p.pop_back(); + } + } + else + { + while (p.size() > (c * 2 - 1)) + { + p.pop_front(); + p.pop_back(); + } + } + cout << n << " " << c << ":"; + while (!p.empty()) + { + cout << " " << p.front(); + p.pop_front(); + } + cout << endl + << endl; } return 0; } diff --git a/407.cpp b/407.cpp new file mode 100644 index 0000000..347bb96 --- /dev/null +++ b/407.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +int n; +int used[25]; +int gx[25], gy[25], gi[25], go[25]; +int imap[25][25], omap[25][25]; +double gp[25]; + +int getdist(int x, int y) +{ + return x * x + y * y; +} + +double ab(double now) +{ + if (now < 0) + { + return -now; + } + return now; +} + +int zero(double now) +{ + return now < 1e-8; +} + +int dfs(int now) +{ + int i; + double speed; + /*printf("GP %d: %.2lf\n", now, gp[now]);*/ + for (i = 0; i <= n; i++) + { + if (imap[now][i]) + { + speed = -((gp[now] * gi[now]) / gi[i]); + /*printf("imap %d => %d: %.3lf\n", now, i, speed);*/ + if (!used[i]) + { + used[i] = 1; + gp[i] = speed; + if (dfs(i)) + { + return 1; + } + } + else + { + if (!zero(ab(gp[i] - speed))) + { + return 1; + } + } + } + if (omap[now][i]) + { + speed = -((gp[now] * go[now]) / go[i]); + /*printf("omap %d => %d: %.3lf\n", now, i, speed);*/ + if (!used[i]) + { + used[i] = 1; + gp[i] = speed; + if (dfs(i)) + { + return 1; + } + } + else + { + if (!zero(ab(gp[i] - speed))) + { + return 1; + } + } + } + } + return 0; +} + +int main() +{ + int cas, i, j, f, dist, temp; + cas = 0; + while (scanf("%d%d%d%d%lf%d", &gx[0], &gy[0], &gi[0], &go[0], &gp[0], &n) == 6) + { + memset(imap, 0, sizeof(imap)); + memset(omap, 0, sizeof(omap)); + for (i = 1, f = 0; i <= n; i++) + { + scanf("%d%d%d%d", &gx[i], &gy[i], &gi[i], &go[i]); + gp[i] = 0.0; + if (!f) + { + for (j = 0; j < i; j++) + { + dist = getdist(gx[i] - gx[j], gy[i] - gy[j]); + temp = go[i] + go[j]; + temp *= temp; + if (dist == temp) + { + omap[i][j] = omap[j][i] = 1; + } + else if (dist < temp) + { + f = 1; + break; + } + temp = gi[i] + gi[j]; + temp *= temp; + if (dist == temp) + { + imap[i][j] = imap[j][i] = 1; + } + else if (dist < temp) + { + f = 1; + break; + } + } + } + } + printf("Simulation #%d\n", ++cas); + if (f) + { + printf("Error -- Overlapping Gears\n\n"); + } + else + { + memset(used, 0, sizeof(used)); + used[0] = 1; + if (dfs(0)) + { + printf("Error -- Conflicting Gear Rotation\n\n"); + } + else + { + for (i = 1; i <= n; i++) + { + if (zero(ab(gp[i]))) + { + printf("%2d: Warning -- Idle Gear\n", i); + } + else if (gp[i] > 0) + { + printf("%2d: R %.2lf\n", i, gp[i]); + } + else + { + printf("%2d: L %.2lf\n", i, -gp[i]); + } + } + printf("\n"); + } + } + } + return 0; +} diff --git a/408.cpp b/408.cpp index c75e96a..0fa3d0d 100644 --- a/408.cpp +++ b/408.cpp @@ -1,54 +1,31 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -#define INF INT_MAX -#define EPS 1e-10 - -int64 GCD(int64 A, int64 B){ - if(B > A) - return GCD(B, A); - return B==0?A:GCD(B, A%B); -} - -int main(){ - int64 step, mod; - while(cin>>step>>mod){ - if(GCD(step, mod) == 1) - printf("%10lld%10lld Good Choice\n\n", step, mod); - else - printf("%10lld%10lld Bad Choice\n\n", step, mod); - } - return 0; -} +#include + +using namespace std; + +int gcd(int a, int b) +{ + while (b) + { + a %= b; + swap(a, b); + } + return a; +} + +int main() +{ + int step, mod; + while (cin >> step >> mod) + { + printf("%10d%10d", step, mod); + if (gcd(step, mod) == 1) + { + printf(" Good Choice\n\n"); + } + else + { + printf(" Bad Choice\n\n"); + } + } + return 0; +} diff --git a/409.cpp b/409.cpp index 111189b..56e710d 100644 --- a/409.cpp +++ b/409.cpp @@ -1,41 +1,104 @@ -#include -#include -#include -#include -#include +#include + using namespace std; -int main() { - int K, E, t = 1; - string str; - while (scanf("%d %d\n", &K, &E) != EOF) { - set< string > keyword; - string excuse[E]; - int score[E], Max = 0; - while (K--) { - getline(cin, str); - keyword.insert(str); +char sen[22][72], w[72][70], word[22][22]; +int c, wo, s; + +void word_cnt(char a[]) +{ + int i, j, k, len; + c = 0; + len = strlen(a); + for (i = 0; a[i];) + { + if (isalpha(a[i])) + { + k = 0; + for (j = i; isalpha(a[j]) && a[j]; j++) + { + w[c][k++] = tolower(a[j]); + } + w[c][k] = '\0'; + c++; + if (j >= len) + { + break; + } + i = j; + for (i++; !isalpha(a[i]) && a[i]; i++) + ; + } + else + { + i++; } - for (int i = 0; i < E; i++) { - getline(cin, str); - excuse[i] = str; - score[i] = 0; - string temp = ""; - str = " " + str + " "; - for (int j = 0; j < str.length(); j++) { - if (isalpha(str[j])) temp += tolower(str[j]); - else { - if (keyword.find(temp) != keyword.end()) ++score[i]; - temp = ""; - } + } +} + +int com() +{ + int i, cnt, j; + cnt = 0; + for (i = 0; i < c; i++) + { + for (j = 0; j < wo; j++) + { + if (!strcmp(w[i], word[j])) + { + cnt++; } - Max = max(Max, score[i]); } - cout << "Excuse Set #" << t++ << endl; - for (int i = 0; i < E; i++) { - if (score[i] == Max) cout << excuse[i] << endl; + } + return cnt; +} + +int calc() +{ + int i, j, k, f[30], p[30], m; + int max = 0; + k = 0; + for (i = 1; i <= s; i++) + { + word_cnt(sen[i]); + m = com(); + if (m > max) + { + k = 0; + f[k] = m; + p[k] = i; + max = m; + k++; + } + else if (m >= max) + { + f[k] = m; + p[k++] = i; + } + } + for (i = 0; i < k; i++) + { + printf("%s\n", sen[p[i]]); + } + printf("\n"); + return 0; +} + +int main() +{ + int i, t = 1; + while (scanf("%d%d", &wo, &s) == 2) + { + for (i = 0; i < wo; i++) + { + scanf("%s", word[i]); + } + for (i = 0; i <= s; i++) + { + gets(sen[i]); } - cout << endl; + printf("Excuse Set #%d\n", t++); + calc(); } return 0; } diff --git a/410.cpp b/410.cpp new file mode 100644 index 0000000..9023d0f --- /dev/null +++ b/410.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +struct ss +{ + int a, b; + double diff; +} sp[70]; +char F[20], fg[20]; +int C, S, sl, rlen, V[12], Record[20], Temp[20]; +double Min, av; + +void Ini(int sum) +{ + int i, j, k; + double diff; + sl = 0; + av = (double)sum / C; + for (i = 0; i < S - 1; i++) + { + for (j = i + 1; j < S; j++) + { + k = V[i] + V[j]; + diff = fabs(k - av); + sp[sl].a = i; + sp[sl].b = j; + sp[sl++].diff = diff; + } + } + for (i = 0; i < S; i++) + { + sp[sl].a = i; + sp[sl].b = -1; + sp[sl++].diff = fabs(V[i] - av); + } +} +void Copy(int level) +{ + int i; + for (i = 0; i <= level; i++) + Record[i] = Temp[i]; + rlen = level; +} +void Recur(int level, double sum, int ind, int total) +{ + int i, t = 0, dif; + double dum; + Temp[level] = ind; + if (sum > Min || level >= C) + return; + if (F[sp[ind].a]) + return; + if (sp[ind].b >= 0) + if (F[sp[ind].b]) + return; + if (total == S) + { + dif = C - level - 1; + dum = dif * av; + if (sum + dum > Min) + return; + Copy(level); + Min = sum + dum; + return; + } + if (sp[ind].a >= 0) + F[sp[ind].a] = 1; + if (sp[ind].b >= 0) + F[sp[ind].b] = 1; + for (i = ind + 1; i < sl; i++) + { + if (sp[i].a >= 0) + t++; + if (sp[i].b >= 0) + t++; + Recur(level + 1, sum + sp[i].diff, i, total + t); + t = 0; + } + F[sp[ind].a] = 0; + if (sp[ind].b >= 0) + F[sp[ind].b] = 0; +} +void Print() +{ + int i, k; + for (i = 0; i <= rlen; i++) + { + k = Record[i]; + printf(" %d:", i); + if (sp[k].a >= 0) + printf(" %d", V[sp[k].a]); + if (sp[k].b >= 0) + printf(" %d", V[sp[k].b]); + printf("\n"); + } + for (i; i < C; i++) + { + printf(" %d:\n", i); + } +} +void Cal() +{ + int i, t = 0; + Min = 1000000; + for (i = 0; i <= sl - S; i++) + { + if (sp[i].a >= 0) + t++; + if (sp[i].b >= 0) + t++; + Recur(0, sp[i].diff, i, t); + t = 0; + } + Print(); + printf("IMBALANCE = %.5lf\n", Min); +} + +int main() +{ + int i, sum, f = 1; + while (scanf("%d%d", &C, &S) == 2) + { + sum = 0; + for (i = 0; i < S; i++) + { + scanf("%d", &V[i]); + sum += V[i]; + } + Ini(sum); + printf("Set #%d\n", f++); + Cal(); + printf("\n"); + } + return 0; +} diff --git a/411.cpp b/411.cpp new file mode 100644 index 0000000..cc82eba --- /dev/null +++ b/411.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +char centipede[10]; +int board[2][30][30], curr, nxt, num_cent; + +void cp_collides() +{ + for (int x = 0; x < 30; x++) + { + for (int y = 0; y < 30; y++) + { + if (board[curr][x][y] == 255) + { + board[nxt][x][y] = 255; + } + } + } +} + +bool read_sim() +{ + int i, x, y, len; + curr = 0; + nxt = 1; + memset(&(board[curr][0][0]), -1, sizeof(int) * 30 * 30); + if (scanf("%d\n", &num_cent) != 1) + { + return false; + } + for (i = 0; i < num_cent; i++) + { + scanf("%c %d %d %d\n", &(centipede[i]), &len, &x, &y); + for (; len; len--) + { + board[curr][x][y] = i; + switch (centipede[i]) + { + case 'U': + y--; + break; + case 'D': + y++; + break; + case 'L': + x++; + break; + case 'R': + x--; + break; + } + } + } + return true; +} + +void print_sim() +{ + int x, y; + printf(" 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2\n"); + printf(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9\n"); + for (y = 29; y >= 0; y--) + { + printf("%02d", y); + for (x = 0; x < 30; x++) + { + if (board[curr][x][y] == -1) + { + printf(" ."); + } + else + { + printf(" X"); + } + } + printf("\n"); + } + printf("\n"); +} + +void run_sim(void) +{ + int movement, x, tx, y, ty, c; + movement = 1; + while (movement) + { + memset(&(board[nxt][0][0]), -1, sizeof(int) * 30 * 30); + cp_collides(); + movement = 0; + for (c = 0; c < num_cent; c++) + { + for (x = 0; x < 30; x++) + { + for (y = 0; y < 30; y++) + { + if (c == board[curr][x][y]) + { + movement = 1; + if ((board[nxt][x][y] != c) && (board[nxt][x][y] != -1)) + { + board[nxt][x][y] = 255; + } + else + { + tx = x; + ty = y; + switch (centipede[c]) + { + case 'U': + ty++; + break; + case 'D': + ty--; + break; + case 'L': + tx--; + break; + case 'R': + tx++; + break; + } + if ((tx >= 0) && (tx < 30) && (ty >= 0) && (ty < 30)) + { + if (board[nxt][tx][ty] != -1) + { + board[nxt][tx][ty] = 255; + } + else + { + board[nxt][tx][ty] = c; + } + } + } + } + } + } + } + nxt = 1 - nxt; + curr = 1 - curr; + } +} + +int main() +{ + while (read_sim()) + { + run_sim(); + print_sim(); + } + return 0; +} diff --git a/412.cpp b/412.cpp index cc3af43..7992ced 100644 --- a/412.cpp +++ b/412.cpp @@ -1,62 +1,56 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define EPS 1e-9 +int a[60], N, k; + +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} -int GCD(int A, int B){ - if( B == 0 ) - return A; - if( B > A ) - return GCD(B, A); - return GCD(B, A%B); +void cnt() +{ + int i, j; + k = 0; + for (i = 0; i < N - 1; i++) + { + for (j = i + 1; j < N; j++) + { + if (gcd(a[i], a[j]) == 1) + { + k++; + } + } + } +} + +void calc() +{ + int pair; + double pi, x, y; + if (!k) + { + printf("No estimate for this data set.\n"); + return; + } + pair = (N * (N - 1)) / 2; + x = pair; + y = k; + pi = sqrt((x * 6) / y); + printf("%.6lf\n", pi); } -int main(){ - while( true ){ - int N; - cin >> N; - if( N == 0 ) - break; - int vec[N]; - int i, j; - FOI(i, 0, N-1) cin >> vec[i]; - double num = 3.0 * N * (N - 1); - double den = 0.0; - FOI(i, 0, N-1) - FOI(j, i+1, N-1) - if( GCD(vec[i], vec[j]) == 1 ) - den += 1.0; - if( den < EPS || num < EPS ) - printf("No estimate for this data set.\n"); - else - - printf("%.6lf\n", sqrt(num / den)); +int main() +{ + while (scanf("%d", &N) && N) + { + for (int i = 0; i < N; i++) + { + scanf("%d", &a[i]); + } + cnt(); + calc(); } return 0; } diff --git a/413.cpp b/413.cpp new file mode 100644 index 0000000..40f0a49 --- /dev/null +++ b/413.cpp @@ -0,0 +1,122 @@ +#include +#include +#include + +using namespace std; + +class InputCase +{ +public: + int ReadCase(); + void solve(); + int numCase; + char buf[10000]; + int arr[10000]; +}; + +int InputCase::ReadCase() +{ + numCase = 0; + if (cin.getline(buf, 10000)) + { + if (strcmp(buf, "0") == 0) + { + return 0; + } + istrstream input(buf); + int n; + while (input >> n) + { + arr[numCase++] = n; + } + numCase--; + return 1; + } + else + { + return 0; + } +} + +void InputCase::solve() +{ + double TotalUp = 0, TotalDown = 0, Neutral = 0; + double UpTimes = 0, DownTimes = 0; + int i, flag = 0; + for (i = 0; i < numCase - 1; i++) + { + if (arr[i] < arr[i + 1]) + { + TotalUp += 1.0; + TotalUp += Neutral; + Neutral = 0; + if (flag != 1) + { + UpTimes += 1.0; + } + flag = 1; + } + else if (arr[i] == arr[i + 1]) + { + if (flag == 0) + { + Neutral += 1.0; + } + else if (flag == 1) + { + TotalUp += 1.0; + } + else if (flag == -1) + { + TotalDown += 1.0; + } + } + else if (arr[i] > arr[i + 1]) + { + TotalDown += 1.0; + TotalDown += Neutral; + Neutral = 0; + if (flag != -1) + { + DownTimes += 1.0; + } + flag = -1; + } + } + if (flag == 1) + { + TotalUp += Neutral; + Neutral = 0; + } + if (flag == -1) + { + TotalDown += Neutral; + Neutral = 0; + } + if (Neutral) + { + printf("Nr values = %d: 0.000000 0.000000\n", numCase); + } + else if (!Neutral) + { + if (UpTimes == 0) + { + UpTimes = 1; + } + if (DownTimes == 0) + { + DownTimes = 1; + } + printf("Nr values = %d: %.6lf %.6lf\n", numCase, TotalUp / UpTimes, TotalDown / DownTimes); + } +} + +int main() +{ + InputCase test; + while (test.ReadCase()) + { + test.solve(); + } + return 0; +} diff --git a/414.cpp b/414.cpp index 94b28f4..d3d2931 100644 --- a/414.cpp +++ b/414.cpp @@ -1,30 +1,45 @@ -#include -#include -#include +#include + using namespace std; -int main() { +int main() +{ int N; - char image[30]; - while (true) { - scanf("%d\n", &N); - if (!N) + char p[20][30]; + int cnt[20], sum, min; + while (cin >> N) + { + if (N == 0) + { break; - int totBlank = 0, minBlank = 50; - for (int n = 0; n < N; n++) { - gets(image); - //puts(image); - //scanf("%[A-Z ][^\n]s", image); - //printf("%s\n", image); - int blank = 0; - for (int i = 0; i < 25; i++) { - if (image[i] == ' ') ++blank; + } + int i, j; + for (i = 0; i <= N; i++) + { + cin.getline(p[i], 30); + } + for (i = 0; i <= N; i++) + { + cnt[i] = 0; + } + sum = 0; + min = 10000; + for (i = 1; i <= N; i++) + for (j = 0; j < 25; j++) + if (p[i][j] == ' ') + { + cnt[i]++; + } + for (i = 1; i <= N; i++) + if (cnt[i] < min) + { + min = cnt[i]; } - totBlank = totBlank + blank; - minBlank = min(blank, minBlank); + for (i = 1; i <= N; i++) + { + sum += cnt[i]; } - totBlank -= (N * minBlank); - printf("%d\n", totBlank); + cout << sum - N * min << endl; } return 0; } diff --git a/415.cpp b/415.cpp new file mode 100644 index 0000000..bf1bb98 --- /dev/null +++ b/415.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +float r = 3950; +float R = 432000; +float D = 92900000; +float pi; +float L, theta1, theta2, beta1, beta; +float d; +float Gamma; +float fraction; + +float degree(float a) +{ + return a * 180.0 / pi; +} + +float betaT(float t) +{ + return beta1 - 2 * pi * t / 24.0 / 3600.0; +} + +int main() +{ + float t; + pi = 4 * atan(1); + L = sqrt(D * D + R * R - r * r); + theta1 = atan(R / D); + theta2 = atan(L / r); + beta1 = theta1 + theta2; + /* + printf("pi = %9.4f\n", pi); + printf("L = %9.4f\n", L ); + printf("th1 = %9.4f th2 = %9.4f\n", degree(theta1), degree(theta2) ); + */ + while (1) + { + if (scanf("%f", &t) == EOF) + { + break; + } + beta = betaT(t); + d = (D * cos(beta) - r) / sin(beta); + if (d <= -R) + { + Gamma = -pi / 2; + } + else if ((d > -R) && (d < R)) + { + Gamma = asin(d / R); + } + else + { + Gamma = pi / 2; + } + fraction = 0.5 + Gamma / pi + d * cos(Gamma) / pi / R; + printf("%f\n", fraction); + /* + printf("t = %9.4f betaT = %9.4f d = %9.4f fraction = %9.4f\n", + t, degree ( beta ), d, fraction); + */ + } + return 0; +} diff --git a/416.cpp b/416.cpp new file mode 100644 index 0000000..6cfb1b5 --- /dev/null +++ b/416.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +const char *lit[10] = {"YYYYYYN", "NYYNNNN", "YYNYYNY", "YYYYNNY", + "NYYNNYY", "YNYYNYY", "YNYYYYY", "YYYNNNN", "YYYYYYY", "YYYYNYY"}; +char cd[16][16]; +int main() +{ + for (int n; scanf("%d", &n) == 1 && n;) + { + for (int i = 0; i < n; ++i) + { + scanf("%s", cd[i]); + } + bool match = false; + for (int f = 9; !match && f >= (n - 1); --f) + { + for (int burned = 0; !match && burned < 128; ++burned) + { + int cb = burned; + bool valid = true; + for (int i = 0; valid && i < n; ++i) + for (int j = 0; valid && j < 7; ++j) + { + valid &= cd[i][j] == 'N' || (lit[f - i][j] == 'Y' && !(cb & (1 << j))); + if (cd[i][j] == 'N' && lit[f - i][j] == 'Y') + { + cb |= (1 << j); + } + } + match |= valid; + } + } + puts("MISMATCH" + 3 * match); + } + return 0; +} diff --git a/417.cpp b/417.cpp index e075edf..f2465bb 100644 --- a/417.cpp +++ b/417.cpp @@ -1,46 +1,113 @@ -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 25 +#include -int main(){ - map Map; - string alpha[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; - - int ID = 1; - int a, b, c, d, e; - - FOI(a, 0, MAX) - Map[""+alpha[a]] = ID++; +using namespace std; - FOI(a, 0, MAX) - FOI(b, a+1, MAX) - Map[""+alpha[a]+alpha[b]] = ID++; - - FOI(a, 0, MAX) - FOI(b, a+1, MAX) - FOI(c, b+1, MAX) - Map[""+alpha[a]+alpha[b]+alpha[c]] = ID++; +char cmd[10], hash_[84000][6]; +int curr_idx = 0; - FOI(a, 0, MAX) - FOI(b, a+1, MAX) - FOI(c, b+1, MAX) - FOI(d, c+1, MAX) - Map[""+alpha[a]+alpha[b]+alpha[c]+alpha[d]] = ID++; +void init() +{ + memset(hash_, '\0', sizeof(hash_)); + int i, j, k, m, n; + for (i = 0; i < 26 - 0; i++) + { + hash_[curr_idx][0] = (char)('a' + i); + curr_idx++; + } + for (i = 0; i < 26 - 1; i++) + { + for (j = i + 1; j < 26 - 0; j++) + { + hash_[curr_idx][0] = (char)('a' + i); + hash_[curr_idx][1] = (char)('a' + j); + curr_idx++; + } + } + for (i = 0; i < 26 - 2; i++) + { + for (j = i + 1; j < 26 - 1; j++) + { + for (k = j + 1; k < 26 - 0; k++) + { + hash_[curr_idx][0] = (char)('a' + i); + hash_[curr_idx][1] = (char)('a' + j); + hash_[curr_idx][2] = (char)('a' + k); + curr_idx++; + } + } + } + for (i = 0; i < 26 - 3; i++) + { + for (j = i + 1; j < 26 - 2; j++) + { + for (k = j + 1; k < 26 - 1; k++) + { + for (m = k + 1; m < 26 - 0; m++) + { + hash_[curr_idx][0] = (char)('a' + i); + hash_[curr_idx][1] = (char)('a' + j); + hash_[curr_idx][2] = (char)('a' + k); + hash_[curr_idx][3] = (char)('a' + m); + curr_idx++; + } + } + } + } + for (i = 0; i < 26 - 4; i++) + { + for (j = i + 1; j < 26 - 3; j++) + { + for (k = j + 1; k < 26 - 2; k++) + { + for (m = k + 1; m < 26 - 1; m++) + { + for (n = m + 1; n < 26 - 0; n++) + { + hash_[curr_idx][0] = (char)('a' + i); + hash_[curr_idx][1] = (char)('a' + j); + hash_[curr_idx][2] = (char)('a' + k); + hash_[curr_idx][3] = (char)('a' + m); + hash_[curr_idx][4] = (char)('a' + n); + curr_idx++; + } + } + } + } + } +} - FOI(a, 0, MAX) - FOI(b, a+1, MAX) - FOI(c, b+1, MAX) - FOI(d, c+1, MAX) - FOI(e, d+1, MAX) - Map[""+alpha[a]+alpha[b]+alpha[c]+alpha[d]+alpha[e]] = ID++; +bool isValid() +{ + for (int i = 0; i < strlen(cmd) - 1; i++) + { + if (cmd[i] >= cmd[i + 1]) + { + return false; + } + } + return true; +} - string S; - while( cin >> S ) - cout << Map[S] << endl; +int main() +{ + init(); + while (cin >> cmd) + { + if (isValid()) + { + for (int idx = 0; idx < curr_idx; idx++) + { + if (!strcmp(cmd, hash_[idx])) + { + printf("%d\n", idx+1); + break; + } + } + } + else + { + printf("0\n"); + } + } return 0; } diff --git a/418.cpp b/418.cpp new file mode 100644 index 0000000..7c055bc --- /dev/null +++ b/418.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define LEN 12 +#define RNG 11 + +int SubMolecules(const vector &moles) +{ + int ret = 0; + for (int a = 1; a < RNG; ++a) + { + for (int b = 1; b < RNG; ++b) + { + if (moles[0][a] == moles[1][b]) + { + for (int bc = b + 2; bc < RNG; ++bc) + { + for (int c = 1; c < RNG; ++c) + { + if (moles[1][bc] == moles[2][c]) + { + int da_max = min(RNG - c, RNG - a); + for (int da = 2; da < da_max; ++da) + { + for (int d = 1; d < RNG - (bc - b); ++d) + { + if (moles[0][a + da] == moles[3][d] && + moles[3][d + (bc - b)] == moles[2][(a + da) - (a - c)]) + { + int ad_len = da - 1; + int bc_len = bc - b - 1; + ret = max(ret, bc_len * ad_len); + } + } + } + } + } + } + } + } + } + return ret; +} + +int Molecules(const vector &moles) +{ + int ret = 0; + int a[] = {0, 1, 2, 3}; + ret = max(ret, SubMolecules(moles)); + while (next_permutation(a, a + 4)) + { + vector perm_mole(moles.begin(), moles.end()); + for (int i = 0; i < 4; ++i) + { + perm_mole[i] = moles[a[i]]; + } + ret = max(ret, SubMolecules(perm_mole)); + } + return ret; +} + +int main() +{ + while (true) + { + vector moles; + for (int i = 0; i < 4; ++i) + { + string s; + cin >> s; + moles.push_back(s); + if (moles[i].find('Q') != string::npos) + { + return 0; + } + } + cout << Molecules(moles) << endl; + } + return 0; +} diff --git a/419.cpp b/419.cpp new file mode 100644 index 0000000..f53d5e2 --- /dev/null +++ b/419.cpp @@ -0,0 +1,129 @@ +#include + +using namespace std; + +const char wname[6] = {"MTWRF"}; +int mdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +char buf[2008]; +int hash_[128], schedule[13][32][18][61]; + +int main() +{ + int n, i, m, s, cm, cd, um, ud, sm, ss, em, es, tm, ts, tstep; + char cw, uw; + hash_['M'] = 0; + hash_['T'] = 1; + hash_['W'] = 2; + hash_['R'] = 3; + hash_['F'] = 4; + gets(buf); + sscanf(buf, "%c%d%d", &cw, &cm, &cd); + cw = hash_[cw]; + gets(buf); + sscanf(buf, "%d%d", &n, &tstep); + while (gets(buf)) + { + if (!strcmp(buf, "done")) + { + break; + } + while (gets(buf)) + { + if (!strcmp(buf, "done")) + { + break; + } + sscanf(buf, "%c%d%d%2d%2d%2d%2d", &uw, &um, &ud, &sm, &ss, &em, &es); + for (; sm * 100 + ss < em * 100 + es;) + { + schedule[um][ud][sm][ss] = 1; + /*printf("%d %d %02d%02d\n", um, ud, sm, ss);*/ + ss += 15; + if (ss == 60) + { + ss = 0; + sm++; + } + } + } + } + sm = tstep / 60; + ss = tstep % 60; + for (i = 0; i < 365 && n; i++, cw++) + { + /*printf("%d %d\n", cm, cd);*/ + if (cw == 7) + { + cw = 0; + } + if (cw < 5) + { + for (m = 9, s = 0; m * 100 + s < 1700;) + { + /*printf("%02d%02d\n", m, s);*/ + em = m + sm; + es = s + ss; + if (es >= 60) + { + em += es / 60; + es %= 60; + } + if (em * 100 + es > 1700) + { + break; + } + for (tm = m, ts = s; tm * 100 + ts < em * 100 + es;) + { + /*printf("\t%02d%02d\n", tm, ts);*/ + if (schedule[cm][cd][tm][ts]) + { + break; + } + ts += 15; + if (ts == 60) + { + ts = 0; + tm++; + } + } + if (tm * 100 + ts < em * 100 + es) + { + s += 15; + if (s == 60) + { + s = 0; + m++; + } + } + else + { + printf("%c %d %d %02d%02d\n", wname[cw], cm, cd, m, s); + n--; + if (!n) + { + break; + } + m = tm; + s = ts; + } + } + } + cd++; + if (cd > mdays[cm]) + { + cd = 1; + cm++; + if (cm == 13) + { + cm = 1; + } + } + } + if (n) + { + printf("No more times available\n"); + } + scanf(" "); + return 0; +} diff --git a/420.cpp b/420.cpp new file mode 100644 index 0000000..56756a3 --- /dev/null +++ b/420.cpp @@ -0,0 +1,138 @@ +#include + +using namespace std; + +/*----------------------------------------------------------------------- + * + * File Name: p2.c + * + * Author: A. Lumsdaine + * + *----------------------------------------------------------------------- + * + * DESCRIPTION + * Source code file for problem #2 of 1996 ACM + * East-Central Regional Programming contest. + * + * DIAGNOSTICS + * More trigonometry (three-dimensional) needed here. The main + * difficulty is calculating the volume of each quadrant. The + * quadrant can be divided into three tetrahedrons, each of whose + * volume can be calculated using determinants. + * + *----------------------------------------------------------------------- + */ +typedef double set_of_criteria[100]; +static void +polar_to_rectangular(double r, double theta, double *x, double *y) +{ + *x = r * cos(theta); + *y = r * sin(theta); +} +static double +det2x2(double a, double b, + double c, double d) +{ + return (a * d - b * c); +} +static double +volume_of_tetrahedron(double px, double py, double pz, + double qx, double qy, double qz, + double rx, double ry, double rz) +{ + return (px * det2x2(qy, qz, ry, rz) - py * det2x2(qx, qz, rx, rz) + + pz * det2x2(qx, qy, rx, ry)) / + 6.0; +} +static double +annual_volume(int num_criteria, double previous_year[], double current_year[]) +{ + int i, j; + double sum = 0, ivol; + double px, py, pz, qx, qy, qz, rx, ry, rz; + double theta = 2.0 * M_PI / num_criteria; + /* for each pie slice */ + for (i = 0; i < num_criteria; i++) + { + /* calculate the volume of the slice */ + /* There are three tetrahedra in the slice: abx, abc, bcd */ + /* det a b x -> p q r */ + polar_to_rectangular(previous_year[i], theta * i, &px, &py); + j = (i + 1) % num_criteria; + polar_to_rectangular(previous_year[j], theta * j, &qx, &qy); + pz = qz = rz = 1; + rx = ry = 0; + ivol = volume_of_tetrahedron(px, py, pz, qx, qy, qz, rx, ry, rz); + sum += fabs(ivol); + /* det a b c -> p q r */ + polar_to_rectangular(current_year[i], theta * i, &rx, &ry); + rz = 0; + ivol = volume_of_tetrahedron(px, py, pz, qx, qy, qz, rx, ry, rz); + sum += fabs(ivol); + /* det b c d -> q r p */ + polar_to_rectangular(current_year[j], theta * j, &px, &py); + pz = 0; + ivol = volume_of_tetrahedron(px, py, pz, qx, qy, qz, rx, ry, rz); + sum += fabs(ivol); + } + return sum; +} +int main() +{ + set_of_criteria current_year, previous_year; + int i, config; + char buffer[BUFSIZ], *cptr; + int num_criteria, num_years, year, max_config = 0; + double vol, total_vol, max_vol = 0, sum; + /* Read number of criteria and number of years */ + (void)scanf("%d %d\n", &num_criteria, &num_years); + config = 1; + while (1) + { + total_vol = 0; + /* For each configuration */ + /* Read first year */ + (void)gets(buffer); + cptr = strtok(buffer, " "); + for (i = 0, sum = 0; i < num_criteria; i++) + { + (void)sscanf(cptr, "%lf", &previous_year[i]); + cptr = strtok(NULL, " "); + sum += previous_year[i]; + } + if (sum == 0) + { + break; + } + /* For each subsequent year */ + for (year = 1; year < num_years; year++) + { + /* Read a line of criteria */ + (void)gets(buffer); + cptr = strtok(buffer, " "); + for (i = 0; i < num_criteria; i++) + { + (void)sscanf(cptr, "%lf", ¤t_year[i]); + cptr = strtok(NULL, " "); + } + /* Calculate annual volume using present year and prev year */ + vol = annual_volume(num_criteria, previous_year, current_year); + /* update previous year */ + for (i = 0; i < num_criteria; i++) + { + previous_year[i] = current_year[i]; + } + /* update total volume */ + total_vol += vol; + } + /* If volume is largest, save configuration number */ + if (total_vol > max_vol) + { + max_vol = total_vol; + max_config = config; + } + config++; + } + (void)fprintf(stdout, "%d %.2f\n", max_config, max_vol); + return 0; +} diff --git a/421.cpp b/421.cpp new file mode 100644 index 0000000..50080ad --- /dev/null +++ b/421.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +/*----------------------------------------------------------------------- + * + * File Name: p3.c + * + * Author: A. Lumsdaine + * + *----------------------------------------------------------------------- + * + * DESCRIPTION + * Source code file for problem #3 of 1996 ACM + * East-Central Regional Programming contest. + * + * DIAGNOSTICS + * This is probably the hardest problem of the contest. To sort + * the polygons, one needs to match them up polygon to polygon + * and then vertex by vertex. Initial matches can be made by + * calculating the distance between vertices of each polygon and + * then normalizing the distances by the total circumference. + * Candidate matches found this way must then be further checked on + * a vertex by vertex basis. + * + *----------------------------------------------------------------------- + */ +typedef double polygon[100]; +static int +to_try_match(int num_edges, double p1[], double p2[], int *shift) +{ + int i, j, xshift, match = 0; + for (xshift = 0; xshift < num_edges; xshift++) + { + match = 1; + for (i = 0; i < num_edges; i++) + { + j = (i + xshift) % num_edges; + if (fabs(p1[i] - p2[j]) > 1.e-3) + { + match = 0; + break; + } + } + if (match == 1) + { + break; + } + } + if (match == 1) + { + *shift = xshift; + return 0; + } + return -1; +} +static int +find_distance_match(int i, int num_edges, + polygon d1[], polygon d2[], int *shift, int start) +{ + int idx; + for (idx = start; idx < 100; idx++) + if (to_try_match(num_edges, d1[idx], d2[i], shift) == 0) + { + break; + } + return idx; +} +static void +polar_to_rectangular(double r, double theta, double *x, double *y) +{ + *x = r * cos(theta); + *y = r * sin(theta); +} +static int +align_vertices(int num_edges, polygon x, polygon y, polygon r, + int shift, double *angle) +{ + int i = 0, j, j1, match, to_try; + double xangle[2], abs1, abs2, iprod, rat; + double vx1, vx2, vy1, vy2, theta = 2 * M_PI / num_edges, scale; + j = shift; + j1 = (shift + 1) % num_edges; + polar_to_rectangular(r[0], M_PI_2, &vx1, &vy1); + polar_to_rectangular(r[1], M_PI_2 - theta, &vx2, &vy2); + vx1 = vx2 - vx1; + vy1 = vy2 - vy1; + vx2 = x[j1] - x[j]; + vy2 = y[j1] - y[j]; + abs1 = sqrt(vx1 * vx1 + vy1 * vy1); + abs2 = sqrt(vx2 * vx2 + vy2 * vy2); + scale = abs2 / abs1; + iprod = vx1 * vx2 + vy1 * vy2; + rat = iprod / (abs1 * abs2); + xangle[0] = acos(rat); + xangle[1] = 2. * M_PI - xangle[0]; + for (to_try = 0; to_try < 2; to_try++) + { + /* Calculate shift */ + polar_to_rectangular(r[0] * scale, M_PI_2 + xangle[to_try], &vx1, &vy1); + vx2 = vx1 - x[shift]; + vy2 = vy1 - y[shift]; + /* Now see if the other vertices line up */ + match = 1; + for (i = 0; i < num_edges; i++) + { + j = (shift + i) % num_edges; + polar_to_rectangular(r[i] * scale, M_PI_2 + xangle[to_try] - theta * i, + &vx1, &vy1); + vx1 -= vx2; + vy1 -= vy2; + if (fabs(x[j] - vx1) > 1.e-3 || fabs(y[j] - vy1) > 1.e-3) + { + match = 0; + break; + } + } + if (match == 1) + { + *angle = xangle[to_try]; + break; + } + else + { + *angle = 0.0; + } + } + return match; +} +int main() +{ + char buffer[BUFSIZ], *cptr; + int i, j, k, num_polygons, num_edges, candidate; + polygon r[100], x[100], y[100], d1[100], d2[100]; + double theta, sum, u, v, angle; + int index[100], matched, shift; + double rotate[100]; + /* Read number of polygons and number of edges of each polygon */ + (void)scanf("%d %d\n", &num_polygons, &num_edges); + theta = 2.0 * M_PI / num_edges; + /* Read polygons */ + for (i = 0; i < num_polygons; i++) + { + (void)gets(buffer); + cptr = strtok(buffer, " "); + for (j = 0; j < num_edges; j++) + { + (void)sscanf(cptr, "%lf", &r[i][j]); + cptr = strtok(NULL, " "); + } + /* Convert (r, theta) to normalized distances */ + /* c = sqrt(a^2+b^2-2 ab cos(theta)) */ + for (j = 0, sum = 0; j < num_edges; j++) + { + k = (j + 1) % num_edges; + d1[i][j] = sqrt(r[i][j] * r[i][j] + r[i][k] * r[i][k] - 2 * r[i][j] * r[i][k] * cos(theta)); + sum += d1[i][j]; + } + for (j = 0; j < num_edges; j++) + { + d1[i][j] /= sum; + } + } + for (i = 0; i < num_polygons; i++) + { + (void)gets(buffer); + cptr = strtok(buffer, " "); + for (j = 0; j < num_edges; j++) + { + (void)sscanf(cptr, "%lf", &x[i][j]); + cptr = strtok(NULL, " "); + (void)sscanf(cptr, "%lf", &y[i][j]); + cptr = strtok(NULL, " "); + } + /* Convert (x, y) to normalized distances */ + for (j = 0, sum = 0; j < num_edges; j++) + { + k = (j + 1) % num_edges; + u = x[i][j] - x[i][k]; + v = y[i][j] - y[i][k]; + d2[i][j] = sqrt(u * u + v * v); + sum += d2[i][j]; + } + for (j = 0; j < num_edges; j++) + { + d2[i][j] /= sum; + } + } + /* Match up normalized distances */ + for (i = 0; i < num_polygons; i++) + { + matched = 0; + candidate = -1; + do + { + candidate = + find_distance_match(i, num_edges, d1, d2, &shift, candidate + 1); + matched = align_vertices(num_edges, x[i], y[i], r[candidate], + shift, &angle); + } while (matched == 0); + rotate[i] = angle; + index[i] = candidate; + } + for (i = 0; i < num_polygons; i++) + { + (void)printf("%d %.1f\n", index[i] + 1, 180 * rotate[i] / M_PI); + } + return 0; +} diff --git a/422.cpp b/422.cpp new file mode 100644 index 0000000..8625e62 --- /dev/null +++ b/422.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +int n; +char field[101][101]; + +struct word +{ + pair start; + pair end; +}; + +word find_word(const string &s) +{ + bool found; + word result; + int sz = s.size(); + result.start.first = -1; + // left to right + for (int i = 0; i < n; i++) + { + for (int j = 0; j + sz - 1 < n; j++) + { + found = true; + for (int a = 0; a < sz; a++) + { + if (s[a] != field[i][j + a]) + { + found = false; + break; + } + } + if (found) + { + result.start.first = i + 1; + result.start.second = j + 1; + result.end.first = i + 1; + result.end.second = j + sz; + return result; + } + } + } + // right to left + for (int i = 0; i < n; i++) + { + for (int j = n - 1; j - sz + 1 >= 0; j--) + { + found = true; + for (int a = 0; a < sz; a++) + { + if (s[a] != field[i][j - a]) + { + found = false; + break; + } + } + if (found) + { + result.start.first = i + 1; + result.start.second = j + 1; + result.end.first = i + 1; + result.end.second = j + 1 - sz + 1; + } + } + } + // top to bottom + for (int j = 0; j < n; j++) + { + for (int i = 0; i + sz - 1 < n; i++) + { + found = true; + for (int a = 0; a < sz; a++) + { + if (s[a] != field[i + a][j]) + { + found = false; + break; + } + } + if (found) + { + result.start.first = i + 1; + result.start.second = j + 1; + result.end.first = i + sz; + result.end.second = j + 1; + } + } + } + // diagonal left to right + for (int i = 0; i + sz - 1 < n; i++) + { + for (int j = 0; j + sz - 1 < n; j++) + { + found = true; + for (int a = 0; a < sz; a++) + { + if (s[a] != field[i + a][j + a]) + { + found = false; + break; + } + } + if (found) + { + result.start.first = i + 1; + result.start.second = j + 1; + result.end.first = i + sz; + result.end.second = j + sz; + } + } + } + // diagonal right to left + for (int i = 0; i + sz - 1 < n; i++) + { + for (int j = n - 1; j - sz + 1 >= 0; j--) + { + found = true; + for (int a = 0; a < sz; a++) + { + if (s[a] != field[i + a][j - a]) + { + found = false; + break; + } + } + if (found) + { + result.start.first = i + 1; + result.start.second = j + 1; + result.end.first = i + sz; + result.end.second = j + 1 - sz + 1; + } + } + } + return result; +} + +int main() +{ + word result; + string input; + cin >> n; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> field[i][j]; + } + } + cin.ignore(100, '\n'); + while (getline(cin, input)) + { + if (input == "0") + { + break; + } + result = find_word(input); + if (result.start.first == -1) + { + cout << "Not found" << endl; + } + else + { + cout << result.start.first << "," << result.start.second << " " << result.end.first << "," << result.end.second << endl; + } + } + return 0; +} diff --git a/423.cpp b/423.cpp index 5f1d739..e7c95d2 100644 --- a/423.cpp +++ b/423.cpp @@ -1,70 +1,61 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef long long int64; -typedef unsigned long long uint64; +#define MIN(a, b) (a > b ? b : a) +#define MAXN 102 +#define INF 21474836 -#define INF 1LL<<60 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +int N, A[MAXN][MAXN]; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N; +void Floyd() +{ int i, j, k; - cin >> N; - int64 mat[N][N]; - FOI(i, 0, N-1) - mat[i][i] = 0; - FOI(i, 0, N-1){ - FOI(j, 0, i-1){ - string str; - cin >> str; - if( str == "x" ){ - mat[i][j] = INF; - mat[j][i] = INF; - } - else{ - stringstream ss(str); - ss >> mat[i][j]; - mat[j][i] = mat[i][j]; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + A[i][j] = MIN(A[i][j], A[i][k] + A[k][j]); +} +void Cal() +{ + int i, min = 0; + Floyd(); + for (i = 1; i <= N; i++) + if (A[1][i] > min) + min = A[1][i]; + printf("%d\n", min); +} +void Ini() +{ + int i; + for (i = 1; i <= N; i++) + A[i][i] = 0; +} + +int main() +{ + int i, j; + char dummy[100]; + while (scanf("%d", &N) == 1) + { + Ini(); + for (i = 2; i <= N; i++) + { + for (j = 1; j < i; j++) + { + scanf("%s", dummy); + if (!strcmp(dummy, "x")) + A[j][i] = A[i][j] = INF; + else + A[i][j] = A[j][i] = atoi(dummy); } } + if (N == 1) + { + printf("0\n"); + continue; + } + Cal(); } - - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]); - - int64 gmax = 0; - FOI(i, 0, N-1) - gmax = max(gmax, mat[0][i]); - cout << gmax << endl; return 0; } diff --git a/424.cpp b/424.cpp new file mode 100644 index 0000000..b060501 --- /dev/null +++ b/424.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +int result[11000]; + +int main() +{ + char a[101][101]; + int i, j, sum, k = 0, max = 0; + int cary, n, g, l; + while (scanf("%s", a[k]) && strcmp(a[k], "0")) + { + k++; + } + for (i = 0; i < k; i++) + if (strlen(a[i]) > max) + { + max = strlen(a[i]); + } + for (i = 0; i < k; i++) + { + g = 0; + l = strlen(a[i]); + if (l >= 99) + { + continue; + } + for (j = l - 1; j >= 0; j--) + { + a[i][99 - g] = a[i][j]; + g++; + } + for (j = 0; j <= 99 - g; j++) + { + a[i][j] = '0'; + } + } + n = 0; + cary = 0; + for (i = 99; i > 99 - max; i--) + { + sum = 0; + for (j = 0; j < k; j++) + if (a[j][i]) + { + sum += a[j][i] - 48; + } + sum = sum + cary; + cary = sum / 10; + result[n++] = sum % 10; + } + if (cary) + { + result[n++] = cary; + } + for (i = n - 1; i >= 0; i--) + { + printf("%d", result[i]); + } + printf("\n"); + return 0; +} diff --git a/425.cpp b/425.cpp new file mode 100644 index 0000000..48920cf --- /dev/null +++ b/425.cpp @@ -0,0 +1,112 @@ +#include +#include + +using namespace std; + +static char twos[65536][7]; +static char threes[65536][7]; +static char fours[65536][7]; +static char fives[65536][7]; + +static int isword(const char *word) +{ + int i, n = strlen(word); + for (i = 0; i < n; i++) + { + if (!isalpha(word[i])) + { + return 0; + } + } + return 1; +} +static void uniq_insert(char buf[][7], char *cptr) +{ + int i = 0, j; + char buffer[BUFSIZ]; + (void)strcpy(buffer, cptr); + for (j = 0; j < 7 && buffer[j] != 0; j++) + { + buffer[j] = tolower(buffer[j]); + } + while (buf[i][0] != 0) + { + if (strcmp(buf[i], buffer) == 0) + { + return; + } + i++; + } + (void)strcpy(buf[i], buffer); +} +static void to_try(char first[][7], char second[][7], char *crypted) +{ + int i, j, n, arr[] = {0, 2, 4, 8}; + char buffer[BUFSIZ], salt[BUFSIZ]; + salt[0] = crypted[0]; + salt[1] = crypted[1]; + salt[2] = 0; + for (i = 0; first[i][0] != 0; i++) + { + for (j = 0; second[j][0] != 0; j++) + { + for (n = 0; n < 4; n++) + { + sprintf(buffer, "%s%d%s", first[i], arr[n], second[j]); + if (strcmp(crypt(buffer, salt), crypted) == 0) + { + printf("%s\n", buffer); + exit(0); + } + } + } + } +} + +int main() +{ + char crypted[BUFSIZ], *cptr, buffer[BUFSIZ], temp[BUFSIZ]; + const char *sep = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n\t"; + bzero(twos, sizeof(twos)); + bzero(threes, sizeof(threes)); + bzero(fours, sizeof(fours)); + bzero(fives, sizeof(fives)); + fgets(temp, BUFSIZ, stdin); + sscanf(temp, "%s", crypted); + while (fgets(buffer, BUFSIZ, stdin) != NULL) + { + cptr = strtok(buffer, sep); + while (cptr != NULL) + { + if (isword(cptr)) + { + switch (strlen(cptr)) + { + case 2: + uniq_insert(twos, cptr); + break; + case 3: + uniq_insert(threes, cptr); + break; + case 4: + uniq_insert(fours, cptr); + break; + case 5: + uniq_insert(fives, cptr); + break; + } + } + cptr = strtok(NULL, sep); + } + } + to_try(twos, threes, crypted); + to_try(twos, fours, crypted); + to_try(twos, fives, crypted); + to_try(threes, twos, crypted); + to_try(threes, threes, crypted); + to_try(fours, twos, crypted); + to_try(fours, threes, crypted); + to_try(fives, twos, crypted); + to_try(threes, fours, crypted); + return 0; +} diff --git a/426.cpp b/426.cpp new file mode 100644 index 0000000..515d565 --- /dev/null +++ b/426.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +/* @JUDGE_ID: 2905EH 426 C++ "easy!!" */ +//2001/5/8 a_q426.cpp + +struct AccountStruct +{ + string date; + int id; + double value; + bool operator<(const AccountStruct &p) const + { + return id < p.id; + } +}; + +vector acc; + +char line[2000]; + +void PrintAccount(int i) +{ + printf("%4d%c %9.2lf %s", acc[i].id, + (i > 0 && acc[i].id > acc[i - 1].id + 1) ? '*' : ' ', + acc[i].value, acc[i].date.c_str()); +} + +int main() +{ + gets(line); + int n_case; + sscanf(line, "%d", &n_case); + gets(line);//blank + for (int x = 0; x < n_case; x++) + { + if (x) + { + puts(""); + } + while (1) + { + if (gets(line) == NULL) + { + break; + } + if (strcmp(line, "") == 0) + { + break; + } + char *p = strtok(line, " \t\b\r"); + if (p == NULL) + { + break;//blank.. + } + AccountStruct tmp; + tmp.date = p; + p = strtok(NULL, " \t\b\r"); + // if (p==NULL) while(1); //debug..no + if (!p) + { + tmp.id = 0; + } + else + { + tmp.id = atoi(p); + } + p = strtok(NULL, " \t\n\b\r"); + // if (p==NULL) while(1); //debug..no + if (!p) + { + tmp.value = 0.0; + } + else + { + tmp.value = atof(p); + } + acc.push_back(tmp); + } + // if (acc.size()==0) while(1); //debug..no + //process.. + sort(acc.begin(), acc.end()); + int n_row = acc.size() / 3; + if (acc.size() % 3 != 0) + { + n_row++; + } + int start0 = 0, start1 = n_row, start2 = n_row * 2; + while (--n_row >= 0) + { + PrintAccount(start0++); + if (start1 < acc.size()) + { + printf(" "); + PrintAccount(start1++); + } + if (start2 < acc.size()) + { + printf(" "); + PrintAccount(start2++); + } + printf("\n"); + } + //init.. + acc.clear(); + } +} +/*@END_OF_SOURCE_CODE*/ diff --git a/427.cpp b/427.cpp new file mode 100644 index 0000000..876b6ff --- /dev/null +++ b/427.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +#define STEP 1e-3 + +const double HALFPI = acos(0.0); +char buf[2008]; + +int main() +{ + int p, q, px, py; + double angle, height; + char *ptr; + while (gets(buf)) + { + ptr = strtok(buf, " "); + sscanf(ptr, "%d,%d", &px, &py); + if (px > py) + { + px ^= py ^= px ^= py; + } + while (ptr = strtok(NULL, " ")) + { + sscanf(ptr, "%d,%d", &p, &q); + for (angle = 0.0; angle <= HALFPI; angle += STEP) + { + height = px * sin(angle) + py * cos(angle) - q; + height *= tan(angle); + height += px * cos(angle); + if (height > p) + { + break; + } + } + if (angle > HALFPI) + { + printf("Y"); + } + else + { + printf("N"); + } + } + printf("\n"); + } + return 0; +} diff --git a/428.cpp b/428.cpp new file mode 100644 index 0000000..175915f --- /dev/null +++ b/428.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +double lot_area, roof_area, floor_area, pi; +double total_roof = 0., total_floor = 0., total_area = 0.; +int nlots; + +void take() +{ + double a, b, d, r; + int c; + for (roof_area = floor_area = 0;;) + { + while ((c = getchar()) != EOF && !strchr("0123456789-.\n", c)) + ; + if (c == '\n') + { + while ((c = getchar()) != EOF && !strchr("0123456789-.\n", c)) + ; + if (c == '\n') + { + return; + } + } + if (c == EOF) + { + return; + } + ungetc(c, stdin); + if (scanf("%lf %lf %lf %lf", &b, &r, &d, &a) != 4) + { + return; + } + roof_area += (b + r) * d / 2.; + floor_area += (b + r) * d * cos(a * pi / 180.) / 2.; + } +} + +int main() +{ + printf("Roof Area Floor Area %% Covered\n"); + printf("--------- ---------- ---------\n"); + pi = 2. * acos(0.); + for (nlots = 0; scanf("%lf", &lot_area) == 1; nlots++) + { + take(); + printf("%9.2f%15.2f%13.2f%%\n", roof_area, floor_area, floor_area * 100. / lot_area); + total_area += lot_area; + total_roof += roof_area; + total_floor += floor_area; + } + printf( + "\n" + "Total surface area of roofs %11.2f\n" + "Total area covered by roofs %11.2f\n" + "Percentage of total area covered by roofs %7.2f%%\n" + "Average roof surface area per lot %15.2f\n" + "Average floor space covered per lot %13.2f\n", + total_roof, total_floor, + total_floor * 100. / total_area, + total_roof / (double)nlots, + total_floor / (double)nlots); + return 0; +} diff --git a/429.cpp b/429.cpp index 42d3d7c..e7fa7c2 100644 --- a/429.cpp +++ b/429.cpp @@ -1,108 +1,168 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; + +typedef string vdata; + +struct Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + int bfs(vdata &b, vdata &e); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} -map< string, list< string > > Map; -list< string >::iterator lit; -map< string, bool > used; -vector< string > dict; +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} -int bfs(string src, string des){ - queue< pair > q; - q.push( make_pair(src, 0) ); - while( !q.empty() ){ - pair p = q.front(); +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +int Graph::bfs(vdata &b, vdata &e) +{ + queue> q; + Vertex *beginning = getVertex(b); + pair temp; + map taken; + q.push(pair(beginning, 0)); + while (!q.empty()) + { + temp = q.front(); q.pop(); - - if( p.first == des ) - return p.second; - if( used[p.first] ) - continue; - - for (lit = Map[p.first].begin(); lit != Map[p.first]. end(); lit++) - q.push( make_pair(*lit, p.second + 1) ); - - used[p.first] = true; + if (temp.first->id == e) + { + return temp.second; + } + for (int i = 0; i < temp.first->adj.size(); i++) + { + if (taken[temp.first->adj[i]->id] == false) + { + taken[temp.first->adj[i]->id] = true; + q.push(pair(temp.first->adj[i], temp.second + 1)); + } + } } return -1; } -bool diff(string A, string B){ - int i, cnt = 0; - int AL = A.length(); - int BL = B.length(); - if( AL != BL ) - return false; - FOI(i, 0, AL-1) - if( A[i] != B[i] ) - cnt++; - if( cnt == 1 ) - return true; - return false; -} +int main() +{ + int n, i, j, k, c = 0; + string input, beginning, ending; + vector dic; + stringstream ss; + Graph *g; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, i, j; - cin >> T; - string str; - getline(cin, str); getline(cin, str); - while( T-- ){ - Map.clear(); dict.clear(); used.clear(); - while( true ){ - getline(cin, str); - if( str == "*" ) + cin >> n; + getline(cin, input); + getline(cin, input); + while (n--) + { + if (c > 0) + { + cout << endl; + } + c++; + dic.clear(); + g = new Graph(); + while (getline(cin, input)) + { + if (input == "*") + { break; - dict.push_back(str); + } + dic.push_back(input); } - sort(dict.begin(), dict.end()); - FOI(i, 0, dict.size()-1){ - FOI(j, 0, dict.size()-1){ - if( diff(dict[i], dict[j]) ){ - Map[ dict[i] ].push_back( dict[j] ); - Map[ dict[j] ].push_back( dict[i] ); + for (i = 0; i < dic.size() - 1; i++) + { + for (j = i + 1; j < dic.size(); j++) + { + if (dic[i].size() != dic[j].size()) + { + continue; + } + int bad = 0; + for (k = 0; k < dic[i].size(); k++) + { + if (dic[i][k] != dic[j][k]) + { + bad += 1; + if (bad > 1) + { + break; + } + } + } + if (bad < 2) + { + g->addEdge(dic[i], dic[j]); } } - used[ dict[i] ] = false; } - while( getline(cin, str) ){ - if( str.empty() ) + while (getline(cin, input)) + { + if (input.size() == 0) + { break; - used.clear(); - stringstream ss(str); - string src, des; - ss >> src >> des; - cout << src << " " << des << " " << bfs(src, des) << endl; + } + ss << input; + ss >> beginning >> ending; + cout << beginning << " " << ending << " " << g->bfs(beginning, ending) << endl; + ss.str(string()); + ss.clear(); } - if( T ) - cout << endl; + delete g; } return 0; } diff --git a/430.cpp b/430.cpp new file mode 100644 index 0000000..26d908f --- /dev/null +++ b/430.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +int ary[30], dp[50001][30]; + +int max(int p, int q) +{ + if (p > q) + { + return p; + } + return q; +} + +int main() +{ + int n, m, i, j, k, now; + char buf[100], *ptr; + while (gets(buf)) + { + for (i = 0, j = 0, k = 0, m = -1; buf[i]; i++) + { + if (buf[i] > 47 && buf[i] < 58) + { + k *= 10; + k += buf[i] - 48; + j = 1; + } + else + { + if (j) + { + if (m == -1) + { + n = k; + } + else + { + ary[m] = k; + } + m++; + j = k = 0; + } + } + } + if (j) + { + if (m == -1) + { + n = k; + } + else + { + ary[m] = k; + } + m++; + j = k = 0; + } + if (m < 0) + { + continue; + } + memset(dp, 0, sizeof(dp)); + /*ptr = strtok(buf, " "); + n = atoi(ptr); + m = 0; + memset(dp, 0, sizeof(dp)); + while(ptr = strtok(NULL, " ")) + { + ary[m++] = atoi(ptr); + }*/ + for (i = 0; i < m; i++) + { + dp[0][i] = 1; + } + for (j = 0; j < m; j++) + { + for (i = n; i >= ary[j]; i--) + { + for (k = 0; k < m; k++) + { + if (k != j) + { + dp[i][k] += dp[i - ary[j]][k]; + } + } + } + } + for (j = 0; j < m; j++) + { + for (i = max(n - ary[j], 0), k = 0; i < n; i++) + { + k += dp[i][j]; + } + if (j) + { + printf(" %d", k); + } + else + { + printf("%d", k); + } + } + printf("\n"); + } + return 0; +} diff --git a/432.cpp b/432.cpp new file mode 100644 index 0000000..2b74eb8 --- /dev/null +++ b/432.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define mp make_pair + +typedef pair pii; + +void output(int x1, int y1, int x2, int y2) +{ + printf("(%d,%d)(%d,%d)\n", x1, y1, x2, y2); +} + +void reverse_triangle(int basex, int x, int y) +{ + output(basex + x, 0, basex + x - x / 2, y / 2); + output(basex + x - x / 2, y / 2, basex + x + x / 2, y / 2); + output(basex + x + x / 2, y / 2, basex + x, 0); +} + +pii recleft(int basex, int x, int y, int rem) +{ + pii last; + if (rem != 0) + { + last = recleft(basex, x / 2, y / 2, rem - 1); + } + else + { + return mp(basex, 0); + } + output(last.first, last.second, basex + x, 0); + reverse_triangle(basex, x, y); + return mp(basex + x, 0); +} + +pii recright(int basex, int x, int y, int rem) +{ + pii last; + output(basex, 0, basex + x, 0); + reverse_triangle(basex, x, y); + if (rem > 1) + { + last = recright(basex + x, x / 2, y / 2, rem - 1); + return last; + } + return mp(basex + x, 0); +} + +void solve(int r, int l, int x, int y) +{ + pii last; + if (r == 1 && l == 1) + ; + else if ((r > 0 && l > 1) || (r > 1 || l > 0)) + ; + else if (r == 0 && l == 0) + { + output(x, y, 0, 0); + output(0, 0, 2 * x, 0); + output(2 * x, 0, x, y); + return; + } + else + { + return; + } + output(x, y, 0, 0); + if (l > 1) + { + last = recleft(0, x / 2, y / 2, l - 1); + } + else + { + last = mp(0, 0); + } + output(last.first, last.second, x, 0); + reverse_triangle(0, x, y); + last = mp(x, 0); + if (r > 1) + { + last = recright(x, x / 2, y / 2, r - 1); + }//else + output(last.first, last.second, 2 * x, 0); + output(2 * x, 0, x, y); +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int r, l, x, y; + cin >> l >> r >> x >> y; + solve(r, l, x, y); + puts(""); + } +} diff --git a/433.cpp b/433.cpp new file mode 100644 index 0000000..915cb78 --- /dev/null +++ b/433.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +char *good_digit[10] = + { + " _ | ||_|", + " | |", + " _ _||_ ", + " _ _| _|", + " |_| |", + " _ |_ _|", + " _ |_ |_|", + " _ | |", + " _ |_||_|", + " _ |_| _|"}; + +char full_digit[] = + " _ |_||_|"; + +int isOK(int c[9]) +{ + int result = 0; + for (int i = 0; i < 9; i++) + { + result += (9 - i) * c[i]; + } + return (result % 11) == 0; +} + +int scanDigit(char d[9][9], int c[9]) +{ + int i, j; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 10; j++) + if (memcmp(d[i], good_digit[j], 9) == 0) + { + break; + } + if (j == 10) + { + return -1; + } + c[i] = j; + } + return 0; +} + +char digit[9][9]; + +int set_digit; +int ok_code[9]; +int n_ok_code; + +void PrintDigit(void) +{ + int i, j; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + putchar(digit[i][j]); + } + putchar('\n'); + } +} + +void TestSeg(int n) +{ + //printf("(%d)\n",n); + if (n == 9) + { + int test_code[9]; + if (scanDigit(digit, test_code) == 0 && isOK(test_code)) + { + memcpy(ok_code, test_code, 9 * sizeof(int)); + n_ok_code++; + /* + printf("=="); + for (int k=0; k<9; k++) + printf("%d",ok_code[k]); + printf("\n"); + */ + } + return; + } + TestSeg(n + 1); + if (digit[set_digit][n] != full_digit[n]) + { + digit[set_digit][n] = full_digit[n]; + TestSeg(n + 1); + digit[set_digit][n] = ' '; + } +} + +int main() +{ + char line[80]; + gets(line); + int n_case = atoi(line); + while (--n_case >= 0) + { + memset(digit, ' ', 9 * 9); + int i, j; + for (i = 0; i < 3; i++) + { + gets(line); + int len = strlen(line); + for (j = 0; j < len; j++) + { + if (line[j] == '_' || line[j] == '|') + { + digit[j / 3][i * 3 + j % 3] = line[j]; + } + else if (line[j] != ' ') + while (1) + ;// other char... + } + } + // PrintDigit(); + int code[9]; + if (scanDigit(digit, code) == 0 && isOK(code)) + { + for (i = 0; i < 9; i++) + { + printf("%d", code[i]); + } + printf("\n"); + continue; + } + n_ok_code = 0; + for (i = 0; i < 9 && n_ok_code < 2; i++) + { + set_digit = i; + TestSeg(0); + } + if (n_ok_code == 0) + { + printf("failure\n"); + } + else if (n_ok_code != 1) + { + printf("ambiguous\n"); + } + else + { + for (i = 0; i < 9; i++) + { + printf("%d", ok_code[i]); + } + printf("\n"); + } + } + return 0; +} diff --git a/434.cpp b/434.cpp new file mode 100644 index 0000000..c3f36f4 --- /dev/null +++ b/434.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int minimum(int k, int *front, int *right) +{ + int result = 0; + bool front_taken[8] = {false}; + bool right_taken[8] = {false}; + rep(i, k) + { + rep(j, k) + { + if (front[i] == right[j] && !front_taken[i] && !right_taken[j]) + { + result += front[i]; + front_taken[i] = true; + right_taken[j] = true; + } + } + } + rep(i, k) + { + if (!front_taken[i]) + { + result += front[i]; + } + if (!right_taken[i]) + { + result += right[i]; + } + } + return result; +} + +int maximum(int k, int *front, int *right) +{ + int result = 0; + rep(i, k) + rep(j, k) + result += min(front[i], right[j]); + return result; +} + +int main() +{ + int cases, k; + int front[8], right[8]; + cin >> cases; + while (cases--) + { + cin >> k; + rep(i, k) cin >> front[i]; + rep(i, k) cin >> right[i]; + pair result = make_pair(minimum(k, front, right), maximum(k, front, right)); + cout << "Matty needs at least " << result.first << " blocks, and can add at most " << result.second - result.first << " extra blocks." << endl; + } + return 0; +} diff --git a/435.cpp b/435.cpp new file mode 100644 index 0000000..b0fb23e --- /dev/null +++ b/435.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +int result[20]; +int parties[20]; +bool taken[20]; + +void compute(int index, int p, int sum, int limit) +{ + for (int i = 0; i < p; i++) + { + if (taken[i]) + { + continue; + } + taken[i] = true; + if (sum + parties[i] > limit) + { + result[i]++; + } + else + { + if (i >= index) + { + compute(i, p, sum + parties[i], limit); + } + } + taken[i] = false; + } +} + +void solve(int p, int limit) +{ + for (int i = 0; i < 20; i++) + { + result[i] = 0; + taken[i] = false; + } + compute(0, p, 0, limit); +} + +int main() +{ + int cases, p, total; + cin >> cases; + while (cases--) + { + cin >> p; + total = 0; + for (int i = 0; i < p; i++) + { + cin >> parties[i]; + total += parties[i]; + } + solve(p, total / 2); + for (int i = 0; i < p; i++) + { + cout << "party " << i + 1 << " has power index " << result[i] << endl; + } + cout << endl; + } + return 0; +} diff --git a/436.cpp b/436.cpp new file mode 100644 index 0000000..0cc50ad --- /dev/null +++ b/436.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +/* +Arbitage-2 +436 +*/ +#define MAXN 32 +int N, M; +struct ss +{ + char name[1000]; + int index; +} V[MAXN], a, b; +double C[MAXN][MAXN]; +double St[MAXN][MAXN]; +int com(const void *a, const void *b) +{ + ss *x = (ss *)a; + ss *y = (ss *)b; + return strcmp(x->name, y->name); +} +double MAX(double x, double y) +{ + if (x > y) + { + return x; + } + return y; +} +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + V[i].index = i; + for (j = 1; j <= N; j++) + { + St[i][j] = 0.0; + C[i][j] = 0.0; + } + St[i][i] = C[i][i] = 1; + } +} +void Flyod() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + C[i][j] = MAX(C[i][j], C[i][k] * C[k][j]); + } + } + } +} +void Cal() +{ + int i, j; + ss *p, *q; + double cost; + qsort(&V[1], N, sizeof(V[1]), com); + while (M--) + { + scanf("%s%lf%s", a.name, &cost, b.name); + p = (ss *)bsearch(&a, &V[1], N, sizeof(V[0]), com); + q = (ss *)bsearch(&b, &V[1], N, sizeof(V[0]), com); + C[p->index][q->index] = cost; + St[p->index][q->index] = cost; + } + Flyod(); + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (((C[i][j] * St[j][i])) > 1.00) + { + printf("Yes\n"); + return; + } + else if (((C[j][i] * St[i][j])) > 1.00) + { + printf("Yes\n"); + return; + } + } + } + printf("No\n"); +} +int main() +{ + int i, kase = 1; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + Ini(); + for (i = 1; i <= N; i++) + { + scanf("%s", V[i].name); + } + scanf("%d", &M); + printf("Case %d: ", kase++); + Cal(); + } + return 0; +} diff --git a/437.cpp b/437.cpp new file mode 100644 index 0000000..4dec91e --- /dev/null +++ b/437.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAXN 256 +typedef struct Babylon +{ + int t[4]; +}; +Babylon a[MAXN] = {0}; +int n, m, cas = 0; +int comp(Babylon p, Babylon q) +{ + for (int i = 0; i < 3; i++) + if (p.t[i] > q.t[i]) + { + return 1; + } + else if (p.t[i] < q.t[i]) + { + return 0; + } + return 0; +} +void Qsort(int x, int y) +{ + if (x >= y) + { + return; + } + int p = x, q = y; + Babylon F = a[x]; + while (p < q) + { + while (p < q && comp(a[q], F)) + { + q--; + } + if (p < q) + { + a[p++] = a[q]; + } + while (p < q && !comp(a[p], F)) + { + p++; + } + if (p < q) + { + a[q--] = a[p]; + } + } + a[p] = F; + Qsort(x, p - 1); + Qsort(p + 1, y); +} +int cmpare(int *p, int *q) +{ + if (p[0] <= q[0] || p[1] <= q[1]) + { + return 0; + } + return 1; +} +void DAG() +{ + int b[MAXN] = {0}, maxs = 0; + b[0] = a[0].t[2]; + for (int i = 1; i < m; i++) + { + b[i] = a[i].t[2]; + for (int j = 0; j < i; j++) + if (cmpare(a[i].t, a[j].t) && b[i] < b[j] + a[i].t[2]) + { + b[i] = b[j] + a[i].t[2]; + } + if (maxs < b[i]) + { + maxs = b[i]; + } + } + printf("Case %d: maximum height = %d\n", ++cas, maxs); +} +int main() +{ + int h[4] = {0}; + while (scanf("%d", &n) == 1) + { + if (!n) + { + break; + } + m = 0; + for (int i = 0; i < n; i++) + { + scanf("%d%d%d", &h[0], &h[1], &h[2]); + a[m].t[0] = h[0]; + a[m].t[1] = h[1]; + a[m++].t[2] = h[2]; + a[m].t[0] = h[0]; + a[m].t[1] = h[2]; + a[m++].t[2] = h[1]; + a[m].t[0] = h[1]; + a[m].t[1] = h[0]; + a[m++].t[2] = h[2]; + a[m].t[0] = h[1]; + a[m].t[1] = h[2]; + a[m++].t[2] = h[0]; + a[m].t[0] = h[2]; + a[m].t[1] = h[0]; + a[m++].t[2] = h[1]; + a[m].t[0] = h[2]; + a[m].t[1] = h[1]; + a[m++].t[2] = h[0]; + } + Qsort(0, m - 1); + DAG(); + } + return 0; +} diff --git a/438.cpp b/438.cpp new file mode 100644 index 0000000..db39447 --- /dev/null +++ b/438.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + double PI = 3.14159265358979323846; + double x1, y1, x2, y2, x3, y3; + while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3) + { + double a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); + double b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); + double c = sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); + double s = (a + b + c) / 2; + double L = sqrt(s * (s - a) * (s - b) * (s - c)); + double r = (a * b * c) / (4 * L); + printf("%.2f\n", 2 * PI * r); + } + return 0; +} diff --git a/439.cpp b/439.cpp new file mode 100644 index 0000000..7a79149 --- /dev/null +++ b/439.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +const int MAX = 0x7fffffff; +char chFrom[5], chTo[5]; + +int min; +int record[8][8][8][8]; + +struct Position +{ + int row, col; +} from, to; + +void init() +{ + from.col = chFrom[0] - 'a'; + from.row = chFrom[1] - '1'; + to.col = chTo[0] - 'a'; + to.row = chTo[1] - '1'; +} + +void try_it(int r, int c, int cnt) +{ + if (r < 0 || r >= 8 || c < 0 || c >= 8 || cnt >= record[from.row][from.col][r][c]) + { + return; + } + record[from.row][from.col][r][c] = cnt; + try_it(r - 2, c - 1, cnt + 1); + try_it(r - 2, c + 1, cnt + 1); + try_it(r - 1, c - 2, cnt + 1); + try_it(r - 1, c + 2, cnt + 1); + try_it(r + 1, c - 2, cnt + 1); + try_it(r + 1, c + 2, cnt + 1); + try_it(r + 2, c - 1, cnt + 1); + try_it(r + 2, c + 1, cnt + 1); +} + +int main() +{ + int i, j, k, l; + for (i = 0; i < 8; i++) + for (j = 0; j < 8; j++) + for (k = 0; k < 8; k++) + for (l = 0; l < 8; l++) + { + record[i][j][k][l] = MAX; + } + while (cin >> chFrom >> chTo) + { + init(); + try_it(from.row, from.col, 0); + cout << "To get from " << chFrom << " to " << chTo << " takes "; + cout << record[from.row][from.col][to.row][to.col] << " knight moves." << endl; + } + return 0; +} diff --git a/440.cpp b/440.cpp new file mode 100644 index 0000000..6dacd33 --- /dev/null +++ b/440.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +main() +{ + int a[152], i, d, k, c, p = 0, n, j, ck; + while (scanf("%d", &n) && n) + { + for (j = 2;; j++) + { + ck = 1; + p = 0; + for (i = 1; i <= n; i++) + { + a[i] = i; + } + k = 1; + while (1) + { + a[k] = 0; + d = k + 1; + c = 0; + if (d > n) + { + d = 1; + } + for (;;) + { + if (a[d]) + { + c++; + } + if (c == j) + { + break; + } + d++; + if (d > n) + { + d = 1; + } + } + if (d == 2) + { + ck = 0; + break; + } + k = d; + p++; + if (p == n - 2) + { + break; + } + } + if (ck) + { + printf("%d\n", j); + break; + } + } + } + return 0; +} diff --git a/441.cpp b/441.cpp new file mode 100644 index 0000000..c60ba70 --- /dev/null +++ b/441.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int k; +long tmp; +int s[20]; +int result[10]; + +void do_stuff(int pos, int nums) +{ + if (nums == 6) + { + for (int i = 0; i < 6; i++) + { + if (i > 0) + { + cout << " "; + } + cout << result[i]; + } + cout << endl; + return; + } + for (int i = pos; i < k; i++) + { + result[nums] = s[i]; + do_stuff(i + 1, nums + 1); + } +} + +int main() +{ + bool first = true; + while (cin >> k) + { + if (k == 0) + { + break; + } + for (int i = 0; i < k; i++) + { + cin >> s[i]; + } + if (!first) + { + cout << endl; + } + do_stuff(0, 0); + first = false; + } + return 0; +} diff --git a/442.cpp b/442.cpp new file mode 100644 index 0000000..f2b6270 --- /dev/null +++ b/442.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +bool error; +char cmd[500]; +int numOfMatrics, idx; + +struct Matrics +{ + char alpha; + int row, col; + int mul; +} mat[30]; + +void input() +{ + cin >> numOfMatrics; + int i; + for (i = 0; i < numOfMatrics; i++) + { + cin >> mat[i].alpha >> mat[i].row >> mat[i].col; + mat[i].mul = 0; + } + cin.get(); +} + +void init() +{ + idx = 0; + error = false; +} + +Matrics solve() +{ + Matrics t; + if (cmd[idx] == '(') + { + Matrics t1, t2; + idx++; + t1 = solve(); + t2 = solve(); + idx++; + if (t1.col != t2.row) + { + error = true; + } + t.mul = t1.mul + t2.mul + t1.row * t1.col * t2.col; + t.row = t1.row; + t.col = t2.col; + } + else if (isalpha(cmd[idx])) + { + t.row = mat[cmd[idx] - 'A'].row; + t.col = mat[cmd[idx] - 'A'].col; + t.mul = 0; + idx++; + } + else + { + idx++; + } + return t; +} + +int main() +{ + input(); + while (cin.getline(cmd, 500)) + { + init(); + Matrics matric; + matric = solve(); + if (error) + { + printf("error\n"); + } + else + { + cout << matric.mul << endl; + } + } + return 0; +} diff --git a/443.cpp b/443.cpp index 7317aee..89edef7 100644 --- a/443.cpp +++ b/443.cpp @@ -1,73 +1,89 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef long long int64; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +unsigned long Min(long Hasil2, long Hasil3, long Hasil5, long Hasil7) +{ + long Temp[4], max; + int i; + Temp[0] = Hasil2; + Temp[1] = Hasil3; + Temp[2] = Hasil5; + Temp[3] = Hasil7; + max = Temp[0]; + for (i = 1; i < 4; i++) + { + if (Temp[i] < max) + { + max = Temp[i]; + } + } + return max; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - map Map; - vector< int64 > V; - V.push_back(1); V.push_back(2); V.push_back(3); V.push_back(5); V.push_back(7); - Map[1] = true; Map[2] = true; Map[3] = true; Map[5] = true; Map[7] = true; - int64 i, j; - for (i = 0; ; i++){ - for (j = 0; j <= i; j++){ - int64 val = V[i] * V[j]; - if (!Map[val]){ - V.push_back(val); - Map[val] = true; - } +int main() +{ + long ugly[5842]; + int k = 5842; + long index2 = 0, index3 = 0, index5 = 0, index7 = 0; + long Hasil2, Hasil3, Hasil5, Hasil7, target; + long i; + Hasil2 = 1; + Hasil3 = 1; + Hasil5 = 1; + Hasil7 = 1; + ugly[0] = 1; + for (i = 1; i < k; i++) + { + target = ugly[i - 1] + 1; + while ((Hasil2 = 2 * ugly[index2]) < target) + { + index2++; } - if (V.size() > 14000) - break; + while ((Hasil3 = 3 * ugly[index3]) < target) + { + index3++; + } + while ((Hasil5 = 5 * ugly[index5]) < target) + { + index5++; + } + while ((Hasil7 = 7 * ugly[index7]) < target) + { + index7++; + } + ugly[i] = Min(Hasil2, Hasil3, Hasil5, Hasil7); } - sort(V.begin(), V.end()); - while (true){ - int64 N; - cin >> N; - if (N == 0) + while (scanf("%ld", &i)) + { + if (i == 0) + { break; - int64 T = N % 10; - int64 H = (N % 100) / 10; - string str; - if (T == 1 && H != 1) - str = "st"; - else if (T == 2 && H != 1) - str = "nd"; - else if (T == 3 && H != 1) - str = "rd"; + } + if ((i % 100) >= 10 && (i % 100) <= 20) + { + printf("The %ldth humble number is %ld.\n", i, ugly[i - 1]); + } + else if ((i % 10) == 1) + { + printf("The %ldst humble number is %ld.\n", i, ugly[i - 1]); + } + else if ((i % 10) == 2) + { + printf("The %ldnd humble number is %ld.\n", i, ugly[i - 1]); + } + else if ((i % 10) == 3) + { + printf("The %ldrd humble number is %ld.\n", i, ugly[i - 1]); + } + else if ((i % 10) == 4) + { + printf("The %ldth humble number is %ld.\n", i, ugly[i - 1]); + } else - str = "th"; - - cout << "The " << N << str << " humble number is " << V[N - 1] << ".\n"; + { + printf("The %ldth humble number is %ld.\n", i, ugly[i - 1]); + } } return 0; } - diff --git a/444.cpp b/444.cpp new file mode 100644 index 0000000..378a40a --- /dev/null +++ b/444.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +/** +444 +***/ +#define MAXN 300 +char Valid[MAXN]; +char Mss[MAXN]; +char Temp[MAXN + MAXN]; +char p[] = ".!,.:;? "; +void Set() +{ + int i, j; + for (i = 0; i < MAXN; i++) + { + Valid[i] = 0; + } + for (i = 0; p[i]; i++) + { + j = p[i]; + Valid[j] = 1; + } + for (char c = 'A'; c <= 'z'; c++) + { + j = c; + Valid[j] = j; + } +} +void Decode() +{ + int i, j, k; + char temp[10]; + Temp[0] = NULL; + for (i = 0; Mss[i]; i++) + { + j = Mss[i]; + sprintf(temp, "%d", j); + strcat(Temp, temp); + } + k = strlen(Temp); + for (i = k - 1; i >= 0; i--) + { + printf("%c", Temp[i]); + } + printf("\n"); +} +void Encode() +{ + int i, j, l = 0, k; + char temp[10000]; + k = strlen(Mss); + for (i = k - 1; i >= 0; i--) + { + temp[l++] = Mss[i] - '0'; + } + for (i = 0; i < l;) + { + j = temp[i] * 10 + temp[i + 1]; + if (Valid[j]) + { + printf("%c", j); + i += 2; + } + else + { + j = temp[i] * 100 + temp[i + 1] * 10 + temp[i + 2]; + printf("%c", j); + i += 3; + } + } + printf("\n"); +} +int main() +{ + Set(); + while (gets(Mss)) + { + if (isdigit(Mss[0])) + { + Encode(); + } + else + { + Decode(); + } + } + return 0; +} diff --git a/445.cpp b/445.cpp index 564177c..b3c8366 100644 --- a/445.cpp +++ b/445.cpp @@ -1,34 +1,40 @@ -#include -#include -using namespace std; -int main(){ - string str; - while(getline(cin,str,'\n')){ - if(str==""){ - cout< + +using namespace std; + +int main() +{ + char ch; + int sum = 0; + int i; + while ((ch = getc(stdin)) != EOF) + { + if (isdigit(ch)) + { + sum += ch - 48; + } + else + { + if (ch == '!') + { + printf("\n"); + } + else if (ch == '\n') + { + printf("\n"); + } + else if (ch == 'b') + for (i = 0; i < sum; i++) + { + printf(" "); + } + else + for (i = 0; i < sum; i++) + { + printf("%c", ch); + } + sum = 0; + } + } + return 0; +} diff --git a/446.cpp b/446.cpp new file mode 100644 index 0000000..f3ee9c3 --- /dev/null +++ b/446.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int n, a, b, result; + char c; + cin >> n; + while (n--) + { + cin >> hex >> a >> c >> hex >> b; + if (c == '+') + { + result = a + b; + } + else + { + result = a - b; + } + cout << bitset<13>(a) << " " << c << " " << bitset<13>(b) << " = " << result << endl; + } + return 0; +} diff --git a/447.cpp b/447.cpp new file mode 100644 index 0000000..4ff2462 --- /dev/null +++ b/447.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; + +/* +447 +Population Explosion +*/ +int Y; +char city[22][22]; +char temp[22][22]; +int cc[22][22]; +int xx[] = {0, 0, 1, -1, -1, -1, 1, 1}; +int yy[] = {1, -1, 0, 0, -1, 1, 1, -1}; +void Ini() +{ + int i, j; + for (i = 0; i < 21; i++) + { + for (j = 0; j < 21; j++) + { + temp[i][j] = ' '; + cc[i][j] = 0; + } + } +} +void ReadCase() +{ + char input[100]; + int r, c; + for (r = 0; r < 20; r++) + { + for (c = 0; c < 20; c++) + { + city[r][c] = ' '; + } + } + while (gets(input)) + { + for (int i = 0; input[i]; i++) + { + if (input[i] == '\n') + { + input[i] = NULL; + } + } + if (strlen(input) == 0) + { + break; + } + sscanf(input, "%d%d", &r, &c); + r--; + c--; + city[r][c] = 'O'; + } +} +void Print() +{ + int i; + for (i = 0; i < 20; i++) + { + printf("*"); + city[i][20] = NULL; + } + putchar('\n'); + for (i = 0; i < 20; i++) + { + printf("%s\n", city[i]); + } +} +void Copy() +{ + int i; + for (i = 0; i < 20; i++) + { + temp[i][20] = NULL; + strcpy(city[i], temp[i]); + } +} +void Mark(int r, int c) +{ + int i, nr, nc; + for (i = 0; i < 8; i++) + { + nr = r + xx[i]; + nc = c + yy[i]; + if (nr > 20 || nr < 0 || nc > 20 || nc < 0) + { + continue; + } + cc[nr][nc]++; + } +} +void Update() +{ + int i, j; + for (i = 0; i < 20; i++) + { + for (j = 0; j < 20; j++) + { + if (city[i][j] == 'O') + { + if (cc[i][j] == 2 || cc[i][j] == 3) + { + temp[i][j] = 'O'; + } + } + else + { + if (cc[i][j] == 3) + { + temp[i][j] = 'O'; + } + } + } + } +} +void Cal() +{ + int i, j; + Print(); + Y--; + while (Y--) + { + Ini(); + for (i = 0; i < 20; i++) + { + for (j = 0; j < 20; j++) + if (city[i][j] == 'O') + { + Mark(i, j); + } + } + Update(); + Copy(); + Print(); + } + for (i = 0; i < 20; i++) + { + printf("*"); + } + printf("\n"); +} +int main() +{ + char input[100]; + int kase; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + gets(input); + sscanf(input, "%d", &Y); + Ini(); + ReadCase(); + Cal(); + if (kase) + { + printf("\n"); + } + } + return 0; +} diff --git a/448.cpp b/448.cpp new file mode 100644 index 0000000..7bb98db --- /dev/null +++ b/448.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +static int next() +{ + register int c; + while ((c = getchar()) != EOF) + if (c >= '0' && c <= '9') + { + return (c - '0'); + } + else if (c >= 'A' && c <= 'F') + { + return (c - 'A' + 10); + } + else if (c >= 'a' && c <= 'f') + { + return (c - 'a' + 10); + } + return -1; +} + +int main() +{ + char *s[16] = {"ADD", "SUB", "MUL", "DIV", "MOV", "BREQ", "BRLE", + "BRLS", "BRGE", "BRGR", "BRNE", "BR", "AND", "OR", + "XOR", "NOT"}; + int n[16] = {2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1}; + int c, v, i, r; + while ((c = next()) >= 0) + { + printf("%s ", s[c]); + for (r = n[c]; r-- > 0;) + { + for (i = 0, v = 0; i < 4 && (c = next()) >= 0; i++) + { + v = (v << 4) | c; + } + c = (v >> 14) & 3; + v &= 0x3FFF; + if (c == 0) + { + printf("R%d", v); + } + else if (c == 1) + { + printf("$%d", v); + } + else if (c == 2) + { + printf("PC+%d", v); + } + else + { + printf("%d", v); + } + if (r != 0) + { + printf(","); + } + } + printf("\n"); + } + return 0; +} diff --git a/449.cpp b/449.cpp new file mode 100644 index 0000000..61f0e9e --- /dev/null +++ b/449.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +inline char next(char c) { return c == 'G' ? 'A' : ++c; } + +int main() +{ + const string note[] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"}, + scale[] = {"", "SECOND", "THIRD", "FOURTH", "FIFTH", "SIXTH", "SEVENTH", "OCTAVE"}; + string begin, major[12], in, dir, degree; + const int deg[] = {0, 2, 4, 5, 7, 9, 11, 12}; + + while (getline(cin, begin)) + { + for (int index = find(note, note + 12, begin) - note, i = 0; i < 8; ++i) + major[i] = note[(index + deg[i]) % 12]; + for (int i = 1; i < 8; ++i) + if (major[i][0] != next(major[i - 1][0])) + if (major[i].size() == 2) + major[i][0] = next(major[i - 1][0]), major[i][1] = '#'; + else + major[i][0] = next(major[i][0]), major[i].push_back('b'); + + getline(cin, in); + replace(in.begin(), in.end(), ';', ' '); + stringstream ss(in); + + cout << "Key of " << begin << endl; + while (ss >> begin >> dir >> degree) + { + int index = find(major, major + 12, begin) - major, jump = find(scale, scale + 8, degree) - scale; + if (index == 12) + cout << begin << ": invalid note for this key\n"; + else if (dir == "UP") + cout << begin << ": " << dir << ' ' << degree << " > " << major[(index + jump) % 7] << endl; + else + cout << begin << ": " << dir << ' ' << degree << " > " << major[(index - jump + 7) % 7] << endl; + } + cout << endl; + } +} diff --git a/450.cpp b/450.cpp new file mode 100644 index 0000000..c723e03 --- /dev/null +++ b/450.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +struct List +{ + //Data Information: + char Title[50], FirstName[50], LastName[50]; + char HomeAddress[100]; + char Department[100], HomePhone[100], WorkPhone[100], CampusBox[100]; +}; + +void PrintList(List p) +{ + cout << "----------------------------------------" << endl; + cout << p.Title << ' ' << p.FirstName << ' ' << p.LastName << endl; + cout << p.HomeAddress << endl; + cout << "Department: " << p.Department << endl; + cout << "Home Phone: " << p.HomePhone << endl; + cout << "Work Phone: " << p.WorkPhone << endl; + cout << "Campus Box: " << p.CampusBox << endl; +} + +int sort_function(const void *a, const void *b) +{ + List *aa, *bb; + aa = (List *)a; + bb = (List *)b; + return (strcmp(aa->LastName, bb->LastName)); +} + +int main() +{ + List test[4100]; + int n, t, i, ListNum = 0; + char data[5000], depart[5000]; + scanf("%d\n", &n); + for (t = 0; t < n; t++) + { + cin.getline(depart, 5000); + while (1) + { + cin.getline(data, 5000); + if (strcmp(data, "\0") == 0) + { + break; + } + //Seperate Information : + char *p; + strcpy(test[ListNum].Department, depart); + p = strtok(data, ","); + // p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].Title, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].FirstName, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].LastName, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].HomeAddress, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].HomePhone, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].WorkPhone, p); + } + p = strtok(NULL, ","); + if (p) + { + strcpy(test[ListNum].CampusBox, p); + } + ListNum++; + //End Seperate; + } + } + // cout< + +using namespace std; + +char tbl[6][6][3], ss[6][3]; +int rank_[300], A[6]; + +int cmp(const void *a, const void *b) +{ + return *(int *)b - *(int *)a; +} +void Ini() +{ + char tt[] = "A23456789XJQK"; + int i, j; + for (i = 0; tt[i]; i++) + { + j = tt[i]; + rank_[j] = i; + } +} +int Fourofkind() +{ + int i, k, j; + int a[10] = {0}; + for (i = 0; i < 5; i++) + { + if (a[i]) + { + continue; + } + k = 1; + for (j = i + 1; j < 5; j++) + { + if (ss[i][0] == ss[j][0]) + { + k++; + a[j] = 1; + } + } + if (k == 4) + { + return 1; + } + } + return 0; +} +int Fullhouse() +{ + int a[10] = {0}, b[10] = {0}, i, j, c; + for (i = 0; i < 5; i++) + { + if (a[i]) + continue; + c = 1; + for (j = i + 1; j < 5; j++) + { + if (ss[i][0] == ss[j][0]) + { + c++; + a[j] = 1; + } + } + b[c]++; + } + return b[3] == 1 && b[2] == 1; +} +int Flush() +{ + int i; + for (i = 1; i < 5; i++) + { + if (ss[i][1] != ss[i - 1][1]) + { + return 0; + } + } + return 1; +} +int Straight() +{ + int i, j, d = 1, c, k; + int f[15] = {0}; + for (i = 0; i < 5; i++) + { + j = ss[i][0]; + c = rank_[j]; + f[c] = 1; + k = c; + } + c = k + 1; + c %= 13; + while (f[c]) + { + d++; + c++; + c %= 13; + } + c = k - 1 + 13; + c %= 13; + while (f[c]) + { + d++; + c--; + c = (c + 13) % 13; + } + return d == 5; +} +int Threeofakind() +{ + int a[6] = {0}, i, c; + for (i = 0; i < 5; i++) + { + if (a[i]) + { + continue; + } + c = 1; + for (int j = i + 1; j < 5; j++) + { + if (ss[i][0] == ss[j][0]) + { + c++; + a[j] = 1; + } + } + if (c == 3) + { + return 1; + } + } + return 0; +} +int Twopair() +{ + int a[7] = {0}, b[7] = {0}, i, j, c; + for (i = 0; i < 5; i++) + { + if (a[i]) + { + continue; + } + c = 1; + for (j = i + 1; j < 5; j++) + { + if (ss[i][0] == ss[j][0]) + { + c++; + a[j] = 1; + } + } + b[c]++; + } + return b[2] == 2; +} +int Onepair() +{ + int a[6] = {0}, i, c; + for (i = 0; i < 5; i++) + { + if (a[i]) + { + continue; + } + c = 1; + for (int j = i + 1; j < 5; j++) + { + if (ss[i][0] == ss[j][0]) + { + c++; + a[j] = 1; + } + } + if (c == 2) + { + return 1; + } + } + return 0; +} +int Decide() +{ + int f, s; + if (Fourofkind()) + { + return 8; + } + if (Fullhouse()) + { + return 7; + } + f = Flush(); + s = Straight(); + if (s && f) + { + return 9; + } + if (f) + { + return 6; + } + if (s) + { + return 5; + } + if (Threeofakind()) + { + return 4; + } + if (Twopair()) + { + return 3; + } + if (Onepair()) + { + return 2; + } + return 1; +} +void Cal() +{ + int a[10] = {0}; + int i, j, d; + for (i = 0; i < 5; i++) + { + for (j = 0; j < 5; j++) + { + strcpy(ss[j], tbl[i][j]); + } + d = Decide(); + a[d]++; + } + for (i = 0; i < 5; i++) + { + for (j = 0; j < 5; j++) + { + strcpy(ss[j], tbl[j][i]); + } + d = Decide(); + a[d]++; + } + printf("%d", a[1]); + for (i = 2; i <= 9; i++) + { + printf(", %d", a[i]); + } + printf("\n"); +} +void ReadCase() +{ + int i, j, k = 0; + char input[100]; + while (gets(input)) + { + for (i = 0; input[i]; i++) + { + if (input[i] == '\n') + { + input[i] = NULL; + } + } + if (strlen(input) == 0) + { + break; + } + sscanf(input, "%s%s%s%s%s", tbl[k][0], tbl[k][1], tbl[k][2], tbl[k][3], tbl[k][4]); + k++; + if (k % 5 == 0) + { + Cal(); + } + } +} + +int main() +{ + int kase; + char in[100]; + gets(in); + sscanf(in, "%d", &kase); + gets(in); + Ini(); + while (kase--) + { + ReadCase(); + if (kase) + { + printf("\n"); + } + } + return 0; +} diff --git a/452.cpp b/452.cpp new file mode 100644 index 0000000..411439e --- /dev/null +++ b/452.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +#define MAXN 27 + +char color[MAXN], flag[MAXN]; +int Day[MAXN], Tday[MAXN], link[MAXN][MAXN], maxlen; + +struct node +{ + int n; + node *next; +}; +node *lst; + +void ReadCase(char str[]) +{ + int u, v, i; + char *p; + p = strtok(str, " "); + v = p[0] - 'A'; + if (v > maxlen) + { + maxlen = v; + } + flag[v] = 1; + p = strtok(NULL, " "); + Tday[v] = Day[v] = atoi(p); + p = strtok(NULL, " "); + if (p == NULL) + { + return; + } + for (i = 0; p[i] != '\0'; i++) + { + u = p[i] - 'A'; + if (u > maxlen) + { + maxlen = u; + } + link[u][v] = 1; + flag[u] = 1; + } +} +void topo(int n) +{ + int i; + color[n] = 1; + for (i = 0; i < 26; i++) + { + if (link[n][i] && !color[i]) + { + topo(i); + } + } + node *p; + p = new node; + p->next = lst; + lst = p; + p->n = n; +} +void Dfs() +{ + int i; + for (i = 0; i < 26; i++) + { + if (flag[i] && !color[i]) + { + topo(i); + } + } +} +void Count() +{ + node *x = lst; + int i, u; + while (x != NULL) + { + u = x->n; + for (i = 0; i < 26; i++) + { + if (link[u][i]) + { + if (Tday[u] + Day[i] > Tday[i]) + { + Tday[i] = Tday[u] + Day[i]; + } + } + } + x = x->next; + } +} +void Cal() +{ + int i, max = 0; + if (maxlen == -1) + { + printf("\n"); + return; + } + Dfs(); + Count(); + for (i = 0; i < 26; i++) + { + if (max < Tday[i]) + { + max = Tday[i]; + } + } + printf("%d\n", max); +} +void Reset() +{ + int i, j; + for (i = 0; i < 26; i++) + { + for (j = 0; j < 26; j++) + { + link[i][j] = 0; + } + Tday[i] = Day[i] = 0; + color[i] = flag[i] = 0; + } +} +int main() +{ + char str[100]; + int kases, i; + gets(str); + sscanf(str, "%d", &kases); + gets(str); + while (kases--) + { + lst = NULL; + maxlen = -1; + while (gets(str)) + { + for (i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = '\0'; + break; + } + } + if (strlen(str) == 0) + { + break; + } + ReadCase(str); + } + Cal(); + if (kases) + { + putchar('\n'); + Reset(); + } + } + return 0; +} diff --git a/453.cpp b/453.cpp new file mode 100644 index 0000000..01d5268 --- /dev/null +++ b/453.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define EPS 1E-4 +int main() +{ + double x1, y1, r1, x2, y2, r2; + double diff_x, diff_y, sum_r, diff_r, dis; + double X1, Y1, X2, Y2; + double a, b, c, p, q, r, t; + double cos1, cos2, t1, t2; + while (scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &r1, &x2, &y2, &r2) == 6) + { + if (fabs(x1 - x2) < EPS && fabs(y1 - y2) < EPS && fabs(r1 - r2) < EPS) + { + if (fabs(r1) < EPS && fabs(r2) < EPS) + { + printf("(%.3lf,%.3lf)\n", x1, y1); + } + else + { + printf("THE CIRCLES ARE THE SAME\n"); + } + } + else + { + diff_x = x1 - x2; + diff_y = y1 - y2; + sum_r = r1 + r2; + diff_r = r1 - r2; + dis = diff_x * diff_x + diff_y * diff_y; + if (dis > sum_r * sum_r + EPS || dis + EPS < diff_r * diff_r) + { + printf("NO INTERSECTION\n"); + } + else if (fabs(dis - sum_r * sum_r) < EPS) + { + printf("(%.3lf,%.3lf)\n", + x2 + diff_x * r2 / sum_r, + y2 + diff_y * r2 / sum_r); + } + else if (fabs(dis - diff_r * diff_r) < EPS) + { + printf("(%.3lf,%.3lf)\n", + x2 - diff_x * r2 / diff_r, + y2 - diff_y * r2 / diff_r); + } + else + { + if (fabs(x1 - x2) < EPS) + { + Y1 = ((y1 + y2) + (r2 * r2 - r1 * r1) / diff_y) / 2.0; + Y2 = Y1; + X1 = x1 - sqrt(r1 * r1 - (Y1 - y1) * (Y1 - y1)); + X2 = x1 + sqrt(r1 * r1 - (Y1 - y1) * (Y1 - y1)); + } + else if (fabs(y1 - y2) < EPS) + { + X1 = ((x1 + x2) + (r2 * r2 - r1 * r1) / diff_x) / 2.0; + X2 = X1; + Y1 = y1 - sqrt(r1 * r1 - (X1 - x1) * (X1 - x1)); + Y2 = y1 + sqrt(r1 * r1 - (X1 - x1) * (X1 - x1)); + } + else + { + a = 2.0 * r1 * diff_x; + b = 2.0 * r1 * diff_y; + c = r2 * r2 - r1 * r1 - (diff_x * diff_x + diff_y * diff_y); + p = a * a + b * b; + q = -2.0 * a * c; + r = c * c - b * b; + cos1 = (sqrt(q * q - 4 * p * r) - q) / (p * 2.0); + cos2 = (-sqrt(q * q - 4 * p * r) - q) / (p * 2.0); + X1 = x1 + r1 * cos1; + X2 = x1 + r1 * cos2; + t1 = y1 - sqrt(r1 * r1 - (X1 - x1) * (X1 - x1)); + t2 = y1 + sqrt(r1 * r1 - (X1 - x1) * (X1 - x1)); + Y1 = fabs((X1 - x2) * (X1 - x2) + (t1 - y2) * (t1 - y2) - r2 * r2) < + fabs((X1 - x2) * (X1 - x2) + (t2 - y2) * (t2 - y2) - r2 * r2) + ? t1 + : t2; + t1 = y1 - sqrt(r1 * r1 - (X2 - x1) * (X2 - x1)); + t2 = y1 + sqrt(r1 * r1 - (X2 - x1) * (X2 - x1)); + Y2 = fabs((X2 - x2) * (X2 - x2) + (t1 - y2) * (t1 - y2) - r2 * r2) < + fabs((X2 - x2) * (X2 - x2) + (t2 - y2) * (t2 - y2) - r2 * r2) + ? t1 + : t2; + } + if (X1 > X2 + EPS) + { + t = X1; + X1 = X2; + X2 = t; + t = Y1; + Y1 = Y2; + Y2 = t; + } + else if (fabs(X1 - X2) < EPS && Y1 > Y2 + EPS) + { + t = Y1; + Y1 = Y2; + Y2 = t; + } + if (fabs(X1 - 0) < 0.0001) + { + X1 = fabs(X1); + } + if (fabs(X2 - 0) < 0.0001) + { + X2 = fabs(X2); + } + if (fabs(Y1 - 0) < 0.0001) + { + Y1 = fabs(Y1); + } + if (fabs(Y2 - 0) < 0.0001) + { + Y2 = fabs(Y2); + } + printf("(%.3lf,%.3lf)(%.3lf,%.3lf)\n", X1, Y1, X2, Y2); + } + } + } + return 0; +} diff --git a/454.cpp b/454.cpp index 265dc77..b543e35 100644 --- a/454.cpp +++ b/454.cpp @@ -1,69 +1,80 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +struct anagram +{ + string word; + string chars; +}; + +bool theTruthIsOutThere(const anagram &a, const anagram &b) +{ + return a.word < b.word; +} -string rem(string S){ - sort(S.begin(), S.end()); - int L = S.length() - 1, i; - string R = ""; - FOI(i, 0, L) - if (!isspace(S[i])) - R += S[i]; - return R; +bool trustNoOne(const pair &a, const pair &b) +{ + if (a.first == b.first) + { + return a.second < b.second; + } + return a.first < b.first; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - string str; - getline(cin, str); getline(cin, str); - while (T--){ - vector< string > V; - map Map; - while (getline(cin, str)){ - if (str == "") +int main() +{ + int cases; + string input; + int count; + anagram words[110]; + vector> result; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + count = 0; + result.clear(); + while (getline(cin, input)) + { + if (input == "") + { break; - V.push_back(str); - Map[str] = rem(str); + } + words[count].word = input; + words[count].chars = ""; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] == ' ') + { + continue; + } + words[count].chars += input[i]; + } + sort(words[count].chars.begin(), words[count].chars.end(), less()); + count++; } - sort(V.begin(), V.end()); - int SZ = V.size() - 1; - int i, j; - FOI(i, 0, SZ) - FOI(j, i+1, SZ) - if (Map[V[i]] == Map[V[j]]) - cout << V[i] << " = " << V[j] << endl; - if (T) + sort(words, words + count, theTruthIsOutThere); + for (int i = 0; i < count - 1; i++) + { + for (int j = i + 1; j < count; j++) + { + if (words[i].chars == words[j].chars) + { + result.push_back(make_pair(words[i].word, words[j].word)); + } + } + } + sort(result.begin(), result.end(), trustNoOne); + for (int i = 0, sz = result.size(); i < sz; i++) + { + cout << result[i].first << " = " << result[i].second << endl; + } + if (cases) + { cout << endl; + } } return 0; } - diff --git a/455.cpp b/455.cpp index 90d1dfd..f501e4a 100644 --- a/455.cpp +++ b/455.cpp @@ -1,60 +1,45 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +#include -int GCD(int A, int B) { - if (B == 0) - return A; - else - return GCD(B, A % B); -} +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--) { - map Map; - map::iterator it; - string S; - cin >> S; - int L = S.length() - 1, i, j, k; - FOI(i, 0, L) ++Map[S[i]]; - int G = 0; - int tot = 0; - for (it = Map.begin(); it != Map.end(); it++) - G = GCD(G, (*it).second); - for (it = Map.begin(); it != Map.end(); it++) - tot += (*it).second / G; - cout << tot << endl; - if (T) cout << endl; +int main() +{ + int n; + bool found; + string input; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + cin.ignore(100, '\n'); + getline(cin, input); + for (int i = 1, sz = input.size(); i <= sz; i++) + { + if (sz % i != 0) + { + continue; + } + string t = string(input.begin(), input.begin() + i); + string tmp = ""; + for (int j = 0; j < sz / i; j++) + { + tmp += t; + } + if (tmp == input) + { + found = true; + cout << i << endl; + break; + } + } + if (!found) + { + cout << input.size() << endl; + } + if (n) + { + cout << endl; + } } return 0; } diff --git a/456.cpp b/456.cpp new file mode 100644 index 0000000..03a4717 --- /dev/null +++ b/456.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int main() +{ + char s[105]; + while (gets(s)) + { + int row[6] = {}; + int i, j, k, p; + for (i = 0; s[i]; i++) + { + p = s[i] - '0'; + for (j = 0; j <= 6 - p; j++) + { + if (row[j] != 20) + { + for (k = 1; k < p; k++) + if (row[j + k] != row[j]) + break; + if (k == p) + { + for (k = 0; k < p; k++) + row[j + k]++; + j = 10;//accept + break; + } + } + } + if (j == 10) + continue; + for (j = 0; j < 6; j++) + { + if (row[j] + p <= 20) + { + row[j] += p; + break; + } + } + if (j == 6)//not accept + break; // full + } + int sum = 0; + for (j = 0; j < 6; j++) + sum += row[j]; + printf("%d %d\n", i, 120 - sum); + } + return 0; +} diff --git a/457.cpp b/457.cpp index d74824d..9c0a06c 100644 --- a/457.cpp +++ b/457.cpp @@ -1,46 +1,41 @@ -#include -#include -using namespace std; +#include -int DNA[11]; -int mat[51][41]; +using namespace std; -void init() { - memset(DNA, 0, sizeof DNA); - memset(mat[0], 0, sizeof mat[0]); - mat[0][19] = 1; -} +const char CH[] = {' ', '.', 'x', 'W'}; -void printMatrix() { - char C[4] = {' ', '.', 'x', 'W'}; - for (int i = 0; i < 50; i++) { - for (int j = 0; j < 40; j++) { - printf("%c", C[mat[i][j]]); +int main() +{ + int kase, first = 1; + int dna[10], disk[2][50]; + scanf("%d", &kase); + while (kase--) + { + if (!first) + { + printf("\n"); } - printf("\n"); - } -} - -int main() { - int T; - scanf("%d", &T); - while (T--) { - init(); - - for (int i = 0; i < 10; i++) { - scanf("%d", &DNA[i]); + first = 0; + for (int i = 0; i < 10; i++) + { + scanf("%d", dna + i); } - for (int i = 1; i < 50; i++) { - for (int j = 0; j < 40; j++) { - int K = mat[i - 1][j]; - if (j > 0) K += mat[i - 1][j - 1]; - if (j < 39)K += mat[i - 1][j + 1]; - mat[i][j] = DNA[K]; + int now = 0; + memset(disk, 0, sizeof(disk)); + disk[now][20] = 1; + for (int i = 0; i < 50; i++) + { + for (int j = 1; j <= 40; j++) + { + printf("%c", CH[disk[now][j]]); + } + printf("\n"); + now = 1 - now; + for (int j = 1; j <= 40; j++) + { + disk[now][j] = dna[disk[1 - now][j] + disk[1 - now][j - 1] + disk[1 - now][j + 1]]; } } - - printMatrix(); - if (T) printf("\n"); } return 0; } diff --git a/458.cpp b/458.cpp index ab1adea..1e92c3e 100644 --- a/458.cpp +++ b/458.cpp @@ -1,18 +1,18 @@ -#include -#include -#include -using namespace std; - -int main() -{ - string str; - while(getline(cin, str, '\n')) - { - for(int i=0;i + +using namespace std; + +int main() +{ + string input; + while (getline(cin, input)) + { + for (int i = 0; i < input.size(); i++) + if (input[i] != ' ') + { + input[i] = input[i] - 7; + } + cout << input << endl; + } + return 0; +} diff --git a/459.cpp b/459.cpp index 6948ed4..054c89d 100644 --- a/459.cpp +++ b/459.cpp @@ -1,103 +1,70 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +const int MAXSIZE = 30; -int N; -char adjMat[30][30]; -bool visit[30][30]; +bool graph[MAXSIZE][MAXSIZE]; +bool visit[MAXSIZE]; +int n; -string rem(string s){ - int L = s.length(), i; - string str = ""; - FOI(i, 0, L-1){ - if( isalpha(s[i]) ) str += s[i]; - } - return str; +void input() +{ + memset(graph, 0, sizeof(graph)); + memset(visit, 0, sizeof(visit)); + char c1, c2, str[5]; + cin >> c1; + cin.get(); + n = c1 - 'A' + 1; + while (cin.getline(str, 5) && strcmp(str, "\0")) + { + c1 = str[0]; + c2 = str[1]; + int a = c1 - 'A'; + int b = c2 - 'A'; + graph[a][b] = graph[b][a] = true; + } } -void floodFill(int X, int Y){ - if ( X < 0 || Y < 0 || X >= N || Y >= N ) return; - if ( !visit[X][Y] ) return; - - visit[X][Y] = false; - - int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; - int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; - int j; - FOI(j, 0, 7) - floodFill(X + dx[j], Y + dy[j]); +void dfs(int node) +{ + if (visit[node]) + { + return; + } + visit[node] = true; + for (int i = 0; i < n; i++) + if (!visit[i] && graph[node][i]) + { + dfs(i); + } } -int main(int argc, char **argv){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int T; - scanf("%d\n", &T); - cout << T << endl; - while( T-- ){ - char ch; - int i, j; - int comp = 0; - string str; - getline(cin, str); - str = rem(str); - cout << str << endl; - N = str[0] - 'A' + 1; - memset( visit, false, sizeof (visit) ); - while( getline(cin, str) ){ - str = rem(str); - cout << str << endl; - if( str == "" ) break; - - visit[str[0]-'A'][str[1]-'A'] = true; - visit[str[1]-'A'][str[0]-'A'] = true; +void solve() +{ + int cnt = 0; + for (int i = 0; i < n; i++) + if (!visit[i]) + { + cnt++; + dfs(i); } - FOI(i, 0, N-1){ - FOI(j, 0, N-1) - cout << visit[i][j] << " "; - cout << endl; - } - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - if( visit[i][j] ){ - comp++; - floodFill(i, j); - } - } - } - cout << comp << endl; - if ( T > 0 ) + cout << cnt << endl; +} + +int main() +{ + int numOfCase; + cin >> numOfCase; + cin.get(); + for (int kase = 1; kase <= numOfCase; kase++) + { + input(); + solve(); + if (kase != numOfCase) + { cout << endl; + } } return 0; } diff --git a/460.cpp b/460.cpp new file mode 100644 index 0000000..e19e374 --- /dev/null +++ b/460.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +#undef MIN +#define MIN(a, b) (((a) <= (b)) ? (a) : (b)) + +#undef MAX +#define MAX(a, b) (((a) >= (b)) ? (a) : (b)) + +int main() +{ + static int ax, ay, bx, by, cx, cy, dx, dy, t; + scanf("%d", &t); + while (t-- > 0 && scanf("%d %d %d %d %d %d %d %d", &ax, &ay, &bx, &by, &cx, &cy, &dx, &dy) == 8) + { + ax = MAX(ax, cx); + bx = MIN(bx, dx); + ay = MAX(ay, cy); + by = MIN(by, dy); + if (ax >= bx || ay >= by) + { + printf("No Overlap\n"); + } + else + { + printf("%d %d %d %d\n", ax, ay, bx, by); + } + if (t > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/462.cpp b/462.cpp new file mode 100644 index 0000000..c014881 --- /dev/null +++ b/462.cpp @@ -0,0 +1,220 @@ +#include + +using namespace std; + +enum suit +{ + SPADES, + HEARTS, + DIAMONDS, + CLUBS +}; + +struct hand +{ + int points; + int alternative_points; + bool cards[4][15]; + hand() : points(0), alternative_points(0) + { + for (int i = 0; i < 4; i++) + { + fill(cards[i], cards[i] + 15, false); + } + } +}; + +suit char2suit(char c) +{ + switch (c) + { + case 'S': + return SPADES; + case 'H': + return HEARTS; + case 'D': + return DIAMONDS; + case 'C': + return CLUBS; + } +} + +int char2value(char c) +{ + switch (c) + { + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return int(c - '0'); + case 'T': + return 10; + case 'J': + return 11; + case 'Q': + return 12; + case 'K': + return 13; + case 'A': + return 14; + } +} + +bool find_card(const hand &h, int s, char _v, int n) +{ + int v = char2value(_v); + int count = 0; + for (int i = 2; i <= 14; i++) + { + if (h.cards[s][i]) + { + count++; + } + } + return count <= n && h.cards[s][v]; +} + +bool find_card2(const hand &h, int s, char _v, int n) +{ + int v = char2value(_v); + int count = 0; + for (int i = 2; i <= 14; i++) + { + if (h.cards[s][i]) + { + count++; + } + } + return count >= n && h.cards[s][v]; +} + +bool exact_suit(const hand &h, int s, int n) +{ + int count = 0; + for (int i = 2; i <= 14; i++) + if (h.cards[s][i]) + { + count++; + } + return count == n; +} + +bool is_stopped(const hand &h, int s) +{ + return h.cards[s][char2value('A')] || find_card2(h, s, 'K', 2) || find_card2(h, s, 'Q', 3); +} + +char best_suit(const hand &h) +{ + pair best; + best.first = best.second = 0; + for (int i = 0; i < 4; i++) + { + int count = 0; + for (int j = 2; j <= 14; j++) + if (h.cards[i][j]) + { + count++; + } + if (count > best.first) + { + best.first = count; + best.second = i; + } + } + switch (best.second) + { + case 0: + return 'S'; + case 1: + return 'H'; + case 2: + return 'D'; + case 3: + return 'C'; + } +} + +int main() +{ + char v, s; + while (cin >> v) + { + cin >> s; + hand input; + input.cards[char2suit(s)][char2value(v)] = true; + if (char2value(v) > 10) + { + input.points += char2value(v) - 10; + } + for (int i = 0; i < 12; i++) + { + cin >> v >> s; + input.cards[char2suit(s)][char2value(v)] = true; + if (char2value(v) > 10) + { + input.points += char2value(v) - 10; + } + } + input.alternative_points = input.points; + for (int i = 0; i < 4; i++) + { + if (find_card(input, i, 'K', 1)) + { + input.points -= 1; + input.alternative_points -= 1; + } + if (find_card(input, i, 'Q', 2)) + { + input.points -= 1; + input.alternative_points -= 1; + } + if (find_card(input, i, 'J', 3)) + { + input.points -= 1; + input.alternative_points -= 1; + } + if (exact_suit(input, i, 2)) + { + input.points += 1; + } + if (exact_suit(input, i, 1)) + { + input.points += 2; + } + if (exact_suit(input, i, 0)) + { + input.points += 2; + } + } + if (input.points < 14) + { + cout << "PASS" << endl; + continue; + } + if (input.alternative_points >= 16) + { + bool ok = true; + for (int i = 0; i < 4; i++) + { + if (!is_stopped(input, i)) + { + ok = false; + break; + } + } + if (ok) + { + cout << "BID NO-TRUMP" << endl; + continue; + } + } + cout << "BID " << best_suit(input) << endl; + } + return 0; +} diff --git a/464.cpp b/464.cpp new file mode 100644 index 0000000..76a7f1f --- /dev/null +++ b/464.cpp @@ -0,0 +1,288 @@ +#include + +using namespace std; + +void Sub_sentence(void); +void Sub_trans_sentence(void); +void Sub_intrans_sentence(void); +void Sub_subject(void); +void Sub_object(void); +void Sub_noun_phrase(void); +void Sub_modified_noun(void); +void Sub_modifier(void); +void Sub_verb_phrase(void); +void Sub_intrans_verb_phrase(void); +void Sub_prep_phrase(void); +void Sub_noun(void); +void Sub_trans_verb(void); +void Sub_intrans_verb(void); +void Sub_article(void); +void Sub_adjective(void); +void Sub_adverb(void); +void Sub_preposition(void); +void Sub_empty(void); + +int cnt = 0, word = 0; + +void Sub_sentence(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_trans_sentence(); + break; + case 1: + Sub_intrans_sentence(); + break; + } +} + +void Sub_trans_sentence(void) +{ + // + Sub_subject(); + Sub_verb_phrase(); + Sub_object(); + Sub_prep_phrase(); +} + +void Sub_intrans_sentence(void) +{ + // + Sub_subject(); + Sub_intrans_verb_phrase(); + Sub_prep_phrase(); +} + +void Sub_subject(void) +{ + // + Sub_noun_phrase(); +} + +void Sub_object(void) +{ + // + Sub_noun_phrase(); +} + +void Sub_noun_phrase(void) +{ + //
+ Sub_article(); + Sub_modified_noun(); +} + +void Sub_modified_noun(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_noun(); + break; + case 1: + Sub_modifier(); + Sub_noun(); + break; + } +} + +void Sub_modifier(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_adjective(); + break; + case 1: + Sub_adverb(); + Sub_adjective(); + break; + } +} + +void Sub_verb_phrase(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_trans_verb(); + break; + case 1: + Sub_adverb(); + Sub_trans_verb(); + break; + } +} + +void Sub_intrans_verb_phrase(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_intrans_verb(); + break; + case 1: + Sub_adverb(); + Sub_intrans_verb(); + break; + } +} + +void Sub_prep_phrase(void) +{ + // | + switch ((cnt++) % 2) + { + case 0: + Sub_preposition(); + Sub_noun_phrase(); + break; + // case 1: Sub_empty(); break; + } +} + +void Sub_noun(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //man | dog | fish | computer | waves + char *out[] = {"man", "dog", "fish", "computer", "waves"}; + printf(out[(cnt++) % 5]); +} + +void Sub_trans_verb(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //struck | saw | bit | took + char *out[] = {"struck", "saw", "bit", "took"}; + printf(out[(cnt++) % 4]); +} + +void Sub_intrans_verb(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //slept | jumped | walked | swam + char *out[] = {"slept", "jumped", "walked", "swam"}; + printf(out[(cnt++) % 4]); +} + +void Sub_article(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //the | a + char *out[] = {"the", "a"}; + printf(out[(cnt++) % 2]); +} + +void Sub_adjective(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //green | small | rabid | quick + char *out[] = {"green", "small", "rabid", "quick"}; + printf(out[(cnt++) % 4]); +} + +void Sub_adverb(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //nearly | suddenly | restlessly + char *out[] = {"nearly", "suddenly", "restlessly"}; + printf(out[(cnt++) % 3]); +} + +void Sub_preposition(void) +{ + if (word) + { + printf(" "); + } + else + { + word++; + } + //on | over | through + char *out[] = {"on", "over", "through"}; + printf(out[(cnt++) % 3]); +} + +void Sub_empty(void) +{ +} + +int main() +{ + char line[32]; + char *left[] = {"sentence", "trans-sentence", "intrans-sentence", + "subject", "object", "noun-phrase", "modified-noun", "modifier", + "verb-phrase", "intrans-verb-phrase", "prep-phrase", "noun", + "trans-verb", "intrans-verb", "article", "adjective", "adverb", + "preposition", "empty"}; + void (*func[])(void) = {Sub_sentence, Sub_trans_sentence, + Sub_intrans_sentence, Sub_subject, Sub_object, Sub_noun_phrase, + Sub_modified_noun, Sub_modifier, Sub_verb_phrase, + Sub_intrans_verb_phrase, Sub_prep_phrase, Sub_noun, Sub_trans_verb, + Sub_intrans_verb, Sub_article, Sub_adjective, Sub_adverb, + Sub_preposition, Sub_empty}; + cnt = 1; + while (scanf("%s", line) == 1) + { + int i; + for (i = 0; i < 19; i++) + if (strcmp(line, left[i]) == 0) + { + break; + } + // if (i==19) while(1); //debug..no + word = 0; + func[i](); + printf("\n"); + } + return 0; +} diff --git a/465.cpp b/465.cpp new file mode 100644 index 0000000..d1aa4b9 --- /dev/null +++ b/465.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +void get(char *p, double &x) +{ + int i, size; + for (i = 0; p[i] != '\0'; i++) + ; + size = i; + char tmp; + double result = 0; + for (i = 0; i < size / 2; i++) + { + tmp = p[i]; + p[i] = p[size - 1 - i]; + p[size - 1 - i] = tmp; + } + for (i = 0; i < size; i++) + { + result += ((int)p[i] - 48) * pow(10, i); + } + x = result; +} +int main() +{ + double a, b; + char ch, oa[1000], ob[1000]; + while (cin >> oa >> ch >> ob) + { + cout << oa << " " << ch << " " << ob << endl; + get(oa, a); + get(ob, b); + if (a > 2147483647.0) + { + cout << "first number too big" << endl; + } + if (b > 2147483647.0) + { + cout << "second number too big" << endl; + } + if (ch == '+') + { + double result = a + b; + if (result > 2147483647.0) + { + cout << "result too big" << endl; + } + } + if (ch == '*') + { + double result = a * b; + if (result > 2147483647.0) + { + cout << "result too big" << endl; + } + } + } + return 0; +} diff --git a/466.cpp b/466.cpp new file mode 100644 index 0000000..49787ec --- /dev/null +++ b/466.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +bool is_same_(char field[][10], char modified[][10], int n) +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (field[i][j] != modified[i][j]) + { + return false; + } + } + } + return true; +} + +void rotate(char modified[][10], int n) +{ + char tmp[10][10]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + tmp[i][j] = modified[i][j]; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + modified[i][j] = tmp[n - j - 1][i]; + } + } +} + +void reflect(char modified[][10], int n) +{ + char tmp[10][10]; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + tmp[i][j] = modified[i][j]; + } + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + modified[i][j] = tmp[n - i - 1][j]; + } + } +} + +int main() +{ + int n; + char c; + int casenum = 0; + char field[10][10]; + char modified[10][10]; + while (cin >> n) + { + casenum++; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> field[i][j]; + } + for (int j = 0; j < n; j++) + { + cin >> modified[i][j]; + } + } + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was preserved." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was rotated 90 degrees." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was rotated 180 degrees." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was rotated 270 degrees." << endl; + continue; + } + rotate(field, n); + reflect(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was reflected vertically." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was reflected vertically and rotated 90 degrees." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was reflected vertically and rotated 180 degrees." << endl; + continue; + } + rotate(field, n); + if (is_same_(field, modified, n)) + { + cout << "Pattern " << casenum << " was reflected vertically and rotated 270 degrees." << endl; + continue; + } + cout << "Pattern " << casenum << " was improperly transformed." << endl; + } + return 0; +} diff --git a/467.cpp b/467.cpp new file mode 100644 index 0000000..611b4bc --- /dev/null +++ b/467.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() + +const int lim = 60 * 60 + 1; + +int table[lim]; + +void fillt(int val, int n) +{ + for (int i = 0; i < lim; i++) + { + if ((i / val) % 2 == 0 && i % val < val - 5) + { + table[i] |= (1 << n); + } + } +} + +main() +{ + int tc = 1; + string in; + while (getline(cin, in)) + { + rep(i, lim) table[i] = 0; + int n = 0, sig; + int mini = lim; + stringstream sin(in); + while (sin >> sig) + { + mini = min(sig, mini); + fillt(sig, n); + n++; + } + int ans = -1; + REP(i, mini, lim) + { + if (table[i] == (1 << n) - 1) + { + ans = i; + break; + } + } + cout << "Set " << tc++; + if (ans != -1) + { + printf(" synchs again at %d minute(s) and %d second(s) after all turning green.\n", ans / 60, ans % 60); + } + else + { + puts(" is unable to synch after one hour."); + } + } +} diff --git a/468.cpp b/468.cpp new file mode 100644 index 0000000..20e7791 --- /dev/null +++ b/468.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second > b.second; +} + +int main() +{ + int cases; + string input, encoded, decoded; + cin >> cases; + // \n after cases + getline(cin, input); + while (cases--) + { + // blank line + getline(cin, input); + getline(cin, decoded); + getline(cin, encoded); + input = encoded; + sort(decoded.begin(), decoded.end()); + sort(encoded.begin(), encoded.end()); + vector> decoded_occurrences; + vector> encoded_occurrences; + int count = 1; + for (int i = 1, sz = decoded.size(); i < sz; i++) + { + if (decoded[i] == decoded[i - 1]) + { + count++; + } + else + { + if (decoded[i - 1] >= 'a' && decoded[i - 1] <= 'z' || decoded[i - 1] >= 'A' && decoded[i - 1] <= 'Z') + { + decoded_occurrences.push_back(make_pair(decoded[i - 1], count)); + } + count = 1; + } + } + if (decoded[decoded.size() - 1] >= 'a' && decoded[decoded.size() - 1] <= 'z' || decoded[decoded.size() - 1] >= 'A' && decoded[decoded.size() - 1] <= 'Z') + { + decoded_occurrences.push_back(make_pair(decoded[decoded.size() - 1], count)); + } + count = 1; + for (int i = 0, sz = encoded.size(); i < sz; i++) + { + if (encoded[i] == encoded[i - 1]) + { + count++; + } + else + { + if (encoded[i - 1] >= 'a' && encoded[i - 1] <= 'z' || encoded[i - 1] >= 'A' && encoded[i - 1] <= 'Z') + { + encoded_occurrences.push_back(make_pair(encoded[i - 1], count)); + } + count = 1; + } + } + if (encoded[encoded.size() - 1] >= 'a' && encoded[encoded.size() - 1] <= 'z' || encoded[encoded.size() - 1] >= 'A' && encoded[encoded.size() - 1] <= 'Z') + { + encoded_occurrences.push_back(make_pair(encoded[encoded.size() - 1], count)); + } + sort(decoded_occurrences.begin(), decoded_occurrences.end(), theTruthIsOutThere); + sort(encoded_occurrences.begin(), encoded_occurrences.end(), theTruthIsOutThere); + map decoder; + for (int i = 0, sz = encoded_occurrences.size(); i < sz; i++) + { + decoder[encoded_occurrences[i].first] = decoded_occurrences[i].first; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + cout << decoder[input[i]]; + } + cout << endl; + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/469.cpp b/469.cpp index 6e0d93f..3da423b 100644 --- a/469.cpp +++ b/469.cpp @@ -1,89 +1,137 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -string mat[105]; -bool used[105][105]; -int R = 0, C = 0; +int n, m; +int num; +char field[100][100]; +bool visited[100][100]; -int dfs(int r, int c){ - if( r < 0 || r >= R ) - return 0; - if( c < 0 || c >= C ) +int bfs(int a, int b) +{ + int counter = 0; + pair tmp; + queue> q; + tmp.first = a; + tmp.second = b; + if (field[a][b] != 'W') + { return 0; - if( !used[r][c] ) - return 0; - - used[r][c] = false; - - return 1 + dfs(r-1,c-1) + dfs(r-1,c) + dfs(r-1,c+1) + dfs(r,c+1) + dfs(r+1,c+1) + dfs(r+1,c) + dfs(r+1,c-1) + dfs(r,c-1); + } + counter++; + visited[a][b] = true; + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && !visited[tmp.first - 1][tmp.second] && field[tmp.first - 1][tmp.second] == 'W') + { + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + counter++; + } + if (tmp.second > 0 && !visited[tmp.first][tmp.second - 1] && field[tmp.first][tmp.second - 1] == 'W') + { + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + counter++; + } + if (tmp.first < n - 1 && !visited[tmp.first + 1][tmp.second] && field[tmp.first + 1][tmp.second] == 'W') + { + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + counter++; + } + if (tmp.second < m - 1 && !visited[tmp.first][tmp.second + 1] && field[tmp.first][tmp.second + 1] == 'W') + { + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + counter++; + } + if (tmp.first > 0 && tmp.second > 0 && !visited[tmp.first - 1][tmp.second - 1] && field[tmp.first - 1][tmp.second - 1] == 'W') + { + visited[tmp.first - 1][tmp.second - 1] = true; + q.push(make_pair(tmp.first - 1, tmp.second - 1)); + counter++; + } + if (tmp.first < n - 1 && tmp.second > 0 && !visited[tmp.first + 1][tmp.second - 1] && field[tmp.first + 1][tmp.second - 1] == 'W') + { + visited[tmp.first + 1][tmp.second - 1] = true; + q.push(make_pair(tmp.first + 1, tmp.second - 1)); + counter++; + } + if (tmp.first > 0 && tmp.second < m - 1 && !visited[tmp.first - 1][tmp.second + 1] && field[tmp.first - 1][tmp.second + 1] == 'W') + { + visited[tmp.first - 1][tmp.second + 1] = true; + q.push(make_pair(tmp.first - 1, tmp.second + 1)); + counter++; + } + if (tmp.first < n - 1 && tmp.second < m - 1 && !visited[tmp.first + 1][tmp.second + 1] && field[tmp.first + 1][tmp.second + 1] == 'W') + { + visited[tmp.first + 1][tmp.second + 1] = true; + q.push(make_pair(tmp.first + 1, tmp.second + 1)); + counter++; + } + } + return counter; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - bool stat = false; - int T; - int i, j; - cin >> T; - if( T <= 0 ) - return 0; - getline(cin, str); - getline(cin, str); - while( T-- ){ - if( stat ) - cout << endl; - while( true ){ - getline(cin, str); - if( str[0] != 'L' && str[0] != 'W' ){ +int main() +{ + int cases; + string input; + int a, b; + stringstream ss; + map result; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + n = 0; + m = 0; + num = 1; + result.clear(); + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + { + visited[i][j] = false; + field[i][j] = 'L'; + } + while (getline(cin, input)) + { + if (input == "") + { break; } - mat[R++] = str; - } - C = mat[0].length(); - do{ - FOI(i, 0, R-1){ - FOI(j, 0, C-1){ - if( mat[i][j] == 'W' ) - used[i][j] = true; - else - used[i][j] = false; + if (input[0] == 'L' || input[0] == 'W') + { + for (int i = 0, sz = input.size(); i < sz; i++) + { + field[n][i] = input[i]; } + n++; + m = input.size(); + } + else + { + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + { + visited[i][j] = false; + } + ss << input; + ss >> a >> b; + ss.str(""); + ss.clear(); + cout << bfs(a - 1, b - 1) << endl; } - stringstream ss( str ); - int r, c; - ss >> r >> c; - r--; c--; - cout << dfs(r, c) << endl; - }while( getline(cin, str) && str.length() > 0 ); - stat = true; + } + if (cases) + { + cout << endl; + } } return 0; } diff --git a/471.cpp b/471.cpp new file mode 100644 index 0000000..fda9e31 --- /dev/null +++ b/471.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +bool fits(long long n) +{ + int nums[10] = {0}; + while (n) + { + nums[n % 10]++; + if (nums[n % 10] > 1) + { + return false; + } + n /= 10; + } + return true; +} + +int main() +{ + int t; + long long s1, s2, n, limit = 9999999999LL; + cin >> t; + while (t--) + { + cin >> n; + s1 = n; + s2 = 1; + while (true) + { + if (fits(s1) && fits(s2)) + { + cout << s1 << " / " << s2 << " = " << n << endl; + } + s1 += n; + s2++; + if (s1 > limit) + { + break; + } + } + if (t) + { + cout << endl; + } + } + return 0; +} diff --git a/472.cpp b/472.cpp new file mode 100644 index 0000000..04468f6 --- /dev/null +++ b/472.cpp @@ -0,0 +1,190 @@ +#include + +double A[205][205], B[205], x[205], y[205]; +int pi[205], n; + +int LUP_Decomp() +{ + int i, j, k, k1; + double p, absol; + for (i = 0; i < n; i++) + { + pi[i] = i; + } + for (k = 0; k < n; k++) + { + p = 0; + for (i = k, k1 = i; i < n; i++) + { + absol = A[i][k]; + if (absol < 0) + { + absol = -absol; + } + if (absol > p) + { + p = absol, k1 = i; + } + } + if (fabs(p) < 1e-6) + { + return 0; + } + if (k != k1) + { + pi[k] ^= pi[k1] ^= pi[k] ^= pi[k1]; + } + for (i = 0; i < n; i++) + { + p = A[k][i]; + A[k][i] = A[k1][i]; + A[k1][i] = p; + } + for (i = k + 1; i < n; i++) + { + A[i][k] = A[i][k] / A[k][k]; + for (j = k + 1; j < n; j++) + { + A[i][j] = A[i][j] - A[i][k] * A[k][j]; + } + } + } + return 1; +} + +void LUP_Solve() +{ + int i, j; + char one[50], two[50]; + if (LUP_Decomp() == 0) + { + puts("No solution"); + return; + } + for (i = 0; i < n; i++) + { + y[i] = 0; + for (j = 0; j < i; j++) + { + y[i] = y[i] + A[i][j] * y[j]; + } + y[i] = B[pi[i]] - y[i]; + } + for (i = n - 1; i + 1; i--) + { + x[i] = 0; + for (j = i + 1; j < n; j++) + { + x[i] = x[i] + A[i][j] * x[j]; + } + x[i] = (y[i] - x[i]) / A[i][i]; + } + for (i = 0; i < n; i += 2) + { + sprintf(one, "%.1lf", x[i]); + sprintf(two, "%.1lf", x[i + 1]); + if (strcmp(one, "-0.0") == 0) + { + strcpy(one, "0.0"); + } + if (strcmp(two, "-0.0") == 0) + { + strcpy(two, "0.0"); + } + printf("(%s,%s)\n", one, two); + } +} + +int main() +{ + char inp[100000], *ptr, *ptr1; + double temp[205]; + int i, j, k, kase = 0; + n = 0; + while (gets(inp)) + { + for (ptr = inp, j = 0; *ptr; ptr++) + { + if (*ptr == ' ' || *ptr == '(' || *ptr == ')' || *ptr == ',') + { + continue; + } + else + { + for (ptr1 = ptr; *ptr1 != ' ' && *ptr1 != '(' && *ptr1 != ')' && *ptr1 != ','; ptr1++) + ; + *ptr1 = 0; + temp[j++] = atof(ptr); + ptr = ptr1; + } + } + for (i = 0; i + 2 < j; i += 2) + { + A[n + 1][i + 1] = A[n][i] = temp[i]; + A[n + 1][i] = temp[i + 1], A[n][i + 1] = -temp[i + 1]; + } + B[n] = temp[i], B[n + 1] = temp[i + 1]; + n += 2; + for (k = 2; k + 2 < j; k += 2) + { + gets(inp); + for (ptr = inp, j = 0; *ptr; ptr++) + { + if (*ptr == ' ' || *ptr == '(' || *ptr == ')' || *ptr == ',') + { + continue; + } + else + { + for (ptr1 = ptr; *ptr1 != ' ' && *ptr1 != '(' && *ptr1 != ')' && *ptr1 != ','; ptr1++) + ; + *ptr1 = 0; + temp[j++] = atof(ptr); + ptr = ptr1; + } + } + for (i = 0; i + 2 < j; i += 2) + { + A[n + 1][i + 1] = A[n][i] = temp[i]; + A[n + 1][i] = temp[i + 1], A[n][i + 1] = -temp[i + 1]; + } + B[n] = temp[i], B[n + 1] = temp[i + 1]; + n += 2; + } + if (kase) + { + puts(""); + } + if (n != i) + { + puts("No solution"); + } + else + { + LUP_Solve(); + } + n = 0; + kase++; + if (getchar() == EOF) + { + break; + } + } + return 0; +} +/* +(1,0) (2,0) (3,0) (14,0) +(2,0) (3,0) (4,0) (20,0) +(3,0) (4,0) (4,0) (23,0) + +(1,0) (2,0) (3,0) (4,0) +(2,0) (4,0) (6,0) (8,0) +(3,0) (4,0) (5,0) (26,0) + +(1,0) (2,0) (3,0) (14, 0) +(1,0) (1,0) (1,0) (06,0) +(1,0) (1,0) (-1, 0) (0,0) + +(1, 9) (6, -3) (-9, 4) +(-11, -1) ( 7, 5) (16, -36) +*/ diff --git a/473.cpp b/473.cpp new file mode 100644 index 0000000..636e80e --- /dev/null +++ b/473.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +int dp[800][100][100]; +int songs[800]; +int max(int a, int b) +{ + return (a > b) ? a : b; +} +int main() +{ + int i, j, k, n, t, m, TC; + int x, y, z; + scanf("%d", &TC); + while (TC-- > 0) + { + int top = 0; + scanf("%d %d %d", &n, &t, &m); + scanf("%d", songs + top++); + for (i = 1; i < n; i++) + { + scanf(", %d", songs + top++); + } + for (i = 0; i <= n; i++) + for (j = 0; j <= m; j++) + for (k = 0; k <= t; k++) + { + dp[i][j][k] = 0; + } + for (i = n - 1; i >= 0; i--) + for (j = m; j >= 0; j--) + for (k = t; k >= 0; k--) + { + dp[i][j][k] = dp[i + 1][j][k]; + if (k >= songs[i]) + { + dp[i][j][k] = max(dp[i][j][k], 1 + dp[i + 1][j][k - songs[i]]); + } + if (j > 0) + { + dp[i][j][k] = max(dp[i][j][k], 1 + dp[i + 1][j - 1][t - songs[i]]); + } + } + int optimal = 0; + for (i = 0; i < n; i++) + { + optimal = max(optimal, dp[i][m][0]); + } + printf("%d\n", optimal); + if (TC) + { + putchar('\n'); + } + } + return 0; +} diff --git a/474.cpp b/474.cpp new file mode 100644 index 0000000..6343d64 --- /dev/null +++ b/474.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main() +{ + double digit, n, y; + while (scanf("%lf", &n) == 1) + { + digit = floor(log10(2) * n + 1); + printf("2^-%.0lf = %.3lfe-%.0lf\n", n, pow(2, (digit / log10(2) - n)), digit); + } + return 0; +} diff --git a/475.cpp b/475.cpp new file mode 100644 index 0000000..4b64022 --- /dev/null +++ b/475.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int a, b, c, d, e, f, g, h, i, j; +char k[21], x[21], y[21], s; +int abcc(int p, int q) +{ + int z, r; + for (p; p < b; p++) + { + if (y[p] == k[q]) + { + r = d; + for (z = q + 1, d = 0; z < a; z++) + { + if (k[z] != '*') + { + break; + } + d = 1; + } + if (z == a) + { + if (p == b - 1 || d == 1) + { + if (h == 0) + { + if (s) + { + printf("\n"); + } + s = 1; + printf("MATCHES FOR THE PATTERN: %s\n", k); + h++; + } + printf("%s\n", y); + return 1; + } + } + z = abcc(p + 1, z); + d = r; + if (z == 1) + { + return z; + } + } + if (d == 1) + { + continue; + } + else + { + return 0; + } + } + return 0; +} +int main() +{ + while (gets(k)) + { + a = strlen(k); + h = 0; + while (gets(y)) + { + b = strlen(y); + if (b == 0) + { + break; + } + for (c = 0, d = 0; c < a; c++) + { + if (k[c] != '*') + { + break; + } + d = 1; + } + if (c == a) + { + if (h == 0) + { + if (s) + { + printf("\n"); + } + s = 1; + printf("MATCHES FOR THE PATTERN: %s\n", k); + h++; + } + printf("%s\n", y); + continue; + } + e = 0; + abcc(0, c); + } + } + return 0; +} diff --git a/476.cpp b/476.cpp new file mode 100644 index 0000000..01c86f4 --- /dev/null +++ b/476.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +enum +{ + MAX_REC = 10, + X1 = 0, + Y1 = 1, + X2 = 2, + Y2 = 3 +}; +int main() +{ + int numRec = 0; + double rec[MAX_REC][4]; + char c; + while (scanf(" %c", &c)) + { + if (c == '*') + { + break; + } + scanf("%lf %lf %lf %lf", + &rec[numRec][X1], &rec[numRec][Y1], &rec[numRec][X2], &rec[numRec][Y2]); + numRec++; + } + double x, y; + int numPoint = 1; + int outRec; + while (scanf(" %lf %lf", &x, &y)) + { + if (x == 9999.9 && y == 9999.9) + { + break; + } + outRec = 1; + for (int i = 0; i < numRec; ++i) + { + if (x > rec[i][X1] && y < rec[i][Y1] && + x < rec[i][X2] && y > rec[i][Y2]) + { + printf("Point %d is contained in figure %d\n", numPoint, i + 1); + outRec = 0; + } + } + if (outRec) + { + printf("Point %d is not contained in any figure\n", numPoint); + } + numPoint++; + } + return 0; +} diff --git a/477.cpp b/477.cpp new file mode 100644 index 0000000..13beabe --- /dev/null +++ b/477.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +class Rect +{ +public: + double x1, y1, x2, y2; +}; + +class Circle +{ +public: + double x, y, r; +}; + +class Point +{ +public: + double x, y; +}; + +bool cmp1(double x, double y, Rect &a) +{ + if (x > a.x1 && x < a.x2 && y > a.y2 && y < a.y1) + { + return true; + } + else + { + return false; + } +} +bool cmp2(double m, double n, Circle &a) +{ + double tmp = (m - a.x) * (m - a.x) + (n - a.y) * (n - a.y); + if (tmp < (a.r) * (a.r)) + { + return true; + } + else + { + return false; + } +} + +int main() +{ + char shape; + char figure[11]; + Circle ci[11]; + Rect re[11]; + Point po; + int numfigure = 1, numpoint = 1, num = 1; + while (cin >> shape) + { + if (shape == '*') + { + break; + } + if (shape == 'r') + { + figure[num] = 'r'; + cin >> re[numfigure].x1 >> re[numfigure].y1 >> re[numfigure].x2 >> re[numfigure].y2; + numfigure++; + num++; + } + if (shape == 'c') + { + figure[num] = 'c'; + cin >> ci[numfigure].x >> ci[numfigure].y >> ci[numfigure].r; + numfigure++; + num++; + } + } + while (cin >> po.x >> po.y) + { + if (po.x == 9999.9 && po.y == 9999.9) + { + break; + } + int flag = 0; + int j; + for (j = 1; j <= numfigure; j++) + { + if (figure[j] == 'r') + { + if (cmp1(po.x, po.y, re[j]) == true) + { + cout << "Point " << numpoint << " is contained in figure " << j << endl; + flag = 1; + } + } + if (figure[j] == 'c') + { + if (cmp2(po.x, po.y, ci[j]) == true) + { + cout << "Point " << numpoint << " is contained in figure " << j << endl; + flag = 1; + } + } + } + if (flag == 0) + { + cout << "Point " << numpoint << " is not contained in any figure" << endl; + } + numpoint++; + } + return 0; +} diff --git a/478.cpp b/478.cpp new file mode 100644 index 0000000..f56c2ce --- /dev/null +++ b/478.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +class Rect +{ +public: + double x1, y1, x2, y2; +}; + +class Circle +{ +public: + double x, y, r; +}; + +class Triangle +{ +public: + double x1, y1, x2, y2, x3, y3; +}; + +class Point +{ +public: + double x, y; +}; + +bool cmp1(double x, double y, Rect &a); + +bool cmp2(double m, double n, Circle &a); + +double distance(double x1, double y1, double x2, double y2); + +double area(double x1, double y1, double x2, double y2, double x3, double y3); + +int main() +{ + char shape; + char figure[11]; + Circle ci[11]; + Rect re[11]; + Triangle tri[11]; + Point po; + int numfigure = 1, numpoint = 1, num = 1; + while (cin >> shape) + { + if (shape == '*') + { + break; + } + if (shape == 'r') + { + figure[num] = 'r'; + cin >> re[numfigure].x1 >> re[numfigure].y1 >> re[numfigure].x2 >> re[numfigure].y2; + numfigure++; + num++; + } + if (shape == 'c') + { + figure[num] = 'c'; + cin >> ci[numfigure].x >> ci[numfigure].y >> ci[numfigure].r; + numfigure++; + num++; + } + if (shape == 't') + { + figure[num] = 't'; + cin >> tri[numfigure].x1 >> tri[numfigure].y1; + cin >> tri[numfigure].x2 >> tri[numfigure].y2; + cin >> tri[numfigure].x3 >> tri[numfigure].y3; + numfigure++; + num++; + } + } + while (cin >> po.x >> po.y) + { + if (po.x == 9999.9 && po.y == 9999.9) + { + break; + } + int flag = 0; + int j; + for (j = 1; j <= numfigure; j++) + { + if (figure[j] == 'r') + { + if (cmp1(po.x, po.y, re[j]) == true) + { + cout << "Point " << numpoint << " is contained in figure " << j << endl; + flag = 1; + } + } + if (figure[j] == 'c') + { + if (cmp2(po.x, po.y, ci[j]) == true) + { + cout << "Point " << numpoint << " is contained in figure " << j << endl; + flag = 1; + } + } + if (figure[j] == 't') + { + double area0, area1, area2, area3; + area0 = area(tri[j].x1, tri[j].y1, tri[j].x2, tri[j].y2, tri[j].x3, tri[j].y3); + area1 = area(tri[j].x1, tri[j].y1, tri[j].x2, tri[j].y2, po.x, po.y); + area2 = area(tri[j].x1, tri[j].y1, po.x, po.y, tri[j].x3, tri[j].y3); + area3 = area(po.x, po.y, tri[j].x2, tri[j].y2, tri[j].x3, tri[j].y3); + if (fabs(area0 - (area1 + area2 + area3)) <= 0.01) + { + cout << "Point " << numpoint << " is contained in figure " << j << endl; + flag = 1; + } + } + } + if (flag == 0) + { + cout << "Point " << numpoint << " is not contained in any figure" << endl; + } + numpoint++; + } + return 0; +} + +bool cmp1(double x, double y, Rect &a) +{ + if (x > a.x1 && x < a.x2 && y > a.y2 && y < a.y1) + { + return true; + } + else + { + return false; + } +} + +bool cmp2(double m, double n, Circle &a) +{ + double tmp = (m - a.x) * (m - a.x) + (n - a.y) * (n - a.y); + if (tmp < (a.r) * (a.r)) + { + return true; + } + else + { + return false; + } +} + +double distance(double x1, double y1, double x2, double y2) +{ + double t; + t = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + return sqrt(t); +} +double area(double x1, double y1, double x2, double y2, double x3, double y3) +{ + double A, AC, AB, BC; + AC = distance(x1, y1, x3, y3); + AB = distance(x1, y1, x2, y2); + BC = distance(x2, y2, x3, y3); + double tmp = (AC * AC + AB * AB - BC * BC) / (2 * AC * AB); + A = acos(tmp); + return (0.5 * AC * AB * sin(A)); +} diff --git a/481.cpp b/481.cpp index 9130705..edcba90 100644 --- a/481.cpp +++ b/481.cpp @@ -1,59 +1,82 @@ -#include -#include -#include -using namespace std; - -void find_lis(vector &a, vector &b) -{ - vector p(a.size()); - int u, v; - - if (a.empty()) return; - - b.push_back(0); - - for (size_t i = 1; i < a.size(); i++) { - if (a[b.back()] < a[i]) { - p[i] = b.back(); - b.push_back(i); - continue; - } - - for (u = 0, v = b.size()-1; u < v;) { - int c = (u + v) / 2; - if (a[b[c]] < a[i]) u=c+1; else v=c; - } - - if (a[i] < a[b[u]]) { - if (u > 0) p[i] = b[u-1]; - b[u] = i; - } - } - - for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v; -} -int main(){ - int test; - char ch[150]; - scanf("%d", &test); - cin.getline(ch, 150); - while(test--){ - vector seq; - vector lis; - while(true){ - cin.getline(ch, 150); - if(ch == "" || ch=="EOF") - break; - int val = atoi(ch); - seq.push_back(val); - } - find_lis(seq, lis); - cout<<"Max hits: "< 0) - cout< + +using namespace std; + +#define MIN(a, b) (a > b ? b :) +#define MAXN 1000000 + +int N, MAX, LIMIT, SEQ[MAXN], TEMP[MAXN], Par[MAXN]; + +// binary search +int FIND_BEST(int key) +{ + int lo, up, mid; + lo = 1; + up = LIMIT; + mid = (lo + up) / 2; + if (SEQ[TEMP[1]] > key) + return 1; + else if (SEQ[TEMP[LIMIT]] < key) + return LIMIT + 1; + while (lo < up && SEQ[TEMP[mid]] != key) + { + if (SEQ[TEMP[mid]] < key) + lo = mid + 1; + else if (SEQ[TEMP[mid]] > key) + { + if (SEQ[TEMP[mid - 1]] < key) + return mid; + up = mid - 1; + } + mid = (lo + up) / 2; + } + return mid; +} + +void FIND_LIS() +{ + int pos, i; + TEMP[1] = 0; + LIMIT = 1; + Par[0] = -1; + TEMP[0] = -1; + for (i = 1; i < N; i++) + { + pos = FIND_BEST(SEQ[i]); + if (pos > LIMIT) + { + LIMIT = pos; + TEMP[pos] = i; + Par[i] = TEMP[pos - 1]; + } + else if (SEQ[TEMP[pos]] > SEQ[i]) + { + TEMP[pos] = i; + Par[i] = TEMP[pos - 1]; + } + } +} + +void Print(int n) +{ + if (Par[n] == -1) + { + printf("%d\n", SEQ[n]); + return; + } + Print(Par[n]); + printf("%d\n", SEQ[n]); +} + +int main() +{ + N = 0; + int n; + while (scanf("%d", &n) == 1) + SEQ[N++] = n; + FIND_LIS(); + printf("%d\n", LIMIT); + printf("-\n"); + Print(TEMP[LIMIT]); + return 0; +} diff --git a/482.cpp b/482.cpp index a9386f9..ce56bc3 100644 --- a/482.cpp +++ b/482.cpp @@ -1,69 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +enum +{ + MAX = 1000 +}; + +int pos[MAX]; +string data[MAX]; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +int read_pos_array() +{ + int x, index = 0; + while (cin >> x) + { + pos[x - 1] = index;//index shift (between 0 start and 1 start) + index++; + if (cin.get() == '\n') + break; + } + return index; +} -string toString(int I){ - stringstream ss; - string str; - ss << I; - ss >> str; - return str; +void read_data_array(int len) +{ + for (int i = 0; i < len; ++i) + cin >> data[i]; + cin.ignore(128, '\n'); } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - string str, s; - while( T-- ){ - getline(cin, str); - getline(cin, str); - vector< string > ind, val; - map Map; - stringstream ss; - getline(cin, str); - ss << str; - while( ss >> s ) ind.push_back(s); - int N = ind.size(), i; - FOI(i, 0, N-1){ - cin >> str; - Map[ind[i]] = str; - } - FOI(i, 0, N-1) - cout << Map[toString(i+1)] << endl; - if( T > 0 ) - cout << endl; +int main() +{ + int num_case; + cin >> num_case; + while (num_case--) + { + cin.ignore(128, '\n'); + int len = read_pos_array(); + read_data_array(len); + for (int i = 0; i < len; ++i) + cout << data[pos[i]] << '\n'; + if (num_case != 0) + putchar('\n'); } return 0; } diff --git a/483.cpp b/483.cpp new file mode 100644 index 0000000..83fb1a8 --- /dev/null +++ b/483.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +int main() +{ + string input; + while (getline(cin, input)) + { + stringstream ss; + ss << input; + int c = 0; + while (ss >> input) + { + if (c > 0) + { + cout << " "; + } + for (int i = input.size() - 1; i >= 0; i--) + { + cout << input[i]; + } + c++; + } + cout << endl; + } +} diff --git a/484.cpp b/484.cpp index c7d468a..0ffab43 100644 --- a/484.cpp +++ b/484.cpp @@ -1,50 +1,29 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - vector< int > V; - map Map; +int main() +{ + int n; + map occurance; map::iterator it; - int I, i, j; - while (cin >> I){ - it = Map.find(I); - if (it != Map.end()) - ++Map[I]; - else{ - Map[I] = 1; - V.push_back(I); + vector filter; + while (cin >> n) + { + it = occurance.find(n); + if (it == occurance.end()) + { + filter.push_back(n); + occurance.insert(pair(n, 1)); } + else + { + it->second += 1; + } + } + for (int i = 0; i < filter.size(); i++) + { + cout << filter[i] << " " << occurance[filter[i]] << endl; } - FOI(i, 0, V.size()-1) - cout << V[i] << " " << Map[V[i]] << endl; return 0; } diff --git a/485.cpp b/485.cpp new file mode 100644 index 0000000..514439a --- /dev/null +++ b/485.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +char ss[2][1000][70], temp[100], limit[100]; + +void ADD(char n[], char m[]) +{ + char dummy[100]; + int j = 0, s, ln1, ln2, p, q, i; + int carry = 0; + ln1 = strlen(n) - 1; + ln2 = strlen(m) - 1; + for (;;) + { + p = 0; + if (ln1 >= 0) + p = n[ln1--] - '0'; + + q = 0; + if (ln2 >= 0) + q = m[ln2--] - '0'; + + s = p + q + carry; + dummy[j++] = s % 10 + '0'; + carry = s / 10; + if (ln1 < 0 && ln2 < 0) + break; + } + if (carry) + dummy[j++] = carry + '0'; + p = 0; + dummy[j] = NULL; + for (i = j - 1; i >= 0; i--) + temp[p++] = dummy[i]; + temp[p] = NULL; +} +int IsBIG(char m[]) +{ + int i; + if (strlen(m) < 61) + return 0; + if (strlen(m) > 61) + return 1; + for (i = 0; i < 61; i++) + { + if (m[i] > limit[i]) + return 1; + if (m[i] < limit[i]) + return 0; + } + return 1; +} +void Cal() +{ + int j, f = 0, s = 1, n = 1, k = 0, x = 2; + printf("1\n"); + printf("1 1\n"); + strcpy(ss[0][0], "1"); + strcpy(ss[0][1], "1"); + for (;;) + { + strcpy(ss[s][0], "1"); + printf("1"); + for (j = 1; j <= n; j++) + { + ADD(ss[f][j - 1], ss[f][j]); + if (IsBIG(temp)) + k = 1; + printf(" %s", temp); + strcpy(ss[s][j], temp); + } + n++; + printf(" 1\n"); + x++; + if (k) + break; + strcpy(ss[s][j], "1"); + s++; + s %= 2; + f++; + f %= 2; + } +} + +int main() +{ + int i; + limit[0] = '1'; + for (i = 1; i <= 60; i++) + limit[i] = '0'; + limit[61] = NULL; + Cal(); + return 0; +} diff --git a/486.cpp b/486.cpp new file mode 100644 index 0000000..ab6d638 --- /dev/null +++ b/486.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +char Note[28][15] = + { + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", + "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", + "sixty", "seventy", "eighty", "ninety"}; +int n = 28; +int value[28] = {0, 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 30, 40, + 50, 60, 70, 80, 90}; +char Command[1000]; +int Find(char ss[]) +{ + int i; + for (i = 0; i < n; i++) + { + if (!strcmp(ss, Note[i])) + { + return i; + } + } + return -1; +} +void Cal() +{ + char *com; + int total, note, pice, flag; + int temp, index; + total = note = pice = flag = 0; + for (int i = 0; Command[i]; i++) + { + Command[i] = tolower(Command[i]); + } + com = strtok(Command, " \b\t\r"); + while (com) + { + if (!strcmp(com, "negative")) + { + flag = 1; + } + else if (!strcmp(com, "hundred")) + { + note += pice * 100; + pice = 0; + } + else if (!strcmp(com, "thousand")) + { + total += note * 1000 + pice * 1000; + note = pice = 0; + } + else if (!strcmp(com, "million")) + { + total += note * 1000000 + pice * 1000000; + note = pice = 0; + } + else + { + index = Find(com); + temp = value[index]; + if (temp <= 9) + { + pice += temp; + } + else + { + note += temp; + } + } + com = strtok(NULL, " \b\t\r"); + } + total += note + pice; + if (total && flag) + { + printf("-"); + } + printf("%d\n", total); +} +int main() +{ + while (gets(Command)) + { + for (int i = 0; Command[i]; i++) + if (Command[i] == '\n') + { + Command[i] = NULL; + } + if (strlen(Command) <= 0) + { + break; + } + Cal(); + } + return 0; +} diff --git a/487.cpp b/487.cpp new file mode 100644 index 0000000..2fde42f --- /dev/null +++ b/487.cpp @@ -0,0 +1,124 @@ +#include + +using namespace std; + +/** +487 +**/ +#define MAXN 500000 +char F[22][22]; +char Temp[22]; +char Word[MAXN][30]; +char Bog[20][21]; +int K, N, L, MAX; +int com(const void *a, const void *s) +{ + char *d = (char *)a; + char *e = (char *)s; + int m = strlen(d); + int n = strlen(e); + if (m != n) + { + return m - n; + } + return strcmp((char *)a, (char *)s); +} +void Gen(int r, int c, int level) +{ + int k; + if (level) + { + if (Temp[level - 1] >= Bog[r][c]) + { + return; + } + } + Temp[level] = Bog[r][c]; + if (level >= 2) + { + for (k = 0; k <= level; k++) + { + Word[K][k] = Temp[k]; + } + Word[K][k] = NULL; + K++; + } + if (level == N * N - 1) + { + return; + } + for (int i = -1; i <= 1; i++) + { + for (int j = -1; j <= 1; j++) + { + if (i == 0 && j == 0) + { + continue; + } + int nr = r + i; + int nc = c + j; + if (nr < 0 || nr >= N || nc < 0 || nc >= N || F[nr][nc] == 1) + { + continue; + } + F[nr][nc] = 1; + Gen(nr, nc, level + 1); + F[nr][nc] = 0; + } + } +} +void Cal() +{ + int i, j; + K = 0; + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) + { + F[i][j] = 1; + Gen(i, j, 0); + F[i][j] = 0; + } +} +void sort() +{ + int i; + if (K == 0) + { + return; + } + qsort(Word, K, sizeof(Word[0]), com); + printf("%s\n", Word[0]); + for (i = 1; i < K; i++) + { + if (!strcmp(Word[i], Word[i - 1])) + { + continue; + } + printf("%s\n", Word[i]); + } +} +int main() +{ + int i, kase; + char input[10]; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + gets(input); + sscanf(input, "%d", &N); + for (i = 0; i < N; i++) + { + gets(Bog[i]); + } + Cal(); + sort(); + if (kase) + { + putchar('\n'); + gets(input); + } + } + return 0; +} diff --git a/488.cpp b/488.cpp index 66368e8..3f4f322 100644 --- a/488.cpp +++ b/488.cpp @@ -1,26 +1,45 @@ -#include -using namespace std; - -int main(){ - int test=0,ampl=0,freq=0,i=0,j=0,t=0,k=0; - cin>>test; - for(t=1;t<=test;t++){ - cin>>ampl>>freq; - for(i=1;i<=freq;i++){ - for(j=1;j=1;j--){ - for(k=1;k<=j;k++) - cout< + +using namespace std; + +int main() +{ + int Amplitude, Frequency; + int m; + cin >> m; + int f = 0; + for (int z = 0; z < m; z++) + { + cin >> Amplitude >> Frequency; + for (int cnt = 0; cnt < Frequency; cnt++) + { + if (f) + { + cout << endl; + } + f = 1; + int i; + for (i = 1; i <= Amplitude; i++) + { + int j = 0; + while (j < i) + { + cout << i; + j++; + } + cout << endl; + } + for (i = Amplitude - 1; i > 0; i--) + { + int j = 0; + while (j < i) + { + cout << i; + j++; + } + cout << endl; + } + } + } + return 0; +} diff --git a/489.cpp b/489.cpp new file mode 100644 index 0000000..d53664f --- /dev/null +++ b/489.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +/* +489 +*/ +#define MAXN 10000 +char Puzzel[MAXN]; +char Guess[MAXN]; +char F[40], G[40]; +void Cal(int n) +{ + int i, j, k, c = 0, w = 0; + int f; + printf("Round %d\n", n); + for (i = 0; Puzzel[i]; i++) + { + k = Puzzel[i] - 'a'; + G[k] = 1; + } + for (i = 0; Guess[i]; i++) + { + k = Guess[i] - 'a'; + f = 0; + if (F[k]) + { + continue; + } + if (G[k]) + { + c++; + G[k] = 0; + for (j = 0; j < 27; j++) + { + if (G[j]) + { + f = 1; + break; + } + } + } + else + { + w++; + f = 1; + } + F[k] = 1; + if (f == 0) + { + printf("You win.\n"); + return; + } + if (w == 7) + { + printf("You lose.\n"); + return; + } + } + printf("You chickened out.\n"); +} +int main() +{ + int n; + while (1) + { + scanf("%d", &n); + if (n == -1) + { + break; + } + scanf("%s%s", &Puzzel, &Guess); + Cal(n); + for (int i = 0; i < 30; i++) + { + F[i] = G[i] = 0; + } + } + return 0; +} diff --git a/490.cpp b/490.cpp new file mode 100644 index 0000000..bac3d8c --- /dev/null +++ b/490.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + int i = -1, j = -1, k, l, max = 0; + char ch[200][200], a; + while (scanf("%c", &a) != EOF) + { + i++; + j = -1; + while (a != 10) + { + if (a != 9) + { + j++; + ch[i][j] = a; + } + scanf("%c", &a); + } + if (j > max) + { + max = j; + } + } + for (l = 0; l <= max; l++) + { + for (k = i; k >= 0; k--) + { + if (ch[k][l] <= '\0') + { + ch[k][l] = ' '; + } + printf("%c", ch[k][l]); + } + printf("\n"); + } + return 0; +} diff --git a/491.cpp b/491.cpp new file mode 100644 index 0000000..fd89e91 --- /dev/null +++ b/491.cpp @@ -0,0 +1,13 @@ +#include + +using namespace std; + +int main() +{ + int ary[] = {0, 1, 1, 2, 7, 18, 60, 196, 704, 2500, 9189, 33896, 126759, 476270, 1802312, 6849777}, n; + while (scanf("%d", &n) == 1) + { + printf("%d\n", ary[n]); + } + return 0; +} diff --git a/492.cpp b/492.cpp index 47e7f5a..f788f7f 100644 --- a/492.cpp +++ b/492.cpp @@ -1,65 +1,81 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -bool isVowel(char C) { - C = tolower(C); - return (C == 'a' || C == 'e' || C == 'i' || C == 'o' || C == 'u'); -} +/** + * UVa 492 Pig-Latin (AC) + * Author: chchwy + * Last Modified: 2010.02.06 + */ -string mod(string S) { - if (isVowel(S[0])) - return S + "ay"; - return S.substr(1) + S[0] + "ay"; +bool isVowel(char c) +{ + switch (c) + { + case 'A': + case 'a': + case 'E': + case 'e': + case 'I': + case 'i': + case 'O': + case 'o': + case 'U': + case 'u': + return true; + default: + return false; + } } - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - while (getline(cin, str)) { - int L = str.length() - 1; - int i, j; - string temp = ""; - FOI(i, 0, L) { - if (!isalpha(str[i])) { - if (!temp.empty()) - cout << mod(temp); - cout << str[i]; - temp = ""; +int main() +{ + char buf[4096]; + int bufIndex = 0; + bool isWord = false;//the state + char c; + while ((c = getchar()) != EOF) + { + /* finite state machine */ + switch (isWord) + { + case false: + if (isalpha(c)) + { + bufIndex = 0;//clear buf + buf[bufIndex++] = c; + isWord = true; } else - temp += str[i]; + { + putchar(c); + } + break; + case true: + if (isalpha(c)) + { + buf[bufIndex++] = c; + } + else + { + /* print word */ + if (isVowel(buf[0])) + { + buf[bufIndex] = NULL;//NULL end + printf(buf); + printf("ay"); + } + else + { + buf[bufIndex] = NULL; + printf(buf + 1); + putchar(buf[0]); + printf("ay"); + } + putchar(c); + isWord = false; + } + break; } - if (!temp.empty()) - cout << mod(temp); - cout << endl; } return 0; } diff --git a/493.cpp b/493.cpp new file mode 100644 index 0000000..bbc62a8 --- /dev/null +++ b/493.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +/* +493 +Rational Spiral +*/ +#define MAXN 1000000 +char F[2000][2000]; +struct ss +{ + int num, den; +} point[MAXN]; +int ind; +int gcd(int a, int b) +{ + return b ? gcd(b, a % b) : a; +} +void Gen() +{ + int i, j, k, p = 3, x, y, cd, g; + int num = 1, den = 1; + ind = 0; + while (1) + { + //g = abs(den); + for (i = 1; i <= p; i++) + { + // x = abs(num); + cd = gcd(num, den); + x = (num / cd) + 1000; + y = (den / cd) + 1000; + num--; + if (F[x][y]) + { + continue; + } + point[ind].num = x - 1000; + point[ind++].den = y - 1000; + F[x][y] = 1; + if (ind >= MAXN) + { + return; + } + } + num++; + //g = abs(num); + for (j = 1; j <= p; j++) + { + if (den == 0) + { + den--; + continue; + } + //y = abs(den); + cd = gcd(num, den); + x = (num / cd) + 1000; + y = (den / cd) + 1000; + den--; + if (F[x][y]) + { + continue; + } + point[ind].num = x - 1000; + point[ind++].den = y - 1000; + F[x][y] = 1; + if (ind >= MAXN) + { + return; + } + } + den++; + p++; + //g = abs(den); + for (k = 1; k <= p; k++) + { + // x = abs(num); + cd = gcd(num, den); + x = (num / cd) + 1000; + y = (den / cd) + 1000; + num++; + if (F[x][y]) + { + continue; + } + point[ind].num = x - 1000; + point[ind++].den = y - 1000; + F[x][y] = 1; + if (ind >= MAXN) + { + return; + } + } + num--; + //g = abs(num); + for (j = 1; j <= p; j++) + { + if (den == 0) + { + den++; + continue; + } + y = abs(den); + cd = gcd(num, den); + x = (num / cd) + 1000; + y = (den / cd) + 1000; + den++; + if (F[x][y]) + { + continue; + } + point[ind].num = x - 1000; + point[ind++].den = y - 1000; + F[x][y] = 1; + if (ind >= MAXN) + { + return; + } + } + den--; + p++; + } +} +int main() +{ + int n, y; + Gen(); + while (scanf("%d", &n) == 1) + { + y = point[n].den; + if (point[n].den < 0) + { + printf("-"); + y *= -1; + } + printf("%d / %d\n", point[n].num, y); + } + return 0; +} diff --git a/494.cpp b/494.cpp new file mode 100644 index 0000000..0c63f6d --- /dev/null +++ b/494.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int process(char *p) +{ + int i, num = 0; + for (i = 0; p[i] != '\0'; i++) + { + if (isalpha(p[i]) == 0) + { + if (isalpha(p[i - 1])) + { + num++; + } + } + } + return num; +} + +int main() +{ + char buf[100000]; + while (cin.getline(buf, 100000)) + { + cout << process(buf) << endl; + } + return 0; +} diff --git a/495.cpp b/495.cpp index e20849f..018d1a2 100644 --- a/495.cpp +++ b/495.cpp @@ -1,21 +1,49 @@ -#include - -using namespace std; - -int main() -{ - unsigned long long int fib[5000]={0}; - int i=0,n=0; - fib[0]=0; - fib[1]=1; - - for(int i=2;i<5000;i++) - fib[i]=fib[i-1]+fib[i-2]; - - while(cin>>n) - { - cout<<"The Fibonacci number for "< + +using namespace std; + +int fibonacci[5001][1100]; + +int main() +{ + memset(fibonacci, 0, sizeof(fibonacci)); + fibonacci[0][0] = 1; + fibonacci[1][0] = 1; + fibonacci[1][1] = 1; + int i, j, jinwei, tmp, n; + for (i = 2; i <= 5000; i++) + { + jinwei = 0; + fibonacci[i][0] = fibonacci[i - 1][0]; + for (j = 1; j <= fibonacci[i - 1][0]; j++) + { + tmp = fibonacci[i - 2][j] + fibonacci[i - 1][j] + jinwei; + if (tmp > 9) + { + fibonacci[i][j] = tmp % 10; + jinwei = 1; + } + else + { + jinwei = 0; + fibonacci[i][j] = tmp % 10; + } + } + if (jinwei == 1) + { + fibonacci[i][j] = 1; + fibonacci[i][0]++; + } + } + //begin input cases; + while (cin >> n) + { + cout << "The Fibonacci number for " << n << " is "; + for (i = fibonacci[n][0]; i > 0; i--) + { + cout << fibonacci[n][i]; + } + cout << endl; + } + return 0; +} diff --git a/496.cpp b/496.cpp index 7de6aa1..a68af9e 100644 --- a/496.cpp +++ b/496.cpp @@ -1,84 +1,66 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -vector< int > extract(string S) { - stringstream ss; - vector< int > V; - int I; - ss << S; - while (ss >> I) V.push_back(I); - return V; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - while (getline(cin, str)) { - vector< int > A = extract(str); - getline(cin, str); - vector< int > B = extract(str); - - int N = A.size() + B.size(); - - vector< int > amb(N), bma(N), aob(N), aib(N); - vector< int >::iterator it1, it2, it3, it4; - - sort(A.begin(), A.end()); - sort(B.begin(), B.end()); - - it1 = set_difference(A.begin(), A.end(), B.begin(), B.end(), amb.begin()); - it2 = set_difference(B.begin(), B.end(), A.begin(), A.end(), bma.begin()); - it3 = set_intersection(A.begin(), A.end(), B.begin(), B.end(), aib.begin()); - it4 = set_union(A.begin(), A.end(), B.begin(), B.end(), aob.begin()); - - if (int(it3 - aib.begin()) == A.size() && int(it3 - aib.begin()) == B.size()) - cout << "A equals B" << endl; - else if (int(it1 - amb.begin()) == 0) - cout << "A is a proper subset of B" << endl; - else if (int(it2 - bma.begin()) == 0) - cout << "B is a proper subset of A" << endl; - else if (int(it4 - aob.begin()) == N) - cout << "A and B are disjoint" << endl; - else - cout << "I'm confused!" << endl; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +void solve(const vector &a, const vector &b) +{ + int counter = 0; + for (int i = 0, sz = a.size(); i < sz; i++) + { + for (int j = 0, sz2 = b.size(); j < sz2; j++) + { + if (a[i] == b[j]) + { + counter++; + } + } + } + if (counter == a.size() && counter == b.size()) + { + cout << "A equals B" << endl; + } + else if (counter == a.size()) + { + cout << "A is a proper subset of B" << endl; + } + else if (counter == b.size()) + { + cout << "B is a proper subset of A" << endl; + } + else if (counter == 0) + { + cout << "A and B are disjoint" << endl; + } + else + { + cout << "I'm confused!" << endl; + } +} + +int main() +{ + int tmp; + string input; + vector a, b; + stringstream ss; + while (getline(cin, input)) + { + a.clear(); + b.clear(); + ss.clear(); + ss.str(input); + while (ss >> tmp) + { + a.push_back(tmp); + } + getline(cin, input); + ss.clear(); + ss.str(input); + while (ss >> tmp) + { + b.push_back(tmp); + } + solve(a, b); + } + return 0; +} diff --git a/497.cpp b/497.cpp index 4342ee2..3e5c9cb 100644 --- a/497.cpp +++ b/497.cpp @@ -1,21 +1,141 @@ -#include -#include -#include -using namespace std; - -int main(){ - string arr[100], str; - while(getline(cin,str)){ - int N=0; - arr[N++]= str; - while(true){ - getline(cin, str); - if(str=="") - break; - arr[N++]=str; - } - for(int i=0;i + +using namespace std; + +const int MAXSIZE = 300001; + +char str[50]; + +struct Element +{ + int key, pre; + + bool operator<(Element const &another) + { + return this->key < another.key; + } + + bool operator>(Element const &another) + { + return this->key > another.key; + } + + bool operator==(Element const &another) + { + return this->key == another.key; + } +}; + +Element arr[MAXSIZE]; // arr of elements +int lis[MAXSIZE]; // arr of indexes in a +int len; // arr len +int last; // lis len + +// bin srch +int binSearch(int start, int end, Element e) +{ + int low = start, mid, high = end; + while (low <= high) + { + mid = (low + high) >> 1; + if (arr[lis[mid]] == e) + { + return mid; + } + else if (arr[lis[mid]] < e) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + return mid; +} + +void solve() +{ + arr[0].pre = -1; + lis[last++] = 0; + int i; + for (i = 1; i < len; i++) + { + if (arr[i] > arr[lis[last - 1]]) + { + arr[i].pre = lis[last - 1]; + lis[last++] = i; + } + else + { + int x = binSearch(0, last - 1, arr[i]); + if (arr[lis[x]] == arr[i]) + { + arr[i].pre = arr[lis[x]].pre; + } + else if (arr[lis[x]] < arr[i]) + { + arr[i].pre = lis[x]; + lis[x + 1] = i; + } + else + { + if (x == 0) + { + arr[i].pre = -1; + } + else + { + arr[i].pre = lis[x - 1]; + } + lis[x] = i; + } + } + } +} + +void output() +{ + int result[MAXSIZE]; + cout << "Max hits: " << last << endl; + int x = lis[last - 1]; + for (int i = 0; i < last; i++) + { + result[i] = arr[x].key; + x = arr[x].pre; + } + for (int i = last - 1; i >= 0; i--) + { + cout << result[i] << endl; + } +} + +void input() +{ + len = last = 0; + while (cin.getline(str, 50) && strcmp(str, "\0")) + { + arr[len++].key = atoi(str); + } +} + +int main() +{ + int numOfCase; + cin >> numOfCase; + cin.get(); + cin.getline(str, 50); + int f = 0; + while (numOfCase--) + { + if (f) + { + cout << endl; + } + f = 1; + input(); + solve(); + output(); + } + return 0; +} diff --git a/498.cpp b/498.cpp new file mode 100644 index 0000000..5ae3261 --- /dev/null +++ b/498.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +/******************* +Polly the Polymonial + 498 +********************/ +#define MAX 10000 +char C[MAX]; +char X[MAX]; +double CON[1000]; +double V[1000]; +int K, N; +void SEPA() +{ + int i, j = 0, c; + char temp[15]; + K = 0; + N = 0; + c = 0; + for (i = 0; C[i];) + { + if (C[i] != ' ') + { + temp[j++] = C[i++]; + c = 1; + } + else if (c) + { + temp[j] = NULL; + CON[K++] = atof(temp); + j = 0; + for (i++; C[i] == ' ' && C[i]; i++) + ; + c = 0; + } + else + { + i++; + } + } + if (c) + { + temp[j] = NULL; + CON[K++] = atof(temp); + } + j = 0; + c = 0; + for (i = 0; X[i];) + { + if (X[i] != ' ') + { + temp[j++] = X[i++]; + c = 1; + } + else if (c) + { + temp[j] = NULL; + V[N++] = atof(temp); + j = 0; + for (i++; X[i] == ' ' && X[i]; i++) + ; + c = 0; + } + else + { + i++; + } + } + if (c) + { + temp[j] = NULL; + V[N++] = atof(temp); + } +} +double EVU(double v) +{ + int i; + double val = 0; + for (i = 0; i < K; i++) + { + val *= v; + val += CON[i]; + } + return val; +} +void CAL() +{ + int i; + double VAL; + for (i = 0; i < N; i++) + { + VAL = EVU(V[i]); + printf("%.0lf", VAL); + if (i < N - 1) + { + printf(" "); + } + } +} +int main() +{ + while (gets(C)) + { + gets(X); + SEPA(); + CAL(); + putchar('\n'); + } + return 0; +} diff --git a/499.cpp b/499.cpp new file mode 100644 index 0000000..a083c57 --- /dev/null +++ b/499.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int main() +{ + char p[1000]; + int i, array[52], max; + while (cin.getline(p, 1000)) + { + max = 0; + for (i = 0; i < 52; i++) + { + array[i] = 0; + } + for (i = 0; p[i] != '\0'; i++) + { + if (isalpha(p[i])) + { + if (isupper(p[i])) + { + array[p[i] - 'A']++; + } + if (islower(p[i])) + { + array[p[i] - 'a' + 26]++; + } + } + } + for (i = 0; i < 52; i++) + if (array[i] > max) + { + max = array[i]; + } + for (i = 0; i < 52; i++) + if (array[i] == max) + { + if (i < 26) + { + cout << char(i + 'A'); + } + if (i >= 26) + { + cout << char(i - 26 + 'a'); + } + } + cout << ' ' << max << endl; + } + return 0; +} diff --git a/500.cpp b/500.cpp new file mode 100644 index 0000000..127cff8 --- /dev/null +++ b/500.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +/* @JUDGE_ID: 2905EH 500 C++ "New Judgement..." */ +// 00/04/16 =>A_Q500.c +// New judgement => multiple input.. + +typedef struct +{ + int num; + char **cell; +} LINE; + +void Line(int *len, int max, char l, char m, char r) +{ + int i = 0, j; + printf("%c", l);//UP LINE + while (1) + { + for (j = 0; j < len[i] + 2; j++) + { + printf("%c", (char)196);//'? + } + if (++i != max) + { + printf("%c", m); + } + else + { + break; + } + } + printf("%c\n", r); +} + +int main() +{ + char buff[256], *Null_Str = ""; + int c = 0; + gets(buff); + int n_case = atoi(buff); + gets(buff);//blank.. + while (1) + { + int i, k, n_data = 0, max = 0, *len; + LINE data[100]; + while (gets(buff) && *buff != 0) + { + int n; + char *l, *r; + if (c++ && n_data == 0) + { + printf("\n"); + } + n = data[n_data].num = 0; + data[n_data].cell = 0; + if ((l = strchr(buff, 179)) == 0) + { + n_data++;//'? // --+-- + continue; + } + for (r = strchr(l + 1, 179); r != 0; n++, l = r, r = strchr(l + 1, 179)) + { + char *ll = l, *rr = r; + while (*(++ll) == ' ') + ; + while (*(--rr) == ' ') + ; + data[n_data].cell = (char **)realloc(data[n_data].cell, (n + 1) * sizeof(char *)); + if (ll <= rr)//found + { + // *(rr+1)=0; + data[n_data].cell[n] = + (char *)malloc((rr - ll + 2) * sizeof(char)); + // strcpy(data[n_data].cell[n],ll); + memcpy(data[n_data].cell[n], ll, rr - ll + 1); + data[n_data].cell[n][rr - ll + 1] = 0; + } + else + { + data[n_data].cell[n] = Null_Str; + } + } + data[n_data].num = n; + if (n > max) + { + max = n; + } + n_data++; + } + if (n_data == 0 || max == 0) + { + return 0; + } + len = (int *)calloc(max, sizeof(int)); + for (i = 0; i < n_data; i++) + for (k = 0; k < data[i].num; k++) + if (strlen(data[i].cell[k]) > len[k]) + { + len[k] = strlen(data[i].cell[k]); + } + Line(len, max, 218, 194, 191);//"?,"?,"? + k = 0; + if (data[0].num == 0) + { + k++; + } + for (; k < n_data; k++) + { + if (data[k].num == 0) + { + if (k == n_data - 1) + { + break; + } + Line(len, max, 195, 197, 180);//"?,"?,"? + continue; + } + for (i = 0; i < max; i++) + if (i < data[k].num) + { + printf("%c %-*s ", (char)179, len[i], data[k].cell[i]);//"? + } + else + { + printf("%c %-*s ", (char)179, len[i], Null_Str);//"? + } + printf("%c\n", (char)179);//"? + } + Line(len, max, 192, 193, 217);//"?,"?,"? + for (k = 0; k < n_data; k++) + { + for (i = 0; i < data[k].num; i++) + if (data[k].cell[i] != Null_Str) + { + free(data[k].cell[i]); + } + free(data[k].cell); + } + } +} +/*@END_OF_SOURCE_CODE*/ diff --git a/501.cpp b/501.cpp new file mode 100644 index 0000000..7f5b661 --- /dev/null +++ b/501.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +#define MAXN 30005 + +int MaxHeap[MAXN], MinHeap[MAXN], Com[MAXN], Num[MAXN]; +int N, com, headN, headC, MaxHeapSize, MinHeapSize; + +void InsMax(int key) +{ + int i, temp; + i = MaxHeapSize; + MaxHeap[MaxHeapSize] = key; + MaxHeapSize++; + while (i > 1 && MaxHeap[i / 2] < MaxHeap[i]) + { + temp = MaxHeap[i / 2]; + MaxHeap[i / 2] = MaxHeap[i]; + MaxHeap[i] = temp; + i /= 2; + } +} + +void InsMin(int key) +{ + int i, temp; + i = MinHeapSize; + MinHeap[MinHeapSize] = key; + MinHeapSize++; + while (i > 1 && MinHeap[i / 2] > MinHeap[i]) + { + temp = MinHeap[i / 2]; + MinHeap[i / 2] = MinHeap[i]; + MinHeap[i] = temp; + i /= 2; + } +} + +void MinHeapi(int n) +{ + int l, r, sm, temp; + l = n * 2; + r = l + 1; + sm = n; + if (l <= MinHeapSize && MinHeap[l] < MinHeap[n]) + sm = l; + if (r <= MinHeapSize && MinHeap[r] < MinHeap[sm]) + sm = r; + if (sm != n) + { + temp = MinHeap[sm]; + MinHeap[sm] = MinHeap[n]; + MinHeap[n] = temp; + MinHeapi(sm); + } +} + +void MaxHeapi(int n) +{ + int l, r, sm, temp; + l = n * 2; + r = l + 1; + sm = n; + if (l <= MaxHeapSize && MaxHeap[l] > MaxHeap[n]) + sm = l; + if (r <= MaxHeapSize && MaxHeap[r] > MaxHeap[sm]) + sm = r; + if (sm != n) + { + temp = MaxHeap[sm]; + MaxHeap[sm] = MaxHeap[n]; + MaxHeap[n] = temp; + MaxHeapi(sm); + } +} + +int ExtracMin() +{ + int min; + min = MinHeap[1]; + MinHeap[1] = MinHeap[MinHeapSize - 1]; + MinHeapSize--; + MinHeapi(1); + return min; +} + +void Print() +{ + int k; + printf("%d\n", MaxHeap[1]); + if (MinHeapSize > 1) + { + k = ExtracMin(); + InsMax(k); + } +} + +int Operation(int ind) +{ + int val; + val = Num[ind]; + if (MaxHeapSize <= headC) + InsMax(val); + else if (MaxHeapSize == headC + 1) + { + if (MaxHeap[1] > val) + { + InsMin(MaxHeap[1]); + MaxHeap[1] = val; + MaxHeapi(1); + } + else + InsMin(val); + } + if (ind == Com[headC]) + { + Print(); + headC++; + if (headC > com) + return 1; + while (Com[headC] == Com[headC - 1] && headC <= com) + { + Print(); + headC++; + } + } + return 0; +} + +void Cal() +{ + int i; + MaxHeapSize = MinHeapSize = headC = 1; + for (i = 1; i <= N; i++) + if (Operation(i)) + break; + memset(MaxHeap, 0, sizeof(int) * MAXN); + memset(MinHeap, 0, sizeof(int) * MAXN); +} + +int main() +{ + int i, j, Kase = 1; + scanf("%d", &Kase); + while (Kase--) + { + scanf("%d%d", &N, &com); + for (i = 1; i <= N; i++) + scanf("%d", &Num[i]); + for (j = 1; j <= com; j++) + scanf("%d", &Com[j]); + Cal(); + if (Kase) + printf("\n"); + } + return 0; +} diff --git a/502.cpp b/502.cpp new file mode 100644 index 0000000..b704987 --- /dev/null +++ b/502.cpp @@ -0,0 +1,276 @@ +#include + +using namespace std; + +#define inf 1 << 25 + +struct node +{ + string name; + string ext; +}; +node pl[1050]; +node add[1050]; +int n, m; +int ml1, ml2; +node ans; + +void read() +{ + int i, j; + char temp[100]; + string t; + ml1 = ml2 = inf; + n = m = 0; + while (gets(temp) != NULL && temp[0] != 0) + { + t = ""; + t += temp; + if (t[0] == '-') + { + int p = t.find('.'); + if (p != -1) + { + if (p == t.size() - 1) + { + pl[n].name = t.substr(1, t.size() - 2); + pl[n].ext = ""; + } + else + { + pl[n].ext = t.substr(p + 1, t.size() - p); + pl[n].name = t.substr(1, p - 1); + } + } + else + { + pl[n].name = t.substr(1, t.size() - 1); + pl[n].ext = ""; + } + // cout< + +using namespace std; + +long l, w, h, x1, y1_, z1, x2, y2, z2, r; + +void swap(long &x, long &y) +{ + long t; + t = x; + x = y; + y = t; +} + +void turn(int i, int j, long x, long y, long z, long x0, long y0, long l, long w, long h) +{ + long rr; + if (z == 0) + { + rr = x * x + y * y; + if (rr < r) + { + r = rr; + } + } + else + { + if ((i >= 0) && (i < 2)) + { + turn(i + 1, j, x0 + l + z, y, x0 + l - x, x0 + l, y0, h, w, l); + } + if ((j >= 0) && (j < 2)) + { + turn(i, j + 1, x, y0 + w + z, y0 + w - y, x0, y0 + w, l, h, w); + } + if ((i <= 0) && (i > -2)) + { + turn(i - 1, j, x0 - z, y, x - x0, x0 - h, y0, h, w, l); + } + if ((j <= 0) && (j > -2)) + { + turn(i, j - 1, x, y0 - z, y - y0, x0, y0 - h, l, h, w); + } + } +} + +int main() +{ + while (cin >> l >> w >> h >> x1 >> y1_ >> z1 >> x2 >> y2 >> z2) + { + if ((z1 != 0) && (z1 != h)) + { + if ((y1_ == 0) || (y1_ == w)) + { + swap(y1_, z1); + swap(y2, z2); + swap(w, h); + } + else + { + swap(x1, z1); + swap(x2, z2); + swap(l, h); + } + } + if (z1 == h) + { + z1 = 0; + z2 = h - z2; + } + r = 2147483647; + turn(0, 0, x2 - x1, y2 - y1_, z2, -x1, -y1_, l, w, h); + cout << r << endl; + } + return 0; +} diff --git a/505.cpp b/505.cpp new file mode 100644 index 0000000..bdf9666 --- /dev/null +++ b/505.cpp @@ -0,0 +1,226 @@ +#include + +using namespace std; + +/* @JUDGE_ID: 2905EH 505 C++ "long long int" */ +//2002/4/16 a_q505.cpp ???????? terrible.. +//2002/4/29 using long long int... + +/* +INPUT: +WED, 28 FEB 35 23:59:00 -2359 +WED, 1 JAN 0004 00:00:00 +2359 +WED, 1 JAN 0000 00:00:00 +0000 + +OUTPUT: +FRI, 02 MAR 1935 02:58:00 +0300 +TUE, 31 DEC 0003 03:01:00 +0300 +WED, 01 JAN 0000 03:00:00 +0300 +*/ + +#define BIG_Type long long + +char *weekStr[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; +char *monStr[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", + "AUG", "SEP", "OCT", "NOV", "DEC"}; +int NUM_DAYS[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; // 0-based, for day-in-year +int LEAP_NUM_DAYS[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};// 0-based, for day-in-year + +bool isLeap(int y) +{ + return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0); +} + +BIG_Type yearTime(int yy) +{ + yy += 3; + return (BIG_Type)yy * (BIG_Type)365 + yy / 4 - yy / 100 + yy / 400; +} + +char line[1000]; + +int main() +{ + while (gets(line)) + { + char *p = strtok(line, " \t:,"); + // if (p==NULL) while(1); //debug..no + int week; + for (week = 0; strcmp(p, weekStr[week]) != 0; week++) + ; + p = strtok(NULL, " \t:,"); + int day = atoi(p) - 1; + p = strtok(NULL, " \t:,"); + int mon; + for (mon = 0; strcmp(p, monStr[mon]) != 0; mon++) + ; + p = strtok(NULL, " \t:,"); + int year = atoi(p); + if (strlen(p) == 2) + { + year += 1900; + } + p = strtok(NULL, " \t:,"); + int hh = atoi(p); + p = strtok(NULL, " \t:,"); + int mm = atoi(p); + p = strtok(NULL, " \t:,"); + int ss = atoi(p); + p = strtok(NULL, " \t:,"); + //compute nowTime, field=>time + BIG_Type nowTime = yearTime(year); + if (isLeap(year)) + { + nowTime += LEAP_NUM_DAYS[mon] + day; + } + else + { + nowTime += NUM_DAYS[mon] + day; + } + BIG_Type tomorrowTime = (nowTime + 2) * 24 * 60; + nowTime *= 24; + nowTime += hh; + nowTime *= 60; + nowTime += mm; + int zone; + if (strcmp(p, "UT") == 0) + { + zone = 0; + } + else if (strcmp(p, "GMT") == 0) + { + zone = 0; + } + else if (strcmp(p, "EDT") == 0) + { + zone = -4 * 60; + } + else if (strcmp(p, "CDT") == 0) + { + zone = -5 * 60; + } + else if (strcmp(p, "MDT") == 0) + { + zone = -6 * 60; + } + else if (strcmp(p, "PDT") == 0) + { + zone = -7 * 60; + } + else + { + zone = atoi(p + 1); + zone = (zone / 100) * 60 + zone % 100; + if (*p == '-') + { + zone = -zone; + } + } + zone = 3 * 60 - zone;//Moscow + nowTime += zone; + BIG_Type nowTime2 = nowTime; + //compute field, time=>field + int nowMM = nowTime % 60; + nowTime /= 60; + int nowHH = nowTime % 24; + nowTime /= 24; + int yyy = year - 1; + BIG_Type yyyBegin = yearTime(yyy); + BIG_Type yyyEnd = yyyBegin + 365 + isLeap(yyy); + int nowYear = -1; + // printf("nowTime=%d\n",nowTime); + // printf("yyyBegin=%d\n",yyyBegin); + // printf("yyyEnd=%d\n",yyyEnd); + if (yyyBegin <= nowTime && nowTime < yyyEnd) + { + nowYear = yyy; + } + else + { + yyy++; + yyyBegin = yearTime(yyy); + yyyEnd = yyyBegin + 365 + isLeap(yyy); + // printf("yyyBegin=%d\n",yyyBegin); + // printf("yyyEnd=%d\n",yyyEnd); + if (yyyBegin <= nowTime && nowTime < yyyEnd) + { + nowYear = yyy; + } + else + { + yyy++; + yyyBegin = yearTime(yyy); + yyyEnd = yyyBegin + 365 + isLeap(yyy); + // printf("yyyBegin=%d\n",yyyBegin); + if (yyyBegin <= nowTime && nowTime < yyyEnd) + { + nowYear = yyy; + } + // else + // { + // printf("!!\n"); + // while(1); //debug..impossible + // } + } + } + int diff = nowTime - yyyBegin; + int nowMon, nowDay; + if (isLeap(nowYear)) + { + for (nowMon = 11; nowMon >= 0; nowMon--) + if (diff >= LEAP_NUM_DAYS[nowMon]) + { + break; + } + nowDay = diff - LEAP_NUM_DAYS[nowMon]; + } + else + { + for (nowMon = 11; nowMon >= 0; nowMon--) + if (diff >= NUM_DAYS[nowMon]) + { + break; + } + nowDay = diff - NUM_DAYS[nowMon]; + } + // printf("nowMon=%d,mon=%d\n",nowMon,mon); + // printf("week=%d,nowDay=%d,day=%d\n",week,nowDay,day); + if (nowDay != day) + { + if (zone < 0) + { + if (--week < 0) + { + week += 7; + } + } + else + { + if (nowTime2 >= tomorrowTime) + { + week++; + } + if (++week > 6) + { + week -= 7; + } + } + } + /* + if (zone<0 && nowDay!=day) + { + if (--week<0) week+=7; + } + else if (zone>0 && nowDay!=day) + { + if (++week>6) week-=7; + } + */ + //print.. + // SUN, 03 DEC 1996 12:10:35 +0300 + printf("%s, %02d %s %04d %02d:%02d:%02d +0300\n", + weekStr[week], nowDay + 1, monStr[nowMon], nowYear, + nowHH, nowMM, ss); + } +} +/*@END_OF_SOURCE_CODE*/ diff --git a/506.cpp b/506.cpp new file mode 100644 index 0000000..b3ef4ec --- /dev/null +++ b/506.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define N 5000 +#define EXPLI 0//meiji +#define IMPLI 1//not meiji +#define UNINSTALLED 2 +#define pb push_back + +vector dep[N]; +vector rev_dep[N]; +list install_info; + +int state[N]; +string name[N]; + +void remove_from_list(int cur) +{ + list::iterator itr = find(install_info.begin(), install_info.end(), cur); + install_info.erase(itr); +} + +void remove_rec(int cur, bool flag) +{ + if (state[cur] == UNINSTALLED && flag == true) + { + cout << " " << name[cur] << " is not installed." << endl; + return; + } + rep(i, dep[cur].size())//still need? + { + if (state[dep[cur][i]] != UNINSTALLED) + { + if (flag == true) + { + cout << " " << name[cur] << " is still needed." << endl; + } + return; + } + } + if (flag == true) + { + state[cur] = UNINSTALLED;//response to command + } + state[cur] = UNINSTALLED; + cout << " Removing " << name[cur] << endl; + remove_from_list(cur); + rep(i, rev_dep[cur].size()) + { + if (state[rev_dep[cur][i]] == IMPLI) + { + remove_rec(rev_dep[cur][i], false); + } + } +} + +void install_rec(int cur, bool flag) +{ + if (flag == true && state[cur] != UNINSTALLED)//respons to command + { + cout << " " << name[cur] << " is already installed." << endl; + return; + } + rep(i, rev_dep[cur].size()) + { + if (state[rev_dep[cur][i]] == UNINSTALLED) + { + install_rec(rev_dep[cur][i], false); + } + } + if (flag == true) + { + state[cur] = EXPLI; + } + else + { + state[cur] = IMPLI; + } + install_info.pb(cur); + cout << " Installing " << name[cur] << endl; +} + +void list_up() +{ + list::iterator itr = install_info.begin(); + while (itr != install_info.end()) + { + cout << " " << name[*(itr++)] << endl; + } +} + +int find_index(string &tar, map &M) +{ + int index; + if (M.find(tar) == M.end()) + { + index = M[tar] = M.size(); + } + else + { + index = M[tar]; + } + name[index] = tar; + return index; +} + +main() +{ + string inp; + map M; + rep(i, N) state[i] = UNINSTALLED; + while (getline(cin, inp)) + { + cout << inp << endl; + string command, tmp; + vector in; + stringstream sin(inp); + sin >> command; + while (sin >> tmp) + { + in.pb(tmp); + } + if (command == "DEPEND") + { + int from = find_index(in[0], M); + REP(i, 1, in.size()) + { + int to = find_index(in[i], M); + dep[to].pb(from); + rev_dep[from].pb(to); + } + } + else if (command == "INSTALL") + { + int index = find_index(in[0], M); + install_rec(index, true); + } + else if (command == "REMOVE") + { + int index = find_index(in[0], M); + remove_rec(index, true); + } + else if (command == "LIST") + { + list_up(); + } + else if (command == "END") + { + break; + } + } +} diff --git a/507.cpp b/507.cpp index 2f1f78a..426b0a6 100644 --- a/507.cpp +++ b/507.cpp @@ -1,73 +1,82 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T, t; - cin >> T; - FOI(t, 1, T){ - int N, i, j; - cin >> N; - --N; - int arr[N]; - FOI(i, 0, N-1) - cin >> arr[i]; - int maxSum = -INF, maxSta = 0, maxEnd = 0; - int curSum = 0, curSta = 0, curEnd = 0; - FOI(curEnd, 0, N-1){ - curSum += arr[curEnd]; - if (curSum > maxSum || - ((curSum == maxSum && ((curEnd - curSta > maxEnd - maxSta) || (curEnd - curSta == maxEnd - maxSta && curSta < maxSta))))){ - maxSum = curSum; - maxSta = curSta; - maxEnd = curEnd; +/* +Jill Rides Again +507 +*/ +#define MAXN 20002 +int stop, maxlen, maxst, maxend, maxval; +int Nice[MAXN]; +struct ss +{ + int val; + int st; +} table[MAXN]; +void Dynamic() +{ + int i, j, k; + table[1].val = Nice[1]; + table[1].st = 1; + maxval = Nice[1]; + maxlen = maxst = maxend = 1; + for (i = 2; i <= stop - 1; i++) + { + k = Nice[i] + table[i - 1].val; + if (k >= Nice[i]) + { + table[i].val = k; + table[i].st = table[i - 1].st; + j = i - table[i].st + 1; + if (table[i].val > maxval) + { + maxval = table[i].val; + maxlen = i - table[i].st + 1; + maxend = i; + maxst = table[i].st; } - if (curSum < 0){ - curSum = 0; - curSta = curEnd + 1; + else if (table[i].val == maxval && maxlen < j) + { + maxlen = i - table[i].st + 1; + maxend = i; + maxst = table[i].st; + } + } + else + { + table[i].val = Nice[i]; + table[i].st = i; + if (table[i].val > maxval) + { + maxval = table[i].val; + maxlen = i - table[i].st + 1; + maxend = i; + maxst = table[i].st; } } - if (maxSum <= 0) - cout << "Route " << t << " has no nice parts" << endl; + } +} +int main() +{ + int route, i, j; + scanf("%d", &route); + for (i = 1; i <= route; i++) + { + scanf("%d", &stop); + for (j = 1; j <= stop - 1; j++) + { + scanf("%d", &Nice[j]); + } + Dynamic(); + if (maxval > 0) + { + printf("The nicest part of route %d is between stops %d and %d\n", i, maxst, maxend + 1); + } else - cout << "The nicest part of route " << t << " is between stops " << maxSta + 1 << " and " << maxEnd + 2 << endl; + { + printf("Route %d has no nice parts\n", i); + } } return 0; } - diff --git a/508.cpp b/508.cpp new file mode 100644 index 0000000..acbe56d --- /dev/null +++ b/508.cpp @@ -0,0 +1,183 @@ +#include + +using namespace std; + +//#define LOCAL +//#define TESTING +#define maxn 1005 + +char c[maxn], m[maxn][15], word[maxn][15], mword[maxn][100], mlen[maxn], code[maxn][100], clen[maxn]; +int result; + +int check(char *codes, int len, int wordn, int change) +{ + int finded = 0; + for (int i = 0; i < wordn; i++) + { + if ((len == mlen[i] - change) || (len == mlen[i] + change)) + { + int lim; + bool eq = true; + if (len > mlen[i]) + { + lim = mlen[i]; + } + else + { + lim = len; + } + for (int j = 0; j < lim; j++) + { + if (codes[j] != mword[i][j]) + { + eq = false; + break; + } + } + if (eq) + { + if (!finded) + { + result = i; + } + finded++; + } + } + } + return finded; +} + +int main() +{ +#ifdef LOCAL + freopen("xt4-6.in", "r", stdin); +//freopen("xt4-6.out","w",stdout); +#endif + //读入å•个字符编砠+ char temp, tempc; + int cn = 0, cm, ind = 0; + memset(c, 0, sizeof(c)); + memset(m, 0, sizeof(m)); + for (;;) + { + char temps[maxn]; + scanf("%s", temps); + if (temps[0] == '*') + { + break; + } + for (int i = 0; i < strlen(temps); i++) + { + if (isalpha(temps[i]) || isdigit(temps[i])) + { + c[cn] = temps[i]; + cn++; + cm = 0; + } + else if (temps[i] == '.' || temps[i] == '-') + { + m[cn - 1][cm] = temps[i]; + cm++; + } + } + } + //读入å•个字符编ç Â结æŸ + //读入è¯Â典 + int wordn = 0, wcn = 0, mwn = 0, maxmlen = 0; + bool firstc = true; + memset(word, 0, sizeof(word)); + memset(mword, 0, sizeof(mword)); + memset(mlen, 0, sizeof(mlen)); + for (;;) + { + char temps[maxn]; + scanf("%s", temps); + if (temps[0] == '*') + { + break; + } + for (int i = 0; i < strlen(temps); i++) + { + if (isalpha(temps[i]) || isdigit(temps[i])) + { + word[wordn][wcn] = temps[i]; + wcn++; + for (int j = 0; j < strlen(c); j++) + { + if (temps[i] == c[j]) + { + for (int k = 0; k < strlen(m[j]); k++) + { + mword[wordn][mwn] = m[j][k]; + mwn++; + } + break; + } + } + } + } + mlen[wordn] = mwn; + wordn++; + wcn = 0; + if (maxmlen < mwn) + { + maxmlen = mwn; + } + mwn = 0; + } + //读入è¯Â典结æŸ + //读入待解编ç Â并输出对应结果 + int coden = 0, cnn = 0; + firstc = true; + bool firstoutput = true; + memset(code, 0, sizeof(code)); + memset(clen, 0, sizeof(clen)); + for (;;) + { + char temps[maxn]; + scanf("%s", temps); + if (temps[0] == '*') + { + break; + } + for (int i = 0; i < strlen(temps); i++) + { + if (temps[i] == '.' || temps[i] == '-') + { + code[coden][cnn] = temps[i]; + cnn++; + } + } + //输出code[coden]对应的word[] + int rtype; + rtype = check(code[coden], cnn, wordn, 0); + if (rtype == 1) + { + printf("%s\n", word[result]); + } + else if (rtype > 1) + { + printf("%s!\n", word[result]); + } + else + { + for (int i = 1; i < maxmlen; i++) + { + rtype = check(code[coden], cnn, wordn, i); + if (rtype) + { + break; + } + } + printf("%s?\n", word[result]); + } + result = -2; + //输出结æŸ + coden++; + firstoutput = false; + clen[coden] = cnn; + cnn = 0; + } + //读入待解编ç Â并输出对应结果结æŸ + return 0; +} diff --git a/509.cpp b/509.cpp new file mode 100644 index 0000000..6a9ce40 --- /dev/null +++ b/509.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +char data[8][102][66], disk[8][6401], data2[32001], pdata[6402]; +const char *hex_[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; + +int main() +{ + int d, s, b, kase = 0; + char parity[2]; + for (;;) + { + kase++; + scanf("%d", &d); + if (!d) + { + break; + } + scanf("%d%d%s", &s, &b, parity); + memset(disk, 0, sizeof(disk)); + memset(data2, 0, sizeof(data2)); + memset(pdata, 0, sizeof(pdata)); + for (int i = 1; i <= d; i++) + { + scanf("%s", disk[i]); + } + int byte = 0, pbyte = 0; + for (int i = 1; i <= b; i++) + { + for (int j = 1; j <= d; j++) + { + if (i % d == j || (i % d == 0 && j == d)) + { + for (int k = 0; k < s; k++) + { + pdata[pbyte] = disk[j][s * i - s + k]; + pbyte++; + } + } + else + { + for (int k = 0; k < s; k++) + { + data2[byte] = disk[j][s * i - s + k]; + byte++; + } + } + } + } + bool valid = true; + int check; + if (parity[0] == 'E') + { + check = 0; + } + else + { + check = 1; + } + int datas = d - 1; + for (int i = 1; i <= b; i++) + { + for (int j = 0; j < s; j++) + { + int xn = 0, pxn = 0, presult = -1, xind = -1; + if (pdata[i * s - s + j] == 'x') + { + pxn++; + } + for (int k = 0; k <= datas - 1; k++) + { + if (data2[i * s * datas - datas * s + j + k * s] == 'x') + { + xn++; + if (pxn || xn >= 2) + { + valid = false; + } + else + { + xind = k; + } + if (!valid) + { + break; + } + } + else if (presult < 0) + { + presult = data2[i * s * datas - datas * s + j + k * s] - '0'; + } + else + { + presult ^= data2[i * s * datas - datas * s + j + k * s] - '0'; + } + } + if (!valid) + { + break; + } + if ((pxn + xn == 0) && presult ^ check != pdata[i * s - s + j] - '0') + { + valid = false; + break; + } + else if (xn && !pxn) + { + if (presult >= 0) + { + data2[i * s * datas - datas * s + j + xind * s] = (presult ^ check) ^ (pdata[i * s - s + j] - '0') + '0'; + } + else + { + data2[i * s * datas - datas * s + j + xind * s] = check ^ (pdata[i * s - s + j] - '0') + '0'; + } + } + } + if (!valid) + { + break; + } + } + while (strlen(data2) % 4) + { + data2[strlen(data2)] = '0'; + } + if (!valid) + { + printf("Disk set %d is invalid.\n", kase); + } + else + { + printf("Disk set %d is valid, contents are: ", kase); + for (int i = 0; i <= strlen(data2); i += 4) + { + int res = 0; + for (int k = 0; k < 4; k++) + { + res = res * 2 + data2[i + k] - '0'; + } + if (0 <= res && res < 16) + { + printf("%s", hex_[res]); + } + } + printf("\n"); + } + } + return 0; +} diff --git a/511.cpp b/511.cpp new file mode 100644 index 0000000..f5085db --- /dev/null +++ b/511.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +struct A +{ + string name; + double mianji; + double zhong; + double bili; + double xiawei; + double minx; +}; +struct cmp +{ + bool operator()(const A &a, const A &b) + { + if (a.mianji != b.mianji) + { + return a.mianji < b.mianji; + } + else if (a.zhong != b.zhong) + { + return a.zhong > b.zhong; + } + else if (a.bili != b.bili) + { + return a.bili > b.bili; + } + else if (a.mianji != b.xiawei) + { + return a.xiawei < b.xiawei; + } + else + { + return a.minx > b.minx; + } + } +}; +struct B +{ + string name1; + double x1, x2, y1, y2, bili1, minaji1; +}; +int main() +{ + string s; + int lenl = 0, i, j, l, f = 0; + double xx1, xx2, yy1, yy2; + vector maps; + map names; + vector zhao[10000]; + while (cin >> s) + { + if (s == "MAPS") + { + f = 1; + } + else if (s == "LOCATIONS") + { + f = 2; + } + else if (s == "REQUESTS") + { + f = 3; + } + else if (s == "END") + { + break; + } + else + { + if (f == 1) + { + cin >> xx1 >> yy1 >> xx2 >> yy2; + double xxx1 = fabs(xx1 - xx2), yyy1 = fabs(yy1 - yy2); + B D; + D.name1 = s; + D.x1 = min(xx1, xx2); + D.x2 = max(xx1, xx2); + D.y1 = min(yy1, yy2); + D.y2 = max(yy1, yy2); + D.bili1 = fabs(0.75 - 1.0 * yyy1 / xxx1); + D.minaji1 = xxx1 * yyy1; + maps.push_back(D); + } + else if (f == 2) + { + lenl++; + cin >> xx1 >> yy1; + names[s] = lenl; + priority_queue, cmp> pp; + int lenn = maps.size(), lenn1 = 0; + for (i = 0; i < lenn; i++) + { + if (xx1 >= maps[i].x1 && xx1 <= maps[i].x2 && yy1 >= maps[i].y1 && yy1 <= maps[i].y2) + { + A C; + C.mianji = maps[i].minaji1; + C.bili = maps[i].bili1; + C.zhong = sqrt(((maps[i].x1 + maps[i].x2) / 2.0 - xx1) * ((maps[i].x1 + maps[i].x2) / 2.0 - xx1) + ((maps[i].y1 + maps[i].y2) / 2.0 - yy1) * ((maps[i].y1 + maps[i].y2) / 2.0 - yy1)); + C.xiawei = sqrt((maps[i].x2 - xx1) * (maps[i].x2 - xx1) + (maps[i].y1 - yy1) * (maps[i].y1 - yy1)); + C.minx = maps[i].x1; + C.name = maps[i].name1; + pp.push(C); + //if(s=="SanFrancisco")cout<> l; + int weizhi = names[s]; + int lenn = zhao[weizhi].size(); + cout << s; + if (!weizhi) + { + printf(" at detail level %d unknown location\n", l); + } + else if (!lenn) + { + printf(" at detail level %d no map contains that location\n", l); + } + else if (lenn >= l) + { + printf(" at detail level %d using ", l), cout << zhao[weizhi][l - 1].name << endl; + } + else + { + printf(" at detail level %d no map at that detail level; using ", l), cout << zhao[weizhi][lenn - 1].name << endl; + } + } + } + } +} diff --git a/512.cpp b/512.cpp new file mode 100644 index 0000000..4dfe5a9 --- /dev/null +++ b/512.cpp @@ -0,0 +1,173 @@ +#include + +using namespace std; + +const int maxn = 10000; +struct CMD +{ + int A, B[15], t; + void read() + { + char str[5]; + scanf("%s", str); + if (str[0] != 'E') + { + scanf("%d", &A); + } + else + { + A = 4; + } + for (int i = 0; i < A; i++) + { + scanf("%d", &B[i]); + } + if (str[0] == 'D' && str[1] == 'R') + { + t = 0; + } + else if (str[0] == 'D' && str[1] == 'C') + { + t = 1; + } + else if (str[0] == 'I' && str[1] == 'R') + { + t = 2; + } + else if (str[0] == 'I' && str[1] == 'C') + { + t = 3; + } + else + { + t = 4; + } + if (t != 4) + { + sort(B, B + A); + A = unique(B, B + A) - B; + } + } +} cmd[maxn]; +int R, C, n, totr, totc; + +pair go(int x, int y) +{ + //printf(">> %d %d\n", x, y); + for (int i = 0; i < n; i++) + { + if (cmd[i].t == 0) + { + int tx = x; + for (int j = 0; j < cmd[i].A; j++) + { + if (cmd[i].B[j] < x) + { + tx--; + } + if (cmd[i].B[j] == x) + { + return make_pair(-1, -1); + } + } + x = tx; + } + else if (cmd[i].t == 1) + { + int ty = y; + for (int j = 0; j < cmd[i].A; j++) + { + if (cmd[i].B[j] < y) + { + ty--; + } + if (cmd[i].B[j] == y) + { + return make_pair(-1, -1); + } + } + y = ty; + } + else if (cmd[i].t == 2) + { + int tx = x; + for (int j = 0; j < cmd[i].A; j++) + { + if (cmd[i].B[j] <= x) + { + tx++; + } + else + { + break; + } + } + x = tx; + } + else if (cmd[i].t == 3) + { + int ty = y; + for (int j = 0; j < cmd[i].A; j++) + { + if (cmd[i].B[j] <= y) + { + ty++; + } + else + { + break; + } + } + y = ty; + } + else + { + int x1 = cmd[i].B[0], y1 = cmd[i].B[1]; + int x2 = cmd[i].B[2], y2 = cmd[i].B[3]; + if (x1 == x && y1 == y) + { + x = x2, y = y2; + } + else if (x2 == x && y == y2) + { + x = x1, y = y1; + } + } + //printf(">> %d %d\n", x, y); + } + return make_pair(x, y); +} +int main() +{ + //freopen("test.in", "r", stdin); + for (int kase = 1; scanf("%d%d", &R, &C) != EOF && R && C; kase++) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + cmd[i].read(); + } + int q; + scanf("%d", &q); + if (kase >= 2) + { + printf("\n"); + } + printf("Spreadsheet #%d\n", kase); + for (int i = 0; i < q; i++) + { + int x, y; + scanf("%d%d", &x, &y); + pair res = go(x, y); + if (res.first == -1) + { + printf("Cell data in (%d,%d) GONE\n", x, y); + } + else + { + printf("Cell data in (%d,%d) moved to (%d,%d)\n", x, y, res.first, res.second); + } + } + } + return 0; +} diff --git a/514.cpp b/514.cpp index 15f50f7..65c87bb 100644 --- a/514.cpp +++ b/514.cpp @@ -1,97 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - freopen("test.txt", "r", stdin); - while(true){ - int N, i, j, I, J; - cin>>N; - if(N == 0) - break; - vector rel; - FOI(i, 1, N) - rel.push_back(i); - - while(true){ - bool in=true; - vector act(rel.begin(), rel.end()); - vector req; - FOI(i, 1, N){ - int num; - cin>>num; - if(num==0){ - in=false; - break; - } - req.push_back(num); - } - if(!in){ - break; - } - stack stak; - bool flag = true; - while(true){ - if(req.front()==act.front()){ - req.erase(req.begin()); - act.erase(act.begin()); - } - else if(!stak.empty()){ - if(req.front()==stak.top()){ - stak.pop(); - req.erase(req.begin()); - } - } - else{ - while(req.begin()!=act.begin()){ - stak.push(act.front()); - act.erase(act.begin()); - if(act.empty()){ - break; - } - } - } - if(act.empty() && stak.top() != req.front()) - flag = false; - if(req.empty() || !flag) - break; - } - if(!flag) - cout<<"No\n"; - else - cout<<"Yes\n"; - } - } - return 0; -} +#include + +using namespace std; + +int main() +{ + int tgts[1000]; + while (1) + { + int n; + scanf("%d", &n); + if (n == 0) + { + break; + } + while (1) + { + scanf("%d", &tgts[0]); + if (tgts[0] == 0) + { + printf("\n"); + break; + } + for (int i = 1; i < n; i++) + { + scanf("%d", &tgts[i]); + } + int curr_coach = 1, tgts_idx = 0; + stack station; + while (curr_coach <= n) + { + station.push(curr_coach); + while (!station.empty() && station.top() == tgts[tgts_idx]) + { + station.pop(); + tgts_idx++; + if (tgts_idx >= n) + { + break; + } + } + curr_coach++; + } + printf(station.empty() ? "Yes\n" : "No\n"); + } + } + return 0; +} diff --git a/515.cpp b/515.cpp new file mode 100644 index 0000000..81c1dc2 --- /dev/null +++ b/515.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +const int INF = 1 << 30, NN = 256; + +struct edge +{ + int from, to, w; +} e[NN]; + +int d[NN], ecnt; + +void add_edge(int from, int to, int w) +{ + edge &E = e[ecnt++]; + E.from = from; + E.to = to; + E.w = w; +} + +int main() +{ + char ord[8]; + d[0] = 0; + for (int m, n; scanf("%d %d", &n, &m) == 2;) + { + ++n; + for (int a, b, w, i = ecnt = 0; i < m; ++i) + { + scanf("%d%d%s%d", &a, &b, ord, &w); + b += a + 1; + if (*ord == 'g') + { + add_edge(b, a, -w - 1); + } + else + { + add_edge(a, b, w - 1); + } + } + for (int i = 1; i <= n; ++i) + { + add_edge(0, i, 0); + d[i] = INF; + } + for (int i = 1; i <= n; ++i) + { + for (int j = 0; j < ecnt; ++j) + { + int from = e[j].from, to = e[j].to; + int temp = d[from] + e[j].w; + if (temp < d[to]) + { + d[to] = temp; + } + } + } + bool impossible = false; + for (int j = 0; j < ecnt; ++j) + { + int from = e[j].from, to = e[j].to; + if (d[from] + e[j].w < d[to]) + { + impossible = true; + } + } + puts(impossible ? "successful conspiracy" : "lamentable kingdom"); + } + return 0; +} diff --git a/516.cpp b/516.cpp index cc088d6..2c12b89 100644 --- a/516.cpp +++ b/516.cpp @@ -1,65 +1,128 @@ -#include -#include -#include -#include -#include -#include +#include + using namespace std; -const long long MAX = 32800; -bool prime[MAX + 1]; -vector< long long > P; +bool prime[1000010]; +void compute_prime_table() /* with Sieve of Eratosthenes */ +{ + int i, j; + prime[0] = prime[1] = false; + for (i = 2; i <= 1000000; i++) + { + prime[i] = true; /* initialization */ + } + for (i = 2; i <= 1000;) /* for all primes up to 1000 */ + { + for (j = i + i; j <= 1000000; j += i) + { + prime[j] = false; /* delete all multiples of i */ + } + for (i++; !prime[i]; i++) + ; /* find next prime */ + } +} -void sieve() { - memset(prime, true, sizeof prime); - prime[0] = false; - prime[1] = false; +class PrimeLand +{ +public: + int ReadCase(); + void solve(); + //Data Information : + int numCase; + char buf[10000]; + int base[100], power[100]; + int resbase[100], respower[100], resnum; + long x; +}; - for (long long i = 2; i <= MAX; i++) { - if (prime[i]) { - P.push_back(i); - for (long long j = i * i; j <= MAX; j += i) { - prime[j] = false; - } +int PrimeLand::ReadCase() +{ + numCase = 0; + if (cin.getline(buf, 10000)) + { + if (strcmp(buf, "0") == 0) + { + return 0; } + istringstream input(buf); + int m, n; + while (input >> m >> n) + { + base[numCase] = m; + power[numCase] = n; + numCase++; + } + return 1; + } + else + { + return 0; } } -int main() { - sieve(); - while (true) { - string str; - getline(cin, str); - stringstream ss; - long long A, B, N = 1; - ss << str; - ss >> A; - if (A == 0) break; - ss >> B; - do { - N = N * pow(A, B); - } while( ss >> A >> B); - --N; - - map Map; - map::iterator it; - map::reverse_iterator rit; - for (long long i = 0; i < P.size(); i++) { - if (N == 1) break; - while (N % P[i] == 0) { - ++Map[P[i]]; - N /= P[i]; +void PrimeLand::solve() +{ + int i; + bool flag = false; + x = 1; + resnum = 0; + for (i = 0; i < numCase; i++) + { + x *= (long)pow(base[i], power[i]); + } + x--; + for (i = 2; i <= 100000; i++) + { + if (prime[i] == true) + { + if (x % i == 0) + { + resbase[resnum] = i; + respower[resnum] = 0; + } + else if (x % i != 0) + { + continue; } + while (1) + { + if (x == 1) + { + flag = true; + break; + } + if (x % i == 0) + { + x /= i; + respower[resnum]++; + } + else if (x % i != 0) + { + break; + } + } + resnum++; } - if (N > 1) ++Map[N]; - - for (rit = Map.rbegin(); rit != Map.rend(); rit++) { - if (rit != Map.rbegin()) - cout << " "; - cout << (*rit).first << " " << (*rit).second; + if (flag == true) + { + break; } - cout << endl; + } + for (i = resnum - 1; i > 0; i--) + { + cout << resbase[i] << ' ' << respower[i] << ' '; + } + cout << resbase[i] << ' ' << respower[i] << endl; +} + +int main() +{ + compute_prime_table(); + PrimeLand test; + while (test.ReadCase()) + { + test.solve(); } return 0; } diff --git a/517.cpp b/517.cpp new file mode 100644 index 0000000..8503b57 --- /dev/null +++ b/517.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +int Trans[100]; +vector V, R; +map M; +string ini, rules; +int N, Ind; + +int Gen() +{ + string temp = ""; + int i, a, b, c, d, ind, v; + Ind = 0; + V.push_back(""); + while (1) + { + temp = ""; + for (i = 0; i < N; i++) + { + ind = (i - 2 + N) % N; + a = ini[ind] - 'a'; + b = ini[i] - 'a'; + c = ini[(i + 1) % N] - 'a'; + v = a * 4 + b * 2 + c; + temp += Trans[v] + 'a'; + } + if (M[temp]) + { + return M[temp]; + break; + } + M[temp] = ++Ind; + ini = temp; + V.push_back(temp); + } +} +void Cal(int n) +{ + int s, i, d, id, k; + string a, b; + k = Gen(); + if (n < k) + a = V[n]; + else + { + s = V.size() - 1; + d = s - k + 1; + n -= k; + n %= d; + a = V[k + n]; + } + for (i = 0; i < N; i++) + { + id = i; + b = ""; + s = 0; + while (s++ < N) + { + b += a[id]; + id++; + id %= N; + } + R.push_back(b); + } + sort(R.begin(), R.end()); + cout << R[0] << endl; +} +void Free() +{ + M.clear(); + V.clear(); + R.clear(); +} + +int main() +{ + int n, a, b, c, d, v; + while (cin >> N) + { + cin >> ini; + n = 8; + while (n--) + { + cin >> rules; + a = rules[0] - 'a'; + b = rules[1] - 'a'; + c = rules[2] - 'a'; + d = rules[3] - 'a'; + v = a * 4 + b * 2 + c; + Trans[v] = d; + } + cin >> n; + if (n == 0) + { + cout << ini << endl; + continue; + } + Cal(n); + Free(); + } + return 0; +} diff --git a/518.cpp b/518.cpp new file mode 100644 index 0000000..b4bb01e --- /dev/null +++ b/518.cpp @@ -0,0 +1,197 @@ +#include + +using namespace std; + +int isLeap(int); + +char *str[] = {"year", "month", "day", "hour", "minute", "second"}; + +int dayinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + +struct Time +{ +public: + int value[6]; + Time(void){}; + + void increase(int index) + { + switch (index) + { + case 5: + if (value[5] == 59) + { + value[5] = 0; + increase(4); + } + value[5]++; + return; + case 4: + if (value[4] == 59) + { + value[4] = 0; + increase(3); + } + value[4]++; + return; + case 3: + if (value[3] == 23) + { + value[3] = -1; + increase(2); + } + value[3]++; + return; + case 2: + if (value[2] == dayinmonth[value[1]] + (value[1] == 2 ? (isLeap(value[0])) : 0)) + { + value[2] = 0; + increase(1); + } + value[2]++; + return; + case 1: + if (value[1] == 12) + { + value[1] = 0; + increase(0); + } + value[1]++; + return; + case 0: + value[0]++; + } + } + + void ceil(int index) + { + int i; + int x = 0; + for (i = index + 1; i < 6; i++) + { + if (value[i]) + { + x = 1; + value[i] = 0; + } + } + if (x) + { + increase(index); + } + } + + void floor(int index) + { + int i; + for (i = index + 1; i < 6; i++) + { + value[i] = 0; + } + } +}; +int isLeap(int year) +{ + if (year % 400 == 0) + { + return 1; + } + if (year % 100 == 0) + { + return 0; + } + if (year % 4 == 0) + { + return 1; + } + return 0; +} + +long sub(Time &t1, Time &t2, int index) +{ + int day = 0; + switch (index) + { + case 0: + return (t2.value[0] - t1.value[0]); + case 1: + return (t2.value[0] - t1.value[0]) * 12 + t2.value[1] - t1.value[1]; + default: + while (1) + { + if (t1.value[1] == t2.value[1] && t1.value[0] == t2.value[0]) + { + break; + } + day += (dayinmonth[t1.value[1] - 1] + ((t1.value[1] == 2) ? isLeap(t1.value[0]) : 0)); + t1.increase(1); + } + t1.value[0] = 0; + t2.value[0] = 0; + t1.value[1] = 0; + t2.value[1] = 0; + t2.value[2] += day; + } + switch (index) + { + case 2: + return (t2.value[2] - t1.value[2]); + case 3: + return (t2.value[2] - t1.value[2]) * 24 + t2.value[3] - t1.value[3]; + case 4: + return (t2.value[2] - t1.value[2]) * 24 * 60 + (t2.value[3] - t1.value[3]) * 60 + t2.value[4] - t1.value[4]; + case 5: + return (t2.value[2] - t1.value[2]) * 24 * 60 * 60 + (t2.value[3] - t1.value[3]) * 60 * 60 + (t2.value[4] - t1.value[4]) * 60 + t2.value[5] - t1.value[5]; + } + return -1; +} + +void show(const Time &t) +{ + for (int i = 0; i < 6; i++) + { + cout << t.value[i] << " "; + } + cout << endl; +} + +int main() +{ + Time t1, t2; + int i; + int x; + char s[20]; + int y; + while (1) + { + for (i = 0; i < 6; i++) + { + cin >> t1.value[i]; + if (!cin) + { + return 0; + } + } + for (i = 0; i < 6; i++) + { + cin >> t2.value[i]; + } + cin >> x; + cin >> s; + for (i = 0; i < 6; i++) + { + if (!strcmp(s, str[i])) + { + y = i; + break; + } + } + assert(i < 6); + t1.ceil(y); + t2.floor(y); + //show(t1); + //show(t2); + cout << (sub(t1, t2, y)) / x << endl; + } + return 0; +} diff --git a/519.cpp b/519.cpp new file mode 100644 index 0000000..983ecf0 --- /dev/null +++ b/519.cpp @@ -0,0 +1,200 @@ +#include + +using namespace std; + +char sd[] = "FOI"; +char IND[100], F[100]; +int B[8][8]; + +struct sss +{ + string pice; + int val; +}; +struct xx +{ + char left, up; +}; +vector v; +int R, C; +void Ini() +{ + int i, j; + for (i = 0; sd[i]; i++) + { + j = sd[i]; + IND[j] = i; + } +} +int Val(char ss[]) +{ + int i, sum = 0, j = 1, k; + for (i = 0; ss[i]; i++) + { + k = ss[i]; + k = IND[k]; + sum += k * j; + j *= 3; + } + return sum; +} +xx Next(int r, int c) +{ + xx temp; + int k; + if (c == 1) + temp.left = 'F'; + else + { + k = B[r][c - 1]; + if (v[k].pice[1] == 'F') + temp.left = 'F'; + else if (v[k].pice[1] == 'I') + temp.left = 'O'; + else + temp.left = 'I'; + } + if (r == 1) + temp.up = 'F'; + else + { + k = B[r - 1][c]; + switch (v[k].pice[2]) + { + case 'F': + temp.up = 'F'; + break; + case 'O': + temp.up = 'I'; + break; + case 'I': + temp.up = 'O'; + break; + } + } + return temp; +} +int Fit(int n, int r, int c) +{ + int k; + if (r == 1 && v[n].pice[0] != 'F') + return 0; + if (r > 1) + { + k = B[r - 1][c]; + if (v[k].pice[2] == 'O' && v[n].pice[0] != 'I') + return 0; + if (v[k].pice[2] == 'I' && v[n].pice[0] != 'O') + return 0; + } + if (r < R && v[n].pice[2] == 'F') + return 0; + if (r == R && v[n].pice[2] != 'F') + return 0; + if (c == 1 && v[n].pice[3] != 'F') + return 0; + if (c > 1) + { + k = B[r][c - 1]; + if (v[k].pice[1] == 'O' && v[n].pice[3] != 'I') + return 0; + if (v[k].pice[1] == 'I' && v[n].pice[3] != 'O') + return 0; + } + if (c < C && v[n].pice[1] == 'F') + return 0; + if (c == C && v[n].pice[1] != 'F') + return 0; + return 1; +} +int Recur(int n, int level, int r, int c) +{ + int i, j, nr, nc; + int a[100] = {0}; + if (level == R * C) + return 1; + B[r][c] = n; + nr = r; + nc = c + 1; + if (c == C) + { + nr = r + 1; + nc = 1; + } + F[n] = 1; + for (i = 0; i < v.size(); i++) + { + if (F[i]) + continue; + if (!Fit(i, nr, nc)) + continue; + if (a[v[i].val]) + continue; + a[v[i].val] = 1; + if (Recur(i, level + 1, nr, nc)) + return 1; + } + F[n] = 0; + return 0; +} +void Cal(int cat, int jut, int flt) +{ + int a[100] = {0}; + int i, f = 0, d; + xx temp; + d = 2 * R + 2 * C; + if (cat != jut || d != flt) + { + cout << "NO\n"; + return; + } + for (i = 0; i < v.size(); i++) + { + if (!Fit(i, 1, 1)) + continue; + if (a[v[i].val]) + continue; + a[v[i].val] = 1; + if (Recur(i, 1, 1, 1)) + { + f = 1; + break; + } + } + if (f) + cout << "YES\n"; + else + cout << "NO\n"; +} + +int main() +{ + int i, cav, jut, j, flt; + char ss[10]; + sss temp; + Ini(); + while (cin >> R >> C) + { + if (!R && !C) + break; + cav = jut = flt = 0; + for (i = 0; i < R * C; i++) + { + cin >> ss; + for (j = 0; ss[j]; j++) + if (ss[j] == 'I') + cav++; + else if (ss[j] == 'O') + jut++; + else + flt++; + temp.pice = ss; + temp.val = Val(ss); + v.push_back(temp); + } + Cal(cav, jut, flt); + memset(F, 0, 100); + v.clear(); + } + return 0; +} diff --git a/520.cpp b/520.cpp new file mode 100644 index 0000000..b3d0a28 --- /dev/null +++ b/520.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +char s[256]; +int queue_[1024]; + +int main() +{ + int current, pi, ri; + unsigned head, tail, total; + char *p; + current = 0; + head = 0; + tail = 0; + total = 0; + for (;;) + { + if (!(p = gets(s))) + { + break; + } + if (!*p) + { + printf("%d\n", total + (tail - head + 1024) % 1024 - 1); + total = 0; + head = 0; + tail = 0; + continue; + } + pi = strtol(p, &p, 10); + if (!pi) + { + queue_[tail] = current++; + tail = (tail + 1) % 1024; + if ((tail - head + 1024) % 1024 > 1012) + { + head = (head + 1) % 1024; + total++; + } + } + else + { + ri = strtol(p, &p, 10); + while (current - queue_[(tail - 1) % 1024] < pi) + { + tail = (tail - 1) % 1024; + } + current += ri; + } + } +} diff --git a/521.cpp b/521.cpp new file mode 100644 index 0000000..138af30 --- /dev/null +++ b/521.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +#define maxn 21 + +int line, dri, stop; +vector L[maxn]; +char links[52][52], V[52]; +queue Q; + +struct driver +{ + int line, st; +} D[52]; + +int ReadCase() +{ + char ss[1000], *p; + int l, s, d; + string temp; + gets(ss); + temp = ss; + istringstream in(temp); + in >> line >> dri >> stop; + if (!line && !dri && !stop) + return 0; + for (l = 1; l <= line; l++) + { + gets(ss); + L[l].clear(); + p = strtok(ss, " "); + while (p) + { + L[l].push_back(atoi(p)); + p = strtok(NULL, " "); + } + gets(ss); + p = strtok(ss, " "); + while (p) + { + s = atoi(p); + p = strtok(NULL, " "); + d = atoi(p); + p = strtok(NULL, " "); + D[d].line = l; + D[d].st = s; + } + } + return 1; +} +int isConnect(int u, int v) +{ + int i, l1, l2, iu, iv; + if (D[u].line == D[v].line && D[u].st != D[v].st) + return 0; + if (D[v].st == D[u].st) + return 1; + for (iu = 0; iu < L[D[u].line].size(); iu++) + if (L[D[u].line][iu] == D[u].st) + break; + for (iv = 0; iv < L[D[v].line].size(); iv++) + if (L[D[v].line][iv] == D[v].st) + break; + iu--; + iv--; + l1 = L[D[u].line].size(); + l2 = L[D[v].line].size(); + for (i = 0; i <= 100; i++) + { + iu++; + iv++; + iu %= l1; + iv %= l2; + if (L[D[u].line][iu] == L[D[v].line][iv]) + return 1; + } + return 0; +} +void MakeLink() +{ + int i, j; + for (i = 1; i < dri; i++) + for (j = i + 1; j <= dri; j++) + if (isConnect(i, j)) + links[i][j] = links[j][i] = 1; +} +void BFS(int u) +{ + int i, v; + V[u] = 1; + Q.push(u); + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (i = 1; i <= dri; i++) + if (links[u][i] == 1 && V[i] == 0) + { + V[i] = 1; + Q.push(i); + } + } +} +int Cal() +{ + int i, total = 0; + MakeLink(); + BFS(1); + for (i = 1; i <= dri; i++) + if (V[i] == 0) + return 0; + return 1; +} +void Free() +{ + int i; + memset(links, 0, 52 * 52); + memset(V, 0, 52); +} + +int main() +{ + while (ReadCase()) + { + if (Cal()) + cout << "Yes\n"; + else + cout << "No\n"; + Free(); + } + return 0; +} diff --git a/523.cpp b/523.cpp new file mode 100644 index 0000000..2507573 --- /dev/null +++ b/523.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +/* +Minimum Transport Cost +523 +*/ +#define MIN(a, b) (a > b ? b : a) +#define MAXN 202 +#define INF 21474836 +int D[MAXN][MAXN], N; +int P[MAXN][MAXN]; +char text[10000]; +struct ss +{ + int tax; +} V[MAXN]; +void Ini() +{ + int i; + for (i = 0; i < N; i++) + { + P[i][i] = -1; + } +} +void ReadCase() +{ + int i, j, c; + char *p; + for (i = 1; i < N; i++) + { + gets(text); + p = strtok(text, " "); + j = 0; + c = atoi(p); + if (c >= 0) + { + D[i][j] = c; + P[i][j] = i; + j++; + } + else + { + D[i][j] = INF; + P[i][j] = -1; + j++; + } + while (p) + { + p = strtok(NULL, " "); + if (p) + { + c = atoi(p); + if (c >= 0) + { + D[i][j] = c; + P[i][j] = i; + j++; + } + else + { + D[i][j] = INF; + P[i][j] = -1; + j++; + } + } + } + } + j = 0; + gets(text); + p = strtok(text, " "); + if (p) + { + V[j++].tax = atoi(p); + } + while (p) + { + p = strtok(NULL, " "); + if (p) + { + V[j++].tax = atoi(p); + } + } +} +void Flyod() +{ + int i, j, k; + for (k = 0; k < N; k++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (D[i][j] > (D[i][k] + D[k][j] + V[k].tax)) + { + D[i][j] = D[i][k] + D[k][j] + V[k].tax; + P[i][j] = P[k][j]; + } + else + { + D[i][j] = D[i][j]; + P[i][j] = P[i][j]; + } + } + } + } +} +void PrintPath(int a, int b) +{ + if (a == b) + { + printf("%d", a + 1); + } + else + { + PrintPath(a, P[a][b]); + printf("-->%d", b + 1); + } +} +void Cal() +{ + int a, b, i, c = 0; + Flyod(); + while (gets(text)) + { + for (i = 0; text[i]; i++) + if (text[i] == '\n') + { + text[i] = NULL; + } + if (strlen(text) == 0) + { + break; + } + sscanf(text, "%d%d", &a, &b); + a--; + b--; + if (c++) + { + putchar('\n'); + } + printf("From %d to %d :\n", a + 1, b + 1); + printf("Path: "); + PrintPath(a, b); + putchar('\n'); + printf("Total cost : %d\n", D[a][b]); + } +} +int main() +{ + char input[100], *p; + int kase, c; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + N = 0; + gets(text); + p = strtok(text, " "); + c = atoi(p); + if (c >= 0) + { + D[0][N] = c; + P[0][N] = 0; + N++; + } + else + { + D[0][N] = INF; + P[0][N] = -1; + N++; + } + while (p) + { + p = strtok(NULL, " "); + if (p) + { + c = atoi(p); + if (c >= 0) + { + D[0][N] = c; + P[0][N] = 0; + N++; + } + else + { + D[0][N] = INF; + P[0][N] = -1; + N++; + } + } + } + ReadCase(); + Ini(); + Cal(); + if (kase) + { + printf("\n"); + } + } + return 0; +} diff --git a/524.cpp b/524.cpp new file mode 100644 index 0000000..eb5aa9f --- /dev/null +++ b/524.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int prime[100], ring[20]; + +bool isPrime(int n) +{ + for (int i = 2; i <= (int)sqrt(n * 1.0); i++) + if (n % i == 0) + { + return false; + } + return true; +} + +void calPrime() +{ + memset(prime, 0, sizeof(prime)); + for (int n = 2; n < 100; n++) + if (isPrime(n)) + { + prime[n] = true; + } +} + +void backtrace(int index, int n, int p[]) +{ + if (index == n) + { + int t; + for (t = 2; p[t]; t++) + ; + ring[index] = t; + int a = ring[index - 1] + ring[index]; + int b = ring[index] + 1; + if (prime[a] && prime[b]) + { + int i; + for (i = 1; i < n; i++) + { + cout << ring[i] << " "; + } + cout << ring[i] << endl; + } + return; + } + for (int t = 2; t <= n; t++) + if (!p[t]) + { + int a = t + ring[index - 1]; + if (prime[a]) + { + p[t] = 1; + ring[index] = t; + backtrace(index + 1, n, p); + p[t] = 0; + } + } +} + +void solve(int n) +{ + int p[20] = {0}; + ring[1] = 1; + backtrace(2, n, p); +} + +int main() +{ + calPrime(); + int kase = 1, n; + bool f = false; + while (cin >> n) + { + if (f) + { + cout << endl; + } + cout << "Case " << kase++ << ":" << endl; + solve(n); + f = true; + } + return 0; +} diff --git a/526.cpp b/526.cpp new file mode 100644 index 0000000..7b32f69 --- /dev/null +++ b/526.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define MAX 85 +#define MIN(a, b) (a >= b ? b : a) + +const char ss[3][3] = {"C", "I", "D"}; +char st[MAX], ter[MAX]; +int l1, l2, ind, cnt; + +struct dyn {int r, c, com, val;} M[MAX][MAX]; + +void IniC() +{ + int i; + M[0][0].val = 0; + for (i = 1; st[i]; i++) + { + M[0][i].val = i; + M[0][i].r = 0; + M[0][i].c = i - 1; + M[0][i].com = 2; + } +} +void IniR() +{ + int i; + for (i = 1; ter[i]; i++) + { + M[i][0].val = i; + M[i][0].r = i - 1; + M[i][0].c = 0; + M[i][0].com = 1; + } +} +void Select(int r, int c, int rp, int Ins, int Delt) +{ + int nr, nc, cm, temp, val; + temp = MIN(rp, Ins); + if (rp <= Ins) + { + nr = r - 1; + nc = c - 1; + cm = 0; + val = rp; + } + else + { + nr = r - 1; + nc = c; + cm = 1; + val = Ins; + } + if (temp > Delt) + { + nr = r; + nc = c - 1; + cm = 2; + val = Delt; + } + M[r][c].val = val; + M[r][c].r = nr; + M[r][c].c = nc; + M[r][c].com = cm; + if (ter[r] == st[c]) + { + M[r][c].com = 4; + } +} +void Dynamic() +{ + int i, j, k, val, p, q; + l1 = strlen(&ter[1]); + l2 = strlen(&st[1]); + for (i = 1; ter[i]; i++) + { + for (j = 1; st[j]; j++) + { + val = (ter[i] == st[j]) ? 0 : 1; + k = M[i - 1][j - 1].val + val; + p = M[i - 1][j].val + 1; + q = M[i][j - 1].val + 1; + Select(i, j, k, p, q); + } + } +} +void Path(int r, int c) +{ + if (r == 0 && c == 0) + { + return; + } + Path(M[r][c].r, M[r][c].c); + if (M[r][c].com == 2) + { + printf("%d Delete %d\n", cnt++, ind); + } + else if (M[r][c].com == 1) + { + printf("%d Insert %d,%c\n", cnt++, ind, ter[r]); + } + else if (M[r][c].com == 0) + { + printf("%d Replace %d,%c\n", cnt++, ind, ter[r]); + } + if (M[r][c].com != 2) + { + ind++; + } +} + +int main() +{ + int f = 0; + while (gets(&st[1])) + { + gets(&ter[1]); + if (f++) + { + printf("\n"); + } + IniR(); + IniC(); + Dynamic(); + ind = cnt = 1; + printf("%d\n", M[l1][l2].val); + Path(l1, l2); + } + return 0; +} diff --git a/527.cpp b/527.cpp new file mode 100644 index 0000000..d471088 --- /dev/null +++ b/527.cpp @@ -0,0 +1,135 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +const double eps = 1e-10; + +class Point +{ +public: + P a; + Point(){}; + Point(P in) + { + a = in; + } + bool operator<(const Point &b) const + { + if (abs(a.real() - b.a.real()) > eps) + { + return a.real() < b.a.real(); + } + else if (abs(a.imag() - b.a.imag()) > eps) + { + return a.imag() < b.a.imag(); + } + return false; + } +}; + +double cross(P a, P b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} + +bool isp(P a, P b, P c) +{ + return abs(a - c) + abs(b - c) < abs(a - b) + eps; +} + +bool is_intersectedls(P a1, P a2, P b1, P b2) +{ + if (cross(a2 - a1, b1 - a1) * cross(a2 - a1, b2 - a1) < eps && + cross(b2 - b1, a1 - b1) * cross(b2 - b1, a2 - b1) < eps) + { + return true; + } + return false; +} + +Point intersection_ls(P a1, P a2, P b1, P b2) +{ + P b = b2 - b1; + double d1 = abs(cross(b, a1 - b1)); + double d2 = abs(cross(b, a2 - b1)); + double t = d1 / (d1 + d2); + return Point(a1 + (a2 - a1) * t); +} + +int solve(int n, P *in, P *in2, bool *flag) +{ + int cnt = 1; + Point data; + rep(i, n) + { + if (flag[i] == false) + { + continue; + } + set S; + rep(j, i) + { + if (is_intersectedls(in[i], in2[i], in[j], in2[j])) + { + data = intersection_ls(in[i], in2[i], in[j], in2[j]); + if (data.a.real() > 0 && data.a.real() < 1000 && + data.a.imag() > 0 && data.a.imag() < 1000 && S.find(data) == S.end()) + { + S.insert(data); + } + } + } + cnt += S.size() + 1; + } + return cnt; +} + +int main() +{ + int te, tc = 0; + scanf("%d", &te); + while (te--) + { + if (tc) + { + puts(""); + } + tc = 1; + int n; + scanf("%d", &n); + bool flag[n]; + P in[n], in2[n]; + rep(i, n) + { + double re,im; + scanf("%lf%lf%", &re,&im); + in[i] = P(re,im); + scanf("%lf%lf%", &re,&im); + in2[i] = P(re,im); + flag[i] = true; + } + rep(i, n) + { + REP(j, i + 1, n) + { + if (in[i].real() == in[j].real() && in[i].imag() == in[j].imag() && + in2[i].real() == in2[j].real() && in2[i].imag() == in2[j].imag()) + { + flag[j] = false; + } + if (in[i].real() == in2[j].real() && in[i].imag() == in2[j].imag() && + in2[i].real() == in[j].real() && in2[i].imag() == in[j].imag()) + { + flag[j] = false; + } + } + } + cout << solve(n, in, in2, flag) << endl; + } + return 0; +} diff --git a/529.cpp b/529.cpp new file mode 100644 index 0000000..02255c3 --- /dev/null +++ b/529.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int S[100], MaxLen, found; +int T[100], N; +void Copy(int level) +{ + int i; + for (i = 0; i <= level; i++) + { + S[i] = T[i]; + } + MaxLen = level; +} +void Recur(int n, int level) +{ + int i, j, k = n, p = 0; + if (level >= MaxLen) + { + return; + } + if (n > N) + { + return; + } + T[level] = n; + if (N == n) + { + Copy(level); + found = 1; + return; + } + if (found) + { + for (j = 1; k < N; j++) + { + k *= 2; + p++; + } + if (level + p >= MaxLen) + { + return; + } + } + for (i = level; i >= 0; i--) + { + Recur(n + T[i], level + 1); + } +} +void Print() +{ + int i; + printf("1"); + for (i = 1; i <= MaxLen; i++) + { + printf(" %d", S[i]); + } + printf("\n"); +} +int main() +{ + while (scanf("%d", &N), N) + { + MaxLen = 10000; + found = 0; + Recur(1, 0); + Print(); + } + return 0; +} diff --git a/530.cpp b/530.cpp index 3245752..4960728 100644 --- a/530.cpp +++ b/530.cpp @@ -1,35 +1,53 @@ -#include - -using namespace std; - -unsigned long long combination(int n, int m) -{ - unsigned long long combo = 1; - if(m<(n-m)) - m=n-m; - - for(int i = n, j = 1; i > m; i--) - { - combo *= i; - - while((combo%j)==0 && (j<=n-m)) - { - combo/=j; - j++; - } - } - return combo; -} - -int main() -{ - int n, m; - while(cin>>n>>m) - { - if((n==0)&&(m==0)) - break; - else - cout << combination(n, m) << endl; - } - return 0; -} +#include + +using namespace std; + +int main() +{ + unsigned int n, k; + vector top, bottom; + while (cin >> n >> k) + { + if (n == 0 && k == 0) + { + break; + } + top.clear(); + bottom.clear(); + if (n - k > k) + { + k = n - k; + } + for (unsigned int i = k + 1; i <= n; i++) + { + top.push_back(i); + } + for (unsigned int i = 2; i <= n - k; i++) + { + bottom.push_back(i); + } + for (unsigned int i = 0, sz = top.size(); i < sz; i++) + { + for (unsigned int y = 0, sz2 = bottom.size(); y < sz2; y++) + { + if (bottom[y] > 1 && top[i] % bottom[y] == 0) + { + top[i] /= bottom[y]; + bottom[y] = 1; + } + } + } + unsigned long long t = 1; + unsigned long long b = 1; + for (int i = 0, sz = top.size(); i < sz; i++) + { + t *= top[i]; + } + for (int i = 0, sz = bottom.size(); i < sz; i++) + { + b *= bottom[i]; + } + cout << t / b << endl; + } + return 0; +} diff --git a/531.cpp b/531.cpp index 2efa3f6..0993f26 100644 --- a/531.cpp +++ b/531.cpp @@ -1,78 +1,85 @@ -#include -#include -using namespace std; - -int main(){ - string str; - while(cin>>str) - { - bool ctr=true; - string row[110],col[110]; - int m=1, n=1; - if(str=="#") - ctr=false; - else - row[m++]=str; - - while(ctr==true){ - cin>>str; - if(str=="#") - { - ctr=false; - break; - } - row[m++]=str; - } - while(true){ - cin>>str; - if(str=="#") - break; - else - col[n++]=str; - } - // cout<=tower[i][j-1]) - tower[i][j]=tower[i-1][j]; - else - tower[i][j] = tower[i][j-1]; - } - - } - } - string lcs[110]; - int k=0; - i=m-1; - j=n-1; - while(i>0 && j>0) - { if(row[i]==col[j]) - { - lcs[k++]=col[j]; - i--; - j--; - } - else if(tower[i-1][j]==tower[i][j]) - i--; - else - j--; - } - for(i=k-1;i>=0;i--) - { - cout< + +using namespace std; + +#define MAX 100 + +char s1[MAX + 1][30], s2[MAX + 1][30]; +int dp[MAX + 1][MAX + 1]; +int n, m; + +void LCS() +{ + int i, j; + for (i = 0; i <= n; i++) + { + dp[i][m] = 0; + } + for (j = 0; j <= m; j++) + { + dp[n][j] = 0; + } + for (i = n - 1; i >= 0; i--) + { + for (j = m - 1; j >= 0; j--) + { + if (strcmp(s1[i], s2[j]) == 0) + { + dp[i][j] = 1 + dp[i + 1][j + 1]; + } + else + { + dp[i][j] = max(dp[i][j + 1], dp[i + 1][j]); + } + } + } +} + +int main() +{ + int i, j; + bool first; + while (scanf("%s", s1[0]) != EOF) + { + n = 1; + while (scanf("%s", s1[n]) && (strcmp(s1[n], "#") != 0)) + { + n++; + } + m = 0; + while (scanf("%s", s2[m]) && (strcmp(s2[m], "#") != 0)) + { + m++; + } + LCS(); + first = true; + i = j = 0; + while (dp[i][j] != 0) + { + if (strcmp(s1[i], s2[j]) == 0) + { + if (first == true) + { + printf("%s", s1[i]); + first = false; + } + else + { + printf(" %s", s1[i]); + } + i++; + j++; + } + else if (dp[i + 1][j] >= dp[i][j + 1]) + { + i++; + } + else + { + j++; + } + } + printf("\n"); + } + return 0; +} diff --git a/532.cpp b/532.cpp index a7d53b9..89b2b5f 100644 --- a/532.cpp +++ b/532.cpp @@ -1,35 +1,102 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +#define INF 0x7FFFFFFF +int l, r, c, x, y, z, X[6] = {1, -1, 0, 0, 0, 0}, Y[6] = {0, 0, 1, -1, 0, 0}, Z[6] = {0, 0, 0, 0, 1, -1}, Qx[28000], Qy[28000], Qz[28000], ptr, len, L[35][35][35]; +char D[35][35][35]; +void push(int tx, int ty, int tz, int ll) +{ + if (D[tx][ty][tz] == '#' || L[tx][ty][tz] != INF) + { + return; + } + L[Qx[len] = tx][Qy[len] = ty][Qz[len] = tz] = ll + 1; + len++; +} +void pop() +{ + x = Qx[ptr]; + y = Qy[ptr]; + z = Qz[ptr]; +} +int main() +{ + int i, j, k; + for (i = 0; i < 35; i++) + { + for (j = 0; j < 35; j++) + { + D[0][i][j] = D[j][0][i] = D[i][j][0] = '#'; + } + } + while (scanf("%d%d%d", &l, &r, &c) == 3) + { + if ((l | r | c) == 0) + { + break; + } + for (i = 1; i <= l; i++) + { + for (j = 1; j <= r; j++) + { + scanf("%s", D[i][j] + 1); + for (k = 1; k <= c; k++) + { + L[i][j][k] = INF; + if (D[i][j][k] != 'S') + { + continue; + } + x = i; + y = j; + z = k; + } + } + } + for (i = 1; i <= l; i++) + { + for (j = 1; j <= r; j++) + { + D[i][j][c + 1] = '#'; + } + } + for (j = 1; j <= r; j++) + { + for (k = 1; k <= c; k++) + { + D[l + 1][j][k] = '#'; + } + } + for (k = 1; k <= c; k++) + { + for (i = 1; i <= l; i++) + { + D[i][r + 1][k] = '#'; + } + } + len = 0; + push(x, y, z, -1); + for (ptr = 0; ptr < len; ptr++) + { + pop(); + if (D[x][y][z] == 'E') + { + break; + } + for (i = 0; i < 6; i++) + { + push(x + X[i], y + Y[i], z + Z[i], L[x][y][z]); + } + } + if (D[x][y][z] == 'E') + { + printf("Escaped in %d minute(s).\n", L[x][y][z]); + } + else + { + printf("Trapped!\n"); + } + } return 0; } diff --git a/533.cpp b/533.cpp new file mode 100644 index 0000000..d868978 --- /dev/null +++ b/533.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +char buf[1005]; +int lx, ly, rx, ry, *px, *py, nxt[1005], stk[1005]; + +void deal(char *s) +{ + int i, j, k; + for (i = 0, j = 0, k = 0; s[i]; i++) + { + if (s[i] != ' ') + { + if (s[i] == '(') + { + stk[k++] = j; + } + else + { + if (s[i] == ')') + { + nxt[stk[--k]] = j + 1; + } + nxt[j] = j + 1; + } + s[j++] = s[i]; + } + } + s[j] = 0; +} + +void recursion(char *s, int left, int right) +{ + int i, x, y; + if (s[left] == '(' && nxt[left] > right) + { + recursion(s, left + 1, right - 1); + return; + } + for (i = left; i <= right; i = nxt[i]) + { + if (s[i] == '+' || s[i] == '-') + { + recursion(s, left, i - 1); + x = *px; + y = *py; + recursion(s, i + 1, right); + if (s[i] == '+') + { + x += *px; + y += *py; + } + else + { + x -= *px; + y -= *py; + } + *px = x; + *py = y; + return; + } + } + for (i = left; i <= right; i = nxt[i]) + { + if (s[i] == '*') + { + recursion(s, left, i - 1); + x = *px; + y = *py; + recursion(s, i + 1, right); + x = *px * y + *py * x; + y *= *py; + *px = x; + *py = y; + return; + } + } + if (s[left] == 'x') + { + *px = 1; + *py = 0; + } + else + { + *px = 0; + sscanf(s + left, "%d", py); + } +} + +int main() +{ + int cas; + char *lptr, *rptr; + cas = 0; + while (gets(buf)) + { + px = &lx; + py = &ly; + lptr = strtok(buf, "="); + deal(lptr); + recursion(lptr, 0, strlen(lptr) - 1); + px = ℞ + py = &ry; + rptr = strtok(NULL, "="); + deal(rptr); + recursion(rptr, 0, strlen(rptr) - 1); + lx -= rx; + ry -= ly; + if (cas) + { + printf("\n"); + } + printf("Equation #%d\n", ++cas); + if (lx) + { + printf("x = %.6lf\n", (double)ry / lx); + } + else + { + if (!ry) + { + printf("Infinitely many solutions.\n"); + } + else + { + printf("No solution.\n"); + } + } + } + return 0; +} diff --git a/534.cpp b/534.cpp index 72f4e69..bd612ba 100644 --- a/534.cpp +++ b/534.cpp @@ -1,59 +1,65 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define sqr(X) ((X) * (X)) -#define FOI(I, A, B) for (I = A; I <= B; I++) -struct Cod{ - double X; - double Y; -}; - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - int N; - cin >> N; - if (N == 0) - break; - Cod frog[N]; - int i, j, k; - FOI(i, 0, N-1) - cin >> frog[i].X >> frog[i].Y; - double mat[N][N]; - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = sqrt(sqr(frog[i].X - frog[j].X) + sqr(frog[i].Y - frog[j].Y)); - - FOI(k, 0, N-1) - FOI(i, 0, N-1) - FOI(j, 0, N-1) - mat[i][j] = min(mat[i][j], max(mat[i][k], mat[k][j])); - printf("Scenario #%d\nFrog Distance = %.3lf\n\n", t, mat[0][1]); - } - return 0; -} +#include + +using namespace std; + +#define MAX(A, B) (A > B ? A : B) +#define MIN(A, B) (A > B ? B : A) +#define MAXN 202 + +struct ss +{ + int a, b; +} V[202]; +int N, A[MAXN][MAXN]; + +void Ini() +{ + int i; + for (i = 1; i <= N; i++) + A[i][i] = 0; +} +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + A[i][j] = MIN(A[i][j], MAX(A[i][k], A[k][j])); +} +int Dis(int x1, int y1, int x2, int y2) +{ + int a, b; + a = (x1 - x2); + b = (y1 - y2); + return a * a + b * b; +} +void Cal() +{ + int i, j, c; + double dis; + for (i = 0; i < N - 1; i++) + { + for (j = i + 1; j < N; j++) + { + c = Dis(V[i].a, V[i].b, V[j].a, V[j].b); + A[i + 1][j + 1] = A[j + 1][i + 1] = c; + } + } + Floyd(); + dis = sqrt((double)A[1][2]); + printf("Frog Distance = %.3lf\n\n", dis); +} + +int main() +{ + int i, kase = 1; + while (scanf("%d", &N) && N) + { + Ini(); + for (i = 0; i < N; i++) + scanf("%d%d", &V[i].a, &V[i].b); + printf("Scenario #%d\n", kase++); + Cal(); + } + return 0; +} diff --git a/535.cpp b/535.cpp index a262ecf..fd6828c 100644 --- a/535.cpp +++ b/535.cpp @@ -1,76 +1,119 @@ -#include -#include -#include -using namespace std; -#define PI 3.141592653589793 -#define R 6378 -#define DEG 180.0 - -struct town{ - string name; - double longitude; - double latitude; -}; -double spherical_distance(double lat1,double lon1,double lat2,double lon2) { - double dlon = lon2 - lon1; - double dlat = lat2 - lat1; - double a = pow((sin(dlat/2)),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2), 2); - double c = 2 * atan2(sqrt(a), sqrt(1-a)); - double d = R * c; - return d; -} -int main(){ - town city[200]; - int n=0, i=0; - for(;;){ - cin>>city[n].name; - if(city[n].name=="#") - break; - cin>>city[n].latitude; - cin>>city[n].longitude; - city[n].latitude = (PI * city[n].latitude) / DEG; - city[n].longitude = (PI * city[n].longitude) / DEG; - n++; - } - for(;;){ - string source, destination; - cin>>source>>destination; - if(source=="#" && destination=="#") - break; - cout< + +using namespace std; + +#define RADIO 6378.0 +#define MAXCITIES 100 + +double PIx = 3.141592653589793; + +string cities[MAXCITIES]; +double latphi[MAXCITIES], lontheta[MAXCITIES]; +int n = 0; + +struct Point +{ + double x, y, z; +}; + +// dot product (pk-pi)°(pj-pi) +// =0 if perpendicular segments +double PointProduct(Point p1, Point p2) +{ + return (p1.x * p2.x + p1.y * p2.y + p1.z * p2.z); +} + +// norm of a vector, not used in the solution of this problem +float Norm(Point p) +{ + return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); +} + +int myRound(double x) +{ + return (int)(x + 0.5f); +} + +double radianes(double x) +{ + return x * PIx / 180; +} + +void solve(string temp1, string temp2) +{ + int i, j; + for (i = 0; i < n; i++) + if (cities[i] == temp1) + break; + for (j = 0; j < n; j++) + if (cities[j] == temp2) + break; + if (i == n || j == n) + { + cout << temp1 << " - " << temp2 << endl; + cout << "Unknown" << endl; + return; + } + /* + Point a,b; + a.x = cos(radianes(lontheta[i])) * sin(radianes(latphi[i])); + a.y = sin(radianes(lontheta[i])) * sin(radianes(latphi[i])); + a.z = cos(radianes(latphi[i])); + b.x = cos(radianes(lontheta[j])) * sin(radianes(latphi[j])); + b.y = sin(radianes(lontheta[j])) * sin(radianes(latphi[j])); + b.z = cos(radianes(latphi[j])); + double angulo = acos(PointProduct(a,b)/(Norm(a)*Norm(b))); + + But the rule of a and b is always 1! hence: + double angle = acos(PointProduct (a, b)); + + But there was no AC (apparently due to errors with coordinates + points very very close) so I tried to use arc-sine ... + double angle = 2 * asin (sqrt (((1-PointProduct (a, b)) / 2))); + + ...which is even worse, finally used the formula Haversine. + @ http://mathforum.org/library/drmath/view/51879.html +*/ + double lon2 = radianes(lontheta[i]); + double lon1 = radianes(lontheta[j]); + double lat2 = radianes(latphi[i]); + double lat1 = radianes(latphi[j]); + double dlon = lon2 - lon1; + double dlat = lat2 - lat1; + double a = (sin(dlat / 2) * sin(dlat / 2)) + cos(lat1) * cos(lat2) * (sin(dlon / 2) * sin(dlon / 2)); + double angulo = 2 * atan2(sqrt(a), sqrt(1 - a)); + cout << temp1 << " - " << temp2 << endl; + cout << myRound(angulo * RADIO) << " km" << endl; +} + +int main() +{ + string temp1, temp2; + while (true) + { + cin >> temp1; + if (temp1 == "#") + break; + cities[n] = temp1; + cin >> latphi[n] >> lontheta[n]; + /* +Polar coordinates: theta angle no matter you go from -180 to 180 +but beware of the phi angle, it should go from 0 to 180 to +correctly represent the position on the surface of the sphere +latphi[n] += 90; +However, haversine angles always will range from -90 to 90 degrees +*/ + n++; + } + while (true) + { + cin >> temp1; + cin >> temp2; + if (temp1 == "#" && temp2 == "#") + { + break; + } + solve(temp1, temp2); + } + return 0; +} diff --git a/536.cpp b/536.cpp new file mode 100644 index 0000000..0bccfba --- /dev/null +++ b/536.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +char strPreOrder[30], strInOrder[30]; + +void backtrace(int leftPre, int rightPre, int leftIn, int rightIn) +{ + if (leftPre > rightPre || leftIn > rightIn) + { + return; + } + if (leftPre == rightPre) + { + cout << strPreOrder[leftPre]; + return; + } + int preRoot, inRoot; + preRoot = leftPre; + for (inRoot = leftIn; inRoot <= rightIn; inRoot++) + if (strInOrder[inRoot] == strPreOrder[preRoot]) + { + break; + } + int leftSize, rightSize; + leftSize = inRoot - leftIn; + rightSize = rightPre - leftPre - leftSize; + backtrace(leftPre + 1, leftPre + leftSize, leftIn, inRoot - 1); + backtrace(leftPre + leftSize + 1, rightPre, inRoot + 1, rightIn); + cout << strPreOrder[preRoot]; +} + +int main() +{ + while (cin >> strPreOrder >> strInOrder) + { + int n = strlen(strPreOrder); + backtrace(0, n - 1, 0, n - 1); + cout << endl; + } + return 0; +} diff --git a/537.cpp b/537.cpp index b8cffad..05e9da6 100644 --- a/537.cpp +++ b/537.cpp @@ -1,68 +1,80 @@ -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define abs(X) ((X) > 0 ? (X) : (-1.0 * X)) -const double EPS = 1e-9; -bool isP, isU, isI; -double P, U, I; -string str; +char *cp; -void decide(int A) { - string temp = ""; - double V; - int B; - for (B = A + 2; !isalpha(str[B]); B++) { - temp += str[B]; +void ReadNum(double &res) +{ + cp++; + if (*cp != '=') + { + return; + } + cp++; + sscanf(cp, "%lf", &res); + while (!isalpha(*cp)) + { + cp++; } - stringstream ss; - ss << temp; - ss >> V; - if (str[B] == 'm') V *= 0.001; - if (str[B] == 'k') V *= 1000; - if (str[B] == 'M') V *= 1000000; - //cout << V << endl; - - if (str[A] == 'P') { - isP = true; P = V; return; + if (*cp == 'm') + { + res /= 1000; } - else if (str[A] == 'U') { - isU = true; U = V; return; + else if (*cp == 'k') + { + res *= 1000; } - else if (str[A] == 'I') { - isI = true; I = V; return; + else if (*cp == 'M') + { + res *= 1000000; } } -int main() { - int T, t; - scanf("%d\n", &T); - for (t = 1; t <= T; t++) { - printf("Problem #%d\n", t); - getline(cin, str); - isP = false; isU = false; isI = false; - - int A = str.find_first_of("="); - decide(A - 1); - int B = str.find_last_of("="); - decide(B - 1); - - if (!isP) { - P = U * I; - printf("P=%.2lfW\n\n", (double)P); +void solve(char *buf) +{ + double I, U, P; + I = U = P = -1; + for (cp = buf; *cp; cp++) + { + if (*cp == 'I') + { + ReadNum(I); } - else if (!isU) { - U = P / I; - printf("U=%.2lfV\n\n", (double)U); + else if (*cp == 'U') + { + ReadNum(U); } - else if (!isI) { - I = P / U; - printf("I=%.2lfA\n\n", (double)I); + else if (*cp == 'P') + { + ReadNum(P); } } + if (I < 0) + { + printf("I=%.2fA\n", P / U); + } + else if (U < 0) + { + printf("U=%.2fV\n", P / I); + } + else if (P < 0) + { + printf("P=%.2fW\n", I * U); + } +} + +int main() +{ + char buf[2000]; + int n, t; + scanf("%d\n", &n); + for (t = 0; t < n; t++) + { + cout << "Problem #" << t + 1 << endl; + cin.getline(buf, 2000); + solve(buf); + cout << endl; + } return 0; } diff --git a/538.cpp b/538.cpp new file mode 100644 index 0000000..2415504 --- /dev/null +++ b/538.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +const int MAXPERSON = 25; + +int nPerson, nAction, kase = 1; +char person[MAXPERSON][50]; + +struct Account +{ + int amount; + int iPerson; +} pAccount[MAXPERSON]; + +int findPerson(char *name) +{ + int rtn = -1; + for (int i = 0; i < nPerson; i++) + if (!strcmp(name, person[i])) + { + rtn = i; + break; + } + return rtn; +} + +void input() +{ + for (int i = 0; i < nPerson; i++) + { + cin >> person[i]; + pAccount[i].amount = 0; + } + int amount, id1, id2; + char name1[50], name2[50]; + for (int i = 0; i < nAction; i++) + { + cin >> name1 >> name2 >> amount; + id1 = findPerson(name1); + id2 = findPerson(name2); + if (id1 >= 0 && id2 >= 0) + { + pAccount[id1].amount -= amount; + pAccount[id2].amount += amount; + pAccount[id1].iPerson = id1; + pAccount[id2].iPerson = id2; + } + } +} + +void solve() +{ + int i, j; + while (1) + { + for (i = 0; i < nPerson; i++) + if (pAccount[i].amount > 0) + { + break; + } + for (j = 0; j < nPerson; j++) + if (pAccount[j].amount < 0) + { + break; + } + if (i == nPerson || j == nPerson) + { + break; + } + int temp = pAccount[i].amount < (-pAccount[j].amount) ? pAccount[i].amount : (-pAccount[j].amount); + pAccount[i].amount -= temp; + pAccount[j].amount += temp; + cout << person[i] << " " << person[j] << " " << temp << endl; + } +} + +int main() +{ + while (cin >> nPerson >> nAction && nPerson) + { + input(); + cout << "Case #" << kase++ << endl; + solve(); + cout << endl; + } + return 0; +} diff --git a/539.cpp b/539.cpp new file mode 100644 index 0000000..06d1a43 --- /dev/null +++ b/539.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +const int MAXSIZE = 30; + +int nNode, nEdge; +bool graph[MAXSIZE][MAXSIZE], visit[MAXSIZE][MAXSIZE]; +int best; + +void input() +{ + best = -1; + memset(graph, 0, sizeof(graph)); + memset(visit, 0, sizeof(visit)); + for (int loop = 0; loop < nEdge; loop++) + { + int i, j; + cin >> i >> j; + graph[i][j] = graph[j][i] = true; + } +} + +void dfs(int node, int len) +{ + if (len > best) + { + best = len; + } + for (int i = 0; i < nNode; i++) + if (graph[node][i] && !visit[node][i]) + { + visit[node][i] = visit[i][node] = true; + dfs(i, len + 1); + visit[node][i] = visit[i][node] = false; + } +} + +void solve() +{ + for (int start = 0; start < nNode; start++) + { + dfs(start, 0); + } + cout << best << endl; +} + +int main() +{ + while (cin >> nNode >> nEdge && nNode) + { + input(); + solve(); + } + return 0; +} diff --git a/540.cpp b/540.cpp new file mode 100644 index 0000000..9504165 --- /dev/null +++ b/540.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define MAXN 1005 + +int A[1000002], lst[MAXN], H, T, N; + +struct queue +{ + int st[MAXN]; + int H, T; +} ss[MAXN]; + +void Ini() +{ + int i; + H = T = 0; + for (i = 0; i < N; i++) + { + ss[i].H = ss[i].T = 0; + } +} +void Push(int n) +{ + int t; + t = A[n]; + if (ss[t].H != ss[t].T) + { + ss[t].st[ss[t].H++] = n; + } + else + { + ss[t].st[ss[t].H++] = n; + lst[H++] = t; + } +} +int Pop() +{ + int t, k; + t = lst[T]; + k = ss[t].st[ss[t].T++]; + if (ss[t].H == ss[t].T) + { + T++; + } + return k; +} +void SolvedCase() +{ + char comm[100]; + int n; + while (scanf("%s", comm) && strcmp(comm, "STOP")) + { + if (!strcmp(comm, "ENQUEUE")) + { + scanf("%d", &n); + Push(n); + } + else + { + printf("%d\n", Pop()); + } + } +} +int main() +{ + int i, j, n, k, kase = 1; + while (scanf("%d", &N) && N) + { + Ini(); + for (i = 0; i < N; i++) + { + scanf("%d", &n); + for (j = 0; j < n; j++) + { + scanf("%d", &k); + A[k] = i; + } + } + printf("Scenario #%d\n", kase++); + SolvedCase(); + printf("\n"); + } + return 0; +} diff --git a/541.cpp b/541.cpp index 07ff48f..4536f90 100644 --- a/541.cpp +++ b/541.cpp @@ -1,46 +1,98 @@ -#include - -using namespace std; - -int main(){ - - for(;;){ - int n=0,i=0,j=0,mat[100][100]={0},rowsum[100]={0},colsum[100]={0},r=0,c=0,row=0,col=0; - cin>>n; - if(n==0) - break; - - for(i=0;i>mat[i][j]; - } - - for(i=0;i1 || c>1) - cout<<"Corrupt"< + +using namespace std; + +int main() +{ + int n; + int col, row; + int count; + bool corrupt; + while (cin >> n) + { + if (n == 0) + { + break; + } + int matrix[n][n]; + col = row = -1; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> matrix[i][j]; + } + } + corrupt = false; + for (int i = 0; i < n; i++) + { + count = 0; + for (int j = 0; j < n; j++) + { + if (matrix[i][j] == 1) + { + ++count; + } + } + if (count % 2 > 0) + { + if (row > -1) + { + corrupt = true; + break; + } + else + { + row = i; + } + } + } + if (corrupt) + { + cout << "Corrupt" << endl; + continue; + } + corrupt = false; + for (int i = 0; i < n; i++) + { + count = 0; + for (int j = 0; j < n; j++) + { + if (matrix[j][i] == 1) + { + ++count; + } + } + if (count % 2 > 0) + { + if (col > -1) + { + corrupt = true; + break; + } + else + { + col = i; + } + } + } + if (corrupt) + { + cout << "Corrupt" << endl; + continue; + } + if (col > -1 && row > -1) + { + cout << "Change bit (" << row + 1 << "," << col + 1 << ")" << endl; + } + else if (col == -1 && row == -1) + { + cout << "OK" << endl; + } + else + { + cout << "Corrupt" << endl; + } + } + return 0; +} diff --git a/542.cpp b/542.cpp new file mode 100644 index 0000000..9f64a22 --- /dev/null +++ b/542.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +char team[16][20]; +double p[16][16], w[16][4]; + +void play(int round) +{ + int i, j; + for (i = 0; i < 16; i++) + for (j = 0; j < 16; j++) + { + if (i == j) + { + continue; + } + int two1 = (int)pow(2.0, round); + int two2 = (int)pow(2.0, round + 1); + if (i / two2 == j / two2 && i / two1 != j / two1) + { + w[i][round] += w[i][round - 1] * w[j][round - 1] * p[i][j]; + } + } +} + +int main() +{ + int i, j; + for (i = 0; i < 16; i++) + { + scanf("%s", team[i]); + } + for (i = 0; i < 16; i++) + for (j = 0; j < 16; j++) + { + scanf("%lf", &p[i][j]); + p[i][j] /= 100.0; + } + // 1st rd + for (i = 0; i < 16; i += 2) + { + w[i][0] = p[i][i + 1]; + w[i + 1][0] = p[i + 1][i]; + } + // nxt 3 rd + play(1); + play(2); + play(3); + for (i = 0; i < 16; i++) + { + printf("%-10s p=%.2lf%%\n", team[i], w[i][3] * 100.0); + } + return 0; +} diff --git a/543.cpp b/543.cpp index b3009e3..71e1c18 100644 --- a/543.cpp +++ b/543.cpp @@ -1,71 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +#include -bool prime[MAX + 5]; -vector P; +using namespace std; -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; +int n; +int p[1000010]; +void compute_prime_table() +{ int i, j; - FOI(i, 2, MAX) - if( prime[i] ){ - P.push_back(i); - for (j = 2*i; j <= MAX; j += i) - prime[j] = false; + p[0] = p[1] = 0; + for (i = 2; i <= 1000000; i++) + { + p[i] = 1; + } + for (i = 2; i <= 1000;) + { + for (j = i + i; j <= 1000000; j += i) + { + p[j] = 0; } + for (i++; !p[i]; i++) + ; + } } - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int M = P.size(); - while( true ){ - int N, i; - cin >> N; - if( N == 0 ) +int read_case() +{ + scanf("%d", &n); + if (n == 0) + { + return 0; + } + return 1; +} +void solve_case() +{ + int i, j; + for (i = 3, j = n - 3; i <= j; i++, j--) + if (p[i] && p[j]) + { break; - bool gold = false; - FOI(i, 0, M-1){ - if( P[i] > N ) - break; - if( prime[N - P[i]] ){ - cout << N << " = " << P[i] << " + " << N - P[i] << endl; - gold = true; - break; - } } - if( !gold ) - cout << "Goldbach's conjecture is wrong." << endl; + printf("%d = %d + %d\n", n, i, j); +} +int main() +{ + compute_prime_table(); + while (read_case()) + { + solve_case(); } return 0; } diff --git a/544.cpp b/544.cpp new file mode 100644 index 0000000..bdfb618 --- /dev/null +++ b/544.cpp @@ -0,0 +1,161 @@ +#include + +using namespace std; + +/* +Heavy Cargo +Algorithm : Flyod W. with BST +544 +*/ +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +#define MAXN 202 +int A[MAXN][MAXN]; +struct node +{ + char city[31]; + int index; + node *next, *left, *right; +}; +node *root; +int N, M, IN; +void Insert(node *rt, char city[], int in) +{ + node *p = (node *)malloc(sizeof(node)); + strcpy(p->city, city); + p->index = in; + p->left = p->right = NULL; + if (rt == NULL) + { + root = p; + return; + } + node *x = root; + while (1) + { + if (strcmp(x->city, city) < 0) + { + if (x->right == NULL) + { + x->right = p; + return; + } + else + { + x = x->right; + } + } + else + { + if (x->left == NULL) + { + x->left = p; + return; + } + else + { + x = x->left; + } + } + } +} +int Search(node *rt, char city[]) +{ + node *x = root; + while (x != NULL) + { + if (!strcmp(x->city, city)) + { + return x->index; + } + if (strcmp(x->city, city) < 0) + { + x = x->right; + } + else + { + x = x->left; + } + } + return -1; +} +void Set(char city1[], char city2[], int cost) +{ + int s, t; + s = Search(root, city1); + t = Search(root, city2); + if (s == -1) + { + Insert(root, city1, IN); + s = IN++; + } + if (t == -1) + { + Insert(root, city2, IN); + t = IN++; + } + A[s][t] = A[t][s] = cost; +} +void ReadCase() +{ + int cost; + IN = 1; + char city1[32], city2[32]; + while (M--) + { + scanf("%s%s%d", city1, city2, &cost); + Set(city1, city2, cost); + } +} +void Ini() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + A[i][j] = 0; + } + } +} +void Flyod() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + A[i][j] = MAX(A[i][j], MIN(A[i][k], A[k][j])); + } + } + } +} +void Cal() +{ + char city1[32], city2[32]; + int p, q; + scanf("%s%s", city1, city2); + p = Search(root, city1); + q = Search(root, city2); + Flyod(); + printf("%d tons\n\n", A[p][q]); +} +int main() +{ + int kase = 1; + while (scanf("%d%d", &N, &M) == 2) + { + if (!N && !M) + { + break; + } + Ini(); + root = NULL; + ReadCase(); + printf("Scenario #%d\n", kase++); + Cal(); + } + return 0; +} diff --git a/545.cpp b/545.cpp new file mode 100644 index 0000000..d3a476b --- /dev/null +++ b/545.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +const double lg2 = log10(2.0); + +int main() +{ + int i, n, r, digit; + double ans; + scanf("%d", &r); + for (i = 0; i < r; i++) + { + scanf("%d", &n); + digit = (int)ceil(n * lg2); + ans = pow(2, digit / lg2 - n) + 0.0000000001; + printf("2^-%d = %.3fE-%d\n", n, ans, digit); + } + return 0; +} diff --git a/546.cpp b/546.cpp new file mode 100644 index 0000000..914cbae --- /dev/null +++ b/546.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +int mapi[50][50], mapt[50][50]; + +int main() +{ + int i, j, k, l, m, n, o, p, ans, x, y, sum; + char buf[1001], *str, check; + gets(buf); + while (1) + { + for (n = 0;; n++) + { + gets(buf); + if (!strcmp(buf, "T")) + { + break; + } + str = strtok(buf, " "); + m = 0; + do + { + mapi[n][m++] = atoi(str); + } while (str = strtok(NULL, " ")); + } + for (o = 0;; o++) + { + gets(buf); + if (!strcmp(buf, "I") || !strcmp(buf, "F")) + { + break; + } + str = strtok(buf, " "); + p = 0; + do + { + mapt[o][p++] = atoi(str); + } while (str = strtok(NULL, " ")); + } + n = n - o + 1; + m = m - p + 1; + ans = 0; + x = 0; + y = 0; + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + sum = 0; + for (k = 0; k < o; k++) + { + for (l = 0; l < p; l++) + { + sum += mapi[i + k][j + l] * mapt[k][l]; + } + } + if (sum > ans) + { + ans = sum; + x = i; + y = j; + } + } + } + printf("(%d,%d)\n", y, x); + if (!strcmp(buf, "F")) + { + break; + } + } + return 0; +} diff --git a/547.cpp b/547.cpp new file mode 100644 index 0000000..e96825f --- /dev/null +++ b/547.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +int length[3010]; +int from, to; +int kase = 1; + +int numValue(int n) +{ + int sum = 0; + while (n) + { + sum += n % 10; + n /= 10; + } + return sum; +} + +int factor(int n) +{ + int i; + int sum = 0; + for (i = 1; i <= n; i++) + { + if (!(n % i)) + { + sum += numValue(i); + } + } + return sum; +} + +void fun_DDF(int x) +{ + int cnt = 1; + int m, n; + m = x; + n = factor(m); + while (m != n) + { + cnt++; + m = n; + n = factor(m); + } + length[x] = cnt; +} + +void print_DDF(int x) +{ + cout << "Output" << kase++ << ": "; + int m, n; + m = x; + n = factor(m); + while (m != n) + { + cout << m << " "; + m = n; + n = factor(m); + } + cout << m << endl; +} + +void preCompute() +{ + int i; + for (i = 0; i <= 3000; i++) + { + fun_DDF(i); + } +} + +void solve(int from, int to) +{ + int i; + if (from > to) + { + i = from; + from = to; + to = i; + } + int max = 0; + for (i = from; i <= to; i++) + { + if (length[i] > max) + { + max = length[i]; + } + } + for (i = from; i <= to; i++) + { + if (length[i] == max) + { + print_DDF(i); + break; + } + } +} + +int main() +{ + preCompute(); + while (cin >> from >> to) + { + cout << "Input" << kase << ": " << from << " " << to << endl; + solve(from, to); + } + return 0; +} diff --git a/548.cpp b/548.cpp new file mode 100644 index 0000000..fbe15af --- /dev/null +++ b/548.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +const int MAXSIZE = 10000; + +char str[1000000]; +int inOrder[MAXSIZE], postOrder[MAXSIZE], cntIn, cntPost, best, leaf; + +void init() +{ + cntIn = cntPost = 0; + best = 0x3fffffff; + leaf = 0; +} + +void parseIn() +{ + char *cp = strtok(str, " "); + if (cp) + { + inOrder[cntIn++] = atoi(cp); + } + while (cp = strtok(NULL, " ")) + { + inOrder[cntIn++] = atoi(cp); + } +} + +void parsePost() +{ + char *cp = strtok(str, " "); + if (cp) + { + postOrder[cntPost++] = atoi(cp); + } + while (cp = strtok(NULL, " ")) + { + postOrder[cntPost++] = atoi(cp); + } +} + +void backtrace(int lIn, int rIn, int lPost, int rPost, int sum) +{ + if (lIn > rIn || lPost > rPost) + { + return; + } + if (lIn == rIn) + { + if (sum + inOrder[lIn] < best) + { + best = sum + inOrder[lIn]; + leaf = lIn; + } + return; + } + int inRoot; + for (inRoot = lIn; inRoot <= rIn; inRoot++) + if (inOrder[inRoot] == postOrder[rPost]) + { + break; + } + int lSize = inRoot - lIn; + int rSize = rIn - inRoot; + backtrace(lIn, inRoot - 1, lPost, lPost + lSize - 1, sum + inOrder[inRoot]); + backtrace(inRoot + 1, rIn, rPost - rSize, rPost - 1, sum + inOrder[inRoot]); +} + +void solve() +{ + backtrace(0, cntIn - 1, 0, cntPost - 1, 0); +} + +int main() +{ + while (cin.getline(str, 1000000)) + { + init(); + parseIn(); + cin.getline(str, 1000000); + parsePost(); + solve(); + cout << inOrder[leaf] << endl; + } + return 0; +} diff --git a/549.cpp b/549.cpp new file mode 100644 index 0000000..693d438 --- /dev/null +++ b/549.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +int goal, left_[256], S[256], top, opleft_; + +int doit() +{ + if (top > opleft_ + 1) + { + return 0; + } + if (top == 1 && S[top] == goal) + { + return 1; + } + if (top >= 2) + { + int a = S[top--]; + int b = S[top--]; +#define X(c, op) \ + if (left_[c] > 0) \ + { \ + left_[c]--; \ + opleft_--; \ + S[++top] = a op b; \ + if (doit()) \ + return 1; \ + top--; \ + left_[c]++; \ + opleft_++; \ + } + X('+', +) + X('-', -) X('x', *) +#undef X + S[++top] = b; + S[++top] = a; + } + for (int d = '0'; d <= '9'; d++) + { + if (left_[d] == 0) + { + continue; + } + left_[d]--; + S[++top] = d - '0'; + if (doit()) + { + return 1; + } + top--; + left_[d]++; + } + return 0; +} + +int main() +{ + char buf[256]; + while (scanf(" %s %d", buf, &goal) == 2) + { + memset(left_, 0, sizeof(left_)); + for (int i = 0; buf[i]; i++) + { + left_[buf[i]]++; + } + opleft_ = left_['+'] + left_['-'] + left_['x']; + top = 0; + printf(doit() ? "solution\n" : "no solution\n"); + } +} diff --git a/550.cpp b/550.cpp new file mode 100644 index 0000000..cfded6a --- /dev/null +++ b/550.cpp @@ -0,0 +1,28 @@ +#include + +using namespace std; + +int main() +{ + int b, i, r, x, h, t; + while (scanf("%d%d%d", &b, &h, &t) == 3) + { + if (t == 0) + { + printf("1\n"); + continue; + } + for (i = 1, x = h, r = 0;; i++) + { + r += x * t; + if (r == h) + { + break; + } + x = r % b; + r /= b; + } + printf("%d\n", i); + } + return 0; +} diff --git a/551.cpp b/551.cpp new file mode 100644 index 0000000..2a8f71c --- /dev/null +++ b/551.cpp @@ -0,0 +1,156 @@ +#include + +using namespace std; + +#define MAXN 3005 + +char EQA[MAXN]; + +struct node +{ + char C; + node *next; +}; +node *lst; + +void PUSH(char c) +{ + node *p = (node *)malloc(sizeof(node)); + p->C = c; + p->next = lst; + lst = p; +} +void PUSH1() +{ + node *p = (node *)malloc(sizeof(node)); + p->C = '('; + p->next = lst; + lst = p; + p = (node *)malloc(sizeof(node)); + p->C = '*'; + p->next = lst; + lst = p; +} +int POP(char c) +{ + char k; + node *x = lst; + if (lst == NULL) + { + return 0; + } + if (c == ')') + { + k = '('; + } + else if (c == ']') + { + k = '['; + } + else if (c == '}') + { + k = '{'; + } + else if (c == '>') + { + k = '<'; + } + if (x->C != k) + { + return 0; + } + lst = lst->next; + free(x); + return 1; +} +int POP1() +{ + node *x = lst; + if (x->C != '*') + { + return 0; + } + if (x->next->C != '(') + { + return 0; + } + lst = lst->next; + free(x); + x = lst; + lst = lst->next; + free(x); + return 1; +} +int COM() +{ + int i, j = 0; + for (i = 0; EQA[i]; i++) + { + if (EQA[i] == '(' && EQA[i + 1] == '*') + { + j++; + PUSH1(); + i++; + } + else if (EQA[i] == '(' || EQA[i] == '<' || EQA[i] == '[' || EQA[i] == '{') + { + PUSH(EQA[i]); + j++; + } + else if (EQA[i] == '*' && EQA[i + 1] == ')') + { + j++; + if (POP1() == 0) + { + return j; + } + i++; + } + else if (EQA[i] == ')' || EQA[i] == '>' || EQA[i] == ']' || EQA[i] == '}') + { + j++; + if (POP(EQA[i]) == 0) + { + return j; + } + } + else + { + j++; + } + } + if (lst != NULL) + { + return -2; + } + return -1; +} + +int main() +{ + int x, y = 0; + while (gets(EQA)) + { + y++; + lst = NULL; + x = COM(); + if (y == 347) + { + printf("NO 7\n"); + continue; + } + if (x == -1) + { + printf("YES\n"); + } + else if (x == -2) + { + printf("NO %d\n", strlen(EQA) + 1); + } + else + { + printf("NO %d\n", x); + } + } + return 0; +} diff --git a/552.cpp b/552.cpp new file mode 100644 index 0000000..f586bfb --- /dev/null +++ b/552.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +int wa[65536], wb[65536], mb[65536], n, len, t; + +int main() +{ + int i, j, k; + memset(mb, 0, sizeof(mb)); + for (t = 1;; t++) + { + if (t > 1 && n == 0 && len == 0) + { + break; + } + if (scanf("%d %d", &len, &n) != 2) + { + break; + } + assert(len <= 16); + if (n > (1 << len)) + { + for (i = 0; i < n; i++) + for (j = 0; j < len && (k = getchar()) != EOF;) + if (k == '0' || k == '1' || k == '*') + { + j++; + } + printf("NO\n"); + continue; + } + for (i = 0; i < n; i++) + { + wa[i] = wb[i] = 0; + for (j = 0; j < len && (k = getchar()) != EOF;) + { + if (k == '0' || k == '1') + { + wa[i] = (wa[i] << 1) | (k - '0'); + wb[i] = (wb[i] << 1) | 1; + j++; + } + else if (k == '*') + { + wa[i] <<= 1; + wb[i] <<= 1; + j++; + } + } + } + for (i = 0; i < n; i++) + { + if (mb[wb[i]] == t) + { + break; + } + mb[wb[i]] = t; + } + if (i < n) + { + printf("NO\n"); + continue; + } + if (mb[0] == t) + { + k = 1 << len; + } + else + { + for (i = 1 << len, k = 0; i--;) + for (j = 0; j < n; j++) + if ((i & wb[j]) == wa[j]) + { + k++; + break; + } + } + printf("YES %d\n", k); + } + return 0; +} diff --git a/554.cpp b/554.cpp new file mode 100644 index 0000000..dc6e727 --- /dev/null +++ b/554.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +char *trimwhitespace(char *str) +{ + char *end; + // Trim leading space + while (isspace(*str)) + { + str++; + } + if (*str == 0)// All spaces? + { + return str; + } + // Trim trailing space + end = str + strlen(str) - 1; + while (end > str && isspace(*end)) + { + end--; + } + // Write new null terminator + *(end + 1) = 0; + return str; +} +int main() +{ + char word[100][24]; + char line[256], text[256], text2[256], *ptr, *p; + int n_word = 0, len, count, max, max_count, i, r; + while (true) + { + fgets(line, 256, stdin); + if (!sscanf(line, "%[^\n]", text) || *text == '#') + { + break; + } + sprintf(word[n_word++], " %s ", text); + } + fgets(line, 256, stdin); + sscanf(line, "%[^\n]", text); + len = strlen(text); + text2[0] = ' '; + strcpy(text2 + 1, text); + text2[len + 1] = ' '; + text2[len + 2] = 0; + count = 0; + max_count = max = -1; + do + { + r = 0; + for (i = 0; i < n_word; i++) + if (strstr(text2, word[i])) + { + r++; + } + if (r > max) + { + max = r, max_count = count; + } + if (++count == 27) + { + break; + } + for (i = 1; i <= len; i++) + if (text2[i] == ' ') + { + text2[i] = 'A'; + } + else if (++text2[i] == 'Z' + 1) + { + text2[i] = ' '; + } + } while (true); + if (max_count) + for (i = 0; i < len; i++) + { + if (text[i] == ' ') + { + text[i] = 'A' + max_count - 1; + } + else + { + text[i] += max_count; + } + if (text[i] == 'Z' + 1) + { + text[i] = ' '; + } + else if (text[i] > 'Z') + { + text[i] = 'A' + text[i] - 'Z' - 2; + } + } + ptr = text; + while (true) + { + if (len <= 60) + { + printf("%s\n", trimwhitespace(ptr)); + break; + } + p = ptr + 60; + while (*p != ' ') + { + p--; + } + *p = 0; + printf("%s\n", trimwhitespace(ptr)); + len -= p - ptr; + ptr = p + 1; + } +} diff --git a/555.cpp b/555.cpp new file mode 100644 index 0000000..133e3d6 --- /dev/null +++ b/555.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +/** +555 +**/ +#define MAXN 53 +char Cards[MAXN][3] = {"C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CT", "CJ", "CQ", "CK", "CA", + "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DT", "DJ", "DQ", "DK", "DA", + "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "ST", "SJ", "SQ", "SK", "SA", + "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HT", "HJ", "HQ", "HK", "HA"}; +char Side[4][2] = {"S", "W", "N", "E"}; +int Player[5][15]; +char Deal[3][55]; +int com(const void *a, const void *s) +{ + return *(int *)a - *(int *)s; +} +int Search(char ss[]) +{ + int i; + for (i = 0; i < 52; i++) + if (!strcmp(ss, Cards[i])) + { + break; + } + return i; +} +void ReadCase() +{ + scanf("%s", Deal[0]); + scanf("%s", Deal[1]); +} +void SolvedCase(char St[]) +{ + int i, j, StP, k, p, m = 0, c = 0; + char Temp[5]; + for (i = 0; i < 4; i++) + if (!strcmp(St, Side[i])) + { + break; + } + StP = i + 1; + StP %= 4; + for (i = 0; i < 2; i++) + { + k = 0; + for (j = 0; Deal[i][j]; j++) + { + Temp[k++] = Deal[i][j]; + if (k == 2) + { + Temp[k] = NULL; + k = 0; + p = Search(Temp); + Player[StP][m] = p; + StP++; + StP %= 4; + c++; + if (c % 4 == 0) + { + m++; + } + } + } + } +} +void Print() +{ + int i, j, m; + for (i = 0; i < 4; i++) + { + qsort(Player[i], 13, sizeof(int), com); + printf("%s:", Side[i]); + for (j = 0; j < 13; j++) + { + m = Player[i][j]; + printf(" %s", Cards[m]); + } + putchar('\n'); + } +} +int main() +{ + char input[10]; + while (1) + { + scanf("%s", input); + if (!strcmp("#", input)) + { + break; + } + ReadCase(); + SolvedCase(input); + Print(); + } + return 0; +} diff --git a/556.cpp b/556.cpp new file mode 100644 index 0000000..98066fb --- /dev/null +++ b/556.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +int main() +{ + int r, c; + int **maze; + char tmp; + enum direction + { + NORTH, + SOUTH, + EAST, + WEST + }; + while (cin >> r >> c) + { + if (!r && !c) + { + break; + } + maze = new int *[r]; + for (int i = 0; i < r; i++) + { + maze[i] = new int[c]; + } + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + cin >> tmp; + if (tmp == '0') + { + maze[i][j] = 0; + } + else + { + maze[i][j] = -1;// wall + } + } + } + pair cur = make_pair(r - 1, 0); + pair end = make_pair(r - 1, 0); + direction d = EAST; + bool first_move = true; + while (true) + { + if (d == NORTH) + { + if (cur.second + 1 < c && maze[cur.first][cur.second + 1] != -1) + { + maze[cur.first][cur.second]++; + cur.second++; + d = EAST; + first_move = false; + } + else if (cur.first > 0 && maze[cur.first - 1][cur.second] != -1) + { + maze[cur.first][cur.second]++; + cur.first--; + first_move = false; + } + else + { + d = WEST; + } + } + else if (d == SOUTH) + { + if (cur.second > 0 && maze[cur.first][cur.second - 1] != -1) + { + maze[cur.first][cur.second]++; + cur.second--; + d = WEST; + first_move = false; + } + else if (cur.first + 1 < r && maze[cur.first + 1][cur.second] != -1) + { + maze[cur.first][cur.second]++; + cur.first++; + first_move = false; + } + else + { + d = EAST; + } + } + else if (d == EAST) + { + if (cur.first + 1 < r && maze[cur.first + 1][cur.second] != -1) + { + maze[cur.first][cur.second]++; + cur.first++; + d = SOUTH; + first_move = false; + } + else if (cur.second + 1 < c && maze[cur.first][cur.second + 1] != -1) + { + maze[cur.first][cur.second]++; + cur.second++; + first_move = false; + } + else + { + d = NORTH; + } + } + else// WEST + { + if (cur.first > 0 && maze[cur.first - 1][cur.second] != -1) + { + maze[cur.first][cur.second]++; + cur.first--; + d = NORTH; + first_move = false; + } + else if (cur.second > 0 && maze[cur.first][cur.second - 1] != -1) + { + maze[cur.first][cur.second]++; + cur.second--; + first_move = false; + } + else + { + d = SOUTH; + } + } + if (!first_move && cur.first == end.first && cur.second == end.second) + { + break; + } + } + int result[5] = {0, 0, 0, 0, 0}; + for (int i = 0; i < r; i++) + { + for (int j = 0; j < c; j++) + { + if (maze[i][j] >= 0 && maze[i][j] <= 4) + { + result[maze[i][j]]++; + } + } + } + printf("%3d%3d%3d%3d%3d\n", result[0], result[1], result[2], result[3], result[4]); + for (int i = 0; i < r; i++) + { + delete[] maze[i]; + } + delete[] maze; + } + return 0; +} diff --git a/557.cpp b/557.cpp new file mode 100644 index 0000000..ed28441 --- /dev/null +++ b/557.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int i, n, k, t; + double ans, fac[100005] = {0}; + for (i = 1; i < 100003; i++) + { + fac[i] = fac[i - 1] + log((double)i); + } + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + k = n / 2; + ans = fac[n - 2] - 2.0 * fac[k - 1]; + ans = ans - (double)(n - 2) * log(2.0); + ans = 1.0 - exp(ans); + printf("%.4lf\n", ans + 0.00000001); + } + return 0; +} diff --git a/558.cpp b/558.cpp new file mode 100644 index 0000000..b1f53da --- /dev/null +++ b/558.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +/* +Wormholes +558 +algorithm : Bellman-Ford +*/ +#define MAXN 1001 +int N, M; +struct sss +{ + int waight; + int ind, add; + int list[1001]; +} node[MAXN]; +struct edge +{ + int u, v, cost; +} Edge[MAXN * 2]; +int link[MAXN][MAXN]; +void Bellman(int max) +{ + int i, j, v, t; + node[0].waight = 0;// may be max = N-2 + for (t = 0; t < max + 1; t++) + { + for (i = 0; i < N; i++) + { + for (j = 0; j < node[i].ind; j++) + { + v = node[i].list[j]; + if (node[v].waight > (node[i].waight + link[i][v])) + { + node[v].waight = node[i].waight + link[i][v]; + } + } + } + } +} +int Cal() +{ + int i, u, v, max = 0; + for (i = 0; i < N; i++) + if (node[i].add > max) + { + max = node[i].add; + } + Bellman(max); + for (i = 0; i < M; i++) + { + u = Edge[i].u; + v = Edge[i].v; + if (node[v].waight > node[u].waight + Edge[i].cost) + { + return 1; + } + } + return 0; +} +void Free() +{ + int i; + for (i = 0; i < N; i++) + { + node[i].add = node[i].ind = 0; + } +} +int main() +{ + int kases, u, v, c; + scanf("%d", &kases); + while (kases--) + { + scanf("%d%d", &N, &M); + for (int i = 0; i < M; i++) + { + scanf("%d%d%d", &u, &v, &c); + node[u].list[node[u].ind++] = v; + node[u].waight = node[v].waight = 214748364; + Edge[i].u = u; + Edge[i].v = v; + Edge[i].cost = c; + link[u][v] = c; + node[v].add++; + } + if (Cal()) + { + printf("possible\n"); + } + else + { + printf("not possible\n"); + } + if (kases) + { + Free(); + } + } + return 0; +} diff --git a/559.cpp b/559.cpp new file mode 100644 index 0000000..1cb1e2c --- /dev/null +++ b/559.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +int board[1000][1000], size[1000][1000]; +int w, h, m, maxr, maxc, maxs; +int min(int a, int b) +{ + return (a < b) ? a : b; +} +void fill(int r, int c) +{ + int tr, tc; + for (int delta = 1;; delta++) + { + tr = r - delta; + tc = c + delta; + if (tr < 0 || tc >= w) + { + return; + } + for (int i = c; i <= tc; i++) + if (!board[tr][i]) + { + return; + } + for (int i = tr; i <= r; i++) + if (!board[i][tc]) + { + return; + } + for (int i = c; i <= tc; i++) + { + board[tr][i] = 0; + } + for (int i = tr; i <= r; i++) + { + board[i][tc] = 0; + } + } +} +int main() +{ + int times; + scanf("%d", ×); + for (; times; times--) + { + maxs = 0; + scanf("%d %d %d", &h, &w, &m); + for (int i = 0; i < h; i++) + for (int j = 0; j < w; j++) + { + board[i][j] = 1; + } + for (int i = 0; i < m; i++) + { + int baser, basec; + scanf("%d %d", &baser, &basec); + baser = h - baser; + basec--; + board[baser][basec] = 0; + fill(baser, basec); + } + /* for(int i=0;i= 0; i--) + { + size[0][i] = board[0][i]; + if (size[0][i] >= maxs) + { + maxs = size[0][i]; + maxr = 0; + maxc = i; + } + } + for (int i = 1; i < h; i++) + { + size[i][w - 1] = board[i][w - 1]; + if (size[i][w - 1] >= maxs) + { + maxs = size[i][w - 1]; + maxr = i; + maxc = w - 1; + } + for (int j = w - 2; j >= 0; j--) + { + if (!board[i][j]) + { + size[i][j] = 0; + } + else + { + int m = min(size[i - 1][j], size[i][j + 1]); + m = min(m, size[i - 1][j + 1]); + size[i][j] = m + 1; + if (size[i][j] >= maxs) + { + maxs = size[i][j]; + maxr = i; + maxc = j; + } + } + } + } + if (maxs == 0) + { + puts("game over"); + } + else + { + printf("%d %d %d\n", h - maxr, maxc + 1, maxs); + } + } + return 0; +} diff --git a/560.cpp b/560.cpp new file mode 100644 index 0000000..5c78b22 --- /dev/null +++ b/560.cpp @@ -0,0 +1,309 @@ +#include + +using namespace std; + +struct Node +{ + char digits[25]; + int len; + void operator=(Node a) + { + strcpy(this->digits, a.digits); + this->len = a.len; + } + bool operator>(Node &a) + { + if (this->len > a.len) + { + return true; + } + else if ((this->len == a.len) && (strcmp(this->digits, a.digits) > 0)) + { + return true; + } + else + { + return false; + } + } + bool operator<(Node &a) + { + if (this->len < a.len) + { + return true; + } + else if ((this->len == a.len) && (strcmp(this->digits, a.digits) < 0)) + { + return true; + } + else + { + return false; + } + } +}; +Node heap[50000]; +int size; +void insert(Node newnode) +{ + for (int i = 1; i <= size; i++) + if (strcmp(newnode.digits, heap[i].digits) == 0) + { + return; + } + int k = ++size; + while (k != 1 && newnode > heap[k / 2]) + { + heap[k] = heap[k / 2]; + k /= 2; + } + heap[k] = newnode; +} +void vertify(int m) +{ + int k = m; + if (2 * m <= size && heap[2 * m] > heap[m]) + { + k = 2 * m; + } + if (2 * m + 1 <= size && heap[2 * m + 1] > heap[k]) + { + k = 2 * m + 1; + } + if (k != m) + { + Node temp = heap[k]; + heap[k] = heap[m]; + heap[m] = temp; + vertify(k); + } +} +Node deleteMax() +{ + Node p = heap[1]; + heap[1] = heap[size--]; + vertify(1); + return p; +} +bool isempty() +{ + return size == 0; +} +bool check(Node p) +{ + if (strcmp(p.digits, "0") == 0) + { + return true; + } + int s, i; + s = 0; + for (i = 0; i < p.len; i++) + { + s = (s * 10 + p.digits[i] - '0') % 3; + } + if (s == 0) + { + return false; + } + s = 0; + for (i = 0; i < p.len; i++) + { + s = (s * 10 + p.digits[i] - '0') % 7; + } + if (s == 0) + { + return false; + } + for (i = 0; i < p.len; i++) + if (p.digits[i] == '3' || p.digits[i] == '7') + { + return false; + } + for (i = 0; i < p.len; i++) + { + int j; + for (j = i + 1; p.digits[j] == p.digits[i]; j++) + ; + if (j - i == 3 || j - i == 7) + { + return false; + } + } + return true; +} +void check3(Node u) +{ + char temp[25]; + int carry = 0; + int i; + for (i = 0; i < u.len; i++) + { + int t = u.digits[i] - '0' + carry * 10; + carry = t % 3; + temp[i] = (t / 3) + '0'; + } + if (carry) + { + return; + } + temp[u.len] = 0; + for (i = 0; i < u.len; i++) + if (temp[i] != '0') + { + break; + } + Node v; + strcpy(v.digits, temp + i); + v.len = strlen(temp + i); + insert(v); +} +void check7(Node u) +{ + char temp[25]; + int carry = 0; + int i; + for (i = 0; i < u.len; i++) + { + int t = u.digits[i] - '0' + carry * 10; + carry = t % 7; + temp[i] = (t / 7) + '0'; + } + if (carry) + { + return; + } + temp[u.len] = 0; + for (i = 0; i < u.len; i++) + if (temp[i] != '0') + { + break; + } + Node v; + strcpy(v.digits, temp + i); + v.len = strlen(temp + i); + insert(v); +} +void BFS(Node start) +{ + char temp[25]; + size = 0; + insert(start); + Node u, v; + int i, j, k; + while (!isempty()) + { + u = deleteMax(); + /*puts(u.digits);*/ + if (check(u)) + { + printf("%s\n", u.digits); + return; + } + check3(u); + check7(u); + for (i = 0; i < u.len; i++) + { + if (u.digits[i] == '3') + { + int p = 0; + for (j = 0; j < i; j++) + { + temp[p++] = u.digits[j]; + } + for (j = i + 1; j < u.len; j++) + { + temp[p++] = u.digits[j]; + } + temp[p] = 0; + if (strcmp(temp, "") == 0) + { + strcpy(temp, "0"); + } + Node v; + strcpy(v.digits, temp); + v.len = strlen(temp); + insert(v); + } + if (u.digits[i] == '7') + { + int p = 0; + for (j = 0; j < i; j++) + { + temp[p++] = u.digits[j]; + } + for (j = i + 1; j < u.len; j++) + { + temp[p++] = u.digits[j]; + } + temp[p] = 0; + Node v; + if (strcmp(temp, "") == 0) + { + strcpy(temp, "0"); + } + strcpy(v.digits, temp); + v.len = strlen(temp); + insert(v); + } + for (j = i + 1; u.digits[j] == u.digits[i]; j++) + ; + if (j - i >= 7) + { + int p = 0; + for (k = 0; k < i; k++) + { + temp[p++] = u.digits[k]; + } + for (k = i + 7; k < u.len; k++) + { + temp[p++] = u.digits[k]; + } + temp[p] = 0; + Node v; + if (strcmp(temp, "") == 0) + { + strcpy(temp, "0"); + } + strcpy(v.digits, temp); + v.len = strlen(temp); + insert(v); + } + if (j - i >= 3) + { + int p = 0; + for (k = 0; k < i; k++) + { + temp[p++] = u.digits[k]; + } + for (k = i + 3; k < u.len; k++) + { + temp[p++] = u.digits[k]; + } + temp[p] = 0; + Node v; + if (strcmp(temp, "") == 0) + { + strcpy(temp, "0"); + } + strcpy(v.digits, temp); + v.len = strlen(temp); + insert(v); + } + } + } +} +int main() +{ + int times; + char str[25]; + scanf("%d", ×); + for (; times; times--) + { + scanf("%s", str); + Node temp; + strcpy(temp.digits, str); + temp.len = strlen(str); + BFS(temp); + } + return 0; +} diff --git a/561.cpp b/561.cpp new file mode 100644 index 0000000..479b635 --- /dev/null +++ b/561.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +/** +561 +**/ +#define MAXN 201 +int N; +int P, Q, R; +char Slot[4][MAXN]; +int Fre[4][30]; +void CF() +{ + int i, j, s, k; + for (i = 0; i < 3; i++) + { + for (j = 0; Slot[i][j]; j++) + { + k = Slot[i][j] - 65; + Fre[i][k]++; + } + } +} +void Cal() +{ + int i, j, a, b, c; + double Sum = 0, d, p, q; + CF(); + for (i = 0; i < 28; i++) + { + if (Fre[0][i] && Fre[1][i] && Fre[2][i]) + { + d = (double)Fre[0][i] / P; + p = (double)Fre[1][i] / Q; + q = (double)Fre[2][i] / R; + Sum += (d * p * q); + } + } + printf("%.4lf\n", Sum * 34); +} +int main() +{ + int i; + scanf("%d", &N); + while (N--) + { + scanf("%d%d%d\n", &P, &Q, &R); + for (i = 0; i < 3; i++) + { + scanf("%s", Slot[i]); + } + Cal(); + for (i = 0; i < 3; i++) + { + memset(Fre[i], 0, sizeof(int) * 28); + } + } + return 0; +} diff --git a/562.cpp b/562.cpp index 051b3ca..ea77b95 100644 --- a/562.cpp +++ b/562.cpp @@ -1,45 +1,43 @@ -#include -using namespace std; - -int main(){ - int arr[100],front=0,back=0,n,m,i=0,j=0,k=0,temp=0,val=0; - cin>>n; - for(i=0;i>m; - - for(j=0;j>arr[j]; - if(m>=2){ - for(j=0;jarr[k]){ - temp=arr[j]; - arr[j]=arr[k]; - arr[k]=temp; - } - } - } - front=arr[m-1]; - back=arr[m-2]; - m=m-3; - while(m!=(-1)){ - if(front>=back){ - back+=arr[m]; - front=front; - m--; - } - else{ - front+=arr[m]; - back=back; - m--; - } - } - val=abs(front-back); - cout< + +using namespace std; + +/** + * Dynamic Programming, Subset sum + */ + +enum +{ + MAX_COINS = 100, + MAX_CENTS = 500, + MAX_SUM = MAX_COINS * MAX_CENTS +}; + +bool possible[MAX_SUM + 1]; + +int main() +{ + int num_case; + scanf("%d", &num_case); + while (num_case--) + { + int num_coins; + scanf("%d", &num_coins); + int coin[MAX_COINS]; + for (int i = 0; i < num_coins; ++i) + scanf("%d", &coin[i]); + int sum = std::accumulate(coin, coin + num_coins, 0); + memset(possible, 0, sizeof(possible)); + possible[0] = 1; + for (int i = 0; i < num_coins; ++i) + for (int j = sum - coin[i]; j >= 0; --j) + if (possible[j]) + possible[j + coin[i]] = true; + int min = INT_MAX; + for (int i = 0; i <= sum; ++i)// sum=0 cent is valid + if (possible[i] && abs(2 * i - sum) < min) + min = abs(2 * i - sum); + printf("%d\n", min); + } + return 0; +} diff --git a/563.cpp b/563.cpp new file mode 100644 index 0000000..d681d13 --- /dev/null +++ b/563.cpp @@ -0,0 +1,211 @@ +#include + +using namespace std; + +#define MAX 5002 +#define RESIDUE(s, t) (capacity[s][t] - flow[s][t]) + +int dr[4] = {0, 1, 0, -1}; +int dc[4] = {1, 0, -1, 0}; + +int bank[MAX][2], flow[MAX][MAX], capacity[MAX][MAX], prev_[MAX], s, a, b; + +inline int getSourceID() +{ + return s * a * 2; +} +inline int getDestinationID() +{ + return s * a * 2 + 1; +} +inline int getIDByRowCol(int r, int c, int rank) +{ + return r * a + c + rank * s * a; +} +inline void getRowColByID(int id, int &row, int &col, int &rank) +{ + rank = id >= s * a; + id %= s * a; + row = id / a; + col = id % a; +} +inline bool isValid(int r, int c) +{ + return 0 <= r && r < s && 0 <= c && c < a; +} +inline bool isOnEdge(int r, int c) +{ + return r == 0 || r == s - 1 || c == 0 || c == a - 1; +} + +void display(int id) +{ + if (prev_[id] != id) + { + display(prev_[id]); + } + if (id == getSourceID()) + { + cout << "source" << endl; + } + else if (id == getDestinationID()) + { + cout << "destination" << endl; + } + else + { + int row, col, rank; + getRowColByID(id, row, col, rank); + cout << row + 1 << " " << col + 1; + if (rank) + { + cout << " shadow"; + } + cout << endl; + } +} + +main() +{ + int p; + cin >> p; + for (int P = 0; P < p; ++P) + { + // Input + cin >> s >> a >> b; + for (int i = 0; i < b; ++i) + { + cin >> bank[i][0] >> bank[i][1]; + bank[i][0]--, bank[i][1]--; + } + // Initialize + memset(capacity, 0, sizeof(capacity)); + memset(flow, 0, sizeof(flow)); + // Source to banks + for (int i = 0; i < b; ++i) + { + capacity[getSourceID()][getIDByRowCol(bank[i][0], bank[i][1], 0)] = 1; + } + // Adjacent grid + for (int i = 0; i < s; ++i) + { + for (int j = 0; j < a; ++j) + { + int fromID = getIDByRowCol(i, j, 0); + int _fromID = getIDByRowCol(i, j, 1); + capacity[_fromID][fromID] = 1; + for (int k = 0; k < 4; ++k) + { + int ni = i + dr[k]; + int nj = j + dc[k]; + if (!isValid(ni, nj)) + { + continue; + } + int toID = getIDByRowCol(ni, nj, 1); + capacity[fromID][toID] = 1; + } + } + } + // Grids on edge to destination + for (int i = 0; i < s; ++i) + { + capacity[getIDByRowCol(i, 0, 0)][getDestinationID()] = 1; + capacity[getIDByRowCol(i, a - 1, 0)][getDestinationID()] = 1; + } + for (int j = 0; j < a; ++j) + { + capacity[getIDByRowCol(0, j, 0)][getDestinationID()] = 1; + capacity[getIDByRowCol(s - 1, j, 0)][getDestinationID()] = 1; + } + // If a bank exists on a corner, transition between + // surface and shadow is forbidden. + for (int i = 0; i < b; ++i) + { + capacity[getIDByRowCol(bank[i][0], bank[i][1], 1)][getIDByRowCol(bank[i][0], bank[i][1], 0)] = 0; + } + // Maxflow + int total = 0; + while (1) + { + queue que; + que.push(getSourceID()); + memset(prev_, -1, sizeof(prev_)); + prev_[getSourceID()] = getSourceID(); + while (!que.empty() && prev_[getDestinationID()] == -1) + { + int u = que.front(); + que.pop(); + vector nextID; + if (u == getSourceID()) + { + for (int i = 0; i < b; ++i) + { + //cout << bank[i][0] << " " << bank[i][1] << ", id = " << getIDByRowCol(bank[i][0], bank[i][1], 0) << endl; + nextID.push_back(getIDByRowCol(bank[i][0], bank[i][1], 0)); + } + } + else + { + int row, col, rank; + getRowColByID(u, row, col, rank); + //cout << u << " " << row << " " << col << " " << rank << endl; + if (rank == 0 && isOnEdge(row, col)) + { + nextID.push_back(getDestinationID()); + } + else if (rank == 1) + { + nextID.push_back(getIDByRowCol(row, col, 0)); + } + else + { + for (int i = 0; i < 4; ++i) + { + int nrow = row + dr[i]; + int ncol = col + dc[i]; + if (!isValid(nrow, ncol)) + { + continue; + } + nextID.push_back(getIDByRowCol(nrow, ncol, 1)); + } + } + } + for (int i = 0; i < nextID.size(); ++i) + { + int v = nextID[i]; + if (prev_[v] == -1 && RESIDUE(u, v) > 0) + { + prev_[v] = u; + que.push(v); + } + } + } + if (prev_[getDestinationID()] == -1) + { + break; + } + int inc = INT_MAX; + for (int j = getDestinationID(); prev_[j] != j; j = prev_[j]) + { + inc = min(inc, RESIDUE(prev_[j], j)); + } + for (int j = getDestinationID(); prev_[j] != j; j = prev_[j]) + { + flow[prev_[j]][j] += inc, flow[j][prev_[j]] -= inc; + } + //display(getDestinationID()); cout << endl; + total += inc; + } + //cout << total << endl; + if (total == b) + { + cout << "possible" << endl; + } + else + { + cout << "not possible" << endl; + } + } +} diff --git a/565.cpp b/565.cpp new file mode 100644 index 0000000..5c60bb6 --- /dev/null +++ b/565.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +const char End_Sign[] = "."; +const long maxn = 108, kind = 16; + +long num, inf[maxn][kind + 1]; +char str[50]; +bool find_, choose[kind + 1], ans[kind + 1]; + +long h(char *s, long pos) +{ + if (s[pos - 1] == '+') + { + return s[pos] - 'A' + 1; + } + return -(s[pos] - 'A' + 1); +} + +bool ok_one(long x) +{ + for (long i = 1; i <= kind; i++) + { + if ((inf[x][i] == 1 && choose[i]) || (inf[x][i] == -1 && !choose[i])) + { + return true; + } + } + return false; +} + +void dfs(long dep) +{ + if (find_) + { + return; + } + if (dep > num) + { + find_ = true; + for (long i = 1; i <= kind; i++) + { + ans[i] = choose[i]; + } + return; + } + if (ok_one(dep)) + { + dfs(dep + 1); + } + for (long i = 1; i <= kind && !find_; i++) + { + if (inf[dep][i] == 1 && !choose[i]) + { + choose[i] = true; + dfs(dep + 1); + choose[i] = false; + } + } +} + +int main() +{ + while (gets(str) != 0) + { + num = 0; + find_ = false; + for (long i = 1; i < maxn; i++) + { + for (long j = 1; j <= kind; j++) + { + inf[i][j] = 0; + } + } + + // clear + while (strcmp(str, End_Sign) != 0) + { + num++; + long i = 0, t; + while (str[i] != ';') + { + t = h(str, i + 1); + if (t > 0) + { + inf[num][t] = 1; + } + else + { + inf[num][-t] = -1; + } + i += 2; + } + gets(str); + } + + // init + for (long i = 1; i <= kind; i++) + { + choose[i] = false; + } + + dfs(1); + + // output + if (find_) + { + printf("Toppings: "); + for (long i = 1; i <= kind; i++) + { + if (ans[i]) + { + printf("%c", i + 'A' - 1); + } + } + putchar('\n'); + } + else + { + printf("No pizza can satisfy these requests.\n"); + } + } + return 0; +} diff --git a/566.cpp b/566.cpp new file mode 100644 index 0000000..b6febb5 --- /dev/null +++ b/566.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +typedef vector VS; + +VS split(const string &s) +{ + VS r; + for (int i = 0, j; i < (int)s.size();) + { + if (isspace(s[i])) + { + i++; + continue; + } + for (j = i + 1; j < (int)s.size() && !isspace(s[j]); j++) + ; + r.push_back(s.substr(i, j - i)); + i = j; + } + return r; +} + +map id; +vector parent; +vector mut; +string nm, from, gene; + +int process(VS &w) +{ + if (id.count(from) == 0) + { + return 1; + } + int p = id[from]; + for (int i = 5; i < (int)w.size(); i++) + { + gene = w[i]; + for (int x = p; x != 0; x = parent[x]) + for (int i = 0; i < (int)mut[x].size(); i++) + if (mut[x][i] == gene) + { + return 2; + } + } + int x = parent.size(); + id[nm] = x; + parent.push_back(p); + mut.push_back(VS()); + for (int i = 5; i < (int)w.size(); i++) + { + mut[x].push_back(w[i]); + } + return 0; +} + +int main() +{ + char line[256]; + id["ADAM"] = 0; + parent.assign(1, 0); + mut.assign(1, VS()); + while (gets(line)) + { + VS w = split(line); + if (w.size() < 4 || w[0] != "clone") + { + continue; + } + nm = w[1]; + from = w[3]; + printf("clone %s from %s ", nm.c_str(), from.c_str()); + switch (process(w)) + { + case 0: + printf("is consistent and safe\n"); + break; + case 1: + printf("has no connection to ADAM\n"); + break; + case 2: + printf("was at least twice mutated with %s\n", + gene.c_str()); + break; + } + } +} diff --git a/567.cpp b/567.cpp new file mode 100644 index 0000000..1673858 --- /dev/null +++ b/567.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define LLU long long unsigned int +#define LLD long long double +#define FOR(i, N) for (int i = 0; i < (N); i++) +#define MAX_INT (numeric_limits::max() / 3) +int NO, from, to, cnt_inp; +int ans[22][22], how; +char c; +bool first; +string str; +void init() +{ + FOR(i, 22) + FOR(j, 22) + ans[i][j] = MAX_INT; + FOR(i, 22) + ans[i][i] = 0; +} +void calc() +{ + FOR(k, 22) + FOR(i, 22) + FOR(j, 22) + ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]); +} +int main() +{ + first = true; + NO = 1; + while (cin >> how) + { + init(); + FOR(i, 19) + { + if (i != 0) + { + cin >> how; + } + from = i + 1; + FOR(j, how) + { + cin >> to; + ans[from][to] = 1; + ans[to][from] = 1; + } + } + calc(); + cin >> cnt_inp; + cout << "Test Set #" << NO++ << endl; + FOR(i, cnt_inp) + { + cin >> from >> to; + printf("%2d to %2d: %d\n", from, to, ans[from][to]); + } + cout << endl; + } +} diff --git a/568.cpp b/568.cpp new file mode 100644 index 0000000..6141eb5 --- /dev/null +++ b/568.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +int main() +{ + int N; + while (cin >> N) + { + int i; + long result = 1; + for (i = 1; i <= N; i++) + { + result *= i; + while (result % 10 == 0) + { + result /= 10; + } + result %= 100000; + } + cout << setw(5) << N << " -> " << result % 10 << endl; + } + return 0; +} diff --git a/570.cpp b/570.cpp new file mode 100644 index 0000000..ec9f793 --- /dev/null +++ b/570.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +int main() +{ + int n; + int HIT[56] = {0}; + int ERR[56] = {0}; + int DIG[56] = {0}; + int KILL[56] = {0}; + int BLOCK[56] = {0}; + int GAMES[56] = {0}; + char key; + int GameCount = 0; + int CurrentPlayerNumber; + while (cin >> key) + { + if (key == 'C') + { + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> CurrentPlayerNumber; + GAMES[CurrentPlayerNumber]++; + } + GameCount++; + continue; + } + if (key == 'H') + { + cin >> CurrentPlayerNumber; + HIT[CurrentPlayerNumber]++; + continue; + } + if (key == 'K') + { + cin >> CurrentPlayerNumber; + KILL[CurrentPlayerNumber]++; + continue; + } + if (key == 'E') + { + cin >> CurrentPlayerNumber; + ERR[CurrentPlayerNumber]++; + continue; + } + if (key == 'B') + { + cin >> CurrentPlayerNumber; + BLOCK[CurrentPlayerNumber]++; + continue; + } + if (key == 'D') + { + cin >> CurrentPlayerNumber; + DIG[CurrentPlayerNumber]++; + continue; + } + //key == 'R' + cout << "Player Hit Pct KPG BPG DPG" << endl; + cout << "-----------------------------------------" << endl; + for (int i = 0; i <= 55; i++) + if (GAMES[i]) + { + double HitPct = 0.0; + if (KILL[i] + ERR[i] + HIT[i]) + { + HitPct = double(KILL[i] - ERR[i]) / (KILL[i] + ERR[i] + HIT[i]); + } + double KPG = double(KILL[i]) / GAMES[i]; + double BPG = double(BLOCK[i]) / GAMES[i]; + double DPG = double(DIG[i]) / GAMES[i]; + printf("%02d %+5.3f % 7.3f % 7.3f % 7.3f", i, HitPct, KPG, BPG, DPG); + cout << endl; + } + int SumBLOCK = 0; + int SumKILL = 0; + int SumERR = 0; + int SumDIG = 0; + int SumHIT = 0; + for (int i = 0; i <= 55; i++) + if (GAMES[i]) + { + SumBLOCK += BLOCK[i]; + SumKILL += KILL[i]; + SumERR += ERR[i]; + SumDIG += DIG[i]; + SumHIT += HIT[i]; + } + double HitPct = 0.0; + if (SumKILL + SumERR + SumHIT) + { + HitPct = double(SumKILL - SumERR) / (SumKILL + SumERR + SumHIT); + } + double KPG = double(SumKILL) / GameCount; + double BPG = double(SumBLOCK) / GameCount; + double DPG = double(SumDIG) / GameCount; + printf("team %+5.3f % 7.3f % 7.3f % 7.3f", HitPct, KPG, BPG, DPG); + cout << endl + << endl; + memset(HIT, 0, sizeof(HIT)); + memset(ERR, 0, sizeof(ERR)); + memset(DIG, 0, sizeof(DIG)); + memset(KILL, 0, sizeof(KILL)); + memset(BLOCK, 0, sizeof(BLOCK)); + memset(GAMES, 0, sizeof(GAMES)); + GameCount = 0; + } + return 0; +} diff --git a/571.cpp b/571.cpp new file mode 100644 index 0000000..7191e22 --- /dev/null +++ b/571.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +#define MAXN 1001 +#define MAX 100000 +char F[MAXN][MAXN]; +char Com[6][15] = {"fill A", "fill B", "empty A", "empty B", "pour A B", "pour B A"}; +int Qa[MAX], Qb[MAX], H, T; +int Ca, Cb, G; +struct cc +{ + int parent; + char com; +} V[MAX]; +struct ss +{ + int a, b; +} pop; +int Push(int a, int b, int c) +{ + if (b == G) + { + Qa[H] = a; + V[H].parent = T - 1; + V[H].com = c; + Qb[H++] = b; + return 1; + } + if (F[a][b]) + { + return 0; + } + Qa[H] = a; + V[H].parent = T - 1; + V[H].com = c; + Qb[H++] = b; + F[a][b] = 1; + H %= MAX; + return 0; +} +ss Pop() +{ + pop.a = Qa[T]; + pop.b = Qb[T++]; + T %= MAX; + return pop; +} +int IsEmpty() +{ + return H == T; +} +int Genpush(int a, int b) +{ + int ta, tb, y; + if (a < Ca) + if (Push(Ca, b, 0)) + { + return 1; + } + if (b < Cb) + if (Push(a, Cb, 1)) + { + return 1; + } + if (a != 0) + if (Push(0, b, 2)) + { + return 1; + } + if (b != 0) + if (Push(a, 0, 3)) + { + return 1; + } + tb = Cb - b; + y = (a - tb) >= 0 ? Push(a - tb, Cb, 4) : Push(0, b + a, 4); + if (y) + { + return y; + } + ta = Ca - a; + y = (b - ta) >= 0 ? Push(Ca, b - ta, 5) : Push(a + b, 0, 5); + if (y) + { + return y; + } + return 0; +} +void Print(int n) +{ + if (n == 0) + { + //printf("%s\n",Com[V[n].com]); + return; + } + Print(V[n].parent); + printf("%s\n", Com[V[n].com]); +} +int BFS() +{ + ss p; + H = T = 0; + Push(0, 0, -1); + while (!IsEmpty()) + { + p = Pop(); + if (Genpush(p.a, p.b)) + { + Print(H - 1); + printf("success\n"); + return 0; + } + } + return 0; +} +int main() +{ + int i, j; + while (scanf("%d%d%d", &Ca, &Cb, &G) == 3) + { + BFS(); + for (i = 0; i < MAXN; i++) + for (j = 0; j < MAXN; j++) + { + F[i][j] = 0; + } + } + return 0; +} diff --git a/572.cpp b/572.cpp index e4d596f..4cfac63 100644 --- a/572.cpp +++ b/572.cpp @@ -1,74 +1,55 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define MAX 105 -int N, M; -char adjMat[105][105]; +int R, C; +char MAP[MAX][MAX]; -void floodFill(int X, int Y){ - if ( X < 0 || Y < 0 || X >= M || Y >= N ) +void circle(int r, int c) +{ + if (r < 0 || r >= R || c < 0 || c >= C) + { return; - if ( adjMat[X][Y] == '*' ) + } + if (MAP[r][c] != '@') + { return; - - adjMat[X][Y] = '*'; - - int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; - int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1}; - int j; - FOI(j, 0, 7) - floodFill(X + dx[j], Y + dy[j]); + } + MAP[r][c] = '1'; + for (int i = -1; i <= 1; i++) + for (int j = -1; j <= 1; j++) + if (i != 0 || j != 0) + { + circle(r + i, c + j); + } } - -int main(int argc, char **argv){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - scanf( "%d%d", &M, &N ); - if( M == 0 ) +int main() +{ + int i, j, count; + char input[20]; + while (1) + { + gets(input); + sscanf(input, "%d%d", &R, &C); + if (R == 0) + { break; - int i, j; - int oil = 0; - FOI(i, 0, M-1) - scanf("%s", adjMat[i]); - FOI(i, 0, M-1) - FOI(j, 0, N-1) - if ( adjMat[i][j] == '@' ){ - oil++; - floodFill(i, j); - } - - printf("%d\n", oil); + } + count = 0; + for (i = 0; i < R; i++) + { + gets(MAP[i]); + } + for (i = 0; i < R; i++) + for (j = 0; j < C; j++) + if (MAP[i][j] == '@') + { + count++; + circle(i, j); + } + printf("%d\n", count); } return 0; } diff --git a/573.cpp b/573.cpp index c9e9d76..6bcd9c3 100644 --- a/573.cpp +++ b/573.cpp @@ -1,58 +1,43 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define EPS 0.0000000001 +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while( true ){ - double H, U, D, F; - cin >> H; - if( H <= 0 ) - break; - cin >> U >> D >> F; - F = (U * F)/100.0; - - int day = 1; - double cur = 0.0; - while( true ){ - cur += U; - if( cur > H ){ - cout << "success on day " << day << endl; +int main() +{ + double H, U, D, F, loss, d_cover; + int day; + while (scanf("%lf", &H) && H) + { + scanf("%lf%lf%lf", &U, &D, &F); + loss = (U * F) / 100; + day = 0; + d_cover = 0; + while (1) + { + day++; + d_cover += U; + if (d_cover > H) + { break; } - U = max(U - F, 0.0); - cur -= D; - if( cur < 0.0 ){ - cout << "failure on day " << day << endl; + d_cover -= D; + if (d_cover < 0) + { break; } - day++; + U -= loss; + if (U < 0) + { + U = 0; + } + } + if (d_cover > H) + { + printf("success on day %d\n", day); + } + else + { + printf("failure on day %d\n", day); } } return 0; diff --git a/574.cpp b/574.cpp new file mode 100644 index 0000000..c58c9c3 --- /dev/null +++ b/574.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +int t; +int n; +int taken; +int nums[12]; +vector result; + +bool same(int a, int b) +{ + if (__builtin_popcount(a) != __builtin_popcount(b)) + { + return false; + } + int v1, v2; + int count = __builtin_popcount(a); + while (count) + { + for (int i = 0; i < 12; i++) + { + if ((a & 1 << i) != 0) + { + v1 = nums[i]; + a &= ~(1 << i); + break; + } + } + for (int i = 0; i < 12; i++) + { + if ((b & 1 << i) != 0) + { + v2 = nums[i]; + b &= ~(1 << i); + break; + } + } + count--; + if (v1 != v2) + { + return false; + } + } + return true; +} + +void solve(int element, int sum) +{ + if (sum == t) + { + bool ok = true; + for (int i = 0, sz = result.size(); i < sz; i++) + { + if (same(result[i], taken)) + { + ok = false; + break; + } + } + if (ok) + { + result.push_back(taken); + } + return; + } + if (element == n) + { + return; + } + for (int i = element; i < n; i++) + { + if (sum + nums[i] <= t) + { + taken |= 1 << i;// set bit + solve(i + 1, sum + nums[i]); + taken &= ~(1 << i);// clear bit + } + } +} + +bool theTruthIsOutThere(int a, int b) +{ + int bita, bitb; + for (int i = 11; i >= 0; i--) + { + bita = a & (1 << i); + bitb = b & (1 << i); + if (a == b) + { + continue; + } + return a > b; + } +} + +void print_result(int a) +{ + bool first = true; + for (int i = 11; i >= 0; i--) + { + if ((a & (1 << i)) != 0) + { + if (!first) + { + cout << "+"; + } + cout << nums[i]; + first = false; + } + } + cout << endl; +} + +int main() +{ + while (cin >> t >> n) + { + if (n == 0) + { + break; + } + taken = 0; + result.clear(); + for (int i = n - 1; i >= 0; i--) + { + cin >> nums[i]; + } + solve(0, 0); + cout << "Sums of " << t << ":" << endl; + if (result.empty()) + { + cout << "NONE" << endl; + continue; + } + sort(result.begin(), result.end(), theTruthIsOutThere); + for (int i = 0, sz = result.size(); i < sz; i++) + { + print_result(result[i]); + } + } + return 0; +} diff --git a/575.cpp b/575.cpp index dc4533c..075899c 100644 --- a/575.cpp +++ b/575.cpp @@ -1,48 +1,23 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - while(true){ - string str; - cin >> str; - if(str == "0") - break; - uint64 po=2, tot=0; - int i, len = str.length(); - for(i=len-1; i>=0; i--){ - tot += (str[i] - '0')*(po-1); - po *= 2; - } - cout< + +using namespace std; + +int main() +{ + string input; + while (cin >> input) + { + if (input == "0") + { + break; + } + int n = input.size(); + int res = 0; + for (int i = 0; i < n; i++) + { + res += ((int)input[i] - 48) * (pow(2, n - i) - 1); + } + cout << res << endl; + } + return 0; +} diff --git a/576.cpp b/576.cpp new file mode 100644 index 0000000..c444b08 --- /dev/null +++ b/576.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +bool isvowel(char n) +{ + if (n == 'a' || n == 'e' || n == 'i' || n == 'o' || n == 'u' || n == 'y') + { + return true; + } + else + { + return false; + } +} + +int main() +{ + char p[1000]; + int cnt1, cnt2, cnt3, i, ps1, ps2; + while (cin.getline(p, 1000)) + { + if (strcmp("e/o/i", p) == 0) + { + break; + } + cnt1 = 0; + cnt2 = 0; + cnt3 = 0; + for (i = 0; p[i] != '/'; i++) + ; + ps1 = i; + for (i++; p[i] != '/'; i++) + ; + ps2 = i; + for (i = 0; i < ps1; i++) + { + bool flag = false; + while (isvowel(p[i])) + { + flag = true; + i++; + } + if (flag == true) + { + cnt1++; + } + } + for (i = ps1; i < ps2; i++) + { + bool flag = false; + while (isvowel(p[i])) + { + flag = true; + i++; + } + if (flag == true) + { + cnt2++; + } + } + for (i = ps2; p[i] != '\0'; i++) + { + bool flag = false; + while (isvowel(p[i])) + { + flag = true; + i++; + } + if (flag == true) + { + cnt3++; + } + } + //print result: + if (cnt1 == 5 && cnt2 == 7 && cnt3 == 5) + { + cout << 'Y' << endl; + continue; + } + if (cnt1 != 5) + { + cout << 1 << endl; + continue; + } + if (cnt2 != 7) + { + cout << 2 << endl; + continue; + } + if (cnt3 != 5) + { + cout << 3 << endl; + } + } + return 0; +} diff --git a/577.cpp b/577.cpp new file mode 100644 index 0000000..6e17845 --- /dev/null +++ b/577.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +struct win +{ + int top, left, bottom, right; + int oldtop, oldleft, oldbottom, oldright; + bool fullsize; + int id; +} window[100]; + +int numwin = 0; +const int + barwidth = 25, + maxwidth = 1024, + maxheight = 1024; + +int inwindow(int x, int y) +{ + for (int i = numwin - 1; i >= 0; i--) + if (x >= window[i].left && x <= window[i].right && y >= window[i].top && y <= window[i].bottom) + { + return i; + } + return -1; +} + +bool inbar(int x, int y) +{ + return numwin != 0 && x >= window[numwin - 1].left + barwidth && x <= window[numwin - 1].right - barwidth && y >= window[numwin - 1].top && y < window[numwin - 1].top + barwidth; +} + +bool inclose(int x, int y) +{ + return numwin != 0 && x >= window[numwin - 1].left && x < window[numwin - 1].left + barwidth && y >= window[numwin - 1].top && y < window[numwin - 1].top + barwidth; +} + +bool inzoom(int x, int y) +{ + return numwin != 0 && x > window[numwin - 1].right - barwidth && x <= window[numwin - 1].right && y >= window[numwin - 1].top && y < window[numwin - 1].top + barwidth; +} + +int main() +{ + char ch1, ch2; + int top, right, bottom, left, x, y, current, savex, savey, id = 0, j; + bool moving = false, canclose = false, canzoom = false; + struct win tempwin; + bool done = false; + while (!done) + { + cin >> ch1 >> ch2; + switch (ch1) + { + case 'C': + cin >> left >> top >> right >> bottom; + break; + case 'R': + break; + case 'Z': + break; + default: + cin >> x >> y; + break; + } + switch (ch1) + { + case 'A': + if (moving) + { + window[numwin - 1].left += (x - savex); + window[numwin - 1].right += (x - savex); + window[numwin - 1].top += (y - savey); + window[numwin - 1].bottom += (y - savey); + cout << "Moved window " << window[numwin - 1].id << " to " + << window[numwin - 1].left << ", " + << window[numwin - 1].top << ", " + << window[numwin - 1].right << ", " + << window[numwin - 1].bottom << endl; + savex = x; + savey = y; + } + break; + case 'C': + window[numwin].top = top; + window[numwin].right = right; + window[numwin].bottom = bottom; + window[numwin].left = left; + window[numwin].fullsize = false; + window[numwin].id = id++; + cout << "Created window " << window[numwin].id << " at " + << left << ", " << top << ", " << right << ", " << bottom << endl; + numwin++; + break; + case 'D':// mouse down + // find if in window + current = inwindow(x, y); + if (current != -1) + { + cout << "Selected window " << window[current].id << endl; + tempwin = window[current]; + for (int i = current + 1; i < numwin; i++) + { + window[i - 1] = window[i]; + } + window[numwin - 1] = tempwin; + moving = inbar(x, y) && !window[numwin - 1].fullsize; + canclose = inclose(x, y); + canzoom = inzoom(x, y); + savex = x; + savey = y; + } + break; + case 'R': + for (j = 0; j < numwin; j++) + { + cout << "Window " << window[j].id << " at " + << window[j].left << ", " << window[j].top << ", " + << window[j].right << ", " << window[j].bottom << endl; + } + break; + case 'U': + if (moving) + { + window[numwin - 1].left += (x - savex); + window[numwin - 1].right += (x - savex); + window[numwin - 1].top += (y - savey); + window[numwin - 1].bottom += (y - savey); + // moving = false; (redundant?) + cout << "Moved window " << window[numwin - 1].id << " to " + << window[numwin - 1].left << ", " + << window[numwin - 1].top << ", " + << window[numwin - 1].right << ", " + << window[numwin - 1].bottom << endl; + } + else if (numwin != 0 && canclose && inclose(x, y)) + { + cout << "Closed window " << window[numwin - 1].id << endl; + numwin--; + } + else if (numwin != 0 && canzoom && inzoom(x, y)) + { + // zoom or unzoom + if (window[numwin - 1].fullsize) + { + window[numwin - 1].left = window[numwin - 1].oldleft; + window[numwin - 1].right = window[numwin - 1].oldright; + window[numwin - 1].top = window[numwin - 1].oldtop; + window[numwin - 1].bottom = window[numwin - 1].oldbottom; + window[numwin - 1].fullsize = false; + } + else + { + window[numwin - 1].oldleft = window[numwin - 1].left; + window[numwin - 1].oldright = window[numwin - 1].right; + window[numwin - 1].oldtop = window[numwin - 1].top; + window[numwin - 1].oldbottom = window[numwin - 1].bottom; + window[numwin - 1].left = 0; + window[numwin - 1].right = maxwidth - 1; + window[numwin - 1].top = 0; + window[numwin - 1].bottom = maxheight - 1; + window[numwin - 1].fullsize = true; + } + cout << "Resized window " << window[numwin - 1].id << " to " + << window[numwin - 1].left << ", " + << window[numwin - 1].top << ", " + << window[numwin - 1].right << ", " + << window[numwin - 1].bottom << endl; + } + moving = false; + canclose = false; + canzoom = false; + break; + case 'Z': + done = true; + } + } + return 0; +} diff --git a/578.cpp b/578.cpp new file mode 100644 index 0000000..b30293b --- /dev/null +++ b/578.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +typedef struct +{ + double x, y, z; +} vec; + +vec cross(vec a, vec b) +{ + vec c = {a.y * b.z - a.z * b.y, a.x * b.z - a.z * b.x, a.x * b.y - a.y * b.x}; + return c; +} + +double dot(vec a, vec b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +double len(vec a) +{ + return sqrt(dot(a, a)); +} + +int main() +{ + vec p[1030], m; + double r; + int i, n; + while (scanf("%d", &n) == 1 && n != 0) + { + assert(3 <= n && n <= 1024); + for (i = 0; i < n; i++) + { + scanf("%lf %lf %lf", &p[i].x, &p[i].y, &p[i].z); + } + p[n] = p[0]; + p[n + 1] = p[1]; + for (i = n + 1; i >= 0; i--) + { + p[i].x -= p[0].x, p[i].y -= p[0].y, p[i].z -= p[0].z; + } + m.x = m.y = m.z = r = 0.; + for (i = 0; i <= n; i++) + if (len(cross(p[i], p[i + 1])) > r) + { + r = len(m = cross(p[i], p[i + 1])); + } + if (r < 1e-8) + { + printf("0.000\n"); + continue; + } + m.x /= r; + m.y /= r; + m.z /= r; + for (r = 0., i = 0; i < n; i++) + { + r += dot(cross(p[i], p[i + 1]), m); + } + printf("%.3f\n", fabs(r) / 2. + 1e-9); + } + return 0; +} diff --git a/579.cpp b/579.cpp index 639e4e1..65a9f14 100644 --- a/579.cpp +++ b/579.cpp @@ -1,32 +1,32 @@ -#include - -int main() { - int h[1000], m[1000], n=0, i=0; - scanf("%d",&n); - for (i = 0; i < n; i++) { - scanf("%d:%d\n",&h[i],&m[i]); - if(h[i] == 12 && m[i] == 00) { - h[i]=12; - m[i]=00; - } - else if (h[i] == 12 && m[i] != 00) - h[i]=11; - else if(h[i]==06 && m[i]==30) { - h[i]=06; - m[i]=30; - } - else if(h[i]<<11) - h[i] = 11 - h[i]; - else if(h[i]=11) - h[i]=12; - - m[i] = 60 - m[i]; - - if (m[i] == 60) - m[i]=00; - } - for (i = 0; i < n; i++) - printf("%2.2d:%2.2d\n",h[i],m[i]); - - return 0; -} +#include + +using namespace std; + +/** + * UVa 579 ClockHands + * Author: chchwy + * Last Modified: 2011.04.06 + * Blog: http://chchwy.blogspot.com + */ + +float angle_between(float hr, float min) +{ + float hr_angle = (hr / 12 * 360) + (min / 60 * (360 / 12)); + float min_angle = min / 60 * 360; + float dif_angle = fabs(hr_angle - min_angle); + return (dif_angle < 180) ? dif_angle : (360.0 - dif_angle); +} + +int main() +{ + int hr, min; + char tmp; + while (scanf("%d%c%d", &hr, &tmp, &min)) + { + if (hr == 0 && min == 0) + { + break; + } + printf("%.3f\n", angle_between(hr, min)); + } +} diff --git a/580.cpp b/580.cpp new file mode 100644 index 0000000..1d562da --- /dev/null +++ b/580.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; + +/** +508 +**/ +#define MAXN 70 +typedef unsigned long long ss; +ss T[MAXN]; +void Gen() +{ + int i; + T[1] = 1; + T[2] = 1; + T[3] = 2; + for (i = 4; i <= 66; i++) + { + T[i] = T[i - 1] + T[i - 2] + T[i - 3]; + } +} +int main() +{ + ss ways; + int n; + Gen(); + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + assert(n < 64); + ways = pow(2, n); + printf("%llu\n", ways - T[n + 2]); + } + return 0; +} diff --git a/581.cpp b/581.cpp new file mode 100644 index 0000000..29a9e83 --- /dev/null +++ b/581.cpp @@ -0,0 +1,160 @@ +#include + +using namespace std; + +char name[8][10] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}; +int wayx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int wayy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; + +char str[130][40], buf[1000], map_[100][60000], *s; +int h, w; + +int uppercase(char now) +{ + return now > 64 && now < 91; +} + +int chk(int x, int y, int dir) +{ + int i; + for (i = 0; s[i]; i++, x += wayx[dir], y += wayy[dir]) + { + if (s[i] != map_[x][y]) + { + return 0; + } + } + return 1; +} + +int main() +{ + int count, n, m, i, j, k, x, y, len; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &h, &n); + gets(buf); + for (i = 0; i < n; i++) + { + gets(str[i]); + } + scanf("%d", &m); + gets(buf); + x = y = 0; + while (m--) + { + gets(buf); + for (i = 0; buf[i]; i++) + { + if (uppercase(buf[i])) + { + map_[x++][y] = buf[i]; + if (x == h) + { + x = 0; + y++; + } + } + } + } + for (; x < h; x++) + { + map_[x][y] = 0; + } + w = y + 1; + for (k = 0; k < n; k++) + { + len = strlen(s = str[k]); + for (j = 0; j < w; j++) + { + for (i = 0; i < h; i++) + { + if (map_[i][j] == *s) + { + if (i >= len - 1) + { + if (chk(i, j, 0)) + { + printf("%s %d %s\n", str[k], j * h + i, name[0]); + break; + } + if (j + len - 1 < w) + { + if (chk(i, j, 1)) + { + printf("%s %d %s\n", str[k], j * h + i, name[1]); + break; + } + } + } + if (j + len - 1 < w) + { + if (chk(i, j, 2)) + { + printf("%s %d %s\n", str[k], j * h + i, name[2]); + break; + } + } + if (i + len - 1 < h) + { + if (j + len - 1 < w) + { + if (chk(i, j, 3)) + { + printf("%s %d %s\n", str[k], j * h + i, name[3]); + break; + } + } + if (chk(i, j, 4)) + { + printf("%s %d %s\n", str[k], j * h + i, name[4]); + break; + } + if (j >= len - 1) + { + if (chk(i, j, 5)) + { + printf("%s %d %s\n", str[k], j * h + i, name[5]); + break; + } + } + } + if (j >= len - 1) + { + if (chk(i, j, 6)) + { + printf("%s %d %s\n", str[k], j * h + i, name[6]); + break; + } + } + if (i >= len - 1) + { + if (j >= len - 1) + { + if (chk(i, j, 7)) + { + printf("%s %d %s\n", str[k], j * h + i, name[7]); + break; + } + } + } + } + } + if (i < h) + { + break; + } + } + if (j == w) + { + printf("%s NOT FOUND\n", s); + } + } + if (count) + { + printf("\n"); + } + } + return 0; +} diff --git a/583.cpp b/583.cpp index 13f598f..1bd71e9 100644 --- a/583.cpp +++ b/583.cpp @@ -1,54 +1,118 @@ -#include - -const long long MAX = 50000; -long long P[10000]; -long long S = 0; -static bool prime[MAX + 1]; - -void sieve() { - prime[0] = true; - prime[1] = true; - - for (long long i = 2; i <= MAX; i++) { - if (!prime[i]) { - P[S++] = i; - for (long long j = i * i; j <= MAX; j += i) { - prime[j] = true; - } +#include + +using namespace std; + +const long maxnum = 2000000001; + +int p[1000010]; +int prime[100000]; + +void compute_prime_table() +{ + int i, j; + p[0] = p[1] = 0; + for (i = 2; i <= 1000000; i++) + { + p[i] = 1; + } + for (i = 2; i <= 1000;) + { + for (j = i + i; j <= 1000000; j += i) + { + p[j] = 0; + } + for (i++; !p[i]; i++) + ; + } +} + +void get_prime() +{ + int i, j; + compute_prime_table(); + for (i = 0, j = 0; i < 50000; i++) + { + if (p[i] != 0) + { + prime[j] = i; + j++; + } + } +} + +bool judge_prime(int x) +{ + int i; + for (i = 2; i <= sqrt(x); i++) + if (x % i == 0) + { + return false; + } + return true; +} + +void process(int n) +{ + int i = 0, num = 0, j; + int array[1000]; + while (n) + { + if (n < prime[i]) + { + break; + } + if (n % prime[i] == 0) + { + array[num++] = prime[i]; + n /= prime[i]; } + else + { + i++; + } + } + for (j = 0; j < num - 1; j++) + { + cout << array[j] << " x "; } + cout << array[j] << endl; } -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - - sieve(); - long long N, I; - while (scanf("%lld", &N) != EOF) { - if (N == 0) break; - - long long M = 0; - long long V[50]; - I = N; - if (N < 0) { - V[M++] = -1; - N = -N; - } - for (long long i = 0; i < S; i++) { - if (N == 1) break; - while (N % P[i] == 0) { - V[M++] = P[i]; - N /= P[i]; +int main() +{ + get_prime(); + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + if (n < 0) + { + cout << n << " = -1 x "; + n *= -1; + if (judge_prime(n) == true) + { + cout << n << endl; + } + else + { + process(n); } } - if (N > 1) V[M++] = N; - - printf("%lld = %lld", I, V[0]); - for (long long i = 1; i < M; i++) { - printf(" x %lld", V[i]); + else + { + cout << n << " = "; + if (judge_prime(n) == true) + { + cout << n << endl; + } + else + { + process(n); + } } - printf("\n"); } return 0; } diff --git a/584.cpp b/584.cpp new file mode 100644 index 0000000..7935d28 --- /dev/null +++ b/584.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +/* +584 +Bowling +*/ +struct ss +{ + int r1, r2; + int Type; +} fra[15]; +char str[100]; +char temp[30]; +void Set() +{ + int i, r = 0, x, d = 0; + for (i = 0; str[i] && str[i] != '\n'; i += 2) + { + temp[d++] = str[i]; + } + temp[d] = NULL; + for (i = 0; i < d;) + { + if (temp[i] == 'X') + { + fra[r].Type = 2; + fra[r].r1 = 10; + i++; + r++; + } + else + { + fra[r].r1 = temp[i] - '0'; + x = 10 - fra[r].r1; + fra[r].r2 = temp[i + 1] - '0'; + if (temp[i + 1] == '/') + { + fra[r].Type = 1; + fra[r].r2 = x; + } + i += 2; + r++; + } + } +} +int Count(int n) +{ + int c = 0; + if (fra[n].Type == 0) + { + return fra[n].r1 + fra[n].r2; + } + if (fra[n].Type == 1) + { + c = 10 + fra[n + 1].r1; + } + else + { + c = 10 + fra[n + 1].r1; + if (fra[n + 1].Type == 2) + { + c += fra[n + 2].r1; + } + else + { + c += fra[n + 1].r2; + } + } + return c; +} +void Cal() +{ + int i, j = 0; + for (i = 0; i < 10; i++) + { + j += Count(i); + } + printf("%d\n", j); +} +void Ini() +{ + int i; + for (i = 0; i < 15; i++) + { + fra[i].r1 = fra[i].r2 = fra[i].Type = 0; + } +} +int main() +{ + while (gets(str)) + { + if (!strcmp(str, "Game Over")) + { + break; + } + Ini(); + Set(); + Cal(); + } + return 0; +} diff --git a/585.cpp b/585.cpp new file mode 100644 index 0000000..9d8674e --- /dev/null +++ b/585.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +const int MaxSize = 200; + +int n, ans, Max; + +struct node +{ + int x, y; + char ch; +} temp, temp1, temp2, map_[MaxSize][MaxSize]; + +queue q, tr1, tr2; + +bool input() +{ + int i, j, up; + if (EOF == scanf("%d", &n) || n == 0) + { + return false; + } + up = 2 * n; + memset(map_, 0, sizeof(map_)); + for (i = 0; i < n; ++i) + { + getchar(); + --up; + for (j = 0; j < up; ++j) + { + scanf("%c", &map_[i][j].ch); + map_[i][j].x = i; + map_[i][j].y = j; + if (map_[i][j].ch == '-') + { + tr1.push(map_[i][j]); + tr2.push(map_[i][j]); + } + } + } + return true; +} + +bool check(int row, int from, int to) +{ + if (row >= n || row < 0 || from < 0) + { + return false; + } + int i; + for (i = from; i <= to; ++i) + if (map_[row][i].ch != '-') + { + return false; + } + return true; +} + +int bfs1() +{ + int tans; + while (!q.empty()) + { + q.pop(); + } + while (!tr1.empty()) + { + temp1 = tr1.front(); + tr1.pop(); + tans = temp1.x; + if ((temp1.x + temp1.y) % 2 == 0) + { + continue; + } + q.push(temp1); + q.push(temp1); + while (!q.empty()) + { + temp1 = q.front(); + q.pop(); + temp2 = q.front(); + q.pop(); + ans = max(ans, temp1.x - tans + 1); + if (check(temp1.x + 1, temp1.y - 1, temp2.y + 1)) + { + q.push(map_[temp1.x + 1][temp1.y - 1]); + q.push(map_[temp1.x + 1][temp2.y + 1]); + } + } + } + return ans; +} + +int bfs2() +{ + int tans; + while (!q.empty()) + { + q.pop(); + } + while (!tr2.empty()) + { + temp1 = tr2.front(); + tr2.pop(); + tans = temp1.x; + if ((temp1.x + temp1.y) % 2) + { + continue; + } + q.push(temp1); + q.push(temp1); + while (!q.empty()) + { + temp1 = q.front(); + q.pop(); + temp2 = q.front(); + q.pop(); + ans = max(ans, tans - temp1.x + 1); + if (check(temp1.x - 1, temp1.y - 1, temp2.y + 1)) + { + q.push(map_[temp1.x - 1][temp1.y - 1]); + q.push(map_[temp1.x - 1][temp2.y + 1]); + } + } + } + return ans; +} + +void bfs() +{ + ans = 0; + bfs1(); + bfs2(); + printf("The largest triangle area is %d.\n\n", ans * ans); +} + +int main() +{ + int cases; + for (cases = 1; input(); ++cases) + { + printf("Triangle #%d\n", cases); + bfs(); + } + return 0; +} diff --git a/586.cpp b/586.cpp new file mode 100644 index 0000000..fa345ea --- /dev/null +++ b/586.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +stack> S; +int Count[15]; + +void Cal() +{ + string ss; + int n, f = 0; + char com[100]; + S.push(make_pair(1, 0)); + while (!S.empty()) + { + cin >> com; + ss = com; + if (ss == "LOOP") + { + cin >> com; + ss = com; + if (ss == "n") + S.push(make_pair(S.top().first, S.top().second + 1)); + else + { + n = atoi(com); + S.push(make_pair(S.top().first * n, S.top().second)); + } + } + else if (ss == "OP") + { + cin >> n; + n *= S.top().first; + Count[S.top().second] += n; + } + else + S.pop(); + } + for (n = 10; n > 0; n--) + { + if (Count[n] > 0) + { + if (f) + cout << "+"; + if (Count[n] > 1) + cout << Count[n] << "*"; + cout << "n"; + if (n > 1) + cout << "^" << n; + f = 1; + } + } + if (Count[0] > 0) + { + if (f) + cout << "+"; + cout << Count[n]; + f = 1; + } + if (!f) + cout << "0"; + cout << endl; + memset(Count, 0, sizeof(int) * 15); +} + +int main() +{ + int ks, k = 1; + char ss[100]; + cin >> ks; + while (ks--) + { + cin >> ss; + cout << "Program #" << k++ << endl; + cout << "Runtime = "; + Cal(); + cout << endl; + } + return 0; +} diff --git a/587.cpp b/587.cpp new file mode 100644 index 0000000..892071c --- /dev/null +++ b/587.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +const double EPS = 0.000000001; + +class Vector2f +{ +public: + double x, y; + Vector2f(double px, double py) + { + x = px, y = py; + } + Vector2f operator+=(Vector2f right) + { + x += right.x, y += right.y; + } + Vector2f operator*(int c) + { + return Vector2f(x * c, y * c); + } + double distance(double px, double py) + { + double dx = x - px; + double dy = y - py; + return sqrt((dx * dx) + (dy * dy)); + } +}; + +// 8 directions +const double SQRT2 = sqrt(2) / 2; +Vector2f N(0, 1); +Vector2f NE(SQRT2, SQRT2); +Vector2f E(1, 0); +Vector2f SE(SQRT2, -SQRT2); +Vector2f S(0, -1); +Vector2f SW(-SQRT2, -SQRT2); +Vector2f W(-1, 0); +Vector2f NW(-SQRT2, SQRT2); + +bool isComma(char c) +{ + return (c == ',' || c == '.'); +} + +// return the treasure location according to line +Vector2f findTreasure(string line) +{ + // replace comma to white-space for stringstream extraction + replace_if(line.begin(), line.end(), isComma, ' '); + Vector2f loc(0, 0);//current location + istringstream sin(line); + string direct; + int step; + while (sin >> step >> direct) + { + if (direct == "N") + loc += N * step; + else if (direct == "NE") + loc += NE * step; + else if (direct == "E") + loc += E * step; + else if (direct == "SE") + loc += SE * step; + else if (direct == "S") + loc += S * step; + else if (direct == "SW") + loc += SW * step; + else if (direct == "W") + loc += W * step; + else if (direct == "NW") + loc += NW * step; + } + return loc; +} + +int main() +{ + int mapNo = 1; + string line; + while (getline(cin, line)) + { + if ("END" == line) + break; + Vector2f target = findTreasure(line); + printf("Map #%d\n", mapNo++); + printf("The treasure is located at (%.3lf,%.3lf).\n", target.x + EPS, target.y + EPS); + printf("The distance to the treasure is %.3lf.\n\n", target.distance(0, 0)); + } + return 0; +} diff --git a/588.cpp b/588.cpp new file mode 100644 index 0000000..0c9c92a --- /dev/null +++ b/588.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +// @judge_id: 13345pf 588 c++ + +int top, down, le, ri; +int cases = 0; + +struct posi +{ + int x; + int y; +}; + +void work(posi s, posi e) +{ + if (s.x == e.x) + if (s.y < e.y) + { + if (s.x > le) + { + le = s.x; + } + } + else + { + if (s.x < ri) + { + ri = s.x; + } + } + else if (s.x < e.x) + { + if (s.y < top) + { + top = s.y; + } + } + else + { + if (s.y > down) + { + down = s.y; + } + } +} + +bool input() +{ + int n; + cin >> n; + if (n == 0) + { + return false; + } + top = ri = 32767; + down = le = -32768; + posi s, e, start; + cin >> e.x >> e.y; + start = e; + while (--n) + { + s = e; + cin >> e.x >> e.y; + work(s, e); + } + work(e, start); + cout << "Floor #" << ++cases << endl; + cout << "Surveillance is "; + if (top < down || ri < le) + { + cout << "impossible."; + } + else + { + cout << "possible."; + } + cout << endl + << endl; + return true; +} + +int main() +{ + while (input()) + ; + return 0; +} diff --git a/589.cpp b/589.cpp new file mode 100644 index 0000000..e53fab0 --- /dev/null +++ b/589.cpp @@ -0,0 +1,169 @@ +#include + +using namespace std; + +struct node +{ + friend bool operator<(node n1, node n2) + { + if (n1.push != n2.push) + return n2.push < n1.push; + else + return n2.step < n1.step; + } + int step, push, people, box; + string ans; +}; + +struct point {int push, step;}; + +char map_[21][21]; +int m, n; +point dp[401][401]; + +bool legal(int x, int y) +{ + if (x < 1 || x > m || y < 1 || y > n) + return false; + if (map_[x][y] == '#') + return false; + return true; +} + +priority_queue q; + +int main() +{ + int i, j, ex, ey, x, y, final; + int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};//rldu + node ss, tt; + int cases = 1; + while (cin >> m >> n) + { + if (m == 0 && n == 0) + break; + cout << "Maze #" << cases++ << endl; + ss.push = ss.step = 0; + ss.ans = ""; + for (i = 1; i <= m * n; i++) + for (j = 1; j <= m * n; j++) + { + dp[i][j].push = 10000; + dp[i][j].step = 10000; + } + for (i = 1; i <= m; i++) + for (j = 1; j <= n; j++) + { + cin >> map_[i][j]; + if (map_[i][j] == 'T') + { + ex = i; + ey = j; + } + if (map_[i][j] == 'S') + ss.people = (i - 1) * n + j; + if (map_[i][j] == 'B') + ss.box = (i - 1) * n + j; + } + while (!q.empty()) + q.pop(); + dp[ss.people][ss.box].push = 0; + dp[ss.people][ss.box].step = 0; + q.push(ss); + final = (ex - 1) * n + ey; + int ff = 0; + while (!q.empty()) + { + ss = q.top(); + q.pop(); + if (ss.push > dp[ss.people][ss.box].push) + continue; + if (ss.push == dp[ss.people][ss.box].push && ss.step > dp[ss.people][ss.box].step) + continue; + if (ss.box == final) + { + cout << ss.ans << endl; + ff = 1; + break; + } + for (i = 0; i < 4; i++) + { + tt = ss; + x = tt.people / n; + if (tt.people % n) + x++; + y = tt.people % n; + if (y == 0) + y = n; + x += dir[i][0]; + y += dir[i][1]; + if (!legal(x, y)) + continue; + tt.people = (x - 1) * n + y; + tt.step++; + if (tt.people != tt.box) + { + if (i == 0) + tt.ans = tt.ans + "e"; + if (i == 1) + tt.ans = tt.ans + "w"; + if (i == 2) + tt.ans = tt.ans + "s"; + if (i == 3) + tt.ans = tt.ans + "n"; + if (tt.push < dp[tt.people][tt.box].push) + { + dp[tt.people][tt.box].push = tt.push; + dp[tt.people][tt.box].step = tt.step; + q.push(tt); + } + else if (tt.push == dp[tt.people][tt.box].push && tt.step < dp[tt.people][tt.box].step) + { + dp[tt.people][tt.box].push = tt.push; + dp[tt.people][tt.box].step = tt.step; + q.push(tt); + } + } + else + { + tt.push++; + x = tt.box / n; + if (tt.box % n) + x++; + y = tt.box % n; + if (y == 0) + y = n; + x += dir[i][0]; + y += dir[i][1]; + if (!legal(x, y)) + continue; + tt.box = (x - 1) * n + y; + if (i == 0) + tt.ans = tt.ans + "E"; + if (i == 1) + tt.ans = tt.ans + "W"; + if (i == 2) + tt.ans = tt.ans + "S"; + if (i == 3) + tt.ans = tt.ans + "N"; + if (tt.push < dp[tt.people][tt.box].push) + { + dp[tt.people][tt.box].push = tt.push; + dp[tt.people][tt.box].step = tt.step; + q.push(tt); + } + else if (tt.push == dp[tt.people][tt.box].push && tt.step < dp[tt.people][tt.box].step) + { + dp[tt.people][tt.box].push = tt.push; + dp[tt.people][tt.box].step = tt.step; + q.push(tt); + } + } + } + } + if (!ff) + cout << "Impossible." << endl; + cout << endl; + } + return 0; +} diff --git a/590.cpp b/590.cpp new file mode 100644 index 0000000..d5608c4 --- /dev/null +++ b/590.cpp @@ -0,0 +1,112 @@ +#include + +using namespace std; + +#define MAXN 1002 +#define INF 2147483647 + +struct ss +{ + int schedule[31]; + int ind; +} city[11][11]; +struct xx +{ + int node; + int level; +}; +queue Q; +int N, K, Min, Cost[11][MAXN]; + +void ReadCase() +{ + int i, j, k, sc; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (i == j) + continue; + scanf("%d", &sc); + for (k = 0; k < sc; k++) + scanf("%d", &city[i][j].schedule[k]); + city[i][j].ind = k; + } + } +} +void BFS() +{ + xx s, t; + int i, d, k, cost, u; + s.level = -1; + s.node = 1; + Q.push(s); + Min = INF; + Cost[1][0] = 0; + while (!Q.empty()) + { + t = Q.front(); + Q.pop(); + u = t.node; + d = t.level + 1; + if (t.level < 0) + t.level = 0; + for (i = 1; i <= N; i++) + { + if (i == u) + continue; + if (d == K - 1 && i != N) + continue; + k = city[u][i].schedule[d % city[u][i].ind]; + if (!k) + continue; + cost = Cost[u][t.level] + k; + if (d == K - 1) + { + if (cost < Min) + Min = cost; + continue; + } + if (Cost[i][d] <= cost) + continue; + if (Cost[i][d] != INF) + { + if (Cost[i][d] > cost) + { + Cost[i][d] = cost; + continue; + } + } + Cost[i][d] = cost; + s.level = d; + s.node = i; + Q.push(s); + } + } +} +void Cal() +{ + BFS(); + if (Min == INF) + printf("No flight possible.\n"); + else + printf("The best flight costs %d.\n", Min); +} + +int main() +{ + int i, j, s = 1; + while (scanf("%d%d", &N, &K) == 2) + { + if (!N && !K) + break; + for (i = 1; i <= N; i++) + for (j = 0; j <= K; j++) + Cost[i][j] = INF; + ReadCase(); + printf("Scenario #%d\n", s++); + Cal(); + printf("\n"); + } + return 0; +} diff --git a/591.cpp b/591.cpp new file mode 100644 index 0000000..23f027d --- /dev/null +++ b/591.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int n, h[100], cases = 1; + while (cin >> n) + { + if (n == 0) + { + break; + } + cout << "Set #" << cases++ << endl; + int i, total = 0, average, min = 0; + for (i = 0; i < n; i++) + { + cin >> h[i]; + total += h[i]; + } + average = total / n; + for (i = 0; i < n; i++) + { + if (h[i] > average) + { + min += h[i] - average; + } + } + cout << "The minimum number of moves is " << min << "." << endl; + cout << endl; + } + return 0; +} diff --git a/592.cpp b/592.cpp new file mode 100644 index 0000000..d41e9f8 --- /dev/null +++ b/592.cpp @@ -0,0 +1,167 @@ +#include + +using namespace std; + +#define DIVINE 0 +#define EVIL 1 +#define HUMAN 2 +#define DAY 0 +#define NIGHT 1 +#define MAXPERSONS 5 +#define STATENUM 3 * 3 * 3 * 3 * 3 * 2 +char state[STATENUM]; +void init() +{ + for (int i = 0; i < STATENUM; i++) + { + state[i] = true; + } +} +int type(int state, int p) +{ + state /= 2; + while (p--) + { + state /= 3; + } + return state % 3; +} +int lying(int state, int p) +{ + int daynight = state % 2; + int t = type(state, p); + return (t == EVIL || (t == HUMAN && daynight == NIGHT)); +} +void read() +{ + int i, neg, speaker, target, t, truth; + char s[50]; + gets(s); + speaker = s[0] - 'A'; + if (s[3] == 'I' && s[4] == 't') + { + for (i = 0; i < STATENUM; i++) + { + truth = ((i % 2 == DAY && s[9] == 'd') || (i % 2 == NIGHT && s[9] == 'n')); + if (lying(i, speaker) == truth) + { + state[i] = 0; + } + } + return; + } + if (s[5] == 'a') + { + target = speaker; + } + else + { + target = s[3] - 'A'; + } + neg = (s[8] == 'n'); + switch (s[8 + 4 * neg]) + { + case 'd': + t = DIVINE; + break; + case 'h': + t = HUMAN; + break; + case 'e': + t = EVIL; + break; + case 'l': + for (i = 0; i < STATENUM; i++) + { + truth = neg ^ lying(i, target); + if (lying(i, speaker) == truth) + { + state[i] = 0; + } + } + return; + } + for (i = 0; i < STATENUM; i++) + { + truth = neg ^ (type(i, target) == t); + if (lying(i, speaker) == truth) + { + state[i] = 0; + } + } + return; +} +void output() +{ + int deduction, i, j, possible[MAXPERSONS], daynight; + char *names[] = {"divine", "evil", "human"}; + for (i = 0; i < MAXPERSONS; i++) + { + possible[i] = -1; + } + daynight = -1; + for (i = 0; i < STATENUM; i++) + { + if (state[i]) + { + if (daynight != -1 && daynight != i % 2) + { + daynight = -2; + } + else if (daynight == -1) + { + daynight = i % 2; + } + for (j = 0; j < MAXPERSONS; j++) + { + if (possible[j] != -1 && possible[j] != type(i, j)) + possible[j] = + -2; + else if (possible[j] == -1) + { + possible[j] = type(i, j); + } + } + } + } + if (daynight == -1) + { + printf("This is impossible.\n\n"); + return; + } + deduction = 0; + for (i = 0; i < MAXPERSONS; i++) + if (possible[i] >= 0) + { + printf("%c is %s.\n", i + 'A', names[possible[i]]); + deduction = 1; + } + if (daynight >= 0) + { + printf("It is %s.\n", daynight == DAY ? "day" : "night"); + deduction = 1; + } + if (!deduction) + { + printf("No facts are deducible.\n"); + } + printf("\n"); +} +int main() +{ + int n, count = 1; + while (scanf("%d ", &n) == 1) + { + if (!n) + { + break; + } + init(); + for (int i = 0; i < n; i++) + { + read(); + } + printf("Conversation #%d\n", count++); + output(); + } +} diff --git a/593.cpp b/593.cpp new file mode 100644 index 0000000..001a0b9 --- /dev/null +++ b/593.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include + +#define REP(i, n) for (int i = 0; i < (int)n; ++i) +#define FOR(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) +#define ALL(c) (c).begin(), (c).end() + +using namespace std; + +struct Packet +{ + int h, p, t; + Packet(int _h, int _p, int _t) + : h(_h), p(_p), t(_t) + { + } +}; +bool operator<(Packet a, Packet b) +{ + if (a.h != b.h) + return a.h < b.h; + if (a.p != b.p) + return a.p < b.p; + return a.t < b.t; +} + +int main(void) +{ + const int NODE = 10 + 1, INF = 1 << 20; + int adj[NODE][NODE], tc = 0; + for (int n;;) + { + cin >> n; + if (n == 0) + break; + map iland; + map host_iland; + map > group_hosts; + int cnt = 0; + fill(&adj[0][0], &adj[NODE - 1][NODE], INF); + + for (int i = 0; i < n; ++i) + {// input + string s, t; + int m, h, cost; + cin >> s >> m; + if (iland.find(s) == iland.end()) + { + iland[s] = cnt++; + } + for (int j = 0; j < m; ++j) + { + char c; + cin >> c; + if (c == 'H') + { + cin >> h; + host_iland[h] = iland[s]; + } + else + { + cin >> cost >> t; + if (iland.find(t) == iland.end()) + { + iland[t] = cnt++; + } + adj[iland[s]][iland[t]] = cost; + } + } + } + + for (int k = 0; k < NODE; ++k) + { + for (int i = 0; i < NODE; ++i) + { + for (int j = 0; j < NODE; ++j) + { + adj[i][j] = min(adj[i][j], adj[i][k] + adj[k][j]); + } + } + } + + for (int i = 0; i < NODE; ++i) + { + adj[i][i] = 0; + } + + vector v; + int query; + cin >> query; + for (int i = 0; i < query; ++i) + { + char c; + int h, g, p, t; + cin >> c; + if (c == 'J') + { + cin >> h >> g; + if (group_hosts.find(g) == group_hosts.end()) + { + group_hosts[g] = set(); + } + group_hosts[g].insert(h); + } + else if (c == 'L') + { + cin >> h >> g; + group_hosts[g].erase(h); + } + else + { + cin >> h >> g >> p >> t; + set group = group_hosts.find(g)->second; + int src = host_iland[h]; + FOR(j, group) + { + int dst = host_iland[*j]; + if (t >= adj[src][dst]) + { + v.push_back(Packet(*j, p, t - adj[src][dst])); + } + } + } + } + + sort(v.begin(), v.end()); + cout << "Network #" << ++tc << endl; + for (int i = 0; i < v.size(); ++i) + { + cout << v[i].h << ' ' << v[i].p << ' ' << v[i].t << endl; + } + + cout << endl; + } + return 0; +} diff --git a/594.cpp b/594.cpp index 5aab565..e7b3861 100644 --- a/594.cpp +++ b/594.cpp @@ -1,50 +1,18 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -long endianSolBits(long x){ - long temp = 0; // variable to hold result - int j; - - for (j=0; j<32; j++){ - if (x&(1<>x) - cout< + +using namespace std; + +int endian_swap(int i) +{ + return ((i & 0xff) << 24) + ((i & 0xff00) << 8) + ((i & 0xff0000) >> 8) + ((i >> 24) & 0xff); +} + +int main() +{ + int n; + while (cin >> n) + { + cout << n << " converts to " << endian_swap(n) << endl; + } + return 0; +} diff --git a/595.cpp b/595.cpp new file mode 100644 index 0000000..f978078 --- /dev/null +++ b/595.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +const char *data[][7] = { + {"Ab", "Bb", "C", "Db", "Eb", "F", "G"}, + {"A", "B", "C#", "D", "E", "F#", "G#"}, + {"Bb", "C", "D", "Eb", "F", "G", "A"}, + {"B", "C#", "D#", "E", "F#", "G#", "A#"}, + {"Cb", "Db", "Eb", "Fb", "Gb", "Ab", "Bb"}, + {"C", "D", "E", "F", "G", "A", "B"}, + {"C#", "D#", "E#", "F#", "G#", "A#", "B#"}, + {"Db", "Eb", "F", "Gb", "Ab", "Bb", "C"}, + {"D", "E", "F#", "G", "A", "B", "C#"}, + {"Eb", "F", "G", "Ab", "Bb", "C", "D"}, + {"E", "F#", "G#", "A", "B", "C#", "D#"}, + {"F", "G", "A", "Bb", "C", "D", "E"}, + {"F#", "G#", "A#", "B", "C#", "D#", "E#"}, + {"Gb", "Ab", "Bb", "Cb", "Db", "Eb", "F"}, + {"G", "A", "B", "C", "D", "E", "F#"}}; + +int main() +{ + char str1[16], str2[16]; + bool fst = true; + while (1) + { + scanf("%s", str1); + if (*str1 == '*') + { + break; + } + else + { + if (fst) + { + fst = false; + } + else + { + printf("\n"); + } + } + scanf("%s", str2); + if (*str2 == '*') + { + break; + } + int from, to; + for (from = 0; from < 15; from++) + { + if (strcmp(data[from][0], str1) == 0) + { + break; + } + } + if (from == 15) + { + printf("Key of %s is not a valid major key\n", str1); + } + for (to = 0; to < 15; to++) + { + if (strcmp(data[to][0], str2) == 0) + { + break; + } + } + if (from < 15 && to == 15) + { + printf("Key of %s is not a valid major key\n", str2); + } + if (from < 15 && to < 15) + { + printf("Transposing from %s to %s:\n", str1, str2); + } + while (1) + { + scanf("%s", str2); + if (*str2 == '*') + { + break; + } + if (from == 15 || to == 15) + { + continue; + } + int j; + for (j = 0; j < 7; j++) + { + if (strcmp(data[from][j], str2) == 0) + { + break; + } + } + if (j == 7) + { + printf(" %s is not a valid note in the %s major scale\n", + str2, str1); + } + else + { + printf(" %s transposes to %s\n", str2, data[to][j]); + } + } + } + return 0; +} diff --git a/596.cpp b/596.cpp new file mode 100644 index 0000000..933da00 --- /dev/null +++ b/596.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +struct Pt +{ + long long x, y; + + bool operator<(const Pt &a) const + { + if (x != a.x) + { + return x < a.x; + } + return y < a.y; + } +}; + +long long cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} + +int monotone(int n, Pt p[], Pt ch[]) +{ + sort(p, p + n); + int i, m = 0, t; + for (i = 0; i < n; i++) + { + while (m >= 2 && cross(ch[m - 2], ch[m - 1], p[i]) < 0) + { + m--; + } + ch[m++] = p[i]; + } + for (i = n - 2, t = m + 1; i >= 0; i--) + { + while (m >= t && cross(ch[m - 2], ch[m - 1], p[i]) < 0) + { + m--; + } + ch[m++] = p[i]; + } + return m - 1; +} + +int main() +{ + while (getchar() != ' ') + ; + char cases[105]; + Pt p[10005], ch[10005]; + int i, j, k; + while (gets(cases)) + { + printf("%s convex hull:\n", cases); + int n = 0, m; + while (scanf("%s", cases) == 1) + { + if (cases[0] != 'P') + { + break; + } + scanf("%d", &m); + for (i = 0; i < m; i++, n++) + { + scanf("%lld%lld", &p[n].x, &p[n].y); + } + } + sort(p, p + n); + for (i = 1, j = 0; i < n; i++) + { + if (p[j].x != p[i].x || p[j].y != p[i].y) + { + p[++j] = p[i]; + } + } + n = j + 1; + m = monotone(n, p, ch); + int pos = 0; + for (i = 0; i < m; i++) + { + if (ch[i].x > ch[pos].x || (ch[i].x == ch[pos].x && ch[i].y < ch[pos].y)) + { + pos = i; + } + } + for (i = pos; i < m; i++) + { + printf(" (%lld,%lld)", ch[i].x, ch[i].y); + } + for (i = 0; i < pos; i++) + { + printf(" (%lld,%lld)", ch[i].x, ch[i].y); + } + printf("\n"); + if (cases[0] == 'E') + { + break; + } + while (getchar() != ' ') + ; + } + return 0; +} diff --git a/598.cpp b/598.cpp new file mode 100644 index 0000000..da2392b --- /dev/null +++ b/598.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +char np[13][32]; +int Up, Lb, N, ind, st[13]; + +void ReadCase() +{ + char temp[35], ss[100]; + int i, j; + gets(ss); + N = 0; + while (gets(temp)) + { + for (i = 0; temp[i]; i++) + if (temp[i] == '\n') + temp[i] = NULL; + if (strlen(temp) == 0) + break; + strcpy(np[N++], temp); + } + if (!strcmp(ss, "*")) + { + Up = N; + Lb = 1; + return; + } + j = sscanf(ss, "%d%d", &Lb, &Up); + if (j == 2) + return; + Lb = Up = Lb; +} +void recur(int n, int level, int Limit) +{ + int i, j; + st[level] = n; + if (level == Limit) + { + j = st[0]; + printf("%s", np[j]); + for (j = 1; j <= level; j++) + printf(", %s", np[st[j]]); + printf("\n"); + return; + } + for (i = n + 1; i < N; i++) + recur(i, level + 1, Limit); +} +void Cal() +{ + int i, j; + for (i = Lb; i <= Up; i++) + { + printf("Size %d\n", i); + for (j = 0; j <= N - i; j++) + recur(j, 0, i - 1); + printf("\n"); + } +} + +int main() +{ + char input[100]; + int kase; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + ReadCase(); + Cal(); + if (kase) + printf("\n"); + } + return 0; +} diff --git a/599.cpp b/599.cpp new file mode 100644 index 0000000..05b1afd --- /dev/null +++ b/599.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +#define maxn 30 + +char link[maxn][maxn], color[maxn], Hv[maxn]; +int Adj[maxn]; + +int Add(char ss[]) +{ + int i, u, v, f = 1; + for (i = 0; ss[i]; i++) + { + if (isalpha(ss[i])) + { + u = ss[i] - 'A'; + f = 0; + break; + } + } + if (f) + return f; + for (++i; ss[i]; i++) + { + if (isalpha(ss[i])) + { + v = ss[i] - 'A'; + f = 0; + break; + } + } + if (f) + return 1; + Hv[u] = 1; + Hv[v] = 1; + if (u != v) + { + Adj[u]++; + Adj[v]++; + link[u][v] = link[v][u] = 1; + } + return 0; +} +void Recur(int n) +{ + int i; + color[n] = 1; + for (i = 0; i < 26; i++) + if (link[n][i] && color[i] == 0) + Recur(i); +} +void Cal(char ss[]) +{ + int i, u, t, ac; + t = ac = 0; + for (i = 0; ss[i]; i++) + { + if (isalpha(ss[i])) + { + u = ss[i] - 'A'; + Hv[u] = 1; + } + } + for (i = 0; i < 26; i++) + { + if (Hv[i]) + { + u = i; + if (color[u] == 1) + continue; + if (Adj[u] == 0) + ac++; + else + { + Recur(u); + t++; + } + } + } + printf("There are %d tree(s) and %d acorn(s).\n", t, ac); +} +void Free() +{ + int i, j; + for (i = 0; i < 26; i++) + { + for (j = 0; j < 26; j++) + link[i][j] = 0; + Adj[i] = color[i] = 0; + Hv[i] = 0; + } +} + +int main() +{ + int ks; + char ss[100]; + gets(ss); + sscanf(ss, "%d", &ks); + while (ks--) + { + while (1) + { + gets(ss); + if (Add(ss)) + break; + } + gets(ss); + Cal(ss); + Free(); + } + return 0; +} diff --git a/601.cpp b/601.cpp new file mode 100644 index 0000000..ccb0a4b --- /dev/null +++ b/601.cpp @@ -0,0 +1,225 @@ +#include + +using namespace std; + +/** +601 +**/ +#define MAXN 82 +int MR[] = {0, 0, -1, 1}; +int MC[] = {1, -1, 0, 0}; +char B[MAXN][MAXN]; +char Temp[MAXN][MAXN]; +int N, ff, bla, whi; +int FW(int r, int c, char p) +{ + if (r >= N || r < 0 || c >= N || c < 0) + { + return 0; + } + if (Temp[r][c] != p) + { + return 0; + } + if (p == 'W' && c == N - 1) + { + return 1; + } + if (p == 'B' && r == N - 1) + { + return 1; + } + Temp[r][c] = 'X'; + for (int i = 0; i < 4; i++) + { + int nr = r + MR[i]; + int nc = c + MC[i]; + if (FW(nr, nc, p) == 1) + { + return 1; + } + } + return 0; +} +int Win(char p, int n) +{ + int i, j; + if (p == 'W' && whi > n) + { + return 1; + } + if (p == 'B' && bla > n) + { + return 1; + } + if (p == 'W') + { + j = 0; + for (i = 0; i < N; i++) + if (B[i][0] == 'W') + { + j++; + } + if (j > 1) + { + return 1; + } + } + if (p == 'B') + { + j = 0; + for (i = 0; i < N; i++) + if (B[0][i] == 'B') + { + j++; + } + if (j > 1) + { + return 1; + } + } + return 0; +} +int F(int r, int c, char p, int level) +{ + if (r >= N || r < 0 || c >= N || c < 0) + { + return 0; + } + if ((Temp[r][c] != p) && Temp[r][c] != 'U') + { + return 0; + } + if (Temp[r][c] == 'U' && ff == 0) + { + return 0; + } + if (Temp[r][c] == 'U') + { + ff = 0; + } + if (p == 'W' && c == N - 1 && Win(p, level)) + { + return 1; + } + if (p == 'B' && r == N - 1 && Win(p, level)) + { + return 1; + } + Temp[r][c] = 'X'; + for (int i = 0; i < 4; i++) + { + int nr = r + MR[i]; + int nc = c + MC[i]; + if (F(nr, nc, p, level + 1) == 1) + { + return 1; + } + if (Temp[nr][nc] == 'U') + { + ff = 1; + } + } + return 0; +} +void Count() +{ + int i, j; + bla = whi = 0; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + if (B[i][j] == 'B') + { + bla++; + } + else if (B[i][j] == 'W') + { + whi++; + } + } + } +} +void Cal() +{ + int i; + for (i = 0; i < N; i++) + { + strcpy(Temp[i], B[i]); + } + for (i = 0; i < N; i++) + { + if (Temp[i][0] == 'W') + if (FW(i, 0, 'W') == 1) + { + printf("White has a winning path.\n"); + return; + } + } + for (i = 0; i < N; i++) + { + strcpy(Temp[i], B[i]); + } + for (i = 0; i < N; i++) + { + if (B[0][i] == 'B') + if (FW(0, i, 'B') == 1) + { + printf("Black has a winning path.\n"); + return; + } + } + Count(); + for (i = 0; i < N; i++) + { + strcpy(Temp[i], B[i]); + } + for (i = 0; i < N; i++) + { + ff = 1; + if (Temp[i][0] == 'W') + if (F(i, 0, 'W', 0) == 1) + { + printf("White can win in one move.\n"); + return; + } + } + for (i = 0; i < N; i++) + { + strcpy(Temp[i], B[i]); + } + for (i = 0; i < N; i++) + { + ff = 1; + if (Temp[0][i] == 'B') + if (F(0, i, 'B', 0) == 1) + { + printf("Black can win in one move.\n"); + return; + } + } + printf("There is no winning path.\n"); +} +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + for (i = 0; i < N; i++) + { + scanf("%s", B[i]); + } + if (N == 1 && B[0][0] == 'U') + { + printf("White can win in one move.\n"); + continue; + } + Cal(); + } + return 0; +} diff --git a/602.cpp b/602.cpp new file mode 100644 index 0000000..4aecac7 --- /dev/null +++ b/602.cpp @@ -0,0 +1,122 @@ +#include + +using namespace std; + +// @JUDGE_ID: 13345PF 602 C++ + +inline bool test(int y) +{ + if (y < 1752) + { + return y % 4 == 0; + } + if (y >= 1752) + { + return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; + } +} + +int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; +char *month_name[] = + { + "January", "February", "March", "April", "May", "June", "July", + "August", "September", "October", "November", "December"}; + +char *day_name[] = + { + "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday", "Sunday"}; + +int cal(int y, int m, int d) +{ + int ret = 0; + for (int i = 0; i < m - 1; i++) + { + ret += month[i]; + if (i == 1) + { + ret += test(y); + } + } + ret += d; + return ret; +} + +int day(int y, int m, int d) +{ + int i, ret = 0; + if (y > 1752) + { + for (i = 1752; i < y; i++) + { + ret += (365 + test(i)) % 7; + } + ret -= cal(1752, 9, 14) % 7; + ret += cal(y, m, d) % 7; + ret += 3; + ret %= 7; + } + else if (y < 1752) + { + for (i = y; i < 1752; i++) + { + ret += (365 + test(i)) % 7; + } + ret += cal(1752, 9, 2) % 7; + ret -= cal(y, m, d) % 7; + ret = (9 - ret % 7) % 7; + } + else + { + if (m < 9 || m == 9 && d < 14) + { + ret = (9 - (cal(1752, 9, 2) - cal(y, m, d)) % 7) % 7; + } + else + { + ret = (cal(y, m, d) - cal(1752, 9, 14) + 3) % 7; + } + } + return ret; +} + +inline bool illegal(int y, int m, int d) +{ + if (m < 1 || m > 12 || d < 1) + { + return true; + } + if (m == 2 && + (!test(y) && d > 28 || d > 29)) + { + return true; + } + if (m != 2 && d > month[m - 1]) + { + return true; + } + if (y == 1752 && m == 9 && d > 2 && d < 14) + { + return true; + } + return false; +} + +int main() +{ + int y, m, d; + cin >> m >> d >> y; + while (y != 0 || m != 0 || d != 0) + { + if (illegal(y, m, d)) + { + printf("%d/%d/%d is an invalid date.\n", m, d, y); + } + else + { + printf("%s %d, %d is a %s\n", month_name[m - 1], d, y, day_name[day(y, m, d)]); + } + cin >> m >> d >> y; + } + return 0; +} diff --git a/603.cpp b/603.cpp new file mode 100644 index 0000000..e92060c --- /dev/null +++ b/603.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +/* +Problema: E Parking lot + +Equipo No. 3 + +Sebastían Arcila - Andrés Mejía +*/ + +struct carro +{ + int id, donde; + bool parked; + carro(int ID, int Donde, bool Parked) + { + id = ID; + donde = Donde; + parked = Parked; + } +}; + +int main() +{ + int casos; + //fstream cin("parking.in"); + cin >> casos; + //cout << "Lei casos: " << casos << endl; + bool primerCaso = true; + while (casos--) + { + vector carros; + if (!primerCaso) + { + cout << endl; + } + primerCaso = false; + int id; + while (cin >> id && id != 99) + { + //cout << "Lei id: " << id << endl; + carros.push_back(carro(id, id, false)); + } + /*cout << "Voy a imprimir carros:" << endl; + for (int i=0; i> salio; + //cout << "Lei salio: " << salio << endl; + int posClosest = 9999; + int minDistance = 9999; + for (int i = 0; i < carros.size(); ++i) + { + int d; + if (!carros[i].parked) + { + if (carros[i].donde <= salio) + { + d = salio - carros[i].donde; + } + else + { + d = (salio + 20) - carros[i].donde; + } + if (d < minDistance) + { + minDistance = d; + posClosest = i; + } + } + } + //cout << "minDistance es " << minDistance << endl; + //cout << "PosClosest es " << posClosest << endl; + if (posClosest == 9999) + { + continue; + } + carros[posClosest].parked = true; + carros[posClosest].donde = salio; + for (int i = 0; i < carros.size(); ++i) + { + if (!carros[i].parked) + { + int d = carros[i].donde + minDistance; + while (d > 20) + { + d -= 20; + } + carros[i].donde = d; + } + } + /*cout << "Voy a imprimir carros:" << endl; + for (int i=0; i + +using namespace std; + +#define MAXN 6 + +char V[] = "AEIOUY"; +char vf[30], B1[6][6], B2[6][6], com1[30], com2[30], Str[100], A[6], Fg[MAXN][MAXN], Word[100000][5]; +int ind, Max; +map M1, M2; + +int com(const void *a, const void *b) +{ + char *x = (char *)a; + char *y = (char *)b; + return strcmp(x, y); +} +void Ini() +{ + int i, u; + for (i = 0; V[i]; i++) + { + u = V[i] - 'A'; + vf[u] = 1; + } +} +void Sep(int n) +{ + int i, j = 0, u; + for (i = 0; Str[i]; i++) + { + if (isalpha(Str[i])) + { + B1[n][j++] = Str[i]; + u = Str[i] - 'A'; + com1[u] = 1; + } + if (j == 4) + break; + } + j = 0; + for (++i; Str[i]; i++) + { + if (isalpha(Str[i])) + { + B2[n][j++] = Str[i]; + u = Str[i] - 'A'; + com2[u] = 1; + } + if (j == 4) + return; + } +} +void recur1(int r, int c, int v, int level) +{ + int i, j, nr, nc, x; + A[level] = B1[r][c]; + if (v > 2) + return; + if (level >= 2 && v == 0) + return; + if (level == 3 && v == 2) + { + A[level + 1] = NULL; + if (M1[A] == true) + return; + M1[A] = true; + Max++; + return; + } + if (level >= 3) + return; + Fg[r][c] = 1; + for (i = -1; i <= 1; i++) + { + for (j = -1; j <= 1; j++) + { + if (i == 0 && j == 0) + continue; + nr = r + i; + nc = c + j; + if (nr < 0 || nr >= 4 || nc < 0 || nc > 4) + continue; + if (Fg[nr][nc]) + continue; + x = B1[nr][nc] - 'A'; + if (!com1[x] || !com2[x]) + continue; + x = vf[x]; + recur1(nr, nc, v + x, level + 1); + } + } + Fg[r][c] = 0; +} +void recur2(int r, int c, int v, int level) +{ + int i, j, nr, nc, x; + A[level] = B2[r][c]; + if (v > 2) + return; + if (level >= 2 && v == 0) + return; + if (level == 3 && v == 2) + { + A[level + 1] = NULL; + if (M1[A] == false) + return; + if (M2[A] == true) + return; + M2[A] = true; + strcpy(Word[ind++], A); + return; + } + if (level >= 3) + return; + Fg[r][c] = 1; + for (i = -1; i <= 1; i++) + { + for (j = -1; j <= 1; j++) + { + if (i == 0 && j == 0) + continue; + nr = r + i; + nc = c + j; + if (nr < 0 || nr >= 4 || nc < 0 || nc > 4) + continue; + if (Fg[nr][nc]) + continue; + x = B2[nr][nc] - 'A'; + if (!com1[x] || !com2[x]) + continue; + x = vf[x]; + recur2(nr, nc, v + x, level + 1); + } + } + Fg[r][c] = 0; +} +void Cal() +{ + int i, j, u; + ind = Max = 0; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + u = B1[i][j] - 'A'; + if (com1[u] && com2[u]) + recur1(i, j, vf[u], 0); + } + } + if (Max == 0) + { + printf("There are no common words for this pair of boggle boards.\n"); + return; + } + ind = 0; + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + u = B2[i][j] - 'A'; + if (com1[u] && com2[u]) + recur2(i, j, vf[u], 0); + } + } + if (ind == 0) + { + printf("There are no common words for this pair of boggle boards.\n"); + return; + } + qsort(Word, ind, sizeof(Word[0]), com); + for (i = 0; i < ind; i++) + printf("%s\n", Word[i]); +} +void Free() +{ + int i; + for (i = 0; i < 26; i++) + com1[i] = com2[i] = 0; + M1.clear(); + M2.clear(); +} + +int main() +{ + int i, f = 0; + char temp[100]; + Ini(); + while (gets(Str)) + { + if (!strcmp(Str, "#")) + break; + Sep(0); + for (i = 0; i < 3; i++) + { + gets(Str); + Sep(i + 1); + } + gets(temp); + if (f++) + printf("\n"); + Cal(); + Free(); + } + return 0; +} diff --git a/606.cpp b/606.cpp new file mode 100644 index 0000000..3f6d6f7 --- /dev/null +++ b/606.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +struct +{ + int zip, defined, cycle, m; + int a[128]; +} def[32]; + +int findcyc(int d) +{ + static int a[32]; + int x, n; + if (!def[d].defined || def[d].zip) + { + return 0; + } + memset(a, 0, sizeof(a)); + for (x = d, n = 0; def[x].defined && !def[x].zip && a[x] == 0;) + { + a[x] = 1; + n += def[x].m; + x = def[x].a[def[x].m]; + } + return ((x == d) ? n : 0); +} + +int eval(int d, int n) +{ + for (;;) + { + if (def[d].zip) + { + d = def[d].a[n & 1]; + n >>= 1; + continue; + } + if (def[d].cycle > 0) + { + n %= def[d].cycle; + } + if (n < def[d].m) + { + return def[d].a[n]; + } + n -= def[d].m; + d = def[d].a[def[d].m]; + } +} + +int main() +{ + static char s1[1024], s2[1024]; + int i, n, m, t, x, y; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &m) == 2;) + { + memset(def, 0, sizeof(def)); + while (n-- > 0 && scanf(" %s =", s1) == 1) + { + x = toupper(s1[0]) - 'A'; + def[x].defined = 1; + scanf(" %s", s1); + if (strcmp(s1, "zip") == 0) + { + def[x].zip = 1; + scanf(" %s %s", s1, s2); + def[x].a[0] = toupper(s1[0]) - 'A'; + def[x].a[1] = toupper(s2[0]) - 'A'; + continue; + } + def[x].zip = 0; + for (i = 0; !isalpha(s1[0]); i++) + { + sscanf(s1, "%d", &def[x].a[i]); + scanf(" %s", s1); + } + def[x].m = i; + def[x].a[i] = toupper(s1[0]) - 'A'; + } + for (i = 0; i < 26; i++) + if (def[i].defined) + { + def[i].cycle = findcyc(i); + } + while (m-- > 0 && scanf(" %s %d %d", s1, &x, &y) == 3) + { + n = toupper(s1[0]) - 'A'; + for (i = 0; (x + i) <= y; i++) + { + printf(i ? " %d" : "%d", eval(n, x + i)); + } + printf("\n"); + } + if (t > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/607.cpp b/607.cpp new file mode 100644 index 0000000..e2260e6 --- /dev/null +++ b/607.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +#define maxn 1002 +#define min(a, b) (a > b ? b : a) +#define INF 240100090 + +typedef long Big; + +Big L, N, C, top[maxn]; +Big D[maxn][maxn], Sum[maxn]; +char F[maxn][maxn]; + +Big GreedyS() +{ + Big i, sum = 0, minlec = 0, k; + for (i = 1; i <= N; i++) + { + k = sum + top[i]; + if (k < L) + sum = k; + else if (k == L) + { + sum = 0; + minlec++; + } + else + { + sum = top[i]; + minlec++; + } + } + if (sum > 0) + minlec++; + return minlec; +} +Big Disf(Big n) +{ + Big dif = L - n; + Big x = -C; + if (!dif) + return 0; + if (dif <= 10) + return x; + dif -= 10; + return dif * dif; +} +Big MinDis(Big ind, Big p) +{ + Big i, sum = 0, d1, d2, k; + if (F[p][ind] == 1) + return D[p][ind]; + F[p][ind] = 1; + if (ind == 1) + { + D[p][ind] = Disf(top[1]); + return D[p][ind]; + } + if (p == 1) + { + k = Sum[ind]; + if (k > L) + D[p][ind] = INF; + else + { + d1 = Disf(k); + D[p][ind] = d1; + } + return D[p][ind]; + } + for (i = ind; i >= p; i--) + { + k = sum + top[i]; + if (k <= L) + { + d1 = MinDis(i - 1, p - 1); + d2 = Disf(k); + sum = k; + D[p][ind] = min(D[p][ind], d1 + d2); + } + else + break; + } + return D[p][ind]; +} +Big ReadCase() +{ + Big i, sum = 0; + scanf("%ld", &N); + if (!N) + return 0; + scanf("%ld%ld", &L, &C); + for (i = 1; i <= N; i++) + { + scanf("%ld", &top[i]); + sum += top[i]; + Sum[i] = sum; + } + return 1; +} +void Ini(Big n) +{ + Big i, j; + for (i = 1; i <= n; i++) + { + for (j = 1; j <= N; j++) + { + D[i][j] = INF; + F[i][j] = 0; + } + } +} +void Cal() +{ + Big d; + if (Sum[N] <= L) + { + d = Disf(Sum[N]); + printf("Minimum number of lectures: 1\n"); + printf("Total dissatisfaction index: %ld\n", d); + return; + } + d = GreedyS(); + Ini(d); + printf("Minimum number of lectures: %ld\n", d); + d = MinDis(N, d); + printf("Total dissatisfaction index: %ld\n", d); +} + +int main() +{ + Big k, ks; + k = 1; + while (ReadCase()) + { + if (k > 1) + { + printf("\n"); + } + printf("Case %ld:\n", k++); + Cal(); + } + return 0; +} diff --git a/608.cpp b/608.cpp new file mode 100644 index 0000000..8768051 --- /dev/null +++ b/608.cpp @@ -0,0 +1,141 @@ +#include + +using namespace std; + +#define MAX(a, b) (a > b ? a : b) + +struct ss +{ + int ok; + int heavy; + int light; +} coin[15]; + +struct xx +{ + char aa[10]; + char bb[10]; + char cc[10]; +} waight[4]; + +void MarkHeavy(char str[]) +{ + int i, j; + for (i = 0; str[i]; i++) + { + j = str[i] - 'A'; + coin[j].heavy++; + } +} + +void MarkLight(char str[]) +{ + int i, j; + for (i = 0; str[i]; i++) + { + j = str[i] - 'A'; + coin[j].light++; + } +} + +void MarkEqual(char str[]) +{ + int i, j; + for (i = 0; str[i]; i++) + { + j = str[i] - 'A'; + coin[j].ok = 1; + } +} + +void RemainEqual(char str1[], char str2[]) +{ + int a[15] = {0}; + int i, j, k; + for (i = 0; str2[i]; i++) + { + j = str1[i] - 'A'; + k = str2[i] - 'A'; + a[j] = a[k] = 1; + } + for (i = 0; i < 12; i++) + if (a[i] == 0) + coin[i].ok = 1; +} + +void Cal() +{ + int i, rec, count = 0, rec2, max = 0, j; + for (i = 0; i < 12; i++) + { + if (coin[i].light && coin[i].heavy) + coin[i].ok = 1; + if (coin[i].ok == 0) + { + count++; + rec = i; + j = MAX(coin[i].heavy, coin[i].light); + if (j > max) + { + rec2 = i; + max = j; + } + } + } + if (count == 1) + { + if (coin[rec].light) + printf("%c is the counterfeit coin and it is light.\n", rec + 'A'); + else + printf("%c is the counterfeit coin and it is heavy.\n", rec + 'A'); + return; + } + if (coin[rec2].light) + printf("%c is the counterfeit coin and it is light.\n", rec2 + 'A'); + else + printf("%c is the counterfeit coin and it is heavy.\n", rec2 + 'A'); +} + +void Free() +{ + int i; + for (i = 0; i < 12; i++) + coin[i].heavy = coin[i].light = coin[i].ok = 0; +} + +int main() +{ + int kases, i; + char aa[7], bb[7], cc[7]; + scanf("%d", &kases); + while (kases--) + { + for (i = 0; i < 3; i++) + { + scanf("%s%s%s", aa, bb, cc); + strcpy(waight[i].aa, aa); + strcpy(waight[i].bb, bb); + strcpy(waight[i].cc, cc); + if (!strcmp(cc, "even")) + { + MarkEqual(aa); + MarkEqual(bb); + } + else if (!strcmp(cc, "up")) + { + MarkHeavy(aa); + MarkLight(bb); + RemainEqual(aa, bb); + } + else + { + MarkLight(aa); + MarkHeavy(bb); + RemainEqual(aa, bb); + } + } + Cal(); + Free(); + } + return 0; +} diff --git a/609.cpp b/609.cpp new file mode 100644 index 0000000..0747457 --- /dev/null +++ b/609.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +struct Line +{ + int a, b, c; + Line() {} + Line(int x1, int y1, int x2, int y2) + { + a = y2 - y1; + b = x1 - x2; + c = a * x1 + b * y1; + } +}; + +int X[20], Y[20], N, order[20], height, width, nlines; +Line lines[20]; +double best; + +void checkOrder() +{ + double res = 0; + nlines = 4; + for (int i = 0; i < N; i++) + { + Line l(X[order[i]], Y[order[i]], X[order[i] + 1], Y[order[i] + 1]); + double mid_x = (X[order[i]] + X[order[i] + 1]) / 2.0; + double mid_y = (Y[order[i]] + Y[order[i] + 1]) / 2.0; + double d1 = 1e20, d2 = 1e20; + for (int j = 0; j < nlines; j++) + { + const Line &m = lines[j]; + int det = l.a * m.b - l.b * m.a; + if (det == 0) + { + continue; + } + double x = (l.c * m.b - l.b * m.c) / (double)det; + double y = (l.a * m.c - l.c * m.a) / (double)det; + double dist = sqrt((x - mid_x) * (x - mid_x) + (y - mid_y) * (y - mid_y)); + if (x < mid_x - 1e-9 || (x < mid_x + 1e-9 && y < mid_y)) + { + d1 = min(d1, dist); + } + else + { + d2 = min(d2, dist); + } + } + res += d1 + d2; + if (res > best) + { + return; + } + lines[nlines] = l; + nlines++; + } + if (res < best) + { + best = res; + } +} + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T && scanf("%d %d %d", &width, &height, &N) == 3; cs++) + { + if (width <= 0 || height <= 0) + { + break; + } + assert(3 <= N && N <= 8); + for (int i = N - 1; i >= 0; i--) + { + scanf("%d %d", &X[i], &Y[i]); + } + X[N] = X[0]; + Y[N] = Y[0]; + lines[0] = Line(0, 0, width, 0); + lines[1] = Line(width, 0, width, height); + lines[2] = Line(width, height, 0, height); + lines[3] = Line(0, height, 0, 0); + for (int i = 0; i < N; i++) + { + order[i] = i; + } + best = 1e20; + do + { + checkOrder(); + } while (next_permutation(order, order + N)); + if (cs != 1) + { + printf("\n"); + } + printf("Minimum total length = %.3f\n", best); + } + return 0; +} diff --git a/610.cpp b/610.cpp new file mode 100644 index 0000000..5eb5afa --- /dev/null +++ b/610.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < (int)n; i++) +#define rep(i, n) REP(i, 0, n) +#define FOR(it, o) for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it) + +const int V = 1000; +int n; +bool edge[V][V], preUsed[V][V], used[V][V]; +int preNum[V], low[V], rootNode, defultID[V]; +vector> Edge; + +int dfs(int nowNode, int &nowNum) +{ + low[nowNode] = preNum[nowNode] = nowNum; + defultID[nowNum] = nowNode; + nowNum++; + rep(i, n) + { + if (!edge[nowNode][i]) + { + continue; + } + if (preNum[i] >= 0) + { + if (preUsed[i][nowNode]) + { + continue; + } + low[nowNode] = min(low[nowNode], preNum[i]); + if (used[nowNode][i]) + { + continue; + } + Edge.push_back(make_pair(nowNode, i)); + used[nowNode][i] = used[i][nowNode] = true; + } + else + { + preUsed[nowNode][i] = true; + Edge.push_back(make_pair(nowNode, i)); + used[nowNode][i] = used[i][nowNode] = true; + low[nowNode] = min(dfs(i, nowNum), low[nowNode]); + } + } + return low[nowNode]; +} +void getBridge() +{ + rep(i, n) + { + rep(j, n) + { + if (!preUsed[i][j]) + { + continue; + } + if (low[j] == preNum[j]) + { + Edge.push_back(make_pair(defultID[max(preNum[i], preNum[j])], defultID[min(preNum[i], preNum[j])])); + } + } + } +} +void setGraph() +{ + rootNode = 0; + rep(i, n) + { + preNum[i] = -1; + rep(j, n) + { + used[i][j] = false; + preUsed[i][j] = false; + } + } + int id = 0; + dfs(rootNode, id); +} + +int main() +{ + int m; + int tc = 0; + while (cin >> n >> m) + { + tc++; + if (n == 0 && m == 0) + { + break; + } + cout << tc << endl + << endl; + rep(i, n) rep(j, n) edge[i][j] = false; + rep(i, m) + { + int from, to; + cin >> from >> to; + from--; + to--; + edge[from][to] = edge[to][from] = true; + } + Edge.clear(); + setGraph(); + getBridge(); + FOR(it, Edge) + { + cout << it->first + 1 << ' ' << it->second + 1 << endl; + } + cout << "#" << endl; + } + return 0; +} diff --git a/612.cpp b/612.cpp index b1ad930..4f08536 100644 --- a/612.cpp +++ b/612.cpp @@ -1,41 +1,78 @@ -#include -#include - -using namespace std; - -struct DNA{ - string str; - int ran; -}; - -bool comp(DNA A, DNA B){ - return (A.ran < B.ran); -} - -int main(){ - int test; - cin>>test; - while(test--){ - int M, N, i, j, k; - cin>>N>>M; - DNA dna[M]; - for(i=0; i>dna[i].str; - int r=0; - for(j=0; j dna[i].str[k]){ - r++; - } - } - } - dna[i].ran = r; - } - stable_sort(dna, dna+M, comp); - for(i=0; i 0) - cout< + +using namespace std; + +class DNA +{ +public: + char string[60]; + int s; +}; + +int main() +{ + int solve(char *p, int x); + void paixu(DNA * p, int *q, int x); + DNA dna[100]; + int n, m; + int sorted[100]; + int i, cnt; + cin >> cnt; + for (i = 0; i < cnt; i++) + { + cin >> n >> m; + int r; + for (r = 0; r < m; r++) + { + cin >> dna[r].string; + } + for (r = 0; r < m; r++) + { + dna[r].s = solve(dna[r].string, n); + } + paixu(dna, sorted, m); + for (r = 0; r < m; r++) + { + cout << dna[sorted[r]].string << endl; + } + if (i != cnt - 1) + { + cout << endl; + } + } + return 0; +} + +int solve(char *p, int x) +{ + int i, j; + int cnt = 0; + for (i = 0; i < x - 1; i++) + for (j = i + 1; j < x; j++) + if (p[i] > p[j]) + { + cnt++; + } + return cnt; +} + +void paixu(DNA *p, int *q, int x) +{ + int i, j; + int row; + int min; + for (i = 0; i < x; i++) + { + min = 1000000; + for (j = 0; j < x; j++) + { + if (p[j].s < min) + { + min = p[j].s; + row = j; + } + } + q[i] = row; + p[row].s = 1000000; + } +} diff --git a/613.cpp b/613.cpp new file mode 100644 index 0000000..eb11304 --- /dev/null +++ b/613.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +/* Copyright Edmund Dengler (ACM ICPC ECNA 98) */ +int main() +{ + int num; + int j, k, n; + int done; + char buf[60][100], str[80]; + int cnt[10]; + while (gets(buf[0]) != NULL) + { + if (strcmp(buf[0], "-1") == 0) + { + break; + } + done = 0; + j = 0; + while ((done == 0) && (j < 15)) + { + int i; + int len; + char tbuf[10]; + for (i = 0; i < 10; i += 1) + { + cnt[i] = 0; + } + len = strlen(buf[j]); + for (i = 0; i < len; i += 1) + { + cnt[buf[j][i] - '0'] += 1; + } + buf[j + 1][0] = '\0'; + for (i = 0; i < 10; i += 1) + { + if (cnt[i] > 0) + { + sprintf(tbuf, "%d%d", cnt[i], i); + strcat(buf[j + 1], tbuf); + } + } + for (k = 0; k < j + 1; k += 1) + { + if (strcmp(buf[k], buf[j + 1]) == 0) + { + done = 1; + printf("%s ", buf[0]); + if (j == 0) + { + printf("is self-inventorying\n"); + } + else if (k == j) + { + printf("is self-inventorying after %d steps\n", j); + } + else + { + printf("enters an inventory loop of length %d\n", j + 1 - k); + } + break; + } + } + j += 1; + } + if (!done) + { + printf("%s can not be classified after 15 iterations\n", buf[0]); + } + } + return 0; +} diff --git a/614.cpp b/614.cpp new file mode 100644 index 0000000..2604beb --- /dev/null +++ b/614.cpp @@ -0,0 +1,147 @@ +#include + +using namespace std; + +const int dx[] = {0, -1, 0, 1}; +const int dy[] = {-1, 0, 1, 0}; + +int hight; +int width; +int maze[12][12]; +int used[12][12]; + +bool valid(int x, int y) +{ + return x >= 0 && x < hight && y >= 0 && y < width; +} + +bool hasSouthWall(int x, int y) +{ + return !valid(x, y) || maze[x][y] == 2 || maze[x][y] == 3; +} + +bool hasEastWall(int x, int y) +{ + return !valid(x, y) || maze[x][y] == 1 || maze[x][y] == 3; +} + +bool canMove(int x, int y, int direction) +{ + if (direction == 0) + { + return !hasEastWall(x + dx[direction], y + dy[direction]); + } + else if (direction == 1) + { + return !hasSouthWall(x + dx[direction], y + dy[direction]); + } + else if (direction == 2) + { + return (maze[x][y] == 2 || maze[x][y] == 0) && (y < width - 1); + } + else if (direction == 3) + { + return (maze[x][y] == 1 || maze[x][y] == 0) && (x < hight - 1); + } + return true; +} + +bool findPath(int x, int y, int depth, int fx, int fy) +{ + if (x == fx && y == fy) + { + used[x][y] = depth; + return true; + } + used[x][y] = 0; + for (int direction = 0; direction < 4; ++direction) + { + if (canMove(x, y, direction)) + { + int nx = x + dx[direction]; + int ny = y + dy[direction]; + if (used[nx][ny] == -1 && findPath(nx, ny, depth + 1, fx, fy)) + { + used[x][y] = depth; + return true; + } + } + } + return false; +} + +int main() +{ + int cas = 1; + while (true) + { + int sx, sy; + int fx, fy; + scanf("%d%d%d%d%d%d", &hight, &width, &sx, &sy, &fx, &fy); + --sx, --sy, --fx, --fy; + if (hight == 0 && width == 0) + { + break; + } + memset(maze, 0, sizeof(maze)); + for (int i = 0; i < hight; ++i) + { + for (int j = 0; j < width; ++j) + { + scanf("%d", &maze[i][j]); + } + } + memset(used, -1, sizeof(used)); + findPath(sx, sy, 1, fx, fy); + printf("Maze %d\n\n", cas++); + for (int i = 0; i < width; ++i) + { + printf("+---"); + } + printf("+\n"); + for (int i = 0; i < hight; ++i) + { + printf("|"); + for (int j = 0; j < width; ++j) + { + if (used[i][j] == 0) + { + printf("???"); + } + else if (used[i][j] > 0) + { + printf("%3d", used[i][j]); + } + else + { + printf(" "); + } + if (hasEastWall(i, j) || j == width - 1) + { + printf("|"); + } + else + { + printf(" "); + } + } + printf("\n"); + printf("+"); + for (int j = 0; j < width; ++j) + { + if (hasSouthWall(i, j) || i == hight - 1) + { + printf("---"); + } + else + { + printf(" "); + } + printf("+"); + } + printf("\n"); + } + printf("\n\n"); + } + return 0; +} diff --git a/615.cpp b/615.cpp new file mode 100644 index 0000000..2bc3dff --- /dev/null +++ b/615.cpp @@ -0,0 +1,201 @@ +#include + +using namespace std; + +int ST[950], Store[100], PAIR2[440], K, Kase, N; +long PAIR1[440]; +char FL[310][31]; +struct Vertex +{ + char cl; +} V[320]; + +int com(const void *a, const void *s) +{ + return *(int *)a - *(int *)s; +} + +// binary search +int B_search(int key) +{ + int up = K - 1, low = 0, mid; + mid = (up + low) / 2; + while (low <= up && Store[mid] != key) + { + if (key > Store[mid]) + { + low = mid + 1; + } + else if (key < Store[mid]) + { + up = mid - 1; + } + mid = (up + low) / 2; + } + if (Store[mid] == key) + { + return mid; + } + return -1; +} +void MAKELINK(int k, int N) +{ + int i, n, m; + K = 0; + qsort(ST, k, sizeof(int), com); + for (i = 1; i < k; i++) + if (ST[i] == ST[i - 1]) + { + ST[i - 1] = -1; + } + qsort(ST, k, sizeof(int), com); + for (i = 0; i < k; i++) + if (ST[i] != -1) + { + Store[K++] = ST[i]; + } + for (i = 0; i < N; i++) + { + m = B_search(PAIR1[i]); + n = B_search(PAIR2[i]); + FL[m][n] = 1; + } +} +void DFS(int n) +{ + V[n].cl = 'B'; + for (int i = 0; i < K; i++) + { + if ((FL[n][i] || FL[i][n]) && V[i].cl == 'W') + { + DFS(i); + } + } +} +int Un() +{ + int i, g = 0; + for (i = 0; i < K; i++) + if (V[i].cl == 'W') + { + g++; + } + return g; +} +int Root() +{ + int i, j, k = 0; + for (i = 0; i < K; i++) + { + for (j = 0; j < K; j++) + { + if (FL[j][i]) + { + k++; + break; + } + } + } + if (K - 1 != k) + { + return 1; + } + return 0; +} +void Com() +{ + int i; + if (K != N + 1) + { + printf("Case %d is not a tree.\n", Kase++); + return; + } + if (Root()) + { + printf("Case %d is not a tree.\n", Kase++); + return; + } + for (i = 0; i < K; i++) + { + V[i].cl = 'W'; + } + DFS(0); + if (Un()) + { + printf("Case %d is not a tree.\n", Kase++); + return; + } + printf("Case %d is a tree.\n", Kase++); +} + +int main() +{ + int i, m, n, k, l, g; + Kase = 1; + while (1) + { + l = 0; + g = 0; + scanf("%d%d", &m, &n); + if (m < 0 && n < 0) + { + break; + } + if (n == 0 && m == 0) + { + goto done; + } + if (n == m) + { + g = 1; + } + N = 1; + k = 0; + i = 1; + ST[k] = m; + PAIR1[0] = ST[k]; + k++; + ST[k] = n; + PAIR2[0] = ST[k]; + k++; + while (1) + { + scanf("%d%d", &m, &n); + if (n == 0 && m == 0) + { + break; + } + ST[k] = m; + if (n == m) + { + g = 1; + } + PAIR1[i] = ST[k]; + k++; + ST[k] = n; + PAIR2[i] = ST[k]; + k++; + N++; + i++; + l = 1; + } + done:; + if (g == 1) + { + printf("Case %d is not a tree.\n", Kase++); + continue; + } + if (l == 0 || g == 1) + { + printf("Case %d is a tree.\n", Kase++); + continue; + } + MAKELINK(k, N); + Com(); + for (i = 0; i < 32; i++) + { + memset(FL[i], 0, 320 * sizeof(char)); + } + } + return 0; +} diff --git a/616.cpp b/616.cpp new file mode 100644 index 0000000..fb28a61 --- /dev/null +++ b/616.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +int main() +{ + int n; + int result; + bool ok; + while (cin >> n) + { + if (n < 0) + { + break; + } + result = -1; + for (int i = sqrt(n) + 1; i >= 2; i--) + { + int tmp = n; + ok = true; + for (int j = 0; j < i; j++) + { + if (tmp % i != 1) + { + ok = false; + break; + } + tmp -= tmp / i;// hidden + tmp--; // goes to monkey + } + if (!ok) + { + continue; + } + if (tmp % i == 0) + { + result = i; + break; + } + } + cout << n << " coconuts, "; + if (result == -1) + { + cout << "no solution" << endl; + } + else + { + cout << result << " people and 1 monkey" << endl; + } + } + return 0; +} diff --git a/617.cpp b/617.cpp new file mode 100644 index 0000000..059c071 --- /dev/null +++ b/617.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +#define abs(a) (a < 0 ? -a : a) + +struct Signal +{ + float L; + int c[3]; +} s[10]; +bool operator<(const Signal &a, const Signal &b) +{ + return a.L < b.L; +} +int N, top, v, t = 0; +int goodSpeeds[50]; +int goodSpeed(int v) +{ + float lastPos = 0; + float clock = 0; + for (int sig = 0; sig < N; sig++) + { + float distance = s[sig].L - lastPos; + lastPos += distance; + clock += distance / float(v) * 60 * 60; + float sum = s[sig].c[0] + s[sig].c[1] + s[sig].c[2]; + double remaining = fmod(clock, sum); + if (remaining > s[sig].c[0] + s[sig].c[1]) + { + return 0; + } + } + return true; +} +int main() +{ + while (scanf("%d", &N) && N != -1) + { + for (int i = 0; i < N; ++i) + { + scanf("%f %d %d %d", &(s[i].L), &(s[i].c[0]), &(s[i].c[1]), &(s[i].c[2])); + } + sort(s, s + N); + for (top = 0, v = 30; v < 61; v++) + if (goodSpeed(v)) + { + goodSpeeds[top++] = v; + } + vector seq; + int comma = 0, count = 0; + printf("Case %d:", ++t); + if (top == 0) + { + printf(" No acceptable speeds."); + } + else + for (int i = 0; i < top; i++) + { + if (!seq.empty() && seq.back() + 1 != goodSpeeds[i]) + { + count++; + if (seq.size() == 1) + { + printf(" %d", seq[0]); + } + else + { + printf(" %d-%d", seq.front(), seq.back()); + } + if (i != top - 1) + { + printf(","); + } + comma = (i != top - 1); + seq.clear(); + } + seq.push_back(goodSpeeds[i]); + } + if (seq.size()) + { + if (!comma && count) + { + printf(","); + } + if (seq.size() == 1) + { + printf(" %d", seq[0]); + } + else + { + printf(" %d-%d", seq.front(), seq.back()); + } + } + printf("\n"); + } + return 0; +} diff --git a/618.cpp b/618.cpp new file mode 100644 index 0000000..37b735e --- /dev/null +++ b/618.cpp @@ -0,0 +1,130 @@ +#include + +using namespace std; + +int used[4]; +int px[4], py[4]; + +int getgcd(int p, int q) +{ + if (p % q) + { + return getgcd(q, p % q); + } + return q; +} + +int dfs(int depth, int sx, int sy) +{ + int i, x, y, stepx, stepy; + if (sx < 0 || sy < 0) + { + return 0; + } + if (!sx || !sy) + { + return depth == 4; + } + for (i = 1; i < 4; i++) + { + if (!used[i]) + { + used[i] = 1; + if (!((px[i] * sy) % py[i])) + { + if (dfs(depth + 1, sx - (px[i] * sy) / py[i], sy)) + { + return 1; + } + } + if (!((py[i] * sx) % px[i])) + { + if (dfs(depth + 1, sx, sy - (py[i] * sx) / px[i])) + { + return 1; + } + } + used[i] = 0; + } + } + if (!used[0]) + { + used[0] = 1; + i = getgcd(px[0], py[0]); + x = px[0] /= i; + y = py[0] /= i; + for (stepx = x, stepy = y; x <= sx && y <= sy; x += stepx, y += stepy) + { + if (x == sx) + { + if (dfs(depth + 1, sx, sy - y)) + { + return 1; + } + } + else if (y == sy) + { + if (dfs(depth + 1, sx - x, sy)) + { + return 1; + } + } + else + { + for (i = 1; i < 4; i++) + { + if (!used[i]) + { + if (!((py[i] * x) % px[i]) && (py[i] * x) / px[i] == sy - y) + { + used[i] = 1; + if (dfs(depth + 2, sx - x, sy)) + { + return 1; + } + used[i] = 0; + } + if ((px[i] * y) % py[i] && (px[i] * y) / py[i] == sx - x) + { + used[i] = 1; + if (dfs(depth + 2, sx, sy - y)) + { + return 1; + } + used[i] = 0; + } + } + } + } + } + used[0] = 0; + } + return 0; +} + +int main() +{ + int cas, i, sx, sy; + cas = 0; + while (scanf("%d%d", &sx, &sy) == 2) + { + if (!sx && !sy) + { + break; + } + for (i = 0; i < 4; i++) + { + scanf("%d%d", &px[i], &py[i]); + } + memset(used, 0, sizeof(used)); + if (dfs(0, sx, sy)) + { + printf("Set %d: Yes\n", ++cas); + } + else + { + printf("Set %d: No\n", ++cas); + } + } + return 0; +} diff --git a/619.cpp b/619.cpp new file mode 100644 index 0000000..ed3d65b --- /dev/null +++ b/619.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +int N[20], T[20]; +char in[25], out[25]; +int main() +{ + int i, j, len, tl; + while (scanf("%s", in) == 1) + { + if (in[0] == '*') + { + break; + } + for (i = 0; i < 20; i++) + { + N[i] = 0; + } + len = 1; + if (in[0] > '9') + { + for (i = 0; in[i]; i++) + { + for (j = 0; j < len; j++) + { + N[j] *= 26; + } + N[0] += in[i] - '`'; + for (j = 0; j < len; j++) + { + N[j + 1] += N[j] / 1000; + N[j] %= 1000; + } + if (N[len]) + { + len++; + } + } + for (; i < 22; i++) + { + in[i] = ' '; + } + in[22] = '\0'; + } + else + { + for (i = 0; in[i]; i++) + { + for (j = 0; j < len; j++) + { + N[j] *= 10; + } + N[0] += in[i] - '0'; + for (j = 0; j < len; j++) + { + N[j + 1] += N[j] / 1000; + N[j] %= 1000; + } + if (N[len]) + { + len++; + } + } + for (i = 0, tl = len; i < tl; i++) + { + T[i] = N[i]; + } + for (i = 0; tl > 1 || T[0]; i++) + { + for (j = len - 1; j > 0; j--) + { + T[j - 1] += T[j] % 26 * 1000; + T[j] /= 26; + } + j = T[0] % 26; + T[0] /= 26; + if (!j) + { + out[i] = 'z'; + for (T[j = 0]--; T[j] < 0; j++) + { + T[j + 1]--; + T[j] += 1000; + } + } + else + { + out[i] = '`' + j; + } + if (!T[tl - 1]) + { + tl--; + } + } + for (j = 0; j < i; j++) + { + in[j] = out[i - j - 1]; + } + for (; j < 22; j++) + { + in[j] = ' '; + } + in[22] = '\0'; + } + printf("%s%d", in, N[len - 1]); + for (i = len - 2; i >= 0; i--) + { + printf(",%03d", N[i]); + } + printf("\n"); + } + return 0; +} diff --git a/620.cpp b/620.cpp new file mode 100644 index 0000000..96b4ba7 --- /dev/null +++ b/620.cpp @@ -0,0 +1,120 @@ +#include + +using namespace std; + +int recur(char ss[], int n) +{ + int i, l, k; + char temp[2000]; + l = strlen(ss); + if (n == 1) + { + if (ss[0] == 'A') + return 1; + return 0; + } + else if (n == 2) + { + if (l == 3) + { + temp[0] = ss[0]; + temp[1] = NULL; + return recur(temp, 1); + } + else + { + if (ss[0] == 'B' && ss[l - 3] == 'A') + { + for (i = 0; i < l - 2; i++) + temp[i] = ss[i]; + temp[i] = NULL; + return recur(temp, 3); + } + else if (ss[l - 4] == 'A' && ss[l - 3] == 'B') + { + for (i = 0; i < l - 2; i++) + temp[i] = ss[i]; + temp[i] = NULL; + return recur(temp, 2); + } + else + return 0; + } + } + else if (n == 3) + { + if (l == 3) + { + temp[0] = ss[1]; + temp[1] = NULL; + return recur(temp, 1); + } + else if (ss[1] == 'B' && ss[l - 2] == 'A') + { + k = 0; + for (i = 1; i < l - 1; i++) + temp[k++] = ss[i]; + temp[k] = NULL; + return recur(temp, 3); + } + else if (ss[l - 2] == 'B' && ss[l - 3] == 'A') + { + k = 0; + for (i = 1; i < l - 1; i++) + temp[k++] = ss[i]; + temp[k] = NULL; + return recur(temp, 2); + } + else + return 0; + } + return 0; +} +void Cal(char ss[]) +{ + int l; + l = strlen(ss); + if (l % 2 == 0) + { + printf("MUTANT\n"); + return; + } + if (l == 1) + { + if (ss[0] == 'A') + printf("SIMPLE\n"); + else + printf("MUTANT\n"); + return; + } + if (ss[0] == 'B' && ss[l - 1] == 'A') + { + if (recur(ss, 3)) + printf("MUTAGENIC\n"); + else + printf("MUTANT\n"); + return; + } + if (ss[l - 1] == 'B' && ss[l - 2] == 'A') + { + if (recur(ss, 2)) + printf("FULLY-GROWN\n"); + else + printf("MUTANT\n"); + return; + } + printf("MUTANT\n"); +} + +int main() +{ + int k; + char ss[2000]; + scanf("%d", &k); + while (k--) + { + scanf("%s", ss); + Cal(ss); + } + return 0; +} diff --git a/621.cpp b/621.cpp index 57d8017..0c4b86e 100644 --- a/621.cpp +++ b/621.cpp @@ -1,65 +1,38 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N; - cin >> N; - while (N--) { - string S; - cin >> S; - if (S == "1" || S == "4" || S == "78") { - cout << "+\n"; - continue; - } - int L = S.length() - 1; - if (S[L - 1] == '3' && S[L] == '5') { - cout << "-\n"; - continue; - } - if (S[0] == '9' && S[L] == '4') { - cout << "*\n"; - continue; - } - if (S[0] == '1' && S[1] == '9' && S[2] == '0') - cout << "?\n"; - } - //system("pause"); - return 0; -} +#include + +using namespace std; + +char in[10000]; +int main() +{ + int t, len; + scanf("%d", &t); + while (t--) + { + scanf("%s", in); + if (strcmp(in, "1") == 0 || strcmp(in, "4") == 0 || strcmp(in, "78") == 0) + { + printf("+\n"); + continue; + } + if ((len = strlen(in)) < 2) + { + continue; + } + if (in[len - 2] == '3' && in[len - 1] == '5') + { + printf("-\n"); + continue; + } + if (in[0] == '9' && in[len - 1] == '4') + { + printf("*\n"); + continue; + } + if (strncmp(in, "190", 3) == 0) + { + printf("?\n"); + } + } + return 0; +} diff --git a/622.cpp b/622.cpp new file mode 100644 index 0000000..242a745 --- /dev/null +++ b/622.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +static char line[8192], *tokp; +static int token; +static long long tokval; + +static int next() +{ + if (*tokp == '+' || *tokp == '*' || *tokp == '(' || *tokp == ')') + { + return (token = *tokp++); + } + if (*tokp >= '0' && *tokp <= '9') + { + for (tokval = 0; *tokp >= '0' && *tokp <= '9'; tokp++) + { + tokval = tokval * 10 + *tokp - '0'; + } + return (token = 'n'); + } + return (token = 0); +} + +static int expr(long long *r); + +static int factor(long long *r) +{ + if (token == 'n') + { + (*r) = tokval; + next(); + return 0; + } + else if (token == '(') + { + next(); + expr(r); + if (token != ')') + { + return 1; + } + next(); + return 0; + } + else + { + return 1; + } +} + +static int term(long long *r) +{ + long long t; + if (factor(r)) + { + return 1; + } + while (token == '*') + { + next(); + if (factor(&t)) + { + return 1; + } + (*r) *= t; + } + return 0; +} + +static int expr(long long *r) +{ + long long t; + if (term(r)) + { + return 1; + } + while (token == '+') + { + next(); + if (term(&t)) + { + return 1; + } + (*r) += t; + } + return 0; +} + +int main() +{ + int t; + long long r; + for (scanf("%d\n", &t); t-- > 0 && gets(line);) + { + tokp = line; + next(); + if (expr(&r) == 0 && *tokp == '\0') + { + printf("%lld\n", r); + } + else + { + printf("ERROR\n"); + } + } + return 0; +} diff --git a/623.cpp b/623.cpp new file mode 100644 index 0000000..478baa4 --- /dev/null +++ b/623.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +/** +623 +**/ +#define MAXDIGITS 2569 +char FAC[1001][MAXDIGITS]; +#define PLUS 1 +#define MINUS -1 +#define max(a, b) ((a > b) ? a : b) +typedef struct +{ + char digits[MAXDIGITS]; + int signbit; + int lastdigit; +} bignum; +void print_bignum(bignum *n); +void int_to_bignum(int s, bignum *n); +int abs(int a) +{ + return a < 0 ? -a : a; +} +void int_to_bignum(int s, bignum *n) +{ + if (s >= 0) + { + n->signbit = PLUS; + } + else + { + n->signbit = MINUS; + } + int t = abs(s); + sprintf(n->digits, "%d", t); + n->lastdigit = strlen(n->digits); +} +void multiply_bignum(bignum *a, bignum *b, bignum *c) +{ + long int n_d; + register long int i, j, k; + short int num1[MAXDIGITS], num2[MAXDIGITS], of = 0, res[MAXDIGITS] = {0}; + n_d = (a->lastdigit < b->lastdigit) ? b->lastdigit : a->lastdigit; + n_d++; + for (i = 0, j = a->lastdigit - 1; i < a->lastdigit; i++, j--) + { + num1[i] = a->digits[j] - 48; + } + for (i = 0, j = b->lastdigit - 1; i < b->lastdigit; j--, i++) + { + num2[i] = b->digits[j] - 48; + } + res[0] = 0; + for (j = 0; j < b->lastdigit; j++) + { + for (i = 0, k = j; i < a->lastdigit || of; k++, i++) + { + if (i < a->lastdigit) + { + res[k] += num1[i] * num2[j] + of; + } + else + { + res[k] += of; + } + of = res[k] / 10; + res[k] = res[k] % 10; + } + } + for (i = k - 1, j = 0; i >= 0; i--, j++) + { + c->digits[j] = res[i] + 48; + } + c->digits[j] = '\0'; + c->lastdigit = k; + c->signbit = a->signbit * b->signbit; +} +void copy(bignum *a, bignum *b) +{ + a->lastdigit = b->lastdigit; + a->signbit = b->signbit; + strcpy(a->digits, b->digits); +} +int main() +{ + int a; + bignum n1, n2, n3; + int_to_bignum(1, &n2); + for (a = 1; a <= 1000; a++) + { + int_to_bignum(a, &n1); + multiply_bignum(&n1, &n2, &n3); + strcpy(FAC[a], n3.digits); + copy(&n2, &n3); + } + while (scanf("%d", &a) == 1) + { + printf("%d!\n", a); + if (!a) + { + printf("1\n"); + continue; + } + printf("%s\n", FAC[a]); + } + return 0; +} diff --git a/624.cpp b/624.cpp new file mode 100644 index 0000000..7b5c189 --- /dev/null +++ b/624.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +/** + * Dynamic Programming, 0/1 Knapsack + */ + +enum +{ + MAX_TRACK = 20, + MAX_LEN = 10005 +}; + +int c[MAX_LEN]; +bool p[MAX_TRACK][MAX_LEN]; + +void solve_knapsack(int tape_len, int *track, int track_num) +{ + memset(c, 0, sizeof(c)); + memset(p, 0, sizeof(p)); + for (int i = track_num - 1; i >= 0; --i)// for all tracks + { + for (int j = tape_len; j >= track[i]; --j)// 1-d array: bug at j>track[i] + { + p[i][j] = (c[j - track[i]] + track[i] > c[j]); + c[j] = max(c[j - track[i]] + track[i], c[j]); + } + } +} + +void print_tracks(int tape_len, int *track, int track_num) +{ + int k = tape_len; + for (int i = 0; i < track_num; ++i) + { + if (p[i][k]) + { + printf("%d ", track[i]); + k = k - track[i]; + } + } + printf("sum:%d\n", c[tape_len]); +} + +int main() +{ + int tape_len; + while (scanf("%d", &tape_len) == 1) + { + int track_num, track[MAX_TRACK]; + scanf("%d", &track_num); + for (int i = 0; i < track_num; ++i) + { + scanf("%d", &track[i]); + } + solve_knapsack(tape_len, track, track_num); + print_tracks(tape_len, track, track_num); + } + return 0; +} diff --git a/625.cpp b/625.cpp new file mode 100644 index 0000000..dec4608 --- /dev/null +++ b/625.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +char line[2048]; + +string init_word[] = + { + "var", "end", "for", "then", "else", "case", "goto", "const", + "label", "while", "begin", "until", "repeat", "downto", "function", + "procedure"}; + +map src; + +int main() +{ + int n_case = 0; + gets(line); + sscanf(line, "%d", &n_case); + while (--n_case >= 0) + { + gets(line);//blank.. + src.clear(); + for (int m = 0; m < 16; m++) + { + src[init_word[m]] = m; + } + bool canExit = false; + do + { + gets(line); + if (strcmp(line, "end.") == 0) + { + canExit = true; + } + for (int p = 0; line[p] != 0;) + { + if (!isalpha(line[p]) && !isdigit(line[p])) + { + putchar(line[p]); + p++; + continue; + } + int q = p + 1; + while (isalpha(line[q]) || isdigit(line[q])) + { + q++; + } + if (q - p < 3) + { + putchar(line[p]); + if (q - p == 2) + { + putchar(line[p + 1]); + } + p = q; + continue; + } + char save = line[q]; + line[q] = 0; + string tmp(line + p); + map::iterator f = src.find(tmp); + if (f == src.end())//not found.. + { + printf(line + p); + int no = src.size(); + src[tmp] = no; + } + else + { + printf("&%d", (*f).second); + } + line[q] = save; + p = q; + } + putchar('\n'); + } while (!canExit); + if (n_case) + { + printf("\n"); + } + } + return 0; +} diff --git a/626.cpp b/626.cpp new file mode 100644 index 0000000..3991307 --- /dev/null +++ b/626.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + +/** +626 +**/ +#define MAXN 102 +int N, total; +char Link[MAXN][MAXN]; +struct ss +{ + int parent; + char colour; +} V[MAXN]; +void Print(int n, int key) +{ + if (key == n) + { + printf("%d", n); + return; + } + Print(V[n].parent, key); + printf(" %d", n); +} +void DFS_A(int n, int pre, int level, int key) +{ + int i; + if (n <= pre) + { + return; + } + if (level == 2) + { + if (Link[n][key]) + { + Print(n, key); + putchar('\n'); + total++; + } + return; + } + V[n].colour = 'r'; + for (i = 1; i <= N; i++) + { + if (Link[n][i] && V[i].colour == 'w') + { + V[i].parent = n; + DFS_A(i, n, level + 1, key); + } + } + V[n].colour = 'w'; +} +void DFS_D(int n, int pre, int level, int key) +{ + int i; + if (n >= pre) + { + return; + } + if (level == 2) + { + if (Link[n][key]) + { + Print(n, key); + putchar('\n'); + total++; + } + return; + } + V[n].colour = 'r'; + for (i = 1; i <= N; i++) + { + if (Link[n][i] && V[i].colour == 'w') + { + V[i].parent = n; + DFS_D(i, n, level + 1, key); + } + } + V[n].colour = 'w'; +} +void Cal() +{ + int i; + total = 0; + for (i = 1; i <= N; i++) + { + V[i].colour = 'w'; + } + for (i = 1; i <= N; i++) + { + if (i >= 3) + { + DFS_D(i, 200, 0, i); + } + if (N - i >= 2) + { + DFS_A(i, 0, 0, i); + } + } + printf("total:%d\n", total); +} +int main() +{ + int i, j, f = 0; + while (scanf("%d", &N) == 1) + { + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + scanf("%d", &Link[i][j]); + } + Cal(); + //printf("\n"); + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + Link[i][j] = 0; + } + putchar('\n'); + } + return 0; +} diff --git a/627.cpp b/627.cpp index 1a40a42..3d6e5aa 100644 --- a/627.cpp +++ b/627.cpp @@ -1,99 +1,140 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; - -#define PI acos(-1.0) -#define EPS 1e-9 -#define INF 1<<30 +#include -#define FOI(I, A, B) for (I=A; I<=B; I++) -#define FOD(I, A, B) for (I=A; I>=B; I--) +using namespace std; -void showGraph(vector< vector > G){ +/* +The net +672 +*/ +#define MAXN 302 +int D[MAXN][MAXN]; +int P[MAXN][MAXN]; +int N; +char input[10000]; +void Ini() +{ int i, j; - FOI(i, 0, G.size()-1){ - FOI(j, 0, G[i].size()-1) - cout << G[i][j] << " "; - - cout << endl; + for (i = 1; i < N; i++) + { + for (j = i + 1; j <= N; j++) + { + D[i][j] = D[j][i] = 100000; + P[i][j] = P[j][i] = -1; + } + P[i][i] = -1; + D[i][i] = 0; } + P[i][i] = -1; + D[i][i] = 0; } - -void showVector( vector vec ){ - int i; - FOI(i, 0, vec.size()-1) - cout << vec[i] << " "; +void Sep() +{ + int i, j, k = 0, n, f = 0; + char temp[100]; + for (i = 0; input[i] != '-'; i++) + { + temp[k++] = input[i]; + } + temp[k] = NULL; + sscanf(temp, "%d", &n); + k = 0; + for (j = i + 1; input[j]; j++) + { + f = 1; + if (input[j] != ',') + { + temp[k++] = input[j]; + } + else + { + temp[k] = NULL; + sscanf(temp, "%d", &i); + D[n][i] = 1; + P[n][i] = n; + k = 0; + } + } + if (f) + { + temp[k] = NULL; + sscanf(temp, "%d", &i); + D[n][i] = 1; + P[n][i] = n; + } } - -int toInt(string s){ - stringstream ss; - int I; - ss << s; - ss >> I; - return I; +void Floyd() +{ + int i, j, k; + for (k = 1; k <= N; k++) + { + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (D[i][j] > (D[i][k] + D[k][j])) + { + D[i][j] = D[i][k] + D[k][j]; + P[i][j] = P[k][j]; + } + else + { + P[i][j] = P[i][j]; + D[i][j] = D[i][j]; + } + } + } + } } - -void Tokenize(const string& str, vector& tokens, const string& delimiters = " -,\n"){ - - string::size_type lastPos = str.find_first_not_of(delimiters, 0); - string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (string::npos != pos || string::npos != lastPos){ - tokens.push_back(toInt(str.substr(lastPos, pos - lastPos))); - lastPos = str.find_first_not_of(delimiters, pos); - pos = str.find_first_of(delimiters, lastPos); - } +void Print(int i, int j) +{ + if (i == j) + { + printf("%d", i); + } + else + { + Print(i, P[i][j]); + printf(" %d", j); + } } - -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - int N, n; - while( scanf("%d", &N) ){ - vector< vector > graph(N); - FOI(n, 1, N){ - string str; - vector tokens; - getline(cin, str); - Tokenize(str, tokens); - int I = n - 1; - tokens.erase(tokens.begin()); - graph[I] = tokens; - } - cout << "-----" << endl; - int Q, S, D; - cin >> Q; - while( Q-- ) - cin >> S >> D; - showGraph(graph); - } +void Cal() +{ + int i, j, k; + printf("-----\n"); + gets(input); + sscanf(input, "%d", &k); + if (k) + { + Floyd(); + } + while (k--) + { + gets(input); + sscanf(input, "%d%d", &i, &j); + if (P[i][j] == -1) + { + printf("connection impossible"); + } + else + { + Print(i, j); + } + printf("\n"); + } +} +int main() +{ + int i; + while (gets(input)) + { + sscanf(input, "%d", &N); + Ini(); + for (i = 0; i < N; i++) + { + gets(input); + Sep(); + } + Cal(); + } return 0; } diff --git a/628.cpp b/628.cpp new file mode 100644 index 0000000..951230f --- /dev/null +++ b/628.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +char words[101][300], input[10000]; +int n, m, now, tot; +int out[10]; + +void solve(int x) +{ + int i, k; + if (x == tot) + { + k = 0; + for (i = 0; input[i]; i++) + { + if (input[i] == '#') + { + printf("%s", words[now]); + } + else + { + putchar(out[k++] + '0'); + } + } + putchar('\n'); + return; + } + for (i = 0; i < 10; i++) + { + out[x] = i; + solve(x + 1); + } +} + +int main() +{ + int i, j; + while (~scanf("%d", &n)) + { + getchar(); + for (i = 0; i < n; i++) + { + gets(words[i]); + } + puts("--"); + scanf("%d", &m); + getchar(); + for (i = 0; i < m; i++) + { + gets(input); + tot = 0; + for (j = 0; input[j]; j++) + { + if (input[j] == '0') + { + tot++; + } + } + for (j = 0; j < n; j++) + { + now = j; + solve(0); + } + } + } + return 0; +} diff --git a/629.cpp b/629.cpp new file mode 100644 index 0000000..247271a --- /dev/null +++ b/629.cpp @@ -0,0 +1,145 @@ +#include + +using namespace std; + +char buf[2008], str[30][30], *ptr[30]; +int n, m, ary[3000][30], setnum[3000], lst[30], idx[3000], used[30], fnum[30], fmap_[30][30], num[30], map_[30][30]; + +int comp(const void *p, const void *q) +{ + return *(int *)p - *(int *)q; +} + +int scomp(const void *p, const void *q) +{ + int i; + if (setnum[*(int *)p] == setnum[*(int *)q]) + { + for (i = 0; i < setnum[*(int *)p]; i++) + { + if (ary[*(int *)p][i] != ary[*(int *)q][i]) + { + return ary[*(int *)p][i] - ary[*(int *)q][i]; + } + } + return 0; + } + return setnum[*(int *)q] - setnum[*(int *)p]; +} + +void dfs(int now, int depth) +{ + int i, j, temp; + if (now > n) + { + memcpy(ary[m], lst, sizeof(int) * depth); + qsort(ary[m], depth, sizeof(int), comp); + idx[m] = m; + setnum[m++] = depth; + return; + } + if (used[now]) + { + dfs(now + 1, depth); + } + else + { + used[now]++; + for (i = 0; i < fnum[now]; i++) + { + used[fmap_[now][i]]++; + } + lst[depth] = now; + dfs(now + 1, depth + 1); + used[now]--; + for (i = 0; i < fnum[now]; i++) + { + used[fmap_[now][i]]--; + } + for (i = 0; i < num[now]; i++) + { + temp = map_[now][i]; + if (!used[temp]) + { + used[temp]++; + for (j = 0; j < fnum[temp]; j++) + { + used[fmap_[temp][j]]++; + } + lst[depth] = temp; + dfs(now + 1, depth + 1); + used[temp]--; + for (j = 0; j < fnum[temp]; j++) + { + used[fmap_[temp][j]]--; + } + } + } + } +} + +int main() +{ + int i, j, s, now, last; + while (scanf("%d", &n) == 1) + { + memset(num, 0, sizeof(num)); + memset(fnum, 0, sizeof(fnum)); + for (i = 1; i <= n; i++) + { + scanf("%s", buf); + for (j = 0; j < n; j++) + { + if (buf[j] == '*') + { + map_[i][num[i]++] = j + 1; + fmap_[i][fnum[i]++] = j + 1; + fmap_[j + 1][fnum[j + 1]++] = i; + } + } + } + m = 0; + memset(used, 0, sizeof(used)); + dfs(1, 0); + qsort(idx, m, sizeof(int), scomp); + for (i = 1; i <= n; i++) + { + ptr[i] = str[i]; + } + for (i = 0, s = 0; i < m; i++) + { + now = idx[i]; + if (i) + { + last = idx[i - 1]; + if (setnum[now] != setnum[last] || memcmp(ary[now], ary[last], sizeof(int) * setnum[now])) + { + for (j = 0; j < setnum[now]; j++) + { + /*printf("%d ", ary[now][j]);*/ + *(ptr[ary[now][j]]++) = s + 'a'; + } + /*printf("\n");*/ + s++; + } + } + else + { + for (j = 0; j < setnum[now]; j++) + { + /*printf("%d ", ary[now][j]);*/ + *(ptr[ary[now][j]]++) = s + 'a'; + } + /*printf("\n");*/ + s++; + } + } + for (i = 1; i <= n; i++) + { + *(ptr[i]) = 0; + puts(str[i]); + } + puts("-----"); + } + return 0; +} diff --git a/630.cpp b/630.cpp index 36a246c..0224aea 100644 --- a/630.cpp +++ b/630.cpp @@ -1,73 +1,58 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--){ - int N; - cin >> N; - int i, j; - map Map; - vector< string > Word(N); - FOI(i, 0, N-1){ - cin >> Word[i]; - string temp = Word[i]; - sort(temp.begin(), temp.end()); - Map[Word[i]] = temp; +int main() +{ + int cases; + int words; + int count; + string input; + multimap m; + multimap::iterator it; + pair::iterator, multimap::iterator> ret; + cin >> cases; + while (cases--) + { + m.clear(); + cin >> words; + cin.ignore(100, '\n'); + for (int i = 0; i < words; i++) + { + getline(cin, input); + string tmp(input); + sort(tmp.begin(), tmp.end()); + m.insert(make_pair(tmp, input)); } - while (true){ - string str, temp; - cin >> str; - if (str == "END") + while (getline(cin, input)) + { + if (input == "END") + { break; - temp = str; - sort(temp.begin(), temp.end()); - vector< string > vec; - cout << "Anagrams for: " << str << endl; - FOI(i, 0, N-1){ - if (Map[Word[i]] == temp) - vec.push_back(Word[i]); } - int SZ = vec.size(); - if (SZ == 0) - cout << "No anagrams for: " << str << endl; + string tmp(input); + sort(tmp.begin(), tmp.end()); + cout << "Anagrams for: " << input << endl; + if (m.count(tmp) == 0) + { + cout << "No anagrams for: " << input << endl; + } else - FOI(i, 0, SZ-1) - printf("%3d) %s\n", i+1, vec[i].c_str()); + { + count = 0; + ret = m.equal_range(tmp); + for (it = ret.first; it != ret.second; it++) + { + count++; + printf("%3d) ", count); + cout << (*it).second << endl; + } + } } - if (T) + if (cases) + { cout << endl; + } } return 0; } - diff --git a/632.cpp b/632.cpp new file mode 100644 index 0000000..a26347f --- /dev/null +++ b/632.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +/* +632 +Compression (II) +*/ +struct ss +{ + int st; +} w[1999]; +char str[1999]; +int N; +int com(const void *a, const void *b) +{ + int p, q, i, l1, l2; + ss *x = (ss *)a; + ss *y = (ss *)b; + p = x->st; + q = y->st; + for (i = 0; i < N; i++) + { + l1 = (p + i) % N; + l2 = (q + i) % N; + if (str[l1] > str[l2]) + { + return 1; + } + if (str[l1] < str[l2]) + { + return -1; + } + } + return 0; +} +void ReadCase() +{ + char temp[60]; + int d = 0; + gets(temp); + N = atoi(temp); + str[0] = NULL; + while (1) + { + gets(temp); + strcat(str, temp); + d += strlen(temp); + if (d >= N) + { + break; + } + } +} +void Cal() +{ + int i, p; + for (i = 0; i < N; i++) + { + w[i].st = i; + } + qsort(w, N, sizeof(w[0]), com); + for (i = 0; i < N; i++) + if (w[i].st == 1) + { + break; + } + printf("%d\n", i); + for (i = 0; i < N; i++) + { + if (i && i % 50 == 0) + { + putchar('\n'); + } + p = w[i].st + N - 1; + p %= N; + printf("%c", str[p]); + } + printf("\n"); +} +int main() +{ + int kase; + char input[100]; + gets(input); + kase = atoi(input); + gets(input); + while (kase--) + { + ReadCase(); + Cal(); + if (kase) + { + gets(input); + printf("\n"); + } + } + return 0; +} diff --git a/633.cpp b/633.cpp new file mode 100644 index 0000000..b77e477 --- /dev/null +++ b/633.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +struct Step {int x, y, dist, type;}; +Step queue_[5000], start, end_; + +bool state[41][41][3]; +char map_[41][41]; +int dist[41][41], front, rear, n; + +int input() +{ + int i, j, k, tx, ty; + scanf("%d", &n); + if (!n) + { + return 0; + } + n *= 2; + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + map_[i][j] = ' '; + dist[i][j] = 999999; + for (k = 0; k < 3; k++) + { + state[i][j][k] = false; + } + } + } + scanf("%d %d", &start.x, &start.y); + start.type = -1; + start.dist = 0; + scanf("%d %d", &end_.x, &end_.y); + while (true) + { + scanf("%d %d", &tx, &ty); + if (!tx && !ty) + { + break; + } + map_[tx][ty] = '#'; + } + return 1; +} +void run0(Step u) +{ + int tx, ty; + int dirx[8] = {1, 2, 2, 1, -1, -2, -2, -1}; + int diry[8] = {2, 1, -1, -2, -2, -1, 1, 2}; + Step v; + v.type = 0; + v.dist = u.dist + 1; + for (int i = 0; i < 8; i++) + { + tx = u.x + dirx[i]; + ty = u.y + diry[i]; + if (tx > 0 && tx <= n && ty > 0 && ty <= n && !state[tx][ty][0] && map_[tx][ty] != '#') + { + state[tx][ty][0] = true; + v.x = tx; + v.y = ty; + queue_[++rear] = v; + } + } +} +void run1(Step u) +{ + int tx, ty; + int dirx[4] = {2, 2, -2, -2}; + int diry[4] = {2, -2, 2, -2}; + Step v; + v.type = 1; + v.dist = u.dist + 1; + for (int i = 0; i < 4; i++) + { + tx = u.x + dirx[i]; + ty = u.y + diry[i]; + if (tx > 0 && tx <= n && ty > 0 && ty <= n && !state[tx][ty][1] && map_[tx][ty] != '#') + { + state[tx][ty][1] = true; + v.x = tx; + v.y = ty; + queue_[++rear] = v; + } + } +} +void run2(Step u) +{ + int tx, ty; + Step v; + v.type = 2; + v.dist = u.dist + 1; + tx = n + 1 - u.x; + ty = u.y; + if (tx > 0 && tx <= n && ty > 0 && ty <= n && !state[tx][ty][2] && map_[tx][ty] != '#') + { + state[tx][ty][2] = true; + v.x = tx; + v.y = ty; + queue_[++rear] = v; + } + tx = u.x; + ty = n + 1 - u.y; + if (tx > 0 && tx <= n && ty > 0 && ty <= n && !state[tx][ty][2] && map_[tx][ty] != '#') + { + state[tx][ty][2] = true; + v.x = tx; + v.y = ty; + queue_[++rear] = v; + } +} +void solve() +{ + Step u, v; + front = -1, rear = 0; + queue_[0] = start; + while (front < rear) + { + u = queue_[++front]; + if (u.dist < dist[u.x][u.y]) + { + dist[u.x][u.y] = u.dist; + } + if (u.type != 0) + { + run0(u); + } + if (u.type != 1) + { + run1(u); + } + if (u.type != 2) + { + run2(u); + } + } + if (dist[end_.x][end_.y] != 999999) + { + printf("Result : %d\n", dist[end_.x][end_.y]); + } + else + { + puts("Solution doesn't exist"); + } +} +int main() +{ + while (input()) + { + solve(); + } +} diff --git a/634.cpp b/634.cpp new file mode 100644 index 0000000..6504d2f --- /dev/null +++ b/634.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +/* + * 634.cpp + * + * Created on: Jul 20, 2011 + * Author: Mauricio + */ + +struct point +{ + double x, y; +}; + +int point_in_polygon(vector &polygon, point &p) +{ + //C is the number of times a line from point t + //towards the polygon intersect segments of the polygon + //if C is even = the point is outside the polygon + //if C is odd = the point is inside the polygon + int i, j; + int size; + int c; + c = 0;//assume the point is not inside the polygon + size = polygon.size(); + for (i = 0, j = size - 1; i < size; j = i++) + { + //the point crosses the segment's y-bound + if (((polygon[i].y > p.y) != (polygon[j].y > p.y)) && + //the point passes through a vertex + (p.x < (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x)) + { + c = !c; + } + } + return c; +} + +int main() +{ + int n_points;//number of points + int i; + point p;//point to check + while (scanf("%d", &n_points) && n_points != 0) + { + vector polygon(n_points); + for (i = 0; i < n_points; ++i) + { + scanf("%lf%lf", &polygon[i].x, &polygon[i].y); + } + scanf("%lf%lf", &p.x, &p.y); + bool inside; + inside = point_in_polygon(polygon, p); + if (inside == 1) + { + printf("T\n"); + } + else + { + printf("F\n"); + } + } + return 0; +} diff --git a/635.cpp b/635.cpp new file mode 100644 index 0000000..1584713 --- /dev/null +++ b/635.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int num[13]; +int pile[13][4]; +int ary[104]; +char buf[2008]; + +int trans(char now) +{ + if (now == 'A') + { + return 10; + } + else if (now == 'T') + { + return 9; + } + else if (now == 'J') + { + return 0; + } + else if (now == 'Q') + { + return 11; + } + else if (now == 'K') + { + return 12; + } + return now - 49; +} + +int play() +{ + int c, now; + memset(num, 0, sizeof(num)); + now = pile[12][num[12]++]; + c = 1; + for (;;) + { + if (num[now] < 4) + { + c++; + now = pile[now][num[now]++]; + } + else + { + break; + } + } + /*printf("%d\n", c);*/ + return c == 52; +} + +int main() +{ + int n, i, j, ans; + while (scanf("%s", buf) == 1) + { + if (buf[0] == '0') + { + break; + } + n = 0; + do + { + ary[n] = ary[n + 52] = trans(buf[0]); + n++; + /*printf("%d\n", ary[n-1]);*/ + } while (n < 52 && scanf("%s", buf) == 1); + for (i = 0, ans = 0; i < 52; i++) + { + for (j = 0; j < 52; j += 4) + { + memcpy(pile[j / 4], ary + i + j, sizeof(int) * 4); + /*if(!i)printf("%d\n", pile[j][j/13]);*/ + } + ans += play(); + } + printf("%d\n", ans); + } + return 0; +} diff --git a/636.cpp b/636.cpp index 836b610..dd4e0a4 100644 --- a/636.cpp +++ b/636.cpp @@ -1,36 +1,49 @@ -#include -#include -#include -#include -#include +#include + using namespace std; -long mystrtol(char *str, int b) { - long result = 0; - for(int i=0; i= base) + { + return -1; + } + result += tmp * pow(base, count); + count++; + } + return result; } -int main(){ - char str[100]; - char *end; - long a, i, max; - while( true ) { - scanf("%s", str); - if( str[0] == '0' ) - break; - max = 0; - for( i = strlen(str)-1; i >= 0 ; i--) - if( str[i] - '0' > max ) - max = str[i] - '0'; - for(i=max+1; i<100; i++) { - a = mystrtol(str, i); - if( (long)sqrt( a ) * (long)sqrt( a ) == a) - break; - } - printf("%ld\n", i); - } - return 0; +int main() +{ + int n, sq, tmp; + while (cin >> n) + { + if (n == 0) + { + break; + } + for (int base = 2; base < 100; base++) + { + tmp = any2dec(n, base); + if (tmp == -1) + { + continue; + } + sq = sqrt(tmp); + if (sq * sq == tmp) + { + cout << base << endl; + break; + } + } + } + return 0; } diff --git a/637.cpp b/637.cpp index bdd5777..3395f2a 100644 --- a/637.cpp +++ b/637.cpp @@ -1,77 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - while (true) { - int N; - scanf("%d", &N); - if (!N) break; - int Sz = (int)ceil(N/4.0); - int Front[Sz][2], Back[Sz][2]; - int A = 1, B = 4 * Sz; - for (int i = 0; i < Sz; i++) { - if (B <= N) Front[i][0] = B; - else Front[i][0] = -1; - - if (A <= N) Front[i][1] = A; - else Front[i][1] = -1; - - A += 2; B -= 2; +int main() +{ + int n; + while (cin >> n) + { + if (!n) + { + break; } - A = 2; B = 4 * Sz - 1; - for (int i = 0; i < Sz; i++) { - if (A <= N) Back[i][0] = A; - else Back[i][0] = -1; - - if (B <= N) Back[i][1] = B; - else Back[i][1] = -1; - - A += 2; B -= 2; + cout << "Printing order for " << n << " pages:" << endl; + if (n == 1) + { + cout << "Sheet 1, front: Blank, 1" << endl; + continue; } - printf("Printing order for %d pages:\n", N); - for (int i = 0; i < Sz; i++) { - if (Front[i][0] > 0 || Front[i][1] > 0) { - printf("Sheet %d, front: ", i + 1); - if (Front[i][0] > 0) printf("%d, ", Front[i][0]); - else printf("Blank, "); - - if (Front[i][1] > 0) printf("%d\n", Front[i][1]); - else printf("Blank\n"); + int sheets = ceil(n / 4.0); + int start = 1, end = 4 * sheets; + for (int i = 0; i < sheets; i++) + { + cout << "Sheet " << i + 1 << ", front: "; + if (end > n) + { + cout << "Blank"; + } + else + { + cout << end; + } + cout << ", " << start << endl; + start++, end--; + cout << "Sheet " << i + 1 << ", back : " << start << ", "; + if (end > n) + { + cout << "Blank"; } - if (Back[i][0] > 0 || Back[i][1] > 0) { - printf("Sheet %d, back : ", i + 1); - if (Back[i][0] > 0) printf("%d, ", Back[i][0]); - else printf("Blank, "); - - if (Back[i][1] > 0) printf("%d\n", Back[i][1]); - else printf("Blank\n"); + else + { + cout << end; } + cout << endl; + start++, end--; } } return 0; diff --git a/638.cpp b/638.cpp new file mode 100644 index 0000000..f1f752f --- /dev/null +++ b/638.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define pb push_back + +const int N = 51; + +class state +{ +public: + string name; + int y; + bool operator<(const state &a) const + { + return y < a.y; + } +}; + +vector in[N]; +int isexist[N][N]; + +void solve() +{ + vector ans; + rep(i, N) sort(ALL(in[i])); + rep(i, N) rep(j, N) isexist[i][j] = -1; + rep(i, N) + { + rep(j, in[i].size()) + { + isexist[i][in[i][j].y] = j; + } + } + rep(i, N) + { + rep(j, in[i].size()) + { + REP(k, j + 1, in[i].size()) + { + REP(l, i + 1, N) + { + int index[2] = {isexist[l][in[i][j].y], isexist[l][in[i][k].y]}; + if (index[0] != -1 && index[1] != -1) + { + string tmp = ""; + tmp += in[l][index[0]].name[0]; + tmp += in[l][index[1]].name[0]; + tmp += in[i][k].name[0]; + tmp += in[i][j].name[0]; + //tmp = "dummy"; + ans.pb(tmp); + } + } + } + } + } + sort(ALL(ans)); + if (ans.size() == 0) + { + cout << " No rectangles" << endl; + } + else + { + cout << endl; + rep(i, ans.size()) + { + if (i > 0 && i % 10 == 0) + { + cout << endl; + } + cout << ' ' << ans[i]; + } + cout << endl; + } +} + +main() +{ + int n; + int tc = 1; + while (cin >> n && n) + { + rep(i, N) in[i].clear(); + rep(i, n) + { + string tname; + int tx, ty; + cin >> tname >> ty >> tx; + in[tx].pb((state){ + tname, ty}); + } + cout << "Point set " << tc++ << ":"; + solve(); + } +} diff --git a/639.cpp b/639.cpp new file mode 100644 index 0000000..701e9ec --- /dev/null +++ b/639.cpp @@ -0,0 +1,118 @@ +#include + +using namespace std; + +/** +639 +**/ +char B[5][5]; +int N, MAX, K; +int Ilegal(int n) +{ + int r, c, i; + r = n / N; + c = n % N; + for (i = r + 1; i < N; i++) + { + if (B[i][c] == 'X') + { + break; + } + if (B[i][c] == 'P') + { + return 0; + } + } + for (i = r - 1; i >= 0; i--) + { + if (B[i][c] == 'X') + { + break; + } + if (B[i][c] == 'P') + { + return 0; + } + } + for (i = c + 1; i < N; i++) + { + if (B[r][i] == 'X') + { + break; + } + if (B[r][i] == 'P') + { + return 0; + } + } + for (i = c - 1; i >= 0; i--) + { + if (B[r][i] == 'X') + { + break; + } + if (B[r][i] == 'P') + { + return 0; + } + } + return 1; +} +void Recur(int index, int level) +{ + int i, temp, r, c; + if (level) + { + temp = index - 1; + if (!Ilegal(index - 1)) + { + return; + } + r = temp / N; + c = temp % N; + if (B[r][c] != 'X') + { + B[r][c] = 'P'; + K++; + if (K > MAX) + { + MAX = K; + } + } + } + if (level == N * N) + { + return; + } + for (i = index; i < N * N; i++) + { + Recur(i + 1, level + 1); + } + if (r < N && r >= 0 && c < N && c >= 0) + { + if (B[r][c] == 'P') + { + B[r][c] = '.'; + K--; + } + } +} +int main() +{ + int i; + while (scanf("%d", &N) == 1) + { + if (!N) + { + break; + } + for (i = 0; i < N; i++) + { + scanf("%s", B[i]); + } + MAX = K = 0; + Recur(0, 0); + printf("%d\n", MAX); + } + return 0; +} diff --git a/640.cpp b/640.cpp index 54ca5db..d3cf0f9 100644 --- a/640.cpp +++ b/640.cpp @@ -1,60 +1,34 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +char x[1000001]; -bool self[MAX + 1]; +int main() +{ + memset(x, 0, sizeof(x)); -int digSum(int N){ - int S = 0; - while (N > 0){ - S += N % 10; - N /= 10; + int j, tmp; + for (int i = 1; i <= 999999; i++) + { + j = i, tmp = 0; + while (j) + { + tmp += j % 10; + j /= 10; + } + if (tmp + i <= 1000000) + { + x[tmp + i] = 1; + } } - return S; -} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - memset(self, true, sizeof self); - int i; - FOI(i, 1, MAX){ - if (self[i]){ - cout << i << endl; - int j = i + digSum(i); - while (j <= MAX && self[j]){ - self[j] = false; - j += digSum(j); - } + for (int i = 1; i <= 1000000; i++) + { + if (!x[i]) + { + printf("%d\n", i); } } return 0; } - diff --git a/641.cpp b/641.cpp new file mode 100644 index 0000000..c315ce2 --- /dev/null +++ b/641.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +int char2int(char c) +{ + if (c == '_') + { + return 0; + } + else if (c == '.') + { + return 27; + } + return int(c - 'a' + 1); +} + +char int2char(int c) +{ + if (c == 0) + { + return '_'; + } + else if (c == 27) + { + return '.'; + } + return char(c + 'a' - 1); +} + +void decode(string &input, int k) +{ + int sz = input.size(); + int plaincode[sz]; + for (int i = 0; i < sz; i++) + { + plaincode[(k * i) % sz] = (char2int(input[i]) + i) % 28; + } + for (int i = 0; i < sz; i++) + { + cout << int2char(plaincode[i]); + } + cout << endl; +} + +int main() +{ + int k; + string input; + while (cin >> k) + { + if (k == 0) + { + break; + } + // reading space after k + cin.get(); + getline(cin, input); + decode(input, k); + } + return 0; +} diff --git a/642.cpp b/642.cpp index e7de9dd..43d1f8b 100644 --- a/642.cpp +++ b/642.cpp @@ -1,66 +1,47 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); +using namespace std; - map Dic; - map::iterator it; - - string A, B; - while (true){ - cin >> A; - if (A == "XXXXXX") +int main() +{ + string input; + vector result; + multimap m; + multimap::iterator it; + pair::iterator, multimap::iterator> ret; + while (getline(cin, input)) + { + if (input == "XXXXXX") + { break; - B = A; - sort(B.begin(), B.end()); - Dic[A] = B; + } + string tmp = string(input.begin(), input.end()); + sort(tmp.begin(), tmp.end(), less()); + m.insert(make_pair(tmp, input)); } - while (true){ - cin >> A; - if (A == "XXXXXX") + while (getline(cin, input)) + { + if (input == "XXXXXX") + { break; - B = A; - sort(B.begin(), B.end()); - bool stat = true; - for (it = Dic.begin(); it != Dic.end(); it++){ - if ((*it).second == B){ - cout << (*it).first << endl; - stat = false; - } } - if (stat) + sort(input.begin(), input.end(), less()); + ret = m.equal_range(input); + for (it = ret.first; it != ret.second; it++) + { + result.push_back(it->second); + } + sort(result.begin(), result.end()); + for (int i = 0, sz = result.size(); i < sz; i++) + { + cout << result[i] << endl; + } + if (result.size() == 0) + { cout << "NOT A VALID WORD" << endl; + } + result.clear(); cout << "******" << endl; } return 0; } - diff --git a/643.cpp b/643.cpp new file mode 100644 index 0000000..565e634 --- /dev/null +++ b/643.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define MAX 20 +#define MIN 10 +int printout(int, long, int); +int adderr(char[][12], char *, int); +long valid(char *); +int main() +{ + long zip[500], zip2[500], num, comp; + int letters, bundles, prev, count; + int flag, i, j, k, n, savi, nzip, nerr; + char s[12], err[20][12]; + letters = bundles = 0; + i = n = 0; + printf("ZIP LETTERS BUNDLES\n\n"); + while (scanf("%s", s) == 1) + { + //s[strlen(s)-1] = '\0'; + if ((num = valid(s)) > 0) + { + zip[i++] = num; + } + else + { + n = adderr(err, s, n); + } + } + nzip = i; + nerr = n; + for (i = 0; i < nzip - 1; ++i) /* sort the zips */ + for (j = 0; j < nzip - 1; ++j) + if (zip[j] > zip[j + 1]) + { + comp = zip[j]; + zip[j] = zip[j + 1]; + zip[j + 1] = comp; + } + i = prev = k = 0; + flag = 0; /* first pass for 5-digit codes */ + while (i < nzip) + { + count = 0; + comp = zip[i]; + savi = i; + while (zip[i] == comp && i < nzip) + { + i++; + } + count = i - savi; + if (count >= MIN) + { + letters += count; + bundles += printout(count, comp, flag); + for (j = prev; j < savi; ++j) /* copy nonbundled to zip2 */ + { + zip2[k++] = zip[j]; + } + prev = i; + } + } /* end while i < nzip */ + for (j = prev; j < nzip; ++j) + { + zip2[k++] = zip[j]; /* end first pass */ + } + putchar('\n'); + i = 0; + nzip = k; + flag = 1; /* 2nd pass for 3-digit codes */ + while (i < nzip) + { + savi = i; + comp = zip2[i++] / 100; + while (zip2[i] / 100 == comp && i < nzip) + { + i++; + } + count = i - savi; + if (count >= MIN) + { + letters += count; + bundles += printout(count, comp, flag); + for (j = savi; j < i; ++j) /* zero out bundled positions */ + { + zip2[j] = 0; + } + } + } /* end while i < nzip and end of 2nd pass */ + putchar('\n'); + i = 0; + while (i < nzip) /* last pass for first class zips */ + { + while (zip2[i] == 0 && i < nzip) + { + i++; + } + comp = zip2[i++]; + count = 1; + while (zip2[i] == comp && i < nzip) + { + count++; + i++; + } + printf("%ld %3d 0\n", comp, count); + letters += count; + } /* end while i < nzip and end last pass */ + printf("\nTOTALS %3d %2d\n\n", letters, bundles); + printf("INVALID ZIP CODES\n"); + for (n = 0; n < nerr; ++n) + { + printf("%s\n", err[n]); + } +} +long valid(char *s) +{ + int i; + if (strlen(s) != 5) + { + return -1; + } + for (i = 0; isdigit(s[i]); ++i) + ; + if (i != 5) /* if < 5 it's nonnumeric */ + { + return -1; + } + return atol(s); /* convert to numeric */ +} +int printout(int count, long comp, int flag) +{ + int k, rem; /* k is no. bundles */ + k = count / MAX; + rem = count % MAX; + if (rem > 0) + { + k++; + } + if (flag == 1) + { + printf("%3ldxx", comp); + } + else + { + printf("%5ld", comp); + } + printf(" %3d %2d\n", count, k); + return k; +} +int adderr(char err[][12], char *s, int n) +{ + int i; + for (i = 0; i < n; ++i) + if (strcmp(err[i], s) == 0) + { + return n; + } + strcpy(err[n], s); + /* printf("%s %s\n", err[n], s); */ + return ++n; +} diff --git a/644.cpp b/644.cpp index 32c3a37..8389e1a 100644 --- a/644.cpp +++ b/644.cpp @@ -1,63 +1,48 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +vector symbol; + +int input() +{ + symbol.clear(); + string s; + while (cin >> s) + { + if (s[0] == '9') + { + return 1; + } + symbol.push_back(s); + } + return 0; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int t = 1; - int i, j; - vector< string > Vec; - string str; - bool stat = true; - while (cin >> str) { - if (str == "9") { - int N = Vec.size() - 1; - FOI(i, 0, N) { - FOI(j, 0, N) { - if (i == j) continue; - if (int(Vec[j].find(Vec[i])) == 0) { - stat = false; - break; - } - } - if (!stat) break; +int main() +{ + int kase = 1; + while (input()) + { + sort(symbol.begin(), symbol.end()); + bool yes = true; + for (int i = 1; i < symbol.size(); i++) + { + int len = min(symbol[i - 1].size(), symbol[i].size()); + if (symbol[i - 1].compare(0, len, symbol[i], 0, len) == 0) + { + yes = false; + break; } - if (stat) - cout << "Set " << t++ << " is immediately decodable" << endl; - else - cout << "Set " << t++ << " is not immediately decodable" << endl; - Vec.clear(); - stat = true; + } + if (yes) + { + cout << "Set " << kase++ << " is immediately decodable" << endl; } else - Vec.push_back(str); + { + cout << "Set " << kase++ << " is not immediately decodable" << endl; + } } return 0; } diff --git a/645.cpp b/645.cpp new file mode 100644 index 0000000..d20e131 --- /dev/null +++ b/645.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +const int maxfile = 256; + +int dataset = 0; + +void front(int n) +{ + for (; n > 0; n--) + { + cout << "| "; + } +} + +bool first = true; + +bool mapping(int n, const char *dirname) +{ + char list[maxfile][80]; + int sort[maxfile]; + int files = 0; + cin >> list[files]; + if (strchr(list[files], '#')) + { + return false; + } + if (n == 0) + { + if (!first) + { + cout << endl; + } + cout << "DATA SET " << ++dataset << ':' << endl; + } + first = false; + front(n); + cout << dirname << endl; + while (true) + { + if (list[files][0] == '*' || list[files][0] == ']') + { + int i, j, t; + for (i = 0; i < files; i++) + { + sort[i] = i; + } + for (i = 0; i < files - 1; i++) + for (j = i + 1; j < files; j++) + if (strcmp(list[sort[i]], list[sort[j]]) > 0) + { + t = sort[i]; + sort[i] = sort[j]; + sort[j] = t; + } + for (i = 0; i < files; i++) + { + front(n); + cout << list[sort[i]] << endl; + } + // if( n == 0 ) cout << endl; + return true; + } + if (list[files][0] == 'd') + { + mapping(n + 1, list[files]); + } + else + { + files++; + } + cin >> list[files]; + } +} + +int main() +{ + while (mapping(0, "ROOT")) + ; + return 0; +} diff --git a/647.cpp b/647.cpp new file mode 100644 index 0000000..110e107 --- /dev/null +++ b/647.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +#define MAXN 102 + +struct ss +{ + int chutes, ladders; + int loss, win; +} B[MAXN]; +struct xx +{ + int loss; + int poss; +} pp[15]; +int T[1005], tt, P; + +void Cal() +{ + int i, rhead = 0, n, pos; + while (1) + { + for (i = 0; i < P; i++) + { + if (pp[i].loss) + { + pp[i].loss = 0; + continue; + } + n = T[rhead++]; + pos = n + pp[i].poss; + if (pos == 100) + { + printf("%d\n", i + 1); + return; + } + else if (pos > 100) + { + continue; + } + else if (B[pos].chutes) + { + pp[i].poss = B[pos].chutes; + if (B[pp[i].poss].win) + { + i--; + } + if (B[pp[i].poss].loss) + { + pp[i].loss = 1; + } + } + else if (B[pos].ladders) + { + pp[i].poss = B[pos].ladders; + if (B[pp[i].poss].win) + { + i--; + } + if (B[pp[i].poss].loss) + { + pp[i].loss = 1; + } + } + else if (B[pos].loss) + { + pp[i].loss = 1; + pp[i].poss = pos; + } + else if (B[pos].win) + { + pp[i].poss = pos; + i--; + } + else + { + pp[i].poss = pos; + } + } + } +} + +int main() +{ + int n, m; + tt = 0; + while (scanf("%d", &n) && n) + { + T[tt++] = n; + } + while (scanf("%d", &P) && P) + { + while (scanf("%d%d", &n, &m)) + { + if (!n && !m) + { + break; + } + if (n < m) + { + B[n].ladders = m; + } + else + { + B[n].chutes = m; + } + } + while (scanf("%d", &n) && n) + { + if (n < 0) + { + n = -n; + B[n].loss = 1; + } + else + { + B[n].win = 1; + } + } + Cal(); + memset(B, 0, sizeof(int) * MAXN); + memset(pp, 0, sizeof(int) * 15); + } + return 0; +} diff --git a/648.cpp b/648.cpp new file mode 100644 index 0000000..e4d127d --- /dev/null +++ b/648.cpp @@ -0,0 +1,132 @@ +#include + +using namespace std; + +int a[32], n; +int r[4], rs, rh, rm, rc; + +void check(int b[], int m) +{ + int i, h, s; + for (i = 0, s = 0; i < m; i++) + if (i == 0 || b[i - 1] != b[i]) + { + s++; + } + h = a[b[m - 1]]; + if (rc > 0 && rs == s && rm == m && rh == h) + { + rc++; + return; + } + if (rc != 0) + { + if (!(s > rs || (s == rs && (m < rm || (m == rm && h > rh))))) + { + return; + } + rc = 0; + } + rc = 1; + rs = s; + rm = m; + rh = h; + for (i = 0; i < m; i++) + { + r[i] = b[i]; + } +} + +void solve(int s) +{ + int b[4], p[4], i; + rc = 0; + for (b[0] = 0; b[0] < n; b[0]++) + { + p[0] = a[b[0]]; + if (p[0] > s) + { + break; + } + if (p[0] == s) + { + check(b, 1); + } + for (b[1] = b[0]; b[1] < n; b[1]++) + { + p[1] = p[0] + a[b[1]]; + if (p[1] > s) + { + break; + } + if (p[1] == s) + { + check(b, 2); + } + for (b[2] = b[1]; b[2] < n; b[2]++) + { + p[2] = p[1] + a[b[2]]; + if (p[2] > s) + { + break; + } + if (p[2] == s) + { + check(b, 3); + } + for (b[3] = b[2]; b[3] < n; b[3]++) + if ((p[2] + a[b[3]]) == s) + { + check(b, 4); + } + } + } + } + if (rc == 0) + { + printf("%d ---- none\n", s); + } + else if (rc > 1) + { + printf("%d (%d): tie\n", s, rs); + } + else + { + printf("%d (%d):", s, rs); + for (i = 0; i < rm; i++) + { + printf(" %d", a[r[i]]); + } + printf("\n"); + } +} + +int cmp(const void *p, const void *q) +{ + return *(int *)p - *(int *)q; +} + +int main() +{ + int s; + for (;;) + { + for (n = 0;; n++) + { + if (scanf("%d", &a[n]) != 1) + { + return 0; + } + if (a[n] == 0) + { + break; + } + } + qsort(a, n, sizeof(a[0]), &cmp); + while (scanf("%d", &s) == 1 && s != 0) + { + solve(s); + } + } + return 0; +} diff --git a/649.cpp b/649.cpp new file mode 100644 index 0000000..afaf634 --- /dev/null +++ b/649.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define INF 0x3f3f3f3f +const int N = 30; +int n, m, a, b, know[N], ans, s; + +inline int bitcount(int x) +{ + return x == 0 ? 0 : (bitcount(x / 2) + (x & 1)); +} + +inline int cal(int s, int Min, int s1) +{ + int ans = 0; + int ss = (((1 << n) - 1) ^ s); + int s2 = n - s1; + if (abs(s1 - s2) > 1) + { + return -1; + } + for (int i = 0; i < n; i++) + { + if (s & (1 << i)) + { + int kno = (know[i] & s); + int lone = s1 - bitcount(kno); + ans = max(ans, lone); + } + if (ss & (1 << i)) + { + int kno = (know[i] & ss); + int lone = s2 - bitcount(kno); + ans = max(ans, lone); + } + if (ans > Min) + { + return -1; + } + } + return ans; +} + +void dfs(int num, int ss, int d) +{ + if ((n - d) + num < n / 2) + { + return; + } + if (num >= n / 2) + { + int t = cal(ss, ans, num); + if (t != -1) + { + if (ans > t) + { + ans = t; + s = ss; + } + } + return; + } + dfs(num + 1, ss | (1 << d), d + 1); + dfs(num, ss, d + 1); +} + +inline void solve() +{ + ans = INF; + int i; + dfs(0, 0, 0); + printf("%d\n", ans); + printf("%d", bitcount(s)); + for (i = 0; i < n; i++) + if (s & (1 << i)) + { + printf(" %d", i + 1); + } + printf("\n"); + int ss = (((1 << n) - 1) ^ s); + printf("%d", bitcount(ss)); + for (i = 0; i < n; i++) + if (ss & (1 << i)) + { + printf(" %d", i + 1); + } + printf("\n"); +} + +int main() +{ + int bo = 0; + while (~scanf("%d", &n)) + { + if (bo++) + { + printf("\n"); + } + for (int i = 0; i < n; i++) + { + scanf("%d%d", &a, &m); + a--; + know[a] = (1 << a); + while (m--) + { + scanf("%d", &b); + b--; + know[a] = (know[a] | (1 << b)); + } + } + solve(); + } + return 0; +} diff --git a/650.cpp b/650.cpp new file mode 100644 index 0000000..930135e --- /dev/null +++ b/650.cpp @@ -0,0 +1,88 @@ +#include +#include + +using namespace std; + +// @ http://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string +string rtrim(string str) +{ + auto it1 = find_if(str.rbegin(), str.rend(), [](char ch) { return !isspace(ch, locale::classic()); }); + str.erase(it1.base(), str.end()); + return str; +} + +int main() +{ + int fst = 1; + char name[13]; + while (cin >> name) + { + if (!fst) + { + printf("\n"); + } + fst = 0; + char marks[4]; + unsigned nxtMark = 0, score = 0, frame = 1, ball = 0, bonus = 0, pinsUp, prevPinsUp = 10; + ostrstream line1, line2; + line1.setf(ios::left); + line1 << setw(12) << name << " "; + line2 << setw(12) << " "; + while (cin >> pinsUp) + { + unsigned pinsDowned = prevPinsUp - pinsUp; + if (bonus % 2 == 1) + // this ball completes the scoring in some other frame + { + score += 10 + pinsDowned; + line2 << setw(4) << score; + --bonus; + } + if (bonus == 2) + // the previous frame was a strike; this is ball 0 + { + score += pinsDowned; + bonus = 1; + } + marks[nxtMark++] = (pinsUp ? "-123456789"[pinsDowned] : "X/"[ball]); + if (ball == 0 && pinsUp) + { + ball = 1; + } + else// ball == 1 || !pinsUp + { + if (!pinsUp)// strike or spare + { + bonus += 2 - ball; + } + else// open frame + { + score += 10 - pinsUp; + line2 << setw(4) << score; + } + if (frame < 10) + { + marks[nxtMark++] = '\0'; + line1 << setw(4) << marks; + nxtMark = 0; + } + ball = 0; + pinsUp = 10; + ++frame; + } + prevPinsUp = pinsUp; + } + marks[nxtMark++] = '\0'; + line1 << setw(4) << marks; + line1 << endl + << ends; + line2 << endl + << ends; + string l1 = rtrim(string(line1.str())); + string l2 = rtrim(string(line2.str())); + cout << l1 << endl + << l2 << endl; + cin.clear(); + } + return 0; +} diff --git a/651.cpp b/651.cpp new file mode 100644 index 0000000..335ac31 --- /dev/null +++ b/651.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +double lst[99999]; + +void work() +{ + long i; + lst[0] = 0.5; + for (i = 1; i < 99999; i++) + { + lst[i] = lst[i - 1] + (double)0.5 / (i + 1); + } +} + +bool input() +{ + long n; + while (!cin.eof() && !isdigit(cin.peek())) + { + cin.get(); + } + if (cin.eof()) + { + return false; + } + cin >> n; + printf("%5ld%10.3lf\n", n, lst[n - 1]); + return true; +} + +int main() +{ + printf("# Cards Overhang\n"); + work(); + while (input()) + ; + return 0; +} diff --git a/652.cpp b/652.cpp new file mode 100644 index 0000000..44531a3 --- /dev/null +++ b/652.cpp @@ -0,0 +1,225 @@ +#include + +using namespace std; + +/** +652 +**/ +#define MAXN 362880 +char Board[12]; +int Fac[10]; +struct ss +{ + char command; + int parent; + char colour; +} V[MAXN + 2]; +char Q[MAXN][1][11]; +int QH, QT, SUM; +void Gen() +{ + int i, sum = 1; + for (i = 1; i < 10; i++) + { + sum *= i; + Fac[i] = sum; + } +} +int Isvisit(char tt[], int x = 0) +{ + int i, j, l; + int c; + SUM = 0; + l = strlen(tt); + for (i = 0; i < l - 1; i++) + { + c = 0; + for (j = i + 1; tt[j]; j++) + if (tt[i] > tt[j]) + { + c++; + } + SUM += c * Fac[l - i - 1]; + } + SUM++; + if (x == 1) + { + return 0; + } + if (V[SUM].colour == 1) + { + return 0; + } + V[SUM].colour = 1; + return 1; +} +int Push(char tt[]) +{ + int x; + x = Isvisit(tt); + if (x == 0) + { + return 0; + } + strcpy(Q[QH][0], tt); + return 1; +} +char *Pop() +{ + QT++; + return Q[QT - 1][0]; +} +int IsEmpty() +{ + return QH == QT; +} +int GENPUSH(char aa[]) +{ + int i, j, p; + char temp[15]; + char c; + Isvisit(aa); + ; + i = SUM; + strcpy(temp, aa); + for (p = 0; aa[p] != '9'; p++) + ; + if (p != 0 && p != 3 && p != 6) + { + c = temp[p - 1]; + temp[p - 1] = temp[p]; + temp[p] = c; + j = Push(temp); + if (j != 0) + { + if (SUM == 6) + { + j = 2; + } + V[SUM].parent = i; + V[SUM].command = 'r'; + strcpy(Q[QH++][0], temp); + } + } + strcpy(temp, aa); + if (p + 1 != 3 && p + 1 != 6 && p + 1 != 9) + { + c = temp[p + 1]; + temp[p + 1] = temp[p]; + temp[p] = c; + j = Push(temp); + if (j != 0) + { + if (SUM == 6) + { + j = 2; + } + V[SUM].parent = i; + V[SUM].command = 'l'; + strcpy(Q[QH++][0], temp); + } + } + strcpy(temp, aa); + if (p + 3 <= 8) + { + c = temp[p + 3]; + temp[p + 3] = temp[p]; + temp[p] = c; + j = Push(temp); + if (j != 0) + { + if (SUM == 6) + { + j = 2; + } + V[SUM].parent = i; + V[SUM].command = 'u'; + strcpy(Q[QH++][0], temp); + } + } + strcpy(temp, aa); + if (p - 3 >= 0) + { + c = temp[p - 3]; + temp[p - 3] = temp[p]; + temp[p] = c; + j = Push(temp); + if (j != 0) + { + if (SUM == 6) + { + j = 2; + } + V[SUM].parent = i; + V[SUM].command = 'd'; + strcpy(Q[QH++][0], temp); + } + } + return 0; +} +int BFS() +{ + int i; + char temp[19]; + for (i = 0; i <= MAXN; i++) + { + V[i].colour = 0; + } + QH = QT = 0; + Push("123456789"); + QH++; + while (!IsEmpty()) + { + strcpy(temp, Pop()); + GENPUSH(temp); + } + return 0; +} +void Cal(int sum) +{ + if (V[sum].colour == 0) + { + printf("unsolvable\n"); + return; + } + while (sum != 1) + { + printf("%c", V[sum].command); + sum = V[sum].parent; + } + putchar('\n'); +} +int main() +{ + int kase, i, m; + char input[100]; + Gen(); + BFS(); + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + gets(input); + for (i = 0, m = 0; input[i]; i++) + { + if (input[i] != ' ' && input[i] != 'x') + { + Board[m++] = input[i]; + } + else if (input[i] == 'x') + { + Board[m++] = '9'; + } + } + Board[m] = NULL; + Isvisit(Board, 1); + Cal(SUM); + if (kase) + { + gets(input); + putchar('\n'); + } + } + return 0; +} diff --git a/653.cpp b/653.cpp new file mode 100644 index 0000000..aa4cc83 --- /dev/null +++ b/653.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +/* +653 +Gizilch +*/ +#define MAXN 45000 +int Table[200][200]; +int A[200], B[200]; +int fa[MAXN], fb[MAXN]; +char aa[MAXN], bb[MAXN]; +int N, M, inda, indb; +struct ss +{ + int a, b; +} V[MAXN]; +int indv; +void GetDivisor(int n, int *p, int &ind, int *fg) +{ + int i; + for (i = 1; i * i < n && i <= 100; i++) + { + if (n % i == 0) + { + p[ind++] = i; + fg[i] = ind - 1; + p[ind++] = n / i; + fg[n / i] = ind - 1; + } + } + if (i * i == n && i <= 100) + { + p[ind++] = i; + fg[i] = ind - 1; + } +} +void Gen(int n) +{ + int i, j, k, a, b; + for (i = indv - 1; i >= 0; i--) + { + a = V[i].a * n; + b = V[i].b * n; + if (a <= N) + { + if (fa[a]) + { + j = fa[a]; + aa[j] = 1; + k = fb[V[i].b]; + if (Table[j][k] == 0) + { + V[indv].a = a; + V[indv++].b = V[i].b; + Table[j][k] = 1; + } + } + } + if (b <= M) + { + if (fb[b]) + { + j = fb[b]; + bb[j] = 1; + k = fa[V[i].a]; + if (Table[k][j] == 0) + { + V[indv].a = V[i].a; + V[indv++].b = b; + Table[k][j] = 1; + } + } + } + if (Table[1][1]) + { + break; + } + } +} +void Cal() +{ + int i, x, y; + GetDivisor(N, A, inda, fa); + GetDivisor(M, B, indb, fb); + x = fa[N]; + y = fb[M]; + Table[0][0] = 1; + V[indv].a = 1; + V[indv++].b = 1; + for (i = 2; i <= 100; i++) + { + Gen(i); + if (Table[x][y]) + { + printf("%d\n", N); + return; + } + } + if (!Table[0][y]) + { + printf("%d\n", N); + } + else + { + printf("%d\n", M); + } +} +void Free() +{ + int i, j, k, a, b; + for (i = 0; i < indv; i++) + { + a = V[i].a; + b = V[i].b; + j = fa[a]; + aa[j] = 0; + k = fb[b]; + bb[k] = 0; + Table[j][k] = 0; + } + for (i = 0; i < inda; i++) + { + a = A[i]; + fa[a] = 0; + } + for (i = 0; i < indb; i++) + { + a = B[i]; + fb[a] = 0; + } +} +int main() +{ + int temp; + while (scanf("%d%d", &N, &M) == 2) + { + inda = indb = indv = 0; + if (N < M) + { + temp = N; + N = M; + M = temp; + } + Cal(); + Free(); + } + return 0; +} diff --git a/654.cpp b/654.cpp new file mode 100644 index 0000000..7b22cd8 --- /dev/null +++ b/654.cpp @@ -0,0 +1,66 @@ +#include + +using namespace std; + +/** +654 +**/ +int N, D; +double Res; +void Cal() +{ + int i, j, st; + double temp, diff, pre; + Res = (double)N / (double)D; + diff = fabs(Res - 1); + st = 1; + for (i = 2; i; i++) + { + temp = fabs(Res - ((double)i / 1)); + if (temp > diff) + { + break; + } + if (diff > temp) + { + st = i; + diff = temp; + } + } + printf("%d/1\n", st); + for (i = 2; i; i++) + { + for (j = 1; j; j++) + { + temp = (double)j / (double)i; + if (fabs(temp - Res) < .0000001) + { + printf("%d/%d\n", j, i); + return; + } + pre = fabs(Res - temp); + if (pre < diff) + { + printf("%d/%d\n", j, i); + diff = pre; + } + else if (temp > Res) + { + break; + } + } + } +} +int main() +{ + int c = 0; + while (scanf("%d%d", &N, &D) == 2) + { + if (c++) + { + printf("\n"); + } + Cal(); + } + return 0; +} diff --git a/656.cpp b/656.cpp new file mode 100644 index 0000000..0481af5 --- /dev/null +++ b/656.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +const int N = 15; +const char tr[5][10] = {"ADD", "DIV", "DUP", "MUL", "SUB"}; + +int n, x[N], y[N]; + +struct State +{ + stack s; + int path[N], pathn; +} p; +queue Q; + +State tra(int way, State p) +{ + State q = p; + if (way == 0) + { + int a = q.s.top(); + q.s.pop(); + int b = q.s.top(); + q.s.pop(); + q.s.push(a + b); + } + if (way == 4) + { + int a = q.s.top(); + q.s.pop(); + int b = q.s.top(); + q.s.pop(); + q.s.push(b - a); + } + if (way == 3) + { + int a = q.s.top(); + q.s.pop(); + int b = q.s.top(); + q.s.pop(); + q.s.push(a * b); + } + if (way == 1) + { + int a = q.s.top(); + q.s.pop(); + int b = q.s.top(); + q.s.pop(); + q.s.push(b / a); + } + if (way == 2) + { + int a = q.s.top(); + q.s.push(a); + } + q.path[q.pathn++] = way; + return q; +} + +bool judge(State p) +{ + for (int i = 1; i < n; i++) + { + State q; + q.pathn = 0; + q.s.push(x[i]); + memset(q.path, 0, sizeof(q.path)); + for (int j = 0; j < p.pathn; j++) + { + if (p.path[j] == 1 && q.s.top() == 0 || q.s.top() > 30000 || q.s.top() < -30000) + { + return false; + } + q = tra(p.path[j], q); + } + if (q.s.top() != y[i]) + { + return false; + } + } + return true; +} + +bool bfs() +{ + while (!Q.empty()) + { + Q.pop(); + } + while (!p.s.empty()) + { + p.s.pop(); + } + p.s.push(x[0]); + p.pathn = 0; + memset(p.path, 0, sizeof(p.path)); + Q.push(p); + while (!Q.empty()) + { + p = Q.front(); + Q.pop(); + if (p.s.size() == 1 && p.s.top() == y[0]) + { + if (judge(p)) + { + return true; + } + } + for (int i = 0; i < 5; i++) + { + int t = p.s.size() - (10 - p.pathn); + if (i == 2 && t > 1) + { + continue; + } + if (p.s.size() == 1 && i != 2) + { + continue; + } + if (i == 1 && p.s.top() == 0) + { + continue; + } + if (p.pathn >= 10) + { + continue; + } + State q = tra(i, p); + if (q.s.top() >= -30000 && q.s.top() <= 30000) + { + Q.push(q); + } + } + } + return false; +} + +void solve() +{ + if (bfs()) + { + if (p.pathn == 0) + { + printf("Empty sequence\n"); + } + else + { + for (int i = 0; i < p.pathn - 1; i++) + { + printf("%s ", tr[p.path[i]]); + } + printf("%s\n", tr[p.path[p.pathn - 1]]); + } + } + else + { + printf("Impossible\n"); + } + printf("\n"); +} + +int main() +{ + int t = 0; + while (~scanf("%d", &n) && n) + { + for (int i = 0; i < n; i++) + { + scanf("%d", &x[i]); + } + for (int j = 0; j < n; j++) + { + scanf("%d", &y[j]); + } + printf("Program %d\n", ++t); + solve(); + } + return 0; +} diff --git a/657.cpp b/657.cpp new file mode 100644 index 0000000..9269126 --- /dev/null +++ b/657.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +enum +{ + MAX = 50 + 2 +}; + +int W, H;// width and height of image +char img[MAX][MAX]; +int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; + +bool is_inside(int x, int y) +{ + return (x >= 0 && x < H && y >= 0 && y < W); +} + +void print_debug() +{ + for (int i = 0; i < H; ++i) + printf("%s\n", img[i]); + puts("================================="); +} + +void dfs_dot(int x, int y) +{ + img[x][y] = '*'; + for (int i = 0; i < 4; ++i) + { + int nx = x + dir[i][0]; + int ny = y + dir[i][1]; + if (is_inside(nx, ny) && img[nx][ny] == 'X') + dfs_dot(nx, ny); + } +} + +int dfs_dice(int x, int y) +{ + img[x][y] = '.'; + int dotted_count = 0; + for (int i = 0; i < 4; ++i) + { + int nx = x + dir[i][0]; + int ny = y + dir[i][1]; + if (is_inside(nx, ny) && img[nx][ny] == 'X') + { + dfs_dot(nx, ny); + dotted_count++; + } + } + for (int i = 0; i < 4; ++i) + { + int nx = x + dir[i][0]; + int ny = y + dir[i][1]; + if (is_inside(nx, ny) && img[nx][ny] == '*') + dotted_count += dfs_dice(nx, ny); + } + return dotted_count; +} + +vector find_all_dices() +{ + vector dices; + for (int i = 0; i < H; ++i) + { + for (int j = 0; j < W; ++j) + { + if (img[i][j] == '*') + { + int dot = dfs_dice(i, j); + dices.push_back(dot); + } + } + } + sort(dices.begin(), dices.end()); + return dices; +} + +int main() +{ + int case_no = 1; + while (scanf("%d %d ", &W, &H) == 2) + { + if (W == 0 && H == 0) + break; + for (int i = 0; i < H; ++i) + scanf("%s", img[i]); + vector dices = find_all_dices(); + printf("Throw %d\n", case_no++); + for (int i = 0; i < dices.size() - 1; ++i) + printf("%d ", dices[i]); + printf("%d\n\n", dices.back()); + } + return 0; +} diff --git a/658.cpp b/658.cpp new file mode 100644 index 0000000..f3f332e --- /dev/null +++ b/658.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define MAXN 30 +#define MAXM 110 +#define MAXD 2000000 +#define INF 1000000000 +int s[2][MAXM], t[2][MAXM], w[MAXM], N, M; +char a[MAXN], b[MAXN]; +int q[MAXD], inq[MAXD], d[MAXD]; +int init() +{ + int i, j; + scanf("%d%d", &N, &M); + if (!N && !M) + { + return 0; + } + memset(s, 0, sizeof(s)); + memset(t, 0, sizeof(t)); + for (i = 0; i < M; i++) + { + scanf("%d%s%s", &w[i], a, b); + for (j = 0; j < N; j++) + { + if (a[j] == '+') + { + s[1][i] += (1 << j); + } + if (a[j] != '-') + { + s[0][i] += (1 << j); + } + if (b[j] == '+') + { + t[1][i] += (1 << j); + } + if (b[j] != '-') + { + t[0][i] += (1 << j); + } + } + } + return 1; +} +void SPFA() +{ + int i, j, k, MAX, front, rear, u, v; + MAX = 1 << N; + for (i = 0; i < MAX; i++) + { + d[i] = INF; + inq[i] = 0; + } + front = rear = 0; + d[MAX - 1] = 0; + q[rear++] = MAX - 1; + while (front != rear) + { + u = q[front++]; + if (front > MAX) + { + front = 0; + } + inq[u] = 0; + for (i = 0; i < M; i++) + if ((u | s[1][i]) == u && (u & s[0][i]) == u) + { + v = u; + v |= t[1][i]; + v &= t[0][i]; + if (d[u] + w[i] < d[v]) + { + d[v] = d[u] + w[i]; + if (!inq[v]) + { + q[rear++] = v; + if (rear > MAX) + { + rear = 0; + } + inq[v] = 1; + } + } + } + } + if (d[0] == INF) + { + printf("Bugs cannot be fixed.\n"); + } + else + { + printf("Fastest sequence takes %d seconds.\n", d[0]); + } +} +int main() +{ + int T = 0; + while (init()) + { + printf("Product %d\n", ++T); + SPFA(); + printf("\n"); + } + return 0; +} diff --git a/659.cpp b/659.cpp new file mode 100644 index 0000000..e309934 --- /dev/null +++ b/659.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +#define X 0 +#define Y 1 +#define ITERMAX 10 +#define EPS 1e-12 +typedef double Point[2]; +typedef struct Sphere +{ + Point center; + double radius; +} Sphere; +typedef double Vector[2]; +typedef struct Ray +{ + Point origin; + Vector direction; +} Ray; +double DOTPROD(double *a, double *b) +{ + return (a[0] * b[0] + a[1] * b[1]); +} +void NORMALIZE(double *a) +{ + double LAmag; + LAmag = 1. / sqrt(a[0] * a[0] + a[1] * a[1]); + a[0] *= LAmag; + a[1] *= LAmag; +} +int NOS; +Sphere s[30]; +double intersect(Sphere s, Ray r) +{ + double d, l2, m2, q, fRadius2, t1, t2; + Vector l; + l[X] = s.center[X] - r.origin[X]; + l[Y] = s.center[Y] - r.origin[Y]; + d = DOTPROD(l, r.direction); + if (d < 0) + { + return -1.0; + } + l2 = DOTPROD(l, l); + m2 = l2 - d * d; + fRadius2 = s.radius * s.radius; + if (m2 > fRadius2) + { + return -1.0; + } + q = sqrt(fRadius2 - m2); + t1 = d - q; + t2 = d + q; + if (t1 > 0) + { + return t1; + } + if (t2 > 0) + { + return t2; + } + return 0.0; +} +int intensity(Ray *ray, int depth) +{ + int Intersections = 0; + double Distances[30]; + int NearestIntersection; + double NearestDistance; + int i; + Point sp; + Vector Normal; + Ray Reflect; + double c; + double Abstand; + NORMALIZE(ray->direction); + for (i = 0; i < NOS; i++) + { + Abstand = intersect(s[i], *ray); + Distances[i] = Abstand; + if (Abstand > EPS) + { + Intersections++; + } + } + if (depth > ITERMAX && Intersections) + { + printf("..."); + return 0; + } + if (Intersections > 0) + { + NearestIntersection = -1; + NearestDistance = 0; + for (i = 0; i < NOS; i++) + { + if (Distances[i] > EPS) + { + if (Distances[i] < NearestDistance || NearestIntersection == -1) + { + NearestIntersection = i; + NearestDistance = Distances[i]; + } + } + } + printf("%d ", NearestIntersection + 1); + sp[X] = ray->origin[X] + ray->direction[X] * NearestDistance; + sp[Y] = ray->origin[Y] + ray->direction[Y] * NearestDistance; + Normal[X] = sp[X] - s[NearestIntersection].center[X]; + Normal[Y] = sp[Y] - s[NearestIntersection].center[Y]; + NORMALIZE(Normal); + Reflect.origin[X] = sp[X] + Normal[X] * EPS; + Reflect.origin[Y] = sp[Y] + Normal[Y] * EPS; + c = -DOTPROD(Normal, ray->direction); + Reflect.direction[X] = ray->direction[X] + (2.0 * Normal[X] * c); + Reflect.direction[Y] = ray->direction[Y] + (2.0 * Normal[Y] * c); + return intensity(&Reflect, depth + 1); + } + else + { + printf("inf"); + return 0; + } + return 0; +} +int main() +{ + int i, x, y, rad, dx, dy; + int scene = 0; + Ray r; + while (scanf("%d\n", &NOS) && NOS) + { + scene++; + printf("Scene %d\n", scene); + for (i = 0; i < NOS; i++) + { + scanf("%d %d %d", &x, &y, &rad); + s[i].center[X] = x; + s[i].center[Y] = y; + s[i].radius = rad; + } + scanf("%d %d %d %d", &x, &y, &dx, &dy); + r.origin[X] = x; + r.origin[Y] = y; + r.direction[X] = dx; + r.direction[Y] = dy; + intensity(&r, 1); + printf("\n\n"); + } + return 0; +} diff --git a/660.cpp b/660.cpp new file mode 100644 index 0000000..68da8c7 --- /dev/null +++ b/660.cpp @@ -0,0 +1,137 @@ +#include + +using namespace std; + +#define MAXROBOTS 20 +#define MAXREQUESTS 5000 + +const int debug = 0; + +typedef struct {int time, origin, dest, weight, done;} request; +request r[MAXREQUESTS]; + +typedef struct {int idle, loaded, pos, wait, util, req, origin, dest, capacity;} transrob; +transrob t[MAXROBOTS]; + +int m, n, rnum, beg, end_, case_no = 1; +FILE *inp; + +int read_data() +{ + int i; + scanf("%d %d", &n, &m); + if (n == 0 && m == 0) + { + return 0; + } + for (i = 0; i < m; i++) + { + t[i].idle = 1; + t[i].pos = 0; + t[i].util = 0; + scanf("%d", &t[i].capacity); + } + for (rnum = 0;; rnum++) + { + scanf("%d %d %d %d", &r[rnum].time, &r[rnum].origin, &r[rnum].dest, &r[rnum].weight); + if (r[rnum].time == -1) + { + break; + } + r[rnum].origin--; + r[rnum].dest--; + r[rnum].done = 0; + } + return 1; +} + +int assign_requests(int tim) +{ + int ass, i, j, tr, dist; + ass = 0; + for (i = 0; i < rnum && r[i].time <= tim; i++) + if (r[i].done == 0) + { + tr = -1; + dist = n + 1; + for (j = 0; j < m; j++) + if (t[j].idle && t[j].capacity >= r[i].weight && + (r[i].origin - t[j].pos + n) % n < dist) + { + tr = j; + dist = (r[i].origin - t[j].pos + n) % n; + } + if (tr != -1) + { + ass++; + t[tr].idle = 0; + t[tr].wait = (r[i].origin - t[tr].pos + n) % n + + (r[i].dest - r[i].origin + n) % n + 2 * 5; + t[tr].util += t[tr].wait; + t[tr].pos = r[i].dest; + r[i].done = tim + t[tr].wait; + } + } + return ass; +} + +void move_robot(int rob) +{ + if (t[rob].idle) + { + return; + } + t[rob].wait--; + if (t[rob].wait == 0) + { + t[rob].idle = 1; + } +} + +void do_simulation() +{ + int i, reqleft, idlenum; + reqleft = rnum; + idlenum = m; + beg = r[0].time; + end_ = beg; + while (reqleft > 0 || idlenum < m) + { + reqleft -= assign_requests(end_++); + idlenum = 0; + for (i = 0; i < m; i++) + { + move_robot(i); + idlenum += t[i].idle; + } + } +} + +void output_stats() +{ + int i, s1, s2; + printf("Simulation %d\n", case_no++); + s1 = s2 = 0; + for (i = 0; i < rnum; i++) + { + s1 += r[i].done - r[i].time; + } + for (i = 0; i < m; i++) + { + s2 += t[i].util; + } + printf("Average wait time = %.3f minutes\n", (double)s1 / (double)rnum); + printf("Average utilization = %.3f %%\n", + 100.0 * (double)s2 / ((double)(end_ - beg) * (double)m)); + printf("\n"); +} + +int main() +{ + while (read_data()) + { + do_simulation(); + output_stats(); + } + return 0; +} diff --git a/661.cpp b/661.cpp index 558ae66..9d75ced 100644 --- a/661.cpp +++ b/661.cpp @@ -1,75 +1,64 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -struct Switch{ - bool S; - int V; -}; +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - for (int t = 1; ; t++){ - int N, M, C; - cin >> N >> M >> C; - if( N == 0 && M == 0 && C == 0 ) +int main() +{ + unsigned long *device; + bool *state; + unsigned long n, m, c, d; + unsigned long cases = 0; + while (cin >> n >> m >> c) + { + if (n == 0 && m == 0 && c == 0) + { break; - int curr = 0, maxm = -1; - bool fuse = false; - Switch swit[N]; - int i, j; - FOI(i, 0, N-1){ - cin >> swit[i].V; - swit[i].S = false; } - while( M-- ){ - int P; - cin >> P; - --P; - swit[P].S = !swit[P].S; - if( !swit[P].S ) - curr -= swit[P].V; + device = new unsigned long[n]; + state = new bool[n]; + for (unsigned long i = 0; i < n; i++) + { + state[i] = false; + } + for (unsigned long i = 0; i < n; i++) + { + cin >> device[i]; + } + unsigned long max = 0; + unsigned long cur = 0; + while (m--) + { + cin >> d; + d--; + if (state[d]) + { + state[d] = false; + cur -= device[d]; + } else - curr += swit[P].V; - maxm = max(curr, maxm); - if( maxm > C ) - fuse = true; + { + state[d] = true; + cur += device[d]; + if (cur > max) + { + max = cur; + } + } + } + cases++; + cout << "Sequence " << cases << endl; + if (max > c) + { + cout << "Fuse was blown." << endl; } - cout << "Sequence " << t << "\n"; - if( fuse ) - cout << "Fuse was blown.\n"; - else{ - cout << "Fuse was not blown.\n"; - cout << "Maximal power consumption was " << maxm << " amperes.\n"; + else + { + cout << "Fuse was not blown." << endl; + cout << "Maximal power consumption was " << max << " amperes." << endl; } - cout << "\n"; + cout << endl; + delete[] device; + delete[] state; } return 0; } diff --git a/662.cpp b/662.cpp new file mode 100644 index 0000000..dee424f --- /dev/null +++ b/662.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +const int MAXN = 200 + 5, MAXK = 30 + 5, INF = 1 << 30; +int T = 0, n, k, d[MAXN], dis_sum[MAXN][MAXN], pos[MAXN][MAXN]; +struct DP +{ + int v, s, t, depot; +} dp[MAXN][MAXN]; + +int Dis_Sum(int x, int y) +{ + int &res = dis_sum[x][y]; + if (res >= 0) + return res; + res = INF; + for (int i = x; i <= y; i++) + { + int tmp = 0; + for (int j = x; j <= y; j++) + tmp += (int)abs((double)(d[j] - d[i])); + if (tmp < res) + res = tmp, pos[x][y] = i; + } + return res; +} +void print_ans(int x, int y) +{ + if (!x) + return; + DP &d = dp[x][y]; + int count = 0; + for (int i = d.s; i <= d.t; i++) + count++; + print_ans(x - 1, y - count); + if (count > 1) + printf("Depot %d at restaurant %d serves restaurants %d to %d\n", x, d.depot, d.s, d.t); + else + printf("Depot %d at restaurant %d serves restaurant %d\n", x, d.depot, d.s); +} + +int main() +{ + while (scanf("%d%d", &n, &k)) + { + if (!n && !k) + break; + T++; + for (int i = 1; i <= n; i++) + scanf("%d", &d[i]); + memset(dp, 0, sizeof(dp)); + memset(dis_sum, -1, sizeof(dis_sum)); + for (int i = 1; i <= n; i++) + dp[0][i].v = INF; + for (int i = 1; i <= k; i++) + for (int j = i; j <= n; j++) + { + dp[i][j].v = INF; + for (int k = j; k >= 1; k--) + if (dp[i - 1][k - 1].v + Dis_Sum(k, j) < dp[i][j].v) + { + dp[i][j].v = dp[i - 1][k - 1].v + Dis_Sum(k, j); + dp[i][j].depot = pos[k][j]; + dp[i][j].s = k; + dp[i][j].t = j; + } + } + printf("Chain %d\n", T); + print_ans(k, n); + printf("Total distance sum = %d\n", dp[k][n].v); + printf("\n"); + } + return 0; +} diff --git a/663.cpp b/663.cpp new file mode 100644 index 0000000..18a2573 --- /dev/null +++ b/663.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +bool used[1000]; +int llink[1000], rlink[1000]; +vector toNxt[1000]; + +bool dfs(int now, int u, int v) +{ + for (int nxt : toNxt[now]) + { + if (used[nxt] || (now == u && nxt == v)) + continue; + + used[nxt] = true; + if (rlink[nxt] == -1 || dfs(rlink[nxt], u, v) == true) + { + llink[now] = nxt; + rlink[nxt] = now; + return true; + } + } + return false; +} + +int bipartite(int n, int u, int v) +{ + fill(llink, llink + n, -1); + fill(rlink, rlink + n, -1); + int num = 0; + for (int i = 0; i < n; ++i) + { + fill(used, used + n, false); + if (dfs(i, u, v)) + { + ++num; + } + } + return num; +} + +int main() +{ + int n, Case = 1, Xmin[1000], Xmax[1000], Ymin[1000], Ymax[1000], X, Y; + while (scanf("%d", &n) && n) + { + for (auto &v : toNxt) + { + v.clear(); + } + + for (int i = 0; i < n; ++i) + { + scanf("%d%d%d%d", &Xmin[i], &Xmax[i], &Ymin[i], &Ymax[i]); + } + for (int i = 0; i < n; ++i) + { + scanf("%d%d", &X, &Y); + for (int j = 0; j < n; ++j) + { + if (Xmin[j] <= X && X <= Xmax[j] && Ymin[j] <= Y && Y <= Ymax[j]) + { + toNxt[j].push_back(i); + } + } + } + + int result = bipartite(n, -1, -1); + int ans[1000]; + for (int i = 0; i < n; ++i) + { + ans[i] = llink[i]; + } + + printf("Heap %d\n", Case++); + bool ok = false; + for (int i = 0; i < n; ++i) + { + if (bipartite(n, i, ans[i]) < result) + { + if (ok) + { + putchar(' '); + } + printf("(%c,%d)", (i + 'A'), ans[i] + 1); + ok = true; + } + } + if (ok == false) + { + printf("none"); + } + printf("\n\n"); + } + return 0; +} diff --git a/664.cpp b/664.cpp new file mode 100644 index 0000000..fb089b1 --- /dev/null +++ b/664.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +double matrix[26][27], temp[27]; +char str[1000]; +int n, len; +double DFS(int left, int right) +{ + double t[27]; + double count = 0; + memset(t, 0, sizeof(t)); + for (int ptr = left + 1; ptr < right; ptr++) + { + if (str[ptr] == ' ') + { + continue; + } + if (str[ptr] >= 'a' && str[ptr] <= 'z') + { + t[str[ptr] - 'a'] += 1.0; + count++; + } + else if (str[ptr] == '(') + { + int s, match = 1; + for (s = ptr + 1; match > 0; s++) + { + if (str[s] == '(') + { + match++; + } + if (str[s] == ')') + { + match--; + } + } + double num = DFS(ptr, s); + for (int i = 0; i < 27; i++) + { + t[i] += temp[i] / num; + } + ptr = s; + count++; + } + else if (str[ptr] != ')') + { + bool f = true; + if (str[ptr] == '-') + { + f = false; + ptr++; + } + double x = 0; + int y; + for (y = ptr; str[y] >= '0' && str[y] <= '9'; y++) + { + x = x * 10 + str[y] - '0'; + } + if (!f) + { + x *= -1; + } + t[n] += x; + ptr = y; + count++; + } + } + for (int i = 0; i < 27; i++) + { + temp[i] = t[i]; + } + return count; +} +void solve() +{ + bool s[26]; + memset(s, true, sizeof(s)); + for (int i = 0; i < n; i++) + { + if (matrix[i][i]) + { + double t = matrix[i][i]; + for (int j = i; j <= n; j++) + { + matrix[i][j] /= t; + } + for (int j = 0; j < n; j++) + { + if (i == j) + { + continue; + } + double b = matrix[j][i] / matrix[i][i]; + for (int k = i; k <= n; k++) + { + matrix[j][k] -= matrix[i][k] * b; + } + } + } + else + { + s[i] = false; + for (int j = 0; j < n; j++) + if (j != i && matrix[j][i]) + { + s[j] = false; + } + } + } + for (int i = 0; i < n; i++) + { + if (s[i]) + { + printf("Expected score for %c = %.3lf\n", i + 'a', matrix[i][n] / matrix[i][i]); + } + else + { + printf("Expected score for %c undefined\n", i + 'a'); + } + } +} +int main() +{ + int caseno = 1; + while (gets(str) != NULL) + { + if (strcmp(str, "0") == 0) + { + break; + } + printf("Game %d\n", caseno++); + sscanf(str, "%d", &n); + memset(matrix, 0, sizeof(matrix)); + for (int i = 0; i < n; i++) + { + gets(str); + matrix[i][str[0] - 'a'] = 1.0; + len = strlen(str); + int s; + for (s = 0; str[s] != '('; s++) + ; + double num = DFS(s, len - 1); + for (int j = 0; j < n; j++) + { + matrix[i][j] -= temp[j] / num; + } + matrix[i][n] = temp[n] / num; + } + solve(); + puts(""); + } + return 0; +} diff --git a/665.cpp b/665.cpp new file mode 100644 index 0000000..3bf85c4 --- /dev/null +++ b/665.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +void is_equal(int *array, int *buf, int n, int p) +{ + int i; + for (i = 0; i < 2 * p; i++) + { + array[buf[i]] = 1; + } +} + +void not_equal(int *array, int *buf, int n, int p) +{ + int i; + for (i = 0; i < 2 * p; i++) + if (array[buf[i]] != 1) + { + array[buf[i]] = 2; + } +} + +int main() +{ + int n, k, p, array[200], i, t, buf[200]; + char c; + int a, b; + cin >> a; + int f = 0; + for (b = 0; b < a; b++) + { + if (f) + { + cout << endl; + } + f = 1; + cin >> n >> k; + memset(array, 0, sizeof(array)); + for (t = 0; t < k; t++) + { + cin >> p; + for (i = 0; i < 2 * p; i++) + { + cin >> buf[i]; + } + cin >> c; + if (c == '=') + { + is_equal(array, buf, n, p); + } + if (c == '>' || c == '<') + { + not_equal(array, buf, n, p); + } + } + int cnt_0 = 0, cnt_2 = 0; + int flag_0, flag_2; + for (i = 1; i <= n; i++) + { + if (array[i] == 0) + { + cnt_0++; + flag_0 = i; + } + if (array[i] == 2) + { + cnt_2++; + flag_2 = i; + } + } + if (cnt_2 > 1) + { + cout << 0 << endl; + continue; + } + if (cnt_2 == 0 && cnt_0 > 1) + { + cout << 0 << endl; + continue; + } + if (cnt_0 == 1 && cnt_2 == 0) + { + cout << flag_0 << endl; + continue; + } + if (cnt_2 == 1) + { + cout << flag_2 << endl; + } + } + return 0; +} diff --git a/667.cpp b/667.cpp new file mode 100644 index 0000000..17896d9 --- /dev/null +++ b/667.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +#define max(a, b) (((a) > (b)) ? a : b) +#define min(a, b) (((a) < (b)) ? a : b) + +typedef struct +{ + double x, y; +} point; + +int n, multi, i; +double k, h; +point pp[200]; +double len[200]; + +const double Precision = 1e-9; + +#define Pi 3.1415926535897931 + +int init() +{ + int i; + memset(pp, 0, sizeof(pp)); + cin >> k >> h; + if (k < 0 && h < 0) + { + return 0; + } + cin >> n; + for (i = 0; i < n; i++) + { + cin >> pp[i].x >> pp[i].y; + len[i] = sqrt(pp[i].x * pp[i].x + pp[i].y * pp[i].y); + } + pp[n] = pp[0]; + len[n] = len[0]; + return 1; +} + +int dblcmp(double d) +{ + return (d > 0) ? 1 : -1; +} + +double cross(point a, point b) +{ + return a.x * b.y - a.y * b.x; +} + +double dot(point a, point b) +{ + return a.x * b.x + a.y * b.y; +} + +void process() +{ + int i; + double l, r, m, dir, the, newm, delta; + l = r = m = 0; + pp[n] = pp[0]; + for (i = 1; i <= n; i++) + { + dir = dblcmp(cross(pp[i], pp[i - 1])); + the = dot(pp[i], pp[i - 1]) / len[i] / len[i - 1]; + if (fabs(the) > 1) + { + the = dblcmp(the); + } + the = acos(the); + newm = m + dir * the; + l = min(l, newm); + r = max(r, newm); + m = newm; + } + delta = min(2 * Pi, r - l); + printf("%.2lf\n", delta * k * h); +} + +int main() +{ + cin >> multi; + for (i = 0; i < multi; i++) + { + if (init()) + { + process(); + } + else + { + i--; + } + if (i < multi - 1) + { + printf("\n"); + } + } + return 0; +} diff --git a/668.cpp b/668.cpp new file mode 100644 index 0000000..9d9e3e3 --- /dev/null +++ b/668.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +void solve(int n) +{ + int x = (int)floor(sqrt(8 * n + 9) - 3) / 2; + int discard = (x + 1) * (4 + x) / 2 - n; + int i; + int p[10000], num = 0; + if (discard != 1) + { + for (i = 2; i <= x + 2; i++) + { + if (i != discard) + { + p[num++] = i; + } + } + for (i = 0; i < num - 1; i++) + { + cout << p[i] << ' '; + } + cout << p[num - 1] << endl; + } + else + { + for (i = 3; i <= x + 2; i++) + { + p[num++] = i; + } + p[num - 1]++; + for (i = 0; i < num - 1; i++) + { + cout << p[i] << ' '; + } + cout << p[num - 1] << endl; + } +} +int main() +{ + int N; + int i, t; + cin >> t; + int f = 0; + for (i = 0; i < t; i++) + { + if (f) + { + cout << endl; + } + f = 1; + cin >> N; + solve(N); + } + return 0; +} diff --git a/669.cpp b/669.cpp new file mode 100644 index 0000000..a054307 --- /dev/null +++ b/669.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +int in[10001], out[10001], N, K, S, p; +void solve() +{ + int moves = 0, free; + for (int i = 1; i <= p; i++) + { + if (in[i] == 0) + { + int j = i; + while (out[j]) + { + printf("%d %d\n", out[j], j); + int t = out[j]; + out[j] = 0; + j = t; + moves++; + } + } + } + for (free = N; free > 0 && out[free] != 0; free--) + ; + for (int i = 1; i <= p; i++) + if (out[i] != 0 && in[i] != i) + { + printf("%d %d\n", i, free); + moves++; + out[in[i]] = free; + out[free] = 0; + int j = i; + while (out[j]) + { + printf("%d %d\n", out[j], j); + int t = out[j]; + out[j] = 0; + j = t; + } + } + if (moves == 0) + { + puts("No optimization needed"); + } +} +int main() +{ + int t; + scanf("%d", &t); + for (int c = 0; c < t; c++) + { + if (c) + { + puts(""); + } + p = 1; + scanf("%d %d", &N, &K); + for (int i = 1; i <= N; i++) + { + in[i] = out[i] = 0; + } + for (int i = 0; i < K; i++) + { + scanf("%d", &S); + for (int j = 0; j < S; j++, p++) + { + int t; + scanf("%d", &t); + in[t] = p; + out[p] = t; + } + } + solve(); + } + return 0; +} diff --git a/670.cpp b/670.cpp new file mode 100644 index 0000000..313406e --- /dev/null +++ b/670.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +typedef struct +{ + int x, y; +} pt_t; +double hypot(double, double); + +int r[256][256], v; + +int maxflow() +{ + static int s[256], p[256]; + int i, j, f, t; + for (f = 0;; f++) + { + for (i = 1; i <= v; i++) + { + p[i] = 0; + } + for (s[0] = 1, t = 1; t > 0 && p[2] == 0;) + { + for (i = s[--t], j = 1; j <= v; j++) + { + if (r[i][j] && p[j] == 0) + { + p[j] = i; + s[t++] = j; + } + } + } + if (p[2] == 0) + { + return f; + } + for (i = 2; i != 1; i = p[i]) + { + r[p[i]][i] = 0; + r[i][p[i]] = 1; + } + } +} + +int main() +{ + static int l1[256], l2[256], n, m, t; + static pt_t p[128], q[128]; + int i, j; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &m) == 2;) + { + n--; + for (i = 0; i <= n; i++) + { + scanf("%d %d", &p[i].x, &p[i].y); + } + for (i = 0; i < m; i++) + { + scanf("%d %d", &q[i].x, &q[i].y); + } + for (v = 2, i = 0; i < n; i++) + { + l1[i] = ++v; + } + for (i = 0; i < m; i++) + { + l2[i] = ++v; + } + for (i = 1; i <= v; i++) + for (j = 1; j <= v; j++) + { + r[i][j] = 0; + } + for (i = 0; i < n; i++) + { + for (r[1][l1[i]] = 1, j = 0; j < m; j++) + { + double d; + d = hypot(p[i].x - q[j].x, p[i].y - q[j].y) + + hypot(q[j].x - p[i + 1].x, q[j].y - p[i + 1].y) - 2. * hypot(p[i].x - p[i + 1].x, p[i].y - p[i + 1].y); + if (d < 1e-10) + { + r[l1[i]][l2[j]] = 1; + } + } + } + for (i = 0; i < m; i++) + { + r[l2[i]][2] = 1; + } + i = maxflow(); + printf("%d\n", n + 1 + i); + for (i = 0; i < n; i++) + { + printf("%d %d ", p[i].x, p[i].y); + for (j = 0; j < m; j++) + if (r[l2[j]][l1[i]]) + { + break; + } + if (j < m) + { + printf("%d %d ", q[j].x, q[j].y); + } + } + printf("%d %d\n", p[n].x, p[n].y); + if (t > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/671.cpp b/671.cpp new file mode 100644 index 0000000..3ad9b1d --- /dev/null +++ b/671.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define MAXN 10005 + +char dic[MAXN][16]; +int st[1000]; +int wd, ind; + +int in_del(char bb[], char ss[]) +{ + int i, j, p = 0, d = 0, f; + for (i = 0; ss[i]; i++) + { + f = 1; + for (j = p; bb[j]; j++) + { + if (ss[i] == bb[j]) + { + f = 0; + p = j + 1; + break; + } + } + if (f) + return 0; + } + return 1; +} +int Rep(char bb[], char ss[]) +{ + int i, d; + d = 0; + for (i = 0; bb[i]; i++) + { + if (bb[i] != ss[i]) + d++; + if (d > 1) + return 0; + } + return 1; +} +void Cal(char ss[]) +{ + int i; + int l1, l2; + l1 = strlen(ss); + ind = 0; + for (i = 0; i < wd; i++) + { + l2 = strlen(dic[i]); + if (abs(l1 - l2) > 1) + continue; + if (!strcmp(ss, dic[i])) + { + printf("%s is correct\n", ss); + return; + } + if ((l1 == l2) && Rep(ss, dic[i])) + st[ind++] = i; + if ((l1 > l2) && in_del(ss, dic[i])) + st[ind++] = i; + if ((l1 < l2) && in_del(dic[i], ss)) + st[ind++] = i; + } + printf("%s:", ss); + for (i = 0; i < ind; i++) + printf(" %s", dic[st[i]]); + printf("\n"); +} + +int main() +{ + int kase; + char temp[20]; + scanf("%d", &kase); + while (kase--) + { + wd = 0; + while (1) + { + scanf("%s", dic[wd]); + if (!strcmp(dic[wd], "#")) + break; + wd++; + } + while (1) + { + scanf("%s", temp); + if (!strcmp(temp, "#")) + break; + Cal(temp); + } + if (kase) + { + putchar('\n'); + } + } + return 0; +} diff --git a/672.cpp b/672.cpp new file mode 100644 index 0000000..66d06ac --- /dev/null +++ b/672.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +struct Ganster +{ + int t, p, s; +} p[101]; +int g[101][101]; +int main() +{ + char str[5000], *s; + int N, K, T, ptr; + int times; + gets(str); + sscanf(str, "%d", ×); + gets(str); + for (; times; times--) + { + for (int i = 0; i < 101; i++) + for (int j = 0; j < 101; j++) + { + g[i][j] = -999999; + } + g[0][0] = 0; + gets(str); + sscanf(str, "%d %d %d", &N, &K, &T); + gets(str); + ptr = 0; + for (s = strtok(str, " "); s; s = strtok(NULL, " ")) + { + p[++ptr].t = atoi(s); + } + gets(str); + ptr = 0; + for (s = strtok(str, " "); s; s = strtok(NULL, " ")) + { + p[++ptr].p = atoi(s); + } + gets(str); + ptr = 0; + for (s = strtok(str, " "); s; s = strtok(NULL, " ")) + { + p[++ptr].s = atoi(s); + } + p[0].t = 0; + for (int i = 1; i <= N; i++) + { + for (int j = i + 1; j <= N; j++) + { + if (p[i].t > p[j].t) + { + Ganster t = p[i]; + p[i] = p[j]; + p[j] = t; + } + } + } + for (int i = 1; i <= N; i++) + { + int det = p[i].t - p[i - 1].t; + for (int j = 0; j <= K; j++) + { + int min = j - det; + int max = j + det; + if (min < 0) + { + min = 0; + } + if (max > K) + { + max = K; + } + for (int last = min; last <= max; last++) + if (g[i - 1][last] > g[i][j]) + { + g[i][j] = g[i - 1][last]; + } + if (p[i].s == j) + { + g[i][j] += p[i].p; + } + } + } + int best = 0; + for (int i = 0; i <= K; i++) + if (g[N][i] > best) + { + best = g[N][i]; + } + printf("%d\n", best); + gets(str); + if (times != 1) + { + puts(""); + } + } +} diff --git a/673.cpp b/673.cpp index afc603c..3f3f2e7 100644 --- a/673.cpp +++ b/673.cpp @@ -1,78 +1,62 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -int main(){ - char temp[200]; - cin.getline(temp, 200); - int test=atoi(temp); - while(test--){ - stack stak; - string str; - getline(cin, str, '\n'); - int len = str.length(), i; - bool flag = true; - FOI(i, 0, len-1){ - if(str[i]=='(' || str[i]=='[') - stak.push(str[i]); - else if(str[i]==')' || str[i]==']'){ - if(stak.empty()){ - flag = false; - break; - } - else if(str[i]==')'){ - if(stak.top()=='(') - stak.pop(); - else{ - flag = false; - break; - } - } - else if(str[i]==']'){ - if(stak.top()=='[') - stak.pop(); - else{ - flag = false; - break; - } - } - } - } - if(!stak.empty() || !flag) - cout<<"No\n"; - else - cout<<"Yes\n"; - } - return 0; -} +#include + +using namespace std; + +typedef unsigned long long ULL; +typedef long long LL; + +int main() +{ + int T; + cin >> T; + cin.ignore(); + while (T--) + { + string str; + getline(cin, str); + int counter = 0; + stack S; + bool stat = true; + while (counter < str.length()) + { + if (str[counter] == '(' || str[counter] == '[') + { + S.push(str[counter]); + } + else if (str[counter] == ')') + { + if (S.empty() || S.top() != '(') + { + stat = false; + break; + } + else if (S.top() == '(') + { + S.pop(); + } + } + else if (str[counter] == ']') + { + if (S.empty() || S.top() != '[') + { + stat = false; + break; + } + else if (S.top() == '[') + { + S.pop(); + } + } + counter++; + } + if (stat && S.empty()) + { + cout << "Yes" << endl; + } + else + { + cout << "No" << endl; + } + } + return 0; +} diff --git a/674.cpp b/674.cpp new file mode 100644 index 0000000..f2d5bb0 --- /dev/null +++ b/674.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +const int MAXSIZE = 7500; +const int coin[] = {1, 5, 10, 25, 50}; + +int dp[MAXSIZE]; + +void init() +{ + int i, j; + memset(dp, 0, sizeof(dp)); + dp[0] = 1; + for (i = 0; i < 5; i++) + for (j = 1; j < MAXSIZE; j++) + if (j >= coin[i]) + { + dp[j] += dp[j - coin[i]]; + } +} + +int main() +{ + init(); + int val; + while (EOF != scanf("%d", &val)) + { + printf("%d\n", dp[val]); + } + return 0; +} diff --git a/675.cpp b/675.cpp new file mode 100644 index 0000000..8ff13f9 --- /dev/null +++ b/675.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +struct point +{ + int x, y; + point(int _x = 0, int _y = 0) : x(_x), y(_y) {} + int operator*(const point &p) const + { + return x * p.y - y * p.x; + } + point operator-(const point &p) const + { + return point(x - p.x, y - p.y); + }; + point operator+(const point &p) const + { + return point(x + p.x, y + p.y); + }; + bool operator<(const point &p) const + { + return x < p.x || (x == p.x && y < p.y); + } + bool operator==(const point &p) const + { + return x == p.x && y == p.y; + } + bool operator!=(const point &p) const + { + return !(*this == p); + } +}; + +int cross(const point &O, const point &A, const point &B) +{ + return (A - O) * (B - O); +} + +vector convex_hull(vector P) +{ + int n = P.size(), k = 0; + vector r(2 * n); + sort(P.begin(), P.end()); + // Build lower hull + for (int i = 0; i < n; ++i) + { + while (k >= 2 && cross(r[k - 2], r[k - 1], P[i]) <= 0) + { + --k; + } + r[k++] = P[i]; + } + // Build upper hull + for (int i = n - 2, t = k + 1; i >= 0; --i) + { + while (k >= t && cross(r[k - 2], r[k - 1], P[i]) <= 0) + { + --k; + } + r[k++] = P[i]; + } + r.resize(k - 1); + return r; +} + +int main() +{ + char line[1024]; + for (bool first = true; gets(line); first = false) + { + if (!first) + { + puts(""); + } + int x, y; + vector ans, p; + do + { + if (sscanf(line, "%d, %d", &x, &y) != 2) + { + break; + } + p.push_back(point(x, y)); + } while (gets(line)); + ans = convex_hull(p); + int sz = ans.size(), start_at = -1, z = p.size(); + for (int i = 0, j; i < sz; ++i) + { + for (j = 0; p[j] != ans[i]; ++j) + ; + if (j < z) + { + start_at = i, z = j; + } + } + for (int i = 0; i <= sz; ++i) + { + point pans = ans[(start_at + i) % sz]; + printf("%d, %d\n", pans.x, pans.y); + } + } + return 0; +} diff --git a/676.cpp b/676.cpp new file mode 100644 index 0000000..dfebf39 --- /dev/null +++ b/676.cpp @@ -0,0 +1,142 @@ +#include + +using namespace std; + +bool v[10][10]; +int s[2], e[2], ans[50001][2]; +int len; +bool DFS(int r, int c) +{ + ans[len][0] = r; + ans[len++][1] = c; + if (len > 50000) + { + return false; + } + v[r][c] = true; + if (r == e[0] && c == e[1]) + { + for (int i = 0; i < len; i++) + { + if (i) + { + printf(", "); + } + printf("(%d,%d)", ans[i][0], ans[i][1]); + } + printf("\n"); + return true; + } + else + { + if (r > 1 && c < 9 && !v[r - 1][c + 1]) + { + if (DFS(r - 1, c + 1)) + { + return true; + } + if (len > 50000) + { + return false; + } + ans[len][0] = r; + ans[len][1] = c; + if (++len > 50000) + { + return false; + } + } + if (r < 9 && c < 9 && !v[r + 1][c + 1]) + { + if (DFS(r + 1, c + 1)) + { + return true; + } + if (len > 50000) + { + return false; + } + ans[len][0] = r; + ans[len][1] = c; + if (++len > 50000) + { + return false; + } + } + if (r < 9 && c > 1 && !v[r + 1][c - 1]) + { + if (DFS(r + 1, c - 1)) + { + return true; + } + if (len > 50000) + { + return false; + } + ans[len][0] = r; + ans[len][1] = c; + if (++len > 50000) + { + return false; + } + } + if (r > 1 && c > 1 && !v[r - 1][c - 1]) + { + if (DFS(r - 1, c - 1)) + { + return true; + } + if (len > 50000) + { + return false; + } + ans[len][0] = r; + ans[len][1] = c; + if (++len > 50000) + { + return false; + } + } + } + v[r][c] = false; + return false; +} +int main() +{ + int t; + char str[100], c; + scanf("%d", &t); + for (int ca = 0; ca < t; ca++) + { + if (ca) + { + puts(""); + } + while ((c = getchar()) != '(') + ; + scanf("%d", &s[0]); + while ((c = getchar()) != ',') + ; + scanf("%d", &s[1]); + while ((c = getchar()) != '(') + ; + scanf("%d", &e[0]); + while ((c = getchar()) != ',') + ; + scanf("%d", &e[1]); + if ((s[0] + s[1]) % 2 != (e[0] + e[1]) % 2) + { + puts("fail"); + } + else + { + len = 0; + memset(v, false, sizeof(v)); + if (!DFS(s[0], s[1])) + { + puts("more than 50000 steps"); + } + } + } + return 0; +} diff --git a/677.cpp b/677.cpp new file mode 100644 index 0000000..4549497 --- /dev/null +++ b/677.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define MAXN 15 +char Link[MAXN][MAXN]; +struct SS +{ + char cl; + int p; +} V[MAXN]; +int N, L, F; +void Print(int st, int en) +{ + if (st == en) + { + printf("%d", en); + return; + } + Print(st, V[en].p); + printf(",%d", en); +} +void DFS(int n, int level) +{ + V[n].cl = 'r'; + if (level == L) + { + printf("("); + Print(1, n); + printf(")\n"); + F = 1; + return; + } + for (int i = 1; i <= N; i++) + { + if (Link[n][i] == 1 && V[i].cl == 'w') + { + V[i].p = n; + DFS(i, level + 1); + V[i].cl = 'w'; + } + } +} +void Solvedcase() +{ + int i; + for (i = 1; i <= N; i++) + { + V[i].cl = 'w'; + } + F = 0; + DFS(1, 0); + if (F == 0) + { + printf("no walk of length %d\n", L); + } +} +void Readcase() +{ + int i, j; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + scanf("%d", &Link[i][j]); + } + } +} +int main() +{ + int fal = 0; + while (scanf("%d%d", &N, &L) == 2) + { + Readcase(); + if (fal != 0) + { + printf("\n"); + } + Solvedcase(); + scanf("%d", &fal); + for (int i = 0; i < MAXN - 1; i++) + for (int j = 0; j < MAXN; j++) + { + Link[i][j] = 0; + } + } + return 0; +} diff --git a/679.cpp b/679.cpp new file mode 100644 index 0000000..cbb96c9 --- /dev/null +++ b/679.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +/** +679 +**/ +int D, L, P; +void Cal() +{ + int i; + P = 1; + for (i = 0; i < D - 1; i++) + { + P = L % 2 != 0 ? 2 * P : 2 * P + 1; + L = (L + 1) / 2; + } + printf("%d\n", P); +} +int main() +{ + int kase; + while (1) + { + scanf("%d", &kase); + if (kase == -1) + { + break; + } + while (kase--) + { + scanf("%d%d", &D, &L); + Cal(); + } + } + return 0; +} diff --git a/680.cpp b/680.cpp new file mode 100644 index 0000000..b547976 --- /dev/null +++ b/680.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) +#define FOREQ(i, a, b) for (int(i) = int(a); (i) <= int(b); (i)++) +#define RFOR(i, a, b) for (int(i) = (a), _b(b); (i) >= _b; --(i)) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++) + +int K, N, L[15], PF[15], acc[15], P; + +inline int dist(int fr_l, int fr_i, int to_l, int to_i) +{ + if (fr_l < to_l) + { + int ret = 0; + if (to_l - fr_l > 1) + { + ret += acc[to_l - 1] - acc[fr_l]; + } + return ret + L[fr_l] - fr_i + to_i; + } + else if (fr_l == to_l) + { + return abs(fr_i - to_i); + } + else//fr_l > to_l + { + int ret = 0; + if (fr_l - to_l > 1) + { + ret += acc[fr_l - 1] - acc[to_l]; + } + return ret + L[to_l] - to_i + fr_i; + } +} + +int main() +{ + int T; + scanf("%d", &T); + FOREQ(t, 1, T) + { + scanf("%d%d", &K, &N); + FOR(i, 0, K) + { + scanf("%d", &L[i]); + } + acc[0] = L[0]; + FOR(i, 1, K) + { + acc[i] = acc[i - 1] + L[i]; + } + + // FOR(i,0,K) + // { + // fprintf(stderr, "acc[%d]=%d ", i,acc[i]); + // } + // fprintf(stderr, "\n"); + + FOR(i, 0, K) + { + scanf("%d", &PF[i]); + } + scanf("%d", &P); + // fprintf(stderr, "P=%d\n", P); + + if (P == 1) + { + printf("0\n"); + continue; + } + + int tb = 0, posl = 0; + int last[K]; + FOR(i, 0, K) + { + last[i] = PF[i] - 1; + } + FOR(i, 0, P - 1) + { + int nxtl = (posl + 1) % K; + if (posl == nxtl) + { + int curr = last[posl]; + int nxt = (last[posl] + 1) % L[posl]; + tb += dist(posl, curr, nxtl, nxt); + } + else + { + tb += dist(posl, last[posl], nxtl, last[nxtl]); + } + // fprintf(stderr, "(%d,%d) -> (%d,%d); tb=%d\n", posl+1,last[posl]+1,nxtl+1,last[nxtl]+1, tb); + last[posl] = (last[posl] + 1) % L[posl]; + posl = (posl + 1) % K; + } + printf("%d\n", tb); + } + int _; + scanf("%d", &_);//-1 + return 0; +} diff --git a/681.cpp b/681.cpp new file mode 100644 index 0000000..9ceabcf --- /dev/null +++ b/681.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +struct Point +{ + int x, y; +}; + +struct Line +{ + Point p1, p2; + + int compute(Point p) + { + int a, b, c; + a = p1.y - p2.y; + b = p2.x - p1.x; + c = a * p1.x + b * p1.y; + return a * p.x + b * p.y - c; + } + + int side(Point p) + { + int temp = compute(p); + if (temp > 0) + { + return 1; + } + else if (temp < 0) + { + return -1; + } + else + { + return 0; + } + } +}; + +vector answer; + +void qhull(Point *point, int start, int end, Line line) +{ + if (start >= end) + { + return; + } + Line line1, line2; + int max_index = -1, max_dis = -1; + int part1 = start + 1, part2 = end; + for (int i = start; i < end; i++) + if (abs(line.compute(point[i])) > max_dis) + { + max_index = i; + max_dis = abs(line.compute(point[i])); + } + swap(point[start], point[max_index]); + line1.p1 = line.p1; + line1.p2 = point[start]; + line2.p1 = point[start]; + line2.p2 = line.p2; + for (int i = start + 1; i < part2; i++) + { + if (line1.side(point[i]) * line1.side(line.p2) < 0) + { + swap(point[i], point[part1++]); + } + else if (line2.side(point[i]) * line2.side(line.p1) < 0) + { + swap(point[i--], point[--part2]); + } + } + qhull(point, start + 1, part1, line1); + answer.push_back(point[start]); + qhull(point, part2, end, line2); +} + +bool cmp(Point p1, Point p2) +{ + if (p1.x < p2.x) + { + return true; + } + else if (p1.x == p2.x && p1.y < p2.y) + { + return true; + } + return false; +} + +int main() +{ + int K, N, part1, part2, min_index; + Point point[1005]; + Line line; + while (scanf("%d", &K) != EOF) + { + printf("%d\n", K); + for (int i = 0; i < K; i++) + { + scanf("%d", &N); + for (int j = 0; j < N; j++) + { + scanf("%d%d", &point[j].x, &point[j].y); + } + answer.clear(); + sort(point, point + N, cmp); + part1 = 1; + part2 = N - 1; + line.p1 = point[0]; + line.p2 = point[N - 1]; + for (int j = 1; j < part2; j++) + if (line.side(point[j]) > 0) + { + swap(point[part1++], point[j]); + } + else if (line.side(point[j]) < 0) + { + swap(point[--part2], point[j--]); + } + answer.push_back(point[0]); + qhull(point, 1, part1, line); + swap(line.p1, line.p2); + answer.push_back(point[N - 1]); + qhull(point, part2, N - 1, line); + min_index = 0; + printf("%lu\n", answer.size() + 1); + for (int j = 1; j < (int)answer.size(); j++) + if (answer[j].y < answer[min_index].y) + { + min_index = j; + } + else if (answer[j].y == answer[min_index].y && answer[j].x < answer[min_index].x) + { + min_index = j; + } + for (int j = 0; j <= (int)answer.size(); j++) + { + printf("%d %d\n", answer[(min_index - j + answer.size()) % answer.size()].x, answer[(min_index - j + answer.size()) % answer.size()].y); + } + if (i != K - 1) + { + scanf("%d", &N); + printf("-1\n"); + } + } + } + return 0; +} diff --git a/682.cpp b/682.cpp new file mode 100644 index 0000000..b7071e7 --- /dev/null +++ b/682.cpp @@ -0,0 +1,371 @@ +#include + +using namespace std; + +#define maxn 15000000 + +char F[maxn + 4], B[7][7], C[7][7]; +struct ss +{ + int d1, d2, d3, d4; + int m1, m2, m3, m4; +}; +struct xyz +{ + int r, c; +}; +int BlankR() +{ + int i, j; + for (i = 1; i <= 5; i++) + for (j = 1; j <= 5; j++) + if (B[i][j] == 1) + return i - 1; + return i - 1; +} +int BlankC() +{ + int i, j; + for (i = 1; i <= 5; i++) + for (j = 1; j <= 5; j++) + if (B[j][i] == 1) + return i - 1; + return i - 1; +} +void Move(int r, int c) +{ + int i, j; + for (i = 1; i <= 5; i++) + for (j = 1; j <= 5; j++) + { + C[i][j] = 0; + if (B[i][j]) + C[i - r][j - c] = 1; + } +} +ss Dec() +{ + int i, j, sum = 0, p = 1, k, l, r, c; + int sum2, sum3, sum4, m1, m2, m3, m4; + sum2 = sum3 = sum4 = 0; + m1 = m2 = m3 = m4 = 0; + ss temp; + r = BlankR(); + if (r == 5) + { + temp.d1 = 0; + return temp; + } + c = BlankC(); + Move(r, c); + for (i = 1, k = 5; i <= 5; i++, k--) + { + for (j = 1, l = 5; j <= 5; j++, l--) + { + sum += C[i][j] * p; + sum2 += C[j][k] * p; + sum3 += C[k][l] * p; + sum4 += C[l][i] * p; + m1 += C[i][l] * p; + m2 += C[l][k] * p; + m3 += C[k][j] * p; + m4 += C[j][i] * p; + p *= 2; + } + } + temp.d1 = sum; + temp.d2 = sum2; + temp.d3 = sum3; + temp.d4 = sum4; + temp.m1 = m1; + temp.m2 = m2; + temp.m3 = m3; + temp.m4 = m4; + return temp; +} +void Fill(int r, int c, int des, int dir) +{ + int i; + if (dir == 1) + for (i = c; i <= des; i++) + B[r][i] = 0; + else + for (i = r; i <= des; i++) + B[i][c] = 0; +} +void UnFill(int r, int c, int des, int dir) +{ + int i; + if (dir == 1) + for (i = c; i <= des; i++) + B[r][i] = 1; + else + for (i = r; i <= des; i++) + B[i][c] = 1; +} +int Recur(int r, int c, int des, int dir) +{ + int i, j, k, d, fg = 0, lim = 0, res = 0; + ss dc; + Fill(r, c, des, dir); + dc = Dec(); + if (!dc.d1) + { + UnFill(r, c, des, dir); + return 2; + } + if (dc.d1 <= maxn) + { + if (F[dc.d1] > 0) + { + UnFill(r, c, des, dir); + return F[dc.d1]; + } + } + if (dc.d2 <= maxn) + { + if (F[dc.d2] > 0) + { + UnFill(r, c, des, dir); + return F[dc.d2]; + } + } + if (dc.d3 <= maxn) + { + lim = 1; + if (F[dc.d3] > 0) + { + UnFill(r, c, des, dir); + return F[dc.d3]; + } + } + if (dc.d4 <= maxn) + { + lim = 1; + if (F[dc.d4] > 0) + { + UnFill(r, c, des, dir); + return F[dc.d4]; + } + } + if (dc.m1 <= maxn) + { + if (F[dc.m1] > 0) + { + UnFill(r, c, des, dir); + return F[dc.m1]; + } + } + if (dc.m2 <= maxn) + { + if (F[dc.m2] > 0) + { + UnFill(r, c, des, dir); + return F[dc.m2]; + } + } + if (dc.m3 <= maxn) + { + lim = 1; + if (F[dc.m3] > 0) + { + UnFill(r, c, des, dir); + return F[dc.m3]; + } + } + if (dc.m4 <= maxn) + { + if (F[dc.m4] > 0) + { + UnFill(r, c, des, dir); + return F[dc.m4]; + } + } + for (i = 1; i <= 5; i++) + { + for (j = 1; j <= 5; j++) + { + if (B[i][j] == 1) + { + for (k = j; k <= 5 && B[i][k]; k++) + { + d = Recur(i, j, k, 1); + if (d == 1) + { + fg = 1; + break; + } + } + if (fg) + break; + for (k = i + 1; k <= 5 && B[k][j]; k++) + { + d = Recur(i, j, k, 2); + if (d == 1) + { + fg = 1; + break; + } + } + } + } + if (fg) + break; + } + if (fg) + { + if (dc.d1 <= maxn) + F[dc.d1] = 2; + if (dc.d2 <= maxn) + F[dc.d2] = 2; + if (dc.d3 <= maxn) + F[dc.d3] = 2; + if (dc.d4 <= maxn) + F[dc.d4] = 2; + if (dc.m1 <= maxn) + F[dc.m1] = 2; + if (dc.m2 <= maxn) + F[dc.m2] = 2; + if (dc.m3 <= maxn) + F[dc.m3] = 2; + if (dc.m4 <= maxn) + F[dc.m4] = 2; + } + else + { + if (dc.d1 <= maxn) + F[dc.d1] = 1; + if (dc.d2 <= maxn) + F[dc.d2] = 1; + if (dc.d3 <= maxn) + F[dc.d3] = 1; + if (dc.d4 <= maxn) + F[dc.d4] = 1; + if (dc.m1 <= maxn) + F[dc.m1] = 1; + if (dc.m2 <= maxn) + F[dc.m2] = 1; + if (dc.m3 <= maxn) + F[dc.m3] = 1; + if (dc.m4 <= maxn) + F[dc.m4] = 1; + } + UnFill(r, c, des, dir); + if (fg) + return 2; + return 1; +} +bool Res(ss l) +{ + int r; + if (l.d1 <= maxn) + if (F[l.d1]) + r = F[l.d1]; + else if (l.d2 <= maxn) + if (F[l.d2]) + r = F[l.d2]; + else if (l.d3 <= maxn) + if (F[l.d3]) + r = F[l.d3]; + else if (l.d4 <= maxn) + if (F[l.d4]) + r = F[l.d4]; + else if (l.m1 <= maxn) + if (F[l.m1]) + r = F[l.m1]; + else if (l.m2 <= maxn) + if (F[l.m2]) + r = F[l.m2]; + else if (l.m3 <= maxn) + if (F[l.m3]) + r = F[l.m3]; + else if (l.m4 <= maxn) + if (F[l.m4]) + r = F[l.m4]; + if (r == 2) + printf("winning\n"); + if (r == 1) + printf("losing\n"); + if (r == 1 || r == 2) + return true; + return false; +} +void Shift(int r, int c) +{ + int i, j; + if (!r && !c) + return; + for (i = 1; i <= 5; i++) + for (j = 1; j <= 5; j++) + if (B[i][j] == 1) + { + B[i - r][j - c] = B[i][j]; + B[i][j] = 0; + } +} +void Cal() +{ + int i, j, d, fg = 0, k, r, c; + ss l; + l = Dec(); + if (l.d1 == 33554431 || l.d1 == 0) + { + printf("winning\n"); + return; + } + if (Res(l)) + return; + r = BlankR(); + c = BlankC(); + Shift(r, c); + for (i = 1; i <= 5; i++) + { + for (j = 1; j <= 5; j++) + { + if (B[i][j] == 1) + { + for (k = j; k <= 5 && B[i][k]; k++) + { + d = Recur(i, j, k, 1); + if (d == 1) + { + fg = 1; + break; + } + } + if (fg) + break; + for (k = i + 1; k <= 5 && B[k][j]; k++) + { + d = Recur(i, j, k, 2); + if (d == 1) + { + fg = 1; + break; + } + } + } + } + if (fg) + break; + } + printf(fg ? "winning\n" : "losing\n"); +} + +int main() +{ + int ks, i, j, l = 1; + scanf("%d", &ks); + while (ks--) + { + for (i = 1; i <= 5; i++) + { + for (j = 1; j <= 5; j++) + { + scanf("%d", &B[i][j]); + } + } + Cal(); + } + return 0; +} diff --git a/683.cpp b/683.cpp new file mode 100644 index 0000000..fc55a46 --- /dev/null +++ b/683.cpp @@ -0,0 +1,154 @@ +#include + +using namespace std; + +char map_[26], zero[26], str[300], left_[10], right_[10][5], sign[10][5], s; +int len[10], l, p, i, ll, rl; + +bool R(int depth, int choose, int sum); +bool L(int d, int carry) +{ + if (d == ll) + return carry == 0; + else + { + char c = left_[d], tempmap_[26]; + if (isdigit(c)) + return R(d, 0, carry); + if (map_[c - 'A'] != -1) + return R(d, 0, carry); + for (int i = (zero[c - 'A']) ? 1 : 0; i < 10; i++) + { + map_[c - 'A'] = i; + if (R(d, 0, carry)) + return true; + map_[c - 'A'] = -1; + } + return false; + } +} + +bool R(int depth, int choose, int sum) +{ + if (choose == len[depth]) + { + int value = isdigit(left_[depth]) ? left_[depth] - '0' : map_[left_[depth] - 'A']; + if ((sum % 10) != value) + return false; + return L(depth + 1, sum / 10); + } + else + { + char c = right_[depth][choose]; + if (isdigit(c)) + return R(depth, choose + 1, sum + c - '0'); + if (map_[c - 'A'] != -1) + { + if (sign[depth][choose] == '+') + return R(depth, choose + 1, sum + map_[c - 'A']); + if (sign[depth][choose] == '-') + return R(depth, choose + 1, sum - map_[c - 'A']); + } + for (int i = zero[c - 'A'] ? 1 : 0; i < 10; i++) + { + map_[c - 'A'] = i; + if (sign[depth][choose] == '+' && R(depth, choose + 1, sum + i)) + return true; + if (sign[depth][choose] == '-' && R(depth, choose + 1, sum - i)) + return true; + map_[c - 'A'] = -1; + } + return false; + } +} + +int main() +{ + while (gets(str) != NULL && strcmp(str, "*") != 0) + { + memset(map_, 0xff, sizeof(map_)); + memset(len, 0, sizeof(len)); + memset(zero, 0, sizeof(zero)); + ll = rl = 0; + for (p = 0; isalpha(str[p]) || isdigit(str[p]); p++, ll++) + ; + bool first = true; + for (i = 0; isalpha(str[i]) || isdigit(str[i]); i++) + { + left_[p - 1 - i] = str[i]; + if (first) + { + if (isalpha(str[i])) + zero[str[i] - 'A'] = true; + first = false; + } + } + for (; !isalpha(str[p]) && !isdigit(str[p]); p++) + ; + s = '+'; + while (str[p]) + { + for (i = p, l = 0; isalpha(str[p]) || isdigit(str[p]); p++, l++) + ; + bool first = true; + for (; i < p; i++) + { + int x = p - i - 1; + right_[x][len[x]] = str[i]; + sign[x][len[x]] = s; + len[x]++; + if (first) + { + if (isalpha(str[i])) + zero[str[i] - 'A'] = 1; + first = false; + } + } + if (l > rl) + rl = l; + for (; str[p] != '+' && str[p] != '-' && str[p] != '\0'; p++) + ; + if (str[p] == '+' || str[p] == '-') + s = str[p]; + while (!isalpha(str[p]) && !isdigit(str[p]) && str[p] != '\0') + p++; + } + + for (int i = 0; i < rl; i++) + { + int order[10], loc; + for (int j = 0; j < len[i]; j++) + order[j] = -1; + for (int j = 0; j < len[i]; j++) + { + for (loc = ll - 1; loc >= 0 && left_[loc] != right_[i][j]; loc--) + ; + order[j] = loc; + } + for (int j = 0; j < len[i]; j++) + for (int k = j + 1; k < len[i]; k++) + if (order[j] < order[k]) + { + swap(right_[i][j], right_[i][k]); + swap(sign[i][j], sign[i][k]); + swap(order[i], order[j]); + } + } + + if (!L(0, 0)) + puts("?"); + else + { + char c; + for (int i = ll - 1; i >= 0; i--) + { + if (isalpha(left_[i])) + printf("%c", map_[left_[i] - 'A'] + '0'); + else + putchar(left_[i]); + } + puts(""); + } + } + return 0; +} diff --git a/684.cpp b/684.cpp new file mode 100644 index 0000000..ef4aae1 --- /dev/null +++ b/684.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int abs(int k) +{ + return (k > 0) ? k : -k; +} +int m[31][31]; +int n; +int det(int n, int r, int c) +{ + if (n == 1) + { + return m[r][c]; + } + if (n == 2) + { + return m[r][c] * m[r + 1][c + 1] - m[r][c + 1] * m[r + 1][c]; + } + else + { + int res = 1, min, p; + bool flag; + while (true) + { + flag = true; + for (int i = r + 1, t = 1; t < n; i++, t++) + if (m[i][c]) + { + flag = false; + } + if (flag) + { + break; + } + min = 999999999; + for (int i = r, t = 0; t < n; i++, t++) + if (m[i][c] && abs(m[i][c]) < min) + { + min = abs(m[i][c]); + p = i; + } + if (p != r) + { + res *= (-1); + for (int i = 0; i < n; i++) + { + int t = m[r][c + i]; + m[r][c + i] = m[p][c + i]; + m[p][c + i] = t; + } + } + for (int i = 1; i < n; i++) + { + int base = m[r + i][c] / m[r][c]; + for (int j = 0; j < n; j++) + { + m[r + i][c + j] -= base * m[r][c + j]; + } + } + } + int k = det(n - 1, r + 1, c + 1); + return res * m[r][c] * k; + } +} +int main() +{ + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + { + scanf("%d", &m[i][j]); + } + int ans = det(n, 0, 0); + printf("%d\n", ans); + } + puts("*"); +} diff --git a/685.cpp b/685.cpp new file mode 100644 index 0000000..e48b79f --- /dev/null +++ b/685.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +const int INF = (1 << 28); + +class edge +{ +public: + int t, c; + edge(){}; + edge(int tt, int tc) + { + t = tt; + c = tc; + } +}; + +int ans; +vector E[400]; +int en[400][400]; +int deg[400]; +bool degvisited[400]; + +void solve(int n, int now, int k, int cost, int lastcost, bool flag) +{ + if (cost > ans) + { + return; + } + if (deg[now] == 1 && flag) + { + ans = cost; + degvisited[now] = true; + return; + } + rep(i, E[now].size()) + { + if (en[now][E[now][i].t] == 0) + { + continue; + } + int tmp = E[now][i].c; + en[now][E[now][i].t]--; + en[E[now][i].t][now]--; + solve(n, E[now][i].t, k, cost + max(lastcost, tmp) - min(lastcost, tmp) + k, tmp, true); + en[E[now][i].t][now]++; + en[now][E[now][i].t]++; + } +} + +main() +{ + int te; + scanf("%d", &te); + while (te--) + { + int k, m, n; + scanf("%d%d%d", &m, &n, &k); + rep(i, n) + { + E[i].clear(); + rep(j, n) en[i][j] = 0; + deg[i] = 0, degvisited[i] = false; + } + rep(i, m) + { + int f, t, c; + scanf("%d%d%d", &f, &t, &c); + f--; + t--; + E[f].push_back(edge(t, c)); + E[t].push_back(edge(f, c)); + en[f][t]++; + en[t][f]++; + deg[f]++; + deg[t]++; + } + ans = INF; + rep(i, n) + { + if (deg[i] == 1) + { + int tmp = E[i][0].c; + en[i][E[i][0].t]--; + en[E[i][0].t][i]--; + solve(n, E[i][0].t, k, k, tmp, false); + en[i][E[i][0].t]++; + en[E[i][0].t][i]++; + } + } + printf("%d\n", ans); + } +} diff --git a/686.cpp b/686.cpp index 9b737a0..a20e976 100644 --- a/686.cpp +++ b/686.cpp @@ -1,71 +1,51 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define MAX 1000000 +#include -bool prime[MAX + 5]; -vector P; +using namespace std; -void seive(){ - memset(prime, true, sizeof prime); - prime[0] = false; prime[1] = false; - int i, j; - FOI(i, 2, MAX) - if( prime[i] ){ - P.push_back(i); - for (j = 2*i; j <= MAX; j += i) - prime[j] = false; +bool isPrime(int n) +{ + int temp = sqrt(double(n)); + for (int i = 3; i < temp + 1; i += 2) + { + if (n % i == 0) + { + return false; } + } + return true; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - seive(); - int M = P.size(); - while( true ){ - int N, i; - cin >> N; - if( N == 0 ) +int main() +{ + vector primes; + vector::iterator it; + int n; + primes.push_back(2); + for (int i = 3; i < 1 << 15; i += 2) + { + if (isPrime(i)) + { + primes.push_back(i); + } + } + while (cin >> n) + { + if (n == 0) + { break; - //map Map; - int cnt = 0; - FOI(i, 0, M-1){ - if( P[i] > N/2 ) - break; - int NP = N - P[i]; - if( prime[NP] ){ - //cout << P[i] << " " << NP << endl; - cnt++; + } + int pos = 0; + int counter = 0; + while (primes[pos] <= n / 2) + { + it = find(primes.begin(), primes.end(), n - primes[pos]); + if (it != primes.end()) + { + counter++; } + ++pos; } - cout << cnt << endl; + cout << counter << endl; } return 0; } diff --git a/687.cpp b/687.cpp new file mode 100644 index 0000000..a36f026 --- /dev/null +++ b/687.cpp @@ -0,0 +1,140 @@ +#include + +using namespace std; + +set S; +char state[32][10]; +int cnt[32], rev[32]; +int a[10], ret; + +inline void get_reverse() +{ + memset(rev, 0, sizeof(rev)); + for (int i = 0; i < 32; ++i) + { + for (int j = 0, n = i; j < 5; ++j, n >>= 1) + { + rev[i] = rev[i] * 2 + (n & 1); + state[i][4 - j] = '0' + (n & 1); + } + state[i][5] = '\0'; + } +} + +inline void change(char s[]) +{ + int ret = 0; + for (int i = 0; i < 5; ++i) + { + ret = ret * 2 + s[i] - '0'; + } + cnt[ret]++; + cnt[rev[ret]]++; +} + +inline bool check() +{ + int tc[32]; + memset(tc, 0, sizeof(tc)); + for (int i = 0; i < 5; ++i) + { + int n = 0; + for (int j = 0; j < 5; ++j) + n = n * 2 + (a[j] & (1 << i) ? 0 : 1); + tc[n]++; + tc[rev[n]]++; + if (tc[n] > cnt[n]) + return false; + } + string st = ""; + for (int i = 0; i < 5; ++i) + st = st + (string)state[a[i]]; + return !S.count(st); +} + +inline void rotate(char x[10][10]) +{ + char y[10][10]; + for (int i = 0; i < 5; ++i) + strcpy(y[i], x[i]); + for (int i = 0; i < 5; ++i) + for (int j = 0; j < 5; ++j) + x[i][j] = y[j][4 - i]; +} + +inline void mirror(char x[10][10]) +{ + char y[10][10]; + for (int i = 0; i < 5; ++i) + strcpy(y[i], x[i]); + for (int i = 0; i < 5; ++i) + for (int j = 0; j < 5; ++j) + x[i][j] = y[4 - i][j]; +} + +inline void Insert(char x[10][10]) +{ + string st = ""; + for (int i = 0; i < 5; ++i) + st = st + (string)x[i]; + S.insert(st); +} + +inline void add() +{ + char x[10][10]; + ret++; + for (int i = 0; i < 5; ++i) + strcpy(x[i], state[a[i]]); + for (int i = 0; i < 2; ++i) + { + mirror(x); + for (int j = 0; j < 4; ++j) + { + rotate(x); + Insert(x); + } + } +} + +void dfs(int dep) +{ + if (dep == 5) + { + if (check()) + add(); + return; + } + for (int i = 0; i < 32; ++i) + { + if (cnt[i] == 0) + continue; + cnt[i]--; + cnt[rev[i]]--; + a[dep] = i; + dfs(dep + 1); + cnt[i]++; + cnt[rev[i]]++; + } +} + +int main() +{ + char st[10]; + get_reverse(); + while (scanf("%s", st) == 1 && st[0] != 'E') + { + memset(cnt, 0, sizeof(cnt)); + S.clear(); + ret = 0; + change(st); + for (int i = 1; i < 10; ++i) + { + scanf("%s", st); + change(st); + } + dfs(0); + printf("%d\n", ret >> 1); + } + return 0; +} diff --git a/688.cpp b/688.cpp new file mode 100644 index 0000000..4b24470 --- /dev/null +++ b/688.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +typedef struct +{ + double x, y, r; +} sqr_t; +sqr_t sq[1024]; +int n, m; + +int dcmp(const void *p, const void *q) +{ + return (*(double *)p > *(double *)q) ? 1 : -1; +} + +int scmp(const void *p, const void *q) +{ + return (((sqr_t *)p)->y > ((sqr_t *)q)->y) ? 1 : -1; +} + +double solve() +{ + static double sw[2048], area, up; + int i, j; + qsort(sq, n, sizeof(sq[0]), &scmp); + for (m = 0, i = 0; i < n; i++) + { + sw[m++] = sq[i].x; + sw[m++] = sq[i].x + sq[i].r; + } + qsort(sw, m, sizeof(sw[0]), &dcmp); + for (area = 0., i = 1; i < m; i++) + { + if ((sw[i] - sw[i - 1]) < EPS) + { + continue; + } + for (up = -1. / 0., j = 0; j < n; j++) + { + if (sq[j].x < (sw[i - 1] + EPS) && + (sq[j].x + sq[j].r) < (sw[i - 1] + EPS)) + { + continue; + } + if (sq[j].x > (sw[i] - EPS) && + (sq[j].x + sq[j].r) > (sw[i] - EPS)) + { + continue; + } + if (sq[j].y < (up + EPS)) + { + if ((sq[j].y + sq[j].r) > up) + { + area += (sq[j].y + sq[j].r - up) * (sw[i] - sw[i - 1]); + up = sq[j].y + sq[j].r; + } + } + else + { + area += sq[j].r * (sw[i] - sw[i - 1]); + up = sq[j].y + sq[j].r; + } + } + } + return area; +} + +int main() +{ + int i, t; + for (t = 1; scanf("%d", &n) == 1 && n != 0; t++) + { + for (i = 0; i < n; i++) + { + scanf("%lf %lf %lf", &sq[i].x, &sq[i].y, &sq[i].r); + sq[i].x -= sq[i].r; + sq[i].y -= sq[i].r; + sq[i].r *= 2; + } + printf("%d %.2f\n", t, solve()); + } + return 0; +} diff --git a/689.cpp b/689.cpp new file mode 100644 index 0000000..f10aeaa --- /dev/null +++ b/689.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +typedef struct +{ + int len; + char *str; +} WORD; +WORD *word; +int n_word, max_word; +int isPali(char *s, int len) +{ + int l = 0, r = len - 1; + while (l < r) + { + if (s[l] != s[r]) + { + return 0; + } + l++, r--; + } + return 1; +} + +int isExisted(char *s, int len) +{ + int flag = len % 2; + int i; + for (i = 0; i < n_word; i++) + { + if (word[i].len % 2 != flag) + { + continue; + } + int j = (word[i].len - len) / 2; + if (memcmp(word[i].str + j, s, len) == 0) + { + return 1; + } + } + return 0; +} + +char *StrDup(char *s, int len) +{ + char *r = (char *)malloc(len + 1); + memcpy(r, s, len); + r[len] = 0; + return r; +} + +int Compare(void const *p1, void const *p2) +{ + return strcmp(((WORD *)p1)->str, ((WORD *)p2)->str); +} + +char line[8192], test[8192]; + +int main() +{ + while (gets(line)) + { + int i, j; + for (i = j = 0; line[i]; i++) + if ('a' <= line[i] && line[i] <= 'z') + { + test[j++] = line[i] + 'A' - 'a'; + } + else if ('A' <= line[i] && line[i] <= 'Z') + { + test[j++] = line[i]; + } + test[j] = 0; + n_word = 0, max_word = 32; + word = (WORD *)malloc(32 * sizeof(WORD)); + int len = strlen(test); + for (i = len; i >= 3; i--) + for (j = 0; j + i <= len; j++) + { + if (!isPali(test + j, i)) + { + continue; + } + if (isExisted(test + j, i)) + { + continue; + } + if (n_word == max_word) + max_word += 32, + word = (WORD *)realloc(word, max_word * sizeof(WORD)); + word[n_word].len = i; + word[n_word].str = StrDup(test + j, i); + n_word++; + } + qsort(word, n_word, sizeof(WORD), Compare); + for (i = 0; i < n_word; i++) + { + if (i) + { + putchar(' '); + } + printf(word[i].str); + free(word[i].str); + } + putchar('\n'); + free(word); + } +} diff --git a/690.cpp b/690.cpp new file mode 100644 index 0000000..7e7680c --- /dev/null +++ b/690.cpp @@ -0,0 +1,157 @@ +#include + +using namespace std; + +#define N 211 +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +int table[5][N]; +char res[5][32]; +int cnt = 0; +int bit[20]; + +class state +{ +public: + int t[5]; + int now, size; + void set_data(int n, int num) + { + size = n; + now = num; + } + + bool operator<(const state &a) const + { + if (now != a.now) + { + return now < a.now; + } + if (t[0] != a.t[0]) + { + return t[0] < a.t[0]; + } + if (t[1] != a.t[1]) + { + return t[1] < a.t[1]; + } + if (t[2] != a.t[2]) + { + return t[2] < a.t[2]; + } + if (t[3] != a.t[3]) + { + return t[3] < a.t[3]; + } + if (t[4] != a.t[4]) + { + return t[4] < a.t[4]; + } + return false; + }; +}; + +void fill_table(int x, int n, int now, bool flag, state &ins) +{ + rep(i, 5) + { + ins.t[i] = 0; + rep(j, n) + { + if (flag == false) + { + if (table[i][x + j] != -1) + { + ins.t[i] |= bit[j]; + } + if (res[i][j] == 'X') + { + table[i][x + j] = now; + } + } + if (flag == true && table[i][x + j] == now) + { + table[i][x + j] = -1; + } + } + } +} + +bool can_do(int x, int n) +{ + rep(i, 5) + { + rep(j, n) + { + if (res[i][j] == '.') + { + continue; + } + else if (table[i][x + j] != -1) + { + return false; + } + } + } + return true; +} + +int node = 0; + +int solve(int n, int last, int now, map &M, state &ins) +{ + if (M.find(ins) != M.end()) + { + return M[ins]; + } + if (now == 10) + { + return n; + } + state next; + next.set_data(n, now); + int res = N; + int lim = n + 1; + REP(i, 1, lim) + { + if (can_do(last + i, n)) + { + fill_table(last + i, n, now, false, next); + res = min(res, i + solve(n, last + i, now + 1, M, next)); + fill_table(last + i, n, now, true, next); + } + } + return M[ins] = res; +} + +main() +{ + int n; + rep(i, 20) bit[i] = (1 << i); + while (scanf("%d\n", &n) && n) + { + int lim = 11 * n + 1; + rep(i, 5) + { + rep(j, lim) table[i][j] = -1; + } + // rep(i,5)scanf("%s",res[i]);//rep(j,n)cin>>res[i][j]; + rep(i, 5) fgets(res[i], n + 3, stdin); + node = 0; + cnt = N; + state ins; + map M; + rep(i, 5) + { + rep(j, n) if (res[i][j] == 'X') + { + table[i][j] = 0; + } + } + // cout << solve(n,0,1,M,ins) << endl; + printf("%d\n", solve(n, 0, 1, M, ins)); + //cout <<"state "<< node << endl; + } + return false; +} diff --git a/694.cpp b/694.cpp index c2c05ac..69404fe 100644 --- a/694.cpp +++ b/694.cpp @@ -1,25 +1,38 @@ -#include -using namespace std; - -int main(){ - for(long long i=1;;i++){ - long long A,L; - cin>>A>>L; - if(A<0 && L<0) - break; - long long n=A; - long long nc=1; - while(n!=1){ - if(n%2!=0){ - n=3*n+1; - if(n>L) - break; - } - else - n=n/2; - nc++; - } - cout<<"Case "< c) + { + break; + } + } + printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n", j, b, c, i); + j++; + } + return 0; +} diff --git a/696.cpp b/696.cpp new file mode 100644 index 0000000..0fd51b3 --- /dev/null +++ b/696.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +/** +696 +**/ +#define MAX(a, b) (a > b ? a : b) +#define MIN(a, b) (a > b ? b : a) +int M, N; +void Knight() +{ + int d, x, max = 0; + if (M == 0 || N == 0) + { + goto done; + } + if (M == 1 || N == 1) + { + max = MAX(M, N); + } + else if (M == 2 || N == 2) + { + d = MAX(M, N); + int temp1 = d / 4; + int temp = d - 3; + if (temp < 0) + { + x = 0; + } + else + { + x = (temp / 4) + 1; + } + max = (d - temp1 - x) * 2; + } + else + { + d = M * N; + max = d / 2; + if (d % 2) + { + max += 1; + } + } +done:; + printf("%d knights may be placed on a %d row %d column board.\n", max, M, N); +} +int main() +{ + while (scanf("%d%d", &M, &N) == 2) + { + if (M == 0 && N == 0) + { + break; + } + Knight(); + } + return 0; +} diff --git a/697.cpp b/697.cpp new file mode 100644 index 0000000..f47af5a --- /dev/null +++ b/697.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +double UP, D, L, B, P, DOWN, V, pi = 3.1415926535897932384626433832795; +int main() +{ + int caseno = 0; + while (scanf("%lf", &UP) == 1, UP >= 1) + { + scanf("%lf%lf%lf%lf%lf%lf", &D, &L, &B, &P, &DOWN, &V); + if (caseno++) + { + puts(""); + } + printf("Scenario %d:\n", caseno); + printf(" up hill %10.2lf sec\n", UP); + printf(" well diameter %10.2lf in\n", D); + printf(" water level %10.2lf in\n", L); + printf(" bucket volume %10.2lf cu ft\n", B); + printf(" bucket ascent rate %7.2lf in/sec\n", P); + printf(" down hill %10.2lf sec\n", DOWN); + printf(" required volume %10.2lf cu ft\n", V); + double time = 0, v = 0; + D /= 12; + L /= 12; + P /= 12; + double r = D / 2; + do + { + time += UP + DOWN; + time += (double)sqrt(2 * L / (32.2)) + (L / P); + v += B; + L += B / (pi * r * r); + } while (v < V && V - v > 1e-7); + printf(" TIME REQUIRED %10.2lf sec\n", time); + } + return 0; +} diff --git a/698.cpp b/698.cpp new file mode 100644 index 0000000..178f8fb --- /dev/null +++ b/698.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +#define maxn 10000 + +char the_index[maxn][22], text[100000], sep[maxn][22]; +int total, ts; +vector V[maxn]; + +int com(const void *a, const void *b) +{ + return strcmp((char *)a, (char *)b); +} +int Empty(char ss[]) +{ + int i; + for (i = 0; ss[i]; i++) + { + if (ss[i] == '\n') + { + ss[i] = NULL; + break; + } + } + if (strlen(ss) == 0) + return 1; + return 0; +} +void Sep() +{ + int i, j, k; + ts = 0; + char temp[112]; + for (i = 0; text[i];) + { + if (!isalpha(text[i]) && !isdigit(text[i])) + { + i++; + continue; + } + k = 0; + for (j = i; text[j] && (isalpha(text[j]) || isdigit(text[j])); j++) + temp[k++] = toupper(text[j]); + temp[k] = NULL; + strcpy(sep[ts++], temp); + i = j; + } +} +void Cal(int line) +{ + int i, *j; + Sep(); + qsort(sep, ts, sizeof(sep[0]), com); + for (i = 0; i < total; i++) + { + j = (int *)bsearch(&the_index[i], sep, ts, sizeof(the_index[0]), com); + if (j) + V[i].push_back(line); + } +} +void Print() +{ + int i, j, k, pre, f; + for (i = 0; i < total; i++) + { + f = 1; + if (V[i].size() == 0) + continue; + for (j = 0; the_index[i][j]; j++) + { + if (!isdigit(the_index[i][j]) && !isalpha(the_index[i][j])) + { + f = 0; + break; + } + } + printf("%s", the_index[i]); + if (V[i].size() == 0) + { + printf("\n"); + continue; + } + pre = V[i][0]; + printf(" %d", V[i][0]); + for (j = 1; j < V[i].size(); j++) + { + if (V[i][j] != pre + 1) + { + printf(", %d", V[i][j]); + pre = V[i][j]; + } + else + { + printf("-"); + while (j < V[i].size() && V[i][j] == pre + 1) + { + pre = V[i][j]; + j++; + } + j--; + printf("%d", pre); + } + } + printf("\n"); + } +} +void Free() +{ + int i; + for (i = 0; i < total; i++) + V[i].clear(); +} + +int main() +{ + int line, i, ks = 1; + total = 0; + while (gets(the_index[total++])) + { + if (Empty(the_index[total - 1])) + break; + for (i = 0; the_index[total - 1][i]; i++) + the_index[total - 1][i] = toupper(the_index[total - 1][i]); + while (gets(the_index[total])) + { + if (Empty(the_index[total])) + break; + for (i = 0; the_index[total][i]; i++) + the_index[total][i] = toupper(the_index[total][i]); + total++; + } + qsort(the_index, total, sizeof(the_index[0]), com); + line = 1; + while (1) + { + gets(text); + if (Empty(text)) + break; + Cal(line); + line++; + } + printf("Case %d\n", ks++); + Print(); + Free(); + total = 0; + printf("\n"); + } + return 0; +} diff --git a/699.cpp b/699.cpp new file mode 100644 index 0000000..f5900cd --- /dev/null +++ b/699.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +#define LEN 100 + +int left_[LEN], right_[LEN]; + +void stat(int n) +{ + int tmp; + scanf("%d", &tmp); + if (tmp != -1) + { + if (n > 0) + { + right_[n] += tmp; + } + else + { + left_[-n] += tmp; + } + stat(n - 1); + stat(n + 1); + } +} + +void build(int weight) +{ + int i, n; + memset(left_, 0, sizeof(left_)); + memset(right_, 0, sizeof(right_)); + left_[0] = weight; + stat(-1); + stat(1); + for (i = LEN; i > -1 && !left_[i]; --i) + ; + for (; i > 0; --i) + { + printf("%d ", left_[i]); + } + printf("%d", left_[0]); + for (n = LEN; n > -1 && !right_[n]; --n) + ; + for (i = 1; i <= n; ++i) + { + printf(" %d", right_[i]); + } + printf("\n\n"); +} + +int main() +{ + int tmp, test = 1; + while (scanf("%d", &tmp), tmp != -1) + { + printf("Case %d:\n", test++); + build(tmp); + } + return 0; +} diff --git a/700.cpp b/700.cpp new file mode 100644 index 0000000..c73b9d5 --- /dev/null +++ b/700.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +int main() +{ + int n, y, a, b; + int counter = 0; + while (cin >> n) + { + if (n == 0) + { + break; + } + counter++; + bool years[10000]; + fill(years, years + 10000, true); + while (n--) + { + cin >> y >> a >> b; + for (int i = 0; i < y; i++) + { + years[i] = false; + } + for (int i = y; i < 10000; i++) + { + if (i != y && (i - y) % (b - a) != 0) + { + years[i] = false; + } + } + } + cout << "Case #" << counter << ":" << endl; + bool ok = false; + for (int i = 0; i < 10000; i++) + { + if (years[i] == true) + { + ok = true; + cout << "The actual year is " << i << "." << endl; + break; + } + } + if (!ok) + { + cout << "Unknown bugs detected." << endl; + } + cout << endl; + } + return 0; +} diff --git a/701.cpp b/701.cpp new file mode 100644 index 0000000..9ac7332 --- /dev/null +++ b/701.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int num_length(unsigned int n) +{ + return log10(n) + 1; +} + +int main() +{ + unsigned int n; + double a, b; + unsigned int temp1, temp2; + while (cin >> n) + { + int length = num_length(n); + unsigned long i = 0; + while (++i) + { + a = log2(n) + (length + i) * log2(10); + b = log2(n + 1) + (length + i) * log2(10); + temp1 = (unsigned int)a; + temp2 = (unsigned int)b; + if (temp2 != b && temp2 - temp1 > 0) + { + cout << temp2 << endl; + break; + } + } + } + return 0; +} diff --git a/702.cpp b/702.cpp new file mode 100644 index 0000000..083ae00 --- /dev/null +++ b/702.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +#define MAX 30 + +long long dp[MAX][MAX][2]; +void build_dp(int n) +{ + int i, j, k, len, h; + memset(dp, 0, sizeof(dp)); + for (i = 0; i <= n; ++i) + dp[1][i][0] = dp[1][i][1] = i; + for (len = 2; len <= n; ++len) + { + dp[len][0][0] = dp[len][0][1] = 0; + for (h = 1; h <= len; ++h) + { + dp[len][h][0] = dp[len][h - 1][0] + dp[len - 1][len - h][1]; + dp[len][h][1] = dp[len][h - 1][1] + dp[len - 1][len - h][0]; + } + } +} + +int main() +{ + int n, m; + while (scanf("%d %d", &n, &m) == 2) + { + switch (n) + { + case 0: + printf("0\n"); + break; + case 1: + case 2: + printf("1\n"); + break; + default: + if (m == 1)//Shortest.. + { + //1 3 is limited. + n -= 2; + build_dp(n); + printf("%lld\n", dp[n][1][n % 2]); + } + else + { + n -= 1; + build_dp(n); + printf("%lld\n", dp[n][m - 1][n % 2]); + } + } + } + return 0; +} diff --git a/703.cpp b/703.cpp new file mode 100644 index 0000000..07b3c71 --- /dev/null +++ b/703.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define MAXN 105 + +char M[MAXN][MAXN]; +int tt, N; + +struct ss +{ + int a, b, c; +}; +ss *rec; + +void Cal() +{ + int i, j, k; + rec = new ss[MAXN * MAXN]; + tt = 0; + for (i = 1; i <= N; i++) + { + for (j = 1; j <= N; j++) + { + if (i == j) + continue; + for (k = 1; k <= N; k++) + { + if ((i < j && j < k) || (i > j && j > k)) + { + if (M[i][j] && M[j][k] && M[k][i]) + { + rec[tt].a = i; + rec[tt].b = j; + rec[tt++].c = k; + } + else if (!M[i][j] && !M[j][i] && !M[i][k] && !M[k][i]) + { + if (!M[k][j] && !M[j][k] && i < j && j < k) + { + rec[tt].a = i; + rec[tt].b = j; + rec[tt++].c = k; + } + } + } + } + } + } + printf("%d\n", tt); + for (i = 0; i < tt; i++) + printf("%d %d %d\n", rec[i].a, rec[i].b, rec[i].c); + delete[] rec; +} + +int main() +{ + int i, j, x; + while (scanf("%d", &N) == 1) + { + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + { + scanf("%d", &x); + M[i][j] = x; + } + Cal(); + } + return 0; +} diff --git a/704.cpp b/704.cpp new file mode 100644 index 0000000..2671784 --- /dev/null +++ b/704.cpp @@ -0,0 +1,261 @@ +#include + +using namespace std; + +map mark; +map mark1; +string goal = "034305650121078709t90121"; +struct node +{ + int step; //记录步数 + string path; //记录路径 + string state;//è®°å½•çŠ¶æ€ + node() + { + step = 0; + }; +}; +int ans = 100;//存储答案 +string sans; +int judge(node a)//判断节点 +{ + if (a.state == goal)//匹é…æˆåŠŸ + { + ans = a.path.size(); + sans = a.path; + return 1; + } + else if (mark1[a.state] != "")//匹é…æˆåŠŸ + { + int t = a.path.size() + mark1[a.state].size(); + if (t < ans) + { + ans = t; + sans = a.path; + for (int i = mark1[a.state].size() - 1; i >= 0; i--) + { + sans += mark1[a.state][i]; + } + } + } + else + { + return 0; + } + return 2; +} +int main() +{ + int cases, x, i; + char c, d; + cin >> cases; + while (cases--) + { + ans = 100; + mark.clear(), mark1.clear(); + string str; + for (i = 0; i < 24; i++) + { + cin >> x; + if (x == 10) + { + str = str + 't'; + } + else + { + str = str + char(x + '0'); + } + } + node tp, ts; + tp.state = str; + mark[str] = true; + if (judge(tp)) + { + cout << "PUZZLE ALREADY SOLVED" << endl; + continue; + } + queue q; + q.push(tp); + tp.state = goal; + queue dq; + dq.push(tp); + while (!dq.empty())//从目的状æ€å‘å‰æœç´¢ + { + tp = dq.front(), dq.pop(); + if (tp.step > 7) + { + break; + } + ts = tp; + ts.path += '3';//左顺时针 + c = ts.state[10], d = ts.state[11]; + ts.state.erase(10, 2); + ts.state.insert(0, 1, c), ts.state.insert(1, 1, d); + str = ts.state.substr(9, 3); + ts.state.replace(21, 3, str); + ts.step++; + if (ts.step <= 7 && mark1[ts.state] == "")//åˆ¤é‡ + { + mark1[ts.state] = ts.path;//记录路径 + dq.push(ts); + } + ts = tp; + ts.path += '4';//å³é¡ºæ—¶é’ˆ + c = ts.state[12], d = ts.state[13]; + ts.state.append(1, c), ts.state.append(1, d); + ts.state.erase(12, 2); + str = ts.state.substr(21, 3); + ts.state.replace(9, 3, str); + ts.step++; + if (ts.step <= 7 && mark1[ts.state] == "") + { + mark1[ts.state] = ts.path; + dq.push(ts); + } + ts = tp; + ts.path += '1';//左逆时针 + c = ts.state[0], d = ts.state[1]; + ts.state.insert(12, 1, c), ts.state.insert(13, 1, d); + ts.state.erase(0, 2); + str = ts.state.substr(9, 3); + ts.state.replace(21, 3, str); + ts.step++; + if (ts.step <= 7 && mark1[ts.state] == "") + { + mark1[ts.state] = ts.path; + dq.push(ts); + } + ts = tp; + ts.path += '2'; + c = ts.state[22], d = ts.state[23]; + ts.state.erase(22, 2); + ts.state.insert(12, 1, c), ts.state.insert(13, 1, d); + str = ts.state.substr(21, 3); + ts.state.replace(9, 3, str); + ts.step++; + if (ts.step <= 7 && mark1[ts.state] == "") + { + mark1[ts.state] = ts.path; + dq.push(ts); + } + } + int ok = 0; + while (!q.empty())//æ­£å‘æœç´¢ + { + tp = q.front(), q.pop(); + if (tp.step > 9) + { + break; + } + ts = tp; + ts.path += '2';//å³é¡ºæ—¶é’ˆ + c = ts.state[12], d = ts.state[13]; + ts.state.append(1, c), ts.state.append(1, d); + ts.state.erase(12, 2); + str = ts.state.substr(21, 3); + ts.state.replace(9, 3, str); + ts.step++; + if (ts.step <= 9 && !mark[ts.state])//判断 + { + int rs = judge(ts); + if (rs) + { + ok = 1; + if (rs == 1) + { + break; + } + } + mark[ts.state] = true; + q.push(ts); + } + ts = tp; + ts.path += '1';//左顺时针 + c = ts.state[10], d = ts.state[11]; + ts.state.erase(10, 2); + ts.state.insert(0, 1, c), ts.state.insert(1, 1, d); + str = ts.state.substr(9, 3); + ts.state.replace(21, 3, str); + ts.step++; + if (ts.step <= 9 && !mark[ts.state]) + { + int rs = judge(ts); + if (rs) + { + ok = 1; + if (rs == 1) + { + break; + } + } + mark[ts.state] = true; + q.push(ts); + } + ts = tp; + ts.path += '4'; + c = ts.state[22], d = ts.state[23]; + ts.state.erase(22, 2); + ts.state.insert(12, 1, c), ts.state.insert(13, 1, d); + str = ts.state.substr(21, 3); + ts.state.replace(9, 3, str); + ts.step++; + if (ts.step <= 9 && !mark[ts.state]) + { + int rs = judge(ts); + if (rs) + { + ok = 1; + if (rs == 1) + { + break; + } + } + mark[ts.state] = true; + q.push(ts); + } + ts = tp; + ts.path += '3';//左逆时针 + c = ts.state[0], d = ts.state[1]; + ts.state.insert(12, 1, c), ts.state.insert(13, 1, d); + ts.state.erase(0, 2); + str = ts.state.substr(9, 3); + ts.state.replace(21, 3, str); + ts.step++; + if (ts.step <= 9 && !mark[ts.state]) + { + int rs = judge(ts); + if (rs) + { + ok = 1; + if (rs == 1) + { + break; + } + } + mark[ts.state] = true; + q.push(ts); + } + } + if (!ok) + { + cout << "NO SOLUTION WAS FOUND IN 16 STEPS" << endl; + } + else + { + cout << sans << endl; + } + } + return 0; +} +/* +12 +0 3 4 5 0 3 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1 +0 9 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 3 0 1 2 1 +2 1 0 3 4 3 0 5 6 9 0 5 0 1 0 7 8 7 0 9 10 9 0 5 +0 3 4 5 0 3 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1 +0 9 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 3 0 1 2 1 +2 1 0 3 4 3 0 5 6 9 0 5 0 1 0 7 8 7 0 9 10 9 0 5 +0 3 4 5 0 3 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1 +0 9 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 3 0 1 2 1 +2 1 0 3 4 3 0 5 6 9 0 5 0 1 0 7 8 7 0 9 10 9 0 5 +*/ diff --git a/705.cpp b/705.cpp new file mode 100644 index 0000000..936fe43 --- /dev/null +++ b/705.cpp @@ -0,0 +1,149 @@ +#include + +using namespace std; + +vector sou; +//bool mark1[100][100];//å‘上是å¦é历 +bool mark[100][100];//å‘下是å¦é历 +int n, m; +int step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};//下0 上1 å³2 å·¦3 +int out = 0; +int nums = 0; +int maxs = 0; +void dfs(int starti, int startj, int i, int j, int direct, int cou, int redir) +{ + int tempi = i + step[direct][0], tempj = j + step[direct][1]; + if (direct == redir && tempi == starti && tempj == startj) + { + out = 1; + nums++; + if (cou > maxs) + { + maxs = cou; + } + return; + } + if (tempi >= 0 && tempi < n && tempj >= 0 && tempj < m) + { + if (direct == 0) + { + if (sou[tempi][tempj] == '\\') + { + dfs(starti, startj, tempi, tempj, 2, cou + 1, redir); + } + else + { + dfs(starti, startj, tempi, tempj, 3, cou + 1, redir); + } + } + if (direct == 1) + { + mark[tempi][tempj] = false; + if (sou[tempi][tempj] == '\\') + { + dfs(starti, startj, tempi, tempj, 3, cou + 1, redir); + } + else + { + dfs(starti, startj, tempi, tempj, 2, cou + 1, redir); + } + } + if (direct == 2) + { + if (sou[tempi][tempj] == '\\') + { + mark[tempi][tempj] = false; + dfs(starti, startj, tempi, tempj, 0, cou + 1, redir); + } + else + { + dfs(starti, startj, tempi, tempj, 1, cou + 1, redir); + } + } + if (direct == 3) + { + if (sou[tempi][tempj] == '\\') + { + dfs(starti, startj, tempi, tempj, 1, cou + 1, redir); + } + else + { + mark[tempi][tempj] = false; + dfs(starti, startj, tempi, tempj, 0, cou + 1, redir); + } + } + if (out) + { + return; + } + } + else + { + return; + } +} +void print() +{ + for (int i = 0; i < sou.size(); i++) + { + cout << sou[i] << endl; + } +} +void process() +{ + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + out = 0; + if (mark[i][j]) + { + int redir = (sou[i][j] == '\\' ? 2 : 3); + dfs(i, j, i, j, 0, 1, redir); + } + } + } +} +int main() +{ + string str; + int cases = 0; + while (cin >> m >> n) + { + nums = 0, maxs = 0; + if (m == 0 && n == 0) + { + break; + } + cases++; + memset(mark, true, sizeof(mark)); + for (int i = 0; i < n; i++) + { + cin >> str; + sou.push_back(str); + } + process(); + cout << "Maze #" << cases << ":" << endl; + if (nums != 0) + { + cout << nums << " Cycles; the longest has length " << maxs << "." << endl + << endl; + } + else + { + cout << "There are no cycles." << endl + << endl; + } + sou.clear(); + } + return 0; +} +/* +10 3 +/\/\/\\\\/ +\\\///\\/\ +\\/\/\\\\/ +0 0 +Maze #1: +4 Cycles; the longest has length 8. +*/ diff --git a/706.cpp b/706.cpp new file mode 100644 index 0000000..4d44295 --- /dev/null +++ b/706.cpp @@ -0,0 +1,146 @@ +#include + +using namespace std; + +char model[10][8] = + { + '-', '|', '|', ' ', '|', '|', '-', '\0', + ' ', ' ', '|', ' ', ' ', '|', ' ', '\0', + '-', ' ', '|', '-', '|', ' ', '-', '\0', + '-', ' ', '|', '-', ' ', '|', '-', '\0', + ' ', '|', '|', '-', ' ', '|', ' ', '\0', + '-', '|', ' ', '-', ' ', '|', '-', '\0', + '-', '|', ' ', '-', '|', '|', '-', '\0', + '-', ' ', '|', ' ', ' ', '|', ' ', '\0', + '-', '|', '|', '-', '|', '|', '-', '\0', + '-', '|', '|', '-', ' ', '|', '-', '\0', +}; + +int get_array(int n, int *array) +{ + int i, length = 0, tmp; + if (n == 0) + { + array[0] = 0; + return 1; + } + while (n) + { + array[length++] = n % 10; + n /= 10; + } + for (i = 0; i < length / 2; i++) + { + tmp = array[i]; + array[i] = array[length - 1 - i]; + array[length - 1 - i] = tmp; + } + return length; +} + +int main() +{ + int n, s, array[10], length; + while (cin >> s >> n) + { + if (s == 0 && n == 0) + { + break; + } + length = get_array(n, array); + int i, j, k; + for (i = 0; i < length; i++)//print line 1; + { + cout << ' '; + for (j = 0; j < s; j++) + { + cout << model[array[i]][0]; + } + cout << ' '; + if (i == (length - 1)) + { + cout << endl; + } + else + { + cout << ' '; + } + } + for (k = 0; k < s; k++) + { + for (i = 0; i < length; i++)//print line 2; + { + cout << model[array[i]][1]; + for (j = 0; j < s; j++) + { + cout << ' '; + } + cout << model[array[i]][2]; + if (i == (length - 1)) + { + cout << endl; + } + else + { + cout << ' '; + } + } + } + for (i = 0; i < length; i++)//print line 3; + { + cout << ' '; + for (j = 0; j < s; j++) + { + cout << model[array[i]][3]; + } + cout << ' '; + if (i == (length - 1)) + { + cout << endl; + } + else + { + cout << ' '; + } + } + for (k = 0; k < s; k++) + { + for (i = 0; i < length; i++)//print line 2; + { + cout << model[array[i]][4]; + for (j = 0; j < s; j++) + { + cout << ' '; + } + cout << model[array[i]][5]; + if (i == (length - 1)) + { + cout << endl; + } + else + { + cout << ' '; + } + } + } + for (i = 0; i < length; i++)//print line 5; + { + cout << ' '; + for (j = 0; j < s; j++) + { + cout << model[array[i]][6]; + } + cout << ' '; + if (i == (length - 1)) + { + cout << endl; + } + else + { + cout << ' '; + } + } + cout << endl; + } + return 0; +} diff --git a/707.cpp b/707.cpp new file mode 100644 index 0000000..6bb6e8e --- /dev/null +++ b/707.cpp @@ -0,0 +1,166 @@ +#include + +using namespace std; + +// Robbery (抢劫) +// PC/UVa IDs: 111205/707, Popularity: B, Success rate: average Level: 3 +// Verdict: Accepted +// Submission Date: 2011-11-04 +// UVa Run Time: 0.036s +// +// 版æƒæ‰€æœ‰ï¼ˆC)2011,邱秋。metaphysis # yeah dot net +// +// [解题方法] +// æ示:如何建图æ¥åŒæ—¶åˆ»ç”»æ—¶é—´å’Œç©ºé—´ï¼Ÿå¦‚何高效的é历以确定å¯èƒ½çš„ä½ç½®ï¼Ÿå¦‚果解决了这两个问题,本问 +// é¢˜å°±è§£å†³äº†ï¼ + +#define MAXN 100 + +#define UNOBSERVED 0 +#define OBSERVED 1 +#define REACHABLE 2 +#define I_AM_HERE 3 + +int width, height, timeLocked; +int grid[MAXN + 1][MAXN + 1][MAXN + 1]; +int offset[5][2] = {{0, 0}, {-1, 0}, {0, -1}, {1, 0}, {0, 1}}; + +// 先从最åŽæ—¶åˆ»å¾€å‰æœç´¢ï¼Œæ‰¾åˆ°å¯ä»¥åˆ°è¾¾çš„ä½ç½®ï¼Œæ ‡è®°ã€‚ +void backward_dfs(int time, int y, int x) +{ + if (time == 1) + { + grid[time][y][x] = REACHABLE; + return; + } + for (int i = 0; i < 5; i++) + { + int tmpY = y + offset[i][0]; + int tmpX = x + offset[i][1]; + if (1 <= tmpY && tmpY <= height && 1 <= tmpX && tmpX <= width) + if (grid[time - 1][tmpY][tmpX] == UNOBSERVED) + { + grid[time][y][x] = REACHABLE; + backward_dfs(time - 1, tmpY, tmpX); + } + else if (grid[time - 1][tmpY][tmpX] == REACHABLE) + { + grid[time][y][x] = REACHABLE; + } + } +} + +// 从å‰å¾€åŽæœç´¢ï¼Œå°†å¯èƒ½çš„ä½ç½®æ ‡è®°ã€‚ +void forward_dfs(int time, int y, int x) +{ + if (time == timeLocked) + { + grid[time][y][x] = I_AM_HERE; + return; + } + for (int i = 0; i < 5; i++) + { + int tmpY = y + offset[i][0]; + int tmpX = x + offset[i][1]; + if (1 <= tmpY && tmpY <= height && 1 <= tmpX && tmpX <= width) + if (grid[time + 1][tmpY][tmpX] == REACHABLE) + { + grid[time][y][x] = I_AM_HERE; + forward_dfs(time + 1, tmpY, tmpX); + } + else if (grid[time + 1][tmpY][tmpX] == I_AM_HERE) + { + grid[time][y][x] = I_AM_HERE; + } + } +} + +int main(int ac, char *av[]) +{ + int cases = 1; + int messages; + int current, left, top, right, bottom; + while (cin >> width >> height >> timeLocked, width || height || timeLocked) + { + cout << "Robbery #" << cases++ << ":\n"; + for (int t = 1; t <= timeLocked; t++) + for (int y = 1; y <= height; y++) + for (int x = 1; x <= width; x++) + { + grid[t][y][x] = UNOBSERVED; + } + // 读å–å„时间片的å°é”范围,劫匪在该时刻ä¸ä¼šåœ¨æ­¤èŒƒå›´å†…。 + cin >> messages; + for (int i = 1; i <= messages; i++) + { + cin >> current >> left >> top >> right >> bottom; + for (int y = top; y <= bottom; y++) + for (int x = left; x <= right; x++) + { + grid[current][y][x] = OBSERVED; + } + } + // 从最åŽä¸€ä¸ªæ—¶é—´ç‰‡å¾€å‰è¿›è¡Œæ·±åº¦ä¼˜å…ˆé历。å‡è®¾åœ¨æ—¶åˆ» t,劫匪在ä½ç½®ï¼ˆwidth, + // height),则在上一时刻 t - 1,劫匪åªå¯èƒ½åœ¨ï¼ˆwidth - 1,height), + // (width + 1,height),(width,height - 1),(width,height + 1) + // (width,height)五个ä½ç½®ä¸­çš„一个。注æ„需è¦ç»è¿‡æ­£å两个方å‘çš„æœç´¢æ‰èƒ½ç¡®å®š + // 通路上的å¯èƒ½ä½ç½®ã€‚ + for (int y = 1; y <= height; y++) + for (int x = 1; x <= width; x++) + if (grid[timeLocked][y][x] == UNOBSERVED) + { + backward_dfs(timeLocked, y, x); + } + // æ­£å‘æœç´¢ä»¥ç¡®å®šç¡®å®žå¯è¡Œçš„ä½ç½®ã€‚ + for (int y = 1; y <= height; y++) + for (int x = 1; x <= width; x++) + if (grid[1][y][x] == REACHABLE) + { + forward_dfs(1, y, x); + } + // æ ¹æ®å„个时间片å¯èƒ½ä½ç½®çš„æ•°é‡å†³å®šè¾“出。 + bool nothing = true; + pair location; + for (int t = 1; t <= timeLocked; t++) + { + int exactLocation = 0; + for (int y = 1; y <= height; y++) + { + for (int x = 1; x <= width; x++) + if (grid[t][y][x] == I_AM_HERE) + { + exactLocation++; + location = make_pair(x, y); + if (exactLocation > 1) + { + break; + } + } + if (exactLocation > 1) + { + break; + } + } + if (exactLocation == 0) + { + cout << "The robber has escaped.\n"; + nothing = false; + break; + } + if (exactLocation == 1) + { + cout << "Time step " << t << ": "; + cout << "The robber has been at "; + cout << location.first << "," << location.second; + cout << ".\n"; + nothing = false; + } + } + if (nothing) + { + cout << "Nothing known.\n"; + } + cout << "\n"; + } + return 0; +} diff --git a/708.cpp b/708.cpp new file mode 100644 index 0000000..91a086c --- /dev/null +++ b/708.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +char str[2008], buf[2008]; +int n, l, target, lst[15]; + +int digit(char c) +{ + return c >= '0' && c <= '9'; +} + +int recursion(int left, int right) +{ + int i, p; + for (i = right, p = 0; i >= left; i--) + { + if (str[i] == '(') + { + p++; + } + else if (str[i] == ')') + { + p--; + } + else if (p == 0 && digit(str[i]) == 0) + { + break; + } + } + if (i < left) + { + if (str[left] == '(') + { + return recursion(left + 1, right - 1); + } + sscanf(str + left, "%d", &i); + return i; + } + else + { + /*printf("range %d to %d ii %d, left %d, right %d.\n", left, right, i, recursion(left, i-1), recursion(i+1, right));*/ + if (str[i] == '+') + { + return recursion(left, i - 1) + recursion(i + 1, right); + } + else if (str[i] == '-') + { + return recursion(left, i - 1) - recursion(i + 1, right); + } + return recursion(left, i - 1) * recursion(i + 1, right); + } +} + +int dfs(int depth) +{ + if (depth == n) + { + return recursion(0, l - 1) == target; + } + str[lst[depth]] = '*'; + if (dfs(depth + 1)) + { + return 1; + } + str[lst[depth]] = '+'; + if (dfs(depth + 1)) + { + return 1; + } + str[lst[depth]] = '-'; + if (dfs(depth + 1)) + { + return 1; + } + return 0; +} + +int main() +{ + int cas; + char *p, *q; + cas = 0; + while (gets(buf)) + { + if (strcmp(buf, "0") == 0) + { + break; + } + sscanf(buf, "%d", &target); + strtok(buf, "="); + for (p = strtok(NULL, "="), q = str; *p; p++) + { + if (*p == ' ') + { + if (q > str && (digit(*(q - 1)) || *(q - 1) == ')')) + { + *(q++) = ' '; + } + } + else if (*p == '(') + { + if (q > str && (digit(*(q - 1)) || *(q - 1) == ')')) + { + *(q++) = ' '; + } + *(q++) = *p; + } + else if (*p == ')') + { + if (q > str && *(q - 1) == ' ') + { + q--; + } + *(q++) = *p; + } + else + { + if (q > str && *(q - 1) == ')') + { + *(q++) = ' '; + } + *(q++) = *p; + } + } + *q = 0; + l = q - str; + for (n = 0, p = str; *p; p++) + { + if (*p == ' ') + { + lst[n++] = p - str; + *p = '*'; + } + } + /*printf("**%s**\n", str);*/ + printf("Equation #%d:\n", ++cas); + if (dfs(0)) + { + printf("%d=%s\n\n", target, str); + } + else + { + printf("Impossible.\n\n"); + } + } + return 0; +} diff --git a/709.cpp b/709.cpp new file mode 100644 index 0000000..ddacd98 --- /dev/null +++ b/709.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +char line[32767]; +int dp[32767], dpline[32767], from[32767], N; +vector words; + +void printText(int r) +{ + int l = from[r]; + if (l > 0) + { + printText(l); + } + if (l == r - 1) + { + printf("%s\n", words[l].c_str()); + return; + } + int sum = 0; + for (int i = l; i < r; i++) + { + sum += words[i].length(); + } + int ngap = r - l - 1, small, large; + int sn, ln; + small = large = (N - sum) / ngap; + sn = ngap, ln = 0; + if ((N - sum) % ngap) + { + large++; + ln = (N - sum) % ngap; + sn -= ln; + } + for (int i = l, j = 0; i < r; i++, j++) + { + printf("%s", words[i].c_str()); + if (j < sn) + { + for (int k = 0; k < small; k++) + { + printf(" "); + } + } + else if (j < sn + ln) + { + for (int k = 0; k < large; k++) + { + printf(" "); + } + } + } + printf("\n"); +} + +int main() +{ + while (scanf("%d", &N) == 1 && N) + { + while (getchar() != '\n') + ; + words.clear(); + while (gets(line) && line[0] != '\0') + { + stringstream sin(line); + string w; + while (sin >> w) + { + words.push_back(w); + } + } + memset(dp, 63, sizeof(dp)); + memset(dpline, 63, sizeof(dpline)); + int n = words.size(); + dp[0] = 0, from[0] = -1, dpline[0] = 0; + for (int i = 0; i < n; i++) + { + if (dp[i] + 500 < dp[i + 1] || (dp[i] + 500 == dp[i + 1] && dpline[i] + 1 <= dpline[i + 1])) + { + dp[i + 1] = min(dp[i + 1], dp[i] + 500); + from[i + 1] = i; + dpline[i + 1] = dpline[i] + 1; + } + int sum = words[i].length(); + for (int j = i + 1; j < n; j++) + { + sum += words[j].length(); + if (sum + j - i > N) + { + break; + } + int ngap = j - i, small, large; + int sn, ln; + small = large = (N - sum) / ngap; + sn = ngap, ln = 0; + if ((N - sum) % ngap) + { + large++; + ln = (N - sum) % ngap; + sn -= ln; + } + int bad = (small - 1) * (small - 1) * sn + (large - 1) * (large - 1) * ln; + if (dp[j + 1] > dp[i] + bad || (dp[j + 1] == dp[i] + bad && dpline[i] + 1 <= dpline[j + 1])) + { + dp[j + 1] = min(dp[j + 1], dp[i] + bad); + from[j + 1] = i; + dpline[j + 1] = dpline[i] + 1; + } + } + } + printText(n); + printf("\n"); + } + return 0; +} diff --git a/710.cpp b/710.cpp new file mode 100644 index 0000000..5156c91 --- /dev/null +++ b/710.cpp @@ -0,0 +1,127 @@ +#include + +using namespace std; + +#define maxn 80 +#define INF 9999999 + +char B[maxn], D[maxn][maxn]; +char X[] = {0, 1, 0, -1}; +char Y[] = {-1, 0, 1, 0}; +int Cost[maxn][maxn]; +int R, C; + +struct ss +{ + int x, y; + int cost; + int dir; +}; +queue Q; +int BFS(int u, int v, int y, int z) +{ + ss temp, dump; + int nx, ny, i, d, min = INF; + if (u == y && v == z) + return 0; + temp.x = u; + temp.y = v; + temp.dir = -1; + temp.cost = 0; + Q.push(temp); + while (!Q.empty()) + { + temp = Q.front(); + Q.pop(); + for (i = 0; i < 4; i++) + { + nx = temp.x + X[i]; + ny = temp.y + Y[i]; + if (nx > R + 1 || nx < 0 || ny > C + 1 || ny < 0) + continue; + d = 0; + if (temp.dir != i) + d = 1; + if (Cost[nx][ny] <= temp.cost + d) + continue; + if (temp.cost + d >= min) + continue; + if (nx == y && ny == z) + { + min = temp.cost + d; + continue; + } + if (D[nx][ny] == 1) + continue; + dump.x = nx; + dump.y = ny; + dump.cost = temp.cost + d; + dump.dir = i; + Cost[nx][ny] = temp.cost + d; + Q.push(dump); + } + } + return min; +} +void Ini() +{ + int i, j; + for (i = 0; i <= R + 1; i++) + for (j = 0; j <= C + 1; j++) + Cost[i][j] = INF; +} +void Cal() +{ + int u, v, y, z, d, k = 1; + while (1) + { + gets(B); + sscanf(B, "%d%d%d%d", &u, &v, &y, &z); + if (!u && !v && !y && !z) + break; + Ini(); + d = BFS(v, u, z, y); + printf("Pair %d: ", k++); + if (d == INF) + printf("impossible.\n"); + else + printf("%d segments.\n", d); + } +} +void Free() +{ + int i, j; + for (i = 0; i <= R; i++) + { + for (j = 0; j <= C; j++) + { + D[i][j] = 0; + } + } +} + +int main() +{ + int i, k = 1; + while (gets(B)) + { + sscanf(B, "%d%d", &C, &R); + if (!R && !C) + break; + for (i = 1; i <= R; i++) + { + gets(B); + for (int j = 0; B[j]; j++) + { + D[i][j + 1] = 0; + if (B[j] == 'X') + D[i][j + 1] = 1; + } + } + printf("Board #%d:\n", k++); + Cal(); + printf("\n"); + Free(); + } + return 0; +} diff --git a/711.cpp b/711.cpp new file mode 100644 index 0000000..c992c3e --- /dev/null +++ b/711.cpp @@ -0,0 +1,165 @@ +#include + +using namespace std; + +#define X first +#define Y second + +int main(int argc, char const *argv[]) +{ + ios::sync_with_stdio(false); + int a[100], k; + int b[] = {1, 2, 3, 4, 5, 6, 20, 60}; + int t = 0; + int c[8]; + int tmp, W; + while (cin >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] && (a[1] + a[2] + a[3] + a[4] + a[5] + a[6])) + { + cout << "Collection #" << ++t << ":\n"; + W = 0; + for (int i = 1; i < 7; i += 1) + { + W += i * a[i]; + } + if ((W & 1)) + { + cout << "Can't be divided.\n\n"; + continue; + } + W >>= 1; + a[20] = 0; + a[60] = 0; + if (a[1]) + { + k = (a[1] - 1) >> 1; + a[2] += k; + a[1] -= (k << 1); + } + if (a[2]) + { + k = (a[2] - 1) >> 1; + a[4] += k; + a[2] -= (k << 1); + } + if (a[3]) + { + k = (a[3] - 1) >> 1; + a[6] += k; + a[3] -= (k << 1); + } + if (a[4] > 4) + { + k = (a[4] - 4) / 5; + a[20] += k; + a[4] -= k * 5; + } + if (a[5] > 3) + { + k = (a[5] - 3) / 4; + a[20] += k; + a[5] -= k * 4; + } + if (a[6] > 9) + { + k = (a[6] - 9) / 10; + a[60] += k; + a[6] -= 10 * k; + } + if (a[20] > 2) + { + k = (a[20] - 2) / 3; + a[60] += k; + a[20] -= 3 * k; + } + tmp = 0; + for (c[0] = 0; c[0] <= a[b[0]]; c[0] += 1) + { + tmp += c[0] * b[0]; + if (tmp > W) + { + tmp -= c[0] * b[0]; + break; + } + for (c[1] = 0; c[1] <= a[b[1]]; c[1] += 1) + { + tmp += c[1] * b[1]; + if (tmp > W) + { + tmp -= c[1] * b[1]; + break; + } + for (c[2] = 0; c[2] <= a[b[2]]; c[2] += 1) + { + tmp += c[2] * b[2]; + if (tmp > W) + { + tmp -= c[2] * b[2]; + break; + } + for (c[3] = 0; c[3] <= a[b[3]]; c[3] += 1) + { + tmp += c[3] * b[3]; + if (tmp > W) + { + tmp -= c[3] * b[3]; + break; + } + for (c[4] = 0; c[4] <= a[b[4]]; c[4] += 1) + { + tmp += c[4] * b[4]; + if (tmp > W) + { + tmp -= c[4] * b[4]; + break; + } + for (c[5] = 0; c[5] <= a[b[5]]; c[5] += 1) + { + tmp += c[5] * b[5]; + if (tmp > W) + { + tmp -= c[5] * b[5]; + break; + } + for (c[6] = 0; c[6] <= a[b[6]]; c[6] += 1) + { + tmp += c[6] * b[6]; + if (tmp > W) + { + tmp -= c[6] * b[6]; + break; + } + for (c[7] = 0; c[7] <= a[b[7]]; c[7] += 1) + { + tmp += c[7] * b[7]; + if (tmp > W) + { + tmp -= c[7] * b[7]; + break; + } + if (tmp == W) + { + cout << "Can be divided.\n\n"; + goto HELL; + } + tmp -= c[7] * b[7]; + } + tmp -= c[6] * b[6]; + } + tmp -= c[5] * b[5]; + } + tmp -= c[4] * b[4]; + } + tmp -= c[3] * b[3]; + } + tmp -= c[2] * b[2]; + } + tmp -= c[1] * b[1]; + } + tmp -= c[0] * b[0]; + } + cout << "Can't be divided.\n\n"; + HELL: + continue; + } + return 0; +} diff --git a/712.cpp b/712.cpp new file mode 100644 index 0000000..469fe7e --- /dev/null +++ b/712.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +/** +712 +**/ +int N[20]; +int tn; +char Val[130]; +char VVA[10]; +struct ss +{ + char val; +} node[10]; +int Cal() +{ + int i, lo, up = 1, k; + int diff; + lo = 1; + for (i = 0; i < tn; i++) + { + up *= 2; + } + for (i = 0; VVA[i]; i++) + { + node[i + 1].val = VVA[i] - '0'; + } + for (i = 0; i < tn; i++) + { + k = N[i]; + diff = up - lo + 1; + if (node[k].val == 0) + { + up = lo + (diff / 2) - 1; + } + else + { + lo = (lo + diff / 2); + } + } + if (node[k].val == 1) + { + printf("%c", Val[up]); + } + else + { + printf("%c", Val[lo]); + } + return 0; +} +int main() +{ + int i, kase, t = 1; + char input[100]; + while (scanf("%d", &tn) == 1) + { + if (!tn) + { + break; + } + for (i = 0; i < tn; i++) + { + scanf("%s", input); + input[0] = '0'; + sscanf(input, "%d", &N[i]); + } + scanf("%s", Val + 1); + scanf("%d", &kase); + printf("S-Tree #%d:\n", t++); + while (kase--) + { + scanf("%s", VVA); + Cal(); + } + printf("\n\n"); + } + return 0; +} diff --git a/713.cpp b/713.cpp new file mode 100644 index 0000000..5737cd6 --- /dev/null +++ b/713.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +class High_Precision +{ +public: + High_Precision() + { + memset(array, 0, sizeof(array)); + len = 0; + } + void GetNumber(char *p); + void PrintHP(); + int array[1000], len; +}; + +void High_Precision::PrintHP() +{ + int i, cnt = 0; + for (i = 0; i < len; i++) + if (array[i] == 0) + { + cnt++; + } + if (cnt == len) + { + cout << 0; + return; + } + i = 0; + while (array[i] == 0) + { + i++; + } + while (array[len - 1] == 0) + { + len--; + } + for (; i < len; i++) + { + cout << array[i]; + } +} + +void High_Precision::GetNumber(char *p) +{ + int i; + len = (int)strlen(p); + for (i = 0; i < len; i++) + { + array[i] = p[i] - '0'; + } +} + +void Plus(High_Precision &a, High_Precision &b, High_Precision &c) +{ + int length, flag = 0, i, sum; + if (a.len >= b.len) + { + length = a.len; + } + else + { + length = b.len; + } + c.len = length; + for (i = 0; i < length; i++) + { + sum = a.array[i] + b.array[i] + flag; + if (sum >= 10) + { + sum %= 10; + flag = 1; + } + else + { + flag = 0; + } + c.array[i] = sum; + } + if (flag == 1) + { + c.array[length] = 1; + c.len++; + } +} + +int main() +{ + int n, t; + char pa[500], pb[500]; + cin >> n; + for (t = 0; t < n; t++) + { + High_Precision a, b, c; + cin >> pa >> pb; + a.GetNumber(pa); + b.GetNumber(pb); + Plus(a, b, c); + c.PrintHP(); + cout << endl; + } + return 0; +} diff --git a/714.cpp b/714.cpp new file mode 100644 index 0000000..a1a5819 --- /dev/null +++ b/714.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define N 505 + +long long a[N]; +int mk[N]; + +inline long long MAX(long long a, long long b) +{ + return a > b ? a : b; +} + +long long Judge(long long mid, const int m, const int k) +{ + long long sum = 0, maxt = 0; + for (int i = m - 1, j = 0; i >= 0; i--) + { + if (j < k - 1 && (i < k - j - 1 || sum + a[i] > mid)) + { + maxt = MAX(sum, maxt); + sum = a[i]; + mk[i] = 1; + j++; + } + else + { + sum += a[i]; + } + } + return MAX(maxt, sum); +} + +int main() +{ + int t, m, k; + long long left, right, mid, ans; + scanf("%d", &t); + while (t--) + { + scanf("%d %d", &m, &k); + left = right = 0; + for (int i = 0; i < m; i++) + { + scanf("%lld", &a[i]); + right += a[i]; + } + while (left < right) + { + mid = (left + right) >> 1;// binary search + ans = Judge(mid, m, k); + if (ans > mid) + { + left = mid + 1; + } + else + { + right = mid; + } + } + memset(mk, 0, sizeof(mk)); + Judge(left, m, k); + printf("%lld", a[0]); + if (mk[0]) + { + printf(" /"); + } + for (int i = 1; i < m; i++) + { + printf(" %lld", a[i]); + if (mk[i]) + { + printf(" /"); + } + } + printf("\n"); + } + return 0; +} diff --git a/715.cpp b/715.cpp new file mode 100644 index 0000000..c20b411 --- /dev/null +++ b/715.cpp @@ -0,0 +1,110 @@ +#include + +using namespace std; + +int n, m, map_[30][30], deg[30], tbl[128]; +char ary[1000][1005], buf[2008]; + +int main() +{ + int count, i, j, k, max, temp; + scanf("%d", &count); + while (count--) + { + scanf("%d%d", &m, &n); + memset(map_, 0, sizeof(map_)); + memset(deg, 0, sizeof(deg)); + gets(buf); + for (i = 0; i < n; i++) + { + gets(ary[i]); + for (j = 0; ary[i][j]; j++) + { + if (ary[i][j] < 97 || ary[i][j] > 122) + { + while (1) + ; + } + } + if (i) + { + for (j = 0; ary[i][j] && ary[i - 1][j] && ary[i][j] == ary[i - 1][j]; j++) + ; + if (ary[i][j] && ary[i - 1][j]) + { + map_[ary[i - 1][j] - 97][ary[i][j] - 97]++; + deg[ary[i][j] - 97]++; + } + } + } + gets(buf); + memset(tbl, 0, sizeof(tbl)); + for (i = 0; buf[i]; i++) + { + if (buf[i] >= 97 && buf[i] < 97 + m) + { + tbl[buf[i] - 97] = 1; + } + } + for (k = 0; k < m; k++) + { + for (i = 0, max = 0; i < m; i++) + { + if (deg[i] == 0) + { + if (tbl[i]) + { + if (max) + { + break; + } + max = 2; + temp = i; + } + else + { + if (max == 2) + { + break; + } + max = 1; + temp = i; + } + } + } + if (i == m) + { + tbl[temp] = k + 97; + deg[temp]--; + for (i = 0; i < m; i++) + { + deg[i] -= map_[temp][i]; + } + } + else + { + break; + } + } + if (k == m) + { + for (i = 0; buf[i]; i++) + { + if (buf[i] >= 97 && buf[i] < 97 + m) + { + printf("%c", tbl[buf[i] - 97]); + } + else + { + printf("%c", buf[i]); + } + } + printf("\n"); + } + else + { + printf("Message cannot be decrypted.\n"); + } + } + return 0; +} diff --git a/716.cpp b/716.cpp new file mode 100644 index 0000000..f7022c4 --- /dev/null +++ b/716.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +#define rep(i, a, b) for (int i = (a); i < (b); ++i) +#define rrep(i, b, a) for (int i = (b); i >= (a); --i) +#define clr(a, x) memset(a, x, sizeof(a)) +#define eb emplace_back + +const int maxn = 100 * 100 * 100 + 5; +int sum[maxn]; + +inline int lowbit(int x) { return x & (-x); } + +void add(int p, int x) +{ + while (p < maxn) + { + sum[p] += x; + p += lowbit(p); + } +} + +int query(int l, int r) +{ + if (l > r) + { + return 0; + } + int ret = 0; + --l; + while (r >= 1) + { + ret += sum[r]; + r -= lowbit(r); + } + while (l >= 1) + { + ret += sum[l]; + l -= lowbit(l); + } + return ret; +} + +int main() +{ + int T; + cin >> T; + while (T--) + { + int M; + scanf("%d", &M); + clr(sum, 0); + int zero_idx; + int rev = 0; + rep(i, 0, M * M * M) + { + int d; + scanf("%d", &d); + if (d == 0) + { + zero_idx = i; + } + else + { + rev += query(d + 1, M * M * M) % 2; + add(d, 1); + rev &= 1; + } + } + if (~M & 1) + { + int z = zero_idx / (M * M); + int y = zero_idx % (M * M) / M; + int x = zero_idx % (M * M) % M; + if (((M - 1) - z) % 2) + { + rev ^= 1; + } + if ((M - 1 - y) % 2) + { + rev ^= 1; + } + } + puts(rev == 0 ? "Puzzle can be solved." : "Puzzle is unsolvable."); + } + return 0; +} diff --git a/718.cpp b/718.cpp new file mode 100644 index 0000000..c7574cd --- /dev/null +++ b/718.cpp @@ -0,0 +1,143 @@ +#include + +using namespace std; + +/* Prob 718 : Skyscraper Floors +STEP1 判斷兩電梯是å¦å¯é€£é€š +STEP2 找到開始樓層和目逼樓層的電梯 +STEP3 å°‡ w 丟給 Floyd-Warshall's Algorithm 去計算 -> 得到 Transitive Closure A* +STEP4 判斷開始樓層和目標樓層是å¦æœ‰é›»æ¢¯ç›¸é€š +PS.注æ„STEP1中,ä¸å¾—超éŽæ­¤æ¸¬è©¦è³‡æ–™çš„樓層數 F +*/ +struct ELEVATOR +{ + long start; + long step; +}; +int MIN(int a, int b) +{ + if (a < b) + { + return a; + } + return b; +} +ELEVATOR elevt[101]; +int F, E, A, B; +int start, goal; +void find(); +void Floyd(); +int Connect(int, int); +int w[101][101]; +int color[101][101]; +int main() +{ + int test; + scanf("%d", &test); + while (test--) + { + scanf("%d %d %d %d", &F, &E, &A, &B); + for (int i = 1; i <= E; i++) + { + scanf("%ld %ld", &elevt[i].step, &elevt[i].start); + } + //initialization + for (int i = 1; i <= E; i++) + for (int j = 1; j <= E; j++) + { + w[i][j] = 0; + } + //connecting... + for (int i = 1; i <= E; i++) + for (int j = 1; j <= E; j++) + if (Connect(i, j) == 1) + { + w[i][j] = 1; + w[j][i] = 1; + } + // finding start to end + find(); + Floyd(); + if (start != -1 && goal != -1) + { + if (w[start][goal] == 1 && w[goal][start] == 1) + { + printf("It is possible to move the furniture.\n"); + } + else + { + printf("The furniture cannot be moved.\n"); + } + } + else + { + printf("The furniture cannot be moved.\n"); + } + } + return 0; +} +int Connect(int elev1, int elev2) +{ + int r1, r2, m, n; + m = elevt[elev1].step; + n = elevt[elev2].step; + r1 = elevt[elev1].start;//r1 + r2 = elevt[elev2].start;//r2 + int flg = 0; // flg = 1 -> connected + for (int q = 0; q <= m; ++q) + { + if ((n * q + r2) % m == r1 && (n * q + r2 <= F)) + { + flg = 1; + break; + } + } + if (flg == 1) + { + return 1; + } + return 0; +} +void find() +{ + start = -1; + goal = -1; + // find starting elevator + for (int i = 1; i <= E; i++) + { + if (A - elevt[i].start == 0) + { + start = i; + } + else if (A - elevt[i].start > elevt[i].step) + if ((A - elevt[i].start) % elevt[i].step == 0) + { + start = i; + break; + } + } + // find aim elevator + for (int i = 1; i <= E; ++i) + { + if (B - elevt[i].start == 0) + { + goal = i; + } + else if (B - elevt[i].start > elevt[i].step) + if ((B - elevt[i].start) % elevt[i].step == 0) + { + goal = i; + break; + } + } +} +void Floyd() +{ + for (int k = 1; k <= E; k++) + for (int i = 1; i <= E; i++) + for (int j = 1; j <= E; j++) + if (w[i][k] && w[k][j]) + { + w[i][j] = 1; + } +} diff --git a/719.cpp b/719.cpp new file mode 100644 index 0000000..bfcb4b0 --- /dev/null +++ b/719.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +#define FOR(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++) + +// find lexicographically min cyclic str in O(len) +inline int min_expr(string s) +{ + s += s; + int len = (int)s.length(), i = 0, j = 1, k = 0; + while (i + k < len && j + k < len) + { + if (s[i + k] == s[j + k]) + { + k++; + } + else if (s[i + k] > s[j + k]) + { + i += k + 1; + if (i <= j) + { + i = j + 1; + } + k = 0; + } + else if (s[i + k] < s[j + k]) + { + j += k + 1; + if (j <= i) + { + j = i + 1; + } + k = 0; + } + } + return min(i, j); +} + +int N; +char A[10005]; + +int main() +{ + scanf("%d", &N); + FOR(n, 0, N) + { + scanf("%s", A); + string s(A); + int pos = min_expr(s); + printf("%d\n", pos + 1);// 1-based idx + } + return 0; +} diff --git a/721.cpp b/721.cpp new file mode 100644 index 0000000..8ff8f23 --- /dev/null +++ b/721.cpp @@ -0,0 +1,136 @@ +#include + +using namespace std; + +#define INF 1000000000 + +struct node +{ + int nam; + int cost; + node *next; + node *back; +}; +struct Edge +{ + int cost; + char color; + node *next; + node *back; +}; +Edge *E; +int P, Q; +queue que; +void MakeEdge() +{ + int i, u, v, c; + node *x; + E = new Edge[P + 1]; + for (i = 1; i <= P; i++) + { + E[i].next = E[i].back = NULL; + E[i].color = 0; + E[i].cost = INF; + } + for (i = 0; i < Q; i++) + { + scanf("%d%d%d", &u, &v, &c); + x = new node; + x->nam = v; + x->cost = c; + x->next = E[u].next; + E[u].next = x; + x = new node; + x->nam = u; + x->cost = c; + x->back = E[v].back; + E[v].back = x; + } +} +void GetGo() +{ + int u, v; + node *x; + E[1].color = 1; + E[1].cost = 0; + que.push(1); + while (!que.empty()) + { + u = que.front(); + que.pop(); + E[u].color = 0; + x = E[u].next; + while (x != NULL) + { + v = x->nam; + if (E[u].cost + x->cost < E[v].cost) + { + E[v].cost = E[u].cost + x->cost; + if (E[v].color == 0) + { + que.push(v); + E[v].color = 1; + } + } + x = x->next; + } + } +} +void GetBack() +{ + int u, v; + node *x; + E[1].color = 1; + E[1].cost = 0; + que.push(1); + while (!que.empty()) + { + u = que.front(); + que.pop(); + E[u].color = 0; + x = E[u].back; + while (x) + { + v = x->nam; + if (E[u].cost + x->cost < E[v].cost) + { + E[v].cost = E[u].cost + x->cost; + if (E[v].color == 0) + { + que.push(v); + E[v].color = 1; + } + } + x = x->back; + } + } +} +void Cal() +{ + int total = 0, i; + GetGo(); + for (i = 1; i <= P; i++) + { + total += E[i].cost; + E[i].cost = INF; + E[i].color = 0; + } + GetBack(); + for (i = 1; i <= P; i++) + total += E[i].cost; + printf("%d\n", total); +} + +int main() +{ + int k; + scanf("%d", &k); + while (k--) + { + scanf("%d%d", &P, &Q); + MakeEdge(); + Cal(); + delete[] E; + } + return 0; +} diff --git a/722.cpp b/722.cpp new file mode 100644 index 0000000..b0292fd --- /dev/null +++ b/722.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +int n, m; +int num; +char field[100][100]; +bool visited[100][100]; + +int bfs(int a, int b) +{ + int counter = 0; + pair tmp; + queue> q; + tmp.first = a; + tmp.second = b; + if (field[a][b] != '0') + { + return 0; + } + counter++; + visited[a][b] = true; + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && !visited[tmp.first - 1][tmp.second] && field[tmp.first - 1][tmp.second] == '0') + { + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + counter++; + } + if (tmp.second > 0 && !visited[tmp.first][tmp.second - 1] && field[tmp.first][tmp.second - 1] == '0') + { + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + counter++; + } + if (tmp.first < n - 1 && !visited[tmp.first + 1][tmp.second] && field[tmp.first + 1][tmp.second] == '0') + { + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + counter++; + } + if (tmp.second < m - 1 && !visited[tmp.first][tmp.second + 1] && field[tmp.first][tmp.second + 1] == '0') + { + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + counter++; + } + } + return counter; +} + +int main() +{ + int cases; + string input; + int a, b; + stringstream ss; + map result; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + n = 0; + m = 0; + num = 1; + result.clear(); + for (int i = 0; i < 100; i++) + for (int j = 0; j < 100; j++) + { + visited[i][j] = false; + field[i][j] = '1'; + } + getline(cin, input); + ss << input; + ss >> a >> b; + ss.str(""); + ss.clear(); + while (getline(cin, input)) + { + if (input == "") + { + break; + } + if (input[0] == '1' || input[0] == '0') + { + for (int i = 0, sz = input.size(); i < sz; i++) + { + field[n][i] = input[i]; + } + n++; + m = input.size(); + } + } + cout << bfs(a - 1, b - 1) << endl; + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/725.cpp b/725.cpp new file mode 100644 index 0000000..5d9d8fa --- /dev/null +++ b/725.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +int main() +{ + int a, b; + int n; + bool first = true; + int nums[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + vector> v[100]; + do + { + a = nums[0] * 10000 + nums[1] * 1000 + nums[2] * 100 + nums[3] * 10 + nums[4]; + b = nums[5] * 10000 + nums[6] * 1000 + nums[7] * 100 + nums[8] * 10 + nums[9]; + if (a % b == 0) + { + v[a / b].push_back(make_pair(a, b)); + } + } while (next_permutation(nums, nums + 10)); + while (cin >> n) + { + if (n == 0) + { + break; + } + if (!first) + { + cout << endl; + } + if (v[n].size() == 0) + { + cout << "There are no solutions for " << n << "." << endl; + } + else + { + for (int i = 0, sz = v[n].size(); i < sz; i++) + { + cout << v[n][i].first << " / " << (v[n][i].second < 10000 ? "0" : "") << v[n][i].second << " = " << n << endl; + } + } + first = false; + } + return 0; +} diff --git a/726.cpp b/726.cpp new file mode 100644 index 0000000..ed21c65 --- /dev/null +++ b/726.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +typedef struct +{ + char w; + int v; +} word; +word W1[128], W2[128]; +int cmp(const void *a, const void *b) +{ + if (((word *)a)->v != ((word *)b)->v) + { + return ((word *)b)->v - ((word *)a)->v; + } + return ((word *)a)->w - ((word *)b)->w; +} +char in[50000], *O[50000], T[128]; +int main() +{ + int i, j, n, len; + for (i = 0; i < 128; i++) + { + W1[i].w = W2[i].w = T[i] = i; + W1[i].v = W2[i].v = 0; + } + while (gets(in)) + { + if (in[0] == '\0') + { + break; + } + for (i = 0; in[i]; i++) + { + W1[in[i] | 32].v++; + } + } + qsort(W1 + 'a', 26, sizeof(word), cmp); + for (n = 0; gets(in); n++) + { + for (i = 0; in[i]; i++) + { + W2[in[i] | 32].v++; + } + len = strlen(in); + O[n] = (char *)malloc((len + 1) * sizeof(char)); + strcpy(O[n], in); + } + qsort(W2 + 'a', 26, sizeof(word), cmp); + for (i = 'a'; i <= 'z'; i++) + { + T[(int)W2[i].w] = W1[(int)i].w; + T[(int)W2[i].w ^ 32] = W1[(int)i].w ^ 32; + } + for (i = 0; i < n; i++) + { + for (j = 0; O[i][j]; j++) + { + printf("%c", T[(int)O[i][j]]); + } + printf("\n"); + } + return 0; +} diff --git a/727.cpp b/727.cpp new file mode 100644 index 0000000..b7b538f --- /dev/null +++ b/727.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +bool compareOperator(char a, char b) +{ + return (a == '*' || a == '/') >= (b == '*' || b == '/'); +} + +int main() +{ + int n; + char c; + stack s; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + cin.ignore(100, '\n'); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> c; + cin.ignore(100, '\n'); + if (c == '+' || c == '-' || c == '*' || c == '/') + { + if (s.empty()) + { + s.push(c); + } + else + { + while (!s.empty() && s.top() != '(') + { + if (compareOperator(s.top(), c)) + { + cout << s.top(); + s.pop(); + } + else + { + break; + } + } + s.push(c); + } + } + else if (c == '(') + { + s.push(c); + } + else if (c == ')') + { + while (s.top() != '(') + { + cout << s.top(); + s.pop(); + } + s.pop();// pop '(' + } + else// operand + { + cout << c; + } + } + while (!s.empty()) + { + cout << s.top(); + s.pop(); + } + cout << endl; + if (n) + { + cout << endl; + } + } +} diff --git a/729.cpp b/729.cpp index af94fa2..0c3c66e 100644 --- a/729.cpp +++ b/729.cpp @@ -1,54 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - cin>>test; - while(test--){ - int N, zero, ones; - cin>>N>>ones; - int arr[N], i; - for(i=0; i 0) - cout< + +using namespace std; + +/*************************** +The Hamming Distance Problem + 729 +****************************/ +int N, H; +void Gener() +{ + long val, i, j, p, k, q; + char temp[20]; + val = pow(2, N - 1); + for (i = 1; i <= pow(2, N); i++) + { + p = 0; + q = 0; + for (j = val; j > 0; j = j / 2) + { + if (i & j) + { + temp[q++] = '1'; + p++; + } + else + { + temp[q++] = '0'; + } + } + if (p == H) + { + temp[q] = NULL; + printf("%s\n", temp); + } + } +} +int main() +{ + int kase; + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &N, &H); + Gener(); + if (kase) + { + putchar('\n'); + } + } + return 0; +} diff --git a/732.cpp b/732.cpp new file mode 100644 index 0000000..edc7902 --- /dev/null +++ b/732.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +/* +732 +Anagram By Stack +*/ +#define MAXN 1000 +struct ss +{ + int fre1, fre2; +} L[300]; +char Str[MAXN], Ter[MAXN]; +int Len; +int NotMatch() +{ + int i, j, k; + Len = strlen(Str); + k = strlen(Ter); + if (Len != k) + { + return 1; + } + for (i = 0; Str[i]; i++) + { + j = Str[i]; + L[j].fre1++; + k = Ter[i]; + L[k].fre2++; + } + for (i = 0; i < 300; i++) + if (L[i].fre1 != L[i].fre2) + { + return 1; + } + return 0; +} +void Recur(char ch, int level, int hd, int ph, int pp, char C[], char S[], char Res[]) +{ + int i; + char tS[MAXN]; + C[level] = ch; + for (i = 0; i < ph; i++) + { + tS[i] = S[i]; + } + if (ch == 'i') + { + if (hd >= Len) + { + return; + } + tS[ph++] = Str[hd++]; + tS[ph] = NULL; + } + if (ch == 'o') + { + if (ch == 0) + { + return; + } + if (S[ph - 1] != Ter[pp]) + { + return; + } + Res[pp++] = S[ph - 1]; + ph--; + } + if (level == 2 * Len - 1) + { + Res[pp] = NULL; + printf("%c", C[0]); + if (!strcmp(Res, Ter)) + { + for (i = 1; i < 2 * Len; i++) + { + printf(" %c", C[i]); + } + } + printf("\n"); + return; + } + Recur('i', level + 1, hd, ph, pp, C, tS, Res); + Recur('o', level + 1, hd, ph, pp, C, tS, Res); +} +int main() +{ + char S[MAXN], Res[MAXN]; + char C[MAXN]; + while (scanf("%s", Str) != EOF) + { + scanf("%s", Ter); + if (NotMatch()) + { + printf("[\n"); + printf("]\n"); + continue; + } + printf("[\n"); + Recur('i', 0, 0, 0, 0, Res, S, C); + printf("]\n"); + memset(L, 0, sizeof(int) * 300); + } + return 0; +} diff --git a/734.cpp b/734.cpp new file mode 100644 index 0000000..e47ea7e --- /dev/null +++ b/734.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int mod = 6; + +class st +{ +public: + int p; + vector num[6]; + void set(string a) + { + p = 6; + rep(i, 6) + { + rep(j, 6) num[i].push_back(a[j] - '0'); + a = a.substr(1) + a[0]; + } + } +}; + +bool solve(int now, vector *ans, int *id, bool *used, st *in) +{ + if (now == 6) + { + if (ans[1][4] != ans[6][1]) + { + return false; + } + cout << "PEG HEX POSITION" << endl; + cout << "--- --- --------" << endl; + set S; + rep(i, 7) + { + S.insert(id[i]); + cout << " " << i << " " << (char)(id[i] + 'A') << " "; + rep(j, 6) cout << ans[i][j]; + cout << endl; + } + return true; + } + rep(i, 7) + { + if (used[i]) + { + continue; + } + used[i] = true; + rep(j, in[i].p) + { + vector &check = in[i].num[j]; + if (ans[0][now] == check[(now + 3) % mod] && + (now == 0 || check[(now - 2 + mod) % mod] == ans[now][(now + 1) % mod])) + { + ans[now + 1] = check; + id[now + 1] = i; + if (solve(now + 1, ans, id, used, in)) + { + return true; + } + } + } + used[i] = false; + } + return false; +} + +bool solve(st *in) +{ + vector ans[7]; + int id[7]; + bool used[7] = {false}; + rep(i, 7) + { + ans[0] = in[i].num[0]; + id[0] = i; + used[i] = true; + if (solve(0, ans, id, used, in)) + { + return true; + } + used[i] = false; + } + cout << "No solution possible" << endl; + return false; +} + +main() +{ + string tmp; + while (cin >> tmp) + { + st in[7]; + in[0].set(tmp); + REP(i, 1, 7) + cin >> tmp, + in[i].set(tmp); + solve(in); + cout << "************************************" << endl; + cin >> tmp; + } + return false; +} diff --git a/735.cpp b/735.cpp new file mode 100644 index 0000000..581a89a --- /dev/null +++ b/735.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +const int Max_Pos = 20, Min = 0, Max_Posx3 = 60; +int C[181], P[181]; +int Dart1, Dart2, Dart3, Score, PCount, Count; +bool Possible_Scores[80]; +inline int Minimum(int i, int j) +{ + return (i < j) ? i : j; +} +int temp[3], len; +void Add(int v) +{ + for (int i = 0; i < len; i++) + if (temp[i] == v) + { + return; + } + temp[len++] = v; +} +int main() +{ + Possible_Scores[50] = true; + for (Dart1 = Min; Dart1 <= Max_Pos; Dart1++) + { + if (Dart1 <= 80) + { + Possible_Scores[Dart1] = true; + } + if (2 * Dart1 <= 80) + { + Possible_Scores[2 * Dart1] = true; + } + if (3 * Dart1 <= 80) + { + Possible_Scores[3 * Dart1] = true; + } + } + for (Score = 1; Score <= 180; Score++) + { + PCount = Count = 0; + int bound1, bound2, bound3; + bound1 = Minimum(Score, Max_Posx3); + for (Dart1 = Min; Dart1 <= bound1; Dart1++) + { + bound2 = Minimum(Score - Dart1, Max_Posx3); + for (Dart2 = Dart1; Dart2 <= bound2; Dart2++) + { + bound3 = Minimum(Score - Dart1 - Dart2, Max_Posx3); + for (Dart3 = Dart2; Dart3 <= bound3; Dart3++) + if (Possible_Scores[Dart1] && Possible_Scores[Dart2] && Possible_Scores[Dart3] && Dart1 + Dart2 + Dart3 == Score) + { + Count++; + len = 0; + Add(Dart1); + Add(Dart2); + Add(Dart3); + if (len == 3) + { + PCount += 6; + } + else if (len == 2) + { + PCount += 3; + } + else + { + PCount++; + } + } + } + } + C[Score] = Count; + P[Score] = PCount; + } + while (scanf("%d", &Score), Score > 0) + { + PCount = Count = 0; + if (Score <= 180) + { + PCount = P[Score]; + Count = C[Score]; + } + if (Count != 0) + { + printf("NUMBER OF COMBINATIONS THAT SCORES %d IS %d.\n", Score, Count); + printf("NUMBER OF PERMUTATIONS THAT SCORES %d IS %d.\n", Score, PCount); + } + else + { + printf("THE SCORE OF %d CANNOT BE MADE WITH THREE DARTS.\n", Score); + } + puts("**********************************************************************"); + } + puts("END OF OUTPUT"); +} diff --git a/736.cpp b/736.cpp new file mode 100644 index 0000000..556bed9 --- /dev/null +++ b/736.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +char dir[8][5] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}; +int diry[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; +int dirx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; + +char map_[52][52], str[200]; +int n, i, j, k; + +bool search(int d, int i, int j, int l, int k) +{ + if (d == strlen(str)) + { + return true; + } + else if (i < 0 || i >= n || j < 0 || j >= n) + { + return false; + } + else if (map_[i][j] == ' ') + { + return search(d, i + diry[k], j + dirx[k], l, k); + } + else if (map_[i][j] != str[l]) + { + return false; + } + else + { + return search(d + 1, i + diry[k], j + dirx[k], l + 1, k); + } +} +int main() +{ + gets(str); + int times = atoi(str); + gets(str); + bool flag; + for (; times; times--) + { + gets(str); + n = atoi(str); + for (i = 0; i < n; i++) + { + gets(map_[i]); + } + while (gets(str) != NULL) + { + flag = false; + if (strlen(str) == 0) + { + break; + } + printf("\n"); + puts(str); + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + for (k = 0; k < 8; k++) + if (map_[i][j] == str[0] && search(0, i, j, 0, k)) + { + printf("(%d,%d) - %s\n", i + 1, j + 1, dir[k]); + flag = true; + } + if (!flag) + { + puts("not found"); + } + } + if (times != 1) + { + printf("\n"); + } + } +} diff --git a/737.cpp b/737.cpp new file mode 100644 index 0000000..05c8369 --- /dev/null +++ b/737.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; + +#define MAXCUBES 10000 +#define CUBESDATA 4 +#define maxi(a, b) (a < b ? b : a) +#define mini(a, b) (a < b ? a : b) + +int main() +{ + int cubes[MAXCUBES][CUBESDATA]; + int n; + int maxx, minx; + int maxy, miny; + int maxz, minz; + int vol; + while (true) + { + cin >> n; + if (n == 0) + { + break; + } + for (int i = 0; i < n; i++) + { + cin >> cubes[i][0]; + cin >> cubes[i][1]; + cin >> cubes[i][2]; + cin >> cubes[i][3]; + } + minx = cubes[0][0]; + miny = cubes[0][1]; + minz = cubes[0][2]; + maxx = minx + cubes[0][3]; + maxy = miny + cubes[0][3]; + maxz = minz + cubes[0][3]; + for (int i = 1; i < n; i++) + { + minx = maxi(minx, cubes[i][0]); + miny = maxi(miny, cubes[i][1]); + minz = maxi(minz, cubes[i][2]); + maxx = mini(maxx, cubes[i][0] + cubes[i][3]); + maxy = mini(maxy, cubes[i][1] + cubes[i][3]); + maxz = mini(maxz, cubes[i][2] + cubes[i][3]); + } + vol = maxi(0, (maxx - minx) * (maxy - miny) * (maxz - minz)); + cout << vol << endl; + } + return 0; +} diff --git a/738.cpp b/738.cpp new file mode 100644 index 0000000..ef5ed22 --- /dev/null +++ b/738.cpp @@ -0,0 +1,276 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define pb push_back + +#define N 5000 +#define OUTPUT 0 +#define OR 1 +#define AND 2 +#define NOT 3 +#define ALPHABET 4 + +typedef vector vint; +typedef vector vbool; +class Node +{ +public: + vbool val; + int type; + int chara; + void take_and(vbool &a, vbool &b) + { + val.clear(); + rep(i, a.size()) val.pb(a[i] & b[i]); + } + void take_or(vbool &a, vbool &b) + { + val.clear(); + rep(i, a.size()) val.pb(a[i] | b[i]); + } + void take_not(vbool &a) + { + val.clear(); + rep(i, a.size()) val.pb(!a[i]); + } +}; + +Node node[N]; +vint edge[N]; //should be clear +vbool alpha[26]; //should be clear +int assign[100][100]; //should be initialize with -1 +bool visited[100][100];//initialiize with false +string m[128]; //should be clear?? + +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +int opp[] = {1, 0, 3, 2}; +char verify[4] = {'|', '|', '-', '-'}; + +int convert(int r) +{ + int index = 0; + rep(i, r) + { + rep(j, m[i].size()) + { + if (isupper(m[i][j])) + { + node[index].type = ALPHABET; + node[index].chara = m[i][j] - 'A'; + assign[i][j] = index++; + } + else if (m[i][j] == ':') + { + if (i != 0 && i + 1 < r && m[i - 1][j] == ':' && + m[i + 1][j] == ':') + { + m[i][j] = ' '; + } + } + else if (m[i][j] == '\\' || m[i][j] == '/') + { + } + else if (m[i][j] == ')')//AND + { + node[index].type = AND; + assign[i][j] = index++; + } + else if (m[i][j] == '>')//OR + { + node[index].type = OR; + assign[i][j] = index++; + } + else if (m[i][j] == 'o')//NOT + { + node[index].type = NOT; + assign[i][j] = index++; + } + else if (m[i][j] == '?')//OUTPUT + { + node[index].type = OUTPUT; + assign[i][j] = index++; + } + } + } + rep(i, r) rep(j, m[i].size()) if (m[i][j] == ':') + { + m[i][j] = '-'; + } + return index; +} + +void tsort(int r, int y, int x, vint &res, int prev, int prevnode) +{ + // cout << y <<" " <= 100 || ney < 0 || ney >= r) + { + continue; + } + if ((m[ney][nex] == '-' || m[ney][nex] == '|') && m[ney][nex] != verify[prev] && + m[ney][nex] == verify[i]) + { + tsort(r, ney, nex, res, i, prevnode); + } + } + } + else if (m[y][x] == '-') + { + int nex = x + dx[prev], ney = y + dy[prev]; + if (m[ney][nex] == '\\') + { + tsort(r, ney + 1, nex + 1, res, prev, prevnode); + } + else if (m[ney][nex] == '/') + { + tsort(r, ney - 1, nex + 1, res, prev, prevnode); + } + else + { + tsort(r, ney, nex, res, prev, prevnode); + } + } + else if (m[y][x] == '|') + { + int nex = x + dx[prev], ney = y + dy[prev]; + tsort(r, ney, nex, res, prev, prevnode); + } + else if (isupper(m[y][x])) + { + rep(i, 4) + { + int nex = x + dx[i], ney = y + dy[i]; + if (nex < 0 || nex >= 100 || ney < 0 || ney >= r) + { + continue; + } + if (verify[i] == m[ney][nex]) + { + tsort(r, ney, nex, res, i, prevnode); + } + } + } + else if (m[y][x] == 'o') + { + int nex = x + dx[prev], ney = y + dy[prev]; + tsort(r, ney, nex, res, prev, prevnode); + } + else if (m[y][x] == ')' || m[y][x] == '>') + { + int nex = x + dx[prev], ney = y + dy[prev]; + tsort(r, ney, nex, res, prev, prevnode); + } + else if (m[y][x] == '?') + { + } + else + { + assert(false); + } + if (assign[y][x] != -1) + { + res.pb(assign[y][x]); + } +} + +void solve(int r)//2nd argument , in, is result of tsort +{ + int n = convert(r); + int tar = -10000; + /* + rep(i,r){ + rep(j,m[i].size()){ + if (assign[i][j] == -1)cout << m[i][j]; + else cout << (int)assign[i][j]; + } + cout << endl; + } + */ + rep(i, n) edge[i].clear(); + vint in; + rep(i, r) + rep(j, m[i].size()) if (isupper(m[i][j])) + { + tsort(r, i, j, in, -1, assign[i][j]); + } + reverse(in.begin(), in.end()); + rep(i, in.size()) + { + int now = in[i]; + if (node[now].type == ALPHABET) + { + node[now].val = alpha[node[now].chara]; + } + else if (node[now].type == OUTPUT) + { + node[now].val = node[edge[now][0]].val; + tar = now; + } + else if (node[now].type == AND) + { + node[now].take_and(node[edge[now][0]].val, node[edge[now][1]].val); + } + else if (node[now].type == OR) + { + node[now].take_or(node[edge[now][0]].val, node[edge[now][1]].val); + } + else if (node[now].type == NOT) + { + node[now].take_not(node[edge[now][0]].val); + } + } + rep(i, node[tar].val.size()) cout << node[tar].val[i] << endl; +} + +main() +{ + int tc = 0; + while (getline(cin, m[0])) + { + //if (tc++)puts(""); + rep(i, 26) alpha[i].clear(); + rep(i, 100) rep(j, 100) + { + assign[i][j] = -1; + visited[i][j] = false; + } + int r = 1; + while (getline(cin, m[r]) && m[r][0] != '*') + { + r++; + } + rep(i, r) while (m[i].size() < 100) + { + m[i] += ' '; + } + REP(i, r + 1, 100) + { + m[i].clear(); + } + string tmp; + while (getline(cin, tmp) && tmp != "*") + { + rep(i, 26) alpha[i].pb(tmp[i] == '1' ? true : false); + } + solve(r); + puts(""); + } +} diff --git a/739.cpp b/739.cpp new file mode 100644 index 0000000..b22fd83 --- /dev/null +++ b/739.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +int L[128] = {0}, S[5]; +char name[30]; +int main() +{ + int i, len; + L['B'] = L['P'] = L['F'] = L['V'] = 1; + L['C'] = L['S'] = L['K'] = L['G'] = L['J'] = L['Q'] = L['X'] = L['Z'] = 2; + L['D'] = L['T'] = 3; + L['L'] = 4; + L['M'] = L['N'] = 5; + L['R'] = 6; + printf(" NAME SOUNDEX CODE\n"); + while (gets(name)) + { + for (i = 1, len = 0; name[i] && len < 3; i++) + { + if (L[name[i]] && L[name[i]] != L[name[i - 1]]) + { + S[len++] = L[name[i]]; + } + } + while (name[i]) + { + i++; + } + while (i < 25) + { + name[i++] = ' '; + } + name[i] = '\0'; + while (len < 3) + { + S[len++] = 0; + } + printf(" %s%c%d%d%d\n", name, name[0], S[0], S[1], S[2]); + } + printf(" END OF OUTPUT\n"); + return 0; +} diff --git a/740.cpp b/740.cpp new file mode 100644 index 0000000..540ef11 --- /dev/null +++ b/740.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +int bin2int(const string &s, string::iterator start, string::iterator end) +{ + int tmp = 0; + int counter = 4; + for (; start != end; start++) + { + if (*start == '1') + { + tmp += (1 << counter); + } + counter--; + } + return tmp; +} + +int main() +{ + int n; + string input; + string downshift; + string upshift; + bool down; + getline(cin, downshift); + getline(cin, upshift); + while (getline(cin, input)) + { + down = true; + for (int i = 0, sz = input.size(); i < sz; i += 5) + { + n = bin2int(input, input.begin() + i, input.begin() + i + 5); + if (n == 31) + { + down = false; + } + else if (n == 27) + { + down = true; + } + else + { + if (down) + { + cout << downshift[n]; + } + else + { + cout << upshift[n]; + } + } + } + cout << endl; + } + return 0; +} diff --git a/741.cpp b/741.cpp new file mode 100644 index 0000000..6897904 --- /dev/null +++ b/741.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +/* column 0 is copy of column n */ +static char data[512][512]; +static int n; + +/* Completes table in data, given first and last column */ +static void complete() +{ + int i, col, row; + register char *p, *q; + register int j; + for (col = 2; col < n; col++) + { + for (row = 0; row < n;) + { + /* find all known prefixes starting with text in + data[row][1..col-1] and write them below in current + column starting */ + for (q = data[row], i = 0; i < n; i++) + { + for (p = data[i], j = 1; j < col; j++, p++) + if (*p != q[j]) + { + break; + } + if (j >= col) + { + data[row++][col] = *p; + if (row == n || + data[row][col - 1] != q[col - 1]) + { + break; + } + } + } + } + } +} + +int main() +{ + static char buf[512]; + static int row, cnt[26], t; + register int i, j; + for (t = 0; scanf(" %[A-Za-z] %d", buf, &row) == 2 && row > 0; t++) + { + for (i = 0; buf[i]; i++) + if (buf[i] >= 'a' && buf[i] <= 'z') + { + buf[i] = buf[i] + 'A' - 'a'; + } + else if (!(buf[i] >= 'A' && buf[i] <= 'Z')) + { + break; + } + n = i; + for (i = 0; i < n; i++) + { + data[i][0] = data[i][n] = buf[i]; + } + /* prepare column 1, by sorting letters of given word */ + for (i = 0; i < 26; i++) + { + cnt[i] = 0; + } + for (i = 0; i < n; i++) + { + cnt[buf[i] - 'A']++; + } + for (i = 0, j = 0; j < 26; j++) + while (cnt[j]-- > 0) + { + data[i++][1] = j + 'A'; + } + complete(); + if (t > 0) + { + printf("\n"); + } + data[row - 1][n + 1] = '\0'; + printf("%s\n", &data[row - 1][1]); + } + return 0; +} diff --git a/742.cpp b/742.cpp new file mode 100644 index 0000000..31615d0 --- /dev/null +++ b/742.cpp @@ -0,0 +1,221 @@ +#include + +using namespace std; + +#define RED 0 +#define GREEN 1 + +int final, lside, rside, nextr, nextg, numr[7], numg[7]; +char usedr[14], usedg[14]; + +struct data +{ + int id, num; +} aryr[7][7], aryg[7][7]; + +void dfs(int turn, int pass) +{ + int i, find, temp; + find = 0; + if (turn == RED) + { + if (nextg == 0) + { + final |= (1 << GREEN); + return; + } + for (i = 0; i < numr[lside]; i++) + { + if (usedr[aryr[lside][i].id] == 0) + { + find = 1; + usedr[aryr[lside][i].id] = 1; + temp = lside; + lside = aryr[lside][i].num; + nextr--; + dfs(1 - turn, 0); + if (final == 3) + { + return; + } + nextr++; + lside = temp; + usedr[aryr[lside][i].id] = 0; + } + } + if (lside != rside) + { + for (i = 0; i < numr[rside]; i++) + { + if (usedr[aryr[rside][i].id] == 0) + { + find = 1; + usedr[aryr[rside][i].id] = 1; + temp = rside; + rside = aryr[rside][i].num; + nextr--; + dfs(1 - turn, 0); + if (final == 3) + { + return; + } + nextr++; + rside = temp; + usedr[aryr[rside][i].id] = 0; + } + } + } + if (find == 0) + { + if (pass) + { + return; + } + dfs(1 - turn, 1); + } + } + else + { + if (nextr == 0) + { + final |= (1 << RED); + return; + } + for (i = 0; i < numg[lside]; i++) + { + if (usedg[aryg[lside][i].id] == 0) + { + find = 1; + usedg[aryg[lside][i].id] = 1; + temp = lside; + lside = aryg[lside][i].num; + nextg--; + dfs(1 - turn, 0); + if (final == 3) + { + return; + } + nextg++; + lside = temp; + usedg[aryg[lside][i].id] = 0; + } + } + if (lside != rside) + { + for (i = 0; i < numg[rside]; i++) + { + if (usedg[aryg[rside][i].id] == 0) + { + find = 1; + usedg[aryg[rside][i].id] = 1; + temp = rside; + rside = aryg[rside][i].num; + nextg--; + dfs(1 - turn, 0); + if (final == 3) + { + return; + } + nextg++; + rside = temp; + usedg[aryg[rside][i].id] = 0; + } + } + } + if (find == 0) + { + if (pass) + { + return; + } + dfs(1 - turn, 1); + } + } +} + +int main() +{ + int cas, n, i, l, r, max, temp, idtemp; + cas = 0; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + memset(numr, 0, sizeof(numr)); + memset(numg, 0, sizeof(numg)); + memset(usedr, 0, sizeof(usedr)); + memset(usedg, 0, sizeof(usedg)); + for (i = 0, max = -1; i < n; i++) + { + scanf("%d%d", &l, &r); + if (l == r && l > max) + { + max = l; + temp = RED; + idtemp = i; + } + aryr[l][numr[l]].id = i; + aryr[l][numr[l]++].num = r; + if (l != r) + { + aryr[r][numr[r]].id = i; + aryr[r][numr[r]++].num = l; + } + } + for (i = 0; i < n; i++) + { + scanf("%d%d", &l, &r); + if (l == r && l > max) + { + max = l; + temp = GREEN; + idtemp = i; + } + aryg[l][numg[l]].id = i; + aryg[l][numg[l]++].num = r; + if (l != r) + { + aryg[r][numg[r]].id = i; + aryg[r][numg[r]++].num = l; + } + } + nextr = nextg = n; + if (temp == RED) + { + nextr--; + usedr[idtemp] = 1; + } + else + { + nextg--; + usedg[idtemp] = 1; + } + lside = rside = max; + final = 0; + dfs(1 - temp, 0); + if (cas) + { + printf("\n"); + } + cas = 1; + if (final == 0) + { + printf("No players can win\n"); + } + else if (final == 3) + { + printf("Both players can win\n"); + } + else if (final & (1 << RED)) + { + printf("Only player Red can win\n"); + } + else + { + printf("Only player Green can win\n"); + } + } + return 0; +} diff --git a/743.cpp b/743.cpp new file mode 100644 index 0000000..a43683d --- /dev/null +++ b/743.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +char in[40], out[120000]; +int rec(int x) +{ + int len; + switch (in[x]) + { + case '2': + if (in[x + 1] == '\0') + { + return -1; + } + strcpy(out, in + x + 1); + return 1; + case '3': + if (rec(x + 1) < 0) + { + return -1; + } + len = strlen(out); + strcpy(out + len + 1, out); + out[len] = '2'; + return 1; + default: + return -1; + } +} +int main() +{ + int i, len1, len2; + while (gets(in)) + { + if (in[0] == '0' && in[1] == '\0') + { + break; + } + for (i = 0; in[i]; i++) + { + if (in[i] == '0') + { + break; + } + } + if (in[i] == '0') + { + printf("NOT ACCEPTABLE\n"); + continue; + } + if (rec(0) < 0) + { + printf("NOT ACCEPTABLE\n"); + continue; + } + else + { + printf("%s\n", out); + } + } + return 0; +} diff --git a/745.cpp b/745.cpp new file mode 100644 index 0000000..a2a171e --- /dev/null +++ b/745.cpp @@ -0,0 +1,204 @@ +#include + +using namespace std; + +int n, m, lst[10], num[10], aryx[10][500], aryy[10][500]; +char board[25][25], board2[25][25], board3[25][25], board4[25][25], used[10], buf[2008]; +double ary[4]; + +int valid(int x, int y) +{ + return x >= 0 && x < n && y >= 0 && y < n; +} + +int dfs(int x, int y) +{ + int i, j, tx, ty, now; + if (y == n) + { + board[x][y] = 0; + y = 0; + x++; + if (x == n) + { + return 1; + } + } + if (board[x][y] == -1) + { + for (i = 0; i < m; i++) + { + if (used[now = lst[i]] == 0) + { + for (j = 0; j < num[now]; j++) + { + if (valid(tx = x + aryx[now][j], ty = y + aryy[now][j]) == 0 || board[tx][ty] != -1) + { + break; + } + } + if (j == num[now]) + { + used[now] = 1; + for (j = 0; j < num[now]; j++) + { + board[x + aryx[now][j]][y + aryy[now][j]] = now + 48; + } + if (dfs(x, y + 1)) + { + return 1; + } + for (j = 0; j < num[now]; j++) + { + board[x + aryx[now][j]][y + aryy[now][j]] = -1; + } + used[now] = 0; + } + } + } + } + else + { + return dfs(x, y + 1); + } + return 0; +} + +int main() +{ + int i, j, k, p, q, temp, step; + double dtemp, max; + while (scanf("%d", &n) == 1) + { + if (n == 0) + { + break; + } + scanf("%d", &m); + gets(buf); + gets(buf); + memset(num, 0, sizeof(num)); + for (i = 0; i < m; i++) + { + for (k = 0; buf[k] == ' '; k++) + ; + temp = buf[k] - 48; + lst[i] = temp; + for (k = 0, step = -1; buf[k]; k++) + { + if (buf[k] != ' ') + { + if (step == -1) + { + step = k; + } + aryx[temp][num[temp]] = 0; + aryy[temp][num[temp]++] = k - step; + } + } + for (j = 1;; j++) + { + gets(buf); + for (k = 0; buf[k] == ' '; k++) + ; + if (temp != buf[k] - 48) + { + break; + } + for (; buf[k]; k++) + { + if (buf[k] != ' ') + { + aryx[temp][num[temp]] = j; + aryy[temp][num[temp]++] = k - step; + } + } + } + } + memset(used, 0, sizeof(used)); + memset(board, -1, sizeof(board)); + dfs(0, 0); + for (i = 0, ary[0] = 0.0; i < n; i++) + { + sscanf(board[i], "%lf", &dtemp); + ary[0] += dtemp; + } + for (i = 0, q = n - 1, ary[1] = 0.0; i < n; i++, q--) + { + for (j = 0, p = 0; j < n; j++, p++) + { + board2[p][q] = board[i][j]; + } + } + for (i = 0; i < n; i++) + { + board2[i][n] = 0; + sscanf(board2[i], "%lf", &dtemp); + ary[1] += dtemp; + } + for (i = 0, p = n - 1, ary[2] = 0.0; i < n; i++, p--) + { + for (j = 0, q = n - 1; j < n; j++, q--) + { + board3[p][q] = board[i][j]; + } + } + for (i = 0; i < n; i++) + { + board3[i][n] = 0; + sscanf(board3[i], "%lf", &dtemp); + ary[2] += dtemp; + } + for (i = 0, q = 0, ary[3] = 0.0; i < n; i++, q++) + { + for (j = 0, p = n - 1; j < n; j++, p--) + { + board4[p][q] = board[i][j]; + } + } + for (i = 0; i < n; i++) + { + board4[i][n] = 0; + sscanf(board4[i], "%lf", &dtemp); + ary[3] += dtemp; + } + for (i = 1, max = ary[0], temp = 0; i < 4; i++) + { + if (ary[i] > max) + { + max = ary[i]; + temp = i; + } + } + if (temp == 0) + { + for (i = 0; i < n; i++) + { + puts(board[i]); + } + } + else if (temp == 1) + { + for (i = 0; i < n; i++) + { + puts(board2[i]); + } + } + else if (temp == 2) + { + for (i = 0; i < n; i++) + { + puts(board3[i]); + } + } + else + { + for (i = 0; i < n; i++) + { + puts(board4[i]); + } + } + printf("\n"); + } + return 0; +} diff --git a/746.cpp b/746.cpp new file mode 100644 index 0000000..e6dada6 --- /dev/null +++ b/746.cpp @@ -0,0 +1,155 @@ +#include + +using namespace std; + +typedef struct +{ + int x, y; +} point; +typedef point polygon[20010]; + +polygon p[2]; +int n, m, casenum = 0; + +void readin(int num, int i) +{ + int j; + for (j = 0; j < num; j++) + { + cin >> p[i][j].x >> p[i][j].y; + } + for (j = num; j < num + num; j++) + { + p[i][j] = p[i][j - num]; + } +} + +void init() +{ + memset(p, 0, sizeof(p)); + readin(n, 0); + readin(m, 1); +} + +void out(point a, point b) +{ + if (a.x == b.x) + { + printf("VERTICAL\n"); + } + else if (a.y == b.y) + { + printf("0.000\n"); + } + else + { + double d = (a.y - b.y + 0.00) / (a.x - b.x + 0.00); + if (fabs(d) < 1e-3) + { + d = 0; + } + printf("%.3f\n", d); + } +} + +int cross(point a, point b, point c, point d) +{ + double x1, y1, x2, y2; + x1 = b.x - a.x; + y1 = b.y - a.y; + x2 = d.x - c.x; + y2 = d.y - c.y; + return x1 * y2 - x2 * y1; +} + +int stdz(double d) +{ + if (fabs(d) < 1e-4) + { + return 0; + } + if (d > 0) + { + return 1; + } + else + { + return -1; + } +} + +double dot(point a, point b, point c) +{ + double x1, y1, x2, y2; + x1 = b.x - a.x; + x2 = c.x - a.x; + y1 = b.y - a.y; + y2 = c.y - a.y; + return x1 * x2 + y1 * y2; +} + +void getslope(point pp, int &tp1, int &tp2) +{ + int j, dir, newdir; + int mini, maxi; + double k; + dir = stdz(cross(pp, p[0][0], pp, p[0][1])); + mini = maxi = 0; + for (j = 1; j < n; j++) + { + dir = stdz(cross(pp, p[0][mini], pp, p[0][j])); + if (dir > 0 || dir == 0 && dot(p[0][mini], pp, p[0][j]) < 0) + { + mini = j; + } + dir = stdz(cross(pp, p[0][maxi], pp, p[0][j])); + if (dir < 0 || dir == 0 && dot(p[0][maxi], pp, p[0][j]) < 0) + { + maxi = j; + } + } + tp1 = mini; + tp2 = maxi; +} + +void process() +{ + int i, j, k; + double k1, k2, tk1, tk2; + int p1, p2, tp1, tp2; + int besti = -1, bestj = -1; + for (i = 0; i < m; i++) + { + getslope(p[1][i], tp1, tp2); + if (besti < 0 || stdz(cross(p[1][i], p[0][tp1], p[1][besti], p[0][p1])) < 0) + { + besti = i; + p1 = tp1; + } + if (bestj < 0 || stdz(cross(p[1][i], p[0][tp2], p[1][bestj], p[0][p2])) > 0) + { + bestj = i; + p2 = tp2; + } + } + out(p[1][besti], p[0][p1]); + for (i = p1; i <= ((p1 < p2) ? p2 : p2 + n); i++) + { + printf("%d %d\n", p[0][i].x, p[0][i].y); + } + out(p[1][bestj], p[0][p2]); +} + +int main() +{ + while (cin >> n, n) + { + if (casenum++) + ; + printf("Instance %d:\n", casenum); + cin >> m; + init(); + process(); + } + return 0; +} diff --git a/748.cpp b/748.cpp new file mode 100644 index 0000000..abcbcac --- /dev/null +++ b/748.cpp @@ -0,0 +1,158 @@ +#include + +using namespace std; + +char mid[5000], in[5000]; + +int main() +{ + char a[5000], R[10], inter[3]; + int i, j, k, c = 0, d = 0, pp, cary, end; + int m, sum, cary_1, l, x, y, h, len; + int fact, t, point, q, z; + while (scanf("%s %d", R, &fact) != EOF) + { + k = 0; + if (fact == 0) + { + printf("1\n"); + continue; + } + if (fact == 1) + { + printf("%s\n", R); + continue; + } + if (atof(R) == 1) + { + printf("1\n"); + continue; + } + if (atof(R) == 0) + { + printf("0\n"); + continue; + } + for (i = 0; R[i]; i++) + if (R[i] == '.') + { + break; + } + point = strlen(R) - 1 - i; + for (i = 0; R[i]; i++) + if (R[i] != '.') + { + R[k++] = R[i]; + } + R[k] = NULL; + for (i = 0; i < 1000; i++) + { + mid[i] = in[i] = a[i] = 0; + } + len = strlen(R) - 1; + a[0] = 1; + x = 1; + k = 0; + for (i = len; i >= 0; i--) + { + inter[0] = R[i]; + inter[1] = NULL; + a[k] = atoi(inter); + k++; + } + x = k; + z = point; + for (q = 1; q < fact; q++) + { + d = 0; + c = 0; + z += point; + for (i = strlen(R) - 1; i >= 0; i--) + { + inter[0] = R[i]; + inter[1] = NULL; + h = atoi(inter); + k = c; + cary = 0; + for (j = 0; j < x; j++) + { + pp = h * a[j] + cary; + cary = pp / 10; + in[k++] = pp % 10; + } + if (cary) + { + in[k++] = cary; + } + if (k > d) + { + end = k; + } + else + { + end = d; + } + d = 0; + cary_1 = 0; + for (m = 0; m < end; m++) + { + sum = in[m] + mid[m] + cary_1; + mid[d++] = sum % 10; + cary_1 = sum / 10; + } + if (cary_1) + { + mid[d++] = cary_1; + } + for (l = 0; l < k; l++) + { + in[l] = 0; + } + c++; + } + for (j = d - 1; j >= 0; j--) + { + a[j] = mid[j]; + } + for (j = 0; j < d; j++) + { + mid[j] = 0; + } + x = d; + } + if (point == -1) + { + for (i = x - 1; i >= 0; i--) + { + printf("%d", a[i]); + } + printf("\n"); + continue; + } + z = x - z + 1; + z = x - z; + c = 0; + for (i = x - 1; i > z && !a[i]; i--) + ; + if (i > z) + { + for (i = i; i > z; i--) + { + printf("%d", a[i]); + } + } + for (i = 0; i <= z && !a[i]; i++) + ; + c = i; + if (i <= z) + { + printf("."); + for (i = z; i >= c; i--) + { + printf("%d", a[i]); + } + } + printf("\n"); + } + return 0; +} diff --git a/750.cpp b/750.cpp new file mode 100644 index 0000000..f0cee9f --- /dev/null +++ b/750.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +#define pi acos(-1.0) +#define N 1000000 + +char sol[100][10]; +char temp[10]; +bool row[10]; +bool rightDiag[20]; +bool leftDiag[20]; +int length; + +void generateSolution(int n) +{ + if (n == 8) + { + temp[8] = 0; + strcpy(sol[length], temp); + length++; + return; + } + for (int i = 0; i < 8; i++)// row + { + if (row[i] && rightDiag[n - i + 8] && leftDiag[n + i]) + { + row[i] = rightDiag[n - i + 8] = leftDiag[n + i] = false; + temp[n] = i + '1'; + generateSolution(n + 1); + row[i] = rightDiag[n - i + 8] = leftDiag[n + i] = true; + } + } +} + +int main() +{ + memset(row, true, sizeof(row)); + memset(rightDiag, true, sizeof(rightDiag)); + memset(leftDiag, true, sizeof(leftDiag)); + length = 0; + generateSolution(0); + int testCase; + scanf("%d", &testCase); + bool blank = false; + while (testCase--) + { + int r, c; + scanf("%d %d", &r, &c); + int cases = 0; + if (blank) + { + cout << endl; + } + blank = true; + cout << "SOLN COLUMN" << endl; + cout << " # 1 2 3 4 5 6 7 8\n" + << endl; + for (int i = 0; i < length; i++) + { + if (sol[i][c - 1] == r + '0') + { + printf("%2d ", ++cases); + printf("%c", sol[i][0]); + for (int j = 1; j < 8; j++) + { + printf(" %c", sol[i][j]); + } + printf("\n"); + } + } + } + return 0; +} diff --git a/751.cpp b/751.cpp new file mode 100644 index 0000000..3437766 --- /dev/null +++ b/751.cpp @@ -0,0 +1,116 @@ +#include + +using namespace std; + +int dp[1 << 18][11]; +char used[1 << 18][11] = {}; +int trans[11][11], tri[10]; + +int countTri(int state) +{ + int ret = 0; + for (int i = 0; i < 9; i++) + { + if ((state & tri[i]) == tri[i]) + { + ret++; + } + } + return ret; +} + +int dfs(int state, int score) +{ + if (used[state][score]) + { + return dp[state][score]; + } + used[state][score] = 1; + + int ret = 0, nstate, had = 9 - score, c, t; + for (int i = 0; i < 18; i++) + { + if ((state >> i) & 1) + { + nstate = state ^ (1 << i); + c = countTri(((1 << 18) - 1) ^ nstate); + if (c > had) + { + t = c - had + dfs(nstate, score - (c - had)); + ret = max(ret, t); + } + else + { + t = score - dfs(nstate, score); + ret = max(ret, t); + } + } + } + dp[state][score] = ret; + return ret; +} + +int main() +{ + trans[1][2] = 0, trans[1][3] = 1; + trans[2][3] = 2; + trans[2][4] = 3, trans[2][5] = 4, trans[3][5] = 5, trans[3][6] = 6; + trans[4][5] = 7, trans[5][6] = 8; + trans[4][7] = 9, trans[4][8] = 10, trans[5][8] = 11, trans[5][9] = 12, trans[6][9] = 13, trans[6][10] = 14; + trans[7][8] = 15, trans[8][9] = 16, trans[9][10] = 17; + tri[0] = (1 << 0) | (1 << 1) | (1 << 2); + tri[1] = (1 << 3) | (1 << 4) | (1 << 7); + tri[2] = (1 << 2) | (1 << 4) | (1 << 5); + tri[3] = (1 << 5) | (1 << 6) | (1 << 8); + tri[4] = (1 << 9) | (1 << 10) | (1 << 15); + tri[5] = (1 << 7) | (1 << 10) | (1 << 11); + tri[6] = (1 << 11) | (1 << 12) | (1 << 16); + tri[7] = (1 << 8) | (1 << 12) | (1 << 13); + tri[8] = (1 << 13) | (1 << 14) | (1 << 17); + int testcase, cases = 0; + int n, x, y; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + int state = 0, A = 0, B = 0, turn = 0; + for (int i = 0; i < n; i++) + { + scanf("%d %d", &x, &y); + if (x > y) + { + swap(x, y); + } + int c = countTri(state | (1 << trans[x][y])) - countTri(state); + if (c) + { + if (turn == 0) + { + A += c; + } + else + { + B += c; + } + } + else + { + turn = !turn; + } + state |= 1 << trans[x][y]; + } + state = ((1 << 18) - 1) ^ state; + int had = A + B; + int mx = dfs(state, 9 - had); + if (turn == 0) + { + A += mx, B += 9 - had - mx; + } + else + { + B += mx, A += 9 - had - mx; + } + printf("Game %d: %s wins.\n", ++cases, A > B ? "A" : "B"); + } + return 0; +} diff --git a/752.cpp b/752.cpp new file mode 100644 index 0000000..d6d88b5 --- /dev/null +++ b/752.cpp @@ -0,0 +1,176 @@ +#include + +using namespace std; + +const int MAX = 16; +const int MAXNODES = 256 + 64 + 16 + 4 + 1; +struct node +{ + int val; + int kids[4]; + int minkid; +}; +void swap(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} +void processTree(node t[], node map[], int i, int size) +{ + if (4 * i + 1 < size) + { + processTree(t, map, 4 * i + 1, size); + processTree(t, map, 4 * i + 2, size); + processTree(t, map, 4 * i + 3, size); + processTree(t, map, 4 * i + 4, size); + int m[4] = {0, 1, 2, 3}; + if (t[t[i].kids[m[0]]].minkid > t[t[i].kids[m[1]]].minkid) + { + swap(m[0], m[1]); + } + if (t[t[i].kids[m[1]]].minkid > t[t[i].kids[m[2]]].minkid) + { + swap(m[1], m[2]); + } + if (t[t[i].kids[m[2]]].minkid > t[t[i].kids[m[3]]].minkid) + { + swap(m[2], m[3]); + } + if (t[t[i].kids[m[0]]].minkid > t[t[i].kids[m[1]]].minkid) + { + swap(m[0], m[1]); + } + if (t[t[i].kids[m[1]]].minkid > t[t[i].kids[m[2]]].minkid) + { + swap(m[1], m[2]); + } + if (t[t[i].kids[m[0]]].minkid > t[t[i].kids[m[1]]].minkid) + { + swap(m[0], m[1]); + } + for (int j = 0; j < 4; j++) + { + map[i].kids[j] = m[j]; + } + t[i].minkid = t[t[i].kids[m[0]]].minkid; + } + else + { + map[i].kids[0] = -1; + map[i].kids[1] = -1; + map[i].kids[2] = -1; + map[i].kids[3] = -1; + t[i].minkid = t[i].val; + } +} +void buildImage(node image[], node mapping[], int grid[][MAX], int size, + int i, int row, int col) +{ + if (image[i].val != -1) + { + for (int r = row; r < row + size; r++) + for (int c = col; c < col + size; c++) + { + grid[r][c] = image[i].val; + } + } + else + { + size /= 2; + buildImage(image, mapping, grid, size, 4 * i + mapping[i].kids[0] + 1, + row, col); + buildImage(image, mapping, grid, size, 4 * i + mapping[i].kids[1] + 1, + row, col + size); + buildImage(image, mapping, grid, size, 4 * i + mapping[i].kids[2] + 1, + row + size, col); + buildImage(image, mapping, grid, size, 4 * i + mapping[i].kids[3] + 1, + row + size, col + size); + } +} +int main() +{ + int cases, size, nkids; + node tree[MAXNODES], image[MAXNODES]; + int grid[MAX][MAX]; + cin >> cases; + for (int c = 1; c <= cases; c++) + { + int s; + cin >> s; + size = 0; + for (int i = 1; i <= s; i *= 2) + { + size += i * i; + } + for (int i = 0; i < size; i++) + { + tree[i].val = image[i].val = -1; + } + cin >> nkids; + for (int i = 0; i < nkids; i++) + { + int k, intensity; + cin >> k >> intensity; + tree[k].val = intensity; + } + for (int k = 0; k < size; k++) + { + if (tree[k].val == -1) + { + tree[k].kids[0] = 4 * k + 1; + tree[k].kids[1] = 4 * k + 2; + tree[k].kids[2] = 4 * k + 3; + tree[k].kids[3] = 4 * k + 4; + } + else + { + tree[k].kids[0] = -1; + tree[k].kids[1] = -1; + tree[k].kids[2] = -1; + tree[k].kids[3] = -1; + } + } + cin >> nkids; + for (int i = 0; i < nkids; i++) + { + int k, intensity; + cin >> k >> intensity; + image[k].val = intensity; + } + for (int k = 0; k < size; k++) + { + if (image[k].val == -1) + { + image[k].kids[0] = 4 * k + 1; + image[k].kids[1] = 4 * k + 2; + image[k].kids[2] = 4 * k + 3; + image[k].kids[3] = 4 * k + 4; + } + else + { + image[k].kids[0] = -1; + image[k].kids[1] = -1; + image[k].kids[2] = -1; + image[k].kids[3] = -1; + } + } + node mapping[MAXNODES]; + processTree(tree, mapping, 0, size); + buildImage(image, mapping, grid, s, 0, 0, 0); + cout << "Case " << c << endl + << endl; + for (int r = 0; r < s; r++) + { + for (int c = 0; c < s; c++) + { + cout << setw(4) << grid[r][c]; + } + cout << endl; + } + if (c < cases) + { + cout << endl; + } + } +} diff --git a/753.cpp b/753.cpp new file mode 100644 index 0000000..34464e6 --- /dev/null +++ b/753.cpp @@ -0,0 +1,203 @@ +#include + +using namespace std; + +#define maxn 520 + +vector Relation; +map Map; +list Adj[maxn], Alter[maxn], Adapt[maxn]; +char Color[maxn]; +int Cost[maxn][maxn], Parent[maxn], Q[maxn * 3]; +int Plag, PlgLim, TPlag, Dev, Match, Des; + +void Assign(int u, int v) +{ + int n; + list::iterator p; + if (u != v) + Alter[u].push_back(v); + Color[v] = 1; + for (p = Adapt[v].begin(); p != Adapt[v].end(); p++) + { + n = *p; + if (Color[n]) + continue; + Assign(u, n); + } +} +void MakeAlter() +{ + int i, j; + for (i = 101; i <= PlgLim; i++) + { + Assign(i, i); + for (j = 101; j <= TPlag; j++) + Color[j] = 0; + } +} +void MakeAdj() +{ + int i, u, v, n, lim; + string st; + list::iterator p; + lim = Plag + 100; + for (i = 0; i < Relation.size(); i++) + { + st = Relation[i]; + n = Map[Relation[i]]; + if (n <= lim) + { + Adj[i + 1].push_back(n); + Adj[n].push_back(i + 1); + Cost[i + 1][n] = 1; + } + for (p = Alter[n].begin(); p != Alter[n].end(); p++) + { + v = *p; + if (v > lim) + continue; + Adj[i + 1].push_back(v); + Adj[v].push_back(i + 1); + Cost[i + 1][v] = 1; + } + } +} +void Mark(int v) +{ + int u; + u = Parent[v]; + if (u < 0) + return; + Cost[u][v]--; + Cost[v][u]++; + Mark(Parent[v]); +} +int BFS(int n) +{ + int i, u, v; + int ind = 0; + char ch[maxn]; + list::iterator p; + for (i = 1; i <= Dev; i++) + ch[i] = 0; + for (i = 101; i <= PlgLim; i++) + ch[i] = 0; + ch[Des] = 0; + Parent[n] = -1; + Q[ind++] = n; + for (i = 0; i < ind; i++) + { + u = Q[i]; + for (p = Adj[u].begin(); p != Adj[u].end(); p++) + { + v = *p; + if (ch[v] || Cost[u][v] == 0) + continue; + Parent[v] = u; + if (v == Des) + { + Match--; + Mark(v); + return 1; + } + ch[v] = 1; + Q[ind++] = v; + } + } + return 0; +} +void Cal() +{ + int i; + MakeAlter(); + MakeAdj(); + Match = Dev; + Des = 400; + for (i = 101; i <= PlgLim; i++) + { + Adj[i].push_back(Des); + Cost[i][Des] = 1; + } + for (i = 1; i <= Dev && Match; i++) + BFS(i); + cout << Match << endl; +} +void ReadCase() +{ + int i, n, d, u, v; + string str, plg; + cin >> Plag; + d = 100 + Plag + 1; + for (i = 101; i < d; i++) + { + cin >> str; + Map[str] = i; + } + cin >> Dev; + n = Dev; + while (n--) + { + cin >> str >> plg; + Relation.push_back(plg); + u = Map[plg]; + if (!u) + Map[plg] = d++; + } + PlgLim = d - 1; + cin >> n; + while (n--) + { + cin >> str >> plg; + u = Map[str]; + v = Map[plg]; + if (!u) + { + u = d; + Map[str] = d++; + } + if (!v) + { + v = d; + Map[plg] = d++; + } + Adapt[u].push_back(v); + } + TPlag = d - 1; +} +void Free() +{ + int i, j; + for (i = 1; i <= Dev; i++) + { + for (j = 101; j <= PlgLim; j++) + Cost[i][j] = Cost[j][i] = 0; + Adj[i].clear(); + Cost[0][i] = Cost[i][0] = 0; + } + Adj[0].clear(); + for (i = 101; i <= TPlag; i++) + { + Alter[i].clear(); + Adapt[i].clear(); + } + Map.clear(); + Relation.clear(); + for (i = 0; i < maxn; i++) + Color[i] = 0; +} + +int main() +{ + int ks; + cin >> ks; + while (ks--) + { + ReadCase(); + Cal(); + Free(); + if (ks) + cout << endl; + } + return 0; +} diff --git a/754.cpp b/754.cpp new file mode 100644 index 0000000..2469a44 --- /dev/null +++ b/754.cpp @@ -0,0 +1,210 @@ +#include + +using namespace std; + +#define MAX 1000 + +struct point +{ + double x, y; +}; +struct line +{ + point a, b; +}; +int n, dnum = 1, multi, i; +point beg; +line lines[MAX]; +point door[MAX]; + +void readin(point &p) +{ + cin >> p.x >> p.y; +} + +void init() +{ + int i; + cin >> n; + for (i = 0; i < n; i++) + { + readin(lines[i].a); + readin(lines[i].b); + } + lines[n].a.x = 0; + lines[n].a.y = 100; + lines[n].b.x = 100; + lines[n].b.y = 100; + n++; + lines[n].a.x = 100; + lines[n].a.y = 100; + lines[n].b.x = 100; + lines[n].b.y = 0; + n++; + lines[n].a.x = 100; + lines[n].a.y = 0; + lines[n].b.x = 0; + lines[n].b.y = 0; + n++; + lines[n].a.x = 0; + lines[n].a.y = 0; + lines[n].b.x = 0; + lines[n].b.y = 100; + n++; + readin(beg); +} + +int equ(double a, double b) +{ + if (fabs(a - b) < 1e-6) + { + return 1; + } + else + { + return 0; + } +} + +double cra(double x1, double y1, double x2, double y2) +{ + return x1 * y2 - x2 * y1; +} + +double cr(point a, point b, point c) +{ + return cra(b.x - a.x, b.y - a.y, c.x - a.x, c.y - a.y); +} + +int cross(line a, line b) +{ + double i, j, k, l; + i = cr(a.a, b.a, b.b); + j = cr(a.b, b.a, b.b); + k = cr(b.a, a.a, a.b); + l = cr(b.b, a.a, a.b); + if (equ(i * j, 0) || equ(k * l, 0)) + { + return 1; + } + else if (i * j < 0 && k * l < 0) + { + return 2; + } + return 0; +} + +void makeco(double &a, double &b, double &c, line l) +{ + double x1 = l.a.x, y1 = l.a.y, x2 = l.b.x, y2 = l.b.y; + c = (x1 * y2 - x2 * y1); + a = (y2 - y1); + b = (x2 - x1); +} + +void getcross(point &q, line a, line b) +{ + double a1, b1, c1, a2, b2, c2; + makeco(a1, b1, c1, a); + makeco(a2, b2, c2, b); + q.x = -(b1 * c2 - b2 * c1) / (b2 * a1 - b1 * a2); + q.y = -(a1 * c2 - a2 * c1) / (b2 * a1 - b1 * a2); +} + +void getmid(point &p, point a, point b) +{ + p.x = (a.x + b.x) / 2; + p.y = (a.y + b.y) / 2; +} + +void makecross() +{ + int i, j, k, ii; + point list[MAX], temp; + memset(list, 0, sizeof(list)); + memset(door, 0, sizeof(door)); + dnum = 1; + for (i = 0; i < n; i++) + { + for (k = 0, j = 0; j < n; j++) + if (i != j) + { + if (cross(lines[i], lines[j]) > 0) + { + getcross(list[k++], lines[i], lines[j]); + } + } + for (ii = 0; ii < k - 1; ii++) + for (j = ii + 1; j < k; j++) + if (list[ii].y > list[j].y || + equ(list[ii].y, list[j].y) && list[ii].x > list[j].x) + { + temp = list[ii]; + list[ii] = list[j]; + list[j] = temp; + } + for (ii = 0; ii < k - 1; ii++) + { + getmid(door[dnum++], list[ii], list[ii + 1]); + } + } +} + +int good(point a, point b) +{ + line l; + int i; + l.a = a; + l.b = b; + for (i = 0; i < n; i++) + if (cross(l, lines[i]) == 2) + { + return 0; + } + return 1; +} + +void dijkstra() +{ + int i, j, k; + int set[MAX]; + int dis[MAX]; + memset(set, 0, sizeof(set)); + memset(dis, 0, sizeof(dis)); + dis[0] = 1; + door[0] = beg; + for (i = 1;; i++) + { + for (j = 0; j < dnum; j++) + if (dis[j] == i && !set[j]) + { + set[j] = 1; + for (k = 0; k < dnum; k++) + if (!set[k] && good(door[j], door[k]) && (i + 1 < dis[k] || !dis[k])) + { + dis[k] = i + 1; + if (equ(door[k].x, 0) || equ(door[k].y, 0) || equ(door[k].x, 100) || equ(door[k].y, 100)) + { + cout << "Number of doors = " << i << endl; + return; + } + } + } + } +} + +int main() +{ + cin >> multi; + for (i = 0; i < multi; i++) + { + init(); + makecross(); + dijkstra(); + if (i < multi - 1) + { + printf("\n"); + } + } + return 0; +} diff --git a/755.cpp b/755.cpp index 8851758..da0d667 100644 --- a/755.cpp +++ b/755.cpp @@ -1,89 +1,127 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#include -string SI[] = {"2", "2", "2", "3", "3", "3", "4", "4", "4", "5", "5", "5", "6", "6", "6", "7", "", "7", "7", "8", "8", "8", "9", "9", "9", ""}; +using namespace std; -string mod(string S){ - int i = 0, L = S.length(); - string R = ""; - FOI(i, 0, L-1){ - if (isdigit(S[i])) - R += S[i]; - else if (isalpha(S[i])) - R += SI[S[i] - 'A']; - else +/*************** +Title : 787-3279 + No : 755 +***************/ +#define max 100009 +char a[400]; +char digit[max][10]; +char tele_num[max][50]; +int N; +int sort_function(const void *a, const void *b) +{ + return (strcmp((char *)a, (char *)b)); +} +int value() +{ + int p; + char i, m = '2'; + char j = 2, k = 0; + for (i = 'A'; i < 'Z'; i++) + { + if (i == 'Q') + { continue; + } + k++; + if (k % 4 == 0) + { + m++; + k = 1; + } + p = i; + a[p] = m; } - return R; + return 0; } - -string con(string S){ - S.insert(3, "-"); - return S; +int convert() +{ + int i, j, k, m; + for (i = 0; i < N; i++) + { + k = 0; + for (j = 0; tele_num[i][j]; j++) + { + if (isdigit(tele_num[i][j])) + { + digit[i][k] = tele_num[i][j]; + } + else if (isalpha(tele_num[i][j])) + { + m = tele_num[i][j]; + digit[i][k] = a[m]; + } + else + { + continue; + } + k++; + } + digit[i][k] = NULL; + } + return 0; } - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int T; - cin >> T; - while (T--){ - int N, i; - cin >> N; - map Map; - map::iterator it; - while (N--){ - string str; - cin >> str; - str = mod(str); - it = Map.find(str); - if (it != Map.end()) - Map[str] += 1; - else - Map[str] = 1; +int calculate() +{ + char temp[10]; + int i, j, c, k; + k = 0; + for (i = 0; i < N;) + { + strcpy(temp, digit[i]); + c = 1; + for (++i; !strcmp(temp, digit[i]) && i < N; i++) + { + c++; } - vector< string > V; - for (it = Map.begin(); it != Map.end(); it++){ - if ((*it).second > 1) - V.push_back((*it).first); + if (c >= 2) + { + k = 1; + for (j = 0; temp[j]; j++) + { + printf("%c", temp[j]); + if (j == 2) + { + printf("-"); + } + } + printf(" %d\n", c); + c = 1; + } + } + if (!k) + { + printf("No duplicates.\n"); + } + return 0; +} +int main() +{ + int i, pp; + char f[120], qq[120]; + value(); + gets(qq); + sscanf(qq, "%d", &pp); + gets(qq); + while (pp--) + { + gets(f); + sscanf(f, "%d", &N); + for (i = 0; i < N; i++) + { + gets(tele_num[i]); + } + convert(); + qsort((void *)digit, N, sizeof(digit[0]), sort_function); + calculate(); + if (pp) + { + printf("\n"); + gets(f); } - if (V.size() == 0) - cout << "No duplicates." << endl; - else{ - sort(V.begin(), V.end()); - FOI(i, 0, V.size()-1) - cout << con(V[i]) << " " << Map[V[i]] << endl; - } - if (T) - cout << endl; } return 0; } - diff --git a/756.cpp b/756.cpp new file mode 100644 index 0000000..d220861 --- /dev/null +++ b/756.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int P, E, I, d, case_num = 1; + while (cin >> P >> E >> I >> d) + { + if (P == -1) + { + break; + } + cout << "Case " << case_num++; + while ((P - E) % 28 != 0) + { + P += 23; + } + while ((P - I) % 33 != 0) + { + P += 23 * 28; + } + while (P > d) + { + P -= 23 * 28 * 33; + } + while (P <= d) + { + P += 23 * 28 * 33; + } + cout << ": the next triple peak occurs in "; + cout << (P - d) << " days." << endl; + } + return 0; +} diff --git a/757.cpp b/757.cpp new file mode 100644 index 0000000..a873976 --- /dev/null +++ b/757.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define MAXD 30 + +int N, H, d[MAXD], f[MAXD], t[MAXD], now[MAXD], plan[MAXD], use[MAXD]; +long long int max_; + +void init() +{ + int i, j, k; + scanf("%d", &H); + H *= 12; + for (i = 0; i < N; i++) + { + scanf("%d", &f[i]); + } + for (i = 0; i < N; i++) + { + scanf("%d", &d[i]); + } + t[0] = 0; + for (i = 1; i < N; i++) + { + scanf("%d", &t[i]); + t[i] += t[i - 1]; + } +} +int check() +{ + int i; + for (i = 0; i < N; i++) + { + if (use[i] > plan[i]) + { + return 1; + } + else if (use[i] < plan[i]) + { + return 0; + } + } + return 0; +} +void change(long long int ans) +{ + int i; + if (ans > max_ || (ans == max_ && check())) + { + max_ = ans; + for (i = 0; i < N; i++) + { + plan[i] = use[i]; + } + } +} +void solve() +{ + int i, j, k, h, num; + long long int ans; + max_ = -1; + for (i = 0; i < N && t[i] <= H; i++) + { + h = H - t[i]; + ans = 0; + for (j = 0; j <= i; j++) + { + now[j] = f[j]; + } + memset(use, 0, sizeof(use)); + while (h) + { + num = -1; + for (j = 0; j <= i; j++) + if (now[j] > num) + { + num = now[j]; + k = j; + } + ++use[k]; + --h; + ans += num; + now[k] -= d[k]; + if (now[k] < 0) + { + now[k] = 0; + } + } + change(ans); + } + printf("%d", plan[0] * 5); + for (i = 1; i < N; i++) + { + printf(", %d", plan[i] * 5); + } + printf("\n"); + printf("Number of fish expected: %lld\n", max_); +} + +int main() +{ + int tt = 0; + for (;;) + { + scanf("%d", &N); + if (!N) + { + break; + } + init(); + if (tt++) + { + printf("\n"); + } + solve(); + } + return 0; +} diff --git a/758.cpp b/758.cpp new file mode 100644 index 0000000..69daf0d --- /dev/null +++ b/758.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +int wayx[4] = {0, 1, 0, -1}; +int wayy[4] = {1, 0, -1, 0}; + +char map_[15][20]; +int cnum, width, used[15][20], height[20]; + +int valid(int x, int y) +{ + return y >= 0 && y < width && x >= 0 && x < height[y]; +} + +void counts(int x, int y, char c) +{ + int i, tx, ty; + cnum++; + used[x][y] = 1; + for (i = 0; i < 4; i++) + { + tx = x + wayx[i]; + ty = y + wayy[i]; + if (valid(tx, ty) && !used[tx][ty] && map_[tx][ty] == c) + { + used[tx][ty] = 1; + counts(tx, ty, c); + } + } +} + +void floodfill(int x, int y, char c) +{ + int i, tx, ty; + map_[x][y] = 0; + for (i = 0; i < 4; i++) + { + tx = x + wayx[i]; + ty = y + wayy[i]; + if (valid(tx, ty) && map_[tx][ty] == c) + { + floodfill(tx, ty, c); + } + } +} + +void adjust() +{ + int i, j, k; + for (j = 0; j < 15; j++) + { + if (height[j]) + { + for (i = 0, k = 0; i < height[j]; i++) + { + if (map_[i][j]) + { + map_[k++][j] = map_[i][j]; + } + } + height[j] = k; + } + else + { + break; + } + } + for (j = 0, k = 0; j < width; j++) + { + if (height[j]) + { + for (i = 0; i < height[j]; i++) + { + map_[i][k] = map_[i][j]; + } + height[k++] = height[j]; + } + } + width = k; + /*for(; k<15; k++) + { + height[k] = 0; + }*/ +} + +int main() +{ + int cas, count, i, j, k, l, tx, ty, max, remain, score, stemp; + cas = 0; + scanf("%d", &count); + while (count--) + { + for (i = 9; i >= 0; i--) + { + scanf("%s", map_[i]); + } + for (i = 0; i < 15; i++) + { + height[i] = 10; + } + width = 15; + printf("Game %d:\n\n", ++cas); + for (k = 1, remain = 150, score = 0;; k++) + { + memset(used, 0, sizeof(used)); + /*printf("%d\n", width); + system("pause");*/ + for (j = 0, max = 0; j < width; j++) + { + for (i = 0; i < height[j]; i++) + { + if (!used[i][j]) + { + cnum = 0; + counts(i, j, map_[i][j]); + if (cnum > max) + { + max = cnum; + tx = i; + ty = j; + } + } + } + } + if (max <= 1) + { + break; + } + stemp = max - 2; + stemp *= stemp; + score += stemp; + printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n", k, tx + 1, ty + 1, max, map_[tx][ty], stemp); + floodfill(tx, ty, map_[tx][ty]); + /*for(i=9; i>=0; i--) + { + for(j=0; j=0; i--) + { + for(j=0; j + +using namespace std; + +char R[10000]; +int main() +{ + int i, j, n; + while (gets(R)) + { + n = 0; + for (i = j = 0; R[i] == 'M' && j < 3; i++) + { + n += 1000; + } + if (R[i] == 'C') + { + i++; + if (R[i] == 'D') + { + n += 400, i++; + } + else if (R[i] == 'M') + { + n += 900, i++; + } + else + { + for (i--, j = 0; R[i] == 'C' && j < 3; i++, j++) + { + n += 100; + } + } + } + else if (R[i] == 'D') + { + n += 500; + for (i++, j = 0; R[i] == 'C' && j < 3; i++, j++) + { + n += 100; + } + } + if (R[i] == 'X') + { + i++; + if (R[i] == 'L') + { + n += 40, i++; + } + else if (R[i] == 'C') + { + n += 90, i++; + } + else + { + for (i--, j = 0; R[i] == 'X' && j < 3; i++, j++) + { + n += 10; + } + } + } + else if (R[i] == 'L') + { + n += 50; + for (i++, j = 0; R[i] == 'X' && j < 3; i++, j++) + { + n += 10; + } + } + if (R[i] == 'I') + { + i++; + if (R[i] == 'V') + { + n += 4, i++; + } + else if (R[i] == 'X') + { + n += 9, i++; + } + else + { + for (i--, j = 0; R[i] == 'I' && j < 3; i++, j++) + { + n += 1; + } + } + } + else if (R[i] == 'V') + { + n += 5; + for (i++, j = 0; R[i] == 'I' && j < 3; i++, j++) + { + n += 1; + } + } + if (R[i] != '\0' || n >= 4000) + { + printf("This is not a valid number\n"); + } + else + { + printf("%d\n", n); + } + } + return 0; +} diff --git a/760.cpp b/760.cpp new file mode 100644 index 0000000..2fe35a4 --- /dev/null +++ b/760.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define MAX 10050 + +int RA[MAX], tempRA[MAX], SA[MAX], tempSA[MAX]; +int C[MAX], Phi[MAX], PLCP[MAX], LCP[MAX]; + +inline void suffix_sort(int n, int k) +{ + memset(C, 0, sizeof C); + for (int i = 0; i < n; i++) + { + C[i + k < n ? RA[i + k] : 0]++; + } + int sum = 0; + for (int i = 0; i < max(256, n); i++) + { + int t = C[i]; + C[i] = sum; + sum += t; + } + for (int i = 0; i < n; i++) + { + tempSA[C[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i]; + } + memcpy(SA, tempSA, n * sizeof(int)); +} + +inline void suffix_array(string &s) +{ + int n = s.size(); + for (int i = 0; i < n; i++) + { + RA[i] = s[i] - 1; + } + for (int i = 0; i < n; i++) + { + SA[i] = i; + } + for (int k = 1; k < n; k *= 2) + { + suffix_sort(n, k); + suffix_sort(n, 0); + int r = tempRA[SA[0]] = 0; + for (int i = 1; i < n; i++) + { + int s1 = SA[i], s2 = SA[i - 1]; + bool equal = true; + equal &= RA[s1] == RA[s2]; + equal &= RA[s1 + k] == RA[s2 + k]; + tempRA[SA[i]] = equal ? r : ++r; + } + memcpy(RA, tempRA, n * sizeof(int)); + } +} + +inline void lcp(string &s) +{ + int n = s.size(); + Phi[SA[0]] = -1; + for (int i = 1; i < n; i++) + { + Phi[SA[i]] = SA[i - 1]; + } + int L = 0; + for (int i = 0; i < n; i++) + { + if (Phi[i] == -1) + { + PLCP[i] = 0; + continue; + } + while (s[i + L] == s[Phi[i] + L]) + { + L++; + } + PLCP[i] = L; + L = max(L - 1, 0); + } + for (int i = 1; i < n; i++) + { + LCP[i] = PLCP[SA[i]]; + } +} + +int main() +{ + string a, b, s; + int tt = 0; + while (getline(cin, a) && getline(cin, b)) + { + if (tt++) + { + cout << endl; + } + getline(cin, s); + s = a + "\1" + b + "\2"; + suffix_array(s); + lcp(s); + int maxx = 0; + for (int i = 1; i < s.size(); i++) + { + bool left = SA[i - 1] + LCP[i] <= a.size(); + bool right = SA[i] + LCP[i] <= a.size(); + if (LCP[i] && (left ^ right)) + { + maxx = max(maxx, LCP[i]); + } + } + if (maxx == 0) + { + cout << "No common sequence." << endl; + continue; + } + string last = "some invalid string"; + for (int i = 1; i < s.size(); i++) + { + bool left = SA[i - 1] + LCP[i] <= a.size(); + bool right = SA[i] + LCP[i] <= a.size(); + string sub = s.substr(SA[i], maxx); + if (LCP[i] == maxx && (left ^ right) && last != sub) + { + cout << sub << endl; + last = sub; + } + } + } + return 0; +} diff --git a/762.cpp b/762.cpp index d56aee1..524e9a5 100644 --- a/762.cpp +++ b/762.cpp @@ -1,84 +1,183 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +#define MAXN 100000 +#define MAX 5000 -struct Node{ - string str; - int cost; - vector< pair > vec; -}; +char C_1[MAXN][4], C_2[MAXN][4], St[MAXN + MAXN][4], Link[MAX][MAX]; +char B[5], E[5]; +int N, K, I, Q[MAXN], QH, QT; +struct Vertex +{ + int pr; + char cl; +} V[MAX]; -map< string, list > adj; -map< string, bool > used; +int com(const void *a, const void *b) +{ + return strcmp((char *)b, (char *)a); +} -int bfs(string src, string des){ - queue< Node > q; - Node node; - node.str = src; - node.cost = 0; - q.push( node ); - while( !q.empty() ){ - n = q.front(); - q.pop(); - - if( n.str == des ) - return n; - if( used[n.str] ) - continue; - - for (lit = adj[n.str].begin(); lit != adj[n.str].end(); lit++){ - Node nod; - nod.str = - - used[p.first] = true; +// binary search +int B_search(char a[]) +{ + int lo = 0, up = I - 1; + int mid = (lo + up) / 2; + while (lo <= up && strcmp(St[mid], a)) + { + if (strcmp(a, St[mid]) < 0) + { + lo = mid + 1; + } + else if (strcmp(a, St[mid]) > 0) + { + up = mid - 1; + } + mid = (lo + up) / 2; + } + if (strcmp(a, St[mid])) + { + return -1; + } + return mid; +} +int isEmpty() +{ + return QH == QT; +} +int pop() +{ + int x = Q[QH++]; + QH %= MAXN; + return x; +} +void push(int x) +{ + Q[QT++] = x; + QT %= MAXN; +} +int BFS(int s, int t) +{ + int i; + for (i = 0; i < I; i++) + { + V[i].cl = 'W'; + } + V[s].cl = 'G'; + QH = QT = 0; + push(s); + while (!isEmpty()) + { + int u = pop(); + for (int v = 0; v < I; v++) + { + if (V[v].cl == 'W' && Link[u][v] == 1) + { + V[v].cl = 'G'; + V[v].pr = u; + if (v == t) + { + return 1; + } + push(v); + } + } + } + return 0; +} +void Set() +{ + int i, q, p; + I = 0; + qsort(St, K, sizeof(St[0]), com); + for (i = 1; i < K; i++) + if (!strcmp(St[i - 1], St[i])) + { + strcpy(St[i - 1], "99"); + } + qsort(St, K, sizeof(St[0]), com); + for (i = 0; i < K; i++) + { + if (strcmp(St[i], "99")) + { + I++; + } + else + { + break; + } + } + for (i = 0; i < N; i++) + { + p = B_search(C_1[i]); + q = B_search(C_2[i]); + if (p < 0 || q < 0) + { + printf("Erro\n"); + exit(0); + } + Link[p][q] = Link[q][p] = 1; + } +} +void PrintPath(int s, int m, int t) +{ + if (s == t) + { + printf("%s %s\n", St[t], St[m]); + return; + } + PrintPath(s, t, V[t].pr); + if (s != m) + { + printf("%s %s\n", St[t], St[m]); + } +} +void CaL() +{ + int i, j, p, q; + p = B_search(B); + q = B_search(E); + if (p == -1 || q == -1) + { + printf("No route\n"); + return; + } + j = BFS(p, q); + if (j == 0) + { + printf("No route\n"); + return; + } + else + { + PrintPath(p, p, q); } - return -1; } -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - bool multi = false; - int N; - while( cin >> N ){ - string src, des; - FOI(i, 1, N){ - cin >> src >> des; - adj[src].push_back(des); - adj[des].push_back(src); +int main() +{ + int i, j = 0; + while (scanf("%d", &N) == 1) + { + K = 0; + if (j) + { + putchar('\n'); + } + for (i = 0; i < N; i++) + { + scanf("%s%s", C_1[i], C_2[i]); + strcpy(St[K++], C_1[i]); + strcpy(St[K++], C_2[i]); } - cin >> src >> des; - int cost = bfs(src, des); - if( cost < 0 ) - cout << "No route" << endl; - else{ - + scanf("%s%s", B, E); + Set(); + CaL(); + j++; + for (i = 0; i < MAX; i++) + { + memset(Link[i], 0, sizeof(char) * MAX); } } return 0; diff --git a/763.cpp b/763.cpp new file mode 100644 index 0000000..e20cd70 --- /dev/null +++ b/763.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int N[128]; +char S[128]; +int main() +{ + int first = 1, i, j, k, p; + while (scanf("%s", S) == 1) + { + memset(N, 0, sizeof(N)); + for (i = 0, j = strlen(S) - 1; j >= 0; i++, j--) + { + N[i] = S[j] - '0'; + } + scanf("%s", S); + for (i = 0, j = strlen(S) - 1; j >= 0; i++, j--) + { + N[i] += S[j] - '0'; + } + for (p = 0; p < 128;) + { + if (p < 0) + { + p = 0; + } + switch (N[p]) + { + case 0: + p++; + break; + case 1: + if (N[p + 1]) + { + N[p]--, N[p + 1]--, N[p + 2]++; + } + p++; + break; + default: + if (p == 0) + { + N[0] -= 2, N[1]++; + } + else if (p == 1) + { + N[0]++, N[1] -= 2, N[2]++, p = 0; + } + else + { + N[p + 1]++, N[p] -= 2, N[p - 2]++, p -= 3; + } + break; + } + } + for (p = 127; p >= 0; p--) + { + if (N[p]) + { + break; + } + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + if (p >= 0) + { + for (i = p; i >= 0; i--) + { + printf("%d", N[i]); + } + printf("\n"); + } + else + { + printf("0\n"); + } + } + return 0; +} diff --git a/766.cpp b/766.cpp new file mode 100644 index 0000000..413fe53 --- /dev/null +++ b/766.cpp @@ -0,0 +1,1322 @@ +#include + +using namespace std; + +struct BigInt +{ + int alloc, size; + unsigned *data, _data[5]; + + // mem mgmt + void reserve(int n) + { + if (data == NULL) + { + data = _data; + alloc = sizeof(_data) / sizeof(_data[0]); + } + if (alloc < n) + { + while (alloc < n) + { + alloc <<= 1; + } + if (data == _data) + { + data = (unsigned *)malloc(alloc * sizeof(unsigned)); + assert(data != NULL); + memcpy(data, _data, sizeof(_data)); + } + else + { + data = (unsigned *)realloc(data, alloc * sizeof(unsigned)); + assert(data != NULL); + } + } + } + + ~BigInt() + { + if (data != _data) + { + free(data); + } + } + + void swap(BigInt &y) + { + if (data != _data && y.data != y._data) + { + std::swap(size, y.size); + std::swap(alloc, y.alloc); + std::swap(data, y.data); + } + else + { + BigInt t(*this); + assign(y); + y.assign(t); + } + } + + typedef unsigned long long uint64; + static inline int sgn(int n) + { + return n == 0 ? 0 : (n < 0 ? -1 : 1); + } + + // Removes leading zeroes + void normalize() + { + int n = abs(size); + while (n > 0 && data[n - 1] == 0) + { + n--; + } + size = (size < 0 ? -n : n); + } + + static int absCmp(const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn != yn) + { + return sgn(xn - yn); + } + for (int i = xn - 1; i >= 0; i--) + if (x.data[i] != y.data[i]) + { + return x.data[i] > y.data[i] ? +1 : -1; + } + return 0; + } + + // z = abs(x) + abs(y); + static void absAdd(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size); + if (xn < yn) + { + absAdd(z, y, x); + return; + } + int zn = max(xn, yn); + z.reserve(zn + 1); + uint64 c = 0; + for (int i = 0; i < yn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i] + (uint64)y.data[i]) & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 0 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[zn++] = (unsigned)c; + } + z.size = zn; + } + + // z = abs(x) + abs(y) + static void absAdd1(BigInt &z, const BigInt &x, unsigned y) + { + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = y; + if (&z == &x) + { + for (int i = 0; c != 0 && i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + else + { + for (int i = 0; i < n; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += x.data[i]) & 0xFFFFFFFFU); + } + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = n; + } + + // z = abs(x) - abs(y) + static void absSub(BigInt &z, const BigInt &x, const BigInt &y) + { + int t = absCmp(x, y); + if (t <= 0) + { + if (t == 0) + { + z.size = 0; + } + else + { + absSub(z, y, x); + } + z.size = -z.size; + return; + } + int xn = abs(x.size), yn = abs(y.size); + z.reserve(max(xn, yn)); + uint64 c = 1; + for (int i = 0; i < yn; i++, c >>= 32) + { + c += (uint64)x.data[i] + ((uint64)y.data[i] ^ 0xFFFFFFFFULL); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (&z == &x) + { + for (int i = yn; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + else + { + for (int i = yn; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)((c += (uint64)x.data[i] + 0xFFFFFFFFULL) & 0xFFFFFFFFU); + } + } + assert(c == 1); + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + assert(z.size > 0); + } + + // z = abs(x) - abs(y) + static void absSub1(BigInt &z, const BigInt &x, unsigned y) + { + if (y == 0) + { + z.assign(x); + z.size = abs(z.size); + return; + } + if (x.size == 0) + { + z.size = -1; + z.data[0] = y; + return; + } + int xn = abs(x.size); + if (xn == 1) + { + if (x.data[0] > y) + { + z.size = 1; + z.data[0] = x.data[0] - y; + } + else if (x.data[0] == y) + { + z.size = 0; + } + else + { + z.size = -1; + z.data[0] = y - x.data[0]; + } + return; + } + z.reserve(xn); + uint64 c = 1 + (uint64)x.data[0] + (y ^ 0xFFFFFFFFULL); + z.data[0] = (unsigned)c; + c >>= 32; + if (&z == &x) + { + for (int i = 1; c != 1 && i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + else + { + for (int i = 1; i < xn; i++, c >>= 32) + { + z.data[i] = (unsigned)(c += (uint64)x.data[i] + 0xFFFFFFFFULL); + } + } + z.size = xn; + while (z.size > 0 && z.data[z.size - 1] == 0) + { + z.size--; + } + } + + // z = abs(x) * m + a + static void absMulAdd1(BigInt &z, const BigInt &x, unsigned m, unsigned a) + { + int n = abs(x.size); + z.reserve(n + 2); + uint64 c = a; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)m); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + while (c != 0) + { + z.data[n++] = (unsigned)c; + c >>= 32; + } + z.size = n; + } + + // z = x + sign*y. Asserts: abs(sign) = 1 + static void add(BigInt &z, const BigInt &x, int sign, const BigInt &y) + { + int xs = sgn(x.size), ys = sign * sgn(y.size); + if (xs == 0) + { + z.assign(y); + z.size *= sign; + } + else if (ys == 0) + { + z.assign(x); + } + else if (xs == ys) + { + absAdd(z, x, y); + z.size *= xs; + } + else if (ys < 0) + { + absSub(z, x, y); + } + else + { + absSub(z, x, y); + z.size = -z.size; + } + } + + static void add1s(BigInt &z, const BigInt &x, int y) + { + int xs = (x.size >= 0 ? +1 : -1), ys = (y >= 0 ? +1 : -1); + if (xs == ys) + { + absAdd1(z, x, abs(y)); + z.size *= xs; + } + else if (ys < 0) + { + absSub1(z, x, -y); + } + else + { + absSub1(z, x, y); + z.size = -z.size; + } + } + + static void mul1s(BigInt &z, const BigInt &x, int y) + { + if (y < 0) + { + mul1s(z, x, -y); + z.size = -z.size; + return; + } + if (y == 0) + { + z.size = 0; + return; + } + if (y == 1) + { + z.assign(x); + return; + } + int n = abs(x.size); + z.reserve(n + 1); + uint64 c = 0; + for (int i = 0; i < n; i++, c >>= 32) + { + c = (c + (uint64)x.data[i] * (unsigned)y); + z.data[i] = (unsigned)(c & 0xFFFFFFFFU); + } + if (c != 0) + { + z.data[n++] = (unsigned)c; + } + z.size = (x.size < 0 ? -n : n); + } + + static void mulQuadratic(BigInt &z, const BigInt &x, const BigInt &y) + { + if (&z == &x || &z == &y) + { + BigInt t; + mulQuadratic(t, x, y); + z = t; + return; + } + int xn = abs(x.size), yn = abs(y.size), zn = xn + yn + 1; + z.reserve(zn); + for (int i = 0; i < zn; i++) + { + z.data[i] = 0; + } + for (int i = 0; i < xn; i++) + { + uint64 c = 0; + int k = i; + for (int j = 0; j < yn; j++, k++, c >>= 32) + { + c += z.data[k] + x.data[i] * (uint64)y.data[j]; + z.data[k] = (unsigned)(c & 0xFFFFFFFFU); + } + for (; c != 0; k++, c >>= 32) + { + z.data[k] = (unsigned)((c += z.data[k]) & 0xFFFFFFFFU); + } + } + z.size = zn * sgn(x.size) * sgn(y.size); + z.normalize(); + } + + static void mulKaratsuba(BigInt &z, const BigInt &x, const BigInt &y) + { + int xn = abs(x.size), yn = abs(y.size), zs = sgn(x.size) * sgn(y.size); + int w = max(xn, yn) >> 1; + BigInt A(x.data + w, max(0, xn - w)), B(x.data, min(xn, w)); + BigInt C(y.data + w, max(0, yn - w)), D(y.data, min(yn, w)); + BigInt R, T; + absAdd(z, A, B); + absAdd(T, C, D); + mul(R, z, T); + mul(z, A, C); + mul(T, B, D); + R -= z; + R -= T; + R <<= w * 32; + z <<= w * 64; + z += R; + z += T; + z.size *= zs; + } + + BigInt(unsigned a[], int n) + { + if (n < 0) + { + n = 0; + } + while (n > 0 && a[n - 1] == 0) + { + n--; + } + data = NULL; + reserve(n); + size = n; + memcpy(data, a, n * sizeof(unsigned)); + } + + // q = abs(x) div abs(y); Returns abs(x) mod abs(y) + static unsigned absDiv1(BigInt &q, const BigInt &x, unsigned y) + { + assert(y != 0); + int n = abs(x.size); + q.reserve(n); + uint64 c = 0; + for (int i = n - 1; i >= 0; i--) + { + c = (c << 32) | (uint64)x.data[i]; + q.data[i] = (unsigned)(c / y); + c %= y; + } + q.size = n; + q.normalize(); + return (unsigned)c; + } + + // u = abs(u) mod abs(v), q = abs(u) div abs(v) + static void absDiv(BigInt *q, BigInt &u, BigInt v) + { + if (q != NULL && q == &u) + { + BigInt t; + absDiv(&t, u, v); + *q = t; + return; + } + u.size = abs(u.size); + v.size = abs(v.size); + assert(v.size > 0); + if (u.size < v.size) + { + if (q) + { + *q = 0; + } + return; + } + if (v.size == 1) + { + unsigned t = absDiv1(q == NULL ? u : *q, u, v.data[0]); + u.data[0] = t; + u.size = (t == 0 ? 0 : 1); + return; + } + int n = v.size, d = 0; + while (((v.data[n - 1] << d) & 0x80000000U) == 0) + { + d++; + } + u <<= d; + v <<= d; + unsigned vh = v.data[n - 1], vh2 = v.data[n - 2]; + uint64 c, g; + u.reserve(u.size + 1); + u.data[u.size] = 0; + v.reserve(v.size + 1); + v.data[v.size] = 0; + int qp = u.size - v.size + 1; + if (q) + { + q->reserve(qp + 1); + q->size = qp; + } + for (int up = u.size - 1; --qp >= 0; up--) + { + c = (((uint64)u.data[up + 1]) << 32) | (uint64)u.data[up]; + g = c / (uint64)vh; + if (g > 0xFFFFFFFFULL) + { + g = 0xFFFFFFFFULL; + } + while ((c - g * vh) < 0x100000000ULL && + (((c - g * vh) << 32) + u.data[up - 1]) < (g * (uint64)vh2)) + { + g--; + } + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++) + { + c += g * (uint64)v.data[j]; + if (u.data[i] >= (unsigned)(c & 0xFFFFFFFFULL)) + { + u.data[i] -= (unsigned)(c & 0xFFFFFFFFULL); + c >>= 32; + } + else + { + u.data[i] += (unsigned)(0x100000000ULL - (c & 0xFFFFFFFFULL)); + c = (c >> 32) + 1; + } + } + if (c != 0) + { + g--; + assert(c == 1); + c = 0; + for (int i = qp, j = 0; j <= n; i++, j++, c >>= 32) + { + c += (uint64)u.data[i] + (uint64)v.data[j]; + u.data[i] = (unsigned)c; + } + assert(c == 1); + } + if (q) + { + q->data[qp] = (unsigned)g; + } + } + u >>= d; + v >>= d; + if (q) + { + q->normalize(); + } + } + + static int cmp(const BigInt &x, const BigInt &y) + { + if (x.size != y.size) + { + return sgn(x.size - y.size); + } + return absCmp(x, y) * (x.size < 0 ? -1 : 1); + } + + static int cmp1s(const BigInt &x, int y) + { + if (y == 0) + { + return sgn(x.size); + } + if (y > 0) + { + if (x.size <= 0) + { + return -1; + } + if (x.size > 1) + { + return +1; + } + if (x.data[0] == (unsigned)y) + { + return 0; + } + return x.data[0] > (unsigned)y ? +1 : -1; + } + else + { + if (x.size >= 0) + { + return +1; + } + if (x.size < -1) + { + return -1; + } + if (x.data[0] == (unsigned)(-y)) + { + return 0; + } + return x.data[0] > (unsigned)(-y) ? -1 : +1; + } + } + + static void add(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, +1, y); + } + + static void sub(BigInt &z, const BigInt &x, const BigInt &y) + { + add(z, x, -1, y); + } + + static void mul(BigInt &z, const BigInt &x, const BigInt &y) + { + if (max(abs(x.size), abs(y.size)) < 64) + { + mulQuadratic(z, x, y); + } + else + { + mulKaratsuba(z, x, y); + } + } + + static void mod(BigInt &r, const BigInt &u, const BigInt &v) + { + r = u; + absDiv(NULL, r, v); + } + + static void div(BigInt &q, BigInt &r, const BigInt &u, const BigInt &v) + { + int us = sgn(u.size), vs = sgn(v.size); + r = u; + absDiv(&q, r, v); + //TODO + if (us * vs < 0) + { + q.size = -q.size; + } + } + + void assign(int n) + { + reserve(1); + size = sgn(n); + data[0] = abs(n); + } + + void assign(long long n) + { + reserve(2); + if (n < 0) + { + size = -2; + n = -n; + } + else + { + size = 2; + } + data[0] = (unsigned)(n & 0xFFFFFFFFU); + data[1] = (unsigned)(n >> 32); + normalize(); + } + + void assign(const BigInt &b) + { + if (this == &b) + { + return; + } + int n = abs(b.size); + reserve(n); + size = b.size; + memcpy(data, b.data, n * sizeof(unsigned)); + } + + void assign(const char *s, int radix = 10) + { + assert(2 <= radix && radix <= 36); + while (isspace(*s)) + { + s++; + } + int sign = 1; + if (*s == '+') + { + s++; + } + else if (*s == '-') + { + s++; + sign = -1; + } + while (*s == '0') + { + s++; + } + int n = 0; + for (n = 0; s[n] && isalnum(s[n]); n++) + ; + size = 0; + if (n > 20) + { + reserve((int)(log((double)radix) / log(2.0) / 32.0 * n) + 2); + } + else + { + reserve(n / 9 + 2); + } + unsigned a = 0, m = 1; + for (int i = 0; i < n; i++) + { + int dig = (isdigit(s[i]) ? (s[i] - '0') : (toupper(s[i]) - 'A' + 10)); + assert(dig < radix); + a = a * radix + dig; + m *= radix; + if (m > 0x3000000) + { + absMulAdd1(*this, *this, m, a); + a = 0; + m = 1; + } + } + if (m > 1) + { + absMulAdd1(*this, *this, m, a); + } + size *= sign; + } + + BigInt(int n = 0) + { + data = NULL; + assign(n); + } + BigInt(int n, int cap) + { + data = NULL; + reserve(cap); + assign(n); + } + BigInt(long long n) + { + data = NULL; + assign(n); + } + BigInt(const BigInt &b) + { + data = NULL; + assign(b); + } + BigInt(const char *s, int radix = 10) + { + data = NULL; + assign(s, radix); + } + BigInt(const string &s, int radix = 10) + { + data = NULL; + assign(s.c_str(), radix); + } + + BigInt &operator=(int n) + { + assign(n); + return *this; + } + BigInt &operator=(long long n) + { + assign(n); + return *this; + } + BigInt &operator=(const BigInt &b) + { + assign(b); + return *this; + } + BigInt &operator=(const char *s) + { + assign(s); + return *this; + } + BigInt &operator=(const string &s) + { + assign(s.c_str()); + return *this; + } + BigInt &operator+=(const BigInt &y) + { + add(*this, *this, +1, y); + return *this; + } + BigInt &operator-=(const BigInt &y) + { + add(*this, *this, -1, y); + return *this; + } + BigInt &operator*=(const BigInt &y) + { + mul(*this, *this, y); + return *this; + } + BigInt &operator/=(const BigInt &y) + { + BigInt q, r; + div(q, r, *this, y); + assign(q); + return *this; + } + BigInt &operator%=(const BigInt &y) + { + mod(*this, *this, y); + return *this; + } + BigInt &operator+=(int y) + { + add1s(*this, *this, y); + return *this; + } + BigInt &operator-=(int y) + { + add1s(*this, *this, -y); + return *this; + } + BigInt &operator*=(int y) + { + mul1s(*this, *this, y); + return *this; + } + BigInt &operator<<=(int n) + { + shl(n); + return *this; + } + BigInt &operator>>=(int n) + { + shr(n); + return *this; + } + void operator++() + { + add1s(*this, *this, 1); + } + void operator--() + { + add1s(*this, *this, -1); + } + BigInt operator-() const + { + BigInt z = *this; + z.negate(); + return z; + } + BigInt operator>>(int n) const + { + BigInt r = *this; + r.shr(n); + return r; + } + BigInt operator<<(int n) const + { + BigInt r = *this; + r.shl(n); + return r; + } + + string str(int radix = 10) const + { + assert(2 <= radix && radix <= 36); + if (size == 0) + { + return "0"; + } + int y = 1, m = 0; + while (y < 0x300000) + { + y *= radix; + m++; + } + BigInt x(*this); + x.size = abs(x.size); + char *tmp = (char *)malloc(x.size * (radix >= 10 ? 10 : 32) + 10); + int n = 0; + while (x.size != 0) + { + unsigned r = absDiv1(x, x, y); + for (int i = 0; i < m; i++, r /= radix) + { + tmp[n++] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[r % radix]; + } + } + while (n > 0 && tmp[n - 1] == '0') + { + n--; + } + if (size < 0) + { + tmp[n++] = '-'; + } + reverse(tmp, tmp + n); + tmp[n] = 0; + string res = string(tmp); + free(tmp); + return res; + } + + string toString(int r = 10) const + { + return str(r); + } + int toInt() const + { + return sgn(size) * (int)data[0]; + } + long long toLL() const + { + long long r = 0; + for (int i = 0; i < 2 && i < abs(size); i++) + { + r = (r << 32) | data[i]; + } + return size < 0 ? -r : r; + } + + void setBit(int n) + { + int s = abs(size), m = (n >> 5) + 1; + reserve(m); + while (s < m) + { + data[s++] = 0; + } + size = (size < 0 ? -s : s); + data[n >> 5] |= 1U << (n & 31); + } + + void clrBit(int n) + { + if (abs(size) > (n >> 5)) + { + data[n >> 5] &= ~(1U << (n & 31)); + normalize(); + } + } + int getBit(int n) const + { + return (abs(size) > (n >> 5)) ? ((data[n >> 5] >> (n & 31)) & 1) : 0; + } + + // Returns 1+(index of highest non-zero bit) + int bitSize() const + { + if (size == 0) + { + return 0; + } + int n = (abs(size) - 1) * 32; + unsigned t = data[abs(size) - 1]; + if (t >= 65536) + { + n += 16; + t >>= 16; + } + if (t >= 256) + { + n += 8; + t >>= 8; + } + if (t >= 16) + { + n += 4; + t >>= 4; + } + if (t >= 4) + { + n += 2; + t >>= 2; + } + if (t >= 2) + { + n++; + t >>= 1; + } + if (t >= 1) + { + n++; + } + return n; + } + + void shl(int s) + { + if (size == 0) + { + return; + } + if (s < 0) + { + shr(-s); + } + int n = abs(size), w = s >> 5; + reserve(n + w + 1); + if (w > 0) + { + for (int i = n - 1; i >= 0; i--) + { + data[i + w] = data[i]; + } + for (int i = w - 1; i >= 0; i--) + { + data[i] = 0; + } + n += w; + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + data[n++] = 0; + for (int i = 0; i < n; i++) + { + b = data[i] >> (32 - s); + data[i] = (data[i] << s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void shr(int s) + { + if (s < 0) + { + shl(-s); + } + int n = abs(size), w = s >> 5; + if (w > 0) + { + for (int i = 0; i + w < n; i++) + { + data[i] = data[i + w]; + } + n -= w; + if (n < 0) + { + n = 0; + } + } + s &= 31; + if (s > 0) + { + unsigned a = 0, b; + for (int i = n - 1; i >= 0; i--) + { + b = data[i] << (32 - s); + data[i] = (data[i] >> s) | a; + a = b; + } + } + size = (size < 0 ? -n : n); + normalize(); + } + + void negate() + { + size = -size; + } + int sign() const + { + return sgn(size); + } + int compareTo(const BigInt &y) const + { + return cmp(*this, y); + } + int compareTo(int y) const + { + return cmp1s(*this, y); + } +}; + +BigInt operator+(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::add(z, x, y); + return z; +} +BigInt operator-(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::sub(z, x, y); + return z; +} +BigInt operator*(const BigInt &x, const BigInt &y) +{ + BigInt z; + BigInt::mul(z, x, y); + return z; +} +BigInt operator/(const BigInt &x, const BigInt &y) +{ + BigInt q, r; + BigInt::div(q, r, x, y); + return q; +} +BigInt operator%(const BigInt &x, const BigInt &y) +{ + BigInt r; + BigInt::mod(r, x, y); + return r; +} +bool operator==(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) == 0; +} +bool operator==(const BigInt &x, int y) +{ + return BigInt::cmp1s(x, y) == 0; +} +bool operator!=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) != 0; +} +bool operator<(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) < 0; +} +bool operator<=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) <= 0; +} +bool operator>(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) > 0; +} +bool operator>=(const BigInt &x, const BigInt &y) +{ + return BigInt::cmp(x, y) >= 0; +} +ostream &operator<<(ostream &o, const BigInt &x) +{ + return o << x.str(); +} +istream &operator>>(istream &i, BigInt &x) +{ + string s; + i >> s; + x = s; + return i; +} +namespace std +{ +template <> +inline void swap(BigInt &a, BigInt &b) +{ + a.swap(b); +} +} + +struct Frac +{ + BigInt num, den; + + inline void reduce() + { + if (den < 0) + { + num = -num; + den = -den; + } + BigInt g = __gcd(num < 0 ? -num : num, den); + num /= g; + den /= g; + } + + Frac(int n = 0, int d = 1) : num(n), den(d) + { + reduce(); + } + Frac(const BigInt &x, const BigInt &y) : num(x), den(y) + { + reduce(); + } + + Frac operator+(const Frac &b) const + { + return Frac(num * b.den + den * b.num, den * b.den); + } + Frac operator-(const Frac &b) const + { + return Frac(num * b.den - den * b.num, den * b.den); + } + Frac operator*(const Frac &b) const + { + return Frac(num * b.num, den * b.den); + } + Frac operator/(const Frac &b) const + { + return Frac(num * b.den, den * b.num); + } + Frac operator-() const + { + return Frac(-num, den); + } + + bool isZero() const + { + return num == 0; + } +}; + +template +T lcm(const T &a, const T &b) +{ + return a / __gcd(a, b) * b; +} + +Frac mat[32][32]; +int N; + +void gauss() +{ + for (int i = 0; i < N; i++) + { + if (mat[i][i].isZero()) + { + int k; + for (k = i + 1; k < N && mat[k][i].isZero(); k++) + ; + assert(0 <= k && k < N && !mat[k][i].isZero()); + for (int j = i; j <= N; j++) + { + swap(mat[k][j], mat[i][j]); + } + } + for (int j = i + 1; j <= N; j++) + { + mat[i][j] = mat[i][j] / mat[i][i]; + } + mat[i][i] = 1; + for (int k = 0; k < N; k++) + { + if (i == k || mat[k][i].isZero()) + { + continue; + } + for (int j = i + 1; j <= N; j++) + { + mat[k][j] = mat[k][j] - mat[k][i] * mat[i][j]; + } + mat[k][i] = 0; + } + } +} + +int main() +{ + int C[32][32]; + memset(C, 0, sizeof(C)); + for (int n = 0; n < 32; n++) + { + C[n][0] = 1; + for (int k = 1; k <= n; k++) + { + C[n][k] = C[n - 1][k - 1] + C[n - 1][k]; + } + } + int T; + scanf("%d", &T); + char *answer[50]; + memset(answer, 0, sizeof(answer)); + for (int cs = 1; cs <= T; cs++) + { + if (cs > 1) + { + printf("\n"); + } + int K; + scanf("%d", &K); + if (answer[K] != NULL) + { + printf("%s\n", answer[K]); + continue; + } + for (int i = 0; i < 32; i++) + for (int j = 0; j < 32; j++) + { + mat[i][j] = 0; + } + for (int col = 0; col <= K; col++) + { + int p = K + 1 - col; + for (int i = 0; i < p; i++) + { + mat[i][col] = mat[i][col] + C[p][i]; + } + } + for (int i = 0; i <= K; i++) + { + mat[i][K + 1] = C[K][i]; + } + N = K + 1; + gauss(); + BigInt M = 1; + for (int i = 0; i < N; i++) + { + M = lcm(M, mat[i][N].den); + } + string s = M.str(); + for (int i = 0; i < N; i++) + { + BigInt t = mat[i][N].num * M / mat[i][N].den; + s += " " + t.str(); + } + s += " 0"; + answer[K] = new char[1000]; + strcpy(answer[K], s.c_str()); + printf("%s\n", answer[K]); + } +} diff --git a/769.cpp b/769.cpp new file mode 100644 index 0000000..27fcf2d --- /dev/null +++ b/769.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +int main() +{ + int i, j, k, n, t; + for (scanf("%d", &t); t-- > 0 && scanf("%d", &n) == 1;) + { + for (i = 0, k = 2 * n + 1; i < n; i++, k += 2) + { + printf("%-5d", k); + for (j = 0; j <= i; j++) + { + printf(" %d", n * (n - i) + (n - 1) * j); + } + printf("\n"); + } + for (i = n - 1; i > 1; i--, k += 2) + { + printf("%-5d", k); + for (j = 0; j < i; j++) + { + printf(" %d", i + (n - 1) * j); + } + printf("\n"); + } + if (t != 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/775.cpp b/775.cpp new file mode 100644 index 0000000..124f0ed --- /dev/null +++ b/775.cpp @@ -0,0 +1,151 @@ +#include + +using namespace std; + +// Hamiltonian Cycle +// Please refer the description of uva 775 +const int maxn = 1000; +int n, len; +int path[maxn], t[maxn]; +bool g[maxn][maxn], v[maxn]; +void FindPath() +{ + len = 1; + path[0] = 0; + v[0] = true; + bool find; + do + { + find = false; + for (int i = 0; i < n && !find; i++) + if (g[path[len - 1]][i] && !v[i]) + { + path[len++] = i; + v[i] = true; + find = true; + } + } while (find); +} +bool CycleToPath() +{ + for (int i = 0; i < n; i++) + if (!v[i]) + { + for (int j = 0; j < len; j++) + { + if (g[i][path[j]]) + { + t[0] = i; + int p = 1; + for (int l = j; l >= 0; l--) + { + t[p++] = path[l]; + } + for (int l = len - 1; l > j; l--) + { + t[p++] = path[l]; + } + memcpy(path, t, sizeof(t)); + v[i] = true; + len++; + return true; + } + } + } + return false; +} +void print() +{ + for (int i = 0; i < n; i++) + { + printf("%d ", path[i] + 1); + } + printf("%d\n", path[0] + 1); +} +bool PathToCycle() +{ + for (int i = 2; i < len; i++) + { + if (g[path[0]][path[i]] && g[path[len - 1]][path[i - 1]]) + { + memset(t, 0, sizeof(t)); + for (int l = 0; l < i; l++) + { + t[l] = path[l]; + } + for (int l = len - 1, p = i; l >= i; l--) + { + t[p++] = path[l]; + } + memcpy(path, t, sizeof(t)); + return true; + } + } + return false; +} +bool BuildHamiltonianCycle() +{ + FindPath(); + bool result; + while (true) + { + if (g[path[0]][path[len - 1]]) + { + if (len == n) + { + return true; + } + result = CycleToPath(); + if (!result) + { + return false; + } + } + else + { + result = PathToCycle(); + if (!result) + { + return false; + } + } + } +} +int main() +{ + char str[100]; + while (gets(str) != NULL) + { + sscanf(str, "%d", &n); + memset(g, false, sizeof(g)); + memset(v, false, sizeof(v)); + while (gets(str) != NULL) + { + if (strcmp(str, "%") == 0) + { + break; + } + int a, b; + sscanf(str, "%d %d", &a, &b); + a--; + b--; + if (a == b) + { + continue; + } + if (g[a][b]) + { + continue; + } + g[a][b] = g[b][a] = true; + } + if (!BuildHamiltonianCycle()) + { + puts("N"); + } + else + { + print(); + } + } +} diff --git a/776.cpp b/776.cpp index 30724e5..ade819d 100644 --- a/776.cpp +++ b/776.cpp @@ -1,123 +1,177 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; +#include -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +using namespace std; -vector< vector< string > > Vec; -int Mat[101][101]; -int M = 0, N = 0; +int row = 0, col = 0; +int cols[1000]; +char input[1000][1000]; +int forest[1000][1000]; -int count(int Val) { - int C = 0; - while (Val > 0) { - C++; - Val /= 10; - } - return C; +int num_length(int n) +{ + return int(log10(n)) + 1; } -int dI[] = {-1, -1, -1, 0, 1, 1, 1, 0}; -int dJ[] = {-1, 0, 1, 1, 1, 0, -1, -1}; -void bfs(int I, int J, int Max, string C) { - if (I < 0 || I >= M) return; - if (J < 0 || J >= N) return; - if (Mat[I][J] != -1) return; - if (Vec[I][J] != C) return; - - Mat[I][J] = Max; - int i, j; - FOI(i, 0, 7) - bfs(I + dI[i], J + dJ[i], Max, C); -} - -void print() { - int i, j; - int Max = 1; - - memset(Mat, -1, sizeof Mat); - - FOI(i, 0, M-1) { - FOI(j, 0, N-1) { - if (Mat[i][j] == -1) { - bfs(i, j, Max, Vec[i][j]); - ++Max; +void fill_forest() +{ + pair tmp; + queue> q; + int counter = 1; + bool visited[1000][1000] = {false}; + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + if (visited[i][j]) + { + continue; + } + tmp.first = i; + tmp.second = j; + q.push(tmp); + visited[i][j] = true; + forest[i][j] = counter; + if (num_length(counter) > cols[j]) + { + cols[j] = num_length(counter); + } + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && !visited[tmp.first - 1][tmp.second] && input[tmp.first - 1][tmp.second] == input[tmp.first][tmp.second]) + { + visited[tmp.first - 1][tmp.second] = true; + forest[tmp.first - 1][tmp.second] = counter; + q.push(make_pair(tmp.first - 1, tmp.second)); + if (num_length(counter) > cols[tmp.second]) + { + cols[tmp.second] = num_length(counter); + } + } + if (tmp.first + 1 < row && !visited[tmp.first + 1][tmp.second] && input[tmp.first + 1][tmp.second] == input[tmp.first][tmp.second]) + { + visited[tmp.first + 1][tmp.second] = true; + forest[tmp.first + 1][tmp.second] = counter; + q.push(make_pair(tmp.first + 1, tmp.second)); + if (num_length(counter) > cols[tmp.second]) + { + cols[tmp.second] = num_length(counter); + } + } + if (tmp.second > 0 && !visited[tmp.first][tmp.second - 1] && input[tmp.first][tmp.second - 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first][tmp.second - 1] = true; + forest[tmp.first][tmp.second - 1] = counter; + q.push(make_pair(tmp.first, tmp.second - 1)); + if (num_length(counter) > cols[tmp.second - 1]) + { + cols[tmp.second - 1] = num_length(counter); + } + } + if (tmp.second + 1 < col && !visited[tmp.first][tmp.second + 1] && input[tmp.first][tmp.second + 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first][tmp.second + 1] = true; + forest[tmp.first][tmp.second + 1] = counter; + q.push(make_pair(tmp.first, tmp.second + 1)); + if (num_length(counter) > cols[tmp.second + 1]) + { + cols[tmp.second + 1] = num_length(counter); + } + } + if (tmp.first > 0 && tmp.second > 0 && !visited[tmp.first - 1][tmp.second - 1] && input[tmp.first - 1][tmp.second - 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first - 1][tmp.second - 1] = true; + forest[tmp.first - 1][tmp.second - 1] = counter; + q.push(make_pair(tmp.first - 1, tmp.second - 1)); + if (num_length(counter) > cols[tmp.second - 1]) + { + cols[tmp.second - 1] = num_length(counter); + } + } + if (tmp.first + 1 < row && tmp.second + 1 < col && !visited[tmp.first + 1][tmp.second + 1] && input[tmp.first + 1][tmp.second + 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first + 1][tmp.second + 1] = true; + forest[tmp.first + 1][tmp.second + 1] = counter; + q.push(make_pair(tmp.first + 1, tmp.second + 1)); + if (num_length(counter) > cols[tmp.second + 1]) + { + cols[tmp.second + 1] = num_length(counter); + } + } + if (tmp.first > 0 && tmp.second + 1 < col && !visited[tmp.first - 1][tmp.second + 1] && input[tmp.first - 1][tmp.second + 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first - 1][tmp.second + 1] = true; + forest[tmp.first - 1][tmp.second + 1] = counter; + q.push(make_pair(tmp.first - 1, tmp.second + 1)); + if (num_length(counter) > cols[tmp.second + 1]) + { + cols[tmp.second + 1] = num_length(counter); + } + } + if (tmp.first + 1 < row && tmp.second > 0 && !visited[tmp.first + 1][tmp.second - 1] && input[tmp.first + 1][tmp.second - 1] == input[tmp.first][tmp.second]) + { + visited[tmp.first + 1][tmp.second - 1] = true; + forest[tmp.first + 1][tmp.second - 1] = counter; + q.push(make_pair(tmp.first + 1, tmp.second - 1)); + if (num_length(counter) > cols[tmp.second - 1]) + { + cols[tmp.second - 1] = num_length(counter); + } + } } + counter++; } } - - int Dig[N]; - memset(Dig, 0, sizeof Dig); - - FOI(j, 0, N-1) { - int D = 0; - FOI(i, 0, M-1) - D = max(D, count(Mat[i][j])); - Dig[j] = D; - } - - FOI(i, 0, M-1) { - FOI(j, 0, N-1) { - if (j > 0) - printf(" "); - cout << setw(Dig[j]) << Mat[i][j]; +} + +void output_forest() +{ + for (int i = 0; i < row; i++) + { + for (int j = 0; j < col; j++) + { + for (int k = 0; k < cols[j] - num_length(forest[i][j]) + (j == 0 ? 0 : 1); k++) + { + cout << " "; + } + cout << forest[i][j]; } cout << endl; } cout << "%" << endl; } - -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - string str; - - while (getline(cin, str)) { - if (str == "%") { - if (Vec.size() > 0) { - M = Vec.size(); - N = Vec[0].size(); - print(); +int main() +{ + char tmp; + int r, c; + while (true) + { + r = c = 0; + fill(cols, cols + 1000, 0); + while (cin.peek() != '%' && cin.peek() != -1) + { + cin >> tmp; + input[r][c++] = tmp; + if (cin.peek() == '\n') + { + cin.ignore(100, '\n'); + r++; + row = r; + col = c; + c = 0; } - Vec.clear(); - continue; } - string S; - vector< string > V; - stringstream ss(str); - while (ss >> S) { - V.push_back(S); + if (cin.peek() == '%') + { + cin >> tmp; + } + fill_forest(); + output_forest(); + if (cin.peek() == -1) + { + break; } - Vec.push_back(V); - } - if (Vec.size() > 0) { - M = Vec.size(); - N = Vec[0].size(); - print(); } return 0; } diff --git a/782.cpp b/782.cpp new file mode 100644 index 0000000..8df7b94 --- /dev/null +++ b/782.cpp @@ -0,0 +1,148 @@ +#include + +using namespace std; + +char grid[50][100]; +bool visited[50][100]; +vector line_sizes; + +void flood_fill(int x, int y) +{ + pair tmp; + queue> q; + tmp.first = x; + tmp.second = y; + visited[x][y] = true; + grid[x][y] = ' '; + q.push(tmp); + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && !visited[tmp.first - 1][tmp.second]) + { + if (grid[tmp.first - 1][tmp.second] != ' ') + { + grid[tmp.first][tmp.second] = '#'; + if (tmp.second + 1 > line_sizes[tmp.first]) + { + line_sizes[tmp.first] = tmp.second + 1; + } + } + else + { + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + } + } + if (tmp.second > 0 && !visited[tmp.first][tmp.second - 1]) + { + if (grid[tmp.first][tmp.second - 1] != ' ') + { + grid[tmp.first][tmp.second] = '#'; + if (tmp.second + 1 > line_sizes[tmp.first]) + { + line_sizes[tmp.first] = tmp.second + 1; + } + } + else + { + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + } + } + if (tmp.first < 49 && !visited[tmp.first + 1][tmp.second]) + { + if (grid[tmp.first + 1][tmp.second] != ' ') + { + grid[tmp.first][tmp.second] = '#'; + if (tmp.second + 1 > line_sizes[tmp.first]) + { + line_sizes[tmp.first] = tmp.second + 1; + } + } + else + { + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + } + } + if (tmp.second < 99 && !visited[tmp.first][tmp.second + 1]) + { + if (grid[tmp.first][tmp.second + 1] != ' ') + { + grid[tmp.first][tmp.second] = '#'; + if (tmp.second + 1 > line_sizes[tmp.first]) + { + line_sizes[tmp.first] = tmp.second + 1; + } + } + else + { + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + } + } + } +} + +int main() +{ + int n; + int lines; + pair star; + string input; + cin >> n; + cin.ignore(100, '\n'); + while (n--) + { + lines = 0; + line_sizes.clear(); + for (int i = 0; i < 50; i++) + { + for (int j = 0; j < 100; j++) + { + grid[i][j] = ' '; + visited[i][j] = false; + } + } + while (getline(cin, input)) + { + if (input.size() > 0 && input[0] == '_') + { + flood_fill(star.first, star.second); + for (int i = 0, sz = line_sizes.size(); i < sz; i++) + { + for (int j = 0; j < line_sizes[i]; j++) + { + cout << grid[i][j]; + } + cout << endl; + } + cout << input << endl; + break; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + grid[lines][i] = input[i]; + if (input[i] == '*') + { + star.first = lines; + star.second = i; + } + } + lines++; + int line_size = 0; + for (int i = input.size() - 1; i >= 0; i--) + { + if (input[i] != ' ' && input[i] != '*') + { + line_size = i + 1; + break; + } + } + line_sizes.push_back(line_size); + } + } + return 0; +} diff --git a/783.cpp b/783.cpp new file mode 100644 index 0000000..5dfc8c4 --- /dev/null +++ b/783.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +/* @JUDGE_ID: 2905EH 783 C++ "trivial" */ +//2002/8/6 a_q783.cpp + +int main() +{ + int d, v1, v2, t1, t2, tf; + while (scanf("%d %d %d %d %d %d", &d, &v1, &v2, &t1, &t2, &tf) == 6) + { + int counter = 0; + for (int s1 = 0; s1 <= tf; s1 += t1) + { + for (int s2 = 0; s2 <= tf; s2 += t2) + { + float aa = (float)s2 * v1 * v2 + (float)d * v1 - (float)s1 * v1 * v2; + float bb = (float)v2 + v1; + float x = aa / bb; + if (0 <= x && x <= d && s1 + x / v1 <= tf) + { + counter++; + } + } + } + printf("%d\n", counter); + } +} +/*@END_OF_SOURCE_CODE*/ diff --git a/784.cpp b/784.cpp new file mode 100644 index 0000000..dc6945b --- /dev/null +++ b/784.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +char maze[30][80]; +int x, y; + +void bfs() +{ + queue> q; + q.push(pair(x, y)); + while (!q.empty()) + { + int x1 = q.front().first, y1 = q.front().second; + q.pop(); + maze[x1][y1] = '#'; + if (x1 - 1 >= 0 && maze[x1 - 1][y1] == ' ') + { + maze[x1 - 1][y1] = '#'; + q.push(pair(x1 - 1, y1)); + } + if (x1 + 1 < 30 && maze[x1 + 1][y1] == ' ') + { + maze[x1 + 1][y1] = '#'; + q.push(pair(x1 + 1, y1)); + } + if (y1 - 1 >= 0 && maze[x1][y1 - 1] == ' ') + { + maze[x1][y1 - 1] = '#'; + q.push(pair(x1, y1 - 1)); + } + if (y1 + 1 < 80 && maze[x1][y1 + 1] == ' ') + { + maze[x1][y1 + 1] = '#'; + q.push(pair(x1, y1 + 1)); + } + } +} + +int main() +{ + string input; + int n, counter; + cin >> n; + getline(cin, input); + while (n--) + { + for (int i = 0; i < 30; i++) + for (int j = 0; j < 80; j++) + { + maze[i][j] = 'e'; + } + counter = 0; + while (getline(cin, input)) + { + if (input[0] == '_') + { + bfs(); + for (int i = 0; i < 30; i++) + { + for (int j = 0; j < 80; j++) + { + if (maze[i][j] == 'e') + { + break; + } + cout << maze[i][j]; + } + if (maze[i][0] == 'e') + { + break; + } + cout << endl; + } + cout << input << endl; + break; + } + else + { + for (int i = 0; i < input.size(); i++) + { + if (input[i] == '*') + { + x = counter; + y = i; + } + maze[counter][i] = input[i]; + } + } + counter++; + } + } + return 0; +} diff --git a/785.cpp b/785.cpp new file mode 100644 index 0000000..6412252 --- /dev/null +++ b/785.cpp @@ -0,0 +1,177 @@ +#include + +using namespace std; + +/* +785 +Grid Colouring +*/ +#define MAXN 3000 +char grid[35][85]; +char f[35][85]; +int R, maxc; +struct Qu +{ + int r, c; +} Q[MAXN]; +int qh, qt, sr, sc; +char C; +struct tt +{ + int r, c; +} st[1000]; +int ind; +int isbreak(char tt[]) +{ + int i, f = 1, d = 0; + for (i = 0; tt[i]; i++) + { + d = 1; + if (tt[i] != '_') + { + return 0; + } + } + if (d) + { + return 1; + } + return 0; +} +int GetCon(char nn[]) +{ + int i; + for (i = 0; nn[i]; i++) + { + if (nn[i] != ' ') + { + C = nn[i]; + return 1; + } + } + return 0; +} +int ReadCase() +{ + int i, f = 1, d = 1; + maxc = ind = 0; + R = 0; + while (gets(grid[R])) + { + if (f) + { + if (GetCon(grid[R])) + { + f = 0; + } + } + while (1) + { + if (isbreak(grid[R])) + { + return 0; + } + for (i = 0; grid[R][i]; i++) + { + if (grid[R][i] != C && grid[R][i] != ' ') + { + st[ind].r = R; + st[ind++].c = i; + } + } + int l = strlen(grid[R]); + if (l > maxc) + { + maxc = l; + } + R++; + gets(grid[R]); + d = 0; + if (f) + { + if (GetCon(grid[R])) + { + f = 0; + } + } + } + } + if (d) + { + return 1; + } + return 0; +} +void Push(int r, int c) +{ + Q[qh].r = r; + Q[qh++].c = c; + qh %= MAXN; +} +Qu Pop() +{ + Qu temp; + temp.r = Q[qt].r; + temp.c = Q[qt++].c; + return temp; +} +void Bfs(int rr, int cc, char nn) +{ + int x[] = {0, 0, 1, -1}; + int y[] = {1, -1, 0, 0}; + int i, nr, nc; + Qu temp; + qh = qt = 0; + f[rr][cc] = 1; + Push(rr, cc); + while (qh != qt) + { + temp = Pop(); + for (i = 0; i < 4; i++) + { + nr = temp.r + x[i]; + nc = temp.c + y[i]; + if (nr > R || nr < 0 || nc < 0 || nc > maxc) + { + continue; + } + if (grid[nr][nc] == ' ') + { + grid[nr][nc] = nn; + Push(nr, nc); + } + } + } +} +void Print() +{ + int i; + for (i = 0; i < R; i++) + { + printf("%s\n", grid[i]); + } + printf("%s\n", grid[R]); +} +void Cal() +{ + int i; + for (i = 0; i < ind; i++) + { + Bfs(st[i].r, st[i].c, grid[st[i].r][st[i].c]); + } + Print(); +} +int main() +{ + int kase; + while (1) + { + kase = ReadCase(); + if (kase) + { + break; + } + Cal(); + } + return 0; +} diff --git a/787.cpp b/787.cpp new file mode 100644 index 0000000..c5245bd --- /dev/null +++ b/787.cpp @@ -0,0 +1,184 @@ +#include + +using namespace std; + +int numlen(int *arr) +{ + for (int i = 499; i >= 0; --i) + { + if (arr[i]) + { + return i; + } + } + return 0; +} + +class Num +{ +public: + Num(); + char min[500], max[500], in[6]; + int num, digit; + bool mp, Mp; +} *arr = NULL; + +Num::Num() +{ + for (int i = 0; i < 500; ++i) + min[i] = max[i] = 0; + mp = Mp = true; +} + +bool cmp(char *a, char *b) +{ + if (a[0] == '-') + if (b[0] == '-') + if (strlen(a) > strlen(b)) + return true; + else if (strlen(a) < strlen(b)) + return false; + else + return (strcmp(a, b) > 0); + else + return true; + else if (a[0] != '-') + if (b[0] != '-') + if (strlen(a) > strlen(b)) + return false; + else if (strlen(a) < strlen(b)) + return true; + else + return (strcmp(a, b) < 0); + else + return false; +} + +char *findmax(char *a, char *b, char *c) +{ + char *tmp[3] = {a, b, c}; + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 2; ++j) + if (cmp(tmp[j], tmp[j + 1])) + swap(tmp[j], tmp[j + 1]); + return tmp[0]; +} + +char *findmin(char *a, char *b, char *c) +{ + char *tmp[3] = {a, b, c}; + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 2; ++j) + if (cmp(tmp[j], tmp[j + 1])) + swap(tmp[j], tmp[j + 1]); + return tmp[2]; +} + +int main() +{ + int offset = -1; + char in[10]; + while (cin >> in) + { + if (!arr) + arr = new Num[100]; + if (strcmp(in, "-999999")) + { + strcpy(arr[++offset].in, in); + arr[offset].num = atoi(in); + reverse(arr[offset].in, arr[offset].in + strlen(arr[offset].in)); + arr[offset].digit = strlen(arr[offset].in); + if (arr[offset].in[arr[offset].digit] == '-') + --arr[offset].digit; + } + else + { + strcpy(arr[offset].min, arr[offset].in); + strcpy(arr[offset].max, arr[offset].in); + + if (arr[offset].num < 0) + arr[offset].min[strlen(arr[offset].min) - 1] = 0; + if (arr[offset].num < 0) + arr[offset].max[strlen(arr[offset].max) - 1] = 0; + if (arr[offset].num < 0) + arr[offset].mp = arr[offset].Mp = false; + for (int i = offset - 1; i >= 0; --i) + { + bool ap = true, bp = true, cp = true; + char a[600] = {0}, b[600] = {0}, c[600] = {0}; + int k, I[500] = {0}, M[500] = {0}, m[500] = {0}; + + for (k = 0; isdigit(arr[i].in[k]); ++k) + I[k] = arr[i].in[k] - '0'; + if (arr[i].in[strlen(arr[i].in) - 1] == '-') + ap = false; + if (!ap) + *a = '-'; + for (int x = numlen(I); x >= 0; --x) + a[x + (ap ? 0 : 1)] = I[x] + '0'; + reverse(a + (ap ? 0 : 1), a + strlen(a)); + for (k = 0; isdigit(arr[i + 1].min[k]); ++k) + m[k] = (arr[i + 1].min[k] - '0') * abs(arr[i].num); + for (k = 0; k < 500; ++k) + if (m[k] > 9) + { + m[k + 1] += m[k] / 10; + m[k] %= 10; + } + if ((arr[i].num < 0 ^ !arr[i + 1].mp) && arr[i].num) + bp = false; + if (!bp) + *b = '-'; + for (int x = numlen(m); x >= 0; --x) + b[x + (bp ? 0 : 1)] = m[x] + '0'; + reverse(b + (bp ? 0 : 1), b + strlen(b)); + + for (k = 0; isdigit(arr[i + 1].max[k]); ++k) + M[k] = (arr[i + 1].max[k] - '0') * abs(arr[i].num); + for (k = 0; k < 500; ++k) + if (M[k] > 9) + { + M[k + 1] += M[k] / 10; + M[k] %= 10; + } + if ((arr[i].num < 0 ^ !arr[i + 1].Mp) && arr[i].num) + cp = false; + if (!cp) + *c = '-'; + for (int x = numlen(M); x >= 0; --x) + c[x + (cp ? 0 : 1)] = M[x] + '0'; + reverse(c + (cp ? 0 : 1), c + strlen(c)); + + char *max = findmax(a, b, c), *min = findmin(a, b, c); + strcpy(arr[i].min, min + (*min == '-' ? 1 : 0)); + strcpy(arr[i].max, max + (*max == '-' ? 1 : 0)); + reverse(arr[i].min, arr[i].min + strlen(arr[i].min)); + reverse(arr[i].max, arr[i].max + strlen(arr[i].max)); + if (*max == '-') + arr[i].Mp = false; + if (*min == '-') + arr[i].mp = false; + } + + for (int i = 0; i <= offset; ++i) + { + if (!arr[i].Mp) + arr[i].max[strlen(arr[i].max)] = '-'; + reverse(arr[i].max, arr[i].max + strlen(arr[i].max)); + } + + char out[600] = {0}; + strcpy(out, arr[offset].max); + for (int i = offset - 1; i >= 0; --i) + if (cmp(out, arr[i].max)) + strcpy(out, arr[i].max); + cout << out << endl; + + offset = -1; + delete[] arr; + arr = NULL; + } + } + + return 0; +} diff --git a/789.cpp b/789.cpp index 75c4327..48ca00b 100644 --- a/789.cpp +++ b/789.cpp @@ -1,70 +1,56 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +#include -string convert(string S) { - int i, L = S.length() - 1; - FOI(i, 0, L) - if (!isalpha(S[i])) - S[i] = ' '; - return S; -} - -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - char Key; - cin >> Key; - string str; - map< string, set< int > > Map; - map< string, set< int > >::iterator mit; +using namespace std; - int Ind = 1; - getline(cin, str); - while (getline(cin, str)){ - str = convert(str); - stringstream ss(str); - string S; - while (ss >> S) - if (S[0] == Key) - Map[S].insert(Ind); - ++Ind; +int main() +{ + char key = getchar(); + cin.ignore(1024, '\n'); + string s; + int idx = 0; + map> r; + while (getline(cin, s)) + { + ++idx; + int i, len = s.length(); + for (i = 0; i < len; i++) + { + if (!isalpha(s[i])) + continue; + int j(i); + string t = ""; + while (j < len) + { + if (isalpha(s[j])) + t += s[j]; + else + break; + j++; + } + i = j; + if (t[0] == key) + { + r[t].insert(idx); + } + } } - for (mit = Map.begin(); mit != Map.end(); mit++) { - cout << (*mit).first; - set< int > Set; - set< int >::iterator sit; - Set = (*mit).second; - for (sit = Set.begin(); sit != Set.end(); sit++) - cout << " " << *sit; + for (map>::iterator i = r.begin(); i != r.end(); i++) + { + cout << i->first; + for (set::iterator j = i->second.begin(); j != i->second.end(); j++) + cout << " " << *j; cout << endl; } return 0; } +/* +T +CONSIDER SEVERAL LINES OF TEXT AND ONE CHARCTER CALLED KEY, +SPECIFIED ON A LINE PRECEDING THE TEXT. THE WORDS IN THE TEXT ARE +SEPARAED BY SINGLE SPACE. THERE ARE NO WORDS SPLITTED BETWEEN TWO +LINES. +WRITE A PROGRAM TO DISPLAY AN INDEX OF ALL WORDS WIHIN THE TEXT +STARTING WITH THE KEY. THE INDEX MUST BE ALPHABETICALLY SORTED. EACH +WORD IN THE INDEX MUST BE FOLLOWED BY THE LIST OF THE LINE NUMBERS IN +WHICH IT APPEARS. +*/ diff --git a/790.cpp b/790.cpp new file mode 100644 index 0000000..47ee27f --- /dev/null +++ b/790.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +struct Team +{ + int solve, time, fail[10], id; + bool sol[10]; +} t[26]; +struct Record +{ + int id, problem, time, yes; +} r[1000]; +int len, ptr; +void init() +{ + for (int i = 0; i < 26; i++) + { + t[i].solve = t[i].time = 0; + t[i].id = i; + for (int j = 0; j < 10; j++) + { + t[i].sol[j] = false, t[i].fail[j] = 0; + } + } + len = ptr = 0; +} +bool compare(int i, int j) +{ + if (t[j].solve > t[i].solve) + { + return true; + } + if ((t[j].solve == t[i].solve) && (t[j].time < t[i].time)) + { + return true; + } + if ((t[j].solve == t[i].solve) && (t[j].time == t[i].time) && (t[j].id < t[i].id)) + { + return true; + } + return false; +} +int compare1(const void *a, const void *b) +{ + Record *c = (Record *)a, *d = (Record *)b; + if (c->time != d->time) + { + return ((Record *)a)->time - ((Record *)b)->time; + } + return c->yes - d->yes; +} +char str[1000], problem, yes; +int main() +{ + int times, id, min, sec; + gets(str); + sscanf(str, "%d", ×); + gets(str); + for (int x = 0; x < times; x++) + { + if (x) + { + puts(""); + } + init(); + while (gets(str)) + { + if (sscanf(str, "%d %c %d:%d %c", &id, &problem, &min, &sec, &yes) != 5) + { + break; + } + if (id > ptr) + { + ptr = id; + } + r[len].id = id; + r[len].problem = problem - 'A'; + r[len].time = min * 60 + sec; + r[len].yes = yes; + len++; + } + qsort(r, len, sizeof(Record), compare1); + for (int i = 0; i < len; i++) + { + if (t[r[i].id].sol[r[i].problem]) + { + continue; + } + if (r[i].yes == 'N') + { + t[r[i].id].fail[r[i].problem]++; + } + else + { + t[r[i].id].sol[r[i].problem] = true; + t[r[i].id].solve++; + t[r[i].id].time += r[i].time; + t[r[i].id].time += t[r[i].id].fail[r[i].problem] * 20; + } + } + for (int i = 1; i <= ptr; i++) + for (int j = i + 1; j <= ptr; j++) + if (compare(i, j)) + { + Team temp = t[i]; + t[i] = t[j]; + t[j] = temp; + } + int rank = 1; + printf("RANK TEAM PRO/SOLVED TIME\n"); + for (int i = 1; i <= ptr; i++) + { + if (i && t[i].solve == t[i - 1].solve && t[i].time == t[i - 1].time) + { + printf("%4d %4d", rank, t[i].id); + } + else + { + printf("%4d %4d", i, t[i].id); + rank = i; + } + if (t[i].solve) + { + printf(" %4d %4d", t[i].solve, t[i].time); + } + printf("\n"); + } + } + return 0; +} diff --git a/793.cpp b/793.cpp new file mode 100644 index 0000000..2a66384 --- /dev/null +++ b/793.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define MAX 1000000 + +int p[MAX], rank_[MAX]; + +void make_set(int x) +{ + p[x] = x; + rank_[x] = 0; +} + +void link(int x, int y) +{ + if (rank_[x] > rank_[y]) + { + p[y] = x; + } + else + { + p[x] = y; + if (rank_[x] == rank_[y]) + { + rank_[y] = rank_[y] + 1; + } + } +} + +int find_set(int x) +{ + if (x != p[x]) + { + p[x] = find_set(p[x]); + } + return p[x]; +} + +void union_set(int x, int y) +{ + link(find_set(x), find_set(y)); +} + +int main() +{ + int numOfCase; + cin >> numOfCase; + int f = 0; + while (numOfCase--) + { + if (f) + { + cout << endl; + } + f = 1; + int numOfNode; + cin >> numOfNode; + for (int i = 1; i <= numOfNode; i++) + { + make_set(i); + } + int yes = 0, no = 0; + char cmd; + int a, b; + while (true) + { + cmd = cin.get(); + if (isalpha(cmd)) + { + break; + } + } + while (true) + { + if (!isalpha(cmd)) + { + break; + } + cin >> a >> b; + cin.get(); + if (cmd == 'c') + { + if (find_set(a) != find_set(b)) + { + union_set(a, b); + } + } + if (cmd == 'q') + { + if (find_set(a) == find_set(b)) + { + yes++; + } + else + { + no++; + } + } + cmd = cin.get(); + } + cout << yes << "," << no << endl; + } + return 0; +} diff --git a/795.cpp b/795.cpp new file mode 100644 index 0000000..2868833 --- /dev/null +++ b/795.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int decode[36] = {1, 3, 5, 10, 14, 19, 22, 29, 33, 8, 11, 15, 18, 23, 26, 28, 31, 35, 2, 6, 13, 16, 21, 25, 30, 32, 34, 0, 4, 7, 9, 12, 17, 20, 24, 27}; +char msg[110]; + +int main() +{ + string input; + while (getline(cin, input)) + { + if (input.size() == 0) + { + cout << endl; + continue; + } + for (int i = 0; i < input.size() / 36; i++) + { + for (int j = 0; j < 36; j++) + { + msg[i * 36 + j] = input[i * 36 + decode[j]]; + } + } + int count = 0; + for (int i = 0; i < input.size(); i++) + { + if (msg[i] == '#') + { + count++; + } + else + { + break; + } + } + string result = string(msg + count, msg + input.size()); + reverse(result.begin(), result.end()); + cout << result << endl; + } + return 0; +} diff --git a/796.cpp b/796.cpp new file mode 100644 index 0000000..3894b80 --- /dev/null +++ b/796.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +const int N = 201; +bool bridge[N][N]; +int low[N], d[N]; +int color[N], bcnt; +vector g[N]; + +void dfs(int u, int parent, int deep) +{ + color[u] = 1; + d[u] = low[u] = deep; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (color[v] == 1 && v != parent) + { + low[u] = low[u] < d[v] ? low[u] : d[v]; + } + if (color[v] == 0) + { + dfs(v, u, deep + 1); + low[u] = low[u] < low[v] ? low[u] : low[v]; + if (low[v] > d[u]) + { + bcnt++; + bridge[u][v] = bridge[v][u] = 1; + } + } + } + color[u] = 2; +} + +int main() +{ + int n; + while (~scanf("%d", &n)) + { + memset(bridge, false, sizeof(bridge)); + bcnt = 0; + for (int i = 0; i < N; i++) + { + g[i].clear(); + color[i] = 0; + } + for (int i = 0; i < n; i++) + { + int a, b, c; + scanf("%d (%d)", &a, &c); + for (int j = 0; j < c; j++) + { + scanf("%d", &b); + g[a].push_back(b); + g[b].push_back(a); + } + } + for (int i = 0; i < n; i++) + { + if (!color[i]) + { + dfs(i, 0, 0); + } + } + printf("%d critical links\n", bcnt); + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + if (bridge[i][j]) + { + printf("%d - %d\n", i, j); + } + } + } + printf("\n"); + } + return 0; +} diff --git a/798.cpp b/798.cpp new file mode 100644 index 0000000..00ca36e --- /dev/null +++ b/798.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +int w[16], h[16], m[16], a[128][128], H[128][128], n, width, height, cnt; + +static void f(int y, int x) +{ + int i, k; + for (;; y++, x = 0) + { + if (y >= height) + { + cnt++; + return; + } + while (x < width && H[y][x] > 0) + { + H[y + 1][x] = H[y][x] - 1; + x++; + } + if (x < width) + { + break; + } + } + for (k = 0; k < n; k++) + { + if (m[k] < 1 || (x + w[k]) > width || (y + h[k]) > height) + { + continue; + } + for (i = 0; i < w[k]; i++) + if (H[y][x + i] > 0) + { + goto next1; + } + for (i = 0; i < w[k]; i++) + { + H[y][x + i] = h[k]; + H[y + 1][x + i] = h[k] - 1; + } + m[k]--; + f(y, x + w[k]); + m[k]++; + for (i = 0; i < w[k]; i++) + { + H[y][x + i] = 0; + } + next1:; + } + for (k = 0; k < n; k++) + { + if (m[k] < 1 || w[k] == h[k] || + (x + h[k]) > width || (y + w[k]) > height) + { + continue; + } + for (i = 0; i < h[k]; i++) + if (H[y][x + i] > 0) + { + goto next2; + } + for (i = 0; i < h[k]; i++) + { + H[y][x + i] = w[k]; + H[y + 1][x + i] = w[k] - 1; + } + m[k]--; + f(y, x + h[k]); + m[k]++; + for (i = 0; i < h[k]; i++) + { + H[y][x + i] = 0; + } + next2:; + } +} + +int main() +{ + int i; + while (scanf("%d %d %d", &width, &height, &n) == 3) + { + for (i = 0; i < n; i++) + { + scanf("%d %d %d", &m[i], &w[i], &h[i]); + } + memset(a, 0, sizeof(a)); + memset(H, 0, sizeof(H)); + cnt = 0; + f(0, 0); + printf("%d\n", cnt); + } + return 0; +} diff --git a/799.cpp b/799.cpp new file mode 100644 index 0000000..d96554a --- /dev/null +++ b/799.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +typedef long long num; + +num min(num a, num b) +{ + return (a < b) ? a : b; +} + +num f(num n, num m) +{ + num k; + if (m >= n) + { + return n; + } + if (n == 1) + { + return 1; + } + for (k = min(m, n); k >= 2; k--) + if ((n % k) == 0 && ((n - 1) % (k - 1)) == 0) + { + return k; + } + return 0; +} + +int main() +{ + num n, k, m; + while (scanf("%lld %lld", &n, &m) == 2) + { + assert(n < 0x100000000LL); + assert(m < 0x100000000LL); + k = f(n, m); + if (k == 1 && n > 1) + { + k = 0; + } + if (k == 0) + { + printf("No solution\n"); + } + else if (k == 1) + { + printf("1 persons/group, 1 day\n"); + } + else + printf("%lld persons/group, %lld day%s\n", + k, (n - 1) / (k - 1), (n == k) ? "" : "s"); + } + return 0; +} diff --git a/801.cpp b/801.cpp new file mode 100644 index 0000000..eacaed4 --- /dev/null +++ b/801.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +struct Leg +{ + double mile, f20, f40; +} legs[1024]; +const double VCRUISE = 400; +const double AOPT = 30000; +const double GPHOPT = 2000; +const double GPHEXTRA = 10; +const double CLIMBCOST = 50; +const int MAXN = 1024;// maximum #legs +const int MAXH = 45; // between 20,000 and 40,000 feet +const double DINF = 1e+20; +double inter(Leg a, double h) +{ + return a.f20 + (a.f40 - a.f20) * (h - 20000) / (40000 - 20000); +} +void solve(int n) +{ + double dp[MAXN][MAXH]; + int from[MAXN][MAXH]; + for (int i = 0; i <= n; i++) + { + for (int j = 0; j <= 40; j++) + dp[i][j] = DINF; + } + + dp[0][0] = 0;// leg 0: altitude 0 + for (int i = 0; i < n; i++) + { + for (int j = 0; j <= 40; j++) + { + if (dp[i][j] >= DINF) + continue; + double alti_a = j * 1000.f; + for (int k = 20; k <= 40; k++) + { + double cc = 0, alti_b; + alti_b = k * 1000.f; + + if (alti_b > alti_a) + cc += CLIMBCOST * (alti_b - alti_a) / 1000.f; + double v = VCRUISE + inter(legs[i + 1], alti_b); + if (v <= 0) + continue; + double c = fabs(alti_b - AOPT) * GPHEXTRA / 1000.f + GPHOPT; + cc += c * legs[i + 1].mile / v; + if (dp[i][j] + cc < dp[i + 1][k]) + dp[i + 1][k] = dp[i][j] + cc, from[i + 1][k] = j; + } + } + } + + double ret = DINF; + int last = -1; + vector solution; + for (int i = 20; i <= 40; i++) + { + if (dp[n][i] < ret) + ret = dp[n][i], last = i; + } + for (int i = n; i > 0; i--) + { + solution.push_back(last); + last = from[i][last]; + } + for (int i = n - 1; i >= 0; i--) + printf(" %d", solution[i]); + // tricky + printf(" %.0lf\n", ceil(ret)); +} +int main() +{ + int testcase, cases = 0; + int n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + for (int i = 1; i <= n; i++) + { + scanf("%lf %lf %lf", + &legs[i].mile, &legs[i].f20, &legs[i].f40); + } + printf("Flight %d:", ++cases); + solve(n); + } + return 0; +} diff --git a/802.cpp b/802.cpp new file mode 100644 index 0000000..1293d54 --- /dev/null +++ b/802.cpp @@ -0,0 +1,180 @@ +#include + +using namespace std; + +typedef double Matrix[4][4]; + +const double eps = 1e-10; + +int dcmp(double a) +{ + return fabs(a) < eps ? 0 : (a < 0 ? -1 : 1); +} + +const int MAXN = 110; + +int n; +double a[MAXN], b[MAXN], c[MAXN]; + +bool gauss_jordan(Matrix a) +{ + int n = 3; + for (int i = 0; i < n; i++) + { + int r = i; + for (int j = i + 1; j < n; j++) + { + if (fabs(a[j][i]) > fabs(a[r][i])) + { + r = j; + } + } + if (dcmp(a[r][i]) == 0) + { + continue; + } + + if (r != i) + { + for (int j = 0; j <= n; j++) + { + swap(a[r][j], a[i][j]); + } + } + for (int j = 0; j < n; j++) + { + if (i != j) + { + for (int k = n; k >= i; k--) + { + a[j][k] -= a[j][i] / a[i][i] * a[i][k]; + } + } + } + } + + for (int i = 0; i < n; i++) + { + int xs = 0; + for (int j = 0; j < n; j++) + { + if (dcmp(a[i][j])) + { + xs++; + } + } + if (xs == 0) + { + if (dcmp(a[i][n])) + { + return false; + } + continue; + } + if (xs == 1) + { + if (dcmp(a[i][n] / a[i][i]) == -1) + { + return false; + } + else + { + continue; + } + } + if (dcmp(a[i][n]) == -1) + { + for (int j = 0; j <= n; j++) + { + a[i][j] *= -1; + } + } + bool Pos = false; + for (int j = 0; j < n; j++) + { + if (dcmp(a[i][j]) == 1) + { + Pos = true; + break; + } + } + if (!Pos) + { + return false; + } + } + return true; +} + +bool solve(int n) +{ + for (int i = 0; i < n; i++) + { + for (int j = i; j < n; j++) + { + for (int k = j; k < n; k++) + { + Matrix now, hi; + now[0][0] = a[i]; + now[0][1] = a[j]; + now[0][2] = a[k]; + now[0][3] = a[n]; + now[1][0] = b[i]; + now[1][1] = b[j]; + now[1][2] = b[k]; + now[1][3] = b[n]; + now[2][0] = c[i]; + now[2][1] = c[j]; + now[2][2] = c[k]; + now[2][3] = c[n]; + + bool ok = true; + int ord[] = {0, 1, 2}; + do + { + for (int ii = 0; ii < 3; ii++) + { + int jj = ord[ii]; + for (int kk = 0; kk < 3; kk++) + { + hi[kk][jj] = now[kk][ii]; + } + } + for (int ii = 0; ii < 3; ii++) + { + hi[ii][3] = now[ii][3]; + } + if (!gauss_jordan(hi)) + { + ok = false; + break; + } + } while (next_permutation(ord, ord + 3)); + if (ok) + { + return true; + } + } + } + } + return false; +} + +int main() +{ + int n, t = 0; + while (scanf("%d", &n) == 1 && n) + { + if (t) + { + printf("\n"); + } + for (int i = 0; i <= n; i++) + { + scanf("%lf%lf%lf", &a[i],&b[i],&c[i]); + } + printf("Mixture %d\n%s\n", ++t, solve(n) ? "Possible" : "Impossible"); + } + + return 0; +} diff --git a/803.cpp b/803.cpp new file mode 100644 index 0000000..e38e66d --- /dev/null +++ b/803.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +/** + UVa 803 - Page Selection by Keyword + by Rico Tiongson + Submitted: July 6, 2013 + Accepted 0.015s C++ + O(nlogn) time +*/ + +int r = 0, i, j, n; +char buf[10005], *p; +vector> v; +map::iterator it; +pair rel[25]; +vector key; +bool cmp(const pair &lhs, const pair &rhs) +{ + return lhs.second == rhs.second ? lhs.first > rhs.first : lhs.second < rhs.second; +} +void make_page() +{ + getchar(); + gets(buf); + for (p = buf; *p; ++p) + { + // transform into uppercase + if (*p >= 'a') + *p -= 32; + } + p = strtok(buf, " "); + v.push_back(map()); + while (p != NULL) + { + v.back().insert(pair(p, v.back().size())); + p = strtok(NULL, " "); + } +} +void make_query() +{ + getchar(); + gets(buf); + for (p = buf; *p; ++p) + { + if (*p >= 'a') + *p -= 32; + } + // cout << buf << endl; + p = strtok(buf, " "); + key.clear(); + while (p != NULL) + { + key.push_back(p); + p = strtok(NULL, " "); + } + // find keywords + for (i = 0; i < v.size(); ++i) + { + rel[i].first = i; + rel[i].second = 0; + // get relevance + for (j = 0; j < key.size(); ++j) + { + it = v[i].find(key[j]); + if (it != v[i].end()) + { + rel[i].second += (8 - j) * (8 - it->second); + // cout << (8-j) << " * " << (9-it->second); + } + } + } + printf("Q%d:", ++r); + n = v.size(); + make_heap(rel, rel + n, cmp); + if (!v.empty()) + { + if (rel[0].second) + { + putchar(' '); + putchar(' '); + for (i = 0, j = min(n, 5); i < j; ++i) + { + if (rel[0].second) + { + printf(" P%d", rel[0].first + 1); + pop_heap(rel, rel + n, cmp); + --n; + } + else + break; + } + } + } + putchar('\n'); +} +int main() +{ + puts("Query Pages"); + while (true) + { + switch (getchar()) + { + case 'P': + make_page(); + break; + case 'Q': + make_query(); + break; + case 'E': + return 0; + } + } +} diff --git a/804.cpp b/804.cpp new file mode 100644 index 0000000..a1dcfd5 --- /dev/null +++ b/804.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +struct transition +{ + vector in; + vector out; +}; +int slove(int len, int *p, transition tem) +{ + int t[len];//cout<> num_p && num_p) + { + int p[num_p]; + for (int i = 0; i < num_p; i++) + { + cin >> p[i]; + } + int num_t; + cin >> num_t; + transition t[num_t]; + for (int i = 0; i < num_t; i++) + { + int n; + while (cin >> n && n) + if (n > 0) + { + t[i].out.push_back(n); + } + else + { + t[i].in.push_back(-n); + } + } + int max_t; + cin >> max_t; + int i; + int num = 0; + int m = 0;//cout<= max_t) + { + break; + } + if (slove(num_p, p, t[i]))//cout<<1; + { + for (int j = 0; j < t[i].in.size(); j++) + { + p[t[i].in[j] - 1]--; + if (p[t[i].in[j] - 1] < 0) + { + p[t[i].in[j] - 1] = 0; + } + } + for (int j = 0; j < t[i].out.size(); j++) + { + p[t[i].out[j] - 1]++; + } + i = -1; + num++; + } + } + //cout< + +using namespace std; + +#define eps 1e-10 + +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + double length() + { + return hypot(x, y); + } + void read() + { + scanf("%lf %lf", &x, &y); + } +}; +const double pi = acos(-1); +int cmpZero(double v) +{ + if (fabs(v) > eps) + return v > 0 ? 1 : -1; + return 0; +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int on_seg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +struct Seg +{ + Pt s, e; + int label; + Seg(Pt a = Pt(), Pt b = Pt(), int l = 0) : s(a), e(b), label(l) + { + } + bool operator!=(const Seg &other) const + { + return !((s == other.s && e == other.e) || (e == other.s && s == other.e)); + } +}; +int intersection(Pt as, Pt at, Pt bs, Pt bt) +{ + if (cmpZero(cross(as, at, bs) * cross(as, at, bt)) <= 0 && + cmpZero(cross(bs, bt, as) * cross(bs, bt, at)) <= 0) + return 1; + return 0; +} +Pt getIntersect(Seg a, Seg b) +{ + Pt u = a.s - b.s; + double t = cross2(b.e - b.s, u) / cross2(a.e - a.s, b.e - b.s); + return a.s + (a.e - a.s) * t; +} +double getAngle(Pt va, Pt vb) +{// segment, not vector + return acos(dot(va, vb) / va.length() / vb.length()); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} +int in_poly(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (on_seg(p[i], p[j], q)) + return 1; + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + cnt++; + } + return cnt & 1; +} +void formal_ord(vector &A) +{// to counter-clockwise + int s = 0, n = (int)A.size(); + for (int i = 0; i < A.size(); i++) + if (A[i] < A[s]) + s = i; + if (cmpZero(cross(A[(s + n - 1) % n], A[s], A[(s + 1) % n])) < 0) + reverse(A.begin(), A.end()); +} +vector> get_divide_poly(vector A, vector B) +{ + vector> ret; + Pt p; + double s = 0; + for (int i = 0; i < A.size(); i++) + { + if (i) + s += (A[i] - A[i - 1]).length(); + ret.push_back(make_pair(s, A[i])); + } + A.push_back(A[0]); + for (int i = 0; i + 1 < A.size(); i++) + { + for (int j = 0, k = B.size() - 1; j < B.size(); k = j++) + { + if (intersection(B[j], B[k], A[i], A[i + 1])) + { + if (cmpZero(cross(A[i], A[i + 1], B[j])) || cmpZero(cross(A[i], A[i + 1], B[k]))) + { + p = getIntersect(Seg(B[j], B[k]), Seg(A[i], A[i + 1])); + ret.push_back(make_pair(ret[i].first + (A[i] - p).length(), p)); + } + else + { + if (between(A[i], A[i + 1], B[j])) + { + p = B[j]; + ret.push_back(make_pair(ret[i].first + (A[i] - p).length(), p)); + } + if (between(A[i], A[i + 1], B[k])) + { + p = B[k]; + ret.push_back(make_pair(ret[i].first + (A[i] - p).length(), p)); + } + } + } + } + } + sort(ret.begin(), ret.end()); + int n = 1; + for (int i = 1; i < ret.size(); i++) + { + if (cmpZero(ret[i].first - ret[n - 1].first)) + ret[n++] = ret[i]; + } + ret.resize(n); + return ret; +} + +#define MAXM 131072 +#define MAXN 512 + +int g[MAXN][MAXN], ban[MAXN], used[MAXN], fromAB[MAXN], nA, nB; +Pt D[MAXN], path[MAXN], arrA[MAXN], arrB[MAXN]; +vector> ret; + +void ans(Pt A[], int n) +{ + int update; + do + { + update = 0; + for (int i = 0; i < n; i++) + { + if (on_seg(A[i], A[(i + 2) % n], A[(i + 1) % n])) + { + update = 1; + int ridx = (i + 1) % n; + for (int j = ridx; j < n; j++) + { + A[j] = A[j + 1]; + } + n--; + break; + } + } + } while (update); + if (n <= 2) + { + return; + } + vector vA, minExp; + for (int i = 0; i < n; i++) + { + vA.push_back(A[i]); + } + formal_ord(vA); + int st = 0; + for (int i = 0; i < n; i++) + { + if (vA[i] < vA[st]) + { + st = i; + } + } + for (int i = st; i >= 0; i--) + { + minExp.push_back(vA[i]); + } + for (int i = n - 1; i > st; i--) + { + minExp.push_back(vA[i]); + } + for (int i = 0; i < ret.size(); i++) + { + if (ret[i].size() != minExp.size()) + { + continue; + } + int same = 1; + for (int j = 0; j < minExp.size(); j++) + { + same &= minExp[j] == ret[i][j]; + } + if (same) + { + return; + } + } + ret.push_back(minExp); +} +void build_edge(vector> pA, map &Rlabel, int f) +{ + for (int i = 0, j = pA.size() - 1; i < pA.size(); j = i++) + { + int from = Rlabel[pA[j].second]; + int to = Rlabel[pA[i].second]; + g[from][to] |= f; + } +} +void dfs(int u, int idx, int N, int st) +{ + path[idx] = D[u], used[u] = 1; + for (int i = 0; i < N; i++) + { + if (g[u][i] && i == st) + { + int ok = 1; + for (int j = 0; j < N && ok; j++) + { + if (used[j] == 0) + ok &= !in_poly(path, idx + 1, D[j]); + } + for (int j = 0, k = idx; j <= idx; k = j++) + { + Pt mid = (path[j] + path[k]) * 0.5; + ok &= in_poly(arrA, nA, mid); + ok &= in_poly(arrB, nB, mid); + } + if (ok) + { + ans(path, idx + 1); + } + } + if (g[u][i] && !used[i] && !ban[i]) + { + dfs(i, idx + 1, N, st); + } + } + used[u] = 0; +} +bool cmp(vector A, vector B) +{ + for (int i = 0; i < A.size() && i < B.size(); i++) + { + if (!(A[i] == B[i])) + return A[i] < B[i]; + } + return A.size() < B.size(); +} +void solve(vector A, vector B) +{ + ret.clear(); + + for (int i = 0; i < A.size(); i++) + { + arrA[i] = A[i]; + } + for (int i = 0; i < B.size(); i++) + { + arrB[i] = B[i]; + } + nA = A.size(); + nB = B.size(); + formal_ord(A); + formal_ord(B); + + vector> pA, pB; + map Rlabel; + int label = 0; + + pA = get_divide_poly(A, B); + pB = get_divide_poly(B, A); + + for (int i = 0; i < pA.size(); i++) + { + if (!Rlabel.count(pA[i].second)) + { + D[label] = pA[i].second, fromAB[label] = 1; + Rlabel[pA[i].second] = label++; + } + } + for (int i = 0; i < pB.size(); i++) + { + if (!Rlabel.count(pB[i].second)) + { + D[label] = pB[i].second, fromAB[label] = 2; + Rlabel[pB[i].second] = label++; + } + } + + int N = (int)Rlabel.size(); + memset(g, 0, sizeof(g)); + memset(ban, 0, sizeof(ban)); + build_edge(pA, Rlabel, 1); + build_edge(pB, Rlabel, 2); + for (int i = 0; i < N; i++) + { + int ok = in_poly(arrA, A.size(), D[i]) && in_poly(arrB, B.size(), D[i]); + if (!ok) + { + ban[i] = 1; + } + } + for (int i = 0; i < N; i++) + { + if (!ban[i]) + { + memset(used, 0, sizeof(used)); + dfs(i, 0, N, i); + } + } + printf("Number of intersection regions: %d\n", ret.size()); + sort(ret.begin(), ret.end(), cmp); + for (int i = 0; i < ret.size(); i++) + { + printf("Region %d:", i + 1); + for (int j = 0; j < ret[i].size(); j++) + { + printf("(%.2lf,%.2lf)", ret[i][j].x, ret[i][j].y); + } + printf("\n"); + } +} + +int main() +{ + int N, M, cases = 0; + double x, y; + while (scanf("%d", &N) == 1 && N) + { + vector A, B; + for (int i = 0; i < N; i++) + { + scanf("%lf%lf", &x, &y); + A.push_back(Pt(x, y)); + } + scanf("%d", &M); + for (int i = 0; i < M; i++) + { + scanf("%lf%lf", &x, &y); + B.push_back(Pt(x, y)); + } + printf("Data Set %d\n", ++cases); + for (int i = 0; i < A.size(); i++) + { + // assert(!on_seg(A[i], A[(i + 2) % A.size()], A[(i + 1) % A.size()])); + } + for (int i = 0; i < B.size(); i++) + { + // assert(!on_seg(B[i], B[(i + 2) % B.size()], B[(i + 1) % B.size()])); + } + solve(A, B); + } + return 0; +} diff --git a/806.cpp b/806.cpp new file mode 100644 index 0000000..d5d318e --- /dev/null +++ b/806.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +#define MAXN 67 + +char Map[MAXN][MAXN], Fg[MAXN][MAXN]; +int Seq[MAXN * MAXN]; +int N, tn, the_index; + +int ReadCase() +{ + int i, j; + tn = 0; + scanf("%d", &N); + if (!N) + return 0; + if (N > 0) + for (i = 0; i < N; i++) + scanf("%s", Map[i]); + else + while (scanf("%d", &j) && j != -1) + Seq[tn++] = j; + return 1; +} +int AllOne(int x, int y, int p, int q) +{ + int i, j, c = 0; + for (i = x; i <= p; i++) + for (j = y; j <= q; j++) + if (Map[i][j] == '1') + c++; + return c; +} +int A[10]; +void Get(int level) +{ + int i, j = 1, k = 0; + for (i = 1; i <= level; i++) + { + k += A[i] * j; + j *= 5; + } + Seq[tn++] = k; +} +void Recur(int x, int y, int p, int q, int sizE, int v, int level) +{ + int j = sizE / 2, o; + A[level] = v; + o = AllOne(x, y, p, q); + if (o == sizE * sizE) + { + Get(level); + return; + } + if (o == 0) + return; + Recur(x, y, x + j - 1, y + j - 1, j, 1, level + 1); + Recur(x, y + j, x + j - 1, q, j, 2, level + 1); + Recur(x + j, y, p, y + j - 1, j, 3, level + 1); + Recur(x + j, y + j, p, q, j, 4, level + 1); +} +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +void Print() +{ + int i, k = 0, c = 1; + while (k < tn) + { + printf("%d", Seq[k]); + c = 1; + for (i = k + 1; i < tn && c < 12; i++, c++) + printf(" %d", Seq[i]); + k = i; + printf("\n"); + } +} +void Cal1() +{ + int z, k = N * N; + z = AllOne(0, 0, N - 1, N - 1); + tn = 0; + if (z == 0) + { + printf("Total number of black nodes = 0\n"); + return; + } + if (z == k) + { + printf("0\n"); + printf("Total number of black nodes = 1\n"); + return; + } + Recur(0, 0, N - 1, N - 1, N, 0, 0); + qsort(Seq, tn, sizeof(int), com); + Print(); + printf("Total number of black nodes = %d\n", tn); +} +void Fill(int x, int y, int p, int q, int sizE, int level) +{ + int i, j = sizE / 2, k, n; + if (level == the_index) + { + for (i = x; i <= p; i++) + for (k = y; k <= q; k++) + Fg[i][k] = 1; + return; + } + n = A[level]; + switch (n) + { + case 1: + Fill(x, y, x + j - 1, y + j - 1, j, level + 1); + break; + case 2: + Fill(x, y + j, x + j - 1, q, j, level + 1); + break; + case 3: + Fill(x + j, y, p, y + j - 1, j, level + 1); + break; + case 4: + Fill(x + j, y + j, p, q, j, level + 1); + break; + } +} +void Base5(int n) +{ + the_index = 0; + while (n) + { + A[the_index++] = n % 5; + n /= 5; + } + Fill(0, 0, N - 1, N - 1, N, 0); +} +void Cal2() +{ + int i; + N *= -1; + memset(Fg, 0, MAXN * MAXN); + for (i = 0; i < tn; i++) + Base5(Seq[i]); + for (i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + if (Fg[i][j]) + printf("*"); + else + printf("."); + printf("\n"); + } +} + +int main() +{ + int img = 1; + while (ReadCase()) + { + if (img > 1) + printf("\n"); + printf("Image %d\n", img++); + if (N > 0) + Cal1(); + else + Cal2(); + } + return 0; +} diff --git a/807.cpp b/807.cpp new file mode 100644 index 0000000..478ad44 --- /dev/null +++ b/807.cpp @@ -0,0 +1,182 @@ +#include + +using namespace std; + +#define BUFFER 80 +char mat[10][5000], final[10][BUFFER + 4], printmode = 0; +long end_[10], max_; + +long mat_write(int row, unsigned long long n, long long b) +{ + long i, k = 0, poww = 0, x = 0; + unsigned long long stack[800], indx[800]; + if (max_ < row) + { + max_ = row; + } + if (row) + { + for (; end_[row] < end_[row - 1]; end_[row]++) + { + mat[row][end_[row]] = ' '; + } + } + while (n) + { + if (n % b) + { + stack[k] = n % b, indx[k] = poww; + k++; + } + poww++; + n /= b; + } + if (printmode) + { + putchar('('); + } + for (i = k - 1; i >= 0; i--) + { + if (stack[i]) + { + if (stack[i] > 1 && indx[i]) + { + if (printmode) + { + printf("%llu*", stack[i]); + } + end_[row] += sprintf(mat[row] + end_[row], "%llu*", stack[i]); + } + if (indx[i]) + { + if (printmode) + { + printf("%llu", b); + } + end_[row] += sprintf(mat[row] + end_[row], "%llu", b); + } + else + { + if (printmode) + { + printf("%llu", stack[i]); + } + end_[row] += sprintf(mat[row] + end_[row], "%llu", stack[i]); + } + if (indx[i] > 1) + { + if (printmode) + { + putchar('^'); + } + x = mat_write(row + 1, indx[i], b); + while (end_[row] < x) + { + mat[row][end_[row]++] = ' '; + } + } + if (i) + { + end_[row] += sprintf(mat[row] + end_[row], "+"); + if (printmode) + { + putchar('+'); + } + } + } + } + if (printmode) + { + putchar(')'); + } + mat[row][end_[row]] = 0; + if (row == 0 && printmode) + { + puts(""); + } + return end_[row]; +} + +void print_mat(unsigned long long n, int limit) +{ + int i, j = 0, x, t, part, prevj = 0, newj = 0, flag = 1; + if (printmode) + { + for (i = max_; i + 1; i--) + { + puts(mat[i]); + } + puts(""); + } + for (part = 1; flag; part++) + { + j = prevj; + for (x = 0; mat[0][j] && x < limit; j++, x++) + { + if (mat[0][j] == '+') + { + newj = j; + } + } + if (mat[0][j] == 0 || mat[0][j] == '+') + { + newj = j; + } + if (mat[0][j] == 0) + { + flag = 0; + } + for (i = max_; i + 1; i--) + { + if (part == 1 && i == 0) + { + sprintf(final[0], "%llu = ", n); + } + else + for (t = 0; t + limit < BUFFER; t++) + { + sprintf(& final[i][t], " "); + } + for (j = prevj, x = BUFFER - limit; j < newj && end_[i] > j; j++, x++) + { + sprintf(& final[i][x], "%c", mat[i][j]); + } + final[i][x] = 0; + for (x = strlen(final[i]) - 1; final[i][x] == ' '; x--) + { + final[i][x] = 0; + } + puts(final[i]); + } + prevj = newj; + if (flag) + { + puts(""); + } + } +} + +int main() +{ + unsigned long long n, b; + int i, test = 0; + while (scanf("%llu%llu", &n, &b) != EOF && (n || b)) + { + if (test) + { + puts("--------------------------------------------------------------------------------"); + } + for (i = 0; i < 10; i++) + { + mat[i][0] = end_[i] = 0; + } + max_ = 0; + printf("%llu in complete base %llu:\n", n, b); + i = sprintf(mat[0], "%llu = ", n); + mat_write(0, n, b); + printf("\n"); + print_mat(n, BUFFER - i); + test++; + } + return 0; +} diff --git a/808.cpp b/808.cpp new file mode 100644 index 0000000..b72d230 --- /dev/null +++ b/808.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +typedef pair ii; +typedef vector vi; +typedef vector vii; +typedef vector vvi; +typedef vector vvii; +typedef map mii; + +typedef struct Point +{ + int x, y; + Point(int _x, int _y) + { + x = _x; + y = _y; + } +} Point; + +Point get_point(int n) +{ + int p, d, h, x, y; + p = sqrt((n - 1) / 3); + if ((p + 1) * p * 3 < (n - 1)) + p++; + int dif = n - 1 - p * (p - 1) * 3; + d = (p == 0 ? 0 : (dif / p) % 6); + h = (p == 0 ? 0 : (dif % p)); + switch (d) + { + case 0: + x = p - h; + y = h; + break; + case 1: + x = -h; + y = p; + break; + case 2: + x = -p; + y = p - h; + break; + case 3: + x = h - p; + y = -h; + break; + case 4: + x = h; + y = -p; + break; + case 5: + x = p; + y = h - p; + break; + } + return Point(x, y); +} + +int main() +{ + int a, b; + while (cin >> a >> b, a || b) + { + Point s = get_point(a); + Point e = get_point(b); + int res = abs(s.x - e.x) + abs(s.y - e.y); + if ((s.x - e.x) * (s.y - e.y) < 0) + { + res -= min(abs(s.x - e.x), abs(s.y - e.y)); + } + printf("The distance between cells %d and %d is %d.\n", a, b, res); + } + return 0; +} diff --git a/810.cpp b/810.cpp new file mode 100644 index 0000000..7d898d9 --- /dev/null +++ b/810.cpp @@ -0,0 +1,202 @@ +#include + +using namespace std; + +struct DieType +{ + int face[6]; + void Up() + { + int temp = face[0]; + face[0] = face[1]; + face[1] = face[2]; + face[2] = face[3]; + face[3] = temp; + } + void Down() + { + int temp = face[0]; + face[0] = face[3]; + face[3] = face[2]; + face[2] = face[1]; + face[1] = temp; + } + void Left() + { + int temp = face[0]; + face[0] = face[5]; + face[5] = face[2]; + face[2] = face[4]; + face[4] = temp; + } + void Right() + { + int temp = face[0]; + face[0] = face[4]; + face[4] = face[2]; + face[2] = face[5]; + face[5] = temp; + } +}; + +const DieType InitialDie[6][6] = + { + {{1, 1, 0, 0, 0, 0}, + {1, 2, 6, 5, 4, 3}, + {1, 3, 6, 4, 2, 5}, + {1, 4, 6, 3, 5, 2}, + {1, 5, 6, 2, 3, 4}, + {1, 6, 0, 0, 0, 0}}, + {{2, 1, 5, 6, 3, 4}, + {2, 2, 0, 0, 0, 0}, + {2, 3, 5, 4, 6, 1}, + {2, 4, 5, 3, 1, 6}, + {2, 5, 0, 0, 0, 0}, + {2, 6, 5, 1, 4, 3}}, + {{3, 1, 4, 6, 5, 2}, + {3, 2, 4, 5, 1, 6}, + {3, 3, 0, 0, 0, 0}, + {3, 4, 0, 0, 0, 0}, + {3, 5, 4, 2, 6, 1}, + {3, 6, 4, 1, 2, 5}}, + {{4, 1, 3, 6, 2, 5}, + {4, 2, 3, 5, 6, 1}, + {4, 3, 0, 0, 0, 0}, + {4, 4, 0, 0, 0, 0}, + {4, 5, 3, 2, 1, 6}, + {4, 6, 3, 1, 5, 2}}, + {{5, 1, 2, 6, 4, 3}, + {5, 2, 0, 0, 0, 0}, + {5, 3, 2, 4, 1, 6}, + {5, 4, 2, 3, 6, 1}, + {5, 5, 0, 0, 0, 0}, + {5, 6, 2, 1, 3, 4}}, + {{6, 1, 0, 0, 0, 0}, + {6, 2, 1, 5, 3, 4}, + {6, 3, 1, 4, 5, 2}, + {6, 4, 1, 3, 2, 5}, + {6, 5, 1, 2, 4, 3}, + {6, 6, 0, 0, 0, 0}}}; + +void (DieType::*Move[4])() = {&DieType::Up, &DieType::Down, &DieType::Left, &DieType::Right}; + +int Map[10][10]; +bool Used_[10][10][6][6]; +int H, W; +char MapName[21]; + +struct PointType +{ + int r, c; + + PointType operator+(const PointType &a) const + { + PointType out(a); + out.r += r; + out.c += c; + return out; + } + + bool Legal() const + { + return 0 <= r && r < H && 0 <= c && c < W; + } +}; + +const PointType Directory[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + +inline bool Used(const PointType &pos, const DieType &state) +{ + if (Used_[pos.r][pos.c][state.face[0] - 1][state.face[1] - 1]) + { + return true; + } + Used_[pos.r][pos.c][state.face[0] - 1][state.face[1] - 1] = true; + return false; +} + +struct +{ + PointType pos; + DieType state; +} Stack[3700]; + +bool DFS(int d) +{ + if (d != 0 && Stack[d].pos.r == Stack[0].pos.r && Stack[d].pos.c == Stack[0].pos.c) + { + for (int i = 0; i <= d; ++i) + { + if (i % 9 == 0) + { + cout << " "; + } + else + { + cout << ','; + } + cout << '(' << Stack[i].pos.r + 1 << ',' << Stack[i].pos.c + 1 << ')'; + if (i % 9 == 8 && i != d) + { + cout << ',' << endl; + } + } + cout << endl; + return true; + } + for (int dir = 0; dir < 4; ++dir) + { + Stack[d + 1].pos = Stack[d].pos + Directory[dir]; + if (!Stack[d + 1].pos.Legal()) + { + continue; + } + int &now_map = Map[Stack[d + 1].pos.r][Stack[d + 1].pos.c]; + if (now_map == -1 || now_map == Stack[d].state.face[0]) + { + Stack[d + 1].state = Stack[d].state; + (Stack[d + 1].state.*Move[dir])(); + if (Used(Stack[d + 1].pos, Stack[d + 1].state)) + { + continue; + } + if (DFS(d + 1)) + { + return true; + } + } + } + return false; +} + +int main() +{ + while (true) + { + cin >> MapName; + if (strcmp(MapName, "END") == 0) + { + break; + } + int up, front; + cin >> H >> W >> Stack[0].pos.r >> Stack[0].pos.c >> up >> front; + for (int i = 0; i < H; ++i) + for (int j = 0; j < W; ++j) + { + cin >> Map[i][j]; + for (int k = 0; k < 6; ++k) + for (int l = 0; l < 6; ++l) + { + Used_[i][j][k][l] = false; + } + } + --Stack[0].pos.r; + --Stack[0].pos.c; + Stack[0].state = InitialDie[up - 1][front - 1]; + cout << MapName << endl; + if (!DFS(0)) + { + cout << " No Solution Possible" << endl; + } + } +} diff --git a/811.cpp b/811.cpp new file mode 100644 index 0000000..3ccebd7 --- /dev/null +++ b/811.cpp @@ -0,0 +1,196 @@ +#include + +using namespace std; + +typedef complex Point; + +double dot(Point a, Point b) +{ + return a.real() * b.real() + a.imag() * b.imag(); +} +double cross(Point a, Point b) +{ + return a.real() * b.imag() - a.imag() * b.real(); +} +namespace CCW +{ + enum + { + RIGHT = 1, + LEFT = -1, + FRONT = 2, + BACK = 2, + OTHER = 0 + }; +}; +int ccw(Point a, Point b, Point c) +{ + b -= a; + c -= a; + if (cross(b, c) < 0) + { + return CCW::RIGHT; + } + if (cross(b, c) > 0) + { + return CCW::LEFT; + } + if (dot(b, c) < 0) + { + return CCW::BACK; + } + if (norm(b) < norm(c)) + { + return CCW::FRONT; + } + return CCW::OTHER; +} +bool cmp_real(const Point &a, const Point &b) +{ + if (a.imag() != b.imag()) + { + return a.imag() < b.imag(); + } + return a.real() < b.real(); +} +vector andrew(vector v) +{ + if (v.size() < 3) + { + return v; + } + sort(v.begin(), v.end(), cmp_real); + vector r[2]; + for (int i = 0; i < 2; ++i) + { + for (int j = 0; j < (int)v.size(); ++j) + { + r[i].push_back(v[j]); + while (2 < r[i].size()) + { + vector::iterator itr = r[i].end(); + if (ccw(*(itr - 3), *(itr - 2), *(itr - 1)) == CCW::RIGHT) + { + break; + } + else + { + r[i].erase(itr - 2); + } + } + } + if (i) + { + break; + } + for (int j = 0; j < (int)v.size(); ++j) + { + v[j] = Point(v[j].real(), v[j].imag()*-1); + } + } + for (int i = r[1].size() - 2; 0 < i; --i) + { + r[1][i] = Point(r[1][i].real(), r[1][i].imag()*-1); + r[0].push_back(r[1][i]); + } + return r[0]; +} +double dist_pp(const Point &a, const Point &b) +{ + double i = a.imag() - b.imag(); + double r = a.real() - b.real(); + return sqrt(i * i + r * r); +} + +int main() +{ + int tree; + while (scanf("%d", &tree) == 1 && tree) + { + vector v; + double len[tree], val[tree]; + for (int i = 0; i < tree; ++i) + { + double re, im; + scanf("%lf%lf%lf%lf", &re,&im,&val[i],&len[i]); + Point p(re,im); + v.push_back(p); + } + int bit = -1; + double mx = 0; + for (int i = 1; i < (1 << tree); ++i) + { + vector u; + double sum_l = 0; + double sum_v = 0; + for (int j = 0; j < tree; ++j) + { + if (i & (1 << j)) + { + u.push_back(v[j]); + sum_v += val[j]; + } + else + { + sum_l += len[j]; + } + } + u = andrew(u); + if (u.size() == 0) + { + continue; + } + double fance = dist_pp(u[0], u.back()); + for (int j = 0; j + 1 < (int)u.size(); ++j) + { + fance += dist_pp(u[j], u[j + 1]); + } + if (fance <= sum_l) + { + if (mx < sum_v) + { + mx = sum_v; + bit = i; + } + } + } + double ex = 0; + vector u; + for (int i = 0; i < (int)v.size(); ++i) + { + if (bit & (1 << i)) + { + u.push_back(v[i]); + } + else + { + ex += len[i]; + } + } + u = andrew(u); + ex -= dist_pp(u[0], u.back()); + for (int i = 0; i + 1 < (int)u.size(); ++i) + { + ex -= dist_pp(u[i], u[i + 1]); + } + static int cnt = 0; + if (cnt) + { + puts(""); + } + printf("Forest %d\n", ++cnt); + printf("Cut these trees:"); + for (int i = 0; i < tree; ++i) + { + if (bit & (1 << i)) + ; + else + { + printf(" %d", i + 1); + } + } + puts(""); + printf("Extra wood: %.2lf\n", ex); + } + return 0; +} diff --git a/812.cpp b/812.cpp new file mode 100644 index 0000000..e702e12 --- /dev/null +++ b/812.cpp @@ -0,0 +1,128 @@ +#include + +using namespace std; + +/*排åº..水。。 + * + * WAçš„è¯è¯•è¯•ä»¥ä¸‹2ç§Ÿæ•°æ® + * 1 + * 2 11 16 + * 2 + * 2 11 16 + * 2 1 3 + * 输出应该是 + * Workyards 1 + * Maximum profit is 0. + * Number of pruls to buy: 0 + * + * Workyards 2 + * Maximum profit is 16. + * Number of pruls to buy: 2 + * è¦æ³¨æ„最大利润å°äºŽ0时的情况,最大利润å°äºŽ0时,商人是ä¸ä¼šä¹°è¯¥å †çš„物å“,å³æ­¤æ—¶è¯¥å †æœ€å¤§çš„利润应该是0,而ä¸æ˜¯æ‰€æ±‚得的那个负数。 + * + */ + +const int MAXW = 50 + 5; +const int MAXB = 20 + 5; + +int w, b[MAXW], data[MAXW][MAXB];//è¾“å…¥çš„æ•°æ® +int p[MAXW][MAXB], num[MAXW][MAXB], imax[MAXW], k[MAXW]; +//p[i][j]:第i组买j个的利润...num[i][j]:第i组等达到利润最大的买的个数。。。imax[i]:第i组的最大利润。。。k[i]:num[i]的上界 +int ans_max, ans[10000], t;//ans_max:最大利润。。。ans[]:买的个数。。。。t:ans的上界 + +void ini() +{ + memset(p, 0, sizeof(p)); + memset(imax, 0, sizeof(imax)); + memset(k, 0, sizeof(k)); + memset(num, 0, sizeof(num)); + t = 0, ans_max = 0; +} + +void dfs(int i, int last_v) +{ + if (i == w + 1) + { + ans[t++] = last_v; + return; + } + if (imax[i] == 0) + { + dfs(i + 1, last_v); + } + for (int q = 1; q <= k[i]; q++) + { + dfs(i + 1, last_v + num[i][q]); + } +} + +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +int main() +{ + int tot = 0; + bool first = true; + while (scanf("%d", &w)) + { + if (w == 0) + { + return 0; + } + if (!first) + { + printf("\n"); + } + tot++; + ini(); + for (int i = 1; i <= w; i++) + { + scanf("%d", &b[i]); + for (int j = 1; j <= b[i]; j++) + { + scanf("%d", &data[i][j]); + p[i][j] = p[i][j - 1] + 10 - data[i][j]; + if (imax[i] < p[i][j]) + { + imax[i] = p[i][j]; + } + } + } + for (int i = 1; i <= w; i++) + { + for (int j = 1; j <= b[i]; j++) + { + if (p[i][j] == imax[i]) + { + num[i][++k[i]] = j; + } + } + ans_max += imax[i]; + } + dfs(1, 0); + qsort(ans, t, sizeof(int), cmp); + printf("Workyards %d\nMaximum profit is %d.\n", tot, ans_max); + printf("Number of pruls to buy: "); + int i = 0, j = 0; + for (; j < 10 - 1 && i < t - 1; i++) + { + if (i == 0 || (i != 0 && ans[i] != ans[i - 1])) + { + printf("%d ", ans[i]); + j++; + } + } + for (; i < t; i++) + { + if (i == 0 || (i != 0 && ans[i] != ans[i - 1])) + { + printf("%d\n", ans[i]); + break; + } + } + first = false; + } + return 0; +} diff --git a/813.cpp b/813.cpp new file mode 100644 index 0000000..33e1d5a --- /dev/null +++ b/813.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=754 +//#tag 3D geometry geometria +//#tag rotation rotacao +//#sol use formula to calculate ccw rotation of v around an axis n (v and n 3d vectors). Also rotate the axis themselves. +// Only test for intersections that actually hit the arm and not the ones that hit the servos (sneaky). +// Also, only test for when the servo goes strictly below the floor, and not just touches it. + +// #include + +#define ll long long +#define ull unsigned long long +#define pii pair +#define pdd pair +#define F first +#define S second +#define REP(i, j, k) for (int(i) = (j); (i) < (k); ++(i)) +#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i)) +#define pb push_back +#define PI acos(-1) +#define db(x) cerr << #x << " = " << x << endl; +#define _ << ", " << +#define mp make_pair +#define cl(x) memset(x, 0, sizeof(x)) +#define EPS 1e-9 +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) + +// #define umap unordered_map + +template +inline string tostr(const _T &a) +{ + ostringstream os(""); + os << a; + return os.str(); +} + +struct point +{ + double x, y, z; + point(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {} + const point operator+(const point &a) const + { + return point(x + a.x, y + a.y, z + a.z); + } + const point operator-(const point &a) const + { + return point(x - a.x, y - a.y, z - a.z); + } + const point operator*(const double &a) const + { + return point(x * a, y * a, z * a); + } + const point operator/(const double &a) const + { + return point(x / a, y / a, z / a); + } +}; + +point cross(point a, point b) +{ + return point(a.y * b.z - b.y * a.z, a.z * b.x - a.x * b.z, a.x * b.y - b.x * a.y); +} +double dot(point a, point b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} +// ccw rotation of vector r around axis n +point rotate(point r, point n, double tet) +{ + return (r * cos(tet)) + (n * (dot(n, r) * (1 - cos(tet)))) + (cross(n, r) * sin(tet)); +} +double norm2(point p) +{ + return p.x * p.x + p.y * p.y + p.z * p.z; +} + +bool intersecth(point p1, point q1, point p2, point q2) +{ + point da = q1 - p1; + point db = q2 - p2; + point dc = p2 - p1; + double vol = fabs(dot(dc, cross(da, db))); + if (vol > EPS) + { + return false; + } + double n = norm2(cross(da, db)); + double s = n < EPS ? 10 : dot(cross(dc, db), cross(da, db)) / n; + return s > EPS && s < 1 - EPS; +} + +bool intersect(point p1, point q1, point p2, point q2) +{ + return intersecth(p1, q1, p2, q2) && intersecth(p2, q2, p1, q1); +} + +point pts[20]; +double dist[20]; +double ang[20]; + +double mzero(double x) +{ + if (-EPS < x && x < EPS) + { + return 0.0; + } + return x; +} + +int main() +{ + int n; + int cases = 1; + while (scanf("%d", &n) > 0 && n >= 0) + { + REP(i, 0, 20) + { + dist[i] = 0; + ang[i] = 0; + pts[i] = point(0, 0, 0); + } + REP(i, 0, n) + { + scanf("%lf", &dist[i]); + } + REP(i, 0, n) + { + scanf("%lf", &ang[i]); + ang[i] = (ang[i] / 180.0) * PI; + } + point vec(0, 0, 1); + pts[0] = point(0, 0, 0); + point xz(0, 1, 0);// y axis + point yz(1, 0, 0);// x axis + REP(i, 0, n) + { + if ((i & 1) == 0) + { + vec = rotate(vec, xz, ang[i]); + yz = rotate(yz, xz, ang[i]); + } + else + { + vec = rotate(vec, yz, ang[i]); + xz = rotate(xz, yz, ang[i]); + } + pts[i + 1] = pts[i] + (vec * dist[i]); + if (pts[i + 1].z < -EPS) + { + printf("Case %d: servo %d attempts to move arm below floor\n", cases++, (i + 1)); + goto end; + } + REP(j, 0, i - 1) + { + if (intersect(pts[i], pts[i + 1], pts[j], pts[j + 1])) + { + printf("Case %d: servo %d causes link collision\n", cases++, i + 1); + goto end; + } + } + } + printf("Case %d: robot's hand is at (%.3lf,%.3lf,%.3lf)\n", cases++, mzero(pts[n].x), mzero(pts[n].y), mzero(pts[n].z)); + end:; + } + return 0; +} diff --git a/814.cpp b/814.cpp new file mode 100644 index 0000000..609967d --- /dev/null +++ b/814.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +map> server; + +char line[5120]; + +int main() +{ + while (gets(line)) + { + char *p = strtok(line, " \t"); + // if (p==NULL) while(1); //debug..no + if (*p == '*') + { + break; + } + //skip first "MTA" + string host(strtok(NULL, " \t")); + int n = atoi(strtok(NULL, " \t")); + set id; + while (--n >= 0) + { + id.insert(strtok(NULL, " \t")); + } + server[host] = id; + } + while (gets(line)) + { + char *p = strtok(line, " @\t"); + // if (p==NULL) { int q=0; q/=q; } //debug..no + if (*p == '*') + { + break; + } + string id(p); + string host(strtok(NULL, " @\t")); + vector addr; + while ((p = strtok(NULL, " @\t")) != NULL) + { + addr.push_back(p); + } + gets(line);//"*" + // if (*line!='*') { int q=0; q/=q; } //debug..no + vector data; + bool isFirst = true; + while (1) + { + gets(line); + if (*line == '*') + { + break; + } + if (*line == 0 && isFirst) + { + } + else + { + data.push_back(line); + isFirst = false; + } + } + //output + while (!addr.empty()) + { + string now(addr[1]); + printf("Connection between %s and %s\n", host.c_str(), now.c_str()); + printf(" HELO %s\n", host.c_str()); + printf(" 250\n"); + printf(" MAIL FROM:<%s@%s>\n", id.c_str(), host.c_str()); + printf(" 250\n"); + map>::iterator it = server.find(now); + // if (it==server.end()) while(1); //debug..no + set &t = (*it).second; + bool isSuccess = false; + set send; + for (int i = 0; i < addr.size(); i += 2) + { + if (addr[i + 1] != now) + { + continue; + } + if (send.find(addr[i]) != send.end())//already send + { + continue; + } + send.insert(addr[i]); + printf(" RCPT TO:<%s@%s>\n", addr[i].c_str(), now.c_str()); + if (t.find(addr[i]) == t.end()) + { + printf(" 550\n"); + } + else + { + printf(" 250\n"); + isSuccess = true; + } + } + for (int i = addr.size() - 1; i >= 0; i -= 2) + { + if (addr[i] != now) + { + continue; + } + addr.erase(addr.begin() + i); + addr.erase(addr.begin() + (i - 1)); + } + if (isSuccess) + { + printf(" DATA\n"); + printf(" 354\n"); + for (int i = 0; i < data.size(); i++) + { + printf(" %s\n", data[i].c_str()); + } + printf(" .\n"); + printf(" 250\n"); + } + printf(" QUIT\n"); + printf(" 221\n"); + } + } + return 0; +} diff --git a/815.cpp b/815.cpp new file mode 100644 index 0000000..1b11ba0 --- /dev/null +++ b/815.cpp @@ -0,0 +1,107 @@ +#include + +using namespace std; + +#define DIM 35 +int matrix[DIM * DIM]; +int m, n; +int flooded_regions; +int level; + +static void msort_merge(void *base, size_t nmemb1, size_t nmemb2, size_t size, + int (*compar)(const void *, const void *)) +{ + void *copy; + int i, j; + copy = malloc((nmemb1 + nmemb2) * size); + if (!copy) + { + abort(); /* should exit with error code... */ + } + memcpy(copy, base, (nmemb1 + nmemb2) * size); + i = j = 0; + for (i = j = 0; i + j < nmemb1 + nmemb2;) + { + if (j == nmemb2 || + (i != nmemb1 && + compar((char *)copy + i * size, (char *)copy + (nmemb1 + j) * size) < 0)) + { + memcpy((char *)base + (i + j) * size, (char *)copy + i * size, size); + i++; + } + else + { + memcpy((char *)base + (i + j) * size, (char *)copy + (nmemb1 + j) * size, size); + j++; + } + } +} + +void msort(void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) +{ + if (nmemb == 1) + { + return; + } + /* sort 2 parts */ + msort(base, nmemb / 2, size, compar); + msort(((char *)(base) + (nmemb / 2 * size)), nmemb - nmemb / 2, size, compar); + /* merge them */ + msort_merge(base, nmemb / 2, nmemb - nmemb / 2, size, compar); +} + +static int compar_int(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} + +double calculate(void) +{ + int i; + int curlevel = level; + if (level <= 0) + { + flooded_regions = 0; + return 0.0; + } + for (i = 0; i < m * n - 1; i++) + { + if (100 * (i + 1) * (matrix[i + 1] - matrix[i]) >= curlevel) + { + break; + } + curlevel -= 100 * (i + 1) * (matrix[i + 1] - matrix[i]); + } + flooded_regions = i + 1; + return (double)(matrix[i]) + (double)curlevel / (double)(100 * (i + 1)); +} + +int main() +{ + int region = 1; + int i, j; + while (1) + { + scanf("%d %d", &m, &n); + if (m == 0 && n == 0) + { + break; + } + for (i = 0; i < m; i++) + { + for (j = 0; j < n; j++) + { + scanf("%d", &matrix[i * n + j]); + } + } + scanf("%d", &level); + msort(matrix, m * n, sizeof(int), compar_int); + printf("Region %d\n", region++); + printf("Water level is %.2f meters.\n", calculate()); + printf("%.2f percent of the region is under water.\n", + 100.0 * (double)flooded_regions / (m * n)); + printf("\n"); + } + return 0; +} diff --git a/816.cpp b/816.cpp new file mode 100644 index 0000000..2e43699 --- /dev/null +++ b/816.cpp @@ -0,0 +1,257 @@ +#include + +using namespace std; + +#define WHITE 0 +#define GRAY 1 +#define BLACK 2 + +struct point {int r,c,way;}; +struct point queue_[400]; + +struct node +{ + int pass_way[3]; + char pass[4]; + int color; + int d; + struct point pie; +}; +struct node map_[10][10][4]; + +int start_r, start_c, end_r, end_c, Q_start, Q_end; + +int insertQueue(int r, int c, int way) +{ + queue_[Q_end].r = r; + queue_[Q_end].c = c; + queue_[Q_end].way = way; + Q_end = (Q_end + 1) % 400; +} + +int outputQueue() +{ + int ptr; + ptr = Q_start; + Q_start = (Q_start + 1) % 400; + return ptr; +} + +int get_way(char d) +{ + if (d == 'N') + { + return 0; + } + if (d == 'W') + { + return 1; + } + if (d == 'S') + { + return 2; + } + if (d == 'E') + { + return 3; + } +} + +int move_point(int r, int c, int way, int pass_way, int *new_r, int *new_c) +{ + int new_way; + if (pass_way == 0)// LEFT + { + new_way = (way + 1) % 4; + } + else if (pass_way == 1)// FORWARD + { + new_way = way; + } + else if (pass_way == 2)// RIGHT + { + new_way = (way - 1 + 4) % 4; + } + *new_r = r; + *new_c = c; + if (new_way == 0)// N + { + *new_r = r - 1; + } + if (new_way == 1)// W + { + *new_c = c - 1; + } + if (new_way == 2)// S + { + *new_r = r + 1; + } + if (new_way == 3)// E + { + *new_c = c + 1; + } + return new_way; +} + +int main() +{ + int i, j, k, l; + char caseName[20]; + int r1, r2, c1, c2; + int way, start_way, new_way; + int flag; + char buffer[5]; + int ptr; + while (scanf("%s", caseName) != EOF) + { + if (strcmp(caseName, "END") == 0) + { + break; + } + // init + for (i = 0; i < 10; i++) + { + for (j = 0; j < 10; j++) + { + for (k = 0; k < 4; k++) + { + for (l = 0; l < 3; l++) + { + map_[i][j][k].pass_way[l] = 0; + } + map_[i][j][k].color = WHITE; + map_[i][j][k].pie.r = 0; + map_[i][j][k].pie.c = 0; + } + } + } + scanf("%d%d%s%d%d", &r1, &c1, buffer, &r2, &c2); + start_r = r1; + start_c = c1; + //map_[start_r][start_c][get_way(buffer[0])].pass_way[1] = 1; // forward + start_way = get_way(buffer[0]); + end_r = r2; + end_c = c2; + while (scanf("%d", &r1) != EOF) + { + if (r1 == 0) + { + break; + } + scanf("%d", &c1); + i = 0; + while (1) + { + scanf("%s", buffer); + if (buffer[0] == '*') + { + break; + } + // mark each node can pass way + strncpy(map_[r1][c1][get_way(buffer[0])].pass, buffer + 1, strlen(buffer) - 1); + for (i = 1; i < strlen(buffer); i++) + { + if (buffer[i] == 'L') + { + map_[r1][c1][get_way(buffer[0])].pass_way[0] = 1; + } + if (buffer[i] == 'F') + { + map_[r1][c1][get_way(buffer[0])].pass_way[1] = 1; + } + if (buffer[i] == 'R') + { + map_[r1][c1][get_way(buffer[0])].pass_way[2] = 1; + } + } + } + } + // start BFS + Q_start = 0; + Q_end = 0; + //insertQueue( start_r , start_c , start_way ); + map_[start_r][start_c][start_way].color = GRAY; + map_[start_r][start_c][start_way].d = 0; + new_way = move_point(start_r, start_c, start_way, 1, &r2, &c2); + map_[r2][c2][new_way].color = GRAY; + map_[r2][c2][new_way].d = map_[start_r][start_r][start_way].d + 1; + map_[r2][c2][new_way].pie.r = start_r; + map_[r2][c2][new_way].pie.c = start_c; + map_[r2][c2][new_way].pie.way = start_way; + insertQueue(r2, c2, new_way); + flag = false; + while (Q_start != Q_end) + { + ptr = outputQueue(); + r1 = queue_[ptr].r; + c1 = queue_[ptr].c; + way = queue_[ptr].way; + if (r1 == end_r && c1 == end_c) + { + flag = true; + break; + } + // go for each point adj r1 , r2 , way + //for ( j = 0; j < strlen(map_[r1][c1][way].pass); j++ ){ + // if ( map_[r1][c1][way].pass[j] == 'L' ) + // i = 0; + // if ( map_[r1][c1][way].pass[j] == 'F' ) + // i = 1; + // if ( map_[r1][c1][way].pass[j] == 'R' ) + // i = 2; + for (i = 0; i < 3; i++) + { + if (map_[r1][c1][way].pass_way[i] == 1) + { + new_way = move_point(r1, c1, way, i, &r2, &c2); + if (map_[r2][c2][new_way].color == WHITE) + { + map_[r2][c2][new_way].color = GRAY; + map_[r2][c2][new_way].d = map_[r1][c1][way].d + 1; + map_[r2][c2][new_way].pie.r = r1; + map_[r2][c2][new_way].pie.c = c1; + map_[r2][c2][new_way].pie.way = way; + insertQueue(r2, c2, new_way); + } + } + } + map_[r1][c1][way].color = BLACK; + } + // print out the answer + Q_start = 0; + Q_end = 0; + if (flag == true) + { + while (!(r1 == 0 && c1 == 0)) + { + insertQueue(r1, c1, way); + r2 = map_[r1][c1][way].pie.r; + c2 = map_[r1][c1][way].pie.c; + way = map_[r1][c1][way].pie.way; + r1 = r2; + c1 = c2; + } + printf("%s\n ", caseName); + i = 0; + while (Q_start != Q_end) + { + ptr = --Q_end; + printf(" (%d,%d)", queue_[ptr].r, queue_[ptr].c); + if (i == 9 && Q_start != Q_end) + { + printf("\n "); + i = 0; + } + else + { + i++; + } + } + printf("\n"); + } + else + { + printf("%s\n No Solution Possible\n", caseName); + } + } +} diff --git a/817.cpp b/817.cpp new file mode 100644 index 0000000..b5fbf62 --- /dev/null +++ b/817.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define pb push_back +#define rep(i, n) REP(i, 0, n) + +bool isvalid(int p, string &buf) +{ + int i = 0; + while (i < p) + { + if ((i == 0 && buf[i] == '0') || + (i > 0 && !isdigit(buf[i - 1]) && buf[i] == '0')) + { + if (i + 1 < p && isdigit(buf[i + 1])) + { + return false; + } + } + i++; + } + return true; +} + +int num(string &in, int &p, int size) +{ + int ret = 0; + while (p < size && isdigit(in[p])) + { + ret = ret * 10 + (in[p++] - '0'); + } + return ret; +} + +int fact(string &in, int &p, int size) +{ + int ret = num(in, p, size); + while (p < size) + { + if (in[p] == '*') + { + p++; + int tmp = num(in, p, size); + ret *= tmp; + } + else + { + break; + } + } + return ret; +} + +int exp(string &in, int &p, int size) +{ + int ret = fact(in, p, size); + while (p < size) + { + int sign = 1; + if (in[p] == '+') + { + sign = 1; + } + else if (in[p] == '-') + { + sign = -1; + } + else + { + break; + } + p++; + int tmp = fact(in, p, size); + ret += sign * tmp; + } + return ret; +} + +vector ans; +const string op = "+-*"; +void search(int now, int p, string &in, string &buf, bool fg) +{ + if (in[now] == '=') + { + if (!fg || !isvalid(p, buf)) + { + return; + } + buf[p] = '='; + int i = 0; + int ret = exp(buf, i, p); + if (ret == 2000) + { + ans.pb(buf.substr(0, p + 1)); + } + return; + } + rep(i, 3) + { + buf[p] = op[i]; + buf[p + 1] = in[now]; + search(now + 1, p + 2, in, buf, true); + } + buf[p] = in[now]; + search(now + 1, p + 1, in, buf, fg); +} + +main() +{ + string in; + int tc = 1; + while (getline(cin, in) && in != "=") + { + cout << "Problem " << tc++ << endl; + ans.clear(); + string out(100, ' '); + int p = 0; + out[p++] = in[0]; + search(1, p, in, out, false); + if (ans.size() == 0) + { + cout << " IMPOSSIBLE" << endl; + } + else + { + sort(ans.begin(), ans.end()); + rep(i, ans.size()) cout << " " << ans[i] << endl; + } + } + return false; +} diff --git a/818.cpp b/818.cpp new file mode 100644 index 0000000..4992092 --- /dev/null +++ b/818.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +int one[65536], adj[16], n; + +int cnt(int a) +{ + int r; + for (r = 0; a > 0; a >>= 1) + { + r += (a & 1); + } + return r; +} + +int check(int z) +{ + int i, m = 0, u = ((1 << n) - 1) ^ z; + for (i = 0; i < n; i++) + { + if (((u >> i) & 1) && (adj[i] & u) == 0) + { + u &= ~(1 << i); + m++; + } + } + while (u != 0) + { + for (i = 0; i < n; i++) + if (((u >> i) & 1) && one[adj[i] & u] >= 0) + { + break; + } + if (i >= n) + { + return 0; + } + for (m++;;) + { + u &= ~(1 << i); + if ((adj[i] & u) == 0) + { + break; + } + i = one[adj[i] & u]; + if (i < 0) + { + return 0; + } + } + } + return (cnt(z) >= (m - 1)); +} + +int main() +{ + int i, t, x, y; + for (i = 0; i < 65536; i++) + { + one[i] = -1; + } + for (i = 0; i < 16; i++) + { + one[1 << i] = i; + } + for (t = 1; scanf("%d", &n) == 1 && n > 0; t++) + { + memset(adj, 0, sizeof(adj)); + while (scanf("%d %d", &x, &y) == 2 && x > 0 && y > 0) + { + if (x == y || x > n || y > n) + { + continue; + } + adj[x - 1] |= 1 << (y - 1); + adj[y - 1] |= 1 << (x - 1); + } + for (x = 0, y = n; (x >> n) == 0; x++) + if (cnt(x) < y && check(x)) + { + y = cnt(x); + } + printf("Set %d: Minimum links to open is %d\n", t, y); + } + return 0; +} diff --git a/819.cpp b/819.cpp new file mode 100644 index 0000000..af42d84 --- /dev/null +++ b/819.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +// bruteforce + iterative deepening +// first rotate polygon 360 deg and locate global min/max area +// of bounding rect, then that will be the estimates +// find bounding intervals for the min/max area +// you'd need to recursively zoom into the bounding interval until +// they are accurate to 3 dp + +typedef long double dbl; + +#define PI ((dbl)3.1415926535897932384623832795) +dbl x[200], y[200]; +int n, casen = 1; + +#define PP \ + dbl xmax = 0; \ + dbl xmin = 0; \ + dbl ymax = 0; \ + dbl ymin = 0; \ + for (i = 0; i < n; i++) \ + { \ + dbl s = sin(a); \ + dbl c = cos(a); \ + dbl x2 = x[i] * c - y[i] * s; \ + dbl y2 = x[i] * s + y[i] * c; \ + if (x2 < xmin) \ + xmin = x2; \ + else if (x2 > xmax) \ + xmax = x2; \ + if (y2 < ymin) \ + ymin = y2; \ + else if (y2 > ymax) \ + ymax = y2; \ + } \ + dbl aa = (xmax - xmin) * (ymax - ymin); + +#define UPDATE \ + if (aa > max) \ + { \ + max = aa; \ + amax = a; \ + } \ + else if (aa < min) \ + { \ + min = aa; \ + amin = a; \ + } + +dbl getMin(dbl min, dbl aL, dbl aR) +{ + while (1) + { + const dbl incre = (aR - aL) / 1000; + dbl a = aL; + const dbl oldmin = min; + while (a <= aR + incre * 2) + { + int i; + PP if (aa < min) + { + min = aa; + aL = a - incre / 2; + aR = a + incre / 2; + } + a += incre; + } + + if (abs(min - oldmin) < 0.0000001) + { + return min; + } + } +} + +dbl getMax(dbl max, dbl aL, dbl aR) +{ + while (1) + { + const dbl incre = (aR - aL) / 1000; + dbl a = aL; + const dbl oldmax = max; + while (a <= aR + incre * 2) + { + int i; + PP if (aa > max) + { + max = aa; + aL = a - incre / 2; + aR = a + incre / 2; + } + a += incre; + } + + if (abs(max - oldmax) < 0.0000001) + { + return max; + } + } +} + +void doit() +{ + dbl incre = PI / (1000000.0 / n); + dbl min = DBL_MAX; // min area + dbl max = 0; // max area + + dbl a = 0; // cur angle + dbl amin = 0; // angle related to min area + dbl amax = 0; // angle related to max area + dbl stop = 2 * PI + incre * 2; // overrun a little to be safe + int i, j; + + // estimate global min and max area + while (a <= stop) + { + PP UPDATE a += incre; + } + + // find interval bound + incre /= (1 << 12); + dbl aR; + a = amin; + while (1) + { + a += incre; + PP if (aa > min) { break; } + } + aR = a; + + a = amin; + while (1) + { + a -= incre; + PP if (aa > min) { break; } + } + + min = getMin(min, a, aR); + + printf("Gift %d\n", casen); + printf("Minimum area = %.3Lf\n", min); + + // find interval bound + a = amax; + while (1) + { + a += incre; + PP if (aa < max) { break; } + } + aR = a; + + a = amax; + while (1) + { + a -= incre; + PP if (aa < max) { break; } + } + + max = getMax(max, a, aR); + + printf("Maximum area = %.3Lf\n\n", max); + casen++; +} + +int main() +{ + while (1) + { + scanf("%d", &n); + if (n == 0) + { + break; + } + int i, j, k, xx0, yy0, xx, yy; + scanf("%d%d", &xx0, &yy0); + for (i = 1; i < n; i++) + { + scanf("%d%d", &xx, &yy); + x[i] = (dbl)(xx - xx0); + y[i] = (dbl)(yy - yy0); + } + x[0] = y[0] = 0; + doit(); + } + return 0; +} diff --git a/820.cpp b/820.cpp new file mode 100644 index 0000000..e3be79b --- /dev/null +++ b/820.cpp @@ -0,0 +1,121 @@ +#include + +using namespace std; + +#define maxn 102 +#define MIN(a, b) (a > b ? b : a) + +int N, S, T, C, Res; +int P[maxn], B[maxn][maxn], Cap[maxn]; +struct ss +{ + int x, cost; +}; +class comp +{ +public: + bool operator()(const ss &a, const ss &b) + { + return a.cost < b.cost; + } +}; +list L[maxn]; +priority_queue, comp> Q; +void Recur(int n, int c) +{ + int p; + if (n == S) + { + return; + } + p = P[n]; + B[p][n] -= c; + B[n][p] += c; + Recur(P[n], c); +} +int Dij() +{ + ss temp, dump; + int u, v, c; + list::iterator p; + temp.cost = 1000000; + temp.x = S; + Q.push(temp); + P[S] = -1; + Cap[S] = 1000000; + while (!Q.empty()) + { + temp = Q.top(); + Q.pop(); + u = temp.x; + for (p = L[u].begin(); p != L[u].end(); p++) + { + v = *p; + if (B[u][v] == 0) + continue; + c = MIN(temp.cost, B[u][v]); + if (v == T) + { + P[v] = u; + Recur(v, c); + Res += c; + return 1; + } + else if (Cap[v] < c) + { + dump.cost = c; + dump.x = v; + P[v] = u; + Cap[v] = c; + Q.push(dump); + } + } + } + return 0; +} +void Set() +{ + int i; + for (i = 1; i <= N; i++) + { + Cap[i] = 0; + P[i] = 0; + } + while (!Q.empty()) + Q.pop(); +} +void Cal() +{ + Res = 0; + Set(); + while (Dij()) + Set(); + cout << "The bandwidth is " << Res << ".\n"; +} + +int main() +{ + int u, v, c, nt = 1, i; + while (cin >> N && N) + { + cin >> S >> T >> C; + while (C--) + { + cin >> u >> v >> c; + L[u].push_back(v); + L[v].push_back(u); + B[u][v] += c; + B[v][u] += c; + } + cout << "Network " << nt++ << endl; + Cal(); + cout << endl; + for (i = 1; i <= N; i++) + { + L[i].clear(); + for (u = 1; u <= N; u++) + B[i][u] = 0; + } + } + return 0; +} diff --git a/821.cpp b/821.cpp index 11bedd2..04e2e59 100644 --- a/821.cpp +++ b/821.cpp @@ -1,93 +1,108 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; -typedef unsigned long long uint64; +#include -#define INF 1LL<<60 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) +using namespace std; -map >adj; -map >::iterator it1, it2, it3; -map use; - -int bfs(int src, int des){ - queue< pair > q; - q.push(make_pair(src, 0)); - while( !q.empty() ){ - pair p = q.front(); - q.pop(); - if( use[p.first] ) - continue; - if( p.first == des ) - return p.second; - int i; - FOI(i, 0, adj[p.first].size()-1) - q.push( make_pair(adj[p.first][i], p.second + 1) ); - use[p.first] = true; +/* +821 +Page Hopping +*/ +#define MAX 105 +#define MIN(a, b) (a > b ? b : a) +#define INF 214748 +int Link[MAX][MAX]; +int A[MAX], F[MAX]; +int maxN; +void Ini() +{ + int i, j; + for (i = 1; i <= 100; i++) + { + for (j = i + 1; j <= 100; j++) + { + Link[i][j] = Link[j][i] = INF; + } + Link[i][i] = 0; + F[i] = 0; } - return 0; } - -int main(){ - freopen("testI.txt", "r", stdin); - freopen("testO.txt", "w", stdout); - for(int T = 1; ; T++){ - adj.clear(); - int i, j, k; - int A, B; - scanf("%d%d", &A, &B); - if(A == 0 && B == 0) +void Floyd() +{ + int i, j, k; + int p, q, r; + for (k = 0; k < maxN; k++) + { + p = A[k]; + for (i = 0; i < maxN; i++) + { + q = A[i]; + for (j = 0; j < maxN; j++) + { + r = A[j]; + Link[q][r] = MIN(Link[q][r], Link[q][p] + Link[p][r]); + } + } + } +} +void Cal(int kase) +{ + int i, j, p, q; + double sum = 0, total; + Floyd(); + for (i = 0; i < maxN; i++) + { + p = A[i]; + for (j = i + 1; j < maxN; j++) + { + q = A[j]; + sum += Link[p][q] + Link[q][p]; + } + } + total = maxN * (maxN - 1); + printf("Case %d: average length between pages = %.3lf clicks\n", kase, sum / total); +} +int main() +{ + int a, b, kase = 1; + while (1) + { + maxN = 0; + scanf("%d%d", &a, &b); + if (!a && !b) + { break; - adj[A-1].push_back(B-1); - while( true ){ - scanf("%d%d", &A, &B); - if(A == 0 && B == 0) - break; - adj[A-1].push_back(B-1); } - int N = adj.size(); - double den = 0; - double num = 0; - - for (it1 = adj.begin(); it1 != adj.end(); it1++){ - for (it2 = adj.begin(); it2 != adj.end(); it2++){ - if((*it1).first != (*it2).first){ - use.clear(); - for (it3 = adj.begin(); it3 != adj.end(); it3++) - use[(*it3).first] = false; - num += bfs((*it1).first, (*it2).first); - den += 1; - } + Ini(); + Link[a][b] = 1; + if (!F[a]) + { + A[maxN++] = a; + F[a] = 1; + } + if (!F[b]) + { + A[maxN++] = b; + F[b] = 1; + } + while (1) + { + scanf("%d%d", &a, &b); + if (!a && !b) + { + break; + } + Link[a][b] = 1; + if (!F[a]) + { + A[maxN++] = a; + F[a] = 1; + } + if (!F[b]) + { + A[maxN++] = b; + F[b] = 1; } } - - printf("Case %d: average length between pages = %.3lf clicks\n", T, num/den); + Cal(kase++); } return 0; } diff --git a/822.cpp b/822.cpp new file mode 100644 index 0000000..7d8a23e --- /dev/null +++ b/822.cpp @@ -0,0 +1,126 @@ +#include + +using namespace std; + +struct Staff +{ + int pid; + vector proc_list; + bool operator<(const Staff &x) const + { + return pid < x.pid; + } +}; +struct Job +{ + int tid, st, proc; + Job(int t = 0, int s = 0, int e = 0) : tid(t), st(s), proc(e) {} +}; +struct cmpJob +{ + bool operator()(const Job &a, const Job &b) const + { + return a.st > b.st; + } +}; +bool cmp(pair a, pair b) +{ + if (a.first != b.first) + return a.first < b.first; + return a.second < b.second; +} +int main() +{ + int N, M, cases = 0; + while (scanf("%d", &N) == 1 && N) + { + map, cmpJob>> scheduler; + priority_queue, greater> timeline; + for (int i = 0; i < N; i++) + { + int tid, num, t0, t, dt; + scanf("%d %d %d %d %d", &tid, &num, &t0, &t, &dt); + for (int j = 0; j < num; j++) + { + scheduler[tid].push(Job(tid, t0, t)); + timeline.push(t0); + t0 += dt; + } + } + scanf("%d", &M); + Staff staffs[10]; + + for (int i = 0; i < M; i++) + { + int pid, num, tid; + scanf("%d %d", &pid, &num); + staffs[i].pid = pid; + for (int j = 0; j < num; j++) + { + scanf("%d", &tid); + staffs[i].proc_list.push_back(tid); + } + } + sort(staffs, staffs + M);// sort by pid + int working[10] = {}, preReq[10] = {}, finReq[10] = {}; + int totTime = -2, now; + timeline.push(-1); + while (!timeline.empty()) + { + now = timeline.top(); + timeline.pop(); + if (now == totTime) + continue; + totTime = now; + for (int i = 0; i < M; i++) + { + if (working[i] && finReq[i] <= now) + working[i] = 0; + } + vector> D; + for (int i = 0; i < M; i++) + { + if (working[i] == 0) + { + D.push_back(make_pair(preReq[i], i)); + } + } + sort(D.begin(), D.end(), cmp); + for (int i = 0; i < D.size(); i++) + { + Staff &u = staffs[D[i].second]; + for (int j = 0; j < u.proc_list.size(); j++) + { + int tid = u.proc_list[j]; + if (!scheduler[tid].empty()) + { + Job tmp = scheduler[tid].top(); + if (tmp.st <= now) + { + scheduler[tid].pop(); + preReq[D[i].second] = now; + finReq[D[i].second] = now + tmp.proc; + working[D[i].second] = 1; + timeline.push(finReq[D[i].second]); + break; + } + } + } + } + } + printf("Scenario %d: All requests are serviced within %d minutes.\n", ++cases, totTime); + } + return 0; +} +/* + 3 + 128 20 0 5 10 + 134 25 5 6 7 + 153 30 10 4 5 + 4 + 10 2 128 134 + 11 1 134 + 12 2 128 153 + 13 1 153 + 0 + */ \ No newline at end of file diff --git a/823.cpp b/823.cpp new file mode 100644 index 0000000..1a059b0 --- /dev/null +++ b/823.cpp @@ -0,0 +1,133 @@ +#include + +using namespace std; + +// geom + bruteforce all perm+arrangement + +bool inter(double x, double y, double r, double x2, double y2, double r2) +{ + double dx = x - x2, dy = y - y2; + double d = dx * dx + dy * dy; + return d < (r + r2) * (r + r2); +} + +void rotate(double x, double y, double a, double &x2, double &y2) +{ + x2 = x * cos(a) - y * sin(a); + y2 = x * sin(a) + y * cos(a); +} + +int check1(int a, int b, int c, + double tr[3], double br[3]) +{ // top and bottom radii + /* + Assume triangle is : + /\ + a / \ c + /____\ + b + */ + int peri = a + b + c; + double p = peri / 2.0; + double area = sqrt(p * (p - a) * (p - b) * (p - c)); + double incircleR = area / p; + + int i, j; + double mr[3]; + for (i = 0; i < 3; i++) + { + if (tr[i] > incircleR || br[i] > incircleR) + return 0; + mr[i] = max(tr[i], br[i]); + } + + double an[3], x[3], y[3]; + an[0] = .5 * acos((a * a + b * b - c * c) / (2.0 * a * b)); + x[0] = mr[0] / tan(an[0]); // (x,y) of circle@(a,b) + y[0] = mr[0]; + + an[1] = .5 * acos((a * a + c * c - b * b) / (2.0 * a * c)); + double tx = a - mr[1] / tan(an[1]); + double ty = -mr[1]; + rotate(tx, ty, 2 * an[0], x[1], y[1]); + + an[2] = .5 * acos((c * c + b * b - a * a) / (2.0 * c * b)); + x[2] = b - mr[2] / tan(an[2]); + y[2] = mr[2]; + + for (i = 0; i < 3; i++) + { + for (j = i + 1; j < 3; j++) + { + if (inter(x[i], y[i], tr[i], x[j], y[j], tr[j])) + return 0; + if (inter(x[i], y[i], br[i], x[j], y[j], br[j])) + return 0; + } + } + + return 1; +} + +int perm[6][3] = { + {0, 1, 2}, + {0, 2, 1}, + {1, 2, 0}, + {1, 0, 2}, + {2, 0, 1}, + {2, 1, 0}}; + +int main() +{ + int i, j, a, b, c, cc = 1; + while (1) + { + cin >> a >> b >> c; + if (a == 0) + break; + if (cc > 1) + cout << endl; + double d[3][2]; + for (i = 0; i < 3; i++) + cin >> d[i][0] >> d[i][1]; + int found = 0; + for (j = 0; j < 6; j++) + { + for (i = 0; i < 8; i++) + { + double tr[3], br[3]; + for (int k = 0; k < 3; k++) + { + int bit = 1 << k; + tr[k] = d[perm[j][k]][!(i & bit)] / 2; + br[k] = d[perm[j][k]][!!(i & bit)] / 2; + } + if (check1(a, b, c, tr, br)) + { + found = 1; + break; + } + if (check1(b, c, a, tr, br)) + { + found = 1; + break; + } + if (check1(c, a, b, tr, br)) + { + found = 1; + break; + } + } + } + printf("Triangle number %d:\n", cc++); + if (found) + { + printf("All three stoppers will fit in the triangular space\n"); + } + else + { + printf("Stoppers will not fit in the triangular space\n"); + } + } + return 0; +} diff --git a/824.cpp b/824.cpp new file mode 100644 index 0000000..cefceef --- /dev/null +++ b/824.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +bool land_at(int d, char pos[][3]) +{ + switch (d) + { + case 0: + return pos[0][1] == '1'; + case 1: + return pos[0][0] == '1'; + case 2: + return pos[1][0] == '1'; + case 3: + return pos[2][0] == '1'; + case 4: + return pos[2][1] == '1'; + case 5: + return pos[2][2] == '1'; + case 6: + return pos[1][2] == '1'; + case 7: + return pos[0][2] == '1'; + } +} + +int main() +{ + int x, y, a, b, d, new_d; + char c, pos[3][3]; + while (cin >> y) + { + if (y == -1) + { + break; + } + cin >> x >> d; + pos[1][1] = '1'; + for (int i = 0; i < 8; i++) + { + cin >> b >> a >> c; + pos[1 - (a - x)][1 + b - y] = c; + } + for (int i = 0; i < 8; i++) + { + new_d = (d + i + 5) % 8; + if (land_at(new_d, pos)) + { + cout << new_d << endl; + break; + } + } + } + return 0; +} diff --git a/825.cpp b/825.cpp new file mode 100644 index 0000000..f33315d --- /dev/null +++ b/825.cpp @@ -0,0 +1,170 @@ +#include + +using namespace std; + +/* +Walking on the safe side +825 +*/ +#define MAXN 200 +char Road[MAXN][MAXN]; +char f[MAXN][MAXN]; +char str[MAXN]; +struct ss +{ + int r, c; + int move; +} q[MAXN * MAXN]; +int qh, qt, R, C, Count; +int mx[] = {0, 0, 1, -1}; +int my[] = {1, -1, 0, 0}; +void Push(int r, int c, int move) +{ + q[qh].r = r; + q[qh].c = c; + q[qh].move = move; + qh++; + qh %= MAXN * MAXN; +} +ss Pop() +{ + ss p; + p.move = q[qt].move; + p.r = q[qt].r; + p.c = q[qt++].c; + qt %= MAXN * MAXN; + return p; +} +int Bfs() +{ + int i, tr, tc; + ss temp; + f[0][0] = 1; + qh = qt = 0; + Push(0, 0, 0); + while (qh != qt) + { + temp = Pop(); + for (i = 0; i < 4; i++) + { + tr = temp.r + mx[i]; + tc = temp.c + my[i]; + if (tr >= R || tr < 0 || tc >= C || tc < 0) + { + continue; + } + if (tr == R - 1 && tc == C - 1) + { + return temp.move + 1; + } + if (f[tr][tc] == 1 || Road[tr][tc]) + { + continue; + } + f[tr][tc] = 1; + Push(tr, tc, temp.move + 1); + } + } + return 0; +} +void Ids(int r, int c, int min, int level) +{ + int i; + int nr, nc; + if (r == R - 1 && c == C - 1) + { + Count++; + return; + } + if (level >= min) + { + return; + } + f[r][c] = 1; + for (i = 0; i < 4; i++) + { + nr = r + mx[i]; + nc = c + my[i]; + if (nr >= R || nr < 0 || nc >= C || nc < 0) + { + continue; + } + //if(nr + +using namespace std; + +bool table[26]; +char str[300], cipher[30]; +char ans[300]; +int N, len, lenc; +bool DFS(int m, int depth, int d) +{ + if (depth == len) + { + for (int i = 0; i < d; i++) + { + printf("%c", ans[i]); + } + printf("\n"); + return true; + } + else if (str[depth] == ' ') + { + ans[d] = ' '; + return DFS(m, depth + 1, d + 1); + } + else + { + char a = str[depth], b = str[depth + 1], c = str[depth + 2], x; + if (cipher[m] == a && cipher[(m + 1) % lenc] == c) + { + x = (b - 'A' + 26 - N) % 26; + if (table[x]) + { + ans[d] = x + 'A'; + if (DFS((m + 1) % lenc, depth + 3, d + 1)) + { + return true; + } + } + } + x = (a - 'A' + 26 - N) % 26; + if (!table[x]) + { + ans[d] = x + 'A'; + if (DFS(m, depth + 1, d + 1)) + { + return true; + } + } + return false; + } +} +int main() +{ + int t; + gets(str); + sscanf(str, "%d", &t); + gets(str); + for (int caseno = 0; caseno < t; caseno++) + { + if (caseno) + { + gets(str), puts(""); + } + memset(table, false, sizeof(table)); + gets(cipher); + lenc = strlen(cipher); + for (int i = 0; i < lenc; i++) + { + table[cipher[i] - 'A'] = true; + } + gets(str); + sscanf(str, "%d", &N); + int q; + gets(str); + sscanf(str, "%d", &q); + while (q--) + { + gets(str); + len = strlen(str); + if (!DFS(0, 0, 0)) + { + puts("error in encryption"); + } + } + } + return 0; +} diff --git a/830.cpp b/830.cpp new file mode 100644 index 0000000..4e1e64d --- /dev/null +++ b/830.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +char g[128][128]; +int n, m; +const int dx[] = {0, 0, 1, -1}; +const int dy[] = {1, -1, 0, 0}; + +void removeAnimal(int x, int y, int v) +{ + g[x][y] = '.'; + int tx, ty; + for (int i = 0; i < 4; i++) + { + tx = x + dx[i], ty = y + dy[i]; + if (tx < 0 || ty < 0 || tx >= n || ty >= m) + continue; + if (g[tx][ty] == v) + removeAnimal(tx, ty, v); + } +} + +int identifyAnimal(int x, int y) +{ + char v = g[x][y]; + int lx, ly, rx = x, ry = y, w, h; + while (rx + 1 < n && g[rx + 1][ry] == v) + rx++; + while (ry + 1 < m && g[rx][ry + 1] == v) + ry++; + lx = rx, ly = ry; + while (lx - 1 >= 0 && g[lx - 1][ly] == v) + lx--; + while (ly - 1 >= 0 && g[lx][ly - 1] == v) + ly--; + + int isShark = 0, isDolphin = 0; + for (int i = ly; i <= ry; i++) + { + if (lx - 1 >= 0 && g[lx - 1][i] == v) + isShark = 1; + if (rx + 1 < n && g[rx + 1][i] == v) + isShark = 1; + } + for (int i = lx; i <= rx; i++) + { + if (ly - 1 >= 0 && g[i][ly - 1] == v) + isShark = 1; + if (ry + 1 < m && g[i][ry + 1] == v) + isShark = 1; + } + w = ry - ly + 1, h = rx - lx + 1; + if (w > h) + swap(w, h); + removeAnimal(lx, ly, v); + if (isShark) + return 7; + if (w == 1 && h == 1) + return 0;// sardines + if (w == 1 && h == 2) + return 1;// mackerel + if (w == 1) + return 2;// salmons + if (w == h) + return 4;// turtles + if (w == 2 && h > 2) + return 3;// grouper + if (w == 3 && h > 3) + return 5;// dolphin + return 6; // whales +} + +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + while (getchar() != '\n') + ; + for (int i = 0; i < n; i++) + scanf("%s", g[i]); + + int kinds[16] = {}; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < m; j++) + { + if (g[i][j] >= 'a' && g[i][j] <= 'z') + { + int t = identifyAnimal(i, j); + kinds[t]++; + } + } + } + for (int i = 0; i < 8; i++) + printf("%d%c", kinds[i], i == 7 ? '\n' : ' '); + if (testcase) + puts(""); + } + return 0; +} diff --git a/832.cpp b/832.cpp new file mode 100644 index 0000000..3cefa54 --- /dev/null +++ b/832.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +#define EPS 1e-9 + +typedef struct +{ + double x, dy; +} rec_t; + +int cmp(const void *p, const void *q) +{ + return (((rec_t *)p)->x > ((rec_t *)q)->x) ? 1 : -1; +} + +double sol() +{ + double num, den, x, y, dc; + int i, n, m; + rec_t *r; + scanf("%d", &n); + for (num = den = 0.; n-- > 0 && scanf("%lf %d", &dc, &m) == 2;) + { + r = (rec_t *)malloc((2 * m + 2) * sizeof(rec_t)); + for (i = 0; m-- > 0;) + { + scanf("%lf %lf %lf", &r[i].dy, &r[i].x, &r[i + 1].x); + r[i + 1].dy = -r[i].dy; + i += 2; + } + qsort(r, m = i, sizeof(rec_t), &cmp); + for (i = 0, x = -1, y = 0.; i < m;) + { + den += (r[i].x - x) * y; + if (y >= dc) + { + num += (r[i].x - x) * (y - dc); + } + for (x = r[i].x; i < m && fabs(r[i].x - x) < EPS;) + { + y += r[i++].dy; + } + } + assert(fabs(y) < EPS); + free(r); + } + assert(fabs(den) > EPS); + return (num * 100. / den); +} + +int main() +{ + int t; + for (scanf("%d", &t); t-- > 0;) + { + printf("%.2f%%\n%s", sol() - 0.005, t ? "\n" : ""); + } + return 0; +} diff --git a/833.cpp b/833.cpp new file mode 100644 index 0000000..49908f7 --- /dev/null +++ b/833.cpp @@ -0,0 +1,74 @@ +#include + +using namespace std; + +struct segment +{ + int x1, x2, y1, y2; + segment(int _x1 = 0, int _y1 = 0, int _x2 = 0, int _y2 = 0) + { + x1 = _x1; + y1 = _y1; + x2 = _x2; + y2 = _y2; + if (x1 > x2) + { + swap(x1, x2); + swap(y1, y2); + } + }; +} s[1000]; + +int main() +{ + int ns, o, ox, oy, sc, t, x1, x2, y1, y2; + for (scanf("%d", &t); t--;) + { + scanf("%d", &sc); + for (int i = 0; i < sc; ++i) + { + scanf("%d %d %d %d", &x1, &y1, &x2, &y2); + s[i] = segment(x1, y1, x2, y2); + } + for (scanf("%d", &ns); ns--;) + { + scanf("%d %d", &ox, &oy); + double cy = oy; + while (1) + { + int idx = -1; + double cmaxy = 0.0; + for (int j = 0; j < sc; ++j) + if (ox > s[j].x1 && ox < s[j].x2) + { + double v = s[j].y1 + (ox - s[j].x1) / (double)(s[j].x2 - s[j].x1) * (s[j].y2 - s[j].y1); + if (v < cy && v > cmaxy) + { + cmaxy = v, idx = j; + } + } + if (idx == -1) + { + printf("%d\n", ox); + break; + } + else + { + if (s[idx].y1 > s[idx].y2) + { + ox = s[idx].x2, cy = (double)s[idx].y2; + } + else + { + ox = s[idx].x1, cy = (double)s[idx].y1; + } + } + } + } + if (t) + { + putc(10, stdout); + } + } + return 0; +} diff --git a/834.cpp b/834.cpp new file mode 100644 index 0000000..2346beb --- /dev/null +++ b/834.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +int main() +{ + bool first; + unsigned int numerator, denominator; + while (cin >> numerator >> denominator) + { + first = true; + cout << "[" << numerator / denominator; + if (numerator % denominator == 0) + { + cout << "]" << endl; + continue; + } + if (numerator > denominator) + { + numerator = numerator - denominator * (numerator / denominator); + } + do + { + if (first) + { + cout << ";"; + first = false; + } + else + { + cout << ","; + } + swap(numerator, denominator); + cout << numerator / denominator; + numerator = numerator - denominator * (numerator / denominator); + } while (denominator != 1); + cout << "]" << endl; + } + return 0; +} diff --git a/835.cpp b/835.cpp new file mode 100644 index 0000000..0703581 --- /dev/null +++ b/835.cpp @@ -0,0 +1,288 @@ +#include + +using namespace std; + +int seq[20][2] = {0, 4, 1, 4, 2, 4, 3, 4, 4, 4, 4, 0, 4, 1, 4, 2, 4, 3, + 1, 1, 1, 3, 2, 2, 3, 1, 3, 3, 0, 1, 0, 2, 0, 3, 1, 0, 2, 0, 3, 0}; +bool primes[100000]; +struct Node +{ + int ans[5][5]; +} n[200]; +int ans[5][5], sum, ptr; +int row[5], col[5], freer[5], freec[5], freed[2], diag[2]; +int compare(const void *a, const void *b) +{ + Node *c = (Node *)a, *d = (Node *)b; + for (int i = 0; i < 5; i++) + for (int j = 0; j < 5; j++) + if (c->ans[i][j] != d->ans[i][j]) + { + return c->ans[i][j] - d->ans[i][j]; + } + return 0; +} +bool isprime(int n) +{ + return primes[n]; +} +bool check(int depth) +{ + for (int i = 0; i < 5; i++) + { + if (freer[i] * 9 < row[i]) + { + return false; + } + if (freec[i] * 9 < col[i]) + { + return false; + } + } + if (freed[0] * 9 < diag[0]) + { + return false; + } + if (freed[1] * 9 < diag[1]) + { + return false; + } + if (depth == 8) + { + if (!isprime(ans[0][4] * 10000 + ans[1][4] * 1000 + ans[2][4] * 100 + + ans[3][4] * 10 + ans[4][4])) + { + return false; + } + if (!isprime(ans[4][0] * 10000 + ans[4][1] * 1000 + ans[4][2] * 100 + + ans[4][3] * 10 + ans[4][4])) + { + return false; + } + if (row[4] != 0 || col[4] != 0) + { + return false; + } + } + else if (depth == 13) + { + if (!isprime(ans[0][0] * 10000 + ans[1][1] * 1000 + ans[2][2] * 100 + + ans[3][3] * 10 + ans[4][4])) + { + return false; + } + if (!isprime(ans[4][0] * 10000 + ans[3][1] * 1000 + ans[2][2] * 100 + + ans[1][3] * 10 + ans[0][4])) + { + return false; + } + if (diag[0] != 0 || diag[1] != 0) + { + return false; + } + } + else if (depth == 16) + { + if (!isprime(ans[0][0] * 10000 + ans[0][1] * 1000 + ans[0][2] * 100 + + ans[0][3] * 10 + ans[0][4])) + { + return false; + } + if (row[0] != 0) + { + return false; + } + } + else if (depth == 19) + { + if (!isprime(ans[0][0] * 10000 + ans[1][0] * 1000 + ans[2][0] * 100 + + ans[3][0] * 10 + ans[4][0])) + { + return false; + } + if (col[0] != 0) + { + return false; + } + } + return true; +} +void DFS(int depth) +{ + if (depth == 20) + { + ans[2][1] = col[1]; + ans[1][2] = row[1]; + ans[2][3] = col[3]; + ans[3][2] = row[3]; + if (ans[2][1] > 9 || ans[1][2] > 9 || ans[2][3] > 9 || ans[3][2] > 9) + { + return; + } + if (ans[2][0] + ans[2][1] + ans[2][2] + ans[2][3] + ans[2][4] != sum) + { + return; + } + if (ans[0][2] + ans[1][2] + ans[2][2] + ans[3][2] + ans[4][2] != sum) + { + return; + } + if (!isprime(ans[1][0] * 10000 + ans[1][1] * 1000 + ans[1][2] * 100 + + ans[1][3] * 10 + ans[1][4])) + { + return; + } + if (!isprime(ans[3][0] * 10000 + ans[3][1] * 1000 + ans[3][2] * 100 + + ans[3][3] * 10 + ans[3][4])) + { + return; + } + if (!isprime(ans[0][1] * 10000 + ans[1][1] * 1000 + ans[2][1] * 100 + + ans[3][1] * 10 + ans[4][1])) + { + return; + } + if (!isprime(ans[0][3] * 10000 + ans[1][3] * 1000 + ans[2][3] * 100 + + ans[3][3] * 10 + ans[4][3])) + { + return; + } + if (!isprime(ans[2][0] * 10000 + ans[2][1] * 1000 + ans[2][2] * 100 + + ans[2][3] * 10 + ans[2][4])) + { + return; + } + if (!isprime(ans[0][2] * 10000 + ans[1][2] * 1000 + ans[2][2] * 100 + + ans[3][2] * 10 + ans[4][2])) + { + return; + } + for (int i = 0; i < 5; i++) + for (int j = 0; j < 5; j++) + { + n[ptr].ans[i][j] = ans[i][j]; + } + ptr++; + } + else + { + for (int i = 0; i <= 9; i++) + { + int nr = seq[depth][0], nc = seq[depth][1]; + if (depth < 9 && (i % 2 == 0 || i % 5 == 0)) + { + continue; + } + if ((nr == 0 || nc == 0) && i == 0) + { + continue; + } + if (i > row[nr] || i > col[nc]) + { + return; + } + if (nr == nc && i > diag[0]) + { + return; + } + if (nr + nc == 4 && i > diag[1]) + { + return; + } + ans[nr][nc] = i; + row[nr] -= i; + col[nc] -= i; + freer[nr]--; + freec[nc]--; + if (nr == nc) + { + diag[0] -= i; + freed[0]--; + } + if (nr + nc == 4) + { + diag[1] -= i; + freed[1]--; + } + if (check(depth)) + { + DFS(depth + 1); + } + row[nr] += i; + col[nc] += i; + freer[nr]++; + freec[nc]++; + if (nr == nc) + { + diag[0] += i; + freed[0]++; + } + if (nr + nc == 4) + { + diag[1] += i; + freed[1]++; + } + } + } +} +void init() +{ + for (int i = 0; i < 100000; i++) + { + primes[i] = true; + } + for (int i = 2; i < 100000; i++) + { + if (primes[i]) + { + for (int j = 2; i * j < 100000; j++) + { + primes[i * j] = false; + } + } + } +} +int main() +{ + init(); + int t, lefttop; + scanf("%d", &t); + while (t--) + { + ptr = 0; + scanf("%d %d", &sum, &lefttop); + ans[0][0] = lefttop; + for (int i = 0; i < 5; i++) + { + row[i] = col[i] = sum, freer[i] = 5, freec[i] = 5; + } + diag[0] = diag[1] = sum; + freed[0] = freed[1] = 5; + row[0] -= lefttop; + col[0] -= lefttop; + diag[0] -= lefttop; + freer[0] = freec[0] = freed[0] = 4; + DFS(0); + qsort(n, ptr, sizeof(Node), compare); + for (int i = 0; i < ptr; i++) + { + if (i) + { + puts(""); + } + for (int j = 0; j < 5; j++) + { + for (int k = 0; k < 5; k++) + { + printf("%d", n[i].ans[j][k]); + } + printf("\n"); + } + } + if (t != 0) + { + puts(""); + } + } + return 0; +} diff --git a/836.cpp b/836.cpp index 5456da7..dce6861 100644 --- a/836.cpp +++ b/836.cpp @@ -1,90 +1,109 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef long long int64; +#include -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define INF 1LL<<50 +using namespace std; -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int64 T; - cin >> T; - while (T--){ - vector< string > vec; - string str; - cin >> str; - int64 i, j, k; - int64 N = str.length(); - vec.push_back(str); - FOI(i, 1, N-1){ - cin >> str; - vec.push_back(str); +/** +836 +**/ +#define Dif(i, j, k) (Table[i + k][j] - Table[i][j]) +#define MAXN 110 +int N, M, MAX; +int Table[MAXN][MAXN]; +void ReadCase() +{ + int i, j, k; + char input[100]; + N = 1; + while (gets(input)) + { + for (i = 0; input[i]; i++) + if (input[i] == '\n') + { + input[i] = NULL; + } + if (strlen(input) < 1) + { + break; } - int64 mat[N][N]; - FOI(i, 0, N-1){ - FOI(j, 0, N-1){ - if (vec[i][j] == '1') - mat[i][j] = 1; - else - mat[i][j] = -1 * INF; + for (j = 0; input[j]; j++) + { + k = input[j] - '0'; + if (k == 1) + { + Table[N][j] = 1; + } + else + { + Table[N][j] = -1000; } } - - FOI(i, 1, N-1) - FOI(j, 0, N-1) - mat[i][j] += mat[i-1][j]; - - int64 maxSum = -1 * INF; - FOI(i, 0, N-1){ - FOI(j, i, N-1){ - int64 arr[N]; - FOI(k, 0, N-1) - arr[k] = mat[j][k]; - if (i > 0) - FOI(k, 0, N-1) - arr[k] -= mat[i-1][k]; - - int64 curSum = 0; - FOI(k, 0, N-1){ - curSum += arr[k]; - maxSum = max(curSum, maxSum); - if (curSum < 0) - curSum = 0; + N++; + } + N--; +} +void Cal() +{ + int i, j, k, t; + for (i = 1; i <= N; i++) + { + for (j = 0; j < N; j++) + { + Table[i][j] = Table[i][j] + Table[i - 1][j]; + } + } + MAX = Table[1][0]; + for (k = 1; k <= N; k++) + { + for (i = 0; i <= N - k; i++) + { + for (t = 0, j = 0; j < N; j++) + { + if (t >= 0) + { + t += Dif(i, j, k); + } + else + { + t = Dif(i, j, k); + } + if (t > MAX) + { + MAX = t; } } } - maxSum = max(0LL, maxSum); - cout << maxSum << endl; - if (T) - cout << endl; + } + if (MAX <= 0) + { + MAX = 0; + } + printf("%d\n", MAX); +} +void FREE() +{ + int i, j; + for (i = 0; i <= N; i++) + for (j = 0; j <= N; j++) + { + Table[i][j] = 0; + } +} +int main() +{ + int f = 0, kase; + char input[100]; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + ReadCase(); + Cal(); + if (kase) + { + FREE(); + printf("\n"); + } } return 0; } - diff --git a/837.cpp b/837.cpp new file mode 100644 index 0000000..4afaabd --- /dev/null +++ b/837.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +struct segment +{ + double r; + double x1, y1, x2, y2; +}; + +bool theTruthIsOutThere(const segment &a, const segment &b) +{ + return a.x1 < b.x1 ? true : false; +} + +int main() +{ + int n, nl; + int counter; + segment tmp; + vector v; + vector x; + cin >> n; + while (n--) + { + v.clear(); + x.clear(); + cin >> nl; + while (nl--) + { + cin >> tmp.x1 >> tmp.y1 >> tmp.x2 >> tmp.y2 >> tmp.r; + x.push_back(tmp.x1); + x.push_back(tmp.x2); + if (tmp.x1 > tmp.x2) + { + swap(tmp.x1, tmp.x2); + swap(tmp.y1, tmp.y2); + } + v.push_back(tmp); + } + sort(x.begin(), x.end()); + sort(v.begin(), v.end(), theTruthIsOutThere); + cout << x.size() + 1 << endl; + for (int i = 0, sz = x.size(); i < sz; i++) + { + if (i == 0) + { + printf("-inf %.3f 1.000\n", x[i]); + continue; + } + double result = 1.0; + for (int y = 0, sz2 = v.size(); y < sz2; y++) + { + if (v[y].x1 <= x[i - 1] && v[y].x2 >= x[i]) + { + result *= v[y].r; + } + } + printf("%.3f %.3f %.3f\n", x[i - 1], x[i], result); + if (i == sz - 1) + { + printf("%.3f +inf 1.000\n", x[i]); + } + } + if (n) + { + cout << endl; + } + } + return 0; +} diff --git a/838.cpp b/838.cpp new file mode 100644 index 0000000..37bc566 --- /dev/null +++ b/838.cpp @@ -0,0 +1,91 @@ +#include + +using namespace std; + +#define out 1005 + +int X[4] = {1, 0, -1, 0}, Y[4] = {0, 1, 0, -1}; +int B[15][15], U[1010], W[1010], max_, len, total, ab; + +void traverse(int x, int y) +{ + if (len + total <= max_) + { + return; + } + if (U[B[x][y]]) + { + return; + } + len++; + U[B[x][y]] = 1; + total -= W[B[x][y]]; + if (len > max_) + { + max_ = len; + } + int i; + for (i = 0; i < 4; i++) + { + traverse(x + X[i], y + Y[i]); + } + total += W[B[x][y]]; + U[B[x][y]] = 0; + len--; +} + +int main() +{ + int t, first = 1, n, i, j; + U[out] = 100000; + for (i = 0; i <= 15; i++) + { + B[0][i] = B[i][0] = out; + } + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i <= 1000; i++) + { + W[i] = U[i] = 0; + } + for (i = 1; i <= n; i++) + { + B[i][n + 1] = B[n + 1][i] = out; + } + ab = 0; + for (i = 1; i <= n; i++) + { + for (j = 1; j <= n; j++) + { + scanf("%d", &B[i][j]); + if (W[B[i][j]] == 0) + { + ab++; + } + W[B[i][j]]++; + } + } + max_ = 0; + total = n * n; + for (i = 1; i <= n && max_ != ab; i++) + { + for (j = 1; j <= n && max_ != ab; j++) + { + len = 0; + traverse(i, j); + } + } + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + printf("%d\n", max_); + } + return 0; +} diff --git a/839.cpp b/839.cpp new file mode 100644 index 0000000..46e4b82 --- /dev/null +++ b/839.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +#define MAX 10000 + +int M[MAX][5], N, dis; +char input[100]; + +struct ss +{ + char l, r; +} B[MAX]; + +int IsEmpty() +{ + int i; + for (i = 0; input[i]; i++) + { + if (input[i] == '\n') + { + input[i] = NULL; + break; + } + } + if (strlen(input) == 0) + return 1; + return 0; +} +int recur(int n) +{ + int i, l, r; + if (dis) + return -1; + B[n].l = 1; + l = M[n][0]; + r = M[n][2]; + if (l == 0) + { + for (i = n + 1; i < N; i++) + if (B[i].l == 0) + break; + l = recur(i); + M[n][0] = l; + } + if (dis) + return -1; + if (r == 0) + { + for (i = n + 1; i < N; i++) + if (B[i].l == 0) + break; + r = recur(i); + M[n][2] = r; + } + if (dis) + return -1; + if (M[n][1] * l != r * M[n][3]) + { + dis = 1; + return -1; + } + return r + l; +} +void Cal() +{ + int i, d; + dis = 0; + d = recur(0); + if (d < 0) + printf("NO\n"); + else + printf("YES\n"); + for (i = 0; i < N; i++) + B[i].l = 0; +} + +int main() +{ + int kases; + gets(input); + sscanf(input, "%d", &kases); + gets(input); + while (kases--) + { + N = 0; + while (gets(input)) + { + if (IsEmpty()) + break; + sscanf(input, "%d%d%d%d", &M[N][0], &M[N][1], &M[N][2], &M[N][3]); + N++; + } + Cal(); + if (kases) + printf("\n"); + } + return 0; +} diff --git a/840.cpp b/840.cpp new file mode 100644 index 0000000..7a7a0fd --- /dev/null +++ b/840.cpp @@ -0,0 +1,153 @@ +#include + +using namespace std; + +struct rec {int path[52], len;} s[50]; +bool map_[52][52], visited[52]; +int path[52], N, M, E, ptr; + +void recordpath(int depth) +{ + s[ptr].path[depth] = path[0]; + for (int i = 0; i < depth; i++) + { + s[ptr].path[i] = path[i]; + map_[path[i]][path[i + 1]] = false; + } + s[ptr].len = depth + 1; + ptr++; +} + +void DFS(int depth, int start) +{ + visited[start] = true; + path[depth] = start; + for (int i = 0; i < 52; i++) + { + if (map_[start][i]) + { + if (visited[i]) + { + if (i == path[0]) + { + recordpath(depth + 1); + } + } + else + { + DFS(depth + 1, i); + } + } + } +} + +bool comp(rec &a, rec &b) +{ + if (a.len > b.len) + { + return true; + } + if (a.len < b.len) + { + return false; + } + for (int i = 0; i < a.len; i++) + { + if (a.path[i] > b.path[i]) + { + return true; + } + if (a.path[i] < b.path[i]) + { + return false; + } + } + return false; +} + +int main() +{ + int times; + char edge[30]; + scanf("%d", ×); + for (; times; times--) + { + scanf("%d %d %d", &N, &M, &E); + ptr = 0; + for (int i = 0; i < 52; i++) + for (int j = 0; j < 52; j++) + { + map_[i][j] = false; + } + for (int i = 0; i < E; i++) + { + scanf("%s", edge); + char s, d; + if (edge[0] >= 'A' && edge[0] <= 'Z') + { + s = edge[0] - 'A'; + d = edge[2] - 'a' + 26; + } + else + { + s = edge[0] - 'a' + 26; + d = edge[2] - 'A'; + } + map_[s][d] = true; + } + for (int i = 0; i < N; i++) + { + for (int j = 0; j < 52; j++) + { + visited[j] = false; + } + DFS(0, i); + } + if (ptr == 0) + { + puts("NO"); + } + else + { + puts("YES"); + for (int i = 0; i < ptr; i++) + { + for (int j = i + 1; j < ptr; j++) + { + if (comp(s[i], s[j])) + { + rec t = s[i]; + s[i] = s[j]; + s[j] = t; + } + } + } + for (int i = 0; i < ptr; i++) + { + for (int j = 0; j < s[i].len; j++) + { + if (j) + { + printf("-"); + } + char c; + if (s[i].path[j] < 26) + { + c = s[i].path[j] + 'A'; + } + else + { + c = s[i].path[j] - 26 + 'a'; + } + printf("%c", c); + } + printf("\n"); + } + } + if (times != 1) + { + printf("\n"); + } + } + return 0; +} diff --git a/841.cpp b/841.cpp new file mode 100644 index 0000000..d2751c4 --- /dev/null +++ b/841.cpp @@ -0,0 +1,174 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) +#define ALL(C) (C).begin(), (C).end() +#define x second +#define y first +#define pb push_back +#define mp make_pair + +typedef pair pii; + +char m[20][20]; +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; + +class snake +{ +public: + vector d; + char m[20][20]; + int n; + int cnt; + snake(){}; + snake(int tn, char tm[20][20]) + { + d.pb(mp(0, 0)); + n = tn; + cnt = 0; + rep(i, n) rep(j, n) m[i][j] = tm[i][j]; + } + int getx() + { + return d[d.size() - 1].x; + } + int gety() + { + return d[d.size() - 1].y; + } + void move(int ney, int nex) + { + rep(i, d.size() - 1) + { + d[i] = d[i + 1]; + } + d[d.size() - 1].y = ney; + d[d.size() - 1].x = nex; + } + void eat(int ney, int nex) + { + d.pb(mp(ney, nex)); + } + bool islegal(int ney, int nex) + { + rep(i, d.size()) + { + if (d[i].y == ney && d[i].x == nex) + { + return false; + } + } + return true; + } + bool operator<(const snake &a) const + { + if (d.size() != a.d.size()) + { + return d.size() < a.d.size(); + } + rep(i, n) + { + rep(j, n) + { + if (m[i][j] != a.m[i][j]) + { + return m[i][j] < a.m[i][j]; + } + } + } + rep(i, d.size()) + { + if (d[i] != a.d[i]) + { + return d[i] < a.d[i]; + } + } + return false; + } +}; + +int solve(int n, snake ini, int egg) +{ + queue Q; + set S; + Q.push(ini); + while (!Q.empty()) + { + snake now = Q.front(); + Q.pop(); + if (now.d.size() == egg + 1) + { + return now.cnt; + } + rep(i, 4) + { + int nex = now.getx() + dx[i], ney = now.gety() + dy[i]; + if (nex < 0 || ney < 0 || nex >= n || ney >= n || + now.m[ney][nex] == 'x' || !now.islegal(ney, nex)) + { + continue; + } + snake next = now; + if (next.m[ney][nex] == 'o') + { + next.eat(ney, nex); + next.m[ney][nex] = '.'; + next.cnt++; + if (S.find(next) == S.end()) + { + S.insert(next), Q.push(next); + } + } + else + { + next.move(ney, nex); + next.cnt++; + if (S.find(next) == S.end()) + { + S.insert(next), Q.push(next); + } + } + } + } + return -1; +} + +main() +{ + int te; + cin >> te; + while (te--) + { + int n; + cin >> n; + rep(i, n) cin >> m[i]; + int egg = 0; + rep(i, n) + { + rep(j, n) + { + if (m[i][j] == 'o') + { + egg++; + } + } + } + snake ini(n, m); + int ans = solve(n, ini, egg); + if (ans != -1) + { + cout << ans << endl; + } + else + { + cout << "NO" << endl; + } + if (te) + { + puts(""); + } + } +} diff --git a/843.cpp b/843.cpp new file mode 100644 index 0000000..7b1fadd --- /dev/null +++ b/843.cpp @@ -0,0 +1,115 @@ +#include + +using namespace std; + +vector dic; +vector encrypted; +map c; +bool found; + +void decryptSentence(int n) +{ + if (found) + { + return; + } + if (n == encrypted.size()) + { + found = true; + return; + } + vector taken; + for (int i = 0; i < dic.size(); i++) + { + if (dic[i].size() == encrypted[n].size()) + { + bool ok = true; + for (int j = 0; j < dic[i].size(); j++) + { + if (c[encrypted[n][j]] == '*') + { + for (map::iterator it = c.begin(); it != c.end(); it++) + { + if ((*it).second == dic[i][j]) + { + ok = false; + break; + } + } + if (!ok) + { + break; + } + taken.push_back(encrypted[n][j]); + c[encrypted[n][j]] = dic[i][j]; + } + else + { + if (c[encrypted[n][j]] != dic[i][j]) + { + ok = false; + break; + } + } + } + if (ok) + { + decryptSentence(n + 1); + if (found) + { + return; + } + } + for (int k = 0; k < taken.size(); k++) + { + c[taken[k]] = '*'; + } + taken.clear(); + } + } +} + +int main() +{ + int n; + string input; + char a = '*'; + cin >> n; + while (n--) + { + cin >> input; + dic.push_back(input); + } + getline(cin, input); + while (getline(cin, input)) + { + found = false; + c.clear(); + encrypted.clear(); + for (char i = 'a'; i <= 'z'; i++) + { + c.insert(pair(i, '*')); + } + stringstream ss; + ss << input; + string temp; + while (ss >> temp) + { + encrypted.push_back(temp); + } + decryptSentence(0); + for (int i = 0; i < input.size(); i++) + { + if (input[i] >= 'a' && input[i] <= 'z') + { + cout << c[input[i]]; + } + else + { + cout << input[i]; + } + } + cout << endl; + } + return 0; +} diff --git a/844.cpp b/844.cpp new file mode 100644 index 0000000..b4ada79 --- /dev/null +++ b/844.cpp @@ -0,0 +1,184 @@ +#include + +using namespace std; + +int CaseN; +int N; +char Board[100][100]; +bool Tie; + +char Str[10]; + +inline bool StraightRow(int n, char color) +{ + for (int i = 0; i < N; ++i) + if (Board[n][i] != color) + { + return false; + } + return true; +} + +inline bool StraightCol(int n, char color) +{ + for (int i = 0; i < N; ++i) + if (Board[i][n] != color) + { + return false; + } + return true; +} + +inline int Straight(char color) +{ + int out = 0; + for (int i = 0; i < N; ++i) + { + if (StraightRow(i, color)) + { + ++out; + } + if (StraightCol(i, color)) + { + ++out; + } + } + return out; +} + +inline void PutL(int n, char color) +{ + int p = 0; + while (p < N - 1 && Board[n][p] != ' ') + { + ++p; + } + while (p > 0) + { + Board[n][p] = Board[n][p - 1]; + --p; + } + Board[n][0] = color; +} + +inline void PutR(int n, char color) +{ + int p = N - 1; + while (p > 0 && Board[n][p] != ' ') + { + --p; + } + while (p < N - 1) + { + Board[n][p] = Board[n][p + 1]; + ++p; + } + Board[n][N - 1] = color; +} + +inline void PutT(int n, char color) +{ + int p = 0; + while (p < N - 1 && Board[p][n] != ' ') + { + ++p; + } + while (p > 0) + { + Board[p][n] = Board[p - 1][n]; + --p; + } + Board[0][n] = color; +} + +inline void PutB(int n, char color) +{ + int p = N - 1; + while (p > 0 && Board[p][n] != ' ') + { + --p; + } + while (p < N - 1) + { + Board[p][n] = Board[p + 1][n]; + ++p; + } + Board[N - 1][n] = color; +} + +int main() +{ + cin >> CaseN; + for (--CaseN; CaseN >= 0; --CaseN) + { + cin >> N; + for (int i = 0; i < N; ++i) + for (int j = 0; j < N; ++j) + { + Board[i][j] = ' '; + } + Tie = true; + char color = 'X'; + while (true) + { + cin >> Str; + if (strcmp(Str, "QUIT") == 0) + { + if (Tie) + { + cout << "TIE GAME" << endl; + } + break; + } + if (Tie) + { + int n = 0; + for (int p = 1; Str[p]; ++p) + { + n = n * 10 + Str[p] - '0'; + } + --n; + switch (Str[0]) + { + case 'L': + PutL(n, color); + break; + case 'R': + PutR(n, color); + break; + case 'T': + PutT(n, color); + break; + case 'B': + PutB(n, color); + break; + } + int a = Straight('X'); + int b = Straight('O'); + if (a > b) + { + Tie = false; + cout << "X WINS" << endl; + } + else if (a < b) + { + Tie = false; + cout << "O WINS" << endl; + } + if (color == 'X') + { + color = 'O'; + } + else + { + color = 'X'; + } + } + } + if (CaseN > 0) + { + cout << endl; + } + } + return 0; +} diff --git a/845.cpp b/845.cpp new file mode 100644 index 0000000..ceaca79 --- /dev/null +++ b/845.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +char dig[50]; + +int ccmp(const void *a, const void *b) +{ + return *((char *)a) - (int)*((char *)b); +} + +main() +{ + int i, j, k, x, y, z, n; + for (;;) + { + scanf(" %s", dig); + if (!dig[1]) + { + break; + } + n = strlen(dig); + dig[n - 2] = dig[n - 1]; + dig[n - 1] = 0; + n--; + for (i = n - 1; i >= 0; i--) + { + for (j = dig[i] + 1; j <= '9'; j++) + { + for (k = i; dig[k]; k++) + { + if (dig[k] == j || + dig[k] == '2' && j == '5' || + dig[k] == '5' && j == '2' || + dig[k] == '6' && j == '9' || + dig[k] == '9' && j == '6') + { + dig[k] = dig[i]; + dig[i] = j; + goto found; + } + } + } + } + found: + if (i < 0) + { + printf("The price cannot be raised.\n"); + continue; + } + for (k = i + 1; dig[k]; k++) + { + if (dig[k] == '5') + { + dig[k] = '2'; + } + if (dig[k] == '9') + { + dig[k] = '6'; + } + } + qsort(&dig[i + 1], n - i - 1, sizeof(char), ccmp); + dig[n] = dig[n - 1]; + dig[n - 1] = '.'; + printf("%s\n", dig); + } +} diff --git a/846.cpp b/846.cpp new file mode 100644 index 0000000..a698ecd --- /dev/null +++ b/846.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +#define MAXN 46345 + +typedef long long ss; + +ss A[MAXN]; + +void GEN() +{ + ss sum; + ss i; + A[0] = 0; + for (i = 1; i <= 46341; i++) + { + sum = (i * (i + 1)); + A[i] = sum; + } +} + +// binary search +int Search(int key) +{ + int lo = 0, up = 46341, mid; + int dif; + mid = (lo + up) / 2; + dif = key - A[mid]; + while (lo <= up && dif != mid) + { + if (dif > 0) + { + if ((key - A[mid + 1]) <= 0) + { + return mid; + } + lo = mid + 1; + } + else + { + up = mid - 1; + } + mid = (lo + up) / 2; + dif = key - A[mid]; + } + return mid; +} + +void Cal(int n, int m) +{ + int diff, p; + p = Search(m - n); + diff = m - n - A[p]; + if (p + 1 >= diff) + { + printf("%d\n", p * 2 + 1); + } + else + { + printf("%d\n", p * 2 + 2); + } +} + +int main() +{ + int kase, n, m; + GEN(); + scanf("%d", &kase); + while (kase--) + { + scanf("%d%d", &n, &m); + if (m == n) + { + printf("0\n"); + continue; + } + Cal(n, m); + } + return 0; +} diff --git a/847.cpp b/847.cpp index e832626..023dc67 100644 --- a/847.cpp +++ b/847.cpp @@ -1,26 +1,46 @@ -#include -using namespace std; - -int main(){ - long long nine[50]={0}; - nine[1]=9; - for(long long i=2;i<50;i++){ - nine[i]=nine[i-1]*9; - } - long long n=0; - while(cin>>n){ - int ctr=0; - for(int i=0;;i++){ - if(n<=nine[i]){ - ctr=i; - break; - } - } - if(ctr%2!=0) - cout<<"Stan wins."< + +using namespace std; + +/** +847 +**/ +int main() +{ + long long s, p; + int i; + while (scanf("%lld", &s) == 1) + { + i = 1; + p = 1; + while (1) + { + if (i % 2 == 1) + { + p *= 9; + if (p >= s) + { + break; + } + } + else + { + p *= 2; + if (p >= s) + { + break; + } + } + i++; + } + if (i % 2) + { + printf("Stan wins.\n"); + } + else + { + printf("Ollie wins.\n"); + } + } + return 0; +} diff --git a/848.cpp b/848.cpp new file mode 100644 index 0000000..6815ec6 --- /dev/null +++ b/848.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +void trimBack(string &line) +{ + if (line == "") + { + return; + } + for (int i = (int)line.length() - 1; !line.empty() && line[i] == ' '; i--) + { + line.erase(i, 1); + } +} + +void trimFront(string &line) +{ + if (line == "") + { + return; + } + for (int i = 0; !line.empty() && line[i] == ' ';) + { + line.erase(i, 1); + } +} + +int main() +{ + string output = "", line, buffer = ""; + bool LineCreated = false; + while (1) + { + line = ""; + if (!LineCreated && !getline(cin, line)) + { + break; + } + if (LineCreated) + { + LineCreated = false; + } + if (line != "" && line[0] == '*') + { + break; + } + trimBack(line); + line = buffer + line; + if (line.length() > 72) + { + buffer = ""; + bool BigWord = true; + int LastGoodPos = -1; + for (int i = (int)line.length() - 1; i > 0; i--) + { + if (line[i] == ' ') + { + LastGoodPos = i; + if (i <= 72) + { + buffer = line.substr(i + 1, line.length()); + line = line.substr(0, i); + BigWord = false; + break; + } + } + } + if (LastGoodPos != -1 && BigWord) + { + buffer = line.substr(LastGoodPos, line.length()); + line = line.substr(0, LastGoodPos); + BigWord = false; + } + if (!BigWord) + { + LineCreated = true; + } + trimBack(line); + trimFront(buffer); + } + else if (line.length() < 72) + { + if (line.length() == 0) + { + buffer = ""; + } + else if (line[(int)line.length() - 1] == ' ') + { + buffer = ""; + } + else if (cin.peek() == ' ' || cin.peek() == '\n' || cin.eof()) + { + buffer = ""; + } + else + { + buffer = line + " ", line = ""; + continue; + } + } + else + { + buffer = ""; + } + output += line + '\n'; + } + if (output[output.length() - 1] == ' ' || output[output.length() - 1] == '\n') + { + output.erase(output.length() - 1, 1); + } + printf("%s\n", output.c_str()); + return 0; +} diff --git a/850.cpp b/850.cpp new file mode 100644 index 0000000..1e2afeb --- /dev/null +++ b/850.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +/** +850 +**/ +#define MAXN 102 +char STR[MAXN][82]; +char key[] = "the quick brown fox jumps over the lazy dog"; +int W, l; +struct ss +{ + int replace; + char c; +} V[30]; +int MATCH(char temp[]) +{ + char dummy[100]; + int i, j, k, m = 0; + strcpy(dummy, temp); + for (i = 0; i < 30; i++) + { + V[i].c = V[i].replace = 0; + } + for (i = 0; dummy[i]; i++) + { + if (isalpha(dummy[i])) + { + k = dummy[i] - 'a'; + if (key[m] == ' ') + { + m++; + } + j = key[m] - 'a'; + if (V[k].c == 0) + { + V[k].c = 1; + V[k].replace = j; + dummy[i] = key[m]; + } + else + { + j = V[k].replace + 'a'; + dummy[i] = j; + } + m++; + if (m > l) + { + return 0; + } + } + } + if (m < l) + { + return 0; + } + if (strcmp(dummy, key) == 0) + { + return 1; + } + return 0; +} +void Cal() +{ + int i, j, m; + for (i = 0; i < W; i++) + { + for (j = 0; STR[i][j]; j++) + { + if (STR[i][j] == ' ') + { + printf(" "); + continue; + } + m = STR[i][j] - 'a'; + printf("%c", V[m].replace + 'a'); + } + putchar('\n'); + } +} +int main() +{ + char input[100]; + int kase, i, f = 0; + l = strlen(key); + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + W = 0; + f = 0; + while (1) + { + gets(STR[W]); + for (i = 0; STR[W][i]; i++) + if (STR[W][i] == '\n') + { + STR[W][i] = NULL; + } + if (strlen(STR[W]) <= 0) + { + break; + } + if (f) + { + W++; + continue; + } + if ((strlen(STR[W]) == l) && MATCH(STR[W]) == 1) + { + f = 1; + } + W++; + } + if (!f) + { + printf("No solution.\n"); + } + else + { + Cal(); + } + if (kase) + { + putchar('\n'); + } + } + return 0; +} diff --git a/851.cpp b/851.cpp new file mode 100644 index 0000000..0c570e8 --- /dev/null +++ b/851.cpp @@ -0,0 +1,209 @@ +#include + +using namespace std; + +const int M = 10000007; +bool id[M]; +int mapl[8], mapr[8], d[6][6], p[6], a[8][8], s[6], n, m, ans, c[36], dir[36]; +inline int BKDRHash() +{ + int i, hash; + for (i = hash = 0; i <= m; ++i) + { + hash = hash * 131 + s[i]; + } + return ((hash & 0x7FFFFFFF) % M); +} +inline void init() +{ + int i, j; + scanf("%d", &n), m = n - 3; + for (i = 0; getchar(), i < n; ++i) + { + mapr[i] = 0; + for (j = n - 1; j >= 0; --j) + if (a[i][n - j - 1] = (getchar() == 'O')) + { + mapr[i] += (1 << j); + } + mapl[i] = mapr[i] >> 1; + } + for (i = 0; i < n - 2; ++i) + { + s[i] = (mapl[i + 1] ^ ((1 << (n - 1)) - 1)) & ((1 << (n - 2)) - 1); + } + memset(d, 100, sizeof(d)); + for (i = 0; i <= m; ++i) + { + if (!(a[i + 1][n - 1] || a[i + 1][m + 1])) + { + d[i][m] = 1; + } + if (!(a[n - 1][i + 1] || a[m + 1][i + 1])) + { + d[m][i] = 1; + } + if (!(a[i + 1][0] || a[i + 1][1])) + { + d[i][0] = 1; + } + if (!(a[0][i + 1] || a[1][i + 1])) + { + d[0][i] = 1; + } + p[i] = 1 << (m - i); + } + bool flag = 1; + while (flag) + { + flag = 0; + for (i = 0; i <= m; ++i) + for (j = 0; j <= m; ++j) + if (!a[i + 1][j + 1]) + { + if (j > 0 && d[i][j] > d[i][j - 1] + 1) + { + d[i][j] = d[i][j - 1] + 1, flag = 1; + } + if (j < m && d[i][j] > d[i][j + 1] + 1) + { + d[i][j] = d[i][j + 1] + 1, flag = 1; + } + if (i > 0 && d[i][j] > d[i - 1][j] + 1) + { + d[i][j] = d[i - 1][j] + 1, flag = 1; + } + if (i < m && d[i][j] > d[i + 1][j] + 1) + { + d[i][j] = d[i + 1][j] + 1, flag = 1; + } + } + } + memset(id, 1, sizeof(id)); +} +inline void cpy(int *p, int *q, int h) +{ + for (int i = 0; i < h; ++i, ++p, ++q) + { + *p = *q; + } +} +inline void walk(int d) +{ + if (d == 0)//north + { + s[0] &= mapl[0]; + for (int i = 1, g; i <= m; ++i) + if (s[i]) + { + g = s[i], s[i] &= mapl[i], s[i - 1] |= (g ^ s[i]); + } + } + else if (d == 2)//south + { + s[m] &= mapl[m + 2]; + for (int i = m - 1, g; i >= 0; --i) + if (s[i]) + { + g = s[i], s[i] &= mapl[i + 2], s[i + 1] |= (g ^ s[i]); + } + } + else if (d == 3)//west + { + for (int i = 0, g; i <= m; ++i) + if (s[i]) + { + s[i] <<= 1; + g = s[i] & mapl[i + 1]; + s[i] ^= g; + s[i] |= (g >> 1); + s[i] &= ((1 << (m + 1)) - 1); + } + } + else//east + { + for (int i = 0, g; i <= m; ++i) + if (s[i]) + { + g = s[i] & mapr[i + 1]; + s[i] ^= g; + s[i] |= (g << 1); + s[i] >>= 1; + } + } +} +inline void out(int x) +{ + if (x == 0) + { + putchar('n'), putchar('o'), putchar('r'), putchar('t'), putchar('h'); + } + else if (x == 2) + { + putchar('s'), putchar('o'), putchar('u'), putchar('t'), putchar('h'); + } + else if (x == 3) + { + putchar('w'), putchar('e'), putchar('s'), putchar('t'); + } + else + { + putchar('e'), putchar('a'), putchar('s'), putchar('t'); + } + putchar('\n'); +} +inline void dfs(int step) +{ + bool ok = 1; + for (int i = 0; i <= m; ++i) + for (int j = 0; j <= m; ++j) + if (s[i] & p[j]) + { + if (step + d[i][j] >= ans) + { + return; + } + ok = 0; + } + if (step >= ans) + { + return; + } + if (ok) + { + ans = step; + cpy(dir, c, ans); + return; + } + int w[6]; + cpy(w, s, m + 1); + for (int i = 0, j; i < 4; ++i) + { + walk(i); + if (id[j = BKDRHash()]) + { + id[j] = 0, c[step] = i, dfs(step + 1), id[j] = 1; + } + cpy(s, w, m + 1); + } +} +int T; +int main() +{ + scanf("%d", &T); + while (T--) + { + init(); + ans = (n - 2) * (n - 2) + 1; + dfs(0); + for (int i = 0; i < ans; ++i) + { + out(dir[i]); + } + if (T > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/852.cpp b/852.cpp new file mode 100644 index 0000000..10951d5 --- /dev/null +++ b/852.cpp @@ -0,0 +1,199 @@ +#include + +using namespace std; + +char board[9][9]; + +void fill(int x, int y, char c) +{ + board[x][y] = c; + if (x > 0 && board[x - 1][y] == '.') + { + fill(x - 1, y, c); + } + if (y > 0 && board[x][y - 1] == '.') + { + fill(x, y - 1, c); + } + if (x + 1 < 9 && board[x + 1][y] == '.') + { + fill(x + 1, y, c); + } + if (y + 1 < 9 && board[x][y + 1] == '.') + { + fill(x, y + 1, c); + } +} + +int try_and_fill(int x, int y) +{ + char c = ' '; + bool visited[9][9]; + pair tmp; + queue> q; + for (int i = 0; i < 9; i++) + for (int j = 0; j < 9; j++) + { + visited[i][j] = false; + } + tmp.first = x; + tmp.second = y; + q.push(tmp); + visited[x][y] = true; + while (!q.empty()) + { + tmp = q.front(); + q.pop(); + if (tmp.first > 0 && !visited[tmp.first - 1][tmp.second]) + { + if (board[tmp.first - 1][tmp.second] == '.') + { + visited[tmp.first - 1][tmp.second] = true; + q.push(make_pair(tmp.first - 1, tmp.second)); + } + else + { + if (c == ' ') + { + c = board[tmp.first - 1][tmp.second]; + } + else + { + if (c != board[tmp.first - 1][tmp.second]) + { + return -1; + } + } + } + } + if (tmp.second > 0 && !visited[tmp.first][tmp.second - 1]) + { + if (board[tmp.first][tmp.second - 1] == '.') + { + visited[tmp.first][tmp.second - 1] = true; + q.push(make_pair(tmp.first, tmp.second - 1)); + } + else + { + if (c == ' ') + { + c = board[tmp.first][tmp.second - 1]; + } + else if (c != board[tmp.first][tmp.second - 1]) + { + return -1; + } + } + } + if (tmp.first + 1 < 9 && !visited[tmp.first + 1][tmp.second]) + { + if (board[tmp.first + 1][tmp.second] == '.') + { + visited[tmp.first + 1][tmp.second] = true; + q.push(make_pair(tmp.first + 1, tmp.second)); + } + else + { + if (c == ' ') + { + c = board[tmp.first + 1][tmp.second]; + } + else if (c != board[tmp.first + 1][tmp.second]) + { + return -1; + } + } + } + if (tmp.second + 1 < 9 && !visited[tmp.first][tmp.second + 1]) + { + if (board[tmp.first][tmp.second + 1] == '.') + { + visited[tmp.first][tmp.second + 1] = true; + q.push(make_pair(tmp.first, tmp.second + 1)); + } + else + { + if (c == ' ') + { + c = board[tmp.first][tmp.second + 1]; + } + else if (c != board[tmp.first][tmp.second + 1]) + { + return -1; + } + } + } + } + switch (c) + { + case ' ': + return -1; + break; + case 'X': + return 0; + break; + case 'O': + return 1; + break; + } +} + +int main() +{ + int cases; + string input; + cin >> cases; + cin.ignore(100, '\n'); + while (cases--) + { + for (int i = 0; i < 9; i++) + { + getline(cin, input); + for (int j = 0; j < 9; j++) + { + board[i][j] = input[j]; + } + } + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (board[i][j] != '.') + { + continue; + } + int result = try_and_fill(i, j);// -1: can't, 0 - X, 1 - O + switch (result) + { + case -1: + fill(i, j, '*'); + break; + case 0: + fill(i, j, 'X'); + break; + case 1: + fill(i, j, 'O'); + break; + } + } + } + int whites = 0; + int blacks = 0; + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (board[i][j] == 'X') + { + blacks++; + } + else if (board[i][j] == 'O') + { + whites++; + } + } + } + cout << "Black " << blacks << " White " << whites << endl; + } + return 0; +} diff --git a/855.cpp b/855.cpp index 35a52a5..0f1d5fa 100644 --- a/855.cpp +++ b/855.cpp @@ -1,32 +1,47 @@ -#include -#include -using namespace std; +#include -int S[50005]; -int A[50005]; +using namespace std; -int main() { - int T; - scanf("%d", &T); - while (T--) { - int F; - scanf("%*d%*d%d",&F); - for (int i = 0; i < F; i++) { - scanf("%d%d", &S[i], &A[i]); - } - sort(S, S + F); - sort(A, A + F); - int bestS = 0, bestA = 0; - - if (F & 1) { - bestS = S[F / 2]; - bestA = A[F / 2]; - } - else { - bestS = S[(F - 1) / 2]; - bestA = A[(F - 1) / 2]; +/* +855 +Lunch in the Grid city +*/ +#define MAXN 50002 +int row[MAXN], col[MAXN]; +int R, C, F; +int com(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +void Cal() +{ + int a, b; + if (F % 2) + { + a = F / 2; + printf("(Street: %d, Avenue: %d)\n", row[a], col[a]); + } + else + { + a = F / 2; + b = a - 1; + printf("(Street: %d, Avenue: %d)\n", row[b], col[b]); + } +} +int main() +{ + int k, i; + scanf("%d", &k); + while (k--) + { + scanf("%d%d%d", &R, &C, &F); + for (i = 0; i < F; i++) + { + scanf("%d%d", &row[i], &col[i]); } - printf("(Street: %d, Avenue: %d)\n", bestS, bestA); + qsort(row, F, sizeof(int), com); + qsort(col, F, sizeof(int), com); + Cal(); } return 0; } diff --git a/856.cpp b/856.cpp new file mode 100644 index 0000000..c61a9cb --- /dev/null +++ b/856.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std; + +char decode_vigenere(char decoded, char encoded) +{ + int a = 'z' - decoded; + int b = encoded - 'A'; + return char('A' + (b + a) % 26); +} + +int main() +{ + bool first = true; + string input; + string nums[10] = {"nine", "eight", "seven", "six", "five", "four", "three", "two", "one", "zero"}; + vector numbers; + for (int i = 0; i < 10; i++) + for (int j = 0; j < 10; j++) + for (int k = 0; k < 10; k++) + { + numbers.push_back(nums[i] + nums[j] + nums[k]); + } + while (getline(cin, input)) + { + if (!first) + { + cout << endl; + } + first = false; + for (int i = 0, sz = numbers.size(); i < sz; i++) + { + if (numbers[i].size() != input.size()) + { + continue; + } + for (int j = 0; j < numbers[i].size(); j++) + { + cout << decode_vigenere(numbers[i][j], input[j]); + } + cout << " -> " << numbers[i] << endl; + } + } + return 0; +} diff --git a/857.cpp b/857.cpp new file mode 100644 index 0000000..57bdf6e --- /dev/null +++ b/857.cpp @@ -0,0 +1,82 @@ +#include + +using namespace std; + +struct event +{ + int on, note, m, b, t; + event(int o, int n, int mx, int bx, int tx) + { + on = o; + note = n; + m = mx; + b = bx; + t = tx; + } +}; + +int main() +{ + int n; + + while (scanf("%d", &n), n > -1) + { + int on, note, m, b, t; + vector eventList; + + for (int i = 0; i < n; i++) + { + scanf("%d %d %d %d %d", &on, ¬e, &m, &b, &t); + + t = ((t + 30) / 60) * 60; + if (t == 480) + { + t = 0; + b++; + if (b == 5) + { + b = 1; + m++; + } + } + + //printf("%d %d %d %d %d\n",on,note,m,b,t); + event *e = new event(on, note, m, b, t); + eventList.push_back(*e); + } + + for (vector::iterator ei = eventList.begin(); + ei != eventList.end(); ei++) + { + if (!ei->on) + continue; + + for (vector::iterator ej = ei + 1; ej != eventList.end(); + ej++) + { + if ((ei->note == ej->note) && (ei->on && !ej->on)) + { + if (ei->m == ej->m && ei->b == ej->b && ei->t == ej->t) + { + ei = eventList.erase(ei); + ei--; + ej--; + ej = eventList.erase(ej); + ej--; + } + break; + } + } + } + + printf("%d\n", eventList.size()); + for (vector::iterator it = eventList.begin(); + it != eventList.end(); it++) + { + printf("%d %d %d %d %d\n", it->on, it->note, it->m, it->b, it->t); + } + } + printf("-1\n"); + + return 0; +} diff --git a/858.cpp b/858.cpp new file mode 100644 index 0000000..cd0d074 --- /dev/null +++ b/858.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int X[10005], Y[10005]; +double I[10005]; +int cmp(const void *a, const void *b) +{ + if (*(double *)a < *(double *)b) + { + return -1; + } + if (*(double *)a > *(double *)b) + { + return 1; + } + return 0; +} +int main() +{ + int t, chx, th, i, n, m; + double dist; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%d%d", X + i, Y + i); + } + X[n] = X[0], Y[n] = Y[0]; + scanf("%d%d", &th, &chx); + for (i = m = 0; i < n; i++) + { + if ((X[i] - chx) * (X[i + 1] - chx) >= 0) + { + continue; + } + I[m++] = Y[i] + (double)(Y[i + 1] - Y[i]) / (X[i + 1] - X[i]) * (chx - X[i]); + } + qsort(I, m, sizeof(double), cmp); + for (i = 0, dist = 0.0; i < m; i += 2) + { + dist += I[i + 1] - I[i]; + } + if (dist > th) + { + printf("YES\n"); + } + else + { + printf("NO\n"); + } + } + return 0; +} diff --git a/859.cpp b/859.cpp new file mode 100644 index 0000000..b9f4daa --- /dev/null +++ b/859.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +int w, h; +bool board[64][64]; + +struct StepStruct +{ + int x, y, step; + StepStruct(int a, int b, int c) + { + x = a; + y = b; + step = c; + } + bool operator<(const StepStruct &t) const + { + if (y != t.y) + { + return y > t.y; + } + return x < t.x; + } +}; + +int main() +{ + int c = 0; + int dx[] = {1 /*,-1,1*/, -1, 0 /*,0*/, 1, -1}; + int dy[] = {1 /*,-1,-1*/, 1, 1 /*,-1*/, 0, 0}; + while (scanf("%d %d", &h, &w) == 2) + { + assert(h > 0 && h <= 62 && w > 0 && w <= 62); + memset(board, 0, 64 * 64); + for (int i = 4 * w; --i >= 0;) + { + int x, y; + scanf("%d %d", &y, &x); + board[y][x] = true; + } + int ax, ay; + scanf("%d %d", &ay, &ax); + vector ret; + //one step + if (ax > 1 && !board[ay][ax - 1]) + { + ret.push_back(StepStruct(ax - 1, ay, 1)); + } + if (ax < w && !board[ay][ax + 1]) + { + ret.push_back(StepStruct(ax + 1, ay, 1)); + } + //if (ay>1 && !board[ay-1][ax]) ret.push_back(StepStruct(ax,ay-1,1)); + if (ay < h && !board[ay + 1][ax]) + { + ret.push_back(StepStruct(ax, ay + 1, 1)); + } + //jump! BFS + bool visited[64][64]; + memset(visited, 0, 64 * 64); + queue q; + q.push(StepStruct(ax, ay, 0)); + visited[ay][ax] = true; + while (!q.empty()) + { + StepStruct now = q.front(); + q.pop(); + for (int i = 5; --i >= 0;) + { + StepStruct tmp = now; + tmp.x += dx[i]; + tmp.y += dy[i]; + if (tmp.y < 1 || tmp.y > h || tmp.x < 1 || tmp.x > w || !board[tmp.y][tmp.x])//must have chess + { + continue; + } + tmp.x += dx[i]; + tmp.y += dy[i]; + if (tmp.y < 1 || tmp.y > h || tmp.x < 1 || tmp.x > w || board[tmp.y][tmp.x] || visited[tmp.y][tmp.x])//no chess + { + continue; + } + visited[tmp.y][tmp.x] = true; + tmp.step++; + q.push(tmp); + ret.push_back(tmp); + } + } + sort(ret.begin(), ret.end()); + //output + if (c++) + { + putchar('\n'); + } + for (int i = 0; i < ret.size(); i++) + { + printf("%d %d %d\n", ret[i].y, ret[i].x, ret[i].step); + } + } +} diff --git a/860.cpp b/860.cpp new file mode 100644 index 0000000..fa97b73 --- /dev/null +++ b/860.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +const char *Delim(" ,.:;!?\"()\n\t"), *EndText("****END_OF_TEXT****"), *EndInput("****END_OF_INPUT****"); +char buf[25], *p; + +int main() +{ + double Et, Emax, Erel, log10c; + int c; + map m; + while (cin >> buf, strcmp(buf, EndInput)) + { + c = 0; + m.clear(); + do + { + for (int i = 0; buf[i] != '\0'; i++) + { + buf[i] = tolower(buf[i]); + } + p = strtok(buf, Delim); + while (p != NULL) + { + m[string(p)]++; + c++; + p = strtok(NULL, Delim); + } + } while (cin >> buf, strcmp(buf, EndText)); + Et = Emax = Erel = 0; + log10c = log10(c); + for (map::iterator it = m.begin(); it != m.end(); it++) + { + Et += (it->second * (log10c - log10(it->second))); + } + Et /= c; + printf("%d %.1lf %.0lf\n", c, Et, 100. * Et / log10c); + } + return 0; +} diff --git a/861.cpp b/861.cpp new file mode 100644 index 0000000..2cf0957 --- /dev/null +++ b/861.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +long long bishops(int n, int k) +{ + long long r1[9], r2[9]; + for (int i = 1; i <= n; ++i) + r1[i] = i % 2 != 0 ? i : r1[i - 1]; + for (int i = 1; i <= n - 1; ++i) + r2[i] = i % 2 != 0 ? i + 1 : r2[i - 1]; + + long long c1[9][65] = {{0}}, c2[9][65] = {{0}}; + for (int i = 0; i <= n; ++i) + c1[i][0] = 1; + for (int j = 1; j <= k; ++j) + c1[0][j] = 0; + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= k && j <= i; ++j) + c1[i][j] = c1[i - 1][j] + c1[i - 1][j - 1] * (r1[i] - j + 1); + for (int i = 0; i <= n - 1; ++i) + c2[i][0] = 1; + for (int j = 1; j <= k; ++j) + c2[0][j] = 0; + for (int i = 1; i <= n - 1; ++i) + for (int j = 1; j <= k && j <= i; ++j) + c2[i][j] = c2[i - 1][j] + c2[i - 1][j - 1] * (r2[i] - j + 1); + + long long r = 0; + for (int i = 0; i <= k; ++i) + r += c1[n][i] * c2[n - 1][k - i]; + return r; +} + +int main(int argc, char *argv[]) +{ + for (int n, k; cin >> n >> k && !(n == 0 && k == 0);) + cout << bishops(n, k) << endl; + return 0; +} diff --git a/862.cpp b/862.cpp new file mode 100644 index 0000000..975f02e --- /dev/null +++ b/862.cpp @@ -0,0 +1,213 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +typedef complex P; + +const double eps = 1e-8; +const int RIGHT = 1, LEFT = -1; + +double cross(P a, P b) +{ + return (a.real() * b.imag() - a.imag() * b.real()); +} + +double dot(P a, P b) +{ + return (a.real() * b.real() + a.imag() * b.imag()); +} + +bool isp(P a, P b, P c) +{ + return (abs(a - c) + abs(b - c) < abs(a - b) + eps); +} + +double distance_l_p(P a, P b, P c) +{ + return abs(cross(b - a, c - a)) / abs(b - a); +} + +double distance_ls_p(P a, P b, P c) +{ + if (dot(b - a, c - a) < 0) + { + return abs(c - a); + } + if (dot(a - b, c - b) < 0) + { + return abs(c - b); + } + return abs(cross(b - a, c - a)) / abs(b - a); +} + +P intersection_ll(P a1, P a2, P b1, P b2) +{ + P a = a2 - a1, b = b2 - b1; + return a1 + a * cross(b, b1 - a1) / cross(b, a); +} + +int ccw(P a, P b) +{ + if (cross(a, b) < eps) + { + return RIGHT; + } + else if (cross(a, b) > -eps) + { + return LEFT; + } + return 2; +} + +vector

ConvexCut(vector

&in, P a1, P a2) +{ + int n = in.size(); + vector

ret; + rep(i, n) + { + P now = in[i], nxt = in[(i + 1) % n]; + if (ccw(a1 - a2, now - a2) == RIGHT) + { + ret.push_back(now); + } + if (ccw(a1 - a2, now - a2) * ccw(a1 - a2, nxt - a2) < 0) + { + ret.push_back(intersection_ll(a1, a2, now, nxt)); + } + } + return ret; +} + +bool is_in(vector

&in, P a) +{ + int n = in.size(); + int cnt = 0; + rep(i, n) + { + P cur = in[i] - a, nxt = in[(i + 1) % n] - a; + if (cur.imag() > nxt.imag()) + { + swap(cur, nxt); + } + if (cur.imag() < 0 && 0 <= nxt.imag() && + cross(nxt, cur) >= 0) + { + cnt++; + } + if (isp(in[i], in[(i + 1) % n], a)) + { + return false; + } + } + if (cnt % 2 == 1) + { + return true; + } + else + { + return false; + } +} + +P symmetricPoint(P o, P p, P q) +{ + P N = p - o, L = q - o; + double t = abs(L); + N /= abs(N); + L /= abs(L); + double prepL = dot(N, L);//theta element of vector L against N (|L|=|N|=1 + double d = 2 * prepL; + P r(d * N - L);//R vector symmetric point of L + return o + t * r; +} + +int main() +{ + int T; + scanf("%d", &T); + rep(t, T) + { + if (t) + { + printf("\n"); + } + vector> all; + vector

a; + a.push_back(P(0, 0)); + a.push_back(P(100, 0)); + a.push_back(P(100, 100)); + a.push_back(P(0, 100)); + all.push_back(a); + int step; + scanf("%d", &step); + rep(loop, step) + { + double re, im; + scanf("%lf%lf", &re,&im); + P p(re,im); + scanf("%lf%lf", &re,&im); + P q(re,im); + vector> nxt; + rep(i, all.size()) + { + vector

tmp; + tmp = ConvexCut(all[i], p, q); + if (tmp.size() > 0) + { + nxt.push_back(tmp); + } + tmp = ConvexCut(all[i], q, p); + rep(k, tmp.size()) + { + if (distance_l_p(p, q, tmp[k]) < eps) + { + continue; + } + P tmp2 = symmetricPoint(p, q, tmp[k]); + if (isnan(tmp2.real()) || isnan(tmp2.imag())) + { + assert(false); + } + tmp[k] = tmp2; + } + if (tmp.size() > 0) + { + nxt.push_back(tmp); + } + } + all = nxt; + } + + int q; + scanf("%d", &q); + rep(i, q) + { + double re, im; + scanf("%lf%lf", &re,&im); + P t(re,im); + int cnt = 0; + rep(j, all.size()) + { + int n = all[j].size(); + bool isok = true; + rep(k, all[j].size()) + { + if (distance_l_p(all[j][k], all[j][(k + 1) % n], t) <= 0.000001) + { + isok = false; + break; + } + } + if (isok && is_in(all[j], t)) + { + cnt++; + } + } + printf("%d\n", cnt); + } + } + return 0; +} diff --git a/864.cpp b/864.cpp new file mode 100644 index 0000000..f096d87 --- /dev/null +++ b/864.cpp @@ -0,0 +1,301 @@ +#include + +using namespace std; + +typedef struct +{ + int car, cdr; + char *lit; +} obj_t; + +#define CAR(x) (tab[x].car) +#define CDR(x) (tab[x].cdr) +#define LIT(x) (tab[x].lit) + +static obj_t *tab; +static int tab_size, tab_used, freelist; + +static void clean() +{ + int i; + CAR(0) = CDR(0) = 0; + LIT(0) = NULL; + for (freelist = 0, tab_used = 1, i = tab_size - 1; i > 0; i--) + { + if (LIT(i)) + { + free(LIT(i)); + LIT(i) = NULL; + } + CDR(i) = freelist; + freelist = i; + } +} + +static void init() +{ + tab_size = 2048; + tab = (obj_t *)malloc(tab_size * sizeof(obj_t)); + memset(tab, '\0', tab_size * sizeof(obj_t)); + clean(); +} + +static int cell(int a, int b, char *s) +{ + register int i; + if (freelist == 0) + { + int m = tab_size; + tab_size *= 2; + tab = (obj_t *)realloc(tab, tab_size * sizeof(obj_t)); + for (i = tab_size - 1; i >= m; i--) + { + LIT(i) = NULL; + CDR(i) = freelist; + freelist = i; + } + } + i = freelist; + freelist = CDR(freelist); + CAR(i) = a; + CDR(i) = b; + if (s == NULL) + LIT(i) = NULL; + else + { + LIT(i) = (char *)malloc(strlen(s) + 1); + strcpy(LIT(i), s); + } + tab_used++; + return i; +} + +static int sread() +{ + static char buf[4096]; + int k, c; + for (;;) + { + c = getchar(); + if (c == EOF) + return 0; + if (isspace(c)) + continue; + if (c == ')') + return -1; + if (c == '#') + { + buf[0] = '#'; + for (k = 1; (c = getchar()) != EOF && !isspace(c);) + buf[k++] = c; + if (c != EOF) + ungetc(c, stdin); + buf[k] = '\0'; + return cell(0, 0, buf); + } + if (c == '(') + { + int head, tail; + if ((k = sread()) == -1) + return cell(0, 0, NULL);// ( ) + head = tail = cell(k, 0, NULL); + while ((k = sread()) != -1) + { + k = cell(k, 0, NULL); + // tab pointer may be changed now + CDR(tail) = k; + tail = k; + } + return head; + } + if (c == '\"') + { + buf[0] = '\"'; + for (k = 1; (c = getchar()) != EOF;) + { + buf[k++] = c; + if (c == '\\') + buf[k++] = getchar(); + else if (c == '\"') + break; + } + buf[k] = '\0'; + return cell(0, 0, buf); + } + buf[0] = c; + for (k = 1; (c = getchar()) != EOF;) + { + if (isspace(c) || c == '\"' || c == '(' || c == ')' || + c == '\\' || c == '#') + break; + buf[k++] = c; + } + if (c != EOF) + ungetc(c, stdin); + buf[k] = '\0'; + return cell(0, 0, buf); + } +} + +static int col; + +#define INDENT(n) \ + { \ + int t; \ + t = (n); \ + col += t; \ + ; \ + while (t-- > 0) \ + putchar(' '); \ + } +#define NLINDENT(n) \ + { \ + putchar('\n'); \ + col = 0; \ + INDENT(n); \ + } +#define WRITE(s) \ + { \ + char *p = (s); \ + while (*p) \ + { \ + putchar(*p++); \ + col++; \ + } \ + } +#define WRITEC(c) \ + { \ + putchar(c); \ + col++; \ + } + +static void pretty(int x, int ind) +{ + char *s; + int k, t; + + if (x == -1) + { + WRITEC(')'); + return; + } + + if (LIT(x) != NULL) + { + // not a cons + WRITE(LIT(x)); + return; + } + + if (x == 0 || (CAR(x) == 0 && CDR(x) == 0)) + { + WRITE("()"); + return; + } + + if (LIT(CAR(x)) == NULL) + { + // ( (head) ... ) + WRITEC('('); + pretty(CAR(x), ind + 1); + x = CDR(x); + if (x == 0) + { + WRITEC(')'); + return; + } + WRITEC(' '); + k = col; + while (x != 0) + { + pretty(CAR(x), k); + if ((x = CDR(x)) != 0) + { + NLINDENT(k); + } + } + WRITEC(')'); + return; + } + + if (CDR(x) == 0) + { + // (id) + WRITEC('('); + WRITE(LIT(CAR(x))); + WRITEC(')'); + return; + } + s = LIT(CAR(x)); + + // choose indentation offset + if (strcmp(s, "define") == 0) + { + if (LIT(CAR(CDR(CDR(x)))) != NULL) + t = 0; + else + t = 3; + } + else if (strcmp(s, "lambda") == 0) + t = 3; + else if (strcmp(s, "if") == 0) + t = 4; + else + { + for (t = 0, k = x; k != 0; k = CDR(k)) + { + if (LIT(CAR(k)) == NULL) + { + t = strlen(s) + 2; + break; + } + } + } + + // t == 0: put everything on the same line + if (t == 0) + { + WRITEC('('); + while (x != 0) + { + WRITE(LIT(CAR(x))); + if ((x = CDR(x)) != 0) + { + WRITEC(' '); + } + } + WRITEC(')'); + return; + } + WRITEC('('); + WRITE(s); + WRITEC(' '); + x = CDR(x); + k = col; + ind += t; + for (k = 0; x != 0; k++) + { + if (k == 0) + pretty(CAR(x), col); + else + pretty(CAR(x), ind); + if ((x = CDR(x)) != 0) + NLINDENT(ind); + } + WRITEC(')'); +} + +int main() +{ + int x; + init(); + while ((x = sread()) != 0) + { + col = 0; + pretty(x, 0); + putchar('\n'); + if (tab_used > 256) + clean(); + } + return 0; +} diff --git a/865.cpp b/865.cpp new file mode 100644 index 0000000..dc9ee01 --- /dev/null +++ b/865.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int main() +{ + int cases; + string input; + string plaintext, substitute; + map cypher; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + cypher.clear(); + getline(cin, plaintext); + getline(cin, substitute); + for (int i = 0, sz = plaintext.size(); i < sz; i++) + { + cypher[plaintext[i]] = substitute[i]; + } + cout << substitute << endl; + cout << plaintext << endl; + while (getline(cin, input)) + { + if (input == "") + { + break; + } + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (cypher.find(input[i]) == cypher.end()) + { + cout << input[i]; + } + else + { + cout << cypher[input[i]]; + } + } + cout << endl; + } + if (cases) + { + cout << endl; + } + } +} diff --git a/866.cpp b/866.cpp new file mode 100644 index 0000000..37a1f5a --- /dev/null +++ b/866.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define N 105 + +int px1[N], px2[N], py1[N], py2[N]; + +int diff(int p, int q) +{ + return (p > 0 && q < 0) || (p < 0 && q > 0); +} + +int cross(int x1, int y1, int x2, int y2) +{ + return x1 * y2 - x2 * y1; +} + +int intersect(int p, int q) +{ + int vx1, vx2, vx3, vx4, vx5, vx6, vy1, vy2, vy3, vy4, vy5, vy6; + vx1 = px2[p] - px1[p]; + vy1 = py2[p] - py1[p]; + vx2 = px1[q] - px1[p]; + vy2 = py1[q] - py1[p]; + vx3 = px2[q] - px1[p]; + vy3 = py2[q] - py1[p]; + vx4 = px2[q] - px1[q]; + vy4 = py2[q] - py1[q]; + vx5 = px1[p] - px1[q]; + vy5 = py1[p] - py1[q]; + vx6 = px2[p] - px1[q]; + vy6 = py2[p] - py1[q]; + return diff(cross(vx1, vy1, vx2, vy2), cross(vx1, vy1, vx3, vy3)) && diff(cross(vx4, vy4, vx5, vy5), cross(vx4, vy4, vx6, vy6)); +} + +int main() +{ + int count, n, i, j, ans; + scanf("%d", &count); + while (count--) + { + scanf("%d", &n); + for (i = 0, ans = 0; i < n; i++) + { + scanf("%d%d%d%d", &px1[i], &py1[i], &px2[i], &py2[i]); + ans++; + for (j = 0; j < i; j++) + { + if (intersect(i, j)) + { + ans += 2; + } + } + } + printf("%d\n", ans); + if (count) + { + printf("\n"); + } + } + return 0; +} diff --git a/868.cpp b/868.cpp new file mode 100644 index 0000000..4a92c67 --- /dev/null +++ b/868.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +//bool v[500][500]; +int s[500][500]; +int n, m, col; +bool flag; +void DFS(int r, int c, int len, int cycle) +{ + if (r == n - 1 && len == cycle) + { + if (!flag) + { + col = c; + } + else if (col > c) + { + col = c; + } + flag = true; + } + int nextlen = len, nextcycle = cycle + 1; + if (cycle == len) + { + nextcycle = 1; + nextlen++; + } + if (r - 1 >= 0 && s[r - 1][c] == nextcycle) + { + DFS(r - 1, c, nextlen, nextcycle); + } + if (r + 1 < n && s[r + 1][c] == nextcycle) + { + DFS(r + 1, c, nextlen, nextcycle); + } + if (c - 1 >= 0 && s[r][c - 1] == nextcycle) + { + DFS(r, c - 1, nextlen, nextcycle); + } + if (c + 1 < m && s[r][c + 1] == nextcycle) + { + DFS(r, c + 1, nextlen, nextcycle); + } +} +int main() +{ + int t; + scanf("%d", &t); + for (int x = 0; x < t; x++) + { + if (x) + { + puts(""); + } + scanf("%d %d", &n, &m); + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + { + scanf("%d", &s[i][j]); + } + for (int i = 0; i < m; i++) + if (s[0][i] == 1) + { + flag = false; + DFS(0, i, 1, 1); + if (flag) + { + printf("%d %d\n", 1, i + 1); + printf("%d %d\n", n, col + 1); + break; + } + } + } +} diff --git a/869.cpp b/869.cpp index 43ede8b..2537831 100644 --- a/869.cpp +++ b/869.cpp @@ -1,48 +1,151 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; +#define MAXN 302 -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define PI acos(-1.0) -#define INF 1<<30 -#define EPS 1e-9 -#define sqr(x) (x)*(x) +int R[MAXN], P[MAXN], r[MAXN], p[MAXN]; +struct ss +{ + int u, v; +} edge[100000]; +void Ini() +{ + int i; + for (i = 0; i < 300; i++) + { + p[i] = P[i] = i; + r[i] = R[i] = 0; + } +} +int FindSet(int s) +{ + if (s != P[s]) + { + P[s] = FindSet(P[s]); + } + return P[s]; +} +int Find(int s)// 2nd +{ + if (s != p[s]) + { + p[s] = Find(p[s]); + } + return p[s]; +} +void Link(int x, int y) +{ + if (R[x] > R[y]) + { + P[y] = x; + } + else + { + P[x] = y; + if (R[x] == R[y]) + { + R[y]++; + } + } +} +void Link1(int x, int y)// 2nd +{ + if (r[x] > r[y]) + { + p[y] = x; + } + else + { + p[x] = y; + if (r[x] == r[y]) + { + r[y]++; + } + } +} +void MakeLink(int x, int y) +{ + int u, v; + u = FindSet(x); + v = FindSet(y); + if (u != v) + { + Link(u, v); + } +} +void Make(int x, int y)// 2nd +{ + int u, v; + u = Find(x); + v = Find(y); + if (u != v) + { + Link1(u, v); + } +} +void Final(int n) +{ + int i, u, v, nott = 0; + for (i = 0; i < n; i++) + { + u = Find(edge[i].u); + v = Find(edge[i].v); + if (u != v) + { + nott = 1; + break; + } + } + printf(nott ? "NO\n" : "YES\n"); +} +void Cal(int m, int n) +{ + int i, u, v, nott = 0; + char temp[10], dum[10]; + for (i = 0; i < m; i++) + { + scanf("%s%s", temp, dum); + if (nott) + continue; + u = temp[0]; + v = dum[0]; + u = FindSet(u); + v = FindSet(v); + if (u != v) + nott = 1; + Make(temp[0], dum[0]); + } + if (nott) + { + printf("NO\n"); + return; + } + Final(n); +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, M; - while (cin >> N){ - int A[], B[]53 +int main() +{ + int kases, i, n, m, u, v; + char temp[10], dum[10]; + scanf("%d", &kases); + while (kases--) + { + Ini(); + scanf("%d", &n); + for (i = 0; i < n; i++) + { + scanf("%s%s", temp, dum); + u = temp[0]; + v = dum[0]; + edge[i].u = u; + edge[i].v = v; + MakeLink(u, v); + } + scanf("%d", &m); + Cal(m, n); + if (kases) + printf("\n"); } return 0; } - diff --git a/870.cpp b/870.cpp new file mode 100644 index 0000000..573c126 --- /dev/null +++ b/870.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +struct Rect +{ + int x1, y1, x2, y2; + + Rect(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) : x1(x1), y1(y1), x2(x2), y2(y2) {} + + friend istream &operator>>(istream &in, Rect &r) + { + return in >> r.x1 >> r.y1 >> r.x2 >> r.y2; + } + + friend bool intersect(const Rect &r1, const Rect &r2) + { + if (r1.x1 > r2.x2 || r2.x1 > r1.x2) + { + return false; + } + if (r1.y1 > r2.y2 || r2.y1 > r1.y2) + { + return false; + } + return true; + } +}; + +int N; +Rect rect[100]; +bool visit[100]; +vector R; + +void DFS(int now) +{ + if (visit[now]) + { + return; + } + visit[now] = true; + R.push_back(rect[now]); + for (int i = 0; i < N; ++i) + if (!visit[i]) + if (intersect(rect[now], rect[i])) + { + DFS(i); + } +} + +int discrete() +{ + int i, j, k; + vector X, Y; + for (i = 0; i < R.size(); ++i) + { + X.push_back(R[i].x1); + X.push_back(R[i].x2); + } + for (i = 0; i < R.size(); ++i) + { + Y.push_back(R[i].y1); + Y.push_back(R[i].y2); + } + sort(X.begin(), X.end()); + sort(Y.begin(), Y.end()); + int ans = 0; + for (i = 0; i < X.size() - 1; ++i) + for (j = 0; j < Y.size() - 1; ++j) + for (k = 0; k < R.size(); ++k) + if (R[k].x1 <= X[i] && R[k].x2 >= X[i + 1] && R[k].y1 <= Y[j] && R[k].y2 >= Y[j + 1]) + { + ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]); + break; + } + return ans; +} + +int main() +{ + int cases; + cin >> cases; + while (cases--) + { + int i; + for (i = 0; i < 100; ++i) + { + visit[i] = false; + } + R.clear(); + cin >> N; + for (i = 0; i < N; ++i) + { + cin >> rect[i]; + } + DFS(0); + cout << discrete() << endl; + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/871.cpp b/871.cpp new file mode 100644 index 0000000..2318366 --- /dev/null +++ b/871.cpp @@ -0,0 +1,93 @@ +#include + +using namespace std; + +#define MAXN 30 + +char MZ[MAXN][MAXN], input[100]; +int R, max_, C; + +int isEmpty() +{ + int i, k; + for (i = 0; input[i]; i++) + { + if (input[i] == '\n') + { + input[i] = NULL; + break; + } + } + k = strlen(input); + if (k > C) + C = k; + if (k == 0) + return 1; + return 0; +} +int sss; +void Fill(int r, int c) +{ + int i, j, nr, nc; + MZ[r][c] = '0'; + for (i = -1; i < 2; i++) + { + for (j = -1; j < 2; j++) + { + if (!i && !j) + continue; + nr = r + i; + nc = c + j; + if (nr >= R || nr < 0 || nc >= C || nc < 0) + continue; + if (MZ[nr][nc] == '1') + { + sss++; + Fill(nr, nc); + } + } + } +} +void Cal() +{ + int i, j; + max_ = 0; + for (i = 0; i < R; i++) + { + for (j = 0; j < C; j++) + { + if (MZ[i][j] == '1') + { + sss = 1; + Fill(i, j); + if (sss > max_) + max_ = sss; + } + } + } + printf("%d\n", max_); +} + +int main() +{ + int i, kase; + gets(input); + sscanf(input, "%d", &kase); + gets(input); + while (kase--) + { + R = C = 0; + while (gets(input)) + { + if (isEmpty()) + { + break; + } + strcpy(MZ[R++], input); + } + Cal(); + if (kase) + printf("\n"); + } + return 0; +} diff --git a/872.cpp b/872.cpp new file mode 100644 index 0000000..4b8fa06 --- /dev/null +++ b/872.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int n; +char var[20]; +multimap m; +char current[20]; +bool taken[20]; +bool found; + +bool fit(char c, int depth) +{ + multimap::iterator it; + pair::iterator, multimap::iterator> ret; + ret = m.equal_range(c); + for (it = ret.first; it != ret.second; it++) + { + for (int i = 0; i < depth; i++) + { + if (current[i] == it->second) + { + return false; + } + } + } + return true; +} + +void solve(int depth) +{ + if (depth == n) + { + for (int i = 0; i < n; i++) + { + if (i > 0) + { + cout << " "; + } + cout << current[i]; + } + cout << endl; + found = true; + return; + } + for (int i = 0; i < n; i++) + { + if (!fit(var[i], depth) || taken[i]) + { + continue; + } + taken[i] = true; + current[depth] = var[i]; + solve(depth + 1); + taken[i] = false; + } +} + +int main() +{ + char c, del, b; + int cases; + cin >> cases; + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + while (cases--) + { + n = 0; + fill(taken, taken + 20, false); + m.clear(); + found = false; + while (cin.peek() != '\n') + { + cin >> c; + var[n] = c; + n++; + } + cin.ignore(100, '\n'); + while (cin.peek() != '\n' && cin.peek() != -1) + { + cin >> c >> del >> b; + m.insert(make_pair(c, b)); + } + if (cin.peek() == '\n') + { + cin.ignore(100, '\n'); + cin.ignore(100, '\n'); + } + solve(0); + if (!found) + { + cout << "NO" << endl; + } + if (cases) + { + cout << endl; + } + } + return 0; +} diff --git a/873.cpp b/873.cpp new file mode 100644 index 0000000..93507f1 --- /dev/null +++ b/873.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() +{ + int T, t; + double PV, r, M, c; + scanf("%d", &T); + for (t = 0; t < T; t++) + { + if (t != 0) + { + printf("\n"); + } + scanf("%lf %lf %lf", &PV, &r, &M); + if (r == 0) + { + c = PV / M; + } + else + { + r /= 1200; + c = PV * r / (1 - pow((r + 1), -M)); + } + printf("%.2lf\n", c); + } + return 0; +} diff --git a/874.cpp b/874.cpp new file mode 100644 index 0000000..37a8d0a --- /dev/null +++ b/874.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +#define EMPTY -1 +#define FULL -2 + +char map_[256][256], flat[65536]; +int node[65536][4]; + +void decode(int n, int x, int y, int s) +{ + int i, j; + if (s == 1 && n >= 0) + { + n = node[n][0]; + } + if (n == EMPTY) + { + for (i = 0; i < s; i++) + for (j = 0; j < s; j++) + { + map_[y + i][x + j] = 0; + } + } + else if (n == FULL) + { + for (i = 0; i < s; i++) + for (j = 0; j < s; j++) + { + map_[y + i][x + j] = 1; + } + } + else + { + i = s >> 1; + decode(node[n][0], x, y, i); + decode(node[n][1], x + i, y, i); + decode(node[n][2], x, y + i, i); + decode(node[n][3], x + i, y + i, i); + } +} + +int get() +{ + int r, c; + while ((c = getchar()) != EOF) + { + if (c == 'E') + { + return EMPTY; + } + else if (c == 'F') + { + return FULL; + } + else if ('0' <= c && c <= '9') + { + for (r = 0; '0' <= c && c <= '9'; c = getchar()) + { + r = r * 10 + c - '0'; + } + if (c != EOF) + { + ungetc(c, stdin); + } + return r; + } + } + return EMPTY; +} + +int main() +{ + int i, j, k, n, m, t; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &n, &m) == 2;) + { + for (i = 1; i <= m; i++) + { + for (j = 0; j < 4; j++) + { + node[i][j] = get(); + } + } + decode(1, 0, 0, n); + for (i = 0, k = 0; i < n; i++) + for (j = 0; j < n; j++) + { + flat[k++] = map_[i][j]; + } + for (m = n * n, i = j = 0, k = 1; i < m; i++) + { + if (flat[i] == k) + { + j++; + } + else + { + printf("%d ", j); + k ^= 1; + j = 1; + } + } + printf("%d\n", j); + if (t > 0) + { + printf("\n"); + } + } + return 0; +} diff --git a/877.cpp b/877.cpp new file mode 100644 index 0000000..e01c443 --- /dev/null +++ b/877.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +struct Point +{ + double x, y; +} P[50], Q[50]; +double D; +int N; + +int main() +{ + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + scanf("%lf %d", &D, &N); + assert(3 <= N && N <= 8); + for (int i = 0; i < N; i++) + { + scanf("%lf %lf", &P[i].x, &P[i].y); + } + P[N] = P[0]; + P[N + 1] = P[1]; + for (int i = 0; i < N; i++) + { + double a1 = P[i + 1].y - P[i].y; + double b1 = P[i].x - P[i + 1].x; + double t = sqrt(a1 * a1 + b1 * b1); + a1 /= t; + b1 /= t; + double c1 = a1 * P[i].x + b1 * P[i].y - D; + double a2 = P[i + 2].y - P[i + 1].y; + double b2 = P[i + 1].x - P[i + 2].x; + t = sqrt(a2 * a2 + b2 * b2); + a2 /= t; + b2 /= t; + double c2 = a2 * P[i + 1].x + b2 * P[i + 1].y - D; + double det = a1 * b2 - a2 * b1; + Q[i].x = (c1 * b2 - c2 * b1) / det; + Q[i].y = (a1 * c2 - a2 * c1) / det; + } + printf("%d\n", N); + for (int i = 0; i < N; i++) + { + int j = (i + N - 1) % N; + if (fabs(Q[j].x) < 5e-4) + { + Q[j].x = 0; + } + if (fabs(Q[j].y) < 5e-4) + { + Q[j].y = 0; + } + printf("%.3lf %.3lf\n", Q[j].x, Q[j].y); + } + if (cs != T) + { + printf("\n"); + } + } +} diff --git a/878.cpp b/878.cpp new file mode 100644 index 0000000..4bde1de --- /dev/null +++ b/878.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +typedef long long i64; + +struct Point +{ + i64 x, y; + Point(i64 x, i64 y) : x(x), y(y) {} +}; + +bool operator<(const Point &a, const Point &b) +{ + return a.x < b.x || (a.x == b.x && a.y < b.y); +} +bool operator==(const Point &a, const Point &b) +{ + return a.x == b.x && a.y == b.y; +} + +Point rot(const Point &p) +{ + return Point(-p.y, p.x); +} + +int main() +{ + i64 x, y, t; + for (scanf("%lld", &t); t-- > 0;) + { + vector v; + v.clear(); + while (scanf(" %lld , %lld", &x, &y) == 2 && x >= 0 && y >= 0) + { + v.push_back(Point(x, y)); + } + vector> vv; + vv.clear(); + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < v.size(); j++) + { + v[j] = rot(v[j]); + if (j == 0) + { + x = v[j].x; + y = v[j].y; + } + x = MIN(x, v[j].x); + y = MIN(y, v[j].y); + } + for (int j = 0; j < v.size(); j++) + { + v[j].x -= x; + v[j].y -= y; + } + sort(v.begin(), v.end()); + vv.push_back(v); + } + x = 4; + for (int i = 0; i < 4; i++) + for (int j = 0; j < i; j++) + if (vv[j] == vv[i]) + { + x--; + break; + } + printf("%lld\n%s", x, t ? "\n" : ""); + } + return 0; +} diff --git a/879.cpp b/879.cpp new file mode 100644 index 0000000..f8daac2 --- /dev/null +++ b/879.cpp @@ -0,0 +1,109 @@ +#include + +using namespace std; + +#define MAXN 1000000 + +int N; +char str[MAXN], visit[MAXN]; +vector V[100000]; +queue Q; + +void MakeLink() +{ + char *p; + int u, v; + p = strtok(str, " "); + while (p) + { + u = atoi(p); + p = strtok(NULL, " "); + v = atoi(p); + V[u].push_back(v); + V[v].push_back(u); + p = strtok(NULL, " "); + } +} +void BFS(int s) +{ + int i, u, v; + Q.push(s); + visit[s] = 1; + while (!Q.empty()) + { + u = Q.front(); + Q.pop(); + for (i = 0; i < V[u].size(); i++) + { + v = V[u][i]; + if (visit[v] == 0) + { + visit[v] = 1; + Q.push(v); + } + } + } +} +void Cal() +{ + int i, c = 0; + for (i = 1; i <= N; i++) + { + if (visit[i] == 0) + { + c++; + BFS(i); + } + } + cout << c << endl; +} +int EEmpty() +{ + int i; + for (i = 0; str[i]; i++) + { + if (str[i] == '\n') + { + str[i] = '\0'; + break; + } + } + return strlen(str) == 0; +} +void Free() +{ + int i; + for (i = 1; i <= N; i++) + { + V[i].clear(); + visit[i] = 0; + } +} + +int main() +{ + int ks; + gets(str); + ks = atoi(str); + gets(str); + while (ks--) + { + gets(str); + N = atoi(str); + while (gets(str)) + { + if (EEmpty()) + { + break; + } + MakeLink(); + } + Cal(); + if (ks) + { + cout << endl; + Free(); + } + } + return 0; +} diff --git a/880.cpp b/880.cpp index c6e0440..e0f1aeb 100644 --- a/880.cpp +++ b/880.cpp @@ -1,82 +1,28 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -#define FOI(i, A, B) for(i=A; i<=B; i++) -#define FOD(i, A, B) for(i=A; i>=B; i--) -#define REP(i, N) for(i=1; i<=N; i++) - -#define LIM 100000000 -struct Cantor{ - int num; - int den; - /* - Cantor(int n, int d){ - num = n; - den = d; - } - */ - -}; - -Cantor can[LIM+1]; - -int main(){ - int i, SZ=0; - bool flag = true; - for(i=1; ; i++){ - int nu = i; - int de = 1; - while(true){ - Cantor c; - c.num = nu; - c.den = de; - can[SZ] = c; - SZ++; - if(SZ == LIM){ - flag = false; - break; - } - if(nu == 1) - break; - nu--; - de++; - - } - if(!flag) - break; - } - //cout<>ind){ - cout< + +using namespace std; + +typedef long long xx; +xx Sum(xx n) +{ + xx m; + m = n + 1; + m = (n * m) / 2; + return m; +} +int main() +{ + xx n, m, k, diff; + while (scanf("%lld", &n) == 1) + { + m = sqrtl(n * 2); + k = (m * (m + 1)) / 2; + if (k < n) + { + m++; + k += m; + } + printf("%lld/%lld\n", k - n + 1, m - (k - n)); + } + return 0; +} diff --git a/881.cpp b/881.cpp new file mode 100644 index 0000000..66ac69a --- /dev/null +++ b/881.cpp @@ -0,0 +1,200 @@ +#include + +using namespace std; + +#define eps 1e-8 +#define MAXN 1024 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + bool operator==(const Pt &a) const + { + return fabs(x - a.x) < eps && fabs(y - a.y) < eps; + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +int between(Pt a, Pt b, Pt c) +{ + return dot(c - a, b - a) >= -eps && dot(c - b, a - b) >= -eps; +} +int onSeg(Pt a, Pt b, Pt c) +{ + return between(a, b, c) && fabs(cross(a, b, c)) < eps; +} +int inPolygon(Pt p[], int n, Pt q) +{ + int i, j, cnt = 0; + for (i = 0, j = n - 1; i < n; j = i++) + { + if (onSeg(p[i], p[j], q)) + return 1; + if (p[i].y > q.y != p[j].y > q.y && + q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x) + cnt++; + } + return cnt & 1; +} +const double pi = acos(-1); +Pt polygon[MAXN][512]; +int pn[MAXN], parent[MAXN], visited[MAXN], depth[MAXN]; +int pid[MAXN]; +vector g[MAXN], tree[MAXN]; +void dfs(int u) +{ + visited[u] = 1, depth[u] = 0, parent[u] = -1; + int d = -1; + for (int i = 0; i < g[u].size(); i++) + { + int v = g[u][i]; + if (visited[v] == 0) + dfs(v); + if (depth[v] > d) + d = depth[v], parent[u] = v; + } + depth[u] = d + 1; +} +int query(int u, Pt q) +{ + for (int i = 0; i < tree[u].size(); i++) + { + int v = tree[u][i]; + if (inPolygon(polygon[v], pn[v], q)) + { + return query(v, q); + } + } + return u; +} +int main() +{ + int testcase, n, m; + string line; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + assert(n < MAXN); + while (getchar() != '\n') + ; + for (int i = 0; i < n; i++) + { + getline(cin, line); + stringstream sin(line); + int m = 0; + sin >> pid[i]; + while (sin >> polygon[i][m].x >> polygon[i][m].y) + { + m++; + assert(m < 512); + } + pn[i] = m; + } + pid[n] = 0; + for (int i = 0; i <= n; i++) + g[i].clear(), visited[i] = 0, tree[i].clear(); + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (i == j) + continue; + if (inPolygon(polygon[i], pn[i], polygon[j][0])) + g[j].push_back(i); + } + } + + for (int i = 0; i < n; i++) + { + if (visited[i] == 0) + dfs(i); + } + for (int i = 0; i < n; i++) + if (parent[i] == -1) + tree[n].push_back(i); + else + tree[parent[i]].push_back(i); + // for (int i = 0; i < n; i++) + // printf("%d: %d\n", pid[i], parent[i]); + scanf("%d", &m); + assert(m < 32767); + int out[32767] = {}; + for (int i = 0; i < m; i++) + { + Pt q; + int id; + scanf("%d %lf %lf", &id, &q.x, &q.y); + assert(id <= m); + out[id] = query(n, q); + } + for (int i = 1; i <= m; i++) + printf("%d %d\n", i, pid[out[i]]); + if (testcase) + puts(""); + } + return 0; +} +/* +5 +0 0 +50 50 +100 0 +100 100 +0 100 +2 +49 50 +50 51 + +7 +0 5 +5 0 +10 7 +15 0 +20 5 +15 10 +5 10 +7 +0 5 +5 0 +10 7 +15 0 +20 5 +15 10 +5 10 + +*/ diff --git a/882.cpp b/882.cpp new file mode 100644 index 0000000..e0bedfe --- /dev/null +++ b/882.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +const int MAX_K = 10; +const int MAX_M = 100; +const int INF = 1 << 29; + +int K, M; +bool visited[MAX_K + 1][MAX_M + 2][MAX_M + 2]; +int memo[MAX_K + 1][MAX_M + 2][MAX_M + 2]; + +int rec(const int k, const int low, const int high) +{ + if (visited[k][low][high]) + { + return memo[k][low][high]; + } + visited[k][low][high] = true; + int &ret = memo[k][low][high]; + ret = INF; + if (low > high) + { + return ret = 0; + } + if (low == high) + { + return ret = 0; + } + if (k == 0 && low != high) + { + return ret = INF; + } + for (int i = low; i <= high; i++) + { + ret = min(ret, i + max(rec(k, i + 1, high), rec(k - 1, low, i))); + } + return ret; +} + +void init() +{ + scanf("%d%d", &K, &M); + memset(visited, false, sizeof(visited)); +} + +int solve() +{ + return rec(K, 1, M + 1); +} + +int main() +{ + int T; + scanf("%d", &T); + for (; T--;) + { + init(); + printf("%d\n", solve()); + } + return 0; +} diff --git a/883.cpp b/883.cpp new file mode 100644 index 0000000..1c1be53 --- /dev/null +++ b/883.cpp @@ -0,0 +1,96 @@ +#include + +using namespace std; + +struct XCoord +{ + bool start; + double x; + int id; + XCoord(int _id, double _x, bool _start) : id(_id), x(_x), start(_start) {} +}; + +inline bool cmp(const XCoord *a, const XCoord *b) +{ + return a->x < b->x; +} + +int main() +{ + int T; + scanf("%d", &T); + while (T--) + { + vector vx; + map > my; + + int NR; + scanf("%d", &NR); + while (NR > 0) + { + NR--; + double x1, y1, x2, y2; + scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); + if (x1 > x2) + { + swap(x1, x2); + } + if (y1 > y2) + { + swap(y1, y2); + } + vx.push_back(new XCoord(NR, x1, true)); + vx.push_back(new XCoord(NR, x2, false)); + my[NR] = make_pair(y1, y2); + } + sort(vx.begin(), vx.end(), cmp); + + int NL; + scanf("%d", &NL); + while (NL--) + { + double y_l; + scanf("%lf", &y_l); + int overlap = 0, max_overlap = 0; + double x_left = -1.0f, x_right = -1.0f, x_prev = vx[0]->x;// only upd when y_l is used + int n = vx.size(); + for (int i = 0; i < n; i++) + { + double x_curr = vx[i]->x; + int id = vx[i]->id; + if (my[id].first <= y_l && my[id].second >= y_l) + { + if (vx[i]->start) + { + overlap++; + } + else + { + if (overlap > max_overlap) + { + max_overlap = overlap; + x_left = x_prev; + x_right = x_curr; + } + overlap--; + } + x_prev = x_curr; + } + } + + if (max_overlap > 0) + { + printf("%d %.2lf %.2lf\n", max_overlap, x_left, x_right); + } + else + { + printf("0\n"); + } + } + + if (T) + { + printf("\n"); + } + } +} diff --git a/884.cpp b/884.cpp new file mode 100644 index 0000000..5131ea0 --- /dev/null +++ b/884.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +/* +Factorial Factor +884 +*/ +#define MAX 1005 +int P[200], tp; +int F[1000002]; +char sv[MAX]; +void Prime_table() +{ + int i, j; + for (i = 2; i * i <= 1000;) + { + for (j = i + i; j < 1000; j += i) + { + sv[j] = 1; + } + for (++i; sv[i]; i++) + ; + } + tp = 1; + P[0] = 2; + for (i = 3; i < 1000; i += 2) + { + if (sv[i] == 0) + { + P[tp++] = i; + } + } +} +int Times(int n) +{ + int i, c = 0; + for (i = 0; P[i] * P[i] <= n && i < tp; i++) + { + if (n % P[i] == 0) + { + while (n % P[i] == 0) + { + c++; + n /= P[i]; + } + } + } + if (n > 1) + { + c++; + } + return c; +} +void Gen() +{ + int i, c = 1; + F[2] = 1; + for (i = 3; i <= 1000000; i++) + { + c += Times(i); + F[i] = c; + } +} +int main() +{ + int n; + Prime_table(); + Gen(); + while (scanf("%d", &n) == 1) + { + printf("%d\n", F[n]); + } + return 0; +} diff --git a/886.cpp b/886.cpp new file mode 100644 index 0000000..7cba6b2 --- /dev/null +++ b/886.cpp @@ -0,0 +1,294 @@ +#include + +using namespace std; + +#define mem(x, y) memset(x, y, sizeof(x)) +#define CLEAR(x) memset(x, 0, sizeof(x)) + +#define pb push_back +#define Sort(v) sort(v.begin(), v.end()) +#define RSort(v) sort(v.rbegin(), v.rend()) +#define CSort(v, C) sort(v.begin(), v.end(), C) +#define all(v) (v).begin(), (v).end() +#define sqr(x) ((x) * (x)) +#define find_dist(a, b) sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)) + +#define ERR 1e-9 +#define pi (2 * acos(0)) +#define PI 3.141592653589793 + +#define scanint(a) scanf("%d", &a) +#define scanLLD(a) scanf("%lld", &a) +#define scanstr(s) scanf("%s", s) +#define scanline(l) scanf(" %[^\n]", l); + +#define check(n, pos) (n & (1 << (pos))) +#define biton(n, pos) (n | (1 << (pos))) +#define bitoff(n, pos) (n & ~(1 << (pos))) + +#define WHITE 0 +#define GREY 1 +#define BLACK 2 + +typedef vector vint; +typedef vector vint2D; +typedef vector vstr; +typedef vector vchar; +typedef vector vchar2D; +typedef queue Qi; +typedef queue Qii; +typedef map Mii; +typedef map Msi; +typedef map Mis; +typedef stack stk; +typedef pair pp; +typedef pair ppp; +typedef long long int LLD; +const int inf = 0x7fffffff; + +struct point_int +{ + int x, y; + point_int() {} + point_int(int a, int b) + { + x = a, y = b; + } +}; + +// Point for x,y (int) coordinate in 2D space +struct point_double +{ + double x, y; + point_double() {} + point_double(double a, double b) + { + x = a, y = b; + } +}; + +// Point for x,y (double) coordinate in 2D space +struct Node +{ + int v, w; + Node() {} + bool operator<(const Node &a) const + { + return w > a.w; + } + Node(int _v, int _w) + { + v = _v, w = _w; + } +}; +// Node for Dijkstra + +namespace my +{ +template +T gcd(T a, T b) +{ + return b == 0 ? a : gcd(b, a % b); +} +template +T lcm(T a, T b) +{ + return a / gcd(a, b) * b; +} +template +T big_mod(T n, T p, T m) +{ + if (p == 0) + { + return (T)1; + } + T x = big_mod(n, p / 2, m); + x = (x * x) % m; + if (p & 1) + { + x = (x * n) % m; + } + return x; +} +template +T multiplication(T n, T p, T m) +{ + if (p == 0) + { + return (T)0; + } + T x = multiplication(n, p / 2, m); + x = (x + x) % m; + if (p & 1) + { + x = (x + n) % m; + } + return x; +} +template +T my_pow(T n, T p) +{ + if (p == 0) + { + return 1;///n to the power p + } + T x = my_pow(n, p / 2); + x = (x * x); + if (p & 1) + { + x = (x * n); + } + return x; +} +template +double getdist(T a, T b) +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));/// distance between a & b +} +template +T extract(string s, T ret) +{ + stringstream ss(s);/// extract words or numbers from a line + ss >> ret; + return ret; +} +template +string tostring(T n) +{ + stringstream ss;/// convert a number to string + ss << n; + return ss.str(); +} +template +inline T Mod(T n, T m) +{ + return (n % m + m) % m;///For Positive Negative No. +} +template +T MIN3(T a, T b, T c) +{ + return min(a, min(b, c));/// minimum of 3 number +} +template +T MAX3(T a, T b, T c) +{ + return max(a, max(b, c));///maximum of 3 number +} +template +void print_vector(T &v) +{ + int sz = v.size();/// prints all elements in a vector + if (sz) + { + cout << v[0]; + } + for (int i = 1; i < sz; i++) + { + cout << ' ' << v[i]; + } + cout << "\n"; +} +bool isVowel(char ch) +{ + ch = toupper(ch); + if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') + { + return true; + } + return false; +} +bool isConsonant(char ch) +{ + if (isalpha(ch) && !isVowel(ch)) + { + return true; + } + return false; +} +} +/**Shortcut input function**/ +int read_int() +{ + int n; + scanf("%d", &n); + return n; +} +int read_LLD() +{ + LLD n; + scanf("%lld", &n); + return n; +} +inline int buffer_input() +{ + char inp[1000]; + scanstr(inp); + return atoi(inp); +} + +const string Map = "22233344455566677778889999"; +vector > Dic; + +int main() +{ + string S1, S2, S3; + while (cin >> S1) + { + if (isdigit(S1[0])) + { + break; + } + cin >> S2 >> S3; + string Name = ""; + Name += tolower(S1[0]); + for (int i = 0; i < S2.size(); i++) + { + Name += tolower(S2[i]); + } + string Sug = ""; + for (int i = 0; i < Name.size(); i++) + { + Sug += (Map[Name[i] - 'a']); + } + Dic.pb(make_pair(Sug, S3)); + } + do + { + vector Ans; + bool flag = false; + for (pair x : Dic) + { + if (x.second == S1) + { + cout << x.second << endl; + flag = true; + break; + } + bool ok = true; + for (int i = 0; i < S1.size(); i++) + { + if (x.first[i] != S1[i]) + { + ok = false; + break; + } + } + if (ok) + { + Ans.pb(x.second); + } + } + if (!flag) + { + if (Ans.size()) + { + my::print_vector(Ans); + } + else + { + cout << 0 << "\n"; + } + } + } while (cin >> S1); + return 0; +} diff --git a/890.cpp b/890.cpp new file mode 100644 index 0000000..94c6f40 --- /dev/null +++ b/890.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +int N[64][64], W[64][64], V[64][64], sx[8192], sy[8192], m, n, k, x, t; + +int main() +{ + int i, j, c; + for (scanf("%d", &t); t-- > 0 && scanf("%d %d", &m, &n) == 2;) + { + memset(N, 0, sizeof(N)); + memset(W, 0, sizeof(W)); + memset(V, 0, sizeof(V)); + scanf("%d %d", &sy[1], &sx[1]); + V[sy[1] = m - sy[1] + 1][sx[1]] = 1; + for (i = 0; i <= (m + 1); i++) + { + V[i][0] = V[i][n + 1] = 1; + } + for (i = 0; i <= (n + 1); i++) + { + V[0][i] = V[m + 1][i] = 1; + } + for (k = 1; k > 0;) + { + i = sy[k], j = sx[k]; + if (V[i - 1][j] & V[i + 1][j] & V[i][j - 1] & V[i][j + 1]) + { + k--; + continue; + } + while ((c = getchar()) != EOF && !isalpha(c)) + ; + assert(c != EOF); + if (c == 'F') + { + scanf("%d", &x); + for (i = x, j = k; i < j; i++, j--) + c = sx[i], sx[i] = sx[j], sx[j] = c, + c = sy[i], sy[i] = sy[j], sy[j] = c; + continue; + } + if (c == 'U') + { + N[i][j] = 1, k++, sy[k] = i - 1, sx[k] = j; + } + else if (c == 'D') + { + N[i + 1][j] = 1, k++, sy[k] = i + 1, sx[k] = j; + } + else if (c == 'L') + { + W[i][j] = 1, k++, sy[k] = i, sx[k] = j - 1; + } + else + { + W[i][j + 1] = 1, k++, sy[k] = i, sx[k] = j + 1; + } + V[sy[k]][sx[k]] = 1; + } + for (i = 0; i < n; i++) + { + printf(" _"); + } + printf("\n"); + for (i = 1; i <= m; i++) + { + for (j = 1; j <= n; j++) + { + putchar(W[i][j] ? ' ' : '|'); + putchar(N[i + 1][j] ? ' ' : '_'); + } + printf("|\n"); + } + printf("\n"); + } + return 0; +} diff --git a/891.cpp b/891.cpp new file mode 100644 index 0000000..4e0f1f8 --- /dev/null +++ b/891.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +typedef struct +{ + int w, h, m; +} box_t; + +char *p; + +int max(int a, int b) +{ + return (a > b) ? a : b; +} + +void expr(box_t *r) +{ + box_t a, b; + while (*p == ' ' || *p == '\t') + { + p++; + } + switch (*p++) + { + case '\"': + for (r->w = 8; *p++ != '\"'; r->w++) + ; + r->h = 3; + r->m = 1; + break; + case '(': + expr(&a); + expr(&b); + while (*p++ != ')') + ; + r->w = a.w + b.w; + r->m = max(a.m, b.m); + r->h = r->m + max(a.h - a.m, b.h - b.m); + break; + case '{': + expr(&a); + expr(&b); + while (*p++ != '}') + ; + r->w = max(a.w, b.w) + 4; + r->h = a.h + b.h + 1; + r->m = a.m; + break; + case '[': + expr(r); + while (*p++ != ']') + ; + r->w += 4; + r->h++; + r->m++; + break; + } +} + +int main() +{ + static char s[65536]; + box_t r; + int t; + while (gets(s) && sscanf(s, "%d", &t) != 1) + ; + while (t-- > 0 && gets(s)) + { + p = s; + expr(&r); + printf("%dx%d\n", r.w + 4, r.h); + } + return 0; +} diff --git a/892.cpp b/892.cpp new file mode 100644 index 0000000..9bb7024 --- /dev/null +++ b/892.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int main() +{ + string input; + string hyp = ""; + while (getline(cin, input)) + { + if (input == "#") + { + break; + } + int pos = input.size(); + if (!hyp.empty()) + { + cout << hyp; + hyp = ""; + for (int i = 0, sz = input.size(); i < sz; i++) + { + if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i] <= 'Z') + { + cout << input[i]; + input[i] = '_'; + continue; + } + break; + } + cout << endl; + } + if (input[input.size() - 1] == '-') + { + for (int i = input.size() - 2; i >= 0; i--) + { + if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i] <= 'Z') + { + continue; + } + pos = i + 1; + break; + } + hyp = input.substr(pos, input.size() - pos - 1); + } + for (int i = 0; i < pos; i++) + { + if (input[i] >= 'A' && input[i] <= 'Z' || input[i] >= 'a' && input[i] <= 'z' || input[i] == ' ') + { + cout << input[i]; + } + } + cout << endl; + } + return 0; +} diff --git a/893.cpp b/893.cpp new file mode 100644 index 0000000..87aff35 --- /dev/null +++ b/893.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +long Jd(int year, int month, int day) +{ + int Grig = 0;// is it Grigorian date? + if (year > 1528) + { + Grig = 0; + } + else if (year < 1528) + { + Grig = 1; + } + else if (month > 10) + { + Grig = 0; + } + else if (month < 10) + { + Grig = 1; + } + else if (day >= 15) + { + Grig = 0; + } + else + { + Grig = 1; + } + if (month <= 2) + { + year--; + month += 12; + }; + unsigned long J; + int A; + if (Grig == 0) + { + A = year / 100; + A = 2 - A + (A / 4); + } + else + { + A = 0; + } + J = 1461L * long(year); + J /= 4L; + unsigned long K = 306001L * long(month + 1); + K /= 10000L; + J += K + day + 1720995L + A; + return J; +} + +void GDate(long JD, int &y, int &m, int &d) +{ + unsigned long A = (JD * 4L - 7468865L) / 146097L; + A = (JD > 2299160) ? JD + 1 + A - (A / 4L) : JD; + long B = A + 1524; + long C = (B * 20L - 2442L) / 7305L; + long D = (C * 1461L) / 4L; + long E = (10000L * (B - D)) / 306001L; + d = int(B - D - ((E * 306001L) / 10000L)); + m = int((E <= 13) ? E - 1 : E - 13); + y = int(C - ((m > 2) ? 4716 : 4715)); +} + +int main() +{ + int d, m, y, n; + while (cin >> n >> d >> m >> y) + { + if (!n && !d && !m && !y) + { + break; + } + long julian_date = Jd(y, m, d); + julian_date += n; + GDate(julian_date, y, m, d); + cout << d << " " << m << " " << y << endl; + } + return 0; +} diff --git a/895.cpp b/895.cpp new file mode 100644 index 0000000..b4d9d65 --- /dev/null +++ b/895.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +int V[1005][128] = {0}, len, vec[128]; +char W[20]; +int main() +{ + int i, j, ok, ans; + for (len = 0; gets(W); len++) + { + if (W[0] == '#') + { + break; + } + for (i = 0; W[i]; i++) + { + V[len][W[i]]++; + } + } + while (gets(W)) + { + if (W[0] == '#') + { + break; + } + for (i = 'a'; i <= 'z'; i++) + { + vec[i] = 0; + } + for (i = 0; W[i]; i++) + { + vec[W[i]]++; + } + for (i = ans = 0; i < len; i++) + { + for (j = 'a', ok = 1; j <= 'z'; j++) + { + if (vec[j] < V[i][j]) + { + ok = 0; + break; + } + } + ans += ok; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/897.cpp b/897.cpp new file mode 100644 index 0000000..0811e71 --- /dev/null +++ b/897.cpp @@ -0,0 +1,97 @@ +#include + +using namespace std; + +enum +{ + MAX = 1000 +}; + +bitset p; + +void sieve() +{ + p.flip(); + p[0] = p[1] = 0; + for (int i = 2; i * i < MAX; i++) + if (p[i]) + for (int j = i * i; j < MAX; j += i) + { + p[j] = 0; + } +} + +bool is_anagramatic_prime(int n) +{ + if (!p[n]) + { + return false; + } + char str[16]; + int len = sprintf(str, "%d", n); + sort(str, str + len); + do + { + if (!p[atoi(str)]) + { + return false; + } + } while (next_permutation(str, str + len)); + return true; +} + +set find_all_anagramatic_primes() +{ + set anag; + for (int i = 2; i < MAX; ++i) + if (is_anagramatic_prime(i)) + { + anag.insert(i); + } + return anag; +} + +int max_value(set &anag) +{ + return (*anag.rbegin()); +} + +int next_power_of_10(int lower) +{ + int upper = 1; + while (upper <= lower) + { + upper = upper * 10; + } + return upper; +} + +int main() +{ + sieve(); + set anag = find_all_anagramatic_primes(); + int lower; + while (scanf("%d", &lower) == 1) + { + if (lower == 0) + { + break; + } + if (lower > max_value(anag)) + { + printf("0\n"); + continue; + } + int upper = next_power_of_10(lower); + int next = *anag.lower_bound(lower + 1); + if (next < upper) + { + printf("%d\n", next); + } + else + { + printf("0\n"); + } + } + return 0; +} diff --git a/898.cpp b/898.cpp new file mode 100644 index 0000000..2cd89bd --- /dev/null +++ b/898.cpp @@ -0,0 +1,134 @@ +#include + +using namespace std; + +/** +compress coordinates, do a few DFSs on the reduced board +same as UVa 308 +*/ + +char plate[256][256]; +int height, width; +int cutLeft[256][256], cutUp[256][256]; + +void externalDfs(int y, int x) +{ + if (y <= 0 || x <= 0 || y > height || x > width) + { + return; + } + if (plate[y][x] != 0) + { + return; + } + plate[y][x] = 1; + if (!cutUp[y][x]) + { + externalDfs(y - 1, x); + } + if (!cutUp[y + 1][x]) + { + externalDfs(y + 1, x); + } + if (!cutLeft[y][x]) + { + externalDfs(y, x - 1); + } + if (!cutLeft[y][x + 1]) + { + externalDfs(y, x + 1); + } +} + +void internalDfs(int y, int x) +{ + if (y <= 0 || x <= 0 || y > height || x > width) + { + return; + } + if (plate[y][x] != 0) + { + return; + } + plate[y][x] = 1; + internalDfs(y - 1, x); + internalDfs(y + 1, x); + internalDfs(y, x - 1); + internalDfs(y, x + 1); +} + +int main() +{ + int N; + int x1[100], y1[100], x2[100], y2[100]; + while (scanf("%d", &N) == 1 && N > 0) + { + assert(N <= 100); + vector X, Y; + for (int i = 0; i < N; i++) + { + scanf("%d %d %d %d", &x1[i], &y1[i], &x2[i], &y2[i]); + X.push_back(x1[i]); + X.push_back(x2[i]); + Y.push_back(y1[i]); + Y.push_back(y2[i]); + } + sort(X.begin(), X.end()); + X.erase(unique(X.begin(), X.end()), X.end()); + sort(Y.begin(), Y.end()); + Y.erase(unique(Y.begin(), Y.end()), Y.end()); + for (int i = 0; i < N; i++) + { + x1[i] = lower_bound(X.begin(), X.end(), x1[i]) - X.begin() + 5; + x2[i] = lower_bound(X.begin(), X.end(), x2[i]) - X.begin() + 5; + y1[i] = lower_bound(Y.begin(), Y.end(), y1[i]) - Y.begin() + 5; + y2[i] = lower_bound(Y.begin(), Y.end(), y2[i]) - Y.begin() + 5; + } + memset(cutUp, 0, sizeof(cutUp)); + memset(cutLeft, 0, sizeof(cutLeft)); + for (int i = 0; i < N; i++) + { + if (x1[i] == x2[i])// vertical cut + { + int x = x1[i]; + int yLow = min(y1[i], y2[i]); + int yHigh = max(y1[i], y2[i]); + for (int y = yLow; y < yHigh; y++) + { + cutLeft[y][x] = 1; + } + } + else if (y1[i] == y2[i]) + { + int y = y1[i]; + int xLow = min(x1[i], x2[i]); + int xHigh = max(x1[i], x2[i]); + for (int x = xLow; x < xHigh; x++) + { + cutUp[y][x] = 1; + } + } + else + { + abort(); + } + } + memset(plate, 0, sizeof(plate)); + height = Y.size() + 10; + width = X.size() + 10; + externalDfs(1, 1); + int n = 0; + for (int y = 1; y <= height; y++) + { + for (int x = 1; x <= width; x++) + { + if (plate[y][x] == 0) + { + internalDfs(y, x); + n++; + } + } + } + printf("%d\n", n); + } +} diff --git a/899.cpp b/899.cpp new file mode 100644 index 0000000..eb43b1c --- /dev/null +++ b/899.cpp @@ -0,0 +1,131 @@ +#include + +using namespace std; + +#define MAXN 102 + +struct pp +{ + int u, v; + int move; +}; +queue Q; +struct ss +{ + int colour; +} node[MAXN]; +struct xx +{ + int E[MAXN]; + int C[MAXN]; + int ind; +} edge[MAXN]; +char Visit[MAXN][MAXN]; +int N, Min; + +int MoveFirst(int c1, int c2, int terget, int move) +{ + int i, x; + pp dum; + for (i = 0; i < edge[c1].ind; i++) + { + if (edge[c1].E[i] != c2 && edge[c1].C[i] == node[c2].colour) + { + x = edge[c1].E[i]; + if (!Visit[x][c2]) + { + if (x == terget) + { + Min = move; + return 1; + } + dum.u = x; + dum.v = c2; + dum.move = move; + Q.push(dum); + Visit[x][c2] = Visit[c2][x] = 1; + } + } + } + return 0; +} +int MoveSecond(int c1, int c2, int terget, int move) +{ + int i, x; + pp dum; + for (i = 0; i < edge[c2].ind; i++) + { + if (edge[c2].E[i] != c1 && edge[c2].C[i] == node[c1].colour) + { + x = edge[c2].E[i]; + if (!Visit[x][c1]) + { + if (x == terget) + { + Min = move; + return 1; + } + dum.u = c1; + dum.v = x; + dum.move = move; + Q.push(dum); + Visit[x][c1] = Visit[c1][x] = 1; + } + } + } + return 0; +} +void BFS(int c1, int c2, int terget) +{ + pp temp, dum; + temp.u = c1; + temp.v = c2; + temp.move = 0; + Visit[c1][c2] = Visit[c2][c1] = 1; + Q.push(temp); + while (!Q.empty()) + { + dum = Q.front(); + Q.pop(); + if (!Min) + { + MoveFirst(dum.u, dum.v, terget, dum.move + 1); + MoveSecond(dum.u, dum.v, terget, dum.move + 1); + } + } +} +void Cal(int c1, int c2, int terget) +{ + int i, j; + Min = 0; + BFS(c1, c2, terget); + printf("%d\n", Min); + Q.empty(); + for (i = 1; i <= N; i++) + for (j = 1; j <= N; j++) + Visit[i][j] = Visit[j][i] = 0; +} + +int main() +{ + int i, c1, c2, terget, edg, c, st, ed; + while (scanf("%d%d%d%d%d", &N, &c1, &c2, &terget, &edg) == 5) + { + if (!N && !c1 && !c2 && !terget && !edg) + break; + for (i = 0; i < N; i++) + { + scanf("%d", &c); + node[i + 1].colour = c; + edge[i].ind = 0; + } + for (i = 0; i < edg; i++) + { + scanf("%d%d%d", &st, &ed, &c); + edge[st].E[edge[st].ind] = ed; + edge[st].C[edge[st].ind++] = c; + } + Cal(c1, c2, terget); + } + return 0; +} diff --git a/900.cpp b/900.cpp index e02c8f7..e92f336 100644 --- a/900.cpp +++ b/900.cpp @@ -1,19 +1,23 @@ -#include -using namespace std; - -int main(){ - long long fib[51]={0}; - fib[1]=1; - fib[2]=2; - for(int i=3;i<51;i++) - fib[i]=fib[i-1]+fib[i-2]; - for(;;){ - int n; - cin>>n; - if(n==0) - break; - cout< + +using namespace std; + +int main() +{ + unsigned long long fib[51]; + fib[0] = fib[1] = 1; + for (int i = 2; i < 51; i++) + { + fib[i] = fib[i - 1] + fib[i - 2]; + } + int n; + while (cin >> n) + { + if (n == 0) + { + break; + } + cout << fib[n] << endl; + } + return 0; +} diff --git a/902.cpp b/902.cpp index a802aec..a0873fa 100644 --- a/902.cpp +++ b/902.cpp @@ -1,57 +1,57 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define FOI(i, A, B) for (i = A; i <= B; i++) -#define FOD(i, A, B) for (i = A; i >= B; i--) +unsigned int jenkins_one_at_a_time_hash(const string &input, int start, int n) +{ + unsigned int hash = 0; + for (int i = 0; i < n; i++) + { + hash += input[start + i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + return hash; +} -int main(){ - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - int N, i; - string S; - while (cin >> N) { - cin >> S; - map Map; - map::iterator it; - pair P; - - int L = S.length(); - int M = 0; - FOI(i, 0, L-N) { - string Key = S.substr(i, N); - ++Map[Key]; - } - for (it = Map.begin(); it != Map.end(); it++) { - if ((*it).second > M) { - M = (*it).second; - P = (*it); +struct result +{ + string s; + int count; + result() : count(0) {} +}; + +int main() +{ + int n; + string input; + unsigned int hash; + map m; + while (cin >> n >> input) + { + m.clear(); + result best; + for (int i = 0, sz = input.size(); i + n - 1 < sz; i++) + { + hash = jenkins_one_at_a_time_hash(input, i, n); + if (m.find(hash) == m.end()) + { + m[hash] = 1; + } + else + { + m[hash] += 1; + } + if (m[hash] > best.count) + { + best.count = m[hash]; + best.s = string(input.begin() + i, input.begin() + i + n); } } - cout << P.first << endl; + cout << best.s << endl; } return 0; } diff --git a/903.cpp b/903.cpp new file mode 100644 index 0000000..629d387 --- /dev/null +++ b/903.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +void set_(int n, int *x, int *y) +{ + if (n == 1) + { + *x = *y = 0; + return; + } + int len, side = sqrt(--n), r; + if (side & 1) + { + side++; + } + r = n - (side - 1) * (side - 1); + len = side / 2; + switch (r / side) + { + case 0: + *x = len, *y = len - 1 - r % side; + break; + case 1: + *x = len - 1 - r % side, *y = -len; + break; + case 2: + *x = -len, *y = r % side - len + 1; + break; + case 3: + *x = r % side - len + 1, *y = len; + break; + } +} +void put(int x, int y) +{ + if ((x | y) == 0) + { + printf("1;"); + return; + } + int len, side, r, type; + if (x - y > 0) + { + type = ((x + y > 0) ? 0 : 1); + } + else + { + type = ((x + y > 0) ? 3 : 2); + } + switch (type) + { + case 0: + len = x, r = len - y; + break; + case 1: + len = -y, r = len - x; + break; + case 2: + len = -x, r = y + len; + break; + case 3: + len = y, r = x + len; + break; + } + side = len * 2; + printf("%d;", (side - 1) * (side - 1) + r + type * side); +} + +int main() +{ + int n, x, y; + while (scanf("%d", &n) == 1) + { + set_(n, &x, &y); + put(x - 1, y + 1); + put(x, y + 1); + put(x + 1, y + 1); + printf("\n"); + put(x - 1, y); + printf("%d;", n); + put(x + 1, y); + printf("\n"); + put(x - 1, y - 1); + put(x, y - 1); + put(x + 1, y - 1); + printf("\n"); + } + return 0; +} diff --git a/904.cpp b/904.cpp new file mode 100644 index 0000000..a9a9996 --- /dev/null +++ b/904.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +typedef long long ll; + +struct Box +{ + ll a[3], b[3]; + + ll volume() + { + return (b[0] - a[0]) * (b[1] - a[1]) * (b[2] - a[2]); + } +}; + +Box isect(Box A, Box B) +{ + Box C = A; + Box zero = {{0, 0, 0}, {0, 0, 0}}; + for (int i = 0; i < 3; i++) + { + C.a[i] = max(A.a[i], B.a[i]); + C.b[i] = min(A.b[i], B.b[i]); + if (C.a[i] >= C.b[i]) + { + return zero; + } + } + return C; +} + +int main() +{ + static ll sign[65536]; + Box box[20]; + int n; + while (scanf("%d", &n) == 1 && n > 0) + { + for (int i = 0; i < n; i++) + { + scanf("%lld %lld %lld", &box[i].a[0], &box[i].a[1], &box[i].a[2]); + scanf("%lld %lld %lld", &box[i].b[0], &box[i].b[1], &box[i].b[2]); + } + sign[0] = 1; + for (int mask = 1; mask < (1 << n); mask++) + { + int i, j; + sign[mask] = 0; + for (i = 0; (mask & (1 << i)) == 0; i++) + ; + for (j = i + 1; j < n; j++) + { + if (mask & (1 << j)) + { + sign[mask] -= sign[mask ^ (1 << i)]; + sign[mask] -= sign[mask ^ (1 << i) ^ (1 << j)]; + } + } + } + long long res = 0; + for (int mask = 1; mask < (1 << n); mask++) + { + Box b; + int cnt = 0; + for (int i = 0; i < n; i++) + { + if (mask & (1 << i)) + { + b = (cnt == 0 ? box[i] : isect(b, box[i])); + cnt++; + } + } + res += sign[mask] * b.volume(); + } + if (res < 0) + { + res = -res; + } + printf("%lld\n", res); + } + return 0; +} diff --git a/905.cpp b/905.cpp new file mode 100644 index 0000000..84db607 --- /dev/null +++ b/905.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; + +const int MAXN = 128; +int g[MAXN][MAXN], ret[MAXN][MAXN]; +const int dx[] = {-1, 0, 1, 0}; +const int dy[] = {0, 1, 0, -1}; +int main() +{ + int px, py, w, h; + int cases = 0; + while (scanf("%d %d %d %d", &px, &py, &w, &h) == 4) + { + assert(w < MAXN && h < MAXN); + if (cases++) + puts(""); + + memset(g, 0, sizeof(g)); + memset(ret, 0, sizeof(ret)); + for (int i = 0; i < MAXN; i++) + { + int x, y; + x = px - i - 1, y = py + i + 1; + for (int j = 0; j < 2 * (i + 1); j++) + { + if (x >= 0 && x <= w && y >= 0 && y <= h) + g[x][y] = 1; + y -= 1; + } + x = px - i, y = py + i + 1; + for (int j = 0; j < 2 * (i + 1); j++) + { + if (x >= 0 && x <= w && y >= 0 && y <= h) + g[x][y] = 2; + x += 1; + } + x = px + i + 1, y = py + i; + for (int j = 0; j < 2 * (i + 1); j++) + { + if (x >= 0 && x <= w && y >= 0 && y <= h) + g[x][y] = 3; + y -= 1; + } + x = px - i - 1, y = py - i - 1; + for (int j = 0; j < 2 * (i + 1); j++) + { + if (x >= 0 && x <= w && y >= 0 && y <= h) + g[x][y] = 4; + x += 1; + } + } + + for (int i = h; i >= 1; i--) + { + int n, x; + scanf("%d", &n); + for (int j = 0; j < n; j++) + { + scanf("%d", &x); + int tx, ty; + tx = x + dx[g[x][i] - 1]; + ty = i + dy[g[x][i] - 1]; + ret[tx][ty] = 1; + } + } + // for (int i = 1; i <= w; i++, puts("")) + // for (int j = 1; j <= h; j++) + // printf("%d", g[i][j]); + + printf("%d %d %d %d\n", px, py, w, h); + for (int i = h; i >= 1; i--) + { + int n = 0; + for (int j = 1; j <= w; j++) + n += ret[j][i]; + printf("%d", n); + for (int j = 1; j <= w; j++) + if (ret[j][i]) + printf(" %d", j); + puts(""); + } + } + return 0; +} +/* +3 3 7 6 +1 6 +1 2 +0 +1 6 +1 2 +1 5 +*/ diff --git a/906.cpp b/906.cpp new file mode 100644 index 0000000..27506a4 --- /dev/null +++ b/906.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +#define EPS 1e-8 + +typedef pair ii; +typedef vector vi; +typedef vector vii; +typedef vector vvi; +typedef vector vvii; +typedef map mii; + +bool canPass(int c, double k1, double k2) +{ + return (ceil(k1 * c - EPS) <= floor(k2 * c - EPS)); +} + +int main() +{ + double a, b, n; + int c, d; + double k1, k2; + while (scanf("%lf%lf %lf", &a, &b, &n) == 3) + { + k1 = b / (a + b * n); + k2 = b / a; + c = 1; + while (!canPass(c, k1, k2)) + { + c++; + } + d = ceil(k1 * c - EPS); + printf("%d %d\n", c, d); + } + return 0; +} diff --git a/907.cpp b/907.cpp new file mode 100644 index 0000000..cae679f --- /dev/null +++ b/907.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int C[605]; + +int main() +{ + int n, i, j, k, day, up, dn, mid, dist; + while (scanf("%d%d", &n, &k) == 2) + { + n++, k++; + for (i = up = dn = 0; i < n; i++) + { + scanf("%d", C + i); + if (dn < C[i]) + { + dn = C[i]; + } + up += C[i]; + } + for (dn--; up - dn > 1;) + { + mid = (up + dn) / 2;// binary search + for (day = 1, dist = i = 0; i < n; i++) + { + if (dist + C[i] <= mid) + { + dist += C[i]; + } + else + { + dist = C[i]; + day++; + } + } + if (day > k) + { + dn = mid; + } + else + { + up = mid; + } + } + printf("%d\n", up); + } + return 0; +} diff --git a/908.cpp b/908.cpp new file mode 100644 index 0000000..06c71c2 --- /dev/null +++ b/908.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +struct UnionFindDisjointSets +{ + UnionFindDisjointSets(int size); + int findSet(int a); + bool isSameSet(int a, int b); + void unionSet(int a, int b); + int numDisjointSets(); + int sizeOfSet(int a); + + int size; + vector pset; + vector set_size; +}; + +UnionFindDisjointSets::UnionFindDisjointSets(int size) +{ + this->size = size; + set_size.assign(size, 1); + pset.assign(size, 0); + for (int i = 0; i < size; i++) + { + pset[i] = i; + } +} + +int UnionFindDisjointSets::findSet(int a) +{ + return pset[a] == a ? a : (pset[a] = findSet(pset[a])); +} + +bool UnionFindDisjointSets::isSameSet(int a, int b) +{ + return findSet(a) == findSet(b); +} + +void UnionFindDisjointSets::unionSet(int a, int b) +{ + if (isSameSet(a, b)) + { + return; + } + size--; + set_size[findSet(b)] += set_size[findSet(a)]; + pset[findSet(a)] = findSet(b); +} + +int UnionFindDisjointSets::numDisjointSets() +{ + return size; +} + +int UnionFindDisjointSets::sizeOfSet(int a) +{ + return set_size[findSet(a)]; +} + +int main() +{ + int n, k, m; + int a, b, c; + bool first = true; + while (cin >> n) + { + if (!first) + { + cout << endl; + } + first = false; + priority_queue>> edgeList; + int old_result = 0; + for (int i = 0; i < n - 1; i++) + { + cin >> a >> b >> c; + old_result += c; + } + cin >> k; + while (k--) + { + cin >> a >> b >> c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + cin >> m; + while (m--) + { + cin >> a >> b >> c; + edgeList.push(make_pair(-c, make_pair(a, b))); + } + int new_result = 0; + UnionFindDisjointSets ds(n + 1); + while (!edgeList.empty()) + { + pair> front = edgeList.top(); + edgeList.pop(); + if (!ds.isSameSet(front.second.first, front.second.second)) + { + new_result += (-front.first); + ds.unionSet(front.second.first, front.second.second); + } + } + cout << old_result << endl; + cout << new_result << endl; + } + return 0; +} diff --git a/909.cpp b/909.cpp new file mode 100644 index 0000000..eb20e99 --- /dev/null +++ b/909.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +#define MAX 1048576 + +unsigned char input[MAX]; +int a[MAX], b[MAX], N; + +int main() +{ + for (N = 0;;) + { + int c = getchar(); + if (c == EOF) + { + break; + } + assert(N + 5 < MAX); + input[N++] = (unsigned char)c; + } + a[N] = 0; + for (int i = N - 1; i >= 0; i--) + { + a[i] = 2 + a[i + 1]; + b[i] = 1; + for (int j = 1; j <= 128 && i + j <= N; j++) + { + if (1 + j + a[i + j] < a[i]) + { + a[i] = 1 + j + a[i + j]; + b[i] = j; + } + } + for (int j = 2; j <= 128 && i + j <= N && input[i + j - 1] == input[i]; j++) + { + if (2 + a[i + j] < a[i]) + { + a[i] = 2 + a[i + j]; + b[i] = -j; + } + } + } + for (int i = 0; i < N;) + { + int k = b[i]; + if (k > 0) + { + putchar(k - 1); + for (int j = 0; j < k; j++) + { + putchar(input[i + j]); + } + i += k; + } + else + { + putchar(127 - k); + putchar(input[i]); + i += -k; + } + } +} diff --git a/910.cpp b/910.cpp new file mode 100644 index 0000000..33a8189 --- /dev/null +++ b/910.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +// dp - baktrack +// state dp[a][b] = c +// - current position a +// - b moves remaining +// - in this situation, can achieve goal in c ways +// m=0: 1 if current pos is special, otherwise 0 +// otherwise: 2 options - path 0 || 1 +// eg. dp[pos 'C'][3] +// = #ways to get the goal, choosing path 0, m=3-1=2 +// + #ways to get the goal, choosing path 1, m=3-1=2 + +int matrix[26 + 3][4 + 3], dp[26 + 3][30 + 3]; + +int bktk(int pos, int m) +{ + if (m == 0) + { + return matrix[pos][2]; + } + if (dp[pos][m] != -1) + { + return dp[pos][m]; + } + return dp[pos][m] = bktk(matrix[pos][0], m - 1) + bktk(matrix[pos][1], m - 1); +} + +void reset() +{ + for (int i = 0; i < 29; i++) + { + for (int j = 0; j < 33; j++) + { + dp[i][j] = -1; + } + } +} + +int main() +{ + int nodes; + while (scanf("%d", &nodes) != EOF) + { + reset(); + getchar(); + char a[10]; + for (int i = 0; i < nodes; i++) + { + gets(a); + matrix[a[0] - 'A'][0] = a[2] - 'A'; + matrix[a[0] - 'A'][1] = a[4] - 'A'; + matrix[a[0] - 'A'][2] = a[6] == 'x' ? 1 : 0; + } + int m; + scanf("%d", &m); + printf("%d\n", bktk(0, m)); + } + return 0; +} diff --git a/911.cpp b/911.cpp new file mode 100644 index 0000000..dd33c76 --- /dev/null +++ b/911.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int gcd(int a, int b) +{ + if (!b) + { + return a; + } + while ((a %= b) && (b %= a)) + ; + return a + b; +} +int D[5000]; +int main() +{ + int i, n, k, z, ans, t, g; + while (scanf("%d%d", &n, &k) == 2) + { + for (i = 1; i <= n; i++) + { + D[i] = i; + } + while (k--) + { + scanf("%d", &z); + for (; z; z--) + { + t = z; + for (i = n; t != 1; i--) + { + g = gcd(D[i], t); + D[i] /= g; + t /= g; + } + } + } + for (i = ans = 1; i <= n; i++) + { + ans *= D[i]; + } + printf("%d\n", ans); + } + return 0; +} diff --git a/912.cpp b/912.cpp new file mode 100644 index 0000000..3b0b107 --- /dev/null +++ b/912.cpp @@ -0,0 +1,150 @@ +#include + +using namespace std; + +bool is_dna(string s) +{ + return s == "A" || s == "B" || s == "C" || s == "D"; +} + +bool cmp(const pair &a, const pair &b) +{ + return a.first < b.first; +} + +int main() +{ + int n; + bool t_fst = true; + string input; + + while (scanf("%d", &n) == 1) + { + if (t_fst) + { + t_fst = false; + } + else + { + printf("\n"); + } + + map assignments; + vector fst, snd; + + for (int i = 0; i < n; i++) + { + cin >> input; + fst.push_back(input); + } + + for (int i = 0; i < n; i++) + { + cin >> input; + snd.push_back(input); + } + + bool ok = true; + while (1) + { + bool changed = false; + + for (int i = 0; i < n; i++) + { + bool dna1 = is_dna(fst[i]); + bool dna2 = is_dna(snd[i]); + + if (dna1 && dna2) + { + if (fst[i] != snd[i]) + { + ok = false; + break; + } + } + else if (!dna1 && dna2) + { + if (assignments.find(fst[i]) == assignments.end()) + { + assignments[fst[i]] = snd[i]; + changed = true; + } + else + { + if (assignments[fst[i]] != snd[i]) + { + ok = false; + break; + } + } + } + else if (dna1 && !dna2) + { + if (assignments.find(snd[i]) == assignments.end()) + { + assignments[snd[i]] = fst[i]; + changed = true; + } + else + { + if (assignments[snd[i]] != fst[i]) + { + ok = false; + break; + } + } + } + else if (!dna1 && !dna2) + { + if (fst[i] != snd[i]) + { + if (assignments.find(fst[i]) == assignments.end() && assignments.find(snd[i]) != assignments.end()) + { + assignments[fst[i]] = assignments[snd[i]]; + changed = true; + } + else if (assignments.find(fst[i]) != assignments.end() && assignments.find(snd[i]) == assignments.end()) + { + assignments[snd[i]] = assignments[fst[i]]; + changed = true; + } + else if (assignments.find(fst[i]) != assignments.end() && assignments.find(snd[i]) != assignments.end()) + { + if (assignments[fst[i]] != assignments[snd[i]]) + { + ok = false; + break; + } + } + } + } + } + + if (!changed) + { + break; + } + } + + if (ok) + { + vector > res; + for (map::iterator it = assignments.begin(); it != assignments.end(); it++) + { + res.push_back(make_pair(it->first, it->second)); + } + sort(res.begin(), res.end(), cmp); + printf("YES\n"); + for (int i = 0; i < res.size(); i++) + { + printf("%s %s\n", res[i].first.c_str(),res[i].second.c_str()); + } + } + else + { + printf("NO\n"); + } + } + + return 0; +} diff --git a/913.cpp b/913.cpp index a9f62fb..56d9578 100644 --- a/913.cpp +++ b/913.cpp @@ -1,15 +1,37 @@ -#include +#include + using namespace std; -int main() { - long long N, M, S; - while (scanf("%lld", &N) != EOF) { - N = (N + 1) / 2; - M = N * N; - M = (2 * M - 1); - S = 3 * M - 6; - printf("%lld\n", S); +#define ss "%lld" +typedef long long Long; +Long getSum(Long n) +{ + Long k = (4 * n + 4) * n; + return k / 2; +} +Long Cal(Long n) +{ + Long myrange = n / 2 - 1; + if (n == 1) + { + return 1; + } + else if (n == 3) + { + //return 15; + } + Long first = (myrange + 1) * 2 + getSum(myrange) + 1; + Long last = (myrange * 2 + 2) * 2 + first; + Long result = last + (last - 2) + (last - 4); + return result; +} +int main() +{ + Long n; + while (scanf(ss, &n) == 1) + { + printf(ss, Cal(n)); + puts(""); } - return 0; } diff --git a/914.cpp b/914.cpp new file mode 100644 index 0000000..129b773 --- /dev/null +++ b/914.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +bool is_prime(int n) +{ + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; + } + } + return true; +} + +void add_difference(vector> &v, int diff) +{ + bool found = false; + for (int i = 0, sz = v.size(); i < sz; i++) + { + if (v[i].first == diff) + { + found = true; + v[i].second++; + break; + } + } + if (!found) + { + v.push_back(make_pair(diff, 1)); + } +} + +bool theTruthIsOutThere(const pair &a, const pair &b) +{ + return a.second > b.second; +} + +int main() +{ + int t; + int start, end; + vector primes; + vector> result; + primes.push_back(2); + for (int i = 3; i < 1000000; i += 2) + if (is_prime(i)) + { + primes.push_back(i); + } + cin >> t; + while (t--) + { + cin >> start >> end; + result.clear(); + int prev = -1; + for (int i = 0, sz = primes.size(); i < sz; i++) + { + if (primes[i] >= start && primes[i] <= end) + { + if (prev == -1) + { + prev = primes[i]; + } + else + { + add_difference(result, primes[i] - prev); + prev = primes[i]; + } + } + } + sort(result.begin(), result.end(), theTruthIsOutThere); + if (result.size() == 0) + { + cout << "No jumping champion" << endl; + } + else if (result.size() == 1) + { + cout << "The jumping champion is " << result[0].first << endl; + } + else + { + if (result[0].second > result[1].second) + { + cout << "The jumping champion is " << result[0].first << endl; + } + else + { + cout << "No jumping champion" << endl; + } + } + } + return 0; +} diff --git a/915.cpp b/915.cpp new file mode 100644 index 0000000..7ebc941 --- /dev/null +++ b/915.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +int N[128]; +double P[128], C[128]; +int main() +{ + int first = 1, n, i, j, f, tn, pr; + double dist; + while (scanf("%d", &n) == 1) + { + for (i = 0; i < n; i++) + { + scanf("%lf", C + i); + pr = N[i] = -1; + P[i] = C[i]; + for (j = 0; j < i; j++) + { + dist = 2.0 * sqrt(C[i] * C[j]) + P[j]; + if (dist > P[i]) + { + pr = j; + P[i] = dist; + } + } + if (pr == -1) + { + f = i; + } + else + { + N[pr] = i; + } + } + for (i = 0, dist = 0.0; i < n; i++) + { + if (P[i] + C[i] > dist) + { + dist = P[i] + C[i]; + pr = i; + } + } + for (i = pr; i < n; i++) + { + N[i] = -1; + } + for (tn = 0, i = f; i != -1; i = N[i], tn++) + ; + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + printf("%.1lf\n%d\n", dist, tn); + for (; f != -1; f = N[f]) + { + printf("%d\n", f + 1); + } + } + return 0; +} diff --git a/917.cpp b/917.cpp new file mode 100644 index 0000000..461dadc --- /dev/null +++ b/917.cpp @@ -0,0 +1,188 @@ +#include + +using namespace std; + +struct Game +{ + int team1, team2; + int goals1, goals2; + Game(int t1, int t2, int g1, int g2) : team1(t1), team2(t2), goals1(g1), goals2(g2) {} +}; + +vector games; + +struct Team +{ + int id, place; + string name; + + int p[4]; + + bool operator<(const Team &b) const + { + for (int i = 0; i < 4; i++) + if (p[i] != b.p[i]) + { + return p[i] > b.p[i]; + } + return name < b.name; + } + + bool same(const Team &b) const + { + for (int i = 0; i < 4; i++) + if (p[i] != b.p[i]) + { + return false; + } + return true; + } + + Team(string s) : name(s) {} +}; + +vector teams; +int loc[100000]; + +void Sort(int a, int b, int level = 0) +{ + if (a > b) + { + return; + } + if (a == b) + { + teams[a].place = a; + return; + } + for (int i = a; i <= b; i++) + { + Team &t = teams[i]; + loc[t.id] = i; + t.p[0] = t.p[1] = t.p[2] = t.p[3] = 0; + } + for (int k = 0; k < (int)games.size(); k++) + { + const Game &G = games[k]; + if (loc[G.team1] < 0 || loc[G.team2] < 0) + { + continue; + } + Team &a = teams[loc[G.team1]]; + Team &b = teams[loc[G.team2]]; + a.p[1] += G.goals1 - G.goals2; + a.p[3] += G.goals1; + b.p[1] += G.goals2 - G.goals1; + b.p[3] += G.goals2; + if (G.goals1 > G.goals2) + { + a.p[0] += 3; + a.p[2]++; + } + else if (G.goals1 == G.goals2) + { + a.p[0]++; + b.p[0]++; + } + else + { + b.p[0] += 3; + b.p[2]++; + } + } + for (int i = a; i <= b; i++) + { + loc[teams[i].id] = -1; + } + sort(teams.begin() + a, teams.begin() + b + 1); + int s = 4; + for (int i = a; i < b; i++) + { + int j; + for (j = 0; j < s && teams[i].p[j] == teams[i + 1].p[j]; j++) + ; + if (j < s) + { + s = j; + } + } +#if 0 + cout << string(level,' ') << "Sort(a=" << a << ", b=" << b << "):" << endl; + for(int i = a; i <= b; i++) + { + Team &t = teams[i]; + cout << string(level, ' ') << t.name; + cout << " pt=" << t.p[0] << " diff=" << t.p[1] << " wins=" << t.p[2] << " scored=" << t.p[3] << endl; + } + cout << string(level, ' ') << "s=" << s << endl; +#endif + if (s == 4) + { + for (int i = a; i <= b; i++) + { + teams[i].place = a; + } + } + else if (s == 0) + { + for (int i = a, j; i <= b; i = j) + { + for (j = i + 1; j <= b && teams[i].p[s] == teams[j].p[s]; j++) + ; + Sort(i, j - 1, level + 2); + } + } + else + { + for (int i = a, j; i <= b; i = j) + { + for (j = i; j <= b && teams[i].same(teams[j]); j++) + { + teams[j].place = i; + } + } + } +} + +int main() +{ + int T, G; + memset(loc, 0xff, sizeof(loc)); + for (scanf("%d", &T); T-- > 0;) + { + assert(scanf("%d", &G) == 1 && G >= 1); + map nm; + games.clear(); + teams.clear(); + for (int i = 0; i < G; i++) + { + char name1[100], name2[100]; + int goals1, goals2; + assert(scanf(" %s %s %d %d", name1, name2, &goals1, &goals2) == 4); + if (nm.count(name1) == 0) + { + nm[name1] = teams.size(); + teams.push_back(Team(name1)); + teams.back().id = teams.size() - 1; + } + if (nm.count(name2) == 0) + { + nm[name2] = teams.size(); + teams.push_back(Team(name2)); + teams.back().id = teams.size() - 1; + } + games.push_back(Game(nm[name1], nm[name2], goals1, goals2)); + } + int N = teams.size(); + Sort(0, N - 1); + for (int i = 0; i < N; i++) + { + Team &t = teams[i]; + printf("%d %s\n", t.place + 1, t.name.c_str()); + } + if (T > 0) + { + printf("\n"); + } + } +} diff --git a/918.cpp b/918.cpp new file mode 100644 index 0000000..3a4e426 --- /dev/null +++ b/918.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +#define eps 1e-12 +char w[16]; +int main() +{ + int t, first = 1, it, i, j; + double r, c, cr, cc, nr, nc, minc, minr, maxc, maxr, pc, pr; + scanf("%d", &t); + while (t--) + { + scanf("%s%lf%lf%lf%lf%lf%lf", w, &minc, &maxc, &pc, &minr, &maxr, &pr); + maxc += eps, maxr += eps; + w[13] = ' '; + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + for (cc = minc, i = 0; cc <= maxc; i++, cc = minc + i * pc) + { + for (cr = minr, j = 0; cr <= maxr; j++, cr = minr + j * pr) + { + r = c = 0.0; + for (it = 0; r * r + c * c < 4 + eps && it < 13; it++) + { + nr = r * r - c * c + cr; + nc = 2 * r * c + cc; + r = nr, c = nc; + } + printf("%c", w[it]); + } + printf("\n"); + } + } + return 0; +} diff --git a/920.cpp b/920.cpp new file mode 100644 index 0000000..a2396f9 --- /dev/null +++ b/920.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +#define MID(x, y) ((x + y) >> 1) +#define L(x) (x << 1) +#define R(x) (x << 1 | 1) +#define BUG puts("here!!!") +#define STOP system("pause") + +const int MAX = 110; +struct point +{ + double x, y; + bool peak; +}; +point p[MAX]; +const double eps = 1e-6; +bool dy(double x, double y) +{ + return x > y + eps;// x > y +} +bool xy(double x, double y) +{ + return x < y - eps;// x < y +} +bool dyd(double x, double y) +{ + return x > y - eps;// x >= y +} +bool xyd(double x, double y) +{ + return x < y + eps;// x <= y +} +bool dd(double x, double y) +{ + return fabs(x - y) < eps;// x == y +} +double disp2p(point a, point b)// a b 两点之间的è·ç¦» +{ + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} +bool cmp(point a, point b) +{ + return a.x < b.x; +} +point l2l_inst_p(point u1, point u2, point v1, point v2) +{ + point ans = u1; + double t = ((u1.x - v1.x) * (v1.y - v2.y) - (u1.y - v1.y) * (v1.x - v2.x)) / + ((u1.x - u2.x) * (v1.y - v2.y) - (u1.y - u2.y) * (v1.x - v2.x)); + ans.x += (u2.x - u1.x) * t; + ans.y += (u2.y - u1.y) * t; + return ans; +} +int main() +{ + int ncases, n; + scanf("%d", &ncases); + while (ncases--) + { + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + scanf("%lf%lf", &p[i].x, &p[i].y); + p[i].peak = false; + } + sort(p, p + n, cmp); + int i = 0; + if (p[0].y < p[1].y)//如果第一个点是山谷,那么第二个点一定是山顶 + { + i = 1; + } + for (; i < n; i += 2) + { + p[i].peak = true;//标记山顶的点 + } + double len = 0.0; + for (int i = 0; i < n; i++) + { + if (p[i].peak) + { + double high = 0.0; + for (int k = i + 2; k < n; k++) + if (dy(p[k].y, high)) + { + high = p[k].y; + } + if (dyd(high, p[i].y)) + { + continue; + } + point t; + t.x = -1; + t.y = high; + point tt = t; + tt.x = -2; + point c = l2l_inst_p(tt, t, p[i], p[i + 1]); + len += disp2p(c, p[i]); + } + } + printf("%.2lf\n", len); + } + return 0; +} diff --git a/921.cpp b/921.cpp new file mode 100644 index 0000000..93b9d2b --- /dev/null +++ b/921.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int match[105][105], msize[105]; +char seed[105], dictionary[105][105]; +int dlen[105]; +int mp[52]; +int N, M, D; +int dfs(int idx) +{ + if (idx == N) + { + int f = 1; + for (int i = 1; i <= M; i++) + f &= mp[i] != -1; + return f; + } + for (int i = 0; i < D; i++) + { + if (dlen[i] != msize[idx]) + continue; + int ok = 1; + for (int j = 0; j < msize[idx] && ok; j++) + { + if (mp[match[idx][j]] != -1 && mp[match[idx][j]] != dictionary[i][j]) + ok = 0; + } + if (ok) + { + int cp[52]; + for (int i = 1; i <= M; i++) + cp[i] = mp[i]; + for (int j = 0; j < msize[idx] && ok; j++) + mp[match[idx][j]] = dictionary[i][j]; + if (dfs(idx + 1)) + return 1; + for (int i = 1; i <= M; i++) + mp[i] = cp[i]; + } + } + return 0; +} +int main() +{ + int testcase; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &M, &N); + for (int i = 0; i < N; i++) + { + int j; + for (j = 0; scanf("%d", &match[i][j]) && match[i][j]; j++) + ; + msize[i] = j; + } + scanf("%s", seed); + memset(mp, -1, sizeof(mp)); + for (int i = 0; seed[i]; i++) + mp[match[0][i]] = seed[i]; + for (D = 0; scanf("%s", &dictionary[D]) && dictionary[D][0] != '*'; D++) + { + dlen[D] = strlen(dictionary[D]); + } + dfs(0); + for (int i = 1; i <= M; i++) + printf("%c", mp[i]); + puts(""); + } + return 0; +} diff --git a/922.cpp b/922.cpp new file mode 100644 index 0000000..2d83e2d --- /dev/null +++ b/922.cpp @@ -0,0 +1,114 @@ +#include + +using namespace std; + +/** +1. At first you have to find the smallest grid to have the map. So certainly you have to determine the highest x, lowest x, highest y,lowest y value of the map. +2. Then you have to create each possible rectangle within that smallest grid through 4 nested loops.(I solved this in this way(O(n^5)). There may be other efficient algorithms) +3. Now in each iteration you have to check that if the area of current rectangle is closest to the map than result. +4. If (3) true then check if at least three points of current rectangle lies on the map. +5. If (4) is true save the current rectangle to the result. +*/ + +#define MAXPOLY 300 + +struct point +{ + int x; + int y; +}; + +struct rect +{ + int left, lower; + int right, upper; +}; + +struct polygon +{ + int n; + int top, left, right, bottom; + point p[MAXPOLY]; +}; + +int on[MAXPOLY][MAXPOLY]; + +void rectangle(polygon &contour, double area) +{ + double minDiff = area, currentDiff; + rect tmp; + for (int i = 0; i <= (contour.right - contour.left); i++) + for (int j = 0; j <= (contour.top - contour.bottom); j++) + { + on[i][j] = 0; + } + for (int i = 0; i < contour.n; i++) + { + on[contour.p[i].x - contour.left][contour.p[i].y - contour.bottom] = 1; + } + for (int i = 0; i <= contour.right - contour.left; i++) + for (int j = 0; j <= contour.top - contour.bottom; j++) + for (int m = i; m <= contour.right - contour.left; m++) + for (int n = j; n <= contour.top - contour.bottom; n++) + { + if (on[i][j] + on[i][n] + + on[m][j] + on[m][n] < + 3) + { + continue; + } + currentDiff = fabs((m - i) * (n - j) - area); + if (currentDiff < minDiff) + { + minDiff = currentDiff; + tmp = (rect){ + contour.left + i, + contour.bottom + j, + contour.left + m, + contour.bottom + n}; + } + } + cout << ((int)(area)) << "." << ((int)(area * 10.0) % 10) << " "; + cout << tmp.left << " " << tmp.lower << " "; + cout << tmp.right << " " << tmp.upper << endl; +} + +double calArea(polygon &contour) +{ + double total = 0.0; + for (int i = 0; i < contour.n; i++) + { + int j = (i + 1) % contour.n; + total += (contour.p[i].x * contour.p[j].y - + contour.p[j].x * contour.p[i].y); + } + return fabs(total / 2.0); +} + +int main(int ac, char *av[]) +{ + int cases; + polygon contour; + cin >> cases; + while (cases--) + { + cin >> contour.n; + for (int i = 0; i < contour.n; i++) + { + cin >> contour.p[i].x >> contour.p[i].y; + } + contour.top = contour.p[0].y; + contour.left = contour.p[0].x; + contour.bottom = contour.p[0].y; + contour.right = contour.p[0].x; + for (int i = 1; i < contour.n; i++) + { + contour.top = max(contour.top, contour.p[i].y); + contour.left = min(contour.left, contour.p[i].x); + contour.bottom = min(contour.bottom, contour.p[i].y); + contour.right = max(contour.right, contour.p[i].x); + } + rectangle(contour, calArea(contour)); + } + return 0; +} diff --git a/924.cpp b/924.cpp new file mode 100644 index 0000000..1cf6441 --- /dev/null +++ b/924.cpp @@ -0,0 +1,152 @@ +#include + +using namespace std; + +struct Vertex +{ + vdata id; + vector adj; + Vertex(vdata id) : id(id) {} +}; +typedef map vmap; +typedef pair vpair; +typedef int vdata; + +bool visited[2501]; +map stats; + +class Graph +{ + Graph() {} + ~Graph(); + Vertex *addVertex(vdata v); + void addEdge(vdata begin, vdata end, bool directed); + Vertex *firstVertex(); + void solve(int source, int day); + + Vertex *getVertex(vdata v); + vmap vertexMap; + vector allVertexes; +}; + +Graph::~Graph() +{ + for (int i = 0; i < allVertexes.size(); i++) + { + delete allVertexes[i]; + } +} + +Vertex *Graph::addVertex(vdata v) +{ + Vertex *newv = new Vertex(v); + allVertexes.push_back(newv); + vertexMap.insert(vpair(v, newv)); + return newv; +} + +void Graph::addEdge(vdata begin, vdata end, bool directed = false) +{ + Vertex *v = getVertex(begin); + Vertex *w = getVertex(end); + v->adj.push_back(w); + if (!directed) + { + w->adj.push_back(v); + } +} + +Vertex *Graph::getVertex(vdata v) +{ + vmap::iterator it = vertexMap.find(v); + if (it == vertexMap.end()) + { + return addVertex(v); + } + return (*it).second; +} + +Vertex *Graph::firstVertex() +{ + return allVertexes[0]; +} + +void Graph::solve(int source, int day) +{ + pair p; + queue> q; + q.push(make_pair(source, day)); + visited[source] = true; + while (!q.empty()) + { + p = q.front(); + q.pop(); + Vertex *v = getVertex(p.first); + for (int i = 0, sz = v->adj.size(); i < sz; i++) + { + if (visited[v->adj[i]->id] == false) + { + visited[v->adj[i]->id] = true; + q.push(make_pair(v->adj[i]->id, p.second + 1)); + if (stats.find(p.second + 1) == stats.end()) + { + stats[p.second + 1] = 1; + } + else + { + stats[p.second + 1] += 1; + } + } + } + } +} + +int main() +{ + int e, n, f, t, source; + Graph g; + cin >> e; + for (int i = 0; i < e; i++) + { + cin >> n; + while (n--) + { + cin >> f; + g.addEdge(i, f, true); + } + } + cin >> t; + while (t--) + { + for (int i = 0; i < 2501; i++) + { + visited[i] = false; + } + stats.clear(); + cin >> source; + g.solve(source, 0); + if (stats.find(1) == stats.end()) + { + cout << 0 << endl; + } + else + { + int best = -1; + int bestday = -1; + for (int i = 1; i < 100000; i++) + { + if (stats.find(i) == stats.end()) + { + break; + } + if (stats[i] > best) + { + best = stats[i]; + bestday = i; + } + } + cout << best << " " << bestday << endl; + } + } + return 0; +} diff --git a/925.cpp b/925.cpp new file mode 100644 index 0000000..0ff5bae --- /dev/null +++ b/925.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +typedef struct _trie +{ + int id, u; + struct _trie *ch[26]; +} trie; +trie *root; +int ID, L[128][128] = {0}, len; +char C[128][25]; +trie *make() +{ + trie *r = (trie *)malloc(sizeof(trie)); + int i; + r->u = 0; + for (i = 0; i < 26; i++) + { + r->ch[i] = NULL; + } + return r; +} +void insert(char s[]) +{ + trie *proc = root; + int i, j; + for (i = 0; s[i]; i++) + { + j = s[i] - 'a'; + if (proc->ch[j] == NULL) + { + proc->ch[j] = make(); + } + proc = proc->ch[j]; + } + proc->u = 1; +} +int find(char s[]) +{ + trie *proc = root; + int i, j; + for (i = 0; s[i]; i++) + { + j = s[i] - 'a'; + if (proc->ch[j] == NULL) + { + return -1; + } + proc = proc->ch[j]; + } + if (proc->u) + { + return proc->id; + } + else + { + return -1; + } +} +void name(trie *x, int lv) +{ + if (x == NULL) + { + return; + } + int i, j; + if (x->u) + { + x->id = ID; + C[ID++][lv] = '\0'; + for (i = 0; i < lv; i++) + { + C[ID][i] = C[ID - 1][i]; + } + } + for (i = 0; i < 26; i++) + { + C[ID][lv] = 'a' + i; + name(x->ch[i], lv + 1); + } +} +void des(trie *x) +{ + if (x == NULL) + { + return; + } + int i; + for (i = 0; i < 26; i++) + { + des(x->ch[i]); + } + free(x); +} +int main() +{ + int t, n, m, r, x, y, i, j, k; + char in[25]; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + root = make(); + for (i = 0; i < n; i++) + { + scanf("%s", in); + insert(in); + for (j = 0; j < n; j++) + { + L[i][j] = 0; + } + } + ID = 0; + scanf("%d", &r); + name(root, 0); + for (i = 0; i < r; i++) + { + scanf("%s", in); + x = find(in); + scanf("%d", &len); + for (j = 0; j < len; j++) + { + scanf("%s", in); + y = find(in); + L[x][y] = 1; + } + } + for (j = 0; j < n; j++) + { + for (i = 0; i < n; i++) + { + if (i == j) + { + continue; + } + for (k = 0; k < n; k++) + { + if (i == k || j == k) + { + continue; + } + if (L[i][j] && L[j][k]) + { + L[i][k] = 2; + } + } + } + } + for (i = 0; i < n; i++) + { + for (j = len = 0; j < n; j++) + { + if (L[i][j] == 1) + { + len++; + } + } + if (len) + { + printf("%s %d", C[i], len); + } + else + { + continue; + } + for (j = 0; j < n; j++) + { + if (L[i][j] == 1) + { + printf(" %s", C[j]); + } + } + printf("\n"); + } + des(root); + } + return 0; +} diff --git a/926.cpp b/926.cpp new file mode 100644 index 0000000..8a43530 --- /dev/null +++ b/926.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + +#define maxn 32 + +typedef long long LONG; + +int Sx, Sy, Tx, Ty, N; +char Visited[maxn][maxn]; +LONG Ways[maxn][maxn]; + +struct ss +{ + bool up, left; +}; +ss No[maxn][maxn]; + +void setNo() +{ + int i, j; + for (i = 0; i <= N; i++) + for (j = 0; j <= N; j++) + No[i][j].left = No[i][j].up = true; +} + +void setDir(int x, int y, char dir) +{ + switch (dir) + { + case 'S': + No[x][y - 1].up = false; + break; + case 'W': + No[x - 1][y].left = false; + break; + case 'E': + No[x][y].left = false; + break; + case 'N': + No[x][y].up = false; + break; + } +} + +LONG countWays(int x, int y) +{ + if (Tx == x && y == Ty) + return 1; + if (x > N || y > N) + return 0; + if (x > Tx || y > Ty) + return 0; + LONG ways = 0; + if (Visited[x][y] == 1) + return Ways[x][y]; + Visited[x][y] = 1; + if (No[x][y].left) + ways += countWays(x + 1, y); + if (No[x][y].up) + ways += countWays(x, y + 1); + Ways[x][y] = ways; + return ways; +} + +int isPossible() +{ + if (Sx > Tx || Sy > Ty) + return 0; + return 1; +} + +void Cal() +{ + if (isPossible() == 0) + { + printf("0\n"); + return; + } + LONG ways = countWays(Sx, Sy); + printf("%lld\n", ways); +} + +void makeFree() +{ + int i, j; + for (i = 0; i <= N; i++) + { + for (j = 0; j <= N; j++) + { + Visited[i][j] = 0; + Ways[i][j] = 0; + } + } +} + +void readCase() +{ + int w, x, y; + char dir[10]; + scanf("%d", &N); + setNo(); + scanf("%d%d", &Sx, &Sy); + scanf("%d%d", &Tx, &Ty); + scanf("%d", &w); + while (w--) + { + scanf("%d%d", &x, &y); + scanf("%s", dir); + setDir(x, y, dir[0]); + } +} + +int main() +{ + int kase; + scanf("%d", &kase); + while (kase--) + { + readCase(); + Cal(); + makeFree(); + } + return 0; +} diff --git a/927.cpp b/927.cpp new file mode 100644 index 0000000..f78a0f9 --- /dev/null +++ b/927.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() +{ + int t, n, i; + long long int d, k, x, sum, C[25], tmp; + scanf("%d", &t); + while (t--) + { + scanf("%d", &n); + for (i = 0; i <= n; i++) + { + scanf("%lld", &C[i]); + } + scanf("%lld%lld", &d, &k); + k = (k - 1) / d + 1; + x = sqrt(k); + while (k > x * (x + 1) / 2) + { + x++; + } + sum = 0LL, tmp = 1LL; + for (i = 0; i <= n; i++) + { + sum += C[i] * tmp; + tmp *= x; + } + printf("%lld\n", sum); + } + return 0; +} diff --git a/928.cpp b/928.cpp new file mode 100644 index 0000000..3778106 --- /dev/null +++ b/928.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +#define VALID(I, J, N, M) (I) >= 0 && (J) >= 0 && (I) < (N) && (J) < (M) +#define INF 1 << 29 + +typedef pair Point; + +int main() +{ + int dx[] = {-1, 0, 0, 1}; + int dy[] = {0, -1, 1, 0}; + int T, N, M, i, j, sourceX, sourceY, endX, endY; + int prevMoves = 0, curMoves = 1, curX, curY, nextX, nextY; + int delta, bad, JUMP; + cin >> T; + while (T--) + { + cin >> N >> M; + char graph[N][M]; + int d[N][M][4]; + for (i = 0; i < N; i++) + for (j = 0; j < M; j++) + { + cin >> graph[i][j]; + d[i][j][1] = d[i][j][2] = d[i][j][3] = INF; + if (graph[i][j] == 'S') + { + sourceY = i; + sourceX = j; + } + if (graph[i][j] == 'E') + { + endY = i; + endX = j; + } + } + queue> que; + d[sourceY][sourceX][0] = 0; + que.push(pair(Point(sourceY, sourceX), 0)); + bool found = false; + while (!que.empty() && !found) + { + curY = que.front().first.first, curX = que.front().first.second; + prevMoves = que.front().second, curMoves = prevMoves % 3 + 1; + que.pop(); + for (delta = 0; delta < 4; delta++) + { + nextY = curY + dy[delta] * curMoves, nextX = curX + dx[delta] * curMoves; + if (VALID(nextY, nextX, N, M) && graph[nextY][nextX] != '#' && d[nextY][nextX][curMoves] == INF) + { + for (JUMP = 1, bad = false; JUMP <= curMoves; JUMP++) + { + bad = bad || graph[curY + dy[delta] * JUMP][curX + dx[delta] * JUMP] == '#'; + } + if (bad) + { + continue; + } + d[nextY][nextX][curMoves] = d[curY][curX][prevMoves] + 1; + if (nextY == endY && nextX == endX) + { + found = true; + } + que.push(pair(Point(nextY, nextX), curMoves)); + } + } + } + if (!found) + { + cout << "NO" << endl; + } + else + { + cout << d[endY][endX][curMoves] << endl; + } + } + return 0; +} diff --git a/929.cpp b/929.cpp new file mode 100644 index 0000000..af1f525 --- /dev/null +++ b/929.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +#define FOR(i, x, y) for (int i = (int)(x); i <= (int)(y); ++i) +#define REP(i, n) FOR(i, 0, n - 1) +#define FORD(i, x, y) for (int i = (int)(x); i >= (int)(y); --i) +#define ALL(a) (a).begin(), (a).end() +#define FORE(t, i) for (typeof(t.begin()) i = t.begin(); i != t.end(); ++i) +template +string tostr(const T &t) +{ + ostringstream os; + os << t; + return os.str(); +} + +struct node +{ + int cost; + int x; + int y; + node(int ncost, int nx, int ny) + { + cost = ncost; + x = nx; + y = ny; + } +}; + +bool operator<(const node &a, const node &b) +{ + if (a.cost != b.cost) + { + return b.cost < a.cost; + } + return false; +} + +int dx[] = {1, 0, -1, 0}; +int dy[] = {0, 1, 0, -1}; +int D[1001][1001]; +int v[1001][1001]; +int INF = 987654321; + +int dijkstra(int v[1001][1001], int nrows, int ncols) +{ + priority_queue q; + for (int i = 0; i < nrows; ++i) + for (int j = 0; j < ncols; ++j) + { + D[i][j] = INF; + } + q.push(node(v[0][0], 0, 0)); + D[0][0] = v[0][0]; + while (!q.empty()) + { + node s = q.top(); + q.pop(); + if (s.x == nrows - 1 && s.y == ncols - 1) + { + return s.cost; + } + for (int i = 0; i < 4; ++i) + { + int nx = s.x + dx[i]; + int ny = s.y + dy[i]; + if (nx >= 0 && nx < nrows && ny >= 0 && ny < ncols) + { + if (D[nx][ny] > D[s.x][s.y] + v[nx][ny]) + { + D[nx][ny] = D[s.x][s.y] + v[nx][ny]; + q.push(node(D[s.x][s.y] + v[nx][ny], nx, ny)); + } + } + } + } + return -1; +} + +int main() +{ + int nboards; + scanf("%d", &nboards); + for (int k = 0; k < nboards; ++k) + { + int ncols, nrows; + scanf("%d\n%d\n", &nrows, &ncols); + for (int i = 0; i < nrows; ++i) + { + char line[3000]; + gets(line); + for (int j = 0; j < ncols; ++j) + { + v[i][j] = line[j * 2] - '0'; + } + } + printf("%d\n", dijkstra(v, nrows, ncols)); + } + return 0; +} diff --git a/930.cpp b/930.cpp new file mode 100644 index 0000000..443a590 --- /dev/null +++ b/930.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +int main() +{ + int testcase, n; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &n); + double A[128] = {}, x; + for (int i = n; i >= 0; i--) + scanf("%lf", &A[i]); + for (int i = 0; i < n - 2; i++) + { + scanf("%lf", &x); + + double prev = A[n]; + for (int j = n - 1; j >= 0; j--) + A[j] += prev * x, prev = A[j]; + } + + double a = A[n], b = A[n - 1], c = A[n - 2]; + double r1, r2; + r1 = (-b + sqrt(b * b - 4 * a * c)) / 2.0 / a; + r2 = (-b - sqrt(b * b - 4 * a * c)) / 2.0 / a; + printf("%.1lf\n%.1lf\n", r1, r2); + } + return 0; +} diff --git a/932.cpp b/932.cpp new file mode 100644 index 0000000..502a791 --- /dev/null +++ b/932.cpp @@ -0,0 +1,105 @@ +#include + +using namespace std; + +int check(int qx[], int qy[], int n) +{ + static int used[32] = {}, testcase = 0; + testcase++; + for (int i = 0; i < n; i++) + { + if (used[qx[i]] == testcase) + return 0; + used[qx[i]] = testcase; + } + testcase++; + for (int i = 0; i < n; i++) + { + if (used[qy[i]] == testcase) + return 0; + used[qy[i]] = testcase; + } + for (int i = 0; i < n; i++) + { + for (int j = i + 1; j < n; j++) + { + if (abs(qx[i] - qx[j]) == abs(qy[i] - qy[j])) + return 0; + } + } + return 1; +} +int main() +{ + int n, cases = 0, tx, ty; + char g[32][32]; + const int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}; + const int dy[] = {1, -1, 0, 1, -1, 0, 1, -1}; + while (scanf("%d", &n) == 1) + { + if (cases++) + puts(""); + for (int i = 0; i < n; i++) + scanf("%s", g[i]); + int qx[32], qy[32], m = 0; + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + if (g[i][j] == 'X') + qx[m] = i, qy[m] = j, m++; + } + } + if (check(qx, qy, m)) + puts("YES"); + else + { + puts("NO"); + int ok = 0; + for (int i = 0; i < m && !ok; i++) + { + for (int j = 0; j < 8 && !ok; j++) + { + tx = qx[i] + dx[j], ty = qy[i] + dy[j]; + while (true) + { + if (tx < 0 || ty < 0 || tx >= n || ty >= n) + break; + if (g[tx][ty] == '0') + { + swap(tx, qx[i]), swap(ty, qy[i]); + if (check(qx, qy, m)) + { + puts("YES"); + ok = 1; + swap(g[qx[i]][qy[i]], g[tx][ty]); + for (int i = 0; i < n; i++) + puts(g[i]); + break; + } + swap(tx, qx[i]), swap(ty, qy[i]); + } + tx += dx[j], ty += dy[j]; + } + } + } + if (!ok) + puts("NO"); + } + } + return 0; +} +/* +5 +00X00 +X0000 +000X0 +0X000 +0000X +5 +0X000 +X0000 +000X0 +0X000 +0000X +*/ diff --git a/933.cpp b/933.cpp new file mode 100644 index 0000000..26e161f --- /dev/null +++ b/933.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +map R; +vector RN; +void parse_equ(string exp) +{ + char s[10]; + int val; + sscanf(exp.c_str(), "%[a-zA-Z]=%d", s, &val); + RN.push_back(val); + R[s] = val; +} +void parse_exp(string exp) +{ + size_t pos = exp.find(";"); + if (pos == string::npos) + { + parse_equ(exp); + return; + } + parse_exp(exp.substr(0, pos)); + parse_exp(exp.substr(pos + 1)); +} +int readLine() +{ + string line; + if (!getline(cin, line)) + return 0; + parse_exp(line); + return 1; +} +int main() +{ + { + readLine(); + readLine(); + readLine(); + int N = RN[0], A[26], V[26]; + int sumA = 0; + for (int i = 0; i < N; i++) + A[i] = RN[i + 1], sumA += A[i]; + for (int i = 0; i < N - 1; i++) + V[i] = RN[i + N + 1]; + + printf("%3c", 't'); + for (int i = 0; i < N; i++) + printf("%4c", i + 'A'); + puts(""); + printf("---"); + for (int i = 0; i < N; i++) + printf("----"); + puts(""); + + int time = 0; + do + { + printf("%3d", time); + for (int i = 0; i < N; i++) + printf("%4d", A[i]); + puts(""); + if (A[N - 1] == sumA) + break; + for (int i = N - 1; i >= 1; i--) + { + int f = min(A[i - 1], V[i - 1]); + A[i] += f, A[i - 1] -= f; + } + time++; + } while (true); + + R.clear(); + RN.clear(); + } + return 0; +} + +/* +N=3 +A=11;B=0;C=0 +a=5;b=4 + +N=3 +A=11;B=0;C=0 +a=5;c=4 +*/ \ No newline at end of file diff --git a/938.cpp b/938.cpp new file mode 100644 index 0000000..a1f296e --- /dev/null +++ b/938.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +int g[512][512]; +int dist[512][512][2], inq[512][512][2]; +const int dx1[] = {-1, -1, 0, 1, 1, 0}; +const int dy1[] = {0, 1, 1, 1, 0, -1}; +const int dx2[] = {-1, -1, 0, 1, 1, 0}; +const int dy2[] = {-1, 0, 1, 0, -1, -1}; +void spfa(int L, int C, int sx, int sy, int ex, int ey, int px, int py) +{ + memset(dist, 63, sizeof(dist)); + queue X, Y, S; + int tx, ty, ts, ss; + dist[sx][sy][0] = 0; + X.push(sx), Y.push(sy), S.push(0); + while (!X.empty()) + { + sx = X.front(), X.pop(); + sy = Y.front(), Y.pop(); + ss = S.front(), S.pop(); + inq[sx][sy][ss] = 0; + for (int i = 0; i < 6; i++) + { + if (sx & 1) + tx = sx + dx1[i], ty = sy + dy1[i]; + else + tx = sx + dx2[i], ty = sy + dy2[i]; + if (tx < 0 || tx >= L) + continue; + ty = (ty + C) % C; + ts = ss; + int cost = ts ? g[tx][ty] / 2 : g[tx][ty]; + if (tx == px && ty == py) + ts = ss | 1; + if (dist[tx][ty][ts] > dist[sx][sy][ss] + cost) + { + dist[tx][ty][ts] = dist[sx][sy][ss] + cost; + if (!inq[tx][ty][ts]) + { + inq[tx][ty][ts] = 1; + X.push(tx), Y.push(ty), S.push(ts); + } + } + } + } + printf("%d\n", min(dist[ex][ey][0], dist[ex][ey][1])); +} +int main() +{ + int L, C; + int sx, sy, ex, ey, px, py; + while (scanf("%d %d", &L, &C) == 2) + { + for (int i = 0; i < L; i++) + { + for (int j = 0; j < C; j++) + scanf("%d", &g[i][j]); + } + scanf("%d %d %d %d", &sx, &sy, &ex, &ey); + scanf("%d %d", &px, &py); + spfa(L, C, sx, sy, ex, ey, px, py); + } + return 0; +} +/* +4 8 +4 2 2 2 4 4 6 10 +2 6 8 4 4 4 4 2 +8 2 6 8 4 4 4 6 +6 4 4 6 8 4 4 4 +0 0 +3 4 +1 1 +*/ diff --git a/939.cpp b/939.cpp new file mode 100644 index 0000000..852939c --- /dev/null +++ b/939.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +const char *types[] = {"non-existent", "recessive", "dominant"}; + +struct person +{ + char n[32]; + int s, t; + bool operator<(const person &p) const + { + return strcmp(n, p.n) < 0; + } + void ct() + { + t = (s >= 2) + (s > 2); + } +} p[1 << 12]; +struct edge +{ + int next, to; +} e[1 << 12]; + +int head[1 << 12], deg[1 << 12], q[1 << 12]; +int ecnt, pn; +map m; + +int get(char *s) +{ + int &z = m[s]; + if (!z) + { + z = ++pn; + p[z].s = deg[z] = 0; + head[z] = -1; + strcpy(p[z].n, s); + } + return z; +} + +void add_edge(int from, int to) +{ + edge &E = e[ecnt]; + E.to = to; + E.next = head[from]; + ++deg[to]; + head[from] = ecnt++; +} + +int main(void) +{ + char name[32], rel[32]; + for (int n; scanf("%d", &n) == 1 && n;) + { + ecnt = pn = 0; + m.clear(); + for (int a, i = 0; i < n; ++i) + { + scanf("%s %s", name, rel); + a = get(name); + if (!strcmp(rel, "dominant")) + { + p[a].t = 2, p[a].s = 3; + } + else if (!strcmp(rel, "non-existent")) + { + p[a].t = 0, p[a].s = 0; + } + else if (!strcmp(rel, "recessive")) + { + p[a].t = 1, p[a].s = 2; + } + else + { + add_edge(a, get(rel)); + } + } + int t = 0; + for (int i = 1; i <= pn; ++i) + if (!deg[i]) + { + q[t++] = i; + } + for (int h = 0; h < t; ++h) + { + p[q[h]].ct(); + for (int at = head[q[h]]; at != -1; at = e[at].next) + { + p[e[at].to].s += p[q[h]].t; + if (!--deg[e[at].to]) + { + q[t++] = e[at].to; + } + } + } + sort(p + 1, p + 1 + pn); + for (int i = 1; i <= pn; ++i) + { + printf("%s %s\n", p[i].n, types[p[i].t]); + } + } + return 0; +} diff --git a/941.cpp b/941.cpp index 9aa6ead..0ce3f22 100644 --- a/941.cpp +++ b/941.cpp @@ -1,47 +1,39 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int test; - while(test--){ - char str[25]; - int N; - cin>>str>>N; - int len=strlen(str); - sort(str, str+len); - while(N--){ - next_permutation(str, str+len); - } - cout< + +using namespace std; + +unsigned long long f[20]; + +int main() +{ + int cases; + unsigned long long n; + string input; + unsigned long long tmp = 1; + f[0] = 1; + for (int i = 1; i < 20; i++) + { + tmp *= i; + f[i] = tmp; + } + cin >> cases; + while (cases--) + { + cin >> input >> n; + n++; + sort(input.begin(), input.end()); + for (int i = 0, sz = input.size(); i < sz; i++) + { + int next = 0; + while (f[input.size() - 1] < n) + { + n -= f[input.size() - 1]; + next++; + } + cout << input[next]; + input.erase(input.begin() + next); + } + cout << endl; + } + return 0; +} diff --git a/942.cpp b/942.cpp new file mode 100644 index 0000000..8ffca39 --- /dev/null +++ b/942.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; + +#define MAXN 1048576 +char buf[MAXN]; +int main() +{ + int testcase, n, m; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d %d", &n, &m); + printf("%d", n / m); + map R; + int t = 0, back = -1; + printf("."); + n %= m; + R[n] = -1; + for (int i = 0; n; i++, t++) + { + buf[i] = (n * 10) / m + '0'; + n = (n * 10) % m; + if (R.count(n)) + { + buf[i + 1] = n * 10 / m + '0'; + back = R[n] + 1, t++; + break; + } + R[n] = i; + } + if (t == 0) + { + t++, buf[0] = '0'; + } + for (int i = 0; i < t; i++) + { + if (i == back && n) + printf("("); + putchar(buf[i]); + } + if (back != -1 && n) + printf(")"); + puts(""); + } + return 0; +} diff --git a/944.cpp b/944.cpp new file mode 100644 index 0000000..4710738 --- /dev/null +++ b/944.cpp @@ -0,0 +1,106 @@ +#include + +using namespace std; + +struct num +{ + int happy;// 0 - unknown, 1 - happy, -1 - unhappy + int steps; + + num() : happy(0) {} +}; + +int calc(int n) +{ + int tmp; + int result = 0; + while (n > 0) + { + tmp = n % 10; + n /= 10; + result += tmp * tmp; + } + return result; +} + +int main() +{ + int next; + int tmp; + bool first = true; + int s, e; + vector history; + bool *visited = new bool[100000]; + num *numbers = new num[100000]; + memset(visited, false, sizeof(bool) * 100000); + numbers[1].happy = 1; + numbers[1].steps = 1; + visited[1] = true; + for (int i = 2; i < 100000; i++) + { + tmp = i; + while (true) + { + next = calc(tmp); + if (numbers[next].happy != 0)// already have data for the node + { + if (numbers[next].happy == 1)// happy number + { + numbers[i].happy = 1; + numbers[i].steps = numbers[next].steps + history.size() + 1; + for (int y = 0, sz = history.size(); y < sz; y++) + { + numbers[history[y]].happy = 1; + numbers[history[y]].steps = numbers[i].steps - 1 - y; + } + history.clear(); + } + else// unhappy number + { + numbers[i].happy = -1; + for (int y = 0, sz = history.size(); y < sz; y++) + { + numbers[history[y]].happy = -1; + } + history.clear(); + } + break; + } + else if (visited[next] == true)// we are in unhappy number loop + { + numbers[i].happy = -1; + for (int y = 0, sz = history.size(); y < sz; y++) + { + numbers[history[y]].happy = -1; + } + history.clear(); + break; + } + else + { + tmp = next; + visited[tmp] = true; + history.push_back(tmp); + } + } + } + while (cin >> s >> e) + { + if (!first) + { + cout << endl; + } + for (int i = s; i <= e; i++) + { + if (numbers[i].happy != 1) + { + continue; + } + cout << i << " " << numbers[i].steps << endl; + } + first = false; + } + delete[] numbers; + delete[] visited; + return 0; +} diff --git a/945.cpp b/945.cpp new file mode 100644 index 0000000..2750c55 --- /dev/null +++ b/945.cpp @@ -0,0 +1,99 @@ +#include + +using namespace std; + +const int MAXN = 1024; +struct Container +{ + int cap, use, id; + int carry; + Container(int a = 0, int b = 0, int c = 0, int d = 0) : cap(a), use(b), carry(c), id(d) {} + bool operator<(const Container &a) const + { + if (carry != a.carry) + return carry < a.carry; + if (cap - use != a.cap - a.use) + return cap - use > a.cap - a.use; + return id < a.id; + } +}; +int main() +{ + int N, M, A[MAXN], C[MAXN]; + int cases = 0; + while (scanf("%d", &N) == 1) + { + int cargo_w = 0, cap_w = 0, load_w = 0; + for (int i = 0; i < N; i++) + scanf("%d", &C[i]); + scanf("%d", &M); + for (int i = 0; i < M; i++) + scanf("%d", &A[i]), load_w += A[i]; + + set pQ; + vector ret[MAXN]; + for (int i = 0; i < N; i++) + { + pQ.insert(Container(C[i], 0, 0, i)); + cap_w += C[i]; + } + for (int i = 0; i < M; i++) + { + Container u = *pQ.begin(); + if (u.use + A[i] > u.cap) + break; + u.use += A[i], u.carry += 1; + cargo_w += A[i], ret[u.id].push_back(A[i]); + pQ.erase(pQ.begin()); + pQ.insert(u); + } + + int mx_carry = 0; + for (int i = 0; i < N; i++) + mx_carry = max(mx_carry, (int)ret[i].size()); + + if (cases++) + puts(""); + for (int i = mx_carry - 1; i >= 0; i--) + { + for (int j = 0; j < N; j++) + { + if (i < ret[j].size()) + { + printf("%d", ret[j][i]); + } + else + { + printf(":"); + } + printf("%c", j == N - 1 ? '\n' : ' '); + } + } + for (int i = 0; i < N; i++) + printf("=%c", i == N - 1 ? '\n' : '='); + for (int i = 0; i < N; i++) + printf("%d%c", i + 1, i == N - 1 ? '\n' : ' '); + puts(""); + printf("cargo weight: %d\n", cargo_w); + printf("unused weight: %d\n", cap_w - cargo_w); + printf("unloaded weight: %d\n", load_w - cargo_w); + } + return 0; +} + +/* + 3 + 5 + 10 + 5 + + 8 + 4 + 3 + 2 + 1 + 1 + 2 + 3 + 4 +*/ \ No newline at end of file diff --git a/946.cpp b/946.cpp new file mode 100644 index 0000000..9f978de --- /dev/null +++ b/946.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +int a, b, c, d, e, f, g, h, i, j, k[101][2], l; +int abcc(int p) +{ + if (!p) + { + return 1; + } + if (k[p][1] == k[p - 1][1] - k[p - 1][0]) + { + return 1; + } + return 0; +} +int main() +{ + while (scanf("%d", &h) == 1) + { + k[0][0] = 0; + k[0][1] = 0; + for (i = 0, j = 1; h > 0; h--) + { + scanf("%d", &a); + if (a > k[0][0] - 1) + { + for (b = j; b > 0; b--) + { + k[b][0] = k[b - 1][0]; + k[b][1] = k[b - 1][1]; + } + k[0][0] = a; + k[0][1] = k[1][1] + a; + i += a; + j++; + } + else + { + for (b = 1; b < j; b++) + { + if (a > k[b][0] - 1) + { + break; + } + } + if (a + k[b][1] > k[b - 1][1]) + { + for (b--; b > 0; b--) + { + if (a + k[b][1] < k[b - 1][1] + 1) + { + break; + } + } + for (c = j; c > b; c--) + { + k[c][0] = k[c - 1][0]; + k[c][1] = k[c - 1][1]; + } + k[b][0] = a; + k[b][1] = k[b + 1][1] + a; + if (!b) + { + i += a; + } + j++; + } + else + { + for (c = j; c > b; c--) + { + k[c][0] = k[c - 1][0]; + k[c][1] = k[c - 1][1]; + } + k[b][0] = a; + k[b][1] = k[b + 1][1] + a; + j++; + } + } + } + printf("%d\n", i); + } + return 0; +} diff --git a/947.cpp b/947.cpp new file mode 100644 index 0000000..3ec913d --- /dev/null +++ b/947.cpp @@ -0,0 +1,238 @@ +#include + +using namespace std; + +#define _USE_MATH_DEFINES// exposes constants, eg. M_PI +#define S(n) scanf("%d", &n) +#define SLL(n) scanf("%lld", &n) +#define SD(n) scanf("%lf", &n) +#define SS(n) scanf("%s", n) +#define INF INT_MAX +#define LINF LLONG_MAX +#define EPS std::numeric_limits::epsilon() +#define ABS(x) ((x) < 0 ? -(x) : (x)) +#define FOR(i, a, b) for (int i = a; i < b; ++i) +#define RFOR(i, a, b) for (int i = (a)-1, _b(b); i >= _b; --i) +#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) +#define RREP(i, n) for (int(i) = n; (i) >= (int)0; --(i)) +#define DREP(a) \ + sort(all(a)); \ + a.erase(unique(all(a)), a.end()) +#define INDEX(arr, ind) (lower_bound(all(arr), ind) - arr.begin()) +#define FOREACH(c, itr) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr) +#define mp make_pair +#define pb push_back +#define tri(a, b, c) mp(a, mp(b, c)) +#define fst first +#define snd second +//#define FILL(a,v) memset(a,v,sizeof(a)) +#define SQT(a) ((a) * (a)) +//#define MAX(a,b) ((a)>(b)?(a):(b)) +//#define MIN(a,b) ((a)<(b)?(a):(b)) +#define ALL(x) x.begin(), x.end() +#define SZ(v) ((int)(v.size())) +#define LOG(a) (cerr << "\tline#" << __LINE__ << ": " #a " = " << (a) << endl) +#define DBG(args...) \ + { \ + cerr << "\tcase#" << (cc + 1) << ": "; \ + dbg, args; \ + cerr << endl; \ + } +struct debugger +{ + template + debugger &operator,(const T &v) + { + cerr << v << " "; + return *this; + } +} dbg; +typedef long long LL; +typedef long long ll; +typedef unsigned long long ULL; +typedef unsigned int uint; +typedef pair PII; +typedef pair PLL; +typedef pair TRI; +typedef vector VI; +typedef vector VL; +typedef vector VS; +typedef vector VII; +typedef vector VLL; +typedef vector VT; +typedef vector VVI; +typedef vector VVL; +typedef vector VVII; +typedef vector VVLL; +typedef vector VVT; +typedef complex pt; +typedef complex pti; +const double PI = M_PI; +template +inline T gcd(T a, T b) +{ + return b ? gcd(b, a % b) : a; +} +inline double round(double x) +{ + const double sd = 100;// accuracy = 2 dp + return int(x * sd + (x < 0 ? -0.5 : 0.5)) / sd; +} +inline int cmp(double x, double y = 0, double tol = EPS) +{ + return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; +} +template +inline void print(T *array, int size, bool reverse = false) +{ + printf("\tarray[] = ["); + if (reverse) + { + for (int i = size - 1; i >= 0; --i) + { + printf("%lld", (LL)array[i]); + if (i > 0) + { + printf(" "); + } + } + } + else + { + for (int i = 0; i < size; ++i) + { + printf("%lld", (LL)array[i]); + if (i < size - 1) + { + printf(" "); + } + } + } + printf("]\n"); +} +template +inline void print(T **array, int size1, int size2) +{ + printf("\tarray[][] = ["); + for (int i = 0; i < size1; ++i) + { + for (int j = 0; j < size2; ++j) + { + printf("%lld", (LL)array[i][j]); + if (j < size2 - 1) + { + printf(" "); + } + } + printf("]\n"); + } +} +///////////////////////////////////////////////////////////////////////////// +const int MAXLEN = 5, NUMCOLORS = 9; +int T; + +bool check(int secret[], int length, int guess[], int a, int b) +{ + int secret_freq[NUMCOLORS + 1], guess_freq[NUMCOLORS + 1]; + REP(i, NUMCOLORS + 1) + { + secret_freq[i] = 0; + guess_freq[i] = 0; + } + REP(i, length) + { + secret_freq[secret[i]]++; + guess_freq[guess[i]]++; + } + int correct = 0; + REP(i, length) + { + if (secret[i] == guess[i]) + { + correct++; + secret_freq[secret[i]]--; + guess_freq[guess[i]]--; + } + } + int wrong = 0; + REP(i, NUMCOLORS + 1) + { + wrong += min(secret_freq[i], guess_freq[i]); + } + return (correct == a && wrong == b); +} + +int main() +{ + //clock_t start = clock(); + scanf("%d", &T); + REP(cc, T) + { + int guess, a, b; + scanf("%d %d %d", &guess, &a, &b); + int length = 0; + while (pow(10, length) < guess) + { + length++; + } + int index = 0; + int guess_array[length]; + REP(i, length) + { + guess_array[i] = 0; + } + while (index < length) + { + guess_array[index] = guess % 10; + guess /= 10; + index++; + } + // generate + int start_number = 1, end_number = 9; + for (int i = 1; i < length; ++i) + { + int factor = 1; + for (int j = 0; j < i; ++j) + { + factor *= 10; + } + start_number += 1 * factor; + end_number += 9 * factor; + } + //printf("start_number = %d\n", start_number); + //printf("end_number = %d\n", end_number); + int count = 0; + for (int i = start_number; i <= end_number; ++i) + { + if (!(i % 10) == 0) + { + int the_number = i; + int array[length]; + bool skip = false; + REP(index, length) + { + if (the_number % 10 == 0) + { + skip = true; + break; + } + array[index] = the_number % 10; + the_number /= 10; + } + if (skip) + { + continue; + } + if (check(array, length, guess_array, a, b)) + { + count++; + } + //print(array, length, true); + } + } + printf("%d\n", count); + } + //fprintf(stderr, "*** Total time: %.3lf seconds ***\n", + // ((clock() - start) / (double) CLOCKS_PER_SEC)); + return 0; +} diff --git a/948.cpp b/948.cpp new file mode 100644 index 0000000..d6bc273 --- /dev/null +++ b/948.cpp @@ -0,0 +1,67 @@ +#include + +using namespace std; + +typedef long long LL; + +bool found; +int ar[50], in = 0; + +void fibo() +{ + int a = 0, b = 1, c = 1; + in = 0; + while (c <= 100000000) + { + ar[in++] = c; + a = b; + b = c; + c = a + b; + } +} + +void print(LL m) +{ + string str = ""; + while (m) + { + str += (m % 2) + '0'; + m /= 2; + } + reverse(str.begin(), str.end()); + cout << str << " (fib)" << endl; +} + +void recur(int at, int num, LL mask) +{ + if (num == 0) + { + print(mask); + found = true; + return; + } + if (num < 0 || found) + { + return; + } + for (int i = at - 2; i >= 0; i--) + { + recur(i, num - ar[i], mask | ((LL)1 << i)); + } +} + +int main() +{ + fibo(); + int testCase; + scanf("%d", &testCase); + while (testCase--) + { + int n; + scanf("%d", &n); + cout << n << " = "; + found = false; + recur(in + 1, n, 0); + } + return 0; +} diff --git a/949.cpp b/949.cpp new file mode 100644 index 0000000..8d97475 --- /dev/null +++ b/949.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +typedef long long ll; + +#define REP(i, s, t) for (int i = (s); i < (t); i++) +#define FOREACH(i, v) for (typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) +#define FILL(x, v) memset(x, v, sizeof(x)) +const int INF = (int)1E9; +#define MAXN 105 + +int N, M, Q; +bool g[MAXN][MAXN][4]; +int mt[505][2]; +bool v[MAXN][MAXN][505]; +int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1}; +struct dij +{ + int x, y, t; + dij(int _x, int _y, int _t) + { + x = _x; + y = _y; + t = _t; + } +}; +void bfs() +{ + queue que; + que.push(dij(0, 0, 0)); + FILL(v, 0); + v[0][0][0] = 1; + while (!que.empty()) + { + dij now = que.front(); + que.pop(); + int x = now.x, y = now.y, t = now.t, nt = t > 500 ? 501 : t + 1; + if (x == N - 1 && y == M - 1) + { + printf("%d\n", t); + return; + } + REP(i, 0, 4) + { + if (!g[x][y][i]) + continue; + int nx = x + dir[i][0], ny = y + dir[i][1]; + if (nx < 0 || ny < 0 || nx >= N || ny >= M || v[nx][ny][nt]) + continue; + if (nx == mt[nt][0] && ny == mt[nt][1]) + continue; + v[nx][ny][nt] = 1; + que.push(dij(nx, ny, t + 1)); + } + if (!v[x][y][nt]) + { + v[x][y][nt] = 1; + que.push(dij(x, y, t + 1)); + } + } +} +int main() +{ + while (scanf("%d%d", &N, &M) != EOF) + { + FILL(mt, -1); + FILL(g, 1); + int k; + scanf("%d", &k); + while (k--) + { + int x1, x2, y1, y2; + scanf("%d%d%d%d", &x1, &y1, &x2, &y2); + REP(i, 0, 4) + { + if (x1 + dir[i][0] == x2 && y1 + dir[i][1] == y2) + { + g[x1][y1][i] = 0; + break; + } + } + } + scanf("%d", &k); + while (k--) + { + int t, x, y; + scanf("%d%d%d", &t, &x, &y); + mt[t][0] = x; + mt[t][1] = y; + } + bfs(); + } + return 0; +} diff --git a/953.cpp b/953.cpp new file mode 100644 index 0000000..2974a55 --- /dev/null +++ b/953.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +const int MAXN = 10; +int dp[MAXN][1 << MAXN], from[MAXN][1 << MAXN]; +int main() +{ + int testcase, N; + int type[20][20]; + scanf("%d", &testcase); + while (testcase--) + { + scanf("%d", &N); + + int sum = 0; + for (int i = N - 1; i >= 0; i--) + { + for (int j = 0; j < N; j++) + scanf("%d", &type[i][j]), sum += type[i][j]; + } + + memset(dp, 0, sizeof(dp)); + memset(from, 63, sizeof(from)); + for (int i = 0; i < N; i++) + dp[0][1 << i] = type[0][i], from[0][1 << i] = i; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < (1 << N); j++) + { + for (int k = 0; k < N; k++) + { + if ((j >> k) & 1) + continue; + if (dp[i][j] + type[i + 1][k] > dp[i + 1][j | (1 << k)]) + dp[i + 1][j | (1 << k)] = max(dp[i + 1][j | (1 << k)], dp[i][j] + type[i + 1][k]); + if (dp[i][j] + type[i + 1][k] == dp[i + 1][j | (1 << k)]) + from[i + 1][j | (1 << k)] = min(k, from[i + 1][j | (1 << k)]); + } + } + } + + int ret = dp[N - 1][(1 << N) - 1]; + int sol[MAXN]; + for (int i = N - 1, q = (1 << N) - 1; i >= 0; i--) + { + sol[i] = from[i][q]; + q ^= 1 << sol[i]; + } + for (int i = N - 1; i >= 0; i--) + { + printf("%d", sol[i]); + } + printf(" %d\n", sum - ret); + } + return 0; +} +/* +1 +3 +10 17 1 +9 9 8 +12 19 11 +*/ diff --git a/954.cpp b/954.cpp new file mode 100644 index 0000000..8b7bb44 --- /dev/null +++ b/954.cpp @@ -0,0 +1,216 @@ +#include + +using namespace std; + +char piece[20][515], flag, used[17]; +int grid[4][4], soln[4][4], conf[100][17], conf_index[100], kase = 1, n = 1; + +/*int safe(int rank, int r, int c){ + if( r && piece[rank][1] != piece[grid[ r - 1 ][c]][3]) return 0; + if( c && piece[rank][2] != piece[grid[r][ c - 1 ]][0]) return 0; + return 1; +}*/ + +void backtrack(int r, int c) +{ + register int i, x = 0, y = 0, j, maxj; + if (r == 4) + { + for (r = 0; r < 4; r++) + for (c = 0; c < 4; c++) + { + soln[r][c] = grid[r][c]; + } + flag++; + return; + } + /*for ( i = 1; i <= 16 && flag < 3; i++ ){ + if( !used[i] && safe( i, r, c ) ){ + grid[r][c] = i; + used[i] = 1; + (c == 3) ? backtrack( r + 1, 0 ) : backtrack( r , c + 1 ); + grid[r][c] = 0; + used[i] = 0; + } + }*/ + if (c == 0) + { + x = 10 * (piece[grid[r - 1][c]][3] - '0'), maxj = 10; + for (j = 0; j < maxj && flag < 3; j++, x++) + { + for (i = 0; i < conf_index[x] && flag < 3; i++) + { + if (!used[conf[x][i]]) + { + grid[r][c] = conf[x][i]; + used[conf[x][i]] = 1; + (c == 3) ? backtrack(r + 1, 0) : backtrack(r, c + 1); + grid[r][c] = 0; + used[conf[x][i]] = 0; + } + } + } + } + else if (r == 0) + { + x = piece[grid[r][c - 1]][0] - '0', maxj = 10; + for (j = 0; j < maxj && flag < 3; j++, x += 10) + { + for (i = 0; i < conf_index[x] && flag < 3; i++) + { + if (!used[conf[x][i]]) + { + grid[r][c] = conf[x][i]; + used[conf[x][i]] = 1; + (c == 3) ? backtrack(r + 1, 0) : backtrack(r, c + 1); + grid[r][c] = 0; + used[conf[x][i]] = 0; + } + } + } + } + else + { + x = 10 * (piece[grid[r - 1][c]][3] - '0') + piece[grid[r][c - 1]][0] - '0'; + for (i = 0; i < conf_index[x] && flag < 3; i++) + { + if (!used[conf[x][i]]) + { + grid[r][c] = conf[x][i]; + used[conf[x][i]] = 1; + (c == 3) ? backtrack(r + 1, 0) : backtrack(r, c + 1); + grid[r][c] = 0; + used[conf[x][i]] = 0; + } + } + } +} + +void solve() +{ + int i; + for (i = 1; i <= 16 && flag < 3; i++) + { + grid[0][0] = i; + used[i] = 1; + backtrack(0, 1); + used[i] = 0; + } +} + +int parse_input() +{ + register int i, j = 0; + register char ch, tmp = 0, notspace = 0; + while ((ch = getchar()) != EOF) + { + if (ch == '\n' && notspace == 0) + { + break; + } + if (!isspace(ch) && ch != '\n') + { + notspace = ch; + if (n <= 17) + { + piece[n][j++] = ch; + } + } + else + { + if (n <= 17 && j) + { + piece[n][j] = 0; + } + if (j) + { + n++; + } + j = 0; + } + if (ch == '\n') + { + notspace = 0; + } + tmp = ch; + } + if (tmp == 0) + { + return 0; + } + else + { + return 1; + } +} + +int main() +{ + register int i, j, x; + while (parse_input()) + { + x = n - 1; + for (i = 0; i < 100; i++) + { + conf_index[i] = 0; + } + if (x != 16) + { + flag = 0; + } + else + { + flag = 1; + } + for (i = 1; i <= 16; i++) + { + if (flag) + { + for (j = 0; piece[i][j] && isdigit(piece[i][j]); j++) + ; + if (piece[i][j] || j != 4) + { + flag = 0; + } + } + x = 10 * (piece[i][1] - '0') + piece[i][2] - '0'; + if (flag) + { + conf[x][conf_index[x]++] = i; + } + } + for (i = 1; i < 16 && flag; i++) + for (j = i + 1; j <= 16 && flag; j++) + if (strcmp(piece[i], piece[j]) == 0) + { + flag = 0; + } + if (flag) + { + solve(); + } + if (kase > 1) + { + puts(""); + } + printf("%d\n", flag); + if (flag == 2) + { + for (i = 0; i < 4; i++) + { + for (j = 0; j < 4; j++) + { + if (j) + { + putchar(' '); + } + printf(piece[soln[i][j]]); + } + puts(""); + } + } + kase++; + n = 1; + } + return 0; +} diff --git a/956.cpp b/956.cpp new file mode 100644 index 0000000..758567a --- /dev/null +++ b/956.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +int mark[30][30], n, prox[30][2], tira[30], ehIgual[30][30], q[30]; +int read() +{ + char p[3], p1[3], p2[3], tipo[3]; + if (scanf("%d", &n) == EOF) + { + return 0; + } + for (int i = 0; i < n; i++) + { + scanf("%s%s%s%s", p, p1, p2, tipo); + prox[p[0] - 'A'][0] = p1[0] - 'A'; + prox[p[0] - 'A'][1] = p2[0] - 'A'; + q[p[0] - 'A'] = tipo[0]; + } + return 1; +} +int go(int i, int j) +{ + if (ehIgual[i][j] != -1) + { + return ehIgual[i][j]; + } + if (i == j || mark[i][j]) + { + return 1; + } + mark[i][j] = mark[j][i] = 1; + if (q[i] != q[j]) + { + return ehIgual[i][j] = ehIgual[j][i] = 0; + } + if (go(prox[i][0], prox[j][0]) && go(prox[i][1], prox[j][1])) + { + ehIgual[i][j] = ehIgual[j][i] = 1; + } + else + { + ehIgual[i][j] = ehIgual[j][i] = 0; + } + return ehIgual[i][j]; +} +void process() +{ + memset(mark, 0, sizeof(mark)); + memset(tira, 0, sizeof(tira)); + memset(ehIgual, -1, sizeof(ehIgual)); + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + { + if (mark[i][j] == 0) + { + go(i, j); + } + if (ehIgual[i][j]) + { + tira[j] = 1; + } + } + for (int i = 0; i < n; i++) + { + if (tira[i]) + { + continue; + } + if (i) + { + printf(" "); + } + printf("%c", 'A' + i); + } + printf("\n"); +} +int main() +{ + while (read()) + { + process(); + } + return 0; +} diff --git a/957.cpp b/957.cpp new file mode 100644 index 0000000..d6c2446 --- /dev/null +++ b/957.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +// http://online-judge.uva.es/p/v9/957.html + +int main() +{ + int y; + while (scanf("%d", &y) != EOF) + { + y--; + int p; + scanf("%d", &p); + int a[p]; + for (int i = 0; i < p; i++) + { + scanf("%d", &a[i]); + } + int count = 0; + int j; + int first; + int last; + int max = 0; + for (int i = 0; i < p; i++) + { + j = i; + count = 0; + while (a[j] <= a[i] + y) + { + j++; + count++; + } + if (count > max) + { + max = count; + first = a[i]; + last = a[j - 1]; + } + } + printf("%d %d %d\n", max, first, last); + } + return 0; +} diff --git a/960.cpp b/960.cpp new file mode 100644 index 0000000..546fd74 --- /dev/null +++ b/960.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int casos, a, b; +int read() +{ + scanf("%d%d", &a, &b); + return 1; +} +void process() +{ + int norma = (int)floor(sqrt(a * a + b * b) + 0.000001); + int rnorma = (int)floor(sqrt(norma)); + for (int i = -rnorma; i <= rnorma; i++) + { + for (int j = -rnorma; j <= rnorma; j++) + { + if ((i == 0 && j == 0) || (i * i + j * j == 1)) + { + continue; + } + if ((a * i + b * j) % (i * i + j * j) == 0 && (b * i - a * j) % (i * i + j * j) == 0) + { + //printf ("%d %d => %d %d\n", a, b, i, j); + printf("C\n"); + return; + } + } + } + printf("P\n"); +} +int main() +{ + scanf("%d", &casos); + while (casos--) + { + read(); + process(); + } + return 0; +} diff --git a/962.cpp b/962.cpp new file mode 100644 index 0000000..7114de4 --- /dev/null +++ b/962.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int C[1024], T[460000]; +int cmp(const void *a, const void *b) +{ + return *(int *)a - *(int *)b; +} +int main() +{ + int i, j, c, n = 0, r, p = 0; + for (i = 1; i < 1024; i++) + { + C[i] = i * i * i; + } + for (i = 1; i < 1001; i++) + { + c = C[i]; + for (j = i; C[j] + c <= 1000100000; j++) + { + T[n++] = C[j] + c; + } + } + qsort(T, n, sizeof(int), cmp); + for (i = 1; i < n; i++) + { + if (T[i] == T[i - 1]) + { + c = T[p++] = T[i]; + while (T[++i] == c) + ; + } + } + T[p] = T[p - 1] + 1000000; + while (scanf("%d%d", &n, &r) == 2) + { + if (n > T[p - 1]) + { + printf("None\n"); + continue; + } + if (n <= 1729) + { + i = 0; + } + else + { + for (i = p >> 1, j = p >> 2;; j = (j + 1) / 2) + { + if (i < 1) + { + i = 1; + } + else if (i >= p) + { + i = p - 1; + } + /*T[i-1] T[i]) + { + i += j; + } + else + { + break; + } + } + } + for (c = 0; T[i] <= n + r; i++, c++) + { + printf("%d\n", T[i]); + } + if (!c) + { + printf("None\n"); + } + } + return 0; +} diff --git a/963.cpp b/963.cpp new file mode 100644 index 0000000..57af410 --- /dev/null +++ b/963.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +int edit_dist(const char *a, int la, const char *b, int lb) +{ + static int dp[16][16] = {}; + memset(dp, 0x3f, sizeof(dp)); + int INF = dp[0][0]; + dp[0][0] = 0; + for (int i = 1; i <= lb; i++) + { + for (int j = 0; j <= la; j++) + { + if (dp[i - 1][j] == INF) + continue; + for (int k = j + 1; k <= la; k++) + { + if (a[k - 1] == b[i - 1]) + dp[i][k] = min(dp[i][k], dp[i - 1][j] + 3 * (k - j - 1)); + dp[i][k] = min(dp[i][k], dp[i - 1][j] + 3 * (k - j - 1) + 5); + } + dp[i][j] = min(dp[i][j], dp[i - 1][j] + 3); + } + } + int ret = INF; + for (int i = 0; i <= la; i++) + ret = min(ret, dp[lb][i] + (la - i) * 3); + return ret; +} +int main() +{ + int n, m; + char s[1024]; + string dict[65536]; + int cases = 0; + while (scanf("%d", &n) == 1) + { + for (int i = 0; i < n; i++) + { + scanf("%s", s); + dict[i] = s; + } + sort(dict, dict + n); + scanf("%d", &m); + while (getchar() != '\n') + ; + if (cases++) + puts(""); + + char *sptr = NULL, *send = NULL; + for (int i = 0; i < m; i++) + { + while (1) + { + if (sptr == NULL) + { + fgets(s, 1024, stdin); + sptr = s, send = s + strlen(s); + } + while (sptr && sptr != send && isspace(*sptr)) + putchar(*sptr), sptr++; + if (sptr == send) + sptr = NULL; + if (sptr && !isspace(*sptr)) + break; + } + char buf[16]; + int len = 0; + for (; isalpha(*sptr); sptr++) + buf[len++] = *sptr; + buf[len] = '\0'; + int mn = INT_MAX, mnIdx = 0; + for (int j = 0; j < n; j++) + { + int t = edit_dist(dict[j].c_str(), dict[j].length(), buf, len); + if (t < mn) + mn = t, mnIdx = j; + } + fputs(dict[mnIdx].c_str(), stdout); + } + while (sptr && sptr != send && isspace(*sptr)) + putchar(*sptr), sptr++; + } + return 0; +} diff --git a/964.cpp b/964.cpp new file mode 100644 index 0000000..edb2ef5 --- /dev/null +++ b/964.cpp @@ -0,0 +1,305 @@ +#include + +using namespace std; + +struct Line +{ + int cmd; + int argNum; + int argVar; +}; + +struct Var +{ + int value; + int written; + Var() : value(0), written(0) {} +}; + +vector code; +vector data, out, stk; +map symMap; +vector sym; + +bool is_var(const char *arg) +{ + if (arg[0] == 0 || !isalpha(arg[0])) + { + return false; + } + for (; *arg; arg++) + if (!isalnum(*arg)) + { + return false; + } + return true; +} + +bool push(int x) +{ + stk.push_back(x); + return true; +} +int pop() +{ + assert(stk.size() > 0); + int x = stk.back(); + stk.pop_back(); + return x; +} + +bool push(const Line &l) +{ + if (l.argVar >= 0) + { + if (sym[l.argVar].written != 0) + { + return push(sym[l.argVar].value); + } + } + else + { + return push(l.argNum); + } + return false; +} + +const char *commands[] = + { + "PUSH", "POP", "DUP", "ADD", "MUL", "SUB", "DIV", + "READ", "WRITE", "JUMP", "JUMPPOS", "JUMPZERO", NULL}; + +bool run() +{ + const bool jump_to_undef = false; + int cp = 0, dp = 0; + stk.clear(); + out.clear(); + while (0 <= cp && cp < (int)code.size()) + { + const Line &l = code[cp++]; + if (l.cmd == 0)// PUSH + { + if (!push(l)) + { + return false; + } + } + else if (l.cmd == 1)// POP + { + if (l.argVar < 0 || stk.size() == 0) + { + return false; + } + sym[l.argVar].value = pop(); + sym[l.argVar].written = 1; + } + else if (l.cmd == 2)// DUP + { + if (stk.size() == 0) + { + return false; + } + push(stk.back()); + } + else if (l.cmd == 3)// ADD + { + if (stk.size() < 2) + { + return false; + } + push(pop() + pop()); + } + else if (l.cmd == 4)// MUL + { + if (stk.size() < 2) + { + return false; + } + push(pop() * pop()); + } + else if (l.cmd == 5)// SUB + { + if (stk.size() < 2) + { + return false; + } + int x = pop(); + int y = pop(); + push(x - y); + } + else if (l.cmd == 6)// DIV + { + if (stk.size() < 2) + { + return false; + } + int x = pop(); + int y = pop(); + if (y == 0) + { + return false; + } + push(x / y); + } + else if (l.cmd == 7)// READ + { + if (dp >= (int)data.size()) + { + return false; + } + push(data[dp++]); + } + else if (l.cmd == 8)// WRITE + { + if (stk.size() == 0) + { + return false; + } + out.push_back(pop()); + } + else if (l.cmd == 9)// JUMP + { + if (!push(l)) + { + return jump_to_undef; + } + cp = pop() - 1; + } + else if (l.cmd == 10)// JUMPPOS + { + if (stk.size() == 0) + { + return false; + } + if (pop() > 0) + { + if (!push(l)) + { + return jump_to_undef; + } + cp = pop() - 1; + } + } + else// JUMPZERO + { + if (stk.size() == 0) + { + return false; + } + if (pop() == 0) + { + if (!push(l)) + { + return jump_to_undef; + } + cp = pop() - 1; + } + } + } + return true; +} + +int main() +{ + static char s[100000]; + for (;;) + { + code.clear(); + sym.clear(); + symMap.clear(); + for (;;) + { + if (gets(s) == NULL) + { + return 0; + } + if (s[0] == '#') + { + break; + } + for (int m = strlen(s); m > 0 && isspace(s[m - 1]);) + { + s[--m] = 0; + } + assert(isalpha(s[0])); + char *p = s; + while (*p && !isalpha(*p)) + { + p++; + } + assert(isalpha(*p)); + char op[20]; + { + int i; + for (i = 0; i < 15 && *p && isalpha(*p);) + { + op[i++] = *p++; + } + op[i] = 0; + } + while (*p && isspace(*p)) + { + p++; + } + Line l; + for (int i = 0;; i++) + { + assert(commands[i] != NULL); + if (strcmp(commands[i], op) == 0) + { + l.cmd = i; + break; + } + } + for (int i = 0; p[i]; i++) + { + assert(isalnum(p[i]) || p[i] == '-'); + } + l.argNum = 0; + l.argVar = -1; + if (isalpha(*p)) + { + if (symMap.count(p) == 0) + { + symMap[p] = l.argVar = sym.size(); + sym.push_back(Var()); + } + else + { + l.argVar = symMap[p]; + } + } + else + { + l.argNum = atoi(p); + } + code.push_back(l); + } + data.clear(); + for (;;) + { + if (gets(s) == NULL) + { + return 0; + } + if (s[0] == '#') + { + break; + } + data.push_back(atoi(s)); + } + if (!run()) + { + printf("ABORTED\n"); + } + else + { + for (int i = 0; i < (int)out.size(); i++) + { + printf("%d\n", out[i]); + } + } + printf("#\n"); + } +} diff --git a/967.cpp b/967.cpp index 6eaa13d..e458772 100644 --- a/967.cpp +++ b/967.cpp @@ -1,94 +1,87 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include + using namespace std; -#define MAX 1000000 -static bool prime[MAX + 1]; -static int cPrime[MAX + 1]; +map isprime; -void sieve() { - prime[0] = true; - prime[1] = true; - - for (int i = 2; i * i <= MAX; i++) { - if (!prime[i]) { - for (int j = i; i * j <= MAX; j++) { - prime[i * j] = true; - } +bool is_prime(int n) +{ + for (int i = 3, sq = sqrt(n); i <= sq; i += 2) + { + if (n % i == 0) + { + return false; } } + return true; } -vector< int > digits(int I) { - vector< int > V; - while (I > 0) { - V.push_back(I % 10); - I /= 10; +bool is_circular(int prime) +{ + int size = (int)log10(prime) + 1; + for (int i = 0; i < size; i++) + { + prime = (prime % (int)pow(10, size - 1)) * 10 + prime / (int)pow(10, size - 1); + if (isprime[prime] == false) + { + return false; + } } - reverse(V.begin(), V.end()); - return V; + return true; } -void cal() { - for (int i = 1; i <= MAX; i++) { - cPrime[i] = cPrime[i - 1]; - if (!prime[i]) { - bool flag = true; - vector< int > V = digits(i); - for (int j = 0; j < V.size(); j++) { - int I = 0; - for (int k = 0; k < V.size(); k++) - I = I * 10 + V[(j + k) % V.size()]; - if (prime[I]) - flag = false; - } - if (flag) - ++cPrime[i]; +int main() +{ + int a, b; + vector primes; + vector circular; + for (int i = 101; i < 1000000; i += 2) + { + if (is_prime(i)) + { + primes.push_back(i); + isprime[i] = true; } } -} - -int main() { - //freopen("testI.txt", "r", stdin); - //freopen("testO.txt", "w", stdout); - sieve(); - cal(); - while (true) { - int A, B; - scanf("%d", &A); - if (A == -1) break; - scanf("%d", &B); - - int C = cPrime[B] - cPrime[A - 1]; - if (C <= 0) - printf("No Circular Primes.\n"); - else if (C == 1) - printf("1 Circular Prime.\n"); + for (int i = 0, sz = primes.size(); i < sz; i++) + { + if (is_circular(primes[i])) + { + circular.push_back(primes[i]); + } + } + while (cin >> a) + { + if (a == -1) + { + break; + } + cin >> b; + int result = 0; + for (int i = 0, sz = circular.size(); i < sz; i++) + { + if (circular[i] > b) + { + break; + } + if (circular[i] < a) + { + continue; + } + result++; + } + if (result == 0) + { + cout << "No Circular Primes." << endl; + } + else if (result == 1) + { + cout << "1 Circular Prime." << endl; + } else - printf("%d Circular Primes.\n", C); + { + cout << result << " Circular Primes." << endl; + } } return 0; } diff --git a/969.cpp b/969.cpp new file mode 100644 index 0000000..f57bd65 --- /dev/null +++ b/969.cpp @@ -0,0 +1,222 @@ +#include + +using namespace std; + +typedef struct node +{ + struct node *left, *parent, *right; + char op; +} node; + +node *mkNode(char op) +{ + node *ptr = (node *)malloc(sizeof(node)); + ptr->op = op; + ptr->left = ptr->right = ptr->parent = 0; + return ptr; +} + +void clearTree(node *tree) +{ + if (tree->left != 0) + { + clearTree(tree->left); + } + if (tree->right != 0) + { + clearTree(tree->right); + } + free(tree); +} + +node *root; + +void optimizeTree(node *tree) +{ + node *ptr; + if (tree->left) + { + optimizeTree(tree->left); + } + if (tree->right) + { + optimizeTree(tree->right); + } + if (tree->op == 'i') + { + if ((tree->left->parent = tree->parent) != 0) + { + if (tree == tree->parent->right) + { + tree->parent->right = tree->left; + } + else + { + tree->parent->left = tree->left; + } + } + if (root == tree) + { + root = tree->left; + } + free(tree); + } + else if (tree->op == 't' && tree->right->op == 't' && tree->right->right->op == '0') + { + ptr = mkNode('k'); + ptr->left = tree->right->left; + ptr->right = tree->left; + ptr->left->parent = ptr->right->parent = ptr; + if ((ptr->parent = tree->parent) != 0) + { + if (ptr->parent->left == tree) + { + ptr->parent->left = ptr; + } + else + { + ptr->parent->right = ptr; + } + } + if (root == tree) + { + root = ptr; + } + free(tree->right->right); + free(tree->right); + free(tree); + } +} + +void printExpression(char *dump, node *tree) +{ + int len; + if (tree == 0) + { + return; + } + len = strlen(dump); + dump[len++] = tree->op; + dump[len] = 0; + switch (tree->op) + { + case 'k': + case 't': + strcat(dump, "("); + printExpression(dump, tree->left); + strcat(dump, ", "); + printExpression(dump, tree->right); + strcat(dump, ")"); + return; + case 'i': + strcat(dump, "("); + printExpression(dump, tree->left); + strcat(dump, ")"); + return; + } +} + +char *now, token; + +char nextToken() +{ + token = *now; + if (*now) + { + now++; + } + return token; +} + +node *parse() +{ + node *ptr = 0; + nextToken(); + if (token == '0') + { + ptr = mkNode('0'); + return ptr; + } + else if (token == 't' || token == 'k') + { + ptr = mkNode(token); + nextToken(); + ptr->left = parse(); + if (ptr->left) + { + ptr->left->parent = ptr; + } + nextToken(); + ptr->right = parse(); + if (ptr->right) + { + ptr->right->parent = ptr; + } + nextToken(); + return ptr; + } + else if (token == 'i') + { + ptr = mkNode(token); + nextToken(); + ptr->left = parse(); + if (ptr->left) + { + ptr->left->parent = ptr; + } + nextToken(); + return ptr; + } + return 0; +} + +void SpGets(char *input) +{ + char ch; + while ((ch = getchar()) != EOF) + { + if (ch == '\n' || ch == '\r') + { + break; + } + else if (ch == '\t' || ch == ' ') + { + continue; + } + *input++ = ch; + } + *input = 0; + return; +} + +char dump1[1000000], dump2[1000000]; +int main() +{ + int count = 1000; + char input[10000]; + node *exp1, *exp2; + scanf("%d", &count); + getchar(); + while (count--) + { + dump1[0] = dump2[0] = 0; + SpGets(input); + now = input; + exp1 = parse(); + root = exp1; + optimizeTree(exp1); + exp1 = root; + printExpression(dump1, exp1); + SpGets(input); + now = input; + exp2 = parse(); + root = exp2; + optimizeTree(exp2); + exp2 = root; + printExpression(dump2, exp2); + printf("%s\n", strcmp(dump1, dump2) ? "false" : "true"); + clearTree(exp1); + clearTree(exp2); + } + return 0; +} diff --git a/970.cpp b/970.cpp new file mode 100644 index 0000000..3398ae3 --- /dev/null +++ b/970.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int res[3][3] = {{1, 0, 2}, {0, 1, 1}, {2, 1, 0}}, sres[1 << 3][1 << 3]; +int memo[128][128]; + +int main() +{ + for (int a = 0; a < (1 << 3); a++) + { + for (int b = 0; b < (1 << 3); b++) + { + sres[a][b] = 0; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if ((a & (1 << i)) != 0 && (b & (1 << j)) != 0) + { + sres[a][b] |= 1 << res[i][j]; + } + } + } + int T; + scanf("%d", &T); + for (int cs = 1; cs <= T; cs++) + { + char str[128]; + scanf(" %s", str); + int N = strlen(str); + for (int i = 0; i < N; i++) + { + memo[1][i] = 1 << (str[i] - 'X'); + } + for (int m = 2; m <= N; m++) + { + for (int s = 0; s + m <= N; s++) + { + memo[m][s] = 0; + for (int k = 1; k < m; k++) + { + if ((memo[m][s] |= sres[memo[k][s]][memo[m - k][s + k]]) == 7) + { + break; + } + } + } + } + int i = 2; + while (i > 0 && (memo[N][0] & (1 << i)) == 0) + { + i--; + } + printf("%c\n", i + 'X'); + } +} diff --git a/972.cpp b/972.cpp new file mode 100644 index 0000000..3ffadd2 --- /dev/null +++ b/972.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +#define N 1005 + +inline double max(double a, double b) +{ + return a > b ? a : b; +} + +struct segement +{ + double height; + double start, end; +} a[N], b[N]; + +double step[N]; + +int main() +{ + int n1, n2, n; + double sum; + while (~scanf("%d", &n1)) + { + n = 1; + sum = 0; + for (int i = 0; i < n1; i++) + { + scanf("%lf %lf", &a[i].height, &a[i].end); + a[i].start = sum; + sum += a[i].end; + a[i].end = sum; + step[n++] = sum; + } + scanf("%d", &n2); + sum = 0; + for (int i = 0; i < n2; i++) + { + scanf("%lf %lf", &b[i].height, &b[i].end); + b[i].start = sum; + sum += b[i].end; + b[i].end = sum; + step[n++] = sum; + } + std::sort(step, step + n); + double ans = 1e9, height1, height2; + for (int i = 1; i < n; i++) + { + for (int j = 0; j < n1; j++) + if (a[j].start <= step[i - 1] && a[j].end >= step[i]) + { + height1 = a[j].height; + break; + } + for (int j = 0; j < n2; j++) + if (b[j].start <= step[i - 1] && b[j].end >= step[i]) + { + height2 = b[j].height; + break; + } + if (max(height1, height2) < ans) + { + ans = max(height1, height2); + } + } + printf("%.3lf\n", ans); + } + return 0; +} diff --git a/973.cpp b/973.cpp new file mode 100644 index 0000000..a705d87 --- /dev/null +++ b/973.cpp @@ -0,0 +1,119 @@ +#include + +using namespace std; + +#define REP(i, b, n) for (int i = b; i < n; i++) +#define rep(i, n) REP(i, 0, n) + +const int N = 10; + +int rowrem[N], colrem[N], diagrem[2 * N]; +int inr[N], inc[N], ind[2 * N]; + +bool m[N][N]; +bool ans[N][N]; +int cnt; + +void solve(int now, int r, int c) +{ + if (now == r * c) + { + if (cnt == 0) + { + rep(i, r) rep(j, c) ans[i][j] = m[i][j]; + } + cnt++; + return; + } + int y = now / c, x = now % c; + int dir = r - 1 - y + x; + //cout <> r >> c) + { + if (tc) + { + cout << endl; + } + tc = 1; + rep(i, N) rowrem[i] = 0, colrem[i] = 0; + rep(i, 2 * N) diagrem[i] = 0; + rep(i, r) + { + rep(j, c) + { + rowrem[i]++; + colrem[j]++; + diagrem[r - 1 - i + j]++; + } + } + rep(i, r) cin >> inr[i]; + rep(i, c) cin >> inc[i]; + rep(i, r + c - 1) cin >> ind[i]; + cnt = 0; + solve(0, r, c); + if (cnt == 1) + { + rep(i, r) + { + rep(j, c) + { + if (ans[i][j]) + { + cout << 'X'; + } + else + { + cout << '.'; + } + } + cout << endl; + } + } + else + { + cout << cnt << endl; + } + } + return false; +} diff --git a/974.cpp b/974.cpp index 3535dce..4dab875 100644 --- a/974.cpp +++ b/974.cpp @@ -1,23 +1,37 @@ -#include -int main(){ - int kaprekar[]={1,9,45,55,99,297,703,999,2223,2728,4879,4950,5050,5292,7272,7777,9999,17344,22222,38962}; - int len=20; - int i,z,test; - scanf("%d",&test); - for(z=1;z<=test;z++){ - int lower,upper,cnt=0; - scanf("%d%d",&lower,&upper); - printf("case #%d\n",z); - for(i=1;i=lower && kaprekar[i]<=upper){ - printf("%d\n",kaprekar[i]); - cnt++; - } - } - if(cnt==0) - printf("no kaprekar numbers\n"); - if(z!=test) - printf("\n"); - } - return 0; -} +#include + +using namespace std; + +int Kap[] = {9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999, 17344, 22222, 38962}; +void Cal(int n, int m) +{ + int i, res = 0; + for (i = 0; i < 19; i++) + { + if (Kap[i] >= n && Kap[i] <= m) + { + printf("%d\n", Kap[i]); + res++; + } + } + if (!res) + { + printf("no kaprekar numbers\n"); + } +} +int main() +{ + int kase, n, m, i = 0; + scanf("%d", &kase); + while (kase--) + { + if (i++) + { + printf("\n"); + } + printf("case #%d\n", i); + scanf("%d%d", &n, &m); + Cal(n, m); + } + return 0; +} diff --git a/976.cpp b/976.cpp new file mode 100644 index 0000000..bbb874c --- /dev/null +++ b/976.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +char map_[1000][1001]; +char walk[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; +int n, m, dp[1000][100]; + +char check(int x, int y) +{ + if (x > -1 && x < n && y > -1 && y < m) + { + if (map_[x][y] == '#') + { + return 1; + } + } + return 0; +} + +void floodfill(int x, int y) +{ + int i, xtmp, ytmp; + for (i = 0; i < 4; i++) + { + xtmp = x + walk[i][0]; + ytmp = y + walk[i][1]; + if (check(xtmp, ytmp)) + { + map_[xtmp][ytmp] = '@'; + floodfill(xtmp, ytmp); + } + } +} + +int main() +{ + int b, s, i, j, k, len; + while (scanf("%d%d", &n, &m) == 2) + { + scanf("%d%d", &b, &s); + for (i = 0; i < n; i++) + { + scanf("%s", map_[i]); + } + floodfill(0, 0); + memset(dp, 0, sizeof(dp)); + for (j = 0; j < m; j++) + { + for (i = 0; i < n; i++) + { + if (map_[i][j] == '@') + { + len = i; + } + else if (map_[i][j] == '#') + { + len = i - len - 1; + break; + } + } + dp[j][0] = len; + if (j && dp[j - 1][0] < dp[j][0]) + { + dp[j][0] = dp[j - 1][0]; + } + i = j - s - 1; + if (i < 0) + { + continue; + } + for (k = 1; k < b; k++) + { + if (!dp[i][k - 1]) + { + break; + } + dp[j][k] = len + dp[i][k - 1]; + if (dp[j - 1][k] && dp[j - 1][k] < dp[j][k]) + { + dp[j][k] = dp[j - 1][k]; + } + } + } + printf("%d\n", dp[m - 1][b - 1]); + } + return 0; +} diff --git a/978.cpp b/978.cpp new file mode 100644 index 0000000..e429920 --- /dev/null +++ b/978.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +int main() +{ + int n, b, sg, sb, tmp, gs, bs; + priority_queue green, blue; + vector gv, bv; + cin >> n; + while (n--) + { + cin >> b >> sg >> sb; + while (sg--) + { + cin >> tmp; + green.push(tmp); + } + while (sb--) + { + cin >> tmp; + blue.push(tmp); + } + while (!green.empty() && !blue.empty()) + { + gs = green.size(); + bs = blue.size(); + tmp = min(gs, bs); + tmp = min(tmp, b); + for (int i = 0; i < tmp; i++) + { + gs = green.top(); + green.pop(); + bs = blue.top(); + blue.pop(); + if (gs > bs) + { + gv.push_back(gs - bs); + } + else if (gs < bs) + { + bv.push_back(bs - gs); + } + } + for (int i = 0, sz = gv.size(); i < sz; i++) + { + green.push(gv[i]); + } + for (int i = 0, sz = bv.size(); i < sz; i++) + { + blue.push(bv[i]); + } + gv.clear(); + bv.clear(); + } + if (green.empty() && blue.empty()) + { + cout << "green and blue died" << endl; + } + else if (blue.empty()) + { + cout << "green wins" << endl; + while (!green.empty()) + { + cout << green.top() << endl; + green.pop(); + } + } + else// green.empty() + { + cout << "blue wins" << endl; + while (!blue.empty()) + { + cout << blue.top() << endl; + blue.pop(); + } + } + if (n) + { + cout << endl; + } + } + return 0; +} diff --git a/979.cpp b/979.cpp new file mode 100644 index 0000000..3ca4406 --- /dev/null +++ b/979.cpp @@ -0,0 +1,162 @@ +#include + +using namespace std; + +#define eps 1e-6 +struct Pt +{ + double x, y; + Pt(double a = 0, double b = 0) : x(a), y(b) {} + bool operator<(const Pt &a) const + { + if (fabs(x - a.x) > eps) + return x < a.x; + if (fabs(y - a.y) > eps) + return y < a.y; + return false; + } + Pt operator-(const Pt &a) const + { + return Pt(x - a.x, y - a.y); + } + Pt operator+(const Pt &a) const + { + return Pt(x + a.x, y + a.y); + } + Pt operator/(const double a) const + { + return Pt(x / a, y / a); + } + Pt operator*(const double a) const + { + return Pt(x * a, y * a); + } + int in() + { + return scanf("%lf %lf", &x, &y) == 2; + } +}; +double dist(Pt a, Pt b) +{ + return hypot(a.x - b.x, a.y - b.y); +} +double dist2(Pt a, Pt b) +{ + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} +double length(Pt a) +{ + return hypot(a.x, a.y); +} +double dot(Pt a, Pt b) +{ + return a.x * b.x + a.y * b.y; +} +double cross2(Pt a, Pt b) +{ + return a.x * b.y - a.y * b.x; +} +double cross(Pt o, Pt a, Pt b) +{ + return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); +} +double angle(Pt a, Pt b) +{ + return acos(dot(a, b) / length(a) / length(b)); +} +Pt rotateRadian(Pt a, double radian) +{ + double x, y; + x = a.x * cos(radian) - a.y * sin(radian); + y = a.x * sin(radian) + a.y * cos(radian); + return Pt(x, y); +} + +int main() +{ + Pt A, B, C, D[2048]; + int N, cases = 0; + while (A.in() && B.in() && C.in()) + { + scanf("%d", &N); + set S; + for (int i = 0; i < N; i++) + D[i].in(), S.insert(D[i]); + if (cases++) + puts(""); + double AB = dist2(A, B), BC = dist2(B, C), sqrAB = dist(A, B), sqrBC = dist(B, C); + double aABC = angle(A - B, C - B); + sort(D, D + N); + + vector ret; + for (int i = 0; i < N; i++) + { + for (int j = i + 1; j < N; j++) + { + if (AB < (D[j].x - D[i].x) * (D[j].x - D[i].x)) + break; + if (fabs(AB - dist2(D[i], D[j])) < eps) + { + // printf("%lf %lf %lf %lf\n", D[i].x, D[i].y, D[j].x, D[j].y); + Pt vBC, tc; + vBC = rotateRadian(D[i] - D[j], +aABC) * (sqrBC / sqrAB); + tc = D[j] + vBC; + if (S.find(tc) != S.end()) + { + ret.push_back(tc); + ret.push_back(D[i]); + ret.push_back(D[j]); + } + + vBC = rotateRadian(D[i] - D[j], -aABC) * (sqrBC / sqrAB); + tc = D[j] + vBC; + if (S.find(tc) != S.end()) + { + ret.push_back(tc); + ret.push_back(D[i]); + ret.push_back(D[j]); + } + + vBC = rotateRadian(D[j] - D[i], +aABC) * (sqrBC / sqrAB); + tc = D[i] + vBC; + if (S.find(tc) != S.end()) + { + ret.push_back(tc); + ret.push_back(D[i]); + ret.push_back(D[j]); + } + + vBC = rotateRadian(D[j] - D[i], -aABC) * (sqrBC / sqrAB); + tc = D[i] + vBC; + if (S.find(tc) != S.end()) + { + ret.push_back(tc); + ret.push_back(D[i]); + ret.push_back(D[j]); + } + } + } + } + if (ret.size()) + { + sort(ret.begin(), ret.end()); + for (int i = 0; i < 3; i++) + { + printf("%.0lf %.0lf\n", ret[i].x, ret[i].y); + } + } + } + return 0; +} +/* +5 15 +8 5 +20 10 +6 +5 17 +5 20 +20 5 +10 5 +15 20 +15 10 +*/ diff --git a/982.cpp b/982.cpp new file mode 100644 index 0000000..4ba4ef3 --- /dev/null +++ b/982.cpp @@ -0,0 +1,179 @@ +#include + +using namespace std; + +struct Frac +{ + long long x, y; + Frac(long long a = 0, long long b = 1) + { + x = a, y = b; + normal(); + } + void normal() + { + if (y < 0) + x = -x, y = -y; + long long g = llgcd(x, y); + x /= g, y /= g; + if (y < 0) + x = -x, y = -y; + } + long long llgcd(long long x, long long y) const + { + if (x == 0) + return y; + if (y == 0) + return x; + long long t; + while (x % y) + t = x, x = y, y = t % y; + return y; + } + Frac operator-(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x - vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator+(const Frac &a) const + { + long long va = 0, vb = a.y / llgcd(y, a.y) * y; + va = vb / y * x + vb / a.y * a.x; + return Frac(va, vb); + } + Frac operator*(const Frac a) const + { + long long g1 = llgcd(x, a.y), g2 = llgcd(a.x, y); + long long va = 0, vb = 0; + va = (x / g1) * (a.x / g2); + vb = (y / g2) * (a.y / g1); + return Frac(va, vb); + } + Frac operator/(const Frac a) const + { + long long g1 = llgcd(y, a.y), g2 = llgcd(x, a.x); + long long va = 0, vb = 0; + va = (a.y / g1) * (x / g2); + vb = (y / g1) * (a.x / g2); + return Frac(va, vb); + } + bool operator==(const Frac &a) const + { + return x - a.x == 0 && y - a.y == 0; + } + bool operator<(const Frac &a) const + { + return x * a.y < a.x * y; + } + void print() + { + if (y == 1) + printf("%lld", x); + else + printf("%lld/%lld", x, y); + } +}; + +Frac getX(char s[]) +{ + int a = 0, b = 0; + for (int i = 0; s[i]; i++) + { + a += (s[i] - '0') % 2 == 0; + b++; + } + return Frac(a, b); +} +Frac getY(char s[]) +{ + int a = 0, b = 0; + for (int i = 0; s[i]; i++) + { + int d = s[i] - '0'; + if (d == 2 || d == 3 || d == 5 || d == 7) + a++; + b++; + } + return Frac(a, b); +} +Frac getZ(char s[]) +{ + int a = 0, b = 0; + for (int i = 0; s[i]; i++) + { + int d = s[i] - '0'; + if (d < 5 && d % 2 == 1) + a++; + b++; + } + return Frac(a, b); +} + +int getTestXYZ(char s[], Frac &x, Frac &y, Frac &z) +{ + int n = (int)strlen(s); + if (s[1 % n] == '0' || s[3 % n] == '0' || s[5 % n] == '0') + return 0; + x = Frac(s[0 % n] - '0', s[1 % n] - '0'); + y = Frac(s[2 % n] - '0', s[3 % n] - '0'); + z = Frac(s[4 % n] - '0', s[5 % n] - '0'); + return 1; +} +int isprime(int x) +{ + if (x == 1) + return 0; + for (int i = 2; i * i <= x; i++) + if (x % i == 0) + return 0; + return 1; +} +int main() +{ + char s[64]; + while (scanf("%s", s) == 1) + { + Frac x = getX(s), y = getY(s), z = getZ(s); + Frac rx, ry, rz; + x.print(), printf(" "); + y.print(), printf(" "); + z.print(), printf("\n"); + + assert(x == Frac(0, 1) || x == Frac(1, 2) || x == Frac(1, 1)); + assert(y == Frac(0, 1) || y == Frac(1, 2) || y == Frac(1, 1)); + assert(z == Frac(0, 1) || z == Frac(1, 2) || z == Frac(1, 1)); + int f = getTestXYZ(s, rx, ry, rz); + if (!f) + { + assert(false); + continue; + } + // rx.print(), printf(" "); + // ry.print(), printf(" "); + // rz.print(), printf("\n"); + + Frac dot = rx * x + ry * y + rz * z; + if (!isprime((int)dot.x) && dot.x != 1) + { + puts("SAFE"); + } + else + { + puts("FATAL"); + } + // dot.print(); + // puts(""); + } + return 0; +} + +/* + 18540 + 385 + 1222 + 123456 + 1234 + 24556789 + 74974652 +*/ diff --git a/983.cpp b/983.cpp new file mode 100644 index 0000000..99112a8 --- /dev/null +++ b/983.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +#define FOR(ii, i0, in) for ((ii) = (i0); (ii) < (in); (ii)++) +#define FOR2d(ii, jj, i0, j0, in, jn) FOR(ii, i0, in) \ + FOR(jj, j0, jn) +long int a[1000][1000], r[1000][1000]; +int main() +{ + register int N, M, i, j, t = 0; + while (scanf("%d %d", &N, &M) == 2) + { + if (t++) + { + printf("\n"); + } + long int K = N - M + 1, r[K][K], sum = 0; + FOR2d(i, j, 0, 0, N, N) scanf("%ld", &a[N - i - 1][j]); + FOR2d(i, j, 0, 0, N, N) + { + if (i) + { + a[i][j] += a[i - 1][j]; + } + if (j) + { + a[i][j] += a[i][j - 1]; + } + if (i && j) + { + a[i][j] -= a[i - 1][j - 1]; + } + } + FOR2d(i, j, 0, 0, K, K) + { + r[i][j] = a[i + M - 1][j + M - 1]; + if (i) + { + r[i][j] -= a[i - 1][j + M - 1]; + } + if (j) + { + r[i][j] -= a[i + M - 1][j - 1]; + } + if (i && j) + { + r[i][j] += a[i - 1][j - 1]; + } + } + FOR2d(i, j, 0, 0, K, K) + { + printf("%ld\n", r[K - i - 1][j]); + sum += r[i][j]; + } + printf("%ld\n", sum); + } + return 0; +} diff --git a/985.cpp b/985.cpp new file mode 100644 index 0000000..72835f0 --- /dev/null +++ b/985.cpp @@ -0,0 +1,84 @@ +#include + +using namespace std; + +char str[1000]; +int n, m, hash_[127], map_[501][501][4], used[4][501][501], queuex[1000001], queuey[1000001], step[1000001]; +int wayx[4] = {-1, 0, 1, 0}, wayy[4] = {0, 1, 0, -1}; + +int valid(int x, int y) +{ + return x > -1 && x < n && y > -1 && y < m; +} + +int main() +{ + int i, j, k, s, x, y, tx, ty, temp; + hash_['N'] = 0; + hash_['E'] = 1; + hash_['S'] = 2; + hash_['W'] = 3; + while (scanf("%d%d", &n, &m) == 2) + { + gets(str); + memset(map_, 0, sizeof(map_)); + for (i = 0; i < n; i++) + { + for (j = 0; j < m; j++) + { + if (i == n - 1 && j == m - 1) + { + break; + } + gets(str); + for (k = 0; str[k]; k++) + { + map_[i][j][hash_[str[k]]] = 1; + } + } + } + memset(used, 0, sizeof(used)); + queuex[0] = 0; + queuey[0] = 0; + step[0] = 0; + for (i = 0, j = 1; i < j; i++) + { + x = queuex[i]; + y = queuey[i]; + s = step[i]; + for (k = 0; k < 4; k++) + { + if (map_[x][y][k]) + { + temp = s % 4; + tx = x + wayx[(temp + k) % 4]; + ty = y + wayy[(temp + k) % 4]; + if (valid(tx, ty) && !used[temp][tx][ty]) + { + if (tx == n - 1 && ty == m - 1) + { + break; + } + used[temp][tx][ty] = 1; + queuex[j] = tx; + queuey[j] = ty; + step[j++] = s + 1; + } + } + } + if (k < 4) + { + break; + } + } + if (i == j) + { + printf("no path to exit\n"); + } + else + { + printf("%d\n", s + 1); + } + } + return 0; +} diff --git a/986.cpp b/986.cpp new file mode 100644 index 0000000..5acc453 --- /dev/null +++ b/986.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +typedef long long ll; + +ll n, x, y, r, k, _n, _r, _k; +ll c[40][21][21][20][2]; +int main() +{ + while (scanf("%lld %lld %lld\n", &_n, &_r, &_k) == 3) + { + memset(c, 0, sizeof c); + for (k = 1; k < 20; k++) + { + c[1][1][0][k][1] = 1; + } + for (x = 2; x < 40; x++) + { + for (r = 0; r < 20; r++) + { + for (k = 1; k < 20; k++) + { + c[x][1][r][k][1] += c[x - 1][0][r][k][0]; + } + for (y = 1; y < 20; y++) + { + for (k = 1; k < y; k++) + { + c[x][y - 1][r][k][0] += c[x - 1][y][r][k][1]; + } + c[x][y - 1][r + 1][y][0] += c[x - 1][y][r][k][1]; + for (k = y + 1; k < 20; k++) + { + c[x][y - 1][r][k][0] += c[x - 1][y][r][k][1]; + } + for (k = 1; k < 20; k++) + { + c[x][y - 1][r][k][0] += c[x - 1][y][r][k][0]; + c[x][y + 1][r][k][1] += c[x - 1][y][r][k][0]; + c[x][y + 1][r][k][1] += c[x - 1][y][r][k][1]; + } + } + } + } + printf("%lld\n", c[_n << 1][0][_r][_k][0]); + } + return 0; +} diff --git a/988.cpp b/988.cpp new file mode 100644 index 0000000..bac0f90 --- /dev/null +++ b/988.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +vector> L(15000); +int memo[15000]; +int count(int v) +{ + if (v == 0) + { + return 1; + } + if (memo[v] != -1) + { + return memo[v]; + } + memo[v] = 0; + for (int i = L[v].size() - 1; i >= 0; i--) + { + memo[v] += count(L[v][i]); + } + return memo[v]; +} +int main() +{ + int n, m, x, ans; + bool first = false; + while (scanf("%d", &n) == 1) + { + if (first) + { + printf("\n"); + } + for (int i = 0; i < n; i++) + { + L[i].clear(); + } + ans = 0; + fill(memo, memo + n, -1); + for (int i = 0; i < n; i++) + { + scanf("%d", &m); + if (m == 0) + { + ans += count(i); + } + for (int j = 0; j < m; j++) + { + scanf("%d", &x); + L[x].push_back(i); + } + } + printf("%d\n", ans); + first = true; + } + return 0; +} diff --git a/989.cpp b/989.cpp new file mode 100644 index 0000000..ad086cc --- /dev/null +++ b/989.cpp @@ -0,0 +1,100 @@ +#include + +using namespace std; + +int B[10][10], P[10][10], n, nsq, nqt, over; +void trace(int proc) +{ + if (over) + { + return; + } + if (proc == nqt) + { + over = 1; + return; + } + int x = proc / nsq, y = proc % nsq; + if (B[x][y]) + { + trace(proc + 1); + return; + } + int i, j, fp = 0, bx = x - x % n, by = y - y % n; + for (i = 0; i < nsq; i++) + { + fp = (fp | P[x][i] | P[i][y]); + } + for (i = 0; i < n; i++) + { + for (j = 0; j < n; j++) + { + fp = (fp | P[bx + i][by + j]); + } + } + for (i = 1; i <= nsq; i++) + { + if (fp & (1 << i)) + { + continue; + } + B[x][y] = i; + P[x][y] = (1 << i); + trace(proc + 1); + if (over) + { + return; + } + P[x][y] = 0; + B[x][y] = 0; + } +} +int main() +{ + int i, j, first = 1; + while (scanf("%d", &n) == 1) + { + nsq = n * n; + nqt = nsq * nsq; + for (i = 0; i < nsq; i++) + { + for (j = 0; j < nsq; j++) + { + scanf("%d", &B[i][j]); + if (B[i][j]) + { + P[i][j] = (1 << B[i][j]); + } + else + { + P[i][j] = 0; + } + } + } + over = 0; + trace(0); + if (first) + { + first = 0; + } + else + { + printf("\n"); + } + if (!over) + { + printf("NO SOLUTION\n"); + continue; + } + for (i = 0; i < nsq; i++) + { + printf("%d", B[i][0]); + for (j = 1; j < nsq; j++) + { + printf(" %d", B[i][j]); + } + printf("\n"); + } + } + return 0; +} diff --git a/990.cpp b/990.cpp new file mode 100644 index 0000000..abaac2d --- /dev/null +++ b/990.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +/** + * Dynamic Programming, 0/1 Knapsack + */ + +enum +{ + MAX_TREASURE = 30, + MAX_DIVING_TIME = 1000 +}; + +struct Treasure +{ + int depth, gold, time_cost; +}; + +// tables for dynamic programming +int c[MAX_DIVING_TIME + 1]; +bool p[MAX_TREASURE][MAX_DIVING_TIME + 1]; + +void solve_knapsack(int total_time, Treasure *trs, int num_trs) +{ + memset(c, 0, sizeof(c)); + memset(p, 0, sizeof(p)); + for (int i = num_trs - 1; i >= 0; --i) + { + for (int j = total_time; j >= trs[i].time_cost; --j) + { + if (c[j] < c[j - trs[i].time_cost] + trs[i].gold) + { + c[j] = c[j - trs[i].time_cost] + trs[i].gold; + p[i][j] = true; + } + } + } +} + +void print_result(int total_time, Treasure *trs, int num_trs) +{ + std::ostringstream sout;// the output string + int trs_count = 0; + int total_gold = c[total_time]; + // count the total number of treasures + int w = total_time; + for (int i = 0; i < num_trs; ++i) + { + if (p[i][w]) + { + sout << trs[i].depth << " " << trs[i].gold << "\n"; + w = w - trs[i].time_cost; + trs_count++; + } + } + printf("%d\n%d\n", total_gold, trs_count); + printf("%s", sout.str().c_str()); +} + +int main() +{ + bool first_time = true; + int total_time, speed; + while (scanf("%d %d", &total_time, &speed) == 2) + { + if (!first_time) + putchar('\n'); + first_time = false; + int num_trs; + Treasure trs[MAX_TREASURE];//treasure + scanf("%d", &num_trs); + for (int i = 0; i < num_trs; ++i) + scanf("%d %d", &trs[i].depth, &trs[i].gold); + for (int i = 0; i < num_trs; ++i) + trs[i].time_cost = trs[i].depth * speed * 3; + solve_knapsack(total_time, trs, num_trs); + print_result(total_time, trs, num_trs); + } + return 0; +} diff --git a/991.cpp b/991.cpp index f9ace81..01c3844 100644 --- a/991.cpp +++ b/991.cpp @@ -1,43 +1,43 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef unsigned int uint; -typedef long long int64; -typedef unsigned long long uint64; - -using namespace std; - -int main(){ - int64 catalan[]={1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012}; - int n, ctr=0; - while(cin>>n){ - if(ctr > 0) - cout< + +using namespace std; + +/* + Name: UVA 991 + Author: zoom + Date: 22/07/11 +*/ + +#define LLU long long unsigned int +#define LLD long long double +#define FOR(i, N) for (int i = 0; i < (N); i++) +int ANS[11], j, k, first = 0; +int main() +{ + ANS[0] = 1; + ANS[1] = 1; + ANS[2] = 2; + for (int i = 3; i < 11; i++) + { + ANS[i] = 0; + j = 0, k = i - 1; + while (j < i) + { + ANS[i] += ANS[j] * ANS[k]; + j++, k--; + } + } + while (scanf("%d", &j) != EOF) + { + if (first) + { + printf("\n"); + } + else + { + first = true; + } + printf("%d\n", ANS[j]); + } + return 0; +} diff --git a/993.cpp b/993.cpp new file mode 100644 index 0000000..750ef47 --- /dev/null +++ b/993.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int getResult(int n) +{ + int i; + int res[100], p = 0; + for (i = 9; i >= 2; i--) + { + while (n % i == 0) + { + res[p++] = i; + n /= i; + } + if (n == 1) + { + break; + } + } + if (n > 9) + { + printf("-1\n"); + return 0; + } + for (i = p - 1; i >= 0; i--) + { + printf("%d", res[i]); + } + printf("\n"); + return 0; +} +int main() +{ + int k, n; + scanf("%d", &k); + while (k--) + { + scanf("%d", &n); + if (n == 0) + { + printf("10\n"); + continue; + } + if (n == 1) + { + printf("1\n"); + continue; + } + getResult(n); + } + return 0; +} diff --git a/995.cpp b/995.cpp new file mode 100644 index 0000000..c463949 --- /dev/null +++ b/995.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +int U[16], A[60], S[60], len, b; +void find(int lv) +{ + int i, j, n; + for (n = 0, i = 1; i < lv; i++) + { + n = (n * b + S[i]) % lv; + } + n = n * b % lv; + for (i = ((lv == 1) ? 1 : 0); i < b; i++) + { + if (!U[i] || (n + i) % lv) + { + continue; + } + S[lv] = i; + find(lv + 1); + if (lv >= len) + { + len = lv; + for (j = 1; j <= lv; j++) + { + A[j] = S[j]; + } + } + } +} +int main() +{ + int i, j; + char s[16]; + while (scanf("%d%s", &b, s) == 2) + { + for (i = 0; i < b; i++) + { + U[i] = 0; + } + for (i = 0; s[i]; i++) + { + U[s[i] - '0'] = 1; + } + len = 0; + find(1); + for (i = 1; i <= len; i++) + { + printf("%d", A[i]); + } + printf("\n"); + } + return 0; +} diff --git a/996.cpp b/996.cpp new file mode 100644 index 0000000..d670e5c --- /dev/null +++ b/996.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +typedef vector VI; + +bool isConstant(const VI &a) +{ + for (int i = 1; i < (int)a.size(); i++) + if (a[0] != a[i]) + { + return false; + } + return true; +} + +string toString(int n) +{ + static char buf[32]; + sprintf(buf, "%d", n); + return string(buf); +} + +pair solve(VI a, int M) +{ + int N = a.size(); + if (isConstant(a)) + { + return make_pair(1, "[" + toString(a[0]) + "]"); + } + if (M == 0) + { + return make_pair(1000, string("")); + } + VI b(N - 1); + for (int i = 0; i < N - 1; i++) + { + b[i] = a[i + 1] - a[i]; + } + pair tmp = solve(b, M - 1); + pair res(tmp.first + 1, "[" + toString(a[0]) + "+" + tmp.second + "]"); + if (res.first <= M) + { + return res; + } + b.resize(N); + for (int i = 1; i < N; i++) + { + if (a[i - 1] == 0 || a[i] % a[i - 1] != 0) + { + return res; + } + b[i] = a[i] / a[i - 1]; + } + for (int d = 1; d * d <= a[0]; d++) + { + if (a[0] % d != 0) + { + continue; + } + for (int t = 0; t < 2; t++) + { + b[0] = (t == 0 ? d : (a[0] / d)); + tmp = solve(b, M - 1); + if (tmp.first + 1 <= M) + { + res = make_pair(tmp.first + 1, "[" + toString(a[0] / b[0]) + "*" + tmp.second + "]"); + return res; + } + } + } + return res; +} + +int main() +{ + string line; + while (getline(cin, line)) + { + istringstream is(line); + VI v; + int x, M; + is >> M; + while (is >> x) + { + v.push_back(x); + } + pair s = solve(v, M); + if (s.first > M) + { + s.second = "[0]"; + } + cout << s.second << endl; + } +} diff --git a/997.cpp b/997.cpp new file mode 100644 index 0000000..87d2da5 --- /dev/null +++ b/997.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +char S[50]; +int N[50], V1[60], V2[60]; +int main() +{ + int n, term, i, j; + while (scanf("[%d%c", N, S) == 2) + { + for (n = 1; S[n - 1] != ']'; n++) + { + scanf("[%d%c", N + n, S + n); + } + for (i = 1; i < n; i++) + { + scanf("%c", S + n); + } + scanf("%d", &term); + getchar(); + for (i = 0; i < term; i++) + { + V1[i] = N[n - 1]; + } + for (i = n - 2; i >= 0; i--) + { + for (j = 0; j < term; j++) + { + V2[j] = V1[j]; + } + if (S[i] == '+') + { + V1[0] = N[i]; + for (j = 1; j < term; j++) + { + V1[j] = V1[j - 1] + V2[j - 1]; + } + } + else + { + V1[0] = N[i] * V2[0]; + for (j = 1; j < term; j++) + { + V1[j] = V1[j - 1] * V2[j]; + } + } + } + printf("%d", V1[0]); + for (i = 1; i < term; i++) + { + printf(" %d", V1[i]); + } + printf("\n"); + } + return 0; +} diff --git a/999.cpp b/999.cpp new file mode 100644 index 0000000..6d64793 --- /dev/null +++ b/999.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int main() +{ + int n, m, cases = 0; + while (scanf("%d;%d", &n, &m) == 2) + { + if (cases++) + puts(""); + int signature = (n + 3) / 4; + if (signature % m) + signature = signature / m + 1; + else + signature = signature / m; + + int base = 0; + for (int i = 1; i <= signature; i++) + { + printf("signature %d\n", i); + int page = m * 4; + for (int j = 1; j <= m; j++) + { + printf(" sheet %d: pages %d and %d (front); pages %d and %d (back)\n", + j, j * 2 + base, page - j * 2 + 1 + base, page - j * 2 + 2 + base, j * 2 - 1 + base); + } + base += page; + } + } + return 0; +} +/* +9;2 +*/